ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
Reusable Context API

Opaque, reusable compression / decompression contexts. More...

Topics

 Static Context API
 Caller-allocated, fixed-footprint compression / decompression contexts.

Functions

zxc_cctx * zxc_create_cctx (void)
 Creates a reusable compression context, every parameter at its default.
void zxc_free_cctx (zxc_cctx *cctx)
 Frees a compression context and its resources.
int zxc_cctx_set_param (zxc_cctx *cctx, zxc_cparam_t param, int64_t value)
 Sets one parameter on cctx; 0 restores its default.
int zxc_cctx_get_param (const zxc_cctx *cctx, zxc_cparam_t param, int64_t *value)
 Reads one parameter of cctx.
int zxc_cctx_set_dict (zxc_cctx *cctx, const void *dict, size_t dict_size, const void *dict_huf)
 Attaches a pre-trained dictionary to cctx; a NULL dict detaches.
int zxc_cctx_set_progress (zxc_cctx *cctx, zxc_progress_callback_t cb, void *user_data)
 Sets the progress callback; NULL cb disables it.
int zxc_cctx_reset (zxc_cctx *cctx)
 Restores every parameter to its default, detaches the dictionary and the callback.
int64_t zxc_compress_cctx (zxc_cctx *cctx, const void *src, size_t src_size, void *dst, size_t dst_capacity)
 Compresses data using a reusable context.
zxc_dctx * zxc_create_dctx (void)
 Creates a reusable decompression context.
void zxc_free_dctx (zxc_dctx *dctx)
 Frees a decompression context and its resources.
int zxc_dctx_set_param (zxc_dctx *dctx, zxc_dparam_t param, int64_t value)
 Sets one parameter on dctx; 0 restores its default.
int zxc_dctx_get_param (const zxc_dctx *dctx, zxc_dparam_t param, int64_t *value)
 Reads one parameter of dctx.
int zxc_dctx_set_dict (zxc_dctx *dctx, const void *dict, size_t dict_size, const void *dict_huf)
 Attaches the dictionary the archives were compressed with; NULL detaches.
int zxc_dctx_set_progress (zxc_dctx *dctx, zxc_progress_callback_t cb, void *user_data)
 Sets the progress callback; NULL cb disables it.
int zxc_dctx_reset (zxc_dctx *dctx)
 Restores every parameter to its default, detaches the dictionary and the callback.
int64_t zxc_decompress_dctx (zxc_dctx *dctx, const void *src, size_t src_size, void *dst, size_t dst_capacity)
 Decompresses data using a reusable context.
int64_t zxc_decompress_inplace_dctx (zxc_dctx *dctx, void *buffer, const size_t buffer_capacity, const size_t comp_size)
 In-place decompression with a caller-supplied context.

Detailed Description

Opaque, reusable compression / decompression contexts.

Keeping a context across calls removes the per-call allocation overhead. The internal layout stays hidden behind an opaque pointer.

Function Documentation

◆ zxc_cctx_get_param()

int zxc_cctx_get_param ( const zxc_cctx * cctx,
zxc_cparam_t param,
int64_t * value )

Reads one parameter of cctx.

Parameters
[in]cctxContext.
[in]paramParameter identifier.
[out]valueReceives the value in force.
Returns
ZXC_OK, ZXC_ERROR_BAD_PARAM for an unknown param, or ZXC_ERROR_NULL_INPUT.

Definition at line 1207 of file zxc_dispatch.c.

References ZXC_C_BLOCK_SIZE, ZXC_C_CHECKSUM, ZXC_C_LEVEL, ZXC_C_SEEKABLE, ZXC_C_THREADS, ZXC_ERROR_BAD_PARAM, ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_cctx_reset()

int zxc_cctx_reset ( zxc_cctx * cctx)

Restores every parameter to its default, detaches the dictionary and the callback.

The working buffers are kept.

On a static context the pinned block size and level are the defaults.

Parameters
[in,out]cctxContext to reset.
Returns
ZXC_OK, or ZXC_ERROR_NULL_INPUT.

Definition at line 1256 of file zxc_dispatch.c.

References zxc_cparams_defaults(), zxc_dict_copy_set(), ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_cctx_set_dict()

int zxc_cctx_set_dict ( zxc_cctx * cctx,
const void * dict,
size_t dict_size,
const void * dict_huf )

Attaches a pre-trained dictionary to cctx; a NULL dict detaches.

The context keeps its own copy of dict and dict_huf: the caller's buffers are free once this returns. dict_huf, when non-NULL, is the shared literal table of ZXC_HUF_TABLE_SIZE bytes (zxc_dict_huf()), used at levels 6-7 and part of the archive's dict_id binding.

