1 | /* |
---|
2 | * bin2c.c |
---|
3 | * |
---|
4 | * convert a binary file into a C source array. |
---|
5 | * |
---|
6 | * Origin: |
---|
7 | * put into the public domain by Sandro Sigala |
---|
8 | * http://www.wxwidgets.org/wiki/index.php/Embedding_PNG_Images-Bin2c_In_C |
---|
9 | * |
---|
10 | * Subsequently modified by Joel Sherrill <joel.sherrill@oarcorp.com> |
---|
11 | * to add a number of capabilities not in the original. |
---|
12 | * |
---|
13 | * syntax: bin2c [-c] [-z] <input_file> <output_file> |
---|
14 | * |
---|
15 | * -c do NOT add the "const" keyword to definition |
---|
16 | * -s add the "static" keywork to definition |
---|
17 | * -v verbose |
---|
18 | * -z terminate the array with a zero (useful for embedded C strings) |
---|
19 | * |
---|
20 | * examples: |
---|
21 | * bin2c -c myimage.png myimage_png.cpp |
---|
22 | * bin2c -z sometext.txt sometext_txt.cpp |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <ctype.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <string.h> |
---|
30 | |
---|
31 | #ifndef PATH_MAX |
---|
32 | #define PATH_MAX 1024 |
---|
33 | #endif |
---|
34 | |
---|
35 | int useconst = 1; |
---|
36 | int usestatic = 0; |
---|
37 | int verbose = 0; |
---|
38 | int zeroterminated = 0; |
---|
39 | |
---|
40 | int myfgetc(FILE *f) |
---|
41 | { |
---|
42 | int c = fgetc(f); |
---|
43 | if (c == EOF && zeroterminated) { |
---|
44 | zeroterminated = 0; |
---|
45 | return 0; |
---|
46 | } |
---|
47 | return c; |
---|
48 | } |
---|
49 | |
---|
50 | void process(const char *ifname, const char *ofname) |
---|
51 | { |
---|
52 | FILE *ifile, *ocfile, *ohfile; |
---|
53 | char buf[PATH_MAX], *p; |
---|
54 | char obasename[PATH_MAX]; |
---|
55 | char ocname[PATH_MAX]; |
---|
56 | char ohname[PATH_MAX]; |
---|
57 | const char *cp; |
---|
58 | size_t len; |
---|
59 | |
---|
60 | /* Error check */ |
---|
61 | if ( !ifname || !ofname ) { |
---|
62 | fprintf(stderr, "process has NULL filename\n"); |
---|
63 | exit(1); |
---|
64 | } |
---|
65 | |
---|
66 | strncpy( obasename, ofname, PATH_MAX ); |
---|
67 | len = strlen( obasename ); |
---|
68 | if ( obasename[len-2] == '.' && obasename[len-1] == 'c' ) |
---|
69 | obasename[len-2] = '\0'; |
---|
70 | |
---|
71 | sprintf( ocname, "%s.c", obasename ); |
---|
72 | sprintf( ohname, "%s.h", obasename ); |
---|
73 | |
---|
74 | if ( verbose ) { |
---|
75 | fprintf( |
---|
76 | stderr, |
---|
77 | "in file: %s\n" |
---|
78 | "c file: %s\n" |
---|
79 | "h file: %s\n", |
---|
80 | ifname, |
---|
81 | ocname, |
---|
82 | ohname |
---|
83 | ); |
---|
84 | } |
---|
85 | |
---|
86 | /* Open input and output files */ |
---|
87 | ifile = fopen(ifname, "rb"); |
---|
88 | if (ifile == NULL) { |
---|
89 | fprintf(stderr, "cannot open %s for reading\n", ifname); |
---|
90 | exit(1); |
---|
91 | } |
---|
92 | ocfile = fopen(ocname, "wb"); |
---|
93 | if (ocfile == NULL) { |
---|
94 | fprintf(stderr, "cannot open %s for writing\n", ocname); |
---|
95 | exit(1); |
---|
96 | } |
---|
97 | |
---|
98 | ohfile = fopen(ohname, "wb"); |
---|
99 | if (ohfile == NULL) { |
---|
100 | fprintf(stderr, "cannot open %s for writing\n", ohname); |
---|
101 | exit(1); |
---|
102 | } |
---|
103 | |
---|
104 | /* find basename */ |
---|
105 | if ((cp = strrchr(ifname, '/')) != NULL) |
---|
106 | ++cp; |
---|
107 | else { |
---|
108 | if ((cp = strrchr(ifname, '\\')) != NULL) |
---|
109 | ++cp; |
---|
110 | else |
---|
111 | cp = ifname; |
---|
112 | } |
---|
113 | strcpy(buf, cp); |
---|
114 | for (p = buf; *p != '\0'; ++p) |
---|
115 | if (!isalnum(*p)) |
---|
116 | *p = '_'; |
---|
117 | |
---|
118 | /* print C file header */ |
---|
119 | fprintf( |
---|
120 | ocfile, |
---|
121 | "/*\n" |
---|
122 | " * Declarations for C structure representing binary file %s\n" |
---|
123 | " *\n" |
---|
124 | " * WARNING: Automatically generated -- do not edit!\n" |
---|
125 | " */\n" |
---|
126 | "\n" |
---|
127 | "#include <sys/types.h>\n" |
---|
128 | "\n", |
---|
129 | ifname |
---|
130 | ); |
---|
131 | |
---|
132 | /* print structure */ |
---|
133 | fprintf( |
---|
134 | ocfile, |
---|
135 | "%s%sunsigned char %s[] = {\n ", |
---|
136 | ((usestatic) ? "static " : ""), |
---|
137 | ((useconst) ? "const " : ""), |
---|
138 | buf |
---|
139 | ); |
---|
140 | int c, col = 1; |
---|
141 | while ((c = myfgetc(ifile)) != EOF) { |
---|
142 | if (col >= 78 - 6) { |
---|
143 | fprintf(ocfile, "\n "); |
---|
144 | col = 1; |
---|
145 | } |
---|
146 | fprintf(ocfile, "0x%.2x, ", c); |
---|
147 | col += 6; |
---|
148 | |
---|
149 | } |
---|
150 | fprintf(ocfile, "\n};\n"); |
---|
151 | |
---|
152 | /* print sizeof */ |
---|
153 | fprintf( |
---|
154 | ocfile, |
---|
155 | "\n" |
---|
156 | "%s%ssize_t %s_size = sizeof(%s);\n", |
---|
157 | ((usestatic) ? "static " : ""), |
---|
158 | ((useconst) ? "const " : ""), |
---|
159 | buf, |
---|
160 | buf |
---|
161 | ); |
---|
162 | |
---|
163 | /*****************************************************************/ |
---|
164 | /****** END OF C FILE *****/ |
---|
165 | /*****************************************************************/ |
---|
166 | |
---|
167 | /* print H file header */ |
---|
168 | fprintf( |
---|
169 | ohfile, |
---|
170 | "/*\n" |
---|
171 | " * Extern declarations for C structure representing binary file %s\n" |
---|
172 | " *\n" |
---|
173 | " * WARNING: Automatically generated -- do not edit!\n" |
---|
174 | " */\n" |
---|
175 | "\n" |
---|
176 | "#ifndef __%s_h\n" |
---|
177 | "#define __%s_h\n" |
---|
178 | "\n" |
---|
179 | "#include <sys/types.h>\n" |
---|
180 | "\n", |
---|
181 | obasename, /* header */ |
---|
182 | obasename, /* ifndef */ |
---|
183 | obasename /* define */ |
---|
184 | ); |
---|
185 | |
---|
186 | /* print structure */ |
---|
187 | fprintf( |
---|
188 | ohfile, |
---|
189 | "extern %s%sunsigned char %s[];", |
---|
190 | ((usestatic) ? "static " : ""), |
---|
191 | ((useconst) ? "const " : ""), |
---|
192 | buf |
---|
193 | ); |
---|
194 | /* print sizeof */ |
---|
195 | fprintf( |
---|
196 | ohfile, |
---|
197 | "\n" |
---|
198 | "extern %s%ssize_t %s_size;\n", |
---|
199 | ((usestatic) ? "static " : ""), |
---|
200 | ((useconst) ? "const " : ""), |
---|
201 | buf |
---|
202 | ); |
---|
203 | |
---|
204 | fprintf( |
---|
205 | ohfile, |
---|
206 | "\n" |
---|
207 | "#endif\n" |
---|
208 | ); |
---|
209 | |
---|
210 | /*****************************************************************/ |
---|
211 | /****** END OF H FILE *****/ |
---|
212 | /*****************************************************************/ |
---|
213 | |
---|
214 | fclose(ifile); |
---|
215 | fclose(ocfile); |
---|
216 | fclose(ohfile); |
---|
217 | } |
---|
218 | |
---|
219 | void usage(void) |
---|
220 | { |
---|
221 | fprintf( |
---|
222 | stderr, |
---|
223 | "usage: bin2c [-csvz] <input_file> <output_file>\n" |
---|
224 | " <input_file> is the binary file to convert\n" |
---|
225 | " <output_file> should not have a .c or .h extension\n" |
---|
226 | "\n" |
---|
227 | " -c - do NOT use const in declaration\n" |
---|
228 | " -s - do use static in declaration\n" |
---|
229 | " -v - verbose\n" |
---|
230 | " -z - add zero terminator\n" |
---|
231 | ); |
---|
232 | exit(1); |
---|
233 | } |
---|
234 | |
---|
235 | int main(int argc, char **argv) |
---|
236 | { |
---|
237 | while (argc > 3) { |
---|
238 | if (!strcmp(argv[1], "-c")) { |
---|
239 | useconst = 0; |
---|
240 | --argc; |
---|
241 | ++argv; |
---|
242 | } else if (!strcmp(argv[1], "-s")) { |
---|
243 | usestatic = 1; |
---|
244 | --argc; |
---|
245 | ++argv; |
---|
246 | } else if (!strcmp(argv[1], "-v")) { |
---|
247 | usestatic = 1; |
---|
248 | --argc; |
---|
249 | ++argv; |
---|
250 | } else if (!strcmp(argv[1], "-z")) { |
---|
251 | zeroterminated = 1; |
---|
252 | --argc; |
---|
253 | ++argv; |
---|
254 | } else { |
---|
255 | usage(); |
---|
256 | } |
---|
257 | } |
---|
258 | if (argc != 3) { |
---|
259 | usage(); |
---|
260 | } |
---|
261 | |
---|
262 | /* process( input_file, output_basename ) */ |
---|
263 | process(argv[1], argv[2]); |
---|
264 | return 0; |
---|
265 | } |
---|
266 | |
---|