zstd_decompress.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  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 "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
  52. #include "../common/allocations.h" /* ZSTD_customMalloc, ZSTD_customCalloc, ZSTD_customFree */
  53. #include "../common/error_private.h"
  54. #include "../common/zstd_internal.h" /* blockProperties_t */
  55. #include "../common/mem.h" /* low level memory routines */
  56. #include "../common/bits.h" /* ZSTD_highbit32 */
  57. #define FSE_STATIC_LINKING_ONLY
  58. #include "../common/fse.h"
  59. #include "../common/huf.h"
  60. #include <contrib/libs/xxhash/xxhash.h> /* XXH64_reset, XXH64_update, XXH64_digest, XXH64 */
  61. #include "zstd_decompress_internal.h" /* ZSTD_DCtx */
  62. #include "zstd_ddict.h" /* ZSTD_DDictDictContent */
  63. #include "zstd_decompress_block.h" /* ZSTD_decompressBlock_internal */
  64. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  65. # include "../legacy/zstd_legacy.h"
  66. #endif
  67. /*************************************
  68. * Multiple DDicts Hashset internals *
  69. *************************************/
  70. #define DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT 4
  71. #define DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT 3 /* These two constants represent SIZE_MULT/COUNT_MULT load factor without using a float.
  72. * Currently, that means a 0.75 load factor.
  73. * So, if count * COUNT_MULT / size * SIZE_MULT != 0, then we've exceeded
  74. * the load factor of the ddict hash set.
  75. */
  76. #define DDICT_HASHSET_TABLE_BASE_SIZE 64
  77. #define DDICT_HASHSET_RESIZE_FACTOR 2
  78. /* Hash function to determine starting position of dict insertion within the table
  79. * Returns an index between [0, hashSet->ddictPtrTableSize]
  80. */
  81. static size_t ZSTD_DDictHashSet_getIndex(const ZSTD_DDictHashSet* hashSet, U32 dictID) {
  82. const U64 hash = XXH64(&dictID, sizeof(U32), 0);
  83. /* DDict ptr table size is a multiple of 2, use size - 1 as mask to get index within [0, hashSet->ddictPtrTableSize) */
  84. return hash & (hashSet->ddictPtrTableSize - 1);
  85. }
  86. /* Adds DDict to a hashset without resizing it.
  87. * If inserting a DDict with a dictID that already exists in the set, replaces the one in the set.
  88. * Returns 0 if successful, or a zstd error code if something went wrong.
  89. */
  90. static size_t ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict) {
  91. const U32 dictID = ZSTD_getDictID_fromDDict(ddict);
  92. size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID);
  93. const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1;
  94. RETURN_ERROR_IF(hashSet->ddictPtrCount == hashSet->ddictPtrTableSize, GENERIC, "Hash set is full!");
  95. DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx);
  96. while (hashSet->ddictPtrTable[idx] != NULL) {
  97. /* Replace existing ddict if inserting ddict with same dictID */
  98. if (ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]) == dictID) {
  99. DEBUGLOG(4, "DictID already exists, replacing rather than adding");
  100. hashSet->ddictPtrTable[idx] = ddict;
  101. return 0;
  102. }
  103. idx &= idxRangeMask;
  104. idx++;
  105. }
  106. DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
  107. hashSet->ddictPtrTable[idx] = ddict;
  108. hashSet->ddictPtrCount++;
  109. return 0;
  110. }
  111. /* Expands hash table by factor of DDICT_HASHSET_RESIZE_FACTOR and
  112. * rehashes all values, allocates new table, frees old table.
  113. * Returns 0 on success, otherwise a zstd error code.
  114. */
  115. static size_t ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) {
  116. size_t newTableSize = hashSet->ddictPtrTableSize * DDICT_HASHSET_RESIZE_FACTOR;
  117. const ZSTD_DDict** newTable = (const ZSTD_DDict**)ZSTD_customCalloc(sizeof(ZSTD_DDict*) * newTableSize, customMem);
  118. const ZSTD_DDict** oldTable = hashSet->ddictPtrTable;
  119. size_t oldTableSize = hashSet->ddictPtrTableSize;
  120. size_t i;
  121. DEBUGLOG(4, "Expanding DDict hash table! Old size: %zu new size: %zu", oldTableSize, newTableSize);
  122. RETURN_ERROR_IF(!newTable, memory_allocation, "Expanded hashset allocation failed!");
  123. hashSet->ddictPtrTable = newTable;
  124. hashSet->ddictPtrTableSize = newTableSize;
  125. hashSet->ddictPtrCount = 0;
  126. for (i = 0; i < oldTableSize; ++i) {
  127. if (oldTable[i] != NULL) {
  128. FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, oldTable[i]), "");
  129. }
  130. }
  131. ZSTD_customFree((void*)oldTable, customMem);
  132. DEBUGLOG(4, "Finished re-hash");
  133. return 0;
  134. }
  135. /* Fetches a DDict with the given dictID
  136. * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL.
  137. */
  138. static const ZSTD_DDict* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet* hashSet, U32 dictID) {
  139. size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID);
  140. const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1;
  141. DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx);
  142. for (;;) {
  143. size_t currDictID = ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]);
  144. if (currDictID == dictID || currDictID == 0) {
  145. /* currDictID == 0 implies a NULL ddict entry */
  146. break;
  147. } else {
  148. idx &= idxRangeMask; /* Goes to start of table when we reach the end */
  149. idx++;
  150. }
  151. }
  152. DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
  153. return hashSet->ddictPtrTable[idx];
  154. }
  155. /* Allocates space for and returns a ddict hash set
  156. * The hash set's ZSTD_DDict* table has all values automatically set to NULL to begin with.
  157. * Returns NULL if allocation failed.
  158. */
  159. static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) {
  160. ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)ZSTD_customMalloc(sizeof(ZSTD_DDictHashSet), customMem);
  161. DEBUGLOG(4, "Allocating new hash set");
  162. if (!ret)
  163. return NULL;
  164. ret->ddictPtrTable = (const ZSTD_DDict**)ZSTD_customCalloc(DDICT_HASHSET_TABLE_BASE_SIZE * sizeof(ZSTD_DDict*), customMem);
  165. if (!ret->ddictPtrTable) {
  166. ZSTD_customFree(ret, customMem);
  167. return NULL;
  168. }
  169. ret->ddictPtrTableSize = DDICT_HASHSET_TABLE_BASE_SIZE;
  170. ret->ddictPtrCount = 0;
  171. return ret;
  172. }
  173. /* Frees the table of ZSTD_DDict* within a hashset, then frees the hashset itself.
  174. * Note: The ZSTD_DDict* within the table are NOT freed.
  175. */
  176. static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) {
  177. DEBUGLOG(4, "Freeing ddict hash set");
  178. if (hashSet && hashSet->ddictPtrTable) {
  179. ZSTD_customFree((void*)hashSet->ddictPtrTable, customMem);
  180. }
  181. if (hashSet) {
  182. ZSTD_customFree(hashSet, customMem);
  183. }
  184. }
  185. /* Public function: Adds a DDict into the ZSTD_DDictHashSet, possibly triggering a resize of the hash set.
  186. * Returns 0 on success, or a ZSTD error.
  187. */
  188. static size_t ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict, ZSTD_customMem customMem) {
  189. DEBUGLOG(4, "Adding dict ID: %u to hashset with - Count: %zu Tablesize: %zu", ZSTD_getDictID_fromDDict(ddict), hashSet->ddictPtrCount, hashSet->ddictPtrTableSize);
  190. if (hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT / hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT != 0) {
  191. FORWARD_IF_ERROR(ZSTD_DDictHashSet_expand(hashSet, customMem), "");
  192. }
  193. FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, ddict), "");
  194. return 0;
  195. }
  196. /*-*************************************************************
  197. * Context management
  198. ***************************************************************/
  199. size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)
  200. {
  201. if (dctx==NULL) return 0; /* support sizeof NULL */
  202. return sizeof(*dctx)
  203. + ZSTD_sizeof_DDict(dctx->ddictLocal)
  204. + dctx->inBuffSize + dctx->outBuffSize;
  205. }
  206. size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
  207. static size_t ZSTD_startingInputLength(ZSTD_format_e format)
  208. {
  209. size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format);
  210. /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
  211. assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
  212. return startingInputLength;
  213. }
  214. static void ZSTD_DCtx_resetParameters(ZSTD_DCtx* dctx)
  215. {
  216. assert(dctx->streamStage == zdss_init);
  217. dctx->format = ZSTD_f_zstd1;
  218. dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
  219. dctx->outBufferMode = ZSTD_bm_buffered;
  220. dctx->forceIgnoreChecksum = ZSTD_d_validateChecksum;
  221. dctx->refMultipleDDicts = ZSTD_rmd_refSingleDDict;
  222. dctx->disableHufAsm = 0;
  223. dctx->maxBlockSizeParam = 0;
  224. }
  225. static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
  226. {
  227. dctx->staticSize = 0;
  228. dctx->ddict = NULL;
  229. dctx->ddictLocal = NULL;
  230. dctx->dictEnd = NULL;
  231. dctx->ddictIsCold = 0;
  232. dctx->dictUses = ZSTD_dont_use;
  233. dctx->inBuff = NULL;
  234. dctx->inBuffSize = 0;
  235. dctx->outBuffSize = 0;
  236. dctx->streamStage = zdss_init;
  237. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  238. dctx->legacyContext = NULL;
  239. dctx->previousLegacyVersion = 0;
  240. #endif
  241. dctx->noForwardProgress = 0;
  242. dctx->oversizedDuration = 0;
  243. dctx->isFrameDecompression = 1;
  244. #if DYNAMIC_BMI2
  245. dctx->bmi2 = ZSTD_cpuSupportsBmi2();
  246. #endif
  247. dctx->ddictSet = NULL;
  248. ZSTD_DCtx_resetParameters(dctx);
  249. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  250. dctx->dictContentEndForFuzzing = NULL;
  251. #endif
  252. }
  253. ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
  254. {
  255. ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;
  256. if ((size_t)workspace & 7) return NULL; /* 8-aligned */
  257. if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */
  258. ZSTD_initDCtx_internal(dctx);
  259. dctx->staticSize = workspaceSize;
  260. dctx->inBuff = (char*)(dctx+1);
  261. return dctx;
  262. }
  263. static ZSTD_DCtx* ZSTD_createDCtx_internal(ZSTD_customMem customMem) {
  264. if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL;
  265. { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_customMalloc(sizeof(*dctx), customMem);
  266. if (!dctx) return NULL;
  267. dctx->customMem = customMem;
  268. ZSTD_initDCtx_internal(dctx);
  269. return dctx;
  270. }
  271. }
  272. ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
  273. {
  274. return ZSTD_createDCtx_internal(customMem);
  275. }
  276. ZSTD_DCtx* ZSTD_createDCtx(void)
  277. {
  278. DEBUGLOG(3, "ZSTD_createDCtx");
  279. return ZSTD_createDCtx_internal(ZSTD_defaultCMem);
  280. }
  281. static void ZSTD_clearDict(ZSTD_DCtx* dctx)
  282. {
  283. ZSTD_freeDDict(dctx->ddictLocal);
  284. dctx->ddictLocal = NULL;
  285. dctx->ddict = NULL;
  286. dctx->dictUses = ZSTD_dont_use;
  287. }
  288. size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
  289. {
  290. if (dctx==NULL) return 0; /* support free on NULL */
  291. RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx");
  292. { ZSTD_customMem const cMem = dctx->customMem;
  293. ZSTD_clearDict(dctx);
  294. ZSTD_customFree(dctx->inBuff, cMem);
  295. dctx->inBuff = NULL;
  296. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  297. if (dctx->legacyContext)
  298. ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
  299. #endif
  300. if (dctx->ddictSet) {
  301. ZSTD_freeDDictHashSet(dctx->ddictSet, cMem);
  302. dctx->ddictSet = NULL;
  303. }
  304. ZSTD_customFree(dctx, cMem);
  305. return 0;
  306. }
  307. }
  308. /* no longer useful */
  309. void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
  310. {
  311. size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
  312. ZSTD_memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
  313. }
  314. /* Given a dctx with a digested frame params, re-selects the correct ZSTD_DDict based on
  315. * the requested dict ID from the frame. If there exists a reference to the correct ZSTD_DDict, then
  316. * accordingly sets the ddict to be used to decompress the frame.
  317. *
  318. * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is.
  319. *
  320. * ZSTD_d_refMultipleDDicts must be enabled for this function to be called.
  321. */
  322. static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx* dctx) {
  323. assert(dctx->refMultipleDDicts && dctx->ddictSet);
  324. DEBUGLOG(4, "Adjusting DDict based on requested dict ID from frame");
  325. if (dctx->ddict) {
  326. const ZSTD_DDict* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID);
  327. if (frameDDict) {
  328. DEBUGLOG(4, "DDict found!");
  329. ZSTD_clearDict(dctx);
  330. dctx->dictID = dctx->fParams.dictID;
  331. dctx->ddict = frameDDict;
  332. dctx->dictUses = ZSTD_use_indefinitely;
  333. }
  334. }
  335. }
  336. /*-*************************************************************
  337. * Frame header decoding
  338. ***************************************************************/
  339. /*! ZSTD_isFrame() :
  340. * Tells if the content of `buffer` starts with a valid Frame Identifier.
  341. * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
  342. * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
  343. * Note 3 : Skippable Frame Identifiers are considered valid. */
  344. unsigned ZSTD_isFrame(const void* buffer, size_t size)
  345. {
  346. if (size < ZSTD_FRAMEIDSIZE) return 0;
  347. { U32 const magic = MEM_readLE32(buffer);
  348. if (magic == ZSTD_MAGICNUMBER) return 1;
  349. if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
  350. }
  351. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  352. if (ZSTD_isLegacy(buffer, size)) return 1;
  353. #endif
  354. return 0;
  355. }
  356. /*! ZSTD_isSkippableFrame() :
  357. * Tells if the content of `buffer` starts with a valid Frame Identifier for a skippable frame.
  358. * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
  359. */
  360. unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size)
  361. {
  362. if (size < ZSTD_FRAMEIDSIZE) return 0;
  363. { U32 const magic = MEM_readLE32(buffer);
  364. if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
  365. }
  366. return 0;
  367. }
  368. /** ZSTD_frameHeaderSize_internal() :
  369. * srcSize must be large enough to reach header size fields.
  370. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.
  371. * @return : size of the Frame Header
  372. * or an error code, which can be tested with ZSTD_isError() */
  373. static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)
  374. {
  375. size_t const minInputSize = ZSTD_startingInputLength(format);
  376. RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong, "");
  377. { BYTE const fhd = ((const BYTE*)src)[minInputSize-1];
  378. U32 const dictID= fhd & 3;
  379. U32 const singleSegment = (fhd >> 5) & 1;
  380. U32 const fcsId = fhd >> 6;
  381. return minInputSize + !singleSegment
  382. + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
  383. + (singleSegment && !fcsId);
  384. }
  385. }
  386. /** ZSTD_frameHeaderSize() :
  387. * srcSize must be >= ZSTD_frameHeaderSize_prefix.
  388. * @return : size of the Frame Header,
  389. * or an error code (if srcSize is too small) */
  390. size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
  391. {
  392. return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
  393. }
  394. /** ZSTD_getFrameHeader_advanced() :
  395. * decode Frame Header, or require larger `srcSize`.
  396. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
  397. * @return : 0, `zfhPtr` is correctly filled,
  398. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  399. ** or an error code, which can be tested using ZSTD_isError() */
  400. size_t ZSTD_getFrameHeader_advanced(ZSTD_FrameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
  401. {
  402. const BYTE* ip = (const BYTE*)src;
  403. size_t const minInputSize = ZSTD_startingInputLength(format);
  404. DEBUGLOG(5, "ZSTD_getFrameHeader_advanced: minInputSize = %zu, srcSize = %zu", minInputSize, srcSize);
  405. if (srcSize > 0) {
  406. /* note : technically could be considered an assert(), since it's an invalid entry */
  407. RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter : src==NULL, but srcSize>0");
  408. }
  409. if (srcSize < minInputSize) {
  410. if (srcSize > 0 && format != ZSTD_f_zstd1_magicless) {
  411. /* when receiving less than @minInputSize bytes,
  412. * control these bytes at least correspond to a supported magic number
  413. * in order to error out early if they don't.
  414. **/
  415. size_t const toCopy = MIN(4, srcSize);
  416. unsigned char hbuf[4]; MEM_writeLE32(hbuf, ZSTD_MAGICNUMBER);
  417. assert(src != NULL);
  418. ZSTD_memcpy(hbuf, src, toCopy);
  419. if ( MEM_readLE32(hbuf) != ZSTD_MAGICNUMBER ) {
  420. /* not a zstd frame : let's check if it's a skippable frame */
  421. MEM_writeLE32(hbuf, ZSTD_MAGIC_SKIPPABLE_START);
  422. ZSTD_memcpy(hbuf, src, toCopy);
  423. if ((MEM_readLE32(hbuf) & ZSTD_MAGIC_SKIPPABLE_MASK) != ZSTD_MAGIC_SKIPPABLE_START) {
  424. RETURN_ERROR(prefix_unknown,
  425. "first bytes don't correspond to any supported magic number");
  426. } } }
  427. return minInputSize;
  428. }
  429. ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzers may not understand that zfhPtr will be read only if return value is zero, since they are 2 different signals */
  430. if ( (format != ZSTD_f_zstd1_magicless)
  431. && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
  432. if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  433. /* skippable frame */
  434. if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)
  435. return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */
  436. ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr));
  437. zfhPtr->frameType = ZSTD_skippableFrame;
  438. zfhPtr->dictID = MEM_readLE32(src) - ZSTD_MAGIC_SKIPPABLE_START;
  439. zfhPtr->headerSize = ZSTD_SKIPPABLEHEADERSIZE;
  440. zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);
  441. return 0;
  442. }
  443. RETURN_ERROR(prefix_unknown, "");
  444. }
  445. /* ensure there is enough `srcSize` to fully read/decode frame header */
  446. { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);
  447. if (srcSize < fhsize) return fhsize;
  448. zfhPtr->headerSize = (U32)fhsize;
  449. }
  450. { BYTE const fhdByte = ip[minInputSize-1];
  451. size_t pos = minInputSize;
  452. U32 const dictIDSizeCode = fhdByte&3;
  453. U32 const checksumFlag = (fhdByte>>2)&1;
  454. U32 const singleSegment = (fhdByte>>5)&1;
  455. U32 const fcsID = fhdByte>>6;
  456. U64 windowSize = 0;
  457. U32 dictID = 0;
  458. U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;
  459. RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported,
  460. "reserved bits, must be zero");
  461. if (!singleSegment) {
  462. BYTE const wlByte = ip[pos++];
  463. U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
  464. RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge, "");
  465. windowSize = (1ULL << windowLog);
  466. windowSize += (windowSize >> 3) * (wlByte&7);
  467. }
  468. switch(dictIDSizeCode)
  469. {
  470. default:
  471. assert(0); /* impossible */
  472. ZSTD_FALLTHROUGH;
  473. case 0 : break;
  474. case 1 : dictID = ip[pos]; pos++; break;
  475. case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
  476. case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
  477. }
  478. switch(fcsID)
  479. {
  480. default:
  481. assert(0); /* impossible */
  482. ZSTD_FALLTHROUGH;
  483. case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
  484. case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
  485. case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
  486. case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
  487. }
  488. if (singleSegment) windowSize = frameContentSize;
  489. zfhPtr->frameType = ZSTD_frame;
  490. zfhPtr->frameContentSize = frameContentSize;
  491. zfhPtr->windowSize = windowSize;
  492. zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  493. zfhPtr->dictID = dictID;
  494. zfhPtr->checksumFlag = checksumFlag;
  495. }
  496. return 0;
  497. }
  498. /** ZSTD_getFrameHeader() :
  499. * decode Frame Header, or require larger `srcSize`.
  500. * note : this function does not consume input, it only reads it.
  501. * @return : 0, `zfhPtr` is correctly filled,
  502. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  503. * or an error code, which can be tested using ZSTD_isError() */
  504. size_t ZSTD_getFrameHeader(ZSTD_FrameHeader* zfhPtr, const void* src, size_t srcSize)
  505. {
  506. return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);
  507. }
  508. /** ZSTD_getFrameContentSize() :
  509. * compatible with legacy mode
  510. * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
  511. * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
  512. * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
  513. unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
  514. {
  515. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  516. if (ZSTD_isLegacy(src, srcSize)) {
  517. unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize);
  518. return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret;
  519. }
  520. #endif
  521. { ZSTD_FrameHeader zfh;
  522. if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)
  523. return ZSTD_CONTENTSIZE_ERROR;
  524. if (zfh.frameType == ZSTD_skippableFrame) {
  525. return 0;
  526. } else {
  527. return zfh.frameContentSize;
  528. } }
  529. }
  530. static size_t readSkippableFrameSize(void const* src, size_t srcSize)
  531. {
  532. size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE;
  533. U32 sizeU32;
  534. RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, "");
  535. sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE);
  536. RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32,
  537. frameParameter_unsupported, "");
  538. { size_t const skippableSize = skippableHeaderSize + sizeU32;
  539. RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong, "");
  540. return skippableSize;
  541. }
  542. }
  543. /*! ZSTD_readSkippableFrame() :
  544. * Retrieves content of a skippable frame, and writes it to dst buffer.
  545. *
  546. * The parameter magicVariant will receive the magicVariant that was supplied when the frame was written,
  547. * i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START. This can be NULL if the caller is not interested
  548. * in the magicVariant.
  549. *
  550. * Returns an error if destination buffer is not large enough, or if this is not a valid skippable frame.
  551. *
  552. * @return : number of bytes written or a ZSTD error.
  553. */
  554. size_t ZSTD_readSkippableFrame(void* dst, size_t dstCapacity,
  555. unsigned* magicVariant, /* optional, can be NULL */
  556. const void* src, size_t srcSize)
  557. {
  558. RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, "");
  559. { U32 const magicNumber = MEM_readLE32(src);
  560. size_t skippableFrameSize = readSkippableFrameSize(src, srcSize);
  561. size_t skippableContentSize = skippableFrameSize - ZSTD_SKIPPABLEHEADERSIZE;
  562. /* check input validity */
  563. RETURN_ERROR_IF(!ZSTD_isSkippableFrame(src, srcSize), frameParameter_unsupported, "");
  564. RETURN_ERROR_IF(skippableFrameSize < ZSTD_SKIPPABLEHEADERSIZE || skippableFrameSize > srcSize, srcSize_wrong, "");
  565. RETURN_ERROR_IF(skippableContentSize > dstCapacity, dstSize_tooSmall, "");
  566. /* deliver payload */
  567. if (skippableContentSize > 0 && dst != NULL)
  568. ZSTD_memcpy(dst, (const BYTE *)src + ZSTD_SKIPPABLEHEADERSIZE, skippableContentSize);
  569. if (magicVariant != NULL)
  570. *magicVariant = magicNumber - ZSTD_MAGIC_SKIPPABLE_START;
  571. return skippableContentSize;
  572. }
  573. }
  574. /** ZSTD_findDecompressedSize() :
  575. * `srcSize` must be the exact length of some number of ZSTD compressed and/or
  576. * skippable frames
  577. * note: compatible with legacy mode
  578. * @return : decompressed size of the frames contained */
  579. unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
  580. {
  581. unsigned long long totalDstSize = 0;
  582. while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) {
  583. U32 const magicNumber = MEM_readLE32(src);
  584. if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  585. size_t const skippableSize = readSkippableFrameSize(src, srcSize);
  586. if (ZSTD_isError(skippableSize)) return ZSTD_CONTENTSIZE_ERROR;
  587. assert(skippableSize <= srcSize);
  588. src = (const BYTE *)src + skippableSize;
  589. srcSize -= skippableSize;
  590. continue;
  591. }
  592. { unsigned long long const fcs = ZSTD_getFrameContentSize(src, srcSize);
  593. if (fcs >= ZSTD_CONTENTSIZE_ERROR) return fcs;
  594. if (totalDstSize + fcs < totalDstSize)
  595. return ZSTD_CONTENTSIZE_ERROR; /* check for overflow */
  596. totalDstSize += fcs;
  597. }
  598. /* skip to next frame */
  599. { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
  600. if (ZSTD_isError(frameSrcSize)) return ZSTD_CONTENTSIZE_ERROR;
  601. assert(frameSrcSize <= srcSize);
  602. src = (const BYTE *)src + frameSrcSize;
  603. srcSize -= frameSrcSize;
  604. }
  605. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  606. if (srcSize) return ZSTD_CONTENTSIZE_ERROR;
  607. return totalDstSize;
  608. }
  609. /** ZSTD_getDecompressedSize() :
  610. * compatible with legacy mode
  611. * @return : decompressed size if known, 0 otherwise
  612. note : 0 can mean any of the following :
  613. - frame content is empty
  614. - decompressed size field is not present in frame header
  615. - frame header unknown / not supported
  616. - frame header not complete (`srcSize` too small) */
  617. unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
  618. {
  619. unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
  620. ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);
  621. return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;
  622. }
  623. /** ZSTD_decodeFrameHeader() :
  624. * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
  625. * If multiple DDict references are enabled, also will choose the correct DDict to use.
  626. * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
  627. static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
  628. {
  629. size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);
  630. if (ZSTD_isError(result)) return result; /* invalid header */
  631. RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small");
  632. /* Reference DDict requested by frame if dctx references multiple ddicts */
  633. if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts && dctx->ddictSet) {
  634. ZSTD_DCtx_selectFrameDDict(dctx);
  635. }
  636. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  637. /* Skip the dictID check in fuzzing mode, because it makes the search
  638. * harder.
  639. */
  640. RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID),
  641. dictionary_wrong, "");
  642. #endif
  643. dctx->validateChecksum = (dctx->fParams.checksumFlag && !dctx->forceIgnoreChecksum) ? 1 : 0;
  644. if (dctx->validateChecksum) XXH64_reset(&dctx->xxhState, 0);
  645. dctx->processedCSize += headerSize;
  646. return 0;
  647. }
  648. static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
  649. {
  650. ZSTD_frameSizeInfo frameSizeInfo;
  651. frameSizeInfo.compressedSize = ret;
  652. frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
  653. return frameSizeInfo;
  654. }
  655. static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize, ZSTD_format_e format)
  656. {
  657. ZSTD_frameSizeInfo frameSizeInfo;
  658. ZSTD_memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));
  659. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  660. if (format == ZSTD_f_zstd1 && ZSTD_isLegacy(src, srcSize))
  661. return ZSTD_findFrameSizeInfoLegacy(src, srcSize);
  662. #endif
  663. if (format == ZSTD_f_zstd1 && (srcSize >= ZSTD_SKIPPABLEHEADERSIZE)
  664. && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  665. frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize);
  666. assert(ZSTD_isError(frameSizeInfo.compressedSize) ||
  667. frameSizeInfo.compressedSize <= srcSize);
  668. return frameSizeInfo;
  669. } else {
  670. const BYTE* ip = (const BYTE*)src;
  671. const BYTE* const ipstart = ip;
  672. size_t remainingSize = srcSize;
  673. size_t nbBlocks = 0;
  674. ZSTD_FrameHeader zfh;
  675. /* Extract Frame Header */
  676. { size_t const ret = ZSTD_getFrameHeader_advanced(&zfh, src, srcSize, format);
  677. if (ZSTD_isError(ret))
  678. return ZSTD_errorFrameSizeInfo(ret);
  679. if (ret > 0)
  680. return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
  681. }
  682. ip += zfh.headerSize;
  683. remainingSize -= zfh.headerSize;
  684. /* Iterate over each block */
  685. while (1) {
  686. blockProperties_t blockProperties;
  687. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
  688. if (ZSTD_isError(cBlockSize))
  689. return ZSTD_errorFrameSizeInfo(cBlockSize);
  690. if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
  691. return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
  692. ip += ZSTD_blockHeaderSize + cBlockSize;
  693. remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
  694. nbBlocks++;
  695. if (blockProperties.lastBlock) break;
  696. }
  697. /* Final frame content checksum */
  698. if (zfh.checksumFlag) {
  699. if (remainingSize < 4)
  700. return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));
  701. ip += 4;
  702. }
  703. frameSizeInfo.nbBlocks = nbBlocks;
  704. frameSizeInfo.compressedSize = (size_t)(ip - ipstart);
  705. frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)
  706. ? zfh.frameContentSize
  707. : (unsigned long long)nbBlocks * zfh.blockSizeMax;
  708. return frameSizeInfo;
  709. }
  710. }
  711. static size_t ZSTD_findFrameCompressedSize_advanced(const void *src, size_t srcSize, ZSTD_format_e format) {
  712. ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, format);
  713. return frameSizeInfo.compressedSize;
  714. }
  715. /** ZSTD_findFrameCompressedSize() :
  716. * See docs in zstd.h
  717. * Note: compatible with legacy mode */
  718. size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
  719. {
  720. return ZSTD_findFrameCompressedSize_advanced(src, srcSize, ZSTD_f_zstd1);
  721. }
  722. /** ZSTD_decompressBound() :
  723. * compatible with legacy mode
  724. * `src` must point to the start of a ZSTD frame or a skippable frame
  725. * `srcSize` must be at least as large as the frame contained
  726. * @return : the maximum decompressed size of the compressed source
  727. */
  728. unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
  729. {
  730. unsigned long long bound = 0;
  731. /* Iterate over each frame */
  732. while (srcSize > 0) {
  733. ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1);
  734. size_t const compressedSize = frameSizeInfo.compressedSize;
  735. unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
  736. if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
  737. return ZSTD_CONTENTSIZE_ERROR;
  738. assert(srcSize >= compressedSize);
  739. src = (const BYTE*)src + compressedSize;
  740. srcSize -= compressedSize;
  741. bound += decompressedBound;
  742. }
  743. return bound;
  744. }
  745. size_t ZSTD_decompressionMargin(void const* src, size_t srcSize)
  746. {
  747. size_t margin = 0;
  748. unsigned maxBlockSize = 0;
  749. /* Iterate over each frame */
  750. while (srcSize > 0) {
  751. ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1);
  752. size_t const compressedSize = frameSizeInfo.compressedSize;
  753. unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
  754. ZSTD_FrameHeader zfh;
  755. FORWARD_IF_ERROR(ZSTD_getFrameHeader(&zfh, src, srcSize), "");
  756. if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
  757. return ERROR(corruption_detected);
  758. if (zfh.frameType == ZSTD_frame) {
  759. /* Add the frame header to our margin */
  760. margin += zfh.headerSize;
  761. /* Add the checksum to our margin */
  762. margin += zfh.checksumFlag ? 4 : 0;
  763. /* Add 3 bytes per block */
  764. margin += 3 * frameSizeInfo.nbBlocks;
  765. /* Compute the max block size */
  766. maxBlockSize = MAX(maxBlockSize, zfh.blockSizeMax);
  767. } else {
  768. assert(zfh.frameType == ZSTD_skippableFrame);
  769. /* Add the entire skippable frame size to our margin. */
  770. margin += compressedSize;
  771. }
  772. assert(srcSize >= compressedSize);
  773. src = (const BYTE*)src + compressedSize;
  774. srcSize -= compressedSize;
  775. }
  776. /* Add the max block size back to the margin. */
  777. margin += maxBlockSize;
  778. return margin;
  779. }
  780. /*-*************************************************************
  781. * Frame decoding
  782. ***************************************************************/
  783. /** ZSTD_insertBlock() :
  784. * insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
  785. size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
  786. {
  787. DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize);
  788. ZSTD_checkContinuity(dctx, blockStart, blockSize);
  789. dctx->previousDstEnd = (const char*)blockStart + blockSize;
  790. return blockSize;
  791. }
  792. static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
  793. const void* src, size_t srcSize)
  794. {
  795. DEBUGLOG(5, "ZSTD_copyRawBlock");
  796. RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, "");
  797. if (dst == NULL) {
  798. if (srcSize == 0) return 0;
  799. RETURN_ERROR(dstBuffer_null, "");
  800. }
  801. ZSTD_memmove(dst, src, srcSize);
  802. return srcSize;
  803. }
  804. static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
  805. BYTE b,
  806. size_t regenSize)
  807. {
  808. RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, "");
  809. if (dst == NULL) {
  810. if (regenSize == 0) return 0;
  811. RETURN_ERROR(dstBuffer_null, "");
  812. }
  813. ZSTD_memset(dst, b, regenSize);
  814. return regenSize;
  815. }
  816. static void ZSTD_DCtx_trace_end(ZSTD_DCtx const* dctx, U64 uncompressedSize, U64 compressedSize, int streaming)
  817. {
  818. #if ZSTD_TRACE
  819. if (dctx->traceCtx && ZSTD_trace_decompress_end != NULL) {
  820. ZSTD_Trace trace;
  821. ZSTD_memset(&trace, 0, sizeof(trace));
  822. trace.version = ZSTD_VERSION_NUMBER;
  823. trace.streaming = streaming;
  824. if (dctx->ddict) {
  825. trace.dictionaryID = ZSTD_getDictID_fromDDict(dctx->ddict);
  826. trace.dictionarySize = ZSTD_DDict_dictSize(dctx->ddict);
  827. trace.dictionaryIsCold = dctx->ddictIsCold;
  828. }
  829. trace.uncompressedSize = (size_t)uncompressedSize;
  830. trace.compressedSize = (size_t)compressedSize;
  831. trace.dctx = dctx;
  832. ZSTD_trace_decompress_end(dctx->traceCtx, &trace);
  833. }
  834. #else
  835. (void)dctx;
  836. (void)uncompressedSize;
  837. (void)compressedSize;
  838. (void)streaming;
  839. #endif
  840. }
  841. /*! ZSTD_decompressFrame() :
  842. * @dctx must be properly initialized
  843. * will update *srcPtr and *srcSizePtr,
  844. * to make *srcPtr progress by one frame. */
  845. static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
  846. void* dst, size_t dstCapacity,
  847. const void** srcPtr, size_t *srcSizePtr)
  848. {
  849. const BYTE* const istart = (const BYTE*)(*srcPtr);
  850. const BYTE* ip = istart;
  851. BYTE* const ostart = (BYTE*)dst;
  852. BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart;
  853. BYTE* op = ostart;
  854. size_t remainingSrcSize = *srcSizePtr;
  855. DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr);
  856. /* check */
  857. RETURN_ERROR_IF(
  858. remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize,
  859. srcSize_wrong, "");
  860. /* Frame Header */
  861. { size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal(
  862. ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format);
  863. if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
  864. RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize,
  865. srcSize_wrong, "");
  866. FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) , "");
  867. ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize;
  868. }
  869. /* Shrink the blockSizeMax if enabled */
  870. if (dctx->maxBlockSizeParam != 0)
  871. dctx->fParams.blockSizeMax = MIN(dctx->fParams.blockSizeMax, (unsigned)dctx->maxBlockSizeParam);
  872. /* Loop on each block */
  873. while (1) {
  874. BYTE* oBlockEnd = oend;
  875. size_t decodedSize;
  876. blockProperties_t blockProperties;
  877. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties);
  878. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  879. ip += ZSTD_blockHeaderSize;
  880. remainingSrcSize -= ZSTD_blockHeaderSize;
  881. RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong, "");
  882. if (ip >= op && ip < oBlockEnd) {
  883. /* We are decompressing in-place. Limit the output pointer so that we
  884. * don't overwrite the block that we are currently reading. This will
  885. * fail decompression if the input & output pointers aren't spaced
  886. * far enough apart.
  887. *
  888. * This is important to set, even when the pointers are far enough
  889. * apart, because ZSTD_decompressBlock_internal() can decide to store
  890. * literals in the output buffer, after the block it is decompressing.
  891. * Since we don't want anything to overwrite our input, we have to tell
  892. * ZSTD_decompressBlock_internal to never write past ip.
  893. *
  894. * See ZSTD_allocateLiteralsBuffer() for reference.
  895. */
  896. oBlockEnd = op + (ip - op);
  897. }
  898. switch(blockProperties.blockType)
  899. {
  900. case bt_compressed:
  901. assert(dctx->isFrameDecompression == 1);
  902. decodedSize = ZSTD_decompressBlock_internal(dctx, op, (size_t)(oBlockEnd-op), ip, cBlockSize, not_streaming);
  903. break;
  904. case bt_raw :
  905. /* Use oend instead of oBlockEnd because this function is safe to overlap. It uses memmove. */
  906. decodedSize = ZSTD_copyRawBlock(op, (size_t)(oend-op), ip, cBlockSize);
  907. break;
  908. case bt_rle :
  909. decodedSize = ZSTD_setRleBlock(op, (size_t)(oBlockEnd-op), *ip, blockProperties.origSize);
  910. break;
  911. case bt_reserved :
  912. default:
  913. RETURN_ERROR(corruption_detected, "invalid block type");
  914. }
  915. FORWARD_IF_ERROR(decodedSize, "Block decompression failure");
  916. DEBUGLOG(5, "Decompressed block of dSize = %u", (unsigned)decodedSize);
  917. if (dctx->validateChecksum) {
  918. XXH64_update(&dctx->xxhState, op, decodedSize);
  919. }
  920. if (decodedSize) /* support dst = NULL,0 */ {
  921. op += decodedSize;
  922. }
  923. assert(ip != NULL);
  924. ip += cBlockSize;
  925. remainingSrcSize -= cBlockSize;
  926. if (blockProperties.lastBlock) break;
  927. }
  928. if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
  929. RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize,
  930. corruption_detected, "");
  931. }
  932. if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
  933. RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, "");
  934. if (!dctx->forceIgnoreChecksum) {
  935. U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
  936. U32 checkRead;
  937. checkRead = MEM_readLE32(ip);
  938. RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, "");
  939. }
  940. ip += 4;
  941. remainingSrcSize -= 4;
  942. }
  943. ZSTD_DCtx_trace_end(dctx, (U64)(op-ostart), (U64)(ip-istart), /* streaming */ 0);
  944. /* Allow caller to get size read */
  945. DEBUGLOG(4, "ZSTD_decompressFrame: decompressed frame of size %i, consuming %i bytes of input", (int)(op-ostart), (int)(ip - (const BYTE*)*srcPtr));
  946. *srcPtr = ip;
  947. *srcSizePtr = remainingSrcSize;
  948. return (size_t)(op-ostart);
  949. }
  950. static
  951. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  952. size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
  953. void* dst, size_t dstCapacity,
  954. const void* src, size_t srcSize,
  955. const void* dict, size_t dictSize,
  956. const ZSTD_DDict* ddict)
  957. {
  958. void* const dststart = dst;
  959. int moreThan1Frame = 0;
  960. DEBUGLOG(5, "ZSTD_decompressMultiFrame");
  961. assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */
  962. if (ddict) {
  963. dict = ZSTD_DDict_dictContent(ddict);
  964. dictSize = ZSTD_DDict_dictSize(ddict);
  965. }
  966. while (srcSize >= ZSTD_startingInputLength(dctx->format)) {
  967. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  968. if (dctx->format == ZSTD_f_zstd1 && ZSTD_isLegacy(src, srcSize)) {
  969. size_t decodedSize;
  970. size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
  971. if (ZSTD_isError(frameSize)) return frameSize;
  972. RETURN_ERROR_IF(dctx->staticSize, memory_allocation,
  973. "legacy support is not compatible with static dctx");
  974. decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
  975. if (ZSTD_isError(decodedSize)) return decodedSize;
  976. {
  977. unsigned long long const expectedSize = ZSTD_getFrameContentSize(src, srcSize);
  978. RETURN_ERROR_IF(expectedSize == ZSTD_CONTENTSIZE_ERROR, corruption_detected, "Corrupted frame header!");
  979. if (expectedSize != ZSTD_CONTENTSIZE_UNKNOWN) {
  980. RETURN_ERROR_IF(expectedSize != decodedSize, corruption_detected,
  981. "Frame header size does not match decoded size!");
  982. }
  983. }
  984. assert(decodedSize <= dstCapacity);
  985. dst = (BYTE*)dst + decodedSize;
  986. dstCapacity -= decodedSize;
  987. src = (const BYTE*)src + frameSize;
  988. srcSize -= frameSize;
  989. continue;
  990. }
  991. #endif
  992. if (dctx->format == ZSTD_f_zstd1 && srcSize >= 4) {
  993. U32 const magicNumber = MEM_readLE32(src);
  994. DEBUGLOG(5, "reading magic number %08X", (unsigned)magicNumber);
  995. if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {
  996. /* skippable frame detected : skip it */
  997. size_t const skippableSize = readSkippableFrameSize(src, srcSize);
  998. FORWARD_IF_ERROR(skippableSize, "invalid skippable frame");
  999. assert(skippableSize <= srcSize);
  1000. src = (const BYTE *)src + skippableSize;
  1001. srcSize -= skippableSize;
  1002. continue; /* check next frame */
  1003. } }
  1004. if (ddict) {
  1005. /* we were called from ZSTD_decompress_usingDDict */
  1006. FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), "");
  1007. } else {
  1008. /* this will initialize correctly with no dict if dict == NULL, so
  1009. * use this in all cases but ddict */
  1010. FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), "");
  1011. }
  1012. ZSTD_checkContinuity(dctx, dst, dstCapacity);
  1013. { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
  1014. &src, &srcSize);
  1015. RETURN_ERROR_IF(
  1016. (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)
  1017. && (moreThan1Frame==1),
  1018. srcSize_wrong,
  1019. "At least one frame successfully completed, "
  1020. "but following bytes are garbage: "
  1021. "it's more likely to be a srcSize error, "
  1022. "specifying more input bytes than size of frame(s). "
  1023. "Note: one could be unlucky, it might be a corruption error instead, "
  1024. "happening right at the place where we expect zstd magic bytes. "
  1025. "But this is _much_ less likely than a srcSize field error.");
  1026. if (ZSTD_isError(res)) return res;
  1027. assert(res <= dstCapacity);
  1028. if (res != 0)
  1029. dst = (BYTE*)dst + res;
  1030. dstCapacity -= res;
  1031. }
  1032. moreThan1Frame = 1;
  1033. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  1034. RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed");
  1035. return (size_t)((BYTE*)dst - (BYTE*)dststart);
  1036. }
  1037. size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
  1038. void* dst, size_t dstCapacity,
  1039. const void* src, size_t srcSize,
  1040. const void* dict, size_t dictSize)
  1041. {
  1042. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
  1043. }
  1044. static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)
  1045. {
  1046. switch (dctx->dictUses) {
  1047. default:
  1048. assert(0 /* Impossible */);
  1049. ZSTD_FALLTHROUGH;
  1050. case ZSTD_dont_use:
  1051. ZSTD_clearDict(dctx);
  1052. return NULL;
  1053. case ZSTD_use_indefinitely:
  1054. return dctx->ddict;
  1055. case ZSTD_use_once:
  1056. dctx->dictUses = ZSTD_dont_use;
  1057. return dctx->ddict;
  1058. }
  1059. }
  1060. size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  1061. {
  1062. return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx));
  1063. }
  1064. size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  1065. {
  1066. #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
  1067. size_t regenSize;
  1068. ZSTD_DCtx* const dctx = ZSTD_createDCtx_internal(ZSTD_defaultCMem);
  1069. RETURN_ERROR_IF(dctx==NULL, memory_allocation, "NULL pointer!");
  1070. regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
  1071. ZSTD_freeDCtx(dctx);
  1072. return regenSize;
  1073. #else /* stack mode */
  1074. ZSTD_DCtx dctx;
  1075. ZSTD_initDCtx_internal(&dctx);
  1076. return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
  1077. #endif
  1078. }
  1079. /*-**************************************
  1080. * Advanced Streaming Decompression API
  1081. * Bufferless and synchronous
  1082. ****************************************/
  1083. size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
  1084. /**
  1085. * Similar to ZSTD_nextSrcSizeToDecompress(), but when a block input can be streamed, we
  1086. * allow taking a partial block as the input. Currently only raw uncompressed blocks can
  1087. * be streamed.
  1088. *
  1089. * For blocks that can be streamed, this allows us to reduce the latency until we produce
  1090. * output, and avoid copying the input.
  1091. *
  1092. * @param inputSize - The total amount of input that the caller currently has.
  1093. */
  1094. static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t inputSize) {
  1095. if (!(dctx->stage == ZSTDds_decompressBlock || dctx->stage == ZSTDds_decompressLastBlock))
  1096. return dctx->expected;
  1097. if (dctx->bType != bt_raw)
  1098. return dctx->expected;
  1099. return BOUNDED(1, inputSize, dctx->expected);
  1100. }
  1101. ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
  1102. switch(dctx->stage)
  1103. {
  1104. default: /* should not happen */
  1105. assert(0);
  1106. ZSTD_FALLTHROUGH;
  1107. case ZSTDds_getFrameHeaderSize:
  1108. ZSTD_FALLTHROUGH;
  1109. case ZSTDds_decodeFrameHeader:
  1110. return ZSTDnit_frameHeader;
  1111. case ZSTDds_decodeBlockHeader:
  1112. return ZSTDnit_blockHeader;
  1113. case ZSTDds_decompressBlock:
  1114. return ZSTDnit_block;
  1115. case ZSTDds_decompressLastBlock:
  1116. return ZSTDnit_lastBlock;
  1117. case ZSTDds_checkChecksum:
  1118. return ZSTDnit_checksum;
  1119. case ZSTDds_decodeSkippableHeader:
  1120. ZSTD_FALLTHROUGH;
  1121. case ZSTDds_skipFrame:
  1122. return ZSTDnit_skippableFrame;
  1123. }
  1124. }
  1125. static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
  1126. /** ZSTD_decompressContinue() :
  1127. * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
  1128. * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
  1129. * or an error code, which can be tested using ZSTD_isError() */
  1130. size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  1131. {
  1132. DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
  1133. /* Sanity check */
  1134. RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed");
  1135. ZSTD_checkContinuity(dctx, dst, dstCapacity);
  1136. dctx->processedCSize += srcSize;
  1137. switch (dctx->stage)
  1138. {
  1139. case ZSTDds_getFrameHeaderSize :
  1140. assert(src != NULL);
  1141. if (dctx->format == ZSTD_f_zstd1) { /* allows header */
  1142. assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */
  1143. if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  1144. ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
  1145. dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */
  1146. dctx->stage = ZSTDds_decodeSkippableHeader;
  1147. return 0;
  1148. } }
  1149. dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
  1150. if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
  1151. ZSTD_memcpy(dctx->headerBuffer, src, srcSize);
  1152. dctx->expected = dctx->headerSize - srcSize;
  1153. dctx->stage = ZSTDds_decodeFrameHeader;
  1154. return 0;
  1155. case ZSTDds_decodeFrameHeader:
  1156. assert(src != NULL);
  1157. ZSTD_memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
  1158. FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), "");
  1159. dctx->expected = ZSTD_blockHeaderSize;
  1160. dctx->stage = ZSTDds_decodeBlockHeader;
  1161. return 0;
  1162. case ZSTDds_decodeBlockHeader:
  1163. { blockProperties_t bp;
  1164. size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
  1165. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  1166. RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, "Block Size Exceeds Maximum");
  1167. dctx->expected = cBlockSize;
  1168. dctx->bType = bp.blockType;
  1169. dctx->rleSize = bp.origSize;
  1170. if (cBlockSize) {
  1171. dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
  1172. return 0;
  1173. }
  1174. /* empty block */
  1175. if (bp.lastBlock) {
  1176. if (dctx->fParams.checksumFlag) {
  1177. dctx->expected = 4;
  1178. dctx->stage = ZSTDds_checkChecksum;
  1179. } else {
  1180. dctx->expected = 0; /* end of frame */
  1181. dctx->stage = ZSTDds_getFrameHeaderSize;
  1182. }
  1183. } else {
  1184. dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */
  1185. dctx->stage = ZSTDds_decodeBlockHeader;
  1186. }
  1187. return 0;
  1188. }
  1189. case ZSTDds_decompressLastBlock:
  1190. case ZSTDds_decompressBlock:
  1191. DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock");
  1192. { size_t rSize;
  1193. switch(dctx->bType)
  1194. {
  1195. case bt_compressed:
  1196. DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed");
  1197. assert(dctx->isFrameDecompression == 1);
  1198. rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, is_streaming);
  1199. dctx->expected = 0; /* Streaming not supported */
  1200. break;
  1201. case bt_raw :
  1202. assert(srcSize <= dctx->expected);
  1203. rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
  1204. FORWARD_IF_ERROR(rSize, "ZSTD_copyRawBlock failed");
  1205. assert(rSize == srcSize);
  1206. dctx->expected -= rSize;
  1207. break;
  1208. case bt_rle :
  1209. rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize);
  1210. dctx->expected = 0; /* Streaming not supported */
  1211. break;
  1212. case bt_reserved : /* should never happen */
  1213. default:
  1214. RETURN_ERROR(corruption_detected, "invalid block type");
  1215. }
  1216. FORWARD_IF_ERROR(rSize, "");
  1217. RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum");
  1218. DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
  1219. dctx->decodedSize += rSize;
  1220. if (dctx->validateChecksum) XXH64_update(&dctx->xxhState, dst, rSize);
  1221. dctx->previousDstEnd = (char*)dst + rSize;
  1222. /* Stay on the same stage until we are finished streaming the block. */
  1223. if (dctx->expected > 0) {
  1224. return rSize;
  1225. }
  1226. if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */
  1227. DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize);
  1228. RETURN_ERROR_IF(
  1229. dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
  1230. && dctx->decodedSize != dctx->fParams.frameContentSize,
  1231. corruption_detected, "");
  1232. if (dctx->fParams.checksumFlag) { /* another round for frame checksum */
  1233. dctx->expected = 4;
  1234. dctx->stage = ZSTDds_checkChecksum;
  1235. } else {
  1236. ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1);
  1237. dctx->expected = 0; /* ends here */
  1238. dctx->stage = ZSTDds_getFrameHeaderSize;
  1239. }
  1240. } else {
  1241. dctx->stage = ZSTDds_decodeBlockHeader;
  1242. dctx->expected = ZSTD_blockHeaderSize;
  1243. }
  1244. return rSize;
  1245. }
  1246. case ZSTDds_checkChecksum:
  1247. assert(srcSize == 4); /* guaranteed by dctx->expected */
  1248. {
  1249. if (dctx->validateChecksum) {
  1250. U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
  1251. U32 const check32 = MEM_readLE32(src);
  1252. DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32);
  1253. RETURN_ERROR_IF(check32 != h32, checksum_wrong, "");
  1254. }
  1255. ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1);
  1256. dctx->expected = 0;
  1257. dctx->stage = ZSTDds_getFrameHeaderSize;
  1258. return 0;
  1259. }
  1260. case ZSTDds_decodeSkippableHeader:
  1261. assert(src != NULL);
  1262. assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);
  1263. assert(dctx->format != ZSTD_f_zstd1_magicless);
  1264. ZSTD_memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */
  1265. dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */
  1266. dctx->stage = ZSTDds_skipFrame;
  1267. return 0;
  1268. case ZSTDds_skipFrame:
  1269. dctx->expected = 0;
  1270. dctx->stage = ZSTDds_getFrameHeaderSize;
  1271. return 0;
  1272. default:
  1273. assert(0); /* impossible */
  1274. RETURN_ERROR(GENERIC, "impossible to reach"); /* some compilers require default to do something */
  1275. }
  1276. }
  1277. static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1278. {
  1279. dctx->dictEnd = dctx->previousDstEnd;
  1280. dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
  1281. dctx->prefixStart = dict;
  1282. dctx->previousDstEnd = (const char*)dict + dictSize;
  1283. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  1284. dctx->dictContentBeginForFuzzing = dctx->prefixStart;
  1285. dctx->dictContentEndForFuzzing = dctx->previousDstEnd;
  1286. #endif
  1287. return 0;
  1288. }
  1289. /*! ZSTD_loadDEntropy() :
  1290. * dict : must point at beginning of a valid zstd dictionary.
  1291. * @return : size of entropy tables read */
  1292. size_t
  1293. ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
  1294. const void* const dict, size_t const dictSize)
  1295. {
  1296. const BYTE* dictPtr = (const BYTE*)dict;
  1297. const BYTE* const dictEnd = dictPtr + dictSize;
  1298. RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted, "dict is too small");
  1299. assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */
  1300. dictPtr += 8; /* skip header = magic + dictID */
  1301. ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));
  1302. ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));
  1303. ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);
  1304. { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */
  1305. size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);
  1306. #ifdef HUF_FORCE_DECOMPRESS_X1
  1307. /* in minimal huffman, we always use X1 variants */
  1308. size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable,
  1309. dictPtr, dictEnd - dictPtr,
  1310. workspace, workspaceSize, /* flags */ 0);
  1311. #else
  1312. size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,
  1313. dictPtr, (size_t)(dictEnd - dictPtr),
  1314. workspace, workspaceSize, /* flags */ 0);
  1315. #endif
  1316. RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, "");
  1317. dictPtr += hSize;
  1318. }
  1319. { short offcodeNCount[MaxOff+1];
  1320. unsigned offcodeMaxValue = MaxOff, offcodeLog;
  1321. size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (size_t)(dictEnd-dictPtr));
  1322. RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, "");
  1323. RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, "");
  1324. RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, "");
  1325. ZSTD_buildFSETable( entropy->OFTable,
  1326. offcodeNCount, offcodeMaxValue,
  1327. OF_base, OF_bits,
  1328. offcodeLog,
  1329. entropy->workspace, sizeof(entropy->workspace),
  1330. /* bmi2 */0);
  1331. dictPtr += offcodeHeaderSize;
  1332. }
  1333. { short matchlengthNCount[MaxML+1];
  1334. unsigned matchlengthMaxValue = MaxML, matchlengthLog;
  1335. size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
  1336. RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, "");
  1337. RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, "");
  1338. RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, "");
  1339. ZSTD_buildFSETable( entropy->MLTable,
  1340. matchlengthNCount, matchlengthMaxValue,
  1341. ML_base, ML_bits,
  1342. matchlengthLog,
  1343. entropy->workspace, sizeof(entropy->workspace),
  1344. /* bmi2 */ 0);
  1345. dictPtr += matchlengthHeaderSize;
  1346. }
  1347. { short litlengthNCount[MaxLL+1];
  1348. unsigned litlengthMaxValue = MaxLL, litlengthLog;
  1349. size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (size_t)(dictEnd-dictPtr));
  1350. RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, "");
  1351. RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, "");
  1352. RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, "");
  1353. ZSTD_buildFSETable( entropy->LLTable,
  1354. litlengthNCount, litlengthMaxValue,
  1355. LL_base, LL_bits,
  1356. litlengthLog,
  1357. entropy->workspace, sizeof(entropy->workspace),
  1358. /* bmi2 */ 0);
  1359. dictPtr += litlengthHeaderSize;
  1360. }
  1361. RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted, "");
  1362. { int i;
  1363. size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));
  1364. for (i=0; i<3; i++) {
  1365. U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;
  1366. RETURN_ERROR_IF(rep==0 || rep > dictContentSize,
  1367. dictionary_corrupted, "");
  1368. entropy->rep[i] = rep;
  1369. } }
  1370. return (size_t)(dictPtr - (const BYTE*)dict);
  1371. }
  1372. static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1373. {
  1374. if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
  1375. { U32 const magic = MEM_readLE32(dict);
  1376. if (magic != ZSTD_MAGIC_DICTIONARY) {
  1377. return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
  1378. } }
  1379. dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
  1380. /* load entropy tables */
  1381. { size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize);
  1382. RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted, "");
  1383. dict = (const char*)dict + eSize;
  1384. dictSize -= eSize;
  1385. }
  1386. dctx->litEntropy = dctx->fseEntropy = 1;
  1387. /* reference dictionary content */
  1388. return ZSTD_refDictContent(dctx, dict, dictSize);
  1389. }
  1390. size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
  1391. {
  1392. assert(dctx != NULL);
  1393. #if ZSTD_TRACE
  1394. dctx->traceCtx = (ZSTD_trace_decompress_begin != NULL) ? ZSTD_trace_decompress_begin(dctx) : 0;
  1395. #endif
  1396. dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
  1397. dctx->stage = ZSTDds_getFrameHeaderSize;
  1398. dctx->processedCSize = 0;
  1399. dctx->decodedSize = 0;
  1400. dctx->previousDstEnd = NULL;
  1401. dctx->prefixStart = NULL;
  1402. dctx->virtualStart = NULL;
  1403. dctx->dictEnd = NULL;
  1404. dctx->entropy.hufTable[0] = (HUF_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001); /* cover both little and big endian */
  1405. dctx->litEntropy = dctx->fseEntropy = 0;
  1406. dctx->dictID = 0;
  1407. dctx->bType = bt_reserved;
  1408. dctx->isFrameDecompression = 1;
  1409. ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
  1410. ZSTD_memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
  1411. dctx->LLTptr = dctx->entropy.LLTable;
  1412. dctx->MLTptr = dctx->entropy.MLTable;
  1413. dctx->OFTptr = dctx->entropy.OFTable;
  1414. dctx->HUFptr = dctx->entropy.hufTable;
  1415. return 0;
  1416. }
  1417. size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1418. {
  1419. FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
  1420. if (dict && dictSize)
  1421. RETURN_ERROR_IF(
  1422. ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)),
  1423. dictionary_corrupted, "");
  1424. return 0;
  1425. }
  1426. /* ====== ZSTD_DDict ====== */
  1427. size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  1428. {
  1429. DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict");
  1430. assert(dctx != NULL);
  1431. if (ddict) {
  1432. const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);
  1433. size_t const dictSize = ZSTD_DDict_dictSize(ddict);
  1434. const void* const dictEnd = dictStart + dictSize;
  1435. dctx->ddictIsCold = (dctx->dictEnd != dictEnd);
  1436. DEBUGLOG(4, "DDict is %s",
  1437. dctx->ddictIsCold ? "~cold~" : "hot!");
  1438. }
  1439. FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
  1440. if (ddict) { /* NULL ddict is equivalent to no dictionary */
  1441. ZSTD_copyDDictParameters(dctx, ddict);
  1442. }
  1443. return 0;
  1444. }
  1445. /*! ZSTD_getDictID_fromDict() :
  1446. * Provides the dictID stored within dictionary.
  1447. * if @return == 0, the dictionary is not conformant with Zstandard specification.
  1448. * It can still be loaded, but as a content-only dictionary. */
  1449. unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
  1450. {
  1451. if (dictSize < 8) return 0;
  1452. if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
  1453. return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);
  1454. }
  1455. /*! ZSTD_getDictID_fromFrame() :
  1456. * Provides the dictID required to decompress frame stored within `src`.
  1457. * If @return == 0, the dictID could not be decoded.
  1458. * This could for one of the following reasons :
  1459. * - The frame does not require a dictionary (most common case).
  1460. * - The frame was built with dictID intentionally removed.
  1461. * Needed dictionary is a hidden piece of information.
  1462. * Note : this use case also happens when using a non-conformant dictionary.
  1463. * - `srcSize` is too small, and as a result, frame header could not be decoded.
  1464. * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
  1465. * - This is not a Zstandard frame.
  1466. * When identifying the exact failure cause, it's possible to use
  1467. * ZSTD_getFrameHeader(), which will provide a more precise error code. */
  1468. unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)
  1469. {
  1470. ZSTD_FrameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0, 0, 0 };
  1471. size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);
  1472. if (ZSTD_isError(hError)) return 0;
  1473. return zfp.dictID;
  1474. }
  1475. /*! ZSTD_decompress_usingDDict() :
  1476. * Decompression using a pre-digested Dictionary
  1477. * Use dictionary without significant overhead. */
  1478. size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
  1479. void* dst, size_t dstCapacity,
  1480. const void* src, size_t srcSize,
  1481. const ZSTD_DDict* ddict)
  1482. {
  1483. /* pass content and size in case legacy frames are encountered */
  1484. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
  1485. NULL, 0,
  1486. ddict);
  1487. }
  1488. /*=====================================
  1489. * Streaming decompression
  1490. *====================================*/
  1491. ZSTD_DStream* ZSTD_createDStream(void)
  1492. {
  1493. DEBUGLOG(3, "ZSTD_createDStream");
  1494. return ZSTD_createDCtx_internal(ZSTD_defaultCMem);
  1495. }
  1496. ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)
  1497. {
  1498. return ZSTD_initStaticDCtx(workspace, workspaceSize);
  1499. }
  1500. ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
  1501. {
  1502. return ZSTD_createDCtx_internal(customMem);
  1503. }
  1504. size_t ZSTD_freeDStream(ZSTD_DStream* zds)
  1505. {
  1506. return ZSTD_freeDCtx(zds);
  1507. }
  1508. /* *** Initialization *** */
  1509. size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
  1510. size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
  1511. size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
  1512. const void* dict, size_t dictSize,
  1513. ZSTD_dictLoadMethod_e dictLoadMethod,
  1514. ZSTD_dictContentType_e dictContentType)
  1515. {
  1516. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  1517. ZSTD_clearDict(dctx);
  1518. if (dict && dictSize != 0) {
  1519. dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
  1520. RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, "NULL pointer!");
  1521. dctx->ddict = dctx->ddictLocal;
  1522. dctx->dictUses = ZSTD_use_indefinitely;
  1523. }
  1524. return 0;
  1525. }
  1526. size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1527. {
  1528. return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);
  1529. }
  1530. size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1531. {
  1532. return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);
  1533. }
  1534. size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)
  1535. {
  1536. FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType), "");
  1537. dctx->dictUses = ZSTD_use_once;
  1538. return 0;
  1539. }
  1540. size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize)
  1541. {
  1542. return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent);
  1543. }
  1544. /* ZSTD_initDStream_usingDict() :
  1545. * return : expected size, aka ZSTD_startingInputLength().
  1546. * this function cannot fail */
  1547. size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
  1548. {
  1549. DEBUGLOG(4, "ZSTD_initDStream_usingDict");
  1550. FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) , "");
  1551. FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) , "");
  1552. return ZSTD_startingInputLength(zds->format);
  1553. }
  1554. /* note : this variant can't fail */
  1555. size_t ZSTD_initDStream(ZSTD_DStream* zds)
  1556. {
  1557. DEBUGLOG(4, "ZSTD_initDStream");
  1558. FORWARD_IF_ERROR(ZSTD_DCtx_reset(zds, ZSTD_reset_session_only), "");
  1559. FORWARD_IF_ERROR(ZSTD_DCtx_refDDict(zds, NULL), "");
  1560. return ZSTD_startingInputLength(zds->format);
  1561. }
  1562. /* ZSTD_initDStream_usingDDict() :
  1563. * ddict will just be referenced, and must outlive decompression session
  1564. * this function cannot fail */
  1565. size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)
  1566. {
  1567. DEBUGLOG(4, "ZSTD_initDStream_usingDDict");
  1568. FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) , "");
  1569. FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) , "");
  1570. return ZSTD_startingInputLength(dctx->format);
  1571. }
  1572. /* ZSTD_resetDStream() :
  1573. * return : expected size, aka ZSTD_startingInputLength().
  1574. * this function cannot fail */
  1575. size_t ZSTD_resetDStream(ZSTD_DStream* dctx)
  1576. {
  1577. DEBUGLOG(4, "ZSTD_resetDStream");
  1578. FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only), "");
  1579. return ZSTD_startingInputLength(dctx->format);
  1580. }
  1581. size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
  1582. {
  1583. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  1584. ZSTD_clearDict(dctx);
  1585. if (ddict) {
  1586. dctx->ddict = ddict;
  1587. dctx->dictUses = ZSTD_use_indefinitely;
  1588. if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts) {
  1589. if (dctx->ddictSet == NULL) {
  1590. dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem);
  1591. if (!dctx->ddictSet) {
  1592. RETURN_ERROR(memory_allocation, "Failed to allocate memory for hash set!");
  1593. }
  1594. }
  1595. assert(!dctx->staticSize); /* Impossible: ddictSet cannot have been allocated if static dctx */
  1596. FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem), "");
  1597. }
  1598. }
  1599. return 0;
  1600. }
  1601. /* ZSTD_DCtx_setMaxWindowSize() :
  1602. * note : no direct equivalence in ZSTD_DCtx_setParameter,
  1603. * since this version sets windowSize, and the other sets windowLog */
  1604. size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
  1605. {
  1606. ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);
  1607. size_t const min = (size_t)1 << bounds.lowerBound;
  1608. size_t const max = (size_t)1 << bounds.upperBound;
  1609. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  1610. RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound, "");
  1611. RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound, "");
  1612. dctx->maxWindowSize = maxWindowSize;
  1613. return 0;
  1614. }
  1615. size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
  1616. {
  1617. return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (int)format);
  1618. }
  1619. ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)
  1620. {
  1621. ZSTD_bounds bounds = { 0, 0, 0 };
  1622. switch(dParam) {
  1623. case ZSTD_d_windowLogMax:
  1624. bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN;
  1625. bounds.upperBound = ZSTD_WINDOWLOG_MAX;
  1626. return bounds;
  1627. case ZSTD_d_format:
  1628. bounds.lowerBound = (int)ZSTD_f_zstd1;
  1629. bounds.upperBound = (int)ZSTD_f_zstd1_magicless;
  1630. ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);
  1631. return bounds;
  1632. case ZSTD_d_stableOutBuffer:
  1633. bounds.lowerBound = (int)ZSTD_bm_buffered;
  1634. bounds.upperBound = (int)ZSTD_bm_stable;
  1635. return bounds;
  1636. case ZSTD_d_forceIgnoreChecksum:
  1637. bounds.lowerBound = (int)ZSTD_d_validateChecksum;
  1638. bounds.upperBound = (int)ZSTD_d_ignoreChecksum;
  1639. return bounds;
  1640. case ZSTD_d_refMultipleDDicts:
  1641. bounds.lowerBound = (int)ZSTD_rmd_refSingleDDict;
  1642. bounds.upperBound = (int)ZSTD_rmd_refMultipleDDicts;
  1643. return bounds;
  1644. case ZSTD_d_disableHuffmanAssembly:
  1645. bounds.lowerBound = 0;
  1646. bounds.upperBound = 1;
  1647. return bounds;
  1648. case ZSTD_d_maxBlockSize:
  1649. bounds.lowerBound = ZSTD_BLOCKSIZE_MAX_MIN;
  1650. bounds.upperBound = ZSTD_BLOCKSIZE_MAX;
  1651. return bounds;
  1652. default:;
  1653. }
  1654. bounds.error = ERROR(parameter_unsupported);
  1655. return bounds;
  1656. }
  1657. /* ZSTD_dParam_withinBounds:
  1658. * @return 1 if value is within dParam bounds,
  1659. * 0 otherwise */
  1660. static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)
  1661. {
  1662. ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam);
  1663. if (ZSTD_isError(bounds.error)) return 0;
  1664. if (value < bounds.lowerBound) return 0;
  1665. if (value > bounds.upperBound) return 0;
  1666. return 1;
  1667. }
  1668. #define CHECK_DBOUNDS(p,v) { \
  1669. RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \
  1670. }
  1671. size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value)
  1672. {
  1673. switch (param) {
  1674. case ZSTD_d_windowLogMax:
  1675. *value = (int)ZSTD_highbit32((U32)dctx->maxWindowSize);
  1676. return 0;
  1677. case ZSTD_d_format:
  1678. *value = (int)dctx->format;
  1679. return 0;
  1680. case ZSTD_d_stableOutBuffer:
  1681. *value = (int)dctx->outBufferMode;
  1682. return 0;
  1683. case ZSTD_d_forceIgnoreChecksum:
  1684. *value = (int)dctx->forceIgnoreChecksum;
  1685. return 0;
  1686. case ZSTD_d_refMultipleDDicts:
  1687. *value = (int)dctx->refMultipleDDicts;
  1688. return 0;
  1689. case ZSTD_d_disableHuffmanAssembly:
  1690. *value = (int)dctx->disableHufAsm;
  1691. return 0;
  1692. case ZSTD_d_maxBlockSize:
  1693. *value = dctx->maxBlockSizeParam;
  1694. return 0;
  1695. default:;
  1696. }
  1697. RETURN_ERROR(parameter_unsupported, "");
  1698. }
  1699. size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)
  1700. {
  1701. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  1702. switch(dParam) {
  1703. case ZSTD_d_windowLogMax:
  1704. if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT;
  1705. CHECK_DBOUNDS(ZSTD_d_windowLogMax, value);
  1706. dctx->maxWindowSize = ((size_t)1) << value;
  1707. return 0;
  1708. case ZSTD_d_format:
  1709. CHECK_DBOUNDS(ZSTD_d_format, value);
  1710. dctx->format = (ZSTD_format_e)value;
  1711. return 0;
  1712. case ZSTD_d_stableOutBuffer:
  1713. CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value);
  1714. dctx->outBufferMode = (ZSTD_bufferMode_e)value;
  1715. return 0;
  1716. case ZSTD_d_forceIgnoreChecksum:
  1717. CHECK_DBOUNDS(ZSTD_d_forceIgnoreChecksum, value);
  1718. dctx->forceIgnoreChecksum = (ZSTD_forceIgnoreChecksum_e)value;
  1719. return 0;
  1720. case ZSTD_d_refMultipleDDicts:
  1721. CHECK_DBOUNDS(ZSTD_d_refMultipleDDicts, value);
  1722. if (dctx->staticSize != 0) {
  1723. RETURN_ERROR(parameter_unsupported, "Static dctx does not support multiple DDicts!");
  1724. }
  1725. dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value;
  1726. return 0;
  1727. case ZSTD_d_disableHuffmanAssembly:
  1728. CHECK_DBOUNDS(ZSTD_d_disableHuffmanAssembly, value);
  1729. dctx->disableHufAsm = value != 0;
  1730. return 0;
  1731. case ZSTD_d_maxBlockSize:
  1732. if (value != 0) CHECK_DBOUNDS(ZSTD_d_maxBlockSize, value);
  1733. dctx->maxBlockSizeParam = value;
  1734. return 0;
  1735. default:;
  1736. }
  1737. RETURN_ERROR(parameter_unsupported, "");
  1738. }
  1739. size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
  1740. {
  1741. if ( (reset == ZSTD_reset_session_only)
  1742. || (reset == ZSTD_reset_session_and_parameters) ) {
  1743. dctx->streamStage = zdss_init;
  1744. dctx->noForwardProgress = 0;
  1745. dctx->isFrameDecompression = 1;
  1746. }
  1747. if ( (reset == ZSTD_reset_parameters)
  1748. || (reset == ZSTD_reset_session_and_parameters) ) {
  1749. RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
  1750. ZSTD_clearDict(dctx);
  1751. ZSTD_DCtx_resetParameters(dctx);
  1752. }
  1753. return 0;
  1754. }
  1755. size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)
  1756. {
  1757. return ZSTD_sizeof_DCtx(dctx);
  1758. }
  1759. static size_t ZSTD_decodingBufferSize_internal(unsigned long long windowSize, unsigned long long frameContentSize, size_t blockSizeMax)
  1760. {
  1761. size_t const blockSize = MIN((size_t)MIN(windowSize, ZSTD_BLOCKSIZE_MAX), blockSizeMax);
  1762. /* We need blockSize + WILDCOPY_OVERLENGTH worth of buffer so that if a block
  1763. * ends at windowSize + WILDCOPY_OVERLENGTH + 1 bytes, we can start writing
  1764. * the block at the beginning of the output buffer, and maintain a full window.
  1765. *
  1766. * We need another blockSize worth of buffer so that we can store split
  1767. * literals at the end of the block without overwriting the extDict window.
  1768. */
  1769. unsigned long long const neededRBSize = windowSize + (blockSize * 2) + (WILDCOPY_OVERLENGTH * 2);
  1770. unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);
  1771. size_t const minRBSize = (size_t) neededSize;
  1772. RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize,
  1773. frameParameter_windowTooLarge, "");
  1774. return minRBSize;
  1775. }
  1776. size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
  1777. {
  1778. return ZSTD_decodingBufferSize_internal(windowSize, frameContentSize, ZSTD_BLOCKSIZE_MAX);
  1779. }
  1780. size_t ZSTD_estimateDStreamSize(size_t windowSize)
  1781. {
  1782. size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  1783. size_t const inBuffSize = blockSize; /* no block can be larger */
  1784. size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);
  1785. return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;
  1786. }
  1787. size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)
  1788. {
  1789. U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */
  1790. ZSTD_FrameHeader zfh;
  1791. size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);
  1792. if (ZSTD_isError(err)) return err;
  1793. RETURN_ERROR_IF(err>0, srcSize_wrong, "");
  1794. RETURN_ERROR_IF(zfh.windowSize > windowSizeMax,
  1795. frameParameter_windowTooLarge, "");
  1796. return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);
  1797. }
  1798. /* ***** Decompression ***** */
  1799. static int ZSTD_DCtx_isOverflow(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
  1800. {
  1801. return (zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * ZSTD_WORKSPACETOOLARGE_FACTOR;
  1802. }
  1803. static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)
  1804. {
  1805. if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize))
  1806. zds->oversizedDuration++;
  1807. else
  1808. zds->oversizedDuration = 0;
  1809. }
  1810. static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream* zds)
  1811. {
  1812. return zds->oversizedDuration >= ZSTD_WORKSPACETOOLARGE_MAXDURATION;
  1813. }
  1814. /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */
  1815. static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const* output)
  1816. {
  1817. ZSTD_outBuffer const expect = zds->expectedOutBuffer;
  1818. /* No requirement when ZSTD_obm_stable is not enabled. */
  1819. if (zds->outBufferMode != ZSTD_bm_stable)
  1820. return 0;
  1821. /* Any buffer is allowed in zdss_init, this must be the same for every other call until
  1822. * the context is reset.
  1823. */
  1824. if (zds->streamStage == zdss_init)
  1825. return 0;
  1826. /* The buffer must match our expectation exactly. */
  1827. if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size)
  1828. return 0;
  1829. RETURN_ERROR(dstBuffer_wrong, "ZSTD_d_stableOutBuffer enabled but output differs!");
  1830. }
  1831. /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream()
  1832. * and updates the stage and the output buffer state. This call is extracted so it can be
  1833. * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode.
  1834. * NOTE: You must break after calling this function since the streamStage is modified.
  1835. */
  1836. static size_t ZSTD_decompressContinueStream(
  1837. ZSTD_DStream* zds, char** op, char* oend,
  1838. void const* src, size_t srcSize) {
  1839. int const isSkipFrame = ZSTD_isSkipFrame(zds);
  1840. if (zds->outBufferMode == ZSTD_bm_buffered) {
  1841. size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart;
  1842. size_t const decodedSize = ZSTD_decompressContinue(zds,
  1843. zds->outBuff + zds->outStart, dstSize, src, srcSize);
  1844. FORWARD_IF_ERROR(decodedSize, "");
  1845. if (!decodedSize && !isSkipFrame) {
  1846. zds->streamStage = zdss_read;
  1847. } else {
  1848. zds->outEnd = zds->outStart + decodedSize;
  1849. zds->streamStage = zdss_flush;
  1850. }
  1851. } else {
  1852. /* Write directly into the output buffer */
  1853. size_t const dstSize = isSkipFrame ? 0 : (size_t)(oend - *op);
  1854. size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize);
  1855. FORWARD_IF_ERROR(decodedSize, "");
  1856. *op += decodedSize;
  1857. /* Flushing is not needed. */
  1858. zds->streamStage = zdss_read;
  1859. assert(*op <= oend);
  1860. assert(zds->outBufferMode == ZSTD_bm_stable);
  1861. }
  1862. return 0;
  1863. }
  1864. size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  1865. {
  1866. const char* const src = (const char*)input->src;
  1867. const char* const istart = input->pos != 0 ? src + input->pos : src;
  1868. const char* const iend = input->size != 0 ? src + input->size : src;
  1869. const char* ip = istart;
  1870. char* const dst = (char*)output->dst;
  1871. char* const ostart = output->pos != 0 ? dst + output->pos : dst;
  1872. char* const oend = output->size != 0 ? dst + output->size : dst;
  1873. char* op = ostart;
  1874. U32 someMoreWork = 1;
  1875. DEBUGLOG(5, "ZSTD_decompressStream");
  1876. assert(zds != NULL);
  1877. RETURN_ERROR_IF(
  1878. input->pos > input->size,
  1879. srcSize_wrong,
  1880. "forbidden. in: pos: %u vs size: %u",
  1881. (U32)input->pos, (U32)input->size);
  1882. RETURN_ERROR_IF(
  1883. output->pos > output->size,
  1884. dstSize_tooSmall,
  1885. "forbidden. out: pos: %u vs size: %u",
  1886. (U32)output->pos, (U32)output->size);
  1887. DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos));
  1888. FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds, output), "");
  1889. while (someMoreWork) {
  1890. switch(zds->streamStage)
  1891. {
  1892. case zdss_init :
  1893. DEBUGLOG(5, "stage zdss_init => transparent reset ");
  1894. zds->streamStage = zdss_loadHeader;
  1895. zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
  1896. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  1897. zds->legacyVersion = 0;
  1898. #endif
  1899. zds->hostageByte = 0;
  1900. zds->expectedOutBuffer = *output;
  1901. ZSTD_FALLTHROUGH;
  1902. case zdss_loadHeader :
  1903. DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
  1904. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  1905. if (zds->legacyVersion) {
  1906. RETURN_ERROR_IF(zds->staticSize, memory_allocation,
  1907. "legacy support is incompatible with static dctx");
  1908. { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
  1909. if (hint==0) zds->streamStage = zdss_init;
  1910. return hint;
  1911. } }
  1912. #endif
  1913. { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
  1914. if (zds->refMultipleDDicts && zds->ddictSet) {
  1915. ZSTD_DCtx_selectFrameDDict(zds);
  1916. }
  1917. if (ZSTD_isError(hSize)) {
  1918. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  1919. U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
  1920. if (legacyVersion) {
  1921. ZSTD_DDict const* const ddict = ZSTD_getDDict(zds);
  1922. const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL;
  1923. size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0;
  1924. DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion);
  1925. RETURN_ERROR_IF(zds->staticSize, memory_allocation,
  1926. "legacy support is incompatible with static dctx");
  1927. FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext,
  1928. zds->previousLegacyVersion, legacyVersion,
  1929. dict, dictSize), "");
  1930. zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;
  1931. { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input);
  1932. if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */
  1933. return hint;
  1934. } }
  1935. #endif
  1936. return hSize; /* error */
  1937. }
  1938. if (hSize != 0) { /* need more input */
  1939. size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */
  1940. size_t const remainingInput = (size_t)(iend-ip);
  1941. assert(iend >= ip);
  1942. if (toLoad > remainingInput) { /* not enough input to load full header */
  1943. if (remainingInput > 0) {
  1944. ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);
  1945. zds->lhSize += remainingInput;
  1946. }
  1947. input->pos = input->size;
  1948. /* check first few bytes */
  1949. FORWARD_IF_ERROR(
  1950. ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format),
  1951. "First few bytes detected incorrect" );
  1952. /* return hint input size */
  1953. return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
  1954. }
  1955. assert(ip != NULL);
  1956. ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
  1957. break;
  1958. } }
  1959. /* check for single-pass mode opportunity */
  1960. if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
  1961. && zds->fParams.frameType != ZSTD_skippableFrame
  1962. && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
  1963. size_t const cSize = ZSTD_findFrameCompressedSize_advanced(istart, (size_t)(iend-istart), zds->format);
  1964. if (cSize <= (size_t)(iend-istart)) {
  1965. /* shortcut : using single-pass mode */
  1966. size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, (size_t)(oend-op), istart, cSize, ZSTD_getDDict(zds));
  1967. if (ZSTD_isError(decompressedSize)) return decompressedSize;
  1968. DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()");
  1969. assert(istart != NULL);
  1970. ip = istart + cSize;
  1971. op = op ? op + decompressedSize : op; /* can occur if frameContentSize = 0 (empty frame) */
  1972. zds->expected = 0;
  1973. zds->streamStage = zdss_init;
  1974. someMoreWork = 0;
  1975. break;
  1976. } }
  1977. /* Check output buffer is large enough for ZSTD_odm_stable. */
  1978. if (zds->outBufferMode == ZSTD_bm_stable
  1979. && zds->fParams.frameType != ZSTD_skippableFrame
  1980. && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
  1981. && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) {
  1982. RETURN_ERROR(dstSize_tooSmall, "ZSTD_obm_stable passed but ZSTD_outBuffer is too small");
  1983. }
  1984. /* Consume header (see ZSTDds_decodeFrameHeader) */
  1985. DEBUGLOG(4, "Consume header");
  1986. FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), "");
  1987. if (zds->format == ZSTD_f_zstd1
  1988. && (MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  1989. zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);
  1990. zds->stage = ZSTDds_skipFrame;
  1991. } else {
  1992. FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), "");
  1993. zds->expected = ZSTD_blockHeaderSize;
  1994. zds->stage = ZSTDds_decodeBlockHeader;
  1995. }
  1996. /* control buffer memory usage */
  1997. DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)",
  1998. (U32)(zds->fParams.windowSize >>10),
  1999. (U32)(zds->maxWindowSize >> 10) );
  2000. zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
  2001. RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize,
  2002. frameParameter_windowTooLarge, "");
  2003. if (zds->maxBlockSizeParam != 0)
  2004. zds->fParams.blockSizeMax = MIN(zds->fParams.blockSizeMax, (unsigned)zds->maxBlockSizeParam);
  2005. /* Adapt buffer sizes to frame header instructions */
  2006. { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
  2007. size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_bm_buffered
  2008. ? ZSTD_decodingBufferSize_internal(zds->fParams.windowSize, zds->fParams.frameContentSize, zds->fParams.blockSizeMax)
  2009. : 0;
  2010. ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize);
  2011. { int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize);
  2012. int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds);
  2013. if (tooSmall || tooLarge) {
  2014. size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
  2015. DEBUGLOG(4, "inBuff : from %u to %u",
  2016. (U32)zds->inBuffSize, (U32)neededInBuffSize);
  2017. DEBUGLOG(4, "outBuff : from %u to %u",
  2018. (U32)zds->outBuffSize, (U32)neededOutBuffSize);
  2019. if (zds->staticSize) { /* static DCtx */
  2020. DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize);
  2021. assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */
  2022. RETURN_ERROR_IF(
  2023. bufferSize > zds->staticSize - sizeof(ZSTD_DCtx),
  2024. memory_allocation, "");
  2025. } else {
  2026. ZSTD_customFree(zds->inBuff, zds->customMem);
  2027. zds->inBuffSize = 0;
  2028. zds->outBuffSize = 0;
  2029. zds->inBuff = (char*)ZSTD_customMalloc(bufferSize, zds->customMem);
  2030. RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, "");
  2031. }
  2032. zds->inBuffSize = neededInBuffSize;
  2033. zds->outBuff = zds->inBuff + zds->inBuffSize;
  2034. zds->outBuffSize = neededOutBuffSize;
  2035. } } }
  2036. zds->streamStage = zdss_read;
  2037. ZSTD_FALLTHROUGH;
  2038. case zdss_read:
  2039. DEBUGLOG(5, "stage zdss_read");
  2040. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip));
  2041. DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
  2042. if (neededInSize==0) { /* end of frame */
  2043. zds->streamStage = zdss_init;
  2044. someMoreWork = 0;
  2045. break;
  2046. }
  2047. if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
  2048. FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize), "");
  2049. assert(ip != NULL);
  2050. ip += neededInSize;
  2051. /* Function modifies the stage so we must break */
  2052. break;
  2053. } }
  2054. if (ip==iend) { someMoreWork = 0; break; } /* no more input */
  2055. zds->streamStage = zdss_load;
  2056. ZSTD_FALLTHROUGH;
  2057. case zdss_load:
  2058. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
  2059. size_t const toLoad = neededInSize - zds->inPos;
  2060. int const isSkipFrame = ZSTD_isSkipFrame(zds);
  2061. size_t loadedSize;
  2062. /* At this point we shouldn't be decompressing a block that we can stream. */
  2063. assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip)));
  2064. if (isSkipFrame) {
  2065. loadedSize = MIN(toLoad, (size_t)(iend-ip));
  2066. } else {
  2067. RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos,
  2068. corruption_detected,
  2069. "should never happen");
  2070. loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, (size_t)(iend-ip));
  2071. }
  2072. if (loadedSize != 0) {
  2073. /* ip may be NULL */
  2074. ip += loadedSize;
  2075. zds->inPos += loadedSize;
  2076. }
  2077. if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
  2078. /* decode loaded input */
  2079. zds->inPos = 0; /* input is consumed */
  2080. FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize), "");
  2081. /* Function modifies the stage so we must break */
  2082. break;
  2083. }
  2084. case zdss_flush:
  2085. {
  2086. size_t const toFlushSize = zds->outEnd - zds->outStart;
  2087. size_t const flushedSize = ZSTD_limitCopy(op, (size_t)(oend-op), zds->outBuff + zds->outStart, toFlushSize);
  2088. op = op ? op + flushedSize : op;
  2089. zds->outStart += flushedSize;
  2090. if (flushedSize == toFlushSize) { /* flush completed */
  2091. zds->streamStage = zdss_read;
  2092. if ( (zds->outBuffSize < zds->fParams.frameContentSize)
  2093. && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {
  2094. DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
  2095. (int)(zds->outBuffSize - zds->outStart),
  2096. (U32)zds->fParams.blockSizeMax);
  2097. zds->outStart = zds->outEnd = 0;
  2098. }
  2099. break;
  2100. } }
  2101. /* cannot complete flush */
  2102. someMoreWork = 0;
  2103. break;
  2104. default:
  2105. assert(0); /* impossible */
  2106. RETURN_ERROR(GENERIC, "impossible to reach"); /* some compilers require default to do something */
  2107. } }
  2108. /* result */
  2109. input->pos = (size_t)(ip - (const char*)(input->src));
  2110. output->pos = (size_t)(op - (char*)(output->dst));
  2111. /* Update the expected output buffer for ZSTD_obm_stable. */
  2112. zds->expectedOutBuffer = *output;
  2113. if ((ip==istart) && (op==ostart)) { /* no forward progress */
  2114. zds->noForwardProgress ++;
  2115. if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {
  2116. RETURN_ERROR_IF(op==oend, noForwardProgress_destFull, "");
  2117. RETURN_ERROR_IF(ip==iend, noForwardProgress_inputEmpty, "");
  2118. assert(0);
  2119. }
  2120. } else {
  2121. zds->noForwardProgress = 0;
  2122. }
  2123. { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
  2124. if (!nextSrcSizeHint) { /* frame fully decoded */
  2125. if (zds->outEnd == zds->outStart) { /* output fully flushed */
  2126. if (zds->hostageByte) {
  2127. if (input->pos >= input->size) {
  2128. /* can't release hostage (not present) */
  2129. zds->streamStage = zdss_read;
  2130. return 1;
  2131. }
  2132. input->pos++; /* release hostage */
  2133. } /* zds->hostageByte */
  2134. return 0;
  2135. } /* zds->outEnd == zds->outStart */
  2136. if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
  2137. input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */
  2138. zds->hostageByte=1;
  2139. }
  2140. return 1;
  2141. } /* nextSrcSizeHint==0 */
  2142. nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */
  2143. assert(zds->inPos <= nextSrcSizeHint);
  2144. nextSrcSizeHint -= zds->inPos; /* part already loaded*/
  2145. return nextSrcSizeHint;
  2146. }
  2147. }
  2148. size_t ZSTD_decompressStream_simpleArgs (
  2149. ZSTD_DCtx* dctx,
  2150. void* dst, size_t dstCapacity, size_t* dstPos,
  2151. const void* src, size_t srcSize, size_t* srcPos)
  2152. {
  2153. ZSTD_outBuffer output;
  2154. ZSTD_inBuffer input;
  2155. output.dst = dst;
  2156. output.size = dstCapacity;
  2157. output.pos = *dstPos;
  2158. input.src = src;
  2159. input.size = srcSize;
  2160. input.pos = *srcPos;
  2161. { size_t const cErr = ZSTD_decompressStream(dctx, &output, &input);
  2162. *dstPos = output.pos;
  2163. *srcPos = input.pos;
  2164. return cErr;
  2165. }
  2166. }