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) 48*19fcbaf1SConrad Meyer - a single step, reusing a context (described as Explicit context) 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) 52*19fcbaf1SConrad Meyer - a single step, reusing a dictionary (described as Bulk-processing 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*19fcbaf1SConrad Meyer #define ZSTD_VERSION_RELEASE 4 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) 71*19fcbaf1SConrad Meyer ZSTDLIB_API const char* ZSTD_versionString(void); /* added in 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 95*19fcbaf1SConrad Meyer /*! ZSTD_getFrameContentSize() : added in 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(). 123*19fcbaf1SConrad Meyer * Both functions work the same way, but ZSTD_getDecompressedSize() blends 124*19fcbaf1SConrad Meyer * "empty", "unknown" and "error" results to the same return value (0), 125*19fcbaf1SConrad Meyer * while ZSTD_getFrameContentSize() gives them separate return values. 126*19fcbaf1SConrad Meyer * `src` is the start of a zstd compressed frame. 1270c16b537SWarner Losh * @return : content size to be decompressed, as a 64-bits value _if known and not empty_, 0 otherwise. */ 1280c16b537SWarner Losh ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); 1290c16b537SWarner Losh 1300c16b537SWarner Losh 1310c16b537SWarner Losh /*====== Helper functions ======*/ 132052d3c12SConrad 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 */ 133*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */ 1340c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 1350c16b537SWarner Losh ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ 1360c16b537SWarner Losh ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ 1370c16b537SWarner Losh 1380c16b537SWarner Losh 1390c16b537SWarner Losh /*************************************** 140*19fcbaf1SConrad Meyer * Explicit context 1410c16b537SWarner Losh ***************************************/ 1420c16b537SWarner Losh /*= Compression context 1430c16b537SWarner Losh * When compressing many times, 1440c16b537SWarner Losh * it is recommended to allocate a context just once, and re-use it for each successive compression operation. 1450c16b537SWarner Losh * This will make workload friendlier for system's memory. 1460c16b537SWarner Losh * Use one context per thread for parallel execution in multi-threaded environments. */ 1470c16b537SWarner Losh typedef struct ZSTD_CCtx_s ZSTD_CCtx; 1480c16b537SWarner Losh ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); 1490c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); 1500c16b537SWarner Losh 1510c16b537SWarner Losh /*! ZSTD_compressCCtx() : 1520c16b537SWarner Losh * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */ 1530c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, 1540c16b537SWarner Losh void* dst, size_t dstCapacity, 1550c16b537SWarner Losh const void* src, size_t srcSize, 1560c16b537SWarner Losh int compressionLevel); 1570c16b537SWarner Losh 1580c16b537SWarner Losh /*= Decompression context 1590c16b537SWarner Losh * When decompressing many times, 1600c16b537SWarner Losh * it is recommended to allocate a context only once, 1610c16b537SWarner Losh * and re-use it for each successive compression operation. 1620c16b537SWarner Losh * This will make workload friendlier for system's memory. 1630c16b537SWarner Losh * Use one context per thread for parallel execution. */ 1640c16b537SWarner Losh typedef struct ZSTD_DCtx_s ZSTD_DCtx; 1650c16b537SWarner Losh ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); 1660c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); 1670c16b537SWarner Losh 1680c16b537SWarner Losh /*! ZSTD_decompressDCtx() : 1690c16b537SWarner Losh * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) */ 1700c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, 1710c16b537SWarner Losh void* dst, size_t dstCapacity, 1720c16b537SWarner Losh const void* src, size_t srcSize); 1730c16b537SWarner Losh 1740c16b537SWarner Losh 1750c16b537SWarner Losh /************************** 1760c16b537SWarner Losh * Simple dictionary API 1770c16b537SWarner Losh ***************************/ 1780c16b537SWarner Losh /*! ZSTD_compress_usingDict() : 1790c16b537SWarner Losh * Compression using a predefined Dictionary (see dictBuilder/zdict.h). 1800c16b537SWarner Losh * Note : This function loads the dictionary, resulting in significant startup delay. 1810c16b537SWarner Losh * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ 1820c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, 1830c16b537SWarner Losh void* dst, size_t dstCapacity, 1840c16b537SWarner Losh const void* src, size_t srcSize, 1850c16b537SWarner Losh const void* dict,size_t dictSize, 1860c16b537SWarner Losh int compressionLevel); 1870c16b537SWarner Losh 1880c16b537SWarner Losh /*! ZSTD_decompress_usingDict() : 1890c16b537SWarner Losh * Decompression using a predefined Dictionary (see dictBuilder/zdict.h). 1900c16b537SWarner Losh * Dictionary must be identical to the one used during compression. 1910c16b537SWarner Losh * Note : This function loads the dictionary, resulting in significant startup delay. 1920c16b537SWarner Losh * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ 1930c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 1940c16b537SWarner Losh void* dst, size_t dstCapacity, 1950c16b537SWarner Losh const void* src, size_t srcSize, 1960c16b537SWarner Losh const void* dict,size_t dictSize); 1970c16b537SWarner Losh 1980c16b537SWarner Losh 1990c16b537SWarner Losh /********************************** 2000c16b537SWarner Losh * Bulk processing dictionary API 2010c16b537SWarner Losh *********************************/ 2020c16b537SWarner Losh typedef struct ZSTD_CDict_s ZSTD_CDict; 2030c16b537SWarner Losh 2040c16b537SWarner Losh /*! ZSTD_createCDict() : 2050c16b537SWarner Losh * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. 2060c16b537SWarner Losh * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. 2070c16b537SWarner Losh * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 2080c16b537SWarner Losh * `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict */ 2090c16b537SWarner Losh ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, 2100c16b537SWarner Losh int compressionLevel); 2110c16b537SWarner Losh 2120c16b537SWarner Losh /*! ZSTD_freeCDict() : 2130c16b537SWarner Losh * Function frees memory allocated by ZSTD_createCDict(). */ 2140c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); 2150c16b537SWarner Losh 2160c16b537SWarner Losh /*! ZSTD_compress_usingCDict() : 2170c16b537SWarner Losh * Compression using a digested Dictionary. 2180c16b537SWarner Losh * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. 2190c16b537SWarner Losh * Note that compression level is decided during dictionary creation. 2200c16b537SWarner Losh * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ 2210c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, 2220c16b537SWarner Losh void* dst, size_t dstCapacity, 2230c16b537SWarner Losh const void* src, size_t srcSize, 2240c16b537SWarner Losh const ZSTD_CDict* cdict); 2250c16b537SWarner Losh 2260c16b537SWarner Losh 2270c16b537SWarner Losh typedef struct ZSTD_DDict_s ZSTD_DDict; 2280c16b537SWarner Losh 2290c16b537SWarner Losh /*! ZSTD_createDDict() : 2300c16b537SWarner Losh * Create a digested dictionary, ready to start decompression operation without startup delay. 2310c16b537SWarner Losh * dictBuffer can be released after DDict creation, as its content is copied inside DDict */ 2320c16b537SWarner Losh ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); 2330c16b537SWarner Losh 2340c16b537SWarner Losh /*! ZSTD_freeDDict() : 2350c16b537SWarner Losh * Function frees memory allocated with ZSTD_createDDict() */ 2360c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); 2370c16b537SWarner Losh 2380c16b537SWarner Losh /*! ZSTD_decompress_usingDDict() : 2390c16b537SWarner Losh * Decompression using a digested Dictionary. 2400c16b537SWarner Losh * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */ 2410c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 2420c16b537SWarner Losh void* dst, size_t dstCapacity, 2430c16b537SWarner Losh const void* src, size_t srcSize, 2440c16b537SWarner Losh const ZSTD_DDict* ddict); 2450c16b537SWarner Losh 2460c16b537SWarner Losh 2470c16b537SWarner Losh /**************************** 2480c16b537SWarner Losh * Streaming 2490c16b537SWarner Losh ****************************/ 2500c16b537SWarner Losh 2510c16b537SWarner Losh typedef struct ZSTD_inBuffer_s { 2520c16b537SWarner Losh const void* src; /**< start of input buffer */ 2530c16b537SWarner Losh size_t size; /**< size of input buffer */ 2540c16b537SWarner Losh size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ 2550c16b537SWarner Losh } ZSTD_inBuffer; 2560c16b537SWarner Losh 2570c16b537SWarner Losh typedef struct ZSTD_outBuffer_s { 2580c16b537SWarner Losh void* dst; /**< start of output buffer */ 2590c16b537SWarner Losh size_t size; /**< size of output buffer */ 2600c16b537SWarner Losh size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ 2610c16b537SWarner Losh } ZSTD_outBuffer; 2620c16b537SWarner Losh 2630c16b537SWarner Losh 2640c16b537SWarner Losh 2650c16b537SWarner Losh /*-*********************************************************************** 2660c16b537SWarner Losh * Streaming compression - HowTo 2670c16b537SWarner Losh * 2680c16b537SWarner Losh * A ZSTD_CStream object is required to track streaming operation. 2690c16b537SWarner Losh * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. 2700c16b537SWarner Losh * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. 2710c16b537SWarner Losh * It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively, 2720c16b537SWarner Losh * since it will play nicer with system's memory, by re-using already allocated memory. 2730c16b537SWarner Losh * Use one separate ZSTD_CStream per thread for parallel execution. 2740c16b537SWarner Losh * 2750c16b537SWarner Losh * Start a new compression by initializing ZSTD_CStream. 2760c16b537SWarner Losh * Use ZSTD_initCStream() to start a new compression operation. 2770c16b537SWarner Losh * Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section) 2780c16b537SWarner Losh * 2790c16b537SWarner Losh * Use ZSTD_compressStream() repetitively to consume input stream. 2800c16b537SWarner Losh * The function will automatically update both `pos` fields. 2810c16b537SWarner Losh * Note that it may not consume the entire input, in which case `pos < size`, 2820c16b537SWarner Losh * and it's up to the caller to present again remaining data. 2830c16b537SWarner Losh * @return : a size hint, preferred nb of bytes to use as input for next function call 2840c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 2850c16b537SWarner Losh * Note 1 : it's just a hint, to help latency a little, any other value will work fine. 2860c16b537SWarner Losh * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize() 2870c16b537SWarner Losh * 2880c16b537SWarner Losh * At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream(). 2890c16b537SWarner Losh * `output->pos` will be updated. 2900c16b537SWarner Losh * Note that some content might still be left within internal buffer if `output->size` is too small. 2910c16b537SWarner Losh * @return : nb of bytes still present within internal buffer (0 if it's empty) 2920c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 2930c16b537SWarner Losh * 2940c16b537SWarner Losh * ZSTD_endStream() instructs to finish a frame. 2950c16b537SWarner Losh * It will perform a flush and write frame epilogue. 2960c16b537SWarner Losh * The epilogue is required for decoders to consider a frame completed. 2970c16b537SWarner Losh * ZSTD_endStream() may not be able to flush full data if `output->size` is too small. 2980c16b537SWarner Losh * In which case, call again ZSTD_endStream() to complete the flush. 2990c16b537SWarner Losh * @return : 0 if frame fully completed and fully flushed, 3000c16b537SWarner Losh or >0 if some data is still present within internal buffer 3010c16b537SWarner Losh (value is minimum size estimation for remaining data to flush, but it could be more) 3020c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 3030c16b537SWarner Losh * 3040c16b537SWarner Losh * *******************************************************************/ 3050c16b537SWarner Losh 3060c16b537SWarner Losh typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */ 3070c16b537SWarner Losh /* Continue to distinguish them for compatibility with versions <= v1.2.0 */ 3080c16b537SWarner Losh /*===== ZSTD_CStream management functions =====*/ 3090c16b537SWarner Losh ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); 3100c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); 3110c16b537SWarner Losh 3120c16b537SWarner Losh /*===== Streaming compression functions =====*/ 3130c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); 3140c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 3150c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 3160c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 3170c16b537SWarner Losh 3180c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ 3190c16b537SWarner 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. */ 3200c16b537SWarner Losh 3210c16b537SWarner Losh 3220c16b537SWarner Losh 3230c16b537SWarner Losh /*-*************************************************************************** 3240c16b537SWarner Losh * Streaming decompression - HowTo 3250c16b537SWarner Losh * 3260c16b537SWarner Losh * A ZSTD_DStream object is required to track streaming operations. 3270c16b537SWarner Losh * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. 3280c16b537SWarner Losh * ZSTD_DStream objects can be re-used multiple times. 3290c16b537SWarner Losh * 3300c16b537SWarner Losh * Use ZSTD_initDStream() to start a new decompression operation, 3310c16b537SWarner Losh * or ZSTD_initDStream_usingDict() if decompression requires a dictionary. 3320c16b537SWarner Losh * @return : recommended first input size 3330c16b537SWarner Losh * 3340c16b537SWarner Losh * Use ZSTD_decompressStream() repetitively to consume your input. 3350c16b537SWarner Losh * The function will update both `pos` fields. 3360c16b537SWarner Losh * If `input.pos < input.size`, some input has not been consumed. 3370c16b537SWarner Losh * It's up to the caller to present again remaining data. 3380c16b537SWarner Losh * If `output.pos < output.size`, decoder has flushed everything it could. 3390c16b537SWarner Losh * @return : 0 when a frame is completely decoded and fully flushed, 3400c16b537SWarner Losh * an error code, which can be tested using ZSTD_isError(), 3410c16b537SWarner Losh * any other value > 0, which means there is still some decoding to do to complete current frame. 3420c16b537SWarner Losh * The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame. 3430c16b537SWarner Losh * *******************************************************************************/ 3440c16b537SWarner Losh 3450c16b537SWarner Losh typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */ 346*19fcbaf1SConrad Meyer /* For compatibility with versions <= v1.2.0, continue to consider them separated. */ 3470c16b537SWarner Losh /*===== ZSTD_DStream management functions =====*/ 3480c16b537SWarner Losh ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); 3490c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); 3500c16b537SWarner Losh 3510c16b537SWarner Losh /*===== Streaming decompression functions =====*/ 3520c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); 3530c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 3540c16b537SWarner Losh 3550c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ 3560c16b537SWarner 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. */ 3570c16b537SWarner Losh 3580c16b537SWarner Losh #endif /* ZSTD_H_235446 */ 3590c16b537SWarner Losh 3600c16b537SWarner Losh 3610c16b537SWarner Losh 3620c16b537SWarner Losh /**************************************************************************************** 3630c16b537SWarner Losh * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS 3640c16b537SWarner Losh * The definitions in this section are considered experimental. 3650c16b537SWarner Losh * They should never be used with a dynamic library, as prototypes may change in the future. 3660c16b537SWarner Losh * They are provided for advanced scenarios. 3670c16b537SWarner Losh * Use them only in association with static linking. 3680c16b537SWarner Losh * ***************************************************************************************/ 3690c16b537SWarner Losh 3700c16b537SWarner Losh #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) 3710c16b537SWarner Losh #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY 3720c16b537SWarner Losh 3730c16b537SWarner Losh /* --- Constants ---*/ 3740c16b537SWarner Losh #define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */ 3750c16b537SWarner Losh #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U 376*19fcbaf1SConrad Meyer #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* >= v0.7.0 */ 3770c16b537SWarner Losh 3780c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX_32 30 3790c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX_64 31 3800c16b537SWarner Losh #define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) 3810c16b537SWarner Losh #define ZSTD_WINDOWLOG_MIN 10 382*19fcbaf1SConrad Meyer #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30) 3830c16b537SWarner Losh #define ZSTD_HASHLOG_MIN 6 384*19fcbaf1SConrad Meyer #define ZSTD_CHAINLOG_MAX_32 29 385*19fcbaf1SConrad Meyer #define ZSTD_CHAINLOG_MAX_64 30 386*19fcbaf1SConrad Meyer #define ZSTD_CHAINLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64)) 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 */ 393*19fcbaf1SConrad Meyer #define ZSTD_TARGETLENGTH_MIN 1 /* only used by btopt, btultra and btfast */ 3940c16b537SWarner Losh #define ZSTD_LDM_MINMATCH_MIN 4 3950c16b537SWarner Losh #define ZSTD_LDM_MINMATCH_MAX 4096 3960c16b537SWarner Losh #define ZSTD_LDM_BUCKETSIZELOG_MAX 8 3970c16b537SWarner Losh 3980c16b537SWarner Losh #define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size to know frame header size */ 3990c16b537SWarner Losh #define ZSTD_FRAMEHEADERSIZE_MIN 6 4000c16b537SWarner Losh #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */ 4010c16b537SWarner Losh static const size_t ZSTD_frameHeaderSize_prefix = ZSTD_FRAMEHEADERSIZE_PREFIX; 4020c16b537SWarner Losh static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN; 4030c16b537SWarner Losh static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX; 4040c16b537SWarner Losh static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */ 4050c16b537SWarner Losh 4060c16b537SWarner Losh 4070c16b537SWarner Losh /*--- Advanced types ---*/ 4080c16b537SWarner Losh typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, 4090c16b537SWarner Losh ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; /* from faster to stronger */ 4100c16b537SWarner Losh 4110c16b537SWarner Losh typedef struct { 4120c16b537SWarner Losh unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ 4130c16b537SWarner Losh unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ 4140c16b537SWarner Losh unsigned hashLog; /**< dispatch table : larger == faster, more memory */ 4150c16b537SWarner Losh unsigned searchLog; /**< nb of searches : larger == more compression, slower */ 4160c16b537SWarner Losh unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */ 4170c16b537SWarner Losh unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ 4180c16b537SWarner Losh ZSTD_strategy strategy; 4190c16b537SWarner Losh } ZSTD_compressionParameters; 4200c16b537SWarner Losh 4210c16b537SWarner Losh typedef struct { 4220c16b537SWarner Losh unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */ 4230c16b537SWarner Losh unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */ 4240c16b537SWarner Losh unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */ 4250c16b537SWarner Losh } ZSTD_frameParameters; 4260c16b537SWarner Losh 4270c16b537SWarner Losh typedef struct { 4280c16b537SWarner Losh ZSTD_compressionParameters cParams; 4290c16b537SWarner Losh ZSTD_frameParameters fParams; 4300c16b537SWarner Losh } ZSTD_parameters; 4310c16b537SWarner Losh 4320c16b537SWarner Losh typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params; 4330c16b537SWarner Losh 434*19fcbaf1SConrad Meyer typedef enum { 435*19fcbaf1SConrad Meyer ZSTD_dct_auto=0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ 436*19fcbaf1SConrad Meyer ZSTD_dct_rawContent, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ 437*19fcbaf1SConrad Meyer ZSTD_dct_fullDict /* refuses to load a dictionary if it does not respect Zstandard's specification */ 438*19fcbaf1SConrad Meyer } ZSTD_dictContentType_e; 439*19fcbaf1SConrad Meyer 440*19fcbaf1SConrad Meyer typedef enum { 441*19fcbaf1SConrad Meyer ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */ 442*19fcbaf1SConrad Meyer ZSTD_dlm_byRef, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */ 443*19fcbaf1SConrad Meyer } ZSTD_dictLoadMethod_e; 444*19fcbaf1SConrad Meyer 4450c16b537SWarner Losh 4460c16b537SWarner Losh 4470c16b537SWarner Losh /*************************************** 4480c16b537SWarner Losh * Frame size functions 4490c16b537SWarner Losh ***************************************/ 4500c16b537SWarner Losh 4510c16b537SWarner Losh /*! ZSTD_findFrameCompressedSize() : 4520c16b537SWarner Losh * `src` should point to the start of a ZSTD encoded frame or skippable frame 453052d3c12SConrad Meyer * `srcSize` must be >= first frame size 4540c16b537SWarner Losh * @return : the compressed size of the first frame starting at `src`, 4550c16b537SWarner Losh * suitable to pass to `ZSTD_decompress` or similar, 4560c16b537SWarner Losh * or an error code if input is invalid */ 4570c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); 4580c16b537SWarner Losh 4590c16b537SWarner Losh /*! ZSTD_findDecompressedSize() : 4600c16b537SWarner Losh * `src` should point the start of a series of ZSTD encoded and/or skippable frames 4610c16b537SWarner Losh * `srcSize` must be the _exact_ size of this series 4620c16b537SWarner Losh * (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`) 4630c16b537SWarner Losh * @return : - decompressed size of all data in all successive frames 4640c16b537SWarner Losh * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN 4650c16b537SWarner Losh * - if an error occurred: ZSTD_CONTENTSIZE_ERROR 4660c16b537SWarner Losh * 4670c16b537SWarner Losh * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. 4680c16b537SWarner Losh * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 4690c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 4700c16b537SWarner Losh * note 2 : decompressed size is always present when compression is done with ZSTD_compress() 4710c16b537SWarner Losh * note 3 : decompressed size can be very large (64-bits value), 4720c16b537SWarner Losh * potentially larger than what local system can handle as a single memory segment. 4730c16b537SWarner Losh * In which case, it's necessary to use streaming mode to decompress data. 4740c16b537SWarner Losh * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. 4750c16b537SWarner Losh * Always ensure result fits within application's authorized limits. 4760c16b537SWarner Losh * Each application can set its own limits. 4770c16b537SWarner Losh * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to 4780c16b537SWarner Losh * read each contained frame header. This is fast as most of the data is skipped, 4790c16b537SWarner Losh * however it does mean that all frame data must be present and valid. */ 4800c16b537SWarner Losh ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); 4810c16b537SWarner Losh 4820c16b537SWarner Losh /*! ZSTD_frameHeaderSize() : 4830c16b537SWarner Losh * `src` should point to the start of a ZSTD frame 4840c16b537SWarner Losh * `srcSize` must be >= ZSTD_frameHeaderSize_prefix. 4850c16b537SWarner Losh * @return : size of the Frame Header */ 4860c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); 4870c16b537SWarner Losh 4880c16b537SWarner Losh 4890c16b537SWarner Losh /*************************************** 490*19fcbaf1SConrad Meyer * Memory management 4910c16b537SWarner Losh ***************************************/ 4920c16b537SWarner Losh 4930c16b537SWarner Losh /*! ZSTD_sizeof_*() : 4940c16b537SWarner Losh * These functions give the current memory usage of selected object. 495*19fcbaf1SConrad Meyer * Object memory usage can evolve when re-used. */ 4960c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); 4970c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); 4980c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); 4990c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); 5000c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); 5010c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); 5020c16b537SWarner Losh 5030c16b537SWarner Losh /*! ZSTD_estimate*() : 5040c16b537SWarner Losh * These functions make it possible to estimate memory usage 5050c16b537SWarner Losh * of a future {D,C}Ctx, before its creation. 5060c16b537SWarner Losh * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one. 5070c16b537SWarner Losh * It will also consider src size to be arbitrarily "large", which is worst case. 5080c16b537SWarner Losh * If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation. 5090c16b537SWarner Losh * ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 510*19fcbaf1SConrad Meyer * 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_nbWorkers is >= 1. 511*19fcbaf1SConrad Meyer * Note : CCtx size estimation is only correct for single-threaded compression. */ 5120c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel); 5130c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); 5140c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); 5150c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); 5160c16b537SWarner Losh 5170c16b537SWarner Losh /*! ZSTD_estimateCStreamSize() : 5180c16b537SWarner Losh * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. 5190c16b537SWarner Losh * It will also consider src size to be arbitrarily "large", which is worst case. 5200c16b537SWarner Losh * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. 5210c16b537SWarner Losh * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 522*19fcbaf1SConrad Meyer * 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_nbWorkers is >= 1. 523*19fcbaf1SConrad Meyer * Note : CStream size estimation is only correct for single-threaded compression. 5240c16b537SWarner Losh * ZSTD_DStream memory budget depends on window Size. 5250c16b537SWarner Losh * This information can be passed manually, using ZSTD_estimateDStreamSize, 5260c16b537SWarner Losh * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); 5270c16b537SWarner Losh * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), 5280c16b537SWarner Losh * an internal ?Dict will be created, which additional size is not estimated here. 5290c16b537SWarner Losh * In this case, get total size by adding ZSTD_estimate?DictSize */ 5300c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); 5310c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); 5320c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); 5330c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); 5340c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); 5350c16b537SWarner Losh 5360c16b537SWarner Losh /*! ZSTD_estimate?DictSize() : 5370c16b537SWarner Losh * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). 538*19fcbaf1SConrad Meyer * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced(). 539*19fcbaf1SConrad Meyer * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller. 5400c16b537SWarner Losh */ 5410c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); 5420c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); 5430c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); 5440c16b537SWarner Losh 545*19fcbaf1SConrad Meyer /*! ZSTD_initStatic*() : 546*19fcbaf1SConrad Meyer * Initialize an object using a pre-allocated fixed-size buffer. 547*19fcbaf1SConrad Meyer * workspace: The memory area to emplace the object into. 548*19fcbaf1SConrad Meyer * Provided pointer *must be 8-bytes aligned*. 549*19fcbaf1SConrad Meyer * Buffer must outlive object. 550*19fcbaf1SConrad Meyer * workspaceSize: Use ZSTD_estimate*Size() to determine 551*19fcbaf1SConrad Meyer * how large workspace must be to support target scenario. 552*19fcbaf1SConrad Meyer * @return : pointer to object (same address as workspace, just different type), 553*19fcbaf1SConrad Meyer * or NULL if error (size too small, incorrect alignment, etc.) 554*19fcbaf1SConrad Meyer * Note : zstd will never resize nor malloc() when using a static buffer. 555*19fcbaf1SConrad Meyer * If the object requires more memory than available, 556*19fcbaf1SConrad Meyer * zstd will just error out (typically ZSTD_error_memory_allocation). 557*19fcbaf1SConrad Meyer * Note 2 : there is no corresponding "free" function. 558*19fcbaf1SConrad Meyer * Since workspace is allocated externally, it must be freed externally too. 559*19fcbaf1SConrad Meyer * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level 560*19fcbaf1SConrad Meyer * into its associated cParams. 561*19fcbaf1SConrad Meyer * Limitation 1 : currently not compatible with internal dictionary creation, triggered by 562*19fcbaf1SConrad Meyer * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict(). 563*19fcbaf1SConrad Meyer * Limitation 2 : static cctx currently not compatible with multi-threading. 564*19fcbaf1SConrad Meyer * Limitation 3 : static dctx is incompatible with legacy support. 565*19fcbaf1SConrad Meyer */ 566*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); 567*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */ 568*19fcbaf1SConrad Meyer 569*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize); 570*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */ 571*19fcbaf1SConrad Meyer 572*19fcbaf1SConrad Meyer ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict( 573*19fcbaf1SConrad Meyer void* workspace, size_t workspaceSize, 574*19fcbaf1SConrad Meyer const void* dict, size_t dictSize, 575*19fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 576*19fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType, 577*19fcbaf1SConrad Meyer ZSTD_compressionParameters cParams); 578*19fcbaf1SConrad Meyer 579*19fcbaf1SConrad Meyer ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict( 580*19fcbaf1SConrad Meyer void* workspace, size_t workspaceSize, 581*19fcbaf1SConrad Meyer const void* dict, size_t dictSize, 582*19fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 583*19fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType); 584*19fcbaf1SConrad Meyer 585*19fcbaf1SConrad Meyer /*! Custom memory allocation : 586*19fcbaf1SConrad Meyer * These prototypes make it possible to pass your own allocation/free functions. 587*19fcbaf1SConrad Meyer * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. 588*19fcbaf1SConrad Meyer * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones. 589*19fcbaf1SConrad Meyer */ 590*19fcbaf1SConrad Meyer typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); 591*19fcbaf1SConrad Meyer typedef void (*ZSTD_freeFunction) (void* opaque, void* address); 592*19fcbaf1SConrad Meyer typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; 593*19fcbaf1SConrad Meyer static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ 594*19fcbaf1SConrad Meyer 595*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); 596*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); 597*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); 598*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); 599*19fcbaf1SConrad Meyer 600*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, 601*19fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 602*19fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType, 603*19fcbaf1SConrad Meyer ZSTD_compressionParameters cParams, 604*19fcbaf1SConrad Meyer ZSTD_customMem customMem); 605*19fcbaf1SConrad Meyer 606*19fcbaf1SConrad Meyer ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, 607*19fcbaf1SConrad Meyer ZSTD_dictLoadMethod_e dictLoadMethod, 608*19fcbaf1SConrad Meyer ZSTD_dictContentType_e dictContentType, 609*19fcbaf1SConrad Meyer ZSTD_customMem customMem); 610*19fcbaf1SConrad Meyer 611*19fcbaf1SConrad Meyer 6120c16b537SWarner Losh 6130c16b537SWarner Losh /*************************************** 6140c16b537SWarner Losh * Advanced compression functions 6150c16b537SWarner Losh ***************************************/ 6160c16b537SWarner Losh 6170c16b537SWarner Losh /*! ZSTD_createCDict_byReference() : 6180c16b537SWarner Losh * Create a digested dictionary for compression 6190c16b537SWarner Losh * Dictionary content is simply referenced, and therefore stays in dictBuffer. 6200c16b537SWarner Losh * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */ 6210c16b537SWarner Losh ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); 6220c16b537SWarner Losh 6230c16b537SWarner Losh /*! ZSTD_getCParams() : 6240c16b537SWarner Losh * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. 6250c16b537SWarner Losh * `estimatedSrcSize` value is optional, select 0 if not known */ 6260c16b537SWarner Losh ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 6270c16b537SWarner Losh 6280c16b537SWarner Losh /*! ZSTD_getParams() : 6290c16b537SWarner Losh * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. 630052d3c12SConrad Meyer * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */ 6310c16b537SWarner Losh ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 6320c16b537SWarner Losh 6330c16b537SWarner Losh /*! ZSTD_checkCParams() : 6340c16b537SWarner Losh * Ensure param values remain within authorized range */ 6350c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); 6360c16b537SWarner Losh 6370c16b537SWarner Losh /*! ZSTD_adjustCParams() : 6380c16b537SWarner Losh * optimize params for a given `srcSize` and `dictSize`. 6390c16b537SWarner Losh * both values are optional, select `0` if unknown. */ 6400c16b537SWarner Losh ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); 6410c16b537SWarner Losh 6420c16b537SWarner Losh /*! ZSTD_compress_advanced() : 6430c16b537SWarner Losh * Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */ 6440c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, 6450c16b537SWarner Losh void* dst, size_t dstCapacity, 6460c16b537SWarner Losh const void* src, size_t srcSize, 6470c16b537SWarner Losh const void* dict,size_t dictSize, 6480c16b537SWarner Losh ZSTD_parameters params); 6490c16b537SWarner Losh 6500c16b537SWarner Losh /*! ZSTD_compress_usingCDict_advanced() : 6510c16b537SWarner Losh * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */ 6520c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, 6530c16b537SWarner Losh void* dst, size_t dstCapacity, 6540c16b537SWarner Losh const void* src, size_t srcSize, 6550c16b537SWarner Losh const ZSTD_CDict* cdict, ZSTD_frameParameters fParams); 6560c16b537SWarner Losh 6570c16b537SWarner Losh 6580c16b537SWarner Losh /*--- Advanced decompression functions ---*/ 6590c16b537SWarner Losh 6600c16b537SWarner Losh /*! ZSTD_isFrame() : 6610c16b537SWarner Losh * Tells if the content of `buffer` starts with a valid Frame Identifier. 6620c16b537SWarner Losh * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 6630c16b537SWarner Losh * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 6640c16b537SWarner Losh * Note 3 : Skippable Frame Identifiers are considered valid. */ 6650c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); 6660c16b537SWarner Losh 6670c16b537SWarner Losh /*! ZSTD_createDDict_byReference() : 6680c16b537SWarner Losh * Create a digested dictionary, ready to start decompression operation without startup delay. 6690c16b537SWarner Losh * Dictionary content is referenced, and therefore stays in dictBuffer. 6700c16b537SWarner Losh * It is important that dictBuffer outlives DDict, 6710c16b537SWarner Losh * it must remain read accessible throughout the lifetime of DDict */ 6720c16b537SWarner Losh ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); 6730c16b537SWarner Losh 6740c16b537SWarner Losh 6750c16b537SWarner Losh /*! ZSTD_getDictID_fromDict() : 6760c16b537SWarner Losh * Provides the dictID stored within dictionary. 6770c16b537SWarner Losh * if @return == 0, the dictionary is not conformant with Zstandard specification. 6780c16b537SWarner Losh * It can still be loaded, but as a content-only dictionary. */ 6790c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); 6800c16b537SWarner Losh 6810c16b537SWarner Losh /*! ZSTD_getDictID_fromDDict() : 6820c16b537SWarner Losh * Provides the dictID of the dictionary loaded into `ddict`. 6830c16b537SWarner Losh * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 6840c16b537SWarner Losh * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ 6850c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); 6860c16b537SWarner Losh 6870c16b537SWarner Losh /*! ZSTD_getDictID_fromFrame() : 6880c16b537SWarner Losh * Provides the dictID required to decompressed the frame stored within `src`. 6890c16b537SWarner Losh * If @return == 0, the dictID could not be decoded. 6900c16b537SWarner Losh * This could for one of the following reasons : 6910c16b537SWarner Losh * - The frame does not require a dictionary to be decoded (most common case). 6920c16b537SWarner Losh * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. 6930c16b537SWarner Losh * Note : this use case also happens when using a non-conformant dictionary. 6940c16b537SWarner Losh * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). 6950c16b537SWarner Losh * - This is not a Zstandard frame. 6960c16b537SWarner Losh * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */ 6970c16b537SWarner Losh ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); 6980c16b537SWarner Losh 6990c16b537SWarner Losh 7000c16b537SWarner Losh /******************************************************************** 7010c16b537SWarner Losh * Advanced streaming functions 7020c16b537SWarner Losh ********************************************************************/ 7030c16b537SWarner Losh 7040c16b537SWarner Losh /*===== Advanced Streaming compression functions =====*/ 705052d3c12SConrad 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. */ 7060c16b537SWarner 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.*/ 7070c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, 708052d3c12SConrad 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. */ 7090c16b537SWarner 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 */ 710052d3c12SConrad 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. */ 7110c16b537SWarner Losh 7120c16b537SWarner Losh /*! ZSTD_resetCStream() : 7130c16b537SWarner Losh * start a new compression job, using same parameters from previous job. 7140c16b537SWarner Losh * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.. 7150c16b537SWarner Losh * Note that zcs must be init at least once before using ZSTD_resetCStream(). 716052d3c12SConrad Meyer * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. 7170c16b537SWarner Losh * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. 718052d3c12SConrad Meyer * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, 719*19fcbaf1SConrad Meyer * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead. 7200c16b537SWarner Losh * @return : 0, or an error code (which can be tested using ZSTD_isError()) */ 7210c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); 7220c16b537SWarner Losh 7230c16b537SWarner Losh 724*19fcbaf1SConrad Meyer typedef struct { 725*19fcbaf1SConrad Meyer unsigned long long ingested; 726*19fcbaf1SConrad Meyer unsigned long long consumed; 727*19fcbaf1SConrad Meyer unsigned long long produced; 728*19fcbaf1SConrad Meyer } ZSTD_frameProgression; 729*19fcbaf1SConrad Meyer 730*19fcbaf1SConrad Meyer /* ZSTD_getFrameProgression(): 731*19fcbaf1SConrad Meyer * tells how much data has been ingested (read from input) 732*19fcbaf1SConrad Meyer * consumed (input actually compressed) and produced (output) for current frame. 733*19fcbaf1SConrad Meyer * Therefore, (ingested - consumed) is amount of input data buffered internally, not yet compressed. 734*19fcbaf1SConrad Meyer * Can report progression inside worker threads (multi-threading and non-blocking mode). 735*19fcbaf1SConrad Meyer */ 736*19fcbaf1SConrad Meyer ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx); 737*19fcbaf1SConrad Meyer 738*19fcbaf1SConrad Meyer 739*19fcbaf1SConrad Meyer 7400c16b537SWarner Losh /*===== Advanced Streaming decompression functions =====*/ 7410c16b537SWarner Losh typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e; 7420c16b537SWarner 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 */ 7430c16b537SWarner 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 */ 7440c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict is referenced, it must outlive decompression session */ 7450c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */ 7460c16b537SWarner Losh 7470c16b537SWarner Losh 7480c16b537SWarner Losh /********************************************************************* 7490c16b537SWarner Losh * Buffer-less and synchronous inner streaming functions 7500c16b537SWarner Losh * 7510c16b537SWarner Losh * This is an advanced API, giving full control over buffer management, for users which need direct control over memory. 7520c16b537SWarner Losh * But it's also a complex one, with several restrictions, documented below. 7530c16b537SWarner Losh * Prefer normal streaming API for an easier experience. 7540c16b537SWarner Losh ********************************************************************* */ 7550c16b537SWarner Losh 7560c16b537SWarner Losh /** 7570c16b537SWarner Losh Buffer-less streaming compression (synchronous mode) 7580c16b537SWarner Losh 7590c16b537SWarner Losh A ZSTD_CCtx object is required to track streaming operations. 7600c16b537SWarner Losh Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. 7610c16b537SWarner Losh ZSTD_CCtx object can be re-used multiple times within successive compression operations. 7620c16b537SWarner Losh 7630c16b537SWarner Losh Start by initializing a context. 7640c16b537SWarner Losh Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, 7650c16b537SWarner Losh or ZSTD_compressBegin_advanced(), for finer parameter control. 7660c16b537SWarner Losh It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() 7670c16b537SWarner Losh 7680c16b537SWarner Losh Then, consume your input using ZSTD_compressContinue(). 7690c16b537SWarner Losh There are some important considerations to keep in mind when using this advanced function : 7700c16b537SWarner Losh - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. 7710c16b537SWarner Losh - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. 7720c16b537SWarner Losh - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. 7730c16b537SWarner Losh Worst case evaluation is provided by ZSTD_compressBound(). 7740c16b537SWarner Losh ZSTD_compressContinue() doesn't guarantee recover after a failed compression. 7750c16b537SWarner Losh - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). 7760c16b537SWarner Losh It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) 7770c16b537SWarner Losh - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. 7780c16b537SWarner Losh In which case, it will "discard" the relevant memory section from its history. 7790c16b537SWarner Losh 7800c16b537SWarner Losh Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. 7810c16b537SWarner Losh It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. 7820c16b537SWarner Losh Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. 7830c16b537SWarner Losh 7840c16b537SWarner Losh `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. 7850c16b537SWarner Losh */ 7860c16b537SWarner Losh 7870c16b537SWarner Losh /*===== Buffer-less streaming compression functions =====*/ 7880c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); 7890c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 790052d3c12SConrad 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 */ 7910c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */ 792052d3c12SConrad 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 */ 793052d3c12SConrad 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 */ 7940c16b537SWarner Losh 7950c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 7960c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 7970c16b537SWarner Losh 7980c16b537SWarner Losh 7990c16b537SWarner Losh /*- 8000c16b537SWarner Losh Buffer-less streaming decompression (synchronous mode) 8010c16b537SWarner Losh 8020c16b537SWarner Losh A ZSTD_DCtx object is required to track streaming operations. 8030c16b537SWarner Losh Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 8040c16b537SWarner Losh A ZSTD_DCtx object can be re-used multiple times. 8050c16b537SWarner Losh 8060c16b537SWarner Losh First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). 8070c16b537SWarner Losh Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. 8080c16b537SWarner Losh Data fragment must be large enough to ensure successful decoding. 8090c16b537SWarner Losh `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. 8100c16b537SWarner Losh @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. 8110c16b537SWarner Losh >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. 8120c16b537SWarner Losh errorCode, which can be tested using ZSTD_isError(). 8130c16b537SWarner Losh 8140c16b537SWarner Losh It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, 8150c16b537SWarner Losh such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). 8160c16b537SWarner Losh Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. 8170c16b537SWarner Losh As a consequence, check that values remain within valid application range. 8180c16b537SWarner Losh For example, do not allocate memory blindly, check that `windowSize` is within expectation. 8190c16b537SWarner Losh Each application can set its own limits, depending on local restrictions. 8200c16b537SWarner Losh For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. 8210c16b537SWarner Losh 8220c16b537SWarner Losh ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. 8230c16b537SWarner Losh ZSTD_decompressContinue() is very sensitive to contiguity, 8240c16b537SWarner Losh if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, 8250c16b537SWarner Losh or that previous contiguous segment is large enough to properly handle maximum back-reference distance. 8260c16b537SWarner Losh There are multiple ways to guarantee this condition. 8270c16b537SWarner Losh 8280c16b537SWarner Losh The most memory efficient way is to use a round buffer of sufficient size. 8290c16b537SWarner Losh Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), 8300c16b537SWarner Losh which can @return an error code if required value is too large for current system (in 32-bits mode). 8310c16b537SWarner Losh In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, 8320c16b537SWarner Losh up to the moment there is not enough room left in the buffer to guarantee decoding another full block, 8330c16b537SWarner Losh which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. 8340c16b537SWarner Losh At which point, decoding can resume from the beginning of the buffer. 8350c16b537SWarner Losh Note that already decoded data stored in the buffer should be flushed before being overwritten. 8360c16b537SWarner Losh 8370c16b537SWarner Losh There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. 8380c16b537SWarner Losh 8390c16b537SWarner Losh Finally, if you control the compression process, you can also ignore all buffer size rules, 8400c16b537SWarner Losh as long as the encoder and decoder progress in "lock-step", 8410c16b537SWarner Losh aka use exactly the same buffer sizes, break contiguity at the same place, etc. 8420c16b537SWarner Losh 8430c16b537SWarner Losh Once buffers are setup, start decompression, with ZSTD_decompressBegin(). 8440c16b537SWarner Losh If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). 8450c16b537SWarner Losh 8460c16b537SWarner Losh Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 8470c16b537SWarner Losh ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 8480c16b537SWarner Losh ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. 8490c16b537SWarner Losh 8500c16b537SWarner Losh @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). 8510c16b537SWarner Losh It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. 8520c16b537SWarner Losh It can also be an error code, which can be tested with ZSTD_isError(). 8530c16b537SWarner Losh 8540c16b537SWarner Losh A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 8550c16b537SWarner Losh Context can then be reset to start a new decompression. 8560c16b537SWarner Losh 8570c16b537SWarner Losh Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). 8580c16b537SWarner Losh This information is not required to properly decode a frame. 8590c16b537SWarner Losh 8600c16b537SWarner Losh == Special case : skippable frames == 8610c16b537SWarner Losh 8620c16b537SWarner Losh Skippable frames allow integration of user-defined data into a flow of concatenated frames. 8630c16b537SWarner Losh Skippable frames will be ignored (skipped) by decompressor. 8640c16b537SWarner Losh The format of skippable frames is as follows : 8650c16b537SWarner Losh a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F 8660c16b537SWarner Losh b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits 8670c16b537SWarner Losh c) Frame Content - any content (User Data) of length equal to Frame Size 8680c16b537SWarner Losh For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. 8690c16b537SWarner Losh For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. 8700c16b537SWarner Losh */ 8710c16b537SWarner Losh 8720c16b537SWarner Losh /*===== Buffer-less streaming decompression functions =====*/ 8730c16b537SWarner Losh typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; 8740c16b537SWarner Losh typedef struct { 8750c16b537SWarner Losh unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */ 8760c16b537SWarner Losh unsigned long long windowSize; /* can be very large, up to <= frameContentSize */ 8770c16b537SWarner Losh unsigned blockSizeMax; 8780c16b537SWarner Losh ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */ 8790c16b537SWarner Losh unsigned headerSize; 8800c16b537SWarner Losh unsigned dictID; 8810c16b537SWarner Losh unsigned checksumFlag; 8820c16b537SWarner Losh } ZSTD_frameHeader; 8830c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 8840c16b537SWarner 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 */ 8850c16b537SWarner Losh 8860c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); 8870c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 8880c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 8890c16b537SWarner Losh 8900c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); 8910c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 8920c16b537SWarner Losh 8930c16b537SWarner Losh /* misc */ 8940c16b537SWarner Losh ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); 8950c16b537SWarner Losh typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; 8960c16b537SWarner Losh ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); 8970c16b537SWarner Losh 8980c16b537SWarner Losh 8990c16b537SWarner Losh 9000c16b537SWarner Losh /* ============================================ */ 9010c16b537SWarner Losh /** New advanced API (experimental) */ 9020c16b537SWarner Losh /* ============================================ */ 9030c16b537SWarner Losh 9040c16b537SWarner Losh /* notes on API design : 9050c16b537SWarner Losh * In this proposal, parameters are pushed one by one into an existing context, 9060c16b537SWarner Losh * and then applied on all subsequent compression jobs. 9070c16b537SWarner Losh * When no parameter is ever provided, CCtx is created with compression level ZSTD_CLEVEL_DEFAULT. 9080c16b537SWarner Losh * 909*19fcbaf1SConrad Meyer * This API is intended to replace all others advanced / experimental API entry points. 910*19fcbaf1SConrad Meyer * But it stands a reasonable chance to become "stable", after a reasonable testing period. 9110c16b537SWarner Losh */ 9120c16b537SWarner Losh 9130c16b537SWarner Losh /* note on naming convention : 9140c16b537SWarner Losh * Initially, the API favored names like ZSTD_setCCtxParameter() . 9150c16b537SWarner Losh * In this proposal, convention is changed towards ZSTD_CCtx_setParameter() . 9160c16b537SWarner Losh * The main driver is that it identifies more clearly the target object type. 9170c16b537SWarner Losh * It feels clearer when considering multiple targets : 9180c16b537SWarner Losh * ZSTD_CDict_setParameter() (rather than ZSTD_setCDictParameter()) 9190c16b537SWarner Losh * ZSTD_CCtxParams_setParameter() (rather than ZSTD_setCCtxParamsParameter() ) 9200c16b537SWarner Losh * etc... 9210c16b537SWarner Losh */ 9220c16b537SWarner Losh 9230c16b537SWarner Losh /* note on enum design : 9240c16b537SWarner Losh * All enum will be pinned to explicit values before reaching "stable API" status */ 9250c16b537SWarner Losh 9260c16b537SWarner Losh typedef enum { 927*19fcbaf1SConrad Meyer /* Opened question : should we have a format ZSTD_f_auto ? 928*19fcbaf1SConrad Meyer * Today, it would mean exactly the same as ZSTD_f_zstd1. 929*19fcbaf1SConrad Meyer * But, in the future, should several formats become supported, 9300c16b537SWarner Losh * on the compression side, it would mean "default format". 931*19fcbaf1SConrad Meyer * On the decompression side, it would mean "automatic format detection", 932*19fcbaf1SConrad Meyer * so that ZSTD_f_zstd1 would mean "accept *only* zstd frames". 9330c16b537SWarner Losh * Since meaning is a little different, another option could be to define different enums for compression and decompression. 9340c16b537SWarner Losh * This question could be kept for later, when there are actually multiple formats to support, 9350c16b537SWarner Losh * but there is also the question of pinning enum values, and pinning value `0` is especially important */ 9360c16b537SWarner Losh ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */ 9370c16b537SWarner Losh ZSTD_f_zstd1_magicless, /* Variant of zstd frame format, without initial 4-bytes magic number. 9380c16b537SWarner Losh * Useful to save 4 bytes per generated frame. 9390c16b537SWarner Losh * Decoder cannot recognise automatically this format, requiring instructions. */ 9400c16b537SWarner Losh } ZSTD_format_e; 9410c16b537SWarner Losh 9420c16b537SWarner Losh typedef enum { 9430c16b537SWarner Losh /* compression format */ 9440c16b537SWarner Losh ZSTD_p_format = 10, /* See ZSTD_format_e enum definition. 9450c16b537SWarner Losh * Cast selected format as unsigned for ZSTD_CCtx_setParameter() compatibility. */ 9460c16b537SWarner Losh 9470c16b537SWarner Losh /* compression parameters */ 9480c16b537SWarner Losh ZSTD_p_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table 9490c16b537SWarner Losh * Default level is ZSTD_CLEVEL_DEFAULT==3. 950*19fcbaf1SConrad Meyer * Special: value 0 means "do not change cLevel". 951*19fcbaf1SConrad Meyer * Note 1 : it's possible to pass a negative compression level by casting it to unsigned type. 952*19fcbaf1SConrad Meyer * Note 2 : setting a level sets all default values of other compression parameters. 953*19fcbaf1SConrad Meyer * Note 3 : setting compressionLevel automatically updates ZSTD_p_compressLiterals. */ 9540c16b537SWarner Losh ZSTD_p_windowLog, /* Maximum allowed back-reference distance, expressed as power of 2. 9550c16b537SWarner Losh * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. 956*19fcbaf1SConrad Meyer * Special: value 0 means "use default windowLog". 9570c16b537SWarner Losh * Note: Using a window size greater than ZSTD_MAXWINDOWSIZE_DEFAULT (default: 2^27) 958*19fcbaf1SConrad Meyer * requires explicitly allowing such window size during decompression stage. */ 9590c16b537SWarner Losh ZSTD_p_hashLog, /* Size of the probe table, as a power of 2. 9600c16b537SWarner Losh * Resulting table size is (1 << (hashLog+2)). 9610c16b537SWarner Losh * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. 9620c16b537SWarner Losh * Larger tables improve compression ratio of strategies <= dFast, 9630c16b537SWarner Losh * and improve speed of strategies > dFast. 964*19fcbaf1SConrad Meyer * Special: value 0 means "use default hashLog". */ 9650c16b537SWarner Losh ZSTD_p_chainLog, /* Size of the full-search table, as a power of 2. 9660c16b537SWarner Losh * Resulting table size is (1 << (chainLog+2)). 9670c16b537SWarner Losh * Larger tables result in better and slower compression. 9680c16b537SWarner Losh * This parameter is useless when using "fast" strategy. 969*19fcbaf1SConrad Meyer * Special: value 0 means "use default chainLog". */ 9700c16b537SWarner Losh ZSTD_p_searchLog, /* Number of search attempts, as a power of 2. 9710c16b537SWarner Losh * More attempts result in better and slower compression. 9720c16b537SWarner Losh * This parameter is useless when using "fast" and "dFast" strategies. 973*19fcbaf1SConrad Meyer * Special: value 0 means "use default searchLog". */ 9740c16b537SWarner Losh ZSTD_p_minMatch, /* Minimum size of searched matches (note : repCode matches can be smaller). 9750c16b537SWarner Losh * Larger values make faster compression and decompression, but decrease ratio. 9760c16b537SWarner Losh * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX. 9770c16b537SWarner Losh * Note that currently, for all strategies < btopt, effective minimum is 4. 978*19fcbaf1SConrad Meyer * , for all strategies > fast, effective maximum is 6. 979*19fcbaf1SConrad Meyer * Special: value 0 means "use default minMatchLength". */ 980*19fcbaf1SConrad Meyer ZSTD_p_targetLength, /* Impact of this field depends on strategy. 981*19fcbaf1SConrad Meyer * For strategies btopt & btultra: 9820c16b537SWarner Losh * Length of Match considered "good enough" to stop search. 983*19fcbaf1SConrad Meyer * Larger values make compression stronger, and slower. 984*19fcbaf1SConrad Meyer * For strategy fast: 985*19fcbaf1SConrad Meyer * Distance between match sampling. 986*19fcbaf1SConrad Meyer * Larger values make compression faster, and weaker. 987*19fcbaf1SConrad Meyer * Special: value 0 means "use default targetLength". */ 9880c16b537SWarner Losh ZSTD_p_compressionStrategy, /* See ZSTD_strategy enum definition. 9890c16b537SWarner Losh * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility. 9900c16b537SWarner Losh * The higher the value of selected strategy, the more complex it is, 9910c16b537SWarner Losh * resulting in stronger and slower compression. 992*19fcbaf1SConrad Meyer * Special: value 0 means "use default strategy". */ 993*19fcbaf1SConrad Meyer 994*19fcbaf1SConrad Meyer ZSTD_p_enableLongDistanceMatching=160, /* Enable long distance matching. 995*19fcbaf1SConrad Meyer * This parameter is designed to improve compression ratio 996*19fcbaf1SConrad Meyer * for large inputs, by finding large matches at long distance. 997*19fcbaf1SConrad Meyer * It increases memory usage and window size. 998*19fcbaf1SConrad Meyer * Note: enabling this parameter increases ZSTD_p_windowLog to 128 MB 999*19fcbaf1SConrad Meyer * except when expressly set to a different value. */ 1000*19fcbaf1SConrad Meyer ZSTD_p_ldmHashLog, /* Size of the table for long distance matching, as a power of 2. 1001*19fcbaf1SConrad Meyer * Larger values increase memory usage and compression ratio, 1002*19fcbaf1SConrad Meyer * but decrease compression speed. 1003*19fcbaf1SConrad Meyer * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX 1004*19fcbaf1SConrad Meyer * default: windowlog - 7. 1005*19fcbaf1SConrad Meyer * Special: value 0 means "automatically determine hashlog". */ 1006*19fcbaf1SConrad Meyer ZSTD_p_ldmMinMatch, /* Minimum match size for long distance matcher. 1007*19fcbaf1SConrad Meyer * Larger/too small values usually decrease compression ratio. 1008*19fcbaf1SConrad Meyer * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX. 1009*19fcbaf1SConrad Meyer * Special: value 0 means "use default value" (default: 64). */ 1010*19fcbaf1SConrad Meyer ZSTD_p_ldmBucketSizeLog, /* Log size of each bucket in the LDM hash table for collision resolution. 1011*19fcbaf1SConrad Meyer * Larger values improve collision resolution but decrease compression speed. 1012*19fcbaf1SConrad Meyer * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX . 1013*19fcbaf1SConrad Meyer * Special: value 0 means "use default value" (default: 3). */ 1014*19fcbaf1SConrad Meyer ZSTD_p_ldmHashEveryLog, /* Frequency of inserting/looking up entries in the LDM hash table. 1015*19fcbaf1SConrad Meyer * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN). 1016*19fcbaf1SConrad Meyer * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage. 1017*19fcbaf1SConrad Meyer * Larger values improve compression speed. 1018*19fcbaf1SConrad Meyer * Deviating far from default value will likely result in a compression ratio decrease. 1019*19fcbaf1SConrad Meyer * Special: value 0 means "automatically determine hashEveryLog". */ 10200c16b537SWarner Losh 10210c16b537SWarner Losh /* frame parameters */ 1022052d3c12SConrad Meyer ZSTD_p_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1) 1023052d3c12SConrad Meyer * Content size must be known at the beginning of compression, 1024052d3c12SConrad Meyer * it is provided using ZSTD_CCtx_setPledgedSrcSize() */ 10250c16b537SWarner Losh ZSTD_p_checksumFlag, /* A 32-bits checksum of content is written at end of frame (default:0) */ 1026052d3c12SConrad Meyer ZSTD_p_dictIDFlag, /* When applicable, dictionary's ID is written into frame header (default:1) */ 10270c16b537SWarner Losh 10280c16b537SWarner Losh /* multi-threading parameters */ 1029*19fcbaf1SConrad Meyer /* These parameters are only useful if multi-threading is enabled (ZSTD_MULTITHREAD). 1030*19fcbaf1SConrad Meyer * They return an error otherwise. */ 1031*19fcbaf1SConrad Meyer ZSTD_p_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel. 1032*19fcbaf1SConrad Meyer * When nbWorkers >= 1, triggers asynchronous mode : 1033*19fcbaf1SConrad Meyer * ZSTD_compress_generic() consumes some input, flush some output if possible, and immediately gives back control to caller, 1034*19fcbaf1SConrad Meyer * while compression work is performed in parallel, within worker threads. 1035*19fcbaf1SConrad Meyer * (note : a strong exception to this rule is when first invocation sets ZSTD_e_end : it becomes a blocking call). 1036*19fcbaf1SConrad Meyer * More workers improve speed, but also increase memory usage. 1037*19fcbaf1SConrad Meyer * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */ 1038*19fcbaf1SConrad Meyer ZSTD_p_jobSize, /* Size of a compression job. This value is enforced only in non-blocking mode. 1039*19fcbaf1SConrad Meyer * Each compression job is completed in parallel, so this value indirectly controls the nb of active threads. 10400c16b537SWarner Losh * 0 means default, which is dynamically determined based on compression parameters. 1041*19fcbaf1SConrad Meyer * Job size must be a minimum of overlapSize, or 1 MB, whichever is largest. 10420c16b537SWarner Losh * The minimum size is automatically and transparently enforced */ 10430c16b537SWarner Losh ZSTD_p_overlapSizeLog, /* Size of previous input reloaded at the beginning of each job. 10440c16b537SWarner Losh * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */ 10450c16b537SWarner Losh 1046*19fcbaf1SConrad Meyer /* =================================================================== */ 1047*19fcbaf1SConrad Meyer /* experimental parameters - no stability guaranteed */ 1048*19fcbaf1SConrad Meyer /* =================================================================== */ 1049*19fcbaf1SConrad Meyer 1050*19fcbaf1SConrad Meyer ZSTD_p_compressLiterals=1000, /* control huffman compression of literals (enabled) by default. 1051*19fcbaf1SConrad Meyer * disabling it improves speed and decreases compression ratio by a large amount. 1052*19fcbaf1SConrad Meyer * note : this setting is automatically updated when changing compression level. 1053*19fcbaf1SConrad Meyer * positive compression levels set ZSTD_p_compressLiterals to 1. 1054*19fcbaf1SConrad Meyer * negative compression levels set ZSTD_p_compressLiterals to 0. */ 1055*19fcbaf1SConrad Meyer 10560c16b537SWarner Losh ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize, 10570c16b537SWarner Losh * even when referencing into Dictionary content (default:0) */ 10580c16b537SWarner Losh 10590c16b537SWarner Losh } ZSTD_cParameter; 10600c16b537SWarner Losh 10610c16b537SWarner Losh 10620c16b537SWarner Losh /*! ZSTD_CCtx_setParameter() : 10630c16b537SWarner Losh * Set one compression parameter, selected by enum ZSTD_cParameter. 1064*19fcbaf1SConrad Meyer * Setting a parameter is generally only possible during frame initialization (before starting compression), 1065*19fcbaf1SConrad Meyer * except for a few exceptions which can be updated during compression: compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy. 10660c16b537SWarner Losh * Note : when `value` is an enum, cast it to unsigned for proper type checking. 1067*19fcbaf1SConrad Meyer * @result : informational value (typically, value being set clamped correctly), 1068052d3c12SConrad Meyer * or an error code (which can be tested with ZSTD_isError()). */ 10690c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value); 10700c16b537SWarner Losh 10710c16b537SWarner Losh /*! ZSTD_CCtx_setPledgedSrcSize() : 10720c16b537SWarner Losh * Total input data size to be compressed as a single frame. 10730c16b537SWarner Losh * This value will be controlled at the end, and result in error if not respected. 10740c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 10750c16b537SWarner Losh * Note 1 : 0 means zero, empty. 10760c16b537SWarner Losh * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. 1077052d3c12SConrad Meyer * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new compression job. 10780c16b537SWarner Losh * Note 2 : If all data is provided and consumed in a single round, 10790c16b537SWarner Losh * this value is overriden by srcSize instead. */ 10800c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); 10810c16b537SWarner Losh 10820c16b537SWarner Losh /*! ZSTD_CCtx_loadDictionary() : 1083*19fcbaf1SConrad Meyer * Create an internal CDict from `dict` buffer. 1084*19fcbaf1SConrad Meyer * Decompression will have to use same dictionary. 10850c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1086*19fcbaf1SConrad Meyer * Special: Adding a NULL (or 0-size) dictionary invalidates previous dictionary, 10870c16b537SWarner Losh * meaning "return to no-dictionary mode". 1088*19fcbaf1SConrad Meyer * Note 1 : Dictionary will be used for all future compression jobs. 1089*19fcbaf1SConrad Meyer * To return to "no-dictionary" situation, load a NULL dictionary 10900c16b537SWarner Losh * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters. 10910c16b537SWarner Losh * For this reason, compression parameters cannot be changed anymore after loading a dictionary. 1092*19fcbaf1SConrad Meyer * It's also a CPU consuming operation, with non-negligible impact on latency. 1093*19fcbaf1SConrad Meyer * Note 3 :`dict` content will be copied internally. 1094*19fcbaf1SConrad Meyer * Use ZSTD_CCtx_loadDictionary_byReference() to reference dictionary content instead. 1095*19fcbaf1SConrad Meyer * In such a case, dictionary buffer must outlive its users. 1096*19fcbaf1SConrad Meyer * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced() 1097*19fcbaf1SConrad Meyer * to precisely select how dictionary content must be interpreted. */ 10980c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 10990c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 1100*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 11010c16b537SWarner Losh 11020c16b537SWarner Losh 11030c16b537SWarner Losh /*! ZSTD_CCtx_refCDict() : 11040c16b537SWarner Losh * Reference a prepared dictionary, to be used for all next compression jobs. 11050c16b537SWarner Losh * Note that compression parameters are enforced from within CDict, 11060c16b537SWarner Losh * and supercede any compression parameter previously set within CCtx. 11070c16b537SWarner Losh * The dictionary will remain valid for future compression jobs using same CCtx. 11080c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 11090c16b537SWarner Losh * Special : adding a NULL CDict means "return to no-dictionary mode". 11100c16b537SWarner Losh * Note 1 : Currently, only one dictionary can be managed. 11110c16b537SWarner Losh * Adding a new dictionary effectively "discards" any previous one. 1112*19fcbaf1SConrad Meyer * Note 2 : CDict is just referenced, its lifetime must outlive CCtx. */ 11130c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); 11140c16b537SWarner Losh 11150c16b537SWarner Losh /*! ZSTD_CCtx_refPrefix() : 11160c16b537SWarner Losh * Reference a prefix (single-usage dictionary) for next compression job. 11170c16b537SWarner Losh * Decompression need same prefix to properly regenerate data. 11180c16b537SWarner Losh * Prefix is **only used once**. Tables are discarded at end of compression job. 11190c16b537SWarner Losh * Subsequent compression jobs will be done without prefix (if none is explicitly referenced). 11200c16b537SWarner Losh * If there is a need to use same prefix multiple times, consider embedding it into a ZSTD_CDict instead. 11210c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 11220c16b537SWarner Losh * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary 11230c16b537SWarner Losh * Note 1 : Prefix buffer is referenced. It must outlive compression job. 11240c16b537SWarner Losh * Note 2 : Referencing a prefix involves building tables, which are dependent on compression parameters. 1125*19fcbaf1SConrad Meyer * It's a CPU consuming operation, with non-negligible impact on latency. 1126*19fcbaf1SConrad Meyer * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent). 1127*19fcbaf1SConrad Meyer * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. */ 11280c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize); 1129*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 1130*19fcbaf1SConrad Meyer 1131*19fcbaf1SConrad Meyer /*! ZSTD_CCtx_reset() : 1132*19fcbaf1SConrad Meyer * Return a CCtx to clean state. 1133*19fcbaf1SConrad Meyer * Useful after an error, or to interrupt an ongoing compression job and start a new one. 1134*19fcbaf1SConrad Meyer * Any internal data not yet flushed is cancelled. 1135*19fcbaf1SConrad Meyer * Dictionary (if any) is dropped. 1136*19fcbaf1SConrad Meyer * All parameters are back to default values. 1137*19fcbaf1SConrad Meyer * It's possible to modify compression parameters after a reset. 1138*19fcbaf1SConrad Meyer */ 1139*19fcbaf1SConrad Meyer ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx); 11400c16b537SWarner Losh 11410c16b537SWarner Losh 11420c16b537SWarner Losh 11430c16b537SWarner Losh typedef enum { 1144*19fcbaf1SConrad Meyer ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal conditions */ 11450c16b537SWarner Losh ZSTD_e_flush, /* flush any data provided so far - frame will continue, future data can still reference previous data for better compression */ 11460c16b537SWarner Losh ZSTD_e_end /* flush any remaining data and close current frame. Any additional data starts a new frame. */ 11470c16b537SWarner Losh } ZSTD_EndDirective; 11480c16b537SWarner Losh 11490c16b537SWarner Losh /*! ZSTD_compress_generic() : 11500c16b537SWarner Losh * Behave about the same as ZSTD_compressStream. To note : 11510c16b537SWarner Losh * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter() 11520c16b537SWarner Losh * - Compression parameters cannot be changed once compression is started. 11530c16b537SWarner Losh * - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize 11540c16b537SWarner Losh * - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. 1155052d3c12SConrad Meyer * - In single-thread mode (default), function is blocking : it completed its job before returning to caller. 1156052d3c12SConrad Meyer * - In multi-thread mode, function is non-blocking : it just acquires a copy of input, and distribute job to internal worker threads, 1157052d3c12SConrad Meyer * and then immediately returns, just indicating that there is some data remaining to be flushed. 1158052d3c12SConrad Meyer * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. 1159052d3c12SConrad 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. 1160*19fcbaf1SConrad Meyer * - @return provides a minimum amount of data remaining to be flushed from internal buffers 11610c16b537SWarner Losh * or an error code, which can be tested using ZSTD_isError(). 1162052d3c12SConrad Meyer * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. 1163*19fcbaf1SConrad Meyer * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. 1164*19fcbaf1SConrad Meyer * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. 1165052d3c12SConrad Meyer * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), 11660c16b537SWarner Losh * only ZSTD_e_end or ZSTD_e_flush operations are allowed. 1167052d3c12SConrad Meyer * Before starting a new compression job, or changing compression parameters, 1168052d3c12SConrad Meyer * it is required to fully flush internal buffers. 11690c16b537SWarner Losh */ 11700c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, 11710c16b537SWarner Losh ZSTD_outBuffer* output, 11720c16b537SWarner Losh ZSTD_inBuffer* input, 11730c16b537SWarner Losh ZSTD_EndDirective endOp); 11740c16b537SWarner Losh 11750c16b537SWarner Losh 11760c16b537SWarner Losh /*! ZSTD_compress_generic_simpleArgs() : 11770c16b537SWarner Losh * Same as ZSTD_compress_generic(), 11780c16b537SWarner Losh * but using only integral types as arguments. 11790c16b537SWarner Losh * Argument list is larger than ZSTD_{in,out}Buffer, 11800c16b537SWarner Losh * but can be helpful for binders from dynamic languages 11810c16b537SWarner Losh * which have troubles handling structures containing memory pointers. 11820c16b537SWarner Losh */ 11830c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_compress_generic_simpleArgs ( 11840c16b537SWarner Losh ZSTD_CCtx* cctx, 11850c16b537SWarner Losh void* dst, size_t dstCapacity, size_t* dstPos, 11860c16b537SWarner Losh const void* src, size_t srcSize, size_t* srcPos, 11870c16b537SWarner Losh ZSTD_EndDirective endOp); 11880c16b537SWarner Losh 11890c16b537SWarner Losh 11900c16b537SWarner Losh /*! ZSTD_CCtx_params : 11910c16b537SWarner Losh * Quick howto : 11920c16b537SWarner Losh * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure 11930c16b537SWarner Losh * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into 11940c16b537SWarner Losh * an existing ZSTD_CCtx_params structure. 11950c16b537SWarner Losh * This is similar to 11960c16b537SWarner Losh * ZSTD_CCtx_setParameter(). 11970c16b537SWarner Losh * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to 11980c16b537SWarner Losh * an existing CCtx. 11990c16b537SWarner Losh * These parameters will be applied to 12000c16b537SWarner Losh * all subsequent compression jobs. 12010c16b537SWarner Losh * - ZSTD_compress_generic() : Do compression using the CCtx. 12020c16b537SWarner Losh * - ZSTD_freeCCtxParams() : Free the memory. 12030c16b537SWarner Losh * 12040c16b537SWarner Losh * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 12050c16b537SWarner Losh * for static allocation for single-threaded compression. 12060c16b537SWarner Losh */ 12070c16b537SWarner Losh ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); 1208*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); 12090c16b537SWarner Losh 1210*19fcbaf1SConrad Meyer 1211*19fcbaf1SConrad Meyer /*! ZSTD_CCtxParams_reset() : 1212*19fcbaf1SConrad Meyer * Reset params to default values. 12130c16b537SWarner Losh */ 1214*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params); 12150c16b537SWarner Losh 1216*19fcbaf1SConrad Meyer /*! ZSTD_CCtxParams_init() : 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 */ 1220*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel); 12210c16b537SWarner Losh 1222*19fcbaf1SConrad Meyer /*! ZSTD_CCtxParams_init_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 */ 1226*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); 12270c16b537SWarner Losh 12280c16b537SWarner Losh 12290c16b537SWarner Losh /*! ZSTD_CCtxParam_setParameter() : 12300c16b537SWarner Losh * Similar to ZSTD_CCtx_setParameter. 12310c16b537SWarner Losh * Set one compression parameter, selected by enum ZSTD_cParameter. 12320c16b537SWarner Losh * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams(). 12330c16b537SWarner Losh * Note : when `value` is an enum, cast it to unsigned for proper type checking. 12340c16b537SWarner Losh * @result : 0, or an error code (which can be tested with ZSTD_isError()). 12350c16b537SWarner Losh */ 12360c16b537SWarner Losh ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value); 12370c16b537SWarner Losh 12380c16b537SWarner Losh /*! ZSTD_CCtx_setParametersUsingCCtxParams() : 12390c16b537SWarner Losh * Apply a set of ZSTD_CCtx_params to the compression context. 1240*19fcbaf1SConrad Meyer * This can be done even after compression is started, 1241*19fcbaf1SConrad Meyer * if nbWorkers==0, this will have no impact until a new compression is started. 1242*19fcbaf1SConrad Meyer * if nbWorkers>=1, new parameters will be picked up at next job, 1243*19fcbaf1SConrad Meyer * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated). 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 */ 1270*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 1271*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 1272*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType); 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 */ 1284*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 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 */ 1298*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize); 1299*19fcbaf1SConrad Meyer ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 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); 1392*19fcbaf1SConrad Meyer 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