annotate libutil/hexread.c @ 178:cd6a884b8657

data: grcard2-blank-state captured
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 02 Mar 2021 05:10:12 +0000
parents f064dbcc5f41
children fc1635333d81
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 for (count = 0; ; count++) {
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;
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (!isxdigit(c)) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 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
35 filename);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 fclose(inf);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(-1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 c2 = getc(inf);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (!isxdigit(c2))
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 goto inv_input;
141
a1aa8ee2da85 read_hex_data_file(): add maxlen argument
Mychaela Falconia <falcon@freecalypso.org>
parents: 129
diff changeset
42 if (count >= maxlen) {
0
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 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
44 filename);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 fclose(inf);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 return(-1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 databuf[count] = (decode_hex_digit(c) << 4) |
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 decode_hex_digit(c2);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
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 if (!count) {
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 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
54 return(-1);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 return(count);
f7145c77b7fb starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }