zstd_decompress.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. * Tuning parameters
  12. *****************************************************************/
  13. /*!
  14. * HEAPMODE :
  15. * Select how default decompression function ZSTD_decompress() allocates its context,
  16. * on stack (0), or into heap (1, default; requires malloc()).
  17. * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected.
  18. */
  19. #ifndef ZSTD_HEAPMODE
  20. # define ZSTD_HEAPMODE 1
  21. #endif
  22. /*!
  23. * LEGACY_SUPPORT :
  24. * if set to 1+, ZSTD_decompress() can decode older formats (v0.1+)
  25. */
  26. #ifndef ZSTD_LEGACY_SUPPORT
  27. # define ZSTD_LEGACY_SUPPORT 0
  28. #endif
  29. /*!
  30. * MAXWINDOWSIZE_DEFAULT :
  31. * maximum window size accepted by DStream __by default__.
  32. * Frames requiring more memory will be rejected.
  33. * It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize().
  34. */
  35. #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
  36. # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1)
  37. #endif
  38. /*!
  39. * NO_FORWARD_PROGRESS_MAX :
  40. * maximum allowed nb of calls to ZSTD_decompressStream()
  41. * without any forward progress
  42. * (defined as: no byte read from input, and no byte flushed to output)
  43. * before triggering an error.
  44. */
  45. #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX
  46. # define ZSTD_NO_FORWARD_PROGRESS_MAX 16
  47. #endif
  48. /*-*******************************************************
  49. * Dependencies
  50. *********************************************************/
  51. #include <string.h> /* memcpy, memmove, memset */
  52. #include "cpu.h" /* bmi2 */
  53. #include "mem.h" /* low level memory routines */
  54. #define FSE_STATIC_LINKING_ONLY
  55. #include "fse.h"
  56. #define HUF_STATIC_LINKING_ONLY
  57. #include "huf.h"
  58. #include "zstd_internal.h" /* blockProperties_t */
  59. #include "zstd_decompress_internal.h" /* ZSTD_DCtx */
  60. #include "zstd_ddict.h" /* ZSTD_DDictDictContent */
  61. #include "zstd_decompress_block.h" /* ZSTD_decompressBlock_internal */
  62. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  63. # include "zstd_legacy.h"
  64. #endif
  65. /*-*************************************************************
  66. * Context management
  67. ***************************************************************/
  68. size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)
  69. {
  70. if (dctx==NULL) return 0; /* support sizeof NULL */
  71. return sizeof(*dctx)
  72. + ZSTD_sizeof_DDict(dctx->ddictLocal)
  73. + dctx->inBuffSize + dctx->outBuffSize;
  74. }
  75. size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
  76. static size_t ZSTD_startingInputLength(ZSTD_format_e format)
  77. {
  78. size_t const startingInputLength = (format==ZSTD_f_zstd1_magicless) ?
  79. ZSTD_FRAMEHEADERSIZE_PREFIX - ZSTD_FRAMEIDSIZE :
  80. ZSTD_FRAMEHEADERSIZE_PREFIX;
  81. ZSTD_STATIC_ASSERT(ZSTD_FRAMEHEADERSIZE_PREFIX >= ZSTD_FRAMEIDSIZE);
  82. /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
  83. assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
  84. return startingInputLength;
  85. }
  86. static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
  87. {
  88. dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */
  89. dctx->staticSize = 0;
  90. dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
  91. dctx->ddict = NULL;
  92. dctx->ddictLocal = NULL;
  93. dctx->dictEnd = NULL;
  94. dctx->ddictIsCold = 0;
  95. dctx->inBuff = NULL;
  96. dctx->inBuffSize = 0;
  97. dctx->outBuffSize = 0;
  98. dctx->streamStage = zdss_init;
  99. dctx->legacyContext = NULL;
  100. dctx->previousLegacyVersion = 0;
  101. dctx->noForwardProgress = 0;
  102. dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());
  103. }
  104. ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
  105. {
  106. ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;
  107. if ((size_t)workspace & 7) return NULL; /* 8-aligned */
  108. if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */
  109. ZSTD_initDCtx_internal(dctx);
  110. dctx->staticSize = workspaceSize;
  111. dctx->inBuff = (char*)(dctx+1);
  112. return dctx;
  113. }
  114. ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
  115. {
  116. if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
  117. { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);
  118. if (!dctx) return NULL;
  119. dctx->customMem = customMem;
  120. ZSTD_initDCtx_internal(dctx);
  121. return dctx;
  122. }
  123. }
  124. ZSTD_DCtx* ZSTD_createDCtx(void)
  125. {
  126. DEBUGLOG(3, "ZSTD_createDCtx");
  127. return ZSTD_createDCtx_advanced(ZSTD_defaultCMem);
  128. }
  129. size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
  130. {
  131. if (dctx==NULL) return 0; /* support free on NULL */
  132. if (dctx->staticSize) return ERROR(memory_allocation); /* not compatible with static DCtx */
  133. { ZSTD_customMem const cMem = dctx->customMem;
  134. ZSTD_freeDDict(dctx->ddictLocal);
  135. dctx->ddictLocal = NULL;
  136. ZSTD_free(dctx->inBuff, cMem);
  137. dctx->inBuff = NULL;
  138. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  139. if (dctx->legacyContext)
  140. ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
  141. #endif
  142. ZSTD_free(dctx, cMem);
  143. return 0;
  144. }
  145. }
  146. /* no longer useful */
  147. void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
  148. {
  149. size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
  150. memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
  151. }
  152. /*-*************************************************************
  153. * Frame header decoding
  154. ***************************************************************/
  155. /*! ZSTD_isFrame() :
  156. * Tells if the content of `buffer` starts with a valid Frame Identifier.
  157. * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
  158. * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
  159. * Note 3 : Skippable Frame Identifiers are considered valid. */
  160. unsigned ZSTD_isFrame(const void* buffer, size_t size)
  161. {
  162. if (size < ZSTD_FRAMEIDSIZE) return 0;
  163. { U32 const magic = MEM_readLE32(buffer);
  164. if (magic == ZSTD_MAGICNUMBER) return 1;
  165. if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
  166. }
  167. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  168. if (ZSTD_isLegacy(buffer, size)) return 1;
  169. #endif
  170. return 0;
  171. }
  172. /** ZSTD_frameHeaderSize_internal() :
  173. * srcSize must be large enough to reach header size fields.
  174. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.
  175. * @return : size of the Frame Header
  176. * or an error code, which can be tested with ZSTD_isError() */
  177. static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)
  178. {
  179. size_t const minInputSize = ZSTD_startingInputLength(format);
  180. if (srcSize < minInputSize) return ERROR(srcSize_wrong);
  181. { BYTE const fhd = ((const BYTE*)src)[minInputSize-1];
  182. U32 const dictID= fhd & 3;
  183. U32 const singleSegment = (fhd >> 5) & 1;
  184. U32 const fcsId = fhd >> 6;
  185. return minInputSize + !singleSegment
  186. + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
  187. + (singleSegment && !fcsId);
  188. }
  189. }
  190. /** ZSTD_frameHeaderSize() :
  191. * srcSize must be >= ZSTD_frameHeaderSize_prefix.
  192. * @return : size of the Frame Header,
  193. * or an error code (if srcSize is too small) */
  194. size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
  195. {
  196. return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
  197. }
  198. /** ZSTD_getFrameHeader_advanced() :
  199. * decode Frame Header, or require larger `srcSize`.
  200. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
  201. * @return : 0, `zfhPtr` is correctly filled,
  202. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  203. * or an error code, which can be tested using ZSTD_isError() */
  204. size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
  205. {
  206. const BYTE* ip = (const BYTE*)src;
  207. size_t const minInputSize = ZSTD_startingInputLength(format);
  208. memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzer do not understand that zfhPtr is only going to be read only if return value is zero, since they are 2 different signals */
  209. if (srcSize < minInputSize) return minInputSize;
  210. if (src==NULL) return ERROR(GENERIC); /* invalid parameter */
  211. if ( (format != ZSTD_f_zstd1_magicless)
  212. && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
  213. if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  214. /* skippable frame */
  215. if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
  216. return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */
  217. memset(zfhPtr, 0, sizeof(*zfhPtr));
  218. zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
  219. zfhPtr->frameType = ZSTD_skippableFrame;
  220. return 0;
  221. }
  222. return ERROR(prefix_unknown);
  223. }
  224. /* ensure there is enough `srcSize` to fully read/decode frame header */
  225. { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);
  226. if (srcSize < fhsize) return fhsize;
  227. zfhPtr->headerSize = (U32)fhsize;
  228. }
  229. { BYTE const fhdByte = ip[minInputSize-1];
  230. size_t pos = minInputSize;
  231. U32 const dictIDSizeCode = fhdByte&3;
  232. U32 const checksumFlag = (fhdByte>>2)&1;
  233. U32 const singleSegment = (fhdByte>>5)&1;
  234. U32 const fcsID = fhdByte>>6;
  235. U64 windowSize = 0;
  236. U32 dictID = 0;
  237. U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;
  238. if ((fhdByte & 0x08) != 0)
  239. return ERROR(frameParameter_unsupported); /* reserved bits, must be zero */
  240. if (!singleSegment) {
  241. BYTE const wlByte = ip[pos++];
  242. U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
  243. if (windowLog > ZSTD_WINDOWLOG_MAX)
  244. return ERROR(frameParameter_windowTooLarge);
  245. windowSize = (1ULL << windowLog);
  246. windowSize += (windowSize >> 3) * (wlByte&7);
  247. }
  248. switch(dictIDSizeCode)
  249. {
  250. default: assert(0); /* impossible */
  251. case 0 : break;
  252. case 1 : dictID = ip[pos]; pos++; break;
  253. case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
  254. case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
  255. }
  256. switch(fcsID)
  257. {
  258. default: assert(0); /* impossible */
  259. case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
  260. case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
  261. case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
  262. case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
  263. }
  264. if (singleSegment) windowSize = frameContentSize;
  265. zfhPtr->frameType = ZSTD_frame;
  266. zfhPtr->frameContentSize = frameContentSize;
  267. zfhPtr->windowSize = windowSize;
  268. zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  269. zfhPtr->dictID = dictID;
  270. zfhPtr->checksumFlag = checksumFlag;
  271. }
  272. return 0;
  273. }
  274. /** ZSTD_getFrameHeader() :
  275. * decode Frame Header, or require larger `srcSize`.
  276. * note : this function does not consume input, it only reads it.
  277. * @return : 0, `zfhPtr` is correctly filled,
  278. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  279. * or an error code, which can be tested using ZSTD_isError() */
  280. size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize)
  281. {
  282. return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);
  283. }
  284. /** ZSTD_getFrameContentSize() :
  285. * compatible with legacy mode
  286. * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
  287. * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
  288. * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
  289. unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
  290. {
  291. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  292. if (ZSTD_isLegacy(src, srcSize)) {
  293. unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize);
  294. return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret;
  295. }
  296. #endif
  297. { ZSTD_frameHeader zfh;
  298. if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)
  299. return ZSTD_CONTENTSIZE_ERROR;
  300. if (zfh.frameType == ZSTD_skippableFrame) {
  301. return 0;
  302. } else {
  303. return zfh.frameContentSize;
  304. } }
  305. }
  306. static size_t readSkippableFrameSize(void const* src, size_t srcSize)
  307. {
  308. size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE;
  309. U32 sizeU32;
  310. if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
  311. return ERROR(srcSize_wrong);
  312. sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE);
  313. if ((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32)
  314. return ERROR(frameParameter_unsupported);
  315. return skippableHeaderSize + sizeU32;
  316. }
  317. /** ZSTD_findDecompressedSize() :
  318. * compatible with legacy mode
  319. * `srcSize` must be the exact length of some number of ZSTD compressed and/or
  320. * skippable frames
  321. * @return : decompressed size of the frames contained */
  322. unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
  323. {
  324. unsigned long long totalDstSize = 0;
  325. while (srcSize >= ZSTD_FRAMEHEADERSIZE_PREFIX) {
  326. U32 const magicNumber = MEM_readLE32(src);
  327. if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  328. size_t const skippableSize = readSkippableFrameSize(src, srcSize);
  329. if (ZSTD_isError(skippableSize))
  330. return skippableSize;
  331. if (srcSize < skippableSize) {
  332. return ZSTD_CONTENTSIZE_ERROR;
  333. }
  334. src = (const BYTE *)src + skippableSize;
  335. srcSize -= skippableSize;
  336. continue;
  337. }
  338. { unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
  339. if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret;
  340. /* check for overflow */
  341. if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR;
  342. totalDstSize += ret;
  343. }
  344. { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
  345. if (ZSTD_isError(frameSrcSize)) {
  346. return ZSTD_CONTENTSIZE_ERROR;
  347. }
  348. src = (const BYTE *)src + frameSrcSize;
  349. srcSize -= frameSrcSize;
  350. }
  351. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  352. if (srcSize) return ZSTD_CONTENTSIZE_ERROR;
  353. return totalDstSize;
  354. }
  355. /** ZSTD_getDecompressedSize() :
  356. * compatible with legacy mode
  357. * @return : decompressed size if known, 0 otherwise
  358. note : 0 can mean any of the following :
  359. - frame content is empty
  360. - decompressed size field is not present in frame header
  361. - frame header unknown / not supported
  362. - frame header not complete (`srcSize` too small) */
  363. unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
  364. {
  365. unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
  366. ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);
  367. return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;
  368. }
  369. /** ZSTD_decodeFrameHeader() :
  370. * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
  371. * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
  372. static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
  373. {
  374. size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
  375. if (ZSTD_isError(result)) return result; /* invalid header */
  376. if (result>0) return ERROR(srcSize_wrong); /* headerSize too small */
  377. if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID))
  378. return ERROR(dictionary_wrong);
  379. if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
  380. return 0;
  381. }
  382. /** ZSTD_findFrameCompressedSize() :
  383. * compatible with legacy mode
  384. * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame
  385. * `srcSize` must be at least as large as the frame contained
  386. * @return : the compressed size of the frame starting at `src` */
  387. size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
  388. {
  389. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  390. if (ZSTD_isLegacy(src, srcSize))
  391. return ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
  392. #endif
  393. if ( (srcSize >= ZSTD_SKIPPABLEHEADERSIZE)
  394. && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START ) {
  395. return readSkippableFrameSize(src, srcSize);
  396. } else {
  397. const BYTE* ip = (const BYTE*)src;
  398. const BYTE* const ipstart = ip;
  399. size_t remainingSize = srcSize;
  400. ZSTD_frameHeader zfh;
  401. /* Extract Frame Header */
  402. { size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize);
  403. if (ZSTD_isError(ret)) return ret;
  404. if (ret > 0) return ERROR(srcSize_wrong);
  405. }
  406. ip += zfh.headerSize;
  407. remainingSize -= zfh.headerSize;
  408. /* Loop on each block */
  409. while (1) {
  410. blockProperties_t blockProperties;
  411. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
  412. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  413. if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
  414. return ERROR(srcSize_wrong);
  415. ip += ZSTD_blockHeaderSize + cBlockSize;
  416. remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
  417. if (blockProperties.lastBlock) break;
  418. }
  419. if (zfh.checksumFlag) { /* Final frame content checksum */
  420. if (remainingSize < 4) return ERROR(srcSize_wrong);
  421. ip += 4;
  422. }
  423. return ip - ipstart;
  424. }
  425. }
  426. /*-*************************************************************
  427. * Frame decoding
  428. ***************************************************************/
  429. void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
  430. {
  431. if (dst != dctx->previousDstEnd) { /* not contiguous */
  432. dctx->dictEnd = dctx->previousDstEnd;
  433. dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
  434. dctx->prefixStart = dst;
  435. dctx->previousDstEnd = dst;
  436. }
  437. }
  438. /** ZSTD_insertBlock() :
  439. insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
  440. size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
  441. {
  442. ZSTD_checkContinuity(dctx, blockStart);
  443. dctx->previousDstEnd = (const char*)blockStart + blockSize;
  444. return blockSize;
  445. }
  446. static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
  447. const void* src, size_t srcSize)
  448. {
  449. DEBUGLOG(5, "ZSTD_copyRawBlock");
  450. if (dst == NULL) {
  451. if (srcSize == 0) return 0;
  452. return ERROR(dstBuffer_null);
  453. }
  454. if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
  455. memcpy(dst, src, srcSize);
  456. return srcSize;
  457. }
  458. static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
  459. BYTE b,
  460. size_t regenSize)
  461. {
  462. if (dst == NULL) {
  463. if (regenSize == 0) return 0;
  464. return ERROR(dstBuffer_null);
  465. }
  466. if (regenSize > dstCapacity) return ERROR(dstSize_tooSmall);
  467. memset(dst, b, regenSize);
  468. return regenSize;
  469. }
  470. /*! ZSTD_decompressFrame() :
  471. * @dctx must be properly initialized
  472. * will update *srcPtr and *srcSizePtr,
  473. * to make *srcPtr progress by one frame. */
  474. static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
  475. void* dst, size_t dstCapacity,
  476. const void** srcPtr, size_t *srcSizePtr)
  477. {
  478. const BYTE* ip = (const BYTE*)(*srcPtr);
  479. BYTE* const ostart = (BYTE* const)dst;
  480. BYTE* const oend = ostart + dstCapacity;
  481. BYTE* op = ostart;
  482. size_t remainingSrcSize = *srcSizePtr;
  483. DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr);
  484. /* check */
  485. if (remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN+ZSTD_blockHeaderSize)
  486. return ERROR(srcSize_wrong);
  487. /* Frame Header */
  488. { size_t const frameHeaderSize = ZSTD_frameHeaderSize(ip, ZSTD_FRAMEHEADERSIZE_PREFIX);
  489. if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
  490. if (remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize)
  491. return ERROR(srcSize_wrong);
  492. CHECK_F( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) );
  493. ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize;
  494. }
  495. /* Loop on each block */
  496. while (1) {
  497. size_t decodedSize;
  498. blockProperties_t blockProperties;
  499. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties);
  500. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  501. ip += ZSTD_blockHeaderSize;
  502. remainingSrcSize -= ZSTD_blockHeaderSize;
  503. if (cBlockSize > remainingSrcSize) return ERROR(srcSize_wrong);
  504. switch(blockProperties.blockType)
  505. {
  506. case bt_compressed:
  507. decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1);
  508. break;
  509. case bt_raw :
  510. decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);
  511. break;
  512. case bt_rle :
  513. decodedSize = ZSTD_setRleBlock(op, oend-op, *ip, blockProperties.origSize);
  514. break;
  515. case bt_reserved :
  516. default:
  517. return ERROR(corruption_detected);
  518. }
  519. if (ZSTD_isError(decodedSize)) return decodedSize;
  520. if (dctx->fParams.checksumFlag)
  521. XXH64_update(&dctx->xxhState, op, decodedSize);
  522. op += decodedSize;
  523. ip += cBlockSize;
  524. remainingSrcSize -= cBlockSize;
  525. if (blockProperties.lastBlock) break;
  526. }
  527. if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
  528. if ((U64)(op-ostart) != dctx->fParams.frameContentSize) {
  529. return ERROR(corruption_detected);
  530. } }
  531. if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
  532. U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
  533. U32 checkRead;
  534. if (remainingSrcSize<4) return ERROR(checksum_wrong);
  535. checkRead = MEM_readLE32(ip);
  536. if (checkRead != checkCalc) return ERROR(checksum_wrong);
  537. ip += 4;
  538. remainingSrcSize -= 4;
  539. }
  540. /* Allow caller to get size read */
  541. *srcPtr = ip;
  542. *srcSizePtr = remainingSrcSize;
  543. return op-ostart;
  544. }
  545. static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
  546. void* dst, size_t dstCapacity,
  547. const void* src, size_t srcSize,
  548. const void* dict, size_t dictSize,
  549. const ZSTD_DDict* ddict)
  550. {
  551. void* const dststart = dst;
  552. int moreThan1Frame = 0;
  553. DEBUGLOG(5, "ZSTD_decompressMultiFrame");
  554. assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */
  555. if (ddict) {
  556. dict = ZSTD_DDict_dictContent(ddict);
  557. dictSize = ZSTD_DDict_dictSize(ddict);
  558. }
  559. while (srcSize >= ZSTD_FRAMEHEADERSIZE_PREFIX) {
  560. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  561. if (ZSTD_isLegacy(src, srcSize)) {
  562. size_t decodedSize;
  563. size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
  564. if (ZSTD_isError(frameSize)) return frameSize;
  565. /* legacy support is not compatible with static dctx */
  566. if (dctx->staticSize) return ERROR(memory_allocation);
  567. decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
  568. if (ZSTD_isError(decodedSize)) return decodedSize;
  569. assert(decodedSize <=- dstCapacity);
  570. dst = (BYTE*)dst + decodedSize;
  571. dstCapacity -= decodedSize;
  572. src = (const BYTE*)src + frameSize;
  573. srcSize -= frameSize;
  574. continue;
  575. }
  576. #endif
  577. { U32 const magicNumber = MEM_readLE32(src);
  578. DEBUGLOG(4, "reading magic number %08X (expecting %08X)",
  579. (unsigned)magicNumber, ZSTD_MAGICNUMBER);
  580. if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  581. size_t const skippableSize = readSkippableFrameSize(src, srcSize);
  582. if (ZSTD_isError(skippableSize))
  583. return skippableSize;
  584. if (srcSize < skippableSize) return ERROR(srcSize_wrong);
  585. src = (const BYTE *)src + skippableSize;
  586. srcSize -= skippableSize;
  587. continue;
  588. } }
  589. if (ddict) {
  590. /* we were called from ZSTD_decompress_usingDDict */
  591. CHECK_F(ZSTD_decompressBegin_usingDDict(dctx, ddict));
  592. } else {
  593. /* this will initialize correctly with no dict if dict == NULL, so
  594. * use this in all cases but ddict */
  595. CHECK_F(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize));
  596. }
  597. ZSTD_checkContinuity(dctx, dst);
  598. { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
  599. &src, &srcSize);
  600. if ( (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
  601. && (moreThan1Frame==1) ) {
  602. /* at least one frame successfully completed,
  603. * but following bytes are garbage :
  604. * it's more likely to be a srcSize error,
  605. * specifying more bytes than compressed size of frame(s).
  606. * This error message replaces ERROR(prefix_unknown),
  607. * which would be confusing, as the first header is actually correct.
  608. * Note that one could be unlucky, it might be a corruption error instead,
  609. * happening right at the place where we expect zstd magic bytes.
  610. * But this is _much_ less likely than a srcSize field error. */
  611. return ERROR(srcSize_wrong);
  612. }
  613. if (ZSTD_isError(res)) return res;
  614. assert(res <= dstCapacity);
  615. dst = (BYTE*)dst + res;
  616. dstCapacity -= res;
  617. }
  618. moreThan1Frame = 1;
  619. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  620. if (srcSize) return ERROR(srcSize_wrong); /* input not entirely consumed */
  621. return (BYTE*)dst - (BYTE*)dststart;
  622. }
  623. size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
  624. void* dst, size_t dstCapacity,
  625. const void* src, size_t srcSize,
  626. const void* dict, size_t dictSize)
  627. {
  628. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
  629. }
  630. size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  631. {
  632. return ZSTD_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0);
  633. }
  634. size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  635. {
  636. #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
  637. size_t regenSize;
  638. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  639. if (dctx==NULL) return ERROR(memory_allocation);
  640. regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
  641. ZSTD_freeDCtx(dctx);
  642. return regenSize;
  643. #else /* stack mode */
  644. ZSTD_DCtx dctx;
  645. ZSTD_initDCtx_internal(&dctx);
  646. return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
  647. #endif
  648. }
  649. /*-**************************************
  650. * Advanced Streaming Decompression API
  651. * Bufferless and synchronous
  652. ****************************************/
  653. size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
  654. ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
  655. switch(dctx->stage)
  656. {
  657. default: /* should not happen */
  658. assert(0);
  659. case ZSTDds_getFrameHeaderSize:
  660. case ZSTDds_decodeFrameHeader:
  661. return ZSTDnit_frameHeader;
  662. case ZSTDds_decodeBlockHeader:
  663. return ZSTDnit_blockHeader;
  664. case ZSTDds_decompressBlock:
  665. return ZSTDnit_block;
  666. case ZSTDds_decompressLastBlock:
  667. return ZSTDnit_lastBlock;
  668. case ZSTDds_checkChecksum:
  669. return ZSTDnit_checksum;
  670. case ZSTDds_decodeSkippableHeader:
  671. case ZSTDds_skipFrame:
  672. return ZSTDnit_skippableFrame;
  673. }
  674. }
  675. static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
  676. /** ZSTD_decompressContinue() :
  677. * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
  678. * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
  679. * or an error code, which can be tested using ZSTD_isError() */
  680. size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  681. {
  682. DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
  683. /* Sanity check */
  684. if (srcSize != dctx->expected)
  685. return ERROR(srcSize_wrong); /* not allowed */
  686. if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
  687. switch (dctx->stage)
  688. {
  689. case ZSTDds_getFrameHeaderSize :
  690. assert(src != NULL);
  691. if (dctx->format == ZSTD_f_zstd1) { /* allows header */
  692. assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */
  693. if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  694. memcpy(dctx->headerBuffer, src, srcSize);
  695. dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */
  696. dctx->stage = ZSTDds_decodeSkippableHeader;
  697. return 0;
  698. } }
  699. dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
  700. if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
  701. memcpy(dctx->headerBuffer, src, srcSize);
  702. dctx->expected = dctx->headerSize - srcSize;
  703. dctx->stage = ZSTDds_decodeFrameHeader;
  704. return 0;
  705. case ZSTDds_decodeFrameHeader:
  706. assert(src != NULL);
  707. memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
  708. CHECK_F(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize));
  709. dctx->expected = ZSTD_blockHeaderSize;
  710. dctx->stage = ZSTDds_decodeBlockHeader;
  711. return 0;
  712. case ZSTDds_decodeBlockHeader:
  713. { blockProperties_t bp;
  714. size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
  715. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  716. dctx->expected = cBlockSize;
  717. dctx->bType = bp.blockType;
  718. dctx->rleSize = bp.origSize;
  719. if (cBlockSize) {
  720. dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
  721. return 0;
  722. }
  723. /* empty block */
  724. if (bp.lastBlock) {
  725. if (dctx->fParams.checksumFlag) {
  726. dctx->expected = 4;
  727. dctx->stage = ZSTDds_checkChecksum;
  728. } else {
  729. dctx->expected = 0; /* end of frame */
  730. dctx->stage = ZSTDds_getFrameHeaderSize;
  731. }
  732. } else {
  733. dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */
  734. dctx->stage = ZSTDds_decodeBlockHeader;
  735. }
  736. return 0;
  737. }
  738. case ZSTDds_decompressLastBlock:
  739. case ZSTDds_decompressBlock:
  740. DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock");
  741. { size_t rSize;
  742. switch(dctx->bType)
  743. {
  744. case bt_compressed:
  745. DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed");
  746. rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1);
  747. break;
  748. case bt_raw :
  749. rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
  750. break;
  751. case bt_rle :
  752. rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize);
  753. break;
  754. case bt_reserved : /* should never happen */
  755. default:
  756. return ERROR(corruption_detected);
  757. }
  758. if (ZSTD_isError(rSize)) return rSize;
  759. DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
  760. dctx->decodedSize += rSize;
  761. if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);
  762. if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */
  763. DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize);
  764. if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
  765. if (dctx->decodedSize != dctx->fParams.frameContentSize) {
  766. return ERROR(corruption_detected);
  767. } }
  768. if (dctx->fParams.checksumFlag) { /* another round for frame checksum */
  769. dctx->expected = 4;
  770. dctx->stage = ZSTDds_checkChecksum;
  771. } else {
  772. dctx->expected = 0; /* ends here */
  773. dctx->stage = ZSTDds_getFrameHeaderSize;
  774. }
  775. } else {
  776. dctx->stage = ZSTDds_decodeBlockHeader;
  777. dctx->expected = ZSTD_blockHeaderSize;
  778. dctx->previousDstEnd = (char*)dst + rSize;
  779. }
  780. return rSize;
  781. }
  782. case ZSTDds_checkChecksum:
  783. assert(srcSize == 4); /* guaranteed by dctx->expected */
  784. { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
  785. U32 const check32 = MEM_readLE32(src);
  786. DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
  787. if (check32 != h32) return ERROR(checksum_wrong);
  788. dctx->expected = 0;
  789. dctx->stage = ZSTDds_getFrameHeaderSize;
  790. return 0;
  791. }
  792. case ZSTDds_decodeSkippableHeader:
  793. assert(src != NULL);
  794. assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);
  795. memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */
  796. dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */
  797. dctx->stage = ZSTDds_skipFrame;
  798. return 0;
  799. case ZSTDds_skipFrame:
  800. dctx->expected = 0;
  801. dctx->stage = ZSTDds_getFrameHeaderSize;
  802. return 0;
  803. default:
  804. assert(0); /* impossible */
  805. return ERROR(GENERIC); /* some compiler require default to do something */
  806. }
  807. }
  808. static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  809. {
  810. dctx->dictEnd = dctx->previousDstEnd;
  811. dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
  812. dctx->prefixStart = dict;
  813. dctx->previousDstEnd = (const char*)dict + dictSize;
  814. return 0;
  815. }
  816. /*! ZSTD_loadDEntropy() :
  817. * dict : must point at beginning of a valid zstd dictionary.
  818. * @return : size of entropy tables read */
  819. size_t
  820. ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
  821. const void* const dict, size_t const dictSize)
  822. {
  823. const BYTE* dictPtr = (const BYTE*)dict;
  824. const BYTE* const dictEnd = dictPtr + dictSize;
  825. if (dictSize <= 8) return ERROR(dictionary_corrupted);
  826. assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */
  827. dictPtr += 8; /* skip header = magic + dictID */
  828. ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));
  829. ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));
  830. ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);
  831. { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */
  832. size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);
  833. #ifdef HUF_FORCE_DECOMPRESS_X1
  834. /* in minimal huffman, we always use X1 variants */
  835. size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable,
  836. dictPtr, dictEnd - dictPtr,
  837. workspace, workspaceSize);
  838. #else
  839. size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
  840. dictPtr, dictEnd - dictPtr,
  841. workspace, workspaceSize);
  842. #endif
  843. if (HUF_isError(hSize)) return ERROR(dictionary_corrupted);
  844. dictPtr += hSize;
  845. }
  846. { short offcodeNCount[MaxOff+1];
  847. unsigned offcodeMaxValue = MaxOff, offcodeLog;
  848. size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
  849. if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
  850. if (offcodeMaxValue > MaxOff) return ERROR(dictionary_corrupted);
  851. if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted);
  852. ZSTD_buildFSETable( entropy->OFTable,
  853. offcodeNCount, offcodeMaxValue,
  854. OF_base, OF_bits,
  855. offcodeLog);
  856. dictPtr += offcodeHeaderSize;
  857. }
  858. { short matchlengthNCount[MaxML+1];
  859. unsigned matchlengthMaxValue = MaxML, matchlengthLog;
  860. size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
  861. if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
  862. if (matchlengthMaxValue > MaxML) return ERROR(dictionary_corrupted);
  863. if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted);
  864. ZSTD_buildFSETable( entropy->MLTable,
  865. matchlengthNCount, matchlengthMaxValue,
  866. ML_base, ML_bits,
  867. matchlengthLog);
  868. dictPtr += matchlengthHeaderSize;
  869. }
  870. { short litlengthNCount[MaxLL+1];
  871. unsigned litlengthMaxValue = MaxLL, litlengthLog;
  872. size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
  873. if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
  874. if (litlengthMaxValue > MaxLL) return ERROR(dictionary_corrupted);
  875. if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted);
  876. ZSTD_buildFSETable( entropy->LLTable,
  877. litlengthNCount, litlengthMaxValue,
  878. LL_base, LL_bits,
  879. litlengthLog);
  880. dictPtr += litlengthHeaderSize;
  881. }
  882. if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted);
  883. { int i;
  884. size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));
  885. for (i=0; i<3; i++) {
  886. U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;
  887. if (rep==0 || rep >= dictContentSize) return ERROR(dictionary_corrupted);
  888. entropy->rep[i] = rep;
  889. } }
  890. return dictPtr - (const BYTE*)dict;
  891. }
  892. static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  893. {
  894. if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
  895. { U32 const magic = MEM_readLE32(dict);
  896. if (magic != ZSTD_MAGIC_DICTIONARY) {
  897. return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
  898. } }
  899. dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
  900. /* load entropy tables */
  901. { size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize);
  902. if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted);
  903. dict = (const char*)dict + eSize;
  904. dictSize -= eSize;
  905. }
  906. dctx->litEntropy = dctx->fseEntropy = 1;
  907. /* reference dictionary content */
  908. return ZSTD_refDictContent(dctx, dict, dictSize);
  909. }
  910. size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
  911. {
  912. assert(dctx != NULL);
  913. dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
  914. dctx->stage = ZSTDds_getFrameHeaderSize;
  915. dctx->decodedSize = 0;
  916. dctx->previousDstEnd = NULL;
  917. dctx->prefixStart = NULL;
  918. dctx->virtualStart = NULL;
  919. dctx->dictEnd = NULL;
  920. dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
  921. dctx->litEntropy = dctx->fseEntropy = 0;
  922. dctx->dictID = 0;
  923. ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
  924. memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
  925. dctx->LLTptr = dctx->entropy.LLTable;
  926. dctx->MLTptr = dctx->entropy.MLTable;
  927. dctx->OFTptr = dctx->entropy.OFTable;
  928. dctx->HUFptr = dctx->entropy.hufTable;
  929. return 0;
  930. }
  931. size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  932. {
  933. CHECK_F( ZSTD_decompressBegin(dctx) );
  934. if (dict && dictSize)
  935. CHECK_E(ZSTD_decompress_insertDictionary(dctx, dict, dictSize), dictionary_corrupted);
  936. return 0;
  937. }
  938. /* ====== ZSTD_DDict ====== */
  939. size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  940. {
  941. DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
  942. assert(dctx != NULL);
  943. if (ddict) {
  944. const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);
  945. size_t const dictSize = ZSTD_DDict_dictSize(ddict);
  946. const void* const dictEnd = dictStart + dictSize;
  947. dctx->ddictIsCold = (dctx->dictEnd != dictEnd);
  948. DEBUGLOG(4, "DDict is %s",
  949. dctx->ddictIsCold ? "~cold~" : "hot!");
  950. }
  951. CHECK_F( ZSTD_decompressBegin(dctx) );
  952. if (ddict) { /* NULL ddict is equivalent to no dictionary */
  953. ZSTD_copyDDictParameters(dctx, ddict);
  954. }
  955. return 0;
  956. }
  957. /*! ZSTD_getDictID_fromDict() :
  958. * Provides the dictID stored within dictionary.
  959. * if @return == 0, the dictionary is not conformant with Zstandard specification.
  960. * It can still be loaded, but as a content-only dictionary. */
  961. unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
  962. {
  963. if (dictSize < 8) return 0;
  964. if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
  965. return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
  966. }
  967. /*! ZSTD_getDictID_fromFrame() :
  968. * Provides the dictID required to decompresse frame stored within `src`.
  969. * If @return == 0, the dictID could not be decoded.
  970. * This could for one of the following reasons :
  971. * - The frame does not require a dictionary (most common case).
  972. * - The frame was built with dictID intentionally removed.
  973. * Needed dictionary is a hidden information.
  974. * Note : this use case also happens when using a non-conformant dictionary.
  975. * - `srcSize` is too small, and as a result, frame header could not be decoded.
  976. * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
  977. * - This is not a Zstandard frame.
  978. * When identifying the exact failure cause, it's possible to use
  979. * ZSTD_getFrameHeader(), which will provide a more precise error code. */
  980. unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)
  981. {
  982. ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 };
  983. size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);
  984. if (ZSTD_isError(hError)) return 0;
  985. return zfp.dictID;
  986. }
  987. /*! ZSTD_decompress_usingDDict() :
  988. * Decompression using a pre-digested Dictionary
  989. * Use dictionary without significant overhead. */
  990. size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
  991. void* dst, size_t dstCapacity,
  992. const void* src, size_t srcSize,
  993. const ZSTD_DDict* ddict)
  994. {
  995. /* pass content and size in case legacy frames are encountered */
  996. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
  997. NULL, 0,
  998. ddict);
  999. }
  1000. /*=====================================
  1001. * Streaming decompression
  1002. *====================================*/
  1003. ZSTD_DStream* ZSTD_createDStream(void)
  1004. {
  1005. DEBUGLOG(3, "ZSTD_createDStream");
  1006. return ZSTD_createDStream_advanced(ZSTD_defaultCMem);
  1007. }
  1008. ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)
  1009. {
  1010. return ZSTD_initStaticDCtx(workspace, workspaceSize);
  1011. }
  1012. ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
  1013. {
  1014. return ZSTD_createDCtx_advanced(customMem);
  1015. }
  1016. size_t ZSTD_freeDStream(ZSTD_DStream* zds)
  1017. {
  1018. return ZSTD_freeDCtx(zds);
  1019. }
  1020. /* *** Initialization *** */
  1021. size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
  1022. size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
  1023. size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
  1024. const void* dict, size_t dictSize,
  1025. ZSTD_dictLoadMethod_e dictLoadMethod,
  1026. ZSTD_dictContentType_e dictContentType)
  1027. {
  1028. if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
  1029. ZSTD_freeDDict(dctx->ddictLocal);
  1030. if (dict && dictSize >= 8) {
  1031. dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
  1032. if (dctx->ddictLocal == NULL) return ERROR(memory_allocation);
  1033. } else {
  1034. dctx->ddictLocal = NULL;
  1035. }
  1036. dctx->ddict = dctx->ddictLocal;
  1037. return 0;
  1038. }
  1039. size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1040. {
  1041. return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
  1042. }
  1043. size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1044. {
  1045. return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
  1046. }
  1047. size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)
  1048. {
  1049. return ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType);
  1050. }
  1051. size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize)
  1052. {
  1053. return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent);
  1054. }
  1055. /* ZSTD_initDStream_usingDict() :
  1056. * return : expected size, aka ZSTD_FRAMEHEADERSIZE_PREFIX.
  1057. * this function cannot fail */
  1058. size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
  1059. {
  1060. DEBUGLOG(4, "ZSTD_initDStream_usingDict");
  1061. zds->streamStage = zdss_init;
  1062. zds->noForwardProgress = 0;
  1063. CHECK_F( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) );
  1064. return ZSTD_FRAMEHEADERSIZE_PREFIX;
  1065. }
  1066. /* note : this variant can't fail */
  1067. size_t ZSTD_initDStream(ZSTD_DStream* zds)
  1068. {
  1069. DEBUGLOG(4, "ZSTD_initDStream");
  1070. return ZSTD_initDStream_usingDict(zds, NULL, 0);
  1071. }
  1072. /* ZSTD_initDStream_usingDDict() :
  1073. * ddict will just be referenced, and must outlive decompression session
  1074. * this function cannot fail */
  1075. size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)
  1076. {
  1077. size_t const initResult = ZSTD_initDStream(dctx);
  1078. dctx->ddict = ddict;
  1079. return initResult;
  1080. }
  1081. /* ZSTD_resetDStream() :
  1082. * return : expected size, aka ZSTD_FRAMEHEADERSIZE_PREFIX.
  1083. * this function cannot fail */
  1084. size_t ZSTD_resetDStream(ZSTD_DStream* dctx)
  1085. {
  1086. DEBUGLOG(4, "ZSTD_resetDStream");
  1087. dctx->streamStage = zdss_loadHeader;
  1088. dctx->lhSize = dctx->inPos = dctx->outStart = dctx->outEnd = 0;
  1089. dctx->legacyVersion = 0;
  1090. dctx->hostageByte = 0;
  1091. return ZSTD_FRAMEHEADERSIZE_PREFIX;
  1092. }
  1093. size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  1094. {
  1095. if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
  1096. dctx->ddict = ddict;
  1097. return 0;
  1098. }
  1099. /* ZSTD_DCtx_setMaxWindowSize() :
  1100. * note : no direct equivalence in ZSTD_DCtx_setParameter,
  1101. * since this version sets windowSize, and the other sets windowLog */
  1102. size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
  1103. {
  1104. ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
  1105. size_t const min = (size_t)1 << bounds.lowerBound;
  1106. size_t const max = (size_t)1 << bounds.upperBound;
  1107. if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
  1108. if (maxWindowSize < min) return ERROR(parameter_outOfBound);
  1109. if (maxWindowSize > max) return ERROR(parameter_outOfBound);
  1110. dctx->maxWindowSize = maxWindowSize;
  1111. return 0;
  1112. }
  1113. size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
  1114. {
  1115. return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, format);
  1116. }
  1117. ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
  1118. {
  1119. ZSTD_bounds bounds = { 0, 0, 0 };
  1120. switch(dParam) {
  1121. case ZSTD_d_windowLogMax:
  1122. bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN;
  1123. bounds.upperBound = ZSTD_WINDOWLOG_MAX;
  1124. return bounds;
  1125. case ZSTD_d_format:
  1126. bounds.lowerBound = (int)ZSTD_f_zstd1;
  1127. bounds.upperBound = (int)ZSTD_f_zstd1_magicless;
  1128. ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);
  1129. return bounds;
  1130. default:;
  1131. }
  1132. bounds.error = ERROR(parameter_unsupported);
  1133. return bounds;
  1134. }
  1135. /* ZSTD_dParam_withinBounds:
  1136. * @return 1 if value is within dParam bounds,
  1137. * 0 otherwise */
  1138. static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)
  1139. {
  1140. ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam);
  1141. if (ZSTD_isError(bounds.error)) return 0;
  1142. if (value < bounds.lowerBound) return 0;
  1143. if (value > bounds.upperBound) return 0;
  1144. return 1;
  1145. }
  1146. #define CHECK_DBOUNDS(p,v) { \
  1147. if (!ZSTD_dParam_withinBounds(p, v)) \
  1148. return ERROR(parameter_outOfBound); \
  1149. }
  1150. size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)
  1151. {
  1152. if (dctx->streamStage != zdss_init) return ERROR(stage_wrong);
  1153. switch(dParam) {
  1154. case ZSTD_d_windowLogMax:
  1155. CHECK_DBOUNDS(ZSTD_d_windowLogMax, value);
  1156. dctx->maxWindowSize = ((size_t)1) << value;
  1157. return 0;
  1158. case ZSTD_d_format:
  1159. CHECK_DBOUNDS(ZSTD_d_format, value);
  1160. dctx->format = (ZSTD_format_e)value;
  1161. return 0;
  1162. default:;
  1163. }
  1164. return ERROR(parameter_unsupported);
  1165. }
  1166. size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
  1167. {
  1168. if ( (reset == ZSTD_reset_session_only)
  1169. || (reset == ZSTD_reset_session_and_parameters) ) {
  1170. (void)ZSTD_initDStream(dctx);
  1171. }
  1172. if ( (reset == ZSTD_reset_parameters)
  1173. || (reset == ZSTD_reset_session_and_parameters) ) {
  1174. if (dctx->streamStage != zdss_init)
  1175. return ERROR(stage_wrong);
  1176. dctx->format = ZSTD_f_zstd1;
  1177. dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
  1178. }
  1179. return 0;
  1180. }
  1181. size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)
  1182. {
  1183. return ZSTD_sizeof_DCtx(dctx);
  1184. }
  1185. size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
  1186. {
  1187. size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  1188. unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2);
  1189. unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);
  1190. size_t const minRBSize = (size_t) neededSize;
  1191. if ((unsigned long long)minRBSize != neededSize) return ERROR(frameParameter_windowTooLarge);
  1192. return minRBSize;
  1193. }
  1194. size_t ZSTD_estimateDStreamSize(size_t windowSize)
  1195. {
  1196. size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  1197. size_t const inBuffSize = blockSize; /* no block can be larger */
  1198. size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);
  1199. return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;
  1200. }
  1201. size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)
  1202. {
  1203. U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */
  1204. ZSTD_frameHeader zfh;
  1205. size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);
  1206. if (ZSTD_isError(err)) return err;
  1207. if (err>0) return ERROR(srcSize_wrong);
  1208. if (zfh.windowSize > windowSizeMax)
  1209. return ERROR(frameParameter_windowTooLarge);
  1210. return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);
  1211. }
  1212. /* ***** Decompression ***** */
  1213. MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  1214. {
  1215. size_t const length = MIN(dstCapacity, srcSize);
  1216. memcpy(dst, src, length);
  1217. return length;
  1218. }
  1219. size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  1220. {
  1221. const char* const istart = (const char*)(input->src) + input->pos;
  1222. const char* const iend = (const char*)(input->src) + input->size;
  1223. const char* ip = istart;
  1224. char* const ostart = (char*)(output->dst) + output->pos;
  1225. char* const oend = (char*)(output->dst) + output->size;
  1226. char* op = ostart;
  1227. U32 someMoreWork = 1;
  1228. DEBUGLOG(5, "ZSTD_decompressStream");
  1229. if (input->pos > input->size) { /* forbidden */
  1230. DEBUGLOG(5, "in: pos: %u vs size: %u",
  1231. (U32)input->pos, (U32)input->size);
  1232. return ERROR(srcSize_wrong);
  1233. }
  1234. if (output->pos > output->size) { /* forbidden */
  1235. DEBUGLOG(5, "out: pos: %u vs size: %u",
  1236. (U32)output->pos, (U32)output->size);
  1237. return ERROR(dstSize_tooSmall);
  1238. }
  1239. DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos));
  1240. while (someMoreWork) {
  1241. switch(zds->streamStage)
  1242. {
  1243. case zdss_init :
  1244. DEBUGLOG(5, "stage zdss_init => transparent reset ");
  1245. ZSTD_resetDStream(zds); /* transparent reset on starting decoding a new frame */
  1246. /* fall-through */
  1247. case zdss_loadHeader :
  1248. DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
  1249. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  1250. if (zds->legacyVersion) {
  1251. /* legacy support is incompatible with static dctx */
  1252. if (zds->staticSize) return ERROR(memory_allocation);
  1253. { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
  1254. if (hint==0) zds->streamStage = zdss_init;
  1255. return hint;
  1256. } }
  1257. #endif
  1258. { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
  1259. DEBUGLOG(5, "header size : %u", (U32)hSize);
  1260. if (ZSTD_isError(hSize)) {
  1261. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  1262. U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
  1263. if (legacyVersion) {
  1264. const void* const dict = zds->ddict ? ZSTD_DDict_dictContent(zds->ddict) : NULL;
  1265. size_t const dictSize = zds->ddict ? ZSTD_DDict_dictSize(zds->ddict) : 0;
  1266. DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion);
  1267. /* legacy support is incompatible with static dctx */
  1268. if (zds->staticSize) return ERROR(memory_allocation);
  1269. CHECK_F(ZSTD_initLegacyStream(&zds->legacyContext,
  1270. zds->previousLegacyVersion, legacyVersion,
  1271. dict, dictSize));
  1272. zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;
  1273. { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input);
  1274. if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */
  1275. return hint;
  1276. } }
  1277. #endif
  1278. return hSize; /* error */
  1279. }
  1280. if (hSize != 0) { /* need more input */
  1281. size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */
  1282. size_t const remainingInput = (size_t)(iend-ip);
  1283. assert(iend >= ip);
  1284. if (toLoad > remainingInput) { /* not enough input to load full header */
  1285. if (remainingInput > 0) {
  1286. memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
  1287. zds->lhSize += remainingInput;
  1288. }
  1289. input->pos = input->size;
  1290. return (MAX(ZSTD_FRAMEHEADERSIZE_MIN, hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
  1291. }
  1292. assert(ip != NULL);
  1293. memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
  1294. break;
  1295. } }
  1296. /* check for single-pass mode opportunity */
  1297. if (zds->fParams.frameContentSize && zds->fParams.windowSize /* skippable frame if == 0 */
  1298. && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
  1299. size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart);
  1300. if (cSize <= (size_t)(iend-istart)) {
  1301. /* shortcut : using single-pass mode */
  1302. size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, zds->ddict);
  1303. if (ZSTD_isError(decompressedSize)) return decompressedSize;
  1304. DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()")
  1305. ip = istart + cSize;
  1306. op += decompressedSize;
  1307. zds->expected = 0;
  1308. zds->streamStage = zdss_init;
  1309. someMoreWork = 0;
  1310. break;
  1311. } }
  1312. /* Consume header (see ZSTDds_decodeFrameHeader) */
  1313. DEBUGLOG(4, "Consume header");
  1314. CHECK_F(ZSTD_decompressBegin_usingDDict(zds, zds->ddict));
  1315. if ((MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  1316. zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);
  1317. zds->stage = ZSTDds_skipFrame;
  1318. } else {
  1319. CHECK_F(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize));
  1320. zds->expected = ZSTD_blockHeaderSize;
  1321. zds->stage = ZSTDds_decodeBlockHeader;
  1322. }
  1323. /* control buffer memory usage */
  1324. DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)",
  1325. (U32)(zds->fParams.windowSize >>10),
  1326. (U32)(zds->maxWindowSize >> 10) );
  1327. zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
  1328. if (zds->fParams.windowSize > zds->maxWindowSize) return ERROR(frameParameter_windowTooLarge);
  1329. /* Adapt buffer sizes to frame header instructions */
  1330. { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
  1331. size_t const neededOutBuffSize = ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize);
  1332. if ((zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize)) {
  1333. size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
  1334. DEBUGLOG(4, "inBuff : from %u to %u",
  1335. (U32)zds->inBuffSize, (U32)neededInBuffSize);
  1336. DEBUGLOG(4, "outBuff : from %u to %u",
  1337. (U32)zds->outBuffSize, (U32)neededOutBuffSize);
  1338. if (zds->staticSize) { /* static DCtx */
  1339. DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize);
  1340. assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */
  1341. if (bufferSize > zds->staticSize - sizeof(ZSTD_DCtx))
  1342. return ERROR(memory_allocation);
  1343. } else {
  1344. ZSTD_free(zds->inBuff, zds->customMem);
  1345. zds->inBuffSize = 0;
  1346. zds->outBuffSize = 0;
  1347. zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem);
  1348. if (zds->inBuff == NULL) return ERROR(memory_allocation);
  1349. }
  1350. zds->inBuffSize = neededInBuffSize;
  1351. zds->outBuff = zds->inBuff + zds->inBuffSize;
  1352. zds->outBuffSize = neededOutBuffSize;
  1353. } }
  1354. zds->streamStage = zdss_read;
  1355. /* fall-through */
  1356. case zdss_read:
  1357. DEBUGLOG(5, "stage zdss_read");
  1358. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
  1359. DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
  1360. if (neededInSize==0) { /* end of frame */
  1361. zds->streamStage = zdss_init;
  1362. someMoreWork = 0;
  1363. break;
  1364. }
  1365. if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
  1366. int const isSkipFrame = ZSTD_isSkipFrame(zds);
  1367. size_t const decodedSize = ZSTD_decompressContinue(zds,
  1368. zds->outBuff + zds->outStart, (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart),
  1369. ip, neededInSize);
  1370. if (ZSTD_isError(decodedSize)) return decodedSize;
  1371. ip += neededInSize;
  1372. if (!decodedSize && !isSkipFrame) break; /* this was just a header */
  1373. zds->outEnd = zds->outStart + decodedSize;
  1374. zds->streamStage = zdss_flush;
  1375. break;
  1376. } }
  1377. if (ip==iend) { someMoreWork = 0; break; } /* no more input */
  1378. zds->streamStage = zdss_load;
  1379. /* fall-through */
  1380. case zdss_load:
  1381. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
  1382. size_t const toLoad = neededInSize - zds->inPos;
  1383. int const isSkipFrame = ZSTD_isSkipFrame(zds);
  1384. size_t loadedSize;
  1385. if (isSkipFrame) {
  1386. loadedSize = MIN(toLoad, (size_t)(iend-ip));
  1387. } else {
  1388. if (toLoad > zds->inBuffSize - zds->inPos) return ERROR(corruption_detected); /* should never happen */
  1389. loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip);
  1390. }
  1391. ip += loadedSize;
  1392. zds->inPos += loadedSize;
  1393. if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
  1394. /* decode loaded input */
  1395. { size_t const decodedSize = ZSTD_decompressContinue(zds,
  1396. zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart,
  1397. zds->inBuff, neededInSize);
  1398. if (ZSTD_isError(decodedSize)) return decodedSize;
  1399. zds->inPos = 0; /* input is consumed */
  1400. if (!decodedSize && !isSkipFrame) { zds->streamStage = zdss_read; break; } /* this was just a header */
  1401. zds->outEnd = zds->outStart + decodedSize;
  1402. } }
  1403. zds->streamStage = zdss_flush;
  1404. /* fall-through */
  1405. case zdss_flush:
  1406. { size_t const toFlushSize = zds->outEnd - zds->outStart;
  1407. size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize);
  1408. op += flushedSize;
  1409. zds->outStart += flushedSize;
  1410. if (flushedSize == toFlushSize) { /* flush completed */
  1411. zds->streamStage = zdss_read;
  1412. if ( (zds->outBuffSize < zds->fParams.frameContentSize)
  1413. && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {
  1414. DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
  1415. (int)(zds->outBuffSize - zds->outStart),
  1416. (U32)zds->fParams.blockSizeMax);
  1417. zds->outStart = zds->outEnd = 0;
  1418. }
  1419. break;
  1420. } }
  1421. /* cannot complete flush */
  1422. someMoreWork = 0;
  1423. break;
  1424. default:
  1425. assert(0); /* impossible */
  1426. return ERROR(GENERIC); /* some compiler require default to do something */
  1427. } }
  1428. /* result */
  1429. input->pos = (size_t)(ip - (const char*)(input->src));
  1430. output->pos = (size_t)(op - (char*)(output->dst));
  1431. if ((ip==istart) && (op==ostart)) { /* no forward progress */
  1432. zds->noForwardProgress ++;
  1433. if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {
  1434. if (op==oend) return ERROR(dstSize_tooSmall);
  1435. if (ip==iend) return ERROR(srcSize_wrong);
  1436. assert(0);
  1437. }
  1438. } else {
  1439. zds->noForwardProgress = 0;
  1440. }
  1441. { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
  1442. if (!nextSrcSizeHint) { /* frame fully decoded */
  1443. if (zds->outEnd == zds->outStart) { /* output fully flushed */
  1444. if (zds->hostageByte) {
  1445. if (input->pos >= input->size) {
  1446. /* can't release hostage (not present) */
  1447. zds->streamStage = zdss_read;
  1448. return 1;
  1449. }
  1450. input->pos++; /* release hostage */
  1451. } /* zds->hostageByte */
  1452. return 0;
  1453. } /* zds->outEnd == zds->outStart */
  1454. if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
  1455. input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */
  1456. zds->hostageByte=1;
  1457. }
  1458. return 1;
  1459. } /* nextSrcSizeHint==0 */
  1460. nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */
  1461. assert(zds->inPos <= nextSrcSizeHint);
  1462. nextSrcSizeHint -= zds->inPos; /* part already loaded*/
  1463. return nextSrcSizeHint;
  1464. }
  1465. }
  1466. size_t ZSTD_decompressStream_simpleArgs (
  1467. ZSTD_DCtx* dctx,
  1468. void* dst, size_t dstCapacity, size_t* dstPos,
  1469. const void* src, size_t srcSize, size_t* srcPos)
  1470. {
  1471. ZSTD_outBuffer output = { dst, dstCapacity, *dstPos };
  1472. ZSTD_inBuffer input = { src, srcSize, *srcPos };
  1473. /* ZSTD_compress_generic() will check validity of dstPos and srcPos */
  1474. size_t const cErr = ZSTD_decompressStream(dctx, &output, &input);
  1475. *dstPos = output.pos;
  1476. *srcPos = input.pos;
  1477. return cErr;
  1478. }