ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
Buffer API

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.

Detailed Description

Single-shot, buffer-based compression and decompression.

Function Documentation

◆ zxc_compress()

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.

Parameters
[in]srcSource buffer; may be NULL when src_size is 0.
[in]src_sizeSource size in bytes; 0 writes the empty archive (file header + EOF block + footer).
[out]dstDestination buffer.
[in]dst_capacityCapacity of dst.
[in]levelCompression level, 1 to 7 (zxc_compression_level_t); 0 selects ZXC_LEVEL_DEFAULT.
Note
src and dst must not overlap (same contract as memcpy).
Returns
Bytes written to 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.

◆ zxc_compress_bound()

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.

Parameters
[in]input_sizeInput size in bytes.
Returns
Required destination capacity in bytes, or 0 when that capacity would not fit a 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().

◆ zxc_decompress()

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.

Concatenated frames
src may hold several frames back to back; they decode in order into dst. Anything else after a frame is ZXC_ERROR_CORRUPT_DATA.
Asking without a destination
A NULL 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.
Parameters
[in]srcCompressed buffer.
[in]src_sizeCompressed size in bytes.
[out]dstDestination buffer, or NULL to probe (see above).
[in]dst_capacityCapacity of dst.
Note
src and dst must not overlap (same contract as memcpy).
Returns
Bytes written to 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().

◆ zxc_decompress_inplace()

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).

Note
The decode context is heap-allocated for the call, with no checksum verification and no dictionary; for either, or to supply your own context, see zxc_decompress_inplace_dctx().
buffer is both input and output: on success its first N bytes hold the decompressed data (N = the return value).
Parameters
[in,out]bufferWork buffer holding the flush-right archive.
[in]buffer_capacityTotal size of buffer in bytes.
[in]comp_sizeSize of the compressed archive in bytes.
Returns
Decompressed size (> 0), 0 for an empty frame, or a negative zxc_error_t (ZXC_ERROR_DST_TOO_SMALL if the margin is missing).

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.

◆ zxc_decompress_inplace_bound()

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.

Parameters
[in]srcCompressed archive (only header + footer are read).
[in]src_sizeArchive size in bytes.
Returns
Required buffer size in bytes, or 0 if 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.

◆ zxc_frame_info_size()

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.

◆ zxc_get_decompressed_size()

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.

Parameters
[in]srcCompressed buffer.
[in]src_sizeCompressed size in bytes.
Returns
Original uncompressed size in bytes, or 0 if the buffer is invalid, too small, or carries an implausible footer value.

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().

◆ zxc_get_dict_id()

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().

Parameters
[in]srcCompressed buffer.
[in]src_sizeCompressed size in bytes.
Returns
Dictionary ID, or 0 if no dictionary is required, the buffer is too small or the magic word is wrong; the rest of the header is not checked. 0 is also a valid ID: zxc_requires_dict() tells them apart and validates the header.

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.

◆ zxc_get_frame_info()

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.

Parameters
[in]srcCompressed buffer.
[in]src_sizeCompressed size in bytes.
[out]infoFilled on success, untouched otherwise.
[in]info_sizesizeof(*info) as the caller compiled it.
Returns
ZXC_OK, or a negative zxc_error_t (e.g. ZXC_ERROR_BAD_MAGIC, ZXC_ERROR_CORRUPT_DATA).

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.

◆ zxc_get_last_frame_info()

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:

size_t n = src_size;
while (n > 0) {
if (zxc_get_last_frame_info(src, n, &info, sizeof(info)) != ZXC_OK) break;
n -= info.compressed_size; // the frame before ends here
}
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.
@ ZXC_OK
Success (no error).
Definition zxc_error.h:38
Parameters
[in]srcCompressed buffer.
[in]src_sizeBytes of src the frame ends.
[out]infoFilled on success, untouched otherwise.
[in]info_sizesizeof(*info) as the caller compiled it.
Returns
ZXC_OK, or a negative zxc_error_t.

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().

◆ zxc_requires_dict()

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.

Parameters
[in]srcCompressed buffer.
[in]src_sizeCompressed size in bytes.
Returns
1 if a dictionary is required, 0 if not, or a negative zxc_error_t for an invalid header.

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().