xref: /freebsd/sys/contrib/zstd/lib/legacy/zstd_v04.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 ZSTD_V04_H_91868324769238
120c16b537SWarner Losh #define ZSTD_V04_H_91868324769238
130c16b537SWarner Losh 
140c16b537SWarner Losh #if defined (__cplusplus)
150c16b537SWarner Losh extern "C" {
160c16b537SWarner Losh #endif
170c16b537SWarner Losh 
180c16b537SWarner Losh /* *************************************
190c16b537SWarner Losh *  Includes
200c16b537SWarner Losh ***************************************/
210c16b537SWarner Losh #include <stddef.h>   /* size_t */
220c16b537SWarner Losh 
230c16b537SWarner Losh 
240c16b537SWarner Losh /* *************************************
250c16b537SWarner Losh *  Simple one-step function
260c16b537SWarner Losh ***************************************/
270c16b537SWarner Losh /**
280c16b537SWarner Losh ZSTDv04_decompress() : decompress ZSTD frames compliant with v0.4.x format
290c16b537SWarner Losh     compressedSize : is the exact source size
300c16b537SWarner Losh     maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.
310c16b537SWarner Losh                       It must be equal or larger than originalSize, otherwise decompression will fail.
320c16b537SWarner Losh     return : the number of bytes decompressed into destination buffer (originalSize)
330c16b537SWarner Losh              or an errorCode if it fails (which can be tested using ZSTDv01_isError())
340c16b537SWarner Losh */
350c16b537SWarner Losh size_t ZSTDv04_decompress( void* dst, size_t maxOriginalSize,
360c16b537SWarner Losh                      const void* src, size_t compressedSize);
370c16b537SWarner Losh 
380c16b537SWarner Losh  /**
392b9c00cbSConrad Meyer  ZSTDv04_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.4.x format
402b9c00cbSConrad Meyer      srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
412b9c00cbSConrad Meyer      cSize (output parameter)  : the number of bytes that would be read to decompress this frame
422b9c00cbSConrad Meyer                                  or an error code if it fails (which can be tested using ZSTDv01_isError())
432b9c00cbSConrad Meyer      dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
442b9c00cbSConrad Meyer                                  or ZSTD_CONTENTSIZE_ERROR if an error occurs
452b9c00cbSConrad Meyer 
462b9c00cbSConrad Meyer     note : assumes `cSize` and `dBound` are _not_ NULL.
470c16b537SWarner Losh  */
482b9c00cbSConrad Meyer  void ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
492b9c00cbSConrad Meyer                                       size_t* cSize, unsigned long long* dBound);
500c16b537SWarner Losh 
510c16b537SWarner Losh /**
520c16b537SWarner Losh ZSTDv04_isError() : tells if the result of ZSTDv04_decompress() is an error
530c16b537SWarner Losh */
540c16b537SWarner Losh unsigned ZSTDv04_isError(size_t code);
550c16b537SWarner Losh 
560c16b537SWarner Losh 
570c16b537SWarner Losh /* *************************************
580c16b537SWarner Losh *  Advanced functions
590c16b537SWarner Losh ***************************************/
600c16b537SWarner Losh typedef struct ZSTDv04_Dctx_s ZSTDv04_Dctx;
610c16b537SWarner Losh ZSTDv04_Dctx* ZSTDv04_createDCtx(void);
620c16b537SWarner Losh size_t ZSTDv04_freeDCtx(ZSTDv04_Dctx* dctx);
630c16b537SWarner Losh 
640c16b537SWarner Losh size_t ZSTDv04_decompressDCtx(ZSTDv04_Dctx* dctx,
650c16b537SWarner Losh                               void* dst, size_t maxOriginalSize,
660c16b537SWarner Losh                         const void* src, size_t compressedSize);
670c16b537SWarner Losh 
680c16b537SWarner Losh 
690c16b537SWarner Losh /* *************************************
700c16b537SWarner Losh *  Direct Streaming
710c16b537SWarner Losh ***************************************/
720c16b537SWarner Losh size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx);
730c16b537SWarner Losh 
740c16b537SWarner Losh size_t ZSTDv04_nextSrcSizeToDecompress(ZSTDv04_Dctx* dctx);
750c16b537SWarner Losh size_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);
760c16b537SWarner Losh /**
770c16b537SWarner Losh   Use above functions alternatively.
780c16b537SWarner Losh   ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
790c16b537SWarner Losh   ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
800c16b537SWarner Losh   Result is the number of bytes regenerated within 'dst'.
810c16b537SWarner Losh   It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
820c16b537SWarner Losh */
830c16b537SWarner Losh 
840c16b537SWarner Losh 
850c16b537SWarner Losh /* *************************************
860c16b537SWarner Losh *  Buffered Streaming
870c16b537SWarner Losh ***************************************/
880c16b537SWarner Losh typedef struct ZBUFFv04_DCtx_s ZBUFFv04_DCtx;
890c16b537SWarner Losh ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void);
900c16b537SWarner Losh size_t         ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx);
910c16b537SWarner Losh 
920c16b537SWarner Losh size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx);
930c16b537SWarner Losh size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* dict, size_t dictSize);
940c16b537SWarner Losh 
950c16b537SWarner Losh size_t ZBUFFv04_decompressContinue(ZBUFFv04_DCtx* dctx, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr);
960c16b537SWarner Losh 
970c16b537SWarner Losh /** ************************************************
980c16b537SWarner Losh *  Streaming decompression
990c16b537SWarner Losh *
1000c16b537SWarner Losh *  A ZBUFF_DCtx object is required to track streaming operation.
1010c16b537SWarner Losh *  Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
1020c16b537SWarner Losh *  Use ZBUFF_decompressInit() to start a new decompression operation.
1030c16b537SWarner Losh *  ZBUFF_DCtx objects can be reused multiple times.
1040c16b537SWarner Losh *
1050c16b537SWarner Losh *  Optionally, a reference to a static dictionary can be set, using ZBUFF_decompressWithDictionary()
1060c16b537SWarner Losh *  It must be the same content as the one set during compression phase.
1070c16b537SWarner Losh *  Dictionary content must remain accessible during the decompression process.
1080c16b537SWarner Losh *
1090c16b537SWarner Losh *  Use ZBUFF_decompressContinue() repetitively to consume your input.
1100c16b537SWarner Losh *  *srcSizePtr and *maxDstSizePtr can be any size.
1110c16b537SWarner Losh *  The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr.
1120c16b537SWarner Losh *  Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
1130c16b537SWarner Losh *  The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst.
1140c16b537SWarner Losh *  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
1150c16b537SWarner Losh *            or 0 when a frame is completely decoded
1160c16b537SWarner Losh *            or an error code, which can be tested using ZBUFF_isError().
1170c16b537SWarner Losh *
1180c16b537SWarner Losh *  Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize / ZBUFF_recommendedDOutSize
1190c16b537SWarner Losh *  output : ZBUFF_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when it's decoded.
1200c16b537SWarner Losh *  input : ZBUFF_recommendedDInSize==128Kb+3; just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
1210c16b537SWarner Losh * **************************************************/
1220c16b537SWarner Losh unsigned ZBUFFv04_isError(size_t errorCode);
1230c16b537SWarner Losh const char* ZBUFFv04_getErrorName(size_t errorCode);
1240c16b537SWarner Losh 
1250c16b537SWarner Losh 
1260c16b537SWarner Losh /** The below functions provide recommended buffer sizes for Compression or Decompression operations.
1270c16b537SWarner Losh *   These sizes are not compulsory, they just tend to offer better latency */
1280c16b537SWarner Losh size_t ZBUFFv04_recommendedDInSize(void);
1290c16b537SWarner Losh size_t ZBUFFv04_recommendedDOutSize(void);
1300c16b537SWarner Losh 
1310c16b537SWarner Losh 
1320c16b537SWarner Losh /* *************************************
1330c16b537SWarner Losh *  Prefix - version detection
1340c16b537SWarner Losh ***************************************/
1350c16b537SWarner Losh #define ZSTDv04_magicNumber 0xFD2FB524   /* v0.4 */
1360c16b537SWarner Losh 
1370c16b537SWarner Losh 
1380c16b537SWarner Losh #if defined (__cplusplus)
1390c16b537SWarner Losh }
1400c16b537SWarner Losh #endif
1410c16b537SWarner Losh 
1420c16b537SWarner Losh #endif /* ZSTD_V04_H_91868324769238 */
143