diff options
Diffstat (limited to 'src/installer/bin2c.c')
-rw-r--r-- | src/installer/bin2c.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/installer/bin2c.c b/src/installer/bin2c.c new file mode 100644 index 0000000..6c81fe6 --- /dev/null +++ b/src/installer/bin2c.c | |||
@@ -0,0 +1,72 @@ | |||
1 | /* BIN2C Converts binary data to be included in C source codes */ | ||
2 | |||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <string.h> | ||
7 | #include <sys/stat.h> | ||
8 | |||
9 | |||
10 | static void usage(void) | ||
11 | { | ||
12 | fprintf(stderr, "BIN2C Binary to C converter\n"); | ||
13 | fprintf(stderr, "usage: BIN2C <binfile> <cfile> <varname>\n"); | ||
14 | exit(0); | ||
15 | } | ||
16 | |||
17 | |||
18 | int main(int argc, char **argv) | ||
19 | { | ||
20 | FILE* binfile; | ||
21 | FILE* cfile; | ||
22 | struct stat statbuf; | ||
23 | long len; | ||
24 | int cntr; | ||
25 | unsigned char inbyte; | ||
26 | char varname[80], cfilename[80]; | ||
27 | |||
28 | if (argc == 1) | ||
29 | usage(); | ||
30 | |||
31 | if (! (binfile = fopen(argv[1], "rb"))) { | ||
32 | perror("fopen"); | ||
33 | exit(1); | ||
34 | } | ||
35 | |||
36 | stat(argv[1], &statbuf); | ||
37 | len = statbuf.st_size; | ||
38 | |||
39 | if (argc > 2) | ||
40 | strcpy(cfilename, argv[2]); | ||
41 | else | ||
42 | strcpy(cfilename,"bin2c.c"); | ||
43 | cfile = fopen(cfilename, "wt"); | ||
44 | |||
45 | cntr = 0; | ||
46 | |||
47 | if (argc > 3) | ||
48 | strcpy(varname, argv[3]); | ||
49 | else | ||
50 | strcpy(varname, "varname"); | ||
51 | |||
52 | fprintf(cfile, "unsigned char %s[%ld] = {\n", varname, len); | ||
53 | |||
54 | fread(&inbyte, 1, 1, binfile); | ||
55 | while (1) { | ||
56 | fprintf(cfile, "0X%02X", inbyte); | ||
57 | |||
58 | fread(&inbyte, 1, 1, binfile); | ||
59 | if (feof(binfile)) | ||
60 | break; | ||
61 | fprintf(cfile, ","); | ||
62 | if (++cntr > 30) { | ||
63 | fprintf(cfile, "\n"); | ||
64 | cntr = 0; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | fprintf(cfile, "};\n"); | ||
69 | fclose(binfile); | ||
70 | fclose(cfile); | ||
71 | return 0; | ||
72 | } | ||