zstd-ruby 1.4.1.0 → 1.5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +8 -0
  3. data/.github/workflows/ruby.yml +35 -0
  4. data/README.md +2 -2
  5. data/ext/zstdruby/libzstd/BUCK +5 -7
  6. data/ext/zstdruby/libzstd/Makefile +304 -113
  7. data/ext/zstdruby/libzstd/README.md +83 -20
  8. data/ext/zstdruby/libzstd/common/bitstream.h +59 -51
  9. data/ext/zstdruby/libzstd/common/compiler.h +150 -8
  10. data/ext/zstdruby/libzstd/common/cpu.h +1 -3
  11. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  12. data/ext/zstdruby/libzstd/common/debug.h +22 -49
  13. data/ext/zstdruby/libzstd/common/entropy_common.c +201 -75
  14. data/ext/zstdruby/libzstd/common/error_private.c +3 -1
  15. data/ext/zstdruby/libzstd/common/error_private.h +8 -4
  16. data/ext/zstdruby/libzstd/common/fse.h +50 -42
  17. data/ext/zstdruby/libzstd/common/fse_decompress.c +149 -55
  18. data/ext/zstdruby/libzstd/common/huf.h +43 -39
  19. data/ext/zstdruby/libzstd/common/mem.h +69 -25
  20. data/ext/zstdruby/libzstd/common/pool.c +30 -20
  21. data/ext/zstdruby/libzstd/common/pool.h +3 -3
  22. data/ext/zstdruby/libzstd/common/threading.c +51 -4
  23. data/ext/zstdruby/libzstd/common/threading.h +36 -4
  24. data/ext/zstdruby/libzstd/common/xxhash.c +40 -92
  25. data/ext/zstdruby/libzstd/common/xxhash.h +12 -32
  26. data/ext/zstdruby/libzstd/common/zstd_common.c +10 -10
  27. data/ext/zstdruby/libzstd/common/zstd_deps.h +111 -0
  28. data/ext/zstdruby/libzstd/common/zstd_internal.h +230 -111
  29. data/ext/zstdruby/libzstd/common/zstd_trace.h +154 -0
  30. data/ext/zstdruby/libzstd/compress/fse_compress.c +47 -63
  31. data/ext/zstdruby/libzstd/compress/hist.c +41 -63
  32. data/ext/zstdruby/libzstd/compress/hist.h +13 -33
  33. data/ext/zstdruby/libzstd/compress/huf_compress.c +332 -193
  34. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3614 -1696
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +546 -86
  36. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +158 -0
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +29 -0
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +441 -0
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +54 -0
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +572 -0
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  42. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +662 -0
  43. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +43 -41
  44. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  45. data/ext/zstdruby/libzstd/compress/zstd_fast.c +85 -80
  46. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  47. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +1184 -111
  48. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +59 -1
  49. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +333 -208
  50. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +15 -3
  51. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +103 -0
  52. data/ext/zstdruby/libzstd/compress/zstd_opt.c +228 -129
  53. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  54. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +151 -440
  55. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +32 -114
  56. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +395 -276
  57. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +20 -16
  58. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +3 -3
  59. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +630 -231
  60. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +606 -380
  61. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +8 -5
  62. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +39 -9
  63. data/ext/zstdruby/libzstd/deprecated/zbuff.h +9 -8
  64. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +2 -2
  65. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -1
  66. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.c +55 -46
  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 +43 -31
  71. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +53 -30
  72. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  73. data/ext/zstdruby/libzstd/dll/example/README.md +16 -22
  74. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +4 -4
  75. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +24 -14
  76. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +1 -1
  77. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +17 -8
  78. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +1 -1
  79. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +17 -8
  80. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +1 -1
  81. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +25 -11
  82. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +1 -1
  83. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +43 -32
  84. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +2 -2
  85. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +27 -19
  86. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +1 -1
  87. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +32 -20
  88. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +1 -1
  89. data/ext/zstdruby/libzstd/libzstd.pc.in +2 -1
  90. data/ext/zstdruby/libzstd/{dictBuilder/zdict.h → zdict.h} +201 -31
  91. data/ext/zstdruby/libzstd/zstd.h +740 -153
  92. data/ext/zstdruby/libzstd/{common/zstd_errors.h → zstd_errors.h} +3 -1
  93. data/lib/zstd-ruby/version.rb +1 -1
  94. data/zstd-ruby.gemspec +1 -1
  95. metadata +21 -10
  96. data/.travis.yml +0 -14
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Copyright (c) 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
+ /* This file provides common libc dependencies that zstd requires.
12
+ * The purpose is to allow replacing this file with a custom implementation
13
+ * to compile zstd without libc support.
14
+ */
15
+
16
+ /* Need:
17
+ * NULL
18
+ * INT_MAX
19
+ * UINT_MAX
20
+ * ZSTD_memcpy()
21
+ * ZSTD_memset()
22
+ * ZSTD_memmove()
23
+ */
24
+ #ifndef ZSTD_DEPS_COMMON
25
+ #define ZSTD_DEPS_COMMON
26
+
27
+ #include <limits.h>
28
+ #include <stddef.h>
29
+ #include <string.h>
30
+
31
+ #if defined(__GNUC__) && __GNUC__ >= 4
32
+ # define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l))
33
+ # define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l))
34
+ # define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l))
35
+ #else
36
+ # define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l))
37
+ # define ZSTD_memmove(d,s,l) memmove((d),(s),(l))
38
+ # define ZSTD_memset(p,v,l) memset((p),(v),(l))
39
+ #endif
40
+
41
+ #endif /* ZSTD_DEPS_COMMON */
42
+
43
+ /* Need:
44
+ * ZSTD_malloc()
45
+ * ZSTD_free()
46
+ * ZSTD_calloc()
47
+ */
48
+ #ifdef ZSTD_DEPS_NEED_MALLOC
49
+ #ifndef ZSTD_DEPS_MALLOC
50
+ #define ZSTD_DEPS_MALLOC
51
+
52
+ #include <stdlib.h>
53
+
54
+ #define ZSTD_malloc(s) malloc(s)
55
+ #define ZSTD_calloc(n,s) calloc((n), (s))
56
+ #define ZSTD_free(p) free((p))
57
+
58
+ #endif /* ZSTD_DEPS_MALLOC */
59
+ #endif /* ZSTD_DEPS_NEED_MALLOC */
60
+
61
+ /*
62
+ * Provides 64-bit math support.
63
+ * Need:
64
+ * U64 ZSTD_div64(U64 dividend, U32 divisor)
65
+ */
66
+ #ifdef ZSTD_DEPS_NEED_MATH64
67
+ #ifndef ZSTD_DEPS_MATH64
68
+ #define ZSTD_DEPS_MATH64
69
+
70
+ #define ZSTD_div64(dividend, divisor) ((dividend) / (divisor))
71
+
72
+ #endif /* ZSTD_DEPS_MATH64 */
73
+ #endif /* ZSTD_DEPS_NEED_MATH64 */
74
+
75
+ /* Need:
76
+ * assert()
77
+ */
78
+ #ifdef ZSTD_DEPS_NEED_ASSERT
79
+ #ifndef ZSTD_DEPS_ASSERT
80
+ #define ZSTD_DEPS_ASSERT
81
+
82
+ #include <assert.h>
83
+
84
+ #endif /* ZSTD_DEPS_ASSERT */
85
+ #endif /* ZSTD_DEPS_NEED_ASSERT */
86
+
87
+ /* Need:
88
+ * ZSTD_DEBUG_PRINT()
89
+ */
90
+ #ifdef ZSTD_DEPS_NEED_IO
91
+ #ifndef ZSTD_DEPS_IO
92
+ #define ZSTD_DEPS_IO
93
+
94
+ #include <stdio.h>
95
+ #define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
96
+
97
+ #endif /* ZSTD_DEPS_IO */
98
+ #endif /* ZSTD_DEPS_NEED_IO */
99
+
100
+ /* Only requested when <stdint.h> is known to be present.
101
+ * Need:
102
+ * intptr_t
103
+ */
104
+ #ifdef ZSTD_DEPS_NEED_STDINT
105
+ #ifndef ZSTD_DEPS_STDINT
106
+ #define ZSTD_DEPS_STDINT
107
+
108
+ #include <stdint.h>
109
+
110
+ #endif /* ZSTD_DEPS_STDINT */
111
+ #endif /* ZSTD_DEPS_NEED_STDINT */
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -19,12 +19,15 @@
19
19
  /*-*************************************
20
20
  * Dependencies
21
21
  ***************************************/
22
+ #if !defined(ZSTD_NO_INTRINSICS) && defined(__ARM_NEON)
23
+ #include <arm_neon.h>
24
+ #endif
22
25
  #include "compiler.h"
23
26
  #include "mem.h"
24
27
  #include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */
25
28
  #include "error_private.h"
26
29
  #define ZSTD_STATIC_LINKING_ONLY
27
- #include "zstd.h"
30
+ #include "../zstd.h"
28
31
  #define FSE_STATIC_LINKING_ONLY
29
32
  #include "fse.h"
30
33
  #define HUF_STATIC_LINKING_ONLY
@@ -33,6 +36,11 @@
33
36
  # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
34
37
  #endif
35
38
  #include "xxhash.h" /* XXH_reset, update, digest */
39
+ #ifndef ZSTD_NO_TRACE
40
+ # include "zstd_trace.h"
41
+ #else
42
+ # define ZSTD_TRACE 0
43
+ #endif
36
44
 
37
45
  #if defined (__cplusplus)
38
46
  extern "C" {
@@ -53,16 +61,43 @@ extern "C" {
53
61
  #define MIN(a,b) ((a)<(b) ? (a) : (b))
54
62
  #define MAX(a,b) ((a)>(b) ? (a) : (b))
55
63
 
64
+ /**
65
+ * Ignore: this is an internal helper.
66
+ *
67
+ * This is a helper function to help force C99-correctness during compilation.
68
+ * Under strict compilation modes, variadic macro arguments can't be empty.
69
+ * However, variadic function arguments can be. Using a function therefore lets
70
+ * us statically check that at least one (string) argument was passed,
71
+ * independent of the compilation flags.
72
+ */
73
+ static INLINE_KEYWORD UNUSED_ATTR
74
+ void _force_has_format_string(const char *format, ...) {
75
+ (void)format;
76
+ }
77
+
78
+ /**
79
+ * Ignore: this is an internal helper.
80
+ *
81
+ * We want to force this function invocation to be syntactically correct, but
82
+ * we don't want to force runtime evaluation of its arguments.
83
+ */
84
+ #define _FORCE_HAS_FORMAT_STRING(...) \
85
+ if (0) { \
86
+ _force_has_format_string(__VA_ARGS__); \
87
+ }
88
+
56
89
  /**
57
90
  * Return the specified error if the condition evaluates to true.
58
91
  *
59
- * In debug modes, prints additional information. In order to do that
60
- * (particularly, printing the conditional that failed), this can't just wrap
61
- * RETURN_ERROR().
92
+ * In debug modes, prints additional information.
93
+ * In order to do that (particularly, printing the conditional that failed),
94
+ * this can't just wrap RETURN_ERROR().
62
95
  */
63
96
  #define RETURN_ERROR_IF(cond, err, ...) \
64
97
  if (cond) { \
65
- RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \
98
+ RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
99
+ __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \
100
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
66
101
  RAWLOG(3, ": " __VA_ARGS__); \
67
102
  RAWLOG(3, "\n"); \
68
103
  return ERROR(err); \
@@ -75,7 +110,9 @@ extern "C" {
75
110
  */
76
111
  #define RETURN_ERROR(err, ...) \
77
112
  do { \
78
- RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \
113
+ RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
114
+ __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \
115
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
79
116
  RAWLOG(3, ": " __VA_ARGS__); \
80
117
  RAWLOG(3, "\n"); \
81
118
  return ERROR(err); \
@@ -90,7 +127,9 @@ extern "C" {
90
127
  do { \
91
128
  size_t const err_code = (err); \
92
129
  if (ERR_isError(err_code)) { \
93
- RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \
130
+ RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
131
+ __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \
132
+ _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
94
133
  RAWLOG(3, ": " __VA_ARGS__); \
95
134
  RAWLOG(3, "\n"); \
96
135
  return err_code; \
@@ -105,7 +144,7 @@ extern "C" {
105
144
 
106
145
  #define ZSTD_REP_NUM 3 /* number of repcodes */
107
146
  #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
108
- static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
147
+ static UNUSED_ATTR const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
109
148
 
110
149
  #define KB *(1 <<10)
111
150
  #define MB *(1 <<20)
@@ -119,15 +158,17 @@ static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
119
158
  #define BIT0 1
120
159
 
121
160
  #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
122
- static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
123
- static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
161
+ static UNUSED_ATTR const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
162
+ static UNUSED_ATTR const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
124
163
 
125
164
  #define ZSTD_FRAMEIDSIZE 4 /* magic number size */
126
165
 
127
166
  #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
128
- static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
167
+ static UNUSED_ATTR const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
129
168
  typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
130
169
 
170
+ #define ZSTD_FRAMECHECKSUMSIZE 4
171
+
131
172
  #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
132
173
  #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
133
174
 
@@ -150,150 +191,223 @@ typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingTy
150
191
  #define OffFSELog 8
151
192
  #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
152
193
 
153
- static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
154
- 0, 0, 0, 0, 0, 0, 0, 0,
155
- 1, 1, 1, 1, 2, 2, 3, 3,
156
- 4, 6, 7, 8, 9,10,11,12,
157
- 13,14,15,16 };
158
- static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
159
- 2, 2, 2, 2, 2, 1, 1, 1,
160
- 2, 2, 2, 2, 2, 2, 2, 2,
161
- 2, 3, 2, 1, 1, 1, 1, 1,
162
- -1,-1,-1,-1 };
194
+ #define ZSTD_MAX_HUF_HEADER_SIZE 128 /* header + <= 127 byte tree description */
195
+ /* Each table cannot take more than #symbols * FSELog bits */
196
+ #define ZSTD_MAX_FSE_HEADERS_SIZE (((MaxML + 1) * MLFSELog + (MaxLL + 1) * LLFSELog + (MaxOff + 1) * OffFSELog + 7) / 8)
197
+
198
+ static UNUSED_ATTR const U32 LL_bits[MaxLL+1] = {
199
+ 0, 0, 0, 0, 0, 0, 0, 0,
200
+ 0, 0, 0, 0, 0, 0, 0, 0,
201
+ 1, 1, 1, 1, 2, 2, 3, 3,
202
+ 4, 6, 7, 8, 9,10,11,12,
203
+ 13,14,15,16
204
+ };
205
+ static UNUSED_ATTR const S16 LL_defaultNorm[MaxLL+1] = {
206
+ 4, 3, 2, 2, 2, 2, 2, 2,
207
+ 2, 2, 2, 2, 2, 1, 1, 1,
208
+ 2, 2, 2, 2, 2, 2, 2, 2,
209
+ 2, 3, 2, 1, 1, 1, 1, 1,
210
+ -1,-1,-1,-1
211
+ };
163
212
  #define LL_DEFAULTNORMLOG 6 /* for static allocation */
164
- static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
165
-
166
- static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
167
- 0, 0, 0, 0, 0, 0, 0, 0,
168
- 0, 0, 0, 0, 0, 0, 0, 0,
169
- 0, 0, 0, 0, 0, 0, 0, 0,
170
- 1, 1, 1, 1, 2, 2, 3, 3,
171
- 4, 4, 5, 7, 8, 9,10,11,
172
- 12,13,14,15,16 };
173
- static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
174
- 2, 1, 1, 1, 1, 1, 1, 1,
175
- 1, 1, 1, 1, 1, 1, 1, 1,
176
- 1, 1, 1, 1, 1, 1, 1, 1,
177
- 1, 1, 1, 1, 1, 1, 1, 1,
178
- 1, 1, 1, 1, 1, 1,-1,-1,
179
- -1,-1,-1,-1,-1 };
213
+ static UNUSED_ATTR const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
214
+
215
+ static UNUSED_ATTR const U32 ML_bits[MaxML+1] = {
216
+ 0, 0, 0, 0, 0, 0, 0, 0,
217
+ 0, 0, 0, 0, 0, 0, 0, 0,
218
+ 0, 0, 0, 0, 0, 0, 0, 0,
219
+ 0, 0, 0, 0, 0, 0, 0, 0,
220
+ 1, 1, 1, 1, 2, 2, 3, 3,
221
+ 4, 4, 5, 7, 8, 9,10,11,
222
+ 12,13,14,15,16
223
+ };
224
+ static UNUSED_ATTR const S16 ML_defaultNorm[MaxML+1] = {
225
+ 1, 4, 3, 2, 2, 2, 2, 2,
226
+ 2, 1, 1, 1, 1, 1, 1, 1,
227
+ 1, 1, 1, 1, 1, 1, 1, 1,
228
+ 1, 1, 1, 1, 1, 1, 1, 1,
229
+ 1, 1, 1, 1, 1, 1, 1, 1,
230
+ 1, 1, 1, 1, 1, 1,-1,-1,
231
+ -1,-1,-1,-1,-1
232
+ };
180
233
  #define ML_DEFAULTNORMLOG 6 /* for static allocation */
181
- static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
182
-
183
- static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
184
- 2, 1, 1, 1, 1, 1, 1, 1,
185
- 1, 1, 1, 1, 1, 1, 1, 1,
186
- -1,-1,-1,-1,-1 };
234
+ static UNUSED_ATTR const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
235
+
236
+ static UNUSED_ATTR const S16 OF_defaultNorm[DefaultMaxOff+1] = {
237
+ 1, 1, 1, 1, 1, 1, 2, 2,
238
+ 2, 1, 1, 1, 1, 1, 1, 1,
239
+ 1, 1, 1, 1, 1, 1, 1, 1,
240
+ -1,-1,-1,-1,-1
241
+ };
187
242
  #define OF_DEFAULTNORMLOG 5 /* for static allocation */
188
- static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
243
+ static UNUSED_ATTR const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
189
244
 
190
245
 
191
246
  /*-*******************************************
192
247
  * Shared functions to include for inlining
193
248
  *********************************************/
194
- static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
249
+ static void ZSTD_copy8(void* dst, const void* src) {
250
+ #if !defined(ZSTD_NO_INTRINSICS) && defined(__ARM_NEON)
251
+ vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));
252
+ #else
253
+ ZSTD_memcpy(dst, src, 8);
254
+ #endif
255
+ }
195
256
 
196
257
  #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
197
- static void ZSTD_copy16(void* dst, const void* src) { memcpy(dst, src, 16); }
258
+ static void ZSTD_copy16(void* dst, const void* src) {
259
+ #if !defined(ZSTD_NO_INTRINSICS) && defined(__ARM_NEON)
260
+ vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));
261
+ #else
262
+ ZSTD_memcpy(dst, src, 16);
263
+ #endif
264
+ }
198
265
  #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }
