source: rtems-tools/misc/bin2c/rtems-bin2c.c

Last change on this file was 3c0e4f7, checked in by Christian Mauderer <christian.mauderer@…>, on 02/14/22 at 15:14:32

bin2c: Add option for alignment

Sometimes it's useful if structures are aligned. This patch add a

-A alignment

option. Note that this doesn't check for valid alignments. It accepts
any positive number in decimal or hex format. If for example an
alignment of 7 is specified, the compiler will complain that it is not a
power of 2. But it's not really useful to duplicate this check here.

  • Property mode set to 100644
File size: 8.4 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#define _XOPEN_SOURCE 700 // for strnlen
29#include <ctype.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <libgen.h>
34
35#ifndef PATH_MAX
36#define PATH_MAX 1024
37#endif
38
39int useconst = 1;
40int usestatic = 0;
41int verbose = 0;
42int zeroterminated = 0;
43int createC = 1;
44int createH = 1;
45unsigned int align = 0;
46
47static void sanitize_file_name(char *p)
48{
49  while (*p != '\0') {
50    if (!isalnum((unsigned char)*p)) /* cast to avoid negative indexing */
51      *p = '_';
52    ++p;
53  }
54}
55
56int myfgetc(FILE *f)
57{
58  int c = fgetc(f);
59  if (c == EOF && zeroterminated) {
60    zeroterminated = 0;
61    return 0;
62  }
63  return c;
64}
65
66void process(const char *ifname, const char *ofname, const char *forced_name)
67{
68  FILE *ifile, *ocfile, *ohfile;
69  char buf[PATH_MAX+1];
70  char obasename[PATH_MAX+1];
71  char ocname[PATH_MAX+5];
72  char ohname[PATH_MAX+5];
73  size_t len;
74
75  ocfile = NULL;
76  ohfile = NULL;
77
78  /* Error check */
79  if ( !ifname || !ofname ) {
80    fprintf(stderr, "process has NULL filename\n");
81    exit(1);
82  }
83
84  strncpy( obasename, ofname, PATH_MAX );
85  len = strnlen( obasename, PATH_MAX );
86  if ( len >= 2 ) {
87    if ( obasename[len-2] == '.' ) {
88      if ( (obasename[len-1] == 'c') || (obasename[len-1] == 'h') )
89        obasename[len-2] = '\0';
90    }
91  }
92
93  sprintf( ocname, "%s.c", obasename );
94  sprintf( ohname, "%s.h", obasename );
95
96  if ( verbose ) {
97    fprintf(
98      stderr,
99      "in file: %s\n"
100      "c file: %s\n"
101      "h file: %s\n",
102      ifname,
103      ocname,
104      ohname
105    );
106  }
107
108  /* Open input and output files */
109  ifile = fopen(ifname, "rb");
110  if (ifile == NULL) {
111    fprintf(stderr, "cannot open %s for reading\n", ifname);
112    exit(1);
113  }
114
115  if ( createC ) {
116    ocfile = fopen(ocname, "wb");
117    if (ocfile == NULL) {
118      fprintf(stderr, "cannot open %s for writing\n", ocname);
119      exit(1);
120    }
121  }
122
123  if ( createH ) {
124    ohfile = fopen(ohname, "wb");
125    if (ohfile == NULL) {
126      fprintf(stderr, "cannot open %s for writing\n", ohname);
127      exit(1);
128    }
129  }
130
131  /* find basename */
132  char *ifbasename_to_free =
133    forced_name != NULL ? strdup(forced_name) : strdup(ifname);
134  if ( ifbasename_to_free == NULL ) {
135    fprintf(stderr, "cannot allocate memory\n" );
136    fclose(ifile);
137    if ( createC ) { fclose(ocfile); }
138    if ( createH ) { fclose(ohfile); }
139    exit(1);
140  }
141
142  char *ifbasename;
143  ifbasename = basename(ifbasename_to_free);
144
145  /* Ensure length of ifbasename is shorter than length of buf */
146  if (strlen(ifbasename) > PATH_MAX+1) {
147    fprintf(
148      stderr,
149      "error: Base name of %s is too long.\n",
150      ifbasename
151    );
152    fclose(ifile);
153    if ( createC ) { fclose(ocfile); }
154    if ( createH ) { fclose(ohfile); }
155    exit(1);
156  }
157
158  strcpy(buf, ifbasename);
159  sanitize_file_name(buf);
160
161  if ( createC ) {
162    /* print C file header */
163    fprintf(
164      ocfile,
165      "/*\n"
166      " *  Declarations for C structure representing binary file %s\n"
167      " *\n"
168      " *  WARNING: Automatically generated -- do not edit!\n"
169      " */\n"
170      "\n"
171      "#include <sys/types.h>\n"
172      "\n",
173      ifbasename
174    );
175
176    /* print structure */
177    fprintf(
178      ocfile,
179      "%s%sunsigned char %s[] ",
180      ((usestatic) ? "static " : ""),
181      ((useconst) ? "const " : ""),
182      buf
183    );
184    if (align > 0) {
185      fprintf(
186        ocfile,
187        "__attribute__(( __aligned__(%d) )) ",
188        align
189      );
190    }
191    fprintf(
192      ocfile,
193      "= {\n  "
194    );
195    int c, col = 1;
196    while ((c = myfgetc(ifile)) != EOF) {
197      if (col >= 78 - 6) {
198        fprintf(ocfile, "\n  ");
199        col = 1;
200      }
201      fprintf(ocfile, "0x%.2x, ", c);
202      col += 6;
203
204    }
205    fprintf(ocfile, "\n};\n");
206
207    /* print sizeof */
208    fprintf(
209      ocfile,
210      "\n"
211      "%s%ssize_t %s_size = sizeof(%s);\n",
212      ((usestatic) ? "static " : ""),
213      ((useconst) ? "const " : ""),
214      buf,
215      buf
216    );
217  } /* createC */
218
219  /*****************************************************************/
220  /******                    END OF C FILE                     *****/
221  /*****************************************************************/
222
223  if ( createH ) {
224    /* print H file header */
225    char hbasename[PATH_MAX];
226    /* Clean up the file name if it is an abs path */
227    strcpy(
228      hbasename,
229      obasename
230    );
231    sanitize_file_name(hbasename);
232    fprintf(
233      ohfile,
234      "/*\n"
235      " *  Extern declarations for C structure representing binary file %s\n"
236      " *\n"
237      " *  WARNING: Automatically generated -- do not edit!\n"
238      " */\n"
239      "\n"
240      "#ifndef __%s_h\n"
241      "#define __%s_h\n"
242      "\n"
243      "#include <sys/types.h>\n"
244      "\n",
245      ifbasename,  /* header */
246      hbasename,  /* ifndef */
247      hbasename   /* define */
248    );
249
250    /* print structure */
251    fprintf(
252      ohfile,
253      "extern %s%sunsigned char %s[]",
254      ((usestatic) ? "static " : ""),
255      ((useconst) ? "const " : ""),
256      buf
257    );
258    if (align > 0) {
259      fprintf(
260        ohfile,
261        " __attribute__(( __aligned__(%d) ))",
262        align
263      );
264    };
265    /* print sizeof */
266    fprintf(
267      ohfile,
268      ";\n"
269      "extern %s%ssize_t %s_size;\n",
270      ((usestatic) ? "static " : ""),
271      ((useconst) ? "const " : ""),
272      buf
273    );
274
275    fprintf(
276      ohfile,
277      "\n"
278      "#endif\n"
279    );
280  } /* createH */
281
282  /*****************************************************************/
283  /******                    END OF H FILE                     *****/
284  /*****************************************************************/
285
286  fclose(ifile);
287  if ( createC ) { fclose(ocfile); }
288  if ( createH ) { fclose(ohfile); }
289  free(ifbasename_to_free);
290}
291
292void usage(void)
293{
294  fprintf(
295     stderr,
296     "usage: bin2c [-csvzCH] [-N name] [-A alignment] <input_file> <output_file>\n"
297     "  <input_file> is the binary file to convert\n"
298     "  <output_file> should not have a .c or .h extension\n"
299     "\n"
300     "  -c - do NOT use const in declaration\n"
301     "  -s - do use static in declaration\n"
302     "  -v - verbose\n"
303     "  -z - add zero terminator\n"
304     "  -H - create c-header only\n"
305     "  -C - create c-source file only\n"
306     "  -N - force name of data array\n"
307     "  -A - add alignment - parameter can be a hexadecimal or decimal number\n"
308    );
309  exit(1);
310}
311
312int main(int argc, char **argv)
313{
314  const char *name = NULL;
315  while (argc > 3) {
316    if (!strcmp(argv[1], "-c")) {
317      useconst = 0;
318      --argc;
319      ++argv;
320    } else if (!strcmp(argv[1], "-s")) {
321      usestatic = 1;
322      --argc;
323      ++argv;
324    } else if (!strcmp(argv[1], "-v")) {
325      verbose = 1;
326      --argc;
327      ++argv;
328    } else if (!strcmp(argv[1], "-z")) {
329      zeroterminated = 1;
330      --argc;
331      ++argv;
332    } else if (!strcmp(argv[1], "-C")) {
333      createH = 0;
334      createC = 1;
335      --argc;
336      ++argv;
337    } else if (!strcmp(argv[1], "-H")) {
338      createC = 0;
339      createH = 1;
340      --argc;
341      ++argv;
342    } else if (!strcmp(argv[1], "-N")) {
343      --argc;
344      ++argv;
345      if (argc <= 1) {
346        fprintf(stderr, "error: -N needs a name\n");
347        usage();
348      }
349      name = argv[1];
350      --argc;
351      ++argv;
352    } else if (!strcmp(argv[1], "-A")) {
353      --argc;
354      ++argv;
355      if (argc <= 1) {
356        fprintf(stderr, "error: -A needs an alignment\n");
357        usage();
358      }
359      align = strtoul(argv[1], NULL, 0);
360      if (align == 0) {
361        fprintf(stderr, "error: Couldn't convert argument of -A\n");
362        usage();
363      }
364      --argc;
365      ++argv;
366    } else {
367      usage();
368    }
369  }
370  if (argc != 3) {
371    usage();
372  }
373
374  /* process( input_file, output_basename ) */
375  process(argv[1], argv[2], name);
376  return 0;
377}
Note: See TracBrowser for help on using the repository browser.