richgel999 / miniz

miniz: Single C source file zlib-replacement library, originally from code.google.com/p/miniz
MIT License
2.2k stars 322 forks source link

Compressing with small window size #300

Open nlimper opened 9 months ago

nlimper commented 9 months ago

For a zlib compressed file that needs to be decompressed by an MCU with very limited memory, I want to use a window size of only 4096. I started by defining TDEFL_LESS_MEMORY, and changing TDEFL_LZ_DICT_SIZE to 4096, which leaded to corrupted zlib files. After some more investigation, it appeared I could lower TDEFL_LZ_DICT_SIZE to 16384 and 8192, but anything below would corrupt the output file.

Eventually I managed to fix it, because I found some hardcoded values in the source of tdefl_compress_normal: 8U * 1024U in https://github.com/richgel999/miniz/blob/master/miniz_tdef.c#L1185 and 31 * 1024 in https://github.com/richgel999/miniz/blob/master/miniz_tdef.c#L1234 Changing both of the hardcoded values to 4096 solved the problem, and, in combination with the right TDEFL_LZ_DICT_SIZE, leaded to valid zlib files with window size 4096.

As I'm not exactly know what I'm messing with ;-) , I didn't want to make this in to a PR. Ideally, it probably should use the window_bits parameter in int mz_inflateInit2(mz_streamp pStream, int window_bits) (which is restricted to 15 and -15 at the moment, and only used to check if the value is negative or positive).