10c16b537SWarner Losh /* 2*5ff13fbcSAllan Jude * Copyright (c) Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh 110c16b537SWarner Losh #ifndef ZSTDv07_H_235446 120c16b537SWarner Losh #define ZSTDv07_H_235446 130c16b537SWarner Losh 140c16b537SWarner Losh #if defined (__cplusplus) 150c16b537SWarner Losh extern "C" { 160c16b537SWarner Losh #endif 170c16b537SWarner Losh 180c16b537SWarner Losh /*====== Dependency ======*/ 190c16b537SWarner Losh #include <stddef.h> /* size_t */ 200c16b537SWarner Losh 210c16b537SWarner Losh 220c16b537SWarner Losh /*====== Export for Windows ======*/ 230c16b537SWarner Losh /*! 240c16b537SWarner Losh * ZSTDv07_DLL_EXPORT : 250c16b537SWarner Losh * Enable exporting of functions when building a Windows DLL 260c16b537SWarner Losh */ 270c16b537SWarner Losh #if defined(_WIN32) && defined(ZSTDv07_DLL_EXPORT) && (ZSTDv07_DLL_EXPORT==1) 280c16b537SWarner Losh # define ZSTDLIBv07_API __declspec(dllexport) 290c16b537SWarner Losh #else 300c16b537SWarner Losh # define ZSTDLIBv07_API 310c16b537SWarner Losh #endif 320c16b537SWarner Losh 330c16b537SWarner Losh 340c16b537SWarner Losh /* ************************************* 350c16b537SWarner Losh * Simple API 360c16b537SWarner Losh ***************************************/ 370c16b537SWarner Losh /*! ZSTDv07_getDecompressedSize() : 380c16b537SWarner Losh * @return : decompressed size if known, 0 otherwise. 390c16b537SWarner Losh note 1 : if `0`, follow up with ZSTDv07_getFrameParams() to know precise failure cause. 400c16b537SWarner Losh note 2 : decompressed size could be wrong or intentionally modified ! 410c16b537SWarner Losh always ensure results fit within application's authorized limits */ 420c16b537SWarner Losh unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize); 430c16b537SWarner Losh 440c16b537SWarner Losh /*! ZSTDv07_decompress() : 450c16b537SWarner Losh `compressedSize` : must be _exact_ size of compressed input, otherwise decompression will fail. 460c16b537SWarner Losh `dstCapacity` must be equal or larger than originalSize. 470c16b537SWarner Losh @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 480c16b537SWarner Losh or an errorCode if it fails (which can be tested using ZSTDv07_isError()) */ 490c16b537SWarner Losh ZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity, 500c16b537SWarner Losh const void* src, size_t compressedSize); 510c16b537SWarner Losh 520c16b537SWarner Losh /** 532b9c00cbSConrad Meyer ZSTDv07_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.7.x format 542b9c00cbSConrad Meyer srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src' 552b9c00cbSConrad Meyer cSize (output parameter) : the number of bytes that would be read to decompress this frame 562b9c00cbSConrad Meyer or an error code if it fails (which can be tested using ZSTDv01_isError()) 572b9c00cbSConrad Meyer dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame 582b9c00cbSConrad Meyer or ZSTD_CONTENTSIZE_ERROR if an error occurs 592b9c00cbSConrad Meyer 602b9c00cbSConrad Meyer note : assumes `cSize` and `dBound` are _not_ NULL. 610c16b537SWarner Losh */ 622b9c00cbSConrad Meyer void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize, 632b9c00cbSConrad Meyer size_t* cSize, unsigned long long* dBound); 640c16b537SWarner Losh 650c16b537SWarner Losh /*====== Helper functions ======*/ 660c16b537SWarner Losh ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 670c16b537SWarner Losh ZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code); /*!< provides readable string from an error code */ 680c16b537SWarner Losh 690c16b537SWarner Losh 700c16b537SWarner Losh /*-************************************* 710c16b537SWarner Losh * Explicit memory management 720c16b537SWarner Losh ***************************************/ 730c16b537SWarner Losh /** Decompression context */ 740c16b537SWarner Losh typedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx; 750c16b537SWarner Losh ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void); 760c16b537SWarner Losh ZSTDLIBv07_API size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx); /*!< @return : errorCode */ 770c16b537SWarner Losh 780c16b537SWarner Losh /** ZSTDv07_decompressDCtx() : 790c16b537SWarner Losh * Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */ 800c16b537SWarner Losh ZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 810c16b537SWarner Losh 820c16b537SWarner Losh 830c16b537SWarner Losh /*-************************ 840c16b537SWarner Losh * Simple dictionary API 850c16b537SWarner Losh ***************************/ 860c16b537SWarner Losh /*! ZSTDv07_decompress_usingDict() : 870c16b537SWarner Losh * Decompression using a pre-defined Dictionary content (see dictBuilder). 880c16b537SWarner Losh * Dictionary must be identical to the one used during compression. 890c16b537SWarner Losh * Note : This function load the dictionary, resulting in a significant startup time */ 900c16b537SWarner Losh ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx, 910c16b537SWarner Losh void* dst, size_t dstCapacity, 920c16b537SWarner Losh const void* src, size_t srcSize, 930c16b537SWarner Losh const void* dict,size_t dictSize); 940c16b537SWarner Losh 950c16b537SWarner Losh 960c16b537SWarner Losh /*-************************** 970c16b537SWarner Losh * Advanced Dictionary API 980c16b537SWarner Losh ****************************/ 990c16b537SWarner Losh /*! ZSTDv07_createDDict() : 1000c16b537SWarner Losh * Create a digested dictionary, ready to start decompression operation without startup delay. 1010c16b537SWarner Losh * `dict` can be released after creation */ 1020c16b537SWarner Losh typedef struct ZSTDv07_DDict_s ZSTDv07_DDict; 1030c16b537SWarner Losh ZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize); 1040c16b537SWarner Losh ZSTDLIBv07_API size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict); 1050c16b537SWarner Losh 1060c16b537SWarner Losh /*! ZSTDv07_decompress_usingDDict() : 1070c16b537SWarner Losh * Decompression using a pre-digested Dictionary 1080c16b537SWarner Losh * Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */ 1090c16b537SWarner Losh ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx, 1100c16b537SWarner Losh void* dst, size_t dstCapacity, 1110c16b537SWarner Losh const void* src, size_t srcSize, 1120c16b537SWarner Losh const ZSTDv07_DDict* ddict); 1130c16b537SWarner Losh 1140c16b537SWarner Losh typedef struct { 1150c16b537SWarner Losh unsigned long long frameContentSize; 1160c16b537SWarner Losh unsigned windowSize; 1170c16b537SWarner Losh unsigned dictID; 1180c16b537SWarner Losh unsigned checksumFlag; 1190c16b537SWarner Losh } ZSTDv07_frameParams; 1200c16b537SWarner Losh 1210c16b537SWarner Losh ZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 1220c16b537SWarner Losh 1230c16b537SWarner Losh 1240c16b537SWarner Losh 1250c16b537SWarner Losh 1260c16b537SWarner Losh /* ************************************* 1270c16b537SWarner Losh * Streaming functions 1280c16b537SWarner Losh ***************************************/ 1290c16b537SWarner Losh typedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx; 1300c16b537SWarner Losh ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void); 1310c16b537SWarner Losh ZSTDLIBv07_API size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx); 1320c16b537SWarner Losh 1330c16b537SWarner Losh ZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx); 1340c16b537SWarner Losh ZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize); 1350c16b537SWarner Losh 1360c16b537SWarner Losh ZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx, 1370c16b537SWarner Losh void* dst, size_t* dstCapacityPtr, 1380c16b537SWarner Losh const void* src, size_t* srcSizePtr); 1390c16b537SWarner Losh 1400c16b537SWarner Losh /*-*************************************************************************** 1410c16b537SWarner Losh * Streaming decompression howto 1420c16b537SWarner Losh * 1430c16b537SWarner Losh * A ZBUFFv07_DCtx object is required to track streaming operations. 1440c16b537SWarner Losh * Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources. 1450c16b537SWarner Losh * Use ZBUFFv07_decompressInit() to start a new decompression operation, 1460c16b537SWarner Losh * or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary. 1470c16b537SWarner Losh * Note that ZBUFFv07_DCtx objects can be re-init multiple times. 1480c16b537SWarner Losh * 1490c16b537SWarner Losh * Use ZBUFFv07_decompressContinue() repetitively to consume your input. 1500c16b537SWarner Losh * *srcSizePtr and *dstCapacityPtr can be any size. 1510c16b537SWarner Losh * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr. 1520c16b537SWarner Losh * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again. 1530c16b537SWarner Losh * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`. 1540c16b537SWarner Losh * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency), 1550c16b537SWarner Losh * or 0 when a frame is completely decoded, 1560c16b537SWarner Losh * or an error code, which can be tested using ZBUFFv07_isError(). 1570c16b537SWarner Losh * 1580c16b537SWarner Losh * Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize() 1590c16b537SWarner Losh * output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded. 1600c16b537SWarner Losh * input : ZBUFFv07_recommendedDInSize == 128KB + 3; 1610c16b537SWarner Losh * just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . 1620c16b537SWarner Losh * *******************************************************************************/ 1630c16b537SWarner Losh 1640c16b537SWarner Losh 1650c16b537SWarner Losh /* ************************************* 1660c16b537SWarner Losh * Tool functions 1670c16b537SWarner Losh ***************************************/ 1680c16b537SWarner Losh ZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode); 1690c16b537SWarner Losh ZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode); 1700c16b537SWarner Losh 1710c16b537SWarner Losh /** Functions below provide recommended buffer sizes for Compression or Decompression operations. 1720c16b537SWarner Losh * These sizes are just hints, they tend to offer better latency */ 1730c16b537SWarner Losh ZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void); 1740c16b537SWarner Losh ZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void); 1750c16b537SWarner Losh 1760c16b537SWarner Losh 1770c16b537SWarner Losh /*-************************************* 1780c16b537SWarner Losh * Constants 1790c16b537SWarner Losh ***************************************/ 1800c16b537SWarner Losh #define ZSTDv07_MAGICNUMBER 0xFD2FB527 /* v0.7 */ 1810c16b537SWarner Losh 1820c16b537SWarner Losh 1830c16b537SWarner Losh #if defined (__cplusplus) 1840c16b537SWarner Losh } 1850c16b537SWarner Losh #endif 1860c16b537SWarner Losh 1870c16b537SWarner Losh #endif /* ZSTDv07_H_235446 */ 188