FreeCalypso > hg > fc-pcsc-tools
annotate libutil/hexread.c @ 229:ed8cb3c0d312 default tip
new README, indicating repository move
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 14 Mar 2021 18:26:13 +0000 |
parents | fc1635333d81 |
children |
rev | line source |
---|---|
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the function for reading hex files, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * to be used in the implementation of manual write commands. |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <ctype.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdlib.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
129
94d87d05f6c5
libcommon: initial support for file search
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
11 extern FILE *open_script_input_file(); |
94d87d05f6c5
libcommon: initial support for file search
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
12 |
141
a1aa8ee2da85
read_hex_data_file(): add maxlen argument
Mychaela Falconia <falcon@freecalypso.org>
parents:
129
diff
changeset
|
13 read_hex_data_file(filename, databuf, maxlen) |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char *filename; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 u_char *databuf; |
141
a1aa8ee2da85
read_hex_data_file(): add maxlen argument
Mychaela Falconia <falcon@freecalypso.org>
parents:
129
diff
changeset
|
16 unsigned maxlen; |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 FILE *inf; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 unsigned count; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 int c, c2; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
129
94d87d05f6c5
libcommon: initial support for file search
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
22 inf = open_script_input_file(filename); |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 if (!inf) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 perror(filename); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 } |
201
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
27 for (count = 0; ; ) { |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 do |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 c = getc(inf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 while (isspace(c)); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 if (c < 0) |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 break; |
201
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
33 if (c == '#') { |
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
34 do |
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
35 c = getc(inf); |
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
36 while (c >= 0 && c != '\n'); |
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
37 continue; |
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
38 } |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (!isxdigit(c)) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 inv_input: fprintf(stderr, "%s: invalid hex file input\n", |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 filename); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 fclose(inf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 c2 = getc(inf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 if (!isxdigit(c2)) |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 goto inv_input; |
141
a1aa8ee2da85
read_hex_data_file(): add maxlen argument
Mychaela Falconia <falcon@freecalypso.org>
parents:
129
diff
changeset
|
48 if (count >= maxlen) { |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 fprintf(stderr, "%s: hex input data is too long\n", |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 filename); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 fclose(inf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
201
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
54 databuf[count++] = (decode_hex_digit(c) << 4) | |
fc1635333d81
allow comments in hex data files
Mychaela Falconia <falcon@freecalypso.org>
parents:
157
diff
changeset
|
55 decode_hex_digit(c2); |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 fclose(inf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 if (!count) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 fprintf(stderr, "%s: no hex data input found\n", filename); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 return(count); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } |