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 / tests / mkcases.om

544 lines · 12437 bytes Raw Blame History
1om2
2note tests/mkcases.om: writes the hand-built DEFLATE and zlib streams and the patched zip files under
3note corpus/refuse/ (refusal fixtures, and a few valid hand-built streams with their expected output).
4note Build and run from the repository root with the pinned omc (README "Gate"):
5note omc < tests/mkcases.om > build/mkcases && chmod +x build/mkcases && build/mkcases
6note Deterministic: the same inputs give the same bytes. Each stream in the text `cases` is one line
7note NAME KIND DATA. KIND b: DATA is a bit string in stream order ('0'/'1', spaces ignored), header
8note fields least significant bit first, Huffman codes most significant bit first (RFC 1951 3.1.1),
9note padded with zero bits to a whole byte. KIND h: DATA is hex bytes.
10note The zip fixtures are patched copies of corpus/zip/base.zip, st.zip and dd.zip (made by Info-ZIP
11note zip 3.0, corpus/SOURCES.txt), with the offsets of APPNOTE 4.3.7, 4.3.9, 4.3.12 and 4.3.16.
12
13global bo uchar 65536
14global ex uchar 65536
15global fb uchar 65536
16global pth uchar 512
17note st: 0 bytes in bo, 1 bit buffer, 2 bits in it, 3 bytes in ex, 4 bytes in fb, 5 files written
18global st ulong 8
19global lb ushort 29
20global lx uchar 29
21global db ushort 30
22global dx uchar 30
23
24text tdir "corpus/refuse/"
25text tzdir "corpus/zip/"
26text tldir "corpus/zlib/"
27text tfail "mkcases: cannot write or read "
28text tnl "\n"
29text tdone "mkcases: files written "
30
31fn die k ulong to void
32r = write 2 tfail 0 len tfail
33r = write 2 pth 0 k
34r = write 2 tnl 0 1
35exit 1
36end
37
38note ---- paths: pth = DIR + one word of a word list ----
39fn pfx d view uchar * to ulong
40for i ulong len d bound 256
41put pth i get d i
42end
43ret len d
44end
45
46note word: append word k (0-based, space separated) of t at pth[o ..]; the new length.
47fn word t view uchar * k ulong o ulong to ulong
48w = ulong 0
49p = o
50inw = false
51for i ulong len t bound 4096
52c = get t i
53if eq c 32
54if inw
55w = add w 1
56end
57inw = false
58continue
59end
60inw = true
61if eq w k
62put pth p c
63p = add p 1
64end
65end
66ret p
67end
68
69note wbuf: buffer b (0 bo, 1 ex, 2 fb) to the file pth[0 .. k).
70fn wbuf b ulong k ulong to void
71fd = fopen pth 0 k 577 420
72if lt fd 0
73die k
74end
75n = get st 0
76if eq b 1
77n = get st 3
78end
79if eq b 2
80n = get st 4
81end
82r = long 0
83if eq b 0
84r = write int fd bo 0 n
85end
86if eq b 1
87r = write int fd ex 0 n
88end
89if eq b 2
90r = write int fd fb 0 n
91end
92if ne r long n
93die k
94end
95r = fclose int fd
96put st 5 add get st 5 1
97end
98
99note rfb: fb = the file DIR + word k of t.
100fn rfb d view uchar * t view uchar * k ulong to void
101o = pfx d
102n = word t k o
103fd = fopen pth 0 n 0 0
104if lt fd 0
105die n
106end
107r = read int fd fb 0 65536
108if lt r 0
109die n
110end
111put st 4 ulong r
112r = fclose int fd
113end
114
115note ---- bit writer (RFC 1951 3.1.1: bytes filled from the least significant bit) ----
116fn breset to void
117put st 0 0
118put st 1 0
119put st 2 0
120end
121
122fn bput v ulong n ulong to void
123b = get st 1
124c = get st 2
125for i ulong n bound 64
126b = or b shl and shr v i 1 c
127c = add c 1
128if eq c 8
129put bo get st 0 uchar b
130put st 0 add get st 0 1
131b = 0
132c = 0
133end
134end
135put st 1 b
136put st 2 c
137end
138
139fn bcode v ulong n ulong to void
140for i ulong n bound 32
141bput and shr v sub sub n 1 i 1 1
142end
143end
144
145fn bflush to void
146if gt get st 2 0
147put bo get st 0 uchar get st 1
148put st 0 add get st 0 1
149put st 1 0
150put st 2 0
151end
152end
153
154fn hexv c uchar to ulong
155if le c 57
156ret ulong sub c 48
157end
158ret ulong sub c 87
159end
160
161note The stream table. The common dynamic header (lines d-*, except d-repfirst, d-manylen, d-manydist,
162note d-clinc, d-clover): HLIT 0 (257 codes), HCLEN 14, so 18 code-length code lengths in the order
163note 16 17 18 0 8 7 9 6 10 5 11 4 12 3 13 2 14 1; symbols 0, 1, 2 and 18 get length 2, so their codes
164note are 00, 01, 10 and 11. HDIST sits between (00000 one code, 10000 two, 01000 three).
165note "11 1111111" is 138 zeros, "11 0101011" 117, "11 0001011" 115, "11 0011011" 119.
166note d-repfirst: HCLEN 0, lengths 1 2 2 0 for 16 17 18 0 (codes 0, 10, 11), and the first symbol is 16.
167note Fixed blocks: "1 10" is BFINAL 1, BTYPE 01; 'A' is 01110001, length 3 is 0000001, end of block
168note 0000000, distance codes are 5 bits.
169text cases "d-litinc.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 10 11 1111111 11 0101011 01 01\nd-litover.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 01 01 01 11 1111111 11 0001011 01 01\nd-distover.raw b 1 01 00000 01000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 01 11 1111111 11 0101011 01 01 01 01\nd-distinc.raw b 1 01 00000 10000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 01 11 1111111 11 0101011 01 10 00\nd-noeob.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 11 1111111 11 0011011 01\nd-reppast.raw b 1 01 00000 00000 0111 000 000 010 010 000 000 000 000 000 000 000 000 000 000 000 010 000 010 11 1111111 11 1111111\nd-repfirst.raw b 1 01 00000 00000 0000 100 010 010 000 0 00\nd-manylen.raw b 1 01 01111 00000 0000 0000000000\nd-manydist.raw b 1 01 00000 01111 0000 0000000000\nd-clinc.raw b 1 01 00000 00000 0000 100 000 000 000\nd-clover.raw b 1 01 00000 00000 0000 100 100 100 000\nf-lit286.raw b 1 10 11000110\nf-dist30.raw b 1 10 01110001 0000001 11110\nf-far.raw b 1 10 01110001 0000001 00001 0000000\nf-trunc.raw b 1 10 01110001\nok-fixed.raw b 1 10 01110001 0000001 00000 0000000\nb-type3.raw b 1 11\ns-nlen.raw h 01 05 00 fa fe 68 65 6c 6c 6f\ns-trunc.raw h 01 05 00 fa ff 68 65\nok-stored.raw h 00 05 00 fa ff 68 65 6c 6c 6f 01 00 00 ff ff\ne-empty.raw h\nzl-fcheck.zlib h 78 9d 03 00 00 00 00 01\nzl-cinfo.zlib h 88 1c 03 00 00 00 00 01\nzl-method.zlib h 77 09 03 00 00 00 00 01\nzl-dict.zlib h 78 3f 00 00 00 01 03 00 00 00 00 01\nok-empty.zlib h 78 01 01 00 00 ff ff 00 00 00 01\n"
170
171note streams: every line of `cases` into its file.
172fn streams to void
173o = pfx tdir
174p = o
175i = ulong 0
176n = len cases
177while lt i n bound 65536
178c = get cases i
179if eq c 32
180i = add i 1
181k = get cases i
182i = add i 1
183breset
184v = ulong 0
185h = ulong 0
186while lt i n bound 4096
187d = get cases i
188if eq d 10
189break
190end
191if eq k 98
192if eq d 48
193bput 0 1
194end
195if eq d 49
196bput 1 1
197end
198else
199if ne d 32
200v = or shl v 4 hexv d
201h = add h 1
202if eq h 2
203bput v 8
204v = 0
205h = 0
206end
207end
208end
209i = add i 1
210end
211bflush
212wbuf 0 p
213i = add i 1
214p = o
215continue
216end
217put pth p c
218p = add p 1
219i = add i 1
220end
221end
222
223note ---- the all-codes stream ----
224fn flit s ulong to void
225if lt s 144
226bcode add s 48 8
227ret
228end
229if lt s 256
230bcode add s 256 9
231ret
232end
233if lt s 280
234bcode sub s 256 7
235ret
236end
237bcode sub s 88 8
238end
239
240fn tables to void
241b = ushort 3
242for i ulong 28
243e = ulong 0
244if ge i 8
245e = sub div i 4 1
246end
247put lb i b
248put lx i uchar e
249b = add b ushort shl ulong 1 e
250end
251put lb 28 258
252put lx 28 0
253d = ushort 1
254for i ulong 30
255e = ulong 0
256if ge i 4
257e = sub div i 2 1
258end
259put db i d
260put dx i uchar e
261d = add d ushort shl ulong 1 e
262end
263end
264
265note allcodes: one fixed block: 33000 pseudo-random literals (8- and 9-bit codes), then 59 matches
266note that use every length code 257..285 and every distance code 0..29, first with zero extra bits,
267note then with the largest extra value (code 284 with 30, so the length stays inside 227..257), so the
268note longest distance 32768 is used. ex gets the expected output, computed byte by byte.
269note plen, pdist: the k-th match's length and distance codes with their extra bits (zero for k < 29,
270note the largest value from k = 29 on); each returns its length or distance.
271fn plen k ulong to ulong
272lc = rem k 29
273el = ulong get lx lc
274a = ulong 0
275if ge k 29
276a = sub shl ulong 1 el 1
277if eq lc 27
278a = 30
279end
280end
281flit add 257 lc
282bput a el
283ret add ulong get lb lc a
284end
285
286fn pdist k ulong to ulong
287dc = rem k 30
288ed = ulong get dx dc
289b = ulong 0
290if ge k 29
291b = sub shl ulong 1 ed 1
292end
293bcode dc 5
294bput b ed
295ret add ulong get db dc b
296end
297
298fn allcodes to void
299breset
300bput 1 1
301bput 1 2
302x = ulong 12345
303for i ulong 33000
304x = and add mul x 1103515245 12345 2147483647
305v = and shr x 16 255
306flit v
307put ex i uchar v
308end
309o = ulong 33000
310for k ulong 59
311n = plen k
312d = pdist k
313for j ulong n bound 258
314put ex o get ex sub o d
315o = add o 1
316end
317end
318flit 256
319bflush
320put st 3 o
321end
322
323note ---- zip patching (fb holds one zip without a comment; offsets per APPNOTE) ----
324fn le32f o ulong to ulong
325a = or ulong get fb o shl ulong get fb add o 1 8
326b = or shl ulong get fb add o 2 16 shl ulong get fb add o 3 24
327ret or a b
328end
329
330fn put32f o ulong v ulong to void
331put fb o uchar and v 255
332put fb add o 1 uchar and shr v 8 255
333put fb add o 2 uchar and shr v 16 255
334put fb add o 3 uchar and shr v 24 255
335end
336
337fn eocd to ulong
338ret sub get st 4 22
339end
340
341fn cdat to ulong
342ret le32f add eocd 16
343end
344
345note setnm: word k of nnames as the first entry's name (local at 30, central at cd + 46).
346text nnames "../evil.txt /a/evil.txt aa\\evil.txt ./aevil.txt"
347fn setnm k ulong to void
348n = word nnames k 0
349c = cdat
350for i ulong n bound 64
351put fb add 30 i get pth i
352put fb add add c 46 i get pth i
353end
354end
355
356note both: a 32-bit field at local offset lo and central offset co set to v.
357fn both lo ulong co ulong v ulong to void
358put32f lo v
359put32f add cdat co v
360end
361
362text zsrc "base.zip st.zip dd.zip"
363text znames "z-dotdot.zip z-abs.zip z-bslash.zip z-dotpart.zip z-crypt.zip z-method.zip z-crc.zip z-local.zip z-big.zip z-short.zip z-long.zip z-disk.zip z-cdoff.zip z-lsig.zip z-count.zip z-comment.zip z-ssize.zip z-desc.zip z-lname.zip z-lmeth.zip z-lflag.zip z-ldata.zip z-desccs.zip z-count0.zip z-cdend.zip z-cs64.zip z-overlap.zip"
364
365fn wz k ulong to void
366o = pfx tdir
367n = word znames k o
368wbuf 2 n
369end
370
371note overlap: base.zip with its one central entry written twice; both point at the same local
372note header and data, the overlap of D. Fifield's zip bomb. The end record moves up by the entry.
373fn overlap to void
374c = cdat
375e = eocd
376l = sub e c
377for i ulong 22
378put fb add add e l sub 21 i get fb add e sub 21 i
379end
380for i ulong l bound 4096
381put fb add e i get fb add c i
382end
383put st 4 add get st 4 l
384e = eocd
385put fb add e 8 2
386put fb add e 10 2
387put32f add e 12 mul l 2
388end
389
390fn zips to void
391for k ulong 4
392rfb tzdir zsrc 0
393setnm k
394wz k
395end
396rfb tzdir zsrc 0
397c = cdat
398put fb 6 or get fb 6 1
399put fb add c 8 or get fb add c 8 1
400wz 4
401rfb tzdir zsrc 0
402c = cdat
403put fb 8 12
404put fb add c 10 12
405wz 5
406rfb tzdir zsrc 0
407both 14 16 xor le32f 14 1
408wz 6
409rfb tzdir zsrc 0
410put32f 22 add le32f 22 1
411wz 7
412rfb tzdir zsrc 0
413both 22 24 2147483647
414wz 8
415rfb tzdir zsrc 0
416both 22 24 sub le32f 22 1
417wz 9
418rfb tzdir zsrc 0
419both 22 24 add le32f 22 1
420wz 10
421rfb tzdir zsrc 0
422put fb add eocd 4 1
423wz 11
424rfb tzdir zsrc 0
425e = eocd
426put32f add e 16 add le32f add e 16 1
427wz 12
428rfb tzdir zsrc 0
429put fb 0 81
430wz 13
431rfb tzdir zsrc 0
432e = eocd
433put fb add e 8 2
434put fb add e 10 2
435wz 14
436rfb tzdir zsrc 0
437e = eocd
438put fb add e 20 1
439wz 15
440rfb tzdir zsrc 1
441both 18 20 add le32f 18 1
442wz 16
443rfb tzdir zsrc 2
444c = cdat
445nl = or ulong get fb 26 shl ulong get fb 27 8
446xl = or ulong get fb 28 shl ulong get fb 29 8
447p = add add 30 add nl xl le32f add c 20
448if eq le32f p 134695760
449p = add p 4
450end
451put32f p xor le32f p 1
452wz 17
453rfb tzdir zsrc 0
454put fb 30 98
455wz 18
456rfb tzdir zsrc 0
457put fb 8 0
458wz 19
459rfb tzdir zsrc 0
460put fb 6 or get fb 6 8
461wz 20
462rfb tzdir zsrc 0
463put fb 28 add get fb 28 1
464wz 21
465rfb tzdir zsrc 2
466c = cdat
467nl = or ulong get fb 26 shl ulong get fb 27 8
468xl = or ulong get fb 28 shl ulong get fb 29 8
469p = add add 30 add nl xl le32f add c 20
470if eq le32f p 134695760
471p = add p 4
472end
473put32f add p 4 add le32f add p 4 1
474wz 22
475rfb tzdir zsrc 0
476e = eocd
477put fb add e 8 0
478put fb add e 10 0
479wz 23
480rfb tzdir zsrc 0
481c = cdat
482put fb add c 32 22
483e = eocd
484put32f add e 12 add le32f add e 12 22
485wz 24
486rfb tzdir zsrc 0
487c = cdat
488put32f add c 20 4294967295
489wz 25
490rfb tzdir zsrc 0
491overlap
492wz 26
493end
494
495note ---- zlib fixtures from corpus/zlib/text.om.l6 ----
496text zlsrc "text.om.l6"
497text zlnames "zl-adler.zlib zl-trunc.zlib"
498
499fn zlibs to void
500rfb tldir zlsrc 0
501n = get st 4
502put fb sub n 1 xor get fb sub n 1 1
503o = pfx tdir
504k = word zlnames 0 o
505wbuf 2 k
506rfb tldir zlsrc 0
507put st 4 sub get st 4 2
508o = pfx tdir
509k = word zlnames 1 o
510wbuf 2 k
511end
512
513text acnames "ok-allcodes.raw ok-allcodes.out"
514
515fn main to int
516tables
517streams
518allcodes
519o = pfx tdir
520k = word acnames 0 o
521wbuf 0 k
522o = pfx tdir
523k = word acnames 1 o
524wbuf 1 k
525zlibs
526zips
527r = write 1 tdone 0 len tdone
528b = new uchar 24 0
529v = get st 5
530for i ulong 20
531put b sub 19 i uchar add rem v 10 48
532v = div v 10
533end
534k = ulong 0
535while lt k 19 bound 20
536if ne get b k 48
537break
538end
539k = add k 1
540end
541r = write 1 b k sub 20 k
542r = write 1 tnl 0 1
543ret 0
544end