lz4.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. LZ4 - Fast LZ compression algorithm
  3. Header File
  4. Copyright (C) 2011-2013, 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://fastcompression.blogspot.com/p/lz4.html
  28. - LZ4 source repository : http://code.google.com/p/lz4/
  29. */
  30. #pragma once
  31. #if defined (__cplusplus)
  32. extern "C" {
  33. #endif
  34. //**************************************
  35. // Compiler Options
  36. //**************************************
  37. #if defined(_MSC_VER) && !defined(__cplusplus) // Visual Studio
  38. # define inline __inline // Visual C is not C99, but supports some kind of inline
  39. #endif
  40. //****************************
  41. // Simple Functions
  42. //****************************
  43. int LZ4_compress (const char* source, char* dest, int inputSize);
  44. int LZ4_decompress_safe (const char* source, char* dest, int inputSize, int maxOutputSize);
  45. /*
  46. LZ4_compress() :
  47. Compresses 'inputSize' bytes from 'source' into 'dest'.
  48. Destination buffer must be already allocated,
  49. and must be sized to handle worst cases situations (input data not compressible)
  50. Worst case size evaluation is provided by function LZ4_compressBound()
  51. inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
  52. return : the number of bytes written in buffer dest
  53. or 0 if the compression fails
  54. LZ4_decompress_safe() :
  55. maxOutputSize : is the size of the destination buffer (which must be already allocated)
  56. return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
  57. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  58. This function is protected against buffer overflow exploits (never writes outside of output buffer, and never reads outside of input buffer). Therefore, it is protected against malicious data packets
  59. */
  60. //****************************
  61. // Advanced Functions
  62. //****************************
  63. #define LZ4_MAX_INPUT_SIZE 0x7E000000 // 2 113 929 216 bytes
  64. #define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
  65. static inline int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
  66. /*
  67. LZ4_compressBound() :
  68. Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible)
  69. primarily useful for memory allocation of output buffer.
  70. inline function is recommended for the general case,
  71. macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation).
  72. isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE
  73. return : maximum output size in a "worst case" scenario
  74. or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
  75. */
  76. int LZ4_compress_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
  77. /*
  78. LZ4_compress_limitedOutput() :
  79. Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
  80. If it cannot achieve it, compression will stop, and result of the function will be zero.
  81. This function never writes outside of provided output buffer.
  82. inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
  83. maxOutputSize : is the size of the destination buffer (which must be already allocated)
  84. return : the number of bytes written in buffer 'dest'
  85. or 0 if the compression fails
  86. */
  87. int LZ4_decompress_fast (const char* source, char* dest, int outputSize);
  88. /*
  89. LZ4_decompress_fast() :
  90. outputSize : is the original (uncompressed) size
  91. return : the number of bytes read from the source buffer (in other words, the compressed size)
  92. If the source stream is malformed, the function will stop decoding and return a negative result.
  93. note : This function is a bit faster than LZ4_decompress_safe()
  94. This function never writes outside of output buffers, but may read beyond input buffer in case of malicious data packet.
  95. Use this function preferably into a trusted environment (data to decode comes from a trusted source).
  96. Destination buffer must be already allocated. Its size must be a minimum of 'outputSize' bytes.
  97. */
  98. int LZ4_decompress_safe_partial (const char* source, char* dest, int inputSize, int targetOutputSize, int maxOutputSize);
  99. /*
  100. LZ4_decompress_safe_partial() :
  101. This function decompress a compressed block of size 'inputSize' at position 'source'
  102. into output buffer 'dest' of size 'maxOutputSize'.
  103. The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
  104. reducing decompression time.
  105. return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
  106. Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
  107. Always control how many bytes were decoded.
  108. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  109. This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
  110. */
  111. //****************************
  112. // Stream Functions
  113. //****************************
  114. void* LZ4_create (const char* inputBuffer);
  115. int LZ4_compress_continue (void* LZ4_Data, const char* source, char* dest, int inputSize);
  116. int LZ4_compress_limitedOutput_continue (void* LZ4_Data, const char* source, char* dest, int inputSize, int maxOutputSize);
  117. char* LZ4_slideInputBuffer (void* LZ4_Data);
  118. int LZ4_free (void* LZ4_Data);
  119. /*
  120. These functions allow the compression of dependent blocks, where each block benefits from prior 64 KB within preceding blocks.
  121. In order to achieve this, it is necessary to start creating the LZ4 Data Structure, thanks to the function :
  122. void* LZ4_create (const char* inputBuffer);
  123. The result of the function is the (void*) pointer on the LZ4 Data Structure.
  124. This pointer will be needed in all other functions.
  125. If the pointer returned is NULL, then the allocation has failed, and compression must be aborted.
  126. The only parameter 'const char* inputBuffer' must, obviously, point at the beginning of input buffer.
  127. The input buffer must be already allocated, and size at least 192KB.
  128. 'inputBuffer' will also be the 'const char* source' of the first block.
  129. All blocks are expected to lay next to each other within the input buffer, starting from 'inputBuffer'.
  130. To compress each block, use either LZ4_compress_continue() or LZ4_compress_limitedOutput_continue().
  131. Their behavior are identical to LZ4_compress() or LZ4_compress_limitedOutput(),
  132. but require the LZ4 Data Structure as their first argument, and check that each block starts right after the previous one.
  133. If next block does not begin immediately after the previous one, the compression will fail (return 0).
  134. When it's no longer possible to lay the next block after the previous one (not enough space left into input buffer), a call to :
  135. char* LZ4_slideInputBuffer(void* LZ4_Data);
  136. must be performed. It will typically copy the latest 64KB of input at the beginning of input buffer.
  137. Note that, for this function to work properly, minimum size of an input buffer must be 192KB.
  138. ==> The memory position where the next input data block must start is provided as the result of the function.
  139. Compression can then resume, using LZ4_compress_continue() or LZ4_compress_limitedOutput_continue(), as usual.
  140. When compression is completed, a call to LZ4_free() will release the memory used by the LZ4 Data Structure.
  141. */
  142. int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int inputSize, int maxOutputSize);
  143. int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int outputSize);
  144. /*
  145. *_withPrefix64k() :
  146. These decoding functions work the same as their "normal name" versions,
  147. but can use up to 64KB of data in front of 'char* dest'.
  148. These functions are necessary to decode inter-dependant blocks.
  149. */
  150. //****************************
  151. // Obsolete Functions
  152. //****************************
  153. static inline int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); }
  154. static inline int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); }
  155. /*
  156. These functions are deprecated and should no longer be used.
  157. They are provided here for compatibility with existing user programs.
  158. */
  159. #if defined (__cplusplus)
  160. }
  161. #endif