【发布时间】:2022-01-20 14:16:35
【问题描述】:
我正在尝试了解 deflate 的工作原理。也就是说,我想我会尝试手动解码一个放气编码的字符串,而不是RFC1951: DEFLATE Compressed Data Format Specification version 1.3 所说的。我因此生成了deflate编码的字符串:
$result = gzdeflate('A_DEAD_DAD_CEDED_A_BAD_BABE_A_BEADED_ABACA_BED');
echo bin2hex($result);
这给了我这个:
1589c11100000cc166a3cc61ff2dca237709880c45e52c2b08eb043dedb78db8851e
来自 RFC1951 § 3.2.3。块格式的详细信息:
Each block of compressed data begins with 3 header bits
containing the following data:
first bit BFINAL
next 2 bits BTYPE
...
BFINAL is set if and only if this is the last block of the data
set.
BTYPE specifies how the data are compressed, as follows:
00 - no compression
01 - compressed with fixed Huffman codes
10 - compressed with dynamic Huffman codes
11 - reserved (error)
0x15 是 0b00010101 位。前 3 位是 000,这将使 BFINAL 0 和 BTYPE 00(不压缩)。
从后面的 RFC1951 § 3.2.3 开始。块格式的详细信息:
if stored with no compression
skip any remaining bits in current partially
processed byte
read LEN and NLEN (see next section)
copy LEN bytes of data to output
来自 RFC1951 § 3.2.4。非压缩块(BTYPE=00):
Any bits of input up to the next byte boundary are ignored.
The rest of the block consists of the following information:
0 1 2 3 4...
+---+---+---+---+================================+
| LEN | NLEN |... LEN bytes of literal data...|
+---+---+---+---+================================+
LEN is the number of data bytes in the block. NLEN is the
one's complement of LEN.
所以我移动到下一个字节 - 0x89,即 0b10001001 位。前两位 (LEN) 是 10,接下来的两位是 NLEN (00)。但问题就在这里。 00 不是 10 的补码 - 01 是。
另外,如果你想要一个以 0xFF 作为第一个字节的未压缩块怎么办?这不可能吗?
【问题讨论】:
-
我不明白为什么你认为你不能将
0xff作为第一个未压缩字节。