ZXC 0.15.0
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_common.c File Reference

Shared library utilities: context management, header I/O, compress-bound calculation, and error-code name lookup. More...

Go to the source code of this file.

Functions

size_t zxc_compress_opts_size (void)
 Returns sizeof(zxc_compress_opts_t) for ABI-safe allocation.
 
size_t zxc_decompress_opts_size (void)
 Returns sizeof(zxc_decompress_opts_t) for ABI-safe allocation.
 
size_t zxc_frame_info_size (void)
 Returns sizeof(zxc_frame_info_t) for ABI-safe mirroring.
 
static size_t zxc_cctx_max_seq (const size_t chunk_size)
 Worst-case sequence count for one block.
 
static void zxc_dctx_entropy_sizes (const size_t chunk_size, size_t *sz_tok, size_t *sz_pivco)
 Decode-side entropy scratch sizes for one block: token scratch (worst-case sequence count + wild-read pad) and PivCo ping-pong scratch.
 
static zxc_cctx_layout_t compute_cctx_layout (const size_t chunk_size, const int mode, const int level, const size_t dict_size, const int defer_entropy_scratch)
 Computes the single-allocation memory layout for a compression / decompression context.
 
size_t zxc_cctx_compute_workspace_size (const size_t chunk_size, const int mode, const int level, const size_t dict_size)
 Returns the workspace byte count required for the given parameters.
 
int zxc_cctx_init_in_workspace (zxc_cctx_t *ctx, void *workspace, const size_t workspace_size, const size_t chunk_size, const int mode, const int level, const int checksum_enabled, const size_t dict_size, const int defer_entropy_scratch)
 Partitions a caller-supplied workspace into a ready-to-use context.
 
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_MALLOC.
 
int zxc_cctx_alloc_entropy_scratch (zxc_cctx_t *ctx)
 Lazily allocates the deferred decode-side entropy scratch.
 
void zxc_cctx_free (zxc_cctx_t *ctx)
 Releases all resources owned by a compression context.
 
int zxc_cctx_attach_dict_huf (zxc_cctx_t *ctx, const uint8_t *lengths)
 Attach the shared dictionary literal Huffman table to a context.
 
int zxc_write_file_header (uint8_t *dst, const size_t dst_capacity, const size_t chunk_size, const int has_checksum, const uint32_t dict_id, const int has_seek)
 Serialises a ZXC file header into dst.
 
int zxc_read_file_header (const uint8_t *src, const size_t src_size, size_t *out_block_size, int *out_has_checksum, uint32_t *out_dict_id, int *out_has_seek)
 Parses and validates a ZXC file header from src.
 
static int zxc_scan_read (const zxc_scan_src_t *src, const uint64_t off, void *dst, const size_t len)
 Reads a byte range of a scan source, refusing ranges past its end.
 
int zxc_container_next (const zxc_scan_src_t *src, const uint64_t pos)
 Tells whether a frame starts at pos.
 
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)
 Measures the frame ending at end without decoding: its footer's compressed size gives where it starts.
 
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.
 
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_frame.
 
int zxc_read_frame_info (const uint8_t *header, const uint8_t *tail, const size_t tail_len, const uint64_t total, zxc_frame_info_t *info, size_t *footer_len)
 Validates a frame from its header and the end of its bytes.
 
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.
 
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.
 
size_t zxc_seek_table_size (const uint64_t num_blocks)
 Byte size of a seek table holding num_blocks blocks: header, then groups.
 
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.
 
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, and its header checksum.
 
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.
 
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, and advances *anchor past its blocks.
 
int64_t zxc_write_seek_table (uint8_t *dst, const size_t dst_capacity, const uint32_t *comp_sizes, const uint64_t num_blocks)
 Serialises a seek table (a ZXC_BLOCK_SEK block) into dst.
 
static void zxc_store_le_n (uint8_t *dst, uint64_t v, const size_t n)
 Stores the low n bytes of v, little-endian.
 
static uint64_t zxc_load_le_n (const uint8_t *src, const size_t n)
 Loads n little-endian bytes.
 
static void zxc_encode_footer (uint8_t *dst, const zxc_footer_layout_t *l, const uint64_t src_size, const uint64_t digest)
 Encodes the l footer: digest, the two sizes, then L.
 
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 lengths byte.
 
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.
 
