Skip to content

Commit cd112c7

Browse files
committed
Convert lex and dix constant tables into a series of packed bytes to save memory.
1 parent 2963921 commit cd112c7

File tree

1 file changed

+24
-67
lines changed

1 file changed

+24
-67
lines changed

arch/INFLATE.LUA

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -226,57 +226,13 @@ function inflate(r, w, z)
226226
h = mh(ll) d = mh(dl)
227227
end
228228

229-
-- lex = length extra
230-
-- dix = dist extra
231-
local lex, dix = {
232-
[257]={3,0},[258]={4,0},[259]={5,0},[260]={6,0},[261]={7,0},
233-
[262]={8,0},[263]={9,0},[264]={10,0},[265]={11,1},[266]={13,1},
234-
[267]={15,1},[268]={17,1},[269]={19,2},[270]={23,2},[271]={27,2},
235-
[272]={31,2},[273]={35,3},[274]={43,3},[275]={51,3},[276]={59,3},
236-
[277]={67,4},[278]={83,4},[279]={99,4},[280]={115,4},[281]={131,5},
237-
[282]={163,5},[283]={195,5},[284]={227,5},[285]={258,0}
238-
},{
239-
{1,0},{2,0},{3,0},{4,0},
240-
{5,1},{7,1},
241-
{9,2},{13,2},
242-
{17,3},{25,3},
243-
{33,4},{49,4},
244-
{65,5},{97,5},
245-
{129,6},{193,6},
246-
{257,7},{385,7},
247-
{513,8},{769,8},
248-
{1025,9},{1537,9},
249-
{2049,10},{3073,10},
250-
{4097,11},{6145,11},
251-
{8193,12},{12289,12},
252-
{16385,13},{24577,13},
253-
254-
-- Inflate64 below
255-
{32769,14},{49153,14},
256-
{65537,15},{98305,15},
257-
{131073,16},{196609,16},
258-
{262145,17},{393217,17},
259-
{524289,18},{786433,18},
260-
{1048577,19},{1572865,19},
261-
{2097153,20},{3145729,20},
262-
{4194305,21},{6291457,21},
263-
{8388609,22},{12582913,22},
264-
{16777217,23},{25165825,23},
265-
{33554433,24},{50331649,24},
266-
{67108865,25},{100663297,25},
267-
{134217729,26},{201326593,26},
268-
{268435457,27},{402653185,27},
269-
{536870913,28},{805306369,28},
270-
{1073741825,29},{1610612737,29},
271-
{2147483649,30},{3221225473,30},
272-
}
273-
274-
-- Strip addition table values out when not in inflate64 mode
275-
if not z then
276-
for _ = 1, 33 do
277-
table.remove(dix)
278-
end
279-
end
229+
-- lex = length base & extra bits (pairs of bytes)
230+
-- dix = distance extra bits (1 byte per entry)
231+
-- dib = distance base (packed 32-bit LE)
232+
local lex, dix, dib =
233+
"\003\000\004\000\005\000\006\000\007\000\008\000\009\000\010\000\011\001\013\001\015\001\017\001\019\002\023\002\027\002\031\002\035\003\043\003\051\003\059\003\067\004\083\004\099\004\115\004\131\005\163\005\195\005\227\005\002\000",
234+
"\000\000\000\000\001\001\002\002\003\003\004\004\005\005\006\006\007\007\008\008\009\009\010\010\011\011\012\012\013\013\014\014\015\015\016\016\017\017\018\018\019\019\020\020\021\021\022\022\023\023\024\024\025\025\026\026\027\027\028\028\029\029\030\030",
235+
"\001\000\000\000\002\000\000\000\003\000\000\000\004\000\000\000\005\000\000\000\007\000\000\000\009\000\000\000\013\000\000\000\017\000\000\000\025\000\000\000\033\000\000\000\049\000\000\000\065\000\000\000\097\000\000\000\129\000\000\000\193\000\000\000\001\001\000\000\129\001\000\000\001\002\000\000\001\003\000\000\001\004\000\000\001\006\000\000\001\008\000\000\001\012\000\000\001\016\000\000\001\024\000\000\001\032\000\000\001\048\000\000\001\064\000\000\001\096\000\000\001\128\000\000\001\192\000\000\001\000\001\000\001\128\001\000\001\000\002\000\001\128\002\000\001\000\004\000\001\128\004\000\001\000\008\000\001\128\008\000\001\000\016\000\001\128\16\000\001\000\032\000\001\128\032\000\001\000\064\000\001\128\064\000\001\000\128\000\001\128\128\000\001\000\000\001\001\128\000\001\001\000\000\002\001\128\000\002\001\000\000\004\001\128\000\004\001\000\000\008\001\128\000\008\001\000\000\016\001\128\000\016\001\000\000\032\001\128\000\032\001\000\000\064\001\128\000\064\001\000\000\128\001\128\000\128\001\000\000\000\192"
280236

281237
while true do
282238
-- s = sym
@@ -287,27 +243,28 @@ function inflate(r, w, z)
287243
break
288244
else
289245

290-
-- e = entry
291246
-- b = base
292247
-- x = extra
293248
-- a = add
294249
-- l = length
295-
-- ds = dsym
296-
-- de = dentry
297-
-- db = dbase
298-
-- dx = dextra
299-
-- da = dadd
300-
local e, b, x, a, l, ds, de, db, dx, da, dv = lex[s]
301-
302-
b, x = e[1], e[2]
303-
a = x > 0 and bs.r(x) or 0
304-
l = b + a
250+
local li, b, x, l = (s - 257) * 2 + 1
251+
b, x = lex:byte(li), lex:byte(li + 1)
252+
if s == 285 then b = 258 end
253+
254+
l = b + (x > 0 and bs.r(x) or 0)
255+
305256
if d.max > 0 and #d.tab > 0 then
306-
ds = rh(d)
307-
de = dix[ds + 1]
308-
db, dx = de[1], de[2]
309-
da = dx > 0 and bs.r(dx) or 0
310-
dv = db + da
257+
258+
-- ds = distance symbol
259+
-- db = distance base
260+
-- dx = distance extra bits
261+
-- dv = distance value
262+
local ds, db, dx, dv = rh(d)
263+
264+
-- Extract 32-bit unsigned little-endian integer (I4) and extra bits byte
265+
db = string.unpack("<I4", dib, ds * 4 + 1)
266+
dx = dix:byte(ds + 1)
267+
dv = db + (dx > 0 and bs.r(dx) or 0)
311268

312269
if dv <= 0 or dv > op then
313270
print("!Distance", dv, Z, op) os.exit(1)

0 commit comments

Comments
 (0)