zstd-ruby 1.4.4.0 → 1.5.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/README.md +78 -5
  4. data/Rakefile +8 -2
  5. data/ext/zstdruby/common.h +15 -0
  6. data/ext/zstdruby/extconf.rb +3 -2
  7. data/ext/zstdruby/libzstd/common/allocations.h +55 -0
  8. data/ext/zstdruby/libzstd/common/bits.h +200 -0
  9. data/ext/zstdruby/libzstd/common/bitstream.h +74 -97
  10. data/ext/zstdruby/libzstd/common/compiler.h +219 -20
  11. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  12. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  13. data/ext/zstdruby/libzstd/common/debug.h +22 -49
  14. data/ext/zstdruby/libzstd/common/entropy_common.c +184 -80
  15. data/ext/zstdruby/libzstd/common/error_private.c +11 -2
  16. data/ext/zstdruby/libzstd/common/error_private.h +87 -4
  17. data/ext/zstdruby/libzstd/common/fse.h +47 -116
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +127 -127
  19. data/ext/zstdruby/libzstd/common/huf.h +112 -197
  20. data/ext/zstdruby/libzstd/common/mem.h +124 -142
  21. data/ext/zstdruby/libzstd/common/pool.c +54 -27
  22. data/ext/zstdruby/libzstd/common/pool.h +11 -5
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +156 -0
  24. data/ext/zstdruby/libzstd/common/threading.c +78 -22
  25. data/ext/zstdruby/libzstd/common/threading.h +9 -13
  26. data/ext/zstdruby/libzstd/common/xxhash.c +15 -873
  27. data/ext/zstdruby/libzstd/common/xxhash.h +5572 -191
  28. data/ext/zstdruby/libzstd/common/zstd_common.c +2 -37
  29. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  30. data/ext/zstdruby/libzstd/common/zstd_internal.h +186 -144
  31. data/ext/zstdruby/libzstd/common/zstd_trace.h +163 -0
  32. data/ext/zstdruby/libzstd/compress/clevels.h +134 -0
  33. data/ext/zstdruby/libzstd/compress/fse_compress.c +99 -196
  34. data/ext/zstdruby/libzstd/compress/hist.c +41 -63
  35. data/ext/zstdruby/libzstd/compress/hist.h +13 -33
  36. data/ext/zstdruby/libzstd/compress/huf_compress.c +968 -331
  37. data/ext/zstdruby/libzstd/compress/zstd_compress.c +4120 -1191
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +688 -159
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +121 -40
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +16 -6
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +62 -35
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +10 -3
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +577 -0
  44. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  45. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +322 -115
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +394 -154
  47. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +4 -3
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.c +729 -253
  49. data/ext/zstdruby/libzstd/compress/zstd_fast.h +4 -3
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1289 -247
  51. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +61 -1
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +339 -212
  53. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +15 -3
  54. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +106 -0
  55. data/ext/zstdruby/libzstd/compress/zstd_opt.c +508 -282
  56. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +217 -466
  58. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +35 -114
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1220 -572
  60. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +576 -0
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +23 -19
  62. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +3 -3
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +859 -273
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +1244 -375
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +21 -7
  66. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +74 -11
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.c +75 -54
  68. data/ext/zstdruby/libzstd/dictBuilder/cover.h +20 -9
  69. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +55 -36
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +126 -110
  72. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +248 -56
  73. data/ext/zstdruby/libzstd/zstd.h +1277 -306
  74. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +29 -8
  75. data/ext/zstdruby/main.c +20 -0
  76. data/ext/zstdruby/skippable_frame.c +63 -0
  77. data/ext/zstdruby/streaming_compress.c +177 -0
  78. data/ext/zstdruby/streaming_compress.h +5 -0
  79. data/ext/zstdruby/streaming_decompress.c +123 -0
  80. data/ext/zstdruby/zstdruby.c +114 -32
  81. data/lib/zstd-ruby/version.rb +1 -1
  82. data/lib/zstd-ruby.rb +0 -1
  83. data/zstd-ruby.gemspec +1 -1
  84. metadata +24 -39
  85. data/.travis.yml +0 -14
  86. data/ext/zstdruby/libzstd/.gitignore +0 -3
  87. data/ext/zstdruby/libzstd/BUCK +0 -234
  88. data/ext/zstdruby/libzstd/Makefile +0 -289
  89. data/ext/zstdruby/libzstd/README.md +0 -159
  90. data/ext/zstdruby/libzstd/deprecated/zbuff.h +0 -214
  91. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +0 -26
  92. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +0 -147
  93. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +0 -75
  94. data/ext/zstdruby/libzstd/dll/example/Makefile +0 -47
  95. data/ext/zstdruby/libzstd/dll/example/README.md +0 -69
  96. data/ext/zstdruby/libzstd/dll/example/build_package.bat +0 -20
  97. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +0 -25
  98. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +0 -181
  99. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +0 -415
  100. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +0 -2152
  101. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +0 -94
  102. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +0 -3514
  103. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +0 -93
  104. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +0 -3156
  105. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +0 -93
  106. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +0 -3641
  107. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +0 -142
  108. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +0 -4046
  109. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +0 -162
  110. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +0 -4150
  111. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +0 -172
  112. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +0 -4533
  113. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +0 -187
  114. data/ext/zstdruby/libzstd/libzstd.pc.in +0 -15
  115. data/ext/zstdruby/zstdruby.h +0 -6