int zxc_parse_file_footer (const uint8_t *end, const size_t avail, uint64_t *src_size, uint64_t *compressed_size, size_t *sizes_len)
 Parses the two sizes back from the end of a frame.
 
static void zxc_write_gnr_header (uint8_t *dst, const zxc_gnr_header_t *gh)
 Writes the 12-byte GLO/GHI sub-header shared by both block types.
 
static void zxc_read_gnr_header (const uint8_t *src, zxc_gnr_header_t *gh)
 Reads the 12-byte GLO/GHI sub-header shared by both block types.
 
static size_t zxc_glo_desc_size (const uint8_t enc_lit, const uint8_t enc_tok)
 Size of the GLO section descriptors, implied by the encoding fields.
 
int zxc_write_glo_header_and_desc (uint8_t *dst, const size_t rem, const zxc_gnr_header_t *gh, const uint32_t lit_comp, const uint32_t tok_comp)
 Serialises a GLO block header followed by its section descriptors.
 
int zxc_read_glo_header_and_desc (const uint8_t *src, const size_t len, zxc_gnr_header_t *gh, uint32_t *lit_comp, uint32_t *tok_comp)
 Parses a GLO block header and its section descriptors from src.
 
int zxc_write_ghi_header (uint8_t *dst, const size_t rem, const zxc_gnr_header_t *gh)
 Serialises a GHI block header.
 
int zxc_read_ghi_header (const uint8_t *src, const size_t len, zxc_gnr_header_t *gh)
 Parses a GHI block header from src.
 
uint64_t zxc_compress_bound (const size_t input_size)
 Returns the maximum compressed size for a given input size.
 
uint64_t zxc_compress_block_bound (const size_t input_size)
 Returns the maximum compressed size for a single block (no file framing).
 
uint64_t zxc_decompress_block_bound (const size_t uncompressed_size)
 Returns the minimum dst_capacity required by zxc_decompress_block().
 
uint64_t zxc_estimate_cctx_size (const size_t src_size, const int level)
 Estimates the total buffer bytes allocated inside a cctx for a block.
 
const char * zxc_error_name (const int code)
 Returns a human-readable string for the given error code.
 
int zxc_min_level (void)
 Returns the minimum supported compression level.
 
int zxc_max_level (void)
 Returns the maximum supported compression level.
 
int zxc_default_level (void)
 Returns the default compression level.
 
const char * zxc_version_string (void)
 Returns the human-readable library version string.
 

Detailed Description

Shared library utilities: context management, header I/O, compress-bound calculation, and error-code name lookup.

This translation unit contains the functions shared by both the buffer and streaming APIs. It is linked into every build of libzxc.

Definition in file zxc_common.c.

Function Documentation

◆ compute_cctx_layout()

static zxc_cctx_layout_t compute_cctx_layout ( const size_t  chunk_size,
const int  mode,
const int  level,
const size_t  dict_size,
const int  defer_entropy_scratch 
)
static

Computes the single-allocation memory layout for a compression / decompression context.

Walks the same partition order used by zxc_cctx_init_in_workspace and records each sub-buffer's offset plus the running total, so the sizing query and the partitioning step share one source of truth and can never disagree.

Decompress (mode == 0) reserves work_buf, lit_buffer (both padded for wild-copy overshoot) and the token / PivCo decode scratch buffers. Compress (mode == 1) reserves the LZ match-finder tables (hash positions, tags, chain), the sequence buffers, the match-split histograms, the extras and literal buffers and - only at level >= ZXC_LEVEL_DENSITY - the optimal-parser scratch. A dict_size > 0 appends the [dict | data] concat scratch in both modes.

Every offset is cache-line aligned via ZXC_ALIGN_CL.

Parameters
[in]chunk_sizeBlock size in bytes.
[in]mode1 = compression, 0 = decompression.
[in]levelCompression level (only consulted when mode == 1).
[in]dict_sizeDictionary prefill size; when > 0 the layout includes the [dict | data] concat buffer.
[in]defer_entropy_scratchWhen non-zero, the decode-side token and PivCo scratch are left out of the layout and allocated lazily on the first entropy section.
Returns
Fully populated layout; .total is the required workspace size.

Definition at line 140 of file zxc_common.c.

References ZXC_ALIGN_CL, zxc_cctx_max_seq(), zxc_dctx_entropy_sizes(), ZXC_DECOMPRESS_TAIL_PAD, ZXC_HUF_NUDGE_SCRATCH_SIZE, ZXC_LEVEL_DENSITY, zxc_log2_u32(), ZXC_LZ_HASH_SIZE, ZXC_LZ_WINDOW_SIZE, zxc_opt_dp_sizes(), and ZXC_PAD_SIZE.

