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