lizard_decompress.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_DECOMPRESS_H_2983
  31. #define LIZARD_DECOMPRESS_H_2983
  32. #if defined (__cplusplus)
  33. extern "C" {
  34. #endif
  35. #include "entropy/mem.h" /* U32 */
  36. /*^***************************************************************
  37. * Export parameters
  38. *****************************************************************/
  39. /*
  40. * LIZARD_DLL_EXPORT :
  41. * Enable exporting of functions when building a Windows DLL
  42. */
  43. #if defined(LIZARD_DLL_EXPORT) && (LIZARD_DLL_EXPORT==1)
  44. # define LIZARDDLIB_API __declspec(dllexport)
  45. #elif defined(LIZARD_DLL_IMPORT) && (LIZARD_DLL_IMPORT==1)
  46. # define LIZARDDLIB_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.*/
  47. #else
  48. # define LIZARDDLIB_API
  49. #endif
  50. /*-************************************
  51. * Simple Functions
  52. **************************************/
  53. /*
  54. Lizard_decompress_safe() :
  55. compressedSize : is the precise full size of the compressed block.
  56. maxDecompressedSize : is the size of destination buffer, which must be already allocated.
  57. return : the number of bytes decompressed into destination buffer (necessarily <= maxDecompressedSize)
  58. If destination buffer is not large enough, decoding will stop and output an error code (<0).
  59. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  60. This function is protected against buffer overflow exploits, including malicious data packets.
  61. It never writes outside output buffer, nor reads outside input buffer.
  62. */
  63. LIZARDDLIB_API int Lizard_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize);
  64. /*!
  65. Lizard_decompress_safe_partial() :
  66. This function decompress a compressed block of size 'compressedSize' at position 'source'
  67. into destination buffer 'dest' of size 'maxDecompressedSize'.
  68. The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
  69. reducing decompression time.
  70. return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize)
  71. Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
  72. Always control how many bytes were decoded.
  73. If the source stream is detected malformed, the function will stop decoding and return a negative result.
  74. This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
  75. */
  76. LIZARDDLIB_API int Lizard_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize);
  77. /*-**********************************************
  78. * Streaming Decompression Functions
  79. ************************************************/
  80. typedef struct {
  81. const BYTE* externalDict;
  82. size_t extDictSize;
  83. const BYTE* prefixEnd;
  84. size_t prefixSize;
  85. } Lizard_streamDecode_t;
  86. /*
  87. * Lizard_streamDecode_t
  88. * information structure to track an Lizard stream.
  89. * init this structure content using Lizard_setStreamDecode or memset() before first use !
  90. *
  91. * In the context of a DLL (liblizard) please prefer usage of construction methods below.
  92. * They are more future proof, in case of a change of Lizard_streamDecode_t size in the future.
  93. * Lizard_createStreamDecode will allocate and initialize an Lizard_streamDecode_t structure
  94. * Lizard_freeStreamDecode releases its memory.
  95. */
  96. LIZARDDLIB_API Lizard_streamDecode_t* Lizard_createStreamDecode(void);
  97. LIZARDDLIB_API int Lizard_freeStreamDecode (Lizard_streamDecode_t* Lizard_stream);
  98. /*! Lizard_setStreamDecode() :
  99. * Use this function to instruct where to find the dictionary.
  100. * Setting a size of 0 is allowed (same effect as reset).
  101. * @return : 1 if OK, 0 if error
  102. */
  103. LIZARDDLIB_API int Lizard_setStreamDecode (Lizard_streamDecode_t* Lizard_streamDecode, const char* dictionary, int dictSize);
  104. /*
  105. *_continue() :
  106. These decoding functions allow decompression of multiple blocks in "streaming" mode.
  107. Previously decoded blocks *must* remain available at the memory position where they were decoded (up to LIZARD_DICT_SIZE)
  108. In the case of a ring buffers, decoding buffer must be either :
  109. - Exactly same size as encoding buffer, with same update rule (block boundaries at same positions)
  110. In which case, the decoding & encoding ring buffer can have any size, including small ones ( < LIZARD_DICT_SIZE).
  111. - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
  112. maxBlockSize is implementation dependent. It's the maximum size you intend to compress into a single block.
  113. In which case, encoding and decoding buffers do not need to be synchronized,
  114. and encoding ring buffer can have any size, including small ones ( < LIZARD_DICT_SIZE).
  115. - _At least_ LIZARD_DICT_SIZE + 8 bytes + maxBlockSize.
  116. In which case, encoding and decoding buffers do not need to be synchronized,
  117. and encoding ring buffer can have any size, including larger than decoding buffer.
  118. Whenever these conditions are not possible, save the last LIZARD_DICT_SIZE of decoded data into a safe buffer,
  119. and indicate where it is saved using Lizard_setStreamDecode()
  120. */
  121. LIZARDDLIB_API int Lizard_decompress_safe_continue (Lizard_streamDecode_t* Lizard_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize);
  122. /*
  123. Advanced decoding functions :
  124. *_usingDict() :
  125. These decoding functions work the same as
  126. a combination of Lizard_setStreamDecode() followed by Lizard_decompress_x_continue()
  127. They are stand-alone. They don't need nor update an Lizard_streamDecode_t structure.
  128. */
  129. LIZARDDLIB_API int Lizard_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize);
  130. #if defined (__cplusplus)
  131. }
  132. #endif
  133. #endif /* LIZARD_DECOMPRESS_H_2983827168210 */