@amritk/nish-x86_64-linux 0.49.0 → 0.50.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 (92) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
@@ -456,9 +456,11 @@ export class Http1Parser {
456
456
  * @param len how many bytes from `off` to append
457
457
  */
458
458
  feed(data: u8[], off: i32, len: i32): void {
459
- if (!this.tryFeed(data, off, len)) {
459
+ const size: i32 = toI32(data.length)
460
+ if (off < 0 || len < 0 || off > size || len > size - off) {
460
461
  panic("Http1Parser: the window is outside the buffer")
461
462
  }
463
+ http1Append(this, data, off, len)
462
464
  }
463
465
 
464
466
  /**
@@ -477,23 +479,7 @@ export class Http1Parser {
477
479
  if (off < 0 || len < 0 || off > size || len > size - off) {
478
480
  return false
479
481
  }
480
- if (this.state === HTTP1_S_CLOSED || this.state === HTTP1_S_ERROR) {
481
- return true
482
- }
483
- if (!http1Reserve(this, len)) {
484
- return true
485
- }
486
- const buf: u8[] = this.buf
487
- const at: i32 = this.end
488
- // One move: http1Reserve made room for `len` bytes at `at`, and the
489
- // window was checked above, so it always succeeds; were it to fail, the
490
- // parser refuses rather than drop the bytes.
491
- if (!tryCopyBytes(buf, at, data, off, len)) {
492
- http1Fail(this, 413, "the buffer could not take the input")
493
- return true
494
- }
495
- // In range, because the move fit: `at + len` is at most `buf.length`.
496
- this.end = toI32(toU32(at) + toU32(len))
482
+ http1Append(this, data, off, len)
497
483
  return true
498
484
  }
499
485
 
@@ -617,8 +603,15 @@ export class Http1Parser {
617
603
  * @returns the count of unread input bytes, at `buf[start ..]`
618
604
  */
619
605
  buffered(): i32 {
620
- // `start` never passes `end`, and both are offsets into `buf`.
621
- return toI32(toU32(this.end) - toU32(this.start))
606
+ // `start` never passes `end`, and both are offsets into `buf`; a parser
607
+ // whose public fields were moved out of that order holds nothing unread.
608
+ const from: i32 = this.start
609
+ const to: i32 = this.end
610
+ let held: i32 = 0
611
+ if (from >= 0 && to > from) {
612
+ held = to - from
613
+ }
614
+ return held
622
615
  }
623
616
 
624
617
  /**
@@ -676,6 +669,54 @@ const HTTP1_FIRST_SPANS: i32 = 64
676
669
  /** The largest input buffer a parser grows to: 2^30 bytes. */
677
670
  const HTTP1_MOST_INPUT: i32 = 1073741824
678
671
 
672
+ /**
673
+ * `feed`'s and `tryFeed`'s append, once each has checked that
674
+ * `data[off .. off + len)` is a window of `data`. A closed or refused parser
675
+ * drops the bytes; one that cannot make room refuses with a 413.
676
+ */
677
+ const http1Append = (p: Http1Parser, data: u8[], off: i32, len: i32): void => {
678
+ if (p.state === HTTP1_S_CLOSED || p.state === HTTP1_S_ERROR) {
679
+ return
680
+ }
681
+ if (!http1Reserve(p, len)) {
682
+ return
683
+ }
684
+ const buf: u8[] = p.buf
685
+ const at: i32 = p.end
686
+ // One move: http1Reserve made room for `len` bytes at `at`, and the caller
687
+ // checked the window, so it always succeeds; were it to fail, the parser
688
+ // refuses rather than drop the bytes.
689
+ if (!tryCopyBytes(buf, at, data, off, len)) {
690
+ http1Fail(p, 413, "the buffer could not take the input")
691
+ return
692
+ }
693
+ // The move fit, so `at` and `len` are non-negative i32s and their u32 sum
694
+ // is exact: at most `buf.length`.
695
+ const end: u32 = toU32(at) + toU32(len)
696
+ p.end = toI32(end)
697
+ }
698
+
699
+ /**
700
+ * `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
701
+ * step subtracts a non-negative value from one that is no larger than
702
+ * `bytes.length`, so the bounds prover sees no sum that can overflow.
703
+ */
704
+ const http1WindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
705
+ if (off < 0 || len < 0) {
706
+ return -1
707
+ }
708
+ const size: i32 = toI32(bytes.length)
709
+ const room: i32 = size - off
710
+ if (room < 0) {
711
+ return -1
712
+ }
713
+ const gap: i32 = room - len
714
+ if (gap < 0) {
715
+ return -1
716
+ }
717
+ return size - gap
718
+ }
719
+
679
720
  /**
680
721
  * Makes room for `len` more bytes at `p.end`, moving the live bytes down
681
722
  * first. False, with the parser refused, when that would take a buffer past
@@ -721,9 +762,12 @@ const http1Reserve = (p: Http1Parser, len: i32): boolean => {
721
762
  return false
722
763
  }
723
764
  p.buf = buf
724
- p.lineStart = toI32(toU32(p.lineStart) - toU32(from))
725
- p.lineEnd = toI32(toU32(p.lineEnd) - toU32(from))
726
- p.scan = toI32(toU32(p.scan) - toU32(from))
765
+ // A line already consumed sits before `from` and moves below 0, as it
766
+ // always has; `http1Less` holds a mark moved by hand at the i32 range's end
767
+ // rather than let it overflow.
768
+ p.lineStart = http1Less(p.lineStart, from)
769
+ p.lineEnd = http1Less(p.lineEnd, from)
770
+ p.scan = http1Less(p.scan, from)
727
771
  p.start = 0
728
772
  p.end = toI32(live)
729
773
  return true
@@ -742,7 +786,9 @@ const http1Grown = (old: u8[], from: i32, live: u32, size: u32): u8[] => {
742
786
  if (old.length === 0 && size === toU32(HTTP1_FIRST_HEAD)) {
743
787
  return new Array<u8>(HTTP1_FIRST_HEAD)
744
788
  }
745
- const to: i32 = toI32(toU32(from) + live)
789
+ // `live` is the count from `from` to the end of the bytes in `old`, so the
790
+ // window always fits and `to` is its end; -1 would copy nothing.
791
+ const to: i32 = http1WindowEnd(old, from, toI32(live))
746
792
  if (size <= toU32(65535)) {
747
793
  // A size that fits a `u16` is one allocation whose length needs no check.
748
794
  const sized: u8[] = new Array<u8>(toU16(toI32(size)))
@@ -866,7 +912,7 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
866
912
  const bad: u32 = toU32(bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127)))))
867
913
  if (bad !== 0) {
868
914
  // The lowest set lane, 0 to 15: sixteen lanes set at most bit 15.
869
- const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
915
+ const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
870
916
  if (lane >= 0 && lane < 16) {
871
917
  i += lane
872
918
  }
@@ -982,10 +1028,10 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
982
1028
  let run: i32 = 16
983
1029
  if (bad !== 0) {
984
1030
  // The lowest set lane, 0 to 15.
985
- run = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
1031
+ run = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
986
1032
  }
987
- // `0 <= k < stop`, so the u32 difference is the bytes left.
988
- const left: i32 = toI32(toU32(stop) - toU32(k))
1033
+ // `0 <= k < stop`, so the difference is the bytes left.
1034
+ const left: i32 = stop - k
989
1035
  if (run > left) {
990
1036
  run = left
991
1037
  }
@@ -994,8 +1040,13 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
994
1040
  lt(lanes, splatU8x16(toU8(run)))
995
1041
  )
996
1042
  store(select(upper, or(v, splatU8x16(32)), v), head, k)
997
- // `run` is at most the bytes left before `stop`, so the sum fits.
998
- k = toI32(toU32(k) + toU32(run))
1043
+ // `run` is 0 to 16 and `k` at most `head.length - 16`, so the sum is
1044
+ // inside `head`; the test spells that out for the bounds proof and is
1045
+ // never taken.
1046
+ if (run < 0 || run > 16 || k > toI32(head.length) - 16) {
1047
+ return k
1048
+ }
1049
+ k = k + run
999
1050
  if (run < 16) {
1000
1051
  return k
1001
1052
  }
@@ -1055,8 +1106,8 @@ export const http1IsOws = (c: i32): boolean => c === 32 || c === 9
1055
1106
  */
1056
1107
  export const http1SameWord = (text: string, a: i32, b: i32, word: string): boolean => {
1057
1108
  const n: i32 = toI32(word.length)
1058
- // `0 <= a <= b`, so the u32 difference is the slice's length.
1059
- if (a < 0 || b < a || b > toI32(text.length) || toI32(toU32(b) - toU32(a)) !== n) {
1109
+ // `0 <= a <= b` by the tests before it, so the difference is the slice's length.
1110
+ if (a < 0 || b < a || b > toI32(text.length) || b - a !== n) {
1060
1111
  return false
1061
1112
  }
1062
1113
  let j: i32 = a
@@ -1080,8 +1131,8 @@ export const http1SameWord = (text: string, a: i32, b: i32, word: string): boole
1080
1131
  */
1081
1132
  const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean): boolean => {
1082
1133
  const n: i32 = toI32(word.length)
1083
- // `0 <= a <= b`, so the u32 difference is the slice's length.
1084
- if (a < 0 || b < a || b > toI32(bytes.length) || toI32(toU32(b) - toU32(a)) !== n) {
1134
+ // `0 <= a <= b` by the tests before it, so the difference is the slice's length.
1135
+ if (a < 0 || b < a || b > toI32(bytes.length) || b - a !== n) {
1085
1136
  return false
1086
1137
  }
1087
1138
  // `j` walks `bytes[a .. b)` beside `k`: the lengths are equal.
@@ -1162,8 +1213,8 @@ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
1162
1213
  http1Fail(p, 400, "the request line is outside the buffer")
1163
1214
  return -1
1164
1215
  }
1165
- // `0 <= from <= to`, so the u32 difference is the line's length.
1166
- const n: i32 = toI32(toU32(to) - toU32(from))
1216
+ // `0 <= from <= to`, checked above, so the difference is the line's length.
1217
+ const n: i32 = to - from
1167
1218
  const head: u8[] = p.head
1168
1219
  const buf: u8[] = p.buf
1169
1220
  // A line of up to 32 bytes is one or two vector stores; a longer one, or
@@ -1180,8 +1231,14 @@ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
1180
1231
  http1Fail(p, 400, "the request line does not fit the head")
1181
1232
  return -1
1182
1233
  }
1183
- // Both are at most 2^30, so the u32 sum fits.
1184
- p.headLength = toI32(toU32(at) + toU32(n))
1234
+ // The line was stored at `head[at ..]`, so the window fits and its end is
1235
+ // the new length; were it not to, the parser is refused.
1236
+ const headEnd: i32 = http1WindowEnd(head, at, n)
1237
+ if (headEnd < 0) {
1238
+ http1Fail(p, 400, "the request line does not fit the head")
1239
+ return -1
1240
+ }
1241
+ p.headLength = headEnd
1185
1242
  return at
1186
1243
  }
1187
1244
 
@@ -1297,14 +1354,19 @@ const http1HeadIsVector = (
1297
1354
  return false
1298
1355
  }
1299
1356
  if (at > toI32(head.length) - 16) {
1300
- // An offset into the head and a length of at most 16: the u32 sum fits.
1301
- return http1HeadIs(head, at, toI32(toU32(at) + toU32(n)), text, fold)
1357
+ // A window past the head's end matches nothing, as `http1HeadIs` says.
1358
+ const end: i32 = http1WindowEnd(head, at, n)
1359
+ return end >= 0 && http1HeadIs(head, at, end, text, fold)
1302
1360
  }
1303
1361
  let v: u8x16 = loadU8x16(head, at)
1304
1362
  if (fold) {
1305
1363
  v = select(lt(subWrapping(v, splatU8x16(65)), splatU8x16(26)), or(v, splatU8x16(32)), v)
1306
1364
  }
1307
- const want: i32 = n >= 16 ? 65535 : toI32((toU32(1) << toU32(n)) - toU32(1))
1365
+ // The low `n` lanes: `n` is 0 to 16, so the shift is 0 to 16 places.
1366
+ let want: i32 = 65535
1367
+ if (n < 16) {
1368
+ want = toI32(65535) >> (16 - n)
1369
+ }
1308
1370
  return (bitmask(eq(v, word)) & want) === want
1309
1371
  }
