extzstd 0.0.3.CONCEPT → 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.ja +5 -0
  3. data/LICENSE +6 -6
  4. data/README.md +35 -22
  5. data/contrib/zstd/LICENSE +13 -9
  6. data/contrib/zstd/README.md +37 -44
  7. data/contrib/zstd/common/entropy_common.c +33 -39
  8. data/contrib/zstd/common/error_private.c +43 -0
  9. data/contrib/zstd/common/error_private.h +11 -60
  10. data/contrib/zstd/common/fse.h +11 -5
  11. data/contrib/zstd/common/fse_decompress.c +14 -16
  12. data/contrib/zstd/common/huf.h +1 -1
  13. data/contrib/zstd/common/mem.h +36 -43
  14. data/contrib/zstd/common/xxhash.c +31 -18
  15. data/contrib/zstd/common/xxhash.h +71 -35
  16. data/contrib/zstd/common/zbuff.h +29 -35
  17. data/contrib/zstd/common/zstd_common.c +24 -32
  18. data/contrib/zstd/common/zstd_errors.h +60 -0
  19. data/contrib/zstd/common/zstd_internal.h +109 -80
  20. data/contrib/zstd/compress/fse_compress.c +9 -6
  21. data/contrib/zstd/compress/huf_compress.c +30 -74
  22. data/contrib/zstd/compress/zbuff_compress.c +43 -51
  23. data/contrib/zstd/compress/zstd_compress.c +953 -763
  24. data/contrib/zstd/compress/zstd_opt.h +115 -261
  25. data/contrib/zstd/decompress/huf_decompress.c +29 -40
  26. data/contrib/zstd/decompress/zbuff_decompress.c +36 -78
  27. data/contrib/zstd/decompress/zstd_decompress.c +976 -496
  28. data/contrib/zstd/dictBuilder/divsufsort.h +5 -5
  29. data/contrib/zstd/dictBuilder/zdict.c +194 -229
  30. data/contrib/zstd/dictBuilder/zdict.h +66 -68
  31. data/contrib/zstd/legacy/zstd_legacy.h +168 -49
  32. data/contrib/zstd/legacy/zstd_v01.c +95 -178
  33. data/contrib/zstd/legacy/zstd_v01.h +12 -32
  34. data/contrib/zstd/legacy/zstd_v02.c +48 -274
  35. data/contrib/zstd/legacy/zstd_v02.h +12 -32
  36. data/contrib/zstd/legacy/zstd_v03.c +48 -274
  37. data/contrib/zstd/legacy/zstd_v03.h +12 -32
  38. data/contrib/zstd/legacy/zstd_v04.c +63 -320
  39. data/contrib/zstd/legacy/zstd_v04.h +13 -33
  40. data/contrib/zstd/legacy/zstd_v05.c +80 -345
  41. data/contrib/zstd/legacy/zstd_v05.h +9 -31
  42. data/contrib/zstd/legacy/zstd_v06.c +48 -458
  43. data/contrib/zstd/legacy/zstd_v06.h +41 -67
  44. data/contrib/zstd/legacy/zstd_v07.c +4544 -0
  45. data/contrib/zstd/legacy/zstd_v07.h +173 -0
  46. data/contrib/zstd/zstd.h +640 -0
  47. data/ext/extconf.rb +7 -3
  48. data/ext/extzstd.c +263 -106
  49. data/ext/extzstd.h +8 -6
  50. data/ext/extzstd_nogvls.h +0 -117
  51. data/ext/extzstd_stream.c +347 -0
  52. data/ext/zstd_common.c +8 -0
  53. data/ext/zstd_compress.c +6 -0
  54. data/ext/zstd_decompress.c +5 -0
  55. data/ext/zstd_dictbuilder.c +5 -0
  56. data/ext/zstd_legacy_v07.c +1 -0
  57. data/gemstub.rb +18 -16
  58. data/lib/extzstd/version.rb +1 -1
  59. data/lib/extzstd.rb +77 -43
  60. data/test/test_basic.rb +11 -6
  61. metadata +23 -10
  62. data/contrib/zstd/common/error_public.h +0 -77
  63. data/contrib/zstd/common/zstd.h +0 -475
  64. data/ext/extzstd_buffered.c +0 -265
  65. data/ext/zstd_amalgam.c +0 -18
