package core:compress
Overview
Types and input handling shared by the compression packages below core:compress.
Optionscaps a call's output size and working memory, and carries a progress callbackErrorunites every codec's errors;General_Errorholds the ones they shareContextreads a codec's input from a byte slice or anio.Streamwhichdetects a stream's format from its first bytes
The codecs are their own packages: deflate (with gzip and zlib), bzip2, lz4, lzma (with
lzip), xz, ppmd, pkzip-legacy, shoco and filters.
Packages 11
| bzip2 | An implementation of the BZIP2 compression method | |
| deflate | Deflate compression and decompression, following RFC 1950 (zlib), RFC 1951 (DEFLATE) and | |
| filters | Reversible filters that make executables and some other data compress better | |
| gzip | A small GZIP unpacker. | |
| lz4 | This package implements Yann Collet's LZ4 Block and Frame compression and decompression. | |
| lzma | An implementation of the LZMA and LZMA2 compression methods | |
| pkzip-legacy | Decoders for PKZIP's legacy compression methods: Shrink (1), Reduce (2-5) and Implode (6). | |
| ppmd | An implementation of the PPMd compression methods | |
| shoco | Shoco short string compression and decompression. | |
| xz | An implementation of the .xz file format | |
| zlib | Deflate decompression of raw and ZLIB-type streams. |
Index
Constants (22)
Variables (0)
This section is empty.
Procedures (25)
- declared_size
- destroy_context
- init_context_from_slice
- init_context_from_stream
- input_size
- is_memory_context
- memory_limit
output_… (3)
peek_… (3)
- progress_interval
read_… (5)
- refill_window
- report_progress
- rewind
- size
- skip
- which
Procedure Groups (2)
Types
BZip2_Error ¶
BZip2_Error :: enum int { None = 0, Invalid_Magic, Invalid_Block_Magic, Invalid_Original_Pointer, No_Symbols_In_Use, Invalid_Group_Count, Invalid_Selector_Count, Invalid_Selector, Invalid_Code_Length, Invalid_Huffman_Code, Selectors_Exhausted, Run_Too_Long, Block_Too_Large, Block_Checksum_Failed, Stream_Checksum_Failed, Input_Too_Large, // The input is too large to compress }
bzip2's errors
Context ¶
Context :: struct #align (64) { input: io.Stream, // 32-bit: 8, 64-bit: 16 output: ^[dynamic]u8, // 32-bit: 4, 64-bit: 8 code_buffer: u64, // 32-bit: 8, 64-bit: 8 bytes_written: i64, // 32-bit: 8, 64-bit: 8 num_bits: u64, // 32-bit: 8, 64-bit: 8 buffer: []u8, window: []u8, backing: []u8, backing_allocator: runtime.Allocator, owns_backing: b8, _reserved: [15]u8, }
A codec's input, read from a byte slice or an io.Stream, and its in-memory output
A slice is read in place. A stream is read in blocks into backing, so window always holds the
bytes not yet read; set one up with init_context and free it with destroy_context.
Related Procedures With Parameters
Related Procedures With Returns
- init_context_from_slice
- init_context_from_stream
- init_context (procedure groups)
Deflate_Error ¶
Deflate_Error :: enum int { None = 0, Huffman_Bad_Sizes, Huffman_Bad_Code_Lengths, Bad_Distance, Bad_Huffman_Code, Len_Nlen_Mismatch, BType_3, }
Deflate's errors
Error ¶
Error :: union { General_Error, Deflate_Error, ZLIB_Error, GZIP_Error, LZ4_Error, BZip2_Error, PKZIP_Legacy_Error, LZMA_Error, PPMd_Error, BCJ2_Error, XZ_Error, LZIP_Error, io.Error, runtime.Allocator_Error, }
Any error a compression package returns
GZIP_Error ¶
GZIP_Error :: enum int { None = 0, Invalid_GZIP_Signature, Reserved_Flag_Set, Invalid_Extra_Data, Original_Name_Too_Long, Comment_Too_Long, Header_CRC_Invalid, Payload_Length_Invalid, Payload_CRC_Invalid, }
gzip's errors
General_Error ¶
General_Error :: enum int { None = 0, File_Not_Found, Cannot_Open_File, File_Too_Short, Stream_Too_Short, // The input ended early Output_Too_Short, // The output would exceed `max_output_size` or the caller's slice Size_Mismatch, // The output's size differs from the one expected or declared Unknown_Compression_Method, // An unknown method in a header, or an invalid shoco model Checksum_Failed, Incompatible_Options, // An argument or `Options` field is out of range, or they conflict Unimplemented, Canceled, // The progress callback returned `true` Allocation_Failed, Memory_Limit_Exceeded, // The working memory would exceed `max_memory`, or `buffer` is too small }
Errors any codec can return
LZ4_Error ¶
LZ4_Error :: enum int { None = 0, Invalid_Magic, Unsupported_Version, Reserved_Bit_Set, Invalid_Block_Max_Size, Header_Checksum_Failed, Block_Checksum_Failed, Content_Checksum_Failed, Block_Too_Large, Dictionary_Required, // The frame names a dictionary and none was given Invalid_Offset, Offset_Out_Of_Range, Literal_Overrun, Input_Too_Large, // The input is too large to compress }
LZ4's errors
LZIP_Error ¶
LZIP_Error :: enum int { None = 0, Invalid_Magic, Unsupported_Version, Invalid_Dictionary_Size, Invalid_Marker, // A flush marker where lzip allows only the end-of-stream one Empty_Member, // An empty member in a file with more than one Data_CRC_Invalid, Data_Size_Invalid, Member_Size_Invalid, }
lzip's errors
LZMA_Error ¶
LZMA_Error :: enum int { None = 0, Invalid_Properties, Invalid_Header, Corrupt_Range_Coder, Invalid_Distance, Invalid_Chunk, Input_Too_Large, // The input is too large to compress }
LZMA's and LZMA2's errors
Memory_Reader ¶
Memory_Reader :: io.Stream
A fake io.Stream for when the full input is in memory as a ^[]bytein itsdata` pointer.
Related Procedures With Parameters
- init_context_from_stream
- init_context (procedure groups)
Options ¶
Options :: struct { max_output_size: int, // In memory 0 means `COMPRESS_OUTPUT_ALLOCATE_MAX`; a reader's 0 means no limit; writers ignore it max_memory: int, // Working memory: history window, PPMd model; 0 means `COMPRESS_MEMORY_MAX` chunk_size: int, // Output a streaming decoder produces per step, or a writer buffers buffer: []u8, // Caller's memory for a streaming decoder or writer to use instead progress: Progress_Proc, // Called as the work goes on; returning `true` cancels progress_data: rawptr, // Passed to `progress` progress_interval: int, }
Per-call limits and progress reporting; a zero field means the default
Related Procedures With Parameters
PKZIP_Legacy_Error ¶
PKZIP_Legacy_Error :: enum int { None = 0, Invalid_First_Code, Invalid_Control_Code, Code_Size_Too_Large, Invalid_Code, Invalid_Follower_Set, Invalid_Follower_Index, Invalid_Tree, }
The errors of PKZIP's Shrink, Reduce and Implode
PPMd_Error ¶
PPMd_Error :: enum int { None = 0, Invalid_Properties, Corrupt_Data, Unsupported_Restore_Method, }
PPMd's errors
Progress ¶
Progress :: struct { bytes_in: i64, bytes_out: i64, // Read and written so far total_in: i64, total_out: i64, // The totals expected member: string, // The member's path member_index: int, member_count: int, archive_in: i64, archive_total_in: i64, }
What a Progress_Proc is told; -1 means unknown, and outside an archive member_index,
member_count and archive_total_in are -1
Progress_Proc ¶
Called with progress and Options.progress_data; returning true cancels with .Canceled
Stream_Format ¶
Stream_Format :: enum u8 { Unknown, Gzip, Zlib, BZip2, LZ4_Frame, XZ, LZMA_Alone, // A `.lzma` file Lzip, }
A compressed stream's format, as which detects it
XZ_Error ¶
XZ_Error :: enum int { None = 0, Invalid_Magic, Header_Checksum_Failed, Reserved_Bit_Set, Unsupported_Filter, Invalid_Filter_Chain, Invalid_Block_Header, Invalid_Padding, Block_Checksum_Failed, Invalid_Index, Index_Mismatch, Footer_Mismatch, Data_After_End, // Neither padding nor another stream follows a stream Input_Too_Large, // The input or its index is too large for xz's sizes }
xz's errors
ZLIB_Error ¶
ZLIB_Error :: enum int { None = 0, Unsupported_Window_Size, FDICT_Unsupported, Code_Buffer_Malformed, }
zlib's errors
Constants
BZIP2_EXPANSION_MAX ¶
BZIP2_EXPANSION_MAX :: 2_331_000
COMPRESS_GROWTH_STEP ¶
COMPRESS_GROWTH_STEP :: 16 * 1024 * 1024
An output buffer doubles until this size, then grows by a quarter, but at least this much
COMPRESS_INITIAL_RESERVATION ¶
COMPRESS_INITIAL_RESERVATION :: int(#config(COMPRESS_INITIAL_RESERVATION, 16 * 1024 * 1024))
The most output reserved up front; -define to change it
COMPRESS_INITIAL_RESERVATION_RATIO ¶
COMPRESS_INITIAL_RESERVATION_RATIO :: int(#config(COMPRESS_INITIAL_RESERVATION_RATIO, 4))
With only the input size known, the output reserved up front is this many times that size, 1 to 4
COMPRESS_MEMORY_MAX ¶
COMPRESS_MEMORY_MAX :: int(#config(COMPRESS_MEMORY_MAX, COMPRESS_OUTPUT_ALLOCATE_MAX))
The default cap on a decoder's working memory; -define to change it
COMPRESS_OUTPUT_ALLOCATE_MAX ¶
COMPRESS_OUTPUT_ALLOCATE_MAX :: int(#config(COMPRESS_OUTPUT_ALLOCATE_MAX, 1 << 32))
For 64-bit platforms, we set the default max buffer size to 4 GiB, which is GZIP and PKZIP's max payload size.
COMPRESS_RESERVE_DEFAULT ¶
COMPRESS_RESERVE_DEFAULT :: 64 * 1024
The output reserved up front when neither the output nor the input size is known
CONTEXT_HISTORY ¶
CONTEXT_HISTORY :: 16
CONTEXT_WINDOW_SIZE ¶
CONTEXT_WINDOW_SIZE :: 64 * 1024
The size of the read buffer init_context_from_stream allocates
DEFLATE64_EXPANSION_MAX ¶
DEFLATE64_EXPANSION_MAX :: 29128
DEFLATE_EXPANSION_MAX ¶
DEFLATE_EXPANSION_MAX :: 1032
The most output each method can produce per input byte, bounding output reservations
IMPLODE_EXPANSION_MAX ¶
IMPLODE_EXPANSION_MAX :: 256
LZ4_EXPANSION_MAX ¶
LZ4_EXPANSION_MAX :: 255
LZMA_ALONE_DICTIONARY_MIN ¶
LZMA_ALONE_DICTIONARY_MIN :: u32(1) << 12
The smallest .lzma dictionary size which accepts
LZMA_ALONE_HEADER_SIZE ¶
LZMA_ALONE_HEADER_SIZE :: 13
A .lzma header's size: properties byte, dictionary size and uncompressed size
LZMA_ALONE_PROPERTIES_MAX ¶
LZMA_ALONE_PROPERTIES_MAX :: (4 * 5 + 4) * 9 + 8
The largest valid .lzma properties byte, (pb * 5 + lp) * 9 + lc
LZMA_ALONE_UNPACKED_MAX ¶
LZMA_ALONE_UNPACKED_MAX :: u64(1) << 38
which takes a known .lzma uncompressed size from 256 GiB up as not .lzma, as xz does
LZMA_EXPANSION_MAX ¶
LZMA_EXPANSION_MAX :: 7168
PROGRESS_INTERVAL_DEFAULT ¶
PROGRESS_INTERVAL_DEFAULT :: 1 << 20
The default bytes between in-memory progress calls
REDUCE_EXPANSION_MAX ¶
REDUCE_EXPANSION_MAX :: 512
SHRINK_EXPANSION_MAX ¶
SHRINK_EXPANSION_MAX :: 8192
Variables
This section is empty.
Procedures
declared_size ¶
declared_size :: proc "contextless" ( expected_output_size: int, format_size: i64 = i64(-1), ) -> (size: int) {…}
Returns the output size to expect
Inputs:
- expected_output_size The caller's size, or a negative value if unknown
- format_size The size the format's header declares, or -1 (default: -1)
Returns:
- size The caller's size if known, else the format's if it fits in an
int, else -1
destroy_context ¶
destroy_context :: proc(z: ^Context) {…}
Frees a Context's read buffer, unless the caller supplied it
Inputs:
- z The context; its input stream is left open
init_context_from_slice ¶
Sets up a Context that reads a byte slice in place
Inputs:
- data The input; it must stay valid while the context is used
- output The array an in-memory decoder appends to (default: nil)
Returns:
- ctx The context;
destroy_contextis optional for it
init_context_from_stream ¶
init_context_from_stream :: proc( stream: io.Stream, output: ^[dynamic]u8 = nil, buffer: []u8 = nil, allocator := context.allocator, ) -> (ctx: Context, err: runtime.Allocator_Error) #optional_ok {…}
Sets up a Context that reads an io.Stream in blocks
Allocates Using Provided Allocator
Inputs:
- stream The input; it must stay open while the context is used
- output The array an in-memory decoder appends to (default: nil)
- buffer The caller's read buffer, used instead of allocating
CONTEXT_WINDOW_SIZEbytes (default: nil) - allocator (default: context.allocator)
Returns:
- ctx The context;
destroy_contextit when done - err An allocation error, or
nil
input_size ¶
Returns size, or -1 if it is unknown or doesn't fit in an int
is_memory_context ¶
Tests whether the Context was initialized with the full input in memory
memory_limit ¶
Returns options.max_memory, or COMPRESS_MEMORY_MAX if it is zero
output_grow_capacity ¶
Returns an output buffer's next capacity, at least need and at most limit; see COMPRESS_GROWTH_STEP
output_limit ¶
Returns options.max_output_size, or COMPRESS_OUTPUT_ALLOCATE_MAX if it is zero
output_reservation ¶
Returns how much output to reserve up front
Inputs:
- declared The output size from
declared_size, or -1 - bound The most output the input can produce, or -1
- input_size The input's size, or -1
Returns:
- size
declaredif known, elseCOMPRESS_INITIAL_RESERVATION_RATIOtimesinput_size, elseCOMPRESS_RESERVE_DEFAULT; at mostCOMPRESS_INITIAL_RESERVATIONandbound
peek_data_at_cursor ¶
@(optimization_mode="favor_size") peek_data_at_cursor :: proc(z: ^Context, $T: typeid) -> (res: T, err: io.Error) {…}
Reads a T like read_data, without consuming it
peek_data_at_offset ¶
@(optimization_mode="favor_size") peek_data_at_offset :: proc( z: ^Context, $T: typeid, #any_int offset: i64, ) -> (res: T, err: io.Error) {…}
Reads a T offset bytes ahead, without consuming anything
Data beyond a stream's read buffer is read with peek_input_at, so the stream must seek.
Returns:
- res The value, unaligned and in native byte order
- err
.Short_Bufferfor a negativeoffset, the errors ofrefill_window, ornil
peek_input_at ¶
@(optimization_mode="favor_size") peek_input_at :: proc(z: ^Context, buf: []u8, offset: i64) -> (err: io.Error) {…}
Reads a stream's bytes at an offset from the cursor, then seeks back
Inputs:
- z A stream's context; the stream must seek
- buf The bytes to fill
- offset Where to read, counted from the next unread byte
Returns:
- err
.EOFor.Unexpected_EOFif the stream ends first, the stream's error, ornil
progress_interval ¶
Returns options.progress_interval, or PROGRESS_INTERVAL_DEFAULT if it is zero
read_data ¶
@(optimization_mode="favor_size") read_data :: proc(z: ^Context, $T: typeid) -> (res: T, err: io.Error) {…}
Reads a T, unaligned and in native byte order; errors as refill_window
read_ptr ¶
Reads size_of(T) bytes into ptr^, like read_slice
read_remaining ¶
@(optimization_mode="favor_size") read_remaining :: proc( z: ^Context, allocator := context.allocator, ) -> (data: [dynamic]u8, err: io.Error) {…}
Reads the rest of the input into a new array
Allocates Using Provided Allocator
Inputs:
- z The context
- allocator (default: context.allocator)
Returns:
- data The bytes read, also on error; the caller deletes it
- err
.Buffer_FullpastCOMPRESS_OUTPUT_ALLOCATE_MAXor when allocation fails,.No_Progressif the stream keeps returning nothing, the stream's error, ornil
read_slice ¶
Fills a slice from the input
Inputs:
- z The context
- buf The bytes to fill; any length, reading past the read buffer if need be
Returns:
- err
.Short_Bufferif the input ends first, the stream's error, ornil
read_u8 ¶
Reads one byte; errors as refill_window
refill_window ¶
@(optimization_mode="favor_size") refill_window :: proc(z: ^Context, need: int) -> (err: io.Error) {…}
Makes at least need bytes available in z.window, reading more from a stream
Inputs:
- z The context
- need The bytes wanted; a stream gives at most
len(z.backing) - CONTEXT_HISTORYat once
Returns:
- err
.EOFif no input is left,.Short_Bufferif less thanneedis,.Buffer_Fullif a stream's context has no read buffer,.No_Progressif the stream keeps returning nothing, the stream's error, ornil
report_progress ¶
report_progress :: proc( options: Options, bytes_in, bytes_out, total_in, total_out: i64, ) -> (err: Error) {…}
Calls options.progress, if set, for work outside an archive
Inputs:
- options The call's options
- bytes_in, bytes_out Read and written so far
- total_in, total_out The totals expected, or -1
Returns:
- err
General_Error.Canceledif the callback returnedtrue, elsenil
rewind ¶
Steps back over bytes already read
Inputs:
- z The context
- n The bytes to step back; a stream's context keeps at most
CONTEXT_HISTORY
Returns:
- ok
false, leaving the context as it was, if fewer thannbytes are kept
size ¶
Returns how much input is left to read
Inputs:
- z The context
Returns:
- res The bytes left, buffered ones included
- err
.Unsupportedif the stream can't tell its size, the stream's error, ornil
skip ¶
Skips over input, seeking the stream when it can
Inputs:
- z The context
- offset The bytes to skip
Returns:
- err
.Short_Bufferifoffsetis negative or past the input's end, the stream's error, ornil
which ¶
which :: proc "contextless" (data: []u8) -> (format: Stream_Format) {…}
Detects a compressed stream's format from its first bytes
.lzma and zlib have no magic number; their headers are checked for plausible values instead,
so other data can be mistaken for them.
Inputs:
- data The stream's first bytes; 13 are enough for every format
Returns:
- format The format, or
.Unknown
Procedure Groups
init_context ¶
init_context :: proc{ init_context_from_slice, init_context_from_stream, }
peek_data ¶
peek_data :: proc{ peek_data_at_cursor, peek_data_at_offset, }
Reads a T at the cursor or at an offset, without consuming anything
Source Files
Generation Information
Generated with odin version dev-2026-11 (vendor "odin") Windows_amd64 @ 2026-11-30 21:05:49.814964300 +0000 UTC