interplane.Get Omega
Read in your language

omega-pdf

Omega PDF: read and write PDF files in Omega

Brought by drakestapleton

branch main Branches Tags History

omega-pdf / vendor / omega-zip / src / io.om

253 lines · 5164 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 ----
148fn woutfd n ulong to void
149if lt get ofd 0 0
150ret
151end
152o = ulong 0
153while lt o n bound 1000000
154r = write int get ofd 0 win o sub n o
155if le r 0
156fail 4 twrite
157ret
158end
159o = add o ulong r
160end
161end
162
163fn crcwin n ulong to void
164c = get crcv 0
165i = ulong 0
166while lt i n bound 32768
167c = xor get crctab and xor c uint get win i 255 shr c 8
168i = add i 1
169end
170put crcv 0 c
171end
172
173fn adwin n ulong to void
174a = get ada 0
175b = get adb 0
176i = ulong 0
177while lt i n bound 32768
178a = add a ulong get win i
179b = add b a
180i = add i 1
181end
182put ada 0 rem a 65521
183put adb 0 rem b 65521
184end
185
186note wflush: checksum and write win[0 .. n), then the window is empty again.
187fn wflush n ulong to void
188k = get ock 0
189if eq and k 1 1
190crcwin n
191end
192if eq and k 2 2
193adwin n
194end
195wout n
196put wpos 0 0
197end
198
199fn ofail to void
200if eq get omode 0 1
201fail 53 tesize
202ret
203end
204fail 22 tbig
205end
206
207fn outb v ulong to void
208t = add get otot 0 1
209if gt t get olim 0
210ofail
211ret
212end
213put otot 0 t
214p = get wpos 0
215put win p uchar v
216p = add p 1
217if eq p WSIZE
218wflush WSIZE
219ret
220end
221put wpos 0 p
222end
223
224fn osink fd long lim ulong mode ulong ck ulong to void
225put ofd 0 fd
226put olim 0 lim
227put omode 0 mode
228put ock 0 ck
229put wpos 0 0
230put otot 0 0
231put crcv 0 4294967295
232put ada 0 1
233put adb 0 0
234end
235
236note ---- number text ----
237fn sayhex v ulong w ulong to void
238b = new uchar 24 0
239e = tx_fhex v w b 0
240r = write 1 b 0 ulong e
241end
242
243fn saydec v ulong w ulong to void
244b = new uchar 24 0
245k = tx_ndig v 10
246o = ulong 0
247while lt add o k w bound 24
248put b o 32
249o = add o 1
250end
251e = tx_fput v 10 k b o
252r = write 1 b 0 ulong e
253end