summaryrefslogtreecommitdiff
path: root/rbutil/ipodpatcher/ipod2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/ipodpatcher/ipod2c.c')
-rw-r--r--rbutil/ipodpatcher/ipod2c.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/rbutil/ipodpatcher/ipod2c.c b/rbutil/ipodpatcher/ipod2c.c
new file mode 100644
index 0000000000..af2e25dd09
--- /dev/null
+++ b/rbutil/ipodpatcher/ipod2c.c
@@ -0,0 +1,139 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: ipodio-win32.c 12205 2007-02-05 01:20:20Z dave $
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
32static off_t filesize(int fd)
33{
34 struct stat buf;
35
36 fstat(fd,&buf);
37 return buf.st_size;
38}
39
40static 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
73static 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
92int 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: ipod2c file.bin cname\n");
103 return 0;
104 }
105
106 infile=argv[1];
107 cname=argv[2];
108
109 fd = open(infile,O_RDONLY);
110 if (fd < 0) {
111 fprintf(stderr,"Can not open %s\n",infile);
112 return 0;
113 }
114
115 len = filesize(fd) - 8;
116
117 n = lseek(fd,8,SEEK_SET);
118 if (n != 8) {
119 fprintf(stderr,"Seek failed\n");
120 return 0;
121 }
122
123 buf = malloc(len);
124 n = read(fd,buf,len);
125 if (n < len) {
126 fprintf(stderr,"Short read, aborting\n");
127 return 0;
128 }
129 close(fd);
130
131 if (write_cfile(buf,len,cname) < 0) {
132 return -1;
133 }
134 if (write_hfile(buf,len,cname) < 0) {
135 return -1;
136 }
137
138 return 0;
139}