extzstd 0.3.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -3
  3. data/contrib/zstd/CHANGELOG +225 -1
  4. data/contrib/zstd/CONTRIBUTING.md +158 -75
  5. data/contrib/zstd/LICENSE +4 -4
  6. data/contrib/zstd/Makefile +106 -69
  7. data/contrib/zstd/Package.swift +36 -0
  8. data/contrib/zstd/README.md +64 -36
  9. data/contrib/zstd/SECURITY.md +15 -0
  10. data/contrib/zstd/TESTING.md +2 -3
  11. data/contrib/zstd/lib/BUCK +5 -7
  12. data/contrib/zstd/lib/Makefile +117 -199
  13. data/contrib/zstd/lib/README.md +37 -7
  14. data/contrib/zstd/lib/common/allocations.h +55 -0
  15. data/contrib/zstd/lib/common/bits.h +200 -0
  16. data/contrib/zstd/lib/common/bitstream.h +80 -86
  17. data/contrib/zstd/lib/common/compiler.h +225 -63
  18. data/contrib/zstd/lib/common/cpu.h +37 -1
  19. data/contrib/zstd/lib/common/debug.c +7 -1
  20. data/contrib/zstd/lib/common/debug.h +21 -12
  21. data/contrib/zstd/lib/common/entropy_common.c +15 -37
  22. data/contrib/zstd/lib/common/error_private.c +9 -2
  23. data/contrib/zstd/lib/common/error_private.h +93 -5
  24. data/contrib/zstd/lib/common/fse.h +12 -87
  25. data/contrib/zstd/lib/common/fse_decompress.c +37 -117
  26. data/contrib/zstd/lib/common/huf.h +97 -172
  27. data/contrib/zstd/lib/common/mem.h +58 -58
  28. data/contrib/zstd/lib/common/pool.c +38 -17
  29. data/contrib/zstd/lib/common/pool.h +10 -4
  30. data/contrib/zstd/lib/common/portability_macros.h +158 -0
  31. data/contrib/zstd/lib/common/threading.c +74 -14
  32. data/contrib/zstd/lib/common/threading.h +5 -10
  33. data/contrib/zstd/lib/common/xxhash.c +6 -814
  34. data/contrib/zstd/lib/common/xxhash.h +6930 -195
  35. data/contrib/zstd/lib/common/zstd_common.c +1 -36
  36. data/contrib/zstd/lib/common/zstd_deps.h +1 -1
  37. data/contrib/zstd/lib/common/zstd_internal.h +68 -154
  38. data/contrib/zstd/lib/common/zstd_trace.h +163 -0
  39. data/contrib/zstd/lib/compress/clevels.h +134 -0
  40. data/contrib/zstd/lib/compress/fse_compress.c +75 -155
  41. data/contrib/zstd/lib/compress/hist.c +1 -1
  42. data/contrib/zstd/lib/compress/hist.h +1 -1
  43. data/contrib/zstd/lib/compress/huf_compress.c +810 -259
  44. data/contrib/zstd/lib/compress/zstd_compress.c +2864 -919
  45. data/contrib/zstd/lib/compress/zstd_compress_internal.h +523 -192
  46. data/contrib/zstd/lib/compress/zstd_compress_literals.c +117 -40
  47. data/contrib/zstd/lib/compress/zstd_compress_literals.h +16 -6
  48. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +28 -19
  49. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +1 -1
  50. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +251 -412
  51. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +1 -1
  52. data/contrib/zstd/lib/compress/zstd_cwksp.h +284 -97
  53. data/contrib/zstd/lib/compress/zstd_double_fast.c +382 -133
  54. data/contrib/zstd/lib/compress/zstd_double_fast.h +14 -2
  55. data/contrib/zstd/lib/compress/zstd_fast.c +732 -260
  56. data/contrib/zstd/lib/compress/zstd_fast.h +3 -2
  57. data/contrib/zstd/lib/compress/zstd_lazy.c +1177 -390
  58. data/contrib/zstd/lib/compress/zstd_lazy.h +129 -14
  59. data/contrib/zstd/lib/compress/zstd_ldm.c +280 -210
  60. data/contrib/zstd/lib/compress/zstd_ldm.h +3 -2
  61. data/contrib/zstd/lib/compress/zstd_ldm_geartab.h +106 -0
  62. data/contrib/zstd/lib/compress/zstd_opt.c +516 -285
  63. data/contrib/zstd/lib/compress/zstd_opt.h +32 -8
  64. data/contrib/zstd/lib/compress/zstdmt_compress.c +202 -131
  65. data/contrib/zstd/lib/compress/zstdmt_compress.h +9 -6
  66. data/contrib/zstd/lib/decompress/huf_decompress.c +1149 -555
  67. data/contrib/zstd/lib/decompress/huf_decompress_amd64.S +595 -0
  68. data/contrib/zstd/lib/decompress/zstd_ddict.c +4 -4
  69. data/contrib/zstd/lib/decompress/zstd_ddict.h +1 -1
  70. data/contrib/zstd/lib/decompress/zstd_decompress.c +583 -106
  71. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1054 -379
  72. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +14 -3
  73. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +56 -6
  74. data/contrib/zstd/lib/deprecated/zbuff.h +1 -1
  75. data/contrib/zstd/lib/deprecated/zbuff_common.c +1 -1
  76. data/contrib/zstd/lib/deprecated/zbuff_compress.c +24 -4
  77. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +3 -1
  78. data/contrib/zstd/lib/dictBuilder/cover.c +60 -44
  79. data/contrib/zstd/lib/dictBuilder/cover.h +6 -11
  80. data/contrib/zstd/lib/dictBuilder/divsufsort.c +1 -1
  81. data/contrib/zstd/lib/dictBuilder/fastcover.c +26 -18
  82. data/contrib/zstd/lib/dictBuilder/zdict.c +100 -101
  83. data/contrib/zstd/lib/legacy/zstd_legacy.h +38 -1
  84. data/contrib/zstd/lib/legacy/zstd_v01.c +18 -53
  85. data/contrib/zstd/lib/legacy/zstd_v01.h +1 -1
  86. data/contrib/zstd/lib/legacy/zstd_v02.c +28 -85
  87. data/contrib/zstd/lib/legacy/zstd_v02.h +1 -1
  88. data/contrib/zstd/lib/legacy/zstd_v03.c +29 -88
  89. data/contrib/zstd/lib/legacy/zstd_v03.h +1 -1
  90. data/contrib/zstd/lib/legacy/zstd_v04.c +27 -80
  91. data/contrib/zstd/lib/legacy/zstd_v04.h +1 -1
  92. data/contrib/zstd/lib/legacy/zstd_v05.c +36 -85
  93. data/contrib/zstd/lib/legacy/zstd_v05.h +1 -1
  94. data/contrib/zstd/lib/legacy/zstd_v06.c +44 -96
  95. data/contrib/zstd/lib/legacy/zstd_v06.h +1 -1
  96. data/contrib/zstd/lib/legacy/zstd_v07.c +37 -92
  97. data/contrib/zstd/lib/legacy/zstd_v07.h +1 -1
  98. data/contrib/zstd/lib/libzstd.mk +237 -0
  99. data/contrib/zstd/lib/libzstd.pc.in +4 -3
  100. data/contrib/zstd/lib/module.modulemap +35 -0
  101. data/contrib/zstd/lib/{dictBuilder/zdict.h → zdict.h} +202 -33
  102. data/contrib/zstd/lib/zstd.h +1030 -332
  103. data/contrib/zstd/lib/{common/zstd_errors.h → zstd_errors.h} +27 -8
  104. data/ext/extconf.rb +26 -7
  105. data/ext/extzstd.c +51 -24
  106. data/ext/extzstd.h +33 -6
  107. data/ext/extzstd_stream.c +74 -31
  108. data/ext/libzstd_conf.h +0 -1
  109. data/ext/zstd_decompress_asm.S +1 -0
  110. metadata +17 -7
  111. data/contrib/zstd/appveyor.yml +0 -292
  112. data/ext/depend +0 -2
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -14,6 +14,7 @@
14
14
  ******************************************/
