diff --git a/build-linux.sh b/build-linux.sh index dd2e5db..21aa454 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -528,7 +528,7 @@ xtchain_build() for EXEC in dlltool ld objdump; do ln -sf ../${GENERIC}/bin/${EXEC}-wrapper ${BINDIR}/bin/${ARCH}-w64-mingw32-${EXEC} done - for EXEC in exetool windres xtcspecc; do + for EXEC in bin2c exetool windres xtcspecc; do if [ ! -e ${BINDIR}/bin/${EXEC} ]; then gcc ${WRKDIR}/tools/${EXEC}.c -o ${BINDIR}/bin/${EXEC} fi diff --git a/tools/bin2c.c b/tools/bin2c.c new file mode 100644 index 0000000..52b9567 --- /dev/null +++ b/tools/bin2c.c @@ -0,0 +1,74 @@ +/** + * PROJECT: XTchain + * LICENSE: See COPYING.md in the top level directory + * FILE: tools/bin2c.c + * DESCRIPTION: Binary to C converter + * DEVELOPERS: Rafal Kupiec + */ + +#include "xtchain.h" + +int main(int argc, char *argv[]) +{ + /* Check for proper number of arguments */ + if(argc != 4) + { + printf("Usage: %s \n", argv[0]); + return 1; + } + + /* Open the input binary file in binary mode */ + FILE *inputFile = fopen(argv[1], "rb"); + if(inputFile == NULL) + { + printf("Error: unable to open file %s\n", argv[1]); + return 1; + } + + /* Open the destination source code file in text mode */ + FILE *outputFile = fopen(argv[2], "w"); + if(outputFile == NULL) + { + printf("Error: unable to open file %s\n", argv[2]); + fclose(inputFile); + return 1; + } + + /* Get the size of the binary file */ + fseek(inputFile, 0, SEEK_END); + long binSize = ftell(inputFile); + rewind(inputFile); + + /* Allocate memory for the binary data */ + unsigned char *binData = (unsigned char *)malloc(binSize); + if(binData == NULL) + { + printf("Error: unable to allocate memory for binary data\n"); + fclose(inputFile); + fclose(outputFile); + return 1; + } + + /* Read the binary data into memory */ + fread(binData, sizeof(unsigned char), binSize, inputFile); + + /* Write the C structure to the header file */ + fprintf(outputFile, "unsigned char %s[] = {", argv[3]); + for(int i = 0; i < binSize; i++) + { + fprintf(outputFile, "0x%02X", binData[i]); + if(i < binSize - 1) + { + fprintf(outputFile, ","); + } + } + fprintf(outputFile, "};\nunsigned int %s_size = %ld;\n", argv[3], binSize); + free(binData); + + /* Close all open files */ + fclose(inputFile); + fclose(outputFile); + + printf("Binary data converted to C structure successfully.\n"); + return 0; +}