Good day! I try to understand some principles of Compression with dynamic Huffman Codes (Deflate). I made a ZIP with txt file inside and started to analyse compressed data. Here it is.
ed c2 01 0d 00 00 0c 02 a0 ac bf fd 3b 98 c3 0d c6 7d 54 55 55 55 55 55 55 75 6e 01 (in hex format)
First 3 bits - 101: 1 - last block; 10 - Compression with dynamic Huffman codes.
So 11101 = HLIT - 257; HLIT = 286; 00010 = HDIST - 1; HDIST = 3; 1110 = HCLEN - 4; HCLEN = 18.
After that i need to read HCLEN*3 bits. In my example i need to read 18*3 bits and build Huffman tree. So lets go.
> I try to understand some principles of Compression with dynamic Huffman Codes (Deflate).
Note that deflate's huffman is actually static, ie not adaptive.
Deflate's "dynamic huffman" just means that length table is stored in the block's header (RLE + huffman-coded itself).
Thanks for your answer!
Well i just need to decompress not all compressed data, but only a few first bytes of it and so i need to write my own function, which will be able to do it. I cant find in the net suitable code, unfortunately.
After wasting some time on the reading raw2dec.cpp i still don`t understand how to decode just a few bytes from compressed data. Please help me to find out how to do it.
// read secondary huffman code-length table, which is used to decompress primary huffman table
const word order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
for( index=0; index<ncode; index++ ) lengths[order[index]] = bits(3);
// construct huffman code from length table
err = lencode.construct( lengths, 19 );
5. This is followed by decoding of primary length table (which is compressed by RLE+huffman).
Really, there's too much to describe in english, so you have to learn to read C somehow.
In type=2 mode you can't "just decode a few bytes" - you'd have to include half of raw2dec source,
because to decode these bytes, you need to determine which huffman codes are assigned to them.
And for that, you need to decode the primary length table and build the huffman code from it.