2 Made by Maurus Cuelenaere
19 unsigned int checksum
;
21 char other_header
[32];
24 static char* basepath(char* path
)
27 char *ptr
, *ptr2
, *ptr3
;
31 ptr3
= strrchr(path
, 0x5C);
33 ptr3
= strrchr(path
, 0x2F);
35 while((int)ptr
< (int)ptr3
)
52 static void replace(char* str
)
57 if(*ptr
== 0x5C) /* \ */
64 static unsigned int le2int(unsigned char* buf
)
66 unsigned int res
= (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
72 #define PATH_SEPARATOR '\\'
74 #define PATH_SEPARATOR '/'
77 static unsigned int __mkdir(const char *path
)
83 strncpy(opath
, path
, sizeof(opath
));
85 if(opath
[len
- 1] == PATH_SEPARATOR
)
86 opath
[len
- 1] = '\0';
87 for(p
= opath
; *p
; p
++)
88 if(*p
== PATH_SEPARATOR
)
91 if(access(opath
, F_OK
))
95 mkdir(opath
, S_IRWXU
);
99 if(access(opath
, F_OK
))
103 return mkdir(opath
, S_IRWXU
);
110 static bool dir_exists(const char *dir
)
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
;
122 if(*dir_cpy
== PATH_SEPARATOR
&& *(dir_cpy
+1) == 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
);
135 static bool file_exists(const char *file
)
138 return stat(file
, &buf
) == 0;
142 static int split_hxf(const unsigned char* infile
, unsigned int size
, const char* outpath
)
146 unsigned int filenamesize
, filesize
;
149 filenamesize
= le2int((unsigned char*)infile
);
154 filename
= (char*)calloc(1, filenamesize
+1+strlen(outpath
));
155 memcpy(filename
, outpath
, strlen(outpath
));
156 memcpy(&filename
[strlen(outpath
)], infile
, filenamesize
);
160 infile
+= filenamesize
+ 1; /* + padding */
161 size
-= filenamesize
+ 1;
163 filesize
= le2int((unsigned char*)infile
);
167 if(!dir_exists(basepath(filename
)))
170 printf("[INFO] %s\n", basepath(filename
));
171 if(__mkdir(basepath(filename
)) != 0)
174 fprintf(stderr
, "[ERR] Error creating directory %s\n", basepath(filename
));
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
);
190 if(fwrite(infile
, filesize
, 1, outfile
) != 1)
193 fprintf(stderr
, "[ERR] Error writing to file %s\n", filename
);
207 static void print_usage(void)
210 fprintf(stderr
, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
211 fprintf(stderr
, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
213 fprintf(stderr
, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
214 fprintf(stderr
, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
218 int main(int argc
, char *argv
[])
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");
235 if(strcmp((char*)(argv
[2]+strlen(argv
[2])-1), "\\") != 0)
237 fprintf(stderr
, "[ERR] Output path must end with a \\\n");
239 if(strcmp((char*)(argv
[2]+strlen(argv
[2])-1), "/") != 0)
241 fprintf(stderr
, "[ERR] Output path must end with a /\n");
246 if((infile
= fopen(argv
[1], "rb")) == NULL
)
248 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[1]);
252 if((inbuffer
= (unsigned char*)malloc(sizeof(struct header
))) == NULL
)
255 fprintf(stderr
, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header
));
259 if(fread(inbuffer
, sizeof(struct header
), 1, infile
) != 1)
262 fprintf(stderr
, "Cannot read header of %s\n", argv
[1]);
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);
273 if(strcmp(hdr
.other_header
, "Chinachip PMP firmware V1.0") != 0)
276 fprintf(stderr
, "[ERR] Header doesn't match\n");
280 if((inbuffer
= (unsigned char*)malloc(hdr
.size
)) == NULL
)
283 fprintf(stderr
, "[ERR] Error allocating %d bytes buffer\n", hdr
.size
);
287 fseek(infile
, sizeof(struct header
), SEEK_SET
);
289 if(fread(inbuffer
, hdr
.size
-sizeof(struct header
), 1, infile
) != 1)
293 fprintf(stderr
, "[ERR] Cannot read file in buffer\n");
299 split_hxf(inbuffer
, hdr
.size
-sizeof(struct header
), argv
[2]);