ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_pstream.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
24
26
30#include "zxc_internal.h"
31
32// =====================================================================
33// Compression
34// =====================================================================
35
80
124struct zxc_cstream_s {
125 zxc_cparams_t opts;
126 zxc_cctx* cctx;
127 size_t block_size;
128
129 uint8_t* in_block;
130 size_t in_used;
131
132 uint8_t* pending;
133 size_t pending_cap;
134 size_t pending_len;
135 size_t pending_pos;
136
137 uint64_t block_index;
138 uint64_t total_in;
139 uint64_t total_out;
140 uint64_t digest;
141
142 cstream_state_t state;
143 int error_code;
144};
145
158static int cs_set_error(zxc_cstream* cs, const int code) {
159 cs->error_code = code;
160 cs->state = CS_ERRORED;
161 return code;
162}
163
181static int cs_compress_block_from(zxc_cstream* cs, const uint8_t* RESTRICT src, const size_t len) {
182 const size_t bound = zxc_compress_block_bound(len);
183 // LCOV_EXCL_START
184 if (UNLIKELY(bound == 0)) return ZXC_ERROR_OVERFLOW;
185 if (UNLIKELY(bound > cs->pending_cap)) {
186 uint8_t* nb = (uint8_t*)ZXC_REALLOC(cs->pending, bound);
187 if (UNLIKELY(!nb)) return ZXC_ERROR_MEMORY;
188 cs->pending = nb;
189 cs->pending_cap = bound;
190 }
191 // LCOV_EXCL_STOP
192 const int64_t csize = zxc_compress_block_at(cs->cctx, src, len, cs->pending, cs->pending_cap,
193 &cs->opts, cs->block_index);
194 if (UNLIKELY(csize < 0)) return (int)csize; // LCOV_EXCL_LINE
195 cs->block_index++;
196
197 cs->pending_len = (size_t)csize;
198 cs->pending_pos = 0;
199 cs->total_in += len;
200 cs->total_out += (uint64_t)csize;
201 if (cs->opts.checksum_enabled && cs->pending_len >= ZXC_BLOCK_CHECKSUM_SIZE)
202 cs->digest = zxc_digest_combine(
203 cs->digest, zxc_le32(cs->pending + cs->pending_len - ZXC_BLOCK_CHECKSUM_SIZE));
204 return ZXC_OK;
205}
206
221 const int rc = cs_compress_block_from(cs, cs->in_block, cs->in_used);
222 if (LIKELY(rc == ZXC_OK)) cs->in_used = 0;
223 return rc;
224}
225
232static size_t zxc_outbuf_push(zxc_outbuf_t* out, const uint8_t* RESTRICT src, size_t* RESTRICT pos,
233 const size_t len) {
234 const size_t avail_out = out->size - out->pos;
235 const size_t avail_src = len - *pos;
236 const size_t n = avail_out < avail_src ? avail_out : avail_src;
237 if (n) {
238 ZXC_MEMCPY((uint8_t*)out->dst + out->pos, src + *pos, n);
239 out->pos += n;
240 *pos += n;
241 }
242 return n;
243}
244
247 zxc_outbuf_push(out, cs->pending, &cs->pending_pos, cs->pending_len);
248 return cs->pending_pos == cs->pending_len;
249}
250
264 zxc_cstream* cs = (zxc_cstream*)ZXC_CALLOC(1, sizeof(*cs));
265 if (UNLIKELY(!cs)) return NULL; // LCOV_EXCL_LINE
266
267 if (cfg) cs->opts = *zxc_cctx_params(cfg);
268
269 // Push streams take no dictionary yet (deferred until a need arises): the
270 // header never flags one, so a dictionary here would produce archives that
271 // decode wrong elsewhere.
272 if (UNLIKELY(cs->opts.dict || cs->opts.dict_size || cs->opts.dict_huf)) {
273 ZXC_FREE(cs);
274 return NULL;
275 }
276
277 cs->opts.level = ZXC_OPTS_LEVEL(&cs->opts, ZXC_LEVEL_DEFAULT);
278 cs->opts.block_size = ZXC_OPTS_BLOCK_SIZE(&cs->opts, ZXC_BLOCK_SIZE_DEFAULT);
279 // n_threads is ignored on this single-threaded path.
280 cs->opts.n_threads = 0;
281 cs->opts.progress_cb = NULL;
282 cs->opts.user_data = NULL;
283 cs->opts.seekable = 0;
284 cs->block_size = cs->opts.block_size;
285
286 // The block encoder reads its parameters from cs->opts on every call;
287 // the context only needs to exist.
288 cs->cctx = zxc_create_cctx();
289 // LCOV_EXCL_START
290 if (UNLIKELY(!cs->cctx)) {
291 ZXC_FREE(cs);
292 return NULL;
293 }
294 cs->in_block = (uint8_t*)ZXC_MALLOC(cs->block_size);
295 if (UNLIKELY(!cs->in_block)) {
296 zxc_free_cctx(cs->cctx);
297 ZXC_FREE(cs);
298 return NULL;
299 }
300 // LCOV_EXCL_STOP
301 // Pre-size pending so the file header and footer paths never need realloc.
305 cs->pending = (uint8_t*)ZXC_MALLOC(cs->pending_cap);
306 // LCOV_EXCL_START
307 if (UNLIKELY(!cs->pending)) {
308 ZXC_FREE(cs->in_block);
309 zxc_free_cctx(cs->cctx);
310 ZXC_FREE(cs);
311 return NULL;
312 }
313 // LCOV_EXCL_STOP
314 cs->state = CS_INIT;
315 return cs;
316}
317
325 const int w = zxc_write_file_header(cs->pending, cs->pending_cap, cs->block_size,
326 cs->opts.checksum_enabled, 0, 0, 0);
327 if (UNLIKELY(w < 0)) return w; // LCOV_EXCL_LINE
328 cs->pending_len = (size_t)w;
329 cs->pending_pos = 0;
330 cs->total_out = (uint64_t)w;
331 return ZXC_OK;
332}
333
343static int cs_stage_eof(zxc_cstream* cs) {
344 // LCOV_EXCL_START
345 if (UNLIKELY(ZXC_BLOCK_HEADER_SIZE > cs->pending_cap)) {
346 uint8_t* nb = (uint8_t*)ZXC_REALLOC(cs->pending, ZXC_BLOCK_HEADER_SIZE);
347 if (UNLIKELY(!nb)) return ZXC_ERROR_MEMORY;
348 cs->pending = nb;
349 cs->pending_cap = ZXC_BLOCK_HEADER_SIZE;
350 }
351 // LCOV_EXCL_STOP
352 const zxc_block_header_t eof = {
353 .block_type = (uint8_t)ZXC_BLOCK_EOF,
354 .block_flags = 0,
355 .reserved = 0,
356 .header_checksum = 0,
357 .comp_size = 0,
358 };
359 const int w = zxc_write_block_header(cs->pending, cs->pending_cap, &eof);
360 if (UNLIKELY(w < 0)) return w; // LCOV_EXCL_LINE
361 cs->pending_len = (size_t)w;
362 cs->pending_pos = 0;
363 cs->total_out += (uint64_t)w;
364 return ZXC_OK;
365}
366
377 const int w = zxc_write_file_footer(cs->pending, cs->pending_cap, cs->total_out, cs->total_in,
378 cs->digest, cs->opts.checksum_enabled);
379 if (UNLIKELY(w < 0)) return w; // LCOV_EXCL_LINE
380 cs->pending_len = (size_t)w;
381 cs->pending_pos = 0;
382 return ZXC_OK;
383}
384
386 if (!cs) return;
387 ZXC_FREE(cs->pending);
388 ZXC_FREE(cs->in_block);
389 zxc_free_cctx(cs->cctx);
390 ZXC_FREE(cs);
391}
392
396size_t zxc_cstream_in_size(const zxc_cstream* cs) { return cs ? cs->block_size : 0; }
397
406 if (UNLIKELY(!cs)) return 0; // LCOV_EXCL_LINE
407 const size_t b = zxc_compress_block_bound(cs->block_size);
408 return b == 0 ? cs->block_size : b;
409}
410
425 if (UNLIKELY(!cs || !out || !in || in->pos > in->size || out->pos > out->size ||
426 (in->size > in->pos && !in->src) || (out->size > out->pos && !out->dst) ||
427 cs->state == CS_DONE)) {
429 }
430 if (UNLIKELY(cs->state == CS_ERRORED)) return cs->error_code;
431
432 for (;;) {
433 switch (cs->state) {
434 case CS_INIT: {
435 const int rc = cs_stage_file_header(cs);
436 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
437 cs->state = CS_DRAIN_HEADER;
438 break;
439 }
440
441 case CS_DRAIN_HEADER:
442 case CS_DRAIN_BLOCK: {
443 if (!cs_drain_pending(cs, out)) return (int64_t)(cs->pending_len - cs->pending_pos);
444 cs->state = CS_ACCUMULATE;
445 break;
446 }
447
448 case CS_ACCUMULATE: {
449 const size_t avail_in = in->size - in->pos;
450 if (cs->in_used == 0 && avail_in >= cs->block_size) {
451 const int rc = cs_compress_block_from(cs, (const uint8_t*)in->src + in->pos,
452 cs->block_size);
453 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
454 in->pos += cs->block_size;
455 cs->state = CS_DRAIN_BLOCK;
456 break;
457 }
458
459 const size_t room = cs->block_size - cs->in_used;
460 const size_t n = avail_in < room ? avail_in : room;
461 if (n) {
462 ZXC_MEMCPY(cs->in_block + cs->in_used, (const uint8_t*)in->src + in->pos, n);
463 in->pos += n;
464 cs->in_used += n;
465 }
466
467 if (cs->in_used == cs->block_size) {
468 const int rc = cs_compress_one_block(cs);
469 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
470 cs->state = CS_DRAIN_BLOCK;
471 break;
472 }
473 // Block not yet full either in is empty or we made no progress.
474 return 0;
475 }
476
477 case CS_DRAIN_LAST:
478 case CS_DRAIN_EOF:
479 case CS_DRAIN_FOOTER:
480 case CS_DONE:
481 case CS_ERRORED:
482 // These states are owned by _end().
484 }
485 }
486}
487
498 if (UNLIKELY(!cs || !out || cs->state == CS_DONE)) return ZXC_ERROR_NULL_INPUT;
499 if (UNLIKELY(cs->state == CS_ERRORED)) return cs->error_code;
500
501 for (;;) {
502 switch (cs->state) {
503 case CS_INIT: {
504 // _end before any input, still need to emit file header.
505 const int rc = cs_stage_file_header(cs);
506 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
507 cs->state = CS_DRAIN_HEADER;
508 break;
509 }
510
511 case CS_DRAIN_HEADER:
512 case CS_DRAIN_BLOCK: {
513 if (!cs_drain_pending(cs, out)) return (int64_t)(cs->pending_len - cs->pending_pos);
514 cs->state = CS_ACCUMULATE;
515 break;
516 }
517
518 case CS_ACCUMULATE: {
519 // Compress the residual partial block (if any), then EOF + footer.
520 if (cs->in_used > 0) {
521 const int rc = cs_compress_one_block(cs);
522 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
523 cs->state = CS_DRAIN_LAST;
524 break;
525 }
526 // No residual data: go straight to EOF.
527 {
528 const int rc = cs_stage_eof(cs);
529 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
530 cs->state = CS_DRAIN_EOF;
531 break;
532 }
533 }
534
535 case CS_DRAIN_LAST: {
536 if (!cs_drain_pending(cs, out)) return (int64_t)(cs->pending_len - cs->pending_pos);
537 // After last data block -> EOF.
538 const int rc = cs_stage_eof(cs);
539 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
540 cs->state = CS_DRAIN_EOF;
541 break;
542 }
543
544 case CS_DRAIN_EOF: {
545 if (!cs_drain_pending(cs, out)) return (int64_t)(cs->pending_len - cs->pending_pos);
546 const int rc = cs_stage_footer(cs);
547 if (UNLIKELY(rc < 0)) return cs_set_error(cs, rc); // LCOV_EXCL_LINE
548 cs->state = CS_DRAIN_FOOTER;
549 break;
550 }
551
552 case CS_DRAIN_FOOTER: {
553 if (!cs_drain_pending(cs, out)) return (int64_t)(cs->pending_len - cs->pending_pos);
554 cs->state = CS_DONE;
555 return 0;
556 }
557
558 case CS_DONE:
559 case CS_ERRORED:
560 return cs->state == CS_ERRORED ? cs->error_code : 0; // LCOV_EXCL_LINE
561 }
562 }
563}
564
565// =====================================================================
566// Decompression
567// =====================================================================
568
616
688struct zxc_dstream_s {
689 zxc_dparams_t opts;
690 zxc_cctx_t inner;
691 int inner_initialized;
692 size_t block_size;
693 int file_has_checksum;
694 int file_has_seek;
695
696 uint8_t scratch[32];
697 size_t scratch_used;
698 size_t scratch_need;
699
700 uint8_t* payload;
701 size_t payload_cap;
702 size_t payload_used;
703 size_t payload_need;
704
705 uint8_t* decoded;
706 size_t decoded_cap;
707 size_t decoded_size;
708 size_t decoded_pos;
709
710 zxc_block_header_t cur_bh;
711 uint64_t sek_remaining;
712
713 uint64_t block_index;
714 uint64_t total_out;
715 uint64_t frame_in;
716 uint64_t digest;
717 int after_frame;
718 size_t alloc_block_size;
719
720 dstream_state_t state;
721 int error_code;
722};
723
736static int ds_set_error(zxc_dstream* ds, const int code) {
737 ds->error_code = code;
738 ds->state = DS_ERRORED;
739 return code;
740}
741
749static int ds_pull(uint8_t* RESTRICT dst, size_t* RESTRICT used, const size_t need,
750 zxc_inbuf_t* in) {
751 const size_t want = need - *used;
752 const size_t avail = in->size - in->pos;
753 const size_t n = want < avail ? want : avail;
754 if (n) {
755 ZXC_MEMCPY(dst + *used, (const uint8_t*)in->src + in->pos, n);
756 in->pos += n;
757 *used += n;
758 }
759 return *used == need;
760}
761
763static void ds_free_buffers(zxc_dstream* ds) {
764 ZXC_FREE(ds->payload);
765 ZXC_FREE(ds->decoded);
766 if (ds->inner_initialized) zxc_cctx_free(&ds->inner);
767 ds->payload = NULL;
768 ds->decoded = NULL;
769 ds->payload_cap = ds->decoded_cap = 0;
770 ds->inner_initialized = 0;
771 ds->alloc_block_size = 0;
772}
773
775static void ds_start_frame(zxc_dstream* ds) {
776 ds->state = DS_NEED_FILE_HEADER;
777 ds->scratch_used = 0;
778 ds->scratch_need = ZXC_FILE_HEADER_SIZE;
779 ds->payload_used = ds->payload_need = 0;
780 ds->decoded_size = ds->decoded_pos = 0;
781 ds->sek_remaining = 0;
782 ds->block_index = 0;
783 ds->total_out = 0;
784 ds->frame_in = 0;
785 ds->digest = 0;
786}
787
800 zxc_dstream* ds = (zxc_dstream*)ZXC_CALLOC(1, sizeof(*ds));
801 if (UNLIKELY(!ds)) return NULL; // LCOV_EXCL_LINE
802 if (cfg) ds->opts = *zxc_dctx_params(cfg);
803 if (UNLIKELY(ds->opts.dict || ds->opts.dict_size || ds->opts.dict_huf)) {
804 ZXC_FREE(ds);
805 return NULL;
806 }
807 ds->opts.n_threads = 0;
808 ds->opts.progress_cb = NULL;
809 ds->opts.user_data = NULL;
810 ds_start_frame(ds);
811 return ds;
812}
813
820 if (UNLIKELY(!ds)) return; // LCOV_EXCL_LINE
821 ds_free_buffers(ds);
822 ZXC_FREE(ds);
823}
824
828int zxc_dstream_finished(const zxc_dstream* ds) { return (ds && ds->state == DS_DONE) ? 1 : 0; }
829
838 if (UNLIKELY(!ds)) return 0; // LCOV_EXCL_LINE
839 if (ds->block_size == 0) return ZXC_BLOCK_SIZE_DEFAULT;
840 const size_t b = zxc_compress_block_bound(ds->block_size);
841 return b == 0 ? ds->block_size : b;
842}
843
851 if (UNLIKELY(!ds)) return 0; // LCOV_EXCL_LINE
852 return ds->block_size == 0 ? ZXC_BLOCK_SIZE_DEFAULT : ds->block_size;
853}
854
856static int ds_drain_decoded(zxc_dstream* ds, zxc_outbuf_t* out, size_t* produced) {
857 const size_t n = zxc_outbuf_push(out, ds->decoded, &ds->decoded_pos, ds->decoded_size);
858 ds->total_out += n;
859 if (produced) *produced += n;
860 return ds->decoded_pos == ds->decoded_size;
861}
862
880 const int full = ds_pull(ds->scratch, &ds->scratch_used, ds->scratch_need, in);
881 // After a frame, bytes that open no other are corrupt, checked as they arrive.
882 if (UNLIKELY(ds->after_frame && !zxc_magic_prefix_ok(ds->scratch, ds->scratch_used)))
884 if (!full) return 1;
885
886 size_t bs = 0;
887 int has_csum = 0;
888 int has_seek = 0;
889 int has_dict = 0;
890 const int rc = zxc_read_file_header(ds->scratch, ds->scratch_used, &bs, &has_csum, &has_dict,
891 NULL, &has_seek);
892 if (UNLIKELY(rc != ZXC_OK)) return ds_set_error(ds, rc); // LCOV_EXCL_LINE
893 // Push streams take no dictionary yet, so an archive requiring one is refused.
894 if (UNLIKELY(has_dict)) return ds_set_error(ds, ZXC_ERROR_DICT_REQUIRED);
895 ds->block_size = bs;
896 ds->file_has_checksum = has_csum;
897 ds->file_has_seek = has_seek;
898
899 // Payload + decoded buffers sized from block_size; a frame declaring the
900 // block size of the one before keeps them.
901 if (ds->alloc_block_size != ds->block_size) {
902 ds_free_buffers(ds);
903 const size_t pb = zxc_compress_block_bound(ds->block_size);
904 // LCOV_EXCL_START
905 if (UNLIKELY(pb == 0)) return ds_set_error(ds, ZXC_ERROR_OVERFLOW);
906 // LCOV_EXCL_STOP
907 ds->payload_cap = pb;
908 ds->payload = (uint8_t*)ZXC_MALLOC(ds->payload_cap);
909
910 ds->decoded_cap = ds->block_size + ZXC_DECOMPRESS_TAIL_PAD;
911 ds->decoded = (uint8_t*)ZXC_MALLOC(ds->decoded_cap);
912 // LCOV_EXCL_START
913 if (UNLIKELY(!ds->payload || !ds->decoded)) return ds_set_error(ds, ZXC_ERROR_MEMORY);
914 if (UNLIKELY(zxc_cctx_init(&ds->inner, ds->block_size, 0, 0, 0, 0) != ZXC_OK))
915 return ds_set_error(ds, ZXC_ERROR_MEMORY);
916 // LCOV_EXCL_STOP
917 ds->inner_initialized = 1;
918 ds->alloc_block_size = ds->block_size;
919 }
920 ds->inner.checksum_enabled = ds->file_has_checksum && ds->opts.checksum_enabled;
921
922 ds->frame_in = ZXC_FILE_HEADER_SIZE;
923 ds->state = DS_NEED_BLOCK_HEADER;
924 ds->scratch_used = 0;
925 ds->scratch_need = ZXC_BLOCK_HEADER_SIZE;
926 return 0;
927}
928
946 if (!ds_pull(ds->scratch, &ds->scratch_used, ds->scratch_need, in)) return 1;
947
948 const int rc = zxc_read_block_header(ds->scratch, ds->scratch_used, &ds->cur_bh);
949 if (UNLIKELY(rc != ZXC_OK)) return ds_set_error(ds, rc); // LCOV_EXCL_LINE
950 ds->frame_in += ZXC_BLOCK_HEADER_SIZE;
951
952 if (ds->cur_bh.block_type == (uint8_t)ZXC_BLOCK_EOF) {
953 // EOF block: one valid header, no payload, no checksum.
954 if (UNLIKELY(zxc_check_eof_header(ds->scratch) != ZXC_OK))
956 ds->state = ds->file_has_seek ? DS_NEED_SEK_HEADER : DS_NEED_FOOTER;
957 ds->scratch_used = 0;
958 ds->scratch_need =
959 ds->file_has_seek
961 : zxc_footer_layout(ds->frame_in, ds->total_out, ds->file_has_checksum).len;
962 return 0;
963 }
964
965 // Data block: validate comp_size against the block_size negotiated in the file header.
966 if (UNLIKELY((uint64_t)ds->cur_bh.comp_size > (uint64_t)ds->block_size))
968
969 // Read comp_size [+ ZXC_BLOCK_CHECKSUM_SIZE if file-level checksums].
970 const uint64_t need = (uint64_t)ds->cur_bh.comp_size +
971 (ds->file_has_checksum ? (uint64_t)ZXC_BLOCK_CHECKSUM_SIZE : 0U);
972 if (UNLIKELY(need > ds->payload_cap)) return ds_set_error(ds, ZXC_ERROR_BAD_BLOCK_SIZE);
973
974 // Feed the full block (header + payload + opt csum) to zxc_decompress_block,
975 // so prefix with the 8-byte header we just parsed.
976 ZXC_MEMCPY(ds->payload, ds->scratch, ZXC_BLOCK_HEADER_SIZE);
977 ds->payload_used = ZXC_BLOCK_HEADER_SIZE;
978 ds->payload_need = (size_t)need + ZXC_BLOCK_HEADER_SIZE;
979 // LCOV_EXCL_START
980 if (UNLIKELY(ds->payload_need > ds->payload_cap)) {
981 // grow
982 uint8_t* nb = (uint8_t*)ZXC_REALLOC(ds->payload, ds->payload_need);
983 if (UNLIKELY(!nb)) return ds_set_error(ds, ZXC_ERROR_MEMORY);
984 ds->payload = nb;
985 ds->payload_cap = ds->payload_need;
986 }
987 // LCOV_EXCL_STOP
988 ds->state = DS_NEED_BLOCK_PAYLOAD;
989 return 0;
990}
991
1012 if (UNLIKELY(!ds || !out || !in || in->pos > in->size || out->pos > out->size ||
1013 (in->size > in->pos && !in->src) || (out->size > out->pos && !out->dst))) {
1014 return ZXC_ERROR_NULL_INPUT;
1015 }
1016 if (UNLIKELY(ds->state == DS_ERRORED)) return ds->error_code;
1017 if (ds->state == DS_DONE) {
1018 if (in->pos == in->size) return 0;
1019 ds_start_frame(ds); // concatenated archives: what follows is another frame
1020 }
1021
1022 size_t produced = 0;
1023
1024 for (;;) {
1025 switch (ds->state) {
1026 case DS_NEED_FILE_HEADER: {
1027 const int rc = ds_handle_need_file_header(ds, in);
1028 if (rc == 1) return (int64_t)produced;
1029 if (rc < 0) return rc;
1030 break;
1031 }
1032
1033 case DS_NEED_BLOCK_HEADER: {
1034 const int rc = ds_handle_need_block_header(ds, in);
1035 if (rc == 1) return (int64_t)produced;
1036 if (rc < 0) return rc;
1037 break;
1038 }
1039
1040 case DS_NEED_BLOCK_PAYLOAD: {
1041 if (!ds_pull(ds->payload, &ds->payload_used, ds->payload_need, in))
1042 return (int64_t)produced;
1043 ds->frame_in += ds->payload_need - ZXC_BLOCK_HEADER_SIZE;
1044 ds->state = DS_DECODE_BLOCK;
1045 break;
1046 }
1047
1048 case DS_DECODE_BLOCK: {
1049 const int direct = (out->size - out->pos) >= ds->decoded_cap;
1050 uint8_t* const ddst = direct ? (uint8_t*)out->dst + out->pos : ds->decoded;
1051 const int dsz =
1052 zxc_decompress_chunk_wrapper(&ds->inner, ds->payload, ds->payload_used, ddst,
1053 ds->decoded_cap, ds->block_index);
1054 if (UNLIKELY(dsz < 0)) return ds_set_error(ds, dsz);
1055 ds->block_index++;
1056 // Folded only when DS_VALIDATE_FOOTER will compare it.
1057 if (ds->file_has_checksum && ds->opts.checksum_enabled)
1058 ds->digest = zxc_digest_combine(
1059 ds->digest,
1060 zxc_le32(ds->payload + ds->payload_used - ZXC_BLOCK_CHECKSUM_SIZE));
1061
1062 if (direct) {
1063 out->pos += (size_t)dsz;
1064 produced += (size_t)dsz;
1065 ds->total_out += (uint64_t)dsz;
1066 ds->decoded_size = 0;
1067 ds->decoded_pos = 0;
1068 ds->state = DS_NEED_BLOCK_HEADER;
1069 ds->scratch_used = 0;
1070 ds->scratch_need = ZXC_BLOCK_HEADER_SIZE;
1071 break;
1072 }
1073
1074 ds->decoded_size = (size_t)dsz;
1075 ds->decoded_pos = 0;
1076 ds->state = DS_EMIT_DECODED;
1077 break;
1078 }
1079
1080 case DS_EMIT_DECODED: {
1081 const int done = ds_drain_decoded(ds, out, &produced);
1082 if (!done) return (int64_t)produced;
1083 ds->state = DS_NEED_BLOCK_HEADER;
1084 ds->scratch_used = 0;
1085 ds->scratch_need = ZXC_BLOCK_HEADER_SIZE;
1086 break;
1087 }
1088
1089 case DS_NEED_SEK_HEADER: {
1090 // Checked as bytes arrive: a wrong header fails without waiting.
1091 const int full = ds_pull(ds->scratch, &ds->scratch_used, ds->scratch_need, in);
1092 const int rc = zxc_check_seek_header(ds->scratch, ds->scratch_used, ds->total_out,
1093 ds->block_size, &ds->sek_remaining);
1094 if (UNLIKELY(rc == ZXC_ERROR_CORRUPT_DATA)) return ds_set_error(ds, rc);
1095 if (!full) return (int64_t)produced;
1096 ds->frame_in += ZXC_BLOCK_HEADER_SIZE + ds->sek_remaining;
1097 ds->state = DS_DRAIN_SEK_PAYLOAD;
1098 break;
1099 }
1100
1101 case DS_DRAIN_SEK_PAYLOAD: {
1102 const size_t avail = in->size - in->pos;
1103 const size_t n =
1104 (uint64_t)avail < ds->sek_remaining ? avail : (size_t)ds->sek_remaining;
1105 in->pos += n;
1106 ds->sek_remaining -= n;
1107 if (ds->sek_remaining > 0) return (int64_t)produced;
1108 ds->state = DS_NEED_FOOTER;
1109 ds->scratch_used = 0;
1110 ds->scratch_need =
1111 zxc_footer_layout(ds->frame_in, ds->total_out, ds->file_has_checksum).len;
1112 break;
1113 }
1114
1115 case DS_NEED_FOOTER: {
1116 // Checked as bytes arrive, like the SEK header.
1117 const int full = ds_pull(ds->scratch, &ds->scratch_used, ds->scratch_need, in);
1118 const zxc_footer_layout_t fl =
1119 zxc_footer_layout(ds->frame_in, ds->total_out, ds->file_has_checksum);
1120 const int rc =
1121 zxc_check_file_footer(ds->scratch, ds->scratch_used, &fl, ds->total_out, NULL);
1122 if (UNLIKELY(rc == ZXC_ERROR_CORRUPT_DATA)) return ds_set_error(ds, rc);
1123 if (!full) return (int64_t)produced;
1124 ds->state = DS_VALIDATE_FOOTER;
1125 break;
1126 }
1127
1128 case DS_VALIDATE_FOOTER: {
1129 const uint64_t stored_digest = ds->file_has_checksum ? zxc_le64(ds->scratch) : 0;
1130 if (ds->file_has_checksum && ds->opts.checksum_enabled &&
1131 UNLIKELY(stored_digest != ds->digest))
1133 // The call ends with the frame, its output handed over before
1134 // anything that follows is looked at.
1135 ds->after_frame = 1;
1136 ds->state = DS_DONE;
1137 return (int64_t)produced;
1138 }
1139
1140 case DS_DONE:
1141 return (int64_t)produced; // LCOV_EXCL_LINE
1142
1143 case DS_ERRORED:
1144 return ds->error_code;
1145 }
1146 }
1147}
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
size_t zxc_compress_block_bound(size_t input_size)
Maximum compressed size for a single block.
#define ZXC_BLOCK_SIZE_DEFAULT
Default block size (512 KB).
void zxc_free_cctx(zxc_cctx *cctx)
Frees a compression context and its resources.
zxc_cctx * zxc_create_cctx(void)
Creates a reusable compression context, every parameter at its default.
@ 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_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_DICT_REQUIRED
File requires a dictionary but none was provided.
Definition zxc_error.h:69
@ 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_FILE_HEADER_SIZE
File header size: Magic(4) + Version(1) + Chunk(1) + Flags(1) + Reserved(7) + Checksum(2).
#define ZXC_MALLOC(size)
Heap allocator.
Definition zxc_deps.h:68
#define ZXC_CALLOC(nmemb, size)
Zero-initialised heap allocator.
Definition zxc_deps.h:74
#define ZXC_FREE(ptr)
Heap deallocator.
Definition zxc_deps.h:86
#define ZXC_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_BLOCK_CHECKSUM_SIZE
Size of the per-block checksum field in bytes.
static int zxc_magic_prefix_ok(const uint8_t *p, const size_t n)
Whether the first n bytes (up to 4) at p agree with the magic word, so a frame may start there: the c...
#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.
struct zxc_dstream_s zxc_dstream
Opaque push-model decompression stream (see zxc_dstream_create).
size_t zxc_dstream_out_size(const zxc_dstream *ds)
Returns the suggested output chunk size for the decompressor.
void zxc_cstream_free(zxc_cstream *cs)
Releases a compression stream and all internal buffers.
struct zxc_cstream_s zxc_cstream
Opaque push-model compression stream (see zxc_cstream_create).
size_t zxc_dstream_in_size(const zxc_dstream *ds)
Returns the suggested input chunk size for the decompressor.
size_t zxc_cstream_out_size(const zxc_cstream *cs)
Returns the suggested output chunk size.
zxc_cstream * zxc_cstream_create(const zxc_cctx *cfg)
Allocates and initialises a push compression stream.
int64_t zxc_dstream_decompress(zxc_dstream *ds, zxc_outbuf_t *out, zxc_inbuf_t *in)
Push-side entry point: feeds compressed input and drains decoded output.
size_t zxc_cstream_in_size(const zxc_cstream *cs)
Returns the suggested input chunk size (configured block size).
void zxc_dstream_free(zxc_dstream *ds)
Releases a decompression stream and all internal buffers.
zxc_dstream * zxc_dstream_create(const zxc_dctx *cfg)
Allocates and initialises a push decompression stream.
int64_t zxc_cstream_compress(zxc_cstream *cs, zxc_outbuf_t *out, zxc_inbuf_t *in)
Push-side entry point: feeds input and drains compressed output.
int64_t zxc_cstream_end(zxc_cstream *cs, zxc_outbuf_t *out)
Finalises the stream: residual block (if any), EOF, and footer.
int zxc_dstream_finished(const zxc_dstream *ds)
Returns 1 iff the stream has reached DS_DONE.
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).
int checksum_enabled
1 if checksum calculation/verification is enabled.
Input buffer descriptor for push streaming.
Definition zxc_pstream.h:84
size_t size
Total bytes available in src.
Definition zxc_pstream.h:86
size_t pos
Bytes already consumed by the library (in/out).
Definition zxc_pstream.h:87
const void * src
Caller-owned input bytes.
Definition zxc_pstream.h:85
Output buffer descriptor for push streaming.
size_t size
Total capacity available at dst.
void * dst
Caller-owned output region.
size_t pos
Bytes already produced by the library (in/out).
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_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_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
Public constants: library version, block sizes, compression levels.
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).
const zxc_cparams_t * zxc_cctx_params(const zxc_cctx *cctx)
Parameters a public context carries, for the FILE* and push paths.
int64_t zxc_compress_block_at(zxc_cctx *cctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity, const zxc_cparams_t *opts, const uint64_t block_index)
zxc_compress_block, its checksum seeded with block_index (public entry: 0).
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 uint32_t zxc_le32(const void *p)
Reads a little-endian 32-bit value from a possibly unaligned address.
static uint64_t zxc_digest_combine(const uint64_t acc, const uint32_t block_checksum)
Folds a data block's 32-bit checksum into the running archive digest.
#define ZXC_OPTS_LEVEL(o, dflt)
Compression level, 0 meaning the default.
#define ZXC_FOOTER_MAX_SIZE_WITH_DIGEST
Most bytes a footer spans: the digest and the two longest sizes.
#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.
static int ds_handle_need_file_header(zxc_dstream *ds, zxc_inbuf_t *in)
Handles the DS_NEED_FILE_HEADER state.
static int cs_stage_eof(zxc_cstream *cs)
Stages the 8-byte EOF block into the pending buffer.
cstream_state_t
Lifecycle states of the push compression stream.
Definition zxc_pstream.c:69
@ CS_DONE
Finalisation complete; further _compress / _end calls are rejected.
Definition zxc_pstream.c:77
@ CS_DRAIN_EOF
Draining the EOF block; transitions to CS_DRAIN_FOOTER.
Definition zxc_pstream.c:75
@ CS_INIT
Initial state; nothing has been emitted yet.
Definition zxc_pstream.c:70
@ CS_DRAIN_HEADER
File header staged in pending; draining to out, then transitions to CS_ACCUMULATE.
Definition zxc_pstream.c:71
@ CS_DRAIN_BLOCK
A full data block was just compressed; draining it to out, then back to CS_ACCUMULATE.
Definition zxc_pstream.c:73
@ CS_DRAIN_LAST
Draining the final partial block produced inside zxc_cstream_end; transitions to CS_DRAIN_EOF.
Definition zxc_pstream.c:74
@ CS_ACCUMULATE
Copying input bytes into the internal block accumulator until it is full (or the stream is finalised)...
Definition zxc_pstream.c:72
@ CS_DRAIN_FOOTER
Draining the file footer; transitions to CS_DONE.
Definition zxc_pstream.c:76
@ CS_ERRORED
Sticky error state; subsequent calls return the latched error code.
Definition zxc_pstream.c:78
static void ds_start_frame(zxc_dstream *ds)
Puts ds at the start of a frame, its buffers kept for reuse.
static int cs_set_error(zxc_cstream *cs, const int code)
Latches a sticky error on the compression stream.
static int cs_compress_block_from(zxc_cstream *cs, const uint8_t *src, const size_t len)
Compresses one block from src into cs->pending.
static int ds_handle_need_block_header(zxc_dstream *ds, zxc_inbuf_t *in)
Handles the DS_NEED_BLOCK_HEADER state.
static int cs_stage_file_header(zxc_cstream *cs)
Stages the 16-byte file header into the pending buffer.
static int ds_set_error(zxc_dstream *ds, const int code)
Latches a sticky error on the decompression stream.
static int cs_stage_footer(zxc_cstream *cs)
Stages the file footer into the pending buffer.
static int ds_pull(uint8_t *dst, size_t *used, const size_t need, zxc_inbuf_t *in)
Accumulates caller input into a buffer until it holds the wanted size.
static size_t zxc_outbuf_push(zxc_outbuf_t *out, const uint8_t *src, size_t *pos, const size_t len)
Copies what fits of a staged buffer into the caller's output.
static int ds_drain_decoded(zxc_dstream *ds, zxc_outbuf_t *out, size_t *produced)
Flushes the decoded block; 1 once fully drained.
static int cs_compress_one_block(zxc_cstream *cs)
Compresses the accumulated (full or partial) block.
static void ds_free_buffers(zxc_dstream *ds)
Releases the per-frame buffers; the next header allocates them anew.
static int cs_drain_pending(zxc_cstream *cs, zxc_outbuf_t *out)
Flushes the pending staging buffer; 1 once fully drained.
dstream_state_t
Lifecycle states of the push decompression stream.
@ DS_ERRORED
Sticky error state; subsequent calls return the latched code.
@ DS_DECODE_BLOCK
Calling the underlying block decoder on the accumulated payload.
@ DS_VALIDATE_FOOTER
Validating total_out.
@ DS_NEED_BLOCK_PAYLOAD
Pulling a data block payload (and optional checksum) into payload.
@ DS_NEED_BLOCK_HEADER
Pulling an 8-byte block header into scratch.
@ DS_NEED_FOOTER
Pulling the file footer.
@ DS_NEED_SEK_HEADER
Just past the EOF block of an archive whose header announced a seek table: pulling and validating its...
@ DS_NEED_FILE_HEADER
Pulling the 16-byte file header into scratch.
@ DS_DRAIN_SEK_PAYLOAD
Skipping the SEK payload bytes.
@ DS_EMIT_DECODED
Draining decoded bytes from decoded into out.
@ DS_DONE
A frame fully consumed and validated; a call with more input starts the next one.
Push-based, single-threaded streaming compression and decompression.