ZXC 0.15.0
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_dispatch.c File Reference

Runtime CPU feature detection and SIMD dispatch layer. More...

Go to the source code of this file.

Macros

#define ZXC_VARIANT_SET(sfx)
 Defines the constant variant set of suffix sfx.
 
#define ZXC_STATIC_CCTX_HDR_SIZE   ZXC_ALIGN_CL(sizeof(struct zxc_cctx_s))
 
#define ZXC_STATIC_DCTX_HDR_SIZE   ZXC_ALIGN_CL(sizeof(struct zxc_dctx_s))
 

Typedefs

typedef int(* zxc_decompress_func_t) (const zxc_cctx_t *, const uint8_t *, const size_t, uint8_t *, const size_t, const uint64_t)
 Function pointer type for the chunk decompressor.
 
typedef int(* zxc_compress_func_t) (zxc_cctx_t *, const uint8_t *, const size_t, uint8_t *, const size_t, const uint64_t)
 Function pointer type for the chunk compressor.
 

Enumerations

enum  zxc_cpu_feature_t {
  ZXC_CPU_GENERIC = 0 , ZXC_CPU_AVX2 = 1 , ZXC_CPU_AVX512 = 2 , ZXC_CPU_NEON = 3 ,
  ZXC_CPU_SSE2 = 4
}
 Detected CPU SIMD capability level. More...
 

Functions

int zxc_decompress_chunk_wrapper_default (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 
int zxc_decompress_chunk_wrapper_dict_default (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 
int zxc_decompress_chunk_wrapper_safe_default (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 
int zxc_compress_chunk_wrapper_default (zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 
int zxc_huf_build_code_lengths_default (const uint32_t *freq, uint8_t *code_len, void *scratch, int max_code_len)
 
size_t zxc_huf_calc_size_default (const uint32_t *freq, const uint8_t *code_len, int with_header)
 
int zxc_huf_encode_section_default (const uint8_t *literals, size_t n_literals, const uint32_t *freq, const uint8_t *code_len, uint8_t *dst, size_t dst_cap)
 
int zxc_huf_decode_section_default (const uint8_t *payload, size_t payload_size, uint8_t *dst, size_t n, uint8_t *scratch)
 
int zxc_huf_encode_section_dict_default (const uint8_t *literals, size_t n_literals, const uint32_t *freq, const uint8_t *code_len, const zxc_pivco_tree_t *tree, const uint32_t *codes, uint8_t *dst, size_t dst_cap)
 
int zxc_huf_decode_section_dict_default (const uint8_t *payload, size_t payload_size, uint8_t *dst, size_t n, const zxc_pivco_tree_t *tree, uint8_t *scratch)
 
size_t zxc_huf_calc_size_dict_default (const uint32_t *freq, const uint8_t *code_len, const zxc_pivco_tree_t *tree)
 
void zxc_huf_pack_lengths_default (const uint8_t *code_len, uint8_t *out)
 
int zxc_huf_unpack_lengths_default (const uint8_t *in, uint8_t *code_len)
 
static zxc_cpu_feature_t zxc_detect_cpu_features (void)
 Probes the running CPU for SIMD support.
 
static const zxc_variant_set_t * zxc_select_variants (void)
 Detects the CPU tier and returns the matching variant set.
 
static const zxc_variant_set_t * zxc_variants_resolve (void)
 First call: selects and publishes.
 
static const zxc_variant_set_t * zxc_variants (void)
 The variant set in use, selected on the first call.
 
static zxc_decompress_func_t zxc_decompress_safe_resolve (void)
 First call: the _safe decoder of the selected set, published.
 
static zxc_decompress_func_t zxc_decompress_safe_variant (void)
 The _safe decoder in use, selected on the first call.
 
int zxc_decompress_chunk_wrapper (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 Public decompression dispatcher.
 
static int zxc_decompress_chunk_wrapper_safe_public (const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 Internal safe-decompression dispatcher (strict dst_capacity == uncompressed_size).
 
int zxc_compress_chunk_wrapper (zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
 Public compression dispatcher.
 
int zxc_huf_build_code_lengths (const uint32_t *freq, uint8_t *code_len, void *scratch, const int max_code_len)
 Build length-limited per-symbol Huffman code lengths from frequencies.
 
size_t zxc_huf_calc_size (const uint32_t *freq, const uint8_t *code_len, const int with_header)
 Un-suffixed forwarders for the PivCo section codec (tests, tools).
 
int zxc_huf_encode_section (const uint8_t *literals, const size_t n_literals, const uint32_t *freq, const uint8_t *code_len, uint8_t *dst, const size_t dst_cap)
 Encode a PivCo literal section (128-byte lengths header + payload).
 
int zxc_huf_decode_section (const uint8_t *payload, const size_t payload_size, uint8_t *dst, const size_t n, uint8_t *scratch)
 Decode a PivCo literal section into dst (exactly n bytes).
 
int zxc_huf_encode_section_dict (const uint8_t *literals, const size_t n_literals, const uint32_t *freq, const uint8_t *code_len, const zxc_pivco_tree_t *tree, const uint32_t *codes, uint8_t *dst, const size_t dst_cap)
 Encode a PivCo section against a prebuilt dict tree/codes (no header).
 
int zxc_huf_decode_section_dict (const uint8_t *payload, const size_t payload_size, uint8_t *dst, const size_t n, const zxc_pivco_tree_t *tree, uint8_t *scratch)
 Decode a PivCo dict section against a prebuilt dict tree.
 
size_t zxc_huf_calc_size_dict (const uint32_t *freq, const uint8_t *code_len, const zxc_pivco_tree_t *tree)
 zxc_huf_calc_size for a dict section: prebuilt tree, no header.
 
void zxc_huf_pack_lengths (const uint8_t *code_len, uint8_t *out)
 Pack per-symbol code lengths into the 128-byte nibble header.
 
int zxc_huf_unpack_lengths (const uint8_t *in, uint8_t *code_len)
 Unpack and validate a 128-byte packed lengths header.
 
static int64_t zxc_write_empty_frame (uint8_t *dst, const size_t dst_capacity, const size_t block_size, const int checksum_enabled, const uint32_t did, const int seekable)
 Writes the whole archive an empty source produces: file header, EOF block, the empty seek table when seekable, then the footer.
 
static int64_t zxc_write_seek_table_from_blocks (uint8_t *dst, const size_t dst_capacity, const uint8_t *blocks, const uint8_t *const blocks_end, const uint64_t num_blocks, const int checksum_enabled)
 Writes the seek table of the num_blocks blocks in [blocks, blocks_end), sizes read back from their headers.
 
int64_t zxc_compress (const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_compress_opts_t *opts)
 Compresses an entire buffer in one call.
 
static int64_t zxc_decompress_oneshot (const uint8_t *src, size_t src_size, uint8_t *dst, size_t dst_capacity, const zxc_decompress_opts_t *opts)
 
static int64_t zxc_dctx_decode_frame (zxc_dctx *dctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity, const zxc_decompress_opts_t *opts, const int inplace, size_t *frame_end)
 The frame walk behind every buffer decode: zxc_decompress(), its no-destination probe, zxc_decompress_dctx() and both in-place entry points.
 
static int64_t zxc_dctx_decode_container (zxc_dctx *dctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity, const zxc_decompress_opts_t *opts)
 The container walk behind zxc_decompress() and zxc_decompress_dctx().
 
static int zxc_buffer_frame_info (const uint8_t *src, const size_t src_size, zxc_frame_info_t *info, size_t *footer_len)
 Validates a frame without decoding it: the check of zxc_read_frame_info() on a whole buffer, header first.
 
static int64_t zxc_probe_without_dst (const uint8_t *src, const size_t src_size, const zxc_decompress_opts_t *opts, const zxc_container_info_t *scanned)
 Answers a decode request that carries no destination.
 
int64_t zxc_decompress (const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_decompress_opts_t *opts)
 Decompresses an entire buffer in one call.
 
static uint64_t zxc_inplace_margin (const uint64_t dsize, const size_t chunk_size, const int has_cs, const int has_seek, const size_t footer_len)
 Bytes an in-place decode needs on top of the decompressed size.
 
static int zxc_inplace_probe (const uint8_t *comp, const size_t comp_size, uint64_t *dsize, uint64_t *margin, uint64_t *floor)
 Shared archive probe for the in-place entry points: validates the magic + file header, then reads the footer's decompressed size and derives the in-place margin.
 
size_t zxc_decompress_inplace_bound (const void *src, const size_t src_size)
 Minimum single-buffer size for a safe in-place decode of src.
 
static int zxc_inplace_prepare (const void *buffer, const size_t buffer_capacity, const size_t comp_size, const zxc_decompress_opts_t *opts, const uint8_t **comp)
 Argument and buffer checks shared by the in-place entry points.
 
int64_t zxc_decompress_inplace (void *buffer, const size_t buffer_capacity, const size_t comp_size, const zxc_decompress_opts_t *opts)
 Decompresses in place, inside a single caller-owned buffer.
 
uint64_t zxc_get_decompressed_size (const void *src, const size_t src_size)
 Reads the decompressed size from a ZXC-compressed buffer.
 
uint32_t zxc_get_dict_id (const void *src, const size_t src_size)
 Reads the dictionary id from a compressed archive's file header.
 
int zxc_get_frame_info (const void *src, const size_t src_size, zxc_frame_info_t *info, const size_t info_size)
 Reads a frame's header and footer, without decoding.
 
int zxc_get_last_frame_info (const void *src, const size_t src_size, zxc_frame_info_t *info, const size_t info_size)
 Reads the frame that ends src, without decoding.
 
static int zxc_ctx_sync_dict_huf (zxc_cctx_t *inner, uint8_t *cache, int *cached, const uint8_t *dict_huf)
 Attaches the shared literal table to inner; NULL detaches.
 
zxc_cctx * zxc_create_cctx (const zxc_compress_opts_t *opts)
 Creates a reusable compression context.
 
void zxc_free_cctx (zxc_cctx *cctx)
 Releases a reusable compression context.
 
int64_t zxc_compress_cctx (zxc_cctx *cctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_compress_opts_t *opts)
 Compresses data using a reusable context.
 
zxc_dctx * zxc_create_dctx (void)
 Creates a reusable decompression context.
 
void zxc_free_dctx (zxc_dctx *dctx)
 Releases a reusable decompression context.
 
static int64_t zxc_dctx_probe (const zxc_dctx *dctx, const uint8_t *src, const size_t src_size, const zxc_decompress_opts_t *opts)
 Answers a no-destination decode on a reusable context.
 
int64_t zxc_decompress_dctx (zxc_dctx *dctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_decompress_opts_t *opts)
 Decompresses a framed archive into dst, reusing dctx.
 
int64_t zxc_decompress_inplace_dctx (zxc_dctx *dctx, void *buffer, const size_t buffer_capacity, const size_t comp_size, const zxc_decompress_opts_t *opts)
 Decompresses in place inside one caller buffer, reusing dctx.
 
static int zxc_dctx_carve (zxc_dctx *dctx, const size_t chunk_size, const size_t dict_size, const int checksum, const uint8_t *dict_huf)
 Readies dctx for a frame: re-carves the inner buffers when the block or dictionary size changed (the block API shares this context), then syncs the checksum flag and the shared literal table.
 
int64_t zxc_compress_block (zxc_cctx *cctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_compress_opts_t *opts)
 Compresses a single block (no file framing), reusing cctx.
 
int64_t zxc_compress_block_at (zxc_cctx *cctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_compress_opts_t *opts, const uint64_t block_index)
 zxc_compress_block, its checksum seeded with block_index (public entry: 0).
 
static int64_t zxc_decode_bounce (zxc_cctx_t *ctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity, const size_t carved)
 Block decode through work_buf, for a dst too tight for the speculative writes, then copied out.
 
static int64_t zxc_static_decompress_block (zxc_dctx *dctx, const uint8_t *src, const size_t src_size, uint8_t *dst, const size_t dst_capacity, const size_t dict_size, const int checksum_enabled, const int strict)
 Block decode on a static dctx: the carved block is the effective capacity.
 
static int zxc_dctx_prepare (zxc_dctx *dctx, const size_t block_size, const size_t dict_size, const int checksum_enabled)
 Recarves a heap dctx when block_size or dict_size changed; otherwise only refreshes the checksum flag.
 
int64_t zxc_decompress_block (zxc_dctx *dctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_decompress_opts_t *opts)
 Decompresses a single block (no file framing), reusing dctx.
 
int64_t zxc_decompress_block_safe (zxc_dctx *dctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_decompress_opts_t *opts)
 Safe-variant block decompressor: accepts dst_capacity == uncompressed_size.
 
size_t zxc_static_cctx_workspace_size (const size_t block_size, const int level)
 Workspace size needed for a static compression context.
 
zxc_cctx * zxc_init_static_cctx (void *workspace, const size_t workspace_size, const zxc_compress_opts_t *opts)
 Initialises a compression context inside a caller-supplied workspace.
 
size_t zxc_static_dctx_workspace_size (const size_t block_size)
 Workspace size needed for a static decompression 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.
 

Variables

static const zxc_variant_set_t zxc_variants_default = {zxc_decompress_chunk_wrapper_default , zxc_decompress_chunk_wrapper_dict_default , zxc_compress_chunk_wrapper_default }
 
static const zxc_variant_set_t *_Atomic zxc_variants_ptr = NULL
 The selection, NULL until the first call.
 
static _Atomic zxc_decompress_func_t zxc_decompress_safe_ptr = (zxc_decompress_func_t)0
 The _safe decoder in use, NULL until the first call.
 

Detailed Description

Runtime CPU feature detection and SIMD dispatch layer.

Detects AVX2/AVX512 (x86-64) and NEON (32-bit ARM) at runtime and routes compress/decompress calls to the best available implementation via lazy-initialised function pointers. SSE2 on x86-64 and NEON on AArch64 are baseline ISA guarantees, so the _default variant already covers those tiers. Also contains the public one-shot buffer API (zxc_compress, zxc_decompress, zxc_get_decompressed_size).

Definition in file zxc_dispatch.c.

Macro Definition Documentation

◆ ZXC_STATIC_CCTX_HDR_SIZE

#define ZXC_STATIC_CCTX_HDR_SIZE   ZXC_ALIGN_CL(sizeof(struct zxc_cctx_s))

Definition at line 1963 of file zxc_dispatch.c.

◆ ZXC_STATIC_DCTX_HDR_SIZE

#define ZXC_STATIC_DCTX_HDR_SIZE   ZXC_ALIGN_CL(sizeof(struct zxc_dctx_s))

Definition at line 1964 of file zxc_dispatch.c.

◆ ZXC_VARIANT_SET

#define ZXC_VARIANT_SET (   sfx)
Value:
static const zxc_variant_set_t zxc_variants##sfx = {zxc_decompress_chunk_wrapper##sfx, \
int zxc_decompress_chunk_wrapper_dict(const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
Public dictionary chunk decoder.
static const zxc_variant_set_t * zxc_variants(void)
The variant set in use, selected on the first call.
int zxc_compress_chunk_wrapper(zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
Public compression dispatcher.
int zxc_decompress_chunk_wrapper(const zxc_cctx_t *ctx, const uint8_t *src, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
Public decompression dispatcher.

Defines the constant variant set of suffix sfx.

Definition at line 317 of file zxc_dispatch.c.

Typedef Documentation

◆ zxc_compress_func_t

typedef int(* zxc_compress_func_t) (zxc_cctx_t *, const uint8_t *, const size_t, uint8_t *, const size_t, const uint64_t)

Function pointer type for the chunk compressor.

Definition at line 197 of file zxc_dispatch.c.

◆ zxc_decompress_func_t

typedef int(* zxc_decompress_func_t) (const zxc_cctx_t *, const uint8_t *, const size_t, uint8_t *, const size_t, const uint64_t)

Function pointer type for the chunk decompressor.

Definition at line 194 of file zxc_dispatch.c.

Enumeration Type Documentation

◆ zxc_cpu_feature_t

Detected CPU SIMD capability level.

Enumerator
ZXC_CPU_GENERIC 

Scalar-only fallback.


ZXC_CPU_AVX2 

x86-64 AVX2 available.


ZXC_CPU_AVX512 

x86-64 AVX-512F+BW available.

ZXC_CPU_NEON 

ARM NEON available (dedicated variant on 32-bit ARM only; AArch64 baseline, served by _default there).

ZXC_CPU_SSE2 

x86 SSE2 available (no AVX2); x86-64 baseline, served by _default (no dedicated variant).

Definition at line 206 of file zxc_dispatch.c.

Function Documentation

◆ zxc_buffer_frame_info()

static int zxc_buffer_frame_info ( const uint8_t *  src,
const size_t  src_size,
zxc_frame_info_t *  info,
size_t *  footer_len 
)
static

Validates a frame without decoding it: the check of zxc_read_frame_info() on a whole buffer, header first.

The footer's compressed size must be the whole input: one frame, starting at offset 0. Each caller adds its own policy on the result.

Parameters
[in]srcThe frame.
[in]src_sizeIts size.
[out]infoFilled on success.
[out]footer_lenFooter bytes, digest included; may be NULL.

Definition at line 806 of file zxc_dispatch.c.

References ZXC_ERROR_NULL_INPUT, zxc_scan_src_mem(), and zxc_scan_whole_frame().

Referenced by zxc_get_frame_info(), and zxc_inplace_probe().

◆ zxc_compress_block_at()

◆ zxc_compress_chunk_wrapper()

int zxc_compress_chunk_wrapper ( zxc_cctx_t *  ctx,
const uint8_t *  src,
const size_t  src_sz,
uint8_t *  dst,
const size_t  dst_cap,
const uint64_t  block_index 
)

Public compression dispatcher.

Compresses one chunk through the runtime ISA dispatch.

Definition at line 465 of file zxc_dispatch.c.

References zxc_variants().

Referenced by zxc_compress(), zxc_compress_block_at(), and zxc_compress_cctx().

◆ zxc_ctx_sync_dict_huf()

static int zxc_ctx_sync_dict_huf ( zxc_cctx_t *  inner,
uint8_t *  cache,
int *  cached,
const uint8_t *  dict_huf 
)
static

Attaches the shared literal table to inner; NULL detaches.

The tree is rebuilt only when the 128 bytes differ from cache, as a build costs about as much as decoding a small block. Clear cached whenever inner is re-initialised: the attached state dies with it.

Definition at line 1128 of file zxc_dispatch.c.

References zxc_cctx_attach_dict_huf(), ZXC_ERROR_CORRUPT_DATA, ZXC_HUF_TABLE_SIZE, and ZXC_OK.

Referenced by zxc_compress_block_at(), zxc_compress_cctx(), zxc_dctx_carve(), and zxc_decompress_block().

◆ zxc_dctx_carve()

static int zxc_dctx_carve ( zxc_dctx *  dctx,
const size_t  chunk_size,
const size_t  dict_size,
const int  checksum,
const uint8_t *  dict_huf 
)
static

Readies dctx for a frame: re-carves the inner buffers when the block or dictionary size changed (the block API shares this context), then syncs the checksum flag and the shared literal table.

Returns
ZXC_OK, or a negative zxc_error_t.

Definition at line 1434 of file zxc_dispatch.c.

References zxc_cctx_free(), zxc_cctx_init(), zxc_ctx_sync_dict_huf(), ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_MEMORY, and ZXC_OK.

Referenced by zxc_dctx_decode_frame().

◆ zxc_dctx_decode_container()

static int64_t zxc_dctx_decode_container ( zxc_dctx *  dctx,
const uint8_t *  src,
size_t  src_size,
uint8_t *  dst,
size_t  dst_capacity,
const zxc_decompress_opts_t *  opts 
)
static

◆ zxc_dctx_decode_frame()

static int64_t zxc_dctx_decode_frame ( zxc_dctx *  dctx,
const uint8_t *  src,
const size_t  src_size,
uint8_t *  dst,
const size_t  dst_capacity,
const zxc_decompress_opts_t *  opts,
const int  inplace,
size_t *  frame_end 
)
static

◆ zxc_dctx_prepare()

static int zxc_dctx_prepare ( zxc_dctx *  dctx,
const size_t  block_size,
const size_t  dict_size,
const int  checksum_enabled 
)
static

Recarves a heap dctx when block_size or dict_size changed; otherwise only refreshes the checksum flag.

Definition at line 1815 of file zxc_dispatch.c.

References zxc_cctx_free(), zxc_cctx_init(), ZXC_ERROR_MEMORY, and ZXC_OK.

Referenced by zxc_decompress_block(), and zxc_decompress_block_safe().

◆ zxc_dctx_probe()

static int64_t zxc_dctx_probe ( const zxc_dctx *  dctx,
const uint8_t *  src,
const size_t  src_size,
const zxc_decompress_opts_t *  opts 
)
static

Answers a no-destination decode on a reusable context.

This context's own rules come first: one that could never decode the archive has to say why, not "give me a buffer". The answer then comes from the shared probe, which walks a private workspace, so a read-only query leaves the buffers this context has carved untouched.

Definition at line 1377 of file zxc_dispatch.c.

References zxc_container_info_t::uses_dict, ZXC_ERROR_DICT_UNSUPPORTED, ZXC_OK, ZXC_OPTS_DICT_SIZE, zxc_probe_without_dst(), zxc_scan_container(), and zxc_scan_src_mem().

Referenced by zxc_decompress_dctx().

◆ zxc_decode_bounce()

static int64_t zxc_decode_bounce ( zxc_cctx_t *  ctx,
const uint8_t *  src,
const size_t  src_size,
uint8_t *  dst,
const size_t  dst_capacity,
const size_t  carved 
)
static

Block decode through work_buf, for a dst too tight for the speculative writes, then copied out.

carved bounds the decoded size on a static context; 0 leaves it unbounded.

Definition at line 1766 of file zxc_dispatch.c.

References zxc_decompress_chunk_wrapper(), ZXC_ERROR_BAD_BLOCK_SIZE, and ZXC_ERROR_DST_TOO_SMALL.

Referenced by zxc_decompress_block(), and zxc_static_decompress_block().

◆ zxc_decompress_chunk_wrapper()

int zxc_decompress_chunk_wrapper ( const zxc_cctx_t *  ctx,
const uint8_t *  src,
const size_t  src_sz,
uint8_t *  dst,
const size_t  dst_cap,
const uint64_t  block_index 
)

Public decompression dispatcher.

Decompresses one chunk through the runtime ISA dispatch.

Definition at line 434 of file zxc_dispatch.c.

References zxc_variants().

Referenced by zxc_dctx_decode_frame(), zxc_decode_bounce(), zxc_decompress_block(), and zxc_static_decompress_block().

◆ zxc_decompress_chunk_wrapper_safe_public()

static int zxc_decompress_chunk_wrapper_safe_public ( const zxc_cctx_t *  ctx,
const uint8_t *  src,
const size_t  src_sz,
uint8_t *  dst,
const size_t  dst_cap,
const uint64_t  block_index 
)
static

Internal safe-decompression dispatcher (strict dst_capacity == uncompressed_size).

Parameters
[in]ctxDecompression context.
[in]srcCompressed input chunk.
[in]src_szSize of src in bytes.
[out]dstDestination buffer (exactly the decoded size).
[in]dst_capCapacity of dst.
[in]block_indexFrame position of the block: the checksum seed.
Returns
Bytes written on success, or a negative zxc_error_t.

Definition at line 456 of file zxc_dispatch.c.

References zxc_decompress_safe_variant().

Referenced by zxc_decompress_block_safe(), and zxc_static_decompress_block().

◆ zxc_decompress_oneshot()

static int64_t zxc_decompress_oneshot ( const uint8_t *  src,
size_t  src_size,
uint8_t *  dst,
size_t  dst_capacity,
const zxc_decompress_opts_t *  opts 
)
static

Definition at line 1642 of file zxc_dispatch.c.

◆ zxc_decompress_safe_resolve()

static zxc_decompress_func_t zxc_decompress_safe_resolve ( void  )
static

First call: the _safe decoder of the selected set, published.

Definition at line 401 of file zxc_dispatch.c.

References zxc_decompress_safe_ptr, and zxc_variants().

Referenced by zxc_decompress_safe_variant().

◆ zxc_decompress_safe_variant()

static zxc_decompress_func_t zxc_decompress_safe_variant ( void  )
inlinestatic

The _safe decoder in use, selected on the first call.

Definition at line 422 of file zxc_dispatch.c.

References zxc_decompress_safe_ptr, and zxc_decompress_safe_resolve().

Referenced by zxc_decompress_chunk_wrapper_safe_public().

◆ zxc_detect_cpu_features()

static zxc_cpu_feature_t zxc_detect_cpu_features ( void  )
static

Probes the running CPU for SIMD support.

Uses CPUID on x86-64 (MSVC and GCC/Clang paths), getauxval on 32-bit ARM Linux, and compile-time constants on AArch64.

Returns
The highest zxc_cpu_feature_t level supported.

Definition at line 225 of file zxc_dispatch.c.

References ZXC_CPU_AVX2, ZXC_CPU_AVX512, ZXC_CPU_GENERIC, ZXC_CPU_NEON, and ZXC_CPU_SSE2.

Referenced by zxc_select_variants().

◆ zxc_huf_build_code_lengths()

int zxc_huf_build_code_lengths ( const uint32_t *  freq,
uint8_t *  code_len,
void *  scratch,
const int  max_code_len 
)

Build length-limited per-symbol Huffman code lengths from frequencies.

Build length-limited canonical Huffman code lengths from a frequency table.

Un-suffixed entry forwarding to zxc_huf_build_code_lengths_default; full contract in zxc_internal.h.

Definition at line 490 of file zxc_dispatch.c.

Referenced by zxc_encode_block_glo(), zxc_opt_estimate_lit_bits(), and zxc_train_dict_huf().

◆ zxc_huf_calc_size()

size_t zxc_huf_calc_size ( const uint32_t *  freq,
const uint8_t *  code_len,
const int  with_header 
)

Un-suffixed forwarders for the PivCo section codec (tests, tools).

Exact encoded size (bytes) of a PivCo section for this histogram and code lengths; includes the 128-byte lengths header when with_header.

Definition at line 496 of file zxc_dispatch.c.

Referenced by zxc_encode_block_glo().

◆ zxc_huf_calc_size_dict()

size_t zxc_huf_calc_size_dict ( const uint32_t *  freq,
const uint8_t *  code_len,
const zxc_pivco_tree_t *  tree 
)

zxc_huf_calc_size for a dict section: prebuilt tree, no header.

Definition at line 527 of file zxc_dispatch.c.

Referenced by zxc_encode_block_glo().

◆ zxc_huf_decode_section()

int zxc_huf_decode_section ( const uint8_t *  payload,
size_t  payload_size,
uint8_t *  dst,
size_t  n,
uint8_t *  scratch 
)

Decode a PivCo literal section into dst (exactly n bytes).

dst needs ZXC_PAD_SIZE slack, scratch at least n + ZXC_PIVCO_SCRATCH_PAD.

Definition at line 507 of file zxc_dispatch.c.

◆ zxc_huf_decode_section_dict()

int zxc_huf_decode_section_dict ( const uint8_t *  payload,
const size_t  payload_size,
uint8_t *  dst,
const size_t  n,
const zxc_pivco_tree_t *  tree,
uint8_t *  scratch 
)

Decode a PivCo dict section against a prebuilt dict tree.

Definition at line 521 of file zxc_dispatch.c.

◆ zxc_huf_encode_section()

int zxc_huf_encode_section ( const uint8_t *  literals,
const size_t  n_literals,
const uint32_t *  freq,
const uint8_t *  code_len,
uint8_t *  dst,
const size_t  dst_cap 
)

Encode a PivCo literal section (128-byte lengths header + payload).

Definition at line 501 of file zxc_dispatch.c.

Referenced by zxc_encode_block_glo().

◆ zxc_huf_encode_section_dict()

int zxc_huf_encode_section_dict ( const uint8_t *  literals,
const size_t  n_literals,
const uint32_t *  freq,
const uint8_t *  code_len,
const zxc_pivco_tree_t *  tree,
const uint32_t *  codes,
uint8_t *  dst,
const size_t  dst_cap 
)

Encode a PivCo section against a prebuilt dict tree/codes (no header).

Definition at line 512 of file zxc_dispatch.c.

Referenced by zxc_encode_block_glo().

◆ zxc_huf_pack_lengths()

void zxc_huf_pack_lengths ( const uint8_t *  code_len,
uint8_t *  out 
)

Pack per-symbol code lengths into the 128-byte nibble header.

Pack per-symbol code lengths into the 128-byte (4-bit nibble) header.

Un-suffixed entry forwarding to zxc_huf_pack_lengths_default; full contract in zxc_internal.h.

Definition at line 538 of file zxc_dispatch.c.

Referenced by zxc_train_dict_huf().

◆ zxc_huf_unpack_lengths()

int zxc_huf_unpack_lengths ( const uint8_t *  in,
uint8_t *  code_len 
)

Unpack and validate a 128-byte packed lengths header.

Unpack and structurally validate a 128-byte packed lengths header.

Un-suffixed entry forwarding to zxc_huf_unpack_lengths_default; full contract in zxc_internal.h.

Definition at line 548 of file zxc_dispatch.c.

◆ zxc_inplace_margin()

static uint64_t zxc_inplace_margin ( const uint64_t  dsize,
const size_t  chunk_size,
const int  has_cs,
const int  has_seek,
const size_t  footer_len 
)
static

Bytes an in-place decode needs on top of the decompressed size.

The walk never writes into unread input; the margin keeps a full block of room for an archive its header and footer describe, so every block takes the fast path. Flush-right placement puts block 0 at capacity - comp_size, so the read cursor before block k sits at capacity - sum_{j>=k} (c_j + H) - trailing, and sum_{j<k} o_j + chunk_size + PAD <= R_k requires

capacity >= max_k [ sum_{j<k} o_j + chunk_size + sum_{j>=k} (c_j + H) ] + PAD + trailing

Incompressible input (all RAW, c_j = o_j = chunk_size) maximises the bracket at dsize + chunk_size + nblocks * H: the margin carries the whole accumulated per-block overhead, not just one block's.

trailing is everything written after the last data block: EOF header, the seek table when HAS_SEEK_TABLE announces one, and the footer. Like has_cs, the flag is taken on trust here and checked by the walk: a tail it does not announce is trailing bytes the bound did not count, as inserted padding is, and fails the tail check. Leaving out an announced table used to push the bound below comp_size for a seekable archive of incompressible data in small blocks.

Parameters
[in]dsizeDecompressed size, in bytes.
[in]chunk_sizeBlock size from the file header (0 = no blocks).
[in]has_csNon-zero if blocks carry checksums.
[in]has_seekNon-zero if the header announces a seek table.
[in]footer_lenThe archive's footer bytes, digest included.
Returns
Bytes to reserve beyond dsize.

Definition at line 885 of file zxc_dispatch.c.

References ZXC_BLOCK_CHECKSUM_SIZE, ZXC_BLOCK_HEADER_SIZE, ZXC_DECOMPRESS_TAIL_PAD, zxc_seek_block_count(), and zxc_seek_table_bytes().

Referenced by zxc_inplace_probe().

◆ zxc_inplace_prepare()

static int zxc_inplace_prepare ( const void *  buffer,
const size_t  buffer_capacity,
const size_t  comp_size,
const zxc_decompress_opts_t *  opts,
const uint8_t **  comp 
)
static

Argument and buffer checks shared by the in-place entry points.

Runs before any allocation, so both entry points refuse the same calls with the same codes. On success comp is the start of the flush-right archive.

Parameters
[in]bufferCaller buffer holding the flush-right archive.
[in]buffer_capacityTotal size of buffer.
[in]comp_sizeSize of the archive.
[in]optsDecompression options, or NULL.
[out]compStart of the archive inside buffer.
Returns
ZXC_OK, or a negative zxc_error_t.

Definition at line 984 of file zxc_dispatch.c.

References ZXC_DICT_SIZE_MAX, ZXC_ERROR_DICT_TOO_LARGE, ZXC_ERROR_DST_TOO_SMALL, ZXC_ERROR_NULL_INPUT, zxc_inplace_probe(), ZXC_OK, and ZXC_OPTS_DICT_SIZE.

Referenced by zxc_decompress_inplace(), and zxc_decompress_inplace_dctx().

◆ zxc_inplace_probe()

static int zxc_inplace_probe ( const uint8_t *  comp,
const size_t  comp_size,
uint64_t *  dsize,
uint64_t *  margin,
uint64_t *  floor 
)
static

Shared archive probe for the in-place entry points: validates the magic + file header, then reads the footer's decompressed size and derives the in-place margin.

Keeping this parse in one place guarantees zxc_decompress_inplace_bound and zxc_decompress_inplace always agree on what a buffer of at least the bound must satisfy.

The envelope is untrusted, so it goes through zxc_buffer_frame_info() like zxc_get_decompressed_size does; only the error mapping differs.

Parameters
[in]compCompressed archive; only the header and footer are read.
[in]comp_sizeSize of the archive in bytes. The caller guarantees it covers at least the file header and footer.
[out]dsizeDecompressed size read from the footer.
[out]marginIn-place margin for dsize, from zxc_inplace_margin.
[out]floorMinimum off: what has to separate the flush-right archive from the head of the buffer.
Returns
ZXC_OK, or a negative zxc_error_t on an invalid archive.

Definition at line 919 of file zxc_dispatch.c.

References zxc_frame_info_t::block_size, zxc_frame_info_t::decompressed_size, zxc_frame_info_t::has_checksum, zxc_frame_info_t::has_seek_table, zxc_buffer_frame_info(), ZXC_DECOMPRESS_TAIL_PAD, ZXC_ERROR_BAD_HEADER, ZXC_ERROR_BAD_MAGIC, ZXC_ERROR_CORRUPT_DATA, zxc_inplace_margin(), and ZXC_OK.

Referenced by zxc_decompress_inplace_bound(), and zxc_inplace_prepare().

◆ zxc_probe_without_dst()

static int64_t zxc_probe_without_dst ( const uint8_t *  src,
const size_t  src_size,
const zxc_decompress_opts_t *  opts,
const zxc_container_info_t *  scanned 
)
static

Answers a decode request that carries no destination.

Walks the surviving frame for real on a stand-in buffer, so the verdict is the one a caller with a destination would have got, by construction rather than by keeping a second copy of the checks in step.

Definition at line 820 of file zxc_dispatch.c.

References zxc_container_info_t::dsize, ZXC_ERROR_DST_TOO_SMALL, ZXC_OK, zxc_scan_container(), and zxc_scan_src_mem().

Referenced by zxc_dctx_probe(), and zxc_decompress().

◆ zxc_select_variants()

static const zxc_variant_set_t * zxc_select_variants ( void  )
static

Detects the CPU tier and returns the matching variant set.

Falls back to the _default (baseline) set when no ISA extension applies.

Definition at line 345 of file zxc_dispatch.c.

References ZXC_CPU_AVX2, ZXC_CPU_AVX512, ZXC_CPU_NEON, and zxc_detect_cpu_features().

Referenced by zxc_variants_resolve().

◆ zxc_static_decompress_block()

static int64_t zxc_static_decompress_block ( zxc_dctx *  dctx,
const uint8_t *  src,
const size_t  src_size,
uint8_t *  dst,
const size_t  dst_capacity,
const size_t  dict_size,
const int  checksum_enabled,
const int  strict 
)
static

Block decode on a static dctx: the carved block is the effective capacity.

Fast decoder: carved block plus wild-copy margin; strict: at most that. A larger block still within the margin is ZXC_ERROR_BAD_BLOCK_SIZE, beyond it the decoder's own too-small-destination code, never reinterpreted.

Definition at line 1786 of file zxc_dispatch.c.

References zxc_cctx_t::checksum_enabled, zxc_cctx_t::dict_size, zxc_decode_bounce(), zxc_decompress_chunk_wrapper(), zxc_decompress_chunk_wrapper_safe_public(), ZXC_DECOMPRESS_TAIL_PAD, ZXC_DICT_SIZE_MAX, ZXC_ERROR_BAD_BLOCK_SIZE, ZXC_ERROR_DICT_TOO_LARGE, and ZXC_ERROR_DICT_UNSUPPORTED.

Referenced by zxc_decompress_block(), and zxc_decompress_block_safe().

◆ zxc_variants()

static const zxc_variant_set_t * zxc_variants ( void  )
inlinestatic

The variant set in use, selected on the first call.

Definition at line 379 of file zxc_dispatch.c.

References zxc_variants_ptr, and zxc_variants_resolve().

Referenced by zxc_compress_chunk_wrapper(), zxc_decompress_chunk_wrapper(), and zxc_decompress_safe_resolve().

◆ zxc_variants_resolve()

static const zxc_variant_set_t * zxc_variants_resolve ( void  )
static

First call: selects and publishes.

Cold and out of line, so the dispatchers inline only the load and the branch.

Definition at line 367 of file zxc_dispatch.c.

References zxc_select_variants(), and zxc_variants_ptr.

Referenced by zxc_variants().

◆ zxc_write_empty_frame()

static int64_t zxc_write_empty_frame ( uint8_t *  dst,
const size_t  dst_capacity,
const size_t  block_size,
const int  checksum_enabled,
const uint32_t  did,
const int  seekable 
)
static

Writes the whole archive an empty source produces: file header, EOF block, the empty seek table when seekable, then the footer.

Both entry points return through here: same options, same empty archive, by construction. Neither carves a workspace: there is no block to encode.

Returns
Bytes written, or a negative zxc_error_t.

Definition at line 567 of file zxc_dispatch.c.

References zxc_block_header_t::block_type, zxc_seek_table_header(), zxc_write_block_header(), zxc_write_file_footer(), and zxc_write_file_header().

Referenced by zxc_compress(), and zxc_compress_cctx().

◆ zxc_write_seek_table_from_blocks()

static int64_t zxc_write_seek_table_from_blocks ( uint8_t *  dst,
const size_t  dst_capacity,
const uint8_t *  blocks,
const uint8_t *const  blocks_end,
const uint64_t  num_blocks,
const int  checksum_enabled 
)
static

Writes the seek table of the num_blocks blocks in [blocks, blocks_end), sizes read back from their headers.

Rereading spares the context API a per-call allocation. The walk must land exactly on the EOF block, or dst changed under us.

Returns
Bytes written, or a negative zxc_error_t.

Definition at line 602 of file zxc_dispatch.c.

References ZXC_BLOCK_CHECKSUM_SIZE, ZXC_BLOCK_HEADER_SIZE, ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_DST_TOO_SMALL, ZXC_ERROR_OVERFLOW, ZXC_FILE_HEADER_SIZE, zxc_le32(), ZXC_SEEK_ANCHOR_SIZE, ZXC_SEEK_GROUP, ZXC_SEEK_SIZE_ENTRY, zxc_seek_table_header(), zxc_seek_table_size(), zxc_store_le32(), and zxc_store_le64().

Referenced by zxc_compress_cctx().

Variable Documentation

◆ zxc_decompress_safe_ptr

_Atomic zxc_decompress_func_t zxc_decompress_safe_ptr = (zxc_decompress_func_t)0
static

The _safe decoder in use, NULL until the first call.

Definition at line 397 of file zxc_dispatch.c.

Referenced by zxc_decompress_safe_resolve(), and zxc_decompress_safe_variant().

◆ zxc_variants_default

const zxc_variant_set_t zxc_variants_default = {zxc_decompress_chunk_wrapper_default , zxc_decompress_chunk_wrapper_dict_default , zxc_compress_chunk_wrapper_default }
static

Definition at line 322 of file zxc_dispatch.c.

◆ zxc_variants_ptr

const zxc_variant_set_t* _Atomic zxc_variants_ptr = NULL
static

The selection, NULL until the first call.

Definition at line 362 of file zxc_dispatch.c.

Referenced by zxc_variants(), and zxc_variants_resolve().