blob: 1d4abdf202b3fc9f340fdebd0a86cce0b68c2d7e [file] [log] [blame]
Yann Colletaa074052015-10-30 11:21:50 +01001/* ******************************************************************
2 Error codes and messages
3 Copyright (C) 2013-2015, Yann Collet
4
5 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following disclaimer
15 in the documentation and/or other materials provided with the
16 distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 You can contact the author at :
31 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
32 - Public forum : https://groups.google.com/forum/#!forum/lz4c
33****************************************************************** */
34#ifndef ERROR_H_MODULE
35#define ERROR_H_MODULE
36
37#if defined (__cplusplus)
38extern "C" {
39#endif
40
41#include <stddef.h> /* size_t, ptrdiff_t */
42#include "zstd_v02.h"
43
44/******************************************
45* Compiler-specific
46******************************************/
inikep48849f82016-08-10 14:26:35 +020047#if defined(_MSC_VER) /* Visual Studio */
48# include <stdlib.h> /* _byteswap_ulong */
49# include <intrin.h> /* _byteswap_* */
50#endif
Yann Colletaa074052015-10-30 11:21:50 +010051#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
52# define ERR_STATIC static inline
53#elif defined(_MSC_VER)
54# define ERR_STATIC static __inline
55#elif defined(__GNUC__)
56# define ERR_STATIC static __attribute__((unused))
57#else
58# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
59#endif
60
61
62/******************************************
63* Error Management
64******************************************/
65#define PREFIX(name) ZSTD_error_##name
66
67#define ERROR(name) (size_t)-PREFIX(name)
68
69#define ERROR_LIST(ITEM) \
70 ITEM(PREFIX(No_Error)) ITEM(PREFIX(GENERIC)) \
71 ITEM(PREFIX(memory_allocation)) \
72 ITEM(PREFIX(dstSize_tooSmall)) ITEM(PREFIX(srcSize_wrong)) \
73 ITEM(PREFIX(prefix_unknown)) ITEM(PREFIX(corruption_detected)) \
74 ITEM(PREFIX(tableLog_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooSmall)) \
75 ITEM(PREFIX(maxCode))
76
77#define ERROR_GENERATE_ENUM(ENUM) ENUM,
78typedef enum { ERROR_LIST(ERROR_GENERATE_ENUM) } ERR_codes; /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */
79
80#define ERROR_CONVERTTOSTRING(STRING) #STRING,
81#define ERROR_GENERATE_STRING(EXPR) ERROR_CONVERTTOSTRING(EXPR)
82
83ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
84
85
86#if defined (__cplusplus)
87}
88#endif
89
90#endif /* ERROR_H_MODULE */
91
92
93/* ******************************************************************
94 mem.h
95 low-level memory access routines
96 Copyright (C) 2013-2015, Yann Collet.
97
98 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
99
100 Redistribution and use in source and binary forms, with or without
101 modification, are permitted provided that the following conditions are
102 met:
103
104 * Redistributions of source code must retain the above copyright
105 notice, this list of conditions and the following disclaimer.
106 * Redistributions in binary form must reproduce the above
107 copyright notice, this list of conditions and the following disclaimer
108 in the documentation and/or other materials provided with the
109 distribution.
110
111 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
112 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
113 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
114 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
115 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
116 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
117 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
118 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
119 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
120 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
121 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122
123 You can contact the author at :
124 - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
125 - Public forum : https://groups.google.com/forum/#!forum/lz4c
126****************************************************************** */
127#ifndef MEM_H_MODULE
128#define MEM_H_MODULE
129
130#if defined (__cplusplus)
131extern "C" {
132#endif
133
134/******************************************
135* Includes
136******************************************/
137#include <stddef.h> /* size_t, ptrdiff_t */
138#include <string.h> /* memcpy */
139
140
141/******************************************
142* Compiler-specific
143******************************************/
144#if defined(__GNUC__)
145# define MEM_STATIC static __attribute__((unused))
146#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
147# define MEM_STATIC static inline
148#elif defined(_MSC_VER)
149# define MEM_STATIC static __inline
150#else
151# define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
152#endif
153
154
155/****************************************************************
156* Basic Types
157*****************************************************************/
158#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
159# include <stdint.h>
160 typedef uint8_t BYTE;
161 typedef uint16_t U16;
162 typedef int16_t S16;
163 typedef uint32_t U32;
164 typedef int32_t S32;
165 typedef uint64_t U64;
166 typedef int64_t S64;
167#else
168 typedef unsigned char BYTE;
169 typedef unsigned short U16;
170 typedef signed short S16;
171 typedef unsigned int U32;
172 typedef signed int S32;
173 typedef unsigned long long U64;
174 typedef signed long long S64;
175#endif
176
177
178/****************************************************************
179* Memory I/O
180*****************************************************************/
181/* MEM_FORCE_MEMORY_ACCESS
182 * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
183 * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
184 * The below switch allow to select different access method for improved performance.
185 * Method 0 (default) : use `memcpy()`. Safe and portable.
186 * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
187 * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
188 * Method 2 : direct access. This method is portable but violate C standard.
189 * It can generate buggy code on targets generating assembly depending on alignment.
190 * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
191 * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
192 * Prefer these methods in priority order (0 > 1 > 2)
193 */
194#ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
195# if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
196# define MEM_FORCE_MEMORY_ACCESS 2
inikep48849f82016-08-10 14:26:35 +0200197# elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
Yann Colletaa074052015-10-30 11:21:50 +0100198 (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
199# define MEM_FORCE_MEMORY_ACCESS 1
200# endif
201#endif
202
203MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
204MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
205
206MEM_STATIC unsigned MEM_isLittleEndian(void)
207{
208 const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
209 return one.c[0];
210}
211
212#if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
213
214/* violates C standard on structure alignment.
215Only use if no other choice to achieve best performance on target platform */
216MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
217MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
218MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
219
220MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
Yann Colletaa074052015-10-30 11:21:50 +0100221
222#elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
223
224/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
225/* currently only defined for gcc and icc */
226typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;
227
228MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
229MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
230MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
231
232MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
Yann Colletaa074052015-10-30 11:21:50 +0100233
234#else
235
236/* default method, safe and standard.
237 can sometimes prove slower */
238
239MEM_STATIC U16 MEM_read16(const void* memPtr)
240{
241 U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
242}
243
244MEM_STATIC U32 MEM_read32(const void* memPtr)
245{
246 U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
247}
248
249MEM_STATIC U64 MEM_read64(const void* memPtr)
250{
251 U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
252}
253
254MEM_STATIC void MEM_write16(void* memPtr, U16 value)
255{
256 memcpy(memPtr, &value, sizeof(value));
257}
258
Yann Colletaa074052015-10-30 11:21:50 +0100259#endif // MEM_FORCE_MEMORY_ACCESS
260
261
262MEM_STATIC U16 MEM_readLE16(const void* memPtr)
263{
264 if (MEM_isLittleEndian())
265 return MEM_read16(memPtr);
266 else
267 {
268 const BYTE* p = (const BYTE*)memPtr;
269 return (U16)(p[0] + (p[1]<<8));
270 }
271}
272
273MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
274{
275 if (MEM_isLittleEndian())
276 {
277 MEM_write16(memPtr, val);
278 }
279 else
280 {
281 BYTE* p = (BYTE*)memPtr;
282 p[0] = (BYTE)val;
283 p[1] = (BYTE)(val>>8);
284 }
285}
286
287MEM_STATIC U32 MEM_readLE32(const void* memPtr)
288{
289 if (MEM_isLittleEndian())
290 return MEM_read32(memPtr);
291 else
292 {
293 const BYTE* p = (const BYTE*)memPtr;
294 return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
295 }
296}
297
Yann Colletaa074052015-10-30 11:21:50 +0100298
299MEM_STATIC U64 MEM_readLE64(const void* memPtr)
300{
301 if (MEM_isLittleEndian())
302 return MEM_read64(memPtr);
303 else
304 {
305 const BYTE* p = (const BYTE*)memPtr;
306 return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)
307 + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));
308 }
309}
310
Yann Colletaa074052015-10-30 11:21:50 +0100311
312MEM_STATIC size_t MEM_readLEST(const void* memPtr)
313{
314 if (MEM_32bits())
315 return (size_t)MEM_readLE32(memPtr);
316 else
317 return (size_t)MEM_readLE64(memPtr);
318}
319
Yann Colletaa074052015-10-30 11:21:50 +0100320#if defined (__cplusplus)
321}
322#endif
323
324#endif /* MEM_H_MODULE */
325
326
327/* ******************************************************************
328 bitstream
329 Part of NewGen Entropy library
330 header file (to include)
331 Copyright (C) 2013-2015, Yann Collet.
332
333 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
334
335 Redistribution and use in source and binary forms, with or without
336 modification, are permitted provided that the following conditions are
337 met:
338
339 * Redistributions of source code must retain the above copyright
340 notice, this list of conditions and the following disclaimer.
341 * Redistributions in binary form must reproduce the above
342 copyright notice, this list of conditions and the following disclaimer
343 in the documentation and/or other materials provided with the
344 distribution.
345
346 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
347 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
348 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
349 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
350 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
351 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
353 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
354 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
355 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
356 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
357
358 You can contact the author at :
359 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
360 - Public forum : https://groups.google.com/forum/#!forum/lz4c
361****************************************************************** */
362#ifndef BITSTREAM_H_MODULE
363#define BITSTREAM_H_MODULE
364
365#if defined (__cplusplus)
366extern "C" {
367#endif
368
369
370/*
371* This API consists of small unitary functions, which highly benefit from being inlined.
372* Since link-time-optimization is not available for all compilers,
373* these functions are defined into a .h to be included.
374*/
375
376
377/**********************************************
378* bitStream decompression API (read backward)
379**********************************************/
380typedef struct
381{
382 size_t bitContainer;
383 unsigned bitsConsumed;
384 const char* ptr;
385 const char* start;
386} BIT_DStream_t;
387
388typedef enum { BIT_DStream_unfinished = 0,
389 BIT_DStream_endOfBuffer = 1,
390 BIT_DStream_completed = 2,
391 BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
392 /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
393
394MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
395MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
396MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
397MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
398
399
400/*
401* Start by invoking BIT_initDStream().
402* A chunk of the bitStream is then stored into a local register.
403* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
404* You can then retrieve bitFields stored into the local register, **in reverse order**.
405* Local register is manually filled from memory by the BIT_reloadDStream() method.
406* A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished.
407* Otherwise, it can be less than that, so proceed accordingly.
408* Checking if DStream has reached its end can be performed with BIT_endOfDStream()
409*/
410
411
412/******************************************
413* unsafe API
414******************************************/
415MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
416/* faster, but works only if nbBits >= 1 */
417
418
419
420/****************************************************************
421* Helper functions
422****************************************************************/
423MEM_STATIC unsigned BIT_highbit32 (register U32 val)
424{
425# if defined(_MSC_VER) /* Visual */
Yann Collet5054ee02015-11-23 13:34:21 +0100426 unsigned long r=0;
Yann Colletaa074052015-10-30 11:21:50 +0100427 _BitScanReverse ( &r, val );
428 return (unsigned) r;
429# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
430 return 31 - __builtin_clz (val);
431# else /* Software version */
432 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 };
433 U32 v = val;
434 unsigned r;
435 v |= v >> 1;
436 v |= v >> 2;
437 v |= v >> 4;
438 v |= v >> 8;
439 v |= v >> 16;
440 r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
441 return r;
442# endif
443}
444
445
446
447/**********************************************************
448* bitStream decoding
449**********************************************************/
450
451/*!BIT_initDStream
452* Initialize a BIT_DStream_t.
453* @bitD : a pointer to an already allocated BIT_DStream_t structure
454* @srcBuffer must point at the beginning of a bitStream
455* @srcSize must be the exact size of the bitStream
456* @result : size of stream (== srcSize) or an errorCode if a problem is detected
457*/
458MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
459{
460 if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
461
462 if (srcSize >= sizeof(size_t)) /* normal case */
463 {
464 U32 contain32;
465 bitD->start = (const char*)srcBuffer;
466 bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
467 bitD->bitContainer = MEM_readLEST(bitD->ptr);
468 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
469 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
470 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
471 }
472 else
473 {
474 U32 contain32;
475 bitD->start = (const char*)srcBuffer;
476 bitD->ptr = bitD->start;
477 bitD->bitContainer = *(const BYTE*)(bitD->start);
478 switch(srcSize)
479 {
480 case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
481 case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
482 case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
483 case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
484 case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
485 case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
486 default:;
487 }
488 contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
489 if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
490 bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
491 bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
492 }
493
494 return srcSize;
495}
496
497/*!BIT_lookBits
498 * Provides next n bits from local register
499 * local register is not modified (bits are still present for next read/look)
500 * On 32-bits, maxNbBits==25
501 * On 64-bits, maxNbBits==57
502 * @return : value extracted
503 */
504MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
505{
506 const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
507 return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
508}
509
510/*! BIT_lookBitsFast :
511* unsafe version; only works only if nbBits >= 1 */
512MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
513{
514 const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
515 return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
516}
517
518MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
519{
520 bitD->bitsConsumed += nbBits;
521}
522
523/*!BIT_readBits
524 * Read next n bits from local register.
525 * pay attention to not read more than nbBits contained into local register.
526 * @return : extracted value.
527 */
528MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
529{
530 size_t value = BIT_lookBits(bitD, nbBits);
531 BIT_skipBits(bitD, nbBits);
532 return value;
533}
534
535/*!BIT_readBitsFast :
536* unsafe version; only works only if nbBits >= 1 */
537MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
538{
539 size_t value = BIT_lookBitsFast(bitD, nbBits);
540 BIT_skipBits(bitD, nbBits);
541 return value;
542}
543
544MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
545{
546 if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
547 return BIT_DStream_overflow;
548
549 if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))
550 {
551 bitD->ptr -= bitD->bitsConsumed >> 3;
552 bitD->bitsConsumed &= 7;
553 bitD->bitContainer = MEM_readLEST(bitD->ptr);
554 return BIT_DStream_unfinished;
555 }
556 if (bitD->ptr == bitD->start)
557 {
558 if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
559 return BIT_DStream_completed;
560 }
561 {
562 U32 nbBytes = bitD->bitsConsumed >> 3;
563 BIT_DStream_status result = BIT_DStream_unfinished;
564 if (bitD->ptr - nbBytes < bitD->start)
565 {
566 nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
567 result = BIT_DStream_endOfBuffer;
568 }
569 bitD->ptr -= nbBytes;
570 bitD->bitsConsumed -= nbBytes*8;
571 bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
572 return result;
573 }
574}
575
576/*! BIT_endOfDStream
577* @return Tells if DStream has reached its exact end
578*/
579MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
580{
581 return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
582}
583
584#if defined (__cplusplus)
585}
586#endif
587
588#endif /* BITSTREAM_H_MODULE */
589/* ******************************************************************
590 Error codes and messages
591 Copyright (C) 2013-2015, Yann Collet
592
593 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
594
595 Redistribution and use in source and binary forms, with or without
596 modification, are permitted provided that the following conditions are
597 met:
598
599 * Redistributions of source code must retain the above copyright
600 notice, this list of conditions and the following disclaimer.
601 * Redistributions in binary form must reproduce the above
602 copyright notice, this list of conditions and the following disclaimer
603 in the documentation and/or other materials provided with the
604 distribution.
605
606 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
607 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
608 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
609 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
610 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
611 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
612 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
613 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
614 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
615 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
616 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
617
618 You can contact the author at :
619 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
620 - Public forum : https://groups.google.com/forum/#!forum/lz4c
621****************************************************************** */
622#ifndef ERROR_H_MODULE
623#define ERROR_H_MODULE
624
625#if defined (__cplusplus)
626extern "C" {
627#endif
628
629
630/******************************************
631* Compiler-specific
632******************************************/
633#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
634# define ERR_STATIC static inline
635#elif defined(_MSC_VER)
636# define ERR_STATIC static __inline
637#elif defined(__GNUC__)
638# define ERR_STATIC static __attribute__((unused))
639#else
640# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
641#endif
642
643
644/******************************************
645* Error Management
646******************************************/
647#define PREFIX(name) ZSTD_error_##name
648
649#define ERROR(name) (size_t)-PREFIX(name)
650
651#define ERROR_LIST(ITEM) \
652 ITEM(PREFIX(No_Error)) ITEM(PREFIX(GENERIC)) \
653 ITEM(PREFIX(dstSize_tooSmall)) ITEM(PREFIX(srcSize_wrong)) \
654 ITEM(PREFIX(prefix_unknown)) ITEM(PREFIX(corruption_detected)) \
655 ITEM(PREFIX(tableLog_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooLarge)) ITEM(PREFIX(maxSymbolValue_tooSmall)) \
656 ITEM(PREFIX(maxCode))
657
658#define ERROR_GENERATE_ENUM(ENUM) ENUM,
659typedef enum { ERROR_LIST(ERROR_GENERATE_ENUM) } ERR_codes; /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */
660
661#define ERROR_CONVERTTOSTRING(STRING) #STRING,
662#define ERROR_GENERATE_STRING(EXPR) ERROR_CONVERTTOSTRING(EXPR)
663static const char* ERR_strings[] = { ERROR_LIST(ERROR_GENERATE_STRING) };
664
665ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
666
667ERR_STATIC const char* ERR_getErrorName(size_t code)
668{
669 static const char* codeError = "Unspecified error code";
670 if (ERR_isError(code)) return ERR_strings[-(int)(code)];
671 return codeError;
672}
673
674
675#if defined (__cplusplus)
676}
677#endif
678
679#endif /* ERROR_H_MODULE */
680/*
681Constructor and Destructor of type FSE_CTable
682 Note that its size depends on 'tableLog' and 'maxSymbolValue' */
683typedef unsigned FSE_CTable; /* don't allocate that. It's just a way to be more restrictive than void* */
684typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
685
686
687/* ******************************************************************
688 FSE : Finite State Entropy coder
689 header file for static linking (only)
690 Copyright (C) 2013-2015, Yann Collet
691
692 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
693
694 Redistribution and use in source and binary forms, with or without
695 modification, are permitted provided that the following conditions are
696 met:
697
698 * Redistributions of source code must retain the above copyright
699 notice, this list of conditions and the following disclaimer.
700 * Redistributions in binary form must reproduce the above
701 copyright notice, this list of conditions and the following disclaimer
702 in the documentation and/or other materials provided with the
703 distribution.
704
705 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
706 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
707 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
708 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
709 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
710 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
711 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
712 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
713 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
714 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
715 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
716
717 You can contact the author at :
718 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
719 - Public forum : https://groups.google.com/forum/#!forum/lz4c
720****************************************************************** */
721#if defined (__cplusplus)
722extern "C" {
723#endif
724
725
726/******************************************
727* Static allocation
728******************************************/
729/* FSE buffer bounds */
730#define FSE_NCOUNTBOUND 512
731#define FSE_BLOCKBOUND(size) (size + (size>>7))
732#define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
733
734/* You can statically allocate FSE CTable/DTable as a table of unsigned using below macro */
735#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
736#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
737
738
739/******************************************
740* FSE advanced API
741******************************************/
742static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
743/* build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
744
745static size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
746/* build a fake FSE_DTable, designed to always generate the same symbolValue */
747
748
749/******************************************
750* FSE symbol decompression API
751******************************************/
752typedef struct
753{
754 size_t state;
755 const void* table; /* precise table may vary, depending on U16 */
756} FSE_DState_t;
757
758
759static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt);
760
761static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
762
763static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
764
765/*
766Let's now decompose FSE_decompress_usingDTable() into its unitary components.
767You will decode FSE-encoded symbols from the bitStream,
768and also any other bitFields you put in, **in reverse order**.
769
770You will need a few variables to track your bitStream. They are :
771
772BIT_DStream_t DStream; // Stream context
773FSE_DState_t DState; // State context. Multiple ones are possible
774FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable()
775
776The first thing to do is to init the bitStream.
777 errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);
778
779You should then retrieve your initial state(s)
780(in reverse flushing order if you have several ones) :
781 errorCode = FSE_initDState(&DState, &DStream, DTablePtr);
782
783You can then decode your data, symbol after symbol.
784For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.
785Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
786 unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);
787
788You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
789Note : maximum allowed nbBits is 25, for 32-bits compatibility
790 size_t bitField = BIT_readBits(&DStream, nbBits);
791
792All above operations only read from local register (which size depends on size_t).
793Refueling the register from memory is manually performed by the reload method.
794 endSignal = FSE_reloadDStream(&DStream);
795
796BIT_reloadDStream() result tells if there is still some more data to read from DStream.
797BIT_DStream_unfinished : there is still some data left into the DStream.
798BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
799BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
800BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
801
802When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
803to properly detect the exact end of stream.
804After each decoded symbol, check if DStream is fully consumed using this simple test :
805 BIT_reloadDStream(&DStream) >= BIT_DStream_completed
806
807When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
808Checking if DStream has reached its end is performed by :
809 BIT_endOfDStream(&DStream);
810Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
811 FSE_endOfDState(&DState);
812*/
813
814
815/******************************************
816* FSE unsafe API
817******************************************/
818static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);
819/* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
820
821
822/******************************************
823* Implementation of inline functions
824******************************************/
825
826/* decompression */
827
828typedef struct {
829 U16 tableLog;
830 U16 fastMode;
831} FSE_DTableHeader; /* sizeof U32 */
832
833typedef struct
834{
835 unsigned short newState;
836 unsigned char symbol;
837 unsigned char nbBits;
838} FSE_decode_t; /* size == U32 */
839
840MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt)
841{
Yann Collet494c7862016-01-06 12:54:02 +0100842 FSE_DTableHeader DTableH;
843 memcpy(&DTableH, dt, sizeof(DTableH));
844 DStatePtr->state = BIT_readBits(bitD, DTableH.tableLog);
Yann Colletaa074052015-10-30 11:21:50 +0100845 BIT_reloadDStream(bitD);
846 DStatePtr->table = dt + 1;
847}
848
849MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
850{
851 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
852 const U32 nbBits = DInfo.nbBits;
853 BYTE symbol = DInfo.symbol;
854 size_t lowBits = BIT_readBits(bitD, nbBits);
855
856 DStatePtr->state = DInfo.newState + lowBits;
857 return symbol;
858}
859
860MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
861{
862 const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
863 const U32 nbBits = DInfo.nbBits;
864 BYTE symbol = DInfo.symbol;
865 size_t lowBits = BIT_readBitsFast(bitD, nbBits);
866
867 DStatePtr->state = DInfo.newState + lowBits;
868 return symbol;
869}
870
871MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
872{
873 return DStatePtr->state == 0;
874}
875
876
877#if defined (__cplusplus)
878}
879#endif
880/* ******************************************************************
881 Huff0 : Huffman coder, part of New Generation Entropy library
882 header file for static linking (only)
883 Copyright (C) 2013-2015, Yann Collet
884
885 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
886
887 Redistribution and use in source and binary forms, with or without
888 modification, are permitted provided that the following conditions are
889 met:
890
891 * Redistributions of source code must retain the above copyright
892 notice, this list of conditions and the following disclaimer.
893 * Redistributions in binary form must reproduce the above
894 copyright notice, this list of conditions and the following disclaimer
895 in the documentation and/or other materials provided with the
896 distribution.
897
898 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
899 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
900 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
901 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
902 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
903 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
904 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
905 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
906 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
907 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
908 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
909
910 You can contact the author at :
911 - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
912 - Public forum : https://groups.google.com/forum/#!forum/lz4c
913****************************************************************** */
914
915#if defined (__cplusplus)
916extern "C" {
917#endif
918
919/******************************************
920* Static allocation macros
921******************************************/
922/* Huff0 buffer bounds */
923#define HUF_CTABLEBOUND 129
924#define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true if incompressible pre-filtered with fast heuristic */
925#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
926
927/* static allocation of Huff0's DTable */
928#define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<maxTableLog)) /* nb Cells; use unsigned short for X2, unsigned int for X4 */
929#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
930 unsigned short DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
931#define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
932 unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
933#define HUF_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \
934 unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog }
935
936
937/******************************************
938* Advanced functions
939******************************************/
940static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
941static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbols decoder */
942static size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* quad-symbols decoder */
943
944
945#if defined (__cplusplus)
946}
947#endif
948
949/*
950 zstd - standard compression library
951 Header File
952 Copyright (C) 2014-2015, Yann Collet.
953
954 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
955
956 Redistribution and use in source and binary forms, with or without
957 modification, are permitted provided that the following conditions are
958 met:
959 * Redistributions of source code must retain the above copyright
960 notice, this list of conditions and the following disclaimer.
961 * Redistributions in binary form must reproduce the above
962 copyright notice, this list of conditions and the following disclaimer
963 in the documentation and/or other materials provided with the
964 distribution.
965 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
966 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
967 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
968 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
969 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
971 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
972 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
973 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
974 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
975 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
976
977 You can contact the author at :
978 - zstd source repository : https://github.com/Cyan4973/zstd
979 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
980*/
981
982#if defined (__cplusplus)
983extern "C" {
984#endif
985
986/* *************************************
987* Includes
988***************************************/
989#include <stddef.h> /* size_t */
990
991
992/* *************************************
993* Version
994***************************************/
995#define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */
996#define ZSTD_VERSION_MINOR 2 /* for new (non-breaking) interface capabilities */
997#define ZSTD_VERSION_RELEASE 2 /* for tweaks, bug-fixes, or development */
998#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
999
1000
1001/* *************************************
1002* Advanced functions
1003***************************************/
1004typedef struct ZSTD_CCtx_s ZSTD_CCtx; /* incomplete type */
1005
1006#if defined (__cplusplus)
1007}
1008#endif
1009/*
1010 zstd - standard compression library
1011 Header File for static linking only
1012 Copyright (C) 2014-2015, Yann Collet.
1013
1014 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1015
1016 Redistribution and use in source and binary forms, with or without
1017 modification, are permitted provided that the following conditions are
1018 met:
1019 * Redistributions of source code must retain the above copyright
1020 notice, this list of conditions and the following disclaimer.
1021 * Redistributions in binary form must reproduce the above
1022 copyright notice, this list of conditions and the following disclaimer
1023 in the documentation and/or other materials provided with the
1024 distribution.
1025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1026 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1027 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1028 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1029 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1030 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1031 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1032 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1033 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1035 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1036
1037 You can contact the author at :
1038 - zstd source repository : https://github.com/Cyan4973/zstd
1039 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
1040*/
1041
1042/* The objects defined into this file should be considered experimental.
1043 * They are not labelled stable, as their prototype may change in the future.
1044 * You can use them for tests, provide feedback, or if you can endure risk of future changes.
1045 */
1046
1047#if defined (__cplusplus)
1048extern "C" {
1049#endif
1050
1051/* *************************************
1052* Streaming functions
1053***************************************/
1054
1055typedef struct ZSTD_DCtx_s ZSTD_DCtx;
1056
1057/*
1058 Use above functions alternatively.
1059 ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
1060 ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.
1061 Result is the number of bytes regenerated within 'dst'.
1062 It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
1063*/
1064
1065/* *************************************
1066* Prefix - version detection
1067***************************************/
1068#define ZSTD_magicNumber 0xFD2FB522 /* v0.2 (current)*/
1069
1070
1071#if defined (__cplusplus)
1072}
1073#endif
1074/* ******************************************************************
1075 FSE : Finite State Entropy coder
1076 Copyright (C) 2013-2015, Yann Collet.
1077
1078 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1079
1080 Redistribution and use in source and binary forms, with or without
1081 modification, are permitted provided that the following conditions are
1082 met:
1083
1084 * Redistributions of source code must retain the above copyright
1085 notice, this list of conditions and the following disclaimer.
1086 * Redistributions in binary form must reproduce the above
1087 copyright notice, this list of conditions and the following disclaimer
1088 in the documentation and/or other materials provided with the
1089 distribution.
1090
1091 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1092 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1093 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1094 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1095 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1096 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1097 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1098 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1099 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1100 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1101 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1102
1103 You can contact the author at :
1104 - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
1105 - Public forum : https://groups.google.com/forum/#!forum/lz4c
1106****************************************************************** */
1107
1108#ifndef FSE_COMMONDEFS_ONLY
1109
1110/****************************************************************
1111* Tuning parameters
1112****************************************************************/
1113/* MEMORY_USAGE :
1114* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
1115* Increasing memory usage improves compression ratio
1116* Reduced memory usage can improve speed, due to cache effect
1117* Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
1118#define FSE_MAX_MEMORY_USAGE 14
1119#define FSE_DEFAULT_MEMORY_USAGE 13
1120
1121/* FSE_MAX_SYMBOL_VALUE :
1122* Maximum symbol value authorized.
1123* Required for proper stack allocation */
1124#define FSE_MAX_SYMBOL_VALUE 255
1125
1126
1127/****************************************************************
1128* template functions type & suffix
1129****************************************************************/
1130#define FSE_FUNCTION_TYPE BYTE
1131#define FSE_FUNCTION_EXTENSION
1132
1133
1134/****************************************************************
1135* Byte symbol type
1136****************************************************************/
1137#endif /* !FSE_COMMONDEFS_ONLY */
1138
1139
1140/****************************************************************
1141* Compiler specifics
1142****************************************************************/
1143#ifdef _MSC_VER /* Visual Studio */
1144# define FORCE_INLINE static __forceinline
1145# include <intrin.h> /* For Visual 2005 */
1146# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1147# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1148#else
1149# ifdef __GNUC__
1150# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
1151# define FORCE_INLINE static inline __attribute__((always_inline))
1152# else
1153# define FORCE_INLINE static inline
1154# endif
1155#endif
1156
1157
1158/****************************************************************
1159* Includes
1160****************************************************************/
1161#include <stdlib.h> /* malloc, free, qsort */
1162#include <string.h> /* memcpy, memset */
1163#include <stdio.h> /* printf (debug) */
1164
1165/****************************************************************
1166* Constants
1167*****************************************************************/
1168#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)
1169#define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)
1170#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)
1171#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)
1172#define FSE_MIN_TABLELOG 5
1173
1174#define FSE_TABLELOG_ABSOLUTE_MAX 15
1175#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX
1176#error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"
1177#endif
1178
1179
1180/****************************************************************
1181* Error Management
1182****************************************************************/
1183#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1184
1185
1186/****************************************************************
1187* Complex types
1188****************************************************************/
1189typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
1190
1191
1192/****************************************************************
1193* Templates
1194****************************************************************/
1195/*
1196 designed to be included
1197 for type-specific functions (template emulation in C)
1198 Objective is to write these functions only once, for improved maintenance
1199*/
1200
1201/* safety checks */
1202#ifndef FSE_FUNCTION_EXTENSION
1203# error "FSE_FUNCTION_EXTENSION must be defined"
1204#endif
1205#ifndef FSE_FUNCTION_TYPE
1206# error "FSE_FUNCTION_TYPE must be defined"
1207#endif
1208
1209/* Function names */
1210#define FSE_CAT(X,Y) X##Y
1211#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
1212#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
1213
1214
1215/* Function templates */
1216
Yann Colletddb8ebd2016-05-05 04:59:53 +02001217#define FSE_DECODE_TYPE FSE_decode_t
Yann Colletaa074052015-10-30 11:21:50 +01001218
1219static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }
1220
Yann Colletddb8ebd2016-05-05 04:59:53 +02001221static size_t FSE_buildDTable
Yann Colletaa074052015-10-30 11:21:50 +01001222(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1223{
Yann Collet494c7862016-01-06 12:54:02 +01001224 void* ptr = dt+1;
1225 FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)ptr;
1226 FSE_DTableHeader DTableH;
Yann Colletaa074052015-10-30 11:21:50 +01001227 const U32 tableSize = 1 << tableLog;
1228 const U32 tableMask = tableSize-1;
1229 const U32 step = FSE_tableStep(tableSize);
1230 U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
1231 U32 position = 0;
1232 U32 highThreshold = tableSize-1;
1233 const S16 largeLimit= (S16)(1 << (tableLog-1));
1234 U32 noLarge = 1;
1235 U32 s;
1236
1237 /* Sanity Checks */
1238 if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
1239 if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1240
1241 /* Init, lay down lowprob symbols */
Yann Collet494c7862016-01-06 12:54:02 +01001242 DTableH.tableLog = (U16)tableLog;
Yann Colletaa074052015-10-30 11:21:50 +01001243 for (s=0; s<=maxSymbolValue; s++)
1244 {
1245 if (normalizedCounter[s]==-1)
1246 {
1247 tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
1248 symbolNext[s] = 1;
1249 }
1250 else
1251 {
1252 if (normalizedCounter[s] >= largeLimit) noLarge=0;
1253 symbolNext[s] = normalizedCounter[s];
1254 }
1255 }
1256
1257 /* Spread symbols */
1258 for (s=0; s<=maxSymbolValue; s++)
1259 {
1260 int i;
1261 for (i=0; i<normalizedCounter[s]; i++)
1262 {
1263 tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
1264 position = (position + step) & tableMask;
1265 while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
1266 }
1267 }
1268
1269 if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1270
1271 /* Build Decoding table */
1272 {
1273 U32 i;
1274 for (i=0; i<tableSize; i++)
1275 {
1276 FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol);
1277 U16 nextState = symbolNext[symbol]++;
1278 tableDecode[i].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
1279 tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);
1280 }
1281 }
1282
Yann Collet494c7862016-01-06 12:54:02 +01001283 DTableH.fastMode = (U16)noLarge;
1284 memcpy(dt, &DTableH, sizeof(DTableH)); /* memcpy(), to avoid strict aliasing warnings */
Yann Colletaa074052015-10-30 11:21:50 +01001285 return 0;
1286}
1287
1288
1289#ifndef FSE_COMMONDEFS_ONLY
1290/******************************************
1291* FSE helper functions
1292******************************************/
1293static unsigned FSE_isError(size_t code) { return ERR_isError(code); }
1294
1295
1296/****************************************************************
1297* FSE NCount encoding-decoding
1298****************************************************************/
1299static short FSE_abs(short a)
1300{
Yann Colletd5c5a772016-07-19 15:06:55 +02001301 return (short)(a<0 ? -a : a);
Yann Colletaa074052015-10-30 11:21:50 +01001302}
1303
1304static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
1305 const void* headerBuffer, size_t hbSize)
1306{
1307 const BYTE* const istart = (const BYTE*) headerBuffer;
1308 const BYTE* const iend = istart + hbSize;
1309 const BYTE* ip = istart;
1310 int nbBits;
1311 int remaining;
1312 int threshold;
1313 U32 bitStream;
1314 int bitCount;
1315 unsigned charnum = 0;
1316 int previous0 = 0;
1317
1318 if (hbSize < 4) return ERROR(srcSize_wrong);
1319 bitStream = MEM_readLE32(ip);
1320 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
1321 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
1322 bitStream >>= 4;
1323 bitCount = 4;
1324 *tableLogPtr = nbBits;
1325 remaining = (1<<nbBits)+1;
1326 threshold = 1<<nbBits;
1327 nbBits++;
1328
1329 while ((remaining>1) && (charnum<=*maxSVPtr))
1330 {
1331 if (previous0)
1332 {
1333 unsigned n0 = charnum;
1334 while ((bitStream & 0xFFFF) == 0xFFFF)
1335 {
1336 n0+=24;
1337 if (ip < iend-5)
1338 {
1339 ip+=2;
1340 bitStream = MEM_readLE32(ip) >> bitCount;
1341 }
1342 else
1343 {
1344 bitStream >>= 16;
1345 bitCount+=16;
1346 }
1347 }
1348 while ((bitStream & 3) == 3)
1349 {
1350 n0+=3;
1351 bitStream>>=2;
1352 bitCount+=2;
1353 }
1354 n0 += bitStream & 3;
1355 bitCount += 2;
1356 if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
1357 while (charnum < n0) normalizedCounter[charnum++] = 0;
1358 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1359 {
1360 ip += bitCount>>3;
1361 bitCount &= 7;
1362 bitStream = MEM_readLE32(ip) >> bitCount;
1363 }
1364 else
1365 bitStream >>= 2;
1366 }
1367 {
1368 const short max = (short)((2*threshold-1)-remaining);
1369 short count;
1370
1371 if ((bitStream & (threshold-1)) < (U32)max)
1372 {
1373 count = (short)(bitStream & (threshold-1));
1374 bitCount += nbBits-1;
1375 }
1376 else
1377 {
1378 count = (short)(bitStream & (2*threshold-1));
1379 if (count >= threshold) count -= max;
1380 bitCount += nbBits;
1381 }
1382
1383 count--; /* extra accuracy */
1384 remaining -= FSE_abs(count);
1385 normalizedCounter[charnum++] = count;
1386 previous0 = !count;
1387 while (remaining < threshold)
1388 {
1389 nbBits--;
1390 threshold >>= 1;
1391 }
1392
1393 {
1394 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))
1395 {
1396 ip += bitCount>>3;
1397 bitCount &= 7;
1398 }
1399 else
1400 {
1401 bitCount -= (int)(8 * (iend - 4 - ip));
1402 ip = iend - 4;
1403 }
1404 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1405 }
1406 }
1407 }
1408 if (remaining != 1) return ERROR(GENERIC);
1409 *maxSVPtr = charnum-1;
1410
1411 ip += (bitCount+7)>>3;
1412 if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
1413 return ip-istart;
1414}
1415
1416
1417/*********************************************************
1418* Decompression (Byte symbols)
1419*********************************************************/
1420static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
1421{
Yann Collet1fdd8232016-01-06 12:35:42 +01001422 void* ptr = dt;
1423 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1424 FSE_decode_t* const cell = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */
Yann Colletaa074052015-10-30 11:21:50 +01001425
1426 DTableH->tableLog = 0;
1427 DTableH->fastMode = 0;
1428
1429 cell->newState = 0;
1430 cell->symbol = symbolValue;
1431 cell->nbBits = 0;
1432
1433 return 0;
1434}
1435
1436
1437static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
1438{
Yann Collet1fdd8232016-01-06 12:35:42 +01001439 void* ptr = dt;
1440 FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
1441 FSE_decode_t* const dinfo = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */
Yann Colletaa074052015-10-30 11:21:50 +01001442 const unsigned tableSize = 1 << nbBits;
1443 const unsigned tableMask = tableSize - 1;
1444 const unsigned maxSymbolValue = tableMask;
1445 unsigned s;
1446
1447 /* Sanity checks */
1448 if (nbBits < 1) return ERROR(GENERIC); /* min size */
1449
1450 /* Build Decoding Table */
1451 DTableH->tableLog = (U16)nbBits;
1452 DTableH->fastMode = 1;
1453 for (s=0; s<=maxSymbolValue; s++)
1454 {
1455 dinfo[s].newState = 0;
1456 dinfo[s].symbol = (BYTE)s;
1457 dinfo[s].nbBits = (BYTE)nbBits;
1458 }
1459
1460 return 0;
1461}
1462
1463FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
1464 void* dst, size_t maxDstSize,
1465 const void* cSrc, size_t cSrcSize,
1466 const FSE_DTable* dt, const unsigned fast)
1467{
1468 BYTE* const ostart = (BYTE*) dst;
1469 BYTE* op = ostart;
1470 BYTE* const omax = op + maxDstSize;
1471 BYTE* const olimit = omax-3;
1472
1473 BIT_DStream_t bitD;
1474 FSE_DState_t state1;
1475 FSE_DState_t state2;
1476 size_t errorCode;
1477
1478 /* Init */
1479 errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
1480 if (FSE_isError(errorCode)) return errorCode;
1481
1482 FSE_initDState(&state1, &bitD, dt);
1483 FSE_initDState(&state2, &bitD, dt);
1484
1485#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
1486
1487 /* 4 symbols per loop */
1488 for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) && (op<olimit) ; op+=4)
1489 {
1490 op[0] = FSE_GETSYMBOL(&state1);
1491
1492 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1493 BIT_reloadDStream(&bitD);
1494
1495 op[1] = FSE_GETSYMBOL(&state2);
1496
1497 if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1498 { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
1499
1500 op[2] = FSE_GETSYMBOL(&state1);
1501
1502 if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1503 BIT_reloadDStream(&bitD);
1504
1505 op[3] = FSE_GETSYMBOL(&state2);
1506 }
1507
1508 /* tail */
1509 /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
1510 while (1)
1511 {
1512 if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) )
1513 break;
1514
1515 *op++ = FSE_GETSYMBOL(&state1);
1516
1517 if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) )
1518 break;
1519
1520 *op++ = FSE_GETSYMBOL(&state2);
1521 }
1522
1523 /* end ? */
1524 if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2))
1525 return op-ostart;
1526
1527 if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */
1528
1529 return ERROR(corruption_detected);
1530}
1531
1532
1533static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
1534 const void* cSrc, size_t cSrcSize,
1535 const FSE_DTable* dt)
1536{
Yann Collet494c7862016-01-06 12:54:02 +01001537 FSE_DTableHeader DTableH;
1538 memcpy(&DTableH, dt, sizeof(DTableH));
Yann Colletaa074052015-10-30 11:21:50 +01001539
1540 /* select fast mode (static) */
Yann Collet494c7862016-01-06 12:54:02 +01001541 if (DTableH.fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
Yann Colletaa074052015-10-30 11:21:50 +01001542 return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
1543}
1544
1545
1546static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1547{
1548 const BYTE* const istart = (const BYTE*)cSrc;
1549 const BYTE* ip = istart;
1550 short counting[FSE_MAX_SYMBOL_VALUE+1];
1551 DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
1552 unsigned tableLog;
1553 unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
1554 size_t errorCode;
1555
1556 if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
1557
1558 /* normal FSE decoding mode */
1559 errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1560 if (FSE_isError(errorCode)) return errorCode;
1561 if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
1562 ip += errorCode;
1563 cSrcSize -= errorCode;
1564
1565 errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);
1566 if (FSE_isError(errorCode)) return errorCode;
1567
1568 /* always return, even if it is an error code */
1569 return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt);
1570}
1571
1572
1573
1574#endif /* FSE_COMMONDEFS_ONLY */
1575/* ******************************************************************
1576 Huff0 : Huffman coder, part of New Generation Entropy library
1577 Copyright (C) 2013-2015, Yann Collet.
1578
1579 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
1580
1581 Redistribution and use in source and binary forms, with or without
1582 modification, are permitted provided that the following conditions are
1583 met:
1584
1585 * Redistributions of source code must retain the above copyright
1586 notice, this list of conditions and the following disclaimer.
1587 * Redistributions in binary form must reproduce the above
1588 copyright notice, this list of conditions and the following disclaimer
1589 in the documentation and/or other materials provided with the
1590 distribution.
1591
1592 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1593 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1594 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1595 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1596 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1597 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1598 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1599 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1600 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1601 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1602 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1603
1604 You can contact the author at :
1605 - FSE+Huff0 source repository : https://github.com/Cyan4973/FiniteStateEntropy
1606 - Public forum : https://groups.google.com/forum/#!forum/lz4c
1607****************************************************************** */
1608
1609/****************************************************************
1610* Compiler specifics
1611****************************************************************/
1612#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1613/* inline is defined */
1614#elif defined(_MSC_VER)
1615# define inline __inline
1616#else
1617# define inline /* disable inline */
1618#endif
1619
1620
1621#ifdef _MSC_VER /* Visual Studio */
1622# define FORCE_INLINE static __forceinline
1623# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1624#else
1625# ifdef __GNUC__
1626# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
1627# define FORCE_INLINE static inline __attribute__((always_inline))
1628# else
1629# define FORCE_INLINE static inline
1630# endif
1631#endif
1632
1633
1634/****************************************************************
1635* Includes
1636****************************************************************/
1637#include <stdlib.h> /* malloc, free, qsort */
1638#include <string.h> /* memcpy, memset */
1639#include <stdio.h> /* printf (debug) */
1640
1641/****************************************************************
1642* Error Management
1643****************************************************************/
1644#define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1645
1646
1647/******************************************
1648* Helper functions
1649******************************************/
1650static unsigned HUF_isError(size_t code) { return ERR_isError(code); }
1651
1652#define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
1653#define HUF_MAX_TABLELOG 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
1654#define HUF_DEFAULT_TABLELOG HUF_MAX_TABLELOG /* tableLog by default, when not specified */
1655#define HUF_MAX_SYMBOL_VALUE 255
1656#if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG)
1657# error "HUF_MAX_TABLELOG is too large !"
1658#endif
1659
1660
1661
1662/*********************************************************
1663* Huff0 : Huffman block decompression
1664*********************************************************/
1665typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */
1666
1667typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */
1668
1669typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
1670
1671/*! HUF_readStats
1672 Read compact Huffman tree, saved by HUF_writeCTable
1673 @huffWeight : destination buffer
1674 @return : size read from `src`
1675*/
1676static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1677 U32* nbSymbolsPtr, U32* tableLogPtr,
1678 const void* src, size_t srcSize)
1679{
1680 U32 weightTotal;
1681 U32 tableLog;
1682 const BYTE* ip = (const BYTE*) src;
1683 size_t iSize = ip[0];
1684 size_t oSize;
1685 U32 n;
1686
1687 //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1688
1689 if (iSize >= 128) /* special header */
1690 {
1691 if (iSize >= (242)) /* RLE */
1692 {
1693 static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
1694 oSize = l[iSize-242];
1695 memset(huffWeight, 1, hwSize);
1696 iSize = 0;
1697 }
1698 else /* Incompressible */
1699 {
1700 oSize = iSize - 127;
1701 iSize = ((oSize+1)/2);
1702 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1703 if (oSize >= hwSize) return ERROR(corruption_detected);
1704 ip += 1;
1705 for (n=0; n<oSize; n+=2)
1706 {
1707 huffWeight[n] = ip[n/2] >> 4;
1708 huffWeight[n+1] = ip[n/2] & 15;
1709 }
1710 }
1711 }
1712 else /* header compressed with FSE (normal case) */
1713 {
1714 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1715 oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
1716 if (FSE_isError(oSize)) return oSize;
1717 }
1718
1719 /* collect weight stats */
1720 memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32));
1721 weightTotal = 0;
1722 for (n=0; n<oSize; n++)
1723 {
1724 if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1725 rankStats[huffWeight[n]]++;
1726 weightTotal += (1 << huffWeight[n]) >> 1;
1727 }
1728
1729 /* get last non-null symbol weight (implied, total must be 2^n) */
1730 tableLog = BIT_highbit32(weightTotal) + 1;
1731 if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1732 {
1733 U32 total = 1 << tableLog;
1734 U32 rest = total - weightTotal;
1735 U32 verif = 1 << BIT_highbit32(rest);
1736 U32 lastWeight = BIT_highbit32(rest) + 1;
1737 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
1738 huffWeight[oSize] = (BYTE)lastWeight;
1739 rankStats[lastWeight]++;
1740 }
1741
1742 /* check tree construction validity */
1743 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
1744
1745 /* results */
1746 *nbSymbolsPtr = (U32)(oSize+1);
1747 *tableLogPtr = tableLog;
1748 return iSize+1;
1749}
1750
1751
1752/**************************/
1753/* single-symbol decoding */
1754/**************************/
1755
1756static size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1757{
1758 BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1];
1759 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */
1760 U32 tableLog = 0;
1761 const BYTE* ip = (const BYTE*) src;
1762 size_t iSize = ip[0];
1763 U32 nbSymbols = 0;
1764 U32 n;
1765 U32 nextRankStart;
Yann Collet1fdd8232016-01-06 12:35:42 +01001766 void* ptr = DTable+1;
1767 HUF_DEltX2* const dt = (HUF_DEltX2*)ptr;
Yann Colletaa074052015-10-30 11:21:50 +01001768
1769 HUF_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
1770 //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
1771
1772 iSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1773 if (HUF_isError(iSize)) return iSize;
1774
1775 /* check result */
1776 if (tableLog > DTable[0]) return ERROR(tableLog_tooLarge); /* DTable is too small */
1777 DTable[0] = (U16)tableLog; /* maybe should separate sizeof DTable, as allocated, from used size of DTable, in case of DTable re-use */
1778
1779 /* Prepare ranks */
1780 nextRankStart = 0;
1781 for (n=1; n<=tableLog; n++)
1782 {
1783 U32 current = nextRankStart;
1784 nextRankStart += (rankVal[n] << (n-1));
1785 rankVal[n] = current;
1786 }
1787
1788 /* fill DTable */
1789 for (n=0; n<nbSymbols; n++)
1790 {
1791 const U32 w = huffWeight[n];
1792 const U32 length = (1 << w) >> 1;
1793 U32 i;
1794 HUF_DEltX2 D;
1795 D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
1796 for (i = rankVal[w]; i < rankVal[w] + length; i++)
1797 dt[i] = D;
1798 rankVal[w] += length;
1799 }
1800
1801 return iSize;
1802}
1803
1804static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog)
1805{
1806 const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
1807 const BYTE c = dt[val].byte;
1808 BIT_skipBits(Dstream, dt[val].nbBits);
1809 return c;
1810}
1811
1812#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
1813 *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog)
1814
1815#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
1816 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
1817 HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1818
1819#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
1820 if (MEM_64bits()) \
1821 HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1822
1823static inline size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog)
1824{
1825 BYTE* const pStart = p;
1826
1827 /* up to 4 symbols at a time */
1828 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4))
1829 {
1830 HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1831 HUF_DECODE_SYMBOLX2_1(p, bitDPtr);
1832 HUF_DECODE_SYMBOLX2_2(p, bitDPtr);
1833 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1834 }
1835
1836 /* closer to the end */
1837 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd))
1838 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1839
1840 /* no more data to retrieve from bitstream, hence no need to reload */
1841 while (p < pEnd)
1842 HUF_DECODE_SYMBOLX2_0(p, bitDPtr);
1843
1844 return pEnd-pStart;
1845}
1846
1847
1848static size_t HUF_decompress4X2_usingDTable(
1849 void* dst, size_t dstSize,
1850 const void* cSrc, size_t cSrcSize,
1851 const U16* DTable)
1852{
1853 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
1854
1855 {
1856 const BYTE* const istart = (const BYTE*) cSrc;
1857 BYTE* const ostart = (BYTE*) dst;
1858 BYTE* const oend = ostart + dstSize;
1859
Yann Collet1fdd8232016-01-06 12:35:42 +01001860 const void* ptr = DTable;
1861 const HUF_DEltX2* const dt = ((const HUF_DEltX2*)ptr) +1;
Yann Colletaa074052015-10-30 11:21:50 +01001862 const U32 dtLog = DTable[0];
1863 size_t errorCode;
1864
1865 /* Init */
1866 BIT_DStream_t bitD1;
1867 BIT_DStream_t bitD2;
1868 BIT_DStream_t bitD3;
1869 BIT_DStream_t bitD4;
1870 const size_t length1 = MEM_readLE16(istart);
1871 const size_t length2 = MEM_readLE16(istart+2);
1872 const size_t length3 = MEM_readLE16(istart+4);
1873 size_t length4;
1874 const BYTE* const istart1 = istart + 6; /* jumpTable */
1875 const BYTE* const istart2 = istart1 + length1;
1876 const BYTE* const istart3 = istart2 + length2;
1877 const BYTE* const istart4 = istart3 + length3;
1878 const size_t segmentSize = (dstSize+3) / 4;
1879 BYTE* const opStart2 = ostart + segmentSize;
1880 BYTE* const opStart3 = opStart2 + segmentSize;
1881 BYTE* const opStart4 = opStart3 + segmentSize;
1882 BYTE* op1 = ostart;
1883 BYTE* op2 = opStart2;
1884 BYTE* op3 = opStart3;
1885 BYTE* op4 = opStart4;
1886 U32 endSignal;
1887
1888 length4 = cSrcSize - (length1 + length2 + length3 + 6);
1889 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
1890 errorCode = BIT_initDStream(&bitD1, istart1, length1);
1891 if (HUF_isError(errorCode)) return errorCode;
1892 errorCode = BIT_initDStream(&bitD2, istart2, length2);
1893 if (HUF_isError(errorCode)) return errorCode;
1894 errorCode = BIT_initDStream(&bitD3, istart3, length3);
1895 if (HUF_isError(errorCode)) return errorCode;
1896 errorCode = BIT_initDStream(&bitD4, istart4, length4);
1897 if (HUF_isError(errorCode)) return errorCode;
1898
1899 /* 16-32 symbols per loop (4-8 symbols per stream) */
1900 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1901 for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
1902 {
1903 HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1904 HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1905 HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1906 HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1907 HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
1908 HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
1909 HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
1910 HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
1911 HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
1912 HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
1913 HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
1914 HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
1915 HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
1916 HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
1917 HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
1918 HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
1919
1920 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
1921 }
1922
1923 /* check corruption */
1924 if (op1 > opStart2) return ERROR(corruption_detected);
1925 if (op2 > opStart3) return ERROR(corruption_detected);
1926 if (op3 > opStart4) return ERROR(corruption_detected);
1927 /* note : op4 supposed already verified within main loop */
1928
1929 /* finish bitStreams one by one */
1930 HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
1931 HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
1932 HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
1933 HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
1934
1935 /* check */
1936 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
1937 if (!endSignal) return ERROR(corruption_detected);
1938
1939 /* decoded size */
1940 return dstSize;
1941 }
1942}
1943
1944
1945static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1946{
1947 HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG);
1948 const BYTE* ip = (const BYTE*) cSrc;
1949 size_t errorCode;
1950
1951 errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize);
1952 if (HUF_isError(errorCode)) return errorCode;
1953 if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
1954 ip += errorCode;
1955 cSrcSize -= errorCode;
1956
1957 return HUF_decompress4X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
1958}
1959
1960
1961/***************************/
1962/* double-symbols decoding */
1963/***************************/
1964
1965static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed,
1966 const U32* rankValOrigin, const int minWeight,
1967 const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
1968 U32 nbBitsBaseline, U16 baseSeq)
1969{
1970 HUF_DEltX4 DElt;
1971 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
1972 U32 s;
1973
1974 /* get pre-calculated rankVal */
1975 memcpy(rankVal, rankValOrigin, sizeof(rankVal));
1976
1977 /* fill skipped values */
1978 if (minWeight>1)
1979 {
1980 U32 i, skipSize = rankVal[minWeight];
1981 MEM_writeLE16(&(DElt.sequence), baseSeq);
1982 DElt.nbBits = (BYTE)(consumed);
1983 DElt.length = 1;
1984 for (i = 0; i < skipSize; i++)
1985 DTable[i] = DElt;
1986 }
1987
1988 /* fill DTable */
1989 for (s=0; s<sortedListSize; s++) /* note : sortedSymbols already skipped */
1990 {
1991 const U32 symbol = sortedSymbols[s].symbol;
1992 const U32 weight = sortedSymbols[s].weight;
1993 const U32 nbBits = nbBitsBaseline - weight;
1994 const U32 length = 1 << (sizeLog-nbBits);
1995 const U32 start = rankVal[weight];
1996 U32 i = start;
1997 const U32 end = start + length;
1998
1999 MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
2000 DElt.nbBits = (BYTE)(nbBits + consumed);
2001 DElt.length = 2;
2002 do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
2003
2004 rankVal[weight] += length;
2005 }
2006}
2007
2008typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1];
2009
2010static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
2011 const sortedSymbol_t* sortedList, const U32 sortedListSize,
2012 const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
2013 const U32 nbBitsBaseline)
2014{
2015 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
2016 const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
2017 const U32 minBits = nbBitsBaseline - maxWeight;
2018 U32 s;
2019
2020 memcpy(rankVal, rankValOrigin, sizeof(rankVal));
2021
2022 /* fill DTable */
2023 for (s=0; s<sortedListSize; s++)
2024 {
2025 const U16 symbol = sortedList[s].symbol;
2026 const U32 weight = sortedList[s].weight;
2027 const U32 nbBits = nbBitsBaseline - weight;
2028 const U32 start = rankVal[weight];
2029 const U32 length = 1 << (targetLog-nbBits);
2030
2031 if (targetLog-nbBits >= minBits) /* enough room for a second symbol */
2032 {
2033 U32 sortedRank;
2034 int minWeight = nbBits + scaleLog;
2035 if (minWeight < 1) minWeight = 1;
2036 sortedRank = rankStart[minWeight];
2037 HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
2038 rankValOrigin[nbBits], minWeight,
2039 sortedList+sortedRank, sortedListSize-sortedRank,
2040 nbBitsBaseline, symbol);
2041 }
2042 else
2043 {
2044 U32 i;
2045 const U32 end = start + length;
2046 HUF_DEltX4 DElt;
2047
2048 MEM_writeLE16(&(DElt.sequence), symbol);
2049 DElt.nbBits = (BYTE)(nbBits);
2050 DElt.length = 1;
2051 for (i = start; i < end; i++)
2052 DTable[i] = DElt;
2053 }
2054 rankVal[weight] += length;
2055 }
2056}
2057
2058static size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
2059{
2060 BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1];
2061 sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1];
2062 U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
2063 U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
2064 U32* const rankStart = rankStart0+1;
2065 rankVal_t rankVal;
2066 U32 tableLog, maxW, sizeOfSort, nbSymbols;
2067 const U32 memLog = DTable[0];
2068 const BYTE* ip = (const BYTE*) src;
2069 size_t iSize = ip[0];
Yann Collet1fdd8232016-01-06 12:35:42 +01002070 void* ptr = DTable;
2071 HUF_DEltX4* const dt = ((HUF_DEltX4*)ptr) + 1;
Yann Colletaa074052015-10-30 11:21:50 +01002072
2073 HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */
2074 if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2075 //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2076
2077 iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2078 if (HUF_isError(iSize)) return iSize;
2079
2080 /* check result */
2081 if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
2082
2083 /* find maxWeight */
Yann Collet6bff7482016-02-09 17:55:01 +01002084 for (maxW = tableLog; rankStats[maxW]==0; maxW--)
2085 {if (!maxW) return ERROR(GENERIC); } /* necessarily finds a solution before maxW==0 */
Yann Colletaa074052015-10-30 11:21:50 +01002086
2087 /* Get start index of each weight */
2088 {
2089 U32 w, nextRankStart = 0;
2090 for (w=1; w<=maxW; w++)
2091 {
2092 U32 current = nextRankStart;
2093 nextRankStart += rankStats[w];
2094 rankStart[w] = current;
2095 }
2096 rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
2097 sizeOfSort = nextRankStart;
2098 }
2099
2100 /* sort symbols by weight */
2101 {
2102 U32 s;
2103 for (s=0; s<nbSymbols; s++)
2104 {
2105 U32 w = weightList[s];
2106 U32 r = rankStart[w]++;
2107 sortedSymbol[r].symbol = (BYTE)s;
2108 sortedSymbol[r].weight = (BYTE)w;
2109 }
2110 rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
2111 }
2112
2113 /* Build rankVal */
2114 {
2115 const U32 minBits = tableLog+1 - maxW;
2116 U32 nextRankVal = 0;
2117 U32 w, consumed;
2118 const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
2119 U32* rankVal0 = rankVal[0];
2120 for (w=1; w<=maxW; w++)
2121 {
2122 U32 current = nextRankVal;
2123 nextRankVal += rankStats[w] << (w+rescale);
2124 rankVal0[w] = current;
2125 }
2126 for (consumed = minBits; consumed <= memLog - minBits; consumed++)
2127 {
2128 U32* rankValPtr = rankVal[consumed];
2129 for (w = 1; w <= maxW; w++)
2130 {
2131 rankValPtr[w] = rankVal0[w] >> consumed;
2132 }
2133 }
2134 }
2135
2136 HUF_fillDTableX4(dt, memLog,
2137 sortedSymbol, sizeOfSort,
2138 rankStart0, rankVal, maxW,
2139 tableLog+1);
2140
2141 return iSize;
2142}
2143
2144
2145static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
2146{
2147 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2148 memcpy(op, dt+val, 2);
2149 BIT_skipBits(DStream, dt[val].nbBits);
2150 return dt[val].length;
2151}
2152
2153static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog)
2154{
2155 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2156 memcpy(op, dt+val, 1);
2157 if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
2158 else
2159 {
2160 if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8))
2161 {
2162 BIT_skipBits(DStream, dt[val].nbBits);
2163 if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2164 DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8); /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
2165 }
2166 }
2167 return 1;
2168}
2169
2170
2171#define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
2172 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2173
2174#define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
2175 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
2176 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2177
2178#define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
2179 if (MEM_64bits()) \
2180 ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2181
2182static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog)
2183{
2184 BYTE* const pStart = p;
2185
2186 /* up to 8 symbols at a time */
2187 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd-7))
2188 {
2189 HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
2190 HUF_DECODE_SYMBOLX4_1(p, bitDPtr);
2191 HUF_DECODE_SYMBOLX4_2(p, bitDPtr);
2192 HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2193 }
2194
2195 /* closer to the end */
2196 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-2))
2197 HUF_DECODE_SYMBOLX4_0(p, bitDPtr);
2198
2199 while (p <= pEnd-2)
2200 HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
2201
2202 if (p < pEnd)
2203 p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
2204
2205 return p-pStart;
2206}
2207
2208
2209
2210static size_t HUF_decompress4X4_usingDTable(
2211 void* dst, size_t dstSize,
2212 const void* cSrc, size_t cSrcSize,
2213 const U32* DTable)
2214{
2215 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2216
2217 {
2218 const BYTE* const istart = (const BYTE*) cSrc;
2219 BYTE* const ostart = (BYTE*) dst;
2220 BYTE* const oend = ostart + dstSize;
2221
Yann Collet1fdd8232016-01-06 12:35:42 +01002222 const void* ptr = DTable;
2223 const HUF_DEltX4* const dt = ((const HUF_DEltX4*)ptr) +1;
Yann Colletaa074052015-10-30 11:21:50 +01002224 const U32 dtLog = DTable[0];
2225 size_t errorCode;
2226
2227 /* Init */
2228 BIT_DStream_t bitD1;
2229 BIT_DStream_t bitD2;
2230 BIT_DStream_t bitD3;
2231 BIT_DStream_t bitD4;
2232 const size_t length1 = MEM_readLE16(istart);
2233 const size_t length2 = MEM_readLE16(istart+2);
2234 const size_t length3 = MEM_readLE16(istart+4);
2235 size_t length4;
2236 const BYTE* const istart1 = istart + 6; /* jumpTable */
2237 const BYTE* const istart2 = istart1 + length1;
2238 const BYTE* const istart3 = istart2 + length2;
2239 const BYTE* const istart4 = istart3 + length3;
2240 const size_t segmentSize = (dstSize+3) / 4;
2241 BYTE* const opStart2 = ostart + segmentSize;
2242 BYTE* const opStart3 = opStart2 + segmentSize;
2243 BYTE* const opStart4 = opStart3 + segmentSize;
2244 BYTE* op1 = ostart;
2245 BYTE* op2 = opStart2;
2246 BYTE* op3 = opStart3;
2247 BYTE* op4 = opStart4;
2248 U32 endSignal;
2249
2250 length4 = cSrcSize - (length1 + length2 + length3 + 6);
2251 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2252 errorCode = BIT_initDStream(&bitD1, istart1, length1);
2253 if (HUF_isError(errorCode)) return errorCode;
2254 errorCode = BIT_initDStream(&bitD2, istart2, length2);
2255 if (HUF_isError(errorCode)) return errorCode;
2256 errorCode = BIT_initDStream(&bitD3, istart3, length3);
2257 if (HUF_isError(errorCode)) return errorCode;
2258 errorCode = BIT_initDStream(&bitD4, istart4, length4);
2259 if (HUF_isError(errorCode)) return errorCode;
2260
2261 /* 16-32 symbols per loop (4-8 symbols per stream) */
2262 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2263 for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; )
2264 {
2265 HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2266 HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2267 HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2268 HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2269 HUF_DECODE_SYMBOLX4_1(op1, &bitD1);
2270 HUF_DECODE_SYMBOLX4_1(op2, &bitD2);
2271 HUF_DECODE_SYMBOLX4_1(op3, &bitD3);
2272 HUF_DECODE_SYMBOLX4_1(op4, &bitD4);
2273 HUF_DECODE_SYMBOLX4_2(op1, &bitD1);
2274 HUF_DECODE_SYMBOLX4_2(op2, &bitD2);
2275 HUF_DECODE_SYMBOLX4_2(op3, &bitD3);
2276 HUF_DECODE_SYMBOLX4_2(op4, &bitD4);
2277 HUF_DECODE_SYMBOLX4_0(op1, &bitD1);
2278 HUF_DECODE_SYMBOLX4_0(op2, &bitD2);
2279 HUF_DECODE_SYMBOLX4_0(op3, &bitD3);
2280 HUF_DECODE_SYMBOLX4_0(op4, &bitD4);
2281
2282 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2283 }
2284
2285 /* check corruption */
2286 if (op1 > opStart2) return ERROR(corruption_detected);
2287 if (op2 > opStart3) return ERROR(corruption_detected);
2288 if (op3 > opStart4) return ERROR(corruption_detected);
2289 /* note : op4 supposed already verified within main loop */
2290
2291 /* finish bitStreams one by one */
2292 HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
2293 HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
2294 HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
2295 HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
2296
2297 /* check */
2298 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
2299 if (!endSignal) return ERROR(corruption_detected);
2300
2301 /* decoded size */
2302 return dstSize;
2303 }
2304}
2305
2306
2307static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2308{
2309 HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG);
2310 const BYTE* ip = (const BYTE*) cSrc;
2311
2312 size_t hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize);
2313 if (HUF_isError(hSize)) return hSize;
2314 if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2315 ip += hSize;
2316 cSrcSize -= hSize;
2317
2318 return HUF_decompress4X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2319}
2320
2321
2322/**********************************/
2323/* quad-symbol decoding */
2324/**********************************/
2325typedef struct { BYTE nbBits; BYTE nbBytes; } HUF_DDescX6;
2326typedef union { BYTE byte[4]; U32 sequence; } HUF_DSeqX6;
2327
2328/* recursive, up to level 3; may benefit from <template>-like strategy to nest each level inline */
2329static void HUF_fillDTableX6LevelN(HUF_DDescX6* DDescription, HUF_DSeqX6* DSequence, int sizeLog,
2330 const rankVal_t rankValOrigin, const U32 consumed, const int minWeight, const U32 maxWeight,
2331 const sortedSymbol_t* sortedSymbols, const U32 sortedListSize, const U32* rankStart,
2332 const U32 nbBitsBaseline, HUF_DSeqX6 baseSeq, HUF_DDescX6 DDesc)
2333{
2334 const int scaleLog = nbBitsBaseline - sizeLog; /* note : targetLog >= (nbBitsBaseline-1), hence scaleLog <= 1 */
2335 const int minBits = nbBitsBaseline - maxWeight;
2336 const U32 level = DDesc.nbBytes;
2337 U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
2338 U32 symbolStartPos, s;
2339
2340 /* local rankVal, will be modified */
2341 memcpy(rankVal, rankValOrigin[consumed], sizeof(rankVal));
2342
2343 /* fill skipped values */
2344 if (minWeight>1)
2345 {
2346 U32 i;
2347 const U32 skipSize = rankVal[minWeight];
2348 for (i = 0; i < skipSize; i++)
2349 {
2350 DSequence[i] = baseSeq;
2351 DDescription[i] = DDesc;
2352 }
2353 }
2354
2355 /* fill DTable */
2356 DDesc.nbBytes++;
2357 symbolStartPos = rankStart[minWeight];
2358 for (s=symbolStartPos; s<sortedListSize; s++)
2359 {
2360 const BYTE symbol = sortedSymbols[s].symbol;
2361 const U32 weight = sortedSymbols[s].weight; /* >= 1 (sorted) */
2362 const int nbBits = nbBitsBaseline - weight; /* >= 1 (by construction) */
2363 const int totalBits = consumed+nbBits;
2364 const U32 start = rankVal[weight];
2365 const U32 length = 1 << (sizeLog-nbBits);
2366 baseSeq.byte[level] = symbol;
2367 DDesc.nbBits = (BYTE)totalBits;
2368
2369 if ((level<3) && (sizeLog-totalBits >= minBits)) /* enough room for another symbol */
2370 {
2371 int nextMinWeight = totalBits + scaleLog;
2372 if (nextMinWeight < 1) nextMinWeight = 1;
2373 HUF_fillDTableX6LevelN(DDescription+start, DSequence+start, sizeLog-nbBits,
2374 rankValOrigin, totalBits, nextMinWeight, maxWeight,
2375 sortedSymbols, sortedListSize, rankStart,
2376 nbBitsBaseline, baseSeq, DDesc); /* recursive (max : level 3) */
2377 }
2378 else
2379 {
2380 U32 i;
2381 const U32 end = start + length;
2382 for (i = start; i < end; i++)
2383 {
2384 DDescription[i] = DDesc;
2385 DSequence[i] = baseSeq;
2386 }
2387 }
2388 rankVal[weight] += length;
2389 }
2390}
2391
2392
2393/* note : same preparation as X4 */
2394static size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
2395{
2396 BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1];
2397 sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1];
2398 U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
2399 U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
2400 U32* const rankStart = rankStart0+1;
2401 U32 tableLog, maxW, sizeOfSort, nbSymbols;
2402 rankVal_t rankVal;
2403 const U32 memLog = DTable[0];
2404 const BYTE* ip = (const BYTE*) src;
2405 size_t iSize = ip[0];
2406
2407 if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2408 //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2409
2410 iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2411 if (HUF_isError(iSize)) return iSize;
2412
2413 /* check result */
2414 if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable is too small */
2415
2416 /* find maxWeight */
Yann Collet6bff7482016-02-09 17:55:01 +01002417 for (maxW = tableLog; rankStats[maxW]==0; maxW--)
2418 { if (!maxW) return ERROR(GENERIC); } /* necessarily finds a solution before maxW==0 */
2419
Yann Colletaa074052015-10-30 11:21:50 +01002420
2421 /* Get start index of each weight */
2422 {
2423 U32 w, nextRankStart = 0;
2424 for (w=1; w<=maxW; w++)
2425 {
2426 U32 current = nextRankStart;
2427 nextRankStart += rankStats[w];
2428 rankStart[w] = current;
2429 }
2430 rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
2431 sizeOfSort = nextRankStart;
2432 }
2433
2434 /* sort symbols by weight */
2435 {
2436 U32 s;
2437 for (s=0; s<nbSymbols; s++)
2438 {
2439 U32 w = weightList[s];
2440 U32 r = rankStart[w]++;
2441 sortedSymbol[r].symbol = (BYTE)s;
2442 sortedSymbol[r].weight = (BYTE)w;
2443 }
2444 rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
2445 }
2446
2447 /* Build rankVal */
2448 {
2449 const U32 minBits = tableLog+1 - maxW;
2450 U32 nextRankVal = 0;
2451 U32 w, consumed;
2452 const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
2453 U32* rankVal0 = rankVal[0];
2454 for (w=1; w<=maxW; w++)
2455 {
2456 U32 current = nextRankVal;
2457 nextRankVal += rankStats[w] << (w+rescale);
2458 rankVal0[w] = current;
2459 }
2460 for (consumed = minBits; consumed <= memLog - minBits; consumed++)
2461 {
2462 U32* rankValPtr = rankVal[consumed];
2463 for (w = 1; w <= maxW; w++)
2464 {
2465 rankValPtr[w] = rankVal0[w] >> consumed;
2466 }
2467 }
2468 }
2469
2470
2471 /* fill tables */
2472 {
Yann Collet1fdd8232016-01-06 12:35:42 +01002473 void* ptr = DTable+1;
2474 HUF_DDescX6* DDescription = (HUF_DDescX6*)(ptr);
2475 void* dSeqStart = DTable + 1 + ((size_t)1<<(memLog-1));
2476 HUF_DSeqX6* DSequence = (HUF_DSeqX6*)(dSeqStart);
Yann Colletaa074052015-10-30 11:21:50 +01002477 HUF_DSeqX6 DSeq;
2478 HUF_DDescX6 DDesc;
2479 DSeq.sequence = 0;
2480 DDesc.nbBits = 0;
2481 DDesc.nbBytes = 0;
2482 HUF_fillDTableX6LevelN(DDescription, DSequence, memLog,
2483 (const U32 (*)[HUF_ABSOLUTEMAX_TABLELOG + 1])rankVal, 0, 1, maxW,
2484 sortedSymbol, sizeOfSort, rankStart0,
2485 tableLog+1, DSeq, DDesc);
2486 }
2487
2488 return iSize;
2489}
2490
2491
2492static U32 HUF_decodeSymbolX6(void* op, BIT_DStream_t* DStream, const HUF_DDescX6* dd, const HUF_DSeqX6* ds, const U32 dtLog)
2493{
2494 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2495 memcpy(op, ds+val, sizeof(HUF_DSeqX6));
2496 BIT_skipBits(DStream, dd[val].nbBits);
2497 return dd[val].nbBytes;
2498}
2499
2500static U32 HUF_decodeLastSymbolsX6(void* op, const U32 maxL, BIT_DStream_t* DStream,
2501 const HUF_DDescX6* dd, const HUF_DSeqX6* ds, const U32 dtLog)
2502{
2503 const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2504 U32 length = dd[val].nbBytes;
2505 if (length <= maxL)
2506 {
2507 memcpy(op, ds+val, length);
2508 BIT_skipBits(DStream, dd[val].nbBits);
2509 return length;
2510 }
2511 memcpy(op, ds+val, maxL);
2512 if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8))
2513 {
2514 BIT_skipBits(DStream, dd[val].nbBits);
2515 if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2516 DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8); /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
2517 }
2518 return maxL;
2519}
2520
2521
2522#define HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr) \
2523 ptr += HUF_decodeSymbolX6(ptr, DStreamPtr, dd, ds, dtLog)
2524
2525#define HUF_DECODE_SYMBOLX6_1(ptr, DStreamPtr) \
2526 if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \
2527 HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr)
2528
2529#define HUF_DECODE_SYMBOLX6_2(ptr, DStreamPtr) \
2530 if (MEM_64bits()) \
2531 HUF_DECODE_SYMBOLX6_0(ptr, DStreamPtr)
2532
2533static inline size_t HUF_decodeStreamX6(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const U32* DTable, const U32 dtLog)
2534{
Yann Collet1fdd8232016-01-06 12:35:42 +01002535 const void* ddPtr = DTable+1;
2536 const HUF_DDescX6* dd = (const HUF_DDescX6*)(ddPtr);
2537 const void* dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
2538 const HUF_DSeqX6* ds = (const HUF_DSeqX6*)(dsPtr);
Yann Colletaa074052015-10-30 11:21:50 +01002539 BYTE* const pStart = p;
2540
2541 /* up to 16 symbols at a time */
2542 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-16))
2543 {
2544 HUF_DECODE_SYMBOLX6_2(p, bitDPtr);
2545 HUF_DECODE_SYMBOLX6_1(p, bitDPtr);
2546 HUF_DECODE_SYMBOLX6_2(p, bitDPtr);
2547 HUF_DECODE_SYMBOLX6_0(p, bitDPtr);
2548 }
2549
2550 /* closer to the end, up to 4 symbols at a time */
2551 while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4))
2552 HUF_DECODE_SYMBOLX6_0(p, bitDPtr);
2553
2554 while (p <= pEnd-4)
2555 HUF_DECODE_SYMBOLX6_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
2556
2557 while (p < pEnd)
2558 p += HUF_decodeLastSymbolsX6(p, (U32)(pEnd-p), bitDPtr, dd, ds, dtLog);
2559
2560 return p-pStart;
2561}
2562
2563
2564
2565static size_t HUF_decompress4X6_usingDTable(
2566 void* dst, size_t dstSize,
2567 const void* cSrc, size_t cSrcSize,
2568 const U32* DTable)
2569{
2570 if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2571
2572 {
2573 const BYTE* const istart = (const BYTE*) cSrc;
2574 BYTE* const ostart = (BYTE*) dst;
2575 BYTE* const oend = ostart + dstSize;
2576
2577 const U32 dtLog = DTable[0];
Yann Collet1fdd8232016-01-06 12:35:42 +01002578 const void* ddPtr = DTable+1;
2579 const HUF_DDescX6* dd = (const HUF_DDescX6*)(ddPtr);
2580 const void* dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
2581 const HUF_DSeqX6* ds = (const HUF_DSeqX6*)(dsPtr);
Yann Colletaa074052015-10-30 11:21:50 +01002582 size_t errorCode;
2583
2584 /* Init */
2585 BIT_DStream_t bitD1;
2586 BIT_DStream_t bitD2;
2587 BIT_DStream_t bitD3;
2588 BIT_DStream_t bitD4;
2589 const size_t length1 = MEM_readLE16(istart);
2590 const size_t length2 = MEM_readLE16(istart+2);
2591 const size_t length3 = MEM_readLE16(istart+4);
2592 size_t length4;
2593 const BYTE* const istart1 = istart + 6; /* jumpTable */
2594 const BYTE* const istart2 = istart1 + length1;
2595 const BYTE* const istart3 = istart2 + length2;
2596 const BYTE* const istart4 = istart3 + length3;
2597 const size_t segmentSize = (dstSize+3) / 4;
2598 BYTE* const opStart2 = ostart + segmentSize;
2599 BYTE* const opStart3 = opStart2 + segmentSize;
2600 BYTE* const opStart4 = opStart3 + segmentSize;
2601 BYTE* op1 = ostart;
2602 BYTE* op2 = opStart2;
2603 BYTE* op3 = opStart3;
2604 BYTE* op4 = opStart4;
2605 U32 endSignal;
2606
2607 length4 = cSrcSize - (length1 + length2 + length3 + 6);
2608 if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2609 errorCode = BIT_initDStream(&bitD1, istart1, length1);
2610 if (HUF_isError(errorCode)) return errorCode;
2611 errorCode = BIT_initDStream(&bitD2, istart2, length2);
2612 if (HUF_isError(errorCode)) return errorCode;
2613 errorCode = BIT_initDStream(&bitD3, istart3, length3);
2614 if (HUF_isError(errorCode)) return errorCode;
2615 errorCode = BIT_initDStream(&bitD4, istart4, length4);
2616 if (HUF_isError(errorCode)) return errorCode;
2617
2618 /* 16-64 symbols per loop (4-16 symbols per stream) */
2619 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2620 for ( ; (op3 <= opStart4) && (endSignal==BIT_DStream_unfinished) && (op4<=(oend-16)) ; )
2621 {
2622 HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
2623 HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
2624 HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
2625 HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
2626 HUF_DECODE_SYMBOLX6_1(op1, &bitD1);
2627 HUF_DECODE_SYMBOLX6_1(op2, &bitD2);
2628 HUF_DECODE_SYMBOLX6_1(op3, &bitD3);
2629 HUF_DECODE_SYMBOLX6_1(op4, &bitD4);
2630 HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
2631 HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
2632 HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
2633 HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
2634 HUF_DECODE_SYMBOLX6_0(op1, &bitD1);
2635 HUF_DECODE_SYMBOLX6_0(op2, &bitD2);
2636 HUF_DECODE_SYMBOLX6_0(op3, &bitD3);
2637 HUF_DECODE_SYMBOLX6_0(op4, &bitD4);
2638
2639 endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
2640 }
2641
2642 /* check corruption */
2643 if (op1 > opStart2) return ERROR(corruption_detected);
2644 if (op2 > opStart3) return ERROR(corruption_detected);
2645 if (op3 > opStart4) return ERROR(corruption_detected);
2646 /* note : op4 supposed already verified within main loop */
2647
2648 /* finish bitStreams one by one */
2649 HUF_decodeStreamX6(op1, &bitD1, opStart2, DTable, dtLog);
2650 HUF_decodeStreamX6(op2, &bitD2, opStart3, DTable, dtLog);
2651 HUF_decodeStreamX6(op3, &bitD3, opStart4, DTable, dtLog);
2652 HUF_decodeStreamX6(op4, &bitD4, oend, DTable, dtLog);
2653
2654 /* check */
2655 endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
2656 if (!endSignal) return ERROR(corruption_detected);
2657
2658 /* decoded size */
2659 return dstSize;
2660 }
2661}
2662
2663
2664static size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2665{
2666 HUF_CREATE_STATIC_DTABLEX6(DTable, HUF_MAX_TABLELOG);
2667 const BYTE* ip = (const BYTE*) cSrc;
2668
2669 size_t hSize = HUF_readDTableX6 (DTable, cSrc, cSrcSize);
2670 if (HUF_isError(hSize)) return hSize;
2671 if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2672 ip += hSize;
2673 cSrcSize -= hSize;
2674
2675 return HUF_decompress4X6_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2676}
2677
2678
2679/**********************************/
2680/* Generic decompression selector */
2681/**********************************/
2682
2683typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
2684static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
2685{
2686 /* single, double, quad */
2687 {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
2688 {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
2689 {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
2690 {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
2691 {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
2692 {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
2693 {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
2694 {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
2695 {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
2696 {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
2697 {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
2698 {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
2699 {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
2700 {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
2701 {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
2702 {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
2703};
2704
2705typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
2706
2707static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2708{
2709 static const decompressionAlgo decompress[3] = { HUF_decompress4X2, HUF_decompress4X4, HUF_decompress4X6 };
2710 /* estimate decompression time */
2711 U32 Q;
2712 const U32 D256 = (U32)(dstSize >> 8);
2713 U32 Dtime[3];
2714 U32 algoNb = 0;
2715 int n;
2716
2717 /* validation checks */
2718 if (dstSize == 0) return ERROR(dstSize_tooSmall);
2719 if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
2720 if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
2721 if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
2722
2723 /* decoder timing evaluation */
2724 Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */
2725 for (n=0; n<3; n++)
2726 Dtime[n] = algoTime[Q][n].tableTime + (algoTime[Q][n].decode256Time * D256);
2727
2728 Dtime[1] += Dtime[1] >> 4; Dtime[2] += Dtime[2] >> 3; /* advantage to algorithms using less memory, for cache eviction */
2729
2730 if (Dtime[1] < Dtime[0]) algoNb = 1;
2731 if (Dtime[2] < Dtime[algoNb]) algoNb = 2;
2732
2733 return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2734
2735 //return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
2736 //return HUF_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
2737 //return HUF_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */
2738}
2739/*
2740 zstd - standard compression library
2741 Copyright (C) 2014-2015, Yann Collet.
2742
2743 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
2744
2745 Redistribution and use in source and binary forms, with or without
2746 modification, are permitted provided that the following conditions are
2747 met:
2748 * Redistributions of source code must retain the above copyright
2749 notice, this list of conditions and the following disclaimer.
2750 * Redistributions in binary form must reproduce the above
2751 copyright notice, this list of conditions and the following disclaimer
2752 in the documentation and/or other materials provided with the
2753 distribution.
2754 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2755 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2756 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2757 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2758 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2759 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2760 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2761 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2762 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2763 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2764 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2765
2766 You can contact the author at :
2767 - zstd source repository : https://github.com/Cyan4973/zstd
2768 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
2769*/
2770
2771/* ***************************************************************
2772* Tuning parameters
2773*****************************************************************/
2774/*!
2775* MEMORY_USAGE :
2776* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
2777* Increasing memory usage improves compression ratio
2778* Reduced memory usage can improve speed, due to cache effect
2779*/
2780#define ZSTD_MEMORY_USAGE 17
2781
2782/*!
2783 * HEAPMODE :
2784 * Select how default compression functions will allocate memory for their hash table,
2785 * in memory stack (0, fastest), or in memory heap (1, requires malloc())
2786 * Note that compression context is fairly large, as a consequence heap memory is recommended.
2787 */
2788#ifndef ZSTD_HEAPMODE
2789# define ZSTD_HEAPMODE 1
2790#endif /* ZSTD_HEAPMODE */
2791
2792/*!
2793* LEGACY_SUPPORT :
2794* decompressor can decode older formats (starting from Zstd 0.1+)
2795*/
2796#ifndef ZSTD_LEGACY_SUPPORT
2797# define ZSTD_LEGACY_SUPPORT 1
2798#endif
2799
2800
2801/* *******************************************************
2802* Includes
2803*********************************************************/
2804#include <stdlib.h> /* calloc */
2805#include <string.h> /* memcpy, memmove */
2806#include <stdio.h> /* debug : printf */
2807
2808
2809/* *******************************************************
2810* Compiler specifics
2811*********************************************************/
2812#ifdef __AVX2__
2813# include <immintrin.h> /* AVX2 intrinsics */
2814#endif
2815
2816#ifdef _MSC_VER /* Visual Studio */
2817# define FORCE_INLINE static __forceinline
2818# include <intrin.h> /* For Visual 2005 */
2819# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2820# pragma warning(disable : 4324) /* disable: C4324: padded structure */
2821#else
2822# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2823# ifdef __GNUC__
2824# define FORCE_INLINE static inline __attribute__((always_inline))
2825# else
2826# define FORCE_INLINE static inline
2827# endif
2828#endif
2829
2830
2831/* *******************************************************
2832* Constants
2833*********************************************************/
2834#define HASH_LOG (ZSTD_MEMORY_USAGE - 2)
2835#define HASH_TABLESIZE (1 << HASH_LOG)
2836#define HASH_MASK (HASH_TABLESIZE - 1)
2837
2838#define KNUTH 2654435761
2839
2840#define BIT7 128
2841#define BIT6 64
2842#define BIT5 32
2843#define BIT4 16
2844#define BIT1 2
2845#define BIT0 1
2846
2847#define KB *(1 <<10)
2848#define MB *(1 <<20)
2849#define GB *(1U<<30)
2850
2851#define BLOCKSIZE (128 KB) /* define, for static allocation */
2852#define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/)
2853#define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE)
2854#define IS_RAW BIT0
2855#define IS_RLE BIT1
2856
2857#define WORKPLACESIZE (BLOCKSIZE*3)
2858#define MINMATCH 4
2859#define MLbits 7
2860#define LLbits 6
2861#define Offbits 5
2862#define MaxML ((1<<MLbits )-1)
2863#define MaxLL ((1<<LLbits )-1)
2864#define MaxOff 31
2865#define LitFSELog 11
2866#define MLFSELog 10
2867#define LLFSELog 10
2868#define OffFSELog 9
2869#define MAX(a,b) ((a)<(b)?(b):(a))
2870#define MaxSeq MAX(MaxLL, MaxML)
2871
2872#define LITERAL_NOENTROPY 63
2873#define COMMAND_NOENTROPY 7 /* to remove */
2874
2875static const size_t ZSTD_blockHeaderSize = 3;
2876static const size_t ZSTD_frameHeaderSize = 4;
2877
2878
2879/* *******************************************************
2880* Memory operations
2881**********************************************************/
2882static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2883
2884static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
2885
2886#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
2887
2888/*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */
2889static void ZSTD_wildcopy(void* dst, const void* src, size_t length)
2890{
2891 const BYTE* ip = (const BYTE*)src;
2892 BYTE* op = (BYTE*)dst;
2893 BYTE* const oend = op + length;
2894 do COPY8(op, ip) while (op < oend);
2895}
2896
2897
2898/* **************************************
2899* Local structures
2900****************************************/
2901typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
2902
2903typedef struct
2904{
2905 blockType_t blockType;
2906 U32 origSize;
2907} blockProperties_t;
2908
2909typedef struct {
2910 void* buffer;
2911 U32* offsetStart;
2912 U32* offset;
2913 BYTE* offCodeStart;
2914 BYTE* offCode;
2915 BYTE* litStart;
2916 BYTE* lit;
2917 BYTE* litLengthStart;
2918 BYTE* litLength;
2919 BYTE* matchLengthStart;
2920 BYTE* matchLength;
2921 BYTE* dumpsStart;
2922 BYTE* dumps;
2923} seqStore_t;
2924
2925
2926/* *************************************
2927* Error Management
2928***************************************/
2929/*! ZSTD_isError
2930* tells if a return value is an error code */
2931static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
2932
2933
Yann Colletaa074052015-10-30 11:21:50 +01002934
2935/* *************************************************************
2936* Decompression section
2937***************************************************************/
2938struct ZSTD_DCtx_s
2939{
2940 U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];
2941 U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];
2942 U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];
2943 void* previousDstEnd;
2944 void* base;
2945 size_t expected;
2946 blockType_t bType;
2947 U32 phase;
2948 const BYTE* litPtr;
2949 size_t litBufSize;
2950 size_t litSize;
2951 BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */];
2952}; /* typedef'd to ZSTD_Dctx within "zstd_static.h" */
2953
2954
2955static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2956{
2957 const BYTE* const in = (const BYTE* const)src;
2958 BYTE headerFlags;
2959 U32 cSize;
2960
2961 if (srcSize < 3) return ERROR(srcSize_wrong);
2962
2963 headerFlags = *in;
2964 cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
2965
2966 bpPtr->blockType = (blockType_t)(headerFlags >> 6);
2967 bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
2968
2969 if (bpPtr->blockType == bt_end) return 0;
2970 if (bpPtr->blockType == bt_rle) return 1;
2971 return cSize;
2972}
2973
2974static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2975{
2976 if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2977 memcpy(dst, src, srcSize);
2978 return srcSize;
2979}
2980
2981
2982/** ZSTD_decompressLiterals
2983 @return : nb of bytes read from src, or an error code*/
2984static size_t ZSTD_decompressLiterals(void* dst, size_t* maxDstSizePtr,
2985 const void* src, size_t srcSize)
2986{
2987 const BYTE* ip = (const BYTE*)src;
2988
2989 const size_t litSize = (MEM_readLE32(src) & 0x1FFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2990 const size_t litCSize = (MEM_readLE32(ip+2) & 0xFFFFFF) >> 5; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2991
2992 if (litSize > *maxDstSizePtr) return ERROR(corruption_detected);
2993 if (litCSize + 5 > srcSize) return ERROR(corruption_detected);
2994
2995 if (HUF_isError(HUF_decompress(dst, litSize, ip+5, litCSize))) return ERROR(corruption_detected);
2996
2997 *maxDstSizePtr = litSize;
2998 return litCSize + 5;
2999}
3000
3001
3002/** ZSTD_decodeLiteralsBlock
3003 @return : nb of bytes read from src (< srcSize )*/
3004static size_t ZSTD_decodeLiteralsBlock(void* ctx,
3005 const void* src, size_t srcSize)
3006{
3007 ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
3008 const BYTE* const istart = (const BYTE* const)src;
3009
3010 /* any compressed block with literals segment must be at least this size */
3011 if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
3012
3013 switch(*istart & 3)
3014 {
3015 default:
3016 case 0:
3017 {
3018 size_t litSize = BLOCKSIZE;
3019 const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize);
3020 dctx->litPtr = dctx->litBuffer;
3021 dctx->litBufSize = BLOCKSIZE;
3022 dctx->litSize = litSize;
3023 return readSize; /* works if it's an error too */
3024 }
3025 case IS_RAW:
3026 {
3027 const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
3028 if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
3029 {
3030 if (litSize > srcSize-3) return ERROR(corruption_detected);
3031 memcpy(dctx->litBuffer, istart, litSize);
Yann Colletc2ce8902015-11-26 14:12:23 +01003032 dctx->litPtr = dctx->litBuffer;
Yann Colletaa074052015-10-30 11:21:50 +01003033 dctx->litBufSize = BLOCKSIZE;
3034 dctx->litSize = litSize;
3035 return litSize+3;
3036 }
3037 /* direct reference into compressed stream */
3038 dctx->litPtr = istart+3;
3039 dctx->litBufSize = srcSize-3;
3040 dctx->litSize = litSize;
3041 return litSize+3;
3042 }
3043 case IS_RLE:
3044 {
3045 const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
3046 if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
3047 memset(dctx->litBuffer, istart[3], litSize);
3048 dctx->litPtr = dctx->litBuffer;
3049 dctx->litBufSize = BLOCKSIZE;
3050 dctx->litSize = litSize;
3051 return 4;
3052 }
3053 }
3054}
3055
3056
3057static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
3058 FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb,
3059 const void* src, size_t srcSize)
3060{
3061 const BYTE* const istart = (const BYTE* const)src;
3062 const BYTE* ip = istart;
3063 const BYTE* const iend = istart + srcSize;
3064 U32 LLtype, Offtype, MLtype;
3065 U32 LLlog, Offlog, MLlog;
3066 size_t dumpsLength;
3067
3068 /* check */
3069 if (srcSize < 5) return ERROR(srcSize_wrong);
3070
3071 /* SeqHead */
3072 *nbSeq = MEM_readLE16(ip); ip+=2;
3073 LLtype = *ip >> 6;
3074 Offtype = (*ip >> 4) & 3;
3075 MLtype = (*ip >> 2) & 3;
3076 if (*ip & 2)
3077 {
3078 dumpsLength = ip[2];
3079 dumpsLength += ip[1] << 8;
3080 ip += 3;
3081 }
3082 else
3083 {
3084 dumpsLength = ip[1];
3085 dumpsLength += (ip[0] & 1) << 8;
3086 ip += 2;
3087 }
3088 *dumpsPtr = ip;
3089 ip += dumpsLength;
3090 *dumpsLengthPtr = dumpsLength;
3091
3092 /* check */
3093 if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
3094
3095 /* sequences */
3096 {
3097 S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */
3098 size_t headerSize;
3099
3100 /* Build DTables */
3101 switch(LLtype)
3102 {
Yann Colletaa074052015-10-30 11:21:50 +01003103 case bt_rle :
3104 LLlog = 0;
3105 FSE_buildDTable_rle(DTableLL, *ip++); break;
3106 case bt_raw :
3107 LLlog = LLbits;
3108 FSE_buildDTable_raw(DTableLL, LLbits); break;
3109 default :
Yann Collet87c18b22016-08-26 01:43:47 +02003110 { U32 max = MaxLL;
3111 headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
3112 if (FSE_isError(headerSize)) return ERROR(GENERIC);
3113 if (LLlog > LLFSELog) return ERROR(corruption_detected);
3114 ip += headerSize;
3115 FSE_buildDTable(DTableLL, norm, max, LLlog);
3116 } }
Yann Colletaa074052015-10-30 11:21:50 +01003117
3118 switch(Offtype)
3119 {
Yann Colletaa074052015-10-30 11:21:50 +01003120 case bt_rle :
3121 Offlog = 0;
3122 if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
3123 FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */
3124 break;
3125 case bt_raw :
3126 Offlog = Offbits;
3127 FSE_buildDTable_raw(DTableOffb, Offbits); break;
3128 default :
Yann Collet87c18b22016-08-26 01:43:47 +02003129 { U32 max = MaxOff;
3130 headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
3131 if (FSE_isError(headerSize)) return ERROR(GENERIC);
3132 if (Offlog > OffFSELog) return ERROR(corruption_detected);
3133 ip += headerSize;
3134 FSE_buildDTable(DTableOffb, norm, max, Offlog);
3135 } }
Yann Colletaa074052015-10-30 11:21:50 +01003136
3137 switch(MLtype)
3138 {
Yann Colletaa074052015-10-30 11:21:50 +01003139 case bt_rle :
3140 MLlog = 0;
3141 if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
3142 FSE_buildDTable_rle(DTableML, *ip++); break;
3143 case bt_raw :
3144 MLlog = MLbits;
3145 FSE_buildDTable_raw(DTableML, MLbits); break;
3146 default :
Yann Collet87c18b22016-08-26 01:43:47 +02003147 { U32 max = MaxML;
3148 headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
3149 if (FSE_isError(headerSize)) return ERROR(GENERIC);
3150 if (MLlog > MLFSELog) return ERROR(corruption_detected);
3151 ip += headerSize;
3152 FSE_buildDTable(DTableML, norm, max, MLlog);
3153 } } }
Yann Colletaa074052015-10-30 11:21:50 +01003154
3155 return ip-istart;
3156}
3157
3158
3159typedef struct {
3160 size_t litLength;
3161 size_t offset;
3162 size_t matchLength;
3163} seq_t;
3164
3165typedef struct {
3166 BIT_DStream_t DStream;
3167 FSE_DState_t stateLL;
3168 FSE_DState_t stateOffb;
3169 FSE_DState_t stateML;
3170 size_t prevOffset;
3171 const BYTE* dumps;
3172 const BYTE* dumpsEnd;
3173} seqState_t;
3174
3175
3176static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3177{
3178 size_t litLength;
3179 size_t prevOffset;
3180 size_t offset;
3181 size_t matchLength;
3182 const BYTE* dumps = seqState->dumps;
3183 const BYTE* const de = seqState->dumpsEnd;
3184
3185 /* Literal length */
3186 litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
3187 prevOffset = litLength ? seq->offset : seqState->prevOffset;
3188 seqState->prevOffset = seq->offset;
3189 if (litLength == MaxLL)
3190 {
3191 U32 add = *dumps++;
3192 if (add < 255) litLength += add;
3193 else
3194 {
3195 litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3196 dumps += 3;
3197 }
3198 if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
3199 }
3200
3201 /* Offset */
3202 {
3203 static const size_t offsetPrefix[MaxOff+1] = { /* note : size_t faster than U32 */
3204 1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
3205 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
3206 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
3207 U32 offsetCode, nbBits;
3208 offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* <= maxOff, by table construction */
3209 if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
3210 nbBits = offsetCode - 1;
3211 if (offsetCode==0) nbBits = 0; /* cmove */
3212 offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits);
3213 if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream));
3214 if (offsetCode==0) offset = prevOffset; /* cmove */
3215 }
3216
3217 /* MatchLength */
3218 matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3219 if (matchLength == MaxML)
3220 {
3221 U32 add = *dumps++;
3222 if (add < 255) matchLength += add;
3223 else
3224 {
3225 matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3226 dumps += 3;
3227 }
3228 if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
3229 }
3230 matchLength += MINMATCH;
3231
3232 /* save result */
3233 seq->litLength = litLength;
3234 seq->offset = offset;
3235 seq->matchLength = matchLength;
3236 seqState->dumps = dumps;
3237}
3238
3239
3240static size_t ZSTD_execSequence(BYTE* op,
3241 seq_t sequence,
3242 const BYTE** litPtr, const BYTE* const litLimit,
3243 BYTE* const base, BYTE* const oend)
3244{
3245 static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */
3246 static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* substracted */
3247 const BYTE* const ostart = op;
3248 BYTE* const oLitEnd = op + sequence.litLength;
3249 BYTE* const oMatchEnd = op + sequence.litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */
3250 BYTE* const oend_8 = oend-8;
3251 const BYTE* const litEnd = *litPtr + sequence.litLength;
3252
3253 /* checks */
3254 if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
3255 if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3256 if (litEnd > litLimit-8) return ERROR(corruption_detected); /* overRead beyond lit buffer */
3257
3258 /* copy Literals */
3259 ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
3260 op = oLitEnd;
3261 *litPtr = litEnd; /* update for next sequence */
3262
3263 /* copy Match */
3264 {
3265 const BYTE* match = op - sequence.offset;
3266
3267 /* check */
3268 if (sequence.offset > (size_t)op) return ERROR(corruption_detected); /* address space overflow test (this test seems kept by clang optimizer) */
3269 //if (match > op) return ERROR(corruption_detected); /* address space overflow test (is clang optimizer removing this test ?) */
3270 if (match < base) return ERROR(corruption_detected);
3271
3272 /* close range match, overlap */
3273 if (sequence.offset < 8)
3274 {
3275 const int dec64 = dec64table[sequence.offset];
3276 op[0] = match[0];
3277 op[1] = match[1];
3278 op[2] = match[2];
3279 op[3] = match[3];
3280 match += dec32table[sequence.offset];
3281 ZSTD_copy4(op+4, match);
3282 match -= dec64;
3283 }
3284 else
3285 {
3286 ZSTD_copy8(op, match);
3287 }
3288 op += 8; match += 8;
3289
3290 if (oMatchEnd > oend-12)
3291 {
3292 if (op < oend_8)
3293 {
3294 ZSTD_wildcopy(op, match, oend_8 - op);
3295 match += oend_8 - op;
3296 op = oend_8;
3297 }
3298 while (op < oMatchEnd) *op++ = *match++;
3299 }
3300 else
3301 {
3302 ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
3303 }
3304 }
3305
3306 return oMatchEnd - ostart;
3307}
3308
3309static size_t ZSTD_decompressSequences(
3310 void* ctx,
3311 void* dst, size_t maxDstSize,
3312 const void* seqStart, size_t seqSize)
3313{
3314 ZSTD_DCtx* dctx = (ZSTD_DCtx*)ctx;
3315 const BYTE* ip = (const BYTE*)seqStart;
3316 const BYTE* const iend = ip + seqSize;
3317 BYTE* const ostart = (BYTE* const)dst;
3318 BYTE* op = ostart;
3319 BYTE* const oend = ostart + maxDstSize;
3320 size_t errorCode, dumpsLength;
3321 const BYTE* litPtr = dctx->litPtr;
3322 const BYTE* const litMax = litPtr + dctx->litBufSize;
3323 const BYTE* const litEnd = litPtr + dctx->litSize;
3324 int nbSeq;
3325 const BYTE* dumps;
3326 U32* DTableLL = dctx->LLTable;
3327 U32* DTableML = dctx->MLTable;
3328 U32* DTableOffb = dctx->OffTable;
3329 BYTE* const base = (BYTE*) (dctx->base);
3330
3331 /* Build Decoding Tables */
3332 errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
3333 DTableLL, DTableML, DTableOffb,
3334 ip, iend-ip);
3335 if (ZSTD_isError(errorCode)) return errorCode;
3336 ip += errorCode;
3337
3338 /* Regen sequences */
3339 {
3340 seq_t sequence;
3341 seqState_t seqState;
3342
3343 memset(&sequence, 0, sizeof(sequence));
3344 seqState.dumps = dumps;
3345 seqState.dumpsEnd = dumps + dumpsLength;
3346 seqState.prevOffset = 1;
3347 errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip);
3348 if (ERR_isError(errorCode)) return ERROR(corruption_detected);
3349 FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
3350 FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
3351 FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
3352
3353 for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (nbSeq>0) ; )
3354 {
3355 size_t oneSeqSize;
3356 nbSeq--;
3357 ZSTD_decodeSequence(&sequence, &seqState);
3358 oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litMax, base, oend);
3359 if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
3360 op += oneSeqSize;
3361 }
3362
3363 /* check if reached exact end */
3364 if ( !BIT_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* requested too much : data is corrupted */
3365 if (nbSeq<0) return ERROR(corruption_detected); /* requested too many sequences : data is corrupted */
3366
3367 /* last literal segment */
3368 {
3369 size_t lastLLSize = litEnd - litPtr;
3370 if (litPtr > litEnd) return ERROR(corruption_detected);
3371 if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3372 if (op != litPtr) memmove(op, litPtr, lastLLSize);
3373 op += lastLLSize;
3374 }
3375 }
3376
3377 return op-ostart;
3378}
3379
3380
3381static size_t ZSTD_decompressBlock(
3382 void* ctx,
3383 void* dst, size_t maxDstSize,
3384 const void* src, size_t srcSize)
3385{
3386 /* blockType == blockCompressed */
3387 const BYTE* ip = (const BYTE*)src;
3388
3389 /* Decode literals sub-block */
3390 size_t litCSize = ZSTD_decodeLiteralsBlock(ctx, src, srcSize);
3391 if (ZSTD_isError(litCSize)) return litCSize;
3392 ip += litCSize;
3393 srcSize -= litCSize;
3394
3395 return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize);
3396}
3397
3398
3399static size_t ZSTD_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3400{
3401 const BYTE* ip = (const BYTE*)src;
3402 const BYTE* iend = ip + srcSize;
3403 BYTE* const ostart = (BYTE* const)dst;
3404 BYTE* op = ostart;
3405 BYTE* const oend = ostart + maxDstSize;
3406 size_t remainingSize = srcSize;
3407 U32 magicNumber;
3408 blockProperties_t blockProperties;
3409
3410 /* Frame Header */
3411 if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);
3412 magicNumber = MEM_readLE32(src);
3413 if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3414 ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;
3415
3416 /* Loop on each block */
3417 while (1)
3418 {
3419 size_t decodedSize=0;
3420 size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties);
3421 if (ZSTD_isError(cBlockSize)) return cBlockSize;
3422
3423 ip += ZSTD_blockHeaderSize;
3424 remainingSize -= ZSTD_blockHeaderSize;
3425 if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3426
3427 switch(blockProperties.blockType)
3428 {
3429 case bt_compressed:
3430 decodedSize = ZSTD_decompressBlock(ctx, op, oend-op, ip, cBlockSize);
3431 break;
3432 case bt_raw :
3433 decodedSize = ZSTD_copyUncompressedBlock(op, oend-op, ip, cBlockSize);
3434 break;
3435 case bt_rle :
3436 return ERROR(GENERIC); /* not yet supported */
3437 break;
3438 case bt_end :
3439 /* end of frame */
3440 if (remainingSize) return ERROR(srcSize_wrong);
3441 break;
3442 default:
3443 return ERROR(GENERIC); /* impossible */
3444 }
3445 if (cBlockSize == 0) break; /* bt_end */
3446
3447 if (ZSTD_isError(decodedSize)) return decodedSize;
3448 op += decodedSize;
3449 ip += cBlockSize;
3450 remainingSize -= cBlockSize;
3451 }
3452
3453 return op-ostart;
3454}
3455
3456static size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3457{
3458 ZSTD_DCtx ctx;
3459 ctx.base = dst;
3460 return ZSTD_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);
3461}
3462
3463
3464/*******************************
3465* Streaming Decompression API
3466*******************************/
3467
3468static size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx)
3469{
3470 dctx->expected = ZSTD_frameHeaderSize;
3471 dctx->phase = 0;
3472 dctx->previousDstEnd = NULL;
3473 dctx->base = NULL;
3474 return 0;
3475}
3476
3477static ZSTD_DCtx* ZSTD_createDCtx(void)
3478{
3479 ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx));
3480 if (dctx==NULL) return NULL;
3481 ZSTD_resetDCtx(dctx);
3482 return dctx;
3483}
3484
3485static size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
3486{
3487 free(dctx);
3488 return 0;
3489}
3490
3491static size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx)
3492{
3493 return dctx->expected;
3494}
3495
3496static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3497{
3498 /* Sanity check */
3499 if (srcSize != ctx->expected) return ERROR(srcSize_wrong);
3500 if (dst != ctx->previousDstEnd) /* not contiguous */
3501 ctx->base = dst;
3502
3503 /* Decompress : frame header */
3504 if (ctx->phase == 0)
3505 {
3506 /* Check frame magic header */
3507 U32 magicNumber = MEM_readLE32(src);
3508 if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);
3509 ctx->phase = 1;
3510 ctx->expected = ZSTD_blockHeaderSize;
3511 return 0;
3512 }
3513
3514 /* Decompress : block header */
3515 if (ctx->phase == 1)
3516 {
3517 blockProperties_t bp;
3518 size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);
3519 if (ZSTD_isError(blockSize)) return blockSize;
3520 if (bp.blockType == bt_end)
3521 {
3522 ctx->expected = 0;
3523 ctx->phase = 0;
3524 }
3525 else
3526 {
3527 ctx->expected = blockSize;
3528 ctx->bType = bp.blockType;
3529 ctx->phase = 2;
3530 }
3531
3532 return 0;
3533 }
3534
3535 /* Decompress : block content */
3536 {
3537 size_t rSize;
3538 switch(ctx->bType)
3539 {
3540 case bt_compressed:
3541 rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize);
3542 break;
3543 case bt_raw :
3544 rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize);
3545 break;
3546 case bt_rle :
3547 return ERROR(GENERIC); /* not yet handled */
3548 break;
3549 case bt_end : /* should never happen (filtered at phase 1) */
3550 rSize = 0;
3551 break;
3552 default:
3553 return ERROR(GENERIC);
3554 }
3555 ctx->phase = 1;
3556 ctx->expected = ZSTD_blockHeaderSize;
3557 ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);
3558 return rSize;
3559 }
3560
3561}
3562
3563
3564/* wrapper layer */
3565
3566unsigned ZSTDv02_isError(size_t code)
3567{
3568 return ZSTD_isError(code);
3569}
3570
3571size_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,
3572 const void* src, size_t compressedSize)
3573{
3574 return ZSTD_decompress(dst, maxOriginalSize, src, compressedSize);
3575}
3576
3577ZSTDv02_Dctx* ZSTDv02_createDCtx(void)
3578{
Yann Colletec43ba42015-10-30 11:51:26 +01003579 return (ZSTDv02_Dctx*)ZSTD_createDCtx();
Yann Colletaa074052015-10-30 11:21:50 +01003580}
3581
3582size_t ZSTDv02_freeDCtx(ZSTDv02_Dctx* dctx)
3583{
3584 return ZSTD_freeDCtx((ZSTD_DCtx*)dctx);
3585}
3586
3587size_t ZSTDv02_resetDCtx(ZSTDv02_Dctx* dctx)
3588{
3589 return ZSTD_resetDCtx((ZSTD_DCtx*)dctx);
3590}
3591
3592size_t ZSTDv02_nextSrcSizeToDecompress(ZSTDv02_Dctx* dctx)
3593{
3594 return ZSTD_nextSrcSizeToDecompress((ZSTD_DCtx*)dctx);
3595}
3596
3597size_t ZSTDv02_decompressContinue(ZSTDv02_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3598{
3599 return ZSTD_decompressContinue((ZSTD_DCtx*)dctx, dst, maxDstSize, src, srcSize);
3600}