source: rtems/tools/build/rtems-bin2c.c @ 88c93c31

4.11
Last change on this file since 88c93c31 was 88c93c31, checked in by Ralf Corsepius <ralf.corsepius@…>, on Jul 30, 2010 at 12:09:38 PM

2010-07-30 Ralf Corsépius <ralf.corsepius@…>

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