zstd-ruby 1.4.5.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 +45 -62
  10. data/ext/zstdruby/libzstd/common/compiler.h +205 -22
  11. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  12. data/ext/zstdruby/libzstd/common/debug.c +1 -1
  13. data/ext/zstdruby/libzstd/common/debug.h +12 -19
  14. data/ext/zstdruby/libzstd/common/entropy_common.c +172 -48
  15. data/ext/zstdruby/libzstd/common/error_private.c +10 -2
  16. data/ext/zstdruby/libzstd/common/error_private.h +82 -3
  17. data/ext/zstdruby/libzstd/common/fse.h +37 -86
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +117 -92
  19. data/ext/zstdruby/libzstd/common/huf.h +99 -166
  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 +10 -4
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +156 -0
  24. data/ext/zstdruby/libzstd/common/threading.c +74 -19
  25. data/ext/zstdruby/libzstd/common/threading.h +5 -10
  26. data/ext/zstdruby/libzstd/common/xxhash.c +7 -847
  27. data/ext/zstdruby/libzstd/common/xxhash.h +5568 -167
  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 +132 -187
  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 +83 -157
  34. data/ext/zstdruby/libzstd/compress/hist.c +27 -29
  35. data/ext/zstdruby/libzstd/compress/hist.h +2 -2
  36. data/ext/zstdruby/libzstd/compress/huf_compress.c +916 -279
  37. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3773 -1019
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +610 -203
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +119 -42
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +16 -6
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +42 -19
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +1 -1
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +49 -317
  44. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +1 -1
  45. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +320 -103
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +388 -151
  47. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +3 -2
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.c +729 -265
  49. data/ext/zstdruby/libzstd/compress/zstd_fast.h +3 -2
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1270 -251
  51. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +61 -1
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +324 -219
  53. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +9 -2
  54. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +106 -0
  55. data/ext/zstdruby/libzstd/compress/zstd_opt.c +481 -209
  56. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +181 -457
  58. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +34 -113
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +1199 -565
  60. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +576 -0
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +12 -12
  62. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +627 -157
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +1086 -326
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +19 -5
  66. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +62 -13
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.c +73 -52
  68. data/ext/zstdruby/libzstd/dictBuilder/cover.h +7 -6
  69. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1 -1
  70. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +44 -35
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +103 -111
  72. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +203 -34
  73. data/ext/zstdruby/libzstd/zstd.h +1217 -287
  74. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +28 -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 +19 -36
  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 -354
  89. data/ext/zstdruby/libzstd/README.md +0 -179
  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 -48
  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 -2158
  101. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +0 -94
  102. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +0 -3518
  103. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +0 -93
  104. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +0 -3160
  105. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +0 -93
  106. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +0 -3647
  107. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +0 -142
  108. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +0 -4050
  109. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +0 -162
  110. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +0 -4154
  111. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +0 -172
  112. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +0 -4541
  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,6 +1,6 @@
1
1
  /* ******************************************************************
2
2
  * Common functions of New Generation Entropy library
3
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
4
  *
5
5
  * You can contact the author at :
6
6
  * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -19,8 +19,8 @@
19
19
  #include "error_private.h" /* ERR_*, ERROR */
20
20
  #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
21
21
  #include "fse.h"
22
- #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
23
22
  #include "huf.h"
23
+ #include "bits.h" /* ZSDT_highbit32, ZSTD_countTrailingZeros32 */
24
24
 
25
25
 
26
26
  /*=== Version ===*/
@@ -38,8 +38,9 @@ const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
38
38
  /*-**************************************************************
39
39
  * FSE NCount encoding-decoding
40
40
  ****************************************************************/
41
- size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
42
- 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)
43
44
  {
44
45
  const BYTE* const istart = (const BYTE*) headerBuffer;
45
46
  const BYTE* const iend = istart + hbSize;
@@ -50,23 +51,23 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
50
51
  U32 bitStream;
51
52
  int bitCount;
52
53
  unsigned charnum = 0;
54
+ unsigned const maxSV1 = *maxSVPtr + 1;
53
55
  int previous0 = 0;
54
56
 
55
- if (hbSize < 4) {
56
- /* This function only works when hbSize >= 4 */
57
- char buffer[4];
58
- memset(buffer, 0, sizeof(buffer));
59
- 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);
60
61
  { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
61
62
  buffer, sizeof(buffer));
62
63
  if (FSE_isError(countSize)) return countSize;
63
64
  if (countSize > hbSize) return ERROR(corruption_detected);
64
65
  return countSize;
65
66
  } }
66
- assert(hbSize >= 4);
67
+ assert(hbSize >= 8);
67
68
 
68
69
  /* init */
69
- 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 */
70
71
  bitStream = MEM_readLE32(ip);
71
72
  nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
72
73
  if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
@@ -77,36 +78,58 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
77
78
  threshold = 1<<nbBits;
78
79
  nbBits++;
79
80
 
