zstd-ruby 1.3.8.0 → 1.4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -5
  3. data/README.md +1 -1
  4. data/ext/zstdruby/libzstd/Makefile +133 -61
  5. data/ext/zstdruby/libzstd/README.md +51 -18
  6. data/ext/zstdruby/libzstd/common/bitstream.h +38 -39
  7. data/ext/zstdruby/libzstd/common/compiler.h +41 -6
  8. data/ext/zstdruby/libzstd/common/cpu.h +1 -1
  9. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  10. data/ext/zstdruby/libzstd/common/debug.h +11 -31
  11. data/ext/zstdruby/libzstd/common/entropy_common.c +13 -33
  12. data/ext/zstdruby/libzstd/common/error_private.c +2 -1
  13. data/ext/zstdruby/libzstd/common/error_private.h +6 -2
  14. data/ext/zstdruby/libzstd/common/fse.h +13 -33
  15. data/ext/zstdruby/libzstd/common/fse_decompress.c +12 -35
  16. data/ext/zstdruby/libzstd/common/huf.h +15 -33
  17. data/ext/zstdruby/libzstd/common/mem.h +75 -2
  18. data/ext/zstdruby/libzstd/common/pool.c +8 -4
  19. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  20. data/ext/zstdruby/libzstd/common/threading.c +52 -6
  21. data/ext/zstdruby/libzstd/common/threading.h +36 -4
  22. data/ext/zstdruby/libzstd/common/xxhash.c +25 -37
  23. data/ext/zstdruby/libzstd/common/xxhash.h +11 -31
  24. data/ext/zstdruby/libzstd/common/zstd_common.c +1 -1
  25. data/ext/zstdruby/libzstd/common/zstd_errors.h +2 -1
  26. data/ext/zstdruby/libzstd/common/zstd_internal.h +203 -22
  27. data/ext/zstdruby/libzstd/compress/fse_compress.c +19 -42
  28. data/ext/zstdruby/libzstd/compress/hist.c +15 -35
  29. data/ext/zstdruby/libzstd/compress/hist.h +12 -32
  30. data/ext/zstdruby/libzstd/compress/huf_compress.c +92 -92
  31. data/ext/zstdruby/libzstd/compress/zstd_compress.c +1460 -1472
  32. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +330 -65
  33. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +158 -0
  34. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +29 -0
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +419 -0
  36. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +54 -0
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +845 -0
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  39. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +525 -0
  40. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +65 -43
  41. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  42. data/ext/zstdruby/libzstd/compress/zstd_fast.c +264 -159
  43. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  44. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +74 -42
  45. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +2 -2
  46. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +33 -11
  47. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +7 -2
  48. data/ext/zstdruby/libzstd/compress/zstd_opt.c +108 -125
  49. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  50. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +129 -93
  51. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +46 -28
  52. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +76 -60
  53. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +14 -10
  54. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  55. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +471 -258
  56. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +471 -346
  57. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +3 -3
  58. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +25 -4
  59. data/ext/zstdruby/libzstd/deprecated/zbuff.h +9 -8
  60. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +2 -2
  61. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -1
  62. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  63. data/ext/zstdruby/libzstd/dictBuilder/cover.c +220 -65
  64. data/ext/zstdruby/libzstd/dictBuilder/cover.h +81 -7
  65. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +85 -56
  66. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +43 -19
  67. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +73 -35
  68. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  69. data/ext/zstdruby/libzstd/dll/example/build_package.bat +3 -2
  70. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +49 -15
  71. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +142 -117
  72. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +13 -8
  73. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +54 -25
  74. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +13 -8
  75. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +55 -25
  76. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +13 -8
  77. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +62 -29
  78. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +13 -8
  79. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +145 -109
  80. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +14 -9
  81. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +56 -26
  82. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +11 -6
  83. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +65 -28
  84. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +11 -6
  85. data/ext/zstdruby/libzstd/libzstd.pc.in +3 -2
  86. data/ext/zstdruby/libzstd/zstd.h +921 -597
  87. data/lib/zstd-ruby/version.rb +1 -1
  88. data/zstd-ruby.gemspec +2 -2
  89. metadata +19 -14
  90. data/ext/zstdruby/libzstd/dll/libzstd.def +0 -87
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, 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
@@ -46,7 +46,12 @@ extern "C" {
46
46
  * The resulting dictionary will be saved into `dictBuffer`.
47
47
  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
48
48
  * or an error code, which can be tested with ZDICT_isError().
49
- * Note: ZDICT_trainFromBuffer() requires about 9 bytes of memory for each input byte.
49
+ * Note: Dictionary training will fail if there are not enough samples to construct a
50
+ * dictionary, or if most of the samples are too small (< 8 bytes being the lower limit).
51
+ * If dictionary training fails, you should use zstd without a dictionary, as the dictionary
52
+ * would've been ineffective anyways. If you believe your samples would benefit from a dictionary
53
+ * please open an issue with details, and we can look into it.
54
+ * Note: ZDICT_trainFromBuffer()'s memory usage is about 6 MB.
50
55
  * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
51
56
  * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
52
57
  * In general, it's recommended to provide a few thousands samples, though this can vary a lot.
@@ -56,9 +61,57 @@ ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCap
56
61
  const void* samplesBuffer,
57
62
  const size_t* samplesSizes, unsigned nbSamples);
