lz4.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * LZ4 - Fast LZ compression algorithm
  3. * Header File
  4. * Copyright (C) 2011-present, Yann Collet.
  5. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. You can contact the author at :
  27. - LZ4 homepage : http://www.lz4.org
  28. - LZ4 source repository : https://github.com/lz4/lz4
  29. */
  30. #if defined (__cplusplus)
  31. extern "C" {
  32. #endif
  33. #ifndef LZ4_H_2983827168210
  34. #define LZ4_H_2983827168210
  35. /* --- Dependency --- */
  36. #include <stddef.h> /* size_t */
  37. /**
  38. Introduction
  39. LZ4 is lossless compression algorithm, providing compression speed at 500 MB/s per core,
  40. scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
  41. multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
  42. The LZ4 compression library provides in-memory compression and decompression functions.
  43. It gives full buffer control to user.
  44. Compression can be done in:
  45. - a single step (described as Simple Functions)
  46. - a single step, reusing a context (described in Advanced Functions)
  47. - unbounded multiple steps (described as Streaming compression)
  48. lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md).
  49. Decompressing a block requires additional metadata, such as its compressed size.
  50. Each application is free to encode and pass such metadata in whichever way it wants.
  51. lz4.h only handle blocks, it can not generate Frames.
  52. Blocks are different from Frames (doc/lz4_Frame_format.md).
  53. Frames bundle both blocks and metadata in a specified manner.
  54. This are required for compressed data to be self-contained and portable.
  55. Frame format is delivered through a companion API, declared in lz4frame.h.
  56. Note that the `lz4` CLI can only manage frames.
  57. */
  58. /*^***************************************************************
  59. * Export parameters
  60. *****************************************************************/
  61. /*
  62. * LZ4_DLL_EXPORT :
  63. * Enable exporting of functions when building a Windows DLL
  64. * LZ4LIB_VISIBILITY :
  65. * Control library symbols visibility.
  66. */
  67. #ifndef LZ4LIB_VISIBILITY
  68. # if defined(__GNUC__) && (__GNUC__ >= 4)
  69. # define LZ4LIB_VISIBILITY __attribute__ ((visibility ("default")))
  70. # else
  71. # define LZ4LIB_VISIBILITY
  72. # endif
  73. #endif
  74. #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1)
  75. # define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY
  76. #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1)
  77. # 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.*/
  78. #else
  79. # define LZ4LIB_API LZ4LIB_VISIBILITY
  80. #endif
  81. /*------ Version ------*/
  82. #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
  83. #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */
  84. #define LZ4_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */
  85. #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
  86. #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
  87. #define LZ4_QUOTE(str) #str
  88. #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
  89. #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION)
  90. LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version */
  91. LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version */
  92. /*-************************************
  93. * Tuning parameter
  94. **************************************/
  95. /*!
  96. * LZ4_MEMORY_USAGE :
  97. * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  98. * Increasing memory usage improves compression ratio.
  99. * Reduced memory usage may improve speed, thanks to better cache locality.
  100. * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
  101. */
  102. #ifndef LZ4_MEMORY_USAGE
  103. # define LZ4_MEMORY_USAGE 14
  104. #endif
  105. /*-************************************
  106. * Simple Functions
  107. **************************************/
  108. /*! LZ4_compress_default() :
  109. Compresses 'srcSize' bytes from buffer 'src'
  110. into already allocated 'dst' buffer of size 'dstCapacity'.
  111. Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize).
  112. It also runs faster, so it's a recommended setting.
  113. If the function cannot compress 'src' into a more limited 'dst' budget,
  114. compression stops *immediately*, and the function result is zero.
  115. In which case, 'dst' content is undefined (invalid).
  116. srcSize : max supported value is LZ4_MAX_INPUT_SIZE.
  117. dstCapacity : size of buffer 'dst' (which must be already allocated)
  118. @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity)
  119. or 0 if compression fails
  120. Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).
  121. */
  122. LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
  123. /*! LZ4_decompress_safe() :
  124. compressedSize : is the exact complete size of the compressed block.
  125. dstCapacity : is the size of destination buffer, which must be already allocated.
  126. @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
  127. If destination buffer is not large enough, decoding will stop and output an error code (negative value).
  128. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  129. Note : This function is protected against malicious data packets (never writes outside 'dst' buffer, nor read outside 'source' buffer).
  130. */
  131. LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
  132. /*-************************************
  133. * Advanced Functions
  134. **************************************/
  135. #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
  136. #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
  137. /*! LZ4_compressBound() :
  138. Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
  139. This function is primarily useful for memory allocation purposes (destination buffer size).
  140. Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
  141. Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize)
  142. inputSize : max supported value is LZ4_MAX_INPUT_SIZE
  143. return : maximum output size in a "worst case" scenario
  144. or 0, if input size is incorrect (too large or negative)
  145. */
  146. LZ4LIB_API int LZ4_compressBound(int inputSize);
  147. /*! LZ4_compress_fast() :
  148. Same as LZ4_compress_default(), but allows selection of "acceleration" factor.
  149. The larger the acceleration value, the faster the algorithm, but also the lesser the compression.
  150. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed.
  151. An acceleration value of "1" is the same as regular LZ4_compress_default()
  152. Values <= 0 will be replaced by ACCELERATION_DEFAULT (currently == 1, see lz4.c).
  153. */
  154. LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
  155. /*! LZ4_compress_fast_extState() :
  156. * Same as LZ4_compress_fast(), using an externally allocated memory space for its state.
  157. * Use LZ4_sizeofState() to know how much memory must be allocated,
  158. * and allocate it on 8-bytes boundaries (using `malloc()` typically).
  159. * Then, provide this buffer as `void* state` to compression function.
  160. */
  161. LZ4LIB_API int LZ4_sizeofState(void);
  162. LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
  163. /*! LZ4_compress_destSize() :
  164. * Reverse the logic : compresses as much data as possible from 'src' buffer
  165. * into already allocated buffer 'dst', of size >= 'targetDestSize'.
  166. * This function either compresses the entire 'src' content into 'dst' if it's large enough,
  167. * or fill 'dst' buffer completely with as much data as possible from 'src'.
  168. * note: acceleration parameter is fixed to "default".
  169. *
  170. * *srcSizePtr : will be modified to indicate how many bytes where read from 'src' to fill 'dst'.
  171. * New value is necessarily <= input value.
  172. * @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
  173. * or 0 if compression fails.
  174. */
  175. LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize);
  176. /*! LZ4_decompress_safe_partial() :
  177. * Decompress an LZ4 compressed block, of size 'srcSize' at position 'src',
  178. * into destination buffer 'dst' of size 'dstCapacity'.
  179. * Up to 'targetOutputSize' bytes will be decoded.
  180. * The function stops decoding on reaching this objective,
  181. * which can boost performance when only the beginning of a block is required.
  182. *
  183. * @return : the number of bytes decoded in `dst` (necessarily <= dstCapacity)
  184. * If source stream is detected malformed, function returns a negative result.
  185. *
  186. * Note : @return can be < targetOutputSize, if compressed block contains less data.
  187. *
  188. * Note 2 : this function features 2 parameters, targetOutputSize and dstCapacity,
  189. * and expects targetOutputSize <= dstCapacity.
  190. * It effectively stops decoding on reaching targetOutputSize,
  191. * so dstCapacity is kind of redundant.
  192. * This is because in a previous version of this function,
  193. * decoding operation would not "break" a sequence in the middle.
  194. * As a consequence, there was no guarantee that decoding would stop at exactly targetOutputSize,
  195. * it could write more bytes, though only up to dstCapacity.
  196. * Some "margin" used to be required for this operation to work properly.
  197. * This is no longer necessary.
  198. * The function nonetheless keeps its signature, in an effort to not break API.
  199. */
  200. LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
  201. /*-*********************************************
  202. * Streaming Compression Functions
  203. ***********************************************/
  204. typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
  205. LZ4LIB_API LZ4_stream_t* LZ4_createStream(void);
  206. LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr);
  207. /*! LZ4_resetStream_fast() : v1.9.0+
  208. * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks
  209. * (e.g., LZ4_compress_fast_continue()).
  210. *
  211. * An LZ4_stream_t must be initialized once before usage.
  212. * This is automatically done when created by LZ4_createStream().
  213. * However, should the LZ4_stream_t be simply declared on stack (for example),
  214. * it's necessary to initialize it first, using LZ4_initStream().
  215. *
  216. * After init, start any new stream with LZ4_resetStream_fast().
  217. * A same LZ4_stream_t can be re-used multiple times consecutively
  218. * and compress multiple streams,
  219. * provided that it starts each new stream with LZ4_resetStream_fast().
  220. *
  221. * LZ4_resetStream_fast() is much faster than LZ4_initStream(),
  222. * but is not compatible with memory regions containing garbage data.
  223. *
  224. * Note: it's only useful to call LZ4_resetStream_fast()
  225. * in the context of streaming compression.
  226. * The *extState* functions perform their own resets.
  227. * Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive.
  228. */
  229. LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
  230. /*! LZ4_loadDict() :
  231. * Use this function to reference a static dictionary into LZ4_stream_t.
  232. * The dictionary must remain available during compression.
  233. * LZ4_loadDict() triggers a reset, so any previous data will be forgotten.
  234. * The same dictionary will have to be loaded on decompression side for successful decoding.
  235. * Dictionary are useful for better compression of small data (KB range).
  236. * While LZ4 accept any input as dictionary,
  237. * results are generally better when using Zstandard's Dictionary Builder.
  238. * Loading a size of 0 is allowed, and is the same as reset.
  239. * @return : loaded dictionary size, in bytes (necessarily <= 64 KB)
  240. */
  241. LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
  242. /*! LZ4_compress_fast_continue() :
  243. * Compress 'src' content using data from previously compressed blocks, for better compression ratio.
  244. * 'dst' buffer must be already allocated.
  245. * If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster.
  246. *
  247. * @return : size of compressed block
  248. * or 0 if there is an error (typically, cannot fit into 'dst').
  249. *
  250. * Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block.
  251. * Each block has precise boundaries.
  252. * Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata.
  253. * It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together.
  254. *
  255. * Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory !
  256. *
  257. * Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB.
  258. * Make sure that buffers are separated, by at least one byte.
  259. * This construction ensures that each block only depends on previous block.
  260. *
  261. * Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB.
  262. *
  263. * Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed.
  264. */
  265. LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
  266. /*! LZ4_saveDict() :
  267. * If last 64KB data cannot be guaranteed to remain available at its current memory location,
  268. * save it into a safer place (char* safeBuffer).
  269. * This is schematically equivalent to a memcpy() followed by LZ4_loadDict(),
  270. * but is much faster, because LZ4_saveDict() doesn't need to rebuild tables.
  271. * @return : saved dictionary size in bytes (necessarily <= maxDictSize), or 0 if error.
  272. */
  273. LZ4LIB_API int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int maxDictSize);
  274. /*-**********************************************
  275. * Streaming Decompression Functions
  276. * Bufferless synchronous API
  277. ************************************************/
  278. typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */
  279. /*! LZ4_createStreamDecode() and LZ4_freeStreamDecode() :
  280. * creation / destruction of streaming decompression tracking context.
  281. * A tracking context can be re-used multiple times.
  282. */
  283. LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void);
  284. LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
  285. /*! LZ4_setStreamDecode() :
  286. * An LZ4_streamDecode_t context can be allocated once and re-used multiple times.
  287. * Use this function to start decompression of a new stream of blocks.
  288. * A dictionary can optionally be set. Use NULL or size 0 for a reset order.
  289. * Dictionary is presumed stable : it must remain accessible and unmodified during next decompression.
  290. * @return : 1 if OK, 0 if error
  291. */
  292. LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
  293. /*! LZ4_decoderRingBufferSize() : v1.8.2+
  294. * Note : in a ring buffer scenario (optional),
  295. * blocks are presumed decompressed next to each other
  296. * up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize),
  297. * at which stage it resumes from beginning of ring buffer.
  298. * When setting such a ring buffer for streaming decompression,
  299. * provides the minimum size of this ring buffer
  300. * to be compatible with any source respecting maxBlockSize condition.
  301. * @return : minimum ring buffer size,
  302. * or 0 if there is an error (invalid maxBlockSize).
  303. */
  304. LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
  305. #define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */
  306. /*! LZ4_decompress_*_continue() :
  307. * These decoding functions allow decompression of consecutive blocks in "streaming" mode.
  308. * A block is an unsplittable entity, it must be presented entirely to a decompression function.
  309. * Decompression functions only accepts one block at a time.
  310. * The last 64KB of previously decoded data *must* remain available and unmodified at the memory position where they were decoded.
  311. * If less than 64KB of data has been decoded, all the data must be present.
  312. *
  313. * Special : if decompression side sets a ring buffer, it must respect one of the following conditions :
  314. * - Decompression buffer size is _at least_ LZ4_decoderRingBufferSize(maxBlockSize).
  315. * maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes.
  316. * In which case, encoding and decoding buffers do not need to be synchronized.
  317. * Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize.
  318. * - Synchronized mode :
  319. * Decompression buffer size is _exactly_ the same as compression buffer size,
  320. * and follows exactly same update rule (block boundaries at same positions),
  321. * and decoding function is provided with exact decompressed size of each block (exception for last block of the stream),
  322. * _then_ decoding & encoding ring buffer can have any size, including small ones ( < 64 KB).
  323. * - Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes.
  324. * In which case, encoding and decoding buffers do not need to be synchronized,
  325. * and encoding ring buffer can have any size, including small ones ( < 64 KB).
  326. *
  327. * Whenever these conditions are not possible,
  328. * save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression,
  329. * then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block.
  330. */
  331. LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int srcSize, int dstCapacity);
  332. /*! LZ4_decompress_*_usingDict() :
  333. * These decoding functions work the same as
  334. * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue()
  335. * They are stand-alone, and don't need an LZ4_streamDecode_t structure.
  336. * Dictionary is presumed stable : it must remain accessible and unmodified during decompression.
  337. * Performance tip : Decompression speed can be substantially increased
  338. * when dst == dictStart + dictSize.
  339. */
  340. LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
  341. /*^*************************************
  342. * !!!!!! STATIC LINKING ONLY !!!!!!
  343. ***************************************/
  344. /*-****************************************************************************
  345. * Experimental section
  346. *
  347. * Symbols declared in this section must be considered unstable. Their
  348. * signatures or semantics may change, or they may be removed altogether in the
  349. * future. They are therefore only safe to depend on when the caller is
  350. * statically linked against the library.
  351. *
  352. * To protect against unsafe usage, not only are the declarations guarded,
  353. * the definitions are hidden by default
  354. * when building LZ4 as a shared/dynamic library.
  355. *
  356. * In order to access these declarations,
  357. * define LZ4_STATIC_LINKING_ONLY in your application
  358. * before including LZ4's headers.
  359. *
  360. * In order to make their implementations accessible dynamically, you must
  361. * define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
  362. ******************************************************************************/
  363. #ifdef LZ4_PUBLISH_STATIC_FUNCTIONS
  364. #define LZ4LIB_STATIC_API LZ4LIB_API
  365. #else
  366. #define LZ4LIB_STATIC_API
  367. #endif
  368. #ifdef LZ4_STATIC_LINKING_ONLY
  369. /*! LZ4_compress_fast_extState_fastReset() :
  370. * A variant of LZ4_compress_fast_extState().
  371. *
  372. * Using this variant avoids an expensive initialization step.
  373. * It is only safe to call if the state buffer is known to be correctly initialized already
  374. * (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized").
  375. * From a high level, the difference is that
  376. * this function initializes the provided state with a call to something like LZ4_resetStream_fast()
  377. * while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream().
  378. */
  379. LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
  380. /*! LZ4_attach_dictionary() :
  381. * This is an experimental API that allows
  382. * efficient use of a static dictionary many times.
  383. *
  384. * Rather than re-loading the dictionary buffer into a working context before
  385. * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a
  386. * working LZ4_stream_t, this function introduces a no-copy setup mechanism,
  387. * in which the working stream references the dictionary stream in-place.
  388. *
  389. * Several assumptions are made about the state of the dictionary stream.
  390. * Currently, only streams which have been prepared by LZ4_loadDict() should
  391. * be expected to work.
  392. *
  393. * Alternatively, the provided dictionaryStream may be NULL,
  394. * in which case any existing dictionary stream is unset.
  395. *
  396. * If a dictionary is provided, it replaces any pre-existing stream history.
  397. * The dictionary contents are the only history that can be referenced and
  398. * logically immediately precede the data compressed in the first subsequent
  399. * compression call.
  400. *
  401. * The dictionary will only remain attached to the working stream through the
  402. * first compression call, at the end of which it is cleared. The dictionary
  403. * stream (and source buffer) must remain in-place / accessible / unchanged
  404. * through the completion of the first compression call on the stream.
  405. */
  406. LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream);
  407. #endif
  408. /*-************************************************************
  409. * PRIVATE DEFINITIONS
  410. **************************************************************
  411. * Do not use these definitions directly.
  412. * They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
  413. * Accessing members will expose code to API and/or ABI break in future versions of the library.
  414. **************************************************************/
  415. #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
  416. #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
  417. #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
  418. #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  419. #include <stdint.h>
  420. typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
  421. struct LZ4_stream_t_internal {
  422. uint32_t hashTable[LZ4_HASH_SIZE_U32];
  423. uint32_t currentOffset;
  424. uint16_t dirty;
  425. uint16_t tableType;
  426. const uint8_t* dictionary;
  427. const LZ4_stream_t_internal* dictCtx;
  428. uint32_t dictSize;
  429. };
  430. typedef struct {
  431. const uint8_t* externalDict;
  432. size_t extDictSize;
  433. const uint8_t* prefixEnd;
  434. size_t prefixSize;
  435. } LZ4_streamDecode_t_internal;
  436. #else
  437. typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
  438. struct LZ4_stream_t_internal {
  439. unsigned int hashTable[LZ4_HASH_SIZE_U32];
  440. unsigned int currentOffset;
  441. unsigned short dirty;
  442. unsigned short tableType;
  443. const unsigned char* dictionary;
  444. const LZ4_stream_t_internal* dictCtx;
  445. unsigned int dictSize;
  446. };
  447. typedef struct {
  448. const unsigned char* externalDict;
  449. const unsigned char* prefixEnd;
  450. size_t extDictSize;
  451. size_t prefixSize;
  452. } LZ4_streamDecode_t_internal;
  453. #endif
  454. /*! LZ4_stream_t :
  455. * information structure to track an LZ4 stream.
  456. * LZ4_stream_t can also be created using LZ4_createStream(), which is recommended.
  457. * The structure definition can be convenient for static allocation
  458. * (on stack, or as part of larger structure).
  459. * Init this structure with LZ4_initStream() before first use.
  460. * note : only use this definition in association with static linking !
  461. * this definition is not API/ABI safe, and may change in a future version.
  462. */
  463. #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4 + ((sizeof(void*)==16) ? 4 : 0) /*AS-400*/ )
  464. #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
  465. union LZ4_stream_u {
  466. unsigned long long table[LZ4_STREAMSIZE_U64];
  467. LZ4_stream_t_internal internal_donotuse;
  468. } ; /* previously typedef'd to LZ4_stream_t */
  469. /*! LZ4_initStream() : v1.9.0+
  470. * An LZ4_stream_t structure must be initialized at least once.
  471. * This is automatically done when invoking LZ4_createStream(),
  472. * but it's not when the structure is simply declared on stack (for example).
  473. *
  474. * Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t.
  475. * It can also initialize any arbitrary buffer of sufficient size,
  476. * and will @return a pointer of proper type upon initialization.
  477. *
  478. * Note : initialization fails if size and alignment conditions are not respected.
  479. * In which case, the function will @return NULL.
  480. * Note2: An LZ4_stream_t structure guarantees correct alignment and size.
  481. * Note3: Before v1.9.0, use LZ4_resetStream() instead
  482. */
  483. LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size);
  484. /*! LZ4_streamDecode_t :
  485. * information structure to track an LZ4 stream during decompression.
  486. * init this structure using LZ4_setStreamDecode() before first use.
  487. * note : only use in association with static linking !
  488. * this definition is not API/ABI safe,
  489. * and may change in a future version !
  490. */
  491. #define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 2 : 0) /*AS-400*/ )
  492. #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
  493. union LZ4_streamDecode_u {
  494. unsigned long long table[LZ4_STREAMDECODESIZE_U64];
  495. LZ4_streamDecode_t_internal internal_donotuse;
  496. } ; /* previously typedef'd to LZ4_streamDecode_t */
  497. /*-************************************
  498. * Obsolete Functions
  499. **************************************/
  500. /*! Deprecation warnings
  501. *
  502. * Deprecated functions make the compiler generate a warning when invoked.
  503. * This is meant to invite users to update their source code.
  504. * Should deprecation warnings be a problem, it is generally possible to disable them,
  505. * typically with -Wno-deprecated-declarations for gcc
  506. * or _CRT_SECURE_NO_WARNINGS in Visual.
  507. *
  508. * Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS
  509. * before including the header file.
  510. */
  511. #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
  512. # define LZ4_DEPRECATED(message) /* disable deprecation warnings */
  513. #else
  514. # define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  515. # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
  516. # define LZ4_DEPRECATED(message) [[deprecated(message)]]
  517. # elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
  518. # define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
  519. # elif (LZ4_GCC_VERSION >= 301)
  520. # define LZ4_DEPRECATED(message) __attribute__((deprecated))
  521. # elif defined(_MSC_VER)
  522. # define LZ4_DEPRECATED(message) __declspec(deprecated(message))
  523. # else
  524. # pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
  525. # define LZ4_DEPRECATED(message)
  526. # endif
  527. #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
  528. /* Obsolete compression functions */
  529. LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* source, char* dest, int sourceSize);
  530. LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize);
  531. LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
  532. 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);
  533. 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);
  534. 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);
  535. /* Obsolete decompression functions */
  536. LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize);
  537. LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
  538. /* Obsolete streaming functions; degraded functionality; do not use!
  539. *
  540. * In order to perform streaming compression, these functions depended on data
  541. * that is no longer tracked in the state. They have been preserved as well as
  542. * possible: using them will still produce a correct output. However, they don't
  543. * actually retain any history between compression calls. The compression ratio
  544. * achieved will therefore be no better than compressing each chunk
  545. * independently.
  546. */
  547. LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer);
  548. LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void);
  549. LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer);
  550. LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state);
  551. /* Obsolete streaming decoding functions */
  552. LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
  553. LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
  554. /*! LZ4_decompress_fast() : **unsafe!**
  555. * These functions used to be faster than LZ4_decompress_safe(),
  556. * but it has changed, and they are now slower than LZ4_decompress_safe().
  557. * This is because LZ4_decompress_fast() doesn't know the input size,
  558. * and therefore must progress more cautiously in the input buffer to not read beyond the end of block.
  559. * On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability.
  560. * As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated.
  561. *
  562. * The last remaining LZ4_decompress_fast() specificity is that
  563. * it can decompress a block without knowing its compressed size.
  564. * Such functionality could be achieved in a more secure manner,
  565. * by also providing the maximum size of input buffer,
  566. * but it would require new prototypes, and adaptation of the implementation to this new use case.
  567. *
  568. * Parameters:
  569. * originalSize : is the uncompressed size to regenerate.
  570. * `dst` must be already allocated, its size must be >= 'originalSize' bytes.
  571. * @return : number of bytes read from source buffer (== compressed size).
  572. * The function expects to finish at block's end exactly.
  573. * If the source stream is detected malformed, the function stops decoding and returns a negative result.
  574. * note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer.
  575. * However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds.
  576. * Also, since match offsets are not validated, match reads from 'src' may underflow too.
  577. * These issues never happen if input (compressed) data is correct.
  578. * But they may happen if input data is invalid (error or intentional tampering).
  579. * As a consequence, use these functions in trusted environments with trusted data **only**.
  580. */
  581. LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead")
  582. LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
  583. LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead")
  584. LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
  585. LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead")
  586. LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
  587. /*! LZ4_resetStream() :
  588. * An LZ4_stream_t structure must be initialized at least once.
  589. * This is done with LZ4_initStream(), or LZ4_resetStream().
  590. * Consider switching to LZ4_initStream(),
  591. * invoking LZ4_resetStream() will trigger deprecation warnings in the future.
  592. */
  593. LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
  594. #endif /* LZ4_H_2983827168210 */
  595. #if defined (__cplusplus)
  596. }
  597. #endif