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