sereal 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/rsrl +1 -1
- data/ext/sereal/decode.c +9 -27
- data/ext/sereal/encode.c +10 -33
- data/ext/sereal/proto.h +0 -1
- data/ext/sereal/sereal.c +2 -6
- data/ext/sereal/sereal.h +0 -2
- metadata +2 -10
- data/ext/sereal/lz4.c +0 -822
- data/ext/sereal/lz4.h +0 -205
- data/ext/sereal/lz4/lz4.c +0 -822
- data/ext/sereal/lz4/lz4.h +0 -205
- data/ext/sereal/lz4/lz4hc.c +0 -817
- data/ext/sereal/lz4/lz4hc.h +0 -111
- data/ext/sereal/lz4hc.c +0 -817
- data/ext/sereal/lz4hc.h +0 -111
data/ext/sereal/lz4.h
DELETED
@@ -1,205 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
LZ4 - Fast LZ compression algorithm
|
3
|
-
Header File
|
4
|
-
Copyright (C) 2011-2013, Yann Collet.
|
5
|
-
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
6
|
-
|
7
|
-
Redistribution and use in source and binary forms, with or without
|
8
|
-
modification, are permitted provided that the following conditions are
|
9
|
-
met:
|
10
|
-
|
11
|
-
* Redistributions of source code must retain the above copyright
|
12
|
-
notice, this list of conditions and the following disclaimer.
|
13
|
-
* Redistributions in binary form must reproduce the above
|
14
|
-
copyright notice, this list of conditions and the following disclaimer
|
15
|
-
in the documentation and/or other materials provided with the
|
16
|
-
distribution.
|
17
|
-
|
18
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
-
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20
|
-
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21
|
-
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22
|
-
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23
|
-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24
|
-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25
|
-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26
|
-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
-
|
30
|
-
You can contact the author at :
|
31
|
-
- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
|
32
|
-
- LZ4 source repository : http://code.google.com/p/lz4/
|
33
|
-
*/
|
34
|
-
#pragma once
|
35
|
-
|
36
|
-
#if defined (__cplusplus)
|
37
|
-
extern "C" {
|
38
|
-
#endif
|
39
|
-
|
40
|
-
|
41
|
-
//**************************************
|
42
|
-
// Compiler Options
|
43
|
-
//**************************************
|
44
|
-
#if defined(_MSC_VER) && !defined(__cplusplus) // Visual Studio
|
45
|
-
# define inline __inline // Visual C is not C99, but supports some kind of inline
|
46
|
-
#endif
|
47
|
-
|
48
|
-
|
49
|
-
//****************************
|
50
|
-
// Simple Functions
|
51
|
-
//****************************
|
52
|
-
|
53
|
-
int LZ4_compress (const char* source, char* dest, int inputSize);
|
54
|
-
int LZ4_decompress_safe (const char* source, char* dest, int inputSize, int maxOutputSize);
|
55
|
-
|
56
|
-
/*
|
57
|
-
LZ4_compress() :
|
58
|
-
Compresses 'inputSize' bytes from 'source' into 'dest'.
|
59
|
-
Destination buffer must be already allocated,
|
60
|
-
and must be sized to handle worst cases situations (input data not compressible)
|
61
|
-
Worst case size evaluation is provided by function LZ4_compressBound()
|
62
|
-
inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
|
63
|
-
return : the number of bytes written in buffer dest
|
64
|
-
or 0 if the compression fails
|
65
|
-
|
66
|
-
LZ4_decompress_safe() :
|
67
|
-
maxOutputSize : is the size of the destination buffer (which must be already allocated)
|
68
|
-
return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
|
69
|
-
If the source stream is detected malformed, the function will stop decoding and return a negative result.
|
70
|
-
This function is protected against buffer overflow exploits (never writes outside of output buffer, and never reads outside of input buffer). Therefore, it is protected against malicious data packets
|
71
|
-
*/
|
72
|
-
|
73
|
-
|
74
|
-
//****************************
|
75
|
-
// Advanced Functions
|
76
|
-
//****************************
|
77
|
-
#define LZ4_MAX_INPUT_SIZE 0x7E000000 // 2 113 929 216 bytes
|
78
|
-
#define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
|
79
|
-
static inline int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
|
80
|
-
|
81
|
-
/*
|
82
|
-
LZ4_compressBound() :
|
83
|
-
Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible)
|
84
|
-
primarily useful for memory allocation of output buffer.
|
85
|
-
inline function is recommended for the general case,
|
86
|
-
macro is also provided when result needs to be evaluated at compilation (such as stack memory allocation).
|
87
|
-
|
88
|
-
isize : is the input size. Max supported value is LZ4_MAX_INPUT_SIZE
|
89
|
-
return : maximum output size in a "worst case" scenario
|
90
|
-
or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE)
|
91
|
-
*/
|
92
|
-
|
93
|
-
|
94
|
-
int LZ4_compress_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize);
|
95
|
-
|
96
|
-
/*
|
97
|
-
LZ4_compress_limitedOutput() :
|
98
|
-
Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
|
99
|
-
If it cannot achieve it, compression will stop, and result of the function will be zero.
|
100
|
-
This function never writes outside of provided output buffer.
|
101
|
-
|
102
|
-
inputSize : Max supported value is LZ4_MAX_INPUT_VALUE
|
103
|
-
maxOutputSize : is the size of the destination buffer (which must be already allocated)
|
104
|
-
return : the number of bytes written in buffer 'dest'
|
105
|
-
or 0 if the compression fails
|
106
|
-
*/
|
107
|
-
|
108
|
-
|
109
|
-
int LZ4_decompress_fast (const char* source, char* dest, int outputSize);
|
110
|
-
|
111
|
-
/*
|
112
|
-
LZ4_decompress_fast() :
|
113
|
-
outputSize : is the original (uncompressed) size
|
114
|
-
return : the number of bytes read from the source buffer (in other words, the compressed size)
|
115
|
-
If the source stream is malformed, the function will stop decoding and return a negative result.
|
116
|
-
note : This function is a bit faster than LZ4_decompress_safe()
|
117
|
-
This function never writes outside of output buffers, but may read beyond input buffer in case of malicious data packet.
|
118
|
-
Use this function preferably into a trusted environment (data to decode comes from a trusted source).
|
119
|
-
Destination buffer must be already allocated. Its size must be a minimum of 'outputSize' bytes.
|
120
|
-
*/
|
121
|
-
|
122
|
-
int LZ4_decompress_safe_partial (const char* source, char* dest, int inputSize, int targetOutputSize, int maxOutputSize);
|
123
|
-
|
124
|
-
/*
|
125
|
-
LZ4_decompress_safe_partial() :
|
126
|
-
This function decompress a compressed block of size 'inputSize' at position 'source'
|
127
|
-
into output buffer 'dest' of size 'maxOutputSize'.
|
128
|
-
The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached,
|
129
|
-
reducing decompression time.
|
130
|
-
return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
|
131
|
-
Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller.
|
132
|
-
Always control how many bytes were decoded.
|
133
|
-
If the source stream is detected malformed, the function will stop decoding and return a negative result.
|
134
|
-
This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets
|
135
|
-
*/
|
136
|
-
|
137
|
-
|
138
|
-
//****************************
|
139
|
-
// Stream Functions
|
140
|
-
//****************************
|
141
|
-
|
142
|
-
void* LZ4_create (const char* inputBuffer);
|
143
|
-
int LZ4_compress_continue (void* LZ4_Data, const char* source, char* dest, int inputSize);
|
144
|
-
int LZ4_compress_limitedOutput_continue (void* LZ4_Data, const char* source, char* dest, int inputSize, int maxOutputSize);
|
145
|
-
char* LZ4_slideInputBuffer (void* LZ4_Data);
|
146
|
-
int LZ4_free (void* LZ4_Data);
|
147
|
-
|
148
|
-
/*
|
149
|
-
These functions allow the compression of dependent blocks, where each block benefits from prior 64 KB within preceding blocks.
|
150
|
-
In order to achieve this, it is necessary to start creating the LZ4 Data Structure, thanks to the function :
|
151
|
-
|
152
|
-
void* LZ4_create (const char* inputBuffer);
|
153
|
-
The result of the function is the (void*) pointer on the LZ4 Data Structure.
|
154
|
-
This pointer will be needed in all other functions.
|
155
|
-
If the pointer returned is NULL, then the allocation has failed, and compression must be aborted.
|
156
|
-
The only parameter 'const char* inputBuffer' must, obviously, point at the beginning of input buffer.
|
157
|
-
The input buffer must be already allocated, and size at least 192KB.
|
158
|
-
'inputBuffer' will also be the 'const char* source' of the first block.
|
159
|
-
|
160
|
-
All blocks are expected to lay next to each other within the input buffer, starting from 'inputBuffer'.
|
161
|
-
To compress each block, use either LZ4_compress_continue() or LZ4_compress_limitedOutput_continue().
|
162
|
-
Their behavior are identical to LZ4_compress() or LZ4_compress_limitedOutput(),
|
163
|
-
but require the LZ4 Data Structure as their first argument, and check that each block starts right after the previous one.
|
164
|
-
If next block does not begin immediately after the previous one, the compression will fail (return 0).
|
165
|
-
|
166
|
-
When it's no longer possible to lay the next block after the previous one (not enough space left into input buffer), a call to :
|
167
|
-
char* LZ4_slideInputBuffer(void* LZ4_Data);
|
168
|
-
must be performed. It will typically copy the latest 64KB of input at the beginning of input buffer.
|
169
|
-
Note that, for this function to work properly, minimum size of an input buffer must be 192KB.
|
170
|
-
==> The memory position where the next input data block must start is provided as the result of the function.
|
171
|
-
|
172
|
-
Compression can then resume, using LZ4_compress_continue() or LZ4_compress_limitedOutput_continue(), as usual.
|
173
|
-
|
174
|
-
When compression is completed, a call to LZ4_free() will release the memory used by the LZ4 Data Structure.
|
175
|
-
*/
|
176
|
-
|
177
|
-
|
178
|
-
int LZ4_decompress_safe_withPrefix64k (const char* source, char* dest, int inputSize, int maxOutputSize);
|
179
|
-
int LZ4_decompress_fast_withPrefix64k (const char* source, char* dest, int outputSize);
|
180
|
-
|
181
|
-
/*
|
182
|
-
*_withPrefix64k() :
|
183
|
-
These decoding functions work the same as their "normal name" versions,
|
184
|
-
but can use up to 64KB of data in front of 'char* dest'.
|
185
|
-
These functions are necessary to decode inter-dependant blocks.
|
186
|
-
*/
|
187
|
-
|
188
|
-
|
189
|
-
//****************************
|
190
|
-
// Obsolete Functions
|
191
|
-
//****************************
|
192
|
-
|
193
|
-
static inline int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); }
|
194
|
-
static inline int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); }
|
195
|
-
|
196
|
-
/*
|
197
|
-
These functions are deprecated and should no longer be used.
|
198
|
-
They are provided here for compatibility with existing user programs.
|
199
|
-
*/
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
#if defined (__cplusplus)
|
204
|
-
}
|
205
|
-
#endif
|
data/ext/sereal/lz4/lz4.c
DELETED
@@ -1,822 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
LZ4 - Fast LZ compression algorithm
|
3
|
-
Copyright (C) 2011-2013, Yann Collet.
|
4
|
-
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
5
|
-
|
6
|
-
Redistribution and use in source and binary forms, with or without
|
7
|
-
modification, are permitted provided that the following conditions are
|
8
|
-
met:
|
9
|
-
|
10
|
-
* Redistributions of source code must retain the above copyright
|
11
|
-
notice, this list of conditions and the following disclaimer.
|
12
|
-
* Redistributions in binary form must reproduce the above
|
13
|
-
copyright notice, this list of conditions and the following disclaimer
|
14
|
-
in the documentation and/or other materials provided with the
|
15
|
-
distribution.
|
16
|
-
|
17
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
18
|
-
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
19
|
-
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
20
|
-
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
21
|
-
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22
|
-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23
|
-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
24
|
-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
25
|
-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
-
|
29
|
-
You can contact the author at :
|
30
|
-
- LZ4 source repository : http://code.google.com/p/lz4/
|
31
|
-
- LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
|
32
|
-
*/
|
33
|
-
|
34
|
-
//**************************************
|
35
|
-
// Tuning parameters
|
36
|
-
//**************************************
|
37
|
-
// MEMORY_USAGE :
|
38
|
-
// Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
|
39
|
-
// Increasing memory usage improves compression ratio
|
40
|
-
// Reduced memory usage can improve speed, due to cache effect
|
41
|
-
// Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
|
42
|
-
#define MEMORY_USAGE 14
|
43
|
-
|
44
|
-
// HEAPMODE :
|
45
|
-
// Select how default compression functions will allocate memory for their hash table,
|
46
|
-
// in memory stack (0:default, fastest), or in memory heap (1:requires memory allocation (malloc)).
|
47
|
-
#define HEAPMODE 0
|
48
|
-
|
49
|
-
|
50
|
-
//**************************************
|
51
|
-
// CPU Feature Detection
|
52
|
-
//**************************************
|
53
|
-
// 32 or 64 bits ?
|
54
|
-
#if (defined(__x86_64__) || defined(_M_X64) || defined(_WIN64) \
|
55
|
-
|| defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) \
|
56
|
-
|| defined(__64BIT__) || defined(_LP64) || defined(__LP64__) \
|
57
|
-
|| defined(__ia64) || defined(__itanium__) || defined(_M_IA64) ) // Detects 64 bits mode
|
58
|
-
# define LZ4_ARCH64 1
|
59
|
-
#else
|
60
|
-
# define LZ4_ARCH64 0
|
61
|
-
#endif
|
62
|
-
|
63
|
-
// Little Endian or Big Endian ?
|
64
|
-
// Overwrite the #define below if you know your architecture endianess
|
65
|
-
#if defined (__GLIBC__)
|
66
|
-
# include <endian.h>
|
67
|
-
# if (__BYTE_ORDER == __BIG_ENDIAN)
|
68
|
-
# define LZ4_BIG_ENDIAN 1
|
69
|
-
# endif
|
70
|
-
#elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))
|
71
|
-
# define LZ4_BIG_ENDIAN 1
|
72
|
-
#elif defined(__sparc) || defined(__sparc__) \
|
73
|
-
|| defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) \
|
74
|
-
|| defined(__hpux) || defined(__hppa) \
|
75
|
-
|| defined(_MIPSEB) || defined(__s390__)
|
76
|
-
# define LZ4_BIG_ENDIAN 1
|
77
|
-
#else
|
78
|
-
// Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
|
79
|
-
#endif
|
80
|
-
|
81
|
-
// Unaligned memory access is automatically enabled for "common" CPU, such as x86.
|
82
|
-
// For others CPU, such as ARM, the compiler may be more cautious, inserting unnecessary extra code to ensure aligned access property
|
83
|
-
// If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance
|
84
|
-
#if defined(__ARM_FEATURE_UNALIGNED)
|
85
|
-
# define LZ4_FORCE_UNALIGNED_ACCESS 1
|
86
|
-
#endif
|
87
|
-
|
88
|
-
// Define this parameter if your target system or compiler does not support hardware bit count
|
89
|
-
#if defined(_MSC_VER) && defined(_WIN32_WCE) // Visual Studio for Windows CE does not support Hardware bit count
|
90
|
-
# define LZ4_FORCE_SW_BITCOUNT
|
91
|
-
#endif
|
92
|
-
|
93
|
-
// BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE :
|
94
|
-
// This option may provide a small boost to performance for some big endian cpu, although probably modest.
|
95
|
-
// You may set this option to 1 if data will remain within closed environment.
|
96
|
-
// This option is useless on Little_Endian CPU (such as x86)
|
97
|
-
//#define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1
|
98
|
-
|
99
|
-
|
100
|
-
//**************************************
|
101
|
-
// Compiler Options
|
102
|
-
//**************************************
|
103
|
-
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) // C99
|
104
|
-
/* "restrict" is a known keyword */
|
105
|
-
#else
|
106
|
-
# define restrict // Disable restrict
|
107
|
-
#endif
|
108
|
-
|
109
|
-
#ifdef _MSC_VER // Visual Studio
|
110
|
-
# define FORCE_INLINE static __forceinline
|
111
|
-
# include <intrin.h> // For Visual 2005
|
112
|
-
# if LZ4_ARCH64 // 64-bits
|
113
|
-
# pragma intrinsic(_BitScanForward64) // For Visual 2005
|
114
|
-
# pragma intrinsic(_BitScanReverse64) // For Visual 2005
|
115
|
-
# else // 32-bits
|
116
|
-
# pragma intrinsic(_BitScanForward) // For Visual 2005
|
117
|
-
# pragma intrinsic(_BitScanReverse) // For Visual 2005
|
118
|
-
# endif
|
119
|
-
# pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
|
120
|
-
#else
|
121
|
-
# ifdef __GNUC__
|
122
|
-
# define FORCE_INLINE static inline __attribute__((always_inline))
|
123
|
-
# else
|
124
|
-
# define FORCE_INLINE static inline
|
125
|
-
# endif
|
126
|
-
#endif
|
127
|
-
|
128
|
-
#ifdef _MSC_VER
|
129
|
-
# define lz4_bswap16(x) _byteswap_ushort(x)
|
130
|
-
#else
|
131
|
-
# define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
|
132
|
-
#endif
|
133
|
-
|
134
|
-
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
|
135
|
-
|
136
|
-
#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
|
137
|
-
# define expect(expr,value) (__builtin_expect ((expr),(value)) )
|
138
|
-
#else
|
139
|
-
# define expect(expr,value) (expr)
|
140
|
-
#endif
|
141
|
-
|
142
|
-
#define likely(expr) expect((expr) != 0, 1)
|
143
|
-
#define unlikely(expr) expect((expr) != 0, 0)
|
144
|
-
|
145
|
-
|
146
|
-
//**************************************
|
147
|
-
// Memory routines
|
148
|
-
//**************************************
|
149
|
-
#include <stdlib.h> // malloc, calloc, free
|
150
|
-
#define ALLOCATOR(n,s) calloc(n,s)
|
151
|
-
#define FREEMEM free
|
152
|
-
#include <string.h> // memset, memcpy
|
153
|
-
#define MEM_INIT memset
|
154
|
-
|
155
|
-
|
156
|
-
//**************************************
|
157
|
-
// Includes
|
158
|
-
//**************************************
|
159
|
-
#include "lz4.h"
|
160
|
-
|
161
|
-
|
162
|
-
//**************************************
|
163
|
-
// Basic Types
|
164
|
-
//**************************************
|
165
|
-
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
|
166
|
-
# include <stdint.h>
|
167
|
-
typedef uint8_t BYTE;
|
168
|
-
typedef uint16_t U16;
|
169
|
-
typedef uint32_t U32;
|
170
|
-
typedef int32_t S32;
|
171
|
-
typedef uint64_t U64;
|
172
|
-
#else
|
173
|
-
typedef unsigned char BYTE;
|
174
|
-
typedef unsigned short U16;
|
175
|
-
typedef unsigned int U32;
|
176
|
-
typedef signed int S32;
|
177
|
-
typedef unsigned long long U64;
|
178
|
-
#endif
|
179
|
-
|
180
|
-
#if defined(__GNUC__) && !defined(LZ4_FORCE_UNALIGNED_ACCESS)
|
181
|
-
# define _PACKED __attribute__ ((packed))
|
182
|
-
#else
|
183
|
-
# define _PACKED
|
184
|
-
#endif
|
185
|
-
|
186
|
-
#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)
|
187
|
-
# if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
188
|
-
# pragma pack(1)
|
189
|
-
# else
|
190
|
-
# pragma pack(push, 1)
|
191
|
-
# endif
|
192
|
-
#endif
|
193
|
-
|
194
|
-
typedef struct { U16 v; } _PACKED U16_S;
|
195
|
-
typedef struct { U32 v; } _PACKED U32_S;
|
196
|
-
typedef struct { U64 v; } _PACKED U64_S;
|
197
|
-
typedef struct {size_t v;} _PACKED size_t_S;
|
198
|
-
|
199
|
-
#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)
|
200
|
-
# if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
201
|
-
# pragma pack(0)
|
202
|
-
# else
|
203
|
-
# pragma pack(pop)
|
204
|
-
# endif
|
205
|
-
#endif
|
206
|
-
|
207
|
-
#define A16(x) (((U16_S *)(x))->v)
|
208
|
-
#define A32(x) (((U32_S *)(x))->v)
|
209
|
-
#define A64(x) (((U64_S *)(x))->v)
|
210
|
-
#define AARCH(x) (((size_t_S *)(x))->v)
|
211
|
-
|
212
|
-
|
213
|
-
//**************************************
|
214
|
-
// Constants
|
215
|
-
//**************************************
|
216
|
-
#define LZ4_HASHLOG (MEMORY_USAGE-2)
|
217
|
-
#define HASHTABLESIZE (1 << MEMORY_USAGE)
|
218
|
-
#define HASHNBCELLS4 (1 << LZ4_HASHLOG)
|
219
|
-
|
220
|
-
#define MINMATCH 4
|
221
|
-
|
222
|
-
#define COPYLENGTH 8
|
223
|
-
#define LASTLITERALS 5
|
224
|
-
#define MFLIMIT (COPYLENGTH+MINMATCH)
|
225
|
-
const int LZ4_minLength = (MFLIMIT+1);
|
226
|
-
|
227
|
-
#define LZ4_64KLIMIT ((1<<16) + (MFLIMIT-1))
|
228
|
-
#define SKIPSTRENGTH 6 // Increasing this value will make the compression run slower on incompressible data
|
229
|
-
|
230
|
-
#define MAXD_LOG 16
|
231
|
-
#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
|
232
|
-
|
233
|
-
#define ML_BITS 4
|
234
|
-
#define ML_MASK ((1U<<ML_BITS)-1)
|
235
|
-
#define RUN_BITS (8-ML_BITS)
|
236
|
-
#define RUN_MASK ((1U<<RUN_BITS)-1)
|
237
|
-
|
238
|
-
#define KB *(1U<<10)
|
239
|
-
#define MB *(1U<<20)
|
240
|
-
#define GB *(1U<<30)
|
241
|
-
|
242
|
-
|
243
|
-
//**************************************
|
244
|
-
// Structures and local types
|
245
|
-
//**************************************
|
246
|
-
|
247
|
-
typedef struct {
|
248
|
-
U32 hashTable[HASHNBCELLS4];
|
249
|
-
const BYTE* bufferStart;
|
250
|
-
const BYTE* base;
|
251
|
-
const BYTE* nextBlock;
|
252
|
-
} LZ4_Data_Structure;
|
253
|
-
|
254
|
-
typedef enum { notLimited = 0, limited = 1 } limitedOutput_directive;
|
255
|
-
typedef enum { byPtr, byU32, byU16 } tableType_t;
|
256
|
-
|
257
|
-
typedef enum { noPrefix = 0, withPrefix = 1 } prefix64k_directive;
|
258
|
-
|
259
|
-
typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
|
260
|
-
typedef enum { full = 0, partial = 1 } earlyEnd_directive;
|
261
|
-
|
262
|
-
|
263
|
-
//**************************************
|
264
|
-
// Architecture-specific macros
|
265
|
-
//**************************************
|
266
|
-
#define STEPSIZE sizeof(size_t)
|
267
|
-
#define LZ4_COPYSTEP(d,s) { AARCH(d) = AARCH(s); d+=STEPSIZE; s+=STEPSIZE; }
|
268
|
-
#define LZ4_COPY8(d,s) { LZ4_COPYSTEP(d,s); if (STEPSIZE<8) LZ4_COPYSTEP(d,s); }
|
269
|
-
#define LZ4_SECURECOPY(d,s,e) { if ((STEPSIZE==4)||(d<e)) LZ4_WILDCOPY(d,s,e); }
|
270
|
-
|
271
|
-
#if LZ4_ARCH64 // 64-bit
|
272
|
-
# define HTYPE U32
|
273
|
-
# define INITBASE(base) const BYTE* const base = ip
|
274
|
-
#else // 32-bit
|
275
|
-
# define HTYPE const BYTE*
|
276
|
-
# define INITBASE(base) const int base = 0
|
277
|
-
#endif
|
278
|
-
|
279
|
-
#if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
|
280
|
-
# define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
|
281
|
-
# define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }
|
282
|
-
#else // Little Endian
|
283
|
-
# define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
|
284
|
-
# define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
|
285
|
-
#endif
|
286
|
-
|
287
|
-
|
288
|
-
//**************************************
|
289
|
-
// Macros
|
290
|
-
//**************************************
|
291
|
-
#define LZ4_WILDCOPY(d,s,e) { do { LZ4_COPY8(d,s) } while (d<e); } // at the end, d>=e;
|
292
|
-
|
293
|
-
|
294
|
-
//****************************
|
295
|
-
// Private functions
|
296
|
-
//****************************
|
297
|
-
#if LZ4_ARCH64
|
298
|
-
|
299
|
-
FORCE_INLINE int LZ4_NbCommonBytes (register U64 val)
|
300
|
-
{
|
301
|
-
# if defined(LZ4_BIG_ENDIAN)
|
302
|
-
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
303
|
-
unsigned long r = 0;
|
304
|
-
_BitScanReverse64( &r, val );
|
305
|
-
return (int)(r>>3);
|
306
|
-
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
307
|
-
return (__builtin_clzll(val) >> 3);
|
308
|
-
# else
|
309
|
-
int r;
|
310
|
-
if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
|
311
|
-
if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
|
312
|
-
r += (!val);
|
313
|
-
return r;
|
314
|
-
# endif
|
315
|
-
# else
|
316
|
-
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
317
|
-
unsigned long r = 0;
|
318
|
-
_BitScanForward64( &r, val );
|
319
|
-
return (int)(r>>3);
|
320
|
-
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
321
|
-
return (__builtin_ctzll(val) >> 3);
|
322
|
-
# else
|
323
|
-
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 };
|
324
|
-
return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
|
325
|
-
# endif
|
326
|
-
# endif
|
327
|
-
}
|
328
|
-
|
329
|
-
#else
|
330
|
-
|
331
|
-
FORCE_INLINE int LZ4_NbCommonBytes (register U32 val)
|
332
|
-
{
|
333
|
-
# if defined(LZ4_BIG_ENDIAN)
|
334
|
-
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
335
|
-
unsigned long r = 0;
|
336
|
-
_BitScanReverse( &r, val );
|
337
|
-
return (int)(r>>3);
|
338
|
-
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
339
|
-
return (__builtin_clz(val) >> 3);
|
340
|
-
# else
|
341
|
-
int r;
|
342
|
-
if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
|
343
|
-
r += (!val);
|
344
|
-
return r;
|
345
|
-
# endif
|
346
|
-
# else
|
347
|
-
# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
348
|
-
unsigned long r;
|
349
|
-
_BitScanForward( &r, val );
|
350
|
-
return (int)(r>>3);
|
351
|
-
# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
352
|
-
return (__builtin_ctz(val) >> 3);
|
353
|
-
# else
|
354
|
-
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 };
|
355
|
-
return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
|
356
|
-
# endif
|
357
|
-
# endif
|
358
|
-
}
|
359
|
-
|
360
|
-
#endif
|
361
|
-
|
362
|
-
|
363
|
-
//****************************
|
364
|
-
// Compression functions
|
365
|
-
//****************************
|
366
|
-
FORCE_INLINE int LZ4_hashSequence(U32 sequence, tableType_t tableType)
|
367
|
-
{
|
368
|
-
if (tableType == byU16)
|
369
|
-
return (((sequence) * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1)));
|
370
|
-
else
|
371
|
-
return (((sequence) * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG));
|
372
|
-
}
|
373
|
-
|
374
|
-
FORCE_INLINE int LZ4_hashPosition(const BYTE* p, tableType_t tableType) { return LZ4_hashSequence(A32(p), tableType); }
|
375
|
-
|
376
|
-
FORCE_INLINE void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase)
|
377
|
-
{
|
378
|
-
switch (tableType)
|
379
|
-
{
|
380
|
-
case byPtr: { const BYTE** hashTable = (const BYTE**) tableBase; hashTable[h] = p; break; }
|
381
|
-
case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); break; }
|
382
|
-
case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); break; }
|
383
|
-
}
|
384
|
-
}
|
385
|
-
|
386
|
-
FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
|
387
|
-
{
|
388
|
-
U32 h = LZ4_hashPosition(p, tableType);
|
389
|
-
LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
|
390
|
-
}
|
391
|
-
|
392
|
-
FORCE_INLINE const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase)
|
393
|
-
{
|
394
|
-
if (tableType == byPtr) { const BYTE** hashTable = (const BYTE**) tableBase; return hashTable[h]; }
|
395
|
-
if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; }
|
396
|
-
{ U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } // default, to ensure a return
|
397
|
-
}
|
398
|
-
|
399
|
-
FORCE_INLINE const BYTE* LZ4_getPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
|
400
|
-
{
|
401
|
-
U32 h = LZ4_hashPosition(p, tableType);
|
402
|
-
return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
|
403
|
-
}
|
404
|
-
|
405
|
-
|
406
|
-
FORCE_INLINE int LZ4_compress_generic(
|
407
|
-
void* ctx,
|
408
|
-
const char* source,
|
409
|
-
char* dest,
|
410
|
-
int inputSize,
|
411
|
-
int maxOutputSize,
|
412
|
-
|
413
|
-
limitedOutput_directive limitedOutput,
|
414
|
-
tableType_t tableType,
|
415
|
-
prefix64k_directive prefix)
|
416
|
-
{
|
417
|
-
const BYTE* ip = (const BYTE*) source;
|
418
|
-
const BYTE* const base = (prefix==withPrefix) ? ((LZ4_Data_Structure*)ctx)->base : (const BYTE*) source;
|
419
|
-
const BYTE* const lowLimit = ((prefix==withPrefix) ? ((LZ4_Data_Structure*)ctx)->bufferStart : (const BYTE*)source);
|
420
|
-
const BYTE* anchor = (const BYTE*) source;
|
421
|
-
const BYTE* const iend = ip + inputSize;
|
422
|
-
const BYTE* const mflimit = iend - MFLIMIT;
|
423
|
-
const BYTE* const matchlimit = iend - LASTLITERALS;
|
424
|
-
|
425
|
-
BYTE* op = (BYTE*) dest;
|
426
|
-
BYTE* const oend = op + maxOutputSize;
|
427
|
-
|
428
|
-
int length;
|
429
|
-
const int skipStrength = SKIPSTRENGTH;
|
430
|
-
U32 forwardH;
|
431
|
-
|
432
|
-
// Init conditions
|
433
|
-
if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; // Unsupported input size, too large (or negative)
|
434
|
-
if ((prefix==withPrefix) && (ip != ((LZ4_Data_Structure*)ctx)->nextBlock)) return 0; // must continue from end of previous block
|
435
|
-
if (prefix==withPrefix) ((LZ4_Data_Structure*)ctx)->nextBlock=iend; // do it now, due to potential early exit
|
436
|
-
if ((tableType == byU16) && (inputSize>=LZ4_64KLIMIT)) return 0; // Size too large (not within 64K limit)
|
437
|
-
if (inputSize<LZ4_minLength) goto _last_literals; // Input too small, no compression (all literals)
|
438
|
-
|
439
|
-
// First Byte
|
440
|
-
LZ4_putPosition(ip, ctx, tableType, base);
|
441
|
-
ip++; forwardH = LZ4_hashPosition(ip, tableType);
|
442
|
-
|
443
|
-
// Main Loop
|
444
|
-
for ( ; ; )
|
445
|
-
{
|
446
|
-
int findMatchAttempts = (1U << skipStrength) + 3;
|
447
|
-
const BYTE* forwardIp = ip;
|
448
|
-
const BYTE* ref;
|
449
|
-
BYTE* token;
|
450
|
-
|
451
|
-
// Find a match
|
452
|
-
do {
|
453
|
-
U32 h = forwardH;
|
454
|
-
int step = findMatchAttempts++ >> skipStrength;
|
455
|
-
ip = forwardIp;
|
456
|
-
forwardIp = ip + step;
|
457
|
-
|
458
|
-
if unlikely(forwardIp > mflimit) { goto _last_literals; }
|
459
|
-
|
460
|
-
forwardH = LZ4_hashPosition(forwardIp, tableType);
|
461
|
-
ref = LZ4_getPositionOnHash(h, ctx, tableType, base);
|
462
|
-
LZ4_putPositionOnHash(ip, h, ctx, tableType, base);
|
463
|
-
|
464
|
-
} while ((ref + MAX_DISTANCE < ip) || (A32(ref) != A32(ip)));
|
465
|
-
|
466
|
-
// Catch up
|
467
|
-
while ((ip>anchor) && (ref > lowLimit) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; }
|
468
|
-
|
469
|
-
// Encode Literal length
|
470
|
-
length = (int)(ip - anchor);
|
471
|
-
token = op++;
|
472
|
-
if ((limitedOutput) && unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend)) return 0; // Check output limit
|
473
|
-
if (length>=(int)RUN_MASK)
|
474
|
-
{
|
475
|
-
int len = length-RUN_MASK;
|
476
|
-
*token=(RUN_MASK<<ML_BITS);
|
477
|
-
for(; len >= 255 ; len-=255) *op++ = 255;
|
478
|
-
*op++ = (BYTE)len;
|
479
|
-
}
|
480
|
-
else *token = (BYTE)(length<<ML_BITS);
|
481
|
-
|
482
|
-
// Copy Literals
|
483
|
-
{ BYTE* end=(op)+(length); LZ4_WILDCOPY(op,anchor,end); op=end; }
|
484
|
-
|
485
|
-
_next_match:
|
486
|
-
// Encode Offset
|
487
|
-
LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
|
488
|
-
|
489
|
-
// Start Counting
|
490
|
-
ip+=MINMATCH; ref+=MINMATCH; // MinMatch already verified
|
491
|
-
anchor = ip;
|
492
|
-
while likely(ip<matchlimit-(STEPSIZE-1))
|
493
|
-
{
|
494
|
-
size_t diff = AARCH(ref) ^ AARCH(ip);
|
495
|
-
if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
|
496
|
-
ip += LZ4_NbCommonBytes(diff);
|
497
|
-
goto _endCount;
|
498
|
-
}
|
499
|
-
if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
|
500
|
-
if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
|
501
|
-
if ((ip<matchlimit) && (*ref == *ip)) ip++;
|
502
|
-
_endCount:
|
503
|
-
|
504
|
-
// Encode MatchLength
|
505
|
-
length = (int)(ip - anchor);
|
506
|
-
if ((limitedOutput) && unlikely(op + (1 + LASTLITERALS) + (length>>8) > oend)) return 0; // Check output limit
|
507
|
-
if (length>=(int)ML_MASK)
|
508
|
-
{
|
509
|
-
*token += ML_MASK;
|
510
|
-
length -= ML_MASK;
|
511
|
-
for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; }
|
512
|
-
if (length >= 255) { length-=255; *op++ = 255; }
|
513
|
-
*op++ = (BYTE)length;
|
514
|
-
}
|
515
|
-
else *token += (BYTE)(length);
|
516
|
-
|
517
|
-
// Test end of chunk
|
518
|
-
if (ip > mflimit) { anchor = ip; break; }
|
519
|
-
|
520
|
-
// Fill table
|
521
|
-
LZ4_putPosition(ip-2, ctx, tableType, base);
|
522
|
-
|
523
|
-
// Test next position
|
524
|
-
ref = LZ4_getPosition(ip, ctx, tableType, base);
|
525
|
-
LZ4_putPosition(ip, ctx, tableType, base);
|
526
|
-
if ((ref + MAX_DISTANCE >= ip) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; }
|
527
|
-
|
528
|
-
// Prepare next loop
|
529
|
-
anchor = ip++;
|
530
|
-
forwardH = LZ4_hashPosition(ip, tableType);
|
531
|
-
}
|
532
|
-
|
533
|
-
_last_literals:
|
534
|
-
// Encode Last Literals
|
535
|
-
{
|
536
|
-
int lastRun = (int)(iend - anchor);
|
537
|
-
if ((limitedOutput) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) return 0; // Check output limit
|
538
|
-
if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun >= 255 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
|
539
|
-
else *op++ = (BYTE)(lastRun<<ML_BITS);
|
540
|
-
memcpy(op, anchor, iend - anchor);
|
541
|
-
op += iend-anchor;
|
542
|
-
}
|
543
|
-
|
544
|
-
// End
|
545
|
-
return (int) (((char*)op)-dest);
|
546
|
-
}
|
547
|
-
|
548
|
-
|
549
|
-
int LZ4_compress(const char* source, char* dest, int inputSize)
|
550
|
-
{
|
551
|
-
#if (HEAPMODE)
|
552
|
-
void* ctx = ALLOCATOR(HASHNBCELLS4, 4); // Aligned on 4-bytes boundaries
|
553
|
-
#else
|
554
|
-
U32 ctx[1U<<(MEMORY_USAGE-2)] = {0}; // Ensure data is aligned on 4-bytes boundaries
|
555
|
-
#endif
|
556
|
-
int result;
|
557
|
-
|
558
|
-
if (inputSize < (int)LZ4_64KLIMIT)
|
559
|
-
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, 0, notLimited, byU16, noPrefix);
|
560
|
-
else
|
561
|
-
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, 0, notLimited, (sizeof(void*)==8) ? byU32 : byPtr, noPrefix);
|
562
|
-
|
563
|
-
#if (HEAPMODE)
|
564
|
-
FREEMEM(ctx);
|
565
|
-
#endif
|
566
|
-
return result;
|
567
|
-
}
|
568
|
-
|
569
|
-
int LZ4_compress_continue (void* LZ4_Data, const char* source, char* dest, int inputSize)
|
570
|
-
{
|
571
|
-
return LZ4_compress_generic(LZ4_Data, source, dest, inputSize, 0, notLimited, byU32, withPrefix);
|
572
|
-
}
|
573
|
-
|
574
|
-
|
575
|
-
int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize)
|
576
|
-
{
|
577
|
-
#if (HEAPMODE)
|
578
|
-
void* ctx = ALLOCATOR(HASHNBCELLS4, 4); // Aligned on 4-bytes boundaries
|
579
|
-
#else
|
580
|
-
U32 ctx[1U<<(MEMORY_USAGE-2)] = {0}; // Ensure data is aligned on 4-bytes boundaries
|
581
|
-
#endif
|
582
|
-
int result;
|
583
|
-
|
584
|
-
if (inputSize < (int)LZ4_64KLIMIT)
|
585
|
-
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limited, byU16, noPrefix);
|
586
|
-
else
|
587
|
-
result = LZ4_compress_generic((void*)ctx, source, dest, inputSize, maxOutputSize, limited, (sizeof(void*)==8) ? byU32 : byPtr, noPrefix);
|
588
|
-
|
589
|
-
#if (HEAPMODE)
|
590
|
-
FREEMEM(ctx);
|
591
|
-
#endif
|
592
|
-
return result;
|
593
|
-
}
|
594
|
-
|
595
|
-
int LZ4_compress_limitedOutput_continue (void* LZ4_Data, const char* source, char* dest, int inputSize, int maxOutputSize)
|
596
|
-
{
|
597
|
-
return LZ4_compress_generic(LZ4_Data, source, dest, inputSize, maxOutputSize, limited, byU32, withPrefix);
|
598
|
-
}
|
599
|
-
|
600
|
-
|
601
|
-
//****************************
|
602
|
-
// Stream functions
|
603
|
-
//****************************
|
604
|
-
|
605
|
-
FORCE_INLINE void LZ4_init(LZ4_Data_Structure* lz4ds, const BYTE* base)
|
606
|
-
{
|
607
|
-
MEM_INIT(lz4ds->hashTable, 0, sizeof(lz4ds->hashTable));
|
608
|
-
lz4ds->bufferStart = base;
|
609
|
-
lz4ds->base = base;
|
610
|
-
lz4ds->nextBlock = base;
|
611
|
-
}
|
612
|
-
|
613
|
-
|
614
|
-
void* LZ4_create (const char* inputBuffer)
|
615
|
-
{
|
616
|
-
void* lz4ds = ALLOCATOR(1, sizeof(LZ4_Data_Structure));
|
617
|
-
LZ4_init ((LZ4_Data_Structure*)lz4ds, (const BYTE*)inputBuffer);
|
618
|
-
return lz4ds;
|
619
|
-
}
|
620
|
-
|
621
|
-
|
622
|
-
int LZ4_free (void* LZ4_Data)
|
623
|
-
{
|
624
|
-
FREEMEM(LZ4_Data);
|
625
|
-
return (0);
|
626
|
-
}
|
627
|
-
|
628
|
-
|
629
|
-
char* LZ4_slideInputBuffer (void* LZ4_Data)
|
630
|
-
{
|
631
|
-
LZ4_Data_Structure* lz4ds = (LZ4_Data_Structure*)LZ4_Data;
|
632
|
-
size_t delta = lz4ds->nextBlock - (lz4ds->bufferStart + 64 KB);
|
633
|
-
|
634
|
-
if ( (lz4ds->base - delta > lz4ds->base) // underflow control
|
635
|
-
|| ((size_t)(lz4ds->nextBlock - lz4ds->base) > 0xE0000000) ) // close to 32-bits limit
|
636
|
-
{
|
637
|
-
size_t deltaLimit = (lz4ds->nextBlock - 64 KB) - lz4ds->base;
|
638
|
-
int nH;
|
639
|
-
|
640
|
-
for (nH=0; nH < HASHNBCELLS4; nH++)
|
641
|
-
{
|
642
|
-
if ((size_t)(lz4ds->hashTable[nH]) < deltaLimit) lz4ds->hashTable[nH] = 0;
|
643
|
-
else lz4ds->hashTable[nH] -= (U32)deltaLimit;
|
644
|
-
}
|
645
|
-
memcpy((void*)(lz4ds->bufferStart), (const void*)(lz4ds->nextBlock - 64 KB), 64 KB);
|
646
|
-
lz4ds->base = lz4ds->bufferStart;
|
647
|
-
lz4ds->nextBlock = lz4ds->base + 64 KB;
|
648
|
-
}
|
649
|
-
else
|
650
|
-
{
|
651
|
-
memcpy((void*)(lz4ds->bufferStart), (const void*)(lz4ds->nextBlock - 64 KB), 64 KB);
|
652
|
-
lz4ds->nextBlock -= delta;
|
653
|
-
lz4ds->base -= delta;
|
654
|
-
}
|
655
|
-
|
656
|
-
return (char*)(lz4ds->nextBlock);
|
657
|
-
}
|
658
|
-
|
659
|
-
|
660
|
-
//****************************
|
661
|
-
// Decompression functions
|
662
|
-
//****************************
|
663
|
-
|
664
|
-
// This generic decompression function cover all use cases.
|
665
|
-
// It shall be instanciated several times, using different sets of directives
|
666
|
-
// Note that it is essential this generic function is really inlined,
|
667
|
-
// in order to remove useless branches during compilation optimisation.
|
668
|
-
FORCE_INLINE int LZ4_decompress_generic(
|
669
|
-
const char* source,
|
670
|
-
char* dest,
|
671
|
-
int inputSize, //
|
672
|
-
int outputSize, // If endOnInput==endOnInputSize, this value is the max size of Output Buffer.
|
673
|
-
|
674
|
-
int endOnInput, // endOnOutputSize, endOnInputSize
|
675
|
-
int prefix64k, // noPrefix, withPrefix
|
676
|
-
int partialDecoding, // full, partial
|
677
|
-
int targetOutputSize // only used if partialDecoding==partial
|
678
|
-
)
|
679
|
-
{
|
680
|
-
// Local Variables
|
681
|
-
const BYTE* restrict ip = (const BYTE*) source;
|
682
|
-
const BYTE* ref;
|
683
|
-
const BYTE* const iend = ip + inputSize;
|
684
|
-
|
685
|
-
BYTE* op = (BYTE*) dest;
|
686
|
-
BYTE* const oend = op + outputSize;
|
687
|
-
BYTE* cpy;
|
688
|
-
BYTE* oexit = op + targetOutputSize;
|
689
|
-
|
690
|
-
const size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0}; // static reduces speed for LZ4_decompress_safe() on GCC64
|
691
|
-
static const size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
|
692
|
-
|
693
|
-
|
694
|
-
// Special cases
|
695
|
-
if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT; // targetOutputSize too high => decode everything
|
696
|
-
if ((endOnInput) && unlikely(outputSize==0)) return ((inputSize==1) && (*ip==0)) ? 0 : -1; // Empty output buffer
|
697
|
-
if ((!endOnInput) && unlikely(outputSize==0)) return (*ip==0?1:-1);
|
698
|
-
|
699
|
-
|
700
|
-
// Main Loop
|
701
|
-
while (1)
|
702
|
-
{
|
703
|
-
unsigned token;
|
704
|
-
size_t length;
|
705
|
-
|
706
|
-
// get runlength
|
707
|
-
token = *ip++;
|
708
|
-
if ((length=(token>>ML_BITS)) == RUN_MASK)
|
709
|
-
{
|
710
|
-
unsigned s=255;
|
711
|
-
while (((endOnInput)?ip<iend:1) && (s==255))
|
712
|
-
{
|
713
|
-
s = *ip++;
|
714
|
-
length += s;
|
715
|
-
}
|
716
|
-
}
|
717
|
-
|
718
|
-
// copy literals
|
719
|
-
cpy = op+length;
|
720
|
-
if (((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) )
|
721
|
-
|| ((!endOnInput) && (cpy>oend-COPYLENGTH)))
|
722
|
-
{
|
723
|
-
if (partialDecoding)
|
724
|
-
{
|
725
|
-
if (cpy > oend) goto _output_error; // Error : write attempt beyond end of output buffer
|
726
|
-
if ((endOnInput) && (ip+length > iend)) goto _output_error; // Error : read attempt beyond end of input buffer
|
727
|
-
}
|
728
|
-
else
|
729
|
-
{
|
730
|
-
if ((!endOnInput) && (cpy != oend)) goto _output_error; // Error : block decoding must stop exactly there
|
731
|
-
if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; // Error : input must be consumed
|
732
|
-
}
|
733
|
-
memcpy(op, ip, length);
|
734
|
-
ip += length;
|
735
|
-
op += length;
|
736
|
-
break; // Necessarily EOF, due to parsing restrictions
|
737
|
-
}
|
738
|
-
LZ4_WILDCOPY(op, ip, cpy); ip -= (op-cpy); op = cpy;
|
739
|
-
|
740
|
-
// get offset
|
741
|
-
LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;
|
742
|
-
if ((prefix64k==noPrefix) && unlikely(ref < (BYTE* const)dest)) goto _output_error; // Error : offset outside destination buffer
|
743
|
-
|
744
|
-
// get matchlength
|
745
|
-
if ((length=(token&ML_MASK)) == ML_MASK)
|
746
|
-
{
|
747
|
-
while ((!endOnInput) || (ip<iend-(LASTLITERALS+1))) // Ensure enough bytes remain for LASTLITERALS + token
|
748
|
-
{
|
749
|
-
unsigned s = *ip++;
|
750
|
-
length += s;
|
751
|
-
if (s==255) continue;
|
752
|
-
break;
|
753
|
-
}
|
754
|
-
}
|
755
|
-
|
756
|
-
// copy repeated sequence
|
757
|
-
if unlikely((op-ref)<(int)STEPSIZE)
|
758
|
-
{
|
759
|
-
const size_t dec64 = dec64table[(sizeof(void*)==4) ? 0 : op-ref];
|
760
|
-
op[0] = ref[0];
|
761
|
-
op[1] = ref[1];
|
762
|
-
op[2] = ref[2];
|
763
|
-
op[3] = ref[3];
|
764
|
-
op += 4, ref += 4; ref -= dec32table[op-ref];
|
765
|
-
A32(op) = A32(ref);
|
766
|
-
op += STEPSIZE-4; ref -= dec64;
|
767
|
-
} else { LZ4_COPYSTEP(op,ref); }
|
768
|
-
cpy = op + length - (STEPSIZE-4);
|
769
|
-
|
770
|
-
if unlikely(cpy>oend-COPYLENGTH-(STEPSIZE-4))
|
771
|
-
{
|
772
|
-
if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
|
773
|
-
LZ4_SECURECOPY(op, ref, (oend-COPYLENGTH));
|
774
|
-
while(op<cpy) *op++=*ref++;
|
775
|
-
op=cpy;
|
776
|
-
continue;
|
777
|
-
}
|
778
|
-
LZ4_WILDCOPY(op, ref, cpy);
|
779
|
-
op=cpy; // correction
|
780
|
-
}
|
781
|
-
|
782
|
-
// end of decoding
|
783
|
-
if (endOnInput)
|
784
|
-
return (int) (((char*)op)-dest); // Nb of output bytes decoded
|
785
|
-
else
|
786
|
-
return (int) (((char*)ip)-source); // Nb of input bytes read
|
787
|
-
|
788
|
-
// Overflow error detected
|
789
|
-
_output_error:
|
790
|
-
return (int) (-(((char*)ip)-source))-1;
|
791
|
-
}
|
792
|
-
|
793
|
-
|
794
|
-
int LZ4_decompress_safe(const char* source, char* dest, int inputSize, int maxOutputSize)
|
795
|
-
{
|
796
|
-
return LZ4_decompress_generic(source, dest, inputSize, maxOutputSize, endOnInputSize, noPrefix, full, 0);
|
797
|
-
}
|
798
|
-
|
799
|
-
int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int inputSize, int maxOutputSize)
|
800
|
-
{
|
801
|
-
return LZ4_decompress_generic(source, dest, inputSize, maxOutputSize, endOnInputSize, withPrefix, full, 0);
|
802
|
-
}
|
803
|
-
|
804
|
-
int LZ4_decompress_safe_partial(const char* source, char* dest, int inputSize, int targetOutputSize, int maxOutputSize)
|
805
|
-
{
|
806
|
-
return LZ4_decompress_generic(source, dest, inputSize, maxOutputSize, endOnInputSize, noPrefix, partial, targetOutputSize);
|
807
|
-
}
|
808
|
-
|
809
|
-
int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int outputSize)
|
810
|
-
{
|
811
|
-
return LZ4_decompress_generic(source, dest, 0, outputSize, endOnOutputSize, withPrefix, full, 0);
|
812
|
-
}
|
813
|
-
|
814
|
-
int LZ4_decompress_fast(const char* source, char* dest, int outputSize)
|
815
|
-
{
|
816
|
-
#ifdef _MSC_VER // This version is faster with Visual
|
817
|
-
return LZ4_decompress_generic(source, dest, 0, outputSize, endOnOutputSize, noPrefix, full, 0);
|
818
|
-
#else
|
819
|
-
return LZ4_decompress_generic(source, dest, 0, outputSize, endOnOutputSize, withPrefix, full, 0);
|
820
|
-
#endif
|
821
|
-
}
|
822
|
-
|