1*27162e4eSAndroid Build Coastguard Worker /* 2*27162e4eSAndroid Build Coastguard Worker * LZ4 - Fast LZ compression algorithm 3*27162e4eSAndroid Build Coastguard Worker * Header File 4*27162e4eSAndroid Build Coastguard Worker * Copyright (C) 2011-2023, Yann Collet. 5*27162e4eSAndroid Build Coastguard Worker 6*27162e4eSAndroid Build Coastguard Worker BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 7*27162e4eSAndroid Build Coastguard Worker 8*27162e4eSAndroid Build Coastguard Worker Redistribution and use in source and binary forms, with or without 9*27162e4eSAndroid Build Coastguard Worker modification, are permitted provided that the following conditions are 10*27162e4eSAndroid Build Coastguard Worker met: 11*27162e4eSAndroid Build Coastguard Worker 12*27162e4eSAndroid Build Coastguard Worker * Redistributions of source code must retain the above copyright 13*27162e4eSAndroid Build Coastguard Worker notice, this list of conditions and the following disclaimer. 14*27162e4eSAndroid Build Coastguard Worker * Redistributions in binary form must reproduce the above 15*27162e4eSAndroid Build Coastguard Worker copyright notice, this list of conditions and the following disclaimer 16*27162e4eSAndroid Build Coastguard Worker in the documentation and/or other materials provided with the 17*27162e4eSAndroid Build Coastguard Worker distribution. 18*27162e4eSAndroid Build Coastguard Worker 19*27162e4eSAndroid Build Coastguard Worker THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20*27162e4eSAndroid Build Coastguard Worker "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21*27162e4eSAndroid Build Coastguard Worker LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22*27162e4eSAndroid Build Coastguard Worker A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23*27162e4eSAndroid Build Coastguard Worker OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24*27162e4eSAndroid Build Coastguard Worker SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25*27162e4eSAndroid Build Coastguard Worker LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26*27162e4eSAndroid Build Coastguard Worker DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27*27162e4eSAndroid Build Coastguard Worker THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28*27162e4eSAndroid Build Coastguard Worker (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29*27162e4eSAndroid Build Coastguard Worker OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30*27162e4eSAndroid Build Coastguard Worker 31*27162e4eSAndroid Build Coastguard Worker You can contact the author at : 32*27162e4eSAndroid Build Coastguard Worker - LZ4 homepage : http://www.lz4.org 33*27162e4eSAndroid Build Coastguard Worker - LZ4 source repository : https://github.com/lz4/lz4 34*27162e4eSAndroid Build Coastguard Worker */ 35*27162e4eSAndroid Build Coastguard Worker #if defined (__cplusplus) 36*27162e4eSAndroid Build Coastguard Worker extern "C" { 37*27162e4eSAndroid Build Coastguard Worker #endif 38*27162e4eSAndroid Build Coastguard Worker 39*27162e4eSAndroid Build Coastguard Worker #ifndef LZ4_H_2983827168210 40*27162e4eSAndroid Build Coastguard Worker #define LZ4_H_2983827168210 41*27162e4eSAndroid Build Coastguard Worker 42*27162e4eSAndroid Build Coastguard Worker /* --- Dependency --- */ 43*27162e4eSAndroid Build Coastguard Worker #include <stddef.h> /* size_t */ 44*27162e4eSAndroid Build Coastguard Worker 45*27162e4eSAndroid Build Coastguard Worker 46*27162e4eSAndroid Build Coastguard Worker /** 47*27162e4eSAndroid Build Coastguard Worker Introduction 48*27162e4eSAndroid Build Coastguard Worker 49*27162e4eSAndroid Build Coastguard Worker LZ4 is lossless compression algorithm, providing compression speed >500 MB/s per core, 50*27162e4eSAndroid Build Coastguard Worker scalable with multi-cores CPU. It features an extremely fast decoder, with speed in 51*27162e4eSAndroid Build Coastguard Worker multiple GB/s per core, typically reaching RAM speed limits on multi-core systems. 52*27162e4eSAndroid Build Coastguard Worker 53*27162e4eSAndroid Build Coastguard Worker The LZ4 compression library provides in-memory compression and decompression functions. 54*27162e4eSAndroid Build Coastguard Worker It gives full buffer control to user. 55*27162e4eSAndroid Build Coastguard Worker Compression can be done in: 56*27162e4eSAndroid Build Coastguard Worker - a single step (described as Simple Functions) 57*27162e4eSAndroid Build Coastguard Worker - a single step, reusing a context (described in Advanced Functions) 58*27162e4eSAndroid Build Coastguard Worker - unbounded multiple steps (described as Streaming compression) 59*27162e4eSAndroid Build Coastguard Worker 60*27162e4eSAndroid Build Coastguard Worker lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md). 61*27162e4eSAndroid Build Coastguard Worker Decompressing such a compressed block requires additional metadata. 62*27162e4eSAndroid Build Coastguard Worker Exact metadata depends on exact decompression function. 63*27162e4eSAndroid Build Coastguard Worker For the typical case of LZ4_decompress_safe(), 64*27162e4eSAndroid Build Coastguard Worker metadata includes block's compressed size, and maximum bound of decompressed size. 65*27162e4eSAndroid Build Coastguard Worker Each application is free to encode and pass such metadata in whichever way it wants. 66*27162e4eSAndroid Build Coastguard Worker 67*27162e4eSAndroid Build Coastguard Worker lz4.h only handle blocks, it can not generate Frames. 68*27162e4eSAndroid Build Coastguard Worker 69*27162e4eSAndroid Build Coastguard Worker Blocks are different from Frames (doc/lz4_Frame_format.md). 70*27162e4eSAndroid Build Coastguard Worker Frames bundle both blocks and metadata in a specified manner. 71*27162e4eSAndroid Build Coastguard Worker Embedding metadata is required for compressed data to be self-contained and portable. 72*27162e4eSAndroid Build Coastguard Worker Frame format is delivered through a companion API, declared in lz4frame.h. 73*27162e4eSAndroid Build Coastguard Worker The `lz4` CLI can only manage frames. 74*27162e4eSAndroid Build Coastguard Worker */ 75*27162e4eSAndroid Build Coastguard Worker 76*27162e4eSAndroid Build Coastguard Worker /*^*************************************************************** 77*27162e4eSAndroid Build Coastguard Worker * Export parameters 78*27162e4eSAndroid Build Coastguard Worker *****************************************************************/ 79*27162e4eSAndroid Build Coastguard Worker /* 80*27162e4eSAndroid Build Coastguard Worker * LZ4_DLL_EXPORT : 81*27162e4eSAndroid Build Coastguard Worker * Enable exporting of functions when building a Windows DLL 82*27162e4eSAndroid Build Coastguard Worker * LZ4LIB_VISIBILITY : 83*27162e4eSAndroid Build Coastguard Worker * Control library symbols visibility. 84*27162e4eSAndroid Build Coastguard Worker */ 85*27162e4eSAndroid Build Coastguard Worker #ifndef LZ4LIB_VISIBILITY 86*27162e4eSAndroid Build Coastguard Worker # if defined(__GNUC__) && (__GNUC__ >= 4) 87*27162e4eSAndroid Build Coastguard Worker # define LZ4LIB_VISIBILITY __attribute__ ((visibility ("default"))) 88*27162e4eSAndroid Build Coastguard Worker # else 89*27162e4eSAndroid Build Coastguard Worker # define LZ4LIB_VISIBILITY 90*27162e4eSAndroid Build Coastguard Worker # endif 91*27162e4eSAndroid Build Coastguard Worker #endif 92*27162e4eSAndroid Build Coastguard Worker #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1) 93*27162e4eSAndroid Build Coastguard Worker # define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY 94*27162e4eSAndroid Build Coastguard Worker #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1) 95*27162e4eSAndroid Build Coastguard Worker # define LZ4LIB_API __declspec(dllimport) LZ4LIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ 96*27162e4eSAndroid Build Coastguard Worker #else 97*27162e4eSAndroid Build Coastguard Worker # define LZ4LIB_API LZ4LIB_VISIBILITY 98*27162e4eSAndroid Build Coastguard Worker #endif 99*27162e4eSAndroid Build Coastguard Worker 100*27162e4eSAndroid Build Coastguard Worker /*! LZ4_FREESTANDING : 101*27162e4eSAndroid Build Coastguard Worker * When this macro is set to 1, it enables "freestanding mode" that is 102*27162e4eSAndroid Build Coastguard Worker * suitable for typical freestanding environment which doesn't support 103*27162e4eSAndroid Build Coastguard Worker * standard C library. 104*27162e4eSAndroid Build Coastguard Worker * 105*27162e4eSAndroid Build Coastguard Worker * - LZ4_FREESTANDING is a compile-time switch. 106*27162e4eSAndroid Build Coastguard Worker * - It requires the following macros to be defined: 107*27162e4eSAndroid Build Coastguard Worker * LZ4_memcpy, LZ4_memmove, LZ4_memset. 108*27162e4eSAndroid Build Coastguard Worker * - It only enables LZ4/HC functions which don't use heap. 109*27162e4eSAndroid Build Coastguard Worker * All LZ4F_* functions are not supported. 110*27162e4eSAndroid Build Coastguard Worker * - See tests/freestanding.c to check its basic setup. 111*27162e4eSAndroid Build Coastguard Worker */ 112*27162e4eSAndroid Build Coastguard Worker #if defined(LZ4_FREESTANDING) && (LZ4_FREESTANDING == 1) 113*27162e4eSAndroid Build Coastguard Worker # define LZ4_HEAPMODE 0 114*27162e4eSAndroid Build Coastguard Worker # define LZ4HC_HEAPMODE 0 115*27162e4eSAndroid Build Coastguard Worker # define LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION 1 116*27162e4eSAndroid Build Coastguard Worker # if !defined(LZ4_memcpy) 117*27162e4eSAndroid Build Coastguard Worker # error "LZ4_FREESTANDING requires macro 'LZ4_memcpy'." 118*27162e4eSAndroid Build Coastguard Worker # endif 119*27162e4eSAndroid Build Coastguard Worker # if !defined(LZ4_memset) 120*27162e4eSAndroid Build Coastguard Worker # error "LZ4_FREESTANDING requires macro 'LZ4_memset'." 121*27162e4eSAndroid Build Coastguard Worker # endif 122*27162e4eSAndroid Build Coastguard Worker # if !defined(LZ4_memmove) 123*27162e4eSAndroid Build Coastguard Worker # error "LZ4_FREESTANDING requires macro 'LZ4_memmove'." 124*27162e4eSAndroid Build Coastguard Worker # endif 125*27162e4eSAndroid Build Coastguard Worker #elif ! defined(LZ4_FREESTANDING) 126*27162e4eSAndroid Build Coastguard Worker # define LZ4_FREESTANDING 0 127*27162e4eSAndroid Build Coastguard Worker #endif 128*27162e4eSAndroid Build Coastguard Worker 129*27162e4eSAndroid Build Coastguard Worker 130*27162e4eSAndroid Build Coastguard Worker /*------ Version ------*/ 131*27162e4eSAndroid Build Coastguard Worker #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ 132*27162e4eSAndroid Build Coastguard Worker #define LZ4_VERSION_MINOR 10 /* for new (non-breaking) interface capabilities */ 133*27162e4eSAndroid Build Coastguard Worker #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */ 134*27162e4eSAndroid Build Coastguard Worker 135*27162e4eSAndroid Build Coastguard Worker #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) 136*27162e4eSAndroid Build Coastguard Worker 137*27162e4eSAndroid Build Coastguard Worker #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE 138*27162e4eSAndroid Build Coastguard Worker #define LZ4_QUOTE(str) #str 139*27162e4eSAndroid Build Coastguard Worker #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str) 140*27162e4eSAndroid Build Coastguard Worker #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) /* requires v1.7.3+ */ 141*27162e4eSAndroid Build Coastguard Worker 142*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version; requires v1.3.0+ */ 143*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version; requires v1.7.5+ */ 144*27162e4eSAndroid Build Coastguard Worker 145*27162e4eSAndroid Build Coastguard Worker 146*27162e4eSAndroid Build Coastguard Worker /*-************************************ 147*27162e4eSAndroid Build Coastguard Worker * Tuning memory usage 148*27162e4eSAndroid Build Coastguard Worker **************************************/ 149*27162e4eSAndroid Build Coastguard Worker /*! 150*27162e4eSAndroid Build Coastguard Worker * LZ4_MEMORY_USAGE : 151*27162e4eSAndroid Build Coastguard Worker * Can be selected at compile time, by setting LZ4_MEMORY_USAGE. 152*27162e4eSAndroid Build Coastguard Worker * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB) 153*27162e4eSAndroid Build Coastguard Worker * Increasing memory usage improves compression ratio, generally at the cost of speed. 154*27162e4eSAndroid Build Coastguard Worker * Reduced memory usage may improve speed at the cost of ratio, thanks to better cache locality. 155*27162e4eSAndroid Build Coastguard Worker * Default value is 14, for 16KB, which nicely fits into most L1 caches. 156*27162e4eSAndroid Build Coastguard Worker */ 157*27162e4eSAndroid Build Coastguard Worker #ifndef LZ4_MEMORY_USAGE 158*27162e4eSAndroid Build Coastguard Worker # define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT 159*27162e4eSAndroid Build Coastguard Worker #endif 160*27162e4eSAndroid Build Coastguard Worker 161*27162e4eSAndroid Build Coastguard Worker /* These are absolute limits, they should not be changed by users */ 162*27162e4eSAndroid Build Coastguard Worker #define LZ4_MEMORY_USAGE_MIN 10 163*27162e4eSAndroid Build Coastguard Worker #define LZ4_MEMORY_USAGE_DEFAULT 14 164*27162e4eSAndroid Build Coastguard Worker #define LZ4_MEMORY_USAGE_MAX 20 165*27162e4eSAndroid Build Coastguard Worker 166*27162e4eSAndroid Build Coastguard Worker #if (LZ4_MEMORY_USAGE < LZ4_MEMORY_USAGE_MIN) 167*27162e4eSAndroid Build Coastguard Worker # error "LZ4_MEMORY_USAGE is too small !" 168*27162e4eSAndroid Build Coastguard Worker #endif 169*27162e4eSAndroid Build Coastguard Worker 170*27162e4eSAndroid Build Coastguard Worker #if (LZ4_MEMORY_USAGE > LZ4_MEMORY_USAGE_MAX) 171*27162e4eSAndroid Build Coastguard Worker # error "LZ4_MEMORY_USAGE is too large !" 172*27162e4eSAndroid Build Coastguard Worker #endif 173*27162e4eSAndroid Build Coastguard Worker 174*27162e4eSAndroid Build Coastguard Worker /*-************************************ 175*27162e4eSAndroid Build Coastguard Worker * Simple Functions 176*27162e4eSAndroid Build Coastguard Worker **************************************/ 177*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compress_default() : 178*27162e4eSAndroid Build Coastguard Worker * Compresses 'srcSize' bytes from buffer 'src' 179*27162e4eSAndroid Build Coastguard Worker * into already allocated 'dst' buffer of size 'dstCapacity'. 180*27162e4eSAndroid Build Coastguard Worker * Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize). 181*27162e4eSAndroid Build Coastguard Worker * It also runs faster, so it's a recommended setting. 182*27162e4eSAndroid Build Coastguard Worker * If the function cannot compress 'src' into a more limited 'dst' budget, 183*27162e4eSAndroid Build Coastguard Worker * compression stops *immediately*, and the function result is zero. 184*27162e4eSAndroid Build Coastguard Worker * In which case, 'dst' content is undefined (invalid). 185*27162e4eSAndroid Build Coastguard Worker * srcSize : max supported value is LZ4_MAX_INPUT_SIZE. 186*27162e4eSAndroid Build Coastguard Worker * dstCapacity : size of buffer 'dst' (which must be already allocated) 187*27162e4eSAndroid Build Coastguard Worker * @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity) 188*27162e4eSAndroid Build Coastguard Worker * or 0 if compression fails 189*27162e4eSAndroid Build Coastguard Worker * Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer). 190*27162e4eSAndroid Build Coastguard Worker */ 191*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity); 192*27162e4eSAndroid Build Coastguard Worker 193*27162e4eSAndroid Build Coastguard Worker /*! LZ4_decompress_safe() : 194*27162e4eSAndroid Build Coastguard Worker * @compressedSize : is the exact complete size of the compressed block. 195*27162e4eSAndroid Build Coastguard Worker * @dstCapacity : is the size of destination buffer (which must be already allocated), 196*27162e4eSAndroid Build Coastguard Worker * presumed an upper bound of decompressed size. 197*27162e4eSAndroid Build Coastguard Worker * @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity) 198*27162e4eSAndroid Build Coastguard Worker * If destination buffer is not large enough, decoding will stop and output an error code (negative value). 199*27162e4eSAndroid Build Coastguard Worker * If the source stream is detected malformed, the function will stop decoding and return a negative result. 200*27162e4eSAndroid Build Coastguard Worker * Note 1 : This function is protected against malicious data packets : 201*27162e4eSAndroid Build Coastguard Worker * it will never writes outside 'dst' buffer, nor read outside 'source' buffer, 202*27162e4eSAndroid Build Coastguard Worker * even if the compressed block is maliciously modified to order the decoder to do these actions. 203*27162e4eSAndroid Build Coastguard Worker * In such case, the decoder stops immediately, and considers the compressed block malformed. 204*27162e4eSAndroid Build Coastguard Worker * Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them. 205*27162e4eSAndroid Build Coastguard Worker * The implementation is free to send / store / derive this information in whichever way is most beneficial. 206*27162e4eSAndroid Build Coastguard Worker * If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead. 207*27162e4eSAndroid Build Coastguard Worker */ 208*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity); 209*27162e4eSAndroid Build Coastguard Worker 210*27162e4eSAndroid Build Coastguard Worker 211*27162e4eSAndroid Build Coastguard Worker /*-************************************ 212*27162e4eSAndroid Build Coastguard Worker * Advanced Functions 213*27162e4eSAndroid Build Coastguard Worker **************************************/ 214*27162e4eSAndroid Build Coastguard Worker #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ 215*27162e4eSAndroid Build Coastguard Worker #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) 216*27162e4eSAndroid Build Coastguard Worker 217*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compressBound() : 218*27162e4eSAndroid Build Coastguard Worker Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) 219*27162e4eSAndroid Build Coastguard Worker This function is primarily useful for memory allocation purposes (destination buffer size). 220*27162e4eSAndroid Build Coastguard Worker Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). 221*27162e4eSAndroid Build Coastguard Worker Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize) 222*27162e4eSAndroid Build Coastguard Worker inputSize : max supported value is LZ4_MAX_INPUT_SIZE 223*27162e4eSAndroid Build Coastguard Worker return : maximum output size in a "worst case" scenario 224*27162e4eSAndroid Build Coastguard Worker or 0, if input size is incorrect (too large or negative) 225*27162e4eSAndroid Build Coastguard Worker */ 226*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_compressBound(int inputSize); 227*27162e4eSAndroid Build Coastguard Worker 228*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compress_fast() : 229*27162e4eSAndroid Build Coastguard Worker Same as LZ4_compress_default(), but allows selection of "acceleration" factor. 230*27162e4eSAndroid Build Coastguard Worker The larger the acceleration value, the faster the algorithm, but also the lesser the compression. 231*27162e4eSAndroid Build Coastguard Worker It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. 232*27162e4eSAndroid Build Coastguard Worker An acceleration value of "1" is the same as regular LZ4_compress_default() 233*27162e4eSAndroid Build Coastguard Worker Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c). 234*27162e4eSAndroid Build Coastguard Worker Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c). 235*27162e4eSAndroid Build Coastguard Worker */ 236*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); 237*27162e4eSAndroid Build Coastguard Worker 238*27162e4eSAndroid Build Coastguard Worker 239*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compress_fast_extState() : 240*27162e4eSAndroid Build Coastguard Worker * Same as LZ4_compress_fast(), using an externally allocated memory space for its state. 241*27162e4eSAndroid Build Coastguard Worker * Use LZ4_sizeofState() to know how much memory must be allocated, 242*27162e4eSAndroid Build Coastguard Worker * and allocate it on 8-bytes boundaries (using `malloc()` typically). 243*27162e4eSAndroid Build Coastguard Worker * Then, provide this buffer as `void* state` to compression function. 244*27162e4eSAndroid Build Coastguard Worker */ 245*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_sizeofState(void); 246*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); 247*27162e4eSAndroid Build Coastguard Worker 248*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compress_destSize() : 249*27162e4eSAndroid Build Coastguard Worker * Reverse the logic : compresses as much data as possible from 'src' buffer 250*27162e4eSAndroid Build Coastguard Worker * into already allocated buffer 'dst', of size >= 'dstCapacity'. 251*27162e4eSAndroid Build Coastguard Worker * This function either compresses the entire 'src' content into 'dst' if it's large enough, 252*27162e4eSAndroid Build Coastguard Worker * or fill 'dst' buffer completely with as much data as possible from 'src'. 253*27162e4eSAndroid Build Coastguard Worker * note: acceleration parameter is fixed to "default". 254*27162e4eSAndroid Build Coastguard Worker * 255*27162e4eSAndroid Build Coastguard Worker * *srcSizePtr : in+out parameter. Initially contains size of input. 256*27162e4eSAndroid Build Coastguard Worker * Will be modified to indicate how many bytes where read from 'src' to fill 'dst'. 257*27162e4eSAndroid Build Coastguard Worker * New value is necessarily <= input value. 258*27162e4eSAndroid Build Coastguard Worker * @return : Nb bytes written into 'dst' (necessarily <= dstCapacity) 259*27162e4eSAndroid Build Coastguard Worker * or 0 if compression fails. 260*27162e4eSAndroid Build Coastguard Worker * 261*27162e4eSAndroid Build Coastguard Worker * Note : from v1.8.2 to v1.9.1, this function had a bug (fixed in v1.9.2+): 262*27162e4eSAndroid Build Coastguard Worker * the produced compressed content could, in specific circumstances, 263*27162e4eSAndroid Build Coastguard Worker * require to be decompressed into a destination buffer larger 264*27162e4eSAndroid Build Coastguard Worker * by at least 1 byte than the content to decompress. 265*27162e4eSAndroid Build Coastguard Worker * If an application uses `LZ4_compress_destSize()`, 266*27162e4eSAndroid Build Coastguard Worker * it's highly recommended to update liblz4 to v1.9.2 or better. 267*27162e4eSAndroid Build Coastguard Worker * If this can't be done or ensured, 268*27162e4eSAndroid Build Coastguard Worker * the receiving decompression function should provide 269*27162e4eSAndroid Build Coastguard Worker * a dstCapacity which is > decompressedSize, by at least 1 byte. 270*27162e4eSAndroid Build Coastguard Worker * See https://github.com/lz4/lz4/issues/859 for details 271*27162e4eSAndroid Build Coastguard Worker */ 272*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize); 273*27162e4eSAndroid Build Coastguard Worker 274*27162e4eSAndroid Build Coastguard Worker /*! LZ4_decompress_safe_partial() : 275*27162e4eSAndroid Build Coastguard Worker * Decompress an LZ4 compressed block, of size 'srcSize' at position 'src', 276*27162e4eSAndroid Build Coastguard Worker * into destination buffer 'dst' of size 'dstCapacity'. 277*27162e4eSAndroid Build Coastguard Worker * Up to 'targetOutputSize' bytes will be decoded. 278*27162e4eSAndroid Build Coastguard Worker * The function stops decoding on reaching this objective. 279*27162e4eSAndroid Build Coastguard Worker * This can be useful to boost performance 280*27162e4eSAndroid Build Coastguard Worker * whenever only the beginning of a block is required. 281*27162e4eSAndroid Build Coastguard Worker * 282*27162e4eSAndroid Build Coastguard Worker * @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize) 283*27162e4eSAndroid Build Coastguard Worker * If source stream is detected malformed, function returns a negative result. 284*27162e4eSAndroid Build Coastguard Worker * 285*27162e4eSAndroid Build Coastguard Worker * Note 1 : @return can be < targetOutputSize, if compressed block contains less data. 286*27162e4eSAndroid Build Coastguard Worker * 287*27162e4eSAndroid Build Coastguard Worker * Note 2 : targetOutputSize must be <= dstCapacity 288*27162e4eSAndroid Build Coastguard Worker * 289*27162e4eSAndroid Build Coastguard Worker * Note 3 : this function effectively stops decoding on reaching targetOutputSize, 290*27162e4eSAndroid Build Coastguard Worker * so dstCapacity is kind of redundant. 291*27162e4eSAndroid Build Coastguard Worker * This is because in older versions of this function, 292*27162e4eSAndroid Build Coastguard Worker * decoding operation would still write complete sequences. 293*27162e4eSAndroid Build Coastguard Worker * Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize, 294*27162e4eSAndroid Build Coastguard Worker * it could write more bytes, though only up to dstCapacity. 295*27162e4eSAndroid Build Coastguard Worker * Some "margin" used to be required for this operation to work properly. 296*27162e4eSAndroid Build Coastguard Worker * Thankfully, this is no longer necessary. 297*27162e4eSAndroid Build Coastguard Worker * The function nonetheless keeps the same signature, in an effort to preserve API compatibility. 298*27162e4eSAndroid Build Coastguard Worker * 299*27162e4eSAndroid Build Coastguard Worker * Note 4 : If srcSize is the exact size of the block, 300*27162e4eSAndroid Build Coastguard Worker * then targetOutputSize can be any value, 301*27162e4eSAndroid Build Coastguard Worker * including larger than the block's decompressed size. 302*27162e4eSAndroid Build Coastguard Worker * The function will, at most, generate block's decompressed size. 303*27162e4eSAndroid Build Coastguard Worker * 304*27162e4eSAndroid Build Coastguard Worker * Note 5 : If srcSize is _larger_ than block's compressed size, 305*27162e4eSAndroid Build Coastguard Worker * then targetOutputSize **MUST** be <= block's decompressed size. 306*27162e4eSAndroid Build Coastguard Worker * Otherwise, *silent corruption will occur*. 307*27162e4eSAndroid Build Coastguard Worker */ 308*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity); 309*27162e4eSAndroid Build Coastguard Worker 310*27162e4eSAndroid Build Coastguard Worker 311*27162e4eSAndroid Build Coastguard Worker /*-********************************************* 312*27162e4eSAndroid Build Coastguard Worker * Streaming Compression Functions 313*27162e4eSAndroid Build Coastguard Worker ***********************************************/ 314*27162e4eSAndroid Build Coastguard Worker typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */ 315*27162e4eSAndroid Build Coastguard Worker 316*27162e4eSAndroid Build Coastguard Worker /*! 317*27162e4eSAndroid Build Coastguard Worker Note about RC_INVOKED 318*27162e4eSAndroid Build Coastguard Worker 319*27162e4eSAndroid Build Coastguard Worker - RC_INVOKED is predefined symbol of rc.exe (the resource compiler which is part of MSVC/Visual Studio). 320*27162e4eSAndroid Build Coastguard Worker https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros 321*27162e4eSAndroid Build Coastguard Worker 322*27162e4eSAndroid Build Coastguard Worker - Since rc.exe is a legacy compiler, it truncates long symbol (> 30 chars) 323*27162e4eSAndroid Build Coastguard Worker and reports warning "RC4011: identifier truncated". 324*27162e4eSAndroid Build Coastguard Worker 325*27162e4eSAndroid Build Coastguard Worker - To eliminate the warning, we surround long preprocessor symbol with 326*27162e4eSAndroid Build Coastguard Worker "#if !defined(RC_INVOKED) ... #endif" block that means 327*27162e4eSAndroid Build Coastguard Worker "skip this block when rc.exe is trying to read it". 328*27162e4eSAndroid Build Coastguard Worker */ 329*27162e4eSAndroid Build Coastguard Worker #if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */ 330*27162e4eSAndroid Build Coastguard Worker #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) 331*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API LZ4_stream_t* LZ4_createStream(void); 332*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr); 333*27162e4eSAndroid Build Coastguard Worker #endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ 334*27162e4eSAndroid Build Coastguard Worker #endif 335*27162e4eSAndroid Build Coastguard Worker 336*27162e4eSAndroid Build Coastguard Worker /*! LZ4_resetStream_fast() : v1.9.0+ 337*27162e4eSAndroid Build Coastguard Worker * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks 338*27162e4eSAndroid Build Coastguard Worker * (e.g., LZ4_compress_fast_continue()). 339*27162e4eSAndroid Build Coastguard Worker * 340*27162e4eSAndroid Build Coastguard Worker * An LZ4_stream_t must be initialized once before usage. 341*27162e4eSAndroid Build Coastguard Worker * This is automatically done when created by LZ4_createStream(). 342*27162e4eSAndroid Build Coastguard Worker * However, should the LZ4_stream_t be simply declared on stack (for example), 343*27162e4eSAndroid Build Coastguard Worker * it's necessary to initialize it first, using LZ4_initStream(). 344*27162e4eSAndroid Build Coastguard Worker * 345*27162e4eSAndroid Build Coastguard Worker * After init, start any new stream with LZ4_resetStream_fast(). 346*27162e4eSAndroid Build Coastguard Worker * A same LZ4_stream_t can be re-used multiple times consecutively 347*27162e4eSAndroid Build Coastguard Worker * and compress multiple streams, 348*27162e4eSAndroid Build Coastguard Worker * provided that it starts each new stream with LZ4_resetStream_fast(). 349*27162e4eSAndroid Build Coastguard Worker * 350*27162e4eSAndroid Build Coastguard Worker * LZ4_resetStream_fast() is much faster than LZ4_initStream(), 351*27162e4eSAndroid Build Coastguard Worker * but is not compatible with memory regions containing garbage data. 352*27162e4eSAndroid Build Coastguard Worker * 353*27162e4eSAndroid Build Coastguard Worker * Note: it's only useful to call LZ4_resetStream_fast() 354*27162e4eSAndroid Build Coastguard Worker * in the context of streaming compression. 355*27162e4eSAndroid Build Coastguard Worker * The *extState* functions perform their own resets. 356*27162e4eSAndroid Build Coastguard Worker * Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive. 357*27162e4eSAndroid Build Coastguard Worker */ 358*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr); 359*27162e4eSAndroid Build Coastguard Worker 360*27162e4eSAndroid Build Coastguard Worker /*! LZ4_loadDict() : 361*27162e4eSAndroid Build Coastguard Worker * Use this function to reference a static dictionary into LZ4_stream_t. 362*27162e4eSAndroid Build Coastguard Worker * The dictionary must remain available during compression. 363*27162e4eSAndroid Build Coastguard Worker * LZ4_loadDict() triggers a reset, so any previous data will be forgotten. 364*27162e4eSAndroid Build Coastguard Worker * The same dictionary will have to be loaded on decompression side for successful decoding. 365*27162e4eSAndroid Build Coastguard Worker * Dictionary are useful for better compression of small data (KB range). 366*27162e4eSAndroid Build Coastguard Worker * While LZ4 itself accepts any input as dictionary, dictionary efficiency is also a topic. 367*27162e4eSAndroid Build Coastguard Worker * When in doubt, employ the Zstandard's Dictionary Builder. 368*27162e4eSAndroid Build Coastguard Worker * Loading a size of 0 is allowed, and is the same as reset. 369*27162e4eSAndroid Build Coastguard Worker * @return : loaded dictionary size, in bytes (note: only the last 64 KB are loaded) 370*27162e4eSAndroid Build Coastguard Worker */ 371*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize); 372*27162e4eSAndroid Build Coastguard Worker 373*27162e4eSAndroid Build Coastguard Worker /*! LZ4_loadDictSlow() : v1.10.0+ 374*27162e4eSAndroid Build Coastguard Worker * Same as LZ4_loadDict(), 375*27162e4eSAndroid Build Coastguard Worker * but uses a bit more cpu to reference the dictionary content more thoroughly. 376*27162e4eSAndroid Build Coastguard Worker * This is expected to slightly improve compression ratio. 377*27162e4eSAndroid Build Coastguard Worker * The extra-cpu cost is likely worth it if the dictionary is re-used across multiple sessions. 378*27162e4eSAndroid Build Coastguard Worker * @return : loaded dictionary size, in bytes (note: only the last 64 KB are loaded) 379*27162e4eSAndroid Build Coastguard Worker */ 380*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_loadDictSlow(LZ4_stream_t* streamPtr, const char* dictionary, int dictSize); 381*27162e4eSAndroid Build Coastguard Worker 382*27162e4eSAndroid Build Coastguard Worker /*! LZ4_attach_dictionary() : stable since v1.10.0 383*27162e4eSAndroid Build Coastguard Worker * 384*27162e4eSAndroid Build Coastguard Worker * This allows efficient re-use of a static dictionary multiple times. 385*27162e4eSAndroid Build Coastguard Worker * 386*27162e4eSAndroid Build Coastguard Worker * Rather than re-loading the dictionary buffer into a working context before 387*27162e4eSAndroid Build Coastguard Worker * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a 388*27162e4eSAndroid Build Coastguard Worker * working LZ4_stream_t, this function introduces a no-copy setup mechanism, 389*27162e4eSAndroid Build Coastguard Worker * in which the working stream references @dictionaryStream in-place. 390*27162e4eSAndroid Build Coastguard Worker * 391*27162e4eSAndroid Build Coastguard Worker * Several assumptions are made about the state of @dictionaryStream. 392*27162e4eSAndroid Build Coastguard Worker * Currently, only states which have been prepared by LZ4_loadDict() or 393*27162e4eSAndroid Build Coastguard Worker * LZ4_loadDictSlow() should be expected to work. 394*27162e4eSAndroid Build Coastguard Worker * 395*27162e4eSAndroid Build Coastguard Worker * Alternatively, the provided @dictionaryStream may be NULL, 396*27162e4eSAndroid Build Coastguard Worker * in which case any existing dictionary stream is unset. 397*27162e4eSAndroid Build Coastguard Worker * 398*27162e4eSAndroid Build Coastguard Worker * If a dictionary is provided, it replaces any pre-existing stream history. 399*27162e4eSAndroid Build Coastguard Worker * The dictionary contents are the only history that can be referenced and 400*27162e4eSAndroid Build Coastguard Worker * logically immediately precede the data compressed in the first subsequent 401*27162e4eSAndroid Build Coastguard Worker * compression call. 402*27162e4eSAndroid Build Coastguard Worker * 403*27162e4eSAndroid Build Coastguard Worker * The dictionary will only remain attached to the working stream through the 404*27162e4eSAndroid Build Coastguard Worker * first compression call, at the end of which it is cleared. 405*27162e4eSAndroid Build Coastguard Worker * @dictionaryStream stream (and source buffer) must remain in-place / accessible / unchanged 406*27162e4eSAndroid Build Coastguard Worker * through the completion of the compression session. 407*27162e4eSAndroid Build Coastguard Worker * 408*27162e4eSAndroid Build Coastguard Worker * Note: there is no equivalent LZ4_attach_*() method on the decompression side 409*27162e4eSAndroid Build Coastguard Worker * because there is no initialization cost, hence no need to share the cost across multiple sessions. 410*27162e4eSAndroid Build Coastguard Worker * To decompress LZ4 blocks using dictionary, attached or not, 411*27162e4eSAndroid Build Coastguard Worker * just employ the regular LZ4_setStreamDecode() for streaming, 412*27162e4eSAndroid Build Coastguard Worker * or the stateless LZ4_decompress_safe_usingDict() for one-shot decompression. 413*27162e4eSAndroid Build Coastguard Worker */ 414*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API void 415*27162e4eSAndroid Build Coastguard Worker LZ4_attach_dictionary(LZ4_stream_t* workingStream, 416*27162e4eSAndroid Build Coastguard Worker const LZ4_stream_t* dictionaryStream); 417*27162e4eSAndroid Build Coastguard Worker 418*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compress_fast_continue() : 419*27162e4eSAndroid Build Coastguard Worker * Compress 'src' content using data from previously compressed blocks, for better compression ratio. 420*27162e4eSAndroid Build Coastguard Worker * 'dst' buffer must be already allocated. 421*27162e4eSAndroid Build Coastguard Worker * If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster. 422*27162e4eSAndroid Build Coastguard Worker * 423*27162e4eSAndroid Build Coastguard Worker * @return : size of compressed block 424*27162e4eSAndroid Build Coastguard Worker * or 0 if there is an error (typically, cannot fit into 'dst'). 425*27162e4eSAndroid Build Coastguard Worker * 426*27162e4eSAndroid Build Coastguard Worker * Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block. 427*27162e4eSAndroid Build Coastguard Worker * Each block has precise boundaries. 428*27162e4eSAndroid Build Coastguard Worker * Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata. 429*27162e4eSAndroid Build Coastguard Worker * It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together. 430*27162e4eSAndroid Build Coastguard Worker * 431*27162e4eSAndroid Build Coastguard Worker * Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory ! 432*27162e4eSAndroid Build Coastguard Worker * 433*27162e4eSAndroid Build Coastguard Worker * Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB. 434*27162e4eSAndroid Build Coastguard Worker * Make sure that buffers are separated, by at least one byte. 435*27162e4eSAndroid Build Coastguard Worker * This construction ensures that each block only depends on previous block. 436*27162e4eSAndroid Build Coastguard Worker * 437*27162e4eSAndroid Build Coastguard Worker * Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB. 438*27162e4eSAndroid Build Coastguard Worker * 439*27162e4eSAndroid Build Coastguard Worker * Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed. 440*27162e4eSAndroid Build Coastguard Worker */ 441*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); 442*27162e4eSAndroid Build Coastguard Worker 443*27162e4eSAndroid Build Coastguard Worker /*! LZ4_saveDict() : 444*27162e4eSAndroid Build Coastguard Worker * If last 64KB data cannot be guaranteed to remain available at its current memory location, 445*27162e4eSAndroid Build Coastguard Worker * save it into a safer place (char* safeBuffer). 446*27162e4eSAndroid Build Coastguard Worker * This is schematically equivalent to a memcpy() followed by LZ4_loadDict(), 447*27162e4eSAndroid Build Coastguard Worker * but is much faster, because LZ4_saveDict() doesn't need to rebuild tables. 448*27162e4eSAndroid Build Coastguard Worker * @return : saved dictionary size in bytes (necessarily <= maxDictSize), or 0 if error. 449*27162e4eSAndroid Build Coastguard Worker */ 450*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int maxDictSize); 451*27162e4eSAndroid Build Coastguard Worker 452*27162e4eSAndroid Build Coastguard Worker 453*27162e4eSAndroid Build Coastguard Worker /*-********************************************** 454*27162e4eSAndroid Build Coastguard Worker * Streaming Decompression Functions 455*27162e4eSAndroid Build Coastguard Worker * Bufferless synchronous API 456*27162e4eSAndroid Build Coastguard Worker ************************************************/ 457*27162e4eSAndroid Build Coastguard Worker typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */ 458*27162e4eSAndroid Build Coastguard Worker 459*27162e4eSAndroid Build Coastguard Worker /*! LZ4_createStreamDecode() and LZ4_freeStreamDecode() : 460*27162e4eSAndroid Build Coastguard Worker * creation / destruction of streaming decompression tracking context. 461*27162e4eSAndroid Build Coastguard Worker * A tracking context can be re-used multiple times. 462*27162e4eSAndroid Build Coastguard Worker */ 463*27162e4eSAndroid Build Coastguard Worker #if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */ 464*27162e4eSAndroid Build Coastguard Worker #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) 465*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void); 466*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); 467*27162e4eSAndroid Build Coastguard Worker #endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */ 468*27162e4eSAndroid Build Coastguard Worker #endif 469*27162e4eSAndroid Build Coastguard Worker 470*27162e4eSAndroid Build Coastguard Worker /*! LZ4_setStreamDecode() : 471*27162e4eSAndroid Build Coastguard Worker * An LZ4_streamDecode_t context can be allocated once and re-used multiple times. 472*27162e4eSAndroid Build Coastguard Worker * Use this function to start decompression of a new stream of blocks. 473*27162e4eSAndroid Build Coastguard Worker * A dictionary can optionally be set. Use NULL or size 0 for a reset order. 474*27162e4eSAndroid Build Coastguard Worker * Dictionary is presumed stable : it must remain accessible and unmodified during next decompression. 475*27162e4eSAndroid Build Coastguard Worker * @return : 1 if OK, 0 if error 476*27162e4eSAndroid Build Coastguard Worker */ 477*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize); 478*27162e4eSAndroid Build Coastguard Worker 479*27162e4eSAndroid Build Coastguard Worker /*! LZ4_decoderRingBufferSize() : v1.8.2+ 480*27162e4eSAndroid Build Coastguard Worker * Note : in a ring buffer scenario (optional), 481*27162e4eSAndroid Build Coastguard Worker * blocks are presumed decompressed next to each other 482*27162e4eSAndroid Build Coastguard Worker * up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize), 483*27162e4eSAndroid Build Coastguard Worker * at which stage it resumes from beginning of ring buffer. 484*27162e4eSAndroid Build Coastguard Worker * When setting such a ring buffer for streaming decompression, 485*27162e4eSAndroid Build Coastguard Worker * provides the minimum size of this ring buffer 486*27162e4eSAndroid Build Coastguard Worker * to be compatible with any source respecting maxBlockSize condition. 487*27162e4eSAndroid Build Coastguard Worker * @return : minimum ring buffer size, 488*27162e4eSAndroid Build Coastguard Worker * or 0 if there is an error (invalid maxBlockSize). 489*27162e4eSAndroid Build Coastguard Worker */ 490*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize); 491*27162e4eSAndroid Build Coastguard Worker #define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */ 492*27162e4eSAndroid Build Coastguard Worker 493*27162e4eSAndroid Build Coastguard Worker /*! LZ4_decompress_safe_continue() : 494*27162e4eSAndroid Build Coastguard Worker * This decoding function allows decompression of consecutive blocks in "streaming" mode. 495*27162e4eSAndroid Build Coastguard Worker * The difference with the usual independent blocks is that 496*27162e4eSAndroid Build Coastguard Worker * new blocks are allowed to find references into former blocks. 497*27162e4eSAndroid Build Coastguard Worker * A block is an unsplittable entity, and must be presented entirely to the decompression function. 498*27162e4eSAndroid Build Coastguard Worker * LZ4_decompress_safe_continue() only accepts one block at a time. 499*27162e4eSAndroid Build Coastguard Worker * It's modeled after `LZ4_decompress_safe()` and behaves similarly. 500*27162e4eSAndroid Build Coastguard Worker * 501*27162e4eSAndroid Build Coastguard Worker * @LZ4_streamDecode : decompression state, tracking the position in memory of past data 502*27162e4eSAndroid Build Coastguard Worker * @compressedSize : exact complete size of one compressed block. 503*27162e4eSAndroid Build Coastguard Worker * @dstCapacity : size of destination buffer (which must be already allocated), 504*27162e4eSAndroid Build Coastguard Worker * must be an upper bound of decompressed size. 505*27162e4eSAndroid Build Coastguard Worker * @return : number of bytes decompressed into destination buffer (necessarily <= dstCapacity) 506*27162e4eSAndroid Build Coastguard Worker * If destination buffer is not large enough, decoding will stop and output an error code (negative value). 507*27162e4eSAndroid Build Coastguard Worker * If the source stream is detected malformed, the function will stop decoding and return a negative result. 508*27162e4eSAndroid Build Coastguard Worker * 509*27162e4eSAndroid Build Coastguard Worker * The last 64KB of previously decoded data *must* remain available and unmodified 510*27162e4eSAndroid Build Coastguard Worker * at the memory position where they were previously decoded. 511*27162e4eSAndroid Build Coastguard Worker * If less than 64KB of data has been decoded, all the data must be present. 512*27162e4eSAndroid Build Coastguard Worker * 513*27162e4eSAndroid Build Coastguard Worker * Special : if decompression side sets a ring buffer, it must respect one of the following conditions : 514*27162e4eSAndroid Build Coastguard Worker * - Decompression buffer size is _at least_ LZ4_decoderRingBufferSize(maxBlockSize). 515*27162e4eSAndroid Build Coastguard Worker * maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes. 516*27162e4eSAndroid Build Coastguard Worker * In which case, encoding and decoding buffers do not need to be synchronized. 517*27162e4eSAndroid Build Coastguard Worker * Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize. 518*27162e4eSAndroid Build Coastguard Worker * - Synchronized mode : 519*27162e4eSAndroid Build Coastguard Worker * Decompression buffer size is _exactly_ the same as compression buffer size, 520*27162e4eSAndroid Build Coastguard Worker * and follows exactly same update rule (block boundaries at same positions), 521*27162e4eSAndroid Build Coastguard Worker * and decoding function is provided with exact decompressed size of each block (exception for last block of the stream), 522*27162e4eSAndroid Build Coastguard Worker * _then_ decoding & encoding ring buffer can have any size, including small ones ( < 64 KB). 523*27162e4eSAndroid Build Coastguard Worker * - Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes. 524*27162e4eSAndroid Build Coastguard Worker * In which case, encoding and decoding buffers do not need to be synchronized, 525*27162e4eSAndroid Build Coastguard Worker * and encoding ring buffer can have any size, including small ones ( < 64 KB). 526*27162e4eSAndroid Build Coastguard Worker * 527*27162e4eSAndroid Build Coastguard Worker * Whenever these conditions are not possible, 528*27162e4eSAndroid Build Coastguard Worker * save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression, 529*27162e4eSAndroid Build Coastguard Worker * then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block. 530*27162e4eSAndroid Build Coastguard Worker */ 531*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int 532*27162e4eSAndroid Build Coastguard Worker LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, 533*27162e4eSAndroid Build Coastguard Worker const char* src, char* dst, 534*27162e4eSAndroid Build Coastguard Worker int srcSize, int dstCapacity); 535*27162e4eSAndroid Build Coastguard Worker 536*27162e4eSAndroid Build Coastguard Worker 537*27162e4eSAndroid Build Coastguard Worker /*! LZ4_decompress_safe_usingDict() : 538*27162e4eSAndroid Build Coastguard Worker * Works the same as 539*27162e4eSAndroid Build Coastguard Worker * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_safe_continue() 540*27162e4eSAndroid Build Coastguard Worker * However, it's stateless: it doesn't need any LZ4_streamDecode_t state. 541*27162e4eSAndroid Build Coastguard Worker * Dictionary is presumed stable : it must remain accessible and unmodified during decompression. 542*27162e4eSAndroid Build Coastguard Worker * Performance tip : Decompression speed can be substantially increased 543*27162e4eSAndroid Build Coastguard Worker * when dst == dictStart + dictSize. 544*27162e4eSAndroid Build Coastguard Worker */ 545*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int 546*27162e4eSAndroid Build Coastguard Worker LZ4_decompress_safe_usingDict(const char* src, char* dst, 547*27162e4eSAndroid Build Coastguard Worker int srcSize, int dstCapacity, 548*27162e4eSAndroid Build Coastguard Worker const char* dictStart, int dictSize); 549*27162e4eSAndroid Build Coastguard Worker 550*27162e4eSAndroid Build Coastguard Worker /*! LZ4_decompress_safe_partial_usingDict() : 551*27162e4eSAndroid Build Coastguard Worker * Behaves the same as LZ4_decompress_safe_partial() 552*27162e4eSAndroid Build Coastguard Worker * with the added ability to specify a memory segment for past data. 553*27162e4eSAndroid Build Coastguard Worker * Performance tip : Decompression speed can be substantially increased 554*27162e4eSAndroid Build Coastguard Worker * when dst == dictStart + dictSize. 555*27162e4eSAndroid Build Coastguard Worker */ 556*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int 557*27162e4eSAndroid Build Coastguard Worker LZ4_decompress_safe_partial_usingDict(const char* src, char* dst, 558*27162e4eSAndroid Build Coastguard Worker int compressedSize, 559*27162e4eSAndroid Build Coastguard Worker int targetOutputSize, int maxOutputSize, 560*27162e4eSAndroid Build Coastguard Worker const char* dictStart, int dictSize); 561*27162e4eSAndroid Build Coastguard Worker 562*27162e4eSAndroid Build Coastguard Worker #endif /* LZ4_H_2983827168210 */ 563*27162e4eSAndroid Build Coastguard Worker 564*27162e4eSAndroid Build Coastguard Worker 565*27162e4eSAndroid Build Coastguard Worker /*^************************************* 566*27162e4eSAndroid Build Coastguard Worker * !!!!!! STATIC LINKING ONLY !!!!!! 567*27162e4eSAndroid Build Coastguard Worker ***************************************/ 568*27162e4eSAndroid Build Coastguard Worker 569*27162e4eSAndroid Build Coastguard Worker /*-**************************************************************************** 570*27162e4eSAndroid Build Coastguard Worker * Experimental section 571*27162e4eSAndroid Build Coastguard Worker * 572*27162e4eSAndroid Build Coastguard Worker * Symbols declared in this section must be considered unstable. Their 573*27162e4eSAndroid Build Coastguard Worker * signatures or semantics may change, or they may be removed altogether in the 574*27162e4eSAndroid Build Coastguard Worker * future. They are therefore only safe to depend on when the caller is 575*27162e4eSAndroid Build Coastguard Worker * statically linked against the library. 576*27162e4eSAndroid Build Coastguard Worker * 577*27162e4eSAndroid Build Coastguard Worker * To protect against unsafe usage, not only are the declarations guarded, 578*27162e4eSAndroid Build Coastguard Worker * the definitions are hidden by default 579*27162e4eSAndroid Build Coastguard Worker * when building LZ4 as a shared/dynamic library. 580*27162e4eSAndroid Build Coastguard Worker * 581*27162e4eSAndroid Build Coastguard Worker * In order to access these declarations, 582*27162e4eSAndroid Build Coastguard Worker * define LZ4_STATIC_LINKING_ONLY in your application 583*27162e4eSAndroid Build Coastguard Worker * before including LZ4's headers. 584*27162e4eSAndroid Build Coastguard Worker * 585*27162e4eSAndroid Build Coastguard Worker * In order to make their implementations accessible dynamically, you must 586*27162e4eSAndroid Build Coastguard Worker * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library. 587*27162e4eSAndroid Build Coastguard Worker ******************************************************************************/ 588*27162e4eSAndroid Build Coastguard Worker 589*27162e4eSAndroid Build Coastguard Worker #ifdef LZ4_STATIC_LINKING_ONLY 590*27162e4eSAndroid Build Coastguard Worker 591*27162e4eSAndroid Build Coastguard Worker #ifndef LZ4_STATIC_3504398509 592*27162e4eSAndroid Build Coastguard Worker #define LZ4_STATIC_3504398509 593*27162e4eSAndroid Build Coastguard Worker 594*27162e4eSAndroid Build Coastguard Worker #ifdef LZ4_PUBLISH_STATIC_FUNCTIONS 595*27162e4eSAndroid Build Coastguard Worker # define LZ4LIB_STATIC_API LZ4LIB_API 596*27162e4eSAndroid Build Coastguard Worker #else 597*27162e4eSAndroid Build Coastguard Worker # define LZ4LIB_STATIC_API 598*27162e4eSAndroid Build Coastguard Worker #endif 599*27162e4eSAndroid Build Coastguard Worker 600*27162e4eSAndroid Build Coastguard Worker 601*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compress_fast_extState_fastReset() : 602*27162e4eSAndroid Build Coastguard Worker * A variant of LZ4_compress_fast_extState(). 603*27162e4eSAndroid Build Coastguard Worker * 604*27162e4eSAndroid Build Coastguard Worker * Using this variant avoids an expensive initialization step. 605*27162e4eSAndroid Build Coastguard Worker * It is only safe to call if the state buffer is known to be correctly initialized already 606*27162e4eSAndroid Build Coastguard Worker * (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized"). 607*27162e4eSAndroid Build Coastguard Worker * From a high level, the difference is that 608*27162e4eSAndroid Build Coastguard Worker * this function initializes the provided state with a call to something like LZ4_resetStream_fast() 609*27162e4eSAndroid Build Coastguard Worker * while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream(). 610*27162e4eSAndroid Build Coastguard Worker */ 611*27162e4eSAndroid Build Coastguard Worker LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); 612*27162e4eSAndroid Build Coastguard Worker 613*27162e4eSAndroid Build Coastguard Worker /*! LZ4_compress_destSize_extState() : introduced in v1.10.0 614*27162e4eSAndroid Build Coastguard Worker * Same as LZ4_compress_destSize(), but using an externally allocated state. 615*27162e4eSAndroid Build Coastguard Worker * Also: exposes @acceleration 616*27162e4eSAndroid Build Coastguard Worker */ 617*27162e4eSAndroid Build Coastguard Worker int LZ4_compress_destSize_extState(void* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize, int acceleration); 618*27162e4eSAndroid Build Coastguard Worker 619*27162e4eSAndroid Build Coastguard Worker /*! In-place compression and decompression 620*27162e4eSAndroid Build Coastguard Worker * 621*27162e4eSAndroid Build Coastguard Worker * It's possible to have input and output sharing the same buffer, 622*27162e4eSAndroid Build Coastguard Worker * for highly constrained memory environments. 623*27162e4eSAndroid Build Coastguard Worker * In both cases, it requires input to lay at the end of the buffer, 624*27162e4eSAndroid Build Coastguard Worker * and decompression to start at beginning of the buffer. 625*27162e4eSAndroid Build Coastguard Worker * Buffer size must feature some margin, hence be larger than final size. 626*27162e4eSAndroid Build Coastguard Worker * 627*27162e4eSAndroid Build Coastguard Worker * |<------------------------buffer--------------------------------->| 628*27162e4eSAndroid Build Coastguard Worker * |<-----------compressed data--------->| 629*27162e4eSAndroid Build Coastguard Worker * |<-----------decompressed size------------------>| 630*27162e4eSAndroid Build Coastguard Worker * |<----margin---->| 631*27162e4eSAndroid Build Coastguard Worker * 632*27162e4eSAndroid Build Coastguard Worker * This technique is more useful for decompression, 633*27162e4eSAndroid Build Coastguard Worker * since decompressed size is typically larger, 634*27162e4eSAndroid Build Coastguard Worker * and margin is short. 635*27162e4eSAndroid Build Coastguard Worker * 636*27162e4eSAndroid Build Coastguard Worker * In-place decompression will work inside any buffer 637*27162e4eSAndroid Build Coastguard Worker * which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize). 638*27162e4eSAndroid Build Coastguard Worker * This presumes that decompressedSize > compressedSize. 639*27162e4eSAndroid Build Coastguard Worker * Otherwise, it means compression actually expanded data, 640*27162e4eSAndroid Build Coastguard Worker * and it would be more efficient to store such data with a flag indicating it's not compressed. 641*27162e4eSAndroid Build Coastguard Worker * This can happen when data is not compressible (already compressed, or encrypted). 642*27162e4eSAndroid Build Coastguard Worker * 643*27162e4eSAndroid Build Coastguard Worker * For in-place compression, margin is larger, as it must be able to cope with both 644*27162e4eSAndroid Build Coastguard Worker * history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX, 645*27162e4eSAndroid Build Coastguard Worker * and data expansion, which can happen when input is not compressible. 646*27162e4eSAndroid Build Coastguard Worker * As a consequence, buffer size requirements are much higher, 647*27162e4eSAndroid Build Coastguard Worker * and memory savings offered by in-place compression are more limited. 648*27162e4eSAndroid Build Coastguard Worker * 649*27162e4eSAndroid Build Coastguard Worker * There are ways to limit this cost for compression : 650*27162e4eSAndroid Build Coastguard Worker * - Reduce history size, by modifying LZ4_DISTANCE_MAX. 651*27162e4eSAndroid Build Coastguard Worker * Note that it is a compile-time constant, so all compressions will apply this limit. 652*27162e4eSAndroid Build Coastguard Worker * Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX, 653*27162e4eSAndroid Build Coastguard Worker * so it's a reasonable trick when inputs are known to be small. 654*27162e4eSAndroid Build Coastguard Worker * - Require the compressor to deliver a "maximum compressed size". 655*27162e4eSAndroid Build Coastguard Worker * This is the `dstCapacity` parameter in `LZ4_compress*()`. 656*27162e4eSAndroid Build Coastguard Worker * When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail, 657*27162e4eSAndroid Build Coastguard Worker * in which case, the return code will be 0 (zero). 658*27162e4eSAndroid Build Coastguard Worker * The caller must be ready for these cases to happen, 659*27162e4eSAndroid Build Coastguard Worker * and typically design a backup scheme to send data uncompressed. 660*27162e4eSAndroid Build Coastguard Worker * The combination of both techniques can significantly reduce 661*27162e4eSAndroid Build Coastguard Worker * the amount of margin required for in-place compression. 662*27162e4eSAndroid Build Coastguard Worker * 663*27162e4eSAndroid Build Coastguard Worker * In-place compression can work in any buffer 664*27162e4eSAndroid Build Coastguard Worker * which size is >= (maxCompressedSize) 665*27162e4eSAndroid Build Coastguard Worker * with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success. 666*27162e4eSAndroid Build Coastguard Worker * LZ4_COMPRESS_INPLACE_BUFFER_SIZE() depends on both maxCompressedSize and LZ4_DISTANCE_MAX, 667*27162e4eSAndroid Build Coastguard Worker * so it's possible to reduce memory requirements by playing with them. 668*27162e4eSAndroid Build Coastguard Worker */ 669*27162e4eSAndroid Build Coastguard Worker 670*27162e4eSAndroid Build Coastguard Worker #define LZ4_DECOMPRESS_INPLACE_MARGIN(compressedSize) (((compressedSize) >> 8) + 32) 671*27162e4eSAndroid Build Coastguard Worker #define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize)) /**< note: presumes that compressedSize < decompressedSize. note2: margin is overestimated a bit, since it could use compressedSize instead */ 672*27162e4eSAndroid Build Coastguard Worker 673*27162e4eSAndroid Build Coastguard Worker #ifndef LZ4_DISTANCE_MAX /* history window size; can be user-defined at compile time */ 674*27162e4eSAndroid Build Coastguard Worker # define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */ 675*27162e4eSAndroid Build Coastguard Worker #endif 676*27162e4eSAndroid Build Coastguard Worker 677*27162e4eSAndroid Build Coastguard Worker #define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32) /* LZ4_DISTANCE_MAX can be safely replaced by srcSize when it's smaller */ 678*27162e4eSAndroid Build Coastguard Worker #define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN) /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */ 679*27162e4eSAndroid Build Coastguard Worker 680*27162e4eSAndroid Build Coastguard Worker #endif /* LZ4_STATIC_3504398509 */ 681*27162e4eSAndroid Build Coastguard Worker #endif /* LZ4_STATIC_LINKING_ONLY */ 682*27162e4eSAndroid Build Coastguard Worker 683*27162e4eSAndroid Build Coastguard Worker 684*27162e4eSAndroid Build Coastguard Worker 685*27162e4eSAndroid Build Coastguard Worker #ifndef LZ4_H_98237428734687 686*27162e4eSAndroid Build Coastguard Worker #define LZ4_H_98237428734687 687*27162e4eSAndroid Build Coastguard Worker 688*27162e4eSAndroid Build Coastguard Worker /*-************************************************************ 689*27162e4eSAndroid Build Coastguard Worker * Private Definitions 690*27162e4eSAndroid Build Coastguard Worker ************************************************************** 691*27162e4eSAndroid Build Coastguard Worker * Do not use these definitions directly. 692*27162e4eSAndroid Build Coastguard Worker * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`. 693*27162e4eSAndroid Build Coastguard Worker * Accessing members will expose user code to API and/or ABI break in future versions of the library. 694*27162e4eSAndroid Build Coastguard Worker **************************************************************/ 695*27162e4eSAndroid Build Coastguard Worker #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2) 696*27162e4eSAndroid Build Coastguard Worker #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE) 697*27162e4eSAndroid Build Coastguard Worker #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */ 698*27162e4eSAndroid Build Coastguard Worker 699*27162e4eSAndroid Build Coastguard Worker #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 700*27162e4eSAndroid Build Coastguard Worker # include <stdint.h> 701*27162e4eSAndroid Build Coastguard Worker typedef int8_t LZ4_i8; 702*27162e4eSAndroid Build Coastguard Worker typedef uint8_t LZ4_byte; 703*27162e4eSAndroid Build Coastguard Worker typedef uint16_t LZ4_u16; 704*27162e4eSAndroid Build Coastguard Worker typedef uint32_t LZ4_u32; 705*27162e4eSAndroid Build Coastguard Worker #else 706*27162e4eSAndroid Build Coastguard Worker typedef signed char LZ4_i8; 707*27162e4eSAndroid Build Coastguard Worker typedef unsigned char LZ4_byte; 708*27162e4eSAndroid Build Coastguard Worker typedef unsigned short LZ4_u16; 709*27162e4eSAndroid Build Coastguard Worker typedef unsigned int LZ4_u32; 710*27162e4eSAndroid Build Coastguard Worker #endif 711*27162e4eSAndroid Build Coastguard Worker 712*27162e4eSAndroid Build Coastguard Worker /*! LZ4_stream_t : 713*27162e4eSAndroid Build Coastguard Worker * Never ever use below internal definitions directly ! 714*27162e4eSAndroid Build Coastguard Worker * These definitions are not API/ABI safe, and may change in future versions. 715*27162e4eSAndroid Build Coastguard Worker * If you need static allocation, declare or allocate an LZ4_stream_t object. 716*27162e4eSAndroid Build Coastguard Worker **/ 717*27162e4eSAndroid Build Coastguard Worker 718*27162e4eSAndroid Build Coastguard Worker typedef struct LZ4_stream_t_internal LZ4_stream_t_internal; 719*27162e4eSAndroid Build Coastguard Worker struct LZ4_stream_t_internal { 720*27162e4eSAndroid Build Coastguard Worker LZ4_u32 hashTable[LZ4_HASH_SIZE_U32]; 721*27162e4eSAndroid Build Coastguard Worker const LZ4_byte* dictionary; 722*27162e4eSAndroid Build Coastguard Worker const LZ4_stream_t_internal* dictCtx; 723*27162e4eSAndroid Build Coastguard Worker LZ4_u32 currentOffset; 724*27162e4eSAndroid Build Coastguard Worker LZ4_u32 tableType; 725*27162e4eSAndroid Build Coastguard Worker LZ4_u32 dictSize; 726*27162e4eSAndroid Build Coastguard Worker /* Implicit padding to ensure structure is aligned */ 727*27162e4eSAndroid Build Coastguard Worker }; 728*27162e4eSAndroid Build Coastguard Worker 729*27162e4eSAndroid Build Coastguard Worker #define LZ4_STREAM_MINSIZE ((1UL << (LZ4_MEMORY_USAGE)) + 32) /* static size, for inter-version compatibility */ 730*27162e4eSAndroid Build Coastguard Worker union LZ4_stream_u { 731*27162e4eSAndroid Build Coastguard Worker char minStateSize[LZ4_STREAM_MINSIZE]; 732*27162e4eSAndroid Build Coastguard Worker LZ4_stream_t_internal internal_donotuse; 733*27162e4eSAndroid Build Coastguard Worker }; /* previously typedef'd to LZ4_stream_t */ 734*27162e4eSAndroid Build Coastguard Worker 735*27162e4eSAndroid Build Coastguard Worker 736*27162e4eSAndroid Build Coastguard Worker /*! LZ4_initStream() : v1.9.0+ 737*27162e4eSAndroid Build Coastguard Worker * An LZ4_stream_t structure must be initialized at least once. 738*27162e4eSAndroid Build Coastguard Worker * This is automatically done when invoking LZ4_createStream(), 739*27162e4eSAndroid Build Coastguard Worker * but it's not when the structure is simply declared on stack (for example). 740*27162e4eSAndroid Build Coastguard Worker * 741*27162e4eSAndroid Build Coastguard Worker * Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t. 742*27162e4eSAndroid Build Coastguard Worker * It can also initialize any arbitrary buffer of sufficient size, 743*27162e4eSAndroid Build Coastguard Worker * and will @return a pointer of proper type upon initialization. 744*27162e4eSAndroid Build Coastguard Worker * 745*27162e4eSAndroid Build Coastguard Worker * Note : initialization fails if size and alignment conditions are not respected. 746*27162e4eSAndroid Build Coastguard Worker * In which case, the function will @return NULL. 747*27162e4eSAndroid Build Coastguard Worker * Note2: An LZ4_stream_t structure guarantees correct alignment and size. 748*27162e4eSAndroid Build Coastguard Worker * Note3: Before v1.9.0, use LZ4_resetStream() instead 749*27162e4eSAndroid Build Coastguard Worker **/ 750*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* stateBuffer, size_t size); 751*27162e4eSAndroid Build Coastguard Worker 752*27162e4eSAndroid Build Coastguard Worker 753*27162e4eSAndroid Build Coastguard Worker /*! LZ4_streamDecode_t : 754*27162e4eSAndroid Build Coastguard Worker * Never ever use below internal definitions directly ! 755*27162e4eSAndroid Build Coastguard Worker * These definitions are not API/ABI safe, and may change in future versions. 756*27162e4eSAndroid Build Coastguard Worker * If you need static allocation, declare or allocate an LZ4_streamDecode_t object. 757*27162e4eSAndroid Build Coastguard Worker **/ 758*27162e4eSAndroid Build Coastguard Worker typedef struct { 759*27162e4eSAndroid Build Coastguard Worker const LZ4_byte* externalDict; 760*27162e4eSAndroid Build Coastguard Worker const LZ4_byte* prefixEnd; 761*27162e4eSAndroid Build Coastguard Worker size_t extDictSize; 762*27162e4eSAndroid Build Coastguard Worker size_t prefixSize; 763*27162e4eSAndroid Build Coastguard Worker } LZ4_streamDecode_t_internal; 764*27162e4eSAndroid Build Coastguard Worker 765*27162e4eSAndroid Build Coastguard Worker #define LZ4_STREAMDECODE_MINSIZE 32 766*27162e4eSAndroid Build Coastguard Worker union LZ4_streamDecode_u { 767*27162e4eSAndroid Build Coastguard Worker char minStateSize[LZ4_STREAMDECODE_MINSIZE]; 768*27162e4eSAndroid Build Coastguard Worker LZ4_streamDecode_t_internal internal_donotuse; 769*27162e4eSAndroid Build Coastguard Worker } ; /* previously typedef'd to LZ4_streamDecode_t */ 770*27162e4eSAndroid Build Coastguard Worker 771*27162e4eSAndroid Build Coastguard Worker 772*27162e4eSAndroid Build Coastguard Worker 773*27162e4eSAndroid Build Coastguard Worker /*-************************************ 774*27162e4eSAndroid Build Coastguard Worker * Obsolete Functions 775*27162e4eSAndroid Build Coastguard Worker **************************************/ 776*27162e4eSAndroid Build Coastguard Worker 777*27162e4eSAndroid Build Coastguard Worker /*! Deprecation warnings 778*27162e4eSAndroid Build Coastguard Worker * 779*27162e4eSAndroid Build Coastguard Worker * Deprecated functions make the compiler generate a warning when invoked. 780*27162e4eSAndroid Build Coastguard Worker * This is meant to invite users to update their source code. 781*27162e4eSAndroid Build Coastguard Worker * Should deprecation warnings be a problem, it is generally possible to disable them, 782*27162e4eSAndroid Build Coastguard Worker * typically with -Wno-deprecated-declarations for gcc 783*27162e4eSAndroid Build Coastguard Worker * or _CRT_SECURE_NO_WARNINGS in Visual. 784*27162e4eSAndroid Build Coastguard Worker * 785*27162e4eSAndroid Build Coastguard Worker * Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS 786*27162e4eSAndroid Build Coastguard Worker * before including the header file. 787*27162e4eSAndroid Build Coastguard Worker */ 788*27162e4eSAndroid Build Coastguard Worker #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS 789*27162e4eSAndroid Build Coastguard Worker # define LZ4_DEPRECATED(message) /* disable deprecation warnings */ 790*27162e4eSAndroid Build Coastguard Worker #else 791*27162e4eSAndroid Build Coastguard Worker # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ 792*27162e4eSAndroid Build Coastguard Worker # define LZ4_DEPRECATED(message) [[deprecated(message)]] 793*27162e4eSAndroid Build Coastguard Worker # elif defined(_MSC_VER) 794*27162e4eSAndroid Build Coastguard Worker # define LZ4_DEPRECATED(message) __declspec(deprecated(message)) 795*27162e4eSAndroid Build Coastguard Worker # elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45)) 796*27162e4eSAndroid Build Coastguard Worker # define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) 797*27162e4eSAndroid Build Coastguard Worker # elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31) 798*27162e4eSAndroid Build Coastguard Worker # define LZ4_DEPRECATED(message) __attribute__((deprecated)) 799*27162e4eSAndroid Build Coastguard Worker # else 800*27162e4eSAndroid Build Coastguard Worker # pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler") 801*27162e4eSAndroid Build Coastguard Worker # define LZ4_DEPRECATED(message) /* disabled */ 802*27162e4eSAndroid Build Coastguard Worker # endif 803*27162e4eSAndroid Build Coastguard Worker #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */ 804*27162e4eSAndroid Build Coastguard Worker 805*27162e4eSAndroid Build Coastguard Worker /*! Obsolete compression functions (since v1.7.3) */ 806*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* src, char* dest, int srcSize); 807*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* src, char* dest, int srcSize, int maxOutputSize); 808*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); 809*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); 810*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); 811*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize); 812*27162e4eSAndroid Build Coastguard Worker 813*27162e4eSAndroid Build Coastguard Worker /*! Obsolete decompression functions (since v1.8.0) */ 814*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize); 815*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); 816*27162e4eSAndroid Build Coastguard Worker 817*27162e4eSAndroid Build Coastguard Worker /* Obsolete streaming functions (since v1.7.0) 818*27162e4eSAndroid Build Coastguard Worker * degraded functionality; do not use! 819*27162e4eSAndroid Build Coastguard Worker * 820*27162e4eSAndroid Build Coastguard Worker * In order to perform streaming compression, these functions depended on data 821*27162e4eSAndroid Build Coastguard Worker * that is no longer tracked in the state. They have been preserved as well as 822*27162e4eSAndroid Build Coastguard Worker * possible: using them will still produce a correct output. However, they don't 823*27162e4eSAndroid Build Coastguard Worker * actually retain any history between compression calls. The compression ratio 824*27162e4eSAndroid Build Coastguard Worker * achieved will therefore be no better than compressing each chunk 825*27162e4eSAndroid Build Coastguard Worker * independently. 826*27162e4eSAndroid Build Coastguard Worker */ 827*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer); 828*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void); 829*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer); 830*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state); 831*27162e4eSAndroid Build Coastguard Worker 832*27162e4eSAndroid Build Coastguard Worker /*! Obsolete streaming decoding functions (since v1.7.0) */ 833*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize); 834*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize); 835*27162e4eSAndroid Build Coastguard Worker 836*27162e4eSAndroid Build Coastguard Worker /*! Obsolete LZ4_decompress_fast variants (since v1.9.0) : 837*27162e4eSAndroid Build Coastguard Worker * These functions used to be faster than LZ4_decompress_safe(), 838*27162e4eSAndroid Build Coastguard Worker * but this is no longer the case. They are now slower. 839*27162e4eSAndroid Build Coastguard Worker * This is because LZ4_decompress_fast() doesn't know the input size, 840*27162e4eSAndroid Build Coastguard Worker * and therefore must progress more cautiously into the input buffer to not read beyond the end of block. 841*27162e4eSAndroid Build Coastguard Worker * On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability. 842*27162e4eSAndroid Build Coastguard Worker * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated. 843*27162e4eSAndroid Build Coastguard Worker * 844*27162e4eSAndroid Build Coastguard Worker * The last remaining LZ4_decompress_fast() specificity is that 845*27162e4eSAndroid Build Coastguard Worker * it can decompress a block without knowing its compressed size. 846*27162e4eSAndroid Build Coastguard Worker * Such functionality can be achieved in a more secure manner 847*27162e4eSAndroid Build Coastguard Worker * by employing LZ4_decompress_safe_partial(). 848*27162e4eSAndroid Build Coastguard Worker * 849*27162e4eSAndroid Build Coastguard Worker * Parameters: 850*27162e4eSAndroid Build Coastguard Worker * originalSize : is the uncompressed size to regenerate. 851*27162e4eSAndroid Build Coastguard Worker * `dst` must be already allocated, its size must be >= 'originalSize' bytes. 852*27162e4eSAndroid Build Coastguard Worker * @return : number of bytes read from source buffer (== compressed size). 853*27162e4eSAndroid Build Coastguard Worker * The function expects to finish at block's end exactly. 854*27162e4eSAndroid Build Coastguard Worker * If the source stream is detected malformed, the function stops decoding and returns a negative result. 855*27162e4eSAndroid Build Coastguard Worker * note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer. 856*27162e4eSAndroid Build Coastguard Worker * However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds. 857*27162e4eSAndroid Build Coastguard Worker * Also, since match offsets are not validated, match reads from 'src' may underflow too. 858*27162e4eSAndroid Build Coastguard Worker * These issues never happen if input (compressed) data is correct. 859*27162e4eSAndroid Build Coastguard Worker * But they may happen if input data is invalid (error or intentional tampering). 860*27162e4eSAndroid Build Coastguard Worker * As a consequence, use these functions in trusted environments with trusted data **only**. 861*27162e4eSAndroid Build Coastguard Worker */ 862*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_partial() instead") 863*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize); 864*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("This function is deprecated and unsafe. Consider migrating towards LZ4_decompress_safe_continue() instead. " 865*27162e4eSAndroid Build Coastguard Worker "Note that the contract will change (requires block's compressed size, instead of decompressed size)") 866*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize); 867*27162e4eSAndroid Build Coastguard Worker LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_partial_usingDict() instead") 868*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize); 869*27162e4eSAndroid Build Coastguard Worker 870*27162e4eSAndroid Build Coastguard Worker /*! LZ4_resetStream() : 871*27162e4eSAndroid Build Coastguard Worker * An LZ4_stream_t structure must be initialized at least once. 872*27162e4eSAndroid Build Coastguard Worker * This is done with LZ4_initStream(), or LZ4_resetStream(). 873*27162e4eSAndroid Build Coastguard Worker * Consider switching to LZ4_initStream(), 874*27162e4eSAndroid Build Coastguard Worker * invoking LZ4_resetStream() will trigger deprecation warnings in the future. 875*27162e4eSAndroid Build Coastguard Worker */ 876*27162e4eSAndroid Build Coastguard Worker LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr); 877*27162e4eSAndroid Build Coastguard Worker 878*27162e4eSAndroid Build Coastguard Worker 879*27162e4eSAndroid Build Coastguard Worker #endif /* LZ4_H_98237428734687 */ 880*27162e4eSAndroid Build Coastguard Worker 881*27162e4eSAndroid Build Coastguard Worker 882*27162e4eSAndroid Build Coastguard Worker #if defined (__cplusplus) 883*27162e4eSAndroid Build Coastguard Worker } 884*27162e4eSAndroid Build Coastguard Worker #endif 885