Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Add (optional) -w option to support 16-bit word output #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
43 changes: 31 additions & 12 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;
unsigned char *buf;
char *ident;
unsigned int i, file_size, need_comma;
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,25 +89,34 @@ 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];
ident = argv[arg];

need_comma = 0;

fprintf(f_output, "const char %s[%i] = {", ident, file_size);
for (i = 0; i < file_size; ++i) {
if (incr > 1) {
fprintf(f_output, "const unsigned short %s[%i] = {", ident, file_size);
Copy link
Contributor

Choose a reason for hiding this comment

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

In theory and in most cases it is really implemented that way.
Short is 16 bit AT LEAST(!) by ANSI-C standart, but you can not guarantee it, it is compiler-implementation-dependent.
(Only the minimum/maximum values have to be assured, but the developers might have decided to exceed them for some unknown reasons)
If you want to be absolutely sure, better use "uint16_t" instead, this is guaranteed to really be 16 bit in size.
(OK, in most cases this is a typedef to unsigned short, but better be safe than sorry.)

Copy link
Contributor

Choose a reason for hiding this comment

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

almost forgot: for uint16_t to be recognized, the library <stdint.h> has to be included.

} else {
fprintf(f_output, "const unsigned char %s[%i] = {", ident, file_size);
}
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");

Expand Down