ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_dispatch.c
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * ZXC - High-performance lossless compression
4 *
5 * Copyright (c) Bertrand Lebonnois and contributors.
6 */
7
19
22#include "zxc_internal.h"
23
24// Roles bound to _default (ZXC_ONLY_DEFAULT and the rest): see zxc_build.h.
25
26#if defined(_MSC_VER)
27#include <intrin.h>
28#if defined(_M_X64)
29#include <immintrin.h> // _xgetbv (x86-specific header; x64 AVX state check)
30#endif
31#endif
32
33#if (defined(__x86_64__) || defined(_M_X64)) && !defined(_MSC_VER) && !defined(ZXC_ONLY_DEFAULT)
34#include <cpuid.h> // __cpuid_count: CPUID probes in zxc_detect_cpu_features
35#endif
36
37#if defined(__linux__) && (defined(__arm__) || defined(_M_ARM)) && !defined(ZXC_ONLY_DEFAULT)
38#include <sys/auxv.h>
39// musl does not ship <asm/hwcap.h>; HWCAP_NEON is stable arm32 UAPI. Nested:
40// naming __has_include in a single #if is a syntax error where it is missing.
41#ifdef __has_include
42#if __has_include(<asm/hwcap.h>)
43#include <asm/hwcap.h>
44#endif
45#endif
46#ifndef HWCAP_NEON
47#define HWCAP_NEON (1 << 12)
48#endif
49#endif
50
51// ============================================================================
52// PROTOTYPES FOR MULTI-VERSIONED VARIANTS
53// ============================================================================
54// These are compiled in separate translation units with different flags.
55
56// Decompression Prototypes
57int zxc_decompress_chunk_wrapper_default(const zxc_cctx_t* RESTRICT ctx,
58 const uint8_t* RESTRICT src, const size_t src_sz,
59 uint8_t* RESTRICT dst, const size_t dst_cap,
60 const uint64_t block_index);
61int zxc_decompress_chunk_wrapper_dict_default(const zxc_cctx_t* RESTRICT ctx,
62 const uint8_t* RESTRICT src, const size_t src_sz,
63 uint8_t* RESTRICT dst, const size_t dst_cap,
64 const uint64_t block_index);
65int zxc_decompress_chunk_wrapper_safe_default(const zxc_cctx_t* RESTRICT ctx,
66 const uint8_t* RESTRICT src, const size_t src_sz,
67 uint8_t* RESTRICT dst, const size_t dst_cap,
68 const uint64_t block_index);
69
70#ifndef ZXC_ONLY_DEFAULT
71#if defined(__x86_64__) || defined(_M_X64)
72int zxc_decompress_chunk_wrapper_avx2(const zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
73 const size_t src_sz, uint8_t* RESTRICT dst,
74 const size_t dst_cap, const uint64_t block_index);
75int zxc_decompress_chunk_wrapper_dict_avx2(const zxc_cctx_t* RESTRICT ctx,
76 const uint8_t* RESTRICT src, const size_t src_sz,
77 uint8_t* RESTRICT dst, const size_t dst_cap,
78 const uint64_t block_index);
79int zxc_decompress_chunk_wrapper_avx512(const zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
80 const size_t src_sz, uint8_t* RESTRICT dst,
81 const size_t dst_cap, const uint64_t block_index);
82int zxc_decompress_chunk_wrapper_dict_avx512(const zxc_cctx_t* RESTRICT ctx,
83 const uint8_t* RESTRICT src, const size_t src_sz,
84 uint8_t* RESTRICT dst, const size_t dst_cap,
85 const uint64_t block_index);
86int zxc_decompress_chunk_wrapper_safe_avx2(const zxc_cctx_t* RESTRICT ctx,
87 const uint8_t* RESTRICT src, const size_t src_sz,
88 uint8_t* RESTRICT dst, const size_t dst_cap,
89 const uint64_t block_index);
90int zxc_decompress_chunk_wrapper_safe_avx512(const zxc_cctx_t* RESTRICT ctx,
91 const uint8_t* RESTRICT src, const size_t src_sz,
92 uint8_t* RESTRICT dst, const size_t dst_cap,
93 const uint64_t block_index);
94#elif defined(__arm__) || defined(_M_ARM)
95int zxc_decompress_chunk_wrapper_neon32(const zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
96 const size_t src_sz, uint8_t* RESTRICT dst,
97 const size_t dst_cap, const uint64_t block_index);
98int zxc_decompress_chunk_wrapper_dict_neon32(const zxc_cctx_t* RESTRICT ctx,
99 const uint8_t* RESTRICT src, const size_t src_sz,
100 uint8_t* RESTRICT dst, const size_t dst_cap,
101 const uint64_t block_index);
102int zxc_decompress_chunk_wrapper_safe_neon32(const zxc_cctx_t* RESTRICT ctx,
103 const uint8_t* RESTRICT src, const size_t src_sz,
104 uint8_t* RESTRICT dst, const size_t dst_cap,
105 const uint64_t block_index);
106#endif
107#endif
108
109// Compression Prototypes
110int zxc_compress_chunk_wrapper_default(zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
111 const size_t src_sz, uint8_t* RESTRICT dst,
112 const size_t dst_cap, const uint64_t block_index);
113
114// Huffman prototypes (variant TUs of zxc_huffman.c). Compressor and decompressor
115// variants bind to the matching suffixed symbol at compile time, so the hot path
116// pays no dispatch; the wrappers below expose un-suffixed names to callers.
117int zxc_huf_build_code_lengths_default(const uint32_t* RESTRICT freq, uint8_t* RESTRICT code_len,
118 void* RESTRICT scratch, int max_code_len);
119size_t zxc_huf_calc_size_default(const uint32_t* RESTRICT freq, const uint8_t* RESTRICT code_len,
120 int with_header);
121int zxc_huf_encode_section_default(const uint8_t* RESTRICT literals, size_t n_literals,
122 const uint32_t* RESTRICT freq, const uint8_t* RESTRICT code_len,
123 uint8_t* RESTRICT dst, size_t dst_cap);
124int zxc_huf_decode_section_default(const uint8_t* RESTRICT payload, size_t payload_size,
125 uint8_t* RESTRICT dst, size_t n, uint8_t* RESTRICT scratch);
126int zxc_huf_encode_section_dict_default(const uint8_t* RESTRICT literals, size_t n_literals,
127 const uint32_t* RESTRICT freq,
128 const uint8_t* RESTRICT code_len,
129 const zxc_pivco_tree_t* RESTRICT tree,
130 const uint32_t* RESTRICT codes, uint8_t* RESTRICT dst,
131 size_t dst_cap);
132int zxc_huf_decode_section_dict_default(const uint8_t* RESTRICT payload, size_t payload_size,
133 uint8_t* RESTRICT dst, size_t n,
134 const zxc_pivco_tree_t* RESTRICT tree,
135 uint8_t* RESTRICT scratch);
136size_t zxc_huf_calc_size_dict_default(const uint32_t* RESTRICT freq,
137 const uint8_t* RESTRICT code_len,
138 const zxc_pivco_tree_t* RESTRICT tree);
139void zxc_huf_pack_lengths_default(const uint8_t* RESTRICT code_len, uint8_t* RESTRICT out);
140int zxc_huf_unpack_lengths_default(const uint8_t* RESTRICT in, uint8_t* RESTRICT code_len);
141
142#if defined(__x86_64__) || defined(_M_X64)
143int zxc_compress_chunk_wrapper_avx2(zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
144 const size_t src_sz, uint8_t* RESTRICT dst,
145 const size_t dst_cap, const uint64_t block_index);
146int zxc_compress_chunk_wrapper_avx512(zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
147 const size_t src_sz, uint8_t* RESTRICT dst,
148 const size_t dst_cap, const uint64_t block_index);
149#elif defined(__arm__) || defined(_M_ARM)
150int zxc_compress_chunk_wrapper_neon32(zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
151 const size_t src_sz, uint8_t* RESTRICT dst,
152 const size_t dst_cap, const uint64_t block_index);
153#endif
154
155#ifdef ZXC_DISPATCH_ENTROPY
156// Entropy section decoders of the SIMD variants.
157#define ZXC_HUF_DECODE_PROTOTYPES(sfx) \
158 int zxc_huf_decode_section##sfx(const uint8_t* RESTRICT payload, size_t payload_size, \
159 uint8_t* RESTRICT dst, size_t n, uint8_t* RESTRICT scratch); \
160 int zxc_huf_decode_section_dict##sfx( \
161 const uint8_t* RESTRICT payload, size_t payload_size, uint8_t* RESTRICT dst, size_t n, \
162 const zxc_pivco_tree_t* RESTRICT tree, uint8_t* RESTRICT scratch)
163#if defined(__x86_64__) || defined(_M_X64)
164ZXC_HUF_DECODE_PROTOTYPES(_avx2);
165ZXC_HUF_DECODE_PROTOTYPES(_avx512);
166#elif defined(__arm__) || defined(_M_ARM)
167ZXC_HUF_DECODE_PROTOTYPES(_neon32);
168#endif
169#undef ZXC_HUF_DECODE_PROTOTYPES
170#endif
171
172// ============================================================================
173// CPU DETECTION LOGIC
174// ============================================================================
175
176#if (defined(__x86_64__) || defined(_M_X64)) && !defined(ZXC_ONLY_DEFAULT)
177
179static inline void zxc_cpuid(const uint32_t leaf, const uint32_t sub, uint32_t regs[4]) {
180#if defined(_MSC_VER)
181 int r[4];
182 __cpuidex(r, (int)leaf, (int)sub);
183 regs[0] = (uint32_t)r[0];
184 regs[1] = (uint32_t)r[1];
185 regs[2] = (uint32_t)r[2];
186 regs[3] = (uint32_t)r[3];
187#else
188 __cpuid_count(leaf, sub, regs[0], regs[1], regs[2], regs[3]);
189#endif
190}
191
193static inline uint64_t zxc_xgetbv0(void) {
194#if defined(_MSC_VER)
195 return _xgetbv(0);
196#else
197 // Raw encoding: the xgetbv intrinsic needs -mxsave, which the baseline
198 // translation unit is not compiled with.
199 uint32_t lo, hi;
200 __asm__ volatile(".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c"(0));
201 return ((uint64_t)hi << 32) | lo;
202#endif
203}
204#endif /* x86-64 && !ZXC_ONLY_DEFAULT */
205
207typedef int (*zxc_decompress_func_t)(const zxc_cctx_t* RESTRICT, const uint8_t* RESTRICT,
208 const size_t, uint8_t* RESTRICT, const size_t, const uint64_t);
210typedef int (*zxc_compress_func_t)(zxc_cctx_t* RESTRICT, const uint8_t* RESTRICT, const size_t,
211 uint8_t* RESTRICT, const size_t, const uint64_t);
212#ifdef ZXC_DISPATCH_ENTROPY
214typedef int (*zxc_huf_decode_func_t)(const uint8_t* RESTRICT, size_t, uint8_t* RESTRICT, size_t,
215 uint8_t* RESTRICT);
216#ifndef ZXC_NO_DICT
218typedef int (*zxc_huf_decode_dict_func_t)(const uint8_t* RESTRICT, size_t, uint8_t* RESTRICT,
219 size_t, const zxc_pivco_tree_t* RESTRICT,
220 uint8_t* RESTRICT);
221#endif
222#endif
223
224#ifndef ZXC_ONLY_DEFAULT
225
239
248// LCOV_EXCL_START
251
252#if defined(__x86_64__) || defined(_M_X64)
253 // AVX2/AVX512 need OS-enabled YMM/ZMM state: gate on OSXSAVE + XGETBV/XCR0,
254 // not CPUID alone (else a VEX/EVEX op faults #UD when the OS hasn't enabled it).
255 uint32_t regs[4];
256 int sse2 = 0;
257 int avx2 = 0;
258 int avx512 = 0;
259 int bmi_lzcnt = 0;
260
261 zxc_cpuid(0, 0, regs);
262 const uint32_t max_leaf = regs[0];
263
264 zxc_cpuid(1, 0, regs);
265 if (regs[3] & (1U << 26)) sse2 = 1; // CPUID.1:EDX[26]
266 if ((regs[2] & (1U << 27)) && max_leaf >= 7) { // OSXSAVE, and leaf 7 is real
267 const uint64_t xcr0 = zxc_xgetbv0();
268 if ((xcr0 & 0x6) == 0x6) { // SSE+YMM enabled
269 zxc_cpuid(7, 0, regs);
270 const int bmi1 = (regs[1] >> 3) & 1; // CPUID.7.0:EBX[3]
271 const int bmi2 = (regs[1] >> 8) & 1; // CPUID.7.0:EBX[8]
272 if (regs[1] & (1U << 5)) avx2 = 1;
273 // AVX512 also needs XCR0[5..7] (opmask/ZMM)
274 if ((regs[1] & (1U << 16)) && (regs[1] & (1U << 30)) && (regs[2] & (1U << 1)) &&
275 (regs[2] & (1U << 6)) && (xcr0 & 0xE0) == 0xE0)
276 avx512 = 1; /* AVX512 tier = F+BW+VBMI+VBMI2, as the variant is built */
277 // The AVX2/AVX512 variants are compiled with BMI1/BMI2/LZCNT enabled,
278 // so both gates must prove those bits too. LZCNT (ABM) lives in
279 // CPUID.80000001H:ECX[5]; that leaf is architectural on x86-64.
280 zxc_cpuid(0x80000001U, 0, regs);
281 bmi_lzcnt = bmi1 && bmi2 && ((regs[2] >> 5) & 1);
282 }
283 }
284
285 if (avx512 && bmi_lzcnt) {
286 features = ZXC_CPU_AVX512;
287 } else if (avx2 && bmi_lzcnt) {
288 features = ZXC_CPU_AVX2;
289 } else if (sse2) {
290 features = ZXC_CPU_SSE2;
291 }
292
293#elif defined(__aarch64__) || defined(_M_ARM64)
294 // ARM64 usually guarantees NEON
295 features = ZXC_CPU_NEON;
296
297#elif defined(__arm__) || defined(_M_ARM)
298 // ARM32 Runtime detection for Linux
299#if defined(__linux__)
300 const unsigned long hwcaps = getauxval(AT_HWCAP);
301 if (hwcaps & HWCAP_NEON) {
302 features = ZXC_CPU_NEON;
303 }
304#else
305// Fallback for non-Linux: rely on compiler flags.
306// If compiled with -mfpu=neon, we assume target supports it.
307// Otherwise, safe default is GENERIC.
308#if defined(__ARM_NEON)
309 features = ZXC_CPU_NEON;
310#endif
311#endif
312#endif
313
314 return features;
315}
316// LCOV_EXCL_STOP
317
318#endif // ZXC_ONLY_DEFAULT
319
320// ============================================================================
321// DISPATCHERS
322// ============================================================================
323// One constant variant set per ISA, one published pointer to the selected one;
324// ZXC_ONLY_DEFAULT binds _default at compile time.
325
335typedef struct {
336 zxc_decompress_func_t decompress;
337#ifndef ZXC_NO_DICT
338 zxc_decompress_func_t decompress_dict;
339#endif
340 zxc_compress_func_t compress;
341#ifdef ZXC_DISPATCH_ENTROPY
342 zxc_huf_decode_func_t huf_decode;
343#ifndef ZXC_NO_DICT
344 zxc_huf_decode_dict_func_t huf_decode_dict;
345#endif
346#endif
347} zxc_variant_set_t;
348
349#define ZXC_GLUE_IMPL(a, b) a##b
350#define ZXC_GLUE(a, b) ZXC_GLUE_IMPL(a, b)
351
353#ifdef ZXC_LZ_DECODER_DEFAULT
354#define ZXC_DEC_SFX(sfx) _default
355#else
356#define ZXC_DEC_SFX(sfx) sfx
357#endif
358#ifdef ZXC_ENCODER_DEFAULT
359#define ZXC_ENC_SFX(sfx) _default
360#else
361#define ZXC_ENC_SFX(sfx) sfx
362#endif
363
364#ifndef ZXC_NO_DICT
365#define ZXC_VARIANT_DICT(sfx) \
366 .decompress_dict = ZXC_GLUE(zxc_decompress_chunk_wrapper_dict, ZXC_DEC_SFX(sfx)),
367#else
368#define ZXC_VARIANT_DICT(sfx)
369#endif
370
371#if defined(ZXC_DISPATCH_ENTROPY) && !defined(ZXC_NO_DICT)
372#define ZXC_VARIANT_HUF(sfx) \
373 .huf_decode = zxc_huf_decode_section##sfx, .huf_decode_dict = zxc_huf_decode_section_dict##sfx,
374#elif defined(ZXC_DISPATCH_ENTROPY)
375#define ZXC_VARIANT_HUF(sfx) .huf_decode = zxc_huf_decode_section##sfx,
376#else
377#define ZXC_VARIANT_HUF(sfx)
378#endif
379
381#define ZXC_VARIANT_SET(sfx) \
382 static const zxc_variant_set_t zxc_variants##sfx = { \
383 .decompress = ZXC_GLUE(zxc_decompress_chunk_wrapper, ZXC_DEC_SFX(sfx)), \
384 .compress = ZXC_GLUE(zxc_compress_chunk_wrapper, ZXC_ENC_SFX(sfx)), \
385 ZXC_VARIANT_DICT(sfx) ZXC_VARIANT_HUF(sfx)}
386
387ZXC_VARIANT_SET(_default);
388#ifndef ZXC_ONLY_DEFAULT
389#if defined(__x86_64__) || defined(_M_X64)
390ZXC_VARIANT_SET(_avx2);
391#ifndef ZXC_NO_AVX512
392ZXC_VARIANT_SET(_avx512);
393#endif
394#elif defined(__arm__) || defined(_M_ARM)
395ZXC_VARIANT_SET(_neon32);
396#endif
397#endif
398#undef ZXC_VARIANT_SET
399#undef ZXC_VARIANT_HUF
400#undef ZXC_VARIANT_DICT
401#undef ZXC_ENC_SFX
402#undef ZXC_DEC_SFX
403
404#ifdef ZXC_ONLY_DEFAULT
406static ZXC_ALWAYS_INLINE const zxc_variant_set_t* zxc_variants(void) {
407 return &zxc_variants_default;
408}
409#else
415// LCOV_EXCL_START
416static const zxc_variant_set_t* zxc_select_variants(void) {
418#if defined(__x86_64__) || defined(_M_X64)
419#ifdef ZXC_NO_AVX512
420 if (cpu == ZXC_CPU_AVX512 || cpu == ZXC_CPU_AVX2) return &zxc_variants_avx2;
421#else
422 if (cpu == ZXC_CPU_AVX512) return &zxc_variants_avx512;
423 if (cpu == ZXC_CPU_AVX2) return &zxc_variants_avx2;
424#endif
425#elif defined(__arm__) || defined(_M_ARM)
426 // 32-bit ARM: the only arch with a real runtime NEON probe (getauxval).
427 // cppcheck-suppress knownConditionTrueFalse
428 if (cpu == ZXC_CPU_NEON) return &zxc_variants_neon32;
429#else
430 (void)cpu;
431#endif
432 return &zxc_variants_default;
433}
434// LCOV_EXCL_STOP
435
437static const zxc_variant_set_t* ZXC_ATOMIC zxc_variants_ptr = NULL;
438
441// LCOV_EXCL_START
442static ZXC_COLD ZXC_NOINLINE const zxc_variant_set_t* zxc_variants_resolve(void) {
443 const zxc_variant_set_t* v = zxc_select_variants();
444#if ZXC_USE_C11_ATOMICS
445 atomic_store_explicit(&zxc_variants_ptr, v, memory_order_release);
446#else
448#endif
449 return v;
450}
451// LCOV_EXCL_STOP
452
454static ZXC_ALWAYS_INLINE const zxc_variant_set_t* zxc_variants(void) {
455#if ZXC_USE_C11_ATOMICS
456 const zxc_variant_set_t* v = atomic_load_explicit(&zxc_variants_ptr, memory_order_acquire);
457#else
458 const zxc_variant_set_t* v = zxc_variants_ptr;
459#endif
460 if (UNLIKELY(!v)) v = zxc_variants_resolve();
461 return v;
462}
463#endif // ZXC_ONLY_DEFAULT
464
465#ifdef ZXC_LZ_DECODER_DEFAULT
468 return zxc_decompress_chunk_wrapper_safe_default;
469}
470#else
473
475// LCOV_EXCL_START
477 const zxc_variant_set_t* v = zxc_variants();
478 zxc_decompress_func_t f = zxc_decompress_chunk_wrapper_safe_default;
479#if defined(__x86_64__) || defined(_M_X64)
480#ifndef ZXC_NO_AVX512
481 if (v == &zxc_variants_avx512) f = zxc_decompress_chunk_wrapper_safe_avx512;
482#endif
483 if (v == &zxc_variants_avx2) f = zxc_decompress_chunk_wrapper_safe_avx2;
484#elif defined(__arm__) || defined(_M_ARM)
485 if (v == &zxc_variants_neon32) f = zxc_decompress_chunk_wrapper_safe_neon32;
486#else
487 (void)v;
488#endif
489#if ZXC_USE_C11_ATOMICS
490 atomic_store_explicit(&zxc_decompress_safe_ptr, f, memory_order_release);
491#else
493#endif
494 return f;
495}
496// LCOV_EXCL_STOP
497
500#if ZXC_USE_C11_ATOMICS
501 zxc_decompress_func_t f = atomic_load_explicit(&zxc_decompress_safe_ptr, memory_order_acquire);
502#else
504#endif
505 if (UNLIKELY(!f)) f = zxc_decompress_safe_resolve();
506 return f;
507}
508#endif // ZXC_LZ_DECODER_DEFAULT
509
511int zxc_decompress_chunk_wrapper(const zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
512 const size_t src_sz, uint8_t* RESTRICT dst, const size_t dst_cap,
513 const uint64_t block_index) {
514 // dict_size is constant for a stream; this per-block branch (outside the decode
515 // loop) routes to the dict variant only when a dictionary is active, so the
516 // no-dict path runs the dict-free chunk wrapper (identical codegen to main).
517 const zxc_variant_set_t* v = zxc_variants();
518#ifdef ZXC_NO_DICT
519 // dict_size is always 0.
520 return v->decompress(ctx, src, src_sz, dst, dst_cap, block_index);
521#else
522 return (ctx->dict_size ? v->decompress_dict : v->decompress)(ctx, src, src_sz, dst, dst_cap,
523 block_index);
524#endif
525}
526
539 const uint8_t* RESTRICT src,
540 const size_t src_sz, uint8_t* RESTRICT dst,
541 const size_t dst_cap,
542 const uint64_t block_index) {
543 return zxc_decompress_safe_variant()(ctx, src, src_sz, dst, dst_cap, block_index);
544}
545
547int zxc_compress_chunk_wrapper(zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
548 const size_t src_sz, uint8_t* RESTRICT dst, const size_t dst_cap,
549 const uint64_t block_index) {
550 return zxc_variants()->compress(ctx, src, src_sz, dst, dst_cap, block_index);
551}
552
553// ============================================================================
554// HUFFMAN TRAMPOLINES
555// ============================================================================
556// The Huffman codec is built per-variant (default / avx2 / avx512, plus neon32
557// on 32-bit ARM)
558// alongside zxc_compress.c and zxc_decompress.c, so the LZ77 stages and the
559// Huffman stage in a given variant share the same ISA flags (e.g. -mbmi2 on
560// the AVX2/AVX512 variants). The compress/decompress variant TUs resolve
561// their Huffman calls to the matching suffixed symbol at compile time, so
562// the production hot path has zero dispatch overhead.
563//
564// These thin wrappers exist only for tests and external callers that link
565// against the un-suffixed names. They forward to the default (scalar) variant.
566// With ZXC_DISPATCH_ENTROPY, the two section decoders dispatch per CPU instead.
573int zxc_huf_build_code_lengths(const uint32_t* RESTRICT freq, uint8_t* RESTRICT code_len,
574 void* RESTRICT scratch, const int max_code_len) {
575 return zxc_huf_build_code_lengths_default(freq, code_len, scratch, max_code_len);
576}
577
579size_t zxc_huf_calc_size(const uint32_t* RESTRICT freq, const uint8_t* RESTRICT code_len,
580 const int with_header) {
581 return zxc_huf_calc_size_default(freq, code_len, with_header);
582}
583
584int zxc_huf_encode_section(const uint8_t* RESTRICT literals, const size_t n_literals,
585 const uint32_t* RESTRICT freq, const uint8_t* RESTRICT code_len,
586 uint8_t* RESTRICT dst, const size_t dst_cap) {
587 return zxc_huf_encode_section_default(literals, n_literals, freq, code_len, dst, dst_cap);
588}
589
590int zxc_huf_decode_section(const uint8_t* RESTRICT payload, const size_t payload_size,
591 uint8_t* RESTRICT dst, const size_t n, uint8_t* RESTRICT scratch) {
592#ifdef ZXC_DISPATCH_ENTROPY
593 return zxc_variants()->huf_decode(payload, payload_size, dst, n, scratch);
594#else
595 return zxc_huf_decode_section_default(payload, payload_size, dst, n, scratch);
596#endif
597}
598
599int zxc_huf_encode_section_dict(const uint8_t* RESTRICT literals, const size_t n_literals,
600 const uint32_t* RESTRICT freq, const uint8_t* RESTRICT code_len,
601 const zxc_pivco_tree_t* RESTRICT tree,
602 const uint32_t* RESTRICT codes, uint8_t* RESTRICT dst,
603 const size_t dst_cap) {
604 return zxc_huf_encode_section_dict_default(literals, n_literals, freq, code_len, tree, codes,
605 dst, dst_cap);
606}
607
608int zxc_huf_decode_section_dict(const uint8_t* RESTRICT payload, const size_t payload_size,
609 uint8_t* RESTRICT dst, const size_t n,
610 const zxc_pivco_tree_t* RESTRICT tree, uint8_t* RESTRICT scratch) {
611#if defined(ZXC_DISPATCH_ENTROPY) && !defined(ZXC_NO_DICT)
612 return zxc_variants()->huf_decode_dict(payload, payload_size, dst, n, tree, scratch);
613#else
614 return zxc_huf_decode_section_dict_default(payload, payload_size, dst, n, tree, scratch);
615#endif
616}
617
618size_t zxc_huf_calc_size_dict(const uint32_t* RESTRICT freq, const uint8_t* RESTRICT code_len,
619 const zxc_pivco_tree_t* RESTRICT tree) {
620 return zxc_huf_calc_size_dict_default(freq, code_len, tree);
621}
622
629void zxc_huf_pack_lengths(const uint8_t* RESTRICT code_len, uint8_t* RESTRICT out) {
630 zxc_huf_pack_lengths_default(code_len, out);
631}
632
639int zxc_huf_unpack_lengths(const uint8_t* RESTRICT in, uint8_t* RESTRICT code_len) {
640 return zxc_huf_unpack_lengths_default(in, code_len);
641}
642
643// ============================================================================
644// PUBLIC UTILITY API
645// ============================================================================
646// These wrapper functions provide a simplified interface by managing context
647// allocation and looping over blocks. They call the dispatched wrappers above.
648
658static int64_t zxc_write_empty_frame(uint8_t* RESTRICT dst, const size_t dst_capacity,
659 const size_t block_size, const int checksum_enabled,
660 const int has_dict, const uint32_t did, const int seekable) {
661 const int h = zxc_write_file_header(dst, dst_capacity, block_size, checksum_enabled, has_dict,
662 did, seekable);
663 if (UNLIKELY(h < 0)) return h;
664 size_t off = (size_t)h;
665
666 const zxc_block_header_t eof = {
667 .block_type = ZXC_BLOCK_EOF, .block_flags = 0, .reserved = 0, .comp_size = 0};
668 const int e = zxc_write_block_header(dst + off, dst_capacity - off, &eof);
669 if (UNLIKELY(e < 0)) return e;
670 off += (size_t)e;
671
672 // The flag promised a table: an empty one, a bare SEK header.
673 if (seekable) {
674 const int t = zxc_seek_table_header(dst + off, dst_capacity - off, 0);
675 if (UNLIKELY(t < 0)) return t;
676 off += (size_t)t;
677 }
678
679 const int f = zxc_write_file_footer(dst + off, dst_capacity - off, off, 0, 0, checksum_enabled);
680 if (UNLIKELY(f < 0)) return f;
681 return (int64_t)(off + (size_t)f);
682}
683
693static int64_t zxc_write_seek_table_from_blocks(uint8_t* RESTRICT dst, const size_t dst_capacity,
694 const uint8_t* RESTRICT blocks,
695 const uint8_t* const blocks_end,
696 const uint64_t num_blocks,
697 const int checksum_enabled) {
698 const size_t total = zxc_seek_table_size(num_blocks);
699 if (UNLIKELY(total == 0)) return ZXC_ERROR_OVERFLOW; // LCOV_EXCL_LINE
700 if (UNLIKELY(dst_capacity < total)) return ZXC_ERROR_DST_TOO_SMALL;
701
702 const int h = zxc_seek_table_header(dst, dst_capacity, num_blocks);
703 if (UNLIKELY(h < 0)) return h; // LCOV_EXCL_LINE
704 uint8_t* p = dst + h;
705
706 const size_t trailer = ZXC_BLOCK_HEADER_SIZE + (checksum_enabled ? ZXC_BLOCK_CHECKSUM_SIZE : 0);
707 uint64_t anchor = ZXC_FILE_HEADER_SIZE;
708 for (uint64_t i = 0; i < num_blocks; i++) {
709 if (i % ZXC_SEEK_GROUP == 0) {
710 zxc_store_le64(p, anchor);
712 }
713 const size_t left = (size_t)(blocks_end - blocks);
714 if (UNLIKELY(left < ZXC_BLOCK_HEADER_SIZE))
715 return ZXC_ERROR_CORRUPT_DATA; // LCOV_EXCL_LINE
716 const size_t size = (size_t)zxc_le32(blocks + 3) + trailer; // comp_size at offset 3
717 if (UNLIKELY(size > left)) return ZXC_ERROR_CORRUPT_DATA; // LCOV_EXCL_LINE
718 zxc_store_le32(p, (uint32_t)size);
720 anchor += size;
721 blocks += size;
722 }
723 if (UNLIKELY(blocks != blocks_end)) return ZXC_ERROR_CORRUPT_DATA; // LCOV_EXCL_LINE
724 return (int64_t)(p - dst);
725}
726
727// One-shot decode for zxc_decompress and the no-destination probe.
728static int64_t zxc_decompress_oneshot(const uint8_t* src, size_t src_size, uint8_t* dst,
729 size_t dst_capacity, const zxc_dparams_t* opts);
730
731// The frame walk behind every buffer decode, in place when @p inplace is set.
732static int64_t zxc_dctx_decode_frame(zxc_dctx* dctx, const uint8_t* src, size_t src_size,
733 uint8_t* dst, size_t dst_capacity, const zxc_dparams_t* opts,
734 int inplace, size_t* frame_end);
735
736// The container walk: frames back to back.
737static int64_t zxc_dctx_decode_container(zxc_dctx* dctx, const uint8_t* src, size_t src_size,
738 uint8_t* dst, size_t dst_capacity,
739 const zxc_dparams_t* opts);
740
753static int zxc_buffer_frame_info(const uint8_t* src, const size_t src_size, zxc_frame_info_t* info,
754 size_t* footer_len) {
755 if (UNLIKELY(!src)) return ZXC_ERROR_NULL_INPUT;
756 const zxc_scan_src_t scan = zxc_scan_src_mem(src, src_size);
757 return zxc_scan_whole_frame(&scan, info, footer_len);
758}
759
767static int64_t zxc_probe_without_dst(const uint8_t* RESTRICT src, const size_t src_size,
768 const zxc_dparams_t* opts,
769 const zxc_container_info_t* scanned) {
770 // Only an empty archive succeeds without a buffer. @p scanned: the caller's walk, if any.
772 if (!scanned) {
773 const zxc_scan_src_t scan = zxc_scan_src_mem(src, src_size);
774 const int rc = zxc_scan_container(&scan, 0, &info);
775 if (UNLIKELY(rc != ZXC_OK)) return rc;
776 scanned = &info;
777 }
778 if (scanned->dsize != 0) return ZXC_ERROR_DST_TOO_SMALL;
779 uint8_t probe_dst[1];
780 return zxc_decompress_oneshot(src, src_size, probe_dst, 0, opts);
781}
782
812static uint64_t zxc_inplace_margin(const uint64_t dsize, const size_t chunk_size, const int has_cs,
813 const int has_seek, const size_t footer_len) {
814 const uint64_t nblocks = zxc_seek_block_count(dsize, chunk_size);
815 const uint64_t per_block =
816 (uint64_t)ZXC_BLOCK_HEADER_SIZE + (has_cs ? (uint64_t)ZXC_BLOCK_CHECKSUM_SIZE : 0);
817 const uint64_t seek_table =
818 has_seek ? (uint64_t)ZXC_BLOCK_HEADER_SIZE + zxc_seek_table_bytes(nblocks) : 0;
819 const uint64_t trailing = (uint64_t)ZXC_BLOCK_HEADER_SIZE + // EOF block header
820 seek_table + (uint64_t)footer_len;
821 return (uint64_t)chunk_size + nblocks * per_block + trailing +
822 (uint64_t)ZXC_DECOMPRESS_TAIL_PAD;
823}
824
846static int zxc_inplace_probe(const uint8_t* comp, const size_t comp_size, uint64_t* dsize,
847 uint64_t* margin, uint64_t* floor) {
848 zxc_frame_info_t info;
849 size_t footer_len = 0;
850
851 // Own mapping: only a wrong magic word and a forged footer keep their code,
852 // everything else reads as a bad header.
853 const int rc = zxc_buffer_frame_info(comp, comp_size, &info, &footer_len);
854 if (UNLIKELY(rc != ZXC_OK))
855 return (rc == ZXC_ERROR_BAD_MAGIC || rc == ZXC_ERROR_CORRUPT_DATA) ? rc
857
858 *dsize = info.decompressed_size;
860 info.has_seek_table, footer_len);
861 *floor = (uint64_t)info.block_size + (uint64_t)ZXC_DECOMPRESS_TAIL_PAD;
862 return ZXC_OK;
863}
864
880// cppcheck-suppress unusedFunction
881size_t zxc_decompress_inplace_bound(const void* src, const size_t src_size) {
882 if (UNLIKELY(!src || src_size < ZXC_FRAME_MIN_SIZE)) return 0;
883 uint64_t dsize = 0;
884 uint64_t margin = 0;
885 uint64_t floor = 0;
886 if (UNLIKELY(zxc_inplace_probe((const uint8_t*)src, src_size, &dsize, &margin, &floor) !=
887 ZXC_OK))
888 return 0;
889 if (UNLIKELY(margin > (uint64_t)SIZE_MAX || dsize > (uint64_t)SIZE_MAX - margin)) return 0;
890 const uint64_t by_payload = dsize + margin;
891
892 if (UNLIKELY(floor > (uint64_t)SIZE_MAX - (uint64_t)src_size)) return 0;
893 const uint64_t by_placement = (uint64_t)src_size + floor;
894
895 return (size_t)(by_payload > by_placement ? by_payload : by_placement);
896}
897
910static int zxc_inplace_prepare(const void* buffer, const size_t buffer_capacity,
911 const size_t comp_size, const uint8_t** comp) {
912 if (UNLIKELY(!buffer || comp_size < ZXC_FRAME_MIN_SIZE || comp_size > buffer_capacity))
914 const uint8_t* const c = (const uint8_t*)buffer + (buffer_capacity - comp_size);
915 uint64_t dsize = 0;
916 uint64_t margin = 0;
917 uint64_t floor = 0;
918 const int rc = zxc_inplace_probe(c, comp_size, &dsize, &margin, &floor);
919 if (UNLIKELY(rc != ZXC_OK)) return rc;
920 if (UNLIKELY(dsize > (uint64_t)buffer_capacity || (uint64_t)buffer_capacity - dsize < margin))
922 /* The check above sizes the buffer against the payload, this one against the
923 * archive where it lies: block 0 starts a full block clear. */
924 if (UNLIKELY((uint64_t)(buffer_capacity - comp_size) < floor)) return ZXC_ERROR_DST_TOO_SMALL;
925 *comp = c;
926 return ZXC_OK;
927}
928
935uint64_t zxc_get_decompressed_size(const void* src, const size_t src_size) {
936 if (UNLIKELY(!src)) return 0;
937 const zxc_scan_src_t scan = zxc_scan_src_mem((const uint8_t*)src, src_size);
939 if (UNLIKELY(zxc_scan_container(&scan, 0, &info) != ZXC_OK)) return 0;
940 return info.dsize;
941}
942
949// cppcheck-suppress unusedFunction
950uint32_t zxc_get_dict_id(const void* src, const size_t src_size) {
951 if (UNLIKELY(!src || src_size < ZXC_FILE_HEADER_SIZE)) return 0;
952
953 const uint8_t* const p = (const uint8_t*)src;
954 if (UNLIKELY(zxc_le32(p) != ZXC_MAGIC_WORD)) return 0;
955
956 return (p[6] & ZXC_FILE_FLAG_HAS_DICTIONARY) ? zxc_le32(p + 7) : 0;
957}
958
964// cppcheck-suppress unusedFunction
965int zxc_requires_dict(const void* src, const size_t src_size) {
966 if (UNLIKELY(!src)) return ZXC_ERROR_NULL_INPUT;
967 size_t chunk = 0; // asked for so the block size code is checked too
968 int has_dict = 0;
969 const int rc =
970 zxc_read_file_header((const uint8_t*)src, src_size, &chunk, NULL, &has_dict, NULL, NULL);
971 return rc != ZXC_OK ? rc : has_dict;
972}
973
979// cppcheck-suppress unusedFunction
980int zxc_get_frame_info(const void* src, const size_t src_size, zxc_frame_info_t* info,
981 const size_t info_size) {
982 if (UNLIKELY(!info)) return ZXC_ERROR_NULL_INPUT;
984 const int rc = zxc_buffer_frame_info((const uint8_t*)src, src_size, &got, NULL);
985 if (rc == ZXC_OK) zxc_frame_info_copy(info, info_size, &got);
986 return rc;
987}
988
994// cppcheck-suppress unusedFunction
995int zxc_get_last_frame_info(const void* src, const size_t src_size, zxc_frame_info_t* info,
996 const size_t info_size) {
997 if (UNLIKELY(!src || !info)) return ZXC_ERROR_NULL_INPUT;
998 const zxc_scan_src_t scan = zxc_scan_src_mem((const uint8_t*)src, src_size);
1000 uint64_t start = 0;
1001 const int rc = zxc_scan_frame(&scan, src_size, &start, &got, NULL, NULL);
1002 if (rc == ZXC_OK) zxc_frame_info_copy(info, info_size, &got);
1003 return rc;
1004}
1005
1006// ============================================================================
1007// REUSABLE CONTEXT API (Opaque)
1008// ============================================================================
1009//
1010// Provides heap-allocated, opaque contexts that integrators can reuse across
1011// multiple compress / decompress calls, eliminating per-call malloc/free
1012// overhead.
1013
1014// --- Compression ---------------------------------------------------------
1015
1017typedef struct {
1018 int level;
1019 int checksum;
1020 int seekable;
1021 size_t block_size;
1022} zxc_cctx_opts_t;
1023
1032typedef struct {
1033 uint8_t* data; /* heap copy; NULL when none is attached */
1034 size_t size;
1035 uint8_t huf[ZXC_HUF_TABLE_SIZE]; /* copy of the shared table, when has_huf */
1036 int has_huf;
1037} zxc_dict_copy_t;
1038
1044static int zxc_dict_copy_set(zxc_dict_copy_t* c, const void* dict, const size_t dict_size,
1045 const void* dict_huf) {
1046 uint8_t* copy = NULL;
1047 if (dict && dict_size > 0) {
1048 if (UNLIKELY(dict_size > ZXC_DICT_SIZE_MAX)) return ZXC_ERROR_DICT_TOO_LARGE;
1049 copy = (uint8_t*)ZXC_MALLOC(dict_size);
1050 if (UNLIKELY(!copy)) return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
1051 ZXC_MEMCPY(copy, dict, dict_size);
1052 if (dict_huf) ZXC_MEMCPY(c->huf, dict_huf, ZXC_HUF_TABLE_SIZE);
1053 }
1054 ZXC_FREE(c->data);
1055 c->data = copy;
1056 c->size = copy ? dict_size : 0;
1057 c->has_huf = copy && dict_huf;
1058 return ZXC_OK;
1059}
1060
1061struct zxc_cctx_s {
1062 zxc_cctx_t inner; /* existing internal context */
1063 int initialized; /* 1 if inner has live allocations */
1064 int owns_workspace; /* 0 = library-allocated (free in zxc_free_cctx),
1065 1 = caller-supplied static workspace (no-op free,
1066 block_size pinned at init) */
1067 size_t last_block_size; /* block size used for last init */
1068 zxc_cparams_t params; /* in force; see zxc_cctx_set_param() */
1069 zxc_dict_copy_t dict_copy; /* what params.dict / dict_huf point into */
1070 int init_level; /* level a static workspace was carved for */
1071 int huf_cached; /* inner carries the table below */
1072 uint8_t huf_cache[ZXC_HUF_TABLE_SIZE]; /* last table attached */
1073};
1074
1082static int zxc_ctx_sync_dict_huf(zxc_cctx_t* RESTRICT inner, uint8_t* RESTRICT cache,
1083 int* RESTRICT cached, const uint8_t* RESTRICT dict_huf) {
1084 if (dict_huf) {
1085 if (!*cached || memcmp(cache, dict_huf, ZXC_HUF_TABLE_SIZE) != 0) {
1086 *cached = 0; /* a failed attach leaves inner without a tree */
1087 if (UNLIKELY(zxc_cctx_attach_dict_huf(inner, dict_huf) != ZXC_OK))
1089 ZXC_MEMCPY(cache, dict_huf, ZXC_HUF_TABLE_SIZE);
1090 *cached = 1;
1091 }
1092 } else if (*cached) {
1093 (void)zxc_cctx_attach_dict_huf(inner, NULL);
1094 *cached = 0;
1095 }
1096 return ZXC_OK;
1097}
1098
1101static zxc_cctx_opts_t zxc_cparams_resolve(const zxc_cparams_t* p) {
1102 return (zxc_cctx_opts_t){.level = p->level,
1103 .checksum = p->checksum_enabled,
1104 .seekable = p->seekable,
1105 .block_size = p->block_size};
1106}
1107
1110 return (zxc_cparams_t){.level = ZXC_LEVEL_DEFAULT, .block_size = ZXC_BLOCK_SIZE_DEFAULT};
1111}
1112
1114 zxc_cctx* const cctx = (zxc_cctx*)ZXC_CALLOC(1, sizeof(zxc_cctx));
1115 if (UNLIKELY(!cctx)) return NULL; // LCOV_EXCL_LINE
1116 cctx->params = zxc_cparams_defaults();
1117 cctx->init_level = ZXC_LEVEL_DEFAULT;
1118 return cctx;
1119}
1120
1121const zxc_cparams_t* zxc_cctx_params(const zxc_cctx* cctx) { return &cctx->params; }
1122
1123int zxc_cparam_bounds(const zxc_cparam_t param, int64_t* min, int64_t* max) {
1124 int64_t lo, hi;
1125 switch (param) {
1126 case ZXC_C_LEVEL:
1127 lo = ZXC_LEVEL_FASTEST;
1128 hi = ZXC_LEVEL_ULTRA;
1129 break;
1130 case ZXC_C_BLOCK_SIZE:
1131 lo = ZXC_BLOCK_SIZE_MIN;
1132 hi = ZXC_BLOCK_SIZE_MAX;
1133 break;
1134 case ZXC_C_THREADS:
1135 lo = 0;
1136 hi = ZXC_MAX_THREADS;
1137 break;
1138 case ZXC_C_CHECKSUM:
1139 case ZXC_C_SEEKABLE:
1140 lo = 0;
1141 hi = 1;
1142 break;
1143 default:
1144 return ZXC_ERROR_BAD_PARAM;
1145 }
1146 if (min) *min = lo;
1147 if (max) *max = hi;
1148 return ZXC_OK;
1149}
1150
1158int zxc_cctx_set_param(zxc_cctx* cctx, const zxc_cparam_t param, const int64_t value) {
1159 if (UNLIKELY(!cctx)) return ZXC_ERROR_NULL_INPUT;
1160 int64_t lo, hi;
1161 const int rc = zxc_cparam_bounds(param, &lo, &hi);
1162 if (UNLIKELY(rc != ZXC_OK)) return rc;
1163 if (value != 0 && (value < lo || value > hi)) {
1164 if (param == ZXC_C_LEVEL) return ZXC_ERROR_BAD_LEVEL;
1166 }
1167
1168 zxc_cparams_t* const p = &cctx->params;
1169 switch (param) {
1170 case ZXC_C_LEVEL: {
1171 // 0 restores the default: on a static workspace, the pinned level.
1172 const int level = value != 0 ? (int)value
1173 : cctx->owns_workspace ? cctx->init_level
1175 // A static workspace carved below the optimal-parser tier cannot grow into it.
1176 if (UNLIKELY(cctx->owns_workspace && level >= ZXC_LEVEL_DENSITY &&
1177 !cctx->inner.opt_scratch))
1178 return ZXC_ERROR_BAD_LEVEL;
1179 p->level = level;
1180 break;
1181 }
1182 case ZXC_C_BLOCK_SIZE: {
1183 const size_t block_size = value != 0 ? (size_t)value
1184 : cctx->owns_workspace ? cctx->last_block_size
1186 if (UNLIKELY(!zxc_validate_block_size(block_size))) return ZXC_ERROR_BAD_BLOCK_SIZE;
1187 if (UNLIKELY(cctx->owns_workspace && block_size != cctx->last_block_size))
1189 p->block_size = block_size;
1190 break;
1191 }
1192 case ZXC_C_THREADS:
1193 p->n_threads = (int)value;
1194 break;
1195 case ZXC_C_CHECKSUM:
1196 p->checksum_enabled = (int)value;
1197 break;
1198 case ZXC_C_SEEKABLE:
1199 p->seekable = (int)value;
1200 break;
1201 default:
1202 return ZXC_ERROR_BAD_PARAM; // LCOV_EXCL_LINE: bounds already refused it
1203 }
1204 return ZXC_OK;
1205}
1206
1207int zxc_cctx_get_param(const zxc_cctx* cctx, const zxc_cparam_t param, int64_t* value) {
1208 if (UNLIKELY(!cctx || !value)) return ZXC_ERROR_NULL_INPUT;
1209 const zxc_cparams_t* const p = &cctx->params;
1210 switch (param) {
1211 case ZXC_C_LEVEL:
1212 *value = p->level;
1213 break;
1214 case ZXC_C_BLOCK_SIZE:
1215 *value = (int64_t)p->block_size;
1216 break;
1217 case ZXC_C_THREADS:
1218 *value = p->n_threads;
1219 break;
1220 case ZXC_C_CHECKSUM:
1221 *value = p->checksum_enabled;
1222 break;
1223 case ZXC_C_SEEKABLE:
1224 *value = p->seekable;
1225 break;
1226 default:
1227 return ZXC_ERROR_BAD_PARAM;
1228 }
1229 return ZXC_OK;
1230}
1231
1232int zxc_cctx_set_dict(zxc_cctx* cctx, const void* dict, const size_t dict_size,
1233 const void* dict_huf) {
1234 if (UNLIKELY(!cctx)) return ZXC_ERROR_NULL_INPUT;
1235#ifdef ZXC_NO_DICT
1236 // No dictionary support: clearing is a no-op.
1237 if (UNLIKELY(dict && dict_size > 0)) return ZXC_ERROR_DICT_UNSUPPORTED;
1238#endif
1239 // No room for the dictionary prefix in a workspace carved without it.
1240 if (UNLIKELY(cctx->owns_workspace && dict && dict_size > 0)) return ZXC_ERROR_DICT_UNSUPPORTED;
1241 const int rc = zxc_dict_copy_set(&cctx->dict_copy, dict, dict_size, dict_huf);
1242 if (UNLIKELY(rc != ZXC_OK)) return rc;
1243 cctx->params.dict = cctx->dict_copy.data;
1244 cctx->params.dict_size = cctx->dict_copy.size;
1245 cctx->params.dict_huf = cctx->dict_copy.has_huf ? cctx->dict_copy.huf : NULL;
1246 return ZXC_OK;
1247}
1248
1249int zxc_cctx_set_progress(zxc_cctx* cctx, const zxc_progress_callback_t cb, void* user_data) {
1250 if (UNLIKELY(!cctx)) return ZXC_ERROR_NULL_INPUT;
1251 cctx->params.progress_cb = cb;
1252 cctx->params.user_data = cb ? user_data : NULL;
1253 return ZXC_OK;
1254}
1255
1257 if (UNLIKELY(!cctx)) return ZXC_ERROR_NULL_INPUT;
1258 (void)zxc_dict_copy_set(&cctx->dict_copy, NULL, 0, NULL);
1259 cctx->params = zxc_cparams_defaults();
1260 // A static workspace keeps what it was carved for.
1261 if (cctx->owns_workspace) {
1262 cctx->params.level = cctx->init_level;
1263 cctx->params.block_size = cctx->last_block_size;
1264 }
1265 return ZXC_OK;
1266}
1267
1276 if (UNLIKELY(!cctx)) return;
1277 // Static cctx: handle + inner buffers live inside the caller's workspace,
1278 // which we do not own. Free is a no-op; the caller owns the workspace.
1279 if (cctx->owns_workspace) return;
1280 if (cctx->initialized) zxc_cctx_free(&cctx->inner);
1281 ZXC_FREE(cctx->dict_copy.data);
1282 ZXC_FREE(cctx);
1283}
1284
1293static int64_t zxc_compress_cctx_resolved(zxc_cctx* cctx, const void* RESTRICT src,
1294 const size_t src_size, void* RESTRICT dst,
1295 const size_t dst_capacity, const zxc_cctx_opts_t o,
1296 const zxc_cparams_t* opts) {
1297 // The dictionary is the context's parameter; its bytes stay caller-owned.
1298 const uint8_t* dict = opts ? (const uint8_t*)opts->dict : NULL;
1299 const size_t dict_size = ZXC_OPTS_DICT_SIZE(opts);
1300 const uint8_t* dict_huf = ZXC_OPTS_DICT_HUF(opts);
1301
1302 // The encoder sees [dict | block], so the carved chunk covers both.
1303 const size_t eff_chunk =
1304 dict_size > 0 ? zxc_block_size_ceil(dict_size + o.block_size) : o.block_size;
1305 const int has_dict = dict && dict_size > 0;
1306 const uint32_t did = has_dict ? zxc_dict_compute_id(dict, dict_size, dict_huf) : 0;
1307
1308 // Empty source: same writer as the one-shot, workspace left alone.
1309 if (UNLIKELY(src_size == 0))
1310 return zxc_write_empty_frame((uint8_t*)dst, dst_capacity, o.block_size, o.checksum,
1311 has_dict, did, o.seekable);
1312
1313 // Re-init when the chunk changed, a level raise needs the optimal-parser
1314 // scratch, or a dictionary arrives on a context carved without its prefix.
1315 if (UNLIKELY(!cctx->initialized || cctx->last_block_size != eff_chunk ||
1316 (o.level >= ZXC_LEVEL_DENSITY && !cctx->inner.opt_scratch) ||
1317 (dict_size > 0 && !cctx->inner.dict_buffer))) {
1318 if (cctx->initialized) {
1319 zxc_cctx_free(&cctx->inner);
1320 cctx->initialized = 0;
1321 }
1322 // LCOV_EXCL_START
1323 if (UNLIKELY(zxc_cctx_init(&cctx->inner, eff_chunk, 1, o.level, o.checksum, dict_size) !=
1324 ZXC_OK))
1325 return ZXC_ERROR_MEMORY;
1326 // LCOV_EXCL_STOP
1327 cctx->last_block_size = eff_chunk;
1328 cctx->initialized = 1;
1329 cctx->huf_cached = 0;
1330 } else {
1331 // Same chunk: update level + checksum without realloc.
1332 cctx->inner.compression_level = o.level;
1333 cctx->inner.checksum_enabled = o.checksum;
1334 }
1335
1336 zxc_cctx_t* const ctx = &cctx->inner;
1337 ctx->dict_size = dict_size;
1338 if (UNLIKELY(zxc_ctx_sync_dict_huf(ctx, cctx->huf_cache, &cctx->huf_cached, dict_huf) !=
1339 ZXC_OK))
1341
1342 // [dict | block] input for the encoder, NULL without a dictionary.
1343 uint8_t* const dict_input = dict_size > 0 ? ctx->dict_buffer : NULL;
1344 if (dict_input) ZXC_MEMCPY(dict_input, dict, dict_size);
1345
1346 uint8_t* op = (uint8_t*)dst;
1347 const uint8_t* const op_start = op;
1348 const uint8_t* const op_end = op + dst_capacity;
1349 const uint8_t* const ip = (const uint8_t*)src;
1350
1351 const int h_val = zxc_write_file_header(op, (size_t)(op_end - op), o.block_size, o.checksum,
1352 has_dict, did, o.seekable);
1353 if (UNLIKELY(h_val < 0)) return h_val; // LCOV_EXCL_LINE
1354 op += h_val;
1355
1356 uint64_t digest = 0;
1357 size_t pos = 0;
1358 uint64_t bi = 0;
1359 for (; pos < src_size; bi++) {
1360 const size_t chunk_len = (src_size - pos > o.block_size) ? o.block_size : (src_size - pos);
1361 const size_t rem_cap = (size_t)(op_end - op);
1362
1363 int res;
1364 if (dict_input) {
1365 ZXC_MEMCPY(dict_input + dict_size, ip + pos, chunk_len);
1366 res =
1367 zxc_compress_chunk_wrapper(ctx, dict_input, dict_size + chunk_len, op, rem_cap, bi);
1368 } else {
1369 res = zxc_compress_chunk_wrapper(ctx, ip + pos, chunk_len, op, rem_cap, bi);
1370 }
1371 if (UNLIKELY(res < 0)) return res;
1372
1373 if (o.checksum && LIKELY(res >= ZXC_BLOCK_CHECKSUM_SIZE))
1374 digest = zxc_digest_combine(digest, zxc_le32(op + res - ZXC_BLOCK_CHECKSUM_SIZE));
1375
1376 op += res;
1377 pos += chunk_len;
1378 }
1379 // EOF block
1380 uint8_t* const eof_at = op;
1381 const size_t rem_cap = (size_t)(op_end - op);
1382 const zxc_block_header_t eof_bh = {
1383 .block_type = ZXC_BLOCK_EOF, .block_flags = 0, .reserved = 0, .comp_size = 0};
1384 const int eof_val = zxc_write_block_header(op, rem_cap, &eof_bh);
1385 if (UNLIKELY(eof_val < 0)) return eof_val; // LCOV_EXCL_LINE
1386 op += eof_val;
1387
1388 // Seek table between the EOF block and the footer.
1389 if (o.seekable) {
1390 const int64_t st_val = zxc_write_seek_table_from_blocks(
1391 op, (size_t)(op_end - op), op_start + h_val, eof_at, bi, o.checksum);
1392 if (UNLIKELY(st_val < 0)) return st_val;
1393 op += st_val;
1394 }
1395
1396 const int footer_val = zxc_write_file_footer(
1397 op, (size_t)(op_end - op), (uint64_t)(op - op_start), src_size, digest, o.checksum);
1398 if (UNLIKELY(footer_val < 0)) return footer_val;
1399 op += footer_val;
1400
1401 return (int64_t)(op - op_start);
1402}
1403
1410int64_t zxc_compress_cctx(zxc_cctx* cctx, const void* RESTRICT src, const size_t src_size,
1411 void* RESTRICT dst, const size_t dst_capacity) {
1412 if (UNLIKELY(!cctx || !dst || dst_capacity == 0 || (src_size > 0 && !src)))
1413 return ZXC_ERROR_NULL_INPUT;
1414
1415 const zxc_cctx_opts_t o = zxc_cparams_resolve(&cctx->params);
1416 return zxc_compress_cctx_resolved(cctx, src, src_size, dst, dst_capacity, o, &cctx->params);
1417}
1418
1419// --- Decompression -------------------------------------------------------
1420
1427struct zxc_dctx_s {
1428 zxc_cctx_t inner; /* reuses the same internal context type */
1429 size_t last_block_size; /* block size from last header parse */
1430 size_t last_dict_size; /* dict_size the inner buffer was carved for (drives re-init) */
1431 int initialized; /* 1 if inner has live allocations */
1432 int owns_workspace; /* 0 = library-allocated (free in zxc_free_dctx),
1433 1 = caller-supplied static workspace (no-op free,
1434 block_size pinned at init) */
1435 int huf_cached; /* inner carries the table below */
1436 zxc_dparams_t params; /* in force; see zxc_dctx_set_param() */
1437 zxc_dict_copy_t dict_copy; /* what params.dict / dict_huf point into */
1438 uint8_t huf_cache[ZXC_HUF_TABLE_SIZE]; /* last table attached */
1439};
1440
1442 zxc_dctx* const dctx = (zxc_dctx*)ZXC_CALLOC(1, sizeof(zxc_dctx));
1443 return dctx;
1444}
1445
1446const zxc_dparams_t* zxc_dctx_params(const zxc_dctx* dctx) { return &dctx->params; }
1447
1448int zxc_dparam_bounds(const zxc_dparam_t param, int64_t* min, int64_t* max) {
1449 int64_t lo, hi;
1450 switch (param) {
1451 case ZXC_D_THREADS:
1452 lo = 0;
1453 hi = ZXC_MAX_THREADS;
1454 break;
1455 case ZXC_D_CHECKSUM:
1456 lo = 0;
1457 hi = 1;
1458 break;
1459 default:
1460 return ZXC_ERROR_BAD_PARAM;
1461 }
1462 if (min) *min = lo;
1463 if (max) *max = hi;
1464 return ZXC_OK;
1465}
1466
1467int zxc_dctx_set_param(zxc_dctx* dctx, const zxc_dparam_t param, const int64_t value) {
1468 if (UNLIKELY(!dctx)) return ZXC_ERROR_NULL_INPUT;
1469 int64_t lo, hi;
1470 const int rc = zxc_dparam_bounds(param, &lo, &hi);
1471 if (UNLIKELY(rc != ZXC_OK)) return rc;
1472 if (value != 0 && (value < lo || value > hi)) return ZXC_ERROR_BAD_PARAM;
1473 switch (param) {
1474 case ZXC_D_THREADS:
1475 dctx->params.n_threads = (int)value;
1476 break;
1477 case ZXC_D_CHECKSUM:
1478 dctx->params.checksum_enabled = (int)value;
1479 break;
1480 default:
1481 return ZXC_ERROR_BAD_PARAM; // LCOV_EXCL_LINE: bounds already refused it
1482 }
1483 return ZXC_OK;
1484}
1485
1486int zxc_dctx_get_param(const zxc_dctx* dctx, const zxc_dparam_t param, int64_t* value) {
1487 if (UNLIKELY(!dctx || !value)) return ZXC_ERROR_NULL_INPUT;
1488 switch (param) {
1489 case ZXC_D_THREADS:
1490 *value = dctx->params.n_threads;
1491 break;
1492 case ZXC_D_CHECKSUM:
1493 *value = dctx->params.checksum_enabled;
1494 break;
1495 default:
1496 return ZXC_ERROR_BAD_PARAM;
1497 }
1498 return ZXC_OK;
1499}
1500
1501int zxc_dctx_set_dict(zxc_dctx* dctx, const void* dict, const size_t dict_size,
1502 const void* dict_huf) {
1503 if (UNLIKELY(!dctx)) return ZXC_ERROR_NULL_INPUT;
1504#ifdef ZXC_NO_DICT
1505 // No dictionary support: clearing is a no-op.
1506 if (UNLIKELY(dict && dict_size > 0)) return ZXC_ERROR_DICT_UNSUPPORTED;
1507#endif
1508 if (UNLIKELY(dctx->owns_workspace && dict && dict_size > 0)) return ZXC_ERROR_DICT_UNSUPPORTED;
1509 const int rc = zxc_dict_copy_set(&dctx->dict_copy, dict, dict_size, dict_huf);
1510 if (UNLIKELY(rc != ZXC_OK)) return rc;
1511 dctx->params.dict = dctx->dict_copy.data;
1512 dctx->params.dict_size = dctx->dict_copy.size;
1513 dctx->params.dict_huf = dctx->dict_copy.has_huf ? dctx->dict_copy.huf : NULL;
1514 return ZXC_OK;
1515}
1516
1517int zxc_dctx_set_progress(zxc_dctx* dctx, const zxc_progress_callback_t cb, void* user_data) {
1518 if (UNLIKELY(!dctx)) return ZXC_ERROR_NULL_INPUT;
1519 dctx->params.progress_cb = cb;
1520 dctx->params.user_data = cb ? user_data : NULL;
1521 return ZXC_OK;
1522}
1523
1525 if (UNLIKELY(!dctx)) return ZXC_ERROR_NULL_INPUT;
1526 (void)zxc_dict_copy_set(&dctx->dict_copy, NULL, 0, NULL);
1527 ZXC_MEMSET(&dctx->params, 0, sizeof(dctx->params));
1528 return ZXC_OK;
1529}
1530
1538 if (UNLIKELY(!dctx)) return;
1539 // Static dctx: handle + inner buffers live inside the caller's workspace,
1540 // which we do not own. Free is a no-op; the caller owns the workspace.
1541 if (dctx->owns_workspace) return;
1542 if (dctx->initialized) zxc_cctx_free(&dctx->inner);
1543 ZXC_FREE(dctx->dict_copy.data);
1544 ZXC_FREE(dctx);
1545}
1546
1555static int64_t zxc_dctx_probe(const zxc_dctx* dctx, const uint8_t* RESTRICT src,
1556 const size_t src_size, const zxc_dparams_t* opts) {
1557 const size_t dict_size = ZXC_OPTS_DICT_SIZE(opts);
1558 const zxc_scan_src_t scan = zxc_scan_src_mem(src, src_size);
1560 const int rc =
1561 zxc_scan_container(&scan, dctx->owns_workspace ? dctx->last_block_size : 0, &info);
1562 if (UNLIKELY(rc != ZXC_OK)) return rc;
1563 if (UNLIKELY(dctx->owns_workspace && (info.uses_dict || dict_size != 0)))
1565 return zxc_probe_without_dst(src, src_size, opts, &info);
1566}
1567
1575int64_t zxc_decompress_dctx(zxc_dctx* dctx, const void* RESTRICT src, const size_t src_size,
1576 void* RESTRICT dst, const size_t dst_capacity) {
1577 const zxc_dparams_t* const opts = dctx ? &dctx->params : NULL;
1578 if (UNLIKELY(!dctx || !src || (!dst && dst_capacity != 0))) return ZXC_ERROR_NULL_INPUT;
1579 if (UNLIKELY(src_size < ZXC_FRAME_MIN_SIZE))
1580 return zxc_short_input_verdict((const uint8_t*)src, src_size);
1581 if (UNLIKELY(!dst || dst_capacity == 0)) return zxc_dctx_probe(dctx, src, src_size, opts);
1582 return zxc_dctx_decode_container(dctx, (const uint8_t*)src, src_size, (uint8_t*)dst,
1583 dst_capacity, opts);
1584}
1585
1594int64_t zxc_decompress_inplace_dctx(zxc_dctx* dctx, void* buffer, const size_t buffer_capacity,
1595 const size_t comp_size) {
1596 const zxc_dparams_t* const opts = dctx ? &dctx->params : NULL;
1597 if (UNLIKELY(!dctx)) return ZXC_ERROR_NULL_INPUT;
1598 const uint8_t* comp = NULL;
1599 const int rc = zxc_inplace_prepare(buffer, buffer_capacity, comp_size, &comp);
1600 if (UNLIKELY(rc != ZXC_OK)) return rc;
1601 return zxc_dctx_decode_frame(dctx, comp, comp_size, (uint8_t*)buffer, buffer_capacity, opts, 1,
1602 NULL);
1603}
1604
1612static int zxc_dctx_carve(zxc_dctx* dctx, const size_t chunk_size, const size_t dict_size,
1613 const int checksum, const uint8_t* dict_huf) {
1614 if (UNLIKELY(!dctx->initialized || dctx->last_block_size != chunk_size ||
1615 dctx->last_dict_size != dict_size)) {
1616 if (dctx->initialized) {
1617 zxc_cctx_free(&dctx->inner);
1618 dctx->initialized = 0;
1619 }
1620 // LCOV_EXCL_START
1621 if (UNLIKELY(zxc_cctx_init(&dctx->inner, chunk_size, 0, 0, checksum, dict_size) != ZXC_OK))
1622 return ZXC_ERROR_MEMORY;
1623 // LCOV_EXCL_STOP
1624 dctx->last_block_size = chunk_size;
1625 dctx->last_dict_size = dict_size;
1626 dctx->initialized = 1;
1627 dctx->huf_cached = 0;
1628 } else {
1629 dctx->inner.checksum_enabled = checksum;
1630 }
1631 if (UNLIKELY(zxc_ctx_sync_dict_huf(&dctx->inner, dctx->huf_cache, &dctx->huf_cached,
1632 dict_huf) != ZXC_OK))
1634 return ZXC_OK;
1635}
1636
1646static int64_t zxc_dctx_decode_frame(zxc_dctx* dctx, const uint8_t* src, const size_t src_size,
1647 uint8_t* dst, const size_t dst_capacity,
1648 const zxc_dparams_t* opts, const int inplace,
1649 size_t* frame_end) {
1650 const int checksum_enabled = opts ? opts->checksum_enabled : 0;
1651 const uint8_t* dict = opts ? (const uint8_t*)opts->dict : NULL;
1652 size_t dict_size = ZXC_OPTS_DICT_SIZE(opts);
1653 const uint8_t* dict_huf = ZXC_OPTS_DICT_HUF(opts);
1654
1655 const uint8_t* ip = (const uint8_t*)src;
1656 const uint8_t* const ip_end = ip + src_size;
1657 uint8_t* op = (uint8_t*)dst;
1658 const uint8_t* const op_start = op;
1659 const uint8_t* const op_end = op + dst_capacity;
1660 size_t runtime_chunk_size = 0;
1661 int file_has_checksums = 0;
1662 int file_has_seek = 0;
1663 int header_has_dict = 0;
1664 uint32_t header_dict_id = 0;
1665
1666 const int hrc = zxc_read_file_header(ip, src_size, &runtime_chunk_size, &file_has_checksums,
1667 &header_has_dict, &header_dict_id, &file_has_seek);
1668 if (UNLIKELY(hrc != ZXC_OK)) return hrc;
1669
1670 // Static dctx: block_size is locked at workspace init; reject any
1671 // archive whose declared block_size would require a re-partition.
1672 if (UNLIKELY(dctx->owns_workspace && runtime_chunk_size != dctx->last_block_size))
1674 // Static dctx: no room for a dictionary prefix.
1675 if (UNLIKELY(dctx->owns_workspace && (header_has_dict || dict_size != 0)))
1677
1678 // Dictionary binding: same contract as zxc_decompress().
1679 if (header_has_dict) {
1680#ifdef ZXC_NO_DICT
1682#endif
1683 if (UNLIKELY(!dict || dict_size == 0)) return ZXC_ERROR_DICT_REQUIRED;
1684 if (UNLIKELY(zxc_dict_compute_id(dict, dict_size, dict_huf) != header_dict_id))
1686 } else {
1687 // A frame without the flag decodes as if no dictionary were attached: no
1688 // back-reference into the prefix, no shared literal table.
1689 dict = NULL;
1690 dict_size = 0;
1691 dict_huf = NULL;
1692 }
1693
1695
1696 // work_buf is sized to this by the carve below.
1697 const size_t work_sz = runtime_chunk_size + ZXC_DECOMPRESS_TAIL_PAD;
1698 zxc_cctx_t* const ctx = &dctx->inner;
1699
1700 // Carved on the first block that needs it: an archive storing nothing, and a
1701 // no-destination probe of one, never allocate.
1702 int ctx_ready = 0;
1703 // [dict | decode + PAD] scratch, NULL without a dictionary.
1704 uint8_t* dict_dec = NULL;
1705
1706 uint64_t block_index = 0;
1707 uint64_t digest = 0;
1708 for (;;) {
1709 // Only the EOF block may end the walk.
1710 if (UNLIKELY(ip >= ip_end)) return ZXC_ERROR_CORRUPT_DATA;
1711 const size_t rem_src = (size_t)(ip_end - ip);
1713 if (UNLIKELY(zxc_read_block_header(ip, rem_src, &bh) != ZXC_OK))
1714 return ZXC_ERROR_BAD_HEADER;
1715
1716 if (UNLIKELY(bh.block_type == ZXC_BLOCK_EOF)) {
1717 if (UNLIKELY(zxc_check_eof_header(ip) != ZXC_OK)) return ZXC_ERROR_BAD_HEADER;
1718
1719 // After the EOF block: the SEK block when announced, then the footer
1720 // the frame implies. Another frame may follow it.
1721 const uint64_t total_out = (uint64_t)(op - op_start);
1722 size_t pos = (size_t)(ip - src) + ZXC_BLOCK_HEADER_SIZE;
1723 if (file_has_seek) {
1724 uint64_t sek_bytes = 0;
1725 const int src_rc = zxc_check_seek_header(src + pos, src_size - pos, total_out,
1726 runtime_chunk_size, &sek_bytes);
1727 if (UNLIKELY(src_rc != ZXC_OK)) return src_rc;
1728 pos += ZXC_BLOCK_HEADER_SIZE;
1729 if (UNLIKELY(sek_bytes > (uint64_t)(src_size - pos)))
1731 pos += (size_t)sek_bytes;
1732 }
1733 const zxc_footer_layout_t fl = zxc_footer_layout(pos, total_out, file_has_checksums);
1734 uint64_t stored_digest = 0;
1735 const int frc =
1736 zxc_check_file_footer(src + pos, src_size - pos, &fl, total_out, &stored_digest);
1737 if (UNLIKELY(frc != ZXC_OK)) return frc;
1738 if (checksum_enabled && file_has_checksums && UNLIKELY(stored_digest != digest))
1740 pos += fl.len;
1741 // In place, the archive is one frame flush against the buffer's end.
1742 if (frame_end)
1743 *frame_end = pos;
1744 else if (UNLIKELY(pos != src_size))
1746 break; // EOF reached, stop decoding
1747 }
1748
1749 // The decoder only requires the payload, the step also covers the
1750 // checksum.
1751 const size_t advance = ZXC_BLOCK_HEADER_SIZE + bh.comp_size +
1752 (file_has_checksums ? ZXC_BLOCK_CHECKSUM_SIZE : 0);
1753 if (UNLIKELY(advance > rem_src)) return ZXC_ERROR_SRC_TOO_SMALL;
1754
1755 if (UNLIKELY(!ctx_ready)) {
1756 const int crc = zxc_dctx_carve(dctx, runtime_chunk_size, dict_size,
1757 file_has_checksums && checksum_enabled, dict_huf);
1758 if (UNLIKELY(crc != ZXC_OK)) return crc;
1759 dict_dec = ctx->dict_buffer;
1760 if (dict_dec) ZXC_MEMCPY(dict_dec, dict, dict_size);
1761 ctx_ready = 1;
1762 }
1763
1764 // The block's room: the rest of dst, in place cut at the unread input. The
1765 // fast decoder never writes past its capacity; a block that does not fit
1766 // decodes aside and copies only its real output.
1767 const size_t rem_cap = (size_t)(op_end - op);
1768 size_t room = rem_cap;
1769 if (inplace) {
1770 const size_t ahead = ip > op ? (size_t)(ip - op) : 0;
1771 if (ahead < room) room = ahead;
1772 }
1773 int res;
1774 const uint8_t* bounce = NULL;
1775 if (dict_dec) {
1776 // Decode behind the prefix so back-references into it resolve.
1777 res = zxc_decompress_chunk_wrapper(ctx, ip, rem_src, dict_dec + dict_size, work_sz,
1778 block_index);
1779 bounce = dict_dec + dict_size;
1780 } else if (LIKELY(room >= work_sz)) {
1781 res = zxc_decompress_chunk_wrapper(ctx, ip, rem_src, op, work_sz, block_index);
1782 } else {
1783 res = zxc_decompress_chunk_wrapper(ctx, ip, rem_src, ctx->work_buf, ctx->work_buf_cap,
1784 block_index);
1785 bounce = ctx->work_buf;
1786 }
1787 if (bounce && LIKELY(res > 0)) {
1788 // A no-destination probe lands here too. In place, output past the
1789 // room means padding or forged sizes.
1790 if (UNLIKELY((size_t)res > room))
1791 return (size_t)res > rem_cap ? ZXC_ERROR_DST_TOO_SMALL : ZXC_ERROR_CORRUPT_DATA;
1792 ZXC_MEMCPY(op, bounce, (size_t)res);
1793 }
1794 if (UNLIKELY(res < 0)) return res;
1795
1796 if (checksum_enabled && file_has_checksums)
1797 digest =
1799
1800 ip += advance;
1801 op += res;
1802 block_index++;
1803 }
1804
1805 return (int64_t)(op - op_start);
1806}
1807
1809static int64_t zxc_dctx_decode_container(zxc_dctx* dctx, const uint8_t* src, const size_t src_size,
1810 uint8_t* dst, const size_t dst_capacity,
1811 const zxc_dparams_t* opts) {
1812 const zxc_scan_src_t scan = zxc_scan_src_mem(src, src_size);
1813 uint64_t at = 0;
1814 size_t out = 0;
1815 int rc;
1816 while ((rc = zxc_container_next(&scan, at)) == 1) {
1817 size_t frame_size = 0;
1818 const int64_t res = zxc_dctx_decode_frame(dctx, src + at, src_size - (size_t)at, dst + out,
1819 dst_capacity - out, opts, 0, &frame_size);
1820 if (UNLIKELY(res < 0)) return res;
1821 out += (size_t)res;
1822 at += frame_size;
1823 }
1824 if (UNLIKELY(rc < 0)) return rc;
1825 if (UNLIKELY(at == 0)) return ZXC_ERROR_SRC_TOO_SMALL;
1826 return (int64_t)out;
1827}
1828
1829// One-shot decode: the same walk, on a context that lives for the call.
1830static int64_t zxc_decompress_oneshot(const uint8_t* src, const size_t src_size, uint8_t* dst,
1831 const size_t dst_capacity, const zxc_dparams_t* opts) {
1832 zxc_dctx dctx;
1833 ZXC_MEMSET(&dctx, 0, sizeof(dctx));
1834 const int64_t res = zxc_dctx_decode_container(&dctx, src, src_size, dst, dst_capacity, opts);
1835 if (dctx.initialized) zxc_cctx_free(&dctx.inner);
1836 return res;
1837}
1838
1839// =========================================================================
1840// Block-Level API (no file framing)
1841// =========================================================================
1842
1853static int64_t zxc_compress_block_resolved(zxc_cctx* cctx, const void* RESTRICT src,
1854 const size_t src_size, void* RESTRICT dst,
1855 const size_t dst_capacity, const zxc_cctx_opts_t o,
1856 const zxc_cparams_t* opts, const uint64_t block_index) {
1857 // When a dictionary is active, offset_bits must accommodate dict + block.
1858 const uint8_t* b_dict = opts ? (const uint8_t*)opts->dict : NULL;
1859 const size_t b_dict_size = ZXC_OPTS_DICT_SIZE(opts);
1860 const size_t effective_block_size =
1861 b_dict_size > 0 ? zxc_block_size_ceil(b_dict_size + o.block_size) : o.block_size;
1862
1863 // Static cctx: the setters already refuse a dictionary, another block size
1864 // and a level raise, but a source larger than the pinned block would still
1865 // force a re-carve the workspace cannot hold.
1866 if (UNLIKELY(cctx->owns_workspace && effective_block_size != cctx->last_block_size))
1868
1869 // Re-init when [dict | block] outgrows the workspace (a larger one encodes
1870 // the same, so a smaller block keeps it), a level raise needs the
1871 // optimal-parser scratch, or a dictionary meets a context carved without it.
1872 if (UNLIKELY(!cctx->initialized || cctx->last_block_size < effective_block_size ||
1873 (o.level >= ZXC_LEVEL_DENSITY && !cctx->inner.opt_scratch) ||
1874 (b_dict_size > 0 && !cctx->inner.dict_buffer))) {
1875 if (cctx->initialized) {
1876 // LCOV_EXCL_START
1877 zxc_cctx_free(&cctx->inner);
1878 cctx->initialized = 0;
1879 // LCOV_EXCL_STOP
1880 }
1881 // LCOV_EXCL_START
1882 if (UNLIKELY(zxc_cctx_init(&cctx->inner, effective_block_size, 1, o.level, o.checksum,
1883 b_dict_size) != ZXC_OK))
1884 return ZXC_ERROR_MEMORY;
1885 // LCOV_EXCL_STOP
1886 cctx->last_block_size = effective_block_size;
1887 cctx->initialized = 1;
1888 cctx->huf_cached = 0;
1889 } else {
1890 cctx->inner.compression_level = o.level;
1891 cctx->inner.checksum_enabled = o.checksum;
1892 }
1893
1894 cctx->inner.dict_size = b_dict_size;
1895 if (UNLIKELY(zxc_ctx_sync_dict_huf(&cctx->inner, cctx->huf_cache, &cctx->huf_cached,
1896 ZXC_OPTS_DICT_HUF(opts)) != ZXC_OK))
1897 return ZXC_ERROR_CORRUPT_DATA; // LCOV_EXCL_LINE
1898
1899 int res;
1900 if (b_dict && b_dict_size > 0) {
1901 // [dict | block] assembled in the cctx-owned dict_buffer
1902 uint8_t* const combined = cctx->inner.dict_buffer;
1903 ZXC_MEMCPY(combined, b_dict, b_dict_size);
1904 ZXC_MEMCPY(combined + b_dict_size, src, src_size);
1905 res = zxc_compress_chunk_wrapper(&cctx->inner, combined, b_dict_size + src_size,
1906 (uint8_t*)dst, dst_capacity, block_index);
1907 } else {
1908 res = zxc_compress_chunk_wrapper(&cctx->inner, (const uint8_t*)src, src_size, (uint8_t*)dst,
1909 dst_capacity, block_index);
1910 }
1911 if (UNLIKELY(res < 0)) return res;
1912 return (int64_t)res;
1913}
1914
1923int64_t zxc_compress_block(zxc_cctx* cctx, const void* RESTRICT src, const size_t src_size,
1924 void* RESTRICT dst, const size_t dst_capacity) {
1925 return zxc_compress_block_at(cctx, src, src_size, dst, dst_capacity, NULL, 0);
1926}
1927
1928int64_t zxc_compress_block_at(zxc_cctx* cctx, const void* RESTRICT src, const size_t src_size,
1929 void* RESTRICT dst, const size_t dst_capacity,
1930 const zxc_cparams_t* opts, const uint64_t block_index) {
1931 if (UNLIKELY(!cctx || !src || !dst || src_size == 0 || dst_capacity == 0))
1932 return ZXC_ERROR_NULL_INPUT;
1933 if (UNLIKELY(src_size > ZXC_BLOCK_SIZE_MAX)) return ZXC_ERROR_BAD_BLOCK_SIZE;
1934
1935 const zxc_cparams_t* const cp = opts ? opts : &cctx->params;
1936 zxc_cctx_opts_t o = zxc_cparams_resolve(cp);
1937 // Carve for the source if block_size is smaller; remember block_size as asked.
1938 zxc_cctx_opts_t carve = o;
1939 const size_t min_bs = zxc_block_size_ceil(src_size);
1940 if (carve.block_size < min_bs) carve.block_size = min_bs;
1941
1942 return zxc_compress_block_resolved(cctx, src, src_size, dst, dst_capacity, carve, cp,
1943 block_index);
1944}
1945
1951static int64_t zxc_decode_bounce(zxc_cctx_t* RESTRICT ctx, const uint8_t* RESTRICT src,
1952 const size_t src_size, uint8_t* RESTRICT dst,
1953 const size_t dst_capacity, const size_t carved) {
1954 // Block API only: frameless blocks carry index 0.
1955 const int res =
1956 zxc_decompress_chunk_wrapper(ctx, src, src_size, ctx->work_buf, ctx->work_buf_cap, 0);
1957 if (UNLIKELY(res < 0)) return res;
1958 if (UNLIKELY(carved != 0 && (size_t)res > carved)) return ZXC_ERROR_BAD_BLOCK_SIZE;
1959 if (UNLIKELY((size_t)res > dst_capacity)) return ZXC_ERROR_DST_TOO_SMALL;
1960 ZXC_MEMCPY(dst, ctx->work_buf, (size_t)res);
1961 return res;
1962}
1963
1971static int64_t zxc_static_decompress_block(zxc_dctx* RESTRICT dctx, const uint8_t* RESTRICT src,
1972 const size_t src_size, uint8_t* RESTRICT dst,
1973 const size_t dst_capacity, const int checksum_enabled,
1974 const int strict) {
1975 const size_t carved = dctx->last_block_size;
1976 const size_t work_sz = carved + ZXC_DECOMPRESS_TAIL_PAD;
1977 zxc_cctx_t* const ctx = &dctx->inner;
1978 ctx->checksum_enabled = checksum_enabled;
1979 ctx->dict_size = 0;
1980 int res;
1981 if (strict) {
1982 const size_t cap = dst_capacity < work_sz ? dst_capacity : work_sz;
1983 res = zxc_decompress_chunk_wrapper_safe_public(ctx, src, src_size, dst, cap, 0);
1984 } else if (dst_capacity >= work_sz) {
1985 res = zxc_decompress_chunk_wrapper(ctx, src, src_size, dst, work_sz, 0);
1986 } else {
1987 // Bounce through work_buf when dst cannot absorb wild copies.
1988 return zxc_decode_bounce(ctx, src, src_size, dst, dst_capacity, carved);
1989 }
1990 if (UNLIKELY(res > 0 && (size_t)res > carved)) return ZXC_ERROR_BAD_BLOCK_SIZE;
1991 return res;
1992}
1993
1998static int zxc_dctx_prepare(zxc_dctx* RESTRICT dctx, const size_t block_size,
1999 const size_t dict_size, const int checksum_enabled) {
2000 if (LIKELY(dctx->initialized && dctx->last_block_size == block_size &&
2001 dctx->last_dict_size == dict_size)) {
2002 dctx->inner.checksum_enabled = checksum_enabled;
2003 return ZXC_OK;
2004 }
2005 if (dctx->initialized) {
2006 zxc_cctx_free(&dctx->inner);
2007 dctx->initialized = 0;
2008 }
2009 // LCOV_EXCL_START
2010 if (UNLIKELY(zxc_cctx_init(&dctx->inner, block_size, 0, 0, checksum_enabled, dict_size) !=
2011 ZXC_OK))
2012 return ZXC_ERROR_MEMORY;
2013 // LCOV_EXCL_STOP
2014 dctx->last_block_size = block_size;
2015 dctx->last_dict_size = dict_size;
2016 dctx->initialized = 1;
2017 dctx->huf_cached = 0;
2018 return ZXC_OK;
2019}
2020
2031int64_t zxc_decompress_block(zxc_dctx* dctx, const void* RESTRICT src, const size_t src_size,
2032 void* RESTRICT dst, const size_t dst_capacity) {
2033 const zxc_dparams_t* const opts = dctx ? &dctx->params : NULL;
2034 if (UNLIKELY(!dctx || !src || !dst || src_size < ZXC_BLOCK_HEADER_SIZE || dst_capacity == 0))
2035 return ZXC_ERROR_NULL_INPUT;
2036
2037 // One format-conformant block, so the payload cannot exceed
2038 // ZXC_BLOCK_SIZE_MAX; dst_capacity is bounded to that plus the tail-pad the
2039 // wild copies need. Larger outputs belong to the frame or streaming APIs.
2040 if (UNLIKELY(dst_capacity > ZXC_BLOCK_SIZE_MAX + ZXC_DECOMPRESS_TAIL_PAD))
2042
2043 const int checksum_enabled = opts ? opts->checksum_enabled : 0;
2044
2045 const uint8_t* dict = opts ? (const uint8_t*)opts->dict : NULL;
2046 const size_t dict_size = ZXC_OPTS_DICT_SIZE(opts);
2047 if (UNLIKELY(dctx->owns_workspace))
2048 return zxc_static_decompress_block(dctx, (const uint8_t*)src, src_size, (uint8_t*)dst,
2049 dst_capacity, checksum_enabled, 0);
2050
2051 // Carved for the block dst can hold. The tail pad must not round the carve
2052 // up to 4 MiB: its buffers would then outgrow ZXC_VARINT_INVALID.
2053 const size_t block_size =
2054 zxc_block_size_ceil(dst_capacity < ZXC_BLOCK_SIZE_MAX ? dst_capacity : ZXC_BLOCK_SIZE_MAX);
2055 const int rc = zxc_dctx_prepare(dctx, block_size, dict_size, checksum_enabled);
2056 if (UNLIKELY(rc != ZXC_OK)) return rc; // LCOV_EXCL_LINE
2057
2058 zxc_cctx_t* const ctx = &dctx->inner;
2059 ctx->dict_size = dict_size;
2060 if (UNLIKELY(zxc_ctx_sync_dict_huf(ctx, dctx->huf_cache, &dctx->huf_cached,
2061 ZXC_OPTS_DICT_HUF(opts)) != ZXC_OK))
2062 return ZXC_ERROR_CORRUPT_DATA; // LCOV_EXCL_LINE
2063
2064 // The [dict | decode] buffer carries block_size + ZXC_DECOMPRESS_TAIL_PAD.
2065 const size_t work_sz = block_size + ZXC_DECOMPRESS_TAIL_PAD;
2066
2067 int res;
2068 if (dict && dict_size > 0) {
2069 // [dict | decode] assembled in the cctx-owned dict_buffer
2070 uint8_t* const dec_buf = ctx->dict_buffer;
2071 ZXC_MEMCPY(dec_buf, dict, dict_size);
2072 res = zxc_decompress_chunk_wrapper(ctx, (const uint8_t*)src, src_size, dec_buf + dict_size,
2073 work_sz, 0);
2074 if (LIKELY(res > 0)) {
2075 if (UNLIKELY((size_t)res > dst_capacity)) return ZXC_ERROR_DST_TOO_SMALL;
2076 ZXC_MEMCPY(dst, dec_buf + dict_size, (size_t)res);
2077 }
2078 } else {
2079 res = zxc_decompress_chunk_wrapper(ctx, (const uint8_t*)src, src_size, (uint8_t*)dst,
2080 dst_capacity, 0);
2081 // A tight tail aborts a block that fits: work_buf has the margin.
2082 // Corrupt blocks report OVERFLOW too, and pay the second decode.
2083 if (UNLIKELY(res == ZXC_ERROR_OVERFLOW))
2084 return zxc_decode_bounce(ctx, (const uint8_t*)src, src_size, (uint8_t*)dst,
2085 dst_capacity, 0);
2086 }
2087 if (UNLIKELY(res < 0)) return res;
2088 return (int64_t)res;
2089}
2090
2100int64_t zxc_decompress_block_safe(zxc_dctx* dctx, const void* RESTRICT src, const size_t src_size,
2101 void* RESTRICT dst, const size_t dst_capacity) {
2102 const zxc_dparams_t* const opts = dctx ? &dctx->params : NULL;
2103 if (UNLIKELY(!dctx || !src || !dst || src_size < ZXC_BLOCK_HEADER_SIZE || dst_capacity == 0))
2104 return ZXC_ERROR_NULL_INPUT;
2105
2106 // Strict-tail variant: dst_capacity matches the exact uncompressed size
2107 if (UNLIKELY(dst_capacity > ZXC_BLOCK_SIZE_MAX)) return ZXC_ERROR_BAD_BLOCK_SIZE;
2108 if (UNLIKELY(dctx->owns_workspace))
2109 return zxc_static_decompress_block(dctx, (const uint8_t*)src, src_size, (uint8_t*)dst,
2110 dst_capacity, opts ? opts->checksum_enabled : 0, 1);
2111
2112 // A dict needs the [dict|payload] bounce; route to the bounce-capable path.
2113 if (opts && opts->dict && opts->dict_size > 0) {
2114 return zxc_decompress_block(dctx, src, src_size, dst, dst_capacity);
2115 }
2116
2117 const uint8_t type = ((const uint8_t*)src)[0];
2118 // RAW never wild-writes past dst_capacity: route to the existing fast API.
2119 if (type == ZXC_BLOCK_RAW) {
2120 return zxc_decompress_block(dctx, src, src_size, dst, dst_capacity);
2121 }
2122
2123 // SPL/PAK: use the strict-tail decoder (no bounce buffer required).
2124 const int checksum_enabled = opts ? opts->checksum_enabled : 0;
2125 const int rc = zxc_dctx_prepare(dctx, zxc_block_size_ceil(dst_capacity), 0, checksum_enabled);
2126 if (UNLIKELY(rc != ZXC_OK)) return rc; // LCOV_EXCL_LINE
2127 dctx->inner.dict_size = 0;
2128
2130 &dctx->inner, (const uint8_t*)src, src_size, (uint8_t*)dst, dst_capacity, 0);
2131 if (UNLIKELY(res < 0)) return res;
2132 return (int64_t)res;
2133}
2134
2135// ============================================================================
2136// STATIC CONTEXT API (caller-allocated workspace)
2137// ============================================================================
2138// Places the public handle struct at the start of the workspace, then carves
2139// the persistent buffer (via zxc_cctx_init_in_workspace) in the remaining
2140// cache-line-aligned tail. The caller owns the whole workspace; free
2141// functions become no-ops via the owns_workspace flag.
2142
2143// Size occupied by the opaque handle at the start of the workspace, rounded
2144// up to a cache-line boundary so the persistent buffer (which expects 64 B
2145// alignment for the hot zones) starts aligned.
2146#define ZXC_STATIC_CCTX_HDR_SIZE ZXC_ALIGN_CL(sizeof(struct zxc_cctx_s))
2147#define ZXC_STATIC_DCTX_HDR_SIZE ZXC_ALIGN_CL(sizeof(struct zxc_dctx_s))
2148
2156size_t zxc_static_cctx_workspace_size(const size_t block_size, const int level) {
2157 if (UNLIKELY(!zxc_validate_block_size(block_size))) return 0;
2158 if (UNLIKELY(level < ZXC_LEVEL_FASTEST || level > ZXC_LEVEL_ULTRA)) return 0;
2159 const size_t inner_sz = zxc_cctx_compute_workspace_size(block_size, 1, level, 0);
2160 if (UNLIKELY(inner_sz == 0)) return 0;
2161 return ZXC_STATIC_CCTX_HDR_SIZE + inner_sz;
2162}
2163
2164zxc_cctx* zxc_init_static_cctx(void* RESTRICT workspace, const size_t workspace_size,
2165 const size_t block_size, const int level) {
2166 if (UNLIKELY(!workspace)) return NULL;
2167 if (UNLIKELY(level < ZXC_LEVEL_FASTEST || level > ZXC_LEVEL_ULTRA)) return NULL;
2168 if (UNLIKELY(!zxc_validate_block_size(block_size))) return NULL;
2169
2170 const size_t inner_sz = zxc_cctx_compute_workspace_size(block_size, 1, level, 0);
2171 if (UNLIKELY(inner_sz == 0)) return NULL;
2172 if (UNLIKELY(workspace_size < ZXC_STATIC_CCTX_HDR_SIZE + inner_sz)) return NULL;
2173
2174 zxc_cctx* const cctx = (zxc_cctx*)workspace;
2175 ZXC_MEMSET(cctx, 0, sizeof(*cctx));
2176
2177 uint8_t* const inner_ws = (uint8_t*)workspace + ZXC_STATIC_CCTX_HDR_SIZE;
2178 if (UNLIKELY(zxc_cctx_init_in_workspace(&cctx->inner, inner_ws, inner_sz, block_size, 1, level,
2179 0, 0, 0) != ZXC_OK))
2180 return NULL;
2181
2182 cctx->owns_workspace = 1;
2183 cctx->initialized = 1;
2184 cctx->last_block_size = block_size;
2185 cctx->init_level = level;
2186 cctx->params = zxc_cparams_defaults();
2187 cctx->params.level = level;
2188 cctx->params.block_size = block_size;
2189 return cctx;
2190}
2191
2199size_t zxc_static_dctx_workspace_size(const size_t block_size) {
2200 if (UNLIKELY(!zxc_validate_block_size(block_size))) return 0;
2201 const size_t inner_sz = zxc_cctx_compute_workspace_size(block_size, 0, 0, 0);
2202 if (UNLIKELY(inner_sz == 0)) return 0;
2203 return ZXC_STATIC_DCTX_HDR_SIZE + inner_sz;
2204}
2205
2206zxc_dctx* zxc_init_static_dctx(void* RESTRICT workspace, const size_t workspace_size,
2207 const size_t block_size) {
2208 if (UNLIKELY(!workspace)) return NULL;
2209 if (UNLIKELY(!zxc_validate_block_size(block_size))) return NULL;
2210
2211 const size_t inner_sz = zxc_cctx_compute_workspace_size(block_size, 0, 0, 0);
2212 if (UNLIKELY(inner_sz == 0)) return NULL;
2213 if (UNLIKELY(workspace_size < ZXC_STATIC_DCTX_HDR_SIZE + inner_sz)) return NULL;
2214
2215 zxc_dctx* const dctx = (zxc_dctx*)workspace;
2216 ZXC_MEMSET(dctx, 0, sizeof(*dctx));
2217
2218 uint8_t* const inner_ws = (uint8_t*)workspace + ZXC_STATIC_DCTX_HDR_SIZE;
2219 // mode == 0 init: checksum_enabled is updated per-call from the file
2220 // header flags, so it does not need to be locked at workspace init.
2221 if (UNLIKELY(zxc_cctx_init_in_workspace(&dctx->inner, inner_ws, inner_sz, block_size, 0, 0, 0,
2222 0, 0) != ZXC_OK))
2223 return NULL;
2224
2225 dctx->owns_workspace = 1;
2226 dctx->initialized = 1;
2227 dctx->last_block_size = block_size;
2228 return dctx;
2229}
2230
2231// --- One-shot public forms ------------------------------------------------
2232
2241int64_t zxc_compress(const void* RESTRICT src, const size_t src_size, void* RESTRICT dst,
2242 const size_t dst_capacity, const int level) {
2243 if (UNLIKELY(!dst || dst_capacity == 0 || (src_size > 0 && !src))) return ZXC_ERROR_NULL_INPUT;
2244 if (UNLIKELY(level != 0 && (level < ZXC_LEVEL_FASTEST || level > ZXC_LEVEL_ULTRA)))
2245 return ZXC_ERROR_BAD_LEVEL;
2246 zxc_cctx cctx;
2247 ZXC_MEMSET(&cctx, 0, sizeof(cctx));
2248 cctx.params = zxc_cparams_defaults();
2249 if (level != 0) cctx.params.level = level;
2250 const int64_t res = zxc_compress_cctx_resolved(&cctx, src, src_size, dst, dst_capacity,
2251 zxc_cparams_resolve(&cctx.params), &cctx.params);
2252 if (cctx.initialized) zxc_cctx_free(&cctx.inner);
2253 return res;
2254}
2255
2263int64_t zxc_decompress(const void* RESTRICT src, const size_t src_size, void* RESTRICT dst,
2264 const size_t dst_capacity) {
2265 if (UNLIKELY(!src || (!dst && dst_capacity != 0))) return ZXC_ERROR_NULL_INPUT;
2266 if (UNLIKELY(src_size < ZXC_FRAME_MIN_SIZE))
2267 return zxc_short_input_verdict((const uint8_t*)src, src_size);
2268 if (UNLIKELY(!dst || dst_capacity == 0))
2269 return zxc_probe_without_dst((const uint8_t*)src, src_size, NULL, NULL);
2270 return zxc_decompress_oneshot((const uint8_t*)src, src_size, (uint8_t*)dst, dst_capacity, NULL);
2271}
2272
2280int64_t zxc_decompress_inplace(void* buffer, const size_t buffer_capacity, const size_t comp_size) {
2281 const uint8_t* comp = NULL;
2282 const int rc = zxc_inplace_prepare(buffer, buffer_capacity, comp_size, &comp);
2283 if (UNLIKELY(rc != ZXC_OK)) return rc;
2284 zxc_dctx dctx;
2285 ZXC_MEMSET(&dctx, 0, sizeof(dctx));
2286 const int64_t res = zxc_dctx_decode_frame(&dctx, comp, comp_size, (uint8_t*)buffer,
2287 buffer_capacity, NULL, 1, NULL);
2288 if (dctx.initialized) zxc_cctx_free(&dctx.inner);
2289 return res;
2290}
int64_t zxc_compress_block(zxc_cctx *cctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity)
Compresses a single block (no file framing), reusing cctx.
struct zxc_cctx_s zxc_cctx
Opaque reusable compression context (see zxc_create_cctx).
Definition zxc_buffer.h:370
int64_t zxc_decompress_block(zxc_dctx *dctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity)
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)
Safe-variant block decompressor: accepts dst_capacity == uncompressed_size.
struct zxc_dctx_s zxc_dctx
Opaque reusable decompression context (see zxc_create_dctx).
Definition zxc_buffer.h:372
#define ZXC_BLOCK_SIZE_MAX
Maximum allowed block size (2 MB = 2^21).
#define ZXC_BLOCK_SIZE_MIN
Minimum allowed block size (4 KB = 2^12).
#define ZXC_BLOCK_SIZE_DEFAULT
Default block size (512 KB).
int zxc_requires_dict(const void *src, const size_t src_size)
Tells whether an archive requires a dictionary, from its 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.
uint64_t zxc_get_decompressed_size(const void *src, const size_t src_size)
Reads the decompressed size from a ZXC-compressed buffer.
int64_t zxc_decompress_inplace(void *buffer, const size_t buffer_capacity, const size_t comp_size)
In-place decode with no checksum verification and no dictionary.
int64_t zxc_decompress(const void *src, const size_t src_size, void *dst, const size_t dst_capacity)
Decompresses with no checksum verification and no dictionary.
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.
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.
int64_t zxc_compress(const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const int level)
Compresses at level with every other parameter at its default.
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.
int zxc_cctx_set_dict(zxc_cctx *cctx, const void *dict, const size_t dict_size, const void *dict_huf)
Attaches a pre-trained dictionary to cctx; a NULL dict detaches.
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, const size_t src_size, void *dst, const size_t dst_capacity)
Decompresses a framed archive into dst, reusing dctx.
int zxc_dctx_get_param(const zxc_dctx *dctx, const zxc_dparam_t param, int64_t *value)
Reads one parameter of dctx.
int zxc_cctx_get_param(const zxc_cctx *cctx, const zxc_cparam_t param, int64_t *value)
Reads one parameter of cctx.
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)
Compresses data using a reusable context.
int zxc_dctx_set_param(zxc_dctx *dctx, const zxc_dparam_t param, const int64_t value)
Sets one parameter on dctx; 0 restores its default.
int zxc_cctx_set_param(zxc_cctx *cctx, const zxc_cparam_t param, const int64_t value)
Sets one compression parameter.
zxc_cctx * zxc_create_cctx(void)
Creates a reusable compression context, every parameter at its default.
zxc_dctx * zxc_create_dctx(void)
Creates a reusable decompression context.
int zxc_cctx_set_progress(zxc_cctx *cctx, const zxc_progress_callback_t cb, void *user_data)
Sets the progress callback; NULL cb disables it.
int zxc_dctx_set_dict(zxc_dctx *dctx, const void *dict, const size_t dict_size, const void *dict_huf)
Attaches the dictionary the archives were compressed with; NULL detaches.
int zxc_cctx_reset(zxc_cctx *cctx)
Restores every parameter to its default, detaches the dictionary and the callback.
void zxc_free_dctx(zxc_dctx *dctx)
Releases a reusable decompression context.
int zxc_dctx_set_progress(zxc_dctx *dctx, const zxc_progress_callback_t cb, void *user_data)
Sets the progress callback; NULL cb disables it.
int64_t zxc_decompress_inplace_dctx(zxc_dctx *dctx, void *buffer, const size_t buffer_capacity, const size_t comp_size)
Decompresses in place inside one caller buffer, reusing dctx.
uint32_t zxc_dict_compute_id(const void *dict, size_t dict_size, const void *huf_lengths)
Computes the dictionary ID from content, and optionally its table.
Definition zxc_dict.c:29
#define ZXC_DICT_SIZE_MAX
Maximum dictionary content size in bytes (64 KB - 1).
#define ZXC_HUF_TABLE_SIZE
Size of a packed literal Huffman code-lengths table (256 symbols, 4 bits each): the shared table in a...
@ ZXC_ERROR_BAD_MAGIC
Invalid magic word in file header.
Definition zxc_error.h:48
@ ZXC_ERROR_OVERFLOW
Buffer overflow detected during processing.
Definition zxc_error.h:58
@ ZXC_ERROR_CORRUPT_DATA
Corrupted compressed data.
Definition zxc_error.h:56
@ ZXC_ERROR_NULL_INPUT
Required input pointer is NULL.
Definition zxc_error.h:62
@ ZXC_ERROR_DICT_MISMATCH
Provided dictionary ID does not match the file header.
Definition zxc_error.h:70
@ ZXC_ERROR_MEMORY
Memory allocation failure.
Definition zxc_error.h:41
@ ZXC_ERROR_BAD_HEADER
Corrupted or invalid header (checksum mismatch).
Definition zxc_error.h:50
@ ZXC_ERROR_DST_TOO_SMALL
Destination buffer too small.
Definition zxc_error.h:44
@ ZXC_ERROR_SRC_TOO_SMALL
Source buffer too small or truncated input.
Definition zxc_error.h:45
@ ZXC_ERROR_DICT_REQUIRED
File requires a dictionary but none was provided.
Definition zxc_error.h:69
@ ZXC_ERROR_DICT_UNSUPPORTED
Dictionary not supported by this context's workspace, or library built without dictionary support.
Definition zxc_error.h:75
@ ZXC_ERROR_BAD_CHECKSUM
A block's decoded bytes fail its checksum, or the archive digest mismatches.
Definition zxc_error.h:51
@ ZXC_ERROR_BAD_PARAM
Unknown parameter, or value out of its bounds (zxc_cctx_set_param(), zxc_dctx_set_param()).
Definition zxc_error.h:77
@ ZXC_ERROR_BAD_LEVEL
Compression level not supported by this context's workspace.
Definition zxc_error.h:74
@ ZXC_OK
Success (no error).
Definition zxc_error.h:38
@ ZXC_ERROR_BAD_BLOCK_SIZE
Invalid block size.
Definition zxc_error.h:66
@ ZXC_ERROR_DICT_TOO_LARGE
Dictionary exceeds maximum allowed size.
Definition zxc_error.h:71
#define ZXC_MAGIC_WORD
Magic word opening every frame, stored little-endian (F5 2E B0 9C).
#define ZXC_FILE_HEADER_SIZE
File header size: Magic(4) + Version(1) + Chunk(1) + Flags(1) + Reserved(7) + Checksum(2).
#define ZXC_MALLOC(size)
Heap allocator.
Definition zxc_deps.h:68
#define ZXC_SEEK_GROUP
Blocks per group.
#define ZXC_ALWAYS_INLINE
Forces a function to be inlined (fallback for non-GCC/Clang/MSVC compilers).
static uint64_t zxc_seek_table_bytes(const uint64_t nblocks)
Byte size of the groups for nblocks blocks, in 64 bits.
#define ZXC_SEEK_SIZE_ENTRY
One block's on-disk size, u32.
#define ZXC_CALLOC(nmemb, size)
Zero-initialised heap allocator.
Definition zxc_deps.h:74
#define ZXC_FREE(ptr)
Heap deallocator.
Definition zxc_deps.h:86
#define ZXC_FILE_FLAG_HAS_DICTIONARY
Bit flag in the Flags byte indicating a dictionary is required (bit 6).
#define ZXC_BLOCK_HEADER_SIZE
Block header size: Type(1)+Flags(1)+Reserved(1)+Checksum(1)+CompSize(4).
#define ZXC_NOINLINE
Prevents inlining (best-effort no-op fallback for unknown compilers).
#define ZXC_BLOCK_CHECKSUM_SIZE
Size of the per-block checksum field in bytes.
#define ZXC_SEEK_ANCHOR_SIZE
Group anchor: offset of its first block, u64.
#define ZXC_DECOMPRESS_TAIL_PAD
Tail padding required on the decompression destination buffer.
@ ZXC_LEVEL_ULTRA
Maximum density: Huffman-coded literals and sequence tokens.
@ ZXC_LEVEL_DEFAULT
Recommended default: better ratio and decode speed than LZ4.
@ ZXC_LEVEL_FASTEST
Fastest compression; lowest ratio.
@ ZXC_LEVEL_DENSITY
Higher density: adds Huffman-coded literals on top of COMPACT.
size_t zxc_seek_table_size(const uint64_t num_blocks)
Returns the encoded size of a seek table for the given block count.
Definition zxc_common.c:724
size_t zxc_static_dctx_workspace_size(const size_t block_size)
Workspace size needed for a static decompression context.
size_t zxc_static_cctx_workspace_size(const size_t block_size, const int level)
Workspace size needed for a static compression 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.
zxc_cctx * zxc_init_static_cctx(void *workspace, const size_t workspace_size, const size_t block_size, const int level)
Initialises a compression context inside a caller-supplied workspace.
#define ZXC_MAX_THREADS
Thread cap, a guard against memory exhaustion: each worker of the FILE* pipeline holds up to four job...
On-disk header structure for a ZXC block (8 bytes, little-endian).
uint32_t comp_size
Compressed size excluding this header.
uint8_t block_type
Block type (see zxc_block_type_t).
Compression / decompression context.
size_t work_buf_cap
Capacity of the work buffer.
int compression_level
Compression level.
uint8_t * work_buf
Padded scratch buffer for buffer-API decompression.
uint8_t * dict_buffer
[dict | data] concat scratch carved from memory_block when dict_size > 0 (NULL otherwise).
size_t dict_size
Dictionary prefill size (0 = no dictionary).
uint8_t * opt_scratch
Optimal-parser DP scratch (level >= 6 only, lazy-allocated, packs dp/parent_len/parent_off/actions).
int checksum_enabled
1 if checksum calculation/verification is enabled.
What zxc_scan_container learns about a container.
int uses_dict
A frame bound to a dictionary.
uint64_t dsize
Sum of the stored decompressed sizes.
Compression parameters of a zxc_cctx, the private form of what zxc_cctx_set_param() / zxc_cctx_set_di...
Decompression parameters of a zxc_dctx; see zxc_cparams_t.
What a frame's header and footer declare, read without decoding.
Definition zxc_buffer.h:275
uint8_t has_checksum
1 when blocks carry checksums and the footer a digest.
Definition zxc_buffer.h:282
size_t block_size
Block size, 4 KB to 2 MB.
Definition zxc_buffer.h:279
uint64_t decompressed_size
Source bytes the frame decodes to.
Definition zxc_buffer.h:276
uint8_t has_seek_table
1 when a seek table precedes the footer.
Definition zxc_buffer.h:283
A PivCo code tree: its canonical shape, by depth.
Where a container scan reads from: memory, or a positioned callback (the FILE* size query).
int zxc_read_block_header(const uint8_t *src, const size_t src_size, zxc_block_header_t *bh)
Parses and validates a block header from src.
Definition zxc_common.c:699
void zxc_cctx_free(zxc_cctx_t *ctx)
Releases all resources owned by a compression context.
Definition zxc_common.c:382
int zxc_write_block_header(uint8_t *dst, const size_t dst_capacity, const zxc_block_header_t *bh)
Serialises a block header (8 bytes) into dst.
Definition zxc_common.c:680
int zxc_scan_container(const zxc_scan_src_t *src, const size_t req_chunk, zxc_container_info_t *info)
Measures a whole container without decoding: frames from the last back to the first through zxc_scan_...
Definition zxc_common.c:605
int zxc_seek_table_header(uint8_t *dst, const size_t dst_capacity, const uint64_t num_blocks)
Writes a seek table's block header for num_blocks entries.
Definition zxc_common.c:731
int zxc_scan_frame(const zxc_scan_src_t *src, const uint64_t end, uint64_t *start, zxc_frame_info_t *info, size_t *footer_len, int *has_dict)
Measures the frame ending at end without decoding: its footer's compressed size gives where it starts...
Definition zxc_common.c:561
int zxc_cctx_init_in_workspace(zxc_cctx_t *ctx, void *workspace, const size_t workspace_size, const size_t chunk_size, const int mode, const int level, const int checksum_enabled, const size_t dict_size, const int defer_entropy_scratch)
Partitions a caller-supplied workspace into a ready-to-use context.
Definition zxc_common.c:240
int zxc_scan_whole_frame(const zxc_scan_src_t *src, zxc_frame_info_t *info, size_t *footer_len)
The frame that must span all of src: zxc_get_frame_info() and its FILE* twin.
Definition zxc_common.c:593
size_t zxc_cctx_compute_workspace_size(const size_t chunk_size, const int mode, const int level, const size_t dict_size)
Returns the workspace byte count required for the given parameters.
Definition zxc_common.c:225
int zxc_container_next(const zxc_scan_src_t *src, const uint64_t pos)
Tells whether a frame starts at pos.
Definition zxc_common.c:550
int zxc_check_eof_header(const uint8_t *hdr)
Checks an EOF block header against its one valid form: type 255, every other field zero,...
Definition zxc_common.c:741
int zxc_cctx_init(zxc_cctx_t *ctx, const size_t chunk_size, const int mode, const int level, const int checksum_enabled, const size_t dict_size)
Initialises a compression / decompression context, allocating the persistent buffer with ZXC_ALIGNED_...
Definition zxc_common.c:319
int zxc_read_file_header(const uint8_t *src, const size_t src_size, size_t *out_block_size, int *out_has_checksum, int *out_has_dict, uint32_t *out_dict_id, int *out_has_seek)
Parses and validates a ZXC file header from src.
Definition zxc_common.c:499
int zxc_write_file_footer(uint8_t *dst, const size_t dst_capacity, const uint64_t prefix, const uint64_t src_size, const uint64_t digest, const int checksum_enabled)
Writes the file footer: the archive digest when checksum_enabled, then the two sizes and their length...
Definition zxc_common.c:825
int zxc_write_file_header(uint8_t *dst, const size_t dst_capacity, const size_t chunk_size, const int has_checksum, const int has_dict, const uint32_t dict_id, const int has_seek)
Serialises a ZXC file header into dst.
Definition zxc_common.c:468
int zxc_cctx_attach_dict_huf(zxc_cctx_t *ctx, const uint8_t *lengths)
Attach the shared dictionary literal Huffman table to a context.
Definition zxc_common.c:435
int zxc_check_file_footer(const uint8_t *footer, const size_t avail, const zxc_footer_layout_t *l, const uint64_t src_size, uint64_t *digest)
Checks a footer against the one the frame implies.
Definition zxc_common.c:840
int zxc_check_seek_header(const uint8_t *hdr, const size_t avail, const uint64_t total_out, const size_t block_size, uint64_t *sek_bytes)
Checks the SEK block header the file header announced, on the avail bytes present.
Definition zxc_common.c:750
Pre-trained dictionary API for ZXC compression.
const zxc_cparams_t * zxc_cctx_params(const zxc_cctx *cctx)
Parameters a public context carries, for the FILE* and push paths.
static _Atomic zxc_decompress_func_t zxc_decompress_safe_ptr
The _safe decoder in use, NULL until the first call.
#define ZXC_VARIANT_SET(sfx)
Defines the constant variant set of tier sfx.
static int zxc_inplace_prepare(const void *buffer, const size_t buffer_capacity, const size_t comp_size, const uint8_t **comp)
Argument and buffer checks shared by the in-place entry points.
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.
static int64_t zxc_probe_without_dst(const uint8_t *src, const size_t src_size, const zxc_dparams_t *opts, const zxc_container_info_t *scanned)
Answers a decode request that carries no destination.
static int64_t zxc_dctx_decode_frame(zxc_dctx *dctx, const uint8_t *src, size_t src_size, uint8_t *dst, size_t dst_capacity, const zxc_dparams_t *opts, int inplace, size_t *frame_end)
The frame walk behind every buffer decode: zxc_decompress(), its no-destination probe,...
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_cparam_bounds(const zxc_cparam_t param, int64_t *min, int64_t *max)
Reports the accepted range of a compression parameter.
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 he...
static const zxc_variant_set_t * zxc_select_variants(void)
Detects the CPU tier and returns the matching variant set.
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.
int zxc_dparam_bounds(const zxc_dparam_t param, int64_t *min, int64_t *max)
Reports the accepted range of a decompression parameter.
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).
zxc_cpu_feature_t
Detected CPU SIMD capability level.
@ ZXC_CPU_GENERIC
Scalar-only fallback.
@ ZXC_CPU_SSE2
x86 SSE2 available (no AVX2); x86-64 baseline, served by _default (no dedicated variant).
@ ZXC_CPU_AVX512
x86-64 AVX-512F+BW available.
@ ZXC_CPU_AVX2
x86-64 AVX2 available.
@ ZXC_CPU_NEON
ARM NEON available (dedicated variant on 32-bit ARM only; AArch64 baseline, served by _default there)...
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.
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.
static int64_t zxc_dctx_probe(const zxc_dctx *dctx, const uint8_t *src, const size_t src_size, const zxc_dparams_t *opts)
Answers a no-destination decode on a reusable context.
static int zxc_dict_copy_set(zxc_dict_copy_t *c, const void *dict, const size_t dict_size, const void *dict_huf)
Replaces the copy with dict; a NULL or empty dict detaches.
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 int has_dict, const uint32_t did, const int seekable)
Writes the whole archive an empty source produces: file header, EOF block, the empty seek table when ...
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_dctx_decode_container(zxc_dctx *dctx, const uint8_t *src, size_t src_size, uint8_t *dst, size_t dst_capacity, const zxc_dparams_t *opts)
The container walk behind zxc_decompress() and zxc_decompress_dctx().
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_cparams_t *opts, const uint64_t block_index)
zxc_compress_block, its checksum seeded with block_index (public entry: 0).
static int64_t zxc_compress_cctx_resolved(zxc_cctx *cctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_cctx_opts_t o, const zxc_cparams_t *opts)
Frame compression with resolved options o; opts supplies the dictionary only.
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.
static zxc_cctx_opts_t zxc_cparams_resolve(const zxc_cparams_t *p)
Per-call options of a context; its level and block size are always resolved and in range (setters,...
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.
static const zxc_variant_set_t * zxc_variants(void)
The variant set in use, selected on the first call.
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.
static const zxc_variant_set_t * zxc_variants_resolve(void)
First call: selects and publishes.
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).
static int64_t zxc_compress_block_resolved(zxc_cctx *cctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_cctx_opts_t o, const zxc_cparams_t *opts, const uint64_t block_index)
Block compression with resolved options o; opts supplies the dictionary only.
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.
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,...
static zxc_cpu_feature_t zxc_detect_cpu_features(void)
Probes the running CPU for SIMD support.
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,...
static const zxc_variant_set_t *_Atomic zxc_variants_ptr
The selection, NULL until the first call.
static zxc_decompress_func_t zxc_decompress_safe_variant(void)
The _safe decoder in use, selected on the first call.
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...
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_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 ...
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).
static zxc_cparams_t zxc_cparams_defaults(void)
The parameters every public context starts from.
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 int checksum_enabled, const int strict)
Block decode on a static dctx: the carved block is the effective capacity.
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_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_unpack_lengths(const uint8_t *in, uint8_t *code_len)
Unpack and validate a 128-byte packed lengths header.
static zxc_decompress_func_t zxc_decompress_safe_resolve(void)
First call: the _safe decoder of the selected set, published.
Error codes and error-name lookup for the ZXC library.
Internal definitions, constants, SIMD helpers, and utility functions.
static uint32_t zxc_le32(const void *p)
Reads a little-endian 32-bit value from a possibly unaligned address.
static void zxc_frame_info_copy(zxc_frame_info_t *info, const size_t info_size, const zxc_frame_info_t *got)
Copies the fields of got that fit the caller's info_size bytes.
static int zxc_validate_block_size(const size_t bs)
Validates a block size.
static zxc_scan_src_t zxc_scan_src_mem(const uint8_t *data, const size_t size)
A scan source over size bytes of memory at data.
static void zxc_store_le64(void *p, const uint64_t v)
Writes a 64-bit value little-endian to a possibly unaligned address.
static uint64_t zxc_digest_combine(const uint64_t acc, const uint32_t block_checksum)
Folds a data block's 32-bit checksum into the running archive digest.
static size_t zxc_block_size_ceil(const size_t v)
Branchless bit_ceil: smallest power of two >= v, clamped to ZXC_BLOCK_SIZE_MIN.
#define ZXC_OPTS_DICT_HUF(o)
Shared literal Huffman table, gated on dict the same way.
#define ZXC_FRAME_MIN_SIZE
Smallest frame: header, the EOF block and the shortest footer.
static uint64_t zxc_seek_block_count(const uint64_t total_decomp, const size_t block_size)
Blocks in total_decomp bytes of block_size: the seek table's entry count, which the SEK header's fiel...
static void zxc_store_le32(void *p, const uint32_t v)
Writes a 32-bit value little-endian to a possibly unaligned address.
#define ZXC_OPTS_DICT_SIZE(o)
Dictionary length from a (possibly NULL) parameter struct.
static zxc_footer_layout_t zxc_footer_layout(const uint64_t prefix, const uint64_t src_size, const int checksum_enabled)
The footer of a frame whose first prefix bytes precede it.
static int zxc_short_input_verdict(const uint8_t *p, const size_t n)
The verdict on n bytes too short for a frame: the header speaks first, then ZXC_ERROR_SRC_TOO_SMALL.
zxc_cparam_t
Compression parameters, set on a zxc_cctx with zxc_cctx_set_param().
Definition zxc_opts.h:53
@ ZXC_C_CHECKSUM
1 appends the block checksums and the archive digest; default 0.
Definition zxc_opts.h:66
@ ZXC_C_LEVEL
Compression level, 1 to 7 (zxc_compression_level_t); default ZXC_LEVEL_DEFAULT.
Definition zxc_opts.h:56
@ ZXC_C_BLOCK_SIZE
Block size in bytes, a power of two in [ZXC_BLOCK_SIZE_MIN, ZXC_BLOCK_SIZE_MAX]; default ZXC_BLOCK_SI...
Definition zxc_opts.h:59
@ ZXC_C_THREADS
Workers of the FILE* pipeline, at most ZXC_MAX_THREADS; the reader and writer come on top.
Definition zxc_opts.h:64
@ ZXC_C_SEEKABLE
1 appends a seek table for random-access decompression; default 0.
Definition zxc_opts.h:68
zxc_dparam_t
Decompression parameters, set on a zxc_dctx with zxc_dctx_set_param().
Definition zxc_opts.h:78
@ ZXC_D_CHECKSUM
1 verifies the block checksums and the archive digest when the archive carries them; default 0,...
Definition zxc_opts.h:86
@ ZXC_D_THREADS
Workers of the FILE* pipeline, at most ZXC_MAX_THREADS; the reader and writer come on top.
Definition zxc_opts.h:83
void(* zxc_progress_callback_t)(uint64_t bytes_processed, uint64_t bytes_total, const void *user_data)
Progress callback function type.
Definition zxc_opts.h:40