zbuff_compress.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 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. * Dependencies
  12. ***************************************/
  13. #define ZBUFF_STATIC_LINKING_ONLY
  14. #include "zbuff.h"
  15. #include "../common/error_private.h"
  16. /*-***********************************************************
  17. * Streaming compression
  18. *
  19. * A ZBUFF_CCtx object is required to track streaming operation.
  20. * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
  21. * Use ZBUFF_compressInit() to start a new compression operation.
  22. * ZBUFF_CCtx objects can be reused multiple times.
  23. *
  24. * Use ZBUFF_compressContinue() repetitively to consume your input.
  25. * *srcSizePtr and *dstCapacityPtr can be any size.
  26. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  27. * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.
  28. * The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
  29. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
  30. * or an error code, which can be tested using ZBUFF_isError().
  31. *
  32. * ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
  33. * Note that it will not output more than *dstCapacityPtr.
  34. * Therefore, some content might still be left into its internal buffer if dst buffer is too small.
  35. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  36. * or an error code, which can be tested using ZBUFF_isError().
  37. *
  38. * ZBUFF_compressEnd() instructs to finish a frame.
  39. * It will perform a flush and write frame epilogue.
  40. * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
  41. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  42. * or an error code, which can be tested using ZBUFF_isError().
  43. *
  44. * Hint : recommended buffer sizes (not compulsory)
  45. * input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
  46. * output : ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + ZBUFF_endFrameSize : ensures it's always possible to write/flush/end a full block at best speed.
  47. * ***********************************************************/
  48. ZBUFF_CCtx* ZBUFF_createCCtx(void)
  49. {
  50. return ZSTD_createCStream();
  51. }
  52. ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
  53. {
  54. return ZSTD_createCStream_advanced(customMem);
  55. }
  56. size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
  57. {
  58. return ZSTD_freeCStream(zbc);
  59. }
  60. /* ====== Initialization ====== */
  61. size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
  62. const void* dict, size_t dictSize,
  63. ZSTD_parameters params, unsigned long long pledgedSrcSize)
  64. {
  65. if (pledgedSrcSize==0) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN; /* preserve "0 == unknown" behavior */
  66. FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");
  67. FORWARD_IF_ERROR(ZSTD_CCtx_setPledgedSrcSize(zbc, pledgedSrcSize), "");
  68. FORWARD_IF_ERROR(ZSTD_checkCParams(params.cParams), "");
  69. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_windowLog, params.cParams.windowLog), "");
  70. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_hashLog, params.cParams.hashLog), "");
  71. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_chainLog, params.cParams.chainLog), "");
  72. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_searchLog, params.cParams.searchLog), "");
  73. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_minMatch, params.cParams.minMatch), "");
  74. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_targetLength, params.cParams.targetLength), "");
  75. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_strategy, params.cParams.strategy), "");
  76. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_contentSizeFlag, params.fParams.contentSizeFlag), "");
  77. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_checksumFlag, params.fParams.checksumFlag), "");
  78. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_dictIDFlag, params.fParams.noDictIDFlag), "");
  79. FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");
  80. return 0;
  81. }
  82. size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
  83. {
  84. FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");
  85. FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_compressionLevel, compressionLevel), "");
  86. FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");
  87. return 0;
  88. }
  89. size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
  90. {
  91. return ZSTD_initCStream(zbc, compressionLevel);
  92. }
  93. /* ====== Compression ====== */
  94. size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
  95. void* dst, size_t* dstCapacityPtr,
  96. const void* src, size_t* srcSizePtr)
  97. {
  98. size_t result;
  99. ZSTD_outBuffer outBuff;
  100. ZSTD_inBuffer inBuff;
  101. outBuff.dst = dst;
  102. outBuff.pos = 0;
  103. outBuff.size = *dstCapacityPtr;
  104. inBuff.src = src;
  105. inBuff.pos = 0;
  106. inBuff.size = *srcSizePtr;
  107. result = ZSTD_compressStream(zbc, &outBuff, &inBuff);
  108. *dstCapacityPtr = outBuff.pos;
  109. *srcSizePtr = inBuff.pos;
  110. return result;
  111. }
  112. /* ====== Finalize ====== */
  113. size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
  114. {
  115. size_t result;
  116. ZSTD_outBuffer outBuff;
  117. outBuff.dst = dst;
  118. outBuff.pos = 0;
  119. outBuff.size = *dstCapacityPtr;
  120. result = ZSTD_flushStream(zbc, &outBuff);
  121. *dstCapacityPtr = outBuff.pos;
  122. return result;
  123. }
  124. size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
  125. {
  126. size_t result;
  127. ZSTD_outBuffer outBuff;
  128. outBuff.dst = dst;
  129. outBuff.pos = 0;
  130. outBuff.size = *dstCapacityPtr;
  131. result = ZSTD_endStream(zbc, &outBuff);
  132. *dstCapacityPtr = outBuff.pos;
  133. return result;
  134. }
  135. /* *************************************
  136. * Tool functions
  137. ***************************************/
  138. size_t ZBUFF_recommendedCInSize(void) { return ZSTD_CStreamInSize(); }
  139. size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }