@amritk/nish-aarch64-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/router.ts CHANGED
@@ -118,7 +118,8 @@ const ROUTER_WILD: i32 = 2
118
118
 
119
119
  /**
120
120
  * What a `Router` holds and refuses past. Each field has a default a server
121
- * can use as it is; the constructor panics on a value no table could work with.
121
+ * can use as it is; the constructor panics on a value no table could work with, and
122
+ * `routerChecked` refuses one.
122
123
  */
123
124
  export class RouterLimits {
124
125
  /** The longest pattern, in bytes (1 to 65536). */
@@ -236,6 +237,13 @@ export class Router {
236
237
  private foundCount: i32 = 0
237
238
  /** During a match: whether a route of another method ended at the path's end. */
238
239
  private otherMethod: boolean = false
240
+ /**
241
+ * Set by `enter` on a range outside its array, and cleared by the `match`
242
+ * that reads it: only then does `match` test the ranges again, to panic.
243
+ * One left set by `tryMatch` costs the next `match` that answers
244
+ * `ROUTER_NOT_FOUND` a test that passes.
245
+ */
246
+ private outside: boolean = false
239
247
  /** The nodes the last `match` visited: never more than `nodeCount()`, which the tests pin, and none when the exact-path table answered. */
240
248
  private visited: i32 = 0
241
249
 
@@ -244,11 +252,7 @@ export class Router {
244
252
  * @param limits the caps, copied out here, so changing them later has no effect on this table
245
253
  */
246
254
  constructor(limits: RouterLimits) {
247
- routerLimit("maxPatternBytes", limits.maxPatternBytes, 1, 65536)
248
- routerLimit("maxSegments", limits.maxSegments, 1, 256)
249
- routerLimit("maxParams", limits.maxParams, 0, 256)
250
- routerLimit("maxRoutes", limits.maxRoutes, 1, 1048576)
251
- routerLimit("maxMethods", limits.maxMethods, 1, 64)
255
+ routerCheckLimits(limits)
252
256
  this.maxPatternBytes = limits.maxPatternBytes
253
257
  this.maxSegments = limits.maxSegments
254
258
  this.maxParams = limits.maxParams
@@ -275,7 +279,7 @@ export class Router {
275
279
  this.methodStart = []
276
280
  this.methodEnd = []
277
281
  this.methodKey = []
278
- // routerLimit holds both to at most 256; the clamps restate that where the
282
+ // routerCheckLimits holds both to at most 256; the clamps restate that where the
279
283
  // prover reads it, so each length fits a `u16` and its sum cannot overflow.
280
284
  let maxSegments: i32 = limits.maxSegments
281
285
  if (maxSegments > 256) {
@@ -322,6 +326,19 @@ export class Router {
322
326
  return 0
323
327
  }
324
328
 
329
+ /**
330
+ * `paramCount` without its panic: a route the table does not hold answers
331
+ * -1, for a caller whose route came from somewhere it does not trust.
332
+ * @param route a route index `add` answered
333
+ * @returns how many pairs `match` writes to `params` for that route, or -1 for a route the table does not hold
334
+ */
335
+ tryParamCount(route: i32): i32 {
336
+ if (route >= 0 && route < this.routes() && route < toI32(this.routeParams.length)) {
337
+ return this.routeParams[route]
338
+ }
339
+ return -1
340
+ }
341
+
325
342
  /**
326
343
  * How many routes the exact-path table holds: the routes `match` answers
327
344
  * without the tree. Every route with no parameter and no `*` is one, but for
@@ -775,12 +792,53 @@ export class Router {
775
792
  * @returns the matched route's index, or `ROUTER_NOT_FOUND`, `ROUTER_METHOD_NOT_ALLOWED` or `ROUTER_TOO_MANY_PARAMS`
776
793
  */
777
794
  match(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
795
+ const route: i32 = this.lookup(method, mStart, mEnd, target, tStart, tEnd, params)
796
+ if (route === ROUTER_NOT_FOUND && this.outside) {
797
+ this.outside = false
798
+ routerCheckRanges(method, mStart, mEnd, target, tStart, tEnd)
799
+ }
800
+ return route
801
+ }
802
+
803
+ /**
804
+ * `match` without its panic: a method or target range outside its array
805
+ * answers `ROUTER_NOT_FOUND`, `params` untouched, as a target that is not
806
+ * `/`-rooted does. For a server whose ranges come from a parser it does not
807
+ * want a mistake in to stop the process.
808
+ * @param method the buffer holding the request's method
809
+ * @param mStart where the method starts in `method`
810
+ * @param mEnd where the method ends in `method`
811
+ * @param target the buffer holding the request target, which may be the same buffer as `method`
812
+ * @param tStart where the target starts in `target`
813
+ * @param tEnd where the target ends in `target`, the query included
814
+ * @param params the caller's scratch for the parameter pairs, two slots for each pair
815
+ * @returns what `match` answers, or `ROUTER_NOT_FOUND` for a range outside its array
816
+ */
817
+ tryMatch(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
818
+ return this.lookup(method, mStart, mEnd, target, tStart, tEnd, params)
819
+ }
820
+
821
+ /**
822
+ * `tryMatch`, and `match` before its panic: a range outside its array is
823
+ * `ROUTER_NOT_FOUND`, tested once, in `enter`. `match` tests the ranges
824
+ * again only when `enter` found one outside, so the found and not-found
825
+ * answers pay for one test.
826
+ */
827
+ private lookup(
828
+ method: u8[],
829
+ mStart: i32,
830
+ mEnd: i32,
831
+ target: u8[],
832
+ tStart: i32,
833
+ tEnd: i32,
834
+ params: i32[]
835
+ ): i32 {
778
836
  if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
779
837
  return ROUTER_NOT_FOUND
780
838
  }
781
839
  // A static route's path is never longer than the longest one: a target
782
840
  // past that, query and all, is the tree's without a byte of it read here.
783
- // `enter` panics on a range outside `target`, so this never holds; a proof
841
+ // `enter` refuses a range outside `target`, so this never holds; a proof
784
842
  // does not cross a call.
785
843
  if (tStart < 0 || tEnd < 0) {
786
844
  return ROUTER_NOT_FOUND
@@ -812,6 +870,7 @@ export class Router {
812
870
  */
813
871
  matchTree(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
814
872
  if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
873
+ routerCheckRanges(method, mStart, mEnd, target, tStart, tEnd)
815
874
  return ROUTER_NOT_FOUND
816
875
  }
817
876
  return this.descend(target, tStart, tEnd, params)
@@ -819,19 +878,19 @@ export class Router {
819
878
 
820
879
  /**
821
880
  * The start of every lookup: the range checks, and `wanted` set to the
822
- * method's index, or -1 for one the table has never seen. False for a target
823
- * that is not `/`-rooted, which is `ROUTER_NOT_FOUND` with `pathEnd` set.
881
+ * method's index, or -1 for one the table has never seen. False for a range
882
+ * outside its array, which `match` then panics for, and for a target that
883
+ * is not `/`-rooted, which is `ROUTER_NOT_FOUND` with `pathEnd` set.
824
884
  */
825
885
  private enter(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32): boolean {
886
+ // The returns are what tell the prover that both ranges are inside their
887
+ // arrays from here on.
826
888
  if (mStart < 0 || mEnd < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
827
- routerOutside("method", mStart, mEnd, toI32(method.length))
828
- // Not reached, as below.
889
+ this.outside = true
829
890
  return false
830
891
  }
831
892
  if (tStart < 0 || tEnd < tStart || tEnd > toI32(target.length)) {
832
- routerOutside("target", tStart, tEnd, toI32(target.length))
833
- // Not reached: the helper panics. The return is what tells the prover
834
- // that `tStart .. tEnd` is inside `target` from here on.
893
+ this.outside = true
835
894
  return false
836
895
  }
837
896
  this.visited = 0
@@ -1283,27 +1342,80 @@ const routerSlot = (hash: u32, mask: i32): i32 => {
1283
1342
  }
1284
1343
 
1285
1344
  /**
1286
- * Panics for a `what` range `start` to `end` outside its `length` bytes. Apart
1287
- * from `enter`, so that a lookup holds no string of its own.
1345
+ * Panics for a method or target range outside its array, naming it; returns
1346
+ * for ranges inside. `match` calls it only when `enter` found a range
1347
+ * outside, so no other answer is tested twice. Apart
1348
+ * from the lookup, so that it holds no string of its own.
1288
1349
  */
1289
- const routerOutside = (what: string, start: i32, end: i32, length: i32): void => {
1290
- panic(`Router.match: the ${what} range ${start} to ${end} is outside its ${length} bytes`)
1350
+ const routerCheckRanges = (
1351
+ method: u8[],
1352
+ mStart: i32,
1353
+ mEnd: i32,
1354
+ target: u8[],
1355
+ tStart: i32,
1356
+ tEnd: i32
1357
+ ): void => {
1358
+ if (mStart < 0 || mEnd < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
1359
+ panic(`Router.match: the method range ${mStart} to ${mEnd} is outside its ${toI32(method.length)} bytes`)
1360
+ }
1361
+ if (tStart < 0 || tEnd < tStart || tEnd > toI32(target.length)) {
1362
+ panic(`Router.match: the target range ${tStart} to ${tEnd} is outside its ${toI32(target.length)} bytes`)
1363
+ }
1291
1364
  }
1292
1365
 
1293
1366
  /**
1294
1367
  * Panics for a `route` that is not one of the table's `routes`. Apart from
1295
- * `paramCount`, which a server calls per request, for the reason `routerOutside` is.
1368
+ * `paramCount`, which a server calls per request, for the reason `routerCheckRanges` is.
1296
1369
  */
1297
1370
  const routerNoRoute = (route: i32, routes: i32): void => {
1298
1371
  panic(`Router.paramCount: route ${route} is not in a table of ${routes}`)
1299
1372
  }
1300
1373
 
1301
- /** Panics, naming `RouterLimits.<field>`, unless `value` is in `low .. high`. */
1302
- const routerLimit = (field: string, value: i32, low: i32, high: i32): void => {
1303
- if (value < low || value > high) {
1304
- panic(`RouterLimits.${field}: ${value} is outside ${low} to ${high}`)
1374
+ /** Why `value` is no `RouterLimits.<field>`, naming it, or "" when it is in `low .. high`. */
1375
+ const routerLimitFault = (field: string, value: i32, low: i32, high: i32): string =>
1376
+ value < low || value > high ? `RouterLimits.${field}: ${value} is outside ${low} to ${high}` : ""
1377
+
1378
+ /** Why `limits` holds a field no table could work with, naming the first, or "" when every one is in range. */
1379
+ const routerLimitsFault = (limits: RouterLimits): string => {
1380
+ let fault: string = routerLimitFault("maxPatternBytes", limits.maxPatternBytes, 1, 65536)
1381
+ if (fault.length === 0) {
1382
+ fault = routerLimitFault("maxSegments", limits.maxSegments, 1, 256)
1383
+ }
1384
+ if (fault.length === 0) {
1385
+ fault = routerLimitFault("maxParams", limits.maxParams, 0, 256)
1386
+ }
1387
+ if (fault.length === 0) {
1388
+ fault = routerLimitFault("maxRoutes", limits.maxRoutes, 1, 1048576)
1305
1389
  }
1390
+ if (fault.length === 0) {
1391
+ fault = routerLimitFault("maxMethods", limits.maxMethods, 1, 64)
1392
+ }
1393
+ return fault
1394
+ }
1395
+
1396
+ /** Panics unless every field of `limits` is one a table can work with: a limit outside is the program's mistake. */
1397
+ const routerCheckLimits = (limits: RouterLimits): void => {
1398
+ const fault: string = routerLimitsFault(limits)
1399
+ if (fault.length > 0) {
1400
+ panic(fault)
1401
+ }
1402
+ }
1403
+
1404
+ /**
1405
+ * An empty table, the twin of `Router`'s constructor that refuses a limit
1406
+ * outside the range `RouterLimits` gives it as an `Err` rather than
1407
+ * panicking: for a program that reads its limits from a file or flags.
1408
+ * @param limits the caps, copied out by the table, so changing them later has no effect on it
1409
+ * @returns the table, or the first limit outside its range, named with the range it must lie in
1410
+ */
1411
+ export const routerChecked = (limits: RouterLimits): Result<Router, string> => {
1412
+ const fault: string = routerLimitsFault(limits)
1413
+ if (fault.length > 0) {
1414
+ return Err(fault)
1415
+ }
1416
+ return Ok(new Router(limits))
1306
1417
  }
1418
+
1307
1419
  /** Whether `c` is a token character (RFC 9110 §5.6.2), what a method is made of. */
1308
1420
  const routerTchar = (c: i32): boolean => {
1309
1421
  if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
package/std/net/serve.ts CHANGED
@@ -799,11 +799,11 @@ const serveParamSlots = (maxParams: i32): i32 => {
799
799
  return 2 * maxParams + 2
800
800
  }
801
801
 
802
- /** `a + b` on a clock reading, wrapping as the `u64` sum does rather than trapping. */
803
- const serveLater = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
804
-
805
- /** How long from `from` to `to` on a clock reading: 0 when `to` is not after it. */
806
- const serveSince = (from: i64, to: i64): i64 => (to > from ? toI64(toU64(to) - toU64(from)) : toI64(0))
802
+ /** `x`, or 2^63 - 1 where `x` is larger: the most an `i64` holds. */
803
+ const serveBelow2p63 = (x: u64): u64 => {
804
+ const most: u64 = (toU64(1) << toU64(63)) - toU64(1)
805
+ return x > most ? most : x
806
+ }
807
807
 
808
808
  /** A zeroed array of `n` bytes, empty when `n` is not positive. */
809
809
  const serveU8s = (n: i32): u8[] => {
@@ -2094,3 +2094,57 @@ export const serve = async (
2094
2094
  netClose(listener)
2095
2095
  return done
2096
2096
  }
2097
+
2098
+ /**
2099
+ * `a + b` on a clock reading or a count, held at the `i64` range's ends where
2100
+ * the sum would leave it, which a clock centuries short of 2^63 never makes
2101
+ * it do. Two values of opposite signs add in `i64`, as they cannot leave the
2102
+ * range. Two of one sign add as magnitudes in `u64`, where they are below
2103
+ * 2^64 and so exact: a sum of values at least 0 is the `u64` sum, and a sum
2104
+ * of values below 0 is `-1 - ((-1 - a) + (-1 - b) + 1)`. Each magnitude is
2105
+ * held below 2^63 before it is read back as an `i64`, so the read is exact
2106
+ * too, and the bounds prover sees no sum that can overflow.
2107
+ */
2108
+ const serveLater = (a: i64, b: i64): i64 => {
2109
+ if (a >= 0) {
2110
+ if (b < 0) {
2111
+ return a + b
2112
+ }
2113
+ const sum: u64 = toU64(a) + toU64(b)
2114
+ return toI64(serveBelow2p63(sum))
2115
+ }
2116
+ if (b >= 0) {
2117
+ return a + b
2118
+ }
2119
+ const past: u64 = toU64(-1 - a) + toU64(-1 - b) + toU64(1)
2120
+ const k: i64 = toI64(serveBelow2p63(past))
2121
+ if (k < 0) {
2122
+ // Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
2123
+ return -1
2124
+ }
2125
+ return -1 - k
2126
+ }
2127
+
2128
+ /**
2129
+ * How long from `from` to `to` on a clock reading: 0 when `to` is not after
2130
+ * it. Where the two have one sign the difference fits an `i64`; where `from`
2131
+ * is below 0 and `to` is not, it is the magnitudes' sum, taken in `u64` and
2132
+ * held below 2^63 as `serveLater` holds one.
2133
+ */
2134
+ const serveSince = (from: i64, to: i64): i64 => {
2135
+ if (to <= from) {
2136
+ return 0
2137
+ }
2138
+ if (from >= 0) {
2139
+ if (to >= 0) {
2140
+ return to - from
2141
+ }
2142
+ // Not taken: `to` is after `from`, which is at least 0.
2143
+ return 0
2144
+ }
2145
+ if (to < 0) {
2146
+ return to - from
2147
+ }
2148
+ const sum: u64 = toU64(to) + toU64(-1 - from) + toU64(1)
2149
+ return toI64(serveBelow2p63(sum))
2150
+ }
package/std/net/static.ts CHANGED
@@ -60,7 +60,7 @@
60
60
  */
61
61
 
62
62
  import { realpathSync, statMtimeSync } from "nish:fs"
63
- import { copyBytes } from "nish/bytes"
63
+ import { tryCopyBytes } from "nish/bytes"
64
64
  import { HANDLER_SENT, HttpHandler, HttpRequest } from "nish/net/handler"
65
65
  import { HTTP_DATE_SIZE, httpDateWrite, httpFieldBytes } from "nish/net/http-fields"
66
66
  import { HttpServer, HttpServerRequest } from "nish/net/http-server"
@@ -162,13 +162,34 @@ const staticValue = (r: HttpRequest, i: i32, start: boolean): Result<i32, string
162
162
  const staticByte = (b: u8[], i: i32): i32 => (i >= 0 && i < toI32(b.length) ? toI32(b[i]) : -1)
163
163
 
164
164
  /**
165
- * `a + b` for file offsets and sizes, worked in `u64`, whose wrapping sum
166
- * has the bits of the signed one: they are below 2^31, so it never wraps.
165
+ * `a + b` for file offsets and sizes, or -1 when either is below 0 or the
166
+ * sum is past the last `i64`. Every offset and size it is given is from 0 to
167
+ * `STATIC_MAX_FILE`, 2^31 - 1, as `staticPlan` refuses a larger file, so the
168
+ * -1 is never answered. Two values from 0 to 2^63 - 1 sum in `u64` without
169
+ * wrapping, and the sum is turned back only once it is known to fit.
167
170
  */
168
- const staticAdd64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
171
+ const staticAdd64 = (a: i64, b: i64): i64 => {
172
+ if (a < 0 || b < 0) {
173
+ return -1
174
+ }
175
+ const sum: u64 = toU64(a) + toU64(b)
176
+ const top: u64 = (toU64(1) << toU64(63)) - toU64(1)
177
+ if (sum > top) {
178
+ return -1
179
+ }
180
+ return toI64(sum)
181
+ }
169
182
 
170
- /** `a - b` for the same offsets as `staticAdd64`, the same way. */
171
- const staticSub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
183
+ /**
184
+ * `a - b` for the same offsets as `staticAdd64`, or -1 when either is below
185
+ * 0, which none is: the difference of two such values always fits.
186
+ */
187
+ const staticSub64 = (a: i64, b: i64): i64 => {
188
+ if (a < 0 || b < 0) {
189
+ return -1
190
+ }
191
+ return a - b
192
+ }
172
193
 
173
194
  /**
174
195
  * Writes `v` to `out[i]` and answers `i + 1`, the next write's index; -1,
@@ -213,12 +234,16 @@ const staticCap = (size: i32): i32 => {
213
234
 
214
235
  /** Reads `size` bytes of `fd` from its start into `into`: false when a read fails or `into` is short. */
215
236
  const staticReadAll = (fd: i32, into: u8[], size: i32): boolean => {
216
- if (size > toI32(into.length)) {
217
- return false
218
- }
219
237
  let got: i32 = 0
220
238
  while (got < size) {
221
- const n: i32 = fileReadAt(fd, into, got, staticSub(size, got), toI64(got))
239
+ // `got` only grows from 0, by reads short of `size - got`, so the exit
240
+ // is taken only for a short `into`; it is where the prover reads both.
241
+ const at: i32 = got
242
+ const pos: i64 = toI64(at)
243
+ if (at < 0 || at > size || size > toI32(into.length)) {
244
+ return false
245
+ }
246
+ const n: i32 = fileReadAt(fd, into, at, size - at, pos)
222
247
  if (n <= 0) {
223
248
  return false
224
249
  }
@@ -435,18 +460,27 @@ const staticDate = (b: u8[], start: i32, end: i32): i64 => {
435
460
  } else {
436
461
  year = year - 1
437
462
  }
438
- // The prover does not carry `year` through the branches, so `365 * year`
439
- // is worked in `u64`, whose wrapping keeps the signed bits.
463
+ // The prover does not carry `year` through the branches, and it bounds
464
+ // only values of 0 or more, so `365 * year` is worked from `year + 1`,
465
+ // 0 to 10000 by the bounds above: this test never holds.
440
466
  const y: i32 = year
467
+ const shiftedYear: i32 = staticAdd(y, 1)
468
+ if (shiftedYear < 0 || shiftedYear > 10000) {
469
+ return -1
470
+ }
441
471
  const dayOfYear: i32 = (153 * mp + 2) / 5 + day - 1
442
472
  const leaps: i32 = staticAdd(staticSub(y / 4, y / 100), y / 400)
443
- const days: i32 = staticSub(
444
- staticAdd(staticAdd(toI32(toI64(toU64(y) * toU64(365))), leaps), dayOfYear),
445
- 719468
446
- )
447
- // Worked in `u64`, whose wrapping has the bits of the signed result: `days`
448
- // is within 2^22 of 0, so the seconds never wrap.
449
- return toI64(toU64(days) * toU64(86400) + toU64(3600 * hour + 60 * minute + second))
473
+ const yearDays: i32 = shiftedYear * 365 - 365
474
+ const days: i32 = staticSub(staticAdd(staticAdd(yearDays, leaps), dayOfYear), 719468)
475
+ // A year from -1 to 9999 puts `days` from about -720,000 to 2,940,000, so
476
+ // `days + 1000000` is from 0 to 5000000 and this test never holds; it is
477
+ // what proves the seconds, worked from that shifted count and taken back
478
+ // by 1000000 days' seconds at the end, cannot overflow.
479
+ const shiftedDays: i32 = staticAdd(days, 1000000)
480
+ if (shiftedDays < 0 || shiftedDays > 5000000) {
481
+ return -1
482
+ }
483
+ return toI64(shiftedDays) * 86400 + toI64(3600 * hour + 60 * minute + second) - 86400000000
450
484
  }
451
485
 
452
486
  /**
@@ -457,17 +491,17 @@ const staticNumber = (b: u8[], start: i32, end: i32): i64 => {
457
491
  if (start < 0 || end <= start || staticSub(end, start) > 18) {
458
492
  return -1
459
493
  }
460
- let n: i64 = 0
494
+ // The value stays `u64` throughout: eighteen digits are below 10^18, so
495
+ // the sum never wraps and its value fits an `i64` exactly.
496
+ let n: u64 = 0
461
497
  for (let k: i32 = start; k < end && k < toI32(b.length); k++) {
462
498
  const d: i32 = toI32(b[k]) - 48
463
499
  if (d < 0 || d > 9) {
464
500
  return -1
465
501
  }
466
- // Worked in `u64`, whose wrapping result has the bits of the signed
467
- // one: eighteen digits are below 10^18, so it never wraps.
468
- n = toI64(toU64(n) * toU64(10) + toU64(d))
502
+ n = n * 10 + toU64(d)
469
503
  }
470
- return n
504
+ return toI64(n)
471
505
  }
472
506
 
473
507
  /**
@@ -505,10 +539,12 @@ const staticQ = (b: u8[], start: i32, end: i32): i32 => {
505
539
 
506
540
  /** A new array of `src[0 .. length)`. */
507
541
  const staticCut = (src: u8[], length: i32): u8[] => {
508
- const out: u8[] = []
509
- for (let k: i32 = 0; k < length && k < toI32(src.length); k++) {
510
- out.push(src[k])
542
+ const n: i32 = length < toI32(src.length) ? length : toI32(src.length)
543
+ if (n <= 0) {
544
+ return []
511
545
  }
546
+ const out: u8[] = new Array<u8>(n)
547
+ tryCopyBytes(out, 0, src, 0, n)
512
548
  return out
513
549
  }
514
550
 
@@ -1198,8 +1234,10 @@ export class StaticFiles {
1198
1234
  const last: i64 = this.staticSecond()
1199
1235
  // A date validates only a strong Last-Modified, a second or more before
1200
1236
  // now (RFC 9110 §13.1.5): two versions within one second share the date.
1201
- // `last` is a time in seconds, far below 2^53, so the product never wraps.
1202
- return at >= 0 && at === last && toI64((toU64(last) + toU64(1)) * toU64(1000)) <= toI64(Date.now())
1237
+ // `(last + 1) * 1000 <= now` is `last + 1 <= floor(now / 1000)`, as
1238
+ // `last + 1` is whole, and so `last < now / 1000`; for a negative `now`
1239
+ // both are false, as `last` is at least 0. Dividing cannot overflow.
1240
+ return at >= 0 && at === last && last < toI64(Date.now()) / 1000
1203
1241
  }
1204
1242
 
1205
1243
  /**
@@ -1344,7 +1382,10 @@ export class StaticFiles {
1344
1382
  ) {
1345
1383
  return
1346
1384
  }
1347
- copyBytes(keys[at], 0, path, 0, m)
1385
+ // A key shorter than the path leaves the entry unset, so the file streams.
1386
+ if (!tryCopyBytes(keys[at], 0, path, 0, m)) {
1387
+ return
1388
+ }
1348
1389
  keyLength[at] = m
1349
1390
  keyCoding[at] = this.coding
1350
1391
  sizes[at] = this.size
@@ -1363,9 +1404,16 @@ export class StaticFiles {
1363
1404
  if (this.slot >= 0) {
1364
1405
  return toI32(left)
1365
1406
  }
1366
- const n: i32 = left > toI64(STATIC_CHUNK) ? STATIC_CHUNK : toI32(left)
1367
- const got: i32 = fileReadAt(this.fd, this.chunk, 0, n, pos)
1407
+ // `chunk` is `STATIC_CHUNK` bytes from the constructor; were it shorter,
1408
+ // the file would go in shorter pieces. `pos` is short of `to` in every
1409
+ // caller, so `left` is not negative.
1368
1410
  const chunk: u8[] = this.chunk
1411
+ const room: i32 = toI32(chunk.length)
1412
+ const n: i32 = left > toI64(room) ? room : toI32(left)
1413
+ if (n < 0 || n > toI32(chunk.length)) {
1414
+ return 0
1415
+ }
1416
+ const got: i32 = fileReadAt(this.fd, chunk, 0, n, pos)
1369
1417
  for (let k: i32 = 0; k < n && k < toI32(chunk.length); k++) {
1370
1418
  if (k >= got) {
1371
1419
  chunk[k] = 0
@@ -1403,8 +1451,9 @@ export class StaticFiles {
1403
1451
  // A size below 2^31 (`STATIC_MAX_FILE`) has 10 digits, which
1404
1452
  // `scratch` holds; -1 would send the 416 without the field, which
1405
1453
  // RFC 9110 §15.5.17 only recommends.
1406
- const at: i32 = staticDigits(scratch, 8, this.size, 10)
1407
- copyBytes(scratch, 0, httpFieldBytes("bytes */"), 0, 8)
1454
+ const at: i32 = tryCopyBytes(scratch, 0, httpFieldBytes("bytes */"), 0, 8)
1455
+ ? staticDigits(scratch, 8, this.size, 10)
1456
+ : -1
1408
1457
  if (at >= 0) {
1409
1458
  names.push(httpFieldBytes("content-range"))
1410
1459
  values.push(staticCut(scratch, at))
@@ -1445,11 +1494,12 @@ export class StaticFiles {
1445
1494
  values.push(httpFieldBytes(this.coding === STATIC_BR ? "br" : "gzip"))
1446
1495
  }
1447
1496
  if (status === 206) {
1448
- copyBytes(scratch, 0, httpFieldBytes("bytes "), 0, 6)
1449
1497
  // Three numbers below 2^31 (`STATIC_MAX_FILE`) and the words around
1450
1498
  // them, 38 bytes at most, fit `scratch`'s 64, so the field is never
1451
1499
  // dropped: a 206 without it would break RFC 9110 §15.3.7.
1452
- let at: i32 = staticDigits(scratch, 6, this.from, 10)
1500
+ let at: i32 = tryCopyBytes(scratch, 0, httpFieldBytes("bytes "), 0, 6)
1501
+ ? staticDigits(scratch, 6, this.from, 10)
1502
+ : -1
1453
1503
  at = staticPut(scratch, at, 45)
1454
1504
  at = staticDigits(scratch, at, staticSub64(this.to, 1), 10)
1455
1505
  at = staticPut(scratch, at, 47)