FreeCalypso > hg > fc-pcsc-tools
comparison libutil/hexread.c @ 157:f064dbcc5f41
libutil split from libcommon
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Fri, 26 Feb 2021 20:19:58 +0000 |
| parents | libcommon/hexread.c@a1aa8ee2da85 |
| children | fc1635333d81 |
comparison
equal
deleted
inserted
replaced
| 156:5f1f3f6fd865 | 157:f064dbcc5f41 |
|---|---|
| 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; ; count++) { | |
| 28 do | |
| 29 c = getc(inf); | |
| 30 while (isspace(c)); | |
| 31 if (c < 0) | |
| 32 break; | |
| 33 if (!isxdigit(c)) { | |
| 34 inv_input: fprintf(stderr, "%s: invalid hex file input\n", | |
| 35 filename); | |
| 36 fclose(inf); | |
| 37 return(-1); | |
| 38 } | |
| 39 c2 = getc(inf); | |
| 40 if (!isxdigit(c2)) | |
| 41 goto inv_input; | |
| 42 if (count >= maxlen) { | |
| 43 fprintf(stderr, "%s: hex input data is too long\n", | |
| 44 filename); | |
| 45 fclose(inf); | |
| 46 return(-1); | |
| 47 } | |
| 48 databuf[count] = (decode_hex_digit(c) << 4) | | |
| 49 decode_hex_digit(c2); | |
| 50 } | |
| 51 fclose(inf); | |
| 52 if (!count) { | |
| 53 fprintf(stderr, "%s: no hex data input found\n", filename); | |
| 54 return(-1); | |
| 55 } | |
| 56 return(count); | |
| 57 } |
