From: Hermès Bélusca-Maïto Date: Wed, 24 Jun 2015 19:54:19 +0000 (+0000) Subject: [TOOLS_BIN2C] X-Git-Tag: ReactOS-0.4.0~1729 X-Git-Url: https://git.reactos.org/?p=reactos.git;a=commitdiff_plain;h=f4c0c8f78c67ca712dc3dc72d4c66d1e7c069e62;hp=81d3bf265414b2b1e0b4a77ca8c279fef01f5f79 [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 --- diff --git a/reactos/boot/freeldr/install/CMakeLists.txt b/reactos/boot/freeldr/install/CMakeLists.txt index 8bda73d0a00..d495a180fee 100644 --- a/reactos/boot/freeldr/install/CMakeLists.txt +++ b/reactos/boot/freeldr/install/CMakeLists.txt @@ -4,12 +4,12 @@ # add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/fat.c ${CMAKE_CURRENT_BINARY_DIR}/fat.h - COMMAND native-bin2c ${CMAKE_CURRENT_BINARY_DIR}/../bootsect/fat.bin ${CMAKE_CURRENT_BINARY_DIR}/fat.c ${CMAKE_CURRENT_BINARY_DIR}/fat.h fat_data + COMMAND native-bin2c ${CMAKE_CURRENT_BINARY_DIR}/../bootsect/fat.bin ${CMAKE_CURRENT_BINARY_DIR}/fat.c ${CMAKE_CURRENT_BINARY_DIR}/fat.h BIN fat_data DEPENDS native-bin2c fat) add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/fat32.c ${CMAKE_CURRENT_BINARY_DIR}/fat32.h - COMMAND native-bin2c ${CMAKE_CURRENT_BINARY_DIR}/../bootsect/fat32.bin ${CMAKE_CURRENT_BINARY_DIR}/fat32.c ${CMAKE_CURRENT_BINARY_DIR}/fat32.h fat32_data + COMMAND native-bin2c ${CMAKE_CURRENT_BINARY_DIR}/../bootsect/fat32.bin ${CMAKE_CURRENT_BINARY_DIR}/fat32.c ${CMAKE_CURRENT_BINARY_DIR}/fat32.h BIN fat32_data DEPENDS native-bin2c fat32) ##################################### diff --git a/reactos/boot/freeldr/install/install.c b/reactos/boot/freeldr/install/install.c index 702e6bc465d..ba663ce6788 100644 --- a/reactos/boot/freeldr/install/install.c +++ b/reactos/boot/freeldr/install/install.c @@ -36,7 +36,6 @@ BOOL InstallBootSector(LPCTSTR lpszVolumeType); int main(int argc, char *argv[]) { - if (argc < 3) { _tprintf(_T("syntax: install x: [fs_type]\nwhere fs_type is fat or fat32\n")); diff --git a/reactos/hal/halx86/pcidata.cmake b/reactos/hal/halx86/pcidata.cmake index 5860e507fc8..a32bcfb7440 100644 --- a/reactos/hal/halx86/pcidata.cmake +++ b/reactos/hal/halx86/pcidata.cmake @@ -3,11 +3,11 @@ # add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pci_classes.c ${CMAKE_CURRENT_BINARY_DIR}/pci_classes.h - COMMAND native-bin2c ${CMAKE_CURRENT_SOURCE_DIR}/legacy/bus/pci_classes.ids ${CMAKE_CURRENT_BINARY_DIR}/pci_classes.c ${CMAKE_CURRENT_BINARY_DIR}/pci_classes.h ClassTable INIT_FUNCTION ${CMAKE_CURRENT_SOURCE_DIR}/include/hal.h + COMMAND native-bin2c ${CMAKE_CURRENT_SOURCE_DIR}/legacy/bus/pci_classes.ids ${CMAKE_CURRENT_BINARY_DIR}/pci_classes.c ${CMAKE_CURRENT_BINARY_DIR}/pci_classes.h BINSTR ClassTable INIT_FUNCTION ${CMAKE_CURRENT_SOURCE_DIR}/include/hal.h DEPENDS native-bin2c ${CMAKE_CURRENT_SOURCE_DIR}/legacy/bus/pci_classes.ids) add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pci_vendors.c ${CMAKE_CURRENT_BINARY_DIR}/pci_vendors.h - COMMAND native-bin2c ${CMAKE_CURRENT_SOURCE_DIR}/legacy/bus/pci_vendors.ids ${CMAKE_CURRENT_BINARY_DIR}/pci_vendors.c ${CMAKE_CURRENT_BINARY_DIR}/pci_vendors.h VendorTable INIT_FUNCTION ${CMAKE_CURRENT_SOURCE_DIR}/include/hal.h + COMMAND native-bin2c ${CMAKE_CURRENT_SOURCE_DIR}/legacy/bus/pci_vendors.ids ${CMAKE_CURRENT_BINARY_DIR}/pci_vendors.c ${CMAKE_CURRENT_BINARY_DIR}/pci_vendors.h BINSTR VendorTable INIT_FUNCTION ${CMAKE_CURRENT_SOURCE_DIR}/include/hal.h DEPENDS native-bin2c ${CMAKE_CURRENT_SOURCE_DIR}/legacy/bus/pci_vendors.ids) ##################################### diff --git a/reactos/tools/bin2c.c b/reactos/tools/bin2c.c index 83b38e9e241..33b7d8cbdf2 100644 --- a/reactos/tools/bin2c.c +++ b/reactos/tools/bin2c.c @@ -8,18 +8,102 @@ #include -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; } @@ -48,41 +132,36 @@ int main(int argc, char *argv[]) /* 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 */