80
- while ((remaining>1) & (charnum<=*maxSVPtr)) {
81
+ for (;;) {
81
82
  if (previous0) {
82
- unsigned n0 = charnum;
83
- while ((bitStream & 0xFFFF) == 0xFFFF) {
84
- n0 += 24;
85
- if (ip < iend-5) {
86
- ip += 2;
87
- 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;
88
93
  } else {
89
- bitStream >>= 16;
90
- bitCount += 16;
91
- } }
92
- while ((bitStream & 3) == 3) {
93
- n0 += 3;
94
- bitStream >>= 2;
95
- 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;
96
100
  }
97
- 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;
98
108
  bitCount += 2;
99
- if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
100
- while (charnum < n0) normalizedCounter[charnum++] = 0;
101
- 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)) {
102
121
  assert((bitCount >> 3) <= 3); /* For first condition to work */
103
122
  ip += bitCount>>3;
104
123
  bitCount &= 7;
105
- bitStream = MEM_readLE32(ip) >> bitCount;
106
124
  } else {
107
- bitStream >>= 2;
108
- } }
109
- { 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;
110
133
  int count;
111
134
 
112
135
  if ((bitStream & (threshold-1)) < (U32)max) {
@@ -119,24 +142,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
119
142
  }
120
143
 
121
144
  count--; /* extra accuracy */
122
- 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
+ }
123
154
  normalizedCounter[charnum++] = (short)count;
124
155
  previous0 = !count;
125
- while (remaining < threshold) {
126
- nbBits--;
127
- 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);
128
166
  }
167
+ if (charnum >= maxSV1) break;
129
168
 
130
- if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
169
+ if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
131
170
  ip += bitCount>>3;
132
171
  bitCount &= 7;
133
172
  } else {
134
173
  bitCount -= (int)(8 * (iend - 4 - ip));
174
+ bitCount &= 31;
135
175
  ip = iend - 4;
136
176
  }
137
- bitStream = MEM_readLE32(ip) >> (bitCount & 31);
138
- } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
177
+ bitStream = MEM_readLE32(ip) >> bitCount;
178
+ } }
139
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);
140
182
  if (bitCount > 32) return ERROR(corruption_detected);
141
183
  *maxSVPtr = charnum-1;
142
184
 
@@ -144,6 +186,43 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
144
186
  return ip-istart;
145
187
  }
146
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
+
147
226
 
148
227
  /*! HUF_readStats() :
149
228
  Read compact Huffman tree, saved by HUF_writeCTable().
@@ -155,6 +234,17 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
155
234
  size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
156
235
  U32* nbSymbolsPtr, U32* tableLogPtr,
157
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)
158
248
  {
159
249
  U32 weightTotal;
160
250
  const BYTE* ip = (const BYTE*) src;
@@ -163,7 +253,7 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
163
253
 
164
254
  if (!srcSize) return ERROR(srcSize_wrong);
165
255
  iSize = ip[0];
166
- /* 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 ... */
167
257
 
168
258
  if (iSize >= 128) { /* special header */
169
259
  oSize = iSize - 127;
@@ -177,31 +267,31 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
177
267
  huffWeight[n+1] = ip[n/2] & 15;
178
268
  } } }
179
269
  else { /* header compressed with FSE (normal case) */
180
- FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
181
270
  if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
182
- 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);
183
273
  if (FSE_isError(oSize)) return oSize;
184
274
  }
185
275
 
186
276
  /* collect weight stats */
187
- memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
277
+ ZSTD_memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
188
278
  weightTotal = 0;
189
279
  { U32 n; for (n=0; n<oSize; n++) {
190
- if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
280
+ if (huffWeight[n] > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
191
281
  rankStats[huffWeight[n]]++;
192
282
  weightTotal += (1 << huffWeight[n]) >> 1;
193
283
  } }
194
284
  if (weightTotal == 0) return ERROR(corruption_detected);
195
285
 
196
286
  /* get last non-null symbol weight (implied, total must be 2^n) */
197
- { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
287
+ { U32 const tableLog = ZSTD_highbit32(weightTotal) + 1;
198
288
  if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
199
289
  *tableLogPtr = tableLog;
200
290
  /* determine last weight */
201
291
  { U32 const total = 1 << tableLog;
202
292
  U32 const rest = total - weightTotal;
203
- U32 const verif = 1 << BIT_highbit32(rest);
204
- U32 const lastWeight = BIT_highbit32(rest) + 1;
293
+ U32 const verif = 1 << ZSTD_highbit32(rest);
294
+ U32 const lastWeight = ZSTD_highbit32(rest) + 1;
205
295
  if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
206
296
  huffWeight[oSize] = (BYTE)lastWeight;
207
297
  rankStats[lastWeight]++;
@@ -214,3 +304,37 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
214
304
  *nbSymbolsPtr = (U32)(oSize+1);
215
305
  return iSize+1;
216
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-2020, 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,16 +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";
50
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";
51
59
  case PREFIX(maxCode):
52
60
  default: return notErrorCode;
53
61
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, 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
  /* ****************************************
@@ -73,6 +75,83 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
73
75
  return ERR_getErrorString(ERR_getErrorCode(code));
74
76
  }
75
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
+
76
155
  #if defined (__cplusplus)
77
156
  }
78
157
  #endif