Skip to content

Constants ​

Version ​

zig
pub const version_string: []const u8 = "0.0.1";
pub const version_major: u32 = 0;

pub fn version() struct { major: u32, minor: u32, patch: u32 };
zig
const version = bzip2.version();
// .{ .major = 0, .minor = 0, .patch = 1 }

try std.testing.expectEqualStrings("0.0.1", bzip2.version_string);

version_major lets a caller gate on a feature without depending on the patch number:

zig
if (bzip2.version_major >= 1) {
    // a feature added in 1.0
}

Format limits ​

zig
pub const max_block_size: usize = 900_000;

The largest block the format allows. Level 9 uses it. Input larger than one block becomes a multi-block stream automatically; nothing needs to be configured.

zig
// A single block at level 9 is 900 kB of coded data.
const largest = bzip2.max_block_size;

Reading a header ​

zig
pub fn blockSizeOfStream(data: []const u8) Error!usize;

Returns the block size in bytes, which is the level multiplied by 100 kB:

LevelBlock size
1100,000
2200,000
3300,000
4400,000
5500,000
6600,000
7700,000
8800,000
9900,000
zig
try std.testing.expectEqual(
    @as(usize, 900_000),
    try bzip2.blockSizeOfStream("BZh9"),
);
try std.testing.expectEqual(
    @as(usize, 100_000),
    try bzip2.blockSizeOfStream("BZh1"),
);

Four bytes are enough to answer, and nothing is decoded. Anything that is not a well formed BZh<1..9> prefix returns error.InvalidStreamHeader, which makes the function safe to call on data of unknown origin:

zig
try std.testing.expectError(
    error.InvalidStreamHeader,
    bzip2.blockSizeOfStream("not a bzip2 stream"),
);

Sizing a buffer ​

zig
pub fn maxCompressedSize(input_size: usize, level: u8) Error!usize;

An upper bound on the compressed size of input_size bytes at level:

zig
const capacity = try bzip2.maxCompressedSize(4 * 1024 * 1024, 9);

The bound accounts for the 5/4 expansion the run-length stage can cause, plus per-block framing and the Huffman symbol and selector tables. It is generous by design and exists so a caller can size one buffer before compressing into it. It is not a prediction of the result.

Returns error.InvalidCompressionLevel for a level outside 1 through 9.

Convenience aliases ​

zig
pub const Level = u8;

The compression level, 1 through 9. Used as the level field of CompressionOptions: it is the block size in units of 100 kB, not a speed setting.

Stats ​

zig
pub const Stats = struct {
    blocks: u64,
    output_bytes: usize,
};

Populated on a Decompressor after a stream completes:

zig
try decoder.stream(reader, writer);
const stats = decoder.decompressor.stats;
FieldMeaning
blocksNumber of blocks decoded
output_bytesTotal bytes written to the sink

Module layout ​

FileContents
src/bzip2.zigPublic API
src/compress.zigEncoder
src/decompress.zigDecoder
src/bwt.zigBurrows-Wheeler transform
src/huffman.zigHuffman code construction and decoding
src/mtf.zigMove-to-front and run lengths
src/rle.zigRun-length stage
src/randomise.zigLegacy randomised block support
src/utils.zigConstants, errors, CRC, bit level I/O
DirectoryContents
examples/Ten runnable programs

Format constants, the CRC implementation and the bit-level readers and writers live in src/utils.zig. They are internal rather than re-exported, because they describe the wire format rather than the library's surface.

Released under the MIT License.