package core:compress/filters
Overview
Reversible filters that make executables and some other data compress better
Branch converters (BCJ) for x86, PowerPC, IA-64, ARM, ARM Thumb, SPARC, ARM64 and RISC-V, Delta, and Swap2/Swap4,
as used by xz (and so zip method 95) and 7z, plus 7z's four-stream BCJ2.
Each filter runs in place over a buffer (BCJ2 one-shot between whole buffers), or streams through
Reader and Writer (Bcj2_Reader and Bcj2_Writer for BCJ2). Mapping xz filter IDs and 7z
method IDs onto them is up to those containers.
It is a port of Igor Pavlov's public domain LZMA SDK, and the output is byte-identical to the SDK's.
Types
BCJ2_Error ¶
BCJ2_Error :: compress.BCJ2_Error
Bcj2_Reader ¶
Bcj2_Reader :: struct { inputs: [Bcj2_Stream]Bcj2_Input, storage: []u8, owns_storage: bool, allocator: runtime.Allocator, probs: [258]u16, range_: u32, code: u32, ip: u32, prev: u32, marker: u32, pending: [4]u8, pending_pos: int, step: Bcj2_Step, err: compress.Error, }
Decodes BCJ2's four streams as they are read; set it up with bcj2_reader_init
Related Procedures With Parameters
Bcj2_Stream ¶
Bcj2_Stream :: enum u8 { Main, Call, Jump, RC, }
One of BCJ2's four streams, as in Bcj2_Streams
Bcj2_Streams ¶
Bcj2_Streams :: struct { main: [dynamic]u8, // The input without the converted call and jump targets call: [dynamic]u8, // Converted `E8` call targets, as big-endian absolute addresses jump: [dynamic]u8, // Converted `E9` and `0F 8x` jump targets, as big-endian absolute addresses rc: [dynamic]u8, }
The four streams bcj2_encode splits its input into
Bcj2_Writer ¶
Bcj2_Writer :: struct { outputs: [Bcj2_Stream]io.Stream, written: [Bcj2_Stream]i64, buffers: [Bcj2_Stream][]u8, filled: [Bcj2_Stream]int, input: []u8, pending: int, storage: []u8, owns_storage: bool, allocator: runtime.Allocator, probs: [258]u16, low: u64, range_: u32, cache: u8, cache_size: u64, ip: u64, prev: u32, size_limit: u64, relat_limit: u32, scanning: bool, finished: bool, err: compress.Error, }
Encodes what is written to it into BCJ2's four streams; set it up with bcj2_writer_init
Related Procedures With Parameters
Branch_Arch ¶
Branch_Arch :: enum u8 { X86, PowerPC, IA64, ARM, ARM_Thumb, SPARC, ARM64, RISCV, }
The instruction set a branch converter (BCJ) handles
Branch_Filter ¶
Branch_Filter :: struct { arch: Branch_Arch, start_offset: u32, }
A branch converter for arch, whose data starts at start_offset
Branch_State ¶
Branch_State :: struct { arch: Branch_Arch, pc: u32, // The stream offset of the next call's first byte x86_mask: u32, }
A branch converter's state between calls; set it up with branch_init
Related Procedures With Parameters
Delta_Filter ¶
Delta_Filter :: struct { distance: int, }
A Delta filter, distance from DELTA_DISTANCE_MIN to DELTA_DISTANCE_MAX bytes
Delta_State ¶
A Delta filter's state between calls; set it up with delta_init
Related Procedures With Parameters
Direction ¶
Direction :: enum u8 { Decode, Encode, }
Whether a stream filter undoes a filter or applies it
Related Procedures With Parameters
Error ¶
Error :: compress.Error
The errors the filters return, from core:compress
Related Procedures With Returns
Filter ¶
Filter :: union { Branch_Filter, Delta_Filter, Swap_Filter, }
Related Procedures With Parameters
General_Error ¶
General_Error :: compress.General_Error
Reader ¶
Reader :: struct { source: io.Stream, converter: Converter, buffer: []u8, start: int, ready: int, end: int, eof: bool, owns_buffer: bool, allocator: runtime.Allocator, err: compress.Error, }
Runs a Filter over what it reads from another reader; set it up with reader_init
Related Procedures With Parameters
Swap_Filter ¶
Swap_Filter :: enum u8 { Swap2, Swap4, }
A filter that byte-swaps each 2- or 4-byte word; it is its own inverse
Writer ¶
Writer :: struct { w: io.Stream, converter: Converter, buffer: []u8, pending: int, finished: bool, owns_buffer: bool, allocator: runtime.Allocator, err: compress.Error, }
Runs a Filter over what is written to it and passes the result on; set it up with writer_init
Related Procedures With Parameters
Constants
BCJ2_BUFFER_MIN ¶
BCJ2_BUFFER_MIN :: 64
The smallest caller buffer a Bcj2_Reader or Bcj2_Writer accepts
BCJ2_RELAT_LIMIT_DEFAULT ¶
BCJ2_RELAT_LIMIT_DEFAULT :: u32(0x0F) << 24
The default and largest distance from a call or jump to its target that the BCJ2 encoder converts
BCJ2_RELAT_LIMIT_MAX ¶
BCJ2_RELAT_LIMIT_MAX :: u32(1) << 31
BCJ2_SIDE_BUFFER_SIZE ¶
BCJ2_SIDE_BUFFER_SIZE :: 4 * 1024
The most buffer the BCJ2 reader and writer give each of the call, jump and rc streams
DELTA_DISTANCE_MAX ¶
DELTA_DISTANCE_MAX :: 256
FILTER_BUFFER_MIN ¶
FILTER_BUFFER_MIN :: 16
FILTER_CHUNK_SIZE ¶
FILTER_CHUNK_SIZE :: 64 * 1024
Variables
BRANCH_ALIGNMENT ¶
@(rodata) BRANCH_ALIGNMENT: [Branch_Arch]int = …
The alignment of each architecture's instructions; a branch converter's start offset must be a multiple of it
Procedures
bcj2_decode_to_dynamic ¶
bcj2_decode_to_dynamic :: proc( output: ^[dynamic]u8, main, call, jump, rc: []u8, allocator := context.allocator, ) -> (err: compress.Error) {…}
Decodes BCJ2's four streams into a dynamic array
Allocates Using Provided Allocator
Inputs:
- output The array to replace the contents of; it is emptied on error
- main, call, jump, rc The four streams, as in
Bcj2_Streams - allocator (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifoutputis nil,.Size_Mismatchif the sizes overflow,.Stream_Too_Short,BCJ2_Error.Corrupt_Data, an allocation error, ornil
bcj2_decode_to_slice ¶
bcj2_decode_to_slice :: proc( output: []u8, main, call, jump, rc: []u8, ) -> (err: compress.Error) {…}
Decodes BCJ2's four streams into a slice
Inputs:
- output The decoded data; its length must be
len(main) + len(call) + len(jump) - main, call, jump, rc The four streams, as in
Bcj2_Streams
Returns:
- err
.Size_Mismatchifoutputhas the wrong length,.Stream_Too_Short,BCJ2_Error.Corrupt_Data, ornil
bcj2_encode ¶
bcj2_encode :: proc( streams: ^Bcj2_Streams, input: []u8, relat_limit: u32 = BCJ2_RELAT_LIMIT_DEFAULT, file_size: i64 = i64(-1), allocator := context.allocator, ) -> (err: compress.Error) {…}
Splits x86 code into BCJ2's four streams
Allocates Using Provided Allocator
Inputs:
- streams The arrays to replace the contents of; they are emptied on error
- input The data to encode
- relat_limit Calls and jumps further than this aren't converted, up to
BCJ2_RELAT_LIMIT_MAX(default:BCJ2_RELAT_LIMIT_DEFAULT) - file_size Calls and jumps whose target lies past it aren't converted; -1 if unknown (default: -1)
- allocator (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifstreamsis nil orrelat_limittoo large, an allocation error, ornil
bcj2_reader_destroy ¶
bcj2_reader_destroy :: proc(r: ^Bcj2_Reader) {…}
Frees a Bcj2_Reader's buffers, unless the caller supplied them; the streams are left open
bcj2_reader_init ¶
bcj2_reader_init :: proc( r: ^Bcj2_Reader, main, call, jump, rc: io.Stream, options: compress.Options = {}, allocator := context.allocator, ) -> (err: compress.Error) {…}
Sets up a Bcj2_Reader that decodes BCJ2's four streams as it reads them
Allocates Using Provided Allocator
Inputs:
- r The reader to set up
- main, call, jump, rc The four streams; they must stay open while
ris used - options
chunk_sizesets the main stream's buffer size (0 meansFILTER_CHUNK_SIZE), orbuffer, at leastBCJ2_BUFFER_MINbytes, replaces all buffers;max_memorycaps them (default: {}) - allocator (default: context.allocator)
Returns:
- err
.Incompatible_Optionsfor an invalid option,.Memory_Limit_Exceeded, an allocation error, ornil
bcj2_reader_read ¶
bcj2_reader_read :: proc(r: ^Bcj2_Reader, p: []u8) -> (n: int, err: compress.Error) {…}
Reads decoded bytes into p
At the end of the main stream it checks that the other three streams are used up.
Inputs:
- r The reader
- p The buffer to fill
Returns:
- n How many bytes were read
- err
.EOFat the end,.Stream_Too_Short,BCJ2_Error.Corrupt_Data, a stream's error, ornil; an error sticks
bcj2_reader_to_stream ¶
bcj2_reader_to_stream :: proc(r: ^Bcj2_Reader) -> (s: io.Stream) {…}
Wraps a Bcj2_Reader in an io.Stream that supports read and destroy
bcj2_writer_destroy ¶
bcj2_writer_destroy :: proc(bw: ^Bcj2_Writer) {…}
Frees a Bcj2_Writer's buffers, unless the caller supplied them, without writing what they hold;
call bcj2_writer_finish first
bcj2_writer_finish ¶
bcj2_writer_finish :: proc(bw: ^Bcj2_Writer) -> (err: compress.Error) {…}
Encodes what is still buffered, ends the rc stream and flushes all four outputs; later calls do nothing
Inputs:
- bw The writer; its outputs are left open
Returns:
- err An output's error, an earlier error, or
nil
bcj2_writer_init ¶
bcj2_writer_init :: proc( bw: ^Bcj2_Writer, main, call, jump, rc: io.Stream, relat_limit: u32 = BCJ2_RELAT_LIMIT_DEFAULT, file_size: i64 = i64(-1), options: compress.Options = {}, allocator := context.allocator, ) -> (err: compress.Error) {…}
Sets up a Bcj2_Writer that splits what is written to it into BCJ2's four streams
Allocates Using Provided Allocator
Inputs:
- bw The writer to set up
- main, call, jump, rc The four outputs; they must stay open while
bwis used - relat_limit Calls and jumps further than this aren't converted, up to
BCJ2_RELAT_LIMIT_MAX(default:BCJ2_RELAT_LIMIT_DEFAULT) - file_size Calls and jumps whose target lies past it aren't converted; -1 if unknown (default: -1)
- options
chunk_sizesets the input and main buffer sizes (0 meansFILTER_CHUNK_SIZE), orbuffer, at leastBCJ2_BUFFER_MINbytes, replaces all buffers;max_memorycaps them (default: {}) - allocator (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifrelat_limitis too large or an option is invalid,.Memory_Limit_Exceeded, an allocation error, ornil
bcj2_writer_to_stream ¶
bcj2_writer_to_stream :: proc(bw: ^Bcj2_Writer) -> (s: io.Stream) {…}
Wraps a Bcj2_Writer in an io.Stream that supports write, destroy, and close as
bcj2_writer_finish
bcj2_writer_write ¶
bcj2_writer_write :: proc(bw: ^Bcj2_Writer, p: []u8) -> (n: int, err: compress.Error) {…}
Encodes p, writing to the four outputs as their buffers fill
Inputs:
- bw The writer
- p The bytes to write
Returns:
- n How many bytes of
pwere taken - err
.Closedafterbcj2_writer_finish, an output's error, ornil; an error sticks
branch_decode ¶
branch_decode :: proc "contextless" (state: ^Branch_State, data: []u8) -> (processed: int) {…}
Converts the branch targets in data back from absolute to relative addresses, in place
Inputs:
- state The converter, set up with
branch_init - data The next bytes of the stream
Returns:
- processed How many leading bytes are done; pass the rest again at the start of the next call, or leave them as they are at the end of the stream
branch_encode ¶
branch_encode :: proc "contextless" (state: ^Branch_State, data: []u8) -> (processed: int) {…}
Converts the branch targets in data from relative to absolute addresses, in place
Inputs:
- state The converter, set up with
branch_init - data The next bytes of the stream
Returns:
- processed How many leading bytes are done; pass the rest again at the start of the next call, or leave them as they are at the end of the stream
branch_init ¶
branch_init :: proc "contextless" ( state: ^Branch_State, arch: Branch_Arch, start_offset: u32 = 0, ) -> bool {…}
Sets up a branch converter's state
Inputs:
- state The state to reset
- arch The instruction set to convert
- start_offset The stream offset of the first byte, a multiple of
BRANCH_ALIGNMENT[arch](default: 0)
Returns:
falseifarchis out of range orstart_offsetis misaligned, elsetrue
delta_decode ¶
delta_decode :: proc "contextless" (state: ^Delta_State, data: []u8) {…}
Undoes delta_encode in place; it does nothing to a state not set up with delta_init
delta_encode ¶
delta_encode :: proc "contextless" (state: ^Delta_State, data: []u8) {…}
Replaces each byte with its difference from the byte distance before it, in place; it
does nothing to a state not set up with delta_init
delta_init ¶
delta_init :: proc "contextless" (state: ^Delta_State, distance: int) -> bool {…}
Sets up a Delta filter's state
Inputs:
- state The state to reset
- distance The distance in bytes, from
DELTA_DISTANCE_MINtoDELTA_DISTANCE_MAX
Returns:
falseifdistanceis out of range, elsetrue
reader_destroy ¶
reader_destroy :: proc(r: ^Reader) {…}
Frees a Reader's buffer, unless the caller supplied it; the source is left open
reader_init ¶
reader_init :: proc( r: ^Reader, source: io.Stream, filter: Filter, direction: Direction = Direction.Decode, options: compress.Options = {}, allocator := context.allocator, ) -> (err: compress.Error) {…}
Sets up a Reader that runs a filter over what it reads from source
Allocates Using Provided Allocator
Inputs:
- r The reader to set up
- source The input; it must stay open while
ris used - filter The filter to run
- direction Whether to undo or apply the filter (default: .Decode)
- options
chunk_sizesets the buffer size (0 meansFILTER_CHUNK_SIZE), orbuffer, at leastFILTER_BUFFER_MINbytes, replaces it;max_memorycaps it (default: {}) - allocator (default: context.allocator)
Returns:
- err
.Incompatible_Optionsfor an invalid filter, direction or option,.Memory_Limit_Exceeded, an allocation error, ornil
reader_read ¶
Reads filtered bytes into p
Inputs:
- r The reader
- p The buffer to fill
Returns:
- n How many bytes were read
- err
.EOFat the end of the input, the source's error, ornil; an error sticks
swap2 ¶
Swaps the bytes of each 2-byte word in place, and returns how many bytes it swapped; an odd last byte is left as is
swap4 ¶
Reverses the bytes of each 4-byte word in place, and returns how many bytes it swapped; the last
len(data) % 4 bytes are left as is
writer_destroy ¶
writer_destroy :: proc(fw: ^Writer) {…}
Frees a Writer's buffer, unless the caller supplied it, without writing what it holds; call
writer_finish first
writer_finish ¶
Filters and writes what is still buffered; later calls do nothing
Inputs:
- fw The writer; its output is left open
Returns:
- err The output's error, an earlier error, or
nil
writer_init ¶
writer_init :: proc( fw: ^Writer, w: io.Stream, filter: Filter, direction: Direction = Direction.Encode, options: compress.Options = {}, allocator := context.allocator, ) -> (err: compress.Error) {…}
Sets up a Writer that runs a filter over what is written to it and writes the result to w
Allocates Using Provided Allocator
Inputs:
- fw The writer to set up
- w The output; it must stay open while
fwis used - filter The filter to run
- direction Whether to apply or undo the filter (default: .Encode)
- options
chunk_sizesets the buffer size (0 meansFILTER_CHUNK_SIZE), orbuffer, at leastFILTER_BUFFER_MINbytes, replaces it;max_memorycaps it (default: {}) - allocator (default: context.allocator)
Returns:
- err
.Incompatible_Optionsfor an invalid filter, direction or option,.Memory_Limit_Exceeded, an allocation error, ornil
writer_to_stream ¶
Wraps a Writer in an io.Stream that supports write, destroy, and close as writer_finish
writer_write ¶
Filters p and writes the result once the buffer fills
Inputs:
- fw The writer
- p The bytes to write
Returns:
- n How many bytes of
pwere taken - err
.Closedafterwriter_finish, the output's error, ornil; an error sticks
Procedure Groups
bcj2_decode ¶
bcj2_decode :: proc{ bcj2_decode_to_slice, bcj2_decode_to_dynamic, }
Decodes BCJ2's four streams into a slice or 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