199
266
 
200
- #define WILDCOPY_OVERLENGTH 8
201
- #define VECLEN 16
267
+ #define WILDCOPY_OVERLENGTH 32
268
+ #define WILDCOPY_VECLEN 16
202
269
 
203
270
  typedef enum {
204
271
  ZSTD_no_overlap,
205
- ZSTD_overlap_src_before_dst,
272
+ ZSTD_overlap_src_before_dst
206
273
  /* ZSTD_overlap_dst_before_src, */
207
274
  } ZSTD_overlap_e;
208
275
 
209
276
  /*! ZSTD_wildcopy() :
210
- * custom version of memcpy(), can overwrite up to WILDCOPY_OVERLENGTH bytes (if length==0) */
211
- MEM_STATIC FORCE_INLINE_ATTR DONT_VECTORIZE
212
- void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e ovtype)
277
+ * Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)
278
+ * @param ovtype controls the overlap detection
279
+ * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.
280
+ * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.
281
+ * The src buffer must be before the dst buffer.
282
+ */
283
+ MEM_STATIC FORCE_INLINE_ATTR
284
+ void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)
213
285
  {
214
286
  ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
215
287
  const BYTE* ip = (const BYTE*)src;
216
288
  BYTE* op = (BYTE*)dst;
217
289
  BYTE* const oend = op + length;
218
290
 
219
- assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff < -8));
220
- if (length < VECLEN || (ovtype == ZSTD_overlap_src_before_dst && diff < VECLEN)) {
221
- do
222
- COPY8(op, ip)
223
- while (op < oend);
224
- }
225
- else {
226
- if ((length & 8) == 0)
227
- COPY8(op, ip);
228
- do {
229
- COPY16(op, ip);
230
- }
231
- while (op < oend);
291
+ assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN));
292
+
293
+ if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {
294
+ /* Handle short offset copies. */
295
+ do {
296
+ COPY8(op, ip)
297
+ } while (op < oend);
298
+ } else {
299
+ assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);
300
+ /* Separate out the first COPY16() call because the copy length is
301
+ * almost certain to be short, so the branches have different
302
+ * probabilities. Since it is almost certain to be short, only do
303
+ * one COPY16() in the first call. Then, do two calls per loop since
304
+ * at that point it is more likely to have a high trip count.
305
+ */
306
+ #ifdef __aarch64__
307
+ do {
308
+ COPY16(op, ip);
309
+ }
310
+ while (op < oend);
311
+ #else
312
+ ZSTD_copy16(op, ip);
313
+ if (16 >= length) return;
314
+ op += 16;
315
+ ip += 16;
316
+ do {
317
+ COPY16(op, ip);
318
+ COPY16(op, ip);
319
+ }
320
+ while (op < oend);
321
+ #endif
232
322
  }
