1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3*4882a593Smuzhiyun * All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This source code is licensed under the BSD-style license found in the
6*4882a593Smuzhiyun * LICENSE file in the root directory of https://github.com/facebook/zstd.
7*4882a593Smuzhiyun * An additional grant of patent rights can be found in the PATENTS file in the
8*4882a593Smuzhiyun * same directory.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify it under
11*4882a593Smuzhiyun * the terms of the GNU General Public License version 2 as published by the
12*4882a593Smuzhiyun * Free Software Foundation. This program is dual-licensed; you may select
13*4882a593Smuzhiyun * either version 2 of the GNU General Public License ("GPL") or BSD license
14*4882a593Smuzhiyun * ("BSD").
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #ifndef ZSTD_H
18*4882a593Smuzhiyun #define ZSTD_H
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /* ====== Dependency ======*/
21*4882a593Smuzhiyun #include <linux/types.h> /* size_t */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /*-*****************************************************************************
25*4882a593Smuzhiyun * Introduction
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * zstd, short for Zstandard, is a fast lossless compression algorithm,
28*4882a593Smuzhiyun * targeting real-time compression scenarios at zlib-level and better
29*4882a593Smuzhiyun * compression ratios. The zstd compression library provides in-memory
30*4882a593Smuzhiyun * compression and decompression functions. The library supports compression
31*4882a593Smuzhiyun * levels from 1 up to ZSTD_maxCLevel() which is 22. Levels >= 20, labeled
32*4882a593Smuzhiyun * ultra, should be used with caution, as they require more memory.
33*4882a593Smuzhiyun * Compression can be done in:
34*4882a593Smuzhiyun * - a single step, reusing a context (described as Explicit memory management)
35*4882a593Smuzhiyun * - unbounded multiple steps (described as Streaming compression)
36*4882a593Smuzhiyun * The compression ratio achievable on small data can be highly improved using
37*4882a593Smuzhiyun * compression with a dictionary in:
38*4882a593Smuzhiyun * - a single step (described as Simple dictionary API)
39*4882a593Smuzhiyun * - a single step, reusing a dictionary (described as Fast dictionary API)
40*4882a593Smuzhiyun ******************************************************************************/
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /*====== Helper functions ======*/
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun * enum ZSTD_ErrorCode - zstd error codes
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * Functions that return size_t can be checked for errors using ZSTD_isError()
48*4882a593Smuzhiyun * and the ZSTD_ErrorCode can be extracted using ZSTD_getErrorCode().
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun typedef enum {
51*4882a593Smuzhiyun ZSTD_error_no_error,
52*4882a593Smuzhiyun ZSTD_error_GENERIC,
53*4882a593Smuzhiyun ZSTD_error_prefix_unknown,
54*4882a593Smuzhiyun ZSTD_error_version_unsupported,
55*4882a593Smuzhiyun ZSTD_error_parameter_unknown,
56*4882a593Smuzhiyun ZSTD_error_frameParameter_unsupported,
57*4882a593Smuzhiyun ZSTD_error_frameParameter_unsupportedBy32bits,
58*4882a593Smuzhiyun ZSTD_error_frameParameter_windowTooLarge,
59*4882a593Smuzhiyun ZSTD_error_compressionParameter_unsupported,
60*4882a593Smuzhiyun ZSTD_error_init_missing,
61*4882a593Smuzhiyun ZSTD_error_memory_allocation,
62*4882a593Smuzhiyun ZSTD_error_stage_wrong,
63*4882a593Smuzhiyun ZSTD_error_dstSize_tooSmall,
64*4882a593Smuzhiyun ZSTD_error_srcSize_wrong,
65*4882a593Smuzhiyun ZSTD_error_corruption_detected,
66*4882a593Smuzhiyun ZSTD_error_checksum_wrong,
67*4882a593Smuzhiyun ZSTD_error_tableLog_tooLarge,
68*4882a593Smuzhiyun ZSTD_error_maxSymbolValue_tooLarge,
69*4882a593Smuzhiyun ZSTD_error_maxSymbolValue_tooSmall,
70*4882a593Smuzhiyun ZSTD_error_dictionary_corrupted,
71*4882a593Smuzhiyun ZSTD_error_dictionary_wrong,
72*4882a593Smuzhiyun ZSTD_error_dictionaryCreation_failed,
73*4882a593Smuzhiyun ZSTD_error_maxCode
74*4882a593Smuzhiyun } ZSTD_ErrorCode;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /**
77*4882a593Smuzhiyun * ZSTD_maxCLevel() - maximum compression level available
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * Return: Maximum compression level available.
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun int ZSTD_maxCLevel(void);
82*4882a593Smuzhiyun /**
83*4882a593Smuzhiyun * ZSTD_compressBound() - maximum compressed size in worst case scenario
84*4882a593Smuzhiyun * @srcSize: The size of the data to compress.
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * Return: The maximum compressed size in the worst case scenario.
87*4882a593Smuzhiyun */
88*4882a593Smuzhiyun size_t ZSTD_compressBound(size_t srcSize);
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun * ZSTD_isError() - tells if a size_t function result is an error code
91*4882a593Smuzhiyun * @code: The function result to check for error.
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * Return: Non-zero iff the code is an error.
94*4882a593Smuzhiyun */
ZSTD_isError(size_t code)95*4882a593Smuzhiyun static __attribute__((unused)) unsigned int ZSTD_isError(size_t code)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun return code > (size_t)-ZSTD_error_maxCode;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun * ZSTD_getErrorCode() - translates an error function result to a ZSTD_ErrorCode
101*4882a593Smuzhiyun * @functionResult: The result of a function for which ZSTD_isError() is true.
102*4882a593Smuzhiyun *
103*4882a593Smuzhiyun * Return: The ZSTD_ErrorCode corresponding to the functionResult or 0
104*4882a593Smuzhiyun * if the functionResult isn't an error.
105*4882a593Smuzhiyun */
ZSTD_getErrorCode(size_t functionResult)106*4882a593Smuzhiyun static __attribute__((unused)) ZSTD_ErrorCode ZSTD_getErrorCode(
107*4882a593Smuzhiyun size_t functionResult)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun if (!ZSTD_isError(functionResult))
110*4882a593Smuzhiyun return (ZSTD_ErrorCode)0;
111*4882a593Smuzhiyun return (ZSTD_ErrorCode)(0 - functionResult);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun * enum ZSTD_strategy - zstd compression search strategy
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * From faster to stronger.
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun typedef enum {
120*4882a593Smuzhiyun ZSTD_fast,
121*4882a593Smuzhiyun ZSTD_dfast,
122*4882a593Smuzhiyun ZSTD_greedy,
123*4882a593Smuzhiyun ZSTD_lazy,
124*4882a593Smuzhiyun ZSTD_lazy2,
125*4882a593Smuzhiyun ZSTD_btlazy2,
126*4882a593Smuzhiyun ZSTD_btopt,
127*4882a593Smuzhiyun ZSTD_btopt2
128*4882a593Smuzhiyun } ZSTD_strategy;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun * struct ZSTD_compressionParameters - zstd compression parameters
132*4882a593Smuzhiyun * @windowLog: Log of the largest match distance. Larger means more
133*4882a593Smuzhiyun * compression, and more memory needed during decompression.
134*4882a593Smuzhiyun * @chainLog: Fully searched segment. Larger means more compression, slower,
135*4882a593Smuzhiyun * and more memory (useless for fast).
136*4882a593Smuzhiyun * @hashLog: Dispatch table. Larger means more compression,
137*4882a593Smuzhiyun * slower, and more memory.
138*4882a593Smuzhiyun * @searchLog: Number of searches. Larger means more compression and slower.
139*4882a593Smuzhiyun * @searchLength: Match length searched. Larger means faster decompression,
140*4882a593Smuzhiyun * sometimes less compression.
141*4882a593Smuzhiyun * @targetLength: Acceptable match size for optimal parser (only). Larger means
142*4882a593Smuzhiyun * more compression, and slower.
143*4882a593Smuzhiyun * @strategy: The zstd compression strategy.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun typedef struct {
146*4882a593Smuzhiyun unsigned int windowLog;
147*4882a593Smuzhiyun unsigned int chainLog;
148*4882a593Smuzhiyun unsigned int hashLog;
149*4882a593Smuzhiyun unsigned int searchLog;
150*4882a593Smuzhiyun unsigned int searchLength;
151*4882a593Smuzhiyun unsigned int targetLength;
152*4882a593Smuzhiyun ZSTD_strategy strategy;
153*4882a593Smuzhiyun } ZSTD_compressionParameters;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun * struct ZSTD_frameParameters - zstd frame parameters
157*4882a593Smuzhiyun * @contentSizeFlag: Controls whether content size will be present in the frame
158*4882a593Smuzhiyun * header (when known).
159*4882a593Smuzhiyun * @checksumFlag: Controls whether a 32-bit checksum is generated at the end
160*4882a593Smuzhiyun * of the frame for error detection.
161*4882a593Smuzhiyun * @noDictIDFlag: Controls whether dictID will be saved into the frame header
162*4882a593Smuzhiyun * when using dictionary compression.
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * The default value is all fields set to 0.
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun typedef struct {
167*4882a593Smuzhiyun unsigned int contentSizeFlag;
168*4882a593Smuzhiyun unsigned int checksumFlag;
169*4882a593Smuzhiyun unsigned int noDictIDFlag;
170*4882a593Smuzhiyun } ZSTD_frameParameters;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /**
173*4882a593Smuzhiyun * struct ZSTD_parameters - zstd parameters
174*4882a593Smuzhiyun * @cParams: The compression parameters.
175*4882a593Smuzhiyun * @fParams: The frame parameters.
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun typedef struct {
178*4882a593Smuzhiyun ZSTD_compressionParameters cParams;
179*4882a593Smuzhiyun ZSTD_frameParameters fParams;
180*4882a593Smuzhiyun } ZSTD_parameters;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun * ZSTD_getCParams() - returns ZSTD_compressionParameters for selected level
184*4882a593Smuzhiyun * @compressionLevel: The compression level from 1 to ZSTD_maxCLevel().
185*4882a593Smuzhiyun * @estimatedSrcSize: The estimated source size to compress or 0 if unknown.
186*4882a593Smuzhiyun * @dictSize: The dictionary size or 0 if a dictionary isn't being used.
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * Return: The selected ZSTD_compressionParameters.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel,
191*4882a593Smuzhiyun unsigned long long estimatedSrcSize, size_t dictSize);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /**
194*4882a593Smuzhiyun * ZSTD_getParams() - returns ZSTD_parameters for selected level
195*4882a593Smuzhiyun * @compressionLevel: The compression level from 1 to ZSTD_maxCLevel().
196*4882a593Smuzhiyun * @estimatedSrcSize: The estimated source size to compress or 0 if unknown.
197*4882a593Smuzhiyun * @dictSize: The dictionary size or 0 if a dictionary isn't being used.
198*4882a593Smuzhiyun *
199*4882a593Smuzhiyun * The same as ZSTD_getCParams() except also selects the default frame
200*4882a593Smuzhiyun * parameters (all zero).
201*4882a593Smuzhiyun *
202*4882a593Smuzhiyun * Return: The selected ZSTD_parameters.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun ZSTD_parameters ZSTD_getParams(int compressionLevel,
205*4882a593Smuzhiyun unsigned long long estimatedSrcSize, size_t dictSize);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /*-*************************************
208*4882a593Smuzhiyun * Explicit memory management
209*4882a593Smuzhiyun **************************************/
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun * ZSTD_CCtxWorkspaceBound() - amount of memory needed to initialize a ZSTD_CCtx
213*4882a593Smuzhiyun * @cParams: The compression parameters to be used for compression.
214*4882a593Smuzhiyun *
215*4882a593Smuzhiyun * If multiple compression parameters might be used, the caller must call
216*4882a593Smuzhiyun * ZSTD_CCtxWorkspaceBound() for each set of parameters and use the maximum
217*4882a593Smuzhiyun * size.
218*4882a593Smuzhiyun *
219*4882a593Smuzhiyun * Return: A lower bound on the size of the workspace that is passed to
220*4882a593Smuzhiyun * ZSTD_initCCtx().
221*4882a593Smuzhiyun */
222*4882a593Smuzhiyun size_t ZSTD_CCtxWorkspaceBound(ZSTD_compressionParameters cParams);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /**
225*4882a593Smuzhiyun * struct ZSTD_CCtx - the zstd compression context
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * When compressing many times it is recommended to allocate a context just once
228*4882a593Smuzhiyun * and reuse it for each successive compression operation.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun typedef struct ZSTD_CCtx_s ZSTD_CCtx;
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * ZSTD_initCCtx() - initialize a zstd compression context
233*4882a593Smuzhiyun * @workspace: The workspace to emplace the context into. It must outlive
234*4882a593Smuzhiyun * the returned context.
235*4882a593Smuzhiyun * @workspaceSize: The size of workspace. Use ZSTD_CCtxWorkspaceBound() to
236*4882a593Smuzhiyun * determine how large the workspace must be.
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * Return: A compression context emplaced into workspace.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun ZSTD_CCtx *ZSTD_initCCtx(void *workspace, size_t workspaceSize);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun * ZSTD_compressCCtx() - compress src into dst
244*4882a593Smuzhiyun * @ctx: The context. Must have been initialized with a workspace at
245*4882a593Smuzhiyun * least as large as ZSTD_CCtxWorkspaceBound(params.cParams).
246*4882a593Smuzhiyun * @dst: The buffer to compress src into.
247*4882a593Smuzhiyun * @dstCapacity: The size of the destination buffer. May be any size, but
248*4882a593Smuzhiyun * ZSTD_compressBound(srcSize) is guaranteed to be large enough.
249*4882a593Smuzhiyun * @src: The data to compress.
250*4882a593Smuzhiyun * @srcSize: The size of the data to compress.
251*4882a593Smuzhiyun * @params: The parameters to use for compression. See ZSTD_getParams().
252*4882a593Smuzhiyun *
253*4882a593Smuzhiyun * Return: The compressed size or an error, which can be checked using
254*4882a593Smuzhiyun * ZSTD_isError().
255*4882a593Smuzhiyun */
256*4882a593Smuzhiyun size_t ZSTD_compressCCtx(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity,
257*4882a593Smuzhiyun const void *src, size_t srcSize, ZSTD_parameters params);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /**
260*4882a593Smuzhiyun * ZSTD_DCtxWorkspaceBound() - amount of memory needed to initialize a ZSTD_DCtx
261*4882a593Smuzhiyun *
262*4882a593Smuzhiyun * Return: A lower bound on the size of the workspace that is passed to
263*4882a593Smuzhiyun * ZSTD_initDCtx().
264*4882a593Smuzhiyun */
265*4882a593Smuzhiyun size_t ZSTD_DCtxWorkspaceBound(void);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /**
268*4882a593Smuzhiyun * struct ZSTD_DCtx - the zstd decompression context
269*4882a593Smuzhiyun *
270*4882a593Smuzhiyun * When decompressing many times it is recommended to allocate a context just
271*4882a593Smuzhiyun * once and reuse it for each successive decompression operation.
272*4882a593Smuzhiyun */
273*4882a593Smuzhiyun typedef struct ZSTD_DCtx_s ZSTD_DCtx;
274*4882a593Smuzhiyun /**
275*4882a593Smuzhiyun * ZSTD_initDCtx() - initialize a zstd decompression context
276*4882a593Smuzhiyun * @workspace: The workspace to emplace the context into. It must outlive
277*4882a593Smuzhiyun * the returned context.
278*4882a593Smuzhiyun * @workspaceSize: The size of workspace. Use ZSTD_DCtxWorkspaceBound() to
279*4882a593Smuzhiyun * determine how large the workspace must be.
280*4882a593Smuzhiyun *
281*4882a593Smuzhiyun * Return: A decompression context emplaced into workspace.
282*4882a593Smuzhiyun */
283*4882a593Smuzhiyun ZSTD_DCtx *ZSTD_initDCtx(void *workspace, size_t workspaceSize);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /**
286*4882a593Smuzhiyun * ZSTD_decompressDCtx() - decompress zstd compressed src into dst
287*4882a593Smuzhiyun * @ctx: The decompression context.
288*4882a593Smuzhiyun * @dst: The buffer to decompress src into.
289*4882a593Smuzhiyun * @dstCapacity: The size of the destination buffer. Must be at least as large
290*4882a593Smuzhiyun * as the decompressed size. If the caller cannot upper bound the
291*4882a593Smuzhiyun * decompressed size, then it's better to use the streaming API.
292*4882a593Smuzhiyun * @src: The zstd compressed data to decompress. Multiple concatenated
293*4882a593Smuzhiyun * frames and skippable frames are allowed.
294*4882a593Smuzhiyun * @srcSize: The exact size of the data to decompress.
295*4882a593Smuzhiyun *
296*4882a593Smuzhiyun * Return: The decompressed size or an error, which can be checked using
297*4882a593Smuzhiyun * ZSTD_isError().
298*4882a593Smuzhiyun */
299*4882a593Smuzhiyun size_t ZSTD_decompressDCtx(ZSTD_DCtx *ctx, void *dst, size_t dstCapacity,
300*4882a593Smuzhiyun const void *src, size_t srcSize);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /*-************************
303*4882a593Smuzhiyun * Simple dictionary API
304*4882a593Smuzhiyun **************************/
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /**
307*4882a593Smuzhiyun * ZSTD_compress_usingDict() - compress src into dst using a dictionary
308*4882a593Smuzhiyun * @ctx: The context. Must have been initialized with a workspace at
309*4882a593Smuzhiyun * least as large as ZSTD_CCtxWorkspaceBound(params.cParams).
310*4882a593Smuzhiyun * @dst: The buffer to compress src into.
311*4882a593Smuzhiyun * @dstCapacity: The size of the destination buffer. May be any size, but
312*4882a593Smuzhiyun * ZSTD_compressBound(srcSize) is guaranteed to be large enough.
313*4882a593Smuzhiyun * @src: The data to compress.
314*4882a593Smuzhiyun * @srcSize: The size of the data to compress.
315*4882a593Smuzhiyun * @dict: The dictionary to use for compression.
316*4882a593Smuzhiyun * @dictSize: The size of the dictionary.
317*4882a593Smuzhiyun * @params: The parameters to use for compression. See ZSTD_getParams().
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun * Compression using a predefined dictionary. The same dictionary must be used
320*4882a593Smuzhiyun * during decompression.
321*4882a593Smuzhiyun *
322*4882a593Smuzhiyun * Return: The compressed size or an error, which can be checked using
323*4882a593Smuzhiyun * ZSTD_isError().
324*4882a593Smuzhiyun */
325*4882a593Smuzhiyun size_t ZSTD_compress_usingDict(ZSTD_CCtx *ctx, void *dst, size_t dstCapacity,
326*4882a593Smuzhiyun const void *src, size_t srcSize, const void *dict, size_t dictSize,
327*4882a593Smuzhiyun ZSTD_parameters params);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun * ZSTD_decompress_usingDict() - decompress src into dst using a dictionary
331*4882a593Smuzhiyun * @ctx: The decompression context.
332*4882a593Smuzhiyun * @dst: The buffer to decompress src into.
333*4882a593Smuzhiyun * @dstCapacity: The size of the destination buffer. Must be at least as large
334*4882a593Smuzhiyun * as the decompressed size. If the caller cannot upper bound the
335*4882a593Smuzhiyun * decompressed size, then it's better to use the streaming API.
336*4882a593Smuzhiyun * @src: The zstd compressed data to decompress. Multiple concatenated
337*4882a593Smuzhiyun * frames and skippable frames are allowed.
338*4882a593Smuzhiyun * @srcSize: The exact size of the data to decompress.
339*4882a593Smuzhiyun * @dict: The dictionary to use for decompression. The same dictionary
340*4882a593Smuzhiyun * must've been used to compress the data.
341*4882a593Smuzhiyun * @dictSize: The size of the dictionary.
342*4882a593Smuzhiyun *
343*4882a593Smuzhiyun * Return: The decompressed size or an error, which can be checked using
344*4882a593Smuzhiyun * ZSTD_isError().
345*4882a593Smuzhiyun */
346*4882a593Smuzhiyun size_t ZSTD_decompress_usingDict(ZSTD_DCtx *ctx, void *dst, size_t dstCapacity,
347*4882a593Smuzhiyun const void *src, size_t srcSize, const void *dict, size_t dictSize);
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun /*-**************************
350*4882a593Smuzhiyun * Fast dictionary API
351*4882a593Smuzhiyun ***************************/
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /**
354*4882a593Smuzhiyun * ZSTD_CDictWorkspaceBound() - memory needed to initialize a ZSTD_CDict
355*4882a593Smuzhiyun * @cParams: The compression parameters to be used for compression.
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * Return: A lower bound on the size of the workspace that is passed to
358*4882a593Smuzhiyun * ZSTD_initCDict().
359*4882a593Smuzhiyun */
360*4882a593Smuzhiyun size_t ZSTD_CDictWorkspaceBound(ZSTD_compressionParameters cParams);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun /**
363*4882a593Smuzhiyun * struct ZSTD_CDict - a digested dictionary to be used for compression
364*4882a593Smuzhiyun */
365*4882a593Smuzhiyun typedef struct ZSTD_CDict_s ZSTD_CDict;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /**
368*4882a593Smuzhiyun * ZSTD_initCDict() - initialize a digested dictionary for compression
369*4882a593Smuzhiyun * @dictBuffer: The dictionary to digest. The buffer is referenced by the
370*4882a593Smuzhiyun * ZSTD_CDict so it must outlive the returned ZSTD_CDict.
371*4882a593Smuzhiyun * @dictSize: The size of the dictionary.
372*4882a593Smuzhiyun * @params: The parameters to use for compression. See ZSTD_getParams().
373*4882a593Smuzhiyun * @workspace: The workspace. It must outlive the returned ZSTD_CDict.
374*4882a593Smuzhiyun * @workspaceSize: The workspace size. Must be at least
375*4882a593Smuzhiyun * ZSTD_CDictWorkspaceBound(params.cParams).
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * When compressing multiple messages / blocks with the same dictionary it is
378*4882a593Smuzhiyun * recommended to load it just once. The ZSTD_CDict merely references the
379*4882a593Smuzhiyun * dictBuffer, so it must outlive the returned ZSTD_CDict.
380*4882a593Smuzhiyun *
381*4882a593Smuzhiyun * Return: The digested dictionary emplaced into workspace.
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun ZSTD_CDict *ZSTD_initCDict(const void *dictBuffer, size_t dictSize,
384*4882a593Smuzhiyun ZSTD_parameters params, void *workspace, size_t workspaceSize);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /**
387*4882a593Smuzhiyun * ZSTD_compress_usingCDict() - compress src into dst using a ZSTD_CDict
388*4882a593Smuzhiyun * @ctx: The context. Must have been initialized with a workspace at
389*4882a593Smuzhiyun * least as large as ZSTD_CCtxWorkspaceBound(cParams) where
390*4882a593Smuzhiyun * cParams are the compression parameters used to initialize the
391*4882a593Smuzhiyun * cdict.
392*4882a593Smuzhiyun * @dst: The buffer to compress src into.
393*4882a593Smuzhiyun * @dstCapacity: The size of the destination buffer. May be any size, but
394*4882a593Smuzhiyun * ZSTD_compressBound(srcSize) is guaranteed to be large enough.
395*4882a593Smuzhiyun * @src: The data to compress.
396*4882a593Smuzhiyun * @srcSize: The size of the data to compress.
397*4882a593Smuzhiyun * @cdict: The digested dictionary to use for compression.
398*4882a593Smuzhiyun * @params: The parameters to use for compression. See ZSTD_getParams().
399*4882a593Smuzhiyun *
400*4882a593Smuzhiyun * Compression using a digested dictionary. The same dictionary must be used
401*4882a593Smuzhiyun * during decompression.
402*4882a593Smuzhiyun *
403*4882a593Smuzhiyun * Return: The compressed size or an error, which can be checked using
404*4882a593Smuzhiyun * ZSTD_isError().
405*4882a593Smuzhiyun */
406*4882a593Smuzhiyun size_t ZSTD_compress_usingCDict(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
407*4882a593Smuzhiyun const void *src, size_t srcSize, const ZSTD_CDict *cdict);
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun * ZSTD_DDictWorkspaceBound() - memory needed to initialize a ZSTD_DDict
412*4882a593Smuzhiyun *
413*4882a593Smuzhiyun * Return: A lower bound on the size of the workspace that is passed to
414*4882a593Smuzhiyun * ZSTD_initDDict().
415*4882a593Smuzhiyun */
416*4882a593Smuzhiyun size_t ZSTD_DDictWorkspaceBound(void);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun /**
419*4882a593Smuzhiyun * struct ZSTD_DDict - a digested dictionary to be used for decompression
420*4882a593Smuzhiyun */
421*4882a593Smuzhiyun typedef struct ZSTD_DDict_s ZSTD_DDict;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /**
424*4882a593Smuzhiyun * ZSTD_initDDict() - initialize a digested dictionary for decompression
425*4882a593Smuzhiyun * @dictBuffer: The dictionary to digest. The buffer is referenced by the
426*4882a593Smuzhiyun * ZSTD_DDict so it must outlive the returned ZSTD_DDict.
427*4882a593Smuzhiyun * @dictSize: The size of the dictionary.
428*4882a593Smuzhiyun * @workspace: The workspace. It must outlive the returned ZSTD_DDict.
429*4882a593Smuzhiyun * @workspaceSize: The workspace size. Must be at least
430*4882a593Smuzhiyun * ZSTD_DDictWorkspaceBound().
431*4882a593Smuzhiyun *
432*4882a593Smuzhiyun * When decompressing multiple messages / blocks with the same dictionary it is
433*4882a593Smuzhiyun * recommended to load it just once. The ZSTD_DDict merely references the
434*4882a593Smuzhiyun * dictBuffer, so it must outlive the returned ZSTD_DDict.
435*4882a593Smuzhiyun *
436*4882a593Smuzhiyun * Return: The digested dictionary emplaced into workspace.
437*4882a593Smuzhiyun */
438*4882a593Smuzhiyun ZSTD_DDict *ZSTD_initDDict(const void *dictBuffer, size_t dictSize,
439*4882a593Smuzhiyun void *workspace, size_t workspaceSize);
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /**
442*4882a593Smuzhiyun * ZSTD_decompress_usingDDict() - decompress src into dst using a ZSTD_DDict
443*4882a593Smuzhiyun * @ctx: The decompression context.
444*4882a593Smuzhiyun * @dst: The buffer to decompress src into.
445*4882a593Smuzhiyun * @dstCapacity: The size of the destination buffer. Must be at least as large
446*4882a593Smuzhiyun * as the decompressed size. If the caller cannot upper bound the
447*4882a593Smuzhiyun * decompressed size, then it's better to use the streaming API.
448*4882a593Smuzhiyun * @src: The zstd compressed data to decompress. Multiple concatenated
449*4882a593Smuzhiyun * frames and skippable frames are allowed.
450*4882a593Smuzhiyun * @srcSize: The exact size of the data to decompress.
451*4882a593Smuzhiyun * @ddict: The digested dictionary to use for decompression. The same
452*4882a593Smuzhiyun * dictionary must've been used to compress the data.
453*4882a593Smuzhiyun *
454*4882a593Smuzhiyun * Return: The decompressed size or an error, which can be checked using
455*4882a593Smuzhiyun * ZSTD_isError().
456*4882a593Smuzhiyun */
457*4882a593Smuzhiyun size_t ZSTD_decompress_usingDDict(ZSTD_DCtx *dctx, void *dst,
458*4882a593Smuzhiyun size_t dstCapacity, const void *src, size_t srcSize,
459*4882a593Smuzhiyun const ZSTD_DDict *ddict);
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /*-**************************
463*4882a593Smuzhiyun * Streaming
464*4882a593Smuzhiyun ***************************/
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun /**
467*4882a593Smuzhiyun * struct ZSTD_inBuffer - input buffer for streaming
468*4882a593Smuzhiyun * @src: Start of the input buffer.
469*4882a593Smuzhiyun * @size: Size of the input buffer.
470*4882a593Smuzhiyun * @pos: Position where reading stopped. Will be updated.
471*4882a593Smuzhiyun * Necessarily 0 <= pos <= size.
472*4882a593Smuzhiyun */
473*4882a593Smuzhiyun typedef struct ZSTD_inBuffer_s {
474*4882a593Smuzhiyun const void *src;
475*4882a593Smuzhiyun size_t size;
476*4882a593Smuzhiyun size_t pos;
477*4882a593Smuzhiyun } ZSTD_inBuffer;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /**
480*4882a593Smuzhiyun * struct ZSTD_outBuffer - output buffer for streaming
481*4882a593Smuzhiyun * @dst: Start of the output buffer.
482*4882a593Smuzhiyun * @size: Size of the output buffer.
483*4882a593Smuzhiyun * @pos: Position where writing stopped. Will be updated.
484*4882a593Smuzhiyun * Necessarily 0 <= pos <= size.
485*4882a593Smuzhiyun */
486*4882a593Smuzhiyun typedef struct ZSTD_outBuffer_s {
487*4882a593Smuzhiyun void *dst;
488*4882a593Smuzhiyun size_t size;
489*4882a593Smuzhiyun size_t pos;
490*4882a593Smuzhiyun } ZSTD_outBuffer;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun /*-*****************************************************************************
495*4882a593Smuzhiyun * Streaming compression - HowTo
496*4882a593Smuzhiyun *
497*4882a593Smuzhiyun * A ZSTD_CStream object is required to track streaming operation.
498*4882a593Smuzhiyun * Use ZSTD_initCStream() to initialize a ZSTD_CStream object.
499*4882a593Smuzhiyun * ZSTD_CStream objects can be reused multiple times on consecutive compression
500*4882a593Smuzhiyun * operations. It is recommended to re-use ZSTD_CStream in situations where many
501*4882a593Smuzhiyun * streaming operations will be achieved consecutively. Use one separate
502*4882a593Smuzhiyun * ZSTD_CStream per thread for parallel execution.
503*4882a593Smuzhiyun *
504*4882a593Smuzhiyun * Use ZSTD_compressStream() repetitively to consume input stream.
505*4882a593Smuzhiyun * The function will automatically update both `pos` fields.
506*4882a593Smuzhiyun * Note that it may not consume the entire input, in which case `pos < size`,
507*4882a593Smuzhiyun * and it's up to the caller to present again remaining data.
508*4882a593Smuzhiyun * It returns a hint for the preferred number of bytes to use as an input for
509*4882a593Smuzhiyun * the next function call.
510*4882a593Smuzhiyun *
511*4882a593Smuzhiyun * At any moment, it's possible to flush whatever data remains within internal
512*4882a593Smuzhiyun * buffer, using ZSTD_flushStream(). `output->pos` will be updated. There might
513*4882a593Smuzhiyun * still be some content left within the internal buffer if `output->size` is
514*4882a593Smuzhiyun * too small. It returns the number of bytes left in the internal buffer and
515*4882a593Smuzhiyun * must be called until it returns 0.
516*4882a593Smuzhiyun *
517*4882a593Smuzhiyun * ZSTD_endStream() instructs to finish a frame. It will perform a flush and
518*4882a593Smuzhiyun * write frame epilogue. The epilogue is required for decoders to consider a
519*4882a593Smuzhiyun * frame completed. Similar to ZSTD_flushStream(), it may not be able to flush
520*4882a593Smuzhiyun * the full content if `output->size` is too small. In which case, call again
521*4882a593Smuzhiyun * ZSTD_endStream() to complete the flush. It returns the number of bytes left
522*4882a593Smuzhiyun * in the internal buffer and must be called until it returns 0.
523*4882a593Smuzhiyun ******************************************************************************/
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun /**
526*4882a593Smuzhiyun * ZSTD_CStreamWorkspaceBound() - memory needed to initialize a ZSTD_CStream
527*4882a593Smuzhiyun * @cParams: The compression parameters to be used for compression.
528*4882a593Smuzhiyun *
529*4882a593Smuzhiyun * Return: A lower bound on the size of the workspace that is passed to
530*4882a593Smuzhiyun * ZSTD_initCStream() and ZSTD_initCStream_usingCDict().
531*4882a593Smuzhiyun */
532*4882a593Smuzhiyun size_t ZSTD_CStreamWorkspaceBound(ZSTD_compressionParameters cParams);
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun /**
535*4882a593Smuzhiyun * struct ZSTD_CStream - the zstd streaming compression context
536*4882a593Smuzhiyun */
537*4882a593Smuzhiyun typedef struct ZSTD_CStream_s ZSTD_CStream;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /*===== ZSTD_CStream management functions =====*/
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun * ZSTD_initCStream() - initialize a zstd streaming compression context
542*4882a593Smuzhiyun * @params: The zstd compression parameters.
543*4882a593Smuzhiyun * @pledgedSrcSize: If params.fParams.contentSizeFlag == 1 then the caller must
544*4882a593Smuzhiyun * pass the source size (zero means empty source). Otherwise,
545*4882a593Smuzhiyun * the caller may optionally pass the source size, or zero if
546*4882a593Smuzhiyun * unknown.
547*4882a593Smuzhiyun * @workspace: The workspace to emplace the context into. It must outlive
548*4882a593Smuzhiyun * the returned context.
549*4882a593Smuzhiyun * @workspaceSize: The size of workspace.
550*4882a593Smuzhiyun * Use ZSTD_CStreamWorkspaceBound(params.cParams) to determine
551*4882a593Smuzhiyun * how large the workspace must be.
552*4882a593Smuzhiyun *
553*4882a593Smuzhiyun * Return: The zstd streaming compression context.
554*4882a593Smuzhiyun */
555*4882a593Smuzhiyun ZSTD_CStream *ZSTD_initCStream(ZSTD_parameters params,
556*4882a593Smuzhiyun unsigned long long pledgedSrcSize, void *workspace,
557*4882a593Smuzhiyun size_t workspaceSize);
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun /**
560*4882a593Smuzhiyun * ZSTD_initCStream_usingCDict() - initialize a streaming compression context
561*4882a593Smuzhiyun * @cdict: The digested dictionary to use for compression.
562*4882a593Smuzhiyun * @pledgedSrcSize: Optionally the source size, or zero if unknown.
563*4882a593Smuzhiyun * @workspace: The workspace to emplace the context into. It must outlive
564*4882a593Smuzhiyun * the returned context.
565*4882a593Smuzhiyun * @workspaceSize: The size of workspace. Call ZSTD_CStreamWorkspaceBound()
566*4882a593Smuzhiyun * with the cParams used to initialize the cdict to determine
567*4882a593Smuzhiyun * how large the workspace must be.
568*4882a593Smuzhiyun *
569*4882a593Smuzhiyun * Return: The zstd streaming compression context.
570*4882a593Smuzhiyun */
571*4882a593Smuzhiyun ZSTD_CStream *ZSTD_initCStream_usingCDict(const ZSTD_CDict *cdict,
572*4882a593Smuzhiyun unsigned long long pledgedSrcSize, void *workspace,
573*4882a593Smuzhiyun size_t workspaceSize);
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun /*===== Streaming compression functions =====*/
576*4882a593Smuzhiyun /**
577*4882a593Smuzhiyun * ZSTD_resetCStream() - reset the context using parameters from creation
578*4882a593Smuzhiyun * @zcs: The zstd streaming compression context to reset.
579*4882a593Smuzhiyun * @pledgedSrcSize: Optionally the source size, or zero if unknown.
580*4882a593Smuzhiyun *
581*4882a593Smuzhiyun * Resets the context using the parameters from creation. Skips dictionary
582*4882a593Smuzhiyun * loading, since it can be reused. If `pledgedSrcSize` is non-zero the frame
583*4882a593Smuzhiyun * content size is always written into the frame header.
584*4882a593Smuzhiyun *
585*4882a593Smuzhiyun * Return: Zero or an error, which can be checked using ZSTD_isError().
586*4882a593Smuzhiyun */
587*4882a593Smuzhiyun size_t ZSTD_resetCStream(ZSTD_CStream *zcs, unsigned long long pledgedSrcSize);
588*4882a593Smuzhiyun /**
589*4882a593Smuzhiyun * ZSTD_compressStream() - streaming compress some of input into output
590*4882a593Smuzhiyun * @zcs: The zstd streaming compression context.
591*4882a593Smuzhiyun * @output: Destination buffer. `output->pos` is updated to indicate how much
592*4882a593Smuzhiyun * compressed data was written.
593*4882a593Smuzhiyun * @input: Source buffer. `input->pos` is updated to indicate how much data was
594*4882a593Smuzhiyun * read. Note that it may not consume the entire input, in which case
595*4882a593Smuzhiyun * `input->pos < input->size`, and it's up to the caller to present
596*4882a593Smuzhiyun * remaining data again.
597*4882a593Smuzhiyun *
598*4882a593Smuzhiyun * The `input` and `output` buffers may be any size. Guaranteed to make some
599*4882a593Smuzhiyun * forward progress if `input` and `output` are not empty.
600*4882a593Smuzhiyun *
601*4882a593Smuzhiyun * Return: A hint for the number of bytes to use as the input for the next
602*4882a593Smuzhiyun * function call or an error, which can be checked using
603*4882a593Smuzhiyun * ZSTD_isError().
604*4882a593Smuzhiyun */
605*4882a593Smuzhiyun size_t ZSTD_compressStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output,
606*4882a593Smuzhiyun ZSTD_inBuffer *input);
607*4882a593Smuzhiyun /**
608*4882a593Smuzhiyun * ZSTD_flushStream() - flush internal buffers into output
609*4882a593Smuzhiyun * @zcs: The zstd streaming compression context.
610*4882a593Smuzhiyun * @output: Destination buffer. `output->pos` is updated to indicate how much
611*4882a593Smuzhiyun * compressed data was written.
612*4882a593Smuzhiyun *
613*4882a593Smuzhiyun * ZSTD_flushStream() must be called until it returns 0, meaning all the data
614*4882a593Smuzhiyun * has been flushed. Since ZSTD_flushStream() causes a block to be ended,
615*4882a593Smuzhiyun * calling it too often will degrade the compression ratio.
616*4882a593Smuzhiyun *
617*4882a593Smuzhiyun * Return: The number of bytes still present within internal buffers or an
618*4882a593Smuzhiyun * error, which can be checked using ZSTD_isError().
619*4882a593Smuzhiyun */
620*4882a593Smuzhiyun size_t ZSTD_flushStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output);
621*4882a593Smuzhiyun /**
622*4882a593Smuzhiyun * ZSTD_endStream() - flush internal buffers into output and end the frame
623*4882a593Smuzhiyun * @zcs: The zstd streaming compression context.
624*4882a593Smuzhiyun * @output: Destination buffer. `output->pos` is updated to indicate how much
625*4882a593Smuzhiyun * compressed data was written.
626*4882a593Smuzhiyun *
627*4882a593Smuzhiyun * ZSTD_endStream() must be called until it returns 0, meaning all the data has
628*4882a593Smuzhiyun * been flushed and the frame epilogue has been written.
629*4882a593Smuzhiyun *
630*4882a593Smuzhiyun * Return: The number of bytes still present within internal buffers or an
631*4882a593Smuzhiyun * error, which can be checked using ZSTD_isError().
632*4882a593Smuzhiyun */
633*4882a593Smuzhiyun size_t ZSTD_endStream(ZSTD_CStream *zcs, ZSTD_outBuffer *output);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun /**
636*4882a593Smuzhiyun * ZSTD_CStreamInSize() - recommended size for the input buffer
637*4882a593Smuzhiyun *
638*4882a593Smuzhiyun * Return: The recommended size for the input buffer.
639*4882a593Smuzhiyun */
640*4882a593Smuzhiyun size_t ZSTD_CStreamInSize(void);
641*4882a593Smuzhiyun /**
642*4882a593Smuzhiyun * ZSTD_CStreamOutSize() - recommended size for the output buffer
643*4882a593Smuzhiyun *
644*4882a593Smuzhiyun * When the output buffer is at least this large, it is guaranteed to be large
645*4882a593Smuzhiyun * enough to flush at least one complete compressed block.
646*4882a593Smuzhiyun *
647*4882a593Smuzhiyun * Return: The recommended size for the output buffer.
648*4882a593Smuzhiyun */
649*4882a593Smuzhiyun size_t ZSTD_CStreamOutSize(void);
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun /*-*****************************************************************************
654*4882a593Smuzhiyun * Streaming decompression - HowTo
655*4882a593Smuzhiyun *
656*4882a593Smuzhiyun * A ZSTD_DStream object is required to track streaming operations.
657*4882a593Smuzhiyun * Use ZSTD_initDStream() to initialize a ZSTD_DStream object.
658*4882a593Smuzhiyun * ZSTD_DStream objects can be re-used multiple times.
659*4882a593Smuzhiyun *
660*4882a593Smuzhiyun * Use ZSTD_decompressStream() repetitively to consume your input.
661*4882a593Smuzhiyun * The function will update both `pos` fields.
662*4882a593Smuzhiyun * If `input->pos < input->size`, some input has not been consumed.
663*4882a593Smuzhiyun * It's up to the caller to present again remaining data.
664*4882a593Smuzhiyun * If `output->pos < output->size`, decoder has flushed everything it could.
665*4882a593Smuzhiyun * Returns 0 iff a frame is completely decoded and fully flushed.
666*4882a593Smuzhiyun * Otherwise it returns a suggested next input size that will never load more
667*4882a593Smuzhiyun * than the current frame.
668*4882a593Smuzhiyun ******************************************************************************/
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun /**
671*4882a593Smuzhiyun * ZSTD_DStreamWorkspaceBound() - memory needed to initialize a ZSTD_DStream
672*4882a593Smuzhiyun * @maxWindowSize: The maximum window size allowed for compressed frames.
673*4882a593Smuzhiyun *
674*4882a593Smuzhiyun * Return: A lower bound on the size of the workspace that is passed to
675*4882a593Smuzhiyun * ZSTD_initDStream() and ZSTD_initDStream_usingDDict().
676*4882a593Smuzhiyun */
677*4882a593Smuzhiyun size_t ZSTD_DStreamWorkspaceBound(size_t maxWindowSize);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun /**
680*4882a593Smuzhiyun * struct ZSTD_DStream - the zstd streaming decompression context
681*4882a593Smuzhiyun */
682*4882a593Smuzhiyun typedef struct ZSTD_DStream_s ZSTD_DStream;
683*4882a593Smuzhiyun /*===== ZSTD_DStream management functions =====*/
684*4882a593Smuzhiyun /**
685*4882a593Smuzhiyun * ZSTD_initDStream() - initialize a zstd streaming decompression context
686*4882a593Smuzhiyun * @maxWindowSize: The maximum window size allowed for compressed frames.
687*4882a593Smuzhiyun * @workspace: The workspace to emplace the context into. It must outlive
688*4882a593Smuzhiyun * the returned context.
689*4882a593Smuzhiyun * @workspaceSize: The size of workspace.
690*4882a593Smuzhiyun * Use ZSTD_DStreamWorkspaceBound(maxWindowSize) to determine
691*4882a593Smuzhiyun * how large the workspace must be.
692*4882a593Smuzhiyun *
693*4882a593Smuzhiyun * Return: The zstd streaming decompression context.
694*4882a593Smuzhiyun */
695*4882a593Smuzhiyun ZSTD_DStream *ZSTD_initDStream(size_t maxWindowSize, void *workspace,
696*4882a593Smuzhiyun size_t workspaceSize);
697*4882a593Smuzhiyun /**
698*4882a593Smuzhiyun * ZSTD_initDStream_usingDDict() - initialize streaming decompression context
699*4882a593Smuzhiyun * @maxWindowSize: The maximum window size allowed for compressed frames.
700*4882a593Smuzhiyun * @ddict: The digested dictionary to use for decompression.
701*4882a593Smuzhiyun * @workspace: The workspace to emplace the context into. It must outlive
702*4882a593Smuzhiyun * the returned context.
703*4882a593Smuzhiyun * @workspaceSize: The size of workspace.
704*4882a593Smuzhiyun * Use ZSTD_DStreamWorkspaceBound(maxWindowSize) to determine
705*4882a593Smuzhiyun * how large the workspace must be.
706*4882a593Smuzhiyun *
707*4882a593Smuzhiyun * Return: The zstd streaming decompression context.
708*4882a593Smuzhiyun */
709*4882a593Smuzhiyun ZSTD_DStream *ZSTD_initDStream_usingDDict(size_t maxWindowSize,
710*4882a593Smuzhiyun const ZSTD_DDict *ddict, void *workspace, size_t workspaceSize);
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun /*===== Streaming decompression functions =====*/
713*4882a593Smuzhiyun /**
714*4882a593Smuzhiyun * ZSTD_resetDStream() - reset the context using parameters from creation
715*4882a593Smuzhiyun * @zds: The zstd streaming decompression context to reset.
716*4882a593Smuzhiyun *
717*4882a593Smuzhiyun * Resets the context using the parameters from creation. Skips dictionary
718*4882a593Smuzhiyun * loading, since it can be reused.
719*4882a593Smuzhiyun *
720*4882a593Smuzhiyun * Return: Zero or an error, which can be checked using ZSTD_isError().
721*4882a593Smuzhiyun */
722*4882a593Smuzhiyun size_t ZSTD_resetDStream(ZSTD_DStream *zds);
723*4882a593Smuzhiyun /**
724*4882a593Smuzhiyun * ZSTD_decompressStream() - streaming decompress some of input into output
725*4882a593Smuzhiyun * @zds: The zstd streaming decompression context.
726*4882a593Smuzhiyun * @output: Destination buffer. `output.pos` is updated to indicate how much
727*4882a593Smuzhiyun * decompressed data was written.
728*4882a593Smuzhiyun * @input: Source buffer. `input.pos` is updated to indicate how much data was
729*4882a593Smuzhiyun * read. Note that it may not consume the entire input, in which case
730*4882a593Smuzhiyun * `input.pos < input.size`, and it's up to the caller to present
731*4882a593Smuzhiyun * remaining data again.
732*4882a593Smuzhiyun *
733*4882a593Smuzhiyun * The `input` and `output` buffers may be any size. Guaranteed to make some
734*4882a593Smuzhiyun * forward progress if `input` and `output` are not empty.
735*4882a593Smuzhiyun * ZSTD_decompressStream() will not consume the last byte of the frame until
736*4882a593Smuzhiyun * the entire frame is flushed.
737*4882a593Smuzhiyun *
738*4882a593Smuzhiyun * Return: Returns 0 iff a frame is completely decoded and fully flushed.
739*4882a593Smuzhiyun * Otherwise returns a hint for the number of bytes to use as the input
740*4882a593Smuzhiyun * for the next function call or an error, which can be checked using
741*4882a593Smuzhiyun * ZSTD_isError(). The size hint will never load more than the frame.
742*4882a593Smuzhiyun */
743*4882a593Smuzhiyun size_t ZSTD_decompressStream(ZSTD_DStream *zds, ZSTD_outBuffer *output,
744*4882a593Smuzhiyun ZSTD_inBuffer *input);
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun /**
747*4882a593Smuzhiyun * ZSTD_DStreamInSize() - recommended size for the input buffer
748*4882a593Smuzhiyun *
749*4882a593Smuzhiyun * Return: The recommended size for the input buffer.
750*4882a593Smuzhiyun */
751*4882a593Smuzhiyun size_t ZSTD_DStreamInSize(void);
752*4882a593Smuzhiyun /**
753*4882a593Smuzhiyun * ZSTD_DStreamOutSize() - recommended size for the output buffer
754*4882a593Smuzhiyun *
755*4882a593Smuzhiyun * When the output buffer is at least this large, it is guaranteed to be large
756*4882a593Smuzhiyun * enough to flush at least one complete decompressed block.
757*4882a593Smuzhiyun *
758*4882a593Smuzhiyun * Return: The recommended size for the output buffer.
759*4882a593Smuzhiyun */
760*4882a593Smuzhiyun size_t ZSTD_DStreamOutSize(void);
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /* --- Constants ---*/
764*4882a593Smuzhiyun #define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */
765*4882a593Smuzhiyun #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
768*4882a593Smuzhiyun #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun #define ZSTD_WINDOWLOG_MAX_32 27
771*4882a593Smuzhiyun #define ZSTD_WINDOWLOG_MAX_64 27
772*4882a593Smuzhiyun #define ZSTD_WINDOWLOG_MAX \
773*4882a593Smuzhiyun ((unsigned int)(sizeof(size_t) == 4 \
774*4882a593Smuzhiyun ? ZSTD_WINDOWLOG_MAX_32 \
775*4882a593Smuzhiyun : ZSTD_WINDOWLOG_MAX_64))
776*4882a593Smuzhiyun #define ZSTD_WINDOWLOG_MIN 10
777*4882a593Smuzhiyun #define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX
778*4882a593Smuzhiyun #define ZSTD_HASHLOG_MIN 6
779*4882a593Smuzhiyun #define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
780*4882a593Smuzhiyun #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
781*4882a593Smuzhiyun #define ZSTD_HASHLOG3_MAX 17
782*4882a593Smuzhiyun #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
783*4882a593Smuzhiyun #define ZSTD_SEARCHLOG_MIN 1
784*4882a593Smuzhiyun /* only for ZSTD_fast, other strategies are limited to 6 */
785*4882a593Smuzhiyun #define ZSTD_SEARCHLENGTH_MAX 7
786*4882a593Smuzhiyun /* only for ZSTD_btopt, other strategies are limited to 4 */
787*4882a593Smuzhiyun #define ZSTD_SEARCHLENGTH_MIN 3
788*4882a593Smuzhiyun #define ZSTD_TARGETLENGTH_MIN 4
789*4882a593Smuzhiyun #define ZSTD_TARGETLENGTH_MAX 999
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun /* for static allocation */
792*4882a593Smuzhiyun #define ZSTD_FRAMEHEADERSIZE_MAX 18
793*4882a593Smuzhiyun #define ZSTD_FRAMEHEADERSIZE_MIN 6
794*4882a593Smuzhiyun static const size_t ZSTD_frameHeaderSize_prefix = 5;
795*4882a593Smuzhiyun static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN;
796*4882a593Smuzhiyun static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
797*4882a593Smuzhiyun /* magic number + skippable frame length */
798*4882a593Smuzhiyun static const size_t ZSTD_skippableHeaderSize = 8;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun /*-*************************************
802*4882a593Smuzhiyun * Compressed size functions
803*4882a593Smuzhiyun **************************************/
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun /**
806*4882a593Smuzhiyun * ZSTD_findFrameCompressedSize() - returns the size of a compressed frame
807*4882a593Smuzhiyun * @src: Source buffer. It should point to the start of a zstd encoded frame
808*4882a593Smuzhiyun * or a skippable frame.
809*4882a593Smuzhiyun * @srcSize: The size of the source buffer. It must be at least as large as the
810*4882a593Smuzhiyun * size of the frame.
811*4882a593Smuzhiyun *
812*4882a593Smuzhiyun * Return: The compressed size of the frame pointed to by `src` or an error,
813*4882a593Smuzhiyun * which can be check with ZSTD_isError().
814*4882a593Smuzhiyun * Suitable to pass to ZSTD_decompress() or similar functions.
815*4882a593Smuzhiyun */
816*4882a593Smuzhiyun size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize);
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun /*-*************************************
819*4882a593Smuzhiyun * Decompressed size functions
820*4882a593Smuzhiyun **************************************/
821*4882a593Smuzhiyun /**
822*4882a593Smuzhiyun * ZSTD_getFrameContentSize() - returns the content size in a zstd frame header
823*4882a593Smuzhiyun * @src: It should point to the start of a zstd encoded frame.
824*4882a593Smuzhiyun * @srcSize: The size of the source buffer. It must be at least as large as the
825*4882a593Smuzhiyun * frame header. `ZSTD_frameHeaderSize_max` is always large enough.
826*4882a593Smuzhiyun *
827*4882a593Smuzhiyun * Return: The frame content size stored in the frame header if known.
828*4882a593Smuzhiyun * `ZSTD_CONTENTSIZE_UNKNOWN` if the content size isn't stored in the
829*4882a593Smuzhiyun * frame header. `ZSTD_CONTENTSIZE_ERROR` on invalid input.
830*4882a593Smuzhiyun */
831*4882a593Smuzhiyun unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /**
834*4882a593Smuzhiyun * ZSTD_findDecompressedSize() - returns decompressed size of a series of frames
835*4882a593Smuzhiyun * @src: It should point to the start of a series of zstd encoded and/or
836*4882a593Smuzhiyun * skippable frames.
837*4882a593Smuzhiyun * @srcSize: The exact size of the series of frames.
838*4882a593Smuzhiyun *
839*4882a593Smuzhiyun * If any zstd encoded frame in the series doesn't have the frame content size
840*4882a593Smuzhiyun * set, `ZSTD_CONTENTSIZE_UNKNOWN` is returned. But frame content size is always
841*4882a593Smuzhiyun * set when using ZSTD_compress(). The decompressed size can be very large.
842*4882a593Smuzhiyun * If the source is untrusted, the decompressed size could be wrong or
843*4882a593Smuzhiyun * intentionally modified. Always ensure the result fits within the
844*4882a593Smuzhiyun * application's authorized limits. ZSTD_findDecompressedSize() handles multiple
845*4882a593Smuzhiyun * frames, and so it must traverse the input to read each frame header. This is
846*4882a593Smuzhiyun * efficient as most of the data is skipped, however it does mean that all frame
847*4882a593Smuzhiyun * data must be present and valid.
848*4882a593Smuzhiyun *
849*4882a593Smuzhiyun * Return: Decompressed size of all the data contained in the frames if known.
850*4882a593Smuzhiyun * `ZSTD_CONTENTSIZE_UNKNOWN` if the decompressed size is unknown.
851*4882a593Smuzhiyun * `ZSTD_CONTENTSIZE_ERROR` if an error occurred.
852*4882a593Smuzhiyun */
853*4882a593Smuzhiyun unsigned long long ZSTD_findDecompressedSize(const void *src, size_t srcSize);
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun /*-*************************************
856*4882a593Smuzhiyun * Advanced compression functions
857*4882a593Smuzhiyun **************************************/
858*4882a593Smuzhiyun /**
859*4882a593Smuzhiyun * ZSTD_checkCParams() - ensure parameter values remain within authorized range
860*4882a593Smuzhiyun * @cParams: The zstd compression parameters.
861*4882a593Smuzhiyun *
862*4882a593Smuzhiyun * Return: Zero or an error, which can be checked using ZSTD_isError().
863*4882a593Smuzhiyun */
864*4882a593Smuzhiyun size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams);
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /**
867*4882a593Smuzhiyun * ZSTD_adjustCParams() - optimize parameters for a given srcSize and dictSize
868*4882a593Smuzhiyun * @srcSize: Optionally the estimated source size, or zero if unknown.
869*4882a593Smuzhiyun * @dictSize: Optionally the estimated dictionary size, or zero if unknown.
870*4882a593Smuzhiyun *
871*4882a593Smuzhiyun * Return: The optimized parameters.
872*4882a593Smuzhiyun */
873*4882a593Smuzhiyun ZSTD_compressionParameters ZSTD_adjustCParams(
874*4882a593Smuzhiyun ZSTD_compressionParameters cParams, unsigned long long srcSize,
875*4882a593Smuzhiyun size_t dictSize);
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun /*--- Advanced decompression functions ---*/
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun /**
880*4882a593Smuzhiyun * ZSTD_isFrame() - returns true iff the buffer starts with a valid frame
881*4882a593Smuzhiyun * @buffer: The source buffer to check.
882*4882a593Smuzhiyun * @size: The size of the source buffer, must be at least 4 bytes.
883*4882a593Smuzhiyun *
884*4882a593Smuzhiyun * Return: True iff the buffer starts with a zstd or skippable frame identifier.
885*4882a593Smuzhiyun */
886*4882a593Smuzhiyun unsigned int ZSTD_isFrame(const void *buffer, size_t size);
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /**
889*4882a593Smuzhiyun * ZSTD_getDictID_fromDict() - returns the dictionary id stored in a dictionary
890*4882a593Smuzhiyun * @dict: The dictionary buffer.
891*4882a593Smuzhiyun * @dictSize: The size of the dictionary buffer.
892*4882a593Smuzhiyun *
893*4882a593Smuzhiyun * Return: The dictionary id stored within the dictionary or 0 if the
894*4882a593Smuzhiyun * dictionary is not a zstd dictionary. If it returns 0 the
895*4882a593Smuzhiyun * dictionary can still be loaded as a content-only dictionary.
896*4882a593Smuzhiyun */
897*4882a593Smuzhiyun unsigned int ZSTD_getDictID_fromDict(const void *dict, size_t dictSize);
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun /**
900*4882a593Smuzhiyun * ZSTD_getDictID_fromDDict() - returns the dictionary id stored in a ZSTD_DDict
901*4882a593Smuzhiyun * @ddict: The ddict to find the id of.
902*4882a593Smuzhiyun *
903*4882a593Smuzhiyun * Return: The dictionary id stored within `ddict` or 0 if the dictionary is not
904*4882a593Smuzhiyun * a zstd dictionary. If it returns 0 `ddict` will be loaded as a
905*4882a593Smuzhiyun * content-only dictionary.
906*4882a593Smuzhiyun */
907*4882a593Smuzhiyun unsigned int ZSTD_getDictID_fromDDict(const ZSTD_DDict *ddict);
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun /**
910*4882a593Smuzhiyun * ZSTD_getDictID_fromFrame() - returns the dictionary id stored in a zstd frame
911*4882a593Smuzhiyun * @src: Source buffer. It must be a zstd encoded frame.
912*4882a593Smuzhiyun * @srcSize: The size of the source buffer. It must be at least as large as the
913*4882a593Smuzhiyun * frame header. `ZSTD_frameHeaderSize_max` is always large enough.
914*4882a593Smuzhiyun *
915*4882a593Smuzhiyun * Return: The dictionary id required to decompress the frame stored within
916*4882a593Smuzhiyun * `src` or 0 if the dictionary id could not be decoded. It can return
917*4882a593Smuzhiyun * 0 if the frame does not require a dictionary, the dictionary id
918*4882a593Smuzhiyun * wasn't stored in the frame, `src` is not a zstd frame, or `srcSize`
919*4882a593Smuzhiyun * is too small.
920*4882a593Smuzhiyun */
921*4882a593Smuzhiyun unsigned int ZSTD_getDictID_fromFrame(const void *src, size_t srcSize);
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun /**
924*4882a593Smuzhiyun * struct ZSTD_frameParams - zstd frame parameters stored in the frame header
925*4882a593Smuzhiyun * @frameContentSize: The frame content size, or 0 if not present.
926*4882a593Smuzhiyun * @windowSize: The window size, or 0 if the frame is a skippable frame.
927*4882a593Smuzhiyun * @dictID: The dictionary id, or 0 if not present.
928*4882a593Smuzhiyun * @checksumFlag: Whether a checksum was used.
929*4882a593Smuzhiyun */
930*4882a593Smuzhiyun typedef struct {
931*4882a593Smuzhiyun unsigned long long frameContentSize;
932*4882a593Smuzhiyun unsigned int windowSize;
933*4882a593Smuzhiyun unsigned int dictID;
934*4882a593Smuzhiyun unsigned int checksumFlag;
935*4882a593Smuzhiyun } ZSTD_frameParams;
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun /**
938*4882a593Smuzhiyun * ZSTD_getFrameParams() - extracts parameters from a zstd or skippable frame
939*4882a593Smuzhiyun * @fparamsPtr: On success the frame parameters are written here.
940*4882a593Smuzhiyun * @src: The source buffer. It must point to a zstd or skippable frame.
941*4882a593Smuzhiyun * @srcSize: The size of the source buffer. `ZSTD_frameHeaderSize_max` is
942*4882a593Smuzhiyun * always large enough to succeed.
943*4882a593Smuzhiyun *
944*4882a593Smuzhiyun * Return: 0 on success. If more data is required it returns how many bytes
945*4882a593Smuzhiyun * must be provided to make forward progress. Otherwise it returns
946*4882a593Smuzhiyun * an error, which can be checked using ZSTD_isError().
947*4882a593Smuzhiyun */
948*4882a593Smuzhiyun size_t ZSTD_getFrameParams(ZSTD_frameParams *fparamsPtr, const void *src,
949*4882a593Smuzhiyun size_t srcSize);
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun /*-*****************************************************************************
952*4882a593Smuzhiyun * Buffer-less and synchronous inner streaming functions
953*4882a593Smuzhiyun *
954*4882a593Smuzhiyun * This is an advanced API, giving full control over buffer management, for
955*4882a593Smuzhiyun * users which need direct control over memory.
956*4882a593Smuzhiyun * But it's also a complex one, with many restrictions (documented below).
957*4882a593Smuzhiyun * Prefer using normal streaming API for an easier experience
958*4882a593Smuzhiyun ******************************************************************************/
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun /*-*****************************************************************************
961*4882a593Smuzhiyun * Buffer-less streaming compression (synchronous mode)
962*4882a593Smuzhiyun *
963*4882a593Smuzhiyun * A ZSTD_CCtx object is required to track streaming operations.
964*4882a593Smuzhiyun * Use ZSTD_initCCtx() to initialize a context.
965*4882a593Smuzhiyun * ZSTD_CCtx object can be re-used multiple times within successive compression
966*4882a593Smuzhiyun * operations.
967*4882a593Smuzhiyun *
968*4882a593Smuzhiyun * Start by initializing a context.
969*4882a593Smuzhiyun * Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary
970*4882a593Smuzhiyun * compression,
971*4882a593Smuzhiyun * or ZSTD_compressBegin_advanced(), for finer parameter control.
972*4882a593Smuzhiyun * It's also possible to duplicate a reference context which has already been
973*4882a593Smuzhiyun * initialized, using ZSTD_copyCCtx()
974*4882a593Smuzhiyun *
975*4882a593Smuzhiyun * Then, consume your input using ZSTD_compressContinue().
976*4882a593Smuzhiyun * There are some important considerations to keep in mind when using this
977*4882a593Smuzhiyun * advanced function :
978*4882a593Smuzhiyun * - ZSTD_compressContinue() has no internal buffer. It uses externally provided
979*4882a593Smuzhiyun * buffer only.
980*4882a593Smuzhiyun * - Interface is synchronous : input is consumed entirely and produce 1+
981*4882a593Smuzhiyun * (or more) compressed blocks.
982*4882a593Smuzhiyun * - Caller must ensure there is enough space in `dst` to store compressed data
983*4882a593Smuzhiyun * under worst case scenario. Worst case evaluation is provided by
984*4882a593Smuzhiyun * ZSTD_compressBound().
985*4882a593Smuzhiyun * ZSTD_compressContinue() doesn't guarantee recover after a failed
986*4882a593Smuzhiyun * compression.
987*4882a593Smuzhiyun * - ZSTD_compressContinue() presumes prior input ***is still accessible and
988*4882a593Smuzhiyun * unmodified*** (up to maximum distance size, see WindowLog).
989*4882a593Smuzhiyun * It remembers all previous contiguous blocks, plus one separated memory
990*4882a593Smuzhiyun * segment (which can itself consists of multiple contiguous blocks)
991*4882a593Smuzhiyun * - ZSTD_compressContinue() detects that prior input has been overwritten when
992*4882a593Smuzhiyun * `src` buffer overlaps. In which case, it will "discard" the relevant memory
993*4882a593Smuzhiyun * section from its history.
994*4882a593Smuzhiyun *
995*4882a593Smuzhiyun * Finish a frame with ZSTD_compressEnd(), which will write the last block(s)
996*4882a593Smuzhiyun * and optional checksum. It's possible to use srcSize==0, in which case, it
997*4882a593Smuzhiyun * will write a final empty block to end the frame. Without last block mark,
998*4882a593Smuzhiyun * frames will be considered unfinished (corrupted) by decoders.
999*4882a593Smuzhiyun *
1000*4882a593Smuzhiyun * `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress some new
1001*4882a593Smuzhiyun * frame.
1002*4882a593Smuzhiyun ******************************************************************************/
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun /*===== Buffer-less streaming compression functions =====*/
1005*4882a593Smuzhiyun size_t ZSTD_compressBegin(ZSTD_CCtx *cctx, int compressionLevel);
1006*4882a593Smuzhiyun size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx *cctx, const void *dict,
1007*4882a593Smuzhiyun size_t dictSize, int compressionLevel);
1008*4882a593Smuzhiyun size_t ZSTD_compressBegin_advanced(ZSTD_CCtx *cctx, const void *dict,
1009*4882a593Smuzhiyun size_t dictSize, ZSTD_parameters params,
1010*4882a593Smuzhiyun unsigned long long pledgedSrcSize);
1011*4882a593Smuzhiyun size_t ZSTD_copyCCtx(ZSTD_CCtx *cctx, const ZSTD_CCtx *preparedCCtx,
1012*4882a593Smuzhiyun unsigned long long pledgedSrcSize);
1013*4882a593Smuzhiyun size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx *cctx, const ZSTD_CDict *cdict,
1014*4882a593Smuzhiyun unsigned long long pledgedSrcSize);
1015*4882a593Smuzhiyun size_t ZSTD_compressContinue(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
1016*4882a593Smuzhiyun const void *src, size_t srcSize);
1017*4882a593Smuzhiyun size_t ZSTD_compressEnd(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
1018*4882a593Smuzhiyun const void *src, size_t srcSize);
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun /*-*****************************************************************************
1023*4882a593Smuzhiyun * Buffer-less streaming decompression (synchronous mode)
1024*4882a593Smuzhiyun *
1025*4882a593Smuzhiyun * A ZSTD_DCtx object is required to track streaming operations.
1026*4882a593Smuzhiyun * Use ZSTD_initDCtx() to initialize a context.
1027*4882a593Smuzhiyun * A ZSTD_DCtx object can be re-used multiple times.
1028*4882a593Smuzhiyun *
1029*4882a593Smuzhiyun * First typical operation is to retrieve frame parameters, using
1030*4882a593Smuzhiyun * ZSTD_getFrameParams(). It fills a ZSTD_frameParams structure which provide
1031*4882a593Smuzhiyun * important information to correctly decode the frame, such as the minimum
1032*4882a593Smuzhiyun * rolling buffer size to allocate to decompress data (`windowSize`), and the
1033*4882a593Smuzhiyun * dictionary ID used.
1034*4882a593Smuzhiyun * Note: content size is optional, it may not be present. 0 means unknown.
1035*4882a593Smuzhiyun * Note that these values could be wrong, either because of data malformation,
1036*4882a593Smuzhiyun * or because an attacker is spoofing deliberate false information. As a
1037*4882a593Smuzhiyun * consequence, check that values remain within valid application range,
1038*4882a593Smuzhiyun * especially `windowSize`, before allocation. Each application can set its own
1039*4882a593Smuzhiyun * limit, depending on local restrictions. For extended interoperability, it is
1040*4882a593Smuzhiyun * recommended to support at least 8 MB.
1041*4882a593Smuzhiyun * Frame parameters are extracted from the beginning of the compressed frame.
1042*4882a593Smuzhiyun * Data fragment must be large enough to ensure successful decoding, typically
1043*4882a593Smuzhiyun * `ZSTD_frameHeaderSize_max` bytes.
1044*4882a593Smuzhiyun * Result: 0: successful decoding, the `ZSTD_frameParams` structure is filled.
1045*4882a593Smuzhiyun * >0: `srcSize` is too small, provide at least this many bytes.
1046*4882a593Smuzhiyun * errorCode, which can be tested using ZSTD_isError().
1047*4882a593Smuzhiyun *
1048*4882a593Smuzhiyun * Start decompression, with ZSTD_decompressBegin() or
1049*4882a593Smuzhiyun * ZSTD_decompressBegin_usingDict(). Alternatively, you can copy a prepared
1050*4882a593Smuzhiyun * context, using ZSTD_copyDCtx().
1051*4882a593Smuzhiyun *
1052*4882a593Smuzhiyun * Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue()
1053*4882a593Smuzhiyun * alternatively.
1054*4882a593Smuzhiyun * ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize'
1055*4882a593Smuzhiyun * to ZSTD_decompressContinue().
1056*4882a593Smuzhiyun * ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will
1057*4882a593Smuzhiyun * fail.
1058*4882a593Smuzhiyun *
1059*4882a593Smuzhiyun * The result of ZSTD_decompressContinue() is the number of bytes regenerated
1060*4882a593Smuzhiyun * within 'dst' (necessarily <= dstCapacity). It can be zero, which is not an
1061*4882a593Smuzhiyun * error; it just means ZSTD_decompressContinue() has decoded some metadata
1062*4882a593Smuzhiyun * item. It can also be an error code, which can be tested with ZSTD_isError().
1063*4882a593Smuzhiyun *
1064*4882a593Smuzhiyun * ZSTD_decompressContinue() needs previous data blocks during decompression, up
1065*4882a593Smuzhiyun * to `windowSize`. They should preferably be located contiguously, prior to
1066*4882a593Smuzhiyun * current block. Alternatively, a round buffer of sufficient size is also
1067*4882a593Smuzhiyun * possible. Sufficient size is determined by frame parameters.
1068*4882a593Smuzhiyun * ZSTD_decompressContinue() is very sensitive to contiguity, if 2 blocks don't
1069*4882a593Smuzhiyun * follow each other, make sure that either the compressor breaks contiguity at
1070*4882a593Smuzhiyun * the same place, or that previous contiguous segment is large enough to
1071*4882a593Smuzhiyun * properly handle maximum back-reference.
1072*4882a593Smuzhiyun *
1073*4882a593Smuzhiyun * A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
1074*4882a593Smuzhiyun * Context can then be reset to start a new decompression.
1075*4882a593Smuzhiyun *
1076*4882a593Smuzhiyun * Note: it's possible to know if next input to present is a header or a block,
1077*4882a593Smuzhiyun * using ZSTD_nextInputType(). This information is not required to properly
1078*4882a593Smuzhiyun * decode a frame.
1079*4882a593Smuzhiyun *
1080*4882a593Smuzhiyun * == Special case: skippable frames ==
1081*4882a593Smuzhiyun *
1082*4882a593Smuzhiyun * Skippable frames allow integration of user-defined data into a flow of
1083*4882a593Smuzhiyun * concatenated frames. Skippable frames will be ignored (skipped) by a
1084*4882a593Smuzhiyun * decompressor. The format of skippable frames is as follows:
1085*4882a593Smuzhiyun * a) Skippable frame ID - 4 Bytes, Little endian format, any value from
1086*4882a593Smuzhiyun * 0x184D2A50 to 0x184D2A5F
1087*4882a593Smuzhiyun * b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
1088*4882a593Smuzhiyun * c) Frame Content - any content (User Data) of length equal to Frame Size
1089*4882a593Smuzhiyun * For skippable frames ZSTD_decompressContinue() always returns 0.
1090*4882a593Smuzhiyun * For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0
1091*4882a593Smuzhiyun * what means that a frame is skippable.
1092*4882a593Smuzhiyun * Note: If fparamsPtr->frameContentSize==0, it is ambiguous: the frame might
1093*4882a593Smuzhiyun * actually be a zstd encoded frame with no content. For purposes of
1094*4882a593Smuzhiyun * decompression, it is valid in both cases to skip the frame using
1095*4882a593Smuzhiyun * ZSTD_findFrameCompressedSize() to find its size in bytes.
1096*4882a593Smuzhiyun * It also returns frame size as fparamsPtr->frameContentSize.
1097*4882a593Smuzhiyun ******************************************************************************/
1098*4882a593Smuzhiyun
1099*4882a593Smuzhiyun /*===== Buffer-less streaming decompression functions =====*/
1100*4882a593Smuzhiyun size_t ZSTD_decompressBegin(ZSTD_DCtx *dctx);
1101*4882a593Smuzhiyun size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx *dctx, const void *dict,
1102*4882a593Smuzhiyun size_t dictSize);
1103*4882a593Smuzhiyun void ZSTD_copyDCtx(ZSTD_DCtx *dctx, const ZSTD_DCtx *preparedDCtx);
1104*4882a593Smuzhiyun size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx *dctx);
1105*4882a593Smuzhiyun size_t ZSTD_decompressContinue(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity,
1106*4882a593Smuzhiyun const void *src, size_t srcSize);
1107*4882a593Smuzhiyun typedef enum {
1108*4882a593Smuzhiyun ZSTDnit_frameHeader,
1109*4882a593Smuzhiyun ZSTDnit_blockHeader,
1110*4882a593Smuzhiyun ZSTDnit_block,
1111*4882a593Smuzhiyun ZSTDnit_lastBlock,
1112*4882a593Smuzhiyun ZSTDnit_checksum,
1113*4882a593Smuzhiyun ZSTDnit_skippableFrame
1114*4882a593Smuzhiyun } ZSTD_nextInputType_e;
1115*4882a593Smuzhiyun ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx *dctx);
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /*-*****************************************************************************
1118*4882a593Smuzhiyun * Block functions
1119*4882a593Smuzhiyun *
1120*4882a593Smuzhiyun * Block functions produce and decode raw zstd blocks, without frame metadata.
1121*4882a593Smuzhiyun * Frame metadata cost is typically ~18 bytes, which can be non-negligible for
1122*4882a593Smuzhiyun * very small blocks (< 100 bytes). User will have to take in charge required
1123*4882a593Smuzhiyun * information to regenerate data, such as compressed and content sizes.
1124*4882a593Smuzhiyun *
1125*4882a593Smuzhiyun * A few rules to respect:
1126*4882a593Smuzhiyun * - Compressing and decompressing require a context structure
1127*4882a593Smuzhiyun * + Use ZSTD_initCCtx() and ZSTD_initDCtx()
1128*4882a593Smuzhiyun * - It is necessary to init context before starting
1129*4882a593Smuzhiyun * + compression : ZSTD_compressBegin()
1130*4882a593Smuzhiyun * + decompression : ZSTD_decompressBegin()
1131*4882a593Smuzhiyun * + variants _usingDict() are also allowed
1132*4882a593Smuzhiyun * + copyCCtx() and copyDCtx() work too
1133*4882a593Smuzhiyun * - Block size is limited, it must be <= ZSTD_getBlockSizeMax()
1134*4882a593Smuzhiyun * + If you need to compress more, cut data into multiple blocks
1135*4882a593Smuzhiyun * + Consider using the regular ZSTD_compress() instead, as frame metadata
1136*4882a593Smuzhiyun * costs become negligible when source size is large.
1137*4882a593Smuzhiyun * - When a block is considered not compressible enough, ZSTD_compressBlock()
1138*4882a593Smuzhiyun * result will be zero. In which case, nothing is produced into `dst`.
1139*4882a593Smuzhiyun * + User must test for such outcome and deal directly with uncompressed data
1140*4882a593Smuzhiyun * + ZSTD_decompressBlock() doesn't accept uncompressed data as input!!!
1141*4882a593Smuzhiyun * + In case of multiple successive blocks, decoder must be informed of
1142*4882a593Smuzhiyun * uncompressed block existence to follow proper history. Use
1143*4882a593Smuzhiyun * ZSTD_insertBlock() in such a case.
1144*4882a593Smuzhiyun ******************************************************************************/
1145*4882a593Smuzhiyun
1146*4882a593Smuzhiyun /* Define for static allocation */
1147*4882a593Smuzhiyun #define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024)
1148*4882a593Smuzhiyun /*===== Raw zstd block functions =====*/
1149*4882a593Smuzhiyun size_t ZSTD_getBlockSizeMax(ZSTD_CCtx *cctx);
1150*4882a593Smuzhiyun size_t ZSTD_compressBlock(ZSTD_CCtx *cctx, void *dst, size_t dstCapacity,
1151*4882a593Smuzhiyun const void *src, size_t srcSize);
1152*4882a593Smuzhiyun size_t ZSTD_decompressBlock(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity,
1153*4882a593Smuzhiyun const void *src, size_t srcSize);
1154*4882a593Smuzhiyun size_t ZSTD_insertBlock(ZSTD_DCtx *dctx, const void *blockStart,
1155*4882a593Smuzhiyun size_t blockSize);
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun #endif /* ZSTD_H */
1158