Skip to content

Commit 4ed4a08

Browse files
committed
Improved variable documentation and formatting.
1 parent ef1b7af commit 4ed4a08

1 file changed

Lines changed: 63 additions & 64 deletions

File tree

arch/INFLATE.LUA

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ if not LIB then print("Please run unzip instead") os.exit(1) end
66
---@param f file The output file handle to allow seeking the sliding window
77
function inflate(r, w, f)
88

9-
-- Z = size of sliding window
10-
-- b = buffer
11-
-- p = position
12-
-- bb = bit buffer
13-
-- bc = bit count
14-
-- op = output position
15-
-- kg = keep going (in a loop)
16-
-- lex = length base & extra bits (pairs of bytes)
17-
-- dix = distance extra bits (1 byte per entry)
18-
-- dib = distance base (packed 32-bit LE)
19-
local Z, b, p, bb, bc, op, kg, lex, dix, dib = z and 65536 or 32768, "", 1, 0, 0, 0, 1,
20-
-- lex, dix, dib packed strings
21-
9+
-- b = Buffer
10+
-- p = Position
11+
-- bb = Bit buffer
12+
-- bc = Bit count
13+
-- op = Output position
14+
-- kg = Keep going (in a loop)
15+
-- lex = Length base & extra bits (pairs of bytes)
16+
-- dix = Distance extra bits (1 byte per entry)
17+
-- dib = Distance base (packed 32-bit LE)
18+
local b, p, bb, bc, op, kg, lex, dix, dib = "", 1, 0, 0, 0, 1,
19+
20+
-- lex, dix & dib are packed string constants
2221
"\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",
2322
"\0\0\0\0\1\1\2\2\3\3\4\4\5\5\6\6\7\7\8\8\9\9\10\10\11\11\12\12\13\13\14\14\15\15\16\16\17\17\18\18\19\19\20\20\21\21\22\22\23\23\24\24\25\25\26\26\27\27\28\28\29\29\30\30",
2423
"\1\0\0\0\2\0\0\0\3\0\0\0\4\0\0\0\5\0\0\0\7\0\0\0\9\0\0\0\13\0\0\0\17\0\0\0\25\0\0\0\33\0\0\0\49\0\0\0\65\0\0\0\97\0\0\0\129\0\0\0\193\0\0\0\1\1\0\0\129\1\0\0\1\2\0\0\1\3\0\0\1\4\0\0\1\6\0\0\1\8\0\0\1\12\0\0\1\16\0\0\1\24\0\0\1\32\0\0\1\48\0\0\1\64\0\0\1\96\0\0\1\128\0\0\1\192\0\0\1\0\1\0\1\128\1\0\1\0\2\0\1\128\2\0\1\0\4\0\1\128\4\0\1\0\8\0\1\128\8\0\1\0\16\0\1\128\16\0\1\0\32\0\1\128\32\0\1\0\64\0\1\128\64\0\1\0\128\0\1\128\128\0\1\0\0\1\1\128\0\1\1\0\0\2\1\128\0\2\1\0\0\4\1\128\0\4\1\0\0\8\1\128\0\8\1\0\0\16\1\128\0\16\1\0\0\32\1\128\0\32\1\0\0\64\1\128\0\64\1\0\0\128\1\128\0\128\1\0\0\0\192"
@@ -53,7 +52,7 @@ function inflate(r, w, f)
5352
function rb(n)
5453
nb(n)
5554

56-
-- v = extracted bit value
55+
-- v = Extracted bit value
5756
local v = bb & ((1 << n) - 1)
5857

5958
bb, bc = (bb >> n), bc - n
@@ -65,14 +64,14 @@ function inflate(r, w, f)
6564
---@return table A Huffman object containing the lookup table (`tab`) and the maximum code length (`max`).
6665
function mh(ls)
6766

68-
-- m = maximum length
69-
-- c = counts
70-
-- d = code
71-
-- n = next code
72-
-- t = tab
67+
-- m = Maximum length
68+
-- c = Counts
69+
-- d = Code
70+
-- n = Next code
71+
-- t = Tab
7372
local m, c, d, n, t = 0, {}, 0, {}, {}
7473

75-
-- l = length
74+
-- l = Length
7675
for _, l in ipairs(ls) do
7776
if l > 0 then
7877
c[l] = (c[l] or 0) + 1
@@ -81,7 +80,7 @@ function inflate(r, w, f)
8180
end
8281
end
8382

84-
-- i = bits
83+
-- i = Bits
8584
for i = 1, m do
8685
d = (d + (c[i - 1] or 0)) << 1
8786
n[i] = d
@@ -102,8 +101,8 @@ function inflate(r, w, f)
102101
return y
103102
end
104103

105-
-- s = sym
106-
-- l = len
104+
-- s = Sym
105+
-- l = Len
107106
for s, l in ipairs(ls) do
108107
if l > 0 then
109108
t[rv(n[l], l)] = { sym = s - 1, len = l }
@@ -119,14 +118,14 @@ function inflate(r, w, f)
119118
---@return number The decoded symbol value.
120119
function rh(h)
121120

122-
-- c = code
121+
-- c = Code
123122
local c = 0
124123

125-
-- l = len
124+
-- l = Len
126125
for l = 1, h.max do
127126
c = c | (rb(1) << (l - 1))
128127