@@ -1,39 +1,16 @@
1
- /* ******************************************************************
2
- zstd_v05.c
3
- Decompression module for ZSTD v0.5 legacy format
4
- Copyright (C) 2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ /**
2
+ * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
30
9
 
31
- You can contact the author at :
32
- - Homepage : http://www.zstd.net/
33
- ****************************************************************** */
34
10
 
35
11
  /*- Dependencies -*/
36
12
  #include "zstd_v05.h"
13
+ #include "error_private.h"
37
14
 
38
15
 
39
16
  /* ******************************************************************
@@ -140,7 +117,7 @@ extern "C" {
140
117
  #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
141
118
  # 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__) )
142
119
  # define MEM_FORCE_MEMORY_ACCESS 2
143
- # elif defined(__INTEL_COMPILER) || \
120
+ # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
144
121
  (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
145
122
  # define MEM_FORCE_MEMORY_ACCESS 1
146
123
  # endif
@@ -250,18 +227,6 @@ MEM_STATIC U32 MEM_readLE32(const void* memPtr)
250
227
  }
251
228
  }
252
229
 
253
- MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
254
- {
255
- if (MEM_isLittleEndian()) {
256
- MEM_write32(memPtr, val32);
257
- } else {
258
- BYTE* p = (BYTE*)memPtr;
259
- p[0] = (BYTE)val32;
260
- p[1] = (BYTE)(val32>>8);
261
- p[2] = (BYTE)(val32>>16);
262
- p[3] = (BYTE)(val32>>24);
263
- }
264
- }
265
230
 
266
231
  MEM_STATIC U64 MEM_readLE64(const void* memPtr)
267
232
  {
@@ -274,22 +239,6 @@ MEM_STATIC U64 MEM_readLE64(const void* memPtr)
274
239
  }
275
240
  }
276
241
 
277
- MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
278
- {
279
- if (MEM_isLittleEndian()) {
280
- MEM_write64(memPtr, val64);
281
- } else {
282
- BYTE* p = (BYTE*)memPtr;
283
- p[0] = (BYTE)val64;
284
- p[1] = (BYTE)(val64>>8);
285
- p[2] = (BYTE)(val64>>16);
286
- p[3] = (BYTE)(val64>>24);
287
- p[4] = (BYTE)(val64>>32);
288
- p[5] = (BYTE)(val64>>40);
289
- p[6] = (BYTE)(val64>>48);
290
- p[7] = (BYTE)(val64>>56);
291
- }
292
- }
293
242
 
294
243
  MEM_STATIC size_t MEM_readLEST(const void* memPtr)
295
244
  {
@@ -299,13 +248,6 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
299
248
  return (size_t)MEM_readLE64(memPtr);
300
249
  }
301
250
 
302
- MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
303
- {
304
- if (MEM_32bits())
305
- MEM_writeLE32(memPtr, (U32)val);
306
- else
307
- MEM_writeLE64(memPtr, (U64)val);
308
- }
309
251
 
310
252
  #if defined (__cplusplus)
311
253
  }
@@ -313,79 +255,6 @@ MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
313
255
 
314
256
  #endif /* MEM_H_MODULE */
315
257
 
