/src/zstd/lib/legacy/zstd_v01.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) Yann Collet, 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 | | * Includes |
14 | | ******************************************/ |
15 | | #include <stddef.h> /* size_t, ptrdiff_t */ |
16 | | #include "zstd_v01.h" |
17 | | #include "../common/compiler.h" |
18 | | #include "../common/error_private.h" |
19 | | |
20 | | |
21 | | /****************************************** |
22 | | * Static allocation |
23 | | ******************************************/ |
24 | | /* You can statically allocate FSE CTable/DTable as a table of unsigned using below macro */ |
25 | | #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog)) |
26 | | |
27 | | /* You can statically allocate Huff0 DTable as a table of unsigned short using below macro */ |
28 | | #define HUF_DTABLE_SIZE_U16(maxTableLog) (1 + (1<<maxTableLog)) |
29 | | #define HUF_CREATE_STATIC_DTABLE(DTable, maxTableLog) \ |
30 | 1.29k | unsigned short DTable[HUF_DTABLE_SIZE_U16(maxTableLog)] = { maxTableLog } |
31 | | |
32 | | |
33 | | /****************************************** |
34 | | * Error Management |
35 | | ******************************************/ |
36 | | #define FSE_LIST_ERRORS(ITEM) \ |
37 | | ITEM(FSE_OK_NoError) ITEM(FSE_ERROR_GENERIC) \ |
38 | | ITEM(FSE_ERROR_tableLog_tooLarge) ITEM(FSE_ERROR_maxSymbolValue_tooLarge) ITEM(FSE_ERROR_maxSymbolValue_tooSmall) \ |
39 | | ITEM(FSE_ERROR_dstSize_tooSmall) ITEM(FSE_ERROR_srcSize_wrong)\ |
40 | | ITEM(FSE_ERROR_corruptionDetected) \ |
41 | | ITEM(FSE_ERROR_maxCode) |
42 | | |
43 | | #define FSE_GENERATE_ENUM(ENUM) ENUM, |
44 | | typedef enum { FSE_LIST_ERRORS(FSE_GENERATE_ENUM) } FSE_errorCodes; /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */ |
45 | | |
46 | | |
47 | | /****************************************** |
48 | | * FSE symbol compression API |
49 | | ******************************************/ |
50 | | /* |
51 | | This API consists of small unitary functions, which highly benefit from being inlined. |
52 | | You will want to enable link-time-optimization to ensure these functions are properly inlined in your binary. |
53 | | Visual seems to do it automatically. |
54 | | For gcc or clang, you'll need to add -flto flag at compilation and linking stages. |
55 | | If none of these solutions is applicable, include "fse.c" directly. |
56 | | */ |
57 | | |
58 | | typedef unsigned FSE_CTable; /* don't allocate that. It's just a way to be more restrictive than void* */ |
59 | | typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ |
60 | | |
61 | | typedef struct |
62 | | { |
63 | | size_t bitContainer; |
64 | | int bitPos; |
65 | | char* startPtr; |
66 | | char* ptr; |
67 | | char* endPtr; |
68 | | } FSE_CStream_t; |
69 | | |
70 | | typedef struct |
71 | | { |
72 | | ptrdiff_t value; |
73 | | const void* stateTable; |
74 | | const void* symbolTT; |
75 | | unsigned stateLog; |
76 | | } FSE_CState_t; |
77 | | |
78 | | typedef struct |
79 | | { |
80 | | size_t bitContainer; |
81 | | unsigned bitsConsumed; |
82 | | const char* ptr; |
83 | | const char* start; |
84 | | } FSE_DStream_t; |
85 | | |
86 | | typedef struct |
87 | | { |
88 | | size_t state; |
89 | | const void* table; /* precise table may vary, depending on U16 */ |
90 | | } FSE_DState_t; |
91 | | |
92 | | typedef enum { FSE_DStream_unfinished = 0, |
93 | | FSE_DStream_endOfBuffer = 1, |
94 | | FSE_DStream_completed = 2, |
95 | | FSE_DStream_tooFar = 3 } FSE_DStream_status; /* result of FSE_reloadDStream() */ |
96 | | /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... ?! */ |
97 | | |
98 | | |
99 | | /**************************************************************** |
100 | | * Tuning parameters |
101 | | ****************************************************************/ |
102 | | /* MEMORY_USAGE : |
103 | | * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) |
104 | | * Increasing memory usage improves compression ratio |
105 | | * Reduced memory usage can improve speed, due to cache effect |
106 | | * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ |
107 | 19.9k | #define FSE_MAX_MEMORY_USAGE 14 |
108 | | #define FSE_DEFAULT_MEMORY_USAGE 13 |
109 | | |
110 | | /* FSE_MAX_SYMBOL_VALUE : |
111 | | * Maximum symbol value authorized. |
112 | | * Required for proper stack allocation */ |
113 | 5.35k | #define FSE_MAX_SYMBOL_VALUE 255 |
114 | | |
115 | | |
116 | | /**************************************************************** |
117 | | * template functions type & suffix |
118 | | ****************************************************************/ |
119 | 897k | #define FSE_FUNCTION_TYPE BYTE |
120 | | #define FSE_FUNCTION_EXTENSION |
121 | | |
122 | | |
123 | | /**************************************************************** |
124 | | * Byte symbol type |
125 | | ****************************************************************/ |
126 | | typedef struct |
127 | | { |
128 | | unsigned short newState; |
129 | | unsigned char symbol; |
130 | | unsigned char nbBits; |
131 | | } FSE_decode_t; /* size == U32 */ |
132 | | |
133 | | |
134 | | |
135 | | /**************************************************************** |
136 | | * Compiler specifics |
137 | | ****************************************************************/ |
138 | | #ifdef _MSC_VER /* Visual Studio */ |
139 | | # define FORCE_INLINE static __forceinline |
140 | | # include <intrin.h> /* For Visual 2005 */ |
141 | | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
142 | | # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ |
143 | | #else |
144 | | # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
145 | | # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
146 | | # ifdef __GNUC__ |
147 | | # define FORCE_INLINE static inline __attribute__((always_inline)) |
148 | | # else |
149 | | # define FORCE_INLINE static inline |
150 | | # endif |
151 | | # else |
152 | | # define FORCE_INLINE static |
153 | | # endif /* __STDC_VERSION__ */ |
154 | | #endif |
155 | | |
156 | | |
157 | | /**************************************************************** |
158 | | * Includes |
159 | | ****************************************************************/ |
160 | | #include <stdlib.h> /* malloc, free, qsort */ |
161 | | #include <string.h> /* memcpy, memset */ |
162 | | #include <stdio.h> /* printf (debug) */ |
163 | | |
164 | | |
165 | | #ifndef MEM_ACCESS_MODULE |
166 | | #define MEM_ACCESS_MODULE |
167 | | /**************************************************************** |
168 | | * Basic Types |
169 | | *****************************************************************/ |
170 | | #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
171 | | # include <stdint.h> |
172 | | typedef uint8_t BYTE; |
173 | | typedef uint16_t U16; |
174 | | typedef int16_t S16; |
175 | | typedef uint32_t U32; |
176 | | typedef int32_t S32; |
177 | | typedef uint64_t U64; |
178 | | typedef int64_t S64; |
179 | | #else |
180 | | typedef unsigned char BYTE; |
181 | | typedef unsigned short U16; |
182 | | typedef signed short S16; |
183 | | typedef unsigned int U32; |
184 | | typedef signed int S32; |
185 | | typedef unsigned long long U64; |
186 | | typedef signed long long S64; |
187 | | #endif |
188 | | |
189 | | #endif /* MEM_ACCESS_MODULE */ |
190 | | |
191 | | /**************************************************************** |
192 | | * Memory I/O |
193 | | *****************************************************************/ |
194 | | |
195 | | static unsigned FSE_32bits(void) |
196 | 2.21M | { |
197 | 2.21M | return sizeof(void*)==4; |
198 | 2.21M | } |
199 | | |
200 | | static unsigned FSE_isLittleEndian(void) |
201 | 1.09M | { |
202 | 1.09M | const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ |
203 | 1.09M | return one.c[0]; |
204 | 1.09M | } |
205 | | |
206 | | static U16 FSE_read16(const void* memPtr) |
207 | 2.21k | { |
208 | 2.21k | U16 val; memcpy(&val, memPtr, sizeof(val)); return val; |
209 | 2.21k | } |
210 | | |
211 | | static U32 FSE_read32(const void* memPtr) |
212 | 62.9k | { |
213 | 62.9k | U32 val; memcpy(&val, memPtr, sizeof(val)); return val; |
214 | 62.9k | } |
215 | | |
216 | | static U64 FSE_read64(const void* memPtr) |
217 | 1.02M | { |
218 | 1.02M | U64 val; memcpy(&val, memPtr, sizeof(val)); return val; |
219 | 1.02M | } |
220 | | |
221 | | static U16 FSE_readLE16(const void* memPtr) |
222 | 2.21k | { |
223 | 2.21k | if (FSE_isLittleEndian()) |
224 | 2.21k | return FSE_read16(memPtr); |
225 | 0 | else |
226 | 0 | { |
227 | 0 | const BYTE* p = (const BYTE*)memPtr; |
228 | 0 | return (U16)(p[0] + (p[1]<<8)); |
229 | 0 | } |
230 | 2.21k | } |
231 | | |
232 | | static U32 FSE_readLE32(const void* memPtr) |
233 | 62.9k | { |
234 | 62.9k | if (FSE_isLittleEndian()) |
235 | 62.9k | return FSE_read32(memPtr); |
236 | 0 | else |
237 | 0 | { |
238 | 0 | const BYTE* p = (const BYTE*)memPtr; |
239 | 0 | return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24)); |
240 | 0 | } |
241 | 62.9k | } |
242 | | |
243 | | |
244 | | static U64 FSE_readLE64(const void* memPtr) |
245 | 1.02M | { |
246 | 1.02M | if (FSE_isLittleEndian()) |
247 | 1.02M | return FSE_read64(memPtr); |
248 | 0 | else |
249 | 0 | { |
250 | 0 | const BYTE* p = (const BYTE*)memPtr; |
251 | 0 | return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24) |
252 | 0 | + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56)); |
253 | 0 | } |
254 | 1.02M | } |
255 | | |
256 | | static size_t FSE_readLEST(const void* memPtr) |
257 | 1.02M | { |
258 | 1.02M | if (FSE_32bits()) |
259 | 0 | return (size_t)FSE_readLE32(memPtr); |
260 | 1.02M | else |
261 | 1.02M | return (size_t)FSE_readLE64(memPtr); |
262 | 1.02M | } |
263 | | |
264 | | |
265 | | |
266 | | /**************************************************************** |
267 | | * Constants |
268 | | *****************************************************************/ |
269 | 19.9k | #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2) |
270 | | #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG) |
271 | | #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1) |
272 | | #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2) |
273 | 5.02k | #define FSE_MIN_TABLELOG 5 |
274 | | |
275 | 5.02k | #define FSE_TABLELOG_ABSOLUTE_MAX 15 |
276 | | #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX |
277 | | #error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" |
278 | | #endif |
279 | | |
280 | | |
281 | | /**************************************************************** |
282 | | * Error Management |
283 | | ****************************************************************/ |
284 | 1.29k | #define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ |
285 | | |
286 | | |
287 | | /**************************************************************** |
288 | | * Complex types |
289 | | ****************************************************************/ |
290 | | typedef struct |
291 | | { |
292 | | int deltaFindState; |
293 | | U32 deltaNbBits; |
294 | | } FSE_symbolCompressionTransform; /* total 8 bytes */ |
295 | | |
296 | | typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)]; |
297 | | |
298 | | /**************************************************************** |
299 | | * Internal functions |
300 | | ****************************************************************/ |
301 | | FORCE_INLINE unsigned FSE_highbit32 (U32 val) |
302 | 908k | { |
303 | | # if defined(_MSC_VER) /* Visual */ |
304 | | unsigned long r; |
305 | | return _BitScanReverse(&r, val) ? (unsigned)r : 0; |
306 | | # elif defined(__GNUC__) && (GCC_VERSION >= 304) /* GCC Intrinsic */ |
307 | | return __builtin_clz (val) ^ 31; |
308 | | # else /* Software version */ |
309 | | static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; |
310 | | U32 v = val; |
311 | | unsigned r; |
312 | | v |= v >> 1; |
313 | | v |= v >> 2; |
314 | | v |= v >> 4; |
315 | | v |= v >> 8; |
316 | | v |= v >> 16; |
317 | | r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27]; |
318 | | return r; |
319 | | # endif |
320 | 908k | } |
321 | | |
322 | | |
323 | | /**************************************************************** |
324 | | * Templates |
325 | | ****************************************************************/ |
326 | | /* |
327 | | designed to be included |
328 | | for type-specific functions (template emulation in C) |
329 | | Objective is to write these functions only once, for improved maintenance |
330 | | */ |
331 | | |
332 | | /* safety checks */ |
333 | | #ifndef FSE_FUNCTION_EXTENSION |
334 | | # error "FSE_FUNCTION_EXTENSION must be defined" |
335 | | #endif |
336 | | #ifndef FSE_FUNCTION_TYPE |
337 | | # error "FSE_FUNCTION_TYPE must be defined" |
338 | | #endif |
339 | | |
340 | | /* Function names */ |
341 | | #define FSE_CAT(X,Y) X##Y |
342 | | #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) |
343 | | #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) |
344 | | |
345 | | |
346 | | |
347 | 4.84k | static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; } |
348 | | |
349 | 4.84k | #define FSE_DECODE_TYPE FSE_decode_t |
350 | | |
351 | | |
352 | | typedef struct { |
353 | | U16 tableLog; |
354 | | U16 fastMode; |
355 | | } FSE_DTableHeader; /* sizeof U32 */ |
356 | | |
357 | | static size_t FSE_buildDTable |
358 | | (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) |
359 | 4.84k | { |
360 | 4.84k | void* ptr = dt; |
361 | 4.84k | FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; |
362 | 4.84k | FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)(ptr) + 1; /* because dt is unsigned, 32-bits aligned on 32-bits */ |
363 | 4.84k | const U32 tableSize = 1 << tableLog; |
364 | 4.84k | const U32 tableMask = tableSize-1; |
365 | 4.84k | const U32 step = FSE_tableStep(tableSize); |
366 | 4.84k | U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; |
367 | 4.84k | U32 position = 0; |
368 | 4.84k | U32 highThreshold = tableSize-1; |
369 | 4.84k | const S16 largeLimit= (S16)(1 << (tableLog-1)); |
370 | 4.84k | U32 noLarge = 1; |
371 | 4.84k | U32 s; |
372 | | |
373 | | /* Sanity Checks */ |
374 | 4.84k | if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return (size_t)-FSE_ERROR_maxSymbolValue_tooLarge; |
375 | 4.84k | if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_tableLog_tooLarge; |
376 | | |
377 | | /* Init, lay down lowprob symbols */ |
378 | 4.84k | DTableH[0].tableLog = (U16)tableLog; |
379 | 56.5k | for (s=0; s<=maxSymbolValue; s++) |
380 | 51.6k | { |
381 | 51.6k | if (normalizedCounter[s]==-1) |
382 | 26.4k | { |
383 | 26.4k | tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; |
384 | 26.4k | symbolNext[s] = 1; |
385 | 26.4k | } |
386 | 25.2k | else |
387 | 25.2k | { |
388 | 25.2k | if (normalizedCounter[s] >= largeLimit) noLarge=0; |
389 | 25.2k | symbolNext[s] = normalizedCounter[s]; |
390 | 25.2k | } |
391 | 51.6k | } |
392 | | |
393 | | /* Spread symbols */ |
394 | 56.5k | for (s=0; s<=maxSymbolValue; s++) |
395 | 51.6k | { |
396 | 51.6k | int i; |
397 | 922k | for (i=0; i<normalizedCounter[s]; i++) |
398 | 870k | { |
399 | 870k | tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; |
400 | 870k | position = (position + step) & tableMask; |
401 | 896k | while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ |
402 | 870k | } |
403 | 51.6k | } |
404 | | |
405 | 4.84k | if (position!=0) return (size_t)-FSE_ERROR_GENERIC; /* position must reach all cells once, otherwise normalizedCounter is incorrect */ |
406 | | |
407 | | /* Build Decoding table */ |
408 | 4.84k | { |
409 | 4.84k | U32 i; |
410 | 902k | for (i=0; i<tableSize; i++) |
411 | 897k | { |
412 | 897k | FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol); |
413 | 897k | U16 nextState = symbolNext[symbol]++; |
414 | 897k | tableDecode[i].nbBits = (BYTE) (tableLog - FSE_highbit32 ((U32)nextState) ); |
415 | 897k | tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize); |
416 | 897k | } |
417 | 4.84k | } |
418 | | |
419 | 4.84k | DTableH->fastMode = (U16)noLarge; |
420 | 4.84k | return 0; |
421 | 4.84k | } |
422 | | |
423 | | |
424 | | /****************************************** |
425 | | * FSE byte symbol |
426 | | ******************************************/ |
427 | | #ifndef FSE_COMMONDEFS_ONLY |
428 | | |
429 | 17.4k | static unsigned FSE_isError(size_t code) { return (code > (size_t)(-FSE_ERROR_maxCode)); } |
430 | | |
431 | | static short FSE_abs(short a) |
432 | 53.4k | { |
433 | 53.4k | return a<0? -a : a; |
434 | 53.4k | } |
435 | | |
436 | | |
437 | | /**************************************************************** |
438 | | * Header bitstream management |
439 | | ****************************************************************/ |
440 | | static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
441 | | const void* headerBuffer, size_t hbSize) |
442 | 5.07k | { |
443 | 5.07k | const BYTE* const istart = (const BYTE*) headerBuffer; |
444 | 5.07k | const BYTE* const iend = istart + hbSize; |
445 | 5.07k | const BYTE* ip = istart; |
446 | 5.07k | int nbBits; |
447 | 5.07k | int remaining; |
448 | 5.07k | int threshold; |
449 | 5.07k | U32 bitStream; |
450 | 5.07k | int bitCount; |
451 | 5.07k | unsigned charnum = 0; |
452 | 5.07k | int previous0 = 0; |
453 | | |
454 | 5.07k | if (hbSize < 4) return (size_t)-FSE_ERROR_srcSize_wrong; |
455 | 5.02k | bitStream = FSE_readLE32(ip); |
456 | 5.02k | nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */ |
457 | 5.02k | if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return (size_t)-FSE_ERROR_tableLog_tooLarge; |
458 | 5.00k | bitStream >>= 4; |
459 | 5.00k | bitCount = 4; |
460 | 5.00k | *tableLogPtr = nbBits; |
461 | 5.00k | remaining = (1<<nbBits)+1; |
462 | 5.00k | threshold = 1<<nbBits; |
463 | 5.00k | nbBits++; |
464 | | |
465 | 58.4k | while ((remaining>1) && (charnum<=*maxSVPtr)) |
466 | 53.5k | { |
467 | 53.5k | if (previous0) |
468 | 6.46k | { |
469 | 6.46k | unsigned n0 = charnum; |
470 | 7.07k | while ((bitStream & 0xFFFF) == 0xFFFF) |
471 | 615 | { |
472 | 615 | n0+=24; |
473 | 615 | if (ip < iend-5) |
474 | 527 | { |
475 | 527 | ip+=2; |
476 | 527 | bitStream = FSE_readLE32(ip) >> bitCount; |
477 | 527 | } |
478 | 88 | else |
479 | 88 | { |
480 | 88 | bitStream >>= 16; |
481 | 88 | bitCount+=16; |
482 | 88 | } |
483 | 615 | } |
484 | 8.14k | while ((bitStream & 3) == 3) |
485 | 1.68k | { |
486 | 1.68k | n0+=3; |
487 | 1.68k | bitStream>>=2; |
488 | 1.68k | bitCount+=2; |
489 | 1.68k | } |
490 | 6.46k | n0 += bitStream & 3; |
491 | 6.46k | bitCount += 2; |
492 | 6.46k | if (n0 > *maxSVPtr) return (size_t)-FSE_ERROR_maxSymbolValue_tooSmall; |
493 | 18.1k | while (charnum < n0) normalizedCounter[charnum++] = 0; |
494 | 6.42k | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) |
495 | 3.91k | { |
496 | 3.91k | ip += bitCount>>3; |
497 | 3.91k | bitCount &= 7; |
498 | 3.91k | bitStream = FSE_readLE32(ip) >> bitCount; |
499 | 3.91k | } |
500 | 2.51k | else |
501 | 2.51k | bitStream >>= 2; |
502 | 6.42k | } |
503 | 53.4k | { |
504 | 53.4k | const short max = (short)((2*threshold-1)-remaining); |
505 | 53.4k | short count; |
506 | | |
507 | 53.4k | if ((bitStream & (threshold-1)) < (U32)max) |
508 | 36.6k | { |
509 | 36.6k | count = (short)(bitStream & (threshold-1)); |
510 | 36.6k | bitCount += nbBits-1; |
511 | 36.6k | } |
512 | 16.7k | else |
513 | 16.7k | { |
514 | 16.7k | count = (short)(bitStream & (2*threshold-1)); |
515 | 16.7k | if (count >= threshold) count -= max; |
516 | 16.7k | bitCount += nbBits; |
517 | 16.7k | } |
518 | | |
519 | 53.4k | count--; /* extra accuracy */ |
520 | 53.4k | remaining -= FSE_abs(count); |
521 | 53.4k | normalizedCounter[charnum++] = count; |
522 | 53.4k | previous0 = !count; |
523 | 84.3k | while (remaining < threshold) |
524 | 30.8k | { |
525 | 30.8k | nbBits--; |
526 | 30.8k | threshold >>= 1; |
527 | 30.8k | } |
528 | | |
529 | 53.4k | { |
530 | 53.4k | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) |
531 | 43.3k | { |
532 | 43.3k | ip += bitCount>>3; |
533 | 43.3k | bitCount &= 7; |
534 | 43.3k | } |
535 | 10.1k | else |
536 | 10.1k | { |
537 | 10.1k | bitCount -= (int)(8 * (iend - 4 - ip)); |
538 | 10.1k | ip = iend - 4; |
539 | 10.1k | } |
540 | 53.4k | bitStream = FSE_readLE32(ip) >> (bitCount & 31); |
541 | 53.4k | } |
542 | 53.4k | } |
543 | 53.4k | } |
544 | 4.96k | if (remaining != 1) return (size_t)-FSE_ERROR_GENERIC; |
545 | 4.93k | *maxSVPtr = charnum-1; |
546 | | |
547 | 4.93k | ip += (bitCount+7)>>3; |
548 | 4.93k | if ((size_t)(ip-istart) > hbSize) return (size_t)-FSE_ERROR_srcSize_wrong; |
549 | 4.87k | return ip-istart; |
550 | 4.93k | } |
551 | | |
552 | | |
553 | | /********************************************************* |
554 | | * Decompression (Byte symbols) |
555 | | *********************************************************/ |
556 | | static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue) |
557 | 3.95k | { |
558 | 3.95k | void* ptr = dt; |
559 | 3.95k | FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; |
560 | 3.95k | FSE_decode_t* const cell = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */ |
561 | | |
562 | 3.95k | DTableH->tableLog = 0; |
563 | 3.95k | DTableH->fastMode = 0; |
564 | | |
565 | 3.95k | cell->newState = 0; |
566 | 3.95k | cell->symbol = symbolValue; |
567 | 3.95k | cell->nbBits = 0; |
568 | | |
569 | 3.95k | return 0; |
570 | 3.95k | } |
571 | | |
572 | | |
573 | | static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits) |
574 | 9.46k | { |
575 | 9.46k | void* ptr = dt; |
576 | 9.46k | FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; |
577 | 9.46k | FSE_decode_t* const dinfo = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */ |
578 | 9.46k | const unsigned tableSize = 1 << nbBits; |
579 | 9.46k | const unsigned tableMask = tableSize - 1; |
580 | 9.46k | const unsigned maxSymbolValue = tableMask; |
581 | 9.46k | unsigned s; |
582 | | |
583 | | /* Sanity checks */ |
584 | 9.46k | if (nbBits < 1) return (size_t)-FSE_ERROR_GENERIC; /* min size */ |
585 | | |
586 | | /* Build Decoding Table */ |
587 | 9.46k | DTableH->tableLog = (U16)nbBits; |
588 | 9.46k | DTableH->fastMode = 1; |
589 | 791k | for (s=0; s<=maxSymbolValue; s++) |
590 | 781k | { |
591 | 781k | dinfo[s].newState = 0; |
592 | 781k | dinfo[s].symbol = (BYTE)s; |
593 | 781k | dinfo[s].nbBits = (BYTE)nbBits; |
594 | 781k | } |
595 | | |
596 | 9.46k | return 0; |
597 | 9.46k | } |
598 | | |
599 | | |
600 | | /* FSE_initDStream |
601 | | * Initialize a FSE_DStream_t. |
602 | | * srcBuffer must point at the beginning of an FSE block. |
603 | | * The function result is the size of the FSE_block (== srcSize). |
604 | | * If srcSize is too small, the function will return an errorCode; |
605 | | */ |
606 | | static size_t FSE_initDStream(FSE_DStream_t* bitD, const void* srcBuffer, size_t srcSize) |
607 | 8.89k | { |
608 | 8.89k | if (srcSize < 1) return (size_t)-FSE_ERROR_srcSize_wrong; |
609 | | |
610 | 8.85k | if (srcSize >= sizeof(size_t)) |
611 | 2.66k | { |
612 | 2.66k | U32 contain32; |
613 | 2.66k | bitD->start = (const char*)srcBuffer; |
614 | 2.66k | bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t); |
615 | 2.66k | bitD->bitContainer = FSE_readLEST(bitD->ptr); |
616 | 2.66k | contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; |
617 | 2.66k | if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */ |
618 | 2.61k | bitD->bitsConsumed = 8 - FSE_highbit32(contain32); |
619 | 2.61k | } |
620 | 6.19k | else |
621 | 6.19k | { |
622 | 6.19k | U32 contain32; |
623 | 6.19k | bitD->start = (const char*)srcBuffer; |
624 | 6.19k | bitD->ptr = bitD->start; |
625 | 6.19k | bitD->bitContainer = *(const BYTE*)(bitD->start); |
626 | 6.19k | switch(srcSize) |
627 | 6.19k | { |
628 | 195 | case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16); |
629 | | /* fallthrough */ |
630 | 349 | case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24); |
631 | | /* fallthrough */ |
632 | 441 | case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32); |
633 | | /* fallthrough */ |
634 | 524 | case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; |
635 | | /* fallthrough */ |
636 | 3.13k | case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; |
637 | | /* fallthrough */ |
638 | 4.97k | case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; |
639 | | /* fallthrough */ |
640 | 6.19k | default:; |
641 | 6.19k | } |
642 | 6.19k | contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; |
643 | 6.19k | if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */ |
644 | 6.12k | bitD->bitsConsumed = 8 - FSE_highbit32(contain32); |
645 | 6.12k | bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8; |
646 | 6.12k | } |
647 | | |
648 | 8.74k | return srcSize; |
649 | 8.85k | } |
650 | | |
651 | | |
652 | | /*!FSE_lookBits |
653 | | * Provides next n bits from the bitContainer. |
654 | | * bitContainer is not modified (bits are still present for next read/look) |
655 | | * On 32-bits, maxNbBits==25 |
656 | | * On 64-bits, maxNbBits==57 |
657 | | * return : value extracted. |
658 | | */ |
659 | | static size_t FSE_lookBits(FSE_DStream_t* bitD, U32 nbBits) |
660 | 1.20M | { |
661 | 1.20M | const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; |
662 | 1.20M | return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask); |
663 | 1.20M | } |
664 | | |
665 | | static size_t FSE_lookBitsFast(FSE_DStream_t* bitD, U32 nbBits) /* only if nbBits >= 1 !! */ |
666 | 2.05M | { |
667 | 2.05M | const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; |
668 | 2.05M | return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask); |
669 | 2.05M | } |
670 | | |
671 | | static void FSE_skipBits(FSE_DStream_t* bitD, U32 nbBits) |
672 | 3.25M | { |
673 | 3.25M | bitD->bitsConsumed += nbBits; |
674 | 3.25M | } |
675 | | |
676 | | |
677 | | /*!FSE_readBits |
678 | | * Read next n bits from the bitContainer. |
679 | | * On 32-bits, don't read more than maxNbBits==25 |
680 | | * On 64-bits, don't read more than maxNbBits==57 |
681 | | * Use the fast variant *only* if n >= 1. |
682 | | * return : value extracted. |
683 | | */ |
684 | | static size_t FSE_readBits(FSE_DStream_t* bitD, U32 nbBits) |
685 | 1.20M | { |
686 | 1.20M | size_t value = FSE_lookBits(bitD, nbBits); |
687 | 1.20M | FSE_skipBits(bitD, nbBits); |
688 | 1.20M | return value; |
689 | 1.20M | } |
690 | | |
691 | | static size_t FSE_readBitsFast(FSE_DStream_t* bitD, U32 nbBits) /* only if nbBits >= 1 !! */ |
692 | 11.3k | { |
693 | 11.3k | size_t value = FSE_lookBitsFast(bitD, nbBits); |
694 | 11.3k | FSE_skipBits(bitD, nbBits); |
695 | 11.3k | return value; |
696 | 11.3k | } |
697 | | |
698 | | static unsigned FSE_reloadDStream(FSE_DStream_t* bitD) |
699 | 1.20M | { |
700 | 1.20M | if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */ |
701 | 39.9k | return FSE_DStream_tooFar; |
702 | | |
703 | 1.16M | if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) |
704 | 1.00M | { |
705 | 1.00M | bitD->ptr -= bitD->bitsConsumed >> 3; |
706 | 1.00M | bitD->bitsConsumed &= 7; |
707 | 1.00M | bitD->bitContainer = FSE_readLEST(bitD->ptr); |
708 | 1.00M | return FSE_DStream_unfinished; |
709 | 1.00M | } |
710 | 161k | if (bitD->ptr == bitD->start) |
711 | 136k | { |
712 | 136k | if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return FSE_DStream_endOfBuffer; |
713 | 15.8k | return FSE_DStream_completed; |
714 | 136k | } |
715 | 25.5k | { |
716 | 25.5k | U32 nbBytes = bitD->bitsConsumed >> 3; |
717 | 25.5k | U32 result = FSE_DStream_unfinished; |
718 | 25.5k | if (bitD->ptr - nbBytes < bitD->start) |
719 | 806 | { |
720 | 806 | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ |
721 | 806 | result = FSE_DStream_endOfBuffer; |
722 | 806 | } |
723 | 25.5k | bitD->ptr -= nbBytes; |
724 | 25.5k | bitD->bitsConsumed -= nbBytes*8; |
725 | 25.5k | bitD->bitContainer = FSE_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ |
726 | 25.5k | return result; |
727 | 161k | } |
728 | 161k | } |
729 | | |
730 | | |
731 | | static void FSE_initDState(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD, const FSE_DTable* dt) |
732 | 18.2k | { |
733 | 18.2k | const void* ptr = dt; |
734 | 18.2k | const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr; |
735 | 18.2k | DStatePtr->state = FSE_readBits(bitD, DTableH->tableLog); |
736 | 18.2k | FSE_reloadDStream(bitD); |
737 | 18.2k | DStatePtr->table = dt + 1; |
738 | 18.2k | } |
739 | | |
740 | | static BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD) |
741 | 898k | { |
742 | 898k | const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
743 | 898k | const U32 nbBits = DInfo.nbBits; |
744 | 898k | BYTE symbol = DInfo.symbol; |
745 | 898k | size_t lowBits = FSE_readBits(bitD, nbBits); |
746 | | |
747 | 898k | DStatePtr->state = DInfo.newState + lowBits; |
748 | 898k | return symbol; |
749 | 898k | } |
750 | | |
751 | | static BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD) |
752 | 11.3k | { |
753 | 11.3k | const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; |
754 | 11.3k | const U32 nbBits = DInfo.nbBits; |
755 | 11.3k | BYTE symbol = DInfo.symbol; |
756 | 11.3k | size_t lowBits = FSE_readBitsFast(bitD, nbBits); |
757 | | |
758 | 11.3k | DStatePtr->state = DInfo.newState + lowBits; |
759 | 11.3k | return symbol; |
760 | 11.3k | } |
761 | | |
762 | | /* FSE_endOfDStream |
763 | | Tells if bitD has reached end of bitStream or not */ |
764 | | |
765 | | static unsigned FSE_endOfDStream(const FSE_DStream_t* bitD) |
766 | 35.3k | { |
767 | 35.3k | return ((bitD->ptr == bitD->start) && (bitD->bitsConsumed == sizeof(bitD->bitContainer)*8)); |
768 | 35.3k | } |
769 | | |
770 | | static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr) |
771 | 4.22k | { |
772 | 4.22k | return DStatePtr->state == 0; |
773 | 4.22k | } |
774 | | |
775 | | |
776 | | FORCE_INLINE size_t FSE_decompress_usingDTable_generic( |
777 | | void* dst, size_t maxDstSize, |
778 | | const void* cSrc, size_t cSrcSize, |
779 | | const FSE_DTable* dt, const unsigned fast) |
780 | 407 | { |
781 | 407 | BYTE* const ostart = (BYTE*) dst; |
782 | 407 | BYTE* op = ostart; |
783 | 407 | BYTE* const omax = op + maxDstSize; |
784 | 407 | BYTE* const olimit = omax-3; |
785 | | |
786 | 407 | FSE_DStream_t bitD; |
787 | 407 | FSE_DState_t state1; |
788 | 407 | FSE_DState_t state2; |
789 | 407 | size_t errorCode; |
790 | | |
791 | | /* Init */ |
792 | 407 | errorCode = FSE_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */ |
793 | 407 | if (FSE_isError(errorCode)) return errorCode; |
794 | | |
795 | 372 | FSE_initDState(&state1, &bitD, dt); |
796 | 372 | FSE_initDState(&state2, &bitD, dt); |
797 | | |
798 | 49.2k | #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) |
799 | | |
800 | | /* 4 symbols per loop */ |
801 | 5.40k | for ( ; (FSE_reloadDStream(&bitD)==FSE_DStream_unfinished) && (op<olimit) ; op+=4) |
802 | 5.02k | { |
803 | 5.02k | op[0] = FSE_GETSYMBOL(&state1); |
804 | | |
805 | 5.02k | if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
806 | 0 | FSE_reloadDStream(&bitD); |
807 | | |
808 | 5.02k | op[1] = FSE_GETSYMBOL(&state2); |
809 | | |
810 | 5.02k | if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
811 | 0 | { if (FSE_reloadDStream(&bitD) > FSE_DStream_unfinished) { op+=2; break; } } |
812 | | |
813 | 5.02k | op[2] = FSE_GETSYMBOL(&state1); |
814 | | |
815 | 5.02k | if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ |
816 | 0 | FSE_reloadDStream(&bitD); |
817 | | |
818 | 5.02k | op[3] = FSE_GETSYMBOL(&state2); |
819 | 5.02k | } |
820 | | |
821 | | /* tail */ |
822 | | /* note : FSE_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly FSE_DStream_completed */ |
823 | 14.8k | while (1) |
824 | 14.8k | { |
825 | 14.8k | if ( (FSE_reloadDStream(&bitD)>FSE_DStream_completed) || (op==omax) || (FSE_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) ) |
826 | 146 | break; |
827 | | |
828 | 14.6k | *op++ = FSE_GETSYMBOL(&state1); |
829 | | |
830 | 14.6k | if ( (FSE_reloadDStream(&bitD)>FSE_DStream_completed) || (op==omax) || (FSE_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) ) |
831 | 226 | break; |
832 | | |
833 | 14.4k | *op++ = FSE_GETSYMBOL(&state2); |
834 | 14.4k | } |
835 | | |
836 | | /* end ? */ |
837 | 372 | if (FSE_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2)) |
838 | 102 | return op-ostart; |
839 | | |
840 | 270 | if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */ |
841 | | |
842 | 198 | return (size_t)-FSE_ERROR_corruptionDetected; |
843 | 270 | } |
844 | | |
845 | | |
846 | | static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize, |
847 | | const void* cSrc, size_t cSrcSize, |
848 | | const FSE_DTable* dt) |
849 | 407 | { |
850 | 407 | FSE_DTableHeader DTableH; |
851 | 407 | memcpy(&DTableH, dt, sizeof(DTableH)); /* memcpy() into local variable, to avoid strict aliasing warning */ |
852 | | |
853 | | /* select fast mode (static) */ |
854 | 407 | if (DTableH.fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1); |
855 | 256 | return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0); |
856 | 407 | } |
857 | | |
858 | | |
859 | | static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize) |
860 | 511 | { |
861 | 511 | const BYTE* const istart = (const BYTE*)cSrc; |
862 | 511 | const BYTE* ip = istart; |
863 | 511 | short counting[FSE_MAX_SYMBOL_VALUE+1]; |
864 | 511 | DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */ |
865 | 511 | unsigned tableLog; |
866 | 511 | unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; |
867 | 511 | size_t errorCode; |
868 | | |
869 | 511 | if (cSrcSize<2) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */ |
870 | | |
871 | | /* normal FSE decoding mode */ |
872 | 490 | errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); |
873 | 490 | if (FSE_isError(errorCode)) return errorCode; |
874 | 417 | if (errorCode >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */ |
875 | 411 | ip += errorCode; |
876 | 411 | cSrcSize -= errorCode; |
877 | | |
878 | 411 | errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog); |
879 | 411 | if (FSE_isError(errorCode)) return errorCode; |
880 | | |
881 | | /* always return, even if it is an error code */ |
882 | 407 | return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); |
883 | 411 | } |
884 | | |
885 | | |
886 | | |
887 | | /* ******************************************************* |
888 | | * Huff0 : Huffman block compression |
889 | | *********************************************************/ |
890 | 511 | #define HUF_MAX_SYMBOL_VALUE 255 |
891 | | #define HUF_DEFAULT_TABLELOG 12 /* used by default, when not specified */ |
892 | 0 | #define HUF_MAX_TABLELOG 12 /* max possible tableLog; for allocation purpose; can be modified */ |
893 | 85.9k | #define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ |
894 | | #if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG) |
895 | | # error "HUF_MAX_TABLELOG is too large !" |
896 | | #endif |
897 | | |
898 | | typedef struct HUF_CElt_s { |
899 | | U16 val; |
900 | | BYTE nbBits; |
901 | | } HUF_CElt ; |
902 | | |
903 | | typedef struct nodeElt_s { |
904 | | U32 count; |
905 | | U16 parent; |
906 | | BYTE byte; |
907 | | BYTE nbBits; |
908 | | } nodeElt; |
909 | | |
910 | | |
911 | | /* ******************************************************* |
912 | | * Huff0 : Huffman block decompression |
913 | | *********************************************************/ |
914 | | typedef struct { |
915 | | BYTE byte; |
916 | | BYTE nbBits; |
917 | | } HUF_DElt; |
918 | | |
919 | | static size_t HUF_readDTable (U16* DTable, const void* src, size_t srcSize) |
920 | 1.29k | { |
921 | 1.29k | BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1]; |
922 | 1.29k | U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */ |
923 | 1.29k | U32 weightTotal; |
924 | 1.29k | U32 maxBits; |
925 | 1.29k | const BYTE* ip = (const BYTE*) src; |
926 | 1.29k | size_t iSize; |
927 | 1.29k | size_t oSize; |
928 | 1.29k | U32 n; |
929 | 1.29k | U32 nextRankStart; |
930 | 1.29k | void* ptr = DTable+1; |
931 | 1.29k | HUF_DElt* const dt = (HUF_DElt*)ptr; |
932 | | |
933 | 1.29k | if (!srcSize) return (size_t)-FSE_ERROR_srcSize_wrong; |
934 | 1.29k | iSize = ip[0]; |
935 | | |
936 | 1.29k | FSE_STATIC_ASSERT(sizeof(HUF_DElt) == sizeof(U16)); /* if compilation fails here, assertion is false */ |
937 | | //memset(huffWeight, 0, sizeof(huffWeight)); /* should not be necessary, but some analyzer complain ... */ |
938 | 1.29k | if (iSize >= 128) /* special header */ |
939 | 776 | { |
940 | 776 | if (iSize >= (242)) /* RLE */ |
941 | 679 | { |
942 | 679 | static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 }; |
943 | 679 | oSize = l[iSize-242]; |
944 | 679 | memset(huffWeight, 1, sizeof(huffWeight)); |
945 | 679 | iSize = 0; |
946 | 679 | } |
947 | 97 | else /* Incompressible */ |
948 | 97 | { |
949 | 97 | oSize = iSize - 127; |
950 | 97 | iSize = ((oSize+1)/2); |
951 | 97 | if (iSize+1 > srcSize) return (size_t)-FSE_ERROR_srcSize_wrong; |
952 | 76 | ip += 1; |
953 | 1.09k | for (n=0; n<oSize; n+=2) |
954 | 1.02k | { |
955 | 1.02k | huffWeight[n] = ip[n/2] >> 4; |
956 | 1.02k | huffWeight[n+1] = ip[n/2] & 15; |
957 | 1.02k | } |
958 | 76 | } |
959 | 776 | } |
960 | 516 | else /* header compressed with FSE (normal case) */ |
961 | 516 | { |
962 | 516 | if (iSize+1 > srcSize) return (size_t)-FSE_ERROR_srcSize_wrong; |
963 | 511 | oSize = FSE_decompress(huffWeight, HUF_MAX_SYMBOL_VALUE, ip+1, iSize); /* max 255 values decoded, last one is implied */ |
964 | 511 | if (FSE_isError(oSize)) return oSize; |
965 | 511 | } |
966 | | |
967 | | /* collect weight stats */ |
968 | 857 | memset(rankVal, 0, sizeof(rankVal)); |
969 | 857 | weightTotal = 0; |
970 | 86.7k | for (n=0; n<oSize; n++) |
971 | 85.9k | { |
972 | 85.9k | if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return (size_t)-FSE_ERROR_corruptionDetected; |
973 | 85.9k | rankVal[huffWeight[n]]++; |
974 | 85.9k | weightTotal += (1 << huffWeight[n]) >> 1; |
975 | 85.9k | } |
976 | 852 | if (weightTotal == 0) return (size_t)-FSE_ERROR_corruptionDetected; |
977 | | |
978 | | /* get last non-null symbol weight (implied, total must be 2^n) */ |
979 | 846 | maxBits = FSE_highbit32(weightTotal) + 1; |
980 | 846 | if (maxBits > DTable[0]) return (size_t)-FSE_ERROR_tableLog_tooLarge; /* DTable is too small */ |
981 | 807 | DTable[0] = (U16)maxBits; |
982 | 807 | { |
983 | 807 | U32 total = 1 << maxBits; |
984 | 807 | U32 rest = total - weightTotal; |
985 | 807 | U32 verif = 1 << FSE_highbit32(rest); |
986 | 807 | U32 lastWeight = FSE_highbit32(rest) + 1; |
987 | 807 | if (verif != rest) return (size_t)-FSE_ERROR_corruptionDetected; /* last value must be a clean power of 2 */ |
988 | 796 | huffWeight[oSize] = (BYTE)lastWeight; |
989 | 796 | rankVal[lastWeight]++; |
990 | 796 | } |
991 | | |
992 | | /* check tree construction validity */ |
993 | 796 | if ((rankVal[1] < 2) || (rankVal[1] & 1)) return (size_t)-FSE_ERROR_corruptionDetected; /* by construction : at least 2 elts of rank 1, must be even */ |
994 | | |
995 | | /* Prepare ranks */ |
996 | 793 | nextRankStart = 0; |
997 | 5.91k | for (n=1; n<=maxBits; n++) |
998 | 5.12k | { |
999 | 5.12k | U32 current = nextRankStart; |
1000 | 5.12k | nextRankStart += (rankVal[n] << (n-1)); |
1001 | 5.12k | rankVal[n] = current; |
1002 | 5.12k | } |
1003 | | |
1004 | | /* fill DTable */ |
1005 | 85.3k | for (n=0; n<=oSize; n++) |
1006 | 84.5k | { |
1007 | 84.5k | const U32 w = huffWeight[n]; |
1008 | 84.5k | const U32 length = (1 << w) >> 1; |
1009 | 84.5k | U32 i; |
1010 | 84.5k | HUF_DElt D; |
1011 | 84.5k | D.byte = (BYTE)n; D.nbBits = (BYTE)(maxBits + 1 - w); |
1012 | 225k | for (i = rankVal[w]; i < rankVal[w] + length; i++) |
1013 | 141k | dt[i] = D; |
1014 | 84.5k | rankVal[w] += length; |
1015 | 84.5k | } |
1016 | | |
1017 | 793 | return iSize+1; |
1018 | 796 | } |
1019 | | |
1020 | | |
1021 | | static BYTE HUF_decodeSymbol(FSE_DStream_t* Dstream, const HUF_DElt* dt, const U32 dtLog) |
1022 | 2.04M | { |
1023 | 2.04M | const size_t val = FSE_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */ |
1024 | 2.04M | const BYTE c = dt[val].byte; |
1025 | 2.04M | FSE_skipBits(Dstream, dt[val].nbBits); |
1026 | 2.04M | return c; |
1027 | 2.04M | } |
1028 | | |
1029 | | static size_t HUF_decompress_usingDTable( /* -3% slower when non static */ |
1030 | | void* dst, size_t maxDstSize, |
1031 | | const void* cSrc, size_t cSrcSize, |
1032 | | const U16* DTable) |
1033 | 773 | { |
1034 | 773 | if (cSrcSize < 6) return (size_t)-FSE_ERROR_srcSize_wrong; |
1035 | 737 | { |
1036 | 737 | BYTE* const ostart = (BYTE*) dst; |
1037 | 737 | BYTE* op = ostart; |
1038 | 737 | BYTE* const omax = op + maxDstSize; |
1039 | 737 | BYTE* const olimit = maxDstSize < 15 ? op : omax-15; |
1040 | | |
1041 | 737 | const void* ptr = DTable; |
1042 | 737 | const HUF_DElt* const dt = (const HUF_DElt*)(ptr)+1; |
1043 | 737 | const U32 dtLog = DTable[0]; |
1044 | 737 | size_t errorCode; |
1045 | 737 | U32 reloadStatus; |
1046 | | |
1047 | | /* Init */ |
1048 | | |
1049 | 737 | const U16* jumpTable = (const U16*)cSrc; |
1050 | 737 | const size_t length1 = FSE_readLE16(jumpTable); |
1051 | 737 | const size_t length2 = FSE_readLE16(jumpTable+1); |
1052 | 737 | const size_t length3 = FSE_readLE16(jumpTable+2); |
1053 | 737 | const size_t length4 = cSrcSize - 6 - length1 - length2 - length3; /* check coherency !! */ |
1054 | 737 | const char* const start1 = (const char*)(cSrc) + 6; |
1055 | 737 | const char* const start2 = start1 + length1; |
1056 | 737 | const char* const start3 = start2 + length2; |
1057 | 737 | const char* const start4 = start3 + length3; |
1058 | 737 | FSE_DStream_t bitD1, bitD2, bitD3, bitD4; |
1059 | | |
1060 | 737 | if (length1+length2+length3+6 >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong; |
1061 | | |
1062 | 666 | errorCode = FSE_initDStream(&bitD1, start1, length1); |
1063 | 666 | if (FSE_isError(errorCode)) return errorCode; |
1064 | 656 | errorCode = FSE_initDStream(&bitD2, start2, length2); |
1065 | 656 | if (FSE_isError(errorCode)) return errorCode; |
1066 | 642 | errorCode = FSE_initDStream(&bitD3, start3, length3); |
1067 | 642 | if (FSE_isError(errorCode)) return errorCode; |
1068 | 627 | errorCode = FSE_initDStream(&bitD4, start4, length4); |
1069 | 627 | if (FSE_isError(errorCode)) return errorCode; |
1070 | | |
1071 | 615 | reloadStatus=FSE_reloadDStream(&bitD2); |
1072 | | |
1073 | | /* 16 symbols per loop */ |
1074 | 99.6k | for ( ; (reloadStatus<FSE_DStream_completed) && (op<olimit); /* D2-3-4 are supposed to be synchronized and finish together */ |
1075 | 98.9k | op+=16, reloadStatus = FSE_reloadDStream(&bitD2) | FSE_reloadDStream(&bitD3) | FSE_reloadDStream(&bitD4), FSE_reloadDStream(&bitD1)) |
1076 | 98.9k | { |
1077 | 98.9k | #define HUF_DECODE_SYMBOL_0(n, Dstream) \ |
1078 | 856k | op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); |
1079 | | |
1080 | 98.9k | #define HUF_DECODE_SYMBOL_1(n, Dstream) \ |
1081 | 791k | op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \ |
1082 | 791k | if (FSE_32bits() && (HUF_MAX_TABLELOG>12)) FSE_reloadDStream(&Dstream) |
1083 | | |
1084 | 98.9k | #define HUF_DECODE_SYMBOL_2(n, Dstream) \ |
1085 | 395k | op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \ |
1086 | 395k | if (FSE_32bits()) FSE_reloadDStream(&Dstream) |
1087 | | |
1088 | 98.9k | HUF_DECODE_SYMBOL_1( 0, bitD1); |
1089 | 98.9k | HUF_DECODE_SYMBOL_1( 1, bitD2); |
1090 | 98.9k | HUF_DECODE_SYMBOL_1( 2, bitD3); |
1091 | 98.9k | HUF_DECODE_SYMBOL_1( 3, bitD4); |
1092 | 98.9k | HUF_DECODE_SYMBOL_2( 4, bitD1); |
1093 | 98.9k | HUF_DECODE_SYMBOL_2( 5, bitD2); |
1094 | 98.9k | HUF_DECODE_SYMBOL_2( 6, bitD3); |
1095 | 98.9k | HUF_DECODE_SYMBOL_2( 7, bitD4); |
1096 | 98.9k | HUF_DECODE_SYMBOL_1( 8, bitD1); |
1097 | 98.9k | HUF_DECODE_SYMBOL_1( 9, bitD2); |
1098 | 98.9k | HUF_DECODE_SYMBOL_1(10, bitD3); |
1099 | 98.9k | HUF_DECODE_SYMBOL_1(11, bitD4); |
1100 | 98.9k | HUF_DECODE_SYMBOL_0(12, bitD1); |
1101 | 98.9k | HUF_DECODE_SYMBOL_0(13, bitD2); |
1102 | 98.9k | HUF_DECODE_SYMBOL_0(14, bitD3); |
1103 | 98.9k | HUF_DECODE_SYMBOL_0(15, bitD4); |
1104 | 98.9k | } |
1105 | | |
1106 | 615 | if (reloadStatus!=FSE_DStream_completed) /* not complete : some bitStream might be FSE_DStream_unfinished */ |
1107 | 154 | return (size_t)-FSE_ERROR_corruptionDetected; |
1108 | | |
1109 | | /* tail */ |
1110 | 461 | { |
1111 | | /* bitTail = bitD1; */ /* *much* slower : -20% !??! */ |
1112 | 461 | FSE_DStream_t bitTail; |
1113 | 461 | bitTail.ptr = bitD1.ptr; |
1114 | 461 | bitTail.bitsConsumed = bitD1.bitsConsumed; |
1115 | 461 | bitTail.bitContainer = bitD1.bitContainer; /* required in case of FSE_DStream_endOfBuffer */ |
1116 | 461 | bitTail.start = start1; |
1117 | 460k | for ( ; (FSE_reloadDStream(&bitTail) < FSE_DStream_completed) && (op<omax) ; op++) |
1118 | 460k | { |
1119 | 460k | HUF_DECODE_SYMBOL_0(0, bitTail); |
1120 | 460k | } |
1121 | | |
1122 | 461 | if (FSE_endOfDStream(&bitTail)) |
1123 | 418 | return op-ostart; |
1124 | 461 | } |
1125 | | |
1126 | 43 | if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */ |
1127 | | |
1128 | 23 | return (size_t)-FSE_ERROR_corruptionDetected; |
1129 | 43 | } |
1130 | 43 | } |
1131 | | |
1132 | | |
1133 | | static size_t HUF_decompress (void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize) |
1134 | 1.29k | { |
1135 | 1.29k | HUF_CREATE_STATIC_DTABLE(DTable, HUF_MAX_TABLELOG); |
1136 | 1.29k | const BYTE* ip = (const BYTE*) cSrc; |
1137 | 1.29k | size_t errorCode; |
1138 | | |
1139 | 1.29k | errorCode = HUF_readDTable (DTable, cSrc, cSrcSize); |
1140 | 1.29k | if (FSE_isError(errorCode)) return errorCode; |
1141 | 793 | if (errorCode >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong; |
1142 | 773 | ip += errorCode; |
1143 | 773 | cSrcSize -= errorCode; |
1144 | | |
1145 | 773 | return HUF_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, DTable); |
1146 | 793 | } |
1147 | | |
1148 | | |
1149 | | #endif /* FSE_COMMONDEFS_ONLY */ |
1150 | | |
1151 | | /* |
1152 | | zstd - standard compression library |
1153 | | Copyright (C) 2014-2015, Yann Collet. |
1154 | | |
1155 | | BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) |
1156 | | |
1157 | | Redistribution and use in source and binary forms, with or without |
1158 | | modification, are permitted provided that the following conditions are |
1159 | | met: |
1160 | | * Redistributions of source code must retain the above copyright |
1161 | | notice, this list of conditions and the following disclaimer. |
1162 | | * Redistributions in binary form must reproduce the above |
1163 | | copyright notice, this list of conditions and the following disclaimer |
1164 | | in the documentation and/or other materials provided with the |
1165 | | distribution. |
1166 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
1167 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
1168 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
1169 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
1170 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
1171 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
1172 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
1173 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
1174 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
1175 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
1176 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1177 | | |
1178 | | You can contact the author at : |
1179 | | - zstd source repository : https://github.com/Cyan4973/zstd |
1180 | | - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c |
1181 | | */ |
1182 | | |
1183 | | /**************************************************************** |
1184 | | * Tuning parameters |
1185 | | *****************************************************************/ |
1186 | | /* MEMORY_USAGE : |
1187 | | * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) |
1188 | | * Increasing memory usage improves compression ratio |
1189 | | * Reduced memory usage can improve speed, due to cache effect */ |
1190 | | #define ZSTD_MEMORY_USAGE 17 |
1191 | | |
1192 | | |
1193 | | /************************************** |
1194 | | CPU Feature Detection |
1195 | | **************************************/ |
1196 | | /* |
1197 | | * Automated efficient unaligned memory access detection |
1198 | | * Based on known hardware architectures |
1199 | | * This list will be updated thanks to feedbacks |
1200 | | */ |
1201 | | #if defined(CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS) \ |
1202 | | || defined(__ARM_FEATURE_UNALIGNED) \ |
1203 | | || defined(__i386__) || defined(__x86_64__) \ |
1204 | | || defined(_M_IX86) || defined(_M_X64) \ |
1205 | | || defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) \ |
1206 | | || (defined(_M_ARM) && (_M_ARM >= 7)) |
1207 | | # define ZSTD_UNALIGNED_ACCESS 1 |
1208 | | #else |
1209 | | # define ZSTD_UNALIGNED_ACCESS 0 |
1210 | | #endif |
1211 | | |
1212 | | |
1213 | | /******************************************************** |
1214 | | * Includes |
1215 | | *********************************************************/ |
1216 | | #include <stdlib.h> /* calloc */ |
1217 | | #include <string.h> /* memcpy, memmove */ |
1218 | | #include <stdio.h> /* debug : printf */ |
1219 | | |
1220 | | |
1221 | | /******************************************************** |
1222 | | * Compiler specifics |
1223 | | *********************************************************/ |
1224 | | #ifdef __AVX2__ |
1225 | | # include <immintrin.h> /* AVX2 intrinsics */ |
1226 | | #endif |
1227 | | |
1228 | | #ifdef _MSC_VER /* Visual Studio */ |
1229 | | # include <intrin.h> /* For Visual 2005 */ |
1230 | | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
1231 | | # pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
1232 | | #endif |
1233 | | |
1234 | | |
1235 | | #ifndef MEM_ACCESS_MODULE |
1236 | | #define MEM_ACCESS_MODULE |
1237 | | /******************************************************** |
1238 | | * Basic Types |
1239 | | *********************************************************/ |
1240 | | #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
1241 | | # if defined(_AIX) |
1242 | | # include <inttypes.h> |
1243 | | # else |
1244 | | # include <stdint.h> /* intptr_t */ |
1245 | | # endif |
1246 | | typedef uint8_t BYTE; |
1247 | | typedef uint16_t U16; |
1248 | | typedef int16_t S16; |
1249 | | typedef uint32_t U32; |
1250 | | typedef int32_t S32; |
1251 | | typedef uint64_t U64; |
1252 | | #else |
1253 | | typedef unsigned char BYTE; |
1254 | | typedef unsigned short U16; |
1255 | | typedef signed short S16; |
1256 | | typedef unsigned int U32; |
1257 | | typedef signed int S32; |
1258 | | typedef unsigned long long U64; |
1259 | | #endif |
1260 | | |
1261 | | #endif /* MEM_ACCESS_MODULE */ |
1262 | | |
1263 | | |
1264 | | /******************************************************** |
1265 | | * Constants |
1266 | | *********************************************************/ |
1267 | | static const U32 ZSTD_magicNumber = 0xFD2FB51E; /* 3rd version : seqNb header */ |
1268 | | |
1269 | | #define HASH_LOG (ZSTD_MEMORY_USAGE - 2) |
1270 | | #define HASH_TABLESIZE (1 << HASH_LOG) |
1271 | | #define HASH_MASK (HASH_TABLESIZE - 1) |
1272 | | |
1273 | | #define KNUTH 2654435761 |
1274 | | |
1275 | | #define BIT7 128 |
1276 | | #define BIT6 64 |
1277 | | #define BIT5 32 |
1278 | | #define BIT4 16 |
1279 | | |
1280 | 5.83k | #define KB *(1 <<10) |
1281 | | #define MB *(1 <<20) |
1282 | | #define GB *(1U<<30) |
1283 | | |
1284 | 5.83k | #define BLOCKSIZE (128 KB) /* define, for static allocation */ |
1285 | | |
1286 | | #define WORKPLACESIZE (BLOCKSIZE*3) |
1287 | 573k | #define MINMATCH 4 |
1288 | 295k | #define MLbits 7 |
1289 | 294k | #define LLbits 6 |
1290 | 6.25k | #define Offbits 5 |
1291 | 288k | #define MaxML ((1<<MLbits )-1) |
1292 | 288k | #define MaxLL ((1<<LLbits )-1) |
1293 | 1.67k | #define MaxOff ((1<<Offbits)-1) |
1294 | | #define LitFSELog 11 |
1295 | 1.25k | #define MLFSELog 10 |
1296 | 1.57k | #define LLFSELog 10 |
1297 | 1.62k | #define OffFSELog 9 |
1298 | | #define MAX(a,b) ((a)<(b)?(b):(a)) |
1299 | | #define MaxSeq MAX(MaxLL, MaxML) |
1300 | | |
1301 | | #define LITERAL_NOENTROPY 63 |
1302 | | #define COMMAND_NOENTROPY 7 /* to remove */ |
1303 | | |
1304 | 315 | #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) |
1305 | | |
1306 | | static const size_t ZSTD_blockHeaderSize = 3; |
1307 | | static const size_t ZSTD_frameHeaderSize = 4; |
1308 | | |
1309 | | |
1310 | | /******************************************************** |
1311 | | * Memory operations |
1312 | | *********************************************************/ |
1313 | 573k | static unsigned ZSTD_32bits(void) { return sizeof(void*)==4; } |
1314 | | |
1315 | | static unsigned ZSTD_isLittleEndian(void) |
1316 | 6.39k | { |
1317 | 6.39k | const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ |
1318 | 6.39k | return one.c[0]; |
1319 | 6.39k | } |
1320 | | |
1321 | 6.39k | static U16 ZSTD_read16(const void* p) { U16 r; memcpy(&r, p, sizeof(r)); return r; } |
1322 | | |
1323 | 233k | static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); } |
1324 | | |
1325 | 2.92M | static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } |
1326 | | |
1327 | 2.87M | #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } |
1328 | | |
1329 | | static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length) |
1330 | 286k | { |
1331 | 286k | const BYTE* ip = (const BYTE*)src; |
1332 | 286k | BYTE* op = (BYTE*)dst; |
1333 | 286k | BYTE* const oend = op + length; |
1334 | 3.16M | while (op < oend) COPY8(op, ip); |
1335 | 286k | } |
1336 | | |
1337 | | static U16 ZSTD_readLE16(const void* memPtr) |
1338 | 6.39k | { |
1339 | 6.39k | if (ZSTD_isLittleEndian()) return ZSTD_read16(memPtr); |
1340 | 0 | else |
1341 | 0 | { |
1342 | 0 | const BYTE* p = (const BYTE*)memPtr; |
1343 | 0 | return (U16)((U16)p[0] + ((U16)p[1]<<8)); |
1344 | 0 | } |
1345 | 6.39k | } |
1346 | | |
1347 | | static U32 ZSTD_readLE24(const void* memPtr) |
1348 | 293 | { |
1349 | 293 | return ZSTD_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16); |
1350 | 293 | } |
1351 | | |
1352 | | static U32 ZSTD_readBE32(const void* memPtr) |
1353 | 10.8k | { |
1354 | 10.8k | const BYTE* p = (const BYTE*)memPtr; |
1355 | 10.8k | return (U32)(((U32)p[0]<<24) + ((U32)p[1]<<16) + ((U32)p[2]<<8) + ((U32)p[3]<<0)); |
1356 | 10.8k | } |
1357 | | |
1358 | | |
1359 | | /************************************** |
1360 | | * Local structures |
1361 | | ***************************************/ |
1362 | | typedef struct ZSTD_Cctx_s ZSTD_Cctx; |
1363 | | |
1364 | | typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t; |
1365 | | |
1366 | | typedef struct |
1367 | | { |
1368 | | blockType_t blockType; |
1369 | | U32 origSize; |
1370 | | } blockProperties_t; |
1371 | | |
1372 | | typedef struct { |
1373 | | void* buffer; |
1374 | | U32* offsetStart; |
1375 | | U32* offset; |
1376 | | BYTE* offCodeStart; |
1377 | | BYTE* offCode; |
1378 | | BYTE* litStart; |
1379 | | BYTE* lit; |
1380 | | BYTE* litLengthStart; |
1381 | | BYTE* litLength; |
1382 | | BYTE* matchLengthStart; |
1383 | | BYTE* matchLength; |
1384 | | BYTE* dumpsStart; |
1385 | | BYTE* dumps; |
1386 | | } seqStore_t; |
1387 | | |
1388 | | |
1389 | | typedef struct ZSTD_Cctx_s |
1390 | | { |
1391 | | const BYTE* base; |
1392 | | U32 current; |
1393 | | U32 nextUpdate; |
1394 | | seqStore_t seqStore; |
1395 | | #ifdef __AVX2__ |
1396 | | __m256i hashTable[HASH_TABLESIZE>>3]; |
1397 | | #else |
1398 | | U32 hashTable[HASH_TABLESIZE]; |
1399 | | #endif |
1400 | | BYTE buffer[WORKPLACESIZE]; |
1401 | | } cctxi_t; |
1402 | | |
1403 | | |
1404 | | |
1405 | | |
1406 | | /************************************** |
1407 | | * Error Management |
1408 | | **************************************/ |
1409 | | /* published entry point */ |
1410 | 349k | unsigned ZSTDv01_isError(size_t code) { return ERR_isError(code); } |
1411 | | |
1412 | | |
1413 | | /************************************** |
1414 | | * Tool functions |
1415 | | **************************************/ |
1416 | | #define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */ |
1417 | | #define ZSTD_VERSION_MINOR 1 /* for new (non-breaking) interface capabilities */ |
1418 | | #define ZSTD_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */ |
1419 | | #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) |
1420 | | |
1421 | | /************************************************************** |
1422 | | * Decompression code |
1423 | | **************************************************************/ |
1424 | | |
1425 | | static size_t ZSTDv01_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr) |
1426 | 39.4k | { |
1427 | 39.4k | const BYTE* const in = (const BYTE* const)src; |
1428 | 39.4k | BYTE headerFlags; |
1429 | 39.4k | U32 cSize; |
1430 | | |
1431 | 39.4k | if (srcSize < 3) return ERROR(srcSize_wrong); |
1432 | | |
1433 | 38.7k | headerFlags = *in; |
1434 | 38.7k | cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16); |
1435 | | |
1436 | 38.7k | bpPtr->blockType = (blockType_t)(headerFlags >> 6); |
1437 | 38.7k | bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0; |
1438 | | |
1439 | 38.7k | if (bpPtr->blockType == bt_end) return 0; |
1440 | 32.6k | if (bpPtr->blockType == bt_rle) return 1; |
1441 | 23.9k | return cSize; |
1442 | 32.6k | } |
1443 | | |
1444 | | |
1445 | | static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
1446 | 876 | { |
1447 | 876 | if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall); |
1448 | 861 | if (srcSize > 0) { |
1449 | 553 | memcpy(dst, src, srcSize); |
1450 | 553 | } |
1451 | 861 | return srcSize; |
1452 | 876 | } |
1453 | | |
1454 | | |
1455 | | static size_t ZSTD_decompressLiterals(void* ctx, |
1456 | | void* dst, size_t maxDstSize, |
1457 | | const void* src, size_t srcSize) |
1458 | 1.33k | { |
1459 | 1.33k | BYTE* op = (BYTE*)dst; |
1460 | 1.33k | BYTE* const oend = op + maxDstSize; |
1461 | 1.33k | const BYTE* ip = (const BYTE*)src; |
1462 | 1.33k | size_t errorCode; |
1463 | 1.33k | size_t litSize; |
1464 | | |
1465 | | /* check : minimum 2, for litSize, +1, for content */ |
1466 | 1.33k | if (srcSize <= 3) return ERROR(corruption_detected); |
1467 | | |
1468 | 1.33k | litSize = ip[1] + (ip[0]<<8); |
1469 | 1.33k | litSize += ((ip[-3] >> 3) & 7) << 16; /* mmmmh.... */ |
1470 | 1.33k | op = oend - litSize; |
1471 | | |
1472 | 1.33k | (void)ctx; |
1473 | 1.33k | if (litSize > maxDstSize) return ERROR(dstSize_tooSmall); |
1474 | 1.29k | errorCode = HUF_decompress(op, litSize, ip+2, srcSize-2); |
1475 | 1.29k | if (FSE_isError(errorCode)) return ERROR(GENERIC); |
1476 | 418 | return litSize; |
1477 | 1.29k | } |
1478 | | |
1479 | | |
1480 | | static size_t ZSTDv01_decodeLiteralsBlock(void* ctx, |
1481 | | void* dst, size_t maxDstSize, |
1482 | | const BYTE** litStart, size_t* litSize, |
1483 | | const void* src, size_t srcSize) |
1484 | 7.77k | { |
1485 | 7.77k | const BYTE* const istart = (const BYTE* const)src; |
1486 | 7.77k | const BYTE* ip = istart; |
1487 | 7.77k | BYTE* const ostart = (BYTE* const)dst; |
1488 | 7.77k | BYTE* const oend = ostart + maxDstSize; |
1489 | 7.77k | blockProperties_t litbp; |
1490 | | |
1491 | 7.77k | size_t litcSize = ZSTDv01_getcBlockSize(src, srcSize, &litbp); |
1492 | 7.77k | if (ZSTDv01_isError(litcSize)) return litcSize; |
1493 | 7.20k | if (litcSize > srcSize - ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); |
1494 | 7.12k | ip += ZSTD_blockHeaderSize; |
1495 | | |
1496 | 7.12k | switch(litbp.blockType) |
1497 | 7.12k | { |
1498 | 3.14k | case bt_raw: |
1499 | 3.14k | *litStart = ip; |
1500 | 3.14k | ip += litcSize; |
1501 | 3.14k | *litSize = litcSize; |
1502 | 3.14k | break; |
1503 | 2.61k | case bt_rle: |
1504 | 2.61k | { |
1505 | 2.61k | size_t rleSize = litbp.origSize; |
1506 | 2.61k | if (rleSize>maxDstSize) return ERROR(dstSize_tooSmall); |
1507 | 2.57k | if (!srcSize) return ERROR(srcSize_wrong); |
1508 | 2.57k | if (rleSize > 0) { |
1509 | 808 | memset(oend - rleSize, *ip, rleSize); |
1510 | 808 | } |
1511 | 2.57k | *litStart = oend - rleSize; |
1512 | 2.57k | *litSize = rleSize; |
1513 | 2.57k | ip++; |
1514 | 2.57k | break; |
1515 | 2.57k | } |
1516 | 1.33k | case bt_compressed: |
1517 | 1.33k | { |
1518 | 1.33k | size_t decodedLitSize = ZSTD_decompressLiterals(ctx, dst, maxDstSize, ip, litcSize); |
1519 | 1.33k | if (ZSTDv01_isError(decodedLitSize)) return decodedLitSize; |
1520 | 418 | *litStart = oend - decodedLitSize; |
1521 | 418 | *litSize = decodedLitSize; |
1522 | 418 | ip += litcSize; |
1523 | 418 | break; |
1524 | 1.33k | } |
1525 | 23 | case bt_end: |
1526 | 23 | default: |
1527 | 23 | return ERROR(GENERIC); |
1528 | 7.12k | } |
1529 | | |
1530 | 6.13k | return ip-istart; |
1531 | 7.12k | } |
1532 | | |
1533 | | |
1534 | | static size_t ZSTDv01_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr, |
1535 | | FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb, |
1536 | | const void* src, size_t srcSize) |
1537 | 6.13k | { |
1538 | 6.13k | const BYTE* const istart = (const BYTE* const)src; |
1539 | 6.13k | const BYTE* ip = istart; |
1540 | 6.13k | const BYTE* const iend = istart + srcSize; |
1541 | 6.13k | U32 LLtype, Offtype, MLtype; |
1542 | 6.13k | U32 LLlog, Offlog, MLlog; |
1543 | 6.13k | size_t dumpsLength; |
1544 | | |
1545 | | /* check */ |
1546 | 6.13k | if (srcSize < 5) return ERROR(srcSize_wrong); |
1547 | | |
1548 | | /* SeqHead */ |
1549 | 6.09k | *nbSeq = ZSTD_readLE16(ip); ip+=2; |
1550 | 6.09k | LLtype = *ip >> 6; |
1551 | 6.09k | Offtype = (*ip >> 4) & 3; |
1552 | 6.09k | MLtype = (*ip >> 2) & 3; |
1553 | 6.09k | if (*ip & 2) |
1554 | 930 | { |
1555 | 930 | dumpsLength = ip[2]; |
1556 | 930 | dumpsLength += ip[1] << 8; |
1557 | 930 | ip += 3; |
1558 | 930 | } |
1559 | 5.16k | else |
1560 | 5.16k | { |
1561 | 5.16k | dumpsLength = ip[1]; |
1562 | 5.16k | dumpsLength += (ip[0] & 1) << 8; |
1563 | 5.16k | ip += 2; |
1564 | 5.16k | } |
1565 | 6.09k | *dumpsPtr = ip; |
1566 | 6.09k | ip += dumpsLength; |
1567 | 6.09k | *dumpsLengthPtr = dumpsLength; |
1568 | | |
1569 | | /* check */ |
1570 | 6.09k | if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */ |
1571 | | |
1572 | | /* sequences */ |
1573 | 6.06k | { |
1574 | 6.06k | S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */ |
1575 | 6.06k | size_t headerSize; |
1576 | | |
1577 | | /* Build DTables */ |
1578 | 6.06k | switch(LLtype) |
1579 | 6.06k | { |
1580 | 1.16k | case bt_rle : |
1581 | 1.16k | LLlog = 0; |
1582 | 1.16k | FSE_buildDTable_rle(DTableLL, *ip++); break; |
1583 | 3.28k | case bt_raw : |
1584 | 3.28k | LLlog = LLbits; |
1585 | 3.28k | FSE_buildDTable_raw(DTableLL, LLbits); break; |
1586 | 1.61k | default : |
1587 | 1.61k | { U32 max = MaxLL; |
1588 | 1.61k | headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip); |
1589 | 1.61k | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
1590 | 1.57k | if (LLlog > LLFSELog) return ERROR(corruption_detected); |
1591 | 1.56k | ip += headerSize; |
1592 | 1.56k | FSE_buildDTable(DTableLL, norm, max, LLlog); |
1593 | 1.56k | } } |
1594 | | |
1595 | 6.00k | switch(Offtype) |
1596 | 6.00k | { |
1597 | 2.04k | case bt_rle : |
1598 | 2.04k | Offlog = 0; |
1599 | 2.04k | if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ |
1600 | 2.03k | FSE_buildDTable_rle(DTableOffb, *ip++); break; |
1601 | 2.28k | case bt_raw : |
1602 | 2.28k | Offlog = Offbits; |
1603 | 2.28k | FSE_buildDTable_raw(DTableOffb, Offbits); break; |
1604 | 1.67k | default : |
1605 | 1.67k | { U32 max = MaxOff; |
1606 | 1.67k | headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip); |
1607 | 1.67k | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
1608 | 1.62k | if (Offlog > OffFSELog) return ERROR(corruption_detected); |
1609 | 1.62k | ip += headerSize; |
1610 | 1.62k | FSE_buildDTable(DTableOffb, norm, max, Offlog); |
1611 | 1.62k | } } |
1612 | | |
1613 | 5.95k | switch(MLtype) |
1614 | 5.95k | { |
1615 | 759 | case bt_rle : |
1616 | 759 | MLlog = 0; |
1617 | 759 | if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ |
1618 | 753 | FSE_buildDTable_rle(DTableML, *ip++); break; |
1619 | 3.89k | case bt_raw : |
1620 | 3.89k | MLlog = MLbits; |
1621 | 3.89k | FSE_buildDTable_raw(DTableML, MLbits); break; |
1622 | 1.29k | default : |
1623 | 1.29k | { U32 max = MaxML; |
1624 | 1.29k | headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip); |
1625 | 1.29k | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
1626 | 1.25k | if (MLlog > MLFSELog) return ERROR(corruption_detected); |
1627 | 1.24k | ip += headerSize; |
1628 | 1.24k | FSE_buildDTable(DTableML, norm, max, MLlog); |
1629 | 1.24k | } } } |
1630 | | |
1631 | 5.89k | return ip-istart; |
1632 | 5.95k | } |
1633 | | |
1634 | | |
1635 | | typedef struct { |
1636 | | size_t litLength; |
1637 | | size_t offset; |
1638 | | size_t matchLength; |
1639 | | } seq_t; |
1640 | | |
1641 | | typedef struct { |
1642 | | FSE_DStream_t DStream; |
1643 | | FSE_DState_t stateLL; |
1644 | | FSE_DState_t stateOffb; |
1645 | | FSE_DState_t stateML; |
1646 | | size_t prevOffset; |
1647 | | const BYTE* dumps; |
1648 | | const BYTE* dumpsEnd; |
1649 | | } seqState_t; |
1650 | | |
1651 | | |
1652 | | static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState) |
1653 | 286k | { |
1654 | 286k | size_t litLength; |
1655 | 286k | size_t prevOffset; |
1656 | 286k | size_t offset; |
1657 | 286k | size_t matchLength; |
1658 | 286k | const BYTE* dumps = seqState->dumps; |
1659 | 286k | const BYTE* const de = seqState->dumpsEnd; |
1660 | | |
1661 | | /* Literal length */ |
1662 | 286k | litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream)); |
1663 | 286k | prevOffset = litLength ? seq->offset : seqState->prevOffset; |
1664 | 286k | seqState->prevOffset = seq->offset; |
1665 | 286k | if (litLength == MaxLL) |
1666 | 2.67k | { |
1667 | 2.67k | const U32 add = dumps<de ? *dumps++ : 0; |
1668 | 2.67k | if (add < 255) litLength += add; |
1669 | 141 | else |
1670 | 141 | { |
1671 | 141 | if (dumps<=(de-3)) |
1672 | 122 | { |
1673 | 122 | litLength = ZSTD_readLE24(dumps); |
1674 | 122 | dumps += 3; |
1675 | 122 | } |
1676 | 141 | } |
1677 | 2.67k | } |
1678 | | |
1679 | | /* Offset */ |
1680 | 286k | { |
1681 | 286k | U32 offsetCode, nbBits; |
1682 | 286k | offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); |
1683 | 286k | if (ZSTD_32bits()) FSE_reloadDStream(&(seqState->DStream)); |
1684 | 286k | nbBits = offsetCode - 1; |
1685 | 286k | if (offsetCode==0) nbBits = 0; /* cmove */ |
1686 | 286k | offset = ((size_t)1 << (nbBits & ((sizeof(offset)*8)-1))) + FSE_readBits(&(seqState->DStream), nbBits); |
1687 | 286k | if (ZSTD_32bits()) FSE_reloadDStream(&(seqState->DStream)); |
1688 | 286k | if (offsetCode==0) offset = prevOffset; |
1689 | 286k | } |
1690 | | |
1691 | | /* MatchLength */ |
1692 | 286k | matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream)); |
1693 | 286k | if (matchLength == MaxML) |
1694 | 7.05k | { |
1695 | 7.05k | const U32 add = dumps<de ? *dumps++ : 0; |
1696 | 7.05k | if (add < 255) matchLength += add; |
1697 | 199 | else |
1698 | 199 | { |
1699 | 199 | if (dumps<=(de-3)) |
1700 | 171 | { |
1701 | 171 | matchLength = ZSTD_readLE24(dumps); |
1702 | 171 | dumps += 3; |
1703 | 171 | } |
1704 | 199 | } |
1705 | 7.05k | } |
1706 | 286k | matchLength += MINMATCH; |
1707 | | |
1708 | | /* save result */ |
1709 | 286k | seq->litLength = litLength; |
1710 | 286k | seq->offset = offset; |
1711 | 286k | seq->matchLength = matchLength; |
1712 | 286k | seqState->dumps = dumps; |
1713 | 286k | } |
1714 | | |
1715 | | |
1716 | | static size_t ZSTD_execSequence(BYTE* op, |
1717 | | seq_t sequence, |
1718 | | const BYTE** litPtr, const BYTE* const litLimit, |
1719 | | BYTE* const base, BYTE* const oend) |
1720 | 286k | { |
1721 | 286k | static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */ |
1722 | 286k | static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */ |
1723 | 286k | const BYTE* const ostart = op; |
1724 | 286k | BYTE* const oLitEnd = op + sequence.litLength; |
1725 | 286k | const size_t litLength = sequence.litLength; |
1726 | 286k | BYTE* const endMatch = op + litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */ |
1727 | 286k | const BYTE* const litEnd = *litPtr + litLength; |
1728 | | |
1729 | | /* checks */ |
1730 | 286k | size_t const seqLength = sequence.litLength + sequence.matchLength; |
1731 | | |
1732 | 286k | if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall); |
1733 | 286k | if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected); |
1734 | | /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */ |
1735 | 286k | if (sequence.offset > (U32)(oLitEnd - base)) return ERROR(corruption_detected); |
1736 | | |
1737 | 286k | if (endMatch > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */ |
1738 | 286k | if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */ |
1739 | 286k | if (sequence.matchLength > (size_t)(*litPtr-op)) return ERROR(dstSize_tooSmall); /* overwrite literal segment */ |
1740 | | |
1741 | | /* copy Literals */ |
1742 | 286k | ZSTD_memmove(op, *litPtr, sequence.litLength); /* note : v0.1 seems to allow scenarios where output or input are close to end of buffer */ |
1743 | | |
1744 | 286k | op += litLength; |
1745 | 286k | *litPtr = litEnd; /* update for next sequence */ |
1746 | | |
1747 | | /* check : last match must be at a minimum distance of 8 from end of dest buffer */ |
1748 | 286k | if (oend-op < 8) return ERROR(dstSize_tooSmall); |
1749 | | |
1750 | | /* copy Match */ |
1751 | 286k | { |
1752 | 286k | const U32 overlapRisk = (((size_t)(litEnd - endMatch)) < 12); |
1753 | 286k | const BYTE* match = op - sequence.offset; /* possible underflow at op - offset ? */ |
1754 | 286k | size_t qutt = 12; |
1755 | 286k | U64 saved[2]; |
1756 | | |
1757 | | /* check */ |
1758 | 286k | if (match < base) return ERROR(corruption_detected); |
1759 | 286k | if (sequence.offset > (size_t)base) return ERROR(corruption_detected); |
1760 | | |
1761 | | /* save beginning of literal sequence, in case of write overlap */ |
1762 | 286k | if (overlapRisk) |
1763 | 130 | { |
1764 | 130 | if ((endMatch + qutt) > oend) qutt = oend-endMatch; |
1765 | 130 | memcpy(saved, endMatch, qutt); |
1766 | 130 | } |
1767 | | |
1768 | 286k | if (sequence.offset < 8) |
1769 | 233k | { |
1770 | 233k | const int dec64 = dec64table[sequence.offset]; |
1771 | 233k | op[0] = match[0]; |
1772 | 233k | op[1] = match[1]; |
1773 | 233k | op[2] = match[2]; |
1774 | 233k | op[3] = match[3]; |
1775 | 233k | match += dec32table[sequence.offset]; |
1776 | 233k | ZSTD_copy4(op+4, match); |
1777 | 233k | match -= dec64; |
1778 | 233k | } else { ZSTD_copy8(op, match); } |
1779 | 286k | op += 8; match += 8; |
1780 | | |
1781 | 286k | if (endMatch > oend-(16-MINMATCH)) |
1782 | 104 | { |
1783 | 104 | if (op < oend-8) |
1784 | 42 | { |
1785 | 42 | ZSTD_wildcopy(op, match, (oend-8) - op); |
1786 | 42 | match += (oend-8) - op; |
1787 | 42 | op = oend-8; |
1788 | 42 | } |
1789 | 232 | while (op<endMatch) *op++ = *match++; |
1790 | 104 | } |
1791 | 286k | else |
1792 | 286k | ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */ |
1793 | | |
1794 | | /* restore, in case of overlap */ |
1795 | 286k | if (overlapRisk) memcpy(endMatch, saved, qutt); |
1796 | 286k | } |
1797 | | |
1798 | 0 | return endMatch-ostart; |
1799 | 286k | } |
1800 | | |
1801 | | typedef struct ZSTDv01_Dctx_s |
1802 | | { |
1803 | | U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)]; |
1804 | | U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)]; |
1805 | | U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)]; |
1806 | | void* previousDstEnd; |
1807 | | void* base; |
1808 | | size_t expected; |
1809 | | blockType_t bType; |
1810 | | U32 phase; |
1811 | | } dctx_t; |
1812 | | |
1813 | | |
1814 | | static size_t ZSTD_decompressSequences( |
1815 | | void* ctx, |
1816 | | void* dst, size_t maxDstSize, |
1817 | | const void* seqStart, size_t seqSize, |
1818 | | const BYTE* litStart, size_t litSize) |
1819 | 6.13k | { |
1820 | 6.13k | dctx_t* dctx = (dctx_t*)ctx; |
1821 | 6.13k | const BYTE* ip = (const BYTE*)seqStart; |
1822 | 6.13k | const BYTE* const iend = ip + seqSize; |
1823 | 6.13k | BYTE* const ostart = (BYTE* const)dst; |
1824 | 6.13k | BYTE* op = ostart; |
1825 | 6.13k | BYTE* const oend = ostart + maxDstSize; |
1826 | 6.13k | size_t errorCode, dumpsLength; |
1827 | 6.13k | const BYTE* litPtr = litStart; |
1828 | 6.13k | const BYTE* const litEnd = litStart + litSize; |
1829 | 6.13k | int nbSeq; |
1830 | 6.13k | const BYTE* dumps; |
1831 | 6.13k | U32* DTableLL = dctx->LLTable; |
1832 | 6.13k | U32* DTableML = dctx->MLTable; |
1833 | 6.13k | U32* DTableOffb = dctx->OffTable; |
1834 | 6.13k | BYTE* const base = (BYTE*) (dctx->base); |
1835 | | |
1836 | | /* Build Decoding Tables */ |
1837 | 6.13k | errorCode = ZSTDv01_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength, |
1838 | 6.13k | DTableLL, DTableML, DTableOffb, |
1839 | 6.13k | ip, iend-ip); |
1840 | 6.13k | if (ZSTDv01_isError(errorCode)) return errorCode; |
1841 | 5.89k | ip += errorCode; |
1842 | | |
1843 | | /* Regen sequences */ |
1844 | 5.89k | { |
1845 | 5.89k | seq_t sequence; |
1846 | 5.89k | seqState_t seqState; |
1847 | | |
1848 | 5.89k | memset(&sequence, 0, sizeof(sequence)); |
1849 | 5.89k | seqState.dumps = dumps; |
1850 | 5.89k | seqState.dumpsEnd = dumps + dumpsLength; |
1851 | 5.89k | seqState.prevOffset = 1; |
1852 | 5.89k | errorCode = FSE_initDStream(&(seqState.DStream), ip, iend-ip); |
1853 | 5.89k | if (FSE_isError(errorCode)) return ERROR(corruption_detected); |
1854 | 5.83k | FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL); |
1855 | 5.83k | FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb); |
1856 | 5.83k | FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML); |
1857 | | |
1858 | 292k | for ( ; (FSE_reloadDStream(&(seqState.DStream)) <= FSE_DStream_completed) && (nbSeq>0) ; ) |
1859 | 286k | { |
1860 | 286k | size_t oneSeqSize; |
1861 | 286k | nbSeq--; |
1862 | 286k | ZSTD_decodeSequence(&sequence, &seqState); |
1863 | 286k | oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litEnd, base, oend); |
1864 | 286k | if (ZSTDv01_isError(oneSeqSize)) return oneSeqSize; |
1865 | 286k | op += oneSeqSize; |
1866 | 286k | } |
1867 | | |
1868 | | /* check if reached exact end */ |
1869 | 5.24k | if ( !FSE_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* requested too much : data is corrupted */ |
1870 | 5.07k | if (nbSeq<0) return ERROR(corruption_detected); /* requested too many sequences : data is corrupted */ |
1871 | | |
1872 | | /* last literal segment */ |
1873 | 5.07k | { |
1874 | 5.07k | size_t lastLLSize = litEnd - litPtr; |
1875 | 5.07k | if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall); |
1876 | 5.07k | if (lastLLSize > 0) { |
1877 | 807 | if (op != litPtr) memmove(op, litPtr, lastLLSize); |
1878 | 807 | op += lastLLSize; |
1879 | 807 | } |
1880 | 5.07k | } |
1881 | 5.07k | } |
1882 | | |
1883 | 0 | return op-ostart; |
1884 | 5.07k | } |
1885 | | |
1886 | | |
1887 | | static size_t ZSTD_decompressBlock( |
1888 | | void* ctx, |
1889 | | void* dst, size_t maxDstSize, |
1890 | | const void* src, size_t srcSize) |
1891 | 7.77k | { |
1892 | | /* blockType == blockCompressed, srcSize is trusted */ |
1893 | 7.77k | const BYTE* ip = (const BYTE*)src; |
1894 | 7.77k | const BYTE* litPtr = NULL; |
1895 | 7.77k | size_t litSize = 0; |
1896 | 7.77k | size_t errorCode; |
1897 | | |
1898 | | /* Decode literals sub-block */ |
1899 | 7.77k | errorCode = ZSTDv01_decodeLiteralsBlock(ctx, dst, maxDstSize, &litPtr, &litSize, src, srcSize); |
1900 | 7.77k | if (ZSTDv01_isError(errorCode)) return errorCode; |
1901 | 6.13k | ip += errorCode; |
1902 | 6.13k | srcSize -= errorCode; |
1903 | | |
1904 | 6.13k | return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize, litPtr, litSize); |
1905 | 7.77k | } |
1906 | | |
1907 | | |
1908 | | size_t ZSTDv01_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
1909 | 4.70k | { |
1910 | 4.70k | const BYTE* ip = (const BYTE*)src; |
1911 | 4.70k | const BYTE* iend = ip + srcSize; |
1912 | 4.70k | BYTE* const ostart = (BYTE* const)dst; |
1913 | 4.70k | BYTE* op = ostart; |
1914 | 4.70k | BYTE* const oend = ostart + maxDstSize; |
1915 | 4.70k | size_t remainingSize = srcSize; |
1916 | 4.70k | U32 magicNumber; |
1917 | 4.70k | size_t errorCode=0; |
1918 | 4.70k | blockProperties_t blockProperties; |
1919 | | |
1920 | | /* Frame Header */ |
1921 | 4.70k | if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); |
1922 | 4.70k | magicNumber = ZSTD_readBE32(src); |
1923 | 4.70k | if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown); |
1924 | 4.70k | ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize; |
1925 | | |
1926 | | /* Loop on each block */ |
1927 | 10.3k | while (1) |
1928 | 10.3k | { |
1929 | 10.3k | size_t blockSize = ZSTDv01_getcBlockSize(ip, iend-ip, &blockProperties); |
1930 | 10.3k | if (ZSTDv01_isError(blockSize)) return blockSize; |
1931 | | |
1932 | 10.3k | ip += ZSTD_blockHeaderSize; |
1933 | 10.3k | remainingSize -= ZSTD_blockHeaderSize; |
1934 | 10.3k | if (blockSize > remainingSize) return ERROR(srcSize_wrong); |
1935 | | |
1936 | 10.3k | switch(blockProperties.blockType) |
1937 | 10.3k | { |
1938 | 7.77k | case bt_compressed: |
1939 | 7.77k | errorCode = ZSTD_decompressBlock(ctx, op, oend-op, ip, blockSize); |
1940 | 7.77k | break; |
1941 | 876 | case bt_raw : |
1942 | 876 | errorCode = ZSTD_copyUncompressedBlock(op, oend-op, ip, blockSize); |
1943 | 876 | break; |
1944 | 8 | case bt_rle : |
1945 | 8 | return ERROR(GENERIC); /* not yet supported */ |
1946 | 0 | break; |
1947 | 1.66k | case bt_end : |
1948 | | /* end of frame */ |
1949 | 1.66k | if (remainingSize) return ERROR(srcSize_wrong); |
1950 | 1.66k | break; |
1951 | 1.66k | default: |
1952 | 0 | return ERROR(GENERIC); |
1953 | 10.3k | } |
1954 | 10.3k | if (blockSize == 0) break; /* bt_end */ |
1955 | | |
1956 | 7.77k | if (ZSTDv01_isError(errorCode)) return errorCode; |
1957 | 5.62k | op += errorCode; |
1958 | 5.62k | ip += blockSize; |
1959 | 5.62k | remainingSize -= blockSize; |
1960 | 5.62k | } |
1961 | | |
1962 | 2.54k | return op-ostart; |
1963 | 4.70k | } |
1964 | | |
1965 | | size_t ZSTDv01_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
1966 | 4.70k | { |
1967 | 4.70k | dctx_t ctx; |
1968 | 4.70k | ctx.base = dst; |
1969 | 4.70k | return ZSTDv01_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize); |
1970 | 4.70k | } |
1971 | | |
1972 | | /* ZSTD_errorFrameSizeInfoLegacy() : |
1973 | | assumes `cSize` and `dBound` are _not_ NULL */ |
1974 | | static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret) |
1975 | 315 | { |
1976 | 315 | *cSize = ret; |
1977 | 315 | *dBound = ZSTD_CONTENTSIZE_ERROR; |
1978 | 315 | } |
1979 | | |
1980 | | void ZSTDv01_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound) |
1981 | 6.15k | { |
1982 | 6.15k | const BYTE* ip = (const BYTE*)src; |
1983 | 6.15k | size_t remainingSize = srcSize; |
1984 | 6.15k | size_t nbBlocks = 0; |
1985 | 6.15k | U32 magicNumber; |
1986 | 6.15k | blockProperties_t blockProperties; |
1987 | | |
1988 | | /* Frame Header */ |
1989 | 6.15k | if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) { |
1990 | 31 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong)); |
1991 | 31 | return; |
1992 | 31 | } |
1993 | 6.12k | magicNumber = ZSTD_readBE32(src); |
1994 | 6.12k | if (magicNumber != ZSTD_magicNumber) { |
1995 | 0 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown)); |
1996 | 0 | return; |
1997 | 0 | } |
1998 | 6.12k | ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize; |
1999 | | |
2000 | | /* Loop on each block */ |
2001 | 21.3k | while (1) |
2002 | 21.3k | { |
2003 | 21.3k | size_t blockSize = ZSTDv01_getcBlockSize(ip, remainingSize, &blockProperties); |
2004 | 21.3k | if (ZSTDv01_isError(blockSize)) { |
2005 | 77 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, blockSize); |
2006 | 77 | return; |
2007 | 77 | } |
2008 | | |
2009 | 21.2k | ip += ZSTD_blockHeaderSize; |
2010 | 21.2k | remainingSize -= ZSTD_blockHeaderSize; |
2011 | 21.2k | if (blockSize > remainingSize) { |
2012 | 207 | ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong)); |
2013 | 207 | return; |
2014 | 207 | } |
2015 | | |
2016 | 21.0k | if (blockSize == 0) break; /* bt_end */ |
2017 | | |
2018 | 15.1k | ip += blockSize; |
2019 | 15.1k | remainingSize -= blockSize; |
2020 | 15.1k | nbBlocks++; |
2021 | 15.1k | } |
2022 | | |
2023 | 5.83k | *cSize = ip - (const BYTE*)src; |
2024 | 5.83k | *dBound = nbBlocks * BLOCKSIZE; |
2025 | 5.83k | } |
2026 | | |
2027 | | /******************************* |
2028 | | * Streaming Decompression API |
2029 | | *******************************/ |
2030 | | |
2031 | | size_t ZSTDv01_resetDCtx(ZSTDv01_Dctx* dctx) |
2032 | 0 | { |
2033 | 0 | dctx->expected = ZSTD_frameHeaderSize; |
2034 | 0 | dctx->phase = 0; |
2035 | 0 | dctx->previousDstEnd = NULL; |
2036 | 0 | dctx->base = NULL; |
2037 | 0 | return 0; |
2038 | 0 | } |
2039 | | |
2040 | | ZSTDv01_Dctx* ZSTDv01_createDCtx(void) |
2041 | 0 | { |
2042 | 0 | ZSTDv01_Dctx* dctx = (ZSTDv01_Dctx*)malloc(sizeof(ZSTDv01_Dctx)); |
2043 | 0 | if (dctx==NULL) return NULL; |
2044 | 0 | ZSTDv01_resetDCtx(dctx); |
2045 | 0 | return dctx; |
2046 | 0 | } |
2047 | | |
2048 | | size_t ZSTDv01_freeDCtx(ZSTDv01_Dctx* dctx) |
2049 | 0 | { |
2050 | 0 | free(dctx); |
2051 | 0 | return 0; |
2052 | 0 | } |
2053 | | |
2054 | | size_t ZSTDv01_nextSrcSizeToDecompress(ZSTDv01_Dctx* dctx) |
2055 | 0 | { |
2056 | 0 | return ((dctx_t*)dctx)->expected; |
2057 | 0 | } |
2058 | | |
2059 | | size_t ZSTDv01_decompressContinue(ZSTDv01_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
2060 | 0 | { |
2061 | 0 | dctx_t* ctx = (dctx_t*)dctx; |
2062 | | |
2063 | | /* Sanity check */ |
2064 | 0 | if (srcSize != ctx->expected) return ERROR(srcSize_wrong); |
2065 | 0 | if (dst != ctx->previousDstEnd) /* not contiguous */ |
2066 | 0 | ctx->base = dst; |
2067 | | |
2068 | | /* Decompress : frame header */ |
2069 | 0 | if (ctx->phase == 0) |
2070 | 0 | { |
2071 | | /* Check frame magic header */ |
2072 | 0 | U32 magicNumber = ZSTD_readBE32(src); |
2073 | 0 | if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown); |
2074 | 0 | ctx->phase = 1; |
2075 | 0 | ctx->expected = ZSTD_blockHeaderSize; |
2076 | 0 | return 0; |
2077 | 0 | } |
2078 | | |
2079 | | /* Decompress : block header */ |
2080 | 0 | if (ctx->phase == 1) |
2081 | 0 | { |
2082 | 0 | blockProperties_t bp; |
2083 | 0 | size_t blockSize = ZSTDv01_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); |
2084 | 0 | if (ZSTDv01_isError(blockSize)) return blockSize; |
2085 | 0 | if (bp.blockType == bt_end) |
2086 | 0 | { |
2087 | 0 | ctx->expected = 0; |
2088 | 0 | ctx->phase = 0; |
2089 | 0 | } |
2090 | 0 | else |
2091 | 0 | { |
2092 | 0 | ctx->expected = blockSize; |
2093 | 0 | ctx->bType = bp.blockType; |
2094 | 0 | ctx->phase = 2; |
2095 | 0 | } |
2096 | |
|
2097 | 0 | return 0; |
2098 | 0 | } |
2099 | | |
2100 | | /* Decompress : block content */ |
2101 | 0 | { |
2102 | 0 | size_t rSize; |
2103 | 0 | switch(ctx->bType) |
2104 | 0 | { |
2105 | 0 | case bt_compressed: |
2106 | 0 | rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize); |
2107 | 0 | break; |
2108 | 0 | case bt_raw : |
2109 | 0 | rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize); |
2110 | 0 | break; |
2111 | 0 | case bt_rle : |
2112 | 0 | return ERROR(GENERIC); /* not yet handled */ |
2113 | 0 | break; |
2114 | 0 | case bt_end : /* should never happen (filtered at phase 1) */ |
2115 | 0 | rSize = 0; |
2116 | 0 | break; |
2117 | 0 | default: |
2118 | 0 | return ERROR(GENERIC); |
2119 | 0 | } |
2120 | 0 | ctx->phase = 1; |
2121 | 0 | ctx->expected = ZSTD_blockHeaderSize; |
2122 | 0 | if (ZSTDv01_isError(rSize)) return rSize; |
2123 | 0 | ctx->previousDstEnd = (void*)( ((char*)dst) + rSize); |
2124 | 0 | return rSize; |
2125 | 0 | } |
2126 | |
|
2127 | 0 | } |