zstd_decompress.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  1. /*
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /* ***************************************************************
  11. * Tuning parameters
  12. *****************************************************************/
  13. /*!
  14. * HEAPMODE :
  15. * Select how default decompression function ZSTD_decompress() will allocate memory,
  16. * in memory stack (0), or in memory heap (1, requires malloc())
  17. */
  18. #ifndef ZSTD_HEAPMODE
  19. # define ZSTD_HEAPMODE 1
  20. #endif
  21. /*!
  22. * LEGACY_SUPPORT :
  23. * if set to 1, ZSTD_decompress() can decode older formats (v0.1+)
  24. */
  25. #ifndef ZSTD_LEGACY_SUPPORT
  26. # define ZSTD_LEGACY_SUPPORT 0
  27. #endif
  28. /*!
  29. * MAXWINDOWSIZE_DEFAULT :
  30. * maximum window size accepted by DStream, by default.
  31. * Frames requiring more memory will be rejected.
  32. */
  33. #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
  34. # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_DEFAULTMAX) + 1)
  35. #endif
  36. /*-*******************************************************
  37. * Dependencies
  38. *********************************************************/
  39. #include <string.h> /* memcpy, memmove, memset */
  40. #include "mem.h" /* low level memory routines */
  41. #define FSE_STATIC_LINKING_ONLY
  42. #include "fse.h"
  43. #define HUF_STATIC_LINKING_ONLY
  44. #include "huf.h"
  45. #include "zstd_internal.h"
  46. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  47. # include "zstd_legacy.h"
  48. #endif
  49. /*-*************************************
  50. * Errors
  51. ***************************************/
  52. #define ZSTD_isError ERR_isError /* for inlining */
  53. #define FSE_isError ERR_isError
  54. #define HUF_isError ERR_isError
  55. /*_*******************************************************
  56. * Memory operations
  57. **********************************************************/
  58. static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
  59. /*-*************************************************************
  60. * Context management
  61. ***************************************************************/
  62. typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,
  63. ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,
  64. ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,
  65. ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;
  66. typedef enum { zdss_init=0, zdss_loadHeader,
  67. zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;
  68. typedef struct {
  69. FSE_DTable LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
  70. FSE_DTable OFTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
  71. FSE_DTable MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
  72. HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */
  73. U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  74. U32 rep[ZSTD_REP_NUM];
  75. } ZSTD_entropyDTables_t;
  76. struct ZSTD_DCtx_s
  77. {
  78. const FSE_DTable* LLTptr;
  79. const FSE_DTable* MLTptr;
  80. const FSE_DTable* OFTptr;
  81. const HUF_DTable* HUFptr;
  82. ZSTD_entropyDTables_t entropy;
  83. const void* previousDstEnd; /* detect continuity */
  84. const void* base; /* start of current segment */
  85. const void* vBase; /* virtual start of previous segment if it was just before current one */
  86. const void* dictEnd; /* end of previous segment */
  87. size_t expected;
  88. ZSTD_frameHeader fParams;
  89. U64 decodedSize;
  90. blockType_e bType; /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */
  91. ZSTD_dStage stage;
  92. U32 litEntropy;
  93. U32 fseEntropy;
  94. XXH64_state_t xxhState;
  95. size_t headerSize;
  96. U32 dictID;
  97. ZSTD_format_e format;
  98. const BYTE* litPtr;
  99. ZSTD_customMem customMem;
  100. size_t litSize;
  101. size_t rleSize;
  102. size_t staticSize;
  103. /* streaming */
  104. ZSTD_DDict* ddictLocal;
  105. const ZSTD_DDict* ddict;
  106. ZSTD_dStreamStage streamStage;
  107. char* inBuff;
  108. size_t inBuffSize;
  109. size_t inPos;
  110. size_t maxWindowSize;
  111. char* outBuff;
  112. size_t outBuffSize;
  113. size_t outStart;
  114. size_t outEnd;
  115. size_t lhSize;
  116. void* legacyContext;
  117. U32 previousLegacyVersion;
  118. U32 legacyVersion;
  119. U32 hostageByte;
  120. /* workspace */
  121. BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH];
  122. BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
  123. }; /* typedef'd to ZSTD_DCtx within "zstd.h" */
  124. size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)
  125. {
  126. if (dctx==NULL) return 0; /* support sizeof NULL */
  127. return sizeof(*dctx)
  128. + ZSTD_sizeof_DDict(dctx->ddictLocal)
  129. + dctx->inBuffSize + dctx->outBuffSize;
  130. }
  131. size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
  132. static size_t ZSTD_startingInputLength(ZSTD_format_e format)
  133. {
  134. size_t const startingInputLength = (format==ZSTD_f_zstd1_magicless) ?
  135. ZSTD_frameHeaderSize_prefix - ZSTD_frameIdSize :
  136. ZSTD_frameHeaderSize_prefix;
  137. ZSTD_STATIC_ASSERT(ZSTD_FRAMEHEADERSIZE_PREFIX >= ZSTD_FRAMEIDSIZE);
  138. /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */
  139. assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );
  140. return startingInputLength;
  141. }
  142. static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
  143. {
  144. dctx->format = ZSTD_f_zstd1; /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */
  145. dctx->staticSize = 0;
  146. dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
  147. dctx->ddict = NULL;
  148. dctx->ddictLocal = NULL;
  149. dctx->inBuff = NULL;
  150. dctx->inBuffSize = 0;
  151. dctx->outBuffSize = 0;
  152. dctx->streamStage = zdss_init;
  153. }
  154. ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
  155. {
  156. ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;
  157. if ((size_t)workspace & 7) return NULL; /* 8-aligned */
  158. if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */
  159. ZSTD_initDCtx_internal(dctx);
  160. dctx->staticSize = workspaceSize;
  161. dctx->inBuff = (char*)(dctx+1);
  162. return dctx;
  163. }
  164. ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
  165. {
  166. if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
  167. { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);
  168. if (!dctx) return NULL;
  169. dctx->customMem = customMem;
  170. dctx->legacyContext = NULL;
  171. dctx->previousLegacyVersion = 0;
  172. ZSTD_initDCtx_internal(dctx);
  173. return dctx;
  174. }
  175. }
  176. ZSTD_DCtx* ZSTD_createDCtx(void)
  177. {
  178. return ZSTD_createDCtx_advanced(ZSTD_defaultCMem);
  179. }
  180. size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
  181. {
  182. if (dctx==NULL) return 0; /* support free on NULL */
  183. if (dctx->staticSize) return ERROR(memory_allocation); /* not compatible with static DCtx */
  184. { ZSTD_customMem const cMem = dctx->customMem;
  185. ZSTD_freeDDict(dctx->ddictLocal);
  186. dctx->ddictLocal = NULL;
  187. ZSTD_free(dctx->inBuff, cMem);
  188. dctx->inBuff = NULL;
  189. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  190. if (dctx->legacyContext)
  191. ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
  192. #endif
  193. ZSTD_free(dctx, cMem);
  194. return 0;
  195. }
  196. }
  197. /* no longer useful */
  198. void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
  199. {
  200. size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
  201. memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
  202. }
  203. /*-*************************************************************
  204. * Decompression section
  205. ***************************************************************/
  206. /*! ZSTD_isFrame() :
  207. * Tells if the content of `buffer` starts with a valid Frame Identifier.
  208. * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
  209. * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
  210. * Note 3 : Skippable Frame Identifiers are considered valid. */
  211. unsigned ZSTD_isFrame(const void* buffer, size_t size)
  212. {
  213. if (size < ZSTD_frameIdSize) return 0;
  214. { U32 const magic = MEM_readLE32(buffer);
  215. if (magic == ZSTD_MAGICNUMBER) return 1;
  216. if ((magic & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) return 1;
  217. }
  218. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  219. if (ZSTD_isLegacy(buffer, size)) return 1;
  220. #endif
  221. return 0;
  222. }
  223. /** ZSTD_frameHeaderSize_internal() :
  224. * srcSize must be large enough to reach header size fields.
  225. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
  226. * @return : size of the Frame Header
  227. * or an error code, which can be tested with ZSTD_isError() */
  228. static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)
  229. {
  230. size_t const minInputSize = ZSTD_startingInputLength(format);
  231. if (srcSize < minInputSize) return ERROR(srcSize_wrong);
  232. { BYTE const fhd = ((const BYTE*)src)[minInputSize-1];
  233. U32 const dictID= fhd & 3;
  234. U32 const singleSegment = (fhd >> 5) & 1;
  235. U32 const fcsId = fhd >> 6;
  236. return minInputSize + !singleSegment
  237. + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
  238. + (singleSegment && !fcsId);
  239. }
  240. }
  241. /** ZSTD_frameHeaderSize() :
  242. * srcSize must be >= ZSTD_frameHeaderSize_prefix.
  243. * @return : size of the Frame Header */
  244. size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
  245. {
  246. return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);
  247. }
  248. /** ZSTD_getFrameHeader_internal() :
  249. * decode Frame Header, or require larger `srcSize`.
  250. * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless
  251. * @return : 0, `zfhPtr` is correctly filled,
  252. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  253. * or an error code, which can be tested using ZSTD_isError() */
  254. static size_t ZSTD_getFrameHeader_internal(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)
  255. {
  256. const BYTE* ip = (const BYTE*)src;
  257. size_t const minInputSize = ZSTD_startingInputLength(format);
  258. if (srcSize < minInputSize) return minInputSize;
  259. if ( (format != ZSTD_f_zstd1_magicless)
  260. && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {
  261. if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
  262. /* skippable frame */
  263. if (srcSize < ZSTD_skippableHeaderSize)
  264. return ZSTD_skippableHeaderSize; /* magic number + frame length */
  265. memset(zfhPtr, 0, sizeof(*zfhPtr));
  266. zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_frameIdSize);
  267. zfhPtr->frameType = ZSTD_skippableFrame;
  268. return 0;
  269. }
  270. return ERROR(prefix_unknown);
  271. }
  272. /* ensure there is enough `srcSize` to fully read/decode frame header */
  273. { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);
  274. if (srcSize < fhsize) return fhsize;
  275. zfhPtr->headerSize = (U32)fhsize;
  276. }
  277. { BYTE const fhdByte = ip[minInputSize-1];
  278. size_t pos = minInputSize;
  279. U32 const dictIDSizeCode = fhdByte&3;
  280. U32 const checksumFlag = (fhdByte>>2)&1;
  281. U32 const singleSegment = (fhdByte>>5)&1;
  282. U32 const fcsID = fhdByte>>6;
  283. U64 windowSize = 0;
  284. U32 dictID = 0;
  285. U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;
  286. if ((fhdByte & 0x08) != 0)
  287. return ERROR(frameParameter_unsupported); /* reserved bits, must be zero */
  288. if (!singleSegment) {
  289. BYTE const wlByte = ip[pos++];
  290. U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
  291. if (windowLog > ZSTD_WINDOWLOG_MAX)
  292. return ERROR(frameParameter_windowTooLarge);
  293. windowSize = (1ULL << windowLog);
  294. windowSize += (windowSize >> 3) * (wlByte&7);
  295. }
  296. switch(dictIDSizeCode)
  297. {
  298. default: assert(0); /* impossible */
  299. case 0 : break;
  300. case 1 : dictID = ip[pos]; pos++; break;
  301. case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
  302. case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
  303. }
  304. switch(fcsID)
  305. {
  306. default: assert(0); /* impossible */
  307. case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
  308. case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
  309. case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
  310. case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
  311. }
  312. if (singleSegment) windowSize = frameContentSize;
  313. zfhPtr->frameType = ZSTD_frame;
  314. zfhPtr->frameContentSize = frameContentSize;
  315. zfhPtr->windowSize = windowSize;
  316. zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  317. zfhPtr->dictID = dictID;
  318. zfhPtr->checksumFlag = checksumFlag;
  319. }
  320. return 0;
  321. }
  322. /** ZSTD_getFrameHeader() :
  323. * decode Frame Header, or require larger `srcSize`.
  324. * note : this function does not consume input, it only reads it.
  325. * @return : 0, `zfhPtr` is correctly filled,
  326. * >0, `srcSize` is too small, value is wanted `srcSize` amount,
  327. * or an error code, which can be tested using ZSTD_isError() */
  328. size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize)
  329. {
  330. return ZSTD_getFrameHeader_internal(zfhPtr, src, srcSize, ZSTD_f_zstd1);
  331. }
  332. /** ZSTD_getFrameContentSize() :
  333. * compatible with legacy mode
  334. * @return : decompressed size of the single frame pointed to be `src` if known, otherwise
  335. * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
  336. * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */
  337. unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)
  338. {
  339. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  340. if (ZSTD_isLegacy(src, srcSize)) {
  341. unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize);
  342. return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret;
  343. }
  344. #endif
  345. { ZSTD_frameHeader zfh;
  346. if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)
  347. return ZSTD_CONTENTSIZE_ERROR;
  348. if (zfh.frameType == ZSTD_skippableFrame) {
  349. return 0;
  350. } else {
  351. return zfh.frameContentSize;
  352. } }
  353. }
  354. /** ZSTD_findDecompressedSize() :
  355. * compatible with legacy mode
  356. * `srcSize` must be the exact length of some number of ZSTD compressed and/or
  357. * skippable frames
  358. * @return : decompressed size of the frames contained */
  359. unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)
  360. {
  361. unsigned long long totalDstSize = 0;
  362. while (srcSize >= ZSTD_frameHeaderSize_prefix) {
  363. U32 const magicNumber = MEM_readLE32(src);
  364. if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
  365. size_t skippableSize;
  366. if (srcSize < ZSTD_skippableHeaderSize)
  367. return ERROR(srcSize_wrong);
  368. skippableSize = MEM_readLE32((const BYTE *)src + ZSTD_frameIdSize)
  369. + ZSTD_skippableHeaderSize;
  370. if (srcSize < skippableSize) {
  371. return ZSTD_CONTENTSIZE_ERROR;
  372. }
  373. src = (const BYTE *)src + skippableSize;
  374. srcSize -= skippableSize;
  375. continue;
  376. }
  377. { unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
  378. if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret;
  379. /* check for overflow */
  380. if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR;
  381. totalDstSize += ret;
  382. }
  383. { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);
  384. if (ZSTD_isError(frameSrcSize)) {
  385. return ZSTD_CONTENTSIZE_ERROR;
  386. }
  387. src = (const BYTE *)src + frameSrcSize;
  388. srcSize -= frameSrcSize;
  389. }
  390. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  391. if (srcSize) return ZSTD_CONTENTSIZE_ERROR;
  392. return totalDstSize;
  393. }
  394. /** ZSTD_getDecompressedSize() :
  395. * compatible with legacy mode
  396. * @return : decompressed size if known, 0 otherwise
  397. note : 0 can mean any of the following :
  398. - frame content is empty
  399. - decompressed size field is not present in frame header
  400. - frame header unknown / not supported
  401. - frame header not complete (`srcSize` too small) */
  402. unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
  403. {
  404. unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);
  405. ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);
  406. return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;
  407. }
  408. /** ZSTD_decodeFrameHeader() :
  409. * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
  410. * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
  411. static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
  412. {
  413. size_t const result = ZSTD_getFrameHeader_internal(&(dctx->fParams), src, headerSize, dctx->format);
  414. if (ZSTD_isError(result)) return result; /* invalid header */
  415. if (result>0) return ERROR(srcSize_wrong); /* headerSize too small */
  416. if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID))
  417. return ERROR(dictionary_wrong);
  418. if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
  419. return 0;
  420. }
  421. /*! ZSTD_getcBlockSize() :
  422. * Provides the size of compressed block from block header `src` */
  423. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  424. blockProperties_t* bpPtr)
  425. {
  426. if (srcSize < ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
  427. { U32 const cBlockHeader = MEM_readLE24(src);
  428. U32 const cSize = cBlockHeader >> 3;
  429. bpPtr->lastBlock = cBlockHeader & 1;
  430. bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3);
  431. bpPtr->origSize = cSize; /* only useful for RLE */
  432. if (bpPtr->blockType == bt_rle) return 1;
  433. if (bpPtr->blockType == bt_reserved) return ERROR(corruption_detected);
  434. return cSize;
  435. }
  436. }
  437. static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,
  438. const void* src, size_t srcSize)
  439. {
  440. if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
  441. memcpy(dst, src, srcSize);
  442. return srcSize;
  443. }
  444. static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,
  445. const void* src, size_t srcSize,
  446. size_t regenSize)
  447. {
  448. if (srcSize != 1) return ERROR(srcSize_wrong);
  449. if (regenSize > dstCapacity) return ERROR(dstSize_tooSmall);
  450. memset(dst, *(const BYTE*)src, regenSize);
  451. return regenSize;
  452. }
  453. /*! ZSTD_decodeLiteralsBlock() :
  454. * @return : nb of bytes read from src (< srcSize )
  455. * note : symbol not declared but exposed for fullbench */
  456. size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
  457. const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
  458. {
  459. if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
  460. { const BYTE* const istart = (const BYTE*) src;
  461. symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
  462. switch(litEncType)
  463. {
  464. case set_repeat:
  465. if (dctx->litEntropy==0) return ERROR(dictionary_corrupted);
  466. /* fall-through */
  467. case set_compressed:
  468. if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */
  469. { size_t lhSize, litSize, litCSize;
  470. U32 singleStream=0;
  471. U32 const lhlCode = (istart[0] >> 2) & 3;
  472. U32 const lhc = MEM_readLE32(istart);
  473. switch(lhlCode)
  474. {
  475. case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */
  476. /* 2 - 2 - 10 - 10 */
  477. singleStream = !lhlCode;
  478. lhSize = 3;
  479. litSize = (lhc >> 4) & 0x3FF;
  480. litCSize = (lhc >> 14) & 0x3FF;
  481. break;
  482. case 2:
  483. /* 2 - 2 - 14 - 14 */
  484. lhSize = 4;
  485. litSize = (lhc >> 4) & 0x3FFF;
  486. litCSize = lhc >> 18;
  487. break;
  488. case 3:
  489. /* 2 - 2 - 18 - 18 */
  490. lhSize = 5;
  491. litSize = (lhc >> 4) & 0x3FFFF;
  492. litCSize = (lhc >> 22) + (istart[4] << 10);
  493. break;
  494. }
  495. if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected);
  496. if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
  497. if (HUF_isError((litEncType==set_repeat) ?
  498. ( singleStream ?
  499. HUF_decompress1X_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr) :
  500. HUF_decompress4X_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr) ) :
  501. ( singleStream ?
  502. HUF_decompress1X2_DCtx_wksp(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize,
  503. dctx->entropy.workspace, sizeof(dctx->entropy.workspace)) :
  504. HUF_decompress4X_hufOnly_wksp(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize,
  505. dctx->entropy.workspace, sizeof(dctx->entropy.workspace)))))
  506. return ERROR(corruption_detected);
  507. dctx->litPtr = dctx->litBuffer;
  508. dctx->litSize = litSize;
  509. dctx->litEntropy = 1;
  510. if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable;
  511. memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
  512. return litCSize + lhSize;
  513. }
  514. case set_basic:
  515. { size_t litSize, lhSize;
  516. U32 const lhlCode = ((istart[0]) >> 2) & 3;
  517. switch(lhlCode)
  518. {
  519. case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
  520. lhSize = 1;
  521. litSize = istart[0] >> 3;
  522. break;
  523. case 1:
  524. lhSize = 2;
  525. litSize = MEM_readLE16(istart) >> 4;
  526. break;
  527. case 3:
  528. lhSize = 3;
  529. litSize = MEM_readLE24(istart) >> 4;
  530. break;
  531. }
  532. if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
  533. if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
  534. memcpy(dctx->litBuffer, istart+lhSize, litSize);
  535. dctx->litPtr = dctx->litBuffer;
  536. dctx->litSize = litSize;
  537. memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
  538. return lhSize+litSize;
  539. }
  540. /* direct reference into compressed stream */
  541. dctx->litPtr = istart+lhSize;
  542. dctx->litSize = litSize;
  543. return lhSize+litSize;
  544. }
  545. case set_rle:
  546. { U32 const lhlCode = ((istart[0]) >> 2) & 3;
  547. size_t litSize, lhSize;
  548. switch(lhlCode)
  549. {
  550. case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
  551. lhSize = 1;
  552. litSize = istart[0] >> 3;
  553. break;
  554. case 1:
  555. lhSize = 2;
  556. litSize = MEM_readLE16(istart) >> 4;
  557. break;
  558. case 3:
  559. lhSize = 3;
  560. litSize = MEM_readLE24(istart) >> 4;
  561. if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
  562. break;
  563. }
  564. if (litSize > ZSTD_BLOCKSIZE_MAX) return ERROR(corruption_detected);
  565. memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
  566. dctx->litPtr = dctx->litBuffer;
  567. dctx->litSize = litSize;
  568. return lhSize+1;
  569. }
  570. default:
  571. return ERROR(corruption_detected); /* impossible */
  572. }
  573. }
  574. }
  575. typedef union {
  576. FSE_decode_t realData;
  577. U32 alignedBy4;
  578. } FSE_decode_t4;
  579. /* Default FSE distribution table for Literal Lengths */
  580. static const FSE_decode_t4 LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = {
  581. { { LL_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */
  582. /* base, symbol, bits */
  583. { { 0, 0, 4 } }, { { 16, 0, 4 } }, { { 32, 1, 5 } }, { { 0, 3, 5 } },
  584. { { 0, 4, 5 } }, { { 0, 6, 5 } }, { { 0, 7, 5 } }, { { 0, 9, 5 } },
  585. { { 0, 10, 5 } }, { { 0, 12, 5 } }, { { 0, 14, 6 } }, { { 0, 16, 5 } },
  586. { { 0, 18, 5 } }, { { 0, 19, 5 } }, { { 0, 21, 5 } }, { { 0, 22, 5 } },
  587. { { 0, 24, 5 } }, { { 32, 25, 5 } }, { { 0, 26, 5 } }, { { 0, 27, 6 } },
  588. { { 0, 29, 6 } }, { { 0, 31, 6 } }, { { 32, 0, 4 } }, { { 0, 1, 4 } },
  589. { { 0, 2, 5 } }, { { 32, 4, 5 } }, { { 0, 5, 5 } }, { { 32, 7, 5 } },
  590. { { 0, 8, 5 } }, { { 32, 10, 5 } }, { { 0, 11, 5 } }, { { 0, 13, 6 } },
  591. { { 32, 16, 5 } }, { { 0, 17, 5 } }, { { 32, 19, 5 } }, { { 0, 20, 5 } },
  592. { { 32, 22, 5 } }, { { 0, 23, 5 } }, { { 0, 25, 4 } }, { { 16, 25, 4 } },
  593. { { 32, 26, 5 } }, { { 0, 28, 6 } }, { { 0, 30, 6 } }, { { 48, 0, 4 } },
  594. { { 16, 1, 4 } }, { { 32, 2, 5 } }, { { 32, 3, 5 } }, { { 32, 5, 5 } },
  595. { { 32, 6, 5 } }, { { 32, 8, 5 } }, { { 32, 9, 5 } }, { { 32, 11, 5 } },
  596. { { 32, 12, 5 } }, { { 0, 15, 6 } }, { { 32, 17, 5 } }, { { 32, 18, 5 } },
  597. { { 32, 20, 5 } }, { { 32, 21, 5 } }, { { 32, 23, 5 } }, { { 32, 24, 5 } },
  598. { { 0, 35, 6 } }, { { 0, 34, 6 } }, { { 0, 33, 6 } }, { { 0, 32, 6 } },
  599. }; /* LL_defaultDTable */
  600. /* Default FSE distribution table for Match Lengths */
  601. static const FSE_decode_t4 ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = {
  602. { { ML_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */
  603. /* base, symbol, bits */
  604. { { 0, 0, 6 } }, { { 0, 1, 4 } }, { { 32, 2, 5 } }, { { 0, 3, 5 } },
  605. { { 0, 5, 5 } }, { { 0, 6, 5 } }, { { 0, 8, 5 } }, { { 0, 10, 6 } },
  606. { { 0, 13, 6 } }, { { 0, 16, 6 } }, { { 0, 19, 6 } }, { { 0, 22, 6 } },
  607. { { 0, 25, 6 } }, { { 0, 28, 6 } }, { { 0, 31, 6 } }, { { 0, 33, 6 } },
  608. { { 0, 35, 6 } }, { { 0, 37, 6 } }, { { 0, 39, 6 } }, { { 0, 41, 6 } },
  609. { { 0, 43, 6 } }, { { 0, 45, 6 } }, { { 16, 1, 4 } }, { { 0, 2, 4 } },
  610. { { 32, 3, 5 } }, { { 0, 4, 5 } }, { { 32, 6, 5 } }, { { 0, 7, 5 } },
  611. { { 0, 9, 6 } }, { { 0, 12, 6 } }, { { 0, 15, 6 } }, { { 0, 18, 6 } },
  612. { { 0, 21, 6 } }, { { 0, 24, 6 } }, { { 0, 27, 6 } }, { { 0, 30, 6 } },
  613. { { 0, 32, 6 } }, { { 0, 34, 6 } }, { { 0, 36, 6 } }, { { 0, 38, 6 } },
  614. { { 0, 40, 6 } }, { { 0, 42, 6 } }, { { 0, 44, 6 } }, { { 32, 1, 4 } },
  615. { { 48, 1, 4 } }, { { 16, 2, 4 } }, { { 32, 4, 5 } }, { { 32, 5, 5 } },
  616. { { 32, 7, 5 } }, { { 32, 8, 5 } }, { { 0, 11, 6 } }, { { 0, 14, 6 } },
  617. { { 0, 17, 6 } }, { { 0, 20, 6 } }, { { 0, 23, 6 } }, { { 0, 26, 6 } },
  618. { { 0, 29, 6 } }, { { 0, 52, 6 } }, { { 0, 51, 6 } }, { { 0, 50, 6 } },
  619. { { 0, 49, 6 } }, { { 0, 48, 6 } }, { { 0, 47, 6 } }, { { 0, 46, 6 } },
  620. }; /* ML_defaultDTable */
  621. /* Default FSE distribution table for Offset Codes */
  622. static const FSE_decode_t4 OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = {
  623. { { OF_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */
  624. /* base, symbol, bits */
  625. { { 0, 0, 5 } }, { { 0, 6, 4 } },
  626. { { 0, 9, 5 } }, { { 0, 15, 5 } },
  627. { { 0, 21, 5 } }, { { 0, 3, 5 } },
  628. { { 0, 7, 4 } }, { { 0, 12, 5 } },
  629. { { 0, 18, 5 } }, { { 0, 23, 5 } },
  630. { { 0, 5, 5 } }, { { 0, 8, 4 } },
  631. { { 0, 14, 5 } }, { { 0, 20, 5 } },
  632. { { 0, 2, 5 } }, { { 16, 7, 4 } },
  633. { { 0, 11, 5 } }, { { 0, 17, 5 } },
  634. { { 0, 22, 5 } }, { { 0, 4, 5 } },
  635. { { 16, 8, 4 } }, { { 0, 13, 5 } },
  636. { { 0, 19, 5 } }, { { 0, 1, 5 } },
  637. { { 16, 6, 4 } }, { { 0, 10, 5 } },
  638. { { 0, 16, 5 } }, { { 0, 28, 5 } },
  639. { { 0, 27, 5 } }, { { 0, 26, 5 } },
  640. { { 0, 25, 5 } }, { { 0, 24, 5 } },
  641. }; /* OF_defaultDTable */
  642. /*! ZSTD_buildSeqTable() :
  643. * @return : nb bytes read from src,
  644. * or an error code if it fails, testable with ZSTD_isError()
  645. */
  646. static size_t ZSTD_buildSeqTable(FSE_DTable* DTableSpace, const FSE_DTable** DTablePtr,
  647. symbolEncodingType_e type, U32 max, U32 maxLog,
  648. const void* src, size_t srcSize,
  649. const FSE_decode_t4* defaultTable, U32 flagRepeatTable)
  650. {
  651. const void* const tmpPtr = defaultTable; /* bypass strict aliasing */
  652. switch(type)
  653. {
  654. case set_rle :
  655. if (!srcSize) return ERROR(srcSize_wrong);
  656. if ( (*(const BYTE*)src) > max) return ERROR(corruption_detected);
  657. FSE_buildDTable_rle(DTableSpace, *(const BYTE*)src);
  658. *DTablePtr = DTableSpace;
  659. return 1;
  660. case set_basic :
  661. *DTablePtr = (const FSE_DTable*)tmpPtr;
  662. return 0;
  663. case set_repeat:
  664. if (!flagRepeatTable) return ERROR(corruption_detected);
  665. return 0;
  666. default : /* impossible */
  667. case set_compressed :
  668. { U32 tableLog;
  669. S16 norm[MaxSeq+1];
  670. size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
  671. if (FSE_isError(headerSize)) return ERROR(corruption_detected);
  672. if (tableLog > maxLog) return ERROR(corruption_detected);
  673. FSE_buildDTable(DTableSpace, norm, max, tableLog);
  674. *DTablePtr = DTableSpace;
  675. return headerSize;
  676. } }
  677. }
  678. size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
  679. const void* src, size_t srcSize)
  680. {
  681. const BYTE* const istart = (const BYTE* const)src;
  682. const BYTE* const iend = istart + srcSize;
  683. const BYTE* ip = istart;
  684. DEBUGLOG(5, "ZSTD_decodeSeqHeaders");
  685. /* check */
  686. if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong);
  687. /* SeqHead */
  688. { int nbSeq = *ip++;
  689. if (!nbSeq) { *nbSeqPtr=0; return 1; }
  690. if (nbSeq > 0x7F) {
  691. if (nbSeq == 0xFF) {
  692. if (ip+2 > iend) return ERROR(srcSize_wrong);
  693. nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
  694. } else {
  695. if (ip >= iend) return ERROR(srcSize_wrong);
  696. nbSeq = ((nbSeq-0x80)<<8) + *ip++;
  697. }
  698. }
  699. *nbSeqPtr = nbSeq;
  700. }
  701. /* FSE table descriptors */
  702. if (ip+4 > iend) return ERROR(srcSize_wrong); /* minimum possible size */
  703. { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);
  704. symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);
  705. symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3);
  706. ip++;
  707. /* Build DTables */
  708. { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr,
  709. LLtype, MaxLL, LLFSELog,
  710. ip, iend-ip, LL_defaultDTable, dctx->fseEntropy);
  711. if (ZSTD_isError(llhSize)) return ERROR(corruption_detected);
  712. ip += llhSize;
  713. }
  714. { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr,
  715. OFtype, MaxOff, OffFSELog,
  716. ip, iend-ip, OF_defaultDTable, dctx->fseEntropy);
  717. if (ZSTD_isError(ofhSize)) return ERROR(corruption_detected);
  718. ip += ofhSize;
  719. }
  720. { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr,
  721. MLtype, MaxML, MLFSELog,
  722. ip, iend-ip, ML_defaultDTable, dctx->fseEntropy);
  723. if (ZSTD_isError(mlhSize)) return ERROR(corruption_detected);
  724. ip += mlhSize;
  725. }
  726. }
  727. return ip-istart;
  728. }
  729. typedef struct {
  730. size_t litLength;
  731. size_t matchLength;
  732. size_t offset;
  733. const BYTE* match;
  734. } seq_t;
  735. typedef struct {
  736. BIT_DStream_t DStream;
  737. FSE_DState_t stateLL;
  738. FSE_DState_t stateOffb;
  739. FSE_DState_t stateML;
  740. size_t prevOffset[ZSTD_REP_NUM];
  741. const BYTE* base;
  742. size_t pos;
  743. uPtrDiff gotoDict;
  744. } seqState_t;
  745. FORCE_NOINLINE
  746. size_t ZSTD_execSequenceLast7(BYTE* op,
  747. BYTE* const oend, seq_t sequence,
  748. const BYTE** litPtr, const BYTE* const litLimit,
  749. const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
  750. {
  751. BYTE* const oLitEnd = op + sequence.litLength;
  752. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  753. BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
  754. BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
  755. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  756. const BYTE* match = oLitEnd - sequence.offset;
  757. /* check */
  758. if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
  759. if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */
  760. if (oLitEnd <= oend_w) return ERROR(GENERIC); /* Precondition */
  761. /* copy literals */
  762. if (op < oend_w) {
  763. ZSTD_wildcopy(op, *litPtr, oend_w - op);
  764. *litPtr += oend_w - op;
  765. op = oend_w;
  766. }
  767. while (op < oLitEnd) *op++ = *(*litPtr)++;
  768. /* copy Match */
  769. if (sequence.offset > (size_t)(oLitEnd - base)) {
  770. /* offset beyond prefix */
  771. if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected);
  772. match = dictEnd - (base-match);
  773. if (match + sequence.matchLength <= dictEnd) {
  774. memmove(oLitEnd, match, sequence.matchLength);
  775. return sequenceLength;
  776. }
  777. /* span extDict & currentPrefixSegment */
  778. { size_t const length1 = dictEnd - match;
  779. memmove(oLitEnd, match, length1);
  780. op = oLitEnd + length1;
  781. sequence.matchLength -= length1;
  782. match = base;
  783. } }
  784. while (op < oMatchEnd) *op++ = *match++;
  785. return sequenceLength;
  786. }
  787. typedef enum { ZSTD_lo_isRegularOffset, ZSTD_lo_isLongOffset=1 } ZSTD_longOffset_e;
  788. /* We need to add at most (ZSTD_WINDOWLOG_MAX_32 - 1) bits to read the maximum
  789. * offset bits. But we can only read at most (STREAM_ACCUMULATOR_MIN_32 - 1)
  790. * bits before reloading. This value is the maximum number of bytes we read
  791. * after reloading when we are decoding long offets.
  792. */
  793. #define LONG_OFFSETS_MAX_EXTRA_BITS_32 \
  794. (ZSTD_WINDOWLOG_MAX_32 > STREAM_ACCUMULATOR_MIN_32 \
  795. ? ZSTD_WINDOWLOG_MAX_32 - STREAM_ACCUMULATOR_MIN_32 \
  796. : 0)
  797. static seq_t ZSTD_decodeSequence(seqState_t* seqState, const ZSTD_longOffset_e longOffsets)
  798. {
  799. seq_t seq;
  800. U32 const llCode = FSE_peekSymbol(&seqState->stateLL);
  801. U32 const mlCode = FSE_peekSymbol(&seqState->stateML);
  802. U32 const ofCode = FSE_peekSymbol(&seqState->stateOffb); /* <= MaxOff, by table construction */
  803. U32 const llBits = LL_bits[llCode];
  804. U32 const mlBits = ML_bits[mlCode];
  805. U32 const ofBits = ofCode;
  806. U32 const totalBits = llBits+mlBits+ofBits;
  807. static const U32 LL_base[MaxLL+1] = {
  808. 0, 1, 2, 3, 4, 5, 6, 7,
  809. 8, 9, 10, 11, 12, 13, 14, 15,
  810. 16, 18, 20, 22, 24, 28, 32, 40,
  811. 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
  812. 0x2000, 0x4000, 0x8000, 0x10000 };
  813. static const U32 ML_base[MaxML+1] = {
  814. 3, 4, 5, 6, 7, 8, 9, 10,
  815. 11, 12, 13, 14, 15, 16, 17, 18,
  816. 19, 20, 21, 22, 23, 24, 25, 26,
  817. 27, 28, 29, 30, 31, 32, 33, 34,
  818. 35, 37, 39, 41, 43, 47, 51, 59,
  819. 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
  820. 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };
  821. static const U32 OF_base[MaxOff+1] = {
  822. 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D,
  823. 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD,
  824. 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,
  825. 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD, 0x1FFFFFFD, 0x3FFFFFFD, 0x7FFFFFFD };
  826. /* sequence */
  827. { size_t offset;
  828. if (!ofCode)
  829. offset = 0;
  830. else {
  831. ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1);
  832. ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5);
  833. assert(ofBits <= MaxOff);
  834. if (MEM_32bits() && longOffsets) {
  835. U32 const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN_32-1);
  836. offset = OF_base[ofCode] + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);
  837. if (MEM_32bits() || extraBits) BIT_reloadDStream(&seqState->DStream);
  838. if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits);
  839. } else {
  840. offset = OF_base[ofCode] + BIT_readBitsFast(&seqState->DStream, ofBits); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */
  841. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);
  842. }
  843. }
  844. if (ofCode <= 1) {
  845. offset += (llCode==0);
  846. if (offset) {
  847. size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
  848. temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
  849. if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
  850. seqState->prevOffset[1] = seqState->prevOffset[0];
  851. seqState->prevOffset[0] = offset = temp;
  852. } else {
  853. offset = seqState->prevOffset[0];
  854. }
  855. } else {
  856. seqState->prevOffset[2] = seqState->prevOffset[1];
  857. seqState->prevOffset[1] = seqState->prevOffset[0];
  858. seqState->prevOffset[0] = offset;
  859. }
  860. seq.offset = offset;
  861. }
  862. seq.matchLength = ML_base[mlCode]
  863. + ((mlCode>31) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <= 16 bits */
  864. if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32))
  865. BIT_reloadDStream(&seqState->DStream);
  866. if (MEM_64bits() && (totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog)))
  867. BIT_reloadDStream(&seqState->DStream);
  868. /* Verify that there is enough bits to read the rest of the data in 64-bit mode. */
  869. ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64);
  870. seq.litLength = LL_base[llCode]
  871. + ((llCode>15) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <= 16 bits */
  872. if (MEM_32bits())
  873. BIT_reloadDStream(&seqState->DStream);
  874. DEBUGLOG(6, "seq: litL=%u, matchL=%u, offset=%u",
  875. (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);
  876. /* ANS state update */
  877. FSE_updateState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */
  878. FSE_updateState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */
  879. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
  880. FSE_updateState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */
  881. return seq;
  882. }
  883. HINT_INLINE
  884. size_t ZSTD_execSequence(BYTE* op,
  885. BYTE* const oend, seq_t sequence,
  886. const BYTE** litPtr, const BYTE* const litLimit,
  887. const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
  888. {
  889. BYTE* const oLitEnd = op + sequence.litLength;
  890. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  891. BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
  892. BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
  893. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  894. const BYTE* match = oLitEnd - sequence.offset;
  895. /* check */
  896. if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
  897. if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */
  898. if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, base, vBase, dictEnd);
  899. /* copy Literals */
  900. ZSTD_copy8(op, *litPtr);
  901. if (sequence.litLength > 8)
  902. ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
  903. op = oLitEnd;
  904. *litPtr = iLitEnd; /* update for next sequence */
  905. /* copy Match */
  906. if (sequence.offset > (size_t)(oLitEnd - base)) {
  907. /* offset beyond prefix -> go into extDict */
  908. if (sequence.offset > (size_t)(oLitEnd - vBase))
  909. return ERROR(corruption_detected);
  910. match = dictEnd + (match - base);
  911. if (match + sequence.matchLength <= dictEnd) {
  912. memmove(oLitEnd, match, sequence.matchLength);
  913. return sequenceLength;
  914. }
  915. /* span extDict & currentPrefixSegment */
  916. { size_t const length1 = dictEnd - match;
  917. memmove(oLitEnd, match, length1);
  918. op = oLitEnd + length1;
  919. sequence.matchLength -= length1;
  920. match = base;
  921. if (op > oend_w || sequence.matchLength < MINMATCH) {
  922. U32 i;
  923. for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i];
  924. return sequenceLength;
  925. }
  926. } }
  927. /* Requirement: op <= oend_w && sequence.matchLength >= MINMATCH */
  928. /* match within prefix */
  929. if (sequence.offset < 8) {
  930. /* close range match, overlap */
  931. static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
  932. static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
  933. int const sub2 = dec64table[sequence.offset];
  934. op[0] = match[0];
  935. op[1] = match[1];
  936. op[2] = match[2];
  937. op[3] = match[3];
  938. match += dec32table[sequence.offset];
  939. ZSTD_copy4(op+4, match);
  940. match -= sub2;
  941. } else {
  942. ZSTD_copy8(op, match);
  943. }
  944. op += 8; match += 8;
  945. if (oMatchEnd > oend-(16-MINMATCH)) {
  946. if (op < oend_w) {
  947. ZSTD_wildcopy(op, match, oend_w - op);
  948. match += oend_w - op;
  949. op = oend_w;
  950. }
  951. while (op < oMatchEnd) *op++ = *match++;
  952. } else {
  953. ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
  954. }
  955. return sequenceLength;
  956. }
  957. static size_t ZSTD_decompressSequences(
  958. ZSTD_DCtx* dctx,
  959. void* dst, size_t maxDstSize,
  960. const void* seqStart, size_t seqSize,
  961. const ZSTD_longOffset_e isLongOffset)
  962. {
  963. const BYTE* ip = (const BYTE*)seqStart;
  964. const BYTE* const iend = ip + seqSize;
  965. BYTE* const ostart = (BYTE* const)dst;
  966. BYTE* const oend = ostart + maxDstSize;
  967. BYTE* op = ostart;
  968. const BYTE* litPtr = dctx->litPtr;
  969. const BYTE* const litEnd = litPtr + dctx->litSize;
  970. const BYTE* const base = (const BYTE*) (dctx->base);
  971. const BYTE* const vBase = (const BYTE*) (dctx->vBase);
  972. const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
  973. int nbSeq;
  974. DEBUGLOG(5, "ZSTD_decompressSequences");
  975. /* Build Decoding Tables */
  976. { size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, seqSize);
  977. DEBUGLOG(5, "ZSTD_decodeSeqHeaders: size=%u, nbSeq=%i",
  978. (U32)seqHSize, nbSeq);
  979. if (ZSTD_isError(seqHSize)) return seqHSize;
  980. ip += seqHSize;
  981. }
  982. /* Regen sequences */
  983. if (nbSeq) {
  984. seqState_t seqState;
  985. dctx->fseEntropy = 1;
  986. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
  987. CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend-ip), corruption_detected);
  988. FSE_initDState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
  989. FSE_initDState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
  990. FSE_initDState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
  991. for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) {
  992. nbSeq--;
  993. { seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset);
  994. size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
  995. DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize);
  996. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  997. op += oneSeqSize;
  998. } }
  999. /* check if reached exact end */
  1000. DEBUGLOG(5, "after decode loop, remaining nbSeq : %i", nbSeq);
  1001. if (nbSeq) return ERROR(corruption_detected);
  1002. /* save reps for next block */
  1003. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
  1004. }
  1005. /* last literal segment */
  1006. { size_t const lastLLSize = litEnd - litPtr;
  1007. if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall);
  1008. memcpy(op, litPtr, lastLLSize);
  1009. op += lastLLSize;
  1010. }
  1011. return op-ostart;
  1012. }
  1013. HINT_INLINE
  1014. seq_t ZSTD_decodeSequenceLong(seqState_t* seqState, ZSTD_longOffset_e const longOffsets)
  1015. {
  1016. seq_t seq;
  1017. U32 const llCode = FSE_peekSymbol(&seqState->stateLL);
  1018. U32 const mlCode = FSE_peekSymbol(&seqState->stateML);
  1019. U32 const ofCode = FSE_peekSymbol(&seqState->stateOffb); /* <= MaxOff, by table construction */
  1020. U32 const llBits = LL_bits[llCode];
  1021. U32 const mlBits = ML_bits[mlCode];
  1022. U32 const ofBits = ofCode;
  1023. U32 const totalBits = llBits+mlBits+ofBits;
  1024. static const U32 LL_base[MaxLL+1] = {
  1025. 0, 1, 2, 3, 4, 5, 6, 7,
  1026. 8, 9, 10, 11, 12, 13, 14, 15,
  1027. 16, 18, 20, 22, 24, 28, 32, 40,
  1028. 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
  1029. 0x2000, 0x4000, 0x8000, 0x10000 };
  1030. static const U32 ML_base[MaxML+1] = {
  1031. 3, 4, 5, 6, 7, 8, 9, 10,
  1032. 11, 12, 13, 14, 15, 16, 17, 18,
  1033. 19, 20, 21, 22, 23, 24, 25, 26,
  1034. 27, 28, 29, 30, 31, 32, 33, 34,
  1035. 35, 37, 39, 41, 43, 47, 51, 59,
  1036. 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
  1037. 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };
  1038. static const U32 OF_base[MaxOff+1] = {
  1039. 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D,
  1040. 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD,
  1041. 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,
  1042. 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD, 0x1FFFFFFD, 0x3FFFFFFD, 0x7FFFFFFD };
  1043. /* sequence */
  1044. { size_t offset;
  1045. if (!ofCode)
  1046. offset = 0;
  1047. else {
  1048. ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1);
  1049. ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5);
  1050. assert(ofBits <= MaxOff);
  1051. if (MEM_32bits() && longOffsets) {
  1052. U32 const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN_32-1);
  1053. offset = OF_base[ofCode] + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);
  1054. if (MEM_32bits() || extraBits) BIT_reloadDStream(&seqState->DStream);
  1055. if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits);
  1056. } else {
  1057. offset = OF_base[ofCode] + BIT_readBitsFast(&seqState->DStream, ofBits); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */
  1058. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);
  1059. }
  1060. }
  1061. if (ofCode <= 1) {
  1062. offset += (llCode==0);
  1063. if (offset) {
  1064. size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
  1065. temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
  1066. if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
  1067. seqState->prevOffset[1] = seqState->prevOffset[0];
  1068. seqState->prevOffset[0] = offset = temp;
  1069. } else {
  1070. offset = seqState->prevOffset[0];
  1071. }
  1072. } else {
  1073. seqState->prevOffset[2] = seqState->prevOffset[1];
  1074. seqState->prevOffset[1] = seqState->prevOffset[0];
  1075. seqState->prevOffset[0] = offset;
  1076. }
  1077. seq.offset = offset;
  1078. }
  1079. seq.matchLength = ML_base[mlCode] + ((mlCode>31) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <= 16 bits */
  1080. if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32))
  1081. BIT_reloadDStream(&seqState->DStream);
  1082. if (MEM_64bits() && (totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog)))
  1083. BIT_reloadDStream(&seqState->DStream);
  1084. /* Verify that there is enough bits to read the rest of the data in 64-bit mode. */
  1085. ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64);
  1086. seq.litLength = LL_base[llCode] + ((llCode>15) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <= 16 bits */
  1087. if (MEM_32bits())
  1088. BIT_reloadDStream(&seqState->DStream);
  1089. { size_t const pos = seqState->pos + seq.litLength;
  1090. seq.match = seqState->base + pos - seq.offset; /* single memory segment */
  1091. if (seq.offset > pos) seq.match += seqState->gotoDict; /* separate memory segment */
  1092. seqState->pos = pos + seq.matchLength;
  1093. }
  1094. /* ANS state update */
  1095. FSE_updateState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */
  1096. FSE_updateState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */
  1097. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
  1098. FSE_updateState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */
  1099. return seq;
  1100. }
  1101. HINT_INLINE
  1102. size_t ZSTD_execSequenceLong(BYTE* op,
  1103. BYTE* const oend, seq_t sequence,
  1104. const BYTE** litPtr, const BYTE* const litLimit,
  1105. const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
  1106. {
  1107. BYTE* const oLitEnd = op + sequence.litLength;
  1108. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  1109. BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
  1110. BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
  1111. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  1112. const BYTE* match = sequence.match;
  1113. /* check */
  1114. if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
  1115. if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */
  1116. if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit, base, vBase, dictEnd);
  1117. /* copy Literals */
  1118. ZSTD_copy8(op, *litPtr);
  1119. if (sequence.litLength > 8)
  1120. ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
  1121. op = oLitEnd;
  1122. *litPtr = iLitEnd; /* update for next sequence */
  1123. /* copy Match */
  1124. if (sequence.offset > (size_t)(oLitEnd - base)) {
  1125. /* offset beyond prefix */
  1126. if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected);
  1127. if (match + sequence.matchLength <= dictEnd) {
  1128. memmove(oLitEnd, match, sequence.matchLength);
  1129. return sequenceLength;
  1130. }
  1131. /* span extDict & currentPrefixSegment */
  1132. { size_t const length1 = dictEnd - match;
  1133. memmove(oLitEnd, match, length1);
  1134. op = oLitEnd + length1;
  1135. sequence.matchLength -= length1;
  1136. match = base;
  1137. if (op > oend_w || sequence.matchLength < MINMATCH) {
  1138. U32 i;
  1139. for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i];
  1140. return sequenceLength;
  1141. }
  1142. } }
  1143. assert(op <= oend_w);
  1144. assert(sequence.matchLength >= MINMATCH);
  1145. /* match within prefix */
  1146. if (sequence.offset < 8) {
  1147. /* close range match, overlap */
  1148. static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
  1149. static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
  1150. int const sub2 = dec64table[sequence.offset];
  1151. op[0] = match[0];
  1152. op[1] = match[1];
  1153. op[2] = match[2];
  1154. op[3] = match[3];
  1155. match += dec32table[sequence.offset];
  1156. ZSTD_copy4(op+4, match);
  1157. match -= sub2;
  1158. } else {
  1159. ZSTD_copy8(op, match);
  1160. }
  1161. op += 8; match += 8;
  1162. if (oMatchEnd > oend-(16-MINMATCH)) {
  1163. if (op < oend_w) {
  1164. ZSTD_wildcopy(op, match, oend_w - op);
  1165. match += oend_w - op;
  1166. op = oend_w;
  1167. }
  1168. while (op < oMatchEnd) *op++ = *match++;
  1169. } else {
  1170. ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
  1171. }
  1172. return sequenceLength;
  1173. }
  1174. static size_t ZSTD_decompressSequencesLong(
  1175. ZSTD_DCtx* dctx,
  1176. void* dst, size_t maxDstSize,
  1177. const void* seqStart, size_t seqSize,
  1178. const ZSTD_longOffset_e isLongOffset)
  1179. {
  1180. const BYTE* ip = (const BYTE*)seqStart;
  1181. const BYTE* const iend = ip + seqSize;
  1182. BYTE* const ostart = (BYTE* const)dst;
  1183. BYTE* const oend = ostart + maxDstSize;
  1184. BYTE* op = ostart;
  1185. const BYTE* litPtr = dctx->litPtr;
  1186. const BYTE* const litEnd = litPtr + dctx->litSize;
  1187. const BYTE* const base = (const BYTE*) (dctx->base);
  1188. const BYTE* const vBase = (const BYTE*) (dctx->vBase);
  1189. const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
  1190. int nbSeq;
  1191. /* Build Decoding Tables */
  1192. { size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, seqSize);
  1193. if (ZSTD_isError(seqHSize)) return seqHSize;
  1194. ip += seqHSize;
  1195. }
  1196. /* Regen sequences */
  1197. if (nbSeq) {
  1198. #define STORED_SEQS 4
  1199. #define STOSEQ_MASK (STORED_SEQS-1)
  1200. #define ADVANCED_SEQS 4
  1201. seq_t sequences[STORED_SEQS];
  1202. int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS);
  1203. seqState_t seqState;
  1204. int seqNb;
  1205. dctx->fseEntropy = 1;
  1206. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
  1207. seqState.base = base;
  1208. seqState.pos = (size_t)(op-base);
  1209. seqState.gotoDict = (uPtrDiff)dictEnd - (uPtrDiff)base; /* cast to avoid undefined behaviour */
  1210. CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend-ip), corruption_detected);
  1211. FSE_initDState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
  1212. FSE_initDState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
  1213. FSE_initDState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
  1214. /* prepare in advance */
  1215. for (seqNb=0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && seqNb<seqAdvance; seqNb++) {
  1216. sequences[seqNb] = ZSTD_decodeSequenceLong(&seqState, isLongOffset);
  1217. }
  1218. if (seqNb<seqAdvance) return ERROR(corruption_detected);
  1219. /* decode and decompress */
  1220. for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && seqNb<nbSeq ; seqNb++) {
  1221. seq_t const sequence = ZSTD_decodeSequenceLong(&seqState, isLongOffset);
  1222. size_t const oneSeqSize = ZSTD_execSequenceLong(op, oend, sequences[(seqNb-ADVANCED_SEQS) & STOSEQ_MASK], &litPtr, litEnd, base, vBase, dictEnd);
  1223. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  1224. PREFETCH(sequence.match);
  1225. sequences[seqNb&STOSEQ_MASK] = sequence;
  1226. op += oneSeqSize;
  1227. }
  1228. if (seqNb<nbSeq) return ERROR(corruption_detected);
  1229. /* finish queue */
  1230. seqNb -= seqAdvance;
  1231. for ( ; seqNb<nbSeq ; seqNb++) {
  1232. size_t const oneSeqSize = ZSTD_execSequenceLong(op, oend, sequences[seqNb&STOSEQ_MASK], &litPtr, litEnd, base, vBase, dictEnd);
  1233. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  1234. op += oneSeqSize;
  1235. }
  1236. /* save reps for next block */
  1237. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
  1238. }
  1239. /* last literal segment */
  1240. { size_t const lastLLSize = litEnd - litPtr;
  1241. if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall);
  1242. memcpy(op, litPtr, lastLLSize);
  1243. op += lastLLSize;
  1244. }
  1245. return op-ostart;
  1246. }
  1247. static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
  1248. void* dst, size_t dstCapacity,
  1249. const void* src, size_t srcSize, const int frame)
  1250. { /* blockType == blockCompressed */
  1251. const BYTE* ip = (const BYTE*)src;
  1252. /* isLongOffset must be true if there are long offsets.
  1253. * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN.
  1254. * We don't expect that to be the case in 64-bit mode.
  1255. * If we are in block mode we don't know the window size, so we have to be
  1256. * conservative.
  1257. */
  1258. ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN)));
  1259. /* windowSize could be any value at this point, since it is only validated
  1260. * in the streaming API.
  1261. */
  1262. DEBUGLOG(5, "ZSTD_decompressBlock_internal (size : %u)", (U32)srcSize);
  1263. if (srcSize >= ZSTD_BLOCKSIZE_MAX) return ERROR(srcSize_wrong);
  1264. /* Decode literals section */
  1265. { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
  1266. DEBUGLOG(5, "ZSTD_decodeLiteralsBlock : %u", (U32)litCSize);
  1267. if (ZSTD_isError(litCSize)) return litCSize;
  1268. ip += litCSize;
  1269. srcSize -= litCSize;
  1270. }
  1271. if (frame && dctx->fParams.windowSize > (1<<23))
  1272. return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, isLongOffset);
  1273. return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, isLongOffset);
  1274. }
  1275. static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
  1276. {
  1277. if (dst != dctx->previousDstEnd) { /* not contiguous */
  1278. dctx->dictEnd = dctx->previousDstEnd;
  1279. dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
  1280. dctx->base = dst;
  1281. dctx->previousDstEnd = dst;
  1282. }
  1283. }
  1284. size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
  1285. void* dst, size_t dstCapacity,
  1286. const void* src, size_t srcSize)
  1287. {
  1288. size_t dSize;
  1289. ZSTD_checkContinuity(dctx, dst);
  1290. dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 0);
  1291. dctx->previousDstEnd = (char*)dst + dSize;
  1292. return dSize;
  1293. }
  1294. /** ZSTD_insertBlock() :
  1295. insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
  1296. ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
  1297. {
  1298. ZSTD_checkContinuity(dctx, blockStart);
  1299. dctx->previousDstEnd = (const char*)blockStart + blockSize;
  1300. return blockSize;
  1301. }
  1302. static size_t ZSTD_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length)
  1303. {
  1304. if (length > dstCapacity) return ERROR(dstSize_tooSmall);
  1305. memset(dst, byte, length);
  1306. return length;
  1307. }
  1308. /** ZSTD_findFrameCompressedSize() :
  1309. * compatible with legacy mode
  1310. * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame
  1311. * `srcSize` must be at least as large as the frame contained
  1312. * @return : the compressed size of the frame starting at `src` */
  1313. size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
  1314. {
  1315. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  1316. if (ZSTD_isLegacy(src, srcSize))
  1317. return ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
  1318. #endif
  1319. if ( (srcSize >= ZSTD_skippableHeaderSize)
  1320. && (MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START ) {
  1321. return ZSTD_skippableHeaderSize + MEM_readLE32((const BYTE*)src + ZSTD_frameIdSize);
  1322. } else {
  1323. const BYTE* ip = (const BYTE*)src;
  1324. const BYTE* const ipstart = ip;
  1325. size_t remainingSize = srcSize;
  1326. ZSTD_frameHeader zfh;
  1327. /* Extract Frame Header */
  1328. { size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize);
  1329. if (ZSTD_isError(ret)) return ret;
  1330. if (ret > 0) return ERROR(srcSize_wrong);
  1331. }
  1332. ip += zfh.headerSize;
  1333. remainingSize -= zfh.headerSize;
  1334. /* Loop on each block */
  1335. while (1) {
  1336. blockProperties_t blockProperties;
  1337. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
  1338. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  1339. if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)
  1340. return ERROR(srcSize_wrong);
  1341. ip += ZSTD_blockHeaderSize + cBlockSize;
  1342. remainingSize -= ZSTD_blockHeaderSize + cBlockSize;
  1343. if (blockProperties.lastBlock) break;
  1344. }
  1345. if (zfh.checksumFlag) { /* Final frame content checksum */
  1346. if (remainingSize < 4) return ERROR(srcSize_wrong);
  1347. ip += 4;
  1348. remainingSize -= 4;
  1349. }
  1350. return ip - ipstart;
  1351. }
  1352. }
  1353. /*! ZSTD_decompressFrame() :
  1354. * @dctx must be properly initialized */
  1355. static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
  1356. void* dst, size_t dstCapacity,
  1357. const void** srcPtr, size_t *srcSizePtr)
  1358. {
  1359. const BYTE* ip = (const BYTE*)(*srcPtr);
  1360. BYTE* const ostart = (BYTE* const)dst;
  1361. BYTE* const oend = ostart + dstCapacity;
  1362. BYTE* op = ostart;
  1363. size_t remainingSize = *srcSizePtr;
  1364. /* check */
  1365. if (remainingSize < ZSTD_frameHeaderSize_min+ZSTD_blockHeaderSize)
  1366. return ERROR(srcSize_wrong);
  1367. /* Frame Header */
  1368. { size_t const frameHeaderSize = ZSTD_frameHeaderSize(ip, ZSTD_frameHeaderSize_prefix);
  1369. if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
  1370. if (remainingSize < frameHeaderSize+ZSTD_blockHeaderSize)
  1371. return ERROR(srcSize_wrong);
  1372. CHECK_F( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) );
  1373. ip += frameHeaderSize; remainingSize -= frameHeaderSize;
  1374. }
  1375. /* Loop on each block */
  1376. while (1) {
  1377. size_t decodedSize;
  1378. blockProperties_t blockProperties;
  1379. size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
  1380. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  1381. ip += ZSTD_blockHeaderSize;
  1382. remainingSize -= ZSTD_blockHeaderSize;
  1383. if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
  1384. switch(blockProperties.blockType)
  1385. {
  1386. case bt_compressed:
  1387. decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1);
  1388. break;
  1389. case bt_raw :
  1390. decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);
  1391. break;
  1392. case bt_rle :
  1393. decodedSize = ZSTD_generateNxBytes(op, oend-op, *ip, blockProperties.origSize);
  1394. break;
  1395. case bt_reserved :
  1396. default:
  1397. return ERROR(corruption_detected);
  1398. }
  1399. if (ZSTD_isError(decodedSize)) return decodedSize;
  1400. if (dctx->fParams.checksumFlag)
  1401. XXH64_update(&dctx->xxhState, op, decodedSize);
  1402. op += decodedSize;
  1403. ip += cBlockSize;
  1404. remainingSize -= cBlockSize;
  1405. if (blockProperties.lastBlock) break;
  1406. }
  1407. if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
  1408. if ((U64)(op-ostart) != dctx->fParams.frameContentSize) {
  1409. return ERROR(corruption_detected);
  1410. } }
  1411. if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
  1412. U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
  1413. U32 checkRead;
  1414. if (remainingSize<4) return ERROR(checksum_wrong);
  1415. checkRead = MEM_readLE32(ip);
  1416. if (checkRead != checkCalc) return ERROR(checksum_wrong);
  1417. ip += 4;
  1418. remainingSize -= 4;
  1419. }
  1420. /* Allow caller to get size read */
  1421. *srcPtr = ip;
  1422. *srcSizePtr = remainingSize;
  1423. return op-ostart;
  1424. }
  1425. static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict);
  1426. static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict);
  1427. static size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
  1428. void* dst, size_t dstCapacity,
  1429. const void* src, size_t srcSize,
  1430. const void* dict, size_t dictSize,
  1431. const ZSTD_DDict* ddict)
  1432. {
  1433. void* const dststart = dst;
  1434. assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */
  1435. if (ddict) {
  1436. dict = ZSTD_DDictDictContent(ddict);
  1437. dictSize = ZSTD_DDictDictSize(ddict);
  1438. }
  1439. while (srcSize >= ZSTD_frameHeaderSize_prefix) {
  1440. U32 magicNumber;
  1441. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
  1442. if (ZSTD_isLegacy(src, srcSize)) {
  1443. size_t decodedSize;
  1444. size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
  1445. if (ZSTD_isError(frameSize)) return frameSize;
  1446. /* legacy support is not compatible with static dctx */
  1447. if (dctx->staticSize) return ERROR(memory_allocation);
  1448. decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
  1449. dst = (BYTE*)dst + decodedSize;
  1450. dstCapacity -= decodedSize;
  1451. src = (const BYTE*)src + frameSize;
  1452. srcSize -= frameSize;
  1453. continue;
  1454. }
  1455. #endif
  1456. magicNumber = MEM_readLE32(src);
  1457. DEBUGLOG(4, "reading magic number %08X (expecting %08X)",
  1458. (U32)magicNumber, (U32)ZSTD_MAGICNUMBER);
  1459. if (magicNumber != ZSTD_MAGICNUMBER) {
  1460. if ((magicNumber & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
  1461. size_t skippableSize;
  1462. if (srcSize < ZSTD_skippableHeaderSize)
  1463. return ERROR(srcSize_wrong);
  1464. skippableSize = MEM_readLE32((const BYTE*)src + ZSTD_frameIdSize)
  1465. + ZSTD_skippableHeaderSize;
  1466. if (srcSize < skippableSize) return ERROR(srcSize_wrong);
  1467. src = (const BYTE *)src + skippableSize;
  1468. srcSize -= skippableSize;
  1469. continue;
  1470. }
  1471. return ERROR(prefix_unknown);
  1472. }
  1473. if (ddict) {
  1474. /* we were called from ZSTD_decompress_usingDDict */
  1475. CHECK_F(ZSTD_decompressBegin_usingDDict(dctx, ddict));
  1476. } else {
  1477. /* this will initialize correctly with no dict if dict == NULL, so
  1478. * use this in all cases but ddict */
  1479. CHECK_F(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize));
  1480. }
  1481. ZSTD_checkContinuity(dctx, dst);
  1482. { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,
  1483. &src, &srcSize);
  1484. if (ZSTD_isError(res)) return res;
  1485. /* no need to bound check, ZSTD_decompressFrame already has */
  1486. dst = (BYTE*)dst + res;
  1487. dstCapacity -= res;
  1488. }
  1489. } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */
  1490. if (srcSize) return ERROR(srcSize_wrong); /* input not entirely consumed */
  1491. return (BYTE*)dst - (BYTE*)dststart;
  1492. }
  1493. size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
  1494. void* dst, size_t dstCapacity,
  1495. const void* src, size_t srcSize,
  1496. const void* dict, size_t dictSize)
  1497. {
  1498. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
  1499. }
  1500. size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  1501. {
  1502. return ZSTD_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0);
  1503. }
  1504. size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  1505. {
  1506. #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)
  1507. size_t regenSize;
  1508. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  1509. if (dctx==NULL) return ERROR(memory_allocation);
  1510. regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
  1511. ZSTD_freeDCtx(dctx);
  1512. return regenSize;
  1513. #else /* stack mode */
  1514. ZSTD_DCtx dctx;
  1515. return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
  1516. #endif
  1517. }
  1518. /*-**************************************
  1519. * Advanced Streaming Decompression API
  1520. * Bufferless and synchronous
  1521. ****************************************/
  1522. size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
  1523. ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
  1524. switch(dctx->stage)
  1525. {
  1526. default: /* should not happen */
  1527. assert(0);
  1528. case ZSTDds_getFrameHeaderSize:
  1529. case ZSTDds_decodeFrameHeader:
  1530. return ZSTDnit_frameHeader;
  1531. case ZSTDds_decodeBlockHeader:
  1532. return ZSTDnit_blockHeader;
  1533. case ZSTDds_decompressBlock:
  1534. return ZSTDnit_block;
  1535. case ZSTDds_decompressLastBlock:
  1536. return ZSTDnit_lastBlock;
  1537. case ZSTDds_checkChecksum:
  1538. return ZSTDnit_checksum;
  1539. case ZSTDds_decodeSkippableHeader:
  1540. case ZSTDds_skipFrame:
  1541. return ZSTDnit_skippableFrame;
  1542. }
  1543. }
  1544. static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
  1545. /** ZSTD_decompressContinue() :
  1546. * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
  1547. * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
  1548. * or an error code, which can be tested using ZSTD_isError() */
  1549. size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  1550. {
  1551. DEBUGLOG(5, "ZSTD_decompressContinue");
  1552. /* Sanity check */
  1553. if (srcSize != dctx->expected) return ERROR(srcSize_wrong); /* not allowed */
  1554. if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
  1555. switch (dctx->stage)
  1556. {
  1557. case ZSTDds_getFrameHeaderSize :
  1558. assert(src != NULL);
  1559. if (dctx->format == ZSTD_f_zstd1) { /* allows header */
  1560. assert(srcSize >= ZSTD_frameIdSize); /* to read skippable magic number */
  1561. if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  1562. memcpy(dctx->headerBuffer, src, srcSize);
  1563. dctx->expected = ZSTD_skippableHeaderSize - srcSize; /* remaining to load to get full skippable frame header */
  1564. dctx->stage = ZSTDds_decodeSkippableHeader;
  1565. return 0;
  1566. } }
  1567. dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);
  1568. if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
  1569. memcpy(dctx->headerBuffer, src, srcSize);
  1570. dctx->expected = dctx->headerSize - srcSize;
  1571. dctx->stage = ZSTDds_decodeFrameHeader;
  1572. return 0;
  1573. case ZSTDds_decodeFrameHeader:
  1574. assert(src != NULL);
  1575. memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);
  1576. CHECK_F(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize));
  1577. dctx->expected = ZSTD_blockHeaderSize;
  1578. dctx->stage = ZSTDds_decodeBlockHeader;
  1579. return 0;
  1580. case ZSTDds_decodeBlockHeader:
  1581. { blockProperties_t bp;
  1582. size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
  1583. if (ZSTD_isError(cBlockSize)) return cBlockSize;
  1584. dctx->expected = cBlockSize;
  1585. dctx->bType = bp.blockType;
  1586. dctx->rleSize = bp.origSize;
  1587. if (cBlockSize) {
  1588. dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
  1589. return 0;
  1590. }
  1591. /* empty block */
  1592. if (bp.lastBlock) {
  1593. if (dctx->fParams.checksumFlag) {
  1594. dctx->expected = 4;
  1595. dctx->stage = ZSTDds_checkChecksum;
  1596. } else {
  1597. dctx->expected = 0; /* end of frame */
  1598. dctx->stage = ZSTDds_getFrameHeaderSize;
  1599. }
  1600. } else {
  1601. dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */
  1602. dctx->stage = ZSTDds_decodeBlockHeader;
  1603. }
  1604. return 0;
  1605. }
  1606. case ZSTDds_decompressLastBlock:
  1607. case ZSTDds_decompressBlock:
  1608. DEBUGLOG(5, "case ZSTDds_decompressBlock");
  1609. { size_t rSize;
  1610. switch(dctx->bType)
  1611. {
  1612. case bt_compressed:
  1613. DEBUGLOG(5, "case bt_compressed");
  1614. rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1);
  1615. break;
  1616. case bt_raw :
  1617. rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
  1618. break;
  1619. case bt_rle :
  1620. rSize = ZSTD_setRleBlock(dst, dstCapacity, src, srcSize, dctx->rleSize);
  1621. break;
  1622. case bt_reserved : /* should never happen */
  1623. default:
  1624. return ERROR(corruption_detected);
  1625. }
  1626. if (ZSTD_isError(rSize)) return rSize;
  1627. DEBUGLOG(5, "decoded size from block : %u", (U32)rSize);
  1628. dctx->decodedSize += rSize;
  1629. if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);
  1630. if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */
  1631. DEBUGLOG(4, "decoded size from frame : %u", (U32)dctx->decodedSize);
  1632. if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {
  1633. if (dctx->decodedSize != dctx->fParams.frameContentSize) {
  1634. return ERROR(corruption_detected);
  1635. } }
  1636. if (dctx->fParams.checksumFlag) { /* another round for frame checksum */
  1637. dctx->expected = 4;
  1638. dctx->stage = ZSTDds_checkChecksum;
  1639. } else {
  1640. dctx->expected = 0; /* ends here */
  1641. dctx->stage = ZSTDds_getFrameHeaderSize;
  1642. }
  1643. } else {
  1644. dctx->stage = ZSTDds_decodeBlockHeader;
  1645. dctx->expected = ZSTD_blockHeaderSize;
  1646. dctx->previousDstEnd = (char*)dst + rSize;
  1647. }
  1648. return rSize;
  1649. }
  1650. case ZSTDds_checkChecksum:
  1651. assert(srcSize == 4); /* guaranteed by dctx->expected */
  1652. { U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
  1653. U32 const check32 = MEM_readLE32(src);
  1654. DEBUGLOG(4, "checksum : calculated %08X :: %08X read", h32, check32);
  1655. if (check32 != h32) return ERROR(checksum_wrong);
  1656. dctx->expected = 0;
  1657. dctx->stage = ZSTDds_getFrameHeaderSize;
  1658. return 0;
  1659. }
  1660. case ZSTDds_decodeSkippableHeader:
  1661. assert(src != NULL);
  1662. assert(srcSize <= ZSTD_skippableHeaderSize);
  1663. memcpy(dctx->headerBuffer + (ZSTD_skippableHeaderSize - srcSize), src, srcSize); /* complete skippable header */
  1664. dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_frameIdSize); /* note : dctx->expected can grow seriously large, beyond local buffer size */
  1665. dctx->stage = ZSTDds_skipFrame;
  1666. return 0;
  1667. case ZSTDds_skipFrame:
  1668. dctx->expected = 0;
  1669. dctx->stage = ZSTDds_getFrameHeaderSize;
  1670. return 0;
  1671. default:
  1672. return ERROR(GENERIC); /* impossible */
  1673. }
  1674. }
  1675. static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1676. {
  1677. dctx->dictEnd = dctx->previousDstEnd;
  1678. dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
  1679. dctx->base = dict;
  1680. dctx->previousDstEnd = (const char*)dict + dictSize;
  1681. return 0;
  1682. }
  1683. /* ZSTD_loadEntropy() :
  1684. * dict : must point at beginning of a valid zstd dictionary
  1685. * @return : size of entropy tables read */
  1686. static size_t ZSTD_loadEntropy(ZSTD_entropyDTables_t* entropy, const void* const dict, size_t const dictSize)
  1687. {
  1688. const BYTE* dictPtr = (const BYTE*)dict;
  1689. const BYTE* const dictEnd = dictPtr + dictSize;
  1690. if (dictSize <= 8) return ERROR(dictionary_corrupted);
  1691. dictPtr += 8; /* skip header = magic + dictID */
  1692. { size_t const hSize = HUF_readDTableX4_wksp(
  1693. entropy->hufTable, dictPtr, dictEnd - dictPtr,
  1694. entropy->workspace, sizeof(entropy->workspace));
  1695. if (HUF_isError(hSize)) return ERROR(dictionary_corrupted);
  1696. dictPtr += hSize;
  1697. }
  1698. { short offcodeNCount[MaxOff+1];
  1699. U32 offcodeMaxValue = MaxOff, offcodeLog;
  1700. size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
  1701. if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
  1702. if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted);
  1703. CHECK_E(FSE_buildDTable(entropy->OFTable, offcodeNCount, offcodeMaxValue, offcodeLog), dictionary_corrupted);
  1704. dictPtr += offcodeHeaderSize;
  1705. }
  1706. { short matchlengthNCount[MaxML+1];
  1707. unsigned matchlengthMaxValue = MaxML, matchlengthLog;
  1708. size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
  1709. if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
  1710. if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted);
  1711. CHECK_E(FSE_buildDTable(entropy->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog), dictionary_corrupted);
  1712. dictPtr += matchlengthHeaderSize;
  1713. }
  1714. { short litlengthNCount[MaxLL+1];
  1715. unsigned litlengthMaxValue = MaxLL, litlengthLog;
  1716. size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
  1717. if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
  1718. if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted);
  1719. CHECK_E(FSE_buildDTable(entropy->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog), dictionary_corrupted);
  1720. dictPtr += litlengthHeaderSize;
  1721. }
  1722. if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted);
  1723. { int i;
  1724. size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));
  1725. for (i=0; i<3; i++) {
  1726. U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;
  1727. if (rep==0 || rep >= dictContentSize) return ERROR(dictionary_corrupted);
  1728. entropy->rep[i] = rep;
  1729. } }
  1730. return dictPtr - (const BYTE*)dict;
  1731. }
  1732. static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1733. {
  1734. if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
  1735. { U32 const magic = MEM_readLE32(dict);
  1736. if (magic != ZSTD_MAGIC_DICTIONARY) {
  1737. return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
  1738. } }
  1739. dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_frameIdSize);
  1740. /* load entropy tables */
  1741. { size_t const eSize = ZSTD_loadEntropy(&dctx->entropy, dict, dictSize);
  1742. if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted);
  1743. dict = (const char*)dict + eSize;
  1744. dictSize -= eSize;
  1745. }
  1746. dctx->litEntropy = dctx->fseEntropy = 1;
  1747. /* reference dictionary content */
  1748. return ZSTD_refDictContent(dctx, dict, dictSize);
  1749. }
  1750. /* Note : this function cannot fail */
  1751. size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
  1752. {
  1753. assert(dctx != NULL);
  1754. dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
  1755. dctx->stage = ZSTDds_getFrameHeaderSize;
  1756. dctx->decodedSize = 0;
  1757. dctx->previousDstEnd = NULL;
  1758. dctx->base = NULL;
  1759. dctx->vBase = NULL;
  1760. dctx->dictEnd = NULL;
  1761. dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
  1762. dctx->litEntropy = dctx->fseEntropy = 0;
  1763. dctx->dictID = 0;
  1764. ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));
  1765. memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
  1766. dctx->LLTptr = dctx->entropy.LLTable;
  1767. dctx->MLTptr = dctx->entropy.MLTable;
  1768. dctx->OFTptr = dctx->entropy.OFTable;
  1769. dctx->HUFptr = dctx->entropy.hufTable;
  1770. return 0;
  1771. }
  1772. size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
  1773. {
  1774. CHECK_F( ZSTD_decompressBegin(dctx) );
  1775. if (dict && dictSize)
  1776. CHECK_E(ZSTD_decompress_insertDictionary(dctx, dict, dictSize), dictionary_corrupted);
  1777. return 0;
  1778. }
  1779. /* ====== ZSTD_DDict ====== */
  1780. struct ZSTD_DDict_s {
  1781. void* dictBuffer;
  1782. const void* dictContent;
  1783. size_t dictSize;
  1784. ZSTD_entropyDTables_t entropy;
  1785. U32 dictID;
  1786. U32 entropyPresent;
  1787. ZSTD_customMem cMem;
  1788. }; /* typedef'd to ZSTD_DDict within "zstd.h" */
  1789. static const void* ZSTD_DDictDictContent(const ZSTD_DDict* ddict)
  1790. {
  1791. return ddict->dictContent;
  1792. }
  1793. static size_t ZSTD_DDictDictSize(const ZSTD_DDict* ddict)
  1794. {
  1795. return ddict->dictSize;
  1796. }
  1797. size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dstDCtx, const ZSTD_DDict* ddict)
  1798. {
  1799. CHECK_F( ZSTD_decompressBegin(dstDCtx) );
  1800. if (ddict) { /* support begin on NULL */
  1801. dstDCtx->dictID = ddict->dictID;
  1802. dstDCtx->base = ddict->dictContent;
  1803. dstDCtx->vBase = ddict->dictContent;
  1804. dstDCtx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize;
  1805. dstDCtx->previousDstEnd = dstDCtx->dictEnd;
  1806. if (ddict->entropyPresent) {
  1807. dstDCtx->litEntropy = 1;
  1808. dstDCtx->fseEntropy = 1;
  1809. dstDCtx->LLTptr = ddict->entropy.LLTable;
  1810. dstDCtx->MLTptr = ddict->entropy.MLTable;
  1811. dstDCtx->OFTptr = ddict->entropy.OFTable;
  1812. dstDCtx->HUFptr = ddict->entropy.hufTable;
  1813. dstDCtx->entropy.rep[0] = ddict->entropy.rep[0];
  1814. dstDCtx->entropy.rep[1] = ddict->entropy.rep[1];
  1815. dstDCtx->entropy.rep[2] = ddict->entropy.rep[2];
  1816. } else {
  1817. dstDCtx->litEntropy = 0;
  1818. dstDCtx->fseEntropy = 0;
  1819. }
  1820. }
  1821. return 0;
  1822. }
  1823. static size_t ZSTD_loadEntropy_inDDict(ZSTD_DDict* ddict)
  1824. {
  1825. ddict->dictID = 0;
  1826. ddict->entropyPresent = 0;
  1827. if (ddict->dictSize < 8) return 0;
  1828. { U32 const magic = MEM_readLE32(ddict->dictContent);
  1829. if (magic != ZSTD_MAGIC_DICTIONARY) return 0; /* pure content mode */
  1830. }
  1831. ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_frameIdSize);
  1832. /* load entropy tables */
  1833. CHECK_E( ZSTD_loadEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize), dictionary_corrupted );
  1834. ddict->entropyPresent = 1;
  1835. return 0;
  1836. }
  1837. static size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)
  1838. {
  1839. if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {
  1840. ddict->dictBuffer = NULL;
  1841. ddict->dictContent = dict;
  1842. } else {
  1843. void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem);
  1844. ddict->dictBuffer = internalBuffer;
  1845. ddict->dictContent = internalBuffer;
  1846. if (!internalBuffer) return ERROR(memory_allocation);
  1847. memcpy(internalBuffer, dict, dictSize);
  1848. }
  1849. ddict->dictSize = dictSize;
  1850. ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
  1851. /* parse dictionary content */
  1852. CHECK_F( ZSTD_loadEntropy_inDDict(ddict) );
  1853. return 0;
  1854. }
  1855. ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_customMem customMem)
  1856. {
  1857. if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
  1858. { ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
  1859. if (!ddict) return NULL;
  1860. ddict->cMem = customMem;
  1861. if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, dictLoadMethod) )) {
  1862. ZSTD_freeDDict(ddict);
  1863. return NULL;
  1864. }
  1865. return ddict;
  1866. }
  1867. }
  1868. /*! ZSTD_createDDict() :
  1869. * Create a digested dictionary, to start decompression without startup delay.
  1870. * `dict` content is copied inside DDict.
  1871. * Consequently, `dict` can be released after `ZSTD_DDict` creation */
  1872. ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize)
  1873. {
  1874. ZSTD_customMem const allocator = { NULL, NULL, NULL };
  1875. return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, allocator);
  1876. }
  1877. /*! ZSTD_createDDict_byReference() :
  1878. * Create a digested dictionary, to start decompression without startup delay.
  1879. * Dictionary content is simply referenced, it will be accessed during decompression.
  1880. * Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */
  1881. ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize)
  1882. {
  1883. ZSTD_customMem const allocator = { NULL, NULL, NULL };
  1884. return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, allocator);
  1885. }
  1886. ZSTD_DDict* ZSTD_initStaticDDict(void* workspace, size_t workspaceSize,
  1887. const void* dict, size_t dictSize,
  1888. ZSTD_dictLoadMethod_e dictLoadMethod)
  1889. {
  1890. size_t const neededSpace =
  1891. sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
  1892. ZSTD_DDict* const ddict = (ZSTD_DDict*)workspace;
  1893. assert(workspace != NULL);
  1894. assert(dict != NULL);
  1895. if ((size_t)workspace & 7) return NULL; /* 8-aligned */
  1896. if (workspaceSize < neededSpace) return NULL;
  1897. if (dictLoadMethod == ZSTD_dlm_byCopy) {
  1898. memcpy(ddict+1, dict, dictSize); /* local copy */
  1899. dict = ddict+1;
  1900. }
  1901. if (ZSTD_isError( ZSTD_initDDict_internal(ddict, dict, dictSize, ZSTD_dlm_byRef) ))
  1902. return NULL;
  1903. return ddict;
  1904. }
  1905. size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
  1906. {
  1907. if (ddict==NULL) return 0; /* support free on NULL */
  1908. { ZSTD_customMem const cMem = ddict->cMem;
  1909. ZSTD_free(ddict->dictBuffer, cMem);
  1910. ZSTD_free(ddict, cMem);
  1911. return 0;
  1912. }
  1913. }
  1914. /*! ZSTD_estimateDDictSize() :
  1915. * Estimate amount of memory that will be needed to create a dictionary for decompression.
  1916. * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */
  1917. size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)
  1918. {
  1919. return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);
  1920. }
  1921. size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict)
  1922. {
  1923. if (ddict==NULL) return 0; /* support sizeof on NULL */
  1924. return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ;
  1925. }
  1926. /*! ZSTD_getDictID_fromDict() :
  1927. * Provides the dictID stored within dictionary.
  1928. * if @return == 0, the dictionary is not conformant with Zstandard specification.
  1929. * It can still be loaded, but as a content-only dictionary. */
  1930. unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)
  1931. {
  1932. if (dictSize < 8) return 0;
  1933. if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;
  1934. return MEM_readLE32((const char*)dict + ZSTD_frameIdSize);
  1935. }
  1936. /*! ZSTD_getDictID_fromDDict() :
  1937. * Provides the dictID of the dictionary loaded into `ddict`.
  1938. * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
  1939. * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
  1940. unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict)
  1941. {
  1942. if (ddict==NULL) return 0;
  1943. return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize);
  1944. }
  1945. /*! ZSTD_getDictID_fromFrame() :
  1946. * Provides the dictID required to decompresse frame stored within `src`.
  1947. * If @return == 0, the dictID could not be decoded.
  1948. * This could for one of the following reasons :
  1949. * - The frame does not require a dictionary (most common case).
  1950. * - The frame was built with dictID intentionally removed.
  1951. * Needed dictionary is a hidden information.
  1952. * Note : this use case also happens when using a non-conformant dictionary.
  1953. * - `srcSize` is too small, and as a result, frame header could not be decoded.
  1954. * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.
  1955. * - This is not a Zstandard frame.
  1956. * When identifying the exact failure cause, it's possible to use
  1957. * ZSTD_getFrameHeader(), which will provide a more precise error code. */
  1958. unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)
  1959. {
  1960. ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 };
  1961. size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);
  1962. if (ZSTD_isError(hError)) return 0;
  1963. return zfp.dictID;
  1964. }
  1965. /*! ZSTD_decompress_usingDDict() :
  1966. * Decompression using a pre-digested Dictionary
  1967. * Use dictionary without significant overhead. */
  1968. size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
  1969. void* dst, size_t dstCapacity,
  1970. const void* src, size_t srcSize,
  1971. const ZSTD_DDict* ddict)
  1972. {
  1973. /* pass content and size in case legacy frames are encountered */
  1974. return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
  1975. NULL, 0,
  1976. ddict);
  1977. }
  1978. /*=====================================
  1979. * Streaming decompression
  1980. *====================================*/
  1981. ZSTD_DStream* ZSTD_createDStream(void)
  1982. {
  1983. return ZSTD_createDStream_advanced(ZSTD_defaultCMem);
  1984. }
  1985. ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)
  1986. {
  1987. return ZSTD_initStaticDCtx(workspace, workspaceSize);
  1988. }
  1989. ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
  1990. {
  1991. return ZSTD_createDCtx_advanced(customMem);
  1992. }
  1993. size_t ZSTD_freeDStream(ZSTD_DStream* zds)
  1994. {
  1995. return ZSTD_freeDCtx(zds);
  1996. }
  1997. /* *** Initialization *** */
  1998. size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }
  1999. size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
  2000. size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
  2001. {
  2002. zds->streamStage = zdss_loadHeader;
  2003. zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
  2004. ZSTD_freeDDict(zds->ddictLocal);
  2005. if (dict && dictSize >= 8) {
  2006. zds->ddictLocal = ZSTD_createDDict(dict, dictSize);
  2007. if (zds->ddictLocal == NULL) return ERROR(memory_allocation);
  2008. } else zds->ddictLocal = NULL;
  2009. zds->ddict = zds->ddictLocal;
  2010. zds->legacyVersion = 0;
  2011. zds->hostageByte = 0;
  2012. return ZSTD_frameHeaderSize_prefix;
  2013. }
  2014. /* note : this variant can't fail */
  2015. size_t ZSTD_initDStream(ZSTD_DStream* zds)
  2016. {
  2017. return ZSTD_initDStream_usingDict(zds, NULL, 0);
  2018. }
  2019. /* ZSTD_initDStream_usingDDict() :
  2020. * ddict will just be referenced, and must outlive decompression session
  2021. * this function cannot fail */
  2022. size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict)
  2023. {
  2024. size_t const initResult = ZSTD_initDStream(zds);
  2025. zds->ddict = ddict;
  2026. return initResult;
  2027. }
  2028. size_t ZSTD_resetDStream(ZSTD_DStream* zds)
  2029. {
  2030. zds->streamStage = zdss_loadHeader;
  2031. zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
  2032. zds->legacyVersion = 0;
  2033. zds->hostageByte = 0;
  2034. return ZSTD_frameHeaderSize_prefix;
  2035. }
  2036. size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds,
  2037. ZSTD_DStreamParameter_e paramType, unsigned paramValue)
  2038. {
  2039. ZSTD_STATIC_ASSERT((unsigned)zdss_loadHeader >= (unsigned)zdss_init);
  2040. if ((unsigned)zds->streamStage > (unsigned)zdss_loadHeader)
  2041. return ERROR(stage_wrong);
  2042. switch(paramType)
  2043. {
  2044. default : return ERROR(parameter_unsupported);
  2045. case DStream_p_maxWindowSize :
  2046. DEBUGLOG(4, "setting maxWindowSize = %u KB", paramValue >> 10);
  2047. zds->maxWindowSize = paramValue ? paramValue : (U32)(-1);
  2048. break;
  2049. }
  2050. return 0;
  2051. }
  2052. size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)
  2053. {
  2054. ZSTD_STATIC_ASSERT((unsigned)zdss_loadHeader >= (unsigned)zdss_init);
  2055. if ((unsigned)dctx->streamStage > (unsigned)zdss_loadHeader)
  2056. return ERROR(stage_wrong);
  2057. dctx->maxWindowSize = maxWindowSize;
  2058. return 0;
  2059. }
  2060. size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)
  2061. {
  2062. DEBUGLOG(4, "ZSTD_DCtx_setFormat : %u", (unsigned)format);
  2063. ZSTD_STATIC_ASSERT((unsigned)zdss_loadHeader >= (unsigned)zdss_init);
  2064. if ((unsigned)dctx->streamStage > (unsigned)zdss_loadHeader)
  2065. return ERROR(stage_wrong);
  2066. dctx->format = format;
  2067. return 0;
  2068. }
  2069. size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds)
  2070. {
  2071. return ZSTD_sizeof_DCtx(zds);
  2072. }
  2073. size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)
  2074. {
  2075. size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  2076. unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2);
  2077. unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);
  2078. size_t const minRBSize = (size_t) neededSize;
  2079. if ((unsigned long long)minRBSize != neededSize) return ERROR(frameParameter_windowTooLarge);
  2080. return minRBSize;
  2081. }
  2082. size_t ZSTD_estimateDStreamSize(size_t windowSize)
  2083. {
  2084. size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);
  2085. size_t const inBuffSize = blockSize; /* no block can be larger */
  2086. size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);
  2087. return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;
  2088. }
  2089. size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)
  2090. {
  2091. U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable */
  2092. ZSTD_frameHeader zfh;
  2093. size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);
  2094. if (ZSTD_isError(err)) return err;
  2095. if (err>0) return ERROR(srcSize_wrong);
  2096. if (zfh.windowSize > windowSizeMax)
  2097. return ERROR(frameParameter_windowTooLarge);
  2098. return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);
  2099. }
  2100. /* ***** Decompression ***** */
  2101. MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
  2102. {
  2103. size_t const length = MIN(dstCapacity, srcSize);
  2104. memcpy(dst, src, length);
  2105. return length;
  2106. }
  2107. size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  2108. {
  2109. const char* const istart = (const char*)(input->src) + input->pos;
  2110. const char* const iend = (const char*)(input->src) + input->size;
  2111. const char* ip = istart;
  2112. char* const ostart = (char*)(output->dst) + output->pos;
  2113. char* const oend = (char*)(output->dst) + output->size;
  2114. char* op = ostart;
  2115. U32 someMoreWork = 1;
  2116. DEBUGLOG(5, "ZSTD_decompressStream");
  2117. if (input->pos > input->size) { /* forbidden */
  2118. DEBUGLOG(5, "in: pos: %u vs size: %u",
  2119. (U32)input->pos, (U32)input->size);
  2120. return ERROR(srcSize_wrong);
  2121. }
  2122. if (output->pos > output->size) { /* forbidden */
  2123. DEBUGLOG(5, "out: pos: %u vs size: %u",
  2124. (U32)output->pos, (U32)output->size);
  2125. return ERROR(dstSize_tooSmall);
  2126. }
  2127. DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos));
  2128. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  2129. if (zds->legacyVersion) {
  2130. /* legacy support is incompatible with static dctx */
  2131. if (zds->staticSize) return ERROR(memory_allocation);
  2132. return ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
  2133. }
  2134. #endif
  2135. while (someMoreWork) {
  2136. switch(zds->streamStage)
  2137. {
  2138. case zdss_init :
  2139. ZSTD_resetDStream(zds); /* transparent reset on starting decoding a new frame */
  2140. /* fall-through */
  2141. case zdss_loadHeader :
  2142. DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
  2143. { size_t const hSize = ZSTD_getFrameHeader_internal(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
  2144. DEBUGLOG(5, "header size : %u", (U32)hSize);
  2145. if (ZSTD_isError(hSize)) {
  2146. #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
  2147. U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
  2148. if (legacyVersion) {
  2149. const void* const dict = zds->ddict ? zds->ddict->dictContent : NULL;
  2150. size_t const dictSize = zds->ddict ? zds->ddict->dictSize : 0;
  2151. /* legacy support is incompatible with static dctx */
  2152. if (zds->staticSize) return ERROR(memory_allocation);
  2153. CHECK_F(ZSTD_initLegacyStream(&zds->legacyContext,
  2154. zds->previousLegacyVersion, legacyVersion,
  2155. dict, dictSize));
  2156. zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;
  2157. return ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input);
  2158. }
  2159. #endif
  2160. return hSize; /* error */
  2161. }
  2162. if (hSize != 0) { /* need more input */
  2163. size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */
  2164. if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */
  2165. if (iend-ip > 0) {
  2166. memcpy(zds->headerBuffer + zds->lhSize, ip, iend-ip);
  2167. zds->lhSize += iend-ip;
  2168. }
  2169. input->pos = input->size;
  2170. return (MAX(ZSTD_frameHeaderSize_min, hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
  2171. }
  2172. assert(ip != NULL);
  2173. memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
  2174. break;
  2175. } }
  2176. /* check for single-pass mode opportunity */
  2177. if (zds->fParams.frameContentSize && zds->fParams.windowSize /* skippable frame if == 0 */
  2178. && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
  2179. size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart);
  2180. if (cSize <= (size_t)(iend-istart)) {
  2181. size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, zds->ddict);
  2182. if (ZSTD_isError(decompressedSize)) return decompressedSize;
  2183. ip = istart + cSize;
  2184. op += decompressedSize;
  2185. zds->expected = 0;
  2186. zds->streamStage = zdss_init;
  2187. someMoreWork = 0;
  2188. break;
  2189. } }
  2190. /* Consume header (see ZSTDds_decodeFrameHeader) */
  2191. DEBUGLOG(4, "Consume header");
  2192. CHECK_F(ZSTD_decompressBegin_usingDDict(zds, zds->ddict));
  2193. if ((MEM_readLE32(zds->headerBuffer) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
  2194. zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_frameIdSize);
  2195. zds->stage = ZSTDds_skipFrame;
  2196. } else {
  2197. CHECK_F(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize));
  2198. zds->expected = ZSTD_blockHeaderSize;
  2199. zds->stage = ZSTDds_decodeBlockHeader;
  2200. }
  2201. /* control buffer memory usage */
  2202. DEBUGLOG(4, "Control max buffer memory usage (max %u KB)",
  2203. (U32)(zds->maxWindowSize >> 10));
  2204. zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
  2205. if (zds->fParams.windowSize > zds->maxWindowSize) return ERROR(frameParameter_windowTooLarge);
  2206. /* Adapt buffer sizes to frame header instructions */
  2207. { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);
  2208. size_t const neededOutBuffSize = ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize);
  2209. if ((zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize)) {
  2210. size_t const bufferSize = neededInBuffSize + neededOutBuffSize;
  2211. DEBUGLOG(4, "inBuff : from %u to %u",
  2212. (U32)zds->inBuffSize, (U32)neededInBuffSize);
  2213. DEBUGLOG(4, "outBuff : from %u to %u",
  2214. (U32)zds->outBuffSize, (U32)neededOutBuffSize);
  2215. if (zds->staticSize) { /* static DCtx */
  2216. DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize);
  2217. assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */
  2218. if (bufferSize > zds->staticSize - sizeof(ZSTD_DCtx))
  2219. return ERROR(memory_allocation);
  2220. } else {
  2221. ZSTD_free(zds->inBuff, zds->customMem);
  2222. zds->inBuffSize = 0;
  2223. zds->outBuffSize = 0;
  2224. zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem);
  2225. if (zds->inBuff == NULL) return ERROR(memory_allocation);
  2226. }
  2227. zds->inBuffSize = neededInBuffSize;
  2228. zds->outBuff = zds->inBuff + zds->inBuffSize;
  2229. zds->outBuffSize = neededOutBuffSize;
  2230. } }
  2231. zds->streamStage = zdss_read;
  2232. /* fall-through */
  2233. case zdss_read:
  2234. DEBUGLOG(5, "stage zdss_read");
  2235. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
  2236. DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize);
  2237. if (neededInSize==0) { /* end of frame */
  2238. zds->streamStage = zdss_init;
  2239. someMoreWork = 0;
  2240. break;
  2241. }
  2242. if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
  2243. int const isSkipFrame = ZSTD_isSkipFrame(zds);
  2244. size_t const decodedSize = ZSTD_decompressContinue(zds,
  2245. zds->outBuff + zds->outStart, (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart),
  2246. ip, neededInSize);
  2247. if (ZSTD_isError(decodedSize)) return decodedSize;
  2248. ip += neededInSize;
  2249. if (!decodedSize && !isSkipFrame) break; /* this was just a header */
  2250. zds->outEnd = zds->outStart + decodedSize;
  2251. zds->streamStage = zdss_flush;
  2252. break;
  2253. } }
  2254. if (ip==iend) { someMoreWork = 0; break; } /* no more input */
  2255. zds->streamStage = zdss_load;
  2256. /* fall-through */
  2257. case zdss_load:
  2258. { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);
  2259. size_t const toLoad = neededInSize - zds->inPos; /* should always be <= remaining space within inBuff */
  2260. size_t loadedSize;
  2261. if (toLoad > zds->inBuffSize - zds->inPos) return ERROR(corruption_detected); /* should never happen */
  2262. loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip);
  2263. ip += loadedSize;
  2264. zds->inPos += loadedSize;
  2265. if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
  2266. /* decode loaded input */
  2267. { const int isSkipFrame = ZSTD_isSkipFrame(zds);
  2268. size_t const decodedSize = ZSTD_decompressContinue(zds,
  2269. zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart,
  2270. zds->inBuff, neededInSize);
  2271. if (ZSTD_isError(decodedSize)) return decodedSize;
  2272. zds->inPos = 0; /* input is consumed */
  2273. if (!decodedSize && !isSkipFrame) { zds->streamStage = zdss_read; break; } /* this was just a header */
  2274. zds->outEnd = zds->outStart + decodedSize;
  2275. } }
  2276. zds->streamStage = zdss_flush;
  2277. /* fall-through */
  2278. case zdss_flush:
  2279. { size_t const toFlushSize = zds->outEnd - zds->outStart;
  2280. size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize);
  2281. op += flushedSize;
  2282. zds->outStart += flushedSize;
  2283. if (flushedSize == toFlushSize) { /* flush completed */
  2284. zds->streamStage = zdss_read;
  2285. if ( (zds->outBuffSize < zds->fParams.frameContentSize)
  2286. && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {
  2287. DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)",
  2288. (int)(zds->outBuffSize - zds->outStart),
  2289. (U32)zds->fParams.blockSizeMax);
  2290. zds->outStart = zds->outEnd = 0;
  2291. }
  2292. break;
  2293. } }
  2294. /* cannot complete flush */
  2295. someMoreWork = 0;
  2296. break;
  2297. default: return ERROR(GENERIC); /* impossible */
  2298. } }
  2299. /* result */
  2300. input->pos += (size_t)(ip-istart);
  2301. output->pos += (size_t)(op-ostart);
  2302. { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);
  2303. if (!nextSrcSizeHint) { /* frame fully decoded */
  2304. if (zds->outEnd == zds->outStart) { /* output fully flushed */
  2305. if (zds->hostageByte) {
  2306. if (input->pos >= input->size) {
  2307. /* can't release hostage (not present) */
  2308. zds->streamStage = zdss_read;
  2309. return 1;
  2310. }
  2311. input->pos++; /* release hostage */
  2312. } /* zds->hostageByte */
  2313. return 0;
  2314. } /* zds->outEnd == zds->outStart */
  2315. if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
  2316. input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */
  2317. zds->hostageByte=1;
  2318. }
  2319. return 1;
  2320. } /* nextSrcSizeHint==0 */
  2321. nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */
  2322. if (zds->inPos > nextSrcSizeHint) return ERROR(GENERIC); /* should never happen */
  2323. nextSrcSizeHint -= zds->inPos; /* already loaded*/
  2324. return nextSrcSizeHint;
  2325. }
  2326. }
  2327. size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
  2328. {
  2329. return ZSTD_decompressStream(dctx, output, input);
  2330. }
  2331. size_t ZSTD_decompress_generic_simpleArgs (
  2332. ZSTD_DCtx* dctx,
  2333. void* dst, size_t dstCapacity, size_t* dstPos,
  2334. const void* src, size_t srcSize, size_t* srcPos)
  2335. {
  2336. ZSTD_outBuffer output = { dst, dstCapacity, *dstPos };
  2337. ZSTD_inBuffer input = { src, srcSize, *srcPos };
  2338. /* ZSTD_compress_generic() will check validity of dstPos and srcPos */
  2339. size_t const cErr = ZSTD_decompress_generic(dctx, &output, &input);
  2340. *dstPos = output.pos;
  2341. *srcPos = input.pos;
  2342. return cErr;
  2343. }
  2344. void ZSTD_DCtx_reset(ZSTD_DCtx* dctx)
  2345. {
  2346. (void)ZSTD_initDStream(dctx);
  2347. dctx->format = ZSTD_f_zstd1;
  2348. dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
  2349. }