2013-06-05 01:56:07 +00:00
|
|
|
/*
|
|
|
|
exhal - HAL Laboratory decompression tool
|
2013-11-15 21:40:53 +00:00
|
|
|
|
2013-06-05 01:56:07 +00:00
|
|
|
Usage:
|
|
|
|
exhal romfile offset outfile
|
|
|
|
|
2013-11-15 21:40:53 +00:00
|
|
|
Copyright (c) 2013 Devin Acker
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
2013-06-05 01:56:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-10-26 18:25:23 +00:00
|
|
|
#include <stdlib.h>
|
2013-06-05 01:56:07 +00:00
|
|
|
#include "compress.h"
|
|
|
|
|
|
|
|
int main (int argc, char **argv) {
|
2013-11-14 03:34:17 +00:00
|
|
|
printf("exhal - " __DATE__ " " __TIME__"\nby Devin Acker (Revenant)\n\n");
|
2013-06-05 01:56:07 +00:00
|
|
|
|
|
|
|
if (argc != 4) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Usage:\n%s romfile offset outfile\n"
|
|
|
|
"Example: %s kirbybowl.sfc 0x70000 test.bin\n\n"
|
|
|
|
"offset can be in either decimal or hex.\n",
|
|
|
|
argv[0], argv[0]);
|
2013-06-05 01:56:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *infile, *outfile;
|
|
|
|
|
|
|
|
// open ROM file for input
|
|
|
|
infile = fopen(argv[1], "rb");
|
|
|
|
if (!infile) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: unable to open %s\n", argv[1]);
|
2013-06-05 01:56:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// open target file for output
|
|
|
|
outfile = fopen(argv[3], "wb");
|
|
|
|
if (!outfile) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: unable to open %s\n", argv[3]);
|
2013-06-05 01:56:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2013-11-14 03:31:06 +00:00
|
|
|
size_t outputsize, fileoffset;
|
|
|
|
uint8_t unpacked[DATA_SIZE] = {0};
|
2013-06-05 01:56:07 +00:00
|
|
|
|
|
|
|
fileoffset = strtol(argv[2], NULL, 0);
|
|
|
|
|
2013-06-06 01:32:03 +00:00
|
|
|
// decompress the file
|
2013-11-14 03:31:06 +00:00
|
|
|
fseek(infile, 0, SEEK_END);
|
|
|
|
if (fileoffset < ftell(infile)) {
|
|
|
|
outputsize = unpack_from_file(infile, fileoffset, unpacked);
|
|
|
|
} else {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: Unable to decompress %s because an invalid offset was specified\n"
|
|
|
|
" (must be between zero and 0x%X).\n", argv[1], ftell(infile));
|
2013-11-15 21:23:10 +00:00
|
|
|
exit(-1);
|
2013-11-14 03:31:06 +00:00
|
|
|
}
|
2013-06-05 01:56:07 +00:00
|
|
|
|
2013-11-14 03:31:06 +00:00
|
|
|
if (outputsize) {
|
|
|
|
// write the uncompressed data to the file
|
|
|
|
fseek(outfile, 0, SEEK_SET);
|
|
|
|
fwrite((const void*)unpacked, 1, outputsize, outfile);
|
2013-11-14 03:56:13 +00:00
|
|
|
if (ferror(outfile)) {
|
|
|
|
perror("Error writing output file");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2013-11-14 03:31:06 +00:00
|
|
|
|
|
|
|
printf("Uncompressed size: %zu bytes\n", outputsize);
|
|
|
|
} else {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: Unable to decompress %s because the output would have been larger than\n"
|
|
|
|
" 64 kb. The input at 0x%X is likely not valid compressed data.\n", argv[1], fileoffset);
|
2013-11-14 03:31:06 +00:00
|
|
|
}
|
2013-06-05 01:56:07 +00:00
|
|
|
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
}
|