xref: /aosp_15_r20/external/zstd/lib/deprecated/zbuff_compress.c (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui /*
2*01826a49SYabin Cui  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui  * All rights reserved.
4*01826a49SYabin Cui  *
5*01826a49SYabin Cui  * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui  * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui  * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui  */
10*01826a49SYabin Cui 
11*01826a49SYabin Cui 
12*01826a49SYabin Cui 
13*01826a49SYabin Cui /* *************************************
14*01826a49SYabin Cui *  Dependencies
15*01826a49SYabin Cui ***************************************/
16*01826a49SYabin Cui #define ZBUFF_STATIC_LINKING_ONLY
17*01826a49SYabin Cui #include "zbuff.h"
18*01826a49SYabin Cui #include "../common/error_private.h"
19*01826a49SYabin Cui 
20*01826a49SYabin Cui 
21*01826a49SYabin Cui /*-***********************************************************
22*01826a49SYabin Cui *  Streaming compression
23*01826a49SYabin Cui *
24*01826a49SYabin Cui *  A ZBUFF_CCtx object is required to track streaming operation.
25*01826a49SYabin Cui *  Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
26*01826a49SYabin Cui *  Use ZBUFF_compressInit() to start a new compression operation.
27*01826a49SYabin Cui *  ZBUFF_CCtx objects can be reused multiple times.
28*01826a49SYabin Cui *
29*01826a49SYabin Cui *  Use ZBUFF_compressContinue() repetitively to consume your input.
30*01826a49SYabin Cui *  *srcSizePtr and *dstCapacityPtr can be any size.
31*01826a49SYabin Cui *  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
32*01826a49SYabin Cui *  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.
33*01826a49SYabin Cui *  The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
34*01826a49SYabin Cui *  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
35*01826a49SYabin Cui *            or an error code, which can be tested using ZBUFF_isError().
36*01826a49SYabin Cui *
37*01826a49SYabin Cui *  ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
38*01826a49SYabin Cui *  Note that it will not output more than *dstCapacityPtr.
39*01826a49SYabin Cui *  Therefore, some content might still be left into its internal buffer if dst buffer is too small.
40*01826a49SYabin Cui *  @return : nb of bytes still present into internal buffer (0 if it's empty)
41*01826a49SYabin Cui *            or an error code, which can be tested using ZBUFF_isError().
42*01826a49SYabin Cui *
43*01826a49SYabin Cui *  ZBUFF_compressEnd() instructs to finish a frame.
44*01826a49SYabin Cui *  It will perform a flush and write frame epilogue.
45*01826a49SYabin Cui *  Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
46*01826a49SYabin Cui *  @return : nb of bytes still present into internal buffer (0 if it's empty)
47*01826a49SYabin Cui *            or an error code, which can be tested using ZBUFF_isError().
48*01826a49SYabin Cui *
49*01826a49SYabin Cui *  Hint : recommended buffer sizes (not compulsory)
50*01826a49SYabin Cui *  input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
51*01826a49SYabin Cui *  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.
52*01826a49SYabin Cui * ***********************************************************/
53*01826a49SYabin Cui 
ZBUFF_createCCtx(void)54*01826a49SYabin Cui ZBUFF_CCtx* ZBUFF_createCCtx(void)
55*01826a49SYabin Cui {
56*01826a49SYabin Cui     return ZSTD_createCStream();
57*01826a49SYabin Cui }
58*01826a49SYabin Cui 
ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)59*01826a49SYabin Cui ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
60*01826a49SYabin Cui {
61*01826a49SYabin Cui     return ZSTD_createCStream_advanced(customMem);
62*01826a49SYabin Cui }
63*01826a49SYabin Cui 
ZBUFF_freeCCtx(ZBUFF_CCtx * zbc)64*01826a49SYabin Cui size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
65*01826a49SYabin Cui {
66*01826a49SYabin Cui     return ZSTD_freeCStream(zbc);
67*01826a49SYabin Cui }
68*01826a49SYabin Cui 
69*01826a49SYabin Cui 
70*01826a49SYabin Cui /* ======   Initialization   ====== */
71*01826a49SYabin Cui 
ZBUFF_compressInit_advanced(ZBUFF_CCtx * zbc,const void * dict,size_t dictSize,ZSTD_parameters params,unsigned long long pledgedSrcSize)72*01826a49SYabin Cui size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
73*01826a49SYabin Cui                                    const void* dict, size_t dictSize,
74*01826a49SYabin Cui                                    ZSTD_parameters params, unsigned long long pledgedSrcSize)
75*01826a49SYabin Cui {
76*01826a49SYabin Cui     if (pledgedSrcSize==0) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN;  /* preserve "0 == unknown" behavior */
77*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");
78*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setPledgedSrcSize(zbc, pledgedSrcSize), "");
79*01826a49SYabin Cui 
80*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_checkCParams(params.cParams), "");
81*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_windowLog, params.cParams.windowLog), "");
82*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_hashLog, params.cParams.hashLog), "");
83*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_chainLog, params.cParams.chainLog), "");
84*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_searchLog, params.cParams.searchLog), "");
85*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_minMatch, params.cParams.minMatch), "");
86*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_targetLength, params.cParams.targetLength), "");
87*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_strategy, params.cParams.strategy), "");
88*01826a49SYabin Cui 
89*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_contentSizeFlag, params.fParams.contentSizeFlag), "");
90*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_checksumFlag, params.fParams.checksumFlag), "");
91*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_dictIDFlag, params.fParams.noDictIDFlag), "");
92*01826a49SYabin Cui 
93*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");
94*01826a49SYabin Cui     return 0;
95*01826a49SYabin Cui }
96*01826a49SYabin Cui 
ZBUFF_compressInitDictionary(ZBUFF_CCtx * zbc,const void * dict,size_t dictSize,int compressionLevel)97*01826a49SYabin Cui size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
98*01826a49SYabin Cui {
99*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");
100*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_compressionLevel, compressionLevel), "");
101*01826a49SYabin Cui     FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");
102*01826a49SYabin Cui     return 0;
103*01826a49SYabin Cui }
104*01826a49SYabin Cui 
ZBUFF_compressInit(ZBUFF_CCtx * zbc,int compressionLevel)105*01826a49SYabin Cui size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
106*01826a49SYabin Cui {
107*01826a49SYabin Cui     return ZSTD_initCStream(zbc, compressionLevel);
108*01826a49SYabin Cui }
109*01826a49SYabin Cui 
110*01826a49SYabin Cui /* ======   Compression   ====== */
111*01826a49SYabin Cui 
112*01826a49SYabin Cui 
ZBUFF_compressContinue(ZBUFF_CCtx * zbc,void * dst,size_t * dstCapacityPtr,const void * src,size_t * srcSizePtr)113*01826a49SYabin Cui size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
114*01826a49SYabin Cui                               void* dst, size_t* dstCapacityPtr,
115*01826a49SYabin Cui                         const void* src, size_t* srcSizePtr)
116*01826a49SYabin Cui {
117*01826a49SYabin Cui     size_t result;
118*01826a49SYabin Cui     ZSTD_outBuffer outBuff;
119*01826a49SYabin Cui     ZSTD_inBuffer inBuff;
120*01826a49SYabin Cui     outBuff.dst = dst;
121*01826a49SYabin Cui     outBuff.pos = 0;
122*01826a49SYabin Cui     outBuff.size = *dstCapacityPtr;
123*01826a49SYabin Cui     inBuff.src = src;
124*01826a49SYabin Cui     inBuff.pos = 0;
125*01826a49SYabin Cui     inBuff.size = *srcSizePtr;
126*01826a49SYabin Cui     result = ZSTD_compressStream(zbc, &outBuff, &inBuff);
127*01826a49SYabin Cui     *dstCapacityPtr = outBuff.pos;
128*01826a49SYabin Cui     *srcSizePtr = inBuff.pos;
129*01826a49SYabin Cui     return result;
130*01826a49SYabin Cui }
131*01826a49SYabin Cui 
132*01826a49SYabin Cui 
133*01826a49SYabin Cui 
134*01826a49SYabin Cui /* ======   Finalize   ====== */
135*01826a49SYabin Cui 
ZBUFF_compressFlush(ZBUFF_CCtx * zbc,void * dst,size_t * dstCapacityPtr)136*01826a49SYabin Cui size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
137*01826a49SYabin Cui {
138*01826a49SYabin Cui     size_t result;
139*01826a49SYabin Cui     ZSTD_outBuffer outBuff;
140*01826a49SYabin Cui     outBuff.dst = dst;
141*01826a49SYabin Cui     outBuff.pos = 0;
142*01826a49SYabin Cui     outBuff.size = *dstCapacityPtr;
143*01826a49SYabin Cui     result = ZSTD_flushStream(zbc, &outBuff);
144*01826a49SYabin Cui     *dstCapacityPtr = outBuff.pos;
145*01826a49SYabin Cui     return result;
146*01826a49SYabin Cui }
147*01826a49SYabin Cui 
148*01826a49SYabin Cui 
ZBUFF_compressEnd(ZBUFF_CCtx * zbc,void * dst,size_t * dstCapacityPtr)149*01826a49SYabin Cui size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
150*01826a49SYabin Cui {
151*01826a49SYabin Cui     size_t result;
152*01826a49SYabin Cui     ZSTD_outBuffer outBuff;
153*01826a49SYabin Cui     outBuff.dst = dst;
154*01826a49SYabin Cui     outBuff.pos = 0;
155*01826a49SYabin Cui     outBuff.size = *dstCapacityPtr;
156*01826a49SYabin Cui     result = ZSTD_endStream(zbc, &outBuff);
157*01826a49SYabin Cui     *dstCapacityPtr = outBuff.pos;
158*01826a49SYabin Cui     return result;
159*01826a49SYabin Cui }
160*01826a49SYabin Cui 
161*01826a49SYabin Cui 
162*01826a49SYabin Cui 
163*01826a49SYabin Cui /* *************************************
164*01826a49SYabin Cui *  Tool functions
165*01826a49SYabin Cui ***************************************/
ZBUFF_recommendedCInSize(void)166*01826a49SYabin Cui size_t ZBUFF_recommendedCInSize(void)  { return ZSTD_CStreamInSize(); }
ZBUFF_recommendedCOutSize(void)167*01826a49SYabin Cui size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }
168