@@ -1,35 +1,15 @@
1
1
  /* ******************************************************************
2
- debug
3
- Part of FSE library
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
- - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
2
+ * debug
3
+ * Part of FSE library
4
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
5
+ *
6
+ * You can contact the author at :
7
+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
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
 
@@ -71,15 +51,6 @@ extern "C" {
71
51
  #endif
72
52
 
73
53
 
74
- /* DEBUGFILE can be defined externally,
75
- * typically through compiler command line.
76
- * note : currently useless.
77
- * Value must be stderr or stdout */
78
- #ifndef DEBUGFILE
79
- # define DEBUGFILE stderr
80
- #endif
81
-
82
-
83
54
  /* recommended values for DEBUGLEVEL :
84
55
  * 0 : release mode, no debug, all run-time checks disabled
85
56
  * 1 : enables assert() only, no display
@@ -96,7 +67,8 @@ extern "C" {
96
67
  */
97
68
 
98
69
  #if (DEBUGLEVEL>=1)
99
- # include <assert.h>
70
+ # define ZSTD_DEPS_NEED_ASSERT
71
+ # include "zstd_deps.h"
100
72
  #else
101
73
  # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
102
74
  # define assert(condition) ((void)0) /* disable assert (default) */
@@ -104,7 +76,8 @@ extern "C" {
104
76
  #endif
105
77
 
106
78
  #if (DEBUGLEVEL>=2)
107
- # include <stdio.h>
79
+ # define ZSTD_DEPS_NEED_IO
80
+ # include "zstd_deps.h"
108
81
  extern int g_debuglevel; /* the variable is only declared,
109
82
  it actually lives in debug.c,
110
83
  and is shared by the whole process.
@@ -112,14 +85,14 @@ extern int g_debuglevel; /* the variable is only declared,
112
85
  It's useful when enabling very verbose levels
113
86
  on selective conditions (such as position in src) */
114
87
 
115
- # define RAWLOG(l, ...) { \
116
- if (l<=g_debuglevel) { \
117
- fprintf(stderr, __VA_ARGS__); \
88
+ # define RAWLOG(l, ...) { \
89
+ if (l<=g_debuglevel) { \
90
+ ZSTD_DEBUG_PRINT(__VA_ARGS__); \
118
91
  } }
119
- # define DEBUGLOG(l, ...) { \
120
- if (l<=g_debuglevel) { \
121
- fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
122
- fprintf(stderr, " \n"); \
92
+ # define DEBUGLOG(l, ...) { \
93
+ if (l<=g_debuglevel) { \
94
+ ZSTD_DEBUG_PRINT(__FILE__ ": " __VA_ARGS__); \
95
+ ZSTD_DEBUG_PRINT(" \n"); \
123
96
  } }
124
97
  #else
125
98
  # define RAWLOG(l, ...) {} /* disabled */
@@ -1,36 +1,16 @@
1
- /*
2
- Common functions of New Generation Entropy library
3
- Copyright (C) 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
- *************************************************************************** */
1
+ /* ******************************************************************
2
+ * Common functions of New Generation Entropy library
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
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.
13
+ ****************************************************************** */
34
14
 
35
15
  /* *************************************
36
16
  * Dependencies
@@ -39,8 +19,8 @@
39
19
  #include "error_private.h" /* ERR_*, ERROR */
40
20
  #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
41
21
  #include "fse.h"
42
- #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
43
22
  #include "huf.h"
23
+ #include "bits.h" /* ZSDT_highbit32, ZSTD_countTrailingZeros32 */
44
24
 
45
25
 
46
26
  /*=== Version ===*/
@@ -58,8 +38,9 @@ const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
58
38
  /*-**************************************************************
59
39
  * FSE NCount encoding-decoding
60
40
  ****************************************************************/
61
- size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
62
- const void* headerBuffer, size_t hbSize)
41
+ FORCE_INLINE_TEMPLATE
42
+ size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
43
+ const void* headerBuffer, size_t hbSize)
63
44
  {
64
45
  const BYTE* const istart = (const BYTE*) headerBuffer;
65
46
  const BYTE* const iend = istart + hbSize;
@@ -70,23 +51,23 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
70
51
  U32 bitStream;
71
52
  int bitCount;
72
53
  unsigned charnum = 0;
54
+ unsigned const maxSV1 = *maxSVPtr + 1;
73
55
  int previous0 = 0;
74
56
 
75
- if (hbSize < 4) {
76
- /* This function only works when hbSize >= 4 */
77
- char buffer[4];
78
- memset(buffer, 0, sizeof(buffer));
79
- memcpy(buffer, headerBuffer, hbSize);
57
+ if (hbSize < 8) {
58
+ /* This function only works when hbSize >= 8 */
59
+ char buffer[8] = {0};
60
+ ZSTD_memcpy(buffer, headerBuffer, hbSize);
80
61
  { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
81
62
  buffer, sizeof(buffer));
82
63
  if (FSE_isError(countSize)) return countSize;
83
64
  if (countSize > hbSize) return ERROR(corruption_detected);
84
65
  return countSize;
85
66
  } }
86
- assert(hbSize >= 4);
67
+ assert(hbSize >= 8);
87
68
 
88
69
  /* init */
89
- memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
70
+ ZSTD_memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
90
71
  bitStream = MEM_readLE32(ip);
91
72
  nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
92
73
  if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
@@ -97,36 +78,58 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
97
78
  threshold = 1<<nbBits;
98
79
  nbBits++;
99
80
 
100
- while ((remaining>1) & (charnum<=*maxSVPtr)) {
81
+ for (;;) {
101
82
  if (previous0) {
102
- unsigned n0 = charnum;
103
- while ((bitStream & 0xFFFF) == 0xFFFF) {
104
- n0 += 24;
105
- if (ip < iend-5) {
106
- ip += 2;
107
- bitStream = MEM_readLE32(ip) >> bitCount;
83
+ /* Count the number of repeats. Each time the
84
+ * 2-bit repeat code is 0b11 there is another
85
+ * repeat.
86
+ * Avoid UB by setting the high bit to 1.
87
+ */
88
+ int repeats = ZSTD_countTrailingZeros32(~bitStream | 0x80000000) >> 1;
89
+ while (repeats >= 12) {
90
+ charnum += 3 * 12;
91
+ if (LIKELY(ip <= iend-7)) {
92
+ ip += 3;
108
93
  } else {
109
- bitStream >>= 16;
110
- bitCount += 16;
111
- } }
112
- while ((bitStream & 3) == 3) {
113
- n0 += 3;
114
- bitStream >>= 2;
115
- bitCount += 2;
94
+ bitCount -= (int)(8 * (iend - 7 - ip));
95
+ bitCount &= 31;
96
+ ip = iend - 4;
97
+ }
98
+ bitStream = MEM_readLE32(ip) >> bitCount;
99
+ repeats = ZSTD_countTrailingZeros32(~bitStream | 0x80000000) >> 1;
116
100
  }
117
- n0 += bitStream & 3;
101
+ charnum += 3 * repeats;
102
+ bitStream >>= 2 * repeats;
103
+ bitCount += 2 * repeats;
104
+
105
+ /* Add the final repeat which isn't 0b11. */
106
+ assert((bitStream & 3) < 3);
107
+ charnum += bitStream & 3;
118
108
  bitCount += 2;
119
- if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
120
- while (charnum < n0) normalizedCounter[charnum++] = 0;
121
- if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
109
+
110
+ /* This is an error, but break and return an error
111
+ * at the end, because returning out of a loop makes
112
+ * it harder for the compiler to optimize.
113
+ */
114
+ if (charnum >= maxSV1) break;
115
+
116
+ /* We don't need to set the normalized count to 0
117
+ * because we already memset the whole buffer to 0.
118
+ */
119
+
120
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
122
121
  assert((bitCount >> 3) <= 3); /* For first condition to work */
123
122
  ip += bitCount>>3;
124
123
  bitCount &= 7;
125
- bitStream = MEM_readLE32(ip) >> bitCount;
126
124
  } else {
127
- bitStream >>= 2;
128
- } }
129
- { int const max = (2*threshold-1) - remaining;
125
+ bitCount -= (int)(8 * (iend - 4 - ip));
126
+ bitCount &= 31;
127
+ ip = iend - 4;
128
+ }
129
+ bitStream = MEM_readLE32(ip) >> bitCount;
130
+ }
131
+ {
132
+ int const max = (2*threshold-1) - remaining;
130
133
  int count;
131
134
 
132
135
  if ((bitStream & (threshold-1)) < (U32)max) {
@@ -139,24 +142,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
139
142
  }
140
143
 
141
144
  count--; /* extra accuracy */
142
- remaining -= count < 0 ? -count : count; /* -1 means +1 */
145
+ /* When it matters (small blocks), this is a
146
+ * predictable branch, because we don't use -1.
147
+ */
148
+ if (count >= 0) {
149
+ remaining -= count;
150
+ } else {
151
+ assert(count == -1);
152
+ remaining += count;
153
+ }
143
154
  normalizedCounter[charnum++] = (short)count;
144
155
  previous0 = !count;
145
- while (remaining < threshold) {
146
- nbBits--;
147
- threshold >>= 1;
156
+
157
+ assert(threshold > 1);
158
+ if (remaining < threshold) {
159
+ /* This branch can be folded into the
160
+ * threshold update condition because we
161
+ * know that threshold > 1.
162
+ */
163
+ if (remaining <= 1) break;
164
+ nbBits = ZSTD_highbit32(remaining) + 1;
165
+ threshold = 1 << (nbBits - 1);
148
166
  }
167
+ if (charnum >= maxSV1) break;
149
168
 
150
- if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
169
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
151
170
  ip += bitCount>>3;
152
171
  bitCount &= 7;
153
172
  } else {
154
173
  bitCount -= (int)(8 * (iend - 4 - ip));
174
+ bitCount &= 31;
155
175
  ip = iend - 4;
156
176
  }
157
- bitStream = MEM_readLE32(ip) >> (bitCount & 31);
158
- } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
177
+ bitStream = MEM_readLE32(ip) >> bitCount;
178
+ } }
159
179
  if (remaining != 1) return ERROR(corruption_detected);
