@amritk/nish-x86_64-linux 0.26.0 → 0.28.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.
@@ -40,11 +40,13 @@ import {
40
40
  DEFLATE_ERR_TRUNCATED,
41
41
  DEFLATE_NO_ROOM,
42
42
  Deflater,
43
+ FixedDeflater,
43
44
  INFLATE_DONE,
44
45
  INFLATE_NEED_INPUT,
45
46
  INFLATE_NEED_OUTPUT,
46
47
  Inflater,
47
48
  deflateBound,
49
+ fixedDeflateBound,
48
50
  } from "nish/compress/deflate"
49
51
  import { copyBytes } from "nish/bytes"
50
52
 
@@ -203,6 +205,56 @@ export class GzipCompressor {
203
205
  }
204
206
  }
205
207
 
208
+ /** The most bytes `GzipFixedCompressor.compress` writes for `n` bytes of input. */
209
+ export const gzipFixedBound = (n: i32): i32 => fixedDeflateBound(n) + GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE
210
+
211
+ /**
212
+ * A gzip compressor for short inputs: a `FixedDeflater` (one fixed-Huffman
213
+ * block, 16 KB window) and the CRC-32 tables, allocated once. Each call
214
+ * depends on its input alone. Not shared between threads.
215
+ */
216
+ export class GzipFixedCompressor {
217
+ readonly deflater: FixedDeflater
218
+ readonly crc: Crc32
219
+
220
+ /** Builds the tables. */
221
+ constructor() {
222
+ this.deflater = new FixedDeflater()
223
+ this.crc = new Crc32()
224
+ }
225
+
226
+ /**
227
+ * Compresses `src[off .. off + len)` into `out[at .. at + room)` as one gzip
228
+ * member, and answers where it ends, or `DEFLATE_NO_ROOM` (-1) as soon as
229
+ * it would need more than `room` bytes, so a caller passing the most it
230
+ * will take stops early on an input that does not shrink.
231
+ * `gzipFixedBound(len)` always fits. A window outside its array panics.
232
+ * Allocates nothing.
233
+ */
234
+ compress(src: u8[], off: i32, len: i32, out: u8[], at: i32, room: i32): i32 {
235
+ gzipWindow("GzipFixedCompressor.compress", src, off, len)
236
+ gzipWindow("GzipFixedCompressor.compress", out, at, room)
237
+ if (room < GZIP_HEADER_SIZE + GZIP_TRAILER_SIZE) {
238
+ return DEFLATE_NO_ROOM
239
+ }
240
+ gzipWriteHeader(out, at, 1)
241
+ const end: i32 = this.deflater.compress(
242
+ src,
243
+ off,
244
+ len,
245
+ out,
246
+ at + GZIP_HEADER_SIZE,
247
+ room - GZIP_HEADER_SIZE - GZIP_TRAILER_SIZE
248
+ )
249
+ if (end < 0) {
250
+ return DEFLATE_NO_ROOM
251
+ }
252
+ gzipPut32(out, end, this.crc.update(toU32(0), src, off, len))
253
+ gzipPut32(out, end + 4, toU32(len))
254
+ return end + GZIP_TRAILER_SIZE
255
+ }
256
+ }
257
+
206
258
  /**
207
259
  * A gzip decompressor: an `Inflater` and the CRC-32 tables, allocated once.
208
260
  * Not shared between threads.