#include <stdio.h>
-int main(int argc, char *argv[])
+static size_t dumpHex(FILE* inFile, FILE* outCFile, char nullTerminate)
+{
+ size_t bufLen = 0;
+ unsigned char ch;
+
+ fprintf(outCFile, "\n{");
+ do
+ {
+ /* Read the next byte */
+ ch = fgetc(inFile);
+
+ if (feof(inFile) && nullTerminate)
+ ch = 0x00;
+
+ if (!feof(inFile) || nullTerminate)
+ {
+ /* Start a new line if needed */
+ if ((bufLen % 16) == 0)
+ fprintf(outCFile, "\n ");
+
+ /* Write the byte or the optional NULL terminator */
+ fprintf(outCFile, " 0x%02x,", (unsigned int)ch);
+
+ ++bufLen;
+ }
+ } while (!feof(inFile));
+ fprintf(outCFile, "\n}");
+
+ return bufLen;
+}
+
+static size_t dumpStr(FILE* inFile, FILE* outCFile)
+{
+ size_t bufLen = 0;
+ unsigned char ch;
+
+ /* Always start the first line */
+ fprintf(outCFile, "\n \"");
+ do
+ {
+ /* Read the next byte */
+ ch = fgetc(inFile);
+
+ /* If a byte is available... */
+ if (!feof(inFile))
+ {
+ /* ... do we need to start a new line? */
+ if ((bufLen != 0) && (bufLen % 16) == 0)
+ {
+ /* Yes, end the current line and start a new one */
+ fprintf(outCFile, "\"");
+ fprintf(outCFile, "\n \"");
+ }
+
+ /* Now write the byte */
+ fprintf(outCFile, "\\x%02x", (unsigned int)ch);
+ }
+ /* ... otherwise, end the current line... */
+ else
+ {
+ fprintf(outCFile, "\"");
+ /* We break just after */
+ }
+
+ ++bufLen; // This takes also the final NULL terminator into account.
+
+ } while (!feof(inFile));
+
+ return bufLen;
+}
+
+static void usage(char* name)
+{
+ fprintf(stdout, "Usage: %s infile.bin outfile.c outfile.h [BIN|BINSTR|STR] array_name [array_attribute [header_for_attribute]]\n", name);
+}
+
+int main(int argc, char* argv[])
{
FILE* inFile;
FILE* outCFile;
FILE* outHFile;
size_t bufLen;
- unsigned char ch;
/* Validate the arguments */
- if (argc < 5)
+ if (argc < 6)
{
- fprintf(stdout, "Usage: bin2c infile.bin outfile.c outfile.h array_name [array_attribute [header_for_attribute]]\n");
+ usage(argv[0]);
+ return -1;
+ }
+
+ /* Verify the output format */
+ if (_stricmp(argv[4], "BIN" ) != 0 &&
+ _stricmp(argv[4], "BINSTR") != 0 &&
+ _stricmp(argv[4], "STR" ) != 0)
+ {
+ usage(argv[0]);
return -1;
}
/* Generate the source file and close it */
fprintf(outCFile, "/* This file is autogenerated, do not edit. */\n\n");
- if (argc >= 7)
+ if (argc >= 8)
{
/* Include needed header for defining the array attribute */
- fprintf(outCFile, "#include \"%s\"\n", argv[6]);
+ fprintf(outCFile, "#include \"%s\"\n", argv[7]);
}
fprintf(outCFile, "#include \"%s\"\n\n", argv[3]);
/* Generate the data array */
- if (argc >= 6)
+ if (argc >= 7)
{
/* Add the array attribute */
- fprintf(outCFile, "%s ", argv[5]);
+ fprintf(outCFile, "%s ", argv[6]);
}
- fprintf(outCFile, "unsigned char %s[] =\n{", argv[4]);
+ fprintf(outCFile, "unsigned char %s[] =", argv[5]);
- bufLen = 0;
- ch = fgetc(inFile);
- while (!feof(inFile))
- {
- if ((bufLen % 16) == 0)
- fprintf(outCFile, "\n ");
+ /* Output the bytes in the chosen format */
+ if (_stricmp(argv[4], "BIN") == 0)
+ bufLen = dumpHex(inFile, outCFile, 0);
+ else if (_stricmp(argv[4], "BINSTR") == 0)
+ bufLen = dumpHex(inFile, outCFile, 1);
+ else // (_stricmp(argv[4], "STR") == 0)
+ bufLen = dumpStr(inFile, outCFile);
- fprintf(outCFile, " 0x%02x,", (unsigned int)ch);
- ++bufLen;
- ch = fgetc(inFile);
- }
- /* Put a final NULL terminator */
- fprintf(outCFile, "\n 0x00"); ++bufLen;
- fprintf(outCFile, "\n};\n");
+ fprintf(outCFile, ";\n");
fclose(outCFile);
/* Generate the header file and close it */
fprintf(outHFile, "/* This file is autogenerated, do not edit. */\n\n");
- fprintf(outHFile, "#define %s_SIZE %Iu\n" , argv[4], bufLen);
- fprintf(outHFile, "extern unsigned char %s[%Iu];\n", argv[4], bufLen);
+ fprintf(outHFile, "#define %s_SIZE %lu\n" , argv[5], bufLen);
+ fprintf(outHFile, "extern unsigned char %s[%lu];\n", argv[5], bufLen);
fclose(outHFile);
/* Close the input file */