zstd_decompress.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. zstd - standard compression library
  3. Copyright (C) 2014-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. * Tuning parameters
  30. *****************************************************************/
  31. /*!
  32. * HEAPMODE :
  33. * Select how default decompression function ZSTD_decompress() will allocate memory,
  34. * in memory stack (0), or in memory heap (1, requires malloc())
  35. */
  36. #ifndef ZSTD_HEAPMODE
  37. # define ZSTD_HEAPMODE 1
  38. #endif
  39. /*!
  40. * LEGACY_SUPPORT :
  41. * if set to 1, ZSTD_decompress() can decode older formats (v0.1+)
  42. */
  43. #ifndef ZSTD_LEGACY_SUPPORT
  44. # define ZSTD_LEGACY_SUPPORT 0
  45. #endif
  46. /*-*******************************************************
  47. * Dependencies
  48. *********************************************************/
  49. #include <stdlib.h> /* calloc */
  50. #include <string.h> /* memcpy, memmove */
  51. #include <stdio.h> /* debug only : printf */
  52. #include "mem.h" /* low level memory routines */
  53. #include "zstd_internal.h"
  54. #include "fse_static.h"
  55. #include "huf_static.h"
  56. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
  57. # include "zstd_legacy.h"
  58. #endif
  59. /*-*******************************************************
  60. * Compiler specifics
  61. *********************************************************/
  62. #ifdef _MSC_VER /* Visual Studio */
  63. # define FORCE_INLINE static __forceinline
  64. # include <intrin.h> /* For Visual 2005 */
  65. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  66. # pragma warning(disable : 4324) /* disable: C4324: padded structure */
  67. #else
  68. # ifdef __GNUC__
  69. # define FORCE_INLINE static inline __attribute__((always_inline))
  70. # else
  71. # define FORCE_INLINE static inline
  72. # endif
  73. #endif
  74. /*-*************************************
  75. * Macros
  76. ***************************************/
  77. #define ZSTD_isError ERR_isError /* for inlining */
  78. #define FSE_isError ERR_isError
  79. #define HUF_isError ERR_isError
  80. /*_*******************************************************
  81. * Memory operations
  82. **********************************************************/
  83. static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
  84. /*-*************************************************************
  85. * Context management
  86. ***************************************************************/
  87. typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,
  88. ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock } ZSTD_dStage;
  89. struct ZSTD_DCtx_s
  90. {
  91. FSE_DTable LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
  92. FSE_DTable OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
  93. FSE_DTable MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
  94. unsigned hufTableX4[HUF_DTABLE_SIZE(HufLog)];
  95. const void* previousDstEnd;
  96. const void* base;
  97. const void* vBase;
  98. const void* dictEnd;
  99. size_t expected;
  100. size_t headerSize;
  101. ZSTD_frameParams fParams;
  102. blockType_t bType; /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */
  103. ZSTD_dStage stage;
  104. U32 flagRepeatTable;
  105. const BYTE* litPtr;
  106. size_t litBufSize;
  107. size_t litSize;
  108. BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH];
  109. BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
  110. }; /* typedef'd to ZSTD_DCtx within "zstd_static.h" */
  111. size_t ZSTD_sizeofDCtx (void) { return sizeof(ZSTD_DCtx); } /* non published interface */
  112. size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
  113. {
  114. dctx->expected = ZSTD_frameHeaderSize_min;
  115. dctx->stage = ZSTDds_getFrameHeaderSize;
  116. dctx->previousDstEnd = NULL;
  117. dctx->base = NULL;
  118. dctx->vBase = NULL;
  119. dctx->dictEnd = NULL;
  120. dctx->hufTableX4[0] = HufLog;
  121. dctx->flagRepeatTable = 0;
  122. return 0;
  123. }
  124. ZSTD_DCtx* ZSTD_createDCtx(void)
  125. {
  126. ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx));
  127. if (dctx==NULL) return NULL;
  128. ZSTD_decompressBegin(dctx);
  129. return dctx;
  130. }
  131. size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
  132. {
  133. free(dctx);
  134. return 0; /* reserved as a potential error code in the future */
  135. }
  136. void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
  137. {
  138. memcpy(dstDCtx, srcDCtx,
  139. sizeof(ZSTD_DCtx) - (ZSTD_BLOCKSIZE_MAX+WILDCOPY_OVERLENGTH + ZSTD_frameHeaderSize_max)); /* no need to copy workspace */
  140. }
  141. /*-*************************************************************
  142. * Decompression section
  143. ***************************************************************/
  144. /* Frame format description
  145. Frame Header - [ Block Header - Block ] - Frame End
  146. 1) Frame Header
  147. - 4 bytes - Magic Number : ZSTD_MAGICNUMBER (defined within zstd_static.h)
  148. - 1 byte - Frame Descriptor
  149. 2) Block Header
  150. - 3 bytes, starting with a 2-bits descriptor
  151. Uncompressed, Compressed, Frame End, unused
  152. 3) Block
  153. See Block Format Description
  154. 4) Frame End
  155. - 3 bytes, compatible with Block Header
  156. */
  157. /* Frame descriptor
  158. 1 byte, using :
  159. bit 0-3 : windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN (see zstd_internal.h)
  160. bit 4 : minmatch 4(0) or 3(1)
  161. bit 5 : reserved (must be zero)
  162. bit 6-7 : Frame content size : unknown, 1 byte, 2 bytes, 8 bytes
  163. Optional : content size (0, 1, 2 or 8 bytes)
  164. 0 : unknown
  165. 1 : 0-255 bytes
  166. 2 : 256 - 65535+256
  167. 8 : up to 16 exa
  168. */
  169. /* Compressed Block, format description
  170. Block = Literal Section - Sequences Section
  171. Prerequisite : size of (compressed) block, maximum size of regenerated data
  172. 1) Literal Section
  173. 1.1) Header : 1-5 bytes
  174. flags: 2 bits
  175. 00 compressed by Huff0
  176. 01 unused
  177. 10 is Raw (uncompressed)
  178. 11 is Rle
  179. Note : using 01 => Huff0 with precomputed table ?
  180. Note : delta map ? => compressed ?
  181. 1.1.1) Huff0-compressed literal block : 3-5 bytes
  182. srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream
  183. srcSize < 1 KB => 3 bytes (2-2-10-10)
  184. srcSize < 16KB => 4 bytes (2-2-14-14)
  185. else => 5 bytes (2-2-18-18)
  186. big endian convention
  187. 1.1.2) Raw (uncompressed) literal block header : 1-3 bytes
  188. size : 5 bits: (IS_RAW<<6) + (0<<4) + size
  189. 12 bits: (IS_RAW<<6) + (2<<4) + (size>>8)
  190. size&255
  191. 20 bits: (IS_RAW<<6) + (3<<4) + (size>>16)
  192. size>>8&255
  193. size&255
  194. 1.1.3) Rle (repeated single byte) literal block header : 1-3 bytes
  195. size : 5 bits: (IS_RLE<<6) + (0<<4) + size
  196. 12 bits: (IS_RLE<<6) + (2<<4) + (size>>8)
  197. size&255
  198. 20 bits: (IS_RLE<<6) + (3<<4) + (size>>16)
  199. size>>8&255
  200. size&255
  201. 1.1.4) Huff0-compressed literal block, using precomputed CTables : 3-5 bytes
  202. srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream
  203. srcSize < 1 KB => 3 bytes (2-2-10-10)
  204. srcSize < 16KB => 4 bytes (2-2-14-14)
  205. else => 5 bytes (2-2-18-18)
  206. big endian convention
  207. 1- CTable available (stored into workspace ?)
  208. 2- Small input (fast heuristic ? Full comparison ? depend on clevel ?)
  209. 1.2) Literal block content
  210. 1.2.1) Huff0 block, using sizes from header
  211. See Huff0 format
  212. 1.2.2) Huff0 block, using prepared table
  213. 1.2.3) Raw content
  214. 1.2.4) single byte
  215. 2) Sequences section
  216. TO DO
  217. */
  218. /** ZSTD_frameHeaderSize() :
  219. * srcSize must be >= ZSTD_frameHeaderSize_min.
  220. * @return : size of the Frame Header */
  221. static size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
  222. {
  223. if (srcSize < ZSTD_frameHeaderSize_min) return ERROR(srcSize_wrong);
  224. { U32 const fcsId = (((const BYTE*)src)[4]) >> 6;
  225. return ZSTD_frameHeaderSize_min + ZSTD_fcs_fieldSize[fcsId]; }
  226. }
  227. /** ZSTD_getFrameParams() :
  228. * decode Frame Header, or provide expected `srcSize`.
  229. * @return : 0, `fparamsPtr` is correctly filled,
  230. * >0, `srcSize` is too small, result is expected `srcSize`,
  231. * or an error code, which can be tested using ZSTD_isError() */
  232. size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize)
  233. {
  234. const BYTE* ip = (const BYTE*)src;
  235. if (srcSize < ZSTD_frameHeaderSize_min) return ZSTD_frameHeaderSize_min;
  236. if (MEM_readLE32(src) != ZSTD_MAGICNUMBER) return ERROR(prefix_unknown);
  237. /* ensure there is enough `srcSize` to fully read/decode frame header */
  238. { size_t const fhsize = ZSTD_frameHeaderSize(src, srcSize);
  239. if (srcSize < fhsize) return fhsize; }
  240. memset(fparamsPtr, 0, sizeof(*fparamsPtr));
  241. { BYTE const frameDesc = ip[4];
  242. fparamsPtr->windowLog = (frameDesc & 0xF) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
  243. if ((frameDesc & 0x20) != 0) return ERROR(frameParameter_unsupported); /* reserved 1 bit */
  244. switch(frameDesc >> 6) /* fcsId */
  245. {
  246. default: /* impossible */
  247. case 0 : fparamsPtr->frameContentSize = 0; break;
  248. case 1 : fparamsPtr->frameContentSize = ip[5]; break;
  249. case 2 : fparamsPtr->frameContentSize = MEM_readLE16(ip+5)+256; break;
  250. case 3 : fparamsPtr->frameContentSize = MEM_readLE64(ip+5); break;
  251. } }
  252. return 0;
  253. }
  254. /** ZSTD_decodeFrameHeader() :
  255. * `srcSize` must be the size provided by ZSTD_frameHeaderSize().
  256. * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
  257. static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* zc, const void* src, size_t srcSize)
  258. {
  259. size_t const result = ZSTD_getFrameParams(&(zc->fParams), src, srcSize);
  260. if ((MEM_32bits()) && (zc->fParams.windowLog > 25)) return ERROR(frameParameter_unsupportedBy32bits);
  261. return result;
  262. }
  263. typedef struct
  264. {
  265. blockType_t blockType;
  266. U32 origSize;
  267. } blockProperties_t;
  268. /*! ZSTD_getcBlockSize() :
  269. * Provides the size of compressed block from block header `src` */
  270. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
  271. {
  272. const BYTE* const in = (const BYTE* const)src;
  273. U32 cSize;
  274. if (srcSize < ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
  275. bpPtr->blockType = (blockType_t)((*in) >> 6);
  276. cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
  277. bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
  278. if (bpPtr->blockType == bt_end) return 0;
  279. if (bpPtr->blockType == bt_rle) return 1;
  280. return cSize;
  281. }
  282. static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  283. {
  284. if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
  285. memcpy(dst, src, srcSize);
  286. return srcSize;
  287. }
  288. /*! ZSTD_decodeLiteralsBlock() :
  289. @return : nb of bytes read from src (< srcSize ) */
  290. size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
  291. const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
  292. {
  293. const BYTE* const istart = (const BYTE*) src;
  294. /* any compressed block with literals segment must be at least this size */
  295. if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
  296. switch(istart[0]>> 6)
  297. {
  298. case IS_HUF:
  299. { size_t litSize, litCSize, singleStream=0;
  300. U32 lhSize = ((istart[0]) >> 4) & 3;
  301. if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for lhSize, + cSize (+nbSeq) */
  302. switch(lhSize)
  303. {
  304. case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
  305. /* 2 - 2 - 10 - 10 */
  306. lhSize=3;
  307. singleStream = istart[0] & 16;
  308. litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
  309. litCSize = ((istart[1] & 3) << 8) + istart[2];
  310. break;
  311. case 2:
  312. /* 2 - 2 - 14 - 14 */
  313. lhSize=4;
  314. litSize = ((istart[0] & 15) << 10) + (istart[1] << 2) + (istart[2] >> 6);
  315. litCSize = ((istart[2] & 63) << 8) + istart[3];
  316. break;
  317. case 3:
  318. /* 2 - 2 - 18 - 18 */
  319. lhSize=5;
  320. litSize = ((istart[0] & 15) << 14) + (istart[1] << 6) + (istart[2] >> 2);
  321. litCSize = ((istart[2] & 3) << 16) + (istart[3] << 8) + istart[4];
  322. break;
  323. }
  324. if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected);
  325. if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
  326. if (HUF_isError(singleStream ?
  327. HUF_decompress1X2(dctx->litBuffer, litSize, istart+lhSize, litCSize) :
  328. HUF_decompress (dctx->litBuffer, litSize, istart+lhSize, litCSize) ))
  329. return ERROR(corruption_detected);
  330. dctx->litPtr = dctx->litBuffer;
  331. dctx->litBufSize = ZSTD_BLOCKSIZE_MAX+8;
  332. dctx->litSize = litSize;
  333. return litCSize + lhSize;
  334. }
  335. case IS_PCH:
  336. { size_t litSize, litCSize;
  337. U32 lhSize = ((istart[0]) >> 4) & 3;
  338. if (lhSize != 1) /* only case supported for now : small litSize, single stream */
  339. return ERROR(corruption_detected);
  340. if (!dctx->flagRepeatTable)
  341. return ERROR(dictionary_corrupted);
  342. /* 2 - 2 - 10 - 10 */
  343. lhSize=3;
  344. litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
  345. litCSize = ((istart[1] & 3) << 8) + istart[2];
  346. { size_t const errorCode = HUF_decompress1X4_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->hufTableX4);
  347. if (HUF_isError(errorCode)) return ERROR(corruption_detected);
  348. }
  349. dctx->litPtr = dctx->litBuffer;
  350. dctx->litBufSize = ZSTD_BLOCKSIZE_MAX+WILDCOPY_OVERLENGTH;
  351. dctx->litSize = litSize;
  352. return litCSize + lhSize;
  353. }
  354. case IS_RAW:
  355. { size_t litSize;
  356. U32 lhSize = ((istart[0]) >> 4) & 3;
  357. switch(lhSize)
  358. {
  359. case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
  360. lhSize=1;
  361. litSize = istart[0] & 31;
  362. break;
  363. case 2:
  364. litSize = ((istart[0] & 15) << 8) + istart[1];
  365. break;
  366. case 3:
  367. litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
  368. break;
  369. }
  370. if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
  371. if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
  372. memcpy(dctx->litBuffer, istart+lhSize, litSize);
  373. dctx->litPtr = dctx->litBuffer;
  374. dctx->litBufSize = ZSTD_BLOCKSIZE_MAX+8;
  375. dctx->litSize = litSize;
  376. return lhSize+litSize;
  377. }
  378. /* direct reference into compressed stream */
  379. dctx->litPtr = istart+lhSize;
  380. dctx->litBufSize = srcSize-lhSize;
  381. dctx->litSize = litSize;
  382. return lhSize+litSize;
  383. }
  384. case IS_RLE:
  385. { size_t litSize;
  386. U32 lhSize = ((istart[0]) >> 4) & 3;
  387. switch(lhSize)
  388. {
  389. case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
  390. lhSize = 1;
  391. litSize = istart[0] & 31;
  392. break;
  393. case 2:
  394. litSize = ((istart[0] & 15) << 8) + istart[1];
  395. break;
  396. case 3:
  397. litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
  398. if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
  399. break;
  400. }
  401. if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected);
  402. memset(dctx->litBuffer, istart[lhSize], litSize);
  403. dctx->litPtr = dctx->litBuffer;
  404. dctx->litBufSize = ZSTD_BLOCKSIZE_MAX+WILDCOPY_OVERLENGTH;
  405. dctx->litSize = litSize;
  406. return lhSize+1;
  407. }
  408. default:
  409. return ERROR(corruption_detected); /* impossible */
  410. }
  411. }
  412. /*! ZSTD_buildSeqTable() :
  413. @return : nb bytes read from src,
  414. or an error code if it fails, testable with ZSTD_isError()
  415. */
  416. FORCE_INLINE size_t ZSTD_buildSeqTable(FSE_DTable* DTable, U32 type, U32 max, U32 maxLog,
  417. const void* src, size_t srcSize,
  418. const S16* defaultNorm, U32 defaultLog, U32 flagRepeatTable)
  419. {
  420. switch(type)
  421. {
  422. case FSE_ENCODING_RLE :
  423. if (!srcSize) return ERROR(srcSize_wrong);
  424. if ( (*(const BYTE*)src) > max) return ERROR(corruption_detected);
  425. FSE_buildDTable_rle(DTable, *(const BYTE*)src); /* if *src > max, data is corrupted */
  426. return 1;
  427. case FSE_ENCODING_RAW :
  428. FSE_buildDTable(DTable, defaultNorm, max, defaultLog);
  429. return 0;
  430. case FSE_ENCODING_STATIC:
  431. if (!flagRepeatTable) return ERROR(corruption_detected);
  432. return 0;
  433. default : /* impossible */
  434. case FSE_ENCODING_DYNAMIC :
  435. { U32 tableLog;
  436. S16 norm[MaxSeq+1];
  437. size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
  438. if (FSE_isError(headerSize)) return ERROR(corruption_detected);
  439. if (tableLog > maxLog) return ERROR(corruption_detected);
  440. FSE_buildDTable(DTable, norm, max, tableLog);
  441. return headerSize;
  442. } }
  443. }
  444. size_t ZSTD_decodeSeqHeaders(int* nbSeqPtr,
  445. FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb, U32 flagRepeatTable,
  446. const void* src, size_t srcSize)
  447. {
  448. const BYTE* const istart = (const BYTE* const)src;
  449. const BYTE* const iend = istart + srcSize;
  450. const BYTE* ip = istart;
  451. /* check */
  452. if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong);
  453. /* SeqHead */
  454. { int nbSeq = *ip++;
  455. if (!nbSeq) { *nbSeqPtr=0; return 1; }
  456. if (nbSeq > 0x7F) {
  457. if (nbSeq == 0xFF)
  458. nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
  459. else
  460. nbSeq = ((nbSeq-0x80)<<8) + *ip++;
  461. }
  462. *nbSeqPtr = nbSeq;
  463. }
  464. /* FSE table descriptors */
  465. { U32 const LLtype = *ip >> 6;
  466. U32 const Offtype = (*ip >> 4) & 3;
  467. U32 const MLtype = (*ip >> 2) & 3;
  468. ip++;
  469. /* check */
  470. if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
  471. /* Build DTables */
  472. { size_t const bhSize = ZSTD_buildSeqTable(DTableLL, LLtype, MaxLL, LLFSELog, ip, iend-ip, LL_defaultNorm, LL_defaultNormLog, flagRepeatTable);
  473. if (ZSTD_isError(bhSize)) return ERROR(corruption_detected);
  474. ip += bhSize;
  475. }
  476. { size_t const bhSize = ZSTD_buildSeqTable(DTableOffb, Offtype, MaxOff, OffFSELog, ip, iend-ip, OF_defaultNorm, OF_defaultNormLog, flagRepeatTable);
  477. if (ZSTD_isError(bhSize)) return ERROR(corruption_detected);
  478. ip += bhSize;
  479. }
  480. { size_t const bhSize = ZSTD_buildSeqTable(DTableML, MLtype, MaxML, MLFSELog, ip, iend-ip, ML_defaultNorm, ML_defaultNormLog, flagRepeatTable);
  481. if (ZSTD_isError(bhSize)) return ERROR(corruption_detected);
  482. ip += bhSize;
  483. } }
  484. return ip-istart;
  485. }
  486. typedef struct {
  487. size_t litLength;
  488. size_t matchLength;
  489. size_t offset;
  490. } seq_t;
  491. typedef struct {
  492. BIT_DStream_t DStream;
  493. FSE_DState_t stateLL;
  494. FSE_DState_t stateOffb;
  495. FSE_DState_t stateML;
  496. size_t prevOffset[ZSTD_REP_INIT];
  497. } seqState_t;
  498. static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
  499. {
  500. /* Literal length */
  501. U32 const llCode = FSE_peekSymbol(&(seqState->stateLL));
  502. U32 const mlCode = FSE_peekSymbol(&(seqState->stateML));
  503. U32 const ofCode = FSE_peekSymbol(&(seqState->stateOffb)); /* <= maxOff, by table construction */
  504. U32 const llBits = LL_bits[llCode];
  505. U32 const mlBits = ML_bits[mlCode];
  506. U32 const ofBits = ofCode;
  507. U32 const totalBits = llBits+mlBits+ofBits;
  508. static const U32 LL_base[MaxLL+1] = {
  509. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  510. 16, 18, 20, 22, 24, 28, 32, 40, 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
  511. 0x2000, 0x4000, 0x8000, 0x10000 };
  512. static const U32 ML_base[MaxML+1] = {
  513. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  514. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  515. 32, 34, 36, 38, 40, 44, 48, 56, 64, 80, 96, 0x80, 0x100, 0x200, 0x400, 0x800,
  516. 0x1000, 0x2000, 0x4000, 0x8000, 0x10000 };
  517. static const U32 OF_base[MaxOff+1] = {
  518. 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F,
  519. 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF,
  520. 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
  521. 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, /*fake*/ 1, 1 };
  522. /* sequence */
  523. { size_t offset;
  524. if (!ofCode)
  525. offset = 0;
  526. else {
  527. offset = OF_base[ofCode] + BIT_readBits(&(seqState->DStream), ofBits); /* <= 26 bits */
  528. if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
  529. }
  530. if (offset < ZSTD_REP_NUM) {
  531. if (llCode == 0 && offset <= 1) offset = 1-offset;
  532. if (offset != 0) {
  533. size_t temp = seqState->prevOffset[offset];
  534. if (offset != 1) {
  535. seqState->prevOffset[2] = seqState->prevOffset[1];
  536. }
  537. seqState->prevOffset[1] = seqState->prevOffset[0];
  538. seqState->prevOffset[0] = offset = temp;
  539. } else {
  540. offset = seqState->prevOffset[0];
  541. }
  542. } else {
  543. offset -= ZSTD_REP_MOVE;
  544. seqState->prevOffset[2] = seqState->prevOffset[1];
  545. seqState->prevOffset[1] = seqState->prevOffset[0];
  546. seqState->prevOffset[0] = offset;
  547. }
  548. seq->offset = offset;
  549. }
  550. seq->matchLength = ML_base[mlCode] + MINMATCH + ((mlCode>31) ? BIT_readBits(&(seqState->DStream), mlBits) : 0); /* <= 16 bits */
  551. if (MEM_32bits() && (mlBits+llBits>24)) BIT_reloadDStream(&(seqState->DStream));
  552. seq->litLength = LL_base[llCode] + ((llCode>15) ? BIT_readBits(&(seqState->DStream), llBits) : 0); /* <= 16 bits */
  553. if (MEM_32bits() ||
  554. (totalBits > 64 - 7 - (LLFSELog+MLFSELog+OffFSELog)) ) BIT_reloadDStream(&(seqState->DStream));
  555. /* ANS state update */
  556. FSE_updateState(&(seqState->stateLL), &(seqState->DStream)); /* <= 9 bits */
  557. FSE_updateState(&(seqState->stateML), &(seqState->DStream)); /* <= 9 bits */
  558. if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream)); /* <= 18 bits */
  559. FSE_updateState(&(seqState->stateOffb), &(seqState->DStream)); /* <= 8 bits */
  560. }
  561. FORCE_INLINE
  562. size_t ZSTD_execSequence(BYTE* op,
  563. BYTE* const oend, seq_t sequence,
  564. const BYTE** litPtr, const BYTE* const litLimit_8,
  565. const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
  566. {
  567. BYTE* const oLitEnd = op + sequence.litLength;
  568. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  569. BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
  570. BYTE* const oend_8 = oend-8;
  571. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  572. const BYTE* match = oLitEnd - sequence.offset;
  573. /* check */
  574. if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
  575. if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
  576. if (iLitEnd > litLimit_8) return ERROR(corruption_detected); /* over-read beyond lit buffer */
  577. /* copy Literals */
  578. ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
  579. op = oLitEnd;
  580. *litPtr = iLitEnd; /* update for next sequence */
  581. /* copy Match */
  582. if (sequence.offset > (size_t)(oLitEnd - base)) {
  583. /* offset beyond prefix */
  584. if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected);
  585. match = dictEnd - (base-match);
  586. if (match + sequence.matchLength <= dictEnd) {
  587. memmove(oLitEnd, match, sequence.matchLength);
  588. return sequenceLength;
  589. }
  590. /* span extDict & currentPrefixSegment */
  591. { size_t const length1 = dictEnd - match;
  592. memmove(oLitEnd, match, length1);
  593. op = oLitEnd + length1;
  594. sequence.matchLength -= length1;
  595. match = base;
  596. } }
  597. /* match within prefix */
  598. if (sequence.offset < 8) {
  599. /* close range match, overlap */
  600. static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
  601. static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */
  602. int const sub2 = dec64table[sequence.offset];
  603. op[0] = match[0];
  604. op[1] = match[1];
  605. op[2] = match[2];
  606. op[3] = match[3];
  607. match += dec32table[sequence.offset];
  608. ZSTD_copy4(op+4, match);
  609. match -= sub2;
  610. } else {
  611. ZSTD_copy8(op, match);
  612. }
  613. op += 8; match += 8;
  614. if (oMatchEnd > oend-(16-MINMATCH)) {
  615. if (op < oend_8) {
  616. ZSTD_wildcopy(op, match, oend_8 - op);
  617. match += oend_8 - op;
  618. op = oend_8;
  619. }
  620. while (op < oMatchEnd) *op++ = *match++;
  621. } else {
  622. ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
  623. }
  624. return sequenceLength;
  625. }
  626. static size_t ZSTD_decompressSequences(
  627. ZSTD_DCtx* dctx,
  628. void* dst, size_t maxDstSize,
  629. const void* seqStart, size_t seqSize)
  630. {
  631. const BYTE* ip = (const BYTE*)seqStart;
  632. const BYTE* const iend = ip + seqSize;
  633. BYTE* const ostart = (BYTE* const)dst;
  634. BYTE* const oend = ostart + maxDstSize;
  635. BYTE* op = ostart;
  636. const BYTE* litPtr = dctx->litPtr;
  637. const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8;
  638. const BYTE* const litEnd = litPtr + dctx->litSize;
  639. FSE_DTable* DTableLL = dctx->LLTable;
  640. FSE_DTable* DTableML = dctx->MLTable;
  641. FSE_DTable* DTableOffb = dctx->OffTable;
  642. const BYTE* const base = (const BYTE*) (dctx->base);
  643. const BYTE* const vBase = (const BYTE*) (dctx->vBase);
  644. const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
  645. int nbSeq;
  646. /* Build Decoding Tables */
  647. { size_t const seqHSize = ZSTD_decodeSeqHeaders(&nbSeq, DTableLL, DTableML, DTableOffb, dctx->flagRepeatTable, ip, seqSize);
  648. if (ZSTD_isError(seqHSize)) return seqHSize;
  649. ip += seqHSize;
  650. dctx->flagRepeatTable = 0;
  651. }
  652. /* Regen sequences */
  653. if (nbSeq) {
  654. seq_t sequence;
  655. seqState_t seqState;
  656. memset(&sequence, 0, sizeof(sequence));
  657. sequence.offset = REPCODE_STARTVALUE;
  658. { U32 i; for (i=0; i<ZSTD_REP_INIT; i++) seqState.prevOffset[i] = REPCODE_STARTVALUE; }
  659. { size_t const errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip);
  660. if (ERR_isError(errorCode)) return ERROR(corruption_detected); }
  661. FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
  662. FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
  663. FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
  664. for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) {
  665. nbSeq--;
  666. ZSTD_decodeSequence(&sequence, &seqState);
  667. #if 0 /* debug */
  668. static BYTE* start = NULL;
  669. if (start==NULL) start = op;
  670. size_t pos = (size_t)(op-start);
  671. if ((pos >= 5810037) && (pos < 5810400))
  672. printf("Dpos %6u :%5u literals & match %3u bytes at distance %6u \n",
  673. pos, (U32)sequence.litLength, (U32)sequence.matchLength, (U32)sequence.offset);
  674. #endif
  675. { size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd);
  676. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  677. op += oneSeqSize;
  678. } }
  679. /* check if reached exact end */
  680. if (nbSeq) return ERROR(corruption_detected);
  681. }
  682. /* last literal segment */
  683. { size_t const lastLLSize = litEnd - litPtr;
  684. if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
  685. if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
  686. memcpy(op, litPtr, lastLLSize);
  687. op += lastLLSize;
  688. }
  689. return op-ostart;
  690. }
  691. static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
  692. {
  693. if (dst != dctx->previousDstEnd) { /* not contiguous */
  694. dctx->dictEnd = dctx->previousDstEnd;
  695. dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
  696. dctx->base = dst;
  697. dctx->previousDstEnd = dst;
  698. }
  699. }
  700. static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
  701. void* dst, size_t dstCapacity,
  702. const void* src, size_t srcSize)
  703. { /* blockType == blockCompressed */
  704. const BYTE* ip = (const BYTE*)src;
  705. if (srcSize >= ZSTD_BLOCKSIZE_MAX) return ERROR(srcSize_wrong);
  706. /* Decode literals sub-block */
  707. { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
  708. if (ZSTD_isError(litCSize)) return litCSize;
  709. ip += litCSize;
  710. srcSize -= litCSize;
  711. }
  712. return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize);
  713. }
  714. size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
  715. void* dst, size_t dstCapacity,
  716. const void* src, size_t srcSize)
  717. {
  718. ZSTD_checkContinuity(dctx, dst);
  719. return ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
  720. }
  721. /*! ZSTD_decompressFrame() :
  722. * `dctx` must be properly initialized */
  723. static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
  724. void* dst, size_t dstCapacity,
  725. const void* src, size_t srcSize)
  726. {
  727. const BYTE* ip = (const BYTE*)src;
  728. const BYTE* const iend = ip + srcSize;
  729. BYTE* const ostart = (BYTE* const)dst;
  730. BYTE* op = ostart;
  731. BYTE* const oend = ostart + dstCapacity;
  732. size_t remainingSize = srcSize;
  733. blockProperties_t blockProperties = { bt_compressed, 0 };
  734. /* check */
  735. if (srcSize < ZSTD_frameHeaderSize_min+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
  736. /* Frame Header */
  737. { size_t const frameHeaderSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_min);
  738. if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
  739. if (srcSize < frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
  740. if (ZSTD_decodeFrameHeader(dctx, src, frameHeaderSize)) return ERROR(corruption_detected);
  741. ip += frameHeaderSize; remainingSize -= frameHeaderSize;
  742. }
  743. /* Loop on each block */
  744. while (1) {
  745. size_t decodedSize=0;
  746. size_t const cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties);
  747. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  748. ip += ZSTD_blockHeaderSize;
  749. remainingSize -= ZSTD_blockHeaderSize;
  750. if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
  751. switch(blockProperties.blockType)
  752. {
  753. case bt_compressed:
  754. decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize);
  755. break;
  756. case bt_raw :
  757. decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);
  758. break;
  759. case bt_rle :
  760. return ERROR(GENERIC); /* not yet supported */
  761. break;
  762. case bt_end :
  763. /* end of frame */
  764. if (remainingSize) return ERROR(srcSize_wrong);
  765. break;
  766. default:
  767. return ERROR(GENERIC); /* impossible */
  768. }
  769. if (cBlockSize == 0) break; /* bt_end */
  770. if (ZSTD_isError(decodedSize)) return decodedSize;
  771. op += decodedSize;
  772. ip += cBlockSize;
  773. remainingSize -= cBlockSize;
  774. }
  775. return op-ostart;
  776. }
  777. size_t ZSTD_decompress_usingPreparedDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* refDCtx,
  778. void* dst, size_t dstCapacity,
  779. const void* src, size_t srcSize)
  780. {
  781. ZSTD_copyDCtx(dctx, refDCtx);
  782. ZSTD_checkContinuity(dctx, dst);
  783. return ZSTD_decompressFrame(dctx, dst, dstCapacity, src, srcSize);
  784. }
  785. size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
  786. void* dst, size_t dstCapacity,
  787. const void* src, size_t srcSize,
  788. const void* dict, size_t dictSize)
  789. {
  790. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
  791. { const U32 magicNumber = MEM_readLE32(src);
  792. if (ZSTD_isLegacy(magicNumber))
  793. return ZSTD_decompressLegacy(dst, dstCapacity, src, srcSize, dict, dictSize, magicNumber);
  794. }
  795. #endif
  796. ZSTD_decompressBegin_usingDict(dctx, dict, dictSize);
  797. ZSTD_checkContinuity(dctx, dst);
  798. return ZSTD_decompressFrame(dctx, dst, dstCapacity, src, srcSize);
  799. }
  800. size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  801. {
  802. return ZSTD_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0);
  803. }
  804. size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  805. {
  806. #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1)
  807. size_t regenSize;
  808. ZSTD_DCtx* dctx = ZSTD_createDCtx();
  809. if (dctx==NULL) return ERROR(memory_allocation);
  810. regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
  811. ZSTD_freeDCtx(dctx);
  812. return regenSize;
  813. #else /* stack mode */
  814. ZSTD_DCtx dctx;
  815. return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
  816. #endif
  817. }
  818. /*_******************************
  819. * Streaming Decompression API
  820. ********************************/
  821. size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx)
  822. {
  823. return dctx->expected;
  824. }
  825. size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  826. {
  827. /* Sanity check */
  828. if (srcSize != dctx->expected) return ERROR(srcSize_wrong);
  829. if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
  830. /* Decompress : frame header; part 1 */
  831. switch (dctx->stage)
  832. {
  833. case ZSTDds_getFrameHeaderSize :
  834. if (srcSize != ZSTD_frameHeaderSize_min) return ERROR(srcSize_wrong); /* impossible */
  835. dctx->headerSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_min);
  836. if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
  837. memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_min);
  838. if (dctx->headerSize > ZSTD_frameHeaderSize_min) {
  839. dctx->expected = dctx->headerSize - ZSTD_frameHeaderSize_min;
  840. dctx->stage = ZSTDds_decodeFrameHeader;
  841. return 0;
  842. }
  843. dctx->expected = 0; /* not necessary to copy more */
  844. case ZSTDds_decodeFrameHeader:
  845. { size_t result;
  846. memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_min, src, dctx->expected);
  847. result = ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize);
  848. if (ZSTD_isError(result)) return result;
  849. dctx->expected = ZSTD_blockHeaderSize;
  850. dctx->stage = ZSTDds_decodeBlockHeader;
  851. return 0;
  852. }
  853. case ZSTDds_decodeBlockHeader:
  854. { blockProperties_t bp;
  855. size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
  856. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  857. if (bp.blockType == bt_end) {
  858. dctx->expected = 0;
  859. dctx->stage = ZSTDds_getFrameHeaderSize;
  860. } else {
  861. dctx->expected = cBlockSize;
  862. dctx->bType = bp.blockType;
  863. dctx->stage = ZSTDds_decompressBlock;
  864. }
  865. return 0;
  866. }
  867. case ZSTDds_decompressBlock:
  868. { size_t rSize;
  869. switch(dctx->bType)
  870. {
  871. case bt_compressed:
  872. rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
  873. break;
  874. case bt_raw :
  875. rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
  876. break;
  877. case bt_rle :
  878. return ERROR(GENERIC); /* not yet handled */
  879. break;
  880. case bt_end : /* should never happen (filtered at phase 1) */
  881. rSize = 0;
  882. break;
  883. default:
  884. return ERROR(GENERIC); /* impossible */
  885. }
  886. dctx->stage = ZSTDds_decodeBlockHeader;
  887. dctx->expected = ZSTD_blockHeaderSize;
  888. dctx->previousDstEnd = (char*)dst + rSize;
  889. return rSize;
  890. }
  891. default:
  892. return ERROR(GENERIC); /* impossible */
  893. }
  894. }
  895. static void ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  896. {
  897. dctx->dictEnd = dctx->previousDstEnd;
  898. dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
  899. dctx->base = dict;
  900. dctx->previousDstEnd = (const char*)dict + dictSize;
  901. }
  902. static size_t ZSTD_loadEntropy(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  903. {
  904. size_t hSize, offcodeHeaderSize, matchlengthHeaderSize, litlengthHeaderSize;
  905. hSize = HUF_readDTableX4(dctx->hufTableX4, dict, dictSize);
  906. if (HUF_isError(hSize)) return ERROR(dictionary_corrupted);
  907. dict = (const char*)dict + hSize;
  908. dictSize -= hSize;
  909. { short offcodeNCount[MaxOff+1];
  910. U32 offcodeMaxValue=MaxOff, offcodeLog=OffFSELog;
  911. offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dict, dictSize);
  912. if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
  913. { size_t const errorCode = FSE_buildDTable(dctx->OffTable, offcodeNCount, offcodeMaxValue, offcodeLog);
  914. if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); }
  915. dict = (const char*)dict + offcodeHeaderSize;
  916. dictSize -= offcodeHeaderSize;
  917. }
  918. { short matchlengthNCount[MaxML+1];
  919. unsigned matchlengthMaxValue = MaxML, matchlengthLog = MLFSELog;
  920. matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dict, dictSize);
  921. if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
  922. { size_t const errorCode = FSE_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog);
  923. if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); }
  924. dict = (const char*)dict + matchlengthHeaderSize;
  925. dictSize -= matchlengthHeaderSize;
  926. }
  927. { short litlengthNCount[MaxLL+1];
  928. unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSELog;
  929. litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dict, dictSize);
  930. if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
  931. { size_t const errorCode = FSE_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog);
  932. if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); }
  933. }
  934. dctx->flagRepeatTable = 1;
  935. return hSize + offcodeHeaderSize + matchlengthHeaderSize + litlengthHeaderSize;
  936. }
  937. static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  938. {
  939. size_t eSize;
  940. U32 const magic = MEM_readLE32(dict);
  941. if (magic != ZSTD_DICT_MAGIC) {
  942. /* pure content mode */
  943. ZSTD_refDictContent(dctx, dict, dictSize);
  944. return 0;
  945. }
  946. /* load entropy tables */
  947. dict = (const char*)dict + 4;
  948. dictSize -= 4;
  949. eSize = ZSTD_loadEntropy(dctx, dict, dictSize);
  950. if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted);
  951. /* reference dictionary content */
  952. dict = (const char*)dict + eSize;
  953. dictSize -= eSize;
  954. ZSTD_refDictContent(dctx, dict, dictSize);
  955. return 0;
  956. }
  957. size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  958. {
  959. { size_t const errorCode = ZSTD_decompressBegin(dctx);
  960. if (ZSTD_isError(errorCode)) return errorCode; }
  961. if (dict && dictSize) {
  962. size_t const errorCode = ZSTD_decompress_insertDictionary(dctx, dict, dictSize);
  963. if (ZSTD_isError(errorCode)) return ERROR(dictionary_corrupted);
  964. }
  965. return 0;
  966. }