xref: /freebsd/sys/contrib/zstd/lib/legacy/zstd_v05.h (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
10c16b537SWarner Losh /*
2*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh  */
100c16b537SWarner Losh 
110c16b537SWarner Losh #ifndef ZSTDv05_H
120c16b537SWarner Losh #define ZSTDv05_H
130c16b537SWarner Losh 
140c16b537SWarner Losh #if defined (__cplusplus)
150c16b537SWarner Losh extern "C" {
160c16b537SWarner Losh #endif
170c16b537SWarner Losh 
180c16b537SWarner Losh /*-*************************************
190c16b537SWarner Losh *  Dependencies
200c16b537SWarner Losh ***************************************/
210c16b537SWarner Losh #include <stddef.h>   /* size_t */
2237f1f268SConrad Meyer #include "../common/mem.h"      /* U64, U32 */
230c16b537SWarner Losh 
240c16b537SWarner Losh 
250c16b537SWarner Losh /* *************************************
260c16b537SWarner Losh *  Simple functions
270c16b537SWarner Losh ***************************************/
280c16b537SWarner Losh /*! ZSTDv05_decompress() :
290c16b537SWarner Losh     `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.
300c16b537SWarner Losh     `dstCapacity` must be large enough, equal or larger than originalSize.
310c16b537SWarner Losh     @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
320c16b537SWarner Losh               or an errorCode if it fails (which can be tested using ZSTDv05_isError()) */
330c16b537SWarner Losh size_t ZSTDv05_decompress( void* dst, size_t dstCapacity,
340c16b537SWarner Losh                      const void* src, size_t compressedSize);
350c16b537SWarner Losh 
360c16b537SWarner Losh  /**
372b9c00cbSConrad Meyer  ZSTDv05_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.5.x format
382b9c00cbSConrad Meyer      srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
392b9c00cbSConrad Meyer      cSize (output parameter)  : the number of bytes that would be read to decompress this frame
402b9c00cbSConrad Meyer                                  or an error code if it fails (which can be tested using ZSTDv01_isError())
412b9c00cbSConrad Meyer      dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
422b9c00cbSConrad Meyer                                  or ZSTD_CONTENTSIZE_ERROR if an error occurs
432b9c00cbSConrad Meyer 
442b9c00cbSConrad Meyer     note : assumes `cSize` and `dBound` are _not_ NULL.
450c16b537SWarner Losh  */
462b9c00cbSConrad Meyer void ZSTDv05_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
472b9c00cbSConrad Meyer                                      size_t* cSize, unsigned long long* dBound);
480c16b537SWarner Losh 
490c16b537SWarner Losh /* *************************************
500c16b537SWarner Losh *  Helper functions
510c16b537SWarner Losh ***************************************/
520c16b537SWarner Losh /* Error Management */
530c16b537SWarner Losh unsigned    ZSTDv05_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */
540c16b537SWarner Losh const char* ZSTDv05_getErrorName(size_t code);     /*!< provides readable string for an error code */
550c16b537SWarner Losh 
560c16b537SWarner Losh 
570c16b537SWarner Losh /* *************************************
580c16b537SWarner Losh *  Explicit memory management
590c16b537SWarner Losh ***************************************/
600c16b537SWarner Losh /** Decompression context */
610c16b537SWarner Losh typedef struct ZSTDv05_DCtx_s ZSTDv05_DCtx;
620c16b537SWarner Losh ZSTDv05_DCtx* ZSTDv05_createDCtx(void);
630c16b537SWarner Losh size_t ZSTDv05_freeDCtx(ZSTDv05_DCtx* dctx);      /*!< @return : errorCode */
640c16b537SWarner Losh 
650c16b537SWarner Losh /** ZSTDv05_decompressDCtx() :
660c16b537SWarner Losh *   Same as ZSTDv05_decompress(), but requires an already allocated ZSTDv05_DCtx (see ZSTDv05_createDCtx()) */
670c16b537SWarner Losh size_t ZSTDv05_decompressDCtx(ZSTDv05_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
680c16b537SWarner Losh 
690c16b537SWarner Losh 
700c16b537SWarner Losh /*-***********************
710c16b537SWarner Losh *  Simple Dictionary API
720c16b537SWarner Losh *************************/
730c16b537SWarner Losh /*! ZSTDv05_decompress_usingDict() :
740c16b537SWarner Losh *   Decompression using a pre-defined Dictionary content (see dictBuilder).
750c16b537SWarner Losh *   Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.
760c16b537SWarner Losh *   Note : dict can be NULL, in which case, it's equivalent to ZSTDv05_decompressDCtx() */
770c16b537SWarner Losh size_t ZSTDv05_decompress_usingDict(ZSTDv05_DCtx* dctx,
780c16b537SWarner Losh                                             void* dst, size_t dstCapacity,
790c16b537SWarner Losh                                       const void* src, size_t srcSize,
800c16b537SWarner Losh                                       const void* dict,size_t dictSize);
810c16b537SWarner Losh 
820c16b537SWarner Losh /*-************************
830c16b537SWarner Losh *  Advanced Streaming API
840c16b537SWarner Losh ***************************/
850c16b537SWarner Losh typedef enum { ZSTDv05_fast, ZSTDv05_greedy, ZSTDv05_lazy, ZSTDv05_lazy2, ZSTDv05_btlazy2, ZSTDv05_opt, ZSTDv05_btopt } ZSTDv05_strategy;
860c16b537SWarner Losh typedef struct {
870c16b537SWarner Losh     U64 srcSize;
880c16b537SWarner Losh     U32 windowLog;     /* the only useful information to retrieve */
890c16b537SWarner Losh     U32 contentLog; U32 hashLog; U32 searchLog; U32 searchLength; U32 targetLength; ZSTDv05_strategy strategy;
900c16b537SWarner Losh } ZSTDv05_parameters;
910c16b537SWarner Losh size_t ZSTDv05_getFrameParams(ZSTDv05_parameters* params, const void* src, size_t srcSize);
920c16b537SWarner Losh 
930c16b537SWarner Losh size_t ZSTDv05_decompressBegin_usingDict(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize);
940c16b537SWarner Losh void   ZSTDv05_copyDCtx(ZSTDv05_DCtx* dstDCtx, const ZSTDv05_DCtx* srcDCtx);
950c16b537SWarner Losh size_t ZSTDv05_nextSrcSizeToDecompress(ZSTDv05_DCtx* dctx);
960c16b537SWarner Losh size_t ZSTDv05_decompressContinue(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
970c16b537SWarner Losh 
980c16b537SWarner Losh 
990c16b537SWarner Losh /*-***********************
1000c16b537SWarner Losh *  ZBUFF API
1010c16b537SWarner Losh *************************/
1020c16b537SWarner Losh typedef struct ZBUFFv05_DCtx_s ZBUFFv05_DCtx;
1030c16b537SWarner Losh ZBUFFv05_DCtx* ZBUFFv05_createDCtx(void);
1040c16b537SWarner Losh size_t         ZBUFFv05_freeDCtx(ZBUFFv05_DCtx* dctx);
1050c16b537SWarner Losh 
1060c16b537SWarner Losh size_t ZBUFFv05_decompressInit(ZBUFFv05_DCtx* dctx);
1070c16b537SWarner Losh size_t ZBUFFv05_decompressInitDictionary(ZBUFFv05_DCtx* dctx, const void* dict, size_t dictSize);
1080c16b537SWarner Losh 
1090c16b537SWarner Losh size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* dctx,
1100c16b537SWarner Losh                                             void* dst, size_t* dstCapacityPtr,
1110c16b537SWarner Losh                                       const void* src, size_t* srcSizePtr);
1120c16b537SWarner Losh 
1130c16b537SWarner Losh /*-***************************************************************************
1140c16b537SWarner Losh *  Streaming decompression
1150c16b537SWarner Losh *
1160c16b537SWarner Losh *  A ZBUFFv05_DCtx object is required to track streaming operations.
1170c16b537SWarner Losh *  Use ZBUFFv05_createDCtx() and ZBUFFv05_freeDCtx() to create/release resources.
1180c16b537SWarner Losh *  Use ZBUFFv05_decompressInit() to start a new decompression operation,
1190c16b537SWarner Losh *   or ZBUFFv05_decompressInitDictionary() if decompression requires a dictionary.
1200c16b537SWarner Losh *  Note that ZBUFFv05_DCtx objects can be reused multiple times.
1210c16b537SWarner Losh *
1220c16b537SWarner Losh *  Use ZBUFFv05_decompressContinue() repetitively to consume your input.
1230c16b537SWarner Losh *  *srcSizePtr and *dstCapacityPtr can be any size.
1240c16b537SWarner Losh *  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
1250c16b537SWarner Losh *  Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
1260c16b537SWarner 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.
1270c16b537SWarner 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)
1280c16b537SWarner Losh *            or 0 when a frame is completely decoded
1290c16b537SWarner Losh *            or an error code, which can be tested using ZBUFFv05_isError().
1300c16b537SWarner Losh *
1310c16b537SWarner Losh *  Hint : recommended buffer sizes (not compulsory) : ZBUFFv05_recommendedDInSize() / ZBUFFv05_recommendedDOutSize()
1320c16b537SWarner 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.
1330c16b537SWarner Losh *  input  : ZBUFFv05_recommendedDInSize==128Kb+3; just follow indications from ZBUFFv05_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
1340c16b537SWarner Losh * *******************************************************************************/
1350c16b537SWarner Losh 
1360c16b537SWarner Losh 
1370c16b537SWarner Losh /* *************************************
1380c16b537SWarner Losh *  Tool functions
1390c16b537SWarner Losh ***************************************/
1400c16b537SWarner Losh unsigned ZBUFFv05_isError(size_t errorCode);
1410c16b537SWarner Losh const char* ZBUFFv05_getErrorName(size_t errorCode);
1420c16b537SWarner Losh 
1430c16b537SWarner Losh /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
1440c16b537SWarner Losh *   These sizes are just hints, and tend to offer better latency */
1450c16b537SWarner Losh size_t ZBUFFv05_recommendedDInSize(void);
1460c16b537SWarner Losh size_t ZBUFFv05_recommendedDOutSize(void);
1470c16b537SWarner Losh 
1480c16b537SWarner Losh 
1490c16b537SWarner Losh 
1500c16b537SWarner Losh /*-*************************************
1510c16b537SWarner Losh *  Constants
1520c16b537SWarner Losh ***************************************/
1530c16b537SWarner Losh #define ZSTDv05_MAGICNUMBER 0xFD2FB525   /* v0.5 */
1540c16b537SWarner Losh 
1550c16b537SWarner Losh 
1560c16b537SWarner Losh 
1570c16b537SWarner Losh 
1580c16b537SWarner Losh #if defined (__cplusplus)
1590c16b537SWarner Losh }
1600c16b537SWarner Losh #endif
1610c16b537SWarner Losh 
1620c16b537SWarner Losh #endif  /* ZSTDv0505_H */
163