2 Made by Maurus Cuelenaere
17 static unsigned char* int2le(unsigned int val
)
19 static unsigned char addr
[4];
21 addr
[1] = (val
>> 8) & 0xff;
22 addr
[2] = (val
>> 16) & 0xff;
23 addr
[3] = (val
>> 24) & 0xff;
27 static unsigned int le2int(unsigned char* buf
)
29 unsigned int res
= (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
34 unsigned int _filesize(FILE* fd
)
36 unsigned int tmp
, oldpos
;
38 fseek(fd
, 0, SEEK_END
);
40 fseek(fd
, oldpos
, SEEK_SET
);
44 static void print_usage(void)
47 fprintf(stderr
, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
48 fprintf(stderr
, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n");
50 fprintf(stderr
, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
51 fprintf(stderr
, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n");
55 static int checksum(FILE *file
)
57 int oldpos
= ftell(file
);
58 int ret
=0, i
, filesize
= _filesize(file
)-0x40;
61 buf
= (unsigned char*)malloc(filesize
);
65 fseek(file
, oldpos
, SEEK_SET
);
66 fprintf(stderr
, "[ERR] Error while allocating memory\n");
70 fseek(file
, 0x40, SEEK_SET
);
71 if(fread(buf
, filesize
, 1, file
) != 1)
74 fseek(file
, oldpos
, SEEK_SET
);
75 fprintf(stderr
, "[ERR] Error while reading from file\n");
79 fprintf(stderr
, "[INFO] Computing checksum...");
81 for(i
= 0; i
< filesize
; i
+=4)
82 ret
+= le2int(&buf
[i
]);
85 fseek(file
, oldpos
, SEEK_SET
);
87 fprintf(stderr
, " Done!\n");
91 int main(int argc
, char *argv
[])
93 FILE *infile
, *outfile
, *fw
;
95 fprintf(stderr
, "HXFreplace v" VERSION
" - (C) 2008 Maurus Cuelenaere\n");
96 fprintf(stderr
, "This is free software; see the source for copying conditions. There is NO\n");
97 fprintf(stderr
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
105 if((infile
= fopen(argv
[1], "rb")) == NULL
)
107 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[1]);
111 if(fseek(infile
, 0x40, SEEK_SET
) != 0)
113 fprintf(stderr
, "[ERR] Cannot seek to 0x40\n");
118 fprintf(stderr
, "[INFO] Searching for ccpmp.bin...\n");
123 unsigned char tmp
[4];
125 #define READ(x, len) if(fread(x, len, 1, infile) != 1) \
127 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \
134 filenamesize
= le2int(tmp
);
135 filename
= (char*)malloc(filenamesize
);
136 READ(filename
, filenamesize
);
137 if(strcmp(filename
, "ccpmp.bin") == 0)
138 found
= ftell(infile
);
142 fseek(infile
, le2int(tmp
), SEEK_CUR
);
147 fprintf(stderr
, "[INFO] Found ccpmp.bin at 0x%x\n", found
);
149 if((outfile
= fopen(argv
[2], "wb+")) == NULL
)
152 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[2]);
156 #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
158 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \
165 unsigned char* buffer
;
167 buffer
= (unsigned char*)malloc(found
);
168 fseek(infile
, 0, SEEK_SET
);
170 WRITE(buffer
, found
);
173 if((fw
= fopen(argv
[3], "rb")) == NULL
)
177 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[3]);
180 int fw_filesize
= _filesize(fw
);
182 #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \
184 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \
189 buffer
= (unsigned char*)malloc(fw_filesize
);
190 READ2(buffer
, fw_filesize
);
191 fputc(0x20, outfile
); /* Padding */
192 WRITE(int2le(fw_filesize
), 4);
193 WRITE(buffer
, fw_filesize
);
198 fseek(infile
, found
+1, SEEK_SET
);
200 if(fseek(infile
, le2int(&tmp
[0]), SEEK_CUR
) != 0)
202 fprintf(stderr
, "[INFO] Cannot seek into %s\n", argv
[1]);
207 found
= ftell(infile
);
209 int other_size
= _filesize(infile
) - found
;
210 buffer
= (unsigned char*)malloc(other_size
);
211 READ(buffer
, other_size
);
212 WRITE(buffer
, other_size
);
217 fseek(outfile
, 0x14, SEEK_SET
);
218 WRITE(int2le(_filesize(outfile
)), 4);
219 WRITE(int2le(checksum(outfile
)), 4);
222 fprintf(stderr
, "[INFO] Done!\n");