1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/* bin2c.c 4/9/1996
* convert a binary (or text) file to C.
*/
/* SPDX-FileCopyrightText: 1995,1996 Sascha Demetrio
* SPDX-FileCopyrightText: 2009, 2010, 2015, 2018, 2020 Peter Pentchev
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#if !defined(fallthrough)
#if defined(__GNUC__) && __GNUC__ >= 7
#define fallthrough __attribute__((fallthrough))
#else
#define fallthrough /* fallthrough */
#endif
#endif
static const char *usage = "\
usage: bin2c [-t] [-h] [-o outputfile] [-n name] [inputfile]\n\
-t you can use this option if the inputfile is a small (< 1K) textfile.\n\
-h display this help message.\n\
-o outputfile\n\
write the output to outputfile. the default is <stdout>.\n\
-n name\n\
the name of the C variable generated. the default is `data'\n\
if no inputfile is specified, the input is read from <stdin>.\n\
";
int
main(int argc, char **argv)
{
const char *name = "data";
FILE *in = stdin, *out = stdout;
int text_f = 0;
char *outfile = 0;
int c;
int i;
while ((c = getopt(argc, argv, "htn:o:")) != -1)
switch (c) {
case 't': /* generate a C string. this option useful only
* for *very* small text files, since some compilers
* refuse to compile extremely long strings. */
++text_f;
break;
case 'n': /* select the name of the output C array. */
name = optarg;
break;
case 'o': /* select the name of the output file. */
outfile = optarg;
break;
default:
fprintf(stderr, "%s: invalid option -- -%c\n", *argv, (char)c);
fallthrough;
case 'h':
case '?':
fputs(usage, stderr);
exit(1);
}
if (argc - optind > 1) {
fputs(usage, stderr);
exit(1);
} else if (argc - optind == 1)
if (!(in = fopen(argv[optind], "r"))) {
perror(argv[optind]);
exit(1);
}
if (outfile)
if (!(out = fopen(outfile, "w"))) {
perror(outfile);
fclose(in);
exit(1);
}
fprintf(out, "/* %s.c\n * generated by bin2c - do not edit!\n */\n\n", name);
/* SPDX-FileCopyrightText: 1995,1996 Sascha Demetrio
* SPDX-License-Identifier: BSD-3-Clause
*/
if (text_f) {
fprintf(out, "char %s[] = \"\\\n", name);
for (;;) {
c = getc(in);
if (feof(in)) break;
if (!isprint(c))
switch (c) {
case '\n':
fprintf(out, "\\n\\\n");
break;
case '\b':
fprintf(out, "\\b");
break;
default:
fprintf(out, "\\%o", (int)(unsigned char)c);
}
else
switch (c) {
case '\\':
fprintf(out, "\\\\");
break;
case '\"':
fprintf(out, "\\\"");
break;
default:
putc(c, out);
}
}
fprintf(out, "\";\n\n");
} else {
fprintf(out, "char %s[] = {", name);
for (i = 0;; ++i) {
c = getc(in);
if (feof(in)) break;
if (!(i % 8)) fprintf(out, "\n ");
fprintf(out, " 0x%02x,", (int)(unsigned char)c);
}
if (!(i % 8)) fprintf(out, "\n ");
fprintf(out, " 0\n};\n\n");
/* we write a trailing 0 so the array can be used as a string. */
}
fprintf(out, "/* end of %s.c */\n\n", name);
fclose(in);
fclose(out);
return 0;
}
/* main */
|