FreeCalypso > hg > freecalypso-hwlab
comparison uicc/hexread.c @ 145:14dee03e9675
fc-uicc-tool: low-level write commands ported over
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sat, 06 Feb 2021 02:17:51 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 144:429a8f80426e | 145:14dee03e9675 |
|---|---|
| 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 } |
