Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length values from deflate algorithm

I compressed the text "TestingTesting" and the hex result was: 0B 49 2D 2E C9 CC 4B 0F 81 50 00. I can't figure out the length and distance codes. The binary below is reversed because the RFC says to read the bits from right to left (thanks Matthew Slattery for the help). Here is what was parsed so far:

1                       BFINAL (last block)
01                      BTYPE (static)
1000 0100   132-48=  84 T
1001 0101   149-48= 101 e
1010 0011   163-48= 115 s
1010 0100   164-48= 116 t
1001 1001   153-48= 105 i
1001 1110   158-48= 110 n
1001 0111   151-48= 103 g

These are the remaining bits that I don't know how to parse:

1000 0100 0000 1000 0101 0000 0000 0

The final 10 bits (end of block value is x100) is the only part I can parse. I think the length and distance values should be 7 (binary 0111) since the length of "Testing" is 7 letters, and it gets copied 7 characters after the current position, but I can't figure out how its representing this in the remaining bits. What am I doing wrong?

like image 655
ItsJustMe Avatar asked Sep 16 '11 16:09

ItsJustMe


1 Answers

The distance code is 5, but a distance code of 5 is followed by one "extra bit" to indicate an actual distance of either 7 or 8. (See the second table in paragraph 3.2.5 of the RFC.)

The complete decoding of the data is:

1          BFINAL
01         BTYPE=static
10000100  'T'
10010101  'e'
10100011  's'
10100100  't'
10011001  'i'
10011110  'n'
10010111  'g'
10000100  another 'T'
0000100   literal/length code 260 = length 6
00101     distance code 5
0         extra bit  => the distance is 7
0000000   literal/length code 256 = end of block
like image 198
Matthew Slattery Avatar answered Sep 22 '22 21:09

Matthew Slattery