zbuff.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /* ***************************************************************
  11. * NOTES/WARNINGS
  12. ******************************************************************/
  13. /* The streaming API defined here is deprecated.
  14. * Consider migrating towards ZSTD_compressStream() API in `zstd.h`
  15. * See 'lib/README.md'.
  16. *****************************************************************/
  17. #if defined (__cplusplus)
  18. extern "C" {
  19. #endif
  20. #ifndef ZSTD_BUFFERED_H_23987
  21. #define ZSTD_BUFFERED_H_23987
  22. /* *************************************
  23. * Dependencies
  24. ***************************************/
  25. #include <stddef.h> /* size_t */
  26. #include "zstd.h" /* ZSTD_CStream, ZSTD_DStream, ZSTDLIB_API */
  27. /* ***************************************************************
  28. * Compiler specifics
  29. *****************************************************************/
  30. /* Deprecation warnings */
  31. /* Should these warnings be a problem,
  32. it is generally possible to disable them,
  33. typically with -Wno-deprecated-declarations for gcc
  34. or _CRT_SECURE_NO_WARNINGS in Visual.
  35. Otherwise, it's also possible to define ZBUFF_DISABLE_DEPRECATE_WARNINGS */
  36. #ifdef ZBUFF_DISABLE_DEPRECATE_WARNINGS
  37. # define ZBUFF_DEPRECATED(message) ZSTDLIB_API /* disable deprecation warnings */
  38. #else
  39. # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
  40. # define ZBUFF_DEPRECATED(message) [[deprecated(message)]] ZSTDLIB_API
  41. # elif (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
  42. # define ZBUFF_DEPRECATED(message) ZSTDLIB_API __attribute__((deprecated(message)))
  43. # elif defined(__GNUC__) && (__GNUC__ >= 3)
  44. # define ZBUFF_DEPRECATED(message) ZSTDLIB_API __attribute__((deprecated))
  45. # elif defined(_MSC_VER)
  46. # define ZBUFF_DEPRECATED(message) ZSTDLIB_API __declspec(deprecated(message))
  47. # else
  48. # pragma message("WARNING: You need to implement ZBUFF_DEPRECATED for this compiler")
  49. # define ZBUFF_DEPRECATED(message) ZSTDLIB_API
  50. # endif
  51. #endif /* ZBUFF_DISABLE_DEPRECATE_WARNINGS */
  52. /* *************************************
  53. * Streaming functions
  54. ***************************************/
  55. /* This is the easier "buffered" streaming API,
  56. * using an internal buffer to lift all restrictions on user-provided buffers
  57. * which can be any size, any place, for both input and output.
  58. * ZBUFF and ZSTD are 100% interoperable,
  59. * frames created by one can be decoded by the other one */
  60. typedef ZSTD_CStream ZBUFF_CCtx;
  61. ZBUFF_DEPRECATED("use ZSTD_createCStream") ZBUFF_CCtx* ZBUFF_createCCtx(void);
  62. ZBUFF_DEPRECATED("use ZSTD_freeCStream") size_t ZBUFF_freeCCtx(ZBUFF_CCtx* cctx);
  63. ZBUFF_DEPRECATED("use ZSTD_initCStream") size_t ZBUFF_compressInit(ZBUFF_CCtx* cctx, int compressionLevel);
  64. ZBUFF_DEPRECATED("use ZSTD_initCStream_usingDict") size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
  65. ZBUFF_DEPRECATED("use ZSTD_compressStream") size_t ZBUFF_compressContinue(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr, const void* src, size_t* srcSizePtr);
  66. ZBUFF_DEPRECATED("use ZSTD_flushStream") size_t ZBUFF_compressFlush(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr);
  67. ZBUFF_DEPRECATED("use ZSTD_endStream") size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr);
  68. /*-*************************************************
  69. * Streaming compression - howto
  70. *
  71. * A ZBUFF_CCtx object is required to track streaming operation.
  72. * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
  73. * ZBUFF_CCtx objects can be reused multiple times.
  74. *
  75. * Start by initializing ZBUF_CCtx.
  76. * Use ZBUFF_compressInit() to start a new compression operation.
  77. * Use ZBUFF_compressInitDictionary() for a compression which requires a dictionary.
  78. *
  79. * Use ZBUFF_compressContinue() repetitively to consume input stream.
  80. * *srcSizePtr and *dstCapacityPtr can be any size.
  81. * The function will report how many bytes were read or written within *srcSizePtr and *dstCapacityPtr.
  82. * Note that it may not consume the entire input, in which case it's up to the caller to present again remaining data.
  83. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each call, so save its content if it matters or change @dst .
  84. * @return : a hint to preferred nb of bytes to use as input for next function call (it's just a hint, to improve latency)
  85. * or an error code, which can be tested using ZBUFF_isError().
  86. *
  87. * At any moment, it's possible to flush whatever data remains within buffer, using ZBUFF_compressFlush().
  88. * The nb of bytes written into `dst` will be reported into *dstCapacityPtr.
  89. * Note that the function cannot output more than *dstCapacityPtr,
  90. * therefore, some content might still be left into internal buffer if *dstCapacityPtr is too small.
  91. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  92. * or an error code, which can be tested using ZBUFF_isError().
  93. *
  94. * ZBUFF_compressEnd() instructs to finish a frame.
  95. * It will perform a flush and write frame epilogue.
  96. * The epilogue is required for decoders to consider a frame completed.
  97. * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
  98. * In which case, call again ZBUFF_compressFlush() to complete the flush.
  99. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  100. * or an error code, which can be tested using ZBUFF_isError().
  101. *
  102. * Hint : _recommended buffer_ sizes (not compulsory) : ZBUFF_recommendedCInSize() / ZBUFF_recommendedCOutSize()
  103. * input : ZBUFF_recommendedCInSize==128 KB block size is the internal unit, use this value to reduce intermediate stages (better latency)
  104. * output : ZBUFF_recommendedCOutSize==ZSTD_compressBound(128 KB) + 3 + 3 : ensures it's always possible to write/flush/end a full block. Skip some buffering.
  105. * By using both, it ensures that input will be entirely consumed, and output will always contain the result, reducing intermediate buffering.
  106. * **************************************************/
  107. typedef ZSTD_DStream ZBUFF_DCtx;
  108. ZBUFF_DEPRECATED("use ZSTD_createDStream") ZBUFF_DCtx* ZBUFF_createDCtx(void);
  109. ZBUFF_DEPRECATED("use ZSTD_freeDStream") size_t ZBUFF_freeDCtx(ZBUFF_DCtx* dctx);
  110. ZBUFF_DEPRECATED("use ZSTD_initDStream") size_t ZBUFF_decompressInit(ZBUFF_DCtx* dctx);
  111. ZBUFF_DEPRECATED("use ZSTD_initDStream_usingDict") size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* dctx, const void* dict, size_t dictSize);
  112. ZBUFF_DEPRECATED("use ZSTD_decompressStream") size_t ZBUFF_decompressContinue(ZBUFF_DCtx* dctx,
  113. void* dst, size_t* dstCapacityPtr,
  114. const void* src, size_t* srcSizePtr);
  115. /*-***************************************************************************
  116. * Streaming decompression howto
  117. *
  118. * A ZBUFF_DCtx object is required to track streaming operations.
  119. * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
  120. * Use ZBUFF_decompressInit() to start a new decompression operation,
  121. * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary.
  122. * Note that ZBUFF_DCtx objects can be re-init multiple times.
  123. *
  124. * Use ZBUFF_decompressContinue() repetitively to consume your input.
  125. * *srcSizePtr and *dstCapacityPtr can be any size.
  126. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  127. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  128. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
  129. * @return : 0 when a frame is completely decoded and fully flushed,
  130. * 1 when there is still some data left within internal buffer to flush,
  131. * >1 when more data is expected, with value being a suggested next input size (it's just a hint, which helps latency),
  132. * or an error code, which can be tested using ZBUFF_isError().
  133. *
  134. * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize()
  135. * output : ZBUFF_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  136. * input : ZBUFF_recommendedDInSize == 128KB + 3;
  137. * just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  138. * *******************************************************************************/
  139. /* *************************************
  140. * Tool functions
  141. ***************************************/
  142. ZBUFF_DEPRECATED("use ZSTD_isError") unsigned ZBUFF_isError(size_t errorCode);
  143. ZBUFF_DEPRECATED("use ZSTD_getErrorName") const char* ZBUFF_getErrorName(size_t errorCode);
  144. /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
  145. * These sizes are just hints, they tend to offer better latency */
  146. ZBUFF_DEPRECATED("use ZSTD_CStreamInSize") size_t ZBUFF_recommendedCInSize(void);
  147. ZBUFF_DEPRECATED("use ZSTD_CStreamOutSize") size_t ZBUFF_recommendedCOutSize(void);
  148. ZBUFF_DEPRECATED("use ZSTD_DStreamInSize") size_t ZBUFF_recommendedDInSize(void);
  149. ZBUFF_DEPRECATED("use ZSTD_DStreamOutSize") size_t ZBUFF_recommendedDOutSize(void);
  150. #endif /* ZSTD_BUFFERED_H_23987 */
  151. #ifdef ZBUFF_STATIC_LINKING_ONLY
  152. #ifndef ZBUFF_STATIC_H_30298098432
  153. #define ZBUFF_STATIC_H_30298098432
  154. /* ====================================================================================
  155. * The definitions in this section are considered experimental.
  156. * They should never be used in association with a dynamic library, as they may change in the future.
  157. * They are provided for advanced usages.
  158. * Use them only in association with static linking.
  159. * ==================================================================================== */
  160. /*--- Dependency ---*/
  161. #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters, ZSTD_customMem */
  162. #include "zstd.h"
  163. /*--- Custom memory allocator ---*/
  164. /*! ZBUFF_createCCtx_advanced() :
  165. * Create a ZBUFF compression context using external alloc and free functions */
  166. ZBUFF_DEPRECATED("use ZSTD_createCStream_advanced") ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem);
  167. /*! ZBUFF_createDCtx_advanced() :
  168. * Create a ZBUFF decompression context using external alloc and free functions */
  169. ZBUFF_DEPRECATED("use ZSTD_createDStream_advanced") ZBUFF_DCtx* ZBUFF_createDCtx_advanced(ZSTD_customMem customMem);
  170. /*--- Advanced Streaming Initialization ---*/
  171. ZBUFF_DEPRECATED("use ZSTD_initDStream_usingDict") size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
  172. const void* dict, size_t dictSize,
  173. ZSTD_parameters params, unsigned long long pledgedSrcSize);
  174. #endif /* ZBUFF_STATIC_H_30298098432 */
  175. #endif /* ZBUFF_STATIC_LINKING_ONLY */
  176. #if defined (__cplusplus)
  177. }
  178. #endif