316
- /* ******************************************************************
317
- Error codes list
318
- Copyright (C) 2016, Yann Collet
319
-
320
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
321
-
322
- Redistribution and use in source and binary forms, with or without
323
- modification, are permitted provided that the following conditions are
324
- met:
325
-
326
- * Redistributions of source code must retain the above copyright
327
- notice, this list of conditions and the following disclaimer.
328
- * Redistributions in binary form must reproduce the above
329
- copyright notice, this list of conditions and the following disclaimer
330
- in the documentation and/or other materials provided with the
331
- distribution.
332
-
333
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
334
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
335
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
336
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
337
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
338
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
339
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
340
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
341
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
342
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
343
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
344
-
345
- You can contact the author at :
346
- - Source repository : https://github.com/Cyan4973/zstd
347
- ****************************************************************** */
348
- #ifndef ERROR_PUBLIC_H_MODULE
349
- #define ERROR_PUBLIC_H_MODULE
350
-
351
- #if defined (__cplusplus)
352
- extern "C" {
353
- #endif
354
-
355
-
356
- /* ****************************************
357
- * error codes list
358
- ******************************************/
359
- typedef enum {
360
- ZSTDv05_error_no_error,
361
- ZSTDv05_error_GENERIC,
362
- ZSTDv05_error_prefix_unknown,
363
- ZSTDv05_error_frameParameter_unsupported,
364
- ZSTDv05_error_frameParameter_unsupportedBy32bits,
365
- ZSTDv05_error_init_missing,
366
- ZSTDv05_error_memory_allocation,
367
- ZSTDv05_error_stage_wrong,
368
- ZSTDv05_error_dstSize_tooSmall,
369
- ZSTDv05_error_srcSize_wrong,
370
- ZSTDv05_error_corruption_detected,
371
- ZSTDv05_error_tableLog_tooLarge,
372
- ZSTDv05_error_maxSymbolValue_tooLarge,
373
- ZSTDv05_error_maxSymbolValue_tooSmall,
374
- ZSTDv05_error_dictionary_corrupted,
375
- ZSTDv05_error_maxCode
376
- } ZSTDv05_ErrorCode;
377
-
378
- /* note : functions provide error codes in reverse negative order,
379
- so compare with (size_t)(0-enum) */
380
-
381
-
382
- #if defined (__cplusplus)
383
- }
384
- #endif
385
-
386
- #endif /* ERROR_PUBLIC_H_MODULE */
387
-
388
-
389
258
  /*
390
259
  zstd - standard compression library
391
260
  Header File for static linking only
@@ -529,119 +398,6 @@ size_t ZSTDv05_decompressBlock(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity
529
398
  #endif /* ZSTDv05_STATIC_H */
530
399
 
531
400
 
532
-
533
- /* ******************************************************************
534
- Error codes and messages
535
- Copyright (C) 2013-2016, Yann Collet
536
-
537
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
538
-
539
- Redistribution and use in source and binary forms, with or without
540
- modification, are permitted provided that the following conditions are
541
- met:
542
-
543
- * Redistributions of source code must retain the above copyright
544
- notice, this list of conditions and the following disclaimer.
545
- * Redistributions in binary form must reproduce the above
546
- copyright notice, this list of conditions and the following disclaimer
547
- in the documentation and/or other materials provided with the
548
- distribution.
549
-
550
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
551
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
552
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
553
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
554
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
555
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
556
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
557
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
558
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
559
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
560
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
561
-
562
- You can contact the author at :
563
- - Source repository : https://github.com/Cyan4973/zstd
564
- ****************************************************************** */
565
- /* Note : this module is expected to remain private, do not expose it */
566
-
567
- #ifndef ERROR_H_MODULE
568
- #define ERROR_H_MODULE
569
-
570
- #if defined (__cplusplus)
571
- extern "C" {
572
- #endif
573
-
574
-
575
-
576
- /* ****************************************
577
- * Compiler-specific
578
- ******************************************/
579
- #if defined(__GNUC__)
580
- # define ERR_STATIC static __attribute__((unused))
581
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
582
- # define ERR_STATIC static inline
583
- #elif defined(_MSC_VER)
584
- # define ERR_STATIC static __inline
585
- #else
586
- # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
587
- #endif
588
-
589
-
590
- /*-****************************************
591
- * Customization
592
- ******************************************/
593
- typedef ZSTDv05_ErrorCode ERR_enum;
594
- #define PREFIX(name) ZSTDv05_error_##name
595
-
596
-
597
- /*-****************************************
598
- * Error codes handling
599
- ******************************************/
600
- #ifdef ERROR
601
- # undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
602
- #endif
603
- #define ERROR(name) (size_t)-PREFIX(name)
604
-
605
- ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
606
-
607
- ERR_STATIC ERR_enum ERR_getError(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
608
-
609
-
610
- /*-****************************************
611
- * Error Strings
612
- ******************************************/
613
-
614
- ERR_STATIC const char* ERR_getErrorName(size_t code)
615
- {
616
- static const char* notErrorCode = "Unspecified error code";
617
- switch( ERR_getError(code) )
618
- {
619
- case PREFIX(no_error): return "No error detected";
620
- case PREFIX(GENERIC): return "Error (generic)";
621
- case PREFIX(prefix_unknown): return "Unknown frame descriptor";
622
- case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
623
- case PREFIX(frameParameter_unsupportedBy32bits): return "Frame parameter unsupported in 32-bits mode";
624
- case PREFIX(init_missing): return "Context should be init first";
625
- case PREFIX(memory_allocation): return "Allocation error : not enough memory";
626
- case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
627
- case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
628
- case PREFIX(srcSize_wrong): return "Src size incorrect";
629
- case PREFIX(corruption_detected): return "Corrupted block detected";
630
- case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory";
631
- case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max possible Symbol Value : too large";
632
- case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
633
- case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
634
- case PREFIX(maxCode):
635
- default: return notErrorCode; /* should be impossible, due to ERR_getError() */
636
- }
637
- }
638
-
639
-
640
- #if defined (__cplusplus)
641
- }
642
- #endif
643
-
644
- #endif /* ERROR_H_MODULE */
645
401
  /*
646
402
  zstd_internal - common functions to include
647
403
  Header File for include
@@ -763,28 +519,6 @@ MEM_STATIC void ZSTDv05_wildcopy(void* dst, const void* src, size_t length)
763
519
  while (op < oend);
764
520
  }
765
521
 
766
- MEM_STATIC unsigned ZSTDv05_highbit(U32 val)
767
- {
768
- # if defined(_MSC_VER) /* Visual */
769
- unsigned long r=0;
770
- _BitScanReverse(&r, val);
771
- return (unsigned)r;
772
- # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
773
- return 31 - __builtin_clz(val);
774
- # else /* Software version */
775
- static const int 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 };
776
- U32 v = val;
777
- int r;
778
- v |= v >> 1;
779
- v |= v >> 2;
780
- v |= v >> 4;
781
- v |= v >> 8;
782
- v |= v >> 16;
783
- r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
784
- return r;
785
- # endif
786
- }
787
-
788
522
 
