-
Notifications
You must be signed in to change notification settings - Fork 8
/
exhal.c
107 lines (88 loc) · 3.59 KB
/
exhal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
exhal - HAL Laboratory decompression tool
Usage:
exhal romfile offset outfile
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.
*/
#include <stdio.h>
#include <stdlib.h>
#include "compress.h"
int main (int argc, char **argv) {
printf("exhal - " __DATE__ " " __TIME__"\nby Devin Acker (Revenant)\n\n");
if (argc != 4) {
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]);
exit(-1);
}
FILE *infile, *outfile;
// open ROM file for input
infile = fopen(argv[1], "rb");
if (!infile) {
fprintf(stderr, "Error: unable to open %s\n", argv[1]);
exit(-1);
}
// open target file for output
outfile = fopen(argv[3], "wb");
if (!outfile) {
fprintf(stderr, "Error: unable to open %s\n", argv[3]);
exit(-1);
}
size_t outputsize, fileoffset;
uint8_t unpacked[DATA_SIZE] = {0};
unpack_stats_t stats;
fileoffset = strtol(argv[2], NULL, 0);
// decompress the file
fseek(infile, 0, SEEK_END);
if (fileoffset < ftell(infile)) {
outputsize = exhal_unpack_from_file(infile, fileoffset, unpacked, &stats);
} else {
fprintf(stderr, "Error: Unable to decompress %s because an invalid offset was specified\n"
" (must be between zero and 0x%lX).\n", argv[1], ftell(infile));
exit(-1);
}
if (outputsize) {
// write the uncompressed data to the file
fseek(outfile, 0, SEEK_SET);
fwrite((const void*)unpacked, 1, outputsize, outfile);
if (ferror(outfile)) {
perror("Error writing output file");
exit(-1);
}
#ifdef EXTRA_OUT
printf("Method Uses\n");
printf("No compression : %i\n", stats.methoduse[0]);
printf("RLE (8-bit) : %i\n", stats.methoduse[1]);
printf("RLE (16-bit) : %i\n", stats.methoduse[2]);
printf("RLE (sequence) : %i\n", stats.methoduse[3]);
printf("Backref (normal) : %i\n", stats.methoduse[4]);
printf("Backref (rotate) : %i\n", stats.methoduse[5]);
printf("Backref (reverse): %i\n", stats.methoduse[6]);
printf("\n");
#endif
printf("Compressed size: %lu bytes\n", (unsigned long)stats.inputsize);
printf("Uncompressed size: %lu bytes\n", (unsigned long)outputsize);
printf("Compression ratio: %4.2f:1\n", (double)outputsize / stats.inputsize);
} else {
fprintf(stderr, "Error: Unable to decompress %s because the output would have been larger than\n"
" 64 kb. The input at 0x%lX is likely not valid compressed data.\n", argv[1], (unsigned long)fileoffset);
}
fclose(infile);
fclose(outfile);
}