1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 Dave Chapman
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.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
23 #include <sys/types.h>
27 #if !defined(_MSC_VER)
31 #define snprintf _snprintf
42 static void usage(void)
44 fprintf(stderr
, "bin2c [options] infile cfile\n");
45 fprintf(stderr
, " -i ipod mode\n");
49 static off_t
filesize(int fd
)
57 static int write_cfile(const unsigned char* buf
, off_t len
, const char* cname
)
65 snprintf(filename
,256,"%s.c",cname
);
66 strncpy(filebase
, cname
, 256);
67 bn
= basename(filebase
);
69 fp
= fopen(filename
,"w+");
71 fprintf(stderr
,"Couldn't open %s\n",filename
);
75 fprintf(fp
,"/* Generated by bin2c */\n\n");
76 fprintf(fp
,"unsigned char %s[] = {",bn
);
83 fprintf(fp
,"0x%02x",buf
[i
]);
85 fprintf(fp
,"0x%02x, ",buf
[i
]);
94 static int write_hfile(off_t len
, const char* cname
)
101 snprintf(filename
,256,"%s.h",cname
);
102 strncpy(filebase
, cname
, 256);
103 bn
= basename(filebase
);
104 fp
= fopen(filename
,"w+");
106 fprintf(stderr
,"Couldn't open %s\n",filename
);
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
);
117 int main (int argc
, char* argv
[])
133 if(strcmp(argv
[1], "-i") == 0) {
137 if (argc
< opts
+ 3) {
142 infile
=argv
[opts
+ 1];
143 cname
=argv
[opts
+ 2];
145 fd
= open(infile
,O_RDONLY
|O_BINARY
);
147 fprintf(stderr
,"Can not open %s\n",infile
);
151 len
= filesize(fd
) - skip
;
152 n
= lseek(fd
, skip
, SEEK_SET
);
154 fprintf(stderr
,"Seek failed\n");
159 n
= read(fd
,buf
,len
);
161 fprintf(stderr
,"Short read, aborting\n");
166 if (write_cfile(buf
,len
,cname
) < 0) {
169 if (write_hfile(len
,cname
) < 0) {