From 93e7611f8b5e61f4f2784b5fd9f977d3be4ea820 Mon Sep 17 00:00:00 2001 From: Pixel Date: Tue, 1 Jun 2010 21:35:56 +0200 Subject: Adding much needed bin2c software. --- bin2c.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 bin2c.c (limited to 'bin2c.c') diff --git a/bin2c.c b/bin2c.c new file mode 100644 index 0000000..8f6638c --- /dev/null +++ b/bin2c.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include + +unsigned char *buffer; + +int main(int argc, char *argv[]) +{ + int fd_size; + FILE *source, *dest; + int i; + + if (argc != 4) { + printf("bin2c\n" "Usage: bin2c infile outfile label\n\n"); + return 1; + } + + if ((source = fopen(argv[1], "rb")) == NULL) { + printf("Error opening %s for reading.\n", argv[1]); + return 1; + } + + fseek(source, 0, SEEK_END); + fd_size = ftell(source); + fseek(source, 0, SEEK_SET); + + buffer = malloc(fd_size); + if (buffer == NULL) { + printf("Failed to allocate memory.\n"); + return 1; + } + + if (fread(buffer, 1, fd_size, source) != fd_size) { + printf("Failed to read file.\n"); + return 1; + } + fclose(source); + + if ((dest = fopen(argv[2], "w+")) == NULL) { + printf("Failed to open/create %s.\n", argv[2]); + return 1; + } + + fprintf(dest, "unsigned int size_%s = %d;\n", argv[3], fd_size); + fprintf(dest, "unsigned char %s[] = {", argv[3]); + + for (i = 0; i < fd_size; i += 1) { + if ((i % 16) == 0) + fprintf(dest, "\n\t"); + fprintf(dest, "0x%02x, ", buffer[i]); + } + + fprintf(dest, "\n};\n"); + + fclose(dest); + + return 0; +} -- cgit v1.2.3