60 uint64_t total_decomp;
71#define ZXC_SEEK_MAX_FRAMES (1U << 20)
74#define ZXC_SEEK_TAIL_HDRS (2 * ZXC_BLOCK_HEADER_SIZE)
85struct zxc_seekable_s {
94 void* owned_reader_ctx;
97 zxc_seek_frame_t* frames;
100 uint64_t total_decomp;
101 uint32_t max_block_size;
103 int verify_checksums;
109 int dctx_initialized;
110 const zxc_seek_dict_t* dctx_dict;
115 zxc_seek_dict_t* dicts;
117 size_t max_dict_size;
142 const uint64_t off) {
143 if (UNLIKELY(off > src->size || (uint64_t)len > src->size - off))
146 ZXC_MEMCPY(dst, src->data + off, len);
149 const int64_t r = src->rdr->
read_at(src->rdr->
ctx, dst, len, off);
151 if (UNLIKELY(r != (int64_t)len))
return (r < 0 && r >= INT_MIN) ? (int)r :
ZXC_ERROR_IO;
159 const zxc_seek_source_t src = {s->src, s->src ? NULL : &s->reader, s->src_size};
183 size_t footer_len = 0;
188 const uint64_t frame = end - start;
189 const uint32_t block_size = (uint32_t)fi.
block_size;
195 if (UNLIKELY(seek_block_total + footer_len >
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;
211 f->num_blocks = num_blocks;
214 f->eof_off = eof_off;
215 f->entry_max = entry_max;
216 f->block_size = block_size;
219 f->has_dict = has_dict;
228 zxc_seek_source_t src = *source;
229 zxc_seek_frame_t* frames = NULL;
232 uint64_t end = src.size;
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;
247 const zxc_seek_frame_t*
const f = &frames[0];
252 f->total_decomp, f->block_size, NULL) !=
ZXC_OK))
255 end = frames[n++].base;
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;
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;
274 if (f->block_size > max_block_size) max_block_size = f->block_size;
278 if (UNLIKELY(!s))
goto fail;
279 if (src.rdr) s->reader = *src.rdr;
281 s->src_size = src.size;
284 s->num_blocks = blocks;
285 s->total_decomp = decomp;
286 s->max_block_size = max_block_size;
287 s->verify_checksums = 0;
298 size_t hi = s->num_frames;
300 const size_t mid = lo + (hi - lo) / 2;
301 if (s->frames[mid].block_base <= idx)
306 return &s->frames[lo - 1];
312 size_t hi = s->num_frames;
314 const size_t mid = lo + (hi - lo) / 2;
315 if (s->frames[mid].decomp_base <= offset)
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));
353 const uint64_t first,
const uint32_t n, uint64_t* RESTRICT starts,
354 uint32_t* RESTRICT sizes, uint8_t* RESTRICT raw) {
360 for (uint64_t g = g0; g <= g1; g++)
371 if (rc ==
ZXC_OK && g0 != 0)
373 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
376 f->total_decomp, f->block_size, NULL) !=
ZXC_OK))
380 for (uint64_t g = g0; g <= g1; g++) {
384 : pos < ZXC_FILE_HEADER_SIZE || pos > f->eof_off))
390 if (UNLIKELY(sz < ZXC_BLOCK_HEADER_SIZE || sz > f->entry_max))
393 if (idx >= first && idx < first + n) {
394 starts[idx - first] = f->base + pos;
395 sizes[idx - first] = sz;
400 if (UNLIKELY(pos > f->eof_off || (g == last_group && pos != f->eof_off)))
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};
430 if (UNLIKELY(!r || !r->
read_at || r->
size == 0))
return NULL;
431 const zxc_seek_source_t source = {NULL, r, r->
size};
444 return s ? s->total_decomp : 0;
451 if (UNLIKELY(!s || block_idx >= s->num_blocks))
return 0;
456 if (UNLIKELY(
zxc_seek_load_spans(s, f, block_idx - f->block_base, 1, &start, &size, raw) !=
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;
482 if (UNLIKELY(!s || block_idx >= s->num_blocks))
return 0;
499 const zxc_seek_frame_t* f) {
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];
508 if (d) ZXC_MEMCPY(ctx->
dict_buffer, d->data, d->size);
519static int zxc_seek_decode_error(
const int res) {
546 const int has_cs, uint8_t* buf,
const size_t buf_cap,
547 const uint8_t** blk) {
549 if (UNLIKELY(off > s->src_size || (uint64_t)csz > s->src_size - off))
556 if (UNLIKELY(rc !=
ZXC_OK))
return rc;
562 if (UNLIKELY(hdr_res !=
ZXC_OK))
return hdr_res;
563 const uint64_t on_disk =
579 const uint64_t offset,
const size_t len) {
580 if (UNLIKELY(len == 0))
return 0;
583 if (UNLIKELY(offset > s->total_decomp || len > s->total_decomp - offset))
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))
591 s->dctx_initialized = 1;
599 uint8_t* out = (uint8_t*)dst;
600 size_t remaining = len;
606 uint64_t bi = blk_start;
607 while (bi <= blk_end) {
608 while (bi >= f->block_base + f->num_blocks) f++;
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;
613 uint32_t n = left < to_group_end ? (uint32_t)left : to_group_end;
614 if (in_frame < n) n = (uint32_t)in_frame;
616 if (UNLIKELY(span_res < 0))
return span_res;
621 for (uint32_t k = 0; k < n; k++)
622 if (sizes[k] > need) need = sizes[k];
624 if (s->read_buf_cap < need) {
625 uint8_t*
const nb = (uint8_t*)
ZXC_REALLOC(s->read_buf, (
size_t)need);
628 s->read_buf_cap = (size_t)need;
634 if (UNLIKELY(dict_res !=
ZXC_OK))
return dict_res;
639 for (uint32_t k = 0; k < n; k++, bi++) {
640 const uint64_t lb = local + k;
641 const uint8_t* blk = NULL;
643 s->read_buf, s->read_buf_cap, &blk);
644 if (UNLIKELY(read_res < 0))
return read_res;
648 const uint64_t blk_decomp_start = f->decomp_base + lb * f->block_size;
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;
659 uint8_t*
const aside =
661 uint8_t*
const dec_dst = direct ? out : aside;
665 if (UNLIKELY(dec_res < 0))
return zxc_seek_decode_error(dec_res);
668 if (!direct) ZXC_MEMCPY(out, aside + skip, copy);
696 const zxc_seek_frame_t* frame;
717 zxc_seek_mt_job_t* jobs;
724 pthread_mutex_t lock;
725} zxc_seek_mt_shared_t;
743 const zxc_seek_dict_t** held, uint8_t* read_buf,
const size_t read_cap) {
746 const uint8_t* blk = NULL;
749 if (UNLIKELY(read_res < 0))
return job->result = read_res;
752 if (UNLIKELY(dict_res !=
ZXC_OK))
return job->result = dict_res;
757 uint8_t*
const dec_dst =
762 if (UNLIKELY(dec_res < 0))
return job->result = zxc_seek_decode_error(dec_res);
765 ZXC_MEMCPY(sh->dst_base + job->dst_off, dec_dst + job->skip, job->copy_len);
766 return job->result =
ZXC_OK;
783 zxc_seek_mt_shared_t*
const sh = (zxc_seek_mt_shared_t*)arg;
788 if (UNLIKELY(
zxc_cctx_init(&dctx, (
size_t)s->max_block_size, 0, 0, 0, s->max_dict_size) !=
791 const zxc_seek_dict_t* held = NULL;
794 const size_t read_cap = (size_t)sh->max_csz +
ZXC_PAD_SIZE;
795 uint8_t* read_buf = NULL;
798 if (UNLIKELY(!read_buf)) {
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;
812 pthread_mutex_unlock(&sh->lock);
813 if (first == last)
break;
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);
818 if (UNLIKELY(rc !=
ZXC_OK)) {
819 pthread_mutex_lock(&sh->lock);
821 pthread_mutex_unlock(&sh->lock);
839 const uint64_t offset,
const size_t len,
int n_threads) {
840 if (UNLIKELY(len == 0))
return 0;
843 if (UNLIKELY(offset > s->total_decomp || len > s->total_decomp - offset))
848 const uint64_t jobs_64 = blk_end - blk_start + 1;
855 if (n_threads <= 1 || jobs_64 <= 1 || jobs_64 > UINT32_MAX) {
858 const uint32_t num_jobs = (uint32_t)jobs_64;
863 const uint32_t share = (num_jobs - 1) / (uint32_t)n_threads + 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;
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)) {
888 size_t remaining = len;
889 uint32_t max_csz = 0;
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++;
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;
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;
905 (offset > blk_decomp_start) ? (
size_t)(offset - blk_decomp_start) : 0;
907 const size_t avail = blk_decomp_sz - skip;
908 const size_t copy = (avail < remaining) ? avail : remaining;
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;
915 jobs[i].decomp_sz = blk_decomp_sz;
916 jobs[i].has_cs = f->has_checksums;
917 jobs[i].dst_off = out_off;
919 jobs[i].copy_len = copy;
922 if (sizes[i] > max_csz) max_csz = sizes[i];
930 if (UNLIKELY(plan_res !=
ZXC_OK)) {
936 pthread_t*
const threads = (pthread_t*)
ZXC_MALLOC((
size_t)n_threads *
sizeof(pthread_t));
937 if (UNLIKELY(!threads)) {
944 zxc_seek_mt_shared_t sh;
947 sh.dst_base = (uint8_t*)dst;
948 sh.num_jobs = num_jobs;
950 sh.max_csz = max_csz;
953 pthread_mutex_init(&sh.lock, NULL);
956 for (
int t = 0; t < n_threads; t++) {
964 for (
int t = 0; t < launched; t++) pthread_join(threads[t], NULL);
966 pthread_mutex_destroy(&sh.lock);
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;
989 if (UNLIKELY(!s))
return;
992 for (
size_t i = 0; i < s->num_dicts; i++)
ZXC_FREE(s->dicts[i].data);
1007 s->verify_checksums = enabled ? 1 : 0;
1020 const void* dict_huf) {
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;
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];
1043 zxc_seek_dict_t*
const grown =
1044 (zxc_seek_dict_t*)
ZXC_REALLOC(s->dicts, (s->num_dicts + 1) *
sizeof(*s->dicts));
1047 d = &s->dicts[s->num_dicts];
1048 ZXC_MEMSET(d, 0,
sizeof(*d));
1050 uint8_t*
const copy = (uint8_t*)
ZXC_MALLOC(dict_size);
1052 ZXC_MEMCPY(copy, dict, dict_size);
1053 if (d == &s->dicts[s->num_dicts]) s->num_dicts++;
1056 d->size = dict_size;
1058 d->has_huf = dict_huf != NULL;
1060 if (dict_size > s->max_dict_size) s->max_dict_size = dict_size;
1064 if (s->dctx_initialized) {
1066 s->dctx_initialized = 0;
1081 if (s) s->owned_reader_ctx = ctx;
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_OVERFLOW
Buffer overflow detected during processing.
@ ZXC_ERROR_CORRUPT_DATA
Corrupted compressed data.
@ ZXC_ERROR_NULL_INPUT
Required input pointer is NULL.
@ ZXC_ERROR_IO
Read/write/seek failure on file.
@ ZXC_ERROR_DICT_MISMATCH
Provided dictionary ID does not match the file header.
@ ZXC_ERROR_MEMORY
Memory allocation failure.
@ 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_OK
Success (no error).
@ ZXC_ERROR_DICT_TOO_LARGE
Dictionary exceeds maximum allowed size.
#define ZXC_MALLOC(size)
Heap allocator.
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.
#define ZXC_SEEK_GROUP_BYTES
Full group: anchor + ZXC_SEEK_GROUP sizes.
#define ZXC_FREE(ptr)
Heap deallocator.
#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.
#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...
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.
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.
uint32_t dict_id
Dictionary ID; 0 is none or ID 0, see zxc_requires_dict().
uint8_t has_seek_table
1 when a seek table precedes the footer.
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.
void zxc_cctx_free(zxc_cctx_t *ctx)
Releases all resources owned by a compression context.
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_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_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_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.
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.