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

4.115
Last change on this file since d751cec was d751cec, checked in by Joel Sherrill <joel.sherrill@…>, on 05/17/11 at 20:39:40
  • tools/build/.cvsignore, tools/build/ChangeLog, tools/build/Makefile.am, tools/build/README, tools/build/binpatch.c, tools/build/cklength.c, tools/build/config.h.in, tools/build/configure.ac, tools/build/cvsignore-add.sh, tools/build/doxy-filter, tools/build/eolstrip.c, tools/build/install-if-change.in, tools/build/multigen, tools/build/packhex.c, tools/build/rtems-bin2c.c, tools/build/search-id.sh, tools/build/unhex.c, tools/cpu/.cvsignore, tools/cpu/ChangeLog, tools/cpu/Makefile.am, tools/cpu/configure.ac, tools/cpu/generic/.cvsignore, tools/cpu/generic/ChangeLog, tools/cpu/generic/Makefile.am, tools/cpu/generic/configure.ac, tools/cpu/generic/size_rtems.in, tools/cpu/nios2/.cvsignore, tools/cpu/nios2/ChangeLog, tools/cpu/nios2/Makefile.am, tools/cpu/nios2/README, tools/cpu/nios2/bridges.c, tools/cpu/nios2/bridges.h, tools/cpu/nios2/clocks.c, tools/cpu/nios2/clocks.h, tools/cpu/nios2/configure.ac, tools/cpu/nios2/devices.c, tools/cpu/nios2/devices.h, tools/cpu/nios2/linkcmds.c, tools/cpu/nios2/linkcmds.h, tools/cpu/nios2/memory.c, tools/cpu/nios2/memory.h, tools/cpu/nios2/nios2gen.c, tools/cpu/nios2/output.c, tools/cpu/nios2/output.h, tools/cpu/nios2/ptf.c, tools/cpu/nios2/ptf.h, tools/cpu/nios2/sample.ptf, tools/cpu/sh/.cvsignore, tools/cpu/sh/AUTHORS, tools/cpu/sh/COPYING, tools/cpu/sh/ChangeLog, tools/cpu/sh/Makefile.am, tools/cpu/sh/TODO, tools/cpu/sh/configure.ac, tools/cpu/sh/sci.c, tools/cpu/sh/sci.h, tools/cpu/sh/shgen.c: New files.
  • Property mode set to 100644
File size: 6.2 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 ( len >= 2 ) {
74    if ( obasename[len-2] == '.' ) {
75      if ( (obasename[len-1] == 'c') || (obasename[len-1] == 'h') )
76        obasename[len-2] = '\0';
77    }
78  }
79
80  sprintf( ocname, "%s.c", obasename );
81  sprintf( ohname, "%s.h", obasename );
82
83  if ( verbose ) {
84    fprintf(
85      stderr,
86      "in file: %s\n"
87      "c file: %s\n"
88      "h file: %s\n",
89      ifname,
90      ocname,
91      ohname
92    );
93  }
94
95  /* Open input and output files */
96  ifile = fopen(ifname, "rb");
97  if (ifile == NULL) {
98    fprintf(stderr, "cannot open %s for reading\n", ifname);
99    exit(1);
100  }
101 
102  if ( createC ) {
103  ocfile = fopen(ocname, "wb");
104  if (ocfile == NULL) {
105    fprintf(stderr, "cannot open %s for writing\n", ocname);
106    exit(1);
107  }
108  }
109 
110  if ( createH ) {
111  ohfile = fopen(ohname, "wb");
112  if (ohfile == NULL) {
113    fprintf(stderr, "cannot open %s for writing\n", ohname);
114    exit(1);
115  }
116  }
117 
118  /* find basename */
119  char *ifbasename = strdup(ifname);
120  ifbasename = basename(ifbasename);
121 
122  strcpy(buf, ifbasename);
123  for (p = buf; *p != '\0'; ++p)
124    if (!isalnum(*p))
125      *p = '_';
126
127  if ( createC ) {
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    ifbasename
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  );
172  } /* createC */
173 
174  /*****************************************************************/
175  /******                    END OF C FILE                     *****/
176  /*****************************************************************/
177
178  if ( createH ) {
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    ifbasename,  /* 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  );
221  } /* createH */
222 
223  /*****************************************************************/
224  /******                    END OF H FILE                     *****/
225  /*****************************************************************/
226
227  fclose(ifile);
228  if ( createC ) { fclose(ocfile); }
229  if ( createH ) { fclose(ohfile); }
230}
231
232void usage(void)
233{
234  fprintf(
235     stderr,
236     "usage: bin2c [-csvzCH] <input_file> <output_file>\n"
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"
244     "  -H - create c-header only\n"
245     "  -C - create c-source file only\n"
246    );
247  exit(1);
248}
249
250int main(int argc, char **argv)
251{
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      verbose = 1;
263      --argc;
264      ++argv;
265    } else if (!strcmp(argv[1], "-z")) {
266      zeroterminated = 1;
267      --argc;
268      ++argv;
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;
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;
290}
291
Note: See TracBrowser for help on using the repository browser.