package core:compress/filters

⌘K
Ctrl+K
or
/

    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_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 ¶

    Delta_State :: struct {
    	distance: int,
    	history:  [256]u8,
    }
     

    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

    Filter ¶

    Filter :: union {
    	Branch_Filter, 
    	Delta_Filter, 
    	Swap_Filter, 
    }
     

    A filter a Reader or Writer runs

    Related Procedures With Parameters

    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_MIN ¶

    DELTA_DISTANCE_MIN :: 1
     

    The range of a Delta filter's distance in bytes

    FILTER_BUFFER_MIN ¶

    FILTER_BUFFER_MIN :: 16
     

    The smallest caller buffer a filter Reader or Writer accepts

    FILTER_CHUNK_SIZE ¶

    FILTER_CHUNK_SIZE :: 64 * 1024
     

    The buffer a filter Reader or Writer allocates when chunk_size is 0

    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_Options if output is nil, .Size_Mismatch if the sizes overflow, .Stream_Too_Short, BCJ2_Error.Corrupt_Data, an allocation error, or nil

    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:

    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_Options if streams is nil or relat_limit too large, an allocation error, or nil

    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 r is used
    • options chunk_size sets the main stream's buffer size (0 means FILTER_CHUNK_SIZE), or buffer, at least BCJ2_BUFFER_MIN bytes, replaces all buffers; max_memory caps them (default: {})
    • allocator (default: context.allocator)

    Returns:

    • err .Incompatible_Options for an invalid option, .Memory_Limit_Exceeded, an allocation error, or nil

    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 .EOF at the end, .Stream_Too_Short, BCJ2_Error.Corrupt_Data, a stream's error, or nil; 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 bw is 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_size sets the input and main buffer sizes (0 means FILTER_CHUNK_SIZE), or buffer, at least BCJ2_BUFFER_MIN bytes, replaces all buffers; max_memory caps them (default: {})
    • allocator (default: context.allocator)

    Returns:

    • err .Incompatible_Options if relat_limit is too large or an option is invalid, .Memory_Limit_Exceeded, an allocation error, or nil

    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 p were taken
    • err .Closed after bcj2_writer_finish, an output's error, or nil; 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:

    • false if arch is out of range or start_offset is misaligned, else true

    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:

    Returns:

    • false if distance is out of range, else true

    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 r is used
    • filter The filter to run
    • direction Whether to undo or apply the filter (default: .Decode)
    • options chunk_size sets the buffer size (0 means FILTER_CHUNK_SIZE), or buffer, at least FILTER_BUFFER_MIN bytes, replaces it; max_memory caps it (default: {})
    • allocator (default: context.allocator)

    Returns:

    • err .Incompatible_Options for an invalid filter, direction or option, .Memory_Limit_Exceeded, an allocation error, or nil

    reader_read ¶

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

    Reads filtered bytes into p

    Inputs:

    • r The reader
    • p The buffer to fill

    Returns:

    • n How many bytes were read
    • err .EOF at the end of the input, the source's error, or nil; an error sticks

    reader_to_stream ¶

    reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {…}
     

    Wraps a Reader in an io.Stream that supports read and destroy

    swap2 ¶

    swap2 :: proc "contextless" (data: []u8) -> (processed: int) {…}
     

    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 ¶

    swap4 :: proc "contextless" (data: []u8) -> (processed: int) {…}
     

    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 ¶

    writer_finish :: proc(fw: ^Writer) -> (err: compress.Error) {…}
     

    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 fw is used
    • filter The filter to run
    • direction Whether to apply or undo the filter (default: .Encode)
    • options chunk_size sets the buffer size (0 means FILTER_CHUNK_SIZE), or buffer, at least FILTER_BUFFER_MIN bytes, replaces it; max_memory caps it (default: {})
    • allocator (default: context.allocator)

    Returns:

    • err .Incompatible_Options for an invalid filter, direction or option, .Memory_Limit_Exceeded, an allocation error, or nil

    writer_to_stream ¶

    writer_to_stream :: proc(fw: ^Writer) -> (s: io.Stream) {…}
     

    Wraps a Writer in an io.Stream that supports write, destroy, and close as writer_finish

    writer_write ¶

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

    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 p were taken
    • err .Closed after writer_finish, the output's error, or nil; 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