1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 #include <sys/types.h>
36 unsigned int checksum
;
38 char other_header
[32];
41 static char* basepath(char* path
)
44 char *ptr
, *ptr2
, *ptr3
;
48 ptr3
= strrchr(path
, 0x5C);
50 ptr3
= strrchr(path
, 0x2F);
52 while((int)ptr
< (int)ptr3
)
69 static void replace(char* str
)
74 if(*ptr
== 0x5C) /* \ */
81 static unsigned int le2int(unsigned char* buf
)
83 unsigned int res
= (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
89 #define PATH_SEPARATOR '\\'
91 #define PATH_SEPARATOR '/'
94 static unsigned int __mkdir(const char *path
)
100 strncpy(opath
, path
, sizeof(opath
));
102 if(opath
[len
- 1] == PATH_SEPARATOR
)
103 opath
[len
- 1] = '\0';
104 for(p
= opath
; *p
; p
++)
105 if(*p
== PATH_SEPARATOR
)
108 if(access(opath
, F_OK
))
112 mkdir(opath
, S_IRWXU
);
116 if(access(opath
, F_OK
))
120 return mkdir(opath
, S_IRWXU
);
127 static bool dir_exists(const char *dir
)
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
;
139 if(*dir_cpy
== PATH_SEPARATOR
&& *(dir_cpy
+1) == 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
);
152 static bool file_exists(const char *file
)
155 return stat(file
, &buf
) == 0;
159 static int split_hxf(const unsigned char* infile
, unsigned int size
, const char* outpath
)
163 unsigned int filenamesize
, filesize
;
166 filenamesize
= le2int((unsigned char*)infile
);
171 filename
= (char*)calloc(1, filenamesize
+1+strlen(outpath
));
172 memcpy(filename
, outpath
, strlen(outpath
));
173 memcpy(&filename
[strlen(outpath
)], infile
, filenamesize
);
177 infile
+= filenamesize
+ 1; /* + padding */
178 size
-= filenamesize
+ 1;
180 filesize
= le2int((unsigned char*)infile
);
184 if(!dir_exists(basepath(filename
)))
187 printf("[INFO] %s\n", basepath(filename
));
188 if(__mkdir(basepath(filename
)) != 0)
191 fprintf(stderr
, "[ERR] Error creating directory %s\n", basepath(filename
));
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
);
207 if(fwrite(infile
, filesize
, 1, outfile
) != 1)
210 fprintf(stderr
, "[ERR] Error writing to file %s\n", filename
);
224 static void print_usage(void)
227 fprintf(stderr
, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
228 fprintf(stderr
, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
230 fprintf(stderr
, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
231 fprintf(stderr
, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
235 int main(int argc
, char *argv
[])
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");
252 if(strcmp((char*)(argv
[2]+strlen(argv
[2])-1), "\\") != 0)
254 fprintf(stderr
, "[ERR] Output path must end with a \\\n");
256 if(strcmp((char*)(argv
[2]+strlen(argv
[2])-1), "/") != 0)
258 fprintf(stderr
, "[ERR] Output path must end with a /\n");
263 if((infile
= fopen(argv
[1], "rb")) == NULL
)
265 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[1]);
269 if((inbuffer
= (unsigned char*)malloc(sizeof(struct header
))) == NULL
)
272 fprintf(stderr
, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header
));
276 if(fread(inbuffer
, sizeof(struct header
), 1, infile
) != 1)
279 fprintf(stderr
, "Cannot read header of %s\n", argv
[1]);
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);
290 if(strcmp(hdr
.other_header
, "Chinachip PMP firmware V1.0") != 0)
293 fprintf(stderr
, "[ERR] Header doesn't match\n");
297 if((inbuffer
= (unsigned char*)malloc(hdr
.size
)) == NULL
)
300 fprintf(stderr
, "[ERR] Error allocating %d bytes buffer\n", hdr
.size
);
304 fseek(infile
, sizeof(struct header
), SEEK_SET
);
306 if(fread(inbuffer
, hdr
.size
-sizeof(struct header
), 1, infile
) != 1)
310 fprintf(stderr
, "[ERR] Cannot read file in buffer\n");
316 split_hxf(inbuffer
, hdr
.size
-sizeof(struct header
), argv
[2]);