@amritk/nish-x86_64-linux 0.49.1 → 0.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +237 -160
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +94 -11
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +14 -11
- package/std/base64.ts +74 -35
- package/std/bytes.ts +207 -97
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +213 -115
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +484 -149
- package/std/crypto/jwt.ts +51 -40
- package/std/crypto/p256.ts +14 -19
- package/std/crypto/pki-roots.ts +35 -12
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1635 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +212 -90
- package/std/crypto/sha512.ts +135 -49
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +236 -119
- package/std/json.ts +81 -46
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +224 -61
- package/std/net/handler.ts +189 -88
- package/std/net/hpack.ts +216 -103
- package/std/net/http-compress.ts +104 -42
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +326 -24
- package/std/net/http1-client-codec.ts +159 -55
- package/std/net/http1-codec.ts +427 -237
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +139 -60
- package/std/net/http3.ts +140 -79
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +97 -64
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +147 -40
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +61 -39
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +36 -27
- package/std/net/quic.ts +84 -54
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +86 -36
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +80 -35
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +194 -50
- package/std/net/upstream.ts +83 -38
- package/std/net/url.ts +170 -94
- package/std/net/websocket.ts +19 -20
- package/std/net/webtransport.ts +123 -33
- package/std/raster.ts +12 -2
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +176 -24
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +38 -16
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/json.ts
CHANGED
|
@@ -225,6 +225,25 @@ const jsonClosingQuote = (text: string, start: i32): i32 => {
|
|
|
225
225
|
return -1
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Where the `count` bytes from `at` end, `at + count`, when they end by
|
|
230
|
+
* `length`; `-1` when any of the three is negative or they run past it. Written
|
|
231
|
+
* as differences, each of which `src/bounds.ts` proves, so the end needs no
|
|
232
|
+
* overflow check and no cast through `u32`. A `-1` means only a span that
|
|
233
|
+
* runs past `length` (a full writer); no sum of in-range values wraps to it.
|
|
234
|
+
*/
|
|
235
|
+
const jsonSpanEnd = (length: i32, at: i32, count: i32): i32 => {
|
|
236
|
+
if (length < 0 || at < 0 || count < 0) {
|
|
237
|
+
return -1
|
|
238
|
+
}
|
|
239
|
+
const room: i32 = length - at
|
|
240
|
+
if (room < 0) {
|
|
241
|
+
return -1
|
|
242
|
+
}
|
|
243
|
+
const gap: i32 = room - count
|
|
244
|
+
return gap < 0 ? -1 : length - gap
|
|
245
|
+
}
|
|
246
|
+
|
|
228
247
|
/**
|
|
229
248
|
* The index just past the string literal whose opening quote is at `at`, or `-1`
|
|
230
249
|
* when the quote is never closed. A backslash takes the byte after it with it,
|
|
@@ -240,8 +259,10 @@ const jsonEndOfString = (text: string, at: i32): i32 => {
|
|
|
240
259
|
return -1
|
|
241
260
|
}
|
|
242
261
|
let i: i32 = at + 1
|
|
243
|
-
// The
|
|
244
|
-
|
|
262
|
+
// The first `JSON_INLINE_SCAN` bytes after the quote, or all of them when
|
|
263
|
+
// fewer are left.
|
|
264
|
+
const ahead: i32 = jsonSpanEnd(length, i, JSON_INLINE_SCAN)
|
|
265
|
+
const stop: i32 = ahead < 0 ? length : ahead
|
|
245
266
|
// `i < length` follows from `i < stop`, and is there for the bounds proof.
|
|
246
267
|
while (i < stop && i < length) {
|
|
247
268
|
const code: i32 = toI32(text.charCodeAt(i))
|
|
@@ -262,8 +283,9 @@ const jsonEndOfString = (text: string, at: i32): i32 => {
|
|
|
262
283
|
return -1
|
|
263
284
|
}
|
|
264
285
|
const quote: i32 = jsonClosingQuote(text, i)
|
|
265
|
-
//
|
|
266
|
-
|
|
286
|
+
// A quote found is an index of `text`, so the second test never holds; it
|
|
287
|
+
// is the bound one past the quote is proved by.
|
|
288
|
+
return quote < 0 || quote >= length ? -1 : quote + 1
|
|
267
289
|
}
|
|
268
290
|
|
|
269
291
|
/**
|
|
@@ -305,8 +327,9 @@ const jsonEndOfValue = (text: string, at: i32): i32 => {
|
|
|
305
327
|
const code: i32 = toI32(text.charCodeAt(i))
|
|
306
328
|
if (code === JSON_QUOTE) {
|
|
307
329
|
let j: i32 = i + 1
|
|
308
|
-
//
|
|
309
|
-
const
|
|
330
|
+
// As `jsonEndOfString` bounds its inline scan.
|
|
331
|
+
const ahead: i32 = jsonSpanEnd(length, j, JSON_INLINE_SCAN)
|
|
332
|
+
const stop: i32 = ahead < 0 ? length : ahead
|
|
310
333
|
let close: i32 = -1
|
|
311
334
|
while (j < stop && j < length) {
|
|
312
335
|
const inner: i32 = toI32(text.charCodeAt(j))
|
|
@@ -333,8 +356,12 @@ const jsonEndOfValue = (text: string, at: i32): i32 => {
|
|
|
333
356
|
return -1
|
|
334
357
|
}
|
|
335
358
|
}
|
|
336
|
-
//
|
|
337
|
-
|
|
359
|
+
// A quote found is an index of `text`, so this never returns; it is
|
|
360
|
+
// the bound one past the quote is proved by.
|
|
361
|
+
if (close >= length) {
|
|
362
|
+
return -1
|
|
363
|
+
}
|
|
364
|
+
const after: i32 = close + 1
|
|
338
365
|
if (depth === 0) {
|
|
339
366
|
return after
|
|
340
367
|
}
|
|
@@ -762,12 +789,12 @@ const jsonValueText = (object: string, member: JsonMember): string => {
|
|
|
762
789
|
if (valueAt < 0 || valueEnd > toI32(object.length)) {
|
|
763
790
|
return ""
|
|
764
791
|
}
|
|
765
|
-
if (valueAt >= valueEnd) {
|
|
792
|
+
if (valueEnd <= 0 || valueAt >= valueEnd) {
|
|
766
793
|
return ""
|
|
767
794
|
}
|
|
768
795
|
if (toI32(object.charCodeAt(valueAt)) === JSON_QUOTE) {
|
|
769
|
-
// `valueEnd >
|
|
770
|
-
return jsonUnescape(object, valueAt + 1,
|
|
796
|
+
// `valueEnd > 0`, so one before it is a position in `object`.
|
|
797
|
+
return jsonUnescape(object, valueAt + 1, valueEnd - 1)
|
|
771
798
|
}
|
|
772
799
|
return object.substring(valueAt, valueEnd)
|
|
773
800
|
}
|
|
@@ -1143,9 +1170,11 @@ export class JsonWriter {
|
|
|
1143
1170
|
* @returns the byte count, `end - start`.
|
|
1144
1171
|
*/
|
|
1145
1172
|
written(): i32 {
|
|
1146
|
-
//
|
|
1147
|
-
//
|
|
1148
|
-
|
|
1173
|
+
// `end` never falls below `start`, nor `start` below 0, so the test never
|
|
1174
|
+
// holds; it is the bound the difference is proved by.
|
|
1175
|
+
const start: i32 = this.start
|
|
1176
|
+
const end: i32 = this.end
|
|
1177
|
+
return start < 0 || end < start ? 0 : end - start
|
|
1149
1178
|
}
|
|
1150
1179
|
|
|
1151
1180
|
/**
|
|
@@ -1486,7 +1515,8 @@ export class JsonWriter {
|
|
|
1486
1515
|
}
|
|
1487
1516
|
const buf: u8[] = this.buf
|
|
1488
1517
|
const at: i32 = this.end
|
|
1489
|
-
|
|
1518
|
+
const end: i32 = jsonSpanEnd(toI32(buf.length), at, len)
|
|
1519
|
+
if (end < 0) {
|
|
1490
1520
|
this.failed = JSON_WRITE_FULL
|
|
1491
1521
|
return
|
|
1492
1522
|
}
|
|
@@ -1496,8 +1526,7 @@ export class JsonWriter {
|
|
|
1496
1526
|
this.failed = JSON_WRITE_FULL
|
|
1497
1527
|
return
|
|
1498
1528
|
}
|
|
1499
|
-
|
|
1500
|
-
this.end = toI32(toU32(at) + toU32(len))
|
|
1529
|
+
this.end = end
|
|
1501
1530
|
jsonWriterValueDone(this)
|
|
1502
1531
|
}
|
|
1503
1532
|
}
|
|
@@ -1870,8 +1899,9 @@ const jsonWriterPutBytes = (w: JsonWriter, bytes: u8[], off: i32, len: i32): boo
|
|
|
1870
1899
|
}
|
|
1871
1900
|
buf[at] = toU8(JSON_QUOTE)
|
|
1872
1901
|
at += 1
|
|
1873
|
-
//
|
|
1874
|
-
|
|
1902
|
+
// The caller checked the window lies inside `bytes`; were it not, `stop` is
|
|
1903
|
+
// -1 and nothing is read.
|
|
1904
|
+
const stop: i32 = jsonSpanEnd(toI32(bytes.length), off, len)
|
|
1875
1905
|
for (let i: i32 = off; i >= 0 && i < stop && i < toI32(bytes.length); i += 1) {
|
|
1876
1906
|
const code: i32 = toI32(bytes[i])
|
|
1877
1907
|
if (jsonIsPlainByte(code)) {
|
|
@@ -1932,16 +1962,18 @@ const jsonWriterPutRuns = (w: JsonWriter, bytes: u8[], off: i32, len: i32): bool
|
|
|
1932
1962
|
}
|
|
1933
1963
|
buf[at] = toU8(JSON_QUOTE)
|
|
1934
1964
|
at += 1
|
|
1935
|
-
//
|
|
1936
|
-
|
|
1965
|
+
// The caller checked the window lies inside `bytes`; were it not, `stop` is
|
|
1966
|
+
// -1 and nothing is read.
|
|
1967
|
+
const stop: i32 = jsonSpanEnd(toI32(bytes.length), off, len)
|
|
1937
1968
|
let i: i32 = off
|
|
1938
1969
|
while (i >= 0 && i < stop && i < toI32(bytes.length)) {
|
|
1939
1970
|
const run: i32 = skipJsonPlain(bytes, i, stop)
|
|
1940
|
-
//
|
|
1941
|
-
//
|
|
1942
|
-
const count: i32 =
|
|
1971
|
+
// `skipJsonPlain` answers an index in `[i, stop]`, so the count is the
|
|
1972
|
+
// run's length; the test is the bound the difference is proved by.
|
|
1973
|
+
const count: i32 = run > i ? run - i : 0
|
|
1943
1974
|
if (count > 0) {
|
|
1944
|
-
|
|
1975
|
+
const end: i32 = jsonSpanEnd(cap, at, count)
|
|
1976
|
+
if (end < 0) {
|
|
1945
1977
|
w.failed = JSON_WRITE_FULL
|
|
1946
1978
|
return false
|
|
1947
1979
|
}
|
|
@@ -1949,8 +1981,7 @@ const jsonWriterPutRuns = (w: JsonWriter, bytes: u8[], off: i32, len: i32): bool
|
|
|
1949
1981
|
w.failed = JSON_WRITE_FULL
|
|
1950
1982
|
return false
|
|
1951
1983
|
}
|
|
1952
|
-
|
|
1953
|
-
at = toI32(toU32(at) + toU32(count))
|
|
1984
|
+
at = end
|
|
1954
1985
|
i = run
|
|
1955
1986
|
}
|
|
1956
1987
|
if (i < 0 || i >= stop || i >= toI32(bytes.length)) {
|
|
@@ -2016,15 +2047,18 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
2016
2047
|
bound = bound * ten
|
|
2017
2048
|
}
|
|
2018
2049
|
}
|
|
2019
|
-
const sign:
|
|
2020
|
-
const extra:
|
|
2050
|
+
const sign: integer<0, 1> = v < 0 ? 1 : 0
|
|
2051
|
+
const extra: integer<0, 1> = suffix >= 0 ? 1 : 0
|
|
2021
2052
|
const buf: u8[] = w.buf
|
|
2022
2053
|
const cap: i32 = toI32(buf.length)
|
|
2023
2054
|
const at: i32 = w.end
|
|
2024
|
-
//
|
|
2025
|
-
// at most 12 bytes
|
|
2026
|
-
const
|
|
2027
|
-
|
|
2055
|
+
// The loop leaves `digits` from 1 to `JSON_I32_DIGITS`, so the test always
|
|
2056
|
+
// holds; it is the bound the width, at most 12 bytes, is proved by.
|
|
2057
|
+
const found: i32 = digits
|
|
2058
|
+
const count: integer<0, 10> = found >= 0 && found <= 10 ? found : 1
|
|
2059
|
+
const width: i32 = sign + count + extra
|
|
2060
|
+
const end: i32 = jsonSpanEnd(cap, at, width)
|
|
2061
|
+
if (at < 0 || at >= cap || end < 0) {
|
|
2028
2062
|
w.failed = JSON_WRITE_FULL
|
|
2029
2063
|
return false
|
|
2030
2064
|
}
|
|
@@ -2034,7 +2068,7 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
2034
2068
|
// The last byte of the number. The test above keeps every byte of it
|
|
2035
2069
|
// inside `buf`, so each `k` below is too, and the tests of `k` in the loop
|
|
2036
2070
|
// always hold.
|
|
2037
|
-
let k: i32 =
|
|
2071
|
+
let k: i32 = end - 1
|
|
2038
2072
|
if (extra === 1 && k >= 0 && k < toI32(buf.length)) {
|
|
2039
2073
|
buf[k] = toU8(toU32(JSON_DIGIT_ZERO) + toU32(suffix))
|
|
2040
2074
|
k -= 1
|
|
@@ -2045,7 +2079,7 @@ const jsonWriterPutDigits = (w: JsonWriter, v: i32, suffix: i32): boolean => {
|
|
|
2045
2079
|
rest = (rest - digit) / ten
|
|
2046
2080
|
k -= 1
|
|
2047
2081
|
}
|
|
2048
|
-
w.end =
|
|
2082
|
+
w.end = end
|
|
2049
2083
|
return true
|
|
2050
2084
|
}
|
|
2051
2085
|
|
|
@@ -2066,13 +2100,17 @@ const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
|
|
|
2066
2100
|
bound = bound * ten
|
|
2067
2101
|
}
|
|
2068
2102
|
}
|
|
2069
|
-
const sign:
|
|
2103
|
+
const sign: integer<0, 1> = v < 0 ? 1 : 0
|
|
2070
2104
|
const buf: u8[] = w.buf
|
|
2071
2105
|
const cap: i32 = toI32(buf.length)
|
|
2072
2106
|
const at: i32 = w.end
|
|
2073
|
-
//
|
|
2074
|
-
|
|
2075
|
-
|
|
2107
|
+
// From 1 to `JSON_I64_DIGITS` digits, as `jsonWriterPutDigits` bounds them,
|
|
2108
|
+
// so at most 20 bytes, tested against `cap` as there.
|
|
2109
|
+
const found: i32 = digits
|
|
2110
|
+
const count: integer<0, 19> = found >= 0 && found <= 19 ? found : 1
|
|
2111
|
+
const width: i32 = sign + count
|
|
2112
|
+
const end: i32 = jsonSpanEnd(cap, at, width)
|
|
2113
|
+
if (at < 0 || at >= cap || end < 0) {
|
|
2076
2114
|
w.failed = JSON_WRITE_FULL
|
|
2077
2115
|
return false
|
|
2078
2116
|
}
|
|
@@ -2080,14 +2118,14 @@ const jsonWriterPutDigits64 = (w: JsonWriter, v: i64): boolean => {
|
|
|
2080
2118
|
buf[at] = toU8(JSON_MINUS)
|
|
2081
2119
|
}
|
|
2082
2120
|
// The last byte of the number, as `jsonWriterPutDigits` finds it.
|
|
2083
|
-
let k: i32 =
|
|
2121
|
+
let k: i32 = end - 1
|
|
2084
2122
|
for (let left: i32 = digits; left > 0 && k >= 0 && k < toI32(buf.length); left -= 1) {
|
|
2085
2123
|
const digit: u64 = rest % ten
|
|
2086
2124
|
buf[k] = toU8(toU32(JSON_DIGIT_ZERO) + toU32(digit))
|
|
2087
2125
|
rest = (rest - digit) / ten
|
|
2088
2126
|
k -= 1
|
|
2089
2127
|
}
|
|
2090
|
-
w.end =
|
|
2128
|
+
w.end = end
|
|
2091
2129
|
return true
|
|
2092
2130
|
}
|
|
2093
2131
|
|
|
@@ -2200,11 +2238,8 @@ const jsonPutStringWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, to
|
|
|
2200
2238
|
* writes again from `at`.
|
|
2201
2239
|
*/
|
|
2202
2240
|
export const jsonPutString = (dst: u8[], at: i32, src: readonly u8[], from: i32, len: i32): i32 => {
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
}
|
|
2206
|
-
// In u32, which cannot wrap: the window was checked to lie inside `src`.
|
|
2207
|
-
return jsonPutStringWalk(dst, at, src, from, toI32(toU32(from) + toU32(len)))
|
|
2241
|
+
const to: i32 = jsonSpanEnd(toI32(src.length), from, len)
|
|
2242
|
+
return to < 0 ? -1 : jsonPutStringWalk(dst, at, src, from, to)
|
|
2208
2243
|
}
|
|
2209
2244
|
|
|
2210
2245
|
/** The digits `${v}` spells for a finite `v`, at the cursor; all of them or, with `JSON_WRITE_FULL`, none. */
|
package/std/net/buffer-pool.ts
CHANGED
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
* what preallocating every slot would have cost, so a full server behaves as
|
|
19
19
|
* it did before and a `take` can never fail for want of memory. Reaching it
|
|
20
20
|
* therefore means some buffer was not given back, and `take` panics rather
|
|
21
|
-
* than allocating past it
|
|
21
|
+
* than allocating past it; `tryTake` answers `null` there, for a borrower
|
|
22
|
+
* that can refuse its own work.
|
|
22
23
|
* - **Give-back zeroes what was used.** The borrower says how far into the
|
|
23
24
|
* buffer it wrote, and `give` zeroes `[0, used)` before the buffer goes on
|
|
24
25
|
* the free list, so nothing one connection wrote is readable by the next one
|
|
@@ -72,21 +73,15 @@ export class BufferPool {
|
|
|
72
73
|
/**
|
|
73
74
|
* An empty pool: it makes nothing until the first `take`. `name` is what
|
|
74
75
|
* its panics print (the owner's type and field reads best,
|
|
75
|
-
* `"Http2TlsServer.output"`).
|
|
76
|
-
*
|
|
77
|
-
*
|
|
76
|
+
* `"Http2TlsServer.output"`). A `size` of 0 lends empty buffers. A
|
|
77
|
+
* `ceiling` at or below 0, or a `size` below 0, lends none: every
|
|
78
|
+
* `tryTake` answers `null`, and `take` panics as it does at the ceiling.
|
|
78
79
|
*
|
|
79
80
|
* @param name the label every panic of this pool prints, to say whose buffer leaked
|
|
80
81
|
* @param size how many bytes each buffer holds, the same for every buffer the pool lends
|
|
81
82
|
* @param ceiling the most buffers the pool may ever make, sized to what every slot holding one at once would need
|
|
82
83
|
*/
|
|
83
84
|
constructor(name: string, size: i32, ceiling: i32) {
|
|
84
|
-
if (size <= 0) {
|
|
85
|
-
panic(`BufferPool ${name}: a buffer size of ${size} bytes is not above 0`)
|
|
86
|
-
}
|
|
87
|
-
if (ceiling < 1) {
|
|
88
|
-
panic(`BufferPool ${name}: a ceiling of ${ceiling} buffers is below 1`)
|
|
89
|
-
}
|
|
90
85
|
this.name = name
|
|
91
86
|
this.size = size
|
|
92
87
|
this.ceiling = ceiling
|
|
@@ -101,11 +96,26 @@ export class BufferPool {
|
|
|
101
96
|
*
|
|
102
97
|
* Panics when all `ceiling` buffers are lent: the ceiling is what every
|
|
103
98
|
* slot holding a buffer at once would need, so one more means a buffer was
|
|
104
|
-
* never given back.
|
|
99
|
+
* never given back. `tryTake` answers `null` there instead.
|
|
105
100
|
*
|
|
106
101
|
* @returns a buffer of `size` bytes the caller now borrows until it hands it back through `give` or `giveSecret`
|
|
107
102
|
*/
|
|
108
103
|
take(): u8[] {
|
|
104
|
+
const buf: u8[] | null = this.tryTake()
|
|
105
|
+
if (buf === null) {
|
|
106
|
+
panic(`BufferPool ${this.name}: all ${this.ceiling} buffers are lent, so one was never given back`)
|
|
107
|
+
}
|
|
108
|
+
return buf
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* `take` for a borrower that can refuse its own work: a buffer as `take`
|
|
113
|
+
* lends it, or `null` when all `ceiling` buffers are lent or `size` is
|
|
114
|
+
* below 0, with nothing made.
|
|
115
|
+
*
|
|
116
|
+
* @returns a buffer of `size` bytes the caller now borrows, as from `take`, or `null` when the pool has none to lend
|
|
117
|
+
*/
|
|
118
|
+
tryTake(): u8[] | null {
|
|
109
119
|
const free: u8[][] = this.free
|
|
110
120
|
const count: i32 = toI32(free.length)
|
|
111
121
|
// The same test twice: the prover reads the first as covering the index
|
|
@@ -119,11 +129,12 @@ export class BufferPool {
|
|
|
119
129
|
// the ceiling once the test has passed.
|
|
120
130
|
const made: i32 = this.made
|
|
121
131
|
const ceiling: i32 = this.ceiling
|
|
122
|
-
|
|
123
|
-
|
|
132
|
+
const size: i32 = this.size
|
|
133
|
+
if (made >= ceiling || size < 0) {
|
|
134
|
+
return null
|
|
124
135
|
}
|
|
125
136
|
this.made = made + 1
|
|
126
|
-
return new Array<u8>(
|
|
137
|
+
return new Array<u8>(size)
|
|
127
138
|
}
|
|
128
139
|
|
|
129
140
|
/**
|
package/std/net/dns-config.ts
CHANGED
|
@@ -445,7 +445,14 @@ const dnsParseAddressVia = (
|
|
|
445
445
|
p += 1
|
|
446
446
|
k += 1
|
|
447
447
|
}
|
|
448
|
-
|
|
448
|
+
// Never taken (`at <= out.length - 18` was checked above): the port's window is
|
|
449
|
+
// tested again here, after the last call, because that is where `setU16BE` proves it from.
|
|
450
|
+
const portAt: i32 = at + 16
|
|
451
|
+
const portBits: u16 = toU16(port)
|
|
452
|
+
if (portAt < 0 || portAt > toI32(out.length) - 2) {
|
|
453
|
+
return false
|
|
454
|
+
}
|
|
455
|
+
setU16BE(out, portAt, portBits)
|
|
449
456
|
return true
|
|
450
457
|
}
|
|
451
458
|
|
|
@@ -736,9 +743,11 @@ const dnsFallbackServers = (servers: u8[]): void => {
|
|
|
736
743
|
servers[11] = toU8(255)
|
|
737
744
|
servers[12] = toU8(127)
|
|
738
745
|
servers[15] = toU8(1)
|
|
746
|
+
servers[16] = toU8(DNS_PORT >> 8)
|
|
747
|
+
servers[17] = toU8(DNS_PORT & 255)
|
|
739
748
|
servers[33] = toU8(1)
|
|
740
|
-
|
|
741
|
-
|
|
749
|
+
servers[34] = toU8(DNS_PORT >> 8)
|
|
750
|
+
servers[35] = toU8(DNS_PORT & 255)
|
|
742
751
|
}
|
|
743
752
|
|
|
744
753
|
/** Adds `b[from, to)` as a name of entry `entry`, lowercase, one trailing dot dropped, or counts it dropped. */
|
|
@@ -988,7 +997,11 @@ export const dnsHostsFind = (
|
|
|
988
997
|
}
|
|
989
998
|
const base: i32 = found * DNS_ADDRESS_BYTES
|
|
990
999
|
const outRoom: i32 = toI32(out.length) - DNS_ADDRESS_BYTES
|
|
991
|
-
|
|
1000
|
+
// The port's two bytes, `out[base + 16, base + 18)`, tested as the window
|
|
1001
|
+
// `setU16BE` proves from: nothing between here and that call calls anything.
|
|
1002
|
+
const portAt: i32 = base + 16
|
|
1003
|
+
const portBits: u16 = toU16(port)
|
|
1004
|
+
if (base > outRoom || portAt > toI32(out.length) - 2) {
|
|
992
1005
|
panic("dnsHostsFind: out has no room for max addresses")
|
|
993
1006
|
}
|
|
994
1007
|
let q: i32 = base
|
|
@@ -1000,7 +1013,7 @@ export const dnsHostsFind = (
|
|
|
1000
1013
|
r += 1
|
|
1001
1014
|
k += 1
|
|
1002
1015
|
}
|
|
1003
|
-
setU16BE(out,
|
|
1016
|
+
setU16BE(out, portAt, portBits)
|
|
1004
1017
|
found += 1
|
|
1005
1018
|
last = entry
|
|
1006
1019
|
}
|
package/std/net/dns-message.ts
CHANGED
|
@@ -55,7 +55,6 @@
|
|
|
55
55
|
*
|
|
56
56
|
* The record of what this module refuses and why is `docs/security/dns.md`.
|
|
57
57
|
*/
|
|
58
|
-
import { getU16BE, getU32BE, setU16BE } from "nish/bytes"
|
|
59
58
|
|
|
60
59
|
/** The record type of an IPv4 address (RFC 1035 §3.2.2). */
|
|
61
60
|
export const DNS_TYPE_A: i32 = 1
|
|
@@ -195,6 +194,14 @@ const dnsCheckLength = (bytes: u8[], len: i32, who: string): void => {
|
|
|
195
194
|
}
|
|
196
195
|
}
|
|
197
196
|
|
|
197
|
+
/**
|
|
198
|
+
* The big-endian u16 whose high byte is `hi` and low byte `lo`. The message
|
|
199
|
+
* reads index their bytes in place, where the checks before them prove them,
|
|
200
|
+
* rather than call `getU16BE`, whose window check would need a test after
|
|
201
|
+
* every call before it.
|
|
202
|
+
*/
|
|
203
|
+
const dnsBe16 = (hi: u8, lo: u8): u16 => (toU16(hi) << 8) | toU16(lo)
|
|
204
|
+
|
|
198
205
|
/** `b` with A to Z read as a to z, and every other byte as it stands. */
|
|
199
206
|
const dnsLower = (b: i32): i32 => (b >= 65 && b <= 90 ? b + 32 : b)
|
|
200
207
|
|
|
@@ -240,15 +247,23 @@ export const dnsBuildQuery = (out: u8[], id: i32, name: string, qtype: i32): i32
|
|
|
240
247
|
return DNS_ERR_NAME_LONG
|
|
241
248
|
}
|
|
242
249
|
const total: i32 = DNS_HEADER_BYTES + n + 2 + 4
|
|
243
|
-
if (total > toI32(out.length)) {
|
|
250
|
+
if (total > toI32(out.length) || toI32(out.length) < DNS_HEADER_BYTES) {
|
|
244
251
|
return DNS_ERR_SPACE
|
|
245
252
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
253
|
+
// The header's six big-endian u16s, `total` (at least 19) bytes having been
|
|
254
|
+
// checked: ID, flags (RD), QDCOUNT 1 and three zero counts.
|
|
255
|
+
out[0] = toU8(id >> 8)
|
|
256
|
+
out[1] = toU8(id & 255)
|
|
257
|
+
out[2] = toU8(DNS_FLAG_RD >> 8)
|
|
258
|
+
out[3] = toU8(DNS_FLAG_RD & 255)
|
|
259
|
+
out[4] = toU8(0)
|
|
260
|
+
out[5] = toU8(1)
|
|
261
|
+
out[6] = toU8(0)
|
|
262
|
+
out[7] = toU8(0)
|
|
263
|
+
out[8] = toU8(0)
|
|
264
|
+
out[9] = toU8(0)
|
|
265
|
+
out[10] = toU8(0)
|
|
266
|
+
out[11] = toU8(0)
|
|
252
267
|
// The bytes of the label being written so far, held at 64 once it is past
|
|
253
268
|
// `DNS_MAX_LABEL_BYTES`: its length byte goes just before them.
|
|
254
269
|
let label: i32 = 0
|
|
@@ -289,8 +304,15 @@ export const dnsBuildQuery = (out: u8[], id: i32, name: string, qtype: i32): i32
|
|
|
289
304
|
return DNS_ERR_SPACE
|
|
290
305
|
}
|
|
291
306
|
out[rootAt] = toU8(0)
|
|
292
|
-
|
|
293
|
-
|
|
307
|
+
// QTYPE then QCLASS IN, the question's last four bytes.
|
|
308
|
+
const typeAt: i32 = rootAt + 1
|
|
309
|
+
if (typeAt > toI32(out.length) - 4) {
|
|
310
|
+
return DNS_ERR_SPACE
|
|
311
|
+
}
|
|
312
|
+
out[typeAt] = toU8(qtype >> 8)
|
|
313
|
+
out[typeAt + 1] = toU8(qtype & 255)
|
|
314
|
+
out[typeAt + 2] = toU8(DNS_CLASS_IN >> 8)
|
|
315
|
+
out[typeAt + 3] = toU8(DNS_CLASS_IN & 255)
|
|
294
316
|
return total
|
|
295
317
|
}
|
|
296
318
|
|
|
@@ -479,7 +501,9 @@ const dnsKeepAddress = (msg: u8[], at: i32, size: i32, port: i32, ttl: i32, repl
|
|
|
479
501
|
addresses[to] = msg[from]
|
|
480
502
|
k += 1
|
|
481
503
|
}
|
|
482
|
-
|
|
504
|
+
// The port, big-endian, in the address's last two bytes.
|
|
505
|
+
addresses[base + 16] = toU8((port >> 8) & 255)
|
|
506
|
+
addresses[base + 17] = toU8(port & 255)
|
|
483
507
|
reply.count = count + 1
|
|
484
508
|
return true
|
|
485
509
|
}
|
|
@@ -534,25 +558,27 @@ export const dnsParseReply = (
|
|
|
534
558
|
reply.dropped = 0
|
|
535
559
|
reply.rcode = 0
|
|
536
560
|
reply.truncated = false
|
|
537
|
-
|
|
561
|
+
// `len` lies in `msg` (checked above), so the second test is never taken: it
|
|
562
|
+
// is the one the reads below index from.
|
|
563
|
+
if (len < 12 || toI32(msg.length) < 12) {
|
|
538
564
|
return DNS_ERR_HEADER
|
|
539
565
|
}
|
|
540
|
-
const flags: i32 = toI32(
|
|
566
|
+
const flags: i32 = toI32(dnsBe16(msg[2], msg[3]))
|
|
541
567
|
if ((flags & DNS_FLAG_QR) === 0) {
|
|
542
568
|
return DNS_ERR_NOT_REPLY
|
|
543
569
|
}
|
|
544
570
|
if (((flags >> 11) & 15) !== 0) {
|
|
545
571
|
return DNS_ERR_OPCODE
|
|
546
572
|
}
|
|
547
|
-
if (queryLen < 12 ||
|
|
573
|
+
if (queryLen < 12 || toI32(query.length) < 12 || dnsBe16(msg[0], msg[1]) !== dnsBe16(query[0], query[1])) {
|
|
548
574
|
return DNS_ERR_ID
|
|
549
575
|
}
|
|
550
|
-
if (
|
|
576
|
+
if (dnsBe16(msg[4], msg[5]) !== toU16(1)) {
|
|
551
577
|
return DNS_ERR_QUESTION
|
|
552
578
|
}
|
|
553
579
|
const asked: i32 = dnsReadName(query, queryLen, DNS_HEADER_BYTES, reply.asked)
|
|
554
580
|
// `DNS_QUERY_MAX_BYTES` (271) holds any query dnsBuildQuery writes.
|
|
555
|
-
if (asked < 0 || asked > queryLen - 4 || asked > 267) {
|
|
581
|
+
if (asked < 0 || asked > queryLen - 4 || asked > toI32(query.length) - 4 || asked > 267) {
|
|
556
582
|
panic("dnsParseReply: the query is not one dnsBuildQuery wrote")
|
|
557
583
|
}
|
|
558
584
|
const named: i32 = dnsReadName(msg, len, DNS_HEADER_BYTES, reply.chain)
|
|
@@ -560,17 +586,19 @@ export const dnsParseReply = (
|
|
|
560
586
|
return named
|
|
561
587
|
}
|
|
562
588
|
// dnsReadName reads no name past 65535, the longest a message can be.
|
|
563
|
-
if (named > len - 4 || named > 65535) {
|
|
589
|
+
if (named > len - 4 || named > toI32(msg.length) - 4 || named > 65535) {
|
|
564
590
|
return DNS_ERR_SHORT
|
|
565
591
|
}
|
|
566
592
|
if (
|
|
567
593
|
!dnsSameName(reply.chain, reply.asked) ||
|
|
568
|
-
|
|
569
|
-
|
|
594
|
+
msg[named] !== query[asked] ||
|
|
595
|
+
msg[named + 1] !== query[asked + 1] ||
|
|
596
|
+
msg[named + 2] !== query[asked + 2] ||
|
|
597
|
+
msg[named + 3] !== query[asked + 3]
|
|
570
598
|
) {
|
|
571
599
|
return DNS_ERR_QUESTION
|
|
572
600
|
}
|
|
573
|
-
const qtype: i32 = toI32(
|
|
601
|
+
const qtype: i32 = toI32(dnsBe16(query[asked], query[asked + 1]))
|
|
574
602
|
reply.truncated = (flags & DNS_FLAG_TC) !== 0
|
|
575
603
|
const rcode: i32 = flags & 15
|
|
576
604
|
reply.rcode = rcode
|
|
@@ -583,14 +611,14 @@ export const dnsParseReply = (
|
|
|
583
611
|
return rcode
|
|
584
612
|
}
|
|
585
613
|
let pos: i32 = named + 4
|
|
586
|
-
const
|
|
614
|
+
const answers: i32 = toI32(dnsBe16(msg[6], msg[7]))
|
|
615
|
+
const records: i32 = answers + toI32(dnsBe16(msg[8], msg[9])) + toI32(dnsBe16(msg[10], msg[11]))
|
|
587
616
|
if (records > (len - pos) / DNS_MIN_RECORD_BYTES) {
|
|
588
617
|
return DNS_ERR_COUNT
|
|
589
618
|
}
|
|
590
619
|
if (rcode !== DNS_NOERROR) {
|
|
591
620
|
return rcode
|
|
592
621
|
}
|
|
593
|
-
const answers: i32 = toI32(getU16BE(msg, 6))
|
|
594
622
|
let dropped: i32 = 0
|
|
595
623
|
let i: i32 = 0
|
|
596
624
|
while (i < answers) {
|
|
@@ -598,13 +626,18 @@ export const dnsParseReply = (
|
|
|
598
626
|
if (fixed < 0) {
|
|
599
627
|
return fixed
|
|
600
628
|
}
|
|
601
|
-
if (fixed > len - 10 || fixed > 65535) {
|
|
629
|
+
if (fixed > len - 10 || fixed > toI32(msg.length) - 10 || fixed > 65535) {
|
|
602
630
|
return DNS_ERR_SHORT
|
|
603
631
|
}
|
|
604
|
-
const type: i32 = toI32(
|
|
605
|
-
const klass: i32 = toI32(
|
|
606
|
-
const
|
|
607
|
-
|
|
632
|
+
const type: i32 = toI32(dnsBe16(msg[fixed], msg[fixed + 1]))
|
|
633
|
+
const klass: i32 = toI32(dnsBe16(msg[fixed + 2], msg[fixed + 3]))
|
|
634
|
+
const ttlBits: u32 =
|
|
635
|
+
(toU32(msg[fixed + 4]) << 24) |
|
|
636
|
+
(toU32(msg[fixed + 5]) << 16) |
|
|
637
|
+
(toU32(msg[fixed + 6]) << 8) |
|
|
638
|
+
toU32(msg[fixed + 7])
|
|
639
|
+
const rawTtl: i32 = toI32(ttlBits)
|
|
640
|
+
const size: i32 = toI32(dnsBe16(msg[fixed + 8], msg[fixed + 9]))
|
|
608
641
|
const data: i32 = fixed + 10
|
|
609
642
|
if (size > len - data) {
|
|
610
643
|
return DNS_ERR_RDATA
|