annotate utils/gen-hex-c.c @ 50:0db059f4632d

efr-sid OS#6538: more sensible 2-bit errors
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 12 Aug 2024 02:49:28 +0000
parents 871e83f0cb76
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads an arbitrary binary file and converts it into
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * ASCII hex output of the form '0xXX,0xXX,...' intended for inclusion
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * into C sources as a const char array.
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 main(argc, argv)
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 char **argv;
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 {
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 FILE *inf, *outf;
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 int c, lcnt;
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 if (argc != 3) {
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 fprintf(stderr, "usage: %s input.bin output.c\n", argv[0]);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 exit(1);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 }
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 inf = fopen(argv[1], "r");
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 if (!inf) {
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 perror(argv[1]);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 exit(1);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 }
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 outf = fopen(argv[2], "w");
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (!outf) {
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 perror(argv[2]);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 exit(1);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 lcnt = 0;
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 for (;;) {
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 c = getc(inf);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (c < 0)
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 break;
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 fprintf(outf, "0x%02X,", c);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 lcnt++;
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (lcnt >= 16) {
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 putc('\n', outf);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 lcnt = 0;
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (lcnt)
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 putc('\n', outf);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 exit(0);
871e83f0cb76 utils: gen-hex-c utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }