zstdlib 0.9.0-arm64-darwin
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +6 -0
- data/CHANGES.md +78 -0
- data/Gemfile +3 -0
- data/README.md +107 -0
- data/Rakefile +59 -0
- data/ext/zstdlib_c/extconf.rb +54 -0
- data/ext/zstdlib_c/ruby/zlib-2.2/zstdlib.c +4675 -0
- data/ext/zstdlib_c/ruby/zlib-2.3/zstdlib.c +4702 -0
- data/ext/zstdlib_c/ruby/zlib-2.4/zstdlib.c +4859 -0
- data/ext/zstdlib_c/ruby/zlib-2.5/zstdlib.c +4864 -0
- data/ext/zstdlib_c/ruby/zlib-2.6/zstdlib.c +4906 -0
- data/ext/zstdlib_c/ruby/zlib-2.7/zstdlib.c +4895 -0
- data/ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c +4994 -0
- data/ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c +5076 -0
- data/ext/zstdlib_c/zlib-1.2.11/adler32.c +186 -0
- data/ext/zstdlib_c/zlib-1.2.11/compress.c +86 -0
- data/ext/zstdlib_c/zlib-1.2.11/crc32.c +442 -0
- data/ext/zstdlib_c/zlib-1.2.11/crc32.h +441 -0
- data/ext/zstdlib_c/zlib-1.2.11/deflate.c +2163 -0
- data/ext/zstdlib_c/zlib-1.2.11/deflate.h +349 -0
- data/ext/zstdlib_c/zlib-1.2.11/gzclose.c +25 -0
- data/ext/zstdlib_c/zlib-1.2.11/gzguts.h +218 -0
- data/ext/zstdlib_c/zlib-1.2.11/gzlib.c +637 -0
- data/ext/zstdlib_c/zlib-1.2.11/gzread.c +654 -0
- data/ext/zstdlib_c/zlib-1.2.11/gzwrite.c +665 -0
- data/ext/zstdlib_c/zlib-1.2.11/infback.c +640 -0
- data/ext/zstdlib_c/zlib-1.2.11/inffast.c +323 -0
- data/ext/zstdlib_c/zlib-1.2.11/inffast.h +11 -0
- data/ext/zstdlib_c/zlib-1.2.11/inffixed.h +94 -0
- data/ext/zstdlib_c/zlib-1.2.11/inflate.c +1561 -0
- data/ext/zstdlib_c/zlib-1.2.11/inflate.h +125 -0
- data/ext/zstdlib_c/zlib-1.2.11/inftrees.c +304 -0
- data/ext/zstdlib_c/zlib-1.2.11/inftrees.h +62 -0
- data/ext/zstdlib_c/zlib-1.2.11/trees.c +1203 -0
- data/ext/zstdlib_c/zlib-1.2.11/trees.h +128 -0
- data/ext/zstdlib_c/zlib-1.2.11/uncompr.c +93 -0
- data/ext/zstdlib_c/zlib-1.2.11/zconf.h +534 -0
- data/ext/zstdlib_c/zlib-1.2.11/zlib.h +1912 -0
- data/ext/zstdlib_c/zlib-1.2.11/zutil.c +325 -0
- data/ext/zstdlib_c/zlib-1.2.11/zutil.h +271 -0
- data/ext/zstdlib_c/zlib.mk +14 -0
- data/ext/zstdlib_c/zlibwrapper/zlibwrapper.c +10 -0
- data/ext/zstdlib_c/zlibwrapper.mk +14 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/bitstream.h +478 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/compiler.h +335 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/cpu.h +213 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.c +24 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/debug.h +107 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/entropy_common.c +368 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.c +56 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/error_private.h +159 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/fse.h +717 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/fse_decompress.c +403 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/huf.h +364 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/mem.h +442 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/pool.c +355 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/pool.h +84 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/portability_macros.h +137 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/threading.c +122 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/threading.h +155 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.c +24 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/xxhash.h +5686 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_common.c +83 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_deps.h +111 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_internal.h +493 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/common/zstd_trace.h +163 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/clevels.h +134 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/fse_compress.c +741 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/hist.c +181 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/hist.h +75 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/huf_compress.c +1370 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress.c +6327 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_internal.h +1458 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_literals.c +159 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_literals.h +31 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_sequences.c +442 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_sequences.h +54 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.c +573 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_superblock.h +32 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_cwksp.h +676 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_double_fast.c +696 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_double_fast.h +38 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_fast.c +675 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_fast.h +37 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.c +2104 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_lazy.h +125 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm.c +724 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm.h +117 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_ldm_geartab.h +106 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_opt.c +1446 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_opt.h +56 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.c +1859 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstdmt_compress.h +113 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress.c +1889 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/huf_decompress_amd64.S +585 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_ddict.c +244 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_ddict.h +44 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress.c +2230 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.c +2072 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_block.h +68 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/decompress/zstd_decompress_internal.h +236 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/zdict.h +452 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/zstd.h +2575 -0
- data/ext/zstdlib_c/zstd-1.5.2/lib/zstd_errors.h +95 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzclose.c +28 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzcompatibility.h +68 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzguts.h +229 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzlib.c +640 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzread.c +678 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/gzwrite.c +671 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/zstd_zlibwrapper.c +1198 -0
- data/ext/zstdlib_c/zstd-1.5.2/zlibWrapper/zstd_zlibwrapper.h +88 -0
- data/ext/zstdlib_c/zstd.mk +15 -0
- data/lib/2.4/zstdlib_c.bundle +0 -0
- data/lib/2.5/zstdlib_c.bundle +0 -0
- data/lib/2.6/zstdlib_c.bundle +0 -0
- data/lib/2.7/zstdlib_c.bundle +0 -0
- data/lib/3.0/zstdlib_c.bundle +0 -0
- data/lib/3.1/zstdlib_c.bundle +0 -0
- data/lib/zstdlib.rb +6 -0
- data/test/zstdlib_test.rb +21 -0
- metadata +237 -0
@@ -0,0 +1,368 @@
|
|
1
|
+
/* ******************************************************************
|
2
|
+
* Common functions of New Generation Entropy library
|
3
|
+
* Copyright (c) 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.
|
13
|
+
****************************************************************** */
|
14
|
+
|
15
|
+
/* *************************************
|
16
|
+
* Dependencies
|
17
|
+
***************************************/
|
18
|
+
#include "mem.h"
|
19
|
+
#include "error_private.h" /* ERR_*, ERROR */
|
20
|
+
#define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
|
21
|
+
#include "fse.h"
|
22
|
+
#define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
|
23
|
+
#include "huf.h"
|
24
|
+
|
25
|
+
|
26
|
+
/*=== Version ===*/
|
27
|
+
unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
|
28
|
+
|
29
|
+
|
30
|
+
/*=== Error Management ===*/
|
31
|
+
unsigned FSE_isError(size_t code) { return ERR_isError(code); }
|
32
|
+
const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
|
33
|
+
|
34
|
+
unsigned HUF_isError(size_t code) { return ERR_isError(code); }
|
35
|
+
const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
|
36
|
+
|
37
|
+
|
38
|
+
/*-**************************************************************
|
39
|
+
* FSE NCount encoding-decoding
|
40
|
+
****************************************************************/
|
41
|
+
static U32 FSE_ctz(U32 val)
|
42
|
+
{
|
43
|
+
assert(val != 0);
|
44
|
+
{
|
45
|
+
# if defined(_MSC_VER) /* Visual */
|
46
|
+
if (val != 0) {
|
47
|
+
unsigned long r;
|
48
|
+
_BitScanForward(&r, val);
|
49
|
+
return (unsigned)r;
|
50
|
+
} else {
|
51
|
+
/* Should not reach this code path */
|
52
|
+
__assume(0);
|
53
|
+
}
|
54
|
+
# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
|
55
|
+
return __builtin_ctz(val);
|
56
|
+
# elif defined(__ICCARM__) /* IAR Intrinsic */
|
57
|
+
return __CTZ(val);
|
58
|
+
# else /* Software version */
|
59
|
+
U32 count = 0;
|
60
|
+
while ((val & 1) == 0) {
|
61
|
+
val >>= 1;
|
62
|
+
++count;
|
63
|
+
}
|
64
|
+
return count;
|
65
|
+
# endif
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
FORCE_INLINE_TEMPLATE
|
70
|
+
size_t FSE_readNCount_body(short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
|
71
|
+
const void* headerBuffer, size_t hbSize)
|
72
|
+
{
|
73
|
+
const BYTE* const istart = (const BYTE*) headerBuffer;
|
74
|
+
const BYTE* const iend = istart + hbSize;
|
75
|
+
const BYTE* ip = istart;
|
76
|
+
int nbBits;
|
77
|
+
int remaining;
|
78
|
+
int threshold;
|
79
|
+
U32 bitStream;
|
80
|
+
int bitCount;
|
81
|
+
unsigned charnum = 0;
|
82
|
+
unsigned const maxSV1 = *maxSVPtr + 1;
|
83
|
+
int previous0 = 0;
|
84
|
+
|
85
|
+
if (hbSize < 8) {
|
86
|
+
/* This function only works when hbSize >= 8 */
|
87
|
+
char buffer[8] = {0};
|
88
|
+
ZSTD_memcpy(buffer, headerBuffer, hbSize);
|
89
|
+
{ size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
|
90
|
+
buffer, sizeof(buffer));
|
91
|
+
if (FSE_isError(countSize)) return countSize;
|
92
|
+
if (countSize > hbSize) return ERROR(corruption_detected);
|
93
|
+
return countSize;
|
94
|
+
} }
|
95
|
+
assert(hbSize >= 8);
|
96
|
+
|
97
|
+
/* init */
|
98
|
+
ZSTD_memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
|
99
|
+
bitStream = MEM_readLE32(ip);
|
100
|
+
nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
|
101
|
+
if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
|
102
|
+
bitStream >>= 4;
|
103
|
+
bitCount = 4;
|
104
|
+
*tableLogPtr = nbBits;
|
105
|
+
remaining = (1<<nbBits)+1;
|
106
|
+
threshold = 1<<nbBits;
|
107
|
+
nbBits++;
|
108
|
+
|
109
|
+
for (;;) {
|
110
|
+
if (previous0) {
|
111
|
+
/* Count the number of repeats. Each time the
|
112
|
+
* 2-bit repeat code is 0b11 there is another
|
113
|
+
* repeat.
|
114
|
+
* Avoid UB by setting the high bit to 1.
|
115
|
+
*/
|
116
|
+
int repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
|
117
|
+
while (repeats >= 12) {
|
118
|
+
charnum += 3 * 12;
|
119
|
+
if (LIKELY(ip <= iend-7)) {
|
120
|
+
ip += 3;
|
121
|
+
} else {
|
122
|
+
bitCount -= (int)(8 * (iend - 7 - ip));
|
123
|
+
bitCount &= 31;
|
124
|
+
ip = iend - 4;
|
125
|
+
}
|
126
|
+
bitStream = MEM_readLE32(ip) >> bitCount;
|
127
|
+
repeats = FSE_ctz(~bitStream | 0x80000000) >> 1;
|
128
|
+
}
|
129
|
+
charnum += 3 * repeats;
|
130
|
+
bitStream >>= 2 * repeats;
|
131
|
+
bitCount += 2 * repeats;
|
132
|
+
|
133
|
+
/* Add the final repeat which isn't 0b11. */
|
134
|
+
assert((bitStream & 3) < 3);
|
135
|
+
charnum += bitStream & 3;
|
136
|
+
bitCount += 2;
|
137
|
+
|
138
|
+
/* This is an error, but break and return an error
|
139
|
+
* at the end, because returning out of a loop makes
|
140
|
+
* it harder for the compiler to optimize.
|
141
|
+
*/
|
142
|
+
if (charnum >= maxSV1) break;
|
143
|
+
|
144
|
+
/* We don't need to set the normalized count to 0
|
145
|
+
* because we already memset the whole buffer to 0.
|
146
|
+
*/
|
147
|
+
|
148
|
+
if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
|
149
|
+
assert((bitCount >> 3) <= 3); /* For first condition to work */
|
150
|
+
ip += bitCount>>3;
|
151
|
+
bitCount &= 7;
|
152
|
+
} else {
|
153
|
+
bitCount -= (int)(8 * (iend - 4 - ip));
|
154
|
+
bitCount &= 31;
|
155
|
+
ip = iend - 4;
|
156
|
+
}
|
157
|
+
bitStream = MEM_readLE32(ip) >> bitCount;
|
158
|
+
}
|
159
|
+
{
|
160
|
+
int const max = (2*threshold-1) - remaining;
|
161
|
+
int count;
|
162
|
+
|
163
|
+
if ((bitStream & (threshold-1)) < (U32)max) {
|
164
|
+
count = bitStream & (threshold-1);
|
165
|
+
bitCount += nbBits-1;
|
166
|
+
} else {
|
167
|
+
count = bitStream & (2*threshold-1);
|
168
|
+
if (count >= threshold) count -= max;
|
169
|
+
bitCount += nbBits;
|
170
|
+
}
|
171
|
+
|
172
|
+
count--; /* extra accuracy */
|
173
|
+
/* When it matters (small blocks), this is a
|
174
|
+
* predictable branch, because we don't use -1.
|
175
|
+
*/
|
176
|
+
if (count >= 0) {
|
177
|
+
remaining -= count;
|
178
|
+
} else {
|
179
|
+
assert(count == -1);
|
180
|
+
remaining += count;
|
181
|
+
}
|
182
|
+
normalizedCounter[charnum++] = (short)count;
|
183
|
+
previous0 = !count;
|
184
|
+
|
185
|
+
assert(threshold > 1);
|
186
|
+
if (remaining < threshold) {
|
187
|
+
/* This branch can be folded into the
|
188
|
+
* threshold update condition because we
|
189
|
+
* know that threshold > 1.
|
190
|
+
*/
|
191
|
+
if (remaining <= 1) break;
|
192
|
+
nbBits = BIT_highbit32(remaining) + 1;
|
193
|
+
threshold = 1 << (nbBits - 1);
|
194
|
+
}
|
195
|
+
if (charnum >= maxSV1) break;
|
196
|
+
|
197
|
+
if (LIKELY(ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
|
198
|
+
ip += bitCount>>3;
|
199
|
+
bitCount &= 7;
|
200
|
+
} else {
|
201
|
+
bitCount -= (int)(8 * (iend - 4 - ip));
|
202
|
+
bitCount &= 31;
|
203
|
+
ip = iend - 4;
|
204
|
+
}
|
205
|
+
bitStream = MEM_readLE32(ip) >> bitCount;
|
206
|
+
} }
|
207
|
+
if (remaining != 1) return ERROR(corruption_detected);
|
208
|
+
/* Only possible when there are too many zeros. */
|
209
|
+
if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
|
210
|
+
if (bitCount > 32) return ERROR(corruption_detected);
|
211
|
+
*maxSVPtr = charnum-1;
|
212
|
+
|
213
|
+
ip += (bitCount+7)>>3;
|
214
|
+
return ip-istart;
|
215
|
+
}
|
216
|
+
|
217
|
+
/* Avoids the FORCE_INLINE of the _body() function. */
|
218
|
+
static size_t FSE_readNCount_body_default(
|
219
|
+
short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
|
220
|
+
const void* headerBuffer, size_t hbSize)
|
221
|
+
{
|
222
|
+
return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
|
223
|
+
}
|
224
|
+
|
225
|
+
#if DYNAMIC_BMI2
|
226
|
+
BMI2_TARGET_ATTRIBUTE static size_t FSE_readNCount_body_bmi2(
|
227
|
+
short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
|
228
|
+
const void* headerBuffer, size_t hbSize)
|
229
|
+
{
|
230
|
+
return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
|
231
|
+
}
|
232
|
+
#endif
|
233
|
+
|
234
|
+
size_t FSE_readNCount_bmi2(
|
235
|
+
short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
|
236
|
+
const void* headerBuffer, size_t hbSize, int bmi2)
|
237
|
+
{
|
238
|
+
#if DYNAMIC_BMI2
|
239
|
+
if (bmi2) {
|
240
|
+
return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
|
241
|
+
}
|
242
|
+
#endif
|
243
|
+
(void)bmi2;
|
244
|
+
return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
|
245
|
+
}
|
246
|
+
|
247
|
+
size_t FSE_readNCount(
|
248
|
+
short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
|
249
|
+
const void* headerBuffer, size_t hbSize)
|
250
|
+
{
|
251
|
+
return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, /* bmi2 */ 0);
|
252
|
+
}
|
253
|
+
|
254
|
+
|
255
|
+
/*! HUF_readStats() :
|
256
|
+
Read compact Huffman tree, saved by HUF_writeCTable().
|
257
|
+
`huffWeight` is destination buffer.
|
258
|
+
`rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
|
259
|
+
@return : size read from `src` , or an error Code .
|
260
|
+
Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
|
261
|
+
*/
|
262
|
+
size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
|
263
|
+
U32* nbSymbolsPtr, U32* tableLogPtr,
|
264
|
+
const void* src, size_t srcSize)
|
265
|
+
{
|
266
|
+
U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
|
267
|
+
return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp), /* bmi2 */ 0);
|
268
|
+
}
|
269
|
+
|
270
|
+
FORCE_INLINE_TEMPLATE size_t
|
271
|
+
HUF_readStats_body(BYTE* huffWeight, size_t hwSize, U32* rankStats,
|
272
|
+
U32* nbSymbolsPtr, U32* tableLogPtr,
|
273
|
+
const void* src, size_t srcSize,
|
274
|
+
void* workSpace, size_t wkspSize,
|
275
|
+
int bmi2)
|
276
|
+
{
|
277
|
+
U32 weightTotal;
|
278
|
+
const BYTE* ip = (const BYTE*) src;
|
279
|
+
size_t iSize;
|
280
|
+
size_t oSize;
|
281
|
+
|
282
|
+
if (!srcSize) return ERROR(srcSize_wrong);
|
283
|
+
iSize = ip[0];
|
284
|
+
/* ZSTD_memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
|
285
|
+
|
286
|
+
if (iSize >= 128) { /* special header */
|
287
|
+
oSize = iSize - 127;
|
288
|
+
iSize = ((oSize+1)/2);
|
289
|
+
if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
|
290
|
+
if (oSize >= hwSize) return ERROR(corruption_detected);
|
291
|
+
ip += 1;
|
292
|
+
{ U32 n;
|
293
|
+
for (n=0; n<oSize; n+=2) {
|
294
|
+
huffWeight[n] = ip[n/2] >> 4;
|
295
|
+
huffWeight[n+1] = ip[n/2] & 15;
|
296
|
+
} } }
|
297
|
+
else { /* header compressed with FSE (normal case) */
|
298
|
+
if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
|
299
|
+
/* max (hwSize-1) values decoded, as last one is implied */
|
300
|
+
oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize, bmi2);
|
301
|
+
if (FSE_isError(oSize)) return oSize;
|
302
|
+
}
|
303
|
+
|
304
|
+
/* collect weight stats */
|
305
|
+
ZSTD_memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
|
306
|
+
weightTotal = 0;
|
307
|
+
{ U32 n; for (n=0; n<oSize; n++) {
|
308
|
+
if (huffWeight[n] > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
|
309
|
+
rankStats[huffWeight[n]]++;
|
310
|
+
weightTotal += (1 << huffWeight[n]) >> 1;
|
311
|
+
} }
|
312
|
+
if (weightTotal == 0) return ERROR(corruption_detected);
|
313
|
+
|
314
|
+
/* get last non-null symbol weight (implied, total must be 2^n) */
|
315
|
+
{ U32 const tableLog = BIT_highbit32(weightTotal) + 1;
|
316
|
+
if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
|
317
|
+
*tableLogPtr = tableLog;
|
318
|
+
/* determine last weight */
|
319
|
+
{ U32 const total = 1 << tableLog;
|
320
|
+
U32 const rest = total - weightTotal;
|
321
|
+
U32 const verif = 1 << BIT_highbit32(rest);
|
322
|
+
U32 const lastWeight = BIT_highbit32(rest) + 1;
|
323
|
+
if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
|
324
|
+
huffWeight[oSize] = (BYTE)lastWeight;
|
325
|
+
rankStats[lastWeight]++;
|
326
|
+
} }
|
327
|
+
|
328
|
+
/* check tree construction validity */
|
329
|
+
if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
|
330
|
+
|
331
|
+
/* results */
|
332
|
+
*nbSymbolsPtr = (U32)(oSize+1);
|
333
|
+
return iSize+1;
|
334
|
+
}
|
335
|
+
|
336
|
+
/* Avoids the FORCE_INLINE of the _body() function. */
|
337
|
+
static size_t HUF_readStats_body_default(BYTE* huffWeight, size_t hwSize, U32* rankStats,
|
338
|
+
U32* nbSymbolsPtr, U32* tableLogPtr,
|
339
|
+
const void* src, size_t srcSize,
|
340
|
+
void* workSpace, size_t wkspSize)
|
341
|
+
{
|
342
|
+
return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
|
343
|
+
}
|
344
|
+
|
345
|
+
#if DYNAMIC_BMI2
|
346
|
+
static BMI2_TARGET_ATTRIBUTE size_t HUF_readStats_body_bmi2(BYTE* huffWeight, size_t hwSize, U32* rankStats,
|
347
|
+
U32* nbSymbolsPtr, U32* tableLogPtr,
|
348
|
+
const void* src, size_t srcSize,
|
349
|
+
void* workSpace, size_t wkspSize)
|
350
|
+
{
|
351
|
+
return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
|
352
|
+
}
|
353
|
+
#endif
|
354
|
+
|
355
|
+
size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
|
356
|
+
U32* nbSymbolsPtr, U32* tableLogPtr,
|
357
|
+
const void* src, size_t srcSize,
|
358
|
+
void* workSpace, size_t wkspSize,
|
359
|
+
int bmi2)
|
360
|
+
{
|
361
|
+
#if DYNAMIC_BMI2
|
362
|
+
if (bmi2) {
|
363
|
+
return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
|
364
|
+
}
|
365
|
+
#endif
|
366
|
+
(void)bmi2;
|
367
|
+
return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
|
368
|
+
}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) Yann Collet, Facebook, Inc.
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* This source code is licensed under both the BSD-style license (found in the
|
6
|
+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
7
|
+
* in the COPYING file in the root directory of this source tree).
|
8
|
+
* You may select, at your option, one of the above-listed licenses.
|
9
|
+
*/
|
10
|
+
|
11
|
+
/* The purpose of this file is to have a single list of error strings embedded in binary */
|
12
|
+
|
13
|
+
#include "error_private.h"
|
14
|
+
|
15
|
+
const char* ERR_getErrorString(ERR_enum code)
|
16
|
+
{
|
17
|
+
#ifdef ZSTD_STRIP_ERROR_STRINGS
|
18
|
+
(void)code;
|
19
|
+
return "Error strings stripped";
|
20
|
+
#else
|
21
|
+
static const char* const notErrorCode = "Unspecified error code";
|
22
|
+
switch( code )
|
23
|
+
{
|
24
|
+
case PREFIX(no_error): return "No error detected";
|
25
|
+
case PREFIX(GENERIC): return "Error (generic)";
|
26
|
+
case PREFIX(prefix_unknown): return "Unknown frame descriptor";
|
27
|
+
case PREFIX(version_unsupported): return "Version not supported";
|
28
|
+
case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
|
29
|
+
case PREFIX(frameParameter_windowTooLarge): return "Frame requires too much memory for decoding";
|
30
|
+
case PREFIX(corruption_detected): return "Corrupted block detected";
|
31
|
+
case PREFIX(checksum_wrong): return "Restored data doesn't match checksum";
|
32
|
+
case PREFIX(parameter_unsupported): return "Unsupported parameter";
|
33
|
+
case PREFIX(parameter_outOfBound): return "Parameter is out of bound";
|
34
|
+
case PREFIX(init_missing): return "Context should be init first";
|
35
|
+
case PREFIX(memory_allocation): return "Allocation error : not enough memory";
|
36
|
+
case PREFIX(workSpace_tooSmall): return "workSpace buffer is not large enough";
|
37
|
+
case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
|
38
|
+
case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
|
39
|
+
case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
|
40
|
+
case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
|
41
|
+
case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
|
42
|
+
case PREFIX(dictionary_wrong): return "Dictionary mismatch";
|
43
|
+
case PREFIX(dictionaryCreation_failed): return "Cannot create Dictionary from provided samples";
|
44
|
+
case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
|
45
|
+
case PREFIX(srcSize_wrong): return "Src size is incorrect";
|
46
|
+
case PREFIX(dstBuffer_null): return "Operation on NULL destination buffer";
|
47
|
+
/* following error codes are not stable and may be removed or changed in a future version */
|
48
|
+
case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
|
49
|
+
case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
|
50
|
+
case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
|
51
|
+
case PREFIX(srcBuffer_wrong): return "Source buffer is wrong";
|
52
|
+
case PREFIX(maxCode):
|
53
|
+
default: return notErrorCode;
|
54
|
+
}
|
55
|
+
#endif
|
56
|
+
}
|
@@ -0,0 +1,159 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) Yann Collet, Facebook, Inc.
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* This source code is licensed under both the BSD-style license (found in the
|
6
|
+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
7
|
+
* in the COPYING file in the root directory of this source tree).
|
8
|
+
* You may select, at your option, one of the above-listed licenses.
|
9
|
+
*/
|
10
|
+
|
11
|
+
/* Note : this module is expected to remain private, do not expose it */
|
12
|
+
|
13
|
+
#ifndef ERROR_H_MODULE
|
14
|
+
#define ERROR_H_MODULE
|
15
|
+
|
16
|
+
#if defined (__cplusplus)
|
17
|
+
extern "C" {
|
18
|
+
#endif
|
19
|
+
|
20
|
+
|
21
|
+
/* ****************************************
|
22
|
+
* Dependencies
|
23
|
+
******************************************/
|
24
|
+
#include "../zstd_errors.h" /* enum list */
|
25
|
+
#include "compiler.h"
|
26
|
+
#include "debug.h"
|
27
|
+
#include "zstd_deps.h" /* size_t */
|
28
|
+
|
29
|
+
|
30
|
+
/* ****************************************
|
31
|
+
* Compiler-specific
|
32
|
+
******************************************/
|
33
|
+
#if defined(__GNUC__)
|
34
|
+
# define ERR_STATIC static __attribute__((unused))
|
35
|
+
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
|
36
|
+
# define ERR_STATIC static inline
|
37
|
+
#elif defined(_MSC_VER)
|
38
|
+
# define ERR_STATIC static __inline
|
39
|
+
#else
|
40
|
+
# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
|
41
|
+
#endif
|
42
|
+
|
43
|
+
|
44
|
+
/*-****************************************
|
45
|
+
* Customization (error_public.h)
|
46
|
+
******************************************/
|
47
|
+
typedef ZSTD_ErrorCode ERR_enum;
|
48
|
+
#define PREFIX(name) ZSTD_error_##name
|
49
|
+
|
50
|
+
|
51
|
+
/*-****************************************
|
52
|
+
* Error codes handling
|
53
|
+
******************************************/
|
54
|
+
#undef ERROR /* already defined on Visual Studio */
|
55
|
+
#define ERROR(name) ZSTD_ERROR(name)
|
56
|
+
#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
|
57
|
+
|
58
|
+
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
|
59
|
+
|
60
|
+
ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
|
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
|
+
|
66
|
+
|
67
|
+
/*-****************************************
|
68
|
+
* Error Strings
|
69
|
+
******************************************/
|
70
|
+
|
71
|
+
const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
|
72
|
+
|
73
|
+
ERR_STATIC const char* ERR_getErrorName(size_t code)
|
74
|
+
{
|
75
|
+
return ERR_getErrorString(ERR_getErrorCode(code));
|
76
|
+
}
|
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
|
+
|
155
|
+
#if defined (__cplusplus)
|
156
|
+
}
|
157
|
+
#endif
|
158
|
+
|
159
|
+
#endif /* ERROR_H_MODULE */
|