Parameters
[in,out]cctxContext to configure.
[in]dictDictionary content, or NULL for none.
[in]dict_sizeSize of dict in bytes, at most ZXC_DICT_SIZE_MAX.
[in]dict_hufShared literal table, or NULL.
Returns
ZXC_OK; ZXC_ERROR_DICT_TOO_LARGE above the maximum; ZXC_ERROR_DICT_UNSUPPORTED on a static context, which has no room for the dictionary prefix, or when the library was built without dictionary support; ZXC_ERROR_MEMORY when the copy cannot be allocated. A refused call leaves the attached dictionary.

Definition at line 1232 of file zxc_dispatch.c.

References zxc_dict_copy_set(), ZXC_ERROR_DICT_UNSUPPORTED, ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_cctx_set_param()

int zxc_cctx_set_param ( zxc_cctx * cctx,
const zxc_cparam_t param,
const int64_t value )

Sets one parameter on cctx; 0 restores its default.

Takes effect on the next call. A value outside zxc_cparam_bounds(), or an unknown param, is refused and the parameter keeps its value (ZXC_ERROR_BAD_LEVEL, ZXC_ERROR_BAD_BLOCK_SIZE or ZXC_ERROR_BAD_PARAM). On a static context (zxc_init_static_cctx()) the block size is pinned, a level raise into ZXC_LEVEL_DENSITY is refused when the workspace was carved below it, and 0 restores the pinned block size and level.

Parameters
[in,out]cctxContext to configure.
[in]paramParameter identifier.
[in]valueNew value, or 0 for the default.
Returns
ZXC_OK, or a negative zxc_error_t.

Sets one parameter on cctx; 0 restores its default.

Public API; see zxc_buffer.h. Zero restores the default; any other value must sit within zxc_cparam_bounds(). A static context refuses what its workspace cannot hold, so the error comes here, not on the next call.

Definition at line 1158 of file zxc_dispatch.c.

References zxc_cctx_t::opt_scratch, ZXC_BLOCK_SIZE_DEFAULT, ZXC_C_BLOCK_SIZE, ZXC_C_CHECKSUM, ZXC_C_LEVEL, ZXC_C_SEEKABLE, ZXC_C_THREADS, zxc_cparam_bounds(), ZXC_ERROR_BAD_BLOCK_SIZE, ZXC_ERROR_BAD_LEVEL, ZXC_ERROR_BAD_PARAM, ZXC_ERROR_NULL_INPUT, ZXC_LEVEL_DEFAULT, ZXC_LEVEL_DENSITY, ZXC_OK, and zxc_validate_block_size().

◆ zxc_cctx_set_progress()

int zxc_cctx_set_progress ( zxc_cctx * cctx,
zxc_progress_callback_t cb,
void * user_data )

Sets the progress callback; NULL cb disables it.

Honoured by zxc_stream_compress() only: the buffer, block and push APIs are synchronous and never call it.

Parameters
[in,out]cctxContext to configure.
[in]cbCallback, or NULL.
[in]user_dataPointer handed back to cb.
Returns
ZXC_OK, or ZXC_ERROR_NULL_INPUT.

Definition at line 1249 of file zxc_dispatch.c.

References ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_compress_cctx()

int64_t zxc_compress_cctx ( zxc_cctx * cctx,
const void * src,
const size_t src_size,
void * dst,
const size_t dst_capacity )

Compresses data using a reusable context.

Like zxc_compress(), but with the context's parameters (level, block size, checksum, seek table, dictionary) and its buffers, which are re-carved only when the block size or the dictionary changes, or when the level rises into ZXC_LEVEL_DENSITY for the first time. A static (caller-workspace) context cannot grow: such a raise returns ZXC_ERROR_BAD_LEVEL there. ZXC_C_SEEKABLE appends the seek table without allocating, static contexts included.

Parameters
[in,out]cctxConfigured compression context.
[in]srcSource data; may be NULL when src_size is 0.
[in]src_sizeSource size in bytes; 0 writes the empty archive, as zxc_compress() does, without carving the encoder workspace.
[out]dstDestination buffer.
[in]dst_capacityCapacity of dst.
Note
src and dst must not overlap (same contract as memcpy).
Returns
Compressed size (> 0), or a negative zxc_error_t.

Public API; full contract in zxc_buffer.h. Resolves the context's parameters and runs the frame walk.

Definition at line 1410 of file zxc_dispatch.c.

References zxc_compress_cctx_resolved(), zxc_cparams_resolve(), and ZXC_ERROR_NULL_INPUT.

◆ zxc_create_cctx()