180
+ /* Only possible when there are too many zeros. */
181
+ if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
160
182
  if (bitCount > 32) return ERROR(corruption_detected);
161
183
  *maxSVPtr = charnum-1;
162
184
 
@@ -164,6 +186,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
164
186
  return ip-istart;
165
187
  }
166
188
 
189
+ /* Avoids the FORCE_INLINE of the _body() function. */
190
+ static size_t FSE_readNCount_body_default(
191
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
192
+ const void* headerBuffer, size_t hbSize)
193
+ {
194
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
195
+ }
196
+
197
+ #if DYNAMIC_BMI2
198
+ BMI2_TARGET_ATTRIBUTE static size_t FSE_readNCount_body_bmi2(
199
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
200
+ const void* headerBuffer, size_t hbSize)
201
+ {
202
+ return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
203
+ }
204
+ #endif
205
+
206
+ size_t FSE_readNCount_bmi2(
207
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
208
+ const void* headerBuffer, size_t hbSize, int bmi2)
209
+ {
210
+ #if DYNAMIC_BMI2
211
+ if (bmi2) {
212
+ return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
213
+ }
214
+ #endif
215
+ (void)bmi2;
216
+ return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
217
+ }
218
+
219
+ size_t FSE_readNCount(
220
+ short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
221
+ const void* headerBuffer, size_t hbSize)
222
+ {
223
+ return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0);
224
+ }
225
+
167
226
 
168
227
  /*! HUF_readStats() :
169
228
  Read compact Huffman tree, saved by HUF_writeCTable().
@@ -175,6 +234,17 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
175
234
  size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
176
235
  U32* nbSymbolsPtr, U32* tableLogPtr,
177
236
  const void* src, size_t srcSize)
237
+ {
238
+ U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
239
+ return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* flags */ 0);
240
+ }
241
+
242
+ FORCE_INLINE_TEMPLATE size_t
243
+ HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats,
244
+ U32* nbSymbolsPtr, U32* tableLogPtr,
245
+ const void* src, size_t srcSize,
246
+ void* workSpace, size_t wkspSize,
247
+ int bmi2)
178
248
  {
179
249
  U32 weightTotal;
180
250
  const BYTE* ip = (const BYTE*) src;
@@ -183,7 +253,7 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
183
253
 
184
254
  if (!srcSize) return ERROR(srcSize_wrong);
185
255
  iSize = ip[0];
186
- /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
256
+ /* ZSTD_memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
187
257
 
188
258
  if (iSize >= 128) { /* special header */
189
259
  oSize = iSize - 127;
@@ -197,31 +267,31 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
197
267
  huffWeight[n+1] = ip[n/2] & 15;
198
268
  } } }
199
269
  else { /* header compressed with FSE (normal case) */
200
- FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
201
270
  if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
202
- oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */
271
+ /* max (hwSize-1) values decoded, as last one is implied */
272
+ oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize, bmi2);
203
273
  if (FSE_isError(oSize)) return oSize;
