FreeCalypso > hg > fc-sim-tools
comparison libutil/hexread.c @ 8:34bbb0585cab
libutil: import from previous fc-pcsc-tools version
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 14 Mar 2021 05:42:37 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 7:b25d4dfe5798 | 8:34bbb0585cab |
|---|---|
| 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 <stdio.h> | |
| 9 #include <stdlib.h> | |
| 10 | |
| 11 extern FILE *open_script_input_file(); | |
| 12 | |
| 13 read_hex_data_file(filename, databuf, maxlen) | |
| 14 char *filename; | |
| 15 u_char *databuf; | |
| 16 unsigned maxlen; | |
| 17 { | |
| 18 FILE *inf; | |
| 19 unsigned count; | |
| 20 int c, c2; | |
| 21 | |
| 22 inf = open_script_input_file(filename); | |
| 23 if (!inf) { | |
| 24 perror(filename); | |
| 25 return(-1); | |
| 26 } | |
| 27 for (count = 0; ; ) { | |
| 28 do | |
| 29 c = getc(inf); | |
| 30 while (isspace(c)); | |
| 31 if (c < 0) | |
| 32 break; | |
| 33 if (c == '#') { | |
| 34 do | |
| 35 c = getc(inf); | |
| 36 while (c >= 0 && c != '\n'); | |
| 37 continue; | |
| 38 } | |
| 39 if (!isxdigit(c)) { | |
| 40 inv_input: fprintf(stderr, "%s: invalid hex file input\n", | |
| 41 filename); | |
| 42 fclose(inf); | |
| 43 return(-1); | |
| 44 } | |
| 45 c2 = getc(inf); | |
| 46 if (!isxdigit(c2)) | |
| 47 goto inv_input; | |
| 48 if (count >= maxlen) { | |
| 49 fprintf(stderr, "%s: hex input data is too long\n", | |
| 50 filename); | |
| 51 fclose(inf); | |
| 52 return(-1); | |
| 53 } | |
| 54 databuf[count++] = (decode_hex_digit(c) << 4) | | |
| 55 decode_hex_digit(c2); | |
| 56 } | |
| 57 fclose(inf); | |
| 58 if (!count) { | |
| 59 fprintf(stderr, "%s: no hex data input found\n", filename); | |
| 60 return(-1); | |
| 61 } | |
| 62 return(count); | |
| 63 } |
