Search code examples
headerzlibinflatebitstream

ZLIB inflate stream header format


after downloading ZLIB ver. 1.2.11 and looking through RFC1951 I'm trying to use ZLIB.inflate function like this:

#include "zlib.h"

int iRslt;
unsigned char buffIn[4]={0x4b,4,0,0};
unsigned char buffOut[16];
z_stream zStrm;

  memset(&zStrm, 0, sizeof(zStrm));
  zStrm.zalloc = Z_NULL;
  zStrm.zfree = Z_NULL;
  zStrm.opaque = Z_NULL;
  zStrm.avail_in = 0;
  zStrm.next_in = Z_NULL;
  iRslt = inflateInit(&zStrm);
  if(iRslt != Z_OK) return;

  zStrm.avail_in = sizeof(buffIn);
  zStrm.next_in = buffIn;
  zStrm.avail_out = sizeof(buffOut);
  zStrm.next_out = buffOut;
  iRslt = inflate(&zStrm, Z_NO_FLUSH);
  if(iRslt == Z_DATA_ERROR){//-3
    //why do I end up here with zStrm.msg -> "incorrect header check"?
  }

My buffIn contains bitstream header 011b: BFINAL=1; BTYPE=01b; and fixed Huffman code for character 'a' (0x61) followed by at least 7 zero bits to end the block. Evidently that's not enough; please help. Many thanks in advance; Boba.


Solution

  • Your code is looking for a zlib stream header, as defined in RFC 1950. It's not finding it. That RFC defines the zlib header and trailer that is wrapped around a raw deflate stream.

    You have a raw deflate stream in your question. To decode that instead of a zlib stream, you would need to use inflateInit2() with a windowBits value of -15.