204
274
  }
205
275
 
206
276
  /* collect weight stats */
207
- memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
277
+ ZSTD_memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
208
278
  weightTotal = 0;
209
279
  { U32 n; for (n=0; n<oSize; n++) {
210
- if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
280
+ if (huffWeight[n] > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
211
281
  rankStats[huffWeight[n]]++;
212
282
  weightTotal += (1 << huffWeight[n]) >> 1;
213
283
  } }
214
284
  if (weightTotal == 0) return ERROR(corruption_detected);
215
285
 
216
286
  /* get last non-null symbol weight (implied, total must be 2^n) */
217
- { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
287
+ { U32 const tableLog = ZSTD_highbit32(weightTotal) + 1;
218
288
  if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
219
289
  *tableLogPtr = tableLog;
220
290
  /* determine last weight */
221
291
  { U32 const total = 1 << tableLog;
222
292
  U32 const rest = total - weightTotal;
223
- U32 const verif = 1 << BIT_highbit32(rest);
224
- U32 const lastWeight = BIT_highbit32(rest) + 1;
293
+ U32 const verif = 1 << ZSTD_highbit32(rest);
294
+ U32 const lastWeight = ZSTD_highbit32(rest) + 1;
225
295
  if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
226
296
  huffWeight[oSize] = (BYTE)lastWeight;
227
297
  rankStats[lastWeight]++;
@@ -234,3 +304,37 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
234
304
  *nbSymbolsPtr = (U32)(oSize+1);
235
305
  return iSize+1;
236
306
  }
307
+
308
+ /* Avoids the FORCE_INLINE of the _body() function. */
309
+ static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats,
310
+ U32* nbSymbolsPtr, U32* tableLogPtr,
311
+ const void* src, size_t srcSize,
312
+ void* workSpace, size_t wkspSize)
313
+ {
314
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
315
+ }
316
+
317
+ #if DYNAMIC_BMI2
318
+ static BMI2_TARGET_ATTRIBUTE size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats,
319
+ U32* nbSymbolsPtr, U32* tableLogPtr,
320
+ const void* src, size_t srcSize,
321
+ void* workSpace, size_t wkspSize)
322
+ {
323
+ return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
324
+ }
325
+ #endif
326
+
327
+ size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
328
+ U32* nbSymbolsPtr, U32* tableLogPtr,
329
+ const void* src, size_t srcSize,
330
+ void* workSpace, size_t wkspSize,
331
+ int flags)
332
+ {
333
+ #if DYNAMIC_BMI2
334
+ if (flags & HUF_flags_bmi2) {
335
+ return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
336
+ }
337
+ #endif
338
+ (void)flags;
339
+ return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
340
+ }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -27,9 +27,11 @@ const char* ERR_getErrorString(ERR_enum code)
27
27
  case PREFIX(version_unsupported): return "Version not supported";