Referenced by zxc_cctx_compute_workspace_size(), zxc_cctx_init(), and zxc_cctx_init_in_workspace().

◆ zxc_cctx_alloc_entropy_scratch()

int zxc_cctx_alloc_entropy_scratch ( zxc_cctx_t *  ctx)

Lazily allocates the deferred decode-side entropy scratch.

Lazily allocates the decode-side entropy scratch (tok_buffer + pivco_scratch) for a heap context initialised with deferral.

Public contract at the declaration in zxc_internal.h. One aligned block carries [tok_buffer | pivco_scratch], sized by zxc_dctx_entropy_sizes (the same source the full-provision layout uses), owned by the context and released by zxc_cctx_free.

Definition at line 373 of file zxc_common.c.

References zxc_cctx_t::entropy_block, zxc_cctx_t::pivco_scratch, zxc_cctx_t::pivco_scratch_cap, zxc_cctx_t::tok_buffer, zxc_cctx_t::tok_buffer_cap, zxc_cctx_t::work_buf_cap, ZXC_ALIGN_CL, ZXC_ALIGNED_MALLOC, ZXC_CACHE_LINE_SIZE, zxc_dctx_entropy_sizes(), ZXC_DECOMPRESS_TAIL_PAD, ZXC_ERROR_MEMORY, and ZXC_OK.

◆ zxc_cctx_attach_dict_huf()

int zxc_cctx_attach_dict_huf ( zxc_cctx_t *  ctx,
const uint8_t *  lengths 
)

Attach the shared dictionary literal Huffman table to a context.

Attach the shared dictionary literal table to an initialised context.

Validates the 128-byte packed code-lengths header and builds the dictionary's PivCo tree, canonical codes and code lengths ONCE into the context (tree-at-attach); per-block encode/estimate/decode reuse them. lengths need only be valid during this call (the tree is a copy). A NULL lengths is a no-op.

Parameters
[in,out]ctxInitialised context to attach the table to.
[in]lengths128-byte packed code lengths, or NULL for a no-op.
Returns
ZXC_OK on success, ZXC_ERROR_CORRUPT_DATA if lengths is structurally invalid (bad nibble, Kraft inequality).

Definition at line 453 of file zxc_common.c.

References ZXC_ERROR_NULL_INPUT, zxc_huf_dict_tree_build(), ZXC_HUF_TABLE_SIZE, and ZXC_OK.

Referenced by zxc_compress(), zxc_ctx_sync_dict_huf(), zxc_seek_use_dict(), and zxc_stream_worker().

◆ zxc_cctx_compute_workspace_size()

size_t zxc_cctx_compute_workspace_size ( const size_t  chunk_size,
const int  mode,
const int  level,
const size_t  dict_size 
)

Returns the workspace byte count required for the given parameters.

Returns the byte count that zxc_cctx_init would allocate for the given parameters.

Public contract documented at the declaration in zxc_internal.h. Thin wrapper that returns compute_cctx_layout(...).total, or 0 when chunk_size is 0.

Definition at line 245 of file zxc_common.c.

References compute_cctx_layout().

Referenced by zxc_estimate_cctx_size(), zxc_init_static_cctx(), zxc_init_static_dctx(), zxc_static_cctx_workspace_size(), and zxc_static_dctx_workspace_size().

◆ zxc_cctx_free()

◆ zxc_cctx_init()

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

Initialises a ZXC compression / decompression context in place.

Thin wrapper around zxc_cctx_init_in_workspace(): sizes the buffer via zxc_cctx_compute_workspace_size, allocates it, then partitions it. The pointer is stored in ctx->memory_block so zxc_cctx_free can release it. The static-cctx public API (see zxc_buffer.h) bypasses this wrapper and partitions a caller-supplied workspace directly.

Definition at line 339 of file zxc_common.c.

References compute_cctx_layout(), ZXC_ALIGNED_FREE, ZXC_ALIGNED_MALLOC, ZXC_CACHE_LINE_SIZE, zxc_cctx_init_in_workspace(), ZXC_ERROR_MEMORY, ZXC_ERROR_NULL_INPUT, and ZXC_OK.