789
523
  /*-*******************************************
790
524
  * Private interfaces
@@ -916,16 +650,16 @@ void FSEv05_freeDTable(FSEv05_DTable* dt);
916
650
  /*!
917
651
  FSEv05_buildDTable():
918
652
  Builds 'dt', which must be already allocated, using FSEv05_createDTable()
919
- return : 0,
920
- or an errorCode, which can be tested using FSEv05_isError() */
653
+ @return : 0,
654
+ or an errorCode, which can be tested using FSEv05_isError() */
921
655
  size_t FSEv05_buildDTable (FSEv05_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
922
656
 
923
657
  /*!
924
658
  FSEv05_decompress_usingDTable():
925
- Decompress compressed source @cSrc of size @cSrcSize using @dt
926
- into @dst which must be already allocated.
927
- return : size of regenerated data (necessarily <= @dstCapacity)
928
- or an errorCode, which can be tested using FSEv05_isError() */
659
+ Decompress compressed source @cSrc of size @cSrcSize using `dt`
660
+ into `dst` which must be already allocated.
661
+ @return : size of regenerated data (necessarily <= @dstCapacity)
662
+ or an errorCode, which can be tested using FSEv05_isError() */
929
663
  size_t FSEv05_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSEv05_DTable* dt);
930
664
 
931
665
 
@@ -1350,11 +1084,6 @@ MEM_STATIC void FSEv05_initDState(FSEv05_DState_t* DStatePtr, BITv05_DStream_t*
1350
1084
  DStatePtr->table = dt + 1;
1351
1085
  }
1352
1086
 
1353
- MEM_STATIC size_t FSEv05_getStateValue(FSEv05_DState_t* DStatePtr)
1354
- {
1355
- return DStatePtr->state;
1356
- }
1357
-
1358
1087
  MEM_STATIC BYTE FSEv05_peakSymbol(FSEv05_DState_t* DStatePtr)
