@amritk/nish-aarch64-linux 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/INSTALL.md +3 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +18 -16
  4. package/package.json +1 -1
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -61,6 +61,16 @@ export const GZIP_ERR_CRC: i32 = -10
61
61
  /** The trailer's length is not the data's length mod 2^32. */
62
62
  export const GZIP_ERR_SIZE: i32 = -11
63
63
 
64
+ /**
65
+ * `GzipWriter`: the body is finished, so `write`, `flush` and `finish` take
66
+ * nothing more until `start` begins another. Answered after `finish` has
67
+ * returned the trailer, and from a call whose compression overran the room
68
+ * `bound` promised, which leaves the body cut short: its output so far is
69
+ * not a gzip member. Codes from -16 to -19 are left to
70
+ * `nish/compress/deflate`.
71
+ */
72
+ export const GZIP_ERR_FINISHED: i32 = -20
73
+
64
74
  /** `GzipReader`: no more output yet; give it input. */
65
75
  export const GZIP_NEED_INPUT: i32 = 0
66
76
 
@@ -75,9 +85,8 @@ const GZIP_TRAILER_SIZE: i32 = 8
75
85
  const GZIP_WRITER_HISTORY: i32 = 32768
76
86
  const GZIP_WRITER_INPUT: i32 = 65536
77
87
 
78
- /** The bytes of decoded output a `GzipReader` keeps: the 32 KB window and 32 KB to hand out. */
88
+ /** The 32 KB window of decoded output a `GzipReader` keeps behind what it hands out. */
79
89
  const GZIP_READER_WINDOW: i32 = 32768
80
- const GZIP_READER_OUTPUT: i32 = 65536
81
90
 
82
91
  /** The bytes a `GzipReader` buffers of input. */
83
92
  const GZIP_READER_INPUT: i32 = 16384
@@ -89,25 +98,45 @@ const gzipWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
89
98
  }
90
99
  }
91
100
 
