huf_decompress.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /* ******************************************************************
  2. Huffman decoder, part of New Generation Entropy library
  3. Copyright (C) 2013-2016, Yann Collet.
  4. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other materials provided with the
  13. distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. You can contact the author at :
  26. - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
  27. - Public forum : https://groups.google.com/forum/#!forum/lz4c
  28. ****************************************************************** */
  29. /* **************************************************************
  30. * Dependencies
  31. ****************************************************************/
  32. #include <string.h> /* memcpy, memset */
  33. #include "bitstream.h" /* BIT_* */
  34. #include "compiler.h"
  35. #include "fse.h" /* header compression */
  36. #define HUF_STATIC_LINKING_ONLY
  37. #include "huf.h"
  38. #include "error_private.h"
  39. /* **************************************************************
  40. * Error Management
  41. ****************************************************************/
  42. #define HUF_isError ERR_isError
  43. #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  44. /* **************************************************************
  45. * Byte alignment for workSpace management
  46. ****************************************************************/
  47. #define HUF_ALIGN(x, a) HUF_ALIGN_MASK((x), (a) - 1)
  48. #define HUF_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
  49. /*-***************************/
  50. /* generic DTableDesc */
  51. /*-***************************/
  52. typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc;
  53. static DTableDesc HUF_getDTableDesc(const HUF_DTable* table)
  54. {
  55. DTableDesc dtd;
  56. memcpy(&dtd, table, sizeof(dtd));
  57. return dtd;
  58. }
  59. /*-***************************/
  60. /* single-symbol decoding */
  61. /*-***************************/
  62. typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */
  63. size_t HUF_readDTableX2_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize)
  64. {
  65. U32 tableLog = 0;
  66. U32 nbSymbols = 0;
  67. size_t iSize;
  68. void* const dtPtr = DTable + 1;
  69. HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;
  70. U32* rankVal;
  71. BYTE* huffWeight;
  72. size_t spaceUsed32 = 0;
  73. rankVal = (U32 *)workSpace + spaceUsed32;
  74. spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
  75. huffWeight = (BYTE *)((U32 *)workSpace + spaceUsed32);
  76. spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
  77. if ((spaceUsed32 << 2) > wkspSize)
  78. return ERROR(tableLog_tooLarge);
  79. workSpace = (U32 *)workSpace + spaceUsed32;
  80. wkspSize -= (spaceUsed32 << 2);
  81. HUF_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
  82. /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
  83. iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
  84. if (HUF_isError(iSize)) return iSize;
  85. /* Table header */
  86. { DTableDesc dtd = HUF_getDTableDesc(DTable);
  87. if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */
  88. dtd.tableType = 0;
  89. dtd.tableLog = (BYTE)tableLog;
  90. memcpy(DTable, &dtd, sizeof(dtd));
  91. }
  92. /* Calculate starting value for each rank */
  93. { U32 n, nextRankStart = 0;
  94. for (n=1; n<tableLog+1; n++) {
  95. U32 const current = nextRankStart;
  96. nextRankStart += (rankVal[n] << (n-1));
  97. rankVal[n] = current;
  98. } }
  99. /* fill DTable */
  100. { U32 n;
  101. for (n=0; n<nbSymbols; n++) {
  102. U32 const w = huffWeight[n];
  103. U32 const length = (1 << w) >> 1;
  104. U32 u;
  105. HUF_DEltX2 D;
  106. D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
  107. for (u = rankVal[w]; u < rankVal[w] + length; u++)
  108. dt[u] = D;
  109. rankVal[w] += length;
  110. } }
  111. return iSize;
  112. }
  113. size_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize)
  114. {
  115. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  116. return HUF_readDTableX2_wksp(DTable, src, srcSize,
  117. workSpace, sizeof(workSpace));
  118. }
  119. static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog)
  120. {
  121. size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
  122. BYTE const c = dt[val].byte;
  123. BIT_skipBits(Dstream, dt[val].nbBits);
  124. return c;
  125. }
  126. #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
  127. *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
  128. #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
  129. if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
  130. HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
  131. #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
  132. if (MEM_64bits()) \
  133. HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
  134. HINT_INLINE size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog)
  135. {
  136. BYTE* const pStart = p;
  137. /* up to 4 symbols at a time */
  138. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4)) {
  139. HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
  140. HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
  141. HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
  142. HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
  143. }
  144. /* closer to the end */
  145. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd))
  146. HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
  147. /* no more data to retrieve from bitstream, hence no need to reload */
  148. while (p < pEnd)
  149. HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
  150. return pEnd-pStart;
  151. }
  152. static size_t HUF_decompress1X2_usingDTable_internal(
  153. void* dst, size_t dstSize,
  154. const void* cSrc, size_t cSrcSize,
  155. const HUF_DTable* DTable)
  156. {
  157. BYTE* op = (BYTE*)dst;
  158. BYTE* const oend = op + dstSize;
  159. const void* dtPtr = DTable + 1;
  160. const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
  161. BIT_DStream_t bitD;
  162. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  163. U32 const dtLog = dtd.tableLog;
  164. { size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
  165. if (HUF_isError(errorCode)) return errorCode; }
  166. HUF_decodeStreamX2(op, &bitD, oend, dt, dtLog);
  167. /* check */
  168. if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
  169. return dstSize;
  170. }
  171. size_t HUF_decompress1X2_usingDTable(
  172. void* dst, size_t dstSize,
  173. const void* cSrc, size_t cSrcSize,
  174. const HUF_DTable* DTable)
  175. {
  176. DTableDesc dtd = HUF_getDTableDesc(DTable);
  177. if (dtd.tableType != 0) return ERROR(GENERIC);
  178. return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
  179. }
  180. size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
  181. const void* cSrc, size_t cSrcSize,
  182. void* workSpace, size_t wkspSize)
  183. {
  184. const BYTE* ip = (const BYTE*) cSrc;
  185. size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize);
  186. if (HUF_isError(hSize)) return hSize;
  187. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  188. ip += hSize; cSrcSize -= hSize;
  189. return HUF_decompress1X2_usingDTable_internal (dst, dstSize, ip, cSrcSize, DCtx);
  190. }
  191. size_t HUF_decompress1X2_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
  192. const void* cSrc, size_t cSrcSize)
  193. {
  194. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  195. return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
  196. workSpace, sizeof(workSpace));
  197. }
  198. size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  199. {
  200. HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
  201. return HUF_decompress1X2_DCtx (DTable, dst, dstSize, cSrc, cSrcSize);
  202. }
  203. static size_t HUF_decompress4X2_usingDTable_internal(
  204. void* dst, size_t dstSize,
  205. const void* cSrc, size_t cSrcSize,
  206. const HUF_DTable* DTable)
  207. {
  208. /* Check */
  209. if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
  210. { const BYTE* const istart = (const BYTE*) cSrc;
  211. BYTE* const ostart = (BYTE*) dst;
  212. BYTE* const oend = ostart + dstSize;
  213. const void* const dtPtr = DTable + 1;
  214. const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;
  215. /* Init */
  216. BIT_DStream_t bitD1;
  217. BIT_DStream_t bitD2;
  218. BIT_DStream_t bitD3;
  219. BIT_DStream_t bitD4;
  220. size_t const length1 = MEM_readLE16(istart);
  221. size_t const length2 = MEM_readLE16(istart+2);
  222. size_t const length3 = MEM_readLE16(istart+4);
  223. size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
  224. const BYTE* const istart1 = istart + 6; /* jumpTable */
  225. const BYTE* const istart2 = istart1 + length1;
  226. const BYTE* const istart3 = istart2 + length2;
  227. const BYTE* const istart4 = istart3 + length3;
  228. const size_t segmentSize = (dstSize+3) / 4;
  229. BYTE* const opStart2 = ostart + segmentSize;
  230. BYTE* const opStart3 = opStart2 + segmentSize;
  231. BYTE* const opStart4 = opStart3 + segmentSize;
  232. BYTE* op1 = ostart;
  233. BYTE* op2 = opStart2;
  234. BYTE* op3 = opStart3;
  235. BYTE* op4 = opStart4;
  236. U32 endSignal;
  237. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  238. U32 const dtLog = dtd.tableLog;
  239. if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
  240. { size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1);
  241. if (HUF_isError(errorCode)) return errorCode; }
  242. { size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2);
  243. if (HUF_isError(errorCode)) return errorCode; }
  244. { size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3);
  245. if (HUF_isError(errorCode)) return errorCode; }
  246. { size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4);
  247. if (HUF_isError(errorCode)) return errorCode; }
  248. /* 16-32 symbols per loop (4-8 symbols per stream) */
  249. endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
  250. for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) {
  251. HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
  252. HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
  253. HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
  254. HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
  255. HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
  256. HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
  257. HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
  258. HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
  259. HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
  260. HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
  261. HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
  262. HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
  263. HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
  264. HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
  265. HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
  266. HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
  267. endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
  268. }
  269. /* check corruption */
  270. if (op1 > opStart2) return ERROR(corruption_detected);
  271. if (op2 > opStart3) return ERROR(corruption_detected);
  272. if (op3 > opStart4) return ERROR(corruption_detected);
  273. /* note : op4 supposed already verified within main loop */
  274. /* finish bitStreams one by one */
  275. HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
  276. HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
  277. HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
  278. HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
  279. /* check */
  280. endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
  281. if (!endSignal) return ERROR(corruption_detected);
  282. /* decoded size */
  283. return dstSize;
  284. }
  285. }
  286. size_t HUF_decompress4X2_usingDTable(
  287. void* dst, size_t dstSize,
  288. const void* cSrc, size_t cSrcSize,
  289. const HUF_DTable* DTable)
  290. {
  291. DTableDesc dtd = HUF_getDTableDesc(DTable);
  292. if (dtd.tableType != 0) return ERROR(GENERIC);
  293. return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
  294. }
  295. size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
  296. const void* cSrc, size_t cSrcSize,
  297. void* workSpace, size_t wkspSize)
  298. {
  299. const BYTE* ip = (const BYTE*) cSrc;
  300. size_t const hSize = HUF_readDTableX2_wksp (dctx, cSrc, cSrcSize,
  301. workSpace, wkspSize);
  302. if (HUF_isError(hSize)) return hSize;
  303. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  304. ip += hSize; cSrcSize -= hSize;
  305. return HUF_decompress4X2_usingDTable_internal (dst, dstSize, ip, cSrcSize, dctx);
  306. }
  307. size_t HUF_decompress4X2_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  308. {
  309. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  310. return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  311. workSpace, sizeof(workSpace));
  312. }
  313. size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  314. {
  315. HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
  316. return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
  317. }
  318. /* *************************/
  319. /* double-symbols decoding */
  320. /* *************************/
  321. typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */
  322. typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
  323. /* HUF_fillDTableX4Level2() :
  324. * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */
  325. static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed,
  326. const U32* rankValOrigin, const int minWeight,
  327. const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
  328. U32 nbBitsBaseline, U16 baseSeq)
  329. {
  330. HUF_DEltX4 DElt;
  331. U32 rankVal[HUF_TABLELOG_MAX + 1];
  332. /* get pre-calculated rankVal */
  333. memcpy(rankVal, rankValOrigin, sizeof(rankVal));
  334. /* fill skipped values */
  335. if (minWeight>1) {
  336. U32 i, skipSize = rankVal[minWeight];
  337. MEM_writeLE16(&(DElt.sequence), baseSeq);
  338. DElt.nbBits = (BYTE)(consumed);
  339. DElt.length = 1;
  340. for (i = 0; i < skipSize; i++)
  341. DTable[i] = DElt;
  342. }
  343. /* fill DTable */
  344. { U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
  345. const U32 symbol = sortedSymbols[s].symbol;
  346. const U32 weight = sortedSymbols[s].weight;
  347. const U32 nbBits = nbBitsBaseline - weight;
  348. const U32 length = 1 << (sizeLog-nbBits);
  349. const U32 start = rankVal[weight];
  350. U32 i = start;
  351. const U32 end = start + length;
  352. MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
  353. DElt.nbBits = (BYTE)(nbBits + consumed);
  354. DElt.length = 2;
  355. do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
  356. rankVal[weight] += length;
  357. } }
  358. }
  359. typedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];
  360. typedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX];
  361. static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
  362. const sortedSymbol_t* sortedList, const U32 sortedListSize,
  363. const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
  364. const U32 nbBitsBaseline)
  365. {
  366. U32 rankVal[HUF_TABLELOG_MAX + 1];
  367. const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
  368. const U32 minBits = nbBitsBaseline - maxWeight;
  369. U32 s;
  370. memcpy(rankVal, rankValOrigin, sizeof(rankVal));
  371. /* fill DTable */
  372. for (s=0; s<sortedListSize; s++) {
  373. const U16 symbol = sortedList[s].symbol;
  374. const U32 weight = sortedList[s].weight;
  375. const U32 nbBits = nbBitsBaseline - weight;
  376. const U32 start = rankVal[weight];
  377. const U32 length = 1 << (targetLog-nbBits);
  378. if (targetLog-nbBits >= minBits) { /* enough room for a second symbol */
  379. U32 sortedRank;
  380. int minWeight = nbBits + scaleLog;
  381. if (minWeight < 1) minWeight = 1;
  382. sortedRank = rankStart[minWeight];
  383. HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
  384. rankValOrigin[nbBits], minWeight,
  385. sortedList+sortedRank, sortedListSize-sortedRank,
  386. nbBitsBaseline, symbol);
  387. } else {
  388. HUF_DEltX4 DElt;
  389. MEM_writeLE16(&(DElt.sequence), symbol);
  390. DElt.nbBits = (BYTE)(nbBits);
  391. DElt.length = 1;
  392. { U32 const end = start + length;
  393. U32 u;
  394. for (u = start; u < end; u++) DTable[u] = DElt;
  395. } }
  396. rankVal[weight] += length;
  397. }
  398. }
  399. size_t HUF_readDTableX4_wksp(HUF_DTable* DTable, const void* src,
  400. size_t srcSize, void* workSpace,
  401. size_t wkspSize)
  402. {
  403. U32 tableLog, maxW, sizeOfSort, nbSymbols;
  404. DTableDesc dtd = HUF_getDTableDesc(DTable);
  405. U32 const maxTableLog = dtd.maxTableLog;
  406. size_t iSize;
  407. void* dtPtr = DTable+1; /* force compiler to avoid strict-aliasing */
  408. HUF_DEltX4* const dt = (HUF_DEltX4*)dtPtr;
  409. U32 *rankStart;
  410. rankValCol_t* rankVal;
  411. U32* rankStats;
  412. U32* rankStart0;
  413. sortedSymbol_t* sortedSymbol;
  414. BYTE* weightList;
  415. size_t spaceUsed32 = 0;
  416. rankVal = (rankValCol_t *)((U32 *)workSpace + spaceUsed32);
  417. spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;
  418. rankStats = (U32 *)workSpace + spaceUsed32;
  419. spaceUsed32 += HUF_TABLELOG_MAX + 1;
  420. rankStart0 = (U32 *)workSpace + spaceUsed32;
  421. spaceUsed32 += HUF_TABLELOG_MAX + 2;
  422. sortedSymbol = (sortedSymbol_t *)workSpace + (spaceUsed32 * sizeof(U32)) / sizeof(sortedSymbol_t);
  423. spaceUsed32 += HUF_ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;
  424. weightList = (BYTE *)((U32 *)workSpace + spaceUsed32);
  425. spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
  426. if ((spaceUsed32 << 2) > wkspSize)
  427. return ERROR(tableLog_tooLarge);
  428. workSpace = (U32 *)workSpace + spaceUsed32;
  429. wkspSize -= (spaceUsed32 << 2);
  430. rankStart = rankStart0 + 1;
  431. memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));
  432. HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */
  433. if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
  434. /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
  435. iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
  436. if (HUF_isError(iSize)) return iSize;
  437. /* check result */
  438. if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
  439. /* find maxWeight */
  440. for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
  441. /* Get start index of each weight */
  442. { U32 w, nextRankStart = 0;
  443. for (w=1; w<maxW+1; w++) {
  444. U32 current = nextRankStart;
  445. nextRankStart += rankStats[w];
  446. rankStart[w] = current;
  447. }
  448. rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
  449. sizeOfSort = nextRankStart;
  450. }
  451. /* sort symbols by weight */
  452. { U32 s;
  453. for (s=0; s<nbSymbols; s++) {
  454. U32 const w = weightList[s];
  455. U32 const r = rankStart[w]++;
  456. sortedSymbol[r].symbol = (BYTE)s;
  457. sortedSymbol[r].weight = (BYTE)w;
  458. }
  459. rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
  460. }
  461. /* Build rankVal */
  462. { U32* const rankVal0 = rankVal[0];
  463. { int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */
  464. U32 nextRankVal = 0;
  465. U32 w;
  466. for (w=1; w<maxW+1; w++) {
  467. U32 current = nextRankVal;
  468. nextRankVal += rankStats[w] << (w+rescale);
  469. rankVal0[w] = current;
  470. } }
  471. { U32 const minBits = tableLog+1 - maxW;
  472. U32 consumed;
  473. for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
  474. U32* const rankValPtr = rankVal[consumed];
  475. U32 w;
  476. for (w = 1; w < maxW+1; w++) {
  477. rankValPtr[w] = rankVal0[w] >> consumed;
  478. } } } }
  479. HUF_fillDTableX4(dt, maxTableLog,
  480. sortedSymbol, sizeOfSort,
  481. rankStart0, rankVal, maxW,
  482. tableLog+1);
  483. dtd.tableLog = (BYTE)maxTableLog;
  484. dtd.tableType = 1;
  485. memcpy(DTable, &dtd, sizeof(dtd));
  486. return iSize;
  487. }
  488. size_t HUF_readDTableX4(HUF_DTable* DTable, const void* src, size_t srcSize)
  489. {
  490. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  491. return HUF_readDTableX4_wksp(DTable, src, srcSize,
  492. workSpace, sizeof(workSpace));
  493. }
  494. static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
  495. {
  496. size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
  497. memcpy(op, dt+val, 2);
  498. BIT_skipBits(DStream, dt[val].nbBits);
  499. return dt[val].length;
  500. }
  501. static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
  502. {
  503. size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
  504. memcpy(op, dt+val, 1);
  505. if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
  506. else {
  507. if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {
  508. BIT_skipBits(DStream, dt[val].nbBits);
  509. if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
  510. /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
  511. DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);
  512. } }
  513. return 1;
  514. }
  515. #define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
  516. ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
  517. #define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
  518. if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \
  519. ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
  520. #define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
  521. if (MEM_64bits()) \
  522. ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
  523. HINT_INLINE size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog)
  524. {
  525. BYTE* const pStart = p;
  526. /* up to 8 symbols at a time */
  527. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) {
  528. HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
  529. HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
  530. HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
  531. HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
  532. }
  533. /* closer to end : up to 2 symbols at a time */
  534. while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2))
  535. HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
  536. while (p <= pEnd-2)
  537. HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
  538. if (p < pEnd)
  539. p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
  540. return p-pStart;
  541. }
  542. static size_t HUF_decompress1X4_usingDTable_internal(
  543. void* dst, size_t dstSize,
  544. const void* cSrc, size_t cSrcSize,
  545. const HUF_DTable* DTable)
  546. {
  547. BIT_DStream_t bitD;
  548. /* Init */
  549. { size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
  550. if (HUF_isError(errorCode)) return errorCode;
  551. }
  552. /* decode */
  553. { BYTE* const ostart = (BYTE*) dst;
  554. BYTE* const oend = ostart + dstSize;
  555. const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */
  556. const HUF_DEltX4* const dt = (const HUF_DEltX4*)dtPtr;
  557. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  558. HUF_decodeStreamX4(ostart, &bitD, oend, dt, dtd.tableLog);
  559. }
  560. /* check */
  561. if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
  562. /* decoded size */
  563. return dstSize;
  564. }
  565. size_t HUF_decompress1X4_usingDTable(
  566. void* dst, size_t dstSize,
  567. const void* cSrc, size_t cSrcSize,
  568. const HUF_DTable* DTable)
  569. {
  570. DTableDesc dtd = HUF_getDTableDesc(DTable);
  571. if (dtd.tableType != 1) return ERROR(GENERIC);
  572. return HUF_decompress1X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
  573. }
  574. size_t HUF_decompress1X4_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
  575. const void* cSrc, size_t cSrcSize,
  576. void* workSpace, size_t wkspSize)
  577. {
  578. const BYTE* ip = (const BYTE*) cSrc;
  579. size_t const hSize = HUF_readDTableX4_wksp(DCtx, cSrc, cSrcSize,
  580. workSpace, wkspSize);
  581. if (HUF_isError(hSize)) return hSize;
  582. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  583. ip += hSize; cSrcSize -= hSize;
  584. return HUF_decompress1X4_usingDTable_internal (dst, dstSize, ip, cSrcSize, DCtx);
  585. }
  586. size_t HUF_decompress1X4_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
  587. const void* cSrc, size_t cSrcSize)
  588. {
  589. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  590. return HUF_decompress1X4_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
  591. workSpace, sizeof(workSpace));
  592. }
  593. size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  594. {
  595. HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_TABLELOG_MAX);
  596. return HUF_decompress1X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
  597. }
  598. static size_t HUF_decompress4X4_usingDTable_internal(
  599. void* dst, size_t dstSize,
  600. const void* cSrc, size_t cSrcSize,
  601. const HUF_DTable* DTable)
  602. {
  603. if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
  604. { const BYTE* const istart = (const BYTE*) cSrc;
  605. BYTE* const ostart = (BYTE*) dst;
  606. BYTE* const oend = ostart + dstSize;
  607. const void* const dtPtr = DTable+1;
  608. const HUF_DEltX4* const dt = (const HUF_DEltX4*)dtPtr;
  609. /* Init */
  610. BIT_DStream_t bitD1;
  611. BIT_DStream_t bitD2;
  612. BIT_DStream_t bitD3;
  613. BIT_DStream_t bitD4;
  614. size_t const length1 = MEM_readLE16(istart);
  615. size_t const length2 = MEM_readLE16(istart+2);
  616. size_t const length3 = MEM_readLE16(istart+4);
  617. size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
  618. const BYTE* const istart1 = istart + 6; /* jumpTable */
  619. const BYTE* const istart2 = istart1 + length1;
  620. const BYTE* const istart3 = istart2 + length2;
  621. const BYTE* const istart4 = istart3 + length3;
  622. size_t const segmentSize = (dstSize+3) / 4;
  623. BYTE* const opStart2 = ostart + segmentSize;
  624. BYTE* const opStart3 = opStart2 + segmentSize;
  625. BYTE* const opStart4 = opStart3 + segmentSize;
  626. BYTE* op1 = ostart;
  627. BYTE* op2 = opStart2;
  628. BYTE* op3 = opStart3;
  629. BYTE* op4 = opStart4;
  630. U32 endSignal;
  631. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  632. U32 const dtLog = dtd.tableLog;
  633. if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
  634. { size_t const errorCode = BIT_initDStream(&bitD1, istart1, length1);
  635. if (HUF_isError(errorCode)) return errorCode; }
  636. { size_t const errorCode = BIT_initDStream(&bitD2, istart2, length2);
  637. if (HUF_isError(errorCode)) return errorCode; }
  638. { size_t const errorCode = BIT_initDStream(&bitD3, istart3, length3);
  639. if (HUF_isError(errorCode)) return errorCode; }
  640. { size_t const errorCode = BIT_initDStream(&bitD4, istart4, length4);
  641. if (HUF_isError(errorCode)) return errorCode; }
  642. /* 16-32 symbols per loop (4-8 symbols per stream) */
  643. endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
  644. for ( ; (endSignal==BIT_DStream_unfinished) & (op4<(oend-(sizeof(bitD4.bitContainer)-1))) ; ) {
  645. HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
  646. HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
  647. HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
  648. HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
  649. HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
  650. HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
  651. HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
  652. HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
  653. HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
  654. HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
  655. HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
  656. HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
  657. HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
  658. HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
  659. HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
  660. HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
  661. endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
  662. }
  663. /* check corruption */
  664. if (op1 > opStart2) return ERROR(corruption_detected);
  665. if (op2 > opStart3) return ERROR(corruption_detected);
  666. if (op3 > opStart4) return ERROR(corruption_detected);
  667. /* note : op4 already verified within main loop */
  668. /* finish bitStreams one by one */
  669. HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
  670. HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
  671. HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
  672. HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
  673. /* check */
  674. { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
  675. if (!endCheck) return ERROR(corruption_detected); }
  676. /* decoded size */
  677. return dstSize;
  678. }
  679. }
  680. size_t HUF_decompress4X4_usingDTable(
  681. void* dst, size_t dstSize,
  682. const void* cSrc, size_t cSrcSize,
  683. const HUF_DTable* DTable)
  684. {
  685. DTableDesc dtd = HUF_getDTableDesc(DTable);
  686. if (dtd.tableType != 1) return ERROR(GENERIC);
  687. return HUF_decompress4X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
  688. }
  689. size_t HUF_decompress4X4_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
  690. const void* cSrc, size_t cSrcSize,
  691. void* workSpace, size_t wkspSize)
  692. {
  693. const BYTE* ip = (const BYTE*) cSrc;
  694. size_t hSize = HUF_readDTableX4_wksp(dctx, cSrc, cSrcSize,
  695. workSpace, wkspSize);
  696. if (HUF_isError(hSize)) return hSize;
  697. if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
  698. ip += hSize; cSrcSize -= hSize;
  699. return HUF_decompress4X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx);
  700. }
  701. size_t HUF_decompress4X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
  702. const void* cSrc, size_t cSrcSize)
  703. {
  704. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  705. return HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  706. workSpace, sizeof(workSpace));
  707. }
  708. size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  709. {
  710. HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_TABLELOG_MAX);
  711. return HUF_decompress4X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
  712. }
  713. /* ********************************/
  714. /* Generic decompression selector */
  715. /* ********************************/
  716. size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize,
  717. const void* cSrc, size_t cSrcSize,
  718. const HUF_DTable* DTable)
  719. {
  720. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  721. return dtd.tableType ? HUF_decompress1X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable) :
  722. HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
  723. }
  724. size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize,
  725. const void* cSrc, size_t cSrcSize,
  726. const HUF_DTable* DTable)
  727. {
  728. DTableDesc const dtd = HUF_getDTableDesc(DTable);
  729. return dtd.tableType ? HUF_decompress4X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable) :
  730. HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
  731. }
  732. typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
  733. static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
  734. {
  735. /* single, double, quad */
  736. {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
  737. {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
  738. {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
  739. {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
  740. {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
  741. {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
  742. {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
  743. {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
  744. {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
  745. {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
  746. {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
  747. {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
  748. {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
  749. {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
  750. {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
  751. {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
  752. };
  753. /** HUF_selectDecoder() :
  754. * Tells which decoder is likely to decode faster,
  755. * based on a set of pre-determined metrics.
  756. * @return : 0==HUF_decompress4X2, 1==HUF_decompress4X4 .
  757. * Assumption : 0 < cSrcSize, dstSize <= 128 KB */
  758. U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize)
  759. {
  760. /* decoder timing evaluation */
  761. U32 const Q = cSrcSize >= dstSize ? 15 : (U32)(cSrcSize * 16 / dstSize); /* Q < 16 */
  762. U32 const D256 = (U32)(dstSize >> 8);
  763. U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);
  764. U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);
  765. DTime1 += DTime1 >> 3; /* advantage to algorithm using less memory, for cache eviction */
  766. return DTime1 < DTime0;
  767. }
  768. typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
  769. size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  770. {
  771. static const decompressionAlgo decompress[2] = { HUF_decompress4X2, HUF_decompress4X4 };
  772. /* validation checks */
  773. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  774. if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
  775. if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
  776. if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
  777. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  778. return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
  779. }
  780. }
  781. size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  782. {
  783. /* validation checks */
  784. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  785. if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
  786. if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
  787. if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
  788. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  789. return algoNb ? HUF_decompress4X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
  790. HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
  791. }
  792. }
  793. size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
  794. {
  795. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  796. return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  797. workSpace, sizeof(workSpace));
  798. }
  799. size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst,
  800. size_t dstSize, const void* cSrc,
  801. size_t cSrcSize, void* workSpace,
  802. size_t wkspSize)
  803. {
  804. /* validation checks */
  805. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  806. if (cSrcSize == 0) return ERROR(corruption_detected);
  807. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  808. return algoNb ? HUF_decompress4X4_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize):
  809. HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);
  810. }
  811. }
  812. size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
  813. const void* cSrc, size_t cSrcSize,
  814. void* workSpace, size_t wkspSize)
  815. {
  816. /* validation checks */
  817. if (dstSize == 0) return ERROR(dstSize_tooSmall);
  818. if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
  819. if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
  820. if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
  821. { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
  822. return algoNb ? HUF_decompress1X4_DCtx_wksp(dctx, dst, dstSize, cSrc,
  823. cSrcSize, workSpace, wkspSize):
  824. HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,
  825. cSrcSize, workSpace, wkspSize);
  826. }
  827. }
  828. size_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
  829. const void* cSrc, size_t cSrcSize)
  830. {
  831. U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
  832. return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
  833. workSpace, sizeof(workSpace));
  834. }