Referenced by ds_handle_need_file_header(), zxc_compress(), zxc_compress_block_at(), zxc_compress_cctx(), zxc_create_cctx(), zxc_dctx_carve(), zxc_dctx_prepare(), zxc_seek_mt_worker(), zxc_seekable_decompress_range(), zxc_stream_worker(), and zxc_train_dict_huf().

◆ zxc_cctx_init_in_workspace()

int zxc_cctx_init_in_workspace ( zxc_cctx_t *  ctx,
void *  workspace,
const size_t  workspace_size,
const size_t  chunk_size,
const int  mode,
const int  level,
const int  checksum_enabled,
const size_t  dict_size,
const int  defer_entropy_scratch 
)

Partitions a caller-supplied workspace into a ready-to-use context.

Initialises a compression / decompression context inside a caller-supplied workspace.

Public contract (alignment, lifetime, return codes) documented at the declaration in zxc_internal.h. Computes the layout via compute_cctx_layout, rejects an undersized workspace, then carves the sub-buffers out of it. ctx->memory_block stays NULL so zxc_cctx_free leaves the caller-owned workspace untouched.

Definition at line 260 of file zxc_common.c.

References compute_cctx_layout(), ZXC_DECOMPRESS_TAIL_PAD, ZXC_ERROR_DST_TOO_SMALL, ZXC_ERROR_NULL_INPUT, zxc_log2_u32(), ZXC_OK, and ZXC_PAD_SIZE.

Referenced by zxc_cctx_init(), zxc_init_static_cctx(), and zxc_init_static_dctx().

◆ zxc_cctx_max_seq()

static size_t zxc_cctx_max_seq ( const size_t  chunk_size)
inlinestatic

Worst-case sequence count for one block.

Shared by the compressor's buffer sizing and the decoder's token scratch: the decode side must accept exactly what the compress side can emit, so both derive from this single expression.

Definition at line 94 of file zxc_common.c.

References ZXC_LZ_MIN_MATCH_LEN.

Referenced by compute_cctx_layout(), and zxc_dctx_entropy_sizes().

◆ zxc_check_eof_header()

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, and its header checksum.

Parameters
[in]hdrThe 8 header bytes, already read as an EOF block.
Returns
ZXC_OK or ZXC_ERROR_BAD_HEADER.

Definition at line 748 of file zxc_common.c.

References zxc_block_header_t::block_type, ZXC_BLOCK_HEADER_SIZE, ZXC_ERROR_BAD_HEADER, ZXC_OK, and zxc_write_block_header().

Referenced by ds_handle_need_block_header(), zxc_dctx_decode_frame(), zxc_seek_load_spans(), zxc_seekable_parse(), and zxc_stream_read_blocks().

◆ zxc_check_file_footer()

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.

Checks the footer a sequential reader reaches against l, the one the frame implies, on the avail bytes it has.

Compares the bytes present, digest aside, with the footer it would write: one encoding per frame.

Definition at line 847 of file zxc_common.c.

References zxc_footer_layout_t::len, zxc_footer_layout_t::skip, zxc_encode_footer(), ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_SRC_TOO_SMALL, ZXC_FOOTER_MAX_SIZE_WITH_DIGEST, zxc_le64(), and ZXC_OK.

Referenced by zxc_dctx_decode_frame(), zxc_dstream_decompress(), and zxc_stream_finish_decompress().

◆ zxc_check_seek_header()

int zxc_check_seek_header ( const uint8_t *  hdr,
size_t  avail,
uint64_t  total_out,
size_t  block_size,
uint64_t *  sek_bytes 
)

Checks the SEK block header the file header announced, on the avail bytes present.

A frame admits one header, the one zxc_seek_table_header() writes (reserved bytes zero), compared like the footer. A short footer never matches its start, so a flag set without a table reads as corrupt, not truncated.

Parameters
[in]hdrBytes after the EOF block.
[in]availHow many of them are present.
[in]total_outBytes the frame decoded to.
[in]block_sizeBlock size from the file header.
[out]sek_bytesTable length in 64 bits (the field only holds its fold); may be NULL.
Returns
ZXC_OK, ZXC_ERROR_CORRUPT_DATA or ZXC_ERROR_SRC_TOO_SMALL.

Definition at line 757 of file zxc_common.c.

References ZXC_BLOCK_HEADER_SIZE, ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_SRC_TOO_SMALL, ZXC_OK, zxc_seek_block_count(), zxc_seek_table_bytes(), and zxc_seek_table_header().

