2012-09-14 11:12:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
|
* PROJECT: ReactOS bin2c
|
|
|
|
|
* FILE: tools/bin2c/bin2c.c
|
|
|
|
|
* PURPOSE: Converts a binary file into a byte array
|
|
|
|
|
* PROGRAMMER: Herm<EFBFBD>s B<EFBFBD>lusca - Ma<EFBFBD>to
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
FILE* inFile;
|
|
|
|
|
FILE* outCFile;
|
|
|
|
|
FILE* outHFile;
|
|
|
|
|
unsigned char ch;
|
|
|
|
|
unsigned char cnt;
|
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Validate the arguments */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
if (argc < 5)
|
|
|
|
|
{
|
2015-03-29 02:00:15 +00:00
|
|
|
|
fprintf(stdout, "Usage: bin2c infile.bin outfile.c outfile.h array_name [array_attribute [header_for_attribute]]\n");
|
2012-09-14 11:12:40 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Open the input and output files */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
inFile = fopen(argv[1], "rb");
|
|
|
|
|
if (!inFile)
|
|
|
|
|
{
|
2015-03-29 02:00:15 +00:00
|
|
|
|
fprintf(stderr, "ERROR: Couldn't open data file '%s'.\n", argv[1]);
|
2012-09-14 11:12:40 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
outCFile = fopen(argv[2], "w");
|
|
|
|
|
if (!outCFile)
|
|
|
|
|
{
|
|
|
|
|
fclose(inFile);
|
2015-03-29 02:00:15 +00:00
|
|
|
|
fprintf(stderr, "ERROR: Couldn't create output source file '%s'.\n", argv[2]);
|
2012-09-14 11:12:40 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
outHFile = fopen(argv[3], "w");
|
|
|
|
|
if (!outHFile)
|
|
|
|
|
{
|
|
|
|
|
fclose(outCFile);
|
|
|
|
|
fclose(inFile);
|
2015-03-29 02:00:15 +00:00
|
|
|
|
fprintf(stderr, "ERROR: Couldn't create output header file '%s'.\n", argv[3]);
|
2012-09-14 11:12:40 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Generate the header file and close it */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fprintf(outHFile, "/* This file is autogenerated, do not edit. */\n\n");
|
|
|
|
|
fprintf(outHFile, "#ifndef CHAR\n"
|
|
|
|
|
"#define CHAR char\n"
|
|
|
|
|
"#endif\n\n");
|
|
|
|
|
fprintf(outHFile, "extern CHAR %s[];\n", argv[4]);
|
|
|
|
|
fclose(outHFile);
|
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Generate the source file and close it */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fprintf(outCFile, "/* This file is autogenerated, do not edit. */\n\n");
|
|
|
|
|
if (argc >= 7)
|
|
|
|
|
{
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Include needed header for defining the array attribute */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fprintf(outCFile, "#include \"%s\"\n", argv[6]);
|
|
|
|
|
}
|
|
|
|
|
fprintf(outCFile, "#include \"%s\"\n\n", argv[3]);
|
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Generate the data array */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
if (argc >= 6)
|
|
|
|
|
{
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Add the array attribute */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fprintf(outCFile, "%s ", argv[5]);
|
|
|
|
|
}
|
|
|
|
|
fprintf(outCFile, "CHAR %s[] =\n{", argv[4]);
|
|
|
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
|
ch = fgetc(inFile);
|
|
|
|
|
while (!feof(inFile))
|
|
|
|
|
{
|
|
|
|
|
if ((cnt % 16) == 0)
|
|
|
|
|
{
|
|
|
|
|
fprintf(outCFile, "\n ");
|
|
|
|
|
cnt = 0;
|
|
|
|
|
}
|
|
|
|
|
fprintf(outCFile, " 0x%02x,", (unsigned int)ch);
|
|
|
|
|
++cnt;
|
|
|
|
|
ch = fgetc(inFile);
|
|
|
|
|
}
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Put a final NULL terminator */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fprintf(outCFile, "\n 0x00");
|
|
|
|
|
fprintf(outCFile, "\n};\n");
|
|
|
|
|
fclose(outCFile);
|
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Close the input file */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fclose(inFile);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* EOF */
|