extzstd 0.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/HISTORY.ja.md +39 -0
- data/README.md +38 -56
- data/contrib/zstd/CHANGELOG +613 -0
- data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
- data/contrib/zstd/CONTRIBUTING.md +406 -0
- data/contrib/zstd/COPYING +339 -0
- data/contrib/zstd/Makefile +420 -0
- data/contrib/zstd/README.md +179 -41
- data/contrib/zstd/TESTING.md +44 -0
- data/contrib/zstd/appveyor.yml +292 -0
- data/contrib/zstd/lib/BUCK +234 -0
- data/contrib/zstd/lib/Makefile +451 -0
- data/contrib/zstd/lib/README.md +207 -0
- data/contrib/zstd/{common → lib/common}/bitstream.h +187 -138
- data/contrib/zstd/lib/common/compiler.h +288 -0
- data/contrib/zstd/lib/common/cpu.h +213 -0
- data/contrib/zstd/lib/common/debug.c +24 -0
- data/contrib/zstd/lib/common/debug.h +107 -0
- data/contrib/zstd/lib/common/entropy_common.c +362 -0
- data/contrib/zstd/{common → lib/common}/error_private.c +25 -12
- data/contrib/zstd/{common → lib/common}/error_private.h +14 -10
- data/contrib/zstd/{common → lib/common}/fse.h +173 -92
- data/contrib/zstd/{common → lib/common}/fse_decompress.c +149 -85
- data/contrib/zstd/lib/common/huf.h +361 -0
- data/contrib/zstd/{common → lib/common}/mem.h +115 -59
- data/contrib/zstd/lib/common/pool.c +350 -0
- data/contrib/zstd/lib/common/pool.h +84 -0
- data/contrib/zstd/lib/common/threading.c +122 -0
- data/contrib/zstd/lib/common/threading.h +155 -0
- data/contrib/zstd/{common → lib/common}/xxhash.c +55 -96
- data/contrib/zstd/{common → lib/common}/xxhash.h +23 -47
- data/contrib/zstd/lib/common/zstd_common.c +83 -0
- data/contrib/zstd/lib/common/zstd_deps.h +111 -0
- data/contrib/zstd/lib/common/zstd_errors.h +95 -0
- data/contrib/zstd/lib/common/zstd_internal.h +478 -0
- data/contrib/zstd/{compress → lib/compress}/fse_compress.c +214 -319
- data/contrib/zstd/lib/compress/hist.c +181 -0
- data/contrib/zstd/lib/compress/hist.h +75 -0
- data/contrib/zstd/lib/compress/huf_compress.c +913 -0
- data/contrib/zstd/lib/compress/zstd_compress.c +5208 -0
- data/contrib/zstd/lib/compress/zstd_compress_internal.h +1203 -0
- data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
- data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
- data/contrib/zstd/lib/compress/zstd_compress_sequences.c +433 -0
- data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
- data/contrib/zstd/lib/compress/zstd_compress_superblock.c +849 -0
- data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
- data/contrib/zstd/lib/compress/zstd_cwksp.h +561 -0
- data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
- data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
- data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
- data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
- data/contrib/zstd/lib/compress/zstd_lazy.c +1412 -0
- data/contrib/zstd/lib/compress/zstd_lazy.h +87 -0
- data/contrib/zstd/lib/compress/zstd_ldm.c +660 -0
- data/contrib/zstd/lib/compress/zstd_ldm.h +116 -0
- data/contrib/zstd/lib/compress/zstd_opt.c +1345 -0
- data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
- data/contrib/zstd/lib/compress/zstdmt_compress.c +1811 -0
- data/contrib/zstd/lib/compress/zstdmt_compress.h +110 -0
- data/contrib/zstd/lib/decompress/huf_decompress.c +1350 -0
- data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
- data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
- data/contrib/zstd/lib/decompress/zstd_decompress.c +1930 -0
- data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1540 -0
- data/contrib/zstd/lib/decompress/zstd_decompress_block.h +62 -0
- data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +190 -0
- data/contrib/zstd/{common → lib/deprecated}/zbuff.h +68 -45
- data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
- data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
- data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
- data/contrib/zstd/lib/dictBuilder/cover.c +1245 -0
- data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
- data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
- data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +0 -0
- data/contrib/zstd/lib/dictBuilder/fastcover.c +758 -0
- data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +318 -194
- data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
- data/contrib/zstd/{legacy → lib/legacy}/zstd_legacy.h +171 -15
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +191 -124
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +19 -5
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +125 -125
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +19 -5
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +125 -124
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +20 -6
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +151 -299
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +19 -5
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +237 -243
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +19 -6
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +130 -143
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +18 -5
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.c +158 -157
- data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.h +19 -5
- data/contrib/zstd/lib/libzstd.pc.in +15 -0
- data/contrib/zstd/lib/zstd.h +2391 -0
- data/ext/depend +2 -0
- data/ext/extconf.rb +15 -6
- data/ext/extzstd.c +76 -145
- data/ext/extzstd.h +80 -31
- data/ext/extzstd_stream.c +417 -142
- data/ext/libzstd_conf.h +8 -0
- data/ext/zstd_common.c +10 -7
- data/ext/zstd_compress.c +14 -5
- data/ext/zstd_decompress.c +5 -4
- data/ext/zstd_dictbuilder.c +9 -4
- data/ext/zstd_dictbuilder_fastcover.c +3 -0
- data/ext/zstd_legacy_v01.c +3 -1
- data/ext/zstd_legacy_v02.c +3 -1
- data/ext/zstd_legacy_v03.c +3 -1
- data/ext/zstd_legacy_v04.c +3 -1
- data/ext/zstd_legacy_v05.c +3 -1
- data/ext/zstd_legacy_v06.c +3 -1
- data/ext/zstd_legacy_v07.c +3 -1
- data/gemstub.rb +10 -24
- data/lib/extzstd.rb +64 -179
- data/lib/extzstd/version.rb +6 -1
- data/test/test_basic.rb +9 -6
- metadata +113 -57
- data/HISTORY.ja +0 -5
- data/contrib/zstd/common/entropy_common.c +0 -225
- data/contrib/zstd/common/huf.h +0 -228
- data/contrib/zstd/common/zstd_common.c +0 -83
- data/contrib/zstd/common/zstd_errors.h +0 -60
- data/contrib/zstd/common/zstd_internal.h +0 -267
- data/contrib/zstd/compress/huf_compress.c +0 -533
- data/contrib/zstd/compress/zbuff_compress.c +0 -319
- data/contrib/zstd/compress/zstd_compress.c +0 -3264
- data/contrib/zstd/compress/zstd_opt.h +0 -900
- data/contrib/zstd/decompress/huf_decompress.c +0 -883
- data/contrib/zstd/decompress/zbuff_decompress.c +0 -252
- data/contrib/zstd/decompress/zstd_decompress.c +0 -1842
- data/contrib/zstd/dictBuilder/zdict.h +0 -111
- data/contrib/zstd/zstd.h +0 -640
@@ -1,252 +0,0 @@
|
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
/* *************************************
|
13
|
-
* Dependencies
|
14
|
-
***************************************/
|
15
|
-
#include <stdlib.h>
|
16
|
-
#include "error_private.h"
|
17
|
-
#include "zstd_internal.h" /* MIN, ZSTD_blockHeaderSize, ZSTD_BLOCKSIZE_MAX */
|
18
|
-
#define ZBUFF_STATIC_LINKING_ONLY
|
19
|
-
#include "zbuff.h"
|
20
|
-
|
21
|
-
|
22
|
-
typedef enum { ZBUFFds_init, ZBUFFds_loadHeader,
|
23
|
-
ZBUFFds_read, ZBUFFds_load, ZBUFFds_flush } ZBUFF_dStage;
|
24
|
-
|
25
|
-
/* *** Resource management *** */
|
26
|
-
struct ZBUFF_DCtx_s {
|
27
|
-
ZSTD_DCtx* zd;
|
28
|
-
ZSTD_frameParams fParams;
|
29
|
-
ZBUFF_dStage stage;
|
30
|
-
char* inBuff;
|
31
|
-
size_t inBuffSize;
|
32
|
-
size_t inPos;
|
33
|
-
char* outBuff;
|
34
|
-
size_t outBuffSize;
|
35
|
-
size_t outStart;
|
36
|
-
size_t outEnd;
|
37
|
-
size_t blockSize;
|
38
|
-
BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
|
39
|
-
size_t lhSize;
|
40
|
-
ZSTD_customMem customMem;
|
41
|
-
}; /* typedef'd to ZBUFF_DCtx within "zbuff.h" */
|
42
|
-
|
43
|
-
|
44
|
-
ZBUFF_DCtx* ZBUFF_createDCtx(void)
|
45
|
-
{
|
46
|
-
return ZBUFF_createDCtx_advanced(defaultCustomMem);
|
47
|
-
}
|
48
|
-
|
49
|
-
ZBUFF_DCtx* ZBUFF_createDCtx_advanced(ZSTD_customMem customMem)
|
50
|
-
{
|
51
|
-
ZBUFF_DCtx* zbd;
|
52
|
-
|
53
|
-
if (!customMem.customAlloc && !customMem.customFree)
|
54
|
-
customMem = defaultCustomMem;
|
55
|
-
|
56
|
-
if (!customMem.customAlloc || !customMem.customFree)
|
57
|
-
return NULL;
|
58
|
-
|
59
|
-
zbd = (ZBUFF_DCtx*)customMem.customAlloc(customMem.opaque, sizeof(ZBUFF_DCtx));
|
60
|
-
if (zbd==NULL) return NULL;
|
61
|
-
memset(zbd, 0, sizeof(ZBUFF_DCtx));
|
62
|
-
memcpy(&zbd->customMem, &customMem, sizeof(ZSTD_customMem));
|
63
|
-
zbd->zd = ZSTD_createDCtx_advanced(customMem);
|
64
|
-
if (zbd->zd == NULL) { ZBUFF_freeDCtx(zbd); return NULL; }
|
65
|
-
zbd->stage = ZBUFFds_init;
|
66
|
-
return zbd;
|
67
|
-
}
|
68
|
-
|
69
|
-
size_t ZBUFF_freeDCtx(ZBUFF_DCtx* zbd)
|
70
|
-
{
|
71
|
-
if (zbd==NULL) return 0; /* support free on null */
|
72
|
-
ZSTD_freeDCtx(zbd->zd);
|
73
|
-
if (zbd->inBuff) zbd->customMem.customFree(zbd->customMem.opaque, zbd->inBuff);
|
74
|
-
if (zbd->outBuff) zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
|
75
|
-
zbd->customMem.customFree(zbd->customMem.opaque, zbd);
|
76
|
-
return 0;
|
77
|
-
}
|
78
|
-
|
79
|
-
|
80
|
-
/* *** Initialization *** */
|
81
|
-
|
82
|
-
size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* zbd, const void* dict, size_t dictSize)
|
83
|
-
{
|
84
|
-
zbd->stage = ZBUFFds_loadHeader;
|
85
|
-
zbd->lhSize = zbd->inPos = zbd->outStart = zbd->outEnd = 0;
|
86
|
-
return ZSTD_decompressBegin_usingDict(zbd->zd, dict, dictSize);
|
87
|
-
}
|
88
|
-
|
89
|
-
size_t ZBUFF_decompressInit(ZBUFF_DCtx* zbd)
|
90
|
-
{
|
91
|
-
return ZBUFF_decompressInitDictionary(zbd, NULL, 0);
|
92
|
-
}
|
93
|
-
|
94
|
-
|
95
|
-
/* internal util function */
|
96
|
-
MEM_STATIC size_t ZBUFF_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
|
97
|
-
{
|
98
|
-
size_t const length = MIN(dstCapacity, srcSize);
|
99
|
-
memcpy(dst, src, length);
|
100
|
-
return length;
|
101
|
-
}
|
102
|
-
|
103
|
-
|
104
|
-
/* *** Decompression *** */
|
105
|
-
|
106
|
-
size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
|
107
|
-
void* dst, size_t* dstCapacityPtr,
|
108
|
-
const void* src, size_t* srcSizePtr)
|
109
|
-
{
|
110
|
-
const char* const istart = (const char*)src;
|
111
|
-
const char* const iend = istart + *srcSizePtr;
|
112
|
-
const char* ip = istart;
|
113
|
-
char* const ostart = (char*)dst;
|
114
|
-
char* const oend = ostart + *dstCapacityPtr;
|
115
|
-
char* op = ostart;
|
116
|
-
U32 someMoreWork = 1;
|
117
|
-
|
118
|
-
while (someMoreWork) {
|
119
|
-
switch(zbd->stage)
|
120
|
-
{
|
121
|
-
case ZBUFFds_init :
|
122
|
-
return ERROR(init_missing);
|
123
|
-
|
124
|
-
case ZBUFFds_loadHeader :
|
125
|
-
{ size_t const hSize = ZSTD_getFrameParams(&(zbd->fParams), zbd->headerBuffer, zbd->lhSize);
|
126
|
-
if (ZSTD_isError(hSize)) return hSize;
|
127
|
-
if (hSize != 0) { /* need more input */
|
128
|
-
size_t const toLoad = hSize - zbd->lhSize; /* if hSize!=0, hSize > zbd->lhSize */
|
129
|
-
if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */
|
130
|
-
memcpy(zbd->headerBuffer + zbd->lhSize, ip, iend-ip);
|
131
|
-
zbd->lhSize += iend-ip;
|
132
|
-
*dstCapacityPtr = 0;
|
133
|
-
return (hSize - zbd->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
|
134
|
-
}
|
135
|
-
memcpy(zbd->headerBuffer + zbd->lhSize, ip, toLoad); zbd->lhSize = hSize; ip += toLoad;
|
136
|
-
break;
|
137
|
-
} }
|
138
|
-
|
139
|
-
/* Consume header */
|
140
|
-
{ size_t const h1Size = ZSTD_nextSrcSizeToDecompress(zbd->zd); /* == ZSTD_frameHeaderSize_min */
|
141
|
-
size_t const h1Result = ZSTD_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer, h1Size);
|
142
|
-
if (ZSTD_isError(h1Result)) return h1Result; /* should not happen : already checked */
|
143
|
-
if (h1Size < zbd->lhSize) { /* long header */
|
144
|
-
size_t const h2Size = ZSTD_nextSrcSizeToDecompress(zbd->zd);
|
145
|
-
size_t const h2Result = ZSTD_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer+h1Size, h2Size);
|
146
|
-
if (ZSTD_isError(h2Result)) return h2Result;
|
147
|
-
} }
|
148
|
-
|
149
|
-
zbd->fParams.windowSize = MAX(zbd->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
|
150
|
-
|
151
|
-
/* Frame header instruct buffer sizes */
|
152
|
-
{ size_t const blockSize = MIN(zbd->fParams.windowSize, ZSTD_BLOCKSIZE_ABSOLUTEMAX);
|
153
|
-
size_t const neededOutSize = zbd->fParams.windowSize + blockSize;
|
154
|
-
zbd->blockSize = blockSize;
|
155
|
-
if (zbd->inBuffSize < blockSize) {
|
156
|
-
zbd->customMem.customFree(zbd->customMem.opaque, zbd->inBuff);
|
157
|
-
zbd->inBuffSize = blockSize;
|
158
|
-
zbd->inBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, blockSize);
|
159
|
-
if (zbd->inBuff == NULL) return ERROR(memory_allocation);
|
160
|
-
}
|
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
|
-
} }
|
167
|
-
zbd->stage = ZBUFFds_read;
|
168
|
-
/* pass-through */
|
169
|
-
|
170
|
-
case ZBUFFds_read:
|
171
|
-
{ size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zbd->zd);
|
172
|
-
if (neededInSize==0) { /* end of frame */
|
173
|
-
zbd->stage = ZBUFFds_init;
|
174
|
-
someMoreWork = 0;
|
175
|
-
break;
|
176
|
-
}
|
177
|
-
if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
|
178
|
-
const int isSkipFrame = ZSTD_isSkipFrame(zbd->zd);
|
179
|
-
size_t const decodedSize = ZSTD_decompressContinue(zbd->zd,
|
180
|
-
zbd->outBuff + zbd->outStart, (isSkipFrame ? 0 : zbd->outBuffSize - zbd->outStart),
|
181
|
-
ip, neededInSize);
|
182
|
-
if (ZSTD_isError(decodedSize)) return decodedSize;
|
183
|
-
ip += neededInSize;
|
184
|
-
if (!decodedSize && !isSkipFrame) break; /* this was just a header */
|
185
|
-
zbd->outEnd = zbd->outStart + decodedSize;
|
186
|
-
zbd->stage = ZBUFFds_flush;
|
187
|
-
break;
|
188
|
-
}
|
189
|
-
if (ip==iend) { someMoreWork = 0; break; } /* no more input */
|
190
|
-
zbd->stage = ZBUFFds_load;
|
191
|
-
/* pass-through */
|
192
|
-
}
|
193
|
-
|
194
|
-
case ZBUFFds_load:
|
195
|
-
{ size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zbd->zd);
|
196
|
-
size_t const toLoad = neededInSize - zbd->inPos; /* should always be <= remaining space within inBuff */
|
197
|
-
size_t loadedSize;
|
198
|
-
if (toLoad > zbd->inBuffSize - zbd->inPos) return ERROR(corruption_detected); /* should never happen */
|
199
|
-
loadedSize = ZBUFF_limitCopy(zbd->inBuff + zbd->inPos, toLoad, ip, iend-ip);
|
200
|
-
ip += loadedSize;
|
201
|
-
zbd->inPos += loadedSize;
|
202
|
-
if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
|
203
|
-
|
204
|
-
/* decode loaded input */
|
205
|
-
{ const int isSkipFrame = ZSTD_isSkipFrame(zbd->zd);
|
206
|
-
size_t const decodedSize = ZSTD_decompressContinue(zbd->zd,
|
207
|
-
zbd->outBuff + zbd->outStart, zbd->outBuffSize - zbd->outStart,
|
208
|
-
zbd->inBuff, neededInSize);
|
209
|
-
if (ZSTD_isError(decodedSize)) return decodedSize;
|
210
|
-
zbd->inPos = 0; /* input is consumed */
|
211
|
-
if (!decodedSize && !isSkipFrame) { zbd->stage = ZBUFFds_read; break; } /* this was just a header */
|
212
|
-
zbd->outEnd = zbd->outStart + decodedSize;
|
213
|
-
zbd->stage = ZBUFFds_flush;
|
214
|
-
/* pass-through */
|
215
|
-
} }
|
216
|
-
|
217
|
-
case ZBUFFds_flush:
|
218
|
-
{ size_t const toFlushSize = zbd->outEnd - zbd->outStart;
|
219
|
-
size_t const flushedSize = ZBUFF_limitCopy(op, oend-op, zbd->outBuff + zbd->outStart, toFlushSize);
|
220
|
-
op += flushedSize;
|
221
|
-
zbd->outStart += flushedSize;
|
222
|
-
if (flushedSize == toFlushSize) { /* flush completed */
|
223
|
-
zbd->stage = ZBUFFds_read;
|
224
|
-
if (zbd->outStart + zbd->blockSize > zbd->outBuffSize)
|
225
|
-
zbd->outStart = zbd->outEnd = 0;
|
226
|
-
break;
|
227
|
-
}
|
228
|
-
/* cannot flush everything */
|
229
|
-
someMoreWork = 0;
|
230
|
-
break;
|
231
|
-
}
|
232
|
-
default: return ERROR(GENERIC); /* impossible */
|
233
|
-
} }
|
234
|
-
|
235
|
-
/* result */
|
236
|
-
*srcSizePtr = ip-istart;
|
237
|
-
*dstCapacityPtr = op-ostart;
|
238
|
-
{ size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbd->zd);
|
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 */
|
242
|
-
nextSrcSizeHint -= zbd->inPos; /* already loaded*/
|
243
|
-
return nextSrcSizeHint;
|
244
|
-
}
|
245
|
-
}
|
246
|
-
|
247
|
-
|
248
|
-
/* *************************************
|
249
|
-
* Tool functions
|
250
|
-
***************************************/
|
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; }
|
@@ -1,1842 +0,0 @@
|
|
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
|
-
|
10
|
-
|
11
|
-
/* ***************************************************************
|
12
|
-
* Tuning parameters
|
13
|
-
*****************************************************************/
|
14
|
-
/*!
|
15
|
-
* HEAPMODE :
|
16
|
-
* Select how default decompression function ZSTD_decompress() will allocate memory,
|
17
|
-
* in memory stack (0), or in memory heap (1, requires malloc())
|
18
|
-
*/
|
19
|
-
#ifndef ZSTD_HEAPMODE
|
20
|
-
# define ZSTD_HEAPMODE 1
|
21
|
-
#endif
|
22
|
-
|
23
|
-
/*!
|
24
|
-
* LEGACY_SUPPORT :
|
25
|
-
* if set to 1, ZSTD_decompress() can decode older formats (v0.1+)
|
26
|
-
*/
|
27
|
-
#ifndef ZSTD_LEGACY_SUPPORT
|
28
|
-
# define ZSTD_LEGACY_SUPPORT 0
|
29
|
-
#endif
|
30
|
-
|
31
|
-
/*!
|
32
|
-
* MAXWINDOWSIZE_DEFAULT :
|
33
|
-
* maximum window size accepted by DStream, by default.
|
34
|
-
* Frames requiring more memory will be rejected.
|
35
|
-
*/
|
36
|
-
#ifndef ZSTD_MAXWINDOWSIZE_DEFAULT
|
37
|
-
# define ZSTD_MAXWINDOWSIZE_DEFAULT ((1 << ZSTD_WINDOWLOG_MAX) + 1) /* defined within zstd.h */
|
38
|
-
#endif
|
39
|
-
|
40
|
-
|
41
|
-
/*-*******************************************************
|
42
|
-
* Dependencies
|
43
|
-
*********************************************************/
|
44
|
-
#include <string.h> /* memcpy, memmove, memset */
|
45
|
-
#include "mem.h" /* low level memory routines */
|
46
|
-
#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
|
47
|
-
#include "xxhash.h" /* XXH64_* */
|
48
|
-
#define FSE_STATIC_LINKING_ONLY
|
49
|
-
#include "fse.h"
|
50
|
-
#define HUF_STATIC_LINKING_ONLY
|
51
|
-
#include "huf.h"
|
52
|
-
#include "zstd_internal.h"
|
53
|
-
|
54
|
-
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
|
55
|
-
# include "zstd_legacy.h"
|
56
|
-
#endif
|
57
|
-
|
58
|
-
|
59
|
-
/*-*************************************
|
60
|
-
* Macros
|
61
|
-
***************************************/
|
62
|
-
#define ZSTD_isError ERR_isError /* for inlining */
|
63
|
-
#define FSE_isError ERR_isError
|
64
|
-
#define HUF_isError ERR_isError
|
65
|
-
|
66
|
-
|
67
|
-
/*_*******************************************************
|
68
|
-
* Memory operations
|
69
|
-
**********************************************************/
|
70
|
-
static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
|
71
|
-
|
72
|
-
|
73
|
-
/*-*************************************************************
|
74
|
-
* Context management
|
75
|
-
***************************************************************/
|
76
|
-
typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,
|
77
|
-
ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,
|
78
|
-
ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,
|
79
|
-
ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;
|
80
|
-
|
81
|
-
struct ZSTD_DCtx_s
|
82
|
-
{
|
83
|
-
const FSE_DTable* LLTptr;
|
84
|
-
const FSE_DTable* MLTptr;
|
85
|
-
const FSE_DTable* OFTptr;
|
86
|
-
const HUF_DTable* HUFptr;
|
87
|
-
FSE_DTable LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
|
88
|
-
FSE_DTable OFTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
|
89
|
-
FSE_DTable MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
|
90
|
-
HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)]; /* can accommodate HUF_decompress4X */
|
91
|
-
const void* previousDstEnd;
|
92
|
-
const void* base;
|
93
|
-
const void* vBase;
|
94
|
-
const void* dictEnd;
|
95
|
-
size_t expected;
|
96
|
-
U32 rep[ZSTD_REP_NUM];
|
97
|
-
ZSTD_frameParams fParams;
|
98
|
-
blockType_e bType; /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */
|
99
|
-
ZSTD_dStage stage;
|
100
|
-
U32 litEntropy;
|
101
|
-
U32 fseEntropy;
|
102
|
-
XXH64_state_t xxhState;
|
103
|
-
size_t headerSize;
|
104
|
-
U32 dictID;
|
105
|
-
const BYTE* litPtr;
|
106
|
-
ZSTD_customMem customMem;
|
107
|
-
size_t litBufSize;
|
108
|
-
size_t litSize;
|
109
|
-
size_t rleSize;
|
110
|
-
BYTE litBuffer[ZSTD_BLOCKSIZE_ABSOLUTEMAX + WILDCOPY_OVERLENGTH];
|
111
|
-
BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];
|
112
|
-
}; /* typedef'd to ZSTD_DCtx within "zstd.h" */
|
113
|
-
|
114
|
-
size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx) { return (dctx==NULL) ? 0 : sizeof(ZSTD_DCtx); }
|
115
|
-
|
116
|
-
size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }
|
117
|
-
|
118
|
-
size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
|
119
|
-
{
|
120
|
-
dctx->expected = ZSTD_frameHeaderSize_prefix;
|
121
|
-
dctx->stage = ZSTDds_getFrameHeaderSize;
|
122
|
-
dctx->previousDstEnd = NULL;
|
123
|
-
dctx->base = NULL;
|
124
|
-
dctx->vBase = NULL;
|
125
|
-
dctx->dictEnd = NULL;
|
126
|
-
dctx->hufTable[0] = (HUF_DTable)((HufLog)*0x1000001); /* cover both little and big endian */
|
127
|
-
dctx->litEntropy = dctx->fseEntropy = 0;
|
128
|
-
dctx->dictID = 0;
|
129
|
-
MEM_STATIC_ASSERT(sizeof(dctx->rep) == sizeof(repStartValue));
|
130
|
-
memcpy(dctx->rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */
|
131
|
-
dctx->LLTptr = dctx->LLTable;
|
132
|
-
dctx->MLTptr = dctx->MLTable;
|
133
|
-
dctx->OFTptr = dctx->OFTable;
|
134
|
-
dctx->HUFptr = dctx->hufTable;
|
135
|
-
return 0;
|
136
|
-
}
|
137
|
-
|
138
|
-
ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
|
139
|
-
{
|
140
|
-
ZSTD_DCtx* dctx;
|
141
|
-
|
142
|
-
if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
|
143
|
-
if (!customMem.customAlloc || !customMem.customFree) return NULL;
|
144
|
-
|
145
|
-
dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(ZSTD_DCtx), customMem);
|
146
|
-
if (!dctx) return NULL;
|
147
|
-
memcpy(&dctx->customMem, &customMem, sizeof(customMem));
|
148
|
-
ZSTD_decompressBegin(dctx);
|
149
|
-
return dctx;
|
150
|
-
}
|
151
|
-
|
152
|
-
ZSTD_DCtx* ZSTD_createDCtx(void)
|
153
|
-
{
|
154
|
-
return ZSTD_createDCtx_advanced(defaultCustomMem);
|
155
|
-
}
|
156
|
-
|
157
|
-
size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
|
158
|
-
{
|
159
|
-
if (dctx==NULL) return 0; /* support free on NULL */
|
160
|
-
ZSTD_free(dctx, dctx->customMem);
|
161
|
-
return 0; /* reserved as a potential error code in the future */
|
162
|
-
}
|
163
|
-
|
164
|
-
void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
|
165
|
-
{
|
166
|
-
size_t const workSpaceSize = (ZSTD_BLOCKSIZE_ABSOLUTEMAX+WILDCOPY_OVERLENGTH) + ZSTD_frameHeaderSize_max;
|
167
|
-
memcpy(dstDCtx, srcDCtx, sizeof(ZSTD_DCtx) - workSpaceSize); /* no need to copy workspace */
|
168
|
-
}
|
169
|
-
|
170
|
-
static void ZSTD_refDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
|
171
|
-
{
|
172
|
-
ZSTD_decompressBegin(dstDCtx); /* init */
|
173
|
-
if (srcDCtx) { /* support refDCtx on NULL */
|
174
|
-
dstDCtx->dictEnd = srcDCtx->dictEnd;
|
175
|
-
dstDCtx->vBase = srcDCtx->vBase;
|
176
|
-
dstDCtx->base = srcDCtx->base;
|
177
|
-
dstDCtx->previousDstEnd = srcDCtx->previousDstEnd;
|
178
|
-
dstDCtx->dictID = srcDCtx->dictID;
|
179
|
-
dstDCtx->litEntropy = srcDCtx->litEntropy;
|
180
|
-
dstDCtx->fseEntropy = srcDCtx->fseEntropy;
|
181
|
-
dstDCtx->LLTptr = srcDCtx->LLTable;
|
182
|
-
dstDCtx->MLTptr = srcDCtx->MLTable;
|
183
|
-
dstDCtx->OFTptr = srcDCtx->OFTable;
|
184
|
-
dstDCtx->HUFptr = srcDCtx->hufTable;
|
185
|
-
dstDCtx->rep[0] = srcDCtx->rep[0];
|
186
|
-
dstDCtx->rep[1] = srcDCtx->rep[1];
|
187
|
-
dstDCtx->rep[2] = srcDCtx->rep[2];
|
188
|
-
}
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
/*-*************************************************************
|
193
|
-
* Decompression section
|
194
|
-
***************************************************************/
|
195
|
-
|
196
|
-
/* See compression format details in : doc/zstd_compression_format.md */
|
197
|
-
|
198
|
-
/** ZSTD_frameHeaderSize() :
|
199
|
-
* srcSize must be >= ZSTD_frameHeaderSize_prefix.
|
200
|
-
* @return : size of the Frame Header */
|
201
|
-
static size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)
|
202
|
-
{
|
203
|
-
if (srcSize < ZSTD_frameHeaderSize_prefix) return ERROR(srcSize_wrong);
|
204
|
-
{ BYTE const fhd = ((const BYTE*)src)[4];
|
205
|
-
U32 const dictID= fhd & 3;
|
206
|
-
U32 const singleSegment = (fhd >> 5) & 1;
|
207
|
-
U32 const fcsId = fhd >> 6;
|
208
|
-
return ZSTD_frameHeaderSize_prefix + !singleSegment + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]
|
209
|
-
+ (singleSegment && !fcsId);
|
210
|
-
}
|
211
|
-
}
|
212
|
-
|
213
|
-
|
214
|
-
/** ZSTD_getFrameParams() :
|
215
|
-
* decode Frame Header, or require larger `srcSize`.
|
216
|
-
* @return : 0, `fparamsPtr` is correctly filled,
|
217
|
-
* >0, `srcSize` is too small, result is expected `srcSize`,
|
218
|
-
* or an error code, which can be tested using ZSTD_isError() */
|
219
|
-
size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize)
|
220
|
-
{
|
221
|
-
const BYTE* ip = (const BYTE*)src;
|
222
|
-
|
223
|
-
if (srcSize < ZSTD_frameHeaderSize_prefix) return ZSTD_frameHeaderSize_prefix;
|
224
|
-
if (MEM_readLE32(src) != ZSTD_MAGICNUMBER) {
|
225
|
-
if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) {
|
226
|
-
if (srcSize < ZSTD_skippableHeaderSize) return ZSTD_skippableHeaderSize; /* magic number + skippable frame length */
|
227
|
-
memset(fparamsPtr, 0, sizeof(*fparamsPtr));
|
228
|
-
fparamsPtr->frameContentSize = MEM_readLE32((const char *)src + 4);
|
229
|
-
fparamsPtr->windowSize = 0; /* windowSize==0 means a frame is skippable */
|
230
|
-
return 0;
|
231
|
-
}
|
232
|
-
return ERROR(prefix_unknown);
|
233
|
-
}
|
234
|
-
|
235
|
-
/* ensure there is enough `srcSize` to fully read/decode frame header */
|
236
|
-
{ size_t const fhsize = ZSTD_frameHeaderSize(src, srcSize);
|
237
|
-
if (srcSize < fhsize) return fhsize; }
|
238
|
-
|
239
|
-
{ BYTE const fhdByte = ip[4];
|
240
|
-
size_t pos = 5;
|
241
|
-
U32 const dictIDSizeCode = fhdByte&3;
|
242
|
-
U32 const checksumFlag = (fhdByte>>2)&1;
|
243
|
-
U32 const singleSegment = (fhdByte>>5)&1;
|
244
|
-
U32 const fcsID = fhdByte>>6;
|
245
|
-
U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX;
|
246
|
-
U32 windowSize = 0;
|
247
|
-
U32 dictID = 0;
|
248
|
-
U64 frameContentSize = 0;
|
249
|
-
if ((fhdByte & 0x08) != 0) return ERROR(frameParameter_unsupported); /* reserved bits, which must be zero */
|
250
|
-
if (!singleSegment) {
|
251
|
-
BYTE const wlByte = ip[pos++];
|
252
|
-
U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;
|
253
|
-
if (windowLog > ZSTD_WINDOWLOG_MAX) return ERROR(frameParameter_windowTooLarge); /* avoids issue with 1 << windowLog */
|
254
|
-
windowSize = (1U << windowLog);
|
255
|
-
windowSize += (windowSize >> 3) * (wlByte&7);
|
256
|
-
}
|
257
|
-
|
258
|
-
switch(dictIDSizeCode)
|
259
|
-
{
|
260
|
-
default: /* impossible */
|
261
|
-
case 0 : break;
|
262
|
-
case 1 : dictID = ip[pos]; pos++; break;
|
263
|
-
case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
|
264
|
-
case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
|
265
|
-
}
|
266
|
-
switch(fcsID)
|
267
|
-
{
|
268
|
-
default: /* impossible */
|
269
|
-
case 0 : if (singleSegment) frameContentSize = ip[pos]; break;
|
270
|
-
case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
|
271
|
-
case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
|
272
|
-
case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
|
273
|
-
}
|
274
|
-
if (!windowSize) windowSize = (U32)frameContentSize;
|
275
|
-
if (windowSize > windowSizeMax) return ERROR(frameParameter_windowTooLarge);
|
276
|
-
fparamsPtr->frameContentSize = frameContentSize;
|
277
|
-
fparamsPtr->windowSize = windowSize;
|
278
|
-
fparamsPtr->dictID = dictID;
|
279
|
-
fparamsPtr->checksumFlag = checksumFlag;
|
280
|
-
}
|
281
|
-
return 0;
|
282
|
-
}
|
283
|
-
|
284
|
-
|
285
|
-
/** ZSTD_getDecompressedSize() :
|
286
|
-
* compatible with legacy mode
|
287
|
-
* @return : decompressed size if known, 0 otherwise
|
288
|
-
note : 0 can mean any of the following :
|
289
|
-
- decompressed size is not present within frame header
|
290
|
-
- frame header unknown / not supported
|
291
|
-
- frame header not complete (`srcSize` too small) */
|
292
|
-
unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
|
293
|
-
{
|
294
|
-
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
295
|
-
if (ZSTD_isLegacy(src, srcSize)) return ZSTD_getDecompressedSize_legacy(src, srcSize);
|
296
|
-
#endif
|
297
|
-
{ ZSTD_frameParams fparams;
|
298
|
-
size_t const frResult = ZSTD_getFrameParams(&fparams, src, srcSize);
|
299
|
-
if (frResult!=0) return 0;
|
300
|
-
return fparams.frameContentSize;
|
301
|
-
}
|
302
|
-
}
|
303
|
-
|
304
|
-
|
305
|
-
/** ZSTD_decodeFrameHeader() :
|
306
|
-
* `headerSize` must be the size provided by ZSTD_frameHeaderSize().
|
307
|
-
* @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
|
308
|
-
static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
|
309
|
-
{
|
310
|
-
size_t const result = ZSTD_getFrameParams(&(dctx->fParams), src, headerSize);
|
311
|
-
if (ZSTD_isError(result)) return result; /* invalid header */
|
312
|
-
if (result>0) return ERROR(srcSize_wrong); /* headerSize too small */
|
313
|
-
if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID)) return ERROR(dictionary_wrong);
|
314
|
-
if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
|
315
|
-
return 0;
|
316
|
-
}
|
317
|
-
|
318
|
-
|
319
|
-
typedef struct
|
320
|
-
{
|
321
|
-
blockType_e blockType;
|
322
|
-
U32 lastBlock;
|
323
|
-
U32 origSize;
|
324
|
-
} blockProperties_t;
|
325
|
-
|
326
|
-
/*! ZSTD_getcBlockSize() :
|
327
|
-
* Provides the size of compressed block from block header `src` */
|
328
|
-
size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
|
329
|
-
{
|
330
|
-
if (srcSize < ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
|
331
|
-
{ U32 const cBlockHeader = MEM_readLE24(src);
|
332
|
-
U32 const cSize = cBlockHeader >> 3;
|
333
|
-
bpPtr->lastBlock = cBlockHeader & 1;
|
334
|
-
bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3);
|
335
|
-
bpPtr->origSize = cSize; /* only useful for RLE */
|
336
|
-
if (bpPtr->blockType == bt_rle) return 1;
|
337
|
-
if (bpPtr->blockType == bt_reserved) return ERROR(corruption_detected);
|
338
|
-
return cSize;
|
339
|
-
}
|
340
|
-
}
|
341
|
-
|
342
|
-
|
343
|
-
static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
|
344
|
-
{
|
345
|
-
if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
|
346
|
-
memcpy(dst, src, srcSize);
|
347
|
-
return srcSize;
|
348
|
-
}
|
349
|
-
|
350
|
-
|
351
|
-
static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity, const void* src, size_t srcSize, size_t regenSize)
|
352
|
-
{
|
353
|
-
if (srcSize != 1) return ERROR(srcSize_wrong);
|
354
|
-
if (regenSize > dstCapacity) return ERROR(dstSize_tooSmall);
|
355
|
-
memset(dst, *(const BYTE*)src, regenSize);
|
356
|
-
return regenSize;
|
357
|
-
}
|
358
|
-
|
359
|
-
/*! ZSTD_decodeLiteralsBlock() :
|
360
|
-
@return : nb of bytes read from src (< srcSize ) */
|
361
|
-
size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
|
362
|
-
const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
|
363
|
-
{
|
364
|
-
if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
|
365
|
-
|
366
|
-
{ const BYTE* const istart = (const BYTE*) src;
|
367
|
-
symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
|
368
|
-
|
369
|
-
switch(litEncType)
|
370
|
-
{
|
371
|
-
case set_repeat:
|
372
|
-
if (dctx->litEntropy==0) return ERROR(dictionary_corrupted);
|
373
|
-
/* fall-through */
|
374
|
-
case set_compressed:
|
375
|
-
if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */
|
376
|
-
{ size_t lhSize, litSize, litCSize;
|
377
|
-
U32 singleStream=0;
|
378
|
-
U32 const lhlCode = (istart[0] >> 2) & 3;
|
379
|
-
U32 const lhc = MEM_readLE32(istart);
|
380
|
-
switch(lhlCode)
|
381
|
-
{
|
382
|
-
case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */
|
383
|
-
/* 2 - 2 - 10 - 10 */
|
384
|
-
singleStream = !lhlCode;
|
385
|
-
lhSize = 3;
|
386
|
-
litSize = (lhc >> 4) & 0x3FF;
|
387
|
-
litCSize = (lhc >> 14) & 0x3FF;
|
388
|
-
break;
|
389
|
-
case 2:
|
390
|
-
/* 2 - 2 - 14 - 14 */
|
391
|
-
lhSize = 4;
|
392
|
-
litSize = (lhc >> 4) & 0x3FFF;
|
393
|
-
litCSize = lhc >> 18;
|
394
|
-
break;
|
395
|
-
case 3:
|
396
|
-
/* 2 - 2 - 18 - 18 */
|
397
|
-
lhSize = 5;
|
398
|
-
litSize = (lhc >> 4) & 0x3FFFF;
|
399
|
-
litCSize = (lhc >> 22) + (istart[4] << 10);
|
400
|
-
break;
|
401
|
-
}
|
402
|
-
if (litSize > ZSTD_BLOCKSIZE_ABSOLUTEMAX) return ERROR(corruption_detected);
|
403
|
-
if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
|
404
|
-
|
405
|
-
if (HUF_isError((litEncType==set_repeat) ?
|
406
|
-
( singleStream ?
|
407
|
-
HUF_decompress1X_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr) :
|
408
|
-
HUF_decompress4X_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->HUFptr) ) :
|
409
|
-
( singleStream ?
|
410
|
-
HUF_decompress1X2_DCtx(dctx->hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize) :
|
411
|
-
HUF_decompress4X_hufOnly (dctx->hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize)) ))
|
412
|
-
return ERROR(corruption_detected);
|
413
|
-
|
414
|
-
dctx->litPtr = dctx->litBuffer;
|
415
|
-
dctx->litBufSize = ZSTD_BLOCKSIZE_ABSOLUTEMAX+WILDCOPY_OVERLENGTH;
|
416
|
-
dctx->litSize = litSize;
|
417
|
-
dctx->litEntropy = 1;
|
418
|
-
if (litEncType==set_compressed) dctx->HUFptr = dctx->hufTable;
|
419
|
-
return litCSize + lhSize;
|
420
|
-
}
|
421
|
-
|
422
|
-
case set_basic:
|
423
|
-
{ size_t litSize, lhSize;
|
424
|
-
U32 const lhlCode = ((istart[0]) >> 2) & 3;
|
425
|
-
switch(lhlCode)
|
426
|
-
{
|
427
|
-
case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
|
428
|
-
lhSize = 1;
|
429
|
-
litSize = istart[0] >> 3;
|
430
|
-
break;
|
431
|
-
case 1:
|
432
|
-
lhSize = 2;
|
433
|
-
litSize = MEM_readLE16(istart) >> 4;
|
434
|
-
break;
|
435
|
-
case 3:
|
436
|
-
lhSize = 3;
|
437
|
-
litSize = MEM_readLE24(istart) >> 4;
|
438
|
-
break;
|
439
|
-
}
|
440
|
-
|
441
|
-
if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
|
442
|
-
if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
|
443
|
-
memcpy(dctx->litBuffer, istart+lhSize, litSize);
|
444
|
-
dctx->litPtr = dctx->litBuffer;
|
445
|
-
dctx->litBufSize = ZSTD_BLOCKSIZE_ABSOLUTEMAX+8;
|
446
|
-
dctx->litSize = litSize;
|
447
|
-
return lhSize+litSize;
|
448
|
-
}
|
449
|
-
/* direct reference into compressed stream */
|
450
|
-
dctx->litPtr = istart+lhSize;
|
451
|
-
dctx->litBufSize = srcSize-lhSize;
|
452
|
-
dctx->litSize = litSize;
|
453
|
-
return lhSize+litSize;
|
454
|
-
}
|
455
|
-
|
456
|
-
case set_rle:
|
457
|
-
{ U32 const lhlCode = ((istart[0]) >> 2) & 3;
|
458
|
-
size_t litSize, lhSize;
|
459
|
-
switch(lhlCode)
|
460
|
-
{
|
461
|
-
case 0: case 2: default: /* note : default is impossible, since lhlCode into [0..3] */
|
462
|
-
lhSize = 1;
|
463
|
-
litSize = istart[0] >> 3;
|
464
|
-
break;
|
465
|
-
case 1:
|
466
|
-
lhSize = 2;
|
467
|
-
litSize = MEM_readLE16(istart) >> 4;
|
468
|
-
break;
|
469
|
-
case 3:
|
470
|
-
lhSize = 3;
|
471
|
-
litSize = MEM_readLE24(istart) >> 4;
|
472
|
-
if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
|
473
|
-
break;
|
474
|
-
}
|
475
|
-
if (litSize > ZSTD_BLOCKSIZE_ABSOLUTEMAX) return ERROR(corruption_detected);
|
476
|
-
memset(dctx->litBuffer, istart[lhSize], litSize);
|
477
|
-
dctx->litPtr = dctx->litBuffer;
|
478
|
-
dctx->litBufSize = ZSTD_BLOCKSIZE_ABSOLUTEMAX+WILDCOPY_OVERLENGTH;
|
479
|
-
dctx->litSize = litSize;
|
480
|
-
return lhSize+1;
|
481
|
-
}
|
482
|
-
default:
|
483
|
-
return ERROR(corruption_detected); /* impossible */
|
484
|
-
}
|
485
|
-
}
|
486
|
-
}
|
487
|
-
|
488
|
-
|
489
|
-
typedef union {
|
490
|
-
FSE_decode_t realData;
|
491
|
-
U32 alignedBy4;
|
492
|
-
} FSE_decode_t4;
|
493
|
-
|
494
|
-
static const FSE_decode_t4 LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = {
|
495
|
-
{ { LL_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */
|
496
|
-
{ { 0, 0, 4 } }, /* 0 : base, symbol, bits */
|
497
|
-
{ { 16, 0, 4 } },
|
498
|
-
{ { 32, 1, 5 } },
|
499
|
-
{ { 0, 3, 5 } },
|
500
|
-
{ { 0, 4, 5 } },
|
501
|
-
{ { 0, 6, 5 } },
|
502
|
-
{ { 0, 7, 5 } },
|
503
|
-
{ { 0, 9, 5 } },
|
504
|
-
{ { 0, 10, 5 } },
|
505
|
-
{ { 0, 12, 5 } },
|
506
|
-
{ { 0, 14, 6 } },
|
507
|
-
{ { 0, 16, 5 } },
|
508
|
-
{ { 0, 18, 5 } },
|
509
|
-
{ { 0, 19, 5 } },
|
510
|
-
{ { 0, 21, 5 } },
|
511
|
-
{ { 0, 22, 5 } },
|
512
|
-
{ { 0, 24, 5 } },
|
513
|
-
{ { 32, 25, 5 } },
|
514
|
-
{ { 0, 26, 5 } },
|
515
|
-
{ { 0, 27, 6 } },
|
516
|
-
{ { 0, 29, 6 } },
|
517
|
-
{ { 0, 31, 6 } },
|
518
|
-
{ { 32, 0, 4 } },
|
519
|
-
{ { 0, 1, 4 } },
|
520
|
-
{ { 0, 2, 5 } },
|
521
|
-
{ { 32, 4, 5 } },
|
522
|
-
{ { 0, 5, 5 } },
|
523
|
-
{ { 32, 7, 5 } },
|
524
|
-
{ { 0, 8, 5 } },
|
525
|
-
{ { 32, 10, 5 } },
|
526
|
-
{ { 0, 11, 5 } },
|
527
|
-
{ { 0, 13, 6 } },
|
528
|
-
{ { 32, 16, 5 } },
|
529
|
-
{ { 0, 17, 5 } },
|
530
|
-
{ { 32, 19, 5 } },
|
531
|
-
{ { 0, 20, 5 } },
|
532
|
-
{ { 32, 22, 5 } },
|
533
|
-
{ { 0, 23, 5 } },
|
534
|
-
{ { 0, 25, 4 } },
|
535
|
-
{ { 16, 25, 4 } },
|
536
|
-
{ { 32, 26, 5 } },
|
537
|
-
{ { 0, 28, 6 } },
|
538
|
-
{ { 0, 30, 6 } },
|
539
|
-
{ { 48, 0, 4 } },
|
540
|
-
{ { 16, 1, 4 } },
|
541
|
-
{ { 32, 2, 5 } },
|
542
|
-
{ { 32, 3, 5 } },
|
543
|
-
{ { 32, 5, 5 } },
|
544
|
-
{ { 32, 6, 5 } },
|
545
|
-
{ { 32, 8, 5 } },
|
546
|
-
{ { 32, 9, 5 } },
|
547
|
-
{ { 32, 11, 5 } },
|
548
|
-
{ { 32, 12, 5 } },
|
549
|
-
{ { 0, 15, 6 } },
|
550
|
-
{ { 32, 17, 5 } },
|
551
|
-
{ { 32, 18, 5 } },
|
552
|
-
{ { 32, 20, 5 } },
|
553
|
-
{ { 32, 21, 5 } },
|
554
|
-
{ { 32, 23, 5 } },
|
555
|
-
{ { 32, 24, 5 } },
|
556
|
-
{ { 0, 35, 6 } },
|
557
|
-
{ { 0, 34, 6 } },
|
558
|
-
{ { 0, 33, 6 } },
|
559
|
-
{ { 0, 32, 6 } },
|
560
|
-
}; /* LL_defaultDTable */
|
561
|
-
|
562
|
-
static const FSE_decode_t4 ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = {
|
563
|
-
{ { ML_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */
|
564
|
-
{ { 0, 0, 6 } }, /* 0 : base, symbol, bits */
|
565
|
-
{ { 0, 1, 4 } },
|
566
|
-
{ { 32, 2, 5 } },
|
567
|
-
{ { 0, 3, 5 } },
|
568
|
-
{ { 0, 5, 5 } },
|
569
|
-
{ { 0, 6, 5 } },
|
570
|
-
{ { 0, 8, 5 } },
|
571
|
-
{ { 0, 10, 6 } },
|
572
|
-
{ { 0, 13, 6 } },
|
573
|
-
{ { 0, 16, 6 } },
|
574
|
-
{ { 0, 19, 6 } },
|
575
|
-
{ { 0, 22, 6 } },
|
576
|
-
{ { 0, 25, 6 } },
|
577
|
-
{ { 0, 28, 6 } },
|
578
|
-
{ { 0, 31, 6 } },
|
579
|
-
{ { 0, 33, 6 } },
|
580
|
-
{ { 0, 35, 6 } },
|
581
|
-
{ { 0, 37, 6 } },
|
582
|
-
{ { 0, 39, 6 } },
|
583
|
-
{ { 0, 41, 6 } },
|
584
|
-
{ { 0, 43, 6 } },
|
585
|
-
{ { 0, 45, 6 } },
|
586
|
-
{ { 16, 1, 4 } },
|
587
|
-
{ { 0, 2, 4 } },
|
588
|
-
{ { 32, 3, 5 } },
|
589
|
-
{ { 0, 4, 5 } },
|
590
|
-
{ { 32, 6, 5 } },
|
591
|
-
{ { 0, 7, 5 } },
|
592
|
-
{ { 0, 9, 6 } },
|
593
|
-
{ { 0, 12, 6 } },
|
594
|
-
{ { 0, 15, 6 } },
|
595
|
-
{ { 0, 18, 6 } },
|
596
|
-
{ { 0, 21, 6 } },
|
597
|
-
{ { 0, 24, 6 } },
|
598
|
-
{ { 0, 27, 6 } },
|
599
|
-
{ { 0, 30, 6 } },
|
600
|
-
{ { 0, 32, 6 } },
|
601
|
-
{ { 0, 34, 6 } },
|
602
|
-
{ { 0, 36, 6 } },
|
603
|
-
{ { 0, 38, 6 } },
|
604
|
-
{ { 0, 40, 6 } },
|
605
|
-
{ { 0, 42, 6 } },
|
606
|
-
{ { 0, 44, 6 } },
|
607
|
-
{ { 32, 1, 4 } },
|
608
|
-
{ { 48, 1, 4 } },
|
609
|
-
{ { 16, 2, 4 } },
|
610
|
-
{ { 32, 4, 5 } },
|
611
|
-
{ { 32, 5, 5 } },
|
612
|
-
{ { 32, 7, 5 } },
|
613
|
-
{ { 32, 8, 5 } },
|
614
|
-
{ { 0, 11, 6 } },
|
615
|
-
{ { 0, 14, 6 } },
|
616
|
-
{ { 0, 17, 6 } },
|
617
|
-
{ { 0, 20, 6 } },
|
618
|
-
{ { 0, 23, 6 } },
|
619
|
-
{ { 0, 26, 6 } },
|
620
|
-
{ { 0, 29, 6 } },
|
621
|
-
{ { 0, 52, 6 } },
|
622
|
-
{ { 0, 51, 6 } },
|
623
|
-
{ { 0, 50, 6 } },
|
624
|
-
{ { 0, 49, 6 } },
|
625
|
-
{ { 0, 48, 6 } },
|
626
|
-
{ { 0, 47, 6 } },
|
627
|
-
{ { 0, 46, 6 } },
|
628
|
-
}; /* ML_defaultDTable */
|
629
|
-
|
630
|
-
static const FSE_decode_t4 OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = {
|
631
|
-
{ { OF_DEFAULTNORMLOG, 1, 1 } }, /* header : tableLog, fastMode, fastMode */
|
632
|
-
{ { 0, 0, 5 } }, /* 0 : base, symbol, bits */
|
633
|
-
{ { 0, 6, 4 } },
|
634
|
-
{ { 0, 9, 5 } },
|
635
|
-
{ { 0, 15, 5 } },
|
636
|
-
{ { 0, 21, 5 } },
|
637
|
-
{ { 0, 3, 5 } },
|
638
|
-
{ { 0, 7, 4 } },
|
639
|
-
{ { 0, 12, 5 } },
|
640
|
-
{ { 0, 18, 5 } },
|
641
|
-
{ { 0, 23, 5 } },
|
642
|
-
{ { 0, 5, 5 } },
|
643
|
-
{ { 0, 8, 4 } },
|
644
|
-
{ { 0, 14, 5 } },
|
645
|
-
{ { 0, 20, 5 } },
|
646
|
-
{ { 0, 2, 5 } },
|
647
|
-
{ { 16, 7, 4 } },
|
648
|
-
{ { 0, 11, 5 } },
|
649
|
-
{ { 0, 17, 5 } },
|
650
|
-
{ { 0, 22, 5 } },
|
651
|
-
{ { 0, 4, 5 } },
|
652
|
-
{ { 16, 8, 4 } },
|
653
|
-
{ { 0, 13, 5 } },
|
654
|
-
{ { 0, 19, 5 } },
|
655
|
-
{ { 0, 1, 5 } },
|
656
|
-
{ { 16, 6, 4 } },
|
657
|
-
{ { 0, 10, 5 } },
|
658
|
-
{ { 0, 16, 5 } },
|
659
|
-
{ { 0, 28, 5 } },
|
660
|
-
{ { 0, 27, 5 } },
|
661
|
-
{ { 0, 26, 5 } },
|
662
|
-
{ { 0, 25, 5 } },
|
663
|
-
{ { 0, 24, 5 } },
|
664
|
-
}; /* OF_defaultDTable */
|
665
|
-
|
666
|
-
/*! ZSTD_buildSeqTable() :
|
667
|
-
@return : nb bytes read from src,
|
668
|
-
or an error code if it fails, testable with ZSTD_isError()
|
669
|
-
*/
|
670
|
-
static size_t ZSTD_buildSeqTable(FSE_DTable* DTableSpace, const FSE_DTable** DTablePtr,
|
671
|
-
symbolEncodingType_e type, U32 max, U32 maxLog,
|
672
|
-
const void* src, size_t srcSize,
|
673
|
-
const FSE_decode_t4* defaultTable, U32 flagRepeatTable)
|
674
|
-
{
|
675
|
-
const void* const tmpPtr = defaultTable; /* bypass strict aliasing */
|
676
|
-
switch(type)
|
677
|
-
{
|
678
|
-
case set_rle :
|
679
|
-
if (!srcSize) return ERROR(srcSize_wrong);
|
680
|
-
if ( (*(const BYTE*)src) > max) return ERROR(corruption_detected);
|
681
|
-
FSE_buildDTable_rle(DTableSpace, *(const BYTE*)src);
|
682
|
-
*DTablePtr = DTableSpace;
|
683
|
-
return 1;
|
684
|
-
case set_basic :
|
685
|
-
*DTablePtr = (const FSE_DTable*)tmpPtr;
|
686
|
-
return 0;
|
687
|
-
case set_repeat:
|
688
|
-
if (!flagRepeatTable) return ERROR(corruption_detected);
|
689
|
-
return 0;
|
690
|
-
default : /* impossible */
|
691
|
-
case set_compressed :
|
692
|
-
{ U32 tableLog;
|
693
|
-
S16 norm[MaxSeq+1];
|
694
|
-
size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);
|
695
|
-
if (FSE_isError(headerSize)) return ERROR(corruption_detected);
|
696
|
-
if (tableLog > maxLog) return ERROR(corruption_detected);
|
697
|
-
FSE_buildDTable(DTableSpace, norm, max, tableLog);
|
698
|
-
*DTablePtr = DTableSpace;
|
699
|
-
return headerSize;
|
700
|
-
} }
|
701
|
-
}
|
702
|
-
|
703
|
-
size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,
|
704
|
-
const void* src, size_t srcSize)
|
705
|
-
{
|
706
|
-
const BYTE* const istart = (const BYTE* const)src;
|
707
|
-
const BYTE* const iend = istart + srcSize;
|
708
|
-
const BYTE* ip = istart;
|
709
|
-
|
710
|
-
/* check */
|
711
|
-
if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong);
|
712
|
-
|
713
|
-
/* SeqHead */
|
714
|
-
{ int nbSeq = *ip++;
|
715
|
-
if (!nbSeq) { *nbSeqPtr=0; return 1; }
|
716
|
-
if (nbSeq > 0x7F) {
|
717
|
-
if (nbSeq == 0xFF) {
|
718
|
-
if (ip+2 > iend) return ERROR(srcSize_wrong);
|
719
|
-
nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
|
720
|
-
} else {
|
721
|
-
if (ip >= iend) return ERROR(srcSize_wrong);
|
722
|
-
nbSeq = ((nbSeq-0x80)<<8) + *ip++;
|
723
|
-
}
|
724
|
-
}
|
725
|
-
*nbSeqPtr = nbSeq;
|
726
|
-
}
|
727
|
-
|
728
|
-
/* FSE table descriptors */
|
729
|
-
if (ip+4 > iend) return ERROR(srcSize_wrong); /* minimum possible size */
|
730
|
-
{ symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);
|
731
|
-
symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);
|
732
|
-
symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3);
|
733
|
-
ip++;
|
734
|
-
|
735
|
-
/* Build DTables */
|
736
|
-
{ size_t const llhSize = ZSTD_buildSeqTable(dctx->LLTable, &dctx->LLTptr,
|
737
|
-
LLtype, MaxLL, LLFSELog,
|
738
|
-
ip, iend-ip, LL_defaultDTable, dctx->fseEntropy);
|
739
|
-
if (ZSTD_isError(llhSize)) return ERROR(corruption_detected);
|
740
|
-
ip += llhSize;
|
741
|
-
}
|
742
|
-
{ size_t const ofhSize = ZSTD_buildSeqTable(dctx->OFTable, &dctx->OFTptr,
|
743
|
-
OFtype, MaxOff, OffFSELog,
|
744
|
-
ip, iend-ip, OF_defaultDTable, dctx->fseEntropy);
|
745
|
-
if (ZSTD_isError(ofhSize)) return ERROR(corruption_detected);
|
746
|
-
ip += ofhSize;
|
747
|
-
}
|
748
|
-
{ size_t const mlhSize = ZSTD_buildSeqTable(dctx->MLTable, &dctx->MLTptr,
|
749
|
-
MLtype, MaxML, MLFSELog,
|
750
|
-
ip, iend-ip, ML_defaultDTable, dctx->fseEntropy);
|
751
|
-
if (ZSTD_isError(mlhSize)) return ERROR(corruption_detected);
|
752
|
-
ip += mlhSize;
|
753
|
-
}
|
754
|
-
}
|
755
|
-
|
756
|
-
return ip-istart;
|
757
|
-
}
|
758
|
-
|
759
|
-
|
760
|
-
typedef struct {
|
761
|
-
size_t litLength;
|
762
|
-
size_t matchLength;
|
763
|
-
size_t offset;
|
764
|
-
} seq_t;
|
765
|
-
|
766
|
-
typedef struct {
|
767
|
-
BIT_DStream_t DStream;
|
768
|
-
FSE_DState_t stateLL;
|
769
|
-
FSE_DState_t stateOffb;
|
770
|
-
FSE_DState_t stateML;
|
771
|
-
size_t prevOffset[ZSTD_REP_NUM];
|
772
|
-
} seqState_t;
|
773
|
-
|
774
|
-
|
775
|
-
static seq_t ZSTD_decodeSequence(seqState_t* seqState)
|
776
|
-
{
|
777
|
-
seq_t seq;
|
778
|
-
|
779
|
-
U32 const llCode = FSE_peekSymbol(&seqState->stateLL);
|
780
|
-
U32 const mlCode = FSE_peekSymbol(&seqState->stateML);
|
781
|
-
U32 const ofCode = FSE_peekSymbol(&seqState->stateOffb); /* <= maxOff, by table construction */
|
782
|
-
|
783
|
-
U32 const llBits = LL_bits[llCode];
|
784
|
-
U32 const mlBits = ML_bits[mlCode];
|
785
|
-
U32 const ofBits = ofCode;
|
786
|
-
U32 const totalBits = llBits+mlBits+ofBits;
|
787
|
-
|
788
|
-
static const U32 LL_base[MaxLL+1] = {
|
789
|
-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
790
|
-
16, 18, 20, 22, 24, 28, 32, 40, 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
|
791
|
-
0x2000, 0x4000, 0x8000, 0x10000 };
|
792
|
-
|
793
|
-
static const U32 ML_base[MaxML+1] = {
|
794
|
-
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
795
|
-
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
|
796
|
-
35, 37, 39, 41, 43, 47, 51, 59, 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
|
797
|
-
0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };
|
798
|
-
|
799
|
-
static const U32 OF_base[MaxOff+1] = {
|
800
|
-
0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D,
|
801
|
-
0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD,
|
802
|
-
0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,
|
803
|
-
0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD };
|
804
|
-
|
805
|
-
/* sequence */
|
806
|
-
{ size_t offset;
|
807
|
-
if (!ofCode)
|
808
|
-
offset = 0;
|
809
|
-
else {
|
810
|
-
offset = OF_base[ofCode] + BIT_readBits(&seqState->DStream, ofBits); /* <= (ZSTD_WINDOWLOG_MAX-1) bits */
|
811
|
-
if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);
|
812
|
-
}
|
813
|
-
|
814
|
-
if (ofCode <= 1) {
|
815
|
-
offset += (llCode==0);
|
816
|
-
if (offset) {
|
817
|
-
size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];
|
818
|
-
temp += !temp; /* 0 is not valid; input is corrupted; force offset to 1 */
|
819
|
-
if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
|
820
|
-
seqState->prevOffset[1] = seqState->prevOffset[0];
|
821
|
-
seqState->prevOffset[0] = offset = temp;
|
822
|
-
} else {
|
823
|
-
offset = seqState->prevOffset[0];
|
824
|
-
}
|
825
|
-
} else {
|
826
|
-
seqState->prevOffset[2] = seqState->prevOffset[1];
|
827
|
-
seqState->prevOffset[1] = seqState->prevOffset[0];
|
828
|
-
seqState->prevOffset[0] = offset;
|
829
|
-
}
|
830
|
-
seq.offset = offset;
|
831
|
-
}
|
832
|
-
|
833
|
-
seq.matchLength = ML_base[mlCode] + ((mlCode>31) ? BIT_readBits(&seqState->DStream, mlBits) : 0); /* <= 16 bits */
|
834
|
-
if (MEM_32bits() && (mlBits+llBits>24)) BIT_reloadDStream(&seqState->DStream);
|
835
|
-
|
836
|
-
seq.litLength = LL_base[llCode] + ((llCode>15) ? BIT_readBits(&seqState->DStream, llBits) : 0); /* <= 16 bits */
|
837
|
-
if (MEM_32bits() ||
|
838
|
-
(totalBits > 64 - 7 - (LLFSELog+MLFSELog+OffFSELog)) ) BIT_reloadDStream(&seqState->DStream);
|
839
|
-
|
840
|
-
/* ANS state update */
|
841
|
-
FSE_updateState(&seqState->stateLL, &seqState->DStream); /* <= 9 bits */
|
842
|
-
FSE_updateState(&seqState->stateML, &seqState->DStream); /* <= 9 bits */
|
843
|
-
if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream); /* <= 18 bits */
|
844
|
-
FSE_updateState(&seqState->stateOffb, &seqState->DStream); /* <= 8 bits */
|
845
|
-
|
846
|
-
return seq;
|
847
|
-
}
|
848
|
-
|
849
|
-
|
850
|
-
FORCE_NOINLINE
|
851
|
-
size_t ZSTD_execSequenceLast7(BYTE* op,
|
852
|
-
BYTE* const oend, seq_t sequence,
|
853
|
-
const BYTE** litPtr, const BYTE* const litLimit_w,
|
854
|
-
const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
|
855
|
-
{
|
856
|
-
BYTE* const oLitEnd = op + sequence.litLength;
|
857
|
-
size_t const sequenceLength = sequence.litLength + sequence.matchLength;
|
858
|
-
BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
|
859
|
-
BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
|
860
|
-
const BYTE* const iLitEnd = *litPtr + sequence.litLength;
|
861
|
-
const BYTE* match = oLitEnd - sequence.offset;
|
862
|
-
|
863
|
-
/* check */
|
864
|
-
if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
|
865
|
-
if (iLitEnd > litLimit_w) return ERROR(corruption_detected); /* over-read beyond lit buffer */
|
866
|
-
if (oLitEnd <= oend_w) return ERROR(GENERIC); /* Precondition */
|
867
|
-
|
868
|
-
/* copy literals */
|
869
|
-
if (op < oend_w) {
|
870
|
-
ZSTD_wildcopy(op, *litPtr, oend_w - op);
|
871
|
-
*litPtr += oend_w - op;
|
872
|
-
op = oend_w;
|
873
|
-
}
|
874
|
-
while (op < oLitEnd) *op++ = *(*litPtr)++;
|
875
|
-
|
876
|
-
/* copy Match */
|
877
|
-
if (sequence.offset > (size_t)(oLitEnd - base)) {
|
878
|
-
/* offset beyond prefix */
|
879
|
-
if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected);
|
880
|
-
match = dictEnd - (base-match);
|
881
|
-
if (match + sequence.matchLength <= dictEnd) {
|
882
|
-
memmove(oLitEnd, match, sequence.matchLength);
|
883
|
-
return sequenceLength;
|
884
|
-
}
|
885
|
-
/* span extDict & currentPrefixSegment */
|
886
|
-
{ size_t const length1 = dictEnd - match;
|
887
|
-
memmove(oLitEnd, match, length1);
|
888
|
-
op = oLitEnd + length1;
|
889
|
-
sequence.matchLength -= length1;
|
890
|
-
match = base;
|
891
|
-
} }
|
892
|
-
while (op < oMatchEnd) *op++ = *match++;
|
893
|
-
return sequenceLength;
|
894
|
-
}
|
895
|
-
|
896
|
-
|
897
|
-
FORCE_INLINE
|
898
|
-
size_t ZSTD_execSequence(BYTE* op,
|
899
|
-
BYTE* const oend, seq_t sequence,
|
900
|
-
const BYTE** litPtr, const BYTE* const litLimit_w,
|
901
|
-
const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
|
902
|
-
{
|
903
|
-
BYTE* const oLitEnd = op + sequence.litLength;
|
904
|
-
size_t const sequenceLength = sequence.litLength + sequence.matchLength;
|
905
|
-
BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
|
906
|
-
BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;
|
907
|
-
const BYTE* const iLitEnd = *litPtr + sequence.litLength;
|
908
|
-
const BYTE* match = oLitEnd - sequence.offset;
|
909
|
-
|
910
|
-
/* check */
|
911
|
-
if (oMatchEnd>oend) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
|
912
|
-
if (iLitEnd > litLimit_w) return ERROR(corruption_detected); /* over-read beyond lit buffer */
|
913
|
-
if (oLitEnd>oend_w) return ZSTD_execSequenceLast7(op, oend, sequence, litPtr, litLimit_w, base, vBase, dictEnd);
|
914
|
-
|
915
|
-
/* copy Literals */
|
916
|
-
ZSTD_copy8(op, *litPtr);
|
917
|
-
if (sequence.litLength > 8)
|
918
|
-
ZSTD_wildcopy(op+8, (*litPtr)+8, sequence.litLength - 8); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
|
919
|
-
op = oLitEnd;
|
920
|
-
*litPtr = iLitEnd; /* update for next sequence */
|
921
|
-
|
922
|
-
/* copy Match */
|
923
|
-
if (sequence.offset > (size_t)(oLitEnd - base)) {
|
924
|
-
/* offset beyond prefix */
|
925
|
-
if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected);
|
926
|
-
match = dictEnd - (base-match);
|
927
|
-
if (match + sequence.matchLength <= dictEnd) {
|
928
|
-
memmove(oLitEnd, match, sequence.matchLength);
|
929
|
-
return sequenceLength;
|
930
|
-
}
|
931
|
-
/* span extDict & currentPrefixSegment */
|
932
|
-
{ size_t const length1 = dictEnd - match;
|
933
|
-
memmove(oLitEnd, match, length1);
|
934
|
-
op = oLitEnd + length1;
|
935
|
-
sequence.matchLength -= length1;
|
936
|
-
match = base;
|
937
|
-
if (op > oend_w) {
|
938
|
-
U32 i;
|
939
|
-
for (i = 0; i < sequence.matchLength; ++i) op[i] = match[i];
|
940
|
-
return sequenceLength;
|
941
|
-
}
|
942
|
-
} }
|
943
|
-
/* Requirement: op <= oend_w */
|
944
|
-
|
945
|
-
/* match within prefix */
|
946
|
-
if (sequence.offset < 8) {
|
947
|
-
/* close range match, overlap */
|
948
|
-
static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
|
949
|
-
static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */
|
950
|
-
int const sub2 = dec64table[sequence.offset];
|
951
|
-
op[0] = match[0];
|
952
|
-
op[1] = match[1];
|
953
|
-
op[2] = match[2];
|
954
|
-
op[3] = match[3];
|
955
|
-
match += dec32table[sequence.offset];
|
956
|
-
ZSTD_copy4(op+4, match);
|
957
|
-
match -= sub2;
|
958
|
-
} else {
|
959
|
-
ZSTD_copy8(op, match);
|
960
|
-
}
|
961
|
-
op += 8; match += 8;
|
962
|
-
|
963
|
-
if (oMatchEnd > oend-(16-MINMATCH)) {
|
964
|
-
if (op < oend_w) {
|
965
|
-
ZSTD_wildcopy(op, match, oend_w - op);
|
966
|
-
match += oend_w - op;
|
967
|
-
op = oend_w;
|
968
|
-
}
|
969
|
-
while (op < oMatchEnd) *op++ = *match++;
|
970
|
-
} else {
|
971
|
-
ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
|
972
|
-
}
|
973
|
-
return sequenceLength;
|
974
|
-
}
|
975
|
-
|
976
|
-
|
977
|
-
static size_t ZSTD_decompressSequences(
|
978
|
-
ZSTD_DCtx* dctx,
|
979
|
-
void* dst, size_t maxDstSize,
|
980
|
-
const void* seqStart, size_t seqSize)
|
981
|
-
{
|
982
|
-
const BYTE* ip = (const BYTE*)seqStart;
|
983
|
-
const BYTE* const iend = ip + seqSize;
|
984
|
-
BYTE* const ostart = (BYTE* const)dst;
|
985
|
-
BYTE* const oend = ostart + maxDstSize;
|
986
|
-
BYTE* op = ostart;
|
987
|
-
const BYTE* litPtr = dctx->litPtr;
|
988
|
-
const BYTE* const litLimit_w = litPtr + dctx->litBufSize - WILDCOPY_OVERLENGTH;
|
989
|
-
const BYTE* const litEnd = litPtr + dctx->litSize;
|
990
|
-
const BYTE* const base = (const BYTE*) (dctx->base);
|
991
|
-
const BYTE* const vBase = (const BYTE*) (dctx->vBase);
|
992
|
-
const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
|
993
|
-
int nbSeq;
|
994
|
-
|
995
|
-
/* Build Decoding Tables */
|
996
|
-
{ size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, seqSize);
|
997
|
-
if (ZSTD_isError(seqHSize)) return seqHSize;
|
998
|
-
ip += seqHSize;
|
999
|
-
}
|
1000
|
-
|
1001
|
-
/* Regen sequences */
|
1002
|
-
if (nbSeq) {
|
1003
|
-
seqState_t seqState;
|
1004
|
-
dctx->fseEntropy = 1;
|
1005
|
-
{ U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->rep[i]; }
|
1006
|
-
CHECK_E(BIT_initDStream(&seqState.DStream, ip, iend-ip), corruption_detected);
|
1007
|
-
FSE_initDState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);
|
1008
|
-
FSE_initDState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);
|
1009
|
-
FSE_initDState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);
|
1010
|
-
|
1011
|
-
for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) {
|
1012
|
-
nbSeq--;
|
1013
|
-
{ seq_t const sequence = ZSTD_decodeSequence(&seqState);
|
1014
|
-
size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litLimit_w, base, vBase, dictEnd);
|
1015
|
-
if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
|
1016
|
-
op += oneSeqSize;
|
1017
|
-
} }
|
1018
|
-
|
1019
|
-
/* check if reached exact end */
|
1020
|
-
if (nbSeq) return ERROR(corruption_detected);
|
1021
|
-
/* save reps for next block */
|
1022
|
-
{ U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->rep[i] = (U32)(seqState.prevOffset[i]); }
|
1023
|
-
}
|
1024
|
-
|
1025
|
-
/* last literal segment */
|
1026
|
-
{ size_t const lastLLSize = litEnd - litPtr;
|
1027
|
-
if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall);
|
1028
|
-
memcpy(op, litPtr, lastLLSize);
|
1029
|
-
op += lastLLSize;
|
1030
|
-
}
|
1031
|
-
|
1032
|
-
return op-ostart;
|
1033
|
-
}
|
1034
|
-
|
1035
|
-
|
1036
|
-
static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)
|
1037
|
-
{
|
1038
|
-
if (dst != dctx->previousDstEnd) { /* not contiguous */
|
1039
|
-
dctx->dictEnd = dctx->previousDstEnd;
|
1040
|
-
dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
|
1041
|
-
dctx->base = dst;
|
1042
|
-
dctx->previousDstEnd = dst;
|
1043
|
-
}
|
1044
|
-
}
|
1045
|
-
|
1046
|
-
|
1047
|
-
static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
|
1048
|
-
void* dst, size_t dstCapacity,
|
1049
|
-
const void* src, size_t srcSize)
|
1050
|
-
{ /* blockType == blockCompressed */
|
1051
|
-
const BYTE* ip = (const BYTE*)src;
|
1052
|
-
|
1053
|
-
if (srcSize >= ZSTD_BLOCKSIZE_ABSOLUTEMAX) return ERROR(srcSize_wrong);
|
1054
|
-
|
1055
|
-
/* Decode literals sub-block */
|
1056
|
-
{ size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
|
1057
|
-
if (ZSTD_isError(litCSize)) return litCSize;
|
1058
|
-
ip += litCSize;
|
1059
|
-
srcSize -= litCSize;
|
1060
|
-
}
|
1061
|
-
return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize);
|
1062
|
-
}
|
1063
|
-
|
1064
|
-
|
1065
|
-
size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,
|
1066
|
-
void* dst, size_t dstCapacity,
|
1067
|
-
const void* src, size_t srcSize)
|
1068
|
-
{
|
1069
|
-
size_t dSize;
|
1070
|
-
ZSTD_checkContinuity(dctx, dst);
|
1071
|
-
dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
|
1072
|
-
dctx->previousDstEnd = (char*)dst + dSize;
|
1073
|
-
return dSize;
|
1074
|
-
}
|
1075
|
-
|
1076
|
-
|
1077
|
-
/** ZSTD_insertBlock() :
|
1078
|
-
insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
|
1079
|
-
ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)
|
1080
|
-
{
|
1081
|
-
ZSTD_checkContinuity(dctx, blockStart);
|
1082
|
-
dctx->previousDstEnd = (const char*)blockStart + blockSize;
|
1083
|
-
return blockSize;
|
1084
|
-
}
|
1085
|
-
|
1086
|
-
|
1087
|
-
size_t ZSTD_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length)
|
1088
|
-
{
|
1089
|
-
if (length > dstCapacity) return ERROR(dstSize_tooSmall);
|
1090
|
-
memset(dst, byte, length);
|
1091
|
-
return length;
|
1092
|
-
}
|
1093
|
-
|
1094
|
-
|
1095
|
-
/*! ZSTD_decompressFrame() :
|
1096
|
-
* `dctx` must be properly initialized */
|
1097
|
-
static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
|
1098
|
-
void* dst, size_t dstCapacity,
|
1099
|
-
const void* src, size_t srcSize)
|
1100
|
-
{
|
1101
|
-
const BYTE* ip = (const BYTE*)src;
|
1102
|
-
BYTE* const ostart = (BYTE* const)dst;
|
1103
|
-
BYTE* const oend = ostart + dstCapacity;
|
1104
|
-
BYTE* op = ostart;
|
1105
|
-
size_t remainingSize = srcSize;
|
1106
|
-
|
1107
|
-
/* check */
|
1108
|
-
if (srcSize < ZSTD_frameHeaderSize_min+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
|
1109
|
-
|
1110
|
-
/* Frame Header */
|
1111
|
-
{ size_t const frameHeaderSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_prefix);
|
1112
|
-
if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;
|
1113
|
-
if (srcSize < frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
|
1114
|
-
CHECK_F(ZSTD_decodeFrameHeader(dctx, src, frameHeaderSize));
|
1115
|
-
ip += frameHeaderSize; remainingSize -= frameHeaderSize;
|
1116
|
-
}
|
1117
|
-
|
1118
|
-
/* Loop on each block */
|
1119
|
-
while (1) {
|
1120
|
-
size_t decodedSize;
|
1121
|
-
blockProperties_t blockProperties;
|
1122
|
-
size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
|
1123
|
-
if (ZSTD_isError(cBlockSize)) return cBlockSize;
|
1124
|
-
|
1125
|
-
ip += ZSTD_blockHeaderSize;
|
1126
|
-
remainingSize -= ZSTD_blockHeaderSize;
|
1127
|
-
if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
|
1128
|
-
|
1129
|
-
switch(blockProperties.blockType)
|
1130
|
-
{
|
1131
|
-
case bt_compressed:
|
1132
|
-
decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize);
|
1133
|
-
break;
|
1134
|
-
case bt_raw :
|
1135
|
-
decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);
|
1136
|
-
break;
|
1137
|
-
case bt_rle :
|
1138
|
-
decodedSize = ZSTD_generateNxBytes(op, oend-op, *ip, blockProperties.origSize);
|
1139
|
-
break;
|
1140
|
-
case bt_reserved :
|
1141
|
-
default:
|
1142
|
-
return ERROR(corruption_detected);
|
1143
|
-
}
|
1144
|
-
|
1145
|
-
if (ZSTD_isError(decodedSize)) return decodedSize;
|
1146
|
-
if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, op, decodedSize);
|
1147
|
-
op += decodedSize;
|
1148
|
-
ip += cBlockSize;
|
1149
|
-
remainingSize -= cBlockSize;
|
1150
|
-
if (blockProperties.lastBlock) break;
|
1151
|
-
}
|
1152
|
-
|
1153
|
-
if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
|
1154
|
-
U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);
|
1155
|
-
U32 checkRead;
|
1156
|
-
if (remainingSize<4) return ERROR(checksum_wrong);
|
1157
|
-
checkRead = MEM_readLE32(ip);
|
1158
|
-
if (checkRead != checkCalc) return ERROR(checksum_wrong);
|
1159
|
-
remainingSize -= 4;
|
1160
|
-
}
|
1161
|
-
|
1162
|
-
if (remainingSize) return ERROR(srcSize_wrong);
|
1163
|
-
return op-ostart;
|
1164
|
-
}
|
1165
|
-
|
1166
|
-
|
1167
|
-
size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
|
1168
|
-
void* dst, size_t dstCapacity,
|
1169
|
-
const void* src, size_t srcSize,
|
1170
|
-
const void* dict, size_t dictSize)
|
1171
|
-
{
|
1172
|
-
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
1173
|
-
if (ZSTD_isLegacy(src, srcSize)) return ZSTD_decompressLegacy(dst, dstCapacity, src, srcSize, dict, dictSize);
|
1174
|
-
#endif
|
1175
|
-
ZSTD_decompressBegin_usingDict(dctx, dict, dictSize);
|
1176
|
-
ZSTD_checkContinuity(dctx, dst);
|
1177
|
-
return ZSTD_decompressFrame(dctx, dst, dstCapacity, src, srcSize);
|
1178
|
-
}
|
1179
|
-
|
1180
|
-
|
1181
|
-
size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
|
1182
|
-
{
|
1183
|
-
return ZSTD_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0);
|
1184
|
-
}
|
1185
|
-
|
1186
|
-
|
1187
|
-
size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
|
1188
|
-
{
|
1189
|
-
#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1)
|
1190
|
-
size_t regenSize;
|
1191
|
-
ZSTD_DCtx* const dctx = ZSTD_createDCtx();
|
1192
|
-
if (dctx==NULL) return ERROR(memory_allocation);
|
1193
|
-
regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
|
1194
|
-
ZSTD_freeDCtx(dctx);
|
1195
|
-
return regenSize;
|
1196
|
-
#else /* stack mode */
|
1197
|
-
ZSTD_DCtx dctx;
|
1198
|
-
return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
|
1199
|
-
#endif
|
1200
|
-
}
|
1201
|
-
|
1202
|
-
|
1203
|
-
/*-**************************************
|
1204
|
-
* Advanced Streaming Decompression API
|
1205
|
-
* Bufferless and synchronous
|
1206
|
-
****************************************/
|
1207
|
-
size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }
|
1208
|
-
|
1209
|
-
ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
|
1210
|
-
switch(dctx->stage)
|
1211
|
-
{
|
1212
|
-
default: /* should not happen */
|
1213
|
-
case ZSTDds_getFrameHeaderSize:
|
1214
|
-
case ZSTDds_decodeFrameHeader:
|
1215
|
-
return ZSTDnit_frameHeader;
|
1216
|
-
case ZSTDds_decodeBlockHeader:
|
1217
|
-
return ZSTDnit_blockHeader;
|
1218
|
-
case ZSTDds_decompressBlock:
|
1219
|
-
return ZSTDnit_block;
|
1220
|
-
case ZSTDds_decompressLastBlock:
|
1221
|
-
return ZSTDnit_lastBlock;
|
1222
|
-
case ZSTDds_checkChecksum:
|
1223
|
-
return ZSTDnit_checksum;
|
1224
|
-
case ZSTDds_decodeSkippableHeader:
|
1225
|
-
case ZSTDds_skipFrame:
|
1226
|
-
return ZSTDnit_skippableFrame;
|
1227
|
-
}
|
1228
|
-
}
|
1229
|
-
|
1230
|
-
int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; } /* for zbuff */
|
1231
|
-
|
1232
|
-
/** ZSTD_decompressContinue() :
|
1233
|
-
* @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
|
1234
|
-
* or an error code, which can be tested using ZSTD_isError() */
|
1235
|
-
size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
|
1236
|
-
{
|
1237
|
-
/* Sanity check */
|
1238
|
-
if (srcSize != dctx->expected) return ERROR(srcSize_wrong);
|
1239
|
-
if (dstCapacity) ZSTD_checkContinuity(dctx, dst);
|
1240
|
-
|
1241
|
-
switch (dctx->stage)
|
1242
|
-
{
|
1243
|
-
case ZSTDds_getFrameHeaderSize :
|
1244
|
-
if (srcSize != ZSTD_frameHeaderSize_prefix) return ERROR(srcSize_wrong); /* impossible */
|
1245
|
-
if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
|
1246
|
-
memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_prefix);
|
1247
|
-
dctx->expected = ZSTD_skippableHeaderSize - ZSTD_frameHeaderSize_prefix; /* magic number + skippable frame length */
|
1248
|
-
dctx->stage = ZSTDds_decodeSkippableHeader;
|
1249
|
-
return 0;
|
1250
|
-
}
|
1251
|
-
dctx->headerSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_prefix);
|
1252
|
-
if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;
|
1253
|
-
memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_prefix);
|
1254
|
-
if (dctx->headerSize > ZSTD_frameHeaderSize_prefix) {
|
1255
|
-
dctx->expected = dctx->headerSize - ZSTD_frameHeaderSize_prefix;
|
1256
|
-
dctx->stage = ZSTDds_decodeFrameHeader;
|
1257
|
-
return 0;
|
1258
|
-
}
|
1259
|
-
dctx->expected = 0; /* not necessary to copy more */
|
1260
|
-
|
1261
|
-
case ZSTDds_decodeFrameHeader:
|
1262
|
-
memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_prefix, src, dctx->expected);
|
1263
|
-
CHECK_F(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize));
|
1264
|
-
dctx->expected = ZSTD_blockHeaderSize;
|
1265
|
-
dctx->stage = ZSTDds_decodeBlockHeader;
|
1266
|
-
return 0;
|
1267
|
-
|
1268
|
-
case ZSTDds_decodeBlockHeader:
|
1269
|
-
{ blockProperties_t bp;
|
1270
|
-
size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
|
1271
|
-
if (ZSTD_isError(cBlockSize)) return cBlockSize;
|
1272
|
-
dctx->expected = cBlockSize;
|
1273
|
-
dctx->bType = bp.blockType;
|
1274
|
-
dctx->rleSize = bp.origSize;
|
1275
|
-
if (cBlockSize) {
|
1276
|
-
dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
|
1277
|
-
return 0;
|
1278
|
-
}
|
1279
|
-
/* empty block */
|
1280
|
-
if (bp.lastBlock) {
|
1281
|
-
if (dctx->fParams.checksumFlag) {
|
1282
|
-
dctx->expected = 4;
|
1283
|
-
dctx->stage = ZSTDds_checkChecksum;
|
1284
|
-
} else {
|
1285
|
-
dctx->expected = 0; /* end of frame */
|
1286
|
-
dctx->stage = ZSTDds_getFrameHeaderSize;
|
1287
|
-
}
|
1288
|
-
} else {
|
1289
|
-
dctx->expected = 3; /* go directly to next header */
|
1290
|
-
dctx->stage = ZSTDds_decodeBlockHeader;
|
1291
|
-
}
|
1292
|
-
return 0;
|
1293
|
-
}
|
1294
|
-
case ZSTDds_decompressLastBlock:
|
1295
|
-
case ZSTDds_decompressBlock:
|
1296
|
-
{ size_t rSize;
|
1297
|
-
switch(dctx->bType)
|
1298
|
-
{
|
1299
|
-
case bt_compressed:
|
1300
|
-
rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
|
1301
|
-
break;
|
1302
|
-
case bt_raw :
|
1303
|
-
rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);
|
1304
|
-
break;
|
1305
|
-
case bt_rle :
|
1306
|
-
rSize = ZSTD_setRleBlock(dst, dstCapacity, src, srcSize, dctx->rleSize);
|
1307
|
-
break;
|
1308
|
-
case bt_reserved : /* should never happen */
|
1309
|
-
default:
|
1310
|
-
return ERROR(corruption_detected);
|
1311
|
-
}
|
1312
|
-
if (ZSTD_isError(rSize)) return rSize;
|
1313
|
-
if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);
|
1314
|
-
|
1315
|
-
if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */
|
1316
|
-
if (dctx->fParams.checksumFlag) { /* another round for frame checksum */
|
1317
|
-
dctx->expected = 4;
|
1318
|
-
dctx->stage = ZSTDds_checkChecksum;
|
1319
|
-
} else {
|
1320
|
-
dctx->expected = 0; /* ends here */
|
1321
|
-
dctx->stage = ZSTDds_getFrameHeaderSize;
|
1322
|
-
}
|
1323
|
-
} else {
|
1324
|
-
dctx->stage = ZSTDds_decodeBlockHeader;
|
1325
|
-
dctx->expected = ZSTD_blockHeaderSize;
|
1326
|
-
dctx->previousDstEnd = (char*)dst + rSize;
|
1327
|
-
}
|
1328
|
-
return rSize;
|
1329
|
-
}
|
1330
|
-
case ZSTDds_checkChecksum:
|
1331
|
-
{ U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);
|
1332
|
-
U32 const check32 = MEM_readLE32(src); /* srcSize == 4, guaranteed by dctx->expected */
|
1333
|
-
if (check32 != h32) return ERROR(checksum_wrong);
|
1334
|
-
dctx->expected = 0;
|
1335
|
-
dctx->stage = ZSTDds_getFrameHeaderSize;
|
1336
|
-
return 0;
|
1337
|
-
}
|
1338
|
-
case ZSTDds_decodeSkippableHeader:
|
1339
|
-
{ memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_prefix, src, dctx->expected);
|
1340
|
-
dctx->expected = MEM_readLE32(dctx->headerBuffer + 4);
|
1341
|
-
dctx->stage = ZSTDds_skipFrame;
|
1342
|
-
return 0;
|
1343
|
-
}
|
1344
|
-
case ZSTDds_skipFrame:
|
1345
|
-
{ dctx->expected = 0;
|
1346
|
-
dctx->stage = ZSTDds_getFrameHeaderSize;
|
1347
|
-
return 0;
|
1348
|
-
}
|
1349
|
-
default:
|
1350
|
-
return ERROR(GENERIC); /* impossible */
|
1351
|
-
}
|
1352
|
-
}
|
1353
|
-
|
1354
|
-
|
1355
|
-
static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
|
1356
|
-
{
|
1357
|
-
dctx->dictEnd = dctx->previousDstEnd;
|
1358
|
-
dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
|
1359
|
-
dctx->base = dict;
|
1360
|
-
dctx->previousDstEnd = (const char*)dict + dictSize;
|
1361
|
-
return 0;
|
1362
|
-
}
|
1363
|
-
|
1364
|
-
static size_t ZSTD_loadEntropy(ZSTD_DCtx* dctx, const void* const dict, size_t const dictSize)
|
1365
|
-
{
|
1366
|
-
const BYTE* dictPtr = (const BYTE*)dict;
|
1367
|
-
const BYTE* const dictEnd = dictPtr + dictSize;
|
1368
|
-
|
1369
|
-
{ size_t const hSize = HUF_readDTableX4(dctx->hufTable, dict, dictSize);
|
1370
|
-
if (HUF_isError(hSize)) return ERROR(dictionary_corrupted);
|
1371
|
-
dictPtr += hSize;
|
1372
|
-
}
|
1373
|
-
|
1374
|
-
{ short offcodeNCount[MaxOff+1];
|
1375
|
-
U32 offcodeMaxValue=MaxOff, offcodeLog;
|
1376
|
-
size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
|
1377
|
-
if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
|
1378
|
-
if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted);
|
1379
|
-
CHECK_E(FSE_buildDTable(dctx->OFTable, offcodeNCount, offcodeMaxValue, offcodeLog), dictionary_corrupted);
|
1380
|
-
dictPtr += offcodeHeaderSize;
|
1381
|
-
}
|
1382
|
-
|
1383
|
-
{ short matchlengthNCount[MaxML+1];
|
1384
|
-
unsigned matchlengthMaxValue = MaxML, matchlengthLog;
|
1385
|
-
size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
|
1386
|
-
if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
|
1387
|
-
if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted);
|
1388
|
-
CHECK_E(FSE_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog), dictionary_corrupted);
|
1389
|
-
dictPtr += matchlengthHeaderSize;
|
1390
|
-
}
|
1391
|
-
|
1392
|
-
{ short litlengthNCount[MaxLL+1];
|
1393
|
-
unsigned litlengthMaxValue = MaxLL, litlengthLog;
|
1394
|
-
size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
|
1395
|
-
if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
|
1396
|
-
if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted);
|
1397
|
-
CHECK_E(FSE_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog), dictionary_corrupted);
|
1398
|
-
dictPtr += litlengthHeaderSize;
|
1399
|
-
}
|
1400
|
-
|
1401
|
-
if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted);
|
1402
|
-
dctx->rep[0] = MEM_readLE32(dictPtr+0); if (dctx->rep[0] >= dictSize) return ERROR(dictionary_corrupted);
|
1403
|
-
dctx->rep[1] = MEM_readLE32(dictPtr+4); if (dctx->rep[1] >= dictSize) return ERROR(dictionary_corrupted);
|
1404
|
-
dctx->rep[2] = MEM_readLE32(dictPtr+8); if (dctx->rep[2] >= dictSize) return ERROR(dictionary_corrupted);
|
1405
|
-
dictPtr += 12;
|
1406
|
-
|
1407
|
-
dctx->litEntropy = dctx->fseEntropy = 1;
|
1408
|
-
return dictPtr - (const BYTE*)dict;
|
1409
|
-
}
|
1410
|
-
|
1411
|
-
static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
|
1412
|
-
{
|
1413
|
-
if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);
|
1414
|
-
{ U32 const magic = MEM_readLE32(dict);
|
1415
|
-
if (magic != ZSTD_DICT_MAGIC) {
|
1416
|
-
return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */
|
1417
|
-
} }
|
1418
|
-
dctx->dictID = MEM_readLE32((const char*)dict + 4);
|
1419
|
-
|
1420
|
-
/* load entropy tables */
|
1421
|
-
dict = (const char*)dict + 8;
|
1422
|
-
dictSize -= 8;
|
1423
|
-
{ size_t const eSize = ZSTD_loadEntropy(dctx, dict, dictSize);
|
1424
|
-
if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted);
|
1425
|
-
dict = (const char*)dict + eSize;
|
1426
|
-
dictSize -= eSize;
|
1427
|
-
}
|
1428
|
-
|
1429
|
-
/* reference dictionary content */
|
1430
|
-
return ZSTD_refDictContent(dctx, dict, dictSize);
|
1431
|
-
}
|
1432
|
-
|
1433
|
-
size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)
|
1434
|
-
{
|
1435
|
-
CHECK_F(ZSTD_decompressBegin(dctx));
|
1436
|
-
if (dict && dictSize) CHECK_E(ZSTD_decompress_insertDictionary(dctx, dict, dictSize), dictionary_corrupted);
|
1437
|
-
return 0;
|
1438
|
-
}
|
1439
|
-
|
1440
|
-
|
1441
|
-
/* ====== ZSTD_DDict ====== */
|
1442
|
-
|
1443
|
-
struct ZSTD_DDict_s {
|
1444
|
-
void* dict;
|
1445
|
-
size_t dictSize;
|
1446
|
-
ZSTD_DCtx* refContext;
|
1447
|
-
}; /* typedef'd to ZSTD_DDict within "zstd.h" */
|
1448
|
-
|
1449
|
-
ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, ZSTD_customMem customMem)
|
1450
|
-
{
|
1451
|
-
if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
|
1452
|
-
if (!customMem.customAlloc || !customMem.customFree) return NULL;
|
1453
|
-
|
1454
|
-
{ ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);
|
1455
|
-
void* const dictContent = ZSTD_malloc(dictSize, customMem);
|
1456
|
-
ZSTD_DCtx* const dctx = ZSTD_createDCtx_advanced(customMem);
|
1457
|
-
|
1458
|
-
if (!dictContent || !ddict || !dctx) {
|
1459
|
-
ZSTD_free(dictContent, customMem);
|
1460
|
-
ZSTD_free(ddict, customMem);
|
1461
|
-
ZSTD_free(dctx, customMem);
|
1462
|
-
return NULL;
|
1463
|
-
}
|
1464
|
-
|
1465
|
-
if (dictSize) {
|
1466
|
-
memcpy(dictContent, dict, dictSize);
|
1467
|
-
}
|
1468
|
-
{ size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dictContent, dictSize);
|
1469
|
-
if (ZSTD_isError(errorCode)) {
|
1470
|
-
ZSTD_free(dictContent, customMem);
|
1471
|
-
ZSTD_free(ddict, customMem);
|
1472
|
-
ZSTD_free(dctx, customMem);
|
1473
|
-
return NULL;
|
1474
|
-
} }
|
1475
|
-
|
1476
|
-
ddict->dict = dictContent;
|
1477
|
-
ddict->dictSize = dictSize;
|
1478
|
-
ddict->refContext = dctx;
|
1479
|
-
return ddict;
|
1480
|
-
}
|
1481
|
-
}
|
1482
|
-
|
1483
|
-
/*! ZSTD_createDDict() :
|
1484
|
-
* Create a digested dictionary, ready to start decompression without startup delay.
|
1485
|
-
* `dict` can be released after `ZSTD_DDict` creation */
|
1486
|
-
ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize)
|
1487
|
-
{
|
1488
|
-
ZSTD_customMem const allocator = { NULL, NULL, NULL };
|
1489
|
-
return ZSTD_createDDict_advanced(dict, dictSize, allocator);
|
1490
|
-
}
|
1491
|
-
|
1492
|
-
size_t ZSTD_freeDDict(ZSTD_DDict* ddict)
|
1493
|
-
{
|
1494
|
-
if (ddict==NULL) return 0; /* support free on NULL */
|
1495
|
-
{ ZSTD_customMem const cMem = ddict->refContext->customMem;
|
1496
|
-
ZSTD_freeDCtx(ddict->refContext);
|
1497
|
-
ZSTD_free(ddict->dict, cMem);
|
1498
|
-
ZSTD_free(ddict, cMem);
|
1499
|
-
return 0;
|
1500
|
-
}
|
1501
|
-
}
|
1502
|
-
|
1503
|
-
size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict)
|
1504
|
-
{
|
1505
|
-
if (ddict==NULL) return 0; /* support sizeof on NULL */
|
1506
|
-
return sizeof(*ddict) + sizeof(ddict->refContext) + ddict->dictSize;
|
1507
|
-
}
|
1508
|
-
|
1509
|
-
|
1510
|
-
/*! ZSTD_decompress_usingDDict() :
|
1511
|
-
* Decompression using a pre-digested Dictionary
|
1512
|
-
* Use dictionary without significant overhead. */
|
1513
|
-
size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
|
1514
|
-
void* dst, size_t dstCapacity,
|
1515
|
-
const void* src, size_t srcSize,
|
1516
|
-
const ZSTD_DDict* ddict)
|
1517
|
-
{
|
1518
|
-
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
|
1519
|
-
if (ZSTD_isLegacy(src, srcSize)) return ZSTD_decompressLegacy(dst, dstCapacity, src, srcSize, ddict->dict, ddict->dictSize);
|
1520
|
-
#endif
|
1521
|
-
ZSTD_refDCtx(dctx, ddict->refContext);
|
1522
|
-
ZSTD_checkContinuity(dctx, dst);
|
1523
|
-
return ZSTD_decompressFrame(dctx, dst, dstCapacity, src, srcSize);
|
1524
|
-
}
|
1525
|
-
|
1526
|
-
|
1527
|
-
/*=====================================
|
1528
|
-
* Streaming decompression
|
1529
|
-
*====================================*/
|
1530
|
-
|
1531
|
-
typedef enum { zdss_init, zdss_loadHeader,
|
1532
|
-
zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;
|
1533
|
-
|
1534
|
-
/* *** Resource management *** */
|
1535
|
-
struct ZSTD_DStream_s {
|
1536
|
-
ZSTD_DCtx* dctx;
|
1537
|
-
ZSTD_DDict* ddictLocal;
|
1538
|
-
const ZSTD_DDict* ddict;
|
1539
|
-
ZSTD_frameParams fParams;
|
1540
|
-
ZSTD_dStreamStage stage;
|
1541
|
-
char* inBuff;
|
1542
|
-
size_t inBuffSize;
|
1543
|
-
size_t inPos;
|
1544
|
-
size_t maxWindowSize;
|
1545
|
-
char* outBuff;
|
1546
|
-
size_t outBuffSize;
|
1547
|
-
size_t outStart;
|
1548
|
-
size_t outEnd;
|
1549
|
-
size_t blockSize;
|
1550
|
-
BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; /* tmp buffer to store frame header */
|
1551
|
-
size_t lhSize;
|
1552
|
-
ZSTD_customMem customMem;
|
1553
|
-
void* legacyContext;
|
1554
|
-
U32 previousLegacyVersion;
|
1555
|
-
U32 legacyVersion;
|
1556
|
-
U32 hostageByte;
|
1557
|
-
}; /* typedef'd to ZSTD_DStream within "zstd.h" */
|
1558
|
-
|
1559
|
-
|
1560
|
-
ZSTD_DStream* ZSTD_createDStream(void)
|
1561
|
-
{
|
1562
|
-
return ZSTD_createDStream_advanced(defaultCustomMem);
|
1563
|
-
}
|
1564
|
-
|
1565
|
-
ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)
|
1566
|
-
{
|
1567
|
-
ZSTD_DStream* zds;
|
1568
|
-
|
1569
|
-
if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem;
|
1570
|
-
if (!customMem.customAlloc || !customMem.customFree) return NULL;
|
1571
|
-
|
1572
|
-
zds = (ZSTD_DStream*) ZSTD_malloc(sizeof(ZSTD_DStream), customMem);
|
1573
|
-
if (zds==NULL) return NULL;
|
1574
|
-
memset(zds, 0, sizeof(ZSTD_DStream));
|
1575
|
-
memcpy(&zds->customMem, &customMem, sizeof(ZSTD_customMem));
|
1576
|
-
zds->dctx = ZSTD_createDCtx_advanced(customMem);
|
1577
|
-
if (zds->dctx == NULL) { ZSTD_freeDStream(zds); return NULL; }
|
1578
|
-
zds->stage = zdss_init;
|
1579
|
-
zds->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;
|
1580
|
-
return zds;
|
1581
|
-
}
|
1582
|
-
|
1583
|
-
size_t ZSTD_freeDStream(ZSTD_DStream* zds)
|
1584
|
-
{
|
1585
|
-
if (zds==NULL) return 0; /* support free on null */
|
1586
|
-
{ ZSTD_customMem const cMem = zds->customMem;
|
1587
|
-
ZSTD_freeDCtx(zds->dctx);
|
1588
|
-
ZSTD_freeDDict(zds->ddictLocal);
|
1589
|
-
ZSTD_free(zds->inBuff, cMem);
|
1590
|
-
ZSTD_free(zds->outBuff, cMem);
|
1591
|
-
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
|
1592
|
-
if (zds->legacyContext)
|
1593
|
-
ZSTD_freeLegacyStreamContext(zds->legacyContext, zds->previousLegacyVersion);
|
1594
|
-
#endif
|
1595
|
-
ZSTD_free(zds, cMem);
|
1596
|
-
return 0;
|
1597
|
-
}
|
1598
|
-
}
|
1599
|
-
|
1600
|
-
|
1601
|
-
/* *** Initialization *** */
|
1602
|
-
|
1603
|
-
size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX + ZSTD_blockHeaderSize; }
|
1604
|
-
size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; }
|
1605
|
-
|
1606
|
-
size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)
|
1607
|
-
{
|
1608
|
-
zds->stage = zdss_loadHeader;
|
1609
|
-
zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
|
1610
|
-
ZSTD_freeDDict(zds->ddictLocal);
|
1611
|
-
if (dict) {
|
1612
|
-
zds->ddictLocal = ZSTD_createDDict(dict, dictSize);
|
1613
|
-
if (zds->ddictLocal == NULL) return ERROR(memory_allocation);
|
1614
|
-
} else zds->ddictLocal = NULL;
|
1615
|
-
zds->ddict = zds->ddictLocal;
|
1616
|
-
zds->legacyVersion = 0;
|
1617
|
-
zds->hostageByte = 0;
|
1618
|
-
return ZSTD_frameHeaderSize_prefix;
|
1619
|
-
}
|
1620
|
-
|
1621
|
-
size_t ZSTD_initDStream(ZSTD_DStream* zds)
|
1622
|
-
{
|
1623
|
-
return ZSTD_initDStream_usingDict(zds, NULL, 0);
|
1624
|
-
}
|
1625
|
-
|
1626
|
-
size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict) /**< note : ddict will just be referenced, and must outlive decompression session */
|
1627
|
-
{
|
1628
|
-
size_t const initResult = ZSTD_initDStream(zds);
|
1629
|
-
zds->ddict = ddict;
|
1630
|
-
return initResult;
|
1631
|
-
}
|
1632
|
-
|
1633
|
-
size_t ZSTD_resetDStream(ZSTD_DStream* zds)
|
1634
|
-
{
|
1635
|
-
zds->stage = zdss_loadHeader;
|
1636
|
-
zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;
|
1637
|
-
zds->legacyVersion = 0;
|
1638
|
-
zds->hostageByte = 0;
|
1639
|
-
return ZSTD_frameHeaderSize_prefix;
|
1640
|
-
}
|
1641
|
-
|
1642
|
-
size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds,
|
1643
|
-
ZSTD_DStreamParameter_e paramType, unsigned paramValue)
|
1644
|
-
{
|
1645
|
-
switch(paramType)
|
1646
|
-
{
|
1647
|
-
default : return ERROR(parameter_unknown);
|
1648
|
-
case ZSTDdsp_maxWindowSize : zds->maxWindowSize = paramValue ? paramValue : (U32)(-1); break;
|
1649
|
-
}
|
1650
|
-
return 0;
|
1651
|
-
}
|
1652
|
-
|
1653
|
-
|
1654
|
-
size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds)
|
1655
|
-
{
|
1656
|
-
if (zds==NULL) return 0; /* support sizeof on NULL */
|
1657
|
-
return sizeof(*zds) + ZSTD_sizeof_DCtx(zds->dctx) + ZSTD_sizeof_DDict(zds->ddictLocal) + zds->inBuffSize + zds->outBuffSize;
|
1658
|
-
}
|
1659
|
-
|
1660
|
-
|
1661
|
-
/* ***** Decompression ***** */
|
1662
|
-
|
1663
|
-
MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
|
1664
|
-
{
|
1665
|
-
size_t const length = MIN(dstCapacity, srcSize);
|
1666
|
-
memcpy(dst, src, length);
|
1667
|
-
return length;
|
1668
|
-
}
|
1669
|
-
|
1670
|
-
|
1671
|
-
size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
|
1672
|
-
{
|
1673
|
-
const char* const istart = (const char*)(input->src) + input->pos;
|
1674
|
-
const char* const iend = (const char*)(input->src) + input->size;
|
1675
|
-
const char* ip = istart;
|
1676
|
-
char* const ostart = (char*)(output->dst) + output->pos;
|
1677
|
-
char* const oend = (char*)(output->dst) + output->size;
|
1678
|
-
char* op = ostart;
|
1679
|
-
U32 someMoreWork = 1;
|
1680
|
-
|
1681
|
-
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
|
1682
|
-
if (zds->legacyVersion)
|
1683
|
-
return ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
|
1684
|
-
#endif
|
1685
|
-
|
1686
|
-
while (someMoreWork) {
|
1687
|
-
switch(zds->stage)
|
1688
|
-
{
|
1689
|
-
case zdss_init :
|
1690
|
-
return ERROR(init_missing);
|
1691
|
-
|
1692
|
-
case zdss_loadHeader :
|
1693
|
-
{ size_t const hSize = ZSTD_getFrameParams(&zds->fParams, zds->headerBuffer, zds->lhSize);
|
1694
|
-
if (ZSTD_isError(hSize))
|
1695
|
-
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
|
1696
|
-
{ U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
|
1697
|
-
if (legacyVersion) {
|
1698
|
-
const void* const dict = zds->ddict ? zds->ddict->dict : NULL;
|
1699
|
-
size_t const dictSize = zds->ddict ? zds->ddict->dictSize : 0;
|
1700
|
-
CHECK_F(ZSTD_initLegacyStream(&zds->legacyContext, zds->previousLegacyVersion, legacyVersion,
|
1701
|
-
dict, dictSize));
|
1702
|
-
zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;
|
1703
|
-
return ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);
|
1704
|
-
} else {
|
1705
|
-
return hSize; /* error */
|
1706
|
-
} }
|
1707
|
-
#else
|
1708
|
-
return hSize;
|
1709
|
-
#endif
|
1710
|
-
if (hSize != 0) { /* need more input */
|
1711
|
-
size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */
|
1712
|
-
if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */
|
1713
|
-
memcpy(zds->headerBuffer + zds->lhSize, ip, iend-ip);
|
1714
|
-
zds->lhSize += iend-ip;
|
1715
|
-
input->pos = input->size;
|
1716
|
-
return (MAX(ZSTD_frameHeaderSize_min, hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */
|
1717
|
-
}
|
1718
|
-
memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;
|
1719
|
-
break;
|
1720
|
-
} }
|
1721
|
-
|
1722
|
-
/* Consume header */
|
1723
|
-
{ const ZSTD_DCtx* refContext = zds->ddict ? zds->ddict->refContext : NULL;
|
1724
|
-
ZSTD_refDCtx(zds->dctx, refContext);
|
1725
|
-
}
|
1726
|
-
{ size_t const h1Size = ZSTD_nextSrcSizeToDecompress(zds->dctx); /* == ZSTD_frameHeaderSize_prefix */
|
1727
|
-
CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer, h1Size));
|
1728
|
-
{ size_t const h2Size = ZSTD_nextSrcSizeToDecompress(zds->dctx);
|
1729
|
-
CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer+h1Size, h2Size));
|
1730
|
-
} }
|
1731
|
-
|
1732
|
-
zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);
|
1733
|
-
if (zds->fParams.windowSize > zds->maxWindowSize) return ERROR(frameParameter_windowTooLarge);
|
1734
|
-
|
1735
|
-
/* Adapt buffer sizes to frame header instructions */
|
1736
|
-
{ size_t const blockSize = MIN(zds->fParams.windowSize, ZSTD_BLOCKSIZE_ABSOLUTEMAX);
|
1737
|
-
size_t const neededOutSize = zds->fParams.windowSize + blockSize;
|
1738
|
-
zds->blockSize = blockSize;
|
1739
|
-
if (zds->inBuffSize < blockSize) {
|
1740
|
-
ZSTD_free(zds->inBuff, zds->customMem);
|
1741
|
-
zds->inBuffSize = blockSize;
|
1742
|
-
zds->inBuff = (char*)ZSTD_malloc(blockSize, zds->customMem);
|
1743
|
-
if (zds->inBuff == NULL) return ERROR(memory_allocation);
|
1744
|
-
}
|
1745
|
-
if (zds->outBuffSize < neededOutSize) {
|
1746
|
-
ZSTD_free(zds->outBuff, zds->customMem);
|
1747
|
-
zds->outBuffSize = neededOutSize;
|
1748
|
-
zds->outBuff = (char*)ZSTD_malloc(neededOutSize, zds->customMem);
|
1749
|
-
if (zds->outBuff == NULL) return ERROR(memory_allocation);
|
1750
|
-
} }
|
1751
|
-
zds->stage = zdss_read;
|
1752
|
-
/* pass-through */
|
1753
|
-
|
1754
|
-
case zdss_read:
|
1755
|
-
{ size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds->dctx);
|
1756
|
-
if (neededInSize==0) { /* end of frame */
|
1757
|
-
zds->stage = zdss_init;
|
1758
|
-
someMoreWork = 0;
|
1759
|
-
break;
|
1760
|
-
}
|
1761
|
-
if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
|
1762
|
-
const int isSkipFrame = ZSTD_isSkipFrame(zds->dctx);
|
1763
|
-
size_t const decodedSize = ZSTD_decompressContinue(zds->dctx,
|
1764
|
-
zds->outBuff + zds->outStart, (isSkipFrame ? 0 : zds->outBuffSize - zds->outStart),
|
1765
|
-
ip, neededInSize);
|
1766
|
-
if (ZSTD_isError(decodedSize)) return decodedSize;
|
1767
|
-
ip += neededInSize;
|
1768
|
-
if (!decodedSize && !isSkipFrame) break; /* this was just a header */
|
1769
|
-
zds->outEnd = zds->outStart + decodedSize;
|
1770
|
-
zds->stage = zdss_flush;
|
1771
|
-
break;
|
1772
|
-
}
|
1773
|
-
if (ip==iend) { someMoreWork = 0; break; } /* no more input */
|
1774
|
-
zds->stage = zdss_load;
|
1775
|
-
/* pass-through */
|
1776
|
-
}
|
1777
|
-
|
1778
|
-
case zdss_load:
|
1779
|
-
{ size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds->dctx);
|
1780
|
-
size_t const toLoad = neededInSize - zds->inPos; /* should always be <= remaining space within inBuff */
|
1781
|
-
size_t loadedSize;
|
1782
|
-
if (toLoad > zds->inBuffSize - zds->inPos) return ERROR(corruption_detected); /* should never happen */
|
1783
|
-
loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip);
|
1784
|
-
ip += loadedSize;
|
1785
|
-
zds->inPos += loadedSize;
|
1786
|
-
if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */
|
1787
|
-
|
1788
|
-
/* decode loaded input */
|
1789
|
-
{ const int isSkipFrame = ZSTD_isSkipFrame(zds->dctx);
|
1790
|
-
size_t const decodedSize = ZSTD_decompressContinue(zds->dctx,
|
1791
|
-
zds->outBuff + zds->outStart, zds->outBuffSize - zds->outStart,
|
1792
|
-
zds->inBuff, neededInSize);
|
1793
|
-
if (ZSTD_isError(decodedSize)) return decodedSize;
|
1794
|
-
zds->inPos = 0; /* input is consumed */
|
1795
|
-
if (!decodedSize && !isSkipFrame) { zds->stage = zdss_read; break; } /* this was just a header */
|
1796
|
-
zds->outEnd = zds->outStart + decodedSize;
|
1797
|
-
zds->stage = zdss_flush;
|
1798
|
-
/* pass-through */
|
1799
|
-
} }
|
1800
|
-
|
1801
|
-
case zdss_flush:
|
1802
|
-
{ size_t const toFlushSize = zds->outEnd - zds->outStart;
|
1803
|
-
size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize);
|
1804
|
-
op += flushedSize;
|
1805
|
-
zds->outStart += flushedSize;
|
1806
|
-
if (flushedSize == toFlushSize) { /* flush completed */
|
1807
|
-
zds->stage = zdss_read;
|
1808
|
-
if (zds->outStart + zds->blockSize > zds->outBuffSize)
|
1809
|
-
zds->outStart = zds->outEnd = 0;
|
1810
|
-
break;
|
1811
|
-
}
|
1812
|
-
/* cannot complete flush */
|
1813
|
-
someMoreWork = 0;
|
1814
|
-
break;
|
1815
|
-
}
|
1816
|
-
default: return ERROR(GENERIC); /* impossible */
|
1817
|
-
} }
|
1818
|
-
|
1819
|
-
/* result */
|
1820
|
-
input->pos += (size_t)(ip-istart);
|
1821
|
-
output->pos += (size_t)(op-ostart);
|
1822
|
-
{ size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds->dctx);
|
1823
|
-
if (!nextSrcSizeHint) { /* frame fully decoded */
|
1824
|
-
if (zds->outEnd == zds->outStart) { /* output fully flushed */
|
1825
|
-
if (zds->hostageByte) {
|
1826
|
-
if (input->pos >= input->size) { zds->stage = zdss_read; return 1; } /* can't release hostage (not present) */
|
1827
|
-
input->pos++; /* release hostage */
|
1828
|
-
}
|
1829
|
-
return 0;
|
1830
|
-
}
|
1831
|
-
if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */
|
1832
|
-
input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */
|
1833
|
-
zds->hostageByte=1;
|
1834
|
-
}
|
1835
|
-
return 1;
|
1836
|
-
}
|
1837
|
-
nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds->dctx) == ZSTDnit_block); /* preload header of next block */
|
1838
|
-
if (zds->inPos > nextSrcSizeHint) return ERROR(GENERIC); /* should never happen */
|
1839
|
-
nextSrcSizeHint -= zds->inPos; /* already loaded*/
|
1840
|
-
return nextSrcSizeHint;
|
1841
|
-
}
|
1842
|
-
}
|