zstd-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +11 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +29 -0
  9. data/README.md +63 -0
  10. data/Rakefile +22 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/ext/zstdruby/extconf.rb +23 -0
  14. data/ext/zstdruby/libzstd/.gitignore +2 -0
  15. data/ext/zstdruby/libzstd/Makefile +133 -0
  16. data/ext/zstdruby/libzstd/README.md +77 -0
  17. data/ext/zstdruby/libzstd/common/bitstream.h +414 -0
  18. data/ext/zstdruby/libzstd/common/entropy_common.c +227 -0
  19. data/ext/zstdruby/libzstd/common/error_private.c +43 -0
  20. data/ext/zstdruby/libzstd/common/error_private.h +76 -0
  21. data/ext/zstdruby/libzstd/common/fse.h +668 -0
  22. data/ext/zstdruby/libzstd/common/fse_decompress.c +329 -0
  23. data/ext/zstdruby/libzstd/common/huf.h +238 -0
  24. data/ext/zstdruby/libzstd/common/mem.h +372 -0
  25. data/ext/zstdruby/libzstd/common/xxhash.c +867 -0
  26. data/ext/zstdruby/libzstd/common/xxhash.h +309 -0
  27. data/ext/zstdruby/libzstd/common/zstd_common.c +77 -0
  28. data/ext/zstdruby/libzstd/common/zstd_errors.h +60 -0
  29. data/ext/zstdruby/libzstd/common/zstd_internal.h +270 -0
  30. data/ext/zstdruby/libzstd/compress/fse_compress.c +850 -0
  31. data/ext/zstdruby/libzstd/compress/huf_compress.c +609 -0
  32. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3291 -0
  33. data/ext/zstdruby/libzstd/compress/zstd_opt.h +919 -0
  34. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +885 -0
  35. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +2154 -0
  36. data/ext/zstdruby/libzstd/deprecated/zbuff.h +210 -0
  37. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +145 -0
  38. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +74 -0
  39. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1913 -0
  40. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.h +67 -0
  41. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +1012 -0
  42. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +111 -0
  43. data/ext/zstdruby/libzstd/dll/example/Makefile +47 -0
  44. data/ext/zstdruby/libzstd/dll/example/README.md +69 -0
  45. data/ext/zstdruby/libzstd/dll/example/build_package.bat +17 -0
  46. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +25 -0
  47. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +179 -0
  48. data/ext/zstdruby/libzstd/dll/libzstd.def +86 -0
  49. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +259 -0
  50. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +2095 -0
  51. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +80 -0
  52. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +3518 -0
  53. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +79 -0
  54. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +3159 -0
  55. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +79 -0
  56. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +3795 -0
  57. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +128 -0
  58. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +4056 -0
  59. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +149 -0
  60. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +4167 -0
  61. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +159 -0
  62. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +4540 -0
  63. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +173 -0
  64. data/ext/zstdruby/libzstd/libzstd.pc.in +14 -0
  65. data/ext/zstdruby/libzstd/zstd.h +673 -0
  66. data/ext/zstdruby/zstdruby.c +117 -0
  67. data/ext/zstdruby/zstdruby.h +6 -0
  68. data/lib/zstd-ruby.rb +6 -0
  69. data/lib/zstd-ruby/version.rb +3 -0
  70. data/zstd-ruby.gemspec +37 -0
  71. metadata +170 -0
