@amritk/nish-aarch64-linux 0.44.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
@@ -57,7 +57,7 @@
57
57
  * `dnsParseAddress` writes from a literal. The record of what this module
58
58
  * refuses and why is `docs/security/dns.md`.
59
59
  */
60
- import { setU16BE, skipBlank } from "nish/bytes"
60
+ import { setU16BE } from "nish/bytes"
61
61
  import { DNS_ADDRESS_BYTES, DNS_TYPE_A, DNS_TYPE_AAAA } from "nish/net/dns-message"
62
62
 
63
63
  /** How many `nameserver` lines are kept; glibc's MAXNS. */
@@ -91,6 +91,8 @@ export const DNS_HOSTS_MAX_NAMES: i32 = 1024
91
91
  export const DNS_HOSTS_MAX_NAME_BYTES: i32 = 16384
92
92
  /** The longest hosts name kept, the longest a DNS name can be. */
93
93
  export const DNS_HOSTS_MAX_NAME: i32 = 253
94
+ // The parsers below compare against these caps as literals (each marked with its name):
95
+ // the bounds prover takes a bound from a literal in a comparison, not from a named constant.
94
96
 
95
97
  const DNS_TAB: i32 = 9
96
98
  const DNS_NEWLINE: i32 = 10
@@ -142,7 +144,7 @@ export class DnsConfig {
142
144
  * seconds, 2).
143
145
  */
