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
@@ -37,30 +37,22 @@
37
37
  ****************************************************************/
38
38
  #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
39
39
  /* inline is defined */
40
- #elif defined(_MSC_VER)
40
+ #elif defined(_MSC_VER) || defined(__GNUC__)
41
41
  # define inline __inline
42
42
  #else
43
43
  # define inline /* disable inline */
44
44
  #endif
45
45
 
46
-
47
46
  #ifdef _MSC_VER /* Visual Studio */
48
- # define FORCE_INLINE static __forceinline
49
47
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
50
- #else
51
- # ifdef __GNUC__
52
- # define FORCE_INLINE static inline __attribute__((always_inline))
53
- # else
54
- # define FORCE_INLINE static inline
55
- # endif
56
48
  #endif
57
49
 
58
50
 
59
51
  /* **************************************************************
60
- * Includes
52
+ * Dependencies
61
53
  ****************************************************************/
62
54
  #include <string.h> /* memcpy, memset */
63
- #include "bitstream.h"
55
+ #include "bitstream.h" /* BIT_* */
64
56
  #include "fse.h" /* header compression */
65
57
  #define HUF_STATIC_LINKING_ONLY
66
58
  #include "huf.h"
@@ -103,7 +95,7 @@ size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize)
103
95
  HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;
104
96
 
105
97
  HUF_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
106
- //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
98
+ /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
107
99
 
108
100
  iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
109
101
  if (HUF_isError(iSize)) return iSize;
@@ -388,22 +380,22 @@ static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 co
388
380
  }
389
381
 
390
382
  /* fill DTable */
391
- { U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
392
- const U32 symbol = sortedSymbols[s].symbol;
393
- const U32 weight = sortedSymbols[s].weight;
394
- const U32 nbBits = nbBitsBaseline - weight;
395
- const U32 length = 1 << (sizeLog-nbBits);
396
- const U32 start = rankVal[weight];
397
- U32 i = start;
398
- const U32 end = start + length;
399
-
400
- MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
401
- DElt.nbBits = (BYTE)(nbBits + consumed);
402
- DElt.length = 2;
403
- do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
404
-
405
- rankVal[weight] += length;
406
- }}
383
+ { U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
384
+ const U32 symbol = sortedSymbols[s].symbol;
385
+ const U32 weight = sortedSymbols[s].weight;
386
+ const U32 nbBits = nbBitsBaseline - weight;
387
+ const U32 length = 1 << (sizeLog-nbBits);
388
+ const U32 start = rankVal[weight];
389
+ U32 i = start;
390
+ const U32 end = start + length;
391
+
392
+ MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
393
+ DElt.nbBits = (BYTE)(nbBits + consumed);
394
+ DElt.length = 2;
395
+ do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
396
+
397
+ rankVal[weight] += length;
398
+ } }
407
399
  }
408
400
 
409
401
  typedef U32 rankVal_t[HUF_TABLELOG_ABSOLUTEMAX][HUF_TABLELOG_ABSOLUTEMAX + 1];
@@ -442,8 +434,8 @@ static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
442
434
  MEM_writeLE16(&(DElt.sequence), symbol);
443
435
  DElt.nbBits = (BYTE)(nbBits);
444
436
  DElt.length = 1;
445
- { U32 u;
446
- const U32 end = start + length;
437
+ { U32 const end = start + length;
438
+ U32 u;
447
439
  for (u = start; u < end; u++) DTable[u] = DElt;
448
440
  } }
449
441
  rankVal[weight] += length;
@@ -467,7 +459,7 @@ size_t HUF_readDTableX4 (HUF_DTable* DTable, const void* src, size_t srcSize)
467
459
 
468
460
  HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(HUF_DTable)); /* if compilation fails here, assertion is false */
469
461
  if (maxTableLog > HUF_TABLELOG_ABSOLUTEMAX) return ERROR(tableLog_tooLarge);
470
- //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
462
+ /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
471
463
 
472
464
  iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
473
465
  if (HUF_isError(iSize)) return iSize;
@@ -533,7 +525,7 @@ size_t HUF_readDTableX4 (HUF_DTable* DTable, const void* src, size_t srcSize)
533
525
 
534
526
  static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
535
527
  {
536
- const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
528
+ size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
537
529
  memcpy(op, dt+val, 2);
538
530
  BIT_skipBits(DStream, dt[val].nbBits);
539
531
  return dt[val].length;
@@ -541,7 +533,7 @@ static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4
541
533
 
542
534
  static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
543
535
  {
544
- const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
536
+ size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
545
537
  memcpy(op, dt+val, 1);
546
538
  if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
547
539
  else {
@@ -570,7 +562,7 @@ static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* c
570
562
  BYTE* const pStart = p;
571
563
 
572
564
  /* up to 8 symbols at a time */
573
- while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd-7)) {
565
+ while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) {
574
566
  HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
575
567
  HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
576
568
  HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
@@ -578,7 +570,7 @@ static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* c
578
570
  }
579
571
 
580
572
  /* closer to end : up to 2 symbols at a time */
581
- while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-2))
573
+ while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2))
582
574
  HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
583
575
 
584
576
  while (p <= pEnd-2)
@@ -697,7 +689,7 @@ static size_t HUF_decompress4X4_usingDTable_internal(
697
689
 
698
690
  /* 16-32 symbols per loop (4-8 symbols per stream) */
699
691
  endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
700
- for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) {
692
+ for ( ; (endSignal==BIT_DStream_unfinished) & (op4<(oend-(sizeof(bitD4.bitContainer)-1))) ; ) {
701
693
  HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
702
694
  HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
703
695
  HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
@@ -722,7 +714,7 @@ static size_t HUF_decompress4X4_usingDTable_internal(
722
714
  if (op1 > opStart2) return ERROR(corruption_detected);
723
715
  if (op2 > opStart3) return ERROR(corruption_detected);
724
716
  if (op3 > opStart4) return ERROR(corruption_detected);
725
- /* note : op4 supposed already verified within main loop */
717
+ /* note : op4 already verified within main loop */
726
718
 
727
719
  /* finish bitStreams one by one */
728
720
  HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
@@ -848,9 +840,6 @@ size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcS
848
840
  { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
849
841
  return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
850
842
  }
851
-
852
- //return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
853
- //return HUF_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
854
843
  }
855
844
 
856
845
  size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
@@ -1,33 +1,12 @@
1
- /*
2
- Buffered version of Zstd compression library
3
- Copyright (C) 2015-2016, Yann Collet.
4
-
5
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
-
7
- Redistribution and use in source and binary forms, with or without
8
- modification, are permitted provided that the following conditions are
9
- met:
10
- * Redistributions of source code must retain the above copyright
11
- notice, this list of conditions and the following disclaimer.
12
- * Redistributions in binary form must reproduce the above
13
- copyright notice, this list of conditions and the following disclaimer
14
- in the documentation and/or other materials provided with the
15
- distribution.
16
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
-
28
- You can contact the author at :
29
- - zstd homepage : http://www.zstd.net/
30
- */
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
+ */
9
+
31
10
 
32
11
 
33
12
  /* *************************************
@@ -40,30 +19,6 @@
40
19
  #include "zbuff.h"
41
20
 
42
21
 
43
- /*-***************************************************************************
44
- * Streaming decompression howto
45
- *
46
- * A ZBUFF_DCtx object is required to track streaming operations.
47
- * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
48
- * Use ZBUFF_decompressInit() to start a new decompression operation,
49
- * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary.
50
- * Note that ZBUFF_DCtx objects can be re-init multiple times.
51
- *
52
- * Use ZBUFF_decompressContinue() repetitively to consume your input.
53
- * *srcSizePtr and *dstCapacityPtr can be any size.
54
- * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
55
- * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
56
- * The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change @dst.
57
- * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
58
- * or 0 when a frame is completely decoded,
59
- * or an error code, which can be tested using ZBUFF_isError().
60
- *
61
- * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize()
62
- * output : ZBUFF_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
63
- * input : ZBUFF_recommendedDInSize == 128KB + 3;
64
- * just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
65
- * *******************************************************************************/
66
-
67
22
  typedef enum { ZBUFFds_init, ZBUFFds_loadHeader,
68
23
  ZBUFFds_read, ZBUFFds_load, ZBUFFds_flush } ZBUFF_dStage;
69
24
 
@@ -83,7 +38,7 @@ struct ZBUFF_DCtx_s {
83
38
  BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
84
39
  size_t lhSize;
85
40
  ZSTD_customMem customMem;
86
- }; /* typedef'd to ZBUFF_DCtx within "zstd_buffered.h" */
41
+ }; /* typedef'd to ZBUFF_DCtx within "zbuff.h" */
87
42
 
88
43
 
89
44
  ZBUFF_DCtx* ZBUFF_createDCtx(void)
@@ -158,9 +113,9 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
158
113
  char* const ostart = (char*)dst;
159
114
  char* const oend = ostart + *dstCapacityPtr;
160
115
  char* op = ostart;
161
- U32 notDone = 1;
116
+ U32 someMoreWork = 1;
162
117
 
163
- while (notDone) {
118
+ while (someMoreWork) {
164
119
  switch(zbd->stage)
165
120
  {
166
121
  case ZBUFFds_init :
@@ -168,9 +123,9 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
168
123
 
169
124
  case ZBUFFds_loadHeader :
170
125
  { size_t const hSize = ZSTD_getFrameParams(&(zbd->fParams), zbd->headerBuffer, zbd->lhSize);
171
- if (hSize != 0) {
126
+ if (ZSTD_isError(hSize)) return hSize;
127
+ if (hSize != 0) { /* need more input */
172
128
  size_t const toLoad = hSize - zbd->lhSize; /* if hSize!=0, hSize > zbd->lhSize */
173
- if (ZSTD_isError(hSize)) return hSize;
174
129
  if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */
175
130
  memcpy(zbd->headerBuffer + zbd->lhSize, ip, iend-ip);
176
131
  zbd->lhSize += iend-ip;
@@ -184,7 +139,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
184
139
  /* Consume header */
185
140
  { size_t const h1Size = ZSTD_nextSrcSizeToDecompress(zbd->zd); /* == ZSTD_frameHeaderSize_min */
186
141
  size_t const h1Result = ZSTD_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer, h1Size);
187
- if (ZSTD_isError(h1Result)) return h1Result;
142
+ if (ZSTD_isError(h1Result)) return h1Result; /* should not happen : already checked */
188
143
  if (h1Size < zbd->lhSize) { /* long header */
189
144
  size_t const h2Size = ZSTD_nextSrcSizeToDecompress(zbd->zd);
190
145
  size_t const h2Result = ZSTD_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer+h1Size, h2Size);
@@ -194,7 +149,8 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
194
149
  zbd->fParams.windowSize = MAX(zbd->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
195
150
 
196
151
  /* Frame header instruct buffer sizes */
197
- { size_t const blockSize = MIN(zbd->fParams.windowSize, ZSTD_BLOCKSIZE_MAX);
152
+ { size_t const blockSize = MIN(zbd->fParams.windowSize, ZSTD_BLOCKSIZE_ABSOLUTEMAX);
153
+ size_t const neededOutSize = zbd->fParams.windowSize + blockSize;
198
154
  zbd->blockSize = blockSize;
199
155
  if (zbd->inBuffSize < blockSize) {
200
156
  zbd->customMem.customFree(zbd->customMem.opaque, zbd->inBuff);
@@ -202,20 +158,20 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
202
158
  zbd->inBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, blockSize);
203
159
  if (zbd->inBuff == NULL) return ERROR(memory_allocation);
204
160
  }
205
- { size_t const neededOutSize = zbd->fParams.windowSize + blockSize;
206
- if (zbd->outBuffSize < neededOutSize) {
207
- zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
208
- zbd->outBuffSize = neededOutSize;
209
- zbd->outBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, neededOutSize);
210
- if (zbd->outBuff == NULL) return ERROR(memory_allocation);
211
- } } }
161
+ if (zbd->outBuffSize < neededOutSize) {
162
+ zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
163
+ zbd->outBuffSize = neededOutSize;
164
+ zbd->outBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, neededOutSize);
165
+ if (zbd->outBuff == NULL) return ERROR(memory_allocation);
166
+ } }
212
167
  zbd->stage = ZBUFFds_read;
168
+ /* pass-through */
213
169
 
214
170
  case ZBUFFds_read:
215
171
  { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zbd->zd);
216
172
  if (neededInSize==0) { /* end of frame */
217
173
  zbd->stage = ZBUFFds_init;
218
- notDone = 0;
174
+ someMoreWork = 0;
219
175
  break;
220
176
  }
221
177
  if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
@@ -230,8 +186,9 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
230
186
  zbd->stage = ZBUFFds_flush;
231
187
  break;
232
188
  }
233
- if (ip==iend) { notDone = 0; break; } /* no more input */
189
+ if (ip==iend) { someMoreWork = 0; break; } /* no more input */
234
190
  zbd->stage = ZBUFFds_load;
191
+ /* pass-through */
235
192
  }
236
193
 
237
194
  case ZBUFFds_load:
@@ -242,7 +199,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
242
199
  loadedSize = ZBUFF_limitCopy(zbd->inBuff + zbd->inPos, toLoad, ip, iend-ip);
243
200
  ip += loadedSize;
244
201
  zbd->inPos += loadedSize;
245
- if (loadedSize < toLoad) { notDone = 0; break; } /* not enough input, wait for more */
202
+ if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
246
203
 
247
204
  /* decode loaded input */
248
205
  { const int isSkipFrame = ZSTD_isSkipFrame(zbd->zd);
@@ -254,7 +211,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
254
211
  if (!decodedSize && !isSkipFrame) { zbd->stage = ZBUFFds_read; break; } /* this was just a header */
255
212
  zbd->outEnd = zbd->outStart + decodedSize;
256
213
  zbd->stage = ZBUFFds_flush;
257
- // break; /* ZBUFFds_flush follows */
214
+ /* pass-through */
258
215
  } }
259
216
 
260
217
  case ZBUFFds_flush:
@@ -262,14 +219,14 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
262
219
  size_t const flushedSize = ZBUFF_limitCopy(op, oend-op, zbd->outBuff + zbd->outStart, toFlushSize);
263
220
  op += flushedSize;
264
221
  zbd->outStart += flushedSize;
265
- if (flushedSize == toFlushSize) {
222
+ if (flushedSize == toFlushSize) { /* flush completed */
266
223
  zbd->stage = ZBUFFds_read;
267
224
  if (zbd->outStart + zbd->blockSize > zbd->outBuffSize)
268
225
  zbd->outStart = zbd->outEnd = 0;
269
226
  break;
270
227
  }
271
228
  /* cannot flush everything */
272
- notDone = 0;
229
+ someMoreWork = 0;
273
230
  break;
274
231
  }
275
232
  default: return ERROR(GENERIC); /* impossible */
@@ -279,16 +236,17 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
279
236
  *srcSizePtr = ip-istart;
280
237
  *dstCapacityPtr = op-ostart;
281
238
  { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbd->zd);
282
- // if (nextSrcSizeHint > ZSTD_blockHeaderSize) nextSrcSizeHint+= ZSTD_blockHeaderSize; /* get following block header too */
239
+ if (!nextSrcSizeHint) return (zbd->outEnd != zbd->outStart); /* return 0 only if fully flushed too */
240
+ nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zbd->zd) == ZSTDnit_block);
241
+ if (zbd->inPos > nextSrcSizeHint) return ERROR(GENERIC); /* should never happen */
283
242
  nextSrcSizeHint -= zbd->inPos; /* already loaded*/
284
243
  return nextSrcSizeHint;
285
244
  }
286
245
  }
287
246
 
288
247
 
289
-
290
248
  /* *************************************
291
249
  * Tool functions
292
250
  ***************************************/
293
- size_t ZBUFF_recommendedDInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize /* block header size*/ ; }
294
- size_t ZBUFF_recommendedDOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }
251
+ size_t ZBUFF_recommendedDInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX + ZSTD_blockHeaderSize /* block header size*/ ; }
252
+ size_t ZBUFF_recommendedDOutSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; }