@@ -12,10 +12,11 @@ function inflate(r, w, f)
1212 -- bc = Bit count
1313 -- op = Output position
1414 -- kg = Keep going (in a loop)
15+ -- le = Decoded bit-lengths for literal/distance tables
1516 -- lex = Length base & extra bits (pairs of bytes)
1617 -- dix = Distance extra bits (1 byte per entry)
1718 -- dib = Distance base (packed 32-bit LE)
18- local b , p , bb , bc , op , kg , le , ll , dl , lex , dix , dib = " " , 1 , 0 , 0 , 0 , 1 , {}, {} , {},
19+ local b , p , bb , bc , op , kg , le , lex , dix , dib = " " , 1 , 0 , 0 , 0 , 1 , {},
1920
2021 -- lex, dix & dib are packed string constants
2122 " \3\0\4\0\5\0\6\0\7\0\8\0\9\0\10\0\11\1\13\1\15\1\17\1\19\2\23\2\27\2\31\2\35\3\43\3\51\3\59\3\67\4\83\4\99\4\115\4\131\5\163\5\195\5\227\5\2\0 " ,
@@ -64,6 +65,8 @@ function inflate(r, w, f)
6465 --- @param bi number The number of bits to process.
6566 --- @return number The bit-reversed value.
6667 function rv (x , bi )
68+
69+ -- y = The reverse of x
6770 local y = 0
6871
6972 for _ = 1 , bi do
@@ -77,14 +80,23 @@ function inflate(r, w, f)
7780 --- Constructs a canonical Huffman decoding table from a list of code lengths.
7881 --- @param ls table An array where the index is the symbol and the value is its bit length.
7982 --- @param nc number The number of codes to process from the table.
80- --- @param off number The offset in the table to start from.
83+ --- @param of number The offset in the table to start from.
8184 --- @return table A Huffman object containing the lookup table (` tab` ) and the maximum code length (` max` ).
82- function mh (ls , nc , off )
83- off = off or 0
85+ function mh (ls , nc , of )
86+
87+ -- m = Maximum length
88+ -- c = Counts
89+ -- d = Code
90+ -- n = Next code
91+ -- t = Tab
8492 local m , c , d , n , t = 0 , {}, 0 , {}, {}
8593
94+ of = of or 0
8695 for i = 1 , nc do
87- local l = ls [i + off ]
96+
97+ -- l = Bit Length
98+ local l = ls [i + of ]
99+
88100 if l and l > 0 then
89101 c [l ] = (c [l ] or 0 ) + 1
90102 if l > m then m = l end
@@ -94,7 +106,10 @@ function inflate(r, w, f)
94106 for i = 1 , m do d = (d + (c [i - 1 ] or 0 )) << 1 n [i ] = d end
95107
96108 for s = 1 , nc do
97- local l = ls [s + off ]
109+
110+ -- l = Length
111+ local l = ls [s + of ]
112+
98113 if l and l > 0 then
99114 t [rv (n [l ], l )] = { sym = s - 1 , len = l }
100115 n [l ] = n [l ] + 1
@@ -178,26 +193,37 @@ function inflate(r, w, f)
178193 d = mh (le , 32 , 288 )
179194 else
180195 -- Dynamic Huffman
181- local hl , hd , hc , od , cl = rb (5 ) + 257 , rb (5 ) + 1 , rb (4 ) + 4 , " \16\17\18\0\8\7\9\6\10\5\11\4\12\3\13\2\14\1\15 " , {}
196+
197+ -- hl = Literal/length code count (HLIT)
198+ -- hd = Distance code count (HDIST)
199+ -- hc = Meta-code length count (HCLEN)
200+ -- od = Permutation order for code lengths
201+ -- cl = Code lengths for the meta-table
202+ -- ch = Huffman table for decoding bit-lengths
203+ local hl , hd , hc , od , cl , li , ch = rb (5 ) + 257 , rb (5 ) + 1 , rb (4 ) + 4 , " \16\17\18\0\8\7\9\6\10\5\11\4\12\3\13\2\14\1\15 " , {}, 1
182204
183205 for i = 1 , 19 do cl [i ] = 0 end
184206 for i = 1 , hc do cl [od :byte (i ) + 1 ] = rb (3 ) end
185- local ch = mh (cl , 19 )
207+ ch = mh (cl , 19 )
186208
187- local lei = 1
188- while lei <= hl + hd do
209+ while li <= hl + hd do
210+
211+ -- s = Symbol
212+ -- x = Repeat count
213+ -- l = Code length
189214 local s , x , l = rh (ch )
215+
190216 if s <= 15 then
191- le [lei ] = s ; lei = lei + 1
217+ le [li ] = s ; li = li + 1
192218 elseif s == 16 then
193- x , l = rb (2 ) + 3 , le [lei - 1 ]
194- for _ = 1 , x do le [lei ] = l ; lei = lei + 1 end
219+ x , l = rb (2 ) + 3 , le [li - 1 ]
220+ for _ = 1 , x do le [li ] = l ; li = li + 1 end
195221 elseif s == 17 then
196222 x = rb (3 ) + 3
197- for _ = 1 , x do le [lei ] = 0 ; lei = lei + 1 end
223+ for _ = 1 , x do le [li ] = 0 ; li = li + 1 end
198224 elseif s == 18 then
199225 x = rb (7 ) + 11
200- for _ = 1 , x do le [lei ] = 0 ; lei = lei + 1 end
226+ for _ = 1 , x do le [li ] = 0 ; li = li + 1 end
201227 end
202228 end
203229 h = mh (le , hl ) d = mh (le , hd , hl )
0 commit comments