lz4-ruby 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +14 -0
- data/README.rdoc +0 -1
- data/Rakefile +52 -10
- data/VERSION +1 -1
- data/ext/lz4ruby/extconf.rb +6 -0
- data/ext/lz4ruby/lz4.c +819 -0
- data/ext/lz4ruby/lz4.h +120 -0
- data/ext/lz4ruby/lz4hc.c +663 -0
- data/ext/lz4ruby/lz4hc.h +60 -0
- data/ext/{lz4ruby.c → lz4ruby/lz4ruby.c} +0 -0
- data/lib/lz4-ruby.rb +7 -1
- data/test/helper.rb +10 -1
- metadata +102 -74
- data/ext/extconf.rb +0 -28
- data/lz4-ruby.gemspec +0 -66
data/ext/lz4ruby/lz4.h
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
/*
|
2
|
+
LZ4 - Fast LZ compression algorithm
|
3
|
+
Header File
|
4
|
+
Copyright (C) 2011-2012, 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
|
+
// Simple Functions
|
43
|
+
//****************************
|
44
|
+
|
45
|
+
int LZ4_compress (const char* source, char* dest, int isize);
|
46
|
+
int LZ4_uncompress (const char* source, char* dest, int osize);
|
47
|
+
|
48
|
+
/*
|
49
|
+
LZ4_compress() :
|
50
|
+
isize : is the input size. Max supported value is ~1.9GB
|
51
|
+
return : the number of bytes written in buffer dest
|
52
|
+
or 0 if the compression fails (if LZ4_COMPRESSMIN is set)
|
53
|
+
note : destination buffer must be already allocated.
|
54
|
+
destination buffer must be sized to handle worst cases situations (input data not compressible)
|
55
|
+
worst case size evaluation is provided by function LZ4_compressBound()
|
56
|
+
|
57
|
+
LZ4_uncompress() :
|
58
|
+
osize : is the output size, therefore the original size
|
59
|
+
return : the number of bytes read in the source buffer
|
60
|
+
If the source stream is malformed, the function will stop decoding and return a negative result, indicating the byte position of the faulty instruction
|
61
|
+
This function never writes beyond dest + osize, and is therefore protected against malicious data packets
|
62
|
+
note : destination buffer must be already allocated
|
63
|
+
*/
|
64
|
+
|
65
|
+
|
66
|
+
//****************************
|
67
|
+
// Advanced Functions
|
68
|
+
//****************************
|
69
|
+
|
70
|
+
int LZ4_compressBound(int isize);
|
71
|
+
|
72
|
+
/*
|
73
|
+
LZ4_compressBound() :
|
74
|
+
Provides the maximum size that LZ4 may output in a "worst case" scenario (input data not compressible)
|
75
|
+
primarily useful for memory allocation of output buffer.
|
76
|
+
|
77
|
+
isize : is the input size. Max supported value is ~1.9GB
|
78
|
+
return : maximum output size in a "worst case" scenario
|
79
|
+
note : this function is limited by "int" range (2^31-1)
|
80
|
+
*/
|
81
|
+
|
82
|
+
|
83
|
+
int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
|
84
|
+
|
85
|
+
/*
|
86
|
+
LZ4_uncompress_unknownOutputSize() :
|
87
|
+
isize : is the input size, therefore the compressed size
|
88
|
+
maxOutputSize : is the size of the destination buffer (which must be already allocated)
|
89
|
+
return : the number of bytes decoded in the destination buffer (necessarily <= maxOutputSize)
|
90
|
+
If the source stream is malformed, the function will stop decoding and return a negative result, indicating the byte position of the faulty instruction
|
91
|
+
This function never writes beyond dest + maxOutputSize, and is therefore protected against malicious data packets
|
92
|
+
note : Destination buffer must be already allocated.
|
93
|
+
This version is slightly slower than LZ4_uncompress()
|
94
|
+
*/
|
95
|
+
|
96
|
+
|
97
|
+
int LZ4_compressCtx(void** ctx, const char* source, char* dest, int isize);
|
98
|
+
int LZ4_compress64kCtx(void** ctx, const char* source, char* dest, int isize);
|
99
|
+
|
100
|
+
/*
|
101
|
+
LZ4_compressCtx() :
|
102
|
+
This function explicitly handles the CTX memory structure.
|
103
|
+
It avoids allocating/deallocating memory between each call, improving performance when malloc is heavily invoked.
|
104
|
+
This function is only useful when memory is allocated into the heap (HASH_LOG value beyond STACK_LIMIT)
|
105
|
+
Performance difference will be noticeable only when repetitively calling the compression function over many small segments.
|
106
|
+
Note : by default, memory is allocated into the stack, therefore "malloc" is not invoked.
|
107
|
+
LZ4_compress64kCtx() :
|
108
|
+
Same as LZ4_compressCtx(), but specific to small inputs (<64KB).
|
109
|
+
isize *Must* be <64KB, otherwise the output will be corrupted.
|
110
|
+
|
111
|
+
On first call : provide a *ctx=NULL; It will be automatically allocated.
|
112
|
+
On next calls : reuse the same ctx pointer.
|
113
|
+
Use different pointers for different threads when doing multi-threading.
|
114
|
+
|
115
|
+
*/
|
116
|
+
|
117
|
+
|
118
|
+
#if defined (__cplusplus)
|
119
|
+
}
|
120
|
+
#endif
|
data/ext/lz4ruby/lz4hc.c
ADDED
@@ -0,0 +1,663 @@
|
|
1
|
+
/*
|
2
|
+
LZ4 HC - High Compression Mode of LZ4
|
3
|
+
Copyright (C) 2011-2012, 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 homepage : http://fastcompression.blogspot.com/p/lz4.html
|
31
|
+
- LZ4 source repository : http://code.google.com/p/lz4/
|
32
|
+
*/
|
33
|
+
|
34
|
+
|
35
|
+
//**************************************
|
36
|
+
// CPU Feature Detection
|
37
|
+
//**************************************
|
38
|
+
// 32 or 64 bits ?
|
39
|
+
#if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || defined(__LP64__) || defined(_LP64) ) // Detects 64 bits mode
|
40
|
+
#define LZ4_ARCH64 1
|
41
|
+
#else
|
42
|
+
#define LZ4_ARCH64 0
|
43
|
+
#endif
|
44
|
+
|
45
|
+
// Little Endian or Big Endian ?
|
46
|
+
#if (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN) || defined(_ARCH_PPC) || defined(__PPC__) || defined(__PPC) || defined(PPC) || defined(__powerpc__) || defined(__powerpc) || defined(powerpc) || ((defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))) )
|
47
|
+
#define LZ4_BIG_ENDIAN 1
|
48
|
+
#else
|
49
|
+
// Little Endian assumed. PDP Endian and other very rare endian format are unsupported.
|
50
|
+
#endif
|
51
|
+
|
52
|
+
// Unaligned memory access is automatically enabled for "common" CPU, such as x86.
|
53
|
+
// For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected
|
54
|
+
// If you know your target CPU supports unaligned memory access, you may want to force this option manually to improve performance
|
55
|
+
#if defined(__ARM_FEATURE_UNALIGNED)
|
56
|
+
#define LZ4_FORCE_UNALIGNED_ACCESS 1
|
57
|
+
#endif
|
58
|
+
|
59
|
+
|
60
|
+
//**************************************
|
61
|
+
// Compiler Options
|
62
|
+
//**************************************
|
63
|
+
#if __STDC_VERSION__ >= 199901L // C99
|
64
|
+
/* "restrict" is a known keyword */
|
65
|
+
#else
|
66
|
+
#define restrict // Disable restrict
|
67
|
+
#endif
|
68
|
+
|
69
|
+
#ifdef _MSC_VER
|
70
|
+
#define inline __forceinline // Visual is not C99, but supports some kind of inline
|
71
|
+
#endif
|
72
|
+
|
73
|
+
#ifdef _MSC_VER // Visual Studio
|
74
|
+
#define bswap16(x) _byteswap_ushort(x)
|
75
|
+
#else
|
76
|
+
#define bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
|
77
|
+
#endif
|
78
|
+
|
79
|
+
|
80
|
+
//**************************************
|
81
|
+
// Includes
|
82
|
+
//**************************************
|
83
|
+
#include <stdlib.h> // calloc, free
|
84
|
+
#include <string.h> // memset, memcpy
|
85
|
+
#include "lz4hc.h"
|
86
|
+
|
87
|
+
#define ALLOCATOR(s) calloc(1,s)
|
88
|
+
#define FREEMEM free
|
89
|
+
#define MEM_INIT memset
|
90
|
+
|
91
|
+
|
92
|
+
//**************************************
|
93
|
+
// Basic Types
|
94
|
+
//**************************************
|
95
|
+
#if defined(_MSC_VER) // Visual Studio does not support 'stdint' natively
|
96
|
+
#define BYTE unsigned __int8
|
97
|
+
#define U16 unsigned __int16
|
98
|
+
#define U32 unsigned __int32
|
99
|
+
#define S32 __int32
|
100
|
+
#define U64 unsigned __int64
|
101
|
+
#else
|
102
|
+
#include <stdint.h>
|
103
|
+
#define BYTE uint8_t
|
104
|
+
#define U16 uint16_t
|
105
|
+
#define U32 uint32_t
|
106
|
+
#define S32 int32_t
|
107
|
+
#define U64 uint64_t
|
108
|
+
#endif
|
109
|
+
|
110
|
+
#ifndef LZ4_FORCE_UNALIGNED_ACCESS
|
111
|
+
#pragma pack(push, 1)
|
112
|
+
#endif
|
113
|
+
|
114
|
+
typedef struct _U16_S { U16 v; } U16_S;
|
115
|
+
typedef struct _U32_S { U32 v; } U32_S;
|
116
|
+
typedef struct _U64_S { U64 v; } U64_S;
|
117
|
+
|
118
|
+
#ifndef LZ4_FORCE_UNALIGNED_ACCESS
|
119
|
+
#pragma pack(pop)
|
120
|
+
#endif
|
121
|
+
|
122
|
+
#define A64(x) (((U64_S *)(x))->v)
|
123
|
+
#define A32(x) (((U32_S *)(x))->v)
|
124
|
+
#define A16(x) (((U16_S *)(x))->v)
|
125
|
+
|
126
|
+
|
127
|
+
//**************************************
|
128
|
+
// Constants
|
129
|
+
//**************************************
|
130
|
+
#define MINMATCH 4
|
131
|
+
|
132
|
+
#define DICTIONARY_LOGSIZE 16
|
133
|
+
#define MAXD (1<<DICTIONARY_LOGSIZE)
|
134
|
+
#define MAXD_MASK ((U32)(MAXD - 1))
|
135
|
+
#define MAX_DISTANCE (MAXD - 1)
|
136
|
+
|
137
|
+
#define HASH_LOG (DICTIONARY_LOGSIZE-1)
|
138
|
+
#define HASHTABLESIZE (1 << HASH_LOG)
|
139
|
+
#define HASH_MASK (HASHTABLESIZE - 1)
|
140
|
+
|
141
|
+
#define MAX_NB_ATTEMPTS 256
|
142
|
+
|
143
|
+
#define ML_BITS 4
|
144
|
+
#define ML_MASK (size_t)((1U<<ML_BITS)-1)
|
145
|
+
#define RUN_BITS (8-ML_BITS)
|
146
|
+
#define RUN_MASK ((1U<<RUN_BITS)-1)
|
147
|
+
|
148
|
+
#define COPYLENGTH 8
|
149
|
+
#define LASTLITERALS 5
|
150
|
+
#define MFLIMIT (COPYLENGTH+MINMATCH)
|
151
|
+
#define MINLENGTH (MFLIMIT+1)
|
152
|
+
#define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH)
|
153
|
+
|
154
|
+
|
155
|
+
//**************************************
|
156
|
+
// Architecture-specific macros
|
157
|
+
//**************************************
|
158
|
+
#if LZ4_ARCH64 // 64-bit
|
159
|
+
#define STEPSIZE 8
|
160
|
+
#define LZ4_COPYSTEP(s,d) A64(d) = A64(s); d+=8; s+=8;
|
161
|
+
#define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d)
|
162
|
+
#define UARCH U64
|
163
|
+
#define AARCH A64
|
164
|
+
#define HTYPE U32
|
165
|
+
#define INITBASE(b,s) const BYTE* const b = s
|
166
|
+
#else // 32-bit
|
167
|
+
#define STEPSIZE 4
|
168
|
+
#define LZ4_COPYSTEP(s,d) A32(d) = A32(s); d+=4; s+=4;
|
169
|
+
#define LZ4_COPYPACKET(s,d) LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);
|
170
|
+
#define UARCH U32
|
171
|
+
#define AARCH A32
|
172
|
+
#define HTYPE const BYTE*
|
173
|
+
#define INITBASE(b,s) const int b = 0
|
174
|
+
#endif
|
175
|
+
|
176
|
+
#if defined(LZ4_BIG_ENDIAN)
|
177
|
+
#define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = bswap16(v); d = (s) - v; }
|
178
|
+
#define LZ4_WRITE_LITTLEENDIAN_16(p,i) { U16 v = (U16)(i); v = bswap16(v); A16(p) = v; p+=2; }
|
179
|
+
#else // Little Endian
|
180
|
+
#define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }
|
181
|
+
#define LZ4_WRITE_LITTLEENDIAN_16(p,v) { A16(p) = v; p+=2; }
|
182
|
+
#endif
|
183
|
+
|
184
|
+
|
185
|
+
//************************************************************
|
186
|
+
// Local Types
|
187
|
+
//************************************************************
|
188
|
+
typedef struct
|
189
|
+
{
|
190
|
+
const BYTE* base;
|
191
|
+
HTYPE hashTable[HASHTABLESIZE];
|
192
|
+
U16 chainTable[MAXD];
|
193
|
+
const BYTE* nextToUpdate;
|
194
|
+
} LZ4HC_Data_Structure;
|
195
|
+
|
196
|
+
|
197
|
+
//**************************************
|
198
|
+
// Macros
|
199
|
+
//**************************************
|
200
|
+
#define LZ4_WILDCOPY(s,d,e) do { LZ4_COPYPACKET(s,d) } while (d<e);
|
201
|
+
#define LZ4_BLINDCOPY(s,d,l) { BYTE* e=d+l; LZ4_WILDCOPY(s,d,e); d=e; }
|
202
|
+
#define HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASH_LOG))
|
203
|
+
#define HASH_VALUE(p) HASH_FUNCTION(*(U32*)(p))
|
204
|
+
#define HASH_POINTER(p) (HashTable[HASH_VALUE(p)] + base)
|
205
|
+
#define DELTANEXT(p) chainTable[(size_t)(p) & MAXD_MASK]
|
206
|
+
#define GETNEXT(p) ((p) - (size_t)DELTANEXT(p))
|
207
|
+
#define ADD_HASH(p) { size_t delta = (p) - HASH_POINTER(p); if (delta>MAX_DISTANCE) delta = MAX_DISTANCE; DELTANEXT(p) = (U16)delta; HashTable[HASH_VALUE(p)] = (p) - base; }
|
208
|
+
|
209
|
+
|
210
|
+
//**************************************
|
211
|
+
// Private functions
|
212
|
+
//**************************************
|
213
|
+
#if LZ4_ARCH64
|
214
|
+
|
215
|
+
inline static int LZ4_NbCommonBytes (register U64 val)
|
216
|
+
{
|
217
|
+
#if defined(LZ4_BIG_ENDIAN)
|
218
|
+
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
219
|
+
unsigned long r = 0;
|
220
|
+
_BitScanReverse64( &r, val );
|
221
|
+
return (int)(r>>3);
|
222
|
+
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
223
|
+
return (__builtin_clzll(val) >> 3);
|
224
|
+
#else
|
225
|
+
int r;
|
226
|
+
if (!(val>>32)) { r=4; } else { r=0; val>>=32; }
|
227
|
+
if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }
|
228
|
+
r += (!val);
|
229
|
+
return r;
|
230
|
+
#endif
|
231
|
+
#else
|
232
|
+
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
233
|
+
unsigned long r = 0;
|
234
|
+
_BitScanForward64( &r, val );
|
235
|
+
return (int)(r>>3);
|
236
|
+
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
237
|
+
return (__builtin_ctzll(val) >> 3);
|
238
|
+
#else
|
239
|
+
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 };
|
240
|
+
return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];
|
241
|
+
#endif
|
242
|
+
#endif
|
243
|
+
}
|
244
|
+
|
245
|
+
#else
|
246
|
+
|
247
|
+
inline static int LZ4_NbCommonBytes (register U32 val)
|
248
|
+
{
|
249
|
+
#if defined(LZ4_BIG_ENDIAN)
|
250
|
+
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
251
|
+
unsigned long r = 0;
|
252
|
+
_BitScanReverse( &r, val );
|
253
|
+
return (int)(r>>3);
|
254
|
+
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
255
|
+
return (__builtin_clz(val) >> 3);
|
256
|
+
#else
|
257
|
+
int r;
|
258
|
+
if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }
|
259
|
+
r += (!val);
|
260
|
+
return r;
|
261
|
+
#endif
|
262
|
+
#else
|
263
|
+
#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
264
|
+
unsigned long r = 0;
|
265
|
+
_BitScanForward( &r, val );
|
266
|
+
return (int)(r>>3);
|
267
|
+
#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)
|
268
|
+
return (__builtin_ctz(val) >> 3);
|
269
|
+
#else
|
270
|
+
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 };
|
271
|
+
return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
|
272
|
+
#endif
|
273
|
+
#endif
|
274
|
+
}
|
275
|
+
|
276
|
+
#endif
|
277
|
+
|
278
|
+
|
279
|
+
inline static int LZ4HC_Init (LZ4HC_Data_Structure* hc4, const BYTE* base)
|
280
|
+
{
|
281
|
+
MEM_INIT((void*)hc4->hashTable, 0, sizeof(hc4->hashTable));
|
282
|
+
MEM_INIT(hc4->chainTable, 0xFF, sizeof(hc4->chainTable));
|
283
|
+
hc4->nextToUpdate = base + LZ4_ARCH64;
|
284
|
+
hc4->base = base;
|
285
|
+
return 1;
|
286
|
+
}
|
287
|
+
|
288
|
+
|
289
|
+
inline static void* LZ4HC_Create (const BYTE* base)
|
290
|
+
{
|
291
|
+
void* hc4 = ALLOCATOR(sizeof(LZ4HC_Data_Structure));
|
292
|
+
|
293
|
+
LZ4HC_Init (hc4, base);
|
294
|
+
return hc4;
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
inline static int LZ4HC_Free (void** LZ4HC_Data)
|
299
|
+
{
|
300
|
+
FREEMEM(*LZ4HC_Data);
|
301
|
+
*LZ4HC_Data = NULL;
|
302
|
+
return (1);
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
inline static void LZ4HC_Insert (LZ4HC_Data_Structure* hc4, const BYTE* ip)
|
307
|
+
{
|
308
|
+
U16* chainTable = hc4->chainTable;
|
309
|
+
HTYPE* HashTable = hc4->hashTable;
|
310
|
+
INITBASE(base,hc4->base);
|
311
|
+
|
312
|
+
while(hc4->nextToUpdate < ip)
|
313
|
+
{
|
314
|
+
ADD_HASH(hc4->nextToUpdate);
|
315
|
+
hc4->nextToUpdate++;
|
316
|
+
}
|
317
|
+
}
|
318
|
+
|
319
|
+
|
320
|
+
inline static int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, const BYTE* ip, const BYTE* const matchlimit, const BYTE** matchpos)
|
321
|
+
{
|
322
|
+
U16* const chainTable = hc4->chainTable;
|
323
|
+
HTYPE* const HashTable = hc4->hashTable;
|
324
|
+
const BYTE* ref;
|
325
|
+
INITBASE(base,hc4->base);
|
326
|
+
int nbAttempts=MAX_NB_ATTEMPTS;
|
327
|
+
int ml=0;
|
328
|
+
|
329
|
+
// HC4 match finder
|
330
|
+
LZ4HC_Insert(hc4, ip);
|
331
|
+
ref = HASH_POINTER(ip);
|
332
|
+
while ((ref > (ip-MAX_DISTANCE)) && (nbAttempts))
|
333
|
+
{
|
334
|
+
nbAttempts--;
|
335
|
+
if (*(ref+ml) == *(ip+ml))
|
336
|
+
if (*(U32*)ref == *(U32*)ip)
|
337
|
+
{
|
338
|
+
const BYTE* reft = ref+MINMATCH;
|
339
|
+
const BYTE* ipt = ip+MINMATCH;
|
340
|
+
|
341
|
+
while (ipt<matchlimit-(STEPSIZE-1))
|
342
|
+
{
|
343
|
+
UARCH diff = AARCH(reft) ^ AARCH(ipt);
|
344
|
+
if (!diff) { ipt+=STEPSIZE; reft+=STEPSIZE; continue; }
|
345
|
+
ipt += LZ4_NbCommonBytes(diff);
|
346
|
+
goto _endCount;
|
347
|
+
}
|
348
|
+
if (LZ4_ARCH64) if ((ipt<(matchlimit-3)) && (A32(reft) == A32(ipt))) { ipt+=4; reft+=4; }
|
349
|
+
if ((ipt<(matchlimit-1)) && (A16(reft) == A16(ipt))) { ipt+=2; reft+=2; }
|
350
|
+
if ((ipt<matchlimit) && (*reft == *ipt)) ipt++;
|
351
|
+
_endCount:
|
352
|
+
|
353
|
+
if (ipt-ip > ml) { ml = ipt-ip; *matchpos = ref; }
|
354
|
+
}
|
355
|
+
ref = GETNEXT(ref);
|
356
|
+
}
|
357
|
+
|
358
|
+
return ml;
|
359
|
+
}
|
360
|
+
|
361
|
+
|
362
|
+
inline static int LZ4HC_InsertAndGetWiderMatch (LZ4HC_Data_Structure* hc4, const BYTE* ip, const BYTE* startLimit, const BYTE* matchlimit, int longest, const BYTE** matchpos, const BYTE** startpos)
|
363
|
+
{
|
364
|
+
U16* const chainTable = hc4->chainTable;
|
365
|
+
HTYPE* const HashTable = hc4->hashTable;
|
366
|
+
INITBASE(base,hc4->base);
|
367
|
+
const BYTE* ref;
|
368
|
+
int nbAttempts = MAX_NB_ATTEMPTS;
|
369
|
+
int delta = ip-startLimit;
|
370
|
+
|
371
|
+
// First Match
|
372
|
+
LZ4HC_Insert(hc4, ip);
|
373
|
+
ref = HASH_POINTER(ip);
|
374
|
+
|
375
|
+
while ((ref > ip-MAX_DISTANCE) && (ref >= hc4->base) && (nbAttempts))
|
376
|
+
{
|
377
|
+
nbAttempts--;
|
378
|
+
if (*(startLimit + longest) == *(ref - delta + longest))
|
379
|
+
if (*(U32*)ref == *(U32*)ip)
|
380
|
+
{
|
381
|
+
const BYTE* reft = ref+MINMATCH;
|
382
|
+
const BYTE* ipt = ip+MINMATCH;
|
383
|
+
const BYTE* startt = ip;
|
384
|
+
|
385
|
+
while (ipt<matchlimit-(STEPSIZE-1))
|
386
|
+
{
|
387
|
+
UARCH diff = AARCH(reft) ^ AARCH(ipt);
|
388
|
+
if (!diff) { ipt+=STEPSIZE; reft+=STEPSIZE; continue; }
|
389
|
+
ipt += LZ4_NbCommonBytes(diff);
|
390
|
+
goto _endCount;
|
391
|
+
}
|
392
|
+
if (LZ4_ARCH64) if ((ipt<(matchlimit-3)) && (A32(reft) == A32(ipt))) { ipt+=4; reft+=4; }
|
393
|
+
if ((ipt<(matchlimit-1)) && (A16(reft) == A16(ipt))) { ipt+=2; reft+=2; }
|
394
|
+
if ((ipt<matchlimit) && (*reft == *ipt)) ipt++;
|
395
|
+
_endCount:
|
396
|
+
|
397
|
+
reft = ref;
|
398
|
+
while ((startt>startLimit) && (reft > hc4->base) && (startt[-1] == reft[-1])) {startt--; reft--;}
|
399
|
+
|
400
|
+
if ((ipt-startt) > longest)
|
401
|
+
{
|
402
|
+
longest = ipt-startt;
|
403
|
+
*matchpos = reft;
|
404
|
+
*startpos = startt;
|
405
|
+
}
|
406
|
+
}
|
407
|
+
ref = GETNEXT(ref);
|
408
|
+
}
|
409
|
+
|
410
|
+
return longest;
|
411
|
+
}
|
412
|
+
|
413
|
+
|
414
|
+
inline static int LZ4_encodeSequence(const BYTE** ip, BYTE** op, const BYTE** anchor, int ml, const BYTE* ref)
|
415
|
+
{
|
416
|
+
int length, len;
|
417
|
+
BYTE* token;
|
418
|
+
|
419
|
+
// Encode Literal length
|
420
|
+
length = *ip - *anchor;
|
421
|
+
token = (*op)++;
|
422
|
+
if (length>=(int)RUN_MASK) { *token=(RUN_MASK<<ML_BITS); len = length-RUN_MASK; for(; len > 254 ; len-=255) *(*op)++ = 255; *(*op)++ = (BYTE)len; }
|
423
|
+
else *token = (length<<ML_BITS);
|
424
|
+
|
425
|
+
// Copy Literals
|
426
|
+
LZ4_BLINDCOPY(*anchor, *op, length);
|
427
|
+
|
428
|
+
// Encode Offset
|
429
|
+
LZ4_WRITE_LITTLEENDIAN_16(*op,*ip-ref);
|
430
|
+
|
431
|
+
// Encode MatchLength
|
432
|
+
len = (int)(ml-MINMATCH);
|
433
|
+
if (len>=(int)ML_MASK) { *token+=ML_MASK; len-=ML_MASK; for(; len > 509 ; len-=510) { *(*op)++ = 255; *(*op)++ = 255; } if (len > 254) { len-=255; *(*op)++ = 255; } *(*op)++ = (BYTE)len; }
|
434
|
+
else *token += len;
|
435
|
+
|
436
|
+
// Prepare next loop
|
437
|
+
*ip += ml;
|
438
|
+
*anchor = *ip;
|
439
|
+
|
440
|
+
return 0;
|
441
|
+
}
|
442
|
+
|
443
|
+
|
444
|
+
//****************************
|
445
|
+
// Compression CODE
|
446
|
+
//****************************
|
447
|
+
|
448
|
+
int LZ4_compressHCCtx(LZ4HC_Data_Structure* ctx,
|
449
|
+
const char* source,
|
450
|
+
char* dest,
|
451
|
+
int isize)
|
452
|
+
{
|
453
|
+
const BYTE* ip = (const BYTE*) source;
|
454
|
+
const BYTE* anchor = ip;
|
455
|
+
const BYTE* const iend = ip + isize;
|
456
|
+
const BYTE* const mflimit = iend - MFLIMIT;
|
457
|
+
const BYTE* const matchlimit = (iend - LASTLITERALS);
|
458
|
+
|
459
|
+
BYTE* op = (BYTE*) dest;
|
460
|
+
|
461
|
+
int ml, ml2, ml3, ml0;
|
462
|
+
const BYTE* ref=NULL;
|
463
|
+
const BYTE* start2=NULL;
|
464
|
+
const BYTE* ref2=NULL;
|
465
|
+
const BYTE* start3=NULL;
|
466
|
+
const BYTE* ref3=NULL;
|
467
|
+
const BYTE* start0;
|
468
|
+
const BYTE* ref0;
|
469
|
+
|
470
|
+
ip++;
|
471
|
+
|
472
|
+
// Main Loop
|
473
|
+
while (ip < mflimit)
|
474
|
+
{
|
475
|
+
ml = LZ4HC_InsertAndFindBestMatch (ctx, ip, matchlimit, (&ref));
|
476
|
+
if (!ml) { ip++; continue; }
|
477
|
+
|
478
|
+
// saved, in case we would skip too much
|
479
|
+
start0 = ip;
|
480
|
+
ref0 = ref;
|
481
|
+
ml0 = ml;
|
482
|
+
|
483
|
+
_Search2:
|
484
|
+
if (ip+ml < mflimit)
|
485
|
+
ml2 = LZ4HC_InsertAndGetWiderMatch(ctx, ip + ml - 2, ip + 1, matchlimit, ml, &ref2, &start2);
|
486
|
+
else ml2=ml;
|
487
|
+
|
488
|
+
if (ml2 == ml) // No better match
|
489
|
+
{
|
490
|
+
LZ4_encodeSequence(&ip, &op, &anchor, ml, ref);
|
491
|
+
continue;
|
492
|
+
}
|
493
|
+
|
494
|
+
if (start0 < ip)
|
495
|
+
{
|
496
|
+
if (start2 < ip + ml0) // empirical
|
497
|
+
{
|
498
|
+
ip = start0;
|
499
|
+
ref = ref0;
|
500
|
+
ml = ml0;
|
501
|
+
}
|
502
|
+
}
|
503
|
+
|
504
|
+
// Here, start0==ip
|
505
|
+
if ((start2 - ip) < 3) // First Match too small : removed
|
506
|
+
{
|
507
|
+
ml = ml2;
|
508
|
+
ip = start2;
|
509
|
+
ref =ref2;
|
510
|
+
goto _Search2;
|
511
|
+
}
|
512
|
+
|
513
|
+
_Search3:
|
514
|
+
// Currently we have :
|
515
|
+
// ml2 > ml1, and
|
516
|
+
// ip1+3 <= ip2 (usually < ip1+ml1)
|
517
|
+
if ((start2 - ip) < OPTIMAL_ML)
|
518
|
+
{
|
519
|
+
int correction;
|
520
|
+
int new_ml = ml;
|
521
|
+
if (new_ml > OPTIMAL_ML) new_ml = OPTIMAL_ML;
|
522
|
+
if (ip+new_ml > start2 + ml2 - MINMATCH) new_ml = start2 - ip + ml2 - MINMATCH;
|
523
|
+
correction = new_ml - (start2 - ip);
|
524
|
+
if (correction > 0)
|
525
|
+
{
|
526
|
+
start2 += correction;
|
527
|
+
ref2 += correction;
|
528
|
+
ml2 -= correction;
|
529
|
+
}
|
530
|
+
}
|
531
|
+
// Now, we have start2 = ip+new_ml, with new_ml=min(ml, OPTIMAL_ML=18)
|
532
|
+
|
533
|
+
if (start2 + ml2 < mflimit)
|
534
|
+
ml3 = LZ4HC_InsertAndGetWiderMatch(ctx, start2 + ml2 - 3, start2, matchlimit, ml2, &ref3, &start3);
|
535
|
+
else ml3=ml2;
|
536
|
+
|
537
|
+
if (ml3 == ml2) // No better match : 2 sequences to encode
|
538
|
+
{
|
539
|
+
// ip & ref are known; Now for ml
|
540
|
+
if (start2 < ip+ml)
|
541
|
+
{
|
542
|
+
if ((start2 - ip) < OPTIMAL_ML)
|
543
|
+
{
|
544
|
+
int correction;
|
545
|
+
if (ml > OPTIMAL_ML) ml = OPTIMAL_ML;
|
546
|
+
if (ip+ml > start2 + ml2 - MINMATCH) ml = start2 - ip + ml2 - MINMATCH;
|
547
|
+
correction = ml - (start2 - ip);
|
548
|
+
if (correction > 0)
|
549
|
+
{
|
550
|
+
start2 += correction;
|
551
|
+
ref2 += correction;
|
552
|
+
ml2 -= correction;
|
553
|
+
}
|
554
|
+
}
|
555
|
+
else
|
556
|
+
{
|
557
|
+
ml = start2 - ip;
|
558
|
+
}
|
559
|
+
}
|
560
|
+
// Now, encode 2 sequences
|
561
|
+
LZ4_encodeSequence(&ip, &op, &anchor, ml, ref);
|
562
|
+
ip = start2;
|
563
|
+
LZ4_encodeSequence(&ip, &op, &anchor, ml2, ref2);
|
564
|
+
continue;
|
565
|
+
}
|
566
|
+
|
567
|
+
if (start3 < ip+ml+3) // Not enough space for match 2 : remove it
|
568
|
+
{
|
569
|
+
if (start3 >= (ip+ml)) // can write Seq1 immediately ==> Seq2 is removed, so Seq3 becomes Seq1
|
570
|
+
{
|
571
|
+
if (start2 < ip+ml)
|
572
|
+
{
|
573
|
+
int correction = (ip+ml) - start2;
|
574
|
+
start2 += correction;
|
575
|
+
ref2 += correction;
|
576
|
+
ml2 -= correction;
|
577
|
+
if (ml2 < MINMATCH)
|
578
|
+
{
|
579
|
+
start2 = start3;
|
580
|
+
ref2 = ref3;
|
581
|
+
ml2 = ml3;
|
582
|
+
}
|
583
|
+
}
|
584
|
+
|
585
|
+
LZ4_encodeSequence(&ip, &op, &anchor, ml, ref);
|
586
|
+
ip = start3;
|
587
|
+
ref = ref3;
|
588
|
+
ml = ml3;
|
589
|
+
|
590
|
+
start0 = start2;
|
591
|
+
ref0 = ref2;
|
592
|
+
ml0 = ml2;
|
593
|
+
goto _Search2;
|
594
|
+
}
|
595
|
+
|
596
|
+
start2 = start3;
|
597
|
+
ref2 = ref3;
|
598
|
+
ml2 = ml3;
|
599
|
+
goto _Search3;
|
600
|
+
}
|
601
|
+
|
602
|
+
// OK, now we have 3 ascending matches; let's write at least the first one
|
603
|
+
// ip & ref are known; Now for ml
|
604
|
+
if (start2 < ip+ml)
|
605
|
+
{
|
606
|
+
if ((start2 - ip) < (int)ML_MASK)
|
607
|
+
{
|
608
|
+
int correction;
|
609
|
+
if (ml > OPTIMAL_ML) ml = OPTIMAL_ML;
|
610
|
+
if (ip + ml > start2 + ml2 - MINMATCH) ml = start2 - ip + ml2 - MINMATCH;
|
611
|
+
correction = ml - (start2 - ip);
|
612
|
+
if (correction > 0)
|
613
|
+
{
|
614
|
+
start2 += correction;
|
615
|
+
ref2 += correction;
|
616
|
+
ml2 -= correction;
|
617
|
+
}
|
618
|
+
}
|
619
|
+
else
|
620
|
+
{
|
621
|
+
ml = start2 - ip;
|
622
|
+
}
|
623
|
+
}
|
624
|
+
LZ4_encodeSequence(&ip, &op, &anchor, ml, ref);
|
625
|
+
|
626
|
+
ip = start2;
|
627
|
+
ref = ref2;
|
628
|
+
ml = ml2;
|
629
|
+
|
630
|
+
start2 = start3;
|
631
|
+
ref2 = ref3;
|
632
|
+
ml2 = ml3;
|
633
|
+
|
634
|
+
goto _Search3;
|
635
|
+
|
636
|
+
}
|
637
|
+
|
638
|
+
// Encode Last Literals
|
639
|
+
{
|
640
|
+
int lastRun = iend - anchor;
|
641
|
+
if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
|
642
|
+
else *op++ = (lastRun<<ML_BITS);
|
643
|
+
memcpy(op, anchor, iend - anchor);
|
644
|
+
op += iend-anchor;
|
645
|
+
}
|
646
|
+
|
647
|
+
// End
|
648
|
+
return (int) (((char*)op)-dest);
|
649
|
+
}
|
650
|
+
|
651
|
+
|
652
|
+
int LZ4_compressHC(const char* source,
|
653
|
+
char* dest,
|
654
|
+
int isize)
|
655
|
+
{
|
656
|
+
void* ctx = LZ4HC_Create((const BYTE*)source);
|
657
|
+
int result = LZ4_compressHCCtx(ctx, source, dest, isize);
|
658
|
+
LZ4HC_Free (&ctx);
|
659
|
+
|
660
|
+
return result;
|
661
|
+
}
|
662
|
+
|
663
|
+
|