Skip to content

Commit a0d7fda

Browse files
committed
Removed redundant function nbs() from INFLATE.LUA and use its nested functions directly.
1 parent fd677d1 commit a0d7fda

1 file changed

Lines changed: 79 additions & 88 deletions

File tree

arch/INFLATE.LUA

Lines changed: 79 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,53 @@ if not LIB then print("Please run unzip instead") os.exit(1) end
66
---@param z boolean Is this deflate64
77
function inflate(r, w, z)
88

9-
local Z = z and 65536 or 32768
10-
11-
---New bit stream
12-
---@param rf function The reader function that returns a string of raw bytes
13-
---@return table A table full of functions to advance the bitstream (LSB first)
14-
function nbs(rf)
15-
16-
-- b = buffer
17-
-- p = position
18-
-- bb = bit buffer
19-
-- bc = bit count
20-
local b, p, bb, bc = "", 1, 0, 0
21-
22-
---Ensure at least the requested amount of bits are available in the stream, refilling from the byte stream if needed
23-
---@param n number The number of bits to have available in the bitstream
24-
function nb(n)
25-
while bc < n do
26-
if p > #b then
27-
28-
---Fill
29-
function I()
30-
if p > #b then
31-
b = rf()
32-
if b or #b ~= 0 then p = 1 else return 0 end
33-
end
34-
return 1
9+
-- Z = size of sliding window
10+
-- b = buffer
11+
-- p = position
12+
-- bb = bit buffer
13+
-- bc = bit count
14+
local Z, b, p, bb, bc = z and 65536 or 32768, "", 1, 0, 0
15+
16+
---Ensure at least the requested amount of bits are available in the stream, refilling from the byte stream if needed
17+
---@param n number The number of bits to have available in the bitstream
18+
function nb(n)
19+
while bc < n do
20+
if p > #b then
21+
22+
---Fill
23+
function I()
24+
if p > #b then
25+
b = r()
26+
if b or #b ~= 0 then p = 1 else return 0 end
3527
end
36-
37-
if I()==0 then error("!EOF",0) end
28+
return 1
3829
end
3930

40-
bb = bb + (b:byte(p) << bc)
41-
p = p + 1
42-
bc = bc + 8
31+
if I()==0 then error("!EOF",0) end
4332
end
44-
end
4533

46-
---Read a certain number of bits from the bitstream
47-
---@param n number The amount of bits to read
48-
---@return number The bits that where read
49-
function rb(n)
50-
nb(n)
51-
local v = bb & ((1 << n) - 1)
52-
bb, bc = (bb >> n), bc - n
53-
return v
34+
bb = bb + (b:byte(p) << bc)
35+
p = p + 1
36+
bc = bc + 8
5437
end
38+
end
5539

56-
---Discard pending bits and align to the next byte boundary
57-
function ab() bb, bc = 0, 0 end
58-
59-
-- return.r = read bits
60-
-- return.a = align to byte
61-
return { r = rb, a = ab }
40+
---Read a certain number of bits from the bitstream
41+
---@param n number The amount of bits to read
42+
---@return number The bits that where read
43+
function rb(n)
44+
nb(n)
45+
local v = bb & ((1 << n) - 1)
46+
bb, bc = (bb >> n), bc - n
47+
return v
6248
end
6349

64-
-- bs = bit stream
6550
-- op = output position
6651
-- kg = keep going (in a loop)
6752
-- sw = sliding window (32/64KB buffer)
6853
-- wp = window pointer
6954
-- o = output chunk buffer
70-
local bs, op, kg, sw, wp, o = nbs(r), 0, 1, {}, 0, {}
55+
local op, kg, sw, wp, o = 0, 1, {}, 0, {}
7156

7257
---Flush all unwritten data to the output file
7358
function F()
@@ -82,7 +67,7 @@ function inflate(r, w, z)
8267
op = op + 1
8368

