2015-04-15 09:47:20 -04:00
|
|
|
/*
|
|
|
|
* bin2c: A Program to convert binary data into C source code
|
|
|
|
* Copyright 2004 by Adrian Prantl <adrian@f4z.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
char* self = 0;
|
|
|
|
|
|
|
|
void usage() {
|
|
|
|
printf("Usage:\n%s input.bin output.h name\n\n", self);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bail_out(const char* s1, const char* s2) {
|
|
|
|
fprintf(stderr, "%s: FATAL ERROR:\n%s%s\n", self, s1, s2);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
FILE *fi, *fo;
|
|
|
|
int c, i;
|
|
|
|
|
|
|
|
self = argv[0];
|
|
|
|
|
|
|
|
if (argc != 4) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fi = fopen(argv[1], "rb")) == 0)
|
|
|
|
bail_out("Cannot open input file ", argv[1]);
|
|
|
|
|
|
|
|
if ((fo = fopen(argv[2], "w")) == 0)
|
|
|
|
bail_out("Cannot open output file ", argv[2]);
|
|
|
|
|
|
|
|
if ((c = fgetc(fi)) != EOF) {
|
|
|
|
fprintf(fo, "#ifndef %s_H\n", argv[3]);
|
|
|
|
fprintf(fo, "#define %s_H\n\n", argv[3]);
|
|
|
|
fprintf(fo, "const char %s[] = {\n", argv[3]);
|
|
|
|
fprintf(fo, c < 16 ? " 0x%02x" : " 0x%02x", c);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
while ((c = fgetc(fi)) != EOF) {
|
|
|
|
if (i < 12)
|
2019-06-04 16:07:21 -04:00
|
|
|
fprintf(fo, ", 0x%02x", c);
|
2015-04-15 09:47:20 -04:00
|
|
|
else {
|
2019-06-04 16:07:21 -04:00
|
|
|
fprintf(fo, ",\n 0x%02x", c);
|
2015-04-15 09:47:20 -04:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
fprintf(fo, "\n};\n\n");
|
|
|
|
fprintf(fo, "#endif\n");
|
|
|
|
|
2019-06-24 09:18:08 -04:00
|
|
|
printf("converted %s to .h file\n", argv[1]);
|
2015-04-15 09:47:20 -04:00
|
|
|
|
|
|
|
return 0;
|
2019-06-04 16:07:21 -04:00
|
|
|
}
|