xref: /freebsd/sys/contrib/zstd/lib/legacy/zstd_v05.h (revision 0c16b53773565120a8f80a31a0af2ef56ccd368e)
1*0c16b537SWarner Losh /*
2*0c16b537SWarner Losh  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3*0c16b537SWarner Losh  * All rights reserved.
4*0c16b537SWarner Losh  *
5*0c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
6*0c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*0c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
8*0c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
9*0c16b537SWarner Losh  */
10*0c16b537SWarner Losh 
11*0c16b537SWarner Losh #ifndef ZSTDv05_H
12*0c16b537SWarner Losh #define ZSTDv05_H
13*0c16b537SWarner Losh 
14*0c16b537SWarner Losh #if defined (__cplusplus)
15*0c16b537SWarner Losh extern "C" {
16*0c16b537SWarner Losh #endif
17*0c16b537SWarner Losh 
18*0c16b537SWarner Losh /*-*************************************
19*0c16b537SWarner Losh *  Dependencies
20*0c16b537SWarner Losh ***************************************/
21*0c16b537SWarner Losh #include <stddef.h>   /* size_t */
22*0c16b537SWarner Losh #include "mem.h"      /* U64, U32 */
23*0c16b537SWarner Losh 
24*0c16b537SWarner Losh 
25*0c16b537SWarner Losh /* *************************************
26*0c16b537SWarner Losh *  Simple functions
27*0c16b537SWarner Losh ***************************************/
28*0c16b537SWarner Losh /*! ZSTDv05_decompress() :
29*0c16b537SWarner Losh     `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.
30*0c16b537SWarner Losh     `dstCapacity` must be large enough, equal or larger than originalSize.
31*0c16b537SWarner Losh     @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
32*0c16b537SWarner Losh               or an errorCode if it fails (which can be tested using ZSTDv05_isError()) */
33*0c16b537SWarner Losh size_t ZSTDv05_decompress( void* dst, size_t dstCapacity,
34*0c16b537SWarner Losh                      const void* src, size_t compressedSize);
35*0c16b537SWarner Losh 
36*0c16b537SWarner Losh /**
37*0c16b537SWarner Losh ZSTDv05_getFrameSrcSize() : get the source length of a ZSTD frame
38*0c16b537SWarner Losh     compressedSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
39*0c16b537SWarner Losh     return : the number of bytes that would be read to decompress this frame
40*0c16b537SWarner Losh              or an errorCode if it fails (which can be tested using ZSTDv05_isError())
41*0c16b537SWarner Losh */
42*0c16b537SWarner Losh size_t ZSTDv05_findFrameCompressedSize(const void* src, size_t compressedSize);
43*0c16b537SWarner Losh 
44*0c16b537SWarner Losh /* *************************************
45*0c16b537SWarner Losh *  Helper functions
46*0c16b537SWarner Losh ***************************************/
47*0c16b537SWarner Losh /* Error Management */
48*0c16b537SWarner Losh unsigned    ZSTDv05_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */
49*0c16b537SWarner Losh const char* ZSTDv05_getErrorName(size_t code);     /*!< provides readable string for an error code */
50*0c16b537SWarner Losh 
51*0c16b537SWarner Losh 
52*0c16b537SWarner Losh /* *************************************
53*0c16b537SWarner Losh *  Explicit memory management
54*0c16b537SWarner Losh ***************************************/
55*0c16b537SWarner Losh /** Decompression context */
56*0c16b537SWarner Losh typedef struct ZSTDv05_DCtx_s ZSTDv05_DCtx;
57*0c16b537SWarner Losh ZSTDv05_DCtx* ZSTDv05_createDCtx(void);
58*0c16b537SWarner Losh size_t ZSTDv05_freeDCtx(ZSTDv05_DCtx* dctx);      /*!< @return : errorCode */
59*0c16b537SWarner Losh 
60*0c16b537SWarner Losh /** ZSTDv05_decompressDCtx() :
61*0c16b537SWarner Losh *   Same as ZSTDv05_decompress(), but requires an already allocated ZSTDv05_DCtx (see ZSTDv05_createDCtx()) */
62*0c16b537SWarner Losh size_t ZSTDv05_decompressDCtx(ZSTDv05_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
63*0c16b537SWarner Losh 
64*0c16b537SWarner Losh 
65*0c16b537SWarner Losh /*-***********************
66*0c16b537SWarner Losh *  Simple Dictionary API
67*0c16b537SWarner Losh *************************/
68*0c16b537SWarner Losh /*! ZSTDv05_decompress_usingDict() :
69*0c16b537SWarner Losh *   Decompression using a pre-defined Dictionary content (see dictBuilder).
70*0c16b537SWarner Losh *   Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.
71*0c16b537SWarner Losh *   Note : dict can be NULL, in which case, it's equivalent to ZSTDv05_decompressDCtx() */
72*0c16b537SWarner Losh size_t ZSTDv05_decompress_usingDict(ZSTDv05_DCtx* dctx,
73*0c16b537SWarner Losh                                             void* dst, size_t dstCapacity,
74*0c16b537SWarner Losh                                       const void* src, size_t srcSize,
75*0c16b537SWarner Losh                                       const void* dict,size_t dictSize);
76*0c16b537SWarner Losh 
77*0c16b537SWarner Losh /*-************************
78*0c16b537SWarner Losh *  Advanced Streaming API
79*0c16b537SWarner Losh ***************************/
80*0c16b537SWarner Losh typedef enum { ZSTDv05_fast, ZSTDv05_greedy, ZSTDv05_lazy, ZSTDv05_lazy2, ZSTDv05_btlazy2, ZSTDv05_opt, ZSTDv05_btopt } ZSTDv05_strategy;
81*0c16b537SWarner Losh typedef struct {
82*0c16b537SWarner Losh     U64 srcSize;
83*0c16b537SWarner Losh     U32 windowLog;     /* the only useful information to retrieve */
84*0c16b537SWarner Losh     U32 contentLog; U32 hashLog; U32 searchLog; U32 searchLength; U32 targetLength; ZSTDv05_strategy strategy;
85*0c16b537SWarner Losh } ZSTDv05_parameters;
86*0c16b537SWarner Losh size_t ZSTDv05_getFrameParams(ZSTDv05_parameters* params, const void* src, size_t srcSize);
87*0c16b537SWarner Losh 
88*0c16b537SWarner Losh size_t ZSTDv05_decompressBegin_usingDict(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize);
89*0c16b537SWarner Losh void   ZSTDv05_copyDCtx(ZSTDv05_DCtx* dstDCtx, const ZSTDv05_DCtx* srcDCtx);
90*0c16b537SWarner Losh size_t ZSTDv05_nextSrcSizeToDecompress(ZSTDv05_DCtx* dctx);
91*0c16b537SWarner Losh size_t ZSTDv05_decompressContinue(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
92*0c16b537SWarner Losh 
93*0c16b537SWarner Losh 
94*0c16b537SWarner Losh /*-***********************
95*0c16b537SWarner Losh *  ZBUFF API
96*0c16b537SWarner Losh *************************/
97*0c16b537SWarner Losh typedef struct ZBUFFv05_DCtx_s ZBUFFv05_DCtx;
98*0c16b537SWarner Losh ZBUFFv05_DCtx* ZBUFFv05_createDCtx(void);
99*0c16b537SWarner Losh size_t         ZBUFFv05_freeDCtx(ZBUFFv05_DCtx* dctx);
100*0c16b537SWarner Losh 
101*0c16b537SWarner Losh size_t ZBUFFv05_decompressInit(ZBUFFv05_DCtx* dctx);
102*0c16b537SWarner Losh size_t ZBUFFv05_decompressInitDictionary(ZBUFFv05_DCtx* dctx, const void* dict, size_t dictSize);
103*0c16b537SWarner Losh 
104*0c16b537SWarner Losh size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* dctx,
105*0c16b537SWarner Losh                                             void* dst, size_t* dstCapacityPtr,
106*0c16b537SWarner Losh                                       const void* src, size_t* srcSizePtr);
107*0c16b537SWarner Losh 
108*0c16b537SWarner Losh /*-***************************************************************************
109*0c16b537SWarner Losh *  Streaming decompression
110*0c16b537SWarner Losh *
111*0c16b537SWarner Losh *  A ZBUFFv05_DCtx object is required to track streaming operations.
112*0c16b537SWarner Losh *  Use ZBUFFv05_createDCtx() and ZBUFFv05_freeDCtx() to create/release resources.
113*0c16b537SWarner Losh *  Use ZBUFFv05_decompressInit() to start a new decompression operation,
114*0c16b537SWarner Losh *   or ZBUFFv05_decompressInitDictionary() if decompression requires a dictionary.
115*0c16b537SWarner Losh *  Note that ZBUFFv05_DCtx objects can be reused multiple times.
116*0c16b537SWarner Losh *
117*0c16b537SWarner Losh *  Use ZBUFFv05_decompressContinue() repetitively to consume your input.
118*0c16b537SWarner Losh *  *srcSizePtr and *dstCapacityPtr can be any size.
119*0c16b537SWarner Losh *  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
120*0c16b537SWarner Losh *  Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
121*0c16b537SWarner Losh *  The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change @dst.
122*0c16b537SWarner Losh *  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency)
123*0c16b537SWarner Losh *            or 0 when a frame is completely decoded
124*0c16b537SWarner Losh *            or an error code, which can be tested using ZBUFFv05_isError().
125*0c16b537SWarner Losh *
126*0c16b537SWarner Losh *  Hint : recommended buffer sizes (not compulsory) : ZBUFFv05_recommendedDInSize() / ZBUFFv05_recommendedDOutSize()
127*0c16b537SWarner Losh *  output : ZBUFFv05_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
128*0c16b537SWarner Losh *  input  : ZBUFFv05_recommendedDInSize==128Kb+3; just follow indications from ZBUFFv05_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
129*0c16b537SWarner Losh * *******************************************************************************/
130*0c16b537SWarner Losh 
131*0c16b537SWarner Losh 
132*0c16b537SWarner Losh /* *************************************
133*0c16b537SWarner Losh *  Tool functions
134*0c16b537SWarner Losh ***************************************/
135*0c16b537SWarner Losh unsigned ZBUFFv05_isError(size_t errorCode);
136*0c16b537SWarner Losh const char* ZBUFFv05_getErrorName(size_t errorCode);
137*0c16b537SWarner Losh 
138*0c16b537SWarner Losh /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
139*0c16b537SWarner Losh *   These sizes are just hints, and tend to offer better latency */
140*0c16b537SWarner Losh size_t ZBUFFv05_recommendedDInSize(void);
141*0c16b537SWarner Losh size_t ZBUFFv05_recommendedDOutSize(void);
142*0c16b537SWarner Losh 
143*0c16b537SWarner Losh 
144*0c16b537SWarner Losh 
145*0c16b537SWarner Losh /*-*************************************
146*0c16b537SWarner Losh *  Constants
147*0c16b537SWarner Losh ***************************************/
148*0c16b537SWarner Losh #define ZSTDv05_MAGICNUMBER 0xFD2FB525   /* v0.5 */
149*0c16b537SWarner Losh 
150*0c16b537SWarner Losh 
151*0c16b537SWarner Losh 
152*0c16b537SWarner Losh 
153*0c16b537SWarner Losh #if defined (__cplusplus)
154*0c16b537SWarner Losh }
155*0c16b537SWarner Losh #endif
156*0c16b537SWarner Losh 
157*0c16b537SWarner Losh #endif  /* ZSTDv0505_H */
158