233
323
  }
234
324
 
235
- /*! ZSTD_wildcopy_16min() :
236
- * same semantics as ZSTD_wilcopy() except guaranteed to be able to copy 16 bytes at the start */
237
- MEM_STATIC FORCE_INLINE_ATTR DONT_VECTORIZE
238
- void ZSTD_wildcopy_16min(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e ovtype)
325
+ MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
239
326
  {
240
- ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;
241
- const BYTE* ip = (const BYTE*)src;
242
- BYTE* op = (BYTE*)dst;
243
- BYTE* const oend = op + length;
244
-
245
- assert(length >= 8);
246
- assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff < -8));
247
-
248
- if (ovtype == ZSTD_overlap_src_before_dst && diff < VECLEN) {
249
- do
250
- COPY8(op, ip)
251
- while (op < oend);
252
- }
253
- else {
254
- if ((length & 8) == 0)
255
- COPY8(op, ip);
256
- do {
257
- COPY16(op, ip);
258
- }
259
- while (op < oend);
327
+ size_t const length = MIN(dstCapacity, srcSize);
328
+ if (length > 0) {
329
+ ZSTD_memcpy(dst, src, length);
260
330
  }
331
+ return length;
261
332
  }
262
333
 
263
- MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
264
- {
265
- const BYTE* ip = (const BYTE*)src;
266
- BYTE* op = (BYTE*)dst;
267
- BYTE* const oend = (BYTE*)dstEnd;
268
- do
269
- COPY8(op, ip)
270
- while (op < oend);
271
- }
334
+ /* define "workspace is too large" as this number of times larger than needed */
335
+ #define ZSTD_WORKSPACETOOLARGE_FACTOR 3
336
+
337
+ /* when workspace is continuously too large
338
+ * during at least this number of times,
339
+ * context's memory usage is considered wasteful,
340
+ * because it's sized to handle a worst case scenario which rarely happens.
341
+ * In which case, resize it down to free some memory */
342
+ #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128
343
+
344
+ /* Controls whether the input/output buffer is buffered or stable. */
345
+ typedef enum {
346
+ ZSTD_bm_buffered = 0, /* Buffer the input/output */
347
+ ZSTD_bm_stable = 1 /* ZSTD_inBuffer/ZSTD_outBuffer is stable */
348
+ } ZSTD_bufferMode_e;
272
349
 
