@amritk/nish-x86_64-linux 0.44.0 → 0.46.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- 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 +84 -61
- 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 +3 -4
- package/std/crypto/sha1.ts +107 -65
- 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/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 +1315 -222
- 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 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- 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 +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- 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 +50 -18
- 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 +225 -124
- package/std/net/quic-listener.ts +197 -89
- 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 +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- 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 +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +171 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
|
@@ -0,0 +1,1017 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/net/protobuf` — protobuf's wire format, without a schema: a reader
|
|
3
|
+
* that walks the fields of a message held in a `u8[]` span, and a writer that
|
|
4
|
+
* appends fields to a caller's buffer. It knows tags, the four wire types
|
|
5
|
+
* proto3 uses and the value encodings on top of them (varint, zigzag, the
|
|
6
|
+
* little-endian fixed widths, length-delimited spans); what a field number
|
|
7
|
+
* means is the caller's schema, not this module's.
|
|
8
|
+
*
|
|
9
|
+
* import { ProtoReader, ProtoWriter, PROTO_OK } from "nish/net/protobuf";
|
|
10
|
+
*
|
|
11
|
+
* const w = new ProtoWriter([]);
|
|
12
|
+
* w.uint64(1, toU64(150));
|
|
13
|
+
* w.string(2, "hi");
|
|
14
|
+
*
|
|
15
|
+
* const r = new ProtoReader(w.buf, 0, w.end);
|
|
16
|
+
* while (r.next()) {
|
|
17
|
+
* if (r.field === 1) {
|
|
18
|
+
* const id: u64 = r.uint64();
|
|
19
|
+
* } else if (r.field === 2) {
|
|
20
|
+
* const n: i32 = r.len(); // the bytes are w.buf[r.spanStart, r.spanStart + n)
|
|
21
|
+
* } // any other field is skipped by the next call to next()
|
|
22
|
+
* }
|
|
23
|
+
* if (r.failed !== PROTO_OK) {
|
|
24
|
+
* // a negative PROTO_ERR_* code: the message is malformed
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* **The reader refuses with a code, never a panic.** Peer bytes cannot make
|
|
28
|
+
* it read outside its span: every value is checked against the span's end
|
|
29
|
+
* before a byte of it is read. The first fault sets `failed` to a negative
|
|
30
|
+
* `PROTO_ERR_*` code, which is sticky: every later call answers `false` or
|
|
31
|
+
* zero until `reset`. It refuses a varint whose tenth byte still has its
|
|
32
|
+
* continuation bit, a varint or fixed value that runs off the span, a length
|
|
33
|
+
* past the span, a tag whose field number is 0 or whose varint is above
|
|
34
|
+
* 2^32 - 1 (so a field number past 2^29 - 1), the group wire types 3 and 4,
|
|
35
|
+
* the undefined wire types 6 and 7, an `enter` past `PROTO_MAX_DEPTH`
|
|
36
|
+
* nested spans, and a typed read whose wire type is not the field's.
|
|
37
|
+
*
|
|
38
|
+
* **A field the caller does not read is skipped by its wire type** when
|
|
39
|
+
* `next` is called again, so an unknown field is checked and passed, never
|
|
40
|
+
* interpreted. A varint's bits past the 64th (the tenth byte's top six) are
|
|
41
|
+
* dropped, and an over-long varint (`0x80 0x00` for 0) is read as
|
|
42
|
+
* its value; neither can move the reader outside its span.
|
|
43
|
+
*
|
|
44
|
+
* **The writer appends to the caller's buffer** from `end`, storing over the
|
|
45
|
+
* bytes already there and pushing past its length, so a buffer that has grown
|
|
46
|
+
* to a message's size is reused without allocating once the writer is `reset`.
|
|
47
|
+
* Misuse (a field number outside 1 to 2^29 - 1, nesting past
|
|
48
|
+
* `PROTO_MAX_DEPTH`, an `endMessage` or a packed element with no open
|
|
49
|
+
* message, a `bytes` window outside its source array, a message past
|
|
50
|
+
* `PROTO_MAX_SIZE` bytes) sets the same sticky `failed` code and writes nothing more. Neither side panics: a
|
|
51
|
+
* reader window outside its array is `PROTO_ERR_WINDOW` too, so a program
|
|
52
|
+
* held to no panic may call both.
|
|
53
|
+
*
|
|
54
|
+
* The record of what this module refuses and why is
|
|
55
|
+
* `docs/security/protobuf.md`.
|
|
56
|
+
*/
|
|
57
|
+
/** The varint wire type: int32, int64, uint32, uint64, sint32, sint64, bool and enum. */
|
|
58
|
+
export const PROTO_WIRE_VARINT: i32 = 0
|
|
59
|
+
/** The 64-bit wire type: fixed64, sfixed64 and double. */
|
|
60
|
+
export const PROTO_WIRE_I64: i32 = 1
|
|
61
|
+
/** The length-delimited wire type: string, bytes, an embedded message and a packed repeated field. */
|
|
62
|
+
export const PROTO_WIRE_LEN: i32 = 2
|
|
63
|
+
/** The start-group wire type, deprecated and refused. */
|
|
64
|
+
export const PROTO_WIRE_SGROUP: i32 = 3
|
|
65
|
+
/** The end-group wire type, deprecated and refused. */
|
|
66
|
+
export const PROTO_WIRE_EGROUP: i32 = 4
|
|
67
|
+
/** The 32-bit wire type: fixed32, sfixed32 and float. */
|
|
68
|
+
export const PROTO_WIRE_I32: i32 = 5
|
|
69
|
+
|
|
70
|
+
/** The largest field number, 2^29 - 1. */
|
|
71
|
+
export const PROTO_MAX_FIELD: i32 = 536870911
|
|
72
|
+
/** How many spans `ProtoReader.enter` and `ProtoWriter.beginMessage` may nest, protobuf's own default recursion limit. */
|
|
73
|
+
export const PROTO_MAX_DEPTH: i32 = 100
|
|
74
|
+
/** The largest message the writer writes, 2^31 - 2 bytes, so that its `end` fits an `i32`. */
|
|
75
|
+
export const PROTO_MAX_SIZE: i32 = 2147483646
|
|
76
|
+
|
|
77
|
+
/** No fault. */
|
|
78
|
+
export const PROTO_OK: i32 = 0
|
|
79
|
+
/** A varint's tenth byte has its continuation bit set: no varint is longer than ten bytes. */
|
|
80
|
+
export const PROTO_ERR_VARINT: i32 = -1
|
|
81
|
+
/** A varint or a fixed-width value runs past the end of the span. */
|
|
82
|
+
export const PROTO_ERR_TRUNCATED: i32 = -2
|
|
83
|
+
/** A length-delimited value's length is past the end of the span. */
|
|
84
|
+
export const PROTO_ERR_LENGTH: i32 = -3
|
|
85
|
+
/** A field number of 0 or past 2^29 - 1: read in a tag, or given to a writer. */
|
|
86
|
+
export const PROTO_ERR_FIELD: i32 = -4
|
|
87
|
+
/** A tag with the group wire types 3 or 4, which proto3 does not have. */
|
|
88
|
+
export const PROTO_ERR_GROUP: i32 = -5
|
|
89
|
+
/** A tag with the wire types 6 or 7, which protobuf does not define. */
|
|
90
|
+
export const PROTO_ERR_WIRE_TYPE: i32 = -6
|
|
91
|
+
/** More than `PROTO_MAX_DEPTH` nested spans. */
|
|
92
|
+
export const PROTO_ERR_DEPTH: i32 = -7
|
|
93
|
+
/** A read whose wire type is not the current field's, or of a value already read. */
|
|
94
|
+
export const PROTO_ERR_MISMATCH: i32 = -8
|
|
95
|
+
/** A `leave` or `endMessage` with no span open, or a packed element written outside one. */
|
|
96
|
+
export const PROTO_ERR_UNBALANCED: i32 = -9
|
|
97
|
+
/** A reader window or a `bytes` source window outside its array, or a message that would grow past `PROTO_MAX_SIZE` bytes. */
|
|
98
|
+
export const PROTO_ERR_WINDOW: i32 = -10
|
|
99
|
+
|
|
100
|
+
/** The bytes a varint of `v` takes, 1 to 10. */
|
|
101
|
+
const varintSize = (v: u64): i32 => {
|
|
102
|
+
let n: i32 = 1
|
|
103
|
+
let rest: u64 = v >> toU64(7)
|
|
104
|
+
while (rest !== toU64(0) && n < 10) {
|
|
105
|
+
n += 1
|
|
106
|
+
rest = rest >> toU64(7)
|
|
107
|
+
}
|
|
108
|
+
return n
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The `f32` whose IEEE 754 bits are `bits`, built through the `f64` that holds it exactly. */
|
|
112
|
+
const f32OfBits = (bits: u32): f32 => {
|
|
113
|
+
const sign: u64 = toU64(bits >> toU32(31)) << toU64(63)
|
|
114
|
+
const exp: u32 = (bits >> toU32(23)) & toU32(255)
|
|
115
|
+
const man: u64 = toU64(bits & toU32(8388607))
|
|
116
|
+
if (exp === toU32(255)) {
|
|
117
|
+
return toF32(bitsToF64(toI64(sign | (toU64(2047) << toU64(52)) | (man << toU64(29)))))
|
|
118
|
+
}
|
|
119
|
+
if (exp === toU32(0)) {
|
|
120
|
+
// A subnormal is man * 2^-149, exact in an f64; 874 is 2^-149's f64 exponent field.
|
|
121
|
+
const mag: f64 = toF64(man) * bitsToF64(toI64(toU64(874) << toU64(52)))
|
|
122
|
+
return toF32(sign === toU64(0) ? mag : -mag)
|
|
123
|
+
}
|
|
124
|
+
return toF32(bitsToF64(toI64(sign | (toU64(exp + toU32(896)) << toU64(52)) | (man << toU64(29)))))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Reads the fields of one protobuf message from `buf[from, to)`. `next`
|
|
129
|
+
* moves to the next field and answers its number and wire type in `field`
|
|
130
|
+
* and `wireType`; one typed read then takes its value, or the next call to
|
|
131
|
+
* `next` skips it. `enter` and `leave` walk an embedded message, and
|
|
132
|
+
* `enterPacked` and `more` the elements of a packed repeated field.
|
|
133
|
+
*/
|
|
134
|
+
export class ProtoReader {
|
|
135
|
+
/** The array the message is in; the reader never writes to it. */
|
|
136
|
+
buf: readonly u8[]
|
|
137
|
+
/** The offset of the next byte to read. */
|
|
138
|
+
pos: i32
|
|
139
|
+
/** The end of the span being read: the message's, or the entered field's. */
|
|
140
|
+
end: i32
|
|
141
|
+
/** `PROTO_OK`, or the first `PROTO_ERR_*` code; sticky until `reset`. */
|
|
142
|
+
failed: i32
|
|
143
|
+
/** The current field's number, 1 to 2^29 - 1, after `next` answers `true`. */
|
|
144
|
+
field: i32
|
|
145
|
+
/** The current field's wire type, one of the `PROTO_WIRE_*` values. */
|
|
146
|
+
wireType: i32
|
|
147
|
+
/** Where the bytes of the last value `len` read begin in `buf`. */
|
|
148
|
+
spanStart: i32
|
|
149
|
+
/** How many spans are entered. */
|
|
150
|
+
depth: i32
|
|
151
|
+
/** `true` while the current field's value is not read yet. @internal */
|
|
152
|
+
pending: boolean
|
|
153
|
+
/** The wire type of a packed field's elements while one is entered, else -1. @internal */
|
|
154
|
+
packedType: i32
|
|
155
|
+
/** The span end to return to at each depth. @internal */
|
|
156
|
+
ends: i32[]
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A reader over `buf[from, to)`.
|
|
160
|
+
*
|
|
161
|
+
* @param buf - The array the message is in.
|
|
162
|
+
* @param from - The offset of the message's first byte.
|
|
163
|
+
* @param to - The offset one past its last byte.
|
|
164
|
+
*/
|
|
165
|
+
constructor(buf: readonly u8[], from: i32, to: i32) {
|
|
166
|
+
this.buf = buf
|
|
167
|
+
this.pos = 0
|
|
168
|
+
this.end = 0
|
|
169
|
+
this.failed = PROTO_OK
|
|
170
|
+
this.field = 0
|
|
171
|
+
this.wireType = 0
|
|
172
|
+
this.spanStart = 0
|
|
173
|
+
this.depth = 0
|
|
174
|
+
this.pending = false
|
|
175
|
+
this.packedType = -1
|
|
176
|
+
this.ends = []
|
|
177
|
+
for (let i: i32 = 0; i < PROTO_MAX_DEPTH; i += 1) {
|
|
178
|
+
this.ends.push(toI32(0))
|
|
179
|
+
}
|
|
180
|
+
this.reset(buf, from, to)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Starts over on `buf[from, to)`, clearing `failed`. A window outside
|
|
185
|
+
* `buf` is a caller bug: the reader holds an empty span and `failed` is
|
|
186
|
+
* `PROTO_ERR_WINDOW`.
|
|
187
|
+
*
|
|
188
|
+
* @param buf - The array the message is in.
|
|
189
|
+
* @param from - The offset of the message's first byte.
|
|
190
|
+
* @param to - The offset one past its last byte.
|
|
191
|
+
*/
|
|
192
|
+
reset(buf: readonly u8[], from: i32, to: i32): void {
|
|
193
|
+
const bad: boolean = from < 0 || to < from || to > toI32(buf.length)
|
|
194
|
+
this.buf = buf
|
|
195
|
+
this.pos = bad ? 0 : from
|
|
196
|
+
this.end = bad ? 0 : to
|
|
197
|
+
this.failed = bad ? PROTO_ERR_WINDOW : PROTO_OK
|
|
198
|
+
this.field = 0
|
|
199
|
+
this.wireType = 0
|
|
200
|
+
this.spanStart = this.pos
|
|
201
|
+
this.depth = 0
|
|
202
|
+
this.pending = false
|
|
203
|
+
this.packedType = -1
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Sets `failed` to `code` unless a fault is already recorded. @internal */
|
|
207
|
+
fail(code: i32): void {
|
|
208
|
+
if (this.failed === PROTO_OK) {
|
|
209
|
+
this.failed = code
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Reads a varint of at most ten bytes; 0 and a fault when it is malformed. @internal */
|
|
214
|
+
varint(): u64 {
|
|
215
|
+
const buf: readonly u8[] = this.buf
|
|
216
|
+
const end: i32 = this.end
|
|
217
|
+
let p: i32 = this.pos
|
|
218
|
+
let result: u64 = 0
|
|
219
|
+
let shift: u64 = 0
|
|
220
|
+
for (let i: i32 = 0; i < 9; i += 1) {
|
|
221
|
+
if (p < 0 || p >= end || p >= toI32(buf.length)) {
|
|
222
|
+
this.fail(PROTO_ERR_TRUNCATED)
|
|
223
|
+
return 0
|
|
224
|
+
}
|
|
225
|
+
const b: u8 = buf[p]
|
|
226
|
+
p = p + 1
|
|
227
|
+
result = result | (toU64(b & toU8(127)) << shift)
|
|
228
|
+
if ((b & toU8(128)) === toU8(0)) {
|
|
229
|
+
this.pos = p
|
|
230
|
+
return result
|
|
231
|
+
}
|
|
232
|
+
shift += toU64(7)
|
|
233
|
+
}
|
|
234
|
+
if (p < 0 || p >= end || p >= toI32(buf.length)) {
|
|
235
|
+
this.fail(PROTO_ERR_TRUNCATED)
|
|
236
|
+
return 0
|
|
237
|
+
}
|
|
238
|
+
const last: u8 = buf[p]
|
|
239
|
+
this.pos = p + 1
|
|
240
|
+
if ((last & toU8(128)) !== toU8(0)) {
|
|
241
|
+
this.fail(PROTO_ERR_VARINT)
|
|
242
|
+
return 0
|
|
243
|
+
}
|
|
244
|
+
// The tenth byte carries bit 63; its other bits are past 64 and dropped.
|
|
245
|
+
return result | (toU64(last) << toU64(63))
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Reads a length varint and checks it against the span; 0 and a fault when it does not fit. @internal */
|
|
249
|
+
length(): i32 {
|
|
250
|
+
const n: u64 = this.varint()
|
|
251
|
+
if (this.failed !== PROTO_OK) {
|
|
252
|
+
return 0
|
|
253
|
+
}
|
|
254
|
+
const room: i64 = toI64(this.end) - toI64(this.pos)
|
|
255
|
+
if (room < toI64(0) || n > toU64(room)) {
|
|
256
|
+
this.fail(PROTO_ERR_LENGTH)
|
|
257
|
+
return 0
|
|
258
|
+
}
|
|
259
|
+
return toI32(n)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Reads `count` little-endian bytes as one value; 0 and a fault when they run off the span. @internal */
|
|
263
|
+
fixed(count: i32): u64 {
|
|
264
|
+
const buf: readonly u8[] = this.buf
|
|
265
|
+
const end: i32 = this.end
|
|
266
|
+
let p: i32 = this.pos
|
|
267
|
+
let v: u64 = 0
|
|
268
|
+
for (let i: i32 = 0; i < count && i < 8; i += 1) {
|
|
269
|
+
if (p < 0 || p >= end || p >= toI32(buf.length)) {
|
|
270
|
+
this.fail(PROTO_ERR_TRUNCATED)
|
|
271
|
+
return 0
|
|
272
|
+
}
|
|
273
|
+
v = v | (toU64(buf[p]) << toU64(8 * i))
|
|
274
|
+
p = p + 1
|
|
275
|
+
}
|
|
276
|
+
this.pos = p
|
|
277
|
+
return v
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Moves past `n` bytes that `length` checked against the span. @internal */
|
|
281
|
+
pass(n: i32): void {
|
|
282
|
+
this.pos = toI32(toI64(this.pos) + toI64(n))
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Passes over the current field's value by its wire type. @internal */
|
|
286
|
+
skip(): void {
|
|
287
|
+
this.pending = false
|
|
288
|
+
if (this.wireType === PROTO_WIRE_VARINT) {
|
|
289
|
+
this.varint()
|
|
290
|
+
} else if (this.wireType === PROTO_WIRE_I64) {
|
|
291
|
+
this.fixed(toI32(8))
|
|
292
|
+
} else if (this.wireType === PROTO_WIRE_I32) {
|
|
293
|
+
this.fixed(toI32(4))
|
|
294
|
+
} else {
|
|
295
|
+
this.pass(this.length())
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Answers whether the current value may be read as `wireType`, and marks it read. @internal */
|
|
300
|
+
take(wireType: i32): boolean {
|
|
301
|
+
if (this.failed !== PROTO_OK) {
|
|
302
|
+
return false
|
|
303
|
+
}
|
|
304
|
+
if (!this.pending || this.wireType !== wireType) {
|
|
305
|
+
this.fail(PROTO_ERR_MISMATCH)
|
|
306
|
+
return false
|
|
307
|
+
}
|
|
308
|
+
this.pending = false
|
|
309
|
+
return true
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Moves to the next field of the span, skipping the current field's value
|
|
314
|
+
* if it was not read.
|
|
315
|
+
*
|
|
316
|
+
* @returns `true` with `field` and `wireType` set; `false` at the span's
|
|
317
|
+
* end or on a fault, which `failed` tells apart.
|
|
318
|
+
*/
|
|
319
|
+
next(): boolean {
|
|
320
|
+
if (this.failed !== PROTO_OK) {
|
|
321
|
+
return false
|
|
322
|
+
}
|
|
323
|
+
if (this.packedType >= 0) {
|
|
324
|
+
this.fail(PROTO_ERR_MISMATCH)
|
|
325
|
+
return false
|
|
326
|
+
}
|
|
327
|
+
if (this.pending) {
|
|
328
|
+
this.skip()
|
|
329
|
+
if (this.failed !== PROTO_OK) {
|
|
330
|
+
return false
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (this.pos >= this.end) {
|
|
334
|
+
return false
|
|
335
|
+
}
|
|
336
|
+
const tag: u64 = this.varint()
|
|
337
|
+
if (this.failed !== PROTO_OK) {
|
|
338
|
+
return false
|
|
339
|
+
}
|
|
340
|
+
if (tag >> toU64(32) !== toU64(0)) {
|
|
341
|
+
this.fail(PROTO_ERR_FIELD)
|
|
342
|
+
return false
|
|
343
|
+
}
|
|
344
|
+
const wireType: i32 = toI32(tag & toU64(7))
|
|
345
|
+
const field: i32 = toI32(tag >> toU64(3))
|
|
346
|
+
if (wireType === PROTO_WIRE_SGROUP || wireType === PROTO_WIRE_EGROUP) {
|
|
347
|
+
this.fail(PROTO_ERR_GROUP)
|
|
348
|
+
return false
|
|
349
|
+
}
|
|
350
|
+
if (wireType > PROTO_WIRE_I32) {
|
|
351
|
+
this.fail(PROTO_ERR_WIRE_TYPE)
|
|
352
|
+
return false
|
|
353
|
+
}
|
|
354
|
+
if (field === 0) {
|
|
355
|
+
this.fail(PROTO_ERR_FIELD)
|
|
356
|
+
return false
|
|
357
|
+
}
|
|
358
|
+
this.field = field
|
|
359
|
+
this.wireType = wireType
|
|
360
|
+
this.pending = true
|
|
361
|
+
return true
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Reads the current varint field as a uint64.
|
|
366
|
+
*
|
|
367
|
+
* @returns The value, or 0 on a fault.
|
|
368
|
+
*/
|
|
369
|
+
uint64(): u64 {
|
|
370
|
+
return this.take(PROTO_WIRE_VARINT) ? this.varint() : toU64(0)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Reads the current varint field as an int64.
|
|
375
|
+
*
|
|
376
|
+
* @returns The value, or 0 on a fault.
|
|
377
|
+
*/
|
|
378
|
+
int64(): i64 {
|
|
379
|
+
return toI64(this.uint64())
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Reads the current varint field as a uint32, keeping its low 32 bits as
|
|
384
|
+
* protobuf does.
|
|
385
|
+
*
|
|
386
|
+
* @returns The value, or 0 on a fault.
|
|
387
|
+
*/
|
|
388
|
+
uint32(): u32 {
|
|
389
|
+
return toU32(this.uint64())
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Reads the current varint field as an int32, keeping its low 32 bits: a
|
|
394
|
+
* negative int32 arrives sign-extended to ten bytes.
|
|
395
|
+
*
|
|
396
|
+
* @returns The value, or 0 on a fault.
|
|
397
|
+
*/
|
|
398
|
+
int32(): i32 {
|
|
399
|
+
return toI32(this.uint64())
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Reads the current varint field as a bool: any value but 0 is `true`.
|
|
404
|
+
*
|
|
405
|
+
* @returns The value, or `false` on a fault.
|
|
406
|
+
*/
|
|
407
|
+
bool(): boolean {
|
|
408
|
+
return this.uint64() !== toU64(0)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Reads the current varint field as a zigzag-coded sint64.
|
|
413
|
+
*
|
|
414
|
+
* @returns The value, or 0 on a fault.
|
|
415
|
+
*/
|
|
416
|
+
sint64(): i64 {
|
|
417
|
+
const v: u64 = this.uint64()
|
|
418
|
+
return toI64((v >> toU64(1)) ^ (toU64(0) - (v & toU64(1))))
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Reads the current varint field as a zigzag-coded sint32, from its low 32
|
|
423
|
+
* bits.
|
|
424
|
+
*
|
|
425
|
+
* @returns The value, or 0 on a fault.
|
|
426
|
+
*/
|
|
427
|
+
sint32(): i32 {
|
|
428
|
+
const v: u32 = toU32(this.uint64())
|
|
429
|
+
return toI32((v >> toU32(1)) ^ (toU32(0) - (v & toU32(1))))
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Reads the current 32-bit field as a fixed32.
|
|
434
|
+
*
|
|
435
|
+
* @returns The value, or 0 on a fault.
|
|
436
|
+
*/
|
|
437
|
+
fixed32(): u32 {
|
|
438
|
+
return this.take(PROTO_WIRE_I32) ? toU32(this.fixed(toI32(4))) : toU32(0)
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Reads the current 32-bit field as an sfixed32.
|
|
443
|
+
*
|
|
444
|
+
* @returns The value, or 0 on a fault.
|
|
445
|
+
*/
|
|
446
|
+
sfixed32(): i32 {
|
|
447
|
+
return toI32(this.fixed32())
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Reads the current 32-bit field as a float. A signaling NaN comes back
|
|
452
|
+
* quiet, its top mantissa bit set; `fixed32` reads the exact bits.
|
|
453
|
+
*
|
|
454
|
+
* @returns The value, or 0 on a fault.
|
|
455
|
+
*/
|
|
456
|
+
float(): f32 {
|
|
457
|
+
return f32OfBits(this.fixed32())
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Reads the current 64-bit field as a fixed64.
|
|
462
|
+
*
|
|
463
|
+
* @returns The value, or 0 on a fault.
|
|
464
|
+
*/
|
|
465
|
+
fixed64(): u64 {
|
|
466
|
+
return this.take(PROTO_WIRE_I64) ? this.fixed(toI32(8)) : toU64(0)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Reads the current 64-bit field as an sfixed64.
|
|
471
|
+
*
|
|
472
|
+
* @returns The value, or 0 on a fault.
|
|
473
|
+
*/
|
|
474
|
+
sfixed64(): i64 {
|
|
475
|
+
return toI64(this.fixed64())
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Reads the current 64-bit field as a double.
|
|
480
|
+
*
|
|
481
|
+
* @returns The value, or 0 on a fault.
|
|
482
|
+
*/
|
|
483
|
+
double(): f64 {
|
|
484
|
+
return bitsToF64(toI64(this.fixed64()))
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Reads the current length-delimited field as a span of bytes: a string,
|
|
489
|
+
* bytes, or a message to read later with a reader of its own. The span
|
|
490
|
+
* begins at `spanStart` in `buf`; a string's bytes are not checked to be
|
|
491
|
+
* UTF-8, which is the caller's to decode.
|
|
492
|
+
*
|
|
493
|
+
* @returns The span's length, or 0 on a fault.
|
|
494
|
+
*/
|
|
495
|
+
len(): i32 {
|
|
496
|
+
if (!this.take(PROTO_WIRE_LEN)) {
|
|
497
|
+
return 0
|
|
498
|
+
}
|
|
499
|
+
const n: i32 = this.length()
|
|
500
|
+
this.spanStart = this.pos
|
|
501
|
+
this.pass(n)
|
|
502
|
+
return n
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Enters the current length-delimited field as an embedded message:
|
|
507
|
+
* `next` then reads its fields, until `leave`.
|
|
508
|
+
*
|
|
509
|
+
* @returns `true` once entered; `false` on a fault, which is
|
|
510
|
+
* `PROTO_ERR_DEPTH` past `PROTO_MAX_DEPTH` spans.
|
|
511
|
+
*/
|
|
512
|
+
enter(): boolean {
|
|
513
|
+
if (!this.take(PROTO_WIRE_LEN)) {
|
|
514
|
+
return false
|
|
515
|
+
}
|
|
516
|
+
const n: i32 = this.length()
|
|
517
|
+
if (this.failed !== PROTO_OK) {
|
|
518
|
+
return false
|
|
519
|
+
}
|
|
520
|
+
const ends: i32[] = this.ends
|
|
521
|
+
const d: i32 = this.depth
|
|
522
|
+
if (d < 0 || d >= PROTO_MAX_DEPTH || d >= toI32(ends.length)) {
|
|
523
|
+
this.fail(PROTO_ERR_DEPTH)
|
|
524
|
+
return false
|
|
525
|
+
}
|
|
526
|
+
ends[d] = this.end
|
|
527
|
+
this.depth = d + 1
|
|
528
|
+
this.spanStart = this.pos
|
|
529
|
+
this.pass(n)
|
|
530
|
+
this.end = this.pos
|
|
531
|
+
this.pos = this.spanStart
|
|
532
|
+
return true
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Enters the current length-delimited field as a packed repeated field
|
|
537
|
+
* whose elements have `wireType`: `more` then moves from element to
|
|
538
|
+
* element, until `leave`.
|
|
539
|
+
*
|
|
540
|
+
* @param wireType - The elements' wire type: `PROTO_WIRE_VARINT`,
|
|
541
|
+
* `PROTO_WIRE_I64` or `PROTO_WIRE_I32`; any other is `PROTO_ERR_MISMATCH`.
|
|
542
|
+
* @returns `true` once entered; `false` on a fault.
|
|
543
|
+
*/
|
|
544
|
+
enterPacked(wireType: i32): boolean {
|
|
545
|
+
if (wireType !== PROTO_WIRE_VARINT && wireType !== PROTO_WIRE_I64 && wireType !== PROTO_WIRE_I32) {
|
|
546
|
+
this.fail(PROTO_ERR_MISMATCH)
|
|
547
|
+
return false
|
|
548
|
+
}
|
|
549
|
+
if (!this.enter()) {
|
|
550
|
+
return false
|
|
551
|
+
}
|
|
552
|
+
this.packedType = wireType
|
|
553
|
+
return true
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Moves to the next element of the entered packed field, skipping the
|
|
558
|
+
* current one if it was not read. A typed read then takes it.
|
|
559
|
+
*
|
|
560
|
+
* @returns `true` while an element remains; `false` at the field's end or
|
|
561
|
+
* on a fault, which `failed` tells apart.
|
|
562
|
+
*/
|
|
563
|
+
more(): boolean {
|
|
564
|
+
if (this.failed !== PROTO_OK) {
|
|
565
|
+
return false
|
|
566
|
+
}
|
|
567
|
+
if (this.packedType < 0) {
|
|
568
|
+
this.fail(PROTO_ERR_MISMATCH)
|
|
569
|
+
return false
|
|
570
|
+
}
|
|
571
|
+
if (this.pending) {
|
|
572
|
+
this.skip()
|
|
573
|
+
if (this.failed !== PROTO_OK) {
|
|
574
|
+
return false
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (this.pos >= this.end) {
|
|
578
|
+
return false
|
|
579
|
+
}
|
|
580
|
+
this.wireType = this.packedType
|
|
581
|
+
this.pending = true
|
|
582
|
+
return true
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Leaves the span `enter` or `enterPacked` opened, skipping what is left of
|
|
587
|
+
* it the way `next` or `more` would, so a malformed rest is refused, and
|
|
588
|
+
* returns to the enclosing span.
|
|
589
|
+
*
|
|
590
|
+
* @returns `true` once left; `false` on a fault, which is
|
|
591
|
+
* `PROTO_ERR_UNBALANCED` with no span entered.
|
|
592
|
+
*/
|
|
593
|
+
leave(): boolean {
|
|
594
|
+
if (this.failed !== PROTO_OK) {
|
|
595
|
+
return false
|
|
596
|
+
}
|
|
597
|
+
const ends: i32[] = this.ends
|
|
598
|
+
const d: i32 = this.depth
|
|
599
|
+
if (d <= 0 || d > toI32(ends.length)) {
|
|
600
|
+
this.fail(PROTO_ERR_UNBALANCED)
|
|
601
|
+
return false
|
|
602
|
+
}
|
|
603
|
+
if (this.packedType >= 0) {
|
|
604
|
+
while (this.more()) {
|
|
605
|
+
this.skip()
|
|
606
|
+
}
|
|
607
|
+
} else {
|
|
608
|
+
while (this.next()) {
|
|
609
|
+
this.skip()
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (this.failed !== PROTO_OK) {
|
|
613
|
+
return false
|
|
614
|
+
}
|
|
615
|
+
const top: i32 = d - 1
|
|
616
|
+
if (top >= 0 && top < toI32(ends.length)) {
|
|
617
|
+
this.end = ends[top]
|
|
618
|
+
}
|
|
619
|
+
this.depth = top
|
|
620
|
+
this.packedType = -1
|
|
621
|
+
this.pending = false
|
|
622
|
+
return true
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Writes protobuf fields into `buf` from offset 0, up to `end`. An embedded
|
|
628
|
+
* message or packed field is written between `beginMessage` and
|
|
629
|
+
* `endMessage`, which fills in its length.
|
|
630
|
+
*/
|
|
631
|
+
export class ProtoWriter {
|
|
632
|
+
/** The caller's buffer: the message is `buf[0, end)`; bytes past `end` are stale. */
|
|
633
|
+
buf: u8[]
|
|
634
|
+
/** The offset one past the last byte written. */
|
|
635
|
+
end: i32
|
|
636
|
+
/** `PROTO_OK`, or the first `PROTO_ERR_*` code; sticky until `reset`. */
|
|
637
|
+
failed: i32
|
|
638
|
+
/** How many messages are open. */
|
|
639
|
+
depth: i32
|
|
640
|
+
/** Where each open message's length byte is. @internal */
|
|
641
|
+
starts: i32[]
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* A writer into `buf`, which it grows as the message needs and reuses
|
|
645
|
+
* after `reset`.
|
|
646
|
+
*
|
|
647
|
+
* @param buf - The buffer to write into; its contents are overwritten.
|
|
648
|
+
*/
|
|
649
|
+
constructor(buf: u8[]) {
|
|
650
|
+
this.buf = buf
|
|
651
|
+
this.end = 0
|
|
652
|
+
this.failed = PROTO_OK
|
|
653
|
+
this.depth = 0
|
|
654
|
+
this.starts = []
|
|
655
|
+
for (let i: i32 = 0; i < PROTO_MAX_DEPTH; i += 1) {
|
|
656
|
+
this.starts.push(toI32(0))
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** Starts a new message at offset 0 of the same buffer, clearing `failed`. */
|
|
661
|
+
reset(): void {
|
|
662
|
+
this.end = 0
|
|
663
|
+
this.failed = PROTO_OK
|
|
664
|
+
this.depth = 0
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/** Stores one byte at `end`, growing the buffer when it is full. @internal */
|
|
668
|
+
put(b: u8): void {
|
|
669
|
+
const buf: u8[] = this.buf
|
|
670
|
+
const e: i32 = this.end
|
|
671
|
+
if (e < 0 || e >= PROTO_MAX_SIZE) {
|
|
672
|
+
if (this.failed === PROTO_OK) {
|
|
673
|
+
this.failed = PROTO_ERR_WINDOW
|
|
674
|
+
}
|
|
675
|
+
return
|
|
676
|
+
}
|
|
677
|
+
if (e < toI32(buf.length)) {
|
|
678
|
+
buf[e] = b
|
|
679
|
+
} else {
|
|
680
|
+
buf.push(b)
|
|
681
|
+
}
|
|
682
|
+
this.end = e + 1
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/** Stores `b` at `at` of the bytes already written; an offset outside them stores nothing. @internal */
|
|
686
|
+
poke(at: i32, b: u8): void {
|
|
687
|
+
const buf: u8[] = this.buf
|
|
688
|
+
if (at >= 0 && at < this.end && at < toI32(buf.length)) {
|
|
689
|
+
buf[at] = b
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** Appends `v` as a varint. @internal */
|
|
694
|
+
putVarint(v: u64): void {
|
|
695
|
+
let rest: u64 = v
|
|
696
|
+
while (rest >= toU64(128)) {
|
|
697
|
+
this.put(toU8(rest & toU64(127)) | toU8(128))
|
|
698
|
+
rest = rest >> toU64(7)
|
|
699
|
+
}
|
|
700
|
+
this.put(toU8(rest))
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/** Appends the low `count` bytes of `v`, little-endian. @internal */
|
|
704
|
+
putFixed(v: u64, count: i32): void {
|
|
705
|
+
for (let i: i32 = 0; i < count && i < 8; i += 1) {
|
|
706
|
+
this.put(toU8((v >> toU64(8 * i)) & toU64(255)))
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** Appends a tag, or sets `PROTO_ERR_FIELD`; answers whether the value may follow. @internal */
|
|
711
|
+
tag(field: i32, wireType: i32): boolean {
|
|
712
|
+
if (this.failed !== PROTO_OK) {
|
|
713
|
+
return false
|
|
714
|
+
}
|
|
715
|
+
if (field < 1 || field > PROTO_MAX_FIELD) {
|
|
716
|
+
this.failed = PROTO_ERR_FIELD
|
|
717
|
+
return false
|
|
718
|
+
}
|
|
719
|
+
this.putVarint((toU64(field) << toU64(3)) | toU64(wireType))
|
|
720
|
+
return true
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* Writes a uint64 field.
|
|
725
|
+
*
|
|
726
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
727
|
+
* @param v - The value.
|
|
728
|
+
*/
|
|
729
|
+
uint64(field: i32, v: u64): void {
|
|
730
|
+
if (this.tag(field, PROTO_WIRE_VARINT)) {
|
|
731
|
+
this.putVarint(v)
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* Writes an int64 field; a negative value takes ten bytes.
|
|
737
|
+
*
|
|
738
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
739
|
+
* @param v - The value.
|
|
740
|
+
*/
|
|
741
|
+
int64(field: i32, v: i64): void {
|
|
742
|
+
this.uint64(field, toU64(v))
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* Writes a uint32 field.
|
|
747
|
+
*
|
|
748
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
749
|
+
* @param v - The value.
|
|
750
|
+
*/
|
|
751
|
+
uint32(field: i32, v: u32): void {
|
|
752
|
+
this.uint64(field, toU64(v))
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Writes an int32 field, a negative value sign-extended to ten bytes as
|
|
757
|
+
* protobuf requires.
|
|
758
|
+
*
|
|
759
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
760
|
+
* @param v - The value.
|
|
761
|
+
*/
|
|
762
|
+
int32(field: i32, v: i32): void {
|
|
763
|
+
this.uint64(field, toU64(toI64(v)))
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* Writes a bool field.
|
|
768
|
+
*
|
|
769
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
770
|
+
* @param v - The value.
|
|
771
|
+
*/
|
|
772
|
+
bool(field: i32, v: boolean): void {
|
|
773
|
+
this.uint64(field, v ? toU64(1) : toU64(0))
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Writes a zigzag-coded sint64 field.
|
|
778
|
+
*
|
|
779
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
780
|
+
* @param v - The value.
|
|
781
|
+
*/
|
|
782
|
+
sint64(field: i32, v: i64): void {
|
|
783
|
+
this.uint64(field, toU64((v << toI64(1)) ^ (v >> toI64(63))))
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Writes a zigzag-coded sint32 field.
|
|
788
|
+
*
|
|
789
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
790
|
+
* @param v - The value.
|
|
791
|
+
*/
|
|
792
|
+
sint32(field: i32, v: i32): void {
|
|
793
|
+
this.uint64(field, toU64(toU32((v << 1) ^ (v >> 31))))
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* Writes a fixed32 field.
|
|
798
|
+
*
|
|
799
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
800
|
+
* @param v - The value.
|
|
801
|
+
*/
|
|
802
|
+
fixed32(field: i32, v: u32): void {
|
|
803
|
+
if (this.tag(field, PROTO_WIRE_I32)) {
|
|
804
|
+
this.putFixed(toU64(v), toI32(4))
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* Writes an sfixed32 field.
|
|
810
|
+
*
|
|
811
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
812
|
+
* @param v - The value.
|
|
813
|
+
*/
|
|
814
|
+
sfixed32(field: i32, v: i32): void {
|
|
815
|
+
this.fixed32(field, toU32(v))
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Writes a float field.
|
|
820
|
+
*
|
|
821
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
822
|
+
* @param v - The value.
|
|
823
|
+
*/
|
|
824
|
+
float(field: i32, v: f32): void {
|
|
825
|
+
this.fixed32(field, toU32(f32ToBits(v)))
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Writes a fixed64 field.
|
|
830
|
+
*
|
|
831
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
832
|
+
* @param v - The value.
|
|
833
|
+
*/
|
|
834
|
+
fixed64(field: i32, v: u64): void {
|
|
835
|
+
if (this.tag(field, PROTO_WIRE_I64)) {
|
|
836
|
+
this.putFixed(v, toI32(8))
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Writes an sfixed64 field.
|
|
842
|
+
*
|
|
843
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
844
|
+
* @param v - The value.
|
|
845
|
+
*/
|
|
846
|
+
sfixed64(field: i32, v: i64): void {
|
|
847
|
+
this.fixed64(field, toU64(v))
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Writes a double field.
|
|
852
|
+
*
|
|
853
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
854
|
+
* @param v - The value.
|
|
855
|
+
*/
|
|
856
|
+
double(field: i32, v: f64): void {
|
|
857
|
+
this.fixed64(field, toU64(f64ToBits(v)))
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Writes a bytes field from `src[from, from + count)`. A window outside
|
|
862
|
+
* `src` is a caller bug: nothing is written and `failed` is
|
|
863
|
+
* `PROTO_ERR_WINDOW`.
|
|
864
|
+
*
|
|
865
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
866
|
+
* @param src - The array the bytes are in.
|
|
867
|
+
* @param from - The offset of the first byte.
|
|
868
|
+
* @param count - How many bytes.
|
|
869
|
+
*/
|
|
870
|
+
bytes(field: i32, src: readonly u8[], from: i32, count: i32): void {
|
|
871
|
+
const bad: boolean = from < 0 || count < 0 || toI64(from) + toI64(count) > toI64(src.length)
|
|
872
|
+
if (bad && this.failed === PROTO_OK) {
|
|
873
|
+
this.failed = PROTO_ERR_WINDOW
|
|
874
|
+
}
|
|
875
|
+
if (this.tag(field, PROTO_WIRE_LEN)) {
|
|
876
|
+
this.putVarint(toU64(count))
|
|
877
|
+
const stop: i64 = toI64(from) + toI64(count)
|
|
878
|
+
for (let q: i32 = from; q >= 0 && q < toI32(src.length) && toI64(q) < stop; q += 1) {
|
|
879
|
+
this.put(src[q])
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Writes a string field: the string's UTF-8 bytes.
|
|
886
|
+
*
|
|
887
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
888
|
+
* @param s - The string.
|
|
889
|
+
*/
|
|
890
|
+
string(field: i32, s: string): void {
|
|
891
|
+
if (this.tag(field, PROTO_WIRE_LEN)) {
|
|
892
|
+
const n: i32 = toI32(s.length)
|
|
893
|
+
this.putVarint(toU64(n))
|
|
894
|
+
for (let i: i32 = 0; i < n; i += 1) {
|
|
895
|
+
this.put(toU8(s.charCodeAt(i)))
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Opens a length-delimited field, an embedded message or a packed repeated
|
|
902
|
+
* field, whose contents are the fields or elements written until
|
|
903
|
+
* `endMessage`.
|
|
904
|
+
*
|
|
905
|
+
* @param field - The field number, 1 to 2^29 - 1.
|
|
906
|
+
*/
|
|
907
|
+
beginMessage(field: i32): void {
|
|
908
|
+
if (this.failed === PROTO_OK && this.depth >= PROTO_MAX_DEPTH) {
|
|
909
|
+
this.failed = PROTO_ERR_DEPTH
|
|
910
|
+
}
|
|
911
|
+
const starts: i32[] = this.starts
|
|
912
|
+
const d: i32 = this.depth
|
|
913
|
+
if (this.tag(field, PROTO_WIRE_LEN)) {
|
|
914
|
+
if (d >= 0 && d < toI32(starts.length)) {
|
|
915
|
+
starts[d] = this.end
|
|
916
|
+
this.depth = d + 1
|
|
917
|
+
}
|
|
918
|
+
this.put(toU8(0))
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/**
|
|
923
|
+
* Closes the field `beginMessage` opened, writing its length; contents of
|
|
924
|
+
* 128 bytes or more are moved right to make room for a longer length.
|
|
925
|
+
*/
|
|
926
|
+
endMessage(): void {
|
|
927
|
+
if (this.failed !== PROTO_OK) {
|
|
928
|
+
return
|
|
929
|
+
}
|
|
930
|
+
const starts: i32[] = this.starts
|
|
931
|
+
const d: i32 = this.depth
|
|
932
|
+
if (d <= 0 || d > toI32(starts.length)) {
|
|
933
|
+
this.failed = PROTO_ERR_UNBALANCED
|
|
934
|
+
return
|
|
935
|
+
}
|
|
936
|
+
const top: i32 = d - 1
|
|
937
|
+
this.depth = top
|
|
938
|
+
const at: i32 = top >= 0 && top < toI32(starts.length) ? starts[top] : 0
|
|
939
|
+
// `at` is where beginMessage put the one length byte, so `n` is 0 or more.
|
|
940
|
+
const n: i32 = toI32(toI64(this.end) - toI64(at) - toI64(1))
|
|
941
|
+
const size: i32 = varintSize(toU64(n))
|
|
942
|
+
for (let i: i32 = 1; i < size; i += 1) {
|
|
943
|
+
this.put(toU8(0))
|
|
944
|
+
}
|
|
945
|
+
if (this.failed !== PROTO_OK) {
|
|
946
|
+
return
|
|
947
|
+
}
|
|
948
|
+
// Move the contents right by size - 1 from the top, so no byte is read after it is overwritten.
|
|
949
|
+
const buf: u8[] = this.buf
|
|
950
|
+
for (let k: i32 = n; k > 0 && size > 1; k -= 1) {
|
|
951
|
+
const from: i32 = toI32(toI64(at) + toI64(k))
|
|
952
|
+
if (from >= 0 && from < toI32(buf.length)) {
|
|
953
|
+
this.poke(toI32(toI64(from) + toI64(size) - toI64(1)), buf[from])
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
let rest: u64 = toU64(n)
|
|
957
|
+
for (let i: i32 = 0; i < size; i += 1) {
|
|
958
|
+
const more: u8 = i + 1 < size ? toU8(128) : toU8(0)
|
|
959
|
+
this.poke(toI32(toI64(at) + toI64(i)), toU8(rest & toU64(127)) | more)
|
|
960
|
+
rest = rest >> toU64(7)
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Whether an element may be written: no fault and a span open; with none
|
|
966
|
+
* open it sets `PROTO_ERR_UNBALANCED`. The writer does not tell a packed
|
|
967
|
+
* field's span from an embedded message's: that the open span is a packed
|
|
968
|
+
* field is the caller's guarantee. @internal
|
|
969
|
+
*/
|
|
970
|
+
packed(): boolean {
|
|
971
|
+
if (this.failed === PROTO_OK && this.depth <= 0) {
|
|
972
|
+
this.failed = PROTO_ERR_UNBALANCED
|
|
973
|
+
}
|
|
974
|
+
return this.failed === PROTO_OK
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* Writes one element of the packed field the last `beginMessage` opened,
|
|
979
|
+
* as a varint: a uint64, or an int64, int32 or bool converted to `u64` as
|
|
980
|
+
* `int64`, `int32` and `bool` do. With no span open it sets
|
|
981
|
+
* `PROTO_ERR_UNBALANCED`; that the open span is a packed field, not an
|
|
982
|
+
* embedded message, is the caller's to guarantee.
|
|
983
|
+
*
|
|
984
|
+
* @param v - The element.
|
|
985
|
+
*/
|
|
986
|
+
packedVarint(v: u64): void {
|
|
987
|
+
if (this.packed()) {
|
|
988
|
+
this.putVarint(v)
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* Writes one element of the packed field the last `beginMessage` opened,
|
|
994
|
+
* as four little-endian bytes: a fixed32, an sfixed32 or a float's bits.
|
|
995
|
+
* Open spans are checked as `packedVarint` checks them.
|
|
996
|
+
*
|
|
997
|
+
* @param v - The element.
|
|
998
|
+
*/
|
|
999
|
+
packedFixed32(v: u32): void {
|
|
1000
|
+
if (this.packed()) {
|
|
1001
|
+
this.putFixed(toU64(v), toI32(4))
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* Writes one element of the packed field the last `beginMessage` opened,
|
|
1007
|
+
* as eight little-endian bytes: a fixed64, an sfixed64 or a double's bits.
|
|
1008
|
+
* Open spans are checked as `packedVarint` checks them.
|
|
1009
|
+
*
|
|
1010
|
+
* @param v - The element.
|
|
1011
|
+
*/
|
|
1012
|
+
packedFixed64(v: u64): void {
|
|
1013
|
+
if (this.packed()) {
|
|
1014
|
+
this.putFixed(v, toI32(8))
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|