]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/zstd/lib/deprecated/zbuff_compress.c
Import zstandard 1.3.1
[FreeBSD/FreeBSD.git] / contrib / zstd / lib / deprecated / zbuff_compress.c
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  */
9
10
11
12 /* *************************************
13 *  Dependencies
14 ***************************************/
15 #define ZBUFF_STATIC_LINKING_ONLY
16 #include "zbuff.h"
17
18
19 /*-***********************************************************
20 *  Streaming compression
21 *
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.
26 *
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().
34 *
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().
40 *
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().
46 *
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 * ***********************************************************/
51
52 ZBUFF_CCtx* ZBUFF_createCCtx(void)
53 {
54     return ZSTD_createCStream();
55 }
56
57 ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
58 {
59     return ZSTD_createCStream_advanced(customMem);
60 }
61
62 size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
63 {
64     return ZSTD_freeCStream(zbc);
65 }
66
67
68 /* ======   Initialization   ====== */
69
70 size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
71                                    const void* dict, size_t dictSize,
72                                    ZSTD_parameters params, unsigned long long pledgedSrcSize)
73 {
74     return ZSTD_initCStream_advanced(zbc, dict, dictSize, params, pledgedSrcSize);
75 }
76
77
78 size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
79 {
80     return ZSTD_initCStream_usingDict(zbc, dict, dictSize, compressionLevel);
81 }
82
83 size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
84 {
85     return ZSTD_initCStream(zbc, compressionLevel);
86 }
87
88 /* ======   Compression   ====== */
89
90
91 size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
92                               void* dst, size_t* dstCapacityPtr,
93                         const void* src, size_t* srcSizePtr)
94 {
95     size_t result;
96     ZSTD_outBuffer outBuff;
97     ZSTD_inBuffer inBuff;
98     outBuff.dst = dst;
99     outBuff.pos = 0;
100     outBuff.size = *dstCapacityPtr;
101     inBuff.src = src;
102     inBuff.pos = 0;
103     inBuff.size = *srcSizePtr;
104     result = ZSTD_compressStream(zbc, &outBuff, &inBuff);
105     *dstCapacityPtr = outBuff.pos;
106     *srcSizePtr = inBuff.pos;
107     return result;
108 }
109
110
111
112 /* ======   Finalize   ====== */
113
114 size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
115 {
116     size_t result;
117     ZSTD_outBuffer outBuff;
118     outBuff.dst = dst;
119     outBuff.pos = 0;
120     outBuff.size = *dstCapacityPtr;
121     result = ZSTD_flushStream(zbc, &outBuff);
122     *dstCapacityPtr = outBuff.pos;
123     return result;
124 }
125
126
127 size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
128 {
129     size_t result;
130     ZSTD_outBuffer outBuff;
131     outBuff.dst = dst;
132     outBuff.pos = 0;
133     outBuff.size = *dstCapacityPtr;
134     result = ZSTD_endStream(zbc, &outBuff);
135     *dstCapacityPtr = outBuff.pos;
136     return result;
137 }
138
139
140
141 /* *************************************
142 *  Tool functions
143 ***************************************/
144 size_t ZBUFF_recommendedCInSize(void)  { return ZSTD_CStreamInSize(); }
145 size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }