zbuff_compress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Buffered version of Zstd compression library
  3. Copyright (C) 2015-2016, Yann Collet.
  4. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other materials provided with the
  13. distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. You can contact the author at :
  26. - zstd homepage : http://www.zstd.net/
  27. */
  28. /* *************************************
  29. * Dependencies
  30. ***************************************/
  31. #include <stdlib.h>
  32. #include "error_private.h"
  33. #include "zstd_internal.h" /* MIN, ZSTD_blockHeaderSize */
  34. #include "zstd_static.h" /* ZSTD_BLOCKSIZE_MAX */
  35. #include "zbuff_static.h"
  36. /* *************************************
  37. * Constants
  38. ***************************************/
  39. static size_t const ZBUFF_endFrameSize = ZSTD_BLOCKHEADERSIZE;
  40. /*_**************************************************
  41. * Streaming compression
  42. *
  43. * A ZBUFF_CCtx object is required to track streaming operation.
  44. * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
  45. * Use ZBUFF_compressInit() to start a new compression operation.
  46. * ZBUFF_CCtx objects can be reused multiple times.
  47. *
  48. * Use ZBUFF_compressContinue() repetitively to consume your input.
  49. * *srcSizePtr and *dstCapacityPtr can be any size.
  50. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  51. * 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.
  52. * The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
  53. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
  54. * or an error code, which can be tested using ZBUFF_isError().
  55. *
  56. * ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
  57. * Note that it will not output more than *dstCapacityPtr.
  58. * Therefore, some content might still be left into its internal buffer if dst buffer is too small.
  59. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  60. * or an error code, which can be tested using ZBUFF_isError().
  61. *
  62. * ZBUFF_compressEnd() instructs to finish a frame.
  63. * It will perform a flush and write frame epilogue.
  64. * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
  65. * @return : nb of bytes still present into internal buffer (0 if it's empty)
  66. * or an error code, which can be tested using ZBUFF_isError().
  67. *
  68. * Hint : recommended buffer sizes (not compulsory)
  69. * input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
  70. * 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.
  71. * **************************************************/
  72. typedef enum { ZBUFFcs_init, ZBUFFcs_load, ZBUFFcs_flush } ZBUFF_cStage;
  73. /* *** Ressources *** */
  74. struct ZBUFF_CCtx_s {
  75. ZSTD_CCtx* zc;
  76. char* inBuff;
  77. size_t inBuffSize;
  78. size_t inToCompress;
  79. size_t inBuffPos;
  80. size_t inBuffTarget;
  81. size_t blockSize;
  82. char* outBuff;
  83. size_t outBuffSize;
  84. size_t outBuffContentSize;
  85. size_t outBuffFlushedSize;
  86. ZBUFF_cStage stage;
  87. }; /* typedef'd tp ZBUFF_CCtx within "zstd_buffered.h" */
  88. ZBUFF_CCtx* ZBUFF_createCCtx(void)
  89. {
  90. ZBUFF_CCtx* zbc = (ZBUFF_CCtx*)malloc(sizeof(ZBUFF_CCtx));
  91. if (zbc==NULL) return NULL;
  92. memset(zbc, 0, sizeof(*zbc));
  93. zbc->zc = ZSTD_createCCtx();
  94. return zbc;
  95. }
  96. size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
  97. {
  98. if (zbc==NULL) return 0; /* support free on NULL */
  99. ZSTD_freeCCtx(zbc->zc);
  100. free(zbc->inBuff);
  101. free(zbc->outBuff);
  102. free(zbc);
  103. return 0;
  104. }
  105. /* *** Initialization *** */
  106. size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
  107. const void* dict, size_t dictSize,
  108. ZSTD_parameters params, U64 pledgedSrcSize)
  109. {
  110. /* allocate buffers */
  111. { size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
  112. if (zbc->inBuffSize < neededInBuffSize) {
  113. zbc->inBuffSize = neededInBuffSize;
  114. free(zbc->inBuff); /* should not be necessary */
  115. zbc->inBuff = (char*)malloc(neededInBuffSize);
  116. if (zbc->inBuff == NULL) return ERROR(memory_allocation);
  117. }
  118. zbc->blockSize = MIN(ZSTD_BLOCKSIZE_MAX, neededInBuffSize/2);
  119. }
  120. if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) {
  121. zbc->outBuffSize = ZSTD_compressBound(zbc->blockSize)+1;
  122. free(zbc->outBuff); /* should not be necessary */
  123. zbc->outBuff = (char*)malloc(zbc->outBuffSize);
  124. if (zbc->outBuff == NULL) return ERROR(memory_allocation);
  125. }
  126. { size_t const errorCode = ZSTD_compressBegin_advanced(zbc->zc, dict, dictSize, params, pledgedSrcSize);
  127. if (ZSTD_isError(errorCode)) return errorCode; }
  128. zbc->inToCompress = 0;
  129. zbc->inBuffPos = 0;
  130. zbc->inBuffTarget = zbc->blockSize;
  131. zbc->outBuffFlushedSize = 0;
  132. zbc->stage = ZBUFFcs_load;
  133. return 0; /* ready to go */
  134. }
  135. size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
  136. {
  137. ZSTD_parameters params;
  138. params.cParams = ZSTD_getCParams(compressionLevel, 0, dictSize);
  139. params.fParams.contentSizeFlag = 0;
  140. ZSTD_adjustCParams(&params.cParams, 0, dictSize);
  141. return ZBUFF_compressInit_advanced(zbc, dict, dictSize, params, 0);
  142. }
  143. size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
  144. {
  145. return ZBUFF_compressInitDictionary(zbc, NULL, 0, compressionLevel);
  146. }
  147. /* *** Compression *** */
  148. static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc,
  149. void* dst, size_t* dstCapacityPtr,
  150. const void* src, size_t* srcSizePtr,
  151. int flush) /* aggregate : wait for full block before compressing */
  152. {
  153. U32 notDone = 1;
  154. const char* const istart = (const char*)src;
  155. const char* const iend = istart + *srcSizePtr;
  156. const char* ip = istart;
  157. char* const ostart = (char*)dst;
  158. char* const oend = ostart + *dstCapacityPtr;
  159. char* op = ostart;
  160. while (notDone) {
  161. switch(zbc->stage)
  162. {
  163. case ZBUFFcs_init: return ERROR(init_missing); /* call ZBUFF_compressInit() first ! */
  164. case ZBUFFcs_load:
  165. /* complete inBuffer */
  166. { size_t const toLoad = zbc->inBuffTarget - zbc->inBuffPos;
  167. size_t const loaded = ZBUFF_limitCopy(zbc->inBuff + zbc->inBuffPos, toLoad, ip, iend-ip);
  168. zbc->inBuffPos += loaded;
  169. ip += loaded;
  170. if ( (zbc->inBuffPos==zbc->inToCompress) || (!flush && (toLoad != loaded)) ) {
  171. notDone = 0; break; /* not enough input to get a full block : stop there, wait for more */
  172. } }
  173. /* compress current block (note : this stage cannot be stopped in the middle) */
  174. { void* cDst;
  175. size_t cSize;
  176. size_t const iSize = zbc->inBuffPos - zbc->inToCompress;
  177. size_t oSize = oend-op;
  178. if (oSize >= ZSTD_compressBound(iSize))
  179. cDst = op; /* compress directly into output buffer (avoid flush stage) */
  180. else
  181. cDst = zbc->outBuff, oSize = zbc->outBuffSize;
  182. cSize = ZSTD_compressContinue(zbc->zc, cDst, oSize, zbc->inBuff + zbc->inToCompress, iSize);
  183. if (ZSTD_isError(cSize)) return cSize;
  184. /* prepare next block */
  185. zbc->inBuffTarget = zbc->inBuffPos + zbc->blockSize;
  186. if (zbc->inBuffTarget > zbc->inBuffSize)
  187. zbc->inBuffPos = 0, zbc->inBuffTarget = zbc->blockSize; /* note : inBuffSize >= blockSize */
  188. zbc->inToCompress = zbc->inBuffPos;
  189. if (cDst == op) { op += cSize; break; } /* no need to flush */
  190. zbc->outBuffContentSize = cSize;
  191. zbc->outBuffFlushedSize = 0;
  192. zbc->stage = ZBUFFcs_flush; /* continue to flush stage */
  193. }
  194. case ZBUFFcs_flush:
  195. /* flush into dst */
  196. { size_t const toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize;
  197. size_t const flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush);
  198. op += flushed;
  199. zbc->outBuffFlushedSize += flushed;
  200. if (toFlush!=flushed) { notDone = 0; break; } /* not enough space within dst to store compressed block : stop there */
  201. zbc->outBuffContentSize = 0;
  202. zbc->outBuffFlushedSize = 0;
  203. zbc->stage = ZBUFFcs_load;
  204. break;
  205. }
  206. default:
  207. return ERROR(GENERIC); /* impossible */
  208. }
  209. }
  210. *srcSizePtr = ip - istart;
  211. *dstCapacityPtr = op - ostart;
  212. { size_t hintInSize = zbc->inBuffTarget - zbc->inBuffPos;
  213. if (hintInSize==0) hintInSize = zbc->blockSize;
  214. return hintInSize;
  215. }
  216. }
  217. size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
  218. void* dst, size_t* dstCapacityPtr,
  219. const void* src, size_t* srcSizePtr)
  220. {
  221. return ZBUFF_compressContinue_generic(zbc, dst, dstCapacityPtr, src, srcSizePtr, 0);
  222. }
  223. /* *** Finalize *** */
  224. size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
  225. {
  226. size_t srcSize = 0;
  227. ZBUFF_compressContinue_generic(zbc, dst, dstCapacityPtr, &srcSize, &srcSize, 1); /* use a valid src address instead of NULL */
  228. return zbc->outBuffContentSize - zbc->outBuffFlushedSize;
  229. }
  230. size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
  231. {
  232. BYTE* const ostart = (BYTE*)dst;
  233. BYTE* const oend = ostart + *dstCapacityPtr;
  234. BYTE* op = ostart;
  235. size_t outSize = *dstCapacityPtr;
  236. size_t epilogueSize, remaining;
  237. ZBUFF_compressFlush(zbc, dst, &outSize); /* flush any remaining inBuff */
  238. op += outSize;
  239. epilogueSize = ZSTD_compressEnd(zbc->zc, zbc->outBuff + zbc->outBuffContentSize, zbc->outBuffSize - zbc->outBuffContentSize); /* epilogue into outBuff */
  240. zbc->outBuffContentSize += epilogueSize;
  241. outSize = oend-op;
  242. zbc->stage = ZBUFFcs_flush;
  243. remaining = ZBUFF_compressFlush(zbc, op, &outSize); /* attempt to flush epilogue into dst */
  244. op += outSize;
  245. if (!remaining) zbc->stage = ZBUFFcs_init; /* close only if nothing left to flush */
  246. *dstCapacityPtr = op-ostart; /* tells how many bytes were written */
  247. return remaining;
  248. }
  249. /* *************************************
  250. * Tool functions
  251. ***************************************/
  252. size_t ZBUFF_recommendedCInSize(void) { return ZSTD_BLOCKSIZE_MAX; }
  253. size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + ZBUFF_endFrameSize; }