如何从放气压缩输出结果中解码放气块header

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

我正在尝试根据 deflate 压缩输出的输出字节解码 header 位。

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]);
}

结果:

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

等等。

我只想了解 deflate 规范中描述的 3 位块 header 中的哪些位。第一位指定块 final/BFINAL。接下来的两位指定 BTYPE。

基于此结果,0x78 - 前 3 位为 000,表示 BFINAL=0,BTYPE=00/无压缩。但这对我来说似乎不对。 BTYPE 应指定 01 或 10。

我是不是漏掉了什么?有人可以帮忙吗?

参考: deflate specification

您正在制作 zlib 流,而不是原始压缩流。所以 78 da 是 zlib header,而不是 deflate 压缩数据。 deflate 数据以 f3 开头。它的低三位是011。低1是BFINAL(这是最后一个块),01是BTYPE(固定霍夫曼码)。