interplane.Get Omega
Read in your language

omega-zip

omega-zip: ZIP archive reader in seeded Omega (store and deflate, CRC-32, fail-closed refusals)

Brought by drakestapleton

branch main Branches Tags History

omega-zip / src / inflate.om

373 lines · 6531 bytes Raw Blame History
1note ======== src/inflate.om: DEFLATE decoder (RFC 1951) ========
2note Canonical Huffman decoding one bit at a time (RFC 1951 section 3.2.2), the 32 KiB window of
3note section 2, stored (3.2.4), fixed (3.2.6) and dynamic (3.2.7) blocks, every length code 257..285
4note and distance code 0..29 (3.2.5). Each malformed case is its own exit code (README table).
5
6text tbtype "invalid block type"
7text tstlen "stored block length does not match its complement"
8text tnsym "too many length or distance codes"
9text tclset "code-length code is over-subscribed or incomplete"
10text trep "invalid code-length repeat"
11text tlset "literal/length code lengths are over-subscribed or incomplete"
12text tdset "distance code lengths are over-subscribed or incomplete"
13text teob "no end-of-block code"
14text tlcode "invalid literal/length code"
15text tdcode "invalid distance code"
16text tfar "distance too far back"
17
18note tabinit: code-length order (3.2.7) and the base and extra-bit tables of section 3.2.5.
19note Lengths: codes 257..264 are 3..10 with no extra bits, then four codes per extra-bit count 1..5,
20note and code 285 is 258 with none. Distances: codes 0..3 are 1..4, then two codes per extra-bit
21note count 1..13.
22fn tabinit to void
23put ord 0 16
24put ord 1 17
25put ord 2 18
26put ord 3 0
27put ord 4 8
28put ord 5 7
29put ord 6 9
30put ord 7 6
31put ord 8 10
32put ord 9 5
33put ord 10 11
34put ord 11 4
35put ord 12 12
36put ord 13 3
37put ord 14 13
38put ord 15 2
39put ord 16 14
40put ord 17 1
41put ord 18 15
42b = ushort 3
43for i ulong 28
44e = ulong 0
45if ge i 8
46e = sub div i 4 1
47end
48put lbase i b
49put lext i uchar e
50b = add b ushort shl ulong 1 e
51end
52put lbase 28 258
53put lext 28 0
54d = ushort 1
55for i ulong 30
56e = ulong 0
57if ge i 4
58e = sub div i 2 1
59end
60put dbase i d
61put dext i uchar e
62d = add d ushort shl ulong 1 e
63end
64end
65
66fn needb n ulong to void
67c = get bitn 0
68if ge c n
69ret
70end
71b = get bitb 0
72while lt c n bound 9
73b = or b shl inbyte c
74c = add c 8
75end
76put bitb 0 b
77put bitn 0 c
78end
79
80fn bits n ulong to ulong
81needb n
82b = get bitb 0
83v = and b sub shl ulong 1 n 1
84put bitb 0 shr b n
85put bitn 0 sub get bitn 0 n
86ret v
87end
88
89note hbuild: canonical code for the n lengths hlen[lo ..] into table t. Returns the codes left
90note unused at the longest length: 0 complete, above 0 incomplete, below 0 over-subscribed.
91fn hbuild t ulong n ulong lo ulong to long
92co = mul t 16
93so = mul t 288
94for l ulong 16
95put hcnt add co l 0
96end
97for s ulong n bound 320
98k = add co ulong get hlen add lo s
99put hcnt k add get hcnt k 1
100end
101left = long 1
102for l ulong 15
103left = sub shl left 1 long get hcnt add co add l 1
104if lt left 0
105ret left
106end
107end
108put hoff 1 0
109for l ulong 14
110m = add l 1
111put hoff add m 1 add get hoff m get hcnt add co m
112end
113for s ulong n bound 320
114k = ulong get hlen add lo s
115if ne k 0
116o = ulong get hoff k
117put hsym add so o ushort s
118put hoff k ushort add o 1
119end
120end
121ret left
122end
123
124note hdec: one symbol of table t, reading the code most significant bit first (3.1.1).
125fn hdec t ulong to ulong
126co = mul t 16
127so = mul t 288
128code = ulong 0
129first = ulong 0
130index = ulong 0
131b = get bitb 0
132c = get bitn 0
133for l ulong 15
134if eq c 0
135b = inbyte
136c = 8
137end
138code = or code and b 1
139b = shr b 1
140c = sub c 1
141n = ulong get hcnt add co add l 1
142if lt code add first n
143put bitb 0 b
144put bitn 0 c
145ret ulong get hsym add so add index sub code first
146end
147index = add index n
148first = shl add first n 1
149code = shl code 1
150end
151if eq t 0
152fail 19 tlcode
153ret 0
154end
155fail 20 tdcode
156ret 0
157end
158
159note hsingle: an incomplete code is admitted only when it is one code of length 1 (as zlib does).
160fn hsingle t ulong n ulong to bool
161co = mul t 16
162ret eq n add ulong get hcnt co ulong get hcnt add co 1
163end
164
165note fixed: the tables of 3.2.6; all 32 five-bit distance codes exist, so codes 30 and 31 decode
166note (and are refused as invalid distance codes) instead of reading on.
167fn fixed to void
168if eq get htyp 0 1
169ret
170end
171for s ulong 288
172v = uchar 8
173if ge s 144
174v = 9
175end
176if ge s 256
177v = 7
178end
179if ge s 280
180v = 8
181end
182put hlen s v
183end
184for s ulong 32
185put hlen add 288 s 5
186end
187l = hbuild 0 288 0
188l = hbuild 1 32 288
189put htyp 0 1
190end
191
192fn dynamic to void
193put htyp 0 0
194nlen = add bits 5 257
195ndist = add bits 5 1
196ncode = add bits 4 4
197if gt nlen 286
198fail 13 tnsym
199end
200if gt ndist 30
201fail 13 tnsym
202end
203for i ulong 19
204put hlen add 320 i 0
205end
206for i ulong ncode bound 19
207put hlen add 320 ulong get ord i uchar bits 3
208end
209l = hbuild 0 19 320
210if ne l 0
211fail 14 tclset
212end
213if eq ulong get hcnt 0 19
214fail 14 tclset
215end
216tot = add nlen ndist
217i = ulong 0
218while lt i tot bound 320
219s = hdec 0
220if lt s 16
221put hlen i uchar s
222i = add i 1
223continue
224end
225v = uchar 0
226r = ulong 0
227if eq s 16
228if eq i 0
229fail 15 trep
230end
231v = get hlen sub i 1
232r = add bits 2 3
233else
234if eq s 17
235r = add bits 3 3
236else
237r = add bits 7 11
238end
239end
240if gt add i r tot
241fail 15 trep
242end
243for j ulong r bound 138
244put hlen add i j v
245end
246i = add i r
247end
248if eq get hlen 256 0
249fail 18 teob
250end
251l = hbuild 0 nlen 0
252if lt l 0
253fail 16 tlset
254end
255if gt l 0
256if not hsingle 0 nlen
257fail 16 tlset
258end
259end
260l = hbuild 1 ndist nlen
261if lt l 0
262fail 17 tdset
263end
264if gt l 0
265if not hsingle 1 ndist
266fail 17 tdset
267end
268end
269end
270
271note wcopy: n bytes from d back, one at a time so an overlapping copy repeats (3.2.3).
272fn wcopy d ulong n ulong to void
273if gt d get otot 0
274fail 21 tfar
275ret
276end
277t = add get otot 0 n
278if gt t get olim 0
279ofail
280ret
281end
282put otot 0 t
283p = get wpos 0
284s = and sub add p WSIZE d WMASK
285for i ulong n bound 258
286put win p get win s
287p = add p 1
288s = and add s 1 WMASK
289if eq p WSIZE
290wflush WSIZE
291p = 0
292end
293end
294put wpos 0 p
295end
296
297fn codes to void
298while true bound 9223372036854775807
299s = hdec 0
300if lt s 256
301outb s
302continue
303end
304if eq s 256
305ret
306end
307s = sub s 257
308if ge s 29
309fail 19 tlcode
310ret
311end
312n = add ulong get lbase s bits ulong get lext s
313x = hdec 1
314if ge x 30
315fail 20 tdcode
316ret
317end
318d = add ulong get dbase x bits ulong get dext x
319wcopy d n
320end
321end
322
323fn stored to void
324put bitb 0 0
325put bitn 0 0
326a = inbyte
327b = inbyte
328n = or a shl b 8
329a = inbyte
330b = inbyte
331m = or a shl b 8
332if ne n xor m 65535
333fail 12 tstlen
334ret
335end
336if gt add get otot 0 n get olim 0
337ofail
338ret
339end
340for i ulong n bound 65535
341outb inbyte
342end
343end
344
345note inflate: one raw DEFLATE stream from the input source into the output sink; the last
346note window part is flushed. Bytes after the final block are not read (the caller may read on).
347fn inflate to void
348put bitb 0 0
349put bitn 0 0
350put wpos 0 0
351put otot 0 0
352last = ulong 0
353while eq last 0 bound 9223372036854775807
354last = bits 1
355t = bits 2
356if eq t 0
357stored
358continue
359end
360if eq t 1
361fixed
362codes
363continue
364end
365if eq t 2
366dynamic
367codes
368continue
369end
370fail 11 tbtype
371end
372wflush get wpos 0
373end