omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / reservoir / res_inflate.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note Pure seeded OM-2 RFC 1950 zlib / RFC 1951 DEFLATE decoder and Adler-32. |
| 5 | note Replaces tools/reservoir/res_inflate.{c,h}. |
| 6 | note Concatenated after src/reservoir/reservoir.om (which supplies the om2 header). |
| 7 | |
| 8 | const RES_INF_OK int 0 |
| 9 | const RES_INF_ERR_HEADER int -1 |
| 10 | const RES_INF_ERR_TRUNC int -2 |
| 11 | const RES_INF_ERR_BLOCK int -3 |
| 12 | const RES_INF_ERR_CODES int -4 |
| 13 | const RES_INF_ERR_SYMBOL int -5 |
| 14 | const RES_INF_ERR_ADLER int -6 |
| 15 | const RES_INF_ERR_NOMEM int -7 |
| 16 | const RES_INF_ERR_ARG int -8 |
| 17 | |
| 18 | const RES_INF_MAX_OUT ulong 1073741824 |
| 19 | |
| 20 | global inf_out uchar * |
| 21 | global inf_out_len ulong 1 |
| 22 | global inf_out_cap ulong 1 |
| 23 | global inf_consumed ulong 1 |
| 24 | note inf_st: [0]=in_off [1]=in_end [2]=in_pos [3]=bitbuf [4]=bitcnt |
| 25 | global inf_st ulong 5 |
| 26 | |
| 27 | global inf_lc_cnt int 16 |
| 28 | global inf_lc_sym int 288 |
| 29 | global inf_dc_cnt int 16 |
| 30 | global inf_dc_sym int 32 |
| 31 | global inf_fix_lc_cnt int 16 |
| 32 | global inf_fix_lc_sym int 288 |
| 33 | global inf_fix_dc_cnt int 16 |
| 34 | global inf_fix_dc_sym int 32 |
| 35 | global inf_fix_built uint 1 |
| 36 | global inf_lengths int 320 |
| 37 | |
| 38 | fn inf_lbase idx ulong to ulong |
| 39 | if eq idx 0 |
| 40 | ret 3 |
| 41 | end |
| 42 | if eq idx 1 |
| 43 | ret 4 |
| 44 | end |
| 45 | if eq idx 2 |
| 46 | ret 5 |
| 47 | end |
| 48 | if eq idx 3 |
| 49 | ret 6 |
| 50 | end |
| 51 | if eq idx 4 |
| 52 | ret 7 |
| 53 | end |
| 54 | if eq idx 5 |
| 55 | ret 8 |
| 56 | end |
| 57 | if eq idx 6 |
| 58 | ret 9 |
| 59 | end |
| 60 | if eq idx 7 |
| 61 | ret 10 |
| 62 | end |
| 63 | if eq idx 8 |
| 64 | ret 11 |
| 65 | end |
| 66 | if eq idx 9 |
| 67 | ret 13 |
| 68 | end |
| 69 | if eq idx 10 |
| 70 | ret 15 |
| 71 | end |
| 72 | if eq idx 11 |
| 73 | ret 17 |
| 74 | end |
| 75 | if eq idx 12 |
| 76 | ret 19 |
| 77 | end |
| 78 | if eq idx 13 |
| 79 | ret 23 |
| 80 | end |
| 81 | if eq idx 14 |
| 82 | ret 27 |
| 83 | end |
| 84 | if eq idx 15 |
| 85 | ret 31 |
| 86 | end |
| 87 | if eq idx 16 |
| 88 | ret 35 |
| 89 | end |
| 90 | if eq idx 17 |
| 91 | ret 43 |
| 92 | end |
| 93 | if eq idx 18 |
| 94 | ret 51 |
| 95 | end |
| 96 | if eq idx 19 |
| 97 | ret 59 |
| 98 | end |
| 99 | if eq idx 20 |
| 100 | ret 67 |
| 101 | end |
| 102 | if eq idx 21 |
| 103 | ret 83 |
| 104 | end |
| 105 | if eq idx 22 |
| 106 | ret 99 |
| 107 | end |
| 108 | if eq idx 23 |
| 109 | ret 115 |
| 110 | end |
| 111 | if eq idx 24 |
| 112 | ret 131 |
| 113 | end |
| 114 | if eq idx 25 |
| 115 | ret 163 |
| 116 | end |
| 117 | if eq idx 26 |
| 118 | ret 195 |
| 119 | end |
| 120 | if eq idx 27 |
| 121 | ret 227 |
| 122 | end |
| 123 | ret 258 |
| 124 | end |
| 125 | |
| 126 | fn inf_lext idx ulong to ulong |
| 127 | if lt idx 8 |
| 128 | ret 0 |
| 129 | end |
| 130 | if eq idx 28 |
| 131 | ret 0 |
| 132 | end |
| 133 | ret div sub idx 4 4 |
| 134 | end |
| 135 | |
| 136 | fn inf_dbase idx ulong to ulong |
| 137 | if eq idx 0 |
| 138 | ret 1 |
| 139 | end |
| 140 | if eq idx 1 |
| 141 | ret 2 |
| 142 | end |
| 143 | if eq idx 2 |
| 144 | ret 3 |
| 145 | end |
| 146 | if eq idx 3 |
| 147 | ret 4 |
| 148 | end |
| 149 | if eq idx 4 |
| 150 | ret 5 |
| 151 | end |
| 152 | if eq idx 5 |
| 153 | ret 7 |
| 154 | end |
| 155 | if eq idx 6 |
| 156 | ret 9 |
| 157 | end |
| 158 | if eq idx 7 |
| 159 | ret 13 |
| 160 | end |
| 161 | if eq idx 8 |
| 162 | ret 17 |
| 163 | end |
| 164 | if eq idx 9 |
| 165 | ret 25 |
| 166 | end |
| 167 | if eq idx 10 |
| 168 | ret 33 |
| 169 | end |
| 170 | if eq idx 11 |
| 171 | ret 49 |
| 172 | end |
| 173 | if eq idx 12 |
| 174 | ret 65 |
| 175 | end |
| 176 | if eq idx 13 |
| 177 | ret 97 |
| 178 | end |
| 179 | if eq idx 14 |
| 180 | ret 129 |
| 181 | end |
| 182 | if eq idx 15 |
| 183 | ret 193 |
| 184 | end |
| 185 | if eq idx 16 |
| 186 | ret 257 |
| 187 | end |
| 188 | if eq idx 17 |
| 189 | ret 385 |
| 190 | end |
| 191 | if eq idx 18 |
| 192 | ret 513 |
| 193 | end |
| 194 | if eq idx 19 |
| 195 | ret 769 |
| 196 | end |
| 197 | if eq idx 20 |
| 198 | ret 1025 |
| 199 | end |
| 200 | if eq idx 21 |
| 201 | ret 1537 |
| 202 | end |
| 203 | if eq idx 22 |
| 204 | ret 2049 |
| 205 | end |
| 206 | if eq idx 23 |
| 207 | ret 3073 |
| 208 | end |
| 209 | if eq idx 24 |
| 210 | ret 4097 |
| 211 | end |
| 212 | if eq idx 25 |
| 213 | ret 6145 |
| 214 | end |
| 215 | if eq idx 26 |
| 216 | ret 8193 |
| 217 | end |
| 218 | if eq idx 27 |
| 219 | ret 12289 |
| 220 | end |
| 221 | if eq idx 28 |
| 222 | ret 16385 |
| 223 | end |
| 224 | ret 24577 |
| 225 | end |
| 226 | |
| 227 | fn inf_dext idx ulong to ulong |
| 228 | if lt idx 4 |
| 229 | ret 0 |
| 230 | end |
| 231 | ret div sub idx 2 2 |
| 232 | end |
| 233 | |
| 234 | fn inf_order idx ulong to ulong |
| 235 | if eq idx 0 |
| 236 | ret 16 |
| 237 | end |
| 238 | if eq idx 1 |
| 239 | ret 17 |
| 240 | end |
| 241 | if eq idx 2 |
| 242 | ret 18 |
| 243 | end |
| 244 | if eq idx 3 |
| 245 | ret 0 |
| 246 | end |
| 247 | if eq idx 4 |
| 248 | ret 8 |
| 249 | end |
| 250 | if eq idx 5 |
| 251 | ret 7 |
| 252 | end |
| 253 | if eq idx 6 |
| 254 | ret 9 |
| 255 | end |
| 256 | if eq idx 7 |
| 257 | ret 6 |
| 258 | end |
| 259 | if eq idx 8 |
| 260 | ret 10 |
| 261 | end |
| 262 | if eq idx 9 |
| 263 | ret 5 |
| 264 | end |
| 265 | if eq idx 10 |
| 266 | ret 11 |
| 267 | end |
| 268 | if eq idx 11 |
| 269 | ret 4 |
| 270 | end |
| 271 | if eq idx 12 |
| 272 | ret 12 |
| 273 | end |
| 274 | if eq idx 13 |
| 275 | ret 3 |
| 276 | end |
| 277 | if eq idx 14 |
| 278 | ret 13 |
| 279 | end |
| 280 | if eq idx 15 |
| 281 | ret 2 |
| 282 | end |
| 283 | if eq idx 16 |
| 284 | ret 14 |
| 285 | end |
| 286 | if eq idx 17 |
| 287 | ret 1 |
| 288 | end |
| 289 | ret 15 |
| 290 | end |
| 291 | |
| 292 | fn inf_getbits in view uchar * need ulong val_out edit uint 1 to int |
| 293 | if eq need 0 |
| 294 | put val_out 0 0 |
| 295 | ret RES_INF_OK |
| 296 | end |
| 297 | v = get inf_st 3 |
| 298 | bc = get inf_st 4 |
| 299 | pos = get inf_st 2 |
| 300 | iend = get inf_st 1 |
| 301 | while lt bc need bound 8 |
| 302 | if ge pos iend |
| 303 | ret RES_INF_ERR_TRUNC |
| 304 | end |
| 305 | b = ulong get in pos |
| 306 | v = or v shl b bc |
| 307 | pos = add pos 1 |
| 308 | bc = add bc 8 |
| 309 | end |
| 310 | mask = sub shl ulong 1 need 1 |
| 311 | put val_out 0 uint and v mask |
| 312 | put inf_st 2 pos |
| 313 | put inf_st 3 shr v need |
| 314 | put inf_st 4 sub bc need |
| 315 | ret RES_INF_OK |
| 316 | end |
| 317 | |
| 318 | fn inf_put b uchar to int |
| 319 | ol = get inf_out_len 0 |
| 320 | oc = get inf_out_cap 0 |
| 321 | if ge ol oc |
| 322 | if ge oc RES_INF_MAX_OUT |
| 323 | ret RES_INF_ERR_NOMEM |
| 324 | end |
| 325 | ncap = ulong 65536 |
| 326 | if gt oc 0 |
| 327 | ncap = mul oc 2 |
| 328 | end |
| 329 | if gt ncap RES_INF_MAX_OUT |
| 330 | ncap = RES_INF_MAX_OUT |
| 331 | end |
| 332 | e = enlarge inf_out ncap |
| 333 | if lt e 0 |
| 334 | ret RES_INF_ERR_NOMEM |
| 335 | end |
| 336 | put inf_out_cap 0 ncap |
| 337 | end |
| 338 | put inf_out ol b |
| 339 | put inf_out_len 0 add ol 1 |
| 340 | ret RES_INF_OK |
| 341 | end |
| 342 | |
| 343 | fn inf_stored in view uchar * to int |
| 344 | put inf_st 3 0 |
| 345 | put inf_st 4 0 |
| 346 | pos = get inf_st 2 |
| 347 | iend = get inf_st 1 |
| 348 | if lt sub iend pos 4 |
| 349 | ret RES_INF_ERR_TRUNC |
| 350 | end |
| 351 | blen = or ulong get in pos shl ulong get in add pos 1 8 |
| 352 | nlen = or ulong get in add pos 2 shl ulong get in add pos 3 8 |
| 353 | if ne blen and xor nlen 0xffff 0xffff |
| 354 | ret RES_INF_ERR_BLOCK |
| 355 | end |
| 356 | pos = add pos 4 |
| 357 | if lt sub iend pos blen |
| 358 | ret RES_INF_ERR_TRUNC |
| 359 | end |
| 360 | for i ulong blen bound 65536 |
| 361 | e = inf_put get in add pos i |
| 362 | if ne e RES_INF_OK |
| 363 | ret e |
| 364 | end |
| 365 | end |
| 366 | put inf_st 2 add pos blen |
| 367 | ret RES_INF_OK |
| 368 | end |
| 369 | |
| 370 | fn inf_construct_lc off ulong n ulong to int |
| 371 | offs = new int 16 0 |
| 372 | for l ulong 16 bound 16 |
| 373 | put inf_lc_cnt l 0 |
| 374 | end |
| 375 | for sym ulong n bound 288 |
| 376 | lv = ulong get inf_lengths add off sym |
| 377 | put inf_lc_cnt lv add get inf_lc_cnt lv 1 |
| 378 | end |
| 379 | if eq get inf_lc_cnt 0 int n |
| 380 | ret 0 |
| 381 | end |
| 382 | left = int 1 |
| 383 | for l ulong 15 bound 15 |
| 384 | ll = add l 1 |
| 385 | left = sub shl left 1 get inf_lc_cnt ll |
| 386 | if lt left 0 |
| 387 | ret left |
| 388 | end |
| 389 | end |
| 390 | put offs 1 0 |
| 391 | for l ulong 14 bound 14 |
| 392 | ll = add l 1 |
| 393 | put offs add ll 1 add get offs ll get inf_lc_cnt ll |
| 394 | end |
| 395 | for sym ulong n bound 288 |
| 396 | lv = get inf_lengths add off sym |
| 397 | if ne lv 0 |
| 398 | idx = get offs ulong lv |
| 399 | put inf_lc_sym ulong idx int sym |
| 400 | put offs ulong lv add idx 1 |
| 401 | end |
| 402 | end |
| 403 | ret left |
| 404 | end |
| 405 | |
| 406 | fn inf_construct_dc off ulong n ulong to int |
| 407 | offs = new int 16 0 |
| 408 | for l ulong 16 bound 16 |
| 409 | put inf_dc_cnt l 0 |
| 410 | end |
| 411 | for sym ulong n bound 32 |
| 412 | lv = ulong get inf_lengths add off sym |
| 413 | put inf_dc_cnt lv add get inf_dc_cnt lv 1 |
| 414 | end |
| 415 | if eq get inf_dc_cnt 0 int n |
| 416 | ret 0 |
| 417 | end |
| 418 | left = int 1 |
| 419 | for l ulong 15 bound 15 |
| 420 | ll = add l 1 |
| 421 | left = sub shl left 1 get inf_dc_cnt ll |
| 422 | if lt left 0 |
| 423 | ret left |
| 424 | end |
| 425 | end |
| 426 | put offs 1 0 |
| 427 | for l ulong 14 bound 14 |
| 428 | ll = add l 1 |
| 429 | put offs add ll 1 add get offs ll get inf_dc_cnt ll |
| 430 | end |
| 431 | for sym ulong n bound 32 |
| 432 | lv = get inf_lengths add off sym |
| 433 | if ne lv 0 |
| 434 | idx = get offs ulong lv |
| 435 | put inf_dc_sym ulong idx int sym |
| 436 | put offs ulong lv add idx 1 |
| 437 | end |
| 438 | end |
| 439 | ret left |
| 440 | end |
| 441 | |
| 442 | fn inf_decode_lc in view uchar * use_fix bool sym_out edit int 1 to int |
| 443 | code = int 0 |
| 444 | first = int 0 |
| 445 | index = int 0 |
| 446 | bv = new uint 1 0 |
| 447 | for l ulong 15 bound 15 |
| 448 | e = inf_getbits in 1 bv |
| 449 | if ne e RES_INF_OK |
| 450 | ret e |
| 451 | end |
| 452 | code = or code int get bv 0 |
| 453 | cnt = get inf_lc_cnt add l 1 |
| 454 | if use_fix |
| 455 | cnt = get inf_fix_lc_cnt add l 1 |
| 456 | end |
| 457 | if lt sub code cnt first |
| 458 | si = ulong add index sub code first |
| 459 | if use_fix |
| 460 | put sym_out 0 get inf_fix_lc_sym si |
| 461 | else |
| 462 | put sym_out 0 get inf_lc_sym si |
| 463 | end |
| 464 | ret RES_INF_OK |
| 465 | end |
| 466 | index = add index cnt |
| 467 | first = shl add first cnt 1 |
| 468 | code = shl code 1 |
| 469 | end |
| 470 | ret RES_INF_ERR_CODES |
| 471 | end |
| 472 | |
| 473 | fn inf_decode_dc in view uchar * use_fix bool sym_out edit int 1 to int |
| 474 | code = int 0 |
| 475 | first = int 0 |
| 476 | index = int 0 |
| 477 | bv = new uint 1 0 |
| 478 | for l ulong 15 bound 15 |
| 479 | e = inf_getbits in 1 bv |
| 480 | if ne e RES_INF_OK |
| 481 | ret e |
| 482 | end |
| 483 | code = or code int get bv 0 |
| 484 | cnt = get inf_dc_cnt add l 1 |
| 485 | if use_fix |
| 486 | cnt = get inf_fix_dc_cnt add l 1 |
| 487 | end |
| 488 | if lt sub code cnt first |
| 489 | si = ulong add index sub code first |
| 490 | if use_fix |
| 491 | put sym_out 0 get inf_fix_dc_sym si |
| 492 | else |
| 493 | put sym_out 0 get inf_dc_sym si |
| 494 | end |
| 495 | ret RES_INF_OK |
| 496 | end |
| 497 | index = add index cnt |
| 498 | first = shl add first cnt 1 |
| 499 | code = shl code 1 |
| 500 | end |
| 501 | ret RES_INF_ERR_CODES |
| 502 | end |
| 503 | |
| 504 | fn inf_copy_match mlen ulong dist ulong to int |
| 505 | if gt dist get inf_out_len 0 |
| 506 | ret RES_INF_ERR_SYMBOL |
| 507 | end |
| 508 | for i ulong mlen bound 260 |
| 509 | src = sub get inf_out_len 0 dist |
| 510 | e = inf_put get inf_out src |
| 511 | if ne e RES_INF_OK |
| 512 | ret e |
| 513 | end |
| 514 | end |
| 515 | ret RES_INF_OK |
| 516 | end |
| 517 | |
| 518 | fn inf_codes in view uchar * use_fix bool to int |
| 519 | sv = new int 1 0 |
| 520 | xv = new uint 1 0 |
| 521 | while true bound 500000000 |
| 522 | e = inf_decode_lc in use_fix sv |
| 523 | if ne e RES_INF_OK |
| 524 | ret e |
| 525 | end |
| 526 | sym = get sv 0 |
| 527 | if lt sym 256 |
| 528 | e = inf_put uchar and sym 0xff |
| 529 | if ne e RES_INF_OK |
| 530 | ret e |
| 531 | end |
| 532 | continue |
| 533 | end |
| 534 | if eq sym 256 |
| 535 | ret RES_INF_OK |
| 536 | end |
| 537 | sym = sub sym 257 |
| 538 | if or lt sym 0 ge sym 29 |
| 539 | ret RES_INF_ERR_SYMBOL |
| 540 | end |
| 541 | e = inf_getbits in inf_lext ulong sym xv |
| 542 | if ne e RES_INF_OK |
| 543 | ret e |
| 544 | end |
| 545 | mlen = add inf_lbase ulong sym ulong get xv 0 |
| 546 | e = inf_decode_dc in use_fix sv |
| 547 | if ne e RES_INF_OK |
| 548 | ret e |
| 549 | end |
| 550 | dsym = get sv 0 |
| 551 | if or lt dsym 0 ge dsym 30 |
| 552 | ret RES_INF_ERR_SYMBOL |
| 553 | end |
| 554 | e = inf_getbits in inf_dext ulong dsym xv |
| 555 | if ne e RES_INF_OK |
| 556 | ret e |
| 557 | end |
| 558 | dist = add inf_dbase ulong dsym ulong get xv 0 |
| 559 | e = inf_copy_match mlen dist |
| 560 | if ne e RES_INF_OK |
| 561 | ret e |
| 562 | end |
| 563 | end |
| 564 | ret RES_INF_ERR_BLOCK |
| 565 | end |
| 566 | |
| 567 | fn inf_build_fixed to void |
| 568 | if ne get inf_fix_built 0 0 |
| 569 | ret |
| 570 | end |
| 571 | for sym ulong 288 bound 288 |
| 572 | if lt sym 144 |
| 573 | put inf_lengths sym 8 |
| 574 | else |
| 575 | if lt sym 256 |
| 576 | put inf_lengths sym 9 |
| 577 | else |
| 578 | if lt sym 280 |
| 579 | put inf_lengths sym 7 |
| 580 | else |
| 581 | put inf_lengths sym 8 |
| 582 | end |
| 583 | end |
| 584 | end |
| 585 | end |
| 586 | _e1 = inf_construct_lc 0 288 |
| 587 | for i ulong 16 bound 16 |
| 588 | put inf_fix_lc_cnt i get inf_lc_cnt i |
| 589 | end |
| 590 | for i ulong 288 bound 288 |
| 591 | put inf_fix_lc_sym i get inf_lc_sym i |
| 592 | end |
| 593 | for sym ulong 30 bound 30 |
| 594 | put inf_lengths sym 5 |
| 595 | end |
| 596 | _e2 = inf_construct_dc 0 30 |
| 597 | for i ulong 16 bound 16 |
| 598 | put inf_fix_dc_cnt i get inf_dc_cnt i |
| 599 | end |
| 600 | for i ulong 32 bound 32 |
| 601 | put inf_fix_dc_sym i get inf_dc_sym i |
| 602 | end |
| 603 | put inf_fix_built 0 1 |
| 604 | end |
| 605 | |
| 606 | fn inf_dynamic_read_lens in view uchar * total ulong to int |
| 607 | idx = ulong 0 |
| 608 | sv = new int 1 0 |
| 609 | xv = new uint 1 0 |
| 610 | while lt idx total bound 400 |
| 611 | e = inf_decode_lc in false sv |
| 612 | if ne e RES_INF_OK |
| 613 | ret e |
| 614 | end |
| 615 | sym = get sv 0 |
| 616 | if lt sym 16 |
| 617 | put inf_lengths idx sym |
| 618 | idx = add idx 1 |
| 619 | continue |
| 620 | end |
| 621 | lv = int 0 |
| 622 | rep = ulong 0 |
| 623 | if eq sym 16 |
| 624 | if eq idx 0 |
| 625 | ret RES_INF_ERR_CODES |
| 626 | end |
| 627 | lv = get inf_lengths sub idx 1 |
| 628 | e = inf_getbits in 2 xv |
| 629 | if ne e RES_INF_OK |
| 630 | ret e |
| 631 | end |
| 632 | rep = add 3 ulong get xv 0 |
| 633 | else |
| 634 | if eq sym 17 |
| 635 | e = inf_getbits in 3 xv |
| 636 | if ne e RES_INF_OK |
| 637 | ret e |
| 638 | end |
| 639 | rep = add 3 ulong get xv 0 |
| 640 | else |
| 641 | e = inf_getbits in 7 xv |
| 642 | if ne e RES_INF_OK |
| 643 | ret e |
| 644 | end |
| 645 | rep = add 11 ulong get xv 0 |
| 646 | end |
| 647 | end |
| 648 | if gt add idx rep total |
| 649 | ret RES_INF_ERR_CODES |
| 650 | end |
| 651 | for r ulong rep bound 140 |
| 652 | put inf_lengths idx lv |
| 653 | idx = add idx 1 |
| 654 | end |
| 655 | end |
| 656 | ret RES_INF_OK |
| 657 | end |
| 658 | |
| 659 | fn inf_dynamic in view uchar * to int |
| 660 | xv = new uint 1 0 |
| 661 | e = inf_getbits in 5 xv |
| 662 | if ne e RES_INF_OK |
| 663 | ret e |
| 664 | end |
| 665 | nlen = add ulong get xv 0 257 |
| 666 | e = inf_getbits in 5 xv |
| 667 | if ne e RES_INF_OK |
| 668 | ret e |
| 669 | end |
| 670 | ndist = add ulong get xv 0 1 |
| 671 | e = inf_getbits in 4 xv |
| 672 | if ne e RES_INF_OK |
| 673 | ret e |
| 674 | end |
| 675 | ncode = add ulong get xv 0 4 |
| 676 | if or gt nlen 286 gt ndist 30 |
| 677 | ret RES_INF_ERR_CODES |
| 678 | end |
| 679 | for i ulong 19 bound 19 |
| 680 | put inf_lengths i 0 |
| 681 | end |
| 682 | for i ulong ncode bound 19 |
| 683 | e = inf_getbits in 3 xv |
| 684 | if ne e RES_INF_OK |
| 685 | ret e |
| 686 | end |
| 687 | put inf_lengths inf_order i int get xv 0 |
| 688 | end |
| 689 | if ne inf_construct_lc 0 19 0 |
| 690 | ret RES_INF_ERR_CODES |
| 691 | end |
| 692 | e = inf_dynamic_read_lens in add nlen ndist |
| 693 | if ne e RES_INF_OK |
| 694 | ret e |
| 695 | end |
| 696 | if eq get inf_lengths 256 0 |
| 697 | ret RES_INF_ERR_CODES |
| 698 | end |
| 699 | e = inf_construct_lc 0 nlen |
| 700 | if ne e 0 |
| 701 | if or lt e 0 ne int nlen add get inf_lc_cnt 0 get inf_lc_cnt 1 |
| 702 | ret RES_INF_ERR_CODES |
| 703 | end |
| 704 | end |
| 705 | e = inf_construct_dc nlen ndist |
| 706 | if ne e 0 |
| 707 | if or lt e 0 ne int ndist add get inf_dc_cnt 0 get inf_dc_cnt 1 |
| 708 | ret RES_INF_ERR_CODES |
| 709 | end |
| 710 | end |
| 711 | ret inf_codes in false |
| 712 | end |
| 713 | |
| 714 | fn res_adler32 p view uchar * off ulong n ulong to uint |
| 715 | a = uint 1 |
| 716 | b = uint 0 |
| 717 | pos = off |
| 718 | remn = n |
| 719 | while gt remn 0 bound 500000 |
| 720 | k = remn |
| 721 | if gt k 5552 |
| 722 | k = 5552 |
| 723 | end |
| 724 | remn = sub remn k |
| 725 | for i ulong k bound 5552 |
| 726 | a = add a uint get p pos |
| 727 | b = add b a |
| 728 | pos = add pos 1 |
| 729 | end |
| 730 | a = rem a 65521 |
| 731 | b = rem b 65521 |
| 732 | end |
| 733 | ret or shl b 16 a |
| 734 | end |
| 735 | |
| 736 | fn res_adler32_inf_out n ulong to uint |
| 737 | a = uint 1 |
| 738 | b = uint 0 |
| 739 | pos = ulong 0 |
| 740 | remn = n |
| 741 | while gt remn 0 bound 500000 |
| 742 | k = remn |
| 743 | if gt k 5552 |
| 744 | k = 5552 |
| 745 | end |
| 746 | remn = sub remn k |
| 747 | for i ulong k bound 5552 |
| 748 | a = add a uint get inf_out pos |
| 749 | b = add b a |
| 750 | pos = add pos 1 |
| 751 | end |
| 752 | a = rem a 65521 |
| 753 | b = rem b 65521 |
| 754 | end |
| 755 | ret or shl b 16 a |
| 756 | end |
| 757 | |
| 758 | fn inf_check_adler in view uchar * in_off ulong to int |
| 759 | pos = get inf_st 2 |
| 760 | iend = get inf_st 1 |
| 761 | if lt sub iend pos 4 |
| 762 | ret RES_INF_ERR_TRUNC |
| 763 | end |
| 764 | want = shl uint get in pos 24 |
| 765 | want = or want shl uint get in add pos 1 16 |
| 766 | want = or want shl uint get in add pos 2 8 |
| 767 | want = or want uint get in add pos 3 |
| 768 | got = res_adler32_inf_out get inf_out_len 0 |
| 769 | if ne got want |
| 770 | ret RES_INF_ERR_ADLER |
| 771 | end |
| 772 | put inf_consumed 0 sub add pos 4 in_off |
| 773 | ret RES_INF_OK |
| 774 | end |
| 775 | |
| 776 | fn res_zlib_inflate in view uchar * in_off ulong in_len ulong to int |
| 777 | put inf_out_len 0 0 |
| 778 | put inf_consumed 0 0 |
| 779 | if lt in_len 2 |
| 780 | ret RES_INF_ERR_TRUNC |
| 781 | end |
| 782 | if gt add in_off in_len len in |
| 783 | ret RES_INF_ERR_ARG |
| 784 | end |
| 785 | cmf = uint get in in_off |
| 786 | flg = uint get in add in_off 1 |
| 787 | if or ne and cmf 0x0f 8 gt shr cmf 4 7 |
| 788 | ret RES_INF_ERR_HEADER |
| 789 | end |
| 790 | if ne rem or shl cmf 8 flg 31 0 |
| 791 | ret RES_INF_ERR_HEADER |
| 792 | end |
| 793 | if ne and flg 0x20 0 |
| 794 | ret RES_INF_ERR_HEADER |
| 795 | end |
| 796 | put inf_st 0 in_off |
| 797 | put inf_st 1 add in_off in_len |
| 798 | put inf_st 2 add in_off 2 |
| 799 | put inf_st 3 0 |
| 800 | put inf_st 4 0 |
| 801 | last = uint 0 |
| 802 | btype = new uint 1 0 |
| 803 | lv = new uint 1 0 |
| 804 | while eq last 0 bound 1000000 |
| 805 | e = inf_getbits in 1 lv |
| 806 | if ne e RES_INF_OK |
| 807 | ret e |
| 808 | end |
| 809 | last = get lv 0 |
| 810 | e = inf_getbits in 2 btype |
| 811 | if ne e RES_INF_OK |
| 812 | ret e |
| 813 | end |
| 814 | bt = get btype 0 |
| 815 | if eq bt 0 |
| 816 | e = inf_stored in |
| 817 | else |
| 818 | if eq bt 1 |
| 819 | inf_build_fixed |
| 820 | e = inf_codes in true |
| 821 | else |
| 822 | if eq bt 2 |
| 823 | e = inf_dynamic in |
| 824 | else |
| 825 | e = RES_INF_ERR_BLOCK |
| 826 | end |
| 827 | end |
| 828 | end |
| 829 | if ne e RES_INF_OK |
| 830 | ret e |
| 831 | end |
| 832 | end |
| 833 | put inf_st 3 0 |
| 834 | put inf_st 4 0 |
| 835 | ret inf_check_adler in in_off |
| 836 | end |