nish 0.44.0 → 0.46.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.
Files changed (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
package/std/net/qpack.ts CHANGED
@@ -522,13 +522,15 @@ const qpackSameText = (buf: u8[], off: i32, len: i32, text: string): boolean =>
522
522
  if (len !== toI32(text.length)) {
523
523
  return false
524
524
  }
525
- for (let i: i32 = 0; i < len && i < toI32(text.length); i += 1) {
526
- const at: i32 = off + i
527
- if (at < 0 || at >= toI32(buf.length) || toI32(buf[at]) !== toI32(text.charCodeAt(i))) {
525
+ // `i` counts the characters compared and `at` walks the buffer beside it, so neither is a sum.
526
+ let i: i32 = 0
527
+ for (let at: i32 = off; at >= 0 && at < toI32(buf.length) && i < len && i < toI32(text.length); at += 1) {
528
+ if (toI32(buf[at]) !== toI32(text.charCodeAt(i))) {
528
529
  return false
529
530
  }
531
+ i += 1
530
532
  }
531
- return true
533
+ return i === len
532
534
  }
533
535
 
534
536
  /** Appends the bytes of `text`, which is ASCII. */
@@ -541,9 +543,10 @@ const qpackPushText = (out: u8[], text: string): void => {
541
543
 
542
544
  /** Appends `src[off, off + len)` to `out`. */
543
545
  const qpackPushBytes = (out: u8[], src: u8[], off: i32, len: i32): void => {
544
- const end: i32 = off + len
545
- for (let i: i32 = off; i >= 0 && i < end && i < toI32(src.length); i += 1) {
546
+ let left: i32 = len
547
+ for (let i: i32 = off; i >= 0 && left > 0 && i < toI32(src.length); i += 1) {
546
548
  out.push(src[i])
549
+ left -= 1
547
550
  }
548
551
  }
549
552
 
@@ -556,7 +559,7 @@ const qpackPushInteger = (out: u8[], flags: i32, prefixBits: i32, value: i64): v
556
559
  if (prefixBits < 1 || prefixBits > 8 || value < 0 || value > QPACK_MAX_INTEGER) {
557
560
  panic(`qpackPushInteger: a value of ${value} with a ${prefixBits}-bit prefix`)
558
561
  }
559
- const max: i32 = (1 << prefixBits) - 1
562
+ const max: i32 = (toI32(255) >> (toI32(8) - prefixBits)) & toI32(255)
560
563
  if (value < toI64(max)) {
561
564
  out.push(toU8(flags | toI32(value)))
562
565
  return
@@ -610,77 +613,107 @@ export const qpackPushStreamCancellation = (out: u8[], streamId: i64): void => {
610
613
  qpackPushInteger(out, 64, 6, streamId)
611
614
  }
612
615
 
613
- /** Whether the section being decoded has a byte left at `d.at`. */
614
- const qpackHasByte = (d: QpackDecoder, src: u8[]): boolean =>
615
- d.at >= 0 && d.at < d.end && d.at < toI32(src.length)
616
+ /** The byte at `d.at` of the section being decoded, or -1 when the section has none left there. */
617
+ const qpackPeekByte = (d: QpackDecoder, src: u8[]): i32 => {
618
+ const at: i32 = d.at
619
+ return at >= 0 && at < d.end && at < toI32(src.length) ? toI32(src[at]) : -1
620
+ }
621
+
622
+ /** Like `qpackPeekByte`, and moves `d.at` past the byte it answers. */
623
+ const qpackNextByte = (d: QpackDecoder, src: u8[]): i32 => {
624
+ const at: i32 = d.at
625
+ if (at < 0 || at >= d.end || at >= toI32(src.length)) {
626
+ return -1
627
+ }
628
+ d.at = at + 1
629
+ return toI32(src[at])
630
+ }
616
631
 
617
632
  /** The `QPACK_REASON_*` for a negative `qpackReadInteger` answer. */
618
633
  const qpackIntegerReason = (status: i64): i32 =>
619
634
  status === QPACK_INTEGER_TRUNCATED ? QPACK_REASON_TRUNCATED : QPACK_REASON_INTEGER
620
635
 
621
636
  /**
622
- * Reads a §4.1.1 integer whose prefix is the low `prefixBits` bits of the
623
- * byte at `d.at`, and answers it, or `QPACK_INTEGER_TRUNCATED` or
624
- * `QPACK_INTEGER_OVERFLOW`. Nine continuation bytes carry 63 bits, so a
625
- * tenth is refused even when its bits are zero.
637
+ * Reads a §4.1.1 integer whose prefix is the low bits of the byte at `d.at`
638
+ * that `max` (2^N - 1 for an N-bit prefix) covers, and answers it, or
639
+ * `QPACK_INTEGER_TRUNCATED` or `QPACK_INTEGER_OVERFLOW`. Nine continuation
640
+ * bytes carry 63 bits, so a tenth is refused even when its bits are zero.
626
641
  */
627
- const qpackReadInteger = (d: QpackDecoder, src: u8[], prefixBits: i32): i64 => {
628
- if (!qpackHasByte(d, src)) {
642
+ const qpackReadInteger = (d: QpackDecoder, src: u8[], max: i32): i64 => {
643
+ const head: i32 = qpackNextByte(d, src)
644
+ if (head < 0) {
629
645
  return QPACK_INTEGER_TRUNCATED
630
646
  }
631
- const max: i32 = (1 << prefixBits) - 1
632
- const first: i32 = toI32(src[d.at]) & max
633
- d.at += 1
647
+ const first: i32 = head & max
634
648
  if (first < max) {
635
649
  return toI64(first)
636
650
  }
637
- let value: i64 = toI64(max)
651
+ // The continuation bytes' 7-bit groups never overlap, so they are gathered with `|`, and the
652
+ // prefix's all-ones value, at most 255, is added once at the end; `spare` is what the integer
653
+ // may still grow by before it passes `QPACK_MAX_INTEGER`.
654
+ let rest: i64 = 0
638
655
  for (let shift: i32 = 0; shift <= 56; shift += 7) {
639
- if (!qpackHasByte(d, src)) {
656
+ const b: i32 = qpackNextByte(d, src)
657
+ if (b < 0) {
640
658
  return QPACK_INTEGER_TRUNCATED
641
659
  }
642
- const b: i32 = toI32(src[d.at])
643
- d.at += 1
644
- const digit: i64 = toI64(b & 127)
645
- if (digit > (QPACK_MAX_INTEGER - value) >> toI64(shift)) {
660
+ rest = rest | (toI64(b & 127) << toI64(shift))
661
+ // Never taken: the ninth group, at shift 56, ends at bit 62, so `rest` stays positive, and
662
+ // `spare` below refuses whatever passes the limit. The test tells the prover the sign.
663
+ if (rest < 0) {
664
+ return QPACK_INTEGER_OVERFLOW
665
+ }
666
+ const spare: i64 = QPACK_MAX_INTEGER - toI64(first) - rest
667
+ if (spare < 0) {
646
668
  return QPACK_INTEGER_OVERFLOW
647
669
  }
648
- value += digit << toI64(shift)
649
670
  if ((b & 128) === 0) {
650
- return value
671
+ return QPACK_MAX_INTEGER - spare
651
672
  }
652
673
  }
653
674
  return QPACK_INTEGER_OVERFLOW
654
675
  }
655
676
 
656
677
  /**
657
- * How many symbols the Huffman code in `src[off, off + len)` spells, found by
658
- * walking `h`'s canonical tables the way `hpackHuffmanDecode` does but writing
659
- * nothing. An EOS inside the string is counted as a symbol, so the answer is
660
- * never below what the decode will write; whether the string is well formed
661
- * is the decode's to say.
678
+ * Whether the Huffman code in `src[off, off + len)` spells at most `room`
679
+ * symbols, found by walking `h`'s canonical tables the way `hpackHuffmanDecode`
680
+ * does but writing nothing, and stopping at the first symbol past `room`. An
681
+ * EOS inside the string is counted as a symbol, so a string that fits never
682
+ * decodes to more than `room` octets; whether it is well formed is the
683
+ * decode's to say.
662
684
  */
663
- const qpackHuffmanSymbols = (h: HpackHuffman, src: u8[], off: i32, len: i32): i64 => {
685
+ const qpackHuffmanFits = (h: HpackHuffman, src: u8[], off: i32, len: i32, room: i64): boolean => {
664
686
  let code: i32 = 0
665
687
  let bits: i32 = 0
666
- let count: i64 = 0
667
- const end: i32 = off + len
668
- for (let i: i32 = off; i >= 0 && i < end && i < toI32(src.length); i += 1) {
688
+ let left: i64 = room
689
+ const firstCode: i32[] = h.firstCode
690
+ const countAt: i32[] = h.countAt
691
+ let bytes: i32 = len
692
+ for (let i: i32 = off; i >= 0 && bytes > 0 && i < toI32(src.length); i += 1) {
693
+ bytes -= 1
669
694
  const byte: i32 = toI32(src[i])
670
695
  for (let k: i32 = 7; k >= 0; k -= 1) {
696
+ // A code longer than the tables' longest matches nothing, however many bits follow, so the
697
+ // symbols counted so far are all the string has.
698
+ if (bits < 0 || bits >= toI32(firstCode.length) - 1) {
699
+ return true
700
+ }
671
701
  code = (code << 1) | ((byte >> k) & 1)
672
702
  bits += 1
673
- if (bits >= 5 && bits < toI32(h.firstCode.length)) {
674
- const at: i32 = code - h.firstCode[bits]
675
- if (at >= 0 && at < h.countAt[bits]) {
676
- count += toI64(1)
703
+ if (bits >= 5 && bits < toI32(firstCode.length) && bits < toI32(countAt.length)) {
704
+ const at: i64 = toI64(code) - toI64(firstCode[bits])
705
+ if (at >= 0 && at < toI64(countAt[bits])) {
706
+ if (left <= 0) {
707
+ return false
708
+ }
709
+ left -= toI64(1)
677
710
  code = 0
678
711
  bits = 0
679
712
  }
680
713
  }
681
714
  }
682
715
  }
683
- return count
716
+ return true
684
717
  }
685
718
 
686
719
  /**
@@ -689,33 +722,35 @@ const qpackHuffmanSymbols = (h: HpackHuffman, src: u8[], off: i32, len: i32): i6
689
722
  * it read one, or `QPACK_STRING_PAST_LIMIT`. The length is checked against
690
723
  * what the section has left, and the octets against `d.room`, before anything
691
724
  * is appended: a Huffman string decodes to at most 8/5 of its length, and only
692
- * one that could pass the room is counted first, so `bytes` never grows past
725
+ * one that could pass the room is walked first, so `bytes` never grows past
693
726
  * the field-section limit however a peer spells its strings.
694
727
  */
695
- const qpackReadString = (d: QpackDecoder, src: u8[], prefixBits: i32): i32 => {
696
- if (!qpackHasByte(d, src)) {
728
+ const qpackReadString = (d: QpackDecoder, src: u8[], huffmanBit: i32, lengthMax: i32): i32 => {
729
+ const head: i32 = qpackPeekByte(d, src)
730
+ if (head < 0) {
697
731
  return QPACK_REASON_TRUNCATED
698
732
  }
699
- const lengthBits: i32 = prefixBits - 1
700
- const huffman: boolean = (toI32(src[d.at]) & (1 << lengthBits)) !== 0
701
- const n: i64 = qpackReadInteger(d, src, lengthBits)
733
+ const huffman: boolean = (head & huffmanBit) !== 0
734
+ const n: i64 = qpackReadInteger(d, src, lengthMax)
702
735
  if (n < 0) {
703
736
  return qpackIntegerReason(n)
704
737
  }
705
- if (n > toI64(d.end - d.at)) {
738
+ const start: i32 = d.at
739
+ if (n > toI64(d.end) - toI64(start)) {
706
740
  return QPACK_REASON_TRUNCATED
707
741
  }
708
- const start: i32 = d.at
742
+ // At most `end - start`, so the `i32` holds it exactly.
709
743
  const length: i32 = toI32(n)
710
- d.at = start + length
711
- let octets: i64 = n
712
- if (huffman) {
713
- octets = (n * toI64(8)) / toI64(5)
714
- if (octets > d.room) {
715
- octets = qpackHuffmanSymbols(d.huffman, src, start, length)
716
- }
744
+ d.at = toI32(toI64(start) + toI64(length))
745
+ // `room` never passes the limit it starts at; one that did would be refused like a string past it.
746
+ const room: i64 = d.room
747
+ if (room < 0 || room > toI64(d.maxFieldSectionSize)) {
748
+ return QPACK_STRING_PAST_LIMIT
717
749
  }
718
- if (octets > d.room) {
750
+ // At most the `i32` limit, so the `i32` holds it exactly.
751
+ const left: i32 = toI32(room)
752
+ const most: i64 = huffman ? (toI64(length) * toI64(8)) / toI64(5) : toI64(length)
753
+ if (most > toI64(left) && (!huffman || !qpackHuffmanFits(d.huffman, src, start, length, toI64(left)))) {
719
754
  return QPACK_STRING_PAST_LIMIT
720
755
  }
721
756
  const before: i32 = toI32(d.bytes.length)
@@ -726,7 +761,7 @@ const qpackReadString = (d: QpackDecoder, src: u8[], prefixBits: i32): i32 => {
726
761
  } else {
727
762
  qpackPushBytes(d.bytes, src, start, length)
728
763
  }
729
- d.room -= toI64(d.bytes.length) - toI64(before)
764
+ d.room = toI64(left) - (toI64(d.bytes.length) - toI64(before))
730
765
  return QPACK_REASON_NONE
731
766
  }
732
767
 
@@ -781,16 +816,17 @@ export class QpackDecoder {
781
816
  * What the section being decoded may still add to `bytes` and its fields'
782
817
  * overhead before it is past `maxFieldSectionSize`: every append is checked
783
818
  * against it first, so a section past the limit never grows `bytes`.
784
- * @internal Read and lowered by this module's `qpackReadString`; not for callers.
819
+ * @internal Read and lowered by this module's `qpackReadString`, which refuses a string once it
820
+ * is negative or past `maxFieldSectionSize`; not for callers.
785
821
  */
786
822
  room: i64
787
823
  /**
788
824
  * @internal Where the next read of the section being decoded starts; read and advanced by this
789
- * module's `qpackHasByte`, `qpackReadInteger` and `qpackReadString`.
825
+ * module's `qpackPeekByte`, `qpackNextByte` and `qpackReadString`.
790
826
  */
791
827
  at: i32
792
828
  /**
793
- * @internal Where the section being decoded ends; read by this module's `qpackHasByte` and
829
+ * @internal Where the section being decoded ends; read by this module's `qpackPeekByte`, `qpackNextByte` and
794
830
  * `qpackReadString`.
795
831
  */
796
832
  end: i32
@@ -842,7 +878,8 @@ export class QpackDecoder {
842
878
  return this.failed
843
879
  }
844
880
  this.at = off
845
- this.end = off + len
881
+ // The window was checked against `section`, so its end is at most `section.length`.
882
+ this.end = toI32(toI64(off) + toI64(len))
846
883
  const result: i64 = this.decodeLines(section)
847
884
  if (result !== QPACK_OK) {
848
885
  this.clear()
@@ -872,12 +909,14 @@ export class QpackDecoder {
872
909
  if (this.failed !== QPACK_OK) {
873
910
  return this.failed
874
911
  }
875
- const end: i32 = off + len
876
- for (let i: i32 = off; i >= 0 && i < end && i < toI32(buf.length); i += 1) {
912
+ let left: i32 = len
913
+ for (let i: i32 = off; i >= 0 && left > 0 && i < toI32(buf.length); i += 1) {
914
+ left -= 1
877
915
  const b: i32 = toI32(buf[i])
878
916
  if (b === 32) {
879
- if (this.capacityInstructions < QPACK_COUNT_MAX) {
880
- this.capacityInstructions += 1
917
+ const seen: i32 = this.capacityInstructions
918
+ if (seen < QPACK_COUNT_MAX) {
919
+ this.capacityInstructions = seen + 1
881
920
  }
882
921
  } else {
883
922
  if ((b & 192) !== 0) {
@@ -922,10 +961,11 @@ export class QpackDecoder {
922
961
  * answers false and takes nothing when the section would pass its limit.
923
962
  */
924
963
  private take(cost: i64): boolean {
925
- if (cost > this.room) {
964
+ const room: i64 = this.room
965
+ if (cost < 0 || room < 0 || cost > room) {
926
966
  return false
927
967
  }
928
- this.room -= cost
968
+ this.room = room - cost
929
969
  return true
930
970
  }
931
971
 
@@ -950,12 +990,13 @@ export class QpackDecoder {
950
990
 
951
991
  /** Records a field whose name and value end where `bytes` ends now. */
952
992
  private addField(nameStart: i32, valueStart: i32, neverIndexed: boolean): void {
993
+ // Both starts and the end lie in `bytes`, from 0 to its length, so each difference fits an `i32`.
953
994
  this.nameStart.push(nameStart)
954
- this.nameLength.push(valueStart - nameStart)
995
+ this.nameLength.push(toI32(toI64(valueStart) - toI64(nameStart)))
955
996
  this.valueStart.push(valueStart)
956
- this.valueLength.push(toI32(this.bytes.length) - valueStart)
997
+ this.valueLength.push(toI32(toI64(this.bytes.length) - toI64(valueStart)))
957
998
  this.neverIndexed.push(neverIndexed)
958
- this.count += 1
999
+ this.count = toI32(this.nameStart.length)
959
1000
  }
960
1001
 
961
1002
  /**
@@ -963,21 +1004,22 @@ export class QpackDecoder {
963
1004
  * each told apart by its first byte's pattern (§4.5.2 to §4.5.6).
964
1005
  */
965
1006
  private decodeLines(src: u8[]): i64 {
966
- const insertCount: i64 = qpackReadInteger(this, src, 8)
1007
+ const insertCount: i64 = qpackReadInteger(this, src, 255)
967
1008
  if (insertCount < 0) {
968
1009
  return this.refuseInteger(insertCount)
969
1010
  }
970
1011
  if (insertCount !== toI64(0)) {
971
1012
  return this.refuse(QPACK_REASON_INSERT_COUNT)
972
1013
  }
973
- if (!qpackHasByte(this, src)) {
1014
+ const sign: i32 = qpackPeekByte(this, src)
1015
+ if (sign < 0) {
974
1016
  return this.refuse(QPACK_REASON_TRUNCATED)
975
1017
  }
976
1018
  // With a Required Insert Count of 0, a Sign bit of 1 makes the Base
977
1019
  // 0 - Delta Base - 1, which is negative whatever Delta Base is. A Base of
978
1020
  // 0 or more is accepted at any value, since nothing may use it (§4.5.1.2).
979
- const negative: boolean = (toI32(src[this.at]) & 128) !== 0
980
- const deltaBase: i64 = qpackReadInteger(this, src, 7)
1021
+ const negative: boolean = (sign & 128) !== 0
1022
+ const deltaBase: i64 = qpackReadInteger(this, src, 127)
981
1023
  if (deltaBase < 0) {
982
1024
  return this.refuseInteger(deltaBase)
983
1025
  }
@@ -985,8 +1027,7 @@ export class QpackDecoder {
985
1027
  return this.refuse(QPACK_REASON_BASE)
986
1028
  }
987
1029
  this.room = toI64(this.maxFieldSectionSize)
988
- while (qpackHasByte(this, src)) {
989
- const b: i32 = toI32(src[this.at])
1030
+ for (let b: i32 = qpackPeekByte(this, src); b >= 0; b = qpackPeekByte(this, src)) {
990
1031
  let neverIndexed: boolean = false
991
1032
  const nameStart: i32 = toI32(this.bytes.length)
992
1033
  // Where the value starts once it is in `bytes`; -1 while it is still to
@@ -997,7 +1038,7 @@ export class QpackDecoder {
997
1038
  if ((b & 64) === 0) {
998
1039
  return this.refuse(QPACK_REASON_DYNAMIC)
999
1040
  }
1000
- const index: i64 = qpackReadInteger(this, src, 6)
1041
+ const index: i64 = qpackReadInteger(this, src, 63)
1001
1042
  if (index < 0) {
1002
1043
  return this.refuseInteger(index)
1003
1044
  }
@@ -1018,7 +1059,7 @@ export class QpackDecoder {
1018
1059
  return this.refuse(QPACK_REASON_DYNAMIC)
1019
1060
  }
1020
1061
  neverIndexed = (b & 32) !== 0
1021
- const index: i64 = qpackReadInteger(this, src, 4)
1062
+ const index: i64 = qpackReadInteger(this, src, 15)
1022
1063
  if (index < 0) {
1023
1064
  return this.refuseInteger(index)
1024
1065
  }
@@ -1036,7 +1077,7 @@ export class QpackDecoder {
1036
1077
  if (!this.take(toI64(QPACK_FIELD_OVERHEAD))) {
1037
1078
  return QPACK_SECTION_TOO_LARGE
1038
1079
  }
1039
- const name: i64 = this.stringResult(qpackReadString(this, src, 4))
1080
+ const name: i64 = this.stringResult(qpackReadString(this, src, 8, 7))
1040
1081
  if (name !== QPACK_OK) {
1041
1082
  return name
1042
1083
  }
@@ -1048,7 +1089,7 @@ export class QpackDecoder {
1048
1089
  }
1049
1090
  if (valueStart < 0) {
1050
1091
  valueStart = toI32(this.bytes.length)
1051
- const value: i64 = this.stringResult(qpackReadString(this, src, 8))
1092
+ const value: i64 = this.stringResult(qpackReadString(this, src, 128, 127))
1052
1093
  if (value !== QPACK_OK) {
1053
1094
  return value
1054
1095
  }
@@ -1198,8 +1239,9 @@ export class QpackEncoder {
1198
1239
  if (this.failed !== QPACK_OK) {
1199
1240
  return this.failed
1200
1241
  }
1201
- const end: i32 = off + len
1202
- for (let i: i32 = off; i >= 0 && i < end && i < toI32(buf.length); i += 1) {
1242
+ let left: i32 = len
1243
+ for (let i: i32 = off; i >= 0 && left > 0 && i < toI32(buf.length); i += 1) {
1244
+ left -= 1
1203
1245
  const b: i32 = toI32(buf[i])
1204
1246
  if (this.pendingShift < 0) {
1205
1247
  if ((b & 128) !== 0) {
@@ -1215,19 +1257,27 @@ export class QpackEncoder {
1215
1257
  this.pendingShift = 0
1216
1258
  }
1217
1259
  } else {
1218
- const digit: i64 = toI64(b & 127)
1219
- if (
1220
- this.pendingShift > 56 ||
1221
- digit > (QPACK_MAX_INTEGER - this.pendingId) >> toI64(this.pendingShift)
1222
- ) {
1260
+ const shift: i32 = this.pendingShift
1261
+ const id: i64 = this.pendingId
1262
+ // `spare` is how far the ID may still grow before it passes `QPACK_MAX_INTEGER`; the
1263
+ // ID read so far never does, so a negative one is refused, and so is a tenth group.
1264
+ const spare: i64 = id < 0 ? -1 : QPACK_MAX_INTEGER - id
1265
+ if (shift > 56 || spare < 0) {
1266
+ return this.refuse(QPACK_REASON_INTEGER)
1267
+ }
1268
+ // At most shift 56, a group ends at bit 62, so `grown` is never negative; the test
1269
+ // tells the prover the sign.
1270
+ const grown: i64 = toI64(b & 127) << toI64(shift)
1271
+ const room: i64 = grown < 0 ? -1 : spare - grown
1272
+ if (room < 0) {
1223
1273
  return this.refuse(QPACK_REASON_INTEGER)
1224
1274
  }
1225
- this.pendingId += digit << toI64(this.pendingShift)
1275
+ this.pendingId = QPACK_MAX_INTEGER - room
1226
1276
  if ((b & 128) === 0) {
1227
1277
  this.cancelled(this.pendingId)
1228
1278
  this.pendingShift = -1
1229
1279
  } else {
1230
- this.pendingShift += 7
1280
+ this.pendingShift = shift + 7
1231
1281
  }
1232
1282
  }
1233
1283
  }
@@ -1236,8 +1286,9 @@ export class QpackEncoder {
1236
1286
 
1237
1287
  /** Counts a Stream Cancellation for `streamId`. */
1238
1288
  private cancelled(streamId: i64): void {
1239
- if (this.cancellations < QPACK_COUNT_MAX) {
1240
- this.cancellations += 1
1289
+ const seen: i32 = this.cancellations
1290
+ if (seen < QPACK_COUNT_MAX) {
1291
+ this.cancellations = seen + 1
1241
1292
  }
1242
1293
  this.lastCancelled = streamId
1243
1294
  }
@@ -41,6 +41,14 @@ import { quicPushAck, quicPushAckEcn, quicPutAck, quicPutAckEcn } from "nish/net
41
41
  /** The most disjoint ranges a `QuicAckRanges` keeps, and so the most an ACK frame it writes lists. */
42
42
  export const QUIC_ACK_MAX_RANGES: i32 = 32
43
43
 
44
+ /**
45
+ * One more than the ECN count `n`, or `n` once it is the largest a varint
46
+ * carries, since an ACK_ECN frame could not write a larger one.
47
+ */
48
+ const quicAckCountUp = (n: i64): i64 =>
49
+ // Below `QUIC_MAX_VARINT`, so the unsigned sum cannot wrap.
50
+ n < QUIC_MAX_VARINT ? toI64(toU64(n) + 1) : n
51
+
44
52
  /**
45
53
  * The packet numbers received in one space. `ranges` holds `count` pairs
46
54
  * `[smallest, largest]`, highest first and never touching; `largest` is the
@@ -68,7 +76,8 @@ export class QuicAckRanges {
68
76
  * `QUIC_ACK_MAX_RANGES` ranges.
69
77
  */
70
78
  constructor() {
71
- this.ranges = new Array<i64>(QUIC_ACK_MAX_RANGES * 2)
79
+ // 64 slots, which a u16 length holds.
80
+ this.ranges = new Array<i64>(toU16(QUIC_ACK_MAX_RANGES * 2))
72
81
  }
73
82
 
74
83
  /**
@@ -78,8 +87,13 @@ export class QuicAckRanges {
78
87
  * @returns The range's smallest packet number, or -1 for an index outside the storage.
79
88
  */
80
89
  low(k: i32): i64 {
90
+ // Below half the storage, `k * 2` cannot wrap.
91
+ const half: i32 = toI32(this.ranges.length) >>> 1
92
+ if (k < 0 || k >= half) {
93
+ return -1
94
+ }
81
95
  const at: i32 = k * 2
82
- if (at < 0 || at >= toI32(this.ranges.length)) {
96
+ if (at >= toI32(this.ranges.length)) {
83
97
  return -1
84
98
  }
85
99
  return this.ranges[at]
@@ -92,8 +106,13 @@ export class QuicAckRanges {
92
106
  * @returns The range's largest packet number, or -1 for an index outside the storage.
93
107
  */
94
108
  high(k: i32): i64 {
109
+ // Below half the storage, `k * 2 + 1` cannot wrap.
110
+ const half: i32 = toI32(this.ranges.length) >>> 1
111
+ if (k < 0 || k >= half) {
112
+ return -1
113
+ }
95
114
  const at: i32 = k * 2 + 1
96
- if (at < 0 || at >= toI32(this.ranges.length)) {
115
+ if (at >= toI32(this.ranges.length)) {
97
116
  return -1
98
117
  }
99
118
  return this.ranges[at]
@@ -108,9 +127,14 @@ export class QuicAckRanges {
108
127
  * @param high The range's largest packet number, at least `low`.
109
128
  */
110
129
  set(k: i32, low: i64, high: i64): void {
130
+ // Below half the storage, `k * 2 + 1` cannot wrap.
131
+ const half: i32 = toI32(this.ranges.length) >>> 1
132
+ if (k < 0 || k >= half) {
133
+ return
134
+ }
111
135
  const at: i32 = k * 2
112
136
  const next: i32 = at + 1
113
- if (at < 0 || at >= toI32(this.ranges.length) || next < 0 || next >= toI32(this.ranges.length)) {
137
+ if (at >= toI32(this.ranges.length) || next >= toI32(this.ranges.length)) {
114
138
  return
115
139
  }
116
140
  this.ranges[at] = low
@@ -164,7 +188,8 @@ export class QuicAckRanges {
164
188
  while (k < this.count && this.low(k) > pn) {
165
189
  k += 1
166
190
  }
167
- const joinsAbove: boolean = k > 0 && this.low(k - 1) === pn + 1
191
+ // `pn` is at most 2^62 - 1 here, so one past it cannot wrap.
192
+ const joinsAbove: boolean = k > 0 && this.low(k - 1) === toI64(toU64(pn) + 1)
168
193
  const joinsBelow: boolean = k < this.count && this.high(k) === pn - 1
169
194
  if (joinsAbove && joinsBelow) {
170
195
  // `pn` closes the hole between two ranges: merge them into the upper one.
@@ -194,11 +219,11 @@ export class QuicAckRanges {
194
219
  countEcn(codepoint: i32): void {
195
220
  const bits: i32 = codepoint & 3
196
221
  if (bits === 2) {
197
- this.ect0 = this.ect0 + 1
222
+ this.ect0 = quicAckCountUp(this.ect0)
198
223
  } else if (bits === 1) {
199
- this.ect1 = this.ect1 + 1
224
+ this.ect1 = quicAckCountUp(this.ect1)
200
225
  } else if (bits === 3) {
201
- this.ce = this.ce + 1
226
+ this.ce = quicAckCountUp(this.ce)
202
227
  }
203
228
  }
204
229
 
@@ -211,12 +236,16 @@ export class QuicAckRanges {
211
236
  return this.ect0 > 0 || this.ect1 > 0 || this.ce > 0
212
237
  }
213
238
 
214
- /** Removes range `k`, moving the lower ones up. */
239
+ /** Removes range `k`, moving the lower ones up; `k` is a held range, below `count`. */
215
240
  private remove(k: i32): void {
216
- for (let j: i32 = k; j + 1 < this.count; j += 1) {
217
- this.set(j, this.low(j + 1), this.high(j + 1))
241
+ const count: i32 = this.count
242
+ if (count <= 0 || k < 0 || k >= count) {
243
+ return
244
+ }
245
+ for (let j: i32 = k + 1; j < count; j += 1) {
246
+ this.set(j - 1, this.low(j), this.high(j))
218
247
  }
219
- this.count = this.count - 1
248
+ this.count = count - 1
220
249
  }
221
250
 
222
251
  /**
@@ -225,19 +254,22 @@ export class QuicAckRanges {
225
254
  * if `pn` itself would be that lowest range, it is the one dropped.
226
255
  */
227
256
  private insert(k: i32, pn: i64): void {
228
- if (this.count === QUIC_ACK_MAX_RANGES) {
229
- if (k === this.count) {
257
+ let count: i32 = this.count
258
+ if (count === QUIC_ACK_MAX_RANGES) {
259
+ if (k === count) {
230
260
  this.floor = pn
231
261
  return
232
262
  }
233
- this.floor = this.high(this.count - 1)
234
- this.count = this.count - 1
263
+ this.floor = this.high(QUIC_ACK_MAX_RANGES - 1)
264
+ count = QUIC_ACK_MAX_RANGES - 1
235
265
  }
236
- for (let j: i32 = this.count; j > k; j -= 1) {
266
+ // `k` is 0 or more, so `j > 0` adds nothing but keeps `j - 1` from wrapping.
267
+ for (let j: i32 = count; j > 0 && j > k; j -= 1) {
237
268
  this.set(j, this.low(j - 1), this.high(j - 1))
238
269
  }
239
270
  this.set(k, pn, pn)
240
- this.count = this.count + 1
271
+ // `count` is below `QUIC_ACK_MAX_RANGES` here, so one more cannot wrap.
272
+ this.count = toI32(toU32(count) + 1)
241
273
  }
242
274
 
243
275
  /**