diff options
Diffstat (limited to 'rbutil/tools/bin2c.c')
-rw-r--r-- | rbutil/tools/bin2c.c | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/rbutil/tools/bin2c.c b/rbutil/tools/bin2c.c new file mode 100644 index 0000000000..75b44d5df7 --- /dev/null +++ b/rbutil/tools/bin2c.c | |||
@@ -0,0 +1,143 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2007 Dave Chapman | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include <stdio.h> | ||
23 | #include <string.h> | ||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <fcntl.h> | ||
27 | #include <stdlib.h> | ||
28 | #if !defined(_MSC_VER) | ||
29 | #include <unistd.h> | ||
30 | #else | ||
31 | #include <io.h> | ||
32 | #define snprintf _snprintf | ||
33 | #define open _open | ||
34 | #define close _close | ||
35 | #define read _read | ||
36 | #endif | ||
37 | |||
38 | #ifndef O_BINARY | ||
39 | #define O_BINARY 0 | ||
40 | #endif | ||
41 | |||
42 | static off_t filesize(int fd) | ||
43 | { | ||
44 | struct stat buf; | ||
45 | |||
46 | fstat(fd,&buf); | ||
47 | return buf.st_size; | ||
48 | } | ||
49 | |||
50 | static int write_cfile(const unsigned char* buf, off_t len, const char* cname) | ||
51 | { | ||
52 | char filename[256]; | ||
53 | FILE* fp; | ||
54 | int i; | ||
55 | |||
56 | snprintf(filename,256,"%s.c",cname); | ||
57 | |||
58 | fp = fopen(filename,"w+"); | ||
59 | if (fp == NULL) { | ||
60 | fprintf(stderr,"Couldn't open %s\n",filename); | ||
61 | return -1; | ||
62 | } | ||
63 | |||
64 | fprintf(fp,"/* Generated by bin2c */\n\n"); | ||
65 | fprintf(fp,"unsigned char %s[] = {",cname); | ||
66 | |||
67 | for (i=0;i<len;i++) { | ||
68 | if ((i % 16) == 0) { | ||
69 | fprintf(fp,"\n "); | ||
70 | } | ||
71 | if (i == (len-1)) { | ||
72 | fprintf(fp,"0x%02x",buf[i]); | ||
73 | } else { | ||
74 | fprintf(fp,"0x%02x, ",buf[i]); | ||
75 | } | ||
76 | } | ||
77 | fprintf(fp,"\n};\n"); | ||
78 | |||
79 | fclose(fp); | ||
80 | return 0; | ||
81 | } | ||
82 | |||
83 | static int write_hfile(off_t len, const char* cname) | ||
84 | { | ||
85 | char filename[256]; | ||
86 | FILE* fp; | ||
87 | |||
88 | snprintf(filename,256,"%s.h",cname); | ||
89 | fp = fopen(filename,"w+"); | ||
90 | if (fp == NULL) { | ||
91 | fprintf(stderr,"Couldn't open %s\n",filename); | ||
92 | return -1; | ||
93 | } | ||
94 | |||
95 | fprintf(fp,"/* Generated by bin2c */\n\n"); | ||
96 | fprintf(fp,"#define LEN_%s %d\n",cname,(int)len); | ||
97 | fprintf(fp,"extern unsigned char %s[];\n",cname); | ||
98 | fclose(fp); | ||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | int main (int argc, char* argv[]) | ||
103 | { | ||
104 | char* infile; | ||
105 | char* cname; | ||
106 | int fd; | ||
107 | unsigned char* buf; | ||
108 | int len; | ||
109 | int n; | ||
110 | |||
111 | if (argc != 3) { | ||
112 | fprintf(stderr,"Usage: bin2c file cname\n"); | ||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | infile=argv[1]; | ||
117 | cname=argv[2]; | ||
118 | |||
119 | fd = open(infile,O_RDONLY|O_BINARY); | ||
120 | if (fd < 0) { | ||
121 | fprintf(stderr,"Can not open %s\n",infile); | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | len = filesize(fd); | ||
126 | |||
127 | buf = malloc(len); | ||
128 | n = read(fd,buf,len); | ||
129 | if (n < len) { | ||
130 | fprintf(stderr,"Short read, aborting\n"); | ||
131 | return 0; | ||
132 | } | ||
133 | close(fd); | ||
134 | |||
135 | if (write_cfile(buf,len,cname) < 0) { | ||
136 | return -1; | ||
137 | } | ||
138 | if (write_hfile(len,cname) < 0) { | ||
139 | return -1; | ||
140 | } | ||
141 | |||
142 | return 0; | ||
143 | } | ||