source: rtems/tools/build/rtems-bin2c.c @ bafe269d

4.115
Last change on this file since bafe269d was bafe269d, checked in by Ralf Corsepius <ralf.corsepius@…>, on 07/29/10 at 17:12:38

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

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