ddn.compressor.unix
ddn.compressor.unix
Rock-solid implementation of the historic UNIX compress format (".Z") built around a self-contained D port of the classic ncompress 4.2 LZW codec.
The module implements the streaming Compressor / Decompressor interfaces from ddn.api.compressor and registers itself under:
---------------------
- Enum format:
CompressionFormat.unix. - String id : "unix".
- Provider : "ddn".
---------------------
Design notes: ---
- The actual LZW codec is an internal port of
ncompress42.d
(https://github.com/ZILtoid1991/lzwford) and is therefore known to be interoperable with classic UNIX compress.
- To keep the public API simple and robust, the ddn-facing
compressor and decompressor accumulate all input in memory and invoke the ncompress engine when finish() is called. This satisfies the streaming interface contract while avoiding subtle corner cases in incremental LZW decoding.
- No external D packages are used; all functionality is contained
in this module plus the standard library.
CREDITS: László Szerémi for the ncompress port to D. This module is based on his work:
https://github.com/ZILtoid1991/lzwfordModule Initializers 1
()Types 11
Byte type used internally by the ncompress port.
Reader delegate used by the ncompress codec.
The reader must fill at most numBytes bytes into the buffer pointed to by bytes and return:
> 0: number of bytes actually read,0: end of stream,-1: error (mapped to anNCompressError).
Writer delegate used by the ncompress codec.
The writer must push exactly numBytes bytes from bytes to the output sink and return the number of bytes accepted or -1 on error.
Context structure used by the ncompress codec.
Instances of this struct are passed to nInitCompress / nInitDecompress and then to nCompress / nDecompress.
NCmpStreamReader readerReader delegate supplying uncompressed or compressed data.NCmpStreamWriter writerWriter delegate receiving compressed or decompressed data.void * rwCtxtOptional opaque pointer forwarded to the reader/writer.void * privPointer to private state owned by the codec implementation.Error codes returned by the ncompress codec.
Integer types mirroring the original C implementation.
UNIX .Z compressor that implements the ddn Compressor interface using the embedded ncompress codec.
The compressor buffers all input until finish() is called, at which point the underlying codec is invoked once to generate a complete .Z stream.
private CompressionOptions _optsprivate OutputSink _sinkprivate bool _finishedprivate ubyte[] _inputprivate ulong _bytesInprivate ulong _bytesOutprivate size_t _readPosvoid setOutputSink(OutputSink sink)Set the output sink delegate that receives compressed bytes.void setProgressCallback(ProgressCallback callback)Set an optional progress callback.void write(const(ubyte)[] data)Buffer more uncompressed data for later compression.void flush(FlushMode mode = FlushMode.SYNC)Flush pending data. For `.Z` this is equivalent to `sync` and does not force a full frame; real compression happens in `finish()`.void finish()Finalise the stream by invoking the ncompress codec and emitting the resulting `.Z` data to the output sink.void reset()Reset the compressor to its initial state.bool setDictionary(const(ubyte)[] dict)UNIX `.Z` format does not support user-specified dictionaries.bool isFinished() @property constReturns true if finish() has been called and the stream is closed for further writes.this(CompressionOptions opts)Create a new UNIX `.Z` compressor.~thisDestructor ensures cleanup of the input buffer even if finish() was not called.UNIX .Z decompressor implementing the ddn Decompressor interface using the embedded ncompress codec.
Compressed data is buffered until finish() is called, where the codec is invoked once to decode the full stream.
private DecompressionOptions _optsprivate OutputSink _sinkprivate bool _finishedprivate ubyte[] _inputprivate ulong _bytesInprivate ulong _bytesOutprivate size_t _readPosvoid setOutputSink(OutputSink sink)Set the output sink delegate that receives decompressed bytes.void setProgressCallback(ProgressCallback callback)Set an optional progress callback.void write(const(ubyte)[] data)Buffer additional compressed data.void finish()Signal end-of-input and run the decoder.void reset()Reset the decompressor to its initial state.bool setDictionary(const(ubyte)[] dict)UNIX `.Z` does not use external dictionaries; this is a no-op that always returns `true`.bool isFinished() @property constReturns true if finish() has been called and the stream is closed for further writes.this(DecompressionOptions opts)Create a new UNIX `.Z` decompressor.~thisDestructor ensures cleanup of the input buffer even if finish() was not called.Functions 12
int bitsFromOptions(CompressionOptions opts)Map `CompressionOptions.numericLevel` to the UNIX `.Z` `bits` parameter used by the ncompress codec.void createPrivState(NCompressCtxt * ctxt, int bits)Allocate and initialize the private state for a context if needed.NCompressError nCompress(NCompressCtxt * ctxt)Compress the stream defined by `ctxt.reader` into `ctxt.writer`.NCompressError nDecompress(NCompressCtxt * ctxt)Decompress the stream defined by `ctxt.reader` into `ctxt.writer`.void throwForNCompressError(NCompressError err, string op)Map an `NCompressError` to a `CompressionError` exception.Compressor makeUnixCompressor(CompressionOptions opts)Factory function constructing a `UnixZCompressor`.Decompressor makeUnixDecompressor(DecompressionOptions opts)Factory function constructing a `UnixZDecompressor`.Variables 6
UNIX_Z_MIN_BITS = 9Minimum LZW code size in bits for UNIX .Z (inclusive).
UNIX_Z_MAX_BITS = 16Maximum LZW code size in bits for UNIX .Z (inclusive).
UNIX_Z_MAGIC_0 = 0x1FFirst magic byte for UNIX .Z streams.
UNIX_Z_MAGIC_1 = 0x9DSecond magic byte for UNIX .Z streams.
UNIX_Z_FLAG_BLOCK_MODE = 0x80Bit flag in the header indicating block compression mode.
int[256] primetabPrime table used by the ncompress hash function.