ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_dict.h File Reference

Pre-trained dictionary API for ZXC compression. More...

#include <stddef.h>
#include <stdint.h>
#include "zxc_export.h"

Go to the source code of this file.

Functions

uint32_t zxc_dict_compute_id (const void *dict, size_t dict_size, const void *huf_lengths)
 Computes the dictionary ID from content, and optionally its table.
int zxc_dict_load (const void *buf, size_t buf_size, const void **content_out, size_t *content_size_out, const void **huf_out, uint32_t *dict_id_out)
 Loads and validates a .zxd dictionary file from a memory buffer.
int64_t zxc_dict_save (const void *content, size_t content_size, const void *huf_lengths, void *buf, size_t buf_capacity)
 Serializes dictionary content and its shared Huffman table to .zxd.
size_t zxc_dict_save_bound (size_t content_size)
 Returns the .zxd file size for a given content size.
uint32_t zxc_dict_read_id (const void *buf, size_t buf_size)
 Reads the dictionary ID stored in a .zxd file buffer.
int64_t zxc_dict_train_content (const void *const *samples, const size_t *sample_sizes, size_t n_samples, void *dict_buf, size_t dict_capacity)
 Trains a dictionary from a corpus of samples.
int zxc_dict_train_huf (const void *const *samples, const size_t *sample_sizes, size_t n_samples, const void *dict, size_t dict_size, uint8_t *huf_lengths_out)
 Trains the shared literal Huffman table for an already-trained dictionary.
int64_t zxc_dict_train (const void *const *samples, const size_t *sample_sizes, size_t n_samples, void *zxd_buf, size_t zxd_capacity)
 One-call dictionary creation: content + shared table, serialized to ready-to-write .zxd bytes.
const void * zxc_dict_huf (const void *buf, size_t buf_size)
 Returns a pointer to the shared Huffman table inside a .zxd buffer.

Detailed Description

Pre-trained dictionary API for ZXC compression.

Train, save, load and identify dictionaries, which buy back compression ratio on small, similar payloads. A dictionary is raw byte content that prefills the LZ77 window at the start of every block, so the compressor starts out already knowing the patterns instead of waiting for them to show up in the input. Dictionaries live in .zxd files and are referenced by a 32-bit ID in the ZXC file header.

// Train a dictionary from a corpus of JSON samples
void* dict_buf = malloc(32768);
int64_t dict_sz = zxc_dict_train_content(samples, sizes, n, dict_buf, 32768);
// Train the shared literal Huffman table on the same corpus
uint8_t huf[ZXC_HUF_TABLE_SIZE];
zxc_dict_train_huf(samples, sizes, n, dict_buf, dict_sz, huf);
// Save to .zxd file (content + table)
void* zxd = malloc(zxc_dict_save_bound(dict_sz));
int64_t zxd_sz = zxc_dict_save(dict_buf, dict_sz, huf, zxd, ...);
// Use for compression
zxc_cctx_set_dict(cctx, dict_buf, dict_sz, huf);
zxc_compress_cctx(cctx, src, src_size, dst, dst_capacity);
struct zxc_cctx_s zxc_cctx
Opaque reusable compression context (see zxc_create_cctx).
Definition zxc_buffer.h:370
int zxc_cctx_set_dict(zxc_cctx *cctx, const void *dict, size_t dict_size, const void *dict_huf)
Attaches a pre-trained dictionary to cctx; a NULL dict detaches.
void zxc_free_cctx(zxc_cctx *cctx)
Frees a compression context and its resources.
int64_t zxc_compress_cctx(zxc_cctx *cctx, const void *src, size_t src_size, void *dst, size_t dst_capacity)
Compresses data using a reusable context.
int zxc_cctx_set_param(zxc_cctx *cctx, zxc_cparam_t param, int64_t value)
Sets one parameter on cctx; 0 restores its default.
zxc_cctx * zxc_create_cctx(void)
Creates a reusable compression context, every parameter at its default.
size_t zxc_dict_save_bound(size_t content_size)
Returns the .zxd file size for a given content size.
Definition zxc_dict.c:69
int zxc_dict_train_huf(const void *const *samples, const size_t *sample_sizes, size_t n_samples, const void *dict, size_t dict_size, uint8_t *huf_lengths_out)
Trains the shared literal Huffman table for an already-trained dictionary.
Definition zxc_dict.c:458
int64_t zxc_dict_train_content(const void *const *samples, const size_t *sample_sizes, size_t n_samples, void *dict_buf, size_t dict_capacity)
Trains a dictionary from a corpus of samples.
Definition zxc_dict.c:283
int64_t zxc_dict_save(const void *content, size_t content_size, const void *huf_lengths, void *buf, size_t buf_capacity)
Serializes dictionary content and its shared Huffman table to .zxd.
Definition zxc_dict.c:79
#define ZXC_HUF_TABLE_SIZE
Size of a packed literal Huffman code-lengths table (256 symbols, 4 bits each): the shared table in a...
@ ZXC_C_LEVEL
Compression level, 1 to 7 (zxc_compression_level_t); default ZXC_LEVEL_DEFAULT.
Definition zxc_opts.h:56

Definition in file zxc_dict.h.