FreeCalypso > hg > freecalypso-hwlab
annotate simtool/hexread.c @ 137:c331560c15a4
fc-uicc-tool: dir command implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 04 Feb 2021 04:27:09 +0000 |
parents | 4aaf722ab933 |
children | 54e33e9238b6 |
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 decode_hex_digit(c) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 if (c >= '0' && c <= '9') |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 return(c - '0'); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 if (c >= 'A' && c <= 'F') |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 return(c - 'A' + 10); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 if (c >= 'a' && c <= 'f') |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 return(c - 'a' + 10); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 read_hex_data_file(filename, databuf) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 char *filename; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 u_char *databuf; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 FILE *inf; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 unsigned count; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 int c, c2; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 inf = fopen(filename, "r"); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (!inf) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 perror(filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 for (count = 0; ; count++) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 do |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 c = getc(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 while (isspace(c)); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 if (c < 0) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 break; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (!isxdigit(c)) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 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
|
45 filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 c2 = getc(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (!isxdigit(c2)) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 goto inv_input; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 if (count >= 255) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 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
|
54 filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 databuf[count] = (decode_hex_digit(c) << 4) | |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 decode_hex_digit(c2); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (!count) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 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
|
64 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 return(count); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } |
109
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
68 |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
69 decode_hex_data_from_string(arg, databuf) |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
70 char *arg; |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
71 u_char *databuf; |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
72 { |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
73 unsigned count; |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
74 |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
75 for (count = 0; ; count++) { |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
76 while (isspace(*arg)) |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
77 arg++; |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
78 if (!*arg) |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
79 break; |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
80 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) { |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
81 fprintf(stderr, "error: invalid hex string input\n"); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
82 return(-1); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
83 } |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
84 if (count >= 255) { |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
85 fprintf(stderr, "error: hex input data is too long\n"); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
86 return(-1); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
87 } |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
88 databuf[count] = (decode_hex_digit(arg[0]) << 4) | |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
89 decode_hex_digit(arg[1]); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
90 arg += 2; |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
91 } |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
92 if (!count) { |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
93 fprintf(stderr, "error: empty hex string argument\n"); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
94 return(-1); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
95 } |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
96 return(count); |
4aaf722ab933
fc-simtool: update-bin-imm command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
101
diff
changeset
|
97 } |