zbuff_compress.c 5.2 KB

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