lz4.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. Version
  36. **************************************/
  37. #define LZ4_VERSION_MAJOR 1 /* for major interface/format changes */
  38. #define LZ4_VERSION_MINOR 3 /* for minor interface/format changes */
  39. #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
  40. #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
  41. int LZ4_versionNumber (void);
  42. /**************************************
  43. Tuning parameter
  44. **************************************/
  45. /*
  46. * LZ4_MEMORY_USAGE :
  47. * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  48. * Increasing memory usage improves compression ratio
  49. * Reduced memory usage can improve speed, due to cache effect
  50. * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
  51. */
  52. #define LZ4_MEMORY_USAGE 14
  53. /**************************************
  54. Simple Functions
  55. **************************************/
  56. int LZ4_compress (const char* source, char* dest, int inputSize);
  57. int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize);
  58. /*
  59. LZ4_compress() :
  60. Compresses 'inputSize' bytes from 'source' into 'dest'.
  61. Destination buffer must be already allocated,
  62. and must be sized to handle worst cases situations (input data not compressible)
  63. Worst case size evaluation is provided by function LZ4_compressBound()
  64. inputSize : Max supported value is LZ4_MAX_INPUT_SIZE
  65. return : the number of bytes written in buffer dest
  66. or 0 if the compression fails
  67. LZ4_decompress_safe() :
  68. compressedSize : is obviously the source size
  69. maxDecompressedSize : is the size of the destination buffer, which must be already allocated.
  70. return : the number of bytes decompressed into the destination buffer (necessarily <= maxDecompressedSize)
  71. If the destination buffer is not large enough, decoding will stop and output an error code (<0).
  72. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  73. This function is protected against buffer overflow exploits :
  74. it never writes outside of output buffer, and never reads outside of input buffer.
  75. Therefore, it is protected against malicious data packets.
  76. */
  77. /*
  78. Note :
  79. Should you prefer to explicitly allocate compression-table memory using your own allocation method,
  80. use the streaming functions provided below, simply reset the memory area between each call to LZ4_compress_continue()
  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 may output in a "worst case" scenario (input data not compressible)
  90. primarily useful for memory allocation of output buffer.
  91. macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation).
  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 'inputSize' 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 function never writes outside of provided output buffer.
  102. inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
  103. maxOutputSize : is the size of the destination buffer (which must be already allocated)
  104. return : the number of bytes written in buffer 'dest'
  105. or 0 if the compression fails
  106. */
  107. int LZ4_compress_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
  108. /*
  109. LZ4_compress_withState() :
  110. Same compression functions, but using an externally allocated memory space to store compression state.
  111. Use LZ4_sizeofState() to know how much memory must be allocated,
  112. and then, provide it as 'void* state' to compression functions.
  113. */
  114. int LZ4_sizeofState(void);
  115. int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
  116. int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
  117. /*
  118. LZ4_decompress_fast() :
  119. originalSize : is the original and therefore uncompressed size
  120. return : the number of bytes read from the source buffer (in other words, the compressed size)
  121. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  122. Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes.
  123. note : This function fully respect memory boundaries for properly formed compressed data.
  124. It is a bit faster than LZ4_decompress_safe().
  125. However, it does not provide any protection against intentionnally modified data stream (malicious input).
  126. Use this function in trusted environment only (data to decode comes from a trusted source).
  127. */
  128. int LZ4_decompress_fast (const char* source, char* dest, int originalSize);
  129. /*
  130. LZ4_decompress_safe_partial() :
  131. This function decompress a compressed block of size 'compressedSize' at position 'source'
  132. into destination buffer 'dest' of size 'maxDecompressedSize'.
  133. The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
  134. reducing decompression time.
  135. return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize)
  136. Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
  137. Always control how many bytes were decoded.
  138. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  139. This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
  140. */
  141. int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize);
  142. /***********************************************
  143. Experimental Streaming Compression Functions
  144. ***********************************************/
  145. #define LZ4_STREAMSIZE_U32 ((1 << (LZ4_MEMORY_USAGE-2)) + 8)
  146. #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U32 * sizeof(unsigned int))
  147. /*
  148. * LZ4_stream_t
  149. * information structure to track an LZ4 stream.
  150. * important : init this structure content before first use !
  151. */
  152. typedef struct { unsigned int table[LZ4_STREAMSIZE_U32]; } LZ4_stream_t;
  153. /*
  154. * LZ4_resetStream
  155. * Use this function to init a newly allocated LZ4_stream_t structure
  156. * You can also reset an existing LZ4_stream_t structure
  157. */
  158. void LZ4_resetStream (LZ4_stream_t* LZ4_stream);
  159. /*
  160. * If you prefer dynamic allocation methods,
  161. * LZ4_createStream will allocate and initialize an LZ4_stream_t structure
  162. * LZ4_freeStream releases its memory.
  163. */
  164. LZ4_stream_t* LZ4_createStream(void);
  165. int LZ4_freeStream (LZ4_stream_t* LZ4_stream);
  166. /*
  167. * LZ4_loadDict
  168. * Use this function to load a static dictionary into LZ4_stream.
  169. * Any previous data will be forgotten, only 'dictionary' will remain in memory.
  170. * Loading a size of 0 is allowed.
  171. * Return : 1 if OK, 0 if error
  172. */
  173. int LZ4_loadDict (LZ4_stream_t* LZ4_stream, const char* dictionary, int dictSize);
  174. /*
  175. * LZ4_compress_continue
  176. * Compress data block 'source', using blocks compressed before as dictionary to improve compression ratio
  177. * Previous data blocks are assumed to still be present at their previous location.
  178. */
  179. int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize);
  180. /*
  181. * LZ4_compress_limitedOutput_continue
  182. * Same as before, but also specify a maximum target compressed size (maxOutputSize)
  183. * If objective cannot be met, compression exits, and returns a zero.
  184. */
  185. int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize);
  186. /*
  187. * LZ4_saveDict
  188. * If previously compressed data block is not guaranteed to remain available at its memory location
  189. * save it into a safe place (char* safeBuffer)
  190. * Note : you don't need to call LZ4_loadDict() afterwards,
  191. * dictionary is immediately usable, you can therefore call again LZ4_compress_continue()
  192. * Return : 1 if OK, 0 if error
  193. * Note : any dictSize > 64 KB will be interpreted as 64KB.
  194. */
  195. int LZ4_saveDict (LZ4_stream_t* LZ4_stream, char* safeBuffer, int dictSize);
  196. /************************************************
  197. Experimental Streaming Decompression Functions
  198. ************************************************/
  199. #define LZ4_STREAMDECODESIZE_U32 4
  200. #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U32 * sizeof(unsigned int))
  201. /*
  202. * LZ4_streamDecode_t
  203. * information structure to track an LZ4 stream.
  204. * important : init this structure content using LZ4_setStreamDecode or memset() before first use !
  205. */
  206. typedef struct { unsigned int table[LZ4_STREAMDECODESIZE_U32]; } LZ4_streamDecode_t;
  207. /*
  208. * LZ4_setStreamDecode
  209. * Use this function to instruct where to find the dictionary.
  210. * This function can be used to specify a static dictionary,
  211. * or to instruct where to find some previously decoded data saved into a different memory space.
  212. * Setting a size of 0 is allowed (same effect as no dictionary).
  213. * Return : 1 if OK, 0 if error
  214. */
  215. int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
  216. /*
  217. * If you prefer dynamic allocation methods,
  218. * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure
  219. * LZ4_freeStreamDecode releases its memory.
  220. */
  221. LZ4_streamDecode_t* LZ4_createStreamDecode(void);
  222. int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
  223. /*
  224. *_continue() :
  225. These decoding functions allow decompression of multiple blocks in "streaming" mode.
  226. Previously decoded blocks must still be available at the memory position where they were decoded.
  227. If it's not possible, save the relevant part of decoded data into a safe buffer,
  228. and indicate where its new address using LZ4_setDictDecode()
  229. */
  230. int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize);
  231. int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize);
  232. /*
  233. Advanced decoding functions :
  234. *_usingDict() :
  235. These decoding functions work the same as
  236. a combination of LZ4_setDictDecode() followed by LZ4_decompress_x_continue()
  237. all together into a single function call.
  238. It doesn't use nor update an LZ4_streamDecode_t structure.
  239. */
  240. int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxOutputSize, 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 migrated 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. /*
  256. * If you prefer dynamic allocation methods,
  257. * LZ4_createStreamDecode()
  258. * provides a pointer (void*) towards an initialized LZ4_streamDecode_t structure.
  259. * LZ4_free just frees it.
  260. */
  261. /* void* LZ4_createStreamDecode(void); */
  262. /*int LZ4_free (void* LZ4_stream); yes, it's the same one as for compression */
  263. /* Obsolete streaming functions; use new streaming interface whenever possible */
  264. void* LZ4_create (const char* inputBuffer);
  265. int LZ4_sizeofStreamState(void);
  266. int LZ4_resetStreamState(void* state, const char* inputBuffer);
  267. char* LZ4_slideInputBuffer (void* state);
  268. /* Obsolete streaming decoding functions */
  269. int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int compressedSize, int maxOutputSize);
  270. int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int originalSize);
  271. #if defined (__cplusplus)
  272. }
  273. #endif