FreeCalypso > hg > fc-pcsc-tools
comparison libcommon/hexread.c @ 0:f7145c77b7fb
starting libcommon: factored out of fc-simtool
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 11 Feb 2021 22:28:45 +0000 |
parents | |
children | 94d87d05f6c5 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f7145c77b7fb |
---|---|
1 /* | |
2 * This module contains the function for reading hex files, | |
3 * to be used in the implementation of manual write commands. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <ctype.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 | |
13 read_hex_data_file(filename, databuf) | |
14 char *filename; | |
15 u_char *databuf; | |
16 { | |
17 FILE *inf; | |
18 unsigned count; | |
19 int c, c2; | |
20 | |
21 inf = fopen(filename, "r"); | |
22 if (!inf) { | |
23 perror(filename); | |
24 return(-1); | |
25 } | |
26 for (count = 0; ; count++) { | |
27 do | |
28 c = getc(inf); | |
29 while (isspace(c)); | |
30 if (c < 0) | |
31 break; | |
32 if (!isxdigit(c)) { | |
33 inv_input: fprintf(stderr, "%s: invalid hex file input\n", | |
34 filename); | |
35 fclose(inf); | |
36 return(-1); | |
37 } | |
38 c2 = getc(inf); | |
39 if (!isxdigit(c2)) | |
40 goto inv_input; | |
41 if (count >= 255) { | |
42 fprintf(stderr, "%s: hex input data is too long\n", | |
43 filename); | |
44 fclose(inf); | |
45 return(-1); | |
46 } | |
47 databuf[count] = (decode_hex_digit(c) << 4) | | |
48 decode_hex_digit(c2); | |
49 } | |
50 fclose(inf); | |
51 if (!count) { | |
52 fprintf(stderr, "%s: no hex data input found\n", filename); | |
53 return(-1); | |
54 } | |
55 return(count); | |
56 } |