Search code examples
czlibhuffman-code

Can I force zlib to use RLE encoding AND FIXED Huffman trees?


I am trying to create a zlib compatible compressor/decompressor for an architecture without memory. I have a limited number of registers, but no memory buffers. My routine must compress files in a manner compatible with zlib, so that they can be decompressed on a computer. The reverse is not true. I only need to decompress my own files.

Since I have no memory, dynamic Huffman is out. I have to use a fixed Huffman tree as defined in RFC 1951 section 3.2.6. However, I also want to do RLE only, since I have no memory of any literal beyond distance 1. I am trying to understand if I can force the standard zlib library to output compressed files in this format, so I can run test vectors through both implementations and verify my implementation gives the exact same output as the zlib library.

However, I can not see any option to force zlib to create a file both with Z_RLE AND Z_FIXED. If I specify Z_RLE, it seems to want to build dynamic Huffman trees. And if I specify Z_FIXED, I don't see any way to limit the match distance to 1.

Have I correctly understood that this mode (Z_RLE + Z_FIXED) is not supported by zlib, and that there is no existing combination of options which would force a similar output? If this is not currently possible, and I want to implement a new mode in zlib for my testing, is it as simple as doing a global search for instances of Z_FIXED and changing every instance of ( == Z_FIXED) to ( == Z_FIXED) || ( == Z_RLE)?

Thank you for any assistance.


Solution

  • There is not currently a way to limit distances to one and use only fixed codes.

    There is one place in trees.c that looks for Z_FIXED. You could change that one occurrence to s->strategy == Z_FIXED || s->strategy == Z_RLE. Then if you request Z_RLE, it will do what you're asking for.