Skip to content

Commit 8ebdb33

Browse files
committed
Reduce error checking clutter by implementing D() to sudden death any strange behavior.
1 parent d1c6ba6 commit 8ebdb33

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

util/UNZIP.LUA

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env lua
22

3+
---Halt and catch fire if E is set
4+
local function D() if E then error(E, 0) end end
5+
36
---New bit stream
47
local function nbs(r)
58

@@ -48,15 +51,16 @@ local function nbs(r)
4851

4952
-- o = out
5053
-- g = got
51-
local o, g = {}, 0
54+
-- t = take
55+
local o, g, t = {}, 0
5256
while g < n do
5357
if p > #b then
5458
if not F() then error("Unexpected EOF") end
5559
end
56-
local take = math.min(n - g, #b - p + 1)
57-
o[#o +1] = b:sub(p, p + take - 1)
58-
p = p + take
59-
g = g + take
60+
t = math.min(n - g, #b - p + 1)
61+
o[#o +1] = b:sub(p, p + t - 1)
62+
p = p + t
63+
g = g + t
6064
end
6165
return table.concat(o)
6266
end
@@ -316,13 +320,11 @@ local function unzip(p)
316320
-- f = file
317321
local C, f = 4096
318322

319-
f, E = io.open(p, "rb")
320-
if E then print(E) os.exit(1) end
323+
f, E = io.open(p, "rb") D()
321324

322325
-- R = read bytes
323326
local function R(n)
324-
local s, E = f:read(n)
325-
if E then print(E) os.exit(1) end
327+
local s, E = f:read(n) D()
326328
return s
327329
end
328330

@@ -354,17 +356,17 @@ local function unzip(p)
354356
if not s or #s < 4 then break end
355357
if s ~= "PK\3\4" then break end
356358

357-
v , E = R16() if not v then break end -- version
358-
fl, E = R16() if not fl then break end -- flags
359-
cm, E = R16() if not cm then break end -- method
359+
v , E = R16() D() -- version
360+
fl, E = R16() D() -- flags
361+
cm, E = R16() D() -- method
360362
S(4) -- skip modification time and date
361-
cr, E = R32() if not cr then break end -- crc32
362-
cs, E = R32() if not cs then break end -- compressed size
363-
us, E = R32() if not us then break end -- uncompressed size
364-
nl, E = R16() if not nl then break end -- name length
365-
el, E = R16() if not el then break end -- extra name length
363+
cr, E = R32() D() -- crc32
364+
cs, E = R32() D() -- compressed size
365+
us, E = R32() D() -- uncompressed size
366+
nl, E = R16() D() -- name length
367+
el, E = R16() D() -- extra name length
366368

367-
fn, E = R(nl) if not fn then break end -- filename
369+
fn, E = R(nl) D() -- filename
368370
if el > 0 then S(el) end
369371

370372
print("Extracting: " .. fn)
@@ -387,10 +389,9 @@ local function unzip(p)
387389
os.execute('mkdir -p "'.. di ..'"')
388390
end
389391

390-
of, E = io.open(fn, "wb")
391-
if not of then
392-
print(E)
393-
else
392+
of, E = io.open(fn, "wb") D()
393+
394+
if of then
394395
---Write decompressed data and update CRC
395396
---@param c string The chunk of decompressed data to write to the file
396397
local function W(c)
@@ -401,8 +402,7 @@ local function unzip(p)
401402
if cm == 0 then -- uncompressed file
402403
while re > 0 do
403404
local c = RC()
404-
if not c then break end
405-
W(c)
405+
if c then W(c) else break end
406406
end
407407
elseif cm == 8 then -- deflated file
408408
inf(RC, W)

0 commit comments

Comments
 (0)