10c16b537SWarner Losh /* 20c16b537SWarner Losh * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh #if defined (__cplusplus) 110c16b537SWarner Losh extern "C" { 120c16b537SWarner Losh #endif 130c16b537SWarner Losh 140c16b537SWarner Losh #ifndef ZSTD_H_235446 150c16b537SWarner Losh #define ZSTD_H_235446 160c16b537SWarner Losh 170c16b537SWarner Losh /* ====== Dependency ======*/ 180c16b537SWarner Losh #include <stddef.h> /* size_t */ 190c16b537SWarner Losh 200c16b537SWarner Losh 210c16b537SWarner Losh /* ===== ZSTDLIB_API : control library symbols visibility ===== */ 220c16b537SWarner Losh #ifndef ZSTDLIB_VISIBILITY 230c16b537SWarner Losh # if defined(__GNUC__) && (__GNUC__ >= 4) 240c16b537SWarner Losh # define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default"))) 250c16b537SWarner Losh # else 260c16b537SWarner Losh # define ZSTDLIB_VISIBILITY 270c16b537SWarner Losh # endif 280c16b537SWarner Losh #endif 290c16b537SWarner Losh #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) 300c16b537SWarner Losh # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY 310c16b537SWarner Losh #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) 320c16b537SWarner Losh # define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ 330c16b537SWarner Losh #else 340c16b537SWarner Losh # define ZSTDLIB_API ZSTDLIB_VISIBILITY 350c16b537SWarner Losh #endif 360c16b537SWarner Losh 370c16b537SWarner Losh 380c16b537SWarner Losh /******************************************************************************************************* 390c16b537SWarner Losh Introduction 400c16b537SWarner Losh 410c16b537SWarner Losh zstd, short for Zstandard, is a fast lossless compression algorithm, 420c16b537SWarner Losh targeting real-time compression scenarios at zlib-level and better compression ratios. 430c16b537SWarner Losh The zstd compression library provides in-memory compression and decompression functions. 440c16b537SWarner Losh The library supports compression levels from 1 up to ZSTD_maxCLevel() which is currently 22. 450c16b537SWarner Losh Levels >= 20, labeled `--ultra`, should be used with caution, as they require more memory. 460c16b537SWarner Losh Compression can be done in: 470c16b537SWarner Losh - a single step (described as Simple API) 480c16b537SWarner Losh - a single step, reusing a context (described as Explicit memory management) 490c16b537SWarner Losh - unbounded multiple steps (described as Streaming compression) 500c16b537SWarner Losh The compression ratio achievable on small data can be highly improved using a dictionary in: 510c16b537SWarner Losh - a single step (described as Simple dictionary API) 520c16b537SWarner Losh - a single step, reusing a dictionary (described as Fast dictionary API) 530c16b537SWarner Losh 540c16b537SWarner Losh Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h. 550c16b537SWarner Losh Advanced experimental APIs shall never be used with a dynamic library. 560c16b537SWarner Losh They are not "stable", their definition may change in the future. Only static linking is allowed. 570c16b537SWarner Losh *********************************************************************************************************/ 580c16b537SWarner Losh 590c16b537SWarner Losh /*------ Version ------*/ 600c16b537SWarner Losh #define ZSTD_VERSION_MAJOR 1 610c16b537SWarner Losh #define ZSTD_VERSION_MINOR 3 62*052d3c12SConrad Meyer #define ZSTD_VERSION_RELEASE 3 630c16b537SWarner Losh 640c16b537SWarner Losh #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) 650c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< useful to check dll version */ 660c16b537SWarner Losh 670c16b537SWarner Losh #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE 680c16b537SWarner Losh #define ZSTD_QUOTE(str) #str 690c16b537SWarner Losh #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) 700c16b537SWarner Losh #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) 710c16b537SWarner Losh ZSTDLIB_API const char* ZSTD_versionString(void); /* v1.3.0 */ 720c16b537SWarner Losh 730c16b537SWarner Losh 740c16b537SWarner Losh /*************************************** 750c16b537SWarner Losh * Simple API 760c16b537SWarner Losh ***************************************/ 770c16b537SWarner Losh /*! ZSTD_compress() : 780c16b537SWarner Losh * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. 790c16b537SWarner Losh * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 800c16b537SWarner Losh * @return : compressed size written into `dst` (<= `dstCapacity), 810c16b537SWarner Losh * or an error code if it fails (which can be tested using ZSTD_isError()). */ 820c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, 830c16b537SWarner Losh const void* src, size_t srcSize, 840c16b537SWarner Losh int compressionLevel); 850c16b537SWarner Losh 860c16b537SWarner Losh /*! ZSTD_decompress() : 870c16b537SWarner Losh * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. 880c16b537SWarner Losh * `dstCapacity` is an upper bound of originalSize to regenerate. 890c16b537SWarner Losh * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. 900c16b537SWarner Losh * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 910c16b537SWarner Losh * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ 920c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, 930c16b537SWarner Losh const void* src, size_t compressedSize); 940c16b537SWarner Losh 950c16b537SWarner Losh /*! ZSTD_getFrameContentSize() : v1.3.0 960c16b537SWarner Losh * `src` should point to the start of a ZSTD encoded frame. 970c16b537SWarner Losh * `srcSize` must be at least as large as the frame header. 980c16b537SWarner Losh * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough. 990c16b537SWarner Losh * @return : - decompressed size of the frame in `src`, if known 1000c16b537SWarner Losh * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined 1010c16b537SWarner Losh * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) 1020c16b537SWarner Losh * note 1 : a 0 return value means the frame is valid but "empty". 1030c16b537SWarner Losh * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode. 1040c16b537SWarner Losh * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 1050c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 1060c16b537SWarner Losh * Optionally, application can rely on some implicit limit, 1070c16b537SWarner Losh * as ZSTD_decompress() only needs an upper bound of decompressed size. 1080c16b537SWarner Losh * (For example, data could be necessarily cut into blocks <= 16 KB). 1090c16b537SWarner Losh * note 3 : decompressed size is always present when compression is done with ZSTD_compress() 1100c16b537SWarner Losh * note 4 : decompressed size can be very large (64-bits value), 1110c16b537SWarner Losh * potentially larger than what local system can handle as a single memory segment. 1120c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 1130c16b537SWarner Losh * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified. 1140c16b537SWarner Losh * Always ensure return value fits within application's authorized limits. 1150c16b537SWarner Losh * Each application can set its own limits. 1160c16b537SWarner Losh * note 6 : This function replaces ZSTD_getDecompressedSize() */ 1170c16b537SWarner Losh #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) 1180c16b537SWarner Losh #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) 1190c16b537SWarner Losh ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); 1200c16b537SWarner Losh 1210c16b537SWarner Losh /*! ZSTD_getDecompressedSize() : 1220c16b537SWarner Losh * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize(). 1230c16b537SWarner Losh * Both functions work the same way, 1240c16b537SWarner Losh * but ZSTD_getDecompressedSize() blends 1250c16b537SWarner Losh * "empty", "unknown" and "error" results in the same return value (0), 1260c16b537SWarner Losh * while ZSTD_getFrameContentSize() distinguishes them. 1270c16b537SWarner Losh * 1280c16b537SWarner Losh * 'src' is the start of a zstd compressed frame. 1290c16b537SWarner Losh * @return : content size to be decompressed, as a 64-bits value _if known and not empty_, 0 otherwise. */ 1300c16b537SWarner Losh ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); 1310c16b537SWarner Losh 1320c16b537SWarner Losh 1330c16b537SWarner Losh /*====== Helper functions ======*/ 134*052d3c12SConrad Meyer #define ZSTD_COMPRESSBOUND(srcSize) ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0)) /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */ 1350c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case scenario */ 1360c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 1370c16b537SWarner Losh ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ 1380c16b537SWarner Losh ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ 1390c16b537SWarner Losh 1400c16b537SWarner Losh 1410c16b537SWarner Losh /*************************************** 1420c16b537SWarner Losh * Explicit memory management 1430c16b537SWarner Losh ***************************************/ 1440c16b537SWarner Losh /*= Compression context 1450c16b537SWarner Losh * When compressing many times, 1460c16b537SWarner Losh * it is recommended to allocate a context just once, and re-use it for each successive compression operation. 1470c16b537SWarner Losh * This will make workload friendlier for system's memory. 1480c16b537SWarner Losh * Use one context per thread for parallel execution in multi-threaded environments. */ 1490c16b537SWarner Losh typedef struct ZSTD_CCtx_s ZSTD_CCtx; 1500c16b537SWarner Losh ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); 1510c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); 1520c16b537SWarner Losh 1530c16b537SWarner Losh /*! ZSTD_compressCCtx() : 1540c16b537SWarner Losh * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */ 1550c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, 1560c16b537SWarner Losh void* dst, size_t dstCapacity, 1570c16b537SWarner Losh const void* src, size_t srcSize, 1580c16b537SWarner Losh int compressionLevel); 1590c16b537SWarner Losh 1600c16b537SWarner Losh /*= Decompression context 1610c16b537SWarner Losh * When decompressing many times, 1620c16b537SWarner Losh * it is recommended to allocate a context only once, 1630c16b537SWarner Losh * and re-use it for each successive compression operation. 1640c16b537SWarner Losh * This will make workload friendlier for system's memory. 1650c16b537SWarner Losh * Use one context per thread for parallel execution. */ 1660c16b537SWarner Losh typedef struct ZSTD_DCtx_s ZSTD_DCtx; 1670c16b537SWarner Losh ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); 1680c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); 1690c16b537SWarner Losh 1700c16b537SWarner Losh /*! ZSTD_decompressDCtx() : 1710c16b537SWarner Losh * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) */ 1720c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, 1730c16b537SWarner Losh void* dst, size_t dstCapacity, 1740c16b537SWarner Losh const void* src, size_t srcSize); 1750c16b537SWarner Losh 1760c16b537SWarner Losh 1770c16b537SWarner Losh /************************** 1780c16b537SWarner Losh * Simple dictionary API 1790c16b537SWarner Losh ***************************/ 1800c16b537SWarner Losh /*! ZSTD_compress_usingDict() : 1810c16b537SWarner Losh * Compression using a predefined Dictionary (see dictBuilder/zdict.h). 1820c16b537SWarner Losh * Note : This function loads the dictionary, resulting in significant startup delay. 1830c16b537SWarner Losh * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ 1840c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, 1850c16b537SWarner Losh void* dst, size_t dstCapacity, 1860c16b537SWarner Losh const void* src, size_t srcSize, 1870c16b537SWarner Losh const void* dict,size_t dictSize, 1880c16b537SWarner Losh int compressionLevel); 1890c16b537SWarner Losh 1900c16b537SWarner Losh /*! ZSTD_decompress_usingDict() : 1910c16b537SWarner Losh * Decompression using a predefined Dictionary (see dictBuilder/zdict.h). 1920c16b537SWarner Losh * Dictionary must be identical to the one used during compression. 1930c16b537SWarner Losh * Note : This function loads the dictionary, resulting in significant startup delay. 1940c16b537SWarner Losh * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ 1950c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 1960c16b537SWarner Losh void* dst, size_t dstCapacity, 1970c16b537SWarner Losh const void* src, size_t srcSize, 1980c16b537SWarner Losh const void* dict,size_t dictSize); 1990c16b537SWarner Losh 2000c16b537SWarner Losh 2010c16b537SWarner Losh /********************************** 2020c16b537SWarner Losh * Bulk processing dictionary API 2030c16b537SWarner Losh *********************************/ 2040c16b537SWarner Losh typedef struct ZSTD_CDict_s ZSTD_CDict; 2050c16b537SWarner Losh 2060c16b537SWarner Losh /*! ZSTD_createCDict() : 2070c16b537SWarner Losh * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. 2080c16b537SWarner Losh * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. 2090c16b537SWarner Losh * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 2100c16b537SWarner Losh * `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict */ 2110c16b537SWarner Losh ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, 2120c16b537SWarner Losh int compressionLevel); 2130c16b537SWarner Losh 2140c16b537SWarner Losh /*! ZSTD_freeCDict() : 2150c16b537SWarner Losh * Function frees memory allocated by ZSTD_createCDict(). */ 2160c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); 2170c16b537SWarner Losh 2180c16b537SWarner Losh /*! ZSTD_compress_usingCDict() : 2190c16b537SWarner Losh * Compression using a digested Dictionary. 2200c16b537SWarner Losh * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. 2210c16b537SWarner Losh * Note that compression level is decided during dictionary creation. 2220c16b537SWarner Losh * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ 2230c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, 2240c16b537SWarner Losh void* dst, size_t dstCapacity, 2250c16b537SWarner Losh const void* src, size_t srcSize, 2260c16b537SWarner Losh const ZSTD_CDict* cdict); 2270c16b537SWarner Losh 2280c16b537SWarner Losh 2290c16b537SWarner Losh typedef struct ZSTD_DDict_s ZSTD_DDict; 2300c16b537SWarner Losh 2310c16b537SWarner Losh /*! ZSTD_createDDict() : 2320c16b537SWarner Losh * Create a digested dictionary, ready to start decompression operation without startup delay. 2330c16b537SWarner Losh * dictBuffer can be released after DDict creation, as its content is copied inside DDict */ 2340c16b537SWarner Losh ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); 2350c16b537SWarner Losh 2360c16b537SWarner Losh /*! ZSTD_freeDDict() : 2370c16b537SWarner Losh * Function frees memory allocated with ZSTD_createDDict() */ 2380c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); 2390c16b537SWarner Losh 2400c16b537SWarner Losh /*! ZSTD_decompress_usingDDict() : 2410c16b537SWarner Losh * Decompression using a digested Dictionary. 2420c16b537SWarner Losh * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */ 2430c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 2440c16b537SWarner Losh void* dst, size_t dstCapacity, 2450c16b537SWarner Losh const void* src, size_t srcSize, 2460c16b537SWarner Losh const ZSTD_DDict* ddict); 2470c16b537SWarner Losh 2480c16b537SWarner Losh 2490c16b537SWarner Losh /**************************** 2500c16b537SWarner Losh * Streaming 2510c16b537SWarner Losh ****************************/ 2520c16b537SWarner Losh 2530c16b537SWarner Losh typedef struct ZSTD_inBuffer_s { 2540c16b537SWarner Losh const void* src; /**< start of input buffer */ 2550c16b537SWarner Losh size_t size; /**< size of input buffer */ 2560c16b537SWarner Losh size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ 2570c16b537SWarner Losh } ZSTD_inBuffer; 2580c16b537SWarner Losh 2590c16b537SWarner Losh typedef struct ZSTD_outBuffer_s { 2600c16b537SWarner Losh void* dst; /**< start of output buffer */ 2610c16b537SWarner Losh size_t size; /**< size of output buffer */ 2620c16b537SWarner Losh size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ 2630c16b537SWarner Losh } ZSTD_outBuffer; 2640c16b537SWarner Losh 2650c16b537SWarner Losh 2660c16b537SWarner Losh 2670c16b537SWarner Losh /*-*********************************************************************** 2680c16b537SWarner Losh * Streaming compression - HowTo 2690c16b537SWarner Losh * 2700c16b537SWarner Losh * A ZSTD_CStream object is required to track streaming operation. 2710c16b537SWarner Losh * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. 2720c16b537SWarner Losh * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. 2730c16b537SWarner Losh * It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively, 2740c16b537SWarner Losh * since it will play nicer with system's memory, by re-using already allocated memory. 2750c16b537SWarner Losh * Use one separate ZSTD_CStream per thread for parallel execution. 2760c16b537SWarner Losh * 2770c16b537SWarner Losh * Start a new compression by initializing ZSTD_CStream. 2780c16b537SWarner Losh * Use ZSTD_initCStream() to start a new compression operation. 2790c16b537SWarner Losh * Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section) 2800c16b537SWarner Losh * 2810c16b537SWarner Losh * Use ZSTD_compressStream() repetitively to consume input stream. 2820c16b537SWarner Losh * The function will automatically update both `pos` fields. 2830c16b537SWarner Losh * Note that it may not consume the entire input, in which case `pos < size`, 2840c16b537SWarner Losh * and it's up to the caller to present again remaining data. 2850c16b537SWarner Losh * @return : a size hint, preferred nb of bytes to use as input for next function call 2860c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 2870c16b537SWarner Losh * Note 1 : it's just a hint, to help latency a little, any other value will work fine. 2880c16b537SWarner Losh * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize() 2890c16b537SWarner Losh * 2900c16b537SWarner Losh * At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream(). 2910c16b537SWarner Losh * `output->pos` will be updated. 2920c16b537SWarner Losh * Note that some content might still be left within internal buffer if `output->size` is too small. 2930c16b537SWarner Losh * @return : nb of bytes still present within internal buffer (0 if it's empty) 2940c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 2950c16b537SWarner Losh * 2960c16b537SWarner Losh * ZSTD_endStream() instructs to finish a frame. 2970c16b537SWarner Losh * It will perform a flush and write frame epilogue. 2980c16b537SWarner Losh * The epilogue is required for decoders to consider a frame completed. 2990c16b537SWarner Losh * ZSTD_endStream() may not be able to flush full data if `output->size` is too small. 3000c16b537SWarner Losh * In which case, call again ZSTD_endStream() to complete the flush. 3010c16b537SWarner Losh * @return : 0 if frame fully completed and fully flushed, 3020c16b537SWarner Losh or >0 if some data is still present within internal buffer 3030c16b537SWarner Losh (value is minimum size estimation for remaining data to flush, but it could be more) 3040c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 3050c16b537SWarner Losh * 3060c16b537SWarner Losh * *******************************************************************/ 3070c16b537SWarner Losh 3080c16b537SWarner Losh typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */ 3090c16b537SWarner Losh /* Continue to distinguish them for compatibility with versions <= v1.2.0 */ 3100c16b537SWarner Losh /*===== ZSTD_CStream management functions =====*/ 3110c16b537SWarner Losh ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); 3120c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); 3130c16b537SWarner Losh 3140c16b537SWarner Losh /*===== Streaming compression functions =====*/ 3150c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); 3160c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 3170c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 3180c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 3190c16b537SWarner Losh 3200c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ 3210c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */ 3220c16b537SWarner Losh 3230c16b537SWarner Losh 3240c16b537SWarner Losh 3250c16b537SWarner Losh /*-*************************************************************************** 3260c16b537SWarner Losh * Streaming decompression - HowTo 3270c16b537SWarner Losh * 3280c16b537SWarner Losh * A ZSTD_DStream object is required to track streaming operations. 3290c16b537SWarner Losh * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. 3300c16b537SWarner Losh * ZSTD_DStream objects can be re-used multiple times. 3310c16b537SWarner Losh * 3320c16b537SWarner Losh * Use ZSTD_initDStream() to start a new decompression operation, 3330c16b537SWarner Losh * or ZSTD_initDStream_usingDict() if decompression requires a dictionary. 3340c16b537SWarner Losh * @return : recommended first input size 3350c16b537SWarner Losh * 3360c16b537SWarner Losh * Use ZSTD_decompressStream() repetitively to consume your input. 3370c16b537SWarner Losh * The function will update both `pos` fields. 3380c16b537SWarner Losh * If `input.pos < input.size`, some input has not been consumed. 3390c16b537SWarner Losh * It's up to the caller to present again remaining data. 3400c16b537SWarner Losh * If `output.pos < output.size`, decoder has flushed everything it could. 3410c16b537SWarner Losh * @return : 0 when a frame is completely decoded and fully flushed, 3420c16b537SWarner Losh * an error code, which can be tested using ZSTD_isError(), 3430c16b537SWarner Losh * any other value > 0, which means there is still some decoding to do to complete current frame. 3440c16b537SWarner Losh * The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame. 3450c16b537SWarner Losh * *******************************************************************************/ 3460c16b537SWarner Losh 3470c16b537SWarner Losh typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */ 3480c16b537SWarner Losh /* Continue to distinguish them for compatibility with versions <= v1.2.0 */ 3490c16b537SWarner Losh /*===== ZSTD_DStream management functions =====*/ 3500c16b537SWarner Losh ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); 3510c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); 3520c16b537SWarner Losh 3530c16b537SWarner Losh /*===== Streaming decompression functions =====*/ 3540c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); 3550c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 3560c16b537SWarner Losh 3570c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ 3580c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ 3590c16b537SWarner Losh 3600c16b537SWarner Losh #endif /* ZSTD_H_235446 */ 3610c16b537SWarner Losh 3620c16b537SWarner Losh 3630c16b537SWarner Losh 3640c16b537SWarner Losh /**************************************************************************************** 3650c16b537SWarner Losh * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS 3660c16b537SWarner Losh * The definitions in this section are considered experimental. 3670c16b537SWarner Losh * They should never be used with a dynamic library, as prototypes may change in the future. 3680c16b537SWarner Losh * They are provided for advanced scenarios. 3690c16b537SWarner Losh * Use them only in association with static linking. 3700c16b537SWarner Losh * ***************************************************************************************/ 3710c16b537SWarner Losh 3720c16b537SWarner Losh #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) 3730c16b537SWarner Losh #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY 3740c16b537SWarner Losh 3750c16b537SWarner Losh /* --- Constants ---*/ 3760c16b537SWarner Losh #define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */ 3770c16b537SWarner Losh #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U 3780c16b537SWarner Losh #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* v0.7+ */ 3790c16b537SWarner Losh 3800c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX_32 30 3810c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX_64 31 3820c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) 3830c16b537SWarner Losh #define ZSTD_WINDOWLOG_MIN 10 3840c16b537SWarner Losh #define ZSTD_HASHLOG_MAX MIN(ZSTD_WINDOWLOG_MAX, 30) 3850c16b537SWarner Losh #define ZSTD_HASHLOG_MIN 6 3860c16b537SWarner Losh #define ZSTD_CHAINLOG_MAX MIN(ZSTD_WINDOWLOG_MAX+1, 30) 3870c16b537SWarner Losh #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN 3880c16b537SWarner Losh #define ZSTD_HASHLOG3_MAX 17 3890c16b537SWarner Losh #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) 3900c16b537SWarner Losh #define ZSTD_SEARCHLOG_MIN 1 3910c16b537SWarner Losh #define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ 3920c16b537SWarner Losh #define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */ 3930c16b537SWarner Losh #define ZSTD_TARGETLENGTH_MIN 4 /* only useful for btopt */ 3940c16b537SWarner Losh #define ZSTD_TARGETLENGTH_MAX 999 /* only useful for btopt */ 3950c16b537SWarner Losh #define ZSTD_LDM_MINMATCH_MIN 4 3960c16b537SWarner Losh #define ZSTD_LDM_MINMATCH_MAX 4096 3970c16b537SWarner Losh #define ZSTD_LDM_BUCKETSIZELOG_MAX 8 3980c16b537SWarner Losh 3990c16b537SWarner Losh #define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size to know frame header size */ 4000c16b537SWarner Losh #define ZSTD_FRAMEHEADERSIZE_MIN 6 4010c16b537SWarner Losh #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */ 4020c16b537SWarner Losh static const size_t ZSTD_frameHeaderSize_prefix = ZSTD_FRAMEHEADERSIZE_PREFIX; 4030c16b537SWarner Losh static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN; 4040c16b537SWarner Losh static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX; 4050c16b537SWarner Losh static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */ 4060c16b537SWarner Losh 4070c16b537SWarner Losh 4080c16b537SWarner Losh /*--- Advanced types ---*/ 4090c16b537SWarner Losh typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, 4100c16b537SWarner Losh ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; /* from faster to stronger */ 4110c16b537SWarner Losh 4120c16b537SWarner Losh typedef struct { 4130c16b537SWarner Losh unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ 4140c16b537SWarner Losh unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ 4150c16b537SWarner Losh unsigned hashLog; /**< dispatch table : larger == faster, more memory */ 4160c16b537SWarner Losh unsigned searchLog; /**< nb of searches : larger == more compression, slower */ 4170c16b537SWarner Losh unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */ 4180c16b537SWarner Losh unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ 4190c16b537SWarner Losh ZSTD_strategy strategy; 4200c16b537SWarner Losh } ZSTD_compressionParameters; 4210c16b537SWarner Losh 4220c16b537SWarner Losh typedef struct { 4230c16b537SWarner Losh unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */ 4240c16b537SWarner Losh unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */ 4250c16b537SWarner Losh unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */ 4260c16b537SWarner Losh } ZSTD_frameParameters; 4270c16b537SWarner Losh 4280c16b537SWarner Losh typedef struct { 4290c16b537SWarner Losh ZSTD_compressionParameters cParams; 4300c16b537SWarner Losh ZSTD_frameParameters fParams; 4310c16b537SWarner Losh } ZSTD_parameters; 4320c16b537SWarner Losh 4330c16b537SWarner Losh typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params; 4340c16b537SWarner Losh 435*052d3c12SConrad Meyer /*--- Custom memory allocation functions ---*/ 4360c16b537SWarner Losh typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); 4370c16b537SWarner Losh typedef void (*ZSTD_freeFunction) (void* opaque, void* address); 4380c16b537SWarner Losh typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; 4390c16b537SWarner Losh /* use this constant to defer to stdlib's functions */ 440*052d3c12SConrad Meyer static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; 4410c16b537SWarner Losh 4420c16b537SWarner Losh 4430c16b537SWarner Losh /*************************************** 4440c16b537SWarner Losh * Frame size functions 4450c16b537SWarner Losh ***************************************/ 4460c16b537SWarner Losh 4470c16b537SWarner Losh /*! ZSTD_findFrameCompressedSize() : 4480c16b537SWarner Losh * `src` should point to the start of a ZSTD encoded frame or skippable frame 449*052d3c12SConrad Meyer * `srcSize` must be >= first frame size 4500c16b537SWarner Losh * @return : the compressed size of the first frame starting at `src`, 4510c16b537SWarner Losh * suitable to pass to `ZSTD_decompress` or similar, 4520c16b537SWarner Losh * or an error code if input is invalid */ 4530c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); 4540c16b537SWarner Losh 4550c16b537SWarner Losh /*! ZSTD_findDecompressedSize() : 4560c16b537SWarner Losh * `src` should point the start of a series of ZSTD encoded and/or skippable frames 4570c16b537SWarner Losh * `srcSize` must be the _exact_ size of this series 4580c16b537SWarner Losh * (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`) 4590c16b537SWarner Losh * @return : - decompressed size of all data in all successive frames 4600c16b537SWarner Losh * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN 4610c16b537SWarner Losh * - if an error occurred: ZSTD_CONTENTSIZE_ERROR 4620c16b537SWarner Losh * 4630c16b537SWarner Losh * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. 4640c16b537SWarner Losh * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 4650c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 4660c16b537SWarner Losh * note 2 : decompressed size is always present when compression is done with ZSTD_compress() 4670c16b537SWarner Losh * note 3 : decompressed size can be very large (64-bits value), 4680c16b537SWarner Losh * potentially larger than what local system can handle as a single memory segment. 4690c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 4700c16b537SWarner Losh * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. 4710c16b537SWarner Losh * Always ensure result fits within application's authorized limits. 4720c16b537SWarner Losh * Each application can set its own limits. 4730c16b537SWarner Losh * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to 4740c16b537SWarner Losh * read each contained frame header. This is fast as most of the data is skipped, 4750c16b537SWarner Losh * however it does mean that all frame data must be present and valid. */ 4760c16b537SWarner Losh ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); 4770c16b537SWarner Losh 4780c16b537SWarner Losh /*! ZSTD_frameHeaderSize() : 4790c16b537SWarner Losh * `src` should point to the start of a ZSTD frame 4800c16b537SWarner Losh * `srcSize` must be >= ZSTD_frameHeaderSize_prefix. 4810c16b537SWarner Losh * @return : size of the Frame Header */ 4820c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); 4830c16b537SWarner Losh 4840c16b537SWarner Losh 4850c16b537SWarner Losh /*************************************** 4860c16b537SWarner Losh * Context memory usage 4870c16b537SWarner Losh ***************************************/ 4880c16b537SWarner Losh 4890c16b537SWarner Losh /*! ZSTD_sizeof_*() : 4900c16b537SWarner Losh * These functions give the current memory usage of selected object. 4910c16b537SWarner Losh * Object memory usage can evolve when re-used multiple times. */ 4920c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); 4930c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); 4940c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); 4950c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); 4960c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); 4970c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); 4980c16b537SWarner Losh 4990c16b537SWarner Losh /*! ZSTD_estimate*() : 5000c16b537SWarner Losh * These functions make it possible to estimate memory usage 5010c16b537SWarner Losh * of a future {D,C}Ctx, before its creation. 5020c16b537SWarner Losh * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one. 5030c16b537SWarner Losh * It will also consider src size to be arbitrarily "large", which is worst case. 5040c16b537SWarner Losh * If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation. 5050c16b537SWarner Losh * ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 5060c16b537SWarner Losh * ZSTD_estimateCCtxSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbThreads is > 1. 5070c16b537SWarner Losh * Note : CCtx estimation is only correct for single-threaded compression */ 5080c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel); 5090c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); 5100c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); 5110c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); 5120c16b537SWarner Losh 5130c16b537SWarner Losh /*! ZSTD_estimateCStreamSize() : 5140c16b537SWarner Losh * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. 5150c16b537SWarner Losh * It will also consider src size to be arbitrarily "large", which is worst case. 5160c16b537SWarner Losh * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. 5170c16b537SWarner Losh * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 5180c16b537SWarner Losh * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_p_nbThreads is set to a value > 1. 5190c16b537SWarner Losh * Note : CStream estimation is only correct for single-threaded compression. 5200c16b537SWarner Losh * ZSTD_DStream memory budget depends on window Size. 5210c16b537SWarner Losh * This information can be passed manually, using ZSTD_estimateDStreamSize, 5220c16b537SWarner Losh * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); 5230c16b537SWarner Losh * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), 5240c16b537SWarner Losh * an internal ?Dict will be created, which additional size is not estimated here. 5250c16b537SWarner Losh * In this case, get total size by adding ZSTD_estimate?DictSize */ 5260c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); 5270c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); 5280c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); 5290c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); 5300c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); 5310c16b537SWarner Losh 5320c16b537SWarner Losh typedef enum { 5330c16b537SWarner Losh ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */ 5340c16b537SWarner Losh ZSTD_dlm_byRef, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */ 5350c16b537SWarner Losh } ZSTD_dictLoadMethod_e; 5360c16b537SWarner Losh 5370c16b537SWarner Losh /*! ZSTD_estimate?DictSize() : 5380c16b537SWarner Losh * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). 5390c16b537SWarner Losh * ZSTD_estimateCStreamSize_advanced_usingCParams() makes it possible to control precisely compression parameters, like ZSTD_createCDict_advanced(). 5400c16b537SWarner Losh * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller 5410c16b537SWarner Losh */ 5420c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); 5430c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); 5440c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); 5450c16b537SWarner Losh 5460c16b537SWarner Losh 5470c16b537SWarner Losh /*************************************** 5480c16b537SWarner Losh * Advanced compression functions 5490c16b537SWarner Losh ***************************************/ 5500c16b537SWarner Losh /*! ZSTD_createCCtx_advanced() : 5510c16b537SWarner Losh * Create a ZSTD compression context using external alloc and free functions */ 5520c16b537SWarner Losh ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); 5530c16b537SWarner Losh 5540c16b537SWarner Losh /*! ZSTD_initStaticCCtx() : initialize a fixed-size zstd compression context 5550c16b537SWarner Losh * workspace: The memory area to emplace the context into. 5560c16b537SWarner Losh * Provided pointer must 8-bytes aligned. 5570c16b537SWarner Losh * It must outlive context usage. 5580c16b537SWarner Losh * workspaceSize: Use ZSTD_estimateCCtxSize() or ZSTD_estimateCStreamSize() 5590c16b537SWarner Losh * to determine how large workspace must be to support scenario. 560*052d3c12SConrad Meyer * @return : pointer to ZSTD_CCtx* (same address as workspace, but different type), 561*052d3c12SConrad Meyer * or NULL if error (typically size too small) 5620c16b537SWarner Losh * Note : zstd will never resize nor malloc() when using a static cctx. 5630c16b537SWarner Losh * If it needs more memory than available, it will simply error out. 5640c16b537SWarner Losh * Note 2 : there is no corresponding "free" function. 5650c16b537SWarner Losh * Since workspace was allocated externally, it must be freed externally too. 5660c16b537SWarner Losh * Limitation 1 : currently not compatible with internal CDict creation, such as 5670c16b537SWarner Losh * ZSTD_CCtx_loadDictionary() or ZSTD_initCStream_usingDict(). 5680c16b537SWarner Losh * Limitation 2 : currently not compatible with multi-threading 5690c16b537SWarner Losh */ 5700c16b537SWarner Losh ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); 5710c16b537SWarner Losh 5720c16b537SWarner Losh 5730c16b537SWarner Losh /*! ZSTD_createCDict_byReference() : 5740c16b537SWarner Losh * Create a digested dictionary for compression 5750c16b537SWarner Losh * Dictionary content is simply referenced, and therefore stays in dictBuffer. 5760c16b537SWarner Losh * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */ 5770c16b537SWarner Losh ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); 5780c16b537SWarner Losh 5790c16b537SWarner Losh typedef enum { ZSTD_dm_auto=0, /* dictionary is "full" if it starts with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ 5800c16b537SWarner Losh ZSTD_dm_rawContent, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ 5810c16b537SWarner Losh ZSTD_dm_fullDict /* refuses to load a dictionary if it does not respect Zstandard's specification */ 5820c16b537SWarner Losh } ZSTD_dictMode_e; 5830c16b537SWarner Losh /*! ZSTD_createCDict_advanced() : 5840c16b537SWarner Losh * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */ 5850c16b537SWarner Losh ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, 5860c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod, 5870c16b537SWarner Losh ZSTD_dictMode_e dictMode, 5880c16b537SWarner Losh ZSTD_compressionParameters cParams, 5890c16b537SWarner Losh ZSTD_customMem customMem); 5900c16b537SWarner Losh 591*052d3c12SConrad Meyer /*! ZSTD_initStaticCDict() : 5920c16b537SWarner Losh * Generate a digested dictionary in provided memory area. 5930c16b537SWarner Losh * workspace: The memory area to emplace the dictionary into. 5940c16b537SWarner Losh * Provided pointer must 8-bytes aligned. 5950c16b537SWarner Losh * It must outlive dictionary usage. 5960c16b537SWarner Losh * workspaceSize: Use ZSTD_estimateCDictSize() 5970c16b537SWarner Losh * to determine how large workspace must be. 5980c16b537SWarner Losh * cParams : use ZSTD_getCParams() to transform a compression level 5990c16b537SWarner Losh * into its relevants cParams. 600*052d3c12SConrad Meyer * @return : pointer to ZSTD_CDict* (same address as workspace, but different type), 601*052d3c12SConrad Meyer * or NULL if error (typically, size too small). 6020c16b537SWarner Losh * Note : there is no corresponding "free" function. 6030c16b537SWarner Losh * Since workspace was allocated externally, it must be freed externally. 6040c16b537SWarner Losh */ 6050c16b537SWarner Losh ZSTDLIB_API ZSTD_CDict* ZSTD_initStaticCDict( 6060c16b537SWarner Losh void* workspace, size_t workspaceSize, 6070c16b537SWarner Losh const void* dict, size_t dictSize, 6080c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode, 6090c16b537SWarner Losh ZSTD_compressionParameters cParams); 6100c16b537SWarner Losh 6110c16b537SWarner Losh /*! ZSTD_getCParams() : 6120c16b537SWarner Losh * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. 6130c16b537SWarner Losh * `estimatedSrcSize` value is optional, select 0 if not known */ 6140c16b537SWarner Losh ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 6150c16b537SWarner Losh 6160c16b537SWarner Losh /*! ZSTD_getParams() : 6170c16b537SWarner Losh * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. 618*052d3c12SConrad Meyer * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */ 6190c16b537SWarner Losh ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 6200c16b537SWarner Losh 6210c16b537SWarner Losh /*! ZSTD_checkCParams() : 6220c16b537SWarner Losh * Ensure param values remain within authorized range */ 6230c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); 6240c16b537SWarner Losh 6250c16b537SWarner Losh /*! ZSTD_adjustCParams() : 6260c16b537SWarner Losh * optimize params for a given `srcSize` and `dictSize`. 6270c16b537SWarner Losh * both values are optional, select `0` if unknown. */ 6280c16b537SWarner Losh ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); 6290c16b537SWarner Losh 6300c16b537SWarner Losh /*! ZSTD_compress_advanced() : 6310c16b537SWarner Losh * Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */ 6320c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, 6330c16b537SWarner Losh void* dst, size_t dstCapacity, 6340c16b537SWarner Losh const void* src, size_t srcSize, 6350c16b537SWarner Losh const void* dict,size_t dictSize, 6360c16b537SWarner Losh ZSTD_parameters params); 6370c16b537SWarner Losh 6380c16b537SWarner Losh /*! ZSTD_compress_usingCDict_advanced() : 6390c16b537SWarner Losh * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */ 6400c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, 6410c16b537SWarner Losh void* dst, size_t dstCapacity, 6420c16b537SWarner Losh const void* src, size_t srcSize, 6430c16b537SWarner Losh const ZSTD_CDict* cdict, ZSTD_frameParameters fParams); 6440c16b537SWarner Losh 6450c16b537SWarner Losh 6460c16b537SWarner Losh /*--- Advanced decompression functions ---*/ 6470c16b537SWarner Losh 6480c16b537SWarner Losh /*! ZSTD_isFrame() : 6490c16b537SWarner Losh * Tells if the content of `buffer` starts with a valid Frame Identifier. 6500c16b537SWarner Losh * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 6510c16b537SWarner Losh * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 6520c16b537SWarner Losh * Note 3 : Skippable Frame Identifiers are considered valid. */ 6530c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); 6540c16b537SWarner Losh 6550c16b537SWarner Losh /*! ZSTD_createDCtx_advanced() : 6560c16b537SWarner Losh * Create a ZSTD decompression context using external alloc and free functions */ 6570c16b537SWarner Losh ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); 6580c16b537SWarner Losh 6590c16b537SWarner Losh /*! ZSTD_initStaticDCtx() : initialize a fixed-size zstd decompression context 6600c16b537SWarner Losh * workspace: The memory area to emplace the context into. 6610c16b537SWarner Losh * Provided pointer must 8-bytes aligned. 6620c16b537SWarner Losh * It must outlive context usage. 6630c16b537SWarner Losh * workspaceSize: Use ZSTD_estimateDCtxSize() or ZSTD_estimateDStreamSize() 6640c16b537SWarner Losh * to determine how large workspace must be to support scenario. 665*052d3c12SConrad Meyer * @return : pointer to ZSTD_DCtx* (same address as workspace, but different type), 666*052d3c12SConrad Meyer * or NULL if error (typically size too small) 6670c16b537SWarner Losh * Note : zstd will never resize nor malloc() when using a static dctx. 6680c16b537SWarner Losh * If it needs more memory than available, it will simply error out. 6690c16b537SWarner Losh * Note 2 : static dctx is incompatible with legacy support 6700c16b537SWarner Losh * Note 3 : there is no corresponding "free" function. 6710c16b537SWarner Losh * Since workspace was allocated externally, it must be freed externally. 6720c16b537SWarner Losh * Limitation : currently not compatible with internal DDict creation, 6730c16b537SWarner Losh * such as ZSTD_initDStream_usingDict(). 6740c16b537SWarner Losh */ 6750c16b537SWarner Losh ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize); 6760c16b537SWarner Losh 6770c16b537SWarner Losh /*! ZSTD_createDDict_byReference() : 6780c16b537SWarner Losh * Create a digested dictionary, ready to start decompression operation without startup delay. 6790c16b537SWarner Losh * Dictionary content is referenced, and therefore stays in dictBuffer. 6800c16b537SWarner Losh * It is important that dictBuffer outlives DDict, 6810c16b537SWarner Losh * it must remain read accessible throughout the lifetime of DDict */ 6820c16b537SWarner Losh ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); 6830c16b537SWarner Losh 6840c16b537SWarner Losh /*! ZSTD_createDDict_advanced() : 6850c16b537SWarner Losh * Create a ZSTD_DDict using external alloc and free, optionally by reference */ 6860c16b537SWarner Losh ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, 6870c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod, 6880c16b537SWarner Losh ZSTD_customMem customMem); 6890c16b537SWarner Losh 6900c16b537SWarner Losh /*! ZSTD_initStaticDDict() : 6910c16b537SWarner Losh * Generate a digested dictionary in provided memory area. 6920c16b537SWarner Losh * workspace: The memory area to emplace the dictionary into. 6930c16b537SWarner Losh * Provided pointer must 8-bytes aligned. 6940c16b537SWarner Losh * It must outlive dictionary usage. 6950c16b537SWarner Losh * workspaceSize: Use ZSTD_estimateDDictSize() 6960c16b537SWarner Losh * to determine how large workspace must be. 6970c16b537SWarner Losh * @return : pointer to ZSTD_DDict*, or NULL if error (size too small) 6980c16b537SWarner Losh * Note : there is no corresponding "free" function. 6990c16b537SWarner Losh * Since workspace was allocated externally, it must be freed externally. 7000c16b537SWarner Losh */ 7010c16b537SWarner Losh ZSTDLIB_API ZSTD_DDict* ZSTD_initStaticDDict(void* workspace, size_t workspaceSize, 7020c16b537SWarner Losh const void* dict, size_t dictSize, 7030c16b537SWarner Losh ZSTD_dictLoadMethod_e dictLoadMethod); 7040c16b537SWarner Losh 7050c16b537SWarner Losh /*! ZSTD_getDictID_fromDict() : 7060c16b537SWarner Losh * Provides the dictID stored within dictionary. 7070c16b537SWarner Losh * if @return == 0, the dictionary is not conformant with Zstandard specification. 7080c16b537SWarner Losh * It can still be loaded, but as a content-only dictionary. */ 7090c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); 7100c16b537SWarner Losh 7110c16b537SWarner Losh /*! ZSTD_getDictID_fromDDict() : 7120c16b537SWarner Losh * Provides the dictID of the dictionary loaded into `ddict`. 7130c16b537SWarner Losh * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 7140c16b537SWarner Losh * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ 7150c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); 7160c16b537SWarner Losh 7170c16b537SWarner Losh /*! ZSTD_getDictID_fromFrame() : 7180c16b537SWarner Losh * Provides the dictID required to decompressed the frame stored within `src`. 7190c16b537SWarner Losh * If @return == 0, the dictID could not be decoded. 7200c16b537SWarner Losh * This could for one of the following reasons : 7210c16b537SWarner Losh * - The frame does not require a dictionary to be decoded (most common case). 7220c16b537SWarner Losh * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. 7230c16b537SWarner Losh * Note : this use case also happens when using a non-conformant dictionary. 7240c16b537SWarner Losh * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). 7250c16b537SWarner Losh * - This is not a Zstandard frame. 7260c16b537SWarner Losh * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */ 7270c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); 7280c16b537SWarner Losh 7290c16b537SWarner Losh 7300c16b537SWarner Losh /******************************************************************** 7310c16b537SWarner Losh * Advanced streaming functions 7320c16b537SWarner Losh ********************************************************************/ 7330c16b537SWarner Losh 7340c16b537SWarner Losh /*===== Advanced Streaming compression functions =====*/ 7350c16b537SWarner Losh ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); 7360c16b537SWarner Losh ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */ 737*052d3c12SConrad Meyer ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If it is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, "0" also disables frame content size field. It may be enabled in the future. */ 7380c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.*/ 7390c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, 740*052d3c12SConrad Meyer ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. */ 7410c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); /**< note : cdict will just be referenced, and must outlive compression session */ 742*052d3c12SConrad Meyer ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters. pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. */ 7430c16b537SWarner Losh 7440c16b537SWarner Losh /*! ZSTD_resetCStream() : 7450c16b537SWarner Losh * start a new compression job, using same parameters from previous job. 7460c16b537SWarner Losh * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.. 7470c16b537SWarner Losh * Note that zcs must be init at least once before using ZSTD_resetCStream(). 748*052d3c12SConrad Meyer * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. 7490c16b537SWarner Losh * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. 750*052d3c12SConrad Meyer * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, 751*052d3c12SConrad Meyer * but it may change to mean "empty" in some future version, so prefer using macro ZSTD_CONTENTSIZE_UNKNOWN. 7520c16b537SWarner Losh * @return : 0, or an error code (which can be tested using ZSTD_isError()) */ 7530c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); 7540c16b537SWarner Losh 7550c16b537SWarner Losh 7560c16b537SWarner Losh /*===== Advanced Streaming decompression functions =====*/ 7570c16b537SWarner Losh ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); 7580c16b537SWarner Losh ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */ 7590c16b537SWarner Losh typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e; 7600c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); /* obsolete : this API will be removed in a future version */ 7610c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: no dictionary will be used if dict == NULL or dictSize < 8 */ 7620c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict is referenced, it must outlive decompression session */ 7630c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */ 7640c16b537SWarner Losh 7650c16b537SWarner Losh 7660c16b537SWarner Losh /********************************************************************* 7670c16b537SWarner Losh * Buffer-less and synchronous inner streaming functions 7680c16b537SWarner Losh * 7690c16b537SWarner Losh * This is an advanced API, giving full control over buffer management, for users which need direct control over memory. 7700c16b537SWarner Losh * But it's also a complex one, with several restrictions, documented below. 7710c16b537SWarner Losh * Prefer normal streaming API for an easier experience. 7720c16b537SWarner Losh ********************************************************************* */ 7730c16b537SWarner Losh 7740c16b537SWarner Losh /** 7750c16b537SWarner Losh Buffer-less streaming compression (synchronous mode) 7760c16b537SWarner Losh 7770c16b537SWarner Losh A ZSTD_CCtx object is required to track streaming operations. 7780c16b537SWarner Losh Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. 7790c16b537SWarner Losh ZSTD_CCtx object can be re-used multiple times within successive compression operations. 7800c16b537SWarner Losh 7810c16b537SWarner Losh Start by initializing a context. 7820c16b537SWarner Losh Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, 7830c16b537SWarner Losh or ZSTD_compressBegin_advanced(), for finer parameter control. 7840c16b537SWarner Losh It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() 7850c16b537SWarner Losh 7860c16b537SWarner Losh Then, consume your input using ZSTD_compressContinue(). 7870c16b537SWarner Losh There are some important considerations to keep in mind when using this advanced function : 7880c16b537SWarner Losh - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. 7890c16b537SWarner Losh - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. 7900c16b537SWarner Losh - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. 7910c16b537SWarner Losh Worst case evaluation is provided by ZSTD_compressBound(). 7920c16b537SWarner Losh ZSTD_compressContinue() doesn't guarantee recover after a failed compression. 7930c16b537SWarner Losh - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). 7940c16b537SWarner Losh It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) 7950c16b537SWarner Losh - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. 7960c16b537SWarner Losh In which case, it will "discard" the relevant memory section from its history. 7970c16b537SWarner Losh 7980c16b537SWarner Losh Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. 7990c16b537SWarner Losh It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. 8000c16b537SWarner Losh Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. 8010c16b537SWarner Losh 8020c16b537SWarner Losh `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. 8030c16b537SWarner Losh */ 8040c16b537SWarner Losh 8050c16b537SWarner Losh /*===== Buffer-less streaming compression functions =====*/ 8060c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); 8070c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 808*052d3c12SConrad Meyer ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */ 8090c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */ 810*052d3c12SConrad Meyer ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */ 811*052d3c12SConrad Meyer ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */ 8120c16b537SWarner Losh 8130c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 8140c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 8150c16b537SWarner Losh 8160c16b537SWarner Losh 8170c16b537SWarner Losh /*- 8180c16b537SWarner Losh Buffer-less streaming decompression (synchronous mode) 8190c16b537SWarner Losh 8200c16b537SWarner Losh A ZSTD_DCtx object is required to track streaming operations. 8210c16b537SWarner Losh Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 8220c16b537SWarner Losh A ZSTD_DCtx object can be re-used multiple times. 8230c16b537SWarner Losh 8240c16b537SWarner Losh First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). 8250c16b537SWarner Losh Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. 8260c16b537SWarner Losh Data fragment must be large enough to ensure successful decoding. 8270c16b537SWarner Losh `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. 8280c16b537SWarner Losh @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. 8290c16b537SWarner Losh >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. 8300c16b537SWarner Losh errorCode, which can be tested using ZSTD_isError(). 8310c16b537SWarner Losh 8320c16b537SWarner Losh It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, 8330c16b537SWarner Losh such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). 8340c16b537SWarner Losh Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. 8350c16b537SWarner Losh As a consequence, check that values remain within valid application range. 8360c16b537SWarner Losh For example, do not allocate memory blindly, check that `windowSize` is within expectation. 8370c16b537SWarner Losh Each application can set its own limits, depending on local restrictions. 8380c16b537SWarner Losh For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. 8390c16b537SWarner Losh 8400c16b537SWarner Losh ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. 8410c16b537SWarner Losh ZSTD_decompressContinue() is very sensitive to contiguity, 8420c16b537SWarner Losh if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, 8430c16b537SWarner Losh or that previous contiguous segment is large enough to properly handle maximum back-reference distance. 8440c16b537SWarner Losh There are multiple ways to guarantee this condition. 8450c16b537SWarner Losh 8460c16b537SWarner Losh The most memory efficient way is to use a round buffer of sufficient size. 8470c16b537SWarner Losh Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), 8480c16b537SWarner Losh which can @return an error code if required value is too large for current system (in 32-bits mode). 8490c16b537SWarner Losh In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, 8500c16b537SWarner Losh up to the moment there is not enough room left in the buffer to guarantee decoding another full block, 8510c16b537SWarner Losh which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. 8520c16b537SWarner Losh At which point, decoding can resume from the beginning of the buffer. 8530c16b537SWarner Losh Note that already decoded data stored in the buffer should be flushed before being overwritten. 8540c16b537SWarner Losh 8550c16b537SWarner Losh There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. 8560c16b537SWarner Losh 8570c16b537SWarner Losh Finally, if you control the compression process, you can also ignore all buffer size rules, 8580c16b537SWarner Losh as long as the encoder and decoder progress in "lock-step", 8590c16b537SWarner Losh aka use exactly the same buffer sizes, break contiguity at the same place, etc. 8600c16b537SWarner Losh 8610c16b537SWarner Losh Once buffers are setup, start decompression, with ZSTD_decompressBegin(). 8620c16b537SWarner Losh If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). 8630c16b537SWarner Losh 8640c16b537SWarner Losh Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 8650c16b537SWarner Losh ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 8660c16b537SWarner Losh ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. 8670c16b537SWarner Losh 8680c16b537SWarner Losh @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). 8690c16b537SWarner Losh It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. 8700c16b537SWarner Losh It can also be an error code, which can be tested with ZSTD_isError(). 8710c16b537SWarner Losh 8720c16b537SWarner Losh A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 8730c16b537SWarner Losh Context can then be reset to start a new decompression. 8740c16b537SWarner Losh 8750c16b537SWarner Losh Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). 8760c16b537SWarner Losh This information is not required to properly decode a frame. 8770c16b537SWarner Losh 8780c16b537SWarner Losh == Special case : skippable frames == 8790c16b537SWarner Losh 8800c16b537SWarner Losh Skippable frames allow integration of user-defined data into a flow of concatenated frames. 8810c16b537SWarner Losh Skippable frames will be ignored (skipped) by decompressor. 8820c16b537SWarner Losh The format of skippable frames is as follows : 8830c16b537SWarner Losh a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F 8840c16b537SWarner Losh b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits 8850c16b537SWarner Losh c) Frame Content - any content (User Data) of length equal to Frame Size 8860c16b537SWarner Losh For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. 8870c16b537SWarner Losh For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. 8880c16b537SWarner Losh */ 8890c16b537SWarner Losh 8900c16b537SWarner Losh /*===== Buffer-less streaming decompression functions =====*/ 8910c16b537SWarner Losh typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; 8920c16b537SWarner Losh typedef struct { 8930c16b537SWarner Losh unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */ 8940c16b537SWarner Losh unsigned long long windowSize; /* can be very large, up to <= frameContentSize */ 8950c16b537SWarner Losh unsigned blockSizeMax; 8960c16b537SWarner Losh ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */ 8970c16b537SWarner Losh unsigned headerSize; 8980c16b537SWarner Losh unsigned dictID; 8990c16b537SWarner Losh unsigned checksumFlag; 9000c16b537SWarner Losh } ZSTD_frameHeader; 9010c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 9020c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); /**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */ 9030c16b537SWarner Losh 9040c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); 9050c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 9060c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 9070c16b537SWarner Losh 9080c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); 9090c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 9100c16b537SWarner Losh 9110c16b537SWarner Losh /* misc */ 9120c16b537SWarner Losh ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); 9130c16b537SWarner Losh typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; 9140c16b537SWarner Losh ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); 9150c16b537SWarner Losh 9160c16b537SWarner Losh 9170c16b537SWarner Losh 9180c16b537SWarner Losh /* ============================================ */ 9190c16b537SWarner Losh /** New advanced API (experimental) */ 9200c16b537SWarner Losh /* ============================================ */ 9210c16b537SWarner Losh 9220c16b537SWarner Losh /* notes on API design : 9230c16b537SWarner Losh * In this proposal, parameters are pushed one by one into an existing context, 9240c16b537SWarner Losh * and then applied on all subsequent compression jobs. 9250c16b537SWarner Losh * When no parameter is ever provided, CCtx is created with compression level ZSTD_CLEVEL_DEFAULT. 9260c16b537SWarner Losh * 9270c16b537SWarner Losh * This API is intended to replace all others experimental API. 9280c16b537SWarner Losh * It can basically do all other use cases, and even new ones. 9290c16b537SWarner Losh * In constrast with _advanced() variants, it stands a reasonable chance to become "stable", 9300c16b537SWarner Losh * after a good testing period. 9310c16b537SWarner Losh */ 9320c16b537SWarner Losh 9330c16b537SWarner Losh /* note on naming convention : 9340c16b537SWarner Losh * Initially, the API favored names like ZSTD_setCCtxParameter() . 9350c16b537SWarner Losh * In this proposal, convention is changed towards ZSTD_CCtx_setParameter() . 9360c16b537SWarner Losh * The main driver is that it identifies more clearly the target object type. 9370c16b537SWarner Losh * It feels clearer when considering multiple targets : 9380c16b537SWarner Losh * ZSTD_CDict_setParameter() (rather than ZSTD_setCDictParameter()) 9390c16b537SWarner Losh * ZSTD_CCtxParams_setParameter() (rather than ZSTD_setCCtxParamsParameter() ) 9400c16b537SWarner Losh * etc... 9410c16b537SWarner Losh */ 9420c16b537SWarner Losh 9430c16b537SWarner Losh /* note on enum design : 9440c16b537SWarner Losh * All enum will be pinned to explicit values before reaching "stable API" status */ 9450c16b537SWarner Losh 9460c16b537SWarner Losh typedef enum { 9470c16b537SWarner Losh /* Question : should we have a format ZSTD_f_auto ? 9480c16b537SWarner Losh * For the time being, it would mean exactly the same as ZSTD_f_zstd1. 9490c16b537SWarner Losh * But, in the future, should several formats be supported, 9500c16b537SWarner Losh * on the compression side, it would mean "default format". 9510c16b537SWarner Losh * On the decompression side, it would mean "multi format", 9520c16b537SWarner Losh * and ZSTD_f_zstd1 could be reserved to mean "accept *only* zstd frames". 9530c16b537SWarner Losh * Since meaning is a little different, another option could be to define different enums for compression and decompression. 9540c16b537SWarner Losh * This question could be kept for later, when there are actually multiple formats to support, 9550c16b537SWarner Losh * but there is also the question of pinning enum values, and pinning value `0` is especially important */ 9560c16b537SWarner Losh ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */ 9570c16b537SWarner Losh ZSTD_f_zstd1_magicless, /* Variant of zstd frame format, without initial 4-bytes magic number. 9580c16b537SWarner Losh * Useful to save 4 bytes per generated frame. 9590c16b537SWarner Losh * Decoder cannot recognise automatically this format, requiring instructions. */ 9600c16b537SWarner Losh } ZSTD_format_e; 9610c16b537SWarner Losh 9620c16b537SWarner Losh typedef enum { 9630c16b537SWarner Losh /* compression format */ 9640c16b537SWarner Losh ZSTD_p_format = 10, /* See ZSTD_format_e enum definition. 9650c16b537SWarner Losh * Cast selected format as unsigned for ZSTD_CCtx_setParameter() compatibility. */ 9660c16b537SWarner Losh 9670c16b537SWarner Losh /* compression parameters */ 9680c16b537SWarner Losh ZSTD_p_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table 9690c16b537SWarner Losh * Default level is ZSTD_CLEVEL_DEFAULT==3. 9700c16b537SWarner Losh * Special: value 0 means "do not change cLevel". */ 9710c16b537SWarner Losh ZSTD_p_windowLog, /* Maximum allowed back-reference distance, expressed as power of 2. 9720c16b537SWarner Losh * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. 9730c16b537SWarner Losh * Special: value 0 means "do not change windowLog". 9740c16b537SWarner Losh * Note: Using a window size greater than ZSTD_MAXWINDOWSIZE_DEFAULT (default: 2^27) 9750c16b537SWarner Losh * requires setting the maximum window size at least as large during decompression. */ 9760c16b537SWarner Losh ZSTD_p_hashLog, /* Size of the probe table, as a power of 2. 9770c16b537SWarner Losh * Resulting table size is (1 << (hashLog+2)). 9780c16b537SWarner Losh * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. 9790c16b537SWarner Losh * Larger tables improve compression ratio of strategies <= dFast, 9800c16b537SWarner Losh * and improve speed of strategies > dFast. 9810c16b537SWarner Losh * Special: value 0 means "do not change hashLog". */ 9820c16b537SWarner Losh ZSTD_p_chainLog, /* Size of the full-search table, as a power of 2. 9830c16b537SWarner Losh * Resulting table size is (1 << (chainLog+2)). 9840c16b537SWarner Losh * Larger tables result in better and slower compression. 9850c16b537SWarner Losh * This parameter is useless when using "fast" strategy. 9860c16b537SWarner Losh * Special: value 0 means "do not change chainLog". */ 9870c16b537SWarner Losh ZSTD_p_searchLog, /* Number of search attempts, as a power of 2. 9880c16b537SWarner Losh * More attempts result in better and slower compression. 9890c16b537SWarner Losh * This parameter is useless when using "fast" and "dFast" strategies. 9900c16b537SWarner Losh * Special: value 0 means "do not change searchLog". */ 9910c16b537SWarner Losh ZSTD_p_minMatch, /* Minimum size of searched matches (note : repCode matches can be smaller). 9920c16b537SWarner Losh * Larger values make faster compression and decompression, but decrease ratio. 9930c16b537SWarner Losh * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX. 9940c16b537SWarner Losh * Note that currently, for all strategies < btopt, effective minimum is 4. 9950c16b537SWarner Losh * Note that currently, for all strategies > fast, effective maximum is 6. 9960c16b537SWarner Losh * Special: value 0 means "do not change minMatchLength". */ 9970c16b537SWarner Losh ZSTD_p_targetLength, /* Only useful for strategies >= btopt. 9980c16b537SWarner Losh * Length of Match considered "good enough" to stop search. 9990c16b537SWarner Losh * Larger values make compression stronger and slower. 10000c16b537SWarner Losh * Special: value 0 means "do not change targetLength". */ 10010c16b537SWarner Losh ZSTD_p_compressionStrategy, /* See ZSTD_strategy enum definition. 10020c16b537SWarner Losh * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility. 10030c16b537SWarner Losh * The higher the value of selected strategy, the more complex it is, 10040c16b537SWarner Losh * resulting in stronger and slower compression. 10050c16b537SWarner Losh * Special: value 0 means "do not change strategy". */ 10060c16b537SWarner Losh 10070c16b537SWarner Losh /* frame parameters */ 1008*052d3c12SConrad Meyer ZSTD_p_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1) 1009*052d3c12SConrad Meyer * Content size must be known at the beginning of compression, 1010*052d3c12SConrad Meyer * it is provided using ZSTD_CCtx_setPledgedSrcSize() */ 10110c16b537SWarner Losh ZSTD_p_checksumFlag, /* A 32-bits checksum of content is written at end of frame (default:0) */ 1012*052d3c12SConrad Meyer ZSTD_p_dictIDFlag, /* When applicable, dictionary's ID is written into frame header (default:1) */ 10130c16b537SWarner Losh 10140c16b537SWarner Losh /* multi-threading parameters */ 10150c16b537SWarner Losh ZSTD_p_nbThreads=400, /* Select how many threads a compression job can spawn (default:1) 10160c16b537SWarner Losh * More threads improve speed, but also increase memory usage. 10170c16b537SWarner Losh * Can only receive a value > 1 if ZSTD_MULTITHREAD is enabled. 10180c16b537SWarner Losh * Special: value 0 means "do not change nbThreads" */ 1019*052d3c12SConrad Meyer ZSTD_p_jobSize, /* Size of a compression job. This value is only enforced in streaming (non-blocking) mode. 1020*052d3c12SConrad Meyer * Each compression job is completed in parallel, so indirectly controls the nb of active threads. 10210c16b537SWarner Losh * 0 means default, which is dynamically determined based on compression parameters. 10220c16b537SWarner Losh * Job size must be a minimum of overlapSize, or 1 KB, whichever is largest 10230c16b537SWarner Losh * The minimum size is automatically and transparently enforced */ 10240c16b537SWarner Losh ZSTD_p_overlapSizeLog, /* Size of previous input reloaded at the beginning of each job. 10250c16b537SWarner Losh * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */ 10260c16b537SWarner Losh 10270c16b537SWarner Losh /* advanced parameters - may not remain available after API update */ 10280c16b537SWarner Losh ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize, 10290c16b537SWarner Losh * even when referencing into Dictionary content (default:0) */ 10300c16b537SWarner Losh ZSTD_p_enableLongDistanceMatching=1200, /* Enable long distance matching. 10310c16b537SWarner Losh * This parameter is designed to improve the compression 10320c16b537SWarner Losh * ratio for large inputs with long distance matches. 10330c16b537SWarner Losh * This increases the memory usage as well as window size. 10340c16b537SWarner Losh * Note: setting this parameter sets all the LDM parameters 10350c16b537SWarner Losh * as well as ZSTD_p_windowLog. It should be set after 10360c16b537SWarner Losh * ZSTD_p_compressionLevel and before ZSTD_p_windowLog and 10370c16b537SWarner Losh * other LDM parameters. Setting the compression level 10380c16b537SWarner Losh * after this parameter overrides the window log, though LDM 10390c16b537SWarner Losh * will remain enabled until explicitly disabled. */ 10400c16b537SWarner Losh ZSTD_p_ldmHashLog, /* Size of the table for long distance matching, as a power of 2. 10410c16b537SWarner Losh * Larger values increase memory usage and compression ratio, but decrease 10420c16b537SWarner Losh * compression speed. 10430c16b537SWarner Losh * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX 10440c16b537SWarner Losh * (default: windowlog - 7). */ 10450c16b537SWarner Losh ZSTD_p_ldmMinMatch, /* Minimum size of searched matches for long distance matcher. 10460c16b537SWarner Losh * Larger/too small values usually decrease compression ratio. 10470c16b537SWarner Losh * Must be clamped between ZSTD_LDM_MINMATCH_MIN 10480c16b537SWarner Losh * and ZSTD_LDM_MINMATCH_MAX (default: 64). */ 10490c16b537SWarner Losh ZSTD_p_ldmBucketSizeLog, /* Log size of each bucket in the LDM hash table for collision resolution. 10500c16b537SWarner Losh * Larger values usually improve collision resolution but may decrease 10510c16b537SWarner Losh * compression speed. 10520c16b537SWarner Losh * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX (default: 3). */ 10530c16b537SWarner Losh ZSTD_p_ldmHashEveryLog, /* Frequency of inserting/looking up entries in the LDM hash table. 10540c16b537SWarner Losh * The default is MAX(0, (windowLog - ldmHashLog)) to 10550c16b537SWarner Losh * optimize hash table usage. 10560c16b537SWarner Losh * Larger values improve compression speed. Deviating far from the 10570c16b537SWarner Losh * default value will likely result in a decrease in compression ratio. 10580c16b537SWarner Losh * Must be clamped between 0 and ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN. */ 10590c16b537SWarner Losh 10600c16b537SWarner Losh } ZSTD_cParameter; 10610c16b537SWarner Losh 10620c16b537SWarner Losh 10630c16b537SWarner Losh /*! ZSTD_CCtx_setParameter() : 10640c16b537SWarner Losh * Set one compression parameter, selected by enum ZSTD_cParameter. 10650c16b537SWarner Losh * Note : when `value` is an enum, cast it to unsigned for proper type checking. 1066*052d3c12SConrad Meyer * @result : informational value (typically, the one being set, possibly corrected), 1067*052d3c12SConrad Meyer * or an error code (which can be tested with ZSTD_isError()). */ 10680c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); 10690c16b537SWarner Losh 10700c16b537SWarner Losh /*! ZSTD_CCtx_setPledgedSrcSize() : 10710c16b537SWarner Losh * Total input data size to be compressed as a single frame. 10720c16b537SWarner Losh * This value will be controlled at the end, and result in error if not respected. 10730c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 10740c16b537SWarner Losh * Note 1 : 0 means zero, empty. 10750c16b537SWarner Losh * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. 1076*052d3c12SConrad Meyer * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new compression job. 10770c16b537SWarner Losh * Note 2 : If all data is provided and consumed in a single round, 10780c16b537SWarner Losh * this value is overriden by srcSize instead. */ 10790c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); 10800c16b537SWarner Losh 10810c16b537SWarner Losh /*! ZSTD_CCtx_loadDictionary() : 10820c16b537SWarner Losh * Create an internal CDict from dict buffer. 10830c16b537SWarner Losh * Decompression will have to use same buffer. 10840c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 10850c16b537SWarner Losh * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 10860c16b537SWarner Losh * meaning "return to no-dictionary mode". 10870c16b537SWarner Losh * Note 1 : `dict` content will be copied internally. Use 10880c16b537SWarner Losh * ZSTD_CCtx_loadDictionary_byReference() to reference dictionary 10890c16b537SWarner Losh * content instead. The dictionary buffer must then outlive its 10900c16b537SWarner Losh * users. 10910c16b537SWarner Losh * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters. 10920c16b537SWarner Losh * For this reason, compression parameters cannot be changed anymore after loading a dictionary. 10930c16b537SWarner Losh * It's also a CPU-heavy operation, with non-negligible impact on latency. 10940c16b537SWarner Losh * Note 3 : Dictionary will be used for all future compression jobs. 10950c16b537SWarner Losh * To return to "no-dictionary" situation, load a NULL dictionary 10960c16b537SWarner Losh * Note 5 : Use ZSTD_CCtx_loadDictionary_advanced() to select how dictionary 10970c16b537SWarner Losh * content will be interpreted. 10980c16b537SWarner Losh */ 10990c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 11000c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 11010c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode); 11020c16b537SWarner Losh 11030c16b537SWarner Losh 11040c16b537SWarner Losh /*! ZSTD_CCtx_refCDict() : 11050c16b537SWarner Losh * Reference a prepared dictionary, to be used for all next compression jobs. 11060c16b537SWarner Losh * Note that compression parameters are enforced from within CDict, 11070c16b537SWarner Losh * and supercede any compression parameter previously set within CCtx. 11080c16b537SWarner Losh * The dictionary will remain valid for future compression jobs using same CCtx. 11090c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 11100c16b537SWarner Losh * Special : adding a NULL CDict means "return to no-dictionary mode". 11110c16b537SWarner Losh * Note 1 : Currently, only one dictionary can be managed. 11120c16b537SWarner Losh * Adding a new dictionary effectively "discards" any previous one. 11130c16b537SWarner Losh * Note 2 : CDict is just referenced, its lifetime must outlive CCtx. 11140c16b537SWarner Losh */ 11150c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); 11160c16b537SWarner Losh 11170c16b537SWarner Losh /*! ZSTD_CCtx_refPrefix() : 11180c16b537SWarner Losh * Reference a prefix (single-usage dictionary) for next compression job. 11190c16b537SWarner Losh * Decompression need same prefix to properly regenerate data. 11200c16b537SWarner Losh * Prefix is **only used once**. Tables are discarded at end of compression job. 11210c16b537SWarner Losh * Subsequent compression jobs will be done without prefix (if none is explicitly referenced). 11220c16b537SWarner Losh * If there is a need to use same prefix multiple times, consider embedding it into a ZSTD_CDict instead. 11230c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 11240c16b537SWarner Losh * Special : Adding any prefix (including NULL) invalidates any previous prefix or dictionary 11250c16b537SWarner Losh * Note 1 : Prefix buffer is referenced. It must outlive compression job. 11260c16b537SWarner Losh * Note 2 : Referencing a prefix involves building tables, which are dependent on compression parameters. 11270c16b537SWarner Losh * It's a CPU-heavy operation, with non-negligible impact on latency. 11280c16b537SWarner Losh * Note 3 : By default, the prefix is treated as raw content 11290c16b537SWarner Losh * (ZSTD_dm_rawContent). Use ZSTD_CCtx_refPrefix_advanced() to alter 11300c16b537SWarner Losh * dictMode. */ 11310c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize); 11320c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictMode_e dictMode); 11330c16b537SWarner Losh 11340c16b537SWarner Losh 11350c16b537SWarner Losh 11360c16b537SWarner Losh typedef enum { 11370c16b537SWarner Losh ZSTD_e_continue=0, /* collect more data, encoder transparently decides when to output result, for optimal conditions */ 11380c16b537SWarner Losh ZSTD_e_flush, /* flush any data provided so far - frame will continue, future data can still reference previous data for better compression */ 11390c16b537SWarner Losh ZSTD_e_end /* flush any remaining data and close current frame. Any additional data starts a new frame. */ 11400c16b537SWarner Losh } ZSTD_EndDirective; 11410c16b537SWarner Losh 11420c16b537SWarner Losh /*! ZSTD_compress_generic() : 11430c16b537SWarner Losh * Behave about the same as ZSTD_compressStream. To note : 11440c16b537SWarner Losh * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter() 11450c16b537SWarner Losh * - Compression parameters cannot be changed once compression is started. 11460c16b537SWarner Losh * - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize 11470c16b537SWarner Losh * - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. 1148*052d3c12SConrad Meyer * - In single-thread mode (default), function is blocking : it completed its job before returning to caller. 1149*052d3c12SConrad Meyer * - In multi-thread mode, function is non-blocking : it just acquires a copy of input, and distribute job to internal worker threads, 1150*052d3c12SConrad Meyer * and then immediately returns, just indicating that there is some data remaining to be flushed. 1151*052d3c12SConrad Meyer * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. 1152*052d3c12SConrad Meyer * - Exception : in multi-threading mode, if the first call requests a ZSTD_e_end directive, it is blocking : it will complete compression before giving back control to caller. 1153*052d3c12SConrad Meyer * - @return provides the minimum amount of data remaining to be flushed from internal buffers 11540c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 1155*052d3c12SConrad Meyer * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. 1156*052d3c12SConrad Meyer * This is useful to determine if a ZSTD_e_flush or ZSTD_e_end directive is completed. 1157*052d3c12SConrad Meyer * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), 11580c16b537SWarner Losh * only ZSTD_e_end or ZSTD_e_flush operations are allowed. 1159*052d3c12SConrad Meyer * Before starting a new compression job, or changing compression parameters, 1160*052d3c12SConrad Meyer * it is required to fully flush internal buffers. 11610c16b537SWarner Losh */ 11620c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, 11630c16b537SWarner Losh ZSTD_outBuffer* output, 11640c16b537SWarner Losh ZSTD_inBuffer* input, 11650c16b537SWarner Losh ZSTD_EndDirective endOp); 11660c16b537SWarner Losh 11670c16b537SWarner Losh /*! ZSTD_CCtx_reset() : 11680c16b537SWarner Losh * Return a CCtx to clean state. 11690c16b537SWarner Losh * Useful after an error, or to interrupt an ongoing compression job and start a new one. 11700c16b537SWarner Losh * Any internal data not yet flushed is cancelled. 11710c16b537SWarner Losh * Dictionary (if any) is dropped. 11720c16b537SWarner Losh * All parameters are back to default values. 11730c16b537SWarner Losh * It's possible to modify compression parameters after a reset. 11740c16b537SWarner Losh */ 11750c16b537SWarner Losh ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx); /* Not ready yet ! */ 11760c16b537SWarner Losh 11770c16b537SWarner Losh 11780c16b537SWarner Losh /*! ZSTD_compress_generic_simpleArgs() : 11790c16b537SWarner Losh * Same as ZSTD_compress_generic(), 11800c16b537SWarner Losh * but using only integral types as arguments. 11810c16b537SWarner Losh * Argument list is larger than ZSTD_{in,out}Buffer, 11820c16b537SWarner Losh * but can be helpful for binders from dynamic languages 11830c16b537SWarner Losh * which have troubles handling structures containing memory pointers. 11840c16b537SWarner Losh */ 11850c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_generic_simpleArgs ( 11860c16b537SWarner Losh ZSTD_CCtx* cctx, 11870c16b537SWarner Losh void* dst, size_t dstCapacity, size_t* dstPos, 11880c16b537SWarner Losh const void* src, size_t srcSize, size_t* srcPos, 11890c16b537SWarner Losh ZSTD_EndDirective endOp); 11900c16b537SWarner Losh 11910c16b537SWarner Losh 11920c16b537SWarner Losh /*! ZSTD_CCtx_params : 11930c16b537SWarner Losh * Quick howto : 11940c16b537SWarner Losh * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure 11950c16b537SWarner Losh * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into 11960c16b537SWarner Losh * an existing ZSTD_CCtx_params structure. 11970c16b537SWarner Losh * This is similar to 11980c16b537SWarner Losh * ZSTD_CCtx_setParameter(). 11990c16b537SWarner Losh * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to 12000c16b537SWarner Losh * an existing CCtx. 12010c16b537SWarner Losh * These parameters will be applied to 12020c16b537SWarner Losh * all subsequent compression jobs. 12030c16b537SWarner Losh * - ZSTD_compress_generic() : Do compression using the CCtx. 12040c16b537SWarner Losh * - ZSTD_freeCCtxParams() : Free the memory. 12050c16b537SWarner Losh * 12060c16b537SWarner Losh * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 12070c16b537SWarner Losh * for static allocation for single-threaded compression. 12080c16b537SWarner Losh */ 12090c16b537SWarner Losh ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); 12100c16b537SWarner Losh 12110c16b537SWarner Losh /*! ZSTD_resetCCtxParams() : 12120c16b537SWarner Losh * Reset params to default, with the default compression level. 12130c16b537SWarner Losh */ 12140c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params); 12150c16b537SWarner Losh 12160c16b537SWarner Losh /*! ZSTD_initCCtxParams() : 12170c16b537SWarner Losh * Initializes the compression parameters of cctxParams according to 12180c16b537SWarner Losh * compression level. All other parameters are reset to their default values. 12190c16b537SWarner Losh */ 12200c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel); 12210c16b537SWarner Losh 12220c16b537SWarner Losh /*! ZSTD_initCCtxParams_advanced() : 12230c16b537SWarner Losh * Initializes the compression and frame parameters of cctxParams according to 12240c16b537SWarner Losh * params. All other parameters are reset to their default values. 12250c16b537SWarner Losh */ 12260c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); 12270c16b537SWarner Losh 12280c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); 12290c16b537SWarner Losh 12300c16b537SWarner Losh /*! ZSTD_CCtxParam_setParameter() : 12310c16b537SWarner Losh * Similar to ZSTD_CCtx_setParameter. 12320c16b537SWarner Losh * Set one compression parameter, selected by enum ZSTD_cParameter. 12330c16b537SWarner Losh * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams(). 12340c16b537SWarner Losh * Note : when `value` is an enum, cast it to unsigned for proper type checking. 12350c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 12360c16b537SWarner Losh */ 12370c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); 12380c16b537SWarner Losh 12390c16b537SWarner Losh /*! ZSTD_CCtx_setParametersUsingCCtxParams() : 12400c16b537SWarner Losh * Apply a set of ZSTD_CCtx_params to the compression context. 12410c16b537SWarner Losh * This must be done before the dictionary is loaded. 12420c16b537SWarner Losh * The pledgedSrcSize is treated as unknown. 12430c16b537SWarner Losh * Multithreading parameters are applied only if nbThreads > 1. 12440c16b537SWarner Losh */ 12450c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams( 12460c16b537SWarner Losh ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); 12470c16b537SWarner Losh 12480c16b537SWarner Losh 12490c16b537SWarner Losh /*=== Advanced parameters for decompression API ===*/ 12500c16b537SWarner Losh 12510c16b537SWarner Losh /* The following parameters must be set after creating a ZSTD_DCtx* (or ZSTD_DStream*) object, 12520c16b537SWarner Losh * but before starting decompression of a frame. 12530c16b537SWarner Losh */ 12540c16b537SWarner Losh 12550c16b537SWarner Losh /*! ZSTD_DCtx_loadDictionary() : 12560c16b537SWarner Losh * Create an internal DDict from dict buffer, 12570c16b537SWarner Losh * to be used to decompress next frames. 12580c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 12590c16b537SWarner Losh * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 12600c16b537SWarner Losh * meaning "return to no-dictionary mode". 12610c16b537SWarner Losh * Note 1 : `dict` content will be copied internally. 12620c16b537SWarner Losh * Use ZSTD_DCtx_loadDictionary_byReference() 12630c16b537SWarner Losh * to reference dictionary content instead. 12640c16b537SWarner Losh * In which case, the dictionary buffer must outlive its users. 12650c16b537SWarner Losh * Note 2 : Loading a dictionary involves building tables, 12660c16b537SWarner Losh * which has a non-negligible impact on CPU usage and latency. 12670c16b537SWarner Losh * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to select 12680c16b537SWarner Losh * how dictionary content will be interpreted and loaded. 12690c16b537SWarner Losh */ 12700c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); /* not implemented */ 12710c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); /* not implemented */ 12720c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode); /* not implemented */ 12730c16b537SWarner Losh 12740c16b537SWarner Losh 12750c16b537SWarner Losh /*! ZSTD_DCtx_refDDict() : 12760c16b537SWarner Losh * Reference a prepared dictionary, to be used to decompress next frames. 12770c16b537SWarner Losh * The dictionary remains active for decompression of future frames using same DCtx. 12780c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 12790c16b537SWarner Losh * Note 1 : Currently, only one dictionary can be managed. 12800c16b537SWarner Losh * Referencing a new dictionary effectively "discards" any previous one. 12810c16b537SWarner Losh * Special : adding a NULL DDict means "return to no-dictionary mode". 12820c16b537SWarner Losh * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. 12830c16b537SWarner Losh */ 12840c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); /* not implemented */ 12850c16b537SWarner Losh 12860c16b537SWarner Losh 12870c16b537SWarner Losh /*! ZSTD_DCtx_refPrefix() : 12880c16b537SWarner Losh * Reference a prefix (single-usage dictionary) for next compression job. 12890c16b537SWarner Losh * Prefix is **only used once**. It must be explicitly referenced before each frame. 12900c16b537SWarner Losh * If there is a need to use same prefix multiple times, consider embedding it into a ZSTD_DDict instead. 12910c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 12920c16b537SWarner Losh * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary 12930c16b537SWarner Losh * Note 2 : Prefix buffer is referenced. It must outlive compression job. 12940c16b537SWarner Losh * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent). 12950c16b537SWarner Losh * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. 12960c16b537SWarner Losh * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. 12970c16b537SWarner Losh */ 12980c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize); /* not implemented */ 12990c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictMode_e dictMode); /* not implemented */ 13000c16b537SWarner Losh 13010c16b537SWarner Losh 13020c16b537SWarner Losh /*! ZSTD_DCtx_setMaxWindowSize() : 13030c16b537SWarner Losh * Refuses allocating internal buffers for frames requiring a window size larger than provided limit. 13040c16b537SWarner Losh * This is useful to prevent a decoder context from reserving too much memory for itself (potential attack scenario). 13050c16b537SWarner Losh * This parameter is only useful in streaming mode, since no internal buffer is allocated in direct mode. 13060c16b537SWarner Losh * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_MAX) 13070c16b537SWarner Losh * @return : 0, or an error code (which can be tested using ZSTD_isError()). 13080c16b537SWarner Losh */ 13090c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize); 13100c16b537SWarner Losh 13110c16b537SWarner Losh 13120c16b537SWarner Losh /*! ZSTD_DCtx_setFormat() : 13130c16b537SWarner Losh * Instruct the decoder context about what kind of data to decode next. 13140c16b537SWarner Losh * This instruction is mandatory to decode data without a fully-formed header, 13150c16b537SWarner Losh * such ZSTD_f_zstd1_magicless for example. 13160c16b537SWarner Losh * @return : 0, or an error code (which can be tested using ZSTD_isError()). 13170c16b537SWarner Losh */ 13180c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format); 13190c16b537SWarner Losh 13200c16b537SWarner Losh 13210c16b537SWarner Losh /*! ZSTD_decompress_generic() : 13220c16b537SWarner Losh * Behave the same as ZSTD_decompressStream. 13230c16b537SWarner Losh * Decompression parameters cannot be changed once decompression is started. 13240c16b537SWarner Losh * @return : an error code, which can be tested using ZSTD_isError() 13250c16b537SWarner Losh * if >0, a hint, nb of expected input bytes for next invocation. 13260c16b537SWarner Losh * `0` means : a frame has just been fully decoded and flushed. 13270c16b537SWarner Losh */ 13280c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx, 13290c16b537SWarner Losh ZSTD_outBuffer* output, 13300c16b537SWarner Losh ZSTD_inBuffer* input); 13310c16b537SWarner Losh 13320c16b537SWarner Losh 13330c16b537SWarner Losh /*! ZSTD_decompress_generic_simpleArgs() : 13340c16b537SWarner Losh * Same as ZSTD_decompress_generic(), 13350c16b537SWarner Losh * but using only integral types as arguments. 13360c16b537SWarner Losh * Argument list is larger than ZSTD_{in,out}Buffer, 13370c16b537SWarner Losh * but can be helpful for binders from dynamic languages 13380c16b537SWarner Losh * which have troubles handling structures containing memory pointers. 13390c16b537SWarner Losh */ 13400c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress_generic_simpleArgs ( 13410c16b537SWarner Losh ZSTD_DCtx* dctx, 13420c16b537SWarner Losh void* dst, size_t dstCapacity, size_t* dstPos, 13430c16b537SWarner Losh const void* src, size_t srcSize, size_t* srcPos); 13440c16b537SWarner Losh 13450c16b537SWarner Losh 13460c16b537SWarner Losh /*! ZSTD_DCtx_reset() : 13470c16b537SWarner Losh * Return a DCtx to clean state. 13480c16b537SWarner Losh * If a decompression was ongoing, any internal data not yet flushed is cancelled. 13490c16b537SWarner Losh * All parameters are back to default values, including sticky ones. 13500c16b537SWarner Losh * Dictionary (if any) is dropped. 13510c16b537SWarner Losh * Parameters can be modified again after a reset. 13520c16b537SWarner Losh */ 13530c16b537SWarner Losh ZSTDLIB_API void ZSTD_DCtx_reset(ZSTD_DCtx* dctx); 13540c16b537SWarner Losh 13550c16b537SWarner Losh 13560c16b537SWarner Losh 13570c16b537SWarner Losh /* ============================ */ 13580c16b537SWarner Losh /** Block level API */ 13590c16b537SWarner Losh /* ============================ */ 13600c16b537SWarner Losh 13610c16b537SWarner Losh /*! 13620c16b537SWarner Losh Block functions produce and decode raw zstd blocks, without frame metadata. 13630c16b537SWarner Losh Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes). 13640c16b537SWarner Losh User will have to take in charge required information to regenerate data, such as compressed and content sizes. 13650c16b537SWarner Losh 13660c16b537SWarner Losh A few rules to respect : 13670c16b537SWarner Losh - Compressing and decompressing require a context structure 13680c16b537SWarner Losh + Use ZSTD_createCCtx() and ZSTD_createDCtx() 13690c16b537SWarner Losh - It is necessary to init context before starting 13700c16b537SWarner Losh + compression : any ZSTD_compressBegin*() variant, including with dictionary 13710c16b537SWarner Losh + decompression : any ZSTD_decompressBegin*() variant, including with dictionary 13720c16b537SWarner Losh + copyCCtx() and copyDCtx() can be used too 13730c16b537SWarner Losh - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB 13740c16b537SWarner Losh + If input is larger than a block size, it's necessary to split input data into multiple blocks 13750c16b537SWarner Losh + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead. 13760c16b537SWarner Losh Frame metadata is not that costly, and quickly becomes negligible as source size grows larger. 13770c16b537SWarner Losh - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero. 13780c16b537SWarner Losh In which case, nothing is produced into `dst`. 13790c16b537SWarner Losh + User must test for such outcome and deal directly with uncompressed data 13800c16b537SWarner Losh + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!! 13810c16b537SWarner Losh + In case of multiple successive blocks, should some of them be uncompressed, 13820c16b537SWarner Losh decoder must be informed of their existence in order to follow proper history. 13830c16b537SWarner Losh Use ZSTD_insertBlock() for such a case. 13840c16b537SWarner Losh */ 13850c16b537SWarner Losh 13860c16b537SWarner Losh #define ZSTD_BLOCKSIZELOG_MAX 17 13870c16b537SWarner Losh #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) /* define, for static allocation */ 13880c16b537SWarner Losh /*===== Raw zstd block functions =====*/ 13890c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx); 13900c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 13910c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 13920c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert uncompressed block into `dctx` history. Useful for multi-blocks decompression */ 13930c16b537SWarner Losh 13940c16b537SWarner Losh 13950c16b537SWarner Losh #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ 13960c16b537SWarner Losh 13970c16b537SWarner Losh #if defined (__cplusplus) 13980c16b537SWarner Losh } 13990c16b537SWarner Losh #endif 1400