package core:compress/ppmd

⌘K
Ctrl+K
or
/

    Overview

    An implementation of the PPMd compression methods

    Reads and writes raw PPMd var.H streams as stored in 7z (method 03 04 01), and raw PPMd var.I rev.1 streams as stored in zip (method 98). It is a port of Igor Pavlov's public domain code from the LZMA SDK and 7-Zip, and the encoder's output is byte-identical to 7-Zip's.

    Types

    Level ¶

    Level :: enum i32 {
    	Level_0 = 0, 
    	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, 
    	Fastest = Level_0, 
    	Default = Level_5, 
    	Best    = Level_9, 
    }
     

    The compression levels level_properties maps to model order and memory size; others are clamped to 0 to 9

    Properties ¶

    Properties :: struct {
    	variant:  Variant,
    	order:    u8,
    	// The model order, `ORDER_MIN` up to `H_ORDER_MAX` or `I_ORDER_MAX`
    	mem_size: u32,
    	// The model's memory in bytes, `MEM_SIZE_MIN` to `MEM_SIZE_MAX`
    	restore:  Restore_Method,
    }
     

    A PPMd stream's parameters; encoder and decoder must use the same ones

    Related Procedures With Parameters

    Related Procedures With Returns

    Reader ¶

    Reader :: struct {
    	decoder:     Decoder,
    	window:      []u8,
    	options:     compress.Options,
    	total_in:    i64,
    	err:         compress.Error,
    	allocator:   runtime.Allocator,
    	owns_window: bool,
    }
     

    A streaming PPMd decoder; set up by reader_init

    Related Procedures With Parameters

    Restore_Method ¶

    Restore_Method :: enum u8 {
    	Restart = 0, // Starts a new model
    	Cut_Off = 1, // Prunes the model, or restarts it while less than half the memory is in use
    	Freeze  = 2, // Stops updating the model; not supported
    }
     

    What var.I does when the model's memory is full; var.H always restarts

    Variant ¶

    Variant :: enum u8 {
    	H, 
    	I, 
    }
     

    The PPMd variant: H as stored in 7z, I rev.1 as stored in zip

    Writer ¶

    Writer :: struct {
    	model_h:     ^Model_H,
    	model_i:     ^Model_I,
    	rc_h:        Encoder_H,
    	rc_i:        Coder_I,
    	sink:        Sink,
    	window:      []u8,
    	options:     compress.Options,
    	total_in:    i64,
    	err:         compress.Error,
    	allocator:   runtime.Allocator,
    	end_marker:  bool,
    	finished:    bool,
    	owns_window: bool,
    }
     

    A streaming PPMd encoder; set up by writer_init

    Related Procedures With Parameters

    Constants

    CHUNK_SIZE ¶

    CHUNK_SIZE :: 256 * 1024
     

    The default output buffer of decompress_to_writer and the Writer

    CUT_OFF_MEM_SIZE_MIN ¶

    CUT_OFF_MEM_SIZE_MIN :: (1 + 256 / 2) * 12 * 4 / 3
     

    The smallest model memory size .Cut_Off accepts

    MEMORY_MAX ¶

    MEMORY_MAX :: int(#config(PPMD_MEMORY_MAX, MEM_SIZE_MAX))

    MEM_SIZE_MAX ¶

    MEM_SIZE_MAX :: 0xFFFF_FFFF - 12 * 3

    MEM_SIZE_MIN ¶

    MEM_SIZE_MIN :: 1 << 11
     

    The model memory size's range, in bytes

    ORDER_MIN ¶

    ORDER_MIN :: 2
     

    The model order's range for each variant

    PROPERTIES_7Z_SIZE ¶

    PROPERTIES_7Z_SIZE :: 5
     

    The size of 7z's properties: order, then memory size as a little-endian u32

    PROPERTIES_ZIP_SIZE ¶

    PROPERTIES_ZIP_SIZE :: 2
     

    The size of zip's properties, a little-endian u16: order - 1, memory size in MiB - 1 and restore method, in 4, 8 and 4 bits

    ZIP_MEM_SIZE_MAX ¶

    ZIP_MEM_SIZE_MAX :: 256 << 20
     

    The largest model memory size zip's properties hold

    Variables

    This section is empty.

    Procedures

    compress_to_dynamic ¶

    compress_to_dynamic :: proc(
    	output:     ^[dynamic]u8, 
    	input:      []u8, 
    	props:      Properties, 
    	end_marker: bool = false, 
    	options:    compress.Options = {}, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Compresses a byte slice into a dynamic array

    Allocates Using Provided Allocator

    Inputs:

    • output Replaced by the compressed data; emptied on an error
    • input The data
    • props The model's properties; see level_properties
    • end_marker Ends the stream with an end marker, so it decodes without its size (default: false)
    • options Caps the output size and working memory, and reports progress (default: {})
    • allocator For the model, freed before returning, and for output if it has no allocator yet (default: context.allocator)

    Returns:

    • err .Incompatible_Options if output is nil, .Output_Too_Short past options.max_output_size or COMPRESS_OUTPUT_ALLOCATE_MAX, else as compress_to_slice

    compress_to_slice ¶

    compress_to_slice :: proc(
    	output, input: []u8, 
    	props:         Properties, 
    	end_marker:    bool = false, 
    	options:       compress.Options = {}, 
    	allocator := context.allocator, 
    ) -> (written: int, err: compress.Error) {…}
     

    Compresses a byte slice into a slice

    Allocates Using Provided Allocator

    Inputs:

    • output The buffer to compress into; options.max_output_size can shorten it
    • input The data
    • props The model's properties; see level_properties
    • end_marker Ends the stream with an end marker, so it decodes without its size (default: false)
    • options Caps the output size and working memory, and reports progress (default: {})
    • allocator For the model, freed before returning (default: context.allocator)

    Returns:

    • written The compressed size
    • err .Incompatible_Options if an option is negative, .Invalid_Properties or .Unsupported_Restore_Method for bad props, .Memory_Limit_Exceeded if the model's memory exceeds options.max_memory or MEMORY_MAX, .Output_Too_Short if the output doesn't fit, .Canceled, an allocation error, or nil

    decode_properties_7z ¶

    decode_properties_7z :: proc "contextless" (data: []u8) -> (props: Properties, err: compress.Error) {…}
     

    Reads 7z's PPMd properties

    Inputs:

    Returns:

    • props var.H properties
    • err .Invalid_Properties if data is short or a value out of range, or nil

    decode_properties_zip ¶

    decode_properties_zip :: proc "contextless" (data: []u8) -> (props: Properties, err: compress.Error) {…}
     

    Reads zip's PPMd properties, the first two bytes of a method 98 entry's data

    Inputs:

    Returns:

    • props var.I properties
    • err .Unsupported_Restore_Method for .Freeze, .Invalid_Properties if data is short or a value out of range, or nil

    decompress_from_bytes ¶

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

    Decompresses a raw PPMd stream from a byte slice into a dynamic array

    Allocates Using Provided Allocator

    Inputs:

    • output Replaced by the decompressed data
    • input The compressed data
    • props The properties it was compressed with
    • expected_output_size The exact output size, or -1 if the stream ends with an end marker (default: -1)
    • options Caps the output size and working memory, and reports progress (default: {})
    • allocator For the model, freed before returning, and for output if it has no allocator yet (default: context.allocator)

    Returns:

    decompress_from_context ¶

    decompress_from_context :: proc(
    	z:                    ^compress.Context, 
    	props:                Properties, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Decompresses a raw PPMd stream from a compress.Context into its output

    Allocates Using Provided Allocator

    Inputs:

    • z The context; its output is replaced by the decompressed data
    • props The properties it was compressed with
    • expected_output_size The exact output size, or -1 if the stream ends with an end marker (default: -1)
    • options Caps the output size and working memory, and reports progress (default: {})
    • allocator For the model, 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, .Invalid_Properties or .Unsupported_Restore_Method for bad props, .Memory_Limit_Exceeded if the model's memory exceeds options.max_memory or MEMORY_MAX, .Output_Too_Short past the output cap, .Stream_Too_Short, .Size_Mismatch if the data ends before expected_output_size bytes or goes on past them, .Corrupt_Data, .Canceled, the input stream's error, an allocation error, or nil

    decompress_to_writer ¶

    decompress_to_writer :: proc(
    	w:                    io.Stream, 
    	z:                    ^compress.Context, 
    	props:                Properties, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Decompresses a raw PPMd stream to a writer, a chunk at a time

    Allocates Using Provided Allocator

    Inputs:

    • w Where the decompressed data goes; it's never closed
    • z The input
    • props The properties it was compressed with
    • expected_output_size The exact output size, or -1 if the stream ends with an end marker (default: -1)
    • options Caps the output size and working memory, sizes or supplies the chunk buffer (CHUNK_SIZE by default), and reports progress once per chunk (default: {})
    • allocator For the model and the chunk buffer, freed before returning (default: context.allocator)

    Returns:

    • err .Incompatible_Options if an option is negative or options.buffer empty but not nil, .Memory_Limit_Exceeded if the model's memory and chunk buffer exceed options.max_memory, w's error, else as reader_init and decompress_from_context

    encode_properties_7z ¶

    encode_properties_7z :: proc "contextless" (props: Properties) -> (data: [5]u8, err: compress.Error) {…}
     

    Writes PPMd properties as 7z stores them

    Inputs:

    • props var.H properties

    Returns:

    • data The coder properties
    • err .Invalid_Properties if props isn't var.H or is out of range, or nil

    encode_properties_zip ¶

    encode_properties_zip :: proc "contextless" (props: Properties) -> (data: [2]u8, err: compress.Error) {…}
     

    Writes PPMd properties as zip stores them, ahead of the compressed data

    Inputs:

    • props var.I properties; the memory size a whole number of MiB up to ZIP_MEM_SIZE_MAX

    Returns:

    • data The two bytes
    • err .Unsupported_Restore_Method for .Freeze, .Invalid_Properties if props isn't var.I or is out of range, or nil

    level_properties ¶

    level_properties :: proc "contextless" (
    	variant:    Variant, 
    	level:      Level = Level.Default, 
    	input_size: int = -1, 
    ) -> (props: Properties) {…}
     

    Returns the properties for a compression level

    Level l takes 1 << (19 + l) bytes of memory, at least 1 MiB for var.I. var.H's order rises from 3 to 32, var.I's is 3 + l, restoring with .Cut_Off from level 7.

    Inputs:

    • variant The variant to compress with
    • level The level, clamped to 0 to 9 (default: .Default)
    • input_size The input's size, to shrink the memory to the power of two at or above 16 times it, at least 64 KiB, or 1 MiB for var.I; -1 if unknown (default: -1)

    Returns:

    • props The properties

    reader_destroy ¶

    reader_destroy :: proc(r: ^Reader) {…}
     

    Frees a Reader's model; its compress.Context is left alone

    reader_init ¶

    reader_init :: proc(
    	r:                    ^Reader, 
    	z:                    ^compress.Context, 
    	props:                Properties, 
    	expected_output_size: int = -1, 
    	options:              compress.Options = {}, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Sets up a Reader that decompresses a raw PPMd stream as it is read

    Allocates Using Provided Allocator

    Inputs:

    • r The reader; it's cleared first, and destroyed on an error
    • z The input; it must stay valid while the reader is used
    • props The properties it was compressed with
    • expected_output_size The exact output size, or -1 if the stream ends with an end marker (default: -1)
    • options Caps the output size and working memory, and reports progress (default: {})
    • allocator For the model, freed by reader_destroy (default: context.allocator)

    Returns:

    • err .Incompatible_Options if an option is negative, .Output_Too_Short if expected_output_size exceeds options.max_output_size, .Invalid_Properties or .Unsupported_Restore_Method for bad props, .Memory_Limit_Exceeded if the model's memory exceeds options.max_memory or MEMORY_MAX, .Stream_Too_Short or .Corrupt_Data for a bad stream start, the input stream's error, an allocation error, or nil

    reader_read ¶

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

    Decompresses into a buffer

    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 returned, or nil

    reader_to_stream ¶

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

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

    writer_destroy ¶

    writer_destroy :: proc(pw: ^Writer) {…}
     

    Frees a Writer's model and its buffer, unless it is options.buffer, without finishing it

    writer_finish ¶

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

    Ends the stream, writing the end marker if asked for and flushing the buffer to w

    Inputs:

    • pw The writer; w is left open, and later calls do nothing

    Returns:

    • err An earlier call's error, w's error, .Canceled, or nil

    writer_init ¶

    writer_init :: proc(
    	pw:         ^Writer, 
    	w:          io.Stream, 
    	props:      Properties, 
    	end_marker: bool = false, 
    	options:    compress.Options = {}, 
    	allocator := context.allocator, 
    ) -> (err: compress.Error) {…}
     

    Sets up a Writer that compresses to w

    The output is buffered and written to w whenever the buffer fills, and by writer_finish.

    Allocates Using Provided Allocator

    Inputs:

    • pw The writer; it's cleared first, and destroyed on an error
    • w Where the compressed data goes; it's never closed
    • props The model's properties; see level_properties
    • end_marker Ends the stream with an end marker, so it decodes without its size (default: false)
    • options Caps the working memory, sizes or supplies the buffer (CHUNK_SIZE by default), and reports progress (default: {})
    • allocator For the model and the buffer, freed by writer_destroy (default: context.allocator)

    Returns:

    • err .Incompatible_Options if an option is negative or options.buffer empty but not nil, .Invalid_Properties or .Unsupported_Restore_Method for bad props, .Memory_Limit_Exceeded if the model's memory and buffer exceed options.max_memory, or the model's MEMORY_MAX, an allocation error, or nil

    writer_to_stream ¶

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

    Wraps a Writer as an io.Stream whose close finishes it and whose destroy destroys it; errors that aren't io.Errors read as .Unknown

    writer_write ¶

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

    Compresses more data

    Inputs:

    • pw The writer
    • p The data

    Returns:

    Procedure Groups

    compress ¶

    compress :: proc{
    	compress_to_slice,
    	compress_to_dynamic,
    }
    
     

    Compresses a byte slice into a slice or a dynamic array

    decompress ¶

     

    Decompresses a raw PPMd stream 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