8469
-- Add to output chunk
85-
o[#o +1] = string.char(byte)
70+
o[#o + 1] = string.char(byte)
8671
if #o >= 4096 then F() end
8772

8873
-- Add to history window
@@ -91,7 +76,7 @@ function inflate(r, w, z)
9176
end
9277

9378
---Make huffman
94-
function mh(lengths)
79+
function mh(ls)
9580

9681
-- m = maximum length
9782
-- c = counts
@@ -100,24 +85,26 @@ function inflate(r, w, z)
10085
-- t = tab
10186
local m, c, d, n, t = 0, {}, 0, {}, {}
10287

103-
for _, len in ipairs(lengths) do
104-
if len > 0 then
105-
c[len] = (c[len] or 0) + 1
106-
if len > m then m = len end
88+
-- l = length
89+
for _, l in ipairs(ls) do
90+
if l > 0 then
91+
c[l] = (c[l] or 0) + 1
92+
if l > m then m = l
93+
end
10794
end
10895
end
10996

110-
-- b = bits
111-
for b = 1, m do
112-
d = (d + (c[b - 1] or 0)) << 1
113-
n[b] = d
97+
-- i = bits
98+
for i = 1, m do
99+
d = (d + (c[i - 1] or 0)) << 1
100+
n[i] = d
114101
end
115102

116103
---Reverse bits
117-
function rv(x, bits)
104+
function rv(x, bi)
118105
local y = 0
119106

120-
for _ = 1, bits do
107+
for _ = 1, bi do
121108
y = (y << 1) | (x & 1)
122109
x = x >> 1
123110
end
@@ -127,7 +114,7 @@ function inflate(r, w, z)
127114

128115
-- s = sym
129116
-- l = len
130-
for s, l in ipairs(lengths) do
117+
for s, l in ipairs(ls) do
131118
if l > 0 then
132119
t[rv(n[l], l)] = { sym = s - 1, len = l }
133120
n[l] = n[l] + 1
@@ -145,7 +132,7 @@ function inflate(r, w, z)
145132

146133
-- l = len
147134
for l = 1, h.max do
148-
c = c | (bs.r(1) << (l - 1))
135+
c = c | (rb(1) << (l - 1))
149136
local e = h.tab[c & ((1 << l) - 1)]
150137
if e and e.len == l then return e.sym end
151138
end
@@ -157,20 +144,20 @@ function inflate(r, w, z)
157144

158145
-- f = final
159146
-- t = block type
160-
local f, t = bs.r(1), bs.r(2)
147+
local f, t = rb(1), rb(2)
161148

162149
if t == 0 then -- uncompressed block
163150

164151
-- lo = lower bits of 16-bit value
165152
-- hi = higher bits of 16-bit value
166153
-- l = length of value
167154
local lo, hi, l
168-
bs.a() -- align (disregard remaining bits)
169-
lo, hi = bs.r(8), bs.r(8) -- read length
155+
bb, bc = 0, 0 -- align (disregard remaining bits)
156+
lo, hi = rb(8), rb(8) -- read length
170157
l = lo + hi * 256 -- calculate length
171-
lo, hi = bs.r(8), bs.r(8) -- read n-length
158+
lo, hi = rb(8), rb(8) -- read n-length
172159
if (l ~ (lo + hi * 256)) ~= 0xFFFF then error("stored block LEN/NLEN mismatch", 0) end
173-
for _ = 1, l do ab(bs.r(8)) end -- copy raw data
160+
for _ = 1, l do ab(rb(8)) end -- copy raw data
174161

175162
elseif t == 1 or t == 2 then
176163

@@ -182,12 +169,12 @@ function inflate(r, w, z)
182169

183170
if t == 1 then
184171
-- fixed Huffman
185-
for _ = 0, 143 do ll[#ll+1] = 8 end
186-
for _ = 144, 255 do ll[#ll+1] = 9 end
187-
for _ = 256, 279 do ll[#ll+1] = 7 end
188-
for _ = 280, 287 do ll[#ll+1] = 8 end
172+
for _ = 0, 143 do ll[#ll + 1] = 8 end
173+
for _ = 144, 255 do ll[#ll + 1] = 9 end
174+
for _ = 256, 279 do ll[#ll + 1] = 7 end
175+
for _ = 280, 287 do ll[#ll + 1] = 8 end
189176
h = mh(ll)
190-
for _ = 0, 31 do dl[#dl+1] = 5 end
177+
for _ = 0, 31 do dl[#dl + 1] = 5 end
191178
d = mh(dl)
192179
else
193180
-- dynamic Huffman
@@ -199,10 +186,10 @@ function inflate(r, w, z)
199186
-- cl = clen
200187
-- le = lens
201188
-- ch = chuff
202-
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", {}, {}
189+
local hl, hd, hc, od, cl, le, 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", {}, {}
203190

204191
for i = 1, 19 do cl[i] = 0 end
205-
for i = 1, hc do cl[od:byte(i) + 1] = bs.r(3) end
192+
for i = 1, hc do cl[od:byte(i) + 1] = rb(3) end
206193
ch = mh(cl)
207194

208195
while #le < hl + hd do
@@ -213,13 +200,13 @@ function inflate(r, w, z)
213200
if s <= 15 then
214201
le[#le + 1] = s
215202
elseif s == 16 then
216-
local x, l = bs.r(2) + 3, le[#le]
203+
local x, l = rb(2) + 3, le[#le]
217204
for _ = 1, x do le[#le + 1] = l end
218205
elseif s == 17 then
219-
local x = bs.r(3) + 3
206+
local x = rb(3) + 3
220207
for _ = 1, x do le[#le + 1] = 0 end
221208
elseif s == 18 then
222-
local x = bs.r(7) + 11
209+
local x = rb(7) + 11
223210
for _ = 1, x do le[#le + 1] = 0 end
224211
end
225212
end
@@ -247,15 +234,16 @@ function inflate(r, w, z)
247234
break
248235
else
249236

250-
-- b = base
251-
-- x = extra
252-
-- a = add
253-
-- l = length
254-
local li, b, x, l = (s - 257) * 2 + 1
237+
-- li = length index
238+
-- ba = base
239+
-- x = extra
240+
-- a = add
241+
-- l = length
242+
local li, ba, x, l = (s - 257) * 2 + 1
255243

256-
b, x = lex:byte(li), lex:byte(li + 1)
257-
if s == 285 then b = 258 end
258-
l = b + (x > 0 and bs.r(x) or 0)
244+
ba, x = lex:byte(li), lex:byte(li + 1)
245+
if s == 285 then ba = 258 end
246+
l = ba + (x > 0 and rb(x) or 0)
259247

260248
if d.max > 0 and #d.tab > 0 then
261249

@@ -268,16 +256,19 @@ function inflate(r, w, z)
268256
-- Extract 32-bit unsigned little-endian integer (I4) and extra bits byte
269257
db = string.unpack("<I4", dib, ds * 4 + 1)
270258
dx = dix:byte(ds + 1)
271-
dv = db + (dx > 0 and bs.r(dx) or 0)
259+
dv = db + (dx > 0 and rb(dx) or 0)
272260

273261
if dv <= 0 or dv > op then
274262
print("!Distance", dv, Z, op) os.exit(1)
275263
end
276264

277265
-- Read from window history
278266
for _ = 1, l do
279-
local p = (wp - dv) % Z
280-
ab(sw[p] or 0)
267+
268+
-- hi = history
269+
local hi = (wp - dv) % Z
270+
271+
ab(sw[hi] or 0)
281272
end
282273
end
283274
end

0 commit comments

Comments
 (0)