@amritk/nish-aarch64-linux 0.50.0 → 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/bin/nish +0 -0
- package/docs/AI.md +4 -1
- package/package.json +1 -1
- package/runtime/nish.d.ts +82 -2
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +7 -7
- package/std/base64.ts +61 -95
- package/std/bytes.ts +6 -4
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +193 -100
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +483 -148
- package/std/crypto/jwt.ts +19 -21
- package/std/crypto/p256.ts +5 -7
- package/std/crypto/pki-roots.ts +28 -8
- package/std/crypto/rsa-sign.ts +5 -2
- package/std/crypto/sha256.ts +81 -26
- package/std/crypto/sha512.ts +135 -49
- package/std/json-value.ts +14 -15
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/fetch.ts +108 -27
- package/std/net/handler.ts +11 -22
- package/std/net/hpack.ts +38 -19
- package/std/net/http-compress.ts +21 -12
- package/std/net/http-server.ts +9 -4
- package/std/net/http1-client-codec.ts +22 -15
- package/std/net/http1-codec.ts +37 -36
- package/std/net/http3-server.ts +127 -45
- package/std/net/http3.ts +99 -50
- package/std/net/postgres-async.ts +5 -10
- package/std/net/qpack.ts +83 -17
- package/std/net/quic-listener.ts +42 -27
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-stream.ts +29 -22
- package/std/net/quic.ts +48 -23
- package/std/net/static.ts +5 -3
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +54 -22
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls.ts +42 -12
- package/std/net/upstream.ts +18 -19
- package/std/net/url.ts +78 -21
- package/std/net/websocket.ts +3 -11
- package/std/net/webtransport.ts +95 -21
- package/std/raster.ts +12 -2
- package/std/term-pixels.ts +18 -2
- package/std/text.ts +5 -5
package/std/compress/checksum.ts
CHANGED
|
@@ -161,11 +161,32 @@ export class Crc32 {
|
|
|
161
161
|
checksumWindow("Crc32.update", buf, off, len)
|
|
162
162
|
return crc32Walk(this.table, this.braid, crc, buf, off, len)
|
|
163
163
|
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The CRC-32 of `buf[off .. end)` alone, a range given by its two ends:
|
|
167
|
+
* `update` from 0, or -1 for a range outside `buf`. It answers rather than
|
|
168
|
+
* panics, so the decoders under `nish/net/http-compress` reach no panic
|
|
169
|
+
* through it: a malformed body there is the peer's, never the program's.
|
|
170
|
+
*
|
|
171
|
+
* @internal Called by std/compress/gzip.ts's range decoder and header reader.
|
|
172
|
+
* @param buf The array that holds the bytes to checksum.
|
|
173
|
+
* @param off Where in `buf` the bytes start.
|
|
174
|
+
* @param end One past the last byte; `off` answers 0.
|
|
175
|
+
* @returns The CRC-32 of the range, as `update(0, buf, off, end - off)`
|
|
176
|
+
* answers it; or -1 when `off` is negative, past `end`, or `end` is past
|
|
177
|
+
* `buf`'s end.
|
|
178
|
+
*/
|
|
179
|
+
ofRange(buf: u8[], off: i32, end: i32): i64 {
|
|
180
|
+
if (off < 0 || off > end || end > toI32(buf.length)) {
|
|
181
|
+
return -1
|
|
182
|
+
}
|
|
183
|
+
return toI64(crc32Walk(this.table, this.braid, toU32(0), buf, off, end - off))
|
|
184
|
+
}
|
|
164
185
|
}
|
|
165
186
|
|
|
166
187
|
/**
|
|
167
188
|
* `Crc32.update`'s loop: the CRC-32 of the bytes `crc` was the CRC of,
|
|
168
|
-
* followed by `buf[off .. off + len)`, a window its
|
|
189
|
+
* followed by `buf[off .. off + len)`, a window its callers have checked,
|
|
169
190
|
* over the two tables `Crc32`'s constructor built at their full size and
|
|
170
191
|
* nothing replaces. Natively the compiler puts the call
|
|
171
192
|
* behind a branch on `nish_crc32_hw` and hands the same window to the
|
|
@@ -260,16 +281,25 @@ const crc32Walk = (t: u32[], bt: u32[], crc: u32, buf: u8[], off: i32, len: i32)
|
|
|
260
281
|
}
|
|
261
282
|
|
|
262
283
|
/**
|
|
263
|
-
* The CRC-32 of `buf[off .. off + len)
|
|
264
|
-
* call, which costs about as much as
|
|
265
|
-
*
|
|
284
|
+
* The CRC-32 of `buf[off .. off + len)`, or -1 for a window outside `buf`.
|
|
285
|
+
* It builds a `Crc32` for the one call, which costs about as much as
|
|
286
|
+
* checksumming 5 KB: keep a `Crc32` for anything repeated. It answers rather
|
|
287
|
+
* than panics, so a program that calls it builds under the default no-panic
|
|
288
|
+
* rule.
|
|
266
289
|
*
|
|
267
290
|
* @param buf The array that holds the bytes to checksum.
|
|
268
291
|
* @param off Where in `buf` the bytes start.
|
|
269
292
|
* @param len How many bytes to checksum; 0 answers 0.
|
|
270
|
-
* @returns The CRC-32 of the window, the value gzip's trailer carries
|
|
293
|
+
* @returns The CRC-32 of the window, the value gzip's trailer carries, as a
|
|
294
|
+
* u32 widened to i64; or -1 when `off` or `len` is negative or the window
|
|
295
|
+
* runs past `buf`'s end.
|
|
271
296
|
*/
|
|
272
|
-
export const crc32 = (buf: u8[], off: i32, len: i32):
|
|
297
|
+
export const crc32 = (buf: u8[], off: i32, len: i32): i64 => {
|
|
298
|
+
if (off < 0 || len < 0 || off > toI32(buf.length) - len) {
|
|
299
|
+
return -1
|
|
300
|
+
}
|
|
301
|
+
return new Crc32().ofRange(buf, off, toI32(toI64(off) + toI64(len)))
|
|
302
|
+
}
|
|
273
303
|
|
|
274
304
|
/**
|
|
275
305
|
* The Adler-32 of the bytes `adler` was the Adler-32 of, followed by
|
|
@@ -291,7 +321,7 @@ export const adler32Update = (adler: u32, buf: u8[], off: i32, len: i32): u32 =>
|
|
|
291
321
|
|
|
292
322
|
/**
|
|
293
323
|
* `adler32Update`'s loop: the Adler-32 of the bytes `adler` was the Adler-32
|
|
294
|
-
* of, followed by `buf[off .. off + len)`, a window its
|
|
324
|
+
* of, followed by `buf[off .. off + len)`, a window its callers have
|
|
295
325
|
* checked. Natively the compiler puts the call behind a branch on
|
|
296
326
|
* `nish_adler32_hw` and hands the same window to the runtime's
|
|
297
327
|
* `nish_adler32_update`, which runs it on SSSE3 or NEON vectors
|
|
@@ -331,11 +361,39 @@ const adler32Walk = (adler: u32, buf: u8[], off: i32, len: i32): u32 => {
|
|
|
331
361
|
}
|
|
332
362
|
|
|
333
363
|
/**
|
|
334
|
-
* The Adler-32 of `buf[off .. off + len)
|
|
364
|
+
* The Adler-32 of `buf[off .. off + len)`, or -1 for a window outside `buf`.
|
|
365
|
+
* It answers rather than panics, so a program that calls it builds under the
|
|
366
|
+
* default no-panic rule.
|
|
335
367
|
*
|
|
336
368
|
* @param buf The array that holds the bytes to checksum.
|
|
337
369
|
* @param off Where in `buf` the bytes start.
|
|
338
370
|
* @param len How many bytes to checksum; 0 answers 1.
|
|
339
|
-
* @returns The Adler-32 of the window, the value zlib's trailer carries
|
|
371
|
+
* @returns The Adler-32 of the window, the value zlib's trailer carries, as
|
|
372
|
+
* a u32 widened to i64; or -1 when `off` or `len` is negative or the
|
|
373
|
+
* window runs past `buf`'s end.
|
|
340
374
|
*/
|
|
341
|
-
export const adler32 = (buf: u8[], off: i32, len: i32):
|
|
375
|
+
export const adler32 = (buf: u8[], off: i32, len: i32): i64 => {
|
|
376
|
+
if (off < 0 || len < 0 || off > toI32(buf.length) - len) {
|
|
377
|
+
return -1
|
|
378
|
+
}
|
|
379
|
+
return toI64(adler32Walk(toU32(1), buf, off, len))
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* `adler32` over `buf[off .. end)`, a range given by its two ends, or -1 for
|
|
384
|
+
* a range outside `buf`. It answers rather than panics, so the decoders
|
|
385
|
+
* under `nish/net/http-compress` reach no panic through it.
|
|
386
|
+
*
|
|
387
|
+
* @internal Called by std/compress/zlib.ts's range decoder.
|
|
388
|
+
* @param buf The array that holds the bytes to checksum.
|
|
389
|
+
* @param off Where in `buf` the bytes start.
|
|
390
|
+
* @param end One past the last byte to checksum; `off` answers 1.
|
|
391
|
+
* @returns The Adler-32 of the range, the value zlib's trailer carries; or
|
|
392
|
+
* -1 when `off` is negative, past `end`, or `end` is past `buf`'s end.
|
|
393
|
+
*/
|
|
394
|
+
export const adler32Range = (buf: u8[], off: i32, end: i32): i64 => {
|
|
395
|
+
if (off < 0 || off > end || end > toI32(buf.length)) {
|
|
396
|
+
return -1
|
|
397
|
+
}
|
|
398
|
+
return toI64(adler32Walk(toU32(1), buf, off, end - off))
|
|
399
|
+
}
|
package/std/compress/deflate.ts
CHANGED
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
* ported from another implementation.
|
|
54
54
|
*/
|
|
55
55
|
import { uncheckedGet, uncheckedSet, wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
|
|
56
|
-
import {
|
|
56
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
57
57
|
|
|
58
58
|
/** Level 0: stored blocks only, the input copied with five bytes of framing per 65,535. */
|
|
59
59
|
export const DEFLATE_STORE: i32 = 0
|
|
@@ -98,6 +98,14 @@ export const DEFLATE_ERR_OUTPUT_LIMIT: i32 = -8
|
|
|
98
98
|
/** `decompress`: bytes follow the final block. */
|
|
99
99
|
export const DEFLATE_ERR_TRAILING: i32 = -15
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* A bad window or a negative `limit`, answered rather than panicked on by
|
|
103
|
+
* `deflateCompress`, `Deflater.compress`, `FixedDeflater.compress`,
|
|
104
|
+
* `Inflater.inflate`, `Inflater.decompress`, `inflateAll` and
|
|
105
|
+
* `decompressRange`.
|
|
106
|
+
*/
|
|
107
|
+
export const DEFLATE_ERR_WINDOW: i32 = -16
|
|
108
|
+
|
|
101
109
|
/** `Inflater.inflate`: the final block has ended; `inPos` is the first byte after it. */
|
|
102
110
|
export const INFLATE_DONE: i32 = 1
|
|
103
111
|
|
|
@@ -223,12 +231,9 @@ const inflateFill16 = (b: u8[], to: integer<0, 2147483631>, w: u64): void => {
|
|
|
223
231
|
deflateStore64(b, to8, w)
|
|
224
232
|
}
|
|
225
233
|
|
|
226
|
-
/**
|
|
227
|
-
const deflateWindow = (
|
|
228
|
-
|
|
229
|
-
panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${toI32(buf.length)} bytes`)
|
|
230
|
-
}
|
|
231
|
-
}
|
|
234
|
+
/** Whether `buf[off .. off + len)` lies inside `buf`, with `off` and `len` not negative. */
|
|
235
|
+
const deflateWindow = (buf: u8[], off: i32, len: i32): boolean =>
|
|
236
|
+
off >= 0 && len >= 0 && off <= toI32(buf.length) - len
|
|
232
237
|
|
|
233
238
|
/**
|
|
234
239
|
* The little-endian 32-bit word at `b[p .. p + 4)`; the caller has checked the
|
|
@@ -975,7 +980,10 @@ export class Deflater {
|
|
|
975
980
|
* lazily on longer chains, `DEFLATE_DEFAULT` being 6.
|
|
976
981
|
*/
|
|
977
982
|
constructor(level: i32) {
|
|
978
|
-
|
|
983
|
+
// `level * level > 81` is `level > 9` for a level past the first test,
|
|
984
|
+
// in the product shape a caller contract reads (src/caller-contract.ts),
|
|
985
|
+
// so a caller that has checked the level proves this panic away.
|
|
986
|
+
if (level < 0 || toI64(level) * toI64(level) > toI64(81)) {
|
|
979
987
|
panic(`Deflater: level ${level} is outside 0 to 9`)
|
|
980
988
|
}
|
|
981
989
|
this.level = level
|
|
@@ -1134,8 +1142,9 @@ export class Deflater {
|
|
|
1134
1142
|
/**
|
|
1135
1143
|
* Compresses `src[off .. off + len)` into `out` from `at` as one raw
|
|
1136
1144
|
* DEFLATE stream, and answers where it ends, or `DEFLATE_NO_ROOM` when
|
|
1137
|
-
* `out` cannot hold it: `deflateBound(len)` bytes always can
|
|
1138
|
-
*
|
|
1145
|
+
* `out` cannot hold it: `deflateBound(len)` bytes always can, or
|
|
1146
|
+
* `DEFLATE_ERR_WINDOW`, writing nothing, when a window lies outside its
|
|
1147
|
+
* array. Allocates nothing.
|
|
1139
1148
|
*
|
|
1140
1149
|
* @param src The array that holds the data to compress.
|
|
1141
1150
|
* @param off Where in `src` the data starts.
|
|
@@ -1144,28 +1153,16 @@ export class Deflater {
|
|
|
1144
1153
|
* @param at Where in `out` the stream starts.
|
|
1145
1154
|
* @returns The index in `out` one past the stream's last byte, so the
|
|
1146
1155
|
* stream is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
|
|
1147
|
-
* bytes of `out` from `at` may have been overwritten and mean nothing
|
|
1148
|
-
*
|
|
1156
|
+
* bytes of `out` from `at` may have been overwritten and mean nothing;
|
|
1157
|
+
* or `DEFLATE_ERR_WINDOW` when `src[off .. off + len)` is not inside
|
|
1158
|
+
* `src` or `at` is not inside `out` (its end included). Each call is a
|
|
1159
|
+
* whole stream: nothing carries over to the next.
|
|
1149
1160
|
*/
|
|
1150
1161
|
compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
|
|
1151
|
-
deflateWindow(
|
|
1152
|
-
|
|
1153
|
-
this.bitBuf = 0
|
|
1154
|
-
this.bitCount = 0
|
|
1155
|
-
this.overrun = false
|
|
1156
|
-
const srcEnd: i32 = deflateWindowEnd(src, off, len)
|
|
1157
|
-
this.begin(off)
|
|
1158
|
-
const end: i32 = this.compressRange(src, off, srcEnd, off, true, out, at, toI32(out.length))
|
|
1159
|
-
this.finish(srcEnd)
|
|
1160
|
-
if (end < 0) {
|
|
1161
|
-
// Every other -1 under `compressRange` (a table not its size, a code
|
|
1162
|
-
// that does not build, a position past i32) cannot happen: the
|
|
1163
|
-
// constructor sizes the tables, the codes are built from frequencies
|
|
1164
|
-
// this class counted, and `begin` and `rebase` keep positions low. So
|
|
1165
|
-
// this is a lack of room, which a larger `out` cures.
|
|
1166
|
-
return DEFLATE_NO_ROOM
|
|
1162
|
+
if (!deflateWindow(src, off, len) || !deflateWindow(out, at, 0)) {
|
|
1163
|
+
return DEFLATE_ERR_WINDOW
|
|
1167
1164
|
}
|
|
1168
|
-
return this
|
|
1165
|
+
return deflaterStream(this, src, off, len, out, at)
|
|
1169
1166
|
}
|
|
1170
1167
|
|
|
1171
1168
|
/**
|
|
@@ -2452,6 +2449,29 @@ export class Deflater {
|
|
|
2452
2449
|
}
|
|
2453
2450
|
}
|
|
2454
2451
|
|
|
2452
|
+
/**
|
|
2453
|
+
* `Deflater.compress` past its window checks: one whole raw DEFLATE stream
|
|
2454
|
+
* of `src[off .. off + len)` into `out` from `at`, whose windows the caller
|
|
2455
|
+
* has checked: `Deflater.compress` and `deflateCompress` answer
|
|
2456
|
+
* `DEFLATE_ERR_WINDOW` for a bad one.
|
|
2457
|
+
*/
|
|
2458
|
+
const deflaterStream = (d: Deflater, src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 => {
|
|
2459
|
+
d.reset()
|
|
2460
|
+
const srcEnd: i32 = deflateWindowEnd(src, off, len)
|
|
2461
|
+
d.begin(off)
|
|
2462
|
+
const end: i32 = d.compressRange(src, off, srcEnd, off, true, out, at, toI32(out.length))
|
|
2463
|
+
d.finish(srcEnd)
|
|
2464
|
+
if (end < 0) {
|
|
2465
|
+
// Every other -1 under `compressRange` (a table not its size, a code
|
|
2466
|
+
// that does not build, a position past i32) cannot happen: the
|
|
2467
|
+
// constructor sizes the tables, the codes are built from frequencies
|
|
2468
|
+
// the class counted, and `begin` and `rebase` keep positions low. So
|
|
2469
|
+
// this is a lack of room, which a larger `out` cures.
|
|
2470
|
+
return DEFLATE_NO_ROOM
|
|
2471
|
+
}
|
|
2472
|
+
return d.flushBits(out, end, toI32(out.length))
|
|
2473
|
+
}
|
|
2474
|
+
|
|
2455
2475
|
/**
|
|
2456
2476
|
* The most bytes `FixedDeflater.compress` writes for `n` bytes of input: nine
|
|
2457
2477
|
* bits a byte, the block's header and end code, and the eight bytes of slack
|
|
@@ -2570,7 +2590,8 @@ export class FixedDeflater {
|
|
|
2570
2590
|
* soon as it would need more than `room` bytes: a caller that only wants
|
|
2571
2591
|
* the output when it is smaller passes the most it will take, and an input
|
|
2572
2592
|
* that does not compress is abandoned part way. `fixedDeflateBound(len)`
|
|
2573
|
-
* always fits. A window outside its array
|
|
2593
|
+
* always fits. A window outside its array is `DEFLATE_ERR_WINDOW`, with
|
|
2594
|
+
* nothing written. Answers
|
|
2574
2595
|
* `DEFLATE_NO_ROOM` too, writing nothing, when a table is not its size,
|
|
2575
2596
|
* which the constructor rules out. Allocates nothing.
|
|
2576
2597
|
*
|
|
@@ -2584,11 +2605,13 @@ export class FixedDeflater {
|
|
|
2584
2605
|
* @returns The index in `out` one past the block's last byte, so the
|
|
2585
2606
|
* stream is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
|
|
2586
2607
|
* bytes of `out[at .. at + room)` may have been overwritten and mean
|
|
2587
|
-
* nothing
|
|
2608
|
+
* nothing; or `DEFLATE_ERR_WINDOW` when `src[off .. off + len)` or
|
|
2609
|
+
* `out[at .. at + room)` is not inside its array.
|
|
2588
2610
|
*/
|
|
2589
2611
|
compress(src: u8[], off: i32, len: i32, out: u8[], at: i32, room: i32): i32 {
|
|
2590
|
-
deflateWindow(
|
|
2591
|
-
|
|
2612
|
+
if (!deflateWindow(src, off, len) || !deflateWindow(out, at, room)) {
|
|
2613
|
+
return DEFLATE_ERR_WINDOW
|
|
2614
|
+
}
|
|
2592
2615
|
const head: i32[] = this.head
|
|
2593
2616
|
const literal: i32[] = this.literal
|
|
2594
2617
|
const length: i32[] = this.length
|
|
@@ -3662,8 +3685,9 @@ export class Inflater {
|
|
|
3662
3685
|
* the last call stopped: the resumable step under `decompress`, for a
|
|
3663
3686
|
* stream that arrives in pieces or an output buffer that is drained
|
|
3664
3687
|
* between calls. `GzipDecompressor`, `GzipReader` and `ZlibDecompressor`
|
|
3665
|
-
* are built on it. `inPos` and `outPos` say where it stopped.
|
|
3666
|
-
* outside
|
|
3688
|
+
* are built on it. `inPos` and `outPos` say where it stopped. A window
|
|
3689
|
+
* outside its array is `DEFLATE_ERR_WINDOW`, with nothing read or written.
|
|
3690
|
+
* Allocates nothing.
|
|
3667
3691
|
*
|
|
3668
3692
|
* @param src The array that holds the compressed input.
|
|
3669
3693
|
* @param ip Where in `src` this call's input starts; the bytes before it
|
|
@@ -3681,7 +3705,8 @@ export class Inflater {
|
|
|
3681
3705
|
* @returns `INFLATE_DONE` once the final block has ended, with `inPos` the
|
|
3682
3706
|
* first byte after it; `INFLATE_NEED_INPUT` when the input ran out with
|
|
3683
3707
|
* `more`; `INFLATE_NEED_OUTPUT` when the next symbol does not fit before
|
|
3684
|
-
* `outEnd`;
|
|
3708
|
+
* `outEnd`; `DEFLATE_ERR_WINDOW` for a window outside its array; or
|
|
3709
|
+
* another negative `DEFLATE_ERR_*` for a malformed stream.
|
|
3685
3710
|
*/
|
|
3686
3711
|
inflate(
|
|
3687
3712
|
src: u8[],
|
|
@@ -3692,12 +3717,51 @@ export class Inflater {
|
|
|
3692
3717
|
op: i32,
|
|
3693
3718
|
outEnd: i32,
|
|
3694
3719
|
histStart: i32
|
|
3720
|
+
): i32 {
|
|
3721
|
+
return this.window(src, ip, inEnd, more, out, op, outEnd, histStart)
|
|
3722
|
+
}
|
|
3723
|
+
|
|
3724
|
+
/**
|
|
3725
|
+
* `inflate` for a whole stream in one call: `more` is false and matches
|
|
3726
|
+
* copy from `op` on, which is what a one-shot decoder passes. A window
|
|
3727
|
+
* outside its array is `DEFLATE_ERR_WINDOW`, with nothing read or written.
|
|
3728
|
+
*
|
|
3729
|
+
* @internal Called by the one-shot decoders of std/compress/gzip.ts and
|
|
3730
|
+
* std/compress/zlib.ts.
|
|
3731
|
+
* @param src The array that holds the compressed stream.
|
|
3732
|
+
* @param ip Where in `src` the stream starts.
|
|
3733
|
+
* @param inEnd One past the stream's last byte; running out before the
|
|
3734
|
+
* final block is `DEFLATE_ERR_TRUNCATED`.
|
|
3735
|
+
* @param out The array the decoded bytes are written into.
|
|
3736
|
+
* @param op Where in `out` the first decoded byte goes.
|
|
3737
|
+
* @param outEnd One past the last byte of `out` this call may write.
|
|
3738
|
+
* @returns As `inflate` answers, never `INFLATE_NEED_INPUT`; or
|
|
3739
|
+
* `DEFLATE_ERR_WINDOW` for a window outside its array.
|
|
3740
|
+
*/
|
|
3741
|
+
inflateAll(src: u8[], ip: i32, inEnd: i32, out: u8[], op: i32, outEnd: i32): i32 {
|
|
3742
|
+
return this.window(src, ip, inEnd, false, out, op, outEnd, op)
|
|
3743
|
+
}
|
|
3744
|
+
|
|
3745
|
+
/**
|
|
3746
|
+
* `inflate` and `inflateAll`: checks both windows, answering
|
|
3747
|
+
* `DEFLATE_ERR_WINDOW` for one outside its array, then runs the state
|
|
3748
|
+
* machine. Its one call of `step` keeps that inlined.
|
|
3749
|
+
*/
|
|
3750
|
+
private window(
|
|
3751
|
+
src: u8[],
|
|
3752
|
+
ip: i32,
|
|
3753
|
+
inEnd: i32,
|
|
3754
|
+
more: boolean,
|
|
3755
|
+
out: u8[],
|
|
3756
|
+
op: i32,
|
|
3757
|
+
outEnd: i32,
|
|
3758
|
+
histStart: i32
|
|
3695
3759
|
): i32 {
|
|
3696
3760
|
if (ip < 0 || ip > inEnd || inEnd > toI32(src.length)) {
|
|
3697
|
-
|
|
3761
|
+
return DEFLATE_ERR_WINDOW
|
|
3698
3762
|
}
|
|
3699
3763
|
if (histStart < 0 || histStart > op || op > outEnd || outEnd > toI32(out.length)) {
|
|
3700
|
-
|
|
3764
|
+
return DEFLATE_ERR_WINDOW
|
|
3701
3765
|
}
|
|
3702
3766
|
this.inPos = ip
|
|
3703
3767
|
this.outPos = op
|
|
@@ -3792,46 +3856,9 @@ export class Inflater {
|
|
|
3792
3856
|
return DEFLATE_ERR_BLOCK_TYPE
|
|
3793
3857
|
}
|
|
3794
3858
|
} else if (s === INF_S_STORED) {
|
|
3795
|
-
const
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
// every step keeps both.
|
|
3799
|
-
if (ip < 0 || ip > inEnd || opos < 0 || opos > outEnd) {
|
|
3800
|
-
return DEFLATE_ERR_TRUNCATED
|
|
3801
|
-
}
|
|
3802
|
-
const inAvail: i32 = inEnd - ip
|
|
3803
|
-
const outAvail: i32 = outEnd - opos
|
|
3804
|
-
if (inAvail < 0 || outAvail < 0 || inEnd < 0 || outEnd < 0) {
|
|
3805
|
-
return DEFLATE_ERR_TRUNCATED
|
|
3806
|
-
}
|
|
3807
|
-
// A stored length is 16 bits.
|
|
3808
|
-
const left: i32 = this.storedLeft
|
|
3809
|
-
if (left < 0 || left > 65535) {
|
|
3810
|
-
return DEFLATE_ERR_STORED_LENGTH
|
|
3811
|
-
}
|
|
3812
|
-
let n: i32 = left
|
|
3813
|
-
if (n > inAvail) {
|
|
3814
|
-
n = inAvail
|
|
3815
|
-
}
|
|
3816
|
-
if (n > outAvail) {
|
|
3817
|
-
n = outAvail
|
|
3818
|
-
}
|
|
3819
|
-
// `n` is at most each room, and each room at least zero: `ip + n`
|
|
3820
|
-
// and `opos + n` are the ends less what is left of each room.
|
|
3821
|
-
const inGap: i32 = inAvail - n
|
|
3822
|
-
const outGap: i32 = outAvail - n
|
|
3823
|
-
if (inGap < 0 || outGap < 0) {
|
|
3824
|
-
return DEFLATE_ERR_TRUNCATED
|
|
3825
|
-
}
|
|
3826
|
-
copyBytes(out, opos, src, ip, n)
|
|
3827
|
-
this.inPos = inEnd - inGap
|
|
3828
|
-
this.outPos = outEnd - outGap
|
|
3829
|
-
this.storedLeft = left - n
|
|
3830
|
-
if (this.storedLeft > 0) {
|
|
3831
|
-
if (this.outPos === outEnd) {
|
|
3832
|
-
return INFLATE_NEED_OUTPUT
|
|
3833
|
-
}
|
|
3834
|
-
return more ? INFLATE_NEED_INPUT : DEFLATE_ERR_TRUNCATED
|
|
3859
|
+
const r: i32 = this.stored(src, inEnd, more, out, outEnd)
|
|
3860
|
+
if (r !== 0) {
|
|
3861
|
+
return r
|
|
3835
3862
|
}
|
|
3836
3863
|
this.state = INF_S_BLOCK
|
|
3837
3864
|
} else {
|
|
@@ -3843,6 +3870,61 @@ export class Inflater {
|
|
|
3843
3870
|
}
|
|
3844
3871
|
}
|
|
3845
3872
|
|
|
3873
|
+
/**
|
|
3874
|
+
* One stored block's copy: as much of what is left of it as both rooms
|
|
3875
|
+
* hold. Answers 0 once the block is done, `INFLATE_NEED_OUTPUT` or
|
|
3876
|
+
* `INFLATE_NEED_INPUT` when a room ran out first, and
|
|
3877
|
+
* `DEFLATE_ERR_TRUNCATED` or `DEFLATE_ERR_STORED_LENGTH` for a window or a
|
|
3878
|
+
* length `step` and `inflate` rule out.
|
|
3879
|
+
*/
|
|
3880
|
+
private stored(src: u8[], inEnd: i32, more: boolean, out: u8[], outEnd: i32): i32 {
|
|
3881
|
+
const ip: i32 = this.inPos
|
|
3882
|
+
const opos: i32 = this.outPos
|
|
3883
|
+
// `inflate` checked `inPos <= inEnd` and `outPos <= outEnd`, and
|
|
3884
|
+
// every step keeps both.
|
|
3885
|
+
if (ip < 0 || ip > inEnd || opos < 0 || opos > outEnd) {
|
|
3886
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3887
|
+
}
|
|
3888
|
+
const inAvail: i32 = inEnd - ip
|
|
3889
|
+
const outAvail: i32 = outEnd - opos
|
|
3890
|
+
if (inAvail < 0 || outAvail < 0 || inEnd < 0 || outEnd < 0) {
|
|
3891
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3892
|
+
}
|
|
3893
|
+
// A stored length is 16 bits.
|
|
3894
|
+
const left: i32 = this.storedLeft
|
|
3895
|
+
if (left < 0 || left > 65535) {
|
|
3896
|
+
return DEFLATE_ERR_STORED_LENGTH
|
|
3897
|
+
}
|
|
3898
|
+
let n: i32 = left
|
|
3899
|
+
if (n > inAvail) {
|
|
3900
|
+
n = inAvail
|
|
3901
|
+
}
|
|
3902
|
+
if (n > outAvail) {
|
|
3903
|
+
n = outAvail
|
|
3904
|
+
}
|
|
3905
|
+
// `n` is at most each room, and each room at least zero: `ip + n`
|
|
3906
|
+
// and `opos + n` are the ends less what is left of each room.
|
|
3907
|
+
const inGap: i32 = inAvail - n
|
|
3908
|
+
const outGap: i32 = outAvail - n
|
|
3909
|
+
if (inGap < 0 || outGap < 0) {
|
|
3910
|
+
return DEFLATE_ERR_TRUNCATED
|
|
3911
|
+
}
|
|
3912
|
+
// `inflate` checked both windows, so the copy always fits; the prover
|
|
3913
|
+
// cannot follow them through the fields. One that did not answers
|
|
3914
|
+
// `DEFLATE_ERR_TRUNCATED` through the same exit as a block cut short.
|
|
3915
|
+
const copied: boolean = tryCopyBytes(out, opos, src, ip, n)
|
|
3916
|
+
this.inPos = inEnd - inGap
|
|
3917
|
+
this.outPos = outEnd - outGap
|
|
3918
|
+
this.storedLeft = left - n
|
|
3919
|
+
if (!copied || this.storedLeft > 0) {
|
|
3920
|
+
if (copied && this.outPos === outEnd) {
|
|
3921
|
+
return INFLATE_NEED_OUTPUT
|
|
3922
|
+
}
|
|
3923
|
+
return more && copied ? INFLATE_NEED_INPUT : DEFLATE_ERR_TRUNCATED
|
|
3924
|
+
}
|
|
3925
|
+
return 0
|
|
3926
|
+
}
|
|
3927
|
+
|
|
3846
3928
|
/**
|
|
3847
3929
|
* The Huffman block loop: decodes symbols until the end of the block
|
|
3848
3930
|
* (answers 0, the state back at a block header), or until the input or
|
|
@@ -4253,9 +4335,9 @@ export class Inflater {
|
|
|
4253
4335
|
* from `at`, writing at most `limit` bytes and never past `out`'s end, and
|
|
4254
4336
|
* answers where the output ends; or `DEFLATE_ERR_OUTPUT_LIMIT` when the
|
|
4255
4337
|
* stream holds more, `DEFLATE_ERR_TRAILING` when bytes follow its final
|
|
4256
|
-
* block,
|
|
4257
|
-
*
|
|
4258
|
-
* nothing.
|
|
4338
|
+
* block, `DEFLATE_ERR_WINDOW` for a window outside its array or a negative
|
|
4339
|
+
* `limit`, or another negative `DEFLATE_ERR_*` for a malformed stream.
|
|
4340
|
+
* Allocates nothing.
|
|
4259
4341
|
*
|
|
4260
4342
|
* @param src The array that holds the raw DEFLATE stream.
|
|
4261
4343
|
* @param off Where in `src` the stream starts.
|
|
@@ -4271,40 +4353,37 @@ export class Inflater {
|
|
|
4271
4353
|
* nothing. Either way `consumed` says how far into `src` it read.
|
|
4272
4354
|
*/
|
|
4273
4355
|
decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
|
|
4274
|
-
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
// `deflateWindow` checked `off + len` against `src` and `at` against
|
|
4280
|
-
// `out`, so the stream ends `src.length` less the bytes after it, and the
|
|
4281
|
-
// output `out.length` less the room it leaves; the input is read from
|
|
4282
|
-
// `off` on.
|
|
4283
|
-
if (off < 0 || len < 0 || at < 0) {
|
|
4356
|
+
// The stream ends `src.length` less the bytes after it, and the output
|
|
4357
|
+
// `out.length` less the room it leaves; the input is read from `off` on.
|
|
4358
|
+
// A window outside its array, or a negative `limit`, fails one of these
|
|
4359
|
+
// tests and answers `DEFLATE_ERR_WINDOW`.
|
|
4360
|
+
if (off < 0 || len < 0 || at < 0 || limit < 0) {
|
|
4284
4361
|
this.consumed = 0
|
|
4285
|
-
return
|
|
4362
|
+
return DEFLATE_ERR_WINDOW
|
|
4286
4363
|
}
|
|
4287
4364
|
const after: i32 = toI32(src.length) - off
|
|
4288
4365
|
const room: i32 = toI32(out.length) - at
|
|
4289
4366
|
if (after < 0 || room < 0) {
|
|
4290
4367
|
this.consumed = 0
|
|
4291
|
-
return
|
|
4368
|
+
return DEFLATE_ERR_WINDOW
|
|
4292
4369
|
}
|
|
4293
4370
|
const inGap: i32 = after - len
|
|
4294
4371
|
const take: i32 = limit < room ? limit : room
|
|
4295
4372
|
if (inGap < 0 || take < 0) {
|
|
4296
4373
|
this.consumed = 0
|
|
4297
|
-
return
|
|
4374
|
+
return DEFLATE_ERR_WINDOW
|
|
4298
4375
|
}
|
|
4299
4376
|
const outGap: i32 = room - take
|
|
4300
4377
|
if (outGap < 0) {
|
|
4301
4378
|
this.consumed = 0
|
|
4302
|
-
return
|
|
4379
|
+
return DEFLATE_ERR_WINDOW
|
|
4303
4380
|
}
|
|
4304
4381
|
const inEnd: i32 = toI32(src.length) - inGap
|
|
4305
4382
|
const end: i32 = toI32(out.length) - outGap
|
|
4306
4383
|
this.reset()
|
|
4307
|
-
|
|
4384
|
+
// The tests above put both windows inside their arrays, so `inflateAll`
|
|
4385
|
+
// answers no `DEFLATE_ERR_WINDOW` here.
|
|
4386
|
+
const r: i32 = this.inflateAll(src, off, inEnd, out, at, end)
|
|
4308
4387
|
const pos: i32 = this.inPos
|
|
4309
4388
|
if (pos < 0) {
|
|
4310
4389
|
this.consumed = 0
|
|
@@ -4324,6 +4403,9 @@ export class Inflater {
|
|
|
4324
4403
|
}
|
|
4325
4404
|
}
|
|
4326
4405
|
|
|
4406
|
+
/** `deflateCompress`: a level outside 0 to 9. */
|
|
4407
|
+
export const DEFLATE_ERR_LEVEL: i32 = -17
|
|
4408
|
+
|
|
4327
4409
|
/**
|
|
4328
4410
|
* `Deflater.compress` at `level`, building the compressor for this call
|
|
4329
4411
|
* alone: 330 KB allocated and filled each time, so keep a `Deflater` for
|
|
@@ -4334,12 +4416,23 @@ export class Inflater {
|
|
|
4334
4416
|
* @param len How many bytes of data to compress.
|
|
4335
4417
|
* @param out The caller's array the stream is written into.
|
|
4336
4418
|
* @param at Where in `out` the stream starts.
|
|
4337
|
-
* @param level How hard to compress, 0 to 9; another level
|
|
4419
|
+
* @param level How hard to compress, 0 to 9; another level answers
|
|
4420
|
+
* `DEFLATE_ERR_LEVEL`.
|
|
4338
4421
|
* @returns The index in `out` one past the stream's last byte, or
|
|
4339
|
-
* `DEFLATE_NO_ROOM`, as `Deflater.compress` answers
|
|
4422
|
+
* `DEFLATE_NO_ROOM`, as `Deflater.compress` answers; `DEFLATE_ERR_LEVEL`
|
|
4423
|
+
* for a level outside 0 to 9, or `DEFLATE_ERR_WINDOW` for a window
|
|
4424
|
+
* outside its array, before anything is allocated.
|
|
4340
4425
|
*/
|
|
4341
|
-
export const deflateCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 =>
|
|
4342
|
-
|
|
4426
|
+
export const deflateCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 => {
|
|
4427
|
+
// The constructor's own test, so the prover reads the level as checked.
|
|
4428
|
+
if (level < 0 || toI64(level) * toI64(level) > toI64(81)) {
|
|
4429
|
+
return DEFLATE_ERR_LEVEL
|
|
4430
|
+
}
|
|
4431
|
+
if (deflateWindowEnd(src, off, len) < 0 || at < 0 || at > toI32(out.length)) {
|
|
4432
|
+
return DEFLATE_ERR_WINDOW
|
|
4433
|
+
}
|
|
4434
|
+
return deflaterStream(new Deflater(level), src, off, len, out, at)
|
|
4435
|
+
}
|
|
4343
4436
|
|
|
4344
4437
|
/**
|
|
4345
4438
|
* `Inflater.decompress`, building the decompressor for this call alone.
|