FreeCalypso > hg > freecalypso-hwlab
comparison simtool/hexread.c @ 101:454ff8bd0b83
fc-simtool: update-bin command implemented
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Mon, 25 Jan 2021 01:54:43 +0000 |
| parents | |
| children | 4aaf722ab933 |
comparison
equal
deleted
inserted
replaced
| 100:fa7005185b84 | 101:454ff8bd0b83 |
|---|---|
| 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 decode_hex_digit(c) | |
| 14 { | |
| 15 if (c >= '0' && c <= '9') | |
| 16 return(c - '0'); | |
| 17 if (c >= 'A' && c <= 'F') | |
| 18 return(c - 'A' + 10); | |
| 19 if (c >= 'a' && c <= 'f') | |
| 20 return(c - 'a' + 10); | |
| 21 return(-1); | |
| 22 } | |
| 23 | |
| 24 read_hex_data_file(filename, databuf) | |
| 25 char *filename; | |
| 26 u_char *databuf; | |
| 27 { | |
| 28 FILE *inf; | |
| 29 unsigned count; | |
| 30 int c, c2; | |
| 31 | |
| 32 inf = fopen(filename, "r"); | |
| 33 if (!inf) { | |
| 34 perror(filename); | |
| 35 return(-1); | |
| 36 } | |
| 37 for (count = 0; ; count++) { | |
| 38 do | |
| 39 c = getc(inf); | |
| 40 while (isspace(c)); | |
| 41 if (c < 0) | |
| 42 break; | |
| 43 if (!isxdigit(c)) { | |
| 44 inv_input: fprintf(stderr, "%s: invalid hex file input\n", | |
| 45 filename); | |
| 46 fclose(inf); | |
| 47 return(-1); | |
| 48 } | |
| 49 c2 = getc(inf); | |
| 50 if (!isxdigit(c2)) | |
| 51 goto inv_input; | |
| 52 if (count >= 255) { | |
| 53 fprintf(stderr, "%s: hex input data is too long\n", | |
| 54 filename); | |
| 55 fclose(inf); | |
| 56 return(-1); | |
| 57 } | |
| 58 databuf[count] = (decode_hex_digit(c) << 4) | | |
| 59 decode_hex_digit(c2); | |
| 60 } | |
| 61 fclose(inf); | |
| 62 if (!count) { | |
| 63 fprintf(stderr, "%s: no hex data input found\n", filename); | |
| 64 return(-1); | |
| 65 } | |
| 66 return(count); | |
| 67 } |
