@amritk/nish-x86_64-linux 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.
Files changed (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
@@ -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 one caller has checked,
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)`. It builds a `Crc32` for the one
264
- * call, which costs about as much as checksumming 5 KB: keep a `Crc32` for
265
- * anything repeated. A window outside `buf` panics.
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): u32 => new Crc32().update(toU32(0), buf, off, len)
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 one caller has
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)`. A window outside `buf` panics.
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): u32 => adler32Update(toU32(1), buf, off, len)
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
+ }
@@ -52,8 +52,8 @@
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"
56
- import { copyBytes, tryCopyBytes } from "nish/bytes"
55
+ import { uncheckedGet, uncheckedSet, wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
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
 
@@ -122,8 +130,8 @@ const DEFLATE_BLOCK_TOKENS: i32 = 16384
122
130
  /** A table position this far up is slid back down before the next block. */
123
131
  const DEFLATE_REBASE: i32 = 1073741824
124
132
 
125
- /** Huffman's multiplier for the hash (Knuth's 2^32 / phi). */
126
- const DEFLATE_HASH_MUL: i64 = 2654435761
133
+ /** Huffman's multiplier for the hash (Knuth's 2^32 / phi, 2654435761, in 32 signed bits). */
134
+ const DEFLATE_HASH_MUL: i32 = -1640531535
127
135
 
128
136
  /** `FixedDeflater`'s largest hash table, 2^14 entries; a call clears only as much as its input needs. */
129
137
  const FIXED_HASH_BITS: i32 = 14
@@ -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
- /** Panics unless `buf[off .. off + len)` lies inside `buf`. */
227
- const deflateWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
228
- if (off < 0 || len < 0 || off > toI32(buf.length) - len) {
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
@@ -241,6 +246,16 @@ const deflateLoad32 = (b: u8[], p: integer<0, 2147483639>): u32 =>
241
246
  (toU32(uncheckedGet(b, p + 2)) << 16) |
242
247
  (toU32(uncheckedGet(b, p + 3)) << 24)
243
248
 
249
+ /**
250
+ * The multiplicative hash of a little-endian `word`: the top `32 - shift`
251
+ * bits of its product with `DEFLATE_HASH_MUL`, a head-table index.
252
+ */
253
+ const deflateHash = (word: u32, shift: i32): i32 =>
254
+ // Multiplicative hashing keeps the low 32 bits of the product and reads its
255
+ // top bits, so the multiply wraps by design; the shift, not the product,
256
+ // bounds the index.
257
+ wrappingMul(toI32(word), DEFLATE_HASH_MUL) >>> shift
258
+
244
259
  /**
245
260
  * The little-endian 64-bit word at `b[p .. p + 8)`; the caller has checked the
246
261
  * range. Eight byte loads, not two `deflateLoad32`s: LLVM folds these into
@@ -965,7 +980,10 @@ export class Deflater {
965
980
  * lazily on longer chains, `DEFLATE_DEFAULT` being 6.
966
981
  */
967
982
  constructor(level: i32) {
968
- if (level < 0 || level > 9) {
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)) {
969
987
  panic(`Deflater: level ${level} is outside 0 to 9`)
970
988
  }
971
989
  this.level = level
@@ -1124,8 +1142,9 @@ export class Deflater {
1124
1142
  /**
1125
1143
  * Compresses `src[off .. off + len)` into `out` from `at` as one raw
1126
1144
  * DEFLATE stream, and answers where it ends, or `DEFLATE_NO_ROOM` when
1127
- * `out` cannot hold it: `deflateBound(len)` bytes always can. A window
1128
- * outside its array panics. Allocates nothing.
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.
1129
1148
  *
1130
1149
  * @param src The array that holds the data to compress.
1131
1150
  * @param off Where in `src` the data starts.
@@ -1134,28 +1153,16 @@ export class Deflater {
1134
1153
  * @param at Where in `out` the stream starts.
1135
1154
  * @returns The index in `out` one past the stream's last byte, so the
1136
1155
  * stream is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
1137
- * bytes of `out` from `at` may have been overwritten and mean nothing.
1138
- * Each call is a whole stream: nothing carries over to the next.
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.
1139
1160
  */
1140
1161
  compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
1141
- deflateWindow("Deflater.compress", src, off, len)
1142
- deflateWindow("Deflater.compress", out, at, 0)
1143
- this.bitBuf = 0
1144
- this.bitCount = 0
1145
- this.overrun = false
1146
- const srcEnd: i32 = deflateWindowEnd(src, off, len)
1147
- this.begin(off)
1148
- const end: i32 = this.compressRange(src, off, srcEnd, off, true, out, at, toI32(out.length))
1149
- this.finish(srcEnd)
1150
- if (end < 0) {
1151
- // Every other -1 under `compressRange` (a table not its size, a code
1152
- // that does not build, a position past i32) cannot happen: the
1153
- // constructor sizes the tables, the codes are built from frequencies
1154
- // this class counted, and `begin` and `rebase` keep positions low. So
1155
- // this is a lack of room, which a larger `out` cures.
1156
- return DEFLATE_NO_ROOM
1162
+ if (!deflateWindow(src, off, len) || !deflateWindow(out, at, 0)) {
1163
+ return DEFLATE_ERR_WINDOW
1157
1164
  }
1158
- return this.flushBits(out, end, toI32(out.length))
1165
+ return deflaterStream(this, src, off, len, out, at)
1159
1166
  }
1160
1167
 
1161
1168
  /**
@@ -1470,10 +1477,7 @@ export class Deflater {
1470
1477
  }
1471
1478
  const at: integer<0, 2147483639> = p
1472
1479
  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)
1480
+ const h: i32 = minMatch === 3 ? deflateHash(word << 8, 17) : deflateHash(word, 17)
1477
1481
  let cand: i32 = uncheckedGet(head, h)
1478
1482
  uncheckedSet(head, h, v)
1479
1483
  uncheckedSet(prev, v & (DEFLATE_WINDOW - 1), cand)
@@ -1626,7 +1630,7 @@ export class Deflater {
1626
1630
  at0 = v
1627
1631
  const at: integer<0, 2147483639> = p
1628
1632
  const word: u32 = deflateLoad32(src, at)
1629
- const h: i32 = toI32((word * toU32(DEFLATE_HASH_MUL)) >>> 17)
1633
+ const h: i32 = deflateHash(word, 17)
1630
1634
  let cand: i32 = uncheckedGet(head, h)
1631
1635
  uncheckedSet(head, h, v)
1632
1636
  uncheckedSet(prev, v & (DEFLATE_WINDOW - 1), cand)
@@ -1711,7 +1715,7 @@ export class Deflater {
1711
1715
  for (let k: i32 = 1; k < cnt && k < 258; k += 1) {
1712
1716
  const q: i32 = p + k
1713
1717
  const vq: i32 = v + k
1714
- const hq: i32 = toI32((deflateLoad32(src, q) * toU32(DEFLATE_HASH_MUL)) >>> 17)
1718
+ const hq: i32 = deflateHash(deflateLoad32(src, q), 17)
1715
1719
  uncheckedSet(prev, vq & (DEFLATE_WINDOW - 1), uncheckedGet(head, hq))
1716
1720
  uncheckedSet(head, hq, vq)
1717
1721
  }
@@ -1847,9 +1851,7 @@ export class Deflater {
1847
1851
  while (q < insertEnd && q >= 0 && q <= 2147483639 && vq >= 0 && vq < 2147483647) {
1848
1852
  const qa: integer<0, 2147483639> = q
1849
1853
  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)
1854
+ const hq: i32 = three ? deflateHash(word << 8, 17) : deflateHash(word, 17)
1853
1855
  uncheckedSet(prev, vq & (DEFLATE_WINDOW - 1), uncheckedGet(head, hq))
1854
1856
  uncheckedSet(head, hq, vq)
1855
1857
  q += 1
@@ -2447,6 +2449,29 @@ export class Deflater {
2447
2449
  }
2448
2450
  }
2449
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
+
2450
2475
  /**
2451
2476
  * The most bytes `FixedDeflater.compress` writes for `n` bytes of input: nine
2452
2477
  * bits a byte, the block's header and end code, and the eight bytes of slack
@@ -2565,7 +2590,8 @@ export class FixedDeflater {
2565
2590
  * soon as it would need more than `room` bytes: a caller that only wants
2566
2591
  * the output when it is smaller passes the most it will take, and an input
2567
2592
  * that does not compress is abandoned part way. `fixedDeflateBound(len)`
2568
- * always fits. A window outside its array panics. Answers
2593
+ * always fits. A window outside its array is `DEFLATE_ERR_WINDOW`, with
2594
+ * nothing written. Answers
2569
2595
  * `DEFLATE_NO_ROOM` too, writing nothing, when a table is not its size,
2570
2596
  * which the constructor rules out. Allocates nothing.
2571
2597
  *
@@ -2579,11 +2605,13 @@ export class FixedDeflater {
2579
2605
  * @returns The index in `out` one past the block's last byte, so the
2580
2606
  * stream is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
2581
2607
  * bytes of `out[at .. at + room)` may have been overwritten and mean
2582
- * nothing.
2608
+ * nothing; or `DEFLATE_ERR_WINDOW` when `src[off .. off + len)` or
2609
+ * `out[at .. at + room)` is not inside its array.
2583
2610
  */
2584
2611
  compress(src: u8[], off: i32, len: i32, out: u8[], at: i32, room: i32): i32 {
2585
- deflateWindow("FixedDeflater.compress", src, off, len)
2586
- deflateWindow("FixedDeflater.compress", out, at, room)
2612
+ if (!deflateWindow(src, off, len) || !deflateWindow(out, at, room)) {
2613
+ return DEFLATE_ERR_WINDOW
2614
+ }
2587
2615
  const head: i32[] = this.head
2588
2616
  const literal: i32[] = this.literal
2589
2617
  const length: i32[] = this.length
@@ -2609,7 +2637,7 @@ export class FixedDeflater {
2609
2637
  bits += 1
2610
2638
  }
2611
2639
  head.fill(0, 0, 1 << bits)
2612
- const shift: u32 = toU32(32 - bits)
2640
+ const shift: i32 = 32 - bits
2613
2641
  // `deflateWindow` has checked both windows, so neither end is -1.
2614
2642
  const end: i32 = deflateWindowEnd(src, off, len)
2615
2643
  const outEnd: i32 = deflateWindowEnd(out, at, room)
@@ -2640,7 +2668,7 @@ export class FixedDeflater {
2640
2668
  if (pc <= last && pc <= 2147483639) {
2641
2669
  const pi: integer<0, 2147483639> = pc
2642
2670
  const w: u64 = deflateLoad64(src, pi)
2643
- const h: i32 = toI32((toU32(w) * toU32(DEFLATE_HASH_MUL)) >>> shift)
2671
+ const h: i32 = deflateHash(toU32(w), shift)
2644
2672
  // The table holds 0 or `q - off + 1` for an earlier position `q`, so
2645
2673
  // the candidate `pc - dist` lies in [off - 1, pc).
2646
2674
  const seen: i32 = uncheckedGet(head, h)
@@ -2714,7 +2742,7 @@ export class FixedDeflater {
2714
2742
  const q: i32 = next - 2
2715
2743
  if (q >= off && q <= last && q <= 2147483639) {
2716
2744
  const qi: integer<0, 2147483639> = q
2717
- const hq: i32 = toI32((deflateLoad32(src, qi) * toU32(DEFLATE_HASH_MUL)) >>> shift)
2745
+ const hq: i32 = deflateHash(deflateLoad32(src, qi), shift)
2718
2746
  uncheckedSet(head, hq, q - off + 1)
2719
2747
  }
2720
2748
  p = next
@@ -3657,8 +3685,9 @@ export class Inflater {
3657
3685
  * the last call stopped: the resumable step under `decompress`, for a
3658
3686
  * stream that arrives in pieces or an output buffer that is drained
3659
3687
  * between calls. `GzipDecompressor`, `GzipReader` and `ZlibDecompressor`
3660
- * are built on it. `inPos` and `outPos` say where it stopped. Windows
3661
- * outside their arrays panic. Allocates nothing.
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.
3662
3691
  *
3663
3692
  * @param src The array that holds the compressed input.
3664
3693
  * @param ip Where in `src` this call's input starts; the bytes before it
@@ -3676,7 +3705,8 @@ export class Inflater {
3676
3705
  * @returns `INFLATE_DONE` once the final block has ended, with `inPos` the
3677
3706
  * first byte after it; `INFLATE_NEED_INPUT` when the input ran out with
3678
3707
  * `more`; `INFLATE_NEED_OUTPUT` when the next symbol does not fit before
3679
- * `outEnd`; or a negative `DEFLATE_ERR_*` for a malformed stream.
3708
+ * `outEnd`; `DEFLATE_ERR_WINDOW` for a window outside its array; or
3709
+ * another negative `DEFLATE_ERR_*` for a malformed stream.
3680
3710
  */
3681
3711
  inflate(
3682
3712
  src: u8[],
@@ -3687,12 +3717,51 @@ export class Inflater {
3687
3717
  op: i32,
3688
3718
  outEnd: i32,
3689
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
3690
3759
  ): i32 {
3691
3760
  if (ip < 0 || ip > inEnd || inEnd > toI32(src.length)) {
3692
- panic("Inflater.inflate: an input window outside its array")
3761
+ return DEFLATE_ERR_WINDOW
3693
3762
  }
3694
3763
  if (histStart < 0 || histStart > op || op > outEnd || outEnd > toI32(out.length)) {
3695
- panic("Inflater.inflate: an output window outside its array")
3764
+ return DEFLATE_ERR_WINDOW
3696
3765
  }
3697
3766
  this.inPos = ip
3698
3767
  this.outPos = op
@@ -3787,46 +3856,9 @@ export class Inflater {
3787
3856
  return DEFLATE_ERR_BLOCK_TYPE
3788
3857
  }
3789
3858
  } else if (s === INF_S_STORED) {
3790
- const ip: i32 = this.inPos
3791
- const opos: i32 = this.outPos
3792
- // `inflate` checked `inPos <= inEnd` and `outPos <= outEnd`, and
3793
- // every step keeps both.
3794
- if (ip < 0 || ip > inEnd || opos < 0 || opos > outEnd) {
3795
- return DEFLATE_ERR_TRUNCATED
3796
- }
3797
- const inAvail: i32 = inEnd - ip
3798
- const outAvail: i32 = outEnd - opos
3799
- if (inAvail < 0 || outAvail < 0 || inEnd < 0 || outEnd < 0) {
3800
- return DEFLATE_ERR_TRUNCATED
3801
- }
3802
- // A stored length is 16 bits.
3803
- const left: i32 = this.storedLeft
3804
- if (left < 0 || left > 65535) {
3805
- return DEFLATE_ERR_STORED_LENGTH
3806
- }
3807
- let n: i32 = left
3808
- if (n > inAvail) {
3809
- n = inAvail
3810
- }
3811
- if (n > outAvail) {
3812
- n = outAvail
3813
- }
3814
- // `n` is at most each room, and each room at least zero: `ip + n`
3815
- // and `opos + n` are the ends less what is left of each room.
3816
- const inGap: i32 = inAvail - n
3817
- const outGap: i32 = outAvail - n
3818
- if (inGap < 0 || outGap < 0) {
3819
- return DEFLATE_ERR_TRUNCATED
3820
- }
3821
- copyBytes(out, opos, src, ip, n)
3822
- this.inPos = inEnd - inGap
3823
- this.outPos = outEnd - outGap
3824
- this.storedLeft = left - n
3825
- if (this.storedLeft > 0) {
3826
- if (this.outPos === outEnd) {
3827
- return INFLATE_NEED_OUTPUT
3828
- }
3829
- 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
3830
3862
  }
3831
3863
  this.state = INF_S_BLOCK
3832
3864
  } else {
@@ -3838,6 +3870,61 @@ export class Inflater {
3838
3870
  }
3839
3871
  }
3840
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
+
3841
3928
  /**
3842
3929
  * The Huffman block loop: decodes symbols until the end of the block
3843
3930
  * (answers 0, the state back at a block header), or until the input or
@@ -4248,9 +4335,9 @@ export class Inflater {
4248
4335
  * from `at`, writing at most `limit` bytes and never past `out`'s end, and
4249
4336
  * answers where the output ends; or `DEFLATE_ERR_OUTPUT_LIMIT` when the
4250
4337
  * stream holds more, `DEFLATE_ERR_TRAILING` when bytes follow its final
4251
- * block, or another negative `DEFLATE_ERR_*` for a malformed stream. A
4252
- * window outside its array, or a negative `limit`, panics. Allocates
4253
- * 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.
4254
4341
  *
4255
4342
  * @param src The array that holds the raw DEFLATE stream.
4256
4343
  * @param off Where in `src` the stream starts.
@@ -4266,40 +4353,37 @@ export class Inflater {
4266
4353
  * nothing. Either way `consumed` says how far into `src` it read.
4267
4354
  */
4268
4355
  decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
4269
- deflateWindow("Inflater.decompress", src, off, len)
4270
- deflateWindow("Inflater.decompress", out, at, 0)
4271
- if (limit < 0) {
4272
- panic("Inflater.decompress: a negative limit")
4273
- }
4274
- // `deflateWindow` checked `off + len` against `src` and `at` against
4275
- // `out`, so the stream ends `src.length` less the bytes after it, and the
4276
- // output `out.length` less the room it leaves; the input is read from
4277
- // `off` on.
4278
- 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) {
4279
4361
  this.consumed = 0
4280
- return DEFLATE_ERR_TRUNCATED
4362
+ return DEFLATE_ERR_WINDOW
4281
4363
  }
4282
4364
  const after: i32 = toI32(src.length) - off
4283
4365
  const room: i32 = toI32(out.length) - at
4284
4366
  if (after < 0 || room < 0) {
4285
4367
  this.consumed = 0
4286
- return DEFLATE_ERR_TRUNCATED
4368
+ return DEFLATE_ERR_WINDOW
4287
4369
  }
4288
4370
  const inGap: i32 = after - len
4289
4371
  const take: i32 = limit < room ? limit : room
4290
4372
  if (inGap < 0 || take < 0) {
4291
4373
  this.consumed = 0
4292
- return DEFLATE_ERR_TRUNCATED
4374
+ return DEFLATE_ERR_WINDOW
4293
4375
  }
4294
4376
  const outGap: i32 = room - take
4295
4377
  if (outGap < 0) {
4296
4378
  this.consumed = 0
4297
- return DEFLATE_ERR_TRUNCATED
4379
+ return DEFLATE_ERR_WINDOW
4298
4380
  }
4299
4381
  const inEnd: i32 = toI32(src.length) - inGap
4300
4382
  const end: i32 = toI32(out.length) - outGap
4301
4383
  this.reset()
4302
- const r: i32 = this.inflate(src, off, inEnd, false, out, at, end, at)
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)
4303
4387
  const pos: i32 = this.inPos
4304
4388
  if (pos < 0) {
4305
4389
  this.consumed = 0
@@ -4319,6 +4403,9 @@ export class Inflater {
4319
4403
  }
4320
4404
  }
4321
4405
 
4406
+ /** `deflateCompress`: a level outside 0 to 9. */
4407
+ export const DEFLATE_ERR_LEVEL: i32 = -17
4408
+
4322
4409
  /**
4323
4410
  * `Deflater.compress` at `level`, building the compressor for this call
4324
4411
  * alone: 330 KB allocated and filled each time, so keep a `Deflater` for
@@ -4329,12 +4416,23 @@ export class Inflater {
4329
4416
  * @param len How many bytes of data to compress.
4330
4417
  * @param out The caller's array the stream is written into.
4331
4418
  * @param at Where in `out` the stream starts.
4332
- * @param level How hard to compress, 0 to 9; another level panics.
4419
+ * @param level How hard to compress, 0 to 9; another level answers
4420
+ * `DEFLATE_ERR_LEVEL`.
4333
4421
  * @returns The index in `out` one past the stream's last byte, or
4334
- * `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.
4335
4425
  */
4336
- export const deflateCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 =>
4337
- new Deflater(level).compress(src, off, len, out, at)
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
+ }
4338
4436
 
4339
4437
  /**
4340
4438
  * `Inflater.decompress`, building the decompressor for this call alone.