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

167 lines · 2564 bytes Raw Blame History
1note ======== src/main.om: the ozip command line ========
2
3text tusage "usage: ozip crc32|inflate|zlib (stdin to stdout); ozip l|v|t ZIP; ozip p ZIP [NAME]; ozip x ZIP DIR [NAME]"
4text tbadarg "bad arguments"
5text topen "cannot open the zip file"
6text tcrc32 "crc32"
7text tinfl "inflate"
8text tzlib "zlib"
9text tl "l"
10text tv "v"
11text tt "t"
12text tp "p"
13text tx "x"
14
15note wout: the output sink that io.om's wflush calls with win[0 .. n). ozip writes the window to the
16note output descriptor; a program that reuses io.om, inflate.om and unzip.om defines its own wout.
17note It returns n (wflush assigns the result, so a missing wout is a compile error, not silence).
18fn wout n ulong to ulong
19woutfd n
20ret n
21end
22
23fn usage to int
24warn tpref
25warn tusage
26warn tnl
27ret 1
28end
29
30note argnm: argument i into want (the entry name asked for).
31fn argnm i ulong to void
32k = argread i want 0 4096
33if lt k 0
34fail 1 tbadarg
35end
36if gt k long NAMEMAX
37fail 1 tbadarg
38end
39put wantn 0 ulong k
40put wantf 0 1
41end
42
43note argdir: argument i into opath[0 .. dirn), the output directory; a trailing "/" is dropped.
44fn argdir i ulong to void
45k = argread i opath 0 4096
46if le k 0
47fail 1 tbadarg
48end
49n = ulong k
50if gt n NAMEMAX
51fail 1 tbadarg
52end
53if gt n 1
54if eq get opath sub n 1 47
55n = sub n 1
56end
57end
58put dirn 0 n
59end
60
61note zipcmd: c 0 l, 1 v, 2 t, 3 p, 4 x; n arguments in all (the program name included).
62fn zipcmd c ulong n ulong to int
63zp = new uchar 4096 0
64k = argread 2 zp 0 4096
65if le k 0
66fail 1 tbadarg
67end
68if gt k long NAMEMAX
69fail 1 tbadarg
70end
71fd = fopen zp 0 ulong k 0 0
72if lt fd 0
73fail 2 topen
74end
75put zfd 0 ulong fd
76if lt c 3
77if ne n 3
78ret usage
79end
80end
81if eq c 3
82if gt n 4
83ret usage
84end
85end
86if eq c 4
87if lt n 4
88ret usage
89end
90if gt n 5
91ret usage
92end
93end
94zopen
95if eq c 0
96ret listing false part zp 0 ulong k
97end
98if eq c 1
99ret listing true part zp 0 ulong k
100end
101if eq c 2
102ret extract 2
103end
104if eq c 3
105if eq n 4
106argnm 3
107end
108ret extract 0
109end
110argdir 3
111if eq n 5
112argnm 4
113end
114ret extract 1
115end
116
117fn main to int
118crcinit
119tabinit
120n = ulong argcnt
121if lt n 2
122ret usage
123end
124a = new uchar 16 0
125k = argread 1 a 0 16
126if lt k 0
127ret usage
128end
129m = ulong k
130if tx_eq part a 0 m tcrc32
131if ne n 2
132ret usage
133end
134ret crcrun
135end
136if tx_eq part a 0 m tinfl
137if ne n 2
138ret usage
139end
140ret rawrun
141end
142if tx_eq part a 0 m tzlib
143if ne n 2
144ret usage
145end
146ret zlibrun
147end
148if lt n 3
149ret usage
150end
151if tx_eq part a 0 m tl
152ret zipcmd 0 n
153end
154if tx_eq part a 0 m tv
155ret zipcmd 1 n
156end
157if tx_eq part a 0 m tt
158ret zipcmd 2 n
159end
160if tx_eq part a 0 m tp
161ret zipcmd 3 n
162end
163if tx_eq part a 0 m tx
164ret zipcmd 4 n
165end
166ret usage
167end