package core:compress/ppmd
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.
Index
Variables (0)
This section is empty.
Procedures (19)
- compress_to_dynamic
- compress_to_slice
- decode_properties_7z
- decode_properties_zip
decompress_… (3)
- encode_properties_7z
- encode_properties_zip
- level_properties
reader_… (4)
writer_… (5)
Procedure Groups (2)
Types
Error ¶
Error :: compress.Error
The core:compress error types this package returns
Related Procedures With Returns
- compress_to_dynamic
- compress_to_slice
- decode_properties_7z
- decode_properties_zip
- decompress_from_bytes
- decompress_from_context
- decompress_to_writer
- encode_properties_7z
- encode_properties_zip
- reader_init
- reader_read
- writer_finish
- writer_init
- writer_write
- compress (procedure groups)
- decompress (procedure groups)
General_Error ¶
General_Error :: compress.General_Error
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
PPMd_Error ¶
PPMd_Error :: compress.PPMd_Error
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
- compress_to_dynamic
- compress_to_slice
- decompress_from_bytes
- decompress_from_context
- decompress_to_writer
- encode_properties_7z
- encode_properties_zip
- reader_init
- writer_init
- compress (procedure groups)
- decompress (procedure groups)
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
H_ORDER_MAX ¶
H_ORDER_MAX :: 64
I_ORDER_MAX ¶
I_ORDER_MAX :: 16
MEMORY_MAX ¶
MEMORY_MAX :: int(#config(PPMD_MEMORY_MAX, MEM_SIZE_MAX))
MEM_SIZE_MAX ¶
MEM_SIZE_MAX :: 0xFFFF_FFFF - 12 * 3
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
outputif it has no allocator yet (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifoutputis nil,.Output_Too_Shortpastoptions.max_output_sizeorCOMPRESS_OUTPUT_ALLOCATE_MAX, else ascompress_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_sizecan 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_Optionsif an option is negative,.Invalid_Propertiesor.Unsupported_Restore_Methodfor badprops,.Memory_Limit_Exceededif the model's memory exceedsoptions.max_memoryorMEMORY_MAX,.Output_Too_Shortif the output doesn't fit,.Canceled, an allocation error, ornil
decode_properties_7z ¶
decode_properties_7z :: proc "contextless" (data: []u8) -> (props: Properties, err: compress.Error) {…}
Reads 7z's PPMd properties
Inputs:
- data The coder properties;
PROPERTIES_7Z_SIZEbytes are read
Returns:
- props var.H properties
- err
.Invalid_Propertiesifdatais short or a value out of range, ornil
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:
- data The entry's data;
PROPERTIES_ZIP_SIZEbytes are read
Returns:
- props var.I properties
- err
.Unsupported_Restore_Methodfor.Freeze,.Invalid_Propertiesifdatais short or a value out of range, ornil
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
outputif it has no allocator yet (default: context.allocator)
Returns:
- err As
decompress_from_context
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
outputis 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.outputif it has no allocator yet (default: context.allocator)
Returns:
- err
.Incompatible_Optionsifz.outputis nil or an option negative,.Invalid_Propertiesor.Unsupported_Restore_Methodfor badprops,.Memory_Limit_Exceededif the model's memory exceedsoptions.max_memoryorMEMORY_MAX,.Output_Too_Shortpast the output cap,.Stream_Too_Short,.Size_Mismatchif the data ends beforeexpected_output_sizebytes or goes on past them,.Corrupt_Data,.Canceled, the input stream's error, an allocation error, ornil
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_SIZEby 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_Optionsif an option is negative oroptions.bufferempty but not nil,.Memory_Limit_Exceededif the model's memory and chunk buffer exceedoptions.max_memory,w's error, else asreader_initanddecompress_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_Propertiesifpropsisn't var.H or is out of range, ornil
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_Methodfor.Freeze,.Invalid_Propertiesifpropsisn't var.I or is out of range, ornil
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_Optionsif an option is negative,.Output_Too_Shortifexpected_output_sizeexceedsoptions.max_output_size,.Invalid_Propertiesor.Unsupported_Restore_Methodfor badprops,.Memory_Limit_Exceededif the model's memory exceedsoptions.max_memoryorMEMORY_MAX,.Stream_Too_Shortor.Corrupt_Datafor a bad stream start, the input stream's error, an allocation error, ornil
reader_read ¶
Decompresses into a buffer
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 returned, ornil
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 ¶
Ends the stream, writing the end marker if asked for and flushing the buffer to w
Inputs:
- pw The writer;
wis left open, and later calls do nothing
Returns:
- err An earlier call's error,
w's error,.Canceled, ornil
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_SIZEby default), and reports progress (default: {}) - allocator For the model and the buffer, freed by
writer_destroy(default: context.allocator)
Returns:
- err
.Incompatible_Optionsif an option is negative oroptions.bufferempty but not nil,.Invalid_Propertiesor.Unsupported_Restore_Methodfor badprops,.Memory_Limit_Exceededif the model's memory and buffer exceedoptions.max_memory, or the model'sMEMORY_MAX, an allocation error, ornil
writer_write ¶
Compresses more data
Inputs:
- pw The writer
- p The data
Returns:
- n The bytes taken from
p - err An earlier call's error,
io.Error.Closedafterwriter_finish,w's error,.Canceled, ornil
Procedure Groups
compress ¶
compress :: proc{ compress_to_slice, compress_to_dynamic, }
Compresses a byte slice into a slice or a dynamic array
decompress ¶
decompress :: proc{ decompress_from_bytes, decompress_from_context, }
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