zstd-ruby 1.4.4.0 → 1.4.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/zstdruby/libzstd/Makefile +123 -58
  4. data/ext/zstdruby/libzstd/README.md +34 -14
  5. data/ext/zstdruby/libzstd/common/bitstream.h +31 -37
  6. data/ext/zstdruby/libzstd/common/compiler.h +19 -3
  7. data/ext/zstdruby/libzstd/common/cpu.h +1 -1
  8. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  9. data/ext/zstdruby/libzstd/common/debug.h +11 -31
  10. data/ext/zstdruby/libzstd/common/entropy_common.c +13 -33
  11. data/ext/zstdruby/libzstd/common/error_private.c +2 -1
  12. data/ext/zstdruby/libzstd/common/error_private.h +6 -2
  13. data/ext/zstdruby/libzstd/common/fse.h +11 -31
  14. data/ext/zstdruby/libzstd/common/fse_decompress.c +12 -37
  15. data/ext/zstdruby/libzstd/common/huf.h +15 -33
  16. data/ext/zstdruby/libzstd/common/mem.h +1 -1
  17. data/ext/zstdruby/libzstd/common/pool.c +1 -1
  18. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  19. data/ext/zstdruby/libzstd/common/threading.c +4 -3
  20. data/ext/zstdruby/libzstd/common/threading.h +4 -3
  21. data/ext/zstdruby/libzstd/common/xxhash.c +15 -33
  22. data/ext/zstdruby/libzstd/common/xxhash.h +11 -31
  23. data/ext/zstdruby/libzstd/common/zstd_common.c +1 -1
  24. data/ext/zstdruby/libzstd/common/zstd_errors.h +2 -1
  25. data/ext/zstdruby/libzstd/common/zstd_internal.h +112 -15
  26. data/ext/zstdruby/libzstd/compress/fse_compress.c +17 -40
  27. data/ext/zstdruby/libzstd/compress/hist.c +15 -35
  28. data/ext/zstdruby/libzstd/compress/hist.h +12 -32
  29. data/ext/zstdruby/libzstd/compress/huf_compress.c +92 -92
  30. data/ext/zstdruby/libzstd/compress/zstd_compress.c +450 -275
  31. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +136 -14
  32. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +10 -6
  33. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +1 -1
  34. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +24 -20
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +10 -3
  36. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +845 -0
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  38. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +3 -13
  39. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +11 -8
  40. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  41. data/ext/zstdruby/libzstd/compress/zstd_fast.c +36 -24
  42. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  43. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +34 -11
  44. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +1 -1
  45. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +27 -5
  46. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +7 -2
  47. data/ext/zstdruby/libzstd/compress/zstd_opt.c +38 -84
  48. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  49. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +48 -21
  50. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +2 -2
  51. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +76 -62
  52. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +12 -8
  53. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  54. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +264 -148
  55. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +312 -203
  56. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +3 -3
  57. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +18 -4
  58. data/ext/zstdruby/libzstd/deprecated/zbuff.h +3 -3
  59. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +2 -2
  60. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -1
  61. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  62. data/ext/zstdruby/libzstd/dictBuilder/cover.c +5 -5
  63. data/ext/zstdruby/libzstd/dictBuilder/cover.h +14 -4
  64. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +14 -4
  65. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +33 -9
  66. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +51 -28
  67. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  68. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +4 -4
  69. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +18 -12
  70. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  71. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +10 -6
  72. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  73. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +10 -6
  74. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  75. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +13 -7
  76. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  77. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +17 -13
  78. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +2 -2
  79. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +17 -13
  80. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  81. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +22 -14
  82. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  83. data/ext/zstdruby/libzstd/libzstd.pc.in +2 -2
  84. data/ext/zstdruby/libzstd/zstd.h +62 -21
  85. data/lib/zstd-ruby/version.rb +1 -1
  86. metadata +7 -5
@@ -1,42 +1,22 @@
1
1
  /* ******************************************************************
2
- hist : Histogram functions
3
- part of Finite State Entropy project
4
- Copyright (C) 2013-present, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-
31
- You can contact the author at :
32
- - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
33
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
2
+ * hist : Histogram functions
3
+ * part of Finite State Entropy project
4
+ * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
5
+ *
6
+ * You can contact the author at :
7
+ * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
+ * - Public forum : https://groups.google.com/forum/#!forum/lz4c
9
+ *
10
+ * This source code is licensed under both the BSD-style license (found in the
11
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
12
+ * in the COPYING file in the root directory of this source tree).
13
+ * You may select, at your option, one of the above-listed licenses.
34
14
  ****************************************************************** */
