@amritk/nish-aarch64-linux 0.42.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 (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  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 +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. 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
  }