2012-09-14 11:12:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
|
* PROJECT: ReactOS bin2c
|
2015-09-28 17:31:37 +00:00
|
|
|
|
* FILE: tools/bin2c.c
|
2012-09-14 11:12:40 +00:00
|
|
|
|
* PURPOSE: Converts a binary file into a byte array
|
|
|
|
|
* PROGRAMMER: Herm<EFBFBD>s B<EFBFBD>lusca - Ma<EFBFBD>to
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2015-06-24 20:00:23 +00:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <string.h> // Required for _stricmp()
|
|
|
|
|
#else
|
|
|
|
|
#include <strings.h> // Required for strcasecmp()
|
|
|
|
|
#define _stricmp strcasecmp
|
|
|
|
|
#endif
|
|
|
|
|
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
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[])
|
2012-09-14 11:12:40 +00:00
|
|
|
|
{
|
|
|
|
|
FILE* inFile;
|
|
|
|
|
FILE* outCFile;
|
|
|
|
|
FILE* outHFile;
|
2015-06-14 18:13:50 +00:00
|
|
|
|
size_t bufLen;
|
2012-09-14 11:12:40 +00:00
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Validate the arguments */
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
if (argc < 6)
|
2012-09-14 11:12:40 +00:00
|
|
|
|
{
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
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]);
|
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 source file and close it */
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fprintf(outCFile, "/* This file is autogenerated, do not edit. */\n\n");
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
if (argc >= 8)
|
2012-09-14 11:12:40 +00:00
|
|
|
|
{
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Include needed header for defining the array attribute */
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
fprintf(outCFile, "#include \"%s\"\n", argv[7]);
|
2012-09-14 11:12:40 +00:00
|
|
|
|
}
|
|
|
|
|
fprintf(outCFile, "#include \"%s\"\n\n", argv[3]);
|
|
|
|
|
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Generate the data array */
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
if (argc >= 7)
|
2012-09-14 11:12:40 +00:00
|
|
|
|
{
|
2015-03-29 00:13:25 +00:00
|
|
|
|
/* Add the array attribute */
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
fprintf(outCFile, "%s ", argv[6]);
|
2012-09-14 11:12:40 +00:00
|
|
|
|
}
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
fprintf(outCFile, "unsigned char %s[] =", argv[5]);
|
2012-09-14 11:12:40 +00:00
|
|
|
|
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
/* 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);
|
2015-06-14 18:13:50 +00:00
|
|
|
|
|
[TOOLS_BIN2C]
- Fix a GCC warning (error on Macs) concerning the printf "%Iu" specifier.
- To make sure data included by bin2c, that need to be interpreted as a (long) string, is correctly NULL-terminated, introduce an output format called "STR": this creates a long character array using the "\xXX" format.
- To circumvent a silly limitation of MSVC (all versions?) about the maximum length (65535 bytes) of a char string (aka. char MyArray[] = "...my_long_string...";), introduced yet another output format called "BINSTR" which creates an array of bytes that is NULL-terminated (aka. char MyArray[] = {0xde, 0xad, 0xbe, 0xef, 0x00};).
- Finally, the output format "BIN" is the regular bin2c format (simple array of bytes).
[HAL_PCIDATA][FREELDR_INSTALL]
Use the above-described functionality (for HAL_PCIDATA, use BINSTR format because the PciVendors data is otherwise too large to be stored in "char_string" format).
svn path=/trunk/; revision=68255
2015-06-24 19:54:19 +00:00
|
|
|
|
fprintf(outCFile, ";\n");
|
2012-09-14 11:12:40 +00:00
|
|
|
|
fclose(outCFile);
|
|
|
|
|
|
2015-06-14 18:13:50 +00:00
|
|
|
|
/* Generate the header file and close it */
|
|
|
|
|
fprintf(outHFile, "/* This file is autogenerated, do not edit. */\n\n");
|
2019-04-15 11:29:33 +00:00
|
|
|
|
fprintf(outHFile, "#define %s_SIZE %lu\n" , argv[5], (unsigned long)bufLen);
|
|
|
|
|
fprintf(outHFile, "extern unsigned char %s[%lu];\n", argv[5], (unsigned long)bufLen);
|
2015-06-14 18:13:50 +00:00
|
|
|
|
fclose(outHFile);
|
|
|
|
|
|
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 */
|