28
28
  case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
29
29
  case PREFIX(frameParameter_windowTooLarge): return "Frame requires too much memory for decoding";
30
- case PREFIX(corruption_detected): return "Corrupted block detected";
30
+ case PREFIX(corruption_detected): return "Data corruption detected";
31
31
  case PREFIX(checksum_wrong): return "Restored data doesn't match checksum";
32
+ case PREFIX(literals_headerWrong): return "Header of Literals' block doesn't respect format specification";
32
33
  case PREFIX(parameter_unsupported): return "Unsupported parameter";
34
+ case PREFIX(parameter_combination_unsupported): return "Unsupported combination of parameters";
33
35
  case PREFIX(parameter_outOfBound): return "Parameter is out of bound";
34
36
  case PREFIX(init_missing): return "Context should be init first";
35
37
  case PREFIX(memory_allocation): return "Allocation error : not enough memory";
@@ -38,15 +40,22 @@ const char* ERR_getErrorString(ERR_enum code)
38
40
  case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
39
41
  case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
40
42
  case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
43
+ case PREFIX(stabilityCondition_notRespected): return "pledged buffer stability condition is not respected";
41
44
  case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
42
45
  case PREFIX(dictionary_wrong): return "Dictionary mismatch";
43
46
  case PREFIX(dictionaryCreation_failed): return "Cannot create Dictionary from provided samples";
44
47
  case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
45
48
  case PREFIX(srcSize_wrong): return "Src size is incorrect";
46
49
  case PREFIX(dstBuffer_null): return "Operation on NULL destination buffer";
50
+ case PREFIX(noForwardProgress_destFull): return "Operation made no progress over multiple calls, due to output buffer being full";
51
+ case PREFIX(noForwardProgress_inputEmpty): return "Operation made no progress over multiple calls, due to input being empty";
47
52
  /* following error codes are not stable and may be removed or changed in a future version */
48
53
  case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
49
54
  case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
55
+ case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
56
+ case PREFIX(srcBuffer_wrong): return "Source buffer is wrong";
57
+ case PREFIX(sequenceProducer_failed): return "Block-level external sequence producer returned an error code";
58
+ case PREFIX(externalSequences_invalid): return "External sequences are not valid";
50
59
  case PREFIX(maxCode):
51
60
  default: return notErrorCode;
