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

4.104.114.84.95
Last change on this file since c82e98f4 was c82e98f4, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 10, 2007 at 11:27:44 AM

2007-09-10 Joel Sherrill <joel.sherrill@…>

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