From 1e62870c37e888a9a18d605cdf8c522633bb57ff Mon Sep 17 00:00:00 2001 From: Gudf <28691694+Gudf@users.noreply.github.com> Date: Sun, 4 May 2025 21:23:29 +0200 Subject: [PATCH] Add flag to disable padding lz-encoded files --- lz.c | 14 ++++++++------ lz.h | 2 +- main.c | 7 ++++++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lz.c b/lz.c index de55317..e9bea86 100644 --- a/lz.c +++ b/lz.c @@ -122,7 +122,7 @@ static void FindBestBlockBackwards(unsigned char *src, int srcPos, int srcSize, typedef void (*FindBestBlockFunc)(unsigned char *src, int srcPos, int srcSize, const int minDistance, int *outBestBlockDistance, int *outBestBlockSize); -unsigned char *LZCompress(unsigned char *src, int srcSize, int *compressedSize, const int minDistance, bool forwardIteration) +unsigned char *LZCompress(unsigned char *src, int srcSize, int *compressedSize, const int minDistance, bool forwardIteration, bool pad) { if (srcSize <= 0) goto fail; @@ -169,12 +169,14 @@ unsigned char *LZCompress(unsigned char *src, int srcSize, int *compressedSize, } if (srcPos == srcSize) { - // Pad to multiple of 4 bytes. - int remainder = destPos % 4; + if (pad) { + // Pad to multiple of 4 bytes. + int remainder = destPos % 4; - if (remainder != 0) { - for (int i = 0; i < 4 - remainder; i++) - dest[destPos++] = 0; + if (remainder != 0) { + for (int i = 0; i < 4 - remainder; i++) + dest[destPos++] = 0; + } } *compressedSize = destPos; diff --git a/lz.h b/lz.h index fdc1370..21f18a8 100644 --- a/lz.h +++ b/lz.h @@ -6,6 +6,6 @@ #include "stdbool.h" unsigned char *LZDecompress(unsigned char *src, int srcSize, int *uncompressedSize); -unsigned char *LZCompress(unsigned char *src, int srcSize, int *compressedSize, const int minDistance, bool forwardIteration); +unsigned char *LZCompress(unsigned char *src, int srcSize, int *compressedSize, const int minDistance, bool forwardIteration, bool pad); #endif // LZ_H diff --git a/main.c b/main.c index 88fc487..cf50267 100644 --- a/main.c +++ b/main.c @@ -973,6 +973,7 @@ void HandleLZCompressCommand(char *inputPath, char *outputPath, int argc, char * int overflowSize = 0; int minDistance = 2; // default, for compatibility with LZ77UnCompVram() bool forwardIteration = true; + bool nopad = false; for (int i = 3; i < argc; i++) { @@ -1008,6 +1009,10 @@ void HandleLZCompressCommand(char *inputPath, char *outputPath, int argc, char * { forwardIteration = false; } + else if (strcmp(option, "-nopad") == 0) + { + nopad = true; + } else { FATAL_ERROR("Unrecognized option \"%s\".\n", option); @@ -1024,7 +1029,7 @@ void HandleLZCompressCommand(char *inputPath, char *outputPath, int argc, char * unsigned char *buffer = ReadWholeFileZeroPadded(inputPath, &fileSize, overflowSize); int compressedSize; - unsigned char *compressedData = LZCompress(buffer, fileSize + overflowSize, &compressedSize, minDistance, forwardIteration); + unsigned char *compressedData = LZCompress(buffer, fileSize + overflowSize, &compressedSize, minDistance, forwardIteration, !nopad); compressedData[1] = (unsigned char)fileSize; compressedData[2] = (unsigned char)(fileSize >> 8);