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-zip/blob/main/src/io.om # omega-zip omega-zip: ZIP archive reader in seeded Omega (store and deflate, CRC-32, fail-closed refusals) 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-zip / src / io.om` 257 lines ยท 5566 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 note woutfd: win[0 .. n) to the output descriptor ofd (nothing when ofd < 0). wflush calls 149 note "w = wout n", which the program defines as "fn wout n ulong to ulong" (it returns n): ozip's 150 note src/main.om passes the window here; another program may consume it. The call is an assignment so 151 note that a program without wout is refused by omc (error 3); a bare "wout n" would compile as "wout = n". 152 fn woutfd n ulong to void 153 if lt get ofd 0 0 154 ret 155 end 156 o = ulong 0 157 while lt o n bound `1000000` 158 r = write int get ofd 0 win o sub n o 159 if le r 0 160 fail 4 twrite 161 ret 162 end 163 o = add o ulong r 164 end 165 end 166 167 fn crcwin n ulong to void 168 c = get crcv 0 169 i = ulong 0 170 while lt i n bound 32768 171 c = xor get crctab and xor c uint get win i 255 shr c 8 172 i = add i 1 173 end 174 put crcv 0 c 175 end 176 177 fn adwin n ulong to void 178 a = get ada 0 179 b = get adb 0 180 i = ulong 0 181 while lt i n bound 32768 182 a = add a ulong get win i 183 b = add b a 184 i = add i 1 185 end 186 put ada 0 rem a 65521 187 put adb 0 rem b 65521 188 end 189 190 note wflush: checksum and write win[0 .. n), then the window is empty again. 191 fn wflush n ulong to void 192 k = get ock 0 193 if eq and k 1 1 194 crcwin n 195 end 196 if eq and k 2 2 197 adwin n 198 end 199 w = wout n 200 put wpos 0 0 201 end 202 203 fn ofail to void 204 if eq get omode 0 1 205 fail 53 tesize 206 ret 207 end 208 fail 22 tbig 209 end 210 211 fn outb v ulong to void 212 t = add get otot 0 1 213 if gt t get olim 0 214 ofail 215 ret 216 end 217 put otot 0 t 218 p = get wpos 0 219 put win p uchar v 220 p = add p 1 221 if eq p WSIZE 222 wflush WSIZE 223 ret 224 end 225 put wpos 0 p 226 end 227 228 fn osink fd long lim ulong mode ulong ck ulong to void 229 put ofd 0 fd 230 put olim 0 lim 231 put omode 0 mode 232 put ock 0 ck 233 put wpos 0 0 234 put otot 0 0 235 put crcv 0 `4294967295` 236 put ada 0 1 237 put adb 0 0 238 end 239 240 note ---- number text ---- 241 fn sayhex v ulong w ulong to void 242 b = new uchar 24 0 243 e = tx_fhex v w b 0 244 r = write 1 b 0 ulong e 245 end 246 247 fn saydec v ulong w ulong to void 248 b = new uchar 24 0 249 k = tx_ndig v 10 250 o = ulong 0 251 while lt add o k w bound 24 252 put b o 32 253 o = add o 1 254 end 255 e = tx_fput v 10 k b o 256 r = write 1 b 0 ulong e 257 end