58
63
 
64
+ typedef struct {
65
+ int compressionLevel; /*< optimize for a specific zstd compression level; 0 means default */
66
+ unsigned notificationLevel; /*< Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */
67
+ unsigned dictID; /*< force dictID value; 0 means auto mode (32-bits random value) */
68
+ } ZDICT_params_t;
69
+
70
+ /*! ZDICT_finalizeDictionary():
71
+ * Given a custom content as a basis for dictionary, and a set of samples,
72
+ * finalize dictionary by adding headers and statistics according to the zstd
73
+ * dictionary format.
74
+ *
75
+ * Samples must be stored concatenated in a flat buffer `samplesBuffer`,
76
+ * supplied with an array of sizes `samplesSizes`, providing the size of each
77
+ * sample in order. The samples are used to construct the statistics, so they
78
+ * should be representative of what you will compress with this dictionary.
79
+ *
80
+ * The compression level can be set in `parameters`. You should pass the
81
+ * compression level you expect to use in production. The statistics for each
82
+ * compression level differ, so tuning the dictionary for the compression level
83
+ * can help quite a bit.
84
+ *
85
+ * You can set an explicit dictionary ID in `parameters`, or allow us to pick
86
+ * a random dictionary ID for you, but we can't guarantee no collisions.
87
+ *
88
+ * The dstDictBuffer and the dictContent may overlap, and the content will be
89
+ * appended to the end of the header. If the header + the content doesn't fit in
90
+ * maxDictSize the beginning of the content is truncated to make room, since it
91
+ * is presumed that the most profitable content is at the end of the dictionary,
92
+ * since that is the cheapest to reference.
93
+ *
94
+ * `dictContentSize` must be >= ZDICT_CONTENTSIZE_MIN bytes.
95
+ * `maxDictSize` must be >= max(dictContentSize, ZSTD_DICTSIZE_MIN).
96
+ *
97
+ * @return: size of dictionary stored into `dstDictBuffer` (<= `maxDictSize`),
98
+ * or an error code, which can be tested by ZDICT_isError().
99
+ * Note: ZDICT_finalizeDictionary() will push notifications into stderr if
100
+ * instructed to, using notificationLevel>0.
101
+ * NOTE: This function currently may fail in several edge cases including:
102
+ * * Not enough samples
103
+ * * Samples are uncompressible
104
+ * * Samples are all exactly the same
105
+ */
106
+ ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dstDictBuffer, size_t maxDictSize,
107
+ const void* dictContent, size_t dictContentSize,
108
+ const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
109
+ ZDICT_params_t parameters);
110
+
59
111
 
60
112
  /*====== Helper functions ======*/
61
113
  ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize); /**< extracts dictID; @return zero if error (not a valid dictionary) */
114
+ ZDICTLIB_API size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize); /* returns dict header size; returns a ZSTD error code on failure */
62
115
  ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode);
63
116
  ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode);
64
117
 
@@ -73,11 +126,8 @@ ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode);
73
126
  * Use them only in association with static linking.
74
127
  * ==================================================================================== */
75
128
 
76
- typedef struct {
77
- int compressionLevel; /* optimize for a specific zstd compression level; 0 means default */
78
- unsigned notificationLevel; /* Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */
79
- unsigned dictID; /* force dictID value; 0 means auto mode (32-bits random value) */
80
- } ZDICT_params_t;
129
+ #define ZDICT_CONTENTSIZE_MIN 128
130
+ #define ZDICT_DICTSIZE_MIN 256
81
131
 
82
132
  /*! ZDICT_cover_params_t:
83
133
  * k and d are the only required parameters.
@@ -89,6 +139,8 @@ typedef struct {
89
139
  unsigned steps; /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */
90
140
  unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
91
141
  double splitPoint; /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (1.0), 1.0 when all samples are used for both training and testing */
142
+ unsigned shrinkDict; /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking */
143
+ unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */
92
144
  ZDICT_params_t zParams;
93
145
  } ZDICT_cover_params_t;
94
146
 
@@ -100,6 +152,9 @@ typedef struct {
100
152
  unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
101
153
  double splitPoint; /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (0.75), 1.0 when all samples are used for both training and testing */
102
154
  unsigned accel; /* Acceleration level: constraint: 0 < accel <= 10, higher means faster and less accurate, 0 means default(1) */
155
+ unsigned shrinkDict; /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking */
156
+ unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */
157
+
103
158
  ZDICT_params_t zParams;
104
159
  } ZDICT_fastCover_params_t;
105
160
 
