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