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