15
15
  #include <stddef.h> /* size_t, ptrdiff_t */
16
16
  #include "zstd_v01.h"
17
+ #include "../common/compiler.h"
17
18
  #include "../common/error_private.h"
18
19
 
19
20
 
@@ -190,28 +191,6 @@ typedef signed long long S64;
190
191
  /****************************************************************
191
192
  * Memory I/O
192
193
  *****************************************************************/
193
- /* FSE_FORCE_MEMORY_ACCESS
194
- * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
195
- * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
196
- * The below switch allow to select different access method for improved performance.
197
- * Method 0 (default) : use `memcpy()`. Safe and portable.
198
- * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
199
- * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
200
- * Method 2 : direct access. This method is portable but violate C standard.
201
- * It can generate buggy code on targets generating assembly depending on alignment.
202
- * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
203
- * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
204
- * Prefer these methods in priority order (0 > 1 > 2)
205
- */
206
- #ifndef FSE_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
207
- # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
208
- # define FSE_FORCE_MEMORY_ACCESS 2
209
- # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
210
- (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
211
- # define FSE_FORCE_MEMORY_ACCESS 1
212
- # endif
213
- #endif
214
-
215
194
 
216
195
  static unsigned FSE_32bits(void)
217
196
  {
@@ -224,24 +203,6 @@ static unsigned FSE_isLittleEndian(void)
224
203
  return one.c[0];
225
204
  }
226
205
 
227
- #if defined(FSE_FORCE_MEMORY_ACCESS) && (FSE_FORCE_MEMORY_ACCESS==2)
228
-
229
- static U16 FSE_read16(const void* memPtr) { return *(const U16*) memPtr; }
230
- static U32 FSE_read32(const void* memPtr) { return *(const U32*) memPtr; }
231
- static U64 FSE_read64(const void* memPtr) { return *(const U64*) memPtr; }
232
-
233
- #elif defined(FSE_FORCE_MEMORY_ACCESS) && (FSE_FORCE_MEMORY_ACCESS==1)
234
-
235
- /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
236
- /* currently only defined for gcc and icc */
237
- typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;
238
-
239
- static U16 FSE_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
240
- static U32 FSE_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
241
- static U64 FSE_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
242
-
243
- #else
244
-
245
206
  static U16 FSE_read16(const void* memPtr)
246
207
  {
247
208
  U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
@@ -257,8 +218,6 @@ static U64 FSE_read64(const void* memPtr)
257
218
  U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
258
219
  }
259
220
 
260
- #endif /* FSE_FORCE_MEMORY_ACCESS */
261
-
262
221
  static U16 FSE_readLE16(const void* memPtr)
263
222
  {
264
223
  if (FSE_isLittleEndian())
@@ -343,8 +302,7 @@ FORCE_INLINE unsigned FSE_highbit32 (U32 val)
343
302
  {
344
303
  # if defined(_MSC_VER) /* Visual */
345
304
  unsigned long r;
346
- _BitScanReverse ( &r, val );
347
- return (unsigned) r;
305
+ return _BitScanReverse(&r, val) ? (unsigned)r : 0;
348
306
  # elif defined(__GNUC__) && (GCC_VERSION >= 304) /* GCC Intrinsic */
349
307
  return __builtin_clz (val) ^ 31;
350
308
  # else /* Software version */
@@ -1194,7 +1152,7 @@ static size_t HUF_decompress (void* dst, size_t maxDstSize, const void* cSrc, si
1194
1152
  zstd - standard compression library
1195
1153
  Copyright (C) 2014-2015, Yann Collet.
1196
1154
 
1197
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1155
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1198
1156
 
1199
1157
  Redistribution and use in source and binary forms, with or without
1200
1158
  modification, are permitted provided that the following conditions are
@@ -1763,20 +1721,26 @@ static size_t ZSTD_execSequence(BYTE* op,
1763
1721
  static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
1764
1722
  static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */
1765
1723
  const BYTE* const ostart = op;
1724
+ BYTE* const oLitEnd = op + sequence.litLength;
1766
1725
  const size_t litLength = sequence.litLength;
1767
1726
  BYTE* const endMatch = op + litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
1768
1727
  const BYTE* const litEnd = *litPtr + litLength;
1769
1728
 
1770
- /* check */
1729
+ /* checks */
1730
+ size_t const seqLength = sequence.litLength + sequence.matchLength;
1731
+
1732
+ if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
1733
+ if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected);
1734
+ /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */
1735
+ if (sequence.offset > (U32)(oLitEnd - base)) return ERROR(corruption_detected);
1736
+
1771
1737
  if (endMatch > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
1772
- if (litEnd > litLimit) return ERROR(corruption_detected);
1773
- if (sequence.matchLength > (size_t)(*litPtr-op)) return ERROR(dstSize_tooSmall); /* overwrite literal segment */
1738
+ if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */
1739
+ if (sequence.matchLength > (size_t)(*litPtr-op)) return ERROR(dstSize_tooSmall); /* overwrite literal segment */
1774
1740
 
1775
1741
  /* copy Literals */
1776
- if (((size_t)(*litPtr - op) < 8) || ((size_t)(oend-litEnd) < 8) || (op+litLength > oend-8))
1777
- memmove(op, *litPtr, litLength); /* overwrite risk */
1778
- else
1779
- ZSTD_wildcopy(op, *litPtr, litLength);
1742
+ ZSTD_memmove(op, *litPtr, sequence.litLength); /* note : v0.1 seems to allow scenarios where output or input are close to end of buffer */
1743
+
1780
1744
  op += litLength;
1781
1745
  *litPtr = litEnd; /* update for next sequence */
1782
1746
 
@@ -2155,6 +2119,7 @@ size_t ZSTDv01_decompressContinue(ZSTDv01_Dctx* dctx, void* dst, size_t maxDstSi
2155
2119
  }
2156
2120
  ctx->phase = 1;
2157
2121
  ctx->expected = ZSTD_blockHeaderSize;
2122
+ if (ZSTDv01_isError(rSize)) return rSize;
2158
2123
  ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
2159
2124
  return rSize;
2160
2125
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -11,6 +11,7 @@
11
11
 
12
12
  #include <stddef.h> /* size_t, ptrdiff_t */
13
13
  #include "zstd_v02.h"
14
+ #include "../common/compiler.h"
14
15
  #include "../common/error_private.h"
15
16
 
16
17
 
@@ -28,7 +29,7 @@
28
29
  low-level memory access routines
29
30
  Copyright (C) 2013-2015, Yann Collet.
30
31
 
31
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
32
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
32
33
 
33
34
  Redistribution and use in source and binary forms, with or without
34
35
  modification, are permitted provided that the following conditions are
@@ -71,20 +72,6 @@ extern "C" {
71
72
  #include <string.h> /* memcpy */
72
73
 
73
74
 
74
- /******************************************
75
- * Compiler-specific
76
- ******************************************/
77
- #if defined(__GNUC__)
78
- # define MEM_STATIC static __attribute__((unused))
79
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
80
- # define MEM_STATIC static inline
81
- #elif defined(_MSC_VER)
82
- # define MEM_STATIC static __inline
83
- #else
84
- # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
85
- #endif
86
-
87
-
88
75
  /****************************************************************
89
76
  * Basic Types
90
77
  *****************************************************************/
@@ -115,27 +102,6 @@ extern "C" {
115
102
  /****************************************************************
116
103
  * Memory I/O
117
104
  *****************************************************************/
118
- /* MEM_FORCE_MEMORY_ACCESS
119
- * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
120
- * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
121
- * The below switch allow to select different access method for improved performance.
122
- * Method 0 (default) : use `memcpy()`. Safe and portable.
123
- * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
124
- * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
125
- * Method 2 : direct access. This method is portable but violate C standard.
126
- * It can generate buggy code on targets generating assembly depending on alignment.
127
- * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
128
- * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
129
- * Prefer these methods in priority order (0 > 1 > 2)
130
- */
131
- #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
132
- # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
133
- # define MEM_FORCE_MEMORY_ACCESS 2
134
- # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
135
- (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
136
- # define MEM_FORCE_MEMORY_ACCESS 1
137
- # endif
138
- #endif
139
105
 
140
106
  MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
141
107
  MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
@@ -146,33 +112,6 @@ MEM_STATIC unsigned MEM_isLittleEndian(void)
146
112
  return one.c[0];
147
113
  }
148
114
 
149
- #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
150
-
151
- /* violates C standard on structure alignment.
152
- Only use if no other choice to achieve best performance on target platform */
153
- MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
154
- MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
155
- MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
156
-
157
- MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
158
-
159
- #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
160
-
161
- /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
162
- /* currently only defined for gcc and icc */
163
- typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;
164
-
165
- MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
166
- MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
167
- MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
168
-
169
- MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
170
-
171
- #else
172
-
173
- /* default method, safe and standard.
174
- can sometimes prove slower */
175
-
176
115
  MEM_STATIC U16 MEM_read16(const void* memPtr)
177
116
  {
178
117
  U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
@@ -193,9 +132,6 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
193
132
  memcpy(memPtr, &value, sizeof(value));
194
133
  }
195
134
 
196
- #endif /* MEM_FORCE_MEMORY_ACCESS */
197
-
198
-
199
135
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
200
136
  {
201
137
  if (MEM_isLittleEndian())
@@ -272,7 +208,7 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
272
208
  header file (to include)
273
209
  Copyright (C) 2013-2015, Yann Collet.
274
210
 
275
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
211
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
276
212
 
277
213
  Redistribution and use in source and binary forms, with or without
278
214
  modification, are permitted provided that the following conditions are
@@ -353,9 +289,8 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
353
289
  MEM_STATIC unsigned BIT_highbit32 (U32 val)
354
290
  {
355
291
  # if defined(_MSC_VER) /* Visual */
356
- unsigned long r=0;
357
- _BitScanReverse ( &r, val );
358
- return (unsigned) r;
292
+ unsigned long r;
293
+ return _BitScanReverse(&r, val) ? (unsigned)r : 0;
359
294
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
360
295
  return __builtin_clz (val) ^ 31;
361
296
  # else /* Software version */
@@ -437,7 +372,7 @@ MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
437
372
  }
438
373
 
439
374
  /*! BIT_lookBitsFast :
440
- * unsafe version; only works only if nbBits >= 1 */
375
+ * unsafe version; only works if nbBits >= 1 */
441
376
  MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
442
377
  {
443
378
  const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
@@ -457,7 +392,7 @@ MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
457
392
  }
458
393
 
459
394
  /*!BIT_readBitsFast :
460
- * unsafe version; only works only if nbBits >= 1 */
395
+ * unsafe version; only works if nbBits >= 1 */
461
396
  MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
462
397
  {
463
398
  size_t value = BIT_lookBitsFast(bitD, nbBits);
@@ -514,7 +449,7 @@ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
514
449
  Error codes and messages
515
450
  Copyright (C) 2013-2015, Yann Collet
516
451
 
517
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
452
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
518
453
 
519
454
  Redistribution and use in source and binary forms, with or without
520
455
  modification, are permitted provided that the following conditions are
@@ -613,7 +548,7 @@ typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be mor
613
548
  header file for static linking (only)
614
549
  Copyright (C) 2013-2015, Yann Collet
615
550
 
616
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
551
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
617
552
 
618
553
  Redistribution and use in source and binary forms, with or without
619
554
  modification, are permitted provided that the following conditions are
@@ -757,7 +692,7 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
757
692
  header file for static linking (only)
758
693
  Copyright (C) 2013-2015, Yann Collet
759
694
 
760
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
695
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
761
696
 
762
697
  Redistribution and use in source and binary forms, with or without
763
698
  modification, are permitted provided that the following conditions are
@@ -826,7 +761,7 @@ static size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, si
826
761
  Header File
827
762
  Copyright (C) 2014-2015, Yann Collet.
828
763
 
829
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
764
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
830
765
 
831
766
  Redistribution and use in source and binary forms, with or without
832
767
  modification, are permitted provided that the following conditions are
@@ -886,7 +821,7 @@ typedef struct ZSTD_CCtx_s ZSTD_CCtx; /* incomplete type */
886
821
  Header File for static linking only
887
822
  Copyright (C) 2014-2015, Yann Collet.
888
823
 
889
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
824
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
890
825
 
891
826
  Redistribution and use in source and binary forms, with or without
892
827
  modification, are permitted provided that the following conditions are
@@ -927,7 +862,7 @@ extern "C" {
927
862
  * Streaming functions
928
863
  ***************************************/
929
864
 
930
- typedef struct ZSTD_DCtx_s ZSTD_DCtx;
865
+ typedef struct ZSTDv02_Dctx_s ZSTD_DCtx;
931
866
 
932
867
  /*
933
868
  Use above functions alternatively.
@@ -950,7 +885,7 @@ typedef struct ZSTD_DCtx_s ZSTD_DCtx;
950
885
  FSE : Finite State Entropy coder
951
886
  Copyright (C) 2013-2015, Yann Collet.
952
887
 
953
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
888
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
954
889
 
955
890
  Redistribution and use in source and binary forms, with or without
956
891
  modification, are permitted provided that the following conditions are
@@ -1454,7 +1389,7 @@ static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, siz
1454
1389
  Huff0 : Huffman coder, part of New Generation Entropy library
1455
1390
  Copyright (C) 2013-2015, Yann Collet.
1456
1391
 
1457
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1392
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1458
1393
 
1459
1394
  Redistribution and use in source and binary forms, with or without
1460
1395
  modification, are permitted provided that the following conditions are
@@ -2613,7 +2548,7 @@ static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_
2613
2548
  zstd - standard compression library
2614
2549
  Copyright (C) 2014-2015, Yann Collet.
2615
2550
 
2616
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
2551
+ BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2617
2552
 
2618
2553
  Redistribution and use in source and binary forms, with or without
2619
2554
  modification, are permitted provided that the following conditions are
@@ -2802,7 +2737,7 @@ static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
2802
2737
  /* *************************************************************
2803
2738
  * Decompression section
2804
2739
  ***************************************************************/
2805
- struct ZSTD_DCtx_s
2740
+ struct ZSTDv02_Dctx_s
2806
2741
  {
2807
2742
  U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
2808
2743
  U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
@@ -3118,12 +3053,19 @@ static size_t ZSTD_execSequence(BYTE* op,
3118
3053
  const BYTE* const litEnd = *litPtr + sequence.litLength;
3119
3054
 
3120
3055
  /* checks */
3121
- if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
3056
+ size_t const seqLength = sequence.litLength + sequence.matchLength;
3057
+
3058
+ if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
3059
+ if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected);
3060
+ /* Now we know there are no overflow in literal nor match lengths, can use the pointer check */
3061
+ if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall);
3062
+ if (sequence.offset > (U32)(oLitEnd - base)) return ERROR(corruption_detected);
3063
+
3122
3064
  if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3123
3065
  if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */
3124
3066
 
3125
3067
  /* copy Literals */
3126
- ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
3068
+ ZSTD_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
3127
3069
  op = oLitEnd;
3128
3070
  *litPtr = litEnd; /* update for next sequence */
3129
3071
 
@@ -3476,6 +3418,7 @@ static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSi
3476
3418
  }
3477
3419
  ctx->phase = 1;
3478
3420
  ctx->expected = ZSTD_blockHeaderSize;
3421
+ if (ZSTD_isError(rSize)) return rSize;
3479
3422
  ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
3480
3423
  return rSize;
3481
3424
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the