@amritk/nish-aarch64-linux 0.49.0 → 0.50.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.
Files changed (92) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. 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 access is an ordinary index**, so an offset whose bytes do not all lie
26
- * in the array panics `index`, naming the first byte that does not.
27
- *
28
- * A guard in the caller does not yet remove these checks: in a loop guarded by
29
- * `off + 3 < b.length`, the checker proves an index it can see but not one
30
- * inside a call, and after `getU32LE` inlines, LLVM keeps the checks on `off +
31
- * 1` to `off + 3`, because the guard compares the length truncated to `i32` and
32
- * the checks compare it unsigned at 64 bits. A loop that needs them gone
33
- * indexes the bytes itself.
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`,
@@ -81,98 +76,119 @@
81
76
  * 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 a byte of those outside `b` panics `index`.
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 => toU16(b[off]) | (toU16(b[off + 1]) << 8)
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 a byte of those outside `b` panics `index`.
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 => (toU16(b[off]) << 8) | toU16(b[off + 1])
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 a byte of those outside `b` panics `index`.
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
- (toU32(b[off]) | (toU32(b[off + 1]) << 8) | (toU32(b[off + 2]) << 16)) +
114
- toU32(b[off + 3]) * toU32(0x1000000)
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 a byte of those outside `b` panics `index`.
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
- toU32(b[off]) * toU32(0x1000000) +
126
- ((toU32(b[off + 1]) << 16) | (toU32(b[off + 2]) << 8) | toU32(b[off + 3]))
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 a byte of those outside `b` panics `index`.
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
- let v = toU64(0)
138
- for (let i: i32 = 0; i < 8; i++) {
139
- v = v | (toU64(b[off + i]) << toU64(i * 8))
140
- }
141
- return v
142
- }
143
-
144
- /** `getU64BE` a byte at a time, each index checked: the path for an offset whose eight bytes are not all in the array. */
145
- const getU64BEByByte = (b: u8[], off: i32): u64 => {
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 fast path's ors are grouped in pairs, which LLVM folds into one load and
156
- * a `bswap`; a flat chain of eight `or`s stayed byte by byte. The byte loop is
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 a byte of those outside `b` panics `index`.
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
- if (off >= 0 && off < 2147483640 && off + 8 <= toI32(b.length)) {
166
- return (
167
- (toU64(b[off]) << toU64(56)) |
168
- (toU64(b[off + 1]) << toU64(48)) |
169
- ((toU64(b[off + 2]) << toU64(40)) | (toU64(b[off + 3]) << toU64(32))) |
170
- ((toU64(b[off + 4]) << toU64(24)) |
171
- (toU64(b[off + 5]) << toU64(16)) |
172
- ((toU64(b[off + 6]) << toU64(8)) | toU64(b[off + 7])))
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 a byte of those outside `b` panics `index`.
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 => toI32(getU32LE(b, off))
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 a byte of those outside `b` panics `index`.
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 => toI32(getU32BE(b, off))
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 a byte of those outside `b` panics `index`.
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 => toI64(getU64LE(b, off))
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 a byte of those outside `b` panics `index`.
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 => toI64(getU64BE(b, off))
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 a byte of those outside `b` panics `index`.
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 => bitsToF64(toI64(getU64LE(b, off)))
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 a byte of those outside `b` panics `index`.
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 => bitsToF64(toI64(getU64BE(b, off)))
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 a byte of those outside `b` panics `index`.
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 a byte of those outside `b` panics `index`.
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 a byte of those outside `b` panics `index`.
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 a byte of those outside `b` panics `index`.
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 a byte of those outside `b` panics `index`.
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
- for (let i: i32 = 0; i < 8; i++) {
304
- b[off + i] = toU8((v >> toU64(i * 8)) & toU64(255))
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 a byte of those outside `b` panics `index`.
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
- for (let i: i32 = 0; i < 8; i++) {
318
- b[off + i] = toU8((v >> toU64((7 - i) * 8)) & toU64(255))
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 a byte of those outside `b` panics `index`.
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 => setU32LE(b, off, toU32(v))
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 a byte of those outside `b` panics `index`.
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 => setU32BE(b, off, toU32(v))
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 a byte of those outside `b` panics `index`.
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 => setU64LE(b, off, toU64(v))
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 a byte of those outside `b` panics `index`.
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 => setU64BE(b, off, toU64(v))
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 a byte of those outside `b` panics `index`.
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 => setU64LE(b, off, toU64(f64ToBits(v)))
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 a byte of those outside `b` panics `index`.
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 => setU64BE(b, off, toU64(f64ToBits(v)))
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
 
@@ -707,7 +815,7 @@ const copyBytesWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, count:
707
815
 
708
816
  /**
709
817
  * `getU32LE` that answers an offset whose four bytes do not all lie in `b`
710
- * as `Err`, where `getU32LE` panics `index`: the form a strict module reads a
818
+ * as `Err`, where `getU32LE` panics: the form a strict module reads a
711
819
  * record it did not size itself with.
712
820
  *
713
821
  * @param b The bytes the number is read from; they are not changed.
@@ -52,7 +52,7 @@
52
52
  * the length limit is the Kraft-sum repair of the counts per length. Not
53
53
  * ported from another implementation.
54
54
  */
55
- import { uncheckedGet, uncheckedSet, wrappingAdd, wrappingSub } from "nish:unsafe"
55
+ import { uncheckedGet, uncheckedSet, wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
56
56
  import { copyBytes, tryCopyBytes } from "nish/bytes"
57
57
 
58
58
  /** Level 0: stored blocks only, the input copied with five bytes of framing per 65,535. */
@@ -122,8 +122,8 @@ const DEFLATE_BLOCK_TOKENS: i32 = 16384
122
122
  /** A table position this far up is slid back down before the next block. */
123
123
  const DEFLATE_REBASE: i32 = 1073741824
124
124
 
125
- /** Huffman's multiplier for the hash (Knuth's 2^32 / phi). */
126
- const DEFLATE_HASH_MUL: i64 = 2654435761
125
+ /** Huffman's multiplier for the hash (Knuth's 2^32 / phi, 2654435761, in 32 signed bits). */
126
+ const DEFLATE_HASH_MUL: i32 = -1640531535
127
127
 
128
128
  /** `FixedDeflater`'s largest hash table, 2^14 entries; a call clears only as much as its input needs. */
129
129
  const FIXED_HASH_BITS: i32 = 14
@@ -241,6 +241,16 @@ const deflateLoad32 = (b: u8[], p: integer<0, 2147483639>): u32 =>
241
241
  (toU32(uncheckedGet(b, p + 2)) << 16) |
242
242
  (toU32(uncheckedGet(b, p + 3)) << 24)
243
243
 
244
+ /**
245
+ * The multiplicative hash of a little-endian `word`: the top `32 - shift`
246
+ * bits of its product with `DEFLATE_HASH_MUL`, a head-table index.
247
+ */
248
+ const deflateHash = (word: u32, shift: i32): i32 =>
249
+ // Multiplicative hashing keeps the low 32 bits of the product and reads its
250
+ // top bits, so the multiply wraps by design; the shift, not the product,
251
+ // bounds the index.
252
+ wrappingMul(toI32(word), DEFLATE_HASH_MUL) >>> shift
253
+
244
254
  /**
245
255
  * The little-endian 64-bit word at `b[p .. p + 8)`; the caller has checked the
246
256
  * range. Eight byte loads, not two `deflateLoad32`s: LLVM folds these into
@@ -1470,10 +1480,7 @@ export class Deflater {
1470
1480
  }
1471
1481
  const at: integer<0, 2147483639> = p
1472
1482
  const word: u32 = deflateLoad32(src, at)
1473
- const h: i32 =
1474
- minMatch === 3
1475
- ? toI32(((word << 8) * toU32(DEFLATE_HASH_MUL)) >>> 17)
1476
- : toI32((word * toU32(DEFLATE_HASH_MUL)) >>> 17)
1483
+ const h: i32 = minMatch === 3 ? deflateHash(word << 8, 17) : deflateHash(word, 17)
1477
1484
  let cand: i32 = uncheckedGet(head, h)
1478
1485
  uncheckedSet(head, h, v)
1479
1486
  uncheckedSet(prev, v & (DEFLATE_WINDOW - 1), cand)
@@ -1626,7 +1633,7 @@ export class Deflater {
1626
1633
  at0 = v
1627
1634
  const at: integer<0, 2147483639> = p
1628
1635
  const word: u32 = deflateLoad32(src, at)
1629
- const h: i32 = toI32((word * toU32(DEFLATE_HASH_MUL)) >>> 17)
1636
+ const h: i32 = deflateHash(word, 17)
1630
1637
  let cand: i32 = uncheckedGet(head, h)
1631
1638
  uncheckedSet(head, h, v)
1632
1639
  uncheckedSet(prev, v & (DEFLATE_WINDOW - 1), cand)
@@ -1711,7 +1718,7 @@ export class Deflater {
1711
1718
  for (let k: i32 = 1; k < cnt && k < 258; k += 1) {
1712
1719
  const q: i32 = p + k
1713
1720
  const vq: i32 = v + k
1714
- const hq: i32 = toI32((deflateLoad32(src, q) * toU32(DEFLATE_HASH_MUL)) >>> 17)
1721
+ const hq: i32 = deflateHash(deflateLoad32(src, q), 17)
1715
1722
  uncheckedSet(prev, vq & (DEFLATE_WINDOW - 1), uncheckedGet(head, hq))
1716
1723
  uncheckedSet(head, hq, vq)
1717
1724
  }
@@ -1847,9 +1854,7 @@ export class Deflater {
1847
1854
  while (q < insertEnd && q >= 0 && q <= 2147483639 && vq >= 0 && vq < 2147483647) {
1848
1855
  const qa: integer<0, 2147483639> = q
1849
1856
  const word: u32 = deflateLoad32(src, qa)
1850
- const hq: i32 = three
1851
- ? toI32(((word << 8) * toU32(DEFLATE_HASH_MUL)) >>> 17)
1852
- : toI32((word * toU32(DEFLATE_HASH_MUL)) >>> 17)
1857
+ const hq: i32 = three ? deflateHash(word << 8, 17) : deflateHash(word, 17)
1853
1858
  uncheckedSet(prev, vq & (DEFLATE_WINDOW - 1), uncheckedGet(head, hq))
1854
1859
  uncheckedSet(head, hq, vq)
1855
1860
  q += 1
@@ -2609,7 +2614,7 @@ export class FixedDeflater {
2609
2614
  bits += 1
2610
2615
  }
2611
2616
  head.fill(0, 0, 1 << bits)
2612
- const shift: u32 = toU32(32 - bits)
2617
+ const shift: i32 = 32 - bits
2613
2618
  // `deflateWindow` has checked both windows, so neither end is -1.
2614
2619
  const end: i32 = deflateWindowEnd(src, off, len)
2615
2620
  const outEnd: i32 = deflateWindowEnd(out, at, room)
@@ -2640,7 +2645,7 @@ export class FixedDeflater {
2640
2645
  if (pc <= last && pc <= 2147483639) {
2641
2646
  const pi: integer<0, 2147483639> = pc
2642
2647
  const w: u64 = deflateLoad64(src, pi)
2643
- const h: i32 = toI32((toU32(w) * toU32(DEFLATE_HASH_MUL)) >>> shift)
2648
+ const h: i32 = deflateHash(toU32(w), shift)
2644
2649
  // The table holds 0 or `q - off + 1` for an earlier position `q`, so
2645
2650
  // the candidate `pc - dist` lies in [off - 1, pc).
2646
2651
  const seen: i32 = uncheckedGet(head, h)
@@ -2714,7 +2719,7 @@ export class FixedDeflater {
2714
2719
  const q: i32 = next - 2
2715
2720
  if (q >= off && q <= last && q <= 2147483639) {
2716
2721
  const qi: integer<0, 2147483639> = q
2717
- const hq: i32 = toI32((deflateLoad32(src, qi) * toU32(DEFLATE_HASH_MUL)) >>> shift)
2722
+ const hq: i32 = deflateHash(deflateLoad32(src, qi), shift)
2718
2723
  uncheckedSet(head, hq, q - off + 1)
2719
2724
  }
2720
2725
  p = next