package core:compress/lz4
Overview
This package implements Yann Collet's LZ4 Block and Frame compression and decompression. It is a cleanroom implementation straight from the spec without referencing Yann's code.
Index
Variables (0)
This section is empty.
Procedures (22)
Procedure Groups (4)
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
Error ¶
Error :: compress.Error
The core:compress error types this package returns
Related Procedures With Returns
- compress_block_to_dynamic
- compress_block_to_slice
- compress_frame_to_dynamic
- compress_frame_to_slice
- decompress_block_from_bytes
- decompress_block_from_context
- decompress_block_to_slice
- decompress_frame_from_bytes
- decompress_frame_from_context
- decompress_frame_to_writer
- frame_reader_init
- frame_reader_read
- frame_writer_finish
- frame_writer_init
- frame_writer_reset
- frame_writer_write
- compress_block (procedure groups)
- compress_frame (procedure groups)
- decompress_block (procedure groups)
- decompress_frame (procedure groups)
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_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
General_Error ¶
General_Error :: compress.General_Error
LZ4 ¶
The compressor's match finder tables, 400 KiB; the _to_slice procs take one to reuse
Related Procedures With Parameters
- compress_block_to_slice
- compress_frame_to_slice
- compress_block (procedure groups)
- compress_frame (procedure groups)
LZ4_Error ¶
LZ4_Error :: compress.LZ4_Error
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
- compress_block_to_dynamic
- compress_block_to_slice
- compress_frame_to_dynamic
- compress_frame_to_slice
- frame_writer_init
- compress_block (procedure groups)
- compress_frame (procedure groups)
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_FOOTER_MAX ¶
FRAME_FOOTER_MAX :: 4 + 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 ¶
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
outputif it has no allocator yet (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifoutputis nil or an option negative,.Input_Too_Large,.Memory_Limit_Exceeded,.Output_Too_Shortif the output limit is too small,.Canceled, an allocation error, ornil
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_sizecan 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_Optionsifsis nil or an option negative,.Input_Too_Large,.Output_Too_Short,.Canceled, an allocation error, ornil
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
lengthis 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
outputif it has no allocator yet (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifoutputis nil or an option negative,.Input_Too_Large,.Memory_Limit_Exceeded,.Output_Too_Shortif the output limit is too small,.Canceled, an allocation error, ornil
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_sizecan 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_Optionsifsis nil or an option negative,.Input_Too_Large,.Output_Too_Short,.Canceled, an allocation error, ornil
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
outputif it has no allocator yet (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifoutputis nil or an option negative,.Output_Too_Shortpast the output limit,.Size_Mismatchif the decoded size isn't the expected one,.Stream_Too_Short, anLZ4_Errorfor corrupt data,.Canceled, an allocation error, ornil
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
outputis 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.outputif it has no allocator yet (default: context.allocator)
Returns:
- err As
decompress_block_from_bytes, or the input stream's error
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_sizecan 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_Optionsif an option is negative,.Output_Too_Short,.Stream_Too_Short, anLZ4_Errorfor corrupt data,.Canceled, ornil
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
outputif 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
outputis 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.outputif it has no allocator yet (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifz.outputis nil or an option negative,.Stream_Too_Short,.Size_Mismatchif the header's or the decoded size isn't the expected one,.Output_Too_Shortpast the output limit, anLZ4_Errorfor corrupt data, also.Dictionary_Required, the input stream's error,.Canceled, an allocation error, ornil
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_Shortifoptions.max_output_sizeis exceeded,.Incompatible_Optionsif an option is negative oroptions.buffertoo small for a frame,.Size_Mismatchif the header's or the decoded size isn't the expected one,.Memory_Limit_Exceeded,.Stream_Too_Short, anLZ4_Errorfor corrupt data, the input stream's error,.Canceled, theio.Writer's error, an allocation error, ornil
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_Shortifoptions.max_output_sizeis below the expected size,.Incompatible_Optionsif an option is negative, ornil
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
.EOFonce all data has been read, a decoding error once the bytes before it have been read, ornil; 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_Mismatchif fewer bytes came than the input size given, theio.Writer's error,.Canceled, an earlier error, ornil
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_Optionsif an option orinput_sizeis negative oroptions.buffertoo small,.Memory_Limit_Exceeded, an allocation error, ornil; on errorfwis 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_Optionsiffwisn't set up orinput_sizeis below -1, ornil
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_Mismatchif it goes past the input size given, taking nothing,.Closedafterframe_writer_finish, theio.Writer's error,.Canceled, an earlier error, ornil
Procedure Groups
compress_block ¶
compress_block :: proc{ compress_block_to_slice, compress_block_to_dynamic, }
Compresses a byte slice to a raw LZ4 block, into a slice or a dynamic array
compress_frame ¶
compress_frame :: proc{ compress_frame_to_slice, compress_frame_to_dynamic, }
Compresses a byte slice to an LZ4 frame, into a slice or a dynamic array
decompress_block ¶
decompress_block :: proc{ decompress_block_to_slice, decompress_block_from_bytes, decompress_block_from_context, }
Decompresses a raw LZ4 block into a slice, or from a byte slice or a compress.Context into a
dynamic array
decompress_frame ¶
decompress_frame :: proc{ decompress_frame_from_bytes, decompress_frame_from_context, }
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