1359
1088
  {
1360
1089
  const FSEv05_decode_t DInfo = ((const FSEv05_decode_t*)(DStatePtr->table))[DStatePtr->state];
@@ -1466,12 +1195,15 @@ MEM_STATIC unsigned FSEv05_endOfDState(const FSEv05_DState_t* DStatePtr)
1466
1195
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1467
1196
  # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1468
1197
  #else
1469
- # ifdef __GNUC__
1470
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
1471
- # define FORCE_INLINE static inline __attribute__((always_inline))
1198
+ # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1199
+ # ifdef __GNUC__
1200
+ # define FORCE_INLINE static inline __attribute__((always_inline))
1201
+ # else
1202
+ # define FORCE_INLINE static inline
1203
+ # endif
1472
1204
  # else
1473
- # define FORCE_INLINE static inline
1474
- # endif
1205
+ # define FORCE_INLINE static
1206
+ # endif /* __STDC_VERSION__ */
1475
1207
  #endif
1476
1208
 
1477
1209
 
@@ -2088,14 +1820,7 @@ size_t HUFv05_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void
2088
1820
 
2089
1821
 
2090
1822
  #ifdef _MSC_VER /* Visual Studio */
2091
- # define FORCE_INLINE static __forceinline
2092
1823
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2093
- #else
2094
- # ifdef __GNUC__
2095
- # define FORCE_INLINE static inline __attribute__((always_inline))
2096
- # else
2097
- # define FORCE_INLINE static inline
2098
- # endif
2099
1824
  #endif
2100
1825
 
2101
1826
 
@@ -2148,10 +1873,12 @@ static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
2148
1873
  U32 weightTotal;
2149
1874
  U32 tableLog;
2150
1875
  const BYTE* ip = (const BYTE*) src;
2151
- size_t iSize = ip[0];
1876
+ size_t iSize;
2152
1877
  size_t oSize;
2153
1878
  U32 n;
2154
1879
 
1880
+ if (!srcSize) return ERROR(srcSize_wrong);
1881
+ iSize = ip[0];
2155
1882
  //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
2156
1883
 
2157
1884
  if (iSize >= 128) { /* special header */
@@ -2185,6 +1912,7 @@ static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
2185
1912
  rankStats[huffWeight[n]]++;
2186
1913
  weightTotal += (1 << huffWeight[n]) >> 1;
2187
1914
  }
1915
+ if (weightTotal == 0) return ERROR(corruption_detected);
2188
1916
 
2189
1917
  /* get last non-null symbol weight (implied, total must be 2^n) */
2190
1918
  tableLog = BITv05_highbit32(weightTotal) + 1;
@@ -2307,13 +2035,14 @@ size_t HUFv05_decompress1X2_usingDTable(
2307
2035
  {
2308
2036
  BYTE* op = (BYTE*)dst;
2309
2037
  BYTE* const oend = op + dstSize;
2310
- size_t errorCode;
2311
2038
  const U32 dtLog = DTable[0];
2312
2039
  const void* dtPtr = DTable;
2313
2040
  const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr)+1;
2314
2041
  BITv05_DStream_t bitD;
2315
- errorCode = BITv05_initDStream(&bitD, cSrc, cSrcSize);
2316
- if (HUFv05_isError(errorCode)) return errorCode;
2042
+
2043
+ if (dstSize <= cSrcSize) return ERROR(dstSize_tooSmall);
2044
+ { size_t const errorCode = BITv05_initDStream(&bitD, cSrc, cSrcSize);
2045
+ if (HUFv05_isError(errorCode)) return errorCode; }
2317
2046
 
2318
2047
  HUFv05_decodeStreamX2(op, &bitD, oend, dt, dtLog);
2319
2048
 
@@ -2944,17 +2673,9 @@ size_t HUFv05_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
2944
2673
  * Compiler specifics
2945
2674
  *********************************************************/
2946
2675
  #ifdef _MSC_VER /* Visual Studio */
2947
- # define FORCE_INLINE static __forceinline
2948
2676
  # include <intrin.h> /* For Visual 2005 */
2949
2677
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2950
2678
  # pragma warning(disable : 4324) /* disable: C4324: padded structure */
2951
- #else
2952
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2953
- # ifdef __GNUC__
2954
- # define FORCE_INLINE static inline __attribute__((always_inline))
2955
- # else
2956
- # define FORCE_INLINE static inline
2957
- # endif
2958
2679
  #endif
2959
2680
 
2960
2681
 
@@ -2981,9 +2702,6 @@ static void ZSTDv05_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2981
2702
  * tells if a return value is an error code */
2982
2703
  unsigned ZSTDv05_isError(size_t code) { return ERR_isError(code); }
2983
2704
 
2984
- /*! ZSTDv05_getError() :
2985
- * convert a `size_t` function result into a proper ZSTDv05_errorCode enum */
2986
- ZSTDv05_ErrorCode ZSTDv05_getError(size_t code) { return ERR_getError(code); }
2987
2705
 
2988
2706
  /*! ZSTDv05_getErrorName() :
2989
2707
  * provides error code string (useful for debugging) */
@@ -3228,6 +2946,7 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3228
2946
  {
3229
2947
  size_t litSize, litCSize, singleStream=0;
3230
2948
  U32 lhSize = ((istart[0]) >> 4) & 3;
2949
+ if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */
3231
2950
  switch(lhSize)
3232
2951
  {
3233
2952
  case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
@@ -3251,6 +2970,7 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3251
2970
  break;
3252
2971
  }
3253
2972
  if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2973
+ if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
3254
2974
 
3255
2975
  if (HUFv05_isError(singleStream ?
3256
2976
  HUFv05_decompress1X2(dctx->litBuffer, litSize, istart+lhSize, litCSize) :
@@ -3276,6 +2996,7 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3276
2996
  lhSize=3;
3277
2997
  litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
3278
2998
  litCSize = ((istart[1] & 3) << 8) + istart[2];
2999
+ if (litCSize + litSize > srcSize) return ERROR(corruption_detected);
3279
3000
 
3280
3001
  errorCode = HUFv05_decompress1X4_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->hufTableX4);
3281
3002
  if (HUFv05_isError(errorCode)) return ERROR(corruption_detected);
@@ -3332,6 +3053,7 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3332
3053
  break;
3333
3054
  case 3:
3334
3055
  litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
3056
+ if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
3335
3057
  break;
3336
3058
  }
3337
3059
  if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
@@ -3349,7 +3071,7 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3349
3071
 
3350
3072
  size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
3351
3073
  FSEv05_DTable* DTableLL, FSEv05_DTable* DTableML, FSEv05_DTable* DTableOffb,
3352
- const void* src, size_t srcSize)
3074
+ const void* src, size_t srcSize, U32 flagStaticTable)
3353
3075
  {
3354
3076
  const BYTE* const istart = (const BYTE* const)src;
3355
3077
  const BYTE* ip = istart;
@@ -3365,17 +3087,22 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3365
3087
  /* SeqHead */
3366
3088
  *nbSeq = *ip++;
3367
3089
  if (*nbSeq==0) return 1;
3368
- if (*nbSeq >= 128)
3090
+ if (*nbSeq >= 128) {
3091
+ if (ip >= iend) return ERROR(srcSize_wrong);
3369
3092
  *nbSeq = ((nbSeq[0]-128)<<8) + *ip++;
3093
+ }
3370
3094
 
3095
+ if (ip >= iend) return ERROR(srcSize_wrong);
3371
3096
  LLtype = *ip >> 6;
3372
3097
  Offtype = (*ip >> 4) & 3;
3373
3098
  MLtype = (*ip >> 2) & 3;
3374
3099
  if (*ip & 2) {
3100
+ if (ip+3 > iend) return ERROR(srcSize_wrong);
3375
3101
  dumpsLength = ip[2];
3376
3102
  dumpsLength += ip[1] << 8;
3377
3103
  ip += 3;
3378
3104
  } else {
3105
+ if (ip+2 > iend) return ERROR(srcSize_wrong);
3379
3106
  dumpsLength = ip[1];
3380
3107
  dumpsLength += (ip[0] & 1) << 8;
3381
3108
  ip += 2;
@@ -3395,7 +3122,6 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3395
3122
  /* Build DTables */
3396
3123
  switch(LLtype)
3397
3124
  {
3398
- U32 max;
3399
3125
  case FSEv05_ENCODING_RLE :
3400
3126
  LLlog = 0;
3401
3127
  FSEv05_buildDTable_rle(DTableLL, *ip++);
@@ -3405,20 +3131,20 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3405
3131
  FSEv05_buildDTable_raw(DTableLL, LLbits);
3406
3132
  break;
3407
3133
  case FSEv05_ENCODING_STATIC:
3134
+ if (!flagStaticTable) return ERROR(corruption_detected);
3408
3135
  break;
3409
3136
  case FSEv05_ENCODING_DYNAMIC :
3410
3137
  default : /* impossible */
3411
- max = MaxLL;
3412
- headerSize = FSEv05_readNCount(norm, &max, &LLlog, ip, iend-ip);
3413
- if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3414
- if (LLlog > LLFSEv05Log) return ERROR(corruption_detected);
3415
- ip += headerSize;
3416
- FSEv05_buildDTable(DTableLL, norm, max, LLlog);
3417
- }
3138
+ { U32 max = MaxLL;
3139
+ headerSize = FSEv05_readNCount(norm, &max, &LLlog, ip, iend-ip);
3140
+ if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3141
+ if (LLlog > LLFSEv05Log) return ERROR(corruption_detected);
3142
+ ip += headerSize;
3143
+ FSEv05_buildDTable(DTableLL, norm, max, LLlog);
3144
+ } }
3418
3145
 
3419
3146
  switch(Offtype)
3420
3147
  {
3421
- U32 max;
3422
3148
  case FSEv05_ENCODING_RLE :
3423
3149
  Offlog = 0;
3424
3150
  if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
@@ -3429,20 +3155,20 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3429
3155
  FSEv05_buildDTable_raw(DTableOffb, Offbits);
3430
3156
  break;
3431
3157
  case FSEv05_ENCODING_STATIC:
3158
+ if (!flagStaticTable) return ERROR(corruption_detected);
3432
3159
  break;
3433
3160
  case FSEv05_ENCODING_DYNAMIC :
3434
3161
  default : /* impossible */
3435
- max = MaxOff;
3436
- headerSize = FSEv05_readNCount(norm, &max, &Offlog, ip, iend-ip);
3437
- if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3438
- if (Offlog > OffFSEv05Log) return ERROR(corruption_detected);
3439
- ip += headerSize;
3440
- FSEv05_buildDTable(DTableOffb, norm, max, Offlog);
3441
- }
3162
+ { U32 max = MaxOff;
3163
+ headerSize = FSEv05_readNCount(norm, &max, &Offlog, ip, iend-ip);
3164
+ if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3165
+ if (Offlog > OffFSEv05Log) return ERROR(corruption_detected);
3166
+ ip += headerSize;
3167
+ FSEv05_buildDTable(DTableOffb, norm, max, Offlog);
3168
+ } }
3442
3169
 
3443
3170
  switch(MLtype)
3444
3171
  {
3445
- U32 max;
3446
3172
  case FSEv05_ENCODING_RLE :
3447
3173
  MLlog = 0;
3448
3174
  if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
@@ -3453,16 +3179,17 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3453
3179
  FSEv05_buildDTable_raw(DTableML, MLbits);
3454
3180
  break;
3455
3181
  case FSEv05_ENCODING_STATIC:
3182
+ if (!flagStaticTable) return ERROR(corruption_detected);
3456
3183
  break;
3457
3184
  case FSEv05_ENCODING_DYNAMIC :
3458
3185
  default : /* impossible */
3459
- max = MaxML;
3460
- headerSize = FSEv05_readNCount(norm, &max, &MLlog, ip, iend-ip);
3461
- if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3462
- if (MLlog > MLFSEv05Log) return ERROR(corruption_detected);
3463
- ip += headerSize;
3464
- FSEv05_buildDTable(DTableML, norm, max, MLlog);
3465
- } }
3186
+ { U32 max = MaxML;
3187
+ headerSize = FSEv05_readNCount(norm, &max, &MLlog, ip, iend-ip);
3188
+ if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3189
+ if (MLlog > MLFSEv05Log) return ERROR(corruption_detected);
3190
+ ip += headerSize;
3191
+ FSEv05_buildDTable(DTableML, norm, max, MLlog);
3192
+ } } }
3466
3193
 
