Search code examples
zlibdeflate

How to decode deflate block header from the deflate compression output result


I am trying to decode the header bits based on the output byte of deflate compression output.

char a[50] = "Hello";
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = ZNULL;

defstream.avail_in = (uInt)strlen(a)+1;
defstream.next_in = (Bytef *)a;
defstream.avail_out = (uINt)sizeof(b);
defstream.next_out = (Bytef *)b;

deflateInit(&defstream, Z_BEST_COMPRESSION);
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);

for (int i=0; i<strlen(b); i++) {
  printf("--- byte[%d]=%hhx\n", i, b[i]);
}

The result:

--- byte[0]=78
--- byte[1]=da
--- byte[2]=f3

and so on.

I just want to understand which bits are the 3-bit block header as described in deflate specification. First bit specifies the block final/BFINAL. Next two bits specify the BTYPE.

Based on this result, 0x78 - the first 3 bits are 000 which means BFINAL=0, BTYPE=00/no compression. But this seems not right to me. The BTYPE should specify either 01 or 10.

Am I missing out something here? Can someone please help?

Reference: deflate specification


Solution

  • You are making a zlib stream, not a raw deflate stream. So the 78 da is the zlib header, not deflate compressed data. The deflate data starts with f3. The low three bits of that are 011. The low 1 is BFINAL (this is the last block), and the 01 is BTYPE (fixed Huffman codes).