xref: /freebsd/sys/contrib/zstd/lib/deprecated/zbuff_compress.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
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).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 
12 
13 /* *************************************
14 *  Dependencies
15 ***************************************/
16 #define ZBUFF_STATIC_LINKING_ONLY
17 #include "zbuff.h"
18 
19 
20 /*-***********************************************************
21 *  Streaming compression
22 *
23 *  A ZBUFF_CCtx object is required to track streaming operation.
24 *  Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
25 *  Use ZBUFF_compressInit() to start a new compression operation.
26 *  ZBUFF_CCtx objects can be reused multiple times.
27 *
28 *  Use ZBUFF_compressContinue() repetitively to consume your input.
29 *  *srcSizePtr and *dstCapacityPtr can be any size.
30 *  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
31 *  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.
32 *  The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
33 *  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
34 *            or an error code, which can be tested using ZBUFF_isError().
35 *
36 *  ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
37 *  Note that it will not output more than *dstCapacityPtr.
38 *  Therefore, some content might still be left into its internal buffer if dst buffer is too small.
39 *  @return : nb of bytes still present into internal buffer (0 if it's empty)
40 *            or an error code, which can be tested using ZBUFF_isError().
41 *
42 *  ZBUFF_compressEnd() instructs to finish a frame.
43 *  It will perform a flush and write frame epilogue.
44 *  Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
45 *  @return : nb of bytes still present into internal buffer (0 if it's empty)
46 *            or an error code, which can be tested using ZBUFF_isError().
47 *
48 *  Hint : recommended buffer sizes (not compulsory)
49 *  input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
50 *  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.
51 * ***********************************************************/
52 
53 ZBUFF_CCtx* ZBUFF_createCCtx(void)
54 {
55     return ZSTD_createCStream();
56 }
57 
58 ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
59 {
60     return ZSTD_createCStream_advanced(customMem);
61 }
62 
63 size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
64 {
65     return ZSTD_freeCStream(zbc);
66 }
67 
68 
69 /* ======   Initialization   ====== */
70 
71 size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
72                                    const void* dict, size_t dictSize,
73                                    ZSTD_parameters params, unsigned long long pledgedSrcSize)
74 {
75     return ZSTD_initCStream_advanced(zbc, dict, dictSize, params, pledgedSrcSize);
76 }
77 
78 
79 size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
80 {
81     return ZSTD_initCStream_usingDict(zbc, dict, dictSize, compressionLevel);
82 }
83 
84 size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
85 {
86     return ZSTD_initCStream(zbc, compressionLevel);
87 }
88 
89 /* ======   Compression   ====== */
90 
91 
92 size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
93                               void* dst, size_t* dstCapacityPtr,
94                         const void* src, size_t* srcSizePtr)
95 {
96     size_t result;
97     ZSTD_outBuffer outBuff;
98     ZSTD_inBuffer inBuff;
99     outBuff.dst = dst;
100     outBuff.pos = 0;
101     outBuff.size = *dstCapacityPtr;
102     inBuff.src = src;
103     inBuff.pos = 0;
104     inBuff.size = *srcSizePtr;
105     result = ZSTD_compressStream(zbc, &outBuff, &inBuff);
106     *dstCapacityPtr = outBuff.pos;
107     *srcSizePtr = inBuff.pos;
108     return result;
109 }
110 
111 
112 
113 /* ======   Finalize   ====== */
114 
115 size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
116 {
117     size_t result;
118     ZSTD_outBuffer outBuff;
119     outBuff.dst = dst;
120     outBuff.pos = 0;
121     outBuff.size = *dstCapacityPtr;
122     result = ZSTD_flushStream(zbc, &outBuff);
123     *dstCapacityPtr = outBuff.pos;
124     return result;
125 }
126 
127 
128 size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
129 {
130     size_t result;
131     ZSTD_outBuffer outBuff;
132     outBuff.dst = dst;
133     outBuff.pos = 0;
134     outBuff.size = *dstCapacityPtr;
135     result = ZSTD_endStream(zbc, &outBuff);
136     *dstCapacityPtr = outBuff.pos;
137     return result;
138 }
139 
140 
141 
142 /* *************************************
143 *  Tool functions
144 ***************************************/
145 size_t ZBUFF_recommendedCInSize(void)  { return ZSTD_CStreamInSize(); }
146 size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }
147