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