35
15
 
36
16
  /* --- dependencies --- */
37
- #include "mem.h" /* U32, BYTE, etc. */
38
- #include "debug.h" /* assert, DEBUGLOG */
39
- #include "error_private.h" /* ERROR */
17
+ #include "../common/mem.h" /* U32, BYTE, etc. */
18
+ #include "../common/debug.h" /* assert, DEBUGLOG */
19
+ #include "../common/error_private.h" /* ERROR */
40
20
  #include "hist.h"
41
21
 
42
22
 
@@ -1,36 +1,16 @@
1
1
  /* ******************************************************************
2
- hist : Histogram functions
3
- part of Finite State Entropy project
4
- Copyright (C) 2013-present, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-
31
- You can contact the author at :
32
- - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
33
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
2
+ * hist : Histogram functions
3
+ * part of Finite State Entropy project
4
+ * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
5
+ *
6
+ * You can contact the author at :
7
+ * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
+ * - Public forum : https://groups.google.com/forum/#!forum/lz4c
9
+ *
10
+ * This source code is licensed under both the BSD-style license (found in the
11
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
12
+ * in the COPYING file in the root directory of this source tree).
13
+ * You may select, at your option, one of the above-listed licenses.
34
14
  ****************************************************************** */
35
15
 
36
16
  /* --- dependencies --- */
@@ -1,35 +1,15 @@
1
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
2
+ * Huffman encoder, part of New Generation Entropy library
3
+ * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
4
+ *
5
+ * You can contact the author at :
6
+ * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
7
+ * - Public forum : https://groups.google.com/forum/#!forum/lz4c
8
+ *
9
+ * This source code is licensed under both the BSD-style license (found in the
10
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
+ * in the COPYING file in the root directory of this source tree).
12
+ * You may select, at your option, one of the above-listed licenses.
33
13
  ****************************************************************** */
34
14
 
35
15
  /* **************************************************************
@@ -45,14 +25,14 @@
45
25
  ****************************************************************/
46
26
  #include <string.h> /* memcpy, memset */
47
27
  #include <stdio.h> /* printf (debug) */
48
- #include "compiler.h"
49
- #include "bitstream.h"
28
+ #include "../common/compiler.h"
29
+ #include "../common/bitstream.h"
50
30
  #include "hist.h"
51
31
  #define FSE_STATIC_LINKING_ONLY /* FSE_optimalTableLog_internal */
52
- #include "fse.h" /* header compression */
32
+ #include "../common/fse.h" /* header compression */
53
33
  #define HUF_STATIC_LINKING_ONLY
54
- #include "huf.h"
55
- #include "error_private.h"
34
+ #include "../common/huf.h"
35
+ #include "../common/error_private.h"
56
36
 
57
37
 
58
38
  /* **************************************************************
@@ -60,8 +40,6 @@
60
40
  ****************************************************************/
61
41
  #define HUF_isError ERR_isError
62
42
  #define HUF_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */
63
- #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
64
- #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
65
43
 
66
44
 
67
45
  /* **************************************************************
@@ -110,18 +88,18 @@ static size_t HUF_compressWeights (void* dst, size_t dstSize, const void* weight
110
88
  CHECK_F( FSE_normalizeCount(norm, tableLog, count, wtSize, maxSymbolValue) );
111
89
 
112
90
  /* Write table description header */
113
- { CHECK_V_F(hSize, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) );
91
+ { CHECK_V_F(hSize, FSE_writeNCount(op, (size_t)(oend-op), norm, maxSymbolValue, tableLog) );
114
92
  op += hSize;
115
93
  }
116
94
 
117
95
  /* Compress */
118
96
  CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, sizeof(scratchBuffer)) );
119
- { CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, weightTable, wtSize, CTable) );
97
+ { CHECK_V_F(cSize, FSE_compress_usingCTable(op, (size_t)(oend - op), weightTable, wtSize, CTable) );
120
98
  if (cSize == 0) return 0; /* not enough space for compressed data */
121
99
  op += cSize;
122
100
  }
123
101
 
124
- return op-ostart;
102
+ return (size_t)(op-ostart);
125
103
  }
