zstd_decompress_block.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. /* zstd_decompress_block :
  11. * this module takes care of decompressing _compressed_ block */
  12. /*-*******************************************************
  13. * Dependencies
  14. *********************************************************/
  15. #include <string.h> /* memcpy, memmove, memset */
  16. #include "compiler.h" /* prefetch */
  17. #include "cpu.h" /* bmi2 */
  18. #include "mem.h" /* low level memory routines */
  19. #define FSE_STATIC_LINKING_ONLY
  20. #include "fse.h"
  21. #define HUF_STATIC_LINKING_ONLY
  22. #include "huf.h"
  23. #include "zstd_internal.h"
  24. #include "zstd_decompress_internal.h" /* ZSTD_DCtx */
  25. #include "zstd_ddict.h" /* ZSTD_DDictDictContent */
  26. #include "zstd_decompress_block.h"
  27. /*_*******************************************************
  28. * Macros
  29. **********************************************************/
  30. /* These two optional macros force the use one way or another of the two
  31. * ZSTD_decompressSequences implementations. You can't force in both directions
  32. * at the same time.
  33. */
  34. #if defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  35. defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  36. #error "Cannot force the use of the short and the long ZSTD_decompressSequences variants!"
  37. #endif
  38. /*_*******************************************************
  39. * Memory operations
  40. **********************************************************/
  41. static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
  42. /*-*************************************************************
  43. * Block decoding
  44. ***************************************************************/
  45. /*! ZSTD_getcBlockSize() :
  46. * Provides the size of compressed block from block header `src` */
  47. size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
  48. blockProperties_t* bpPtr)
  49. {
  50. RETURN_ERROR_IF(srcSize < ZSTD_blockHeaderSize, srcSize_wrong);
  51. { U32 const cBlockHeader = MEM_readLE24(src);
  52. U32 const cSize = cBlockHeader >> 3;
  53. bpPtr->lastBlock = cBlockHeader & 1;
  54. bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3);
  55. bpPtr->origSize = cSize; /* only useful for RLE */
  56. if (bpPtr->blockType == bt_rle) return 1;
  57. RETURN_ERROR_IF(bpPtr->blockType == bt_reserved, corruption_detected);
  58. return cSize;
  59. }
  60. }
  61. /* Hidden declaration for fullbench */
  62. size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
  63. const void* src, size_t srcSize);
  64. /*! ZSTD_decodeLiteralsBlock() :
  65. * @return : nb of bytes read from src (< srcSize )
  66. * note : symbol not declared but exposed for fullbench */
  67. size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
  68. const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
  69. {
  70. DEBUGLOG(5, "ZSTD_decodeLiteralsBlock");
  71. RETURN_ERROR_IF(srcSize < MIN_CBLOCK_SIZE, corruption_detected);
  72. { const BYTE* const istart = (const BYTE*) src;
  73. symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
  74. switch(litEncType)
  75. {
  76. case set_repeat:
  77. DEBUGLOG(5, "set_repeat flag : re-using stats from previous compressed literals block");
  78. RETURN_ERROR_IF(dctx->litEntropy==0, dictionary_corrupted);
  79. /* fall-through */
  80. case set_compressed:
  81. RETURN_ERROR_IF(srcSize < 5, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3");
  82. { size_t lhSize, litSize, litCSize;
  83. U32 singleStream=0;
  84. U32 const lhlCode = (istart[0] >> 2) & 3;
  85. U32 const lhc = MEM_readLE32(istart);
  86. size_t hufSuccess;
  87. switch(lhlCode)
  88. {
  89. case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */
  90. /* 2 - 2 - 10 - 10 */
  91. singleStream = !lhlCode;
  92. lhSize = 3;
  93. litSize = (lhc >> 4) & 0x3FF;
  94. litCSize = (lhc >> 14) & 0x3FF;
  95. break;
  96. case 2:
  97. /* 2 - 2 - 14 - 14 */
  98. lhSize = 4;
  99. litSize = (lhc >> 4) & 0x3FFF;
  100. litCSize = lhc >> 18;
  101. break;
  102. case 3:
  103. /* 2 - 2 - 18 - 18 */
  104. lhSize = 5;
  105. litSize = (lhc >> 4) & 0x3FFFF;
  106. litCSize = (lhc >> 22) + ((size_t)istart[4] << 10);
  107. break;
  108. }
  109. RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected);
  110. RETURN_ERROR_IF(litCSize + lhSize > srcSize, corruption_detected);
  111. /* prefetch huffman table if cold */
  112. if (dctx->ddictIsCold && (litSize > 768 /* heuristic */)) {
  113. PREFETCH_AREA(dctx->HUFptr, sizeof(dctx->entropy.hufTable));
  114. }
  115. if (litEncType==set_repeat) {
  116. if (singleStream) {
  117. hufSuccess = HUF_decompress1X_usingDTable_bmi2(
  118. dctx->litBuffer, litSize, istart+lhSize, litCSize,
  119. dctx->HUFptr, dctx->bmi2);
  120. } else {
  121. hufSuccess = HUF_decompress4X_usingDTable_bmi2(
  122. dctx->litBuffer, litSize, istart+lhSize, litCSize,
  123. dctx->HUFptr, dctx->bmi2);
  124. }
  125. } else {
  126. if (singleStream) {
  127. #if defined(HUF_FORCE_DECOMPRESS_X2)
  128. hufSuccess = HUF_decompress1X_DCtx_wksp(
  129. dctx->entropy.hufTable, dctx->litBuffer, litSize,
  130. istart+lhSize, litCSize, dctx->workspace,
  131. sizeof(dctx->workspace));
  132. #else
  133. hufSuccess = HUF_decompress1X1_DCtx_wksp_bmi2(
  134. dctx->entropy.hufTable, dctx->litBuffer, litSize,
  135. istart+lhSize, litCSize, dctx->workspace,
  136. sizeof(dctx->workspace), dctx->bmi2);
  137. #endif
  138. } else {
  139. hufSuccess = HUF_decompress4X_hufOnly_wksp_bmi2(
  140. dctx->entropy.hufTable, dctx->litBuffer, litSize,
  141. istart+lhSize, litCSize, dctx->workspace,
  142. sizeof(dctx->workspace), dctx->bmi2);
  143. }
  144. }
  145. RETURN_ERROR_IF(HUF_isError(hufSuccess), corruption_detected);
  146. dctx->litPtr = dctx->litBuffer;
  147. dctx->litSize = litSize;
  148. dctx->litEntropy = 1;
  149. if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable;
  150. memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
  151. return litCSize + lhSize;
  152. }
  153. case set_basic:
  154. { size_t litSize, lhSize;
  155. U32 const lhlCode = ((istart[0]) >> 2) & 3;
  156. switch(lhlCode)
  157. {
  158. case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
  159. lhSize = 1;
  160. litSize = istart[0] >> 3;
  161. break;
  162. case 1:
  163. lhSize = 2;
  164. litSize = MEM_readLE16(istart) >> 4;
  165. break;
  166. case 3:
  167. lhSize = 3;
  168. litSize = MEM_readLE24(istart) >> 4;
  169. break;
  170. }
  171. if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
  172. RETURN_ERROR_IF(litSize+lhSize > srcSize, corruption_detected);
  173. memcpy(dctx->litBuffer, istart+lhSize, litSize);
  174. dctx->litPtr = dctx->litBuffer;
  175. dctx->litSize = litSize;
  176. memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
  177. return lhSize+litSize;
  178. }
  179. /* direct reference into compressed stream */
  180. dctx->litPtr = istart+lhSize;
  181. dctx->litSize = litSize;
  182. return lhSize+litSize;
  183. }
  184. case set_rle:
  185. { U32 const lhlCode = ((istart[0]) >> 2) & 3;
  186. size_t litSize, lhSize;
  187. switch(lhlCode)
  188. {
  189. case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
  190. lhSize = 1;
  191. litSize = istart[0] >> 3;
  192. break;
  193. case 1:
  194. lhSize = 2;
  195. litSize = MEM_readLE16(istart) >> 4;
  196. break;
  197. case 3:
  198. lhSize = 3;
  199. litSize = MEM_readLE24(istart) >> 4;
  200. RETURN_ERROR_IF(srcSize<4, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4");
  201. break;
  202. }
  203. RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected);
  204. memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
  205. dctx->litPtr = dctx->litBuffer;
  206. dctx->litSize = litSize;
  207. return lhSize+1;
  208. }
  209. default:
  210. RETURN_ERROR(corruption_detected, "impossible");
  211. }
  212. }
  213. }
  214. /* Default FSE distribution tables.
  215. * These are pre-calculated FSE decoding tables using default distributions as defined in specification :
  216. * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#default-distributions
  217. * They were generated programmatically with following method :
  218. * - start from default distributions, present in /lib/common/zstd_internal.h
  219. * - generate tables normally, using ZSTD_buildFSETable()
  220. * - printout the content of tables
  221. * - pretify output, report below, test with fuzzer to ensure it's correct */
  222. /* Default FSE distribution table for Literal Lengths */
  223. static const ZSTD_seqSymbol LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = {
  224. { 1, 1, 1, LL_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
  225. /* nextState, nbAddBits, nbBits, baseVal */
  226. { 0, 0, 4, 0}, { 16, 0, 4, 0},
  227. { 32, 0, 5, 1}, { 0, 0, 5, 3},
  228. { 0, 0, 5, 4}, { 0, 0, 5, 6},
  229. { 0, 0, 5, 7}, { 0, 0, 5, 9},
  230. { 0, 0, 5, 10}, { 0, 0, 5, 12},
  231. { 0, 0, 6, 14}, { 0, 1, 5, 16},
  232. { 0, 1, 5, 20}, { 0, 1, 5, 22},
  233. { 0, 2, 5, 28}, { 0, 3, 5, 32},
  234. { 0, 4, 5, 48}, { 32, 6, 5, 64},
  235. { 0, 7, 5, 128}, { 0, 8, 6, 256},
  236. { 0, 10, 6, 1024}, { 0, 12, 6, 4096},
  237. { 32, 0, 4, 0}, { 0, 0, 4, 1},
  238. { 0, 0, 5, 2}, { 32, 0, 5, 4},
  239. { 0, 0, 5, 5}, { 32, 0, 5, 7},
  240. { 0, 0, 5, 8}, { 32, 0, 5, 10},
  241. { 0, 0, 5, 11}, { 0, 0, 6, 13},
  242. { 32, 1, 5, 16}, { 0, 1, 5, 18},
  243. { 32, 1, 5, 22}, { 0, 2, 5, 24},
  244. { 32, 3, 5, 32}, { 0, 3, 5, 40},
  245. { 0, 6, 4, 64}, { 16, 6, 4, 64},
  246. { 32, 7, 5, 128}, { 0, 9, 6, 512},
  247. { 0, 11, 6, 2048}, { 48, 0, 4, 0},
  248. { 16, 0, 4, 1}, { 32, 0, 5, 2},
  249. { 32, 0, 5, 3}, { 32, 0, 5, 5},
  250. { 32, 0, 5, 6}, { 32, 0, 5, 8},
  251. { 32, 0, 5, 9}, { 32, 0, 5, 11},
  252. { 32, 0, 5, 12}, { 0, 0, 6, 15},
  253. { 32, 1, 5, 18}, { 32, 1, 5, 20},
  254. { 32, 2, 5, 24}, { 32, 2, 5, 28},
  255. { 32, 3, 5, 40}, { 32, 4, 5, 48},
  256. { 0, 16, 6,65536}, { 0, 15, 6,32768},
  257. { 0, 14, 6,16384}, { 0, 13, 6, 8192},
  258. }; /* LL_defaultDTable */
  259. /* Default FSE distribution table for Offset Codes */
  260. static const ZSTD_seqSymbol OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = {
  261. { 1, 1, 1, OF_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
  262. /* nextState, nbAddBits, nbBits, baseVal */
  263. { 0, 0, 5, 0}, { 0, 6, 4, 61},
  264. { 0, 9, 5, 509}, { 0, 15, 5,32765},
  265. { 0, 21, 5,2097149}, { 0, 3, 5, 5},
  266. { 0, 7, 4, 125}, { 0, 12, 5, 4093},
  267. { 0, 18, 5,262141}, { 0, 23, 5,8388605},
  268. { 0, 5, 5, 29}, { 0, 8, 4, 253},
  269. { 0, 14, 5,16381}, { 0, 20, 5,1048573},
  270. { 0, 2, 5, 1}, { 16, 7, 4, 125},
  271. { 0, 11, 5, 2045}, { 0, 17, 5,131069},
  272. { 0, 22, 5,4194301}, { 0, 4, 5, 13},
  273. { 16, 8, 4, 253}, { 0, 13, 5, 8189},
  274. { 0, 19, 5,524285}, { 0, 1, 5, 1},
  275. { 16, 6, 4, 61}, { 0, 10, 5, 1021},
  276. { 0, 16, 5,65533}, { 0, 28, 5,268435453},
  277. { 0, 27, 5,134217725}, { 0, 26, 5,67108861},
  278. { 0, 25, 5,33554429}, { 0, 24, 5,16777213},
  279. }; /* OF_defaultDTable */
  280. /* Default FSE distribution table for Match Lengths */
  281. static const ZSTD_seqSymbol ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = {
  282. { 1, 1, 1, ML_DEFAULTNORMLOG}, /* header : fastMode, tableLog */
  283. /* nextState, nbAddBits, nbBits, baseVal */
  284. { 0, 0, 6, 3}, { 0, 0, 4, 4},
  285. { 32, 0, 5, 5}, { 0, 0, 5, 6},
  286. { 0, 0, 5, 8}, { 0, 0, 5, 9},
  287. { 0, 0, 5, 11}, { 0, 0, 6, 13},
  288. { 0, 0, 6, 16}, { 0, 0, 6, 19},
  289. { 0, 0, 6, 22}, { 0, 0, 6, 25},
  290. { 0, 0, 6, 28}, { 0, 0, 6, 31},
  291. { 0, 0, 6, 34}, { 0, 1, 6, 37},
  292. { 0, 1, 6, 41}, { 0, 2, 6, 47},
  293. { 0, 3, 6, 59}, { 0, 4, 6, 83},
  294. { 0, 7, 6, 131}, { 0, 9, 6, 515},
  295. { 16, 0, 4, 4}, { 0, 0, 4, 5},
  296. { 32, 0, 5, 6}, { 0, 0, 5, 7},
  297. { 32, 0, 5, 9}, { 0, 0, 5, 10},
  298. { 0, 0, 6, 12}, { 0, 0, 6, 15},
  299. { 0, 0, 6, 18}, { 0, 0, 6, 21},
  300. { 0, 0, 6, 24}, { 0, 0, 6, 27},
  301. { 0, 0, 6, 30}, { 0, 0, 6, 33},
  302. { 0, 1, 6, 35}, { 0, 1, 6, 39},
  303. { 0, 2, 6, 43}, { 0, 3, 6, 51},
  304. { 0, 4, 6, 67}, { 0, 5, 6, 99},
  305. { 0, 8, 6, 259}, { 32, 0, 4, 4},
  306. { 48, 0, 4, 4}, { 16, 0, 4, 5},
  307. { 32, 0, 5, 7}, { 32, 0, 5, 8},
  308. { 32, 0, 5, 10}, { 32, 0, 5, 11},
  309. { 0, 0, 6, 14}, { 0, 0, 6, 17},
  310. { 0, 0, 6, 20}, { 0, 0, 6, 23},
  311. { 0, 0, 6, 26}, { 0, 0, 6, 29},
  312. { 0, 0, 6, 32}, { 0, 16, 6,65539},
  313. { 0, 15, 6,32771}, { 0, 14, 6,16387},
  314. { 0, 13, 6, 8195}, { 0, 12, 6, 4099},
  315. { 0, 11, 6, 2051}, { 0, 10, 6, 1027},
  316. }; /* ML_defaultDTable */
  317. static void ZSTD_buildSeqTable_rle(ZSTD_seqSymbol* dt, U32 baseValue, U32 nbAddBits)
  318. {
  319. void* ptr = dt;
  320. ZSTD_seqSymbol_header* const DTableH = (ZSTD_seqSymbol_header*)ptr;
  321. ZSTD_seqSymbol* const cell = dt + 1;
  322. DTableH->tableLog = 0;
  323. DTableH->fastMode = 0;
  324. cell->nbBits = 0;
  325. cell->nextState = 0;
  326. assert(nbAddBits < 255);
  327. cell->nbAdditionalBits = (BYTE)nbAddBits;
  328. cell->baseValue = baseValue;
  329. }
  330. /* ZSTD_buildFSETable() :
  331. * generate FSE decoding table for one symbol (ll, ml or off)
  332. * cannot fail if input is valid =>
  333. * all inputs are presumed validated at this stage */
  334. void
  335. ZSTD_buildFSETable(ZSTD_seqSymbol* dt,
  336. const short* normalizedCounter, unsigned maxSymbolValue,
  337. const U32* baseValue, const U32* nbAdditionalBits,
  338. unsigned tableLog)
  339. {
  340. ZSTD_seqSymbol* const tableDecode = dt+1;
  341. U16 symbolNext[MaxSeq+1];
  342. U32 const maxSV1 = maxSymbolValue + 1;
  343. U32 const tableSize = 1 << tableLog;
  344. U32 highThreshold = tableSize-1;
  345. /* Sanity Checks */
  346. assert(maxSymbolValue <= MaxSeq);
  347. assert(tableLog <= MaxFSELog);
  348. /* Init, lay down lowprob symbols */
  349. { ZSTD_seqSymbol_header DTableH;
  350. DTableH.tableLog = tableLog;
  351. DTableH.fastMode = 1;
  352. { S16 const largeLimit= (S16)(1 << (tableLog-1));
  353. U32 s;
  354. for (s=0; s<maxSV1; s++) {
  355. if (normalizedCounter[s]==-1) {
  356. tableDecode[highThreshold--].baseValue = s;
  357. symbolNext[s] = 1;
  358. } else {
  359. if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
  360. assert(normalizedCounter[s]>=0);
  361. symbolNext[s] = (U16)normalizedCounter[s];
  362. } } }
  363. memcpy(dt, &DTableH, sizeof(DTableH));
  364. }
  365. /* Spread symbols */
  366. { U32 const tableMask = tableSize-1;
  367. U32 const step = FSE_TABLESTEP(tableSize);
  368. U32 s, position = 0;
  369. for (s=0; s<maxSV1; s++) {
  370. int i;
  371. for (i=0; i<normalizedCounter[s]; i++) {
  372. tableDecode[position].baseValue = s;
  373. position = (position + step) & tableMask;
  374. while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
  375. } }
  376. assert(position == 0); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
  377. }
  378. /* Build Decoding table */
  379. { U32 u;
  380. for (u=0; u<tableSize; u++) {
  381. U32 const symbol = tableDecode[u].baseValue;
  382. U32 const nextState = symbolNext[symbol]++;
  383. tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
  384. tableDecode[u].nextState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
  385. assert(nbAdditionalBits[symbol] < 255);
  386. tableDecode[u].nbAdditionalBits = (BYTE)nbAdditionalBits[symbol];
  387. tableDecode[u].baseValue = baseValue[symbol];
  388. } }
  389. }
  390. /*! ZSTD_buildSeqTable() :
  391. * @return : nb bytes read from src,
  392. * or an error code if it fails */
  393. static size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr,
  394. symbolEncodingType_e type, unsigned max, U32 maxLog,
  395. const void* src, size_t srcSize,
  396. const U32* baseValue, const U32* nbAdditionalBits,
  397. const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable,
  398. int ddictIsCold, int nbSeq)
  399. {
  400. switch(type)
  401. {
  402. case set_rle :
  403. RETURN_ERROR_IF(!srcSize, srcSize_wrong);
  404. RETURN_ERROR_IF((*(const BYTE*)src) > max, corruption_detected);
  405. { U32 const symbol = *(const BYTE*)src;
  406. U32 const baseline = baseValue[symbol];
  407. U32 const nbBits = nbAdditionalBits[symbol];
  408. ZSTD_buildSeqTable_rle(DTableSpace, baseline, nbBits);
  409. }
  410. *DTablePtr = DTableSpace;
  411. return 1;
  412. case set_basic :
  413. *DTablePtr = defaultTable;
  414. return 0;
  415. case set_repeat:
  416. RETURN_ERROR_IF(!flagRepeatTable, corruption_detected);
  417. /* prefetch FSE table if used */
  418. if (ddictIsCold && (nbSeq > 24 /* heuristic */)) {
  419. const void* const pStart = *DTablePtr;
  420. size_t const pSize = sizeof(ZSTD_seqSymbol) * (SEQSYMBOL_TABLE_SIZE(maxLog));
  421. PREFETCH_AREA(pStart, pSize);
  422. }
  423. return 0;
  424. case set_compressed :
  425. { unsigned tableLog;
  426. S16 norm[MaxSeq+1];
  427. size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
  428. RETURN_ERROR_IF(FSE_isError(headerSize), corruption_detected);
  429. RETURN_ERROR_IF(tableLog > maxLog, corruption_detected);
  430. ZSTD_buildFSETable(DTableSpace, norm, max, baseValue, nbAdditionalBits, tableLog);
  431. *DTablePtr = DTableSpace;
  432. return headerSize;
  433. }
  434. default :
  435. assert(0);
  436. RETURN_ERROR(GENERIC, "impossible");
  437. }
  438. }
  439. size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
  440. const void* src, size_t srcSize)
  441. {
  442. const BYTE* const istart = (const BYTE* const)src;
  443. const BYTE* const iend = istart + srcSize;
  444. const BYTE* ip = istart;
  445. int nbSeq;
  446. DEBUGLOG(5, "ZSTD_decodeSeqHeaders");
  447. /* check */
  448. RETURN_ERROR_IF(srcSize < MIN_SEQUENCES_SIZE, srcSize_wrong);
  449. /* SeqHead */
  450. nbSeq = *ip++;
  451. if (!nbSeq) {
  452. *nbSeqPtr=0;
  453. RETURN_ERROR_IF(srcSize != 1, srcSize_wrong);
  454. return 1;
  455. }
  456. if (nbSeq > 0x7F) {
  457. if (nbSeq == 0xFF) {
  458. RETURN_ERROR_IF(ip+2 > iend, srcSize_wrong);
  459. nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
  460. } else {
  461. RETURN_ERROR_IF(ip >= iend, srcSize_wrong);
  462. nbSeq = ((nbSeq-0x80)<<8) + *ip++;
  463. }
  464. }
  465. *nbSeqPtr = nbSeq;
  466. /* FSE table descriptors */
  467. RETURN_ERROR_IF(ip+1 > iend, srcSize_wrong); /* minimum possible size: 1 byte for symbol encoding types */
  468. { symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);
  469. symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);
  470. symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3);
  471. ip++;
  472. /* Build DTables */
  473. { size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr,
  474. LLtype, MaxLL, LLFSELog,
  475. ip, iend-ip,
  476. LL_base, LL_bits,
  477. LL_defaultDTable, dctx->fseEntropy,
  478. dctx->ddictIsCold, nbSeq);
  479. RETURN_ERROR_IF(ZSTD_isError(llhSize), corruption_detected);
  480. ip += llhSize;
  481. }
  482. { size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr,
  483. OFtype, MaxOff, OffFSELog,
  484. ip, iend-ip,
  485. OF_base, OF_bits,
  486. OF_defaultDTable, dctx->fseEntropy,
  487. dctx->ddictIsCold, nbSeq);
  488. RETURN_ERROR_IF(ZSTD_isError(ofhSize), corruption_detected);
  489. ip += ofhSize;
  490. }
  491. { size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr,
  492. MLtype, MaxML, MLFSELog,
  493. ip, iend-ip,
  494. ML_base, ML_bits,
  495. ML_defaultDTable, dctx->fseEntropy,
  496. dctx->ddictIsCold, nbSeq);
  497. RETURN_ERROR_IF(ZSTD_isError(mlhSize), corruption_detected);
  498. ip += mlhSize;
  499. }
  500. }
  501. return ip-istart;
  502. }
  503. typedef struct {
  504. size_t litLength;
  505. size_t matchLength;
  506. size_t offset;
  507. const BYTE* match;
  508. } seq_t;
  509. typedef struct {
  510. size_t state;
  511. const ZSTD_seqSymbol* table;
  512. } ZSTD_fseState;
  513. typedef struct {
  514. BIT_DStream_t DStream;
  515. ZSTD_fseState stateLL;
  516. ZSTD_fseState stateOffb;
  517. ZSTD_fseState stateML;
  518. size_t prevOffset[ZSTD_REP_NUM];
  519. const BYTE* prefixStart;
  520. const BYTE* dictEnd;
  521. size_t pos;
  522. } seqState_t;
  523. /*! ZSTD_overlapCopy8() :
  524. * Copies 8 bytes from ip to op and updates op and ip where ip <= op.
  525. * If the offset is < 8 then the offset is spread to at least 8 bytes.
  526. *
  527. * Precondition: *ip <= *op
  528. * Postcondition: *op - *op >= 8
  529. */
  530. static void ZSTD_overlapCopy8(BYTE** op, BYTE const** ip, size_t offset) {
  531. assert(*ip <= *op);
  532. if (offset < 8) {
  533. /* close range match, overlap */
  534. static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
  535. static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
  536. int const sub2 = dec64table[offset];
  537. (*op)[0] = (*ip)[0];
  538. (*op)[1] = (*ip)[1];
  539. (*op)[2] = (*ip)[2];
  540. (*op)[3] = (*ip)[3];
  541. *ip += dec32table[offset];
  542. ZSTD_copy4(*op+4, *ip);
  543. *ip -= sub2;
  544. } else {
  545. ZSTD_copy8(*op, *ip);
  546. }
  547. *ip += 8;
  548. *op += 8;
  549. assert(*op - *ip >= 8);
  550. }
  551. /*! ZSTD_safecopy() :
  552. * Specialized version of memcpy() that is allowed to READ up to WILDCOPY_OVERLENGTH past the input buffer
  553. * and write up to 16 bytes past oend_w (op >= oend_w is allowed).
  554. * This function is only called in the uncommon case where the sequence is near the end of the block. It
  555. * should be fast for a single long sequence, but can be slow for several short sequences.
  556. *
  557. * @param ovtype controls the overlap detection
  558. * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
  559. * - ZSTD_overlap_src_before_dst: The src and dst may overlap and may be any distance apart.
  560. * The src buffer must be before the dst buffer.
  561. */
  562. static void ZSTD_safecopy(BYTE* op, BYTE* const oend_w, BYTE const* ip, ptrdiff_t length, ZSTD_overlap_e ovtype) {
  563. ptrdiff_t const diff = op - ip;
  564. BYTE* const oend = op + length;
  565. assert((ovtype == ZSTD_no_overlap && (diff <= -8 || diff >= 8 || op >= oend_w)) ||
  566. (ovtype == ZSTD_overlap_src_before_dst && diff >= 0));
  567. if (length < 8) {
  568. /* Handle short lengths. */
  569. while (op < oend) *op++ = *ip++;
  570. return;
  571. }
  572. if (ovtype == ZSTD_overlap_src_before_dst) {
  573. /* Copy 8 bytes and ensure the offset >= 8 when there can be overlap. */
  574. assert(length >= 8);
  575. ZSTD_overlapCopy8(&op, &ip, diff);
  576. assert(op - ip >= 8);
  577. assert(op <= oend);
  578. }
  579. if (oend <= oend_w) {
  580. /* No risk of overwrite. */
  581. ZSTD_wildcopy(op, ip, length, ovtype);
  582. return;
  583. }
  584. if (op <= oend_w) {
  585. /* Wildcopy until we get close to the end. */
  586. assert(oend > oend_w);
  587. ZSTD_wildcopy(op, ip, oend_w - op, ovtype);
  588. ip += oend_w - op;
  589. op = oend_w;
  590. }
  591. /* Handle the leftovers. */
  592. while (op < oend) *op++ = *ip++;
  593. }
  594. /* ZSTD_execSequenceEnd():
  595. * This version handles cases that are near the end of the output buffer. It requires
  596. * more careful checks to make sure there is no overflow. By separating out these hard
  597. * and unlikely cases, we can speed up the common cases.
  598. *
  599. * NOTE: This function needs to be fast for a single long sequence, but doesn't need
  600. * to be optimized for many small sequences, since those fall into ZSTD_execSequence().
  601. */
  602. FORCE_NOINLINE
  603. size_t ZSTD_execSequenceEnd(BYTE* op,
  604. BYTE* const oend, seq_t sequence,
  605. const BYTE** litPtr, const BYTE* const litLimit,
  606. const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)
  607. {
  608. BYTE* const oLitEnd = op + sequence.litLength;
  609. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  610. BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
  611. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  612. const BYTE* match = oLitEnd - sequence.offset;
  613. BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
  614. /* bounds checks */
  615. assert(oLitEnd < oMatchEnd);
  616. RETURN_ERROR_IF(oMatchEnd > oend, dstSize_tooSmall, "last match must fit within dstBuffer");
  617. RETURN_ERROR_IF(iLitEnd > litLimit, corruption_detected, "try to read beyond literal buffer");
  618. /* copy literals */
  619. ZSTD_safecopy(op, oend_w, *litPtr, sequence.litLength, ZSTD_no_overlap);
  620. op = oLitEnd;
  621. *litPtr = iLitEnd;
  622. /* copy Match */
  623. if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {
  624. /* offset beyond prefix */
  625. RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected);
  626. match = dictEnd - (prefixStart-match);
  627. if (match + sequence.matchLength <= dictEnd) {
  628. memmove(oLitEnd, match, sequence.matchLength);
  629. return sequenceLength;
  630. }
  631. /* span extDict & currentPrefixSegment */
  632. { size_t const length1 = dictEnd - match;
  633. memmove(oLitEnd, match, length1);
  634. op = oLitEnd + length1;
  635. sequence.matchLength -= length1;
  636. match = prefixStart;
  637. } }
  638. ZSTD_safecopy(op, oend_w, match, sequence.matchLength, ZSTD_overlap_src_before_dst);
  639. return sequenceLength;
  640. }
  641. HINT_INLINE
  642. size_t ZSTD_execSequence(BYTE* op,
  643. BYTE* const oend, seq_t sequence,
  644. const BYTE** litPtr, const BYTE* const litLimit,
  645. const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)
  646. {
  647. BYTE* const oLitEnd = op + sequence.litLength;
  648. size_t const sequenceLength = sequence.litLength + sequence.matchLength;
  649. BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
  650. BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
  651. const BYTE* const iLitEnd = *litPtr + sequence.litLength;
  652. const BYTE* match = oLitEnd - sequence.offset;
  653. /* Errors and uncommon cases handled here. */
  654. assert(oLitEnd < oMatchEnd);
  655. if (iLitEnd > litLimit || oMatchEnd > oend_w)
  656. return ZSTD_execSequenceEnd(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd);
  657. /* Assumptions (everything else goes into ZSTD_execSequenceEnd()) */
  658. assert(iLitEnd <= litLimit /* Literal length is in bounds */);
  659. assert(oLitEnd <= oend_w /* Can wildcopy literals */);
  660. assert(oMatchEnd <= oend_w /* Can wildcopy matches */);
  661. /* Copy Literals:
  662. * Split out litLength <= 16 since it is nearly always true. +1.6% on gcc-9.
  663. * We likely don't need the full 32-byte wildcopy.
  664. */
  665. assert(WILDCOPY_OVERLENGTH >= 16);
  666. ZSTD_copy16(op, (*litPtr));
  667. if (sequence.litLength > 16) {
  668. ZSTD_wildcopy(op+16, (*litPtr)+16, sequence.litLength-16, ZSTD_no_overlap);
  669. }
  670. op = oLitEnd;
  671. *litPtr = iLitEnd; /* update for next sequence */
  672. /* Copy Match */
  673. if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {
  674. /* offset beyond prefix -> go into extDict */
  675. RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected);
  676. match = dictEnd + (match - prefixStart);
  677. if (match + sequence.matchLength <= dictEnd) {
  678. memmove(oLitEnd, match, sequence.matchLength);
  679. return sequenceLength;
  680. }
  681. /* span extDict & currentPrefixSegment */
  682. { size_t const length1 = dictEnd - match;
  683. memmove(oLitEnd, match, length1);
  684. op = oLitEnd + length1;
  685. sequence.matchLength -= length1;
  686. match = prefixStart;
  687. } }
  688. /* Match within prefix of 1 or more bytes */
  689. assert(op <= oMatchEnd);
  690. assert(oMatchEnd <= oend_w);
  691. assert(match >= prefixStart);
  692. assert(sequence.matchLength >= 1);
  693. /* Nearly all offsets are >= WILDCOPY_VECLEN bytes, which means we can use wildcopy
  694. * without overlap checking.
  695. */
  696. if (sequence.offset >= WILDCOPY_VECLEN) {
  697. /* We bet on a full wildcopy for matches, since we expect matches to be
  698. * longer than literals (in general). In silesia, ~10% of matches are longer
  699. * than 16 bytes.
  700. */
  701. ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength, ZSTD_no_overlap);
  702. return sequenceLength;
  703. }
  704. assert(sequence.offset < WILDCOPY_VECLEN);
  705. /* Copy 8 bytes and spread the offset to be >= 8. */
  706. ZSTD_overlapCopy8(&op, &match, sequence.offset);
  707. /* If the match length is > 8 bytes, then continue with the wildcopy. */
  708. if (sequence.matchLength > 8) {
  709. assert(op < oMatchEnd);
  710. ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8, ZSTD_overlap_src_before_dst);
  711. }
  712. return sequenceLength;
  713. }
  714. static void
  715. ZSTD_initFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, const ZSTD_seqSymbol* dt)
  716. {
  717. const void* ptr = dt;
  718. const ZSTD_seqSymbol_header* const DTableH = (const ZSTD_seqSymbol_header*)ptr;
  719. DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);
  720. DEBUGLOG(6, "ZSTD_initFseState : val=%u using %u bits",
  721. (U32)DStatePtr->state, DTableH->tableLog);
  722. BIT_reloadDStream(bitD);
  723. DStatePtr->table = dt + 1;
  724. }
  725. FORCE_INLINE_TEMPLATE void
  726. ZSTD_updateFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD)
  727. {
  728. ZSTD_seqSymbol const DInfo = DStatePtr->table[DStatePtr->state];
  729. U32 const nbBits = DInfo.nbBits;
  730. size_t const lowBits = BIT_readBits(bitD, nbBits);
  731. DStatePtr->state = DInfo.nextState + lowBits;
  732. }
  733. /* We need to add at most (ZSTD_WINDOWLOG_MAX_32 - 1) bits to read the maximum
  734. * offset bits. But we can only read at most (STREAM_ACCUMULATOR_MIN_32 - 1)
  735. * bits before reloading. This value is the maximum number of bytes we read
  736. * after reloading when we are decoding long offsets.
  737. */
  738. #define LONG_OFFSETS_MAX_EXTRA_BITS_32 \
  739. (ZSTD_WINDOWLOG_MAX_32 > STREAM_ACCUMULATOR_MIN_32 \
  740. ? ZSTD_WINDOWLOG_MAX_32 - STREAM_ACCUMULATOR_MIN_32 \
  741. : 0)
  742. typedef enum { ZSTD_lo_isRegularOffset, ZSTD_lo_isLongOffset=1 } ZSTD_longOffset_e;
  743. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  744. FORCE_INLINE_TEMPLATE seq_t
  745. ZSTD_decodeSequence(seqState_t* seqState, const ZSTD_longOffset_e longOffsets)
  746. {
  747. seq_t seq;
  748. U32 const llBits = seqState->stateLL.table[seqState->stateLL.state].nbAdditionalBits;
  749. U32 const mlBits = seqState->stateML.table[seqState->stateML.state].nbAdditionalBits;
  750. U32 const ofBits = seqState->stateOffb.table[seqState->stateOffb.state].nbAdditionalBits;
  751. U32 const totalBits = llBits+mlBits+ofBits;
  752. U32 const llBase = seqState->stateLL.table[seqState->stateLL.state].baseValue;
  753. U32 const mlBase = seqState->stateML.table[seqState->stateML.state].baseValue;
  754. U32 const ofBase = seqState->stateOffb.table[seqState->stateOffb.state].baseValue;
  755. /* sequence */
  756. { size_t offset;
  757. if (!ofBits)
  758. offset = 0;
  759. else {
  760. ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1);
  761. ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5);
  762. assert(ofBits <= MaxOff);
  763. if (MEM_32bits() && longOffsets && (ofBits >= STREAM_ACCUMULATOR_MIN_32)) {
  764. U32 const extraBits = ofBits - MIN(ofBits, 32 - seqState->DStream.bitsConsumed);
  765. offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);
  766. BIT_reloadDStream(&seqState->DStream);
  767. if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits);
  768. assert(extraBits <= LONG_OFFSETS_MAX_EXTRA_BITS_32); /* to avoid another reload */
  769. } else {
  770. offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits/*>0*/); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */
  771. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);
  772. }
  773. }
  774. if (ofBits <= 1) {
  775. offset += (llBase==0);
  776. if (offset) {
  777. size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
  778. temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
  779. if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
  780. seqState->prevOffset[1] = seqState->prevOffset[0];
  781. seqState->prevOffset[0] = offset = temp;
  782. } else { /* offset == 0 */
  783. offset = seqState->prevOffset[0];
  784. }
  785. } else {
  786. seqState->prevOffset[2] = seqState->prevOffset[1];
  787. seqState->prevOffset[1] = seqState->prevOffset[0];
  788. seqState->prevOffset[0] = offset;
  789. }
  790. seq.offset = offset;
  791. }
  792. seq.matchLength = mlBase
  793. + ((mlBits>0) ? BIT_readBitsFast(&seqState->DStream, mlBits/*>0*/) : 0); /* <= 16 bits */
  794. if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32))
  795. BIT_reloadDStream(&seqState->DStream);
  796. if (MEM_64bits() && (totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog)))
  797. BIT_reloadDStream(&seqState->DStream);
  798. /* Ensure there are enough bits to read the rest of data in 64-bit mode. */
  799. ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64);
  800. seq.litLength = llBase
  801. + ((llBits>0) ? BIT_readBitsFast(&seqState->DStream, llBits/*>0*/) : 0); /* <= 16 bits */
  802. if (MEM_32bits())
  803. BIT_reloadDStream(&seqState->DStream);
  804. DEBUGLOG(6, "seq: litL=%u, matchL=%u, offset=%u",
  805. (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);
  806. /* ANS state update */
  807. ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */
  808. ZSTD_updateFseState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */
  809. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
  810. ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */
  811. return seq;
  812. }
  813. FORCE_INLINE_TEMPLATE size_t
  814. DONT_VECTORIZE
  815. ZSTD_decompressSequences_body( ZSTD_DCtx* dctx,
  816. void* dst, size_t maxDstSize,
  817. const void* seqStart, size_t seqSize, int nbSeq,
  818. const ZSTD_longOffset_e isLongOffset)
  819. {
  820. const BYTE* ip = (const BYTE*)seqStart;
  821. const BYTE* const iend = ip + seqSize;
  822. BYTE* const ostart = (BYTE* const)dst;
  823. BYTE* const oend = ostart + maxDstSize;
  824. BYTE* op = ostart;
  825. const BYTE* litPtr = dctx->litPtr;
  826. const BYTE* const litEnd = litPtr + dctx->litSize;
  827. const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
  828. const BYTE* const vBase = (const BYTE*) (dctx->virtualStart);
  829. const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
  830. DEBUGLOG(5, "ZSTD_decompressSequences_body");
  831. /* Regen sequences */
  832. if (nbSeq) {
  833. seqState_t seqState;
  834. dctx->fseEntropy = 1;
  835. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
  836. RETURN_ERROR_IF(
  837. ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),
  838. corruption_detected);
  839. ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
  840. ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
  841. ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
  842. ZSTD_STATIC_ASSERT(
  843. BIT_DStream_unfinished < BIT_DStream_completed &&
  844. BIT_DStream_endOfBuffer < BIT_DStream_completed &&
  845. BIT_DStream_completed < BIT_DStream_overflow);
  846. for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) {
  847. nbSeq--;
  848. { seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset);
  849. size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd);
  850. DEBUGLOG(6, "regenerated sequence size : %u", (U32)oneSeqSize);
  851. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  852. op += oneSeqSize;
  853. } }
  854. /* check if reached exact end */
  855. DEBUGLOG(5, "ZSTD_decompressSequences_body: after decode loop, remaining nbSeq : %i", nbSeq);
  856. RETURN_ERROR_IF(nbSeq, corruption_detected);
  857. RETURN_ERROR_IF(BIT_reloadDStream(&seqState.DStream) < BIT_DStream_completed, corruption_detected);
  858. /* save reps for next block */
  859. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
  860. }
  861. /* last literal segment */
  862. { size_t const lastLLSize = litEnd - litPtr;
  863. RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall);
  864. memcpy(op, litPtr, lastLLSize);
  865. op += lastLLSize;
  866. }
  867. return op-ostart;
  868. }
  869. static size_t
  870. ZSTD_decompressSequences_default(ZSTD_DCtx* dctx,
  871. void* dst, size_t maxDstSize,
  872. const void* seqStart, size_t seqSize, int nbSeq,
  873. const ZSTD_longOffset_e isLongOffset)
  874. {
  875. return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  876. }
  877. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
  878. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  879. FORCE_INLINE_TEMPLATE seq_t
  880. ZSTD_decodeSequenceLong(seqState_t* seqState, ZSTD_longOffset_e const longOffsets)
  881. {
  882. seq_t seq;
  883. U32 const llBits = seqState->stateLL.table[seqState->stateLL.state].nbAdditionalBits;
  884. U32 const mlBits = seqState->stateML.table[seqState->stateML.state].nbAdditionalBits;
  885. U32 const ofBits = seqState->stateOffb.table[seqState->stateOffb.state].nbAdditionalBits;
  886. U32 const totalBits = llBits+mlBits+ofBits;
  887. U32 const llBase = seqState->stateLL.table[seqState->stateLL.state].baseValue;
  888. U32 const mlBase = seqState->stateML.table[seqState->stateML.state].baseValue;
  889. U32 const ofBase = seqState->stateOffb.table[seqState->stateOffb.state].baseValue;
  890. /* sequence */
  891. { size_t offset;
  892. if (!ofBits)
  893. offset = 0;
  894. else {
  895. ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1);
  896. ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5);
  897. assert(ofBits <= MaxOff);
  898. if (MEM_32bits() && longOffsets) {
  899. U32 const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN_32-1);
  900. offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);
  901. if (MEM_32bits() || extraBits) BIT_reloadDStream(&seqState->DStream);
  902. if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits);
  903. } else {
  904. offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */
  905. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);
  906. }
  907. }
  908. if (ofBits <= 1) {
  909. offset += (llBase==0);
  910. if (offset) {
  911. size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
  912. temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
  913. if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
  914. seqState->prevOffset[1] = seqState->prevOffset[0];
  915. seqState->prevOffset[0] = offset = temp;
  916. } else {
  917. offset = seqState->prevOffset[0];
  918. }
  919. } else {
  920. seqState->prevOffset[2] = seqState->prevOffset[1];
  921. seqState->prevOffset[1] = seqState->prevOffset[0];
  922. seqState->prevOffset[0] = offset;
  923. }
  924. seq.offset = offset;
  925. }
  926. seq.matchLength = mlBase + ((mlBits>0) ? BIT_readBitsFast(&seqState->DStream, mlBits) : 0); /* <= 16 bits */
  927. if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32))
  928. BIT_reloadDStream(&seqState->DStream);
  929. if (MEM_64bits() && (totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog)))
  930. BIT_reloadDStream(&seqState->DStream);
  931. /* Verify that there is enough bits to read the rest of the data in 64-bit mode. */
  932. ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64);
  933. seq.litLength = llBase + ((llBits>0) ? BIT_readBitsFast(&seqState->DStream, llBits) : 0); /* <= 16 bits */
  934. if (MEM_32bits())
  935. BIT_reloadDStream(&seqState->DStream);
  936. { size_t const pos = seqState->pos + seq.litLength;
  937. const BYTE* const matchBase = (seq.offset > pos) ? seqState->dictEnd : seqState->prefixStart;
  938. seq.match = matchBase + pos - seq.offset; /* note : this operation can overflow when seq.offset is really too large, which can only happen when input is corrupted.
  939. * No consequence though : no memory access will occur, overly large offset will be detected in ZSTD_execSequenceLong() */
  940. seqState->pos = pos + seq.matchLength;
  941. }
  942. /* ANS state update */
  943. ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */
  944. ZSTD_updateFseState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */
  945. if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
  946. ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */
  947. return seq;
  948. }
  949. FORCE_INLINE_TEMPLATE size_t
  950. ZSTD_decompressSequencesLong_body(
  951. ZSTD_DCtx* dctx,
  952. void* dst, size_t maxDstSize,
  953. const void* seqStart, size_t seqSize, int nbSeq,
  954. const ZSTD_longOffset_e isLongOffset)
  955. {
  956. const BYTE* ip = (const BYTE*)seqStart;
  957. const BYTE* const iend = ip + seqSize;
  958. BYTE* const ostart = (BYTE* const)dst;
  959. BYTE* const oend = ostart + maxDstSize;
  960. BYTE* op = ostart;
  961. const BYTE* litPtr = dctx->litPtr;
  962. const BYTE* const litEnd = litPtr + dctx->litSize;
  963. const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);
  964. const BYTE* const dictStart = (const BYTE*) (dctx->virtualStart);
  965. const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
  966. /* Regen sequences */
  967. if (nbSeq) {
  968. #define STORED_SEQS 4
  969. #define STORED_SEQS_MASK (STORED_SEQS-1)
  970. #define ADVANCED_SEQS 4
  971. seq_t sequences[STORED_SEQS];
  972. int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS);
  973. seqState_t seqState;
  974. int seqNb;
  975. dctx->fseEntropy = 1;
  976. { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }
  977. seqState.prefixStart = prefixStart;
  978. seqState.pos = (size_t)(op-prefixStart);
  979. seqState.dictEnd = dictEnd;
  980. assert(iend >= ip);
  981. RETURN_ERROR_IF(
  982. ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),
  983. corruption_detected);
  984. ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
  985. ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
  986. ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
  987. /* prepare in advance */
  988. for (seqNb=0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && (seqNb<seqAdvance); seqNb++) {
  989. sequences[seqNb] = ZSTD_decodeSequenceLong(&seqState, isLongOffset);
  990. PREFETCH_L1(sequences[seqNb].match); PREFETCH_L1(sequences[seqNb].match + sequences[seqNb].matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */
  991. }
  992. RETURN_ERROR_IF(seqNb<seqAdvance, corruption_detected);
  993. /* decode and decompress */
  994. for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (seqNb<nbSeq) ; seqNb++) {
  995. seq_t const sequence = ZSTD_decodeSequenceLong(&seqState, isLongOffset);
  996. size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[(seqNb-ADVANCED_SEQS) & STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);
  997. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  998. PREFETCH_L1(sequence.match); PREFETCH_L1(sequence.match + sequence.matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */
  999. sequences[seqNb & STORED_SEQS_MASK] = sequence;
  1000. op += oneSeqSize;
  1001. }
  1002. RETURN_ERROR_IF(seqNb<nbSeq, corruption_detected);
  1003. /* finish queue */
  1004. seqNb -= seqAdvance;
  1005. for ( ; seqNb<nbSeq ; seqNb++) {
  1006. size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[seqNb&STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);
  1007. if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
  1008. op += oneSeqSize;
  1009. }
  1010. /* save reps for next block */
  1011. { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }
  1012. }
  1013. /* last literal segment */
  1014. { size_t const lastLLSize = litEnd - litPtr;
  1015. RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall);
  1016. memcpy(op, litPtr, lastLLSize);
  1017. op += lastLLSize;
  1018. }
  1019. return op-ostart;
  1020. }
  1021. static size_t
  1022. ZSTD_decompressSequencesLong_default(ZSTD_DCtx* dctx,
  1023. void* dst, size_t maxDstSize,
  1024. const void* seqStart, size_t seqSize, int nbSeq,
  1025. const ZSTD_longOffset_e isLongOffset)
  1026. {
  1027. return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  1028. }
  1029. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
  1030. #if DYNAMIC_BMI2
  1031. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  1032. static TARGET_ATTRIBUTE("bmi2") size_t
  1033. DONT_VECTORIZE
  1034. ZSTD_decompressSequences_bmi2(ZSTD_DCtx* dctx,
  1035. void* dst, size_t maxDstSize,
  1036. const void* seqStart, size_t seqSize, int nbSeq,
  1037. const ZSTD_longOffset_e isLongOffset)
  1038. {
  1039. return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  1040. }
  1041. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
  1042. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  1043. static TARGET_ATTRIBUTE("bmi2") size_t
  1044. ZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx* dctx,
  1045. void* dst, size_t maxDstSize,
  1046. const void* seqStart, size_t seqSize, int nbSeq,
  1047. const ZSTD_longOffset_e isLongOffset)
  1048. {
  1049. return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  1050. }
  1051. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
  1052. #endif /* DYNAMIC_BMI2 */
  1053. typedef size_t (*ZSTD_decompressSequences_t)(
  1054. ZSTD_DCtx* dctx,
  1055. void* dst, size_t maxDstSize,
  1056. const void* seqStart, size_t seqSize, int nbSeq,
  1057. const ZSTD_longOffset_e isLongOffset);
  1058. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  1059. static size_t
  1060. ZSTD_decompressSequences(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize,
  1061. const void* seqStart, size_t seqSize, int nbSeq,
  1062. const ZSTD_longOffset_e isLongOffset)
  1063. {
  1064. DEBUGLOG(5, "ZSTD_decompressSequences");
  1065. #if DYNAMIC_BMI2
  1066. if (dctx->bmi2) {
  1067. return ZSTD_decompressSequences_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  1068. }
  1069. #endif
  1070. return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  1071. }
  1072. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */
  1073. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  1074. /* ZSTD_decompressSequencesLong() :
  1075. * decompression function triggered when a minimum share of offsets is considered "long",
  1076. * aka out of cache.
  1077. * note : "long" definition seems overloaded here, sometimes meaning "wider than bitstream register", and sometimes meaning "farther than memory cache distance".
  1078. * This function will try to mitigate main memory latency through the use of prefetching */
  1079. static size_t
  1080. ZSTD_decompressSequencesLong(ZSTD_DCtx* dctx,
  1081. void* dst, size_t maxDstSize,
  1082. const void* seqStart, size_t seqSize, int nbSeq,
  1083. const ZSTD_longOffset_e isLongOffset)
  1084. {
  1085. DEBUGLOG(5, "ZSTD_decompressSequencesLong");
  1086. #if DYNAMIC_BMI2
  1087. if (dctx->bmi2) {
  1088. return ZSTD_decompressSequencesLong_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  1089. }
  1090. #endif
  1091. return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset);
  1092. }
  1093. #endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */
  1094. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  1095. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  1096. /* ZSTD_getLongOffsetsShare() :
  1097. * condition : offTable must be valid
  1098. * @return : "share" of long offsets (arbitrarily defined as > (1<<23))
  1099. * compared to maximum possible of (1<<OffFSELog) */
  1100. static unsigned
  1101. ZSTD_getLongOffsetsShare(const ZSTD_seqSymbol* offTable)
  1102. {
  1103. const void* ptr = offTable;
  1104. U32 const tableLog = ((const ZSTD_seqSymbol_header*)ptr)[0].tableLog;
  1105. const ZSTD_seqSymbol* table = offTable + 1;
  1106. U32 const max = 1 << tableLog;
  1107. U32 u, total = 0;
  1108. DEBUGLOG(5, "ZSTD_getLongOffsetsShare: (tableLog=%u)", tableLog);
  1109. assert(max <= (1 << OffFSELog)); /* max not too large */
  1110. for (u=0; u<max; u++) {
  1111. if (table[u].nbAdditionalBits > 22) total += 1;
  1112. }
  1113. assert(tableLog <= OffFSELog);
  1114. total <<= (OffFSELog - tableLog); /* scale to OffFSELog */
  1115. return total;
  1116. }
  1117. #endif
  1118. size_t
  1119. ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
  1120. void* dst, size_t dstCapacity,
  1121. const void* src, size_t srcSize, const int frame)
  1122. { /* blockType == blockCompressed */
  1123. const BYTE* ip = (const BYTE*)src;
  1124. /* isLongOffset must be true if there are long offsets.
  1125. * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN.
  1126. * We don't expect that to be the case in 64-bit mode.
  1127. * In block mode, window size is not known, so we have to be conservative.
  1128. * (note: but it could be evaluated from current-lowLimit)
  1129. */
  1130. ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || (dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN))));
  1131. DEBUGLOG(5, "ZSTD_decompressBlock_internal (size : %u)", (U32)srcSize);
  1132. RETURN_ERROR_IF(srcSize >= ZSTD_BLOCKSIZE_MAX, srcSize_wrong);
  1133. /* Decode literals section */
  1134. { size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
  1135. DEBUGLOG(5, "ZSTD_decodeLiteralsBlock : %u", (U32)litCSize);
  1136. if (ZSTD_isError(litCSize)) return litCSize;
  1137. ip += litCSize;
  1138. srcSize -= litCSize;
  1139. }
  1140. /* Build Decoding Tables */
  1141. {
  1142. /* These macros control at build-time which decompressor implementation
  1143. * we use. If neither is defined, we do some inspection and dispatch at
  1144. * runtime.
  1145. */
  1146. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  1147. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  1148. int usePrefetchDecoder = dctx->ddictIsCold;
  1149. #endif
  1150. int nbSeq;
  1151. size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, srcSize);
  1152. if (ZSTD_isError(seqHSize)) return seqHSize;
  1153. ip += seqHSize;
  1154. srcSize -= seqHSize;
  1155. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  1156. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  1157. if ( !usePrefetchDecoder
  1158. && (!frame || (dctx->fParams.windowSize > (1<<24)))
  1159. && (nbSeq>ADVANCED_SEQS) ) { /* could probably use a larger nbSeq limit */
  1160. U32 const shareLongOffsets = ZSTD_getLongOffsetsShare(dctx->OFTptr);
  1161. U32 const minShare = MEM_64bits() ? 7 : 20; /* heuristic values, correspond to 2.73% and 7.81% */
  1162. usePrefetchDecoder = (shareLongOffsets >= minShare);
  1163. }
  1164. #endif
  1165. dctx->ddictIsCold = 0;
  1166. #if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \
  1167. !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)
  1168. if (usePrefetchDecoder)
  1169. #endif
  1170. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  1171. return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset);
  1172. #endif
  1173. #ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  1174. /* else */
  1175. return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset);
  1176. #endif
  1177. }
  1178. }
  1179. size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
  1180. void* dst, size_t dstCapacity,
  1181. const void* src, size_t srcSize)
  1182. {
  1183. size_t dSize;
  1184. ZSTD_checkContinuity(dctx, dst);
  1185. dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 0);
  1186. dctx->previousDstEnd = (char*)dst + dSize;
  1187. return dSize;
  1188. }