3467
3194
  return ip-istart;
3468
3195
  }
@@ -3601,7 +3328,12 @@ static size_t ZSTDv05_execSequence(BYTE* op,
3601
3328
  op = oLitEnd + length1;
3602
3329
  sequence.matchLength -= length1;
3603
3330
  match = base;
3331
+ if (op > oend_8) {
3332
+ while (op < oMatchEnd) *op++ = *match++;
3333
+ return sequenceLength;
3334
+ }
3604
3335
  } }
3336
+ /* Requirement: op <= oend_8 */
3605
3337
 
3606
3338
  /* match within prefix */
3607
3339
  if (sequence.offset < 8) {
@@ -3660,7 +3392,7 @@ static size_t ZSTDv05_decompressSequences(
3660
3392
  /* Build Decoding Tables */
3661
3393
  errorCode = ZSTDv05_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
3662
3394
  DTableLL, DTableML, DTableOffb,
3663
- ip, seqSize);
3395
+ ip, seqSize, dctx->flagStaticTables);
3664
3396
  if (ZSTDv05_isError(errorCode)) return errorCode;
3665
3397
  ip += errorCode;
3666
3398
 
@@ -3949,11 +3681,11 @@ static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t d
3949
3681
  {
3950
3682
  size_t hSize, offcodeHeaderSize, matchlengthHeaderSize, errorCode, litlengthHeaderSize;
3951
3683
  short offcodeNCount[MaxOff+1];
3952
- U32 offcodeMaxValue=MaxOff, offcodeLog=OffFSEv05Log;
3684
+ U32 offcodeMaxValue=MaxOff, offcodeLog;
3953
3685
  short matchlengthNCount[MaxML+1];
3954
- unsigned matchlengthMaxValue = MaxML, matchlengthLog = MLFSEv05Log;
3686
+ unsigned matchlengthMaxValue = MaxML, matchlengthLog;
3955
3687
  short litlengthNCount[MaxLL+1];
3956
- unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSEv05Log;
3688
+ unsigned litlengthMaxValue = MaxLL, litlengthLog;
3957
3689
 
3958
3690
  hSize = HUFv05_readDTableX4(dctx->hufTableX4, dict, dictSize);
3959
3691
  if (HUFv05_isError(hSize)) return ERROR(dictionary_corrupted);
@@ -3962,6 +3694,7 @@ static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t d
3962
3694
 
3963
3695
  offcodeHeaderSize = FSEv05_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dict, dictSize);