126
104
 
127
105
 
@@ -169,7 +147,7 @@ size_t HUF_writeCTable (void* dst, size_t maxDstSize,
169
147
  }
170
148
 
171
149
 
172
- size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize)
150
+ size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned* hasZeroWeights)
173
151
  {
174
152
  BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1]; /* init not required, even though some static analyzer may complain */
175
153
  U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */
@@ -192,9 +170,11 @@ size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void
192
170
  } }
193
171
 
194
172
  /* fill nbBits */
173
+ *hasZeroWeights = 0;
195
174
  { U32 n; for (n=0; n<nbSymbols; n++) {
196
175
  const U32 w = huffWeight[n];
197
- CTable[n].nbBits = (BYTE)(tableLog + 1 - w);
176
+ *hasZeroWeights |= (w == 0);
177
+ CTable[n].nbBits = (BYTE)(tableLog + 1 - w) & -(w != 0);
198
178
  } }
199
179
 
200
180
  /* fill val */
@@ -240,7 +220,7 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
240
220
  /* there are several too large elements (at least >= 2) */
241
221
  { int totalCost = 0;
242
222
  const U32 baseCost = 1 << (largestBits - maxNbBits);
243
- U32 n = lastNonNull;
223
+ int n = (int)lastNonNull;
244
224
 
245
225
  while (huffNode[n].nbBits > maxNbBits) {
246
226
  totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits));
@@ -255,22 +235,22 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
255
235
  /* repay normalized cost */
