问题描述
下载 ZLIB 版本后。 1.2.11 并查看 RFC1951 我正在尝试像这样使用 ZLIB.inflate 函数:
#include "zlib.h"
int irslt;
unsigned char buffIn[4]={0x4b,4,0};
unsigned char buffOut[16];
z_stream zStrm;
memset(&zStrm,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"?
}
我的 buffIn 包含比特流头 011b:BFINAL=1; BTYPE=01b;并修复了字符“a”(0x61)的霍夫曼代码,后跟至少 7 个零位以结束块。显然这还不够。请帮忙。 提前谢谢了; 波巴。
解决方法
您的代码正在寻找 RFC 1950 中定义的 zlib 流标头。它没有找到。该 RFC 定义了包裹在原始 deflate 流中的 zlib 头和尾。
您的问题中有一个原始的 deflate 流。要解码它而不是 zlib 流,您需要使用 inflateInit2()
和 windowBits
的 -15
值。