nish 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -44,13 +44,16 @@
44
44
  * Written from RFC 6890, RFC 4291, RFC 6052, RFC 3056, RFC 4380 and the IANA
45
45
  * special-purpose address registries, not ported from another implementation.
46
46
  */
47
- import { copyBytes } from "nish/bytes"
47
+ import { tryCopyBytes } from "nish/bytes"
48
48
  import { DNS_ADDRESS_BYTES } from "nish/net/dns-message"
49
49
  import { dnsParseAddress } from "nish/net/dns-config"
50
50
 
51
51
  /** The bytes of one `permit` entry: sixteen of address, then the prefix length in bits. */
52
52
  const OUTBOUND_PERMIT_BYTES: i32 = 17
53
53
 
54
+ /** The longest address text `permit` reads; an IPv6 address with an IPv4 tail takes 45 bytes. */
55
+ const OUTBOUND_TEXT_MAX: i32 = 64
56
+
54
57
  /** Where an IPv4 address starts in its sixteen-byte mapped form. */
55
58
  const OUTBOUND_V4_AT: i32 = 12
56
59
 
@@ -108,9 +111,12 @@ export class OutboundPolicy {
108
111
  const n: i32 = toI32(cidr.length)
109
112
  const slash: i32 = toI32(cidr.indexOf("/"))
110
113
  const end: i32 = slash >= 0 ? slash : n
111
- const text: u8[] = new Array<u8>(end)
114
+ if (end <= 0 || end > OUTBOUND_TEXT_MAX) {
115
+ return false
116
+ }
117
+ const text: u8[] = new Array<u8>(OUTBOUND_TEXT_MAX)
112
118
  let v4: boolean = true
113
- for (let i: i32 = 0; i < toI32(text.length) && i < n; i += 1) {
119
+ for (let i: i32 = 0; i < end && i < n && i < toI32(text.length); i += 1) {
114
120
  const c: i32 = toI32(cidr.charCodeAt(i))
115
121
  if (c > 127) {
116
122
  return false
@@ -119,64 +125,51 @@ export class OutboundPolicy {
119
125
  text[i] = toU8(c)
120
126
  }
121
127
  const address: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
122
- if (end === 0 || !dnsParseAddress(text, 0, end, address, 0, 0)) {
128
+ if (!dnsParseAddress(text, 0, end, address, 0, 0)) {
123
129
  return false
124
130
  }
125
- const width: i32 = v4 ? 32 : 128
126
- let bits: i32 = width
127
- if (slash >= 0) {
131
+ let bits: i32 = v4 ? 32 : 128
132
+ if (slash >= 0 && slash < n) {
128
133
  bits = outboundDecimal(cidr, slash + 1, n)
129
- if (bits < 0 || bits > width) {
134
+ }
135
+ if (bits < 0) {
136
+ return false
137
+ }
138
+ // An IPv4 prefix is kept in its ::ffff: form, 96 bits longer.
139
+ let stored: i32 = bits
140
+ if (v4) {
141
+ if (bits > 32) {
130
142
  return false
131
143
  }
144
+ stored = bits + 96
145
+ } else if (bits > 128) {
146
+ return false
132
147
  }
133
148
  for (let i: i32 = 0; i < 16 && i < toI32(address.length); i += 1) {
134
149
  this.permitted.push(address[i])
135
150
  }
136
- this.permitted.push(toU8(v4 ? bits + 96 : bits))
151
+ this.permitted.push(toU8(stored))
137
152
  return true
138
153
  }
139
154
  }
140
155
 
141
156
  /**
142
157
  * Whether the sixteen address bytes at `addr[off]` are public, as the module
143
- * comment defines it. Panics when sixteen bytes do not fit there.
158
+ * comment defines it. False when sixteen bytes do not fit there.
144
159
  *
145
160
  * @param addr The array holding the address; an IPv4 one in its `::ffff:` form, as nish:net writes it.
146
161
  * @param off Where the address's sixteen bytes start in `addr`.
147
162
  * @returns True for a public address. An IPv4 one carried by NAT64 or 6to4 is judged as that IPv4 address.
148
163
  */
149
164
  export const outboundAddressPublic = (addr: u8[], off: i32): boolean => {
150
- if (off < 0 || off > toI32(addr.length) - 16) {
151
- panic("outboundAddressPublic: sixteen bytes do not fit at off")
152
- }
153
- const carried: i32 = outboundCarriedV4(addr, off)
154
- if (carried >= 0) {
155
- return outboundV4Public(addr, carried)
156
- }
157
- const b0: i32 = toI32(addr[off])
158
- const b1: i32 = toI32(addr[off + 1])
159
- const b2: i32 = toI32(addr[off + 2])
160
- const b3: i32 = toI32(addr[off + 3])
161
- if ((b0 & 0xe0) !== 0x20) {
162
- // Outside 2000::/3: loopback, unspecified, unique local, link-local, multicast, the rest.
163
- return false
164
- }
165
- if (b0 === 0x20 && b1 === 0x01 && (b2 & 0xfe) === 0) {
166
- // 2001::/23, IETF protocol assignments, Teredo's 2001::/32 among them.
167
- return false
168
- }
169
- if (b0 === 0x20 && b1 === 0x01 && b2 === 0x0d && b3 === 0xb8) {
170
- // 2001:db8::/32, documentation.
171
- return false
172
- }
173
- // 3fff::/20, documentation (RFC 9637).
174
- return !(b0 === 0x3f && b1 === 0xff && (b2 & 0xf0) === 0)
165
+ const a: u8[] | null = outboundSixteen(addr, off)
166
+ return a !== null && outboundPublic(a)
175
167
  }
176
168
 
177
169
  /**
178
170
  * Whether `policy` lets a connection go to the sixteen address bytes at
179
- * `addr[off]`: a prefix it `permit`s, or else a public address.
171
+ * `addr[off]`: a prefix it `permit`s, or else a public address. False when
172
+ * sixteen bytes do not fit there.
180
173
  *
181
174
  * @param policy The policy whose permits are checked first.
182
175
  * @param addr The array holding the address; an IPv4 one in its `::ffff:` form, as nish:net writes it.
@@ -184,24 +177,27 @@ export const outboundAddressPublic = (addr: u8[], off: i32): boolean => {
184
177
  * @returns True when a connection may go there. An IPv4 address carried by NAT64 or 6to4 is judged as that address, by a permit too.
185
178
  */
186
179
  export const outboundAddressAllowed = (policy: OutboundPolicy, addr: u8[], off: i32): boolean => {
187
- if (outboundPermitted(policy, addr, off)) {
180
+ const a: u8[] | null = outboundSixteen(addr, off)
181
+ if (a === null) {
182
+ return false
183
+ }
184
+ if (outboundPermitted(policy, a)) {
188
185
  return true
189
186
  }
190
- const carried: i32 = outboundCarriedV4(addr, off)
187
+ const carried: i32 = outboundCarriedV4(a)
191
188
  if (carried < 0) {
192
- return outboundAddressPublic(addr, off)
189
+ return outboundPublic(a)
193
190
  }
194
- if (!outboundMapped(addr, off) && toI32(policy.permitted.length) > 0) {
191
+ if (!outboundMapped(a) && toI32(policy.permitted.length) > 0) {
195
192
  // A permit names an IPv4 prefix in its ::ffff: form, so try the address a NAT64 or 6to4 one carries in that form too.
196
193
  const mapped: u8[] = new Array<u8>(16)
197
194
  mapped[10] = toU8(255)
198
195
  mapped[11] = toU8(255)
199
- copyBytes(mapped, OUTBOUND_V4_AT, addr, carried, 4)
200
- if (outboundPermitted(policy, mapped, 0)) {
196
+ if (tryCopyBytes(mapped, OUTBOUND_V4_AT, a, carried, 4) && outboundPermitted(policy, mapped)) {
201
197
  return true
202
198
  }
203
199
  }
204
- return outboundV4Public(addr, carried)
200
+ return outboundV4Public(a, carried)
205
201
  }
206
202
 
207
203
  /**
@@ -211,29 +207,35 @@ export const outboundAddressAllowed = (policy: OutboundPolicy, addr: u8[], off:
211
207
  *
212
208
  * @param policy The policy each address is checked against, as `outboundAddressAllowed` checks it.
213
209
  * @param addresses The records, each sixteen address bytes and a two-byte port, reordered in place.
214
- * @param count How many records `addresses` holds from its start; one that does not fit panics.
210
+ * @param count How many records `addresses` holds from its start; records past `addresses`' end are not read, and a negative count keeps none.
215
211
  * @returns How many records were kept, now `addresses`' first ones; 0 when every one was refused.
216
212
  */
217
213
  export const outboundFilter = (policy: OutboundPolicy, addresses: u8[], count: i32): i32 => {
218
- if (count < 0 || count * DNS_ADDRESS_BYTES > toI32(addresses.length)) {
219
- panic("outboundFilter: count records do not fit in addresses")
220
- }
221
- let kept: i32 = 0
222
- for (let i: i32 = 0; i < count; i += 1) {
223
- if (!outboundAddressAllowed(policy, addresses, i * DNS_ADDRESS_BYTES)) {
224
- continue
225
- }
226
- if (i !== kept) {
214
+ const held: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
215
+ // Where the next kept record goes, and where the record checked now starts.
216
+ let keptAt: i32 = 0
217
+ let at: i32 = 0
218
+ for (let i: i32 = 0; i < count && at <= toI32(addresses.length) - DNS_ADDRESS_BYTES; i += 1) {
219
+ if (outboundAddressAllowed(policy, addresses, at)) {
227
220
  // Swap, so the refused record stays in the list for an error to name.
228
- for (let k: i32 = 0; k < DNS_ADDRESS_BYTES; k += 1) {
229
- const held: u8 = addresses[kept * DNS_ADDRESS_BYTES + k]
230
- addresses[kept * DNS_ADDRESS_BYTES + k] = addresses[i * DNS_ADDRESS_BYTES + k]
231
- addresses[i * DNS_ADDRESS_BYTES + k] = held
221
+ // Both records fit (`keptAt` is at most `at`), so no copy can refuse.
222
+ if (
223
+ keptAt !== at &&
224
+ !(
225
+ tryCopyBytes(held, 0, addresses, keptAt, DNS_ADDRESS_BYTES) &&
226
+ tryCopyBytes(addresses, keptAt, addresses, at, DNS_ADDRESS_BYTES) &&
227
+ tryCopyBytes(addresses, at, held, 0, DNS_ADDRESS_BYTES)
228
+ )
229
+ ) {
230
+ break
232
231
  }
232
+ // At most `at`, which is at most the length less a record: unsigned
233
+ // arithmetic only so the prover need not relate the two.
234
+ keptAt = toI32(toU32(keptAt) + toU32(DNS_ADDRESS_BYTES))
233
235
  }
234
- kept += 1
236
+ at = toI32(toU32(at) + toU32(DNS_ADDRESS_BYTES))
235
237
  }
236
- return kept
238
+ return keptAt / DNS_ADDRESS_BYTES
237
239
  }
238
240
 
239
241
  /**
@@ -259,7 +261,7 @@ export const outboundHostAllowed = (policy: OutboundPolicy, host: string): boole
259
261
  if (
260
262
  !literal &&
261
263
  entry.startsWith(".") &&
262
- (name.endsWith(entry) || (toI32(name.length) + 1 === toI32(entry.length) && entry.endsWith(name)))
264
+ (name.endsWith(entry) || (toI32(entry.length) - 1 === toI32(name.length) && entry.endsWith(name)))
263
265
  ) {
264
266
  return true
265
267
  }
@@ -272,128 +274,190 @@ export const outboundHostAllowed = (policy: OutboundPolicy, host: string): boole
272
274
  *
273
275
  * @param addr The array holding the address.
274
276
  * @param off Where the address's sixteen bytes start in `addr`.
275
- * @returns Dotted decimal for an IPv4 address in its `::ffff:` form, otherwise all eight lower-case hex groups with no `::` shortening.
277
+ * @returns Dotted decimal for an IPv4 address in its `::ffff:` form, otherwise all eight lower-case hex groups with no `::` shortening; empty when sixteen bytes do not fit there.
276
278
  */
277
279
  export const outboundAddressText = (addr: u8[], off: i32): string => {
278
- if (outboundMapped(addr, off)) {
279
- const at: i32 = off + OUTBOUND_V4_AT
280
- return `${addr[at]}.${addr[at + 1]}.${addr[at + 2]}.${addr[at + 3]}`
280
+ const a: u8[] | null = outboundSixteen(addr, off)
281
+ return a === null ? "" : outboundText(a)
282
+ }
283
+
284
+ /** The address in `a`, `outboundSixteen`'s, as `outboundAddressText` writes it. */
285
+ const outboundText = (a: u8[]): string => {
286
+ const mapped: boolean = outboundMapped(a)
287
+ if (toI32(a.length) < 16) {
288
+ return ""
281
289
  }
282
- // Eight groups of at most four digits and seven colons.
283
- const out: u8[] = new Array<u8>(39)
284
- let at: i32 = 0
285
- for (let g: i32 = 0; g < 8; g += 1) {
286
- const v: i32 = toI32(addr[off + 2 * g]) * 256 + toI32(addr[off + 2 * g + 1])
287
- if (g > 0 && at < toI32(out.length)) {
288
- out[at] = toU8(58)
289
- at += 1
290
+ if (mapped) {
291
+ return `${a[12]}.${a[13]}.${a[14]}.${a[15]}`
292
+ }
293
+ const parts: string[] = []
294
+ let high: u8 = toU8(0)
295
+ for (let i: i32 = 0; i < 16 && i < toI32(a.length); i += 1) {
296
+ if ((i & 1) === 0) {
297
+ high = a[i]
298
+ continue
299
+ }
300
+ const v: i32 = toI32(high) * 256 + toI32(a[i])
301
+ if (i > 1) {
302
+ parts.push(":")
290
303
  }
291
304
  for (let shift: i32 = 12; shift >= 0; shift -= 4) {
292
305
  const d: i32 = (v >> shift) & 15
293
306
  if (v >> shift !== 0 || shift === 0) {
294
- if (at < toI32(out.length)) {
295
- out[at] = toU8(d < 10 ? 48 + d : 87 + d)
296
- at += 1
297
- }
307
+ parts.push(String.fromCharCode(toU8(d < 10 ? 48 + d : 87 + d)))
298
308
  }
299
309
  }
300
310
  }
301
- return String.fromBytes(out, 0, at)
311
+ return parts.join("")
302
312
  }
303
313
 
304
314
  // --- helpers -----------------------------------------------------------------
315
+ // Each takes an address as an array of its own, `outboundSixteen`'s, so its
316
+ // reads are proven against that array's length rather than a caller's offset.
317
+
318
+ /** The sixteen address bytes at `addr[off]` in an array of their own, or null when they do not fit there. */
319
+ const outboundSixteen = (addr: u8[], off: i32): u8[] | null => {
320
+ const a: u8[] = new Array<u8>(16)
321
+ return tryCopyBytes(a, 0, addr, off, 16) ? a : null
322
+ }
305
323
 
306
- /** Whether one of `policy`'s permits covers the sixteen address bytes at `addr[off]`. */
307
- const outboundPermitted = (policy: OutboundPolicy, addr: u8[], off: i32): boolean => {
324
+ /** Whether the address in `a`, `outboundSixteen`'s, is public, as `outboundAddressPublic` answers. */
325
+ const outboundPublic = (a: u8[]): boolean => {
326
+ if (toI32(a.length) < 16) {
327
+ return false
328
+ }
329
+ const carried: i32 = outboundCarriedV4(a)
330
+ if (carried >= 0) {
331
+ return outboundV4Public(a, carried)
332
+ }
333
+ const b0: i32 = toI32(a[0])
334
+ const b1: i32 = toI32(a[1])
335
+ const b2: i32 = toI32(a[2])
336
+ const b3: i32 = toI32(a[3])
337
+ if ((b0 & 0xe0) !== 0x20) {
338
+ // Outside 2000::/3: loopback, unspecified, unique local, link-local, multicast, the rest.
339
+ return false
340
+ }
341
+ if (b0 === 0x20 && b1 === 0x01 && (b2 & 0xfe) === 0) {
342
+ // 2001::/23, IETF protocol assignments, Teredo's 2001::/32 among them.
343
+ return false
344
+ }
345
+ if (b0 === 0x20 && b1 === 0x01 && b2 === 0x0d && b3 === 0xb8) {
346
+ // 2001:db8::/32, documentation.
347
+ return false
348
+ }
349
+ // 3fff::/20, documentation (RFC 9637).
350
+ return !(b0 === 0x3f && b1 === 0xff && (b2 & 0xf0) === 0)
351
+ }
352
+
353
+ /** Whether one of `policy`'s permits covers the address in `a`. */
354
+ const outboundPermitted = (policy: OutboundPolicy, a: u8[]): boolean => {
308
355
  const p: u8[] = policy.permitted
309
- for (let at: i32 = 0; at + OUTBOUND_PERMIT_BYTES <= toI32(p.length); at += OUTBOUND_PERMIT_BYTES) {
310
- const bitsAt: i32 = at + 16
311
- if (bitsAt >= 0 && bitsAt < toI32(p.length) && outboundPrefixMatch(p, at, toI32(p[bitsAt]), addr, off)) {
356
+ const entry: u8[] = new Array<u8>(OUTBOUND_PERMIT_BYTES)
357
+ let at: i32 = 0
358
+ while (at >= 0 && at <= toI32(p.length) - OUTBOUND_PERMIT_BYTES) {
359
+ if (tryCopyBytes(entry, 0, p, at, OUTBOUND_PERMIT_BYTES) && outboundPrefixMatch(entry, a)) {
312
360
  return true
313
361
  }
362
+ // At most the length less a permit before, so the sum fits.
363
+ at = toI32(toU32(at) + toU32(OUTBOUND_PERMIT_BYTES))
314
364
  }
315
365
  return false
316
366
  }
317
367
 
318
- /** Whether the sixteen address bytes at `addr[off]` are an IPv4-mapped address, `::ffff:0:0/96`. */
319
- const outboundMapped = (addr: u8[], off: i32): boolean =>
320
- outboundZeros(addr, off, 10) && addr[off + 10] === toU8(255) && addr[off + 11] === toU8(255)
368
+ /** Whether the address in `a` is an IPv4-mapped address, `::ffff:0:0/96`. */
369
+ const outboundMapped = (a: u8[]): boolean =>
370
+ toI32(a.length) >= 16 && outboundZeros(a, 0, 10) && a[10] === toU8(255) && a[11] === toU8(255)
321
371
 
322
372
  /**
323
- * Where the IPv4 address that the sixteen address bytes at `addr[off]` carry
324
- * starts, or -1 for none: `::ffff:a.b.c.d`, how nish:net writes every IPv4
325
- * address; `64:ff9b::/96`, the NAT64 well-known prefix (RFC 6052), in its
326
- * last four bytes; `2002::/16`, 6to4 (RFC 3056), in the four after the prefix.
373
+ * Where the IPv4 address that the address in `a` carries starts, or -1 for
374
+ * none: `::ffff:a.b.c.d`, how nish:net writes every IPv4 address;
375
+ * `64:ff9b::/96`, the NAT64 well-known prefix (RFC 6052), in its last four
376
+ * bytes; `2002::/16`, 6to4 (RFC 3056), in the four after the prefix.
327
377
  */
328
- const outboundCarriedV4 = (addr: u8[], off: i32): i32 => {
329
- if (outboundMapped(addr, off)) {
330
- return off + OUTBOUND_V4_AT
331
- }
332
- const b0: i32 = toI32(addr[off])
333
- const b1: i32 = toI32(addr[off + 1])
334
- if (
335
- b0 === 0 &&
336
- b1 === 0x64 &&
337
- addr[off + 2] === toU8(0xff) &&
338
- addr[off + 3] === toU8(0x9b) &&
339
- outboundZeros(addr, off + 4, 8)
340
- ) {
341
- return off + OUTBOUND_V4_AT
378
+ const outboundCarriedV4 = (a: u8[]): i32 => {
379
+ if (toI32(a.length) < 16) {
380
+ return -1
381
+ }
382
+ if (outboundMapped(a)) {
383
+ return OUTBOUND_V4_AT
384
+ }
385
+ const b0: i32 = toI32(a[0])
386
+ const b1: i32 = toI32(a[1])
387
+ if (b0 === 0 && b1 === 0x64 && a[2] === toU8(0xff) && a[3] === toU8(0x9b) && outboundZeros(a, 4, 12)) {
388
+ return OUTBOUND_V4_AT
342
389
  }
343
390
  if (b0 === 0x20 && b1 === 0x02) {
344
- return off + 2
391
+ return 2
345
392
  }
346
393
  return -1
347
394
  }
348
395
 
349
- /** Whether the IPv4 address in the four bytes at `addr[at]` is public. */
350
- const outboundV4Public = (addr: u8[], at: i32): boolean => {
351
- const a: i32 = toI32(addr[at])
352
- const b: i32 = toI32(addr[at + 1])
353
- const c: i32 = toI32(addr[at + 2])
354
- const d: i32 = toI32(addr[at + 3])
396
+ /** Whether the IPv4 address in the four bytes at `a[at]` is public; false when four do not fit there. */
397
+ const outboundV4Public = (a: u8[], at: i32): boolean => {
398
+ if (at < 0 || at > toI32(a.length) - 4) {
399
+ return false
400
+ }
401
+ const x: i32 = toI32(a[at])
402
+ const b: i32 = toI32(a[at + 1])
403
+ const c: i32 = toI32(a[at + 2])
404
+ const d: i32 = toI32(a[at + 3])
355
405
  return !(
356
- a === 0 ||
357
- a === 10 ||
358
- a === 127 ||
359
- a >= 224 ||
360
- (a === 100 && b >= 64 && b <= 127) ||
361
- (a === 169 && b === 254) ||
362
- (a === 172 && b >= 16 && b <= 31) ||
363
- (a === 192 && b === 0 && (c === 0 || c === 2)) ||
364
- (a === 192 && b === 88 && c === 99) ||
365
- (a === 192 && b === 168) ||
366
- (a === 198 && (b === 18 || b === 19)) ||
367
- (a === 198 && b === 51 && c === 100) ||
368
- (a === 203 && b === 0 && c === 113) ||
369
- (a === 168 && b === 63 && c === 129 && d === 16)
406
+ x === 0 ||
407
+ x === 10 ||
408
+ x === 127 ||
409
+ x >= 224 ||
410
+ (x === 100 && b >= 64 && b <= 127) ||
411
+ (x === 169 && b === 254) ||
412
+ (x === 172 && b >= 16 && b <= 31) ||
413
+ (x === 192 && b === 0 && (c === 0 || c === 2)) ||
414
+ (x === 192 && b === 88 && c === 99) ||
415
+ (x === 192 && b === 168) ||
416
+ (x === 198 && (b === 18 || b === 19)) ||
417
+ (x === 198 && b === 51 && c === 100) ||
418
+ (x === 203 && b === 0 && c === 113) ||
419
+ (x === 168 && b === 63 && c === 129 && d === 16)
370
420
  )
371
421
  }
372
422
 
373
- /** Whether the `n` bytes at `b[at]` are all zero. */
374
- const outboundZeros = (b: u8[], at: i32, n: i32): boolean => {
375
- for (let i: i32 = 0; i < n; i += 1) {
376
- if (b[at + i] !== toU8(0)) {
423
+ /** Whether the bytes `b[from..to)` are all zero; a range past `b`'s end is not. */
424
+ const outboundZeros = (b: u8[], from: i32, to: i32): boolean => {
425
+ if (from < 0 || to > toI32(b.length)) {
426
+ return false
427
+ }
428
+ for (let i: i32 = from; i < to; i += 1) {
429
+ if (b[i] !== toU8(0)) {
377
430
  return false
378
431
  }
379
432
  }
380
433
  return true
381
434
  }
382
435
 
383
- /** Whether the first `bits` bits of `p[at]` and `addr[off]` agree. */
384
- const outboundPrefixMatch = (p: u8[], at: i32, bits: i32, addr: u8[], off: i32): boolean => {
436
+ /**
437
+ * Whether the address in `a` is under the permit `entry`, one
438
+ * `OUTBOUND_PERMIT_BYTES` record: its first prefix-length bits agree with
439
+ * the entry's sixteen address bytes. Never for a length past 128.
440
+ */
441
+ const outboundPrefixMatch = (entry: u8[], a: u8[]): boolean => {
442
+ if (toI32(entry.length) <= 16 || toI32(a.length) < 16) {
443
+ return false
444
+ }
445
+ const bits: i32 = toI32(entry[16])
446
+ if (bits > 128) {
447
+ return false
448
+ }
385
449
  const whole: i32 = bits >> 3
386
- for (let i: i32 = 0; i < whole; i += 1) {
387
- if (p[at + i] !== addr[off + i]) {
450
+ for (let i: i32 = 0; i < whole && i < 16; i += 1) {
451
+ if (entry[i] !== a[i]) {
388
452
  return false
389
453
  }
390
454
  }
391
455
  const rest: i32 = bits & 7
392
- if (rest === 0) {
456
+ if (rest === 0 || whole >= 16) {
393
457
  return true
394
458
  }
395
459
  const mask: i32 = (toI32(0xff) << (8 - rest)) & toI32(0xff)
396
- return (toI32(p[at + whole]) & mask) === (toI32(addr[off + whole]) & mask)
460
+ return (toI32(entry[whole]) & mask) === (toI32(a[whole]) & mask)
397
461
  }
398
462
 
399
463
  /** `s[from..to)` as a decimal of one to three digits, or -1. */
@@ -401,15 +465,17 @@ const outboundDecimal = (s: string, from: i32, to: i32): i32 => {
401
465
  if (to <= from || to - from > 3) {
402
466
  return -1
403
467
  }
404
- let v: i32 = 0
468
+ // Unsigned, so the prover need not carry `v`'s bound round the loop: three
469
+ // digits make at most 999.
470
+ let v: u32 = toU32(0)
405
471
  for (let i: i32 = from; i < to; i += 1) {
406
472
  const c: i32 = toI32(s.charCodeAt(i))
407
473
  if (c < 48 || c > 57) {
408
474
  return -1
409
475
  }
410
- v = v * 10 + c - 48
476
+ v = v * toU32(10) + toU32(c - 48)
411
477
  }
412
- return v
478
+ return toI32(v)
413
479
  }
414
480
 
415
481
  /** Whether `name` is an address literal: it holds a colon, or its last label is all digits. */
@@ -427,13 +493,12 @@ const outboundIsLiteral = (name: string): boolean => {
427
493
 
428
494
  /** `s` with A to Z lower-cased. */
429
495
  const outboundLower = (s: string): string => {
430
- const n: i32 = toI32(s.length)
431
- const out: u8[] = new Array<u8>(n)
432
- for (let i: i32 = 0; i < toI32(out.length) && i < n; i += 1) {
496
+ const parts: string[] = []
497
+ for (let i: i32 = 0; i < toI32(s.length); i += 1) {
433
498
  const c: i32 = toI32(s.charCodeAt(i))
434
- out[i] = c >= 65 && c <= 90 ? toU8(c + 32) : toU8(c)
499
+ parts.push(String.fromCharCode(toU8(c >= 65 && c <= 90 ? c + 32 : c)))
435
500
  }
436
- return String.fromBytes(out, 0, n)
501
+ return parts.join("")
437
502
  }
438
503
 
439
504
  /** `s` without one trailing dot. */