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