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).
12 /* *************************************
14 ***************************************/
15 #define ZBUFF_STATIC_LINKING_ONLY
19 /*-***********************************************************
20 * Streaming compression
22 * A ZBUFF_CCtx object is required to track streaming operation.
23 * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
24 * Use ZBUFF_compressInit() to start a new compression operation.
25 * ZBUFF_CCtx objects can be reused multiple times.
27 * Use ZBUFF_compressContinue() repetitively to consume your input.
28 * *srcSizePtr and *dstCapacityPtr can be any size.
29 * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
30 * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.
31 * The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
32 * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
33 * or an error code, which can be tested using ZBUFF_isError().
35 * ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
36 * Note that it will not output more than *dstCapacityPtr.
37 * Therefore, some content might still be left into its internal buffer if dst buffer is too small.
38 * @return : nb of bytes still present into internal buffer (0 if it's empty)
39 * or an error code, which can be tested using ZBUFF_isError().
41 * ZBUFF_compressEnd() instructs to finish a frame.
42 * It will perform a flush and write frame epilogue.
43 * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
44 * @return : nb of bytes still present into internal buffer (0 if it's empty)
45 * or an error code, which can be tested using ZBUFF_isError().
47 * Hint : recommended buffer sizes (not compulsory)
48 * input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
49 * output : ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + ZBUFF_endFrameSize : ensures it's always possible to write/flush/end a full block at best speed.
50 * ***********************************************************/
52 ZBUFF_CCtx* ZBUFF_createCCtx(void)
54 return ZSTD_createCStream();
57 ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
59 return ZSTD_createCStream_advanced(customMem);
62 size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
64 return ZSTD_freeCStream(zbc);
68 /* ====== Initialization ====== */
70 size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
71 const void* dict, size_t dictSize,
72 ZSTD_parameters params, unsigned long long pledgedSrcSize)
74 return ZSTD_initCStream_advanced(zbc, dict, dictSize, params, pledgedSrcSize);
78 size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
80 return ZSTD_initCStream_usingDict(zbc, dict, dictSize, compressionLevel);
83 size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
85 return ZSTD_initCStream(zbc, compressionLevel);
88 /* ====== Compression ====== */
91 size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
92 void* dst, size_t* dstCapacityPtr,
93 const void* src, size_t* srcSizePtr)
96 ZSTD_outBuffer outBuff;
100 outBuff.size = *dstCapacityPtr;
103 inBuff.size = *srcSizePtr;
104 result = ZSTD_compressStream(zbc, &outBuff, &inBuff);
105 *dstCapacityPtr = outBuff.pos;
106 *srcSizePtr = inBuff.pos;
112 /* ====== Finalize ====== */
114 size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
117 ZSTD_outBuffer outBuff;
120 outBuff.size = *dstCapacityPtr;
121 result = ZSTD_flushStream(zbc, &outBuff);
122 *dstCapacityPtr = outBuff.pos;
127 size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
130 ZSTD_outBuffer outBuff;
133 outBuff.size = *dstCapacityPtr;
134 result = ZSTD_endStream(zbc, &outBuff);
135 *dstCapacityPtr = outBuff.pos;
141 /* *************************************
143 ***************************************/
144 size_t ZBUFF_recommendedCInSize(void) { return ZSTD_CStreamInSize(); }
145 size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }