FreeCalypso > hg > freecalypso-hwlab
annotate simtool/hexread.c @ 160:19b96c52348f
fc-uicc-tool: readef command ported over
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 07 Feb 2021 04:15:59 +0000 |
parents | 54e33e9238b6 |
children |
rev | line source |
---|---|
101
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the function for reading hex files, |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * to be used in the implementation of manual write commands. |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <ctype.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <string.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <strings.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdio.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdlib.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 read_hex_data_file(filename, databuf) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char *filename; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 u_char *databuf; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 FILE *inf; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 unsigned count; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 int c, c2; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 inf = fopen(filename, "r"); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 if (!inf) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 perror(filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 for (count = 0; ; count++) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 do |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 c = getc(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 while (isspace(c)); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 if (c < 0) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 break; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 if (!isxdigit(c)) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 inv_input: fprintf(stderr, "%s: invalid hex file input\n", |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 c2 = getc(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (!isxdigit(c2)) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 goto inv_input; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 if (count >= 255) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 fprintf(stderr, "%s: hex input data is too long\n", |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 databuf[count] = (decode_hex_digit(c) << 4) | |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 decode_hex_digit(c2); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 if (!count) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 fprintf(stderr, "%s: no hex data input found\n", filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 return(count); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |