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