Referenced by zxc_dctx_decode_frame(), zxc_dstream_decompress(), zxc_seek_load_spans(), zxc_seekable_parse(), and zxc_stream_finish_decompress().

◆ zxc_compress_opts_size()

size_t zxc_compress_opts_size ( void  )

Returns sizeof(zxc_compress_opts_t) for ABI-safe allocation.

Returns sizeof(zxc_compress_opts_t) as compiled into the library.

Public API; see zxc_buffer.h. Lets callers (other languages, or a different library version) size the options struct without knowing its layout.

Returns
Size of zxc_compress_opts_t in bytes.

Definition at line 33 of file zxc_common.c.

◆ zxc_container_next()

int zxc_container_next ( const zxc_scan_src_t *  src,
uint64_t  pos 
)

Tells whether a frame starts at pos.

A wrong magic word is ZXC_ERROR_BAD_MAGIC at offset 0, as for a single frame, and ZXC_ERROR_CORRUPT_DATA after one.

Parameters
[in]srcContainer source.
[in]posOffset just past the previous frame, 0 for the first.
Returns
1 on a frame, 0 at the end of the input, or a negative zxc_error_t.

Definition at line 558 of file zxc_common.c.

References ZXC_ERROR_BAD_MAGIC, ZXC_ERROR_CORRUPT_DATA, zxc_magic_prefix_ok(), ZXC_OK, and zxc_scan_read().

Referenced by zxc_dctx_decode_container().

◆ zxc_dctx_entropy_sizes()

static void zxc_dctx_entropy_sizes ( const size_t  chunk_size,
size_t *  sz_tok,
size_t *  sz_pivco 
)
static

Decode-side entropy scratch sizes for one block: token scratch (worst-case sequence count + wild-read pad) and PivCo ping-pong scratch.

Single definition shared by the layout (full provisioning: static workspaces) and the lazy heap allocator (zxc_cctx_alloc_entropy_scratch), so the two can never drift.

Definition at line 105 of file zxc_common.c.

References zxc_cctx_max_seq(), ZXC_PAD_SIZE, and ZXC_PIVCO_SCRATCH_PAD.

Referenced by compute_cctx_layout(), and zxc_cctx_alloc_entropy_scratch().

◆ zxc_decompress_opts_size()

size_t zxc_decompress_opts_size ( void  )

Returns sizeof(zxc_decompress_opts_t) for ABI-safe allocation.

Returns sizeof(zxc_decompress_opts_t) as compiled into the library.

Public API; see zxc_buffer.h. Lets callers (other languages, or a different library version) size the options struct without knowing its layout.

Returns
Size of zxc_decompress_opts_t in bytes.

Definition at line 43 of file zxc_common.c.

◆ zxc_encode_footer()

static void zxc_encode_footer ( uint8_t *  dst,
const zxc_footer_layout_t *  l,
const uint64_t  src_size,
const uint64_t  digest 
)
static

◆ zxc_glo_desc_size()

static size_t zxc_glo_desc_size ( const uint8_t  enc_lit,
const uint8_t  enc_tok 
)
inlinestatic

Size of the GLO section descriptors, implied by the encoding fields.

0, 4 or 8 bytes (ZXC_GLO_DESC_MAX_SIZE) - see zxc_write_glo_header_and_desc for what they hold.

Definition at line 926 of file zxc_common.c.

Referenced by zxc_read_glo_header_and_desc(), and zxc_write_glo_header_and_desc().

◆ zxc_load_le_n()

static uint64_t zxc_load_le_n ( const uint8_t *  src,
const size_t  n 
)
static

Loads n little-endian bytes.

Definition at line 811 of file zxc_common.c.

Referenced by zxc_parse_file_footer().

◆ zxc_parse_file_footer()

int zxc_parse_file_footer ( const uint8_t *  end,
const size_t  avail,
uint64_t *  src_size,
uint64_t *  compressed_size,
size_t *  sizes_len 
)

Parses the two sizes back from the end of a frame.

Parses the sizes back from the end of a frame.

Bits 3 and 7 of L are reserved; a length longer than its value needs is refused.

Definition at line 865 of file zxc_common.c.

References ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_SRC_TOO_SMALL, ZXC_FILE_FOOTER_MIN_SIZE, zxc_load_le_n(), ZXC_OK, and zxc_uint_bytes().

Referenced by zxc_read_frame_info(), and zxc_scan_frame().

