lz4.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. LZ4 - Fast LZ compression algorithm
  3. Header File
  4. Copyright (C) 2011-2014, 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 source repository : http://code.google.com/p/lz4/
  28. - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
  29. */
  30. #pragma once
  31. #if defined (__cplusplus)
  32. extern "C" {
  33. #endif
  34. /*
  35. * lz4.h provides raw compression format functions, for optimal performance and integration into programs.
  36. * If you need to generate data using an inter-operable format (respecting the framing specification),
  37. * please use lz4frame.h instead.
  38. */
  39. /**************************************
  40. Version
  41. **************************************/
  42. #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
  43. #define LZ4_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */
  44. #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
  45. #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
  46. int LZ4_versionNumber (void);
  47. /**************************************
  48. Tuning parameter
  49. **************************************/
  50. /*
  51. * LZ4_MEMORY_USAGE :
  52. * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  53. * Increasing memory usage improves compression ratio
  54. * Reduced memory usage can improve speed, due to cache effect
  55. * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
  56. */
  57. #define LZ4_MEMORY_USAGE 14
  58. /**************************************
  59. Simple Functions
  60. **************************************/
  61. int LZ4_compress (const char* source, char* dest, int sourceSize);
  62. int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize);
  63. /*
  64. LZ4_compress() :
  65. Compresses 'sourceSize' bytes from 'source' into 'dest'.
  66. Destination buffer must be already allocated,
  67. and must be sized to handle worst cases situations (input data not compressible)
  68. Worst case size evaluation is provided by function LZ4_compressBound()
  69. inputSize : Max supported value is LZ4_MAX_INPUT_SIZE
  70. return : the number of bytes written in buffer dest
  71. or 0 if the compression fails
  72. LZ4_decompress_safe() :
  73. compressedSize : is obviously the source size
  74. maxDecompressedSize : is the size of the destination buffer, which must be already allocated.
  75. return : the number of bytes decompressed into the destination buffer (necessarily <= maxDecompressedSize)
  76. If the destination buffer is not large enough, decoding will stop and output an error code (<0).
  77. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  78. This function is protected against buffer overflow exploits,
  79. and never writes outside of output buffer, nor reads outside of input buffer.
  80. It is also protected against malicious data packets.
  81. */
  82. /**************************************
  83. Advanced Functions
  84. **************************************/
  85. #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
  86. #define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
  87. /*
  88. LZ4_compressBound() :
  89. Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
  90. This function is primarily useful for memory allocation purposes (output buffer size).
  91. Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
  92. isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE
  93. return : maximum output size in a "worst case" scenario
  94. or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
  95. */
  96. int LZ4_compressBound(int isize);
  97. /*
  98. LZ4_compress_limitedOutput() :
  99. Compress 'sourceSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
  100. If it cannot achieve it, compression will stop, and result of the function will be zero.
  101. This saves time and memory on detecting non-compressible (or barely compressible) data.
  102. This function never writes outside of provided output buffer.
  103. sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE
  104. maxOutputSize : is the size of the destination buffer (which must be already allocated)
  105. return : the number of bytes written in buffer 'dest'
  106. or 0 if compression fails
  107. */
  108. int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize);
  109. /*
  110. LZ4_compress_withState() :
  111. Same compression functions, but using an externally allocated memory space to store compression state.
  112. Use LZ4_sizeofState() to know how much memory must be allocated,
  113. and then, provide it as 'void* state' to compression functions.
  114. */
  115. int LZ4_sizeofState(void);
  116. int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
  117. int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
  118. /*
  119. LZ4_decompress_fast() :
  120. originalSize : is the original and therefore uncompressed size
  121. return : the number of bytes read from the source buffer (in other words, the compressed size)
  122. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  123. Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes.
  124. note : This function fully respect memory boundaries for properly formed compressed data.
  125. It is a bit faster than LZ4_decompress_safe().
  126. However, it does not provide any protection against intentionally modified data stream (malicious input).
  127. Use this function in trusted environment only (data to decode comes from a trusted source).
  128. */
  129. int LZ4_decompress_fast (const char* source, char* dest, int originalSize);
  130. /*
  131. LZ4_decompress_safe_partial() :
  132. This function decompress a compressed block of size 'compressedSize' at position 'source'
  133. into destination buffer 'dest' of size 'maxDecompressedSize'.
  134. The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
  135. reducing decompression time.
  136. return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize)
  137. Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
  138. Always control how many bytes were decoded.
  139. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  140. This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
  141. */
  142. int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize);
  143. /***********************************************
  144. Streaming Compression Functions
  145. ***********************************************/
  146. #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
  147. #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long))
  148. /*
  149. * LZ4_stream_t
  150. * information structure to track an LZ4 stream.
  151. * important : init this structure content before first use !
  152. * note : only allocated directly the structure if you are statically linking LZ4
  153. * If you are using liblz4 as a DLL, please use below construction methods instead.
  154. */
  155. typedef struct { long long table[LZ4_STREAMSIZE_U64]; } LZ4_stream_t;
  156. /*
  157. * LZ4_resetStream
  158. * Use this function to init an allocated LZ4_stream_t structure
  159. */
  160. void LZ4_resetStream (LZ4_stream_t* LZ4_streamPtr);
  161. /*
  162. * LZ4_createStream will allocate and initialize an LZ4_stream_t structure
  163. * LZ4_freeStream releases its memory.
  164. * In the context of a DLL (liblz4), please use these methods rather than the static struct.
  165. * They are more future proof, in case of a change of LZ4_stream_t size.
  166. */
  167. LZ4_stream_t* LZ4_createStream(void);
  168. int LZ4_freeStream (LZ4_stream_t* LZ4_streamPtr);
  169. /*
  170. * LZ4_loadDict
  171. * Use this function to load a static dictionary into LZ4_stream.
  172. * Any previous data will be forgotten, only 'dictionary' will remain in memory.
  173. * Loading a size of 0 is allowed.
  174. * Return : dictionary size, in bytes (necessarily <= 64 KB)
  175. */
  176. int LZ4_loadDict (LZ4_stream_t* LZ4_streamPtr, const char* dictionary, int dictSize);
  177. /*
  178. * LZ4_compress_continue
  179. * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio
  180. * Previous data blocks are assumed to still be present at their previous location.
  181. */
  182. int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize);
  183. /*
  184. * LZ4_compress_limitedOutput_continue
  185. * Same as before, but also specify a maximum target compressed size (maxOutputSize)
  186. * If objective cannot be met, compression exits, and returns a zero.
  187. */
  188. int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
  189. /*
  190. * LZ4_saveDict
  191. * If previously compressed data block is not guaranteed to remain available at its memory location
  192. * save it into a safer place (char* safeBuffer)
  193. * Note : you don't need to call LZ4_loadDict() afterwards,
  194. * dictionary is immediately usable, you can therefore call again LZ4_compress_continue()
  195. * Return : dictionary size in bytes, or 0 if error
  196. * Note : any dictSize > 64 KB will be interpreted as 64KB.
  197. */
  198. int LZ4_saveDict (LZ4_stream_t* LZ4_streamPtr, char* safeBuffer, int dictSize);
  199. /************************************************
  200. Streaming Decompression Functions
  201. ************************************************/
  202. #define LZ4_STREAMDECODESIZE_U64 4
  203. #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
  204. typedef struct { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; } LZ4_streamDecode_t;
  205. /*
  206. * LZ4_streamDecode_t
  207. * information structure to track an LZ4 stream.
  208. * init this structure content using LZ4_setStreamDecode or memset() before first use !
  209. *
  210. * In the context of a DLL (liblz4) please prefer usage of construction methods below.
  211. * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future.
  212. * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure
  213. * LZ4_freeStreamDecode releases its memory.
  214. */
  215. LZ4_streamDecode_t* LZ4_createStreamDecode(void);
  216. int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
  217. /*
  218. * LZ4_setStreamDecode
  219. * Use this function to instruct where to find the dictionary.
  220. * Setting a size of 0 is allowed (same effect as reset).
  221. * Return : 1 if OK, 0 if error
  222. */
  223. int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
  224. /*
  225. *_continue() :
  226. These decoding functions allow decompression of multiple blocks in "streaming" mode.
  227. Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB)
  228. If this condition is not possible, save the relevant part of decoded data into a safe buffer,
  229. and indicate where is its new address using LZ4_setStreamDecode()
  230. */
  231. int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize);
  232. int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize);
  233. /*
  234. Advanced decoding functions :
  235. *_usingDict() :
  236. These decoding functions work the same as
  237. a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue()
  238. They are stand-alone and don't use nor update an LZ4_streamDecode_t structure.
  239. */
  240. int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize);
  241. int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize);
  242. /**************************************
  243. Obsolete Functions
  244. **************************************/
  245. /*
  246. Obsolete decompression functions
  247. These function names are deprecated and should no longer be used.
  248. They are only provided here for compatibility with older user programs.
  249. - LZ4_uncompress is the same as LZ4_decompress_fast
  250. - LZ4_uncompress_unknownOutputSize is the same as LZ4_decompress_safe
  251. These function prototypes are now disabled; uncomment them if you really need them.
  252. It is highly recommended to stop using these functions and migrate to newer ones */
  253. /* int LZ4_uncompress (const char* source, char* dest, int outputSize); */
  254. /* int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); */
  255. /* Obsolete streaming functions; use new streaming interface whenever possible */
  256. void* LZ4_create (const char* inputBuffer);
  257. int LZ4_sizeofStreamState(void);
  258. int LZ4_resetStreamState(void* state, const char* inputBuffer);
  259. char* LZ4_slideInputBuffer (void* state);
  260. /* Obsolete streaming decoding functions */
  261. int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int compressedSize, int maxOutputSize);
  262. int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int originalSize);
  263. #if defined (__cplusplus)
  264. }
  265. #endif