lizard_compress.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Lizard - Fast LZ compression algorithm
  3. Header File
  4. Copyright (C) 2011-2016, Yann Collet
  5. Copyright (C) 2016-2017, Przemyslaw Skibinski <[email protected]>
  6. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the following disclaimer
  14. in the documentation and/or other materials provided with the
  15. distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. You can contact the author at :
  28. - Lizard source repository : https://github.com/inikep/lizard
  29. */
  30. #ifndef LIZARD_H_2983
  31. #define LIZARD_H_2983
  32. #if defined (__cplusplus)
  33. extern "C" {
  34. #endif
  35. /*
  36. * lizard_compress.h provides block compression functions. It gives full buffer control to user.
  37. * Block compression functions are not-enough to send information,
  38. * since it's still necessary to provide metadata (such as compressed size),
  39. * and each application can do it in whichever way it wants.
  40. * For interoperability, there is Lizard frame specification (lizard_Frame_format.md).
  41. * A library is provided to take care of it, see lizard_frame.h.
  42. */
  43. /*^***************************************************************
  44. * Export parameters
  45. *****************************************************************/
  46. /*
  47. * LIZARD_DLL_EXPORT :
  48. * Enable exporting of functions when building a Windows DLL
  49. */
  50. #if defined(LIZARD_DLL_EXPORT) && (LIZARD_DLL_EXPORT==1)
  51. # define LIZARDLIB_API __declspec(dllexport)
  52. #elif defined(LIZARD_DLL_IMPORT) && (LIZARD_DLL_IMPORT==1)
  53. # define LIZARDLIB_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
  54. #else
  55. # define LIZARDLIB_API
  56. #endif
  57. /*-************************************
  58. * Version
  59. **************************************/
  60. #define LIZARD_VERSION_MAJOR 1 /* for breaking interface changes */
  61. #define LIZARD_VERSION_MINOR 0 /* for new (non-breaking) interface capabilities */
  62. #define LIZARD_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
  63. #define LIZARD_VERSION_NUMBER (LIZARD_VERSION_MAJOR *100*100 + LIZARD_VERSION_MINOR *100 + LIZARD_VERSION_RELEASE)
  64. int Lizard_versionNumber (void);
  65. #define LIZARD_LIB_VERSION LIZARD_VERSION_MAJOR.LIZARD_VERSION_MINOR.LIZARD_VERSION_RELEASE
  66. #define LIZARD_QUOTE(str) #str
  67. #define LIZARD_EXPAND_AND_QUOTE(str) LIZARD_QUOTE(str)
  68. #define LIZARD_VERSION_STRING LIZARD_EXPAND_AND_QUOTE(LIZARD_LIB_VERSION)
  69. const char* Lizard_versionString (void);
  70. typedef struct Lizard_stream_s Lizard_stream_t;
  71. #define LIZARD_MIN_CLEVEL 10 /* minimum compression level */
  72. #ifndef LIZARD_NO_HUFFMAN
  73. #define LIZARD_MAX_CLEVEL 49 /* maximum compression level */
  74. #else
  75. #define LIZARD_MAX_CLEVEL 29 /* maximum compression level */
  76. #endif
  77. #define LIZARD_DEFAULT_CLEVEL 17
  78. /*-************************************
  79. * Simple Functions
  80. **************************************/
  81. LIZARDLIB_API int Lizard_compress (const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
  82. /*
  83. Lizard_compress() :
  84. Compresses 'sourceSize' bytes from buffer 'source'
  85. into already allocated 'dest' buffer of size 'maxDestSize'.
  86. Compression is guaranteed to succeed if 'maxDestSize' >= Lizard_compressBound(sourceSize).
  87. It also runs faster, so it's a recommended setting.
  88. If the function cannot compress 'source' into a more limited 'dest' budget,
  89. compression stops *immediately*, and the function result is zero.
  90. As a consequence, 'dest' content is not valid.
  91. This function never writes outside 'dest' buffer, nor read outside 'source' buffer.
  92. sourceSize : Max supported value is LIZARD_MAX_INPUT_VALUE
  93. maxDestSize : full or partial size of buffer 'dest' (which must be already allocated)
  94. return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize)
  95. or 0 if compression fails
  96. */
  97. /*-************************************
  98. * Advanced Functions
  99. **************************************/
  100. #define LIZARD_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
  101. #define LIZARD_BLOCK_SIZE (1<<17)
  102. #define LIZARD_BLOCK64K_SIZE (1<<16)
  103. #define LIZARD_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LIZARD_MAX_INPUT_SIZE ? 0 : (isize) + 1 + 1 + ((isize/LIZARD_BLOCK_SIZE)+1)*4)
  104. /*!
  105. Lizard_compressBound() :
  106. Provides the maximum size that Lizard compression may output in a "worst case" scenario (input data not compressible)
  107. This function is primarily useful for memory allocation purposes (destination buffer size).
  108. Macro LIZARD_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
  109. Note that Lizard_compress() compress faster when dest buffer size is >= Lizard_compressBound(srcSize)
  110. inputSize : max supported value is LIZARD_MAX_INPUT_SIZE
  111. return : maximum output size in a "worst case" scenario
  112. or 0, if input size is too large ( > LIZARD_MAX_INPUT_SIZE)
  113. */
  114. LIZARDLIB_API int Lizard_compressBound(int inputSize);
  115. /*!
  116. Lizard_compress_extState() :
  117. Same compression function, just using an externally allocated memory space to store compression state.
  118. Use Lizard_sizeofState() to know how much memory must be allocated,
  119. and allocate it on 8-bytes boundaries (using malloc() typically).
  120. Then, provide it as 'void* state' to compression function.
  121. */
  122. LIZARDLIB_API int Lizard_sizeofState(int compressionLevel);
  123. LIZARDLIB_API int Lizard_compress_extState(void* state, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel);
  124. /*-*********************************************
  125. * Streaming Compression Functions
  126. ***********************************************/
  127. /*! Lizard_createStream() will allocate and initialize an `Lizard_stream_t` structure.
  128. * Lizard_freeStream() releases its memory.
  129. * In the context of a DLL (liblizard), please use these methods rather than the static struct.
  130. * They are more future proof, in case of a change of `Lizard_stream_t` size.
  131. */
  132. LIZARDLIB_API Lizard_stream_t* Lizard_createStream(int compressionLevel);
  133. LIZARDLIB_API int Lizard_freeStream (Lizard_stream_t* streamPtr);
  134. /*! Lizard_resetStream() :
  135. * Use this function to reset/reuse an allocated `Lizard_stream_t` structure
  136. */
  137. LIZARDLIB_API Lizard_stream_t* Lizard_resetStream (Lizard_stream_t* streamPtr, int compressionLevel);
  138. /*! Lizard_loadDict() :
  139. * Use this function to load a static dictionary into Lizard_stream.
  140. * Any previous data will be forgotten, only 'dictionary' will remain in memory.
  141. * Loading a size of 0 is allowed.
  142. * Return : dictionary size, in bytes (necessarily <= LIZARD_DICT_SIZE)
  143. */
  144. LIZARDLIB_API int Lizard_loadDict (Lizard_stream_t* streamPtr, const char* dictionary, int dictSize);
  145. /*! Lizard_compress_continue() :
  146. * Compress buffer content 'src', using data from previously compressed blocks as dictionary to improve compression ratio.
  147. * Important : Previous data blocks are assumed to still be present and unmodified !
  148. * 'dst' buffer must be already allocated.
  149. * If maxDstSize >= Lizard_compressBound(srcSize), compression is guaranteed to succeed, and runs faster.
  150. * If not, and if compressed data cannot fit into 'dst' buffer size, compression stops, and function returns a zero.
  151. */
  152. LIZARDLIB_API int Lizard_compress_continue (Lizard_stream_t* streamPtr, const char* src, char* dst, int srcSize, int maxDstSize);
  153. /*! Lizard_saveDict() :
  154. * If previously compressed data block is not guaranteed to remain available at its memory location,
  155. * save it into a safer place (char* safeBuffer).
  156. * Note : you don't need to call Lizard_loadDict() afterwards,
  157. * dictionary is immediately usable, you can therefore call Lizard_compress_continue().
  158. * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error.
  159. */
  160. LIZARDLIB_API int Lizard_saveDict (Lizard_stream_t* streamPtr, char* safeBuffer, int dictSize);
  161. #if defined (__cplusplus)
  162. }
  163. #endif
  164. #endif /* LIZARD_H_2983827168210 */