Build doom on clipv2 and clip+
[kugel-rb.git] / utils / jz4740_tools / HXFsplit.c
blobdbeace8a202fa64ce6832e9431bf3c18f09854a1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <stdbool.h>
31 #define VERSION "0.2"
33 struct header{
34 char main_header[20];
35 unsigned int size;
36 unsigned int checksum;
37 unsigned int unknown;
38 char other_header[32];
41 static char* basepath(char* path)
43 static char tmp[255];
44 char *ptr, *ptr2, *ptr3;
45 ptr = path;
46 ptr2 = (char*)tmp;
47 #ifdef _WIN32
48 ptr3 = strrchr(path, 0x5C);
49 #else
50 ptr3 = strrchr(path, 0x2F);
51 #endif
52 while((int)ptr < (int)ptr3)
54 *ptr2 = *ptr;
55 ptr++;
56 ptr2++;
58 #ifdef _WIN32
59 *ptr2 = 0x5C;
60 #else
61 *ptr2 = 0x2F;
62 #endif
63 ptr2++;
64 *ptr2 = 0;
65 return (char*)tmp;
68 #ifndef _WIN32
69 static void replace(char* str)
71 char *ptr = str;
72 while(*ptr != 0)
74 if(*ptr == 0x5C) /* \ */
75 *ptr = 0x2F; /* / */
76 ptr++;
79 #endif
81 static unsigned int le2int(unsigned char* buf)
83 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
85 return res;
88 #ifdef _WIN32
89 #define PATH_SEPARATOR '\\'
90 #else
91 #define PATH_SEPARATOR '/'
92 #endif
94 static unsigned int __mkdir(const char *path)
96 char opath[256];
97 char *p;
98 size_t len;
100 strncpy(opath, path, sizeof(opath));
101 len = strlen(opath);
102 if(opath[len - 1] == PATH_SEPARATOR)
103 opath[len - 1] = '\0';
104 for(p = opath; *p; p++)
105 if(*p == PATH_SEPARATOR)
107 *p = '\0';
108 if(access(opath, F_OK))
109 #ifdef _WIN32
110 mkdir(opath);
111 #else
112 mkdir(opath, S_IRWXU);
113 #endif
114 *p = PATH_SEPARATOR;
116 if(access(opath, F_OK))
117 #ifdef _WIN32
118 return mkdir(opath);
119 #else
120 return mkdir(opath, S_IRWXU);
121 #endif
122 else
123 return -1;
126 #if 0
127 static bool dir_exists(const char *dir)
129 struct stat buf;
130 memset(&buf, 0, sizeof(struct stat));
131 printf("start: %s\n", dir);
132 char *dir_cpy = (char*)malloc(strlen(dir));
133 strcpy(dir_cpy, dir);
134 printf("%s\n", dir_cpy);
135 int tmp = (int)dir_cpy;
136 while(*dir_cpy != 0)
138 dir_cpy++;
139 if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0)
140 *dir_cpy = 0;
142 printf("while_done\n");
143 dir_cpy = (char*)tmp;
144 printf("statting %s...\n", dir_cpy);
145 tmp = stat(dir_cpy, &buf);
146 printf("chk_dir(%s) = %d\n", dir_cpy, tmp);
147 free(dir_cpy);
148 return tmp == 0;
150 #endif
152 static bool file_exists(const char *file)
154 struct stat buf;
155 return stat(file, &buf) == 0;
159 static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath)
161 FILE *outfile;
162 char *filename;
163 unsigned int filenamesize, filesize;
164 while(size > 0)
166 filenamesize = le2int((unsigned char*)infile);
167 infile += 4;
168 size -= 4;
169 if(size > 0)
171 filename = (char*)calloc(1, filenamesize+1+strlen(outpath));
172 memcpy(filename, outpath, strlen(outpath));
173 memcpy(&filename[strlen(outpath)], infile, filenamesize);
174 #ifndef _WIN32
175 replace(filename);
176 #endif
177 infile += filenamesize + 1; /* + padding */
178 size -= filenamesize + 1;
180 filesize = le2int((unsigned char*)infile);
181 infile += 4;
182 size -= 4;
183 #if 0
184 if(!dir_exists(basepath(filename)))
185 #endif
187 printf("[INFO] %s\n", basepath(filename));
188 if(__mkdir(basepath(filename)) != 0)
190 #if 0
191 fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename));
192 return -3;
193 #endif
197 if(!file_exists(filename))
199 printf("[INFO] %s: %d bytes\n", filename, filesize);
200 if((outfile = fopen(filename, "wb")) == NULL)
202 fprintf(stderr, "[ERR] Error opening file %s\n", filename);
203 return -1;
205 if(filesize>0)
207 if(fwrite(infile, filesize, 1, outfile) != 1)
209 fclose(outfile);
210 fprintf(stderr, "[ERR] Error writing to file %s\n", filename);
211 return -2;
214 fclose(outfile);
217 infile += filesize;
218 size -= filesize;
221 return 0;
224 static void print_usage(void)
226 #ifdef _WIN32
227 fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
228 fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
229 #else
230 fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
231 fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
232 #endif
235 int main(int argc, char *argv[])
237 FILE *infile;
238 struct header hdr;
239 unsigned char *inbuffer;
241 fprintf(stderr, "HXFsplit v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
242 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
243 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
245 if(argc != 3)
247 print_usage();
248 return 1;
251 #ifdef _WIN32
252 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0)
254 fprintf(stderr, "[ERR] Output path must end with a \\\n");
255 #else
256 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0)
258 fprintf(stderr, "[ERR] Output path must end with a /\n");
259 #endif
260 return 2;
263 if((infile = fopen(argv[1], "rb")) == NULL)
265 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
266 return 3;
269 if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL)
271 fclose(infile);
272 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header));
273 return 4;
276 if(fread(inbuffer, sizeof(struct header), 1, infile) != 1)
278 fclose(infile);
279 fprintf(stderr, "Cannot read header of %s\n", argv[1]);
280 return 5;
283 memcpy(hdr.main_header, inbuffer, 20);
284 hdr.size = le2int(&inbuffer[20]);
285 hdr.checksum = le2int(&inbuffer[24]);
286 hdr.unknown = le2int(&inbuffer[28]);
287 memcpy(hdr.other_header, &inbuffer[32], 32);
288 free(inbuffer);
290 if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0)
292 fclose(infile);
293 fprintf(stderr, "[ERR] Header doesn't match\n");
294 return 6;
297 if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL)
299 fclose(infile);
300 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size);
301 return 7;
304 fseek(infile, sizeof(struct header), SEEK_SET);
306 if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1)
308 fclose(infile);
309 free(inbuffer);
310 fprintf(stderr, "[ERR] Cannot read file in buffer\n");
311 return 8;
314 fclose(infile);
316 split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]);
318 free(inbuffer);
320 return 0;