Add bin2c utility
Builds / XTChain (push) Successful in 1h24m30s Details

This commit is contained in:
Rafal Kupiec 2023-09-28 22:47:04 +02:00
parent 737dda2960
commit 37230435aa
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
2 changed files with 75 additions and 1 deletions

View File

@ -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

74
tools/bin2c.c Normal file
View File

@ -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 <belliash@codingworkshop.eu.org>
*/
#include "xtchain.h"
int main(int argc, char *argv[])
{
/* Check for proper number of arguments */
if(argc != 4)
{
printf("Usage: %s <input binary> <output file> <structure name>\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;
}