273
350
 
274
351
  /*-*******************************************
275
352
  * Private declarations
276
353
  *********************************************/
277
354
  typedef struct seqDef_s {
278
- U32 offset;
355
+ U32 offset; /* offset == rawOffset + ZSTD_REP_NUM, or equivalently, offCode + 1 */
279
356
  U16 litLength;
280
357
  U16 matchLength;
281
358
  } seqDef;
282
359
 
360
+ /* Controls whether seqStore has a single "long" litLength or matchLength. See seqStore_t. */
361
+ typedef enum {
362
+ ZSTD_llt_none = 0, /* no longLengthType */
363
+ ZSTD_llt_literalLength = 1, /* represents a long literal */
364
+ ZSTD_llt_matchLength = 2 /* represents a long match */
365
+ } ZSTD_longLengthType_e;
366
+
283
367
  typedef struct {
284
368
  seqDef* sequencesStart;
285
- seqDef* sequences;
369
+ seqDef* sequences; /* ptr to end of sequences */
286
370
  BYTE* litStart;
287
- BYTE* lit;
371
+ BYTE* lit; /* ptr to end of literals */
288
372
  BYTE* llCode;
289
373
  BYTE* mlCode;
290
374
  BYTE* ofCode;
291
375
  size_t maxNbSeq;
292
376
  size_t maxNbLit;
293
- U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
294
- U32 longLengthPos;
377
+
378
+ /* longLengthPos and longLengthType to allow us to represent either a single litLength or matchLength
379
+ * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment
380
+ * the existing value of the litLength or matchLength by 0x10000.
381
+ */
382
+ ZSTD_longLengthType_e longLengthType;
383
+ U32 longLengthPos; /* Index of the sequence to apply long length modification to */
295
384
  } seqStore_t;
