zstd_decompress.c 85 KB

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