129-
-- e = entry
128+
-- e = Entry
130129
local e = h.tab[c & ((1 << l) - 1)]
131130

132131
if e and e.len == l then return e.sym end
@@ -137,21 +136,21 @@ function inflate(r, w, f)
137136

138137
while kg > 0 do
139138

140-
-- a = final
141-
-- t = block type
139+
-- a = Final
140+
-- t = Block type
142141
local a, t = rb(1), rb(2)
143142

144-
if t == 0 then -- uncompressed block
143+
if t == 0 then -- Uncompressed block
145144

146-
-- lo = lower bits of 16-bit value
147-
-- hi = higher bits of 16-bit value
148-
-- l = length of value
145+
-- lo = Lower bits of 16-bit value
146+
-- hi = Higher bits of 16-bit value
147+
-- l = Length of value
149148
local lo, hi, l
150149

151-
bb, bc = 0, 0 -- align and disregard remaining bits
152-
lo, hi = rb(8), rb(8) -- read length
153-
l = lo + hi * 256 -- calculate length
154-
lo, hi = rb(8), rb(8) -- read n-length
150+
bb, bc = 0, 0 -- Align and disregard remaining bits
151+
lo, hi = rb(8), rb(8) -- Read length
152+
l = lo + hi * 256 -- Calculate length
153+
lo, hi = rb(8), rb(8) -- Read n-length
155154
if (l ~ (lo + hi * 256)) ~= 0xFFFF then error("stored block LEN/NLEN mismatch", 0) end
156155

157156
-- Copy raw data
@@ -171,14 +170,14 @@ function inflate(r, w, f)
171170

172171
elseif t == 1 or t == 2 then
173172

174-
-- ll = literal length
175-
-- dl = distance length
176-
-- h = huffman
177-
-- d = distances
173+
-- ll = Literal/length bit-lengths
174+
-- dl = Distance bit-lengths
175+
-- h = Literal/length Huffman decoding table
176+
-- d = Distance Huffman decoding table
178177
local ll, dl, h, d = {}, {}
179178

180179
if t == 1 then
181-
-- fixed Huffman
180+
-- Fixed Huffman
182181
for _ = 0, 143 do ll[#ll + 1] = 8 end
183182
for _ = 144, 255 do ll[#ll + 1] = 9 end
184183
for _ = 256, 279 do ll[#ll + 1] = 7 end
@@ -187,15 +186,15 @@ function inflate(r, w, f)
187186
for _ = 0, 31 do dl[#dl + 1] = 5 end
188187
d = mh(dl)
189188
else
190-
-- dynamic Huffman
191-
192-
-- hl = hlit
193-
-- hd = hdist
194-
-- hc = hclen
195-
-- od = order
196-
-- cl = clen
197-
-- le = lens
198-
-- ch = chuff
189+
-- Dynamic Huffman
190+
191+
-- hl = Literal/length code count (HLIT)
192+
-- hd = Distance code count (HDIST)
193+
-- hc = Meta-code length count (HCLEN)
194+
-- od = Permutation order for code lengths
195+
-- cl = Code lengths for the meta-table
196+
-- le = Decoded bit-lengths for literal/distance tables
197+
-- ch = Huffman table for decoding bit-lengths
199198
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", {}, {}
200199

201200
for i = 1, 19 do cl[i] = 0 end
@@ -204,9 +203,9 @@ function inflate(r, w, f)
204203

205204
while #le < hl + hd do
206205

207-
-- s = symbol
208-
-- x = repeat count
209-
-- l = code length
206+
-- s = Symbol
207+
-- x = Repeat count
208+
-- l = Code length
210209
local s, x, l = rh(ch)
211210

212211
if s <= 15 then
@@ -229,7 +228,7 @@ function inflate(r, w, f)
229228

230229
while true do
231230

232-
-- s = symbol
231+
-- s = Symbol
233232
local s = rh(h)
234233

235234
if s < 256 then
@@ -239,11 +238,11 @@ function inflate(r, w, f)
239238
break
240239
else
241240

242-
-- li = length index
243-
-- ba = base
244-
-- x = extra
245-
-- a = add
246-
-- l = length
241+
-- li = Length index
242+
-- ba = Base
243+
-- x = Extra
244+
-- a = Add
245+
-- l = Length
247246
local li, ba, x, l = (s - 257) * 2 + 1
248247

249248
ba, x = lex:byte(li), lex:byte(li + 1)
@@ -252,10 +251,10 @@ function inflate(r, w, f)
252251

253252
if d.max > 0 and #d.tab > 0 then
254253

255-
-- ds = distance symbol
256-
-- db = distance base
257-
-- dx = distance extra bits
258-
-- dv = distance value
254+
-- ds = Distance symbol
255+
-- db = Distance base
256+
-- dx = Distance extra bits
257+
-- dv = Distance value
259258
local ds, db, dx, dv = rh(d)
260259

261260
-- Extract 32-bit unsigned little-endian integer (I4) and extra bits byte
@@ -270,7 +269,7 @@ function inflate(r, w, f)
270269
-- Read from output history using the file-backed sliding window
271270
f:seek("end", -dv)
272271

273-
-- n = pattern
272+
-- n = Pattern
274273
local n = f:read(math.min(dv, l))
275274

276275
n = n:rep(l // dv) .. n:sub(1, l % dv)

0 commit comments

Comments
 (0)