@amritk/nish-x86_64-linux 0.49.1 → 0.51.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 (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
package/std/net/url.ts CHANGED
@@ -117,14 +117,31 @@ const URL_SLASH: i32 = 47
117
117
  const URL_EQUALS: i32 = 61
118
118
  const URL_QUESTION: i32 = 63
119
119
 
120
+ /**
121
+ * `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
122
+ * step subtracts a non-negative value from one that is no larger than
123
+ * `bytes.length`, so the bounds prover sees no sum that can overflow.
124
+ */
125
+ const urlWindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
126
+ if (off < 0 || len < 0) {
127
+ return -1
128
+ }
129
+ const size: i32 = toI32(bytes.length)
130
+ const room: i32 = size - off
131
+ if (room < 0) {
132
+ return -1
133
+ }
134
+ const gap: i32 = room - len
135
+ return gap < 0 ? -1 : size - gap
136
+ }
137
+
120
138
  /** Panics unless `bytes[off, off + len)` lies inside `bytes`, and answers the window's end, `off + len`. */
121
139
  const urlCheckWindow = (bytes: u8[], off: i32, len: i32, who: string): i32 => {
122
- const size: i32 = toI32(bytes.length)
123
- if (off < 0 || len < 0 || off > size || len > size - off) {
140
+ const end: i32 = urlWindowEnd(bytes, off, len)
141
+ if (end < 0) {
124
142
  panic(who + ": the window is outside the array")
125
143
  }
126
- // The test above bounds the sum by `bytes.length`, so it cannot wrap.
127
- return toI32(toU32(off) + toU32(len))
144
+ return end
128
145
  }
129
146
 
130
147
  /** The value of one hex digit, either case, or -1. */
@@ -186,14 +203,13 @@ const urlDecodeRun = (
186
203
  ): i32 => {
187
204
  const n: i32 = toI32(src.length)
188
205
  const size: i32 = toI32(out.length)
189
- if (off < 0 || len < 0 || off > n || len > n - off) {
206
+ const end: i32 = urlWindowEnd(src, off, len)
207
+ if (end < 0) {
190
208
  return URL_DECODE_ERR_WINDOW
191
209
  }
192
210
  if (at < 0 || at > size) {
193
211
  return URL_DECODE_AT_OUTSIDE
194
212
  }
195
- // The window test bounds the sum by `src.length`, so it cannot wrap.
196
- const end: i32 = toI32(toU32(off) + toU32(len))
197
213
  const form: boolean = (flags & URL_DECODE_FORM) !== 0
198
214
  const lenient: boolean = (flags & URL_DECODE_LENIENT) !== 0
199
215
  const allowNul: boolean = (flags & URL_DECODE_ALLOW_NUL) !== 0
@@ -201,7 +217,7 @@ const urlDecodeRun = (
201
217
  let w: i32 = at
202
218
  while (i >= 0 && i < end && i < n) {
203
219
  let b: i32 = toI32(src[i])
204
- let step: i32 = 1
220
+ let escaped: boolean = false
205
221
  if (b === URL_PERCENT) {
206
222
  const e: i32 = urlEscapeAt(src, i, end)
207
223
  if (e >= 0) {
@@ -209,7 +225,7 @@ const urlDecodeRun = (
209
225
  return wantPos ? i : URL_DECODE_ERR_NUL
210
226
  }
211
227
  b = e
212
- step = 3
228
+ escaped = true
213
229
  } else if (!lenient) {
214
230
  return wantPos ? i : URL_DECODE_ERR_ESCAPE
215
231
  }
@@ -221,12 +237,13 @@ const urlDecodeRun = (
221
237
  }
222
238
  out[w] = toU8(b)
223
239
  w += 1
224
- // An escape is taken only when its digits lie inside `src`, so the step
225
- // ends at most at `src.length` and cannot wrap.
226
- i = toI32(toU32(i) + toU32(step))
240
+ // An escape is taken only when its two digits lie inside `src`, so
241
+ // `i < n - 2` always holds for one; the test is what proves the step
242
+ // cannot overflow.
243
+ i = escaped && i < n - 2 ? i + 3 : i + 1
227
244
  }
228
245
  // `w` starts at `at` and only grows, so the difference cannot wrap.
229
- return wantPos ? -1 : toI32(toU32(w) - toU32(at))
246
+ return wantPos ? -1 : w - at
230
247
  }
231
248
 
232
249
  /** `urlQueryStart` of `bytes[off, end)`, for a caller that built the window from `bytes.length` and so has nothing to check. */
@@ -397,8 +414,8 @@ export class UrlPathSegments {
397
414
  j += 1
398
415
  }
399
416
  this.start = pos
400
- // `j` starts at `pos` and only grows, so the difference cannot wrap.
401
- this.length = toI32(toU32(j) - toU32(pos))
417
+ // `j` starts at `pos` and only grows, so the length is `j - pos` or 0.
418
+ this.length = j > pos && pos >= 0 ? j - pos : 0
402
419
  if (j < end) {
403
420
  this.pos = j + 1
404
421
  } else {
@@ -521,17 +538,18 @@ export class UrlQuery {
521
538
  i += 1
522
539
  }
523
540
  // `start <= eq < i` when `eq` was found, as the loop only moves `i` on,
524
- // so none of the differences below can wrap.
541
+ // so each length is the difference its test checks, never the 0.
525
542
  this.keyStart = start
526
543
  if (eq < 0) {
527
- this.keyLength = toI32(toU32(i) - toU32(start))
544
+ this.keyLength = i > start && start >= 0 ? i - start : 0
528
545
  this.valueStart = i
529
546
  this.valueLength = 0
530
547
  this.hasValue = false
531
548
  } else {
532
- this.keyLength = toI32(toU32(eq) - toU32(start))
533
- this.valueStart = toI32(toU32(eq) + 1)
534
- this.valueLength = toI32(toU32(i) - toU32(eq) - 1)
549
+ const valueStart: i32 = eq < i ? eq + 1 : i
550
+ this.keyLength = eq > start && start >= 0 ? eq - start : 0
551
+ this.valueStart = valueStart
552
+ this.valueLength = i > valueStart && valueStart >= 0 ? i - valueStart : 0
535
553
  this.hasValue = true
536
554
  }
537
555
  this.pos = i
@@ -550,22 +568,21 @@ export class UrlQuery {
550
568
  const urlKeyEquals = (bytes: u8[], off: i32, len: i32, name: string): boolean => {
551
569
  const n: i32 = toI32(name.length)
552
570
  const size: i32 = toI32(bytes.length)
553
- if (off < 0 || len < 0 || off > size || len > size - off) {
571
+ const end: i32 = urlWindowEnd(bytes, off, len)
572
+ if (end < 0) {
554
573
  return false
555
574
  }
556
- // `off + len` is at most `size`, checked above, so it cannot wrap.
557
- const end: i32 = toI32(toU32(off) + toU32(len))
558
575
  let i: i32 = off
559
576
  let o: i32 = 0
560
577
  while (i >= 0 && i < end && i < size) {
561
578
  let b: i32 = toI32(bytes[i])
562
- let step: i32 = 1
579
+ let escaped: boolean = false
563
580
  if (b === URL_PERCENT) {
564
581
  b = urlEscapeAt(bytes, i, end)
565
582
  if (b <= 0) {
566
583
  return false
567
584
  }
568
- step = 3
585
+ escaped = true
569
586
  } else if (b === URL_PLUS) {
570
587
  b = URL_SPACE
571
588
  }
@@ -573,9 +590,10 @@ const urlKeyEquals = (bytes: u8[], off: i32, len: i32, name: string): boolean =>
573
590
  return false
574
591
  }
575
592
  o += 1
576
- // An escape is taken only when its digits lie inside `bytes`, so the step
577
- // ends at most at `bytes.length` and cannot wrap.
578
- i = toI32(toU32(i) + toU32(step))
593
+ // An escape is taken only when its two digits lie inside `bytes`, so
594
+ // `i < size - 2` always holds for one; the test is what proves the step
595
+ // cannot overflow.
596
+ i = escaped && i < size - 2 ? i + 3 : i + 1
579
597
  }
580
598
  return o === n
581
599
  }
@@ -639,7 +657,7 @@ const urlIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
639
657
  } else {
640
658
  return false
641
659
  }
642
- if (end - i - 1 < need || i + 1 >= n) {
660
+ if (end - i - 1 < need || i >= n - 1) {
643
661
  return false
644
662
  }
645
663
  const second: i32 = toI32(buf[i + 1])
@@ -647,27 +665,31 @@ const urlIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
647
665
  return false
648
666
  }
649
667
  // The test on `end` above already keeps the sequence inside `buf`; each
650
- // byte's own guard is spelled out for the bounds proof.
651
- if (need >= 2) {
652
- if (i >= n - 2) {
653
- return false
654
- }
655
- const c: i32 = toI32(buf[i + 2])
656
- if (c < 0x80 || c > 0xbf) {
657
- return false
658
- }
668
+ // byte's own guard is spelled out for the bounds proof, and the step
669
+ // past the sequence is taken under the guard that proves it fits.
670
+ if (need === 1) {
671
+ i = i + 2
672
+ continue
659
673
  }
660
- if (need >= 3) {
661
- if (i >= n - 3) {
662
- return false
663
- }
664
- const c: i32 = toI32(buf[i + 3])
665
- if (c < 0x80 || c > 0xbf) {
666
- return false
667
- }
674
+ if (i >= n - 2) {
675
+ return false
676
+ }
677
+ const third: i32 = toI32(buf[i + 2])
678
+ if (third < 0x80 || third > 0xbf) {
679
+ return false
680
+ }
681
+ if (need === 2) {
682
+ i = i + 3
683
+ continue
684
+ }
685
+ if (i >= n - 3) {
686
+ return false
687
+ }
688
+ const fourth: i32 = toI32(buf[i + 3])
689
+ if (fourth < 0x80 || fourth > 0xbf) {
690
+ return false
668
691
  }
669
- // Every byte of the sequence was read inside `buf`, so the step cannot wrap.
670
- i = toI32(toU32(i) + toU32(need) + 1)
692
+ i = i + 4
671
693
  }
672
694
  return true
673
695
  }
@@ -690,13 +712,18 @@ const urlBytesOf = (s: string): u8[] | null => {
690
712
 
691
713
  /** `percentDecode` of `src[off, off + len)` as a new string, or null on an error or when the result is not UTF-8. */
692
714
  const urlDecodeString = (src: u8[], off: i32, len: i32, flags: i32): string | null => {
715
+ // Both callers, `urlDecode` and `urlQueryGet`, cut the window from
716
+ // `src.length`, so this never answers; it is what proves `len` an array
717
+ // length, and a window outside `src` is text nobody can decode.
718
+ if (off < 0 || len < 0 || len > toI32(src.length) - off) {
719
+ return null
720
+ }
693
721
  // `len` bytes of room; decoding never makes the text longer.
694
722
  const out: u8[] = new Array<u8>(len)
695
- // A window outside `src` is a caller contract, kept as `percentDecode`'s
696
- // panic rather than answered as null: both callers, `urlDecode` and
697
- // `urlQueryGet`, cut the window from `src.length`.
698
- const n: i32 = percentDecode(src, off, len, out, 0, flags)
699
- if (n < 0 || !urlIsUtf8(out, 0, n)) {
723
+ const n: i32 = tryPercentDecode(src, off, len, out, 0, flags)
724
+ // A count is at most `out.length`, which `tryPercentDecode` never writes
725
+ // past; the last test is spelled out for the proof.
726
+ if (n < 0 || !urlIsUtf8(out, 0, n) || n > toI32(out.length)) {
700
727
  return null
701
728
  }
702
729
  return String.fromBytes(out, 0, n)
@@ -762,8 +789,6 @@ const URL_AT: i32 = 64
762
789
  const URL_OPEN_BRACKET: i32 = 91
763
790
  const URL_CLOSE_BRACKET: i32 = 93
764
791
  const URL_BACKSLASH: i32 = 92
765
- /** The largest port a URL may name (RFC 6335 §6: a port is 16 bits). */
766
- const URL_MAX_PORT: i32 = 65535
767
792
 
768
793
  /**
769
794
  * A URL split into its parts (RFC 3986 §3). Each part is held as it was
@@ -792,25 +817,50 @@ export class Url {
792
817
  hasAuthority: boolean = false
793
818
  }
794
819
 
795
- /** `bytes[a, b)` as a string with A to Z lower-cased. */
796
- const urlLowerString = (bytes: u8[], a: i32, b: i32): string => {
797
- // `a <= b` is the caller's contract: a window the other way round is a
798
- // negative length, and `new Array` refuses it.
799
- const out: u8[] = new Array<u8>(toI32(toU32(b) - toU32(a)))
820
+ /**
821
+ * `bytes[a, b)` as a string with A to Z lower-cased, or null when the window
822
+ * starts below 0 or ends past `bytes`, which no caller's does: each cuts it
823
+ * from `bytes.length`. A window the other way round is empty.
824
+ */
825
+ const urlLowerString = (bytes: u8[], a: i32, b: i32): string | null => {
826
+ const n: i32 = toI32(bytes.length)
827
+ if (a < 0 || b > n) {
828
+ return null
829
+ }
830
+ if (b <= a) {
831
+ return ""
832
+ }
833
+ // `0 <= a < b <= n`, so the difference fits and is above 0; the prover
834
+ // reads that only from the clamp, which is never taken.
835
+ let width: i32 = b - a
836
+ if (width < 0) {
837
+ width = 0
838
+ }
839
+ const out: u8[] = new Array<u8>(width)
800
840
  let j: i32 = 0
801
- while (j < toI32(out.length)) {
802
- const i: i32 = toI32(toU32(a) + toU32(j))
803
- if (i < 0 || i >= toI32(bytes.length)) {
804
- break
805
- }
841
+ let i: i32 = a
842
+ while (j < toI32(out.length) && i >= 0 && i < n) {
806
843
  const c: i32 = toI32(bytes[i])
807
844
  // `c | 32` is `c + 32` for A to Z, whose bit 5 is clear.
808
845
  out[j] = c >= 65 && c <= 90 ? toU8(c | 32) : toU8(c)
809
846
  j += 1
847
+ i += 1
810
848
  }
811
849
  return String.fromBytes(out, 0, toI32(out.length))
812
850
  }
813
851
 
852
+ /**
853
+ * `bytes[a, b)` as a string, or null when the window is not inside `bytes`,
854
+ * which no caller's is: each cuts it with `urlFind`, which answers inside the
855
+ * window it was given. The test is what proves the read.
856
+ */
857
+ const urlSlice = (bytes: u8[], a: i32, b: i32): string | null => {
858
+ if (a < 0 || b < a || b > toI32(bytes.length)) {
859
+ return null
860
+ }
861
+ return String.fromBytes(bytes, a, b)
862
+ }
863
+
814
864
  /** The first `c` in `bytes[a, b)`, or `b`. */
815
865
  const urlFind = (bytes: u8[], a: i32, b: i32, c: i32): i32 => {
816
866
  let i: i32 = a
@@ -997,9 +1047,17 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
997
1047
  return false
998
1048
  }
999
1049
  const colon: i32 = urlFind(bytes, a, at, URL_COLON)
1000
- u.username = String.fromBytes(bytes, a, colon)
1050
+ const username: string | null = urlSlice(bytes, a, colon)
1051
+ if (username === null) {
1052
+ return false
1053
+ }
1054
+ u.username = username
1001
1055
  if (colon < at) {
1002
- u.password = String.fromBytes(bytes, colon + 1, at)
1056
+ const password: string | null = urlSlice(bytes, colon + 1, at)
1057
+ if (password === null) {
1058
+ return false
1059
+ }
1060
+ u.password = password
1003
1061
  }
1004
1062
  h = at + 1
1005
1063
  }
@@ -1009,11 +1067,16 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
1009
1067
  }
1010
1068
  // `h` lies inside the window, which lies inside `bytes`; the bounds are
1011
1069
  // spelled out for the proof.
1070
+ let host: string | null = null
1012
1071
  if (h >= 0 && h < b && h < toI32(bytes.length) && toI32(bytes[h]) === URL_OPEN_BRACKET) {
1013
- u.host = urlLowerString(bytes, h + 1, portAt - 1)
1072
+ host = urlLowerString(bytes, h + 1, portAt - 1)
1014
1073
  } else {
1015
- u.host = urlLowerString(bytes, h, portAt)
1074
+ host = urlLowerString(bytes, h, portAt)
1075
+ }
1076
+ if (host === null) {
1077
+ return false
1016
1078
  }
1079
+ u.host = host
1017
1080
  if (portAt < b && portAt + 1 < b) {
1018
1081
  const port: i32 = urlPortOf(bytes, portAt + 1, b)
1019
1082
  if (port < 0) {
@@ -1060,7 +1123,10 @@ const urlHostEnd = (bytes: u8[], h: i32, b: i32): i32 => {
1060
1123
 
1061
1124
  /** The port `bytes[a, b)` names, digits up to 65535, or -1 when it is not one. */
1062
1125
  const urlPortOf = (bytes: u8[], a: i32, b: i32): i32 => {
1063
- let port: i32 = 0
1126
+ // At most 65535, the largest port (RFC 6335 §6: a port is 16 bits), held
1127
+ // as a range the proof reads, so with a digit added the sum is at most
1128
+ // 655359.
1129
+ let port: integer<0, 65535> = 0
1064
1130
  let i: i32 = a
1065
1131
  while (i >= 0 && i < b && i < toI32(bytes.length)) {
1066
1132
  const c: i32 = toI32(bytes[i])
@@ -1068,12 +1134,11 @@ const urlPortOf = (bytes: u8[], a: i32, b: i32): i32 => {
1068
1134
  if (c < 48 || c > 57) {
1069
1135
  return -1
1070
1136
  }
1071
- // `port` is at most 65535 here and `c` a digit, so the sum is at most
1072
- // 655359 and cannot wrap.
1073
- port = toI32(toU32(port) * 10 + toU32(c) - 48)
1074
- if (port > URL_MAX_PORT) {
1137
+ const next: i32 = port * 10 + (c - 48)
1138
+ if (next > 65535) {
1075
1139
  return -1
1076
1140
  }
1141
+ port = next
1077
1142
  i += 1
1078
1143
  }
1079
1144
  return port
@@ -1134,15 +1199,23 @@ const urlReadReference = (s: string): Url | null => {
1134
1199
  i += 1
1135
1200
  }
1136
1201
  // `urlFind` answers an index at most `n`, so the tests against the length
1137
- // below always hold; they are spelled out for the proof.
1202
+ // below, and `urlSlice`'s, always hold; they are spelled out for the proof.
1138
1203
  const u: Url = new Url()
1139
1204
  const hash: i32 = urlFind(bytes, 0, n, URL_HASH)
1140
1205
  if (hash < n) {
1141
- u.fragment = String.fromBytes(bytes, hash + 1, n)
1206
+ const fragment: string | null = urlSlice(bytes, hash + 1, n)
1207
+ if (fragment === null) {
1208
+ return null
1209
+ }
1210
+ u.fragment = fragment
1142
1211
  }
1143
1212
  const question: i32 = urlFind(bytes, 0, hash, URL_QUESTION)
1144
1213
  if (question < hash) {
1145
- u.query = String.fromBytes(bytes, question + 1, hash)
1214
+ const query: string | null = urlSlice(bytes, question + 1, hash)
1215
+ if (query === null) {
1216
+ return null
1217
+ }
1218
+ u.query = query
1146
1219
  }
1147
1220
  let pos: i32 = 0
1148
1221
  if (question > 0 && toI32(bytes.length) > 0 && urlIsAlpha(toI32(bytes[0]))) {
@@ -1155,7 +1228,11 @@ const urlReadReference = (s: string): Url | null => {
1155
1228
  j += 1
1156
1229
  }
1157
1230
  if (j < question && j < toI32(bytes.length) && toI32(bytes[j]) === URL_COLON) {
1158
- u.scheme = urlLowerString(bytes, 0, j)
1231
+ const scheme: string | null = urlLowerString(bytes, 0, j)
1232
+ if (scheme === null) {
1233
+ return null
1234
+ }
1235
+ u.scheme = scheme
1159
1236
  pos = j + 1
1160
1237
  }
1161
1238
  }
@@ -1173,20 +1250,24 @@ const urlReadReference = (s: string): Url | null => {
1173
1250
  }
1174
1251
  pos = end
1175
1252
  }
1176
- u.path = String.fromBytes(bytes, pos, question)
1253
+ const path: string | null = urlSlice(bytes, pos, question)
1254
+ if (path === null) {
1255
+ return null
1256
+ }
1257
+ u.path = path
1177
1258
  return u
1178
1259
  }
1179
1260
 
1180
- /** The width of a `.` (1) or `%2e` (3) at `s[i]`, or 0. */
1261
+ /** The index just past a `.` or `%2e` at `s[i]`, or -1 when there is neither. */
1181
1262
  const urlDotAt = (s: string, i: i32, end: i32): i32 => {
1182
1263
  // Callers pass `0 <= i` and `end <= s.length`, so only `i >= end` can
1183
1264
  // answer here; the other bounds are spelled out for the proof.
1184
1265
  const n: i32 = toI32(s.length)
1185
1266
  if (i < 0 || i >= end || i >= n) {
1186
- return 0
1267
+ return -1
1187
1268
  }
1188
1269
  if (toI32(s.charCodeAt(i)) === URL_DOT) {
1189
- return 1
1270
+ return i + 1
1190
1271
  }
1191
1272
  if (
1192
1273
  i < n - 2 &&
@@ -1195,28 +1276,23 @@ const urlDotAt = (s: string, i: i32, end: i32): i32 => {
1195
1276
  toI32(s.charCodeAt(i + 1)) === 50 &&
1196
1277
  (toI32(s.charCodeAt(i + 2)) | 32) === 101
1197
1278
  ) {
1198
- return 3
1279
+ return i + 3
1199
1280
  }
1200
- return 0
1281
+ return -1
1201
1282
  }
1202
1283
 
1203
1284
  /** 1 when the segment `s[a, b)` is `.`, 2 when it is `..`, either written with `%2e`, else 0. */
1204
1285
  const urlDotSegment = (s: string, a: i32, b: i32): i32 => {
1205
- const first: i32 = urlDotAt(s, a, b)
1206
- if (first === 0) {
1286
+ // `urlDotAt` answers the index past the dot, which it steps to under the
1287
+ // bound that proves the step fits, so no sum is taken here.
1288
+ const next: i32 = urlDotAt(s, a, b)
1289
+ if (next < 0) {
1207
1290
  return 0
1208
1291
  }
1209
- // `urlDotAt` answers a width only for characters inside `s`, so each sum
1210
- // is at most `s.length` and cannot wrap.
1211
- const next: i32 = toI32(toU32(a) + toU32(first))
1212
1292
  if (next === b) {
1213
1293
  return 1
1214
1294
  }
1215
- const second: i32 = urlDotAt(s, next, b)
1216
- if (second > 0 && toI32(toU32(next) + toU32(second)) === b) {
1217
- return 2
1218
- }
1219
- return 0
1295
+ return urlDotAt(s, next, b) === b ? 2 : 0
1220
1296
  }
1221
1297
 
1222
1298
  /**
@@ -190,22 +190,14 @@ const websocketAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
190
190
  const websocketSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
191
191
 
192
192
  /**
193
- * A zeroed array of `n` bytes, or of none when `n` is below zero. Up to
194
- * 65535 bytes is one allocation; past that the array grows a byte at a time
195
- * from a 65535-byte start, which an `i32` length cannot make fail.
193
+ * A zeroed array of `n` bytes, or of none when `n` is below zero: one
194
+ * allocation, its length proven non-negative by the guard.
196
195
  */
197
196
  const websocketBytes = (n: i32): u8[] => {
198
197
  if (n < 0) {
199
198
  return []
200
199
  }
201
- if (n <= 65535) {
202
- return new Array<u8>(toU16(n))
203
- }
204
- const out: u8[] = new Array<u8>(65535)
205
- for (let k: i32 = 65535; k < n; k += 1) {
206
- out.push(0)
207
- }
208
- return out
200
+ return new Array<u8>(n)
209
201
  }
210
202
 
211
203
  /**
@@ -251,6 +243,18 @@ const websocketUtf8Run = (state: WsUtf8, buf: u8[], off: i32, len: i32): boolean
251
243
  return true
252
244
  }
253
245
 
246
+ /**
247
+ * Panics unless `buf[off .. off + len)` is inside `buf`: a window past its
248
+ * buffer is the caller's mistake. A window guard, so a caller that proves
249
+ * the window in range removes the panic site.
250
+ */
251
+ const websocketCheckWindow = (what: string, buf: readonly u8[], off: i32, len: i32): void => {
252
+ const size: i32 = toI32(buf.length)
253
+ if (off < 0 || len < 0 || off > size || len > size - off) {
254
+ panic(`${what}: the window is outside the buffer`)
255
+ }
256
+ }
257
+
254
258
  /**
255
259
  * Whether `buf[off .. off + len)` is well-formed UTF-8, no character cut short
256
260
  * at the end. The window is the caller's, already checked against `buf`, as
@@ -272,10 +276,7 @@ const websocketUtf8Whole = (buf: u8[], off: i32, len: i32): boolean => {
272
276
  * @returns True when the bytes are UTF-8 with no overlong form, surrogate or code point past U+10FFFF, and no character cut short at the end.
273
277
  */
274
278
  export const websocketIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
275
- const size: i32 = toI32(buf.length)
276
- if (off < 0 || len < 0 || off > size || len > size - off) {
277
- panic("websocketIsUtf8: the window is outside the buffer")
278
- }
279
+ websocketCheckWindow("websocketIsUtf8", buf, off, len)
279
280
  return websocketUtf8Whole(buf, off, len)
280
281
  }
281
282
 
@@ -360,10 +361,7 @@ export class WsDecoder {
360
361
  * @param len How many bytes to append.
361
362
  */
362
363
  feed(data: u8[], off: i32, len: i32): void {
363
- const size: i32 = toI32(data.length)
364
- if (off < 0 || len < 0 || off > size || len > size - off) {
365
- panic("WsDecoder: the window is outside the buffer")
366
- }
364
+ websocketCheckWindow("WsDecoder", data, off, len)
367
365
  if (this.state !== WS_S_FRAMES) {
368
366
  return
369
367
  }
@@ -421,7 +419,8 @@ const websocketReserve = (d: WsDecoder, len: i32): boolean => {
421
419
  return true
422
420
  }
423
421
  const live: i32 = end - start
424
- if (toI64(len) > toI64(2147483647) - toI64(live)) {
422
+ const most: i32 = 2147483647
423
+ if (toI64(len) > toI64(most) - toI64(live)) {
425
424
  return false
426
425
  }
427
426
  const need: i32 = websocketAdd(live, len)