summaryrefslogtreecommitdiff
path: root/utils/jz4740_tools/HXFsplit.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/jz4740_tools/HXFsplit.c')
-rwxr-xr-xutils/jz4740_tools/HXFsplit.c304
1 files changed, 304 insertions, 0 deletions
diff --git a/utils/jz4740_tools/HXFsplit.c b/utils/jz4740_tools/HXFsplit.c
new file mode 100755
index 0000000000..ede22170e4
--- /dev/null
+++ b/utils/jz4740_tools/HXFsplit.c
@@ -0,0 +1,304 @@
1/*
2Made by Maurus Cuelenaere
3*/
4
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>
13
14#define VERSION "0.2"
15
16struct header{
17 char main_header[20];
18 unsigned int size;
19 unsigned int checksum;
20 unsigned int unknown;
21 char other_header[32];
22};
23
24static char* basepath(char* path)
25{
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)
36 {
37 *ptr2 = *ptr;
38 ptr++;
39 ptr2++;
40 }
41#ifdef _WIN32
42 *ptr2 = 0x5C;
43#else
44 *ptr2 = 0x2F;
45#endif
46 *ptr2++;
47 *ptr2 = 0;
48 return (char*)tmp;
49}
50
51#ifndef _WIN32
52static void replace(char* str)
53{
54 char *ptr = str;
55 while(*ptr != 0)
56 {
57 if(*ptr == 0x5C) /* \ */
58 *ptr = 0x2F; /* / */
59 ptr++;
60 }
61}
62#endif
63
64static unsigned int le2int(unsigned char* buf)
65{
66 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
67
68 return res;
69}
70
71#ifdef _WIN32
72 #define PATH_SEPARATOR '\\'
73#else
74 #define PATH_SEPARATOR '/'
75#endif
76
77static unsigned int __mkdir(const char *path)
78{
79 char opath[256];
80 char *p;
81 size_t len;
82
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)
89 {
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;
98 }
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;
107}
108
109#if 0
110static bool dir_exists(const char *dir)
111{
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)
120 {
121 dir_cpy++;
122 if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0)
123 *dir_cpy = 0;
124 }
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;
132}
133#endif
134
135static bool file_exists(const char *file)
136{
137 struct stat buf;
138 return stat(file, &buf) == 0;
139}
140
141
142static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath)
143{
144 FILE *outfile;
145 char *filename;
146 unsigned int filenamesize, filesize;
147 while(size > 0)
148 {
149 filenamesize = le2int((unsigned char*)infile);
150 infile += 4;
151 size -= 4;
152 if(size > 0)
153 {
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;
162
163 filesize = le2int((unsigned char*)infile);
164 infile += 4;
165 size -= 4;
166#if 0
167 if(!dir_exists(basepath(filename)))
168#endif
169 {
170 printf("[INFO] %s\n", basepath(filename));
171 if(__mkdir(basepath(filename)) != 0)
172 {
173#if 0
174 fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename));
175 return -3;
176#endif
177 }
178 }
179
180 if(!file_exists(filename))
181 {
182 printf("[INFO] %s: %d bytes\n", filename, filesize);
183 if((outfile = fopen(filename, "wb")) == NULL)
184 {
185 fprintf(stderr, "[ERR] Error opening file %s\n", filename);
186 return -1;
187 }
188 if(filesize>0)
189 {
190 if(fwrite(infile, filesize, 1, outfile) != 1)
191 {
192 fclose(outfile);
193 fprintf(stderr, "[ERR] Error writing to file %s\n", filename);
194 return -2;
195 }
196 }
197 fclose(outfile);
198 }
199
200 infile += filesize;
201 size -= filesize;
202 }
203 }
204 return 0;
205}
206
207static void print_usage(void)
208{
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
216}
217
218int main(int argc, char *argv[])
219{
220 FILE *infile;
221 struct header hdr;
222 unsigned char *inbuffer;
223
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");
227
228 if(argc != 3)
229 {
230 print_usage();
231 return 1;
232 }
233
234#ifdef _WIN32
235 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0)
236 {
237 fprintf(stderr, "[ERR] Output path must end with a \\\n");
238#else
239 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0)
240 {
241 fprintf(stderr, "[ERR] Output path must end with a /\n");
242#endif
243 return 2;
244 }
245
246 if((infile = fopen(argv[1], "rb")) == NULL)
247 {
248 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
249 return 3;
250 }
251
252 if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL)
253 {
254 fclose(infile);
255 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header));
256 return 4;
257 }
258
259 if(fread(inbuffer, sizeof(struct header), 1, infile) != 1)
260 {
261 fclose(infile);
262 fprintf(stderr, "Cannot read header of %s\n", argv[1]);
263 return 5;
264 }
265
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);
272
273 if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0)
274 {
275 fclose(infile);
276 fprintf(stderr, "[ERR] Header doesn't match\n");
277 return 6;
278 }
279
280 if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL)
281 {
282 fclose(infile);
283 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size);
284 return 7;
285 }
286
287 fseek(infile, sizeof(struct header), SEEK_SET);
288
289 if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1)
290 {
291 fclose(infile);
292 free(inbuffer);
293 fprintf(stderr, "[ERR] Cannot read file in buffer\n");
294 return 8;
295 }
296
297 fclose(infile);
298
299 split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]);
300
301 free(inbuffer);
302
303 return 0;
304}