33#if (defined(__x86_64__) || defined(_M_X64)) && !defined(_MSC_VER) && !defined(ZXC_ONLY_DEFAULT)
37#if defined(__linux__) && (defined(__arm__) || defined(_M_ARM)) && !defined(ZXC_ONLY_DEFAULT)
42#if __has_include(<asm/hwcap.h>)
47#define HWCAP_NEON (1 << 12)
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);
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);
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);
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,
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,
130 const uint32_t* RESTRICT codes, uint8_t* RESTRICT dst,
132int zxc_huf_decode_section_dict_default(
const uint8_t* RESTRICT payload,
size_t payload_size,
133 uint8_t* RESTRICT dst,
size_t n,
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,
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);
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);
155#ifdef ZXC_DISPATCH_ENTROPY
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);
169#undef ZXC_HUF_DECODE_PROTOTYPES
176#if (defined(__x86_64__) || defined(_M_X64)) && !defined(ZXC_ONLY_DEFAULT)
179static inline void zxc_cpuid(
const uint32_t leaf,
const uint32_t sub, uint32_t regs[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];
188 __cpuid_count(leaf, sub, regs[0], regs[1], regs[2], regs[3]);
193static inline uint64_t zxc_xgetbv0(
void) {
200 __asm__
volatile(
".byte 0x0f, 0x01, 0xd0" :
"=a"(lo),
"=d"(hi) :
"c"(0));
201 return ((uint64_t)hi << 32) | lo;
208 const size_t, uint8_t* RESTRICT,
const size_t,
const uint64_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,
218typedef int (*zxc_huf_decode_dict_func_t)(
const uint8_t* RESTRICT, size_t, uint8_t* RESTRICT,
224#ifndef ZXC_ONLY_DEFAULT
252#if defined(__x86_64__) || defined(_M_X64)
261 zxc_cpuid(0, 0, regs);
262 const uint32_t max_leaf = regs[0];
264 zxc_cpuid(1, 0, regs);
265 if (regs[3] & (1U << 26)) sse2 = 1;
266 if ((regs[2] & (1U << 27)) && max_leaf >= 7) {
267 const uint64_t xcr0 = zxc_xgetbv0();
268 if ((xcr0 & 0x6) == 0x6) {
269 zxc_cpuid(7, 0, regs);
270 const int bmi1 = (regs[1] >> 3) & 1;
271 const int bmi2 = (regs[1] >> 8) & 1;
272 if (regs[1] & (1U << 5)) avx2 = 1;
274 if ((regs[1] & (1U << 16)) && (regs[1] & (1U << 30)) && (regs[2] & (1U << 1)) &&
275 (regs[2] & (1U << 6)) && (xcr0 & 0xE0) == 0xE0)
280 zxc_cpuid(0x80000001U, 0, regs);
281 bmi_lzcnt = bmi1 && bmi2 && ((regs[2] >> 5) & 1);
285 if (avx512 && bmi_lzcnt) {
287 }
else if (avx2 && bmi_lzcnt) {
293#elif defined(__aarch64__) || defined(_M_ARM64)
297#elif defined(__arm__) || defined(_M_ARM)
299#if defined(__linux__)
300 const unsigned long hwcaps = getauxval(AT_HWCAP);
301 if (hwcaps & HWCAP_NEON) {
308#if defined(__ARM_NEON)
341#ifdef ZXC_DISPATCH_ENTROPY
342 zxc_huf_decode_func_t huf_decode;
344 zxc_huf_decode_dict_func_t huf_decode_dict;
349#define ZXC_GLUE_IMPL(a, b) a##b
350#define ZXC_GLUE(a, b) ZXC_GLUE_IMPL(a, b)
353#ifdef ZXC_LZ_DECODER_DEFAULT
354#define ZXC_DEC_SFX(sfx) _default
356#define ZXC_DEC_SFX(sfx) sfx
358#ifdef ZXC_ENCODER_DEFAULT
359#define ZXC_ENC_SFX(sfx) _default
361#define ZXC_ENC_SFX(sfx) sfx
365#define ZXC_VARIANT_DICT(sfx) \
366 .decompress_dict = ZXC_GLUE(zxc_decompress_chunk_wrapper_dict, ZXC_DEC_SFX(sfx)),
368#define ZXC_VARIANT_DICT(sfx)
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,
377#define ZXC_VARIANT_HUF(sfx)
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)}
388#ifndef ZXC_ONLY_DEFAULT
389#if defined(__x86_64__) || defined(_M_X64)
394#elif defined(__arm__) || defined(_M_ARM)
398#undef ZXC_VARIANT_SET
399#undef ZXC_VARIANT_HUF
400#undef ZXC_VARIANT_DICT
404#ifdef ZXC_ONLY_DEFAULT
407 return &zxc_variants_default;
418#if defined(__x86_64__) || defined(_M_X64)
425#elif defined(__arm__) || defined(_M_ARM)
432 return &zxc_variants_default;
444#if ZXC_USE_C11_ATOMICS
455#if ZXC_USE_C11_ATOMICS
456 const zxc_variant_set_t* v = atomic_load_explicit(&
zxc_variants_ptr, memory_order_acquire);
465#ifdef ZXC_LZ_DECODER_DEFAULT
468 return zxc_decompress_chunk_wrapper_safe_default;
479#if defined(__x86_64__) || defined(_M_X64)
481 if (v == &zxc_variants_avx512) f = zxc_decompress_chunk_wrapper_safe_avx512;
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;
489#if ZXC_USE_C11_ATOMICS
500#if ZXC_USE_C11_ATOMICS
512 const size_t src_sz, uint8_t* RESTRICT dst,
const size_t dst_cap,
513 const uint64_t block_index) {
520 return v->decompress(ctx, src, src_sz, dst, dst_cap, block_index);
522 return (ctx->dict_size ? v->decompress_dict : v->decompress)(ctx, src, src_sz, dst, dst_cap,
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) {
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);
574 void* RESTRICT scratch,
const int max_code_len) {
575 return zxc_huf_build_code_lengths_default(freq, code_len, scratch, max_code_len);
580 const int with_header) {
581 return zxc_huf_calc_size_default(freq, code_len, with_header);
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);
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);
595 return zxc_huf_decode_section_default(payload, payload_size, dst, n, scratch);
600 const uint32_t* RESTRICT freq,
const uint8_t* RESTRICT code_len,
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,
609 uint8_t* RESTRICT dst,
const size_t n,
611#if defined(ZXC_DISPATCH_ENTROPY) && !defined(ZXC_NO_DICT)
612 return zxc_variants()->huf_decode_dict(payload, payload_size, dst, n, tree, scratch);
614 return zxc_huf_decode_section_dict_default(payload, payload_size, dst, n, tree, scratch);
620 return zxc_huf_calc_size_dict_default(freq, code_len, tree);
630 zxc_huf_pack_lengths_default(code_len, out);
640 return zxc_huf_unpack_lengths_default(in, code_len);
659 const size_t block_size,
const int checksum_enabled,
660 const int has_dict,
const uint32_t did,
const int seekable) {
663 if (UNLIKELY(h < 0))
return h;
664 size_t off = (size_t)h;
667 .block_type = ZXC_BLOCK_EOF, .block_flags = 0, .reserved = 0, .comp_size = 0};
669 if (UNLIKELY(e < 0))
return e;
675 if (UNLIKELY(t < 0))
return t;
680 if (UNLIKELY(f < 0))
return f;
681 return (int64_t)(off + (size_t)f);
694 const uint8_t* RESTRICT blocks,
695 const uint8_t*
const blocks_end,
696 const uint64_t num_blocks,
697 const int checksum_enabled) {
703 if (UNLIKELY(h < 0))
return h;
704 uint8_t* p = dst + h;
708 for (uint64_t i = 0; i < num_blocks; i++) {
713 const size_t left = (size_t)(blocks_end - blocks);
716 const size_t size = (size_t)
zxc_le32(blocks + 3) + trailer;
724 return (int64_t)(p - dst);
728static int64_t zxc_decompress_oneshot(
const uint8_t* src,
size_t src_size, uint8_t* dst,
733 uint8_t* dst,
size_t dst_capacity,
const zxc_dparams_t* opts,
734 int inplace,
size_t* frame_end);
738 uint8_t* dst,
size_t dst_capacity,
754 size_t* footer_len) {
775 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
779 uint8_t probe_dst[1];
780 return zxc_decompress_oneshot(src, src_size, probe_dst, 0, opts);
813 const int has_seek,
const size_t footer_len) {
815 const uint64_t per_block =
817 const uint64_t seek_table =
820 seek_table + (uint64_t)footer_len;
821 return (uint64_t)chunk_size + nblocks * per_block + trailing +
847 uint64_t* margin, uint64_t* floor) {
849 size_t footer_len = 0;
854 if (UNLIKELY(rc !=
ZXC_OK))
886 if (UNLIKELY(
zxc_inplace_probe((
const uint8_t*)src, src_size, &dsize, &margin, &floor) !=
889 if (UNLIKELY(margin > (uint64_t)SIZE_MAX || dsize > (uint64_t)SIZE_MAX - margin))
return 0;
890 const uint64_t by_payload = dsize + margin;
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;
895 return (
size_t)(by_payload > by_placement ? by_payload : by_placement);
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);
919 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
920 if (UNLIKELY(dsize > (uint64_t)buffer_capacity || (uint64_t)buffer_capacity - dsize < margin))
936 if (UNLIKELY(!src))
return 0;
953 const uint8_t*
const p = (
const uint8_t*)src;
971 return rc !=
ZXC_OK ? rc : has_dict;
981 const size_t info_size) {
996 const size_t info_size) {
1001 const int rc =
zxc_scan_frame(&scan, src_size, &start, &got, NULL, NULL);
1045 const void* dict_huf) {
1046 uint8_t* copy = NULL;
1047 if (dict && dict_size > 0) {
1051 ZXC_MEMCPY(copy, dict, dict_size);
1056 c->size = copy ? dict_size : 0;
1057 c->has_huf = copy && dict_huf;
1067 size_t last_block_size;
1068 zxc_cparams_t params;
1069 zxc_dict_copy_t dict_copy;
1083 int* RESTRICT cached,
const uint8_t* RESTRICT dict_huf) {
1092 }
else if (*cached) {
1102 return (zxc_cctx_opts_t){.level = p->level,
1103 .checksum = p->checksum_enabled,
1104 .seekable = p->seekable,
1105 .block_size = p->block_size};
1115 if (UNLIKELY(!cctx))
return NULL;
1162 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
1163 if (value != 0 && (value < lo || value > hi)) {
1172 const int level = value != 0 ? (int)value
1173 : cctx->owns_workspace ? cctx->init_level
1183 const size_t block_size = value != 0 ? (size_t)value
1184 : cctx->owns_workspace ? cctx->last_block_size
1187 if (UNLIKELY(cctx->owns_workspace && block_size != cctx->last_block_size))
1189 p->block_size = block_size;
1193 p->n_threads = (int)value;
1196 p->checksum_enabled = (int)value;
1199 p->seekable = (int)value;
1215 *value = (int64_t)p->block_size;
1218 *value = p->n_threads;
1221 *value = p->checksum_enabled;
1224 *value = p->seekable;
1233 const void* 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;
1251 cctx->params.progress_cb = cb;
1252 cctx->params.user_data = cb ? user_data : NULL;
1261 if (cctx->owns_workspace) {
1262 cctx->params.level = cctx->init_level;
1263 cctx->params.block_size = cctx->last_block_size;
1276 if (UNLIKELY(!cctx))
return;
1279 if (cctx->owns_workspace)
return;
1294 const size_t src_size,
void* RESTRICT dst,
1295 const size_t dst_capacity,
const zxc_cctx_opts_t o,
1298 const uint8_t* dict = opts ? (
const uint8_t*)opts->dict : NULL;
1303 const size_t eff_chunk =
1305 const int has_dict = dict && dict_size > 0;
1309 if (UNLIKELY(src_size == 0))
1311 has_dict, did, o.seekable);
1315 if (UNLIKELY(!cctx->initialized || cctx->last_block_size != eff_chunk ||
1318 if (cctx->initialized) {
1320 cctx->initialized = 0;
1323 if (UNLIKELY(
zxc_cctx_init(&cctx->inner, eff_chunk, 1, o.level, o.checksum, dict_size) !=
1327 cctx->last_block_size = eff_chunk;
1328 cctx->initialized = 1;
1329 cctx->huf_cached = 0;
1343 uint8_t*
const dict_input = dict_size > 0 ? ctx->
dict_buffer : NULL;
1344 if (dict_input) ZXC_MEMCPY(dict_input, dict, dict_size);
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;
1352 has_dict, did, o.seekable);
1353 if (UNLIKELY(h_val < 0))
return h_val;
1356 uint64_t digest = 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);
1365 ZXC_MEMCPY(dict_input + dict_size, ip + pos, chunk_len);
1371 if (UNLIKELY(res < 0))
return res;
1380 uint8_t*
const eof_at = op;
1381 const size_t rem_cap = (size_t)(op_end - op);
1383 .block_type = ZXC_BLOCK_EOF, .block_flags = 0, .reserved = 0, .comp_size = 0};
1385 if (UNLIKELY(eof_val < 0))
return eof_val;
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;
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;
1401 return (int64_t)(op - op_start);
1411 void* RESTRICT dst,
const size_t dst_capacity) {
1412 if (UNLIKELY(!cctx || !dst || dst_capacity == 0 || (src_size > 0 && !src)))
1429 size_t last_block_size;
1430 size_t last_dict_size;
1436 zxc_dparams_t params;
1437 zxc_dict_copy_t dict_copy;
1471 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
1475 dctx->params.n_threads = (int)value;
1478 dctx->params.checksum_enabled = (int)value;
1490 *value = dctx->params.n_threads;
1493 *value = dctx->params.checksum_enabled;
1502 const void* 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;
1519 dctx->params.progress_cb = cb;
1520 dctx->params.user_data = cb ? user_data : NULL;
1527 ZXC_MEMSET(&dctx->params, 0,
sizeof(dctx->params));
1538 if (UNLIKELY(!dctx))
return;
1541 if (dctx->owns_workspace)
return;
1562 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
1563 if (UNLIKELY(dctx->owns_workspace && (info.
uses_dict || dict_size != 0)))
1576 void* RESTRICT dst,
const size_t dst_capacity) {
1577 const zxc_dparams_t*
const opts = dctx ? &dctx->params : NULL;
1581 if (UNLIKELY(!dst || dst_capacity == 0))
return zxc_dctx_probe(dctx, src, src_size, opts);
1583 dst_capacity, opts);
1595 const size_t comp_size) {
1596 const zxc_dparams_t*
const opts = dctx ? &dctx->params : NULL;
1598 const uint8_t* comp = NULL;
1600 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
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) {
1618 dctx->initialized = 0;
1621 if (UNLIKELY(
zxc_cctx_init(&dctx->inner, chunk_size, 0, 0, checksum, dict_size) !=
ZXC_OK))
1624 dctx->last_block_size = chunk_size;
1625 dctx->last_dict_size = dict_size;
1626 dctx->initialized = 1;
1627 dctx->huf_cached = 0;
1647 uint8_t* dst,
const size_t dst_capacity,
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;
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;
1667 &header_has_dict, &header_dict_id, &file_has_seek);
1668 if (UNLIKELY(hrc !=
ZXC_OK))
return hrc;
1672 if (UNLIKELY(dctx->owns_workspace && runtime_chunk_size != dctx->last_block_size))
1675 if (UNLIKELY(dctx->owns_workspace && (header_has_dict || dict_size != 0)))
1679 if (header_has_dict) {
1704 uint8_t* dict_dec = NULL;
1706 uint64_t block_index = 0;
1707 uint64_t digest = 0;
1711 const size_t rem_src = (size_t)(ip_end - ip);
1716 if (UNLIKELY(bh.
block_type == ZXC_BLOCK_EOF)) {
1721 const uint64_t total_out = (uint64_t)(op - op_start);
1723 if (file_has_seek) {
1724 uint64_t sek_bytes = 0;
1726 runtime_chunk_size, &sek_bytes);
1727 if (UNLIKELY(src_rc !=
ZXC_OK))
return src_rc;
1729 if (UNLIKELY(sek_bytes > (uint64_t)(src_size - pos)))
1731 pos += (size_t)sek_bytes;
1734 uint64_t stored_digest = 0;
1737 if (UNLIKELY(frc !=
ZXC_OK))
return frc;
1738 if (checksum_enabled && file_has_checksums && UNLIKELY(stored_digest != digest))
1744 else if (UNLIKELY(pos != src_size))
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;
1760 if (dict_dec) ZXC_MEMCPY(dict_dec, dict, dict_size);
1767 const size_t rem_cap = (size_t)(op_end - op);
1768 size_t room = rem_cap;
1770 const size_t ahead = ip > op ? (size_t)(ip - op) : 0;
1771 if (ahead < room) room = ahead;
1774 const uint8_t* bounce = NULL;
1779 bounce = dict_dec + dict_size;
1780 }
else if (LIKELY(room >= work_sz)) {
1787 if (bounce && LIKELY(res > 0)) {
1790 if (UNLIKELY((
size_t)res > room))
1792 ZXC_MEMCPY(op, bounce, (
size_t)res);
1794 if (UNLIKELY(res < 0))
return res;
1796 if (checksum_enabled && file_has_checksums)
1805 return (int64_t)(op - op_start);
1810 uint8_t* dst,
const size_t dst_capacity,
1817 size_t frame_size = 0;
1819 dst_capacity - out, opts, 0, &frame_size);
1820 if (UNLIKELY(res < 0))
return res;
1824 if (UNLIKELY(rc < 0))
return rc;
1826 return (int64_t)out;
1830static int64_t zxc_decompress_oneshot(
const uint8_t* src,
const size_t src_size, uint8_t* dst,
1833 ZXC_MEMSET(&dctx, 0,
sizeof(dctx));
1854 const size_t src_size,
void* RESTRICT dst,
1855 const size_t dst_capacity,
const zxc_cctx_opts_t o,
1858 const uint8_t* b_dict = opts ? (
const uint8_t*)opts->dict : NULL;
1860 const size_t effective_block_size =
1866 if (UNLIKELY(cctx->owns_workspace && effective_block_size != cctx->last_block_size))
1872 if (UNLIKELY(!cctx->initialized || cctx->last_block_size < effective_block_size ||
1875 if (cctx->initialized) {
1878 cctx->initialized = 0;
1882 if (UNLIKELY(
zxc_cctx_init(&cctx->inner, effective_block_size, 1, o.level, o.checksum,
1886 cctx->last_block_size = effective_block_size;
1887 cctx->initialized = 1;
1888 cctx->huf_cached = 0;
1900 if (b_dict && b_dict_size > 0) {
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);
1906 (uint8_t*)dst, dst_capacity, block_index);
1909 dst_capacity, block_index);
1911 if (UNLIKELY(res < 0))
return res;
1912 return (int64_t)res;
1924 void* RESTRICT dst,
const size_t dst_capacity) {
1929 void* RESTRICT dst,
const size_t dst_capacity,
1931 if (UNLIKELY(!cctx || !src || !dst || src_size == 0 || dst_capacity == 0))
1935 const zxc_cparams_t*
const cp = opts ? opts : &cctx->params;
1938 zxc_cctx_opts_t carve = o;
1940 if (carve.block_size < min_bs) carve.block_size = min_bs;
1952 const size_t src_size, uint8_t* RESTRICT dst,
1953 const size_t dst_capacity,
const size_t carved) {
1957 if (UNLIKELY(res < 0))
return res;
1960 ZXC_MEMCPY(dst, ctx->work_buf, (
size_t)res);
1972 const size_t src_size, uint8_t* RESTRICT dst,
1973 const size_t dst_capacity,
const int checksum_enabled,
1975 const size_t carved = dctx->last_block_size;
1982 const size_t cap = dst_capacity < work_sz ? dst_capacity : work_sz;
1984 }
else if (dst_capacity >= work_sz) {
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;
2005 if (dctx->initialized) {
2007 dctx->initialized = 0;
2010 if (UNLIKELY(
zxc_cctx_init(&dctx->inner, block_size, 0, 0, checksum_enabled, dict_size) !=
2014 dctx->last_block_size = block_size;
2015 dctx->last_dict_size = dict_size;
2016 dctx->initialized = 1;
2017 dctx->huf_cached = 0;
2032 void* RESTRICT dst,
const size_t dst_capacity) {
2033 const zxc_dparams_t*
const opts = dctx ? &dctx->params : NULL;
2043 const int checksum_enabled = opts ? opts->checksum_enabled : 0;
2045 const uint8_t* dict = opts ? (
const uint8_t*)opts->dict : NULL;
2047 if (UNLIKELY(dctx->owns_workspace))
2049 dst_capacity, checksum_enabled, 0);
2053 const size_t block_size =
2055 const int rc =
zxc_dctx_prepare(dctx, block_size, dict_size, checksum_enabled);
2056 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
2068 if (dict && dict_size > 0) {
2071 ZXC_MEMCPY(dec_buf, dict, dict_size);
2074 if (LIKELY(res > 0)) {
2076 ZXC_MEMCPY(dst, dec_buf + dict_size, (
size_t)res);
2087 if (UNLIKELY(res < 0))
return res;
2088 return (int64_t)res;
2101 void* RESTRICT dst,
const size_t dst_capacity) {
2102 const zxc_dparams_t*
const opts = dctx ? &dctx->params : NULL;
2108 if (UNLIKELY(dctx->owns_workspace))
2110 dst_capacity, opts ? opts->checksum_enabled : 0, 1);
2113 if (opts && opts->dict && opts->dict_size > 0) {
2117 const uint8_t type = ((
const uint8_t*)src)[0];
2119 if (type == ZXC_BLOCK_RAW) {
2124 const int checksum_enabled = opts ? opts->checksum_enabled : 0;
2126 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
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;
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))
2158 if (UNLIKELY(level < ZXC_LEVEL_FASTEST || level >
ZXC_LEVEL_ULTRA))
return 0;
2160 if (UNLIKELY(inner_sz == 0))
return 0;
2161 return ZXC_STATIC_CCTX_HDR_SIZE + inner_sz;
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;
2171 if (UNLIKELY(inner_sz == 0))
return NULL;
2172 if (UNLIKELY(workspace_size < ZXC_STATIC_CCTX_HDR_SIZE + inner_sz))
return NULL;
2175 ZXC_MEMSET(cctx, 0,
sizeof(*cctx));
2177 uint8_t*
const inner_ws = (uint8_t*)workspace + ZXC_STATIC_CCTX_HDR_SIZE;
2182 cctx->owns_workspace = 1;
2183 cctx->initialized = 1;
2184 cctx->last_block_size = block_size;
2185 cctx->init_level = level;
2187 cctx->params.level = level;
2188 cctx->params.block_size = block_size;
2202 if (UNLIKELY(inner_sz == 0))
return 0;
2203 return ZXC_STATIC_DCTX_HDR_SIZE + inner_sz;
2207 const size_t block_size) {
2208 if (UNLIKELY(!workspace))
return NULL;
2212 if (UNLIKELY(inner_sz == 0))
return NULL;
2213 if (UNLIKELY(workspace_size < ZXC_STATIC_DCTX_HDR_SIZE + inner_sz))
return NULL;
2216 ZXC_MEMSET(dctx, 0,
sizeof(*dctx));
2218 uint8_t*
const inner_ws = (uint8_t*)workspace + ZXC_STATIC_DCTX_HDR_SIZE;
2225 dctx->owns_workspace = 1;
2226 dctx->initialized = 1;
2227 dctx->last_block_size = block_size;
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)))
2247 ZXC_MEMSET(&cctx, 0,
sizeof(cctx));
2249 if (level != 0) cctx.params.level = level;
2263int64_t
zxc_decompress(
const void* RESTRICT src,
const size_t src_size,
void* RESTRICT dst,
2264 const size_t dst_capacity) {
2268 if (UNLIKELY(!dst || dst_capacity == 0))
2270 return zxc_decompress_oneshot((
const uint8_t*)src, src_size, (uint8_t*)dst, dst_capacity, NULL);
2281 const uint8_t* comp = NULL;
2283 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
2285 ZXC_MEMSET(&dctx, 0,
sizeof(dctx));
2287 buffer_capacity, NULL, 1, NULL);
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).
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).
#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.
#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.
@ ZXC_ERROR_OVERFLOW
Buffer overflow detected during processing.
@ ZXC_ERROR_CORRUPT_DATA
Corrupted compressed data.
@ ZXC_ERROR_NULL_INPUT
Required input pointer is NULL.
@ ZXC_ERROR_DICT_MISMATCH
Provided dictionary ID does not match the file header.
@ ZXC_ERROR_MEMORY
Memory allocation failure.
@ ZXC_ERROR_BAD_HEADER
Corrupted or invalid header (checksum mismatch).
@ ZXC_ERROR_DST_TOO_SMALL
Destination buffer too small.
@ ZXC_ERROR_SRC_TOO_SMALL
Source buffer too small or truncated input.
@ ZXC_ERROR_DICT_REQUIRED
File requires a dictionary but none was provided.
@ ZXC_ERROR_DICT_UNSUPPORTED
Dictionary not supported by this context's workspace, or library built without dictionary support.
@ ZXC_ERROR_BAD_CHECKSUM
A block's decoded bytes fail its checksum, or the archive digest mismatches.
@ ZXC_ERROR_BAD_PARAM
Unknown parameter, or value out of its bounds (zxc_cctx_set_param(), zxc_dctx_set_param()).
@ ZXC_ERROR_BAD_LEVEL
Compression level not supported by this context's workspace.
@ ZXC_OK
Success (no error).
@ ZXC_ERROR_BAD_BLOCK_SIZE
Invalid block size.
@ ZXC_ERROR_DICT_TOO_LARGE
Dictionary exceeds maximum allowed size.
#define ZXC_MALLOC(size)
Heap allocator.
#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.
#define ZXC_FREE(ptr)
Heap deallocator.
#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.
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...
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.
uint8_t has_checksum
1 when blocks carry checksums and the footer a digest.
size_t block_size
Block size, 4 KB to 2 MB.
uint64_t decompressed_size
Source bytes the frame decodes to.
uint8_t has_seek_table
1 when a seek table precedes the footer.
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.
void zxc_cctx_free(zxc_cctx_t *ctx)
Releases all resources owned by a compression context.
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.
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_...
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.
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...
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.
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.
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.
int zxc_container_next(const zxc_scan_src_t *src, const uint64_t pos)
Tells whether a frame starts at pos.
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,...
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_...
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.
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...
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.
int zxc_cctx_attach_dict_huf(zxc_cctx_t *ctx, const uint8_t *lengths)
Attach the shared dictionary literal Huffman table to a context.
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.
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.
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().
@ ZXC_C_CHECKSUM
1 appends the block checksums and the archive digest; default 0.
@ ZXC_C_LEVEL
Compression level, 1 to 7 (zxc_compression_level_t); default ZXC_LEVEL_DEFAULT.
@ 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...
@ ZXC_C_THREADS
Workers of the FILE* pipeline, at most ZXC_MAX_THREADS; the reader and writer come on top.
@ ZXC_C_SEEKABLE
1 appends a seek table for random-access decompression; default 0.
zxc_dparam_t
Decompression parameters, set on a zxc_dctx with zxc_dctx_set_param().
@ ZXC_D_CHECKSUM
1 verifies the block checksums and the archive digest when the archive carries them; default 0,...
@ ZXC_D_THREADS
Workers of the FILE* pipeline, at most ZXC_MAX_THREADS; the reader and writer come on top.
void(* zxc_progress_callback_t)(uint64_t bytes_processed, uint64_t bytes_total, const void *user_data)
Progress callback function type.