package core:compress/lz4

⌘K
Ctrl+K
or
/

    Types

    Block_Max_Size ¶

    Block_Max_Size :: enum u8 {
    	Max_4MiB   = 0, 
    	Max_1MiB   = 1, 
    	Max_256KiB = 2, 
    	Max_64KiB  = 3, 
    }
     

    The largest block a frame holds; the zero value is 4 MiB

    Frame_Flag ¶

    Frame_Flag :: enum u8 {
    	Content_Checksum   = 2, // An xxHash-32 of the content after the last block
    	Content_Size       = 3, // The content size in the header
    	Block_Checksum     = 4, // An xxHash-32 after each block
    	Independent_Blocks = 5, // Blocks don't refer back to earlier blocks
    }
     

    A frame's optional features, at their bit positions in the frame descriptor

    Frame_Flags ¶

    Frame_Flags :: bit_set[Frame_Flag; u8]
     

    The features a frame has

    Related Constants

    Frame_Options ¶

    Frame_Options :: struct {
    	block_max_size: Block_Max_Size,
    	flags:          bit_set[Frame_Flag; u8],
    	dictionary_id:  runtime.Maybe($T=u32),
    }
     

    How to lay out a frame

    Related Procedures With Parameters

    Frame_Reader ¶

    Frame_Reader :: struct {
    	z:            ^compress.Context,
    	window:       []u8,
    	buffer:       []u8,
    	dictionary:   []u8,
    	scratch:      [dynamic]u8,
    	written:      int,
    	flushed:      int,
    	base:         int,
    	block_max:    int,
    	history:      int,
    	chunk:        int,
    	memory_max:   int,
    	limit:        i64,
    	total:        i64,
    	frame_limit:  i64,
    	frame_start:  i64,
    	content_size: i64,
    	consumed:     i64,
    	total_in:     i64,
    	total_out:    i64,
    	frames:       int,
    	digest:       xxhash.XXH32_state,
    	err:          compress.Error,
    	allocator:    runtime.Allocator,
    	options:      compress.Options,
    	state:        LZ4_Stream_State,
    	independent:  bool,
    	checksummed:  bool,
    	content_hash: bool,
    	done:         bool,
    	owns_window:  bool,
    }
     

    A streaming decoder of LZ4 frames; set up by frame_reader_init

    Related Procedures With Parameters

    Frame_Writer ¶

    Frame_Writer :: struct {
    	w:             io.Stream,
    	state:         ^LZ4,
    	window:        []u8,
    	out:           []u8,
    	dictionary:    []u8,
    	filled:        int,
    	start:         int,
    	history:       int,
    	out_len:       int,
    	block_max:     int,
    	total:         i64,
    	flushed:       i64,
    	input_size:    i64,
    	digest:        xxhash.XXH32_state,
    	err:           compress.Error,
    	allocator:     runtime.Allocator,
    	options:       compress.Options,
    	level:         Level,
    	frame_options: Frame_Options,
    	flags:         bit_set[Frame_Flag; u8],
    	needs_index:   bool,
    	finished:      bool,
    	owns_window:   bool,
    }
     

    A streaming encoder of one LZ4 frame to an io.Writer; set up by frame_writer_init

    Related Procedures With Parameters

    LZ4 ¶

    LZ4 :: struct {
    	head:  [65536]u32,
    	chain: [65536]u16,
    	fast:  [4096]u32,
    }
     

    The compressor's match finder tables, 400 KiB; the _to_slice procs take one to reuse

    Related Procedures With Parameters

    Level ¶

    Level :: enum i32 {
    	Level_1  = 1, 
    	Level_2  = 2, 
    	Level_3  = 3, 
    	Level_4  = 4, 
    	Level_5  = 5, 
    	Level_6  = 6, 
    	Level_7  = 7, 
    	Level_8  = 8, 
    	Level_9  = 9, 
    	Level_10 = 10, 
    	Level_11 = 11, 
    	Level_12 = 12, 
    	Fastest  = Level_1, 
    	Default  = Level_1, 
    	Best     = Level_12, 
    }
     

    1 and 2 are fast hash-table parsers, 3 to 9 lazy hash-chain parsers and 10 to 12 optimal parsers; others are clamped to that range

    Related Procedures With Parameters

    Constants

    CHUNK_SIZE ¶

    CHUNK_SIZE :: 256 * 1024
     

    The output a Frame_Reader decodes per step on top of its history when compress.Options.chunk_size is 0; at least a block is used

    DEFAULT_FRAME_FLAGS ¶

    DEFAULT_FRAME_FLAGS: bit_set[Frame_Flag; u8] : Frame_Flags{.Content_Checksum, .Content_Size}
     

    The flags frames get by default

    FRAME_DESCRIPTOR_MAX ¶

    FRAME_DESCRIPTOR_MAX :: 2 + 8 + 4

    FRAME_HEADER_MAX ¶

    FRAME_HEADER_MAX :: 4 + 2 + 8 + 4 + 1
     

    The largest frame header, its descriptor without magic and checksum, and the end mark with content checksum

    FRAME_MAGIC ¶

    FRAME_MAGIC :: u32(0x184D2204)
     

    The magic numbers that start a frame, a skippable frame (16 of them) and a legacy frame, and a legacy frame's block size

    LEGACY_BLOCK_SIZE ¶

    LEGACY_BLOCK_SIZE :: 8 << 20

    LEGACY_MAGIC ¶

    LEGACY_MAGIC :: u32(0x184C2102)

    SKIPPABLE_MAGIC_MAX ¶

    SKIPPABLE_MAGIC_MAX :: u32(0x184D2A5F)

    SKIPPABLE_MAGIC_MIN ¶

    SKIPPABLE_MAGIC_MIN :: u32(0x184D2A50)

    WRITER_CHUNK_SIZE ¶

    WRITER_CHUNK_SIZE :: 1024 * 1024
     

    The input a Frame_Writer buffers beyond its history and a block when compress.Options.chunk_size is 0

    Variables

    This section is empty.

    Procedures

    compress_block_bound ¶

    compress_block_bound :: proc(length: int) -> int {…}
     

    Returns the most bytes a block can take for an input size, or -1 if it's out of range

    compress_block_to_dynamic ¶

    compress_block_to_dynamic :: proc(
    	output:     ^[dynamic]u8, 
    	input:      []u8, 
    	level:      Level = Level.Default, 
    	options:    compress.Options = {}, 
    	dictionary: []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Compresses a byte slice to a raw LZ4 block into a dynamic array

    Allocates Using Provided Allocator

    Inputs:

    • output Replaced by the compressed data
    • input The data to compress
    • level (default: .Default)
    • options Caps the output size and working memory, and reports progress (default: {})
    • dictionary As for compress_block_to_slice (default: nil)
    • allocator For the compressor's state, and for output if it has no allocator yet (default: context.allocator)

    Returns:

    • err .Incompatible_Options if output is nil or an option negative, .Input_Too_Large, .Memory_Limit_Exceeded, .Output_Too_Short if the output limit is too small, .Canceled, an allocation error, or nil

    compress_block_to_slice ¶

    compress_block_to_slice :: proc(
    	s:             ^LZ4, 
    	output, input: []u8, 
    	level:         Level = Level.Default, 
    	options:       compress.Options = {}, 
    	dictionary:    []u8 = nil, 
    ) -> (written: int, err: compress.Error) {…}
     

    Compresses a byte slice to a raw LZ4 block into a slice, using the caller's state

    An output of at least compress_block_bound(len(input)) bytes always fits. A dictionary is joined to the input in temporary memory, freed before returning.

    Allocates Using Context's Temporary Allocator

    Inputs:

    • s The compressor's state; its contents don't matter
    • output The buffer to compress into; options.max_output_size can shorten it
    • input The data to compress
    • level (default: .Default)
    • options Caps the output size and reports progress (default: {})
    • dictionary Data the block may refer back to, up to its last 65535 bytes; decoding needs it (default: nil)

    Returns:

    • written The compressed size
    • err .Incompatible_Options if s is nil or an option negative, .Input_Too_Large, .Output_Too_Short, .Canceled, an allocation error, or nil

    compress_frame_bound ¶

    compress_frame_bound :: proc(
    	length:        int, 
    	frame_options: Frame_Options = Frame_Options{flags = DEFAULT_FRAME_FLAGS}, 
    ) -> int {…}
     

    Returns the most bytes a frame can take for an input size

    Inputs:

    • length The input size
    • frame_options The frame's layout (default: {flags = DEFAULT_FRAME_FLAGS})

    Returns:

    • The bound, or -1 if length is out of range

    compress_frame_to_dynamic ¶

    compress_frame_to_dynamic :: proc(
    	output:        ^[dynamic]u8, 
    	input:         []u8, 
    	level:         Level = Level.Default, 
    	frame_options: Frame_Options = Frame_Options{flags = DEFAULT_FRAME_FLAGS}, 
    	options:       compress.Options = {}, 
    	dictionary:    []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Compresses a byte slice to an LZ4 frame into a dynamic array

    Allocates Using Provided Allocator

    Inputs:

    • output Replaced by the compressed data; emptied on error
    • input The data to compress
    • level (default: .Default)
    • frame_options The frame's layout (default: {flags = DEFAULT_FRAME_FLAGS})
    • options Caps the output size and working memory, and reports progress (default: {})
    • dictionary As for compress_frame_to_slice (default: nil)
    • allocator For the compressor's state, and for output if it has no allocator yet (default: context.allocator)

    Returns:

    • err .Incompatible_Options if output is nil or an option negative, .Input_Too_Large, .Memory_Limit_Exceeded, .Output_Too_Short if the output limit is too small, .Canceled, an allocation error, or nil

    compress_frame_to_slice ¶

    compress_frame_to_slice :: proc(
    	s:             ^LZ4, 
    	output, input: []u8, 
    	level:         Level = Level.Default, 
    	frame_options: Frame_Options = Frame_Options{flags = DEFAULT_FRAME_FLAGS}, 
    	options:       compress.Options = {}, 
    	dictionary:    []u8 = nil, 
    ) -> (written: int, err: compress.Error) {…}
     

    Compresses a byte slice to an LZ4 frame into a slice, using the caller's state

    A block that doesn't shrink is stored. An output of at least compress_frame_bound(len(input), frame_options) bytes always fits. A dictionary is joined to the input in temporary memory, freed before returning.

    Allocates Using Context's Temporary Allocator

    Inputs:

    • s The compressor's state; its contents don't matter
    • output The buffer to compress into; options.max_output_size can shorten it
    • input The data to compress
    • level (default: .Default)
    • frame_options The frame's layout (default: {flags = DEFAULT_FRAME_FLAGS})
    • options Caps the output size and reports progress (default: {})
    • dictionary Data the blocks may refer back to, up to its last 65535 bytes; decoding needs it (default: nil)

    Returns:

    • written The compressed size
    • err .Incompatible_Options if s is nil or an option negative, .Input_Too_Large, .Output_Too_Short, .Canceled, an allocation error, or nil

    decompress_block_from_bytes ¶

    decompress_block_from_bytes :: proc(
    	output:               ^[dynamic]u8, 
    	input:                []u8, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	dictionary:           []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Decompresses a raw LZ4 block from a byte slice into a dynamic array

    Allocates Using Provided Allocator

    Inputs:

    • output Replaced by the decompressed data
    • input The block
    • expected_output_size The exact output size, or -1 if unknown (default: -1)
    • options Caps the output size and reports progress (default: {})
    • dictionary The data the block was compressed against, if any (default: nil)
    • allocator For output if it has no allocator yet (default: context.allocator)

    Returns:

    • err .Incompatible_Options if output is nil or an option negative, .Output_Too_Short past the output limit, .Size_Mismatch if the decoded size isn't the expected one, .Stream_Too_Short, an LZ4_Error for corrupt data, .Canceled, an allocation error, or nil

    decompress_block_from_context ¶

    decompress_block_from_context :: proc(
    	z:                    ^compress.Context, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	dictionary:           []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Decompresses a raw LZ4 block, the rest of a compress.Context's input, into its output array

    A stream's input is read into memory first.

    Allocates Using Provided Allocator

    Inputs:

    • z The context; its output is replaced by the decompressed data
    • expected_output_size The exact output size, or -1 if unknown (default: -1)
    • options Caps the output size and reports progress (default: {})
    • dictionary The data the block was compressed against, if any (default: nil)
    • allocator For the input read, freed before returning, and for z.output if it has no allocator yet (default: context.allocator)

    Returns:

    decompress_block_to_slice ¶

    decompress_block_to_slice :: proc(
    	output, input: []u8, 
    	options:       compress.Options = {}, 
    	dictionary:    []u8 = nil, 
    ) -> (written: int, err: compress.Error) {…}
     

    Decompresses a raw LZ4 block into a slice

    Inputs:

    • output The buffer to decompress into; options.max_output_size can shorten it
    • input The block
    • options Caps the output size and reports progress (default: {})
    • dictionary The data the block was compressed against, if any (default: nil)

    Returns:

    • written The decompressed size
    • err .Incompatible_Options if an option is negative, .Output_Too_Short, .Stream_Too_Short, an LZ4_Error for corrupt data, .Canceled, or nil

    decompress_frame_from_bytes ¶

    decompress_frame_from_bytes :: proc(
    	output:               ^[dynamic]u8, 
    	input:                []u8, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	dictionary:           []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Decompresses LZ4 frames from a byte slice into a dynamic array

    Allocates Using Provided Allocator

    Inputs:

    • output Replaced by the decompressed data
    • input The compressed data
    • expected_output_size The exact output size, or -1 if unknown (default: -1)
    • options Caps the output size and reports progress (default: {})
    • dictionary The data the frames were compressed against, if any (default: nil)
    • allocator For output if it has no allocator yet (default: context.allocator)

    Returns:

    decompress_frame_from_context ¶

    decompress_frame_from_context :: proc(
    	z:                    ^compress.Context, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	dictionary:           []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Decompresses LZ4 frames from a compress.Context into its output array

    Concatenated frames, legacy frames included, are decoded as one and skippable frames skipped; anything after them that doesn't start with a frame magic is left unread.

    Allocates Using Provided Allocator

    Inputs:

    • z The context; its output is replaced by the decompressed data
    • expected_output_size The exact output size, or -1 if unknown (default: -1)
    • options Caps the output size and reports progress (default: {})
    • dictionary The data the frames were compressed against, if any (default: nil)
    • allocator For a stream's blocks, freed before returning, and for z.output if it has no allocator yet (default: context.allocator)

    Returns:

    • err .Incompatible_Options if z.output is nil or an option negative, .Stream_Too_Short, .Size_Mismatch if the header's or the decoded size isn't the expected one, .Output_Too_Short past the output limit, an LZ4_Error for corrupt data, also .Dictionary_Required, the input stream's error, .Canceled, an allocation error, or nil

    decompress_frame_to_writer ¶

    decompress_frame_to_writer :: proc(
    	w:                    io.Stream, 
    	z:                    ^compress.Context, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	dictionary:           []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Decompresses LZ4 frames from a compress.Context to an io.Writer

    Frames are decoded as for decompress_frame_from_context. The decoder keeps a window of a frame's 64 KiB history, unless its blocks are independent, plus a block or options.chunk_size (default: CHUNK_SIZE), whichever is larger, within options.max_memory; options.buffer replaces it.

    Allocates Using Provided Allocator

    Inputs:

    • w Where the decompressed data goes; it's never closed
    • z The input
    • expected_output_size The exact output size, or -1 if unknown (default: -1)
    • options Caps the output size and working memory, sets the chunk size, and reports progress (default: {})
    • dictionary The data the frames were compressed against, if any (default: nil)
    • allocator For the decoder's window and blocks, freed before returning (default: context.allocator)

    Returns:

    • err .Output_Too_Short if options.max_output_size is exceeded, .Incompatible_Options if an option is negative or options.buffer too small for a frame, .Size_Mismatch if the header's or the decoded size isn't the expected one, .Memory_Limit_Exceeded, .Stream_Too_Short, an LZ4_Error for corrupt data, the input stream's error, .Canceled, the io.Writer's error, an allocation error, or nil

    frame_reader_destroy ¶

    frame_reader_destroy :: proc(r: ^Frame_Reader) {…}
     

    Frees a Frame_Reader's window, unless it is options.buffer, and its block buffer; its compress.Context is left alone

    frame_reader_init ¶

    frame_reader_init :: proc(
    	r:                    ^Frame_Reader, 
    	z:                    ^compress.Context, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	dictionary:           []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Sets up a Frame_Reader that decompresses LZ4 frames from a compress.Context

    Nothing is read until the first frame_reader_read. Frames and memory use are as for decompress_frame_to_writer.

    Inputs:

    • r The reader; it's cleared first
    • z The input; it must stay valid while the reader is used
    • expected_output_size The exact output size, or -1 if unknown (default: -1)
    • options Caps the output size and working memory, sets the chunk size, and reports progress (default: {})
    • dictionary The data the frames were compressed against, if any; it must stay valid while the reader is used (default: nil)
    • allocator For the window and blocks, allocated as frames need them (default: context.allocator)

    Returns:

    • err .Output_Too_Short if options.max_output_size is below the expected size, .Incompatible_Options if an option is negative, or nil

    frame_reader_read ¶

    frame_reader_read :: proc(r: ^Frame_Reader, p: []u8) -> (n: int, err: compress.Error) {…}
     

    Reads decompressed data from a Frame_Reader, decoding more as needed

    Inputs:

    • r The reader
    • p The buffer to fill

    Returns:

    • n The bytes read
    • err .EOF once all data has been read, a decoding error once the bytes before it have been read, or nil; an error sticks

    frame_reader_to_stream ¶

    frame_reader_to_stream :: proc(r: ^Frame_Reader) -> (s: io.Stream) {…}
     

    Wraps a Frame_Reader as an io.Stream that reads and destroys it; errors that aren't io.Errors read as .Unknown

    frame_writer_destroy ¶

    frame_writer_destroy :: proc(fw: ^Frame_Writer) {…}
     

    Frees a Frame_Writer's state, output buffer and window, unless the window is options.buffer, without finishing it; the io.Writer is left open

    frame_writer_finish ¶

    frame_writer_finish :: proc(fw: ^Frame_Writer) -> (err: compress.Error) {…}
     

    Compresses what a Frame_Writer still holds and ends the frame

    Calling it again does nothing. It doesn't free the writer; frame_writer_reset starts another frame.

    Inputs:

    • fw The writer

    Returns:

    • err .Size_Mismatch if fewer bytes came than the input size given, the io.Writer's error, .Canceled, an earlier error, or nil

    frame_writer_init ¶

    frame_writer_init :: proc(
    	fw:            ^Frame_Writer, 
    	w:             io.Stream, 
    	level:         Level = Level.Default, 
    	frame_options: Frame_Options = Frame_Options{flags = DEFAULT_FRAME_FLAGS}, 
    	input_size:    i64 = -1, 
    	options:       compress.Options = {}, 
    	dictionary:    []u8 = nil, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Sets up a Frame_Writer that compresses to one LZ4 frame on an io.Writer

    The header goes out with the first block. Without an input size the header leaves out the content size. The writer holds the compressor's state, an output buffer for a block, and an input window of 128 KiB of history plus a block, plus options.chunk_size or WRITER_CHUNK_SIZE as options.max_memory allows; options.buffer replaces the window.

    Allocates Using Provided Allocator

    Inputs:

    • fw The writer; it's cleared first
    • w Where the compressed stream goes; it's never closed
    • level (default: .Default)
    • frame_options The frame's layout (default: {flags = DEFAULT_FRAME_FLAGS})
    • input_size The exact bytes that will be written, or -1 if unknown (default: -1)
    • options Caps the working memory, sets the window size, and reports progress (default: {})
    • dictionary Data the blocks may refer back to, up to its last 65535 bytes; it must stay valid while the writer is used (default: nil)
    • allocator (default: context.allocator)

    Returns:

    • err .Incompatible_Options if an option or input_size is negative or options.buffer too small, .Memory_Limit_Exceeded, an allocation error, or nil; on error fw is destroyed

    frame_writer_reset ¶

    frame_writer_reset :: proc(
    	fw:         ^Frame_Writer, 
    	w:          io.Stream, 
    	input_size: i64 = -1, 
    ) -> (err: compress.Error) {…}
     

    Starts a new frame on a Frame_Writer, keeping its buffers, level, frame options and dictionary

    It clears an earlier error. The previous frame isn't finished.

    Inputs:

    • fw The writer, set up by frame_writer_init
    • w Where the new frame goes; it's never closed
    • input_size The exact bytes that will be written, or -1 if unknown (default: -1)

    Returns:

    • err .Incompatible_Options if fw isn't set up or input_size is below -1, or nil

    frame_writer_to_stream ¶

    frame_writer_to_stream :: proc(fw: ^Frame_Writer) -> (s: io.Stream) {…}
     

    Wraps a Frame_Writer as an io.Stream: closing it finishes the writer, destroying it destroys it; errors that aren't io.Errors read as .Unknown

    frame_writer_write ¶

    frame_writer_write :: proc(fw: ^Frame_Writer, p: []u8) -> (n: int, err: compress.Error) {…}
     

    Compresses data into a Frame_Writer, writing blocks out as they fill

    Inputs:

    • fw The writer
    • p The data to compress

    Returns:

    • n The bytes taken
    • err .Size_Mismatch if it goes past the input size given, taking nothing, .Closed after frame_writer_finish, the io.Writer's error, .Canceled, an earlier error, or nil

    Procedure Groups

    compress_block ¶

     

    Compresses a byte slice to a raw LZ4 block, into a slice or a dynamic array

    compress_frame ¶

     

    Compresses a byte slice to an LZ4 frame, into a slice or a dynamic array

    decompress_block ¶

     

    Decompresses a raw LZ4 block into a slice, or from a byte slice or a compress.Context into a dynamic array

    decompress_frame ¶

     

    Decompresses LZ4 frames from a byte slice or a compress.Context into a dynamic array

    Source Files

    Generation Information

    Generated with odin version dev-2026-11 (vendor "odin") Windows_amd64 @ 2026-11-30 21:05:49.814964300 +0000 UTC