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