Rename jz4740 tools + add some new ones
[Rockbox.git] / utils / jz4740_tools / HXFsplit.c
blobede22170e4203a889e0db5727ae51a0e27a60e3b
1 /*
2 Made by Maurus Cuelenaere
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <sys/stat.h>
12 #include <stdbool.h>
14 #define VERSION "0.2"
16 struct header{
17 char main_header[20];
18 unsigned int size;
19 unsigned int checksum;
20 unsigned int unknown;
21 char other_header[32];
24 static char* basepath(char* path)
26 static char tmp[255];
27 char *ptr, *ptr2, *ptr3;
28 ptr = path;
29 ptr2 = (char*)tmp;
30 #ifdef _WIN32
31 ptr3 = strrchr(path, 0x5C);
32 #else
33 ptr3 = strrchr(path, 0x2F);
34 #endif
35 while((int)ptr < (int)ptr3)
37 *ptr2 = *ptr;
38 ptr++;
39 ptr2++;
41 #ifdef _WIN32
42 *ptr2 = 0x5C;
43 #else
44 *ptr2 = 0x2F;
45 #endif
46 *ptr2++;
47 *ptr2 = 0;
48 return (char*)tmp;
51 #ifndef _WIN32
52 static void replace(char* str)
54 char *ptr = str;
55 while(*ptr != 0)
57 if(*ptr == 0x5C) /* \ */
58 *ptr = 0x2F; /* / */
59 ptr++;
62 #endif
64 static unsigned int le2int(unsigned char* buf)
66 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
68 return res;
71 #ifdef _WIN32
72 #define PATH_SEPARATOR '\\'
73 #else
74 #define PATH_SEPARATOR '/'
75 #endif
77 static unsigned int __mkdir(const char *path)
79 char opath[256];
80 char *p;
81 size_t len;
83 strncpy(opath, path, sizeof(opath));
84 len = strlen(opath);
85 if(opath[len - 1] == PATH_SEPARATOR)
86 opath[len - 1] = '\0';
87 for(p = opath; *p; p++)
88 if(*p == PATH_SEPARATOR)
90 *p = '\0';
91 if(access(opath, F_OK))
92 #ifdef _WIN32
93 mkdir(opath);
94 #else
95 mkdir(opath, S_IRWXU);
96 #endif
97 *p = PATH_SEPARATOR;
99 if(access(opath, F_OK))
100 #ifdef _WIN32
101 return mkdir(opath);
102 #else
103 return mkdir(opath, S_IRWXU);
104 #endif
105 else
106 return -1;
109 #if 0
110 static bool dir_exists(const char *dir)
112 struct stat buf;
113 memset(&buf, 0, sizeof(struct stat));
114 printf("start: %s\n", dir);
115 char *dir_cpy = (char*)malloc(strlen(dir));
116 strcpy(dir_cpy, dir);
117 printf("%s\n", dir_cpy);
118 int tmp = (int)dir_cpy;
119 while(*dir_cpy != 0)
121 dir_cpy++;
122 if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0)
123 *dir_cpy = 0;
125 printf("while_done\n");
126 dir_cpy = (char*)tmp;
127 printf("statting %s...\n", dir_cpy);
128 tmp = stat(dir_cpy, &buf);
129 printf("chk_dir(%s) = %d\n", dir_cpy, tmp);
130 free(dir_cpy);
131 return tmp == 0;
133 #endif
135 static bool file_exists(const char *file)
137 struct stat buf;
138 return stat(file, &buf) == 0;
142 static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath)
144 FILE *outfile;
145 char *filename;
146 unsigned int filenamesize, filesize;
147 while(size > 0)
149 filenamesize = le2int((unsigned char*)infile);
150 infile += 4;
151 size -= 4;
152 if(size > 0)
154 filename = (char*)calloc(1, filenamesize+1+strlen(outpath));
155 memcpy(filename, outpath, strlen(outpath));
156 memcpy(&filename[strlen(outpath)], infile, filenamesize);
157 #ifndef _WIN32
158 replace(filename);
159 #endif
160 infile += filenamesize + 1; /* + padding */
161 size -= filenamesize + 1;
163 filesize = le2int((unsigned char*)infile);
164 infile += 4;
165 size -= 4;
166 #if 0
167 if(!dir_exists(basepath(filename)))
168 #endif
170 printf("[INFO] %s\n", basepath(filename));
171 if(__mkdir(basepath(filename)) != 0)
173 #if 0
174 fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename));
175 return -3;
176 #endif
180 if(!file_exists(filename))
182 printf("[INFO] %s: %d bytes\n", filename, filesize);
183 if((outfile = fopen(filename, "wb")) == NULL)
185 fprintf(stderr, "[ERR] Error opening file %s\n", filename);
186 return -1;
188 if(filesize>0)
190 if(fwrite(infile, filesize, 1, outfile) != 1)
192 fclose(outfile);
193 fprintf(stderr, "[ERR] Error writing to file %s\n", filename);
194 return -2;
197 fclose(outfile);
200 infile += filesize;
201 size -= filesize;
204 return 0;
207 static void print_usage(void)
209 #ifdef _WIN32
210 fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
211 fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
212 #else
213 fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
214 fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
215 #endif
218 int main(int argc, char *argv[])
220 FILE *infile;
221 struct header hdr;
222 unsigned char *inbuffer;
224 fprintf(stderr, "HXFsplit v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
225 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
226 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
228 if(argc != 3)
230 print_usage();
231 return 1;
234 #ifdef _WIN32
235 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0)
237 fprintf(stderr, "[ERR] Output path must end with a \\\n");
238 #else
239 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0)
241 fprintf(stderr, "[ERR] Output path must end with a /\n");
242 #endif
243 return 2;
246 if((infile = fopen(argv[1], "rb")) == NULL)
248 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
249 return 3;
252 if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL)
254 fclose(infile);
255 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header));
256 return 4;
259 if(fread(inbuffer, sizeof(struct header), 1, infile) != 1)
261 fclose(infile);
262 fprintf(stderr, "Cannot read header of %s\n", argv[1]);
263 return 5;
266 memcpy(hdr.main_header, inbuffer, 20);
267 hdr.size = le2int(&inbuffer[20]);
268 hdr.checksum = le2int(&inbuffer[24]);
269 hdr.unknown = le2int(&inbuffer[28]);
270 memcpy(hdr.other_header, &inbuffer[32], 32);
271 free(inbuffer);
273 if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0)
275 fclose(infile);
276 fprintf(stderr, "[ERR] Header doesn't match\n");
277 return 6;
280 if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL)
282 fclose(infile);
283 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size);
284 return 7;
287 fseek(infile, sizeof(struct header), SEEK_SET);
289 if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1)
291 fclose(infile);
292 free(inbuffer);
293 fprintf(stderr, "[ERR] Cannot read file in buffer\n");
294 return 8;
297 fclose(infile);
299 split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]);
301 free(inbuffer);
303 return 0;