2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS bin2c
5 * PURPOSE: Converts a binary file into a byte array
6 * PROGRAMMER: Hermès Bélusca - Maïto
12 #include <string.h> // Required for _stricmp()
14 #include <strings.h> // Required for strcasecmp()
15 #define _stricmp strcasecmp
18 static size_t dumpHex(FILE* inFile
, FILE* outCFile
, char nullTerminate
)
23 fprintf(outCFile
, "\n{");
26 /* Read the next byte */
29 if (feof(inFile
) && nullTerminate
)
32 if (!feof(inFile
) || nullTerminate
)
34 /* Start a new line if needed */
35 if ((bufLen
% 16) == 0)
36 fprintf(outCFile
, "\n ");
38 /* Write the byte or the optional NULL terminator */
39 fprintf(outCFile
, " 0x%02x,", (unsigned int)ch
);
43 } while (!feof(inFile
));
44 fprintf(outCFile
, "\n}");
49 static size_t dumpStr(FILE* inFile
, FILE* outCFile
)
54 /* Always start the first line */
55 fprintf(outCFile
, "\n \"");
58 /* Read the next byte */
61 /* If a byte is available... */
64 /* ... do we need to start a new line? */
65 if ((bufLen
!= 0) && (bufLen
% 16) == 0)
67 /* Yes, end the current line and start a new one */
68 fprintf(outCFile
, "\"");
69 fprintf(outCFile
, "\n \"");
72 /* Now write the byte */
73 fprintf(outCFile
, "\\x%02x", (unsigned int)ch
);
75 /* ... otherwise, end the current line... */
78 fprintf(outCFile
, "\"");
79 /* We break just after */
82 ++bufLen
; // This takes also the final NULL terminator into account.
84 } while (!feof(inFile
));
89 static void usage(char* name
)
91 fprintf(stdout
, "Usage: %s infile.bin outfile.c outfile.h [BIN|BINSTR|STR] array_name [array_attribute [header_for_attribute]]\n", name
);
94 int main(int argc
, char* argv
[])
101 /* Validate the arguments */
108 /* Verify the output format */
109 if (_stricmp(argv
[4], "BIN" ) != 0 &&
110 _stricmp(argv
[4], "BINSTR") != 0 &&
111 _stricmp(argv
[4], "STR" ) != 0)
117 /* Open the input and output files */
118 inFile
= fopen(argv
[1], "rb");
121 fprintf(stderr
, "ERROR: Couldn't open data file '%s'.\n", argv
[1]);
124 outCFile
= fopen(argv
[2], "w");
128 fprintf(stderr
, "ERROR: Couldn't create output source file '%s'.\n", argv
[2]);
131 outHFile
= fopen(argv
[3], "w");
136 fprintf(stderr
, "ERROR: Couldn't create output header file '%s'.\n", argv
[3]);
140 /* Generate the source file and close it */
141 fprintf(outCFile
, "/* This file is autogenerated, do not edit. */\n\n");
144 /* Include needed header for defining the array attribute */
145 fprintf(outCFile
, "#include \"%s\"\n", argv
[7]);
147 fprintf(outCFile
, "#include \"%s\"\n\n", argv
[3]);
149 /* Generate the data array */
152 /* Add the array attribute */
153 fprintf(outCFile
, "%s ", argv
[6]);
155 fprintf(outCFile
, "unsigned char %s[] =", argv
[5]);
157 /* Output the bytes in the chosen format */
158 if (_stricmp(argv
[4], "BIN") == 0)
159 bufLen
= dumpHex(inFile
, outCFile
, 0);
160 else if (_stricmp(argv
[4], "BINSTR") == 0)
161 bufLen
= dumpHex(inFile
, outCFile
, 1);
162 else // (_stricmp(argv[4], "STR") == 0)
163 bufLen
= dumpStr(inFile
, outCFile
);
165 fprintf(outCFile
, ";\n");
168 /* Generate the header file and close it */
169 fprintf(outHFile
, "/* This file is autogenerated, do not edit. */\n\n");
170 fprintf(outHFile
, "#define %s_SIZE %lu\n" , argv
[5], bufLen
);
171 fprintf(outHFile
, "extern unsigned char %s[%lu];\n", argv
[5], bufLen
);
174 /* Close the input file */