extzstd 0.3.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -14
  3. data/contrib/zstd/CHANGELOG +301 -56
  4. data/contrib/zstd/CONTRIBUTING.md +169 -72
  5. data/contrib/zstd/LICENSE +4 -4
  6. data/contrib/zstd/Makefile +116 -87
  7. data/contrib/zstd/Package.swift +36 -0
  8. data/contrib/zstd/README.md +62 -32
  9. data/contrib/zstd/TESTING.md +2 -3
  10. data/contrib/zstd/appveyor.yml +52 -136
  11. data/contrib/zstd/lib/BUCK +5 -7
  12. data/contrib/zstd/lib/Makefile +225 -222
  13. data/contrib/zstd/lib/README.md +51 -6
  14. data/contrib/zstd/lib/common/allocations.h +55 -0
  15. data/contrib/zstd/lib/common/bits.h +200 -0
  16. data/contrib/zstd/lib/common/bitstream.h +45 -62
  17. data/contrib/zstd/lib/common/compiler.h +205 -22
  18. data/contrib/zstd/lib/common/cpu.h +1 -3
  19. data/contrib/zstd/lib/common/debug.c +1 -1
  20. data/contrib/zstd/lib/common/debug.h +12 -19
  21. data/contrib/zstd/lib/common/entropy_common.c +172 -48
  22. data/contrib/zstd/lib/common/error_private.c +10 -2
  23. data/contrib/zstd/lib/common/error_private.h +82 -3
  24. data/contrib/zstd/lib/common/fse.h +37 -86
  25. data/contrib/zstd/lib/common/fse_decompress.c +117 -92
  26. data/contrib/zstd/lib/common/huf.h +99 -166
  27. data/contrib/zstd/lib/common/mem.h +124 -142
  28. data/contrib/zstd/lib/common/pool.c +54 -27
  29. data/contrib/zstd/lib/common/pool.h +10 -4
  30. data/contrib/zstd/lib/common/portability_macros.h +156 -0
  31. data/contrib/zstd/lib/common/threading.c +74 -19
  32. data/contrib/zstd/lib/common/threading.h +5 -10
  33. data/contrib/zstd/lib/common/xxhash.c +7 -847
  34. data/contrib/zstd/lib/common/xxhash.h +5568 -167
  35. data/contrib/zstd/lib/common/zstd_common.c +2 -37
  36. data/contrib/zstd/lib/common/zstd_deps.h +111 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +132 -187
  38. data/contrib/zstd/lib/common/zstd_trace.h +163 -0
  39. data/contrib/zstd/lib/compress/clevels.h +134 -0
  40. data/contrib/zstd/lib/compress/fse_compress.c +83 -157
  41. data/contrib/zstd/lib/compress/hist.c +27 -29
  42. data/contrib/zstd/lib/compress/hist.h +2 -2
  43. data/contrib/zstd/lib/compress/huf_compress.c +916 -279
  44. data/contrib/zstd/lib/compress/zstd_compress.c +3773 -1019
  45. data/contrib/zstd/lib/compress/zstd_compress_internal.h +610 -203
  46. data/contrib/zstd/lib/compress/zstd_compress_literals.c +119 -42
  47. data/contrib/zstd/lib/compress/zstd_compress_literals.h +16 -6
  48. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +42 -19
  49. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +1 -1
  50. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +49 -317
  51. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +1 -1
  52. data/contrib/zstd/lib/compress/zstd_cwksp.h +320 -103
  53. data/contrib/zstd/lib/compress/zstd_double_fast.c +388 -151
  54. data/contrib/zstd/lib/compress/zstd_double_fast.h +3 -2
  55. data/contrib/zstd/lib/compress/zstd_fast.c +729 -265
  56. data/contrib/zstd/lib/compress/zstd_fast.h +3 -2
  57. data/contrib/zstd/lib/compress/zstd_lazy.c +1270 -251
  58. data/contrib/zstd/lib/compress/zstd_lazy.h +61 -1
  59. data/contrib/zstd/lib/compress/zstd_ldm.c +324 -219
  60. data/contrib/zstd/lib/compress/zstd_ldm.h +9 -2
  61. data/contrib/zstd/lib/compress/zstd_ldm_geartab.h +106 -0
  62. data/contrib/zstd/lib/compress/zstd_opt.c +481 -209
  63. data/contrib/zstd/lib/compress/zstd_opt.h +1 -1
  64. data/contrib/zstd/lib/compress/zstdmt_compress.c +181 -457
  65. data/contrib/zstd/lib/compress/zstdmt_compress.h +34 -113
  66. data/contrib/zstd/lib/decompress/huf_decompress.c +1199 -565
  67. data/contrib/zstd/lib/decompress/huf_decompress_amd64.S +576 -0
  68. data/contrib/zstd/lib/decompress/zstd_ddict.c +12 -12
  69. data/contrib/zstd/lib/decompress/zstd_ddict.h +2 -2
  70. data/contrib/zstd/lib/decompress/zstd_decompress.c +627 -157
  71. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1086 -326
  72. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +19 -5
  73. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +62 -13
  74. data/contrib/zstd/lib/deprecated/zbuff.h +1 -1
  75. data/contrib/zstd/lib/deprecated/zbuff_common.c +1 -1
  76. data/contrib/zstd/lib/deprecated/zbuff_compress.c +24 -4
  77. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +3 -1
  78. data/contrib/zstd/lib/dictBuilder/cover.c +73 -52
  79. data/contrib/zstd/lib/dictBuilder/cover.h +7 -6
  80. data/contrib/zstd/lib/dictBuilder/divsufsort.c +1 -1
  81. data/contrib/zstd/lib/dictBuilder/fastcover.c +44 -35
  82. data/contrib/zstd/lib/dictBuilder/zdict.c +103 -111
  83. data/contrib/zstd/lib/legacy/zstd_legacy.h +8 -1
  84. data/contrib/zstd/lib/legacy/zstd_v01.c +21 -54
  85. data/contrib/zstd/lib/legacy/zstd_v01.h +1 -1
  86. data/contrib/zstd/lib/legacy/zstd_v02.c +29 -70
  87. data/contrib/zstd/lib/legacy/zstd_v02.h +1 -1
  88. data/contrib/zstd/lib/legacy/zstd_v03.c +30 -73
  89. data/contrib/zstd/lib/legacy/zstd_v03.h +1 -1
  90. data/contrib/zstd/lib/legacy/zstd_v04.c +29 -71
  91. data/contrib/zstd/lib/legacy/zstd_v04.h +1 -1
  92. data/contrib/zstd/lib/legacy/zstd_v05.c +40 -86
  93. data/contrib/zstd/lib/legacy/zstd_v05.h +1 -1
  94. data/contrib/zstd/lib/legacy/zstd_v06.c +47 -88
  95. data/contrib/zstd/lib/legacy/zstd_v06.h +1 -1
  96. data/contrib/zstd/lib/legacy/zstd_v07.c +40 -83
  97. data/contrib/zstd/lib/legacy/zstd_v07.h +1 -1
  98. data/contrib/zstd/lib/libzstd.mk +214 -0
  99. data/contrib/zstd/lib/libzstd.pc.in +7 -6
  100. data/contrib/zstd/lib/module.modulemap +35 -0
  101. data/contrib/zstd/lib/{dictBuilder/zdict.h → zdict.h} +203 -34
  102. data/contrib/zstd/lib/zstd.h +1217 -287
  103. data/contrib/zstd/lib/{common/zstd_errors.h → zstd_errors.h} +28 -8
  104. data/ext/extconf.rb +7 -6
  105. data/ext/extzstd.c +19 -10
  106. data/ext/extzstd.h +6 -0
  107. data/ext/libzstd_conf.h +0 -1
  108. data/ext/zstd_decompress_asm.S +1 -0
  109. data/gemstub.rb +3 -21
  110. data/lib/extzstd/version.rb +6 -1
  111. data/lib/extzstd.rb +0 -2
  112. data/test/test_basic.rb +0 -5
  113. metadata +18 -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