1 | /* |
---|
2 | Buffered version of Zstd compression library |
---|
3 | Copyright (C) 2015-2016, Yann Collet. |
---|
4 | |
---|
5 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
---|
6 | |
---|
7 | Redistribution and use in source and binary forms, with or without |
---|
8 | modification, are permitted provided that the following conditions are |
---|
9 | met: |
---|
10 | * Redistributions of source code must retain the above copyright |
---|
11 | notice, this list of conditions and the following disclaimer. |
---|
12 | * Redistributions in binary form must reproduce the above |
---|
13 | copyright notice, this list of conditions and the following disclaimer |
---|
14 | in the documentation and/or other materials provided with the |
---|
15 | distribution. |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
20 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
27 | |
---|
28 | You can contact the author at : |
---|
29 | - zstd homepage : http://www.zstd.net/ |
---|
30 | */ |
---|
31 | #ifndef ZSTD_BUFFERED_H_23987 |
---|
32 | #define ZSTD_BUFFERED_H_23987 |
---|
33 | |
---|
34 | #if defined (__cplusplus) |
---|
35 | extern "C" { |
---|
36 | #endif |
---|
37 | |
---|
38 | /* ************************************* |
---|
39 | * Dependencies |
---|
40 | ***************************************/ |
---|
41 | #include <stddef.h> /* size_t */ |
---|
42 | |
---|
43 | |
---|
44 | /* *************************************************************** |
---|
45 | * Compiler specifics |
---|
46 | *****************************************************************/ |
---|
47 | /* ZSTD_DLL_EXPORT : |
---|
48 | * Enable exporting of functions when building a Windows DLL */ |
---|
49 | #if defined(_WIN32) && defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) |
---|
50 | # define ZSTDLIB_API __declspec(dllexport) |
---|
51 | #else |
---|
52 | # define ZSTDLIB_API |
---|
53 | #endif |
---|
54 | |
---|
55 | |
---|
56 | /* ************************************* |
---|
57 | * Streaming functions |
---|
58 | ***************************************/ |
---|
59 | typedef struct ZBUFF_CCtx_s ZBUFF_CCtx; |
---|
60 | ZSTDLIB_API ZBUFF_CCtx* ZBUFF_createCCtx(void); |
---|
61 | ZSTDLIB_API size_t ZBUFF_freeCCtx(ZBUFF_CCtx* cctx); |
---|
62 | |
---|
63 | ZSTDLIB_API size_t ZBUFF_compressInit(ZBUFF_CCtx* cctx, int compressionLevel); |
---|
64 | ZSTDLIB_API size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); |
---|
65 | |
---|
66 | ZSTDLIB_API size_t ZBUFF_compressContinue(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr, const void* src, size_t* srcSizePtr); |
---|
67 | ZSTDLIB_API size_t ZBUFF_compressFlush(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr); |
---|
68 | ZSTDLIB_API size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr); |
---|
69 | |
---|
70 | /*-************************************************* |
---|
71 | * Streaming compression - howto |
---|
72 | * |
---|
73 | * A ZBUFF_CCtx object is required to track streaming operation. |
---|
74 | * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources. |
---|
75 | * ZBUFF_CCtx objects can be reused multiple times. |
---|
76 | * |
---|
77 | * Start by initializing ZBUF_CCtx. |
---|
78 | * Use ZBUFF_compressInit() to start a new compression operation. |
---|
79 | * Use ZBUFF_compressInitDictionary() for a compression which requires a dictionary. |
---|
80 | * |
---|
81 | * Use ZBUFF_compressContinue() repetitively to consume input stream. |
---|
82 | * *srcSizePtr and *dstCapacityPtr can be any size. |
---|
83 | * The function will report how many bytes were read or written within *srcSizePtr and *dstCapacityPtr. |
---|
84 | * Note that it may not consume the entire input, in which case it's up to the caller to present again remaining data. |
---|
85 | * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each call, so save its content if it matters or change @dst . |
---|
86 | * @return : a hint to preferred nb of bytes to use as input for next function call (it's just a hint, to improve latency) |
---|
87 | * or an error code, which can be tested using ZBUFF_isError(). |
---|
88 | * |
---|
89 | * At any moment, it's possible to flush whatever data remains within buffer, using ZBUFF_compressFlush(). |
---|
90 | * The nb of bytes written into `dst` will be reported into *dstCapacityPtr. |
---|
91 | * Note that the function cannot output more than *dstCapacityPtr, |
---|
92 | * therefore, some content might still be left into internal buffer if *dstCapacityPtr is too small. |
---|
93 | * @return : nb of bytes still present into internal buffer (0 if it's empty) |
---|
94 | * or an error code, which can be tested using ZBUFF_isError(). |
---|
95 | * |
---|
96 | * ZBUFF_compressEnd() instructs to finish a frame. |
---|
97 | * It will perform a flush and write frame epilogue. |
---|
98 | * The epilogue is required for decoders to consider a frame completed. |
---|
99 | * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small. |
---|
100 | * In which case, call again ZBUFF_compressFlush() to complete the flush. |
---|
101 | * @return : nb of bytes still present into internal buffer (0 if it's empty) |
---|
102 | * or an error code, which can be tested using ZBUFF_isError(). |
---|
103 | * |
---|
104 | * Hint : _recommended buffer_ sizes (not compulsory) : ZBUFF_recommendedCInSize() / ZBUFF_recommendedCOutSize() |
---|
105 | * input : ZBUFF_recommendedCInSize==128 KB block size is the internal unit, use this value to reduce intermediate stages (better latency) |
---|
106 | * output : ZBUFF_recommendedCOutSize==ZSTD_compressBound(128 KB) + 3 + 3 : ensures it's always possible to write/flush/end a full block. Skip some buffering. |
---|
107 | * By using both, it ensures that input will be entirely consumed, and output will always contain the result, reducing intermediate buffering. |
---|
108 | * **************************************************/ |
---|
109 | |
---|
110 | |
---|
111 | typedef struct ZBUFF_DCtx_s ZBUFF_DCtx; |
---|
112 | ZSTDLIB_API ZBUFF_DCtx* ZBUFF_createDCtx(void); |
---|
113 | ZSTDLIB_API size_t ZBUFF_freeDCtx(ZBUFF_DCtx* dctx); |
---|
114 | |
---|
115 | ZSTDLIB_API size_t ZBUFF_decompressInit(ZBUFF_DCtx* dctx); |
---|
116 | ZSTDLIB_API size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* dctx, const void* dict, size_t dictSize); |
---|
117 | |
---|
118 | ZSTDLIB_API size_t ZBUFF_decompressContinue(ZBUFF_DCtx* dctx, |
---|
119 | void* dst, size_t* dstCapacityPtr, |
---|
120 | const void* src, size_t* srcSizePtr); |
---|
121 | |
---|
122 | /*-*************************************************************************** |
---|
123 | * Streaming decompression howto |
---|
124 | * |
---|
125 | * A ZBUFF_DCtx object is required to track streaming operations. |
---|
126 | * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources. |
---|
127 | * Use ZBUFF_decompressInit() to start a new decompression operation, |
---|
128 | * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary. |
---|
129 | * Note that ZBUFF_DCtx objects can be re-init multiple times. |
---|
130 | * |
---|
131 | * Use ZBUFF_decompressContinue() repetitively to consume your input. |
---|
132 | * *srcSizePtr and *dstCapacityPtr can be any size. |
---|
133 | * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr. |
---|
134 | * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again. |
---|
135 | * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`. |
---|
136 | * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency), |
---|
137 | * or 0 when a frame is completely decoded, |
---|
138 | * or an error code, which can be tested using ZBUFF_isError(). |
---|
139 | * |
---|
140 | * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize() |
---|
141 | * output : ZBUFF_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded. |
---|
142 | * input : ZBUFF_recommendedDInSize == 128KB + 3; |
---|
143 | * just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . |
---|
144 | * *******************************************************************************/ |
---|
145 | |
---|
146 | |
---|
147 | /* ************************************* |
---|
148 | * Tool functions |
---|
149 | ***************************************/ |
---|
150 | ZSTDLIB_API unsigned ZBUFF_isError(size_t errorCode); |
---|
151 | ZSTDLIB_API const char* ZBUFF_getErrorName(size_t errorCode); |
---|
152 | |
---|
153 | /** Functions below provide recommended buffer sizes for Compression or Decompression operations. |
---|
154 | * These sizes are just hints, they tend to offer better latency */ |
---|
155 | ZSTDLIB_API size_t ZBUFF_recommendedCInSize(void); |
---|
156 | ZSTDLIB_API size_t ZBUFF_recommendedCOutSize(void); |
---|
157 | ZSTDLIB_API size_t ZBUFF_recommendedDInSize(void); |
---|
158 | ZSTDLIB_API size_t ZBUFF_recommendedDOutSize(void); |
---|
159 | |
---|
160 | |
---|
161 | #ifdef ZBUFF_STATIC_LINKING_ONLY |
---|
162 | |
---|
163 | /* ==================================================================================== |
---|
164 | * The definitions in this section are considered experimental. |
---|
165 | * They should never be used in association with a dynamic library, as they may change in the future. |
---|
166 | * They are provided for advanced usages. |
---|
167 | * Use them only in association with static linking. |
---|
168 | * ==================================================================================== */ |
---|
169 | |
---|
170 | /*--- Dependency ---*/ |
---|
171 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters */ |
---|
172 | #include "zstd.h" |
---|
173 | |
---|
174 | |
---|
175 | /*--- External memory ---*/ |
---|
176 | /*! ZBUFF_createCCtx_advanced() : |
---|
177 | * Create a ZBUFF compression context using external alloc and free functions */ |
---|
178 | ZSTDLIB_API ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem); |
---|
179 | |
---|
180 | /*! ZBUFF_createDCtx_advanced() : |
---|
181 | * Create a ZBUFF decompression context using external alloc and free functions */ |
---|
182 | ZSTDLIB_API ZBUFF_DCtx* ZBUFF_createDCtx_advanced(ZSTD_customMem customMem); |
---|
183 | |
---|
184 | |
---|
185 | /*--- Advanced Streaming function ---*/ |
---|
186 | ZSTDLIB_API size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc, |
---|
187 | const void* dict, size_t dictSize, |
---|
188 | ZSTD_parameters params, unsigned long long pledgedSrcSize); |
---|
189 | |
---|
190 | #endif /* ZBUFF_STATIC_LINKING_ONLY */ |
---|
191 | |
---|
192 | |
---|
193 | #if defined (__cplusplus) |
---|
194 | } |
---|
195 | #endif |
---|
196 | |
---|
197 | #endif /* ZSTD_BUFFERED_H_23987 */ |
---|