ZXC 1.0.3
High-performance asymmetric lossless compression library
Loading...
Searching...
No Matches
zxc_buffer.h
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
37
38#ifndef ZXC_BUFFER_H
39#define ZXC_BUFFER_H
40
41#include <stddef.h>
42#include <stdint.h>
43
44#include "zxc_constants.h"
45#include "zxc_export.h"
46#include "zxc_opts.h"
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
61
66ZXC_EXPORT int zxc_min_level(void);
67
72ZXC_EXPORT int zxc_max_level(void);
73
78ZXC_EXPORT int zxc_default_level(void);
79
84ZXC_EXPORT const char* zxc_version_string(void);
85
93ZXC_EXPORT unsigned zxc_version_number(void);
94
99ZXC_EXPORT const char* zxc_version_codename(void);
100 /* end of library_info */
102
108
119ZXC_EXPORT size_t zxc_compress_bound(const size_t input_size);
120
143ZXC_EXPORT int64_t zxc_compress(const void* src, const size_t src_size, void* dst,
144 const size_t dst_capacity, int level);
145
179ZXC_EXPORT int64_t zxc_decompress(const void* src, const size_t src_size, void* dst,
180 const size_t dst_capacity);
181
194ZXC_EXPORT size_t zxc_decompress_inplace_bound(const void* src, const size_t src_size);
195
221ZXC_EXPORT int64_t zxc_decompress_inplace(void* buffer, const size_t buffer_capacity,
222 const size_t comp_size);
223
237ZXC_EXPORT uint64_t zxc_get_decompressed_size(const void* src, const size_t src_size);
238
252ZXC_EXPORT uint32_t zxc_get_dict_id(const void* src, size_t src_size);
253
266ZXC_EXPORT int zxc_requires_dict(const void* src, size_t src_size);
267
275typedef struct {
278 uint64_t digest;
279 size_t block_size;
280 uint32_t dict_id;
282 uint8_t has_checksum;
285
301ZXC_EXPORT int zxc_get_frame_info(const void* src, size_t src_size, zxc_frame_info_t* info,
302 size_t info_size);
303
324ZXC_EXPORT int zxc_get_last_frame_info(const void* src, size_t src_size, zxc_frame_info_t* info,
325 size_t info_size);
326
334ZXC_EXPORT size_t zxc_frame_info_size(void);
335
336/* ========================================================================= */
337/* Block-Level API (no file framing) */
338/* ========================================================================= */
339
367
368/* Forward declarations for context types (defined below). */
370typedef struct zxc_cctx_s zxc_cctx;
372typedef struct zxc_dctx_s zxc_dctx;
373
385ZXC_EXPORT size_t zxc_compress_block_bound(size_t input_size);
386
402ZXC_EXPORT size_t zxc_decompress_block_bound(const size_t uncompressed_size);
403
426ZXC_EXPORT int64_t zxc_compress_block(zxc_cctx* cctx, const void* src, size_t src_size, void* dst,
427 size_t dst_capacity);
428
460ZXC_EXPORT int64_t zxc_decompress_block(zxc_dctx* dctx, const void* src, size_t src_size, void* dst,
461 size_t dst_capacity);
462
495ZXC_EXPORT int64_t zxc_decompress_block_safe(zxc_dctx* dctx, const void* src, const size_t src_size,
496 void* dst, const size_t dst_capacity);
497
513ZXC_EXPORT size_t zxc_estimate_cctx_size(size_t src_size, int level);
514 /* end of block_api */
516
517/* ========================================================================= */
518/* Reusable Context API (opaque, heap-allocated) */
519/* ========================================================================= */
520
530
531/* --- Compression context ------------------------------------------------- */
532
543ZXC_EXPORT zxc_cctx* zxc_create_cctx(void);
544
550ZXC_EXPORT void zxc_free_cctx(zxc_cctx* cctx);
551
568ZXC_EXPORT int zxc_cctx_set_param(zxc_cctx* cctx, zxc_cparam_t param, int64_t value);
569
579ZXC_EXPORT int zxc_cctx_get_param(const zxc_cctx* cctx, zxc_cparam_t param, int64_t* value);
580
599ZXC_EXPORT int zxc_cctx_set_dict(zxc_cctx* cctx, const void* dict, size_t dict_size,
600 const void* dict_huf);
601
613ZXC_EXPORT int zxc_cctx_set_progress(zxc_cctx* cctx, zxc_progress_callback_t cb, void* user_data);
614
624ZXC_EXPORT int zxc_cctx_reset(zxc_cctx* cctx);
625
649ZXC_EXPORT int64_t zxc_compress_cctx(zxc_cctx* cctx, const void* src, size_t src_size, void* dst,
650 size_t dst_capacity);
651
652/* --- Decompression context ----------------------------------------------- */
653
664ZXC_EXPORT zxc_dctx* zxc_create_dctx(void);
665
671ZXC_EXPORT void zxc_free_dctx(zxc_dctx* dctx);
672
685ZXC_EXPORT int zxc_dctx_set_param(zxc_dctx* dctx, zxc_dparam_t param, int64_t value);
686
691ZXC_EXPORT int zxc_dctx_get_param(const zxc_dctx* dctx, zxc_dparam_t param, int64_t* value);
692
709ZXC_EXPORT int zxc_dctx_set_dict(zxc_dctx* dctx, const void* dict, size_t dict_size,
710 const void* dict_huf);
711
722ZXC_EXPORT int zxc_dctx_set_progress(zxc_dctx* dctx, zxc_progress_callback_t cb, void* user_data);
723
731ZXC_EXPORT int zxc_dctx_reset(zxc_dctx* dctx);
732
756ZXC_EXPORT int64_t zxc_decompress_dctx(zxc_dctx* dctx, const void* src, size_t src_size, void* dst,
757 size_t dst_capacity);
758
784ZXC_EXPORT int64_t zxc_decompress_inplace_dctx(zxc_dctx* dctx, void* buffer,
785 const size_t buffer_capacity,
786 const size_t comp_size);
787
788/* ========================================================================= */
789/* Static Context API (caller-allocated workspace) */
790/* ========================================================================= */
791
821
838ZXC_EXPORT size_t zxc_static_cctx_workspace_size(const size_t block_size, const int level);
839
864ZXC_EXPORT zxc_cctx* zxc_init_static_cctx(void* workspace, const size_t workspace_size,
865 const size_t block_size, const int level);
866
878ZXC_EXPORT size_t zxc_static_dctx_workspace_size(const size_t block_size);
879
904ZXC_EXPORT zxc_dctx* zxc_init_static_dctx(void* workspace, const size_t workspace_size,
905 const size_t block_size);
906 /* end of static_context_api */ /* end of context_api */ /* end of buffer_api */
910
911#ifdef __cplusplus
912}
913#endif
914
915#endif // ZXC_BUFFER_H
int64_t zxc_compress_block(zxc_cctx *cctx, const void *src, size_t src_size, void *dst, size_t dst_capacity)
Compresses a single block without file framing.
struct zxc_cctx_s zxc_cctx
Opaque reusable compression context (see zxc_create_cctx).
Definition zxc_buffer.h:370
int64_t zxc_decompress_block(zxc_dctx *dctx, const void *src, size_t src_size, void *dst, size_t dst_capacity)
Decompresses a single block produced by zxc_compress_block().
int64_t zxc_decompress_block_safe(zxc_dctx *dctx, const void *src, const size_t src_size, void *dst, const size_t dst_capacity)
Decompresses a single block into an exactly-sized destination.
size_t zxc_estimate_cctx_size(size_t src_size, int level)
Estimates peak compression memory for a given block size and level.
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.
size_t zxc_decompress_block_bound(const size_t uncompressed_size)
Minimum dst_capacity zxc_decompress_block() needs for a block of uncompressed_size bytes.
int zxc_requires_dict(const void *src, size_t src_size)
Tells whether an archive requires a dictionary, from its header.
size_t zxc_frame_info_size(void)
Returns sizeof(zxc_frame_info_t) as compiled into the library.
Definition zxc_common.c:31
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.
uint64_t zxc_get_decompressed_size(const void *src, const size_t src_size)
Reads the original size from the archive footers, without decoding.
int64_t zxc_decompress_inplace(void *buffer, const size_t buffer_capacity, const size_t comp_size)
Decompresses inside a single caller-owned buffer.
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.
size_t zxc_compress_bound(const size_t input_size)
Maximum compressed size for an input of input_size bytes.
uint32_t zxc_get_dict_id(const void *src, size_t src_size)
Reads the dictionary ID an archive requires, from its header.
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.
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.
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.
int zxc_dctx_reset(zxc_dctx *dctx)
Restores every parameter to its default, detaches the dictionary and the callback.
int64_t zxc_decompress_dctx(zxc_dctx *dctx, const void *src, size_t src_size, void *dst, size_t dst_capacity)
Decompresses data using a reusable context.
int zxc_dctx_get_param(const zxc_dctx *dctx, zxc_dparam_t param, int64_t *value)
Reads one parameter of dctx.
int zxc_cctx_get_param(const zxc_cctx *cctx, zxc_cparam_t param, int64_t *value)
Reads one parameter of cctx.
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_dctx_set_param(zxc_dctx *dctx, zxc_dparam_t param, int64_t value)
Sets one parameter on dctx; 0 restores its default.
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.
zxc_dctx * zxc_create_dctx(void)
Creates a reusable decompression context.
int zxc_cctx_set_progress(zxc_cctx *cctx, zxc_progress_callback_t cb, void *user_data)
Sets the progress callback; NULL cb disables it.
int zxc_dctx_set_dict(zxc_dctx *dctx, const void *dict, size_t dict_size, const void *dict_huf)
Attaches the dictionary the archives were compressed with; NULL detaches.
int zxc_cctx_reset(zxc_cctx *cctx)
Restores every parameter to its default, detaches the dictionary and the callback.
void zxc_free_dctx(zxc_dctx *dctx)
Frees a decompression context and its resources.
int zxc_dctx_set_progress(zxc_dctx *dctx, zxc_progress_callback_t cb, void *user_data)
Sets the progress callback; NULL cb disables it.
int64_t zxc_decompress_inplace_dctx(zxc_dctx *dctx, void *buffer, const size_t buffer_capacity, const size_t comp_size)
In-place decompression with a caller-supplied context.
const char * zxc_version_string(void)
Library version string, "MAJOR.MINOR.PATCH".
int zxc_default_level(void)
Default compression level.
int zxc_max_level(void)
Maximum supported compression level.
unsigned zxc_version_number(void)
Library version as one integer, ZXC_VERSION_NUMBER of the loaded library.
int zxc_min_level(void)
Minimum supported compression level.
const char * zxc_version_codename(void)
Release code name of the loaded library, ZXC_VERSION_CODENAME.
size_t zxc_static_dctx_workspace_size(const size_t block_size)
Exact size of a static decompression workspace.
size_t zxc_static_cctx_workspace_size(const size_t block_size, const int level)
Exact size of a static compression workspace.
zxc_dctx * zxc_init_static_dctx(void *workspace, const size_t workspace_size, const size_t block_size)
Initialises a decompression context inside a caller-supplied workspace.
zxc_cctx * zxc_init_static_cctx(void *workspace, const size_t workspace_size, const size_t block_size, const int level)
Initialises a compression context inside a caller-supplied workspace.
What a frame's header and footer declare, read without decoding.
Definition zxc_buffer.h:275
uint8_t format_version
Format version of the frame.
Definition zxc_buffer.h:281
uint64_t digest
Archive digest; 0 when has_checksum is 0.
Definition zxc_buffer.h:278
uint8_t has_checksum
1 when blocks carry checksums and the footer a digest.
Definition zxc_buffer.h:282
size_t block_size
Block size, 4 KB to 2 MB.
Definition zxc_buffer.h:279
uint64_t decompressed_size
Source bytes the frame decodes to.
Definition zxc_buffer.h:276
uint32_t dict_id
Dictionary ID; 0 is none or ID 0, see zxc_requires_dict().
Definition zxc_buffer.h:280
uint8_t has_seek_table
1 when a seek table precedes the footer.
Definition zxc_buffer.h:283
uint64_t compressed_size
Compressed bytes of the frame, footer included.
Definition zxc_buffer.h:277
Public constants: library version, block sizes, compression levels.
Platform-specific symbol visibility macros.
Compression and decompression parameters.
zxc_cparam_t
Compression parameters, set on a zxc_cctx with zxc_cctx_set_param().
Definition zxc_opts.h:53
zxc_dparam_t
Decompression parameters, set on a zxc_dctx with zxc_dctx_set_param().
Definition zxc_opts.h:78
void(* zxc_progress_callback_t)(uint64_t bytes_processed, uint64_t bytes_total, const void *user_data)
Progress callback function type.
Definition zxc_opts.h:40