Skip to content

Commit 53f6cc1

Browse files
committed
Refactored code to limit memory usage on 16-bit systems.
1 parent 90c5d1e commit 53f6cc1

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

arch/INFLATE.LUA

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ function inflate(r, w, f)
5959
return v
6060
end
6161

62+
---Reverses the order of the lower `bi` bits in an integer.
63+
---@param x number The integer value containing the bits to reverse.
64+
---@param bi number The number of bits to process.
65+
---@return number The bit-reversed value.
66+
function rv(x, bi)
67+
local y = 0
68+
69+
for _ = 1, bi do
70+
y = (y << 1) | (x & 1)
71+
x = x >> 1
72+
end
73+
74+
return y
75+
end
76+
6277
---Constructs a canonical Huffman decoding table from a list of code lengths.
6378
---@param ls table An array where the index is the symbol and the value is its bit length.
6479
---@return table A Huffman object containing the lookup table (`tab`) and the maximum code length (`max`).
@@ -86,21 +101,6 @@ function inflate(r, w, f)
86101
n[i] = d
87102
end
88103

89-
---Reverses the order of the lower `bi` bits in an integer.
90-
---@param x number The integer value containing the bits to reverse.
91-
---@param bi number The number of bits to process.
92-
---@return number The bit-reversed value.
93-
function rv(x, bi)
94-
local y = 0
95-
96-
for _ = 1, bi do
97-
y = (y << 1) | (x & 1)
98-
x = x >> 1
99-
end
100-
101-
return y
102-
end
103-
104104
-- s = Sym
105105
-- l = Len
106106
for s, l in ipairs(ls) do
@@ -151,7 +151,7 @@ function inflate(r, w, f)
151151
lo, hi = rb(8), rb(8) -- Read length
152152
l = lo + hi * 256 -- Calculate length
153153
lo, hi = rb(8), rb(8) -- Read n-length
154-
if (l ~ (lo + hi * 256)) ~= 0xFFFF then error("stored block LEN/NLEN mismatch", 0) end
154+
if (l ~ (lo + hi * 256)) ~= 0xFFFF then error("!LEN", 0) end
155155

156156
-- Copy raw data
157157
while l > 0 do
@@ -234,6 +234,7 @@ function inflate(r, w, f)
234234
if s < 256 then
235235
op = op + 1
236236
w(string.char(s))
237+
f:flush()
237238
elseif s == 256 then
238239
break
239240
else
@@ -275,14 +276,17 @@ function inflate(r, w, f)
275276
n = n:rep(l // dv) .. n:sub(1, l % dv)
276277
f:seek("end")
277278
w(n)
279+
f:flush()
278280
op = op + l
279281
end
280282
end
283+
collectgarbage("step")
281284
end
282285
else
283286
error("!Unsupported", 0)
284287
end
285288

286289
if a == 1 then kg = 0 end
290+
collectgarbage("step")
287291
end
288292
end

arch/UNZIP.LUA

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ function unzip(z, d, l, x)
128128

129129
of, E = io.open(fn, "w+b") D()
130130
print("Extracting: " .. fn)
131+
fn = nil
131132

132133
if of then
133134
---Write decompressed data and update CRC

0 commit comments

Comments
 (0)