1*01826a49SYabin Cui /* 2*01826a49SYabin Cui * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates. 3*01826a49SYabin Cui * All rights reserved. 4*01826a49SYabin Cui * 5*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the 6*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree). 8*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses. 9*01826a49SYabin Cui */ 10*01826a49SYabin Cui 11*01826a49SYabin Cui #ifndef ZSTD_V04_H_91868324769238 12*01826a49SYabin Cui #define ZSTD_V04_H_91868324769238 13*01826a49SYabin Cui 14*01826a49SYabin Cui #if defined (__cplusplus) 15*01826a49SYabin Cui extern "C" { 16*01826a49SYabin Cui #endif 17*01826a49SYabin Cui 18*01826a49SYabin Cui /* ************************************* 19*01826a49SYabin Cui * Includes 20*01826a49SYabin Cui ***************************************/ 21*01826a49SYabin Cui #include <stddef.h> /* size_t */ 22*01826a49SYabin Cui 23*01826a49SYabin Cui 24*01826a49SYabin Cui /* ************************************* 25*01826a49SYabin Cui * Simple one-step function 26*01826a49SYabin Cui ***************************************/ 27*01826a49SYabin Cui /** 28*01826a49SYabin Cui ZSTDv04_decompress() : decompress ZSTD frames compliant with v0.4.x format 29*01826a49SYabin Cui compressedSize : is the exact source size 30*01826a49SYabin Cui maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated. 31*01826a49SYabin Cui It must be equal or larger than originalSize, otherwise decompression will fail. 32*01826a49SYabin Cui return : the number of bytes decompressed into destination buffer (originalSize) 33*01826a49SYabin Cui or an errorCode if it fails (which can be tested using ZSTDv01_isError()) 34*01826a49SYabin Cui */ 35*01826a49SYabin Cui size_t ZSTDv04_decompress( void* dst, size_t maxOriginalSize, 36*01826a49SYabin Cui const void* src, size_t compressedSize); 37*01826a49SYabin Cui 38*01826a49SYabin Cui /** 39*01826a49SYabin Cui ZSTDv04_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.4.x format 40*01826a49SYabin Cui srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src' 41*01826a49SYabin Cui cSize (output parameter) : the number of bytes that would be read to decompress this frame 42*01826a49SYabin Cui or an error code if it fails (which can be tested using ZSTDv01_isError()) 43*01826a49SYabin Cui dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame 44*01826a49SYabin Cui or ZSTD_CONTENTSIZE_ERROR if an error occurs 45*01826a49SYabin Cui 46*01826a49SYabin Cui note : assumes `cSize` and `dBound` are _not_ NULL. 47*01826a49SYabin Cui */ 48*01826a49SYabin Cui void ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize, 49*01826a49SYabin Cui size_t* cSize, unsigned long long* dBound); 50*01826a49SYabin Cui 51*01826a49SYabin Cui /** 52*01826a49SYabin Cui ZSTDv04_isError() : tells if the result of ZSTDv04_decompress() is an error 53*01826a49SYabin Cui */ 54*01826a49SYabin Cui unsigned ZSTDv04_isError(size_t code); 55*01826a49SYabin Cui 56*01826a49SYabin Cui 57*01826a49SYabin Cui /* ************************************* 58*01826a49SYabin Cui * Advanced functions 59*01826a49SYabin Cui ***************************************/ 60*01826a49SYabin Cui typedef struct ZSTDv04_Dctx_s ZSTDv04_Dctx; 61*01826a49SYabin Cui ZSTDv04_Dctx* ZSTDv04_createDCtx(void); 62*01826a49SYabin Cui size_t ZSTDv04_freeDCtx(ZSTDv04_Dctx* dctx); 63*01826a49SYabin Cui 64*01826a49SYabin Cui size_t ZSTDv04_decompressDCtx(ZSTDv04_Dctx* dctx, 65*01826a49SYabin Cui void* dst, size_t maxOriginalSize, 66*01826a49SYabin Cui const void* src, size_t compressedSize); 67*01826a49SYabin Cui 68*01826a49SYabin Cui 69*01826a49SYabin Cui /* ************************************* 70*01826a49SYabin Cui * Direct Streaming 71*01826a49SYabin Cui ***************************************/ 72*01826a49SYabin Cui size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx); 73*01826a49SYabin Cui 74*01826a49SYabin Cui size_t ZSTDv04_nextSrcSizeToDecompress(ZSTDv04_Dctx* dctx); 75*01826a49SYabin Cui size_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize); 76*01826a49SYabin Cui /** 77*01826a49SYabin Cui Use above functions alternatively. 78*01826a49SYabin Cui ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 79*01826a49SYabin Cui ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block. 80*01826a49SYabin Cui Result is the number of bytes regenerated within 'dst'. 81*01826a49SYabin Cui It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header. 82*01826a49SYabin Cui */ 83*01826a49SYabin Cui 84*01826a49SYabin Cui 85*01826a49SYabin Cui /* ************************************* 86*01826a49SYabin Cui * Buffered Streaming 87*01826a49SYabin Cui ***************************************/ 88*01826a49SYabin Cui typedef struct ZBUFFv04_DCtx_s ZBUFFv04_DCtx; 89*01826a49SYabin Cui ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void); 90*01826a49SYabin Cui size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx); 91*01826a49SYabin Cui 92*01826a49SYabin Cui size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx); 93*01826a49SYabin Cui size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* dict, size_t dictSize); 94*01826a49SYabin Cui 95*01826a49SYabin Cui size_t ZBUFFv04_decompressContinue(ZBUFFv04_DCtx* dctx, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr); 96*01826a49SYabin Cui 97*01826a49SYabin Cui /** ************************************************ 98*01826a49SYabin Cui * Streaming decompression 99*01826a49SYabin Cui * 100*01826a49SYabin Cui * A ZBUFF_DCtx object is required to track streaming operation. 101*01826a49SYabin Cui * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources. 102*01826a49SYabin Cui * Use ZBUFF_decompressInit() to start a new decompression operation. 103*01826a49SYabin Cui * ZBUFF_DCtx objects can be reused multiple times. 104*01826a49SYabin Cui * 105*01826a49SYabin Cui * Optionally, a reference to a static dictionary can be set, using ZBUFF_decompressWithDictionary() 106*01826a49SYabin Cui * It must be the same content as the one set during compression phase. 107*01826a49SYabin Cui * Dictionary content must remain accessible during the decompression process. 108*01826a49SYabin Cui * 109*01826a49SYabin Cui * Use ZBUFF_decompressContinue() repetitively to consume your input. 110*01826a49SYabin Cui * *srcSizePtr and *maxDstSizePtr can be any size. 111*01826a49SYabin Cui * The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr. 112*01826a49SYabin Cui * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again. 113*01826a49SYabin Cui * The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst. 114*01826a49SYabin Cui * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency) 115*01826a49SYabin Cui * or 0 when a frame is completely decoded 116*01826a49SYabin Cui * or an error code, which can be tested using ZBUFF_isError(). 117*01826a49SYabin Cui * 118*01826a49SYabin Cui * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize / ZBUFF_recommendedDOutSize 119*01826a49SYabin Cui * 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. 120*01826a49SYabin Cui * input : ZBUFF_recommendedDInSize==128Kb+3; just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . 121*01826a49SYabin Cui * **************************************************/ 122*01826a49SYabin Cui unsigned ZBUFFv04_isError(size_t errorCode); 123*01826a49SYabin Cui const char* ZBUFFv04_getErrorName(size_t errorCode); 124*01826a49SYabin Cui 125*01826a49SYabin Cui 126*01826a49SYabin Cui /** The below functions provide recommended buffer sizes for Compression or Decompression operations. 127*01826a49SYabin Cui * These sizes are not compulsory, they just tend to offer better latency */ 128*01826a49SYabin Cui size_t ZBUFFv04_recommendedDInSize(void); 129*01826a49SYabin Cui size_t ZBUFFv04_recommendedDOutSize(void); 130*01826a49SYabin Cui 131*01826a49SYabin Cui 132*01826a49SYabin Cui /* ************************************* 133*01826a49SYabin Cui * Prefix - version detection 134*01826a49SYabin Cui ***************************************/ 135*01826a49SYabin Cui #define ZSTDv04_magicNumber 0xFD2FB524 /* v0.4 */ 136*01826a49SYabin Cui 137*01826a49SYabin Cui 138*01826a49SYabin Cui #if defined (__cplusplus) 139*01826a49SYabin Cui } 140*01826a49SYabin Cui #endif 141*01826a49SYabin Cui 142*01826a49SYabin Cui #endif /* ZSTD_V04_H_91868324769238 */ 143