Merge pull request #21 from Gudf/lz-pad-option

Add a flag to disable padding lz-encoded files
This commit is contained in:
red031000 2025-05-11 17:51:36 +01:00 committed by GitHub
commit 14aaef3caf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 8 deletions

14
lz.c
View File

@ -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); 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) if (srcSize <= 0)
goto fail; goto fail;
@ -169,12 +169,14 @@ unsigned char *LZCompress(unsigned char *src, int srcSize, int *compressedSize,
} }
if (srcPos == srcSize) { if (srcPos == srcSize) {
// Pad to multiple of 4 bytes. if (pad) {
int remainder = destPos % 4; // Pad to multiple of 4 bytes.
int remainder = destPos % 4;
if (remainder != 0) { if (remainder != 0) {
for (int i = 0; i < 4 - remainder; i++) for (int i = 0; i < 4 - remainder; i++)
dest[destPos++] = 0; dest[destPos++] = 0;
}
} }
*compressedSize = destPos; *compressedSize = destPos;

2
lz.h
View File

@ -6,6 +6,6 @@
#include "stdbool.h" #include "stdbool.h"
unsigned char *LZDecompress(unsigned char *src, int srcSize, int *uncompressedSize); 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 #endif // LZ_H

7
main.c
View File

@ -973,6 +973,7 @@ void HandleLZCompressCommand(char *inputPath, char *outputPath, int argc, char *
int overflowSize = 0; int overflowSize = 0;
int minDistance = 2; // default, for compatibility with LZ77UnCompVram() int minDistance = 2; // default, for compatibility with LZ77UnCompVram()
bool forwardIteration = true; bool forwardIteration = true;
bool nopad = false;
for (int i = 3; i < argc; i++) for (int i = 3; i < argc; i++)
{ {
@ -1008,6 +1009,10 @@ void HandleLZCompressCommand(char *inputPath, char *outputPath, int argc, char *
{ {
forwardIteration = false; forwardIteration = false;
} }
else if (strcmp(option, "-nopad") == 0)
{
nopad = true;
}
else else
{ {
FATAL_ERROR("Unrecognized option \"%s\".\n", option); 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); unsigned char *buffer = ReadWholeFileZeroPadded(inputPath, &fileSize, overflowSize);
int compressedSize; 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[1] = (unsigned char)fileSize;
compressedData[2] = (unsigned char)(fileSize >> 8); compressedData[2] = (unsigned char)(fileSize >> 8);