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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 740e7a99360590057c85f528f9b73fd17415283d
|
4
|
+
data.tar.gz: 338ad8c8127e570f13a64ff876d59a547408110c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7c9123137dbc21efd1b803a5ae4d3ad6be6bffef51407f1279abe798ab0a2263ff79cb0d98434f081ed30efc1bd0ec95b6437598a1524ed3de733de17020557
|
7
|
+
data.tar.gz: 88f9ae7c6244c38ba9f28996ed5eb1505a603f9d5c0c16ef2b37ac296b7e1d52d3c3a784c0cf35df7b6fe3dc15453296b7a627720cbebb22490f803b4dc6ca41
|
data/bin/rsrl
CHANGED
@@ -4,7 +4,7 @@ require 'sereal'
|
|
4
4
|
content = ARGF.read
|
5
5
|
compress = Sereal::RAW
|
6
6
|
|
7
|
-
{'
|
7
|
+
{'Sereal_SNAPPY' => Sereal::SNAPPY,'Sereal_SNAPPY_INCR' => Sereal::SNAPPY_INCR }.each do |k,v|
|
8
8
|
if ENV[k]
|
9
9
|
compress = v
|
10
10
|
end
|
data/ext/sereal/decode.c
CHANGED
@@ -218,46 +218,28 @@ again:
|
|
218
218
|
is_compressed = __SNAPPY;
|
219
219
|
else if ((version & SRL_PROTOCOL_ENCODING_MASK) == SRL_PROTOCOL_ENCODING_SNAPPY_INCR)
|
220
220
|
is_compressed = __SNAPPY_INCR;
|
221
|
-
else if ((version & SRL_PROTOCOL_ENCODING_MASK) == SRL_PROTOCOL_ENCODING_LZ4_INCR)
|
222
|
-
is_compressed = __LZ4_INCR;
|
223
221
|
else
|
224
222
|
is_compressed = __RAW;
|
225
223
|
if (is_compressed) {
|
226
224
|
u32 uncompressed_len;
|
227
225
|
u32 compressed_len;
|
228
226
|
|
229
|
-
if (is_compressed ==
|
230
|
-
uncompressed_len = s_get_varint_bang(s);
|
231
|
-
if (!uncompressed_len)
|
232
|
-
rb_raise(rb_eTypeError,"LZ4 compression requires <varint uncompressed size><varint compressed size><blob>, unable to get uncompressed_len");
|
233
|
-
|
227
|
+
if (is_compressed == __SNAPPY_INCR) {
|
234
228
|
compressed_len = s_get_varint_bang(s);
|
235
229
|
} else {
|
236
|
-
|
237
|
-
compressed_len = s_get_varint_bang(s);
|
238
|
-
} else {
|
239
|
-
compressed_len = s->size - s->pos;
|
240
|
-
}
|
241
|
-
|
242
|
-
int snappy_header_len = csnappy_get_uncompressed_length(s_get_p(s),
|
243
|
-
compressed_len,
|
244
|
-
&uncompressed_len);
|
245
|
-
if (snappy_header_len == CSNAPPY_E_HEADER_BAD)
|
246
|
-
rb_raise(rb_eTypeError,"invalid snappy header");
|
230
|
+
compressed_len = s->size - s->pos;
|
247
231
|
}
|
232
|
+
|
233
|
+
int snappy_header_len = csnappy_get_uncompressed_length(s_get_p(s),
|
234
|
+
compressed_len,
|
235
|
+
&uncompressed_len);
|
236
|
+
if (snappy_header_len == CSNAPPY_E_HEADER_BAD)
|
237
|
+
rb_raise(rb_eTypeError,"invalid snappy header");
|
248
238
|
u8 *uncompressed = alloc_or_raise(uncompressed_len);
|
249
|
-
int done
|
250
|
-
if (is_compressed == __LZ4_INCR) {
|
251
|
-
done = LZ4_decompress_safe(s_get_p(s),
|
252
|
-
uncompressed,
|
253
|
-
compressed_len,
|
254
|
-
uncompressed_len) == uncompressed_len ? 1 : 0;
|
255
|
-
} else {
|
256
|
-
done = csnappy_decompress(s_get_p(s),
|
239
|
+
int done = csnappy_decompress(s_get_p(s),
|
257
240
|
compressed_len,
|
258
241
|
uncompressed,
|
259
242
|
uncompressed_len) == CSNAPPY_E_OK ? 1 : 0;
|
260
|
-
}
|
261
243
|
if (!done)
|
262
244
|
s_raise(s,rb_eTypeError, "decompression failed error: %d type: %d, unompressed size: %d compressed size: %d",done,is_compressed,uncompressed_len,compressed_len);
|
263
245
|
|
data/ext/sereal/encode.c
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
#include "buffer.h"
|
3
3
|
#include "encode.h"
|
4
4
|
#include "snappy/csnappy_compress.c"
|
5
|
-
#include "lz4/lz4.h"
|
6
|
-
#include "lz4/lz4hc.h"
|
7
5
|
#define W_SIZE 32
|
8
6
|
#if T_FIXNUM > W_SIZE
|
9
7
|
#define W_SIZE T_FIXNUM
|
@@ -265,10 +263,6 @@ VALUE method_sereal_encode(VALUE self, VALUE args) {
|
|
265
263
|
case __SNAPPY_INCR:
|
266
264
|
version |= SRL_PROTOCOL_ENCODING_SNAPPY_INCR;
|
267
265
|
break;
|
268
|
-
case __LZ4HC_INCR:
|
269
|
-
case __LZ4_INCR:
|
270
|
-
version |= SRL_PROTOCOL_ENCODING_LZ4_INCR;
|
271
|
-
break;
|
272
266
|
case __RAW:
|
273
267
|
default:
|
274
268
|
version |= SRL_PROTOCOL_ENCODING_RAW;
|
@@ -293,15 +287,9 @@ VALUE method_sereal_encode(VALUE self, VALUE args) {
|
|
293
287
|
// lz4 <varint uncompressed len><varint blob len><compressed blob>
|
294
288
|
if (do_compress == __SNAPPY || do_compress == __SNAPPY_INCR) {
|
295
289
|
compressed_len = csnappy_max_compressed_length(s_body_len);
|
296
|
-
} else {
|
297
|
-
compressed_len = LZ4_compressBound(s_body_len);
|
298
|
-
start_un_compressed_varint = s_get_p(s);
|
299
|
-
s_append_varint(s,s_body_len);
|
300
|
-
end = s_get_p(s);
|
301
|
-
un_compressed_len_varint = end - start_un_compressed_varint;
|
302
290
|
}
|
303
291
|
|
304
|
-
if (do_compress == __SNAPPY_INCR
|
292
|
+
if (do_compress == __SNAPPY_INCR) {
|
305
293
|
start_compressed_varint = s_get_p(s);
|
306
294
|
s_append_varint(s,compressed_len);
|
307
295
|
end = s_get_p(s);
|
@@ -321,26 +309,15 @@ VALUE method_sereal_encode(VALUE self, VALUE args) {
|
|
321
309
|
compressed_len_varint);
|
322
310
|
|
323
311
|
u8 *start = s_get_p_at_pos(s,s_header_len,1);
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
} else {
|
334
|
-
u8 *working_buf = alloc_or_raise(CSNAPPY_WORKMEM_BYTES);
|
335
|
-
csnappy_compress(start,
|
336
|
-
s_body_len,
|
337
|
-
(compressed + s_header_len + compressed_len_varint + un_compressed_len_varint),
|
338
|
-
&compressed_len,
|
339
|
-
working_buf,
|
340
|
-
CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
|
341
|
-
|
342
|
-
free(working_buf);
|
343
|
-
}
|
312
|
+
u8 *working_buf = alloc_or_raise(CSNAPPY_WORKMEM_BYTES);
|
313
|
+
csnappy_compress(start,
|
314
|
+
s_body_len,
|
315
|
+
(compressed + s_header_len + compressed_len_varint + un_compressed_len_varint),
|
316
|
+
&compressed_len,
|
317
|
+
working_buf,
|
318
|
+
CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
|
319
|
+
|
320
|
+
free(working_buf);
|
344
321
|
if (compressed_len == 0)
|
345
322
|
s_raise(s,rb_eTypeError,"failed to compress");
|
346
323
|
if (start_compressed_varint)
|
data/ext/sereal/proto.h
CHANGED
@@ -9,7 +9,6 @@
|
|
9
9
|
#define SRL_PROTOCOL_ENCODING_RAW ( 0 << SRL_PROTOCOL_VERSION_BITS )
|
10
10
|
#define SRL_PROTOCOL_ENCODING_SNAPPY ( 1 << SRL_PROTOCOL_VERSION_BITS )
|
11
11
|
#define SRL_PROTOCOL_ENCODING_SNAPPY_INCR ( 2 << SRL_PROTOCOL_VERSION_BITS )
|
12
|
-
#define SRL_PROTOCOL_ENCODING_LZ4_INCR ( 3 << SRL_PROTOCOL_VERSION_BITS )
|
13
12
|
|
14
13
|
#define SRL_HDR_POS ((char)0) /* small positive integer - value in low 4 bits (identity) */
|
15
14
|
#define SRL_HDR_POS_LOW ((char)0) /* small positive integer - value in low 4 bits (identity) */
|
data/ext/sereal/sereal.c
CHANGED
@@ -9,17 +9,15 @@ void Init_sereal();
|
|
9
9
|
* https://github.com/Sereal/Sereal/blob/master/sereal_spec.pod
|
10
10
|
*
|
11
11
|
* Sereal.encode(object) -> serialized blob
|
12
|
-
* Sereal.encode(object,Sereal::LZ4) -> LZ4 compressed blob
|
13
|
-
* Sereal.encode(object,Sereal::LZ4HC) -> LZ4HC compressed blob
|
14
12
|
* Sereal.encode(object,Sereal::SNAPPY_INCR) -> snappy compressed blob
|
15
13
|
* Sereal.encode(object,Sereal::SNAPPY) -> snappy compressed blob
|
16
14
|
*
|
17
|
-
*
|
15
|
+
* SNAPPY_INCR encoded objects can be appended into one output and then the
|
18
16
|
* decoder will know what to do.
|
19
17
|
*
|
20
18
|
* Sereal.decode(blob) - returns the decoded object
|
21
19
|
*
|
22
|
-
* If the blob contains multiple compressed
|
20
|
+
* If the blob contains multiple compressed
|
23
21
|
* sub-blobs you should call it with:
|
24
22
|
*
|
25
23
|
* Sereal.decode(blob) do |decoded|
|
@@ -35,8 +33,6 @@ void Init_sereal() {
|
|
35
33
|
rb_define_const(Sereal, "SNAPPY",INT2NUM(__SNAPPY));
|
36
34
|
rb_define_const(Sereal, "SNAPPY_INCR",INT2NUM(__SNAPPY_INCR));
|
37
35
|
rb_define_const(Sereal, "RAW",INT2NUM(__RAW));
|
38
|
-
rb_define_const(Sereal, "LZ4",INT2NUM(__LZ4_INCR));
|
39
|
-
rb_define_const(Sereal, "LZ4HC",INT2NUM(__LZ4HC_INCR));
|
40
36
|
s_init_writers();
|
41
37
|
}
|
42
38
|
|
data/ext/sereal/sereal.h
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sereal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Borislav Nikolov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|
@@ -34,23 +34,15 @@ extra_rdoc_files: []
|
|
34
34
|
files:
|
35
35
|
- ext/sereal/decode.c
|
36
36
|
- ext/sereal/encode.c
|
37
|
-
- ext/sereal/lz4/lz4.c
|
38
|
-
- ext/sereal/lz4/lz4hc.c
|
39
37
|
- ext/sereal/snappy/csnappy_compress.c
|
40
38
|
- ext/sereal/snappy/csnappy_decompress.c
|
41
|
-
- ext/sereal/lz4.c
|
42
|
-
- ext/sereal/lz4hc.c
|
43
39
|
- ext/sereal/sereal.c
|
44
|
-
- ext/sereal/lz4hc.h
|
45
40
|
- ext/sereal/sereal.h
|
46
|
-
- ext/sereal/lz4/lz4hc.h
|
47
|
-
- ext/sereal/lz4/lz4.h
|
48
41
|
- ext/sereal/buffer.h
|
49
42
|
- ext/sereal/snappy/csnappy_internal.h
|
50
43
|
- ext/sereal/snappy/csnappy.h
|
51
44
|
- ext/sereal/snappy/csnappy_internal_userspace.h
|
52
45
|
- ext/sereal/snappy/csnappy_compat.h
|
53
|
-
- ext/sereal/lz4.h
|
54
46
|
- ext/sereal/decode.h
|
55
47
|
- ext/sereal/proto.h
|
56
48
|
- ext/sereal/encode.h
|
data/ext/sereal/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
|
-
|