@amritk/nish-x86_64-linux 0.46.0 → 0.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +29 -18
- package/package.json +1 -1
- package/runtime/gfx/README.md +55 -0
- package/runtime/gfx/nish-gfx.c +1063 -0
- package/runtime/gfx/nish-gfx.h +221 -0
- package/runtime/gfx/nish-gfx.overrides.json +14 -0
- package/runtime/nish.d.ts +22 -5
- package/runtime/nish.h +1 -0
- package/runtime/nish.mjs +1 -0
- package/runtime/runtime-bytes.c +167 -7
- package/runtime/runtime-net.c +122 -52
- package/runtime/runtime-uring.c +150 -69
- package/runtime/sqlite/nish-sqlite.c +166 -39
- package/scripts/build.sh +88 -3
- package/std/README.md +2 -2
- package/std/base64.ts +28 -30
- package/std/compress/deflate.ts +1371 -704
- package/std/crypto/aes.ts +81 -45
- package/std/crypto/bcrypt-batch.ts +23 -4
- package/std/crypto/bcrypt.ts +36 -11
- package/std/crypto/chacha20poly1305.ts +93 -52
- package/std/crypto/x25519.ts +7 -4
- package/std/host/game.ts +52 -20
- package/std/host/ring.ts +115 -31
- package/std/host/text.ts +9 -3
- package/std/json.ts +83 -0
- package/std/math.ts +161 -131
- package/std/net/handler.ts +24 -6
- package/std/net/http-server.ts +358 -141
- package/std/net/http1-server.ts +44 -13
- package/std/net/prefork.ts +1 -2
- package/std/net/static.ts +7 -2
- package/std/random.ts +34 -51
- package/std/raster.ts +377 -113
- package/std/regex.ts +46 -15
- package/std/release.ts +19 -13
- package/std/sqlite.ts +380 -16
- package/std/synth.ts +20 -3
- package/std/temporal.ts +740 -363
- package/std/term-io.ts +81 -33
- package/std/term-pixels.ts +287 -112
- package/std/term-probe.ts +7 -3
- package/std/term-rate.ts +142 -66
- package/std/term.ts +782 -406
- package/std/testing.ts +14 -7
package/std/compress/deflate.ts
CHANGED
|
@@ -122,15 +122,9 @@ const DEFLATE_BLOCK_TOKENS: i32 = 16384
|
|
|
122
122
|
/** A table position this far up is slid back down before the next block. */
|
|
123
123
|
const DEFLATE_REBASE: i32 = 1073741824
|
|
124
124
|
|
|
125
|
-
/** The largest stored block (§3.2.4). */
|
|
126
|
-
const DEFLATE_STORED_MAX: i32 = 65535
|
|
127
|
-
|
|
128
125
|
/** Huffman's multiplier for the hash (Knuth's 2^32 / phi). */
|
|
129
126
|
const DEFLATE_HASH_MUL: i64 = 2654435761
|
|
130
127
|
|
|
131
|
-
/** `FixedDeflater`'s farthest match back: its inputs are short. */
|
|
132
|
-
const FIXED_WINDOW: i32 = 16384
|
|
133
|
-
|
|
134
128
|
/** `FixedDeflater`'s largest hash table, 2^14 entries; a call clears only as much as its input needs. */
|
|
135
129
|
const FIXED_HASH_BITS: i32 = 14
|
|
136
130
|
/** `FixedDeflater`'s hash table size, `1 << FIXED_HASH_BITS`. */
|
|
@@ -285,33 +279,51 @@ const deflateCtz64 = (x: u64): i32 => {
|
|
|
285
279
|
return lo !== 0 ? low : high
|
|
286
280
|
}
|
|
287
281
|
|
|
282
|
+
/**
|
|
283
|
+
* `off + len` for a window of `a` that `deflateWindow` accepted, or -1 for
|
|
284
|
+
* one outside `a`. It is `a.length` less the room the window leaves: each
|
|
285
|
+
* step subtracts two values from zero up, so the bounds prover sees no sum
|
|
286
|
+
* that can overflow.
|
|
287
|
+
*/
|
|
288
|
+
const deflateWindowEnd = (a: u8[], off: i32, len: i32): i32 => {
|
|
289
|
+
if (off < 0 || len < 0) {
|
|
290
|
+
return -1
|
|
291
|
+
}
|
|
292
|
+
const n: i32 = toI32(a.length)
|
|
293
|
+
const room: i32 = n - off
|
|
294
|
+
if (room < 0) {
|
|
295
|
+
return -1
|
|
296
|
+
}
|
|
297
|
+
const gap: i32 = room - len
|
|
298
|
+
return gap < 0 ? -1 : n - gap
|
|
299
|
+
}
|
|
300
|
+
|
|
288
301
|
/**
|
|
289
302
|
* How many bytes from `a` and from `b` agree, at most `max` and at most
|
|
290
303
|
* `DEFLATE_MAX_MATCH`; `a < b` and `b + max` lie inside `src`, which the
|
|
291
304
|
* caller has checked.
|
|
292
305
|
*/
|
|
293
306
|
const deflateMatchLength = (src: u8[], a: i32, b: i32, max: i32): i32 => {
|
|
294
|
-
// `
|
|
295
|
-
//
|
|
296
|
-
//
|
|
297
|
-
//
|
|
307
|
+
// `src` is at most 2^31 - 1 bytes and `b + max` lies inside it, so a
|
|
308
|
+
// position past 2,147,483,389 only starts a match within 258 bytes of the
|
|
309
|
+
// largest array's end: that one is reported as none, a valid if shorter
|
|
310
|
+
// answer, and the bound is what lets `a + k` and `b + k` be proven sums.
|
|
311
|
+
// `k`'s caps are no-ops for a caller, whose `max` is at most 258.
|
|
312
|
+
if (a < 0 || b < 0 || a > 2147483389 || b > 2147483389) {
|
|
313
|
+
return 0
|
|
314
|
+
}
|
|
298
315
|
let k: i32 = 0
|
|
299
|
-
while (k >= 0 && k <= 250 && k + 8 <= max
|
|
300
|
-
const pa: i32 =
|
|
301
|
-
const pb: i32 =
|
|
302
|
-
if (pa < 0 || pa > 2147483639 || pb < 0 || pb > 2147483639) {
|
|
303
|
-
return k
|
|
304
|
-
}
|
|
316
|
+
while (k >= 0 && k <= 250 && k + 8 <= max) {
|
|
317
|
+
const pa: i32 = a + k
|
|
318
|
+
const pb: i32 = b + k
|
|
305
319
|
const x: u64 = deflateLoad64(src, pa) ^ deflateLoad64(src, pb)
|
|
306
320
|
if (x !== toU64(0)) {
|
|
307
321
|
return k + (deflateCtz64(x) >> 3)
|
|
308
322
|
}
|
|
309
323
|
k += 8
|
|
310
324
|
}
|
|
311
|
-
while (k >= 0 && k < 258 && k < max
|
|
312
|
-
|
|
313
|
-
const pb: i32 = toI32(toU32(b) + toU32(k))
|
|
314
|
-
if (pa < 0 || pb < 0 || uncheckedGet(src, pa) !== uncheckedGet(src, pb)) {
|
|
325
|
+
while (k >= 0 && k < 258 && k < max) {
|
|
326
|
+
if (uncheckedGet(src, a + k) !== uncheckedGet(src, b + k)) {
|
|
315
327
|
return k
|
|
316
328
|
}
|
|
317
329
|
k += 1
|
|
@@ -341,8 +353,20 @@ const deflateLengthBase = (s: i32): i32 => {
|
|
|
341
353
|
if (s >= 28) {
|
|
342
354
|
return 258
|
|
343
355
|
}
|
|
344
|
-
//
|
|
345
|
-
|
|
356
|
+
// 4 to 7 times 2^((s >> 2) - 1), plus 3.
|
|
357
|
+
const mant: i32 = toI32(4) + (s & 3)
|
|
358
|
+
switch (s >> 2) {
|
|
359
|
+
case 2:
|
|
360
|
+
return mant * 2 + 3
|
|
361
|
+
case 3:
|
|
362
|
+
return mant * 4 + 3
|
|
363
|
+
case 4:
|
|
364
|
+
return mant * 8 + 3
|
|
365
|
+
case 5:
|
|
366
|
+
return mant * 16 + 3
|
|
367
|
+
default:
|
|
368
|
+
return mant * 32 + 3
|
|
369
|
+
}
|
|
346
370
|
}
|
|
347
371
|
|
|
348
372
|
/**
|
|
@@ -362,9 +386,36 @@ const deflateDistBase = (d: i32): i32 => {
|
|
|
362
386
|
if (d >= 29) {
|
|
363
387
|
return 24577
|
|
364
388
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
389
|
+
// 2 or 3 times 2^((d >> 1) - 1), plus 1.
|
|
390
|
+
const mant: i32 = toI32(2) + (d & 1)
|
|
391
|
+
switch (d >> 1) {
|
|
392
|
+
case 2:
|
|
393
|
+
return mant * 2 + 1
|
|
394
|
+
case 3:
|
|
395
|
+
return mant * 4 + 1
|
|
396
|
+
case 4:
|
|
397
|
+
return mant * 8 + 1
|
|
398
|
+
case 5:
|
|
399
|
+
return mant * 16 + 1
|
|
400
|
+
case 6:
|
|
401
|
+
return mant * 32 + 1
|
|
402
|
+
case 7:
|
|
403
|
+
return mant * 64 + 1
|
|
404
|
+
case 8:
|
|
405
|
+
return mant * 128 + 1
|
|
406
|
+
case 9:
|
|
407
|
+
return mant * 256 + 1
|
|
408
|
+
case 10:
|
|
409
|
+
return mant * 512 + 1
|
|
410
|
+
case 11:
|
|
411
|
+
return mant * 1024 + 1
|
|
412
|
+
case 12:
|
|
413
|
+
return mant * 2048 + 1
|
|
414
|
+
case 13:
|
|
415
|
+
return mant * 4096 + 1
|
|
416
|
+
default:
|
|
417
|
+
return mant * 8192 + 1
|
|
418
|
+
}
|
|
368
419
|
}
|
|
369
420
|
|
|
370
421
|
/** The extra bits of each distance symbol (§3.2.5). */
|
|
@@ -450,8 +501,10 @@ const deflateFixedLitCode = (s: i32): i32 => {
|
|
|
450
501
|
* Fills `codes[0 .. n)` with the canonical code (§3.2.2) of the lengths
|
|
451
502
|
* `lens[0 .. n)`, each already bit-reversed for the LSB-first stream. `count`
|
|
452
503
|
* and `next` are scratch of at least 16 entries. A length of 0 gets code 0.
|
|
453
|
-
* Answers false, filling nothing, when a table is smaller than that
|
|
454
|
-
* is past 288, the largest alphabet,
|
|
504
|
+
* Answers false, filling nothing, when a table is smaller than that, `n`
|
|
505
|
+
* is past 288, the largest alphabet, or the lengths oversubscribe the code;
|
|
506
|
+
* the lengths `deflateLengths` sets and every caller's tables rule all three
|
|
507
|
+
* out.
|
|
455
508
|
*/
|
|
456
509
|
const deflateCanonical = (lens: i32[], n: i32, codes: i32[], count: i32[], next: i32[]): boolean => {
|
|
457
510
|
if (
|
|
@@ -470,17 +523,24 @@ const deflateCanonical = (lens: i32[], n: i32, codes: i32[], count: i32[], next:
|
|
|
470
523
|
for (let s: i32 = 0; s < n; s += 1) {
|
|
471
524
|
const l: i32 = lens[s]
|
|
472
525
|
if (l >= 0 && l < 16) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
526
|
+
const c: i32 = count[l]
|
|
527
|
+
if (c >= 288) {
|
|
528
|
+
return false
|
|
529
|
+
}
|
|
530
|
+
count[l] = c + 1
|
|
476
531
|
}
|
|
477
532
|
}
|
|
478
533
|
count[0] = 0
|
|
479
534
|
let code: i32 = 0
|
|
480
535
|
for (let l: i32 = 1; l < 16; l += 1) {
|
|
481
|
-
//
|
|
482
|
-
//
|
|
483
|
-
|
|
536
|
+
// The codes of lengths below `l` take at most the first 2^(l - 1) of
|
|
537
|
+
// its prefixes, so a code that fits never passes 2^14 here; lengths
|
|
538
|
+
// that oversubscribe the code fail.
|
|
539
|
+
const c: i32 = count[l - 1]
|
|
540
|
+
if (c < 0 || c > 288 || code < 0 || code > 16384) {
|
|
541
|
+
return false
|
|
542
|
+
}
|
|
543
|
+
code = (code + c) << 1
|
|
484
544
|
next[l] = code
|
|
485
545
|
}
|
|
486
546
|
// Each length's next code is kept bit-reversed, as it is written, and
|
|
@@ -493,12 +553,12 @@ const deflateCanonical = (lens: i32[], n: i32, codes: i32[], count: i32[], next:
|
|
|
493
553
|
if (l > 0 && l < 16) {
|
|
494
554
|
const r: i32 = next[l]
|
|
495
555
|
codes[s] = r
|
|
496
|
-
// `
|
|
497
|
-
//
|
|
498
|
-
const top: i32 = (
|
|
556
|
+
// `top` is the `l` low bits. Flipping `r` from its highest zero below
|
|
557
|
+
// `l` up clears the ones above that zero and sets it.
|
|
558
|
+
const top: i32 = ~(toI32(-1) << l)
|
|
499
559
|
if (r !== top) {
|
|
500
|
-
const
|
|
501
|
-
next[l] =
|
|
560
|
+
const from: i32 = toI32(-1) << (31 - toI32(Math.clz32(toU32(r ^ top))))
|
|
561
|
+
next[l] = r ^ (top & from)
|
|
502
562
|
}
|
|
503
563
|
} else {
|
|
504
564
|
codes[s] = 0
|
|
@@ -521,7 +581,7 @@ export class DeflateHuffmanScratch {
|
|
|
521
581
|
/** @internal Used by `deflateLengths`. */
|
|
522
582
|
readonly tmpKeys: i32[]
|
|
523
583
|
/** @internal Used by `deflateLengths`. The radix pass's 256 buckets. */
|
|
524
|
-
readonly buckets:
|
|
584
|
+
readonly buckets: u16[]
|
|
525
585
|
/** @internal Used by `deflateLengths`. How many codes have each length, 0 to 32. */
|
|
526
586
|
readonly perLength: i32[]
|
|
527
587
|
|
|
@@ -531,7 +591,7 @@ export class DeflateHuffmanScratch {
|
|
|
531
591
|
this.keys = new Array<i32>(288)
|
|
532
592
|
this.tmpSyms = new Array<i32>(288)
|
|
533
593
|
this.tmpKeys = new Array<i32>(288)
|
|
534
|
-
this.buckets = new Array<
|
|
594
|
+
this.buckets = new Array<u16>(256)
|
|
535
595
|
this.perLength = new Array<i32>(33)
|
|
536
596
|
}
|
|
537
597
|
}
|
|
@@ -546,15 +606,16 @@ export class DeflateHuffmanScratch {
|
|
|
546
606
|
* one, so the code is always complete. Answers false, setting nothing, when
|
|
547
607
|
* the alphabet or `limit` is outside the scratch's sizes or a frequency is
|
|
548
608
|
* 2^16 or more, which every caller's tables and blocks rule out. Those
|
|
549
|
-
* bounds keep every weight below 2^25 and every count
|
|
550
|
-
*
|
|
609
|
+
* bounds keep every weight below 2^25 and every count at most 288, which
|
|
610
|
+
* the guards below check where the bounds prover reads them; one that fails
|
|
611
|
+
* answers false with `lens[0 .. n)` already zeroed.
|
|
551
612
|
*/
|
|
552
613
|
const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: DeflateHuffmanScratch): boolean => {
|
|
553
614
|
const syms: i32[] = w.syms
|
|
554
615
|
const keys: i32[] = w.keys
|
|
555
616
|
const tsyms: i32[] = w.tmpSyms
|
|
556
617
|
const tkeys: i32[] = w.tmpKeys
|
|
557
|
-
const buckets:
|
|
618
|
+
const buckets: u16[] = w.buckets
|
|
558
619
|
const per: i32[] = w.perLength
|
|
559
620
|
if (
|
|
560
621
|
n < 2 ||
|
|
@@ -574,12 +635,14 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
|
|
|
574
635
|
}
|
|
575
636
|
// The used symbols, by a two-pass radix sort on the 16-bit frequency.
|
|
576
637
|
let used: i32 = 0
|
|
638
|
+
let wide: i32 = 0
|
|
577
639
|
for (let s: i32 = 0; s < n; s += 1) {
|
|
578
640
|
const f: i32 = freq[s]
|
|
579
641
|
if (f > 65535) {
|
|
580
642
|
return false
|
|
581
643
|
}
|
|
582
644
|
if (f > 0) {
|
|
645
|
+
wide = wide | f
|
|
583
646
|
uncheckedSet(tsyms, used, s)
|
|
584
647
|
uncheckedSet(tkeys, used, f)
|
|
585
648
|
used += 1
|
|
@@ -606,10 +669,12 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
|
|
|
606
669
|
}
|
|
607
670
|
return true
|
|
608
671
|
}
|
|
609
|
-
|
|
610
|
-
|
|
672
|
+
// Every frequency below 256 needs only the low pass.
|
|
673
|
+
const passes: i32 = wide > 255 ? 2 : 1
|
|
674
|
+
for (let pass: i32 = 0; pass < passes; pass += 1) {
|
|
675
|
+
const shift: i32 = pass === 0 ? 0 : 8
|
|
611
676
|
for (let b: i32 = 0; b < 256; b += 1) {
|
|
612
|
-
uncheckedSet(buckets, b, 0)
|
|
677
|
+
uncheckedSet(buckets, b, toU16(0))
|
|
613
678
|
}
|
|
614
679
|
const fromSyms: i32[] = pass === 0 ? tsyms : syms
|
|
615
680
|
const fromKeys: i32[] = pass === 0 ? tkeys : keys
|
|
@@ -617,65 +682,106 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
|
|
|
617
682
|
const toKeys: i32[] = pass === 0 ? keys : tkeys
|
|
618
683
|
for (let i: i32 = 0; i < used; i += 1) {
|
|
619
684
|
const b: i32 = (uncheckedGet(fromKeys, i) >> shift) & 255
|
|
620
|
-
|
|
685
|
+
// A bucket holds at most `used`, at most 288, symbols.
|
|
686
|
+
const c: i32 = toI32(uncheckedGet(buckets, b))
|
|
687
|
+
if (c >= 288) {
|
|
688
|
+
return false
|
|
689
|
+
}
|
|
690
|
+
uncheckedSet(buckets, b, toU16(c + 1))
|
|
621
691
|
}
|
|
692
|
+
// Each bucket's first slot. The counts are 16-bit, so 256 of them cannot
|
|
693
|
+
// overflow `sum`. They are never negative, so every prefix is at most the
|
|
694
|
+
// total: when that is `used`, at most 288, no slot stored here lost a
|
|
695
|
+
// bit to `toU16`, and when it is not, the sort fails.
|
|
622
696
|
let sum: i32 = 0
|
|
623
697
|
for (let b: i32 = 0; b < 256; b += 1) {
|
|
624
|
-
const c:
|
|
625
|
-
uncheckedSet(buckets, b, sum)
|
|
626
|
-
sum
|
|
698
|
+
const c: u16 = uncheckedGet(buckets, b)
|
|
699
|
+
uncheckedSet(buckets, b, toU16(sum))
|
|
700
|
+
sum += toI32(c)
|
|
701
|
+
}
|
|
702
|
+
if (sum !== used) {
|
|
703
|
+
return false
|
|
627
704
|
}
|
|
628
705
|
for (let i: i32 = 0; i < used; i += 1) {
|
|
629
706
|
const k: i32 = uncheckedGet(fromKeys, i)
|
|
630
707
|
const b: i32 = (k >> shift) & 255
|
|
631
|
-
|
|
632
|
-
|
|
708
|
+
// Each bucket's next slot is below `used`, so inside the 288 entries
|
|
709
|
+
// `toSyms` and `toKeys` hold.
|
|
710
|
+
const at: i32 = toI32(uncheckedGet(buckets, b))
|
|
711
|
+
if (at >= 288) {
|
|
712
|
+
return false
|
|
713
|
+
}
|
|
714
|
+
uncheckedSet(buckets, b, toU16(at + 1))
|
|
633
715
|
uncheckedSet(toSyms, at, uncheckedGet(fromSyms, i))
|
|
634
716
|
uncheckedSet(toKeys, at, k)
|
|
635
717
|
}
|
|
636
718
|
}
|
|
637
719
|
// Two passes leave the sorted run back in `tsyms` / `tkeys`.
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
720
|
+
if (passes === 2) {
|
|
721
|
+
for (let i: i32 = 0; i < used; i += 1) {
|
|
722
|
+
uncheckedSet(syms, i, uncheckedGet(tsyms, i))
|
|
723
|
+
uncheckedSet(keys, i, uncheckedGet(tkeys, i))
|
|
724
|
+
}
|
|
641
725
|
}
|
|
642
726
|
// Moffat and Katajainen, phase 1: each internal node's weight, and each
|
|
643
|
-
// node's parent, in place over the ascending weights.
|
|
644
|
-
|
|
727
|
+
// node's parent, in place over the ascending weights. A weight is at most
|
|
728
|
+
// the sum of 288 frequencies below 2^16, so below 2^25.
|
|
729
|
+
const w0: i32 = uncheckedGet(keys, 0)
|
|
730
|
+
const w1: i32 = uncheckedGet(keys, 1)
|
|
731
|
+
if (w0 < 0 || w0 > 33554431 || w1 < 0 || w1 > 33554431) {
|
|
732
|
+
return false
|
|
733
|
+
}
|
|
734
|
+
uncheckedSet(keys, 0, w0 + w1)
|
|
645
735
|
let root: i32 = 0
|
|
646
736
|
let leaf: i32 = 2
|
|
647
737
|
for (let next: i32 = 1; next < used - 1; next += 1) {
|
|
738
|
+
// `root` names an internal node, below `next`, so below 287.
|
|
739
|
+
if (root > 286) {
|
|
740
|
+
return false
|
|
741
|
+
}
|
|
648
742
|
if (leaf >= used || uncheckedGet(keys, root) < uncheckedGet(keys, leaf)) {
|
|
649
743
|
uncheckedSet(keys, next, uncheckedGet(keys, root))
|
|
650
744
|
uncheckedSet(keys, root, next)
|
|
651
|
-
root
|
|
745
|
+
root += 1
|
|
652
746
|
} else {
|
|
653
747
|
uncheckedSet(keys, next, uncheckedGet(keys, leaf))
|
|
654
748
|
leaf += 1
|
|
655
749
|
}
|
|
750
|
+
if (root > 286) {
|
|
751
|
+
return false
|
|
752
|
+
}
|
|
753
|
+
const first: i32 = uncheckedGet(keys, next)
|
|
656
754
|
if (leaf >= used || (root < next && uncheckedGet(keys, root) < uncheckedGet(keys, leaf))) {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
)
|
|
755
|
+
const second: i32 = uncheckedGet(keys, root)
|
|
756
|
+
if (first < 0 || first > 33554431 || second < 0 || second > 33554431) {
|
|
757
|
+
return false
|
|
758
|
+
}
|
|
759
|
+
uncheckedSet(keys, next, first + second)
|
|
662
760
|
uncheckedSet(keys, root, next)
|
|
663
|
-
root
|
|
761
|
+
root += 1
|
|
664
762
|
} else {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
)
|
|
763
|
+
const second: i32 = uncheckedGet(keys, leaf)
|
|
764
|
+
if (first < 0 || first > 33554431 || second < 0 || second > 33554431) {
|
|
765
|
+
return false
|
|
766
|
+
}
|
|
767
|
+
uncheckedSet(keys, next, first + second)
|
|
670
768
|
leaf += 1
|
|
671
769
|
}
|
|
672
770
|
}
|
|
673
|
-
// Phase 2: each internal node's depth
|
|
771
|
+
// Phase 2: each internal node's depth, one more than its parent's, which
|
|
772
|
+
// is below the 287 internal nodes.
|
|
674
773
|
uncheckedSet(keys, used - 2, 0)
|
|
675
774
|
for (let next: i32 = used - 3; next >= 0; next -= 1) {
|
|
676
|
-
|
|
775
|
+
const up: i32 = uncheckedGet(keys, uncheckedGet(keys, next))
|
|
776
|
+
if (up > 286) {
|
|
777
|
+
return false
|
|
778
|
+
}
|
|
779
|
+
uncheckedSet(keys, next, up + 1)
|
|
677
780
|
}
|
|
678
|
-
// Phase 3: how many leaves sit at each depth
|
|
781
|
+
// Phase 3: how many leaves sit at each depth, every depth past `limit`
|
|
782
|
+
// counted at `limit + 1`, which the length limit below folds into `limit`
|
|
783
|
+
// whatever the depth. A tree of at most 288 leaves is under 288 deep and
|
|
784
|
+
// has under 288 internal nodes at any depth.
|
|
679
785
|
for (let l: i32 = 0; l < 33; l += 1) {
|
|
680
786
|
per[l] = 0
|
|
681
787
|
}
|
|
@@ -683,18 +789,28 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
|
|
|
683
789
|
let usedHere: i32 = 0
|
|
684
790
|
let depth: i32 = 0
|
|
685
791
|
root = used - 2
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
792
|
+
while (avail > 0) {
|
|
793
|
+
if (depth < 0 || depth >= 288) {
|
|
794
|
+
return false
|
|
795
|
+
}
|
|
796
|
+
while (root >= 0 && uncheckedGet(keys, root) === depth) {
|
|
797
|
+
if (usedHere >= 288) {
|
|
798
|
+
return false
|
|
799
|
+
}
|
|
690
800
|
usedHere += 1
|
|
691
801
|
root -= 1
|
|
692
802
|
}
|
|
803
|
+
let slot: i32 = limit + 1
|
|
804
|
+
if (depth <= limit) {
|
|
805
|
+
slot = depth
|
|
806
|
+
}
|
|
693
807
|
// `usedHere` is never negative, so `avail > 0` only restates the first test.
|
|
694
808
|
while (avail > usedHere && avail > 0) {
|
|
695
|
-
|
|
696
|
-
|
|
809
|
+
const c: i32 = per[slot]
|
|
810
|
+
if (c >= 288) {
|
|
811
|
+
return false
|
|
697
812
|
}
|
|
813
|
+
per[slot] = c + 1
|
|
698
814
|
avail -= 1
|
|
699
815
|
}
|
|
700
816
|
avail = usedHere << 1
|
|
@@ -703,42 +819,71 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
|
|
|
703
819
|
}
|
|
704
820
|
// The length limit: fold the deeper codes into `limit`, then lengthen
|
|
705
821
|
// shorter codes until the Kraft sum is exactly 2^limit again.
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
822
|
+
const over: i32 = per[limit + 1]
|
|
823
|
+
if (over > 0) {
|
|
824
|
+
const kept: i32 = per[limit]
|
|
825
|
+
if (kept > 288 || over > 288) {
|
|
826
|
+
return false
|
|
710
827
|
}
|
|
711
|
-
|
|
828
|
+
per[limit] = kept + over
|
|
829
|
+
per[limit + 1] = 0
|
|
830
|
+
// The Kraft sum in units of 2^-limit, by Horner's rule: after length
|
|
831
|
+
// `l` it is at most 288 << (l - 1), below 2^23 for `l` up to 15.
|
|
712
832
|
let total: i32 = 0
|
|
713
|
-
for (let l: i32 =
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
833
|
+
for (let l: i32 = 1; l <= limit; l += 1) {
|
|
834
|
+
const c: i32 = per[l]
|
|
835
|
+
if (c < 0 || c > 288 || total < 0 || total > 8388607) {
|
|
836
|
+
return false
|
|
837
|
+
}
|
|
838
|
+
total = total * 2 + c
|
|
839
|
+
}
|
|
840
|
+
// Each pass takes one off the sum: a code of `limit` bits goes, and one
|
|
841
|
+
// of a shorter length `l` becomes two of `l + 1`.
|
|
842
|
+
const want: i32 = toI32(1) << limit
|
|
843
|
+
// `want` is at least 2, so `total > 0` only restates the first test.
|
|
844
|
+
while (total > want && total > 0) {
|
|
845
|
+
const most: i32 = per[limit]
|
|
846
|
+
if (most < 1) {
|
|
847
|
+
return false
|
|
848
|
+
}
|
|
849
|
+
per[limit] = most - 1
|
|
850
|
+
let l: i32 = limit - 1
|
|
851
|
+
while (l > 0 && per[l] === 0) {
|
|
852
|
+
l -= 1
|
|
853
|
+
}
|
|
854
|
+
if (l <= 0) {
|
|
855
|
+
return false
|
|
856
|
+
}
|
|
857
|
+
const c: i32 = per[l]
|
|
858
|
+
const below: i32 = per[l + 1]
|
|
859
|
+
if (c < 1 || below > 286) {
|
|
860
|
+
return false
|
|
726
861
|
}
|
|
862
|
+
per[l] = c - 1
|
|
863
|
+
per[l + 1] = below + 2
|
|
727
864
|
total -= 1
|
|
728
865
|
}
|
|
866
|
+
if (total !== want) {
|
|
867
|
+
return false
|
|
868
|
+
}
|
|
729
869
|
}
|
|
730
|
-
// The most frequent symbols take the shortest lengths
|
|
870
|
+
// The most frequent symbols take the shortest lengths; the counts per
|
|
871
|
+
// length add up to `used`.
|
|
731
872
|
let j: i32 = used
|
|
732
873
|
for (let l: i32 = 1; l <= limit; l += 1) {
|
|
733
|
-
for (let c: i32 = per[l]; c > 0
|
|
874
|
+
for (let c: i32 = per[l]; c > 0; c -= 1) {
|
|
875
|
+
if (j <= 0) {
|
|
876
|
+
return false
|
|
877
|
+
}
|
|
734
878
|
j -= 1
|
|
735
879
|
const s: i32 = uncheckedGet(syms, j)
|
|
736
|
-
if (s
|
|
737
|
-
|
|
880
|
+
if (s < 0 || s >= n) {
|
|
881
|
+
return false
|
|
738
882
|
}
|
|
883
|
+
lens[s] = l
|
|
739
884
|
}
|
|
740
885
|
}
|
|
741
|
-
return
|
|
886
|
+
return j === 0
|
|
742
887
|
}
|
|
743
888
|
|
|
744
889
|
/**
|
|
@@ -772,20 +917,25 @@ export class Deflater {
|
|
|
772
917
|
private readonly fixedDistLens: i32[]
|
|
773
918
|
private readonly fixedDistCodes: i32[]
|
|
774
919
|
/** Length - 3 to length symbol - 257; distance to distance symbol, as `deflateDistSymbol` reads it. */
|
|
775
|
-
private readonly lengthSymbol:
|
|
776
|
-
private readonly distSymbol:
|
|
777
|
-
/**
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
920
|
+
private readonly lengthSymbol: u8[]
|
|
921
|
+
private readonly distSymbol: u8[]
|
|
922
|
+
/**
|
|
923
|
+
* Base and extra bits of each length and distance symbol. A base is a
|
|
924
|
+
* `u16` so the writer's `length - base` is an `i32` difference whose
|
|
925
|
+
* range the prover knows without a guard.
|
|
926
|
+
*/
|
|
927
|
+
private readonly lengthBase: u16[]
|
|
928
|
+
private readonly lengthExtra: u8[]
|
|
929
|
+
private readonly distBase: u16[]
|
|
930
|
+
private readonly distExtra: u8[]
|
|
782
931
|
/** Canonical-code and length-building scratch. */
|
|
783
932
|
private readonly countScratch: i32[]
|
|
784
933
|
private readonly nextScratch: i32[]
|
|
785
934
|
private readonly scratch: DeflateHuffmanScratch
|
|
786
935
|
/** Bits written but not yet stored, low bit first, and how many. */
|
|
787
936
|
private bitBuf: u64
|
|
788
|
-
|
|
937
|
+
/** 0 to 31 between writes; every reader checks it, as a field has no proven range. */
|
|
938
|
+
private bitCount: i32
|
|
789
939
|
/** Set when a store would have run past `out`: the call answers `DEFLATE_NO_ROOM`. */
|
|
790
940
|
private overrun: boolean
|
|
791
941
|
/** @internal Used by `GzipCompressor`, `GzipWriter` (std/compress/gzip.ts) and `ZlibCompressor` (std/compress/zlib.ts). The level, 0 to 9. */
|
|
@@ -893,12 +1043,12 @@ export class Deflater {
|
|
|
893
1043
|
this.fixedLitCodes = new Array<i32>(288)
|
|
894
1044
|
this.fixedDistLens = new Array<i32>(32)
|
|
895
1045
|
this.fixedDistCodes = new Array<i32>(32)
|
|
896
|
-
this.lengthSymbol = new Array<
|
|
897
|
-
this.distSymbol = new Array<
|
|
898
|
-
this.lengthBase = new Array<
|
|
899
|
-
this.lengthExtra = new Array<
|
|
900
|
-
this.distBase = new Array<
|
|
901
|
-
this.distExtra = new Array<
|
|
1046
|
+
this.lengthSymbol = new Array<u8>(256)
|
|
1047
|
+
this.distSymbol = new Array<u8>(512)
|
|
1048
|
+
this.lengthBase = new Array<u16>(29)
|
|
1049
|
+
this.lengthExtra = new Array<u8>(29)
|
|
1050
|
+
this.distBase = new Array<u16>(30)
|
|
1051
|
+
this.distExtra = new Array<u8>(30)
|
|
902
1052
|
this.countScratch = new Array<i32>(16)
|
|
903
1053
|
this.nextScratch = new Array<i32>(16)
|
|
904
1054
|
this.scratch = new DeflateHuffmanScratch()
|
|
@@ -906,8 +1056,8 @@ export class Deflater {
|
|
|
906
1056
|
this.bitCount = 0
|
|
907
1057
|
this.overrun = false
|
|
908
1058
|
for (let s: i32 = 0; s < 29; s += 1) {
|
|
909
|
-
this.lengthBase[s] = deflateLengthBase(s)
|
|
910
|
-
this.lengthExtra[s] = deflateLengthExtra(s)
|
|
1059
|
+
this.lengthBase[s] = toU16(deflateLengthBase(s))
|
|
1060
|
+
this.lengthExtra[s] = toU8(deflateLengthExtra(s))
|
|
911
1061
|
}
|
|
912
1062
|
for (let s: i32 = 0; s < 29; s += 1) {
|
|
913
1063
|
const lo: i32 = deflateLengthBase(s) - 3
|
|
@@ -920,12 +1070,12 @@ export class Deflater {
|
|
|
920
1070
|
hi = 255
|
|
921
1071
|
}
|
|
922
1072
|
for (let l: i32 = lo; l < hi && l >= 0 && l < 256; l += 1) {
|
|
923
|
-
this.lengthSymbol[l] = s
|
|
1073
|
+
this.lengthSymbol[l] = toU8(s)
|
|
924
1074
|
}
|
|
925
1075
|
}
|
|
926
1076
|
for (let d: i32 = 0; d < 30; d += 1) {
|
|
927
|
-
this.distBase[d] = deflateDistBase(d)
|
|
928
|
-
this.distExtra[d] = deflateDistExtra(d)
|
|
1077
|
+
this.distBase[d] = toU16(deflateDistBase(d))
|
|
1078
|
+
this.distExtra[d] = toU8(deflateDistExtra(d))
|
|
929
1079
|
}
|
|
930
1080
|
// Distances 1 to 256 directly; above, by (distance - 1) >> 7 from 256 on.
|
|
931
1081
|
for (let d: i32 = 0; d < 30; d += 1) {
|
|
@@ -937,11 +1087,11 @@ export class Deflater {
|
|
|
937
1087
|
}
|
|
938
1088
|
for (let dist: i32 = lo; dist < hi && dist >= 1 && dist <= 32768; dist += 1) {
|
|
939
1089
|
if (dist <= 256) {
|
|
940
|
-
this.distSymbol[dist - 1] = d
|
|
1090
|
+
this.distSymbol[dist - 1] = toU8(d)
|
|
941
1091
|
} else {
|
|
942
1092
|
const at: i32 = ((dist - 1) >> 7) + 256
|
|
943
1093
|
if (at < 512) {
|
|
944
|
-
this.distSymbol[at] = d
|
|
1094
|
+
this.distSymbol[at] = toU8(d)
|
|
945
1095
|
}
|
|
946
1096
|
}
|
|
947
1097
|
}
|
|
@@ -993,8 +1143,7 @@ export class Deflater {
|
|
|
993
1143
|
this.bitBuf = 0
|
|
994
1144
|
this.bitCount = 0
|
|
995
1145
|
this.overrun = false
|
|
996
|
-
|
|
997
|
-
const srcEnd: i32 = toI32(toU32(off) + toU32(len))
|
|
1146
|
+
const srcEnd: i32 = deflateWindowEnd(src, off, len)
|
|
998
1147
|
this.begin(off)
|
|
999
1148
|
const end: i32 = this.compressRange(src, off, srcEnd, off, true, out, at, toI32(out.length))
|
|
1000
1149
|
this.finish(srcEnd)
|
|
@@ -1083,11 +1232,10 @@ export class Deflater {
|
|
|
1083
1232
|
const v: i32 = p + delta
|
|
1084
1233
|
return v < 0 ? -1 : v
|
|
1085
1234
|
}
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
return toI32(toU32(p) + toU32(delta))
|
|
1235
|
+
// `p + delta`, as 2147483647 less the room left above it: each step
|
|
1236
|
+
// subtracts two values from zero up, so none can overflow.
|
|
1237
|
+
const gap: i32 = 2147483647 - delta - p
|
|
1238
|
+
return gap < 0 ? -1 : 2147483647 - gap
|
|
1091
1239
|
}
|
|
1092
1240
|
|
|
1093
1241
|
/**
|
|
@@ -1102,14 +1250,22 @@ export class Deflater {
|
|
|
1102
1250
|
|
|
1103
1251
|
/** `flushBits` without settling `overrun`: a step inside a write. */
|
|
1104
1252
|
private writeBits(out: u8[], op: i32, oe: i32): i32 {
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1253
|
+
const bc: i32 = this.bitCount
|
|
1254
|
+
if (bc < 0 || bc > 63 || op < 0 || oe < 0 || oe > toI32(out.length)) {
|
|
1255
|
+
return DEFLATE_NO_ROOM
|
|
1256
|
+
}
|
|
1257
|
+
const bytes: i32 = (bc + 7) >> 3
|
|
1258
|
+
// `op + bytes`, as `oe` less the room the bytes leave: each step is
|
|
1259
|
+
// checked to be from zero up, so none can overflow.
|
|
1260
|
+
const room: i32 = oe - op
|
|
1261
|
+
if (room < 0) {
|
|
1262
|
+
return DEFLATE_NO_ROOM
|
|
1263
|
+
}
|
|
1264
|
+
const gap: i32 = room - bytes
|
|
1265
|
+
if (gap < 0) {
|
|
1109
1266
|
return DEFLATE_NO_ROOM
|
|
1110
1267
|
}
|
|
1111
|
-
|
|
1112
|
-
const end: i32 = toI32(toU32(op) + toU32(bytes))
|
|
1268
|
+
const end: i32 = oe - gap
|
|
1113
1269
|
let p: i32 = op
|
|
1114
1270
|
let bb: u64 = this.bitBuf
|
|
1115
1271
|
while (p < end) {
|
|
@@ -1128,10 +1284,16 @@ export class Deflater {
|
|
|
1128
1284
|
* has checked the room.
|
|
1129
1285
|
*/
|
|
1130
1286
|
private putBits(out: u8[], op: i32, v: i32, n: i32): i32 {
|
|
1131
|
-
|
|
1132
|
-
|
|
1287
|
+
const c: i32 = this.bitCount
|
|
1288
|
+
if (c < 0 || c > 31 || n < 0 || n > 32) {
|
|
1289
|
+
// No caller passes more than 32 bits, and every write leaves under 32.
|
|
1290
|
+
this.overrun = true
|
|
1291
|
+
return op
|
|
1292
|
+
}
|
|
1293
|
+
let bb: u64 = this.bitBuf | (toU64(toU32(v)) << toU64(c))
|
|
1294
|
+
let bc: i32 = c + n
|
|
1133
1295
|
let p: i32 = op
|
|
1134
|
-
if (bc >=
|
|
1296
|
+
if (bc >= 32) {
|
|
1135
1297
|
if (p >= 0 && p <= toI32(out.length) - 4) {
|
|
1136
1298
|
uncheckedSet(out, p, toU8(bb & toU64(255)))
|
|
1137
1299
|
uncheckedSet(out, p + 1, toU8((bb >> toU64(8)) & toU64(255)))
|
|
@@ -1142,7 +1304,7 @@ export class Deflater {
|
|
|
1142
1304
|
this.overrun = true
|
|
1143
1305
|
}
|
|
1144
1306
|
bb = bb >> toU64(32)
|
|
1145
|
-
bc -=
|
|
1307
|
+
bc -= 32
|
|
1146
1308
|
}
|
|
1147
1309
|
this.bitBuf = bb
|
|
1148
1310
|
this.bitCount = bc
|
|
@@ -1193,7 +1355,8 @@ export class Deflater {
|
|
|
1193
1355
|
return o
|
|
1194
1356
|
}
|
|
1195
1357
|
// An empty final block, in the fixed code: ten bits.
|
|
1196
|
-
|
|
1358
|
+
const bc: i32 = this.bitCount
|
|
1359
|
+
if (bc < 0 || bc > 31 || (bc + 17) >> 3 > oe - o) {
|
|
1197
1360
|
return this.settle(toI32(-1))
|
|
1198
1361
|
}
|
|
1199
1362
|
o = this.putBits(out, o, toI32(3), toI32(3))
|
|
@@ -1249,19 +1412,31 @@ export class Deflater {
|
|
|
1249
1412
|
return
|
|
1250
1413
|
}
|
|
1251
1414
|
const shift: i32 = v - (DEFLATE_WINDOW + 1)
|
|
1252
|
-
// A
|
|
1415
|
+
// A table entry is a position or 0, the never-a-candidate fill; only a
|
|
1416
|
+
// positive one shifts, and one that falls out of the window becomes 0.
|
|
1253
1417
|
const head: i32[] = this.head
|
|
1254
1418
|
const prev: i32[] = this.prev
|
|
1255
1419
|
for (let i: i32 = 0; i < toI32(head.length); i += 1) {
|
|
1256
|
-
const
|
|
1257
|
-
|
|
1420
|
+
const e: i32 = head[i]
|
|
1421
|
+
let h: i32 = 0
|
|
1422
|
+
if (e > shift) {
|
|
1423
|
+
h = e - shift
|
|
1424
|
+
}
|
|
1425
|
+
head[i] = h
|
|
1258
1426
|
}
|
|
1259
1427
|
for (let i: i32 = 0; i < toI32(prev.length); i += 1) {
|
|
1260
|
-
const
|
|
1261
|
-
|
|
1428
|
+
const e: i32 = prev[i]
|
|
1429
|
+
let h: i32 = 0
|
|
1430
|
+
if (e > shift) {
|
|
1431
|
+
h = e - shift
|
|
1432
|
+
}
|
|
1433
|
+
prev[i] = h
|
|
1262
1434
|
}
|
|
1263
1435
|
this.delta = 32769 - p // DEFLATE_WINDOW + 1 - p
|
|
1264
|
-
|
|
1436
|
+
// `base` is a position, never negative; a stream's next `begin` restarts
|
|
1437
|
+
// the tables from any `base` below the window's floor.
|
|
1438
|
+
const base: i32 = this.base
|
|
1439
|
+
this.base = base > shift ? base - shift : 0
|
|
1265
1440
|
}
|
|
1266
1441
|
|
|
1267
1442
|
/**
|
|
@@ -1285,16 +1460,20 @@ export class Deflater {
|
|
|
1285
1460
|
) {
|
|
1286
1461
|
return -1
|
|
1287
1462
|
}
|
|
1288
|
-
const
|
|
1289
|
-
|
|
1290
|
-
|
|
1463
|
+
const minMatch: i32 = this.minMatch
|
|
1464
|
+
if (minMatch < 3 || minMatch > 4) {
|
|
1465
|
+
return -1
|
|
1466
|
+
}
|
|
1467
|
+
const v: i32 = this.position(p)
|
|
1468
|
+
if (v < 0) {
|
|
1469
|
+
return -1
|
|
1470
|
+
}
|
|
1291
1471
|
const at: integer<0, 2147483639> = p
|
|
1292
1472
|
const word: u32 = deflateLoad32(src, at)
|
|
1293
1473
|
const h: i32 =
|
|
1294
1474
|
minMatch === 3
|
|
1295
1475
|
? toI32(((word << 8) * toU32(DEFLATE_HASH_MUL)) >>> 17)
|
|
1296
1476
|
: toI32((word * toU32(DEFLATE_HASH_MUL)) >>> 17)
|
|
1297
|
-
const v: i32 = wrappingAdd(p, delta)
|
|
1298
1477
|
let cand: i32 = uncheckedGet(head, h)
|
|
1299
1478
|
uncheckedSet(head, h, v)
|
|
1300
1479
|
uncheckedSet(prev, v & (DEFLATE_WINDOW - 1), cand)
|
|
@@ -1304,45 +1483,75 @@ export class Deflater {
|
|
|
1304
1483
|
const maxLen: i32 = roomy ? DEFLATE_MAX_MATCH : end - p
|
|
1305
1484
|
let best: i32 = minMatch - 1
|
|
1306
1485
|
let bestDist: i32 = 0
|
|
1307
|
-
//
|
|
1308
|
-
//
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1486
|
+
// Past 2,147,483,389 a match runs within 258 bytes of the largest
|
|
1487
|
+
// array's end; `p` is recorded but reported as matching nothing, a valid
|
|
1488
|
+
// if shorter answer, and the bound is what lets `p + best` be a proven sum.
|
|
1489
|
+
if (p > 2147483389) {
|
|
1490
|
+
return 0
|
|
1491
|
+
}
|
|
1492
|
+
if (histStart < 0) {
|
|
1493
|
+
return -1
|
|
1494
|
+
}
|
|
1495
|
+
const back: i32 = p - histStart
|
|
1496
|
+
if (back < 0) {
|
|
1497
|
+
return -1
|
|
1498
|
+
}
|
|
1499
|
+
// A candidate is at most a window back and no further than `histStart`:
|
|
1500
|
+
// its distance, `v` less its position, is 1 to `span`.
|
|
1501
|
+
const span: i32 = back < DEFLATE_WINDOW ? back : DEFLATE_WINDOW
|
|
1312
1502
|
const nice: i32 = this.niceLength < maxLen ? this.niceLength : maxLen
|
|
1313
1503
|
const mask: u32 = minMatch === 3 ? toU32(0xffffff) : ~toU32(0)
|
|
1314
1504
|
let chain: i32 = this.maxChain
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1505
|
+
// A position is never negative, so `cand >= 0` only tells the prover so;
|
|
1506
|
+
// nor is a match longer than 258 (DEFLATE_MAX_MATCH, spelled out for the
|
|
1507
|
+
// prover), and the bound on `best` tells it that `p + best` fits.
|
|
1508
|
+
while (cand >= 0 && chain > 0 && best >= 0 && best < 258) {
|
|
1509
|
+
// The byte that would make a match the longest yet: most candidates
|
|
1510
|
+
// fail on it, so the scan for one that has it holds `best` still.
|
|
1511
|
+
const b: i32 = best
|
|
1512
|
+
const want: u8 = uncheckedGet(src, p + b)
|
|
1513
|
+
let hit: i32 = 0
|
|
1514
|
+
while (cand >= 0 && chain > 0) {
|
|
1515
|
+
const dist: i32 = v - cand
|
|
1516
|
+
if (dist < 1 || dist > span) {
|
|
1517
|
+
break
|
|
1518
|
+
}
|
|
1519
|
+
// `cand`'s input index, `cand - delta`; `span` keeps it at or past
|
|
1520
|
+
// `histStart`, and the check tells the prover so.
|
|
1521
|
+
const cd: i32 = p - dist
|
|
1522
|
+
if (cd < 0) {
|
|
1523
|
+
break
|
|
1524
|
+
}
|
|
1525
|
+
if (uncheckedGet(src, cd + b) === want) {
|
|
1526
|
+
hit = dist
|
|
1527
|
+
break
|
|
1528
|
+
}
|
|
1529
|
+
cand = uncheckedGet(prev, cand & (DEFLATE_WINDOW - 1))
|
|
1530
|
+
chain -= 1
|
|
1531
|
+
}
|
|
1532
|
+
// `hit` is a distance the scan checked, 1 to `span`, or 0 for none,
|
|
1533
|
+
// and a scan stops on one with `chain` left; the check tells the
|
|
1534
|
+
// prover so.
|
|
1535
|
+
if (hit < 1 || hit > span || chain < 1) {
|
|
1536
|
+
break
|
|
1537
|
+
}
|
|
1538
|
+
const cd: i32 = p - hit
|
|
1539
|
+
if (cd < 0) {
|
|
1319
1540
|
break
|
|
1320
1541
|
}
|
|
1321
1542
|
const ci: integer<0, 2147483639> = cd
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
if (
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
? deflateCtz64(x) >> 3
|
|
1335
|
-
: toI32(toU32(8) + toU32(deflateMatchLength(src, ci + 8, p + 8, DEFLATE_MAX_MATCH - 8)))
|
|
1336
|
-
} else if (((deflateLoad32(src, ci) ^ word) & mask) === 0) {
|
|
1337
|
-
l = deflateMatchLength(src, ci, p, maxLen)
|
|
1338
|
-
}
|
|
1339
|
-
if (l > best) {
|
|
1340
|
-
best = l
|
|
1341
|
-
// Cannot wrap: `cand` is below `v`, by at most the window.
|
|
1342
|
-
bestDist = toI32(toU32(v) - toU32(cand))
|
|
1343
|
-
if (l >= nice) {
|
|
1344
|
-
break
|
|
1345
|
-
}
|
|
1543
|
+
let l: i32 = 0
|
|
1544
|
+
if (roomy) {
|
|
1545
|
+
const x: u64 = deflateLoad64(src, ci) ^ deflateLoad64(src, at)
|
|
1546
|
+
l = x !== toU64(0) ? deflateCtz64(x) >> 3 : deflateMatchLength(src, ci, p, DEFLATE_MAX_MATCH)
|
|
1547
|
+
} else if (((deflateLoad32(src, ci) ^ word) & mask) === 0) {
|
|
1548
|
+
l = deflateMatchLength(src, ci, p, maxLen)
|
|
1549
|
+
}
|
|
1550
|
+
if (l > best) {
|
|
1551
|
+
best = l
|
|
1552
|
+
bestDist = hit
|
|
1553
|
+
if (l >= nice) {
|
|
1554
|
+
break
|
|
1346
1555
|
}
|
|
1347
1556
|
}
|
|
1348
1557
|
cand = uncheckedGet(prev, cand & (DEFLATE_WINDOW - 1))
|
|
@@ -1379,8 +1588,8 @@ export class Deflater {
|
|
|
1379
1588
|
const tokens: i32[] = this.tokens
|
|
1380
1589
|
const lf: i32[] = this.litFreq
|
|
1381
1590
|
const df: i32[] = this.distFreq
|
|
1382
|
-
const lsym:
|
|
1383
|
-
const dsym:
|
|
1591
|
+
const lsym: u8[] = this.lengthSymbol
|
|
1592
|
+
const dsym: u8[] = this.distSymbol
|
|
1384
1593
|
if (
|
|
1385
1594
|
toI32(head.length) !== DEFLATE_HASH_SIZE ||
|
|
1386
1595
|
toI32(prev.length) !== DEFLATE_WINDOW ||
|
|
@@ -1392,69 +1601,76 @@ export class Deflater {
|
|
|
1392
1601
|
) {
|
|
1393
1602
|
return -1
|
|
1394
1603
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
const lo: i32 = toI32(toU32(histStart) + toU32(delta))
|
|
1604
|
+
if (start < 0 || end < 0 || end > toI32(src.length) || histStart < 0) {
|
|
1605
|
+
return -1
|
|
1606
|
+
}
|
|
1399
1607
|
const maxChain: i32 = this.maxChain
|
|
1400
1608
|
const niceLength: i32 = this.niceLength
|
|
1401
1609
|
const room: i32 = DEFLATE_BLOCK_TOKENS - 1
|
|
1402
|
-
const last: i32 =
|
|
1403
|
-
const full: i32 =
|
|
1610
|
+
const last: i32 = end - 4
|
|
1611
|
+
const full: i32 = end - DEFLATE_MAX_MATCH
|
|
1404
1612
|
let n: i32 = 0
|
|
1405
1613
|
let p: i32 = start
|
|
1406
1614
|
while (p < end && n < room) {
|
|
1407
1615
|
let best: i32 = 0
|
|
1408
1616
|
let bestDist: i32 = 0
|
|
1409
|
-
|
|
1410
|
-
|
|
1617
|
+
let at0: i32 = 0
|
|
1618
|
+
// Past 2,147,483,381 a match runs within 266 bytes of the largest
|
|
1619
|
+
// array's end; such a `p` is taken as a literal, and the bound is what
|
|
1620
|
+
// lets the match's sums be proven.
|
|
1621
|
+
if (p >= 0 && p >= histStart && p <= last && p <= 2147483381) {
|
|
1622
|
+
const v: i32 = this.position(p)
|
|
1623
|
+
if (v < 0) {
|
|
1624
|
+
return -1
|
|
1625
|
+
}
|
|
1626
|
+
at0 = v
|
|
1627
|
+
const at: integer<0, 2147483639> = p
|
|
1628
|
+
const word: u32 = deflateLoad32(src, at)
|
|
1411
1629
|
const h: i32 = toI32((word * toU32(DEFLATE_HASH_MUL)) >>> 17)
|
|
1412
|
-
const v: i32 = toI32(toU32(p) + toU32(delta))
|
|
1413
1630
|
let cand: i32 = uncheckedGet(head, h)
|
|
1414
1631
|
uncheckedSet(head, h, v)
|
|
1415
1632
|
uncheckedSet(prev, v & (DEFLATE_WINDOW - 1), cand)
|
|
1416
|
-
//
|
|
1417
|
-
const
|
|
1633
|
+
// `maxLen - 4`, which `p` at most `last` keeps at least 0.
|
|
1634
|
+
const tail: i32 = p <= full ? DEFLATE_MAX_MATCH - 4 : last - p
|
|
1635
|
+
const maxLen: i32 = p <= full ? DEFLATE_MAX_MATCH : end - p
|
|
1418
1636
|
const nice: i32 = niceLength < maxLen ? niceLength : maxLen
|
|
1419
|
-
|
|
1637
|
+
// A candidate is at most a window back and no further than
|
|
1638
|
+
// `histStart`: its distance, `v` less its position, is 1 to `span`.
|
|
1639
|
+
const back: i32 = p - histStart
|
|
1640
|
+
const span: i32 = back < DEFLATE_WINDOW ? back : DEFLATE_WINDOW
|
|
1420
1641
|
let chain: i32 = maxChain
|
|
1421
|
-
while (cand >=
|
|
1422
|
-
|
|
1423
|
-
|
|
1642
|
+
while (cand >= 0 && chain > 0) {
|
|
1643
|
+
const dist: i32 = v - cand
|
|
1644
|
+
if (dist < 1 || dist > span) {
|
|
1645
|
+
break
|
|
1646
|
+
}
|
|
1647
|
+
// `cand`'s input index, `cand - delta`, at or past `histStart`.
|
|
1648
|
+
const ci: i32 = p - dist
|
|
1424
1649
|
if (ci >= 0 && ci <= 2147483639 && deflateLoad32(src, ci) === word) {
|
|
1425
1650
|
let l: i32 = 0
|
|
1426
1651
|
if (p <= full && ci <= 2147483627) {
|
|
1427
1652
|
const a4: i32 = ci + 4
|
|
1428
1653
|
const b4: i32 = p + 4
|
|
1429
|
-
|
|
1430
|
-
if (
|
|
1431
|
-
|
|
1654
|
+
const x: u64 = deflateLoad64(src, a4) ^ deflateLoad64(src, b4)
|
|
1655
|
+
if (x !== toU64(0)) {
|
|
1656
|
+
l = toI32(4) + (deflateCtz64(x) >> 3)
|
|
1657
|
+
} else {
|
|
1658
|
+
const m: i32 = deflateMatchLength(src, ci + 12, p + 12, DEFLATE_MAX_MATCH - 12)
|
|
1659
|
+
if (m < 0 || m > 246) {
|
|
1660
|
+
return -1
|
|
1661
|
+
}
|
|
1662
|
+
l = 12 + m
|
|
1432
1663
|
}
|
|
1433
|
-
// Cannot wrap: `p` is at most `end - 258`, `maxLen` is at least
|
|
1434
|
-
// 4 (`p` is at most `last`), and a match length is at most 258.
|
|
1435
|
-
l =
|
|
1436
|
-
x !== toU64(0)
|
|
1437
|
-
? toI32(4) + (deflateCtz64(x) >> 3)
|
|
1438
|
-
: toI32(
|
|
1439
|
-
toU32(12) +
|
|
1440
|
-
toU32(
|
|
1441
|
-
deflateMatchLength(
|
|
1442
|
-
src,
|
|
1443
|
-
ci + 12,
|
|
1444
|
-
toI32(toU32(p) + toU32(12)),
|
|
1445
|
-
DEFLATE_MAX_MATCH - 12
|
|
1446
|
-
)
|
|
1447
|
-
)
|
|
1448
|
-
)
|
|
1449
1664
|
} else {
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1665
|
+
const m: i32 = deflateMatchLength(src, ci + 4, p + 4, tail)
|
|
1666
|
+
if (m < 0 || m > 254) {
|
|
1667
|
+
return -1
|
|
1668
|
+
}
|
|
1669
|
+
l = 4 + m
|
|
1453
1670
|
}
|
|
1454
1671
|
if (l > best) {
|
|
1455
1672
|
best = l
|
|
1456
|
-
|
|
1457
|
-
bestDist = toI32(toU32(v) - toU32(cand))
|
|
1673
|
+
bestDist = dist
|
|
1458
1674
|
if (l >= nice) {
|
|
1459
1675
|
break
|
|
1460
1676
|
}
|
|
@@ -1465,24 +1681,37 @@ export class Deflater {
|
|
|
1465
1681
|
}
|
|
1466
1682
|
}
|
|
1467
1683
|
if (best >= 4) {
|
|
1468
|
-
//
|
|
1469
|
-
//
|
|
1470
|
-
//
|
|
1471
|
-
|
|
1684
|
+
// A match is 4 to 258 bytes at a distance of 1 to the window, and
|
|
1685
|
+
// starts where the search ran; the checks tell the prover so, with
|
|
1686
|
+
// DEFLATE_MAX_MATCH and DEFLATE_WINDOW spelled out for it.
|
|
1687
|
+
const len: i32 = best
|
|
1688
|
+
const dist: i32 = bestDist
|
|
1689
|
+
const v: i32 = at0
|
|
1690
|
+
if (p < 0 || p > 2147483381 || len > 258 || dist < 1 || dist > 32768 || v < 0 || v > 2147483389) {
|
|
1691
|
+
return -1
|
|
1692
|
+
}
|
|
1693
|
+
uncheckedSet(tokens, n, (dist << 9) | (253 + len))
|
|
1472
1694
|
n += 1
|
|
1473
|
-
const ls: i32 =
|
|
1474
|
-
|
|
1475
|
-
const
|
|
1476
|
-
|
|
1477
|
-
|
|
1695
|
+
const ls: i32 = 257 + toI32(uncheckedGet(lsym, len - 3))
|
|
1696
|
+
const lc: i32 = uncheckedGet(lf, ls)
|
|
1697
|
+
const ds: i32 = toI32(
|
|
1698
|
+
dist <= 256 ? uncheckedGet(dsym, dist - 1) : uncheckedGet(dsym, ((dist - 1) >> 7) + 256)
|
|
1699
|
+
)
|
|
1700
|
+
const dc: i32 = uncheckedGet(df, ds)
|
|
1701
|
+
if (lc >= DEFLATE_BLOCK_TOKENS || dc >= DEFLATE_BLOCK_TOKENS) {
|
|
1702
|
+
return -1
|
|
1703
|
+
}
|
|
1704
|
+
uncheckedSet(lf, ls, lc + 1)
|
|
1705
|
+
uncheckedSet(df, ds, dc + 1)
|
|
1478
1706
|
// Every position the match covers goes into the chains.
|
|
1479
|
-
const stop: i32 =
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
const
|
|
1485
|
-
vq =
|
|
1707
|
+
const stop: i32 = p + len
|
|
1708
|
+
// The positions up to `last` that the match covers, as `p + k` with
|
|
1709
|
+
// their positions `v + k`.
|
|
1710
|
+
const cnt: i32 = stop <= last ? len : end - p - 3
|
|
1711
|
+
for (let k: i32 = 1; k < cnt && k < 258; k += 1) {
|
|
1712
|
+
const q: i32 = p + k
|
|
1713
|
+
const vq: i32 = v + k
|
|
1714
|
+
const hq: i32 = toI32((deflateLoad32(src, q) * toU32(DEFLATE_HASH_MUL)) >>> 17)
|
|
1486
1715
|
uncheckedSet(prev, vq & (DEFLATE_WINDOW - 1), uncheckedGet(head, hq))
|
|
1487
1716
|
uncheckedSet(head, hq, vq)
|
|
1488
1717
|
}
|
|
@@ -1491,8 +1720,11 @@ export class Deflater {
|
|
|
1491
1720
|
const b: i32 = toI32(uncheckedGet(src, p))
|
|
1492
1721
|
uncheckedSet(tokens, n, b)
|
|
1493
1722
|
n += 1
|
|
1494
|
-
|
|
1495
|
-
|
|
1723
|
+
const c: i32 = uncheckedGet(lf, b)
|
|
1724
|
+
if (c >= DEFLATE_BLOCK_TOKENS) {
|
|
1725
|
+
return -1
|
|
1726
|
+
}
|
|
1727
|
+
uncheckedSet(lf, b, c + 1)
|
|
1496
1728
|
p += 1
|
|
1497
1729
|
}
|
|
1498
1730
|
}
|
|
@@ -1515,8 +1747,8 @@ export class Deflater {
|
|
|
1515
1747
|
const tokens: i32[] = this.tokens
|
|
1516
1748
|
const lf: i32[] = this.litFreq
|
|
1517
1749
|
const df: i32[] = this.distFreq
|
|
1518
|
-
const lsym:
|
|
1519
|
-
const dsym:
|
|
1750
|
+
const lsym: u8[] = this.lengthSymbol
|
|
1751
|
+
const dsym: u8[] = this.distSymbol
|
|
1520
1752
|
if (
|
|
1521
1753
|
toI32(head.length) !== DEFLATE_HASH_SIZE ||
|
|
1522
1754
|
toI32(prev.length) !== DEFLATE_WINDOW ||
|
|
@@ -1531,8 +1763,10 @@ export class Deflater {
|
|
|
1531
1763
|
const three: boolean = this.minMatch === 3
|
|
1532
1764
|
const lazyLength: i32 = this.lazyLength
|
|
1533
1765
|
const room: i32 = DEFLATE_BLOCK_TOKENS - 2
|
|
1534
|
-
|
|
1535
|
-
|
|
1766
|
+
if (start < 0 || end < 0) {
|
|
1767
|
+
return -1
|
|
1768
|
+
}
|
|
1769
|
+
const last: i32 = end - 4
|
|
1536
1770
|
let n: i32 = 0
|
|
1537
1771
|
let p: i32 = start
|
|
1538
1772
|
// The match `longest` found one position on, when it beat the one at
|
|
@@ -1543,25 +1777,23 @@ export class Deflater {
|
|
|
1543
1777
|
let inserted: i32 = p
|
|
1544
1778
|
// A carried match is written even when the block is otherwise full: the
|
|
1545
1779
|
// room leaves two tokens of slack.
|
|
1546
|
-
while (p < end && (n < room || carry !== 0)) {
|
|
1780
|
+
while (p < end && n <= room && (n < room || carry !== 0)) {
|
|
1547
1781
|
let m: i32 = carry
|
|
1548
1782
|
carry = 0
|
|
1549
1783
|
if (m === 0 && p <= last) {
|
|
1550
1784
|
m = this.longest(src, p, end, histStart)
|
|
1551
|
-
|
|
1552
|
-
inserted = toI32(toU32(p) + toU32(1))
|
|
1785
|
+
inserted = p + 1
|
|
1553
1786
|
}
|
|
1554
1787
|
if (m < 0) {
|
|
1555
1788
|
return -1
|
|
1556
1789
|
}
|
|
1557
1790
|
if (m !== 0 && m >> 16 < lazyLength && p < last && n < room) {
|
|
1558
|
-
|
|
1559
|
-
const p1: i32 = toI32(toU32(p) + toU32(1))
|
|
1791
|
+
const p1: i32 = p + 1
|
|
1560
1792
|
const m2: i32 = this.longest(src, p1, end, histStart)
|
|
1561
1793
|
if (m2 < 0) {
|
|
1562
1794
|
return -1
|
|
1563
1795
|
}
|
|
1564
|
-
inserted =
|
|
1796
|
+
inserted = p + 2
|
|
1565
1797
|
if (m2 >> 16 > m >> 16) {
|
|
1566
1798
|
carry = m2
|
|
1567
1799
|
m = 0
|
|
@@ -1570,42 +1802,61 @@ export class Deflater {
|
|
|
1570
1802
|
if (m === 0) {
|
|
1571
1803
|
const b: i32 = toI32(uncheckedGet(src, p))
|
|
1572
1804
|
uncheckedSet(tokens, n, b)
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1805
|
+
n += 1
|
|
1806
|
+
const c: i32 = uncheckedGet(lf, b)
|
|
1807
|
+
if (c >= DEFLATE_BLOCK_TOKENS) {
|
|
1808
|
+
return -1
|
|
1809
|
+
}
|
|
1810
|
+
uncheckedSet(lf, b, c + 1)
|
|
1577
1811
|
p += 1
|
|
1578
1812
|
} else {
|
|
1579
1813
|
const len: i32 = m >> 16
|
|
1580
1814
|
const dist: i32 = m & 0xffff
|
|
1581
|
-
if (len < 3 || len > DEFLATE_MAX_MATCH || dist < 1 || dist > DEFLATE_WINDOW) {
|
|
1815
|
+
if (p < 0 || len < 3 || len > DEFLATE_MAX_MATCH || dist < 1 || dist > DEFLATE_WINDOW) {
|
|
1582
1816
|
return -1
|
|
1583
1817
|
}
|
|
1584
1818
|
uncheckedSet(tokens, n, (dist << 9) | (253 + len))
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
const ls: i32 = toI32(toU32(257) + toU32(uncheckedGet(lsym, len - 3)))
|
|
1590
|
-
uncheckedSet(lf, ls, toI32(toU32(uncheckedGet(lf, ls)) + toU32(1)))
|
|
1591
|
-
const ds: i32 =
|
|
1819
|
+
n += 1
|
|
1820
|
+
const ls: i32 = 257 + toI32(uncheckedGet(lsym, len - 3))
|
|
1821
|
+
const lc: i32 = uncheckedGet(lf, ls)
|
|
1822
|
+
const ds: i32 = toI32(
|
|
1592
1823
|
dist <= 256 ? uncheckedGet(dsym, dist - 1) : uncheckedGet(dsym, ((dist - 1) >> 7) + 256)
|
|
1593
|
-
|
|
1824
|
+
)
|
|
1825
|
+
const dc: i32 = uncheckedGet(df, ds)
|
|
1826
|
+
if (lc >= DEFLATE_BLOCK_TOKENS || dc >= DEFLATE_BLOCK_TOKENS) {
|
|
1827
|
+
return -1
|
|
1828
|
+
}
|
|
1829
|
+
uncheckedSet(lf, ls, lc + 1)
|
|
1830
|
+
uncheckedSet(df, ds, dc + 1)
|
|
1594
1831
|
// The positions the match covers that `longest` did not record go
|
|
1595
1832
|
// into the chains.
|
|
1596
|
-
|
|
1833
|
+
// `p + len` as `end` less the room past the match, which `longest`
|
|
1834
|
+
// keeps from going negative.
|
|
1835
|
+
const left: i32 = end - p
|
|
1836
|
+
if (left < 0) {
|
|
1837
|
+
return -1
|
|
1838
|
+
}
|
|
1839
|
+
const over: i32 = left - len
|
|
1840
|
+
if (over < 0) {
|
|
1841
|
+
return -1
|
|
1842
|
+
}
|
|
1843
|
+
const stop: i32 = end - over
|
|
1597
1844
|
const insertEnd: i32 = stop <= last ? stop : last + 1
|
|
1598
|
-
|
|
1845
|
+
let q: i32 = inserted
|
|
1846
|
+
let vq: i32 = this.position(q)
|
|
1847
|
+
while (q < insertEnd && q >= 0 && q <= 2147483639 && vq >= 0 && vq < 2147483647) {
|
|
1599
1848
|
const qa: integer<0, 2147483639> = q
|
|
1600
1849
|
const word: u32 = deflateLoad32(src, qa)
|
|
1601
1850
|
const hq: i32 = three
|
|
1602
1851
|
? toI32(((word << 8) * toU32(DEFLATE_HASH_MUL)) >>> 17)
|
|
1603
1852
|
: toI32((word * toU32(DEFLATE_HASH_MUL)) >>> 17)
|
|
1604
|
-
// Cannot wrap: `rebase` keeps a position in the chains' numbering
|
|
1605
|
-
// between 0 and 2^31 - 1.
|
|
1606
|
-
const vq: i32 = toI32(toU32(q) + toU32(this.delta))
|
|
1607
1853
|
uncheckedSet(prev, vq & (DEFLATE_WINDOW - 1), uncheckedGet(head, hq))
|
|
1608
1854
|
uncheckedSet(head, hq, vq)
|
|
1855
|
+
q += 1
|
|
1856
|
+
vq += 1
|
|
1857
|
+
}
|
|
1858
|
+
if (q < insertEnd) {
|
|
1859
|
+
return -1
|
|
1609
1860
|
}
|
|
1610
1861
|
p = stop
|
|
1611
1862
|
}
|
|
@@ -1621,14 +1872,15 @@ export class Deflater {
|
|
|
1621
1872
|
* bytes of `LEN` / `NLEN`. The answer is under 2^18.
|
|
1622
1873
|
*/
|
|
1623
1874
|
private storedExtra(n: i32, pending: i32): i32 {
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1875
|
+
if (n < 0 || pending < 0 || pending > 63) {
|
|
1876
|
+
return -1
|
|
1877
|
+
}
|
|
1878
|
+
// The pieces after the first, each of 65,535 bytes but the last.
|
|
1879
|
+
const more: i32 = n > 65535 ? (n - 1) / 65535 : 0
|
|
1880
|
+
if (more < 0 || more > 32768) {
|
|
1881
|
+
return -1
|
|
1629
1882
|
}
|
|
1630
|
-
|
|
1631
|
-
return ((((pending & 63) + 10) >> 3) & 15) + 4 + (more & 0x7fff) * 5
|
|
1883
|
+
return ((pending + 10) >> 3) + 4 + more * 5
|
|
1632
1884
|
}
|
|
1633
1885
|
|
|
1634
1886
|
/**
|
|
@@ -1638,43 +1890,59 @@ export class Deflater {
|
|
|
1638
1890
|
* not fit in `out[op .. oe)` or the ranges are backwards or negative.
|
|
1639
1891
|
*/
|
|
1640
1892
|
writeStored(src: u8[], start: i32, end: i32, final: boolean, out: u8[], op: i32, oe: i32): i32 {
|
|
1641
|
-
if (start < 0 || end < start || op < 0 || oe < op) {
|
|
1893
|
+
if (start < 0 || end < start || end < 0 || op < 0 || oe < op) {
|
|
1642
1894
|
return -1
|
|
1643
1895
|
}
|
|
1644
|
-
|
|
1645
|
-
const
|
|
1646
|
-
const
|
|
1647
|
-
|
|
1648
|
-
const extra: i32 = this.storedExtra(n, toI32(this.bitCount)) & 0x3ffff
|
|
1649
|
-
// Cannot wrap: `extra <= room` by the first test.
|
|
1650
|
-
if (room < extra || n > toI32(toU32(room) - toU32(extra))) {
|
|
1896
|
+
const n: i32 = end - start
|
|
1897
|
+
const room: i32 = oe - op
|
|
1898
|
+
const extra: i32 = this.storedExtra(n, this.bitCount)
|
|
1899
|
+
if (extra < 0 || room < extra || n > room - extra) {
|
|
1651
1900
|
return -1
|
|
1652
1901
|
}
|
|
1653
|
-
|
|
1902
|
+
const len: i32 = toI32(out.length)
|
|
1903
|
+
if (len < 0) {
|
|
1904
|
+
return -1
|
|
1905
|
+
}
|
|
1906
|
+
const lim: i32 = len - 4
|
|
1654
1907
|
let left: i32 = n
|
|
1655
1908
|
let o: i32 = op
|
|
1656
1909
|
let first: boolean = true
|
|
1657
1910
|
while (first || left > 0) {
|
|
1658
1911
|
first = false
|
|
1659
|
-
//
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1912
|
+
// A stored block's length is 16 bits: 65,535.
|
|
1913
|
+
const l0: i32 = left
|
|
1914
|
+
if (l0 < 0 || l0 > n) {
|
|
1915
|
+
return -1
|
|
1916
|
+
}
|
|
1917
|
+
const take: i32 = l0 < 65535 ? l0 : 65535
|
|
1918
|
+
if (take < 0 || take > 65535) {
|
|
1919
|
+
return -1
|
|
1920
|
+
}
|
|
1921
|
+
o = this.putBits(out, o, toI32(final && take === l0 ? 1 : 0), toI32(3))
|
|
1922
|
+
const at: i32 = this.alignBits(out, o)
|
|
1923
|
+
o = at
|
|
1924
|
+
const p: i32 = end - l0
|
|
1925
|
+
if (at >= 0 && at <= lim && tryCopyBytes(out, at + 4, src, p, take)) {
|
|
1926
|
+
uncheckedSet(out, at, toU8(take & 255))
|
|
1927
|
+
uncheckedSet(out, at + 1, toU8(take >> 8))
|
|
1928
|
+
uncheckedSet(out, at + 2, toU8(~take & 255))
|
|
1929
|
+
uncheckedSet(out, at + 3, toU8((~take >> 8) & 255))
|
|
1930
|
+
// The copy put `take` bytes after `at + 4` inside `out`, so `after`
|
|
1931
|
+
// is not negative and the new position is at most `len`.
|
|
1932
|
+
const gap: i32 = lim - at
|
|
1933
|
+
const after: i32 = gap < 0 ? -1 : gap - take
|
|
1934
|
+
if (after < 0) {
|
|
1935
|
+
return -1
|
|
1936
|
+
}
|
|
1937
|
+
o = len - after
|
|
1671
1938
|
} else {
|
|
1672
1939
|
this.overrun = true
|
|
1673
1940
|
}
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1941
|
+
const rem: i32 = l0 - take
|
|
1942
|
+
if (rem < 0) {
|
|
1943
|
+
return -1
|
|
1944
|
+
}
|
|
1945
|
+
left = rem
|
|
1678
1946
|
}
|
|
1679
1947
|
return this.settle(o)
|
|
1680
1948
|
}
|
|
@@ -1682,9 +1950,9 @@ export class Deflater {
|
|
|
1682
1950
|
/**
|
|
1683
1951
|
* Builds the run-length coded lengths of the dynamic header (§3.2.7) for
|
|
1684
1952
|
* `hlit` literal/length and `hdist` distance lengths, and their
|
|
1685
|
-
* frequencies in `clFreq`. Answers false,
|
|
1686
|
-
*
|
|
1687
|
-
* constructor
|
|
1953
|
+
* frequencies in `clFreq`. Answers false, the scratch then unusable, when
|
|
1954
|
+
* it is not its size, the counts are past the format's or a length is past
|
|
1955
|
+
* 15, which the constructor, `writeBlock` and the length builder rule out.
|
|
1688
1956
|
*/
|
|
1689
1957
|
private buildRle(hlit: i32, hdist: i32): boolean {
|
|
1690
1958
|
const rle: i32[] = this.rle
|
|
@@ -1702,70 +1970,115 @@ export class Deflater {
|
|
|
1702
1970
|
) {
|
|
1703
1971
|
return false
|
|
1704
1972
|
}
|
|
1705
|
-
// At most 320 lengths, each giving at most one entry
|
|
1706
|
-
//
|
|
1707
|
-
//
|
|
1973
|
+
// At most 320 lengths, each giving at most one entry, and each count is
|
|
1974
|
+
// at most the number of entries: the checks below fail only on a scratch
|
|
1975
|
+
// or length table this writer did not fill.
|
|
1708
1976
|
const total: i32 = hlit + hdist
|
|
1709
1977
|
let n: i32 = 0
|
|
1710
1978
|
let i: i32 = 0
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1979
|
+
while (i < total) {
|
|
1980
|
+
const at: i32 = i
|
|
1981
|
+
if (at < 0) {
|
|
1982
|
+
return false
|
|
1983
|
+
}
|
|
1984
|
+
const l: i32 = at < hlit ? uncheckedGet(ll, at) : uncheckedGet(dl, at - hlit)
|
|
1714
1985
|
let run: i32 = 1
|
|
1715
1986
|
while (
|
|
1716
1987
|
run < 320 &&
|
|
1717
|
-
|
|
1718
|
-
(
|
|
1988
|
+
at + run < total &&
|
|
1989
|
+
(at + run < hlit ? uncheckedGet(ll, at + run) : uncheckedGet(dl, at + run - hlit)) === l
|
|
1719
1990
|
) {
|
|
1720
1991
|
run += 1
|
|
1721
1992
|
}
|
|
1722
|
-
|
|
1723
|
-
|
|
1993
|
+
const k: i32 = run
|
|
1994
|
+
if (k < 1 || k > 320) {
|
|
1995
|
+
return false
|
|
1996
|
+
}
|
|
1997
|
+
i = at + k
|
|
1998
|
+
let left: i32 = k
|
|
1724
1999
|
if (l === 0) {
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
const
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
2000
|
+
while (left >= 11) {
|
|
2001
|
+
const r0: i32 = left
|
|
2002
|
+
const r: i32 = r0 < 138 ? r0 : 138
|
|
2003
|
+
const at18: i32 = n
|
|
2004
|
+
const f18: i32 = freq[18]
|
|
2005
|
+
if (r < 11 || r > 138 || r0 > 320 || at18 < 0 || at18 >= 320 || f18 < 0 || f18 >= 320) {
|
|
2006
|
+
return false
|
|
2007
|
+
}
|
|
2008
|
+
uncheckedSet(rle, at18, ((r - 11) << 8) | 18)
|
|
2009
|
+
n = at18 + 1
|
|
2010
|
+
freq[18] = f18 + 1
|
|
2011
|
+
left = r0 - r
|
|
2012
|
+
}
|
|
2013
|
+
const r3: i32 = left
|
|
2014
|
+
if (r3 >= 3) {
|
|
2015
|
+
const at17: i32 = n
|
|
2016
|
+
const f17: i32 = freq[17]
|
|
2017
|
+
if (r3 > 10 || at17 < 0 || at17 >= 320 || f17 < 0 || f17 >= 320) {
|
|
2018
|
+
return false
|
|
2019
|
+
}
|
|
2020
|
+
uncheckedSet(rle, at17, ((r3 - 3) << 8) | 17)
|
|
2021
|
+
n = at17 + 1
|
|
2022
|
+
freq[17] = f17 + 1
|
|
2023
|
+
left = 0
|
|
1739
2024
|
}
|
|
1740
2025
|
} else {
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
uncheckedSet(freq, l, (uncheckedGet(freq, l) & 511) + 1)
|
|
2026
|
+
const next: i32 = this.rlePut(n, l)
|
|
2027
|
+
if (next < 0) {
|
|
2028
|
+
return false
|
|
1745
2029
|
}
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
while (
|
|
1749
|
-
const
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
2030
|
+
n = next
|
|
2031
|
+
left = k - 1
|
|
2032
|
+
while (left >= 3) {
|
|
2033
|
+
const r0: i32 = left
|
|
2034
|
+
const r: i32 = r0 < 6 ? r0 : 6
|
|
2035
|
+
const at16: i32 = n
|
|
2036
|
+
const f16: i32 = freq[16]
|
|
2037
|
+
if (r < 3 || r > 6 || r0 > 320 || at16 < 0 || at16 >= 320 || f16 < 0 || f16 >= 320) {
|
|
2038
|
+
return false
|
|
2039
|
+
}
|
|
2040
|
+
uncheckedSet(rle, at16, ((r - 3) << 8) | 16)
|
|
2041
|
+
n = at16 + 1
|
|
2042
|
+
freq[16] = f16 + 1
|
|
2043
|
+
left = r0 - r
|
|
1754
2044
|
}
|
|
1755
2045
|
}
|
|
1756
|
-
while (
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
uncheckedSet(freq, l, (uncheckedGet(freq, l) & 511) + 1)
|
|
2046
|
+
while (left > 0) {
|
|
2047
|
+
const next: i32 = this.rlePut(n, l)
|
|
2048
|
+
if (next < 0) {
|
|
2049
|
+
return false
|
|
1761
2050
|
}
|
|
1762
|
-
|
|
2051
|
+
n = next
|
|
2052
|
+
left -= 1
|
|
1763
2053
|
}
|
|
1764
2054
|
}
|
|
1765
2055
|
this.rleCount = n
|
|
1766
2056
|
return true
|
|
1767
2057
|
}
|
|
1768
2058
|
|
|
2059
|
+
/**
|
|
2060
|
+
* Appends length `l` at `rle[n]` and counts it in `clFreq`; answers -1,
|
|
2061
|
+
* writing nothing, when `n` is outside the 320-entry scratch, `l` is not a
|
|
2062
|
+
* code length 0 to 15, or its count would pass 320.
|
|
2063
|
+
* @param n The entry's position in `rle`.
|
|
2064
|
+
* @param l The code length to append.
|
|
2065
|
+
* @returns The next entry's position, or -1.
|
|
2066
|
+
*/
|
|
2067
|
+
private rlePut(n: i32, l: i32): i32 {
|
|
2068
|
+
const rle: i32[] = this.rle
|
|
2069
|
+
const freq: i32[] = this.clFreq
|
|
2070
|
+
if (n < 0 || n >= 320 || l < 0 || l > 15 || toI32(rle.length) < 320 || toI32(freq.length) < 19) {
|
|
2071
|
+
return -1
|
|
2072
|
+
}
|
|
2073
|
+
const f: i32 = uncheckedGet(freq, l)
|
|
2074
|
+
if (f < 0 || f >= 320) {
|
|
2075
|
+
return -1
|
|
2076
|
+
}
|
|
2077
|
+
uncheckedSet(rle, n, l)
|
|
2078
|
+
uncheckedSet(freq, l, f + 1)
|
|
2079
|
+
return n + 1
|
|
2080
|
+
}
|
|
2081
|
+
|
|
1769
2082
|
/**
|
|
1770
2083
|
* Writes the block's symbols, `src[start .. end)`'s, as the smallest of a
|
|
1771
2084
|
* dynamic, a fixed and a stored block; answers the new position, or -1
|
|
@@ -1846,49 +2159,124 @@ export class Deflater {
|
|
|
1846
2159
|
hclen -= 1
|
|
1847
2160
|
}
|
|
1848
2161
|
// The three sizes, exactly. Extra bits are the same in both codes. A
|
|
1849
|
-
// block holds at most 16,
|
|
1850
|
-
// bits, so every
|
|
1851
|
-
//
|
|
1852
|
-
//
|
|
2162
|
+
// block holds at most 16,384 symbols of at most 15 bits and 13 extra
|
|
2163
|
+
// bits, so every count is at most 16,384, every length and extra below
|
|
2164
|
+
// 16, and every sum below 2^24 (16,777,216); the checks tell the bounds
|
|
2165
|
+
// prover so, and fail only on tables the constructor and the match
|
|
2166
|
+
// finders rule out.
|
|
2167
|
+
const lens: i32 = hclen
|
|
2168
|
+
if (lens < 4 || lens > 19) {
|
|
2169
|
+
return -1
|
|
2170
|
+
}
|
|
1853
2171
|
let extra: i32 = 0
|
|
1854
|
-
let dynBits: i32 =
|
|
2172
|
+
let dynBits: i32 = lens * 3 + 17
|
|
1855
2173
|
let fixBits: i32 = 3
|
|
2174
|
+
const lx: u8[] = this.lengthExtra
|
|
2175
|
+
const dx: u8[] = this.distExtra
|
|
2176
|
+
const fl: i32[] = this.fixedLitLens
|
|
2177
|
+
if (toI32(lx.length) < 29 || toI32(dx.length) < 30 || toI32(fl.length) < 286 || toI32(df.length) < 30) {
|
|
2178
|
+
return -1
|
|
2179
|
+
}
|
|
1856
2180
|
for (let s: i32 = 0; s < 286; s += 1) {
|
|
1857
|
-
const f: i32 = uncheckedGet(lf, s)
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
2181
|
+
const f: i32 = uncheckedGet(lf, s)
|
|
2182
|
+
const dyn: i32 = uncheckedGet(ll, s)
|
|
2183
|
+
const fix: i32 = uncheckedGet(fl, s)
|
|
2184
|
+
const x: i32 = s >= 257 ? toI32(uncheckedGet(lx, s - 257)) : 0
|
|
2185
|
+
if (
|
|
2186
|
+
f < 0 ||
|
|
2187
|
+
f > 16384 ||
|
|
2188
|
+
dyn < 0 ||
|
|
2189
|
+
dyn > 15 ||
|
|
2190
|
+
fix < 0 ||
|
|
2191
|
+
fix > 15 ||
|
|
2192
|
+
x < 0 ||
|
|
2193
|
+
x > 15 ||
|
|
2194
|
+
dynBits < 0 ||
|
|
2195
|
+
dynBits > 16777215 ||
|
|
2196
|
+
fixBits < 0 ||
|
|
2197
|
+
fixBits > 16777215 ||
|
|
2198
|
+
extra < 0 ||
|
|
2199
|
+
extra > 16777215
|
|
2200
|
+
) {
|
|
2201
|
+
return -1
|
|
1862
2202
|
}
|
|
2203
|
+
dynBits += f * dyn
|
|
2204
|
+
fixBits += f * fix
|
|
2205
|
+
extra += f * x
|
|
1863
2206
|
}
|
|
1864
2207
|
for (let d: i32 = 0; d < 30; d += 1) {
|
|
1865
|
-
const f: i32 = uncheckedGet(df, d)
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
2208
|
+
const f: i32 = uncheckedGet(df, d)
|
|
2209
|
+
const dyn: i32 = uncheckedGet(dl, d)
|
|
2210
|
+
const x: i32 = toI32(uncheckedGet(dx, d))
|
|
2211
|
+
if (
|
|
2212
|
+
f < 0 ||
|
|
2213
|
+
f > 16384 ||
|
|
2214
|
+
dyn < 0 ||
|
|
2215
|
+
dyn > 15 ||
|
|
2216
|
+
x < 0 ||
|
|
2217
|
+
x > 15 ||
|
|
2218
|
+
dynBits < 0 ||
|
|
2219
|
+
dynBits > 16777215 ||
|
|
2220
|
+
fixBits < 0 ||
|
|
2221
|
+
fixBits > 16777215 ||
|
|
2222
|
+
extra < 0 ||
|
|
2223
|
+
extra > 16777215
|
|
2224
|
+
) {
|
|
2225
|
+
return -1
|
|
2226
|
+
}
|
|
2227
|
+
dynBits += f * dyn
|
|
2228
|
+
fixBits += f * 5
|
|
2229
|
+
extra += f * x
|
|
1869
2230
|
}
|
|
2231
|
+
// The header's code-length symbols, each with its code and, for 16, 17
|
|
2232
|
+
// and 18, its 2, 3 or 7 repeat bits; `buildRle` writes at most 320.
|
|
1870
2233
|
for (let s: i32 = 0; s < 19; s += 1) {
|
|
1871
|
-
|
|
2234
|
+
const f: i32 = cf[s]
|
|
2235
|
+
const len: i32 = cl[s]
|
|
2236
|
+
let rep: i32 = 0
|
|
2237
|
+
if (s === 16) {
|
|
2238
|
+
rep = 2
|
|
2239
|
+
} else if (s === 17) {
|
|
2240
|
+
rep = 3
|
|
2241
|
+
} else if (s === 18) {
|
|
2242
|
+
rep = 7
|
|
2243
|
+
}
|
|
2244
|
+
if (f < 0 || f > 320 || len < 0 || len > 7 || rep < 0 || rep > 7 || dynBits < 0 || dynBits > 16777215) {
|
|
2245
|
+
return -1
|
|
2246
|
+
}
|
|
2247
|
+
dynBits += f * (len + rep)
|
|
2248
|
+
}
|
|
2249
|
+
if (
|
|
2250
|
+
dynBits < 0 ||
|
|
2251
|
+
dynBits > 16777215 ||
|
|
2252
|
+
fixBits < 0 ||
|
|
2253
|
+
fixBits > 16777215 ||
|
|
2254
|
+
extra < 0 ||
|
|
2255
|
+
extra > 16777215
|
|
2256
|
+
) {
|
|
2257
|
+
return -1
|
|
2258
|
+
}
|
|
2259
|
+
const dynSize: i32 = dynBits + extra
|
|
2260
|
+
const fixSize: i32 = fixBits + extra
|
|
2261
|
+
const bc: i32 = this.bitCount
|
|
2262
|
+
if (bc < 0 || bc > 63) {
|
|
2263
|
+
return -1
|
|
1872
2264
|
}
|
|
1873
|
-
dynBits = (dynBits + (cf[16] & 0xffff) * 2 + (cf[17] & 0xffff) * 3 + (cf[18] & 0xffff) * 7) & 0xffffff
|
|
1874
|
-
dynBits = (dynBits + extra) & 0xffffff
|
|
1875
|
-
fixBits = (fixBits + extra) & 0xffffff
|
|
1876
|
-
const bc: i32 = toI32(this.bitCount) & 63
|
|
1877
2265
|
// The stored size, `8 * (n + storedExtra) - bc` bits, is at most a size
|
|
1878
2266
|
// `b` exactly when `n + storedExtra` is at most `(b + bc) >> 3` bytes.
|
|
1879
|
-
|
|
1880
|
-
const
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
const
|
|
2267
|
+
const n: i32 = end - start
|
|
2268
|
+
const storedExtra: i32 = this.storedExtra(n, bc)
|
|
2269
|
+
if (storedExtra < 0 || storedExtra > 262143) {
|
|
2270
|
+
return -1
|
|
2271
|
+
}
|
|
2272
|
+
const dynBytes: i32 = (dynSize + bc) >> 3
|
|
2273
|
+
const fixBytes: i32 = (fixSize + bc) >> 3
|
|
1885
2274
|
if (n <= dynBytes - storedExtra && n <= fixBytes - storedExtra) {
|
|
1886
2275
|
return this.writeStored(src, start, end, final, out, op, oe)
|
|
1887
2276
|
}
|
|
1888
|
-
const dynamic: boolean =
|
|
1889
|
-
const
|
|
1890
|
-
|
|
1891
|
-
if ((bc + bits + 7) >> 3 > toI32(toU32(oe) - toU32(op))) {
|
|
2277
|
+
const dynamic: boolean = dynSize < fixSize
|
|
2278
|
+
const bytes: i32 = dynamic ? (bc + dynSize + 7) >> 3 : (bc + fixSize + 7) >> 3
|
|
2279
|
+
if (bytes > oe - op) {
|
|
1892
2280
|
return -1
|
|
1893
2281
|
}
|
|
1894
2282
|
let o: i32 = this.putBits(
|
|
@@ -1898,14 +2286,15 @@ export class Deflater {
|
|
|
1898
2286
|
toI32(3)
|
|
1899
2287
|
)
|
|
1900
2288
|
if (dynamic) {
|
|
1901
|
-
// `hlit
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
2289
|
+
// `hlit` and `hdist` are 257 to 286 and 1 to 30; the check tells the
|
|
2290
|
+
// bounds prover so.
|
|
2291
|
+
const nl: i32 = hlit
|
|
2292
|
+
const nd: i32 = hdist
|
|
2293
|
+
if (nl < 257 || nl > 286 || nd < 1 || nd > 30) {
|
|
2294
|
+
return -1
|
|
2295
|
+
}
|
|
2296
|
+
o = this.putBits(out, o, (nl - 257) | ((nd - 1) << 5) | ((lens - 4) << 10), toI32(14))
|
|
2297
|
+
for (let i: i32 = 0; i < lens; i += 1) {
|
|
1909
2298
|
const k: i32 = deflateClOrder(i)
|
|
1910
2299
|
if (k < 0 || k >= 19) {
|
|
1911
2300
|
return -1
|
|
@@ -1952,12 +2341,12 @@ export class Deflater {
|
|
|
1952
2341
|
private writeSymbols(lc: i32[], ll: i32[], dc: i32[], dl: i32[], out: u8[], op: i32): i32 {
|
|
1953
2342
|
const tokens: i32[] = this.tokens
|
|
1954
2343
|
const n: i32 = this.count
|
|
1955
|
-
const lsym:
|
|
1956
|
-
const lbase:
|
|
1957
|
-
const lextra:
|
|
1958
|
-
const dsym:
|
|
1959
|
-
const dbase:
|
|
1960
|
-
const dextra:
|
|
2344
|
+
const lsym: u8[] = this.lengthSymbol
|
|
2345
|
+
const lbase: u16[] = this.lengthBase
|
|
2346
|
+
const lextra: u8[] = this.lengthExtra
|
|
2347
|
+
const dsym: u8[] = this.distSymbol
|
|
2348
|
+
const dbase: u16[] = this.distBase
|
|
2349
|
+
const dextra: u8[] = this.distExtra
|
|
1961
2350
|
if (
|
|
1962
2351
|
toI32(lc.length) < 288 ||
|
|
1963
2352
|
toI32(ll.length) < 288 ||
|
|
@@ -1973,26 +2362,45 @@ export class Deflater {
|
|
|
1973
2362
|
) {
|
|
1974
2363
|
return -1
|
|
1975
2364
|
}
|
|
2365
|
+
// Each token adds at most 15 + 5 bits of length and 15 + 13 of
|
|
2366
|
+
// distance, flushing a word after each half, so the count stays under
|
|
2367
|
+
// 64; the checks tell the bounds prover so, and fail only on tables the
|
|
2368
|
+
// constructor rules out.
|
|
1976
2369
|
let bb: u64 = this.bitBuf
|
|
1977
|
-
let bc:
|
|
2370
|
+
let bc: i32 = this.bitCount
|
|
1978
2371
|
let o: i32 = op
|
|
1979
2372
|
for (let i: i32 = 0; i < n; i += 1) {
|
|
2373
|
+
const c0: i32 = bc
|
|
2374
|
+
if (c0 < 0 || c0 > 31) {
|
|
2375
|
+
return -1
|
|
2376
|
+
}
|
|
1980
2377
|
const t: i32 = uncheckedGet(tokens, i)
|
|
2378
|
+
let c: i32 = c0
|
|
1981
2379
|
if (t < 256) {
|
|
1982
|
-
|
|
1983
|
-
|
|
2380
|
+
const a: i32 = uncheckedGet(ll, t)
|
|
2381
|
+
if (a < 0 || a > 15) {
|
|
2382
|
+
return -1
|
|
2383
|
+
}
|
|
2384
|
+
bb = bb | (toU64(toU32(uncheckedGet(lc, t))) << toU64(c0))
|
|
2385
|
+
c = c0 + a
|
|
1984
2386
|
} else {
|
|
1985
|
-
|
|
2387
|
+
// The low byte of a match token is its length less 3: bits 0 to 8 hold
|
|
2388
|
+
// 253 + length, 256 to 511, so bit 8 is always set.
|
|
2389
|
+
const len3: i32 = t & 255
|
|
1986
2390
|
const dist: i32 = t >> 9
|
|
1987
|
-
const s: i32 = uncheckedGet(lsym, len3)
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
2391
|
+
const s: i32 = toI32(uncheckedGet(lsym, len3))
|
|
2392
|
+
const ls: i32 = 257 + s
|
|
2393
|
+
const a: i32 = uncheckedGet(ll, ls)
|
|
2394
|
+
if (a < 0 || a > 15) {
|
|
2395
|
+
return -1
|
|
2396
|
+
}
|
|
2397
|
+
const x: i32 = toI32(uncheckedGet(lextra, s))
|
|
2398
|
+
bb = bb | (toU64(toU32(uncheckedGet(lc, ls))) << toU64(c0))
|
|
2399
|
+
const c1: i32 = c0 + a
|
|
2400
|
+
bb = bb | (toU64(toU32(len3 + 3 - toI32(uncheckedGet(lbase, s)))) << toU64(c1))
|
|
2401
|
+
const c2: i32 = c1 + x
|
|
2402
|
+
let c3: i32 = c2
|
|
2403
|
+
if (c2 >= 32) {
|
|
1996
2404
|
if (o >= 0 && o <= toI32(out.length) - 4) {
|
|
1997
2405
|
uncheckedSet(out, o, toU8(bb))
|
|
1998
2406
|
uncheckedSet(out, o + 1, toU8(bb >> toU64(8)))
|
|
@@ -2003,16 +2411,23 @@ export class Deflater {
|
|
|
2003
2411
|
this.overrun = true
|
|
2004
2412
|
}
|
|
2005
2413
|
bb = bb >> toU64(32)
|
|
2006
|
-
|
|
2414
|
+
c3 = c2 - 32
|
|
2007
2415
|
}
|
|
2008
|
-
const d: i32 =
|
|
2416
|
+
const d: i32 = toI32(
|
|
2009
2417
|
dist <= 256 ? uncheckedGet(dsym, dist - 1) : uncheckedGet(dsym, ((dist - 1) >> 7) + 256)
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2418
|
+
)
|
|
2419
|
+
const da: i32 = uncheckedGet(dl, d)
|
|
2420
|
+
if (da < 0 || da > 15) {
|
|
2421
|
+
return -1
|
|
2422
|
+
}
|
|
2423
|
+
const dx: i32 = toI32(uncheckedGet(dextra, d))
|
|
2424
|
+
bb = bb | (toU64(toU32(uncheckedGet(dc, d))) << toU64(c3))
|
|
2425
|
+
const c4: i32 = c3 + da
|
|
2426
|
+
bb = bb | (toU64(toU32(dist - toI32(uncheckedGet(dbase, d)))) << toU64(c4))
|
|
2427
|
+
c = c4 + dx
|
|
2014
2428
|
}
|
|
2015
|
-
|
|
2429
|
+
bc = c
|
|
2430
|
+
if (c >= 32) {
|
|
2016
2431
|
if (o >= 0 && o <= toI32(out.length) - 4) {
|
|
2017
2432
|
uncheckedSet(out, o, toU8(bb))
|
|
2018
2433
|
uncheckedSet(out, o + 1, toU8(bb >> toU64(8)))
|
|
@@ -2023,7 +2438,7 @@ export class Deflater {
|
|
|
2023
2438
|
this.overrun = true
|
|
2024
2439
|
}
|
|
2025
2440
|
bb = bb >> toU64(32)
|
|
2026
|
-
bc
|
|
2441
|
+
bc = c - 32
|
|
2027
2442
|
}
|
|
2028
2443
|
}
|
|
2029
2444
|
this.bitBuf = bb
|
|
@@ -2056,7 +2471,7 @@ export const fixedDeflateBound = (n: i32): i32 => {
|
|
|
2056
2471
|
/**
|
|
2057
2472
|
* A one-block DEFLATE compressor in the fixed Huffman code (BTYPE=01, §3.2.6)
|
|
2058
2473
|
* for short inputs such as an HTTP response body: one hash probe per position,
|
|
2059
|
-
* matches at most
|
|
2474
|
+
* matches at most 16,384 back, no code to build or send. It carries
|
|
2060
2475
|
* nothing from one call to the next: the hash table, sized to the input, is
|
|
2061
2476
|
* cleared at the start of every call, so the output is a function of the
|
|
2062
2477
|
* input alone. About 70 KB of tables; reuse one per thread.
|
|
@@ -2069,21 +2484,21 @@ export class FixedDeflater {
|
|
|
2069
2484
|
/** Per length - 3: the length symbol's code and then its extra bits, below bit 16, and how many bits above. */
|
|
2070
2485
|
private readonly length: i32[]
|
|
2071
2486
|
/** Distance to distance symbol, as `Deflater.distSymbol` maps it. */
|
|
2072
|
-
private readonly distSymbol:
|
|
2487
|
+
private readonly distSymbol: u8[]
|
|
2073
2488
|
/** Per distance symbol: its five-bit code, bit-reversed; its base distance; its extra bits. */
|
|
2074
2489
|
private readonly distCode: i32[]
|
|
2075
|
-
private readonly distBase:
|
|
2076
|
-
private readonly distExtra:
|
|
2490
|
+
private readonly distBase: u16[]
|
|
2491
|
+
private readonly distExtra: u8[]
|
|
2077
2492
|
|
|
2078
2493
|
/** The tables, once. */
|
|
2079
2494
|
constructor() {
|
|
2080
2495
|
this.head = new Array<i32>(FIXED_HASH_SIZE)
|
|
2081
2496
|
this.literal = new Array<i32>(256)
|
|
2082
2497
|
this.length = new Array<i32>(256)
|
|
2083
|
-
this.distSymbol = new Array<
|
|
2498
|
+
this.distSymbol = new Array<u8>(512)
|
|
2084
2499
|
this.distCode = new Array<i32>(30)
|
|
2085
|
-
this.distBase = new Array<
|
|
2086
|
-
this.distExtra = new Array<
|
|
2500
|
+
this.distBase = new Array<u16>(30)
|
|
2501
|
+
this.distExtra = new Array<u8>(30)
|
|
2087
2502
|
const lens: i32[] = new Array<i32>(288)
|
|
2088
2503
|
for (let s: i32 = 0; s < 288; s += 1) {
|
|
2089
2504
|
lens[s] = deflateFixedLitLength(s)
|
|
@@ -2121,23 +2536,23 @@ export class FixedDeflater {
|
|
|
2121
2536
|
}
|
|
2122
2537
|
for (let d: i32 = 0; d < 30; d += 1) {
|
|
2123
2538
|
this.distCode[d] = deflateReverse(d, 5)
|
|
2124
|
-
this.distBase[d] = deflateDistBase(d)
|
|
2125
|
-
this.distExtra[d] = deflateDistExtra(d)
|
|
2539
|
+
this.distBase[d] = toU16(deflateDistBase(d))
|
|
2540
|
+
this.distExtra[d] = toU8(deflateDistExtra(d))
|
|
2126
2541
|
}
|
|
2127
2542
|
// Distances 1 to 256 directly; above, by (distance - 1) >> 7 from 256 on.
|
|
2128
2543
|
for (let d: i32 = 0; d < 30; d += 1) {
|
|
2129
|
-
const lo: i32 = this.distBase[d]
|
|
2544
|
+
const lo: i32 = toI32(this.distBase[d])
|
|
2130
2545
|
let hi: i32 = 32769
|
|
2131
2546
|
if (d < 29) {
|
|
2132
|
-
hi = this.distBase[d + 1]
|
|
2547
|
+
hi = toI32(this.distBase[d + 1])
|
|
2133
2548
|
}
|
|
2134
2549
|
for (let dist: i32 = lo; dist < hi && dist >= 1 && dist <= 32768; dist += 1) {
|
|
2135
2550
|
if (dist <= 256) {
|
|
2136
|
-
this.distSymbol[dist - 1] = d
|
|
2551
|
+
this.distSymbol[dist - 1] = toU8(d)
|
|
2137
2552
|
} else {
|
|
2138
2553
|
const at: i32 = ((dist - 1) >> 7) + 256
|
|
2139
2554
|
if (at < 512) {
|
|
2140
|
-
this.distSymbol[at] = d
|
|
2555
|
+
this.distSymbol[at] = toU8(d)
|
|
2141
2556
|
}
|
|
2142
2557
|
}
|
|
2143
2558
|
}
|
|
@@ -2172,10 +2587,10 @@ export class FixedDeflater {
|
|
|
2172
2587
|
const head: i32[] = this.head
|
|
2173
2588
|
const literal: i32[] = this.literal
|
|
2174
2589
|
const length: i32[] = this.length
|
|
2175
|
-
const dsym:
|
|
2590
|
+
const dsym: u8[] = this.distSymbol
|
|
2176
2591
|
const dcode: i32[] = this.distCode
|
|
2177
|
-
const dbase:
|
|
2178
|
-
const dextra:
|
|
2592
|
+
const dbase: u16[] = this.distBase
|
|
2593
|
+
const dextra: u8[] = this.distExtra
|
|
2179
2594
|
if (
|
|
2180
2595
|
toI32(head.length) !== 1 << FIXED_HASH_BITS ||
|
|
2181
2596
|
toI32(literal.length) !== 256 ||
|
|
@@ -2195,117 +2610,154 @@ export class FixedDeflater {
|
|
|
2195
2610
|
}
|
|
2196
2611
|
head.fill(0, 0, 1 << bits)
|
|
2197
2612
|
const shift: u32 = toU32(32 - bits)
|
|
2198
|
-
//
|
|
2199
|
-
|
|
2200
|
-
const
|
|
2201
|
-
|
|
2613
|
+
// `deflateWindow` has checked both windows, so neither end is -1.
|
|
2614
|
+
const end: i32 = deflateWindowEnd(src, off, len)
|
|
2615
|
+
const outEnd: i32 = deflateWindowEnd(out, at, room)
|
|
2616
|
+
if (off < 0 || at < 0 || end < 0 || outEnd < 0) {
|
|
2617
|
+
return DEFLATE_NO_ROOM
|
|
2618
|
+
}
|
|
2202
2619
|
// Matches start where eight bytes can be read; the last few are literals.
|
|
2203
|
-
const last: i32 =
|
|
2620
|
+
const last: i32 = end - 8
|
|
2204
2621
|
// A flush writes four bytes of the final output, so one that would pass
|
|
2205
2622
|
// the room's end means the whole cannot fit.
|
|
2206
|
-
const oe: i32 =
|
|
2623
|
+
const oe: i32 = outEnd - 4
|
|
2207
2624
|
// BFINAL = 1, BTYPE = 01.
|
|
2208
2625
|
let bb: u64 = toU64(3)
|
|
2209
2626
|
let bc: i32 = 3
|
|
2210
2627
|
let o: i32 = at
|
|
2211
2628
|
let p: i32 = off
|
|
2212
2629
|
while (p < end) {
|
|
2630
|
+
// Each test below fails only on a table this class did not fill: `p`
|
|
2631
|
+
// lies in [off, end), `bc` is under 32 between symbols, a code with
|
|
2632
|
+
// its extra bits is at most 13 bits, and a match ends by `end`.
|
|
2633
|
+
const pc: i32 = p
|
|
2634
|
+
const c0: i32 = bc
|
|
2635
|
+
if (pc < 0 || pc > 2147483646 || c0 < 0 || c0 > 31) {
|
|
2636
|
+
return DEFLATE_NO_ROOM
|
|
2637
|
+
}
|
|
2213
2638
|
let matched: i32 = 0
|
|
2214
2639
|
let dist: i32 = 0
|
|
2215
|
-
if (
|
|
2216
|
-
const pi: integer<0, 2147483639> =
|
|
2640
|
+
if (pc <= last && pc <= 2147483639) {
|
|
2641
|
+
const pi: integer<0, 2147483639> = pc
|
|
2217
2642
|
const w: u64 = deflateLoad64(src, pi)
|
|
2218
2643
|
const h: i32 = toI32((toU32(w) * toU32(DEFLATE_HASH_MUL)) >>> shift)
|
|
2219
|
-
//
|
|
2220
|
-
//
|
|
2221
|
-
const
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2644
|
+
// The table holds 0 or `q - off + 1` for an earlier position `q`, so
|
|
2645
|
+
// the candidate `pc - dist` lies in [off - 1, pc).
|
|
2646
|
+
const seen: i32 = uncheckedGet(head, h)
|
|
2647
|
+
const rel: i32 = pc - off
|
|
2648
|
+
if (rel < 0) {
|
|
2649
|
+
return DEFLATE_NO_ROOM
|
|
2650
|
+
}
|
|
2651
|
+
uncheckedSet(head, h, rel + 1)
|
|
2652
|
+
const dd: i32 = seen < 1 ? 0 : rel + 1 - seen
|
|
2653
|
+
if (dd >= 1 && dd <= 16384) {
|
|
2654
|
+
dist = dd
|
|
2655
|
+
const cand: i32 = pc - dd
|
|
2656
|
+
if (cand >= off && cand >= 0 && cand <= 2147483639) {
|
|
2657
|
+
const ci: integer<0, 2147483639> = cand
|
|
2658
|
+
const x: u64 = deflateLoad64(src, ci) ^ w
|
|
2659
|
+
if (toU32(x) === toU32(0)) {
|
|
2660
|
+
const left: i32 = end - pc
|
|
2661
|
+
const max: i32 = left < DEFLATE_MAX_MATCH ? left : DEFLATE_MAX_MATCH
|
|
2662
|
+
if (max < 8 || max > 258) {
|
|
2663
|
+
return DEFLATE_NO_ROOM
|
|
2664
|
+
}
|
|
2665
|
+
if (x !== toU64(0)) {
|
|
2666
|
+
matched = deflateCtz64(x) >> 3
|
|
2667
|
+
} else {
|
|
2668
|
+
const more: i32 = deflateMatchLength(src, cand + 8, pc + 8, max - 8)
|
|
2669
|
+
if (more < 0 || more > 250) {
|
|
2670
|
+
return DEFLATE_NO_ROOM
|
|
2671
|
+
}
|
|
2672
|
+
matched = 8 + more
|
|
2673
|
+
}
|
|
2674
|
+
}
|
|
2236
2675
|
}
|
|
2237
2676
|
}
|
|
2238
2677
|
}
|
|
2678
|
+
let c: i32 = c0
|
|
2239
2679
|
if (matched === 0) {
|
|
2240
|
-
const lc: i32 = uncheckedGet(literal, toI32(uncheckedGet(src,
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
p += 1
|
|
2680
|
+
const lc: i32 = uncheckedGet(literal, toI32(uncheckedGet(src, pc)))
|
|
2681
|
+
const ln: i32 = lc >> 16
|
|
2682
|
+
bb = bb | (toU64(toU32(lc & 0xffff)) << toU64(c0))
|
|
2683
|
+
c = c0 + ln
|
|
2684
|
+
p = pc + 1
|
|
2246
2685
|
} else {
|
|
2247
|
-
//
|
|
2248
|
-
const
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
// Cannot wrap: a distance is 1 to the window and at least its
|
|
2265
|
-
// base, a distance code with its extra bits is at most 18 bits,
|
|
2266
|
-
// `bc` is under 32 here, and the match ends by `end`.
|
|
2267
|
-
const d1: i32 = toI32(toU32(dist) - toU32(1))
|
|
2268
|
-
const d: i32 = dist <= 256 ? uncheckedGet(dsym, d1) : uncheckedGet(dsym, (d1 >> 7) + 256)
|
|
2269
|
-
const dx: i32 = toI32(toU32(dist) - toU32(uncheckedGet(dbase, d)))
|
|
2270
|
-
bb = bb | (toU64(toU32(uncheckedGet(dcode, d) | (dx << 5))) << toU64(bc))
|
|
2271
|
-
bc = toI32(toU32(bc) + toU32(5) + toU32(uncheckedGet(dextra, d)))
|
|
2686
|
+
// The first four bytes agree, so a match is at least 4.
|
|
2687
|
+
const mt: i32 = matched
|
|
2688
|
+
const dv: i32 = dist
|
|
2689
|
+
if (mt < 4 || mt > 258 || dv < 1 || dv > 16384) {
|
|
2690
|
+
return DEFLATE_NO_ROOM
|
|
2691
|
+
}
|
|
2692
|
+
const lc: i32 = uncheckedGet(length, mt - 3)
|
|
2693
|
+
const ln: i32 = lc >> 16
|
|
2694
|
+
const d1: i32 = dv - 1
|
|
2695
|
+
const d: i32 = toI32(dv <= 256 ? uncheckedGet(dsym, d1) : uncheckedGet(dsym, (d1 >> 7) + 256))
|
|
2696
|
+
const dn: i32 = toI32(uncheckedGet(dextra, d))
|
|
2697
|
+
bb = bb | (toU64(toU32(lc & 0xffff)) << toU64(c0))
|
|
2698
|
+
const c1: i32 = c0 + ln
|
|
2699
|
+
bb = bb | (toU64(toU32(uncheckedGet(dcode, d))) << toU64(c1))
|
|
2700
|
+
const c2: i32 = c1 + 5
|
|
2701
|
+
bb = bb | (toU64(toU32(dv - toI32(uncheckedGet(dbase, d)))) << toU64(c2))
|
|
2702
|
+
c = c2 + dn
|
|
2272
2703
|
// One position near the match's end goes into the table, for the
|
|
2273
2704
|
// next repeat of what follows it; the rest are skipped.
|
|
2274
|
-
const
|
|
2275
|
-
|
|
2705
|
+
const rest: i32 = end - pc
|
|
2706
|
+
const gap: i32 = rest < 0 ? -1 : rest - mt
|
|
2707
|
+
if (gap < 0) {
|
|
2708
|
+
return DEFLATE_NO_ROOM
|
|
2709
|
+
}
|
|
2710
|
+
const next: i32 = end - gap
|
|
2711
|
+
if (next < 2) {
|
|
2712
|
+
return DEFLATE_NO_ROOM
|
|
2713
|
+
}
|
|
2714
|
+
const q: i32 = next - 2
|
|
2715
|
+
if (q >= off && q <= last && q <= 2147483639) {
|
|
2276
2716
|
const qi: integer<0, 2147483639> = q
|
|
2277
2717
|
const hq: i32 = toI32((deflateLoad32(src, qi) * toU32(DEFLATE_HASH_MUL)) >>> shift)
|
|
2278
|
-
uncheckedSet(head, hq,
|
|
2718
|
+
uncheckedSet(head, hq, q - off + 1)
|
|
2279
2719
|
}
|
|
2280
|
-
p =
|
|
2720
|
+
p = next
|
|
2281
2721
|
}
|
|
2282
|
-
|
|
2283
|
-
|
|
2722
|
+
// At most 31 + 13 + 5 + 13 = 62 bits are pending, so one flush leaves
|
|
2723
|
+
// fewer than 32.
|
|
2724
|
+
const ct: i32 = c
|
|
2725
|
+
bc = ct
|
|
2726
|
+
if (ct >= 32) {
|
|
2727
|
+
const oc: i32 = o
|
|
2728
|
+
if (oc < 0 || oc > 2147483643 || oc > oe) {
|
|
2284
2729
|
return DEFLATE_NO_ROOM
|
|
2285
2730
|
}
|
|
2286
|
-
uncheckedSet(out,
|
|
2287
|
-
|
|
2288
|
-
uncheckedSet(out,
|
|
2289
|
-
uncheckedSet(out,
|
|
2290
|
-
uncheckedSet(out, toI32(toU32(o) + toU32(3)), toU8(bb >> toU64(24)))
|
|
2731
|
+
uncheckedSet(out, oc, toU8(bb))
|
|
2732
|
+
uncheckedSet(out, oc + 1, toU8(bb >> toU64(8)))
|
|
2733
|
+
uncheckedSet(out, oc + 2, toU8(bb >> toU64(16)))
|
|
2734
|
+
uncheckedSet(out, oc + 3, toU8(bb >> toU64(24)))
|
|
2291
2735
|
bb = bb >> toU64(32)
|
|
2292
|
-
bc
|
|
2293
|
-
o =
|
|
2736
|
+
bc = ct - 32
|
|
2737
|
+
o = oc + 4
|
|
2294
2738
|
}
|
|
2295
2739
|
}
|
|
2296
2740
|
// The end-of-block code, 256: seven zero bits.
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2741
|
+
const cf: i32 = bc
|
|
2742
|
+
const of: i32 = o
|
|
2743
|
+
if (cf < 0 || cf > 31 || of < 0) {
|
|
2744
|
+
return DEFLATE_NO_ROOM
|
|
2745
|
+
}
|
|
2746
|
+
const bytes: i32 = (cf + 14) >> 3
|
|
2747
|
+
const g: i32 = outEnd - of
|
|
2748
|
+
const after: i32 = g < 0 ? -1 : g - bytes
|
|
2749
|
+
if (after < 0) {
|
|
2750
|
+
return DEFLATE_NO_ROOM
|
|
2751
|
+
}
|
|
2752
|
+
// `fin` is `of + bytes`; the bytes are written from the last back.
|
|
2753
|
+
const fin: i32 = outEnd - after
|
|
2754
|
+
if (fin < 0) {
|
|
2303
2755
|
return DEFLATE_NO_ROOM
|
|
2304
2756
|
}
|
|
2305
2757
|
for (let i: i32 = 0; i < bytes; i += 1) {
|
|
2306
|
-
uncheckedSet(out,
|
|
2758
|
+
uncheckedSet(out, fin - 1 - i, toU8(bb >> toU64(toI32(8) * (bytes - 1 - i))))
|
|
2307
2759
|
}
|
|
2308
|
-
return
|
|
2760
|
+
return fin
|
|
2309
2761
|
}
|
|
2310
2762
|
}
|
|
2311
2763
|
|
|
@@ -2358,13 +2810,24 @@ export class InflateTable {
|
|
|
2358
2810
|
* bit. Answers false, copying nothing, unless `2 * end` is within the table.
|
|
2359
2811
|
*/
|
|
2360
2812
|
const inflateDouble = (entries: i32[], end: i32): boolean => {
|
|
2361
|
-
if (end < 0
|
|
2813
|
+
if (end < 0) {
|
|
2814
|
+
return false
|
|
2815
|
+
}
|
|
2816
|
+
// `2 * end` as `n - ((n - end) - end)`: each step subtracts one value at
|
|
2817
|
+
// least zero from another, so the bounds prover proves all three, and the
|
|
2818
|
+
// middle one is negative exactly when `2 * end` is past the table.
|
|
2819
|
+
const n: i32 = toI32(entries.length)
|
|
2820
|
+
const room: i32 = n - end
|
|
2821
|
+
if (room < 0) {
|
|
2822
|
+
return false
|
|
2823
|
+
}
|
|
2824
|
+
const gap: i32 = room - end
|
|
2825
|
+
if (gap < 0) {
|
|
2362
2826
|
return false
|
|
2363
2827
|
}
|
|
2364
2828
|
// A counter below a bound needs no overflow check, and the loop without
|
|
2365
2829
|
// one is a vector copy.
|
|
2366
|
-
|
|
2367
|
-
const twice: i32 = toI32(toU32(end) + toU32(end))
|
|
2830
|
+
const twice: i32 = n - gap
|
|
2368
2831
|
for (let j: i32 = end; j < twice; j += 1) {
|
|
2369
2832
|
uncheckedSet(entries, j, uncheckedGet(entries, j - end))
|
|
2370
2833
|
}
|
|
@@ -2560,7 +3023,7 @@ export class Inflater {
|
|
|
2560
3023
|
bits > 15 ||
|
|
2561
3024
|
from < 0 ||
|
|
2562
3025
|
n < 0 ||
|
|
2563
|
-
|
|
3026
|
+
n > 288 ||
|
|
2564
3027
|
n > toI32(syms.length) ||
|
|
2565
3028
|
toI32(count.length) < 16 ||
|
|
2566
3029
|
toI32(offs.length) < 16 ||
|
|
@@ -2568,61 +3031,98 @@ export class Inflater {
|
|
|
2568
3031
|
) {
|
|
2569
3032
|
return DEFLATE_ERR_HUFFMAN
|
|
2570
3033
|
}
|
|
3034
|
+
// `from + n` as `lens.length - ((lens.length - from) - n)`: each step
|
|
3035
|
+
// subtracts a value at least zero, so the bounds prover proves all
|
|
3036
|
+
// three, and the middle one is negative exactly when the run is past
|
|
3037
|
+
// `lens`.
|
|
3038
|
+
const room: i32 = toI32(lens.length) - from
|
|
3039
|
+
if (room < 0) {
|
|
3040
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3041
|
+
}
|
|
3042
|
+
const gap: i32 = room - n
|
|
3043
|
+
if (gap < 0) {
|
|
3044
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3045
|
+
}
|
|
3046
|
+
const hi: i32 = toI32(lens.length) - gap
|
|
2571
3047
|
for (let l: i32 = 0; l < 16; l += 1) {
|
|
2572
3048
|
count[l] = 0
|
|
2573
3049
|
}
|
|
2574
|
-
//
|
|
2575
|
-
//
|
|
2576
|
-
//
|
|
2577
|
-
const hi: i32 = toI32(toU32(from) + toU32(n))
|
|
2578
|
-
let seen: i32 = 0
|
|
3050
|
+
// A count is at most the `n` lengths seen, and `n` at most 288, the
|
|
3051
|
+
// literal/length alphabet. The bounds prover reads a decimal literal in a
|
|
3052
|
+
// guard, not a named constant or a hex one.
|
|
2579
3053
|
for (let i: i32 = from; i < hi; i += 1) {
|
|
2580
3054
|
const l: i32 = uncheckedGet(lens, i)
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
3055
|
+
if (l < 0 || l > 15) {
|
|
3056
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3057
|
+
}
|
|
3058
|
+
const c: i32 = count[l]
|
|
3059
|
+
if (c < 0 || c >= 288) {
|
|
3060
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3061
|
+
}
|
|
3062
|
+
count[l] = c + 1
|
|
2584
3063
|
}
|
|
2585
|
-
|
|
3064
|
+
const unused: i32 = count[0]
|
|
3065
|
+
if (unused < 0 || unused > n) {
|
|
2586
3066
|
return DEFLATE_ERR_HUFFMAN
|
|
2587
3067
|
}
|
|
3068
|
+
const codes: i32 = n - unused
|
|
2588
3069
|
count[0] = 0
|
|
2589
3070
|
// The Kraft sum: over-subscribed is an error; incomplete is allowed for
|
|
2590
|
-
// one one-bit code only.
|
|
2591
|
-
//
|
|
2592
|
-
// size bounds, and `left` stays at most 2^15: the masks keep every bit,
|
|
2593
|
-
// and tell the bounds prover so.
|
|
3071
|
+
// one one-bit code only. `left` is the codes still free at length `l`,
|
|
3072
|
+
// at most 2^(l - 1) before the step, and a count at most `n`.
|
|
2594
3073
|
let left: i32 = 1
|
|
2595
|
-
let codes: i32 = 0
|
|
2596
3074
|
for (let l: i32 = 1; l < 16; l += 1) {
|
|
2597
|
-
const c: i32 = count[l]
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
if (left < 0) {
|
|
3075
|
+
const c: i32 = count[l]
|
|
3076
|
+
const free: i32 = left
|
|
3077
|
+
if (free < 0 || free > 16384) {
|
|
2601
3078
|
return DEFLATE_ERR_HUFFMAN
|
|
2602
3079
|
}
|
|
3080
|
+
if (c < 0 || c > 288) {
|
|
3081
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3082
|
+
}
|
|
3083
|
+
left = free * 2 - c
|
|
3084
|
+
}
|
|
3085
|
+
if (left < 0) {
|
|
3086
|
+
return DEFLATE_ERR_HUFFMAN
|
|
2603
3087
|
}
|
|
2604
3088
|
if (left > 0 && codes !== 0 && !(kind !== INF_TABLE_CODELEN && codes === 1 && count[1] === 1)) {
|
|
2605
3089
|
return DEFLATE_ERR_HUFFMAN
|
|
2606
3090
|
}
|
|
2607
3091
|
// Sorted by length, then symbol, with no branch per symbol: the unused
|
|
2608
3092
|
// symbols (length 0) go after the rest, where nothing reads them.
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
3093
|
+
// The running sum is at most `codes`, checked as it grows.
|
|
3094
|
+
let acc: i32 = 0
|
|
3095
|
+
for (let l: i32 = 1; l < 16; l += 1) {
|
|
3096
|
+
const c: i32 = count[l]
|
|
3097
|
+
const sum: i32 = acc
|
|
3098
|
+
if (sum < 0 || sum > 288) {
|
|
3099
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3100
|
+
}
|
|
3101
|
+
if (c < 0 || c > 288) {
|
|
3102
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3103
|
+
}
|
|
3104
|
+
offs[l] = sum
|
|
3105
|
+
acc = sum + c
|
|
2613
3106
|
}
|
|
2614
3107
|
offs[0] = codes
|
|
2615
3108
|
for (let i: i32 = from; i < hi; i += 1) {
|
|
2616
|
-
const l: i32 = uncheckedGet(lens, i)
|
|
3109
|
+
const l: i32 = uncheckedGet(lens, i)
|
|
3110
|
+
if (l < 0 || l > 15) {
|
|
3111
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3112
|
+
}
|
|
2617
3113
|
const at: i32 = offs[l]
|
|
2618
|
-
if (at
|
|
2619
|
-
|
|
3114
|
+
if (at < 0 || at >= toI32(syms.length)) {
|
|
3115
|
+
return DEFLATE_ERR_HUFFMAN
|
|
2620
3116
|
}
|
|
2621
|
-
|
|
3117
|
+
syms[at] = i - from
|
|
3118
|
+
offs[l] = at + 1
|
|
3119
|
+
}
|
|
3120
|
+
// `bits` is 1 to 15, so `size` is 2 to 2^15; the guard tells the bounds
|
|
3121
|
+
// prover so, which does not range a shift.
|
|
3122
|
+
const size: i32 = 1 << bits
|
|
3123
|
+
if (size < 2 || size > 32768) {
|
|
3124
|
+
return DEFLATE_ERR_HUFFMAN
|
|
2622
3125
|
}
|
|
2623
|
-
// `bits` is at most 15: the mask keeps every bit, and tells the bounds
|
|
2624
|
-
// prover so.
|
|
2625
|
-
const size: i32 = (1 << bits) & 0xffff
|
|
2626
3126
|
const slow: i32 = INF_SLOW << 8
|
|
2627
3127
|
let tmpl: i32[] = this.clEntries
|
|
2628
3128
|
if (kind === INF_TABLE_LITLEN) {
|
|
@@ -2645,24 +3145,28 @@ export class Inflater {
|
|
|
2645
3145
|
let k: i32 = 0
|
|
2646
3146
|
for (let l: i32 = 1; l <= bits && l < 16; l += 1) {
|
|
2647
3147
|
const codesHere: i32 = uncheckedGet(count, l)
|
|
3148
|
+
const step: i32 = 1 << l
|
|
3149
|
+
if (step < 2 || step > 32768) {
|
|
3150
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3151
|
+
}
|
|
2648
3152
|
for (let c: i32 = 0; c < codesHere; c += 1) {
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
3153
|
+
// `k` is under `codes`, and `code` under 2^l: the set is not
|
|
3154
|
+
// over-subscribed, checked above.
|
|
3155
|
+
const at: i32 = k
|
|
3156
|
+
const cur: i32 = code
|
|
3157
|
+
if (at < 0 || at >= toI32(syms.length) || cur < 0 || cur > 32767) {
|
|
3158
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3159
|
+
}
|
|
3160
|
+
const s: i32 = syms[at]
|
|
3161
|
+
if (s < 0 || s >= toI32(tmpl.length)) {
|
|
3162
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3163
|
+
}
|
|
3164
|
+
const e: i32 = tmpl[s] | l
|
|
3165
|
+
for (let r: i32 = deflateReverse(cur, l); r < size; r += step) {
|
|
3166
|
+
uncheckedSet(entries, r, e)
|
|
2660
3167
|
}
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
// prover so.
|
|
2664
|
-
k = (k & 0xffff) + 1
|
|
2665
|
-
code = (code & 0x7fff) + 1
|
|
3168
|
+
k = at + 1
|
|
3169
|
+
code = cur + 1
|
|
2666
3170
|
}
|
|
2667
3171
|
code = code << 1
|
|
2668
3172
|
}
|
|
@@ -2702,9 +3206,12 @@ export class Inflater {
|
|
|
2702
3206
|
return 0
|
|
2703
3207
|
}
|
|
2704
3208
|
// `code` differs from `end - 1`, so the count of leading zeros is
|
|
2705
|
-
// under 32
|
|
2706
|
-
const
|
|
2707
|
-
|
|
3209
|
+
// under 32 and the shift inside the width.
|
|
3210
|
+
const top: i32 = toI32(31) - Math.clz32(toU32(code ^ (end - 1)))
|
|
3211
|
+
if (top < 0 || top > 14) {
|
|
3212
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3213
|
+
}
|
|
3214
|
+
code = (code & ~(-1 << top)) | (1 << top)
|
|
2708
3215
|
here -= 1
|
|
2709
3216
|
}
|
|
2710
3217
|
len += 1
|
|
@@ -2725,7 +3232,12 @@ export class Inflater {
|
|
|
2725
3232
|
// tables already built, as wide as it takes for them to fill it (the
|
|
2726
3233
|
// code is complete, so they do), and the slot points to it. A subtable
|
|
2727
3234
|
// entry carries the bits past the main table's as its length.
|
|
3235
|
+
// A subtable's start is packed into an entry's top bits, so a table and
|
|
3236
|
+
// its subtables fit 2^15 entries.
|
|
2728
3237
|
const limit: i32 = toI32(entries.length)
|
|
3238
|
+
if (limit > 32767) {
|
|
3239
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3240
|
+
}
|
|
2729
3241
|
let prefix: i32 = -1
|
|
2730
3242
|
let start: i32 = 0
|
|
2731
3243
|
let subEnd: i32 = size
|
|
@@ -2735,24 +3247,40 @@ export class Inflater {
|
|
|
2735
3247
|
}
|
|
2736
3248
|
if ((code & (size - 1)) !== prefix) {
|
|
2737
3249
|
prefix = code & (size - 1)
|
|
2738
|
-
start = subEnd
|
|
2739
3250
|
let subBits: i32 = len - bits
|
|
2740
3251
|
let used: i32 = here
|
|
2741
|
-
// `used` is under 2^subBits when it doubles, and
|
|
2742
|
-
// most 15 after the step
|
|
2743
|
-
// bounds prover so.
|
|
3252
|
+
// `used` is under 2^subBits, at most 2^13, when it doubles, and
|
|
3253
|
+
// `bits + subBits` at most 15 after the step.
|
|
2744
3254
|
while (subBits < 14 && bits + subBits < 15 && used < 1 << subBits) {
|
|
2745
|
-
|
|
2746
|
-
const
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
3255
|
+
const wider: i32 = subBits
|
|
3256
|
+
const twice: i32 = used
|
|
3257
|
+
if (wider < 0 || wider > 13 || twice < 0 || twice > 8191) {
|
|
3258
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3259
|
+
}
|
|
3260
|
+
subBits = wider + 1
|
|
3261
|
+
const at: i32 = bits + subBits
|
|
3262
|
+
if (at > 15) {
|
|
3263
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3264
|
+
}
|
|
3265
|
+
const c: i32 = count[at]
|
|
3266
|
+
if (c < 0 || c > 288) {
|
|
3267
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3268
|
+
}
|
|
3269
|
+
used = twice * 2 + c
|
|
3270
|
+
}
|
|
3271
|
+
// The subtable goes after the last one, and must fit the table.
|
|
3272
|
+
const base: i32 = subEnd
|
|
3273
|
+
const width: i32 = 1 << subBits
|
|
3274
|
+
if (base < 0 || base > 32767 || width < 1 || width > 16384 || prefix < 0) {
|
|
3275
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3276
|
+
}
|
|
3277
|
+
const next: i32 = base + width
|
|
3278
|
+
if (next > limit) {
|
|
2753
3279
|
return DEFLATE_ERR_HUFFMAN
|
|
2754
3280
|
}
|
|
2755
|
-
|
|
3281
|
+
start = base
|
|
3282
|
+
subEnd = next
|
|
3283
|
+
uncheckedSet(entries, prefix, (base << 16) | (INF_SUB << 8) | (subBits << 11) | bits)
|
|
2756
3284
|
}
|
|
2757
3285
|
if (k < 0 || k >= toI32(syms.length)) {
|
|
2758
3286
|
return DEFLATE_ERR_HUFFMAN
|
|
@@ -2763,31 +3291,52 @@ export class Inflater {
|
|
|
2763
3291
|
}
|
|
2764
3292
|
const e: i32 = tmpl[s] | (len - bits)
|
|
2765
3293
|
k += 1
|
|
3294
|
+
// The code past the main table's bits is under 2^14, as is the
|
|
3295
|
+
// stride, and the subtable ends within the table's 2^15 entries.
|
|
3296
|
+
const first: i32 = start
|
|
3297
|
+
const stop: i32 = subEnd
|
|
3298
|
+
const high: i32 = code >> bits
|
|
2766
3299
|
const stride: i32 = toI32(1) << (len - bits)
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
3300
|
+
if (
|
|
3301
|
+
first < 0 ||
|
|
3302
|
+
first > 32767 ||
|
|
3303
|
+
stop > 32767 ||
|
|
3304
|
+
high < 0 ||
|
|
3305
|
+
high > 16383 ||
|
|
3306
|
+
stride < 1 ||
|
|
3307
|
+
stride > 16384
|
|
3308
|
+
) {
|
|
3309
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3310
|
+
}
|
|
3311
|
+
for (let i: i32 = first + high; i < stop; i += stride) {
|
|
2772
3312
|
uncheckedSet(entries, i, e)
|
|
2773
|
-
i = toI32(toU32(i) + toU32(stride))
|
|
2774
3313
|
}
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
3314
|
+
const top: i32 = 1 << len
|
|
3315
|
+
if (top < 2 || top > 32768) {
|
|
3316
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3317
|
+
}
|
|
3318
|
+
const full: i32 = top - 1
|
|
2778
3319
|
if (code === full) {
|
|
2779
3320
|
return 0
|
|
2780
3321
|
}
|
|
2781
3322
|
// As above: `code` is not `full`, so the leading zeros are under 32.
|
|
2782
|
-
const
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
3323
|
+
const lead: i32 = toI32(31) - Math.clz32(toU32(code ^ full))
|
|
3324
|
+
if (lead < 0 || lead > 14) {
|
|
3325
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3326
|
+
}
|
|
3327
|
+
code = (code & ~(-1 << lead)) | (1 << lead)
|
|
3328
|
+
// At least one code of this length remains, and at most `n`: the code
|
|
3329
|
+
// is complete, so the last code, `full`, comes before the count runs
|
|
3330
|
+
// out.
|
|
3331
|
+
const remain: i32 = here
|
|
3332
|
+
if (remain < 1 || remain > 288) {
|
|
3333
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3334
|
+
}
|
|
3335
|
+
here = remain - 1
|
|
2787
3336
|
while (here === 0 && len < 15) {
|
|
2788
3337
|
const next: i32 = len + 1
|
|
2789
3338
|
if (next < 0 || next >= toI32(count.length)) {
|
|
2790
|
-
|
|
3339
|
+
return DEFLATE_ERR_HUFFMAN
|
|
2791
3340
|
}
|
|
2792
3341
|
len = next
|
|
2793
3342
|
here = count[next]
|
|
@@ -2808,26 +3357,32 @@ export class Inflater {
|
|
|
2808
3357
|
let first: i32 = 0
|
|
2809
3358
|
let index: i32 = 0
|
|
2810
3359
|
let b: u64 = bb
|
|
2811
|
-
// A built table has at most
|
|
2812
|
-
// count
|
|
2813
|
-
//
|
|
3360
|
+
// A built table has at most 288 symbols and no code past 15 bits, so a
|
|
3361
|
+
// count and `index` are at most 288 and `code` and `first` under 2^16:
|
|
3362
|
+
// a value past those is a table `build` did not make, and no code
|
|
3363
|
+
// matches.
|
|
2814
3364
|
for (let l: i32 = 1; l < 16 && l < toI32(count.length); l += 1) {
|
|
2815
3365
|
if (l > bc) {
|
|
2816
3366
|
return -1
|
|
2817
3367
|
}
|
|
2818
|
-
|
|
3368
|
+
const cur: i32 = code | toI32(b & toU64(1))
|
|
2819
3369
|
b = b >> toU64(1)
|
|
2820
|
-
const c: i32 = count[l]
|
|
2821
|
-
|
|
2822
|
-
|
|
3370
|
+
const c: i32 = count[l]
|
|
3371
|
+
const base: i32 = first
|
|
3372
|
+
const seen: i32 = index
|
|
3373
|
+
if (c < 0 || c > 288 || cur < 0 || cur > 65535 || base < 0 || base > 65535 || seen < 0 || seen > 288) {
|
|
3374
|
+
return -2
|
|
3375
|
+
}
|
|
3376
|
+
if (cur - c < base) {
|
|
3377
|
+
const at: i32 = seen + (cur - base)
|
|
2823
3378
|
if (at < 0 || at >= toI32(syms.length)) {
|
|
2824
3379
|
return -2
|
|
2825
3380
|
}
|
|
2826
3381
|
return this.entryOf(kind, syms[at], l)
|
|
2827
3382
|
}
|
|
2828
|
-
index =
|
|
2829
|
-
first = (
|
|
2830
|
-
code =
|
|
3383
|
+
index = seen + c
|
|
3384
|
+
first = (base + c) * 2
|
|
3385
|
+
code = cur * 2
|
|
2831
3386
|
}
|
|
2832
3387
|
return -2
|
|
2833
3388
|
}
|
|
@@ -2835,14 +3390,15 @@ export class Inflater {
|
|
|
2835
3390
|
/**
|
|
2836
3391
|
* Reads bytes of `src` into the bit buffer from `inPos` while it has room
|
|
2837
3392
|
* and `end` allows. Answers false, reading nothing, when `inPos` is
|
|
2838
|
-
* negative
|
|
3393
|
+
* negative, `end` past `src` or the bit count outside 0 to 63, which
|
|
3394
|
+
* `inflate` rules out.
|
|
2839
3395
|
*/
|
|
2840
3396
|
private fill(src: u8[], end: i32): boolean {
|
|
2841
3397
|
let bb: u64 = this.bitBuf
|
|
2842
|
-
|
|
2843
|
-
let bc: i32 = this.bitCount & 63
|
|
3398
|
+
let bc: i32 = this.bitCount
|
|
2844
3399
|
let ip: i32 = this.inPos
|
|
2845
|
-
|
|
3400
|
+
// The buffer holds under 64 bits.
|
|
3401
|
+
if (bc < 0 || bc > 63 || ip < 0 || end > toI32(src.length)) {
|
|
2846
3402
|
return false
|
|
2847
3403
|
}
|
|
2848
3404
|
// `ip < end - 7` keeps `ip` at most 2^31 - 9: the third test never
|
|
@@ -2866,52 +3422,65 @@ export class Inflater {
|
|
|
2866
3422
|
return true
|
|
2867
3423
|
}
|
|
2868
3424
|
|
|
2869
|
-
/**
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
3425
|
+
/**
|
|
3426
|
+
* Gives back the whole bytes of the bit buffer to the input, keeping the
|
|
3427
|
+
* bits short of a byte. Answers false, changing nothing, when the bit
|
|
3428
|
+
* count is outside 0 to 63 or the bytes would take `inPos` below zero:
|
|
3429
|
+
* the buffer only holds bytes read from before `inPos`, so neither
|
|
3430
|
+
* happens.
|
|
3431
|
+
*/
|
|
3432
|
+
private giveBack(): boolean {
|
|
3433
|
+
const bc: i32 = this.bitCount
|
|
3434
|
+
const ip: i32 = this.inPos
|
|
3435
|
+
if (bc < 0 || bc > 63 || ip < 0) {
|
|
3436
|
+
return false
|
|
3437
|
+
}
|
|
3438
|
+
const back: i32 = ip - (bc >> 3)
|
|
3439
|
+
if (back < 0) {
|
|
3440
|
+
return false
|
|
3441
|
+
}
|
|
3442
|
+
this.inPos = back
|
|
3443
|
+
this.bitCount = bc & 7
|
|
3444
|
+
this.bitBuf = this.bitBuf & ((toU64(1) << toU64(this.bitCount)) - toU64(1))
|
|
3445
|
+
return true
|
|
2876
3446
|
}
|
|
2877
3447
|
|
|
2878
3448
|
/**
|
|
2879
3449
|
* Gives back the whole bytes of the bit buffer to the input, after dropping
|
|
2880
3450
|
* the bits to the next byte boundary: where a stored block's `LEN`, or the
|
|
2881
|
-
* framing after the final block, begins.
|
|
3451
|
+
* framing after the final block, begins. Answers false as `giveBack` does.
|
|
2882
3452
|
*/
|
|
2883
|
-
private unread():
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
this.inPos = toI32(toU32(this.inPos) - toU32(bc >> 3))
|
|
3453
|
+
private unread(): boolean {
|
|
3454
|
+
if (!this.giveBack()) {
|
|
3455
|
+
return false
|
|
3456
|
+
}
|
|
2888
3457
|
this.bitBuf = 0
|
|
2889
3458
|
this.bitCount = 0
|
|
3459
|
+
return true
|
|
2890
3460
|
}
|
|
2891
3461
|
|
|
2892
3462
|
/**
|
|
2893
3463
|
* Reads a dynamic block's header (§3.2.7) from the bit buffer and `src`,
|
|
2894
3464
|
* and builds its tables. Answers 0, `INFLATE_NEED_INPUT` when the input ran
|
|
2895
3465
|
* out first (nothing is consumed then: the caller rewinds), or an error:
|
|
2896
|
-
* `DEFLATE_ERR_TRUNCATED` too when `inPos` is negative
|
|
2897
|
-
*
|
|
3466
|
+
* `DEFLATE_ERR_TRUNCATED` too when `inPos` is negative, `end` past `src`
|
|
3467
|
+
* or the bit count outside 0 to 63, and `DEFLATE_ERR_HUFFMAN` when the code-length table is not its
|
|
2898
3468
|
* size, which `inflate` and the constructor rule out.
|
|
2899
3469
|
*/
|
|
2900
3470
|
private readDynamic(src: u8[], end: i32): i32 {
|
|
2901
3471
|
// The bit buffer in locals: the lengths are stored into an `i32[]`,
|
|
2902
3472
|
// which the fields could alias, and every symbol would reload them.
|
|
2903
3473
|
let bb: u64 = this.bitBuf
|
|
2904
|
-
|
|
2905
|
-
// the bounds prover so.
|
|
2906
|
-
let bc: i32 = this.bitCount & 63
|
|
3474
|
+
let bc: i32 = this.bitCount
|
|
2907
3475
|
let ip: i32 = this.inPos
|
|
2908
|
-
|
|
3476
|
+
// The buffer holds under 64 bits.
|
|
3477
|
+
if (bc < 0 || bc > 63 || ip < 0 || end > toI32(src.length)) {
|
|
2909
3478
|
return DEFLATE_ERR_TRUNCATED
|
|
2910
3479
|
}
|
|
2911
3480
|
if (end >= 8 && ip < end - 7 && ip <= 2147483639) {
|
|
2912
3481
|
const at: integer<0, 2147483639> = ip
|
|
2913
3482
|
bb = bb | (deflateLoad64(src, at) << toU64(bc))
|
|
2914
|
-
ip += (
|
|
3483
|
+
ip += (63 - bc) >> 3
|
|
2915
3484
|
bc = bc | 56
|
|
2916
3485
|
} else {
|
|
2917
3486
|
while (bc <= 56 && ip < end) {
|
|
@@ -2944,12 +3513,14 @@ export class Inflater {
|
|
|
2944
3513
|
}
|
|
2945
3514
|
for (let i: i32 = 0; i < hclen; i += 1) {
|
|
2946
3515
|
// Under 64 bits, as above.
|
|
2947
|
-
bc
|
|
3516
|
+
if (bc < 0 || bc > 63) {
|
|
3517
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3518
|
+
}
|
|
2948
3519
|
if (bc < 3) {
|
|
2949
3520
|
if (ip >= 0 && end >= 8 && ip < end - 7 && ip <= 2147483639) {
|
|
2950
3521
|
const at: integer<0, 2147483639> = ip
|
|
2951
3522
|
bb = bb | (deflateLoad64(src, at) << toU64(bc))
|
|
2952
|
-
ip += (
|
|
3523
|
+
ip += (63 - bc) >> 3
|
|
2953
3524
|
bc = bc | 56
|
|
2954
3525
|
} else {
|
|
2955
3526
|
while (bc <= 56 && ip < end) {
|
|
@@ -2983,14 +3554,17 @@ export class Inflater {
|
|
|
2983
3554
|
let i: i32 = 0
|
|
2984
3555
|
while (i < total) {
|
|
2985
3556
|
// Under 64 bits, as above.
|
|
2986
|
-
bc
|
|
3557
|
+
if (bc < 0 || bc > 63) {
|
|
3558
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3559
|
+
}
|
|
2987
3560
|
// A code-length code is at most 7 bits and its repeat count at most 7.
|
|
2988
3561
|
if (bc < 14) {
|
|
2989
3562
|
if (ip >= 0 && end >= 8 && ip < end - 7 && ip <= 2147483639) {
|
|
2990
3563
|
const at: integer<0, 2147483639> = ip
|
|
2991
3564
|
bb = bb | (deflateLoad64(src, at) << toU64(bc))
|
|
2992
|
-
ip += (
|
|
2993
|
-
|
|
3565
|
+
ip += (63 - bc) >> 3
|
|
3566
|
+
// `bc | 56`, as a sum the bounds prover follows.
|
|
3567
|
+
bc = toI32(56) + (bc & 7)
|
|
2994
3568
|
} else {
|
|
2995
3569
|
while (bc <= 56 && ip < end) {
|
|
2996
3570
|
bb = bb | (toU64(uncheckedGet(src, ip)) << toU64(bc))
|
|
@@ -3009,8 +3583,7 @@ export class Inflater {
|
|
|
3009
3583
|
return INFLATE_NEED_INPUT
|
|
3010
3584
|
}
|
|
3011
3585
|
bb = bb >> toU64(l)
|
|
3012
|
-
|
|
3013
|
-
bc = toI32(toU32(bc) - toU32(l))
|
|
3586
|
+
bc -= l
|
|
3014
3587
|
const sym: i32 = e >> 16
|
|
3015
3588
|
if (sym < 16) {
|
|
3016
3589
|
if (i < 0 || i >= toI32(lens.length)) {
|
|
@@ -3039,15 +3612,23 @@ export class Inflater {
|
|
|
3039
3612
|
if (bc < x) {
|
|
3040
3613
|
return INFLATE_NEED_INPUT
|
|
3041
3614
|
}
|
|
3042
|
-
// At most 7 bits
|
|
3043
|
-
|
|
3044
|
-
|
|
3615
|
+
// At most 7 bits.
|
|
3616
|
+
const more: i32 = toI32(bb & ((toU64(1) << toU64(x)) - toU64(1)))
|
|
3617
|
+
if (more < 0 || more > 127) {
|
|
3618
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3619
|
+
}
|
|
3620
|
+
rep += more
|
|
3045
3621
|
bb = bb >> toU64(x)
|
|
3046
|
-
|
|
3047
|
-
|
|
3622
|
+
const have: i32 = bc
|
|
3623
|
+
if (have < 0 || have > 64) {
|
|
3624
|
+
return DEFLATE_ERR_HUFFMAN
|
|
3625
|
+
}
|
|
3626
|
+
bc = have - x
|
|
3048
3627
|
if (i < 0 || rep > total - i) {
|
|
3049
3628
|
return DEFLATE_ERR_HUFFMAN
|
|
3050
3629
|
}
|
|
3630
|
+
// `total` is at most 286 + 30, under `lens.length`'s 320: the second
|
|
3631
|
+
// test never stops the loop.
|
|
3051
3632
|
for (let k: i32 = 0; k < rep && i < toI32(lens.length); k += 1) {
|
|
3052
3633
|
lens[i] = value
|
|
3053
3634
|
i += 1
|
|
@@ -3118,12 +3699,9 @@ export class Inflater {
|
|
|
3118
3699
|
const r: i32 = this.step(src, inEnd, more, out, outEnd, histStart)
|
|
3119
3700
|
// Whole bytes the bit buffer read ahead go back to the input, so that a
|
|
3120
3701
|
// caller may drop everything before `inPos`.
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
this.inPos = toI32(toU32(this.inPos) - toU32(bc >> 3))
|
|
3125
|
-
this.bitCount = bc & 7
|
|
3126
|
-
this.bitBuf = this.bitBuf & ((toU64(1) << toU64(this.bitCount)) - toU64(1))
|
|
3702
|
+
if (!this.giveBack()) {
|
|
3703
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3704
|
+
}
|
|
3127
3705
|
return r
|
|
3128
3706
|
}
|
|
3129
3707
|
|
|
@@ -3136,7 +3714,9 @@ export class Inflater {
|
|
|
3136
3714
|
}
|
|
3137
3715
|
if (s === INF_S_BLOCK) {
|
|
3138
3716
|
if (this.last) {
|
|
3139
|
-
this.unread()
|
|
3717
|
+
if (!this.unread()) {
|
|
3718
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3719
|
+
}
|
|
3140
3720
|
this.state = INF_S_DONE
|
|
3141
3721
|
return INFLATE_DONE
|
|
3142
3722
|
}
|
|
@@ -3146,16 +3726,21 @@ export class Inflater {
|
|
|
3146
3726
|
if (!this.fill(src, inEnd)) {
|
|
3147
3727
|
return DEFLATE_ERR_TRUNCATED
|
|
3148
3728
|
}
|
|
3149
|
-
|
|
3729
|
+
const have: i32 = this.bitCount
|
|
3730
|
+
if (have < 3) {
|
|
3150
3731
|
this.bitBuf = saveBuf
|
|
3151
3732
|
this.bitCount = saveCount
|
|
3152
3733
|
this.inPos = savePos
|
|
3153
3734
|
return more ? INFLATE_NEED_INPUT : DEFLATE_ERR_TRUNCATED
|
|
3154
3735
|
}
|
|
3155
|
-
const header: i32 = this.
|
|
3736
|
+
const header: i32 = toI32(this.bitBuf & toU64(7))
|
|
3737
|
+
this.bitBuf = this.bitBuf >> toU64(3)
|
|
3738
|
+
this.bitCount = have - 3
|
|
3156
3739
|
const type: i32 = header >> 1
|
|
3157
3740
|
if (type === 0) {
|
|
3158
|
-
this.unread()
|
|
3741
|
+
if (!this.unread()) {
|
|
3742
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3743
|
+
}
|
|
3159
3744
|
const at: i32 = this.inPos
|
|
3160
3745
|
if (at < 0) {
|
|
3161
3746
|
return DEFLATE_ERR_TRUNCATED
|
|
@@ -3204,12 +3789,21 @@ export class Inflater {
|
|
|
3204
3789
|
} else if (s === INF_S_STORED) {
|
|
3205
3790
|
const ip: i32 = this.inPos
|
|
3206
3791
|
const opos: i32 = this.outPos
|
|
3207
|
-
//
|
|
3208
|
-
//
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
const
|
|
3792
|
+
// `inflate` checked `inPos <= inEnd` and `outPos <= outEnd`, and
|
|
3793
|
+
// every step keeps both.
|
|
3794
|
+
if (ip < 0 || ip > inEnd || opos < 0 || opos > outEnd) {
|
|
3795
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3796
|
+
}
|
|
3797
|
+
const inAvail: i32 = inEnd - ip
|
|
3798
|
+
const outAvail: i32 = outEnd - opos
|
|
3799
|
+
if (inAvail < 0 || outAvail < 0 || inEnd < 0 || outEnd < 0) {
|
|
3800
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3801
|
+
}
|
|
3802
|
+
// A stored length is 16 bits.
|
|
3803
|
+
const left: i32 = this.storedLeft
|
|
3804
|
+
if (left < 0 || left > 65535) {
|
|
3805
|
+
return DEFLATE_ERR_STORED_LENGTH
|
|
3806
|
+
}
|
|
3213
3807
|
let n: i32 = left
|
|
3214
3808
|
if (n > inAvail) {
|
|
3215
3809
|
n = inAvail
|
|
@@ -3217,11 +3811,17 @@ export class Inflater {
|
|
|
3217
3811
|
if (n > outAvail) {
|
|
3218
3812
|
n = outAvail
|
|
3219
3813
|
}
|
|
3814
|
+
// `n` is at most each room, and each room at least zero: `ip + n`
|
|
3815
|
+
// and `opos + n` are the ends less what is left of each room.
|
|
3816
|
+
const inGap: i32 = inAvail - n
|
|
3817
|
+
const outGap: i32 = outAvail - n
|
|
3818
|
+
if (inGap < 0 || outGap < 0) {
|
|
3819
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3820
|
+
}
|
|
3220
3821
|
copyBytes(out, opos, src, ip, n)
|
|
3221
|
-
|
|
3222
|
-
this.
|
|
3223
|
-
this.
|
|
3224
|
-
this.storedLeft = toI32(toU32(left) - toU32(n))
|
|
3822
|
+
this.inPos = inEnd - inGap
|
|
3823
|
+
this.outPos = outEnd - outGap
|
|
3824
|
+
this.storedLeft = left - n
|
|
3225
3825
|
if (this.storedLeft > 0) {
|
|
3226
3826
|
if (this.outPos === outEnd) {
|
|
3227
3827
|
return INFLATE_NEED_OUTPUT
|
|
@@ -3254,7 +3854,7 @@ export class Inflater {
|
|
|
3254
3854
|
if (toI32(le.length) !== INF_LIT_ENOUGH || toI32(de.length) !== INF_DIST_ENOUGH) {
|
|
3255
3855
|
return DEFLATE_ERR_HUFFMAN
|
|
3256
3856
|
}
|
|
3257
|
-
if (inEnd < 0 || outEnd < 0) {
|
|
3857
|
+
if (inEnd < 0 || outEnd < 0 || histStart < 0) {
|
|
3258
3858
|
return DEFLATE_ERR_TRUNCATED
|
|
3259
3859
|
}
|
|
3260
3860
|
const litMask: u64 = (toU64(1) << toU64(INF_LIT_BITS)) - toU64(1)
|
|
@@ -3318,11 +3918,11 @@ export class Inflater {
|
|
|
3318
3918
|
// A code longer than the table: its last bits index a subtable.
|
|
3319
3919
|
bb = bb >> toU64(e)
|
|
3320
3920
|
bc = wrappingSub(bc, e)
|
|
3321
|
-
//
|
|
3322
|
-
//
|
|
3921
|
+
// The subtable's start and the index into it are both under
|
|
3922
|
+
// 2^16, so the sum is an i32.
|
|
3323
3923
|
e = uncheckedGet(
|
|
3324
3924
|
le,
|
|
3325
|
-
|
|
3925
|
+
((e >> 16) & 65535) + (toI32(bb) & 32767 & ~(toI32(-1) << ((e >> 11) & 15)))
|
|
3326
3926
|
)
|
|
3327
3927
|
} else if ((e & 0x700) === INF_SLOW << 8) {
|
|
3328
3928
|
// Only a table built for an incomplete code (none at all, or
|
|
@@ -3349,19 +3949,19 @@ export class Inflater {
|
|
|
3349
3949
|
}
|
|
3350
3950
|
} else {
|
|
3351
3951
|
const lx: i32 = (e >> 11) & 15
|
|
3352
|
-
//
|
|
3353
|
-
//
|
|
3354
|
-
const len: i32 =
|
|
3952
|
+
// A length base is at most 258 and its extra bits at most 5;
|
|
3953
|
+
// the two masks only bound the sum for the prover.
|
|
3954
|
+
const len: i32 = ((e >> 16) & 65535) + (toI32(bb) & 32767 & ~(-1 << lx))
|
|
3355
3955
|
bb = bb >> toU64(lx)
|
|
3356
3956
|
bc = wrappingSub(bc, lx)
|
|
3357
3957
|
let d: i32 = uncheckedGet(de, toI32(bb & distMask))
|
|
3358
3958
|
if ((d & 0x700) === INF_SUB << 8) {
|
|
3359
3959
|
bb = bb >> toU64(d)
|
|
3360
3960
|
bc = wrappingSub(bc, d)
|
|
3361
|
-
//
|
|
3961
|
+
// As for the literal/length subtable.
|
|
3362
3962
|
d = uncheckedGet(
|
|
3363
3963
|
de,
|
|
3364
|
-
|
|
3964
|
+
((d >> 16) & 65535) + (toI32(bb) & 32767 & ~(toI32(-1) << ((d >> 11) & 15)))
|
|
3365
3965
|
)
|
|
3366
3966
|
}
|
|
3367
3967
|
if ((d & 0x700) !== 0) {
|
|
@@ -3373,12 +3973,14 @@ export class Inflater {
|
|
|
3373
3973
|
bb = bb >> toU64(d)
|
|
3374
3974
|
bc = wrappingSub(bc, d)
|
|
3375
3975
|
const dx: i32 = (d >> 11) & 15
|
|
3376
|
-
//
|
|
3377
|
-
|
|
3378
|
-
const dist: i32 = toI32(toU32(d >> 16) + toU32(bb & ((toU64(1) << toU64(dx)) - toU64(1))))
|
|
3976
|
+
// A distance base is under 2^15 and its extra bits at most 13.
|
|
3977
|
+
const dist: i32 = ((d >> 16) & 65535) + (toI32(bb) & 32767 & ~(-1 << dx))
|
|
3379
3978
|
bb = bb >> toU64(dx)
|
|
3380
3979
|
bc = wrappingSub(bc, dx)
|
|
3381
|
-
|
|
3980
|
+
// `op` stays at or past `histStart`, which is not negative, so
|
|
3981
|
+
// the test of `at` fails only on a position this loop broke.
|
|
3982
|
+
const at: i32 = op
|
|
3983
|
+
if (at < 0 || dist > at - histStart) {
|
|
3382
3984
|
status = DEFLATE_ERR_DISTANCE
|
|
3383
3985
|
break
|
|
3384
3986
|
}
|
|
@@ -3394,9 +3996,9 @@ export class Inflater {
|
|
|
3394
3996
|
// always hold; they are what lets the positions enter the
|
|
3395
3997
|
// copy's range without a check, and the bytes loop after is
|
|
3396
3998
|
// what would finish a copy they ever stopped.
|
|
3397
|
-
//
|
|
3398
|
-
let to: i32 =
|
|
3399
|
-
let from: i32 =
|
|
3999
|
+
// `dist` is at most `at - histStart`, so `from` is at or past `histStart`.
|
|
4000
|
+
let to: i32 = at
|
|
4001
|
+
let from: i32 = at - dist
|
|
3400
4002
|
while (to < stop && to >= 0 && to <= 2147483631 && from >= 0 && from <= 2147483631) {
|
|
3401
4003
|
inflateCopy16(out, to, from)
|
|
3402
4004
|
to = wrappingAdd(to, 16)
|
|
@@ -3409,8 +4011,8 @@ export class Inflater {
|
|
|
3409
4011
|
}
|
|
3410
4012
|
} else if (dist === 1) {
|
|
3411
4013
|
// A run of one byte, sixteen at a time, bounded as above.
|
|
3412
|
-
//
|
|
3413
|
-
const four: u64 = toU64(uncheckedGet(out,
|
|
4014
|
+
// `at` is past `histStart` by at least `dist`.
|
|
4015
|
+
const four: u64 = toU64(uncheckedGet(out, at - 1)) * toU64(0x01010101)
|
|
3414
4016
|
const w: u64 = four | (four << toU64(32))
|
|
3415
4017
|
let to: i32 = op
|
|
3416
4018
|
while (to < stop && to >= 0 && to <= 2147483631) {
|
|
@@ -3422,9 +4024,9 @@ export class Inflater {
|
|
|
3422
4024
|
to = wrappingAdd(to, 1)
|
|
3423
4025
|
}
|
|
3424
4026
|
} else {
|
|
3425
|
-
//
|
|
3426
|
-
let from: i32 =
|
|
3427
|
-
let to: i32 =
|
|
4027
|
+
// As above.
|
|
4028
|
+
let from: i32 = at - dist
|
|
4029
|
+
let to: i32 = at
|
|
3428
4030
|
while (to < stop) {
|
|
3429
4031
|
uncheckedSet(out, to, uncheckedGet(out, from))
|
|
3430
4032
|
to = wrappingAdd(to, 1)
|
|
@@ -3455,7 +4057,8 @@ export class Inflater {
|
|
|
3455
4057
|
if (ip >= 0 && ip <= inFast) {
|
|
3456
4058
|
bb = bb | (deflateLoad64(src, ip) << toU64(bc))
|
|
3457
4059
|
ip += (63 - bc) >> 3
|
|
3458
|
-
|
|
4060
|
+
// `bc | 56`, as a sum the bounds prover follows.
|
|
4061
|
+
bc = toI32(56) + (bc & 7)
|
|
3459
4062
|
} else {
|
|
3460
4063
|
while (bc <= 56 && ip < inEnd) {
|
|
3461
4064
|
bb = bb | (toU64(uncheckedGet(src, ip)) << toU64(bc))
|
|
@@ -3483,8 +4086,9 @@ export class Inflater {
|
|
|
3483
4086
|
const kind: i32 = (e >> 8) & 7
|
|
3484
4087
|
const n: i32 = e & 15
|
|
3485
4088
|
bb = bb >> toU64(n)
|
|
3486
|
-
//
|
|
3487
|
-
|
|
4089
|
+
// `lookup` answers only a code whose bits are all here.
|
|
4090
|
+
const left: i32 = bc - n
|
|
4091
|
+
bc = left
|
|
3488
4092
|
if (kind === INF_LITERAL) {
|
|
3489
4093
|
if (op >= outEnd) {
|
|
3490
4094
|
bb = sBb
|
|
@@ -3497,18 +4101,25 @@ export class Inflater {
|
|
|
3497
4101
|
op += 1
|
|
3498
4102
|
} else if (kind === INF_LENGTH) {
|
|
3499
4103
|
const lx: i32 = (e >> 11) & 15
|
|
3500
|
-
if (lx >
|
|
4104
|
+
if (lx > left) {
|
|
3501
4105
|
bb = sBb
|
|
3502
4106
|
bc = sBc
|
|
3503
4107
|
ip = sIp
|
|
3504
4108
|
status = more ? INFLATE_NEED_INPUT : DEFLATE_ERR_TRUNCATED
|
|
3505
4109
|
break
|
|
3506
4110
|
}
|
|
3507
|
-
//
|
|
3508
|
-
//
|
|
3509
|
-
const
|
|
4111
|
+
// A length base is at most 258 and its extra bits at most 5: an
|
|
4112
|
+
// entry past those is one `build` did not make.
|
|
4113
|
+
const lbase: i32 = e >> 16
|
|
4114
|
+
const lextra: i32 = toI32(bb & ((toU64(1) << toU64(lx)) - toU64(1)))
|
|
4115
|
+
if (lbase < 0 || lbase > 258 || lextra < 0 || lextra > 32767) {
|
|
4116
|
+
status = DEFLATE_ERR_SYMBOL
|
|
4117
|
+
break
|
|
4118
|
+
}
|
|
4119
|
+
const len: i32 = lbase + lextra
|
|
3510
4120
|
bb = bb >> toU64(lx)
|
|
3511
|
-
|
|
4121
|
+
const rest: i32 = left - lx
|
|
4122
|
+
bc = rest
|
|
3512
4123
|
const d: i32 = this.lookup(dt, INF_TABLE_DIST, de, bb, bc)
|
|
3513
4124
|
if (d === -1) {
|
|
3514
4125
|
bb = sBb
|
|
@@ -3523,7 +4134,8 @@ export class Inflater {
|
|
|
3523
4134
|
}
|
|
3524
4135
|
const dn: i32 = d & 15
|
|
3525
4136
|
const dx: i32 = (d >> 11) & 15
|
|
3526
|
-
|
|
4137
|
+
const used: i32 = dn + dx
|
|
4138
|
+
if (used > rest) {
|
|
3527
4139
|
bb = sBb
|
|
3528
4140
|
bc = sBc
|
|
3529
4141
|
ip = sIp
|
|
@@ -3531,30 +4143,49 @@ export class Inflater {
|
|
|
3531
4143
|
break
|
|
3532
4144
|
}
|
|
3533
4145
|
bb = bb >> toU64(dn)
|
|
3534
|
-
//
|
|
3535
|
-
//
|
|
3536
|
-
const
|
|
4146
|
+
// A distance base is 1 to 24577 and its extra bits at most 13, as
|
|
4147
|
+
// for a length.
|
|
4148
|
+
const dbase: i32 = d >> 16
|
|
4149
|
+
const dextra: i32 = toI32(bb & ((toU64(1) << toU64(dx)) - toU64(1)))
|
|
4150
|
+
if (dbase < 1 || dbase > 32768 || dextra < 0 || dextra > 32767) {
|
|
4151
|
+
status = DEFLATE_ERR_SYMBOL
|
|
4152
|
+
break
|
|
4153
|
+
}
|
|
4154
|
+
const dist: i32 = dbase + dextra
|
|
3537
4155
|
bb = bb >> toU64(dx)
|
|
3538
|
-
bc =
|
|
3539
|
-
|
|
4156
|
+
bc = rest - used
|
|
4157
|
+
// `inflate` checked `histStart <= outPos <= outEnd`, and every step
|
|
4158
|
+
// keeps it.
|
|
4159
|
+
if (histStart < 0 || op < 0 || op < histStart || op > outEnd) {
|
|
4160
|
+
status = DEFLATE_ERR_TRUNCATED
|
|
4161
|
+
break
|
|
4162
|
+
}
|
|
4163
|
+
const span: i32 = op - histStart
|
|
4164
|
+
const room: i32 = outEnd - op
|
|
4165
|
+
if (room < 0) {
|
|
4166
|
+
status = DEFLATE_ERR_TRUNCATED
|
|
4167
|
+
break
|
|
4168
|
+
}
|
|
4169
|
+
if (dist > span) {
|
|
3540
4170
|
status = DEFLATE_ERR_DISTANCE
|
|
3541
4171
|
break
|
|
3542
4172
|
}
|
|
3543
|
-
|
|
4173
|
+
// `op + len` is `outEnd` less the room the copy leaves.
|
|
4174
|
+
const gap: i32 = room - len
|
|
4175
|
+
if (gap < 0) {
|
|
3544
4176
|
bb = sBb
|
|
3545
4177
|
bc = sBc
|
|
3546
4178
|
ip = sIp
|
|
3547
4179
|
status = INFLATE_NEED_OUTPUT
|
|
3548
4180
|
break
|
|
3549
4181
|
}
|
|
3550
|
-
|
|
3551
|
-
// `
|
|
3552
|
-
let from: i32 =
|
|
3553
|
-
|
|
3554
|
-
while (op < stop) {
|
|
4182
|
+
const stop: i32 = outEnd - gap
|
|
4183
|
+
// `dist` is at least 1, so the second test never stops the copy.
|
|
4184
|
+
let from: i32 = op - dist
|
|
4185
|
+
while (op < stop && from < op) {
|
|
3555
4186
|
uncheckedSet(out, op, uncheckedGet(out, from))
|
|
3556
4187
|
op += 1
|
|
3557
|
-
from
|
|
4188
|
+
from += 1
|
|
3558
4189
|
}
|
|
3559
4190
|
} else if (kind === INF_END) {
|
|
3560
4191
|
this.state = INF_S_BLOCK
|
|
@@ -3580,18 +4211,27 @@ export class Inflater {
|
|
|
3580
4211
|
* rules out. A code is decoded only once all its bits are here.
|
|
3581
4212
|
*/
|
|
3582
4213
|
private lookup(t: InflateTable, kind: i32, entries: i32[], bb: u64, bc: i32): i32 {
|
|
3583
|
-
// Codes are at most 15 bits, and a table is under 2^15 entries:
|
|
3584
|
-
//
|
|
3585
|
-
const
|
|
3586
|
-
|
|
3587
|
-
|
|
4214
|
+
// Codes are at most 15 bits, and a table is under 2^15 entries: a
|
|
4215
|
+
// table past those is one `build` did not make.
|
|
4216
|
+
const bits: i32 = t.bits
|
|
4217
|
+
if (bits < 1 || bits > 15) {
|
|
4218
|
+
return -2
|
|
4219
|
+
}
|
|
4220
|
+
const mask: u64 = (toU64(1) << toU64(bits)) - toU64(1)
|
|
4221
|
+
const i: i32 = toI32(bb & mask)
|
|
4222
|
+
if (i < 0 || i >= toI32(entries.length)) {
|
|
3588
4223
|
return -2
|
|
3589
4224
|
}
|
|
3590
4225
|
let e: i32 = entries[i]
|
|
3591
4226
|
if (((e >> 8) & 7) === INF_SUB) {
|
|
3592
4227
|
const root: i32 = e & 15
|
|
3593
4228
|
const subMask: u64 = (toU64(1) << toU64((e >> 11) & 15)) - toU64(1)
|
|
3594
|
-
const
|
|
4229
|
+
const start: i32 = e >> 16
|
|
4230
|
+
const off: i32 = toI32((bb >> toU64(root)) & subMask)
|
|
4231
|
+
if (start < 0 || start > 32767 || off < 0 || off > 32767) {
|
|
4232
|
+
return -2
|
|
4233
|
+
}
|
|
4234
|
+
const at: i32 = start + off
|
|
3595
4235
|
if (at >= toI32(entries.length)) {
|
|
3596
4236
|
return -2
|
|
3597
4237
|
}
|
|
@@ -3631,14 +4271,41 @@ export class Inflater {
|
|
|
3631
4271
|
if (limit < 0) {
|
|
3632
4272
|
panic("Inflater.decompress: a negative limit")
|
|
3633
4273
|
}
|
|
3634
|
-
//
|
|
3635
|
-
//
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
4274
|
+
// `deflateWindow` checked `off + len` against `src` and `at` against
|
|
4275
|
+
// `out`, so the stream ends `src.length` less the bytes after it, and the
|
|
4276
|
+
// output `out.length` less the room it leaves; the input is read from
|
|
4277
|
+
// `off` on.
|
|
4278
|
+
if (off < 0 || len < 0 || at < 0) {
|
|
4279
|
+
this.consumed = 0
|
|
4280
|
+
return DEFLATE_ERR_TRUNCATED
|
|
4281
|
+
}
|
|
4282
|
+
const after: i32 = toI32(src.length) - off
|
|
4283
|
+
const room: i32 = toI32(out.length) - at
|
|
4284
|
+
if (after < 0 || room < 0) {
|
|
4285
|
+
this.consumed = 0
|
|
4286
|
+
return DEFLATE_ERR_TRUNCATED
|
|
4287
|
+
}
|
|
4288
|
+
const inGap: i32 = after - len
|
|
4289
|
+
const take: i32 = limit < room ? limit : room
|
|
4290
|
+
if (inGap < 0 || take < 0) {
|
|
4291
|
+
this.consumed = 0
|
|
4292
|
+
return DEFLATE_ERR_TRUNCATED
|
|
4293
|
+
}
|
|
4294
|
+
const outGap: i32 = room - take
|
|
4295
|
+
if (outGap < 0) {
|
|
4296
|
+
this.consumed = 0
|
|
4297
|
+
return DEFLATE_ERR_TRUNCATED
|
|
4298
|
+
}
|
|
4299
|
+
const inEnd: i32 = toI32(src.length) - inGap
|
|
4300
|
+
const end: i32 = toI32(out.length) - outGap
|
|
3639
4301
|
this.reset()
|
|
3640
4302
|
const r: i32 = this.inflate(src, off, inEnd, false, out, at, end, at)
|
|
3641
|
-
|
|
4303
|
+
const pos: i32 = this.inPos
|
|
4304
|
+
if (pos < 0) {
|
|
4305
|
+
this.consumed = 0
|
|
4306
|
+
return DEFLATE_ERR_TRUNCATED
|
|
4307
|
+
}
|
|
4308
|
+
this.consumed = pos - off
|
|
3642
4309
|
if (r === INFLATE_NEED_OUTPUT) {
|
|
3643
4310
|
return DEFLATE_ERR_OUTPUT_LIMIT
|
|
3644
4311
|
}
|