256
236
  { U32 const noSymbol = 0xF0F0F0F0;
257
237
  U32 rankLast[HUF_TABLELOG_MAX+2];
258
- int pos;
259
238
 
260
239
  /* Get pos of last (smallest) symbol per rank */
261
240
  memset(rankLast, 0xF0, sizeof(rankLast));
262
241
  { U32 currentNbBits = maxNbBits;
242
+ int pos;
263
243
  for (pos=n ; pos >= 0; pos--) {
264
244
  if (huffNode[pos].nbBits >= currentNbBits) continue;
265
245
  currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */
266
- rankLast[maxNbBits-currentNbBits] = pos;
246
+ rankLast[maxNbBits-currentNbBits] = (U32)pos;
267
247
  } }
268
248
 
269
249
  while (totalCost > 0) {
270
- U32 nBitsToDecrease = BIT_highbit32(totalCost) + 1;
250
+ U32 nBitsToDecrease = BIT_highbit32((U32)totalCost) + 1;
271
251
  for ( ; nBitsToDecrease > 1; nBitsToDecrease--) {
272
- U32 highPos = rankLast[nBitsToDecrease];
273
- U32 lowPos = rankLast[nBitsToDecrease-1];
252
+ U32 const highPos = rankLast[nBitsToDecrease];
253
+ U32 const lowPos = rankLast[nBitsToDecrease-1];
274
254
  if (highPos == noSymbol) continue;
275
255
  if (lowPos == noSymbol) break;
276
256
  { U32 const highTotal = huffNode[highPos].count;
@@ -297,7 +277,8 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
297
277
  if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */
298
278
  while (huffNode[n].nbBits == maxNbBits) n--;
299
279
  huffNode[n+1].nbBits--;
300
- rankLast[1] = n+1;
280
+ assert(n >= 0);
281
+ rankLast[1] = (U32)(n+1);
301
282
  totalCost++;
302
283
  continue;
303
284
  }
@@ -309,29 +290,36 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
309
290
  return maxNbBits;
310
291
  }
311
292
 
312
-
313
293
  typedef struct {
314
294
  U32 base;
315
295
  U32 current;
316
296
  } rankPos;
317
297
 
318
- static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValue)
298
+ typedef nodeElt huffNodeTable[HUF_CTABLE_WORKSPACE_SIZE_U32];
299
+
300
+ #define RANK_POSITION_TABLE_SIZE 32
301
+
302
+ typedef struct {
303
+ huffNodeTable huffNodeTbl;
304
+ rankPos rankPosition[RANK_POSITION_TABLE_SIZE];
305
+ } HUF_buildCTable_wksp_tables;
306
+
307
+ static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValue, rankPos* rankPosition)
319
308
  {
320
- rankPos rank[32];
321
309
  U32 n;
322
310
 
323
- memset(rank, 0, sizeof(rank));
311
+ memset(rankPosition, 0, sizeof(*rankPosition) * RANK_POSITION_TABLE_SIZE);
324
312
  for (n=0; n<=maxSymbolValue; n++) {
325
313
  U32 r = BIT_highbit32(count[n] + 1);
326
- rank[r].base ++;
314
+ rankPosition[r].base ++;
327
315
  }
328
- for (n=30; n>0; n--) rank[n-1].base += rank[n].base;
329
- for (n=0; n<32; n++) rank[n].current = rank[n].base;
316
+ for (n=30; n>0; n--) rankPosition[n-1].base += rankPosition[n].base;
317
+ for (n=0; n<32; n++) rankPosition[n].current = rankPosition[n].base;
330
318
  for (n=0; n<=maxSymbolValue; n++) {
331
319
  U32 const c = count[n];
332
320
  U32 const r = BIT_highbit32(c+1) + 1;
333
- U32 pos = rank[r].current++;
334
- while ((pos > rank[r].base) && (c > huffNode[pos-1].count)) {
321
+ U32 pos = rankPosition[r].current++;
322
+ while ((pos > rankPosition[r].base) && (c > huffNode[pos-1].count)) {
335
323
  huffNode[pos] = huffNode[pos-1];
336
324
  pos--;
337
325
  }
@@ -343,45 +331,48 @@ static void HUF_sort(nodeElt* huffNode, const unsigned* count, U32 maxSymbolValu
343
331
 
344
332
  /** HUF_buildCTable_wksp() :
345
333
  * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
346
- * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of HUF_CTABLE_WORKSPACE_SIZE_U32 unsigned.
334
+ * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as sizeof(HUF_buildCTable_wksp_tables).
347
335
  */
348
336
  #define STARTNODE (HUF_SYMBOLVALUE_MAX+1)
349
- typedef nodeElt huffNodeTable[HUF_CTABLE_WORKSPACE_SIZE_U32];
337
+
350
338
  size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize)
351
339
  {
352
- nodeElt* const huffNode0 = (nodeElt*)workSpace;
340
+ HUF_buildCTable_wksp_tables* const wksp_tables = (HUF_buildCTable_wksp_tables*)workSpace;
341
+ nodeElt* const huffNode0 = wksp_tables->huffNodeTbl;
353
342
  nodeElt* const huffNode = huffNode0+1;
354
- U32 n, nonNullRank;
343
+ int nonNullRank;
355
344
  int lowS, lowN;
356
- U16 nodeNb = STARTNODE;
357
- U32 nodeRoot;
345
+ int nodeNb = STARTNODE;
346
+ int n, nodeRoot;
358
347
 
359
348
  /* safety checks */
360
349
  if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
361
- if (wkspSize < sizeof(huffNodeTable)) return ERROR(workSpace_tooSmall);
350
+ if (wkspSize < sizeof(HUF_buildCTable_wksp_tables))
351
+ return ERROR(workSpace_tooSmall);
362
352
  if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
363
- if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(maxSymbolValue_tooLarge);
353
+ if (maxSymbolValue > HUF_SYMBOLVALUE_MAX)
354
+ return ERROR(maxSymbolValue_tooLarge);
364
355
  memset(huffNode0, 0, sizeof(huffNodeTable));
365
356
 
366
357
  /* sort, decreasing order */
367
- HUF_sort(huffNode, count, maxSymbolValue);
358
+ HUF_sort(huffNode, count, maxSymbolValue, wksp_tables->rankPosition);
368
359
 
369
360
  /* init for parents */
370
- nonNullRank = maxSymbolValue;
361
+ nonNullRank = (int)maxSymbolValue;
371
362
  while(huffNode[nonNullRank].count == 0) nonNullRank--;
372
363
  lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb;
373
364
  huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS-1].count;
374
- huffNode[lowS].parent = huffNode[lowS-1].parent = nodeNb;
365
+ huffNode[lowS].parent = huffNode[lowS-1].parent = (U16)nodeNb;
375
366
  nodeNb++; lowS-=2;
376
367
  for (n=nodeNb; n<=nodeRoot; n++) huffNode[n].count = (U32)(1U<<30);
377
368
  huffNode0[0].count = (U32)(1U<<31); /* fake entry, strong barrier */
378
369
 
379
370
  /* create parents */
380
371
  while (nodeNb <= nodeRoot) {
381
- U32 n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
382
- U32 n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
372
+ int const n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
373
+ int const n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
383
374
  huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count;
384
- huffNode[n1].parent = huffNode[n2].parent = nodeNb;
375
+ huffNode[n1].parent = huffNode[n2].parent = (U16)nodeNb;
385
376
  nodeNb++;
386
377
  }
387
378
 
@@ -393,24 +384,25 @@ size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbo
393
384
  huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
394
385
 
395
386
  /* enforce maxTableLog */
396
- maxNbBits = HUF_setMaxHeight(huffNode, nonNullRank, maxNbBits);
387
+ maxNbBits = HUF_setMaxHeight(huffNode, (U32)nonNullRank, maxNbBits);
397
388
 
398
389
  /* fill result into tree (val, nbBits) */
399
390
  { U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
400
391
  U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
392
+ int const alphabetSize = (int)(maxSymbolValue + 1);
401
393
  if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */
402
394
  for (n=0; n<=nonNullRank; n++)
403
395
  nbPerRank[huffNode[n].nbBits]++;
404
396
  /* determine stating value per rank */
405
397
  { U16 min = 0;
406
- for (n=maxNbBits; n>0; n--) {
398
+ for (n=(int)maxNbBits; n>0; n--) {
407
399
  valPerRank[n] = min; /* get starting value within each rank */
408
400
  min += nbPerRank[n];
409
401
  min >>= 1;
410
402
  } }
411
- for (n=0; n<=maxSymbolValue; n++)
403
+ for (n=0; n<alphabetSize; n++)
412
404
  tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
413
- for (n=0; n<=maxSymbolValue; n++)
405
+ for (n=0; n<alphabetSize; n++)
414
406
  tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */
415
407
  }
416
408
 
@@ -423,11 +415,11 @@ size_t HUF_buildCTable_wksp (HUF_CElt* tree, const unsigned* count, U32 maxSymbo
423
415
  */
424
416
  size_t HUF_buildCTable (HUF_CElt* tree, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits)
425
417
  {
426
- huffNodeTable nodeTable;
427
- return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, nodeTable, sizeof(nodeTable));
418
+ HUF_buildCTable_wksp_tables workspace;
419
+ return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, &workspace, sizeof(workspace));
428
420
  }
429
421
 
430
- static size_t HUF_estimateCompressedSize(HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue)
422
+ size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue)
431
423
  {
432
424
  size_t nbBits = 0;
433
425
  int s;
@@ -437,7 +429,7 @@ static size_t HUF_estimateCompressedSize(HUF_CElt* CTable, const unsigned* count
437
429
  return nbBits >> 3;
438
430
  }
439
431
 
440
- static int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) {
432
+ int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue) {
441
433
  int bad = 0;
442
434
  int s;
443
435
  for (s = 0; s <= (int)maxSymbolValue; ++s) {
@@ -476,7 +468,7 @@ HUF_compress1X_usingCTable_internal_body(void* dst, size_t dstSize,
476
468
 
477
469
  /* init */
478
470
  if (dstSize < 8) return 0; /* not enough space to compress */
479
- { size_t const initErr = BIT_initCStream(&bitC, op, oend-op);
471
+ { size_t const initErr = BIT_initCStream(&bitC, op, (size_t)(oend-op));
480
472
  if (HUF_isError(initErr)) return 0; }
481
473
 
482
474
  n = srcSize & ~3; /* join to mod 4 */
@@ -573,7 +565,8 @@ HUF_compress4X_usingCTable_internal(void* dst, size_t dstSize,
573
565
  if (srcSize < 12) return 0; /* no saving possible : too small input */
574
566
  op += 6; /* jumpTable */
575
567
 
576
- { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, oend-op, ip, segmentSize, CTable, bmi2) );
568
+ assert(op <= oend);
569
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) );
577
570
  if (cSize==0) return 0;
578
571
  assert(cSize <= 65535);
579
572
  MEM_writeLE16(ostart, (U16)cSize);
@@ -581,7 +574,8 @@ HUF_compress4X_usingCTable_internal(void* dst, size_t dstSize,
581
574
  }
582
575
 
583
576
  ip += segmentSize;
584
- { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, oend-op, ip, segmentSize, CTable, bmi2) );
577
+ assert(op <= oend);
578
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) );
585
579
  if (cSize==0) return 0;
586
580
  assert(cSize <= 65535);
587
581
  MEM_writeLE16(ostart+2, (U16)cSize);
@@ -589,7 +583,8 @@ HUF_compress4X_usingCTable_internal(void* dst, size_t dstSize,
589
583
  }
590
584
 
591
585
  ip += segmentSize;
592
- { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, oend-op, ip, segmentSize, CTable, bmi2) );
586
+ assert(op <= oend);
587
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, segmentSize, CTable, bmi2) );
593
588
  if (cSize==0) return 0;
