Skip to content

Errors ​

snappy.zig returns typed, structured errors from the canonical error sets:

zig
pub const Error = error{
    CorruptStream,
    TruncatedInput,
    OutputBufferTooSmall,
    ResourceLimitExceeded,
    InvalidChunkLength,
    UnknownChunkType,
    ChecksumMismatch,
    InvalidMagic,
    InvalidVarint,
    OutOfMemory,
    InputTooLarge,
};

pub const StreamError = Error || std.Io.Writer.Error || std.Io.Reader.Error;

Error Descriptions ​

ErrorDescription
CorruptStreamInput data is malformed or violates the Snappy format specification (e.g. invalid tag or illegal copy offset).
TruncatedInputInput data ended prematurely before all expected bytes or instructions were read.
OutputBufferTooSmallCaller-provided output buffer has insufficient capacity.
ResourceLimitExceededDecompressed content exceeds caller-configured maxOutputSize limit (decompression bomb protection).
InvalidChunkLengthChunk length in framed stream exceeds 65,536 bytes or is less than header minimums.
UnknownChunkTypeUnrecognized unskippable chunk type (0x02..0x7f) encountered in framed stream.
ChecksumMismatch32-bit masked CRC-32C checksum does not match decoded chunk data.
InvalidMagicStream does not start with the mandatory Snappy stream identifier (0xff 0x06 0x00 0x00 sNaPpY).
InvalidVarintLittle-endian varint encoding is unterminated or overflows 32 bits.
OutOfMemorySystem allocator failed to allocate memory.
InputTooLargeInput size exceeds format limits ($2^{32} - 1$ bytes).

Idiomatic Error Handling Pattern ​

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

pub fn processInput(allocator: std.mem.Allocator, data: []const u8) !void {
    const uncompressed = snappy.decompress(allocator, data) catch |err| switch (err) {
        error.InvalidMagic => {
            std.debug.print("Not a valid Snappy framed stream\n", .{});
            return;
        },
        error.ChecksumMismatch => {
            std.debug.print("Data integrity check failed: corrupt payload\n", .{});
            return;
        },
        error.CorruptStream, error.TruncatedInput, error.InvalidVarint => {
            std.debug.print("Malformed Snappy data\n", .{});
            return;
        },
        error.ResourceLimitExceeded => {
            std.debug.print("Decompressed payload exceeded safety limit\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.