1310
1372
 
@@ -1371,7 +1433,7 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
1371
1433
  const commas: u32 = toU32(bitmask(eq(loadU8x16(head, end), splatU8x16(44))))
1372
1434
  if (commas !== 0) {
1373
1435
  // The lowest set lane, 0 to 15.
1374
- const lane: i32 = toI32(31) - toI32(Math.clz32(commas & (toU32(0) - commas)))
1436
+ const lane: i32 = toI32(31) - toI32(Math.clz32(commas & (0 - commas)))
1375
1437
  if (lane >= 0 && lane < 16) {
1376
1438
  end += lane
1377
1439
  }
@@ -1396,8 +1458,11 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
1396
1458
  while (b > a && b > 0 && b <= toI32(head.length) && http1IsOws(toI32(head[b - 1]))) {
1397
1459
  b -= 1
1398
1460
  }
1399
- // `0 <= a <= b`, so the u32 difference is the element's length.
1400
- const n: i32 = a >= 0 && b >= a ? toI32(toU32(b) - toU32(a)) : 0
1461
+ // `0 <= a <= b`, so the difference is the element's length.
1462
+ let n: i32 = 0
1463
+ if (a >= 0 && b >= a) {
1464
+ n = b - a
1465
+ }
1401
1466
  if (n === 5 && http1HeadIsVector(head, a, n, close, "close", true)) {
1402
1467
  found = found | HTTP1_CONNECTION_CLOSE
1403
1468
  } else if (n === 10 && http1HeadIsVector(head, a, n, keepAlive, "keep-alive", true)) {
@@ -1441,8 +1506,10 @@ const http1MethodOverrun = (p: Http1Parser): boolean => {
1441
1506
  return false
1442
1507
  }
1443
1508
  }
1444
- // `start` is within a buffer under 2^30 bytes, so the sum is an i32.
1445
- const stop: i32 = toI32(toU32(from) + toU32(HTTP1_MAX_METHOD))
1509
+ // `start` is within a buffer under 2^30 bytes, so the sum is an i32;
1510
+ // `http1Past` holds a start moved by hand at the i32 range's end rather
1511
+ // than let it overflow.
1512
+ const stop: i32 = http1Past(from, HTTP1_MAX_METHOD)
1446
1513
  for (let k: i32 = from; k >= 0 && k < p.end && k < toI32(buf.length); k += 1) {
1447
1514
  if (buf[k] === 32 || buf[k] === 13 || buf[k] === 10) {
1448
1515
  return false
@@ -1477,7 +1544,7 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1477
1544
  const bad: u32 = toU32(http1NonTokenLanes(loadU8x16(buf, i)))
1478
1545
  if (bad !== 0) {
1479
1546
  // The lowest set lane, 0 to 15.
1480
- const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
1547
+ const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
1481
1548
  if (lane >= 0 && lane < 16) {
1482
1549
  i += lane
1483
1550
  }
@@ -1514,7 +1581,7 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1514
1581
  const bad: u32 = toU32(bitmask(gt(subWrapping(loadU8x16(buf, i), splatU8x16(33)), splatU8x16(93))))
1515
1582
  if (bad !== 0) {
1516
1583
  // The lowest set lane, 0 to 15.
1517
- const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
1584
+ const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
1518
1585
  if (lane >= 0 && lane < 16) {
1519
1586
  i += lane
1520
1587
  }
@@ -1569,8 +1636,8 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1569
1636
  return http1Fail(p, 505, "an HTTP major version other than 1")
1570
1637
  }
1571
1638
  p.minor = buf[i + 7] === 48 ? 0 : 1
1572
- // `a <= b`, checked above.
1573
- const lineLength: i32 = toI32(toU32(b) - toU32(a))
1639
+ // `0 <= a <= b`, checked above.
1640
+ const lineLength: i32 = b - a
1574
1641
  if (p.headLength < 0 || toU64(p.headLength) + toU64(lineLength) + toU64(16) > toU64(p.head.length)) {
1575
1642
  if (!http1HeadGrow(p, lineLength)) {
1576
1643
  return http1Fail(p, 431, "a head of more than 2^30 bytes")
@@ -1581,10 +1648,15 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1581
1648
  return HTTP1_ERROR
1582
1649
  }
1583
1650
  p.methodStart = at
1584
- // Each mark is within the line, `a` to `b`, and the line is in the head.
1585
- p.methodEnd = http1Past(at, toI32(toU32(methodEnd) - toU32(a)))
1586
- p.targetStart = http1Past(at, toI32(toU32(targetStart) - toU32(a)))
1587
- p.targetEnd = http1Past(at, toI32(toU32(targetEnd) - toU32(a)))
1651
+ // Each mark is within the line, `a` to `b`, and the line now ends the head,
1652
+ // so each offset is non-negative and its u32 sum with `at` is exact: at
1653
+ // most the head's length.
1654
+ const methodAt: u32 = toU32(at) + toU32(methodEnd - a)
1655
+ const targetFrom: u32 = toU32(at) + toU32(targetStart - a)
1656
+ const targetTo: u32 = toU32(at) + toU32(targetEnd - a)
1657
+ p.methodEnd = toI32(methodAt)
1658
+ p.targetStart = toI32(targetFrom)
1659
+ p.targetEnd = toI32(targetTo)
1588
1660
  if (p.keepText) {
1589
1661
  p.method = http1Text(buf, a, methodEnd, false)
1590
1662
  p.target = http1Text(buf, targetStart, targetEnd, false)
@@ -1599,7 +1671,7 @@ const http1RequestLine = (p: Http1Parser): i32 => {
1599
1671
  * head, `http1Section`'s copy of it: `buf[x]` is at `head[x + delta]`.
1600
1672
  * Answers `HTTP1_NEED_MORE` or the refusal.
1601
1673
  */
1602
- const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
1674
+ const http1FieldLine = (p: Http1Parser, keep: boolean, shift: integer<0, 2147483646>): i32 => {
1603
1675
  const buf: u8[] = p.buf
1604
1676
  const n: i32 = toI32(buf.length)
1605
1677
  const a: i32 = p.lineStart
@@ -1610,103 +1682,138 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
1610
1682
  // read outside `buf`.
1611
1683
  return http1Fail(p, 400, "a field line out of order")
1612
1684
  }
1613
- // Every offset below is in the line, `a` to `b`, or its copy in the head,
1614
- // both under 2^31 bytes, so they are added with `http1Past` and
1615
- // subtracted with `http1Less`, which do not check what cannot wrap.
1616
- // A kept line's name is read in the head's copy, each upper-case letter
1617
- // lowered as the scan passes it: one walk over the name, not a check of
1618
- // `buf` and then a second walk of the copy. The copy of a line refused
1619
- // below is never read, since a refusal ends the parse.
1620
- let at: i32 = 0
1621
- let i: i32 = a
1622
- if (keep) {
1623
- at = http1Past(a, delta)
1624
- i = http1Past(a, http1Less(http1LowerName(p.head, at, http1Past(at, http1Less(b, a))), at))
1625
- } else {
1685
+ if (!keep) {
1686
+ // A trailer is checked and counted, but not kept: nothing here reads one.
1687
+ let i: i32 = a
1626
1688
  while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
1627
1689
  i += 1
1628
1690
  }
1629
- }
1630
- if (i === a || i >= b || i < 0 || i >= toI32(buf.length) || buf[i] !== 58) {
1631
- // Whitespace is no tchar, so an obs-fold line has an empty name: its
1632
- // own refusal is told apart here, off the path every field takes.
1633
- if (i === a && a >= 0 && a < b && a < toI32(buf.length) && (buf[a] === 32 || buf[a] === 9)) {
1634
- return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
1691
+ if (i === a || i >= b || i < 0 || i >= toI32(buf.length) || buf[i] !== 58) {
1692
+ return http1NameRefused(p, i === a)
1635
1693
  }
1636
- return http1Fail(p, 400, "a malformed field name")
1694
+ return http1FieldCounted(p)
1695
+ }
1696
+ // A kept line is read in the head's copy, `head[at .. end)`, its name
1697
+ // lowered as the scan passes it: one walk over the name, not a check of
1698
+ // `buf` and then a second walk of the copy. The copy of a line refused
1699
+ // below is never read, since a refusal ends the parse. The head is at most
1700
+ // 2^30 bytes, so the copy's start and length are too; the test spells that
1701
+ // out for the bounds proof, and refuses a parser moved by hand.
1702
+ if (a > 1073741823) {
1703
+ return http1Fail(p, 431, "a head of more than 2^30 bytes")
1704
+ }
1705
+ const at: i32 = a - 1073741823 + shift
1706
+ const length: i32 = b - a
1707
+ if (at < 0 || at > 1073741823 || length > 1073741823) {
1708
+ return http1Fail(p, 431, "a head of more than 2^30 bytes")
1709
+ }
1710
+ const end: i32 = at + length
1711
+ const head: u8[] = p.head
1712
+ const nameEnd: i32 = http1LowerName(head, at, end)
1713
+ if (
1714
+ nameEnd === at ||
1715
+ nameEnd >= end ||
1716
+ nameEnd < 0 ||
1717
+ nameEnd >= toI32(head.length) ||
1718
+ head[nameEnd] !== 58
1719
+ ) {
1720
+ return http1NameRefused(p, nameEnd === at)
1721
+ }
1722
+ const counted: i32 = http1FieldCounted(p)
1723
+ if (counted !== HTTP1_NEED_MORE) {
1724
+ return counted
1725
+ }
1726
+ // `nameEnd` is inside the head, so the value starts inside it or at its end.
1727
+ let valueStart: i32 = nameEnd + 1
1728
+ let valueEnd: i32 = end
1729
+ while (
1730
+ valueStart >= 0 &&
1731
+ valueStart < valueEnd &&
1732
+ valueStart < toI32(head.length) &&
1733
+ (head[valueStart] === 32 || head[valueStart] === 9)
1734
+ ) {
1735
+ valueStart += 1
1637
1736
  }
1638
- const nameEnd: i32 = i
1639
- // The name is tchars and the colon is field text, so a byte no value may
1640
- // hold anywhere on the line is one in the value.
1737
+ while (
1738
+ valueEnd >= 1 &&
1739
+ valueEnd > valueStart &&
1740
+ valueEnd <= toI32(head.length) &&
1741
+ (head[valueEnd - 1] === 32 || head[valueEnd - 1] === 9)
1742
+ ) {
1743
+ valueEnd -= 1
1744
+ }
1745
+ // The fields that frame the message are counted here, as their names
1746
+ // are read, for `http1EndOfHead`: every kept field asks the same question.
1747
+ // Each count is at most `fieldCount`, and the value bytes at most the
1748
+ // head's.
1749
+ const kind: i32 = http1FramingField(head, at, nameEnd - at)
1750
+ if (kind === HTTP1_FIELD_HOST) {
1751
+ p.hostFields = http1Past(p.hostFields, 1)
1752
+ p.hostField = p.headerCount
1753
+ } else if (kind === HTTP1_FIELD_LENGTH) {
1754
+ p.lengthFields = http1Past(p.lengthFields, 1)
1755
+ p.lengthField = p.headerCount
1756
+ } else if (kind === HTTP1_FIELD_CODING) {
1757
+ p.codingFields = http1Past(p.codingFields, 1)
1758
+ } else if (kind === HTTP1_FIELD_CONNECTION) {
1759
+ p.connectionOptions = p.connectionOptions | http1ConnectionList(head, valueStart, valueEnd)
1760
+ } else if (kind === HTTP1_FIELD_UPGRADE) {
1761
+ p.upgradeFields = http1Past(p.upgradeFields, 1)
1762
+ p.upgradeValueBytes = http1Past(p.upgradeValueBytes, http1Less(valueEnd, valueStart))
1763
+ }
1764
+ // Four spans a field. Each field line is at least four bytes of a head
1765
+ // under 2^30, so the count is below 2^28 and the product fits; the test
1766
+ // spells that out for the bounds proof, and refuses a count moved by hand.
1767
+ const count: i32 = p.headerCount
1768
+ if (count < 0 || count > 536870911) {
1769
+ return http1Fail(p, 431, "more fields than the limit")
1770
+ }
1771
+ const base: i32 = count * 4
1772
+ http1SpanSet(p, base + 3, valueEnd)
1773
+ // The last of the four is set first, so the array has room for them all.
1774
+ const spans: i32[] = p.spans
1775
+ if (base >= 0 && base < toI32(spans.length) - 2) {
1776
+ spans[base] = at
1777
+ spans[base + 1] = nameEnd
1778
+ spans[base + 2] = valueStart
1779
+ }
1780
+ p.headerCount = count + 1
1781
+ if (p.keepText) {
1782
+ // The copy holds the line's bytes, its name already lowered.
1783
+ p.names.push(http1Text(head, at, nameEnd, true))
1784
+ p.values.push(http1Text(head, valueStart, valueEnd, false))
1785
+ }
1786
+ return HTTP1_NEED_MORE
1787
+ }
1788
+
1789
+ /**
1790
+ * The refusal of a field line whose name is not tchars up to a colon:
1791
+ * whitespace is no tchar, so an obs-fold line has an empty name, and its own
1792
+ * refusal is told apart here, off the path every field takes.
1793
+ */
1794
+ const http1NameRefused = (p: Http1Parser, empty: boolean): i32 => {
1795
+ const buf: u8[] = p.buf
1796
+ const a: i32 = p.lineStart
1797
+ if (empty && a >= 0 && a < p.lineEnd && a < toI32(buf.length) && (buf[a] === 32 || buf[a] === 9)) {
1798
+ return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
1799
+ }
1800
+ return http1Fail(p, 400, "a malformed field name")
1801
+ }
1802
+
1803
+ /**
1804
+ * Counts a field line whose name has been read, or refuses it: a control
1805
+ * byte on the line is one in its value, the name being tchars and the colon
1806
+ * field text, and the count stops at `maxHeaders`.
1807
+ */
1808
+ const http1FieldCounted = (p: Http1Parser): i32 => {
1641
1809
  if (p.lineControl) {
1642
1810
  return http1Fail(p, 400, "a control character in a field value")
1643
1811
  }
1644
- // The count is at most `maxHeaders` before it, checked below each time.
1645
- p.fieldCount = http1Past(p.fieldCount, 1)
1646
- if (p.fieldCount > p.maxHeaders) {
1812
+ const count: i32 = p.fieldCount
1813
+ if (count >= p.maxHeaders) {
1647
1814
  return http1Fail(p, 431, "more fields than the limit")
1648
1815
  }
1649
- // A trailer is checked and counted, but not kept: nothing here reads one.
1650
- if (keep) {
1651
- let valueStart: i32 = http1Past(nameEnd, 1)
1652
- let valueEnd: i32 = b
1653
- while (
1654
- valueStart >= 0 &&
1655
- valueStart < valueEnd &&
1656
- valueStart < toI32(buf.length) &&
1657
- (buf[valueStart] === 32 || buf[valueStart] === 9)
1658
- ) {
1659
- valueStart += 1
1660
- }
1661
- while (
1662
- valueEnd >= 1 &&
1663
- valueEnd > valueStart &&
1664
- valueEnd <= toI32(buf.length) &&
1665
- (buf[valueEnd - 1] === 32 || buf[valueEnd - 1] === 9)
1666
- ) {
1667
- valueEnd -= 1
1668
- }
1669
- // The fields that frame the message are counted here, as their names
1670
- // are read, for `http1EndOfHead`: every kept field asks the same question.
1671
- // Each count is at most `fieldCount`, and the value bytes at most the
1672
- // head's.
1673
- const kind: i32 = http1FramingField(p.head, at, http1Less(nameEnd, a))
1674
- if (kind === HTTP1_FIELD_HOST) {
1675
- p.hostFields = http1Past(p.hostFields, 1)
1676
- p.hostField = p.headerCount
1677
- } else if (kind === HTTP1_FIELD_LENGTH) {
1678
- p.lengthFields = http1Past(p.lengthFields, 1)
1679
- p.lengthField = p.headerCount
1680
- } else if (kind === HTTP1_FIELD_CODING) {
1681
- p.codingFields = http1Past(p.codingFields, 1)
1682
- } else if (kind === HTTP1_FIELD_CONNECTION) {
1683
- p.connectionOptions =
1684
- p.connectionOptions |
1685
- http1ConnectionList(
1686
- p.head,
1687
- http1Past(at, http1Less(valueStart, a)),
1688
- http1Past(at, http1Less(valueEnd, a))
1689
- )
1690
- } else if (kind === HTTP1_FIELD_UPGRADE) {
1691
- p.upgradeFields = http1Past(p.upgradeFields, 1)
1692
- p.upgradeValueBytes = http1Past(p.upgradeValueBytes, http1Less(valueEnd, valueStart))
1693
- }
1694
- // Four spans a field, and fields are at most `maxHeaders`.
1695
- const base: i32 = toI32(toU32(4) * toU32(p.headerCount))
1696
- http1SpanSet(p, http1Past(base, 3), http1Past(at, http1Less(valueEnd, a)))
1697
- // The last of the four is set first, so the array has room for them all.
1698
- const spans: i32[] = p.spans
1699
- if (base >= 0 && base < toI32(spans.length) - 2) {
1700
- spans[base] = at
1701
- spans[base + 1] = http1Past(at, http1Less(nameEnd, a))
1702
- spans[base + 2] = http1Past(at, http1Less(valueStart, a))
1703
- }
1704
- p.headerCount = http1Past(p.headerCount, 1)
1705
- if (p.keepText) {
1706
- p.names.push(http1Text(buf, a, nameEnd, true))
1707
- p.values.push(http1Text(buf, valueStart, valueEnd, false))
1708
- }
1709
- }
1816
+ p.fieldCount = count + 1
1710
1817
  return HTTP1_NEED_MORE
1711
1818
  }
1712
1819
 
@@ -2032,7 +2139,7 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
2032
2139
  if (from < 0 || to <= from) {
2033
2140
  return HTTP1_NEED_MORE
2034
2141
  }
2035
- const ready: i32 = toI32(toU32(to) - toU32(from))
2142
+ const ready: i32 = to - from
2036
2143
  let take: i32 = ready < p.left ? ready : p.left
2037
2144
  if (take > p.maxChunk && p.maxChunk > 0) {
2038
2145
  take = p.maxChunk
@@ -2048,10 +2155,10 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
2048
2155
  p.bodyOff = from
2049
2156
  p.bodyLen = take
2050
2157
  // `take` is at most `end - start`, and at most `left`, which was counted
2051
- // within `maxBody`: none of the three wraps.
2158
+ // within `maxBody`: none of the three leaves the i32 range.
2052
2159
  p.start = http1Past(from, take)
2053
2160
  p.scan = p.start
2054
- p.left = toI32(toU32(p.left) - toU32(take))
2161
+ p.left = http1Less(p.left, take)
2055
2162
  p.bodyTotal = http1Past(total, take)
2056
2163
  if (p.left === 0) {
2057
2164
  p.state = after
@@ -2059,6 +2166,20 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
2059
2166
  return HTTP1_BODY
2060
2167
  }
2061
2168
 
2169
+ /**
2170
+ * The last `q` whose vector, landing at `q + delta`, still fits in `head`
2171
+ * with sixteen bytes to spare: `head.length - 32 - delta`, held at the i32
2172
+ * range's end, which a head under 2^30 bytes never reaches.
2173
+ */
2174
+ const http1GrowAt = (head: u8[], delta: i32): i32 => {
2175
+ const room: i64 = toI64(toI32(head.length)) - toI64(32) - toI64(delta)
2176
+ let at: i32 = 2147483647
2177
+ if (room >= toI64(-2147483647) && room < toI64(2147483647)) {
2178
+ at = toI32(room)
2179
+ }
2180
+ return at
2181
+ }
2182
+
2062
2183
  /**
2063
2184
  * Reads a header or trailer section's lines while whole ones are buffered:
2064
2185
  * `http1TakeLine` and `http1FieldLine` in one loop, each line limited by what
@@ -2093,11 +2214,29 @@ const http1Section = (p: Http1Parser): i32 => {
2093
2214
  // line), so the copy, and the head, stay within the limit.
2094
2215
  //
2095
2216
  // Every offset here is a position in `buf` or the head, both under 2^31
2096
- // bytes, and `delta` moves one to the other, so the sums below are taken
2097
- // with `http1Past` and `http1Less`, which do not check what cannot pass.
2217
+ // bytes, and `delta` moves one to the other, so the sums below never leave
2218
+ // the i32 range. A sum a guard bounds is written plainly; the rest are
2219
+ // taken with `http1Past` and `http1Less`, whose proofs need no bound from
2220
+ // the caller.
2098
2221
  const copy: boolean = !trailer
2099
- const delta: i32 = http1Less(p.headLength, p.start)
2100
- const wideStop: i64 = toI64(p.start) + toI64(p.maxHeaderBytes) - toI64(p.sectionBytes) + toI64(2)
2222
+ // The input and the head are each at most 2^30 bytes, so `delta` is
2223
+ // within 2^30 either way; the test spells that out for the bounds proof,
2224
+ // and refuses a parser moved by hand.
2225
+ const headLength: i32 = p.headLength
2226
+ const from: i32 = p.start
2227
+ if (headLength < 0 || headLength > 1073741823 || from < 0 || from > 1073741823) {
2228
+ return http1Fail(p, 431, "a head of more than 2^30 bytes")
2229
+ }
2230
+ const lead: integer<0, 1073741823> = headLength
2231
+ const fed: integer<0, 1073741823> = from
2232
+ const shifted: i32 = lead + (1073741823 - fed)
2233
+ if (shifted < 0 || shifted > 2147483646) {
2234
+ return http1Fail(p, 431, "a head of more than 2^30 bytes")
2235
+ }
2236
+ const shift: integer<0, 2147483646> = shifted
2237
+ const delta: i32 = shift - 1073741823
2238
+ const most: i32 = p.maxHeaderBytes
2239
+ const wideStop: i64 = toI64(p.start) + toI64(most) - toI64(p.sectionBytes) + toI64(2)
2101
2240
  let copyStop: i32 = 2147483647
2102
2241
  if (wideStop >= toI64(-2147483647) && wideStop < toI64(2147483647)) {
2103
2242
  copyStop = toI32(wideStop)
@@ -2106,51 +2245,61 @@ const http1Section = (p: Http1Parser): i32 => {
2106
2245
  // passes `growAt`, so most vectors pay one compare for each.
2107
2246
  const copyEnd: i32 = copy ? copyStop : -2147483647
2108
2247
  let head: u8[] = p.head
2109
- let growAt: i32 = http1Less(http1Less(toI32(head.length), 32), delta)
2248
+ let growAt: i32 = http1GrowAt(head, delta)
2110
2249
  // Bit `k` of `bits` is set when `buf[base + k]` is a control byte or DEL
2111
- // at or past `i`: the vector last loaded, its lanes before `i` cleared.
2112
- let base: i32 = http1Less(i, 16)
2250
+ // at or past `i`: the vector last loaded, at `base`, its lanes before `i`
2251
+ // cleared. `base` is the start of a vector in `buf`, so its range is
2252
+ // declared for a lane to be added to it, and the next vector is at `next`
2253
+ // or `i`.
2254
+ let base: integer<0, 1073741823> = 0
2255
+ let next: i32 = i
2113
2256
  let bits: u32 = 0
2257
+ // A vector at or below `lastWhole` holds sixteen bytes before `stop`.
2258
+ const lastWhole: i32 = http1Less(stop, 16)
2114
2259
  while (true) {
2115
2260
  // The next control byte at or past `i`, or the end of the bytes so far.
2116
2261
  let j: i32 = stop
2117
2262
  let seeking: boolean = true
2118
2263
  while (seeking) {
2119
2264
  if (bits !== 0) {
2120
- // The lowest set bit, 0 to 15.
2121
- j = http1Past(base, toI32(31) - toI32(Math.clz32(bits & (toU32(0) - bits))))
2265
+ // The lowest set bit, 0 to 15, a lane of the vector at `base`, which
2266
+ // is at most `buf.length - 16`: the sum is a position in `buf`.
2267
+ const low: u32 = bits & (toU32(0) - bits)
2268
+ j = base + (31 - toI32(Math.clz32(low)))
2122
2269
  seeking = false
2123
2270
  } else {
2124
- const next: i32 = http1Past(base, 16)
2125
2271
  const q: i32 = next > i ? next : i
2126
2272
  if (q >= stop) {
2127
2273
  seeking = false
2128
- } else if (q >= 0 && q <= toI32(buf.length) - 16) {
2274
+ } else if (q >= 0 && q < 1073741824 && q < toI32(buf.length) - 15) {
2129
2275
  // The vector may run past `stop` into bytes not yet fed: its lanes
2130
2276
  // there are dropped from `bits`, and its copy of them is
2131
2277
  // overwritten when they arrive.
2132
2278
  const v: u8x16 = loadU8x16(buf, q)
2279
+ base = q
2280
+ next = q + 16
2133
2281
  if (q < copyEnd) {
2134
2282
  if (q > growAt) {
2135
2283
  if (!http1HeadGrow(p, http1Less(http1Past(http1Past(q, delta), 16), p.headLength))) {
2136
2284
  return http1Fail(p, 431, "a head of more than 2^30 bytes")
2137
2285
  }
2138
2286
  head = p.head
2139
- growAt = http1Less(http1Less(toI32(head.length), 32), delta)
2287
+ growAt = http1GrowAt(head, delta)
2140
2288
  }
2141
2289
  // `q <= growAt` here, so the vector fits in the head.
2142
- const to: i32 = http1Past(q, delta)
2290
+ const to: i32 = q - 1073741823 + shift
2143
2291
  if (to >= 0 && to <= toI32(head.length) - 16) {
2144
2292
  store(v, head, to)
2145
2293
  }
2146
2294
  }
2147
2295
  bits = toU32(bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127)))))
2148
- // `q < stop` here: keep the lanes before `stop`, 1 to 15 of them.
2149
- const keep: i32 = http1Less(stop, q)
2150
- if (keep < 16) {
2151
- bits = bits & ((toU32(1) << toU32(keep)) - toU32(1))
2296
+ if (q > lastWhole) {
2297
+ // `q < stop` here: keep the lanes before `stop`, 1 to 15 of them.
2298
+ const keep: i32 = http1Less(stop, q)
2299
+ if (keep < 16) {
2300
+ bits = bits & ((toU32(1) << toU32(keep)) - toU32(1))
2301
+ }
2152
2302
  }
2153
- base = q
2154
2303
  } else {
2155
2304
  // Fewer than sixteen bytes are left in `buf`: a byte at a time.
2156
2305
  let k: i32 = q
@@ -2175,7 +2324,7 @@ const http1Section = (p: Http1Parser): i32 => {
2175
2324
  growAt = http1Less(http1Less(toI32(head.length), 32), delta)
2176
2325
  }
2177
2326
  j = k
2178
- base = http1Less(k, 15)
2327
+ next = http1Past(k, 1)
2179
2328
  seeking = false
2180
2329
  }
2181
2330
  }
@@ -2193,18 +2342,19 @@ const http1Section = (p: Http1Parser): i32 => {
2193
2342
  }
2194
2343
  if (lf >= 0) {
2195
2344
  const start: i32 = p.start
2196
- if (lf === start || lf < 1 || lf > toI32(buf.length) || buf[lf - 1] !== 13) {
2345
+ if (lf === start || lf < 1 || lf > 1073741823 || lf >= toI32(buf.length) || buf[lf - 1] !== 13) {
2197
2346
  return http1Fail(p, 400, "a line ended in a bare LF")
2198
2347
  }
2199
- // The line, `start` to `lf`, is in `buf`.
2200
- const after: i32 = http1Past(lf, 1)
2201
- const sectionBytes: i32 = http1Past(p.sectionBytes, http1Less(after, start))
2202
- if (toI64(sectionBytes) - toI64(2) > toI64(p.maxHeaderBytes)) {
2348
+ // The line, `start` to `lf`, is in `buf`, so `after` is at most its
2349
+ // length.
2350
+ const after: i32 = lf + 1
2351
+ const sectionBytes: i64 = toI64(p.sectionBytes) + toI64(after) - toI64(start)
2352
+ if (sectionBytes > toI64(most) + toI64(2)) {
2203
2353
  return http1Fail(p, 431, "a line over its limit")
2204
2354
  }
2205
2355
  // The scan stops at the CR, so the LF after it may be in no vector
2206
2356
  // or tail copied: store it, and the head holds each line's CRLF.
2207
- const at: i32 = http1Past(lf, delta)
2357
+ const at: i32 = lf - 1073741823 + shift
2208
2358
  if (copy && at >= 0 && at < toI32(head.length)) {
2209
2359
  head[at] = 10
2210
2360
  }
@@ -2219,19 +2369,20 @@ const http1Section = (p: Http1Parser): i32 => {
2219
2369
  if (c === 13) {
2220
2370
  bits = bits & (bits - toU32(1))
2221
2371
  }
2222
- p.sectionBytes = sectionBytes
2223
- if (sectionBytes > p.maxHeaderBytes) {
2372
+ if (sectionBytes > toI64(most)) {
2224
2373
  return http1Fail(p, 431, "a header section over the limit")
2225
2374
  }
2375
+ // At most `maxHeaderBytes`, checked just above.
2376
+ p.sectionBytes = toI32(sectionBytes)
2226
2377
  if (lf - 1 === start) {
2227
2378
  if (trailer) {
2228
2379
  p.state = HTTP1_S_MESSAGE_END
2229
2380
  return HTTP1_LINE
2230
2381
  }
2231
- p.headLength = http1Past(p.start, delta)
2382
+ p.headLength = after - 1073741823 + shift
2232
2383
  return http1EndOfHead(p)
2233
2384
  }
2234
- if (http1FieldLine(p, copy, delta) === HTTP1_ERROR) {
2385
+ if (http1FieldLine(p, copy, shift) === HTTP1_ERROR) {
2235
2386
  return HTTP1_ERROR
2236
2387
  }
2237
2388
  p.lineControl = false
@@ -2244,7 +2395,8 @@ const http1Section = (p: Http1Parser): i32 => {
2244
2395
  if (c !== 9) {
2245
2396
  p.lineControl = true
2246
2397
  }
2247
- i = http1Past(j, 1)
2398
+ // `j < buf.length`, checked above.
2399
+ i = j + 1
2248
2400
  bits = bits & (bits - toU32(1))
2249
2401
  }
2250
2402
  }
@@ -2275,8 +2427,14 @@ const http1Next = (p: Http1Parser): i32 => {
2275
2427
  }
2276
2428
  // The longest well-formed request line: the method, the target, two
2277
2429
  // spaces and "HTTP/1.1".
2278
- // `maxTarget` is clamped to 2^30, so the sum is an i32.
2279
- const got: i32 = http1TakeLine(p, http1Past(p.maxTarget, HTTP1_MAX_METHOD + 10), 414)
2430
+ // `maxTarget` is clamped to 2^30, and one moved past it by hand is held
2431
+ // there, so the sum is an i32.
2432
+ const target: i32 = p.maxTarget
2433
+ const got: i32 = http1TakeLine(
2434
+ p,
2435
+ target < 1073741824 ? target + (HTTP1_MAX_METHOD + 10) : 1073741824,
2436
+ 414
2437
+ )
2280
2438
  if (got !== HTTP1_LINE) {
2281
2439
  return got
2282
2440
  }
@@ -2521,23 +2679,29 @@ export const http1TryWriteChunk = (out: u8[], at: i32, data: readonly u8[], off:
2521
2679
  if (at < 0 || at > outLength || need > toU64(toU32(outLength) - toU32(at))) {
2522
2680
  return HTTP1_NO_ROOM
2523
2681
  }
2524
- // It fits, so none of the offsets below passes `outLength`.
2525
- const end: i32 = http1Past(at, digits)
2526
- const headEnd: i32 = http1Past(end, 2)
2682
+ // It fits, so none of the offsets below passes `outLength`: their u32
2683
+ // sums are exact.
2684
+ const end32: u32 = toU32(at) + toU32(digits)
2685
+ const headEnd32: u32 = end32 + toU32(2)
2686
+ const end: i32 = toI32(end32)
2687
+ const headEnd: i32 = toI32(headEnd32)
2527
2688
  // The bytes go first, so a copy that fails leaves `out` untouched. It
2528
2689
  // cannot: the window is inside `data` and the chunk fits, both checked.
2529
2690
  if (!tryCopyBytes(out, headEnd, data, off, len)) {
2530
2691
  return HTTP1_NO_ROOM
2531
2692
  }
2532
2693
  let v: i32 = len
2533
- // The digits are written last first, `out[k - 1]` each time.
2534
- for (let k: i32 = end; k > 0 && k > at && k <= toI32(out.length); k -= 1) {
2694
+ // The digits are written last first, `out[j]` each time. `end32` is at
2695
+ // least `digits`, which is at least 1, so `last32` is exact.
2696
+ const last32: u32 = end32 - toU32(1)
2697
+ for (let j: i32 = toI32(last32); j >= at && j >= 0 && j < outLength; j -= 1) {
2535
2698
  const d: i32 = v & 15
2536
- out[k - 1] = toU8(d < 10 ? 48 + d : 87 + d)
2699
+ out[j] = toU8(d < 10 ? 48 + d : 87 + d)
2537
2700
  v = v >> 4
2538
2701
  }
2539
2702
  http1PutCrlf(out, end)
2540
- return http1PutCrlf(out, http1Past(headEnd, len))
2703
+ const tail32: u32 = headEnd32 + toU32(len)
2704
+ return http1PutCrlf(out, toI32(tail32))
2541
2705
  }
2542
2706
 
2543
2707
  /**
@@ -2701,32 +2865,49 @@ export const http1PutText = (out: u8[], at: i32, text: string): i32 => {
2701
2865
  k = at > -n ? http1Less(0, at) : n
2702
2866
  }
2703
2867
  let j: i32 = http1Past(at, k)
2704
- while (k >= 0 && k < n && j >= 0 && j < toI32(out.length)) {
2705
- out[j] = toU8(text.charCodeAt(k))
2706
- j += 1
2707
- k += 1
2868
+ // Both start at 0 or past it and only climb, so the test is made once.
2869
+ if (k >= 0 && j >= 0) {
2870
+ while (k < n && j < toI32(out.length)) {
2871
+ out[j] = toU8(text.charCodeAt(k))
2872
+ j += 1
2873
+ k += 1
2874
+ }
2708
2875
  }
2709
2876
  return http1Past(at, n)
2710
2877
  }
2711
2878
 
2712
2879
  /**
2713
- * `at + n` for an `n` that is not negative, wrapping as an i32 where the sum
2714
- * would pass 2^31: the writers' callers measure the head first, so it never
2715
- * does for them.
2880
+ * `at + n`, held at the i32 range's end where the sum would leave it: the
2881
+ * writers' callers measure the head first and the parser adds only offsets
2882
+ * in buffers under 2^30 bytes, so it never does for them. The sum is taken
2883
+ * in i64, where two i32s cannot overflow, and clamped back: two selects,
2884
+ * where a branch on each sign would cost the scan a compare per offset.
2716
2885
  */
2717
- const http1Past = (at: i32, n: i32): i32 => toI32(toU32(at) + toU32(n))
2886
+ const http1Past = (at: i32, n: i32): i32 => {
2887
+ const s: i64 = toI64(at) + toI64(n)
2888
+ const hi: i64 = s > toI64(2147483647) ? toI64(2147483647) : s
2889
+ const lo: i64 = hi < toI64(-2147483648) ? toI64(-2147483648) : hi
2890
+ return toI32(lo)
2891
+ }
2718
2892
 
2719
2893
  /**
2720
- * `at - n`, wrapping as an i32 where the difference would leave its range:
2894
+ * `at - n`, held at the i32 range's end where the difference would leave it:
2721
2895
  * the parser takes it only between offsets in one buffer, so it never does.
2896
+ * Taken in i64 and clamped, as `http1Past` takes a sum.
2722
2897
  */
2723
- const http1Less = (at: i32, n: i32): i32 => toI32(toU32(at) - toU32(n))
2898
+ const http1Less = (at: i32, n: i32): i32 => {
2899
+ const s: i64 = toI64(at) - toI64(n)
2900
+ const hi: i64 = s > toI64(2147483647) ? toI64(2147483647) : s
2901
+ const lo: i64 = hi < toI64(-2147483648) ? toI64(-2147483648) : hi
2902
+ return toI32(lo)
2903
+ }
2724
2904
 
2725
2905
  /** Writes CR LF at `out[at ..)`, likewise: two stores, where `http1PutText` walks a string. */
2726
2906
  const http1PutCrlf = (out: u8[], at: i32): i32 => {
2727
2907
  if (at >= 0 && at < toI32(out.length) - 1) {
2728
2908
  out[at] = 13
2729
2909
  out[at + 1] = 10
2910
+ return at + 2
2730
2911
  }
2731
2912
  return http1Past(at, 2)
2732
2913
  }
@@ -2736,6 +2917,7 @@ const http1PutColonSpace = (out: u8[], at: i32): i32 => {
2736
2917
  if (at >= 0 && at < toI32(out.length) - 1) {
2737
2918
  out[at] = 58
2738
2919
  out[at + 1] = 32
2920
+ return at + 2
2739
2921
  }
2740
2922
  return http1Past(at, 2)
2741
2923
  }
@@ -2743,12 +2925,16 @@ const http1PutColonSpace = (out: u8[], at: i32): i32 => {
2743
2925
  /** Writes `bytes` at `out[at ..)`, likewise, as one copy when it fits. */
2744
2926
  const http1PutBytes = (out: u8[], at: i32, bytes: u8[]): i32 => {
2745
2927
  const n: i32 = toI32(bytes.length)
2746
- if (!tryCopyBytes(out, at, bytes, 0, n)) {
2747
- let j: i32 = at
2748
- for (let k: i32 = 0; k < n && k < toI32(bytes.length) && j >= 0 && j < toI32(out.length); k += 1) {
2749
- out[j] = bytes[k]
2750
- j += 1
2751
- }
2928
+ if (tryCopyBytes(out, at, bytes, 0, n)) {
2929
+ // The copy fit, so `at` and `n` are non-negative i32s whose u32 sum is
2930
+ // exact: at most `out.length`.
2931
+ const end: u32 = toU32(at) + toU32(n)
2932
+ return toI32(end)
2933
+ }
2934
+ let j: i32 = at
2935
+ for (let k: i32 = 0; k < n && k < toI32(bytes.length) && j >= 0 && j < toI32(out.length); k += 1) {
2936
+ out[j] = bytes[k]
2937
+ j += 1
2752
2938
  }
2753
2939
  return http1Past(at, n)
2754
2940
  }
@@ -2763,7 +2949,10 @@ const http1PutBytes = (out: u8[], at: i32, bytes: u8[]): i32 => {
2763
2949
  const http1PutFixed = (out: u8[], at: i32, limit: i32, v: u8x16, text: string): i32 => {
2764
2950
  if (at >= 0 && limit >= 16 && at <= limit - 16 && at <= toI32(out.length) - 16) {
2765
2951
  store(v, out, at)
2766
- return http1Past(at, toI32(text.length))
2952
+ // `at` is at most `out.length - 16` and `text` at most sixteen bytes, so
2953
+ // the u32 sum is exact.
2954
+ const end: u32 = toU32(at) + toU32(text.length)
2955
+ return toI32(end)
2767
2956
  }
2768
2957
  return http1PutText(out, at, text)
2769
2958
  }
@@ -2785,7 +2974,7 @@ const http1PutStatusLine = (out: u8[], at: i32, limit: i32, status: i32): i32 =>
2785
2974
  out,
2786
2975
  at
2787
2976
  )
2788
- return http1Past(at, 13)
2977
+ return at + 13
2789
2978
  }
2790
2979
  const end: i32 = http1PutText(out, at, "HTTP/1.1 ")
2791
2980
  if (end >= 0 && end < toI32(out.length) - 3) {