1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * BSD 3-Clause Clear License 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * Redistribution and use in source and binary forms, with or without 5*eda14cbcSMatt Macy * modification, are permitted provided that the following conditions are met: 6*eda14cbcSMatt Macy * 7*eda14cbcSMatt Macy * 1. Redistributions of source code must retain the above copyright notice, 8*eda14cbcSMatt Macy * this list of conditions and the following disclaimer. 9*eda14cbcSMatt Macy * 10*eda14cbcSMatt Macy * 2. Redistributions in binary form must reproduce the above copyright notice, 11*eda14cbcSMatt Macy * this list of conditions and the following disclaimer in the documentation 12*eda14cbcSMatt Macy * and/or other materials provided with the distribution. 13*eda14cbcSMatt Macy * 14*eda14cbcSMatt Macy * 3. Neither the name of the copyright holder nor the names of its 15*eda14cbcSMatt Macy * contributors may be used to endorse or promote products derived from this 16*eda14cbcSMatt Macy * software without specific prior written permission. 17*eda14cbcSMatt Macy * 18*eda14cbcSMatt Macy * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*eda14cbcSMatt Macy * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*eda14cbcSMatt Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*eda14cbcSMatt Macy * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*eda14cbcSMatt Macy * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*eda14cbcSMatt Macy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*eda14cbcSMatt Macy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*eda14cbcSMatt Macy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*eda14cbcSMatt Macy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*eda14cbcSMatt Macy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*eda14cbcSMatt Macy * POSSIBILITY OF SUCH DAMAGE. 29*eda14cbcSMatt Macy */ 30*eda14cbcSMatt Macy 31*eda14cbcSMatt Macy /* 32*eda14cbcSMatt Macy * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. All rights reserved. 33*eda14cbcSMatt Macy */ 34*eda14cbcSMatt Macy 35*eda14cbcSMatt Macy #if defined (__cplusplus) 36*eda14cbcSMatt Macy extern "C" { 37*eda14cbcSMatt Macy #endif 38*eda14cbcSMatt Macy 39*eda14cbcSMatt Macy #ifndef ZSTD_H_235446 40*eda14cbcSMatt Macy #define ZSTD_H_235446 41*eda14cbcSMatt Macy 42*eda14cbcSMatt Macy /* ====== Dependency ======*/ 43*eda14cbcSMatt Macy #include <limits.h> /* INT_MAX */ 44*eda14cbcSMatt Macy #include <stddef.h> /* size_t */ 45*eda14cbcSMatt Macy 46*eda14cbcSMatt Macy 47*eda14cbcSMatt Macy /* ===== ZSTDLIB_API : control library symbols visibility ===== */ 48*eda14cbcSMatt Macy #ifndef ZSTDLIB_VISIBILITY 49*eda14cbcSMatt Macy # if defined(__GNUC__) && (__GNUC__ >= 4) 50*eda14cbcSMatt Macy # define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default"))) 51*eda14cbcSMatt Macy # else 52*eda14cbcSMatt Macy # define ZSTDLIB_VISIBILITY 53*eda14cbcSMatt Macy # endif 54*eda14cbcSMatt Macy #endif 55*eda14cbcSMatt Macy #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) 56*eda14cbcSMatt Macy # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY 57*eda14cbcSMatt Macy #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) 58*eda14cbcSMatt Macy # 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.*/ 59*eda14cbcSMatt Macy #else 60*eda14cbcSMatt Macy # define ZSTDLIB_API ZSTDLIB_VISIBILITY 61*eda14cbcSMatt Macy #endif 62*eda14cbcSMatt Macy 63*eda14cbcSMatt Macy 64*eda14cbcSMatt Macy /******************************************************************************* 65*eda14cbcSMatt Macy Introduction 66*eda14cbcSMatt Macy 67*eda14cbcSMatt Macy zstd, short for Zstandard, is a fast lossless compression algorithm, targeting 68*eda14cbcSMatt Macy real-time compression scenarios at zlib-level and better compression ratios. 69*eda14cbcSMatt Macy The zstd compression library provides in-memory compression and decompression 70*eda14cbcSMatt Macy functions. 71*eda14cbcSMatt Macy 72*eda14cbcSMatt Macy The library supports regular compression levels from 1 up to ZSTD_maxCLevel(), 73*eda14cbcSMatt Macy which is currently 22. Levels >= 20, labeled `--ultra`, should be used with 74*eda14cbcSMatt Macy caution, as they require more memory. The library also offers negative 75*eda14cbcSMatt Macy compression levels, which extend the range of speed vs. ratio preferences. 76*eda14cbcSMatt Macy The lower the level, the faster the speed (at the cost of compression). 77*eda14cbcSMatt Macy 78*eda14cbcSMatt Macy Compression can be done in: 79*eda14cbcSMatt Macy - a single step (described as Simple API) 80*eda14cbcSMatt Macy - a single step, reusing a context (described as Explicit context) 81*eda14cbcSMatt Macy - unbounded multiple steps (described as Streaming compression) 82*eda14cbcSMatt Macy 83*eda14cbcSMatt Macy The compression ratio achievable on small data can be highly improved using 84*eda14cbcSMatt Macy a dictionary. Dictionary compression can be performed in: 85*eda14cbcSMatt Macy - a single step (described as Simple dictionary API) 86*eda14cbcSMatt Macy - a single step, reusing a dictionary (described as Bulk-processing 87*eda14cbcSMatt Macy dictionary API) 88*eda14cbcSMatt Macy 89*eda14cbcSMatt Macy Advanced experimental functions can be accessed using 90*eda14cbcSMatt Macy `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h. 91*eda14cbcSMatt Macy 92*eda14cbcSMatt Macy Advanced experimental APIs should never be used with a dynamically-linked 93*eda14cbcSMatt Macy library. They are not "stable"; their definitions or signatures may change in 94*eda14cbcSMatt Macy the future. Only static linking is allowed. 95*eda14cbcSMatt Macy *******************************************************************************/ 96*eda14cbcSMatt Macy 97*eda14cbcSMatt Macy /*------ Version ------*/ 98*eda14cbcSMatt Macy #define ZSTD_VERSION_MAJOR 1 99*eda14cbcSMatt Macy #define ZSTD_VERSION_MINOR 4 100*eda14cbcSMatt Macy #define ZSTD_VERSION_RELEASE 5 101*eda14cbcSMatt Macy 102*eda14cbcSMatt Macy #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) 103*eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to check runtime library version */ 104*eda14cbcSMatt Macy 105*eda14cbcSMatt Macy #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE 106*eda14cbcSMatt Macy #define ZSTD_QUOTE(str) #str 107*eda14cbcSMatt Macy #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) 108*eda14cbcSMatt Macy #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) 109*eda14cbcSMatt Macy ZSTDLIB_API const char* ZSTD_versionString(void); /* requires v1.3.0+ */ 110*eda14cbcSMatt Macy 111*eda14cbcSMatt Macy /* ************************************* 112*eda14cbcSMatt Macy * Default constant 113*eda14cbcSMatt Macy ***************************************/ 114*eda14cbcSMatt Macy #ifndef ZSTD_CLEVEL_DEFAULT 115*eda14cbcSMatt Macy # define ZSTD_CLEVEL_DEFAULT 3 116*eda14cbcSMatt Macy #endif 117*eda14cbcSMatt Macy 118*eda14cbcSMatt Macy /* ************************************* 119*eda14cbcSMatt Macy * Constants 120*eda14cbcSMatt Macy ***************************************/ 121*eda14cbcSMatt Macy 122*eda14cbcSMatt Macy /* All magic numbers are supposed read/written to/from files/memory using little-endian convention */ 123*eda14cbcSMatt Macy #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */ 124*eda14cbcSMatt Macy #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */ 125*eda14cbcSMatt Macy #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */ 126*eda14cbcSMatt Macy #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0 127*eda14cbcSMatt Macy 128*eda14cbcSMatt Macy #define ZSTD_BLOCKSIZELOG_MAX 17 129*eda14cbcSMatt Macy #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) 130*eda14cbcSMatt Macy 131*eda14cbcSMatt Macy 132*eda14cbcSMatt Macy 133*eda14cbcSMatt Macy /*************************************** 134*eda14cbcSMatt Macy * Simple API 135*eda14cbcSMatt Macy ***************************************/ 136*eda14cbcSMatt Macy /*! ZSTD_compress() : 137*eda14cbcSMatt Macy * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. 138*eda14cbcSMatt Macy * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 139*eda14cbcSMatt Macy * @return : compressed size written into `dst` (<= `dstCapacity), 140*eda14cbcSMatt Macy * or an error code if it fails (which can be tested using ZSTD_isError()). */ 141*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, 142*eda14cbcSMatt Macy const void* src, size_t srcSize, 143*eda14cbcSMatt Macy int compressionLevel); 144*eda14cbcSMatt Macy 145*eda14cbcSMatt Macy /*! ZSTD_decompress() : 146*eda14cbcSMatt Macy * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. 147*eda14cbcSMatt Macy * `dstCapacity` is an upper bound of originalSize to regenerate. 148*eda14cbcSMatt Macy * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. 149*eda14cbcSMatt Macy * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 150*eda14cbcSMatt Macy * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ 151*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, 152*eda14cbcSMatt Macy const void* src, size_t compressedSize); 153*eda14cbcSMatt Macy 154*eda14cbcSMatt Macy /*! ZSTD_getFrameContentSize() : requires v1.3.0+ 155*eda14cbcSMatt Macy * `src` should point to the start of a ZSTD encoded frame. 156*eda14cbcSMatt Macy * `srcSize` must be at least as large as the frame header. 157*eda14cbcSMatt Macy * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough. 158*eda14cbcSMatt Macy * @return : - decompressed size of `src` frame content, if known 159*eda14cbcSMatt Macy * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined 160*eda14cbcSMatt Macy * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) 161*eda14cbcSMatt Macy * note 1 : a 0 return value means the frame is valid but "empty". 162*eda14cbcSMatt Macy * note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode. 163*eda14cbcSMatt Macy * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 164*eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 165*eda14cbcSMatt Macy * Optionally, application can rely on some implicit limit, 166*eda14cbcSMatt Macy * as ZSTD_decompress() only needs an upper bound of decompressed size. 167*eda14cbcSMatt Macy * (For example, data could be necessarily cut into blocks <= 16 KB). 168*eda14cbcSMatt Macy * note 3 : decompressed size is always present when compression is completed using single-pass functions, 169*eda14cbcSMatt Macy * such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict(). 170*eda14cbcSMatt Macy * note 4 : decompressed size can be very large (64-bits value), 171*eda14cbcSMatt Macy * potentially larger than what local system can handle as a single memory segment. 172*eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 173*eda14cbcSMatt Macy * note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified. 174*eda14cbcSMatt Macy * Always ensure return value fits within application's authorized limits. 175*eda14cbcSMatt Macy * Each application can set its own limits. 176*eda14cbcSMatt Macy * note 6 : This function replaces ZSTD_getDecompressedSize() */ 177*eda14cbcSMatt Macy #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) 178*eda14cbcSMatt Macy #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) 179*eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); 180*eda14cbcSMatt Macy 181*eda14cbcSMatt Macy /*! ZSTD_getDecompressedSize() : 182*eda14cbcSMatt Macy * NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize(). 183*eda14cbcSMatt Macy * Both functions work the same way, but ZSTD_getDecompressedSize() blends 184*eda14cbcSMatt Macy * "empty", "unknown" and "error" results to the same return value (0), 185*eda14cbcSMatt Macy * while ZSTD_getFrameContentSize() gives them separate return values. 186*eda14cbcSMatt Macy * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */ 187*eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); 188*eda14cbcSMatt Macy 189*eda14cbcSMatt Macy /*! ZSTD_findFrameCompressedSize() : 190*eda14cbcSMatt Macy * `src` should point to the start of a ZSTD frame or skippable frame. 191*eda14cbcSMatt Macy * `srcSize` must be >= first frame size 192*eda14cbcSMatt Macy * @return : the compressed size of the first frame starting at `src`, 193*eda14cbcSMatt Macy * suitable to pass as `srcSize` to `ZSTD_decompress` or similar, 194*eda14cbcSMatt Macy * or an error code if input is invalid */ 195*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); 196*eda14cbcSMatt Macy 197*eda14cbcSMatt Macy 198*eda14cbcSMatt Macy /*====== Helper functions ======*/ 199*eda14cbcSMatt Macy #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 */ 200*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */ 201*eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 202*eda14cbcSMatt Macy ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ 203*eda14cbcSMatt Macy ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */ 204*eda14cbcSMatt Macy ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ 205*eda14cbcSMatt Macy 206*eda14cbcSMatt Macy 207*eda14cbcSMatt Macy /*************************************** 208*eda14cbcSMatt Macy * Explicit context 209*eda14cbcSMatt Macy ***************************************/ 210*eda14cbcSMatt Macy /*= Compression context 211*eda14cbcSMatt Macy * When compressing many times, 212*eda14cbcSMatt Macy * it is recommended to allocate a context just once, 213*eda14cbcSMatt Macy * and re-use it for each successive compression operation. 214*eda14cbcSMatt Macy * This will make workload friendlier for system's memory. 215*eda14cbcSMatt Macy * Note : re-using context is just a speed / resource optimization. 216*eda14cbcSMatt Macy * It doesn't change the compression ratio, which remains identical. 217*eda14cbcSMatt Macy * Note 2 : In multi-threaded environments, 218*eda14cbcSMatt Macy * use one different context per thread for parallel execution. 219*eda14cbcSMatt Macy */ 220*eda14cbcSMatt Macy typedef struct ZSTD_CCtx_s ZSTD_CCtx; 221*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); 222*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); 223*eda14cbcSMatt Macy 224*eda14cbcSMatt Macy /*! ZSTD_compressCCtx() : 225*eda14cbcSMatt Macy * Same as ZSTD_compress(), using an explicit ZSTD_CCtx. 226*eda14cbcSMatt Macy * Important : in order to behave similarly to `ZSTD_compress()`, 227*eda14cbcSMatt Macy * this function compresses at requested compression level, 228*eda14cbcSMatt Macy * __ignoring any other parameter__ . 229*eda14cbcSMatt Macy * If any advanced parameter was set using the advanced API, 230*eda14cbcSMatt Macy * they will all be reset. Only `compressionLevel` remains. 231*eda14cbcSMatt Macy */ 232*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx, 233*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 234*eda14cbcSMatt Macy const void* src, size_t srcSize, 235*eda14cbcSMatt Macy int compressionLevel); 236*eda14cbcSMatt Macy 237*eda14cbcSMatt Macy /*= Decompression context 238*eda14cbcSMatt Macy * When decompressing many times, 239*eda14cbcSMatt Macy * it is recommended to allocate a context only once, 240*eda14cbcSMatt Macy * and re-use it for each successive compression operation. 241*eda14cbcSMatt Macy * This will make workload friendlier for system's memory. 242*eda14cbcSMatt Macy * Use one context per thread for parallel execution. */ 243*eda14cbcSMatt Macy typedef struct ZSTD_DCtx_s ZSTD_DCtx; 244*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); 245*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); 246*eda14cbcSMatt Macy 247*eda14cbcSMatt Macy /*! ZSTD_decompressDCtx() : 248*eda14cbcSMatt Macy * Same as ZSTD_decompress(), 249*eda14cbcSMatt Macy * requires an allocated ZSTD_DCtx. 250*eda14cbcSMatt Macy * Compatible with sticky parameters. 251*eda14cbcSMatt Macy */ 252*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, 253*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 254*eda14cbcSMatt Macy const void* src, size_t srcSize); 255*eda14cbcSMatt Macy 256*eda14cbcSMatt Macy 257*eda14cbcSMatt Macy /*************************************** 258*eda14cbcSMatt Macy * Advanced compression API 259*eda14cbcSMatt Macy ***************************************/ 260*eda14cbcSMatt Macy 261*eda14cbcSMatt Macy /* API design : 262*eda14cbcSMatt Macy * Parameters are pushed one by one into an existing context, 263*eda14cbcSMatt Macy * using ZSTD_CCtx_set*() functions. 264*eda14cbcSMatt Macy * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame. 265*eda14cbcSMatt Macy * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` ! 266*eda14cbcSMatt Macy * __They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()__ . 267*eda14cbcSMatt Macy * 268*eda14cbcSMatt Macy * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset(). 269*eda14cbcSMatt Macy * 270*eda14cbcSMatt Macy * This API supercedes all other "advanced" API entry points in the experimental section. 271*eda14cbcSMatt Macy * In the future, we expect to remove from experimental API entry points which are redundant with this API. 272*eda14cbcSMatt Macy */ 273*eda14cbcSMatt Macy 274*eda14cbcSMatt Macy 275*eda14cbcSMatt Macy /* Compression strategies, listed from fastest to strongest */ 276*eda14cbcSMatt Macy typedef enum { ZSTD_fast=1, 277*eda14cbcSMatt Macy ZSTD_dfast=2, 278*eda14cbcSMatt Macy ZSTD_greedy=3, 279*eda14cbcSMatt Macy ZSTD_lazy=4, 280*eda14cbcSMatt Macy ZSTD_lazy2=5, 281*eda14cbcSMatt Macy ZSTD_btlazy2=6, 282*eda14cbcSMatt Macy ZSTD_btopt=7, 283*eda14cbcSMatt Macy ZSTD_btultra=8, 284*eda14cbcSMatt Macy ZSTD_btultra2=9 285*eda14cbcSMatt Macy /* note : new strategies _might_ be added in the future. 286*eda14cbcSMatt Macy Only the order (from fast to strong) is guaranteed */ 287*eda14cbcSMatt Macy } ZSTD_strategy; 288*eda14cbcSMatt Macy 289*eda14cbcSMatt Macy 290*eda14cbcSMatt Macy typedef enum { 291*eda14cbcSMatt Macy 292*eda14cbcSMatt Macy /* compression parameters 293*eda14cbcSMatt Macy * Note: When compressing with a ZSTD_CDict these parameters are superseded 294*eda14cbcSMatt Macy * by the parameters used to construct the ZSTD_CDict. 295*eda14cbcSMatt Macy * See ZSTD_CCtx_refCDict() for more info (superseded-by-cdict). */ 296*eda14cbcSMatt Macy ZSTD_c_compressionLevel=100, /* Set compression parameters according to pre-defined cLevel table. 297*eda14cbcSMatt Macy * Note that exact compression parameters are dynamically determined, 298*eda14cbcSMatt Macy * depending on both compression level and srcSize (when known). 299*eda14cbcSMatt Macy * Default level is ZSTD_CLEVEL_DEFAULT==3. 300*eda14cbcSMatt Macy * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT. 301*eda14cbcSMatt Macy * Note 1 : it's possible to pass a negative compression level. 302*eda14cbcSMatt Macy * Note 2 : setting a level does not automatically set all other compression parameters 303*eda14cbcSMatt Macy * to default. Setting this will however eventually dynamically impact the compression 304*eda14cbcSMatt Macy * parameters which have not been manually set. The manually set 305*eda14cbcSMatt Macy * ones will 'stick'. */ 306*eda14cbcSMatt Macy /* Advanced compression parameters : 307*eda14cbcSMatt Macy * It's possible to pin down compression parameters to some specific values. 308*eda14cbcSMatt Macy * In which case, these values are no longer dynamically selected by the compressor */ 309*eda14cbcSMatt Macy ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2. 310*eda14cbcSMatt Macy * This will set a memory budget for streaming decompression, 311*eda14cbcSMatt Macy * with larger values requiring more memory 312*eda14cbcSMatt Macy * and typically compressing more. 313*eda14cbcSMatt Macy * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX. 314*eda14cbcSMatt Macy * Special: value 0 means "use default windowLog". 315*eda14cbcSMatt Macy * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT 316*eda14cbcSMatt Macy * requires explicitly allowing such size at streaming decompression stage. */ 317*eda14cbcSMatt Macy ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2. 318*eda14cbcSMatt Macy * Resulting memory usage is (1 << (hashLog+2)). 319*eda14cbcSMatt Macy * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX. 320*eda14cbcSMatt Macy * Larger tables improve compression ratio of strategies <= dFast, 321*eda14cbcSMatt Macy * and improve speed of strategies > dFast. 322*eda14cbcSMatt Macy * Special: value 0 means "use default hashLog". */ 323*eda14cbcSMatt Macy ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2. 324*eda14cbcSMatt Macy * Resulting memory usage is (1 << (chainLog+2)). 325*eda14cbcSMatt Macy * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX. 326*eda14cbcSMatt Macy * Larger tables result in better and slower compression. 327*eda14cbcSMatt Macy * This parameter is useless for "fast" strategy. 328*eda14cbcSMatt Macy * It's still useful when using "dfast" strategy, 329*eda14cbcSMatt Macy * in which case it defines a secondary probe table. 330*eda14cbcSMatt Macy * Special: value 0 means "use default chainLog". */ 331*eda14cbcSMatt Macy ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2. 332*eda14cbcSMatt Macy * More attempts result in better and slower compression. 333*eda14cbcSMatt Macy * This parameter is useless for "fast" and "dFast" strategies. 334*eda14cbcSMatt Macy * Special: value 0 means "use default searchLog". */ 335*eda14cbcSMatt Macy ZSTD_c_minMatch=105, /* Minimum size of searched matches. 336*eda14cbcSMatt Macy * Note that Zstandard can still find matches of smaller size, 337*eda14cbcSMatt Macy * it just tweaks its search algorithm to look for this size and larger. 338*eda14cbcSMatt Macy * Larger values increase compression and decompression speed, but decrease ratio. 339*eda14cbcSMatt Macy * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX. 340*eda14cbcSMatt Macy * Note that currently, for all strategies < btopt, effective minimum is 4. 341*eda14cbcSMatt Macy * , for all strategies > fast, effective maximum is 6. 342*eda14cbcSMatt Macy * Special: value 0 means "use default minMatchLength". */ 343*eda14cbcSMatt Macy ZSTD_c_targetLength=106, /* Impact of this field depends on strategy. 344*eda14cbcSMatt Macy * For strategies btopt, btultra & btultra2: 345*eda14cbcSMatt Macy * Length of Match considered "good enough" to stop search. 346*eda14cbcSMatt Macy * Larger values make compression stronger, and slower. 347*eda14cbcSMatt Macy * For strategy fast: 348*eda14cbcSMatt Macy * Distance between match sampling. 349*eda14cbcSMatt Macy * Larger values make compression faster, and weaker. 350*eda14cbcSMatt Macy * Special: value 0 means "use default targetLength". */ 351*eda14cbcSMatt Macy ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition. 352*eda14cbcSMatt Macy * The higher the value of selected strategy, the more complex it is, 353*eda14cbcSMatt Macy * resulting in stronger and slower compression. 354*eda14cbcSMatt Macy * Special: value 0 means "use default strategy". */ 355*eda14cbcSMatt Macy 356*eda14cbcSMatt Macy /* LDM mode parameters */ 357*eda14cbcSMatt Macy ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching. 358*eda14cbcSMatt Macy * This parameter is designed to improve compression ratio 359*eda14cbcSMatt Macy * for large inputs, by finding large matches at long distance. 360*eda14cbcSMatt Macy * It increases memory usage and window size. 361*eda14cbcSMatt Macy * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB 362*eda14cbcSMatt Macy * except when expressly set to a different value. */ 363*eda14cbcSMatt Macy ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2. 364*eda14cbcSMatt Macy * Larger values increase memory usage and compression ratio, 365*eda14cbcSMatt Macy * but decrease compression speed. 366*eda14cbcSMatt Macy * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX 367*eda14cbcSMatt Macy * default: windowlog - 7. 368*eda14cbcSMatt Macy * Special: value 0 means "automatically determine hashlog". */ 369*eda14cbcSMatt Macy ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher. 370*eda14cbcSMatt Macy * Larger/too small values usually decrease compression ratio. 371*eda14cbcSMatt Macy * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX. 372*eda14cbcSMatt Macy * Special: value 0 means "use default value" (default: 64). */ 373*eda14cbcSMatt Macy ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution. 374*eda14cbcSMatt Macy * Larger values improve collision resolution but decrease compression speed. 375*eda14cbcSMatt Macy * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX. 376*eda14cbcSMatt Macy * Special: value 0 means "use default value" (default: 3). */ 377*eda14cbcSMatt Macy ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table. 378*eda14cbcSMatt Macy * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN). 379*eda14cbcSMatt Macy * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage. 380*eda14cbcSMatt Macy * Larger values improve compression speed. 381*eda14cbcSMatt Macy * Deviating far from default value will likely result in a compression ratio decrease. 382*eda14cbcSMatt Macy * Special: value 0 means "automatically determine hashRateLog". */ 383*eda14cbcSMatt Macy 384*eda14cbcSMatt Macy /* frame parameters */ 385*eda14cbcSMatt Macy ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1) 386*eda14cbcSMatt Macy * Content size must be known at the beginning of compression. 387*eda14cbcSMatt Macy * This is automatically the case when using ZSTD_compress2(), 388*eda14cbcSMatt Macy * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */ 389*eda14cbcSMatt Macy ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */ 390*eda14cbcSMatt Macy ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */ 391*eda14cbcSMatt Macy 392*eda14cbcSMatt Macy /* multi-threading parameters */ 393*eda14cbcSMatt Macy /* These parameters are only useful if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD). 394*eda14cbcSMatt Macy * They return an error otherwise. */ 395*eda14cbcSMatt Macy ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel. 396*eda14cbcSMatt Macy * When nbWorkers >= 1, triggers asynchronous mode when used with ZSTD_compressStream*() : 397*eda14cbcSMatt Macy * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller, 398*eda14cbcSMatt Macy * while compression work is performed in parallel, within worker threads. 399*eda14cbcSMatt Macy * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end : 400*eda14cbcSMatt Macy * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call). 401*eda14cbcSMatt Macy * More workers improve speed, but also increase memory usage. 402*eda14cbcSMatt Macy * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */ 403*eda14cbcSMatt Macy ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1. 404*eda14cbcSMatt Macy * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads. 405*eda14cbcSMatt Macy * 0 means default, which is dynamically determined based on compression parameters. 406*eda14cbcSMatt Macy * Job size must be a minimum of overlap size, or 1 MB, whichever is largest. 407*eda14cbcSMatt Macy * The minimum size is automatically and transparently enforced. */ 408*eda14cbcSMatt Macy ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size. 409*eda14cbcSMatt Macy * The overlap size is an amount of data reloaded from previous job at the beginning of a new job. 410*eda14cbcSMatt Macy * It helps preserve compression ratio, while each job is compressed in parallel. 411*eda14cbcSMatt Macy * This value is enforced only when nbWorkers >= 1. 412*eda14cbcSMatt Macy * Larger values increase compression ratio, but decrease speed. 413*eda14cbcSMatt Macy * Possible values range from 0 to 9 : 414*eda14cbcSMatt Macy * - 0 means "default" : value will be determined by the library, depending on strategy 415*eda14cbcSMatt Macy * - 1 means "no overlap" 416*eda14cbcSMatt Macy * - 9 means "full overlap", using a full window size. 417*eda14cbcSMatt Macy * Each intermediate rank increases/decreases load size by a factor 2 : 418*eda14cbcSMatt Macy * 9: full window; 8: w/2; 7: w/4; 6: w/8; 5:w/16; 4: w/32; 3:w/64; 2:w/128; 1:no overlap; 0:default 419*eda14cbcSMatt Macy * default value varies between 6 and 9, depending on strategy */ 420*eda14cbcSMatt Macy 421*eda14cbcSMatt Macy /* note : additional experimental parameters are also available 422*eda14cbcSMatt Macy * within the experimental section of the API. 423*eda14cbcSMatt Macy * At the time of this writing, they include : 424*eda14cbcSMatt Macy * ZSTD_c_rsyncable 425*eda14cbcSMatt Macy * ZSTD_c_format 426*eda14cbcSMatt Macy * ZSTD_c_forceMaxWindow 427*eda14cbcSMatt Macy * ZSTD_c_forceAttachDict 428*eda14cbcSMatt Macy * ZSTD_c_literalCompressionMode 429*eda14cbcSMatt Macy * ZSTD_c_targetCBlockSize 430*eda14cbcSMatt Macy * ZSTD_c_srcSizeHint 431*eda14cbcSMatt Macy * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. 432*eda14cbcSMatt Macy * note : never ever use experimentalParam? names directly; 433*eda14cbcSMatt Macy * also, the enums values themselves are unstable and can still change. 434*eda14cbcSMatt Macy */ 435*eda14cbcSMatt Macy ZSTD_c_experimentalParam1=500, 436*eda14cbcSMatt Macy ZSTD_c_experimentalParam2=10, 437*eda14cbcSMatt Macy ZSTD_c_experimentalParam3=1000, 438*eda14cbcSMatt Macy ZSTD_c_experimentalParam4=1001, 439*eda14cbcSMatt Macy ZSTD_c_experimentalParam5=1002, 440*eda14cbcSMatt Macy ZSTD_c_experimentalParam6=1003, 441*eda14cbcSMatt Macy ZSTD_c_experimentalParam7=1004 442*eda14cbcSMatt Macy } ZSTD_cParameter; 443*eda14cbcSMatt Macy 444*eda14cbcSMatt Macy typedef struct { 445*eda14cbcSMatt Macy size_t error; 446*eda14cbcSMatt Macy int lowerBound; 447*eda14cbcSMatt Macy int upperBound; 448*eda14cbcSMatt Macy } ZSTD_bounds; 449*eda14cbcSMatt Macy 450*eda14cbcSMatt Macy /*! ZSTD_cParam_getBounds() : 451*eda14cbcSMatt Macy * All parameters must belong to an interval with lower and upper bounds, 452*eda14cbcSMatt Macy * otherwise they will either trigger an error or be automatically clamped. 453*eda14cbcSMatt Macy * @return : a structure, ZSTD_bounds, which contains 454*eda14cbcSMatt Macy * - an error status field, which must be tested using ZSTD_isError() 455*eda14cbcSMatt Macy * - lower and upper bounds, both inclusive 456*eda14cbcSMatt Macy */ 457*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam); 458*eda14cbcSMatt Macy 459*eda14cbcSMatt Macy /*! ZSTD_CCtx_setParameter() : 460*eda14cbcSMatt Macy * Set one compression parameter, selected by enum ZSTD_cParameter. 461*eda14cbcSMatt Macy * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds(). 462*eda14cbcSMatt Macy * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). 463*eda14cbcSMatt Macy * Setting a parameter is generally only possible during frame initialization (before starting compression). 464*eda14cbcSMatt Macy * Exception : when using multi-threading mode (nbWorkers >= 1), 465*eda14cbcSMatt Macy * the following parameters can be updated _during_ compression (within same frame): 466*eda14cbcSMatt Macy * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy. 467*eda14cbcSMatt Macy * new parameters will be active for next job only (after a flush()). 468*eda14cbcSMatt Macy * @return : an error code (which can be tested using ZSTD_isError()). 469*eda14cbcSMatt Macy */ 470*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value); 471*eda14cbcSMatt Macy 472*eda14cbcSMatt Macy /*! ZSTD_CCtx_setPledgedSrcSize() : 473*eda14cbcSMatt Macy * Total input data size to be compressed as a single frame. 474*eda14cbcSMatt Macy * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag. 475*eda14cbcSMatt Macy * This value will also be controlled at end of frame, and trigger an error if not respected. 476*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 477*eda14cbcSMatt Macy * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame. 478*eda14cbcSMatt Macy * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. 479*eda14cbcSMatt Macy * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame. 480*eda14cbcSMatt Macy * Note 2 : pledgedSrcSize is only valid once, for the next frame. 481*eda14cbcSMatt Macy * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN. 482*eda14cbcSMatt Macy * Note 3 : Whenever all input data is provided and consumed in a single round, 483*eda14cbcSMatt Macy * for example with ZSTD_compress2(), 484*eda14cbcSMatt Macy * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end), 485*eda14cbcSMatt Macy * this value is automatically overridden by srcSize instead. 486*eda14cbcSMatt Macy */ 487*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); 488*eda14cbcSMatt Macy 489*eda14cbcSMatt Macy typedef enum { 490*eda14cbcSMatt Macy ZSTD_reset_session_only = 1, 491*eda14cbcSMatt Macy ZSTD_reset_parameters = 2, 492*eda14cbcSMatt Macy ZSTD_reset_session_and_parameters = 3 493*eda14cbcSMatt Macy } ZSTD_ResetDirective; 494*eda14cbcSMatt Macy 495*eda14cbcSMatt Macy /*! ZSTD_CCtx_reset() : 496*eda14cbcSMatt Macy * There are 2 different things that can be reset, independently or jointly : 497*eda14cbcSMatt Macy * - The session : will stop compressing current frame, and make CCtx ready to start a new one. 498*eda14cbcSMatt Macy * Useful after an error, or to interrupt any ongoing compression. 499*eda14cbcSMatt Macy * Any internal data not yet flushed is cancelled. 500*eda14cbcSMatt Macy * Compression parameters and dictionary remain unchanged. 501*eda14cbcSMatt Macy * They will be used to compress next frame. 502*eda14cbcSMatt Macy * Resetting session never fails. 503*eda14cbcSMatt Macy * - The parameters : changes all parameters back to "default". 504*eda14cbcSMatt Macy * This removes any reference to any dictionary too. 505*eda14cbcSMatt Macy * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing) 506*eda14cbcSMatt Macy * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError()) 507*eda14cbcSMatt Macy * - Both : similar to resetting the session, followed by resetting parameters. 508*eda14cbcSMatt Macy */ 509*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset); 510*eda14cbcSMatt Macy 511*eda14cbcSMatt Macy /*! ZSTD_compress2() : 512*eda14cbcSMatt Macy * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API. 513*eda14cbcSMatt Macy * ZSTD_compress2() always starts a new frame. 514*eda14cbcSMatt Macy * Should cctx hold data from a previously unfinished frame, everything about it is forgotten. 515*eda14cbcSMatt Macy * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() 516*eda14cbcSMatt Macy * - The function is always blocking, returns when compression is completed. 517*eda14cbcSMatt Macy * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. 518*eda14cbcSMatt Macy * @return : compressed size written into `dst` (<= `dstCapacity), 519*eda14cbcSMatt Macy * or an error code if it fails (which can be tested using ZSTD_isError()). 520*eda14cbcSMatt Macy */ 521*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx, 522*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 523*eda14cbcSMatt Macy const void* src, size_t srcSize); 524*eda14cbcSMatt Macy 525*eda14cbcSMatt Macy 526*eda14cbcSMatt Macy /*************************************** 527*eda14cbcSMatt Macy * Advanced decompression API 528*eda14cbcSMatt Macy ***************************************/ 529*eda14cbcSMatt Macy 530*eda14cbcSMatt Macy /* The advanced API pushes parameters one by one into an existing DCtx context. 531*eda14cbcSMatt Macy * Parameters are sticky, and remain valid for all following frames 532*eda14cbcSMatt Macy * using the same DCtx context. 533*eda14cbcSMatt Macy * It's possible to reset parameters to default values using ZSTD_DCtx_reset(). 534*eda14cbcSMatt Macy * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream(). 535*eda14cbcSMatt Macy * Therefore, no new decompression function is necessary. 536*eda14cbcSMatt Macy */ 537*eda14cbcSMatt Macy 538*eda14cbcSMatt Macy typedef enum { 539*eda14cbcSMatt Macy 540*eda14cbcSMatt Macy ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which 541*eda14cbcSMatt Macy * the streaming API will refuse to allocate memory buffer 542*eda14cbcSMatt Macy * in order to protect the host from unreasonable memory requirements. 543*eda14cbcSMatt Macy * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. 544*eda14cbcSMatt Macy * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT). 545*eda14cbcSMatt Macy * Special: value 0 means "use default maximum windowLog". */ 546*eda14cbcSMatt Macy 547*eda14cbcSMatt Macy /* note : additional experimental parameters are also available 548*eda14cbcSMatt Macy * within the experimental section of the API. 549*eda14cbcSMatt Macy * At the time of this writing, they include : 550*eda14cbcSMatt Macy * ZSTD_d_format 551*eda14cbcSMatt Macy * ZSTD_d_stableOutBuffer 552*eda14cbcSMatt Macy * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. 553*eda14cbcSMatt Macy * note : never ever use experimentalParam? names directly 554*eda14cbcSMatt Macy */ 555*eda14cbcSMatt Macy ZSTD_d_experimentalParam1=1000, 556*eda14cbcSMatt Macy ZSTD_d_experimentalParam2=1001 557*eda14cbcSMatt Macy 558*eda14cbcSMatt Macy } ZSTD_dParameter; 559*eda14cbcSMatt Macy 560*eda14cbcSMatt Macy /*! ZSTD_dParam_getBounds() : 561*eda14cbcSMatt Macy * All parameters must belong to an interval with lower and upper bounds, 562*eda14cbcSMatt Macy * otherwise they will either trigger an error or be automatically clamped. 563*eda14cbcSMatt Macy * @return : a structure, ZSTD_bounds, which contains 564*eda14cbcSMatt Macy * - an error status field, which must be tested using ZSTD_isError() 565*eda14cbcSMatt Macy * - both lower and upper bounds, inclusive 566*eda14cbcSMatt Macy */ 567*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam); 568*eda14cbcSMatt Macy 569*eda14cbcSMatt Macy /*! ZSTD_DCtx_setParameter() : 570*eda14cbcSMatt Macy * Set one compression parameter, selected by enum ZSTD_dParameter. 571*eda14cbcSMatt Macy * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds(). 572*eda14cbcSMatt Macy * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). 573*eda14cbcSMatt Macy * Setting a parameter is only possible during frame initialization (before starting decompression). 574*eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()). 575*eda14cbcSMatt Macy */ 576*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value); 577*eda14cbcSMatt Macy 578*eda14cbcSMatt Macy /*! ZSTD_DCtx_reset() : 579*eda14cbcSMatt Macy * Return a DCtx to clean state. 580*eda14cbcSMatt Macy * Session and parameters can be reset jointly or separately. 581*eda14cbcSMatt Macy * Parameters can only be reset when no active frame is being decompressed. 582*eda14cbcSMatt Macy * @return : 0, or an error code, which can be tested with ZSTD_isError() 583*eda14cbcSMatt Macy */ 584*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset); 585*eda14cbcSMatt Macy 586*eda14cbcSMatt Macy 587*eda14cbcSMatt Macy /**************************** 588*eda14cbcSMatt Macy * Streaming 589*eda14cbcSMatt Macy ****************************/ 590*eda14cbcSMatt Macy 591*eda14cbcSMatt Macy typedef struct ZSTD_inBuffer_s { 592*eda14cbcSMatt Macy const void* src; /**< start of input buffer */ 593*eda14cbcSMatt Macy size_t size; /**< size of input buffer */ 594*eda14cbcSMatt Macy size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ 595*eda14cbcSMatt Macy } ZSTD_inBuffer; 596*eda14cbcSMatt Macy 597*eda14cbcSMatt Macy typedef struct ZSTD_outBuffer_s { 598*eda14cbcSMatt Macy void* dst; /**< start of output buffer */ 599*eda14cbcSMatt Macy size_t size; /**< size of output buffer */ 600*eda14cbcSMatt Macy size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ 601*eda14cbcSMatt Macy } ZSTD_outBuffer; 602*eda14cbcSMatt Macy 603*eda14cbcSMatt Macy 604*eda14cbcSMatt Macy 605*eda14cbcSMatt Macy /*-*********************************************************************** 606*eda14cbcSMatt Macy * Streaming compression - HowTo 607*eda14cbcSMatt Macy * 608*eda14cbcSMatt Macy * A ZSTD_CStream object is required to track streaming operation. 609*eda14cbcSMatt Macy * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. 610*eda14cbcSMatt Macy * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. 611*eda14cbcSMatt Macy * It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory. 612*eda14cbcSMatt Macy * 613*eda14cbcSMatt Macy * For parallel execution, use one separate ZSTD_CStream per thread. 614*eda14cbcSMatt Macy * 615*eda14cbcSMatt Macy * note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing. 616*eda14cbcSMatt Macy * 617*eda14cbcSMatt Macy * Parameters are sticky : when starting a new compression on the same context, 618*eda14cbcSMatt Macy * it will re-use the same sticky parameters as previous compression session. 619*eda14cbcSMatt Macy * When in doubt, it's recommended to fully initialize the context before usage. 620*eda14cbcSMatt Macy * Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(), 621*eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to 622*eda14cbcSMatt Macy * set more specific parameters, the pledged source size, or load a dictionary. 623*eda14cbcSMatt Macy * 624*eda14cbcSMatt Macy * Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to 625*eda14cbcSMatt Macy * consume input stream. The function will automatically update both `pos` 626*eda14cbcSMatt Macy * fields within `input` and `output`. 627*eda14cbcSMatt Macy * Note that the function may not consume the entire input, for example, because 628*eda14cbcSMatt Macy * the output buffer is already full, in which case `input.pos < input.size`. 629*eda14cbcSMatt Macy * The caller must check if input has been entirely consumed. 630*eda14cbcSMatt Macy * If not, the caller must make some room to receive more compressed data, 631*eda14cbcSMatt Macy * and then present again remaining input data. 632*eda14cbcSMatt Macy * note: ZSTD_e_continue is guaranteed to make some forward progress when called, 633*eda14cbcSMatt Macy * but doesn't guarantee maximal forward progress. This is especially relevant 634*eda14cbcSMatt Macy * when compressing with multiple threads. The call won't block if it can 635*eda14cbcSMatt Macy * consume some input, but if it can't it will wait for some, but not all, 636*eda14cbcSMatt Macy * output to be flushed. 637*eda14cbcSMatt Macy * @return : provides a minimum amount of data remaining to be flushed from internal buffers 638*eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 639*eda14cbcSMatt Macy * 640*eda14cbcSMatt Macy * At any moment, it's possible to flush whatever data might remain stuck within internal buffer, 641*eda14cbcSMatt Macy * using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated. 642*eda14cbcSMatt Macy * Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0). 643*eda14cbcSMatt Macy * In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush. 644*eda14cbcSMatt Macy * You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the 645*eda14cbcSMatt Macy * operation. 646*eda14cbcSMatt Macy * note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will 647*eda14cbcSMatt Macy * block until the flush is complete or the output buffer is full. 648*eda14cbcSMatt Macy * @return : 0 if internal buffers are entirely flushed, 649*eda14cbcSMatt Macy * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 650*eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 651*eda14cbcSMatt Macy * 652*eda14cbcSMatt Macy * Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame. 653*eda14cbcSMatt Macy * It will perform a flush and write frame epilogue. 654*eda14cbcSMatt Macy * The epilogue is required for decoders to consider a frame completed. 655*eda14cbcSMatt Macy * flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush. 656*eda14cbcSMatt Macy * You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to 657*eda14cbcSMatt Macy * start a new frame. 658*eda14cbcSMatt Macy * note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will 659*eda14cbcSMatt Macy * block until the flush is complete or the output buffer is full. 660*eda14cbcSMatt Macy * @return : 0 if frame fully completed and fully flushed, 661*eda14cbcSMatt Macy * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), 662*eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 663*eda14cbcSMatt Macy * 664*eda14cbcSMatt Macy * *******************************************************************/ 665*eda14cbcSMatt Macy 666*eda14cbcSMatt Macy typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */ 667*eda14cbcSMatt Macy /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */ 668*eda14cbcSMatt Macy /*===== ZSTD_CStream management functions =====*/ 669*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); 670*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); 671*eda14cbcSMatt Macy 672*eda14cbcSMatt Macy /*===== Streaming compression functions =====*/ 673*eda14cbcSMatt Macy typedef enum { 674*eda14cbcSMatt Macy ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */ 675*eda14cbcSMatt Macy ZSTD_e_flush=1, /* flush any data provided so far, 676*eda14cbcSMatt Macy * it creates (at least) one new block, that can be decoded immediately on reception; 677*eda14cbcSMatt Macy * frame will continue: any future data can still reference previously compressed data, improving compression. 678*eda14cbcSMatt Macy * note : multithreaded compression will block to flush as much output as possible. */ 679*eda14cbcSMatt Macy ZSTD_e_end=2 /* flush any remaining data _and_ close current frame. 680*eda14cbcSMatt Macy * note that frame is only closed after compressed data is fully flushed (return value == 0). 681*eda14cbcSMatt Macy * After that point, any additional data starts a new frame. 682*eda14cbcSMatt Macy * note : each frame is independent (does not reference any content from previous frame). 683*eda14cbcSMatt Macy : note : multithreaded compression will block to flush as much output as possible. */ 684*eda14cbcSMatt Macy } ZSTD_EndDirective; 685*eda14cbcSMatt Macy 686*eda14cbcSMatt Macy /*! ZSTD_compressStream2() : 687*eda14cbcSMatt Macy * Behaves about the same as ZSTD_compressStream, with additional control on end directive. 688*eda14cbcSMatt Macy * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() 689*eda14cbcSMatt Macy * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode) 690*eda14cbcSMatt Macy * - output->pos must be <= dstCapacity, input->pos must be <= srcSize 691*eda14cbcSMatt Macy * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. 692*eda14cbcSMatt Macy * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller. 693*eda14cbcSMatt Macy * - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available, 694*eda14cbcSMatt Macy * and then immediately returns, just indicating that there is some data remaining to be flushed. 695*eda14cbcSMatt Macy * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. 696*eda14cbcSMatt Macy * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking. 697*eda14cbcSMatt Macy * - @return provides a minimum amount of data remaining to be flushed from internal buffers 698*eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(). 699*eda14cbcSMatt Macy * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. 700*eda14cbcSMatt Macy * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. 701*eda14cbcSMatt Macy * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. 702*eda14cbcSMatt Macy * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), 703*eda14cbcSMatt Macy * only ZSTD_e_end or ZSTD_e_flush operations are allowed. 704*eda14cbcSMatt Macy * Before starting a new compression job, or changing compression parameters, 705*eda14cbcSMatt Macy * it is required to fully flush internal buffers. 706*eda14cbcSMatt Macy */ 707*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx, 708*eda14cbcSMatt Macy ZSTD_outBuffer* output, 709*eda14cbcSMatt Macy ZSTD_inBuffer* input, 710*eda14cbcSMatt Macy ZSTD_EndDirective endOp); 711*eda14cbcSMatt Macy 712*eda14cbcSMatt Macy 713*eda14cbcSMatt Macy /* These buffer sizes are softly recommended. 714*eda14cbcSMatt Macy * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output. 715*eda14cbcSMatt Macy * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(), 716*eda14cbcSMatt Macy * reducing the amount of memory shuffling and buffering, resulting in minor performance savings. 717*eda14cbcSMatt Macy * 718*eda14cbcSMatt Macy * However, note that these recommendations are from the perspective of a C caller program. 719*eda14cbcSMatt Macy * If the streaming interface is invoked from some other language, 720*eda14cbcSMatt Macy * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo, 721*eda14cbcSMatt Macy * a major performance rule is to reduce crossing such interface to an absolute minimum. 722*eda14cbcSMatt Macy * It's not rare that performance ends being spent more into the interface, rather than compression itself. 723*eda14cbcSMatt Macy * In which cases, prefer using large buffers, as large as practical, 724*eda14cbcSMatt Macy * for both input and output, to reduce the nb of roundtrips. 725*eda14cbcSMatt Macy */ 726*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ 727*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */ 728*eda14cbcSMatt Macy 729*eda14cbcSMatt Macy 730*eda14cbcSMatt Macy /* ***************************************************************************** 731*eda14cbcSMatt Macy * This following is a legacy streaming API. 732*eda14cbcSMatt Macy * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2(). 733*eda14cbcSMatt Macy * It is redundant, but remains fully supported. 734*eda14cbcSMatt Macy * Advanced parameters and dictionary compression can only be used through the 735*eda14cbcSMatt Macy * new API. 736*eda14cbcSMatt Macy ******************************************************************************/ 737*eda14cbcSMatt Macy 738*eda14cbcSMatt Macy /*! 739*eda14cbcSMatt Macy * Equivalent to: 740*eda14cbcSMatt Macy * 741*eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 742*eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) 743*eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 744*eda14cbcSMatt Macy */ 745*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); 746*eda14cbcSMatt Macy /*! 747*eda14cbcSMatt Macy * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue). 748*eda14cbcSMatt Macy * NOTE: The return value is different. ZSTD_compressStream() returns a hint for 749*eda14cbcSMatt Macy * the next read size (if non-zero and not an error). ZSTD_compressStream2() 750*eda14cbcSMatt Macy * returns the minimum nb of bytes left to flush (if non-zero and not an error). 751*eda14cbcSMatt Macy */ 752*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 753*eda14cbcSMatt Macy /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */ 754*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 755*eda14cbcSMatt Macy /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */ 756*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); 757*eda14cbcSMatt Macy 758*eda14cbcSMatt Macy 759*eda14cbcSMatt Macy /*-*************************************************************************** 760*eda14cbcSMatt Macy * Streaming decompression - HowTo 761*eda14cbcSMatt Macy * 762*eda14cbcSMatt Macy * A ZSTD_DStream object is required to track streaming operations. 763*eda14cbcSMatt Macy * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. 764*eda14cbcSMatt Macy * ZSTD_DStream objects can be re-used multiple times. 765*eda14cbcSMatt Macy * 766*eda14cbcSMatt Macy * Use ZSTD_initDStream() to start a new decompression operation. 767*eda14cbcSMatt Macy * @return : recommended first input size 768*eda14cbcSMatt Macy * Alternatively, use advanced API to set specific properties. 769*eda14cbcSMatt Macy * 770*eda14cbcSMatt Macy * Use ZSTD_decompressStream() repetitively to consume your input. 771*eda14cbcSMatt Macy * The function will update both `pos` fields. 772*eda14cbcSMatt Macy * If `input.pos < input.size`, some input has not been consumed. 773*eda14cbcSMatt Macy * It's up to the caller to present again remaining data. 774*eda14cbcSMatt Macy * The function tries to flush all data decoded immediately, respecting output buffer size. 775*eda14cbcSMatt Macy * If `output.pos < output.size`, decoder has flushed everything it could. 776*eda14cbcSMatt Macy * But if `output.pos == output.size`, there might be some data left within internal buffers., 777*eda14cbcSMatt Macy * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer. 778*eda14cbcSMatt Macy * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX. 779*eda14cbcSMatt Macy * @return : 0 when a frame is completely decoded and fully flushed, 780*eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError(), 781*eda14cbcSMatt Macy * or any other value > 0, which means there is still some decoding or flushing to do to complete current frame : 782*eda14cbcSMatt Macy * the return value is a suggested next input size (just a hint for better latency) 783*eda14cbcSMatt Macy * that will never request more than the remaining frame size. 784*eda14cbcSMatt Macy * *******************************************************************************/ 785*eda14cbcSMatt Macy 786*eda14cbcSMatt Macy typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */ 787*eda14cbcSMatt Macy /* For compatibility with versions <= v1.2.0, prefer differentiating them. */ 788*eda14cbcSMatt Macy /*===== ZSTD_DStream management functions =====*/ 789*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); 790*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); 791*eda14cbcSMatt Macy 792*eda14cbcSMatt Macy /*===== Streaming decompression functions =====*/ 793*eda14cbcSMatt Macy 794*eda14cbcSMatt Macy /* This function is redundant with the advanced API and equivalent to: 795*eda14cbcSMatt Macy * 796*eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 797*eda14cbcSMatt Macy * ZSTD_DCtx_refDDict(zds, NULL); 798*eda14cbcSMatt Macy */ 799*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); 800*eda14cbcSMatt Macy 801*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); 802*eda14cbcSMatt Macy 803*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ 804*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ 805*eda14cbcSMatt Macy 806*eda14cbcSMatt Macy 807*eda14cbcSMatt Macy /************************** 808*eda14cbcSMatt Macy * Simple dictionary API 809*eda14cbcSMatt Macy ***************************/ 810*eda14cbcSMatt Macy /*! ZSTD_compress_usingDict() : 811*eda14cbcSMatt Macy * Compression at an explicit compression level using a Dictionary. 812*eda14cbcSMatt Macy * A dictionary can be any arbitrary data segment (also called a prefix), 813*eda14cbcSMatt Macy * or a buffer with specified information (see dictBuilder/zdict.h). 814*eda14cbcSMatt Macy * Note : This function loads the dictionary, resulting in significant startup delay. 815*eda14cbcSMatt Macy * It's intended for a dictionary used only once. 816*eda14cbcSMatt Macy * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */ 817*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, 818*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 819*eda14cbcSMatt Macy const void* src, size_t srcSize, 820*eda14cbcSMatt Macy const void* dict,size_t dictSize, 821*eda14cbcSMatt Macy int compressionLevel); 822*eda14cbcSMatt Macy 823*eda14cbcSMatt Macy /*! ZSTD_decompress_usingDict() : 824*eda14cbcSMatt Macy * Decompression using a known Dictionary. 825*eda14cbcSMatt Macy * Dictionary must be identical to the one used during compression. 826*eda14cbcSMatt Macy * Note : This function loads the dictionary, resulting in significant startup delay. 827*eda14cbcSMatt Macy * It's intended for a dictionary used only once. 828*eda14cbcSMatt Macy * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ 829*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, 830*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 831*eda14cbcSMatt Macy const void* src, size_t srcSize, 832*eda14cbcSMatt Macy const void* dict,size_t dictSize); 833*eda14cbcSMatt Macy 834*eda14cbcSMatt Macy 835*eda14cbcSMatt Macy /*********************************** 836*eda14cbcSMatt Macy * Bulk processing dictionary API 837*eda14cbcSMatt Macy **********************************/ 838*eda14cbcSMatt Macy typedef struct ZSTD_CDict_s ZSTD_CDict; 839*eda14cbcSMatt Macy 840*eda14cbcSMatt Macy /*! ZSTD_createCDict() : 841*eda14cbcSMatt Macy * When compressing multiple messages or blocks using the same dictionary, 842*eda14cbcSMatt Macy * it's recommended to digest the dictionary only once, since it's a costly operation. 843*eda14cbcSMatt Macy * ZSTD_createCDict() will create a state from digesting a dictionary. 844*eda14cbcSMatt Macy * The resulting state can be used for future compression operations with very limited startup cost. 845*eda14cbcSMatt Macy * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 846*eda14cbcSMatt Macy * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict. 847*eda14cbcSMatt Macy * Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content. 848*eda14cbcSMatt Macy * Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer, 849*eda14cbcSMatt Macy * in which case the only thing that it transports is the @compressionLevel. 850*eda14cbcSMatt Macy * This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively, 851*eda14cbcSMatt Macy * expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */ 852*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, 853*eda14cbcSMatt Macy int compressionLevel); 854*eda14cbcSMatt Macy 855*eda14cbcSMatt Macy /*! ZSTD_freeCDict() : 856*eda14cbcSMatt Macy * Function frees memory allocated by ZSTD_createCDict(). */ 857*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); 858*eda14cbcSMatt Macy 859*eda14cbcSMatt Macy /*! ZSTD_compress_usingCDict() : 860*eda14cbcSMatt Macy * Compression using a digested Dictionary. 861*eda14cbcSMatt Macy * Recommended when same dictionary is used multiple times. 862*eda14cbcSMatt Macy * Note : compression level is _decided at dictionary creation time_, 863*eda14cbcSMatt Macy * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ 864*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, 865*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 866*eda14cbcSMatt Macy const void* src, size_t srcSize, 867*eda14cbcSMatt Macy const ZSTD_CDict* cdict); 868*eda14cbcSMatt Macy 869*eda14cbcSMatt Macy 870*eda14cbcSMatt Macy typedef struct ZSTD_DDict_s ZSTD_DDict; 871*eda14cbcSMatt Macy 872*eda14cbcSMatt Macy /*! ZSTD_createDDict() : 873*eda14cbcSMatt Macy * Create a digested dictionary, ready to start decompression operation without startup delay. 874*eda14cbcSMatt Macy * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */ 875*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); 876*eda14cbcSMatt Macy 877*eda14cbcSMatt Macy /*! ZSTD_freeDDict() : 878*eda14cbcSMatt Macy * Function frees memory allocated with ZSTD_createDDict() */ 879*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); 880*eda14cbcSMatt Macy 881*eda14cbcSMatt Macy /*! ZSTD_decompress_usingDDict() : 882*eda14cbcSMatt Macy * Decompression using a digested Dictionary. 883*eda14cbcSMatt Macy * Recommended when same dictionary is used multiple times. */ 884*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, 885*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 886*eda14cbcSMatt Macy const void* src, size_t srcSize, 887*eda14cbcSMatt Macy const ZSTD_DDict* ddict); 888*eda14cbcSMatt Macy 889*eda14cbcSMatt Macy 890*eda14cbcSMatt Macy /******************************** 891*eda14cbcSMatt Macy * Dictionary helper functions 892*eda14cbcSMatt Macy *******************************/ 893*eda14cbcSMatt Macy 894*eda14cbcSMatt Macy /*! ZSTD_getDictID_fromDict() : 895*eda14cbcSMatt Macy * Provides the dictID stored within dictionary. 896*eda14cbcSMatt Macy * if @return == 0, the dictionary is not conformant with Zstandard specification. 897*eda14cbcSMatt Macy * It can still be loaded, but as a content-only dictionary. */ 898*eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); 899*eda14cbcSMatt Macy 900*eda14cbcSMatt Macy /*! ZSTD_getDictID_fromDDict() : 901*eda14cbcSMatt Macy * Provides the dictID of the dictionary loaded into `ddict`. 902*eda14cbcSMatt Macy * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. 903*eda14cbcSMatt Macy * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ 904*eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); 905*eda14cbcSMatt Macy 906*eda14cbcSMatt Macy /*! ZSTD_getDictID_fromFrame() : 907*eda14cbcSMatt Macy * Provides the dictID required to decompressed the frame stored within `src`. 908*eda14cbcSMatt Macy * If @return == 0, the dictID could not be decoded. 909*eda14cbcSMatt Macy * This could for one of the following reasons : 910*eda14cbcSMatt Macy * - The frame does not require a dictionary to be decoded (most common case). 911*eda14cbcSMatt Macy * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. 912*eda14cbcSMatt Macy * Note : this use case also happens when using a non-conformant dictionary. 913*eda14cbcSMatt Macy * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). 914*eda14cbcSMatt Macy * - This is not a Zstandard frame. 915*eda14cbcSMatt Macy * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */ 916*eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); 917*eda14cbcSMatt Macy 918*eda14cbcSMatt Macy 919*eda14cbcSMatt Macy /******************************************************************************* 920*eda14cbcSMatt Macy * Advanced dictionary and prefix API 921*eda14cbcSMatt Macy * 922*eda14cbcSMatt Macy * This API allows dictionaries to be used with ZSTD_compress2(), 923*eda14cbcSMatt Macy * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and 924*eda14cbcSMatt Macy * only reset with the context is reset with ZSTD_reset_parameters or 925*eda14cbcSMatt Macy * ZSTD_reset_session_and_parameters. Prefixes are single-use. 926*eda14cbcSMatt Macy ******************************************************************************/ 927*eda14cbcSMatt Macy 928*eda14cbcSMatt Macy 929*eda14cbcSMatt Macy /*! ZSTD_CCtx_loadDictionary() : 930*eda14cbcSMatt Macy * Create an internal CDict from `dict` buffer. 931*eda14cbcSMatt Macy * Decompression will have to use same dictionary. 932*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 933*eda14cbcSMatt Macy * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary, 934*eda14cbcSMatt Macy * meaning "return to no-dictionary mode". 935*eda14cbcSMatt Macy * Note 1 : Dictionary is sticky, it will be used for all future compressed frames. 936*eda14cbcSMatt Macy * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters). 937*eda14cbcSMatt Macy * Note 2 : Loading a dictionary involves building tables. 938*eda14cbcSMatt Macy * It's also a CPU consuming operation, with non-negligible impact on latency. 939*eda14cbcSMatt Macy * Tables are dependent on compression parameters, and for this reason, 940*eda14cbcSMatt Macy * compression parameters can no longer be changed after loading a dictionary. 941*eda14cbcSMatt Macy * Note 3 :`dict` content will be copied internally. 942*eda14cbcSMatt Macy * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead. 943*eda14cbcSMatt Macy * In such a case, dictionary buffer must outlive its users. 944*eda14cbcSMatt Macy * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced() 945*eda14cbcSMatt Macy * to precisely select how dictionary content must be interpreted. */ 946*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 947*eda14cbcSMatt Macy 948*eda14cbcSMatt Macy /*! ZSTD_CCtx_refCDict() : 949*eda14cbcSMatt Macy * Reference a prepared dictionary, to be used for all next compressed frames. 950*eda14cbcSMatt Macy * Note that compression parameters are enforced from within CDict, 951*eda14cbcSMatt Macy * and supersede any compression parameter previously set within CCtx. 952*eda14cbcSMatt Macy * The parameters ignored are labled as "superseded-by-cdict" in the ZSTD_cParameter enum docs. 953*eda14cbcSMatt Macy * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode. 954*eda14cbcSMatt Macy * The dictionary will remain valid for future compressed frames using same CCtx. 955*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 956*eda14cbcSMatt Macy * Special : Referencing a NULL CDict means "return to no-dictionary mode". 957*eda14cbcSMatt Macy * Note 1 : Currently, only one dictionary can be managed. 958*eda14cbcSMatt Macy * Referencing a new dictionary effectively "discards" any previous one. 959*eda14cbcSMatt Macy * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */ 960*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); 961*eda14cbcSMatt Macy 962*eda14cbcSMatt Macy /*! ZSTD_CCtx_refPrefix() : 963*eda14cbcSMatt Macy * Reference a prefix (single-usage dictionary) for next compressed frame. 964*eda14cbcSMatt Macy * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end). 965*eda14cbcSMatt Macy * Decompression will need same prefix to properly regenerate data. 966*eda14cbcSMatt Macy * Compressing with a prefix is similar in outcome as performing a diff and compressing it, 967*eda14cbcSMatt Macy * but performs much faster, especially during decompression (compression speed is tunable with compression level). 968*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 969*eda14cbcSMatt Macy * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary 970*eda14cbcSMatt Macy * Note 1 : Prefix buffer is referenced. It **must** outlive compression. 971*eda14cbcSMatt Macy * Its content must remain unmodified during compression. 972*eda14cbcSMatt Macy * Note 2 : If the intention is to diff some large src data blob with some prior version of itself, 973*eda14cbcSMatt Macy * ensure that the window size is large enough to contain the entire source. 974*eda14cbcSMatt Macy * See ZSTD_c_windowLog. 975*eda14cbcSMatt Macy * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters. 976*eda14cbcSMatt Macy * It's a CPU consuming operation, with non-negligible impact on latency. 977*eda14cbcSMatt Macy * If there is a need to use the same prefix multiple times, consider loadDictionary instead. 978*eda14cbcSMatt Macy * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent). 979*eda14cbcSMatt Macy * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */ 980*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, 981*eda14cbcSMatt Macy const void* prefix, size_t prefixSize); 982*eda14cbcSMatt Macy 983*eda14cbcSMatt Macy /*! ZSTD_DCtx_loadDictionary() : 984*eda14cbcSMatt Macy * Create an internal DDict from dict buffer, 985*eda14cbcSMatt Macy * to be used to decompress next frames. 986*eda14cbcSMatt Macy * The dictionary remains valid for all future frames, until explicitly invalidated. 987*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 988*eda14cbcSMatt Macy * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, 989*eda14cbcSMatt Macy * meaning "return to no-dictionary mode". 990*eda14cbcSMatt Macy * Note 1 : Loading a dictionary involves building tables, 991*eda14cbcSMatt Macy * which has a non-negligible impact on CPU usage and latency. 992*eda14cbcSMatt Macy * It's recommended to "load once, use many times", to amortize the cost 993*eda14cbcSMatt Macy * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading. 994*eda14cbcSMatt Macy * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead. 995*eda14cbcSMatt Macy * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of 996*eda14cbcSMatt Macy * how dictionary content is loaded and interpreted. 997*eda14cbcSMatt Macy */ 998*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 999*eda14cbcSMatt Macy 1000*eda14cbcSMatt Macy /*! ZSTD_DCtx_refDDict() : 1001*eda14cbcSMatt Macy * Reference a prepared dictionary, to be used to decompress next frames. 1002*eda14cbcSMatt Macy * The dictionary remains active for decompression of future frames using same DCtx. 1003*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1004*eda14cbcSMatt Macy * Note 1 : Currently, only one dictionary can be managed. 1005*eda14cbcSMatt Macy * Referencing a new dictionary effectively "discards" any previous one. 1006*eda14cbcSMatt Macy * Special: referencing a NULL DDict means "return to no-dictionary mode". 1007*eda14cbcSMatt Macy * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. 1008*eda14cbcSMatt Macy */ 1009*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 1010*eda14cbcSMatt Macy 1011*eda14cbcSMatt Macy /*! ZSTD_DCtx_refPrefix() : 1012*eda14cbcSMatt Macy * Reference a prefix (single-usage dictionary) to decompress next frame. 1013*eda14cbcSMatt Macy * This is the reverse operation of ZSTD_CCtx_refPrefix(), 1014*eda14cbcSMatt Macy * and must use the same prefix as the one used during compression. 1015*eda14cbcSMatt Macy * Prefix is **only used once**. Reference is discarded at end of frame. 1016*eda14cbcSMatt Macy * End of frame is reached when ZSTD_decompressStream() returns 0. 1017*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1018*eda14cbcSMatt Macy * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary 1019*eda14cbcSMatt Macy * Note 2 : Prefix buffer is referenced. It **must** outlive decompression. 1020*eda14cbcSMatt Macy * Prefix buffer must remain unmodified up to the end of frame, 1021*eda14cbcSMatt Macy * reached when ZSTD_decompressStream() returns 0. 1022*eda14cbcSMatt Macy * Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent). 1023*eda14cbcSMatt Macy * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section) 1024*eda14cbcSMatt Macy * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. 1025*eda14cbcSMatt Macy * A full dictionary is more costly, as it requires building tables. 1026*eda14cbcSMatt Macy */ 1027*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, 1028*eda14cbcSMatt Macy const void* prefix, size_t prefixSize); 1029*eda14cbcSMatt Macy 1030*eda14cbcSMatt Macy /* === Memory management === */ 1031*eda14cbcSMatt Macy 1032*eda14cbcSMatt Macy /*! ZSTD_sizeof_*() : 1033*eda14cbcSMatt Macy * These functions give the _current_ memory usage of selected object. 1034*eda14cbcSMatt Macy * Note that object memory usage can evolve (increase or decrease) over time. */ 1035*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); 1036*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); 1037*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); 1038*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); 1039*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); 1040*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); 1041*eda14cbcSMatt Macy 1042*eda14cbcSMatt Macy #endif /* ZSTD_H_235446 */ 1043*eda14cbcSMatt Macy 1044*eda14cbcSMatt Macy 1045*eda14cbcSMatt Macy /* ************************************************************************************** 1046*eda14cbcSMatt Macy * ADVANCED AND EXPERIMENTAL FUNCTIONS 1047*eda14cbcSMatt Macy **************************************************************************************** 1048*eda14cbcSMatt Macy * The definitions in the following section are considered experimental. 1049*eda14cbcSMatt Macy * They are provided for advanced scenarios. 1050*eda14cbcSMatt Macy * They should never be used with a dynamic library, as prototypes may change in the future. 1051*eda14cbcSMatt Macy * Use them only in association with static linking. 1052*eda14cbcSMatt Macy * ***************************************************************************************/ 1053*eda14cbcSMatt Macy 1054*eda14cbcSMatt Macy #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) 1055*eda14cbcSMatt Macy #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY 1056*eda14cbcSMatt Macy 1057*eda14cbcSMatt Macy /**************************************************************************************** 1058*eda14cbcSMatt Macy * experimental API (static linking only) 1059*eda14cbcSMatt Macy **************************************************************************************** 1060*eda14cbcSMatt Macy * The following symbols and constants 1061*eda14cbcSMatt Macy * are not planned to join "stable API" status in the near future. 1062*eda14cbcSMatt Macy * They can still change in future versions. 1063*eda14cbcSMatt Macy * Some of them are planned to remain in the static_only section indefinitely. 1064*eda14cbcSMatt Macy * Some of them might be removed in the future (especially when redundant with existing stable functions) 1065*eda14cbcSMatt Macy * ***************************************************************************************/ 1066*eda14cbcSMatt Macy 1067*eda14cbcSMatt Macy #define ZSTD_FRAMEHEADERSIZE_PREFIX(format) ((format) == ZSTD_f_zstd1 ? 5 : 1) /* minimum input size required to query frame header size */ 1068*eda14cbcSMatt Macy #define ZSTD_FRAMEHEADERSIZE_MIN(format) ((format) == ZSTD_f_zstd1 ? 6 : 2) 1069*eda14cbcSMatt Macy #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */ 1070*eda14cbcSMatt Macy #define ZSTD_SKIPPABLEHEADERSIZE 8 1071*eda14cbcSMatt Macy 1072*eda14cbcSMatt Macy /* compression parameter bounds */ 1073*eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MAX_32 30 1074*eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MAX_64 31 1075*eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) 1076*eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_MIN 10 1077*eda14cbcSMatt Macy #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30) 1078*eda14cbcSMatt Macy #define ZSTD_HASHLOG_MIN 6 1079*eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MAX_32 29 1080*eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MAX_64 30 1081*eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64)) 1082*eda14cbcSMatt Macy #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN 1083*eda14cbcSMatt Macy #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) 1084*eda14cbcSMatt Macy #define ZSTD_SEARCHLOG_MIN 1 1085*eda14cbcSMatt Macy #define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ 1086*eda14cbcSMatt Macy #define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */ 1087*eda14cbcSMatt Macy #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX 1088*eda14cbcSMatt Macy #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */ 1089*eda14cbcSMatt Macy #define ZSTD_STRATEGY_MIN ZSTD_fast 1090*eda14cbcSMatt Macy #define ZSTD_STRATEGY_MAX ZSTD_btultra2 1091*eda14cbcSMatt Macy 1092*eda14cbcSMatt Macy 1093*eda14cbcSMatt Macy #define ZSTD_OVERLAPLOG_MIN 0 1094*eda14cbcSMatt Macy #define ZSTD_OVERLAPLOG_MAX 9 1095*eda14cbcSMatt Macy 1096*eda14cbcSMatt Macy #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame 1097*eda14cbcSMatt Macy * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size, 1098*eda14cbcSMatt Macy * to preserve host's memory from unreasonable requirements. 1099*eda14cbcSMatt Macy * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,). 1100*eda14cbcSMatt Macy * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */ 1101*eda14cbcSMatt Macy 1102*eda14cbcSMatt Macy 1103*eda14cbcSMatt Macy /* LDM parameter bounds */ 1104*eda14cbcSMatt Macy #define ZSTD_LDM_HASHLOG_MIN ZSTD_HASHLOG_MIN 1105*eda14cbcSMatt Macy #define ZSTD_LDM_HASHLOG_MAX ZSTD_HASHLOG_MAX 1106*eda14cbcSMatt Macy #define ZSTD_LDM_MINMATCH_MIN 4 1107*eda14cbcSMatt Macy #define ZSTD_LDM_MINMATCH_MAX 4096 1108*eda14cbcSMatt Macy #define ZSTD_LDM_BUCKETSIZELOG_MIN 1 1109*eda14cbcSMatt Macy #define ZSTD_LDM_BUCKETSIZELOG_MAX 8 1110*eda14cbcSMatt Macy #define ZSTD_LDM_HASHRATELOG_MIN 0 1111*eda14cbcSMatt Macy #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN) 1112*eda14cbcSMatt Macy 1113*eda14cbcSMatt Macy /* Advanced parameter bounds */ 1114*eda14cbcSMatt Macy #define ZSTD_TARGETCBLOCKSIZE_MIN 64 1115*eda14cbcSMatt Macy #define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX 1116*eda14cbcSMatt Macy #define ZSTD_SRCSIZEHINT_MIN 0 1117*eda14cbcSMatt Macy #define ZSTD_SRCSIZEHINT_MAX INT_MAX 1118*eda14cbcSMatt Macy 1119*eda14cbcSMatt Macy /* internal */ 1120*eda14cbcSMatt Macy #define ZSTD_HASHLOG3_MAX 17 1121*eda14cbcSMatt Macy 1122*eda14cbcSMatt Macy 1123*eda14cbcSMatt Macy /* --- Advanced types --- */ 1124*eda14cbcSMatt Macy 1125*eda14cbcSMatt Macy typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params; 1126*eda14cbcSMatt Macy 1127*eda14cbcSMatt Macy typedef struct { 1128*eda14cbcSMatt Macy unsigned int matchPos; /* Match pos in dst */ 1129*eda14cbcSMatt Macy /* If seqDef.offset > 3, then this is seqDef.offset - 3 1130*eda14cbcSMatt Macy * If seqDef.offset < 3, then this is the corresponding repeat offset 1131*eda14cbcSMatt Macy * But if seqDef.offset < 3 and litLength == 0, this is the 1132*eda14cbcSMatt Macy * repeat offset before the corresponding repeat offset 1133*eda14cbcSMatt Macy * And if seqDef.offset == 3 and litLength == 0, this is the 1134*eda14cbcSMatt Macy * most recent repeat offset - 1 1135*eda14cbcSMatt Macy */ 1136*eda14cbcSMatt Macy unsigned int offset; 1137*eda14cbcSMatt Macy unsigned int litLength; /* Literal length */ 1138*eda14cbcSMatt Macy unsigned int matchLength; /* Match length */ 1139*eda14cbcSMatt Macy /* 0 when seq not rep and seqDef.offset otherwise 1140*eda14cbcSMatt Macy * when litLength == 0 this will be <= 4, otherwise <= 3 like normal 1141*eda14cbcSMatt Macy */ 1142*eda14cbcSMatt Macy unsigned int rep; 1143*eda14cbcSMatt Macy } ZSTD_Sequence; 1144*eda14cbcSMatt Macy 1145*eda14cbcSMatt Macy typedef struct { 1146*eda14cbcSMatt Macy unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ 1147*eda14cbcSMatt Macy unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ 1148*eda14cbcSMatt Macy unsigned hashLog; /**< dispatch table : larger == faster, more memory */ 1149*eda14cbcSMatt Macy unsigned searchLog; /**< nb of searches : larger == more compression, slower */ 1150*eda14cbcSMatt Macy unsigned minMatch; /**< match length searched : larger == faster decompression, sometimes less compression */ 1151*eda14cbcSMatt Macy unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ 1152*eda14cbcSMatt Macy ZSTD_strategy strategy; /**< see ZSTD_strategy definition above */ 1153*eda14cbcSMatt Macy } ZSTD_compressionParameters; 1154*eda14cbcSMatt Macy 1155*eda14cbcSMatt Macy typedef struct { 1156*eda14cbcSMatt Macy int contentSizeFlag; /**< 1: content size will be in frame header (when known) */ 1157*eda14cbcSMatt Macy int checksumFlag; /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */ 1158*eda14cbcSMatt Macy int noDictIDFlag; /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */ 1159*eda14cbcSMatt Macy } ZSTD_frameParameters; 1160*eda14cbcSMatt Macy 1161*eda14cbcSMatt Macy typedef struct { 1162*eda14cbcSMatt Macy ZSTD_compressionParameters cParams; 1163*eda14cbcSMatt Macy ZSTD_frameParameters fParams; 1164*eda14cbcSMatt Macy } ZSTD_parameters; 1165*eda14cbcSMatt Macy 1166*eda14cbcSMatt Macy typedef enum { 1167*eda14cbcSMatt Macy ZSTD_dct_auto = 0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ 1168*eda14cbcSMatt Macy ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ 1169*eda14cbcSMatt Macy ZSTD_dct_fullDict = 2 /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */ 1170*eda14cbcSMatt Macy } ZSTD_dictContentType_e; 1171*eda14cbcSMatt Macy 1172*eda14cbcSMatt Macy typedef enum { 1173*eda14cbcSMatt Macy ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */ 1174*eda14cbcSMatt Macy ZSTD_dlm_byRef = 1 /**< Reference dictionary content -- the dictionary buffer must outlive its users. */ 1175*eda14cbcSMatt Macy } ZSTD_dictLoadMethod_e; 1176*eda14cbcSMatt Macy 1177*eda14cbcSMatt Macy typedef enum { 1178*eda14cbcSMatt Macy ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */ 1179*eda14cbcSMatt Macy ZSTD_f_zstd1_magicless = 1 /* Variant of zstd frame format, without initial 4-bytes magic number. 1180*eda14cbcSMatt Macy * Useful to save 4 bytes per generated frame. 1181*eda14cbcSMatt Macy * Decoder cannot recognise automatically this format, requiring this instruction. */ 1182*eda14cbcSMatt Macy } ZSTD_format_e; 1183*eda14cbcSMatt Macy 1184*eda14cbcSMatt Macy typedef enum { 1185*eda14cbcSMatt Macy /* Note: this enum and the behavior it controls are effectively internal 1186*eda14cbcSMatt Macy * implementation details of the compressor. They are expected to continue 1187*eda14cbcSMatt Macy * to evolve and should be considered only in the context of extremely 1188*eda14cbcSMatt Macy * advanced performance tuning. 1189*eda14cbcSMatt Macy * 1190*eda14cbcSMatt Macy * Zstd currently supports the use of a CDict in three ways: 1191*eda14cbcSMatt Macy * 1192*eda14cbcSMatt Macy * - The contents of the CDict can be copied into the working context. This 1193*eda14cbcSMatt Macy * means that the compression can search both the dictionary and input 1194*eda14cbcSMatt Macy * while operating on a single set of internal tables. This makes 1195*eda14cbcSMatt Macy * the compression faster per-byte of input. However, the initial copy of 1196*eda14cbcSMatt Macy * the CDict's tables incurs a fixed cost at the beginning of the 1197*eda14cbcSMatt Macy * compression. For small compressions (< 8 KB), that copy can dominate 1198*eda14cbcSMatt Macy * the cost of the compression. 1199*eda14cbcSMatt Macy * 1200*eda14cbcSMatt Macy * - The CDict's tables can be used in-place. In this model, compression is 1201*eda14cbcSMatt Macy * slower per input byte, because the compressor has to search two sets of 1202*eda14cbcSMatt Macy * tables. However, this model incurs no start-up cost (as long as the 1203*eda14cbcSMatt Macy * working context's tables can be reused). For small inputs, this can be 1204*eda14cbcSMatt Macy * faster than copying the CDict's tables. 1205*eda14cbcSMatt Macy * 1206*eda14cbcSMatt Macy * - The CDict's tables are not used at all, and instead we use the working 1207*eda14cbcSMatt Macy * context alone to reload the dictionary and use params based on the source 1208*eda14cbcSMatt Macy * size. See ZSTD_compress_insertDictionary() and ZSTD_compress_usingDict(). 1209*eda14cbcSMatt Macy * This method is effective when the dictionary sizes are very small relative 1210*eda14cbcSMatt Macy * to the input size, and the input size is fairly large to begin with. 1211*eda14cbcSMatt Macy * 1212*eda14cbcSMatt Macy * Zstd has a simple internal heuristic that selects which strategy to use 1213*eda14cbcSMatt Macy * at the beginning of a compression. However, if experimentation shows that 1214*eda14cbcSMatt Macy * Zstd is making poor choices, it is possible to override that choice with 1215*eda14cbcSMatt Macy * this enum. 1216*eda14cbcSMatt Macy */ 1217*eda14cbcSMatt Macy ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */ 1218*eda14cbcSMatt Macy ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */ 1219*eda14cbcSMatt Macy ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */ 1220*eda14cbcSMatt Macy ZSTD_dictForceLoad = 3 /* Always reload the dictionary */ 1221*eda14cbcSMatt Macy } ZSTD_dictAttachPref_e; 1222*eda14cbcSMatt Macy 1223*eda14cbcSMatt Macy typedef enum { 1224*eda14cbcSMatt Macy ZSTD_lcm_auto = 0, /**< Automatically determine the compression mode based on the compression level. 1225*eda14cbcSMatt Macy * Negative compression levels will be uncompressed, and positive compression 1226*eda14cbcSMatt Macy * levels will be compressed. */ 1227*eda14cbcSMatt Macy ZSTD_lcm_huffman = 1, /**< Always attempt Huffman compression. Uncompressed literals will still be 1228*eda14cbcSMatt Macy * emitted if Huffman compression is not profitable. */ 1229*eda14cbcSMatt Macy ZSTD_lcm_uncompressed = 2 /**< Always emit uncompressed literals. */ 1230*eda14cbcSMatt Macy } ZSTD_literalCompressionMode_e; 1231*eda14cbcSMatt Macy 1232*eda14cbcSMatt Macy 1233*eda14cbcSMatt Macy /*************************************** 1234*eda14cbcSMatt Macy * Frame size functions 1235*eda14cbcSMatt Macy ***************************************/ 1236*eda14cbcSMatt Macy 1237*eda14cbcSMatt Macy /*! ZSTD_findDecompressedSize() : 1238*eda14cbcSMatt Macy * `src` should point to the start of a series of ZSTD encoded and/or skippable frames 1239*eda14cbcSMatt Macy * `srcSize` must be the _exact_ size of this series 1240*eda14cbcSMatt Macy * (i.e. there should be a frame boundary at `src + srcSize`) 1241*eda14cbcSMatt Macy * @return : - decompressed size of all data in all successive frames 1242*eda14cbcSMatt Macy * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN 1243*eda14cbcSMatt Macy * - if an error occurred: ZSTD_CONTENTSIZE_ERROR 1244*eda14cbcSMatt Macy * 1245*eda14cbcSMatt Macy * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. 1246*eda14cbcSMatt Macy * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. 1247*eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 1248*eda14cbcSMatt Macy * note 2 : decompressed size is always present when compression is done with ZSTD_compress() 1249*eda14cbcSMatt Macy * note 3 : decompressed size can be very large (64-bits value), 1250*eda14cbcSMatt Macy * potentially larger than what local system can handle as a single memory segment. 1251*eda14cbcSMatt Macy * In which case, it's necessary to use streaming mode to decompress data. 1252*eda14cbcSMatt Macy * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. 1253*eda14cbcSMatt Macy * Always ensure result fits within application's authorized limits. 1254*eda14cbcSMatt Macy * Each application can set its own limits. 1255*eda14cbcSMatt Macy * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to 1256*eda14cbcSMatt Macy * read each contained frame header. This is fast as most of the data is skipped, 1257*eda14cbcSMatt Macy * however it does mean that all frame data must be present and valid. */ 1258*eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); 1259*eda14cbcSMatt Macy 1260*eda14cbcSMatt Macy /*! ZSTD_decompressBound() : 1261*eda14cbcSMatt Macy * `src` should point to the start of a series of ZSTD encoded and/or skippable frames 1262*eda14cbcSMatt Macy * `srcSize` must be the _exact_ size of this series 1263*eda14cbcSMatt Macy * (i.e. there should be a frame boundary at `src + srcSize`) 1264*eda14cbcSMatt Macy * @return : - upper-bound for the decompressed size of all data in all successive frames 1265*eda14cbcSMatt Macy * - if an error occured: ZSTD_CONTENTSIZE_ERROR 1266*eda14cbcSMatt Macy * 1267*eda14cbcSMatt Macy * note 1 : an error can occur if `src` contains an invalid or incorrectly formatted frame. 1268*eda14cbcSMatt Macy * note 2 : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`. 1269*eda14cbcSMatt Macy * in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value. 1270*eda14cbcSMatt Macy * note 3 : when the decompressed size field isn't available, the upper-bound for that frame is calculated by: 1271*eda14cbcSMatt Macy * upper-bound = # blocks * min(128 KB, Window_Size) 1272*eda14cbcSMatt Macy */ 1273*eda14cbcSMatt Macy ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize); 1274*eda14cbcSMatt Macy 1275*eda14cbcSMatt Macy /*! ZSTD_frameHeaderSize() : 1276*eda14cbcSMatt Macy * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX. 1277*eda14cbcSMatt Macy * @return : size of the Frame Header, 1278*eda14cbcSMatt Macy * or an error code (if srcSize is too small) */ 1279*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); 1280*eda14cbcSMatt Macy 1281*eda14cbcSMatt Macy /*! ZSTD_getSequences() : 1282*eda14cbcSMatt Macy * Extract sequences from the sequence store 1283*eda14cbcSMatt Macy * zc can be used to insert custom compression params. 1284*eda14cbcSMatt Macy * This function invokes ZSTD_compress2 1285*eda14cbcSMatt Macy * @return : number of sequences extracted 1286*eda14cbcSMatt Macy */ 1287*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs, 1288*eda14cbcSMatt Macy size_t outSeqsSize, const void* src, size_t srcSize); 1289*eda14cbcSMatt Macy 1290*eda14cbcSMatt Macy 1291*eda14cbcSMatt Macy /*************************************** 1292*eda14cbcSMatt Macy * Memory management 1293*eda14cbcSMatt Macy ***************************************/ 1294*eda14cbcSMatt Macy 1295*eda14cbcSMatt Macy /*! ZSTD_estimate*() : 1296*eda14cbcSMatt Macy * These functions make it possible to estimate memory usage 1297*eda14cbcSMatt Macy * of a future {D,C}Ctx, before its creation. 1298*eda14cbcSMatt Macy * 1299*eda14cbcSMatt Macy * ZSTD_estimateCCtxSize() will provide a memory budget large enough 1300*eda14cbcSMatt Macy * for any compression level up to selected one. 1301*eda14cbcSMatt Macy * Note : Unlike ZSTD_estimateCStreamSize*(), this estimate 1302*eda14cbcSMatt Macy * does not include space for a window buffer. 1303*eda14cbcSMatt Macy * Therefore, the estimation is only guaranteed for single-shot compressions, not streaming. 1304*eda14cbcSMatt Macy * The estimate will assume the input may be arbitrarily large, 1305*eda14cbcSMatt Macy * which is the worst case. 1306*eda14cbcSMatt Macy * 1307*eda14cbcSMatt Macy * When srcSize can be bound by a known and rather "small" value, 1308*eda14cbcSMatt Macy * this fact can be used to provide a tighter estimation 1309*eda14cbcSMatt Macy * because the CCtx compression context will need less memory. 1310*eda14cbcSMatt Macy * This tighter estimation can be provided by more advanced functions 1311*eda14cbcSMatt Macy * ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(), 1312*eda14cbcSMatt Macy * and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter(). 1313*eda14cbcSMatt Macy * Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits. 1314*eda14cbcSMatt Macy * 1315*eda14cbcSMatt Macy * Note 2 : only single-threaded compression is supported. 1316*eda14cbcSMatt Macy * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1. 1317*eda14cbcSMatt Macy */ 1318*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel); 1319*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams); 1320*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params); 1321*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); 1322*eda14cbcSMatt Macy 1323*eda14cbcSMatt Macy /*! ZSTD_estimateCStreamSize() : 1324*eda14cbcSMatt Macy * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. 1325*eda14cbcSMatt Macy * It will also consider src size to be arbitrarily "large", which is worst case. 1326*eda14cbcSMatt Macy * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. 1327*eda14cbcSMatt Macy * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. 1328*eda14cbcSMatt Macy * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1. 1329*eda14cbcSMatt Macy * Note : CStream size estimation is only correct for single-threaded compression. 1330*eda14cbcSMatt Macy * ZSTD_DStream memory budget depends on window Size. 1331*eda14cbcSMatt Macy * This information can be passed manually, using ZSTD_estimateDStreamSize, 1332*eda14cbcSMatt Macy * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); 1333*eda14cbcSMatt Macy * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), 1334*eda14cbcSMatt Macy * an internal ?Dict will be created, which additional size is not estimated here. 1335*eda14cbcSMatt Macy * In this case, get total size by adding ZSTD_estimate?DictSize */ 1336*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel); 1337*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams); 1338*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params); 1339*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize); 1340*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize); 1341*eda14cbcSMatt Macy 1342*eda14cbcSMatt Macy /*! ZSTD_estimate?DictSize() : 1343*eda14cbcSMatt Macy * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). 1344*eda14cbcSMatt Macy * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced(). 1345*eda14cbcSMatt Macy * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller. 1346*eda14cbcSMatt Macy */ 1347*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel); 1348*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod); 1349*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod); 1350*eda14cbcSMatt Macy 1351*eda14cbcSMatt Macy /*! ZSTD_initStatic*() : 1352*eda14cbcSMatt Macy * Initialize an object using a pre-allocated fixed-size buffer. 1353*eda14cbcSMatt Macy * workspace: The memory area to emplace the object into. 1354*eda14cbcSMatt Macy * Provided pointer *must be 8-bytes aligned*. 1355*eda14cbcSMatt Macy * Buffer must outlive object. 1356*eda14cbcSMatt Macy * workspaceSize: Use ZSTD_estimate*Size() to determine 1357*eda14cbcSMatt Macy * how large workspace must be to support target scenario. 1358*eda14cbcSMatt Macy * @return : pointer to object (same address as workspace, just different type), 1359*eda14cbcSMatt Macy * or NULL if error (size too small, incorrect alignment, etc.) 1360*eda14cbcSMatt Macy * Note : zstd will never resize nor malloc() when using a static buffer. 1361*eda14cbcSMatt Macy * If the object requires more memory than available, 1362*eda14cbcSMatt Macy * zstd will just error out (typically ZSTD_error_memory_allocation). 1363*eda14cbcSMatt Macy * Note 2 : there is no corresponding "free" function. 1364*eda14cbcSMatt Macy * Since workspace is allocated externally, it must be freed externally too. 1365*eda14cbcSMatt Macy * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level 1366*eda14cbcSMatt Macy * into its associated cParams. 1367*eda14cbcSMatt Macy * Limitation 1 : currently not compatible with internal dictionary creation, triggered by 1368*eda14cbcSMatt Macy * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict(). 1369*eda14cbcSMatt Macy * Limitation 2 : static cctx currently not compatible with multi-threading. 1370*eda14cbcSMatt Macy * Limitation 3 : static dctx is incompatible with legacy support. 1371*eda14cbcSMatt Macy */ 1372*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize); 1373*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */ 1374*eda14cbcSMatt Macy 1375*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize); 1376*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */ 1377*eda14cbcSMatt Macy 1378*eda14cbcSMatt Macy ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict( 1379*eda14cbcSMatt Macy void* workspace, size_t workspaceSize, 1380*eda14cbcSMatt Macy const void* dict, size_t dictSize, 1381*eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1382*eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType, 1383*eda14cbcSMatt Macy ZSTD_compressionParameters cParams); 1384*eda14cbcSMatt Macy 1385*eda14cbcSMatt Macy ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict( 1386*eda14cbcSMatt Macy void* workspace, size_t workspaceSize, 1387*eda14cbcSMatt Macy const void* dict, size_t dictSize, 1388*eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1389*eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType); 1390*eda14cbcSMatt Macy 1391*eda14cbcSMatt Macy 1392*eda14cbcSMatt Macy /*! Custom memory allocation : 1393*eda14cbcSMatt Macy * These prototypes make it possible to pass your own allocation/free functions. 1394*eda14cbcSMatt Macy * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below. 1395*eda14cbcSMatt Macy * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones. 1396*eda14cbcSMatt Macy */ 1397*eda14cbcSMatt Macy typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); 1398*eda14cbcSMatt Macy typedef void (*ZSTD_freeFunction) (void* opaque, void* address); 1399*eda14cbcSMatt Macy typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; 1400*eda14cbcSMatt Macy static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */ 1401*eda14cbcSMatt Macy 1402*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); 1403*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); 1404*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); 1405*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); 1406*eda14cbcSMatt Macy 1407*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, 1408*eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1409*eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType, 1410*eda14cbcSMatt Macy ZSTD_compressionParameters cParams, 1411*eda14cbcSMatt Macy ZSTD_customMem customMem); 1412*eda14cbcSMatt Macy 1413*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, 1414*eda14cbcSMatt Macy ZSTD_dictLoadMethod_e dictLoadMethod, 1415*eda14cbcSMatt Macy ZSTD_dictContentType_e dictContentType, 1416*eda14cbcSMatt Macy ZSTD_customMem customMem); 1417*eda14cbcSMatt Macy 1418*eda14cbcSMatt Macy 1419*eda14cbcSMatt Macy 1420*eda14cbcSMatt Macy /*************************************** 1421*eda14cbcSMatt Macy * Advanced compression functions 1422*eda14cbcSMatt Macy ***************************************/ 1423*eda14cbcSMatt Macy 1424*eda14cbcSMatt Macy /*! ZSTD_createCDict_byReference() : 1425*eda14cbcSMatt Macy * Create a digested dictionary for compression 1426*eda14cbcSMatt Macy * Dictionary content is just referenced, not duplicated. 1427*eda14cbcSMatt Macy * As a consequence, `dictBuffer` **must** outlive CDict, 1428*eda14cbcSMatt Macy * and its content must remain unmodified throughout the lifetime of CDict. 1429*eda14cbcSMatt Macy * note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */ 1430*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); 1431*eda14cbcSMatt Macy 1432*eda14cbcSMatt Macy /*! ZSTD_getCParams() : 1433*eda14cbcSMatt Macy * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. 1434*eda14cbcSMatt Macy * `estimatedSrcSize` value is optional, select 0 if not known */ 1435*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 1436*eda14cbcSMatt Macy 1437*eda14cbcSMatt Macy /*! ZSTD_getParams() : 1438*eda14cbcSMatt Macy * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. 1439*eda14cbcSMatt Macy * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */ 1440*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); 1441*eda14cbcSMatt Macy 1442*eda14cbcSMatt Macy /*! ZSTD_checkCParams() : 1443*eda14cbcSMatt Macy * Ensure param values remain within authorized range. 1444*eda14cbcSMatt Macy * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */ 1445*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); 1446*eda14cbcSMatt Macy 1447*eda14cbcSMatt Macy /*! ZSTD_adjustCParams() : 1448*eda14cbcSMatt Macy * optimize params for a given `srcSize` and `dictSize`. 1449*eda14cbcSMatt Macy * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN. 1450*eda14cbcSMatt Macy * `dictSize` must be `0` when there is no dictionary. 1451*eda14cbcSMatt Macy * cPar can be invalid : all parameters will be clamped within valid range in the @return struct. 1452*eda14cbcSMatt Macy * This function never fails (wide contract) */ 1453*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); 1454*eda14cbcSMatt Macy 1455*eda14cbcSMatt Macy /*! ZSTD_compress_advanced() : 1456*eda14cbcSMatt Macy * Note : this function is now DEPRECATED. 1457*eda14cbcSMatt Macy * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters. 1458*eda14cbcSMatt Macy * This prototype will be marked as deprecated and generate compilation warning on reaching v1.5.x */ 1459*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx, 1460*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 1461*eda14cbcSMatt Macy const void* src, size_t srcSize, 1462*eda14cbcSMatt Macy const void* dict,size_t dictSize, 1463*eda14cbcSMatt Macy ZSTD_parameters params); 1464*eda14cbcSMatt Macy 1465*eda14cbcSMatt Macy /*! ZSTD_compress_usingCDict_advanced() : 1466*eda14cbcSMatt Macy * Note : this function is now REDUNDANT. 1467*eda14cbcSMatt Macy * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_loadDictionary() and other parameter setters. 1468*eda14cbcSMatt Macy * This prototype will be marked as deprecated and generate compilation warning in some future version */ 1469*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, 1470*eda14cbcSMatt Macy void* dst, size_t dstCapacity, 1471*eda14cbcSMatt Macy const void* src, size_t srcSize, 1472*eda14cbcSMatt Macy const ZSTD_CDict* cdict, 1473*eda14cbcSMatt Macy ZSTD_frameParameters fParams); 1474*eda14cbcSMatt Macy 1475*eda14cbcSMatt Macy 1476*eda14cbcSMatt Macy /*! ZSTD_CCtx_loadDictionary_byReference() : 1477*eda14cbcSMatt Macy * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx. 1478*eda14cbcSMatt Macy * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */ 1479*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize); 1480*eda14cbcSMatt Macy 1481*eda14cbcSMatt Macy /*! ZSTD_CCtx_loadDictionary_advanced() : 1482*eda14cbcSMatt Macy * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over 1483*eda14cbcSMatt Macy * how to load the dictionary (by copy ? by reference ?) 1484*eda14cbcSMatt Macy * and how to interpret it (automatic ? force raw mode ? full mode only ?) */ 1485*eda14cbcSMatt Macy 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); 1486*eda14cbcSMatt Macy 1487*eda14cbcSMatt Macy /*! ZSTD_CCtx_refPrefix_advanced() : 1488*eda14cbcSMatt Macy * Same as ZSTD_CCtx_refPrefix(), but gives finer control over 1489*eda14cbcSMatt Macy * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ 1490*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 1491*eda14cbcSMatt Macy 1492*eda14cbcSMatt Macy /* === experimental parameters === */ 1493*eda14cbcSMatt Macy /* these parameters can be used with ZSTD_setParameter() 1494*eda14cbcSMatt Macy * they are not guaranteed to remain supported in the future */ 1495*eda14cbcSMatt Macy 1496*eda14cbcSMatt Macy /* Enables rsyncable mode, 1497*eda14cbcSMatt Macy * which makes compressed files more rsync friendly 1498*eda14cbcSMatt Macy * by adding periodic synchronization points to the compressed data. 1499*eda14cbcSMatt Macy * The target average block size is ZSTD_c_jobSize / 2. 1500*eda14cbcSMatt Macy * It's possible to modify the job size to increase or decrease 1501*eda14cbcSMatt Macy * the granularity of the synchronization point. 1502*eda14cbcSMatt Macy * Once the jobSize is smaller than the window size, 1503*eda14cbcSMatt Macy * it will result in compression ratio degradation. 1504*eda14cbcSMatt Macy * NOTE 1: rsyncable mode only works when multithreading is enabled. 1505*eda14cbcSMatt Macy * NOTE 2: rsyncable performs poorly in combination with long range mode, 1506*eda14cbcSMatt Macy * since it will decrease the effectiveness of synchronization points, 1507*eda14cbcSMatt Macy * though mileage may vary. 1508*eda14cbcSMatt Macy * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s. 1509*eda14cbcSMatt Macy * If the selected compression level is already running significantly slower, 1510*eda14cbcSMatt Macy * the overall speed won't be significantly impacted. 1511*eda14cbcSMatt Macy */ 1512*eda14cbcSMatt Macy #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1 1513*eda14cbcSMatt Macy 1514*eda14cbcSMatt Macy /* Select a compression format. 1515*eda14cbcSMatt Macy * The value must be of type ZSTD_format_e. 1516*eda14cbcSMatt Macy * See ZSTD_format_e enum definition for details */ 1517*eda14cbcSMatt Macy #define ZSTD_c_format ZSTD_c_experimentalParam2 1518*eda14cbcSMatt Macy 1519*eda14cbcSMatt Macy /* Force back-reference distances to remain < windowSize, 1520*eda14cbcSMatt Macy * even when referencing into Dictionary content (default:0) */ 1521*eda14cbcSMatt Macy #define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3 1522*eda14cbcSMatt Macy 1523*eda14cbcSMatt Macy /* Controls whether the contents of a CDict 1524*eda14cbcSMatt Macy * are used in place, or copied into the working context. 1525*eda14cbcSMatt Macy * Accepts values from the ZSTD_dictAttachPref_e enum. 1526*eda14cbcSMatt Macy * See the comments on that enum for an explanation of the feature. */ 1527*eda14cbcSMatt Macy #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4 1528*eda14cbcSMatt Macy 1529*eda14cbcSMatt Macy /* Controls how the literals are compressed (default is auto). 1530*eda14cbcSMatt Macy * The value must be of type ZSTD_literalCompressionMode_e. 1531*eda14cbcSMatt Macy * See ZSTD_literalCompressionMode_t enum definition for details. 1532*eda14cbcSMatt Macy */ 1533*eda14cbcSMatt Macy #define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5 1534*eda14cbcSMatt Macy 1535*eda14cbcSMatt Macy /* Tries to fit compressed block size to be around targetCBlockSize. 1536*eda14cbcSMatt Macy * No target when targetCBlockSize == 0. 1537*eda14cbcSMatt Macy * There is no guarantee on compressed block size (default:0) */ 1538*eda14cbcSMatt Macy #define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6 1539*eda14cbcSMatt Macy 1540*eda14cbcSMatt Macy /* User's best guess of source size. 1541*eda14cbcSMatt Macy * Hint is not valid when srcSizeHint == 0. 1542*eda14cbcSMatt Macy * There is no guarantee that hint is close to actual source size, 1543*eda14cbcSMatt Macy * but compression ratio may regress significantly if guess considerably underestimates */ 1544*eda14cbcSMatt Macy #define ZSTD_c_srcSizeHint ZSTD_c_experimentalParam7 1545*eda14cbcSMatt Macy 1546*eda14cbcSMatt Macy /*! ZSTD_CCtx_getParameter() : 1547*eda14cbcSMatt Macy * Get the requested compression parameter value, selected by enum ZSTD_cParameter, 1548*eda14cbcSMatt Macy * and store it into int* value. 1549*eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested with ZSTD_isError()). 1550*eda14cbcSMatt Macy */ 1551*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value); 1552*eda14cbcSMatt Macy 1553*eda14cbcSMatt Macy 1554*eda14cbcSMatt Macy /*! ZSTD_CCtx_params : 1555*eda14cbcSMatt Macy * Quick howto : 1556*eda14cbcSMatt Macy * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure 1557*eda14cbcSMatt Macy * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into 1558*eda14cbcSMatt Macy * an existing ZSTD_CCtx_params structure. 1559*eda14cbcSMatt Macy * This is similar to 1560*eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(). 1561*eda14cbcSMatt Macy * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to 1562*eda14cbcSMatt Macy * an existing CCtx. 1563*eda14cbcSMatt Macy * These parameters will be applied to 1564*eda14cbcSMatt Macy * all subsequent frames. 1565*eda14cbcSMatt Macy * - ZSTD_compressStream2() : Do compression using the CCtx. 1566*eda14cbcSMatt Macy * - ZSTD_freeCCtxParams() : Free the memory. 1567*eda14cbcSMatt Macy * 1568*eda14cbcSMatt Macy * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() 1569*eda14cbcSMatt Macy * for static allocation of CCtx for single-threaded compression. 1570*eda14cbcSMatt Macy */ 1571*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void); 1572*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params); 1573*eda14cbcSMatt Macy 1574*eda14cbcSMatt Macy /*! ZSTD_CCtxParams_reset() : 1575*eda14cbcSMatt Macy * Reset params to default values. 1576*eda14cbcSMatt Macy */ 1577*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params); 1578*eda14cbcSMatt Macy 1579*eda14cbcSMatt Macy /*! ZSTD_CCtxParams_init() : 1580*eda14cbcSMatt Macy * Initializes the compression parameters of cctxParams according to 1581*eda14cbcSMatt Macy * compression level. All other parameters are reset to their default values. 1582*eda14cbcSMatt Macy */ 1583*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel); 1584*eda14cbcSMatt Macy 1585*eda14cbcSMatt Macy /*! ZSTD_CCtxParams_init_advanced() : 1586*eda14cbcSMatt Macy * Initializes the compression and frame parameters of cctxParams according to 1587*eda14cbcSMatt Macy * params. All other parameters are reset to their default values. 1588*eda14cbcSMatt Macy */ 1589*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params); 1590*eda14cbcSMatt Macy 1591*eda14cbcSMatt Macy /*! ZSTD_CCtxParams_setParameter() : 1592*eda14cbcSMatt Macy * Similar to ZSTD_CCtx_setParameter. 1593*eda14cbcSMatt Macy * Set one compression parameter, selected by enum ZSTD_cParameter. 1594*eda14cbcSMatt Macy * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams(). 1595*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1596*eda14cbcSMatt Macy */ 1597*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value); 1598*eda14cbcSMatt Macy 1599*eda14cbcSMatt Macy /*! ZSTD_CCtxParams_getParameter() : 1600*eda14cbcSMatt Macy * Similar to ZSTD_CCtx_getParameter. 1601*eda14cbcSMatt Macy * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter. 1602*eda14cbcSMatt Macy * @result : 0, or an error code (which can be tested with ZSTD_isError()). 1603*eda14cbcSMatt Macy */ 1604*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value); 1605*eda14cbcSMatt Macy 1606*eda14cbcSMatt Macy /*! ZSTD_CCtx_setParametersUsingCCtxParams() : 1607*eda14cbcSMatt Macy * Apply a set of ZSTD_CCtx_params to the compression context. 1608*eda14cbcSMatt Macy * This can be done even after compression is started, 1609*eda14cbcSMatt Macy * if nbWorkers==0, this will have no impact until a new compression is started. 1610*eda14cbcSMatt Macy * if nbWorkers>=1, new parameters will be picked up at next job, 1611*eda14cbcSMatt Macy * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated). 1612*eda14cbcSMatt Macy */ 1613*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams( 1614*eda14cbcSMatt Macy ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params); 1615*eda14cbcSMatt Macy 1616*eda14cbcSMatt Macy /*! ZSTD_compressStream2_simpleArgs() : 1617*eda14cbcSMatt Macy * Same as ZSTD_compressStream2(), 1618*eda14cbcSMatt Macy * but using only integral types as arguments. 1619*eda14cbcSMatt Macy * This variant might be helpful for binders from dynamic languages 1620*eda14cbcSMatt Macy * which have troubles handling structures containing memory pointers. 1621*eda14cbcSMatt Macy */ 1622*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressStream2_simpleArgs ( 1623*eda14cbcSMatt Macy ZSTD_CCtx* cctx, 1624*eda14cbcSMatt Macy void* dst, size_t dstCapacity, size_t* dstPos, 1625*eda14cbcSMatt Macy const void* src, size_t srcSize, size_t* srcPos, 1626*eda14cbcSMatt Macy ZSTD_EndDirective endOp); 1627*eda14cbcSMatt Macy 1628*eda14cbcSMatt Macy 1629*eda14cbcSMatt Macy /*************************************** 1630*eda14cbcSMatt Macy * Advanced decompression functions 1631*eda14cbcSMatt Macy ***************************************/ 1632*eda14cbcSMatt Macy 1633*eda14cbcSMatt Macy /*! ZSTD_isFrame() : 1634*eda14cbcSMatt Macy * Tells if the content of `buffer` starts with a valid Frame Identifier. 1635*eda14cbcSMatt Macy * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. 1636*eda14cbcSMatt Macy * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. 1637*eda14cbcSMatt Macy * Note 3 : Skippable Frame Identifiers are considered valid. */ 1638*eda14cbcSMatt Macy ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); 1639*eda14cbcSMatt Macy 1640*eda14cbcSMatt Macy /*! ZSTD_createDDict_byReference() : 1641*eda14cbcSMatt Macy * Create a digested dictionary, ready to start decompression operation without startup delay. 1642*eda14cbcSMatt Macy * Dictionary content is referenced, and therefore stays in dictBuffer. 1643*eda14cbcSMatt Macy * It is important that dictBuffer outlives DDict, 1644*eda14cbcSMatt Macy * it must remain read accessible throughout the lifetime of DDict */ 1645*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); 1646*eda14cbcSMatt Macy 1647*eda14cbcSMatt Macy /*! ZSTD_DCtx_loadDictionary_byReference() : 1648*eda14cbcSMatt Macy * Same as ZSTD_DCtx_loadDictionary(), 1649*eda14cbcSMatt Macy * but references `dict` content instead of copying it into `dctx`. 1650*eda14cbcSMatt Macy * This saves memory if `dict` remains around., 1651*eda14cbcSMatt Macy * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */ 1652*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 1653*eda14cbcSMatt Macy 1654*eda14cbcSMatt Macy /*! ZSTD_DCtx_loadDictionary_advanced() : 1655*eda14cbcSMatt Macy * Same as ZSTD_DCtx_loadDictionary(), 1656*eda14cbcSMatt Macy * but gives direct control over 1657*eda14cbcSMatt Macy * how to load the dictionary (by copy ? by reference ?) 1658*eda14cbcSMatt Macy * and how to interpret it (automatic ? force raw mode ? full mode only ?). */ 1659*eda14cbcSMatt Macy 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); 1660*eda14cbcSMatt Macy 1661*eda14cbcSMatt Macy /*! ZSTD_DCtx_refPrefix_advanced() : 1662*eda14cbcSMatt Macy * Same as ZSTD_DCtx_refPrefix(), but gives finer control over 1663*eda14cbcSMatt Macy * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ 1664*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType); 1665*eda14cbcSMatt Macy 1666*eda14cbcSMatt Macy /*! ZSTD_DCtx_setMaxWindowSize() : 1667*eda14cbcSMatt Macy * Refuses allocating internal buffers for frames requiring a window size larger than provided limit. 1668*eda14cbcSMatt Macy * This protects a decoder context from reserving too much memory for itself (potential attack scenario). 1669*eda14cbcSMatt Macy * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. 1670*eda14cbcSMatt Macy * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) 1671*eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()). 1672*eda14cbcSMatt Macy */ 1673*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize); 1674*eda14cbcSMatt Macy 1675*eda14cbcSMatt Macy /* ZSTD_d_format 1676*eda14cbcSMatt Macy * experimental parameter, 1677*eda14cbcSMatt Macy * allowing selection between ZSTD_format_e input compression formats 1678*eda14cbcSMatt Macy */ 1679*eda14cbcSMatt Macy #define ZSTD_d_format ZSTD_d_experimentalParam1 1680*eda14cbcSMatt Macy /* ZSTD_d_stableOutBuffer 1681*eda14cbcSMatt Macy * Experimental parameter. 1682*eda14cbcSMatt Macy * Default is 0 == disabled. Set to 1 to enable. 1683*eda14cbcSMatt Macy * 1684*eda14cbcSMatt Macy * Tells the decompressor that the ZSTD_outBuffer will ALWAYS be the same 1685*eda14cbcSMatt Macy * between calls, except for the modifications that zstd makes to pos (the 1686*eda14cbcSMatt Macy * caller must not modify pos). This is checked by the decompressor, and 1687*eda14cbcSMatt Macy * decompression will fail if it ever changes. Therefore the ZSTD_outBuffer 1688*eda14cbcSMatt Macy * MUST be large enough to fit the entire decompressed frame. This will be 1689*eda14cbcSMatt Macy * checked when the frame content size is known. The data in the ZSTD_outBuffer 1690*eda14cbcSMatt Macy * in the range [dst, dst + pos) MUST not be modified during decompression 1691*eda14cbcSMatt Macy * or you will get data corruption. 1692*eda14cbcSMatt Macy * 1693*eda14cbcSMatt Macy * When this flags is enabled zstd won't allocate an output buffer, because 1694*eda14cbcSMatt Macy * it can write directly to the ZSTD_outBuffer, but it will still allocate 1695*eda14cbcSMatt Macy * an input buffer large enough to fit any compressed block. This will also 1696*eda14cbcSMatt Macy * avoid the memcpy() from the internal output buffer to the ZSTD_outBuffer. 1697*eda14cbcSMatt Macy * If you need to avoid the input buffer allocation use the buffer-less 1698*eda14cbcSMatt Macy * streaming API. 1699*eda14cbcSMatt Macy * 1700*eda14cbcSMatt Macy * NOTE: So long as the ZSTD_outBuffer always points to valid memory, using 1701*eda14cbcSMatt Macy * this flag is ALWAYS memory safe, and will never access out-of-bounds 1702*eda14cbcSMatt Macy * memory. However, decompression WILL fail if you violate the preconditions. 1703*eda14cbcSMatt Macy * 1704*eda14cbcSMatt Macy * WARNING: The data in the ZSTD_outBuffer in the range [dst, dst + pos) MUST 1705*eda14cbcSMatt Macy * not be modified during decompression or you will get data corruption. This 1706*eda14cbcSMatt Macy * is because zstd needs to reference data in the ZSTD_outBuffer to regenerate 1707*eda14cbcSMatt Macy * matches. Normally zstd maintains its own buffer for this purpose, but passing 1708*eda14cbcSMatt Macy * this flag tells zstd to use the user provided buffer. 1709*eda14cbcSMatt Macy */ 1710*eda14cbcSMatt Macy #define ZSTD_d_stableOutBuffer ZSTD_d_experimentalParam2 1711*eda14cbcSMatt Macy 1712*eda14cbcSMatt Macy /*! ZSTD_DCtx_setFormat() : 1713*eda14cbcSMatt Macy * Instruct the decoder context about what kind of data to decode next. 1714*eda14cbcSMatt Macy * This instruction is mandatory to decode data without a fully-formed header, 1715*eda14cbcSMatt Macy * such ZSTD_f_zstd1_magicless for example. 1716*eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()). */ 1717*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format); 1718*eda14cbcSMatt Macy 1719*eda14cbcSMatt Macy /*! ZSTD_decompressStream_simpleArgs() : 1720*eda14cbcSMatt Macy * Same as ZSTD_decompressStream(), 1721*eda14cbcSMatt Macy * but using only integral types as arguments. 1722*eda14cbcSMatt Macy * This can be helpful for binders from dynamic languages 1723*eda14cbcSMatt Macy * which have troubles handling structures containing memory pointers. 1724*eda14cbcSMatt Macy */ 1725*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs ( 1726*eda14cbcSMatt Macy ZSTD_DCtx* dctx, 1727*eda14cbcSMatt Macy void* dst, size_t dstCapacity, size_t* dstPos, 1728*eda14cbcSMatt Macy const void* src, size_t srcSize, size_t* srcPos); 1729*eda14cbcSMatt Macy 1730*eda14cbcSMatt Macy 1731*eda14cbcSMatt Macy /******************************************************************** 1732*eda14cbcSMatt Macy * Advanced streaming functions 1733*eda14cbcSMatt Macy * Warning : most of these functions are now redundant with the Advanced API. 1734*eda14cbcSMatt Macy * Once Advanced API reaches "stable" status, 1735*eda14cbcSMatt Macy * redundant functions will be deprecated, and then at some point removed. 1736*eda14cbcSMatt Macy ********************************************************************/ 1737*eda14cbcSMatt Macy 1738*eda14cbcSMatt Macy /*===== Advanced Streaming compression functions =====*/ 1739*eda14cbcSMatt Macy /**! ZSTD_initCStream_srcSize() : 1740*eda14cbcSMatt Macy * This function is deprecated, and equivalent to: 1741*eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1742*eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) 1743*eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 1744*eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1745*eda14cbcSMatt Macy * 1746*eda14cbcSMatt Macy * pledgedSrcSize must be correct. If it is not known at init time, use 1747*eda14cbcSMatt Macy * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, 1748*eda14cbcSMatt Macy * "0" also disables frame content size field. It may be enabled in the future. 1749*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1750*eda14cbcSMatt Macy */ 1751*eda14cbcSMatt Macy ZSTDLIB_API size_t 1752*eda14cbcSMatt Macy ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, 1753*eda14cbcSMatt Macy int compressionLevel, 1754*eda14cbcSMatt Macy unsigned long long pledgedSrcSize); 1755*eda14cbcSMatt Macy 1756*eda14cbcSMatt Macy /**! ZSTD_initCStream_usingDict() : 1757*eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1758*eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1759*eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); 1760*eda14cbcSMatt Macy * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); 1761*eda14cbcSMatt Macy * 1762*eda14cbcSMatt Macy * Creates of an internal CDict (incompatible with static CCtx), except if 1763*eda14cbcSMatt Macy * dict == NULL or dictSize < 8, in which case no dict is used. 1764*eda14cbcSMatt Macy * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if 1765*eda14cbcSMatt Macy * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy. 1766*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1767*eda14cbcSMatt Macy */ 1768*eda14cbcSMatt Macy ZSTDLIB_API size_t 1769*eda14cbcSMatt Macy ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, 1770*eda14cbcSMatt Macy const void* dict, size_t dictSize, 1771*eda14cbcSMatt Macy int compressionLevel); 1772*eda14cbcSMatt Macy 1773*eda14cbcSMatt Macy /**! ZSTD_initCStream_advanced() : 1774*eda14cbcSMatt Macy * This function is deprecated, and is approximately equivalent to: 1775*eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1776*eda14cbcSMatt Macy * // Pseudocode: Set each zstd parameter and leave the rest as-is. 1777*eda14cbcSMatt Macy * for ((param, value) : params) { 1778*eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, param, value); 1779*eda14cbcSMatt Macy * } 1780*eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1781*eda14cbcSMatt Macy * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); 1782*eda14cbcSMatt Macy * 1783*eda14cbcSMatt Macy * dict is loaded with ZSTD_dct_auto and ZSTD_dlm_byCopy. 1784*eda14cbcSMatt Macy * pledgedSrcSize must be correct. 1785*eda14cbcSMatt Macy * If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. 1786*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1787*eda14cbcSMatt Macy */ 1788*eda14cbcSMatt Macy ZSTDLIB_API size_t 1789*eda14cbcSMatt Macy ZSTD_initCStream_advanced(ZSTD_CStream* zcs, 1790*eda14cbcSMatt Macy const void* dict, size_t dictSize, 1791*eda14cbcSMatt Macy ZSTD_parameters params, 1792*eda14cbcSMatt Macy unsigned long long pledgedSrcSize); 1793*eda14cbcSMatt Macy 1794*eda14cbcSMatt Macy /**! ZSTD_initCStream_usingCDict() : 1795*eda14cbcSMatt Macy * This function is deprecated, and equivalent to: 1796*eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1797*eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, cdict); 1798*eda14cbcSMatt Macy * 1799*eda14cbcSMatt Macy * note : cdict will just be referenced, and must outlive compression session 1800*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1801*eda14cbcSMatt Macy */ 1802*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); 1803*eda14cbcSMatt Macy 1804*eda14cbcSMatt Macy /**! ZSTD_initCStream_usingCDict_advanced() : 1805*eda14cbcSMatt Macy * This function is DEPRECATED, and is approximately equivalent to: 1806*eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1807*eda14cbcSMatt Macy * // Pseudocode: Set each zstd frame parameter and leave the rest as-is. 1808*eda14cbcSMatt Macy * for ((fParam, value) : fParams) { 1809*eda14cbcSMatt Macy * ZSTD_CCtx_setParameter(zcs, fParam, value); 1810*eda14cbcSMatt Macy * } 1811*eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1812*eda14cbcSMatt Macy * ZSTD_CCtx_refCDict(zcs, cdict); 1813*eda14cbcSMatt Macy * 1814*eda14cbcSMatt Macy * same as ZSTD_initCStream_usingCDict(), with control over frame parameters. 1815*eda14cbcSMatt Macy * pledgedSrcSize must be correct. If srcSize is not known at init time, use 1816*eda14cbcSMatt Macy * value ZSTD_CONTENTSIZE_UNKNOWN. 1817*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1818*eda14cbcSMatt Macy */ 1819*eda14cbcSMatt Macy ZSTDLIB_API size_t 1820*eda14cbcSMatt Macy ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, 1821*eda14cbcSMatt Macy const ZSTD_CDict* cdict, 1822*eda14cbcSMatt Macy ZSTD_frameParameters fParams, 1823*eda14cbcSMatt Macy unsigned long long pledgedSrcSize); 1824*eda14cbcSMatt Macy 1825*eda14cbcSMatt Macy /*! ZSTD_resetCStream() : 1826*eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1827*eda14cbcSMatt Macy * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); 1828*eda14cbcSMatt Macy * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); 1829*eda14cbcSMatt Macy * 1830*eda14cbcSMatt Macy * start a new frame, using same parameters from previous frame. 1831*eda14cbcSMatt Macy * This is typically useful to skip dictionary loading stage, since it will re-use it in-place. 1832*eda14cbcSMatt Macy * Note that zcs must be init at least once before using ZSTD_resetCStream(). 1833*eda14cbcSMatt Macy * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN. 1834*eda14cbcSMatt Macy * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. 1835*eda14cbcSMatt Macy * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs, 1836*eda14cbcSMatt Macy * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead. 1837*eda14cbcSMatt Macy * @return : 0, or an error code (which can be tested using ZSTD_isError()) 1838*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1839*eda14cbcSMatt Macy */ 1840*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); 1841*eda14cbcSMatt Macy 1842*eda14cbcSMatt Macy 1843*eda14cbcSMatt Macy typedef struct { 1844*eda14cbcSMatt Macy unsigned long long ingested; /* nb input bytes read and buffered */ 1845*eda14cbcSMatt Macy unsigned long long consumed; /* nb input bytes actually compressed */ 1846*eda14cbcSMatt Macy unsigned long long produced; /* nb of compressed bytes generated and buffered */ 1847*eda14cbcSMatt Macy unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */ 1848*eda14cbcSMatt Macy unsigned currentJobID; /* MT only : latest started job nb */ 1849*eda14cbcSMatt Macy unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */ 1850*eda14cbcSMatt Macy } ZSTD_frameProgression; 1851*eda14cbcSMatt Macy 1852*eda14cbcSMatt Macy /* ZSTD_getFrameProgression() : 1853*eda14cbcSMatt Macy * tells how much data has been ingested (read from input) 1854*eda14cbcSMatt Macy * consumed (input actually compressed) and produced (output) for current frame. 1855*eda14cbcSMatt Macy * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed. 1856*eda14cbcSMatt Macy * Aggregates progression inside active worker threads. 1857*eda14cbcSMatt Macy */ 1858*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx); 1859*eda14cbcSMatt Macy 1860*eda14cbcSMatt Macy /*! ZSTD_toFlushNow() : 1861*eda14cbcSMatt Macy * Tell how many bytes are ready to be flushed immediately. 1862*eda14cbcSMatt Macy * Useful for multithreading scenarios (nbWorkers >= 1). 1863*eda14cbcSMatt Macy * Probe the oldest active job, defined as oldest job not yet entirely flushed, 1864*eda14cbcSMatt Macy * and check its output buffer. 1865*eda14cbcSMatt Macy * @return : amount of data stored in oldest job and ready to be flushed immediately. 1866*eda14cbcSMatt Macy * if @return == 0, it means either : 1867*eda14cbcSMatt Macy * + there is no active job (could be checked with ZSTD_frameProgression()), or 1868*eda14cbcSMatt Macy * + oldest job is still actively compressing data, 1869*eda14cbcSMatt Macy * but everything it has produced has also been flushed so far, 1870*eda14cbcSMatt Macy * therefore flush speed is limited by production speed of oldest job 1871*eda14cbcSMatt Macy * irrespective of the speed of concurrent (and newer) jobs. 1872*eda14cbcSMatt Macy */ 1873*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx); 1874*eda14cbcSMatt Macy 1875*eda14cbcSMatt Macy 1876*eda14cbcSMatt Macy /*===== Advanced Streaming decompression functions =====*/ 1877*eda14cbcSMatt Macy /** 1878*eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1879*eda14cbcSMatt Macy * 1880*eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 1881*eda14cbcSMatt Macy * ZSTD_DCtx_loadDictionary(zds, dict, dictSize); 1882*eda14cbcSMatt Macy * 1883*eda14cbcSMatt Macy * note: no dictionary will be used if dict == NULL or dictSize < 8 1884*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1885*eda14cbcSMatt Macy */ 1886*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); 1887*eda14cbcSMatt Macy 1888*eda14cbcSMatt Macy /** 1889*eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1890*eda14cbcSMatt Macy * 1891*eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 1892*eda14cbcSMatt Macy * ZSTD_DCtx_refDDict(zds, ddict); 1893*eda14cbcSMatt Macy * 1894*eda14cbcSMatt Macy * note : ddict is referenced, it must outlive decompression session 1895*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1896*eda14cbcSMatt Macy */ 1897*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); 1898*eda14cbcSMatt Macy 1899*eda14cbcSMatt Macy /** 1900*eda14cbcSMatt Macy * This function is deprecated, and is equivalent to: 1901*eda14cbcSMatt Macy * 1902*eda14cbcSMatt Macy * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only); 1903*eda14cbcSMatt Macy * 1904*eda14cbcSMatt Macy * re-use decompression parameters from previous init; saves dictionary loading 1905*eda14cbcSMatt Macy * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x 1906*eda14cbcSMatt Macy */ 1907*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); 1908*eda14cbcSMatt Macy 1909*eda14cbcSMatt Macy 1910*eda14cbcSMatt Macy /********************************************************************* 1911*eda14cbcSMatt Macy * Buffer-less and synchronous inner streaming functions 1912*eda14cbcSMatt Macy * 1913*eda14cbcSMatt Macy * This is an advanced API, giving full control over buffer management, for users which need direct control over memory. 1914*eda14cbcSMatt Macy * But it's also a complex one, with several restrictions, documented below. 1915*eda14cbcSMatt Macy * Prefer normal streaming API for an easier experience. 1916*eda14cbcSMatt Macy ********************************************************************* */ 1917*eda14cbcSMatt Macy 1918*eda14cbcSMatt Macy /** 1919*eda14cbcSMatt Macy Buffer-less streaming compression (synchronous mode) 1920*eda14cbcSMatt Macy 1921*eda14cbcSMatt Macy A ZSTD_CCtx object is required to track streaming operations. 1922*eda14cbcSMatt Macy Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. 1923*eda14cbcSMatt Macy ZSTD_CCtx object can be re-used multiple times within successive compression operations. 1924*eda14cbcSMatt Macy 1925*eda14cbcSMatt Macy Start by initializing a context. 1926*eda14cbcSMatt Macy Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, 1927*eda14cbcSMatt Macy or ZSTD_compressBegin_advanced(), for finer parameter control. 1928*eda14cbcSMatt Macy It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() 1929*eda14cbcSMatt Macy 1930*eda14cbcSMatt Macy Then, consume your input using ZSTD_compressContinue(). 1931*eda14cbcSMatt Macy There are some important considerations to keep in mind when using this advanced function : 1932*eda14cbcSMatt Macy - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only. 1933*eda14cbcSMatt Macy - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks. 1934*eda14cbcSMatt Macy - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. 1935*eda14cbcSMatt Macy Worst case evaluation is provided by ZSTD_compressBound(). 1936*eda14cbcSMatt Macy ZSTD_compressContinue() doesn't guarantee recover after a failed compression. 1937*eda14cbcSMatt Macy - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). 1938*eda14cbcSMatt Macy It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) 1939*eda14cbcSMatt Macy - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. 1940*eda14cbcSMatt Macy In which case, it will "discard" the relevant memory section from its history. 1941*eda14cbcSMatt Macy 1942*eda14cbcSMatt Macy Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. 1943*eda14cbcSMatt Macy It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. 1944*eda14cbcSMatt Macy Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders. 1945*eda14cbcSMatt Macy 1946*eda14cbcSMatt Macy `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again. 1947*eda14cbcSMatt Macy */ 1948*eda14cbcSMatt Macy 1949*eda14cbcSMatt Macy /*===== Buffer-less streaming compression functions =====*/ 1950*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); 1951*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 1952*eda14cbcSMatt Macy 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 */ 1953*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */ 1954*eda14cbcSMatt Macy 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 */ 1955*eda14cbcSMatt Macy 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 */ 1956*eda14cbcSMatt Macy 1957*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 1958*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 1959*eda14cbcSMatt Macy 1960*eda14cbcSMatt Macy 1961*eda14cbcSMatt Macy /*- 1962*eda14cbcSMatt Macy Buffer-less streaming decompression (synchronous mode) 1963*eda14cbcSMatt Macy 1964*eda14cbcSMatt Macy A ZSTD_DCtx object is required to track streaming operations. 1965*eda14cbcSMatt Macy Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 1966*eda14cbcSMatt Macy A ZSTD_DCtx object can be re-used multiple times. 1967*eda14cbcSMatt Macy 1968*eda14cbcSMatt Macy First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader(). 1969*eda14cbcSMatt Macy Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough. 1970*eda14cbcSMatt Macy Data fragment must be large enough to ensure successful decoding. 1971*eda14cbcSMatt Macy `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough. 1972*eda14cbcSMatt Macy @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled. 1973*eda14cbcSMatt Macy >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. 1974*eda14cbcSMatt Macy errorCode, which can be tested using ZSTD_isError(). 1975*eda14cbcSMatt Macy 1976*eda14cbcSMatt Macy It fills a ZSTD_frameHeader structure with important information to correctly decode the frame, 1977*eda14cbcSMatt Macy such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`). 1978*eda14cbcSMatt Macy Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information. 1979*eda14cbcSMatt Macy As a consequence, check that values remain within valid application range. 1980*eda14cbcSMatt Macy For example, do not allocate memory blindly, check that `windowSize` is within expectation. 1981*eda14cbcSMatt Macy Each application can set its own limits, depending on local restrictions. 1982*eda14cbcSMatt Macy For extended interoperability, it is recommended to support `windowSize` of at least 8 MB. 1983*eda14cbcSMatt Macy 1984*eda14cbcSMatt Macy ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes. 1985*eda14cbcSMatt Macy ZSTD_decompressContinue() is very sensitive to contiguity, 1986*eda14cbcSMatt Macy if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, 1987*eda14cbcSMatt Macy or that previous contiguous segment is large enough to properly handle maximum back-reference distance. 1988*eda14cbcSMatt Macy There are multiple ways to guarantee this condition. 1989*eda14cbcSMatt Macy 1990*eda14cbcSMatt Macy The most memory efficient way is to use a round buffer of sufficient size. 1991*eda14cbcSMatt Macy Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(), 1992*eda14cbcSMatt Macy which can @return an error code if required value is too large for current system (in 32-bits mode). 1993*eda14cbcSMatt Macy In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one, 1994*eda14cbcSMatt Macy up to the moment there is not enough room left in the buffer to guarantee decoding another full block, 1995*eda14cbcSMatt Macy which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`. 1996*eda14cbcSMatt Macy At which point, decoding can resume from the beginning of the buffer. 1997*eda14cbcSMatt Macy Note that already decoded data stored in the buffer should be flushed before being overwritten. 1998*eda14cbcSMatt Macy 1999*eda14cbcSMatt Macy There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory. 2000*eda14cbcSMatt Macy 2001*eda14cbcSMatt Macy Finally, if you control the compression process, you can also ignore all buffer size rules, 2002*eda14cbcSMatt Macy as long as the encoder and decoder progress in "lock-step", 2003*eda14cbcSMatt Macy aka use exactly the same buffer sizes, break contiguity at the same place, etc. 2004*eda14cbcSMatt Macy 2005*eda14cbcSMatt Macy Once buffers are setup, start decompression, with ZSTD_decompressBegin(). 2006*eda14cbcSMatt Macy If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). 2007*eda14cbcSMatt Macy 2008*eda14cbcSMatt Macy Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 2009*eda14cbcSMatt Macy ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 2010*eda14cbcSMatt Macy ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. 2011*eda14cbcSMatt Macy 2012*eda14cbcSMatt Macy @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). 2013*eda14cbcSMatt Macy It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item. 2014*eda14cbcSMatt Macy It can also be an error code, which can be tested with ZSTD_isError(). 2015*eda14cbcSMatt Macy 2016*eda14cbcSMatt Macy A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 2017*eda14cbcSMatt Macy Context can then be reset to start a new decompression. 2018*eda14cbcSMatt Macy 2019*eda14cbcSMatt Macy Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). 2020*eda14cbcSMatt Macy This information is not required to properly decode a frame. 2021*eda14cbcSMatt Macy 2022*eda14cbcSMatt Macy == Special case : skippable frames == 2023*eda14cbcSMatt Macy 2024*eda14cbcSMatt Macy Skippable frames allow integration of user-defined data into a flow of concatenated frames. 2025*eda14cbcSMatt Macy Skippable frames will be ignored (skipped) by decompressor. 2026*eda14cbcSMatt Macy The format of skippable frames is as follows : 2027*eda14cbcSMatt Macy a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F 2028*eda14cbcSMatt Macy b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits 2029*eda14cbcSMatt Macy c) Frame Content - any content (User Data) of length equal to Frame Size 2030*eda14cbcSMatt Macy For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame. 2031*eda14cbcSMatt Macy For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content. 2032*eda14cbcSMatt Macy */ 2033*eda14cbcSMatt Macy 2034*eda14cbcSMatt Macy /*===== Buffer-less streaming decompression functions =====*/ 2035*eda14cbcSMatt Macy typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e; 2036*eda14cbcSMatt Macy typedef struct { 2037*eda14cbcSMatt Macy unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */ 2038*eda14cbcSMatt Macy unsigned long long windowSize; /* can be very large, up to <= frameContentSize */ 2039*eda14cbcSMatt Macy unsigned blockSizeMax; 2040*eda14cbcSMatt Macy ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */ 2041*eda14cbcSMatt Macy unsigned headerSize; 2042*eda14cbcSMatt Macy unsigned dictID; 2043*eda14cbcSMatt Macy unsigned checksumFlag; 2044*eda14cbcSMatt Macy } ZSTD_frameHeader; 2045*eda14cbcSMatt Macy 2046*eda14cbcSMatt Macy /*! ZSTD_getFrameHeader() : 2047*eda14cbcSMatt Macy * decode Frame Header, or requires larger `srcSize`. 2048*eda14cbcSMatt Macy * @return : 0, `zfhPtr` is correctly filled, 2049*eda14cbcSMatt Macy * >0, `srcSize` is too small, value is wanted `srcSize` amount, 2050*eda14cbcSMatt Macy * or an error code, which can be tested using ZSTD_isError() */ 2051*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 2052*eda14cbcSMatt Macy /*! ZSTD_getFrameHeader_advanced() : 2053*eda14cbcSMatt Macy * same as ZSTD_getFrameHeader(), 2054*eda14cbcSMatt Macy * with added capability to select a format (like ZSTD_f_zstd1_magicless) */ 2055*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format); 2056*eda14cbcSMatt Macy 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 */ 2057*eda14cbcSMatt Macy 2058*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); 2059*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); 2060*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); 2061*eda14cbcSMatt Macy 2062*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); 2063*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2064*eda14cbcSMatt Macy 2065*eda14cbcSMatt Macy /* misc */ 2066*eda14cbcSMatt Macy ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); 2067*eda14cbcSMatt Macy typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; 2068*eda14cbcSMatt Macy ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); 2069*eda14cbcSMatt Macy 2070*eda14cbcSMatt Macy 2071*eda14cbcSMatt Macy 2072*eda14cbcSMatt Macy 2073*eda14cbcSMatt Macy /* ============================ */ 2074*eda14cbcSMatt Macy /** Block level API */ 2075*eda14cbcSMatt Macy /* ============================ */ 2076*eda14cbcSMatt Macy 2077*eda14cbcSMatt Macy /*! 2078*eda14cbcSMatt Macy Block functions produce and decode raw zstd blocks, without frame metadata. 2079*eda14cbcSMatt Macy Frame metadata cost is typically ~12 bytes, which can be non-negligible for very small blocks (< 100 bytes). 2080*eda14cbcSMatt Macy But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes. 2081*eda14cbcSMatt Macy 2082*eda14cbcSMatt Macy A few rules to respect : 2083*eda14cbcSMatt Macy - Compressing and decompressing require a context structure 2084*eda14cbcSMatt Macy + Use ZSTD_createCCtx() and ZSTD_createDCtx() 2085*eda14cbcSMatt Macy - It is necessary to init context before starting 2086*eda14cbcSMatt Macy + compression : any ZSTD_compressBegin*() variant, including with dictionary 2087*eda14cbcSMatt Macy + decompression : any ZSTD_decompressBegin*() variant, including with dictionary 2088*eda14cbcSMatt Macy + copyCCtx() and copyDCtx() can be used too 2089*eda14cbcSMatt Macy - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB 2090*eda14cbcSMatt Macy + If input is larger than a block size, it's necessary to split input data into multiple blocks 2091*eda14cbcSMatt Macy + For inputs larger than a single block, consider using regular ZSTD_compress() instead. 2092*eda14cbcSMatt Macy Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block. 2093*eda14cbcSMatt Macy - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) ! 2094*eda14cbcSMatt Macy ===> In which case, nothing is produced into `dst` ! 2095*eda14cbcSMatt Macy + User __must__ test for such outcome and deal directly with uncompressed data 2096*eda14cbcSMatt Macy + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0. 2097*eda14cbcSMatt Macy Doing so would mess up with statistics history, leading to potential data corruption. 2098*eda14cbcSMatt Macy + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !! 2099*eda14cbcSMatt Macy + In case of multiple successive blocks, should some of them be uncompressed, 2100*eda14cbcSMatt Macy decoder must be informed of their existence in order to follow proper history. 2101*eda14cbcSMatt Macy Use ZSTD_insertBlock() for such a case. 2102*eda14cbcSMatt Macy */ 2103*eda14cbcSMatt Macy 2104*eda14cbcSMatt Macy /*===== Raw zstd block functions =====*/ 2105*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_getBlockSize (const ZSTD_CCtx* cctx); 2106*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2107*eda14cbcSMatt Macy ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 2108*eda14cbcSMatt Macy 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. */ 2109*eda14cbcSMatt Macy 2110*eda14cbcSMatt Macy 2111*eda14cbcSMatt Macy #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ 2112*eda14cbcSMatt Macy 2113*eda14cbcSMatt Macy #if defined (__cplusplus) 2114*eda14cbcSMatt Macy } 2115*eda14cbcSMatt Macy #endif 2116