bcc.c

Tue, 13 Apr 2021 21:30:13 +0200

author
David Demelier <markand@malikania.fr>
date
Tue, 13 Apr 2021 21:30:13 +0200
changeset 12
40adb00674ba
parent 9
ab5c41277358
permissions
-rw-r--r--

misc: added signature for changeset 737e7df59283

0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * bcc.c -- binary to C/C++ arrays converter
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
9
ab5c41277358 misc: update copyright years
David Demelier <markand@malikania.fr>
parents: 7
diff changeset
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <errno.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <stdarg.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <stdio.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 #include <stdlib.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include <stdnoreturn.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 #include <string.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include <unistd.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
26
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 static const char *charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 static char findentchar = '\t';
7
43056d03d89a bcc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 4
diff changeset
29 static int findent = 1, fconst, fnull, fstatic;
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
30
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 noreturn static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 usage(void)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 {
4
327e7d392f19 bcc: remove references to signedness
David Demelier <markand@malikania.fr>
parents: 3
diff changeset
34 fprintf(stderr, "usage: bcc [-0cs] [-I tab-indent] [-i space-indent] input variable\n");
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 exit(1);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
37
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 noreturn static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 die(const char *fmt, ...)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 va_list ap;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
42
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
43 va_start(ap, fmt);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 fputs("abort: ", stderr);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 vfprintf(stderr, fmt, ap);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 va_end(ap);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 exit(1);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
49
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 static char *
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 mangle(char *variable)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 char *p;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 size_t pos;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
55
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 /* Remove extension. */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 if ((p = strrchr(variable, '.')))
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 *p = '\0';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
59
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 /* Remove disallowed characters. */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
61 while ((pos = strspn(variable, charset)) != strlen(variable))
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 variable[pos] = '_';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
63
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
64 return variable;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
66
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 indent(void)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 for (int i = 0; i < findent; ++i)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
71 putchar(findentchar);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
72 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
73
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 put(int ch)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 {
3
b91e030a2040 bcc: generates only unsigned arrays
David Demelier <markand@malikania.fr>
parents: 0
diff changeset
77 printf("0x%02hhx", (unsigned char)ch);
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
79
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 process(const char *input, const char *variable)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 FILE *fp;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 int ch, col = 0;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
85
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 if (strcmp(input, "-") == 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
87 fp = stdin;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 else if (!(fp = fopen(input, "rb")))
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 die("%s: %s\n", input, strerror(errno));
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
90
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 if (fstatic)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 printf("static ");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 if (fconst)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 printf("const ");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
95
3
b91e030a2040 bcc: generates only unsigned arrays
David Demelier <markand@malikania.fr>
parents: 0
diff changeset
96 printf("unsigned char %s[] = {\n", variable);
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
97
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 for (ch = fgetc(fp); ch != EOF; ) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
99 if (col == 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 indent();
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
101
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 put(ch);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
103
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
104 if ((ch = fgetc(fp)) != EOF || fnull)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
105 printf(",%s", col < 3 ? " " : "");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
106
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 if (++col == 4) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
108 col = 0;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 putchar('\n');
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
110 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
111
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
112 /* Add final '\0' if required. */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
113 if (ch == EOF && fnull) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
114 if (col++ == 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
115 indent();
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
116
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
117 put(0);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
118 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
119 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
120
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
121 if (col != 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
122 printf("\n");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
123
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
124 puts("};");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
125 fclose(fp);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
126 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
127
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
128 int
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
129 main(int argc, char **argv)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
130 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
131 int ch;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
132
3
b91e030a2040 bcc: generates only unsigned arrays
David Demelier <markand@malikania.fr>
parents: 0
diff changeset
133 while ((ch = getopt(argc, argv, "0cI:i:s")) != -1) {
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
134 switch (ch) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
135 case '0':
7
43056d03d89a bcc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 4
diff changeset
136 fnull = 1;
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
137 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
138 case 'c':
7
43056d03d89a bcc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 4
diff changeset
139 fconst = 1;
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
140 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
141 case 'I':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
142 findentchar = '\t';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
143 findent = atoi(optarg);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
144 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
145 case 'i':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
146 findentchar = ' ';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
147 findent = atoi(optarg);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
148 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
149 case 's':
7
43056d03d89a bcc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 4
diff changeset
150 fstatic = 1;
0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
151 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
152 default:
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
153 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
154 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
155 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
156
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
157 argc -= optind;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
158 argv += optind;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
159
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
160 if (argc < 2)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
161 usage();
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
162
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
163 process(argv[0], mangle(argv[1]));
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
164 }

mercurial