594
589
  assert(cSize <= 65535);
595
590
  MEM_writeLE16(ostart+4, (U16)cSize);
@@ -597,12 +592,14 @@ HUF_compress4X_usingCTable_internal(void* dst, size_t dstSize,
597
592
  }
598
593
 
599
594
  ip += segmentSize;
600
- { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, oend-op, ip, iend-ip, CTable, bmi2) );
595
+ assert(op <= oend);
596
+ assert(ip <= iend);
597
+ { CHECK_V_F(cSize, HUF_compress1X_usingCTable_internal(op, (size_t)(oend-op), ip, (size_t)(iend-ip), CTable, bmi2) );
601
598
  if (cSize==0) return 0;
602
599
  op += cSize;
603
600
  }
604
601
 
605
- return op-ostart;
602
+ return (size_t)(op-ostart);
606
603
  }
607
604
 
608
605
  size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
@@ -618,20 +615,21 @@ static size_t HUF_compressCTable_internal(
618
615
  HUF_nbStreams_e nbStreams, const HUF_CElt* CTable, const int bmi2)
619
616
  {
620
617
  size_t const cSize = (nbStreams==HUF_singleStream) ?
621
- HUF_compress1X_usingCTable_internal(op, oend - op, src, srcSize, CTable, bmi2) :
622
- HUF_compress4X_usingCTable_internal(op, oend - op, src, srcSize, CTable, bmi2);
618
+ HUF_compress1X_usingCTable_internal(op, (size_t)(oend - op), src, srcSize, CTable, bmi2) :
619
+ HUF_compress4X_usingCTable_internal(op, (size_t)(oend - op), src, srcSize, CTable, bmi2);
623
620
  if (HUF_isError(cSize)) { return cSize; }
624
621
  if (cSize==0) { return 0; } /* uncompressible */
625
622
  op += cSize;
626
623
  /* check compressibility */
624
+ assert(op >= ostart);
627
625
  if ((size_t)(op-ostart) >= srcSize-1) { return 0; }
628
- return op-ostart;
626
+ return (size_t)(op-ostart);
629
627
  }
