Skip to content

Errors ​

lz4.zig returns typed, structured errors from the unified lz4.Error error set:

zig
pub const Error = CompressionError || DecompressionError || FrameError;

lz4.CompressionError ​

ErrorDescription
DstCapacityTooSmallDestination buffer is too small to receive compressed output.
InputTooLargeUncompressed input exceeds format limits (2,113,929,216 bytes).
CorruptInputInput contains invalid sequences for the selected compression mode.
OutOfMemorySystem allocator failed to allocate internal context buffers.

lz4.DecompressionError ​

ErrorDescription
MalformedInputInput is corrupted, has illegal tokens, or violates LZ4 grammar.
OffsetOutOfBoundsBack-reference match offset is zero or points before the start of available history.
CorruptLiteralLengthVariable-length literal run was truncated or overflowed.
CorruptMatchLengthVariable-length match extension was truncated or overflowed.
DstCapacityTooSmallCaller output buffer capacity exhausted before decompression completed.
SourceBufferExhaustedCompressed input ended abruptly in the middle of a sequence.
UnexpectedEndOfStreamUnexpected termination or missing required 5 trailing literal bytes.
OutOfMemorySystem allocator failed during buffer growth.

lz4.FrameError ​

ErrorDescription
InvalidMagicFirst 4 bytes do not match 0x184D2204 or skippable frame magics.
UnsupportedVersionFrame version field is not 01b.
ReservedBitsNonZeroReserved descriptor bits were non-zero.
HeaderChecksumMismatch1-byte descriptor checksum failed verification.
BlockChecksumMismatch4-byte block checksum failed verification.
ContentChecksumMismatch4-byte content xxHash-32 failed verification.
InvalidBlockMaxSizeDescriptor block max size is not in 4..7 (64KB..4MB).
BlockSizeTooLargeBlock size declared in frame exceeds descriptor limit.
MissingEndMarkExpected 4-byte 0x00000000 EndMark was not found.
PrematureEndOfFrameFrame truncated before reading block, EndMark, or footer.
DictionaryMismatchFrame requires a dictionary ID that does not match provided dictionary.
CorruptFrameCorrupt block payload inside frame container.

Idiomatic Error Handling ​

zig
const std = @import("std");
const lz4 = @import("lz4");

pub fn processInput(allocator: std.mem.Allocator, data: []const u8) !void {
    const uncompressed = lz4.decompress(allocator, data) catch |err| switch (err) {
        error.InvalidMagic => {
            std.debug.print("Not an LZ4 file or unsupported header\n", .{});
            return;
        },
        error.ContentChecksumMismatch, error.BlockChecksumMismatch => {
            std.debug.print("Data integrity check failed: corrupt payload\n", .{});
            return;
        },
        error.MalformedInput, error.OffsetOutOfBounds => {
            std.debug.print("Malformed LZ4 block data\n", .{});
            return;
        },
        error.OutOfMemory => return err,
        else => {
            std.debug.print("Decompression error: {}\n", .{err});
            return err;
        },
    };
    defer allocator.free(uncompressed);
}

Released under the MIT License.