ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_seekable.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
39
41
44#include "zxc_internal.h"
45#include "zxc_threads.h"
46
47// =========================================================================
48// Seekable Reader (Opaque Handle)
49// =========================================================================
50
55typedef struct {
56 uint64_t base; /* archive offset of its file header */
57 uint64_t decomp_base; /* decompressed offset of its first byte */
58 uint64_t block_base; /* archive-wide index of its first block */
59 uint64_t num_blocks; /* ceil(total_decomp / block_size) */
60 uint64_t total_decomp; /* from its footer */
61 uint64_t table_off; /* first table entry, absolute */
62 uint64_t eof_off; /* EOF block header, relative to base like the anchors */
63 uint64_t entry_max; /* largest legal block on disk: header + block_size + checksum */
64 uint32_t block_size; /* a power of 2 in [4 KB, 2 MB] */
65 uint32_t dict_id; /* meaningful when has_dict */
66 int has_dict;
67 int has_checksums;
68} zxc_seek_frame_t;
69
71#define ZXC_SEEK_MAX_FRAMES (1U << 20)
72
74#define ZXC_SEEK_TAIL_HDRS (2 * ZXC_BLOCK_HEADER_SIZE)
75
77typedef struct {
78 uint8_t* data;
79 size_t size;
80 uint32_t id;
81 int has_huf;
82 uint8_t huf[ZXC_HUF_TABLE_SIZE]; /* shared literal table, when has_huf */
83} zxc_seek_dict_t;
84
85struct zxc_seekable_s {
86 // Source - exactly one of {src, reader.read_at} is set. The FILE* variant
87 // wraps pread() in its own reader ctx, indistinguishable from here.
88 const uint8_t* src;
89 uint64_t src_size;
90 zxc_reader_t reader; /* user-supplied callback reader; read_at == NULL when unused */
91
92 // Reader context owned by the handle and freed in zxc_seekable_free, set by
93 // the thin wrappers. NULL when the caller owns reader.ctx itself.
94 void* owned_reader_ctx;
95
96 // Frames in archive order; their tables stay on disk.
97 zxc_seek_frame_t* frames;
98 size_t num_frames;
99 uint64_t num_blocks; /* over every frame */
100 uint64_t total_decomp; /* over every frame */
101 uint32_t max_block_size; /* sizes the decoding contexts */
102
103 int verify_checksums; /* caller's switch; needs the frame's checksums too */
104
105 // Reusable decompression context and compressed-block scratch. Both belong
106 // to the single-threaded path, which is already not reentrant per handle;
107 // the multi-threaded path gives each worker its own.
108 zxc_cctx_t dctx;
109 int dctx_initialized;
110 const zxc_seek_dict_t* dctx_dict; /* the dictionary dctx holds, NULL for none */
111 uint8_t* read_buf;
112 size_t read_buf_cap;
113
114 // Dictionaries installed by zxc_seekable_set_dict, one per id.
115 zxc_seek_dict_t* dicts;
116 size_t num_dicts;
117 size_t max_dict_size; /* sizes the contexts' [dict | decode] buffers */
118};
119
129typedef struct {
130 const uint8_t* data; /* in-memory archive, NULL in callback mode */
131 const zxc_reader_t* rdr; /* callback mode, NULL in buffer mode */
132 uint64_t size; /* archive size, both modes */
133} zxc_seek_source_t;
134
141static int zxc_seek_source_read(const zxc_seek_source_t* src, void* dst, const size_t len,
142 const uint64_t off) {
143 if (UNLIKELY(off > src->size || (uint64_t)len > src->size - off))
145 if (src->data) {
146 ZXC_MEMCPY(dst, src->data + off, len);
147 return ZXC_OK;
148 }
149 const int64_t r = src->rdr->read_at(src->rdr->ctx, dst, len, off);
150 // A code outside int would truncate, possibly to ZXC_OK.
151 if (UNLIKELY(r != (int64_t)len)) return (r < 0 && r >= INT_MIN) ? (int)r : ZXC_ERROR_IO;
152 return ZXC_OK;
153}
154
158static zxc_seek_source_t zxc_seek_source_of(const zxc_seekable* s) {
159 const zxc_seek_source_t src = {s->src, s->src ? NULL : &s->reader, s->src_size};
160 return src;
161}
162
164static int zxc_seek_scan_read(const zxc_scan_src_t* scan, const uint64_t off, void* dst,
165 const size_t len) {
166 return zxc_seek_source_read((const zxc_seek_source_t*)scan->ctx, dst, len, off);
167}
168
179static int zxc_seek_parse_frame(zxc_seek_source_t* src, const uint64_t end, zxc_seek_frame_t* f) {
180 const zxc_scan_src_t scan = {zxc_seek_scan_read, NULL, src, end};
182 uint64_t start = 0;
183 size_t footer_len = 0;
184 int has_dict = 0;
185 if (UNLIKELY(zxc_scan_frame(&scan, end, &start, &fi, &footer_len, &has_dict) != ZXC_OK ||
186 !fi.has_seek_table))
187 return 0;
188 const uint64_t frame = end - start;
189 const uint32_t block_size = (uint32_t)fi.block_size;
190 const uint64_t num_blocks = zxc_seek_block_count(fi.decompressed_size, block_size);
191
192 // Layout: [header 16][data blocks][EOF 8][SEK block][footer], in 64 bits:
193 // the SEK header's field only holds the table size modulo 2^32.
194 const uint64_t seek_block_total = ZXC_BLOCK_HEADER_SIZE + zxc_seek_table_bytes(num_blocks);
195 if (UNLIKELY(seek_block_total + footer_len >
197 return 0;
198 const uint64_t eof_off = frame - footer_len - seek_block_total - ZXC_BLOCK_HEADER_SIZE;
199
200 // Geometry only, no table read: num_blocks blocks of [header, entry_max] bytes
201 // must fit the data area. Groups are checked on access.
202 const uint64_t entry_max = (uint64_t)ZXC_BLOCK_HEADER_SIZE + block_size +
204 const uint64_t data_area = eof_off - ZXC_FILE_HEADER_SIZE;
205 const uint64_t min_span = num_blocks * ZXC_BLOCK_HEADER_SIZE;
206 const uint64_t max_span =
207 num_blocks > UINT64_MAX / entry_max ? UINT64_MAX : num_blocks * entry_max;
208 if (UNLIKELY(data_area < min_span || data_area > max_span)) return 0;
209
210 f->base = start;
211 f->num_blocks = num_blocks;
212 f->total_decomp = fi.decompressed_size;
213 f->table_off = start + eof_off + 2 * ZXC_BLOCK_HEADER_SIZE;
214 f->eof_off = eof_off;
215 f->entry_max = entry_max;
216 f->block_size = block_size;
217 f->has_checksums = fi.has_checksum;
218 f->dict_id = fi.dict_id;
219 f->has_dict = has_dict;
220 return 1;
221}
222
227static zxc_seekable* zxc_seekable_parse(const zxc_seek_source_t* source) {
228 zxc_seek_source_t src = *source;
229 zxc_seek_frame_t* frames = NULL;
230 size_t n = 0;
231 size_t cap = 0;
232 uint64_t end = src.size;
233 do {
234 if (UNLIKELY(n == ZXC_SEEK_MAX_FRAMES)) goto fail;
235 if (n == cap) {
236 cap = cap ? 2 * cap : 4;
237 zxc_seek_frame_t* const grown =
238 (zxc_seek_frame_t*)ZXC_REALLOC(frames, cap * sizeof(*frames));
239 if (UNLIKELY(!grown)) goto fail; // LCOV_EXCL_LINE
240 frames = grown;
241 }
242 if (UNLIKELY(!zxc_seek_parse_frame(&src, end, &frames[n]))) goto fail;
243 // The last frame's EOF/SEK headers (a plain archive's only frame) now: a
244 // lying seek flag fails the open.
245 if (n == 0) {
246 uint8_t hdrs[ZXC_SEEK_TAIL_HDRS];
247 const zxc_seek_frame_t* const f = &frames[0];
248 if (UNLIKELY(zxc_seek_source_read(&src, hdrs, sizeof(hdrs),
249 f->table_off - ZXC_SEEK_TAIL_HDRS) != ZXC_OK ||
250 zxc_check_eof_header(hdrs) != ZXC_OK ||
252 f->total_decomp, f->block_size, NULL) != ZXC_OK))
253 goto fail;
254 }
255 end = frames[n++].base;
256 } while (end > 0);
257
258 // Walked back: archive order, then running bytes and blocks.
259 uint64_t decomp = 0;
260 uint64_t blocks = 0;
261 uint32_t max_block_size = 0;
262 for (size_t i = 0; i < n / 2; i++) {
263 const zxc_seek_frame_t t = frames[i];
264 frames[i] = frames[n - 1 - i];
265 frames[n - 1 - i] = t;
266 }
267 for (size_t i = 0; i < n; i++) {
268 zxc_seek_frame_t* const f = &frames[i];
269 if (UNLIKELY(f->total_decomp > UINT64_MAX - decomp)) goto fail;
270 f->decomp_base = decomp;
271 f->block_base = blocks;
272 decomp += f->total_decomp;
273 blocks += f->num_blocks; // no wrap: each frame holds at least 8 bytes per block
274 if (f->block_size > max_block_size) max_block_size = f->block_size;
275 }
276
277 zxc_seekable* const s = (zxc_seekable*)ZXC_CALLOC(1, sizeof(zxc_seekable));
278 if (UNLIKELY(!s)) goto fail; // LCOV_EXCL_LINE
279 if (src.rdr) s->reader = *src.rdr;
280 s->src = src.data;
281 s->src_size = src.size;
282 s->frames = frames;
283 s->num_frames = n;
284 s->num_blocks = blocks;
285 s->total_decomp = decomp;
286 s->max_block_size = max_block_size;
287 s->verify_checksums = 0; /* opt-in, see zxc_seekable_set_checksum */
288 return s;
289
290fail:
291 ZXC_FREE(frames);
292 return NULL;
293}
294
296static const zxc_seek_frame_t* zxc_seek_frame_of_block(const zxc_seekable* s, const uint64_t idx) {
297 size_t lo = 0;
298 size_t hi = s->num_frames;
299 while (lo < hi) { // the first frame starting past idx; the one before holds it
300 const size_t mid = lo + (hi - lo) / 2;
301 if (s->frames[mid].block_base <= idx)
302 lo = mid + 1;
303 else
304 hi = mid;
305 }
306 return &s->frames[lo - 1];
307}
308
310static uint64_t zxc_seek_block_of_offset(const zxc_seekable* s, const uint64_t offset) {
311 size_t lo = 0;
312 size_t hi = s->num_frames;
313 while (lo < hi) {
314 const size_t mid = lo + (hi - lo) / 2;
315 if (s->frames[mid].decomp_base <= offset)
316 lo = mid + 1;
317 else
318 hi = mid;
319 }
320 const zxc_seek_frame_t* const f = &s->frames[lo - 1];
321 return f->block_base + ((offset - f->decomp_base) >> zxc_ctz32(f->block_size));
322}
323
326static size_t zxc_seek_spans_raw_max(const uint64_t first, const uint32_t n) {
327 const uint64_t groups = (first + n - 1) / ZXC_SEEK_GROUP - first / ZXC_SEEK_GROUP + 1;
328 return ZXC_SEEK_TAIL_HDRS + (size_t)groups * ZXC_SEEK_GROUP_BYTES;
329}
330
352static int zxc_seek_load_spans(const zxc_seekable* s, const zxc_seek_frame_t* f,
353 const uint64_t first, const uint32_t n, uint64_t* RESTRICT starts,
354 uint32_t* RESTRICT sizes, uint8_t* RESTRICT raw) {
355 const uint64_t last_group = zxc_seek_group_count(f->num_blocks) - 1;
356 const uint64_t g0 = first / ZXC_SEEK_GROUP;
357 const uint64_t g1 = (first + n - 1) / ZXC_SEEK_GROUP;
358
359 size_t need = 0;
360 for (uint64_t g = g0; g <= g1; g++)
361 need += ZXC_SEEK_ANCHOR_SIZE +
362 (size_t)zxc_seek_group_len(f->num_blocks, g) * ZXC_SEEK_SIZE_ENTRY;
363 // The EOF/SEK headers, checked on each access rather than kept as state; read
364 // with group 0 when it is wanted.
365 const zxc_seek_source_t src = zxc_seek_source_of(s);
366 const uint64_t at = f->table_off + g0 * ZXC_SEEK_GROUP_BYTES;
367 int rc = g0 == 0 ? zxc_seek_source_read(&src, raw, ZXC_SEEK_TAIL_HDRS + need,
370 f->table_off - ZXC_SEEK_TAIL_HDRS);
371 if (rc == ZXC_OK && g0 != 0)
372 rc = zxc_seek_source_read(&src, raw + ZXC_SEEK_TAIL_HDRS, need, at);
373 if (UNLIKELY(rc != ZXC_OK)) return rc;
374 if (UNLIKELY(zxc_check_eof_header(raw) != ZXC_OK ||
376 f->total_decomp, f->block_size, NULL) != ZXC_OK))
378
379 const uint8_t* p = raw + ZXC_SEEK_TAIL_HDRS;
380 for (uint64_t g = g0; g <= g1; g++) {
381 uint64_t pos = zxc_le64(p);
383 if (UNLIKELY(g == 0 ? pos != ZXC_FILE_HEADER_SIZE
384 : pos < ZXC_FILE_HEADER_SIZE || pos > f->eof_off))
386
387 const uint32_t cnt = zxc_seek_group_len(f->num_blocks, g);
388 for (uint32_t k = 0; k < cnt; k++, p += ZXC_SEEK_SIZE_ENTRY) {
389 const uint32_t sz = zxc_le32(p);
390 if (UNLIKELY(sz < ZXC_BLOCK_HEADER_SIZE || sz > f->entry_max))
392 const uint64_t idx = g * ZXC_SEEK_GROUP + k;
393 if (idx >= first && idx < first + n) {
394 starts[idx - first] = f->base + pos;
395 sizes[idx - first] = sz;
396 }
397 pos += sz; // no wrap: at most ZXC_SEEK_GROUP sizes of at most entry_max
398 }
399 // Within the data area; the last group, exactly on the EOF block.
400 if (UNLIKELY(pos > f->eof_off || (g == last_group && pos != f->eof_off)))
402 }
403 return ZXC_OK;
404}
405
412zxc_seekable* zxc_seekable_open(const void* src, const size_t src_size) {
413 if (UNLIKELY(!src || src_size == 0)) return NULL;
414 const zxc_seek_source_t source = {(const uint8_t*)src, NULL, (uint64_t)src_size};
415 return zxc_seekable_parse(&source);
416}
417
418// zxc_seekable_open_file lives elsewhere: it builds a zxc_reader_t over pread()
419// and delegates below, keeping this TU free of <stdio.h>.
420
430 if (UNLIKELY(!r || !r->read_at || r->size == 0)) return NULL;
431 const zxc_seek_source_t source = {NULL, r, r->size};
432 return zxc_seekable_parse(&source);
433}
434
438uint64_t zxc_seekable_get_num_blocks(const zxc_seekable* s) { return s ? s->num_blocks : 0; }
439
444 return s ? s->total_decomp : 0;
445}
446
450uint32_t zxc_seekable_get_block_comp_size(const zxc_seekable* s, const uint64_t block_idx) {
451 if (UNLIKELY(!s || block_idx >= s->num_blocks)) return 0;
452 const zxc_seek_frame_t* const f = zxc_seek_frame_of_block(s, block_idx);
453 uint64_t start = 0;
454 uint32_t size = 0;
456 if (UNLIKELY(zxc_seek_load_spans(s, f, block_idx - f->block_base, 1, &start, &size, raw) !=
457 ZXC_OK))
458 return 0;
459 return size;
460}
461
473static uint32_t zxc_seek_decomp_size(const uint32_t block_size, const uint64_t total_decomp,
474 const uint64_t idx) {
475 const uint64_t start = idx * (uint64_t)block_size;
476 const uint64_t remaining = total_decomp - start;
477 return (remaining >= (uint64_t)block_size) ? block_size : (uint32_t)remaining;
478}
479
481uint32_t zxc_seekable_get_block_decomp_size(const zxc_seekable* s, const uint64_t block_idx) {
482 if (UNLIKELY(!s || block_idx >= s->num_blocks)) return 0;
483 const zxc_seek_frame_t* const f = zxc_seek_frame_of_block(s, block_idx);
484 return zxc_seek_decomp_size(f->block_size, f->total_decomp, block_idx - f->block_base);
485}
486
487// =========================================================================
488// Random-Access Decompression
489// =========================================================================
490
498static int zxc_seek_use_dict(const zxc_seekable* s, zxc_cctx_t* ctx, const zxc_seek_dict_t** held,
499 const zxc_seek_frame_t* f) {
500#ifdef ZXC_NO_DICT
501 if (UNLIKELY(f->dict_id)) return ZXC_ERROR_DICT_UNSUPPORTED;
502#endif
503 const zxc_seek_dict_t* d = NULL;
504 for (size_t i = 0; f->has_dict && i < s->num_dicts && !d; i++)
505 if (s->dicts[i].id == f->dict_id) d = &s->dicts[i];
506 if (UNLIKELY(f->has_dict && !d)) return ZXC_ERROR_DICT_REQUIRED;
507 if (d != *held) {
508 if (d) ZXC_MEMCPY(ctx->dict_buffer, d->data, d->size);
509 if (UNLIKELY(zxc_cctx_attach_dict_huf(ctx, d && d->has_huf ? d->huf : NULL) != ZXC_OK))
510 return ZXC_ERROR_CORRUPT_DATA; // LCOV_EXCL_LINE
511 *held = d;
512 }
513 ctx->dict_size = d ? d->size : 0;
514 return ZXC_OK;
515}
516
517// A block decodes into a capacity of its own, never the caller's: running out
518// of it is corruption.
519static int zxc_seek_decode_error(const int res) {
521 : res;
522}
523
545static int zxc_seek_read_block(const zxc_seekable* s, const uint64_t off, const uint32_t csz,
546 const int has_cs, uint8_t* buf, const size_t buf_cap,
547 const uint8_t** blk) {
548 if (s->src) {
549 if (UNLIKELY(off > s->src_size || (uint64_t)csz > s->src_size - off))
551 *blk = s->src + off;
552 } else {
553 if (UNLIKELY(csz > buf_cap)) return ZXC_ERROR_DST_TOO_SMALL;
554 const zxc_seek_source_t src = zxc_seek_source_of(s);
555 const int rc = zxc_seek_source_read(&src, buf, csz, off);
556 if (UNLIKELY(rc != ZXC_OK)) return rc;
557 *blk = buf;
558 }
559
561 const int hdr_res = zxc_read_block_header(*blk, csz, &bh);
562 if (UNLIKELY(hdr_res != ZXC_OK)) return hdr_res;
563 const uint64_t on_disk =
564 (uint64_t)ZXC_BLOCK_HEADER_SIZE + bh.comp_size + (has_cs ? ZXC_BLOCK_CHECKSUM_SIZE : 0U);
565 if (UNLIKELY(on_disk != csz)) return ZXC_ERROR_CORRUPT_DATA;
566 return (int)csz;
567}
568
578int64_t zxc_seekable_decompress_range(zxc_seekable* s, void* dst, const size_t dst_capacity,
579 const uint64_t offset, const size_t len) {
580 if (UNLIKELY(len == 0)) return 0;
581 if (UNLIKELY(!s || !dst)) return ZXC_ERROR_NULL_INPUT;
582 if (UNLIKELY(dst_capacity < len)) return ZXC_ERROR_DST_TOO_SMALL;
583 if (UNLIKELY(offset > s->total_decomp || len > s->total_decomp - offset))
585
586 // Initialize decompression context on first use.
587 if (!s->dctx_initialized) {
588 if (UNLIKELY(zxc_cctx_init(&s->dctx, (size_t)s->max_block_size, 0, 0, 0,
589 s->max_dict_size) != ZXC_OK))
590 return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
591 s->dctx_initialized = 1;
592 s->dctx_dict = NULL;
593 }
594
595 const uint64_t blk_start = zxc_seek_block_of_offset(s, offset);
596 const uint64_t blk_end = zxc_seek_block_of_offset(s, offset + len - 1);
597 const zxc_seek_frame_t* f = zxc_seek_frame_of_block(s, blk_start);
598
599 uint8_t* out = (uint8_t*)dst;
600 size_t remaining = len;
601
602 // One slice, and one read, per table group of a frame.
603 uint64_t starts[ZXC_SEEK_GROUP] = {0};
604 uint32_t sizes[ZXC_SEEK_GROUP] = {0};
606 uint64_t bi = blk_start;
607 while (bi <= blk_end) {
608 while (bi >= f->block_base + f->num_blocks) f++; // past this frame, empty ones too
609 const uint64_t local = bi - f->block_base;
610 const uint64_t left = blk_end - bi + 1;
611 const uint64_t in_frame = f->num_blocks - local;
612 const uint32_t to_group_end = ZXC_SEEK_GROUP - (uint32_t)(local % ZXC_SEEK_GROUP);
613 uint32_t n = left < to_group_end ? (uint32_t)left : to_group_end;
614 if (in_frame < n) n = (uint32_t)in_frame;
615 const int span_res = zxc_seek_load_spans(s, f, local, n, starts, sizes, raw);
616 if (UNLIKELY(span_res < 0)) return span_res;
617
618 // A reader's block scratch, sized to the slice's largest block, not entry_max.
619 if (!s->src) {
620 uint64_t need = 0;
621 for (uint32_t k = 0; k < n; k++)
622 if (sizes[k] > need) need = sizes[k];
623 need += ZXC_PAD_SIZE;
624 if (s->read_buf_cap < need) {
625 uint8_t* const nb = (uint8_t*)ZXC_REALLOC(s->read_buf, (size_t)need);
626 if (UNLIKELY(!nb)) return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
627 s->read_buf = nb;
628 s->read_buf_cap = (size_t)need;
629 }
630 }
631
632 // The frame's geometry and dictionary: the context is sized for the largest.
633 const int dict_res = zxc_seek_use_dict(s, &s->dctx, &s->dctx_dict, f);
634 if (UNLIKELY(dict_res != ZXC_OK)) return dict_res;
635 s->dctx.chunk_size = f->block_size;
636 s->dctx.checksum_enabled = f->has_checksums && s->verify_checksums;
637 const size_t work_sz = (size_t)f->block_size + ZXC_DECOMPRESS_TAIL_PAD;
638
639 for (uint32_t k = 0; k < n; k++, bi++) {
640 const uint64_t lb = local + k; // position in the frame: the checksum seed
641 const uint8_t* blk = NULL;
642 const int read_res = zxc_seek_read_block(s, starts[k], sizes[k], f->has_checksums,
643 s->read_buf, s->read_buf_cap, &blk);
644 if (UNLIKELY(read_res < 0)) return read_res;
645
646 // The wanted part of the block
647 const uint32_t dec_sz = zxc_seek_decomp_size(f->block_size, f->total_decomp, lb);
648 const uint64_t blk_decomp_start = f->decomp_base + lb * f->block_size;
649 const size_t skip =
650 (offset > blk_decomp_start) ? (size_t)(offset - blk_decomp_start) : 0;
651 const size_t avail = (size_t)dec_sz - skip;
652 const size_t copy = (avail < remaining) ? avail : remaining;
653
654 // A whole block decodes in place if the fast decoder's margin stays in
655 // the range (the next blocks overwrite it); otherwise aside, behind the
656 // dict prefix if any, then copied.
657 const int direct =
658 !s->dctx.dict_size && skip == 0 && remaining - copy >= ZXC_DECOMPRESS_TAIL_PAD;
659 uint8_t* const aside =
660 s->dctx.dict_buffer ? s->dctx.dict_buffer + s->dctx.dict_size : s->dctx.work_buf;
661 uint8_t* const dec_dst = direct ? out : aside;
662 const size_t dec_cap = direct ? (size_t)dec_sz + ZXC_DECOMPRESS_TAIL_PAD : work_sz;
663 const int dec_res =
664 zxc_decompress_chunk_wrapper(&s->dctx, blk, (size_t)read_res, dec_dst, dec_cap, lb);
665 if (UNLIKELY(dec_res < 0)) return zxc_seek_decode_error(dec_res);
666 if (UNLIKELY((uint32_t)dec_res != dec_sz)) return ZXC_ERROR_CORRUPT_DATA;
667
668 if (!direct) ZXC_MEMCPY(out, aside + skip, copy);
669 out += copy;
670 remaining -= copy;
671 }
672 }
673
674 if (UNLIKELY(remaining != 0)) return ZXC_ERROR_CORRUPT_DATA;
675 return (int64_t)len;
676}
677
678// =========================================================================
679// Multi-Threaded Random-Access Decompression (Fork-Join)
680// =========================================================================
681
688typedef struct {
689 uint64_t block_idx; /* position in its frame: the checksum seed */
690 uint64_t off; /* where it starts in the archive (validated span) */
691 size_t dst_off; /* output offset from the caller's buffer start */
692 size_t skip; /* bytes to skip at start of decompressed block */
693 size_t copy_len; /* bytes to copy out */
694 uint32_t csz; /* its on-disk size */
695 uint32_t block_size; /* its frame's */
696 const zxc_seek_frame_t* frame;
697 uint32_t decomp_sz; /* what it decodes to */
698 int has_cs; /* its frame carries block checksums */
699 int result; /* 0 = OK, < 0 = error; starts negative, see the launch loop */
700} zxc_seek_mt_job_t;
701
715typedef struct {
716 const zxc_seekable* s; /* handle being read (read-only) */
717 zxc_seek_mt_job_t* jobs; /* job array; each worker touches only the jobs it claimed */
718 uint8_t* dst_base; /* start of the caller's output buffer */
719 uint32_t num_jobs; /* job count */
720 uint32_t batch; /* jobs per claim, the last one may be shorter */
721 uint32_t max_csz; /* largest on-disk block: sizes a reader's scratch */
722 uint32_t next; /* first job of the next batch, under lock */
723 int failed; /* a job failed: no further claims, under lock */
724 pthread_mutex_t lock; /* guards next and failed */
725} zxc_seek_mt_shared_t;
726
742static int zxc_seek_mt_job(const zxc_seek_mt_shared_t* sh, zxc_seek_mt_job_t* job, zxc_cctx_t* dctx,
743 const zxc_seek_dict_t** held, uint8_t* read_buf, const size_t read_cap) {
744 const zxc_seekable* const s = sh->s;
745
746 const uint8_t* blk = NULL;
747 const int read_res =
748 zxc_seek_read_block(s, job->off, job->csz, job->has_cs, read_buf, read_cap, &blk);
749 if (UNLIKELY(read_res < 0)) return job->result = read_res;
750
751 const int dict_res = zxc_seek_use_dict(s, dctx, held, job->frame);
752 if (UNLIKELY(dict_res != ZXC_OK)) return job->result = dict_res;
753 dctx->chunk_size = job->block_size;
754 dctx->checksum_enabled = job->has_cs && s->verify_checksums;
755
756 // Behind the dict prefix, if any, for back-references into it.
757 uint8_t* const dec_dst =
758 dctx->dict_buffer ? dctx->dict_buffer + dctx->dict_size : dctx->work_buf;
759 const int dec_res = zxc_decompress_chunk_wrapper(
760 dctx, blk, (size_t)read_res, dec_dst, (size_t)job->block_size + ZXC_DECOMPRESS_TAIL_PAD,
761 job->block_idx);
762 if (UNLIKELY(dec_res < 0)) return job->result = zxc_seek_decode_error(dec_res);
763 if (UNLIKELY((uint32_t)dec_res != job->decomp_sz)) return job->result = ZXC_ERROR_CORRUPT_DATA;
764
765 ZXC_MEMCPY(sh->dst_base + job->dst_off, dec_dst + job->skip, job->copy_len);
766 return job->result = ZXC_OK;
767}
768
782static void* zxc_seek_mt_worker(void* arg) {
783 zxc_seek_mt_shared_t* const sh = (zxc_seek_mt_shared_t*)arg;
784 const zxc_seekable* const s = sh->s;
785
786 // Thread-local decompression context (mode=0 for decompress-only)
787 zxc_cctx_t dctx;
788 if (UNLIKELY(zxc_cctx_init(&dctx, (size_t)s->max_block_size, 0, 0, 0, s->max_dict_size) !=
789 ZXC_OK))
790 return NULL; // LCOV_EXCL_LINE
791 const zxc_seek_dict_t* held = NULL;
792
793 // Block scratch, for a reader only.
794 const size_t read_cap = (size_t)sh->max_csz + ZXC_PAD_SIZE;
795 uint8_t* read_buf = NULL;
796 if (!s->src) {
797 read_buf = (uint8_t*)ZXC_MALLOC(read_cap);
798 if (UNLIKELY(!read_buf)) {
799 // LCOV_EXCL_START
800 zxc_cctx_free(&dctx);
801 return NULL;
802 // LCOV_EXCL_STOP
803 }
804 }
805
806 int rc = ZXC_OK;
807 while (rc == ZXC_OK) {
808 pthread_mutex_lock(&sh->lock);
809 const uint32_t first = sh->failed ? sh->num_jobs : sh->next;
810 const uint32_t last = sh->num_jobs - first > sh->batch ? first + sh->batch : sh->num_jobs;
811 sh->next = last;
812 pthread_mutex_unlock(&sh->lock);
813 if (first == last) break;
814
815 for (uint32_t i = first; i < last && rc == ZXC_OK; i++)
816 rc = zxc_seek_mt_job(sh, &sh->jobs[i], &dctx, &held, read_buf, read_cap);
817 }
818 if (UNLIKELY(rc != ZXC_OK)) {
819 pthread_mutex_lock(&sh->lock);
820 sh->failed = 1;
821 pthread_mutex_unlock(&sh->lock);
822 }
823
824 ZXC_FREE(read_buf);
825 zxc_cctx_free(&dctx);
826 return NULL;
827}
828
838int64_t zxc_seekable_decompress_range_mt(zxc_seekable* s, void* dst, const size_t dst_capacity,
839 const uint64_t offset, const size_t len, int n_threads) {
840 if (UNLIKELY(len == 0)) return 0;
841 if (UNLIKELY(!s || !dst)) return ZXC_ERROR_NULL_INPUT;
842 if (UNLIKELY(dst_capacity < len)) return ZXC_ERROR_DST_TOO_SMALL;
843 if (UNLIKELY(offset > s->total_decomp || len > s->total_decomp - offset))
845
846 const uint64_t blk_start = zxc_seek_block_of_offset(s, offset);
847 const uint64_t blk_end = zxc_seek_block_of_offset(s, offset + len - 1);
848 const uint64_t jobs_64 = blk_end - blk_start + 1;
849
850 // Auto-detect thread count (0 = use all available cores)
851 if (n_threads == 0) n_threads = zxc_num_procs();
852
853 // Fallback to single-threaded path for trivial cases, and for a range holding
854 // more blocks than one job table can index (4 G blocks is 16 TiB of dst).
855 if (n_threads <= 1 || jobs_64 <= 1 || jobs_64 > UINT32_MAX) {
856 return zxc_seekable_decompress_range(s, dst, dst_capacity, offset, len);
857 }
858 const uint32_t num_jobs = (uint32_t)jobs_64;
859
860 // About ZXC_MT_BATCH_BYTES of output per batch, less if that would idle
861 // threads; no more threads than batches (two or more: batch <= ceil(jobs/2)).
862 if (n_threads > ZXC_MAX_THREADS) n_threads = ZXC_MAX_THREADS;
863 const uint32_t share = (num_jobs - 1) / (uint32_t)n_threads + 1;
864 uint32_t batch = s->max_block_size < ZXC_MT_BATCH_BYTES
865 ? (uint32_t)(ZXC_MT_BATCH_BYTES / s->max_block_size)
866 : 1;
867 if (batch > share) batch = share;
868 if (UNLIKELY(batch == 0)) batch = 1;
869 const uint32_t num_batches = num_jobs / batch + (num_jobs % batch != 0);
870 if ((uint32_t)n_threads > num_batches) n_threads = (int)num_batches;
871
872 zxc_seek_mt_job_t* const jobs =
873 (zxc_seek_mt_job_t*)ZXC_CALLOC(num_jobs, sizeof(zxc_seek_mt_job_t));
874 uint64_t* const starts = (uint64_t*)ZXC_CALLOC(num_jobs, sizeof(uint64_t));
875 uint32_t* const sizes = (uint32_t*)ZXC_CALLOC(num_jobs, sizeof(uint32_t));
876 if (UNLIKELY(!jobs || !starts || !sizes)) {
877 // LCOV_EXCL_START
878 ZXC_FREE(jobs);
879 ZXC_FREE(starts);
880 ZXC_FREE(sizes);
881 return ZXC_ERROR_MEMORY;
882 // LCOV_EXCL_STOP
883 }
884
885 // Plan jobs frame by frame, each frame's table groups in one read.
886 const zxc_seek_frame_t* f = zxc_seek_frame_of_block(s, blk_start);
887 size_t out_off = 0;
888 size_t remaining = len;
889 uint32_t max_csz = 0;
890 int plan_res = ZXC_OK;
891 for (uint32_t i = 0; i < num_jobs && plan_res == ZXC_OK;) {
892 const uint64_t bi = blk_start + i;
893 while (bi >= f->block_base + f->num_blocks) f++; // past this frame, empty ones too
894 const uint64_t local = bi - f->block_base;
895 const uint64_t in_frame = f->num_blocks - local;
896 const uint32_t n = in_frame < num_jobs - i ? (uint32_t)in_frame : num_jobs - i;
897 uint8_t* const raw = (uint8_t*)ZXC_MALLOC(zxc_seek_spans_raw_max(local, n));
898 plan_res = raw ? zxc_seek_load_spans(s, f, local, n, starts + i, sizes + i, raw)
899 : ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
900 ZXC_FREE(raw);
901 for (uint32_t k = 0; k < n && plan_res == ZXC_OK; k++, i++) {
902 const uint64_t lb = local + k;
903 const uint64_t blk_decomp_start = f->decomp_base + lb * f->block_size;
904 const size_t skip =
905 (offset > blk_decomp_start) ? (size_t)(offset - blk_decomp_start) : 0;
906 const uint32_t blk_decomp_sz = zxc_seek_decomp_size(f->block_size, f->total_decomp, lb);
907 const size_t avail = blk_decomp_sz - skip;
908 const size_t copy = (avail < remaining) ? avail : remaining;
909
910 jobs[i].block_idx = lb;
911 jobs[i].off = starts[i];
912 jobs[i].csz = sizes[i];
913 jobs[i].block_size = f->block_size;
914 jobs[i].frame = f;
915 jobs[i].decomp_sz = blk_decomp_sz;
916 jobs[i].has_cs = f->has_checksums;
917 jobs[i].dst_off = out_off;
918 jobs[i].skip = skip;
919 jobs[i].copy_len = copy;
920 // Negative until done: a job nobody took reports itself.
921 jobs[i].result = ZXC_ERROR_MEMORY;
922 if (sizes[i] > max_csz) max_csz = sizes[i];
923
924 out_off += copy;
925 remaining -= copy;
926 }
927 }
928 ZXC_FREE(starts);
929 ZXC_FREE(sizes);
930 if (UNLIKELY(plan_res != ZXC_OK)) {
931 ZXC_FREE(jobs);
932 return plan_res;
933 }
934
935 // Launch one persistent worker per thread
936 pthread_t* const threads = (pthread_t*)ZXC_MALLOC((size_t)n_threads * sizeof(pthread_t));
937 if (UNLIKELY(!threads)) {
938 // LCOV_EXCL_START
939 ZXC_FREE(jobs);
940 return ZXC_ERROR_MEMORY;
941 // LCOV_EXCL_STOP
942 }
943
944 zxc_seek_mt_shared_t sh;
945 sh.s = s;
946 sh.jobs = jobs;
947 sh.dst_base = (uint8_t*)dst;
948 sh.num_jobs = num_jobs;
949 sh.batch = batch;
950 sh.max_csz = max_csz;
951 sh.next = 0;
952 sh.failed = 0;
953 pthread_mutex_init(&sh.lock, NULL);
954
955 int launched = 0;
956 for (int t = 0; t < n_threads; t++) {
957 // A failed start leaves its jobs to the others; with none, all stay negative.
958 if (UNLIKELY(pthread_create(&threads[launched], NULL, zxc_seek_mt_worker, &sh) != 0))
959 continue; // LCOV_EXCL_LINE
960 launched++;
961 }
962
963 // Join phase
964 for (int t = 0; t < launched; t++) pthread_join(threads[t], NULL);
965
966 pthread_mutex_destroy(&sh.lock);
967 ZXC_FREE(threads);
968
969 // Report the first error in job order, if any.
970 int64_t result = (int64_t)len;
971 for (uint32_t i = 0; i < num_jobs; i++) {
972 if (jobs[i].result < 0) {
973 result = (int64_t)jobs[i].result;
974 break;
975 }
976 }
977
978 ZXC_FREE(jobs);
979 return result;
980}
981
989 if (UNLIKELY(!s)) return;
990 if (s->dctx_initialized) zxc_cctx_free(&s->dctx);
991 ZXC_FREE(s->frames);
992 for (size_t i = 0; i < s->num_dicts; i++) ZXC_FREE(s->dicts[i].data);
993 ZXC_FREE(s->dicts);
994 ZXC_FREE(s->read_buf);
995 ZXC_FREE(s->owned_reader_ctx);
996 ZXC_FREE(s);
997}
998
1005int zxc_seekable_set_checksum(zxc_seekable* s, const int enabled) {
1006 if (UNLIKELY(!s)) return ZXC_ERROR_NULL_INPUT;
1007 s->verify_checksums = enabled ? 1 : 0;
1008 return ZXC_OK;
1009}
1010
1019int zxc_seekable_set_dict(zxc_seekable* s, const void* dict, const size_t dict_size,
1020 const void* dict_huf) {
1021 if (UNLIKELY(!s || !dict || dict_size == 0)) return ZXC_ERROR_NULL_INPUT;
1022#ifdef ZXC_NO_DICT
1023 // No dictionary support, as zxc_dctx_set_dict().
1024 (void)dict_huf;
1026#else
1027 if (UNLIKELY(dict_size > ZXC_DICT_SIZE_MAX)) return ZXC_ERROR_DICT_TOO_LARGE;
1028 const uint32_t id = zxc_dict_compute_id(dict, dict_size, (const uint8_t*)dict_huf);
1029 int used = 0;
1030 int any = 0;
1031 for (size_t i = 0; i < s->num_frames && !used; i++) {
1032 used = s->frames[i].has_dict && s->frames[i].dict_id == id;
1033 any |= s->frames[i].has_dict;
1034 }
1035 // No frame needs a dictionary: ignore it, as before. Otherwise one must use it.
1036 if (!any) return ZXC_OK;
1037 if (UNLIKELY(!used)) return ZXC_ERROR_DICT_MISMATCH;
1038
1039 zxc_seek_dict_t* d = NULL;
1040 for (size_t i = 0; i < s->num_dicts && !d; i++)
1041 if (s->dicts[i].id == id) d = &s->dicts[i];
1042 if (!d) {
1043 zxc_seek_dict_t* const grown =
1044 (zxc_seek_dict_t*)ZXC_REALLOC(s->dicts, (s->num_dicts + 1) * sizeof(*s->dicts));
1045 if (UNLIKELY(!grown)) return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
1046 s->dicts = grown;
1047 d = &s->dicts[s->num_dicts];
1048 ZXC_MEMSET(d, 0, sizeof(*d));
1049 }
1050 uint8_t* const copy = (uint8_t*)ZXC_MALLOC(dict_size);
1051 if (UNLIKELY(!copy)) return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
1052 ZXC_MEMCPY(copy, dict, dict_size);
1053 if (d == &s->dicts[s->num_dicts]) s->num_dicts++;
1054 ZXC_FREE(d->data);
1055 d->data = copy;
1056 d->size = dict_size;
1057 d->id = id;
1058 d->has_huf = dict_huf != NULL;
1059 if (dict_huf) ZXC_MEMCPY(d->huf, dict_huf, ZXC_HUF_TABLE_SIZE);
1060 if (dict_size > s->max_dict_size) s->max_dict_size = dict_size;
1061
1062 // The context's [dict | decode] buffer and the table it holds are re-carved
1063 // and reloaded on the next decompress.
1064 if (s->dctx_initialized) {
1065 zxc_cctx_free(&s->dctx);
1066 s->dctx_initialized = 0;
1067 }
1068 return ZXC_OK;
1069#endif // ZXC_NO_DICT
1070}
1071
1081 if (s) s->owned_reader_ctx = ctx;
1082}
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_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_IO
Read/write/seek failure on file.
Definition zxc_error.h:61
@ 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_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_OK
Success (no error).
Definition zxc_error.h:38
@ ZXC_ERROR_DICT_TOO_LARGE
Dictionary exceeds maximum allowed size.
Definition zxc_error.h:71
#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
static uint32_t zxc_seek_group_len(const uint64_t nblocks, const uint64_t g)
Blocks in group g of nblocks: ZXC_SEEK_GROUP, fewer for the last.
#define ZXC_SEEK_GROUP
Blocks per group.
static uint64_t zxc_seek_table_bytes(const uint64_t nblocks)
Byte size of the groups for nblocks blocks, in 64 bits.
static uint64_t zxc_seek_group_count(const uint64_t nblocks)
Groups holding nblocks blocks; the last one may be partial.
#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_SEEK_GROUP_BYTES
Full group: anchor + ZXC_SEEK_GROUP sizes.
#define ZXC_FREE(ptr)
Heap deallocator.
Definition zxc_deps.h:86
#define ZXC_PAD_SIZE
Safety padding appended to buffers to tolerate overruns.
#define ZXC_BLOCK_HEADER_SIZE
Block header size: Type(1)+Flags(1)+Reserved(1)+Checksum(1)+CompSize(4).
#define ZXC_REALLOC(ptr, size)
In-place / move heap reallocator.
Definition zxc_deps.h:80
#define ZXC_MT_BATCH_BYTES
Output per multi-threaded decode job: smaller blocks travel several to a job, paying the thread hando...
#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.
int64_t zxc_seekable_decompress_range_mt(zxc_seekable *s, void *dst, const size_t dst_capacity, const uint64_t offset, const size_t len, int n_threads)
Multi-threaded variant of zxc_seekable_decompress_range.
void zxc_seekable_free(zxc_seekable *s)
Releases a seekable handle and every resource it owns.
int64_t zxc_seekable_decompress_range(zxc_seekable *s, void *dst, const size_t dst_capacity, const uint64_t offset, const size_t len)
Decompresses the byte range [offset, offset + len) into dst.
uint64_t zxc_seekable_get_num_blocks(const zxc_seekable *s)
Number of blocks in the archive.
uint32_t zxc_seekable_get_block_decomp_size(const zxc_seekable *s, const uint64_t block_idx)
Decompressed byte size of a given block.
uint64_t zxc_seekable_get_decompressed_size(const zxc_seekable *s)
Total decompressed size of the archive.
int zxc_seekable_set_dict(zxc_seekable *s, const void *dict, const size_t dict_size, const void *dict_huf)
Installs the dictionary needed to decode a dict-compressed archive.
uint32_t zxc_seekable_get_block_comp_size(const zxc_seekable *s, const uint64_t block_idx)
Compressed byte size of a given block, from its seek table entry.
int zxc_seekable_set_checksum(zxc_seekable *s, const int enabled)
Turns per-block checksum verification on or off.
zxc_seekable * zxc_seekable_open(const void *src, const size_t src_size)
Opens a seekable archive held entirely in a memory buffer.
zxc_seekable * zxc_seekable_open_reader(const zxc_reader_t *r)
Opens a seekable archive over a caller-supplied random-access reader.
struct zxc_seekable_s zxc_seekable
Opaque handle for a seekable ZXC archive.
#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.
Compression / decompression context.
uint8_t * work_buf
Padded scratch buffer for buffer-API decompression.
size_t chunk_size
Effective block size in bytes.
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).
int checksum_enabled
1 if checksum calculation/verification is enabled.
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
uint32_t dict_id
Dictionary ID; 0 is none or ID 0, see zxc_requires_dict().
Definition zxc_buffer.h:280
uint8_t has_seek_table
1 when a seek table precedes the footer.
Definition zxc_buffer.h:283
Storage-agnostic reader interface for seekable archives.
uint64_t size
Total size of the compressed archive in bytes.
int64_t(* read_at)(void *ctx, void *dst, size_t len, uint64_t offset)
Reads exactly len bytes at offset into dst.
void * ctx
Opaque user context passed unchanged to read_at.
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_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_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_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_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
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 no-dict chunk decoder (decompression hot path).
Pre-trained dictionary API for ZXC compression.
Error codes and error-name lookup for the ZXC library.
Internal definitions, constants, SIMD helpers, and utility functions.
static uint64_t zxc_le64(const void *p)
Reads a little-endian 64-bit value from a possibly unaligned address.
static int zxc_ctz32(const uint32_t x)
Counts trailing zeros, returning 32 for a zero input.
static uint32_t zxc_le32(const void *p)
Reads a little-endian 32-bit value from a possibly unaligned address.
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 const zxc_seek_frame_t * zxc_seek_frame_of_block(const zxc_seekable *s, const uint64_t idx)
The frame holding archive-wide block idx, below the block count.
static uint32_t zxc_seek_decomp_size(const uint32_t block_size, const uint64_t total_decomp, const uint64_t idx)
Decompressed size of block idx (O(1)).
static int zxc_seek_mt_job(const zxc_seek_mt_shared_t *sh, zxc_seek_mt_job_t *job, zxc_cctx_t *dctx, const zxc_seek_dict_t **held, uint8_t *read_buf, const size_t read_cap)
Reads one job's block, decodes it and copies the requested part out.
static int zxc_seek_load_spans(const zxc_seekable *s, const zxc_seek_frame_t *f, const uint64_t first, const uint32_t n, uint64_t *starts, uint32_t *sizes, uint8_t *raw)
Loads where each block of [first, first + n) starts and its on-disk size, both taken from the block's...
static void * zxc_seek_mt_worker(void *arg)
Worker thread entry point for multi-threaded seekable decompression.
void zxc_seekable_attach_owned_ctx(zxc_seekable *s, void *ctx)
Transfers ownership of a heap reader context to the handle.
static zxc_seek_source_t zxc_seek_source_of(const zxc_seekable *s)
The handle's archive as a read source, for the per-block readers.
static int zxc_seek_scan_read(const zxc_scan_src_t *scan, const uint64_t off, void *dst, const size_t len)
zxc_scan_src_t over a zxc_seek_source_t.
#define ZXC_SEEK_TAIL_HDRS
EOF and SEK block headers, ahead of a frame's table: checked when it is read.
static int zxc_seek_parse_frame(zxc_seek_source_t *src, const uint64_t end, zxc_seek_frame_t *f)
Validates the seekable frame that ends at end, in two reads: header and footer as zxc_get_last_frame_...
static zxc_seekable * zxc_seekable_parse(const zxc_seek_source_t *source)
Walks the frames back from the end through zxc_seek_parse_frame, then numbers their bytes and blocks.
static uint64_t zxc_seek_block_of_offset(const zxc_seekable *s, const uint64_t offset)
Archive-wide index of the block holding decompressed offset, below the size.
#define ZXC_SEEK_MAX_FRAMES
Most frames a handle indexes: bounds the reads and memory of an open.
static size_t zxc_seek_spans_raw_max(const uint64_t first, const uint32_t n)
Scratch bound for zxc_seek_load_spans(): the groups blocks [first, first + n) touch.
static int zxc_seek_source_read(const zxc_seek_source_t *src, void *dst, const size_t len, const uint64_t off)
Reads a byte range from the archive, whatever backs it: the one bounds check every parsed offset goes...
static int zxc_seek_read_block(const zxc_seekable *s, const uint64_t off, const uint32_t csz, const int has_cs, uint8_t *buf, const size_t buf_cap, const uint8_t **blk)
Fetches a compressed block from the memory buffer or reader.
static int zxc_seek_use_dict(const zxc_seekable *s, zxc_cctx_t *ctx, const zxc_seek_dict_t **held, const zxc_seek_frame_t *f)
Gives ctx frame f's dictionary, copied in unless *held, the one it holds, is already that one.
Seekable compression and random-access decompression API.
POSIX-shaped threading layer shared by the stream engine (zxc_driver.c) and the seekable reader (zxc_...
static int zxc_num_procs(void)
Returns the number of online logical processors.