nish 0.42.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.
- package/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/skills/writing-nish/SKILL.md +12 -4
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/quic-frame.ts
CHANGED
|
@@ -177,6 +177,9 @@ export const QUIC_ERROR_CRYPTO: i64 = 0x0100
|
|
|
177
177
|
*/
|
|
178
178
|
export const QUIC_FRAME_MAX_ACK_RANGES: i32 = 32
|
|
179
179
|
|
|
180
|
+
/** The `ackRanges` slots: a smallest and a largest per range. */
|
|
181
|
+
const QUIC_FRAME_ACK_SLOTS: i32 = QUIC_FRAME_MAX_ACK_RANGES * 2
|
|
182
|
+
|
|
180
183
|
/** A stream count's ceiling, 2^60 (RFC 9000 §4.6, §19.11). Spelled as a product: an `i64` literal past 2^53 is refused. */
|
|
181
184
|
const QUIC_FRAME_MAX_STREAMS: i64 = 1073741824 * 1073741824
|
|
182
185
|
|
|
@@ -275,7 +278,7 @@ export class QuicFrame {
|
|
|
275
278
|
*/
|
|
276
279
|
constructor() {
|
|
277
280
|
// Sized once, so reading an ACK stores nothing new into the frame.
|
|
278
|
-
this.ackRanges = new Array<i64>(
|
|
281
|
+
this.ackRanges = new Array<i64>(QUIC_FRAME_ACK_SLOTS)
|
|
279
282
|
}
|
|
280
283
|
}
|
|
281
284
|
|
|
@@ -311,7 +314,8 @@ const quicFrameVarint = (r: QuicFrameReader, d: u8[]): i64 => {
|
|
|
311
314
|
r.failed = true
|
|
312
315
|
return 0
|
|
313
316
|
}
|
|
314
|
-
|
|
317
|
+
// The varint was read inside the window, so its end fits.
|
|
318
|
+
r.at = toI32(toI64(r.at) + toI64(quicVarintLength(d, r.at)))
|
|
315
319
|
return value
|
|
316
320
|
}
|
|
317
321
|
|
|
@@ -321,21 +325,24 @@ const quicFrameVarint = (r: QuicFrameReader, d: u8[]): i64 => {
|
|
|
321
325
|
*/
|
|
322
326
|
const quicFrameSkip = (r: QuicFrameReader, length: i64): i32 => {
|
|
323
327
|
const at: i32 = r.at
|
|
324
|
-
if (r.failed || length < 0 || length > toI64(r.end - at)) {
|
|
328
|
+
if (r.failed || length < 0 || length > toI64(r.end) - toI64(at)) {
|
|
325
329
|
r.failed = true
|
|
326
330
|
return at
|
|
327
331
|
}
|
|
328
|
-
|
|
332
|
+
// `length` is at most `end - at`, so it and the new position fit.
|
|
333
|
+
r.at = toI32(toI64(at) + toI64(toI32(length)))
|
|
329
334
|
return at
|
|
330
335
|
}
|
|
331
336
|
|
|
332
337
|
/** Stores the `index`-th ACK range pair; the caller keeps `index` under `QUIC_FRAME_MAX_ACK_RANGES`. */
|
|
333
338
|
const quicFrameStoreRange = (frame: QuicFrame, index: i32, smallest: i64, largest: i64): void => {
|
|
334
|
-
|
|
339
|
+
// `index` is under `QUIC_FRAME_MAX_ACK_RANGES`, so both slots fit an i32.
|
|
340
|
+
const at: i32 = toI32(toI64(index) * 2)
|
|
341
|
+
const next: i32 = toI32(toI64(at) + 1)
|
|
335
342
|
const ranges: i64[] = frame.ackRanges
|
|
336
|
-
if (at >= 0 && at
|
|
343
|
+
if (at >= 0 && at < toI32(ranges.length) && next >= 0 && next < toI32(ranges.length)) {
|
|
337
344
|
ranges[at] = smallest
|
|
338
|
-
ranges[
|
|
345
|
+
ranges[next] = largest
|
|
339
346
|
}
|
|
340
347
|
}
|
|
341
348
|
|
|
@@ -377,7 +384,9 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
|
|
|
377
384
|
if (r.failed) {
|
|
378
385
|
return QUIC_ERROR_FRAME_ENCODING
|
|
379
386
|
}
|
|
380
|
-
|
|
387
|
+
// Varints are below 2^62, so the difference is exact in wrapping u64 and a
|
|
388
|
+
// negative answer means the range reached below zero.
|
|
389
|
+
let smallest: i64 = toI64(toU64(frame.largest) - toU64(first))
|
|
381
390
|
if (smallest < 0) {
|
|
382
391
|
return QUIC_ERROR_FRAME_ENCODING
|
|
383
392
|
}
|
|
@@ -385,26 +394,33 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
|
|
|
385
394
|
let stored: i32 = 1
|
|
386
395
|
// Each further range takes at least two bytes, so the loop is bounded by the
|
|
387
396
|
// payload whatever count the peer claims: a short read ends it.
|
|
388
|
-
for (let k:
|
|
397
|
+
for (let k: u64 = 0; k < toU64(count); k += 1) {
|
|
389
398
|
const gap: i64 = quicFrameVarint(r, d)
|
|
390
399
|
const length: i64 = quicFrameVarint(r, d)
|
|
391
400
|
if (r.failed) {
|
|
392
401
|
return QUIC_ERROR_FRAME_ENCODING
|
|
393
402
|
}
|
|
394
403
|
// The next range's largest is two below this one's smallest, less the gap.
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
404
|
+
// `smallest` is 0 or more and the varints below 2^62, so wrapping u64
|
|
405
|
+
// is exact and a negative answer is a range below zero.
|
|
406
|
+
const top: i64 = toI64(toU64(smallest) - toU64(gap) - 2)
|
|
407
|
+
if (top < 0) {
|
|
408
|
+
return QUIC_ERROR_FRAME_ENCODING
|
|
409
|
+
}
|
|
410
|
+
const bottom: i64 = toI64(toU64(top) - toU64(length))
|
|
411
|
+
if (bottom < 0) {
|
|
398
412
|
return QUIC_ERROR_FRAME_ENCODING
|
|
399
413
|
}
|
|
400
414
|
if (stored < QUIC_FRAME_MAX_ACK_RANGES) {
|
|
401
415
|
quicFrameStoreRange(frame, stored, bottom, top)
|
|
402
|
-
|
|
416
|
+
// Under `QUIC_FRAME_MAX_ACK_RANGES`, so the count fits.
|
|
417
|
+
stored = toI32(toI64(stored) + 1)
|
|
403
418
|
}
|
|
404
419
|
smallest = bottom
|
|
405
420
|
}
|
|
406
421
|
frame.ackRangeCount = stored
|
|
407
|
-
|
|
422
|
+
// `count` is a varint, below 2^62.
|
|
423
|
+
frame.ackRangeTotal = toI64(toU64(count) + 1)
|
|
408
424
|
if (ecn) {
|
|
409
425
|
frame.ect0 = quicFrameVarint(r, d)
|
|
410
426
|
frame.ect1 = quicFrameVarint(r, d)
|
|
@@ -423,8 +439,10 @@ const quicFrameReadData = (r: QuicFrameReader, frame: QuicFrame, length: i64): i
|
|
|
423
439
|
if (r.failed) {
|
|
424
440
|
return QUIC_ERROR_FRAME_ENCODING
|
|
425
441
|
}
|
|
426
|
-
|
|
427
|
-
|
|
442
|
+
// The skip held `length` inside the window.
|
|
443
|
+
const dataLength: i32 = toI32(length)
|
|
444
|
+
frame.dataLength = dataLength
|
|
445
|
+
return frame.offset > QUIC_MAX_VARINT - toI64(dataLength) ? QUIC_ERROR_FRAME_ENCODING : QUIC_ERROR_NO_ERROR
|
|
428
446
|
}
|
|
429
447
|
|
|
430
448
|
/** STREAM (§19.8): the type's three low bits say whether Offset and Length are present and whether FIN is set. */
|
|
@@ -434,7 +452,7 @@ const quicFrameReadStream = (r: QuicFrameReader, d: u8[], frame: QuicFrame, bits
|
|
|
434
452
|
frame.offset = quicFrameVarint(r, d)
|
|
435
453
|
}
|
|
436
454
|
// Without a Length the data runs to the end of the packet.
|
|
437
|
-
const length: i64 = (bits & 0x02) !== 0 ? quicFrameVarint(r, d) : toI64(r.end - r.at)
|
|
455
|
+
const length: i64 = (bits & 0x02) !== 0 ? quicFrameVarint(r, d) : toI64(r.end) - toI64(r.at)
|
|
438
456
|
frame.fin = (bits & 0x01) !== 0
|
|
439
457
|
if (r.failed) {
|
|
440
458
|
return QUIC_ERROR_FRAME_ENCODING
|
|
@@ -444,7 +462,7 @@ const quicFrameReadStream = (r: QuicFrameReader, d: u8[], frame: QuicFrame, bits
|
|
|
444
462
|
|
|
445
463
|
/** DATAGRAM (RFC 9221 §4): with a Length, that many bytes; without, the rest of the packet. */
|
|
446
464
|
const quicFrameReadDatagram = (r: QuicFrameReader, d: u8[], frame: QuicFrame, hasLength: boolean): i64 => {
|
|
447
|
-
const length: i64 = hasLength ? quicFrameVarint(r, d) : toI64(r.end - r.at)
|
|
465
|
+
const length: i64 = hasLength ? quicFrameVarint(r, d) : toI64(r.end) - toI64(r.at)
|
|
448
466
|
frame.fin = hasLength
|
|
449
467
|
frame.dataStart = quicFrameSkip(r, length)
|
|
450
468
|
if (r.failed) {
|
|
@@ -490,9 +508,11 @@ const quicFrameReadBody = (r: QuicFrameReader, d: u8[], frame: QuicFrame, type:
|
|
|
490
508
|
case QUIC_FRAME_PADDING: {
|
|
491
509
|
// Padding is a run of zero bytes; read it as one frame, so a packet of
|
|
492
510
|
// a thousand padding bytes is one call rather than a thousand.
|
|
493
|
-
|
|
494
|
-
|
|
511
|
+
let at: i32 = r.at
|
|
512
|
+
while (at >= 0 && at < r.end && at < toI32(d.length) && toI32(d[at]) === 0) {
|
|
513
|
+
at += 1
|
|
495
514
|
}
|
|
515
|
+
r.at = at
|
|
496
516
|
return QUIC_ERROR_NO_ERROR
|
|
497
517
|
}
|
|
498
518
|
case QUIC_FRAME_PING:
|
|
@@ -693,26 +713,46 @@ export const quicFrameAckEliciting = (type: i32): boolean =>
|
|
|
693
713
|
const quicFrameWindowFits = (bytes: u8[], from: i32, length: i32): boolean =>
|
|
694
714
|
from >= 0 && length >= 0 && from <= toI32(bytes.length) - length
|
|
695
715
|
|
|
696
|
-
/** Whether `
|
|
697
|
-
const quicFrameRoom = (buf: u8[], at: i32, end: i32,
|
|
698
|
-
|
|
716
|
+
/** Whether a frame of `header` bytes and `length` more from `at` fits before `end` and inside `buf`. */
|
|
717
|
+
const quicFrameRoom = (buf: u8[], at: i32, end: i32, header: i32, length: i32): boolean =>
|
|
718
|
+
header > 0 &&
|
|
719
|
+
length >= 0 &&
|
|
720
|
+
at >= 0 &&
|
|
721
|
+
end <= toI32(buf.length) &&
|
|
722
|
+
toI64(header) + toI64(length) <= toI64(end) - toI64(at)
|
|
723
|
+
|
|
724
|
+
/** `at + count` for a cursor inside a frame `quicFrameRoom` fitted before `end`, so the sum fits. */
|
|
725
|
+
const quicFrameAdvance = (at: i32, count: i32): i32 => toI32(toI64(at) + toI64(count))
|
|
699
726
|
|
|
700
|
-
/**
|
|
727
|
+
/**
|
|
728
|
+
* Writes one byte and answers the offset past it. Every caller has checked the
|
|
729
|
+
* room with `quicFrameRoom` or made it with `quicFrameGrow`, so the bounds
|
|
730
|
+
* guard never skips the byte.
|
|
731
|
+
*/
|
|
701
732
|
const quicFramePutByte = (buf: u8[], at: i32, value: i32): i32 => {
|
|
702
733
|
if (at >= 0 && at < toI32(buf.length)) {
|
|
703
734
|
buf[at] = toU8(value)
|
|
704
735
|
}
|
|
705
|
-
return at
|
|
736
|
+
return quicFrameAdvance(at, 1)
|
|
706
737
|
}
|
|
707
738
|
|
|
708
739
|
/** Writes a varint in the fewest bytes; the caller has checked the room and the value. */
|
|
709
740
|
const quicFramePutVarint = (buf: u8[], at: i32, value: i64): i32 =>
|
|
710
741
|
quicVarintPut(buf, at, value, quicVarintSize(value))
|
|
711
742
|
|
|
712
|
-
/**
|
|
713
|
-
|
|
743
|
+
/**
|
|
744
|
+
* Grows `out` by `header` plus `length` bytes and answers where they start, for a `push` writer to
|
|
745
|
+
* `put` into; -1, growing nothing, for a size below one or past what an array holds, which every
|
|
746
|
+
* `put` writer refuses.
|
|
747
|
+
*/
|
|
748
|
+
const quicFrameGrow = (out: u8[], header: i32, length: i32): i32 => {
|
|
714
749
|
const at: i32 = toI32(out.length)
|
|
715
|
-
|
|
750
|
+
const size: i64 = toI64(header) + toI64(length)
|
|
751
|
+
if (header < 1 || length < 0 || size > toI64(2147483647) - toI64(at)) {
|
|
752
|
+
return -1
|
|
753
|
+
}
|
|
754
|
+
// Checked against the room the array has left just above.
|
|
755
|
+
quicPacketGrow(out, toI32(size))
|
|
716
756
|
return at
|
|
717
757
|
}
|
|
718
758
|
|
|
@@ -741,12 +781,14 @@ export const quicPushPadding = (out: u8[], count: i32): void => {
|
|
|
741
781
|
* `at` itself when nothing could be written.
|
|
742
782
|
*/
|
|
743
783
|
export const quicPutPadding = (buf: u8[], at: i32, count: i32): i32 => {
|
|
784
|
+
let cursor: i32 = at
|
|
744
785
|
let k: i32 = 0
|
|
745
|
-
while (k < count &&
|
|
746
|
-
buf[
|
|
786
|
+
while (k < count && cursor >= 0 && cursor < toI32(buf.length)) {
|
|
787
|
+
buf[cursor] = toU8(0)
|
|
788
|
+
cursor += 1
|
|
747
789
|
k += 1
|
|
748
790
|
}
|
|
749
|
-
return
|
|
791
|
+
return cursor
|
|
750
792
|
}
|
|
751
793
|
|
|
752
794
|
/**
|
|
@@ -759,7 +801,10 @@ export const quicPutPadding = (buf: u8[], at: i32, count: i32): i32 => {
|
|
|
759
801
|
* @returns `at + 1`, or -1 with nothing written.
|
|
760
802
|
*/
|
|
761
803
|
export const quicPutTypeOnly = (buf: u8[], at: i32, end: i32, type: i32): i32 => {
|
|
762
|
-
if (
|
|
804
|
+
if (
|
|
805
|
+
(type !== QUIC_FRAME_PING && type !== QUIC_FRAME_HANDSHAKE_DONE) ||
|
|
806
|
+
!quicFrameRoom(buf, at, end, 1, 0)
|
|
807
|
+
) {
|
|
763
808
|
return -1
|
|
764
809
|
}
|
|
765
810
|
return quicFramePutByte(buf, at, type)
|
|
@@ -776,8 +821,8 @@ export const quicPushTypeOnly = (out: u8[], type: i32): boolean => {
|
|
|
776
821
|
if (type !== QUIC_FRAME_PING && type !== QUIC_FRAME_HANDSHAKE_DONE) {
|
|
777
822
|
return false
|
|
778
823
|
}
|
|
779
|
-
const at: i32 = quicFrameGrow(out, 1)
|
|
780
|
-
return quicPutTypeOnly(out, at,
|
|
824
|
+
const at: i32 = quicFrameGrow(out, 1, 0)
|
|
825
|
+
return quicPutTypeOnly(out, at, toI32(out.length), type) >= 0
|
|
781
826
|
}
|
|
782
827
|
|
|
783
828
|
/**
|
|
@@ -788,17 +833,23 @@ export const quicPushTypeOnly = (out: u8[], type: i32): boolean => {
|
|
|
788
833
|
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
789
834
|
* @param rangeCount How many pairs to read from the front of `ranges`; the rest of the array is ignored.
|
|
790
835
|
* @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
|
|
791
|
-
* @returns The frame's size in bytes, or 0 when `quicPutAck` would refuse these arguments
|
|
836
|
+
* @returns The frame's size in bytes, or 0 when `quicPutAck` would refuse these arguments or the
|
|
837
|
+
* frame would pass 2^31 − 33 bytes.
|
|
792
838
|
*/
|
|
793
839
|
export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32 => {
|
|
794
|
-
if (
|
|
840
|
+
if (
|
|
841
|
+
rangeCount < 1 ||
|
|
842
|
+
toI64(rangeCount) * 2 > toI64(toI32(ranges.length)) ||
|
|
843
|
+
quicVarintSize(ackDelay) === 0
|
|
844
|
+
) {
|
|
795
845
|
return 0
|
|
796
846
|
}
|
|
797
847
|
let size: i32 = 1
|
|
798
848
|
let previousSmallest: i64 = 0
|
|
799
849
|
for (let k: i32 = 0; k < rangeCount; k += 1) {
|
|
800
|
-
|
|
801
|
-
const
|
|
850
|
+
// `k` is under `rangeCount`, which is at most half the array, so both slots fit.
|
|
851
|
+
const at: i32 = toI32(toI64(k) * 2)
|
|
852
|
+
const next: i32 = toI32(toI64(at) + 1)
|
|
802
853
|
if (at < 0 || at >= toI32(ranges.length) || next < 0 || next >= toI32(ranges.length)) {
|
|
803
854
|
return 0
|
|
804
855
|
}
|
|
@@ -807,17 +858,29 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
|
|
|
807
858
|
if (smallest < 0 || largest < smallest || largest > QUIC_MAX_VARINT) {
|
|
808
859
|
return 0
|
|
809
860
|
}
|
|
861
|
+
// A range adds at most 32 bytes, so refusing past 2^31 − 33 keeps the size
|
|
862
|
+
// an i32; no buffer holds a frame that large.
|
|
863
|
+
if (size > 2147483615) {
|
|
864
|
+
return 0
|
|
865
|
+
}
|
|
866
|
+
// `smallest` is 0 or more and at most `largest`, so the difference is exact.
|
|
867
|
+
const span: i64 = toI64(toU64(largest) - toU64(smallest))
|
|
810
868
|
if (k === 0) {
|
|
811
|
-
size =
|
|
869
|
+
size =
|
|
870
|
+
size +
|
|
871
|
+
quicVarintSize(largest) +
|
|
872
|
+
quicVarintSize(ackDelay) +
|
|
873
|
+
quicVarintSize(toI64(rangeCount - 1)) +
|
|
874
|
+
quicVarintSize(span)
|
|
812
875
|
} else {
|
|
813
|
-
// Gap: the packets missing between this range and the one above, less one (§19.3.1).
|
|
814
|
-
|
|
876
|
+
// Gap: the packets missing between this range and the one above, less one (§19.3.1). Both
|
|
877
|
+
// are 0 to 2^62 − 1, so wrapping u64 is exact and a negative gap is ranges out of order.
|
|
878
|
+
const gap: i64 = toI64(toU64(previousSmallest) - toU64(largest) - 2)
|
|
815
879
|
if (gap < 0) {
|
|
816
880
|
return 0
|
|
817
881
|
}
|
|
818
|
-
size = size + quicVarintSize(gap)
|
|
882
|
+
size = size + quicVarintSize(gap) + quicVarintSize(span)
|
|
819
883
|
}
|
|
820
|
-
size = size + quicVarintSize(largest - smallest)
|
|
821
884
|
previousSmallest = smallest
|
|
822
885
|
}
|
|
823
886
|
return size
|
|
@@ -836,8 +899,8 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
|
|
|
836
899
|
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
837
900
|
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
838
901
|
* @param ackDelay The ACK Delay field, already scaled: microseconds shifted right by the exponent.
|
|
839
|
-
* @returns The offset past the frame, or -1 with nothing written. The frame is
|
|
840
|
-
* counts.
|
|
902
|
+
* @returns The offset past the frame, or -1 with nothing written. The frame is type 0x02, without ECN
|
|
903
|
+
* counts; `quicPutAckEcn` writes the 0x03 frame that carries them.
|
|
841
904
|
*/
|
|
842
905
|
export const quicPutAck = (
|
|
843
906
|
buf: u8[],
|
|
@@ -848,24 +911,28 @@ export const quicPutAck = (
|
|
|
848
911
|
ackDelay: i64
|
|
849
912
|
): i32 => {
|
|
850
913
|
const size: i32 = quicAckSize(ranges, rangeCount, ackDelay)
|
|
851
|
-
if (!quicFrameRoom(buf, at, end, size)) {
|
|
914
|
+
if (!quicFrameRoom(buf, at, end, size, 0)) {
|
|
852
915
|
return -1
|
|
853
916
|
}
|
|
854
917
|
let cursor: i32 = quicFramePutByte(buf, at, QUIC_FRAME_ACK)
|
|
855
918
|
let previousSmallest: i64 = 0
|
|
856
919
|
for (let k: i32 = 0; k < rangeCount; k += 1) {
|
|
857
|
-
|
|
858
|
-
|
|
920
|
+
// `quicAckSize` held `rangeCount` to half the array and checked every
|
|
921
|
+
// pair, so the slots fit, the guard below never skips a pair, and the
|
|
922
|
+
// differences are exact in wrapping u64.
|
|
923
|
+
const pair: i32 = toI32(toI64(k) * 2)
|
|
924
|
+
const next: i32 = toI32(toI64(pair) + 1)
|
|
925
|
+
if (pair >= 0 && pair < toI32(ranges.length) && next >= 0 && next < toI32(ranges.length)) {
|
|
859
926
|
const smallest: i64 = ranges[pair]
|
|
860
|
-
const largest: i64 = ranges[
|
|
927
|
+
const largest: i64 = ranges[next]
|
|
861
928
|
if (k === 0) {
|
|
862
929
|
cursor = quicFramePutVarint(buf, cursor, largest)
|
|
863
930
|
cursor = quicFramePutVarint(buf, cursor, ackDelay)
|
|
864
|
-
cursor = quicFramePutVarint(buf, cursor, toI64(rangeCount - 1)
|
|
931
|
+
cursor = quicFramePutVarint(buf, cursor, toI64(rangeCount) - 1)
|
|
865
932
|
} else {
|
|
866
|
-
cursor = quicFramePutVarint(buf, cursor, previousSmallest - largest - 2)
|
|
933
|
+
cursor = quicFramePutVarint(buf, cursor, toI64(toU64(previousSmallest) - toU64(largest) - 2))
|
|
867
934
|
}
|
|
868
|
-
cursor = quicFramePutVarint(buf, cursor, largest - smallest)
|
|
935
|
+
cursor = quicFramePutVarint(buf, cursor, toI64(toU64(largest) - toU64(smallest)))
|
|
869
936
|
previousSmallest = smallest
|
|
870
937
|
}
|
|
871
938
|
}
|
|
@@ -886,8 +953,112 @@ export const quicPushAck = (out: u8[], ranges: i64[], rangeCount: i32, ackDelay:
|
|
|
886
953
|
if (size === 0) {
|
|
887
954
|
return false
|
|
888
955
|
}
|
|
889
|
-
const at: i32 = quicFrameGrow(out, size)
|
|
890
|
-
return quicPutAck(out, at,
|
|
956
|
+
const at: i32 = quicFrameGrow(out, size, 0)
|
|
957
|
+
return quicPutAck(out, at, toI32(out.length), ranges, rangeCount, ackDelay) >= 0
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* The size of the ACK_ECN frame `quicPutAckEcn` writes: the ACK frame
|
|
962
|
+
* `quicAckSize` sizes, then the three ECN counts. Answers 0 where
|
|
963
|
+
* `quicAckSize` does, or when a count is negative or above 2^62-1.
|
|
964
|
+
*
|
|
965
|
+
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
966
|
+
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
967
|
+
* @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
|
|
968
|
+
* @param ect0 The ECT(0) count: packets received in this space marked ECT(0).
|
|
969
|
+
* @param ect1 The ECT(1) count: packets received in this space marked ECT(1).
|
|
970
|
+
* @param ce The ECN-CE count: packets received in this space marked CE.
|
|
971
|
+
* @returns The frame's size in bytes, or 0 when `quicPutAckEcn` would refuse these arguments or the
|
|
972
|
+
* frame would pass 2^31 − 1 bytes.
|
|
973
|
+
*/
|
|
974
|
+
export const quicAckEcnSize = (
|
|
975
|
+
ranges: i64[],
|
|
976
|
+
rangeCount: i32,
|
|
977
|
+
ackDelay: i64,
|
|
978
|
+
ect0: i64,
|
|
979
|
+
ect1: i64,
|
|
980
|
+
ce: i64
|
|
981
|
+
): i32 => {
|
|
982
|
+
const base: i32 = quicAckSize(ranges, rangeCount, ackDelay)
|
|
983
|
+
const a: i32 = quicVarintSize(ect0)
|
|
984
|
+
const b: i32 = quicVarintSize(ect1)
|
|
985
|
+
const c: i32 = quicVarintSize(ce)
|
|
986
|
+
if (base === 0 || a === 0 || b === 0 || c === 0) {
|
|
987
|
+
return 0
|
|
988
|
+
}
|
|
989
|
+
const size: i64 = toI64(base) + toI64(a) + toI64(b) + toI64(c)
|
|
990
|
+
return size > 2147483647 ? 0 : toI32(size)
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Writes an ACK_ECN frame (type 0x03, RFC 9000 §19.3.2): the ranges and
|
|
995
|
+
* ACK Delay `quicPutAck` writes, then the ECT(0), ECT(1) and ECN-CE counts a
|
|
996
|
+
* receiver keeps for the space (§13.4.1). Answers -1, writing nothing, where
|
|
997
|
+
* `quicAckEcnSize` answers 0 or the frame does not fit.
|
|
998
|
+
*
|
|
999
|
+
* @param buf The buffer to write into.
|
|
1000
|
+
* @param at The offset of the frame's first byte.
|
|
1001
|
+
* @param end One past the last byte the frame may use.
|
|
1002
|
+
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
1003
|
+
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
1004
|
+
* @param ackDelay The ACK Delay field, already scaled: microseconds shifted right by the exponent.
|
|
1005
|
+
* @param ect0 The ECT(0) count.
|
|
1006
|
+
* @param ect1 The ECT(1) count.
|
|
1007
|
+
* @param ce The ECN-CE count.
|
|
1008
|
+
* @returns The offset past the frame, or -1 with nothing written.
|
|
1009
|
+
*/
|
|
1010
|
+
export const quicPutAckEcn = (
|
|
1011
|
+
buf: u8[],
|
|
1012
|
+
at: i32,
|
|
1013
|
+
end: i32,
|
|
1014
|
+
ranges: i64[],
|
|
1015
|
+
rangeCount: i32,
|
|
1016
|
+
ackDelay: i64,
|
|
1017
|
+
ect0: i64,
|
|
1018
|
+
ect1: i64,
|
|
1019
|
+
ce: i64
|
|
1020
|
+
): i32 => {
|
|
1021
|
+
const size: i32 = quicAckEcnSize(ranges, rangeCount, ackDelay, ect0, ect1, ce)
|
|
1022
|
+
if (!quicFrameRoom(buf, at, end, size, 0)) {
|
|
1023
|
+
return -1
|
|
1024
|
+
}
|
|
1025
|
+
let cursor: i32 = quicPutAck(buf, at, end, ranges, rangeCount, ackDelay)
|
|
1026
|
+
if (cursor < 0) {
|
|
1027
|
+
return -1
|
|
1028
|
+
}
|
|
1029
|
+
quicFramePutByte(buf, at, QUIC_FRAME_ACK_ECN)
|
|
1030
|
+
cursor = quicFramePutVarint(buf, cursor, ect0)
|
|
1031
|
+
cursor = quicFramePutVarint(buf, cursor, ect1)
|
|
1032
|
+
return quicFramePutVarint(buf, cursor, ce)
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* Appends the ACK_ECN frame `quicPutAckEcn` writes. Answers `false`, appending nothing, where it refuses.
|
|
1037
|
+
*
|
|
1038
|
+
* @param out The bytes to append the frame to.
|
|
1039
|
+
* @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
|
|
1040
|
+
* @param rangeCount How many pairs to read from the front of `ranges`.
|
|
1041
|
+
* @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
|
|
1042
|
+
* @param ect0 The ECT(0) count.
|
|
1043
|
+
* @param ect1 The ECT(1) count.
|
|
1044
|
+
* @param ce The ECN-CE count.
|
|
1045
|
+
* @returns True when the frame was appended.
|
|
1046
|
+
*/
|
|
1047
|
+
export const quicPushAckEcn = (
|
|
1048
|
+
out: u8[],
|
|
1049
|
+
ranges: i64[],
|
|
1050
|
+
rangeCount: i32,
|
|
1051
|
+
ackDelay: i64,
|
|
1052
|
+
ect0: i64,
|
|
1053
|
+
ect1: i64,
|
|
1054
|
+
ce: i64
|
|
1055
|
+
): boolean => {
|
|
1056
|
+
const size: i32 = quicAckEcnSize(ranges, rangeCount, ackDelay, ect0, ect1, ce)
|
|
1057
|
+
if (size === 0) {
|
|
1058
|
+
return false
|
|
1059
|
+
}
|
|
1060
|
+
const at: i32 = quicFrameGrow(out, size, 0)
|
|
1061
|
+
return quicPutAckEcn(out, at, toI32(out.length), ranges, rangeCount, ackDelay, ect0, ect1, ce) >= 0
|
|
891
1062
|
}
|
|
892
1063
|
|
|
893
1064
|
/**
|
|
@@ -929,7 +1100,7 @@ export const quicPutCrypto = (
|
|
|
929
1100
|
!quicFrameWindowFits(data, from, length) ||
|
|
930
1101
|
offset < 0 ||
|
|
931
1102
|
offset > QUIC_MAX_VARINT - toI64(length) ||
|
|
932
|
-
!quicFrameRoom(buf, at, end, quicCryptoOverhead(offset, length)
|
|
1103
|
+
!quicFrameRoom(buf, at, end, quicCryptoOverhead(offset, length), length)
|
|
933
1104
|
) {
|
|
934
1105
|
return -1
|
|
935
1106
|
}
|
|
@@ -937,7 +1108,7 @@ export const quicPutCrypto = (
|
|
|
937
1108
|
cursor = quicFramePutVarint(buf, cursor, offset)
|
|
938
1109
|
cursor = quicFramePutVarint(buf, cursor, toI64(length))
|
|
939
1110
|
quicPacketCopy(buf, cursor, data, from, length)
|
|
940
|
-
return cursor
|
|
1111
|
+
return quicFrameAdvance(cursor, length)
|
|
941
1112
|
}
|
|
942
1113
|
|
|
943
1114
|
/**
|
|
@@ -954,9 +1125,8 @@ export const quicPushCrypto = (out: u8[], offset: i64, data: u8[], from: i32, le
|
|
|
954
1125
|
if (!quicFrameWindowFits(data, from, length) || offset < 0 || offset > QUIC_MAX_VARINT - toI64(length)) {
|
|
955
1126
|
return false
|
|
956
1127
|
}
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
return quicPutCrypto(out, at, at + size, offset, data, from, length) >= 0
|
|
1128
|
+
const at: i32 = quicFrameGrow(out, quicCryptoOverhead(offset, length), length)
|
|
1129
|
+
return quicPutCrypto(out, at, toI32(out.length), offset, data, from, length) >= 0
|
|
960
1130
|
}
|
|
961
1131
|
|
|
962
1132
|
/**
|
|
@@ -970,7 +1140,9 @@ export const quicPushCrypto = (out: u8[], offset: i64, data: u8[], from: i32, le
|
|
|
970
1140
|
* @returns The header's size in bytes, not counting the data.
|
|
971
1141
|
*/
|
|
972
1142
|
export const quicStreamOverhead = (streamId: i64, offset: i64, length: i32): i32 =>
|
|
973
|
-
|
|
1143
|
+
offset === 0
|
|
1144
|
+
? 1 + quicVarintSize(streamId) + quicVarintSize(toI64(length))
|
|
1145
|
+
: 1 + quicVarintSize(streamId) + quicVarintSize(offset) + quicVarintSize(toI64(length))
|
|
974
1146
|
|
|
975
1147
|
/**
|
|
976
1148
|
* Writes a STREAM frame (§19.8) carrying `length` bytes of `data` on
|
|
@@ -1013,7 +1185,7 @@ export const quicPutStream = (
|
|
|
1013
1185
|
quicVarintSize(streamId) === 0 ||
|
|
1014
1186
|
offset < 0 ||
|
|
1015
1187
|
offset > QUIC_MAX_VARINT - toI64(length) ||
|
|
1016
|
-
!quicFrameRoom(buf, at, end, quicStreamOverhead(streamId, offset, length)
|
|
1188
|
+
!quicFrameRoom(buf, at, end, quicStreamOverhead(streamId, offset, length), length)
|
|
1017
1189
|
) {
|
|
1018
1190
|
return -1
|
|
1019
1191
|
}
|
|
@@ -1025,11 +1197,15 @@ export const quicPutStream = (
|
|
|
1025
1197
|
cursor = quicFramePutVarint(buf, cursor, offset)
|
|
1026
1198
|
}
|
|
1027
1199
|
cursor = quicFramePutVarint(buf, cursor, toI64(length))
|
|
1028
|
-
// Two runs: up to the end of the array, then from its start.
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1200
|
+
// Two runs: up to the end of the array, then from its start. With no data
|
|
1201
|
+
// `from` may lie past the array, and there is nothing to copy.
|
|
1202
|
+
if (length > 0) {
|
|
1203
|
+
// `from` is inside the array here, so `first` is 1 to `length`.
|
|
1204
|
+
const first: i32 = length < size - from ? length : size - from
|
|
1205
|
+
quicPacketCopy(buf, cursor, data, from, first)
|
|
1206
|
+
quicPacketCopy(buf, quicFrameAdvance(cursor, first), data, 0, toI32(toI64(length) - toI64(first)))
|
|
1207
|
+
}
|
|
1208
|
+
return quicFrameAdvance(cursor, length)
|
|
1033
1209
|
}
|
|
1034
1210
|
|
|
1035
1211
|
/**
|
|
@@ -1064,10 +1240,9 @@ export const quicPushStream = (
|
|
|
1064
1240
|
) {
|
|
1065
1241
|
return false
|
|
1066
1242
|
}
|
|
1067
|
-
const
|
|
1068
|
-
const at: i32 = quicFrameGrow(out, size)
|
|
1243
|
+
const at: i32 = quicFrameGrow(out, quicStreamOverhead(streamId, offset, length), length)
|
|
1069
1244
|
// A window inside the array is a ring that does not wrap.
|
|
1070
|
-
return quicPutStream(out, at,
|
|
1245
|
+
return quicPutStream(out, at, toI32(out.length), streamId, offset, data, from, length, fin) >= 0
|
|
1071
1246
|
}
|
|
1072
1247
|
|
|
1073
1248
|
/** Whether `type` is one of the frames `quicPutValue` writes, and `value` one it may carry. */
|
|
@@ -1100,7 +1275,7 @@ const quicFrameValueFits = (type: i32, value: i64): boolean => {
|
|
|
1100
1275
|
* @returns The offset past the frame, or -1 with nothing written.
|
|
1101
1276
|
*/
|
|
1102
1277
|
export const quicPutValue = (buf: u8[], at: i32, end: i32, type: i32, value: i64): i32 => {
|
|
1103
|
-
if (!quicFrameValueFits(type, value) || !quicFrameRoom(buf, at, end, 1 + quicVarintSize(value))) {
|
|
1278
|
+
if (!quicFrameValueFits(type, value) || !quicFrameRoom(buf, at, end, 1 + quicVarintSize(value), 0)) {
|
|
1104
1279
|
return -1
|
|
1105
1280
|
}
|
|
1106
1281
|
return quicFramePutVarint(buf, quicFramePutByte(buf, at, type), value)
|
|
@@ -1119,8 +1294,8 @@ export const quicPushValue = (out: u8[], type: i32, value: i64): boolean => {
|
|
|
1119
1294
|
return false
|
|
1120
1295
|
}
|
|
1121
1296
|
const size: i32 = 1 + quicVarintSize(value)
|
|
1122
|
-
const at: i32 = quicFrameGrow(out, size)
|
|
1123
|
-
return quicPutValue(out, at,
|
|
1297
|
+
const at: i32 = quicFrameGrow(out, size, 0)
|
|
1298
|
+
return quicPutValue(out, at, toI32(out.length), type, value) >= 0
|
|
1124
1299
|
}
|
|
1125
1300
|
|
|
1126
1301
|
/**
|
|
@@ -1148,7 +1323,7 @@ export const quicPutStreamValue = (
|
|
|
1148
1323
|
(type !== QUIC_FRAME_MAX_STREAM_DATA && type !== QUIC_FRAME_STREAM_DATA_BLOCKED) ||
|
|
1149
1324
|
quicVarintSize(streamId) === 0 ||
|
|
1150
1325
|
quicVarintSize(value) === 0 ||
|
|
1151
|
-
!quicFrameRoom(buf, at, end, 1 + quicVarintSize(streamId) + quicVarintSize(value))
|
|
1326
|
+
!quicFrameRoom(buf, at, end, 1 + quicVarintSize(streamId) + quicVarintSize(value), 0)
|
|
1152
1327
|
) {
|
|
1153
1328
|
return -1
|
|
1154
1329
|
}
|
|
@@ -1174,8 +1349,8 @@ export const quicPushStreamValue = (out: u8[], type: i32, streamId: i64, value:
|
|
|
1174
1349
|
return false
|
|
1175
1350
|
}
|
|
1176
1351
|
const size: i32 = 1 + quicVarintSize(streamId) + quicVarintSize(value)
|
|
1177
|
-
const at: i32 = quicFrameGrow(out, size)
|
|
1178
|
-
return quicPutStreamValue(out, at,
|
|
1352
|
+
const at: i32 = quicFrameGrow(out, size, 0)
|
|
1353
|
+
return quicPutStreamValue(out, at, toI32(out.length), type, streamId, value) >= 0
|
|
1179
1354
|
}
|
|
1180
1355
|
|
|
1181
1356
|
/** The size of a NEW_CONNECTION_ID frame, or 0 for arguments `quicPutNewConnectionId` refuses. */
|
|
@@ -1190,7 +1365,12 @@ const quicFrameNewCidSize = (sequence: i64, retirePriorTo: i64, cidLength: i32,
|
|
|
1190
1365
|
) {
|
|
1191
1366
|
return 0
|
|
1192
1367
|
}
|
|
1193
|
-
|
|
1368
|
+
// `cidLength` is 1 to 20 here, so the sum fits.
|
|
1369
|
+
return toI32(
|
|
1370
|
+
toI64(2 + quicVarintSize(sequence) + quicVarintSize(retirePriorTo)) +
|
|
1371
|
+
toI64(cidLength) +
|
|
1372
|
+
toI64(QUIC_RESET_TOKEN_SIZE)
|
|
1373
|
+
)
|
|
1194
1374
|
}
|
|
1195
1375
|
|
|
1196
1376
|
/**
|
|
@@ -1220,7 +1400,7 @@ export const quicPutNewConnectionId = (
|
|
|
1220
1400
|
resetToken: u8[]
|
|
1221
1401
|
): i32 => {
|
|
1222
1402
|
const size: i32 = quicFrameNewCidSize(sequence, retirePriorTo, cidLength, toI32(resetToken.length))
|
|
1223
|
-
if (size === 0 || cidLength > toI32(connectionId.length) || !quicFrameRoom(buf, at, end, size)) {
|
|
1403
|
+
if (size === 0 || cidLength > toI32(connectionId.length) || !quicFrameRoom(buf, at, end, size, 0)) {
|
|
1224
1404
|
return -1
|
|
1225
1405
|
}
|
|
1226
1406
|
let cursor: i32 = quicFramePutByte(buf, at, QUIC_FRAME_NEW_CONNECTION_ID)
|
|
@@ -1228,8 +1408,9 @@ export const quicPutNewConnectionId = (
|
|
|
1228
1408
|
cursor = quicFramePutVarint(buf, cursor, retirePriorTo)
|
|
1229
1409
|
cursor = quicFramePutByte(buf, cursor, cidLength)
|
|
1230
1410
|
quicPacketCopy(buf, cursor, connectionId, 0, cidLength)
|
|
1231
|
-
|
|
1232
|
-
|
|
1411
|
+
const tokenAt: i32 = quicFrameAdvance(cursor, cidLength)
|
|
1412
|
+
quicPacketCopy(buf, tokenAt, resetToken, 0, QUIC_RESET_TOKEN_SIZE)
|
|
1413
|
+
return quicFrameAdvance(tokenAt, QUIC_RESET_TOKEN_SIZE)
|
|
1233
1414
|
}
|
|
1234
1415
|
|
|
1235
1416
|
/**
|
|
@@ -1256,12 +1437,12 @@ export const quicPushNewConnectionId = (
|
|
|
1256
1437
|
if (size === 0) {
|
|
1257
1438
|
return false
|
|
1258
1439
|
}
|
|
1259
|
-
const at: i32 = quicFrameGrow(out, size)
|
|
1440
|
+
const at: i32 = quicFrameGrow(out, size, 0)
|
|
1260
1441
|
return (
|
|
1261
1442
|
quicPutNewConnectionId(
|
|
1262
1443
|
out,
|
|
1263
1444
|
at,
|
|
1264
|
-
|
|
1445
|
+
toI32(out.length),
|
|
1265
1446
|
sequence,
|
|
1266
1447
|
retirePriorTo,
|
|
1267
1448
|
connectionId,
|
|
@@ -1288,13 +1469,13 @@ export const quicPutPathData = (buf: u8[], at: i32, end: i32, type: i32, data: u
|
|
|
1288
1469
|
if (
|
|
1289
1470
|
(type !== QUIC_FRAME_PATH_CHALLENGE && type !== QUIC_FRAME_PATH_RESPONSE) ||
|
|
1290
1471
|
!quicFrameWindowFits(data, from, QUIC_PATH_DATA_SIZE) ||
|
|
1291
|
-
!quicFrameRoom(buf, at, end, 1 + QUIC_PATH_DATA_SIZE)
|
|
1472
|
+
!quicFrameRoom(buf, at, end, 1 + QUIC_PATH_DATA_SIZE, 0)
|
|
1292
1473
|
) {
|
|
1293
1474
|
return -1
|
|
1294
1475
|
}
|
|
1295
1476
|
const cursor: i32 = quicFramePutByte(buf, at, type)
|
|
1296
1477
|
quicPacketCopy(buf, cursor, data, from, QUIC_PATH_DATA_SIZE)
|
|
1297
|
-
return cursor
|
|
1478
|
+
return quicFrameAdvance(cursor, QUIC_PATH_DATA_SIZE)
|
|
1298
1479
|
}
|
|
1299
1480
|
|
|
1300
1481
|
/**
|
|
@@ -1313,8 +1494,20 @@ export const quicPushPathData = (out: u8[], type: i32, data: u8[], from: i32): b
|
|
|
1313
1494
|
) {
|
|
1314
1495
|
return false
|
|
1315
1496
|
}
|
|
1316
|
-
const at: i32 = quicFrameGrow(out, 1 + QUIC_PATH_DATA_SIZE)
|
|
1317
|
-
return quicPutPathData(out, at,
|
|
1497
|
+
const at: i32 = quicFrameGrow(out, 1 + QUIC_PATH_DATA_SIZE, 0)
|
|
1498
|
+
return quicPutPathData(out, at, toI32(out.length), type, data, from) >= 0
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
/** The size of a RESET_STREAM or, with `finalSize` at -1, STOP_SENDING frame, or 0 for a value no varint holds. */
|
|
1502
|
+
const quicFrameStreamErrorSize = (streamId: i64, errorCode: i64, finalSize: i64): i32 => {
|
|
1503
|
+
if (finalSize === -1) {
|
|
1504
|
+
return quicFrameVarints3(streamId, errorCode, 0)
|
|
1505
|
+
? 1 + quicVarintSize(streamId) + quicVarintSize(errorCode)
|
|
1506
|
+
: 0
|
|
1507
|
+
}
|
|
1508
|
+
return quicFrameVarints3(streamId, errorCode, finalSize)
|
|
1509
|
+
? 1 + quicVarintSize(streamId) + quicVarintSize(errorCode) + quicVarintSize(finalSize)
|
|
1510
|
+
: 0
|
|
1318
1511
|
}
|
|
1319
1512
|
|
|
1320
1513
|
/**
|
|
@@ -1339,12 +1532,8 @@ export const quicPutStreamError = (
|
|
|
1339
1532
|
finalSize: i64
|
|
1340
1533
|
): i32 => {
|
|
1341
1534
|
const reset: boolean = finalSize !== -1
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
}
|
|
1345
|
-
const size: i32 =
|
|
1346
|
-
1 + quicVarintSize(streamId) + quicVarintSize(errorCode) + (reset ? quicVarintSize(finalSize) : 0)
|
|
1347
|
-
if (!quicFrameRoom(buf, at, end, size)) {
|
|
1535
|
+
const size: i32 = quicFrameStreamErrorSize(streamId, errorCode, finalSize)
|
|
1536
|
+
if (!quicFrameRoom(buf, at, end, size, 0)) {
|
|
1348
1537
|
return -1
|
|
1349
1538
|
}
|
|
1350
1539
|
let cursor: i32 = quicFramePutByte(buf, at, reset ? QUIC_FRAME_RESET_STREAM : QUIC_FRAME_STOP_SENDING)
|
|
@@ -1366,28 +1555,30 @@ export const quicPutStreamError = (
|
|
|
1366
1555
|
* @returns True when the frame was appended.
|
|
1367
1556
|
*/
|
|
1368
1557
|
export const quicPushStreamError = (out: u8[], streamId: i64, errorCode: i64, finalSize: i64): boolean => {
|
|
1369
|
-
const
|
|
1370
|
-
if (
|
|
1558
|
+
const size: i32 = quicFrameStreamErrorSize(streamId, errorCode, finalSize)
|
|
1559
|
+
if (size === 0) {
|
|
1371
1560
|
return false
|
|
1372
1561
|
}
|
|
1373
|
-
const
|
|
1374
|
-
|
|
1375
|
-
const at: i32 = quicFrameGrow(out, size)
|
|
1376
|
-
return quicPutStreamError(out, at, at + size, streamId, errorCode, finalSize) >= 0
|
|
1562
|
+
const at: i32 = quicFrameGrow(out, size, 0)
|
|
1563
|
+
return quicPutStreamError(out, at, toI32(out.length), streamId, errorCode, finalSize) >= 0
|
|
1377
1564
|
}
|
|
1378
1565
|
|
|
1379
|
-
/**
|
|
1380
|
-
|
|
1566
|
+
/**
|
|
1567
|
+
* The size of a CONNECTION_CLOSE frame before its `reasonLength`-byte reason, or 0 for a value no
|
|
1568
|
+
* varint holds.
|
|
1569
|
+
*/
|
|
1570
|
+
const quicFrameCloseHeader = (
|
|
1571
|
+
application: boolean,
|
|
1572
|
+
errorCode: i64,
|
|
1573
|
+
frameType: i64,
|
|
1574
|
+
reasonLength: i32
|
|
1575
|
+
): i32 => {
|
|
1381
1576
|
if (!quicFrameVarints3(errorCode, application ? 0 : frameType, toI64(reasonLength))) {
|
|
1382
1577
|
return 0
|
|
1383
1578
|
}
|
|
1384
|
-
return
|
|
1385
|
-
1 +
|
|
1386
|
-
quicVarintSize(errorCode) +
|
|
1387
|
-
(application ? 0 : quicVarintSize(frameType)) +
|
|
1388
|
-
quicVarintSize(toI64(reasonLength)) +
|
|
1389
|
-
reasonLength
|
|
1390
|
-
)
|
|
1579
|
+
return application
|
|
1580
|
+
? 1 + quicVarintSize(errorCode) + quicVarintSize(toI64(reasonLength))
|
|
1581
|
+
: 1 + quicVarintSize(errorCode) + quicVarintSize(frameType) + quicVarintSize(toI64(reasonLength))
|
|
1391
1582
|
}
|
|
1392
1583
|
|
|
1393
1584
|
/**
|
|
@@ -1416,8 +1607,8 @@ export const quicPutConnectionClose = (
|
|
|
1416
1607
|
reason: u8[]
|
|
1417
1608
|
): i32 => {
|
|
1418
1609
|
const reasonLength: i32 = toI32(reason.length)
|
|
1419
|
-
const
|
|
1420
|
-
if (!quicFrameRoom(buf, at, end,
|
|
1610
|
+
const header: i32 = quicFrameCloseHeader(application, errorCode, frameType, reasonLength)
|
|
1611
|
+
if (!quicFrameRoom(buf, at, end, header, reasonLength)) {
|
|
1421
1612
|
return -1
|
|
1422
1613
|
}
|
|
1423
1614
|
let cursor: i32 = quicFramePutByte(
|
|
@@ -1431,7 +1622,7 @@ export const quicPutConnectionClose = (
|
|
|
1431
1622
|
}
|
|
1432
1623
|
cursor = quicFramePutVarint(buf, cursor, toI64(reasonLength))
|
|
1433
1624
|
quicPacketCopy(buf, cursor, reason, 0, reasonLength)
|
|
1434
|
-
return cursor
|
|
1625
|
+
return quicFrameAdvance(cursor, reasonLength)
|
|
1435
1626
|
}
|
|
1436
1627
|
|
|
1437
1628
|
/**
|
|
@@ -1452,12 +1643,13 @@ export const quicPushConnectionClose = (
|
|
|
1452
1643
|
frameType: i64,
|
|
1453
1644
|
reason: u8[]
|
|
1454
1645
|
): boolean => {
|
|
1455
|
-
const
|
|
1456
|
-
|
|
1646
|
+
const reasonLength: i32 = toI32(reason.length)
|
|
1647
|
+
const header: i32 = quicFrameCloseHeader(application, errorCode, frameType, reasonLength)
|
|
1648
|
+
if (header === 0) {
|
|
1457
1649
|
return false
|
|
1458
1650
|
}
|
|
1459
|
-
const at: i32 = quicFrameGrow(out,
|
|
1460
|
-
return quicPutConnectionClose(out, at,
|
|
1651
|
+
const at: i32 = quicFrameGrow(out, header, reasonLength)
|
|
1652
|
+
return quicPutConnectionClose(out, at, toI32(out.length), application, errorCode, frameType, reason) >= 0
|
|
1461
1653
|
}
|
|
1462
1654
|
|
|
1463
1655
|
/**
|
|
@@ -1466,15 +1658,19 @@ export const quicPushConnectionClose = (
|
|
|
1466
1658
|
*
|
|
1467
1659
|
* @param out The bytes to append the frame to.
|
|
1468
1660
|
* @param token The whole array is the token the client is to present later; copied, not kept.
|
|
1469
|
-
* @returns True when the frame was appended; false, appending nothing, for an empty token
|
|
1661
|
+
* @returns True when the frame was appended; false, appending nothing, for an empty token or
|
|
1662
|
+
* one too large to append to `out`.
|
|
1470
1663
|
*/
|
|
1471
1664
|
export const quicPushNewToken = (out: u8[], token: u8[]): boolean => {
|
|
1472
1665
|
const length: i32 = toI32(token.length)
|
|
1473
1666
|
if (length === 0) {
|
|
1474
1667
|
return false
|
|
1475
1668
|
}
|
|
1476
|
-
const
|
|
1477
|
-
|
|
1669
|
+
const at: i32 = quicFrameGrow(out, 1 + quicVarintSize(toI64(length)), length)
|
|
1670
|
+
if (at < 0) {
|
|
1671
|
+
return false
|
|
1672
|
+
}
|
|
1673
|
+
let cursor: i32 = quicFramePutByte(out, at, QUIC_FRAME_NEW_TOKEN)
|
|
1478
1674
|
cursor = quicFramePutVarint(out, cursor, toI64(length))
|
|
1479
1675
|
quicPacketCopy(out, cursor, token, 0, length)
|
|
1480
1676
|
return true
|
|
@@ -1485,10 +1681,14 @@ export const quicPushNewToken = (out: u8[], token: u8[]): boolean => {
|
|
|
1485
1681
|
* `length` bytes: the type, the Length and the payload. This is the size RFC
|
|
1486
1682
|
* 9221 §3 holds to `max_datagram_frame_size`.
|
|
1487
1683
|
*
|
|
1488
|
-
* @param length The payload's size in bytes.
|
|
1489
|
-
*
|
|
1684
|
+
* @param length The payload's size in bytes, 0 or more. A negative one is not refused: it
|
|
1685
|
+
* answers `1 + length`, which is not a frame's size.
|
|
1686
|
+
* @returns The whole frame's size in bytes, held at 2^31 − 1, a size no buffer has room for.
|
|
1490
1687
|
*/
|
|
1491
|
-
export const quicDatagramSize = (length: i32): i32 =>
|
|
1688
|
+
export const quicDatagramSize = (length: i32): i32 => {
|
|
1689
|
+
const size: i64 = toI64(1 + quicVarintSize(toI64(length))) + toI64(length)
|
|
1690
|
+
return size > toI64(2147483647) ? 2147483647 : toI32(size)
|
|
1691
|
+
}
|
|
1492
1692
|
|
|
1493
1693
|
/**
|
|
1494
1694
|
* Writes a DATAGRAM frame with a Length (type 0x31, RFC 9221 §4) carrying
|
|
@@ -1503,7 +1703,10 @@ export const quicDatagramSize = (length: i32): i32 => 1 + quicVarintSize(toI64(l
|
|
|
1503
1703
|
* @returns The offset past the frame, or -1 with nothing written.
|
|
1504
1704
|
*/
|
|
1505
1705
|
export const quicPutDatagram = (buf: u8[], at: i32, end: i32, data: u8[], from: i32, length: i32): i32 => {
|
|
1506
|
-
if (
|
|
1706
|
+
if (
|
|
1707
|
+
!quicFrameWindowFits(data, from, length) ||
|
|
1708
|
+
!quicFrameRoom(buf, at, end, 1 + quicVarintSize(toI64(length)), length)
|
|
1709
|
+
) {
|
|
1507
1710
|
return -1
|
|
1508
1711
|
}
|
|
1509
1712
|
const cursor: i32 = quicFramePutVarint(
|
|
@@ -1512,5 +1715,5 @@ export const quicPutDatagram = (buf: u8[], at: i32, end: i32, data: u8[], from:
|
|
|
1512
1715
|
toI64(length)
|
|
1513
1716
|
)
|
|
1514
1717
|
quicPacketCopy(buf, cursor, data, from, length)
|
|
1515
|
-
return cursor
|
|
1718
|
+
return quicFrameAdvance(cursor, length)
|
|
1516
1719
|
}
|