ddn.compressor.zlib
ddn.compressor.zlib
A pure D implementation of the zlib compression library interface. This module provides a drop-in replacement for etc.c.zlib with identical API, implementing the DEFLATE compression algorithm (RFC 1951), zlib format (RFC 1950), and GZIP format (RFC 1952) entirely in D.
Features:
- Complete DEFLATE compression and decompression
- Support for raw DEFLATE, zlib, and GZIP container formats
- Dictionary support for improved compression
- Streaming API compatible with zlib's z_stream interface
- Adler-32 and CRC-32 checksum implementations
The implementation follows the zlib API conventions for maximum compatibility with existing code that uses etc.c.zlib.
Copyright
Module Initializers 1
()Types 16
Callback type for custom memory allocation
Callback type for custom memory deallocation
The z_stream structure is used to pass information between the application and the compression/decompression library functions.
This structure mirrors the zlib z_stream_s struct for API compatibility.
ubyte * next_inInput buffer pointeruint avail_inNumber of bytes available at next_inc_ulong total_inTotal number of input bytes read so farubyte * next_outOutput buffer pointeruint avail_outRemaining free space at next_outc_ulong total_outTotal number of bytes output so farconst(char) * msgLast error message, null if no errorvoid * internalInternal state (opaque to application)alloc_func zallocCustom allocator function (not used in pure D implementation)free_func zfreeCustom deallocator function (not used in pure D implementation)void * opaquePrivate data for custom allocatorint data_typeBest guess about the data type: ZBINARY or ZTEXTc_ulong adlerAdler-32 or CRC-32 checksum valuec_ulong reservedReserved for future usePointer to z_stream
C unsigned long type alias for cross-platform compatibility
Huffman code table entry
ushort symbolubyte bitsHuffman decode table
Bit reader for input stream
const(ubyte)[] datasize_t posulong bitsint numBitsvoid setInput(const(ubyte)[] input) nothrow @safeSet new input data without resetting the bit buffer. Used for streaming decompression where leftover bits from the previous call must be preserved.size_t bytesRemaining() const nothrow @safeGet remaining bytes available (not yet loaded into bit buffer).size_t totalBytesRemaining() const nothrow @safeGet total unprocessed bytes (including those in bit buffer).void discardBits() nothrow @safeDiscard any bits in the buffer and reset to byte-aligned reading. This effectively "unreads" any partial bytes in the bit buffer.Bit writer for output stream
ubyte[] outputsize_t posuint bitsint numBitsHash chain entry for LZ77 matching
ushort posushort prevLZ77 match result
int lengthint distanceLZ77 compressor state
ubyte[] windowint windowSizeint windowMaskint windowPosint lookaheadushort[] headushort[] prevint levelint strategyint maxChainLengthint goodMatchLengthint niceMatchLengthint maxLazyMatchvoid init(int windowBits, int compLevel, int strat)Initialize LZ77 state.void setLevelParams(int compLevel)Set level-dependent compression parameters.Deflate stream state
int levelint methodint windowBitsint memLevelint strategyint statusbool wrapHeaderint wrapTrailerbool finishedLZ77State lz77ubyte[] pendingint pendingPosint pendingLenc_ulong checksumc_ulong crcubyte[] dictionaryc_ulong dictChecksumc_ulong totalInc_ulong totalOutubyte[] compressedDataubyte[] blockLiteralsushort[] blockLengthsushort[] blockDistancesint blockSizeubyte[] bitBuffersize_t bitBufferPosuint bitAccumint bitCount16384 MAX_BLOCK_SIZEInflate stream state
int windowBitsint statusbool wrapHeaderint wrapTrailerbool finishedubyte[] windowint windowSizeint windowPosBitReader bitsbool lastBlockint blockTypeHuffmanTable litTableHuffmanTable distTableubyte[320] codeLengthsint numLitCodesint numDistCodesint numCodeLenCodesc_ulong checksumc_ulong expectedChecksumc_ulong crcuint expectedCrcuint expectedSizeubyte[] dictionaryc_ulong dictChecksumbool needDictc_ulong totalInc_ulong totalOutint decodeStateint copyLengthint copyDistanceint pendingLengthint pendingLengthExtraint pendingLengthBaseint pendingDistanceExtraint pendingDistanceBaseint headerStateint gzipFlagsint gzipExtraFixed Huffman codes for encoding
ushort[288] litCodesubyte[288] litLensushort[32] distCodesubyte[32] distLensInflate decode states
Functions 31
c_ulong adler32(c_ulong adler, const(ubyte) * buf, uint len) nothrow @trustedCompute the Adler-32 checksum of a data buffer.c_ulong crc32(c_ulong crc, const(ubyte) * buf, uint len) nothrow @trustedCompute the CRC-32 checksum of a data buffer.bool buildHuffmanTable(const(ubyte)[] codeLengths, int numCodes, ref HuffmanTable table) nothrow @safeBuild a Huffman decode table from code lengths.void deflateBlockFixedToBits(DeflateState state, const(ubyte)[] input, bool last) nothrow @safeCompress a block of data using fixed Huffman codes.void deflateBlockStoredToBits(DeflateState state, const(ubyte)[] input, bool last) nothrow @safeEncode a stored block directly into the state's bit buffer.ubyte[] deflateBlockFixed(DeflateState state, const(ubyte)[] input, bool last)ubyte[] deflateBlockStored(const(ubyte)[] input, bool last)Compress data as a stored (uncompressed) block.int deflateInit2_(z_stream * strm, int level, int method, int windowBits, int memLevel, int strategy,
const(char) * ver, int streamSize) nothrowInitialize a deflate stream with full parameters.int deflateInit2(z_stream * strm, int level, int method, int windowBits, int memLevel, int strategy) nothrowSimplified deflate initialization.int deflateInit(z_stream * strm, int level) nothrowBasic deflate initialization with default parameters.int deflateSetDictionary(z_stream * strm, const(ubyte) * dictionary, uint dictLength) nothrowSet compression dictionary.int inflateInit2_(z_stream * strm, int windowBits, const(char) * ver, int streamSize) nothrowInitialize an inflate stream with full parameters.int inflateSetDictionary(z_stream * strm, const(ubyte) * dictionary, uint dictLength) nothrowSet decompression dictionary.int compress(ubyte * dest, c_ulong * destLen, const(ubyte) * source, c_ulong sourceLen) nothrowCompress source buffer to destination buffer.int compress2(ubyte * dest, c_ulong * destLen, const(ubyte) * source, c_ulong sourceLen, int level) nothrowCompress source buffer to destination buffer with specified level.int uncompress(ubyte * dest, c_ulong * destLen, const(ubyte) * source, c_ulong sourceLen) nothrowDecompress source buffer to destination buffer.Variables 44
ZLIB_VERSION = "1.3.1-ddn"zlib version string for compatibility
Z_NO_FLUSH = 0Flush modes for compression/decompression No flush, accumulate output
Z_PARTIAL_FLUSH = 1Partial flush (obsolete, use Z_SYNC_FLUSH)
Z_SYNC_FLUSH = 2Flush to byte boundary, sync point
Z_FULL_FLUSH = 3Flush and reset compression state
Z_FINISH = 4Finish the stream
Z_BLOCK = 5Stop at next block boundary
Z_TREES = 6Request deflate header/trees
Z_OK = 0Return codes Success
Z_STREAM_END = 1End of stream reached
Z_NEED_DICT = 2Dictionary required for decompression
Z_ERRNO = - 1File system error
Z_STREAM_ERROR = - 2Stream state error
Z_DATA_ERROR = - 3Data corrupted
Z_MEM_ERROR = - 4Memory allocation failed
Z_BUF_ERROR = - 5Buffer too small or no progress possible
Z_VERSION_ERROR = - 6zlib version mismatch
Z_NO_COMPRESSION = 0Compression levels Store only, no compression
Z_BEST_SPEED = 1Fastest compression
Z_BEST_COMPRESSION = 9Best compression ratio
Z_DEFAULT_COMPRESSION = - 1Default compression level (typically 6)
Z_DEFAULT_STRATEGY = 0Compression strategies Normal compression for general data
Z_FILTERED = 1Data with random distribution
Z_HUFFMAN_ONLY = 2Huffman coding only (no LZ77)
Z_RLE = 3Run-length encoding
Z_FIXED = 4Use fixed Huffman codes
Z_DEFLATED = 8Compression method The deflate compression method
MAX_WBITS = 15Window bits range Maximum window bits (32K window)
MAX_MEM_LEVEL = 9Memory level range Maximum memory level
SEEK_SET = 0Seek modes (for gzip)
SEEK_CUR = 1SEEK_END = 2uint[256] crcTableMAX_BITS = 15Maximum number of bits in a Huffman code
MAX_CODES = 286Maximum number of literal/length codes
MAX_DIST_CODES = 30Maximum number of distance codes
ubyte[19] codeLengthOrderBit lengths for the code length alphabet (RFC 1951 order)
ubyte[29] lengthExtraBitsExtra bits for length codes (257-285)
ushort[29] lengthBaseBase lengths for length codes (257-285)
ubyte[30] distanceExtraBitsExtra bits for distance codes (0-29)
ushort[30] distanceBaseBase distances for distance codes (0-29)
ubyte[288] fixedLiteralLengthsStatic Huffman code lengths for literals/lengths (fixed codes from RFC 1951)
ubyte[32] fixedDistanceLengthsStatic Huffman code lengths for distances (fixed codes from RFC 1951)
FixedCodes fixedCodesCached fixed codes