52
61
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -21,8 +21,10 @@ extern "C" {
21
21
  /* ****************************************
22
22
  * Dependencies
23
23
  ******************************************/
24
- #include <stddef.h> /* size_t */
25
- #include "zstd_errors.h" /* enum list */
24
+ #include "../zstd_errors.h" /* enum list */
25
+ #include "compiler.h"
26
+ #include "debug.h"
27
+ #include "zstd_deps.h" /* size_t */
26
28
 
27
29
 
28
30
  /* ****************************************
@@ -49,7 +51,7 @@ typedef ZSTD_ErrorCode ERR_enum;
49
51
  /*-****************************************
50
52
  * Error codes handling
51
53
  ******************************************/
52
- #undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
54
+ #undef ERROR /* already defined on Visual Studio */
53
55
  #define ERROR(name) ZSTD_ERROR(name)
54
56
  #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
55
57
 
@@ -57,6 +59,10 @@ ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
57
59
 
58
60
  ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
59
61
 
62
+ /* check and forward error code */
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
+
60
66
 
61
67
  /*-****************************************
62
68
  * Error Strings
@@ -69,6 +75,83 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
69
75
  return ERR_getErrorString(ERR_getErrorCode(code));
70
76
  }
71
77
 
78
+ /**
79
+ * Ignore: this is an internal helper.
80
+ *
81
+ * This is a helper function to help force C99-correctness during compilation.
82
+ * Under strict compilation modes, variadic macro arguments can't be empty.
83
+ * However, variadic function arguments can be. Using a function therefore lets
84
+ * us statically check that at least one (string) argument was passed,
85
+ * independent of the compilation flags.
86
+ */
87
+ static INLINE_KEYWORD UNUSED_ATTR
88
+ void _force_has_format_string(const char *format, ...) {
89
+ (void)format;
90
+ }
91
+
92
+ /**
93
+ * Ignore: this is an internal helper.
94
+ *
95
+ * We want to force this function invocation to be syntactically correct, but
96
+ * we don't want to force runtime evaluation of its arguments.
97
+ */
98
+ #define _FORCE_HAS_FORMAT_STRING(...) \
99
+ if (0) { \
100
+ _force_has_format_string(__VA_ARGS__); \
101
+ }
102
+
103
+ #define ERR_QUOTE(str) #str
104
+
105
+ /**
106
+ * Return the specified error if the condition evaluates to true.
107
+ *
108
+ * In debug modes, prints additional information.
109
+ * In order to do that (particularly, printing the conditional that failed),
110
+ * this can't just wrap RETURN_ERROR().
111
+ */
112
+ #define RETURN_ERROR_IF(cond, err, ...) \
113
+ if (cond) { \
114
+ RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
115
+ __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
116
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
117
+ RAWLOG(3, ": " __VA_ARGS__); \
118
+ RAWLOG(3, "\n"); \
119
+ return ERROR(err); \
120
+ }
121
+
122
+ /**
123
+ * Unconditionally return the specified error.
124
+ *
125
+ * In debug modes, prints additional information.
126
+ */
127
+ #define RETURN_ERROR(err, ...) \
128
+ do { \
129
+ RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
130
+ __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
131
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
132
+ RAWLOG(3, ": " __VA_ARGS__); \
133
+ RAWLOG(3, "\n"); \
134
+ return ERROR(err); \
135
+ } while(0);
136
+
137
+ /**
138
+ * If the provided expression evaluates to an error code, returns that error code.
139
+ *
140
+ * In debug modes, prints additional information.
141
+ */
142
+ #define FORWARD_IF_ERROR(err, ...) \
143
+ do { \
144
+ size_t const err_code = (err); \
145
+ if (ERR_isError(err_code)) { \
146
+ RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
147
+ __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
148
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
149
+ RAWLOG(3, ": " __VA_ARGS__); \
150
+ RAWLOG(3, "\n"); \
151
+ return err_code; \
152
+ } \
153
+ } while(0);
154
+
72
155
  #if defined (__cplusplus)
73
156
  }
74
157
  #endif