nish 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/README.md +27 -21
- package/docs/AI.md +237 -160
- package/docs/INSTALL.md +1 -1
- package/package.json +12 -12
- 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/bytes.ts
CHANGED
|
@@ -22,20 +22,15 @@
|
|
|
22
22
|
* bytes are its IEEE 754 bits (`f64ToBits`). There is no `f32`: the language
|
|
23
23
|
* has no bit cast for one.
|
|
24
24
|
*
|
|
25
|
-
* **Each
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* `getU64BE`, and with it `getI64BE` and `getF64BE`, is the exception: it
|
|
36
|
-
* checks the whole range once and reads the eight bytes unchecked, which lowers
|
|
37
|
-
* to one load and a byte swap. An offset outside the range still goes byte by
|
|
38
|
-
* byte, so it panics as the others do.
|
|
25
|
+
* **Each function checks its whole window once, first, through `bytesWindow`**,
|
|
26
|
+
* so an offset whose bytes do not all lie in the array panics before any byte
|
|
27
|
+
* is read or written, naming the function and its width (a literal message).
|
|
28
|
+
* The check is a window contract (docs/LANGUAGE.md, "Panic sites", `window`):
|
|
29
|
+
* it is recorded at each call, so a caller whose own test holds the window,
|
|
30
|
+
* such as `off >= 0 && off + 4 <= b.length` before a `getU32LE`, proves it
|
|
31
|
+
* there, and a `--deny-panics` program that tests its offsets needs no
|
|
32
|
+
* opt-out. Inside, every byte the window covers is proven, so the eight-byte
|
|
33
|
+
* reads lower to one load (and, big-endian, a byte swap).
|
|
39
34
|
*
|
|
40
35
|
* **The in-range answers are byte for byte those of the copies `std/` keeps for
|
|
41
36
|
* itself**: `sha256LoadWord` (`std/crypto/sha256.ts`) for `getU32BE`,
|
|
@@ -76,103 +71,124 @@
|
|
|
76
71
|
* };
|
|
77
72
|
*
|
|
78
73
|
* **The four runs clamp their range and never panic; `copyBytes` panics on a
|
|
79
|
-
* range outside its array
|
|
80
|
-
* (docs/LANGUAGE.md, "Byte runs"), and the
|
|
81
|
-
* for text, never for secret data.
|
|
74
|
+
* range outside its array, a contract its caller proves (or `tryCopyBytes`).**
|
|
75
|
+
* Natively each is a runtime kernel (docs/LANGUAGE.md, "Byte runs"), and the
|
|
76
|
+
* runs are variable-time: they are for text, never for secret data.
|
|
82
77
|
*/
|
|
83
78
|
|
|
79
|
+
/**
|
|
80
|
+
* The window guard every fixed-width read and write calls first: it returns
|
|
81
|
+
* exactly when `b[off]` to `b[off + len - 1]` all lie in `b`, and panics
|
|
82
|
+
* with the message `what` otherwise. Its shape is the `window` contract's
|
|
83
|
+
* (docs/LANGUAGE.md, "Panic sites"), so each function that calls it first
|
|
84
|
+
* forwards the check to its own callers. `what` is a literal at every call:
|
|
85
|
+
* a message built here would cost every read an arena mark and release.
|
|
86
|
+
*/
|
|
87
|
+
const bytesWindow = (what: string, b: readonly u8[], off: i32, len: i32): void => {
|
|
88
|
+
const size: i32 = toI32(b.length)
|
|
89
|
+
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
90
|
+
panic(what)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
84
94
|
/**
|
|
85
95
|
* The `u16` whose low byte is `b[off]` and high byte `b[off + 1]`.
|
|
86
96
|
*
|
|
87
97
|
* @param b The bytes the number is read from; they are not changed.
|
|
88
98
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
89
|
-
* `b[off + 1]`, and
|
|
99
|
+
* `b[off + 1]`, and an offset whose bytes are not all in `b` panics.
|
|
90
100
|
* @returns The number read.
|
|
91
101
|
*/
|
|
92
|
-
export const getU16LE = (b: u8[], off: i32): u16 =>
|
|
102
|
+
export const getU16LE = (b: u8[], off: i32): u16 => {
|
|
103
|
+
bytesWindow("getU16LE: the 2 bytes at the offset do not all lie in the array", b, off, 2)
|
|
104
|
+
return toU16(b[off]) | (toU16(b[off + 1]) << 8)
|
|
105
|
+
}
|
|
93
106
|
|
|
94
107
|
/**
|
|
95
108
|
* The `u16` whose high byte is `b[off]` and low byte `b[off + 1]`.
|
|
96
109
|
*
|
|
97
110
|
* @param b The bytes the number is read from; they are not changed.
|
|
98
111
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
99
|
-
* `b[off + 1]`, and
|
|
112
|
+
* `b[off + 1]`, and an offset whose bytes are not all in `b` panics.
|
|
100
113
|
* @returns The number read.
|
|
101
114
|
*/
|
|
102
|
-
export const getU16BE = (b: u8[], off: i32): u16 =>
|
|
115
|
+
export const getU16BE = (b: u8[], off: i32): u16 => {
|
|
116
|
+
bytesWindow("getU16BE: the 2 bytes at the offset do not all lie in the array", b, off, 2)
|
|
117
|
+
return (toU16(b[off]) << 8) | toU16(b[off + 1])
|
|
118
|
+
}
|
|
103
119
|
|
|
104
120
|
/**
|
|
105
121
|
* The `u32` whose bytes, least significant first, are `b[off]` to `b[off + 3]`.
|
|
106
122
|
*
|
|
107
123
|
* @param b The bytes the number is read from; they are not changed.
|
|
108
124
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
109
|
-
* `b[off + 3]`, and
|
|
125
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
110
126
|
* @returns The number read.
|
|
111
127
|
*/
|
|
112
|
-
export const getU32LE = (b: u8[], off: i32): u32 =>
|
|
113
|
-
(
|
|
114
|
-
|
|
128
|
+
export const getU32LE = (b: u8[], off: i32): u32 => {
|
|
129
|
+
bytesWindow("getU32LE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
130
|
+
return (
|
|
131
|
+
(toU32(b[off]) | (toU32(b[off + 1]) << 8) | (toU32(b[off + 2]) << 16)) +
|
|
132
|
+
toU32(b[off + 3]) * toU32(0x1000000)
|
|
133
|
+
)
|
|
134
|
+
}
|
|
115
135
|
|
|
116
136
|
/**
|
|
117
137
|
* The `u32` whose bytes, most significant first, are `b[off]` to `b[off + 3]`.
|
|
118
138
|
*
|
|
119
139
|
* @param b The bytes the number is read from; they are not changed.
|
|
120
140
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
121
|
-
* `b[off + 3]`, and
|
|
141
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
122
142
|
* @returns The number read.
|
|
123
143
|
*/
|
|
124
|
-
export const getU32BE = (b: u8[], off: i32): u32 =>
|
|
125
|
-
|
|
126
|
-
|
|
144
|
+
export const getU32BE = (b: u8[], off: i32): u32 => {
|
|
145
|
+
bytesWindow("getU32BE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
146
|
+
return (
|
|
147
|
+
toU32(b[off]) * toU32(0x1000000) +
|
|
148
|
+
((toU32(b[off + 1]) << 16) | (toU32(b[off + 2]) << 8) | toU32(b[off + 3]))
|
|
149
|
+
)
|
|
150
|
+
}
|
|
127
151
|
|
|
128
152
|
/**
|
|
129
153
|
* The `u64` whose bytes, least significant first, are `b[off]` to `b[off + 7]`.
|
|
130
154
|
*
|
|
131
155
|
* @param b The bytes the number is read from; they are not changed.
|
|
132
156
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
133
|
-
* `b[off + 7]`, and
|
|
157
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
134
158
|
* @returns The number read.
|
|
135
159
|
*/
|
|
136
160
|
export const getU64LE = (b: u8[], off: i32): u64 => {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
let v = toU64(0)
|
|
147
|
-
for (let i: i32 = 0; i < 8; i++) {
|
|
148
|
-
v = (v << toU64(8)) | toU64(b[off + i])
|
|
149
|
-
}
|
|
150
|
-
return v
|
|
161
|
+
bytesWindow("getU64LE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
162
|
+
return (
|
|
163
|
+
toU64(b[off]) |
|
|
164
|
+
(toU64(b[off + 1]) << toU64(8)) |
|
|
165
|
+
((toU64(b[off + 2]) << toU64(16)) | (toU64(b[off + 3]) << toU64(24))) |
|
|
166
|
+
((toU64(b[off + 4]) << toU64(32)) |
|
|
167
|
+
(toU64(b[off + 5]) << toU64(40)) |
|
|
168
|
+
((toU64(b[off + 6]) << toU64(48)) | (toU64(b[off + 7]) << toU64(56))))
|
|
169
|
+
)
|
|
151
170
|
}
|
|
152
171
|
|
|
153
172
|
/**
|
|
154
173
|
* The `u64` whose bytes, most significant first, are `b[off]` to `b[off + 7]`.
|
|
155
|
-
* The
|
|
156
|
-
* a
|
|
157
|
-
* its own function so its last load is not merged with the fast path's.
|
|
174
|
+
* The ors are grouped in pairs, which LLVM folds into one load and a `bswap`;
|
|
175
|
+
* a flat chain of eight `or`s stayed byte by byte.
|
|
158
176
|
*
|
|
159
177
|
* @param b The bytes the number is read from; they are not changed.
|
|
160
178
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
161
|
-
* `b[off + 7]`, and
|
|
179
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
162
180
|
* @returns The number read.
|
|
163
181
|
*/
|
|
164
182
|
export const getU64BE = (b: u8[], off: i32): u64 => {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
return getU64BEByByte(b, off)
|
|
183
|
+
bytesWindow("getU64BE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
184
|
+
return (
|
|
185
|
+
(toU64(b[off]) << toU64(56)) |
|
|
186
|
+
(toU64(b[off + 1]) << toU64(48)) |
|
|
187
|
+
((toU64(b[off + 2]) << toU64(40)) | (toU64(b[off + 3]) << toU64(32))) |
|
|
188
|
+
((toU64(b[off + 4]) << toU64(24)) |
|
|
189
|
+
(toU64(b[off + 5]) << toU64(16)) |
|
|
190
|
+
((toU64(b[off + 6]) << toU64(8)) | toU64(b[off + 7])))
|
|
191
|
+
)
|
|
176
192
|
}
|
|
177
193
|
|
|
178
194
|
/**
|
|
@@ -180,70 +196,89 @@ export const getU64BE = (b: u8[], off: i32): u64 => {
|
|
|
180
196
|
*
|
|
181
197
|
* @param b The bytes the number is read from; they are not changed.
|
|
182
198
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
183
|
-
* `b[off + 3]`, and
|
|
199
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
184
200
|
* @returns The number read, from -2^31 to 2^31 - 1.
|
|
185
201
|
*/
|
|
186
|
-
export const getI32LE = (b: u8[], off: i32): i32 =>
|
|
202
|
+
export const getI32LE = (b: u8[], off: i32): i32 => {
|
|
203
|
+
bytesWindow("getI32LE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
204
|
+
return toI32(getU32LE(b, off))
|
|
205
|
+
}
|
|
187
206
|
|
|
188
207
|
/**
|
|
189
208
|
* `getU32BE`'s bits as a two's-complement `i32`.
|
|
190
209
|
*
|
|
191
210
|
* @param b The bytes the number is read from; they are not changed.
|
|
192
211
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
193
|
-
* `b[off + 3]`, and
|
|
212
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
194
213
|
* @returns The number read, from -2^31 to 2^31 - 1.
|
|
195
214
|
*/
|
|
196
|
-
export const getI32BE = (b: u8[], off: i32): i32 =>
|
|
215
|
+
export const getI32BE = (b: u8[], off: i32): i32 => {
|
|
216
|
+
bytesWindow("getI32BE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
217
|
+
return toI32(getU32BE(b, off))
|
|
218
|
+
}
|
|
197
219
|
|
|
198
220
|
/**
|
|
199
221
|
* `getU64LE`'s bits as a two's-complement `i64`.
|
|
200
222
|
*
|
|
201
223
|
* @param b The bytes the number is read from; they are not changed.
|
|
202
224
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
203
|
-
* `b[off + 7]`, and
|
|
225
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
204
226
|
* @returns The number read, from -2^63 to 2^63 - 1.
|
|
205
227
|
*/
|
|
206
|
-
export const getI64LE = (b: u8[], off: i32): i64 =>
|
|
228
|
+
export const getI64LE = (b: u8[], off: i32): i64 => {
|
|
229
|
+
bytesWindow("getI64LE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
230
|
+
return toI64(getU64LE(b, off))
|
|
231
|
+
}
|
|
207
232
|
|
|
208
233
|
/**
|
|
209
234
|
* `getU64BE`'s bits as a two's-complement `i64`.
|
|
210
235
|
*
|
|
211
236
|
* @param b The bytes the number is read from; they are not changed.
|
|
212
237
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
213
|
-
* `b[off + 7]`, and
|
|
238
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
214
239
|
* @returns The number read, from -2^63 to 2^63 - 1.
|
|
215
240
|
*/
|
|
216
|
-
export const getI64BE = (b: u8[], off: i32): i64 =>
|
|
241
|
+
export const getI64BE = (b: u8[], off: i32): i64 => {
|
|
242
|
+
bytesWindow("getI64BE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
243
|
+
return toI64(getU64BE(b, off))
|
|
244
|
+
}
|
|
217
245
|
|
|
218
246
|
/**
|
|
219
247
|
* The `f64` whose IEEE 754 bits are `getU64LE`'s.
|
|
220
248
|
*
|
|
221
249
|
* @param b The bytes the number is read from; they are not changed.
|
|
222
250
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
223
|
-
* `b[off + 7]`, and
|
|
251
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
224
252
|
* @returns The double whose bits the eight bytes are.
|
|
225
253
|
*/
|
|
226
|
-
export const getF64LE = (b: u8[], off: i32): f64 =>
|
|
254
|
+
export const getF64LE = (b: u8[], off: i32): f64 => {
|
|
255
|
+
bytesWindow("getF64LE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
256
|
+
return bitsToF64(toI64(getU64LE(b, off)))
|
|
257
|
+
}
|
|
227
258
|
|
|
228
259
|
/**
|
|
229
260
|
* The `f64` whose IEEE 754 bits are `getU64BE`'s.
|
|
230
261
|
*
|
|
231
262
|
* @param b The bytes the number is read from; they are not changed.
|
|
232
263
|
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
233
|
-
* `b[off + 7]`, and
|
|
264
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
234
265
|
* @returns The double whose bits the eight bytes are.
|
|
235
266
|
*/
|
|
236
|
-
export const getF64BE = (b: u8[], off: i32): f64 =>
|
|
267
|
+
export const getF64BE = (b: u8[], off: i32): f64 => {
|
|
268
|
+
bytesWindow("getF64BE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
269
|
+
return bitsToF64(toI64(getU64BE(b, off)))
|
|
270
|
+
}
|
|
237
271
|
|
|
238
272
|
/**
|
|
239
273
|
* Writes `v` into `b[off]` and `b[off + 1]`, low byte first.
|
|
240
274
|
*
|
|
241
275
|
* @param b The bytes written into; only the two at `off` change.
|
|
242
276
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
243
|
-
* `b[off + 1]`, and
|
|
277
|
+
* `b[off + 1]`, and an offset whose bytes are not all in `b` panics.
|
|
244
278
|
* @param v The number to store.
|
|
245
279
|
*/
|
|
246
280
|
export const setU16LE = (b: u8[], off: i32, v: u16): void => {
|
|
281
|
+
bytesWindow("setU16LE: the 2 bytes at the offset do not all lie in the array", b, off, 2)
|
|
247
282
|
b[off] = toU8(v & 255)
|
|
248
283
|
b[off + 1] = toU8((v >> 8) & 255)
|
|
249
284
|
}
|
|
@@ -253,10 +288,11 @@ export const setU16LE = (b: u8[], off: i32, v: u16): void => {
|
|
|
253
288
|
*
|
|
254
289
|
* @param b The bytes written into; only the two at `off` change.
|
|
255
290
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
256
|
-
* `b[off + 1]`, and
|
|
291
|
+
* `b[off + 1]`, and an offset whose bytes are not all in `b` panics.
|
|
257
292
|
* @param v The number to store.
|
|
258
293
|
*/
|
|
259
294
|
export const setU16BE = (b: u8[], off: i32, v: u16): void => {
|
|
295
|
+
bytesWindow("setU16BE: the 2 bytes at the offset do not all lie in the array", b, off, 2)
|
|
260
296
|
b[off] = toU8((v >> 8) & 255)
|
|
261
297
|
b[off + 1] = toU8(v & 255)
|
|
262
298
|
}
|
|
@@ -266,10 +302,11 @@ export const setU16BE = (b: u8[], off: i32, v: u16): void => {
|
|
|
266
302
|
*
|
|
267
303
|
* @param b The bytes written into; only the four at `off` change.
|
|
268
304
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
269
|
-
* `b[off + 3]`, and
|
|
305
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
270
306
|
* @param v The number to store.
|
|
271
307
|
*/
|
|
272
308
|
export const setU32LE = (b: u8[], off: i32, v: u32): void => {
|
|
309
|
+
bytesWindow("setU32LE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
273
310
|
b[off] = toU8(v & 255)
|
|
274
311
|
b[off + 1] = toU8((v >> 8) & 255)
|
|
275
312
|
b[off + 2] = toU8((v >> 16) & 255)
|
|
@@ -281,10 +318,11 @@ export const setU32LE = (b: u8[], off: i32, v: u32): void => {
|
|
|
281
318
|
*
|
|
282
319
|
* @param b The bytes written into; only the four at `off` change.
|
|
283
320
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
284
|
-
* `b[off + 3]`, and
|
|
321
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
285
322
|
* @param v The number to store.
|
|
286
323
|
*/
|
|
287
324
|
export const setU32BE = (b: u8[], off: i32, v: u32): void => {
|
|
325
|
+
bytesWindow("setU32BE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
288
326
|
b[off] = toU8((v >> 24) & 255)
|
|
289
327
|
b[off + 1] = toU8((v >> 16) & 255)
|
|
290
328
|
b[off + 2] = toU8((v >> 8) & 255)
|
|
@@ -296,13 +334,19 @@ export const setU32BE = (b: u8[], off: i32, v: u32): void => {
|
|
|
296
334
|
*
|
|
297
335
|
* @param b The bytes written into; only the eight at `off` change.
|
|
298
336
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
299
|
-
* `b[off + 7]`, and
|
|
337
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
300
338
|
* @param v The number to store.
|
|
301
339
|
*/
|
|
302
340
|
export const setU64LE = (b: u8[], off: i32, v: u64): void => {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
341
|
+
bytesWindow("setU64LE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
342
|
+
b[off] = toU8(v & toU64(255))
|
|
343
|
+
b[off + 1] = toU8((v >> toU64(8)) & toU64(255))
|
|
344
|
+
b[off + 2] = toU8((v >> toU64(16)) & toU64(255))
|
|
345
|
+
b[off + 3] = toU8((v >> toU64(24)) & toU64(255))
|
|
346
|
+
b[off + 4] = toU8((v >> toU64(32)) & toU64(255))
|
|
347
|
+
b[off + 5] = toU8((v >> toU64(40)) & toU64(255))
|
|
348
|
+
b[off + 6] = toU8((v >> toU64(48)) & toU64(255))
|
|
349
|
+
b[off + 7] = toU8((v >> toU64(56)) & toU64(255))
|
|
306
350
|
}
|
|
307
351
|
|
|
308
352
|
/**
|
|
@@ -310,13 +354,19 @@ export const setU64LE = (b: u8[], off: i32, v: u64): void => {
|
|
|
310
354
|
*
|
|
311
355
|
* @param b The bytes written into; only the eight at `off` change.
|
|
312
356
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
313
|
-
* `b[off + 7]`, and
|
|
357
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
314
358
|
* @param v The number to store.
|
|
315
359
|
*/
|
|
316
360
|
export const setU64BE = (b: u8[], off: i32, v: u64): void => {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
361
|
+
bytesWindow("setU64BE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
362
|
+
b[off] = toU8((v >> toU64(56)) & toU64(255))
|
|
363
|
+
b[off + 1] = toU8((v >> toU64(48)) & toU64(255))
|
|
364
|
+
b[off + 2] = toU8((v >> toU64(40)) & toU64(255))
|
|
365
|
+
b[off + 3] = toU8((v >> toU64(32)) & toU64(255))
|
|
366
|
+
b[off + 4] = toU8((v >> toU64(24)) & toU64(255))
|
|
367
|
+
b[off + 5] = toU8((v >> toU64(16)) & toU64(255))
|
|
368
|
+
b[off + 6] = toU8((v >> toU64(8)) & toU64(255))
|
|
369
|
+
b[off + 7] = toU8(v & toU64(255))
|
|
320
370
|
}
|
|
321
371
|
|
|
322
372
|
/**
|
|
@@ -324,64 +374,122 @@ export const setU64BE = (b: u8[], off: i32, v: u64): void => {
|
|
|
324
374
|
*
|
|
325
375
|
* @param b The bytes written into; only the four at `off` change.
|
|
326
376
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
327
|
-
* `b[off + 3]`, and
|
|
377
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
328
378
|
* @param v The number to store; a negative one is stored as its two's
|
|
329
379
|
* complement.
|
|
330
380
|
*/
|
|
331
|
-
export const setI32LE = (b: u8[], off: i32, v: i32): void =>
|
|
381
|
+
export const setI32LE = (b: u8[], off: i32, v: i32): void => {
|
|
382
|
+
bytesWindow("setI32LE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
383
|
+
const u: u32 = toU32(v)
|
|
384
|
+
b[off] = toU8(u & 255)
|
|
385
|
+
b[off + 1] = toU8((u >> 8) & 255)
|
|
386
|
+
b[off + 2] = toU8((u >> 16) & 255)
|
|
387
|
+
b[off + 3] = toU8((u >> 24) & 255)
|
|
388
|
+
}
|
|
332
389
|
|
|
333
390
|
/**
|
|
334
391
|
* Writes `v`'s two's-complement bits as `setU32BE` does.
|
|
335
392
|
*
|
|
336
393
|
* @param b The bytes written into; only the four at `off` change.
|
|
337
394
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
338
|
-
* `b[off + 3]`, and
|
|
395
|
+
* `b[off + 3]`, and an offset whose bytes are not all in `b` panics.
|
|
339
396
|
* @param v The number to store; a negative one is stored as its two's
|
|
340
397
|
* complement.
|
|
341
398
|
*/
|
|
342
|
-
export const setI32BE = (b: u8[], off: i32, v: i32): void =>
|
|
399
|
+
export const setI32BE = (b: u8[], off: i32, v: i32): void => {
|
|
400
|
+
bytesWindow("setI32BE: the 4 bytes at the offset do not all lie in the array", b, off, 4)
|
|
401
|
+
const u: u32 = toU32(v)
|
|
402
|
+
b[off] = toU8((u >> 24) & 255)
|
|
403
|
+
b[off + 1] = toU8((u >> 16) & 255)
|
|
404
|
+
b[off + 2] = toU8((u >> 8) & 255)
|
|
405
|
+
b[off + 3] = toU8(u & 255)
|
|
406
|
+
}
|
|
343
407
|
|
|
344
408
|
/**
|
|
345
409
|
* Writes `v`'s two's-complement bits as `setU64LE` does.
|
|
346
410
|
*
|
|
347
411
|
* @param b The bytes written into; only the eight at `off` change.
|
|
348
412
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
349
|
-
* `b[off + 7]`, and
|
|
413
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
350
414
|
* @param v The number to store; a negative one is stored as its two's
|
|
351
415
|
* complement.
|
|
352
416
|
*/
|
|
353
|
-
export const setI64LE = (b: u8[], off: i32, v: i64): void =>
|
|
417
|
+
export const setI64LE = (b: u8[], off: i32, v: i64): void => {
|
|
418
|
+
bytesWindow("setI64LE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
419
|
+
const u: u64 = toU64(v)
|
|
420
|
+
b[off] = toU8(u & toU64(255))
|
|
421
|
+
b[off + 1] = toU8((u >> toU64(8)) & toU64(255))
|
|
422
|
+
b[off + 2] = toU8((u >> toU64(16)) & toU64(255))
|
|
423
|
+
b[off + 3] = toU8((u >> toU64(24)) & toU64(255))
|
|
424
|
+
b[off + 4] = toU8((u >> toU64(32)) & toU64(255))
|
|
425
|
+
b[off + 5] = toU8((u >> toU64(40)) & toU64(255))
|
|
426
|
+
b[off + 6] = toU8((u >> toU64(48)) & toU64(255))
|
|
427
|
+
b[off + 7] = toU8((u >> toU64(56)) & toU64(255))
|
|
428
|
+
}
|
|
354
429
|
|
|
355
430
|
/**
|
|
356
431
|
* Writes `v`'s two's-complement bits as `setU64BE` does.
|
|
357
432
|
*
|
|
358
433
|
* @param b The bytes written into; only the eight at `off` change.
|
|
359
434
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
360
|
-
* `b[off + 7]`, and
|
|
435
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
361
436
|
* @param v The number to store; a negative one is stored as its two's
|
|
362
437
|
* complement.
|
|
363
438
|
*/
|
|
364
|
-
export const setI64BE = (b: u8[], off: i32, v: i64): void =>
|
|
439
|
+
export const setI64BE = (b: u8[], off: i32, v: i64): void => {
|
|
440
|
+
bytesWindow("setI64BE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
441
|
+
const u: u64 = toU64(v)
|
|
442
|
+
b[off] = toU8((u >> toU64(56)) & toU64(255))
|
|
443
|
+
b[off + 1] = toU8((u >> toU64(48)) & toU64(255))
|
|
444
|
+
b[off + 2] = toU8((u >> toU64(40)) & toU64(255))
|
|
445
|
+
b[off + 3] = toU8((u >> toU64(32)) & toU64(255))
|
|
446
|
+
b[off + 4] = toU8((u >> toU64(24)) & toU64(255))
|
|
447
|
+
b[off + 5] = toU8((u >> toU64(16)) & toU64(255))
|
|
448
|
+
b[off + 6] = toU8((u >> toU64(8)) & toU64(255))
|
|
449
|
+
b[off + 7] = toU8(u & toU64(255))
|
|
450
|
+
}
|
|
365
451
|
|
|
366
452
|
/**
|
|
367
453
|
* Writes `v`'s IEEE 754 bits as `setU64LE` does.
|
|
368
454
|
*
|
|
369
455
|
* @param b The bytes written into; only the eight at `off` change.
|
|
370
456
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
371
|
-
* `b[off + 7]`, and
|
|
457
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
372
458
|
* @param v The double to store, as its IEEE 754 bits.
|
|
373
459
|
*/
|
|
374
|
-
export const setF64LE = (b: u8[], off: i32, v: f64): void =>
|
|
460
|
+
export const setF64LE = (b: u8[], off: i32, v: f64): void => {
|
|
461
|
+
bytesWindow("setF64LE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
462
|
+
const u: u64 = toU64(f64ToBits(v))
|
|
463
|
+
b[off] = toU8(u & toU64(255))
|
|
464
|
+
b[off + 1] = toU8((u >> toU64(8)) & toU64(255))
|
|
465
|
+
b[off + 2] = toU8((u >> toU64(16)) & toU64(255))
|
|
466
|
+
b[off + 3] = toU8((u >> toU64(24)) & toU64(255))
|
|
467
|
+
b[off + 4] = toU8((u >> toU64(32)) & toU64(255))
|
|
468
|
+
b[off + 5] = toU8((u >> toU64(40)) & toU64(255))
|
|
469
|
+
b[off + 6] = toU8((u >> toU64(48)) & toU64(255))
|
|
470
|
+
b[off + 7] = toU8((u >> toU64(56)) & toU64(255))
|
|
471
|
+
}
|
|
375
472
|
|
|
376
473
|
/**
|
|
377
474
|
* Writes `v`'s IEEE 754 bits as `setU64BE` does.
|
|
378
475
|
*
|
|
379
476
|
* @param b The bytes written into; only the eight at `off` change.
|
|
380
477
|
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
381
|
-
* `b[off + 7]`, and
|
|
478
|
+
* `b[off + 7]`, and an offset whose bytes are not all in `b` panics.
|
|
382
479
|
* @param v The double to store, as its IEEE 754 bits.
|
|
383
480
|
*/
|
|
384
|
-
export const setF64BE = (b: u8[], off: i32, v: f64): void =>
|
|
481
|
+
export const setF64BE = (b: u8[], off: i32, v: f64): void => {
|
|
482
|
+
bytesWindow("setF64BE: the 8 bytes at the offset do not all lie in the array", b, off, 8)
|
|
483
|
+
const u: u64 = toU64(f64ToBits(v))
|
|
484
|
+
b[off] = toU8((u >> toU64(56)) & toU64(255))
|
|
485
|
+
b[off + 1] = toU8((u >> toU64(48)) & toU64(255))
|
|
486
|
+
b[off + 2] = toU8((u >> toU64(40)) & toU64(255))
|
|
487
|
+
b[off + 3] = toU8((u >> toU64(32)) & toU64(255))
|
|
488
|
+
b[off + 4] = toU8((u >> toU64(24)) & toU64(255))
|
|
489
|
+
b[off + 5] = toU8((u >> toU64(16)) & toU64(255))
|
|
490
|
+
b[off + 6] = toU8((u >> toU64(8)) & toU64(255))
|
|
491
|
+
b[off + 7] = toU8(u & toU64(255))
|
|
492
|
+
}
|
|
385
493
|
|
|
386
494
|
// ---- Byte-class runs and the byte copy (WP38 S6a) ----------------------------------------
|
|
387
495
|
|
|
@@ -522,9 +630,11 @@ export const skipJsonPlain = (b: u8[], from: i32, to: i32): i32 => {
|
|
|
522
630
|
* @param count How many bytes to copy; zero copies nothing.
|
|
523
631
|
*/
|
|
524
632
|
export const copyBytes = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
|
|
525
|
-
|
|
633
|
+
// A caller contract (`src/caller-contract.ts`): each call proves the range.
|
|
634
|
+
if (count < 0 || at < 0 || from < 0 || count > toI32(dst.length) - at || count > toI32(src.length) - from) {
|
|
526
635
|
panic("copyBytes: the range lies outside an array")
|
|
527
636
|
}
|
|
637
|
+
copyBytesWalk(dst, at, src, from, count)
|
|
528
638
|
}
|
|
529
639
|
|
|
530
640
|
/**
|
|
@@ -707,7 +817,7 @@ const copyBytesWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, count:
|
|
|
707
817
|
|
|
708
818
|
/**
|
|
709
819
|
* `getU32LE` that answers an offset whose four bytes do not all lie in `b`
|
|
710
|
-
* as `Err`, where `getU32LE` panics
|
|
820
|
+
* as `Err`, where `getU32LE` panics: the form a strict module reads a
|
|
711
821
|
* record it did not size itself with.
|
|
712
822
|
*
|
|
713
823
|
* @param b The bytes the number is read from; they are not changed.
|