2013-12-30 17:16:52 +00:00
|
|
|
/*
|
|
|
|
LZ4 - Fast LZ compression algorithm
|
2014-12-10 08:05:44 +00:00
|
|
|
Copyright (C) 2011-2015, Yann Collet.
|
2015-03-15 13:19:47 +00:00
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
You can contact the author at :
|
2015-03-13 01:24:08 +00:00
|
|
|
- LZ4 source repository : https://github.com/Cyan4973/lz4
|
2013-12-30 17:16:52 +00:00
|
|
|
- LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
|
|
|
|
*/
|
|
|
|
|
2014-11-22 23:46:15 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
|
|
|
Tuning parameters
|
|
|
|
**************************************/
|
|
|
|
/*
|
|
|
|
* HEAPMODE :
|
|
|
|
* Select how default compression functions will allocate memory for their hash table,
|
2014-11-22 23:46:15 +00:00
|
|
|
* in memory stack (0:default, fastest), or in memory heap (1:requires malloc()).
|
2014-02-04 14:11:10 +00:00
|
|
|
*/
|
2013-12-30 17:16:52 +00:00
|
|
|
#define HEAPMODE 0
|
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
/*
|
|
|
|
* ACCELERATION_DEFAULT :
|
|
|
|
* Select the value of "acceleration" for LZ4_compress_fast() when parameter == 0
|
|
|
|
*/
|
|
|
|
#define ACCELERATION_DEFAULT 17
|
|
|
|
|
2014-11-22 23:46:15 +00:00
|
|
|
/*
|
|
|
|
* CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS :
|
2014-11-29 15:41:28 +00:00
|
|
|
* By default, the source code expects the compiler to correctly optimize
|
|
|
|
* 4-bytes and 8-bytes read on architectures able to handle it efficiently.
|
|
|
|
* This is not always the case. In some circumstances (ARM notably),
|
|
|
|
* the compiler will issue cautious code even when target is able to correctly handle unaligned memory accesses.
|
|
|
|
*
|
|
|
|
* You can force the compiler to use unaligned memory access by uncommenting the line below.
|
|
|
|
* One of the below scenarios will happen :
|
|
|
|
* 1 - Your target CPU correctly handle unaligned access, and was not well optimized by compiler (good case).
|
|
|
|
* You will witness large performance improvements (+50% and up).
|
|
|
|
* Keep the line uncommented and send a word to upstream (https://groups.google.com/forum/#!forum/lz4c)
|
|
|
|
* The goal is to automatically detect such situations by adding your target CPU within an exception list.
|
2014-11-29 16:44:33 +00:00
|
|
|
* 2 - Your target CPU correctly handle unaligned access, and was already already optimized by compiler
|
2014-11-29 15:41:28 +00:00
|
|
|
* No change will be experienced.
|
|
|
|
* 3 - Your target CPU inefficiently handle unaligned access.
|
|
|
|
* You will experience a performance loss. Comment back the line.
|
|
|
|
* 4 - Your target CPU does not handle unaligned access.
|
|
|
|
* Program will crash.
|
2014-11-29 16:44:33 +00:00
|
|
|
* If uncommenting results in better performance (case 1)
|
2014-11-23 17:36:04 +00:00
|
|
|
* please report your configuration to upstream (https://groups.google.com/forum/#!forum/lz4c)
|
2015-03-07 18:30:45 +00:00
|
|
|
* This way, an automatic detection macro can be added to match your case within later versions of the library.
|
2014-11-22 23:46:15 +00:00
|
|
|
*/
|
|
|
|
/* #define CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS 1 */
|
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
|
|
|
CPU Feature Detection
|
|
|
|
**************************************/
|
|
|
|
/*
|
2014-11-27 21:44:36 +00:00
|
|
|
* Automated efficient unaligned memory access detection
|
|
|
|
* Based on known hardware architectures
|
2014-11-29 15:41:28 +00:00
|
|
|
* This list will be updated thanks to feedbacks
|
2014-02-04 14:11:10 +00:00
|
|
|
*/
|
2014-11-22 23:46:15 +00:00
|
|
|
#if defined(CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS) \
|
|
|
|
|| defined(__ARM_FEATURE_UNALIGNED) \
|
|
|
|
|| defined(__i386__) || defined(__x86_64__) \
|
2014-11-23 00:14:04 +00:00
|
|
|
|| defined(_M_IX86) || defined(_M_X64) \
|
|
|
|
|| defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) \
|
|
|
|
|| (defined(_M_ARM) && (_M_ARM >= 7))
|
2014-11-22 23:46:15 +00:00
|
|
|
# define LZ4_UNALIGNED_ACCESS 1
|
2013-12-30 17:16:52 +00:00
|
|
|
#else
|
2014-11-22 23:46:15 +00:00
|
|
|
# define LZ4_UNALIGNED_ACCESS 0
|
2013-12-30 17:16:52 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-29 15:41:28 +00:00
|
|
|
/*
|
|
|
|
* LZ4_FORCE_SW_BITCOUNT
|
|
|
|
* Define this parameter if your target system or compiler does not support hardware bit count
|
|
|
|
*/
|
2014-02-04 14:11:10 +00:00
|
|
|
#if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for Windows CE does not support Hardware bit count */
|
2013-12-30 17:16:52 +00:00
|
|
|
# define LZ4_FORCE_SW_BITCOUNT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
2015-03-07 18:30:45 +00:00
|
|
|
* Compiler Options
|
2014-02-04 14:11:10 +00:00
|
|
|
**************************************/
|
|
|
|
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
|
2013-12-30 17:16:52 +00:00
|
|
|
/* "restrict" is a known keyword */
|
|
|
|
#else
|
2014-02-04 14:11:10 +00:00
|
|
|
# define restrict /* Disable restrict */
|
2013-12-30 17:16:52 +00:00
|
|
|
#endif
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
#ifdef _MSC_VER /* Visual Studio */
|
2014-06-12 22:29:06 +00:00
|
|
|
# define FORCE_INLINE static __forceinline
|
2014-11-29 15:41:28 +00:00
|
|
|
# include <intrin.h>
|
2014-02-04 14:11:10 +00:00
|
|
|
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
|
2014-12-10 08:05:44 +00:00
|
|
|
# pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */
|
2014-06-12 22:29:06 +00:00
|
|
|
#else
|
2014-12-16 21:03:16 +00:00
|
|
|
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
|
|
|
|
# ifdef __GNUC__
|
|
|
|
# define FORCE_INLINE static inline __attribute__((always_inline))
|
|
|
|
# else
|
|
|
|
# define FORCE_INLINE static inline
|
|
|
|
# endif
|
2014-06-12 22:29:06 +00:00
|
|
|
# else
|
2014-12-16 21:03:16 +00:00
|
|
|
# define FORCE_INLINE static
|
|
|
|
# endif /* __STDC_VERSION__ */
|
|
|
|
#endif /* _MSC_VER */
|
2014-06-12 22:29:06 +00:00
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
|
|
|
|
|
|
|
#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
|
|
|
|
# define expect(expr,value) (__builtin_expect ((expr),(value)) )
|
|
|
|
#else
|
|
|
|
# define expect(expr,value) (expr)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define likely(expr) expect((expr) != 0, 1)
|
|
|
|
#define unlikely(expr) expect((expr) != 0, 0)
|
|
|
|
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
|
|
|
Memory routines
|
|
|
|
**************************************/
|
|
|
|
#include <stdlib.h> /* malloc, calloc, free */
|
2013-12-30 17:16:52 +00:00
|
|
|
#define ALLOCATOR(n,s) calloc(n,s)
|
|
|
|
#define FREEMEM free
|
2014-02-04 14:11:10 +00:00
|
|
|
#include <string.h> /* memset, memcpy */
|
2013-12-30 17:16:52 +00:00
|
|
|
#define MEM_INIT memset
|
|
|
|
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
|
|
|
Includes
|
|
|
|
**************************************/
|
2013-12-30 17:16:52 +00:00
|
|
|
#include "lz4.h"
|
|
|
|
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
|
|
|
Basic Types
|
|
|
|
**************************************/
|
|
|
|
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */
|
2013-12-30 17:16:52 +00:00
|
|
|
# include <stdint.h>
|
|
|
|
typedef uint8_t BYTE;
|
|
|
|
typedef uint16_t U16;
|
|
|
|
typedef uint32_t U32;
|
|
|
|
typedef int32_t S32;
|
|
|
|
typedef uint64_t U64;
|
|
|
|
#else
|
|
|
|
typedef unsigned char BYTE;
|
|
|
|
typedef unsigned short U16;
|
|
|
|
typedef unsigned int U32;
|
|
|
|
typedef signed int S32;
|
|
|
|
typedef unsigned long long U64;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-11-22 23:46:15 +00:00
|
|
|
/**************************************
|
|
|
|
Reading and writing into memory
|
|
|
|
**************************************/
|
2014-11-27 21:44:36 +00:00
|
|
|
#define STEPSIZE sizeof(size_t)
|
|
|
|
|
2014-11-22 23:46:15 +00:00
|
|
|
static unsigned LZ4_64bits(void) { return sizeof(void*)==8; }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-11-22 23:46:15 +00:00
|
|
|
static unsigned LZ4_isLittleEndian(void)
|
|
|
|
{
|
|
|
|
const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
|
|
|
|
return one.c[0];
|
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-11-29 16:44:33 +00:00
|
|
|
|
2014-11-22 23:46:15 +00:00
|
|
|
static U16 LZ4_readLE16(const void* memPtr)
|
|
|
|
{
|
|
|
|
if ((LZ4_UNALIGNED_ACCESS) && (LZ4_isLittleEndian()))
|
|
|
|
return *(U16*)memPtr;
|
2014-11-23 00:14:04 +00:00
|
|
|
else
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2015-03-10 15:57:42 +00:00
|
|
|
const BYTE* p = (const BYTE*)memPtr;
|
2014-11-22 23:46:15 +00:00
|
|
|
return (U16)((U16)p[0] + (p[1]<<8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LZ4_writeLE16(void* memPtr, U16 value)
|
|
|
|
{
|
|
|
|
if ((LZ4_UNALIGNED_ACCESS) && (LZ4_isLittleEndian()))
|
|
|
|
{
|
|
|
|
*(U16*)memPtr = value;
|
|
|
|
return;
|
|
|
|
}
|
2014-11-23 00:14:04 +00:00
|
|
|
else
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2015-03-10 15:57:42 +00:00
|
|
|
BYTE* p = (BYTE*)memPtr;
|
2014-11-22 23:46:15 +00:00
|
|
|
p[0] = (BYTE) value;
|
|
|
|
p[1] = (BYTE)(value>>8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 21:44:36 +00:00
|
|
|
|
2014-11-29 16:44:33 +00:00
|
|
|
static U16 LZ4_read16(const void* memPtr)
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2014-11-27 21:44:36 +00:00
|
|
|
if (LZ4_UNALIGNED_ACCESS)
|
|
|
|
return *(U16*)memPtr;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
U16 val16;
|
|
|
|
memcpy(&val16, memPtr, 2);
|
|
|
|
return val16;
|
2014-11-22 23:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 21:44:36 +00:00
|
|
|
static U32 LZ4_read32(const void* memPtr)
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
|
|
|
if (LZ4_UNALIGNED_ACCESS)
|
2014-11-27 21:44:36 +00:00
|
|
|
return *(U32*)memPtr;
|
2014-11-23 00:14:04 +00:00
|
|
|
else
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2014-11-27 21:44:36 +00:00
|
|
|
U32 val32;
|
|
|
|
memcpy(&val32, memPtr, 4);
|
|
|
|
return val32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 16:44:33 +00:00
|
|
|
static U64 LZ4_read64(const void* memPtr)
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2014-11-29 16:44:33 +00:00
|
|
|
if (LZ4_UNALIGNED_ACCESS)
|
2014-11-22 23:46:15 +00:00
|
|
|
return *(U64*)memPtr;
|
2014-11-23 00:14:04 +00:00
|
|
|
else
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2014-11-29 16:44:33 +00:00
|
|
|
U64 val64;
|
|
|
|
memcpy(&val64, memPtr, 8);
|
|
|
|
return val64;
|
2014-11-22 23:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 16:44:33 +00:00
|
|
|
static size_t LZ4_read_ARCH(const void* p)
|
2014-11-27 21:44:36 +00:00
|
|
|
{
|
|
|
|
if (LZ4_64bits())
|
2014-11-29 16:44:33 +00:00
|
|
|
return (size_t)LZ4_read64(p);
|
2014-11-27 21:44:36 +00:00
|
|
|
else
|
2014-11-29 16:44:33 +00:00
|
|
|
return (size_t)LZ4_read32(p);
|
2014-11-27 21:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void LZ4_copy4(void* dstPtr, const void* srcPtr)
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2014-11-27 21:44:36 +00:00
|
|
|
if (LZ4_UNALIGNED_ACCESS)
|
|
|
|
{
|
|
|
|
*(U32*)dstPtr = *(U32*)srcPtr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(dstPtr, srcPtr, 4);
|
2014-11-22 23:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void LZ4_copy8(void* dstPtr, const void* srcPtr)
|
|
|
|
{
|
2014-12-16 21:03:16 +00:00
|
|
|
#if GCC_VERSION!=409 /* disabled on GCC 4.9, as it generates invalid opcode (crash) */
|
2014-11-22 23:46:15 +00:00
|
|
|
if (LZ4_UNALIGNED_ACCESS)
|
|
|
|
{
|
|
|
|
if (LZ4_64bits())
|
|
|
|
*(U64*)dstPtr = *(U64*)srcPtr;
|
|
|
|
else
|
|
|
|
((U32*)dstPtr)[0] = ((U32*)srcPtr)[0],
|
|
|
|
((U32*)dstPtr)[1] = ((U32*)srcPtr)[1];
|
|
|
|
return;
|
|
|
|
}
|
2014-12-14 13:29:15 +00:00
|
|
|
#endif
|
2014-11-27 21:44:36 +00:00
|
|
|
memcpy(dstPtr, srcPtr, 8);
|
2014-11-22 23:46:15 +00:00
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-11-27 21:44:36 +00:00
|
|
|
/* customized version of memcpy, which may overwrite up to 7 bytes beyond dstEnd */
|
|
|
|
static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd)
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2015-03-10 15:57:42 +00:00
|
|
|
BYTE* d = (BYTE*)dstPtr;
|
|
|
|
const BYTE* s = (const BYTE*)srcPtr;
|
|
|
|
BYTE* e = (BYTE*)dstEnd;
|
2014-11-27 21:44:36 +00:00
|
|
|
do { LZ4_copy8(d,s); d+=8; s+=8; } while (d<e);
|
2014-11-22 23:46:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
2014-11-29 19:19:39 +00:00
|
|
|
Common Constants
|
2014-02-04 14:11:10 +00:00
|
|
|
**************************************/
|
2013-12-30 17:16:52 +00:00
|
|
|
#define MINMATCH 4
|
|
|
|
|
|
|
|
#define COPYLENGTH 8
|
|
|
|
#define LASTLITERALS 5
|
|
|
|
#define MFLIMIT (COPYLENGTH+MINMATCH)
|
2014-02-04 14:11:10 +00:00
|
|
|
static const int LZ4_minLength = (MFLIMIT+1);
|
|
|
|
|
2014-11-29 19:19:39 +00:00
|
|
|
#define KB *(1 <<10)
|
|
|
|
#define MB *(1 <<20)
|
2014-02-04 14:11:10 +00:00
|
|
|
#define GB *(1U<<30)
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
#define MAXD_LOG 16
|
|
|
|
#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
|
|
|
|
|
|
|
|
#define ML_BITS 4
|
|
|
|
#define ML_MASK ((1U<<ML_BITS)-1)
|
|
|
|
#define RUN_BITS (8-ML_BITS)
|
|
|
|
#define RUN_MASK ((1U<<RUN_BITS)-1)
|
|
|
|
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/**************************************
|
2015-03-07 18:30:45 +00:00
|
|
|
* Common Utils
|
2014-02-04 14:11:10 +00:00
|
|
|
**************************************/
|
2014-11-04 10:07:04 +00:00
|
|
|
#define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
|
2015-03-07 18:30:45 +00:00
|
|
|
/**************************************
|
|
|
|
* Common functions
|
|
|
|
**************************************/
|
2014-11-27 21:44:36 +00:00
|
|
|
static unsigned LZ4_NbCommonBytes (register size_t val)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-11-29 16:44:33 +00:00
|
|
|
if (LZ4_isLittleEndian())
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2014-11-29 16:44:33 +00:00
|
|
|
if (LZ4_64bits())
|
|
|
|
{
|
|
|
|
# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
unsigned long r = 0;
|
|
|
|
_BitScanForward64( &r, (U64)val );
|
|
|
|
return (int)(r>>3);
|
|
|
|
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
return (__builtin_ctzll((U64)val) >> 3);
|
|
|
|
# else
|
|
|
|
static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
|
|
|
|
return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
else /* 32 bits */
|
|
|
|
{
|
|
|
|
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
unsigned long r;
|
|
|
|
_BitScanForward( &r, (U32)val );
|
|
|
|
return (int)(r>>3);
|
|
|
|
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
return (__builtin_ctz((U32)val) >> 3);
|
|
|
|
# else
|
|
|
|
static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };
|
|
|
|
return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
|
|
|
|
# endif
|
|
|
|
}
|
2014-11-22 23:46:15 +00:00
|
|
|
}
|
2014-11-29 16:44:33 +00:00
|
|
|
else /* Big Endian CPU */
|
2014-11-22 23:46:15 +00:00
|
|
|
{
|
2014-11-29 16:44:33 +00:00
|
|
|
if (LZ4_64bits())
|
|
|
|
{
|
|
|
|
# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
unsigned long r = 0;
|
|
|
|
_BitScanReverse64( &r, val );
|
|
|
|
return (unsigned)(r>>3);
|
|
|
|
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
return (__builtin_clzll(val) >> 3);
|
|
|
|
# else
|
|
|
|
unsigned r;
|
|
|
|
if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
|
|
|
|
if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
|
|
|
|
r += (!val);
|
|
|
|
return r;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
else /* 32 bits */
|
|
|
|
{
|
|
|
|
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
unsigned long r = 0;
|
2014-11-30 11:58:00 +00:00
|
|
|
_BitScanReverse( &r, (unsigned long)val );
|
2014-11-29 16:44:33 +00:00
|
|
|
return (unsigned)(r>>3);
|
|
|
|
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
|
|
|
return (__builtin_clz(val) >> 3);
|
|
|
|
# else
|
|
|
|
unsigned r;
|
|
|
|
if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
|
|
|
|
r += (!val);
|
|
|
|
return r;
|
|
|
|
# endif
|
|
|
|
}
|
2014-11-22 23:46:15 +00:00
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 19:19:39 +00:00
|
|
|
static unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit)
|
|
|
|
{
|
|
|
|
const BYTE* const pStart = pIn;
|
|
|
|
|
|
|
|
while (likely(pIn<pInLimit-(STEPSIZE-1)))
|
|
|
|
{
|
|
|
|
size_t diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
|
|
|
|
if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; }
|
|
|
|
pIn += LZ4_NbCommonBytes(diff);
|
|
|
|
return (unsigned)(pIn - pStart);
|
|
|
|
}
|
2014-12-03 18:17:10 +00:00
|
|
|
|
2014-11-29 19:19:39 +00:00
|
|
|
if (LZ4_64bits()) if ((pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; }
|
|
|
|
if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; }
|
|
|
|
if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++;
|
|
|
|
return (unsigned)(pIn - pStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LZ4_COMMONDEFS_ONLY
|
|
|
|
/**************************************
|
2015-03-07 18:30:45 +00:00
|
|
|
* Local Constants
|
2014-11-29 19:19:39 +00:00
|
|
|
**************************************/
|
|
|
|
#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
|
|
|
|
#define HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
|
2014-12-14 13:29:15 +00:00
|
|
|
#define HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
|
2014-11-29 19:19:39 +00:00
|
|
|
|
2014-12-14 13:29:15 +00:00
|
|
|
static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1));
|
|
|
|
static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */
|
2014-11-29 19:19:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2015-03-07 18:30:45 +00:00
|
|
|
* Local Utils
|
2014-11-29 19:19:39 +00:00
|
|
|
**************************************/
|
|
|
|
int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; }
|
|
|
|
int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************
|
2015-03-07 18:30:45 +00:00
|
|
|
* Local Structures and types
|
2014-11-29 19:19:39 +00:00
|
|
|
**************************************/
|
|
|
|
typedef struct {
|
|
|
|
U32 hashTable[HASH_SIZE_U32];
|
|
|
|
U32 currentOffset;
|
|
|
|
U32 initCheck;
|
|
|
|
const BYTE* dictionary;
|
|
|
|
const BYTE* bufferStart;
|
|
|
|
U32 dictSize;
|
|
|
|
} LZ4_stream_t_internal;
|
|
|
|
|
|
|
|
typedef enum { notLimited = 0, limitedOutput = 1 } limitedOutput_directive;
|
|
|
|
typedef enum { byPtr, byU32, byU16 } tableType_t;
|
|
|
|
|
|
|
|
typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
|
|
|
|
typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
|
|
|
|
|
|
|
|
typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
|
|
|
|
typedef enum { full = 0, partial = 1 } earlyEnd_directive;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
2015-03-07 18:30:45 +00:00
|
|
|
* Compression functions
|
2014-11-29 19:19:39 +00:00
|
|
|
********************************/
|
2014-03-12 14:51:59 +00:00
|
|
|
|
2015-03-22 12:42:00 +00:00
|
|
|
static U32 LZ4_hashSequence(U32 sequence, tableType_t const tableType)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
if (tableType == byU16)
|
|
|
|
return (((sequence) * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1)));
|
|
|
|
else
|
|
|
|
return (((sequence) * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG));
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:34:15 +00:00
|
|
|
static const U64 prime5bytes = 889523592379ULL;
|
|
|
|
static U32 LZ4_hashSequence64(size_t sequence, tableType_t const tableType)
|
|
|
|
{
|
|
|
|
const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG;
|
|
|
|
const U32 hashMask = (1<<hashLog) - 1;
|
|
|
|
return ((sequence * prime5bytes) >> (40 - hashLog)) & hashMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static U32 LZ4_hashSequenceT(size_t sequence, tableType_t const tableType)
|
|
|
|
{
|
|
|
|
if (LZ4_64bits())
|
|
|
|
return LZ4_hashSequence64(sequence, tableType);
|
|
|
|
return LZ4_hashSequence((U32)sequence, tableType);
|
|
|
|
}
|
|
|
|
|
|
|
|
static U32 LZ4_hashPosition(const void* p, tableType_t tableType) { return LZ4_hashSequenceT(LZ4_read_ARCH(p), tableType); }
|
|
|
|
|
|
|
|
//static U32 LZ4_hashPosition(const BYTE* p, tableType_t tableType) { return LZ4_hashSequence(LZ4_read32(p), tableType); }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-03-22 12:42:00 +00:00
|
|
|
static void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableType_t const tableType, const BYTE* srcBase)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
switch (tableType)
|
|
|
|
{
|
2014-12-02 22:57:15 +00:00
|
|
|
case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p; return; }
|
2014-11-22 23:46:15 +00:00
|
|
|
case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); return; }
|
|
|
|
case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); return; }
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 23:40:29 +00:00
|
|
|
static void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
U32 h = LZ4_hashPosition(p, tableType);
|
|
|
|
LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
|
|
|
|
}
|
|
|
|
|
2014-05-19 23:40:29 +00:00
|
|
|
static const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
if (tableType == byPtr) { const BYTE** hashTable = (const BYTE**) tableBase; return hashTable[h]; }
|
|
|
|
if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; }
|
2014-02-04 14:11:10 +00:00
|
|
|
{ U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 23:40:29 +00:00
|
|
|
static const BYTE* LZ4_getPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
U32 h = LZ4_hashPosition(p, tableType);
|
|
|
|
return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
|
|
|
|
}
|
|
|
|
|
2014-05-21 18:46:36 +00:00
|
|
|
static int LZ4_compress_generic(
|
2015-03-30 20:32:25 +00:00
|
|
|
void* const ctx,
|
|
|
|
const char* const source,
|
|
|
|
char* const dest,
|
|
|
|
const int inputSize,
|
|
|
|
const int maxOutputSize,
|
|
|
|
const limitedOutput_directive outputLimited,
|
|
|
|
const tableType_t tableType,
|
|
|
|
const dict_directive dict,
|
|
|
|
const dictIssue_directive dictIssue,
|
|
|
|
const U32 acceleration)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_stream_t_internal* const dictPtr = (LZ4_stream_t_internal*)ctx;
|
2014-05-27 01:41:29 +00:00
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
const BYTE* ip = (const BYTE*) source;
|
2014-05-19 23:40:29 +00:00
|
|
|
const BYTE* base;
|
2014-05-27 01:41:29 +00:00
|
|
|
const BYTE* lowLimit;
|
2014-06-21 16:01:15 +00:00
|
|
|
const BYTE* const lowRefLimit = ip - dictPtr->dictSize;
|
2014-05-27 01:41:29 +00:00
|
|
|
const BYTE* const dictionary = dictPtr->dictionary;
|
|
|
|
const BYTE* const dictEnd = dictionary + dictPtr->dictSize;
|
|
|
|
const size_t dictDelta = dictEnd - (const BYTE*)source;
|
2013-12-30 17:16:52 +00:00
|
|
|
const BYTE* anchor = (const BYTE*) source;
|
|
|
|
const BYTE* const iend = ip + inputSize;
|
|
|
|
const BYTE* const mflimit = iend - MFLIMIT;
|
|
|
|
const BYTE* const matchlimit = iend - LASTLITERALS;
|
|
|
|
|
|
|
|
BYTE* op = (BYTE*) dest;
|
2014-06-09 17:08:23 +00:00
|
|
|
BYTE* const olimit = op + maxOutputSize;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
U32 forwardH;
|
2014-06-09 17:08:23 +00:00
|
|
|
size_t refDelta=0;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Init conditions */
|
2015-03-30 20:32:25 +00:00
|
|
|
if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size, too large (or negative) */
|
2014-05-19 23:40:29 +00:00
|
|
|
switch(dict)
|
|
|
|
{
|
|
|
|
case noDict:
|
|
|
|
default:
|
2014-05-27 01:41:29 +00:00
|
|
|
base = (const BYTE*)source;
|
|
|
|
lowLimit = (const BYTE*)source;
|
|
|
|
break;
|
2014-05-19 23:40:29 +00:00
|
|
|
case withPrefix64k:
|
2014-06-02 06:07:19 +00:00
|
|
|
base = (const BYTE*)source - dictPtr->currentOffset;
|
2014-06-10 20:45:25 +00:00
|
|
|
lowLimit = (const BYTE*)source - dictPtr->dictSize;
|
2014-05-19 23:40:29 +00:00
|
|
|
break;
|
2014-06-02 06:07:19 +00:00
|
|
|
case usingExtDict:
|
2014-05-27 01:41:29 +00:00
|
|
|
base = (const BYTE*)source - dictPtr->currentOffset;
|
|
|
|
lowLimit = (const BYTE*)source;
|
|
|
|
break;
|
2014-05-19 23:40:29 +00:00
|
|
|
}
|
2014-12-14 13:29:15 +00:00
|
|
|
if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */
|
2014-12-16 01:13:19 +00:00
|
|
|
if (inputSize<LZ4_minLength) goto _last_literals; /* Input too small, no compression (all literals) */
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* First Byte */
|
2013-12-30 17:16:52 +00:00
|
|
|
LZ4_putPosition(ip, ctx, tableType, base);
|
|
|
|
ip++; forwardH = LZ4_hashPosition(ip, tableType);
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Main Loop */
|
2013-12-30 17:16:52 +00:00
|
|
|
for ( ; ; )
|
|
|
|
{
|
2014-12-02 22:57:15 +00:00
|
|
|
const BYTE* match;
|
2013-12-30 17:16:52 +00:00
|
|
|
BYTE* token;
|
2014-06-10 05:10:08 +00:00
|
|
|
{
|
2014-06-14 15:56:24 +00:00
|
|
|
const BYTE* forwardIp = ip;
|
|
|
|
unsigned step=1;
|
2015-03-30 20:32:25 +00:00
|
|
|
unsigned searchMatchNb = ((1+acceleration) << LZ4_skipTrigger);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-06-10 05:10:08 +00:00
|
|
|
/* Find a match */
|
|
|
|
do {
|
|
|
|
U32 h = forwardH;
|
|
|
|
ip = forwardIp;
|
|
|
|
forwardIp += step;
|
2015-03-30 20:32:25 +00:00
|
|
|
step = (searchMatchNb++ >> LZ4_skipTrigger);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-06-14 15:56:24 +00:00
|
|
|
if (unlikely(forwardIp > mflimit)) goto _last_literals;
|
2014-06-10 05:10:08 +00:00
|
|
|
|
2014-12-02 22:57:15 +00:00
|
|
|
match = LZ4_getPositionOnHash(h, ctx, tableType, base);
|
2014-06-21 16:01:15 +00:00
|
|
|
if (dict==usingExtDict)
|
2014-06-09 17:08:23 +00:00
|
|
|
{
|
2014-12-02 22:57:15 +00:00
|
|
|
if (match<(const BYTE*)source)
|
2014-06-10 05:10:08 +00:00
|
|
|
{
|
|
|
|
refDelta = dictDelta;
|
|
|
|
lowLimit = dictionary;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
refDelta = 0;
|
|
|
|
lowLimit = (const BYTE*)source;
|
|
|
|
}
|
2014-06-09 17:08:23 +00:00
|
|
|
}
|
2014-06-10 05:10:08 +00:00
|
|
|
forwardH = LZ4_hashPosition(forwardIp, tableType);
|
|
|
|
LZ4_putPositionOnHash(ip, h, ctx, tableType, base);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-12-02 22:57:15 +00:00
|
|
|
} while ( ((dictIssue==dictSmall) ? (match < lowRefLimit) : 0)
|
|
|
|
|| ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip))
|
|
|
|
|| (LZ4_read32(match+refDelta) != LZ4_read32(ip)) );
|
2014-06-10 05:10:08 +00:00
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Catch up */
|
2014-12-02 22:57:15 +00:00
|
|
|
while ((ip>anchor) && (match+refDelta > lowLimit) && (unlikely(ip[-1]==match[refDelta-1]))) { ip--; match--; }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
{
|
2014-05-19 23:40:29 +00:00
|
|
|
/* Encode Literal length */
|
2014-05-20 00:53:48 +00:00
|
|
|
unsigned litLength = (unsigned)(ip - anchor);
|
2014-05-19 23:40:29 +00:00
|
|
|
token = op++;
|
2014-06-21 16:01:15 +00:00
|
|
|
if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)))
|
|
|
|
return 0; /* Check output limit */
|
2014-05-19 23:40:29 +00:00
|
|
|
if (litLength>=RUN_MASK)
|
|
|
|
{
|
|
|
|
int len = (int)litLength-RUN_MASK;
|
|
|
|
*token=(RUN_MASK<<ML_BITS);
|
|
|
|
for(; len >= 255 ; len-=255) *op++ = 255;
|
|
|
|
*op++ = (BYTE)len;
|
|
|
|
}
|
|
|
|
else *token = (BYTE)(litLength<<ML_BITS);
|
|
|
|
|
|
|
|
/* Copy Literals */
|
2014-11-27 21:44:36 +00:00
|
|
|
LZ4_wildCopy(op, anchor, op+litLength);
|
|
|
|
op+=litLength;
|
2014-05-19 23:40:29 +00:00
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
_next_match:
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Encode Offset */
|
2014-12-02 22:57:15 +00:00
|
|
|
LZ4_writeLE16(op, (U16)(ip-match)); op+=2;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Encode MatchLength */
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-05-27 01:41:29 +00:00
|
|
|
unsigned matchLength;
|
|
|
|
|
2014-06-02 06:07:19 +00:00
|
|
|
if ((dict==usingExtDict) && (lowLimit==dictionary))
|
2014-05-27 01:41:29 +00:00
|
|
|
{
|
2014-06-14 15:56:24 +00:00
|
|
|
const BYTE* limit;
|
2014-12-02 22:57:15 +00:00
|
|
|
match += refDelta;
|
|
|
|
limit = ip + (dictEnd-match);
|
2014-05-27 01:41:29 +00:00
|
|
|
if (limit > matchlimit) limit = matchlimit;
|
2014-12-02 22:57:15 +00:00
|
|
|
matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, limit);
|
2014-05-27 01:41:29 +00:00
|
|
|
ip += MINMATCH + matchLength;
|
|
|
|
if (ip==limit)
|
|
|
|
{
|
|
|
|
unsigned more = LZ4_count(ip, (const BYTE*)source, matchlimit);
|
|
|
|
matchLength += more;
|
|
|
|
ip += more;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-02 22:57:15 +00:00
|
|
|
matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
|
2014-05-27 01:41:29 +00:00
|
|
|
ip += MINMATCH + matchLength;
|
|
|
|
}
|
|
|
|
|
2014-12-10 16:58:15 +00:00
|
|
|
if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchLength>>8) > olimit)))
|
|
|
|
return 0; /* Check output limit */
|
2014-05-19 23:40:29 +00:00
|
|
|
if (matchLength>=ML_MASK)
|
|
|
|
{
|
|
|
|
*token += ML_MASK;
|
|
|
|
matchLength -= ML_MASK;
|
2014-06-10 05:10:08 +00:00
|
|
|
for (; matchLength >= 510 ; matchLength-=510) { *op++ = 255; *op++ = 255; }
|
2014-05-19 23:40:29 +00:00
|
|
|
if (matchLength >= 255) { matchLength-=255; *op++ = 255; }
|
|
|
|
*op++ = (BYTE)matchLength;
|
|
|
|
}
|
|
|
|
else *token += (BYTE)(matchLength);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
2014-05-19 23:40:29 +00:00
|
|
|
|
|
|
|
anchor = ip;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Test end of chunk */
|
2014-05-19 23:40:29 +00:00
|
|
|
if (ip > mflimit) break;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Fill table */
|
2013-12-30 17:16:52 +00:00
|
|
|
LZ4_putPosition(ip-2, ctx, tableType, base);
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Test next position */
|
2014-12-02 22:57:15 +00:00
|
|
|
match = LZ4_getPosition(ip, ctx, tableType, base);
|
2014-06-21 16:01:15 +00:00
|
|
|
if (dict==usingExtDict)
|
2014-05-27 01:41:29 +00:00
|
|
|
{
|
2014-12-02 22:57:15 +00:00
|
|
|
if (match<(const BYTE*)source)
|
2014-05-27 01:41:29 +00:00
|
|
|
{
|
2014-06-09 17:08:23 +00:00
|
|
|
refDelta = dictDelta;
|
2014-05-27 01:41:29 +00:00
|
|
|
lowLimit = dictionary;
|
|
|
|
}
|
2014-06-09 17:08:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
refDelta = 0;
|
|
|
|
lowLimit = (const BYTE*)source;
|
|
|
|
}
|
2014-05-27 01:41:29 +00:00
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
LZ4_putPosition(ip, ctx, tableType, base);
|
2014-12-02 22:57:15 +00:00
|
|
|
if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1)
|
|
|
|
&& (match+MAX_DISTANCE>=ip)
|
|
|
|
&& (LZ4_read32(match+refDelta)==LZ4_read32(ip)) )
|
2014-06-17 20:41:59 +00:00
|
|
|
{ token=op++; *token=0; goto _next_match; }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Prepare next loop */
|
2014-05-19 23:40:29 +00:00
|
|
|
forwardH = LZ4_hashPosition(++ip, tableType);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_last_literals:
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Encode Last Literals */
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
int lastRun = (int)(iend - anchor);
|
2014-06-21 16:01:15 +00:00
|
|
|
if ((outputLimited) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize))
|
|
|
|
return 0; /* Check output limit */
|
2013-12-30 17:16:52 +00:00
|
|
|
if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun >= 255 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
|
|
|
|
else *op++ = (BYTE)(lastRun<<ML_BITS);
|
|
|
|
memcpy(op, anchor, iend - anchor);
|
|
|
|
op += iend-anchor;
|
|
|
|
}
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* End */
|
2013-12-30 17:16:52 +00:00
|
|
|
return (int) (((char*)op)-dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
int LZ4_compress(const char* source, char* dest, const int inputSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
#if (HEAPMODE)
|
2014-11-22 23:46:15 +00:00
|
|
|
void* ctx = ALLOCATOR(LZ4_STREAMSIZE_U64, 8); /* Aligned on 8-bytes boundaries */
|
2013-12-30 17:16:52 +00:00
|
|
|
#else
|
2014-11-22 23:46:15 +00:00
|
|
|
U64 ctx[LZ4_STREAMSIZE_U64] = {0}; /* Ensure data is aligned on 8-bytes boundaries */
|
2013-12-30 17:16:52 +00:00
|
|
|
#endif
|
|
|
|
int result;
|
|
|
|
|
2014-12-14 13:29:15 +00:00
|
|
|
if (inputSize < LZ4_64Klimit)
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, 0);
|
2013-12-30 17:16:52 +00:00
|
|
|
else
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, 0);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
#if (HEAPMODE)
|
|
|
|
FREEMEM(ctx);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize)
|
|
|
|
{
|
|
|
|
#if (HEAPMODE)
|
2014-11-27 21:44:36 +00:00
|
|
|
void* ctx = ALLOCATOR(LZ4_STREAMSIZE_U64, 8); /* Aligned on 8-bytes boundaries */
|
2013-12-30 17:16:52 +00:00
|
|
|
#else
|
2014-11-22 23:46:15 +00:00
|
|
|
U64 ctx[LZ4_STREAMSIZE_U64] = {0}; /* Ensure data is aligned on 8-bytes boundaries */
|
2013-12-30 17:16:52 +00:00
|
|
|
#endif
|
|
|
|
int result;
|
|
|
|
|
2014-12-14 13:29:15 +00:00
|
|
|
if (inputSize < LZ4_64Klimit)
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, 0);
|
2013-12-30 17:16:52 +00:00
|
|
|
else
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, 0);
|
|
|
|
|
|
|
|
#if (HEAPMODE)
|
|
|
|
FREEMEM(ctx);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutputSize, unsigned acceleration)
|
|
|
|
{
|
|
|
|
#if (HEAPMODE)
|
|
|
|
void* ctx = ALLOCATOR(LZ4_STREAMSIZE_U64, 8); /* Aligned on 8-bytes boundaries */
|
|
|
|
#else
|
|
|
|
U64 ctx[LZ4_STREAMSIZE_U64] = {0}; /* Ensure data is aligned on 8-bytes boundaries */
|
|
|
|
#endif
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (acceleration == 0)
|
|
|
|
{
|
|
|
|
if (inputSize < LZ4_64Klimit)
|
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, ACCELERATION_DEFAULT);
|
|
|
|
else
|
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, ACCELERATION_DEFAULT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (inputSize < LZ4_64Klimit)
|
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration);
|
|
|
|
else
|
|
|
|
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration);
|
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
#if (HEAPMODE)
|
|
|
|
FREEMEM(ctx);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-20 22:26:03 +00:00
|
|
|
/*****************************************
|
2015-03-17 17:02:01 +00:00
|
|
|
* Experimental : Streaming functions
|
2014-05-20 22:26:03 +00:00
|
|
|
*****************************************/
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-07-05 15:48:49 +00:00
|
|
|
/*
|
|
|
|
* LZ4_initStream
|
|
|
|
* Use this function once, to init a newly allocated LZ4_stream_t structure
|
|
|
|
* Return : 1 if OK, 0 if error
|
|
|
|
*/
|
|
|
|
void LZ4_resetStream (LZ4_stream_t* LZ4_stream)
|
|
|
|
{
|
|
|
|
MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t));
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
LZ4_stream_t* LZ4_createStream(void)
|
2014-06-09 00:01:04 +00:00
|
|
|
{
|
2014-11-22 23:46:15 +00:00
|
|
|
LZ4_stream_t* lz4s = (LZ4_stream_t*)ALLOCATOR(8, LZ4_STREAMSIZE_U64);
|
2014-11-02 21:32:12 +00:00
|
|
|
LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */
|
2014-07-05 15:48:49 +00:00
|
|
|
LZ4_resetStream(lz4s);
|
2014-06-09 00:01:04 +00:00
|
|
|
return lz4s;
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
int LZ4_freeStream (LZ4_stream_t* LZ4_stream)
|
2014-06-09 00:01:04 +00:00
|
|
|
{
|
|
|
|
FREEMEM(LZ4_stream);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-05 15:48:49 +00:00
|
|
|
int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize)
|
2014-06-02 06:07:19 +00:00
|
|
|
{
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict;
|
2014-06-02 06:07:19 +00:00
|
|
|
const BYTE* p = (const BYTE*)dictionary;
|
|
|
|
const BYTE* const dictEnd = p + dictSize;
|
|
|
|
const BYTE* base;
|
|
|
|
|
2014-07-05 15:48:49 +00:00
|
|
|
if (dict->initCheck) LZ4_resetStream(LZ4_dict); /* Uninitialized structure detected */
|
2014-06-02 06:07:19 +00:00
|
|
|
|
|
|
|
if (dictSize < MINMATCH)
|
|
|
|
{
|
2014-06-10 20:45:25 +00:00
|
|
|
dict->dictionary = NULL;
|
2014-06-02 06:07:19 +00:00
|
|
|
dict->dictSize = 0;
|
2014-11-02 21:32:12 +00:00
|
|
|
return 0;
|
2014-06-02 06:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p <= dictEnd - 64 KB) p = dictEnd - 64 KB;
|
|
|
|
base = p - dict->currentOffset;
|
|
|
|
dict->dictionary = p;
|
|
|
|
dict->dictSize = (U32)(dictEnd - p);
|
|
|
|
dict->currentOffset += dict->dictSize;
|
|
|
|
|
|
|
|
while (p <= dictEnd-MINMATCH)
|
|
|
|
{
|
|
|
|
LZ4_putPosition(p, dict, byU32, base);
|
|
|
|
p+=3;
|
|
|
|
}
|
|
|
|
|
2014-11-02 21:32:12 +00:00
|
|
|
return dict->dictSize;
|
2014-06-02 06:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-14 22:29:40 +00:00
|
|
|
static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, const BYTE* src)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-06-04 21:34:48 +00:00
|
|
|
if ((LZ4_dict->currentOffset > 0x80000000) ||
|
2014-06-10 20:45:25 +00:00
|
|
|
((size_t)LZ4_dict->currentOffset > (size_t)src)) /* address space overflow */
|
2014-05-19 23:40:29 +00:00
|
|
|
{
|
|
|
|
/* rescale hash table */
|
|
|
|
U32 delta = LZ4_dict->currentOffset - 64 KB;
|
2014-06-21 16:01:15 +00:00
|
|
|
const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
|
2014-05-19 23:40:29 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i<HASH_SIZE_U32; i++)
|
|
|
|
{
|
|
|
|
if (LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i]=0;
|
|
|
|
else LZ4_dict->hashTable[i] -= delta;
|
|
|
|
}
|
|
|
|
LZ4_dict->currentOffset = 64 KB;
|
2014-06-21 16:01:15 +00:00
|
|
|
if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB;
|
|
|
|
LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
|
2014-05-19 23:40:29 +00:00
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-19 21:54:16 +00:00
|
|
|
FORCE_INLINE int LZ4_compress_continue_generic (void* LZ4_stream, const char* source, char* dest, int inputSize,
|
|
|
|
int maxOutputSize, limitedOutput_directive limit)
|
2014-06-09 00:01:04 +00:00
|
|
|
{
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_stream;
|
2014-06-09 00:01:04 +00:00
|
|
|
const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize;
|
|
|
|
|
2014-06-10 20:45:25 +00:00
|
|
|
const BYTE* smallest = (const BYTE*) source;
|
2014-06-14 15:56:24 +00:00
|
|
|
if (streamPtr->initCheck) return 0; /* Uninitialized structure detected */
|
2014-06-19 21:54:16 +00:00
|
|
|
if ((streamPtr->dictSize>0) && (smallest>dictEnd)) smallest = dictEnd;
|
2014-06-09 00:01:04 +00:00
|
|
|
LZ4_renormDictT(streamPtr, smallest);
|
|
|
|
|
2014-06-14 15:56:24 +00:00
|
|
|
/* Check overlapping input/dictionary space */
|
|
|
|
{
|
|
|
|
const BYTE* sourceEnd = (const BYTE*) source + inputSize;
|
|
|
|
if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd))
|
|
|
|
{
|
2014-06-17 20:41:59 +00:00
|
|
|
streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
|
2014-06-14 15:56:24 +00:00
|
|
|
if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB;
|
|
|
|
if (streamPtr->dictSize < 4) streamPtr->dictSize = 0;
|
2014-06-14 16:04:30 +00:00
|
|
|
streamPtr->dictionary = dictEnd - streamPtr->dictSize;
|
2014-06-14 15:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 21:54:16 +00:00
|
|
|
/* prefix mode : source data follows dictionary */
|
2014-06-09 00:01:04 +00:00
|
|
|
if (dictEnd == (const BYTE*)source)
|
|
|
|
{
|
2014-06-17 20:41:59 +00:00
|
|
|
int result;
|
2014-06-19 21:54:16 +00:00
|
|
|
if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, withPrefix64k, dictSmall, 0);
|
2014-06-17 20:41:59 +00:00
|
|
|
else
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, withPrefix64k, noDictIssue, 0);
|
2014-06-09 00:01:04 +00:00
|
|
|
streamPtr->dictSize += (U32)inputSize;
|
|
|
|
streamPtr->currentOffset += (U32)inputSize;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-19 21:54:16 +00:00
|
|
|
/* external dictionary mode */
|
2014-06-09 00:01:04 +00:00
|
|
|
{
|
2014-06-17 20:41:59 +00:00
|
|
|
int result;
|
2014-06-19 21:54:16 +00:00
|
|
|
if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset))
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, usingExtDict, dictSmall, 0);
|
2014-06-17 20:41:59 +00:00
|
|
|
else
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limit, byU32, usingExtDict, noDictIssue, 0);
|
2014-06-09 00:01:04 +00:00
|
|
|
streamPtr->dictionary = (const BYTE*)source;
|
|
|
|
streamPtr->dictSize = (U32)inputSize;
|
|
|
|
streamPtr->currentOffset += (U32)inputSize;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 15:48:49 +00:00
|
|
|
int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize)
|
2014-06-19 21:54:16 +00:00
|
|
|
{
|
|
|
|
return LZ4_compress_continue_generic(LZ4_stream, source, dest, inputSize, 0, notLimited);
|
|
|
|
}
|
2014-06-09 00:01:04 +00:00
|
|
|
|
2014-07-05 15:48:49 +00:00
|
|
|
int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize)
|
2014-06-19 21:54:16 +00:00
|
|
|
{
|
|
|
|
return LZ4_compress_continue_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput);
|
2014-06-09 00:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
/* Hidden debug function, to force separate dictionary mode */
|
2014-06-09 15:46:03 +00:00
|
|
|
int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int inputSize)
|
2014-05-21 18:46:36 +00:00
|
|
|
{
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_dict;
|
2014-06-09 00:01:04 +00:00
|
|
|
int result;
|
|
|
|
const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize;
|
|
|
|
|
|
|
|
const BYTE* smallest = dictEnd;
|
|
|
|
if (smallest > (const BYTE*) source) smallest = (const BYTE*) source;
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_renormDictT((LZ4_stream_t_internal*)LZ4_dict, smallest);
|
2014-06-09 00:01:04 +00:00
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
result = LZ4_compress_generic(LZ4_dict, source, dest, inputSize, 0, notLimited, byU32, usingExtDict, noDictIssue, 0);
|
2014-05-27 01:41:29 +00:00
|
|
|
|
|
|
|
streamPtr->dictionary = (const BYTE*)source;
|
|
|
|
streamPtr->dictSize = (U32)inputSize;
|
|
|
|
streamPtr->currentOffset += (U32)inputSize;
|
|
|
|
|
|
|
|
return result;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2014-07-05 15:48:49 +00:00
|
|
|
int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize)
|
2014-05-21 18:46:36 +00:00
|
|
|
{
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict;
|
2014-05-21 18:46:36 +00:00
|
|
|
const BYTE* previousDictEnd = dict->dictionary + dict->dictSize;
|
|
|
|
|
|
|
|
if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */
|
|
|
|
if ((U32)dictSize > dict->dictSize) dictSize = dict->dictSize;
|
|
|
|
|
2014-09-10 12:00:39 +00:00
|
|
|
memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
|
2014-05-21 18:46:36 +00:00
|
|
|
|
2014-06-02 06:07:19 +00:00
|
|
|
dict->dictionary = (const BYTE*)safeBuffer;
|
|
|
|
dict->dictSize = (U32)dictSize;
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2014-09-09 22:54:22 +00:00
|
|
|
return dictSize;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2015-03-17 17:02:01 +00:00
|
|
|
/*******************************
|
|
|
|
* Decompression functions
|
|
|
|
*******************************/
|
2014-02-04 14:11:10 +00:00
|
|
|
/*
|
|
|
|
* This generic decompression function cover all use cases.
|
2014-09-10 12:00:39 +00:00
|
|
|
* It shall be instantiated several times, using different sets of directives
|
2014-02-04 14:11:10 +00:00
|
|
|
* Note that it is essential this generic function is really inlined,
|
2014-09-10 12:00:39 +00:00
|
|
|
* in order to remove useless branches during compilation optimization.
|
2014-02-04 14:11:10 +00:00
|
|
|
*/
|
2014-06-12 22:29:06 +00:00
|
|
|
FORCE_INLINE int LZ4_decompress_generic(
|
2014-10-20 23:12:55 +00:00
|
|
|
const char* const source,
|
|
|
|
char* const dest,
|
2014-02-04 14:11:10 +00:00
|
|
|
int inputSize,
|
|
|
|
int outputSize, /* If endOnInput==endOnInputSize, this value is the max size of Output Buffer. */
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
int endOnInput, /* endOnOutputSize, endOnInputSize */
|
|
|
|
int partialDecoding, /* full, partial */
|
2014-04-28 20:45:35 +00:00
|
|
|
int targetOutputSize, /* only used if partialDecoding==partial */
|
2014-06-02 06:07:19 +00:00
|
|
|
int dict, /* noDict, withPrefix64k, usingExtDict */
|
2014-10-25 19:52:10 +00:00
|
|
|
const BYTE* const lowPrefix, /* == dest if dict == noDict */
|
|
|
|
const BYTE* const dictStart, /* only if dict==usingExtDict */
|
|
|
|
const size_t dictSize /* note : = 0 if noDict */
|
2013-12-30 17:16:52 +00:00
|
|
|
)
|
|
|
|
{
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Local Variables */
|
2013-12-30 17:16:52 +00:00
|
|
|
const BYTE* restrict ip = (const BYTE*) source;
|
|
|
|
const BYTE* const iend = ip + inputSize;
|
|
|
|
|
|
|
|
BYTE* op = (BYTE*) dest;
|
|
|
|
BYTE* const oend = op + outputSize;
|
|
|
|
BYTE* cpy;
|
|
|
|
BYTE* oexit = op + targetOutputSize;
|
2014-10-25 19:52:10 +00:00
|
|
|
const BYTE* const lowLimit = lowPrefix - dictSize;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-06-12 22:29:06 +00:00
|
|
|
const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize;
|
2014-08-13 15:44:44 +00:00
|
|
|
const size_t dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4};
|
|
|
|
const size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
const int safeDecode = (endOnInput==endOnInputSize);
|
|
|
|
const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
|
2014-06-21 16:01:15 +00:00
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Special cases */
|
2014-07-14 22:04:10 +00:00
|
|
|
if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT; /* targetOutputSize too high => decode everything */
|
|
|
|
if ((endOnInput) && (unlikely(outputSize==0))) return ((inputSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */
|
2014-02-04 14:11:10 +00:00
|
|
|
if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0?1:-1);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-06-12 22:29:06 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Main Loop */
|
2013-12-30 17:16:52 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
unsigned token;
|
|
|
|
size_t length;
|
2014-10-25 19:52:10 +00:00
|
|
|
const BYTE* match;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
/* get literal length */
|
2013-12-30 17:16:52 +00:00
|
|
|
token = *ip++;
|
|
|
|
if ((length=(token>>ML_BITS)) == RUN_MASK)
|
|
|
|
{
|
2014-06-22 10:25:04 +00:00
|
|
|
unsigned s;
|
|
|
|
do
|
2014-06-12 22:29:06 +00:00
|
|
|
{
|
|
|
|
s = *ip++;
|
|
|
|
length += s;
|
|
|
|
}
|
2014-06-22 10:25:04 +00:00
|
|
|
while (likely((endOnInput)?ip<iend-RUN_MASK:1) && (s==255));
|
2014-10-25 19:52:10 +00:00
|
|
|
if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)(op))) goto _output_error; /* overflow detection */
|
|
|
|
if ((safeDecode) && unlikely((size_t)(ip+length)<(size_t)(ip))) goto _output_error; /* overflow detection */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* copy literals */
|
2013-12-30 17:16:52 +00:00
|
|
|
cpy = op+length;
|
|
|
|
if (((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) )
|
|
|
|
|| ((!endOnInput) && (cpy>oend-COPYLENGTH)))
|
|
|
|
{
|
|
|
|
if (partialDecoding)
|
|
|
|
{
|
2014-02-04 14:11:10 +00:00
|
|
|
if (cpy > oend) goto _output_error; /* Error : write attempt beyond end of output buffer */
|
|
|
|
if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-04 14:11:10 +00:00
|
|
|
if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */
|
|
|
|
if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
memcpy(op, ip, length);
|
|
|
|
ip += length;
|
|
|
|
op += length;
|
2014-07-14 22:04:10 +00:00
|
|
|
break; /* Necessarily EOF, due to parsing restrictions */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
2014-11-27 21:44:36 +00:00
|
|
|
LZ4_wildCopy(op, ip, cpy);
|
|
|
|
ip += length; op = cpy;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* get offset */
|
2014-11-22 23:46:15 +00:00
|
|
|
match = cpy - LZ4_readLE16(ip); ip+=2;
|
2014-10-25 19:52:10 +00:00
|
|
|
if ((checkOffset) && (unlikely(match < lowLimit))) goto _output_error; /* Error : offset outside destination buffer */
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* get matchlength */
|
2014-11-22 23:46:15 +00:00
|
|
|
length = token & ML_MASK;
|
|
|
|
if (length == ML_MASK)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-06-12 22:29:06 +00:00
|
|
|
unsigned s;
|
|
|
|
do
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-07-02 17:02:29 +00:00
|
|
|
if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error;
|
2014-06-12 22:29:06 +00:00
|
|
|
s = *ip++;
|
2013-12-30 17:16:52 +00:00
|
|
|
length += s;
|
2014-06-12 22:29:06 +00:00
|
|
|
} while (s==255);
|
2014-10-25 19:52:10 +00:00
|
|
|
if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)op)) goto _output_error; /* overflow detection */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
2014-08-13 15:44:44 +00:00
|
|
|
length += MINMATCH;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-04-28 20:45:35 +00:00
|
|
|
/* check external dictionary */
|
2014-10-25 19:52:10 +00:00
|
|
|
if ((dict==usingExtDict) && (match < lowPrefix))
|
2014-04-28 20:45:35 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
if (unlikely(op+length > oend-LASTLITERALS)) goto _output_error; /* doesn't respect parsing restriction */
|
2014-04-28 20:45:35 +00:00
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
if (length <= (size_t)(lowPrefix-match))
|
2014-04-28 20:45:35 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
/* match can be copied as a single segment from external dictionary */
|
|
|
|
match = dictEnd - (lowPrefix-match);
|
|
|
|
memcpy(op, match, length);
|
2014-08-13 15:44:44 +00:00
|
|
|
op += length;
|
2014-04-28 20:45:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
/* match encompass external dictionary and current segment */
|
|
|
|
size_t copySize = (size_t)(lowPrefix-match);
|
2014-04-28 20:45:35 +00:00
|
|
|
memcpy(op, dictEnd - copySize, copySize);
|
|
|
|
op += copySize;
|
2014-08-13 15:44:44 +00:00
|
|
|
copySize = length - copySize;
|
2014-10-25 19:52:10 +00:00
|
|
|
if (copySize > (size_t)(op-lowPrefix)) /* overlap within current segment */
|
2014-04-28 20:45:35 +00:00
|
|
|
{
|
2014-07-05 12:33:57 +00:00
|
|
|
BYTE* const endOfMatch = op + copySize;
|
2014-10-25 19:52:10 +00:00
|
|
|
const BYTE* copyFrom = lowPrefix;
|
2014-07-05 12:33:57 +00:00
|
|
|
while (op < endOfMatch) *op++ = *copyFrom++;
|
2014-04-28 20:45:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
memcpy(op, lowPrefix, copySize);
|
2014-04-28 20:45:35 +00:00
|
|
|
op += copySize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* copy repeated sequence */
|
2014-08-13 15:44:44 +00:00
|
|
|
cpy = op + length;
|
2014-11-29 15:41:28 +00:00
|
|
|
if (unlikely((op-match)<8))
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
const size_t dec64 = dec64table[op-match];
|
|
|
|
op[0] = match[0];
|
|
|
|
op[1] = match[1];
|
|
|
|
op[2] = match[2];
|
|
|
|
op[3] = match[3];
|
|
|
|
match += dec32table[op-match];
|
2014-11-22 23:46:15 +00:00
|
|
|
LZ4_copy4(op+4, match);
|
2014-10-25 19:52:10 +00:00
|
|
|
op += 8; match -= dec64;
|
2014-11-22 23:46:15 +00:00
|
|
|
} else { LZ4_copy8(op, match); op+=8; match+=8; }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-08-13 15:44:44 +00:00
|
|
|
if (unlikely(cpy>oend-12))
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-11-27 21:44:36 +00:00
|
|
|
if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals */
|
2014-11-28 01:48:21 +00:00
|
|
|
if (op < oend-8)
|
|
|
|
{
|
|
|
|
LZ4_wildCopy(op, match, oend-8);
|
2014-11-29 15:41:28 +00:00
|
|
|
match += (oend-8) - op;
|
2014-11-28 01:48:21 +00:00
|
|
|
op = oend-8;
|
|
|
|
}
|
2014-11-27 21:44:36 +00:00
|
|
|
while (op<cpy) *op++ = *match++;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
2014-11-27 21:44:36 +00:00
|
|
|
else
|
|
|
|
LZ4_wildCopy(op, match, cpy);
|
2014-02-04 14:11:10 +00:00
|
|
|
op=cpy; /* correction */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* end of decoding */
|
2013-12-30 17:16:52 +00:00
|
|
|
if (endOnInput)
|
2014-02-04 14:11:10 +00:00
|
|
|
return (int) (((char*)op)-dest); /* Nb of output bytes decoded */
|
2013-12-30 17:16:52 +00:00
|
|
|
else
|
2014-02-04 14:11:10 +00:00
|
|
|
return (int) (((char*)ip)-source); /* Nb of input bytes read */
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-02-04 14:11:10 +00:00
|
|
|
/* Overflow error detected */
|
2013-12-30 17:16:52 +00:00
|
|
|
_output_error:
|
|
|
|
return (int) (-(((char*)ip)-source))-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize)
|
2014-04-28 20:45:35 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, endOnInputSize, full, 0, noDict, (BYTE*)dest, NULL, 0);
|
2014-04-28 20:45:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
int LZ4_decompress_safe_partial(const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, endOnInputSize, partial, targetOutputSize, noDict, (BYTE*)dest, NULL, 0);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 20:41:59 +00:00
|
|
|
int LZ4_decompress_fast(const char* source, char* dest, int originalSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, (BYTE*)(dest - 64 KB), NULL, 64 KB);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
|
2014-06-17 20:41:59 +00:00
|
|
|
/* streaming decompression functions */
|
|
|
|
|
|
|
|
typedef struct
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
BYTE* externalDict;
|
|
|
|
size_t extDictSize;
|
|
|
|
BYTE* prefixEnd;
|
|
|
|
size_t prefixSize;
|
2014-06-17 20:41:59 +00:00
|
|
|
} LZ4_streamDecode_t_internal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If you prefer dynamic allocation methods,
|
|
|
|
* LZ4_createStreamDecode()
|
|
|
|
* provides a pointer (void*) towards an initialized LZ4_streamDecode_t structure.
|
|
|
|
*/
|
2014-07-14 22:04:10 +00:00
|
|
|
LZ4_streamDecode_t* LZ4_createStreamDecode(void)
|
2014-06-17 20:41:59 +00:00
|
|
|
{
|
2015-03-15 00:42:27 +00:00
|
|
|
LZ4_streamDecode_t* lz4s = (LZ4_streamDecode_t*) ALLOCATOR(1, sizeof(LZ4_streamDecode_t));
|
2014-06-17 20:41:59 +00:00
|
|
|
return lz4s;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream)
|
|
|
|
{
|
|
|
|
FREEMEM(LZ4_stream);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-17 20:41:59 +00:00
|
|
|
/*
|
2014-07-14 22:04:10 +00:00
|
|
|
* LZ4_setStreamDecode
|
2014-06-17 20:41:59 +00:00
|
|
|
* Use this function to instruct where to find the dictionary
|
|
|
|
* This function is not necessary if previous data is still available where it was decoded.
|
|
|
|
* Loading a size of 0 is allowed (same effect as no dictionary).
|
|
|
|
* Return : 1 if OK, 0 if error
|
|
|
|
*/
|
2014-07-14 22:04:10 +00:00
|
|
|
int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2014-06-17 20:41:59 +00:00
|
|
|
LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode;
|
2014-10-25 19:52:10 +00:00
|
|
|
lz4sd->prefixSize = (size_t) dictSize;
|
|
|
|
lz4sd->prefixEnd = (BYTE*) dictionary + dictSize;
|
|
|
|
lz4sd->externalDict = NULL;
|
|
|
|
lz4sd->extDictSize = 0;
|
2014-06-17 20:41:59 +00:00
|
|
|
return 1;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 20:41:59 +00:00
|
|
|
/*
|
|
|
|
*_continue() :
|
|
|
|
These decoding functions allow decompression of multiple blocks in "streaming" mode.
|
|
|
|
Previously decoded blocks must still be available at the memory position where they were decoded.
|
2014-06-19 21:54:16 +00:00
|
|
|
If it's not possible, save the relevant part of decoded data into a safe buffer,
|
2014-10-25 19:52:10 +00:00
|
|
|
and indicate where it stands using LZ4_setStreamDecode()
|
2014-06-17 20:41:59 +00:00
|
|
|
*/
|
2014-07-14 22:04:10 +00:00
|
|
|
int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize)
|
2014-05-04 12:26:05 +00:00
|
|
|
{
|
2014-06-17 20:41:59 +00:00
|
|
|
LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode;
|
|
|
|
int result;
|
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
if (lz4sd->prefixEnd == (BYTE*)dest)
|
2014-06-19 21:54:16 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
|
|
|
|
endOnInputSize, full, 0,
|
|
|
|
usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
|
|
|
|
if (result <= 0) return result;
|
|
|
|
lz4sd->prefixSize += result;
|
|
|
|
lz4sd->prefixEnd += result;
|
2014-06-19 21:54:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
lz4sd->extDictSize = lz4sd->prefixSize;
|
|
|
|
lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
|
|
|
|
result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize,
|
|
|
|
endOnInputSize, full, 0,
|
|
|
|
usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize);
|
|
|
|
if (result <= 0) return result;
|
|
|
|
lz4sd->prefixSize = result;
|
|
|
|
lz4sd->prefixEnd = (BYTE*)dest + result;
|
2014-06-19 21:54:16 +00:00
|
|
|
}
|
2014-06-17 20:41:59 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:04:10 +00:00
|
|
|
int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize)
|
2014-06-17 20:41:59 +00:00
|
|
|
{
|
|
|
|
LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode;
|
|
|
|
int result;
|
|
|
|
|
2014-10-25 19:52:10 +00:00
|
|
|
if (lz4sd->prefixEnd == (BYTE*)dest)
|
2014-06-19 21:54:16 +00:00
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
result = LZ4_decompress_generic(source, dest, 0, originalSize,
|
|
|
|
endOnOutputSize, full, 0,
|
|
|
|
usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize);
|
|
|
|
if (result <= 0) return result;
|
|
|
|
lz4sd->prefixSize += originalSize;
|
|
|
|
lz4sd->prefixEnd += originalSize;
|
2014-06-19 21:54:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
lz4sd->extDictSize = lz4sd->prefixSize;
|
|
|
|
lz4sd->externalDict = (BYTE*)dest - lz4sd->extDictSize;
|
|
|
|
result = LZ4_decompress_generic(source, dest, 0, originalSize,
|
|
|
|
endOnOutputSize, full, 0,
|
|
|
|
usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize);
|
|
|
|
if (result <= 0) return result;
|
|
|
|
lz4sd->prefixSize = originalSize;
|
|
|
|
lz4sd->prefixEnd = (BYTE*)dest + originalSize;
|
2014-06-19 21:54:16 +00:00
|
|
|
}
|
2014-06-17 20:41:59 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Advanced decoding functions :
|
|
|
|
*_usingDict() :
|
|
|
|
These decoding functions work the same as "_continue" ones,
|
|
|
|
the dictionary must be explicitly provided within parameters
|
|
|
|
*/
|
|
|
|
|
2014-09-13 18:49:01 +00:00
|
|
|
FORCE_INLINE int LZ4_decompress_usingDict_generic(const char* source, char* dest, int compressedSize, int maxOutputSize, int safe, const char* dictStart, int dictSize)
|
|
|
|
{
|
2014-09-21 08:56:21 +00:00
|
|
|
if (dictSize==0)
|
2014-10-27 23:39:06 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest, NULL, 0);
|
|
|
|
if (dictStart+dictSize == dest)
|
|
|
|
{
|
|
|
|
if (dictSize >= (int)(64 KB - 1))
|
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, withPrefix64k, (BYTE*)dest-64 KB, NULL, 0);
|
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest-dictSize, NULL, 0);
|
|
|
|
}
|
2014-10-25 19:52:10 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, usingExtDict, (BYTE*)dest, (BYTE*)dictStart, dictSize);
|
2014-09-13 18:49:01 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 20:41:59 +00:00
|
|
|
int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize)
|
|
|
|
{
|
2014-09-13 18:49:01 +00:00
|
|
|
return LZ4_decompress_usingDict_generic(source, dest, compressedSize, maxOutputSize, 1, dictStart, dictSize);
|
2014-05-04 12:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize)
|
|
|
|
{
|
2014-09-13 18:49:01 +00:00
|
|
|
return LZ4_decompress_usingDict_generic(source, dest, 0, originalSize, 0, dictStart, dictSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* debug function */
|
|
|
|
int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize)
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, (BYTE*)dest, (BYTE*)dictStart, dictSize);
|
2014-05-04 12:26:05 +00:00
|
|
|
}
|
2014-04-28 20:45:35 +00:00
|
|
|
|
|
|
|
|
2014-06-17 20:41:59 +00:00
|
|
|
/***************************************************
|
2015-03-17 17:02:01 +00:00
|
|
|
* Obsolete Functions
|
2014-06-17 20:41:59 +00:00
|
|
|
***************************************************/
|
2014-04-28 20:45:35 +00:00
|
|
|
/*
|
2014-06-09 00:01:04 +00:00
|
|
|
These function names are deprecated and should no longer be used.
|
|
|
|
They are only provided here for compatibility with older user programs.
|
|
|
|
- LZ4_uncompress is totally equivalent to LZ4_decompress_fast
|
|
|
|
- LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe
|
2014-04-28 20:45:35 +00:00
|
|
|
*/
|
2014-03-12 14:51:59 +00:00
|
|
|
int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); }
|
|
|
|
int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); }
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2014-06-09 00:01:04 +00:00
|
|
|
|
2014-08-09 21:14:26 +00:00
|
|
|
/* Obsolete Streaming functions */
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2014-06-09 15:46:03 +00:00
|
|
|
int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; }
|
2014-06-09 00:01:04 +00:00
|
|
|
|
2014-07-14 22:29:40 +00:00
|
|
|
static void LZ4_init(LZ4_stream_t_internal* lz4ds, const BYTE* base)
|
2014-05-19 23:40:29 +00:00
|
|
|
{
|
2014-06-22 10:25:04 +00:00
|
|
|
MEM_INIT(lz4ds, 0, LZ4_STREAMSIZE);
|
2014-05-19 23:40:29 +00:00
|
|
|
lz4ds->bufferStart = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LZ4_resetStreamState(void* state, const char* inputBuffer)
|
|
|
|
{
|
|
|
|
if ((((size_t)state) & 3) != 0) return 1; /* Error : pointer is not aligned on 4-bytes boundary */
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_init((LZ4_stream_t_internal*)state, (const BYTE*)inputBuffer);
|
2014-05-19 23:40:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* LZ4_create (const char* inputBuffer)
|
|
|
|
{
|
2014-11-22 23:46:15 +00:00
|
|
|
void* lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64);
|
2014-06-14 15:56:24 +00:00
|
|
|
LZ4_init ((LZ4_stream_t_internal*)lz4ds, (const BYTE*)inputBuffer);
|
2014-05-19 23:40:29 +00:00
|
|
|
return lz4ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* LZ4_slideInputBuffer (void* LZ4_Data)
|
|
|
|
{
|
2014-12-03 22:19:11 +00:00
|
|
|
LZ4_stream_t_internal* ctx = (LZ4_stream_t_internal*)LZ4_Data;
|
2015-03-25 17:06:40 +00:00
|
|
|
int dictSize = LZ4_saveDict((LZ4_stream_t*)LZ4_Data, (char*)ctx->bufferStart, 64 KB);
|
2014-12-03 22:19:11 +00:00
|
|
|
return (char*)(ctx->bufferStart + dictSize);
|
2014-05-19 23:40:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 20:41:59 +00:00
|
|
|
/* Obsolete compresson functions using User-allocated state */
|
2014-06-09 15:46:03 +00:00
|
|
|
|
|
|
|
int LZ4_sizeofState() { return LZ4_STREAMSIZE; }
|
|
|
|
|
|
|
|
int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize)
|
|
|
|
{
|
|
|
|
if (((size_t)(state)&3) != 0) return 0; /* Error : state is not aligned on 4-bytes boundary */
|
2014-06-12 22:29:06 +00:00
|
|
|
MEM_INIT(state, 0, LZ4_STREAMSIZE);
|
2014-06-09 15:46:03 +00:00
|
|
|
|
2014-12-14 13:29:15 +00:00
|
|
|
if (inputSize < LZ4_64Klimit)
|
2015-03-30 20:32:25 +00:00
|
|
|
return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, 0);
|
2014-06-09 15:46:03 +00:00
|
|
|
else
|
2015-03-30 20:32:25 +00:00
|
|
|
return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, 0);
|
2014-06-09 15:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize)
|
|
|
|
{
|
|
|
|
if (((size_t)(state)&3) != 0) return 0; /* Error : state is not aligned on 4-bytes boundary */
|
2014-06-12 22:29:06 +00:00
|
|
|
MEM_INIT(state, 0, LZ4_STREAMSIZE);
|
2014-06-09 15:46:03 +00:00
|
|
|
|
2014-12-14 13:29:15 +00:00
|
|
|
if (inputSize < LZ4_64Klimit)
|
2015-03-30 20:32:25 +00:00
|
|
|
return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, 0);
|
2014-06-09 15:46:03 +00:00
|
|
|
else
|
2015-03-30 20:32:25 +00:00
|
|
|
return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, 0);
|
2014-06-09 15:46:03 +00:00
|
|
|
}
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2014-08-09 21:14:26 +00:00
|
|
|
/* Obsolete streaming decompression functions */
|
2014-06-17 20:41:59 +00:00
|
|
|
|
|
|
|
int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize)
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB);
|
2014-06-17 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize)
|
|
|
|
{
|
2014-10-25 19:52:10 +00:00
|
|
|
return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB);
|
2014-06-17 20:41:59 +00:00
|
|
|
}
|
2014-11-29 19:19:39 +00:00
|
|
|
|
|
|
|
#endif /* LZ4_COMMONDEFS_ONLY */
|
|
|
|
|