/src/c-blosc/internal-complibs/zstd-1.5.6/decompress/zstd_decompress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This source code is licensed under both the BSD-style license (found in the |
6 | | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
7 | | * in the COPYING file in the root directory of this source tree). |
8 | | * You may select, at your option, one of the above-listed licenses. |
9 | | */ |
10 | | |
11 | | |
12 | | /* *************************************************************** |
13 | | * Tuning parameters |
14 | | *****************************************************************/ |
15 | | /*! |
16 | | * HEAPMODE : |
17 | | * Select how default decompression function ZSTD_decompress() allocates its context, |
18 | | * on stack (0), or into heap (1, default; requires malloc()). |
19 | | * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected. |
20 | | */ |
21 | | #ifndef ZSTD_HEAPMODE |
22 | | # define ZSTD_HEAPMODE 1 |
23 | | #endif |
24 | | |
25 | | /*! |
26 | | * LEGACY_SUPPORT : |
27 | | * if set to 1+, ZSTD_decompress() can decode older formats (v0.1+) |
28 | | */ |
29 | | #ifndef ZSTD_LEGACY_SUPPORT |
30 | | # define ZSTD_LEGACY_SUPPORT 0 |
31 | | #endif |
32 | | |
33 | | /*! |
34 | | * MAXWINDOWSIZE_DEFAULT : |
35 | | * maximum window size accepted by DStream __by default__. |
36 | | * Frames requiring more memory will be rejected. |
37 | | * It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize(). |
38 | | */ |
39 | | #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT |
40 | 6.20k | # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1) |
41 | | #endif |
42 | | |
43 | | /*! |
44 | | * NO_FORWARD_PROGRESS_MAX : |
45 | | * maximum allowed nb of calls to ZSTD_decompressStream() |
46 | | * without any forward progress |
47 | | * (defined as: no byte read from input, and no byte flushed to output) |
48 | | * before triggering an error. |
49 | | */ |
50 | | #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX |
51 | 0 | # define ZSTD_NO_FORWARD_PROGRESS_MAX 16 |
52 | | #endif |
53 | | |
54 | | |
55 | | /*-******************************************************* |
56 | | * Dependencies |
57 | | *********************************************************/ |
58 | | #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */ |
59 | | #include "../common/allocations.h" /* ZSTD_customMalloc, ZSTD_customCalloc, ZSTD_customFree */ |
60 | | #include "../common/error_private.h" |
61 | | #include "../common/zstd_internal.h" /* blockProperties_t */ |
62 | | #include "../common/mem.h" /* low level memory routines */ |
63 | | #include "../common/bits.h" /* ZSTD_highbit32 */ |
64 | | #define FSE_STATIC_LINKING_ONLY |
65 | | #include "../common/fse.h" |
66 | | #include "../common/huf.h" |
67 | | #include "../common/xxhash.h" /* XXH64_reset, XXH64_update, XXH64_digest, XXH64 */ |
68 | | #include "zstd_decompress_internal.h" /* ZSTD_DCtx */ |
69 | | #include "zstd_ddict.h" /* ZSTD_DDictDictContent */ |
70 | | #include "zstd_decompress_block.h" /* ZSTD_decompressBlock_internal */ |
71 | | |
72 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
73 | | # include "../legacy/zstd_legacy.h" |
74 | | #endif |
75 | | |
76 | | |
77 | | |
78 | | /************************************* |
79 | | * Multiple DDicts Hashset internals * |
80 | | *************************************/ |
81 | | |
82 | 0 | #define DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT 4 |
83 | 0 | #define DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT 3 /* These two constants represent SIZE_MULT/COUNT_MULT load factor without using a float. |
84 | | * Currently, that means a 0.75 load factor. |
85 | | * So, if count * COUNT_MULT / size * SIZE_MULT != 0, then we've exceeded |
86 | | * the load factor of the ddict hash set. |
87 | | */ |
88 | | |
89 | 0 | #define DDICT_HASHSET_TABLE_BASE_SIZE 64 |
90 | 0 | #define DDICT_HASHSET_RESIZE_FACTOR 2 |
91 | | |
92 | | /* Hash function to determine starting position of dict insertion within the table |
93 | | * Returns an index between [0, hashSet->ddictPtrTableSize] |
94 | | */ |
95 | 0 | static size_t ZSTD_DDictHashSet_getIndex(const ZSTD_DDictHashSet* hashSet, U32 dictID) { |
96 | 0 | const U64 hash = XXH64(&dictID, sizeof(U32), 0); |
97 | | /* DDict ptr table size is a multiple of 2, use size - 1 as mask to get index within [0, hashSet->ddictPtrTableSize) */ |
98 | 0 | return hash & (hashSet->ddictPtrTableSize - 1); |
99 | 0 | } |
100 | | |
101 | | /* Adds DDict to a hashset without resizing it. |
102 | | * If inserting a DDict with a dictID that already exists in the set, replaces the one in the set. |
103 | | * Returns 0 if successful, or a zstd error code if something went wrong. |
104 | | */ |
105 | 0 | static size_t ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict) { |
106 | 0 | const U32 dictID = ZSTD_getDictID_fromDDict(ddict); |
107 | 0 | size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID); |
108 | 0 | const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1; |
109 | 0 | RETURN_ERROR_IF(hashSet->ddictPtrCount == hashSet->ddictPtrTableSize, GENERIC, "Hash set is full!"); |
110 | 0 | DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx); |
111 | 0 | while (hashSet->ddictPtrTable[idx] != NULL) { |
112 | | /* Replace existing ddict if inserting ddict with same dictID */ |
113 | 0 | if (ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]) == dictID) { |
114 | 0 | DEBUGLOG(4, "DictID already exists, replacing rather than adding"); |
115 | 0 | hashSet->ddictPtrTable[idx] = ddict; |
116 | 0 | return 0; |
117 | 0 | } |
118 | 0 | idx &= idxRangeMask; |
119 | 0 | idx++; |
120 | 0 | } |
121 | 0 | DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx); |
122 | 0 | hashSet->ddictPtrTable[idx] = ddict; |
123 | 0 | hashSet->ddictPtrCount++; |
124 | 0 | return 0; |
125 | 0 | } |
126 | | |
127 | | /* Expands hash table by factor of DDICT_HASHSET_RESIZE_FACTOR and |
128 | | * rehashes all values, allocates new table, frees old table. |
129 | | * Returns 0 on success, otherwise a zstd error code. |
130 | | */ |
131 | 0 | static size_t ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) { |
132 | 0 | size_t newTableSize = hashSet->ddictPtrTableSize * DDICT_HASHSET_RESIZE_FACTOR; |
133 | 0 | const ZSTD_DDict** newTable = (const ZSTD_DDict**)ZSTD_customCalloc(sizeof(ZSTD_DDict*) * newTableSize, customMem); |
134 | 0 | const ZSTD_DDict** oldTable = hashSet->ddictPtrTable; |
135 | 0 | size_t oldTableSize = hashSet->ddictPtrTableSize; |
136 | 0 | size_t i; |
137 | |
|
138 | 0 | DEBUGLOG(4, "Expanding DDict hash table! Old size: %zu new size: %zu", oldTableSize, newTableSize); |
139 | 0 | RETURN_ERROR_IF(!newTable, memory_allocation, "Expanded hashset allocation failed!"); |
140 | 0 | hashSet->ddictPtrTable = newTable; |
141 | 0 | hashSet->ddictPtrTableSize = newTableSize; |
142 | 0 | hashSet->ddictPtrCount = 0; |
143 | 0 | for (i = 0; i < oldTableSize; ++i) { |
144 | 0 | if (oldTable[i] != NULL) { |
145 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, oldTable[i]), ""); |
146 | 0 | } |
147 | 0 | } |
148 | 0 | ZSTD_customFree((void*)oldTable, customMem); |
149 | 0 | DEBUGLOG(4, "Finished re-hash"); |
150 | 0 | return 0; |
151 | 0 | } |
152 | | |
153 | | /* Fetches a DDict with the given dictID |
154 | | * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL. |
155 | | */ |
156 | 0 | static const ZSTD_DDict* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet* hashSet, U32 dictID) { |
157 | 0 | size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID); |
158 | 0 | const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1; |
159 | 0 | DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx); |
160 | 0 | for (;;) { |
161 | 0 | size_t currDictID = ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]); |
162 | 0 | if (currDictID == dictID || currDictID == 0) { |
163 | | /* currDictID == 0 implies a NULL ddict entry */ |
164 | 0 | break; |
165 | 0 | } else { |
166 | 0 | idx &= idxRangeMask; /* Goes to start of table when we reach the end */ |
167 | 0 | idx++; |
168 | 0 | } |
169 | 0 | } |
170 | 0 | DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx); |
171 | 0 | return hashSet->ddictPtrTable[idx]; |
172 | 0 | } |
173 | | |
174 | | /* Allocates space for and returns a ddict hash set |
175 | | * The hash set's ZSTD_DDict* table has all values automatically set to NULL to begin with. |
176 | | * Returns NULL if allocation failed. |
177 | | */ |
178 | 0 | static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) { |
179 | 0 | ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)ZSTD_customMalloc(sizeof(ZSTD_DDictHashSet), customMem); |
180 | 0 | DEBUGLOG(4, "Allocating new hash set"); |
181 | 0 | if (!ret) |
182 | 0 | return NULL; |
183 | 0 | ret->ddictPtrTable = (const ZSTD_DDict**)ZSTD_customCalloc(DDICT_HASHSET_TABLE_BASE_SIZE * sizeof(ZSTD_DDict*), customMem); |
184 | 0 | if (!ret->ddictPtrTable) { |
185 | 0 | ZSTD_customFree(ret, customMem); |
186 | 0 | return NULL; |
187 | 0 | } |
188 | 0 | ret->ddictPtrTableSize = DDICT_HASHSET_TABLE_BASE_SIZE; |
189 | 0 | ret->ddictPtrCount = 0; |
190 | 0 | return ret; |
191 | 0 | } |
192 | | |
193 | | /* Frees the table of ZSTD_DDict* within a hashset, then frees the hashset itself. |
194 | | * Note: The ZSTD_DDict* within the table are NOT freed. |
195 | | */ |
196 | 0 | static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) { |
197 | 0 | DEBUGLOG(4, "Freeing ddict hash set"); |
198 | 0 | if (hashSet && hashSet->ddictPtrTable) { |
199 | 0 | ZSTD_customFree((void*)hashSet->ddictPtrTable, customMem); |
200 | 0 | } |
201 | 0 | if (hashSet) { |
202 | 0 | ZSTD_customFree(hashSet, customMem); |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | /* Public function: Adds a DDict into the ZSTD_DDictHashSet, possibly triggering a resize of the hash set. |
207 | | * Returns 0 on success, or a ZSTD error. |
208 | | */ |
209 | 0 | static size_t ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict, ZSTD_customMem customMem) { |
210 | 0 | DEBUGLOG(4, "Adding dict ID: %u to hashset with - Count: %zu Tablesize: %zu", ZSTD_getDictID_fromDDict(ddict), hashSet->ddictPtrCount, hashSet->ddictPtrTableSize); |
211 | 0 | if (hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT / hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT != 0) { |
212 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_expand(hashSet, customMem), ""); |
213 | 0 | } |
214 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, ddict), ""); |
215 | 0 | return 0; |
216 | 0 | } |
217 | | |
218 | | /*-************************************************************* |
219 | | * Context management |
220 | | ***************************************************************/ |
221 | | size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx) |
222 | 0 | { |
223 | 0 | if (dctx==NULL) return 0; /* support sizeof NULL */ |
224 | 0 | return sizeof(*dctx) |
225 | 0 | + ZSTD_sizeof_DDict(dctx->ddictLocal) |
226 | 0 | + dctx->inBuffSize + dctx->outBuffSize; |
227 | 0 | } |
228 | | |
229 | 0 | size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); } |
230 | | |
231 | | |
232 | | static size_t ZSTD_startingInputLength(ZSTD_format_e format) |
233 | 98.4k | { |
234 | 98.4k | size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format); |
235 | | /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */ |
236 | 98.4k | assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) ); |
237 | 98.4k | return startingInputLength; |
238 | 98.4k | } |
239 | | |
240 | | static void ZSTD_DCtx_resetParameters(ZSTD_DCtx* dctx) |
241 | 6.20k | { |
242 | 6.20k | assert(dctx->streamStage == zdss_init); |
243 | 6.20k | dctx->format = ZSTD_f_zstd1; |
244 | 6.20k | dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; |
245 | 6.20k | dctx->outBufferMode = ZSTD_bm_buffered; |
246 | 6.20k | dctx->forceIgnoreChecksum = ZSTD_d_validateChecksum; |
247 | 6.20k | dctx->refMultipleDDicts = ZSTD_rmd_refSingleDDict; |
248 | 6.20k | dctx->disableHufAsm = 0; |
249 | 6.20k | dctx->maxBlockSizeParam = 0; |
250 | 6.20k | } |
251 | | |
252 | | static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx) |
253 | 6.20k | { |
254 | 6.20k | dctx->staticSize = 0; |
255 | 6.20k | dctx->ddict = NULL; |
256 | 6.20k | dctx->ddictLocal = NULL; |
257 | 6.20k | dctx->dictEnd = NULL; |
258 | 6.20k | dctx->ddictIsCold = 0; |
259 | 6.20k | dctx->dictUses = ZSTD_dont_use; |
260 | 6.20k | dctx->inBuff = NULL; |
261 | 6.20k | dctx->inBuffSize = 0; |
262 | 6.20k | dctx->outBuffSize = 0; |
263 | 6.20k | dctx->streamStage = zdss_init; |
264 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
265 | | dctx->legacyContext = NULL; |
266 | | dctx->previousLegacyVersion = 0; |
267 | | #endif |
268 | 6.20k | dctx->noForwardProgress = 0; |
269 | 6.20k | dctx->oversizedDuration = 0; |
270 | 6.20k | dctx->isFrameDecompression = 1; |
271 | 6.20k | #if DYNAMIC_BMI2 |
272 | 6.20k | dctx->bmi2 = ZSTD_cpuSupportsBmi2(); |
273 | 6.20k | #endif |
274 | 6.20k | dctx->ddictSet = NULL; |
275 | 6.20k | ZSTD_DCtx_resetParameters(dctx); |
276 | 6.20k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
277 | 6.20k | dctx->dictContentEndForFuzzing = NULL; |
278 | 6.20k | #endif |
279 | 6.20k | } |
280 | | |
281 | | ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize) |
282 | 0 | { |
283 | 0 | ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace; |
284 | |
|
285 | 0 | if ((size_t)workspace & 7) return NULL; /* 8-aligned */ |
286 | 0 | if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */ |
287 | | |
288 | 0 | ZSTD_initDCtx_internal(dctx); |
289 | 0 | dctx->staticSize = workspaceSize; |
290 | 0 | dctx->inBuff = (char*)(dctx+1); |
291 | 0 | return dctx; |
292 | 0 | } |
293 | | |
294 | 6.20k | static ZSTD_DCtx* ZSTD_createDCtx_internal(ZSTD_customMem customMem) { |
295 | 6.20k | if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL; |
296 | | |
297 | 6.20k | { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_customMalloc(sizeof(*dctx), customMem); |
298 | 6.20k | if (!dctx) return NULL; |
299 | 6.20k | dctx->customMem = customMem; |
300 | 6.20k | ZSTD_initDCtx_internal(dctx); |
301 | 6.20k | return dctx; |
302 | 6.20k | } |
303 | 6.20k | } |
304 | | |
305 | | ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem) |
306 | 0 | { |
307 | 0 | return ZSTD_createDCtx_internal(customMem); |
308 | 0 | } |
309 | | |
310 | | ZSTD_DCtx* ZSTD_createDCtx(void) |
311 | 0 | { |
312 | 0 | DEBUGLOG(3, "ZSTD_createDCtx"); |
313 | 0 | return ZSTD_createDCtx_internal(ZSTD_defaultCMem); |
314 | 0 | } |
315 | | |
316 | | static void ZSTD_clearDict(ZSTD_DCtx* dctx) |
317 | 12.4k | { |
318 | 12.4k | ZSTD_freeDDict(dctx->ddictLocal); |
319 | 12.4k | dctx->ddictLocal = NULL; |
320 | 12.4k | dctx->ddict = NULL; |
321 | 12.4k | dctx->dictUses = ZSTD_dont_use; |
322 | 12.4k | } |
323 | | |
324 | | size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) |
325 | 6.20k | { |
326 | 6.20k | if (dctx==NULL) return 0; /* support free on NULL */ |
327 | 6.20k | RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx"); |
328 | 6.20k | { ZSTD_customMem const cMem = dctx->customMem; |
329 | 6.20k | ZSTD_clearDict(dctx); |
330 | 6.20k | ZSTD_customFree(dctx->inBuff, cMem); |
331 | 6.20k | dctx->inBuff = NULL; |
332 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
333 | | if (dctx->legacyContext) |
334 | | ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion); |
335 | | #endif |
336 | 6.20k | if (dctx->ddictSet) { |
337 | 0 | ZSTD_freeDDictHashSet(dctx->ddictSet, cMem); |
338 | 0 | dctx->ddictSet = NULL; |
339 | 0 | } |
340 | 6.20k | ZSTD_customFree(dctx, cMem); |
341 | 6.20k | return 0; |
342 | 6.20k | } |
343 | 6.20k | } |
344 | | |
345 | | /* no longer useful */ |
346 | | void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx) |
347 | 0 | { |
348 | 0 | size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx); |
349 | 0 | ZSTD_memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */ |
350 | 0 | } |
351 | | |
352 | | /* Given a dctx with a digested frame params, re-selects the correct ZSTD_DDict based on |
353 | | * the requested dict ID from the frame. If there exists a reference to the correct ZSTD_DDict, then |
354 | | * accordingly sets the ddict to be used to decompress the frame. |
355 | | * |
356 | | * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is. |
357 | | * |
358 | | * ZSTD_d_refMultipleDDicts must be enabled for this function to be called. |
359 | | */ |
360 | 0 | static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx* dctx) { |
361 | 0 | assert(dctx->refMultipleDDicts && dctx->ddictSet); |
362 | 0 | DEBUGLOG(4, "Adjusting DDict based on requested dict ID from frame"); |
363 | 0 | if (dctx->ddict) { |
364 | 0 | const ZSTD_DDict* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID); |
365 | 0 | if (frameDDict) { |
366 | 0 | DEBUGLOG(4, "DDict found!"); |
367 | 0 | ZSTD_clearDict(dctx); |
368 | 0 | dctx->dictID = dctx->fParams.dictID; |
369 | 0 | dctx->ddict = frameDDict; |
370 | 0 | dctx->dictUses = ZSTD_use_indefinitely; |
371 | 0 | } |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | | |
376 | | /*-************************************************************* |
377 | | * Frame header decoding |
378 | | ***************************************************************/ |
379 | | |
380 | | /*! ZSTD_isFrame() : |
381 | | * Tells if the content of `buffer` starts with a valid Frame Identifier. |
382 | | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. |
383 | | * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. |
384 | | * Note 3 : Skippable Frame Identifiers are considered valid. */ |
385 | | unsigned ZSTD_isFrame(const void* buffer, size_t size) |
386 | 0 | { |
387 | 0 | if (size < ZSTD_FRAMEIDSIZE) return 0; |
388 | 0 | { U32 const magic = MEM_readLE32(buffer); |
389 | 0 | if (magic == ZSTD_MAGICNUMBER) return 1; |
390 | 0 | if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1; |
391 | 0 | } |
392 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
393 | | if (ZSTD_isLegacy(buffer, size)) return 1; |
394 | | #endif |
395 | 0 | return 0; |
396 | 0 | } |
397 | | |
398 | | /*! ZSTD_isSkippableFrame() : |
399 | | * Tells if the content of `buffer` starts with a valid Frame Identifier for a skippable frame. |
400 | | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. |
401 | | */ |
402 | | unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size) |
403 | 0 | { |
404 | 0 | if (size < ZSTD_FRAMEIDSIZE) return 0; |
405 | 0 | { U32 const magic = MEM_readLE32(buffer); |
406 | 0 | if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1; |
407 | 0 | } |
408 | 0 | return 0; |
409 | 0 | } |
410 | | |
411 | | /** ZSTD_frameHeaderSize_internal() : |
412 | | * srcSize must be large enough to reach header size fields. |
413 | | * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless. |
414 | | * @return : size of the Frame Header |
415 | | * or an error code, which can be tested with ZSTD_isError() */ |
416 | | static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format) |
417 | 38.2k | { |
418 | 38.2k | size_t const minInputSize = ZSTD_startingInputLength(format); |
419 | 38.2k | RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong, ""); |
420 | | |
421 | 38.2k | { BYTE const fhd = ((const BYTE*)src)[minInputSize-1]; |
422 | 38.2k | U32 const dictID= fhd & 3; |
423 | 38.2k | U32 const singleSegment = (fhd >> 5) & 1; |
424 | 38.2k | U32 const fcsId = fhd >> 6; |
425 | 38.2k | return minInputSize + !singleSegment |
426 | 38.2k | + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] |
427 | 38.2k | + (singleSegment && !fcsId); |
428 | 38.2k | } |
429 | 38.2k | } |
430 | | |
431 | | /** ZSTD_frameHeaderSize() : |
432 | | * srcSize must be >= ZSTD_frameHeaderSize_prefix. |
433 | | * @return : size of the Frame Header, |
434 | | * or an error code (if srcSize is too small) */ |
435 | | size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize) |
436 | 0 | { |
437 | 0 | return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1); |
438 | 0 | } |
439 | | |
440 | | |
441 | | /** ZSTD_getFrameHeader_advanced() : |
442 | | * decode Frame Header, or require larger `srcSize`. |
443 | | * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless |
444 | | * @return : 0, `zfhPtr` is correctly filled, |
445 | | * >0, `srcSize` is too small, value is wanted `srcSize` amount, |
446 | | ** or an error code, which can be tested using ZSTD_isError() */ |
447 | | size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format) |
448 | 19.1k | { |
449 | 19.1k | const BYTE* ip = (const BYTE*)src; |
450 | 19.1k | size_t const minInputSize = ZSTD_startingInputLength(format); |
451 | | |
452 | 19.1k | DEBUGLOG(5, "ZSTD_getFrameHeader_advanced: minInputSize = %zu, srcSize = %zu", minInputSize, srcSize); |
453 | | |
454 | 19.1k | if (srcSize > 0) { |
455 | | /* note : technically could be considered an assert(), since it's an invalid entry */ |
456 | 19.1k | RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter : src==NULL, but srcSize>0"); |
457 | 19.1k | } |
458 | 19.1k | if (srcSize < minInputSize) { |
459 | 0 | if (srcSize > 0 && format != ZSTD_f_zstd1_magicless) { |
460 | | /* when receiving less than @minInputSize bytes, |
461 | | * control these bytes at least correspond to a supported magic number |
462 | | * in order to error out early if they don't. |
463 | | **/ |
464 | 0 | size_t const toCopy = MIN(4, srcSize); |
465 | 0 | unsigned char hbuf[4]; MEM_writeLE32(hbuf, ZSTD_MAGICNUMBER); |
466 | 0 | assert(src != NULL); |
467 | 0 | ZSTD_memcpy(hbuf, src, toCopy); |
468 | 0 | if ( MEM_readLE32(hbuf) != ZSTD_MAGICNUMBER ) { |
469 | | /* not a zstd frame : let's check if it's a skippable frame */ |
470 | 0 | MEM_writeLE32(hbuf, ZSTD_MAGIC_SKIPPABLE_START); |
471 | 0 | ZSTD_memcpy(hbuf, src, toCopy); |
472 | 0 | if ((MEM_readLE32(hbuf) & ZSTD_MAGIC_SKIPPABLE_MASK) != ZSTD_MAGIC_SKIPPABLE_START) { |
473 | 0 | RETURN_ERROR(prefix_unknown, |
474 | 0 | "first bytes don't correspond to any supported magic number"); |
475 | 0 | } } } |
476 | 0 | return minInputSize; |
477 | 0 | } |
478 | | |
479 | 19.1k | ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzers may not understand that zfhPtr will be read only if return value is zero, since they are 2 different signals */ |
480 | 19.1k | if ( (format != ZSTD_f_zstd1_magicless) |
481 | 19.1k | && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) { |
482 | 125 | if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
483 | | /* skippable frame */ |
484 | 0 | if (srcSize < ZSTD_SKIPPABLEHEADERSIZE) |
485 | 0 | return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */ |
486 | 0 | ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr)); |
487 | 0 | zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE); |
488 | 0 | zfhPtr->frameType = ZSTD_skippableFrame; |
489 | 0 | return 0; |
490 | 0 | } |
491 | 125 | RETURN_ERROR(prefix_unknown, ""); |
492 | 125 | } |
493 | | |
494 | | /* ensure there is enough `srcSize` to fully read/decode frame header */ |
495 | 19.0k | { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format); |
496 | 19.0k | if (srcSize < fhsize) return fhsize; |
497 | 19.0k | zfhPtr->headerSize = (U32)fhsize; |
498 | 19.0k | } |
499 | | |
500 | 0 | { BYTE const fhdByte = ip[minInputSize-1]; |
501 | 19.0k | size_t pos = minInputSize; |
502 | 19.0k | U32 const dictIDSizeCode = fhdByte&3; |
503 | 19.0k | U32 const checksumFlag = (fhdByte>>2)&1; |
504 | 19.0k | U32 const singleSegment = (fhdByte>>5)&1; |
505 | 19.0k | U32 const fcsID = fhdByte>>6; |
506 | 19.0k | U64 windowSize = 0; |
507 | 19.0k | U32 dictID = 0; |
508 | 19.0k | U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN; |
509 | 19.0k | RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported, |
510 | 19.0k | "reserved bits, must be zero"); |
511 | | |
512 | 19.0k | if (!singleSegment) { |
513 | 10.7k | BYTE const wlByte = ip[pos++]; |
514 | 10.7k | U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN; |
515 | 10.7k | RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge, ""); |
516 | 10.7k | windowSize = (1ULL << windowLog); |
517 | 10.7k | windowSize += (windowSize >> 3) * (wlByte&7); |
518 | 10.7k | } |
519 | 19.0k | switch(dictIDSizeCode) |
520 | 19.0k | { |
521 | 0 | default: |
522 | 0 | assert(0); /* impossible */ |
523 | 0 | ZSTD_FALLTHROUGH; |
524 | 12.6k | case 0 : break; |
525 | 5.24k | case 1 : dictID = ip[pos]; pos++; break; |
526 | 415 | case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break; |
527 | 735 | case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break; |
528 | 19.0k | } |
529 | 19.0k | switch(fcsID) |
530 | 19.0k | { |
531 | 0 | default: |
532 | 0 | assert(0); /* impossible */ |
533 | 0 | ZSTD_FALLTHROUGH; |
534 | 13.1k | case 0 : if (singleSegment) frameContentSize = ip[pos]; break; |
535 | 1.31k | case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break; |
536 | 2.26k | case 2 : frameContentSize = MEM_readLE32(ip+pos); break; |
537 | 2.28k | case 3 : frameContentSize = MEM_readLE64(ip+pos); break; |
538 | 19.0k | } |
539 | 19.0k | if (singleSegment) windowSize = frameContentSize; |
540 | | |
541 | 19.0k | zfhPtr->frameType = ZSTD_frame; |
542 | 19.0k | zfhPtr->frameContentSize = frameContentSize; |
543 | 19.0k | zfhPtr->windowSize = windowSize; |
544 | 19.0k | zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX); |
545 | 19.0k | zfhPtr->dictID = dictID; |
546 | 19.0k | zfhPtr->checksumFlag = checksumFlag; |
547 | 19.0k | } |
548 | 0 | return 0; |
549 | 19.0k | } |
550 | | |
551 | | /** ZSTD_getFrameHeader() : |
552 | | * decode Frame Header, or require larger `srcSize`. |
553 | | * note : this function does not consume input, it only reads it. |
554 | | * @return : 0, `zfhPtr` is correctly filled, |
555 | | * >0, `srcSize` is too small, value is wanted `srcSize` amount, |
556 | | * or an error code, which can be tested using ZSTD_isError() */ |
557 | | size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize) |
558 | 0 | { |
559 | 0 | return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1); |
560 | 0 | } |
561 | | |
562 | | /** ZSTD_getFrameContentSize() : |
563 | | * compatible with legacy mode |
564 | | * @return : decompressed size of the single frame pointed to be `src` if known, otherwise |
565 | | * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined |
566 | | * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ |
567 | | unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize) |
568 | 0 | { |
569 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
570 | | if (ZSTD_isLegacy(src, srcSize)) { |
571 | | unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize); |
572 | | return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret; |
573 | | } |
574 | | #endif |
575 | 0 | { ZSTD_frameHeader zfh; |
576 | 0 | if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0) |
577 | 0 | return ZSTD_CONTENTSIZE_ERROR; |
578 | 0 | if (zfh.frameType == ZSTD_skippableFrame) { |
579 | 0 | return 0; |
580 | 0 | } else { |
581 | 0 | return zfh.frameContentSize; |
582 | 0 | } } |
583 | 0 | } |
584 | | |
585 | | static size_t readSkippableFrameSize(void const* src, size_t srcSize) |
586 | 497 | { |
587 | 497 | size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE; |
588 | 497 | U32 sizeU32; |
589 | | |
590 | 497 | RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, ""); |
591 | | |
592 | 496 | sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE); |
593 | 496 | RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32, |
594 | 496 | frameParameter_unsupported, ""); |
595 | 495 | { size_t const skippableSize = skippableHeaderSize + sizeU32; |
596 | 495 | RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong, ""); |
597 | 421 | return skippableSize; |
598 | 495 | } |
599 | 495 | } |
600 | | |
601 | | /*! ZSTD_readSkippableFrame() : |
602 | | * Retrieves content of a skippable frame, and writes it to dst buffer. |
603 | | * |
604 | | * The parameter magicVariant will receive the magicVariant that was supplied when the frame was written, |
605 | | * i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START. This can be NULL if the caller is not interested |
606 | | * in the magicVariant. |
607 | | * |
608 | | * Returns an error if destination buffer is not large enough, or if this is not a valid skippable frame. |
609 | | * |
610 | | * @return : number of bytes written or a ZSTD error. |
611 | | */ |
612 | | size_t ZSTD_readSkippableFrame(void* dst, size_t dstCapacity, |
613 | | unsigned* magicVariant, /* optional, can be NULL */ |
614 | | const void* src, size_t srcSize) |
615 | 0 | { |
616 | 0 | RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, ""); |
617 | | |
618 | 0 | { U32 const magicNumber = MEM_readLE32(src); |
619 | 0 | size_t skippableFrameSize = readSkippableFrameSize(src, srcSize); |
620 | 0 | size_t skippableContentSize = skippableFrameSize - ZSTD_SKIPPABLEHEADERSIZE; |
621 | | |
622 | | /* check input validity */ |
623 | 0 | RETURN_ERROR_IF(!ZSTD_isSkippableFrame(src, srcSize), frameParameter_unsupported, ""); |
624 | 0 | RETURN_ERROR_IF(skippableFrameSize < ZSTD_SKIPPABLEHEADERSIZE || skippableFrameSize > srcSize, srcSize_wrong, ""); |
625 | 0 | RETURN_ERROR_IF(skippableContentSize > dstCapacity, dstSize_tooSmall, ""); |
626 | | |
627 | | /* deliver payload */ |
628 | 0 | if (skippableContentSize > 0 && dst != NULL) |
629 | 0 | ZSTD_memcpy(dst, (const BYTE *)src + ZSTD_SKIPPABLEHEADERSIZE, skippableContentSize); |
630 | 0 | if (magicVariant != NULL) |
631 | 0 | *magicVariant = magicNumber - ZSTD_MAGIC_SKIPPABLE_START; |
632 | 0 | return skippableContentSize; |
633 | 0 | } |
634 | 0 | } |
635 | | |
636 | | /** ZSTD_findDecompressedSize() : |
637 | | * `srcSize` must be the exact length of some number of ZSTD compressed and/or |
638 | | * skippable frames |
639 | | * note: compatible with legacy mode |
640 | | * @return : decompressed size of the frames contained */ |
641 | | unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize) |
642 | 0 | { |
643 | 0 | unsigned long long totalDstSize = 0; |
644 | |
|
645 | 0 | while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) { |
646 | 0 | U32 const magicNumber = MEM_readLE32(src); |
647 | |
|
648 | 0 | if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
649 | 0 | size_t const skippableSize = readSkippableFrameSize(src, srcSize); |
650 | 0 | if (ZSTD_isError(skippableSize)) return ZSTD_CONTENTSIZE_ERROR; |
651 | 0 | assert(skippableSize <= srcSize); |
652 | |
|
653 | 0 | src = (const BYTE *)src + skippableSize; |
654 | 0 | srcSize -= skippableSize; |
655 | 0 | continue; |
656 | 0 | } |
657 | | |
658 | 0 | { unsigned long long const fcs = ZSTD_getFrameContentSize(src, srcSize); |
659 | 0 | if (fcs >= ZSTD_CONTENTSIZE_ERROR) return fcs; |
660 | | |
661 | 0 | if (totalDstSize + fcs < totalDstSize) |
662 | 0 | return ZSTD_CONTENTSIZE_ERROR; /* check for overflow */ |
663 | 0 | totalDstSize += fcs; |
664 | 0 | } |
665 | | /* skip to next frame */ |
666 | 0 | { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize); |
667 | 0 | if (ZSTD_isError(frameSrcSize)) return ZSTD_CONTENTSIZE_ERROR; |
668 | 0 | assert(frameSrcSize <= srcSize); |
669 | |
|
670 | 0 | src = (const BYTE *)src + frameSrcSize; |
671 | 0 | srcSize -= frameSrcSize; |
672 | 0 | } |
673 | 0 | } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ |
674 | | |
675 | 0 | if (srcSize) return ZSTD_CONTENTSIZE_ERROR; |
676 | | |
677 | 0 | return totalDstSize; |
678 | 0 | } |
679 | | |
680 | | /** ZSTD_getDecompressedSize() : |
681 | | * compatible with legacy mode |
682 | | * @return : decompressed size if known, 0 otherwise |
683 | | note : 0 can mean any of the following : |
684 | | - frame content is empty |
685 | | - decompressed size field is not present in frame header |
686 | | - frame header unknown / not supported |
687 | | - frame header not complete (`srcSize` too small) */ |
688 | | unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize) |
689 | 0 | { |
690 | 0 | unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); |
691 | 0 | ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN); |
692 | 0 | return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret; |
693 | 0 | } |
694 | | |
695 | | |
696 | | /** ZSTD_decodeFrameHeader() : |
697 | | * `headerSize` must be the size provided by ZSTD_frameHeaderSize(). |
698 | | * If multiple DDict references are enabled, also will choose the correct DDict to use. |
699 | | * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */ |
700 | | static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize) |
701 | 19.1k | { |
702 | 19.1k | size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format); |
703 | 19.1k | if (ZSTD_isError(result)) return result; /* invalid header */ |
704 | 19.0k | RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small"); |
705 | | |
706 | | /* Reference DDict requested by frame if dctx references multiple ddicts */ |
707 | 19.0k | if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts && dctx->ddictSet) { |
708 | 0 | ZSTD_DCtx_selectFrameDDict(dctx); |
709 | 0 | } |
710 | | |
711 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
712 | | /* Skip the dictID check in fuzzing mode, because it makes the search |
713 | | * harder. |
714 | | */ |
715 | | RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID), |
716 | | dictionary_wrong, ""); |
717 | | #endif |
718 | 19.0k | dctx->validateChecksum = (dctx->fParams.checksumFlag && !dctx->forceIgnoreChecksum) ? 1 : 0; |
719 | 19.0k | if (dctx->validateChecksum) XXH64_reset(&dctx->xxhState, 0); |
720 | 19.0k | dctx->processedCSize += headerSize; |
721 | 19.0k | return 0; |
722 | 19.0k | } |
723 | | |
724 | | static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret) |
725 | 0 | { |
726 | 0 | ZSTD_frameSizeInfo frameSizeInfo; |
727 | 0 | frameSizeInfo.compressedSize = ret; |
728 | 0 | frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; |
729 | 0 | return frameSizeInfo; |
730 | 0 | } |
731 | | |
732 | | static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize, ZSTD_format_e format) |
733 | 0 | { |
734 | 0 | ZSTD_frameSizeInfo frameSizeInfo; |
735 | 0 | ZSTD_memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo)); |
736 | |
|
737 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
738 | | if (format == ZSTD_f_zstd1 && ZSTD_isLegacy(src, srcSize)) |
739 | | return ZSTD_findFrameSizeInfoLegacy(src, srcSize); |
740 | | #endif |
741 | |
|
742 | 0 | if (format == ZSTD_f_zstd1 && (srcSize >= ZSTD_SKIPPABLEHEADERSIZE) |
743 | 0 | && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
744 | 0 | frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize); |
745 | 0 | assert(ZSTD_isError(frameSizeInfo.compressedSize) || |
746 | 0 | frameSizeInfo.compressedSize <= srcSize); |
747 | 0 | return frameSizeInfo; |
748 | 0 | } else { |
749 | 0 | const BYTE* ip = (const BYTE*)src; |
750 | 0 | const BYTE* const ipstart = ip; |
751 | 0 | size_t remainingSize = srcSize; |
752 | 0 | size_t nbBlocks = 0; |
753 | 0 | ZSTD_frameHeader zfh; |
754 | | |
755 | | /* Extract Frame Header */ |
756 | 0 | { size_t const ret = ZSTD_getFrameHeader_advanced(&zfh, src, srcSize, format); |
757 | 0 | if (ZSTD_isError(ret)) |
758 | 0 | return ZSTD_errorFrameSizeInfo(ret); |
759 | 0 | if (ret > 0) |
760 | 0 | return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); |
761 | 0 | } |
762 | | |
763 | 0 | ip += zfh.headerSize; |
764 | 0 | remainingSize -= zfh.headerSize; |
765 | | |
766 | | /* Iterate over each block */ |
767 | 0 | while (1) { |
768 | 0 | blockProperties_t blockProperties; |
769 | 0 | size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); |
770 | 0 | if (ZSTD_isError(cBlockSize)) |
771 | 0 | return ZSTD_errorFrameSizeInfo(cBlockSize); |
772 | | |
773 | 0 | if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) |
774 | 0 | return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); |
775 | | |
776 | 0 | ip += ZSTD_blockHeaderSize + cBlockSize; |
777 | 0 | remainingSize -= ZSTD_blockHeaderSize + cBlockSize; |
778 | 0 | nbBlocks++; |
779 | |
|
780 | 0 | if (blockProperties.lastBlock) break; |
781 | 0 | } |
782 | | |
783 | | /* Final frame content checksum */ |
784 | 0 | if (zfh.checksumFlag) { |
785 | 0 | if (remainingSize < 4) |
786 | 0 | return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); |
787 | 0 | ip += 4; |
788 | 0 | } |
789 | | |
790 | 0 | frameSizeInfo.nbBlocks = nbBlocks; |
791 | 0 | frameSizeInfo.compressedSize = (size_t)(ip - ipstart); |
792 | 0 | frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) |
793 | 0 | ? zfh.frameContentSize |
794 | 0 | : (unsigned long long)nbBlocks * zfh.blockSizeMax; |
795 | 0 | return frameSizeInfo; |
796 | 0 | } |
797 | 0 | } |
798 | | |
799 | 0 | static size_t ZSTD_findFrameCompressedSize_advanced(const void *src, size_t srcSize, ZSTD_format_e format) { |
800 | 0 | ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, format); |
801 | 0 | return frameSizeInfo.compressedSize; |
802 | 0 | } |
803 | | |
804 | | /** ZSTD_findFrameCompressedSize() : |
805 | | * See docs in zstd.h |
806 | | * Note: compatible with legacy mode */ |
807 | | size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize) |
808 | 0 | { |
809 | 0 | return ZSTD_findFrameCompressedSize_advanced(src, srcSize, ZSTD_f_zstd1); |
810 | 0 | } |
811 | | |
812 | | /** ZSTD_decompressBound() : |
813 | | * compatible with legacy mode |
814 | | * `src` must point to the start of a ZSTD frame or a skippeable frame |
815 | | * `srcSize` must be at least as large as the frame contained |
816 | | * @return : the maximum decompressed size of the compressed source |
817 | | */ |
818 | | unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize) |
819 | 0 | { |
820 | 0 | unsigned long long bound = 0; |
821 | | /* Iterate over each frame */ |
822 | 0 | while (srcSize > 0) { |
823 | 0 | ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1); |
824 | 0 | size_t const compressedSize = frameSizeInfo.compressedSize; |
825 | 0 | unsigned long long const decompressedBound = frameSizeInfo.decompressedBound; |
826 | 0 | if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR) |
827 | 0 | return ZSTD_CONTENTSIZE_ERROR; |
828 | 0 | assert(srcSize >= compressedSize); |
829 | 0 | src = (const BYTE*)src + compressedSize; |
830 | 0 | srcSize -= compressedSize; |
831 | 0 | bound += decompressedBound; |
832 | 0 | } |
833 | 0 | return bound; |
834 | 0 | } |
835 | | |
836 | | size_t ZSTD_decompressionMargin(void const* src, size_t srcSize) |
837 | 0 | { |
838 | 0 | size_t margin = 0; |
839 | 0 | unsigned maxBlockSize = 0; |
840 | | |
841 | | /* Iterate over each frame */ |
842 | 0 | while (srcSize > 0) { |
843 | 0 | ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1); |
844 | 0 | size_t const compressedSize = frameSizeInfo.compressedSize; |
845 | 0 | unsigned long long const decompressedBound = frameSizeInfo.decompressedBound; |
846 | 0 | ZSTD_frameHeader zfh; |
847 | |
|
848 | 0 | FORWARD_IF_ERROR(ZSTD_getFrameHeader(&zfh, src, srcSize), ""); |
849 | 0 | if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR) |
850 | 0 | return ERROR(corruption_detected); |
851 | | |
852 | 0 | if (zfh.frameType == ZSTD_frame) { |
853 | | /* Add the frame header to our margin */ |
854 | 0 | margin += zfh.headerSize; |
855 | | /* Add the checksum to our margin */ |
856 | 0 | margin += zfh.checksumFlag ? 4 : 0; |
857 | | /* Add 3 bytes per block */ |
858 | 0 | margin += 3 * frameSizeInfo.nbBlocks; |
859 | | |
860 | | /* Compute the max block size */ |
861 | 0 | maxBlockSize = MAX(maxBlockSize, zfh.blockSizeMax); |
862 | 0 | } else { |
863 | 0 | assert(zfh.frameType == ZSTD_skippableFrame); |
864 | | /* Add the entire skippable frame size to our margin. */ |
865 | 0 | margin += compressedSize; |
866 | 0 | } |
867 | |
|
868 | 0 | assert(srcSize >= compressedSize); |
869 | 0 | src = (const BYTE*)src + compressedSize; |
870 | 0 | srcSize -= compressedSize; |
871 | 0 | } |
872 | | |
873 | | /* Add the max block size back to the margin. */ |
874 | 0 | margin += maxBlockSize; |
875 | |
|
876 | 0 | return margin; |
877 | 0 | } |
878 | | |
879 | | /*-************************************************************* |
880 | | * Frame decoding |
881 | | ***************************************************************/ |
882 | | |
883 | | /** ZSTD_insertBlock() : |
884 | | * insert `src` block into `dctx` history. Useful to track uncompressed blocks. */ |
885 | | size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize) |
886 | 0 | { |
887 | 0 | DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize); |
888 | 0 | ZSTD_checkContinuity(dctx, blockStart, blockSize); |
889 | 0 | dctx->previousDstEnd = (const char*)blockStart + blockSize; |
890 | 0 | return blockSize; |
891 | 0 | } |
892 | | |
893 | | |
894 | | static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, |
895 | | const void* src, size_t srcSize) |
896 | 15.2M | { |
897 | 15.2M | DEBUGLOG(5, "ZSTD_copyRawBlock"); |
898 | 15.2M | RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, ""); |
899 | 15.2M | if (dst == NULL) { |
900 | 0 | if (srcSize == 0) return 0; |
901 | 0 | RETURN_ERROR(dstBuffer_null, ""); |
902 | 0 | } |
903 | 15.2M | ZSTD_memmove(dst, src, srcSize); |
904 | 15.2M | return srcSize; |
905 | 15.2M | } |
906 | | |
907 | | static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity, |
908 | | BYTE b, |
909 | | size_t regenSize) |
910 | 25.0k | { |
911 | 25.0k | RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, ""); |
912 | 25.0k | if (dst == NULL) { |
913 | 0 | if (regenSize == 0) return 0; |
914 | 0 | RETURN_ERROR(dstBuffer_null, ""); |
915 | 0 | } |
916 | 25.0k | ZSTD_memset(dst, b, regenSize); |
917 | 25.0k | return regenSize; |
918 | 25.0k | } |
919 | | |
920 | | static void ZSTD_DCtx_trace_end(ZSTD_DCtx const* dctx, U64 uncompressedSize, U64 compressedSize, unsigned streaming) |
921 | 15.1k | { |
922 | 15.1k | #if ZSTD_TRACE |
923 | 15.1k | if (dctx->traceCtx && ZSTD_trace_decompress_end != NULL) { |
924 | 0 | ZSTD_Trace trace; |
925 | 0 | ZSTD_memset(&trace, 0, sizeof(trace)); |
926 | 0 | trace.version = ZSTD_VERSION_NUMBER; |
927 | 0 | trace.streaming = streaming; |
928 | 0 | if (dctx->ddict) { |
929 | 0 | trace.dictionaryID = ZSTD_getDictID_fromDDict(dctx->ddict); |
930 | 0 | trace.dictionarySize = ZSTD_DDict_dictSize(dctx->ddict); |
931 | 0 | trace.dictionaryIsCold = dctx->ddictIsCold; |
932 | 0 | } |
933 | 0 | trace.uncompressedSize = (size_t)uncompressedSize; |
934 | 0 | trace.compressedSize = (size_t)compressedSize; |
935 | 0 | trace.dctx = dctx; |
936 | 0 | ZSTD_trace_decompress_end(dctx->traceCtx, &trace); |
937 | 0 | } |
938 | | #else |
939 | | (void)dctx; |
940 | | (void)uncompressedSize; |
941 | | (void)compressedSize; |
942 | | (void)streaming; |
943 | | #endif |
944 | 15.1k | } |
945 | | |
946 | | |
947 | | /*! ZSTD_decompressFrame() : |
948 | | * @dctx must be properly initialized |
949 | | * will update *srcPtr and *srcSizePtr, |
950 | | * to make *srcPtr progress by one frame. */ |
951 | | static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx, |
952 | | void* dst, size_t dstCapacity, |
953 | | const void** srcPtr, size_t *srcSizePtr) |
954 | 19.2k | { |
955 | 19.2k | const BYTE* const istart = (const BYTE*)(*srcPtr); |
956 | 19.2k | const BYTE* ip = istart; |
957 | 19.2k | BYTE* const ostart = (BYTE*)dst; |
958 | 19.2k | BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart; |
959 | 19.2k | BYTE* op = ostart; |
960 | 19.2k | size_t remainingSrcSize = *srcSizePtr; |
961 | | |
962 | 19.2k | DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr); |
963 | | |
964 | | /* check */ |
965 | 19.2k | RETURN_ERROR_IF( |
966 | 19.2k | remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize, |
967 | 19.2k | srcSize_wrong, ""); |
968 | | |
969 | | /* Frame Header */ |
970 | 19.1k | { size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal( |
971 | 19.1k | ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format); |
972 | 19.1k | if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; |
973 | 19.1k | RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize, |
974 | 19.1k | srcSize_wrong, ""); |
975 | 19.1k | FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) , ""); |
976 | 19.0k | ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize; |
977 | 19.0k | } |
978 | | |
979 | | /* Shrink the blockSizeMax if enabled */ |
980 | 19.0k | if (dctx->maxBlockSizeParam != 0) |
981 | 0 | dctx->fParams.blockSizeMax = MIN(dctx->fParams.blockSizeMax, (unsigned)dctx->maxBlockSizeParam); |
982 | | |
983 | | /* Loop on each block */ |
984 | 15.3M | while (1) { |
985 | 15.3M | BYTE* oBlockEnd = oend; |
986 | 15.3M | size_t decodedSize; |
987 | 15.3M | blockProperties_t blockProperties; |
988 | 15.3M | size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties); |
989 | 15.3M | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
990 | | |
991 | 15.3M | ip += ZSTD_blockHeaderSize; |
992 | 15.3M | remainingSrcSize -= ZSTD_blockHeaderSize; |
993 | 15.3M | RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong, ""); |
994 | | |
995 | 15.3M | if (ip >= op && ip < oBlockEnd) { |
996 | | /* We are decompressing in-place. Limit the output pointer so that we |
997 | | * don't overwrite the block that we are currently reading. This will |
998 | | * fail decompression if the input & output pointers aren't spaced |
999 | | * far enough apart. |
1000 | | * |
1001 | | * This is important to set, even when the pointers are far enough |
1002 | | * apart, because ZSTD_decompressBlock_internal() can decide to store |
1003 | | * literals in the output buffer, after the block it is decompressing. |
1004 | | * Since we don't want anything to overwrite our input, we have to tell |
1005 | | * ZSTD_decompressBlock_internal to never write past ip. |
1006 | | * |
1007 | | * See ZSTD_allocateLiteralsBuffer() for reference. |
1008 | | */ |
1009 | 0 | oBlockEnd = op + (ip - op); |
1010 | 0 | } |
1011 | | |
1012 | 15.3M | switch(blockProperties.blockType) |
1013 | 15.3M | { |
1014 | 56.4k | case bt_compressed: |
1015 | 56.4k | assert(dctx->isFrameDecompression == 1); |
1016 | 56.4k | decodedSize = ZSTD_decompressBlock_internal(dctx, op, (size_t)(oBlockEnd-op), ip, cBlockSize, not_streaming); |
1017 | 56.4k | break; |
1018 | 15.2M | case bt_raw : |
1019 | | /* Use oend instead of oBlockEnd because this function is safe to overlap. It uses memmove. */ |
1020 | 15.2M | decodedSize = ZSTD_copyRawBlock(op, (size_t)(oend-op), ip, cBlockSize); |
1021 | 15.2M | break; |
1022 | 25.0k | case bt_rle : |
1023 | 25.0k | decodedSize = ZSTD_setRleBlock(op, (size_t)(oBlockEnd-op), *ip, blockProperties.origSize); |
1024 | 25.0k | break; |
1025 | 0 | case bt_reserved : |
1026 | 0 | default: |
1027 | 0 | RETURN_ERROR(corruption_detected, "invalid block type"); |
1028 | 15.3M | } |
1029 | 15.3M | FORWARD_IF_ERROR(decodedSize, "Block decompression failure"); |
1030 | 15.3M | DEBUGLOG(5, "Decompressed block of dSize = %u", (unsigned)decodedSize); |
1031 | 15.3M | if (dctx->validateChecksum) { |
1032 | 14.0M | XXH64_update(&dctx->xxhState, op, decodedSize); |
1033 | 14.0M | } |
1034 | 15.3M | if (decodedSize) /* support dst = NULL,0 */ { |
1035 | 152k | op += decodedSize; |
1036 | 152k | } |
1037 | 15.3M | assert(ip != NULL); |
1038 | 15.3M | ip += cBlockSize; |
1039 | 15.3M | remainingSrcSize -= cBlockSize; |
1040 | 15.3M | if (blockProperties.lastBlock) break; |
1041 | 15.3M | } |
1042 | | |
1043 | 15.3k | if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) { |
1044 | 7.10k | RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize, |
1045 | 7.10k | corruption_detected, ""); |
1046 | 7.10k | } |
1047 | 15.2k | if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */ |
1048 | 1.28k | RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, ""); |
1049 | 1.28k | if (!dctx->forceIgnoreChecksum) { |
1050 | 1.28k | U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState); |
1051 | 1.28k | U32 checkRead; |
1052 | 1.28k | checkRead = MEM_readLE32(ip); |
1053 | 1.28k | RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, ""); |
1054 | 1.28k | } |
1055 | 1.19k | ip += 4; |
1056 | 1.19k | remainingSrcSize -= 4; |
1057 | 1.19k | } |
1058 | 15.1k | ZSTD_DCtx_trace_end(dctx, (U64)(op-ostart), (U64)(ip-istart), /* streaming */ 0); |
1059 | | /* Allow caller to get size read */ |
1060 | 15.1k | DEBUGLOG(4, "ZSTD_decompressFrame: decompressed frame of size %zi, consuming %zi bytes of input", op-ostart, ip - (const BYTE*)*srcPtr); |
1061 | 15.1k | *srcPtr = ip; |
1062 | 15.1k | *srcSizePtr = remainingSrcSize; |
1063 | 15.1k | return (size_t)(op-ostart); |
1064 | 15.2k | } |
1065 | | |
1066 | | static |
1067 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
1068 | | size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx, |
1069 | | void* dst, size_t dstCapacity, |
1070 | | const void* src, size_t srcSize, |
1071 | | const void* dict, size_t dictSize, |
1072 | | const ZSTD_DDict* ddict) |
1073 | 6.20k | { |
1074 | 6.20k | void* const dststart = dst; |
1075 | 6.20k | int moreThan1Frame = 0; |
1076 | | |
1077 | 6.20k | DEBUGLOG(5, "ZSTD_decompressMultiFrame"); |
1078 | 6.20k | assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */ |
1079 | | |
1080 | 6.20k | if (ddict) { |
1081 | 0 | dict = ZSTD_DDict_dictContent(ddict); |
1082 | 0 | dictSize = ZSTD_DDict_dictSize(ddict); |
1083 | 0 | } |
1084 | | |
1085 | 21.7k | while (srcSize >= ZSTD_startingInputLength(dctx->format)) { |
1086 | | |
1087 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
1088 | | if (dctx->format == ZSTD_f_zstd1 && ZSTD_isLegacy(src, srcSize)) { |
1089 | | size_t decodedSize; |
1090 | | size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); |
1091 | | if (ZSTD_isError(frameSize)) return frameSize; |
1092 | | RETURN_ERROR_IF(dctx->staticSize, memory_allocation, |
1093 | | "legacy support is not compatible with static dctx"); |
1094 | | |
1095 | | decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize); |
1096 | | if (ZSTD_isError(decodedSize)) return decodedSize; |
1097 | | |
1098 | | { |
1099 | | unsigned long long const expectedSize = ZSTD_getFrameContentSize(src, srcSize); |
1100 | | RETURN_ERROR_IF(expectedSize == ZSTD_CONTENTSIZE_ERROR, corruption_detected, "Corrupted frame header!"); |
1101 | | if (expectedSize != ZSTD_CONTENTSIZE_UNKNOWN) { |
1102 | | RETURN_ERROR_IF(expectedSize != decodedSize, corruption_detected, |
1103 | | "Frame header size does not match decoded size!"); |
1104 | | } |
1105 | | } |
1106 | | |
1107 | | assert(decodedSize <= dstCapacity); |
1108 | | dst = (BYTE*)dst + decodedSize; |
1109 | | dstCapacity -= decodedSize; |
1110 | | |
1111 | | src = (const BYTE*)src + frameSize; |
1112 | | srcSize -= frameSize; |
1113 | | |
1114 | | continue; |
1115 | | } |
1116 | | #endif |
1117 | | |
1118 | 19.7k | if (dctx->format == ZSTD_f_zstd1 && srcSize >= 4) { |
1119 | 19.7k | U32 const magicNumber = MEM_readLE32(src); |
1120 | 19.7k | DEBUGLOG(5, "reading magic number %08X", (unsigned)magicNumber); |
1121 | 19.7k | if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
1122 | | /* skippable frame detected : skip it */ |
1123 | 497 | size_t const skippableSize = readSkippableFrameSize(src, srcSize); |
1124 | 497 | FORWARD_IF_ERROR(skippableSize, "invalid skippable frame"); |
1125 | 421 | assert(skippableSize <= srcSize); |
1126 | | |
1127 | 421 | src = (const BYTE *)src + skippableSize; |
1128 | 421 | srcSize -= skippableSize; |
1129 | 421 | continue; /* check next frame */ |
1130 | 497 | } } |
1131 | | |
1132 | 19.2k | if (ddict) { |
1133 | | /* we were called from ZSTD_decompress_usingDDict */ |
1134 | 0 | FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), ""); |
1135 | 19.2k | } else { |
1136 | | /* this will initialize correctly with no dict if dict == NULL, so |
1137 | | * use this in all cases but ddict */ |
1138 | 19.2k | FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), ""); |
1139 | 19.2k | } |
1140 | 19.2k | ZSTD_checkContinuity(dctx, dst, dstCapacity); |
1141 | | |
1142 | 19.2k | { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity, |
1143 | 19.2k | &src, &srcSize); |
1144 | 19.2k | RETURN_ERROR_IF( |
1145 | 19.2k | (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown) |
1146 | 19.2k | && (moreThan1Frame==1), |
1147 | 19.2k | srcSize_wrong, |
1148 | 19.2k | "At least one frame successfully completed, " |
1149 | 19.2k | "but following bytes are garbage: " |
1150 | 19.2k | "it's more likely to be a srcSize error, " |
1151 | 19.2k | "specifying more input bytes than size of frame(s). " |
1152 | 19.2k | "Note: one could be unlucky, it might be a corruption error instead, " |
1153 | 19.2k | "happening right at the place where we expect zstd magic bytes. " |
1154 | 19.2k | "But this is _much_ less likely than a srcSize field error."); |
1155 | 19.2k | if (ZSTD_isError(res)) return res; |
1156 | 15.1k | assert(res <= dstCapacity); |
1157 | 15.1k | if (res != 0) |
1158 | 7.33k | dst = (BYTE*)dst + res; |
1159 | 15.1k | dstCapacity -= res; |
1160 | 15.1k | } |
1161 | 0 | moreThan1Frame = 1; |
1162 | 15.1k | } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ |
1163 | | |
1164 | 1.99k | RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed"); |
1165 | | |
1166 | 1.97k | return (size_t)((BYTE*)dst - (BYTE*)dststart); |
1167 | 1.99k | } |
1168 | | |
1169 | | size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
1170 | | void* dst, size_t dstCapacity, |
1171 | | const void* src, size_t srcSize, |
1172 | | const void* dict, size_t dictSize) |
1173 | 0 | { |
1174 | 0 | return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL); |
1175 | 0 | } |
1176 | | |
1177 | | |
1178 | | static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx) |
1179 | 6.20k | { |
1180 | 6.20k | switch (dctx->dictUses) { |
1181 | 0 | default: |
1182 | 0 | assert(0 /* Impossible */); |
1183 | 0 | ZSTD_FALLTHROUGH; |
1184 | 6.20k | case ZSTD_dont_use: |
1185 | 6.20k | ZSTD_clearDict(dctx); |
1186 | 6.20k | return NULL; |
1187 | 0 | case ZSTD_use_indefinitely: |
1188 | 0 | return dctx->ddict; |
1189 | 0 | case ZSTD_use_once: |
1190 | 0 | dctx->dictUses = ZSTD_dont_use; |
1191 | 0 | return dctx->ddict; |
1192 | 6.20k | } |
1193 | 6.20k | } |
1194 | | |
1195 | | size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1196 | 6.20k | { |
1197 | 6.20k | return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx)); |
1198 | 6.20k | } |
1199 | | |
1200 | | |
1201 | | size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1202 | 6.20k | { |
1203 | 6.20k | #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1) |
1204 | 6.20k | size_t regenSize; |
1205 | 6.20k | ZSTD_DCtx* const dctx = ZSTD_createDCtx_internal(ZSTD_defaultCMem); |
1206 | 6.20k | RETURN_ERROR_IF(dctx==NULL, memory_allocation, "NULL pointer!"); |
1207 | 6.20k | regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize); |
1208 | 6.20k | ZSTD_freeDCtx(dctx); |
1209 | 6.20k | return regenSize; |
1210 | | #else /* stack mode */ |
1211 | | ZSTD_DCtx dctx; |
1212 | | ZSTD_initDCtx_internal(&dctx); |
1213 | | return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize); |
1214 | | #endif |
1215 | 6.20k | } |
1216 | | |
1217 | | |
1218 | | /*-************************************** |
1219 | | * Advanced Streaming Decompression API |
1220 | | * Bufferless and synchronous |
1221 | | ****************************************/ |
1222 | 0 | size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; } |
1223 | | |
1224 | | /** |
1225 | | * Similar to ZSTD_nextSrcSizeToDecompress(), but when a block input can be streamed, we |
1226 | | * allow taking a partial block as the input. Currently only raw uncompressed blocks can |
1227 | | * be streamed. |
1228 | | * |
1229 | | * For blocks that can be streamed, this allows us to reduce the latency until we produce |
1230 | | * output, and avoid copying the input. |
1231 | | * |
1232 | | * @param inputSize - The total amount of input that the caller currently has. |
1233 | | */ |
1234 | 0 | static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t inputSize) { |
1235 | 0 | if (!(dctx->stage == ZSTDds_decompressBlock || dctx->stage == ZSTDds_decompressLastBlock)) |
1236 | 0 | return dctx->expected; |
1237 | 0 | if (dctx->bType != bt_raw) |
1238 | 0 | return dctx->expected; |
1239 | 0 | return BOUNDED(1, inputSize, dctx->expected); |
1240 | 0 | } |
1241 | | |
1242 | 0 | ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) { |
1243 | 0 | switch(dctx->stage) |
1244 | 0 | { |
1245 | 0 | default: /* should not happen */ |
1246 | 0 | assert(0); |
1247 | 0 | ZSTD_FALLTHROUGH; |
1248 | 0 | case ZSTDds_getFrameHeaderSize: |
1249 | 0 | ZSTD_FALLTHROUGH; |
1250 | 0 | case ZSTDds_decodeFrameHeader: |
1251 | 0 | return ZSTDnit_frameHeader; |
1252 | 0 | case ZSTDds_decodeBlockHeader: |
1253 | 0 | return ZSTDnit_blockHeader; |
1254 | 0 | case ZSTDds_decompressBlock: |
1255 | 0 | return ZSTDnit_block; |
1256 | 0 | case ZSTDds_decompressLastBlock: |
1257 | 0 | return ZSTDnit_lastBlock; |
1258 | 0 | case ZSTDds_checkChecksum: |
1259 | 0 | return ZSTDnit_checksum; |
1260 | 0 | case ZSTDds_decodeSkippableHeader: |
1261 | 0 | ZSTD_FALLTHROUGH; |
1262 | 0 | case ZSTDds_skipFrame: |
1263 | 0 | return ZSTDnit_skippableFrame; |
1264 | 0 | } |
1265 | 0 | } |
1266 | | |
1267 | 0 | static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; } |
1268 | | |
1269 | | /** ZSTD_decompressContinue() : |
1270 | | * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress()) |
1271 | | * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity) |
1272 | | * or an error code, which can be tested using ZSTD_isError() */ |
1273 | | size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1274 | 0 | { |
1275 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize); |
1276 | | /* Sanity check */ |
1277 | 0 | RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed"); |
1278 | 0 | ZSTD_checkContinuity(dctx, dst, dstCapacity); |
1279 | |
|
1280 | 0 | dctx->processedCSize += srcSize; |
1281 | |
|
1282 | 0 | switch (dctx->stage) |
1283 | 0 | { |
1284 | 0 | case ZSTDds_getFrameHeaderSize : |
1285 | 0 | assert(src != NULL); |
1286 | 0 | if (dctx->format == ZSTD_f_zstd1) { /* allows header */ |
1287 | 0 | assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */ |
1288 | 0 | if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ |
1289 | 0 | ZSTD_memcpy(dctx->headerBuffer, src, srcSize); |
1290 | 0 | dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */ |
1291 | 0 | dctx->stage = ZSTDds_decodeSkippableHeader; |
1292 | 0 | return 0; |
1293 | 0 | } } |
1294 | 0 | dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format); |
1295 | 0 | if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize; |
1296 | 0 | ZSTD_memcpy(dctx->headerBuffer, src, srcSize); |
1297 | 0 | dctx->expected = dctx->headerSize - srcSize; |
1298 | 0 | dctx->stage = ZSTDds_decodeFrameHeader; |
1299 | 0 | return 0; |
1300 | | |
1301 | 0 | case ZSTDds_decodeFrameHeader: |
1302 | 0 | assert(src != NULL); |
1303 | 0 | ZSTD_memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize); |
1304 | 0 | FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), ""); |
1305 | 0 | dctx->expected = ZSTD_blockHeaderSize; |
1306 | 0 | dctx->stage = ZSTDds_decodeBlockHeader; |
1307 | 0 | return 0; |
1308 | | |
1309 | 0 | case ZSTDds_decodeBlockHeader: |
1310 | 0 | { blockProperties_t bp; |
1311 | 0 | size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); |
1312 | 0 | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
1313 | 0 | RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, "Block Size Exceeds Maximum"); |
1314 | 0 | dctx->expected = cBlockSize; |
1315 | 0 | dctx->bType = bp.blockType; |
1316 | 0 | dctx->rleSize = bp.origSize; |
1317 | 0 | if (cBlockSize) { |
1318 | 0 | dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock; |
1319 | 0 | return 0; |
1320 | 0 | } |
1321 | | /* empty block */ |
1322 | 0 | if (bp.lastBlock) { |
1323 | 0 | if (dctx->fParams.checksumFlag) { |
1324 | 0 | dctx->expected = 4; |
1325 | 0 | dctx->stage = ZSTDds_checkChecksum; |
1326 | 0 | } else { |
1327 | 0 | dctx->expected = 0; /* end of frame */ |
1328 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1329 | 0 | } |
1330 | 0 | } else { |
1331 | 0 | dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */ |
1332 | 0 | dctx->stage = ZSTDds_decodeBlockHeader; |
1333 | 0 | } |
1334 | 0 | return 0; |
1335 | 0 | } |
1336 | | |
1337 | 0 | case ZSTDds_decompressLastBlock: |
1338 | 0 | case ZSTDds_decompressBlock: |
1339 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock"); |
1340 | 0 | { size_t rSize; |
1341 | 0 | switch(dctx->bType) |
1342 | 0 | { |
1343 | 0 | case bt_compressed: |
1344 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed"); |
1345 | 0 | assert(dctx->isFrameDecompression == 1); |
1346 | 0 | rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, is_streaming); |
1347 | 0 | dctx->expected = 0; /* Streaming not supported */ |
1348 | 0 | break; |
1349 | 0 | case bt_raw : |
1350 | 0 | assert(srcSize <= dctx->expected); |
1351 | 0 | rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize); |
1352 | 0 | FORWARD_IF_ERROR(rSize, "ZSTD_copyRawBlock failed"); |
1353 | 0 | assert(rSize == srcSize); |
1354 | 0 | dctx->expected -= rSize; |
1355 | 0 | break; |
1356 | 0 | case bt_rle : |
1357 | 0 | rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize); |
1358 | 0 | dctx->expected = 0; /* Streaming not supported */ |
1359 | 0 | break; |
1360 | 0 | case bt_reserved : /* should never happen */ |
1361 | 0 | default: |
1362 | 0 | RETURN_ERROR(corruption_detected, "invalid block type"); |
1363 | 0 | } |
1364 | 0 | FORWARD_IF_ERROR(rSize, ""); |
1365 | 0 | RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum"); |
1366 | 0 | DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize); |
1367 | 0 | dctx->decodedSize += rSize; |
1368 | 0 | if (dctx->validateChecksum) XXH64_update(&dctx->xxhState, dst, rSize); |
1369 | 0 | dctx->previousDstEnd = (char*)dst + rSize; |
1370 | | |
1371 | | /* Stay on the same stage until we are finished streaming the block. */ |
1372 | 0 | if (dctx->expected > 0) { |
1373 | 0 | return rSize; |
1374 | 0 | } |
1375 | | |
1376 | 0 | if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */ |
1377 | 0 | DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize); |
1378 | 0 | RETURN_ERROR_IF( |
1379 | 0 | dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN |
1380 | 0 | && dctx->decodedSize != dctx->fParams.frameContentSize, |
1381 | 0 | corruption_detected, ""); |
1382 | 0 | if (dctx->fParams.checksumFlag) { /* another round for frame checksum */ |
1383 | 0 | dctx->expected = 4; |
1384 | 0 | dctx->stage = ZSTDds_checkChecksum; |
1385 | 0 | } else { |
1386 | 0 | ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1); |
1387 | 0 | dctx->expected = 0; /* ends here */ |
1388 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1389 | 0 | } |
1390 | 0 | } else { |
1391 | 0 | dctx->stage = ZSTDds_decodeBlockHeader; |
1392 | 0 | dctx->expected = ZSTD_blockHeaderSize; |
1393 | 0 | } |
1394 | 0 | return rSize; |
1395 | 0 | } |
1396 | | |
1397 | 0 | case ZSTDds_checkChecksum: |
1398 | 0 | assert(srcSize == 4); /* guaranteed by dctx->expected */ |
1399 | 0 | { |
1400 | 0 | if (dctx->validateChecksum) { |
1401 | 0 | U32 const h32 = (U32)XXH64_digest(&dctx->xxhState); |
1402 | 0 | U32 const check32 = MEM_readLE32(src); |
1403 | 0 | DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32); |
1404 | 0 | RETURN_ERROR_IF(check32 != h32, checksum_wrong, ""); |
1405 | 0 | } |
1406 | 0 | ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1); |
1407 | 0 | dctx->expected = 0; |
1408 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1409 | 0 | return 0; |
1410 | 0 | } |
1411 | | |
1412 | 0 | case ZSTDds_decodeSkippableHeader: |
1413 | 0 | assert(src != NULL); |
1414 | 0 | assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE); |
1415 | 0 | assert(dctx->format != ZSTD_f_zstd1_magicless); |
1416 | 0 | ZSTD_memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */ |
1417 | 0 | dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */ |
1418 | 0 | dctx->stage = ZSTDds_skipFrame; |
1419 | 0 | return 0; |
1420 | | |
1421 | 0 | case ZSTDds_skipFrame: |
1422 | 0 | dctx->expected = 0; |
1423 | 0 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1424 | 0 | return 0; |
1425 | | |
1426 | 0 | default: |
1427 | 0 | assert(0); /* impossible */ |
1428 | 0 | RETURN_ERROR(GENERIC, "impossible to reach"); /* some compilers require default to do something */ |
1429 | 0 | } |
1430 | 0 | } |
1431 | | |
1432 | | |
1433 | | static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1434 | 0 | { |
1435 | 0 | dctx->dictEnd = dctx->previousDstEnd; |
1436 | 0 | dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart)); |
1437 | 0 | dctx->prefixStart = dict; |
1438 | 0 | dctx->previousDstEnd = (const char*)dict + dictSize; |
1439 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
1440 | 0 | dctx->dictContentBeginForFuzzing = dctx->prefixStart; |
1441 | 0 | dctx->dictContentEndForFuzzing = dctx->previousDstEnd; |
1442 | 0 | #endif |
1443 | 0 | return 0; |
1444 | 0 | } |
1445 | | |
1446 | | /*! ZSTD_loadDEntropy() : |
1447 | | * dict : must point at beginning of a valid zstd dictionary. |
1448 | | * @return : size of entropy tables read */ |
1449 | | size_t |
1450 | | ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy, |
1451 | | const void* const dict, size_t const dictSize) |
1452 | 0 | { |
1453 | 0 | const BYTE* dictPtr = (const BYTE*)dict; |
1454 | 0 | const BYTE* const dictEnd = dictPtr + dictSize; |
1455 | |
|
1456 | 0 | RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted, "dict is too small"); |
1457 | 0 | assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */ |
1458 | 0 | dictPtr += 8; /* skip header = magic + dictID */ |
1459 | |
|
1460 | 0 | ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable)); |
1461 | 0 | ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable)); |
1462 | 0 | ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE); |
1463 | 0 | { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */ |
1464 | 0 | size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable); |
1465 | | #ifdef HUF_FORCE_DECOMPRESS_X1 |
1466 | | /* in minimal huffman, we always use X1 variants */ |
1467 | | size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable, |
1468 | | dictPtr, dictEnd - dictPtr, |
1469 | | workspace, workspaceSize, /* flags */ 0); |
1470 | | #else |
1471 | 0 | size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable, |
1472 | 0 | dictPtr, (size_t)(dictEnd - dictPtr), |
1473 | 0 | workspace, workspaceSize, /* flags */ 0); |
1474 | 0 | #endif |
1475 | 0 | RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, ""); |
1476 | 0 | dictPtr += hSize; |
1477 | 0 | } |
1478 | | |
1479 | 0 | { short offcodeNCount[MaxOff+1]; |
1480 | 0 | unsigned offcodeMaxValue = MaxOff, offcodeLog; |
1481 | 0 | size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (size_t)(dictEnd-dictPtr)); |
1482 | 0 | RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, ""); |
1483 | 0 | RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, ""); |
1484 | 0 | RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, ""); |
1485 | 0 | ZSTD_buildFSETable( entropy->OFTable, |
1486 | 0 | offcodeNCount, offcodeMaxValue, |
1487 | 0 | OF_base, OF_bits, |
1488 | 0 | offcodeLog, |
1489 | 0 | entropy->workspace, sizeof(entropy->workspace), |
1490 | 0 | /* bmi2 */0); |
1491 | 0 | dictPtr += offcodeHeaderSize; |
1492 | 0 | } |
1493 | | |
1494 | 0 | { short matchlengthNCount[MaxML+1]; |
1495 | 0 | unsigned matchlengthMaxValue = MaxML, matchlengthLog; |
1496 | 0 | size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (size_t)(dictEnd-dictPtr)); |
1497 | 0 | RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, ""); |
1498 | 0 | RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, ""); |
1499 | 0 | RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, ""); |
1500 | 0 | ZSTD_buildFSETable( entropy->MLTable, |
1501 | 0 | matchlengthNCount, matchlengthMaxValue, |
1502 | 0 | ML_base, ML_bits, |
1503 | 0 | matchlengthLog, |
1504 | 0 | entropy->workspace, sizeof(entropy->workspace), |
1505 | 0 | /* bmi2 */ 0); |
1506 | 0 | dictPtr += matchlengthHeaderSize; |
1507 | 0 | } |
1508 | | |
1509 | 0 | { short litlengthNCount[MaxLL+1]; |
1510 | 0 | unsigned litlengthMaxValue = MaxLL, litlengthLog; |
1511 | 0 | size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (size_t)(dictEnd-dictPtr)); |
1512 | 0 | RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, ""); |
1513 | 0 | RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, ""); |
1514 | 0 | RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, ""); |
1515 | 0 | ZSTD_buildFSETable( entropy->LLTable, |
1516 | 0 | litlengthNCount, litlengthMaxValue, |
1517 | 0 | LL_base, LL_bits, |
1518 | 0 | litlengthLog, |
1519 | 0 | entropy->workspace, sizeof(entropy->workspace), |
1520 | 0 | /* bmi2 */ 0); |
1521 | 0 | dictPtr += litlengthHeaderSize; |
1522 | 0 | } |
1523 | | |
1524 | 0 | RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted, ""); |
1525 | 0 | { int i; |
1526 | 0 | size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12)); |
1527 | 0 | for (i=0; i<3; i++) { |
1528 | 0 | U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4; |
1529 | 0 | RETURN_ERROR_IF(rep==0 || rep > dictContentSize, |
1530 | 0 | dictionary_corrupted, ""); |
1531 | 0 | entropy->rep[i] = rep; |
1532 | 0 | } } |
1533 | | |
1534 | 0 | return (size_t)(dictPtr - (const BYTE*)dict); |
1535 | 0 | } |
1536 | | |
1537 | | static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1538 | 0 | { |
1539 | 0 | if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize); |
1540 | 0 | { U32 const magic = MEM_readLE32(dict); |
1541 | 0 | if (magic != ZSTD_MAGIC_DICTIONARY) { |
1542 | 0 | return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */ |
1543 | 0 | } } |
1544 | 0 | dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE); |
1545 | | |
1546 | | /* load entropy tables */ |
1547 | 0 | { size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize); |
1548 | 0 | RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted, ""); |
1549 | 0 | dict = (const char*)dict + eSize; |
1550 | 0 | dictSize -= eSize; |
1551 | 0 | } |
1552 | 0 | dctx->litEntropy = dctx->fseEntropy = 1; |
1553 | | |
1554 | | /* reference dictionary content */ |
1555 | 0 | return ZSTD_refDictContent(dctx, dict, dictSize); |
1556 | 0 | } |
1557 | | |
1558 | | size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) |
1559 | 19.2k | { |
1560 | 19.2k | assert(dctx != NULL); |
1561 | 19.2k | #if ZSTD_TRACE |
1562 | 19.2k | dctx->traceCtx = (ZSTD_trace_decompress_begin != NULL) ? ZSTD_trace_decompress_begin(dctx) : 0; |
1563 | 19.2k | #endif |
1564 | 19.2k | dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */ |
1565 | 19.2k | dctx->stage = ZSTDds_getFrameHeaderSize; |
1566 | 19.2k | dctx->processedCSize = 0; |
1567 | 19.2k | dctx->decodedSize = 0; |
1568 | 19.2k | dctx->previousDstEnd = NULL; |
1569 | 19.2k | dctx->prefixStart = NULL; |
1570 | 19.2k | dctx->virtualStart = NULL; |
1571 | 19.2k | dctx->dictEnd = NULL; |
1572 | 19.2k | dctx->entropy.hufTable[0] = (HUF_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001); /* cover both little and big endian */ |
1573 | 19.2k | dctx->litEntropy = dctx->fseEntropy = 0; |
1574 | 19.2k | dctx->dictID = 0; |
1575 | 19.2k | dctx->bType = bt_reserved; |
1576 | 19.2k | dctx->isFrameDecompression = 1; |
1577 | 19.2k | ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue)); |
1578 | 19.2k | ZSTD_memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */ |
1579 | 19.2k | dctx->LLTptr = dctx->entropy.LLTable; |
1580 | 19.2k | dctx->MLTptr = dctx->entropy.MLTable; |
1581 | 19.2k | dctx->OFTptr = dctx->entropy.OFTable; |
1582 | 19.2k | dctx->HUFptr = dctx->entropy.hufTable; |
1583 | 19.2k | return 0; |
1584 | 19.2k | } |
1585 | | |
1586 | | size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1587 | 19.2k | { |
1588 | 19.2k | FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , ""); |
1589 | 19.2k | if (dict && dictSize) |
1590 | 0 | RETURN_ERROR_IF( |
1591 | 19.2k | ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)), |
1592 | 19.2k | dictionary_corrupted, ""); |
1593 | 19.2k | return 0; |
1594 | 19.2k | } |
1595 | | |
1596 | | |
1597 | | /* ====== ZSTD_DDict ====== */ |
1598 | | |
1599 | | size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) |
1600 | 0 | { |
1601 | 0 | DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict"); |
1602 | 0 | assert(dctx != NULL); |
1603 | 0 | if (ddict) { |
1604 | 0 | const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict); |
1605 | 0 | size_t const dictSize = ZSTD_DDict_dictSize(ddict); |
1606 | 0 | const void* const dictEnd = dictStart + dictSize; |
1607 | 0 | dctx->ddictIsCold = (dctx->dictEnd != dictEnd); |
1608 | 0 | DEBUGLOG(4, "DDict is %s", |
1609 | 0 | dctx->ddictIsCold ? "~cold~" : "hot!"); |
1610 | 0 | } |
1611 | 0 | FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , ""); |
1612 | 0 | if (ddict) { /* NULL ddict is equivalent to no dictionary */ |
1613 | 0 | ZSTD_copyDDictParameters(dctx, ddict); |
1614 | 0 | } |
1615 | 0 | return 0; |
1616 | 0 | } |
1617 | | |
1618 | | /*! ZSTD_getDictID_fromDict() : |
1619 | | * Provides the dictID stored within dictionary. |
1620 | | * if @return == 0, the dictionary is not conformant with Zstandard specification. |
1621 | | * It can still be loaded, but as a content-only dictionary. */ |
1622 | | unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize) |
1623 | 0 | { |
1624 | 0 | if (dictSize < 8) return 0; |
1625 | 0 | if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0; |
1626 | 0 | return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE); |
1627 | 0 | } |
1628 | | |
1629 | | /*! ZSTD_getDictID_fromFrame() : |
1630 | | * Provides the dictID required to decompress frame stored within `src`. |
1631 | | * If @return == 0, the dictID could not be decoded. |
1632 | | * This could for one of the following reasons : |
1633 | | * - The frame does not require a dictionary (most common case). |
1634 | | * - The frame was built with dictID intentionally removed. |
1635 | | * Needed dictionary is a hidden piece of information. |
1636 | | * Note : this use case also happens when using a non-conformant dictionary. |
1637 | | * - `srcSize` is too small, and as a result, frame header could not be decoded. |
1638 | | * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`. |
1639 | | * - This is not a Zstandard frame. |
1640 | | * When identifying the exact failure cause, it's possible to use |
1641 | | * ZSTD_getFrameHeader(), which will provide a more precise error code. */ |
1642 | | unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize) |
1643 | 0 | { |
1644 | 0 | ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0, 0, 0 }; |
1645 | 0 | size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize); |
1646 | 0 | if (ZSTD_isError(hError)) return 0; |
1647 | 0 | return zfp.dictID; |
1648 | 0 | } |
1649 | | |
1650 | | |
1651 | | /*! ZSTD_decompress_usingDDict() : |
1652 | | * Decompression using a pre-digested Dictionary |
1653 | | * Use dictionary without significant overhead. */ |
1654 | | size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, |
1655 | | void* dst, size_t dstCapacity, |
1656 | | const void* src, size_t srcSize, |
1657 | | const ZSTD_DDict* ddict) |
1658 | 6.20k | { |
1659 | | /* pass content and size in case legacy frames are encountered */ |
1660 | 6.20k | return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, |
1661 | 6.20k | NULL, 0, |
1662 | 6.20k | ddict); |
1663 | 6.20k | } |
1664 | | |
1665 | | |
1666 | | /*===================================== |
1667 | | * Streaming decompression |
1668 | | *====================================*/ |
1669 | | |
1670 | | ZSTD_DStream* ZSTD_createDStream(void) |
1671 | 0 | { |
1672 | 0 | DEBUGLOG(3, "ZSTD_createDStream"); |
1673 | 0 | return ZSTD_createDCtx_internal(ZSTD_defaultCMem); |
1674 | 0 | } |
1675 | | |
1676 | | ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize) |
1677 | 0 | { |
1678 | 0 | return ZSTD_initStaticDCtx(workspace, workspaceSize); |
1679 | 0 | } |
1680 | | |
1681 | | ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem) |
1682 | 0 | { |
1683 | 0 | return ZSTD_createDCtx_internal(customMem); |
1684 | 0 | } |
1685 | | |
1686 | | size_t ZSTD_freeDStream(ZSTD_DStream* zds) |
1687 | 0 | { |
1688 | 0 | return ZSTD_freeDCtx(zds); |
1689 | 0 | } |
1690 | | |
1691 | | |
1692 | | /* *** Initialization *** */ |
1693 | | |
1694 | 0 | size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; } |
1695 | 0 | size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; } |
1696 | | |
1697 | | size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, |
1698 | | const void* dict, size_t dictSize, |
1699 | | ZSTD_dictLoadMethod_e dictLoadMethod, |
1700 | | ZSTD_dictContentType_e dictContentType) |
1701 | 0 | { |
1702 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1703 | 0 | ZSTD_clearDict(dctx); |
1704 | 0 | if (dict && dictSize != 0) { |
1705 | 0 | dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem); |
1706 | 0 | RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, "NULL pointer!"); |
1707 | 0 | dctx->ddict = dctx->ddictLocal; |
1708 | 0 | dctx->dictUses = ZSTD_use_indefinitely; |
1709 | 0 | } |
1710 | 0 | return 0; |
1711 | 0 | } |
1712 | | |
1713 | | size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1714 | 0 | { |
1715 | 0 | return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto); |
1716 | 0 | } |
1717 | | |
1718 | | size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1719 | 0 | { |
1720 | 0 | return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto); |
1721 | 0 | } |
1722 | | |
1723 | | size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType) |
1724 | 0 | { |
1725 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType), ""); |
1726 | 0 | dctx->dictUses = ZSTD_use_once; |
1727 | 0 | return 0; |
1728 | 0 | } |
1729 | | |
1730 | | size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize) |
1731 | 0 | { |
1732 | 0 | return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent); |
1733 | 0 | } |
1734 | | |
1735 | | |
1736 | | /* ZSTD_initDStream_usingDict() : |
1737 | | * return : expected size, aka ZSTD_startingInputLength(). |
1738 | | * this function cannot fail */ |
1739 | | size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize) |
1740 | 0 | { |
1741 | 0 | DEBUGLOG(4, "ZSTD_initDStream_usingDict"); |
1742 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) , ""); |
1743 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) , ""); |
1744 | 0 | return ZSTD_startingInputLength(zds->format); |
1745 | 0 | } |
1746 | | |
1747 | | /* note : this variant can't fail */ |
1748 | | size_t ZSTD_initDStream(ZSTD_DStream* zds) |
1749 | 0 | { |
1750 | 0 | DEBUGLOG(4, "ZSTD_initDStream"); |
1751 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_reset(zds, ZSTD_reset_session_only), ""); |
1752 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_refDDict(zds, NULL), ""); |
1753 | 0 | return ZSTD_startingInputLength(zds->format); |
1754 | 0 | } |
1755 | | |
1756 | | /* ZSTD_initDStream_usingDDict() : |
1757 | | * ddict will just be referenced, and must outlive decompression session |
1758 | | * this function cannot fail */ |
1759 | | size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict) |
1760 | 0 | { |
1761 | 0 | DEBUGLOG(4, "ZSTD_initDStream_usingDDict"); |
1762 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) , ""); |
1763 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) , ""); |
1764 | 0 | return ZSTD_startingInputLength(dctx->format); |
1765 | 0 | } |
1766 | | |
1767 | | /* ZSTD_resetDStream() : |
1768 | | * return : expected size, aka ZSTD_startingInputLength(). |
1769 | | * this function cannot fail */ |
1770 | | size_t ZSTD_resetDStream(ZSTD_DStream* dctx) |
1771 | 0 | { |
1772 | 0 | DEBUGLOG(4, "ZSTD_resetDStream"); |
1773 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only), ""); |
1774 | 0 | return ZSTD_startingInputLength(dctx->format); |
1775 | 0 | } |
1776 | | |
1777 | | |
1778 | | size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) |
1779 | 0 | { |
1780 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1781 | 0 | ZSTD_clearDict(dctx); |
1782 | 0 | if (ddict) { |
1783 | 0 | dctx->ddict = ddict; |
1784 | 0 | dctx->dictUses = ZSTD_use_indefinitely; |
1785 | 0 | if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts) { |
1786 | 0 | if (dctx->ddictSet == NULL) { |
1787 | 0 | dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem); |
1788 | 0 | if (!dctx->ddictSet) { |
1789 | 0 | RETURN_ERROR(memory_allocation, "Failed to allocate memory for hash set!"); |
1790 | 0 | } |
1791 | 0 | } |
1792 | 0 | assert(!dctx->staticSize); /* Impossible: ddictSet cannot have been allocated if static dctx */ |
1793 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem), ""); |
1794 | 0 | } |
1795 | 0 | } |
1796 | 0 | return 0; |
1797 | 0 | } |
1798 | | |
1799 | | /* ZSTD_DCtx_setMaxWindowSize() : |
1800 | | * note : no direct equivalence in ZSTD_DCtx_setParameter, |
1801 | | * since this version sets windowSize, and the other sets windowLog */ |
1802 | | size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize) |
1803 | 0 | { |
1804 | 0 | ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax); |
1805 | 0 | size_t const min = (size_t)1 << bounds.lowerBound; |
1806 | 0 | size_t const max = (size_t)1 << bounds.upperBound; |
1807 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1808 | 0 | RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound, ""); |
1809 | 0 | RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound, ""); |
1810 | 0 | dctx->maxWindowSize = maxWindowSize; |
1811 | 0 | return 0; |
1812 | 0 | } |
1813 | | |
1814 | | size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format) |
1815 | 0 | { |
1816 | 0 | return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (int)format); |
1817 | 0 | } |
1818 | | |
1819 | | ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam) |
1820 | 0 | { |
1821 | 0 | ZSTD_bounds bounds = { 0, 0, 0 }; |
1822 | 0 | switch(dParam) { |
1823 | 0 | case ZSTD_d_windowLogMax: |
1824 | 0 | bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN; |
1825 | 0 | bounds.upperBound = ZSTD_WINDOWLOG_MAX; |
1826 | 0 | return bounds; |
1827 | 0 | case ZSTD_d_format: |
1828 | 0 | bounds.lowerBound = (int)ZSTD_f_zstd1; |
1829 | 0 | bounds.upperBound = (int)ZSTD_f_zstd1_magicless; |
1830 | 0 | ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless); |
1831 | 0 | return bounds; |
1832 | 0 | case ZSTD_d_stableOutBuffer: |
1833 | 0 | bounds.lowerBound = (int)ZSTD_bm_buffered; |
1834 | 0 | bounds.upperBound = (int)ZSTD_bm_stable; |
1835 | 0 | return bounds; |
1836 | 0 | case ZSTD_d_forceIgnoreChecksum: |
1837 | 0 | bounds.lowerBound = (int)ZSTD_d_validateChecksum; |
1838 | 0 | bounds.upperBound = (int)ZSTD_d_ignoreChecksum; |
1839 | 0 | return bounds; |
1840 | 0 | case ZSTD_d_refMultipleDDicts: |
1841 | 0 | bounds.lowerBound = (int)ZSTD_rmd_refSingleDDict; |
1842 | 0 | bounds.upperBound = (int)ZSTD_rmd_refMultipleDDicts; |
1843 | 0 | return bounds; |
1844 | 0 | case ZSTD_d_disableHuffmanAssembly: |
1845 | 0 | bounds.lowerBound = 0; |
1846 | 0 | bounds.upperBound = 1; |
1847 | 0 | return bounds; |
1848 | 0 | case ZSTD_d_maxBlockSize: |
1849 | 0 | bounds.lowerBound = ZSTD_BLOCKSIZE_MAX_MIN; |
1850 | 0 | bounds.upperBound = ZSTD_BLOCKSIZE_MAX; |
1851 | 0 | return bounds; |
1852 | | |
1853 | 0 | default:; |
1854 | 0 | } |
1855 | 0 | bounds.error = ERROR(parameter_unsupported); |
1856 | 0 | return bounds; |
1857 | 0 | } |
1858 | | |
1859 | | /* ZSTD_dParam_withinBounds: |
1860 | | * @return 1 if value is within dParam bounds, |
1861 | | * 0 otherwise */ |
1862 | | static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value) |
1863 | 0 | { |
1864 | 0 | ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam); |
1865 | 0 | if (ZSTD_isError(bounds.error)) return 0; |
1866 | 0 | if (value < bounds.lowerBound) return 0; |
1867 | 0 | if (value > bounds.upperBound) return 0; |
1868 | 0 | return 1; |
1869 | 0 | } |
1870 | | |
1871 | 0 | #define CHECK_DBOUNDS(p,v) { \ |
1872 | 0 | RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \ |
1873 | 0 | } |
1874 | | |
1875 | | size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value) |
1876 | 0 | { |
1877 | 0 | switch (param) { |
1878 | 0 | case ZSTD_d_windowLogMax: |
1879 | 0 | *value = (int)ZSTD_highbit32((U32)dctx->maxWindowSize); |
1880 | 0 | return 0; |
1881 | 0 | case ZSTD_d_format: |
1882 | 0 | *value = (int)dctx->format; |
1883 | 0 | return 0; |
1884 | 0 | case ZSTD_d_stableOutBuffer: |
1885 | 0 | *value = (int)dctx->outBufferMode; |
1886 | 0 | return 0; |
1887 | 0 | case ZSTD_d_forceIgnoreChecksum: |
1888 | 0 | *value = (int)dctx->forceIgnoreChecksum; |
1889 | 0 | return 0; |
1890 | 0 | case ZSTD_d_refMultipleDDicts: |
1891 | 0 | *value = (int)dctx->refMultipleDDicts; |
1892 | 0 | return 0; |
1893 | 0 | case ZSTD_d_disableHuffmanAssembly: |
1894 | 0 | *value = (int)dctx->disableHufAsm; |
1895 | 0 | return 0; |
1896 | 0 | case ZSTD_d_maxBlockSize: |
1897 | 0 | *value = dctx->maxBlockSizeParam; |
1898 | 0 | return 0; |
1899 | 0 | default:; |
1900 | 0 | } |
1901 | 0 | RETURN_ERROR(parameter_unsupported, ""); |
1902 | 0 | } |
1903 | | |
1904 | | size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value) |
1905 | 0 | { |
1906 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1907 | 0 | switch(dParam) { |
1908 | 0 | case ZSTD_d_windowLogMax: |
1909 | 0 | if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT; |
1910 | 0 | CHECK_DBOUNDS(ZSTD_d_windowLogMax, value); |
1911 | 0 | dctx->maxWindowSize = ((size_t)1) << value; |
1912 | 0 | return 0; |
1913 | 0 | case ZSTD_d_format: |
1914 | 0 | CHECK_DBOUNDS(ZSTD_d_format, value); |
1915 | 0 | dctx->format = (ZSTD_format_e)value; |
1916 | 0 | return 0; |
1917 | 0 | case ZSTD_d_stableOutBuffer: |
1918 | 0 | CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value); |
1919 | 0 | dctx->outBufferMode = (ZSTD_bufferMode_e)value; |
1920 | 0 | return 0; |
1921 | 0 | case ZSTD_d_forceIgnoreChecksum: |
1922 | 0 | CHECK_DBOUNDS(ZSTD_d_forceIgnoreChecksum, value); |
1923 | 0 | dctx->forceIgnoreChecksum = (ZSTD_forceIgnoreChecksum_e)value; |
1924 | 0 | return 0; |
1925 | 0 | case ZSTD_d_refMultipleDDicts: |
1926 | 0 | CHECK_DBOUNDS(ZSTD_d_refMultipleDDicts, value); |
1927 | 0 | if (dctx->staticSize != 0) { |
1928 | 0 | RETURN_ERROR(parameter_unsupported, "Static dctx does not support multiple DDicts!"); |
1929 | 0 | } |
1930 | 0 | dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value; |
1931 | 0 | return 0; |
1932 | 0 | case ZSTD_d_disableHuffmanAssembly: |
1933 | 0 | CHECK_DBOUNDS(ZSTD_d_disableHuffmanAssembly, value); |
1934 | 0 | dctx->disableHufAsm = value != 0; |
1935 | 0 | return 0; |
1936 | 0 | case ZSTD_d_maxBlockSize: |
1937 | 0 | if (value != 0) CHECK_DBOUNDS(ZSTD_d_maxBlockSize, value); |
1938 | 0 | dctx->maxBlockSizeParam = value; |
1939 | 0 | return 0; |
1940 | 0 | default:; |
1941 | 0 | } |
1942 | 0 | RETURN_ERROR(parameter_unsupported, ""); |
1943 | 0 | } |
1944 | | |
1945 | | size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset) |
1946 | 0 | { |
1947 | 0 | if ( (reset == ZSTD_reset_session_only) |
1948 | 0 | || (reset == ZSTD_reset_session_and_parameters) ) { |
1949 | 0 | dctx->streamStage = zdss_init; |
1950 | 0 | dctx->noForwardProgress = 0; |
1951 | 0 | dctx->isFrameDecompression = 1; |
1952 | 0 | } |
1953 | 0 | if ( (reset == ZSTD_reset_parameters) |
1954 | 0 | || (reset == ZSTD_reset_session_and_parameters) ) { |
1955 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1956 | 0 | ZSTD_clearDict(dctx); |
1957 | 0 | ZSTD_DCtx_resetParameters(dctx); |
1958 | 0 | } |
1959 | 0 | return 0; |
1960 | 0 | } |
1961 | | |
1962 | | |
1963 | | size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx) |
1964 | 0 | { |
1965 | 0 | return ZSTD_sizeof_DCtx(dctx); |
1966 | 0 | } |
1967 | | |
1968 | | static size_t ZSTD_decodingBufferSize_internal(unsigned long long windowSize, unsigned long long frameContentSize, size_t blockSizeMax) |
1969 | 0 | { |
1970 | 0 | size_t const blockSize = MIN((size_t)MIN(windowSize, ZSTD_BLOCKSIZE_MAX), blockSizeMax); |
1971 | | /* We need blockSize + WILDCOPY_OVERLENGTH worth of buffer so that if a block |
1972 | | * ends at windowSize + WILDCOPY_OVERLENGTH + 1 bytes, we can start writing |
1973 | | * the block at the beginning of the output buffer, and maintain a full window. |
1974 | | * |
1975 | | * We need another blockSize worth of buffer so that we can store split |
1976 | | * literals at the end of the block without overwriting the extDict window. |
1977 | | */ |
1978 | 0 | unsigned long long const neededRBSize = windowSize + (blockSize * 2) + (WILDCOPY_OVERLENGTH * 2); |
1979 | 0 | unsigned long long const neededSize = MIN(frameContentSize, neededRBSize); |
1980 | 0 | size_t const minRBSize = (size_t) neededSize; |
1981 | 0 | RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize, |
1982 | 0 | frameParameter_windowTooLarge, ""); |
1983 | 0 | return minRBSize; |
1984 | 0 | } |
1985 | | |
1986 | | size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize) |
1987 | 0 | { |
1988 | 0 | return ZSTD_decodingBufferSize_internal(windowSize, frameContentSize, ZSTD_BLOCKSIZE_MAX); |
1989 | 0 | } |
1990 | | |
1991 | | size_t ZSTD_estimateDStreamSize(size_t windowSize) |
1992 | 0 | { |
1993 | 0 | size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX); |
1994 | 0 | size_t const inBuffSize = blockSize; /* no block can be larger */ |
1995 | 0 | size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN); |
1996 | 0 | return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize; |
1997 | 0 | } |
1998 | | |
1999 | | size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize) |
2000 | 0 | { |
2001 | 0 | U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */ |
2002 | 0 | ZSTD_frameHeader zfh; |
2003 | 0 | size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize); |
2004 | 0 | if (ZSTD_isError(err)) return err; |
2005 | 0 | RETURN_ERROR_IF(err>0, srcSize_wrong, ""); |
2006 | 0 | RETURN_ERROR_IF(zfh.windowSize > windowSizeMax, |
2007 | 0 | frameParameter_windowTooLarge, ""); |
2008 | 0 | return ZSTD_estimateDStreamSize((size_t)zfh.windowSize); |
2009 | 0 | } |
2010 | | |
2011 | | |
2012 | | /* ***** Decompression ***** */ |
2013 | | |
2014 | | static int ZSTD_DCtx_isOverflow(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize) |
2015 | 0 | { |
2016 | 0 | return (zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * ZSTD_WORKSPACETOOLARGE_FACTOR; |
2017 | 0 | } |
2018 | | |
2019 | | static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize) |
2020 | 0 | { |
2021 | 0 | if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize)) |
2022 | 0 | zds->oversizedDuration++; |
2023 | 0 | else |
2024 | 0 | zds->oversizedDuration = 0; |
2025 | 0 | } |
2026 | | |
2027 | | static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream* zds) |
2028 | 0 | { |
2029 | 0 | return zds->oversizedDuration >= ZSTD_WORKSPACETOOLARGE_MAXDURATION; |
2030 | 0 | } |
2031 | | |
2032 | | /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */ |
2033 | | static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const* output) |
2034 | 0 | { |
2035 | 0 | ZSTD_outBuffer const expect = zds->expectedOutBuffer; |
2036 | | /* No requirement when ZSTD_obm_stable is not enabled. */ |
2037 | 0 | if (zds->outBufferMode != ZSTD_bm_stable) |
2038 | 0 | return 0; |
2039 | | /* Any buffer is allowed in zdss_init, this must be the same for every other call until |
2040 | | * the context is reset. |
2041 | | */ |
2042 | 0 | if (zds->streamStage == zdss_init) |
2043 | 0 | return 0; |
2044 | | /* The buffer must match our expectation exactly. */ |
2045 | 0 | if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size) |
2046 | 0 | return 0; |
2047 | 0 | RETURN_ERROR(dstBuffer_wrong, "ZSTD_d_stableOutBuffer enabled but output differs!"); |
2048 | 0 | } |
2049 | | |
2050 | | /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream() |
2051 | | * and updates the stage and the output buffer state. This call is extracted so it can be |
2052 | | * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode. |
2053 | | * NOTE: You must break after calling this function since the streamStage is modified. |
2054 | | */ |
2055 | | static size_t ZSTD_decompressContinueStream( |
2056 | | ZSTD_DStream* zds, char** op, char* oend, |
2057 | 0 | void const* src, size_t srcSize) { |
2058 | 0 | int const isSkipFrame = ZSTD_isSkipFrame(zds); |
2059 | 0 | if (zds->outBufferMode == ZSTD_bm_buffered) { |
2060 | 0 | size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart; |
2061 | 0 | size_t const decodedSize = ZSTD_decompressContinue(zds, |
2062 | 0 | zds->outBuff + zds->outStart, dstSize, src, srcSize); |
2063 | 0 | FORWARD_IF_ERROR(decodedSize, ""); |
2064 | 0 | if (!decodedSize && !isSkipFrame) { |
2065 | 0 | zds->streamStage = zdss_read; |
2066 | 0 | } else { |
2067 | 0 | zds->outEnd = zds->outStart + decodedSize; |
2068 | 0 | zds->streamStage = zdss_flush; |
2069 | 0 | } |
2070 | 0 | } else { |
2071 | | /* Write directly into the output buffer */ |
2072 | 0 | size_t const dstSize = isSkipFrame ? 0 : (size_t)(oend - *op); |
2073 | 0 | size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize); |
2074 | 0 | FORWARD_IF_ERROR(decodedSize, ""); |
2075 | 0 | *op += decodedSize; |
2076 | | /* Flushing is not needed. */ |
2077 | 0 | zds->streamStage = zdss_read; |
2078 | 0 | assert(*op <= oend); |
2079 | 0 | assert(zds->outBufferMode == ZSTD_bm_stable); |
2080 | 0 | } |
2081 | 0 | return 0; |
2082 | 0 | } |
2083 | | |
2084 | | size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input) |
2085 | 0 | { |
2086 | 0 | const char* const src = (const char*)input->src; |
2087 | 0 | const char* const istart = input->pos != 0 ? src + input->pos : src; |
2088 | 0 | const char* const iend = input->size != 0 ? src + input->size : src; |
2089 | 0 | const char* ip = istart; |
2090 | 0 | char* const dst = (char*)output->dst; |
2091 | 0 | char* const ostart = output->pos != 0 ? dst + output->pos : dst; |
2092 | 0 | char* const oend = output->size != 0 ? dst + output->size : dst; |
2093 | 0 | char* op = ostart; |
2094 | 0 | U32 someMoreWork = 1; |
2095 | |
|
2096 | 0 | DEBUGLOG(5, "ZSTD_decompressStream"); |
2097 | 0 | RETURN_ERROR_IF( |
2098 | 0 | input->pos > input->size, |
2099 | 0 | srcSize_wrong, |
2100 | 0 | "forbidden. in: pos: %u vs size: %u", |
2101 | 0 | (U32)input->pos, (U32)input->size); |
2102 | 0 | RETURN_ERROR_IF( |
2103 | 0 | output->pos > output->size, |
2104 | 0 | dstSize_tooSmall, |
2105 | 0 | "forbidden. out: pos: %u vs size: %u", |
2106 | 0 | (U32)output->pos, (U32)output->size); |
2107 | 0 | DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos)); |
2108 | 0 | FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds, output), ""); |
2109 | | |
2110 | 0 | while (someMoreWork) { |
2111 | 0 | switch(zds->streamStage) |
2112 | 0 | { |
2113 | 0 | case zdss_init : |
2114 | 0 | DEBUGLOG(5, "stage zdss_init => transparent reset "); |
2115 | 0 | zds->streamStage = zdss_loadHeader; |
2116 | 0 | zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0; |
2117 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2118 | | zds->legacyVersion = 0; |
2119 | | #endif |
2120 | 0 | zds->hostageByte = 0; |
2121 | 0 | zds->expectedOutBuffer = *output; |
2122 | 0 | ZSTD_FALLTHROUGH; |
2123 | |
|
2124 | 0 | case zdss_loadHeader : |
2125 | 0 | DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip)); |
2126 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2127 | | if (zds->legacyVersion) { |
2128 | | RETURN_ERROR_IF(zds->staticSize, memory_allocation, |
2129 | | "legacy support is incompatible with static dctx"); |
2130 | | { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); |
2131 | | if (hint==0) zds->streamStage = zdss_init; |
2132 | | return hint; |
2133 | | } } |
2134 | | #endif |
2135 | 0 | { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format); |
2136 | 0 | if (zds->refMultipleDDicts && zds->ddictSet) { |
2137 | 0 | ZSTD_DCtx_selectFrameDDict(zds); |
2138 | 0 | } |
2139 | 0 | if (ZSTD_isError(hSize)) { |
2140 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2141 | | U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart); |
2142 | | if (legacyVersion) { |
2143 | | ZSTD_DDict const* const ddict = ZSTD_getDDict(zds); |
2144 | | const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL; |
2145 | | size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0; |
2146 | | DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion); |
2147 | | RETURN_ERROR_IF(zds->staticSize, memory_allocation, |
2148 | | "legacy support is incompatible with static dctx"); |
2149 | | FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext, |
2150 | | zds->previousLegacyVersion, legacyVersion, |
2151 | | dict, dictSize), ""); |
2152 | | zds->legacyVersion = zds->previousLegacyVersion = legacyVersion; |
2153 | | { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input); |
2154 | | if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */ |
2155 | | return hint; |
2156 | | } } |
2157 | | #endif |
2158 | 0 | return hSize; /* error */ |
2159 | 0 | } |
2160 | 0 | if (hSize != 0) { /* need more input */ |
2161 | 0 | size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */ |
2162 | 0 | size_t const remainingInput = (size_t)(iend-ip); |
2163 | 0 | assert(iend >= ip); |
2164 | 0 | if (toLoad > remainingInput) { /* not enough input to load full header */ |
2165 | 0 | if (remainingInput > 0) { |
2166 | 0 | ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput); |
2167 | 0 | zds->lhSize += remainingInput; |
2168 | 0 | } |
2169 | 0 | input->pos = input->size; |
2170 | | /* check first few bytes */ |
2171 | 0 | FORWARD_IF_ERROR( |
2172 | 0 | ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format), |
2173 | 0 | "First few bytes detected incorrect" ); |
2174 | | /* return hint input size */ |
2175 | 0 | return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */ |
2176 | 0 | } |
2177 | 0 | assert(ip != NULL); |
2178 | 0 | ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad; |
2179 | 0 | break; |
2180 | 0 | } } |
2181 | | |
2182 | | /* check for single-pass mode opportunity */ |
2183 | 0 | if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN |
2184 | 0 | && zds->fParams.frameType != ZSTD_skippableFrame |
2185 | 0 | && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) { |
2186 | 0 | size_t const cSize = ZSTD_findFrameCompressedSize_advanced(istart, (size_t)(iend-istart), zds->format); |
2187 | 0 | if (cSize <= (size_t)(iend-istart)) { |
2188 | | /* shortcut : using single-pass mode */ |
2189 | 0 | size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, (size_t)(oend-op), istart, cSize, ZSTD_getDDict(zds)); |
2190 | 0 | if (ZSTD_isError(decompressedSize)) return decompressedSize; |
2191 | 0 | DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()"); |
2192 | 0 | assert(istart != NULL); |
2193 | 0 | ip = istart + cSize; |
2194 | 0 | op = op ? op + decompressedSize : op; /* can occur if frameContentSize = 0 (empty frame) */ |
2195 | 0 | zds->expected = 0; |
2196 | 0 | zds->streamStage = zdss_init; |
2197 | 0 | someMoreWork = 0; |
2198 | 0 | break; |
2199 | 0 | } } |
2200 | | |
2201 | | /* Check output buffer is large enough for ZSTD_odm_stable. */ |
2202 | 0 | if (zds->outBufferMode == ZSTD_bm_stable |
2203 | 0 | && zds->fParams.frameType != ZSTD_skippableFrame |
2204 | 0 | && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN |
2205 | 0 | && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) { |
2206 | 0 | RETURN_ERROR(dstSize_tooSmall, "ZSTD_obm_stable passed but ZSTD_outBuffer is too small"); |
2207 | 0 | } |
2208 | | |
2209 | | /* Consume header (see ZSTDds_decodeFrameHeader) */ |
2210 | 0 | DEBUGLOG(4, "Consume header"); |
2211 | 0 | FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), ""); |
2212 | | |
2213 | 0 | if (zds->format == ZSTD_f_zstd1 |
2214 | 0 | && (MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ |
2215 | 0 | zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE); |
2216 | 0 | zds->stage = ZSTDds_skipFrame; |
2217 | 0 | } else { |
2218 | 0 | FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), ""); |
2219 | 0 | zds->expected = ZSTD_blockHeaderSize; |
2220 | 0 | zds->stage = ZSTDds_decodeBlockHeader; |
2221 | 0 | } |
2222 | | |
2223 | | /* control buffer memory usage */ |
2224 | 0 | DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)", |
2225 | 0 | (U32)(zds->fParams.windowSize >>10), |
2226 | 0 | (U32)(zds->maxWindowSize >> 10) ); |
2227 | 0 | zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN); |
2228 | 0 | RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize, |
2229 | 0 | frameParameter_windowTooLarge, ""); |
2230 | 0 | if (zds->maxBlockSizeParam != 0) |
2231 | 0 | zds->fParams.blockSizeMax = MIN(zds->fParams.blockSizeMax, (unsigned)zds->maxBlockSizeParam); |
2232 | | |
2233 | | /* Adapt buffer sizes to frame header instructions */ |
2234 | 0 | { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */); |
2235 | 0 | size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_bm_buffered |
2236 | 0 | ? ZSTD_decodingBufferSize_internal(zds->fParams.windowSize, zds->fParams.frameContentSize, zds->fParams.blockSizeMax) |
2237 | 0 | : 0; |
2238 | |
|
2239 | 0 | ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize); |
2240 | |
|
2241 | 0 | { int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize); |
2242 | 0 | int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds); |
2243 | |
|
2244 | 0 | if (tooSmall || tooLarge) { |
2245 | 0 | size_t const bufferSize = neededInBuffSize + neededOutBuffSize; |
2246 | 0 | DEBUGLOG(4, "inBuff : from %u to %u", |
2247 | 0 | (U32)zds->inBuffSize, (U32)neededInBuffSize); |
2248 | 0 | DEBUGLOG(4, "outBuff : from %u to %u", |
2249 | 0 | (U32)zds->outBuffSize, (U32)neededOutBuffSize); |
2250 | 0 | if (zds->staticSize) { /* static DCtx */ |
2251 | 0 | DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize); |
2252 | 0 | assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */ |
2253 | 0 | RETURN_ERROR_IF( |
2254 | 0 | bufferSize > zds->staticSize - sizeof(ZSTD_DCtx), |
2255 | 0 | memory_allocation, ""); |
2256 | 0 | } else { |
2257 | 0 | ZSTD_customFree(zds->inBuff, zds->customMem); |
2258 | 0 | zds->inBuffSize = 0; |
2259 | 0 | zds->outBuffSize = 0; |
2260 | 0 | zds->inBuff = (char*)ZSTD_customMalloc(bufferSize, zds->customMem); |
2261 | 0 | RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, ""); |
2262 | 0 | } |
2263 | 0 | zds->inBuffSize = neededInBuffSize; |
2264 | 0 | zds->outBuff = zds->inBuff + zds->inBuffSize; |
2265 | 0 | zds->outBuffSize = neededOutBuffSize; |
2266 | 0 | } } } |
2267 | 0 | zds->streamStage = zdss_read; |
2268 | 0 | ZSTD_FALLTHROUGH; |
2269 | |
|
2270 | 0 | case zdss_read: |
2271 | 0 | DEBUGLOG(5, "stage zdss_read"); |
2272 | 0 | { size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip)); |
2273 | 0 | DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize); |
2274 | 0 | if (neededInSize==0) { /* end of frame */ |
2275 | 0 | zds->streamStage = zdss_init; |
2276 | 0 | someMoreWork = 0; |
2277 | 0 | break; |
2278 | 0 | } |
2279 | 0 | if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */ |
2280 | 0 | FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize), ""); |
2281 | 0 | assert(ip != NULL); |
2282 | 0 | ip += neededInSize; |
2283 | | /* Function modifies the stage so we must break */ |
2284 | 0 | break; |
2285 | 0 | } } |
2286 | 0 | if (ip==iend) { someMoreWork = 0; break; } /* no more input */ |
2287 | 0 | zds->streamStage = zdss_load; |
2288 | 0 | ZSTD_FALLTHROUGH; |
2289 | |
|
2290 | 0 | case zdss_load: |
2291 | 0 | { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds); |
2292 | 0 | size_t const toLoad = neededInSize - zds->inPos; |
2293 | 0 | int const isSkipFrame = ZSTD_isSkipFrame(zds); |
2294 | 0 | size_t loadedSize; |
2295 | | /* At this point we shouldn't be decompressing a block that we can stream. */ |
2296 | 0 | assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip))); |
2297 | 0 | if (isSkipFrame) { |
2298 | 0 | loadedSize = MIN(toLoad, (size_t)(iend-ip)); |
2299 | 0 | } else { |
2300 | 0 | RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos, |
2301 | 0 | corruption_detected, |
2302 | 0 | "should never happen"); |
2303 | 0 | loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, (size_t)(iend-ip)); |
2304 | 0 | } |
2305 | 0 | if (loadedSize != 0) { |
2306 | | /* ip may be NULL */ |
2307 | 0 | ip += loadedSize; |
2308 | 0 | zds->inPos += loadedSize; |
2309 | 0 | } |
2310 | 0 | if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */ |
2311 | | |
2312 | | /* decode loaded input */ |
2313 | 0 | zds->inPos = 0; /* input is consumed */ |
2314 | 0 | FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize), ""); |
2315 | | /* Function modifies the stage so we must break */ |
2316 | 0 | break; |
2317 | 0 | } |
2318 | 0 | case zdss_flush: |
2319 | 0 | { |
2320 | 0 | size_t const toFlushSize = zds->outEnd - zds->outStart; |
2321 | 0 | size_t const flushedSize = ZSTD_limitCopy(op, (size_t)(oend-op), zds->outBuff + zds->outStart, toFlushSize); |
2322 | |
|
2323 | 0 | op = op ? op + flushedSize : op; |
2324 | |
|
2325 | 0 | zds->outStart += flushedSize; |
2326 | 0 | if (flushedSize == toFlushSize) { /* flush completed */ |
2327 | 0 | zds->streamStage = zdss_read; |
2328 | 0 | if ( (zds->outBuffSize < zds->fParams.frameContentSize) |
2329 | 0 | && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) { |
2330 | 0 | DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)", |
2331 | 0 | (int)(zds->outBuffSize - zds->outStart), |
2332 | 0 | (U32)zds->fParams.blockSizeMax); |
2333 | 0 | zds->outStart = zds->outEnd = 0; |
2334 | 0 | } |
2335 | 0 | break; |
2336 | 0 | } } |
2337 | | /* cannot complete flush */ |
2338 | 0 | someMoreWork = 0; |
2339 | 0 | break; |
2340 | | |
2341 | 0 | default: |
2342 | 0 | assert(0); /* impossible */ |
2343 | 0 | RETURN_ERROR(GENERIC, "impossible to reach"); /* some compilers require default to do something */ |
2344 | 0 | } } |
2345 | | |
2346 | | /* result */ |
2347 | 0 | input->pos = (size_t)(ip - (const char*)(input->src)); |
2348 | 0 | output->pos = (size_t)(op - (char*)(output->dst)); |
2349 | | |
2350 | | /* Update the expected output buffer for ZSTD_obm_stable. */ |
2351 | 0 | zds->expectedOutBuffer = *output; |
2352 | |
|
2353 | 0 | if ((ip==istart) && (op==ostart)) { /* no forward progress */ |
2354 | 0 | zds->noForwardProgress ++; |
2355 | 0 | if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) { |
2356 | 0 | RETURN_ERROR_IF(op==oend, noForwardProgress_destFull, ""); |
2357 | 0 | RETURN_ERROR_IF(ip==iend, noForwardProgress_inputEmpty, ""); |
2358 | 0 | assert(0); |
2359 | 0 | } |
2360 | 0 | } else { |
2361 | 0 | zds->noForwardProgress = 0; |
2362 | 0 | } |
2363 | 0 | { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds); |
2364 | 0 | if (!nextSrcSizeHint) { /* frame fully decoded */ |
2365 | 0 | if (zds->outEnd == zds->outStart) { /* output fully flushed */ |
2366 | 0 | if (zds->hostageByte) { |
2367 | 0 | if (input->pos >= input->size) { |
2368 | | /* can't release hostage (not present) */ |
2369 | 0 | zds->streamStage = zdss_read; |
2370 | 0 | return 1; |
2371 | 0 | } |
2372 | 0 | input->pos++; /* release hostage */ |
2373 | 0 | } /* zds->hostageByte */ |
2374 | 0 | return 0; |
2375 | 0 | } /* zds->outEnd == zds->outStart */ |
2376 | 0 | if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */ |
2377 | 0 | input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */ |
2378 | 0 | zds->hostageByte=1; |
2379 | 0 | } |
2380 | 0 | return 1; |
2381 | 0 | } /* nextSrcSizeHint==0 */ |
2382 | 0 | nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */ |
2383 | 0 | assert(zds->inPos <= nextSrcSizeHint); |
2384 | 0 | nextSrcSizeHint -= zds->inPos; /* part already loaded*/ |
2385 | 0 | return nextSrcSizeHint; |
2386 | 0 | } |
2387 | 0 | } |
2388 | | |
2389 | | size_t ZSTD_decompressStream_simpleArgs ( |
2390 | | ZSTD_DCtx* dctx, |
2391 | | void* dst, size_t dstCapacity, size_t* dstPos, |
2392 | | const void* src, size_t srcSize, size_t* srcPos) |
2393 | 0 | { |
2394 | 0 | ZSTD_outBuffer output; |
2395 | 0 | ZSTD_inBuffer input; |
2396 | 0 | output.dst = dst; |
2397 | 0 | output.size = dstCapacity; |
2398 | 0 | output.pos = *dstPos; |
2399 | 0 | input.src = src; |
2400 | 0 | input.size = srcSize; |
2401 | 0 | input.pos = *srcPos; |
2402 | 0 | { size_t const cErr = ZSTD_decompressStream(dctx, &output, &input); |
2403 | 0 | *dstPos = output.pos; |
2404 | 0 | *srcPos = input.pos; |
2405 | 0 | return cErr; |
2406 | 0 | } |
2407 | 0 | } |