1*3117ece4Schristos /* 2*3117ece4Schristos * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates. 3*3117ece4Schristos * All rights reserved. 4*3117ece4Schristos * 5*3117ece4Schristos * This source code is licensed under both the BSD-style license (found in the 6*3117ece4Schristos * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*3117ece4Schristos * in the COPYING file in the root directory of this source tree). 8*3117ece4Schristos * You may select, at your option, one of the above-listed licenses. 9*3117ece4Schristos */ 10*3117ece4Schristos 11*3117ece4Schristos #ifndef ZSTDv07_H_235446 12*3117ece4Schristos #define ZSTDv07_H_235446 13*3117ece4Schristos 14*3117ece4Schristos #if defined (__cplusplus) 15*3117ece4Schristos extern "C" { 16*3117ece4Schristos #endif 17*3117ece4Schristos 18*3117ece4Schristos /*====== Dependency ======*/ 19*3117ece4Schristos #include <stddef.h> /* size_t */ 20*3117ece4Schristos 21*3117ece4Schristos 22*3117ece4Schristos /*====== Export for Windows ======*/ 23*3117ece4Schristos /*! 24*3117ece4Schristos * ZSTDv07_DLL_EXPORT : 25*3117ece4Schristos * Enable exporting of functions when building a Windows DLL 26*3117ece4Schristos */ 27*3117ece4Schristos #if defined(_WIN32) && defined(ZSTDv07_DLL_EXPORT) && (ZSTDv07_DLL_EXPORT==1) 28*3117ece4Schristos # define ZSTDLIBv07_API __declspec(dllexport) 29*3117ece4Schristos #else 30*3117ece4Schristos # define ZSTDLIBv07_API 31*3117ece4Schristos #endif 32*3117ece4Schristos 33*3117ece4Schristos 34*3117ece4Schristos /* ************************************* 35*3117ece4Schristos * Simple API 36*3117ece4Schristos ***************************************/ 37*3117ece4Schristos /*! ZSTDv07_getDecompressedSize() : 38*3117ece4Schristos * @return : decompressed size if known, 0 otherwise. 39*3117ece4Schristos note 1 : if `0`, follow up with ZSTDv07_getFrameParams() to know precise failure cause. 40*3117ece4Schristos note 2 : decompressed size could be wrong or intentionally modified ! 41*3117ece4Schristos always ensure results fit within application's authorized limits */ 42*3117ece4Schristos unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize); 43*3117ece4Schristos 44*3117ece4Schristos /*! ZSTDv07_decompress() : 45*3117ece4Schristos `compressedSize` : must be _exact_ size of compressed input, otherwise decompression will fail. 46*3117ece4Schristos `dstCapacity` must be equal or larger than originalSize. 47*3117ece4Schristos @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 48*3117ece4Schristos or an errorCode if it fails (which can be tested using ZSTDv07_isError()) */ 49*3117ece4Schristos ZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity, 50*3117ece4Schristos const void* src, size_t compressedSize); 51*3117ece4Schristos 52*3117ece4Schristos /** 53*3117ece4Schristos ZSTDv07_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.7.x format 54*3117ece4Schristos srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src' 55*3117ece4Schristos cSize (output parameter) : the number of bytes that would be read to decompress this frame 56*3117ece4Schristos or an error code if it fails (which can be tested using ZSTDv01_isError()) 57*3117ece4Schristos dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame 58*3117ece4Schristos or ZSTD_CONTENTSIZE_ERROR if an error occurs 59*3117ece4Schristos 60*3117ece4Schristos note : assumes `cSize` and `dBound` are _not_ NULL. 61*3117ece4Schristos */ 62*3117ece4Schristos void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize, 63*3117ece4Schristos size_t* cSize, unsigned long long* dBound); 64*3117ece4Schristos 65*3117ece4Schristos /*====== Helper functions ======*/ 66*3117ece4Schristos ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 67*3117ece4Schristos ZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code); /*!< provides readable string from an error code */ 68*3117ece4Schristos 69*3117ece4Schristos 70*3117ece4Schristos /*-************************************* 71*3117ece4Schristos * Explicit memory management 72*3117ece4Schristos ***************************************/ 73*3117ece4Schristos /** Decompression context */ 74*3117ece4Schristos typedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx; 75*3117ece4Schristos ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void); 76*3117ece4Schristos ZSTDLIBv07_API size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx); /*!< @return : errorCode */ 77*3117ece4Schristos 78*3117ece4Schristos /** ZSTDv07_decompressDCtx() : 79*3117ece4Schristos * Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */ 80*3117ece4Schristos ZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 81*3117ece4Schristos 82*3117ece4Schristos 83*3117ece4Schristos /*-************************ 84*3117ece4Schristos * Simple dictionary API 85*3117ece4Schristos ***************************/ 86*3117ece4Schristos /*! ZSTDv07_decompress_usingDict() : 87*3117ece4Schristos * Decompression using a pre-defined Dictionary content (see dictBuilder). 88*3117ece4Schristos * Dictionary must be identical to the one used during compression. 89*3117ece4Schristos * Note : This function load the dictionary, resulting in a significant startup time */ 90*3117ece4Schristos ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx, 91*3117ece4Schristos void* dst, size_t dstCapacity, 92*3117ece4Schristos const void* src, size_t srcSize, 93*3117ece4Schristos const void* dict,size_t dictSize); 94*3117ece4Schristos 95*3117ece4Schristos 96*3117ece4Schristos /*-************************** 97*3117ece4Schristos * Advanced Dictionary API 98*3117ece4Schristos ****************************/ 99*3117ece4Schristos /*! ZSTDv07_createDDict() : 100*3117ece4Schristos * Create a digested dictionary, ready to start decompression operation without startup delay. 101*3117ece4Schristos * `dict` can be released after creation */ 102*3117ece4Schristos typedef struct ZSTDv07_DDict_s ZSTDv07_DDict; 103*3117ece4Schristos ZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize); 104*3117ece4Schristos ZSTDLIBv07_API size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict); 105*3117ece4Schristos 106*3117ece4Schristos /*! ZSTDv07_decompress_usingDDict() : 107*3117ece4Schristos * Decompression using a pre-digested Dictionary 108*3117ece4Schristos * Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */ 109*3117ece4Schristos ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx, 110*3117ece4Schristos void* dst, size_t dstCapacity, 111*3117ece4Schristos const void* src, size_t srcSize, 112*3117ece4Schristos const ZSTDv07_DDict* ddict); 113*3117ece4Schristos 114*3117ece4Schristos typedef struct { 115*3117ece4Schristos unsigned long long frameContentSize; 116*3117ece4Schristos unsigned windowSize; 117*3117ece4Schristos unsigned dictID; 118*3117ece4Schristos unsigned checksumFlag; 119*3117ece4Schristos } ZSTDv07_frameParams; 120*3117ece4Schristos 121*3117ece4Schristos ZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 122*3117ece4Schristos 123*3117ece4Schristos 124*3117ece4Schristos 125*3117ece4Schristos 126*3117ece4Schristos /* ************************************* 127*3117ece4Schristos * Streaming functions 128*3117ece4Schristos ***************************************/ 129*3117ece4Schristos typedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx; 130*3117ece4Schristos ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void); 131*3117ece4Schristos ZSTDLIBv07_API size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx); 132*3117ece4Schristos 133*3117ece4Schristos ZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx); 134*3117ece4Schristos ZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize); 135*3117ece4Schristos 136*3117ece4Schristos ZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx, 137*3117ece4Schristos void* dst, size_t* dstCapacityPtr, 138*3117ece4Schristos const void* src, size_t* srcSizePtr); 139*3117ece4Schristos 140*3117ece4Schristos /*-*************************************************************************** 141*3117ece4Schristos * Streaming decompression howto 142*3117ece4Schristos * 143*3117ece4Schristos * A ZBUFFv07_DCtx object is required to track streaming operations. 144*3117ece4Schristos * Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources. 145*3117ece4Schristos * Use ZBUFFv07_decompressInit() to start a new decompression operation, 146*3117ece4Schristos * or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary. 147*3117ece4Schristos * Note that ZBUFFv07_DCtx objects can be re-init multiple times. 148*3117ece4Schristos * 149*3117ece4Schristos * Use ZBUFFv07_decompressContinue() repetitively to consume your input. 150*3117ece4Schristos * *srcSizePtr and *dstCapacityPtr can be any size. 151*3117ece4Schristos * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr. 152*3117ece4Schristos * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again. 153*3117ece4Schristos * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`. 154*3117ece4Schristos * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency), 155*3117ece4Schristos * or 0 when a frame is completely decoded, 156*3117ece4Schristos * or an error code, which can be tested using ZBUFFv07_isError(). 157*3117ece4Schristos * 158*3117ece4Schristos * Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize() 159*3117ece4Schristos * output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded. 160*3117ece4Schristos * input : ZBUFFv07_recommendedDInSize == 128KB + 3; 161*3117ece4Schristos * just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . 162*3117ece4Schristos * *******************************************************************************/ 163*3117ece4Schristos 164*3117ece4Schristos 165*3117ece4Schristos /* ************************************* 166*3117ece4Schristos * Tool functions 167*3117ece4Schristos ***************************************/ 168*3117ece4Schristos ZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode); 169*3117ece4Schristos ZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode); 170*3117ece4Schristos 171*3117ece4Schristos /** Functions below provide recommended buffer sizes for Compression or Decompression operations. 172*3117ece4Schristos * These sizes are just hints, they tend to offer better latency */ 173*3117ece4Schristos ZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void); 174*3117ece4Schristos ZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void); 175*3117ece4Schristos 176*3117ece4Schristos 177*3117ece4Schristos /*-************************************* 178*3117ece4Schristos * Constants 179*3117ece4Schristos ***************************************/ 180*3117ece4Schristos #define ZSTDv07_MAGICNUMBER 0xFD2FB527 /* v0.7 */ 181*3117ece4Schristos 182*3117ece4Schristos 183*3117ece4Schristos #if defined (__cplusplus) 184*3117ece4Schristos } 185*3117ece4Schristos #endif 186*3117ece4Schristos 187*3117ece4Schristos #endif /* ZSTDv07_H_235446 */ 188