zbuff_decompress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * Streaming decompression howto
  38. *
  39. * A ZBUFF_DCtx object is required to track streaming operations.
  40. * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
  41. * Use ZBUFF_decompressInit() to start a new decompression operation,
  42. * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary.
  43. * Note that ZBUFF_DCtx objects can be re-init multiple times.
  44. *
  45. * Use ZBUFF_decompressContinue() repetitively to consume your input.
  46. * *srcSizePtr and *dstCapacityPtr can be any size.
  47. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
  48. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
  49. * The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change @dst.
  50. * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
  51. * or 0 when a frame is completely decoded,
  52. * or an error code, which can be tested using ZBUFF_isError().
  53. *
  54. * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize()
  55. * output : ZBUFF_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
  56. * input : ZBUFF_recommendedDInSize == 128KB + 3;
  57. * just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
  58. * *******************************************************************************/
  59. typedef enum { ZBUFFds_init, ZBUFFds_loadHeader,
  60. ZBUFFds_read, ZBUFFds_load, ZBUFFds_flush } ZBUFF_dStage;
  61. /* *** Resource management *** */
  62. struct ZBUFF_DCtx_s {
  63. ZSTD_DCtx* zd;
  64. ZSTD_frameParams fParams;
  65. ZBUFF_dStage stage;
  66. char* inBuff;
  67. size_t inBuffSize;
  68. size_t inPos;
  69. char* outBuff;
  70. size_t outBuffSize;
  71. size_t outStart;
  72. size_t outEnd;
  73. size_t blockSize;
  74. BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
  75. size_t lhSize;
  76. }; /* typedef'd to ZBUFF_DCtx within "zstd_buffered.h" */
  77. ZBUFF_DCtx* ZBUFF_createDCtx(void)
  78. {
  79. ZBUFF_DCtx* zbd = (ZBUFF_DCtx*)malloc(sizeof(ZBUFF_DCtx));
  80. if (zbd==NULL) return NULL;
  81. memset(zbd, 0, sizeof(*zbd));
  82. zbd->zd = ZSTD_createDCtx();
  83. zbd->stage = ZBUFFds_init;
  84. return zbd;
  85. }
  86. size_t ZBUFF_freeDCtx(ZBUFF_DCtx* zbd)
  87. {
  88. if (zbd==NULL) return 0; /* support free on null */
  89. ZSTD_freeDCtx(zbd->zd);
  90. free(zbd->inBuff);
  91. free(zbd->outBuff);
  92. free(zbd);
  93. return 0;
  94. }
  95. /* *** Initialization *** */
  96. size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* zbd, const void* dict, size_t dictSize)
  97. {
  98. zbd->stage = ZBUFFds_loadHeader;
  99. zbd->lhSize = zbd->inPos = zbd->outStart = zbd->outEnd = 0;
  100. return ZSTD_decompressBegin_usingDict(zbd->zd, dict, dictSize);
  101. }
  102. size_t ZBUFF_decompressInit(ZBUFF_DCtx* zbd)
  103. {
  104. return ZBUFF_decompressInitDictionary(zbd, NULL, 0);
  105. }
  106. /* *** Decompression *** */
  107. size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
  108. void* dst, size_t* dstCapacityPtr,
  109. const void* src, size_t* srcSizePtr)
  110. {
  111. const char* const istart = (const char*)src;
  112. const char* const iend = istart + *srcSizePtr;
  113. const char* ip = istart;
  114. char* const ostart = (char*)dst;
  115. char* const oend = ostart + *dstCapacityPtr;
  116. char* op = ostart;
  117. U32 notDone = 1;
  118. while (notDone) {
  119. switch(zbd->stage)
  120. {
  121. case ZBUFFds_init :
  122. return ERROR(init_missing);
  123. case ZBUFFds_loadHeader :
  124. { size_t const hSize = ZSTD_getFrameParams(&(zbd->fParams), zbd->headerBuffer, zbd->lhSize);
  125. if (hSize != 0) {
  126. size_t const toLoad = hSize - zbd->lhSize; /* if hSize!=0, hSize > zbd->lhSize */
  127. if (ZSTD_isError(hSize)) return hSize;
  128. if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */
  129. memcpy(zbd->headerBuffer + zbd->lhSize, ip, iend-ip);
  130. zbd->lhSize += iend-ip; ip = iend; notDone = 0;
  131. *dstCapacityPtr = 0;
  132. return (hSize - zbd->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
  133. }
  134. memcpy(zbd->headerBuffer + zbd->lhSize, ip, toLoad); zbd->lhSize = hSize; ip += toLoad;
  135. break;
  136. } }
  137. /* Consume header */
  138. { size_t const h1Size = ZSTD_nextSrcSizeToDecompress(zbd->zd); /* == ZSTD_frameHeaderSize_min */
  139. size_t const h1Result = ZSTD_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer, h1Size);
  140. if (ZSTD_isError(h1Result)) return h1Result;
  141. if (h1Size < zbd->lhSize) { /* long header */
  142. size_t const h2Size = ZSTD_nextSrcSizeToDecompress(zbd->zd);
  143. size_t const h2Result = ZSTD_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer+h1Size, h2Size);
  144. if (ZSTD_isError(h2Result)) return h2Result;
  145. } }
  146. /* Frame header instruct buffer sizes */
  147. { size_t const blockSize = MIN(1 << zbd->fParams.windowLog, ZSTD_BLOCKSIZE_MAX);
  148. zbd->blockSize = blockSize;
  149. if (zbd->inBuffSize < blockSize) {
  150. free(zbd->inBuff);
  151. zbd->inBuffSize = blockSize;
  152. zbd->inBuff = (char*)malloc(blockSize);
  153. if (zbd->inBuff == NULL) return ERROR(memory_allocation);
  154. }
  155. { size_t const neededOutSize = ((size_t)1 << zbd->fParams.windowLog) + blockSize;
  156. if (zbd->outBuffSize < neededOutSize) {
  157. free(zbd->outBuff);
  158. zbd->outBuffSize = neededOutSize;
  159. zbd->outBuff = (char*)malloc(neededOutSize);
  160. if (zbd->outBuff == NULL) return ERROR(memory_allocation);
  161. } } }
  162. zbd->stage = ZBUFFds_read;
  163. case ZBUFFds_read:
  164. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zbd->zd);
  165. if (neededInSize==0) { /* end of frame */
  166. zbd->stage = ZBUFFds_init;
  167. notDone = 0;
  168. break;
  169. }
  170. if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
  171. size_t const decodedSize = ZSTD_decompressContinue(zbd->zd,
  172. zbd->outBuff + zbd->outStart, zbd->outBuffSize - zbd->outStart,
  173. ip, neededInSize);
  174. if (ZSTD_isError(decodedSize)) return decodedSize;
  175. ip += neededInSize;
  176. if (!decodedSize) break; /* this was just a header */
  177. zbd->outEnd = zbd->outStart + decodedSize;
  178. zbd->stage = ZBUFFds_flush;
  179. break;
  180. }
  181. if (ip==iend) { notDone = 0; break; } /* no more input */
  182. zbd->stage = ZBUFFds_load;
  183. }
  184. case ZBUFFds_load:
  185. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zbd->zd);
  186. size_t const toLoad = neededInSize - zbd->inPos; /* should always be <= remaining space within inBuff */
  187. size_t loadedSize;
  188. if (toLoad > zbd->inBuffSize - zbd->inPos) return ERROR(corruption_detected); /* should never happen */
  189. loadedSize = ZBUFF_limitCopy(zbd->inBuff + zbd->inPos, toLoad, ip, iend-ip);
  190. ip += loadedSize;
  191. zbd->inPos += loadedSize;
  192. if (loadedSize < toLoad) { notDone = 0; break; } /* not enough input, wait for more */
  193. /* decode loaded input */
  194. { size_t const decodedSize = ZSTD_decompressContinue(zbd->zd,
  195. zbd->outBuff + zbd->outStart, zbd->outBuffSize - zbd->outStart,
  196. zbd->inBuff, neededInSize);
  197. if (ZSTD_isError(decodedSize)) return decodedSize;
  198. zbd->inPos = 0; /* input is consumed */
  199. if (!decodedSize) { zbd->stage = ZBUFFds_read; break; } /* this was just a header */
  200. zbd->outEnd = zbd->outStart + decodedSize;
  201. zbd->stage = ZBUFFds_flush;
  202. // break; /* ZBUFFds_flush follows */
  203. } }
  204. case ZBUFFds_flush:
  205. { size_t const toFlushSize = zbd->outEnd - zbd->outStart;
  206. size_t const flushedSize = ZBUFF_limitCopy(op, oend-op, zbd->outBuff + zbd->outStart, toFlushSize);
  207. op += flushedSize;
  208. zbd->outStart += flushedSize;
  209. if (flushedSize == toFlushSize) {
  210. zbd->stage = ZBUFFds_read;
  211. if (zbd->outStart + zbd->blockSize > zbd->outBuffSize)
  212. zbd->outStart = zbd->outEnd = 0;
  213. break;
  214. }
  215. /* cannot flush everything */
  216. notDone = 0;
  217. break;
  218. }
  219. default: return ERROR(GENERIC); /* impossible */
  220. } }
  221. /* result */
  222. *srcSizePtr = ip-istart;
  223. *dstCapacityPtr = op-ostart;
  224. { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbd->zd);
  225. if (nextSrcSizeHint > ZSTD_blockHeaderSize) nextSrcSizeHint+= ZSTD_blockHeaderSize; /* get following block header too */
  226. nextSrcSizeHint -= zbd->inPos; /* already loaded*/
  227. return nextSrcSizeHint;
  228. }
  229. }
  230. /* *************************************
  231. * Tool functions
  232. ***************************************/
  233. size_t ZBUFF_recommendedDInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize /* block header size*/ ; }
  234. size_t ZBUFF_recommendedDOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }