我試圖了解 deflate 是如何作業的。也就是說,我想我會嘗試按照RFC1951: DEFLATE Compressed Data Format Specification version 1.3所說的手動解碼 deflate 編碼的字串。我這樣生成了 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 作為第一個位元組的未壓縮塊怎么辦?那是不可能的嗎?
uj5u.com熱心網友回復:
您沒有閱讀 RFC 的這一部分:
資料元素按照位元組內的位數遞增的順序打包成位元組,即從位元組的最低有效位開始。
前三位是101,不是000。
使用infgen對該流進行解碼:
! infgen 2.6 output
!
last
dynamic
count 259 10 16
code 17 4
code 18 3
code 0 4
code 4 3
code 3 1
code 2 3
zeros 65
lens 3 3 4 3 3
zeros 25
lens 3
zeros 138
zeros 22
lens 4 3 3
zeros 3
lens 2 0 0 2 2 3 3
! litlen 65 3
! litlen 66 3
! litlen 67 4
! litlen 68 3
! litlen 69 3
! litlen 95 3
! litlen 256 4
! litlen 257 3
! litlen 258 3
! dist 3 2
! dist 6 2
! dist 7 2
! dist 8 3
! dist 9 3
literal 'A_DEAD_D
match 3 4
literal 'CEDED_A_B
match 3 12
literal 'BABE
match 4 11
match 3 28
match 4 20
literal 'BAC
match 4 13
literal 'D
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384911.html