zxc_cctx * zxc_create_cctx ( void )

Creates a reusable compression context, every parameter at its default.

Only the handle is allocated here; the working buffers come on the first call, sized from the parameters then in force. Configure it with zxc_cctx_set_param(), zxc_cctx_set_dict() and zxc_cctx_set_progress(); the settings stay until changed or zxc_cctx_reset(). Free with zxc_free_cctx().

Returns
New context, or NULL on allocation failure.

Definition at line 1113 of file zxc_dispatch.c.

References ZXC_CALLOC, zxc_cparams_defaults(), and ZXC_LEVEL_DEFAULT.

Referenced by zxc_cstream_create().

◆ zxc_create_dctx()

zxc_dctx * zxc_create_dctx ( void )

Creates a reusable decompression context.

Only the handle is allocated here: the working buffers are sized from the archive header, so they come on the first decode. Configure it with zxc_dctx_set_param(), zxc_dctx_set_dict() and zxc_dctx_set_progress(). Free with zxc_free_dctx().

Returns
New context, or NULL on allocation failure.

Definition at line 1441 of file zxc_dispatch.c.

References ZXC_CALLOC.

◆ zxc_dctx_get_param()

int zxc_dctx_get_param ( const zxc_dctx * dctx,
zxc_dparam_t param,
int64_t * value )

Reads one parameter of dctx.

See also
zxc_cctx_get_param

Definition at line 1486 of file zxc_dispatch.c.

References ZXC_D_CHECKSUM, ZXC_D_THREADS, ZXC_ERROR_BAD_PARAM, ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_dctx_reset()

int zxc_dctx_reset ( zxc_dctx * dctx)

Restores every parameter to its default, detaches the dictionary and the callback.

The working buffers are kept.

Parameters
[in,out]dctxContext to reset.
Returns
ZXC_OK, or ZXC_ERROR_NULL_INPUT.

Definition at line 1524 of file zxc_dispatch.c.

References zxc_dict_copy_set(), ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_dctx_set_dict()

int zxc_dctx_set_dict ( zxc_dctx * dctx,
const void * dict,
size_t dict_size,
const void * dict_huf )

Attaches the dictionary the archives were compressed with; NULL detaches.

Copies the bytes, as zxc_cctx_set_dict() does. dict_huf must be the table used at compression time when the archive carries one; the header's dict_id is checked against the pair, ZXC_ERROR_DICT_MISMATCH otherwise.

Parameters
[in,out]dctxContext to configure.
[in]dictDictionary content, or NULL for none.
[in]dict_sizeSize of dict in bytes, at most ZXC_DICT_SIZE_MAX.
[in]dict_hufShared literal table, or NULL.
Returns
ZXC_OK; ZXC_ERROR_DICT_TOO_LARGE above the maximum; ZXC_ERROR_DICT_UNSUPPORTED on a static context, or when the library was built without dictionary support; ZXC_ERROR_MEMORY when the copy cannot be allocated.

Definition at line 1501 of file zxc_dispatch.c.

References zxc_dict_copy_set(), ZXC_ERROR_DICT_UNSUPPORTED, ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_dctx_set_param()

int zxc_dctx_set_param ( zxc_dctx * dctx,
zxc_dparam_t param,
int64_t value )

Sets one parameter on dctx; 0 restores its default.

Takes effect on the next call. A value outside zxc_dparam_bounds() is ZXC_ERROR_BAD_PARAM, as is an unknown param; the parameter keeps its value.

Parameters
[in,out]dctxContext to configure.
[in]paramParameter identifier.
[in]valueNew value, or 0 for the default.
Returns
ZXC_OK, or a negative zxc_error_t.

Definition at line 1467 of file zxc_dispatch.c.

References ZXC_D_CHECKSUM, ZXC_D_THREADS, zxc_dparam_bounds(), ZXC_ERROR_BAD_PARAM, ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_dctx_set_progress()

int zxc_dctx_set_progress ( zxc_dctx * dctx,
zxc_progress_callback_t cb,
void * user_data )

Sets the progress callback; NULL cb disables it.

Honoured by zxc_stream_decompress() only.

Parameters
[in,out]dctxContext to configure.
[in]cbCallback, or NULL.
[in]user_dataPointer handed back to cb.
Returns
ZXC_OK, or ZXC_ERROR_NULL_INPUT.

Definition at line 1517 of file zxc_dispatch.c.

References ZXC_ERROR_NULL_INPUT, and ZXC_OK.

◆ zxc_decompress_dctx()

int64_t zxc_decompress_dctx ( zxc_dctx * dctx,
const void * src,
const size_t src_size,
void * dst,
const size_t dst_capacity )

