- ddn.wrp.lzma — High-level D wrapper API
- XZ Utils Homepage
ddn.lib.lzma
Low-level D bindings for liblzma — the XZ Utils compression library.
Overview:
This module provides comprehensive D bindings to liblzma (version 5.x),
a general-purpose data compression library with a zlib-like API. The native file format is .xz, but the legacy .lzma format and raw streams are also supported. LZMA2 is the primary filter.
The bindings cover the stable public API from lzma.h and its
subheaders: version.h, base.h, vli.h, check.h, filter.h, lzma12.h, container.h, stream_flags.h, and hardware.h.
Main_Features:
- Single-call and streaming
.xzStream encoding and decoding - Legacy
.lzma(alone) encoder and decoder - Auto-detecting decoder (
.xz,.lzma,.lz) - Multithreaded encoder (
lzma_mt) and decoder - Integrity checks: CRC32, CRC64, SHA-256, or none
- Filter chain support (
lzma_filter, LZMA1/LZMA2 options) - Memory usage estimation and limits
- Hardware queries: physical memory and CPU thread count
Quick_Start:
Compressing and decompressing a buffer with the C API:
import ddn.lib.lzma;
ubyte[] data = cast(ubyte[])"Hello, LZMA!";
// Compress with the single-call buffer encoder.
size_t bound = lzma_stream_buffer_bound(data.length);
ubyte[] compressed = new ubyte[bound];
size_t outPos = 0;
lzma_filter[2] filters = [
lzma_filter(LZMA_FILTER_LZMA2, null),
lzma_filter(LZMA_VLI_UNKNOWN, null)
];
lzma_options_lzma opts;
lzma_lzma_preset(&opts, LZMA_PRESET_DEFAULT);
filters[0].options = &opts;
lzma_stream_buffer_encode(filters.ptr, LZMA_CHECK_CRC64, null,
data.ptr, data.length, compressed.ptr, &outPos, compressed.length);
// Decompress with the single-call buffer decoder.
ubyte[] restored = new ubyte[data.length];
size_t inPos = 0;
size_t restoredPos = 0;
ulong memlimit = ulong.max;
lzma_stream_buffer_decode(&memlimit, 0, null,
compressed.ptr, &inPos, outPos,
restored.ptr, &restoredPos, restored.length);See Also
License
Copyright
Types 19
Boolean type used by liblzma (C89 compatible unsigned char).
Reserved enumeration type used as ABI padding inside liblzma structs.
Return values used by most functions in liblzma.
Member names are kept identical to the C enum to match the upstream documentation and error strings.
The action argument for lzma_code().
Member names match the C enum.
Variable-length integer type used by liblzma.
Type of the integrity check (Check ID) stored in an .xz stream.
Member names match the C enum.
Custom allocation function signature used by lzma_allocator.
Custom free function signature used by lzma_allocator.
Custom memory allocator hooks.
Pass null for both functions to use the default malloc/free.
lzma_alloc_function allocCustom allocation function, or `null` for default `malloc`.lzma_free_function freeCustom free function, or `null` for default `free`.void * opaqueOpaque pointer passed as the first argument to the hooks.Opaque internal state used by liblzma. Contents are not part of the ABI.
Alias matching the C lzma_internal typedef.
Passing data to and from liblzma.
This is the central streaming structure used by lzma_easy_encoder, lzma_stream_decoder, lzma_code, and friends. It is initialised with LZMA_STREAM_INIT before being passed to any liblzma function.
const(uint8_t) * next_inPointer to the next input byte.size_t avail_inNumber of available input bytes in `next_in`.uint64_t total_inTotal number of input bytes read by liblzma so far.uint8_t * next_outPointer to the next output position.size_t avail_outAmount of free space in `next_out`.uint64_t total_outTotal number of output bytes written by liblzma so far.const(lzma_allocator) * allocatorOptional custom allocator (use `null` for default).lzma_internal * internalInternal state, opaque to applications.void * reserved_ptr1Reserved pointer slot (do not use).void * reserved_ptr2Reserved pointer slot (do not use).void * reserved_ptr3Reserved pointer slot (do not use).void * reserved_ptr4Reserved pointer slot (do not use).uint64_t seek_posNew seek position when `lzma_code()` returns `LZMA_SEEK_NEEDED`.uint64_t reserved_int2Reserved integer slot (do not use).size_t reserved_int3Reserved integer slot (do not use).size_t reserved_int4Reserved integer slot (do not use).lzma_reserved_enum reserved_enum1Reserved enum slot (do not use).lzma_reserved_enum reserved_enum2Reserved enum slot (do not use).A single filter descriptor: an ID plus a pointer to its options.
Terminate filter arrays with lzma_filter(LZMA_VLI_UNKNOWN, null).
lzma_vli idFilter ID (one of the `LZMA_FILTER_*` constants), or `LZMA_VLI_UNKNOWN`.void * optionsPointer to filter-specific options, or `null`.Match finder IDs.
Member names match the C enum.
Compression mode IDs.
Member names match the C enum.
Options shared by the LZMA1 and LZMA2 filters.
Use lzma_lzma_preset() to populate this struct from a compression preset before initialising an encoder.
uint32_t dict_sizeDictionary size in bytes.const(uint8_t) * preset_dictOptional preset dictionary pointer (raw encode/decode only).uint32_t preset_dict_sizeSize in bytes of `preset_dict`.uint32_t lcNumber of literal context bits.uint32_t lpNumber of literal position bits.uint32_t pbNumber of position bits.lzma_mode modeCompression mode (`LZMA_MODE_FAST` or `LZMA_MODE_NORMAL`).uint32_t nice_lenNice length of a match.lzma_match_finder mfMatch finder ID.uint32_t depthMaximum search depth in the match finder.uint32_t ext_flagsExtended flags for LZMA1EXT.uint32_t ext_size_lowLow 32 bits of the extended uncompressed size (LZMA1EXT).uint32_t ext_size_highHigh 32 bits of the extended uncompressed size (LZMA1EXT).uint32_t reserved_int4uint32_t reserved_int5uint32_t reserved_int6uint32_t reserved_int7uint32_t reserved_int8lzma_reserved_enum reserved_enum1lzma_reserved_enum reserved_enum2lzma_reserved_enum reserved_enum3lzma_reserved_enum reserved_enum4void * reserved_ptr1void * reserved_ptr2Options for encoding/decoding the .xz Stream Header and Footer.
uint32_t version_Stream Flags format version (must be 0).lzma_vli backward_sizeBackward Size, in bytes, of the Index (multiple of 4). Footer only.lzma_check checkIntegrity Check ID stored in the header.uint32_t reserved_enum1uint32_t reserved_enum2uint32_t reserved_int1uint32_t reserved_int2Multithreaded compression and decompression options.
Used by lzma_stream_encoder_mt() and lzma_stream_decoder_mt().
uint32_t flagsBitwise-or of zero or more decoder flags.uint32_t threadsNumber of worker threads to use.uint64_t block_sizeEncoder: maximum uncompressed size of a Block.uint32_t timeoutTimeout in milliseconds (0 disables the timeout).uint32_t presetEncoder: compression preset (ignored if `filters` is non-null).const(lzma_filter) * filtersEncoder: filter chain (alternative to `preset`).lzma_check checkEncoder: integrity check type.lzma_reserved_enum reserved_enum1lzma_reserved_enum reserved_enum2lzma_reserved_enum reserved_enum3uint32_t reserved_int1uint32_t reserved_int2uint32_t reserved_int3uint32_t reserved_int4uint64_t memlimit_threadingDecoder: memory limit used to choose the number of threads.uint64_t memlimit_stopDecoder: hard memory usage limit.uint64_t reserved_int7uint64_t reserved_int8void * reserved_ptr1void * reserved_ptr2void * reserved_ptr3void * reserved_ptr4Exception thrown by helper wrappers when a liblzma function fails.
It carries the original lzma_ret code for programmatic inspection.
lzma_ret codeThe underlying liblzma return code.this(string msg, lzma_ret code, string file = __FILE__, size_t line = __LINE__,
Throwable next = null)Functions 73
lzma_vli LZMA_VLI_C(T : ulong)(T n) pure @safe nothrow @nogcMacro-equivalent helper for constructing VLI literals at run time.uint32_t lzma_version_number()const(char) * lzma_version_string()lzma_ret lzma_code(lzma_stream * strm, lzma_action action)void lzma_end(lzma_stream * strm)void lzma_get_progress(const(lzma_stream) * strm, uint64_t * progress_in,
uint64_t * progress_out)uint64_t lzma_memusage(const(lzma_stream) * strm)uint64_t lzma_memlimit_get(const(lzma_stream) * strm)lzma_ret lzma_memlimit_set(lzma_stream * strm, uint64_t memlimit)lzma_ret lzma_vli_encode(lzma_vli vli, size_t * vli_pos, uint8_t * out_,
size_t * out_pos, size_t out_size)lzma_ret lzma_vli_decode(lzma_vli * vli, size_t * vli_pos, const(uint8_t) * in_,
size_t * in_pos, size_t in_size)uint32_t lzma_vli_size(lzma_vli vli)lzma_bool lzma_check_is_supported(lzma_check check)uint32_t lzma_check_size(lzma_check check)uint32_t lzma_crc32(const(uint8_t) * buf, size_t size, uint32_t crc)uint64_t lzma_crc64(const(uint8_t) * buf, size_t size, uint64_t crc)lzma_check lzma_get_check(const(lzma_stream) * strm)uint64_t lzma_physmem()uint32_t lzma_cputhreads()lzma_bool lzma_filter_encoder_is_supported(lzma_vli id)lzma_bool lzma_filter_decoder_is_supported(lzma_vli id)lzma_ret lzma_filters_copy(const(lzma_filter) * filters, lzma_filter * dest,
const(lzma_allocator) * allocator)void lzma_filters_free(lzma_filter * filters, const(lzma_allocator) * allocator)uint64_t lzma_raw_encoder_memusage(const(lzma_filter) * filters)uint64_t lzma_raw_decoder_memusage(const(lzma_filter) * filters)lzma_ret lzma_raw_encoder(lzma_stream * strm, const(lzma_filter) * filters)lzma_ret lzma_raw_decoder(lzma_stream * strm, const(lzma_filter) * filters)lzma_ret lzma_filters_update(lzma_stream * strm, const(lzma_filter) * filters)lzma_ret lzma_raw_buffer_encode(const(lzma_filter) * filters,
const(lzma_allocator) * allocator, const(uint8_t) * in_, size_t in_size,
uint8_t * out_, size_t * out_pos, size_t out_size)lzma_ret lzma_raw_buffer_decode(const(lzma_filter) * filters,
const(lzma_allocator) * allocator, const(uint8_t) * in_, size_t * in_pos,
size_t in_size, uint8_t * out_, size_t * out_pos, size_t out_size)lzma_ret lzma_properties_size(uint32_t * size, const(lzma_filter) * filter)lzma_ret lzma_properties_encode(const(lzma_filter) * filter, uint8_t * props)lzma_ret lzma_properties_decode(lzma_filter * filter, const(lzma_allocator) * allocator,
const(uint8_t) * props, size_t props_size)lzma_ret lzma_filter_flags_size(uint32_t * size, const(lzma_filter) * filter)lzma_ret lzma_filter_flags_encode(const(lzma_filter) * filter, uint8_t * out_,
size_t * out_pos, size_t out_size)lzma_ret lzma_filter_flags_decode(lzma_filter * filter,
const(lzma_allocator) * allocator, const(uint8_t) * in_, size_t * in_pos,
size_t in_size)lzma_bool lzma_lzma_preset(lzma_options_lzma * options, uint32_t preset)lzma_bool lzma_mf_is_supported(lzma_match_finder match_finder)lzma_bool lzma_mode_is_supported(lzma_mode mode)uint64_t lzma_easy_encoder_memusage(uint32_t preset)uint64_t lzma_easy_decoder_memusage(uint32_t preset)lzma_ret lzma_easy_encoder(lzma_stream * strm, uint32_t preset, lzma_check check)lzma_ret lzma_easy_buffer_encode(uint32_t preset, lzma_check check,
const(lzma_allocator) * allocator, const(uint8_t) * in_, size_t in_size,
uint8_t * out_, size_t * out_pos, size_t out_size)lzma_ret lzma_stream_encoder(lzma_stream * strm, const(lzma_filter) * filters,
lzma_check check)uint64_t lzma_stream_encoder_mt_memusage(const(lzma_mt) * options)lzma_ret lzma_stream_encoder_mt(lzma_stream * strm, const(lzma_mt) * options)uint64_t lzma_mt_block_size(const(lzma_filter) * filters)lzma_ret lzma_alone_encoder(lzma_stream * strm, const(lzma_options_lzma) * options)size_t lzma_stream_buffer_bound(size_t uncompressed_size)lzma_ret lzma_stream_buffer_encode(lzma_filter * filters, lzma_check check,
const(lzma_allocator) * allocator, const(uint8_t) * in_, size_t in_size,
uint8_t * out_, size_t * out_pos, size_t out_size)lzma_ret lzma_microlzma_encoder(lzma_stream * strm, const(lzma_options_lzma) * options)lzma_ret lzma_stream_decoder(lzma_stream * strm, uint64_t memlimit, uint32_t flags)lzma_ret lzma_stream_decoder_mt(lzma_stream * strm, const(lzma_mt) * options)lzma_ret lzma_auto_decoder(lzma_stream * strm, uint64_t memlimit, uint32_t flags)lzma_ret lzma_alone_decoder(lzma_stream * strm, uint64_t memlimit)lzma_ret lzma_lzip_decoder(lzma_stream * strm, uint64_t memlimit, uint32_t flags)lzma_ret lzma_stream_buffer_decode(uint64_t * memlimit, uint32_t flags,
const(lzma_allocator) * allocator, const(uint8_t) * in_, size_t * in_pos,
size_t in_size, uint8_t * out_, size_t * out_pos, size_t out_size)lzma_ret lzma_microlzma_decoder(lzma_stream * strm, uint64_t comp_size,
uint64_t uncomp_size, lzma_bool uncomp_size_is_exact, uint32_t dict_size)lzma_ret lzma_stream_header_encode(const(lzma_stream_flags) * options,
uint8_t * out_)lzma_ret lzma_stream_footer_encode(const(lzma_stream_flags) * options,
uint8_t * out_)lzma_ret lzma_stream_header_decode(lzma_stream_flags * options,
const(uint8_t) * in_)lzma_ret lzma_stream_footer_decode(lzma_stream_flags * options,
const(uint8_t) * in_)lzma_ret lzma_stream_flags_compare(const(lzma_stream_flags) * a,
const(lzma_stream_flags) * b)bool isLzmaError(lzma_ret code) @safe @nogc nothrowReturns `true` if the given `lzma_ret` value represents an error.string lzmaErrorName(lzma_ret code) @safe @nogc nothrowReturns a short human-readable description for a `lzma_ret` value.uint lzmaVersionNumber() @safe @nogc nothrowReturns the liblzma version number as a packed integer.bool lzmaCheckIsSupported(lzma_check check) @safe @nogc nothrowReturns `true` if the given Check ID is supported by this liblzma build.uint lzmaCheckSize(lzma_check check) @safe @nogc nothrowReturns the size in bytes of the Check field for the given Check ID.ulong lzmaPhysmem() @safe @nogc nothrowReturns the amount of physical memory reported by the operating system.uint lzmaCputhreads() @safe @nogc nothrowReturns the number of logical CPU threads reported by the operating system.lzma_options_lzma makeLzmaOptions(uint preset = LZMA_PRESET_DEFAULT)Convenience wrapper that initialises `lzma_options_lzma` from a preset.size_t lzmaStreamBufferBound(size_t uncompressedSize) @safe @nogc nothrowReturns an upper bound on the compressed size of `uncompressedSize` bytes when encoded in a single call with `lzma_stream_buffer_encode`.Variables 42
LZMA_VERSION_MAJOR = 5Compile-time liblzma major version against which these bindings were written.
LZMA_VERSION_MINOR = 8Compile-time liblzma minor version against which these bindings were written.
LZMA_VERSION_PATCH = 1Compile-time liblzma patch version against which these bindings were written.
LZMA_VERSION = (LZMA_VERSION_MAJOR * 1_000_000U + LZMA_VERSION_MINOR * 10_000U
+ LZMA_VERSION_PATCH * 10U + LZMA_VERSION_STABILITY_STABLE)Compile-time liblzma version as a packed integer of the form xYYYZZZS (major, minor, patch, stability).
LZMA_VLI_MAX = (ulong.max / 2)Maximum value of a lzma_vli.
LZMA_VLI_UNKNOWN = ulong.maxSpecial VLI value meaning "unknown".
LZMA_VLI_BYTES_MAX = 9Maximum number of bytes a single encoded VLI can occupy.
LZMA_CHECK_ID_MAX = 15Maximum valid Check ID (the .xz format reserves IDs 0..15).
LZMA_CHECK_SIZE_MAX = 64Maximum size in bytes of a Check field.
LZMA_STREAM_INIT = lzma_stream(
null, 0, 0,
null, 0, 0,
null,
null,
null, null, null, null,
0, 0, 0, 0,
lzma_reserved_enum.LZMA_RESERVED_ENUM, lzma_reserved_enum.LZMA_RESERVED_ENUM)Initialiser for lzma_stream.
Use it like:
lzma_stream strm = LZMA_STREAM_INIT;LZMA_FILTERS_MAX = 4Maximum number of filters in a single filter chain.
LZMA_FILTER_LZMA1 = cast(lzma_vli) 0x4000000000000001ULLZMA1 filter ID.
LZMA_FILTER_LZMA1EXT = cast(lzma_vli) 0x4000000000000002ULLZMA1 extended filter ID.
LZMA_FILTER_LZMA2 = cast(lzma_vli) 0x21ULLZMA2 filter ID (the primary modern filter).
LZMA_FILTER_DELTA = cast(lzma_vli) 0x03ULDelta filter ID.
LZMA_FILTER_X86 = cast(lzma_vli) 0x04ULBCJ for x86 (32-bit).
LZMA_FILTER_POWERPC = cast(lzma_vli) 0x05ULBCJ for PowerPC.
LZMA_FILTER_IA64 = cast(lzma_vli) 0x06ULBCJ for IA-64.
LZMA_FILTER_ARM = cast(lzma_vli) 0x07ULBCJ for ARM.
LZMA_FILTER_ARMTHUMB = cast(lzma_vli) 0x08ULBCJ for ARM-Thumb.
LZMA_FILTER_SPARC = cast(lzma_vli) 0x09ULBCJ for SPARC.
LZMA_FILTER_ARM64 = cast(lzma_vli) 0x0AULBCJ for ARM64.
LZMA_DICT_SIZE_MIN = 4096Minimum dictionary size in bytes.
LZMA_DICT_SIZE_DEFAULT = (1U << 23)Default dictionary size in bytes (8 MiB).
LZMA_LCLP_MIN = 0Minimum allowed value of lc and lp.
LZMA_LCLP_MAX = 4Maximum allowed value of lc + lp.
LZMA_LC_DEFAULT = 3Default literal context bits.
LZMA_LP_DEFAULT = 0Default literal position bits.
LZMA_PB_MIN = 0Minimum position bits.
LZMA_PB_MAX = 4Maximum position bits.
LZMA_PB_DEFAULT = 2Default position bits.
LZMA_LZMA1EXT_ALLOW_EOPM = 0x01UFlag enabling the end-of-payload marker with LZMA1EXT.
LZMA_PRESET_DEFAULT = 6Default compression preset (level 6).
LZMA_PRESET_LEVEL_MASK = 0x1FUMask extracting the level (0..9) from a preset value.
LZMA_PRESET_EXTREME = (1U << 31)Flag modifying a preset for extreme (slower, slightly tighter) compression.
LZMA_TELL_NO_CHECK = 0x01UDecoder flag: report streams with no integrity check via LZMA_NO_CHECK.
LZMA_TELL_UNSUPPORTED_CHECK = 0x02UDecoder flag: report streams whose check type is not supported by this build.
LZMA_TELL_ANY_CHECK = 0x04UDecoder flag: report the integrity check type via LZMA_GET_CHECK.
LZMA_CONCATENATED = 0x08UDecoder flag: accept concatenated streams.
LZMA_IGNORE_CHECK = 0x10UDecoder flag: skip integrity check verification (faster, weaker).
LZMA_FAIL_FAST = 0x20UDecoder flag: report errors as soon as they are detected.
LZMA_STREAM_HEADER_SIZE = 12Fixed size in bytes of the .xz Stream Header and Stream Footer.