Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions bin2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
#include <stdlib.h>
#include <string.h>

#define COLS 16

#ifdef USE_BZ2
#include <bzlib.h>
#endif

int
main(int argc, char *argv[])
{
char *buf;
char *ident;
unsigned int i, file_size, need_comma;
unsigned char *buf;
char array_name[80];
unsigned int i, file_size, file_count, need_comma;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about array_size instead of file_count?

int incr = 1;
int arg = 1;
const char* filename;

FILE *f_input, *f_output;

Expand All @@ -33,14 +38,19 @@ main(int argc, char *argv[])
#endif

if (argc < 4) {
fprintf(stderr, "Usage: %s binary_file output_file array_name\n",
fprintf(stderr, "Usage: %s [-w] binary_file output_file [array_name]\n",
argv[0]);
return -1;
}

f_input = fopen(argv[1], "rb");
if (strcmp(argv[arg], "-w") == 0) {
++arg; incr = 2;
}

filename = argv[arg++];
f_input = fopen(filename, "rb");
if (f_input == NULL) {
fprintf(stderr, "%s: can't open %s for reading\n", argv[0], argv[1]);
fprintf(stderr, "%s: can't open %s for reading\n", argv[0], filename);
return -1;
}

Expand All @@ -49,7 +59,7 @@ main(int argc, char *argv[])
file_size = ftell(f_input);
fseek(f_input, 0, SEEK_SET);

buf = (char *) malloc(file_size);
buf = (unsigned char *) malloc(file_size);
assert(buf);

fread(buf, file_size, 1, f_input);
Expand Down Expand Up @@ -79,32 +89,52 @@ main(int argc, char *argv[])
buf = bz2_buf;
#endif

f_output = fopen(argv[2], "w");
filename = argv[arg++];
f_output = fopen(filename, "w");
if (f_output == NULL) {
fprintf(stderr, "%s: can't open %s for writing\n", argv[0], argv[1]);
fprintf(stderr, "%s: can't open %s for writing\n", argv[0], filename);
return -1;
}

ident = argv[3];
// Check if array_name passed on command line
if (arg < argc) {
strcpy(array_name, argv[arg]);
} else {
char ch;
strcpy(array_name, filename);
// Replace non-alphanumeric chars with underscore
for (i = 0; (ch = array_name[i]) != '\0'; ++i) {
if (!isalpha(ch) && !isdigit(ch)) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you going to handle the case where the first character is a number?

array_name[i] = '_';
}
}

}

need_comma = 0;

file_count = file_size / incr;

fprintf(f_output, "const char %s[%i] = {", ident, file_size);
for (i = 0; i < file_size; ++i) {
fprintf(f_output, "const unsigned short %s[%i] = {", array_name, file_count);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep consistent identation :)

for (i = 0; i < file_size; i += incr) {
if (need_comma)
fprintf(f_output, ", ");
else
need_comma = 1;
if ((i % 11) == 0)
if ((i % COLS) == 0)
fprintf(f_output, "\n\t");
fprintf(f_output, "0x%.2x", buf[i] & 0xff);
if (incr > 1) {
fprintf(f_output, "0x%04x", buf[i] | (buf[i + 1] << 8));
} else {
fprintf(f_output, "0x%02x", buf[i]);
}
}
fprintf(f_output, "\n};\n\n");

fprintf(f_output, "const int %s_length = %i;\n", ident, file_size);
fprintf(f_output, "const int %s_length = %i;\n", array_name, file_count);

#ifdef USE_BZ2
fprintf(f_output, "const int %s_length_uncompressed = %i;\n", ident,
fprintf(f_output, "const int %s_length_uncompressed = %i;\n", array_name,
uncompressed_size);
#endif

Expand Down