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/inflate.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 / inflate.om` 373 lines ยท 6531 bytes Raw Blame History 1 note ======== src/inflate.om: DEFLATE decoder (RFC 1951) ======== 2 note Canonical Huffman decoding one bit at a time (RFC 1951 section 3.2.2), the 32 KiB window of 3 note section 2, stored (3.2.4), fixed (3.2.6) and dynamic (3.2.7) blocks, every length code 257..285 4 note and distance code 0..29 (3.2.5). Each malformed case is its own exit code (README table). 5 6 text tbtype "invalid block type" 7 text tstlen "stored block length does not match its complement" 8 text tnsym "too many length or distance codes" 9 text tclset "code-length code is over-subscribed or incomplete" 10 text trep "invalid code-length repeat" 11 text tlset "literal/length code lengths are over-subscribed or incomplete" 12 text tdset "distance code lengths are over-subscribed or incomplete" 13 text teob "no end-of-block code" 14 text tlcode "invalid literal/length code" 15 text tdcode "invalid distance code" 16 text tfar "distance too far back" 17 18 note tabinit: code-length order (3.2.7) and the base and extra-bit tables of section 3.2.5. 19 note Lengths: codes 257..264 are 3..10 with no extra bits, then four codes per extra-bit count 1..5, 20 note and code 285 is 258 with none. Distances: codes 0..3 are 1..4, then two codes per extra-bit 21 note count 1..13. 22 fn tabinit to void 23 put ord 0 16 24 put ord 1 17 25 put ord 2 18 26 put ord 3 0 27 put ord 4 8 28 put ord 5 7 29 put ord 6 9 30 put ord 7 6 31 put ord 8 10 32 put ord 9 5 33 put ord 10 11 34 put ord 11 4 35 put ord 12 12 36 put ord 13 3 37 put ord 14 13 38 put ord 15 2 39 put ord 16 14 40 put ord 17 1 41 put ord 18 15 42 b = ushort 3 43 for i ulong 28 44 e = ulong 0 45 if ge i 8 46 e = sub div i 4 1 47 end 48 put lbase i b 49 put lext i uchar e 50 b = add b ushort shl ulong 1 e 51 end 52 put lbase 28 258 53 put lext 28 0 54 d = ushort 1 55 for i ulong 30 56 e = ulong 0 57 if ge i 4 58 e = sub div i 2 1 59 end 60 put dbase i d 61 put dext i uchar e 62 d = add d ushort shl ulong 1 e 63 end 64 end 65 66 fn needb n ulong to void 67 c = get bitn 0 68 if ge c n 69 ret 70 end 71 b = get bitb 0 72 while lt c n bound 9 73 b = or b shl inbyte c 74 c = add c 8 75 end 76 put bitb 0 b 77 put bitn 0 c 78 end 79 80 fn bits n ulong to ulong 81 needb n 82 b = get bitb 0 83 v = and b sub shl ulong 1 n 1 84 put bitb 0 shr b n 85 put bitn 0 sub get bitn 0 n 86 ret v 87 end 88 89 note hbuild: canonical code for the n lengths hlen[lo ..] into table t. Returns the codes left 90 note unused at the longest length: 0 complete, above 0 incomplete, below 0 over-subscribed. 91 fn hbuild t ulong n ulong lo ulong to long 92 co = mul t 16 93 so = mul t 288 94 for l ulong 16 95 put hcnt add co l 0 96 end 97 for s ulong n bound 320 98 k = add co ulong get hlen add lo s 99 put hcnt k add get hcnt k 1 100 end 101 left = long 1 102 for l ulong 15 103 left = sub shl left 1 long get hcnt add co add l 1 104 if lt left 0 105 ret left 106 end 107 end 108 put hoff 1 0 109 for l ulong 14 110 m = add l 1 111 put hoff add m 1 add get hoff m get hcnt add co m 112 end 113 for s ulong n bound 320 114 k = ulong get hlen add lo s 115 if ne k 0 116 o = ulong get hoff k 117 put hsym add so o ushort s 118 put hoff k ushort add o 1 119 end 120 end 121 ret left 122 end 123 124 note hdec: one symbol of table t, reading the code most significant bit first (3.1.1). 125 fn hdec t ulong to ulong 126 co = mul t 16 127 so = mul t 288 128 code = ulong 0 129 first = ulong 0 130 index = ulong 0 131 b = get bitb 0 132 c = get bitn 0 133 for l ulong 15 134 if eq c 0 135 b = inbyte 136 c = 8 137 end 138 code = or code and b 1 139 b = shr b 1 140 c = sub c 1 141 n = ulong get hcnt add co add l 1 142 if lt code add first n 143 put bitb 0 b 144 put bitn 0 c 145 ret ulong get hsym add so add index sub code first 146 end 147 index = add index n 148 first = shl add first n 1 149 code = shl code 1 150 end 151 if eq t 0 152 fail 19 tlcode 153 ret 0 154 end 155 fail 20 tdcode 156 ret 0 157 end 158 159 note hsingle: an incomplete code is admitted only when it is one code of length 1 (as zlib does). 160 fn hsingle t ulong n ulong to bool 161 co = mul t 16 162 ret eq n add ulong get hcnt co ulong get hcnt add co 1 163 end 164 165 note fixed: the tables of 3.2.6; all 32 five-bit distance codes exist, so codes 30 and 31 decode 166 note (and are refused as invalid distance codes) instead of reading on. 167 fn fixed to void 168 if eq get htyp 0 1 169 ret 170 end 171 for s ulong 288 172 v = uchar 8 173 if ge s 144 174 v = 9 175 end 176 if ge s 256 177 v = 7 178 end 179 if ge s 280 180 v = 8 181 end 182 put hlen s v 183 end 184 for s ulong 32 185 put hlen add 288 s 5 186 end 187 l = hbuild 0 288 0 188 l = hbuild 1 32 288 189 put htyp 0 1 190 end 191 192 fn dynamic to void 193 put htyp 0 0 194 nlen = add bits 5 257 195 ndist = add bits 5 1 196 ncode = add bits 4 4 197 if gt nlen 286 198 fail 13 tnsym 199 end 200 if gt ndist 30 201 fail 13 tnsym 202 end 203 for i ulong 19 204 put hlen add 320 i 0 205 end 206 for i ulong ncode bound 19 207 put hlen add 320 ulong get ord i uchar bits 3 208 end 209 l = hbuild 0 19 320 210 if ne l 0 211 fail 14 tclset 212 end 213 if eq ulong get hcnt 0 19 214 fail 14 tclset 215 end 216 tot = add nlen ndist 217 i = ulong 0 218 while lt i tot bound 320 219 s = hdec 0 220 if lt s 16 221 put hlen i uchar s 222 i = add i 1 223 continue 224 end 225 v = uchar 0 226 r = ulong 0 227 if eq s 16 228 if eq i 0 229 fail 15 trep 230 end 231 v = get hlen sub i 1 232 r = add bits 2 3 233 else 234 if eq s 17 235 r = add bits 3 3 236 else 237 r = add bits 7 11 238 end 239 end 240 if gt add i r tot 241 fail 15 trep 242 end 243 for j ulong r bound 138 244 put hlen add i j v 245 end 246 i = add i r 247 end 248 if eq get hlen 256 0 249 fail 18 teob 250 end 251 l = hbuild 0 nlen 0 252 if lt l 0 253 fail 16 tlset 254 end 255 if gt l 0 256 if not hsingle 0 nlen 257 fail 16 tlset 258 end 259 end 260 l = hbuild 1 ndist nlen 261 if lt l 0 262 fail 17 tdset 263 end 264 if gt l 0 265 if not hsingle 1 ndist 266 fail 17 tdset 267 end 268 end 269 end 270 271 note wcopy: n bytes from d back, one at a time so an overlapping copy repeats (3.2.3). 272 fn wcopy d ulong n ulong to void 273 if gt d get otot 0 274 fail 21 tfar 275 ret 276 end 277 t = add get otot 0 n 278 if gt t get olim 0 279 ofail 280 ret 281 end 282 put otot 0 t 283 p = get wpos 0 284 s = and sub add p WSIZE d WMASK 285 for i ulong n bound 258 286 put win p get win s 287 p = add p 1 288 s = and add s 1 WMASK 289 if eq p WSIZE 290 wflush WSIZE 291 p = 0 292 end 293 end 294 put wpos 0 p 295 end 296 297 fn codes to void 298 while true bound `9223372036854775807` 299 s = hdec 0 300 if lt s 256 301 outb s 302 continue 303 end 304 if eq s 256 305 ret 306 end 307 s = sub s 257 308 if ge s 29 309 fail 19 tlcode 310 ret 311 end 312 n = add ulong get lbase s bits ulong get lext s 313 x = hdec 1 314 if ge x 30 315 fail 20 tdcode 316 ret 317 end 318 d = add ulong get dbase x bits ulong get dext x 319 wcopy d n 320 end 321 end 322 323 fn stored to void 324 put bitb 0 0 325 put bitn 0 0 326 a = inbyte 327 b = inbyte 328 n = or a shl b 8 329 a = inbyte 330 b = inbyte 331 m = or a shl b 8 332 if ne n xor m 65535 333 fail 12 tstlen 334 ret 335 end 336 if gt add get otot 0 n get olim 0 337 ofail 338 ret 339 end 340 for i ulong n bound 65535 341 outb inbyte 342 end 343 end 344 345 note inflate: one raw DEFLATE stream from the input source into the output sink; the last 346 note window part is flushed. Bytes after the final block are not read (the caller may read on). 347 fn inflate to void 348 put bitb 0 0 349 put bitn 0 0 350 put wpos 0 0 351 put otot 0 0 352 last = ulong 0 353 while eq last 0 bound `9223372036854775807` 354 last = bits 1 355 t = bits 2 356 if eq t 0 357 stored 358 continue 359 end 360 if eq t 1 361 fixed 362 codes 363 continue 364 end 365 if eq t 2 366 dynamic 367 codes 368 continue 369 end 370 fail 11 tbtype 371 end 372 wflush get wpos 0 373 end