92
- /** Writes `v` little-endian into `out[at .. at + 4)`; the caller has checked the room. */
93
- const gzipPut32 = (out: u8[], at: i32, v: u32): void => {
94
- out[at] = toU8(v & 255)
95
- out[at + 1] = toU8((v >>> 8) & 255)
96
- out[at + 2] = toU8((v >>> 16) & 255)
97
- out[at + 3] = toU8(v >>> 24)
101
+ /** The byte at `b[i]`, or -1 when `i` is outside `b`. */
102
+ const gzipByte = (b: u8[], i: i32): i32 => (i >= 0 && i < toI32(b.length) ? toI32(b[i]) : -1)
103
+
104
+ /**
105
+ * Writes `v` little-endian into `out[at .. at + 4)` and answers whether
106
+ * those four bytes lie inside `out`; when they do not, it may have written
107
+ * the first of them.
108
+ */
109
+ const gzipPut32 = (out: u8[], at: i32, v: u32): boolean => {
110
+ const n: i32 = toI32(out.length)
111
+ for (let k: i32 = 0; k < 4; k += 1) {
112
+ const i: i32 = toI32(toI64(at) + toI64(k))
113
+ if (i < 0 || i >= n) {
114
+ return false
115
+ }
116
+ out[i] = toU8((v >>> toU32(k * 8)) & 255)
117
+ }
118
+ return true
119
+ }
120
+
121
+ /** The little-endian word at `b[at .. at + 4)`, or -1 when those bytes are not all inside `b`. */
122
+ const gzipGet32 = (b: u8[], at: i32): i64 => {
123
+ let v: i64 = 0
124
+ for (let k: i32 = 0; k < 4; k += 1) {
125
+ const c: i32 = gzipByte(b, toI32(toI64(at) + toI64(k)))
126
+ if (c < 0) {
127
+ return -1
128
+ }
129
+ v = v | (toI64(c & 255) << (toI64(k) * 8))
130
+ }
131
+ return v
98
132
  }
99
133
 
100
- /** The little-endian word at `b[at .. at + 4)`; the caller has checked the range. */
101
- const gzipGet32 = (b: u8[], at: i32): u32 =>
102
- toU32(b[at]) | (toU32(b[at + 1]) << 8) | (toU32(b[at + 2]) << 16) | (toU32(b[at + 3]) << 24)
103
-
104
- /** Writes the ten-byte header for `level` at `out[at]`; the caller has checked the room. */
105
- const gzipWriteHeader = (out: u8[], at: i32, level: i32): void => {
106
- out[at] = 0x1f
107
- out[at + 1] = 0x8b
108
- out[at + 2] = 8
109
- out[at + 3] = 0
110
- gzipPut32(out, at + 4, toU32(0))
134
+ /**
135
+ * Writes the ten-byte header for `level` at `out[at]` and answers the
136
+ * position after it, or `DEFLATE_NO_ROOM` when the ten bytes do not lie
137
+ * inside `out`.
138
+ */
139
+ const gzipWriteHeader = (out: u8[], at: i32, level: i32): i32 => {
111
140
  // XFL (§2.3.1): 2 for the slowest level, 4 for the fastest.
112
141
  let xfl: i32 = 0
113
142
  if (level === 1) {
@@ -115,8 +144,28 @@ const gzipWriteHeader = (out: u8[], at: i32, level: i32): void => {
115
144
  } else if (level === 9) {
116
145
  xfl = 2
117
146
  }
118
- out[at + 8] = toU8(xfl)
119
- out[at + 9] = 255
147
+ // ID1, ID2, CM (8, DEFLATE), FLG 0, MTIME 0, XFL, OS 255.
148
+ const n: i32 = toI32(out.length)
149
+ for (let k: i32 = 0; k < GZIP_HEADER_SIZE; k += 1) {
150
+ const i: i32 = toI32(toI64(at) + toI64(k))
151
+ if (i < 0 || i >= n) {
152
+ return DEFLATE_NO_ROOM
153
+ }
154
+ let b: i32 = 0
155
+ if (k === 0) {
156
+ b = 0x1f
157
+ } else if (k === 1) {
158
+ b = 0x8b
159
+ } else if (k === 2) {
160
+ b = 8
161
+ } else if (k === 8) {
162
+ b = xfl
163
+ } else if (k === 9) {
164
+ b = 255
165
+ }
166
+ out[i] = toU8(b)
167
+ }
168
+ return toI32(toI64(at) + toI64(GZIP_HEADER_SIZE))
120
169
  }
121
170
 
122
171
  /**
@@ -125,51 +174,82 @@ const gzipWriteHeader = (out: u8[], at: i32, level: i32): void => {
125
174
  * or `GZIP_ERR_HEADER`.
126
175
  */
127
176
  const gzipReadHeader = (crc: Crc32, src: u8[], p: i32, end: i32): i32 => {
128
- if (end - p < GZIP_HEADER_SIZE) {
129
- return end - p >= 2 && (src[p] !== 0x1f || src[p + 1] !== 0x8b) ? GZIP_ERR_HEADER : DEFLATE_ERR_TRUNCATED
177
+ // Positions are added in i64 and every byte is read through `gzipByte`:
178
+ // the callers' windows lie inside `src`, but the prover does not carry
179
+ // that into this function. Each step is checked against `end` before it
180
+ // is taken, so no sum leaves the i32 range.
181
+ const b0: i32 = gzipByte(src, p)
182
+ const b1: i32 = gzipByte(src, toI32(toI64(p) + 1))
183
+ if (toI64(end) - toI64(p) < toI64(GZIP_HEADER_SIZE)) {
184
+ return toI64(end) - toI64(p) >= 2 && b0 >= 0 && b1 >= 0 && (b0 !== 0x1f || b1 !== 0x8b)
185
+ ? GZIP_ERR_HEADER
186
+ : DEFLATE_ERR_TRUNCATED
187
+ }
188
+ const b2: i32 = gzipByte(src, toI32(toI64(p) + 2))
189
+ const flags: i32 = gzipByte(src, toI32(toI64(p) + 3))
190
+ if (b0 < 0 || b1 < 0 || b2 < 0 || flags < 0) {
191
+ return DEFLATE_ERR_TRUNCATED
130
192
  }
131
- if (src[p] !== 0x1f || src[p + 1] !== 0x8b || src[p + 2] !== 8) {
193
+ if (b0 !== 0x1f || b1 !== 0x8b || b2 !== 8) {
132
194
  return GZIP_ERR_HEADER
133
195
  }
134
- const flags: i32 = toI32(src[p + 3])
135
196
  if ((flags & 0xe0) !== 0) {
136
197
  return GZIP_ERR_HEADER
137
198
  }
138
- let q: i32 = p + GZIP_HEADER_SIZE
199
+ let q: i32 = toI32(toI64(p) + toI64(GZIP_HEADER_SIZE))
139
200
  if ((flags & 4) !== 0) {
140
- if (end - q < 2) {
201
+ const x0: i32 = gzipByte(src, q)
202
+ const x1: i32 = gzipByte(src, toI32(toI64(q) + 1))
203
+ if (toI64(end) - toI64(q) < 2 || x0 < 0 || x1 < 0) {
141
204
  return DEFLATE_ERR_TRUNCATED
142
205
  }
143
- const xlen: i32 = toI32(src[q]) | (toI32(src[q + 1]) << 8)
144
- if (end - q - 2 < xlen) {
206
+ const xlen: i32 = (x0 & 255) | ((x1 & 255) << 8)
207
+ if (toI64(end) - toI64(q) - 2 < toI64(xlen)) {
145
208
  return DEFLATE_ERR_TRUNCATED
146
209
  }
147
- q += 2 + xlen
210
+ q = toI32(toI64(q) + toI64(xlen) + 2)
148
211
  }
149
212
  for (let field: i32 = 8; field <= 16; field += 8) {
150
213
  if ((flags & field) !== 0) {
151
- while (q >= 0 && q < end && q < toI32(src.length) && src[q] !== 0) {
152
- q += 1
214
+ // FNAME, then FCOMMENT: zero-terminated.
215
+ let c: i32 = gzipByte(src, q)
216
+ while (q < end && c > 0) {
217
+ q = toI32(toI64(q) + 1)
218
+ c = gzipByte(src, q)
153
219
  }
154
- if (q >= end) {
220
+ if (q >= end || c < 0) {
155
221
  return DEFLATE_ERR_TRUNCATED
156
222
  }
157
- q += 1
223
+ q = toI32(toI64(q) + 1)
158
224
  }
159
225
  }
160
226
  if ((flags & 2) !== 0) {
161
- if (end - q < 2) {
227
+ const h0: i32 = gzipByte(src, q)
228
+ const h1: i32 = gzipByte(src, toI32(toI64(q) + 1))
229
+ if (toI64(end) - toI64(q) < 2 || h0 < 0 || h1 < 0) {
162
230
  return DEFLATE_ERR_TRUNCATED
163
231
  }
164
- const want: i32 = toI32(src[q]) | (toI32(src[q + 1]) << 8)
165
- if (toI32(crc.update(toU32(0), src, p, q - p) & 0xffff) !== want) {
232
+ const want: i32 = (h0 & 255) | ((h1 & 255) << 8)
233
+ if (toI32(crc.update(toU32(0), src, p, toI32(toI64(q) - toI64(p))) & 0xffff) !== want) {
166
234
  return GZIP_ERR_HEADER
167
235
  }
168
- q += 2
236
+ q = toI32(toI64(q) + 2)
169
237
  }
170
238
  return q
171
239
  }
172
240
 
241
+ /**
242
+ * Writes the trailer, `crc` then `size`, at `out[at]` and answers the
243
+ * position after it, or `DEFLATE_NO_ROOM` when its eight bytes do not lie
244
+ * inside `out`.
245
+ */
246
+ const gzipTrailer = (out: u8[], at: i32, crc: u32, size: u32): i32 => {
247
+ if (!gzipPut32(out, at, crc) || !gzipPut32(out, toI32(toI64(at) + 4), size)) {
248
+ return DEFLATE_NO_ROOM
249
+ }
250
+ return toI32(toI64(at) + toI64(GZIP_TRAILER_SIZE))
251
+ }
252
+
173
253
  /**
174
254
  * A gzip compressor at one level: a `Deflater` and the CRC-32 tables,
175
255
  * allocated once and reused by every call. Not shared between threads.
@@ -208,17 +288,17 @@ export class GzipCompressor {
208
288
  compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
209
289
  gzipWindow("GzipCompressor.compress", src, off, len)
210
290
  gzipWindow("GzipCompressor.compress", out, at, 0)
211
- if (toI32(out.length) - at < GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE) {
291
+ if (toI64(toI32(out.length)) - toI64(at) < toI64(GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE)) {
212
292
  return DEFLATE_NO_ROOM
213
293
  }
214
- gzipWriteHeader(out, at, this.deflater.level)
215
- const end: i32 = this.deflater.compress(src, off, len, out, at + GZIP_HEADER_SIZE)
216
- if (end < 0 || toI32(out.length) - end < GZIP_TRAILER_SIZE) {
294
+ const body: i32 = gzipWriteHeader(out, at, this.deflater.level)
295
+ if (body < 0) {
217
296
  return DEFLATE_NO_ROOM
218
297
  }
219
- gzipPut32(out, end, this.crc.update(toU32(0), src, off, len))
220
- gzipPut32(out, end + 4, toU32(len))
221
- return end + GZIP_TRAILER_SIZE
298
+ const end: i32 = this.deflater.compress(src, off, len, out, body)
299
+ return end < 0
300
+ ? DEFLATE_NO_ROOM
301
+ : gzipTrailer(out, end, this.crc.update(toU32(0), src, off, len), toU32(len))
222
302
  }
223
303
  }
224
304
 
@@ -228,9 +308,17 @@ export class GzipCompressor {
228
308
  * @param n The length of the input, in bytes; a negative `n` answers the
229
309
  * bound of 0.
230
310
  * @returns A size in bytes, not a position: a `room` of this many always
231
- * holds the member.
311
+ * holds the member. Or `DEFLATE_NO_ROOM` when `n` is over 1,908,874,323,
312
+ * where the size is more than 2^31 - 1 and no array holds it.
232
313
  */
233
- export const gzipFixedBound = (n: i32): i32 => fixedDeflateBound(n) + GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE
314
+ export const gzipFixedBound = (n: i32): i32 => {
315
+ // Past this `n`, `fixedDeflateBound(n) + 18` (n + n / 8 + 34) is more
316
+ // than 2^31 - 1.
317
+ if (n > 1908874323) {
318
+ return DEFLATE_NO_ROOM
319
+ }
320
+ return toI32(toI64(fixedDeflateBound(n)) + toI64(GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE))
321
+ }
234
322
 
235
323
  /**
236
324
  * A gzip compressor for short inputs: a `FixedDeflater` (one fixed-Huffman
@@ -273,21 +361,21 @@ export class GzipFixedCompressor {
273
361
  if (room < GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE) {
274
362
  return DEFLATE_NO_ROOM
275
363
  }
276
- gzipWriteHeader(out, at, 1)
364
+ const body: i32 = gzipWriteHeader(out, at, 1)
365
+ if (body < 0) {
366
+ return DEFLATE_NO_ROOM
367
+ }
277
368
  const end: i32 = this.deflater.compress(
278
369
  src,
279
370
  off,
280
371
  len,
281
372
  out,
282
- at + GZIP_HEADER_SIZE,
283
- room - GZIP_HEADER_SIZE - GZIP_TRAILER_SIZE
373
+ body,
374
+ toI32(toI64(room) - toI64(GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE))
284
375
  )
285
- if (end < 0) {
286
- return DEFLATE_NO_ROOM
287
- }
288
- gzipPut32(out, end, this.crc.update(toU32(0), src, off, len))
289
- gzipPut32(out, end + 4, toU32(len))
290
- return end + GZIP_TRAILER_SIZE
376
+ return end < 0
377
+ ? DEFLATE_NO_ROOM
378
+ : gzipTrailer(out, end, this.crc.update(toU32(0), src, off, len), toU32(len))
291
379
  }
292
380
  }
293
381
 
@@ -338,9 +426,13 @@ export class GzipDecompressor {
338
426
  if (limit < 0) {
339
427
  panic("GzipDecompressor.decompress: a negative limit")
340
428
  }
341
- const room: i32 = toI32(out.length) - at
342
- const outEnd: i32 = at + (limit < room ? limit : room)
343
- const end: i32 = off + len
429
+ // Positions are added in i64: the windows were checked in `gzipWindow`,
430
+ // but the prover does not carry that across the call.
431
+ let outEnd: i32 = toI32(out.length)
432
+ if (toI64(limit) < toI64(outEnd) - toI64(at)) {
433
+ outEnd = toI32(toI64(at) + toI64(limit))
434
+ }
435
+ const end: i32 = toI32(toI64(off) + toI64(len))
344
436
  let p: i32 = off
345
437
  let op: i32 = at
346
438
  let first: boolean = true
@@ -360,18 +452,20 @@ export class GzipDecompressor {
360
452
  return r
361
453
  }
362
454
  const q: i32 = inf.inPos
363
- if (end - q < GZIP_TRAILER_SIZE) {
455
+ const wantCrc: i64 = gzipGet32(src, q)
456
+ const wantSize: i64 = gzipGet32(src, toI32(toI64(q) + 4))
457
+ if (toI64(end) - toI64(q) < toI64(GZIP_TRAILER_SIZE) || wantCrc < 0 || wantSize < 0) {
364
458
  return DEFLATE_ERR_TRUNCATED
365
459
  }
366
- const n: i32 = inf.outPos - op
367
- if (gzipGet32(src, q) !== this.crc.update(toU32(0), out, op, n)) {
460
+ const n: i32 = toI32(toI64(inf.outPos) - toI64(op))
461
+ if (wantCrc !== toI64(this.crc.update(toU32(0), out, op, n))) {
368
462
  return GZIP_ERR_CRC
369
463
  }
370
- if (gzipGet32(src, q + 4) !== toU32(n)) {
464
+ if (wantSize !== toI64(toU32(n))) {
371
465
  return GZIP_ERR_SIZE
372
466
  }
373
467
  op = inf.outPos
374
- p = q + GZIP_TRAILER_SIZE
468
+ p = toI32(toI64(q) + toI64(GZIP_TRAILER_SIZE))
375
469
  }
376
470
  return op
377
471
  }
@@ -417,8 +511,10 @@ export const gzipDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32
417
511
  * compressing whenever that fills, so output arrives in bursts. Every call
418
512
  * writes into a caller window and answers where its output ends, or
419
513
  * `DEFLATE_NO_ROOM` when the window is shorter than `bound(len)`, the most
420
- * that call can produce; nothing is consumed then. About 430 KB, allocated
421
- * once; `start` begins the next body.
514
+ * that call can produce; nothing is consumed then. After `finish`, and
515
+ * after a call whose output overran `bound`, every call answers
516
+ * `GZIP_ERR_FINISHED` until `start`. About 430 KB, allocated once; `start`
517
+ * begins the next body.
422
518
  */
423
519
  export class GzipWriter {
424
520
  private readonly deflater: Deflater
@@ -445,7 +541,8 @@ export class GzipWriter {
445
541
  constructor(level: i32) {
446
542
  this.deflater = new Deflater(level)
447
543
  this.crc = new Crc32()
448
- this.window = new Array<u8>(GZIP_WRITER_HISTORY + GZIP_WRITER_INPUT)
544
+ // GZIP_WRITER_HISTORY + GZIP_WRITER_INPUT, as a literal the prover reads.
545
+ this.window = new Array<u8>(98304)
449
546
  this.pending = 0
450
547
  this.fill = 0
451
548
  this.checksum = 0
@@ -480,21 +577,47 @@ export class GzipWriter {
480
577
  * @param len The length of the next `write`, in bytes; 0 for a `flush` or
481
578
  * `finish`.
482
579
  * @returns A size in bytes, not a position: the room `out` must have from
483
- * `at` for that call not to answer `DEFLATE_NO_ROOM`.
580
+ * `at` for that call not to answer `DEFLATE_NO_ROOM`. Or
581
+ * `DEFLATE_NO_ROOM` itself when `len` is over 2,145,000,000, where the
582
+ * size comes near 2^31 - 1; a `write` of such a piece answers
583
+ * `DEFLATE_NO_ROOM` whatever `out` is.
484
584
  */
485
585
  bound(len: i32): i32 {
486
- const drains: i32 = (len >> 16) + 1
487
- return deflateBound(GZIP_WRITER_INPUT + len) + drains * 18 + GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE + 10
586
+ // Up to this `len`, `deflateBound(65536 + len)` and the sum below stay
587
+ // under 2^31 - 1: 2,146,473,015 at the limit.
588
+ if (len > 2145000000) {
589
+ return DEFLATE_NO_ROOM
590
+ }
591
+ // One deflateBound constant (18) per 64 KB the call compresses, then
592
+ // the header, the trailer and 10 bytes of slack.
593
+ const n: i32 = toI32(toI64(len) + toI64(GZIP_WRITER_INPUT))
594
+ return toI32(
595
+ toI64(deflateBound(n)) + toI64((len >> 16) + 1) * 18 + toI64(GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE + 10)
596
+ )
597
+ }
598
+
599
+ /** Whether `out` has `bound(len)` bytes from `at`. */
600
+ private hasRoom(out: u8[], at: i32, len: i32): boolean {
601
+ const b: i32 = this.bound(len)
602
+ return b >= 0 && toI64(toI32(out.length)) - toI64(at) >= toI64(b)
603
+ }
604
+
605
+ /** Ends the body after an overrun of `bound`; answers `GZIP_ERR_FINISHED`. */
606
+ private fail(): i32 {
607
+ this.finished = true
608
+ return GZIP_ERR_FINISHED
488
609
  }
489
610
 
490
- /** Writes the header into `out` at `at` if it has not been; answers the new position. */
611
+ /** Writes the header into `out` at `at` if it has not been; answers the new position, or -1. */
491
612
  private header(out: u8[], at: i32): i32 {
492
613
  if (this.started) {
493
614
  return at
494
615
  }
495
- gzipWriteHeader(out, at, this.deflater.level)
496
- this.started = true
497
- return at + GZIP_HEADER_SIZE
616
+ const o: i32 = gzipWriteHeader(out, at, this.deflater.level)
617
+ if (o >= 0) {
618
+ this.started = true
619
+ }
620
+ return o
498
621
  }
499
622
 
500
623
  /**
@@ -519,7 +642,7 @@ export class GzipWriter {
519
642
  }
520
643
  this.pending = this.fill
521
644
  if (this.fill > GZIP_WRITER_HISTORY) {
522
- const shift: i32 = this.fill - GZIP_WRITER_HISTORY
645
+ const shift: i32 = toI32(toI64(this.fill) - toI64(GZIP_WRITER_HISTORY))
523
646
  copyBytes(this.window, 0, this.window, shift, GZIP_WRITER_HISTORY)
524
647
  this.fill = GZIP_WRITER_HISTORY
525
648
  this.pending = GZIP_WRITER_HISTORY
@@ -532,8 +655,8 @@ export class GzipWriter {
532
655
  * Takes `src[off .. off + len)` and answers where the output it produced
533
656
  * in `out` from `at` ends (often `at`: input waits until 64 KB are
534
657
  * pending), or `DEFLATE_NO_ROOM` when `out` has less than `bound(len)`
535
- * bytes from `at`. Writing after `finish` panics, as does a window outside
536
- * its array.
658
+ * bytes from `at`. Writing after `finish` answers `GZIP_ERR_FINISHED`; a
659
+ * window outside its array panics.
537
660
  *
538
661
  * @param src The array that holds the next piece of the body; its bytes
539
662
  * are copied, so the caller may reuse it once `write` returns.
@@ -543,36 +666,44 @@ export class GzipWriter {
543
666
  * @param at Where in `out` the output starts.
544
667
  * @returns The index in `out` one past the last byte written, so the
545
668
  * caller sends `out[at .. result)`, which may be empty; or
546
- * `DEFLATE_NO_ROOM`, when nothing was taken and nothing written.
669
+ * `DEFLATE_NO_ROOM`, when nothing was taken and nothing written; or
670
+ * `GZIP_ERR_FINISHED`, after `finish` (nothing taken or written) or
671
+ * when the output overran `bound(len)`, after which the bytes of `out`
672
+ * from `at` mean nothing and the body cannot be completed.
547
673
  */
548
674
  write(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
549
675
  gzipWindow("GzipWriter.write", src, off, len)
550
676
  gzipWindow("GzipWriter.write", out, at, 0)
551
677
  if (this.finished) {
552
- panic("GzipWriter.write: the body has been finished")
678
+ return GZIP_ERR_FINISHED
553
679
  }
554
- if (toI32(out.length) - at < this.bound(len)) {
680
+ if (!this.hasRoom(out, at, len)) {
555
681
  return DEFLATE_NO_ROOM
556
682
  }
557
683
  let o: i32 = this.header(out, at)
684
+ if (o < 0) {
685
+ return this.fail()
686
+ }
558
687
  this.checksum = this.crc.update(this.checksum, src, off, len)
559
688
  this.size = this.size + toU32(len)
560
689
  let p: i32 = off
561
- const end: i32 = off + len
690
+ const end: i32 = toI32(toI64(off) + toI64(len))
562
691
  // Compressed once 64 KB is pending, the first window too: `pending` is
563
692
  // at most `GZIP_WRITER_HISTORY` here, so the window always has the room.
693
+ // Fields are added in i64: the prover keeps no facts about them.
564
694
  while (p < end) {
565
- let take: i32 = GZIP_WRITER_INPUT - (this.fill - this.pending)
566
- if (take > end - p) {
567
- take = end - p
695
+ let take: i64 = toI64(GZIP_WRITER_INPUT) - (toI64(this.fill) - toI64(this.pending))
696
+ if (take > toI64(end) - toI64(p)) {
697
+ take = toI64(end) - toI64(p)
568
698
  }
569
- copyBytes(this.window, this.fill, src, p, take)
570
- this.fill += take
571
- p += take
572
- if (this.fill - this.pending === GZIP_WRITER_INPUT) {
699
+ const t: i32 = toI32(take)
700
+ copyBytes(this.window, this.fill, src, p, t)
701
+ this.fill = toI32(toI64(this.fill) + toI64(t))
702
+ p = toI32(toI64(p) + toI64(t))
703
+ if (toI64(this.fill) - toI64(this.pending) === toI64(GZIP_WRITER_INPUT)) {
573
704
  o = this.drain(out, o, false)
574
705
  if (o < 0) {
575
- panic("GzipWriter.write: a block larger than its bound")
706
+ return this.fail()
576
707
  }
577
708
  }
578
709
  }
@@ -583,67 +714,69 @@ export class GzipWriter {
583
714
  * Compresses everything pending and ends with an empty stored block, so a
584
715
  * reader can decode every byte written so far (zlib's `Z_SYNC_FLUSH`).
585
716
  * Answers the new end of `out`, or `DEFLATE_NO_ROOM` below `bound(0)`.
586
- * Flushing after `finish` panics, as does a window outside `out`.
717
+ * Flushing after `finish` answers `GZIP_ERR_FINISHED`; a window outside
718
+ * `out` panics.
587
719
  *
588
720
  * @param out The caller's array the output is written into.
589
721
  * @param at Where in `out` the output starts.
590
722
  * @returns The index in `out` one past the last byte written, so the
591
723
  * caller sends `out[at .. result)`; or `DEFLATE_NO_ROOM`, when nothing
592
- * was written.
724
+ * was written; or `GZIP_ERR_FINISHED`, after `finish` or when the
725
+ * output overran `bound(0)`, as `write` answers it.
593
726
  */
594
727
  flush(out: u8[], at: i32): i32 {
595
728
  gzipWindow("GzipWriter.flush", out, at, 0)
596
729
  if (this.finished) {
597
- panic("GzipWriter.flush: the body has been finished")
730
+ return GZIP_ERR_FINISHED
598
731
  }
599
- if (toI32(out.length) - at < this.bound(toI32(0))) {
732
+ if (!this.hasRoom(out, at, toI32(0))) {
600
733
  return DEFLATE_NO_ROOM
601
734
  }
602
735
  let o: i32 = this.header(out, at)
603
- if (this.fill > this.pending) {
736
+ if (o >= 0 && this.fill > this.pending) {
604
737
  o = this.drain(out, o, false)
605
- if (o < 0) {
606
- panic("GzipWriter.flush: a block larger than its bound")
607
- }
608
738
  }
609
- o = this.deflater.writeStored(this.window, toI32(0), toI32(0), false, out, o, toI32(out.length))
610
- return o
739
+ if (o >= 0) {
740
+ o = this.deflater.writeStored(this.window, toI32(0), toI32(0), false, out, o, toI32(out.length))
741
+ }
742
+ return o < 0 ? this.fail() : o
611
743
  }
612
744
 
613
745
  /**
614
746
  * Compresses everything pending as the final block and writes the
615
747
  * trailer; answers the new end of `out`, or `DEFLATE_NO_ROOM` below
616
748
  * `bound(0)`. `start` begins another body; until then `write`, `flush`
617
- * and `finish` panic, as does a window outside `out`.
749
+ * and `finish` answer `GZIP_ERR_FINISHED`. A window outside `out`
750
+ * panics.
618
751
  *
619
752
  * @param out The caller's array the output is written into.
620
753
  * @param at Where in `out` the output starts.
621
754
  * @returns The index in `out` one past the trailer's last byte, so the
622
755
  * caller sends `out[at .. result)` as the end of the body; or
623
756
  * `DEFLATE_NO_ROOM`, when nothing was written and the body is not yet
624
- * finished.
757
+ * finished; or `GZIP_ERR_FINISHED`, after `finish` or when the output
758
+ * overran `bound(0)`, as `write` answers it.
625
759
  */
626
760
  finish(out: u8[], at: i32): i32 {
627
761
  gzipWindow("GzipWriter.finish", out, at, 0)
628
762
  if (this.finished) {
629
- panic("GzipWriter.finish: the body has been finished")
763
+ return GZIP_ERR_FINISHED
630
764
  }
631
- if (toI32(out.length) - at < this.bound(toI32(0))) {
765
+ if (!this.hasRoom(out, at, toI32(0))) {
632
766
  return DEFLATE_NO_ROOM
633
767
  }
634
768
  let o: i32 = this.header(out, at)
635
- o = this.drain(out, o, true)
636
- if (o < 0) {
637
- panic("GzipWriter.finish: a block larger than its bound")
769
+ if (o >= 0) {
770
+ o = this.drain(out, o, true)
771
+ }
772
+ if (o >= 0) {
773
+ o = this.deflater.flushBits(out, o, toI32(out.length))
638
774
  }
639
- o = this.deflater.flushBits(out, o, toI32(out.length))
640
- if (o < 0 || toI32(out.length) - o < GZIP_TRAILER_SIZE) {
641
- panic("GzipWriter.finish: a block larger than its bound")
775
+ if (o >= 0) {
776
+ o = gzipTrailer(out, o, this.checksum, this.size)
642
777
  }
643
- gzipPut32(out, o, this.checksum)
644
- gzipPut32(out, o + 4, this.size)
645
778
  this.finished = true
646
- return o + GZIP_TRAILER_SIZE
779
+ return o < 0 ? GZIP_ERR_FINISHED : o
647
780
  }
648
781
  }
649
782
 
@@ -678,14 +811,15 @@ export class GzipReader {
678
811
  private phase: i32
679
812
  /** Whether more input may come. */
680
813
  private more: boolean
681
- /** The member's running CRC-32 and length, and the body's total length. */
814
+ /** Whether a member has been read; another may follow. */
815
+ private sawMember: boolean
816
+ /** The member's running CRC-32 and length. */
682
817
  private checksum: u32
683
818
  private size: u32
684
- private total: i64
819
+ /** How many more decoded bytes the body may have, from `limit` down. */
820
+ private left: i64
685
821
  /** The most decoded bytes `read` hands out in all. */
686
822
  private readonly limit: i64
687
- /** Whether a member has been read; another may follow. */
688
- private members: i32
689
823
 
690
824
  /**
691
825
  * A reader that refuses more than `limit` decoded bytes in all; a negative
@@ -702,7 +836,9 @@ export class GzipReader {
702
836
  this.inflater = new Inflater()
703
837
  this.crc = new Crc32()
704
838
  this.input = new Array<u8>(GZIP_READER_INPUT)
705
- this.output = new Array<u8>(GZIP_READER_WINDOW + GZIP_READER_OUTPUT)
839
+ // The window and 64 KB of output to hand out after it, as a literal the
840
+ // prover reads.
841
+ this.output = new Array<u8>(98304)
706
842
  this.limit = limit
707
843
  this.inStart = 0
708
844
  this.inEnd = 0
@@ -714,8 +850,8 @@ export class GzipReader {
714
850
  this.more = true
715
851
  this.checksum = 0
716
852
  this.size = 0
717
- this.total = 0
718
- this.members = 0
853
+ this.left = this.limit
854
+ this.sawMember = false
719
855
  }
720
856
 
721
857
  /** Begins a new body, dropping any input and output left over. */
@@ -730,8 +866,8 @@ export class GzipReader {
730
866
  this.more = true
731
867
  this.checksum = 0
732
868
  this.size = 0
733
- this.total = 0
734
- this.members = 0
869
+ this.left = this.limit
870
+ this.sawMember = false
735
871
  }
736
872
 
737
873
  /**
@@ -769,7 +905,7 @@ export class GzipReader {
769
905
  */
770
906
  inputRoom(): i32 {
771
907
  this.compact()
772
- return toI32(this.input.length) - this.inEnd
908
+ return this.inputRoomRaw()
773
909
  }
774
910
 
775
911
  /**
@@ -780,10 +916,10 @@ export class GzipReader {
780
916
  * negative `n` panics.
781
917
  */
782
918
  commit(n: i32): void {
783
- if (n < 0 || n > toI32(this.input.length) - this.inEnd) {
919
+ if (n < 0 || n > this.inputRoomRaw()) {
784
920
  panic("GzipReader.commit: more bytes than the buffer has room for")
785
921
  }
786
- this.inEnd += n
922
+ this.inEnd = toI32(toI64(this.inEnd) + toI64(n))
787
923
  }
788
924
 
789
925
  /**
@@ -802,7 +938,7 @@ export class GzipReader {
802
938
  const room: i32 = this.inputRoom()
803
939
  const take: i32 = len < room ? len : room
804
940
  copyBytes(this.input, this.inEnd, src, off, take)
805
- this.inEnd += take
941
+ this.inEnd = toI32(toI64(this.inEnd) + toI64(take))
806
942
  return take
807
943
  }
808
944
 
@@ -814,7 +950,7 @@ export class GzipReader {
814
950
  /** Moves the undecoded input to the front of its buffer. */
815
951
  private compact(): void {
816
952
  if (this.inStart > 0) {
817
- const n: i32 = this.inEnd - this.inStart
953
+ const n: i32 = toI32(toI64(this.inEnd) - toI64(this.inStart))
818
954
  copyBytes(this.input, 0, this.input, this.inStart, n)
819
955
  this.inStart = 0
820
956
  this.inEnd = n
@@ -823,7 +959,7 @@ export class GzipReader {
823
959
 
824
960
  /** Adds the decoded bytes not yet checksummed to the member's CRC-32 and length. */
825
961
  private sum(): void {
826
- const n: i32 = this.writePos - this.crcPos
962
+ const n: i32 = toI32(toI64(this.writePos) - toI64(this.crcPos))
827
963
  if (n > 0) {
828
964
  this.checksum = this.crc.update(this.checksum, this.output, this.crcPos, n)
829
965
  this.size = this.size + toU32(n)
@@ -835,23 +971,24 @@ export class GzipReader {
835
971
  private slide(): void {
836
972
  if (this.readPos === this.writePos && this.writePos > GZIP_READER_WINDOW) {
837
973
  this.sum()
974
+ // Positions are moved in i64: the prover keeps no facts about fields.
838
975
  const keep: i32 = GZIP_READER_WINDOW
839
- const shift: i32 = this.writePos - keep
976
+ const shift: i32 = toI32(toI64(this.writePos) - toI64(keep))
840
977
  copyBytes(this.output, 0, this.output, shift, keep)
841
- this.readPos -= shift
842
- this.writePos -= shift
843
- this.crcPos -= shift
844
- this.histStart = this.histStart > shift ? this.histStart - shift : 0
978
+ this.readPos = toI32(toI64(this.readPos) - toI64(shift))
979
+ this.writePos = toI32(toI64(this.writePos) - toI64(shift))
980
+ this.crcPos = toI32(toI64(this.crcPos) - toI64(shift))
981
+ this.histStart = this.histStart > shift ? toI32(toI64(this.histStart) - toI64(shift)) : 0
845
982
  }
846
983
  }
847
984
 
848
985
  /** Decodes as far as the input and the output buffer allow; answers 0 or a final code. */
849
986
  private advance(): i32 {
850
987
  while (true) {
851
- const avail: i32 = this.inEnd - this.inStart
988
+ const avail: i64 = toI64(this.inEnd) - toI64(this.inStart)
852
989
  if (this.phase === 0) {
853
990
  if (avail === 0 && !this.more) {
854
- if (this.members === 0) {
991
+ if (!this.sawMember) {
855
992
  return DEFLATE_ERR_TRUNCATED
856
993
  }
857
994
  this.phase = 3
@@ -894,12 +1031,15 @@ export class GzipReader {
894
1031
  this.histStart
895
1032
  )
896
1033
  this.inStart = inf.inPos
897
- const made: i32 = inf.outPos - this.writePos
1034
+ const made: i64 = toI64(inf.outPos) - toI64(this.writePos)
898
1035
  this.writePos = inf.outPos
899
- this.total = this.total + toI64(made)
900
- if (this.total > this.limit) {
1036
+ // `left` never goes below 0 and `inflate` never moves `outPos`
1037
+ // back; the first two tests say so to the prover.
1038
+ const left: i64 = this.left
1039
+ if (left < 0 || made < 0 || made > left) {
901
1040
  return DEFLATE_ERR_OUTPUT_LIMIT
902
1041
  }
1042
+ this.left = left - made
903
1043
  if (r === INFLATE_DONE) {
904
1044
  this.sum()
905
1045
  this.phase = 2
@@ -909,17 +1049,22 @@ export class GzipReader {
909
1049
  return r
910
1050
  }
911
1051
  } else if (this.phase === 2) {
912
- if (avail < GZIP_TRAILER_SIZE) {
1052
+ if (avail < toI64(GZIP_TRAILER_SIZE)) {
913
1053
  return this.more ? 0 : DEFLATE_ERR_TRUNCATED
914
1054
  }
915
- if (gzipGet32(this.input, this.inStart) !== this.checksum) {
1055
+ const wantCrc: i64 = gzipGet32(this.input, this.inStart)
1056
+ const wantSize: i64 = gzipGet32(this.input, toI32(toI64(this.inStart) + 4))
1057
+ if (wantCrc < 0 || wantSize < 0) {
1058
+ return DEFLATE_ERR_TRUNCATED
1059
+ }
1060
+ if (wantCrc !== toI64(this.checksum)) {
916
1061
  return GZIP_ERR_CRC
917
1062
  }
918
- if (gzipGet32(this.input, this.inStart + 4) !== this.size) {
1063
+ if (wantSize !== toI64(this.size)) {
919
1064
  return GZIP_ERR_SIZE
920
1065
  }
921
- this.inStart += GZIP_TRAILER_SIZE
922
- this.members += 1
1066
+ this.inStart = toI32(toI64(this.inStart) + toI64(GZIP_TRAILER_SIZE))
1067
+ this.sawMember = true
923
1068
  this.phase = 0
924
1069
  } else {
925
1070
  return 0
@@ -929,7 +1074,7 @@ export class GzipReader {
929
1074
 
930
1075
  /** The room `input` has without compacting. */
931
1076
  private inputRoomRaw(): i32 {
932
- return toI32(this.input.length) - this.inEnd
1077
+ return toI32(toI64(toI32(this.input.length)) - toI64(this.inEnd))
933
1078
  }
934
1079
 
935
1080
  /**
@@ -962,7 +1107,7 @@ export class GzipReader {
962
1107
  return r
963
1108
  }
964
1109
  }
965
- const n0: i32 = this.writePos - this.readPos
1110
+ const n0: i32 = toI32(toI64(this.writePos) - toI64(this.readPos))
966
1111
  if (n0 === 0) {
967
1112
  if (this.phase === 3) {
968
1113
  return GZIP_END
@@ -971,7 +1116,7 @@ export class GzipReader {
971
1116
  }
972
1117
  const n: i32 = n0 < max ? n0 : max
973
1118
  copyBytes(out, at, this.output, this.readPos, n)
974
- this.readPos += n
1119
+ this.readPos = toI32(toI64(this.readPos) + toI64(n))
975
1120
  return n
976
1121
  }
977
1122
  }