ddn.compressor.bzip2

ddn.compressor.bzip2

High-performance BZIP2 compression provider for ddn.api.compressor. This is a pure D implementation of the BZIP2 algorithm designed to match the performance of the system bzip2 tool.

The BZIP2 algorithm consists of the following stages:

  • Run-Length Encoding (RLE1): Initial pass limiting runs to 255.
  • Burrows-Wheeler Transform (BWT): Reversible block sort.
  • Move-To-Front (MTF): Exploits locality in BWT output.
  • Zero Run-Length Encoding (RLE2): Encodes zeros as RUNA/RUNB.
  • Huffman Coding: Multiple tables with selectors.

Features:

  • Supports both streaming and block compression.
  • Compression levels map to bzip2's blockSize100k (1-9).
  • Fully DDoc-documented.

Module Initializers 1

shared static this()

Types 11

structBwtResult

BWT transform result.

Fields
ubyte[] output
uint origPtr
structMtfResult

MTF transform result.

Fields
ubyte[] output
bool[256] inUse
int nInUse

RLE2 encode result.

Fields
ushort[] symbols
int nInUse

Huffman encoding table entry.

Fields
uint code
ubyte len

Build Huffman encoding table.

Parameters

lengthsCode lengths for each symbol.

Returns

Encoding table.

Huffman decoding table.

Fields
uint[] limit
uint[] base
uint[] perm
int minLen
int maxLen
structBitWriter

Bit writer for MSB-first bit packing.

Fields
ubyte[] buffer
size_t pos
uint bsBuff
int bsLive
Methods
void init(size_t initialSize = 4096) pure @safeInitialize the writer.
void ensureCapacity(size_t extra) pure @safeEnsure buffer has space.
void writeBits(int n, uint value) pure @safeWrite n bits from value (MSB-first).
void writeBit(uint bit) pure @safeWrite a single bit.
void writeByte(ubyte b) pure @safeWrite a byte (8 bits).
void writeUInt24(uint value) pure @safeWrite a 24-bit value.
void writeUInt32(uint value) pure @safeWrite a 32-bit value.
void writeUnary(int count) pure @safeWrite unary encoded value.
void writeBitmap16(ushort bitmap) pure @safeWrite bitmap for 16 symbols.
void flush() pure @safeFlush remaining bits to output.
const(ubyte)[] getOutput() pure @safeGet output bytes.
const(ubyte)[] consumeOutput() @safeFlush complete bytes and return them, keeping partial bits in buffer.
void reset() pure @safeReset to initial state.
structBitReader
Fields
const(ubyte)[] data
size_t bytePos
uint bsBuff
int bsLive
Methods
void setInput(const(ubyte)[] input) pure nothrow @nogc @safeSet input data.
uint readBits(int n) @safeRead n bits.
uint readBit() @safeRead a single bit.
uint readUInt24() @safeRead a 24-bit value.
uint readUInt32() @safeRead a 32-bit value.
bool hasBits(int n) pure nothrow @nogc @safeCheck if we have n bits available.

Compressed block result.

Fields
ubyte[] data
uint blockCrc

Decompressed block result.

Fields
ubyte[] data
uint blockCrc
bool isEos

BZIP2 streaming compressor.

Methods
CompressionOptions options() const pure nothrow
void write(const(ubyte)[] data)
void flush(FlushMode mode = FlushMode.SYNC)
void finish()
void reset()
ulong bytesInTotal() const pure nothrow
ulong bytesOutTotal() const pure nothrow
bool setDictionary(const(ubyte)[] dict) pure nothrow
bool isFinished() const pure nothrow
Constructors
this(CompressionOptions opts)Create a new BZIP2 compressor.

BZIP2 streaming decompressor.

Methods
DecompressionOptions options() const pure nothrow
void write(const(ubyte)[] data)
void finish()
void reset()
ulong bytesInTotal() const pure nothrow
ulong bytesOutTotal() const pure nothrow
bool setDictionary(const(ubyte)[] dict) pure nothrow
bool isFinished() const pure nothrow
Constructors
this(DecompressionOptions opts)Create a new BZIP2 decompressor.

Functions 18

fnuint bz2Crc32Update(uint crc, ubyte b) pure nothrow @nogc @safeCompute BZIP2 CRC32 for a single byte.
fnuint bz2Crc32(const(ubyte)[] data) pure nothrow @nogc @safeCompute BZIP2 CRC32 for a buffer.
fnsize_t rle1EncodeCalcSize(const(ubyte)[] input) pure nothrow @nogc @safeCalculate the size of RLE1 encoded output.
fnsize_t rle1Encode(const(ubyte)[] input, ubyte[] output) pure nothrow @nogc @safeEncode data using RLE1.
fnubyte[] rle1Decode(const(ubyte)[] input) pure @safeDecode RLE1 encoded data.
fnBwtResult bwtTransform(const(ubyte)[] input) purePerform the Burrows-Wheeler Transform on the input data.
fnubyte[] bwtInverseTransform(const(ubyte)[] input, uint origPtr) pure @safeInverse BWT.
fnMtfResult mtfTransform(const(ubyte)[] input) pure @safePerform MTF transform.
fnubyte[] mtfInverseTransform(const(ubyte)[] indices, const bool[256] inUse, int nInUse) pure @safeInverse MTF transform.
fnRle2Result rle2Encode(const(ubyte)[] mtfIndices, int nInUse) pure @safeEncode MTF output using RLE2 (zero run encoding).
fnubyte[] rle2Decode(const(ushort)[] symbols, int nInUse, size_t maxOutput = size_t.max) @trustedDecode RLE2 to MTF indices.
fnubyte[] generateHuffmanLengths(const(uint)[] frequencies, int alphaSize, int maxLen = BZ_MAX_CODE_LEN) pureGenerate Huffman code lengths from frequencies.
fnHuffDecTable buildHuffmanDecodeTable(const(ubyte)[] lengths) pure @safeBuild Huffman decoding table.
fnint determineNumGroups(int nMTF) pure nothrow @nogcDetermine number of Huffman tables based on data size.
fnCompressedBlock compressBlock(const(ubyte)[] input, int blockSize100k, uint originalDataCrc = uint.max)Compress a block of RLE1-encoded data into a BZIP2 block.
fnDecompressedBlock decompressBlock(ref BitReader reader, int blockSize100k = 9)Decompress a single block.
fnCompressor makeBzip2Compressor(CompressionOptions opts)Create a BZIP2 compressor.
fnDecompressor makeBzip2Decompressor(DecompressionOptions opts)Create a BZIP2 decompressor.