@amritk/nish-x86_64-linux 0.44.0 → 0.45.1

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.
Files changed (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
package/std/net/hpack.ts CHANGED
@@ -123,6 +123,9 @@ export const HPACK_ERR_SIZE_UPDATE_MISSING: i32 = -8
123
123
  */
124
124
  export const HPACK_ERR_COPY_BUDGET: i32 = -9
125
125
 
126
+ /** What `hpackHuffmanLength` answers for a coded length that does not fit `i32`, or a code cut short. */
127
+ const HPACK_HUFFMAN_TOO_LONG: i32 = 2147483647
128
+
126
129
  /**
127
130
  * How many times the larger of the table's maximum size and a block's length
128
131
  * the decoder copies into its table for one block, at most
@@ -347,8 +350,10 @@ const hpackAppendText = (out: u8[], text: string): void => {
347
350
 
348
351
  /** Appends `src[at, at + n)`, a window the caller has checked, to `out`. */
349
352
  const hpackAppend = (out: u8[], src: u8[], at: i32, n: i32): void => {
350
- // The length test is the caller's check again, there so the prover drops the bounds check.
351
- for (let i: i32 = at; i < at + n && i >= 0 && i < toI32(src.length); i += 1) {
353
+ // Widened, so `at + n` cannot wrap; the length test is the caller's check again, there so the
354
+ // prover drops the bounds check.
355
+ const end: i64 = toI64(at) + toI64(n)
356
+ for (let i: i32 = at; toI64(i) < end && i >= 0 && i < toI32(src.length); i += 1) {
352
357
  out.push(src[i])
353
358
  }
354
359
  }
@@ -382,7 +387,10 @@ const hpackSameAsWindow = (bytes: u8[], store: u8[], at: i32, n: i32): boolean =
382
387
  return false
383
388
  }
384
389
  for (let i: i32 = 0; i < n && i < toI32(bytes.length); i += 1) {
385
- if (bytes[i] !== store[at + i]) {
390
+ // `at` and `i` are each below 2^31, so the unsigned sum cannot wrap; a sum past `store`, a window
391
+ // the caller has checked, reads as a difference.
392
+ const j: i32 = toI32(toU32(at) + toU32(i))
393
+ if (at < 0 || j < 0 || j >= toI32(store.length) || bytes[i] !== store[j]) {
386
394
  return false
387
395
  }
388
396
  }
@@ -450,56 +458,102 @@ export class HpackHuffman {
450
458
 
451
459
  /** Rebuilds the code from Appendix B's 257 code lengths. Build one and reuse it: each build allocates. */
452
460
  constructor() {
461
+ // Every access below goes through the field, never a local holding its array, which is what lets
462
+ // the layout pass store each array inside the object (`src/inline-arrays.ts`).
453
463
  this.codes = new Array<i32>(257)
454
464
  this.lengths = new Array<i32>(257)
455
465
  this.symbols = new Array<i32>(257)
456
466
  this.firstCode = new Array<i32>(31)
457
467
  this.countAt = new Array<i32>(31)
458
468
  this.baseRank = new Array<i32>(31)
459
- for (let sym: i32 = 0; sym <= HPACK_EOS; sym += 1) {
460
- this.lengths[sym] = toI32(HPACK_HUFFMAN_LENGTHS.charCodeAt(sym)) - 60
469
+ // Every bound below is the array's own length, which the constants fix at 257 symbols and 31 code
470
+ // lengths, so no test ever stops a loop early.
471
+ const table: string = HPACK_HUFFMAN_LENGTHS
472
+ for (let sym: i32 = 0; sym < toI32(this.lengths.length) && sym < toI32(table.length); sym += 1) {
473
+ this.lengths[sym] = toI32(table.charCodeAt(sym)) - 60
474
+ }
475
+ // How many symbols each length has. Appendix B's lengths are 5 to 30, so every one lands in a row.
476
+ for (let sym: i32 = 0; sym < toI32(this.lengths.length); sym += 1) {
477
+ const len: i32 = this.lengths[sym]
478
+ if (len >= 0 && len < toI32(this.countAt.length)) {
479
+ this.countAt[len] = toI32(toU32(this.countAt[len]) + 1)
480
+ }
461
481
  }
462
- let code: i32 = 0
463
- let rank: i32 = 0
464
- for (let len: i32 = 5; len <= HPACK_HUFFMAN_MAX_BITS; len += 1) {
465
- this.firstCode[len] = code
466
- this.baseRank[len] = rank
467
- for (let sym: i32 = 0; sym <= HPACK_EOS; sym += 1) {
468
- if (this.lengths[sym] === len) {
469
- this.codes[sym] = code
470
- this.symbols[rank] = sym
471
- code += 1
472
- rank += 1
482
+ // Canonical order is by length, then by symbol: each length's first code and rank follow from the
483
+ // counts of the shorter ones. Unsigned: the code is below 2^30 and the rank at most 257, so neither
484
+ // can wrap. Each count goes back to 0, to count again as the pass after hands the codes out.
485
+ let code: u32 = 0
486
+ let rank: u32 = 0
487
+ for (
488
+ let len: i32 = 5;
489
+ len <= HPACK_HUFFMAN_MAX_BITS &&
490
+ len < toI32(this.firstCode.length) &&
491
+ len < toI32(this.baseRank.length) &&
492
+ len < toI32(this.countAt.length);
493
+ len += 1
494
+ ) {
495
+ this.firstCode[len] = toI32(code)
496
+ this.baseRank[len] = toI32(rank)
497
+ const count: u32 = toU32(this.countAt[len])
498
+ this.countAt[len] = 0
499
+ code = (code + count) << 1
500
+ rank += count
501
+ }
502
+ // Walking the symbols in order hands each length its codes in symbol order, as the canonical code
503
+ // does, and leaves every count as it was. Appendix B has 257 symbols, so the rank never reaches
504
+ // past `symbols`.
505
+ for (let sym: i32 = 0; sym < toI32(this.lengths.length) && sym < toI32(this.codes.length); sym += 1) {
506
+ const len: i32 = this.lengths[sym]
507
+ if (
508
+ len >= 5 &&
509
+ len <= HPACK_HUFFMAN_MAX_BITS &&
510
+ len < toI32(this.firstCode.length) &&
511
+ len < toI32(this.baseRank.length) &&
512
+ len < toI32(this.countAt.length)
513
+ ) {
514
+ const done: u32 = toU32(this.countAt[len])
515
+ const at: i32 = toI32(toU32(this.baseRank[len]) + done)
516
+ if (at >= 0 && at < toI32(this.symbols.length)) {
517
+ this.codes[sym] = toI32(toU32(this.firstCode[len]) + done)
518
+ this.symbols[at] = sym
519
+ this.countAt[len] = toI32(done + 1)
473
520
  }
474
521
  }
475
- this.countAt[len] = rank - this.baseRank[len]
476
- code = code << 1
477
522
  }
478
523
  }
479
524
  }
480
525
 
481
526
  /**
482
527
  * The length of `src[off, off + len)` once Huffman-coded, in bytes, padding included. A window outside
483
- * `src` panics, as does a coded length past 2^31 - 1 bytes.
528
+ * `src` panics. A coded length past 2^31 - 1 bytes answers 2^31 - 1, which is never shorter than the raw
529
+ * window, so a caller comparing the two keeps the octets raw.
484
530
  * @param h The code to measure with.
485
531
  * @param src The octets to measure; only read.
486
532
  * @param off Where the window starts, an index into `src`.
487
533
  * @param len How many octets from `off` on to measure.
488
- * @returns The number of bytes `hpackHuffmanEncode` would append for the same window, 0 for an empty one.
534
+ * @returns The number of bytes `hpackHuffmanEncode` would append for the same window, 0 for an empty one;
535
+ * 2^31 - 1 when that does not fit an `i32`, or when `h`'s tables were cut short (only by writing their
536
+ * internal fields).
489
537
  */
490
538
  export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i32): i32 => {
491
539
  hpackCheckWindow("hpackHuffmanLength", src, off, len)
492
- let bits: i64 = 0
493
- const end: i32 = off + len
494
- // The window check has already proved `0 <= i < src.length` for every `i`
495
- // below `end`; the loop condition says so again, which is what lets the
496
- // prover drop the bounds check on each read. The two loops below do the same.
497
- for (let i: i32 = off; i >= 0 && i < end && i < toI32(src.length); i += 1) {
498
- bits += toI64(h.lengths[toI32(src[i])])
499
- }
500
- const bytes: i64 = (bits + toI64(7)) >> toI64(3)
501
- if (bytes > toI64(2147483647)) {
502
- panic("hpackHuffmanLength: more than 2^31 - 1 bytes once coded")
540
+ // Unsigned: at most 30 bits for each of fewer than 2^31 octets, far below 2^64.
541
+ let bits: u64 = 0
542
+ // Widened, so `off + len` cannot wrap. The window check has already proved
543
+ // `0 <= i < src.length` for every `i` below `end`; the loop condition says so
544
+ // again, which is what lets the prover drop the bounds check on each read.
545
+ // The two loops below do the same.
546
+ const end: i64 = toI64(off) + toI64(len)
547
+ for (let i: i32 = off; i >= 0 && toI64(i) < end && i < toI32(src.length); i += 1) {
548
+ const sym: i32 = toI32(src[i])
549
+ if (sym >= toI32(h.lengths.length)) {
550
+ return HPACK_HUFFMAN_TOO_LONG
551
+ }
552
+ bits += toU64(h.lengths[sym] & 31)
553
+ }
554
+ const bytes: u64 = (bits + toU64(7)) >> toU64(3)
555
+ if (bytes > toU64(2147483647)) {
556
+ return HPACK_HUFFMAN_TOO_LONG
503
557
  }
504
558
  return toI32(bytes)
505
559
  }
@@ -510,7 +564,8 @@ export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i3
510
564
  *
511
565
  * The accumulator holds fewer than eight waiting bits between symbols, and a
512
566
  * code is at most 30, so it never needs more than 37 bits.
513
- * A window outside `src` panics.
567
+ * A window outside `src` panics. A table cut short (only by writing its
568
+ * internal fields) ends the coding at the first octet it has no code for.
514
569
  * @param h The code to encode with.
515
570
  * @param src The octets to encode; only read.
516
571
  * @param off Where the window starts, an index into `src`.
@@ -519,23 +574,27 @@ export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i3
519
574
  */
520
575
  export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): void => {
521
576
  hpackCheckWindow("hpackHuffmanEncode", src, off, len)
522
- let acc: i64 = 0
523
- let bits: i32 = 0
524
- const end: i32 = off + len
525
- for (let i: i32 = off; i >= 0 && i < end && i < toI32(src.length); i += 1) {
577
+ let acc: u64 = 0
578
+ // Unsigned: fewer than eight waiting bits plus one code of at most 31, so it never wraps.
579
+ let bits: u32 = 0
580
+ const end: i64 = toI64(off) + toI64(len)
581
+ for (let i: i32 = off; i >= 0 && toI64(i) < end && i < toI32(src.length); i += 1) {
526
582
  const sym: i32 = toI32(src[i])
527
- const n: i32 = h.lengths[sym]
528
- acc = (acc << toI64(n)) | toI64(h.codes[sym])
583
+ if (sym >= toI32(h.lengths.length) || sym >= toI32(h.codes.length)) {
584
+ break
585
+ }
586
+ const n: u32 = toU32(h.lengths[sym] & 31)
587
+ acc = (acc << toU64(n)) | toU64(h.codes[sym])
529
588
  bits += n
530
589
  while (bits >= 8) {
531
590
  bits -= 8
532
- out.push(toU8((acc >> toI64(bits)) & toI64(255)))
591
+ out.push(toU8((acc >> toU64(bits)) & toU64(255)))
533
592
  }
534
- acc = acc & ((toI64(1) << toI64(bits)) - toI64(1))
593
+ acc = acc & ((toU64(1) << toU64(bits)) - toU64(1))
535
594
  }
536
595
  if (bits > 0) {
537
- const pad: i32 = 8 - bits
538
- out.push(toU8(((acc << toI64(pad)) | ((toI64(1) << toI64(pad)) - toI64(1))) & toI64(255)))
596
+ const pad: u32 = 8 - bits
597
+ out.push(toU8(((acc << toU64(pad)) | ((toU64(1) << toU64(pad)) - toU64(1))) & toU64(255)))
539
598
  }
540
599
  }
541
600
 
@@ -557,22 +616,44 @@ export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i3
557
616
  * since every code is at least five bits long.
558
617
  * @returns `HPACK_OK` when the window decoded whole and ended in valid padding; `HPACK_ERR_HUFFMAN_EOS` when
559
618
  * a complete code was the EOS symbol, at the point it was read; `HPACK_ERR_HUFFMAN_PADDING` when the bits
560
- * left after the last symbol are more than seven or not all ones.
619
+ * left after the last symbol are more than seven or not all ones, and when thirty bits match no code,
620
+ * which only a table cut short (by writing its internal fields) allows.
561
621
  */
562
622
  export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): i32 => {
563
623
  hpackCheckWindow("hpackHuffmanDecode", src, off, len)
564
- let code: i32 = 0
565
- let bits: i32 = 0
566
- const end: i32 = off + len
567
- for (let i: i32 = off; i >= 0 && i < end && i < toI32(src.length); i += 1) {
568
- const byte: i32 = toI32(src[i])
569
- for (let k: i32 = 7; k >= 0; k -= 1) {
570
- code = (code << 1) | ((byte >> k) & 1)
624
+ // Unsigned, and `bits` never passes 30: the code is complete, so thirty bits always end a symbol,
625
+ // and the row test below gives up before a 31st.
626
+ let code: u32 = 0
627
+ let bits: u32 = 0
628
+ const end: i64 = toI64(off) + toI64(len)
629
+ for (let i: i32 = off; i >= 0 && toI64(i) < end && i < toI32(src.length); i += 1) {
630
+ const byte: u32 = toU32(src[i])
631
+ for (let k: u32 = 8; k > 0; k -= 1) {
632
+ code = (code << 1) | ((byte >> (k - 1)) & 1)
571
633
  bits += 1
572
- if (bits >= 5) {
573
- const at: i32 = code - h.firstCode[bits]
574
- if (at >= 0 && at < h.countAt[bits]) {
575
- const sym: i32 = h.symbols[h.baseRank[bits] + at]
634
+ const row: i32 = toI32(bits)
635
+ if (row > HPACK_HUFFMAN_MAX_BITS) {
636
+ return HPACK_ERR_HUFFMAN_PADDING
637
+ }
638
+ // No code is shorter than five bits, so the tables are read only from there on.
639
+ if (row >= 5) {
640
+ if (
641
+ row >= toI32(h.firstCode.length) ||
642
+ row >= toI32(h.countAt.length) ||
643
+ row >= toI32(h.baseRank.length)
644
+ ) {
645
+ return HPACK_ERR_HUFFMAN_PADDING
646
+ }
647
+ // Both sides fit an `i32`, so the `i64` difference cannot wrap.
648
+ const at: i64 = toI64(code) - toI64(h.firstCode[row])
649
+ if (at >= 0 && at < toI64(h.countAt[row])) {
650
+ // `at` is below `h.countAt[row]`, so in `[0, 2^31)`; the unsigned sum of two such cannot wrap,
651
+ // and one past `symbols` matches no code.
652
+ const slot: i32 = toI32(toU32(h.baseRank[row]) + toU32(at))
653
+ if (slot < 0 || slot >= toI32(h.symbols.length)) {
654
+ return HPACK_ERR_HUFFMAN_PADDING
655
+ }
656
+ const sym: i32 = h.symbols[slot]
576
657
  if (sym === HPACK_EOS) {
577
658
  return HPACK_ERR_HUFFMAN_EOS
578
659
  }
@@ -583,7 +664,7 @@ export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i3
583
664
  }
584
665
  }
585
666
  }
586
- if (bits > 7 || code !== (1 << bits) - 1) {
667
+ if (bits > 7 || code !== (toU32(1) << bits) - 1) {
587
668
  return HPACK_ERR_HUFFMAN_PADDING
588
669
  }
589
670
  return HPACK_OK
@@ -618,7 +699,9 @@ export class HpackCursor {
618
699
  hpackCheckWindow("HpackCursor", buf, off, len)
619
700
  this.buf = buf
620
701
  this.pos = off
621
- this.end = off + len
702
+ // The window check proved `off` and `len` non-negative with a sum at most `buf.length`, so the unsigned
703
+ // sum does not wrap.
704
+ this.end = toI32(toU32(off) + toU32(len))
622
705
  }
623
706
 
624
707
  /**
@@ -629,7 +712,9 @@ export class HpackCursor {
629
712
  hpackCheckWindow("HpackCursor", buf, off, len)
630
713
  this.buf = buf
631
714
  this.pos = off
632
- this.end = off + len
715
+ // The window check proved `off` and `len` non-negative with a sum at most `buf.length`, so the unsigned
716
+ // sum does not wrap.
717
+ this.end = toI32(toU32(off) + toU32(len))
633
718
  }
634
719
  }
635
720
 
@@ -649,13 +734,14 @@ export const hpackEncodeInteger = (out: u8[], flags: i32, prefixBits: i32, value
649
734
  if (prefixBits < 1 || prefixBits > 8 || value < 0) {
650
735
  panic(`hpackEncodeInteger: a value of ${value} with a ${prefixBits}-bit prefix, outside 1 to 8`)
651
736
  }
652
- const max: i32 = (1 << prefixBits) - 1
737
+ // Unsigned: the prefix is 1 to 8 bits, so `max` is 1 to 255, and `value` is at least `max` below.
738
+ const max: i32 = toI32((toU32(1) << toU32(prefixBits)) - toU32(1))
653
739
  if (value < max) {
654
740
  out.push(toU8(flags | value))
655
741
  return
656
742
  }
657
743
  out.push(toU8(flags | max))
658
- let rest: i32 = value - max
744
+ let rest: i32 = toI32(toU32(value) - toU32(max))
659
745
  while (rest >= 128) {
660
746
  out.push(toU8((rest & 127) | 128))
661
747
  rest = rest >> 7
@@ -682,24 +768,29 @@ export const hpackDecodeInteger = (c: HpackCursor, prefixBits: i32): i32 => {
682
768
  if (prefixBits < 1 || prefixBits > 8) {
683
769
  panic(`hpackDecodeInteger: a ${prefixBits}-bit prefix, outside 1 to 8`)
684
770
  }
685
- if (c.pos >= c.end) {
771
+ // A position outside the buffer is a cursor whose public `pos` a caller moved; it has nothing to read.
772
+ const buf: u8[] = c.buf
773
+ const at: i32 = c.pos
774
+ if (at >= c.end || at < 0 || at >= toI32(buf.length)) {
686
775
  return HPACK_ERR_TRUNCATED
687
776
  }
688
- const max: i32 = (1 << prefixBits) - 1
689
- const first: i32 = toI32(c.buf[c.pos]) & max
690
- c.pos += 1
777
+ const max: i32 = toI32((toU32(1) << toU32(prefixBits)) - toU32(1))
778
+ const first: i32 = toI32(buf[at]) & max
779
+ c.pos = at + 1
691
780
  if (first < max) {
692
781
  return first
693
782
  }
694
- let value: i64 = toI64(max)
783
+ // Unsigned: at most 2^31 - 1 before each step, plus at most 127 * 2^28, far below 2^64.
784
+ let value: u64 = toU64(max)
695
785
  for (let shift: i32 = 0; shift <= 28; shift += 7) {
696
- if (c.pos >= c.end) {
786
+ const next: i32 = c.pos
787
+ if (next >= c.end || next < 0 || next >= toI32(buf.length)) {
697
788
  return HPACK_ERR_TRUNCATED
698
789
  }
699
- const b: i32 = toI32(c.buf[c.pos])
700
- c.pos += 1
701
- value += toI64(b & 127) << toI64(shift)
702
- if (value > toI64(2147483647)) {
790
+ const b: i32 = toI32(buf[next])
791
+ c.pos = next + 1
792
+ value += toU64(b & 127) << toU64(shift)
793
+ if (value > toU64(2147483647)) {
703
794
  return HPACK_ERR_INTEGER_OVERFLOW
704
795
  }
705
796
  if ((b & 128) === 0) {
@@ -711,7 +802,7 @@ export const hpackDecodeInteger = (c: HpackCursor, prefixBits: i32): i32 => {
711
802
 
712
803
  /**
713
804
  * Appends `data` as a §5.2 string literal: Huffman-coded when `huffman` is
714
- * true, else raw. A caller who wants the shorter of the two compares
805
+ * true and the coded length fits below 2^31 - 1, else raw. A caller who wants the shorter of the two compares
715
806
  * `hpackHuffmanLength` with the raw length.
716
807
  * @param h The code to encode with when `huffman` is true.
717
808
  * @param out Where the literal goes, appended after what it holds: its length with a 7-bit prefix and the
@@ -721,8 +812,11 @@ export const hpackDecodeInteger = (c: HpackCursor, prefixBits: i32): i32 => {
721
812
  */
722
813
  export const hpackEncodeString = (h: HpackHuffman, out: u8[], data: u8[], huffman: boolean): void => {
723
814
  const n: i32 = toI32(data.length)
724
- if (huffman) {
725
- hpackEncodeInteger(out, 128, 7, hpackHuffmanLength(h, data, 0, n))
815
+ // `hpackHuffmanLength` answers 2^31 - 1 when the coded length does not fit, or the code is cut short; the
816
+ // octets then go raw, so the length written is always the length that follows.
817
+ const coded: i32 = huffman ? hpackHuffmanLength(h, data, 0, n) : HPACK_HUFFMAN_TOO_LONG
818
+ if (coded < HPACK_HUFFMAN_TOO_LONG) {
819
+ hpackEncodeInteger(out, 128, 7, coded)
726
820
  hpackHuffmanEncode(h, data, 0, n, out)
727
821
  return
728
822
  }
@@ -748,24 +842,30 @@ export const hpackEncodeString = (h: HpackHuffman, out: u8[], data: u8[], huffma
748
842
  * `HPACK_ERR_HUFFMAN_EOS` or `HPACK_ERR_HUFFMAN_PADDING` from `hpackHuffmanDecode` for a Huffman-coded one.
749
843
  */
750
844
  export const hpackDecodeString = (h: HpackHuffman, c: HpackCursor, out: u8[]): i32 => {
751
- if (c.pos >= c.end) {
845
+ // A position or end outside the buffer is a cursor whose public `pos` a caller moved; it has nothing to
846
+ // read.
847
+ const buf: u8[] = c.buf
848
+ const first: i32 = c.pos
849
+ if (first >= c.end || first < 0 || first >= toI32(buf.length)) {
752
850
  return HPACK_ERR_TRUNCATED
753
851
  }
754
- const huffman: boolean = (toI32(c.buf[c.pos]) & 128) !== 0
852
+ const huffman: boolean = (toI32(buf[first]) & 128) !== 0
755
853
  const n: i32 = hpackDecodeInteger(c, 7)
756
854
  if (n < 0) {
757
855
  return n
758
856
  }
759
- if (n > c.end - c.pos) {
857
+ const at: i32 = c.pos
858
+ // Widened, so the sum of two `i32`s cannot wrap.
859
+ const stop: i64 = toI64(at) + toI64(n)
860
+ if (at < 0 || stop > toI64(c.end) || stop > toI64(buf.length)) {
760
861
  return HPACK_ERR_TRUNCATED
761
862
  }
762
- const at: i32 = c.pos
763
- c.pos += n
863
+ c.pos = toI32(stop)
764
864
  if (huffman) {
765
- return hpackHuffmanDecode(h, c.buf, at, n, out)
865
+ return hpackHuffmanDecode(h, buf, at, n, out)
766
866
  }
767
- for (let i: i32 = at; i < at + n; i += 1) {
768
- out.push(c.buf[i])
867
+ for (let i: i32 = at; toI64(i) < stop && i < toI32(buf.length); i += 1) {
868
+ out.push(buf[i])
769
869
  }
770
870
  return HPACK_OK
771
871
  }
@@ -822,84 +922,111 @@ export class HpackTable {
822
922
  this.maxSize = maxSize
823
923
  }
824
924
 
825
- /** How many entries `entries` describes, evicted ones included. */
826
- private slots(): i32 {
827
- return toI32(this.entries.length) >> 1
828
- }
829
-
830
925
  /**
831
926
  * How many entries the table holds.
832
927
  * @returns The live entry count: dynamic indexes 1 to it are in range; 0 when the table is empty.
833
928
  */
834
929
  count(): i32 {
835
- return this.slots() - this.first
930
+ // Both are in `[0, 2^30)`, so the difference cannot wrap; a `first` outside them, which only a caller
931
+ // writing the public field makes, leaves no entry reachable.
932
+ const n: i32 = toI32(this.entries.length) >> 1
933
+ const f: i32 = this.first
934
+ return f >= 0 && f <= n ? n - f : 0
836
935
  }
837
936
 
838
- /** Where dynamic entry `i`, 1 being the newest, sits in `entries`; `i` must be in range. */
937
+ /** Where dynamic entry `i`, 1 being the newest, sits in `entries`; -1 for an `i` outside 1 to `count()`. */
839
938
  private slot(i: i32): i32 {
840
- return this.slots() - i
939
+ if (i < 1 || i > this.count()) {
940
+ return -1
941
+ }
942
+ // `n` is in `[0, 2^30)` and `i` at least 1, so the difference cannot wrap.
943
+ const n: i32 = toI32(this.entries.length) >> 1
944
+ return n - i
841
945
  }
842
946
 
843
- /** Where entry `k` of `entries` starts in `bytes`. */
947
+ /** Where entry `k` of `entries` starts in `bytes`; -1 for a `k` `entries` does not have. */
844
948
  private startOf(k: i32): i32 {
845
- return this.entries[k * 2]
949
+ const entries: i32[] = this.entries
950
+ // Unsigned: a `k` of 2^30 or more, or a negative one, wraps to a position the test below refuses.
951
+ const at: i32 = toI32(toU32(k) << 1)
952
+ return k >= 0 && at >= 0 && at < toI32(entries.length) ? entries[at] : -1
846
953
  }
847
954
 
848
955
  /** Where entry `k` of `entries` ends in `bytes`: where the next starts, or the end of `bytes`. */
849
956
  private endOf(k: i32): i32 {
850
- return k + 1 < this.slots() ? this.entries[k * 2 + 2] : toI32(this.bytes.length)
957
+ const entries: i32[] = this.entries
958
+ const at: i32 = toI32((toU32(k) << 1) + 2)
959
+ return k >= 0 && at >= 0 && at < toI32(entries.length) ? entries[at] : toI32(this.bytes.length)
851
960
  }
852
961
 
853
962
  /** What entry `k` of `entries` costs (§4.1); it fits an `i32` because the table holds it. */
854
963
  private costOf(k: i32): i32 {
855
- return this.endOf(k) - this.startOf(k) + HPACK_ENTRY_OVERHEAD
964
+ // Widened, so the arithmetic on two `i32`s cannot wrap; the result fits as the table holds the entry.
965
+ return toI32(toI64(this.endOf(k)) - toI64(this.startOf(k)) + toI64(HPACK_ENTRY_OVERHEAD))
856
966
  }
857
967
 
858
968
  /**
859
969
  * Where the name of dynamic entry `i` starts in `bytes`.
860
- * @param i The dynamic index, 1 being the newest; it must be from 1 to `count()` and is not checked.
861
- * @returns The offset in `bytes`, valid until the table next changes.
970
+ * @param i The dynamic index, 1 being the newest, from 1 to `count()`.
971
+ * @returns The offset in `bytes`, valid until the table next changes; -1 for an `i` outside 1 to `count()`.
862
972
  */
863
973
  nameStart(i: i32): i32 {
864
974
  return this.startOf(this.slot(i))
865
975
  }
866
976
 
867
977
  /**
868
- * How many octets the name of dynamic entry `i` has.
978
+ * How many octets the name of dynamic entry `i` has; -1 for an `i` outside 1 to `count()`.
869
979
  * @internal Read by this module's decoder and encoder; not for callers.
870
980
  */
871
981
  nameLength(i: i32): i32 {
872
- return this.entries[this.slot(i) * 2 + 1]
982
+ const entries: i32[] = this.entries
983
+ const k: i32 = this.slot(i)
984
+ const at: i32 = toI32((toU32(k) << 1) + 1)
985
+ return k >= 0 && at >= 0 && at < toI32(entries.length) ? entries[at] : -1
873
986
  }
874
987
 
875
988
  /**
876
- * Where the value of dynamic entry `i` starts in `bytes`, straight after its name.
989
+ * Where the value of dynamic entry `i` starts in `bytes`, straight after its name; -1 for an `i` outside
990
+ * 1 to `count()`.
877
991
  * @internal Read by this module's decoder and encoder; not for callers.
878
992
  */
879
993
  valueStart(i: i32): i32 {
880
- return this.nameStart(i) + this.nameLength(i)
994
+ const start: i32 = this.nameStart(i)
995
+ const length: i32 = this.nameLength(i)
996
+ if (start < 0 || length < 0) {
997
+ return -1
998
+ }
999
+ // Both are non-negative, and the name lies inside `bytes`, so the unsigned sum does not wrap.
1000
+ return toI32(toU32(start) + toU32(length))
881
1001
  }
882
1002
 
883
1003
  /**
884
- * How many octets the value of dynamic entry `i` has.
1004
+ * How many octets the value of dynamic entry `i` has; -1 for an `i` outside 1 to `count()`.
885
1005
  * @internal Read by this module's decoder and encoder; not for callers.
886
1006
  */
887
1007
  valueLength(i: i32): i32 {
888
- return this.endOf(this.slot(i)) - this.valueStart(i)
1008
+ const k: i32 = this.slot(i)
1009
+ const start: i32 = this.valueStart(i)
1010
+ if (k < 0 || start < 0) {
1011
+ return -1
1012
+ }
1013
+ // The value ends at or after it starts, so the unsigned difference does not wrap.
1014
+ return toI32(toU32(this.endOf(k)) - toU32(start))
889
1015
  }
890
1016
 
891
1017
  /**
892
- * What dynamic entry `i` costs (§4.1).
1018
+ * What dynamic entry `i` costs (§4.1); -1 for an `i` outside 1 to `count()`.
893
1019
  * @internal Read by `HpackDecoder`; not for callers.
894
1020
  */
895
1021
  entrySize(i: i32): i32 {
896
- return this.costOf(this.slot(i))
1022
+ const k: i32 = this.slot(i)
1023
+ return k < 0 ? -1 : this.costOf(k)
897
1024
  }
898
1025
 
899
1026
  /**
900
1027
  * A fresh copy of the name of dynamic entry `i`. It allocates; the codec reads the window instead.
901
- * @param i The dynamic index, 1 being the newest; it must be from 1 to `count()` and is not checked.
902
- * @returns A new array, the caller's to keep.
1028
+ * @param i The dynamic index, 1 being the newest, from 1 to `count()`.
1029
+ * @returns A new array, the caller's to keep; empty for an `i` outside 1 to `count()`.
903
1030
  */
904
1031
  name(i: i32): u8[] {
905
1032
  const out: u8[] = []
@@ -909,8 +1036,8 @@ export class HpackTable {
909
1036
 
910
1037
  /**
911
1038
  * A fresh copy of the value of dynamic entry `i`, as `name` makes the name.
912
- * @param i The dynamic index, 1 being the newest; it must be from 1 to `count()` and is not checked.
913
- * @returns A new array, the caller's to keep.
1039
+ * @param i The dynamic index, 1 being the newest, from 1 to `count()`.
1040
+ * @returns A new array, the caller's to keep; empty for an `i` outside 1 to `count()`.
914
1041
  */
915
1042
  value(i: i32): u8[] {
916
1043
  const out: u8[] = []
@@ -924,37 +1051,58 @@ export class HpackTable {
924
1051
  */
925
1052
  evictTo(limit: i32): void {
926
1053
  while (toI64(this.size) > toI64(limit) && this.count() > 0) {
927
- this.size -= this.costOf(this.first)
928
- this.first += 1
1054
+ // The size counts every live entry's cost, so the unsigned difference does not wrap; and `first` is
1055
+ // below the slot count, under 2^30, while `count()` is positive.
1056
+ this.size = toI32(toU32(this.size) - toU32(this.costOf(this.first)))
1057
+ this.first = toI32(toU32(this.first) + 1)
929
1058
  }
930
1059
  this.compact()
931
1060
  }
932
1061
 
933
1062
  /** Shifts the evicted prefix out of `entries`, and out of `bytes`, once it is half of either. */
934
1063
  private compact(): void {
935
- const end: i32 = toI32(this.bytes.length)
936
- const start: i32 = this.count() > 0 ? this.startOf(this.first) : end
937
- const n: i32 = this.slots()
938
- if (this.first > 0 && this.first * 2 >= n) {
939
- const live: i32 = (n - this.first) * 2
940
- const from: i32 = this.first * 2
941
- for (let k: i32 = 0; k < live; k += 1) {
942
- this.entries[k] = this.entries[from + k]
1064
+ const entries: i32[] = this.entries
1065
+ const bytes: u8[] = this.bytes
1066
+ const end: i32 = toI32(bytes.length)
1067
+ // A live entry starts inside `bytes`; were one not to, nothing of `bytes` would be shifted out.
1068
+ const oldest: i32 = this.count() > 0 ? this.startOf(this.first) : end
1069
+ const start: i32 = oldest >= 0 && oldest <= end ? oldest : 0
1070
+ const n: i32 = toI32(entries.length) >> 1
1071
+ const f: i32 = this.first
1072
+ // `n` is under 2^30 and `f` from 1 to it, so neither `n - f` nor the doubling below wraps.
1073
+ if (f > 0 && f <= n && f >= n - f) {
1074
+ const live: i32 = toI32(toU32(n - f) << 1)
1075
+ const from: i32 = toI32(toU32(f) << 1)
1076
+ for (let k: i32 = 0; k < live && k < toI32(entries.length); k += 1) {
1077
+ const at: i32 = toI32(toU32(from) + toU32(k))
1078
+ if (at >= 0 && at < toI32(entries.length)) {
1079
+ entries[k] = entries[at]
1080
+ }
943
1081
  }
944
- while (toI32(this.entries.length) > live) {
945
- this.entries.pop()
1082
+ while (toI32(entries.length) > live && toI32(entries.length) > 0) {
1083
+ entries.pop()
946
1084
  }
947
1085
  this.first = 0
948
1086
  }
949
- if (start > 0 && start * 2 >= end) {
950
- for (let k: i32 = start; k < end; k += 1) {
951
- this.bytes[k - start] = this.bytes[k]
1087
+ // `start` is from 1 to `end`, so `end - start` cannot wrap, nor `k - start` for `k` from `start` on.
1088
+ if (start > 0 && start >= end - start) {
1089
+ for (let k: i32 = start; k < end && k < toI32(bytes.length); k += 1) {
1090
+ const at: i32 = k - start
1091
+ if (at >= 0 && at < toI32(bytes.length)) {
1092
+ bytes[at] = bytes[k]
1093
+ }
952
1094
  }
953
- while (toI32(this.bytes.length) > end - start) {
954
- this.bytes.pop()
1095
+ const keep: i32 = end - start
1096
+ while (toI32(bytes.length) > keep && toI32(bytes.length) > 0) {
1097
+ bytes.pop()
955
1098
  }
956
- for (let k: i32 = this.first; k < this.slots(); k += 1) {
957
- this.entries[k * 2] -= start
1099
+ // Every live entry starts at or after `start`, so the unsigned difference does not wrap.
1100
+ const slots: i32 = toI32(entries.length) >> 1
1101
+ for (let k: i32 = this.first; k >= 0 && k < slots; k += 1) {
1102
+ const at: i32 = toI32(toU32(k) << 1)
1103
+ if (at >= 0 && at < toI32(entries.length)) {
1104
+ entries[at] = toI32(toU32(entries[at]) - toU32(start))
1105
+ }
958
1106
  }
959
1107
  }
960
1108
  }
@@ -978,10 +1126,12 @@ export class HpackTable {
978
1126
  this.evictTo(nothing)
979
1127
  return nothing
980
1128
  }
981
- this.evictTo(this.maxSize - toI32(cost))
1129
+ // The cost is from 32 to `maxSize`, so neither unsigned sum nor difference below wraps: the size after
1130
+ // eviction is at most `maxSize - cost`, and the octets pushed are the cost less 32.
1131
+ this.evictTo(toI32(toU32(this.maxSize) - toU32(cost)))
982
1132
  this.entries.push(toI32(this.bytes.length))
983
1133
  this.entries.push(toI32(name.length))
984
- let pushed: i32 = 0
1134
+ let pushed: u32 = 0
985
1135
  for (const b of name) {
986
1136
  this.bytes.push(b)
987
1137
  pushed += 1
@@ -990,8 +1140,8 @@ export class HpackTable {
990
1140
  this.bytes.push(b)
991
1141
  pushed += 1
992
1142
  }
993
- this.size += toI32(cost)
994
- return pushed
1143
+ this.size = toI32(toU32(this.size) + toU32(cost))
1144
+ return toI32(pushed)
995
1145
  }
996
1146
 
997
1147
  /**
@@ -1079,11 +1229,20 @@ export class HpackWork {
1079
1229
  * @internal Called by `HpackDecoder` as it gives fields back; not for callers.
1080
1230
  */
1081
1231
  pool(j: i32): u8[][] {
1082
- while (toI32(this.pools.length) <= j) {
1232
+ const at: i32 = j > 0 ? j : 0
1233
+ const pools: u8[][][] = this.pools
1234
+ while (toI32(pools.length) <= at) {
1083
1235
  const fresh: u8[][] = []
1084
- this.pools.push(fresh)
1236
+ pools.push(fresh)
1085
1237
  }
1086
- return this.pools[j]
1238
+ const n: i32 = toI32(pools.length)
1239
+ if (at >= 0 && at < n) {
1240
+ return pools[at]
1241
+ }
1242
+ // Not reached: the loop above grew `pools` past `at`.
1243
+ const fresh: u8[][] = []
1244
+ pools.push(fresh)
1245
+ return fresh
1087
1246
  }
1088
1247
 
1089
1248
  /**
@@ -1096,14 +1255,20 @@ export class HpackWork {
1096
1255
  const j: i32 = this.classOf(length)
1097
1256
  const pool: u8[][] = this.pool(j)
1098
1257
  const kept: i32 = toI32(pool.length)
1099
- if (kept === 0) {
1100
- const made: u8[] = new Array<u8>(1 << j)
1101
- hpackEmpty(made)
1102
- return made
1258
+ if (kept > 0 && toI32(pool.length) > 0) {
1259
+ const out: u8[] = pool[kept - 1]
1260
+ pool.pop()
1261
+ return out
1103
1262
  }
1104
- const out: u8[] = pool[kept - 1]
1105
- pool.pop()
1106
- return out
1263
+ // `classOf` answers below `HPACK_FIELD_CLASSES`, so the room is at most 2^23 and an unsigned shift holds it.
1264
+ const room: u32 = toU32(1) << toU32(j)
1265
+ // A length is made at most 65535 at once; a larger class's room is filled out by pushes, which leave the same room.
1266
+ const made: u8[] = new Array<u8>(toU16(room < toU32(65535) ? room : toU32(65535)))
1267
+ while (toU32(made.length) < room) {
1268
+ made.push(0)
1269
+ }
1270
+ hpackEmpty(made)
1271
+ return made
1107
1272
  }
1108
1273
  }
1109
1274
 
@@ -1231,9 +1396,10 @@ export class HpackDecoder {
1231
1396
  hpackAppendText(out, hpackStaticName(index))
1232
1397
  return
1233
1398
  }
1234
- const i: i32 = index - HPACK_STATIC_LENGTH
1399
+ // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1400
+ const i: i32 = toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))
1235
1401
  hpackAppend(out, this.table.bytes, this.table.nameStart(i), this.table.nameLength(i))
1236
- this.tableCopied += toI64(this.table.nameLength(i))
1402
+ this.countCopied(this.table.nameLength(i))
1237
1403
  }
1238
1404
 
1239
1405
  /** Appends the value at `index` to `out`, as `appendNameAt` does the name. */
@@ -1242,9 +1408,10 @@ export class HpackDecoder {
1242
1408
  hpackAppendText(out, hpackStaticValue(index))
1243
1409
  return
1244
1410
  }
1245
- const i: i32 = index - HPACK_STATIC_LENGTH
1411
+ // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1412
+ const i: i32 = toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))
1246
1413
  hpackAppend(out, this.table.bytes, this.table.valueStart(i), this.table.valueLength(i))
1247
- this.tableCopied += toI64(this.table.valueLength(i))
1414
+ this.countCopied(this.table.valueLength(i))
1248
1415
  }
1249
1416
 
1250
1417
  /** How many octets the name at `index` has. */
@@ -1252,7 +1419,8 @@ export class HpackDecoder {
1252
1419
  if (index <= HPACK_STATIC_LENGTH) {
1253
1420
  return toI32(hpackStaticName(index).length)
1254
1421
  }
1255
- return this.table.nameLength(index - HPACK_STATIC_LENGTH)
1422
+ // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1423
+ return this.table.nameLength(toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH)))
1256
1424
  }
1257
1425
 
1258
1426
  /** What the entry at `index` counts towards a header list, without building it. */
@@ -1264,29 +1432,34 @@ export class HpackDecoder {
1264
1432
  toI64(HPACK_ENTRY_OVERHEAD)
1265
1433
  )
1266
1434
  }
1267
- return toI64(this.table.entrySize(index - HPACK_STATIC_LENGTH))
1435
+ // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1436
+ return toI64(this.table.entrySize(toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))))
1437
+ }
1438
+
1439
+ /** Adds `n` octets, when there are any, to `tableCopied`; the count would need 2^63 octets to wrap. */
1440
+ private countCopied(n: i32): void {
1441
+ if (n > 0) {
1442
+ this.tableCopied = toI64(toU64(this.tableCopied) + toU64(n))
1443
+ }
1268
1444
  }
1269
1445
 
1270
1446
  /** Whether `index` names an entry of either table. */
1271
1447
  private hasIndex(index: i32): boolean {
1272
- return index >= 1 && index <= HPACK_STATIC_LENGTH + this.table.count()
1448
+ return index >= 1 && toI64(index) <= toI64(HPACK_STATIC_LENGTH) + toI64(this.table.count())
1273
1449
  }
1274
1450
 
1275
1451
  /** Empties the fields and gives each one's array back, emptied, to its class's pool, for the next block. */
1276
1452
  private clear(): void {
1277
- while (toI32(this.names.length) > 0) {
1453
+ const names: u8[][] = this.names
1454
+ const values: u8[][] = this.values
1455
+ if (toI32(names.length) > 0 || toI32(values.length) > 0) {
1278
1456
  const work: HpackWork = this.working()
1279
- const last: i32 = toI32(this.names.length) - 1
1280
- const name: u8[] = this.names[last]
1281
- const value: u8[] = this.values[last]
1282
- this.names.pop()
1283
- this.values.pop()
1284
- work.pool(work.classOf(toI32(name.length))).push(name)
1285
- work.pool(work.classOf(toI32(value.length))).push(value)
1286
- // Emptied as it goes back, so a reference a caller kept past the block reads nothing until the array is taken again.
1287
- hpackEmpty(name)
1288
- hpackEmpty(value)
1289
- this.neverIndexed.pop()
1457
+ hpackGiveBack(work, names)
1458
+ hpackGiveBack(work, values)
1459
+ }
1460
+ const neverIndexed: boolean[] = this.neverIndexed
1461
+ while (toI32(neverIndexed.length) > 0) {
1462
+ neverIndexed.pop()
1290
1463
  }
1291
1464
  }
1292
1465
 
@@ -1339,19 +1512,32 @@ export class HpackDecoder {
1339
1512
  * those have grown to the largest block and list seen.
1340
1513
  */
1341
1514
  private decodeFields(c: HpackCursor): i32 {
1342
- let listSize: i64 = 0
1515
+ // Unsigned sums: each field adds at most about 2^32, and a block holds fewer than 2^31 fields.
1516
+ let listSize: u64 = 0
1343
1517
  let tooLarge: boolean = false
1344
1518
  let sawField: boolean = false
1345
1519
  // What this block has asked the table to copy, against its budget.
1346
- let copied: i64 = 0
1347
- const length: i32 = c.end - c.pos
1520
+ let copied: u64 = 0
1521
+ const length: i64 = toI64(c.end) - toI64(c.pos)
1522
+ const buf: u8[] = c.buf
1348
1523
  if (this.owedUpdate >= 0) {
1349
- if (c.pos >= c.end || hpackRepresentationOf(toI32(c.buf[c.pos])) !== HPACK_REP_SIZE_UPDATE) {
1524
+ const at: i32 = c.pos
1525
+ if (at >= c.end) {
1526
+ return HPACK_ERR_SIZE_UPDATE_MISSING
1527
+ }
1528
+ if (at < 0 || at >= toI32(buf.length)) {
1529
+ return HPACK_ERR_TRUNCATED
1530
+ }
1531
+ if (hpackRepresentationOf(toI32(buf[at])) !== HPACK_REP_SIZE_UPDATE) {
1350
1532
  return HPACK_ERR_SIZE_UPDATE_MISSING
1351
1533
  }
1352
1534
  }
1353
1535
  while (c.pos < c.end) {
1354
- const rep: i32 = hpackRepresentationOf(toI32(c.buf[c.pos]))
1536
+ const at: i32 = c.pos
1537
+ if (at < 0 || at >= toI32(buf.length)) {
1538
+ return HPACK_ERR_TRUNCATED
1539
+ }
1540
+ const rep: i32 = hpackRepresentationOf(toI32(buf[at]))
1355
1541
  switch (rep) {
1356
1542
  case HPACK_REP_SIZE_UPDATE: {
1357
1543
  if (sawField) {
@@ -1378,14 +1564,16 @@ export class HpackDecoder {
1378
1564
  }
1379
1565
  sawField = true
1380
1566
  const size: i64 = this.sizeAt(index)
1381
- listSize += size
1382
- tooLarge = tooLarge || listSize > toI64(this.maxHeaderListSize)
1567
+ listSize += toU64(size)
1568
+ tooLarge = tooLarge || listSize > toU64(this.maxHeaderListSize)
1383
1569
  if (!tooLarge) {
1384
1570
  // The entry's cost less the overhead is its name and value.
1385
1571
  const nameLength: i32 = this.nameLengthAt(index)
1386
1572
  const name: u8[] = this.working().take(nameLength)
1387
1573
  this.appendNameAt(index, name)
1388
- const value: u8[] = this.working().take(toI32(size) - HPACK_ENTRY_OVERHEAD - nameLength)
1574
+ // The size is the overhead, the name and the value, so the unsigned difference is the value's length.
1575
+ const valueLength: u32 = toU32(toI32(size)) - toU32(HPACK_ENTRY_OVERHEAD) - toU32(nameLength)
1576
+ const value: u8[] = this.working().take(toI32(valueLength))
1389
1577
  this.appendValueAt(index, value)
1390
1578
  this.names.push(name)
1391
1579
  this.values.push(value)
@@ -1420,17 +1608,19 @@ export class HpackDecoder {
1420
1608
  const nameLength: i32 = index === 0 ? toI32(name.length) : this.nameLengthAt(index)
1421
1609
  const size: i64 = toI64(nameLength) + toI64(value.length) + toI64(HPACK_ENTRY_OVERHEAD)
1422
1610
  sawField = true
1423
- listSize += size
1424
- tooLarge = tooLarge || listSize > toI64(this.maxHeaderListSize)
1611
+ listSize += toU64(size)
1612
+ tooLarge = tooLarge || listSize > toU64(this.maxHeaderListSize)
1425
1613
  const incremental: boolean = rep === HPACK_REP_INCREMENTAL
1426
1614
  const fits: boolean = size <= toI64(this.table.maxSize)
1427
1615
  // The table copies the field in, and an indexed name out first.
1428
1616
  const added: boolean = incremental && fits
1429
1617
  const nameOut: i64 = index !== 0 ? toI64(nameLength) : toI64(0)
1430
- const cost: i64 = added ? size - toI64(HPACK_ENTRY_OVERHEAD) + nameOut : toI64(0)
1618
+ // The size is at least the overhead, so the unsigned difference does not wrap.
1619
+ const cost: u64 = added ? toU64(size) - toU64(HPACK_ENTRY_OVERHEAD) + toU64(nameOut) : toU64(0)
1431
1620
  copied += cost
1432
- const scale: i32 = this.table.maxSize > length ? this.table.maxSize : length
1433
- if (copied > toI64(HPACK_COPY_FACTOR) * toI64(scale)) {
1621
+ const maxSize: i64 = toI64(this.table.maxSize)
1622
+ const scale: i64 = maxSize > length ? maxSize : length
1623
+ if (copied > toU64(HPACK_COPY_FACTOR) * toU64(scale)) {
1434
1624
  return HPACK_ERR_COPY_BUDGET
1435
1625
  }
1436
1626
  // An indexed name is copied out only when the table takes the field,
@@ -1455,7 +1645,7 @@ export class HpackDecoder {
1455
1645
  this.neverIndexed.push(rep === HPACK_REP_NEVER)
1456
1646
  }
1457
1647
  if (added) {
1458
- this.tableCopied += toI64(this.table.add(name, value))
1648
+ this.countCopied(this.table.add(name, value))
1459
1649
  } else if (incremental) {
1460
1650
  const nothing: i32 = 0
1461
1651
  this.table.evictTo(nothing)
@@ -1476,6 +1666,25 @@ export class HpackDecoder {
1476
1666
  }
1477
1667
  }
1478
1668
 
1669
+ /**
1670
+ * Empties `list`, giving each array in it back, emptied, to its class's pool
1671
+ * in `work`, for the next block. Each is emptied as it goes back, so a
1672
+ * reference a caller kept past the block reads nothing until the array is
1673
+ * taken again.
1674
+ */
1675
+ const hpackGiveBack = (work: HpackWork, list: u8[][]): void => {
1676
+ while (true) {
1677
+ const n: i32 = toI32(list.length)
1678
+ if (n <= 0 || toI32(list.length) <= 0) {
1679
+ return
1680
+ }
1681
+ const bytes: u8[] = list[n - 1]
1682
+ list.pop()
1683
+ work.pool(work.classOf(toI32(bytes.length))).push(bytes)
1684
+ hpackEmpty(bytes)
1685
+ }
1686
+ }
1687
+
1479
1688
  /**
1480
1689
  * One HTTP/2 connection's sending side: its dynamic table and the size
1481
1690
  * updates it owes the peer.
@@ -1526,19 +1735,22 @@ export class HpackEncoder {
1526
1735
  * from the newest entry.
1527
1736
  */
1528
1737
  private findField(name: u8[], value: u8[]): i32 {
1529
- for (let i: i32 = 1; i <= HPACK_STATIC_LENGTH; i += 1) {
1738
+ for (let k: u32 = 1; k <= toU32(HPACK_STATIC_LENGTH); k += 1) {
1739
+ const i: i32 = toI32(k)
1530
1740
  if (hpackSameAsText(name, hpackStaticName(i)) && hpackSameAsText(value, hpackStaticValue(i))) {
1531
1741
  return i
1532
1742
  }
1533
1743
  }
1534
- const n: i32 = this.table.count()
1535
- for (let i: i32 = 1; i <= n; i += 1) {
1744
+ // Unsigned counters: the dynamic table holds at most `maxSize / 32` entries, so neither they nor the index wrap.
1745
+ const n: u32 = toU32(this.table.count())
1746
+ for (let k: u32 = 1; k <= n; k += 1) {
1536
1747
  const t: HpackTable = this.table
1748
+ const i: i32 = toI32(k)
1537
1749
  if (
1538
1750
  hpackSameAsWindow(name, t.bytes, t.nameStart(i), t.nameLength(i)) &&
1539
1751
  hpackSameAsWindow(value, t.bytes, t.valueStart(i), t.valueLength(i))
1540
1752
  ) {
1541
- return HPACK_STATIC_LENGTH + i
1753
+ return toI32(toU32(HPACK_STATIC_LENGTH) + k)
1542
1754
  }
1543
1755
  }
1544
1756
  return 0
@@ -1546,15 +1758,18 @@ export class HpackEncoder {
1546
1758
 
1547
1759
  /** The lowest index whose entry has `name`, or 0 when neither table has one. */
1548
1760
  private findName(name: u8[]): i32 {
1549
- for (let i: i32 = 1; i <= HPACK_STATIC_LENGTH; i += 1) {
1761
+ for (let k: u32 = 1; k <= toU32(HPACK_STATIC_LENGTH); k += 1) {
1762
+ const i: i32 = toI32(k)
1550
1763
  if (hpackSameAsText(name, hpackStaticName(i))) {
1551
1764
  return i
1552
1765
  }
1553
1766
  }
1554
- const n: i32 = this.table.count()
1555
- for (let i: i32 = 1; i <= n; i += 1) {
1767
+ // Unsigned counters, as in `findField`.
1768
+ const n: u32 = toU32(this.table.count())
1769
+ for (let k: u32 = 1; k <= n; k += 1) {
1770
+ const i: i32 = toI32(k)
1556
1771
  if (hpackSameAsWindow(name, this.table.bytes, this.table.nameStart(i), this.table.nameLength(i))) {
1557
- return HPACK_STATIC_LENGTH + i
1772
+ return toI32(toU32(HPACK_STATIC_LENGTH) + k)
1558
1773
  }
1559
1774
  }
1560
1775
  return 0