296
385
 
386
+ typedef struct {
387
+ U32 litLength;
388
+ U32 matchLength;
389
+ } ZSTD_sequenceLength;
390
+
391
+ /**
392
+ * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences
393
+ * indicated by longLengthPos and longLengthType, and adds MINMATCH back to matchLength.
394
+ */
395
+ MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq)
396
+ {
397
+ ZSTD_sequenceLength seqLen;
398
+ seqLen.litLength = seq->litLength;
399
+ seqLen.matchLength = seq->matchLength + MINMATCH;
400
+ if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {
401
+ if (seqStore->longLengthType == ZSTD_llt_literalLength) {
402
+ seqLen.litLength += 0xFFFF;
403
+ }
404
+ if (seqStore->longLengthType == ZSTD_llt_matchLength) {
405
+ seqLen.matchLength += 0xFFFF;
406
+ }
407
+ }
408
+ return seqLen;
409
+ }
410
+
297
411
  /**
298
412
  * Contains the compressed frame size and an upper-bound for the decompressed frame size.
299
413
  * Note: before using `compressedSize`, check for errors using ZSTD_isError().
@@ -309,9 +423,9 @@ const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBu
309
423
  void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
310
424
 
311
425
  /* custom memory allocation functions */
312
- void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
313
- void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
314
- void ZSTD_free(void* ptr, ZSTD_customMem customMem);
426
+ void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem);
427
+ void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem);
428
+ void ZSTD_customFree(void* ptr, ZSTD_customMem customMem);
315
429
 
316
430
 
317
431
  MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
@@ -319,11 +433,16 @@ MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus
319
433
  assert(val != 0);
320
434
  {
321
435
  # if defined(_MSC_VER) /* Visual */
322
- unsigned long r=0;
323
- _BitScanReverse(&r, val);
324
- return (unsigned)r;
436
+ # if STATIC_BMI2 == 1
437
+ return _lzcnt_u32(val)^31;
438
+ # else
439
+ unsigned long r=0;
440
+ return _BitScanReverse(&r, val) ? (unsigned)r : 0;
441
+ # endif
325
442
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
326
- return 31 - __builtin_clz(val);
443
+ return __builtin_clz (val) ^ 31;
444
+ # elif defined(__ICCARM__) /* IAR Intrinsic */
445
+ return 31 - __CLZ(val);
327
446
  # else /* Software version */
328
447
  static const U32 DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
329
448
  U32 v = val;