|
| 1 | +/* |
| 2 | + * bin2c.c |
| 3 | + * |
| 4 | + * convert a binary file into a C source vector |
| 5 | + * |
| 6 | + * THE "BEER-WARE LICENSE" (Revision 3.1415): |
| 7 | + * sandro AT sigala DOT it wrote this file. As long as you retain this notice you can do |
| 8 | + * whatever you want with this stuff. If we meet some day, and you think this stuff is |
| 9 | + * worth it, you can buy me a beer in return. Sandro Sigala |
| 10 | + * |
| 11 | + * syntax: bin2c [-c] [-z] <input_file> <output_file> |
| 12 | + * |
| 13 | + * -c add the "const" keyword to definition |
| 14 | + * -z terminate the array with a zero (useful for embedded C strings) |
| 15 | + * |
| 16 | + * examples: |
| 17 | + * bin2c -c myimage.png myimage_png.cpp |
| 18 | + * bin2c -z sometext.txt sometext_txt.cpp |
| 19 | + */ |
| 20 | + |
| 21 | +#include <ctype.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#ifndef PATH_MAX |
| 27 | +#define PATH_MAX 1024 |
| 28 | +#endif |
| 29 | + |
| 30 | +int useconst = 0; |
| 31 | +int zeroterminated = 0; |
| 32 | + |
| 33 | +int myfgetc(FILE *f) |
| 34 | +{ |
| 35 | + int c = fgetc(f); |
| 36 | + if (c == EOF && zeroterminated) |
| 37 | + { |
| 38 | + zeroterminated = 0; |
| 39 | + return 0; |
| 40 | + } |
| 41 | + return c; |
| 42 | +} |
| 43 | + |
| 44 | +void process(const char *ifname, const char *ofname) |
| 45 | +{ |
| 46 | + FILE *ifile, *ofile; |
| 47 | + ifile = fopen(ifname, "rb"); |
| 48 | + if (ifile == NULL) |
| 49 | + { |
| 50 | + fprintf(stderr, "cannot open %s for reading\n", ifname); |
| 51 | + exit(1); |
| 52 | + } |
| 53 | + ofile = fopen(ofname, "wb"); |
| 54 | + if (ofile == NULL) |
| 55 | + { |
| 56 | + fprintf(stderr, "cannot open %s for writing\n", ofname); |
| 57 | + exit(1); |
| 58 | + } |
| 59 | + char buf[PATH_MAX], *p; |
| 60 | + const char *cp; |
| 61 | + if ((cp = strrchr(ifname, '/')) != NULL) |
| 62 | + { |
| 63 | + ++cp; |
| 64 | + } else { |
| 65 | + if ((cp = strrchr(ifname, '\\')) != NULL) |
| 66 | + ++cp; |
| 67 | + else |
| 68 | + cp = ifname; |
| 69 | + } |
| 70 | + strcpy(buf, cp); |
| 71 | + for (p = buf; *p != '\0'; ++p) |
| 72 | + { |
| 73 | + if (!isalnum(*p)) |
| 74 | + *p = '_'; |
| 75 | + } |
| 76 | + fprintf(ofile, "static %sunsigned char %s[] = {\n", useconst ? "const " : "", buf); |
| 77 | + int c, col = 1; |
| 78 | + while ((c = myfgetc(ifile)) != EOF) |
| 79 | + { |
| 80 | + if (col >= 78 - 6) |
| 81 | + { |
| 82 | + fputc('\n', ofile); |
| 83 | + col = 1; |
| 84 | + } |
| 85 | + fprintf(ofile, "0x%.2x,", c); |
| 86 | + col += 6; |
| 87 | + } |
| 88 | + fprintf(ofile, "\n};\n"); |
| 89 | + |
| 90 | + fclose(ifile); |
| 91 | + fclose(ofile); |
| 92 | +} |
| 93 | + |
| 94 | +void usage(void) |
| 95 | +{ |
| 96 | + fprintf(stderr, "usage: bin2c [-cz] <input_file> <output_file>\n"); |
| 97 | + exit(1); |
| 98 | +} |
| 99 | + |
| 100 | +int main(int argc, char **argv) |
| 101 | +{ |
| 102 | + while (argc > 3) |
| 103 | + { |
| 104 | + if (!strcmp(argv[1], "-c")) |
| 105 | + { |
| 106 | + useconst = 1; |
| 107 | + --argc; |
| 108 | + ++argv; |
| 109 | + } else if (!strcmp(argv[1], "-z")) |
| 110 | + { |
| 111 | + zeroterminated = 1; |
| 112 | + --argc; |
| 113 | + ++argv; |
| 114 | + } else { |
| 115 | + usage(); |
| 116 | + } |
| 117 | + } |
| 118 | + if (argc != 3) |
| 119 | + { |
| 120 | + usage(); |
| 121 | + } |
| 122 | + process(argv[1], argv[2]); |
| 123 | + return 0; |
| 124 | +} |
0 commit comments