ddn.wrp.zstd

High-level D wrapper for libzstd — idiomatic compression API.

Overview:

This module provides a high-level, idiomatic D interface for working

with Zstandard compression. It wraps the low-level bindings in ddn.lib.zstd with RAII classes, exception-safe error handling, and convenience functions.

Use this module for most applications. The low-level module is available

for advanced use cases requiring direct control over the C API.

Key_Components:

  • ZstdCompressor — RAII class for compression (reusable context)
  • ZstdDecompressor — RAII class for decompression (reusable context)
  • ZstdCDict / ZstdDDict — RAII structs for dictionary objects
  • ZstdFrameInfo — Frame inspection without decompression
  • ZstdException — Exception class with error code context
  • zstdCompress / zstdDecompress — One-shot convenience functions

Quick_Start:

One-shot compression and decompression:

import ddn.wrp.zstd;

ubyte[] data = cast(ubyte[])"Hello, Zstandard!";
ubyte[] compressed = zstdCompress(data);
ubyte[] original = zstdDecompress(compressed);
assert(original == data);

RAII compressor for repeated operations:

import ddn.wrp.zstd;

auto comp = ZstdCompressor.create(6);
auto decomp = ZstdDecompressor.create();

ubyte[] c = comp.compress(cast(ubyte[])"data");
ubyte[] d = decomp.decompress(c);

Error_Handling:

All operations throw ZstdException on failure:

try {
    auto result = zstdDecompress(garbageData);
} catch (ZstdException e) {
    writeln("Error: ", e.msg, " (code: ", e.errorCode, ")");
}

See Also

License

BSD-3-Clause

Types 6

classZstdException : Exception

Exception thrown by high-level Zstandard operations.

Contains the underlying ZSTD_ErrorCode for programmatic error handling.

Fields
ZSTD_ErrorCode errorCodeThe underlying ZSTD error code.
Constructors
this(ZSTD_ErrorCode ec, string msg, string file = __FILE__, size_t line = __LINE__)

RAII wrapper around a ZSTD_CCtx for compression operations.

Reusing a ZstdCompressor instance across multiple compressions avoids repeated context allocation overhead.

Example:

auto comp = ZstdCompressor.create(6);
ubyte[] c1 = comp.compress(cast(ubyte[])"chunk 1");
ubyte[] c2 = comp.compress(cast(ubyte[])"chunk 2");

Fields
private ZSTD_CCtx _cctx
private bool _closed
Methods
private void close()
ZstdCompressor create(int level = ZSTD_CLEVEL_DEFAULT)Creates a new ZstdCompressor with the specified compression level.
ubyte[] compress(const(ubyte)[] data)Compresses the given data and returns the compressed result.
size_t compressInto(const(ubyte)[] src, ubyte[] dst)Compresses data into the provided output buffer.
void setLevel(int level)Sets the compression level for subsequent compressions.
void setStrategy(ZSTD_strategy strategy)Sets the compression strategy.
void setWindowLog(int windowLog)Sets the window log for subsequent compressions.
void setChecksumFlag(bool enabled)Enables or disables the content checksum in the frame header.
void setPledgedSrcSize(ulong size)Hints at the uncompressed source size for the next frame.
void reset(ZSTD_ResetDirective directive = ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)Resets the compressor context.
void loadDictionary(const(ubyte)[] dict)Loads a dictionary for subsequent compressions.
void refCDict(ZstdCDict cdict)References a ZstdCDict for subsequent compressions.
Constructors
Destructors

RAII wrapper around a ZSTD_DCtx for decompression operations.

Reusing a ZstdDecompressor instance across multiple decompressions avoids repeated context allocation overhead.

Example:

auto decomp = ZstdDecompressor.create();
ubyte[] d1 = decomp.decompress(compressed1);
ubyte[] d2 = decomp.decompress(compressed2);

Fields
private ZSTD_DCtx _dctx
private bool _closed
Methods
private void close()
ZstdDecompressor create()Creates a new ZstdDecompressor.
ubyte[] decompress(const(ubyte)[] data)Decompresses the given data and returns the decompressed result.
size_t decompressInto(const(ubyte)[] src, ubyte[] dst)Decompresses data into the provided output buffer.
void reset(ZSTD_ResetDirective directive = ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)Resets the decompressor context.
void loadDictionary(const(ubyte)[] dict)Loads a dictionary for subsequent decompressions.
void refDDict(ZstdDDict ddict)References a ZstdDDict for subsequent decompressions.
Constructors
Destructors
structZstdCDict

RAII wrapper around a ZSTD_CDict (compression dictionary).

The dictionary content is copied at construction time, so the source buffer can be freed afterwards.

Example:

auto cdict = ZstdCDict(cast(const(ubyte)[])"dictionary content", 6);
scope(exit) cdict.close();
auto comp = ZstdCompressor.create(6);
comp.refCDict(cdict);

Fields
private ZSTD_CDict _dict
Methods
void close()Frees the underlying dictionary.
uint dictID() constReturns the dictionary ID embedded in this dictionary.
ZSTD_CDict handle() constReturns the raw C pointer handle (for advanced use with `ddn.lib.zstd`).
Constructors
this(const(ubyte)[] dict, int level)Creates a compressed dictionary for efficient repeated compression.
Destructors
structZstdDDict

RAII wrapper around a ZSTD_DDict (decompression dictionary).

The dictionary content is copied at construction time, so the source buffer can be freed afterwards.

Example:

auto ddict = ZstdDDict(cast(const(ubyte)[])"dictionary content");
scope(exit) ddict.close();
auto decomp = ZstdDecompressor.create();
decomp.refDDict(ddict);

Fields
private ZSTD_DDict _dict
Methods
void close()Frees the underlying dictionary.
uint dictID() constReturns the dictionary ID embedded in this dictionary.
ZSTD_DDict handle() constReturns the raw C pointer handle (for advanced use with `ddn.lib.zstd`).
Constructors
this(const(ubyte)[] dict)Creates a decompression dictionary.
Destructors

Information extracted from a Zstandard frame header without decompression.

Fields
ulong frameContentSizeThe decompressed content size, or ZSTDCONTENTSIZEUNKNOWN if unknown.
uint windowSizeThe window size required for decompression.
uint dictIDThe dictionary ID, or 0 if none.
bool isFrameWhether the data starts with a valid Zstandard frame.
bool contentSizeUnknownWhether the content size is unknown.
Methods
ZstdFrameInfo inspect(const(ubyte)[] data)Inspects a Zstandard frame header and extracts metadata.

Functions 4

fnvoid checkError(size_t result, string context = "")Throws a ZstdException if the result code is a ZSTD error.
fnubyte[] zstdCompress(const(ubyte)[] data, int level = ZSTD_CLEVEL_DEFAULT)One-shot compression of data using Zstandard.
fnubyte[] zstdDecompress(const(ubyte)[] data)One-shot decompression of data using Zstandard.
fnsize_t zstdCompressBound(size_t srcSize) @safeReturns the maximum compressed size for a given source size.