extzstd 0.1 → 0.3.2

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