Decompresses data using a reusable context.

Like zxc_decompress(), dictionary options included, but reuses dctx's buffers; the shared literal table is rebuilt only when it changes between calls. A static context returns ZXC_ERROR_DICT_UNSUPPORTED for any dictionary.

Asking without a destination
Same probe as zxc_decompress(), answered under this context's rules: a static context still rejects a foreign block size and a dictionary-bound archive.
Parameters
[in,out]dctxReusable decompression context.
[in]srcCompressed data.
[in]src_sizeCompressed size in bytes.
[out]dstDestination buffer, or NULL to probe (see above).
[in]dst_capacityCapacity of dst.
Note
src and dst must not overlap (same contract as memcpy).
Returns
Decompressed size, 0 for an empty archive, or a negative zxc_error_t.

Decompresses data using a reusable context.

Public API; full contract in zxc_buffer.h. Same frame walk and dictionary binding as zxc_decompress(); the inner buffers are re-carved only when the block or dictionary size changes, the shared table only when it changes.

Definition at line 1575 of file zxc_dispatch.c.

References zxc_dctx_decode_container(), zxc_dctx_probe(), ZXC_ERROR_NULL_INPUT, ZXC_FRAME_MIN_SIZE, and zxc_short_input_verdict().

◆ zxc_decompress_inplace_dctx()

int64_t zxc_decompress_inplace_dctx ( zxc_dctx * dctx,
void * buffer,
const size_t buffer_capacity,
const size_t comp_size )

In-place decompression with a caller-supplied context.

Same contract and error codes as zxc_decompress_inplace(), with dctx as the decode context. With a static context (zxc_init_static_dctx()) the call allocates nothing.

A static context rejects a foreign block size (ZXC_ERROR_BAD_BLOCK_SIZE) and any dictionary (ZXC_ERROR_DICT_UNSUPPORTED).

static uint8_t ws[...] __attribute__((aligned(64))); // zxc_static_dctx_workspace_size(bs)
zxc_dctx* d = zxc_init_static_dctx(ws, sizeof(ws), bs);
// buf: zxc_decompress_inplace_bound() bytes, archive flush-right
int64_t n = zxc_decompress_inplace_dctx(d, buf, cap, archive_size);
struct zxc_dctx_s zxc_dctx
Opaque reusable decompression context (see zxc_create_dctx).
Definition zxc_buffer.h:372
int64_t zxc_decompress_inplace_dctx(zxc_dctx *dctx, void *buffer, const size_t buffer_capacity, const size_t comp_size)
In-place decompression with a caller-supplied context.
zxc_dctx * zxc_init_static_dctx(void *workspace, const size_t workspace_size, const size_t block_size)
Initialises a decompression context inside a caller-supplied workspace.
Parameters
[in,out]dctxDecompression context (static or heap).
[in,out]bufferWork buffer holding the flush-right archive.
[in]buffer_capacityTotal size of buffer in bytes.
[in]comp_sizeSize of the compressed archive in bytes.
Returns
Decompressed size (> 0), 0 for an empty frame, or a negative zxc_error_t.

In-place decompression with a caller-supplied context.

Public API; full contract in zxc_buffer.h. The archive sits flush-right, at buffer + buffer_capacity - comp_size, and decodes left-to-right into buffer[0..]. Dictionary archives decode through the context's own bounce buffer, which does not alias buffer.

Definition at line 1594 of file zxc_dispatch.c.

References zxc_dctx_decode_frame(), ZXC_ERROR_NULL_INPUT, zxc_inplace_prepare(), and ZXC_OK.

◆ zxc_free_cctx()

void zxc_free_cctx ( zxc_cctx * cctx)

Frees a compression context and its resources.

NULL is a no-op.

Parameters
[in]cctxContext to free.

Frees a compression context and its resources.

Public API; see zxc_buffer.h. Frees the inner buffers and the handle. NULL-safe. For a static (caller-workspace) context this is a no-op, since the caller owns the workspace.

Definition at line 1275 of file zxc_dispatch.c.

References zxc_cctx_free(), and ZXC_FREE.

Referenced by zxc_cstream_create(), and zxc_cstream_free().

◆ zxc_free_dctx()

void zxc_free_dctx ( zxc_dctx * dctx)

Frees a decompression context and its resources.

NULL is a no-op.

Parameters
[in]dctxContext to free.

Frees a decompression context and its resources.

Public API; see zxc_buffer.h. Frees the inner buffers and the handle. NULL-safe; a no-op for a static (caller-workspace) context.

Definition at line 1537 of file zxc_dispatch.c.

References zxc_cctx_free(), and ZXC_FREE.