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 / unzip.om

771 lines · 13684 bytes Raw Blame History
1note ======== src/unzip.om: zip reader (PKWARE APPNOTE.TXT 6.3.10, sections 4.3 and 4.4) ========
2note Reads the end of central directory record (4.3.16), walks the central directory (4.3.12),
3note checks each local header (4.3.7) and data descriptor (4.3.9) against it, and extracts methods
4note 0 (stored) and 8 (deflated) with the CRC-32 checked. ZIP64 (4.3.14, 4.3.15), encryption
5note (4.4.4 bits 0 and 6), split or spanned archives (4.3.16 disk numbers) are refused.
6note Listing modes print exactly what Info-ZIP UnZip 6.0 prints for `unzip -l` and `unzip -v`.
7
8text tnozip "no end of central directory record (not a zip file)"
9text tzip64 "ZIP64 archive: not yet supported"
10text tdisk "split or spanned archive: not supported"
11text tcrypt "encrypted entry: not supported"
12text tmeth "compression method not supported (only 0 stored and 8 deflated)"
13text tname "unsafe entry name (absolute, '..', '.', empty part, backslash or NUL)"
14text tlong "name or output path longer than 4095 bytes"
15text tlocal "local header disagrees with the central directory"
16text tcrc "CRC-32 mismatch"
17text tbound "entry larger than the size bound (1 GiB)"
18text tnone "no entry with that name"
19text tcdir "central directory is malformed"
20text tssize "stored entry has different compressed and uncompressed sizes"
21text tcreate "cannot create the output file (it may exist already)"
22text tmkdir "cannot create a directory"
23text tover "entries overlap (a possible zip bomb)"
24
25note hb: the header being read (central 46, local 30, end record 22, descriptor 16 bytes).
26global hb uchar 64
27note nm: the current central directory name; want: the name asked for on the command line.
28global nm uchar 4096
29global want uchar 4096
30global wantn ulong 1
31global wantf ulong 1
32global zfd ulong 1
33global zsize ulong 1
34global zeocd ulong 1
35global zclen ulong 1
36global cdoff ulong 1
37global cdsize ulong 1
38global cdn ulong 1
39global cdpos ulong 1
40global dirn ulong 1
41note the current central directory entry
42global eflag ulong 1
43global emeth ulong 1
44global etime ulong 1
45global edate ulong 1
46global ecrc ulong 1
47global ecs ulong 1
48global eus ulong 1
49global enl ulong 1
50global eoff ulong 1
51note zspan: the bytes the entries need at least (local header, name, data), summed over the archive.
52global zspan ulong 1
53
54fn le16 o ulong to ulong
55ret or ulong get hb o shl ulong get hb add o 1 8
56end
57
58fn le32 o ulong to ulong
59ret or le16 o shl le16 add o 2 16
60end
61
62note zread: hb[0 .. n) from file offset off; false when the file ends first.
63fn zread off ulong n ulong to bool
64if gt add off n get zsize 0
65ret false
66end
67r = pread int get zfd 0 hb 0 n long off
68if lt r 0
69fail 3 tread
70ret false
71end
72ret eq ulong r n
73end
74
75note ---- end of central directory ----
76note eocdat: true when a valid end record starts at p: signature, and the comment ends the file.
77fn eocdat p ulong to bool
78if not zread p 22
79ret false
80end
81if ne le32 0 101010256
82ret false
83end
84ret eq add add p 22 le16 20 get zsize 0
85end
86
87note findeocd: the last valid end record within the last 65557 bytes (22 + the longest comment).
88note The tail is scanned backwards in 16 KiB reads that overlap by 3 bytes.
89fn findeocd to ulong
90size = get zsize 0
91if lt size 22
92fail 40 tnozip
93ret 0
94end
95lo = ulong 0
96if gt size 65557
97lo = sub size 65557
98end
99ce = sub size 18
100while true bound 8
101cs = lo
102if gt sub ce lo ISIZE
103cs = sub ce ISIZE
104end
105r = pread int get zfd 0 ibuf 0 sub ce cs long cs
106if ne r long sub ce cs
107fail 3 tread
108ret 0
109end
110p = sub ce 4
111while ge p cs bound 16384
112q = sub p cs
113if eq get ibuf q 80
114if eq get ibuf add q 1 75
115if eq get ibuf add q 2 5
116if eq get ibuf add q 3 6
117if eocdat p
118ret p
119end
120end
121end
122end
123end
124if eq p 0
125break
126end
127p = sub p 1
128end
129if eq cs lo
130fail 40 tnozip
131ret 0
132end
133ce = add cs 3
134end
135fail 40 tnozip
136ret 0
137end
138
139fn zopen to void
140s = fseek int get zfd 0 0 2
141if lt s 0
142fail 3 tread
143end
144put zsize 0 ulong s
145p = findeocd
146if ge p 20
147if zread sub p 20 4
148if eq le32 0 117853008
149fail 41 tzip64
150end
151end
152end
153b = eocdat p
154put zeocd 0 p
155put zclen 0 le16 20
156if eq le16 10 65535
157fail 41 tzip64
158end
159if eq le16 8 65535
160fail 41 tzip64
161end
162if eq le32 12 4294967295
163fail 41 tzip64
164end
165if eq le32 16 4294967295
166fail 41 tzip64
167end
168if ne le16 4 0
169fail 42 tdisk
170end
171if ne le16 6 0
172fail 42 tdisk
173end
174if ne le16 8 le16 10
175fail 42 tdisk
176end
177put cdn 0 le16 10
178put cdsize 0 le32 12
179put cdoff 0 le32 16
180if gt add get cdoff 0 get cdsize 0 p
181fail 50 tcdir
182end
183end
184
185note ---- central directory ----
186note cdnext: the entry at cdpos into eflag .. eoff and nm; cdpos moves past it.
187fn cdnext to void
188pos = get cdpos 0
189e = add get cdoff 0 get cdsize 0
190if gt add pos 46 e
191fail 50 tcdir
192end
193if not zread pos 46
194fail 50 tcdir
195end
196if ne le32 0 33639248
197fail 50 tcdir
198end
199put eflag 0 le16 8
200put emeth 0 le16 10
201put etime 0 le16 12
202put edate 0 le16 14
203put ecrc 0 le32 16
204put ecs 0 le32 20
205put eus 0 le32 24
206n = le16 28
207put enl 0 n
208put eoff 0 le32 42
209t = add add add pos 46 n add le16 30 le16 32
210if gt t e
211fail 50 tcdir
212end
213if eq get ecs 0 4294967295
214fail 41 tzip64
215end
216if eq get eus 0 4294967295
217fail 41 tzip64
218end
219if eq get eoff 0 4294967295
220fail 41 tzip64
221end
222if eq le16 34 65535
223fail 41 tzip64
224end
225if ne le16 34 0
226fail 42 tdisk
227end
228if gt n NAMEMAX
229fail 45 tlong
230end
231r = pread int get zfd 0 nm 0 n long add pos 46
232if ne r long n
233fail 50 tcdir
234end
235put cdpos 0 t
236end
237
238note cdcheck: walk the whole directory once: every record well formed, the count and size agree.
239fn cdcheck to void
240put cdpos 0 get cdoff 0
241for i ulong get cdn 0 bound 65535
242cdnext
243end
244if ne get cdpos 0 add get cdoff 0 get cdsize 0
245fail 50 tcdir
246end
247put cdpos 0 get cdoff 0
248end
249
250fn wanted to bool
251if eq get wantf 0 0
252ret true
253end
254n = get enl 0
255if ne n get wantn 0
256ret false
257end
258for i ulong n bound 4096
259if ne get nm i get want i
260ret false
261end
262end
263ret true
264end
265
266note nameok: a relative name of nonempty parts, none "." or "..", no backslash and no NUL byte;
267note one trailing "/" marks a directory.
268fn partok s ulong e ulong to bool
269k = sub e s
270if eq k 0
271ret false
272end
273if eq k 1
274if eq get nm s 46
275ret false
276end
277end
278if eq k 2
279if eq get nm s 46
280if eq get nm add s 1 46
281ret false
282end
283end
284end
285ret true
286end
287
288fn nameok to bool
289n = get enl 0
290if eq n 0
291ret false
292end
293s = ulong 0
294for i ulong n bound 4096
295c = get nm i
296if eq c 0
297ret false
298end
299if eq c 92
300ret false
301end
302if eq c 47
303if not partok s i
304ret false
305end
306s = add i 1
307end
308end
309if lt s n
310ret partok s n
311end
312ret true
313end
314
315fn isdir to bool
316n = get enl 0
317if eq n 0
318ret false
319end
320ret eq get nm sub n 1 47
321end
322
323note ---- listing (unzip -l and unzip -v) ----
324text tarch "Archive: "
325text tlh1 " Length Date Time Name\n--------- ---------- ----- ----\n"
326text tlf1 "--------- -------\n"
327text tvh1 " Length Method Size Cmpr Date Time CRC-32 Name\n"
328text tvh2 "-------- ------ ------- ---- ---------- ----- -------- ----\n"
329text tvf1 "-------- ------- --- -------\n"
330text tsp1 " "
331text tsp2 " "
332text tsp3 " "
333text t10 " "
334text t21 " "
335text t28 " "
336text tdash "-"
337text tcolon ":"
338text tfile " file\n"
339text tfiles " files\n"
340text tstored "Stored "
341text tdefl "Defl:"
342text tunk "Unk:"
343text tmode "NXFS"
344text tpct "%"
345text t100 "100%"
346
347fn sayz v ulong k ulong to void
348b = new uchar 24 0
349e = tx_fput v 10 k b 0
350r = write 1 b 0 ulong e
351end
352
353fn saydate to void
354d = get edate 0
355t = get etime 0
356sayz add 1980 shr d 9 4
357say tdash
358sayz and shr d 5 15 2
359say tdash
360sayz and d 31 2
361say tsp1
362sayz shr t 11 2
363say tcolon
364sayz and shr t 5 63 2
365end
366
367note sayname: control bytes are shown as ^ and the byte plus 64, as UnZip does.
368fn sayname to void
369b = new uchar 8192 0
370o = ulong 0
371for i ulong get enl 0 bound 4096
372c = get nm i
373if lt c 32
374put b o 94
375put b add o 1 add c 64
376o = add o 2
377else
378put b o c
379o = add o 1
380end
381end
382r = write 1 b 0 o
383end
384
385note ratio: UnZip's compression factor in tenths of a percent, rounded half up.
386fn ratio uc ulong c ulong to long
387if eq uc 0
388ret 0
389end
390dn = uc
391mu = ulong 1000
392if gt uc 2000000
393dn = div uc 1000
394mu = 1
395end
396h = shr dn 1
397if ge uc c
398ret long div add mul mu sub uc c h dn
399end
400ret neg long div add mul mu sub c uc h dn
401end
402
403note saypct: the 4-column percentage: "100%", else a sign or space, the percent and "%".
404fn saypct uc ulong c ulong to void
405r = ratio uc c
406sg = false
407if lt r 0
408sg = true
409r = sub 0 r
410end
411f = ulong div add r 5 10
412if eq f 100
413say t100
414ret
415end
416k = add tx_ndig f 10 2
417while lt k 4 bound 4
418say tsp1
419k = add k 1
420end
421if sg
422say tdash
423else
424say tsp1
425end
426saydec f 1
427say tpct
428end
429
430fn saymeth to void
431m = get emeth 0
432if eq m 0
433say tstored
434ret
435end
436if eq m 8
437say tdefl
438r = write 1 tmode and shr get eflag 0 1 3 1
439say tsp1
440ret
441end
442say tunk
443sayz m 3
444end
445
446note saycomm: the archive comment and a newline, as UnZip prints it: a CR before a LF is dropped.
447fn saycomm to void
448n = get zclen 0
449if eq n 0
450ret
451end
452insrc get zfd 0 1 add get zeocd 0 22 n
453cr = false
454for i ulong n bound 65535
455c = inbyte
456if cr
457if ne c 10
458put win 0 13
459r = write 1 win 0 1
460end
461end
462cr = eq c 13
463if not cr
464put win 0 uchar c
465r = write 1 win 0 1
466end
467end
468if cr
469put win 0 13
470r = write 1 win 0 1
471end
472say tnl
473end
474
475fn listing v bool ap view uchar * to int
476cdcheck
477say tarch
478say ap
479say tnl
480saycomm
481if v
482say tvh1
483say tvh2
484else
485say tlh1
486end
487tu = ulong 0
488tc = ulong 0
489for i ulong get cdn 0 bound 65535
490cdnext
491u = get eus 0
492c = get ecs 0
493if eq and get eflag 0 1 1
494if ge c 12
495c = sub c 12
496end
497end
498tu = add tu u
499tc = add tc c
500if v
501saydec u 8
502say tsp2
503saymeth
504saydec c 8
505say tsp1
506saypct u c
507say tsp1
508saydate
509say tsp1
510sayhex get ecrc 0 8
511say tsp2
512else
513saydec u 9
514say tsp2
515saydate
516say tsp3
517end
518sayname
519say tnl
520end
521if v
522say tvf1
523saydec tu 8
524say t10
525saydec tc 7
526say tsp1
527saypct tu tc
528say t28
529saydec get cdn 0 1
530else
531say tlf1
532saydec tu 9
533say t21
534saydec get cdn 0 1
535end
536if eq get cdn 0 1
537say tfile
538else
539say tfiles
540end
541ret 0
542end
543
544note ---- extraction ----
545note precheck: the central directory checks, for every selected entry, before any output.
546fn precheck mode ulong to void
547f = get eflag 0
548if ne and f 65 0
549fail 43 tcrypt
550end
551m = get emeth 0
552if ne m 0
553if ne m 8
554fail 44 tmeth
555end
556end
557if gt get eus 0 ENTMAX
558fail 48 tbound
559end
560if eq m 0
561if ne get ecs 0 get eus 0
562fail 52 tssize
563end
564end
565if eq mode 1
566if not nameok
567fail 45 tname
568end
569if gt add add get dirn 0 1 get enl 0 NAMEMAX
570fail 45 tlong
571end
572end
573end
574
575note localchk: the local header at eoff agrees with the central entry; the data offset.
576fn localchk to ulong
577o = get eoff 0
578if not zread o 30
579fail 46 tlocal
580end
581if ne le32 0 67324752
582fail 46 tlocal
583end
584if ne le16 8 get emeth 0
585fail 46 tlocal
586end
587if ne and le16 6 9 and get eflag 0 9
588fail 46 tlocal
589end
590n = le16 26
591if ne n get enl 0
592fail 46 tlocal
593end
594if eq and get eflag 0 8 0
595if ne le32 14 get ecrc 0
596fail 46 tlocal
597end
598if ne le32 18 get ecs 0
599fail 46 tlocal
600end
601if ne le32 22 get eus 0
602fail 46 tlocal
603end
604end
605d = add add o 30 add n le16 28
606if gt add d get ecs 0 get cdoff 0
607fail 50 tcdir
608end
609r = pread int get zfd 0 ibuf 0 n long add o 30
610if ne r long n
611fail 46 tlocal
612end
613for i ulong n bound 4096
614if ne get ibuf i get nm i
615fail 46 tlocal
616end
617end
618ret d
619end
620
621note desc: with flag bit 3 the sizes and CRC follow the data (4.3.9), with or without the
622note optional signature 0x08074b50; they must equal the central directory's.
623fn desc d ulong to void
624if eq and get eflag 0 8 0
625ret
626end
627p = add d get ecs 0
628k = ulong 0
629if zread p 4
630if eq le32 0 134695760
631k = 4
632end
633end
634if not zread add p k 12
635fail 46 tlocal
636end
637if ne le32 0 get ecrc 0
638fail 46 tlocal
639end
640if ne le32 4 get ecs 0
641fail 46 tlocal
642end
643if ne le32 8 get eus 0
644fail 46 tlocal
645end
646end
647
648note entout: one entry's data into the sink set by the caller; size and CRC checked.
649fn entout to void
650d = localchk
651insrc get zfd 0 1 d get ecs 0
652if eq get emeth 0 0
653for i ulong get eus 0 bound ENTMAX
654outb inbyte
655end
656wflush get wpos 0
657else
658inflate
659end
660if ne get otot 0 get eus 0
661fail 53 tesize
662end
663if ne xor get crcv 0 4294967295 uint get ecrc 0
664fail 47 tcrc
665end
666desc d
667end
668
669note outpath: opath = DIR "/" name (the DIR part is already in opath[0 .. dirn)).
670fn outpath to void
671b = get dirn 0
672put opath b 47
673for i ulong get enl 0 bound 4096
674put opath add add b 1 i get nm i
675end
676put opl 0 add add b 1 get enl 0
677end
678
679note mkparents: every directory on the way to the entry (each part ending in /), existing ones
680note accepted. A directory entry (trailing /) creates the entry itself the same way.
681fn mkparents to void
682b = add get dirn 0 1
683n = get enl 0
684for i ulong n bound 4096
685if eq get nm i 47
686r = makedir opath 0 add b i 493
687if lt r 0
688if ne r -17
689fail 5 tmkdir
690end
691end
692end
693end
694end
695
696fn extone mode ulong to void
697if eq mode 0
698if isdir
699ret
700end
701osink 1 get eus 0 1 1
702entout
703ret
704end
705if eq mode 2
706osink -1 get eus 0 1 1
707entout
708ret
709end
710outpath
711if isdir
712mkparents
713osink -1 get eus 0 1 1
714entout
715ret
716end
717mkparents
718fd = fopen opath 0 get opl 0 32961 420
719if lt fd 0
720fail 5 tcreate
721end
722put ocl 0 1
723osink fd get eus 0 1 1
724entout
725r = fclose int fd
726put ocl 0 0
727if lt r 0
728fail 4 twrite
729end
730end
731
732note spanadd: the current entry's local header, name and data added to zspan.
733fn spanadd to void
734s = add get zspan 0 add 30 get enl 0
735put zspan 0 add s get ecs 0
736end
737
738note extract: mode 0 = to stdout (unzip -p), 1 = into the directory opath[0 .. dirn), 2 = test.
739note Entries that do not overlap need at least zspan bytes before the central directory; more means
740note two entries share data (D. Fifield's overlapping zip bomb), which UnZip also refuses: code 51.
741fn extract mode ulong to int
742cdcheck
743k = ulong 0
744put zspan 0 0
745for i ulong get cdn 0 bound 65535
746cdnext
747spanadd
748if wanted
749precheck mode
750k = add k 1
751end
752end
753if eq k 0
754if eq get wantf 0 1
755fail 49 tnone
756end
757end
758if gt get zspan 0 get cdoff 0
759fail 51 tover
760end
761put cdpos 0 get cdoff 0
762for i ulong get cdn 0 bound 65535
763cdnext
764if wanted
765p = get cdpos 0
766extone mode
767put cdpos 0 p
768end
769end
770ret 0
771end