ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_driver.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
29
30#include <stddef.h>
31#include <stdint.h>
32#include <stdio.h>
33
39#include "zxc_internal.h"
40#include "zxc_threads.h"
41
42// ============================================================================
43// PLATFORM SHIMS
44// ============================================================================
45// Threading comes from zxc_threads.h (POSIX names everywhere, Win32 underneath);
46// only the file-positioning names are remapped here, next to their users.
47#if defined(_WIN32)
48#include <io.h> /* _get_osfhandle, _fileno */
49#include <malloc.h>
50#include <sys/types.h>
51#include <windows.h>
52
53// Map POSIX file positioning functions to Windows equivalents
54#define fseeko _fseeki64
55#define ftello _ftelli64
56#else
57#include <errno.h>
58#include <sys/stat.h>
59#endif
60
61// Below this block size the per-block system calls cost more than they save.
62#define ZXC_STREAM_PREAD_MIN_BLOCK ((size_t)64 * 1024)
63
64// Jobs per worker when they carry several blocks each.
65#define ZXC_STREAM_RING_BATCHED 2U
66// Jobs per worker when they carry one block each: slack for a slow block.
67#define ZXC_STREAM_RING_UNBATCHED 4U
68
69// ============================================================================
70// STREAMING ENGINE (Producer / Worker / Consumer)
71// ============================================================================
72// Implements a Ring Buffer architecture to parallelize block processing.
73
90
127typedef struct {
128 uint8_t* in_buf;
129 size_t in_cap;
130 size_t in_sz;
131 uint8_t* out_buf;
132 size_t out_cap;
133 size_t result_sz;
134 int fail_code;
135 uint64_t block_index;
136 uint64_t in_off;
137 int job_id;
138 job_status_t status; // Under ctx->lock
139 char pad[ZXC_CACHE_LINE_SIZE]; // Prevent False Sharing
140} zxc_stream_job_t;
141
234typedef struct {
235 zxc_stream_job_t* jobs;
236 size_t ring_size;
237 int* worker_queue;
238 int wq_head;
239 int wq_tail;
240 int wq_count;
241 pthread_mutex_t lock;
242 pthread_cond_t cond_reader;
243 pthread_cond_t cond_worker;
244 pthread_cond_t cond_writer;
245 int shutdown_workers;
246 int compression_mode;
247 int io_error;
248 int workers_lost;
249 int fail_code;
250 int write_idx;
251 int compression_level;
252 size_t chunk_size;
253 int checksum_enabled;
254 int file_has_checksum;
255 int file_has_seek;
256 uint64_t frame_in;
257 zxc_progress_callback_t progress_cb;
258 void* progress_user_data;
259 uint64_t total_input_bytes;
260 const uint8_t* dict;
261 size_t dict_size;
262 const uint8_t* dict_huf;
264 int in_fd;
265 uint64_t in_pos;
266 size_t in_alloc;
267 int batch;
268 uint8_t ahead[ZXC_BLOCK_HEADER_SIZE];
269 int ahead_len;
270 uint64_t src_total;
271 uint64_t digest;
272} zxc_stream_ctx_t;
273
275static ZXC_ALWAYS_INLINE int zxc_ring_next(const zxc_stream_ctx_t* ctx, const int idx) {
276 return (int)(((size_t)idx + 1) % ctx->ring_size);
277}
278
309typedef struct {
310 zxc_stream_ctx_t* ctx;
311 FILE* f;
312 int64_t total_bytes;
313 uint64_t digest; // archive digest (compress side)
314 uint64_t bytes_processed; // For progress callback
315 uint32_t* seek_comp;
316 uint64_t seek_count;
317 uint64_t seek_cap;
318} writer_args_t;
319
324typedef struct {
325 zxc_stream_ctx_t ctx;
326 writer_args_t w;
327 pthread_t* workers;
328 int n_workers;
329 pthread_t writer;
330 uint8_t* mem_block;
331 int read_idx; // Next ring slot the reader fills
332} zxc_stream_engine_t;
333
334#if !defined(_WIN32)
341static int64_t zxc_pread_full(const int fd, void* dst, const size_t len, const uint64_t offset) {
342 if (sizeof(off_t) < sizeof(uint64_t) && (offset > INT32_MAX || len > INT32_MAX - offset))
343 return -1; // LCOV_EXCL_LINE
344 size_t done = 0;
345 while (done < len) {
346 const ssize_t r = pread(fd, (uint8_t*)dst + done, len - done, (off_t)(offset + done));
347 if (r > 0)
348 done += (size_t)r;
349 else if (r == 0)
350 break;
351 else if (errno != EINTR)
352 return -1; // LCOV_EXCL_LINE
353 }
354 return (int64_t)done;
355}
356#endif
357
359static int zxc_stream_regular_fd(FILE* f) {
360#if defined(_WIN32)
361 // LCOV_EXCL_START - Windows path, not reachable on POSIX CI
362 const int fd = _fileno(f);
363 if (fd < 0) return -1;
364 const intptr_t handle = _get_osfhandle(fd);
365 return (handle != -1 && GetFileType((HANDLE)handle) == FILE_TYPE_DISK) ? fd : -1;
366 // LCOV_EXCL_STOP
367#else
368 struct stat st;
369 const int fd = fileno(f);
370 return (fd >= 0 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) ? fd : -1;
371#endif
372}
373
375static void zxc_stream_open_positioned(zxc_stream_ctx_t* ctx, FILE* f_in, const int fd) {
376#if defined(_WIN32)
377 (void)ctx;
378 (void)f_in;
379 (void)fd;
380#else
381 if (sizeof(off_t) < sizeof(uint64_t)) return; // LCOV_EXCL_LINE
382 const long long pos = ftello(f_in);
383 if (UNLIKELY(pos < 0)) return; // LCOV_EXCL_LINE
384 ctx->in_fd = fd;
385 ctx->in_pos = (uint64_t)pos;
386 ctx->ahead_len = -1;
387#endif
388}
389
395static int64_t zxc_stream_read_at(const zxc_stream_ctx_t* ctx, void* dst, const size_t len,
396 const uint64_t offset) {
397#if defined(_WIN32)
398 (void)ctx;
399 (void)dst;
400 (void)len;
401 (void)offset;
402 return ZXC_ERROR_IO;
403#else
404 const int64_t r = zxc_pread_full(ctx->in_fd, dst, len, offset);
405 return r < 0 ? ZXC_ERROR_IO : r;
406#endif
407}
408
419static int64_t zxc_stream_in_read(zxc_stream_ctx_t* ctx, FILE* f, void* dst, const size_t len) {
420 if (ctx && ctx->in_fd >= 0) {
421 const int64_t r = zxc_stream_read_at(ctx, dst, len, ctx->in_pos);
422 if (LIKELY(r > 0)) ctx->in_pos += (uint64_t)r;
423 return r;
424 }
425 const size_t got = fread(dst, 1, len, f);
426 // A short read without EOF is a read error. Not ferror(): the caller may have
427 // left a stale error indicator on the stream.
428 if (UNLIKELY(got < len && !feof(f))) return ZXC_ERROR_IO; // LCOV_EXCL_LINE
429 return (int64_t)got;
430}
431
433static void zxc_stream_reader_fail(zxc_stream_ctx_t* ctx, const int code) {
434 pthread_mutex_lock(&ctx->lock);
435 if (code && !ctx->fail_code) ctx->fail_code = code;
436 ctx->io_error = 1;
437 pthread_mutex_unlock(&ctx->lock);
438}
439
446static ZXC_ALWAYS_INLINE int zxc_stream_process(const zxc_stream_ctx_t* ctx, zxc_cctx_t* cctx,
447 const uint8_t* in, const size_t in_sz, uint8_t* out,
448 const size_t out_cap, const uint64_t block_index) {
449 return ctx->compression_mode
450 ? zxc_compress_chunk_wrapper(cctx, in, in_sz, out, out_cap, block_index)
451 : zxc_decompress_chunk_wrapper(cctx, in, in_sz, out, out_cap, block_index);
452}
453
462static int zxc_stream_decode_job(const zxc_stream_ctx_t* ctx, zxc_cctx_t* cctx,
463 const zxc_stream_job_t* job, const uint8_t* in,
464 uint8_t* dict_work) {
465 const size_t checksum_sz = ctx->file_has_checksum ? ZXC_BLOCK_CHECKSUM_SIZE : 0;
466 // One block of output plus the fast decoder's margin.
467 const size_t room = ctx->chunk_size + ZXC_DECOMPRESS_TAIL_PAD;
468 // Dictionary: decode behind the prefix so back-references resolve.
469 uint8_t* const scratch = dict_work ? dict_work + ctx->dict_size : NULL;
470 uint64_t block_index = job->block_index;
471 size_t ip = 0;
472 size_t op = 0;
473
474 while (ip < job->in_sz) {
475 const size_t rem = job->in_sz - ip;
477 if (UNLIKELY(rem < ZXC_BLOCK_HEADER_SIZE ||
480 const uint64_t block_sz = (uint64_t)ZXC_BLOCK_HEADER_SIZE + bh.comp_size + checksum_sz;
481 if (UNLIKELY(block_sz > rem || job->out_cap - op < room)) return ZXC_ERROR_CORRUPT_DATA;
482
483 const int res =
484 zxc_stream_process(ctx, cctx, in + ip, (size_t)block_sz,
485 scratch ? scratch : job->out_buf + op, room, block_index++);
486 if (UNLIKELY(res < 0)) return res;
487 if (scratch) ZXC_MEMCPY(job->out_buf + op, scratch, (size_t)res);
488 ip += (size_t)block_sz;
489 op += (size_t)res;
490 }
491 return (int)op;
492}
493
509static void* zxc_stream_worker(void* arg) {
510 zxc_stream_ctx_t* const ctx = (zxc_stream_ctx_t*)arg;
511 zxc_cctx_t cctx;
512
513 const int unified_chk = (ctx->compression_mode == 1)
514 ? ctx->checksum_enabled
515 : (ctx->file_has_checksum && ctx->checksum_enabled);
516
517 const size_t eff_chunk = (ctx->dict_size > 0 && ctx->compression_mode == 1)
518 ? zxc_block_size_ceil(ctx->dict_size + ctx->chunk_size)
519 : ctx->chunk_size;
520 // Per-worker input buffer for positioned reads: stays in this core's cache.
521 uint8_t* const in_work = ctx->in_fd >= 0 ? (uint8_t*)ZXC_MALLOC(ctx->in_alloc) : NULL;
522 if (UNLIKELY(zxc_cctx_init(&cctx, eff_chunk, ctx->compression_mode, ctx->compression_level,
523 unified_chk, ctx->dict_size) != ZXC_OK ||
524 zxc_cctx_attach_dict_huf(&cctx, ctx->dict_huf) != ZXC_OK ||
525 (ctx->in_fd >= 0 && !in_work))) {
526 // LCOV_EXCL_START
527 ZXC_FREE(in_work);
528 zxc_cctx_free(&cctx);
529 pthread_mutex_lock(&ctx->lock);
530 ctx->io_error = 1;
531 ctx->workers_lost = 1;
532 pthread_cond_broadcast(&ctx->cond_writer);
533 pthread_cond_broadcast(&ctx->cond_reader);
534 pthread_mutex_unlock(&ctx->lock);
535 return NULL;
536 // LCOV_EXCL_STOP
537 }
538
539 cctx.compression_level = ctx->compression_level;
540
541 // Per-worker dict buffer for assembling [dict | block_data]
542 const size_t dsz = ctx->dict_size;
543 uint8_t* const dict_work = cctx.dict_buffer;
544 if (dict_work) ZXC_MEMCPY(dict_work, ctx->dict, dsz);
545
546 while (1) {
547 zxc_stream_job_t* job = NULL;
548 pthread_mutex_lock(&ctx->lock);
549 while (ctx->wq_count == 0 && !ctx->shutdown_workers) {
550 pthread_cond_wait(&ctx->cond_worker, &ctx->lock);
551 }
552 if (ctx->shutdown_workers && ctx->wq_count == 0) {
553 pthread_mutex_unlock(&ctx->lock);
554 break;
555 }
556 const int jid = ctx->worker_queue[ctx->wq_tail];
557 ctx->wq_tail = zxc_ring_next(ctx, ctx->wq_tail);
558 ctx->wq_count--;
559 job = &ctx->jobs[jid];
560 pthread_mutex_unlock(&ctx->lock);
561
562 // A short read (the file shrank) is an I/O error.
563 const uint8_t* const in = in_work ? in_work : job->in_buf;
564 int res;
565 if (in_work && UNLIKELY(zxc_stream_read_at(ctx, in_work, job->in_sz, job->in_off) !=
566 (int64_t)job->in_sz)) {
567 res = ZXC_ERROR_IO;
568 } else if (ctx->compression_mode == 0) {
569 res = zxc_stream_decode_job(ctx, &cctx, job, in, dict_work);
570 } else if (dict_work) {
571 ZXC_MEMCPY(dict_work + dsz, in, job->in_sz);
572 res = zxc_stream_process(ctx, &cctx, dict_work, dsz + job->in_sz, job->out_buf,
573 job->out_cap, job->block_index);
574 } else {
575 res = zxc_stream_process(ctx, &cctx, in, job->in_sz, job->out_buf, job->out_cap,
576 job->block_index);
577 }
578
579 pthread_mutex_lock(&ctx->lock);
580 job->result_sz = UNLIKELY(res < 0) ? 0 : (size_t)res;
581 job->fail_code = UNLIKELY(res < 0) ? res : 0;
582 job->status = JOB_STATUS_PROCESSED;
583 if (UNLIKELY(res < 0)) {
584 // Keep the codec's own diagnosis; io_error only stops the others.
585 if (!ctx->fail_code) ctx->fail_code = res;
586 ctx->io_error = 1;
587 pthread_cond_broadcast(&ctx->cond_writer);
588 pthread_cond_broadcast(&ctx->cond_reader);
589 } else if (jid == ctx->write_idx) {
590 pthread_cond_signal(&ctx->cond_writer);
591 }
592 pthread_mutex_unlock(&ctx->lock);
593 }
594 ZXC_FREE(in_work);
595 zxc_cctx_free(&cctx);
596 return NULL;
597}
598
628static void* zxc_async_writer(void* arg) {
629 writer_args_t* const args = (writer_args_t*)arg;
630 zxc_stream_ctx_t* const ctx = args->ctx;
631 while (1) {
632 zxc_stream_job_t* const job = &ctx->jobs[ctx->write_idx];
633 pthread_mutex_lock(&ctx->lock);
634 while (job->status != JOB_STATUS_PROCESSED && !ctx->workers_lost &&
635 !(ctx->io_error && job->status == JOB_STATUS_FREE))
636 pthread_cond_wait(&ctx->cond_writer, &ctx->lock);
637 if (UNLIKELY(job->status != JOB_STATUS_PROCESSED)) {
638 pthread_mutex_unlock(&ctx->lock);
639 break;
640 }
641 if (UNLIKELY(job->fail_code)) {
642 // First in stream order, not the first to finish.
643 ctx->fail_code = job->fail_code;
644 job->status = JOB_STATUS_FREE;
645 pthread_cond_signal(&ctx->cond_reader);
646 pthread_mutex_unlock(&ctx->lock);
647 break;
648 }
649
650 const size_t result_sz = job->result_sz;
651 const size_t in_sz = job->in_sz;
652 pthread_mutex_unlock(&ctx->lock);
653
654 if (result_sz == (size_t)-1) break;
655
656 if (args->f && result_sz > 0) {
657 if (fwrite(job->out_buf, 1, result_sz, args->f) != result_sz) {
658 pthread_mutex_lock(&ctx->lock);
659 ctx->fail_code = ZXC_ERROR_IO; // Before any job still to be written
660 ctx->io_error = 1;
661 job->status = JOB_STATUS_FREE;
662 pthread_cond_signal(&ctx->cond_reader);
663 pthread_mutex_unlock(&ctx->lock);
664 break;
665 }
666 }
667 args->total_bytes += (int64_t)result_sz;
668 if (ctx->checksum_enabled && ctx->compression_mode == 1 &&
669 LIKELY(result_sz >= ZXC_BLOCK_CHECKSUM_SIZE))
670 args->digest = zxc_digest_combine(
671 args->digest, zxc_le32(job->out_buf + result_sz - ZXC_BLOCK_CHECKSUM_SIZE));
672
673 // Seekable: record compressed block size
674 if (args->seek_comp && ctx->compression_mode == 1) {
675 if (UNLIKELY(args->seek_count >= args->seek_cap)) {
676 const uint64_t max_cap = SIZE_MAX / sizeof(uint32_t);
677 uint32_t* nc = NULL;
678 if (LIKELY(args->seek_cap < max_cap)) {
679 args->seek_cap = args->seek_cap < max_cap / 2 ? args->seek_cap * 2 : max_cap;
680 nc = (uint32_t*)ZXC_REALLOC(args->seek_comp,
681 (size_t)args->seek_cap * sizeof(uint32_t));
682 }
683 // LCOV_EXCL_START
684 if (UNLIKELY(!nc)) {
685 pthread_mutex_lock(&ctx->lock);
686 if (!ctx->fail_code) ctx->fail_code = ZXC_ERROR_MEMORY;
687 ctx->io_error = 1;
688 job->status = JOB_STATUS_FREE;
689 pthread_cond_signal(&ctx->cond_reader);
690 pthread_mutex_unlock(&ctx->lock);
691 break;
692 }
693 // LCOV_EXCL_STOP
694 args->seek_comp = nc;
695 }
696 args->seek_comp[args->seek_count++] = (uint32_t)result_sz;
697 }
698
699 if (ctx->progress_cb) {
700 // LCOV_EXCL_START
701 args->bytes_processed += ctx->compression_mode == 1 ? in_sz : result_sz;
702 ctx->progress_cb(args->bytes_processed, ctx->total_input_bytes,
703 ctx->progress_user_data);
704 // LCOV_EXCL_STOP
705 }
706
707 pthread_mutex_lock(&ctx->lock);
708 job->status = JOB_STATUS_FREE;
709 ctx->write_idx = zxc_ring_next(ctx, ctx->write_idx);
710 pthread_cond_signal(&ctx->cond_reader);
711 pthread_mutex_unlock(&ctx->lock);
712 }
713 return NULL;
714}
715
723static void zxc_stream_engine_stop(zxc_stream_engine_t* e, const int writer_started) {
724 zxc_stream_ctx_t* const ctx = &e->ctx;
725 if (writer_started) {
726 zxc_stream_job_t* const end_job = &ctx->jobs[e->read_idx];
727 pthread_mutex_lock(&ctx->lock);
728 while (end_job->status != JOB_STATUS_FREE && !ctx->io_error)
729 pthread_cond_wait(&ctx->cond_reader, &ctx->lock);
730 if (end_job->status == JOB_STATUS_FREE) {
731 end_job->result_sz = (size_t)-1;
732 end_job->fail_code = 0;
733 end_job->status = JOB_STATUS_PROCESSED;
734 }
735 pthread_cond_broadcast(&ctx->cond_writer);
736 pthread_mutex_unlock(&ctx->lock);
737 pthread_join(e->writer, NULL);
738 }
739 pthread_mutex_lock(&ctx->lock);
740 ctx->shutdown_workers = 1;
741 pthread_cond_broadcast(&ctx->cond_worker);
742 pthread_mutex_unlock(&ctx->lock);
743 for (int i = 0; i < e->n_workers; i++) pthread_join(e->workers[i], NULL);
744
745 pthread_cond_destroy(&ctx->cond_writer);
746 pthread_cond_destroy(&ctx->cond_worker);
747 pthread_cond_destroy(&ctx->cond_reader);
748 pthread_mutex_destroy(&ctx->lock);
749 ZXC_FREE(e->workers);
750 ZXC_ALIGNED_FREE(e->mem_block);
751}
752
784static int zxc_stream_engine_start(zxc_stream_engine_t* e, FILE* f_in, const int n_threads) {
785 zxc_stream_ctx_t* const ctx = &e->ctx;
786 const int mode = ctx->compression_mode;
787 const size_t chunk = ctx->chunk_size;
788
789 const int explicit_threads = n_threads > 0;
790 int num_threads = explicit_threads ? n_threads : zxc_num_procs();
791 if (num_threads > ZXC_MAX_THREADS) num_threads = ZXC_MAX_THREADS;
792 // Positioned reads need two threads: a lone worker is faster fed by the reader.
793 ctx->in_fd = -1;
794 const int in_fd = (mode == 0) ? zxc_stream_regular_fd(f_in) : -1;
795 ctx->batch = (in_fd >= 0 && chunk < ZXC_MT_BATCH_BYTES) ? (int)(ZXC_MT_BATCH_BYTES / chunk) : 1;
796 if (in_fd >= 0 && num_threads > 1 && chunk >= ZXC_STREAM_PREAD_MIN_BLOCK)
797 zxc_stream_open_positioned(ctx, f_in, in_fd);
798 // -T N is N workers; auto-detection leaves one processor to the reader and
799 // writer unless the workers read themselves.
800 const int num_workers = (explicit_threads || ctx->in_fd >= 0) ? num_threads
801 : (num_threads > 1) ? num_threads - 1
802 : 1;
803 // Batched jobs hold several blocks: fewer keep the workers fed.
804 ctx->ring_size = (size_t)num_workers *
805 (ctx->batch > 1 ? ZXC_STREAM_RING_BATCHED : ZXC_STREAM_RING_UNBATCHED);
806
807 const uint64_t max_out = zxc_compress_bound(chunk);
808 const size_t raw_alloc_in =
809 (size_t)((mode ? chunk : max_out * (uint64_t)ctx->batch) + ZXC_PAD_SIZE);
810 const size_t alloc_in = (raw_alloc_in + ZXC_ALIGNMENT_MASK) & ~ZXC_ALIGNMENT_MASK;
811 ctx->in_alloc = alloc_in;
812
813 const size_t raw_alloc_out =
814 (size_t)((mode ? max_out : chunk * (size_t)ctx->batch + ZXC_DECOMPRESS_TAIL_PAD) +
816 const size_t alloc_out = (raw_alloc_out + ZXC_ALIGNMENT_MASK) & ~ZXC_ALIGNMENT_MASK;
817
818 // Positioned reads use per-worker input buffers.
819 const size_t ring_in = ctx->in_fd >= 0 ? 0 : alloc_in;
820 const size_t per_job_sz = sizeof(zxc_stream_job_t) + sizeof(int) + ring_in + alloc_out;
821 if (UNLIKELY(per_job_sz > SIZE_MAX / ctx->ring_size))
822 return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
823
824 uint8_t* const mem_block = ZXC_ALIGNED_MALLOC(ctx->ring_size * per_job_sz, ZXC_CACHE_LINE_SIZE);
825 if (UNLIKELY(!mem_block)) return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
826 e->mem_block = mem_block;
827
828 uint8_t* ptr = mem_block;
829 ctx->jobs = (zxc_stream_job_t*)ptr;
830 ptr += ctx->ring_size * sizeof(zxc_stream_job_t);
831 ctx->worker_queue = (int*)ptr;
832 ptr += ctx->ring_size * sizeof(int);
833 uint8_t* buf_in = ptr;
834 ptr += ctx->ring_size * ring_in;
835 uint8_t* buf_out = ptr;
836
837 ZXC_MEMSET(mem_block, 0, ctx->ring_size * (sizeof(zxc_stream_job_t) + sizeof(int)));
838
839 for (size_t i = 0; i < ctx->ring_size; i++) {
840 ctx->jobs[i].job_id = (int)i;
841 ctx->jobs[i].status = JOB_STATUS_FREE;
842 ctx->jobs[i].in_buf = ring_in ? buf_in + (i * ring_in) : NULL;
843 ctx->jobs[i].in_cap = alloc_in - ZXC_PAD_SIZE;
844 ctx->jobs[i].in_sz = 0;
845 ctx->jobs[i].out_buf = buf_out + (i * alloc_out);
846 ctx->jobs[i].out_cap = alloc_out - ZXC_PAD_SIZE;
847 ctx->jobs[i].result_sz = 0;
848 }
849
850 pthread_mutex_init(&ctx->lock, NULL);
851 pthread_cond_init(&ctx->cond_reader, NULL);
852 pthread_cond_init(&ctx->cond_worker, NULL);
853 pthread_cond_init(&ctx->cond_writer, NULL);
854
855 e->workers = ZXC_MALLOC((size_t)num_workers * sizeof(pthread_t));
856 if (LIKELY(e->workers)) {
857 while (e->n_workers < num_workers &&
858 pthread_create(&e->workers[e->n_workers], NULL, zxc_stream_worker, ctx) == 0)
859 e->n_workers++;
860 }
861 e->w.ctx = ctx;
862 if (UNLIKELY(e->n_workers == 0 ||
863 pthread_create(&e->writer, NULL, zxc_async_writer, &e->w) != 0)) {
864 zxc_stream_engine_stop(e, 0); // LCOV_EXCL_LINE
865 return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
866 }
867 return ZXC_OK;
868}
869
871static int zxc_stream_engine_close(zxc_stream_engine_t* e, FILE* f_in) {
873 if (e->ctx.in_fd < 0) return ZXC_OK;
874 e->ctx.in_fd = -1;
875 return fseeko(f_in, (long long)e->ctx.in_pos, SEEK_SET) == 0 ? ZXC_OK : ZXC_ERROR_IO;
876}
877
879static int zxc_stream_engine_drain(zxc_stream_engine_t* e) {
880 zxc_stream_ctx_t* const ctx = &e->ctx;
881 // The writer frees jobs in order: the last one queued is free last.
882 const zxc_stream_job_t* const last =
883 &ctx->jobs[((size_t)e->read_idx + ctx->ring_size - 1) % ctx->ring_size];
884 pthread_mutex_lock(&ctx->lock);
885 while (last->status != JOB_STATUS_FREE && !ctx->io_error)
886 pthread_cond_wait(&ctx->cond_reader, &ctx->lock);
887 const int failed = ctx->io_error;
888 pthread_mutex_unlock(&ctx->lock);
889 return failed;
890}
891
899static size_t zxc_stream_read_blocks(zxc_stream_ctx_t* ctx, FILE* f_in, zxc_stream_job_t* job,
900 int* n_blocks, int* eof) {
901 const int positioned = ctx->in_fd >= 0;
902 const size_t checksum_sz = ctx->file_has_checksum ? ZXC_BLOCK_CHECKSUM_SIZE : 0;
903 // Folded only when finish_decompress will compare it: like the block
904 // checksums, the file flag and the caller's switch both.
905 const int fold = ctx->file_has_checksum && ctx->checksum_enabled;
906 size_t fill = 0;
907 int code = 0; // 0: a read error
908
909 for (*n_blocks = 0; *n_blocks < ctx->batch; (*n_blocks)++) {
910 uint8_t bh_buf[ZXC_BLOCK_HEADER_SIZE];
911 const uint64_t block_off = ctx->in_pos;
912 int64_t h_read;
913 if (positioned && ctx->ahead_len >= 0) {
914 h_read = ctx->ahead_len;
915 ZXC_MEMCPY(bh_buf, ctx->ahead, (size_t)h_read);
916 ctx->ahead_len = -1;
917 } else if (positioned) {
918 h_read = zxc_stream_read_at(ctx, bh_buf, sizeof(bh_buf), ctx->in_pos);
919 } else {
920 h_read = zxc_stream_in_read(NULL, f_in, bh_buf, sizeof(bh_buf));
921 }
922 if (UNLIKELY(h_read < 0)) goto _failed; // LCOV_EXCL_LINE
923 if (positioned) ctx->in_pos += (uint64_t)h_read;
924 if (UNLIKELY(h_read < ZXC_BLOCK_HEADER_SIZE)) {
925 *eof = 1;
926 return fill;
927 }
928
930 if (UNLIKELY(zxc_read_block_header(bh_buf, ZXC_BLOCK_HEADER_SIZE, &bh) != ZXC_OK)) {
931 // Bad checksum: corrupt data. Good checksum, Flags/Reserved set: bad header.
932 uint8_t sum[ZXC_BLOCK_HEADER_SIZE];
933 ZXC_MEMCPY(sum, bh_buf, ZXC_BLOCK_HEADER_SIZE);
934 sum[7] = 0;
935 code = bh_buf[7] == zxc_hash8(sum) ? ZXC_ERROR_BAD_HEADER : ZXC_ERROR_CORRUPT_DATA;
936 goto _failed;
937 }
938
939 if (bh.block_type == ZXC_BLOCK_EOF) {
940 if (UNLIKELY(zxc_check_eof_header(bh_buf) != ZXC_OK)) {
942 goto _failed;
943 }
944 ctx->frame_in += ZXC_BLOCK_HEADER_SIZE;
945 *eof = 1;
946 return fill;
947 }
948
949 if (UNLIKELY((uint64_t)bh.comp_size > (uint64_t)ctx->chunk_size)) {
950 code = ZXC_ERROR_BAD_BLOCK_SIZE; // LCOV_EXCL_LINE
951 goto _failed; // LCOV_EXCL_LINE
952 }
953
954 const size_t body_total = (size_t)bh.comp_size + checksum_sz;
955 const uint64_t total_len = (uint64_t)body_total + ZXC_BLOCK_HEADER_SIZE;
956 if (UNLIKELY(total_len > (uint64_t)(job->in_cap - fill))) goto _failed;
957
959 const uint8_t* stored = tail;
960 int got = 1;
961
962 if (positioned) {
963 // The worker reads the bodies; the digest needs only the checksum,
964 // read with the next header.
965 if (fill == 0) job->in_off = block_off;
966 ctx->in_pos += body_total;
967 if (fold) {
968 const int64_t r = zxc_stream_read_at(ctx, tail, sizeof(tail),
969 ctx->in_pos - ZXC_BLOCK_CHECKSUM_SIZE);
970 got = r >= ZXC_BLOCK_CHECKSUM_SIZE;
971 if (LIKELY(got)) {
972 ctx->ahead_len = (int)(r - ZXC_BLOCK_CHECKSUM_SIZE);
973 ZXC_MEMCPY(ctx->ahead, tail + ZXC_BLOCK_CHECKSUM_SIZE, (size_t)ctx->ahead_len);
974 }
975 }
976 } else {
977 uint8_t* const block = job->in_buf + fill;
978 ZXC_MEMCPY(block, bh_buf, ZXC_BLOCK_HEADER_SIZE);
979 stored = block + ZXC_BLOCK_HEADER_SIZE + bh.comp_size;
980
981 // Single fread for body + checksum (reduces syscalls)
982 got = fread(block + ZXC_BLOCK_HEADER_SIZE, 1, body_total, f_in) == body_total;
983 }
984
985 if (UNLIKELY(!got)) goto _failed;
986 if (fold) ctx->digest = zxc_digest_combine(ctx->digest, zxc_le32(stored));
987 fill += (size_t)total_len;
988 ctx->frame_in += total_len;
989 }
990 return fill;
991
992_failed:
993 zxc_stream_reader_fail(ctx, code);
994 *eof = 1;
995 return 0;
996}
997
999static size_t zxc_stream_read_chunk(zxc_stream_ctx_t* ctx, FILE* f_in, zxc_stream_job_t* job,
1000 int* eof) {
1001 const size_t got = fread(job->in_buf, 1, ctx->chunk_size, f_in);
1002 ctx->src_total += got;
1003 if (got < ctx->chunk_size) {
1004 *eof = 1;
1005 if (UNLIKELY(!feof(f_in))) {
1007 return 0;
1008 }
1009 }
1010 return got;
1011}
1012
1018static int zxc_stream_read_loop(zxc_stream_ctx_t* ctx, FILE* f_in, int read_idx) {
1019 int read_eof = 0;
1020 uint64_t block_index = 0;
1021
1022 while (!read_eof) {
1023 zxc_stream_job_t* const job = &ctx->jobs[read_idx];
1024 pthread_mutex_lock(&ctx->lock);
1025 while (job->status != JOB_STATUS_FREE && !ctx->io_error)
1026 pthread_cond_wait(&ctx->cond_reader, &ctx->lock);
1027 const int failed = ctx->io_error;
1028 pthread_mutex_unlock(&ctx->lock);
1029
1030 if (UNLIKELY(failed)) break;
1031
1032 int n_blocks = 1;
1033 const size_t read_sz = ctx->compression_mode
1034 ? zxc_stream_read_chunk(ctx, f_in, job, &read_eof)
1035 : zxc_stream_read_blocks(ctx, f_in, job, &n_blocks, &read_eof);
1036 if (UNLIKELY(read_eof && read_sz == 0)) break;
1037
1038 job->in_sz = read_sz;
1039 job->block_index = block_index;
1040 block_index += (uint64_t)n_blocks;
1041 pthread_mutex_lock(&ctx->lock);
1042 job->status = JOB_STATUS_FILLED;
1043 ctx->worker_queue[ctx->wq_head] = read_idx;
1044 ctx->wq_head = zxc_ring_next(ctx, ctx->wq_head);
1045 ctx->wq_count++;
1046 read_idx = zxc_ring_next(ctx, read_idx);
1047 pthread_cond_signal(&ctx->cond_worker);
1048 pthread_mutex_unlock(&ctx->lock);
1049 }
1050 return read_idx;
1051}
1052
1058static void zxc_stream_finish_compress(zxc_stream_ctx_t* ctx, writer_args_t* w, FILE* f_out) {
1059 // EOF block
1060 uint8_t eof_buf[ZXC_BLOCK_HEADER_SIZE];
1061 const zxc_block_header_t eof_bh = {
1062 .block_type = ZXC_BLOCK_EOF, .block_flags = 0, .reserved = 0, .comp_size = 0};
1064 if (UNLIKELY(f_out &&
1065 fwrite(eof_buf, 1, ZXC_BLOCK_HEADER_SIZE, f_out) != ZXC_BLOCK_HEADER_SIZE))
1066 ctx->io_error = 1; // LCOV_EXCL_LINE
1067 else
1068 w->total_bytes += ZXC_BLOCK_HEADER_SIZE;
1069
1070 // Seekable: write SEK block between EOF and footer, empty when no block was read
1071 if (!ctx->io_error && w->seek_comp) {
1072 // Header, then one group at a time (seek_comp itself stays resident).
1073 uint8_t st_buf[ZXC_SEEK_GROUP_BYTES];
1074 const int h = zxc_seek_table_header(st_buf, sizeof(st_buf), w->seek_count);
1075 if (UNLIKELY(h < 0 || (f_out && fwrite(st_buf, 1, (size_t)h, f_out) != (size_t)h)))
1076 ctx->io_error = 1; // LCOV_EXCL_LINE
1077 else
1078 w->total_bytes += h;
1079
1080 uint64_t anchor = ZXC_FILE_HEADER_SIZE;
1081 for (uint64_t i = 0; i < w->seek_count && !ctx->io_error; i += ZXC_SEEK_GROUP) {
1082 const size_t bytes =
1083 zxc_seek_write_group(st_buf, &anchor, w->seek_comp + i,
1084 zxc_seek_group_len(w->seek_count, i / ZXC_SEEK_GROUP));
1085 if (UNLIKELY(f_out && fwrite(st_buf, 1, bytes, f_out) != bytes))
1086 ctx->io_error = 1; // LCOV_EXCL_LINE
1087 else
1088 w->total_bytes += (int64_t)bytes;
1089 }
1090 }
1091
1092 // Footer (FORMAT.md 8)
1093 uint8_t footer_buf[ZXC_FOOTER_MAX_SIZE_WITH_DIGEST];
1094 const size_t footer_len =
1095 (size_t)zxc_write_file_footer(footer_buf, sizeof(footer_buf), (uint64_t)w->total_bytes,
1096 ctx->src_total, w->digest, ctx->checksum_enabled);
1097 if (UNLIKELY(f_out && fwrite(footer_buf, 1, footer_len, f_out) != footer_len))
1098 ctx->io_error = 1;
1099 else
1100 w->total_bytes += (int64_t)footer_len;
1101}
1102
1106static void zxc_stream_finish_decompress(zxc_stream_ctx_t* ctx, const uint64_t frame_out,
1107 FILE* f_in) {
1108 // After the EOF block: the SEK block when the header announced one, then the
1109 // footer, whose length follows from the bytes read and the bytes produced.
1110 int failed = 0;
1111 int code = 0; // 0: an I/O error
1112
1113 if (ctx->file_has_seek) {
1114 uint8_t sek[ZXC_BLOCK_HEADER_SIZE];
1115 uint64_t remaining = 0;
1116 uint64_t sek_bytes = 0;
1117 // A short read is a truncation, unless the bytes read already disagree.
1118 const int64_t got = zxc_stream_in_read(ctx, f_in, sek, sizeof(sek));
1119 const int src_rc = got < 0 ? ZXC_ERROR_IO
1120 : zxc_check_seek_header(sek, (size_t)got, frame_out,
1121 ctx->chunk_size, &remaining);
1122 if (UNLIKELY(src_rc != ZXC_OK)) {
1123 failed = 1;
1124 if (src_rc == ZXC_ERROR_CORRUPT_DATA) code = src_rc;
1125 }
1126 sek_bytes = remaining;
1127 // Skip the SEK payload; positioned, a cut table shows at the footer read.
1128 if (ctx->in_fd >= 0) {
1129 ctx->in_pos += remaining;
1130 remaining = 0;
1131 }
1132 uint8_t discard[512]; // scratch, any size
1133 while (remaining > 0 && !failed) {
1134 const size_t chunk = remaining < sizeof(discard) ? (size_t)remaining : sizeof(discard);
1135 if (UNLIKELY(fread(discard, 1, chunk, f_in) != chunk)) failed = 1;
1136 remaining -= chunk;
1137 }
1138 ctx->frame_in += ZXC_BLOCK_HEADER_SIZE + sek_bytes;
1139 }
1140 const zxc_footer_layout_t fl =
1141 zxc_footer_layout(ctx->frame_in, frame_out, ctx->file_has_checksum);
1142 uint64_t stored_digest = 0;
1143 if (!failed) {
1144 uint8_t footer[ZXC_FOOTER_MAX_SIZE_WITH_DIGEST];
1145 const int64_t got = zxc_stream_in_read(ctx, f_in, footer, fl.len);
1146 const int frc =
1147 got < 0 ? ZXC_ERROR_IO
1148 : zxc_check_file_footer(footer, (size_t)got, &fl, frame_out, &stored_digest);
1149 if (UNLIKELY(frc != ZXC_OK)) {
1150 failed = 1;
1151 if (frc == ZXC_ERROR_CORRUPT_DATA) code = frc;
1152 }
1153 }
1154 if (!failed && ctx->file_has_checksum && ctx->checksum_enabled &&
1155 UNLIKELY(stored_digest != ctx->digest)) {
1156 failed = 1;
1158 }
1159 if (UNLIKELY(failed)) zxc_stream_reader_fail(ctx, code);
1160 // Whatever follows is the next frame's, or an error: see zxc_stream_decompress().
1161}
1162
1175static int zxc_stream_read_exact(zxc_stream_ctx_t* ctx, FILE* f, void* dst, const size_t len,
1176 const int short_code) {
1177 const int64_t got = zxc_stream_in_read(ctx, f, dst, len);
1178 if (LIKELY(got == (int64_t)len)) return ZXC_OK;
1179 if (UNLIKELY(got < 0)) return ZXC_ERROR_IO; // LCOV_EXCL_LINE
1180 return got == 0 ? 1 : short_code;
1181}
1182
1197static int zxc_stream_read_frame_header(zxc_stream_ctx_t* ctx, FILE* f_in, const uint8_t* magic,
1198 const int has_dict, const uint32_t dict_id, size_t* chunk,
1199 int* has_checksum, int* has_seek, int* frame_dict) {
1200 uint8_t h[ZXC_FILE_HEADER_SIZE];
1201 int header_has_dict = 0;
1202 uint32_t header_dict_id = 0;
1203 ZXC_MEMCPY(h, magic, sizeof(uint32_t));
1204 const int rrc =
1205 zxc_stream_read_exact(ctx, f_in, h + sizeof(uint32_t),
1206 ZXC_FILE_HEADER_SIZE - sizeof(uint32_t), ZXC_ERROR_SRC_TOO_SMALL);
1207 if (UNLIKELY(rrc != ZXC_OK)) return rrc == 1 ? ZXC_ERROR_SRC_TOO_SMALL : rrc;
1208
1209 const int hrc = zxc_read_file_header(h, ZXC_FILE_HEADER_SIZE, chunk, has_checksum,
1210 &header_has_dict, &header_dict_id, has_seek);
1211 if (UNLIKELY(hrc != ZXC_OK)) return hrc;
1212 *frame_dict = header_has_dict;
1213 if (header_has_dict) {
1214#ifdef ZXC_NO_DICT
1216#endif
1217 if (UNLIKELY(!has_dict)) return ZXC_ERROR_DICT_REQUIRED;
1218 if (UNLIKELY(dict_id != header_dict_id)) return ZXC_ERROR_DICT_MISMATCH;
1219 }
1220 return ZXC_OK;
1221}
1222
1228static int64_t zxc_stream_decode_frame(zxc_stream_engine_t* e, FILE* f_in, const int has_seek) {
1229 zxc_stream_ctx_t* const ctx = &e->ctx;
1230 const int64_t base = e->w.total_bytes;
1231 ctx->file_has_seek = has_seek;
1232 ctx->frame_in = ZXC_FILE_HEADER_SIZE;
1233 ctx->digest = 0;
1234 ctx->ahead_len = -1;
1235
1236 e->read_idx = zxc_stream_read_loop(ctx, f_in, e->read_idx);
1238 zxc_stream_finish_decompress(ctx, (uint64_t)(e->w.total_bytes - base), f_in);
1239
1240 // fail_code holds the cause, else a read/write failed; a worker may still set it.
1241 pthread_mutex_lock(&ctx->lock);
1242 const int failed = ctx->io_error;
1243 const int code = ctx->fail_code;
1244 pthread_mutex_unlock(&ctx->lock);
1245 if (UNLIKELY(failed)) return code ? code : ZXC_ERROR_IO;
1246 return e->w.total_bytes - base;
1247}
1248
1250static int zxc_file_span(FILE* f, long long* saved, uint64_t* size) {
1251 *saved = ftello(f);
1252 if (UNLIKELY(*saved < 0 || fseeko(f, 0, SEEK_END) != 0)) return ZXC_ERROR_IO;
1253 const long long n = ftello(f);
1254 if (UNLIKELY(n < 0)) {
1255 // LCOV_EXCL_START
1256 fseeko(f, *saved, SEEK_SET);
1257 return ZXC_ERROR_IO;
1258 // LCOV_EXCL_STOP
1259 }
1260 *size = (uint64_t)n;
1261 return ZXC_OK;
1262}
1263
1271int64_t zxc_stream_compress(FILE* f_in, FILE* f_out, const zxc_cctx* cfg) {
1272 if (UNLIKELY(!f_in)) return ZXC_ERROR_NULL_INPUT;
1273 const zxc_cparams_t* const opts = cfg ? zxc_cctx_params(cfg) : NULL;
1274
1275 const int n_threads = opts ? opts->n_threads : 0;
1276 const int checksum_enabled = opts ? opts->checksum_enabled : 0;
1277 const int seekable = opts ? opts->seekable : 0;
1278 const int level = ZXC_OPTS_LEVEL(opts, ZXC_LEVEL_DEFAULT);
1279 const size_t block_size = ZXC_OPTS_BLOCK_SIZE(opts, ZXC_BLOCK_SIZE_DEFAULT);
1280 const uint8_t* dict = opts ? (const uint8_t*)opts->dict : NULL;
1281 const size_t dict_size = ZXC_OPTS_DICT_SIZE(opts);
1282 zxc_progress_callback_t cb = opts ? opts->progress_cb : NULL;
1283 void* ud = opts ? opts->user_data : NULL;
1284
1285 const uint8_t* dict_huf = ZXC_OPTS_DICT_HUF(opts);
1286 zxc_stream_engine_t e;
1287 ZXC_MEMSET(&e, 0, sizeof(e));
1288 zxc_stream_ctx_t* const ctx = &e.ctx;
1289 ctx->compression_mode = 1;
1290 ctx->compression_level = level;
1291 ctx->chunk_size = block_size;
1292 ctx->checksum_enabled = checksum_enabled;
1293 ctx->file_has_checksum = checksum_enabled;
1294 ctx->progress_cb = cb;
1295 ctx->progress_user_data = ud;
1296 ctx->dict = dict;
1297 ctx->dict_size = dict_size;
1298 ctx->dict_huf = dict_huf;
1299
1300 // Input size for progress tracking; for decompression the CLI passes it.
1301 long long saved_pos = 0;
1302 uint64_t in_size = 0;
1303 if (cb && zxc_file_span(f_in, &saved_pos, &in_size) == ZXC_OK) {
1304 ctx->total_input_bytes = in_size; // LCOV_EXCL_LINE
1305 fseeko(f_in, saved_pos, SEEK_SET); // LCOV_EXCL_LINE
1306 }
1307
1308 // Seekable: initial block-size tracking array
1309 if (seekable) {
1310 e.w.seek_cap = 64; // doubled as blocks come
1311 e.w.seek_comp = (uint32_t*)ZXC_MALLOC(e.w.seek_cap * sizeof(uint32_t));
1312 if (UNLIKELY(!e.w.seek_comp)) return ZXC_ERROR_MEMORY; // LCOV_EXCL_LINE
1313 }
1314 e.w.f = f_out;
1315 const int src = zxc_stream_engine_start(&e, f_in, n_threads);
1316 if (UNLIKELY(src != ZXC_OK)) {
1317 ZXC_FREE(e.w.seek_comp); // LCOV_EXCL_LINE
1318 return src; // LCOV_EXCL_LINE
1319 }
1320
1321 // The writer waits for a job: the header goes first.
1322 if (f_out) {
1323 uint8_t h[ZXC_FILE_HEADER_SIZE];
1324 zxc_write_file_header(h, ZXC_FILE_HEADER_SIZE, block_size, checksum_enabled,
1325 dict && dict_size > 0, zxc_dict_compute_id(dict, dict_size, dict_huf),
1326 seekable);
1327 if (UNLIKELY(fwrite(h, 1, ZXC_FILE_HEADER_SIZE, f_out) != ZXC_FILE_HEADER_SIZE))
1328 zxc_stream_reader_fail(ctx, 0);
1329 }
1330 e.w.total_bytes = ZXC_FILE_HEADER_SIZE;
1331
1332 e.read_idx = zxc_stream_read_loop(ctx, f_in, 0);
1334
1335 if (!ctx->io_error && e.w.total_bytes >= 0) zxc_stream_finish_compress(ctx, &e.w, f_out);
1336 // stdio defers write errors to flush time: push the last buffer to the OS
1337 // so a failure (ENOSPC, EFBIG, EPIPE) is reported here instead of being
1338 // left to the caller's fclose, after a success count was returned.
1339 if (UNLIKELY(!ctx->io_error && f_out && fflush(f_out) != 0)) ctx->io_error = 1;
1340 ZXC_FREE(e.w.seek_comp);
1341
1342 if (UNLIKELY(ctx->io_error)) return ctx->fail_code ? ctx->fail_code : ZXC_ERROR_IO;
1343 return e.w.total_bytes;
1344}
1345
1354int64_t zxc_stream_decompress(FILE* f_in, FILE* f_out, const zxc_dctx* cfg) {
1355 if (UNLIKELY(!f_in)) return ZXC_ERROR_NULL_INPUT;
1356 const zxc_dparams_t* const opts = cfg ? zxc_dctx_params(cfg) : NULL;
1357
1358 const int n_threads = opts ? opts->n_threads : 0;
1359 const int checksum_enabled = opts ? opts->checksum_enabled : 0;
1360 const uint8_t* dict = opts ? (const uint8_t*)opts->dict : NULL;
1361 const size_t dict_size = ZXC_OPTS_DICT_SIZE(opts);
1362 const uint8_t* dict_huf = ZXC_OPTS_DICT_HUF(opts);
1363 const uint32_t dict_id = zxc_dict_compute_id(dict, dict_size, dict_huf);
1364
1365 zxc_stream_engine_t e;
1366 int running = 0;
1367 uint8_t magic[sizeof(uint32_t)];
1368 int rc = zxc_stream_read_exact(NULL, f_in, magic, sizeof(magic), ZXC_ERROR_SRC_TOO_SMALL);
1369
1370 int64_t total = 0;
1371 for (int frames = 0;; frames++) {
1372 if (rc == 1) {
1373 // A clean end of input, after at least one frame.
1374 rc = frames ? ZXC_OK : ZXC_ERROR_SRC_TOO_SMALL;
1375 break;
1376 }
1377 if (UNLIKELY(rc != ZXC_OK)) break;
1378 if (UNLIKELY(zxc_le32(magic) != ZXC_MAGIC_WORD)) {
1380 break;
1381 }
1382 size_t chunk = 0;
1383 int has_checksum = 0;
1384 int has_seek = 0;
1385 int frame_dict = 0;
1386 rc = zxc_stream_read_frame_header(running ? &e.ctx : NULL, f_in, magic, dict_size > 0,
1387 dict_id, &chunk, &has_checksum, &has_seek, &frame_dict);
1388 if (UNLIKELY(rc != ZXC_OK)) break;
1389 // A frame without the flag never sees the dictionary nor its literal table.
1390 const size_t frame_dict_size = frame_dict ? dict_size : 0;
1391
1392 // The workers are set up for one block size, checksum flag and dictionary.
1393 if (running && (e.ctx.chunk_size != chunk || e.ctx.file_has_checksum != has_checksum ||
1394 e.ctx.dict_size != frame_dict_size)) {
1395 running = 0;
1396 rc = zxc_stream_engine_close(&e, f_in);
1397 if (UNLIKELY(rc != ZXC_OK)) break; // LCOV_EXCL_LINE
1398 }
1399 if (!running) {
1400 ZXC_MEMSET(&e, 0, sizeof(e));
1401 e.ctx.chunk_size = chunk;
1402 e.ctx.checksum_enabled = checksum_enabled;
1403 e.ctx.file_has_checksum = has_checksum;
1404 e.ctx.progress_cb = opts ? opts->progress_cb : NULL;
1405 e.ctx.progress_user_data = opts ? opts->user_data : NULL;
1406 e.ctx.dict = frame_dict ? dict : NULL;
1407 e.ctx.dict_size = frame_dict_size;
1408 e.ctx.dict_huf = frame_dict ? dict_huf : NULL;
1409 e.w.f = f_out;
1410 e.w.bytes_processed = (uint64_t)total; // Progress counts across frames
1411 rc = zxc_stream_engine_start(&e, f_in, n_threads);
1412 if (UNLIKELY(rc != ZXC_OK)) break; // LCOV_EXCL_LINE
1413 running = 1;
1414 }
1415
1416 const int64_t r = zxc_stream_decode_frame(&e, f_in, has_seek);
1417 if (UNLIKELY(r < 0)) {
1418 rc = (int)r;
1419 break;
1420 }
1421 total += r;
1422 rc = zxc_stream_read_exact(&e.ctx, f_in, magic, sizeof(magic), ZXC_ERROR_CORRUPT_DATA);
1423 }
1424 if (running) {
1425 const int crc = zxc_stream_engine_close(&e, f_in);
1426 if (rc == ZXC_OK) rc = crc;
1427 // Settled by the writer, now stopped.
1428 else if (e.ctx.fail_code)
1429 rc = e.ctx.fail_code;
1430 }
1431 // stdio defers write errors to flush time (see zxc_stream_compress).
1432 if (rc == ZXC_OK && f_out && UNLIKELY(fflush(f_out) != 0)) rc = ZXC_ERROR_IO;
1433 return rc != ZXC_OK ? rc : total;
1434}
1435
1437static int zxc_file_scan_read(const zxc_scan_src_t* src, const uint64_t off, void* dst,
1438 const size_t len) {
1439 FILE* const f = (FILE*)src->ctx;
1440 if (UNLIKELY(fseeko(f, (long long)off, SEEK_SET) != 0 || fread(dst, 1, len, f) != len))
1441 return ZXC_ERROR_IO; // LCOV_EXCL_LINE
1442 return ZXC_OK;
1443}
1444
1449static int zxc_stream_read_frame_info(FILE* f_in, zxc_frame_info_t* info) {
1450 long long saved = 0;
1451 uint64_t size = 0;
1452 int rc = zxc_file_span(f_in, &saved, &size);
1453 if (UNLIKELY(rc != ZXC_OK)) return rc;
1454 const zxc_scan_src_t src = {zxc_file_scan_read, NULL, f_in, size};
1455 rc = zxc_scan_whole_frame(&src, info, NULL);
1456 fseeko(f_in, saved, SEEK_SET);
1457 return rc;
1458}
1459
1467 if (UNLIKELY(!f_in)) return ZXC_ERROR_NULL_INPUT;
1468 long long saved = 0;
1469 uint64_t size = 0;
1470 int rc = zxc_file_span(f_in, &saved, &size);
1471 if (UNLIKELY(rc != ZXC_OK)) return rc;
1473 const zxc_scan_src_t src = {zxc_file_scan_read, NULL, f_in, size};
1474 rc = zxc_scan_container(&src, 0, &info);
1475 fseeko(f_in, saved, SEEK_SET);
1476 if (UNLIKELY(rc != ZXC_OK)) return rc;
1477 if (UNLIKELY(info.dsize > (uint64_t)INT64_MAX)) return ZXC_ERROR_CORRUPT_DATA;
1478 return (int64_t)info.dsize;
1479}
1480
1486// cppcheck-suppress unusedFunction
1487int zxc_stream_get_frame_info(FILE* f_in, zxc_frame_info_t* info, const size_t info_size) {
1488 if (UNLIKELY(!f_in || !info)) return ZXC_ERROR_NULL_INPUT;
1489 zxc_frame_info_t got;
1490 const int rc = zxc_stream_read_frame_info(f_in, &got);
1491 if (rc == ZXC_OK) zxc_frame_info_copy(info, info_size, &got);
1492 return rc;
1493}
1494
1500// cppcheck-suppress unusedFunction
1501int zxc_stream_get_last_frame_info(FILE* f_in, const uint64_t end, zxc_frame_info_t* info,
1502 const size_t info_size) {
1503 if (UNLIKELY(!f_in || !info)) return ZXC_ERROR_NULL_INPUT;
1504 long long saved = 0;
1505 uint64_t size = 0;
1506 int rc = zxc_file_span(f_in, &saved, &size);
1507 if (UNLIKELY(rc != ZXC_OK)) return rc;
1508 zxc_frame_info_t got;
1509 if (UNLIKELY(end > size)) {
1511 } else {
1512 const zxc_scan_src_t src = {zxc_file_scan_read, NULL, f_in, end};
1513 uint64_t start = 0;
1514 rc = zxc_scan_frame(&src, end, &start, &got, NULL, NULL);
1515 }
1516 fseeko(f_in, saved, SEEK_SET);
1517 if (rc == ZXC_OK) zxc_frame_info_copy(info, info_size, &got);
1518 return rc;
1519}
1520
1521// ============================================================================
1522// SEEKABLE FILE* WRAPPER
1523// ============================================================================
1524// Adapts a FILE* into a thread-safe zxc_reader_t (pread, or ReadFile +
1525// OVERLAPPED on Windows). It lives here rather than in zxc_seekable.c so that
1526// file stays freestanding.
1527
1528#if defined(_WIN32)
1530typedef struct {
1531 HANDLE handle; /* OS handle from _get_osfhandle(_fileno(f)) */
1532 uint64_t size; /* total file size in bytes */
1533} zxc_stdio_ctx_t;
1534
1547// LCOV_EXCL_START - Windows I/O path, not reachable on POSIX CI
1548static int64_t zxc_stdio_read_at(void* vctx, void* dst, size_t len, uint64_t offset) {
1549 zxc_stdio_ctx_t* const ctx = (zxc_stdio_ctx_t*)vctx;
1550 OVERLAPPED ov;
1551 ZXC_MEMSET(&ov, 0, sizeof(ov));
1552 ov.Offset = (DWORD)(offset & 0xFFFFFFFFU);
1553 ov.OffsetHigh = (DWORD)(offset >> 32);
1554 DWORD bytes_read = 0;
1555 if (!ReadFile(ctx->handle, dst, (DWORD)len, &bytes_read, &ov)) return ZXC_ERROR_IO;
1556 return (bytes_read == (DWORD)len) ? (int64_t)len : ZXC_ERROR_IO;
1557}
1558// LCOV_EXCL_STOP
1559
1560#else /* POSIX */
1562typedef struct {
1563 int fd; /* descriptor from fileno(f) */
1564 uint64_t size; /* total file size in bytes */
1565} zxc_stdio_ctx_t;
1566
1579// cppcheck-suppress constParameterCallback ; the reader callback type takes void*
1580static int64_t zxc_stdio_read_at(void* vctx, void* dst, size_t len, uint64_t offset) {
1581 const zxc_stdio_ctx_t* const ctx = (const zxc_stdio_ctx_t*)vctx;
1582 return zxc_pread_full(ctx->fd, dst, len, offset) == (int64_t)len ? (int64_t)len : ZXC_ERROR_IO;
1583}
1584#endif /* _WIN32 */
1585
1597 if (UNLIKELY(!f)) return NULL;
1598
1599 // Snapshot the caller's file position so we can restore it.
1600 const long long saved_pos = ftello(f);
1601 if (UNLIKELY(saved_pos < 0)) return NULL; // LCOV_EXCL_LINE
1602
1603 // LCOV_EXCL_START - ftello/fseeko failure paths not reachable in CI
1604 if (UNLIKELY(fseeko(f, 0, SEEK_END) != 0)) return NULL;
1605 const long long file_size = ftello(f);
1606 (void)fseeko(f, saved_pos, SEEK_SET);
1607 if (UNLIKELY(file_size <= 0)) return NULL;
1608 // LCOV_EXCL_STOP
1609
1610 zxc_stdio_ctx_t* const ctx = (zxc_stdio_ctx_t*)ZXC_MALLOC(sizeof(*ctx));
1611 if (UNLIKELY(!ctx)) return NULL; // LCOV_EXCL_LINE
1612
1613#if defined(_WIN32)
1614 ctx->handle = (HANDLE)(intptr_t)_get_osfhandle(_fileno(f)); // LCOV_EXCL_LINE
1615#else
1616 ctx->fd = fileno(f);
1617#endif
1618 ctx->size = (uint64_t)file_size;
1619
1620 const zxc_reader_t reader = {
1621 .read_at = zxc_stdio_read_at, .ctx = ctx, .size = (uint64_t)file_size};
1622
1623 zxc_seekable* const s = zxc_seekable_open_reader(&reader);
1624 if (UNLIKELY(!s)) {
1625 ZXC_FREE(ctx);
1626 return NULL;
1627 }
1628
1629 // Hand the ctx lifetime over to the seekable handle.
1631 return s;
1632}
struct zxc_cctx_s zxc_cctx
Opaque reusable compression context (see zxc_create_cctx).
Definition zxc_buffer.h:370
struct zxc_dctx_s zxc_dctx
Opaque reusable decompression context (see zxc_create_dctx).
Definition zxc_buffer.h:372
#define ZXC_BLOCK_SIZE_DEFAULT
Default block size (512 KB).
size_t zxc_compress_bound(const size_t input_size)
Maximum compressed size for an input of input_size bytes.
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
@ ZXC_ERROR_BAD_MAGIC
Invalid magic word in file header.
Definition zxc_error.h:48
@ 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_BAD_HEADER
Corrupted or invalid header (checksum mismatch).
Definition zxc_error.h:50
@ ZXC_ERROR_SRC_TOO_SMALL
Source buffer too small or truncated input.
Definition zxc_error.h:45
@ ZXC_ERROR_DICT_REQUIRED
File requires a dictionary but none was provided.
Definition zxc_error.h:69
@ ZXC_ERROR_DICT_UNSUPPORTED
Dictionary not supported by this context's workspace, or library built without dictionary support.
Definition zxc_error.h:75
@ ZXC_ERROR_BAD_CHECKSUM
A block's decoded bytes fail its checksum, or the archive digest mismatches.
Definition zxc_error.h:51
@ ZXC_OK
Success (no error).
Definition zxc_error.h:38
@ ZXC_ERROR_BAD_BLOCK_SIZE
Invalid block size.
Definition zxc_error.h:66
#define ZXC_MAGIC_WORD
Magic word opening every frame, stored little-endian (F5 2E B0 9C).
#define ZXC_FILE_HEADER_SIZE
File header size: Magic(4) + Version(1) + Chunk(1) + Flags(1) + Reserved(7) + Checksum(2).
#define ZXC_MALLOC(size)
Heap allocator.
Definition zxc_deps.h:68
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.
#define ZXC_ALWAYS_INLINE
Forces a function to be inlined (fallback for non-GCC/Clang/MSVC compilers).
#define ZXC_ALIGNED_FREE(ptr)
Counterpart deallocator for ZXC_ALIGNED_MALLOC.
Definition zxc_deps.h:137
#define ZXC_ALIGNMENT_MASK
Bitmask for cache-line alignment checks.
#define ZXC_ALIGNED_MALLOC(size, alignment)
Cache-line-aligned allocator.
Definition zxc_deps.h:134
#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_CACHE_LINE_SIZE
Assumed CPU cache line size for alignment.
#define ZXC_DECOMPRESS_TAIL_PAD
Tail padding required on the decompression destination buffer.
@ ZXC_LEVEL_DEFAULT
Recommended default: better ratio and decode speed than LZ4.
zxc_seekable * zxc_seekable_open_reader(const zxc_reader_t *r)
Opens a seekable archive through a user-supplied reader.
struct zxc_seekable_s zxc_seekable
Opaque handle for a seekable ZXC archive.
int64_t zxc_stream_decompress(FILE *f_in, FILE *f_out, const zxc_dctx *cfg)
Decompresses a FILE* stream to another FILE* stream.
zxc_seekable * zxc_seekable_open_file(FILE *f)
Opens a seekable archive backed by an open FILE*.
int64_t zxc_stream_compress(FILE *f_in, FILE *f_out, const zxc_cctx *cfg)
Compresses a FILE* stream to another FILE* stream.
int64_t zxc_stream_get_decompressed_size(FILE *f_in)
Reads the total decompressed size from the footers of a file.
int zxc_stream_get_frame_info(FILE *f_in, zxc_frame_info_t *info, const size_t info_size)
Reads a file's frame header and footer, without decoding.
int zxc_stream_get_last_frame_info(FILE *f_in, const uint64_t end, zxc_frame_info_t *info, const size_t info_size)
Reads the frame that ends at offset end of a file, without decoding.
#define ZXC_MAX_THREADS
Thread cap, a guard against memory exhaustion: each worker of the FILE* pipeline holds up to four job...
On-disk header structure for a ZXC block (8 bytes, little-endian).
uint32_t comp_size
Compressed size excluding this header.
uint8_t block_type
Block type (see zxc_block_type_t).
Compression / decompression context.
int compression_level
Compression level.
uint8_t * dict_buffer
[dict | data] concat scratch carved from memory_block when dict_size > 0 (NULL otherwise).
What zxc_scan_container learns about a container.
uint64_t dsize
Sum of the stored decompressed sizes.
Compression parameters of a zxc_cctx, the private form of what zxc_cctx_set_param() / zxc_cctx_set_di...
Decompression parameters of a zxc_dctx; see zxc_cparams_t.
What a frame's header and footer declare, read without decoding.
Definition zxc_buffer.h:275
Storage-agnostic reader interface for seekable archives.
Where a container scan reads from: memory, or a positioned callback (the FILE* size query).
One-shot buffer compression and decompression.
int zxc_read_block_header(const uint8_t *src, const size_t src_size, zxc_block_header_t *bh)
Parses and validates a block header from src.
Definition zxc_common.c:699
void zxc_cctx_free(zxc_cctx_t *ctx)
Releases all resources owned by a compression context.
Definition zxc_common.c:382
int zxc_write_block_header(uint8_t *dst, const size_t dst_capacity, const zxc_block_header_t *bh)
Serialises a block header (8 bytes) into dst.
Definition zxc_common.c:680
int zxc_scan_container(const zxc_scan_src_t *src, const size_t req_chunk, zxc_container_info_t *info)
Measures a whole container without decoding: frames from the last back to the first through zxc_scan_...
Definition zxc_common.c:605
int zxc_seek_table_header(uint8_t *dst, const size_t dst_capacity, const uint64_t num_blocks)
Writes a seek table's block header for num_blocks entries.
Definition zxc_common.c:731
int zxc_scan_frame(const zxc_scan_src_t *src, const uint64_t end, uint64_t *start, zxc_frame_info_t *info, size_t *footer_len, int *has_dict)
Measures the frame ending at end without decoding: its footer's compressed size gives where it starts...
Definition zxc_common.c:561
int zxc_scan_whole_frame(const zxc_scan_src_t *src, zxc_frame_info_t *info, size_t *footer_len)
The frame that must span all of src: zxc_get_frame_info() and its FILE* twin.
Definition zxc_common.c:593
size_t zxc_seek_write_group(uint8_t *dst, uint64_t *anchor, const uint32_t *sizes, const uint32_t cnt)
Writes one group (*anchor, then cnt sizes) into dst, which holds ZXC_SEEK_GROUP_BYTES,...
Definition zxc_common.c:763
int zxc_check_eof_header(const uint8_t *hdr)
Checks an EOF block header against its one valid form: type 255, every other field zero,...
Definition zxc_common.c:741
int zxc_cctx_init(zxc_cctx_t *ctx, const size_t chunk_size, const int mode, const int level, const int checksum_enabled, const size_t dict_size)
Initialises a compression / decompression context, allocating the persistent buffer with ZXC_ALIGNED_...
Definition zxc_common.c:319
int zxc_read_file_header(const uint8_t *src, const size_t src_size, size_t *out_block_size, int *out_has_checksum, int *out_has_dict, uint32_t *out_dict_id, int *out_has_seek)
Parses and validates a ZXC file header from src.
Definition zxc_common.c:499
int zxc_write_file_footer(uint8_t *dst, const size_t dst_capacity, const uint64_t prefix, const uint64_t src_size, const uint64_t digest, const int checksum_enabled)
Writes the file footer: the archive digest when checksum_enabled, then the two sizes and their length...
Definition zxc_common.c:825
int zxc_write_file_header(uint8_t *dst, const size_t dst_capacity, const size_t chunk_size, const int has_checksum, const int has_dict, const uint32_t dict_id, const int has_seek)
Serialises a ZXC file header into dst.
Definition zxc_common.c:468
int zxc_cctx_attach_dict_huf(zxc_cctx_t *ctx, const uint8_t *lengths)
Attach the shared dictionary literal Huffman table to a context.
Definition zxc_common.c:435
int zxc_check_file_footer(const uint8_t *footer, const size_t avail, const zxc_footer_layout_t *l, const uint64_t src_size, uint64_t *digest)
Checks a footer against the one the frame implies.
Definition zxc_common.c:840
int zxc_check_seek_header(const uint8_t *hdr, const size_t avail, const uint64_t total_out, const size_t block_size, uint64_t *sek_bytes)
Checks the SEK block header the file header announced, on the avail bytes present.
Definition zxc_common.c:750
int zxc_compress_chunk_wrapper(zxc_cctx_t *ctx, const uint8_t *chunk, const size_t src_sz, uint8_t *dst, const size_t dst_cap, const uint64_t block_index)
Compresses one chunk into a single ZXC block (the compression hot path).
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.
const zxc_cparams_t * zxc_cctx_params(const zxc_cctx *cctx)
Parameters a public context carries, for the FILE* and push paths.
static int zxc_stream_engine_drain(zxc_stream_engine_t *e)
Waits until the writer has written every job queued so far; returns io_error.
Definition zxc_driver.c:879
static int zxc_stream_regular_fd(FILE *f)
Descriptor of a regular-file stream, -1 for anything else (may be live).
Definition zxc_driver.c:359
static void zxc_stream_engine_stop(zxc_stream_engine_t *e, const int writer_started)
Stops the threads, then frees the ring and its synchronisation objects.
Definition zxc_driver.c:723
static void zxc_stream_finish_decompress(zxc_stream_ctx_t *ctx, const uint64_t frame_out, FILE *f_in)
Consumes and validates the trailer of a decompressed stream.
static size_t zxc_stream_read_blocks(zxc_stream_ctx_t *ctx, FILE *f_in, zxc_stream_job_t *job, int *n_blocks, int *eof)
Fills a decompression job with up to ctx->batch consecutive blocks.
Definition zxc_driver.c:899
static int64_t zxc_stdio_read_at(void *vctx, void *dst, size_t len, uint64_t offset)
Thread-safe positioned read backing the seekable FILE* reader.
static void zxc_stream_reader_fail(zxc_stream_ctx_t *ctx, const int code)
Records a reader failure; workers write fail_code under the same lock.
Definition zxc_driver.c:433
static int zxc_stream_read_frame_header(zxc_stream_ctx_t *ctx, FILE *f_in, const uint8_t *magic, const int has_dict, const uint32_t dict_id, size_t *chunk, int *has_checksum, int *has_seek, int *frame_dict)
Reads and checks a frame header whose magic word is already read.
static int64_t zxc_stream_read_at(const zxc_stream_ctx_t *ctx, void *dst, const size_t len, const uint64_t offset)
Positioned read of the input, safe from any thread.
Definition zxc_driver.c:395
static void zxc_stream_open_positioned(zxc_stream_ctx_t *ctx, FILE *f_in, const int fd)
Switches a regular file to positioned reads from its current position.
Definition zxc_driver.c:375
static int64_t zxc_stream_in_read(zxc_stream_ctx_t *ctx, FILE *f, void *dst, const size_t len)
Reads up to len input bytes on the reader thread: by position while the engine reads positioned,...
Definition zxc_driver.c:419
static int zxc_stream_read_frame_info(FILE *f_in, zxc_frame_info_t *info)
Reads the header and the end of the file in f_in, from offset 0, and validates the frame they describ...
static int zxc_file_span(FILE *f, long long *saved, uint64_t *size)
The size of f; its position goes to saved, for the caller to restore.
static int zxc_stream_process(const zxc_stream_ctx_t *ctx, zxc_cctx_t *cctx, const uint8_t *in, const size_t in_sz, uint8_t *out, const size_t out_cap, const uint64_t block_index)
Runs one chunk through the codec the stream's mode selects.
Definition zxc_driver.c:446
static int64_t zxc_stream_decode_frame(zxc_stream_engine_t *e, FILE *f_in, const int has_seek)
Decodes one frame's blocks, SEK block and footer through a started engine.
static size_t zxc_stream_read_chunk(zxc_stream_ctx_t *ctx, FILE *f_in, zxc_stream_job_t *job, int *eof)
Fills a compression job with the next chunk of the source.
Definition zxc_driver.c:999
static int zxc_ring_next(const zxc_stream_ctx_t *ctx, const int idx)
The ring slot after idx.
Definition zxc_driver.c:275
static int zxc_stream_engine_close(zxc_stream_engine_t *e, FILE *f_in)
Stops a decompression engine; moves the FILE* to where positioned reads ended.
Definition zxc_driver.c:871
static int64_t zxc_pread_full(const int fd, void *dst, const size_t len, const uint64_t offset)
Thread-safe pread of len bytes, retrying EINTR and partial transfers.
Definition zxc_driver.c:341
static int zxc_stream_read_exact(zxc_stream_ctx_t *ctx, FILE *f, void *dst, const size_t len, const int short_code)
Reads exactly len bytes, telling a clean end from a short read.
static int zxc_stream_engine_start(zxc_stream_engine_t *e, FILE *f_in, const int n_threads)
Allocates the job ring and starts the workers and the writer.
Definition zxc_driver.c:784
static void * zxc_stream_worker(void *arg)
Worker thread: pull a job, process it, hand it to the writer.
Definition zxc_driver.c:509
static int zxc_file_scan_read(const zxc_scan_src_t *src, const uint64_t off, void *dst, const size_t len)
zxc_scan_src_t over a FILE*.
static void * zxc_async_writer(void *arg)
Asynchronous writer thread function.
Definition zxc_driver.c:628
job_status_t
Represents the lifecycle states of a processing job within the ring buffer.
Definition zxc_driver.c:89
@ JOB_STATUS_FREE
The job slot is empty and available to be filled with new data by the writer.
Definition zxc_driver.c:89
@ JOB_STATUS_FILLED
The job slot has been populated with input data and is ready for processing by a worker.
Definition zxc_driver.c:89
@ JOB_STATUS_PROCESSED
The worker has finished processing the data; the result is ready to be consumed/written out.
Definition zxc_driver.c:89
static int zxc_stream_read_loop(zxc_stream_ctx_t *ctx, FILE *f_in, int read_idx)
Reads the input and feeds the ring until the stream ends.
static int zxc_stream_decode_job(const zxc_stream_ctx_t *ctx, zxc_cctx_t *cctx, const zxc_stream_job_t *job, const uint8_t *in, uint8_t *dict_work)
Decodes a job's blocks into its output.
Definition zxc_driver.c:462
static void zxc_stream_finish_compress(zxc_stream_ctx_t *ctx, writer_args_t *w, FILE *f_out)
Closes a compressed stream: EOF block, optional seek table, footer.
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.
void zxc_seekable_attach_owned_ctx(zxc_seekable *s, void *ctx)
Hands ownership of a heap-allocated reader context to a seekable handle.
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_OPTS_LEVEL(o, dflt)
Compression level, 0 meaning the default.
static uint8_t zxc_hash8(const uint8_t *p)
Computes the 1-byte checksum for block headers.
#define ZXC_FOOTER_MAX_SIZE_WITH_DIGEST
Most bytes a footer spans: the digest and the two longest sizes.
#define ZXC_OPTS_DICT_SIZE(o)
Dictionary length from a (possibly NULL) parameter struct.
#define ZXC_OPTS_BLOCK_SIZE(o, dflt)
Block size, 0 meaning the default.
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.
void(* zxc_progress_callback_t)(uint64_t bytes_processed, uint64_t bytes_total, const void *user_data)
Progress callback function type.
Definition zxc_opts.h:40
Seekable compression and random-access decompression API.
FILE*-flavored variants of the ZXC 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.