|
ZXC 1.0.3
High-performance asymmetric lossless compression library
|
Single-shot, buffer-based compression and decompression. More...
Topics | |
| Block API | |
| Single-block compression/decompression without file framing. | |
| Reusable Context API | |
| Opaque, reusable compression / decompression contexts. | |
Data Structures | |
| struct | zxc_frame_info_t |
| What a frame's header and footer declare, read without decoding. More... | |
Functions | |
| size_t | zxc_compress_bound (const size_t input_size) |
Maximum compressed size for an input of input_size bytes. | |
| int64_t | zxc_compress (const void *src, const size_t src_size, void *dst, const size_t dst_capacity, int level) |
Compresses a buffer into a complete ZXC archive, at level. | |
| int64_t | zxc_decompress (const void *src, const size_t src_size, void *dst, const size_t dst_capacity) |
| Decompresses a complete ZXC archive. | |
| size_t | zxc_decompress_inplace_bound (const void *src, const size_t src_size) |
Buffer size required for an in-place decompression of src. | |
| int64_t | zxc_decompress_inplace (void *buffer, const size_t buffer_capacity, const size_t comp_size) |
| Decompresses inside a single caller-owned buffer. | |
| uint64_t | zxc_get_decompressed_size (const void *src, const size_t src_size) |
| Reads the original size from the archive footers, without decoding. | |
| uint32_t | zxc_get_dict_id (const void *src, size_t src_size) |
| Reads the dictionary ID an archive requires, from its header. | |
| int | zxc_requires_dict (const void *src, size_t src_size) |
| Tells whether an archive requires a dictionary, from its header. | |
| int | zxc_get_frame_info (const void *src, size_t src_size, zxc_frame_info_t *info, size_t info_size) |
| Reads a frame's header and footer, without decoding. | |
| int | zxc_get_last_frame_info (const void *src, size_t src_size, zxc_frame_info_t *info, size_t info_size) |
Reads the last frame of src, without decoding. | |
| size_t | zxc_frame_info_size (void) |
| Returns sizeof(zxc_frame_info_t) as compiled into the library. | |
Single-shot, buffer-based compression and decompression.
| int64_t zxc_compress | ( | const void * | src, |
| const size_t | src_size, | ||
| void * | dst, | ||
| const size_t | dst_capacity, | ||
| const int | level ) |
Compresses a buffer into a complete ZXC archive, at level.
The simple form: one level, every other parameter at its default (512 KB blocks, no checksum, no seek table, no dictionary). For anything else, configure a context and call zxc_compress_cctx(). Writes the file header followed by compressed blocks; single-threaded and blocking.
| [in] | src | Source buffer; may be NULL when src_size is 0. |
| [in] | src_size | Source size in bytes; 0 writes the empty archive (file header + EOF block + footer). |
| [out] | dst | Destination buffer. |
| [in] | dst_capacity | Capacity of dst. |
| [in] | level | Compression level, 1 to 7 (zxc_compression_level_t); 0 selects ZXC_LEVEL_DEFAULT. |
src and dst must not overlap (same contract as memcpy).dst (> 0), or a negative zxc_error_t code: ZXC_ERROR_BAD_LEVEL for any other level, ZXC_ERROR_DST_TOO_SMALL, ...Compresses a buffer into a complete ZXC archive, at level.
Public API; full contract in zxc_buffer.h. The frame walk of zxc_compress_cctx() on a context that lives for the call: 0 selects ZXC_LEVEL_DEFAULT, any other level outside 1..7 is refused as the setter would refuse it.
Definition at line 2241 of file zxc_dispatch.c.
References zxc_cctx_free(), zxc_compress_cctx_resolved(), zxc_cparams_defaults(), zxc_cparams_resolve(), ZXC_ERROR_BAD_LEVEL, ZXC_ERROR_NULL_INPUT, and ZXC_LEVEL_ULTRA.
| size_t zxc_compress_bound | ( | const size_t | input_size | ) |
Maximum compressed size for an input of input_size bytes.
Covers the file header, block headers, and worst-case expansion of incompressible data. Use it to size the destination buffer.
| [in] | input_size | Input size in bytes. |
size_t on this platform (no buffer could hold it).Maximum compressed size for an input of input_size bytes.
The result accounts for the file header, per-block headers, block checksums, worst-case expansion, EOF block, seekable overhead (SEK block), and the file footer.
The block count is derived from ZXC_BLOCK_SIZE_MIN (4 KB) to guarantee the bound holds for all valid block sizes and seekable mode.
Definition at line 1022 of file zxc_common.c.
References ZXC_BLOCK_CHECKSUM_SIZE, ZXC_BLOCK_FORMAT_OVERHEAD, ZXC_BLOCK_HEADER_SIZE, ZXC_BLOCK_SIZE_MIN, ZXC_FILE_HEADER_SIZE, ZXC_FOOTER_MAX_SIZE_WITH_DIGEST, and zxc_seek_table_bytes().
Referenced by zxc_dict_train_huf(), and zxc_stream_engine_start().
| int64_t zxc_decompress | ( | const void * | src, |
| const size_t | src_size, | ||
| void * | dst, | ||
| const size_t | dst_capacity ) |
Decompresses a complete ZXC archive.
The simple form: no checksum verification and no dictionary. For either, configure a context and call zxc_decompress_dctx(). Expects a valid file header followed by compressed blocks; single-threaded and blocking.
src may hold several frames back to back; they decode in order into dst. Anything else after a frame is ZXC_ERROR_CORRUPT_DATA.dst, or a dst_capacity of 0, decodes nothing and reports what the archive holds: 0 for a well-formed empty one, ZXC_ERROR_DST_TOO_SMALL when it stores a payload, its own error otherwise. Success is never handed out early: a 0 here means a call with a destination would have returned 0 too, checksum and dictionary binding included. Not the reverse, since nothing is decoded: an archive that stores a payload reports ZXC_ERROR_DST_TOO_SMALL even where a decode would name the actual fault. Caller errors outrank all of it, before anything is read from src, a source too short to hold a frame included: a NULL dst with a non-zero dst_capacity is ZXC_ERROR_NULL_INPUT.| [in] | src | Compressed buffer. |
| [in] | src_size | Compressed size in bytes. |
| [out] | dst | Destination buffer, or NULL to probe (see above). |
| [in] | dst_capacity | Capacity of dst. |
src and dst must not overlap (same contract as memcpy).dst, 0 for an empty archive, or a negative zxc_error_t (e.g. ZXC_ERROR_CORRUPT_DATA).Decompresses a complete ZXC archive.
Public API; full contract in zxc_buffer.h. The checks of zxc_decompress_dctx() with no parameters, then the same container walk on a context that lives for the call.
Definition at line 2263 of file zxc_dispatch.c.
References ZXC_ERROR_NULL_INPUT, ZXC_FRAME_MIN_SIZE, zxc_probe_without_dst(), and zxc_short_input_verdict().
| int64_t zxc_decompress_inplace | ( | void * | buffer, |
| const size_t | buffer_capacity, | ||
| const size_t | comp_size ) |
Decompresses inside a single caller-owned buffer.
The archive must sit flush-right in buffer (its last comp_size bytes). Decoding runs left-to-right from buffer[0]; as long as buffer_capacity is at least zxc_decompress_inplace_bound, the write cursor provably never overtakes the read cursor; an archive whose output would reach unread input (padding, forged block sizes) is refused with ZXC_ERROR_CORRUPT_DATA first. One allocation instead of two, which is what makes this worthwhile on memory-constrained targets (embedded, FOTA, firmware).
buffer is both input and output: on success its first N bytes hold the decompressed data (N = the return value).| [in,out] | buffer | Work buffer holding the flush-right archive. |
| [in] | buffer_capacity | Total size of buffer in bytes. |
| [in] | comp_size | Size of the compressed archive in bytes. |
Decompresses inside a single caller-owned buffer.
Public API; full contract in zxc_buffer.h. The walk of zxc_decompress_inplace_dctx() with no parameters, on a context that lives for the call once the buffer checks have passed.
Definition at line 2280 of file zxc_dispatch.c.
References zxc_cctx_free(), zxc_dctx_decode_frame(), zxc_inplace_prepare(), and ZXC_OK.
| size_t zxc_decompress_inplace_bound | ( | const void * | src, |
| const size_t | src_size ) |
Buffer size required for an in-place decompression of src.
Reads src's header and footer only (no decoding) and returns decompressed_size plus the one-block and wild-copy safety margin zxc_decompress_inplace needs. Allocate that much, place the comp_size-byte archive flush-right in it, then decode.
| [in] | src | Compressed archive (only header + footer are read). |
| [in] | src_size | Archive size in bytes. |
src is not a valid archive.Buffer size required for an in-place decompression of src.
Reads the archive header (block size) and footer (decompressed size) without decoding, and returns decompressed_size + zxc_inplace_margin(...) (one block + accumulated per-block overhead + footer + wild-copy tail). A buffer of at least this size lets zxc_decompress_inplace decode with the compressed data placed flush-right, the write cursor never overtaking the read cursor.
Because src_size is attacker-controlled, inserted padding bytes can slide decoding dangerously close to the output boundary. The src_size + floor term keeps block 0 clear; the walk checks every later block. For authentic archives, this adjustment grows the bound by at most 16 bytes and guarantees it never shrinks.
Definition at line 881 of file zxc_dispatch.c.
References ZXC_FRAME_MIN_SIZE, zxc_inplace_probe(), and ZXC_OK.
| size_t zxc_frame_info_size | ( | void | ) |
Returns sizeof(zxc_frame_info_t) as compiled into the library.
Layout guard for bindings that mirror the struct by hand instead of compiling against this header: comparing the mirrored size against this at load time turns a silent layout drift into an explicit error.
Returns sizeof(zxc_frame_info_t) as compiled into the library.
Public API; see zxc_buffer.h.
Definition at line 31 of file zxc_common.c.
| uint64_t zxc_get_decompressed_size | ( | const void * | src, |
| const size_t | src_size ) |
Reads the original size from the archive footers, without decoding.
Sums the sizes the footers store, walking the frames back from the end. Each frame is checked like zxc_get_frame_info(), its size against what its bytes can hold: a forged footer returns 0 rather than drive an oversized allocation.
| [in] | src | Compressed buffer. |
| [in] | src_size | Compressed size in bytes. |
Reads the original size from the archive footers, without decoding.
Sums the footers through zxc_scan_container(), which checks each frame: a container that does not hold up returns 0.
Definition at line 935 of file zxc_dispatch.c.
References zxc_container_info_t::dsize, ZXC_OK, zxc_scan_container(), and zxc_scan_src_mem().
| uint32_t zxc_get_dict_id | ( | const void * | src, |
| const size_t | src_size ) |
Reads the dictionary ID an archive requires, from its header.
Also reported as zxc_frame_info_t::dict_id. For the ID of a .zxd file see zxc_dict_read_id(); to compute one from content, zxc_dict_compute_id().
| [in] | src | Compressed buffer. |
| [in] | src_size | Compressed size in bytes. |
Reads the dictionary ID an archive requires, from its header.
Public API; see zxc_buffer.h. Validates the magic, then returns the header's dict_id field when the dictionary flag is set. Does not decompress.
Definition at line 950 of file zxc_dispatch.c.
References ZXC_FILE_FLAG_HAS_DICTIONARY, ZXC_FILE_HEADER_SIZE, zxc_le32(), and ZXC_MAGIC_WORD.
| int zxc_get_frame_info | ( | const void * | src, |
| const size_t | src_size, | ||
| zxc_frame_info_t * | info, | ||
| const size_t | info_size ) |
Reads a frame's header and footer, without decoding.
Validates the header as a decoder would, parses the footer back from the end of src, and checks that the frame spans all of src and that its stored size is reachable. The blocks are not read, so a frame that passes may still fail to decode.
| [in] | src | Compressed buffer. |
| [in] | src_size | Compressed size in bytes. |
| [out] | info | Filled on success, untouched otherwise. |
| [in] | info_size | sizeof(*info) as the caller compiled it. |
Public API; see zxc_buffer.h.
Definition at line 980 of file zxc_dispatch.c.
References zxc_buffer_frame_info(), ZXC_ERROR_NULL_INPUT, zxc_frame_info_copy(), and ZXC_OK.
| int zxc_get_last_frame_info | ( | const void * | src, |
| const size_t | src_size, | ||
| zxc_frame_info_t * | info, | ||
| const size_t | info_size ) |
Reads the last frame of src, without decoding.
Same checks as zxc_get_frame_info(), but the frame starts at src_size - info->compressed_size, so concatenated frames walk back:
| [in] | src | Compressed buffer. |
| [in] | src_size | Bytes of src the frame ends. |
| [out] | info | Filled on success, untouched otherwise. |
| [in] | info_size | sizeof(*info) as the caller compiled it. |
Reads the last frame of src, without decoding.
Public API; see zxc_buffer.h.
Definition at line 995 of file zxc_dispatch.c.
References ZXC_ERROR_NULL_INPUT, zxc_frame_info_copy(), ZXC_OK, zxc_scan_frame(), and zxc_scan_src_mem().
| int zxc_requires_dict | ( | const void * | src, |
| const size_t | src_size ) |
Tells whether an archive requires a dictionary, from its header.
The header's dictionary flag alone decides: a required dictionary may have ID 0, which zxc_get_dict_id() cannot tell from none. Only the first ZXC_FILE_HEADER_SIZE bytes are read.
| [in] | src | Compressed buffer. |
| [in] | src_size | Compressed size in bytes. |
Public API; see zxc_buffer.h.
Definition at line 965 of file zxc_dispatch.c.
References ZXC_ERROR_NULL_INPUT, ZXC_OK, and zxc_read_file_header().