◆ zxc_read_block_header()

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.

Reads and parses a ZXC block header from src.

Validates the 8-bit checksum embedded in the header.

Definition at line 703 of file zxc_common.c.

References ZXC_BLOCK_HEADER_SIZE, ZXC_ERROR_BAD_HEADER, ZXC_ERROR_SRC_TOO_SMALL, zxc_hash8(), zxc_le32(), and ZXC_OK.

Referenced by ds_handle_need_block_header(), zxc_dctx_decode_frame(), zxc_seek_read_block(), zxc_stream_decode_job(), and zxc_stream_read_blocks().

◆ zxc_read_file_header()

int zxc_read_file_header ( const uint8_t *  src,
const size_t  src_size,
size_t *  out_block_size,
int *  out_has_checksum,
uint32_t *  out_dict_id,
int *  out_has_seek 
)

◆ zxc_read_frame_info()

int zxc_read_frame_info ( const uint8_t *  header,
const uint8_t *  tail,
const size_t  tail_len,
const uint64_t  total,
zxc_frame_info_t *  info,
size_t *  footer_len 
)

Validates a frame from its header and the end of its bytes.

Validates a frame from its header and the end of its bytes: the one check behind zxc_get_frame_info(), its FILE* twin and the size queries.

Header first, as every decoder does; then the footer, whose frame must span total bytes and whose size the blocks could hold.

Definition at line 648 of file zxc_common.c.

References zxc_frame_info_t::block_size, zxc_frame_info_t::compressed_size, zxc_frame_info_t::decompressed_size, zxc_frame_info_t::dict_id, zxc_frame_info_t::digest, zxc_frame_info_t::format_version, zxc_frame_info_t::has_checksum, zxc_frame_info_t::has_seek_table, ZXC_ERROR_CORRUPT_DATA, ZXC_FILE_DIGEST_SIZE, ZXC_FILE_HEADER_SIZE, zxc_footer_dsize_plausible(), zxc_le64(), ZXC_OK, zxc_parse_file_footer(), and zxc_read_file_header().

Referenced by zxc_scan_frame().

◆ zxc_read_ghi_header()

int zxc_read_ghi_header ( const uint8_t *  src,
const size_t  len,
zxc_gnr_header_t *  gh 
)

Parses a GHI block header from src.

Reads a GHI sub-header from a buffer.

Definition at line 1008 of file zxc_common.c.

References ZXC_ERROR_SRC_TOO_SMALL, ZXC_GHI_HEADER_BINARY_SIZE, ZXC_OK, and zxc_read_gnr_header().

◆ zxc_read_glo_header_and_desc()

int zxc_read_glo_header_and_desc ( const uint8_t *  src,
const size_t  len,
zxc_gnr_header_t *  gh,
uint32_t *  lit_comp,
uint32_t *  tok_comp 
)

Parses a GLO block header and its section descriptors from src.

Reads a GLO sub-header and its section descriptors from a source buffer.

Definition at line 965 of file zxc_common.c.

References ZXC_ERROR_SRC_TOO_SMALL, zxc_glo_desc_size(), ZXC_GLO_HEADER_BINARY_SIZE, zxc_le32(), and zxc_read_gnr_header().

◆ zxc_read_gnr_header()

static void zxc_read_gnr_header ( const uint8_t *  src,
zxc_gnr_header_t *  gh 
)
inlinestatic

Reads the 12-byte GLO/GHI sub-header shared by both block types.

Parameters
[in]srcSource buffer, at least 12 bytes.
[out]ghReceives the decoded header.

Definition at line 910 of file zxc_common.c.

References zxc_le32().

Referenced by zxc_read_ghi_header(), and zxc_read_glo_header_and_desc().

◆ zxc_scan_container()

