Path: blob/main/sys/contrib/zstd/lib/legacy/zstd_v07.h
48378 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#ifndef ZSTDv07_H_23544611#define ZSTDv07_H_2354461213#if defined (__cplusplus)14extern "C" {15#endif1617/*====== Dependency ======*/18#include <stddef.h> /* size_t */192021/*====== Export for Windows ======*/22/*!23* ZSTDv07_DLL_EXPORT :24* Enable exporting of functions when building a Windows DLL25*/26#if defined(_WIN32) && defined(ZSTDv07_DLL_EXPORT) && (ZSTDv07_DLL_EXPORT==1)27# define ZSTDLIBv07_API __declspec(dllexport)28#else29# define ZSTDLIBv07_API30#endif313233/* *************************************34* Simple API35***************************************/36/*! ZSTDv07_getDecompressedSize() :37* @return : decompressed size if known, 0 otherwise.38note 1 : if `0`, follow up with ZSTDv07_getFrameParams() to know precise failure cause.39note 2 : decompressed size could be wrong or intentionally modified !40always ensure results fit within application's authorized limits */41unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize);4243/*! ZSTDv07_decompress() :44`compressedSize` : must be _exact_ size of compressed input, otherwise decompression will fail.45`dstCapacity` must be equal or larger than originalSize.46@return : the number of bytes decompressed into `dst` (<= `dstCapacity`),47or an errorCode if it fails (which can be tested using ZSTDv07_isError()) */48ZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity,49const void* src, size_t compressedSize);5051/**52ZSTDv07_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.7.x format53srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'54cSize (output parameter) : the number of bytes that would be read to decompress this frame55or an error code if it fails (which can be tested using ZSTDv01_isError())56dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame57or ZSTD_CONTENTSIZE_ERROR if an error occurs5859note : assumes `cSize` and `dBound` are _not_ NULL.60*/61void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize,62size_t* cSize, unsigned long long* dBound);6364/*====== Helper functions ======*/65ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */66ZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code); /*!< provides readable string from an error code */676869/*-*************************************70* Explicit memory management71***************************************/72/** Decompression context */73typedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx;74ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void);75ZSTDLIBv07_API size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx); /*!< @return : errorCode */7677/** ZSTDv07_decompressDCtx() :78* Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */79ZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);808182/*-************************83* Simple dictionary API84***************************/85/*! ZSTDv07_decompress_usingDict() :86* Decompression using a pre-defined Dictionary content (see dictBuilder).87* Dictionary must be identical to the one used during compression.88* Note : This function load the dictionary, resulting in a significant startup time */89ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx,90void* dst, size_t dstCapacity,91const void* src, size_t srcSize,92const void* dict,size_t dictSize);939495/*-**************************96* Advanced Dictionary API97****************************/98/*! ZSTDv07_createDDict() :99* Create a digested dictionary, ready to start decompression operation without startup delay.100* `dict` can be released after creation */101typedef struct ZSTDv07_DDict_s ZSTDv07_DDict;102ZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize);103ZSTDLIBv07_API size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict);104105/*! ZSTDv07_decompress_usingDDict() :106* Decompression using a pre-digested Dictionary107* Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */108ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx,109void* dst, size_t dstCapacity,110const void* src, size_t srcSize,111const ZSTDv07_DDict* ddict);112113typedef struct {114unsigned long long frameContentSize;115unsigned windowSize;116unsigned dictID;117unsigned checksumFlag;118} ZSTDv07_frameParams;119120ZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */121122123124125/* *************************************126* Streaming functions127***************************************/128typedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx;129ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void);130ZSTDLIBv07_API size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx);131132ZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx);133ZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize);134135ZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx,136void* dst, size_t* dstCapacityPtr,137const void* src, size_t* srcSizePtr);138139/*-***************************************************************************140* Streaming decompression howto141*142* A ZBUFFv07_DCtx object is required to track streaming operations.143* Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources.144* Use ZBUFFv07_decompressInit() to start a new decompression operation,145* or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary.146* Note that ZBUFFv07_DCtx objects can be re-init multiple times.147*148* Use ZBUFFv07_decompressContinue() repetitively to consume your input.149* *srcSizePtr and *dstCapacityPtr can be any size.150* The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.151* Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.152* The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.153* @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),154* or 0 when a frame is completely decoded,155* or an error code, which can be tested using ZBUFFv07_isError().156*157* Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize()158* output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.159* input : ZBUFFv07_recommendedDInSize == 128KB + 3;160* just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .161* *******************************************************************************/162163164/* *************************************165* Tool functions166***************************************/167ZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode);168ZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode);169170/** Functions below provide recommended buffer sizes for Compression or Decompression operations.171* These sizes are just hints, they tend to offer better latency */172ZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void);173ZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void);174175176/*-*************************************177* Constants178***************************************/179#define ZSTDv07_MAGICNUMBER 0xFD2FB527 /* v0.7 */180181182#if defined (__cplusplus)183}184#endif185186#endif /* ZSTDv07_H_235446 */187188189