630
628
 
631
629
  typedef struct {
632
630
  unsigned count[HUF_SYMBOLVALUE_MAX + 1];
633
631
  HUF_CElt CTable[HUF_SYMBOLVALUE_MAX + 1];
634
- huffNodeTable nodeTable;
632
+ HUF_buildCTable_wksp_tables buildCTable_wksp;
635
633
  } HUF_compress_tables_t;
636
634
 
637
635
  /* HUF_compress_internal() :
@@ -650,6 +648,8 @@ HUF_compress_internal (void* dst, size_t dstSize,
650
648
  BYTE* const oend = ostart + dstSize;
651
649
  BYTE* op = ostart;
652
650
 
651
+ HUF_STATIC_ASSERT(sizeof(*table) <= HUF_WORKSPACE_SIZE);
652
+
653
653
  /* checks & inits */
654
654
  if (((size_t)workSpace & 3) != 0) return ERROR(GENERIC); /* must be aligned on 4-bytes boundaries */
655
655
  if (wkspSize < HUF_WORKSPACE_SIZE) return ERROR(workSpace_tooSmall);
@@ -691,7 +691,7 @@ HUF_compress_internal (void* dst, size_t dstSize,
691
691
  huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue);
692
692
  { size_t const maxBits = HUF_buildCTable_wksp(table->CTable, table->count,
693
693
  maxSymbolValue, huffLog,
694
- table->nodeTable, sizeof(table->nodeTable));
694
+ &table->buildCTable_wksp, sizeof(table->buildCTable_wksp));
695
695
  CHECK_F(maxBits);
696
696
  huffLog = (U32)maxBits;
697
697
  /* Zero unused symbols in CTable, so we can check it for validity */