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