lz4.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. Compression can be done in:
  44. - a single step (described as Simple Functions)
  45. - a single step, reusing a context (described in Advanced Functions)
  46. - unbounded multiple steps (described as Streaming compression)
  47. lz4.h provides block compression functions. It gives full buffer control to user.
  48. Decompressing an lz4-compressed block also requires metadata (such as compressed size).
  49. Each application is free to encode such metadata in whichever way it wants.
  50. An additional format, called LZ4 frame specification (doc/lz4_Frame_format.md),
  51. take care of encoding standard metadata alongside LZ4-compressed blocks.
  52. Frame format is required for interoperability.
  53. It is delivered through a companion API, declared in lz4frame.h.
  54. */
  55. /*^***************************************************************
  56. * Export parameters
  57. *****************************************************************/
  58. /*
  59. * LZ4_DLL_EXPORT :
  60. * Enable exporting of functions when building a Windows DLL
  61. * LZ4LIB_VISIBILITY :
  62. * Control library symbols visibility.
  63. */
  64. #ifndef LZ4LIB_VISIBILITY
  65. # if defined(__GNUC__) && (__GNUC__ >= 4)
  66. # define LZ4LIB_VISIBILITY __attribute__ ((visibility ("default")))
  67. # else
  68. # define LZ4LIB_VISIBILITY
  69. # endif
  70. #endif
  71. #if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1)
  72. # define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY
  73. #elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1)
  74. # 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.*/
  75. #else
  76. # define LZ4LIB_API LZ4LIB_VISIBILITY
  77. #endif
  78. /*------ Version ------*/
  79. #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
  80. #define LZ4_VERSION_MINOR 8 /* for new (non-breaking) interface capabilities */
  81. #define LZ4_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */
  82. #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
  83. #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
  84. #define LZ4_QUOTE(str) #str
  85. #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
  86. #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION)
  87. LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version */
  88. LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; unseful to check dll version */
  89. /*-************************************
  90. * Tuning parameter
  91. **************************************/
  92. /*!
  93. * LZ4_MEMORY_USAGE :
  94. * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  95. * Increasing memory usage improves compression ratio
  96. * Reduced memory usage may improve speed, thanks to cache effect
  97. * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
  98. */
  99. #ifndef LZ4_MEMORY_USAGE
  100. # define LZ4_MEMORY_USAGE 14
  101. #endif
  102. /*-************************************
  103. * Simple Functions
  104. **************************************/
  105. /*! LZ4_compress_default() :
  106. Compresses 'srcSize' bytes from buffer 'src'
  107. into already allocated 'dst' buffer of size 'dstCapacity'.
  108. Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize).
  109. It also runs faster, so it's a recommended setting.
  110. If the function cannot compress 'src' into a more limited 'dst' budget,
  111. compression stops *immediately*, and the function result is zero.
  112. Note : as a consequence, 'dst' content is not valid.
  113. Note 2 : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).
  114. srcSize : max supported value is LZ4_MAX_INPUT_SIZE.
  115. dstCapacity : size of buffer 'dst' (which must be already allocated)
  116. return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity)
  117. or 0 if compression fails */
  118. LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
  119. /*! LZ4_decompress_safe() :
  120. compressedSize : is the exact complete size of the compressed block.
  121. dstCapacity : is the size of destination buffer, which must be already allocated.
  122. return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
  123. If destination buffer is not large enough, decoding will stop and output an error code (negative value).
  124. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  125. This function is protected against malicious data packets.
  126. */
  127. LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
  128. /*-************************************
  129. * Advanced Functions
  130. **************************************/
  131. #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
  132. #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
  133. /*!
  134. LZ4_compressBound() :
  135. Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
  136. This function is primarily useful for memory allocation purposes (destination buffer size).
  137. Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
  138. Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize)
  139. inputSize : max supported value is LZ4_MAX_INPUT_SIZE
  140. return : maximum output size in a "worst case" scenario
  141. or 0, if input size is incorrect (too large or negative)
  142. */
  143. LZ4LIB_API int LZ4_compressBound(int inputSize);
  144. /*!
  145. LZ4_compress_fast() :
  146. Same as LZ4_compress_default(), but allows selection of "acceleration" factor.
  147. The larger the acceleration value, the faster the algorithm, but also the lesser the compression.
  148. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed.
  149. An acceleration value of "1" is the same as regular LZ4_compress_default()
  150. Values <= 0 will be replaced by ACCELERATION_DEFAULT (currently == 1, see lz4.c).
  151. */
  152. LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
  153. /*!
  154. LZ4_compress_fast_extState() :
  155. Same compression function, just using an externally allocated memory space to store compression state.
  156. Use LZ4_sizeofState() to know how much memory must be allocated,
  157. and allocate it on 8-bytes boundaries (using malloc() typically).
  158. Then, provide this buffer as 'void* state' to compression function.
  159. */
  160. LZ4LIB_API int LZ4_sizeofState(void);
  161. LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
  162. /*! LZ4_compress_destSize() :
  163. * Reverse the logic : compresses as much data as possible from 'src' buffer
  164. * into already allocated buffer 'dst', of size >= 'targetDestSize'.
  165. * This function either compresses the entire 'src' content into 'dst' if it's large enough,
  166. * or fill 'dst' buffer completely with as much data as possible from 'src'.
  167. * note: acceleration parameter is fixed to "default".
  168. *
  169. * *srcSizePtr : will be modified to indicate how many bytes where read from 'src' to fill 'dst'.
  170. * New value is necessarily <= input value.
  171. * @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
  172. * or 0 if compression fails.
  173. */
  174. LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize);
  175. /*! LZ4_decompress_fast() : **unsafe!**
  176. * This function used to be a bit faster than LZ4_decompress_safe(),
  177. * though situation has changed in recent versions,
  178. * and now `LZ4_decompress_safe()` can be as fast and sometimes faster than `LZ4_decompress_fast()`.
  179. * Moreover, LZ4_decompress_fast() is not protected vs malformed input, as it doesn't perform full validation of compressed data.
  180. * As a consequence, this function is no longer recommended, and may be deprecated in future versions.
  181. * It's only remaining specificity is that it can decompress data without knowing its compressed size.
  182. *
  183. * originalSize : is the uncompressed size to regenerate.
  184. * `dst` must be already allocated, its size must be >= 'originalSize' bytes.
  185. * @return : number of bytes read from source buffer (== compressed size).
  186. * If the source stream is detected malformed, the function stops decoding and returns a negative result.
  187. * note : This function requires uncompressed originalSize to be known in advance.
  188. * The function never writes past the output buffer.
  189. * However, since it doesn't know its 'src' size, it may read past the intended input.
  190. * Also, because match offsets are not validated during decoding,
  191. * reads from 'src' may underflow.
  192. * Use this function in trusted environment **only**.
  193. */
  194. LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
  195. /*! LZ4_decompress_safe_partial() :
  196. * Decompress an LZ4 compressed block, of size 'srcSize' at position 'src',
  197. * into destination buffer 'dst' of size 'dstCapacity'.
  198. * Up to 'targetOutputSize' bytes will be decoded.
  199. * The function stops decoding on reaching this objective,
  200. * which can boost performance when only the beginning of a block is required.
  201. *
  202. * @return : the number of bytes decoded in `dst` (necessarily <= dstCapacity)
  203. * If source stream is detected malformed, function returns a negative result.
  204. *
  205. * Note : @return can be < targetOutputSize, if compressed block contains less data.
  206. *
  207. * Note 2 : this function features 2 parameters, targetOutputSize and dstCapacity,
  208. * and expects targetOutputSize <= dstCapacity.
  209. * It effectively stops decoding on reaching targetOutputSize,
  210. * so dstCapacity is kind of redundant.
  211. * This is because in a previous version of this function,
  212. * decoding operation would not "break" a sequence in the middle.
  213. * As a consequence, there was no guarantee that decoding would stop at exactly targetOutputSize,
  214. * it could write more bytes, though only up to dstCapacity.
  215. * Some "margin" used to be required for this operation to work properly.
  216. * This is no longer necessary.
  217. * The function nonetheless keeps its signature, in an effort to not break API.
  218. */
  219. LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
  220. /*-*********************************************
  221. * Streaming Compression Functions
  222. ***********************************************/
  223. typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
  224. /*! LZ4_createStream() and LZ4_freeStream() :
  225. * LZ4_createStream() will allocate and initialize an `LZ4_stream_t` structure.
  226. * LZ4_freeStream() releases its memory.
  227. */
  228. LZ4LIB_API LZ4_stream_t* LZ4_createStream(void);
  229. LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr);
  230. /*! LZ4_resetStream() :
  231. * An LZ4_stream_t structure can be allocated once and re-used multiple times.
  232. * Use this function to start compressing a new stream.
  233. */
  234. LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
  235. /*! LZ4_loadDict() :
  236. * Use this function to load a static dictionary into LZ4_stream_t.
  237. * Any previous data will be forgotten, only 'dictionary' will remain in memory.
  238. * Loading a size of 0 is allowed, and is the same as reset.
  239. * @return : 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. * It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together.
  253. * Each block must be decompressed separately, calling LZ4_decompress_*() with associated metadata.
  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 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(mbs) (65536 + 14 + (mbs)) /* for static allocation; mbs 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. LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
  333. /*! LZ4_decompress_*_usingDict() :
  334. * These decoding functions work the same as
  335. * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue()
  336. * They are stand-alone, and don't need an LZ4_streamDecode_t structure.
  337. * Dictionary is presumed stable : it must remain accessible and unmodified during next decompression.
  338. */
  339. LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
  340. LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
  341. /*^**********************************************
  342. * !!!!!! STATIC LINKING ONLY !!!!!!
  343. ***********************************************/
  344. /*-************************************
  345. * Unstable declarations
  346. **************************************
  347. * Declarations in this section should be considered unstable.
  348. * Use at your own peril, etc., etc.
  349. * They may be removed in the future.
  350. * Their signatures may change.
  351. **************************************/
  352. #ifdef LZ4_STATIC_LINKING_ONLY
  353. /*! LZ4_resetStream_fast() :
  354. * Use this, like LZ4_resetStream(), to prepare a context for a new chain of
  355. * calls to a streaming API (e.g., LZ4_compress_fast_continue()).
  356. *
  357. * Note:
  358. * Using this in advance of a non- streaming-compression function is redundant,
  359. * and potentially bad for performance, since they all perform their own custom
  360. * reset internally.
  361. *
  362. * Differences from LZ4_resetStream():
  363. * When an LZ4_stream_t is known to be in a internally coherent state,
  364. * it can often be prepared for a new compression with almost no work, only
  365. * sometimes falling back to the full, expensive reset that is always required
  366. * when the stream is in an indeterminate state (i.e., the reset performed by
  367. * LZ4_resetStream()).
  368. *
  369. * LZ4_streams are guaranteed to be in a valid state when:
  370. * - returned from LZ4_createStream()
  371. * - reset by LZ4_resetStream()
  372. * - memset(stream, 0, sizeof(LZ4_stream_t)), though this is discouraged
  373. * - the stream was in a valid state and was reset by LZ4_resetStream_fast()
  374. * - the stream was in a valid state and was then used in any compression call
  375. * that returned success
  376. * - the stream was in an indeterminate state and was used in a compression
  377. * call that fully reset the state (e.g., LZ4_compress_fast_extState()) and
  378. * that returned success
  379. *
  380. * When a stream isn't known to be in a valid state, it is not safe to pass to
  381. * any fastReset or streaming function. It must first be cleansed by the full
  382. * LZ4_resetStream().
  383. */
  384. LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
  385. /*! LZ4_compress_fast_extState_fastReset() :
  386. * A variant of LZ4_compress_fast_extState().
  387. *
  388. * Using this variant avoids an expensive initialization step. It is only safe
  389. * to call if the state buffer is known to be correctly initialized already
  390. * (see above comment on LZ4_resetStream_fast() for a definition of "correctly
  391. * initialized"). From a high level, the difference is that this function
  392. * initializes the provided state with a call to something like
  393. * LZ4_resetStream_fast() while LZ4_compress_fast_extState() starts with a
  394. * call to LZ4_resetStream().
  395. */
  396. LZ4LIB_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
  397. /*! LZ4_attach_dictionary() :
  398. * This is an experimental API that allows for the efficient use of a
  399. * static dictionary many times.
  400. *
  401. * Rather than re-loading the dictionary buffer into a working context before
  402. * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a
  403. * working LZ4_stream_t, this function introduces a no-copy setup mechanism,
  404. * in which the working stream references the dictionary stream in-place.
  405. *
  406. * Several assumptions are made about the state of the dictionary stream.
  407. * Currently, only streams which have been prepared by LZ4_loadDict() should
  408. * be expected to work.
  409. *
  410. * Alternatively, the provided dictionary stream pointer may be NULL, in which
  411. * case any existing dictionary stream is unset.
  412. *
  413. * If a dictionary is provided, it replaces any pre-existing stream history.
  414. * The dictionary contents are the only history that can be referenced and
  415. * logically immediately precede the data compressed in the first subsequent
  416. * compression call.
  417. *
  418. * The dictionary will only remain attached to the working stream through the
  419. * first compression call, at the end of which it is cleared. The dictionary
  420. * stream (and source buffer) must remain in-place / accessible / unchanged
  421. * through the completion of the first compression call on the stream.
  422. */
  423. LZ4LIB_API void LZ4_attach_dictionary(LZ4_stream_t *working_stream, const LZ4_stream_t *dictionary_stream);
  424. #endif
  425. /*-************************************
  426. * Private definitions
  427. **************************************
  428. * Do not use these definitions.
  429. * They are exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
  430. * Using these definitions will expose code to API and/or ABI break in future versions of the library.
  431. **************************************/
  432. #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
  433. #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
  434. #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
  435. #if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  436. #include <stdint.h>
  437. typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
  438. struct LZ4_stream_t_internal {
  439. uint32_t hashTable[LZ4_HASH_SIZE_U32];
  440. uint32_t currentOffset;
  441. uint16_t initCheck;
  442. uint16_t tableType;
  443. const uint8_t* dictionary;
  444. const LZ4_stream_t_internal* dictCtx;
  445. uint32_t dictSize;
  446. };
  447. typedef struct {
  448. const uint8_t* externalDict;
  449. size_t extDictSize;
  450. const uint8_t* prefixEnd;
  451. size_t prefixSize;
  452. } LZ4_streamDecode_t_internal;
  453. #else
  454. typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
  455. struct LZ4_stream_t_internal {
  456. unsigned int hashTable[LZ4_HASH_SIZE_U32];
  457. unsigned int currentOffset;
  458. unsigned short initCheck;
  459. unsigned short tableType;
  460. const unsigned char* dictionary;
  461. const LZ4_stream_t_internal* dictCtx;
  462. unsigned int dictSize;
  463. };
  464. typedef struct {
  465. const unsigned char* externalDict;
  466. size_t extDictSize;
  467. const unsigned char* prefixEnd;
  468. size_t prefixSize;
  469. } LZ4_streamDecode_t_internal;
  470. #endif
  471. /*!
  472. * LZ4_stream_t :
  473. * information structure to track an LZ4 stream.
  474. * init this structure before first use.
  475. * note : only use in association with static linking !
  476. * this definition is not API/ABI safe,
  477. * it may change in a future version !
  478. */
  479. #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
  480. #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
  481. union LZ4_stream_u {
  482. unsigned long long table[LZ4_STREAMSIZE_U64];
  483. LZ4_stream_t_internal internal_donotuse;
  484. } ; /* previously typedef'd to LZ4_stream_t */
  485. /*!
  486. * LZ4_streamDecode_t :
  487. * information structure to track an LZ4 stream during decompression.
  488. * init this structure using LZ4_setStreamDecode (or memset()) before first use
  489. * note : only use in association with static linking !
  490. * this definition is not API/ABI safe,
  491. * and may change in a future version !
  492. */
  493. #define LZ4_STREAMDECODESIZE_U64 4
  494. #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
  495. union LZ4_streamDecode_u {
  496. unsigned long long table[LZ4_STREAMDECODESIZE_U64];
  497. LZ4_streamDecode_t_internal internal_donotuse;
  498. } ; /* previously typedef'd to LZ4_streamDecode_t */
  499. /*-************************************
  500. * Obsolete Functions
  501. **************************************/
  502. /*! Deprecation warnings
  503. Should deprecation warnings be a problem,
  504. it is generally possible to disable them,
  505. typically with -Wno-deprecated-declarations for gcc
  506. or _CRT_SECURE_NO_WARNINGS in Visual.
  507. Otherwise, it's also possible to define LZ4_DISABLE_DEPRECATE_WARNINGS */
  508. #ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
  509. # define LZ4_DEPRECATED(message) /* disable deprecation warnings */
  510. #else
  511. # define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  512. # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
  513. # define LZ4_DEPRECATED(message) [[deprecated(message)]]
  514. # elif (LZ4_GCC_VERSION >= 405) || defined(__clang__)
  515. # define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
  516. # elif (LZ4_GCC_VERSION >= 301)
  517. # define LZ4_DEPRECATED(message) __attribute__((deprecated))
  518. # elif defined(_MSC_VER)
  519. # define LZ4_DEPRECATED(message) __declspec(deprecated(message))
  520. # else
  521. # pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler")
  522. # define LZ4_DEPRECATED(message)
  523. # endif
  524. #endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
  525. /* Obsolete compression functions */
  526. LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* source, char* dest, int sourceSize);
  527. LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize);
  528. LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
  529. 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);
  530. 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);
  531. 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);
  532. /* Obsolete decompression functions */
  533. LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize);
  534. LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
  535. /* Obsolete streaming functions; degraded functionality; do not use!
  536. *
  537. * In order to perform streaming compression, these functions depended on data
  538. * that is no longer tracked in the state. They have been preserved as well as
  539. * possible: using them will still produce a correct output. However, they don't
  540. * actually retain any history between compression calls. The compression ratio
  541. * achieved will therefore be no better than compressing each chunk
  542. * independently.
  543. */
  544. LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer);
  545. LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void);
  546. LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer);
  547. LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state);
  548. /* Obsolete streaming decoding functions */
  549. LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
  550. LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
  551. #endif /* LZ4_H_2983827168210 */
  552. #if defined (__cplusplus)
  553. }
  554. #endif