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 ZSTDv06_H 120c16b537SWarner Losh #define ZSTDv06_H 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 * ZSTDv06_DLL_EXPORT : 250c16b537SWarner Losh * Enable exporting of functions when building a Windows DLL 260c16b537SWarner Losh */ 270c16b537SWarner Losh #if defined(_WIN32) && defined(ZSTDv06_DLL_EXPORT) && (ZSTDv06_DLL_EXPORT==1) 280c16b537SWarner Losh # define ZSTDLIBv06_API __declspec(dllexport) 290c16b537SWarner Losh #else 300c16b537SWarner Losh # define ZSTDLIBv06_API 310c16b537SWarner Losh #endif 320c16b537SWarner Losh 330c16b537SWarner Losh 340c16b537SWarner Losh /* ************************************* 350c16b537SWarner Losh * Simple functions 360c16b537SWarner Losh ***************************************/ 370c16b537SWarner Losh /*! ZSTDv06_decompress() : 380c16b537SWarner Losh `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail. 390c16b537SWarner Losh `dstCapacity` must be large enough, equal or larger than originalSize. 400c16b537SWarner Losh @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), 410c16b537SWarner Losh or an errorCode if it fails (which can be tested using ZSTDv06_isError()) */ 420c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_decompress( void* dst, size_t dstCapacity, 430c16b537SWarner Losh const void* src, size_t compressedSize); 440c16b537SWarner Losh 450c16b537SWarner Losh /** 462b9c00cbSConrad Meyer ZSTDv06_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.6.x format 472b9c00cbSConrad Meyer srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src' 482b9c00cbSConrad Meyer cSize (output parameter) : the number of bytes that would be read to decompress this frame 492b9c00cbSConrad Meyer or an error code if it fails (which can be tested using ZSTDv01_isError()) 502b9c00cbSConrad Meyer dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame 512b9c00cbSConrad Meyer or ZSTD_CONTENTSIZE_ERROR if an error occurs 522b9c00cbSConrad Meyer 532b9c00cbSConrad Meyer note : assumes `cSize` and `dBound` are _not_ NULL. 540c16b537SWarner Losh */ 552b9c00cbSConrad Meyer void ZSTDv06_findFrameSizeInfoLegacy(const void *src, size_t srcSize, 562b9c00cbSConrad Meyer size_t* cSize, unsigned long long* dBound); 570c16b537SWarner Losh 580c16b537SWarner Losh /* ************************************* 590c16b537SWarner Losh * Helper functions 600c16b537SWarner Losh ***************************************/ 610c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_compressBound(size_t srcSize); /*!< maximum compressed size (worst case scenario) */ 620c16b537SWarner Losh 630c16b537SWarner Losh /* Error Management */ 640c16b537SWarner Losh ZSTDLIBv06_API unsigned ZSTDv06_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ 650c16b537SWarner Losh ZSTDLIBv06_API const char* ZSTDv06_getErrorName(size_t code); /*!< provides readable string for an error code */ 660c16b537SWarner Losh 670c16b537SWarner Losh 680c16b537SWarner Losh /* ************************************* 690c16b537SWarner Losh * Explicit memory management 700c16b537SWarner Losh ***************************************/ 710c16b537SWarner Losh /** Decompression context */ 720c16b537SWarner Losh typedef struct ZSTDv06_DCtx_s ZSTDv06_DCtx; 730c16b537SWarner Losh ZSTDLIBv06_API ZSTDv06_DCtx* ZSTDv06_createDCtx(void); 740c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_freeDCtx(ZSTDv06_DCtx* dctx); /*!< @return : errorCode */ 750c16b537SWarner Losh 760c16b537SWarner Losh /** ZSTDv06_decompressDCtx() : 770c16b537SWarner Losh * Same as ZSTDv06_decompress(), but requires an already allocated ZSTDv06_DCtx (see ZSTDv06_createDCtx()) */ 780c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_decompressDCtx(ZSTDv06_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 790c16b537SWarner Losh 800c16b537SWarner Losh 810c16b537SWarner Losh /*-*********************** 820c16b537SWarner Losh * Dictionary API 830c16b537SWarner Losh *************************/ 840c16b537SWarner Losh /*! ZSTDv06_decompress_usingDict() : 850c16b537SWarner Losh * Decompression using a pre-defined Dictionary content (see dictBuilder). 860c16b537SWarner Losh * Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted. 870c16b537SWarner Losh * Note : dict can be NULL, in which case, it's equivalent to ZSTDv06_decompressDCtx() */ 880c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_decompress_usingDict(ZSTDv06_DCtx* dctx, 890c16b537SWarner Losh void* dst, size_t dstCapacity, 900c16b537SWarner Losh const void* src, size_t srcSize, 910c16b537SWarner Losh const void* dict,size_t dictSize); 920c16b537SWarner Losh 930c16b537SWarner Losh 940c16b537SWarner Losh /*-************************ 950c16b537SWarner Losh * Advanced Streaming API 960c16b537SWarner Losh ***************************/ 970c16b537SWarner Losh struct ZSTDv06_frameParams_s { unsigned long long frameContentSize; unsigned windowLog; }; 980c16b537SWarner Losh typedef struct ZSTDv06_frameParams_s ZSTDv06_frameParams; 990c16b537SWarner Losh 1000c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_getFrameParams(ZSTDv06_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */ 1010c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_decompressBegin_usingDict(ZSTDv06_DCtx* dctx, const void* dict, size_t dictSize); 1020c16b537SWarner Losh ZSTDLIBv06_API void ZSTDv06_copyDCtx(ZSTDv06_DCtx* dctx, const ZSTDv06_DCtx* preparedDCtx); 1030c16b537SWarner Losh 1040c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_nextSrcSizeToDecompress(ZSTDv06_DCtx* dctx); 1050c16b537SWarner Losh ZSTDLIBv06_API size_t ZSTDv06_decompressContinue(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); 1060c16b537SWarner Losh 1070c16b537SWarner Losh 1080c16b537SWarner Losh 1090c16b537SWarner Losh /* ************************************* 1100c16b537SWarner Losh * ZBUFF API 1110c16b537SWarner Losh ***************************************/ 1120c16b537SWarner Losh 1130c16b537SWarner Losh typedef struct ZBUFFv06_DCtx_s ZBUFFv06_DCtx; 1140c16b537SWarner Losh ZSTDLIBv06_API ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void); 1150c16b537SWarner Losh ZSTDLIBv06_API size_t ZBUFFv06_freeDCtx(ZBUFFv06_DCtx* dctx); 1160c16b537SWarner Losh 1170c16b537SWarner Losh ZSTDLIBv06_API size_t ZBUFFv06_decompressInit(ZBUFFv06_DCtx* dctx); 1180c16b537SWarner Losh ZSTDLIBv06_API size_t ZBUFFv06_decompressInitDictionary(ZBUFFv06_DCtx* dctx, const void* dict, size_t dictSize); 1190c16b537SWarner Losh 1200c16b537SWarner Losh ZSTDLIBv06_API size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* dctx, 1210c16b537SWarner Losh void* dst, size_t* dstCapacityPtr, 1220c16b537SWarner Losh const void* src, size_t* srcSizePtr); 1230c16b537SWarner Losh 1240c16b537SWarner Losh /*-*************************************************************************** 1250c16b537SWarner Losh * Streaming decompression howto 1260c16b537SWarner Losh * 1270c16b537SWarner Losh * A ZBUFFv06_DCtx object is required to track streaming operations. 1280c16b537SWarner Losh * Use ZBUFFv06_createDCtx() and ZBUFFv06_freeDCtx() to create/release resources. 1290c16b537SWarner Losh * Use ZBUFFv06_decompressInit() to start a new decompression operation, 1300c16b537SWarner Losh * or ZBUFFv06_decompressInitDictionary() if decompression requires a dictionary. 1310c16b537SWarner Losh * Note that ZBUFFv06_DCtx objects can be re-init multiple times. 1320c16b537SWarner Losh * 1330c16b537SWarner Losh * Use ZBUFFv06_decompressContinue() repetitively to consume your input. 1340c16b537SWarner Losh * *srcSizePtr and *dstCapacityPtr can be any size. 1350c16b537SWarner Losh * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr. 1360c16b537SWarner Losh * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again. 1370c16b537SWarner 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`. 1380c16b537SWarner 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), 1390c16b537SWarner Losh * or 0 when a frame is completely decoded, 1400c16b537SWarner Losh * or an error code, which can be tested using ZBUFFv06_isError(). 1410c16b537SWarner Losh * 1420c16b537SWarner Losh * Hint : recommended buffer sizes (not compulsory) : ZBUFFv06_recommendedDInSize() and ZBUFFv06_recommendedDOutSize() 1430c16b537SWarner Losh * output : ZBUFFv06_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded. 1440c16b537SWarner Losh * input : ZBUFFv06_recommendedDInSize == 128KB + 3; 1450c16b537SWarner Losh * just follow indications from ZBUFFv06_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . 1460c16b537SWarner Losh * *******************************************************************************/ 1470c16b537SWarner Losh 1480c16b537SWarner Losh 1490c16b537SWarner Losh /* ************************************* 1500c16b537SWarner Losh * Tool functions 1510c16b537SWarner Losh ***************************************/ 1520c16b537SWarner Losh ZSTDLIBv06_API unsigned ZBUFFv06_isError(size_t errorCode); 1530c16b537SWarner Losh ZSTDLIBv06_API const char* ZBUFFv06_getErrorName(size_t errorCode); 1540c16b537SWarner Losh 1550c16b537SWarner Losh /** Functions below provide recommended buffer sizes for Compression or Decompression operations. 1560c16b537SWarner Losh * These sizes are just hints, they tend to offer better latency */ 1570c16b537SWarner Losh ZSTDLIBv06_API size_t ZBUFFv06_recommendedDInSize(void); 1580c16b537SWarner Losh ZSTDLIBv06_API size_t ZBUFFv06_recommendedDOutSize(void); 1590c16b537SWarner Losh 1600c16b537SWarner Losh 1610c16b537SWarner Losh /*-************************************* 1620c16b537SWarner Losh * Constants 1630c16b537SWarner Losh ***************************************/ 1640c16b537SWarner Losh #define ZSTDv06_MAGICNUMBER 0xFD2FB526 /* v0.6 */ 1650c16b537SWarner Losh 1660c16b537SWarner Losh 1670c16b537SWarner Losh 1680c16b537SWarner Losh #if defined (__cplusplus) 1690c16b537SWarner Losh } 1700c16b537SWarner Losh #endif 1710c16b537SWarner Losh 1720c16b537SWarner Losh #endif /* ZSTDv06_BUFFERED_H */ 173