@@ -34,7 +34,8 @@ local function nbs(data)
3434 return { r = rb , a = ab , p = p , s = s }
3535end
3636
37- local function revbits (x , bits )
37+ -- rv = revbits
38+ local function rv (x , bits )
3839 local y = 0
3940
4041 for _ = 1 , bits do
@@ -45,7 +46,8 @@ local function revbits(x, bits)
4546 return y
4647end
4748
48- local function make_huff (lengths )
49+ -- mh = make huff
50+ local function mh (lengths )
4951
5052 -- m = maximum length
5153 -- c = counts
@@ -71,7 +73,7 @@ local function make_huff(lengths)
7173 -- l = len
7274 for s , l in ipairs (lengths ) do
7375 if l > 0 then
74- local r = revbits (n [l ], l )
76+ local r = rv (n [l ], l )
7577 t [r ] = { sym = s - 1 , len = l }
7678 n [l ] = n [l ] + 1
7779 end
@@ -80,7 +82,8 @@ local function make_huff(lengths)
8082 return { tab = t , max = m }
8183end
8284
83- local function read_huff (bs , h )
85+ -- rh = read huff
86+ local function rh (bs , h )
8487
8588 -- c = code
8689 local c = 0
98101local function inflate (data )
99102
100103 -- bs = bit stream
101- -- o = output byte stream
104+ -- ob = output byte stream
102105 -- op = output position
103- -- d = done
104- local bs , o , op , d = nbs (data ), {}, 0 , false
106+ -- kg = keep going (in a loop)
107+ local bs , ob , op , kg = nbs (data ), {}, 0 , true
105108
106109 -- ab = append byte
107- local function ab (byte )
108- op = op + 1
109- o [op ] = string.char (byte )
110- end
110+ local function ab (byte ) op = op + 1 ob [op ] = string.char (byte ) end
111111
112112 -- as = append string
113113 local function as (s )
114114 -- push each byte as single-character entry
115- for i = 1 , # s do
116- op = op + 1
117- o [op ] = s :sub (i ,i )
118- end
115+ for i = 1 , # s do op = op + 1 ob [op ] = s :sub (i ,i ) end
119116 end
120117
121- while not d do
118+ while kg do
122119
123120 -- f = final
124121 -- t = block type
@@ -128,28 +125,24 @@ local function inflate(data)
128125 -- uncompressed block: align, then read LEN, NLEN from byte stream
129126 bs .a ()
130127 local p = bs .p ()
131- local function le16 (s , i )
132- local a ,b = s :byte (i ,i + 1 ) return a + b * 256
133- end
128+ local function le16 (s , i ) local a ,b = s :byte (i ,i + 1 ) return a + b * 256 end
134129 local len , _ = le16 (data , p ), le16 (data , p + 2 )
135130 bs .s (4 ) -- consume LEN and NLEN
136131 local s = data :sub (bs .p (), bs .p () + len - 1 )
137132 as (s )
138133 bs .s (len )
139134
140135 elseif t == 1 or t == 2 then
141- local litlen , dist
136+ local ll , dl , h , d = {}, {}
142137 if t == 1 then
143138 -- fixed Huffman
144- local ll = {}
145139 for _ = 0 , 143 do ll [# ll + 1 ] = 8 end
146140 for _ = 144 , 255 do ll [# ll + 1 ] = 9 end
147141 for _ = 256 , 279 do ll [# ll + 1 ] = 7 end
148142 for _ = 280 , 287 do ll [# ll + 1 ] = 8 end
149- litlen = make_huff (ll )
150- local dl = {}
143+ h = mh (ll )
151144 for _ = 0 , 31 do dl [# dl + 1 ] = 5 end
152- dist = make_huff (dl )
145+ d = mh (dl )
153146 else
154147 -- dynamic Huffman
155148
@@ -164,12 +157,12 @@ local function inflate(data)
164157
165158 for i = 1 , 19 do cl [i ] = 0 end
166159 for i = 1 , hc do cl [od [i ] + 1 ] = bs .r (3 ) end
167- ch = make_huff (cl )
160+ ch = mh (cl )
168161
169162 while # le < hl + hd do
170163
171164 -- s = sym
172- local s = read_huff (bs , ch )
165+ local s = rh (bs , ch )
173166
174167 if s <= 15 then
175168 le [# le + 1 ] = s
@@ -185,11 +178,9 @@ local function inflate(data)
185178 for _ = 1 , r do le [# le + 1 ] = 0 end
186179 end
187180 end
188- local ll , dl = {}, {}
189181 for i = 1 , hl do ll [i ] = le [i ] end
190182 for i = hl + 1 , hl + hd do dl [i - hl ] = le [i ] end
191- litlen = make_huff (ll )
192- dist = make_huff (dl )
183+ h = mh (ll ) d = mh (dl )
193184 end
194185
195186 -- lex = length extra
@@ -210,7 +201,7 @@ local function inflate(data)
210201
211202 while true do
212203 -- s = sym
213- local s = read_huff (bs , litlen )
204+ local s = rh (bs , h )
214205 if s < 256 then
215206 ab (s )
216207 elseif s == 256 then
@@ -231,7 +222,7 @@ local function inflate(data)
231222
232223 b , x = e [1 ], e [2 ]
233224 a = x > 0 and bs .r (x ) or 0
234- l , ds = b + a , read_huff (bs , dist )
225+ l , ds = b + a , rh (bs , d )
235226 de = dix [ds + 1 ]
236227 db , dx = de [1 ], de [2 ]
237228 da = dx > 0 and bs .r (dx ) or 0
@@ -243,17 +234,17 @@ local function inflate(data)
243234
244235 -- bi = base index
245236 local bi = op - dv
246- for i = 1 , l do ab (o [bi + i ]:byte ()) end
237+ for i = 1 , l do ab (ob [bi + i ]:byte ()) end
247238 end
248239 end
249240 else
250241 error (" unsupported block type: " .. tostring (t ))
251242 end
252243
253- if f == 1 then d = true end
244+ if f == 1 then kg = false end
254245 end
255246
256- return table.concat (o )
247+ return table.concat (ob )
257248end
258249
259250local function crc32 (s )
0 commit comments