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