3964
3696
  if (FSEv05_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
3697
+ if (offcodeLog > OffFSEv05Log) return ERROR(dictionary_corrupted);
3965
3698
  errorCode = FSEv05_buildDTable(dctx->OffTable, offcodeNCount, offcodeMaxValue, offcodeLog);
3966
3699
  if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3967
3700
  dict = (const char*)dict + offcodeHeaderSize;
@@ -3969,12 +3702,14 @@ static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t d
3969
3702
 
3970
3703
  matchlengthHeaderSize = FSEv05_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dict, dictSize);
3971
3704
  if (FSEv05_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
3705
+ if (matchlengthLog > MLFSEv05Log) return ERROR(dictionary_corrupted);
3972
3706
  errorCode = FSEv05_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog);
3973
3707
  if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3974
3708
  dict = (const char*)dict + matchlengthHeaderSize;
3975
3709
  dictSize -= matchlengthHeaderSize;
3976
3710
 
3977
3711
  litlengthHeaderSize = FSEv05_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dict, dictSize);
3712
+ if (litlengthLog > LLFSEv05Log) return ERROR(dictionary_corrupted);
3978
3713
  if (FSEv05_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
3979
3714
  errorCode = FSEv05_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog);
3980
3715
  if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
@@ -4092,7 +3827,7 @@ static size_t ZBUFFv05_limitCopy(void* dst, size_t maxDstSize, const void* src,
4092
3827
  * The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr.
4093
3828
  * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.
4094
3829
  * The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst .
4095
- * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
3830
+ * return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
4096
3831
  * or 0 when a frame is completely decoded
4097
3832
  * or an error code, which can be tested using ZBUFFv05_isError().
4098
3833
  *