zbuff.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <contrib/libs/zstd06/renames.h>
  2. /*
  3. Buffered version of Zstd compression library
  4. Copyright (C) 2015-2016, 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. - zstd homepage : http://www.zstd.net/
  28. */
  29. #ifndef ZSTD_BUFFERED_H
  30. #define ZSTD_BUFFERED_H
  31. #if defined (__cplusplus)
  32. extern "C" {
  33. #endif
  34. /* *************************************
  35. * Dependencies
  36. ***************************************/
  37. #include <stddef.h> /* size_t */
  38. /* ***************************************************************
  39. * Compiler specifics
  40. *****************************************************************/
  41. /*!
  42. * ZSTD_DLL_EXPORT :
  43. * Enable exporting of functions when building a Windows DLL
  44. */
  45. #if defined(_WIN32) && defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
  46. # define ZSTDLIB_API __declspec(dllexport)
  47. #else
  48. # define ZSTDLIB_API
  49. #endif
  50. /* *************************************
  51. * Streaming functions
  52. ***************************************/
  53. typedef struct ZBUFF_CCtx_s ZBUFF_CCtx;
  54. ZSTDLIB_API ZBUFF_CCtx* ZBUFF_createCCtx(void);
  55. ZSTDLIB_API size_t ZBUFF_freeCCtx(ZBUFF_CCtx* cctx);
  56. ZSTDLIB_API size_t ZBUFF_compressInit(ZBUFF_CCtx* cctx, int compressionLevel);
  57. ZSTDLIB_API size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
  58. ZSTDLIB_API size_t ZBUFF_compressContinue(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr, const void* src, size_t* srcSizePtr);
  59. ZSTDLIB_API size_t ZBUFF_compressFlush(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr);
  60. ZSTDLIB_API size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr);
  61. /*-*************************************************
  62. * Streaming compression - howto
  63. *
  64. * A ZBUFF_CCtx object is required to track streaming operation.
  65. * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
  66. * ZBUFF_CCtx objects can be reused multiple times.
  67. *
  68. * Start by initializing ZBUF_CCtx.
  69. * Use ZBUFF_compressInit() to start a new compression operation.
  70. * Use ZBUFF_compressInitDictionary() for a compression which requires a dictionary.
  71. *
  72. * Use ZBUFF_compressContinue() repetitively to consume input stream.
  73. * *srcSizePtr and *dstCapacityPtr can be any size.
  74. * The function will report how many bytes were read or written within *srcSizePtr and *dstCapacityPtr.
  75. * Note that it may not consume the entire input, in which case it's up to the caller to present again remaining data.
  76. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each call, so save its content if it matters or change @dst .
  77. * @return : a hint to preferred nb of bytes to use as input for next function call (it's just a hint, to improve latency)
  78. * or an error code, which can be tested using ZBUFF_isError().
  79. *
  80. * At any moment, it's possible to flush whatever data remains within buffer, using ZBUFF_compressFlush().
  81. * The nb of bytes written into `dst` will be reported into *dstCapacityPtr.
  82. * Note that the function cannot output more than *dstCapacityPtr,
  83. * therefore, some content might still be left into internal buffer if *dstCapacityPtr is too small.
  84. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  85. * or an error code, which can be tested using ZBUFF_isError().
  86. *
  87. * ZBUFF_compressEnd() instructs to finish a frame.
  88. * It will perform a flush and write frame epilogue.
  89. * The epilogue is required for decoders to consider a frame completed.
  90. * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
  91. * In which case, call again ZBUFF_compressFlush() to complete the flush.
  92. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  93. * or an error code, which can be tested using ZBUFF_isError().
  94. *
  95. * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedCInSize / ZBUFF_recommendedCOutSize
  96. * input : ZBUFF_recommendedCInSize==128 KB block size is the internal unit, it improves latency to use this value (skipped buffering).
  97. * output : ZBUFF_recommendedCOutSize==ZSTD_compressBound(128 KB) + 3 + 3 : ensures it's always possible to write/flush/end a full block. Skip some buffering.
  98. * By using both, it ensures that input will be entirely consumed, and output will always contain the result, reducing intermediate buffering.
  99. * **************************************************/
  100. typedef struct ZBUFF_DCtx_s ZBUFF_DCtx;
  101. ZSTDLIB_API ZBUFF_DCtx* ZBUFF_createDCtx(void);
  102. ZSTDLIB_API size_t ZBUFF_freeDCtx(ZBUFF_DCtx* dctx);
  103. ZSTDLIB_API size_t ZBUFF_decompressInit(ZBUFF_DCtx* dctx);
  104. ZSTDLIB_API size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* dctx, const void* dict, size_t dictSize);
  105. ZSTDLIB_API size_t ZBUFF_decompressContinue(ZBUFF_DCtx* dctx,
  106. void* dst, size_t* dstCapacityPtr,
  107. const void* src, size_t* srcSizePtr);
  108. /*-***************************************************************************
  109. * Streaming decompression howto
  110. *
  111. * A ZBUFF_DCtx object is required to track streaming operations.
  112. * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
  113. * Use ZBUFF_decompressInit() to start a new decompression operation,
  114. * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary.
  115. * Note that ZBUFF_DCtx objects can be re-init multiple times.
  116. *
  117. * Use ZBUFF_decompressContinue() repetitively to consume your input.
  118. * *srcSizePtr and *dstCapacityPtr can be any size.
  119. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  120. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  121. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
  122. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
  123. * or 0 when a frame is completely decoded,
  124. * or an error code, which can be tested using ZBUFF_isError().
  125. *
  126. * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize()
  127. * output : ZBUFF_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  128. * input : ZBUFF_recommendedDInSize == 128KB + 3;
  129. * just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  130. * *******************************************************************************/
  131. /* *************************************
  132. * Tool functions
  133. ***************************************/
  134. ZSTDLIB_API unsigned ZBUFF_isError(size_t errorCode);
  135. ZSTDLIB_API const char* ZBUFF_getErrorName(size_t errorCode);
  136. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  137. * These sizes are just hints, they tend to offer better latency */
  138. ZSTDLIB_API size_t ZBUFF_recommendedCInSize(void);
  139. ZSTDLIB_API size_t ZBUFF_recommendedCOutSize(void);
  140. ZSTDLIB_API size_t ZBUFF_recommendedDInSize(void);
  141. ZSTDLIB_API size_t ZBUFF_recommendedDOutSize(void);
  142. #if defined (__cplusplus)
  143. }
  144. #endif
  145. #endif /* ZSTD_BUFFERED_H */