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