@@ -152,39 +152,49 @@ local function inflate(data)
152152 dist = make_huff (dl )
153153 else
154154 -- dynamic Huffman
155- local hlit = bs .r (5 ) + 257
156- local hdist = bs .r (5 ) + 1
157- local hclen = bs .r (4 ) + 4
158- local order = {16 ,17 ,18 ,0 ,8 ,7 ,9 ,6 ,10 ,5 ,11 ,4 ,12 ,3 ,13 ,2 ,14 ,1 ,15 }
159- local clen = {}
160- for i = 1 , 19 do clen [i ] = 0 end
161- for i = 1 , hclen do clen [order [i ] + 1 ] = bs .r (3 ) end
162- local chuff = make_huff (clen )
163- local lens = {}
164- while # lens < hlit + hdist do
165- local sym = read_huff (bs , chuff )
166- if sym <= 15 then
167- lens [# lens + 1 ] = sym
168- elseif sym == 16 then
169- local r = bs .r (2 ) + 3
170- local last = lens [# lens ]
171- for _ = 1 , r do lens [# lens + 1 ] = last end
172- elseif sym == 17 then
155+
156+ -- hl = hlit
157+ -- hd = hdist
158+ -- hc = hclen
159+ -- od = order
160+ -- cl = clen
161+ -- le = lens
162+ -- ch = chuff
163+ local hl , hd , hc , od , cl , le , ch = bs .r (5 ) + 257 , bs .r (5 ) + 1 , bs .r (4 ) + 4 , {16 ,17 ,18 ,0 ,8 ,7 ,9 ,6 ,10 ,5 ,11 ,4 ,12 ,3 ,13 ,2 ,14 ,1 ,15 }, {}, {}
164+
165+ for i = 1 , 19 do cl [i ] = 0 end
166+ for i = 1 , hc do cl [od [i ] + 1 ] = bs .r (3 ) end
167+ ch = make_huff (cl )
168+
169+ while # le < hl + hd do
170+
171+ -- s = sym
172+ local s = read_huff (bs , ch )
173+
174+ if s <= 15 then
175+ le [# le + 1 ] = s
176+ elseif s == 16 then
177+ local r , l = bs .r (2 ) + 3 , le [# le ]
178+ for _ = 1 , r do le [# le + 1 ] = l
179+ end
180+ elseif s == 17 then
173181 local r = bs .r (3 ) + 3
174- for _ = 1 , r do lens [ # lens + 1 ] = 0 end
175- elseif sym == 18 then
182+ for _ = 1 , r do le [ # le + 1 ] = 0 end
183+ elseif s == 18 then
176184 local r = bs .r (7 ) + 11
177- for _ = 1 , r do lens [ # lens + 1 ] = 0 end
185+ for _ = 1 , r do le [ # le + 1 ] = 0 end
178186 end
179187 end
180188 local ll , dl = {}, {}
181- for i = 1 , hlit do ll [i ] = lens [i ] end
182- for i = hlit + 1 , hlit + hdist do dl [i - hlit ] = lens [i ] end
189+ for i = 1 , hl do ll [i ] = le [i ] end
190+ for i = hl + 1 , hl + hd do dl [i - hl ] = le [i ] end
183191 litlen = make_huff (ll )
184192 dist = make_huff (dl )
185193 end
186194
187- local len_extra , dist_extra = {
195+ -- lex = length extra
196+ -- dix = dist extra
197+ local lex , dix = {
188198 [257 ]= {3 ,0 },[258 ]= {4 ,0 },[259 ]= {5 ,0 },[260 ]= {6 ,0 },[261 ]= {7 ,0 },
189199 [262 ]= {8 ,0 },[263 ]= {9 ,0 },[264 ]= {10 ,0 },[265 ]= {11 ,1 },[266 ]= {13 ,1 },
190200 [267 ]= {15 ,1 },[268 ]= {17 ,1 },[269 ]= {19 ,2 },[270 ]= {23 ,2 },[271 ]= {27 ,2 },
@@ -217,11 +227,12 @@ local function inflate(data)
217227 -- db = dbase
218228 -- dx = dextra
219229 -- da = dadd
220- local e , b , x , a , l , ds , de , db , dx , da , dv = len_extra [s ]
230+ local e , b , x , a , l , ds , de , db , dx , da , dv = lex [s ]
231+
221232 b , x = e [1 ], e [2 ]
222233 a = x > 0 and bs .r (x ) or 0
223234 l , ds = b + a , read_huff (bs , dist )
224- de = dist_extra [ds + 1 ]
235+ de = dix [ds + 1 ]
225236 db , dx = de [1 ], de [2 ]
226237 da = dx > 0 and bs .r (dx ) or 0
227238 dv = db + da
@@ -263,10 +274,16 @@ local function le16(s, i) local a,b = s:byte(i,i+1) return a + b*256 end
263274local function le32 (s , i ) local a ,b ,c ,d = s :byte (i , i + 3 ) return a + b * 256 + c * 65536 + d * 16777216 end
264275
265276local function unzip (path )
266- local f = assert (io.open (path , " rb" ))
267- local data , pos = f :read (" *a" ), 1 f :close ()
277+
278+ -- f = file
279+ -- d = data
280+ -- p = position
281+ local f , d , p = assert (io.open (path , " rb" ))
282+ d , p = f :read (" *a" ), 1 f :close ()
283+
268284 while true do
269- if data :sub (pos , pos + 3 ) ~= " PK\3\4 " then break end
285+ if d :sub (p , p + 3 ) ~= " PK\3\4 " then break end
286+
270287 -- _ = version needed
271288 -- _ = flags
272289 -- cm = cm
@@ -275,16 +292,15 @@ local function unzip(path)
275292 -- us = Uncompressed size
276293 -- nl = Length of file name
277294 -- el = Extra length in the file name
278-
279- local _ , _ , cm , cr , cs , us , nl , el = le16 (data , pos + 4 ), le16 (data , pos + 6 ), le16 (data , pos + 8 ), le32 (data , pos + 14 ), le32 (data , pos + 18 ), le32 (data , pos + 22 ), le16 (data , pos + 26 ), le16 (data , pos + 28 )
295+ local _ , _ , cm , cr , cs , us , nl , el = le16 (d , p + 4 ), le16 (d , p + 6 ), le16 (d , p + 8 ), le32 (d , p + 14 ), le32 (d , p + 18 ), le32 (d , p + 22 ), le16 (d , p + 26 ), le16 (d , p + 28 )
280296
281297 -- fn = The filename
282298 -- st = The file Start offset
283- local fn , st = data :sub (pos + 30 , pos + 29 + nl ), pos + 30 + nl + el
299+ local fn , st = d :sub (p + 30 , p + 29 + nl ), p + 30 + nl + el
284300
285301 -- cd = A buffer of the files data
286302 -- os = The output stream
287- local cd , os = data :sub (st , st + cs - 1 )
303+ local cd , os = d :sub (st , st + cs - 1 )
288304
289305 print (" Extracting: " .. fn )
290306
@@ -308,20 +324,21 @@ local function unzip(path)
308324 io.stderr :write (string.format (" CRC mismatch for %s: expected %08x, got %08x\n " , fn , cr , c ))
309325 end
310326
311- -- d = directory
312- local d = fn :match (" (.+)/" )
313- if d then
327+ -- di = directory
328+ local di = fn :match (" (.+)/" )
329+
330+ if di then
314331 if os.execute then
315332 -- portable make dir (POSIX/Windows-friendly-ish)
316- os.execute (' mkdir -p "' .. d .. ' "' )
333+ os.execute (' mkdir -p "' .. di .. ' "' )
317334 end
318335 end
319336 local of = assert (io.open (fn , " wb" ))
320337 of :write (os )
321338 of :close ()
322339 end
323340
324- pos = st + cs
341+ p = st + cs
325342 end
326343end
327344
0 commit comments