zstd_decompress.c 97 KB

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