@@ -110,6 +165,7 @@ typedef struct {
110
165
  * The resulting dictionary will be saved into `dictBuffer`.
111
166
  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
112
167
  * or an error code, which can be tested with ZDICT_isError().
168
+ * See ZDICT_trainFromBuffer() for details on failure modes.
113
169
  * Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte.
114
170
  * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
115
171
  * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
@@ -133,8 +189,9 @@ ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover(
133
189
  * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000].
134
190
  *
135
191
  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
136
- * or an error code, which can be tested with ZDICT_isError().
137
- * On success `*parameters` contains the parameters selected.
192
+ * or an error code, which can be tested with ZDICT_isError().
193
+ * On success `*parameters` contains the parameters selected.
194
+ * See ZDICT_trainFromBuffer() for details on failure modes.
138
195
  * Note: ZDICT_optimizeTrainFromBuffer_cover() requires about 8 bytes of memory for each input byte and additionally another 5 bytes of memory for each byte of memory for each thread.
139
196
  */
140
197
  ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover(
@@ -151,7 +208,8 @@ ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover(
151
208
  * The resulting dictionary will be saved into `dictBuffer`.
152
209
  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
153
210
  * or an error code, which can be tested with ZDICT_isError().
154
- * Note: ZDICT_trainFromBuffer_fastCover() requires about 1 bytes of memory for each input byte and additionally another 6 * 2^f bytes of memory .
211
+ * See ZDICT_trainFromBuffer() for details on failure modes.
212
+ * Note: ZDICT_trainFromBuffer_fastCover() requires 6 * 2^f bytes of memory.
155
213
  * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
156
214
  * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
157
215
  * In general, it's recommended to provide a few thousands samples, though this can vary a lot.
@@ -175,37 +233,16 @@ ZDICTLIB_API size_t ZDICT_trainFromBuffer_fastCover(void *dictBuffer,
175
233
  * If accel is zero, default value of 1 is used.
176
234
  *
177
235
  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
178
- * or an error code, which can be tested with ZDICT_isError().
179
- * On success `*parameters` contains the parameters selected.
180
- * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 1 byte of memory for each input byte and additionally another 6 * 2^f bytes of memory for each thread.
236
+ * or an error code, which can be tested with ZDICT_isError().
237
+ * On success `*parameters` contains the parameters selected.
238
+ * See ZDICT_trainFromBuffer() for details on failure modes.
239
+ * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 6 * 2^f bytes of memory for each thread.
181
240
  */
182
241
  ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_fastCover(void* dictBuffer,
183
242
  size_t dictBufferCapacity, const void* samplesBuffer,
184
243
  const size_t* samplesSizes, unsigned nbSamples,
185
244
  ZDICT_fastCover_params_t* parameters);
186
245
 
187
- /*! ZDICT_finalizeDictionary():
188
- * Given a custom content as a basis for dictionary, and a set of samples,
189
- * finalize dictionary by adding headers and statistics.
190
- *
191
- * Samples must be stored concatenated in a flat buffer `samplesBuffer`,
192
- * supplied with an array of sizes `samplesSizes`, providing the size of each sample in order.
193
- *
194
- * dictContentSize must be >= ZDICT_CONTENTSIZE_MIN bytes.
195
- * maxDictSize must be >= dictContentSize, and must be >= ZDICT_DICTSIZE_MIN bytes.
196
- *
197
- * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`),
198
- * or an error code, which can be tested by ZDICT_isError().
199
- * Note: ZDICT_finalizeDictionary() will push notifications into stderr if instructed to, using notificationLevel>0.
200
- * Note 2: dictBuffer and dictContent can overlap
201
- */
202
- #define ZDICT_CONTENTSIZE_MIN 128
203
- #define ZDICT_DICTSIZE_MIN 256
204
- ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dictBuffer, size_t dictBufferCapacity,
205
- const void* dictContent, size_t dictContentSize,
206
- const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
207
- ZDICT_params_t parameters);
208
-
209
246
  typedef struct {
210
247
  unsigned selectivityLevel; /* 0 means default; larger => select more => larger dictionary */
211
248
  ZDICT_params_t zParams;
@@ -219,6 +256,7 @@ typedef struct {
219
256
  * `parameters` is optional and can be provided with values set to 0 to mean "default".
220
257
  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
221
258
  * or an error code, which can be tested with ZDICT_isError().
259
+ * See ZDICT_trainFromBuffer() for details on failure modes.
222
260
  * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
223
261
  * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
224
262
  * In general, it's recommended to provide a few thousands samples, though this can vary a lot.
@@ -1,10 +1,11 @@
1
1
  # ################################################################
2
- # Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ # Copyright (c) 2016-2020, 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
6
6
  # LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
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.
8
9
  # ################################################################
9
10
 
10
11
  VOID := /dev/null
@@ -6,14 +6,15 @@ COPY programs\datagen.h bin\example\
6
6
  COPY programs\util.h bin\example\
7
7
  COPY programs\platform.h bin\example\
8
8
  COPY lib\common\mem.h bin\example\
9
- COPY lib\common\zstd_errors.h bin\example\
10
9
  COPY lib\common\zstd_internal.h bin\example\
11
10
  COPY lib\common\error_private.h bin\example\
12
11
  COPY lib\common\xxhash.h bin\example\
13
- COPY lib\zstd.h bin\include\
14
12
  COPY lib\libzstd.a bin\static\libzstd_static.lib
15
13
  COPY lib\dll\libzstd.* bin\dll\
16
14
  COPY lib\dll\example\Makefile bin\example\
17
15
  COPY lib\dll\example\fullbench-dll.* bin\example\
18
16
  COPY lib\dll\example\README.md bin\
17
+ COPY lib\zstd.h bin\include\
18
+ COPY lib\common\zstd_errors.h bin\include\
19
+ COPY lib\dictBuilder\zdict.h bin\include\
19
20
  COPY programs\zstd.exe bin\zstd.exe
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, 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
@@ -18,9 +18,9 @@ extern "C" {
18
18
  /* *************************************
19
19
  * Includes
20
20
  ***************************************/
21
- #include "mem.h" /* MEM_STATIC */
22
- #include "error_private.h" /* ERROR */
23
- #include "zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer */
21
+ #include "../common/mem.h" /* MEM_STATIC */
22
+ #include "../common/error_private.h" /* ERROR */
23
+ #include "../common/zstd_internal.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTD_frameSizeInfo */
24
24
 
25
25
  #if !defined (ZSTD_LEGACY_SUPPORT) || (ZSTD_LEGACY_SUPPORT == 0)
26
26
  # undef ZSTD_LEGACY_SUPPORT
@@ -178,43 +178,77 @@ MEM_STATIC size_t ZSTD_decompressLegacy(
178
178
  }
179
179
  }
180
180
 
181
- MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src,
182
- size_t compressedSize)
181
+ MEM_STATIC ZSTD_frameSizeInfo ZSTD_findFrameSizeInfoLegacy(const void *src, size_t srcSize)
183
182
  {
184
- U32 const version = ZSTD_isLegacy(src, compressedSize);
183
+ ZSTD_frameSizeInfo frameSizeInfo;
184
+ U32 const version = ZSTD_isLegacy(src, srcSize);
185
185
  switch(version)
186
186
  {
187
187
  #if (ZSTD_LEGACY_SUPPORT <= 1)
188
188
  case 1 :
189
- return ZSTDv01_findFrameCompressedSize(src, compressedSize);
189
+ ZSTDv01_findFrameSizeInfoLegacy(src, srcSize,
190
+ &frameSizeInfo.compressedSize,
191
+ &frameSizeInfo.decompressedBound);
192
+ break;
190
193
  #endif
191
194
  #if (ZSTD_LEGACY_SUPPORT <= 2)
192
195
  case 2 :
193
- return ZSTDv02_findFrameCompressedSize(src, compressedSize);
196
+ ZSTDv02_findFrameSizeInfoLegacy(src, srcSize,
197
+ &frameSizeInfo.compressedSize,
198
+ &frameSizeInfo.decompressedBound);
199
+ break;
194
200
  #endif
195
201
  #if (ZSTD_LEGACY_SUPPORT <= 3)
196
202
  case 3 :
197
- return ZSTDv03_findFrameCompressedSize(src, compressedSize);
203
+ ZSTDv03_findFrameSizeInfoLegacy(src, srcSize,
204
+ &frameSizeInfo.compressedSize,
205
+ &frameSizeInfo.decompressedBound);
206
+ break;
198
207
  #endif
199
208
  #if (ZSTD_LEGACY_SUPPORT <= 4)
200
209
  case 4 :
201
- return ZSTDv04_findFrameCompressedSize(src, compressedSize);
210
+ ZSTDv04_findFrameSizeInfoLegacy(src, srcSize,
211
+ &frameSizeInfo.compressedSize,
212
+ &frameSizeInfo.decompressedBound);
213
+ break;
202
214
  #endif
203
215
  #if (ZSTD_LEGACY_SUPPORT <= 5)
204
216
  case 5 :
205
- return ZSTDv05_findFrameCompressedSize(src, compressedSize);
217
+ ZSTDv05_findFrameSizeInfoLegacy(src, srcSize,
218
+ &frameSizeInfo.compressedSize,
219
+ &frameSizeInfo.decompressedBound);
220
+ break;
206
221
  #endif
207
222
  #if (ZSTD_LEGACY_SUPPORT <= 6)
208
223
  case 6 :
209
- return ZSTDv06_findFrameCompressedSize(src, compressedSize);
224
+ ZSTDv06_findFrameSizeInfoLegacy(src, srcSize,
225
+ &frameSizeInfo.compressedSize,
226
+ &frameSizeInfo.decompressedBound);
227
+ break;
210
228
  #endif
211
229
  #if (ZSTD_LEGACY_SUPPORT <= 7)
212
230
  case 7 :
213
- return ZSTDv07_findFrameCompressedSize(src, compressedSize);
231
+ ZSTDv07_findFrameSizeInfoLegacy(src, srcSize,
232
+ &frameSizeInfo.compressedSize,
233
+ &frameSizeInfo.decompressedBound);
234
+ break;
214
235
  #endif
215
236
  default :
216
- return ERROR(prefix_unknown);
237
+ frameSizeInfo.compressedSize = ERROR(prefix_unknown);
238
+ frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
239
+ break;
240
+ }
241
+ if (!ZSTD_isError(frameSizeInfo.compressedSize) && frameSizeInfo.compressedSize > srcSize) {
242
+ frameSizeInfo.compressedSize = ERROR(srcSize_wrong);
243
+ frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
217
244
  }
245
+ return frameSizeInfo;
246
+ }
247
+
248
+ MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src, size_t srcSize)
249
+ {
250
+ ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfoLegacy(src, srcSize);
251
+ return frameSizeInfo.compressedSize;
218
252
  }
219
253
 
220
254
  MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, 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
@@ -14,7 +14,7 @@
14
14
  ******************************************/
15
15
  #include <stddef.h> /* size_t, ptrdiff_t */
16
16
  #include "zstd_v01.h"
17
- #include "error_private.h"
17
+ #include "../common/error_private.h"
18
18
 
19
19
 
20
20
  /******************************************
@@ -257,7 +257,7 @@ static U64 FSE_read64(const void* memPtr)
257
257
  U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
258
258
  }
259
259
 
260
- #endif // FSE_FORCE_MEMORY_ACCESS
260
+ #endif /* FSE_FORCE_MEMORY_ACCESS */
261
261
 
262
262
  static U16 FSE_readLE16(const void* memPtr)
263
263
  {
@@ -346,7 +346,7 @@ FORCE_INLINE unsigned FSE_highbit32 (U32 val)
346
346
  _BitScanReverse ( &r, val );
347
347
  return (unsigned) r;
348
348
  # elif defined(__GNUC__) && (GCC_VERSION >= 304) /* GCC Intrinsic */
349
- return 31 - __builtin_clz (val);
349
+ return __builtin_clz (val) ^ 31;
350
350
  # else /* Software version */
351
351
  static const unsigned 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 };
352
352
  U32 v = val;
@@ -1073,99 +1073,102 @@ static size_t HUF_decompress_usingDTable( /* -3% slower when non static */
1073
1073
  const void* cSrc, size_t cSrcSize,
1074
1074
  const U16* DTable)
1075
1075
  {
1076
- BYTE* const ostart = (BYTE*) dst;
1077
- BYTE* op = ostart;
1078
- BYTE* const omax = op + maxDstSize;
1079
- BYTE* const olimit = omax-15;
1080
-
1081
- const void* ptr = DTable;
1082
- const HUF_DElt* const dt = (const HUF_DElt*)(ptr)+1;
1083
- const U32 dtLog = DTable[0];
1084
- size_t errorCode;
1085
- U32 reloadStatus;
1086
-
1087
- /* Init */
1088
-
1089
- const U16* jumpTable = (const U16*)cSrc;
1090
- const size_t length1 = FSE_readLE16(jumpTable);
1091
- const size_t length2 = FSE_readLE16(jumpTable+1);
1092
- const size_t length3 = FSE_readLE16(jumpTable+2);
1093
- const size_t length4 = cSrcSize - 6 - length1 - length2 - length3; // check coherency !!
1094
- const char* const start1 = (const char*)(cSrc) + 6;
1095
- const char* const start2 = start1 + length1;
1096
- const char* const start3 = start2 + length2;
1097
- const char* const start4 = start3 + length3;
1098
- FSE_DStream_t bitD1, bitD2, bitD3, bitD4;
1099
-
1100
- if (length1+length2+length3+6 >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong;
1101
-
1102
- errorCode = FSE_initDStream(&bitD1, start1, length1);
1103
- if (FSE_isError(errorCode)) return errorCode;
1104
- errorCode = FSE_initDStream(&bitD2, start2, length2);
1105
- if (FSE_isError(errorCode)) return errorCode;
1106
- errorCode = FSE_initDStream(&bitD3, start3, length3);
1107
- if (FSE_isError(errorCode)) return errorCode;
1108
- errorCode = FSE_initDStream(&bitD4, start4, length4);
1109
- if (FSE_isError(errorCode)) return errorCode;
1110
-
1111
- reloadStatus=FSE_reloadDStream(&bitD2);
1112
-
1113
- /* 16 symbols per loop */
1114
- for ( ; (reloadStatus<FSE_DStream_completed) && (op<olimit); /* D2-3-4 are supposed to be synchronized and finish together */
1115
- op+=16, reloadStatus = FSE_reloadDStream(&bitD2) | FSE_reloadDStream(&bitD3) | FSE_reloadDStream(&bitD4), FSE_reloadDStream(&bitD1))
1076
+ if (cSrcSize < 6) return (size_t)-FSE_ERROR_srcSize_wrong;
1116
1077
  {
1117
- #define HUF_DECODE_SYMBOL_0(n, Dstream) \
1118
- op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog);
1119
-
1120
- #define HUF_DECODE_SYMBOL_1(n, Dstream) \
1121
- op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \
1122
- if (FSE_32bits() && (HUF_MAX_TABLELOG>12)) FSE_reloadDStream(&Dstream)
1123
-
1124
- #define HUF_DECODE_SYMBOL_2(n, Dstream) \
1125
- op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \
1126
- if (FSE_32bits()) FSE_reloadDStream(&Dstream)
1127
-
1128
- HUF_DECODE_SYMBOL_1( 0, bitD1);
1129
- HUF_DECODE_SYMBOL_1( 1, bitD2);
1130
- HUF_DECODE_SYMBOL_1( 2, bitD3);
1131
- HUF_DECODE_SYMBOL_1( 3, bitD4);
1132
- HUF_DECODE_SYMBOL_2( 4, bitD1);
1133
- HUF_DECODE_SYMBOL_2( 5, bitD2);
1134
- HUF_DECODE_SYMBOL_2( 6, bitD3);
1135
- HUF_DECODE_SYMBOL_2( 7, bitD4);
1136
- HUF_DECODE_SYMBOL_1( 8, bitD1);
1137
- HUF_DECODE_SYMBOL_1( 9, bitD2);
1138
- HUF_DECODE_SYMBOL_1(10, bitD3);
1139
- HUF_DECODE_SYMBOL_1(11, bitD4);
1140
- HUF_DECODE_SYMBOL_0(12, bitD1);
1141
- HUF_DECODE_SYMBOL_0(13, bitD2);
1142
- HUF_DECODE_SYMBOL_0(14, bitD3);
1143
- HUF_DECODE_SYMBOL_0(15, bitD4);
1144
- }
1078
+ BYTE* const ostart = (BYTE*) dst;
1079
+ BYTE* op = ostart;
1080
+ BYTE* const omax = op + maxDstSize;
1081
+ BYTE* const olimit = maxDstSize < 15 ? op : omax-15;
1082
+
1083
+ const void* ptr = DTable;
1084
+ const HUF_DElt* const dt = (const HUF_DElt*)(ptr)+1;
1085
+ const U32 dtLog = DTable[0];
1086
+ size_t errorCode;
1087
+ U32 reloadStatus;
1088
+
1089
+ /* Init */
1090
+
1091
+ const U16* jumpTable = (const U16*)cSrc;
1092
+ const size_t length1 = FSE_readLE16(jumpTable);
1093
+ const size_t length2 = FSE_readLE16(jumpTable+1);
1094
+ const size_t length3 = FSE_readLE16(jumpTable+2);
1095
+ const size_t length4 = cSrcSize - 6 - length1 - length2 - length3; /* check coherency !! */
1096
+ const char* const start1 = (const char*)(cSrc) + 6;
1097
+ const char* const start2 = start1 + length1;
1098
+ const char* const start3 = start2 + length2;
1099
+ const char* const start4 = start3 + length3;
1100
+ FSE_DStream_t bitD1, bitD2, bitD3, bitD4;
1101
+
1102
+ if (length1+length2+length3+6 >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong;
1103
+
1104
+ errorCode = FSE_initDStream(&bitD1, start1, length1);
1105
+ if (FSE_isError(errorCode)) return errorCode;
1106
+ errorCode = FSE_initDStream(&bitD2, start2, length2);
1107
+ if (FSE_isError(errorCode)) return errorCode;
1108
+ errorCode = FSE_initDStream(&bitD3, start3, length3);
1109
+ if (FSE_isError(errorCode)) return errorCode;
1110
+ errorCode = FSE_initDStream(&bitD4, start4, length4);
1111
+ if (FSE_isError(errorCode)) return errorCode;
1112
+
1113
+ reloadStatus=FSE_reloadDStream(&bitD2);
1114
+
1115
+ /* 16 symbols per loop */
1116
+ for ( ; (reloadStatus<FSE_DStream_completed) && (op<olimit); /* D2-3-4 are supposed to be synchronized and finish together */
1117
+ op+=16, reloadStatus = FSE_reloadDStream(&bitD2) | FSE_reloadDStream(&bitD3) | FSE_reloadDStream(&bitD4), FSE_reloadDStream(&bitD1))
1118
+ {
1119
+ #define HUF_DECODE_SYMBOL_0(n, Dstream) \
1120
+ op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog);
1121
+
1122
+ #define HUF_DECODE_SYMBOL_1(n, Dstream) \
1123
+ op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \
1124
+ if (FSE_32bits() && (HUF_MAX_TABLELOG>12)) FSE_reloadDStream(&Dstream)
1125
+
1126
+ #define HUF_DECODE_SYMBOL_2(n, Dstream) \
1127
+ op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \
1128
+ if (FSE_32bits()) FSE_reloadDStream(&Dstream)
1129
+
1130
+ HUF_DECODE_SYMBOL_1( 0, bitD1);
1131
+ HUF_DECODE_SYMBOL_1( 1, bitD2);
1132
+ HUF_DECODE_SYMBOL_1( 2, bitD3);
1133
+ HUF_DECODE_SYMBOL_1( 3, bitD4);
1134
+ HUF_DECODE_SYMBOL_2( 4, bitD1);
1135
+ HUF_DECODE_SYMBOL_2( 5, bitD2);
1136
+ HUF_DECODE_SYMBOL_2( 6, bitD3);
1137
+ HUF_DECODE_SYMBOL_2( 7, bitD4);
1138
+ HUF_DECODE_SYMBOL_1( 8, bitD1);
1139
+ HUF_DECODE_SYMBOL_1( 9, bitD2);
1140
+ HUF_DECODE_SYMBOL_1(10, bitD3);
1141
+ HUF_DECODE_SYMBOL_1(11, bitD4);
1142
+ HUF_DECODE_SYMBOL_0(12, bitD1);
1143
+ HUF_DECODE_SYMBOL_0(13, bitD2);
1144
+ HUF_DECODE_SYMBOL_0(14, bitD3);
1145
+ HUF_DECODE_SYMBOL_0(15, bitD4);
1146
+ }
1145
1147
 
1146
- if (reloadStatus!=FSE_DStream_completed) /* not complete : some bitStream might be FSE_DStream_unfinished */
1147
- return (size_t)-FSE_ERROR_corruptionDetected;
1148
+ if (reloadStatus!=FSE_DStream_completed) /* not complete : some bitStream might be FSE_DStream_unfinished */
1149
+ return (size_t)-FSE_ERROR_corruptionDetected;
1148
1150
 
1149
- /* tail */
1150
- {
1151
- // bitTail = bitD1; // *much* slower : -20% !??!
1152
- FSE_DStream_t bitTail;
1153
- bitTail.ptr = bitD1.ptr;
1154
- bitTail.bitsConsumed = bitD1.bitsConsumed;
1155
- bitTail.bitContainer = bitD1.bitContainer; // required in case of FSE_DStream_endOfBuffer
1156
- bitTail.start = start1;
1157
- for ( ; (FSE_reloadDStream(&bitTail) < FSE_DStream_completed) && (op<omax) ; op++)
1151
+ /* tail */
1158
1152
  {
1159
- HUF_DECODE_SYMBOL_0(0, bitTail);
1153
+ /* bitTail = bitD1; */ /* *much* slower : -20% !??! */
1154
+ FSE_DStream_t bitTail;
1155
+ bitTail.ptr = bitD1.ptr;
1156
+ bitTail.bitsConsumed = bitD1.bitsConsumed;
1157
+ bitTail.bitContainer = bitD1.bitContainer; /* required in case of FSE_DStream_endOfBuffer */
1158
+ bitTail.start = start1;
1159
+ for ( ; (FSE_reloadDStream(&bitTail) < FSE_DStream_completed) && (op<omax) ; op++)
1160
+ {
1161
+ HUF_DECODE_SYMBOL_0(0, bitTail);
1162
+ }
1163
+
1164
+ if (FSE_endOfDStream(&bitTail))
1165
+ return op-ostart;
1160
1166
  }
1161
1167
 
1162
- if (FSE_endOfDStream(&bitTail))
1163
- return op-ostart;
1164
- }
1168
+ if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */
1165
1169
 
1166
- if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */
1167
-
1168
- return (size_t)-FSE_ERROR_corruptionDetected;
1170
+ return (size_t)-FSE_ERROR_corruptionDetected;
1171
+ }
1169
1172
  }
1170
1173
 
1171
1174
 
@@ -1336,6 +1339,8 @@ static const U32 ZSTD_magicNumber = 0xFD2FB51E; /* 3rd version : seqNb header
1336
1339
  #define LITERAL_NOENTROPY 63
1337
1340
  #define COMMAND_NOENTROPY 7 /* to remove */
1338
1341
 
1342
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
1343
+
1339
1344
  static const size_t ZSTD_blockHeaderSize = 3;
1340
1345
  static const size_t ZSTD_frameHeaderSize = 4;
1341
1346
 
@@ -1353,8 +1358,6 @@ static unsigned ZSTD_isLittleEndian(void)
1353
1358
 
1354
1359
  static U16 ZSTD_read16(const void* p) { U16 r; memcpy(&r, p, sizeof(r)); return r; }
1355
1360
 
1356
- static U32 ZSTD_read32(const void* p) { U32 r; memcpy(&r, p, sizeof(r)); return r; }
1357
-
1358
1361
  static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
1359
1362
 
1360
1363
  static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
@@ -1379,16 +1382,9 @@ static U16 ZSTD_readLE16(const void* memPtr)
1379
1382
  }
1380
1383
  }