@@ -0,0 +1,609 @@
1
+ /* ******************************************************************
2
+ Huffman encoder, 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
+ #ifdef _MSC_VER /* Visual Studio */
39
+ # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
40
+ #endif
41
+
42
+
43
+ /* **************************************************************
44
+ * Includes
45
+ ****************************************************************/
46
+ #include <string.h> /* memcpy, memset */
47
+ #include <stdio.h> /* printf (debug) */
48
+ #include "bitstream.h"
49
+ #define FSE_STATIC_LINKING_ONLY /* FSE_optimalTableLog_internal */
50
+ #include "fse.h" /* header compression */
51
+ #define HUF_STATIC_LINKING_ONLY
52
+ #include "huf.h"
53
+
54
+
55
+ /* **************************************************************
56
+ * Error Management
57
+ ****************************************************************/
58
+ #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
59
+ #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return f
60
+ #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
61
+
62
+
63
+ /* **************************************************************
64
+ * Utils
65
+ ****************************************************************/
66
+ unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
67
+ {
68
+ return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1);
69
+ }
70
+
71
+
72
+ /* *******************************************************
73
+ * HUF : Huffman block compression
74
+ *********************************************************/
75
+ /* HUF_compressWeights() :
76
+ * Same as FSE_compress(), but dedicated to huff0's weights compression.
77
+ * The use case needs much less stack memory.
78
+ * Note : all elements within weightTable are supposed to be <= HUF_TABLELOG_MAX.
79
+ */
80
+ #define MAX_FSE_TABLELOG_FOR_HUFF_HEADER 6
81
+ size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weightTable, size_t wtSize)
82
+ {
83
+ BYTE* const ostart = (BYTE*) dst;
84
+ BYTE* op = ostart;
85
+ BYTE* const oend = ostart + dstSize;
86
+
87
+ U32 maxSymbolValue = HUF_TABLELOG_MAX;
88
+ U32 tableLog = MAX_FSE_TABLELOG_FOR_HUFF_HEADER;
89
+
90
+ FSE_CTable CTable[FSE_CTABLE_SIZE_U32(MAX_FSE_TABLELOG_FOR_HUFF_HEADER, HUF_TABLELOG_MAX)];
91
+ BYTE scratchBuffer[1<<MAX_FSE_TABLELOG_FOR_HUFF_HEADER];
92
+
93
+ U32 count[HUF_TABLELOG_MAX+1];
94
+ S16 norm[HUF_TABLELOG_MAX+1];
95
+
96
+ /* init conditions */
97
+ if (wtSize <= 1) return 0; /* Not compressible */
98
+
99
+ /* Scan input and build symbol stats */
100
+ { CHECK_V_F(maxCount, FSE_count_simple(count, &maxSymbolValue, weightTable, wtSize) );
101
+ if (maxCount == wtSize) return 1; /* only a single symbol in src : rle */
102
+ if (maxCount == 1) return 0; /* each symbol present maximum once => not compressible */
103
+ }
104
+
105
+ tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue);
106
+ CHECK_F( FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue) );
107
+
108
+ /* Write table description header */
109
+ { CHECK_V_F(hSize, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) );
110
+ op += hSize;
111
+ }
112
+
113
+ /* Compress */
114
+ CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, sizeof(scratchBuffer)) );
115
+ { CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, weightTable, wtSize, CTable) );
116
+ if (cSize == 0) return 0; /* not enough space for compressed data */
117
+ op += cSize;
118
+ }
119
+
120
+ return op-ostart;
121
+ }
122
+
123
+
124
+ struct HUF_CElt_s {
125
+ U16 val;
126
+ BYTE nbBits;
127
+ }; /* typedef'd to HUF_CElt within "huf.h" */
128
+
129
+ /*! HUF_writeCTable() :
130
+ `CTable` : huffman tree to save, using huf representation.
131
+ @return : size of saved CTable */
132
+ size_t HUF_writeCTable (void* dst, size_t maxDstSize,
133
+ const HUF_CElt* CTable, U32 maxSymbolValue, U32 huffLog)
134
+ {
135
+ BYTE bitsToWeight[HUF_TABLELOG_MAX + 1]; /* precomputed conversion table */
136
+ BYTE huffWeight[HUF_SYMBOLVALUE_MAX];
137
+ BYTE* op = (BYTE*)dst;
138
+ U32 n;
139
+
140
+ /* check conditions */
141
+ if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge);
142
+
143
+ /* convert to weight */
144
+ bitsToWeight[0] = 0;
145
+ for (n=1; n<huffLog+1; n++)
146
+ bitsToWeight[n] = (BYTE)(huffLog + 1 - n);
147
+ for (n=0; n<maxSymbolValue; n++)
148
+ huffWeight[n] = bitsToWeight[CTable[n].nbBits];
149
+
150
+ /* attempt weights compression by FSE */
151
+ { CHECK_V_F(hSize, HUF_compressWeights(op+1, maxDstSize-1, huffWeight, maxSymbolValue) );
152
+ if ((hSize>1) & (hSize < maxSymbolValue/2)) { /* FSE compressed */
153
+ op[0] = (BYTE)hSize;
154
+ return hSize+1;
155
+ } }
156
+
157
+ /* write raw values as 4-bits (max : 15) */
158
+ if (maxSymbolValue > (256-128)) return ERROR(GENERIC); /* should not happen : likely means source cannot be compressed */
159
+ if (((maxSymbolValue+1)/2) + 1 > maxDstSize) return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */
160
+ op[0] = (BYTE)(128 /*special case*/ + (maxSymbolValue-1));
161
+ huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause msan issue in final combination */
162
+ for (n=0; n<maxSymbolValue; n+=2)
163
+ op[(n/2)+1] = (BYTE)((huffWeight[n] << 4) + huffWeight[n+1]);
164
+ return ((maxSymbolValue+1)/2) + 1;
165
+ }
166
+
167
+
168
+ size_t HUF_readCTable (HUF_CElt* CTable, U32 maxSymbolValue, const void* src, size_t srcSize)
169
+ {
170
+ BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1]; /* init not required, even though some static analyzer may complain */
171
+ U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */
172
+ U32 tableLog = 0;
173
+ U32 nbSymbols = 0;
174
+
175
+ /* get symbol weights */
176
+ CHECK_V_F(readSize, HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX+1, rankVal, &nbSymbols, &tableLog, src, srcSize));
177
+
178
+ /* check result */
179
+ if (tableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
180
+ if (nbSymbols > maxSymbolValue+1) return ERROR(maxSymbolValue_tooSmall);
181
+
182
+ /* Prepare base value per rank */
183
+ { U32 n, nextRankStart = 0;
184
+ for (n=1; n<=tableLog; n++) {
185
+ U32 current = nextRankStart;
186
+ nextRankStart += (rankVal[n] << (n-1));
187
+ rankVal[n] = current;
188
+ } }
189
+
190
+ /* fill nbBits */
191
+ { U32 n; for (n=0; n<nbSymbols; n++) {
192
+ const U32 w = huffWeight[n];
193
+ CTable[n].nbBits = (BYTE)(tableLog + 1 - w);
194
+ } }
195
+
196
+ /* fill val */
197
+ { U16 nbPerRank[HUF_TABLELOG_MAX+2] = {0}; /* support w=0=>n=tableLog+1 */
198
+ U16 valPerRank[HUF_TABLELOG_MAX+2] = {0};
199
+ { U32 n; for (n=0; n<nbSymbols; n++) nbPerRank[CTable[n].nbBits]++; }
200
+ /* determine stating value per rank */
201
+ valPerRank[tableLog+1] = 0; /* for w==0 */
202
+ { U16 min = 0;
203
+ U32 n; for (n=tableLog; n>0; n--) { /* start at n=tablelog <-> w=1 */
204
+ valPerRank[n] = min; /* get starting value within each rank */
205
+ min += nbPerRank[n];
206
+ min >>= 1;
207
+ } }
208
+ /* assign value within rank, symbol order */
209
+ { U32 n; for (n=0; n<=maxSymbolValue; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; }
210
+ }
211
+
212
+ return readSize;
213
+ }
214
+
215
+
216
+ typedef struct nodeElt_s {
217
+ U32 count;
218
+ U16 parent;
219
+ BYTE byte;
220
+ BYTE nbBits;
221
+ } nodeElt;
222
+
223
+ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
224
+ {
225
+ const U32 largestBits = huffNode[lastNonNull].nbBits;
226
+ if (largestBits <= maxNbBits) return largestBits; /* early exit : no elt > maxNbBits */
227
+
228
+ /* there are several too large elements (at least >= 2) */
229
+ { int totalCost = 0;
230
+ const U32 baseCost = 1 << (largestBits - maxNbBits);
231
+ U32 n = lastNonNull;
232
+
233
+ while (huffNode[n].nbBits > maxNbBits) {
234
+ totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits));
235
+ huffNode[n].nbBits = (BYTE)maxNbBits;
236
+ n --;
237
+ } /* n stops at huffNode[n].nbBits <= maxNbBits */
238
+ while (huffNode[n].nbBits == maxNbBits) n--; /* n end at index of smallest symbol using < maxNbBits */
239
+
240
+ /* renorm totalCost */
241
+ totalCost >>= (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */
242
+
243
+ /* repay normalized cost */
244
+ { U32 const noSymbol = 0xF0F0F0F0;
245
+ U32 rankLast[HUF_TABLELOG_MAX+2];
246
+ int pos;
247
+
248
+ /* Get pos of last (smallest) symbol per rank */
249
+ memset(rankLast, 0xF0, sizeof(rankLast));
250
+ { U32 currentNbBits = maxNbBits;
251
+ for (pos=n ; pos >= 0; pos--) {
252
+ if (huffNode[pos].nbBits >= currentNbBits) continue;
253
+ currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */
254
+ rankLast[maxNbBits-currentNbBits] = pos;
255
+ } }
256
+
257
+ while (totalCost > 0) {
258
+ U32 nBitsToDecrease = BIT_highbit32(totalCost) + 1;
259
+ for ( ; nBitsToDecrease > 1; nBitsToDecrease--) {
260
+ U32 highPos = rankLast[nBitsToDecrease];
261
+ U32 lowPos = rankLast[nBitsToDecrease-1];
262
+ if (highPos == noSymbol) continue;
263
+ if (lowPos == noSymbol) break;
264
+ { U32 const highTotal = huffNode[highPos].count;
265
+ U32 const lowTotal = 2 * huffNode[lowPos].count;
266
+ if (highTotal <= lowTotal) break;
267
+ } }
268
+ /* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */
269
+ while ((nBitsToDecrease<=HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol)) /* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */
270
+ nBitsToDecrease ++;
271
+ totalCost -= 1 << (nBitsToDecrease-1);
272
+ if (rankLast[nBitsToDecrease-1] == noSymbol)
273
+ rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease]; /* this rank is no longer empty */
274
+ huffNode[rankLast[nBitsToDecrease]].nbBits ++;
275
+ if (rankLast[nBitsToDecrease] == 0) /* special case, reached largest symbol */
276
+ rankLast[nBitsToDecrease] = noSymbol;
277
+ else {
278
+ rankLast[nBitsToDecrease]--;
279
+ if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease)
280
+ rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */
281
+ } } /* while (totalCost > 0) */
282
+
283
+ while (totalCost < 0) { /* Sometimes, cost correction overshoot */
284
+ if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */
285
+ while (huffNode[n].nbBits == maxNbBits) n--;
286
+ huffNode[n+1].nbBits--;
287
+ rankLast[1] = n+1;
288
+ totalCost++;
289
+ continue;
290
+ }
291
+ huffNode[ rankLast[1] + 1 ].nbBits--;
292
+ rankLast[1]++;
293
+ totalCost ++;
294
+ } } } /* there are several too large elements (at least >= 2) */
295
+
296
+ return maxNbBits;
297
+ }
298
+
299
+
300
+ typedef struct {
301
+ U32 base;
302
+ U32 current;
303
+ } rankPos;
304
+
305
+ static void HUF_sort(nodeElt* huffNode, const U32* count, U32 maxSymbolValue)
306
+ {
307
+ rankPos rank[32];
308
+ U32 n;
309
+
310
+ memset(rank, 0, sizeof(rank));
311
+ for (n=0; n<=maxSymbolValue; n++) {
312
+ U32 r = BIT_highbit32(count[n] + 1);
313
+ rank[r].base ++;
314
+ }
315
+ for (n=30; n>0; n--) rank[n-1].base += rank[n].base;
316
+ for (n=0; n<32; n++) rank[n].current = rank[n].base;
317
+ for (n=0; n<=maxSymbolValue; n++) {
318
+ U32 const c = count[n];
319
+ U32 const r = BIT_highbit32(c+1) + 1;
320
+ U32 pos = rank[r].current++;
321
+ while ((pos > rank[r].base) && (c > huffNode[pos-1].count)) huffNode[pos]=huffNode[pos-1], pos--;
322
+ huffNode[pos].count = c;
323
+ huffNode[pos].byte = (BYTE)n;
324
+ }
325
+ }
326
+
327
+
328
+ /** HUF_buildCTable_wksp() :
329
+ * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
330
+ * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned.
331
+ */
332
+ #define STARTNODE (HUF_SYMBOLVALUE_MAX+1)
333
+ typedef nodeElt huffNodeTable[2*HUF_SYMBOLVALUE_MAX+1 +1];
334
+ size_t HUF_buildCTable_wksp (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize)
335
+ {
336
+ nodeElt* const huffNode0 = (nodeElt*)workSpace;
337
+ nodeElt* const huffNode = huffNode0+1;
338
+ U32 n, nonNullRank;
339
+ int lowS, lowN;
340
+ U16 nodeNb = STARTNODE;
341
+ U32 nodeRoot;
342
+
343
+ /* safety checks */
344
+ if (wkspSize < sizeof(huffNodeTable)) return ERROR(GENERIC); /* workSpace is not large enough */
345
+ if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
346
+ if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(GENERIC);
347
+ memset(huffNode0, 0, sizeof(huffNodeTable));
348
+
349
+ /* sort, decreasing order */
350
+ HUF_sort(huffNode, count, maxSymbolValue);
351
+
352
+ /* init for parents */
353
+ nonNullRank = maxSymbolValue;
354
+ while(huffNode[nonNullRank].count == 0) nonNullRank--;
355
+ lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb;
356
+ huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS-1].count;
357
+ huffNode[lowS].parent = huffNode[lowS-1].parent = nodeNb;
358
+ nodeNb++; lowS-=2;
359
+ for (n=nodeNb; n<=nodeRoot; n++) huffNode[n].count = (U32)(1U<<30);
360
+ huffNode0[0].count = (U32)(1U<<31); /* fake entry, strong barrier */
361
+
362
+ /* create parents */
363
+ while (nodeNb <= nodeRoot) {
364
+ U32 n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
365
+ U32 n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
366
+ huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count;
367
+ huffNode[n1].parent = huffNode[n2].parent = nodeNb;
368
+ nodeNb++;
369
+ }
370
+
371
+ /* distribute weights (unlimited tree height) */
372
+ huffNode[nodeRoot].nbBits = 0;
373
+ for (n=nodeRoot-1; n>=STARTNODE; n--)
374
+ huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
375
+ for (n=0; n<=nonNullRank; n++)
376
+ huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
377
+
378
+ /* enforce maxTableLog */
379
+ maxNbBits = HUF_setMaxHeight(huffNode, nonNullRank, maxNbBits);
380
+
381
+ /* fill result into tree (val, nbBits) */
382
+ { U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
383
+ U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
384
+ if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */
385
+ for (n=0; n<=nonNullRank; n++)
386
+ nbPerRank[huffNode[n].nbBits]++;
387
+ /* determine stating value per rank */
388
+ { U16 min = 0;
389
+ for (n=maxNbBits; n>0; n--) {
390
+ valPerRank[n] = min; /* get starting value within each rank */
391
+ min += nbPerRank[n];
392
+ min >>= 1;
393
+ } }
394
+ for (n=0; n<=maxSymbolValue; n++)
395
+ tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
396
+ for (n=0; n<=maxSymbolValue; n++)
397
+ tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */
398
+ }
399
+
400
+ return maxNbBits;
401
+ }
402
+
403
+ /** HUF_buildCTable() :
404
+ * Note : count is used before tree is written, so they can safely overlap
405
+ */
406
+ size_t HUF_buildCTable (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits)
407
+ {
408
+ huffNodeTable nodeTable;
409
+ return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, nodeTable, sizeof(nodeTable));
410
+ }
411
+
412
+ static void HUF_encodeSymbol(BIT_CStream_t* bitCPtr, U32 symbol, const HUF_CElt* CTable)
413
+ {
414
+ BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits);
415
+ }
416
+
417
+ size_t HUF_compressBound(size_t size) { return HUF_COMPRESSBOUND(size); }
418
+
419
+ #define HUF_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
420
+
421
+ #define HUF_FLUSHBITS_1(stream) \
422
+ if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*2+7) HUF_FLUSHBITS(stream)
423
+
424
+ #define HUF_FLUSHBITS_2(stream) \
425
+ if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*4+7) HUF_FLUSHBITS(stream)
426
+
427
+ size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
428
+ {
429
+ const BYTE* ip = (const BYTE*) src;
430
+ BYTE* const ostart = (BYTE*)dst;
431
+ BYTE* const oend = ostart + dstSize;
432
+ BYTE* op = ostart;
433
+ size_t n;
434
+ const unsigned fast = (dstSize >= HUF_BLOCKBOUND(srcSize));
435
+ BIT_CStream_t bitC;
436
+
437
+ /* init */
438
+ if (dstSize < 8) return 0; /* not enough space to compress */
439
+ { size_t const initErr = BIT_initCStream(&bitC, op, oend-op);
440
+ if (HUF_isError(initErr)) return 0; }
441
+
442
+ n = srcSize & ~3; /* join to mod 4 */
443
+ switch (srcSize & 3)
444
+ {
445
+ case 3 : HUF_encodeSymbol(&bitC, ip[n+ 2], CTable);
446
+ HUF_FLUSHBITS_2(&bitC);
447
+ case 2 : HUF_encodeSymbol(&bitC, ip[n+ 1], CTable);
448
+ HUF_FLUSHBITS_1(&bitC);
449
+ case 1 : HUF_encodeSymbol(&bitC, ip[n+ 0], CTable);
450
+ HUF_FLUSHBITS(&bitC);
451
+ case 0 :
452
+ default: ;
453
+ }
454
+
455
+ for (; n>0; n-=4) { /* note : n&3==0 at this stage */
456
+ HUF_encodeSymbol(&bitC, ip[n- 1], CTable);
457
+ HUF_FLUSHBITS_1(&bitC);
458
+ HUF_encodeSymbol(&bitC, ip[n- 2], CTable);
459
+ HUF_FLUSHBITS_2(&bitC);
460
+ HUF_encodeSymbol(&bitC, ip[n- 3], CTable);
461
+ HUF_FLUSHBITS_1(&bitC);
462
+ HUF_encodeSymbol(&bitC, ip[n- 4], CTable);
463
+ HUF_FLUSHBITS(&bitC);
464
+ }
465
+
466
+ return BIT_closeCStream(&bitC);
467
+ }
468
+
469
+
470
+ size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
471
+ {
472
+ size_t const segmentSize = (srcSize+3)/4; /* first 3 segments */
473
+ const BYTE* ip = (const BYTE*) src;
474
+ const BYTE* const iend = ip + srcSize;
475
+ BYTE* const ostart = (BYTE*) dst;
476
+ BYTE* const oend = ostart + dstSize;
477
+ BYTE* op = ostart;
478
+
479
+ if (dstSize < 6 + 1 + 1 + 1 + 8) return 0; /* minimum space to compress successfully */
480
+ if (srcSize < 12) return 0; /* no saving possible : too small input */
481
+ op += 6; /* jumpTable */
482
+
483
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable) );
484
+ if (cSize==0) return 0;
485
+ MEM_writeLE16(ostart, (U16)cSize);
486
+ op += cSize;
487
+ }
488
+
489
+ ip += segmentSize;
490
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable) );
491
+ if (cSize==0) return 0;
492
+ MEM_writeLE16(ostart+2, (U16)cSize);
493
+ op += cSize;
494
+ }
495
+
496
+ ip += segmentSize;
497
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable) );
498
+ if (cSize==0) return 0;
499
+ MEM_writeLE16(ostart+4, (U16)cSize);
500
+ op += cSize;
501
+ }
502
+
503
+ ip += segmentSize;
504
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable(op, oend-op, ip, iend-ip, CTable) );
505
+ if (cSize==0) return 0;
506
+ op += cSize;
507
+ }
508
+
509
+ return op-ostart;
510
+ }
511
+
512
+
513
+ /* `workSpace` must a table of at least 1024 unsigned */
514
+ static size_t HUF_compress_internal (
515
+ void* dst, size_t dstSize,
516
+ const void* src, size_t srcSize,
517
+ unsigned maxSymbolValue, unsigned huffLog,
518
+ unsigned singleStream,
519
+ void* workSpace, size_t wkspSize)
520
+ {
521
+ BYTE* const ostart = (BYTE*)dst;
522
+ BYTE* const oend = ostart + dstSize;
523
+ BYTE* op = ostart;
524
+
525
+ union {
526
+ U32 count[HUF_SYMBOLVALUE_MAX+1];
527
+ HUF_CElt CTable[HUF_SYMBOLVALUE_MAX+1];
528
+ } table; /* `count` can overlap with `CTable`; saves 1 KB */
529
+
530
+ /* checks & inits */
531
+ if (wkspSize < sizeof(huffNodeTable)) return ERROR(GENERIC);
532
+ if (!srcSize) return 0; /* Uncompressed (note : 1 means rle, so first byte must be correct) */
533
+ if (!dstSize) return 0; /* cannot fit within dst budget */
534
+ if (srcSize > HUF_BLOCKSIZE_MAX) return ERROR(srcSize_wrong); /* current block size limit */
535
+ if (huffLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
536
+ if (!maxSymbolValue) maxSymbolValue = HUF_SYMBOLVALUE_MAX;
537
+ if (!huffLog) huffLog = HUF_TABLELOG_DEFAULT;
538
+
539
+ /* Scan input and build symbol stats */
540
+ { CHECK_V_F(largest, FSE_count_wksp (table.count, &maxSymbolValue, (const BYTE*)src, srcSize, (U32*)workSpace) );
541
+ if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; } /* single symbol, rle */
542
+ if (largest <= (srcSize >> 7)+1) return 0; /* Fast heuristic : not compressible enough */
543
+ }
544
+
545
+ /* Build Huffman Tree */
546
+ huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue);
547
+ { CHECK_V_F(maxBits, HUF_buildCTable_wksp (table.CTable, table.count, maxSymbolValue, huffLog, workSpace, wkspSize) );
548
+ huffLog = (U32)maxBits;
549
+ }
550
+
551
+ /* Write table description header */
552
+ { CHECK_V_F(hSize, HUF_writeCTable (op, dstSize, table.CTable, maxSymbolValue, huffLog) );
553
+ if (hSize + 12 >= srcSize) return 0; /* not useful to try compression */
554
+ op += hSize;
555
+ }
556
+
557
+ /* Compress */
558
+ { size_t const cSize = (singleStream) ?
559
+ HUF_compress1X_usingCTable(op, oend - op, src, srcSize, table.CTable) : /* single segment */
560
+ HUF_compress4X_usingCTable(op, oend - op, src, srcSize, table.CTable);
561
+ if (HUF_isError(cSize)) return cSize;
562
+ if (cSize==0) return 0; /* uncompressible */
563
+ op += cSize;
564
+ }
565
+
566
+ /* check compressibility */
567
+ if ((size_t)(op-ostart) >= srcSize-1)
568
+ return 0;
569
+
570
+ return op-ostart;
571
+ }
572
+
573
+
574
+ size_t HUF_compress1X_wksp (void* dst, size_t dstSize,
575
+ const void* src, size_t srcSize,
576
+ unsigned maxSymbolValue, unsigned huffLog,
577
+ void* workSpace, size_t wkspSize)
578
+ {
579
+ return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1 /* single stream */, workSpace, wkspSize);
580
+ }
581
+
582
+ size_t HUF_compress1X (void* dst, size_t dstSize,
583
+ const void* src, size_t srcSize,
584
+ unsigned maxSymbolValue, unsigned huffLog)
585
+ {
586
+ unsigned workSpace[1024];
587
+ return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace));
588
+ }
589
+
590
+ size_t HUF_compress4X_wksp (void* dst, size_t dstSize,
591
+ const void* src, size_t srcSize,
592
+ unsigned maxSymbolValue, unsigned huffLog,
593
+ void* workSpace, size_t wkspSize)
594
+ {
595
+ return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 0 /* 4 streams */, workSpace, wkspSize);
596
+ }
597
+
598
+ size_t HUF_compress2 (void* dst, size_t dstSize,
599
+ const void* src, size_t srcSize,
600
+ unsigned maxSymbolValue, unsigned huffLog)
601
+ {
602
+ unsigned workSpace[1024];
603
+ return HUF_compress4X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, workSpace, sizeof(workSpace));
604
+ }
605
+
606
+ size_t HUF_compress (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
607
+ {
608
+ return HUF_compress2(dst, maxDstSize, src, (U32)srcSize, 255, HUF_TABLELOG_DEFAULT);
609
+ }