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

257 lines · 5566 bytes Raw Blame History
1note ======== src/io.om: storage, exit codes, messages, input source, output sink ========
2note Every buffer is a fixed global; no array is longer than 32768 elements and all globals together
3note stay below 65536 bytes (profile rule B3 sizes). Nothing ever holds a whole file.
4note Exit codes (README "Exit codes" is the same table): 0 done, 1 usage, 2 cannot open input,
5note 3 read error, 4 write error, 5 cannot create output, 10..22 DEFLATE, 30..32 zlib, 40..53 zip.
6
7const WSIZE ulong 32768
8const WMASK ulong 32767
9const ISIZE ulong 16384
10const NAMEMAX ulong 4095
11const OUTMAX ulong 4294967295
12const ENTMAX ulong 1073741824
13
14note win: the 32 KiB sliding window, which is also the output staging buffer (flushed when full).
15global win uchar 32768
16note ibuf: compressed or stored input, refilled 16 KiB at a time (read on stdin, pread on a file range).
17global ibuf uchar 16384
18global crctab uint 256
19note Huffman tables: hcnt[16*t + l] codes of length l, hsym[288*t + k] symbols in canonical order;
20note t = 0 literal/length (or the code-length code), t = 1 distance. hlen holds code lengths:
21note 0..287 literal/length, then distance lengths, 320..338 the code-length code.
22global hcnt ushort 32
23global hsym ushort 320
24global hoff ushort 16
25global hlen uchar 340
26global ord uchar 19
27global lbase ushort 29
28global lext uchar 29
29global dbase ushort 30
30global dext uchar 30
31note htyp: 1 when the fixed tables are built in hcnt/hsym, else 0.
32global htyp ulong 1
33
34note input source: imode 0 = read(ifd) until end of file, 1 = pread(ifd) of irem bytes from ioff.
35global ifd ulong 1
36global imode ulong 1
37global ioff ulong 1
38global irem ulong 1
39global ipos ulong 1
40global iend ulong 1
41global bitb ulong 1
42global bitn ulong 1
43
44note output sink: ofd < 0 discards; wpos = bytes waiting in win; otot = bytes out in this stream;
45note olim = most bytes allowed; omode 1 = a zip entry (limit = stated size, code 53) else code 22;
46note ock bit 0 = CRC-32, bit 1 = Adler-32.
47global ofd long 1
48global wpos ulong 1
49global otot ulong 1
50global olim ulong 1
51global omode ulong 1
52global ock ulong 1
53global crcv uint 1
54global ada ulong 1
55global adb ulong 1
56
57note cleanup: when ocl is 1, fail unlinks the partly written file opath[0 .. opl) before it exits.
58global ocl ulong 1
59global opath uchar 4096
60global opl ulong 1
61
62text tpref "ozip: "
63text tnl "\n"
64
65fn say t view uchar * to void
66r = write 1 t 0 len t
67end
68
69fn warn t view uchar * to void
70r = write 2 t 0 len t
71end
72
73note fail: one stderr line "ozip: MESSAGE", remove a partly written output file, exit with code c.
74fn fail c int m view uchar * to void
75if eq get ocl 0 1
76put ocl 0 0
77r = unlink opath 0 get opl 0
78end
79warn tpref
80warn m
81warn tnl
82exit c
83end
84
85text tread "read error"
86text twrite "write error"
87text tshort "input ends early"
88text tbig "output above the bound"
89text tesize "entry is longer than its stated size"
90
91note ---- input ----
92fn infill to ulong
93if eq get imode 0 0
94r = read int get ifd 0 ibuf 0 ISIZE
95if lt r 0
96fail 3 tread
97ret 0
98end
99put ipos 0 0
100put iend 0 ulong r
101ret ulong r
102end
103n = get irem 0
104if gt n ISIZE
105n = ISIZE
106end
107if eq n 0
108ret 0
109end
110r = pread int get ifd 0 ibuf 0 n long get ioff 0
111if lt r 0
112fail 3 tread
113ret 0
114end
115put ioff 0 add get ioff 0 ulong r
116put irem 0 sub get irem 0 ulong r
117put ipos 0 0
118put iend 0 ulong r
119ret ulong r
120end
121
122fn inbyte to ulong
123p = get ipos 0
124if ge p get iend 0
125n = infill
126if eq n 0
127fail 10 tshort
128ret 0
129end
130p = 0
131end
132put ipos 0 add p 1
133ret ulong get ibuf p
134end
135
136fn insrc fd ulong mode ulong off ulong n ulong to void
137put ifd 0 fd
138put imode 0 mode
139put ioff 0 off
140put irem 0 n
141put ipos 0 0
142put iend 0 0
143put bitb 0 0
144put bitn 0 0
145end
146
147note ---- output ----
148note woutfd: win[0 .. n) to the output descriptor ofd (nothing when ofd < 0). wflush calls
149note "w = wout n", which the program defines as "fn wout n ulong to ulong" (it returns n): ozip's
150note src/main.om passes the window here; another program may consume it. The call is an assignment so
151note that a program without wout is refused by omc (error 3); a bare "wout n" would compile as "wout = n".
152fn woutfd n ulong to void
153if lt get ofd 0 0
154ret
155end
156o = ulong 0
157while lt o n bound 1000000
158r = write int get ofd 0 win o sub n o
159if le r 0
160fail 4 twrite
161ret
162end
163o = add o ulong r
164end
165end
166
167fn crcwin n ulong to void
168c = get crcv 0
169i = ulong 0
170while lt i n bound 32768
171c = xor get crctab and xor c uint get win i 255 shr c 8
172i = add i 1
173end
174put crcv 0 c
175end
176
177fn adwin n ulong to void
178a = get ada 0
179b = get adb 0
180i = ulong 0
181while lt i n bound 32768
182a = add a ulong get win i
183b = add b a
184i = add i 1
185end
186put ada 0 rem a 65521
187put adb 0 rem b 65521
188end
189
190note wflush: checksum and write win[0 .. n), then the window is empty again.
191fn wflush n ulong to void
192k = get ock 0
193if eq and k 1 1
194crcwin n
195end
196if eq and k 2 2
197adwin n
198end
199w = wout n
200put wpos 0 0
201end
202
203fn ofail to void
204if eq get omode 0 1
205fail 53 tesize
206ret
207end
208fail 22 tbig
209end
210
211fn outb v ulong to void
212t = add get otot 0 1
213if gt t get olim 0
214ofail
215ret
216end
217put otot 0 t
218p = get wpos 0
219put win p uchar v
220p = add p 1
221if eq p WSIZE
222wflush WSIZE
223ret
224end
225put wpos 0 p
226end
227
228fn osink fd long lim ulong mode ulong ck ulong to void
229put ofd 0 fd
230put olim 0 lim
231put omode 0 mode
232put ock 0 ck
233put wpos 0 0
234put otot 0 0
235put crcv 0 4294967295
236put ada 0 1
237put adb 0 0
238end
239
240note ---- number text ----
241fn sayhex v ulong w ulong to void
242b = new uchar 24 0
243e = tx_fhex v w b 0
244r = write 1 b 0 ulong e
245end
246
247fn saydec v ulong w ulong to void
248b = new uchar 24 0
249k = tx_ndig v 10
250o = ulong 0
251while lt add o k w bound 24
252put b o 32
253o = add o 1
254end
255e = tx_fput v 10 k b o
256r = write 1 b 0 ulong e
257end