Skip to content

Commit 1d17494

Browse files
committed
Improve multiple functions documentation, names and layout.
1 parent c3b79b0 commit 1d17494

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

arch/INFLATE.LUA

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ function inflate(r, w, z)
1313
-- bc = bit count
1414
local Z, b, p, bb, bc = z and 65536 or 32768, "", 1, 0, 0
1515

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
16+
---Internal buffer refill function.
17+
---@return number Zero on error, otherwise the buffer has been filled.
18+
function fi()
19+
if p > #b then
20+
b = r()
21+
if b and #b ~= 0 then p = 1 else return 0 end
22+
end
23+
return 1
24+
end
25+
26+
---Ensures at least the requested amount of bits are available in the bitstream, refilling from the reader if needed.
27+
---@param n number The number of bits required.
1828
function nb(n)
1929
while bc < n do
2030
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
27-
end
28-
return 1
29-
end
30-
31-
if I()==0 then error("!EOF",0) end
31+
if fi() == 0 then error("!EOF",0) end
3232
end
3333

3434
bb = bb + (b:byte(p) << bc)
@@ -37,9 +37,9 @@ function inflate(r, w, z)
3737
end
3838
end
3939

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
40+
---Reads a specific number of bits from the bitstream.
41+
---@param n number The number of bits to read.
42+
---@return number The decoded bit value.
4343
function rb(n)
4444
nb(n)
4545

@@ -58,27 +58,30 @@ function inflate(r, w, z)
5858
local op, kg, sw, wp, o = 0, 1, {}, 0, {}
5959

6060
---Flush all unwritten data to the output file
61-
function F()
61+
function fl()
6262
if #o > 0 then
6363
w(table.concat(o))
6464
o = {}
6565
end
6666
end
6767

68-
---Append byte
69-
function ab(byte)
68+
---Appends a decoded byte to the output buffer and updates the sliding window history.
69+
---@param by number The byte value (0-255) to append.
70+
function ab(by)
7071
op = op + 1
7172

7273
-- Add to output chunk
73-
o[#o + 1] = string.char(byte)
74-
if #o >= 4096 then F() end
74+
o[#o + 1] = string.char(by)
75+
if #o >= 4096 then fl() end
7576

7677
-- Add to history window
77-
sw[wp] = byte
78+
sw[wp] = by
7879
wp = (wp + 1) % Z
7980
end
8081

81-
---Make huffman
82+
---Constructs a canonical Huffman decoding table from a list of code lengths.
83+
---@param ls table An array where the index is the symbol and the value is its bit length.
84+
---@return table A Huffman object containing the lookup table (`tab`) and the maximum code length (`max`).
8285
function mh(ls)
8386

8487
-- m = maximum length
@@ -103,7 +106,10 @@ function inflate(r, w, z)
103106
n[i] = d
104107
end
105108

106-
---Reverse bits
109+
---Reverses the order of the lower `bi` bits in an integer.
110+
---@param x number The integer value containing the bits to reverse.
111+
---@param bi number The number of bits to process.
112+
---@return number The bit-reversed value.
107113
function rv(x, bi)
108114
local y = 0
109115

@@ -127,7 +133,9 @@ function inflate(r, w, z)
127133
return { tab = t, max = m }
128134
end
129135

130-
---Read huffman
136+
---Decodes the next symbol from the bitstream using the provided Huffman table.
137+
---@param h table The Huffman table object containing the lookup table (`tab`) and maximum bit length (`max`).
138+
---@return number The decoded symbol value.
131139
function rh(h)
132140

133141
-- c = code
@@ -290,5 +298,5 @@ function inflate(r, w, z)
290298
if f == 1 then kg = 0 end
291299
end
292300

293-
F()
301+
fl()
294302
end

0 commit comments

Comments
 (0)