144
146
  constructor() {
145
- this.servers = new Array<u8>(DNS_MAX_SERVERS * DNS_ADDRESS_BYTES)
147
+ this.servers = new Array<u8>(54)
146
148
  this.scratch = new Array<u8>(16)
147
149
  this.search = new Array<u8>(DNS_MAX_SEARCH_BYTES)
148
150
  this.searchStart = new Array<i32>(DNS_MAX_SEARCH)
@@ -200,7 +202,7 @@ export class DnsHosts {
200
202
  * `DNS_HOSTS_MAX_NAMES` names, for `dnsParseHosts` to fill.
201
203
  */
202
204
  constructor() {
203
- this.addresses = new Array<u8>(DNS_HOSTS_MAX_ENTRIES * DNS_ADDRESS_BYTES)
205
+ this.addresses = new Array<u8>(4608)
204
206
  this.names = new Array<u8>(DNS_HOSTS_MAX_NAME_BYTES)
205
207
  this.scratch = new Array<u8>(16)
206
208
  this.nameStart = new Array<i32>(DNS_HOSTS_MAX_NAMES)
@@ -209,14 +211,16 @@ export class DnsHosts {
209
211
  }
210
212
  }
211
213
 
212
- /** Panics unless `bytes[off, off + len)` lies inside `bytes`. */
213
- const dnsConfigCheckWindow = (bytes: u8[], off: i32, len: i32, who: string): void => {
214
- const size: i32 = toI32(bytes.length)
215
- if (off < 0 || len < 0 || off > size || len > size - off) {
214
+ /** Panics unless `[from, to)` lies inside an array of `size` elements. */
215
+ const dnsConfigCheckWindow = (size: i32, from: i32, to: i32, who: string): void => {
216
+ if (from < 0 || to < from || to > size) {
216
217
  panic(who + ": the window is outside the array")
217
218
  }
218
219
  }
219
220
 
221
+ /** `n + 1`, held at the i32 maximum: a count here moves once per byte of a file at most, so it never gets there. */
222
+ const dnsCount = (n: i32): i32 => (n > 2147483646 ? n : n + 1)
223
+
220
224
  /** Whether `b` separates words on a line. */
221
225
  const dnsIsBlank = (b: i32): boolean => b === DNS_SPACE || b === DNS_TAB || b === DNS_RETURN
222
226
 
@@ -243,12 +247,16 @@ const dnsParseIpv4 = (b: u8[], from: i32, to: i32, out: u8[], at: i32): boolean
243
247
  if (from < 0 || from > to || to > toI32(b.length)) {
244
248
  panic("dnsParseIpv4: the window is outside the array")
245
249
  }
250
+ if (at < 0 || at > toI32(out.length) - 4) {
251
+ panic("dnsParseIpv4: out has no room for four bytes from at")
252
+ }
246
253
  let i: i32 = from
247
254
  let part: i32 = 0
248
255
  while (part < 4) {
249
256
  const start: i32 = i
250
257
  let value: i32 = 0
251
- while (i < to && i - start < 3 && toI32(b[i]) >= 48 && toI32(b[i]) <= 57) {
258
+ // At most three digits, so `value` is below 100 before each one is added.
259
+ while (i < to && i - start < 3 && value >= 0 && value < 100 && toI32(b[i]) >= 48 && toI32(b[i]) <= 57) {
252
260
  value = value * 10 + toI32(b[i]) - 48
253
261
  i += 1
254
262
  }
@@ -270,60 +278,67 @@ const dnsParseIpv4 = (b: u8[], from: i32, to: i32, out: u8[], at: i32): boolean
270
278
  }
271
279
 
272
280
  /**
273
- * Writes the IPv6 literal `b[from, to)` as sixteen bytes at `out[at]`, and
281
+ * Writes the IPv6 literal `b[from, to)` as sixteen bytes at `out[0]`, and
274
282
  * answers whether it was one: eight groups of one to four hex digits, at most
275
283
  * one `::` standing for one or more zero groups, and a dotted quad allowed in
276
284
  * place of the last two groups.
277
285
  */
278
- const dnsParseIpv6 = (b: u8[], from: i32, to: i32, out: u8[], at: i32): boolean => {
279
- if (from < 0 || from > to || to > toI32(b.length)) {
286
+ const dnsParseIpv6 = (b: u8[], from: i32, to: i32, out: u8[]): boolean => {
287
+ if (from < 0 || to < 0 || from > to || to > toI32(b.length)) {
280
288
  panic("dnsParseIpv6: the window is outside the array")
281
289
  }
282
290
  let i: i32 = from
283
291
  let groups: i32 = 0
284
292
  let gap: i32 = -1
285
- if (to - i >= 2 && toI32(b[i]) === DNS_COLON && toI32(b[i + 1]) === DNS_COLON) {
293
+ if (i < to - 1 && toI32(b[i]) === DNS_COLON && toI32(b[i + 1]) === DNS_COLON) {
286
294
  gap = 0
287
295
  i += 2
288
296
  }
289
297
  while (i < to) {
290
- if (groups === 8 || i < 0 || i >= toI32(b.length)) {
298
+ if (groups > 7) {
291
299
  return false
292
300
  }
293
- let j: i32 = i
301
+ const start: i32 = i
294
302
  let value: i32 = 0
295
- while (j < to && j - i < 4 && dnsHexValue(toI32(b[j])) >= 0) {
296
- value = value * 16 + dnsHexValue(toI32(b[j]))
297
- j += 1
303
+ while (i < to && i - start < 4) {
304
+ const d: i32 = dnsHexValue(toI32(b[i]))
305
+ // At most four digits, so `value` is at most 0xfff before each one is added.
306
+ if (d < 0 || d > 15 || value < 0 || value > 4095) {
307
+ break
308
+ }
309
+ value = value * 16 + d
310
+ i += 1
298
311
  }
299
- if (j < to && toI32(b[j]) === DNS_DOT) {
300
- if (groups > 6 || !dnsParseIpv4(b, i, to, out, at + groups * 2)) {
312
+ if (i < to && toI32(b[i]) === DNS_DOT) {
313
+ if (groups > 6 || !dnsParseIpv4(b, start, to, out, groups * 2)) {
301
314
  return false
302
315
  }
303
316
  groups += 2
304
- i = to
305
- } else {
306
- if (j === i) {
317
+ break
318
+ }
319
+ if (i === start) {
320
+ return false
321
+ }
322
+ if (toI32(out.length) < 16) {
323
+ panic("dnsParseIpv6: out is shorter than 16 bytes")
324
+ }
325
+ const g: i32 = groups * 2
326
+ out[g] = toU8(value >> 8)
327
+ out[g + 1] = toU8(value & 255)
328
+ groups += 1
329
+ if (i < to) {
330
+ if (toI32(b[i]) !== DNS_COLON) {
307
331
  return false
308
332
  }
309
- out[at + groups * 2] = toU8(value >> 8)
310
- out[at + groups * 2 + 1] = toU8(value & 255)
311
- groups += 1
312
- i = j
313
- if (i < to) {
314
- if (i < 0 || i >= toI32(b.length) || toI32(b[i]) !== DNS_COLON) {
333
+ i += 1
334
+ if (i < to && toI32(b[i]) === DNS_COLON) {
335
+ if (gap >= 0) {
315
336
  return false
316
337
  }
338
+ gap = groups
317
339
  i += 1
318
- if (i < to && toI32(b[i]) === DNS_COLON) {
319
- if (gap >= 0) {
320
- return false
321
- }
322
- gap = groups
323
- i += 1
324
- } else if (i === to) {
325
- return false
326
- }
340
+ } else if (i === to) {
341
+ return false
327
342
  }
328
343
  }
329
344
  }
@@ -333,18 +348,23 @@ const dnsParseIpv6 = (b: u8[], from: i32, to: i32, out: u8[], at: i32): boolean
333
348
  if (groups > 7) {
334
349
  return false
335
350
  }
336
- // The groups after the gap move to the end, and the gap fills with zeros.
351
+ if (toI32(out.length) < 16) {
352
+ panic("dnsParseIpv6: out is shorter than 16 bytes")
353
+ }
354
+ // The groups after the gap move to the end, and the gap fills with zeros:
355
+ // walking down, byte `k` takes byte `k - shift` while that is past the gap's
356
+ // start, is zero while `k` itself is, and is kept before it.
337
357
  const shift: i32 = (8 - groups) * 2
338
- let k: i32 = groups * 2 - 1
339
- while (k >= gap * 2) {
340
- out[at + k + shift] = out[at + k]
358
+ let k: i32 = 15
359
+ while (k >= 0) {
360
+ const src: i32 = k - shift
361
+ if (src >= 0 && src >> 1 >= gap) {
362
+ out[k] = out[src]
363
+ } else if (k >> 1 >= gap) {
364
+ out[k] = toU8(0)
365
+ }
341
366
  k -= 1
342
367
  }
343
- k = gap * 2
344
- while (k < gap * 2 + shift) {
345
- out[at + k] = toU8(0)
346
- k += 1
347
- }
348
368
  return true
349
369
  }
350
370
 
@@ -368,20 +388,27 @@ const dnsParseIpv6 = (b: u8[], from: i32, to: i32, out: u8[], at: i32): boolean
368
388
  * @returns True when the window was an IPv4 or IPv6 literal and `out` now holds it.
369
389
  */
370
390
  export const dnsParseAddress = (b: u8[], off: i32, len: i32, out: u8[], at: i32, port: i32): boolean =>
371
- dnsParseAddressVia(b, off, len, out, at, port, new Array<u8>(16))
391
+ // A negative `len`, or an end past the i32 range, makes `to` fall below `off`, which panics there.
392
+ dnsParseAddressVia(b, off, toI32(toI64(off) + toI64(len)), out, at, port, new Array<u8>(16))
372
393
 
373
- /** `dnsParseAddress`, parsing into `parsed`, sixteen bytes the caller owns, so it allocates nothing. */
394
+ /**
395
+ * `dnsParseAddress` for the window `b[from, to)`, parsing into `parsed`,
396
+ * sixteen bytes the caller owns, so it allocates nothing.
397
+ */
374
398
  const dnsParseAddressVia = (
375
399
  b: u8[],
376
- off: i32,
377
- len: i32,
400
+ from: i32,
401
+ to: i32,
378
402
  out: u8[],
379
403
  at: i32,
380
404
  port: i32,
381
405
  parsed: u8[]
382
406
  ): boolean => {
383
- dnsConfigCheckWindow(b, off, len, "dnsParseAddress")
384
- if (at < 0 || at > toI32(out.length) - DNS_ADDRESS_BYTES) {
407
+ if (from < 0 || to < from || to > toI32(b.length)) {
408
+ panic("dnsParseAddress: the window is outside the array")
409
+ }
410
+ const room: i32 = toI32(out.length) - DNS_ADDRESS_BYTES
411
+ if (at < 0 || at > room) {
385
412
  panic("dnsParseAddress: at is outside out")
386
413
  }
387
414
  if (port < 0 || port > 0xffff) {
@@ -391,60 +418,58 @@ const dnsParseAddressVia = (
391
418
  panic("dnsParseAddress: parsed is shorter than 16 bytes")
392
419
  }
393
420
  let ok: boolean = false
394
- const end: i32 = off + len
395
- let i: i32 = off
396
- while (i >= 0 && i < end && i < toI32(b.length) && toI32(b[i]) !== DNS_COLON) {
421
+ let i: i32 = from
422
+ while (i < to && toI32(b[i]) !== DNS_COLON) {
397
423
  i += 1
398
424
  }
399
- if (i === end) {
425
+ if (i === to) {
400
426
  let z: i32 = 0
401
427
  while (z < 10) {
402
428
  parsed[z] = toU8(0)
403
429
  z += 1
404
430
  }
405
- ok = dnsParseIpv4(b, off, end, parsed, 12)
406
431
  parsed[10] = toU8(255)
407
432
  parsed[11] = toU8(255)
433
+ ok = dnsParseIpv4(b, from, to, parsed, 12)
408
434
  } else {
409
- ok = dnsParseIpv6(b, off, end, parsed, 0)
435
+ ok = dnsParseIpv6(b, from, to, parsed)
410
436
  }
411
437
  if (!ok) {
412
438
  return false
413
439
  }
440
+ // `out[at, at + 18)` and `parsed[0, 16)` were checked above, so the bounds never stop the copy.
441
+ let p: i32 = at
414
442
  let k: i32 = 0
415
- while (k < 16) {
416
- out[at + k] = parsed[k]
443
+ while (k < 16 && k < toI32(parsed.length) && p < toI32(out.length)) {
444
+ out[p] = parsed[k]
445
+ p += 1
417
446
  k += 1
418
447
  }
419
448
  setU16BE(out, at + 16, toU16(port))
420
449
  return true
421
450
  }
422
451
 
423
- /** Whether the address at `a[at]` is `::ffff:a.b.c.d`, an IPv4 one. */
424
- const dnsIsIpv4 = (a: u8[], at: i32): boolean => {
425
- let k: i32 = 0
426
- while (k < 10) {
427
- if (toI32(a[at + k]) !== 0) {
428
- return false
429
- }
430
- k += 1
452
+ /** Whether `b[from, to)` is the ASCII word `word`. */
453
+ const dnsWordIs = (b: u8[], from: i32, to: i32, word: string): boolean => {
454
+ if (from < 0 || to < from || to > toI32(b.length)) {
455
+ // Never taken: every caller passes a window inside a checked line; this panics as a bad window would.
456
+ dnsConfigCheckWindow(toI32(b.length), from, to, "dnsParseResolvConf")
457
+ return false
431
458
  }
432
- return toI32(a[at + 10]) === 255 && toI32(a[at + 11]) === 255
433
- }
434
-
435
- /**
436
- * The end of the line that starts at `from`, before `to`: the offset of its
437
- * newline, or `to`.
438
- */
439
- const dnsLineEnd = (b: u8[], from: i32, to: i32): i32 => {
440
- if (from < 0 || from > to || to > toI32(b.length)) {
441
- panic("dnsLineEnd: the window is outside the array")
459
+ const n: i32 = toI32(word.length)
460
+ if (to - from !== n) {
461
+ return false
442
462
  }
443
463
  let i: i32 = from
444
- while (i < to && toI32(b[i]) !== DNS_NEWLINE) {
464
+ let k: i32 = 0
465
+ while (k < n && i < to) {
466
+ if (toI32(b[i]) !== toI32(word.charCodeAt(k))) {
467
+ return false
468
+ }
445
469
  i += 1
470
+ k += 1
446
471
  }
447
- return i
472
+ return true
448
473
  }
449
474
 
450
475
  /** Where the line `b[from, to)` ends once a `#` or `;` comment is cut from it. */
@@ -459,34 +484,6 @@ const dnsCommentCut = (b: u8[], from: i32, to: i32): i32 => {
459
484
  return i
460
485
  }
461
486
 
462
- /** The end of the word that starts at `from`, before `to`. */
463
- const dnsWordEnd = (b: u8[], from: i32, to: i32): i32 => {
464
- if (from < 0 || from > to || to > toI32(b.length)) {
465
- panic("dnsWordEnd: the window is outside the array")
466
- }
467
- let i: i32 = from
468
- while (i < to && !dnsIsBlank(toI32(b[i]))) {
469
- i += 1
470
- }
471
- return i
472
- }
473
-
474
- /** Whether `b[from, to)` is the ASCII word `word`. */
475
- const dnsWordIs = (b: u8[], from: i32, to: i32, word: string): boolean => {
476
- const n: i32 = toI32(word.length)
477
- if (to - from !== n) {
478
- return false
479
- }
480
- let k: i32 = 0
481
- while (k < n) {
482
- if (toI32(b[from + k]) !== toI32(word.charCodeAt(k))) {
483
- return false
484
- }
485
- k += 1
486
- }
487
- return true
488
- }
489
-
490
487
  /**
491
488
  * The option `b[from, to)` read as `prefix` and a decimal count, clamped to
492
489
  * `[low, high]`, or `fallback` when it is not that option: a digit past the
@@ -497,17 +494,25 @@ const dnsOption = (b: u8[], from: i32, to: i32, prefix: string, low: i32, high:
497
494
  panic("dnsOption: the window is outside the array")
498
495
  }
499
496
  const n: i32 = toI32(prefix.length)
500
- if (to - from <= n || !dnsWordIs(b, from, from + n, prefix)) {
497
+ if (to - from <= n) {
501
498
  return fallback
502
499
  }
500
+ let i: i32 = from
501
+ let k: i32 = 0
502
+ while (k < n) {
503
+ if (i >= to || toI32(b[i]) !== toI32(prefix.charCodeAt(k))) {
504
+ return fallback
505
+ }
506
+ i += 1
507
+ k += 1
508
+ }
503
509
  let value: i32 = 0
504
- let i: i32 = from + n
505
510
  while (i < to) {
506
511
  const d: i32 = toI32(b[i]) - 48
507
512
  if (d < 0 || d > 9) {
508
513
  return fallback
509
514
  }
510
- if (value < 10000) {
515
+ if (value >= 0 && value < 10000) {
511
516
  value = value * 10 + d
512
517
  }
513
518
  i += 1
@@ -523,76 +528,137 @@ const dnsOption = (b: u8[], from: i32, to: i32, prefix: string, low: i32, high:
523
528
 
524
529
  /** Appends `b[from, to)` to `config`'s search list, lowercase, one trailing dot dropped, or counts it dropped. */
525
530
  const dnsAddSearch = (b: u8[], from: i32, to: i32, config: DnsConfig): void => {
526
- let end: i32 = to
527
- if (end > from && toI32(b[end - 1]) === DNS_DOT) {
528
- end -= 1
529
- }
530
- const n: i32 = end - from
531
- const used: i32 =
532
- config.searchCount === 0
533
- ? 0
534
- : config.searchStart[config.searchCount - 1] + config.searchLength[config.searchCount - 1]
535
- if (n === 0 || config.searchCount === DNS_MAX_SEARCH || n > DNS_MAX_SEARCH_BYTES - used) {
536
- config.droppedSearch += 1
531
+ if (from < 0 || to < 0 || to < from || to > toI32(b.length)) {
532
+ // Never taken: the caller passes a word inside a checked line; this panics as a bad window would.
533
+ dnsConfigCheckWindow(toI32(b.length), from, to, "dnsParseResolvConf")
537
534
  return
538
535
  }
539
- let k: i32 = 0
540
- while (k < n) {
541
- const c: i32 = toI32(b[from + k])
542
- config.search[used + k] = toU8(c >= 65 && c <= 90 ? c + 32 : c)
536
+ let n: i32 = to - from
537
+ const lastByte: i32 = to - 1
538
+ if (n > 0 && lastByte >= 0 && lastByte < toI32(b.length) && toI32(b[lastByte]) === DNS_DOT) {
539
+ n -= 1
540
+ }
541
+ const search: u8[] = config.search
542
+ const starts: i32[] = config.searchStart
543
+ const lengths: i32[] = config.searchLength
544
+ const size: i32 = toI32(search.length)
545
+ const count: i32 = config.searchCount
546
+ if (count < 0) {
547
+ // Never taken: the line reset the count to 0 and only this adds to it; a negative index panics.
548
+ dnsConfigCheckWindow(DNS_MAX_SEARCH, count, count, "dnsParseResolvConf: config.searchCount")
549
+ return
550
+ }
551
+ let used: i32 = 0
552
+ if (count > 0) {
553
+ const last: i32 = count - 1
554
+ if (last >= toI32(starts.length) || last >= toI32(lengths.length)) {
555
+ dnsConfigCheckWindow(toI32(starts.length), last, count, "dnsParseResolvConf: config.searchStart")
556
+ dnsConfigCheckWindow(toI32(lengths.length), last, count, "dnsParseResolvConf: config.searchLength")
557
+ return
558
+ }
559
+ const s: i32 = starts[last]
560
+ const l: i32 = lengths[last]
561
+ // 256 is DNS_MAX_SEARCH_BYTES.
562
+ if (s < 0 || l < 0 || s > 256 || l > 256 || l > 256 - s) {
563
+ // Never taken: this line wrote the domain, inside the cap; -1 fits no window, so this panics.
564
+ dnsConfigCheckWindow(DNS_MAX_SEARCH_BYTES, s, -1, "dnsParseResolvConf: config.searchStart")
565
+ return
566
+ }
567
+ used = s + l
568
+ }
569
+ // 6 is DNS_MAX_SEARCH and 256 DNS_MAX_SEARCH_BYTES; `n > 256` alone never decides, as `used >= 0`.
570
+ if (n === 0 || count >= 6 || n > 256 || n > 256 - used) {
571
+ config.droppedSearch = dnsCount(config.droppedSearch)
572
+ return
573
+ }
574
+ if (count >= toI32(starts.length) || count >= toI32(lengths.length) || n > size - used) {
575
+ // `config`'s arrays were replaced with shorter ones: the writes below would panic, and this does.
576
+ dnsConfigCheckWindow(size, used, used + n, "dnsParseResolvConf: config.search")
577
+ dnsConfigCheckWindow(toI32(starts.length), count, count + 1, "dnsParseResolvConf: config.searchStart")
578
+ dnsConfigCheckWindow(toI32(lengths.length), count, count + 1, "dnsParseResolvConf: config.searchLength")
579
+ return
580
+ }
581
+ let p: i32 = used
582
+ let k: i32 = from
583
+ while (k < to && k - from < n && p < size) {
584
+ const c: i32 = toI32(b[k])
585
+ search[p] = toU8(c >= 65 && c <= 90 ? c + 32 : c)
586
+ p += 1
543
587
  k += 1
544
588
  }
545
- config.searchStart[config.searchCount] = used
546
- config.searchLength[config.searchCount] = n
547
- config.searchCount += 1
589
+ starts[count] = used
590
+ lengths[count] = n
591
+ config.searchCount = count + 1
548
592
  }
549
593
 
550
594
  /** Reads one resolv.conf line, `b[from, to)` with its comment already cut, into `config`. */
551
595
  const dnsResolvLine = (b: u8[], from: i32, to: i32, config: DnsConfig): void => {
552
- const keyStart: i32 = skipBlank(b, from, to)
553
- const keyEnd: i32 = dnsWordEnd(b, keyStart, to)
554
- let i: i32 = skipBlank(b, keyEnd, to)
596
+ if (from < 0 || to < from || to > toI32(b.length)) {
597
+ // Never taken: the caller cuts the line from a checked window; this panics as a bad window would.
598
+ dnsConfigCheckWindow(toI32(b.length), from, to, "dnsParseResolvConf")
599
+ return
600
+ }
601
+ let i: i32 = from
602
+ while (i < to && dnsIsBlank(toI32(b[i]))) {
603
+ i += 1
604
+ }
605
+ const keyStart: i32 = i
606
+ while (i < to && !dnsIsBlank(toI32(b[i]))) {
607
+ i += 1
608
+ }
609
+ const keyEnd: i32 = i
610
+ while (i < to && dnsIsBlank(toI32(b[i]))) {
611
+ i += 1
612
+ }
555
613
  if (dnsWordIs(b, keyStart, keyEnd, "nameserver")) {
556
- const end: i32 = dnsWordEnd(b, i, to)
557
- if (end === i) {
614
+ const start: i32 = i
615
+ while (i < to && !dnsIsBlank(toI32(b[i]))) {
616
+ i += 1
617
+ }
618
+ if (i === start) {
558
619
  return
559
620
  }
560
- if (config.serverCount === DNS_MAX_SERVERS) {
561
- config.droppedServers += 1
621
+ // The parse reset the count to 0 and only this adds 1 below the cap, so it is never negative.
622
+ const count: i32 = config.serverCount
623
+ // 3 is DNS_MAX_SERVERS.
624
+ if (count < 0 || count >= 3) {
625
+ config.droppedServers = dnsCount(config.droppedServers)
562
626
  return
563
627
  }
564
628
  if (
565
- dnsParseAddressVia(
566
- b,
567
- i,
568
- end - i,
569
- config.servers,
570
- config.serverCount * DNS_ADDRESS_BYTES,
571
- DNS_PORT,
572
- config.scratch
573
- )
629
+ dnsParseAddressVia(b, start, i, config.servers, count * DNS_ADDRESS_BYTES, DNS_PORT, config.scratch)
574
630
  ) {
575
- config.serverCount += 1
631
+ config.serverCount = count + 1
576
632
  }
577
633
  } else if (dnsWordIs(b, keyStart, keyEnd, "search") || dnsWordIs(b, keyStart, keyEnd, "domain")) {
578
634
  const single: boolean = dnsWordIs(b, keyStart, keyEnd, "domain")
579
635
  config.searchCount = 0
580
636
  config.droppedSearch = 0
581
637
  while (i < to) {
582
- const end: i32 = dnsWordEnd(b, i, to)
583
- dnsAddSearch(b, i, end, config)
584
- i = skipBlank(b, end, to)
638
+ const start: i32 = i
639
+ while (i < to && !dnsIsBlank(toI32(b[i]))) {
640
+ i += 1
641
+ }
642
+ dnsAddSearch(b, start, i, config)
643
+ while (i < to && dnsIsBlank(toI32(b[i]))) {
644
+ i += 1
645
+ }
585
646
  if (single) {
586
647
  return
587
648
  }
588
649
  }
589
650
  } else if (dnsWordIs(b, keyStart, keyEnd, "options")) {
590
651
  while (i < to) {
591
- const end: i32 = dnsWordEnd(b, i, to)
592
- config.ndots = dnsOption(b, i, end, "ndots:", 0, DNS_MAX_NDOTS, config.ndots)
593
- config.timeout = dnsOption(b, i, end, "timeout:", 1, DNS_MAX_TIMEOUT, config.timeout)
594
- config.attempts = dnsOption(b, i, end, "attempts:", 1, DNS_MAX_ATTEMPTS, config.attempts)
595
- i = skipBlank(b, end, to)
652
+ const start: i32 = i
653
+ while (i < to && !dnsIsBlank(toI32(b[i]))) {
654
+ i += 1
655
+ }
656
+ config.ndots = dnsOption(b, start, i, "ndots:", 0, DNS_MAX_NDOTS, config.ndots)
657
+ config.timeout = dnsOption(b, start, i, "timeout:", 1, DNS_MAX_TIMEOUT, config.timeout)
658
+ config.attempts = dnsOption(b, start, i, "attempts:", 1, DNS_MAX_ATTEMPTS, config.attempts)
659
+ while (i < to && dnsIsBlank(toI32(b[i]))) {
660
+ i += 1
661
+ }
596
662
  }
597
663
  }
598
664
  }
@@ -600,8 +666,10 @@ const dnsResolvLine = (b: u8[], from: i32, to: i32, config: DnsConfig): void =>
600
666
  /**
601
667
  * Reads the resolv.conf `b[off, off + len)` into `config`, which starts over
602
668
  * from the defaults, and gives it 127.0.0.1 and ::1 on port 53 when no
603
- * nameserver line held an address. It allocates nothing, and nothing in the
604
- * bytes makes it panic or fail: what it cannot use is ignored or counted.
669
+ * nameserver line held an address. It allocates nothing, unless it finds
670
+ * `config.servers` replaced with an array shorter than `DNS_MAX_SERVERS`
671
+ * addresses, when it first gives `config` a new full table. Nothing in the bytes
672
+ * makes it panic or fail: what it cannot use is ignored or counted.
605
673
  *
606
674
  * @param b The file's bytes, read by the caller.
607
675
  * @param off Where the file starts in `b`.
@@ -609,7 +677,18 @@ const dnsResolvLine = (b: u8[], from: i32, to: i32, config: DnsConfig): void =>
609
677
  * @param config The table to fill; its counts and options are reset first, so one can be reused.
610
678
  */
611
679
  export const dnsParseResolvConf = (b: u8[], off: i32, len: i32, config: DnsConfig): void => {
612
- dnsConfigCheckWindow(b, off, len, "dnsParseResolvConf")
680
+ // A negative `len`, or an end past the i32 range, makes `to` fall below `off`.
681
+ const to: i32 = toI32(toI64(off) + toI64(len))
682
+ if (off < 0 || to < off || to > toI32(b.length)) {
683
+ dnsConfigCheckWindow(toI32(b.length), off, to, "dnsParseResolvConf")
684
+ return
685
+ }
686
+ if (toI32(config.servers.length) < 54) {
687
+ // Only a caller that replaced `config.servers` with a shorter array gets
688
+ // here. Both a nameserver line and the fallback write into it, so it gets
689
+ // a full table of `DNS_MAX_SERVERS` addresses, 54 bytes.
690
+ config.servers = new Array<u8>(54)
691
+ }
613
692
  config.serverCount = 0
614
693
  config.droppedServers = 0
615
694
  config.searchCount = 0
@@ -618,16 +697,20 @@ export const dnsParseResolvConf = (b: u8[], off: i32, len: i32, config: DnsConfi
618
697
  config.timeout = DNS_DEFAULT_TIMEOUT
619
698
  config.attempts = DNS_DEFAULT_ATTEMPTS
620
699
  config.skippedLines = 0
621
- const to: i32 = off + len
622
700
  let i: i32 = off
623
701
  while (i < to) {
624
- const end: i32 = dnsLineEnd(b, i, to)
625
- if (end - i > DNS_CONFIG_MAX_LINE) {
626
- config.skippedLines += 1
702
+ const start: i32 = i
703
+ while (i < to && toI32(b[i]) !== DNS_NEWLINE) {
704
+ i += 1
705
+ }
706
+ if (i - start > DNS_CONFIG_MAX_LINE) {
707
+ config.skippedLines = dnsCount(config.skippedLines)
627
708
  } else {
628
- dnsResolvLine(b, i, dnsCommentCut(b, i, end), config)
709
+ dnsResolvLine(b, start, dnsCommentCut(b, start, i), config)
710
+ }
711
+ if (i < to) {
712
+ i += 1
629
713
  }
630
- i = end + 1
631
714
  }
632
715
  if (config.serverCount === 0) {
633
716
  dnsFallbackServers(config.servers)
@@ -635,13 +718,17 @@ export const dnsParseResolvConf = (b: u8[], off: i32, len: i32, config: DnsConfi
635
718
  }
636
719
  }
637
720
 
638
- /** Writes 127.0.0.1 and ::1, each on `DNS_PORT`, as the first two of `servers`, allocating nothing. */
721
+ /**
722
+ * Writes 127.0.0.1 and ::1, each on `DNS_PORT`, as the first two of
723
+ * `servers`, allocating nothing. The caller passes at least 36 bytes, two
724
+ * addresses; a shorter `servers` is left as it is.
725
+ */
639
726
  const dnsFallbackServers = (servers: u8[]): void => {
640
- if (toI32(servers.length) < 2 * DNS_ADDRESS_BYTES) {
641
- panic("dnsFallbackServers: servers has no room for two addresses")
727
+ if (toI32(servers.length) < 36) {
728
+ return
642
729
  }
643
730
  let k: i32 = 0
644
- while (k < 2 * DNS_ADDRESS_BYTES && k < toI32(servers.length)) {
731
+ while (k < 36) {
645
732
  servers[k] = toU8(0)
646
733
  k += 1
647
734
  }
@@ -649,62 +736,116 @@ const dnsFallbackServers = (servers: u8[]): void => {
649
736
  servers[11] = toU8(255)
650
737
  servers[12] = toU8(127)
651
738
  servers[15] = toU8(1)
739
+ servers[33] = toU8(1)
652
740
  setU16BE(servers, 16, toU16(DNS_PORT))
653
- servers[DNS_ADDRESS_BYTES + 15] = toU8(1)
654
741
  setU16BE(servers, DNS_ADDRESS_BYTES + 16, toU16(DNS_PORT))
655
742
  }
656
743
 
657
744
  /** Adds `b[from, to)` as a name of entry `entry`, lowercase, one trailing dot dropped, or counts it dropped. */
658
745
  const dnsAddHostName = (b: u8[], from: i32, to: i32, entry: i32, hosts: DnsHosts): void => {
659
- let end: i32 = to
660
- if (end > from && toI32(b[end - 1]) === DNS_DOT) {
661
- end -= 1
746
+ if (from < 0 || to < 0 || to < from || to > toI32(b.length)) {
747
+ // Never taken: the caller passes a word inside a checked line; this panics as a bad window would.
748
+ dnsConfigCheckWindow(toI32(b.length), from, to, "dnsParseHosts")
749
+ return
750
+ }
751
+ let n: i32 = to - from
752
+ const lastByte: i32 = to - 1
753
+ if (n > 0 && lastByte >= 0 && lastByte < toI32(b.length) && toI32(b[lastByte]) === DNS_DOT) {
754
+ n -= 1
662
755
  }
663
- const n: i32 = end - from
756
+ const names: u8[] = hosts.names
757
+ const starts: i32[] = hosts.nameStart
758
+ const lengths: i32[] = hosts.nameLength
759
+ const entries: i32[] = hosts.nameEntry
760
+ const size: i32 = toI32(names.length)
761
+ // The parse reset both counts to 0 and only this moves them, inside their caps, so the
762
+ // first two tests on `count` and `used` are never what drops a name.
763
+ const count: i32 = hosts.nameCount
764
+ const used: i32 = hosts.nameBytes
664
765
  if (
766
+ count < 0 ||
767
+ used < 0 ||
768
+ used > 16384 || // DNS_HOSTS_MAX_NAME_BYTES
665
769
  n === 0 ||
666
- n > DNS_HOSTS_MAX_NAME ||
667
- hosts.nameCount === DNS_HOSTS_MAX_NAMES ||
668
- n > DNS_HOSTS_MAX_NAME_BYTES - hosts.nameBytes
770
+ n > 253 || // DNS_HOSTS_MAX_NAME
771
+ count >= 1024 || // DNS_HOSTS_MAX_NAMES
772
+ n > 16384 - used
669
773
  ) {
670
- hosts.droppedNames += 1
774
+ hosts.droppedNames = dnsCount(hosts.droppedNames)
671
775
  return
672
776
  }
673
- let k: i32 = 0
674
- while (k < n) {
675
- const c: i32 = toI32(b[from + k])
676
- hosts.names[hosts.nameBytes + k] = toU8(c >= 65 && c <= 90 ? c + 32 : c)
777
+ if (
778
+ count >= toI32(starts.length) ||
779
+ count >= toI32(lengths.length) ||
780
+ count >= toI32(entries.length) ||
781
+ n > size - used
782
+ ) {
783
+ // `hosts`' arrays were replaced with shorter ones: the writes below would panic, and this does.
784
+ dnsConfigCheckWindow(size, used, used + n, "dnsParseHosts: hosts.names")
785
+ dnsConfigCheckWindow(toI32(starts.length), count, count + 1, "dnsParseHosts: hosts.nameStart")
786
+ dnsConfigCheckWindow(toI32(lengths.length), count, count + 1, "dnsParseHosts: hosts.nameLength")
787
+ dnsConfigCheckWindow(toI32(entries.length), count, count + 1, "dnsParseHosts: hosts.nameEntry")
788
+ return
789
+ }
790
+ let p: i32 = used
791
+ let k: i32 = from
792
+ while (k < to && k - from < n && p < size) {
793
+ const c: i32 = toI32(b[k])
794
+ names[p] = toU8(c >= 65 && c <= 90 ? c + 32 : c)
795
+ p += 1
677
796
  k += 1
678
797
  }
679
- hosts.nameStart[hosts.nameCount] = hosts.nameBytes
680
- hosts.nameLength[hosts.nameCount] = n
681
- hosts.nameEntry[hosts.nameCount] = entry
682
- hosts.nameCount += 1
683
- hosts.nameBytes += n
798
+ starts[count] = used
799
+ lengths[count] = n
800
+ entries[count] = entry
801
+ hosts.nameCount = count + 1
802
+ hosts.nameBytes = used + n
684
803
  }
685
804
 
686
805
  /** Reads one hosts line, `b[from, to)` with its comment already cut, into `hosts`. */
687
806
  const dnsHostsLine = (b: u8[], from: i32, to: i32, hosts: DnsHosts): void => {
688
- const addrStart: i32 = skipBlank(b, from, to)
689
- const addrEnd: i32 = dnsWordEnd(b, addrStart, to)
690
- let i: i32 = skipBlank(b, addrEnd, to)
807
+ if (from < 0 || to < from || to > toI32(b.length)) {
808
+ // Never taken: the caller cuts the line from a checked window; this panics as a bad window would.
809
+ dnsConfigCheckWindow(toI32(b.length), from, to, "dnsParseHosts")
810
+ return
811
+ }
812
+ let i: i32 = from
813
+ while (i < to && dnsIsBlank(toI32(b[i]))) {
814
+ i += 1
815
+ }
816
+ const addrStart: i32 = i
817
+ while (i < to && !dnsIsBlank(toI32(b[i]))) {
818
+ i += 1
819
+ }
820
+ const addrEnd: i32 = i
821
+ while (i < to && dnsIsBlank(toI32(b[i]))) {
822
+ i += 1
823
+ }
691
824
  if (addrEnd === addrStart || i === to) {
692
825
  return
693
826
  }
694
- if (hosts.entryCount === DNS_HOSTS_MAX_ENTRIES) {
695
- hosts.droppedEntries += 1
827
+ // The parse reset the count to 0 and only this adds 1 below the cap, so it is never negative.
828
+ const count: i32 = hosts.entryCount
829
+ // 256 is DNS_HOSTS_MAX_ENTRIES.
830
+ if (count < 0 || count >= 256) {
831
+ hosts.droppedEntries = dnsCount(hosts.droppedEntries)
696
832
  return
697
833
  }
698
- const at: i32 = hosts.entryCount * DNS_ADDRESS_BYTES
699
- if (!dnsParseAddressVia(b, addrStart, addrEnd - addrStart, hosts.addresses, at, 0, hosts.scratch)) {
834
+ if (
835
+ !dnsParseAddressVia(b, addrStart, addrEnd, hosts.addresses, count * DNS_ADDRESS_BYTES, 0, hosts.scratch)
836
+ ) {
700
837
  return
701
838
  }
702
- const entry: i32 = hosts.entryCount
703
- hosts.entryCount += 1
839
+ hosts.entryCount = count + 1
704
840
  while (i < to) {
705
- const end: i32 = dnsWordEnd(b, i, to)
706
- dnsAddHostName(b, i, end, entry, hosts)
707
- i = skipBlank(b, end, to)
841
+ const start: i32 = i
842
+ while (i < to && !dnsIsBlank(toI32(b[i]))) {
843
+ i += 1
844
+ }
845
+ dnsAddHostName(b, start, i, count, hosts)
846
+ while (i < to && dnsIsBlank(toI32(b[i]))) {
847
+ i += 1
848
+ }
708
849
  }
709
850
  }
710
851
 
@@ -719,43 +860,34 @@ const dnsHostsLine = (b: u8[], from: i32, to: i32, hosts: DnsHosts): void => {
719
860
  * @param hosts The table to fill; it is emptied first, so one can be reused.
720
861
  */
721
862
  export const dnsParseHosts = (b: u8[], off: i32, len: i32, hosts: DnsHosts): void => {
722
- dnsConfigCheckWindow(b, off, len, "dnsParseHosts")
863
+ // A negative `len`, or an end past the i32 range, makes `to` fall below `off`.
864
+ const to: i32 = toI32(toI64(off) + toI64(len))
865
+ if (off < 0 || to < off || to > toI32(b.length)) {
866
+ dnsConfigCheckWindow(toI32(b.length), off, to, "dnsParseHosts")
867
+ return
868
+ }
723
869
  hosts.entryCount = 0
724
870
  hosts.nameCount = 0
725
871
  hosts.nameBytes = 0
726
872
  hosts.droppedEntries = 0
727
873
  hosts.droppedNames = 0
728
874
  hosts.skippedLines = 0
729
- const to: i32 = off + len
730
875
  let i: i32 = off
731
876
  while (i < to) {
732
- const end: i32 = dnsLineEnd(b, i, to)
733
- if (end - i > DNS_CONFIG_MAX_LINE) {
734
- hosts.skippedLines += 1
877
+ const start: i32 = i
878
+ while (i < to && toI32(b[i]) !== DNS_NEWLINE) {
879
+ i += 1
880
+ }
881
+ if (i - start > DNS_CONFIG_MAX_LINE) {
882
+ hosts.skippedLines = dnsCount(hosts.skippedLines)
735
883
  } else {
736
- dnsHostsLine(b, i, dnsCommentCut(b, i, end), hosts)
884
+ dnsHostsLine(b, start, dnsCommentCut(b, start, i), hosts)
737
885
  }
738
- i = end + 1
739
- }
740
- }
741
-
742
- /** Whether kept name `index` of `hosts` is `name[0, n)`, without case. */
743
- const dnsHostNameIs = (hosts: DnsHosts, index: i32, name: string, n: i32): boolean => {
744
- if (hosts.nameLength[index] !== n) {
745
- return false
746
- }
747
- const start: i32 = hosts.nameStart[index]
748
- let k: i32 = 0
749
- while (k < n) {
750
- const c: i32 = toI32(name.charCodeAt(k))
751
- if (toI32(hosts.names[start + k]) !== (c >= 65 && c <= 90 ? c + 32 : c)) {
752
- return false
886
+ if (i < to) {
887
+ i += 1
753
888
  }
754
- k += 1
755
889
  }
756
- return true
757
890
  }
758
-
759
891
  /**
760
892
  * Writes the addresses `hosts` gives `name`, of `qtype` (`DNS_TYPE_A` or
761
893
  * `DNS_TYPE_AAAA`), into `out` from offset 0 with `port`, in file order and at
@@ -794,25 +926,84 @@ export const dnsHostsFind = (
794
926
  if (n > 0 && toI32(name.charCodeAt(n - 1)) === DNS_DOT) {
795
927
  n -= 1
796
928
  }
929
+ const addresses: u8[] = hosts.addresses
930
+ const names: u8[] = hosts.names
931
+ const starts: i32[] = hosts.nameStart
932
+ const lengths: i32[] = hosts.nameLength
933
+ const entries: i32[] = hosts.nameEntry
934
+ const count: i32 = hosts.nameCount
935
+ const wantV4: boolean = qtype === DNS_TYPE_A
797
936
  let found: i32 = 0
798
937
  let last: i32 = -1
799
938
  let i: i32 = 0
800
- while (i < hosts.nameCount && found < max) {
801
- const entry: i32 = hosts.nameEntry[i]
802
- const at: i32 = entry * DNS_ADDRESS_BYTES
803
- if (
804
- entry !== last &&
805
- dnsIsIpv4(hosts.addresses, at) === (qtype === DNS_TYPE_A) &&
806
- dnsHostNameIs(hosts, i, name, n)
807
- ) {
808
- let k: i32 = 0
809
- while (k < 16) {
810
- out[found * DNS_ADDRESS_BYTES + k] = hosts.addresses[at + k]
811
- k += 1
939
+ while (i < count && found < max) {
940
+ // A table `dnsParseHosts` filled always passes these; only one whose arrays or counts
941
+ // were changed by hand can fail them, where the reads below would have panicked.
942
+ if (i >= toI32(entries.length) || i >= toI32(lengths.length) || i >= toI32(starts.length)) {
943
+ panic("dnsHostsFind: hosts is not a table dnsParseHosts filled")
944
+ }
945
+ const entry: i32 = entries[i]
946
+ if (entry !== last) {
947
+ // 256 is DNS_HOSTS_MAX_ENTRIES.
948
+ if (entry < 0 || entry >= 256) {
949
+ panic("dnsHostsFind: hosts is not a table dnsParseHosts filled")
950
+ }
951
+ const at: i32 = entry * DNS_ADDRESS_BYTES
952
+ const room: i32 = toI32(addresses.length) - DNS_ADDRESS_BYTES
953
+ if (at > room) {
954
+ panic("dnsHostsFind: hosts is not a table dnsParseHosts filled")
955
+ }
956
+ let p: i32 = at
957
+ let zeros: i32 = 0
958
+ while (zeros < 10 && p < toI32(addresses.length) && toI32(addresses[p]) === 0) {
959
+ p += 1
960
+ zeros += 1
961
+ }
962
+ // After ten zeros `p` is `at + 10`, inside the 18 bytes checked above.
963
+ const v4: boolean =
964
+ zeros === 10 &&
965
+ p < toI32(addresses.length) - 1 &&
966
+ toI32(addresses[p]) === 255 &&
967
+ toI32(addresses[p + 1]) === 255
968
+ let same: boolean = v4 === wantV4 && lengths[i] === n
969
+ if (same) {
970
+ const start: i32 = starts[i]
971
+ if (start < 0 || start > toI32(names.length) - n) {
972
+ panic("dnsHostsFind: hosts is not a table dnsParseHosts filled")
973
+ }
974
+ let q: i32 = start
975
+ let k: i32 = 0
976
+ while (same && k < n && q < toI32(names.length)) {
977
+ const c: i32 = toI32(name.charCodeAt(k))
978
+ same = toI32(names[q]) === (c >= 65 && c <= 90 ? c + 32 : c)
979
+ q += 1
980
+ k += 1
981
+ }
982
+ }
983
+ if (same) {
984
+ // Never taken: `found < max <= out.length / 18`, so `found` is at most 119304646;
985
+ // the cap only states it as a literal, and fails as a short `out` would.
986
+ if (found < 0 || found > 119304646) {
987
+ panic("dnsHostsFind: out has no room for max addresses")
988
+ }
989
+ const base: i32 = found * DNS_ADDRESS_BYTES
990
+ const outRoom: i32 = toI32(out.length) - DNS_ADDRESS_BYTES
991
+ if (base > outRoom) {
992
+ panic("dnsHostsFind: out has no room for max addresses")
993
+ }
994
+ let q: i32 = base
995
+ let r: i32 = at
996
+ let k: i32 = 0
997
+ while (k < 16 && q < toI32(out.length) && r < toI32(addresses.length)) {
998
+ out[q] = addresses[r]
999
+ q += 1
1000
+ r += 1
1001
+ k += 1
1002
+ }
1003
+ setU16BE(out, base + 16, toU16(port))
1004
+ found += 1
1005
+ last = entry
812
1006
  }
813
- setU16BE(out, found * DNS_ADDRESS_BYTES + 16, toU16(port))
814
- found += 1
815
- last = entry
816
1007
  }
817
1008
  i += 1
818
1009
  }