When applying the system proxy values really use the values.
[Rockbox.git] / rbutil / ipodpatcher / ipod2c.c
bloba39f780f081b275a65afd98fa155b06d51559f30
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: ipodio-win32.c 12205 2007-02-05 01:20:20Z dave $
10 * Copyright (C) 2007 Dave Chapman
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
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>
28 #ifndef O_BINARY
29 #define O_BINARY 0
30 #endif
32 static off_t filesize(int fd)
34 struct stat buf;
36 fstat(fd,&buf);
37 return buf.st_size;
40 static int write_cfile(unsigned char* buf, off_t len, char* cname)
42 char filename[256];
43 FILE* fp;
44 int i;
46 snprintf(filename,256,"%s.c",cname);
48 fp = fopen(filename,"w+");
49 if (fp == NULL) {
50 fprintf(stderr,"Couldn't open %s\n",filename);
51 return -1;
54 fprintf(fp,"/* Generated by ipod2c */\n\n");
55 fprintf(fp,"unsigned char %s[] = {",cname);
57 for (i=0;i<len;i++) {
58 if ((i % 16) == 0) {
59 fprintf(fp,"\n ");
61 if (i == (len-1)) {
62 fprintf(fp,"0x%02x",buf[i]);
63 } else {
64 fprintf(fp,"0x%02x, ",buf[i]);
67 fprintf(fp,"\n};\n");
69 fclose(fp);
70 return 0;
73 static int write_hfile(unsigned char* buf, off_t len, char* cname)
75 char filename[256];
76 FILE* fp;
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;
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;
92 int main (int argc, char* argv[])
94 char* infile;
95 char* cname;
96 int fd;
97 unsigned char* buf;
98 int len;
99 int n;
101 if (argc != 3) {
102 fprintf(stderr,"Usage: ipod2c file.ipod cname\n");
103 return 0;
106 infile=argv[1];
107 cname=argv[2];
109 fd = open(infile,O_RDONLY|O_BINARY);
110 if (fd < 0) {
111 fprintf(stderr,"Can not open %s\n",infile);
112 return 0;
115 len = filesize(fd) - 8;
117 n = lseek(fd,8,SEEK_SET);
118 if (n != 8) {
119 fprintf(stderr,"Seek failed\n");
120 return 0;
123 buf = malloc(len);
124 n = read(fd,buf,len);
125 if (n < len) {
126 fprintf(stderr,"Short read, aborting\n");
127 return 0;
129 close(fd);
131 if (write_cfile(buf,len,cname) < 0) {
132 return -1;
134 if (write_hfile(buf,len,cname) < 0) {
135 return -1;
138 return 0;