int zxc_scan_container ( const zxc_scan_src_t *  src,
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_frame.

Parameters
[in]srcContainer source.
[in]req_chunkBlock size every frame must declare, 0 for any.
[out]infoTotals over the container.
Returns
ZXC_OK, or a negative zxc_error_t; an empty container is ZXC_ERROR_SRC_TOO_SMALL.

Definition at line 613 of file zxc_common.c.

References zxc_frame_info_t::block_size, zxc_frame_info_t::decompressed_size, zxc_frame_info_t::dict_id, zxc_container_info_t::dsize, zxc_container_info_t::first_dict_id, zxc_container_info_t::frames, zxc_container_info_t::uses_dict, ZXC_ERROR_BAD_BLOCK_SIZE, ZXC_ERROR_CORRUPT_DATA, ZXC_FILE_HEADER_SIZE, ZXC_OK, zxc_read_file_header(), zxc_scan_frame(), and zxc_scan_read().

Referenced by zxc_dctx_probe(), zxc_get_decompressed_size(), zxc_probe_without_dst(), and zxc_stream_get_decompressed_size().

◆ zxc_scan_frame()

int zxc_scan_frame ( const zxc_scan_src_t *  src,
uint64_t  end,
uint64_t *  start,
zxc_frame_info_t *  info,
size_t *  footer_len 
)

Measures the frame ending at end without decoding: its footer's compressed size gives where it starts.

Parameters
[in]srcContainer source.
[in]endOffset just past the frame.
[out]startOffset of the frame's header.
[out]infoIts header and footer, checked like zxc_get_frame_info().
[out]footer_lenFooter bytes, digest included; may be NULL.
Returns
ZXC_OK, or a negative zxc_error_t.

Definition at line 569 of file zxc_common.c.

References ZXC_ERROR_BAD_MAGIC, ZXC_ERROR_CORRUPT_DATA, ZXC_FILE_HEADER_SIZE, ZXC_FOOTER_MAX_SIZE_WITH_DIGEST, ZXC_FRAME_MIN_SIZE, zxc_frame_tail_len(), ZXC_OK, zxc_parse_file_footer(), zxc_read_frame_info(), zxc_scan_read(), and zxc_short_input_verdict().

Referenced by zxc_get_last_frame_info(), zxc_scan_container(), zxc_scan_whole_frame(), zxc_seek_parse_frame(), and zxc_stream_get_last_frame_info().

◆ zxc_scan_read()

static int zxc_scan_read ( const zxc_scan_src_t *  src,
const uint64_t  off,
void *  dst,
const size_t  len 
)
static

Reads a byte range of a scan source, refusing ranges past its end.

Definition at line 549 of file zxc_common.c.

References ZXC_ERROR_SRC_TOO_SMALL, and ZXC_OK.

Referenced by zxc_container_next(), zxc_scan_container(), zxc_scan_frame(), and zxc_scan_whole_frame().

◆ zxc_scan_whole_frame()

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.

The header speaks first, then zxc_scan_frame; a frame starting past offset 0 is ZXC_ERROR_CORRUPT_DATA.

Definition at line 601 of file zxc_common.c.

References ZXC_ERROR_CORRUPT_DATA, ZXC_ERROR_SRC_TOO_SMALL, ZXC_FILE_HEADER_SIZE, ZXC_FRAME_MIN_SIZE, ZXC_OK, zxc_scan_frame(), zxc_scan_read(), and zxc_short_input_verdict().

Referenced by zxc_buffer_frame_info(), and zxc_stream_read_frame_info().

◆ zxc_seek_table_header()

int zxc_seek_table_header ( uint8_t *  dst,
size_t  dst_capacity,
uint64_t  num_blocks 
)

Writes a seek table's block header for num_blocks entries.

Shared by the writers and by the readers, which compare against it.

Parameters
[out]dstDestination, at least ZXC_BLOCK_HEADER_SIZE bytes.
[in]dst_capacitySize of dst.
[in]num_blocksBlocks the table indexes.
Returns
ZXC_BLOCK_HEADER_SIZE, or a negative zxc_error_t.

Definition at line 738 of file zxc_common.c.

References zxc_block_header_t::block_type, zxc_seek_size_field(), zxc_seek_table_bytes(), and zxc_write_block_header().

Referenced by zxc_check_seek_header(), zxc_stream_finish_compress(), zxc_write_empty_frame(), zxc_write_seek_table(), and zxc_write_seek_table_from_blocks().

◆ zxc_seek_write_group()

size_t zxc_seek_write_group ( uint8_t *  dst,
uint64_t *  anchor,
const uint32_t *  sizes,
uint32_t  cnt 
)

Writes one group (*anchor, then cnt sizes) into dst, which holds ZXC_SEEK_GROUP_BYTES, and advances *anchor past its blocks.

Returns
Bytes written.

Definition at line 770 of file zxc_common.c.

References ZXC_SEEK_ANCHOR_SIZE, ZXC_SEEK_SIZE_ENTRY, zxc_store_le32(), and zxc_store_le64().

Referenced by zxc_stream_finish_compress(), and zxc_write_seek_table().

◆ zxc_store_le_n()

static void zxc_store_le_n ( uint8_t *  dst,
uint64_t  v,
const size_t  n 
)
static

Stores the low n bytes of v, little-endian.

Definition at line 806 of file zxc_common.c.

Referenced by zxc_encode_footer().

◆ zxc_write_block_header()

int zxc_write_block_header ( uint8_t *  dst,
const size_t  dst_capacity,
const zxc_block_header_t *  bh 
)

◆ zxc_write_file_footer()

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 lengths byte.

Writes the footer of a frame whose first prefix bytes precede it.

Definition at line 832 of file zxc_common.c.

References zxc_footer_layout_t::len, zxc_encode_footer(), ZXC_ERROR_DST_TOO_SMALL, and zxc_footer_layout().

Referenced by cs_stage_footer(), zxc_compress(), zxc_compress_cctx(), zxc_stream_finish_compress(), and zxc_write_empty_frame().

◆ zxc_write_file_header()

int zxc_write_file_header ( uint8_t *  dst,
const size_t  dst_capacity,
const size_t  chunk_size,
const int  has_checksum,
const uint32_t  dict_id,
const int  has_seek 
)

Serialises a ZXC file header into dst.

Writes the standard ZXC file header into dst.

Layout (16 bytes): Magic (4) | Version (1) | Chunk (1) | Flags (1) | Reserved (7) | Checksum-16 (2).

Definition at line 486 of file zxc_common.c.

References ZXC_CHECKSUM_RAPIDHASH, ZXC_ERROR_DST_TOO_SMALL, ZXC_FILE_FLAG_HAS_CHECKSUM, ZXC_FILE_FLAG_HAS_DICTIONARY, ZXC_FILE_FLAG_HAS_SEEK_TABLE, ZXC_FILE_FORMAT_VERSION, zxc_file_header_sign(), ZXC_FILE_HEADER_SIZE, zxc_log2_u32(), ZXC_MAGIC_WORD, and zxc_store_le32().

Referenced by cs_stage_file_header(), zxc_compress(), zxc_compress_cctx(), zxc_stream_compress(), and zxc_write_empty_frame().

◆ zxc_write_ghi_header()

int zxc_write_ghi_header ( uint8_t *  dst,
const size_t  rem,
const zxc_gnr_header_t *  gh 
)

Serialises a GHI block header.

Writes a GHI sub-header.

GHI carries no section descriptors at all: its literals are always RAW (lit_comp == gh->n_literals), its sequence stream is gh->n_sequences * 4 bytes wide, and its extras run from there to the payload end.

Definition at line 997 of file zxc_common.c.

References ZXC_ERROR_DST_TOO_SMALL, ZXC_GHI_HEADER_BINARY_SIZE, and zxc_write_gnr_header().

Referenced by zxc_encode_block_ghi().

◆ zxc_write_glo_header_and_desc()

int zxc_write_glo_header_and_desc ( uint8_t *  dst,
const size_t  rem,
const zxc_gnr_header_t *  gh,
const uint32_t  lit_comp,
const uint32_t  tok_comp 
)

Serialises a GLO block header followed by its section descriptors.

Writes a GLO sub-header followed by its section descriptors.

Only the two sizes the header cannot imply are stored: the literal section's compressed size when it is RLE- or entropy-coded, and the token section's when level 7 Huffman-codes it. The decoder derives the rest - literals raw size from n_literals, offsets from n_sequences and enc_off, extras from the payload residue - so those cannot be forged inconsistently.

Definition at line 940 of file zxc_common.c.

References ZXC_ERROR_DST_TOO_SMALL, zxc_glo_desc_size(), ZXC_GLO_HEADER_BINARY_SIZE, zxc_store_le32(), and zxc_write_gnr_header().

Referenced by zxc_encode_block_glo().

◆ zxc_write_gnr_header()

static void zxc_write_gnr_header ( uint8_t *  dst,
const zxc_gnr_header_t *  gh 
)
inlinestatic

Writes the 12-byte GLO/GHI sub-header shared by both block types.

Parameters
[out]dstDestination buffer, at least 12 bytes.
[in]ghPopulated header descriptor.

Definition at line 893 of file zxc_common.c.

References zxc_store_le32().

Referenced by zxc_write_ghi_header(), and zxc_write_glo_header_and_desc().