1381
1384
 
1382
-
1383
- static U32 ZSTD_readLE32(const void* memPtr)
1385
+ static U32 ZSTD_readLE24(const void* memPtr)
1384
1386
  {
1385
- if (ZSTD_isLittleEndian())
1386
- return ZSTD_read32(memPtr);
1387
- else
1388
- {
1389
- const BYTE* p = (const BYTE*)memPtr;
1390
- return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
1391
- }
1387
+ return ZSTD_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
1392
1388
  }
1393
1389
 
1394
1390
  static U32 ZSTD_readBE32(const void* memPtr)
@@ -1487,7 +1483,9 @@ static size_t ZSTDv01_getcBlockSize(const void* src, size_t srcSize, blockProper
1487
1483
  static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
1488
1484
  {
1489
1485
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
1490
- memcpy(dst, src, srcSize);
1486
+ if (srcSize > 0) {
1487
+ memcpy(dst, src, srcSize);
1488
+ }
1491
1489
  return srcSize;
1492
1490
  }
1493
1491
 
@@ -1506,7 +1504,7 @@ static size_t ZSTD_decompressLiterals(void* ctx,
1506
1504
  if (srcSize <= 3) return ERROR(corruption_detected);
1507
1505
 
1508
1506
  litSize = ip[1] + (ip[0]<<8);
1509
- litSize += ((ip[-3] >> 3) & 7) << 16; // mmmmh....
1507
+ litSize += ((ip[-3] >> 3) & 7) << 16; /* mmmmh.... */
1510
1508
  op = oend - litSize;
1511
1509
 
1512
1510
  (void)ctx;
@@ -1545,7 +1543,9 @@ static size_t ZSTDv01_decodeLiteralsBlock(void* ctx,
1545
1543
  size_t rleSize = litbp.origSize;
1546
1544
  if (rleSize>maxDstSize) return ERROR(dstSize_tooSmall);
1547
1545
  if (!srcSize) return ERROR(srcSize_wrong);
1548
- memset(oend - rleSize, *ip, rleSize);
1546
+ if (rleSize > 0) {
1547
+ memset(oend - rleSize, *ip, rleSize);
1548
+ }
1549
1549
  *litStart = oend - rleSize;
1550
1550
  *litSize = rleSize;
1551
1551
  ip++;
@@ -1702,13 +1702,13 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
1702
1702
  seqState->prevOffset = seq->offset;
1703
1703
  if (litLength == MaxLL)
1704
1704
  {
1705
- U32 add = dumps<de ? *dumps++ : 0;
1705
+ const U32 add = dumps<de ? *dumps++ : 0;
1706
1706
  if (add < 255) litLength += add;
1707
1707
  else
1708
1708
  {
1709
1709
  if (dumps<=(de-3))
1710
1710
  {
1711
- litLength = ZSTD_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
1711
+ litLength = ZSTD_readLE24(dumps);
1712
1712
  dumps += 3;
1713
1713
  }
1714
1714
  }
@@ -1730,13 +1730,13 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
1730
1730
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
1731
1731
  if (matchLength == MaxML)
1732
1732
  {
1733
- U32 add = dumps<de ? *dumps++ : 0;
1733
+ const U32 add = dumps<de ? *dumps++ : 0;
1734
1734
  if (add < 255) matchLength += add;
1735
1735
  else
1736
1736
  {
1737
1737
  if (dumps<=(de-3))
1738
1738
  {
1739
- matchLength = ZSTD_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
1739
+ matchLength = ZSTD_readLE24(dumps);
1740
1740
  dumps += 3;
1741
1741
  }
1742
1742
  }
@@ -1757,7 +1757,7 @@ static size_t ZSTD_execSequence(BYTE* op,
1757
1757
  BYTE* const base, BYTE* const oend)
1758
1758
  {
1759
1759
  static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
1760
- static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* substracted */
1760
+ static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */
1761
1761
  const BYTE* const ostart = op;
1762
1762
  const size_t litLength = sequence.litLength;
1763
1763
  BYTE* const endMatch = op + litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
@@ -1905,8 +1905,10 @@ static size_t ZSTD_decompressSequences(
1905
1905
  {
1906
1906
  size_t lastLLSize = litEnd - litPtr;
1907
1907
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
1908
- if (op != litPtr) memmove(op, litPtr, lastLLSize);
1909
- op += lastLLSize;
1908
+ if (lastLLSize > 0) {
1909
+ if (op != litPtr) memmove(op, litPtr, lastLLSize);
1910
+ op += lastLLSize;
1911
+ }
1910
1912
  }
1911
1913
  }
1912
1914
 
@@ -1999,36 +2001,59 @@ size_t ZSTDv01_decompress(void* dst, size_t maxDstSize, const void* src, size_t
1999
2001
  return ZSTDv01_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
2000
2002
  }
2001
2003
 
2002
- size_t ZSTDv01_findFrameCompressedSize(const void* src, size_t srcSize)
2004
+ /* ZSTD_errorFrameSizeInfoLegacy() :
2005
+ assumes `cSize` and `dBound` are _not_ NULL */
2006
+ static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
2007
+ {
2008
+ *cSize = ret;
2009
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
2010
+ }
2011
+
2012
+ void ZSTDv01_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
2003
2013
  {
2004
2014
  const BYTE* ip = (const BYTE*)src;
2005
2015
  size_t remainingSize = srcSize;
2016
+ size_t nbBlocks = 0;
2006
2017
  U32 magicNumber;
2007
2018
  blockProperties_t blockProperties;
2008
2019
 
2009
2020
  /* Frame Header */
2010
- if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
2021
+ if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) {
2022
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
2023
+ return;
2024
+ }
2011
2025
  magicNumber = ZSTD_readBE32(src);
2012
- if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
2026
+ if (magicNumber != ZSTD_magicNumber) {
2027
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
2028
+ return;
2029
+ }
2013
2030
  ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
2014
2031
 
2015
2032
  /* Loop on each block */
2016
2033
  while (1)
2017
2034
  {
2018
2035
  size_t blockSize = ZSTDv01_getcBlockSize(ip, remainingSize, &blockProperties);
2019
- if (ZSTDv01_isError(blockSize)) return blockSize;
2036
+ if (ZSTDv01_isError(blockSize)) {
2037
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, blockSize);
2038
+ return;
2039
+ }
2020
2040
 
2021
2041
  ip += ZSTD_blockHeaderSize;
2022
2042
  remainingSize -= ZSTD_blockHeaderSize;
2023
- if (blockSize > remainingSize) return ERROR(srcSize_wrong);
2043
+ if (blockSize > remainingSize) {
2044
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
2045
+ return;
2046
+ }
2024
2047
 
2025
2048
  if (blockSize == 0) break; /* bt_end */
2026
2049
 
2027
2050
  ip += blockSize;
2028
2051
  remainingSize -= blockSize;
2052
+ nbBlocks++;
2029
2053
  }
2030
2054
 
2031
- return ip - (const BYTE*)src;
2055
+ *cSize = ip - (const BYTE*)src;
2056
+ *dBound = nbBlocks * BLOCKSIZE;
2032
2057
  }
2033
2058
 
2034
2059
  /*******************************