old-exhal/inhal.c

142 lines
4.2 KiB
C
Raw Permalink Normal View History

/*
inhal - HAL Laboratory compression tool
Usage:
inhal [-fast] infile romfile offset
inhal -n [-fast] infile 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 <time.h>
#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 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-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]);
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 23:50:07 +00:00
if (fast)
printf("Fast compression enabled.\n");
// check for -n switch
2013-06-05 23:50:07 +00:00
if (newfile) {
fileoffset = 0;
2013-06-05 23:50:07 +00:00
infile = fopen(argv[argc - 2], "rb");
outfile = fopen(argv[argc - 1], "wb");
} 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");
}
if (!infile) {
2013-11-14 04:32:05 +00:00
fprintf(stderr, "Error: unable to open input file\n");
exit(-1);
}
if (!outfile) {
2013-11-14 04:32:05 +00:00
fprintf(stderr, "Error: unable to open output file\n");
exit(-1);
}
size_t inputsize, outputsize;
uint8_t unpacked[DATA_SIZE];
uint8_t packed[DATA_SIZE] = {0};
// 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);
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");
exit(-1);
} else if (!inputsize) {
2013-11-14 04:32:05 +00:00
fprintf(stderr, "Error: Input file is empty!\n");
exit(-1);
}
// read the file
fseek(infile, 0, SEEK_SET);
fread(unpacked, sizeof(uint8_t), inputsize, infile);
if (ferror(infile)) {
perror("Error reading input file");
exit(-1);
}
// compress the file
clock_t time = clock();
2013-06-05 23:50:07 +00:00
outputsize = pack(unpacked, inputsize, packed, fast);
time = clock() - time;
if (outputsize) {
// write the compressed data to the file
fseek(outfile, fileoffset, SEEK_SET);
fwrite((const void*)packed, 1, outputsize, outfile);
if (ferror(outfile)) {
perror("Error writing output file");
exit(-1);
}
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");
}
fclose(infile);
fclose(outfile);
}