2013-06-05 01:56:07 +00:00
|
|
|
/*
|
|
|
|
inhal - HAL Laboratory compression tool
|
2013-11-15 21:40:53 +00:00
|
|
|
|
2013-06-05 01:56:07 +00:00
|
|
|
Usage:
|
2013-11-15 21:40:53 +00:00
|
|
|
inhal [-fast] infile romfile offset
|
|
|
|
inhal -n [-fast] infile outfile
|
2013-06-05 01:56:07 +00:00
|
|
|
|
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 23:14:09 +00:00
|
|
|
#include <time.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("inhal - " __DATE__ " " __TIME__"\nby Devin Acker (Revenant)\n\n");
|
2013-06-05 01:56:07 +00:00
|
|
|
|
2013-06-05 23:50:07 +00:00
|
|
|
if (argc < 4) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "To insert compressed data into a ROM:\n"
|
|
|
|
"%s [-fast] infile romfile offset\n"
|
|
|
|
|
|
|
|
"To write compressed data to a new file:\n"
|
|
|
|
"%s [-fast] -n infile outfile\n\n"
|
|
|
|
|
|
|
|
"Running with the -fast switch increases compression speed at the expense of size.\n"
|
2013-06-05 01:56:07 +00:00
|
|
|
|
2013-11-14 04:32:05 +00:00
|
|
|
"\nExample:\n%s -fast test.chr kirbybowl.sfc 0x70000\n"
|
|
|
|
"%s -n test.chr test-packed.bin\n\n"
|
|
|
|
"offset can be in either decimal or hex.\n",
|
|
|
|
argv[0], argv[0], argv[0], argv[0]);
|
2013-06-05 01:56:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *infile, *outfile;
|
|
|
|
int fileoffset;
|
2013-06-05 23:50:07 +00:00
|
|
|
int newfile = 0;
|
|
|
|
int fast = 0;
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-n"))
|
|
|
|
newfile = 1;
|
|
|
|
else if (!strcmp(argv[i], "-fast"))
|
|
|
|
fast = 1;
|
|
|
|
}
|
2013-06-05 01:56:07 +00:00
|
|
|
|
2013-06-05 23:50:07 +00:00
|
|
|
if (fast)
|
|
|
|
printf("Fast compression enabled.\n");
|
|
|
|
|
2013-06-05 01:56:07 +00:00
|
|
|
// check for -n switch
|
2013-06-05 23:50:07 +00:00
|
|
|
if (newfile) {
|
2013-06-05 01:56:07 +00:00
|
|
|
fileoffset = 0;
|
2013-06-05 23:50:07 +00:00
|
|
|
infile = fopen(argv[argc - 2], "rb");
|
|
|
|
outfile = fopen(argv[argc - 1], "wb");
|
2013-06-05 01:56:07 +00:00
|
|
|
} else {
|
2013-06-05 23:50:07 +00:00
|
|
|
fileoffset = strtol(argv[argc - 1], NULL, 0);
|
|
|
|
infile = fopen(argv[argc - 3], "rb");
|
|
|
|
outfile = fopen(argv[argc - 2], "r+b");
|
2013-06-05 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!infile) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: unable to open input file\n");
|
2013-06-05 01:56:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (!outfile) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: unable to open output file\n");
|
2013-06-05 01:56:07 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t inputsize, outputsize;
|
|
|
|
uint8_t unpacked[DATA_SIZE];
|
2013-06-06 01:32:03 +00:00
|
|
|
uint8_t packed[DATA_SIZE] = {0};
|
2013-06-05 01:56:07 +00:00
|
|
|
|
|
|
|
// check size of input file
|
|
|
|
fseek(infile, 0, SEEK_END);
|
|
|
|
inputsize = ftell(infile);
|
|
|
|
|
2013-11-14 04:32:05 +00:00
|
|
|
printf("Uncompressed size: %zd bytes\n", inputsize);
|
2013-06-05 01:56:07 +00:00
|
|
|
|
|
|
|
if (inputsize > DATA_SIZE) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: File must be a maximum of 65,536 bytes!\n");
|
2013-06-05 01:56:07 +00:00
|
|
|
exit(-1);
|
2013-11-14 03:31:06 +00:00
|
|
|
} else if (!inputsize) {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: Input file is empty!\n");
|
2013-11-14 03:31:06 +00:00
|
|
|
exit(-1);
|
2013-06-05 01:56:07 +00:00
|
|
|
}
|
2013-11-14 03:31:06 +00:00
|
|
|
|
2013-06-05 01:56:07 +00:00
|
|
|
// read the file
|
|
|
|
fseek(infile, 0, SEEK_SET);
|
|
|
|
fread(unpacked, sizeof(uint8_t), inputsize, infile);
|
2013-11-14 03:56:13 +00:00
|
|
|
if (ferror(infile)) {
|
|
|
|
perror("Error reading input file");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2013-06-05 01:56:07 +00:00
|
|
|
|
|
|
|
// compress the file
|
2013-06-05 23:14:09 +00:00
|
|
|
clock_t time = clock();
|
2013-06-05 23:50:07 +00:00
|
|
|
outputsize = pack(unpacked, inputsize, packed, fast);
|
2013-06-05 23:14:09 +00:00
|
|
|
time = clock() - time;
|
2013-11-14 03:31:06 +00:00
|
|
|
|
|
|
|
if (outputsize) {
|
|
|
|
// write the compressed data to the file
|
|
|
|
fseek(outfile, fileoffset, SEEK_SET);
|
|
|
|
fwrite((const void*)packed, 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("Compressed size: % zu bytes\n", outputsize);
|
|
|
|
printf("Compression ratio: %4.2f%%\n", 100 * (double)outputsize / inputsize);
|
|
|
|
printf("Compression time: %4.3f seconds\n\n", (double)time / CLOCKS_PER_SEC);
|
|
|
|
|
|
|
|
printf("Inserted at 0x%06X - 0x%06lX\n", fileoffset, ftell(outfile) - 1);
|
|
|
|
} else {
|
2013-11-14 04:32:05 +00:00
|
|
|
fprintf(stderr, "Error: File could not be compressed because the resulting compressed data would\n"
|
|
|
|
" have been larger than 64 kb.\n");
|
2013-11-14 03:31:06 +00:00
|
|
|
}
|
2013-06-05 01:56:07 +00:00
|
|
|
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
}
|