sereal 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 802e13eb6a0c49314803629027fd015345366f57
4
- data.tar.gz: cbf309f9f930d9943e39851b48f5292832495e66
3
+ metadata.gz: b17ebc8e9c34db04bd5e21e712925a91c6e03aa6
4
+ data.tar.gz: f0984c679f1c7a7053f961d1d211ce5160c76375
5
5
  SHA512:
6
- metadata.gz: 753e34f2e15086c4c4ec707ec8bfda05df122c237ac2bd18b992132a470a9475039753c9383cb0ffcaf43289802a5c9e513c0f4ed5026165ea38f59bb62a19d5
7
- data.tar.gz: ba290f161d4c651e5b0a783fff75b22d8c356e8019f9a706ff8a4a1a7ec8797fe8b53ed285b5c306289c1d8ba6ac9d575b5b881d1b6e0bf19371a164f030b296
6
+ metadata.gz: d7d044c11e0c06e24d3d5ab7fe131761bc8031a85030e23bd693e633d2e8ab842e52d4f76665314b1ae155eafa812e74abf26c13d91582ed24c939c476342fc9
7
+ data.tar.gz: 71ab51d02f317100e0d9fe613227e4e1d26856d6b1c0374ff8af223867d8ceefae71712be8f4d04ce9dc7b3023e7809850511a85b26a61b802f1a13aa02a3996
@@ -16,7 +16,7 @@ static inline sereal_t * s_create(void) {
16
16
  }
17
17
 
18
18
  static inline void s_destroy(sereal_t *s) {
19
- if (s->data)
19
+ if (s->data && (s->flags & FLAG_NOT_MINE) == 0)
20
20
  free(s->data);
21
21
 
22
22
  free(s);
@@ -76,14 +76,21 @@ static inline u32 s_shift_position_bang(sereal_t *s, u32 len) {
76
76
  s->pos += len;
77
77
  return len;
78
78
  }
79
- static inline void s_dump(sereal_t *s) {
80
- u32 i;
81
- for (i = 0; i < s->size; i++) {
82
- if (i == s->pos)
83
- fprintf(stderr," [%c %d] ",s->data[i],s->data[i]);
79
+
80
+ static void b_dump(u8 *p, u32 len, u32 pos) {
81
+ int i;
82
+
83
+ fprintf(stderr,"\n-----------\n");
84
+ for (i = 0; i < len; i++) {
85
+ if (i == pos)
86
+ fprintf(stderr," [%c %d] ",p[i],p[i]);
84
87
  else
85
- fprintf(stderr," (%c %d) ",s->data[i],s->data[i]);
88
+ fprintf(stderr," (%c %d) ",p[i],p[i]);
86
89
  }
87
- fprintf(stderr,"\n");
90
+ fprintf(stderr,"\n-----------\n");
91
+ }
92
+
93
+ static void s_dump(sereal_t *s) {
94
+ b_dump(s->data,s->size,s->pos);
88
95
  }
89
96
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  static VALUE s_default_reader(sereal_t *s, u8 tag) {
7
7
  // s_dump(s);
8
- rb_raise(rb_eTypeError,"unsupported tag %d",tag);
8
+ s_raise(s,rb_eTypeError,"unsupported tag %d",tag);
9
9
  return Qnil;
10
10
  }
11
11
 
@@ -16,8 +16,8 @@ static u64 s_get_varint_bang(sereal_t *s) {
16
16
  while (s_get_u8(s) & 0x80) {
17
17
  uv |= ((u64)(s_get_u8_bang(s) & 0x7F) << lshift);
18
18
  lshift += 7;
19
- if (lshift > (sizeof(uv) * 8))
20
- rb_raise(rb_eTypeError, "varint too big");
19
+ if (lshift > (sizeof(uv) * 8))
20
+ s_raise(s,rb_eTypeError, "varint too big");
21
21
  }
22
22
  uv |= ((u64)(s_get_u8_bang(s)) << lshift);
23
23
  return uv;
@@ -128,7 +128,7 @@ static VALUE s_read_rb_string_bang(sereal_t *s,u8 t) {
128
128
  rb_str_new(s_get_p(s), len));
129
129
  }
130
130
  #undef RETURN_STRING
131
- rb_raise(rb_eTypeError, "undefined string type %d",t);
131
+ s_raise(s,rb_eTypeError, "undefined string type %d",t);
132
132
  }
133
133
 
134
134
  static VALUE s_read_next_rb_string_bang(sereal_t *s) {
@@ -172,7 +172,7 @@ static VALUE s_read_pad(sereal_t *s, u8 tag) {
172
172
  return sereal_to_rb_object(s);
173
173
  }
174
174
  static VALUE s_read_extend(sereal_t *s, u8 tag) {
175
- rb_raise(rb_eArgError,"extend tags are not supported");
175
+ s_raise(s,rb_eArgError,"extend tags are not supported");
176
176
  }
177
177
 
178
178
  VALUE sereal_to_rb_object(sereal_t *s) {
@@ -181,7 +181,7 @@ VALUE sereal_to_rb_object(sereal_t *s) {
181
181
  while (s->pos < s->size) {
182
182
  t = s_get_u8_bang(s);
183
183
  if (t & SRL_HDR_TRACK_FLAG)
184
- rb_raise(rb_eArgError, "trackable objects are not supported");
184
+ s_raise(s,rb_eArgError, "trackable objects are not supported");
185
185
  S_RECURSE_DEC(s);
186
186
  return (*READERS[t])(s,t);
187
187
  }
@@ -193,46 +193,90 @@ VALUE method_sereal_decode(VALUE self, VALUE args) {
193
193
  if (argc < 1)
194
194
  rb_raise(rb_eArgError,"need at least 1 argument (object)");
195
195
  VALUE payload = rb_ary_shift(args);
196
- VALUE safe = Qtrue;
197
196
  if (TYPE(payload) != T_STRING)
198
197
  rb_raise(rb_eTypeError,"can not decode objects of type %s",rb_obj_classname(payload));
199
198
 
200
199
  sereal_t *s = s_create();
201
- s->data = RSTRING_PTR(payload);
202
- s->size = RSTRING_LEN(payload);
203
-
204
- if (s->size < 6 || s_get_u32_bang(s) != 0x6c72733d)
205
- rb_raise(rb_eTypeError,"invalid header");
200
+ u64 offset = 0;
201
+ again:
202
+ if (offset >= RSTRING_LEN(payload)) {
203
+ free(s);
204
+ return Qnil;
205
+ }
206
+ s->flags |= FLAG_NOT_MINE;
207
+ s->data = RSTRING_PTR(payload) + offset;
208
+ s->size = RSTRING_LEN(payload) - offset;
209
+ s->pos = 0;
210
+ if (s->size < 6 || s_get_u32_bang(s) != SRL_MAGIC_STRING_LILIPUTIAN)
211
+ s_raise(s,rb_eTypeError,"invalid header");
206
212
 
207
213
  u8 version = s_get_u8_bang(s);
208
214
  u8 suffix = s_get_varint_bang(s);
209
- int is_compressed = (version & SRL_PROTOCOL_ENCODING_MASK) == SRL_PROTOCOL_ENCODING_SNAPPY ? TRUE : FALSE;
215
+ u8 is_compressed;
210
216
 
217
+ if ((version & SRL_PROTOCOL_ENCODING_MASK) == SRL_PROTOCOL_ENCODING_SNAPPY)
218
+ is_compressed = __SNAPPY;
219
+ else if ((version & SRL_PROTOCOL_ENCODING_MASK) == SRL_PROTOCOL_ENCODING_SNAPPY_INCR)
220
+ is_compressed = __SNAPPY_INCR;
221
+ else if ((version & SRL_PROTOCOL_ENCODING_MASK) == SRL_PROTOCOL_ENCODING_LZ4_INCR)
222
+ is_compressed = __LZ4_INCR;
223
+ else
224
+ is_compressed = __RAW;
211
225
  if (is_compressed) {
212
226
  u32 uncompressed_len;
213
- int snappy_header_len = csnappy_get_uncompressed_length(s_get_p(s),
214
- (s->size - s->pos),
215
- &uncompressed_len);
216
- if (snappy_header_len == CSNAPPY_E_HEADER_BAD)
217
- rb_raise(rb_eTypeError,"invalid snappy header");
227
+ u32 compressed_len;
228
+
229
+ if (is_compressed == __LZ4_INCR) {
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
+
234
+ compressed_len = s_get_varint_bang(s);
235
+ } else {
236
+ if (is_compressed == __SNAPPY_INCR) {
237
+ compressed_len = s_get_varint_bang(s);
238
+ } else {
239
+ compressed_len = s->size - s->pos;
240
+ }
218
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");
247
+ }
219
248
  u8 *uncompressed = alloc_or_raise(uncompressed_len);
220
- int done = csnappy_decompress(s_get_p(s),
221
- (s->size - s->pos),
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),
257
+ compressed_len,
222
258
  uncompressed,
223
- uncompressed_len);
224
- if (done)
225
- rb_raise(rb_eTypeError, "snappy decompression failed error: %d",done);
259
+ uncompressed_len) == CSNAPPY_E_OK ? 1 : 0;
260
+ }
261
+ if (!done)
262
+ 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
+
264
+ offset += s->pos + compressed_len;
226
265
  s->data = uncompressed;
227
266
  s->size = uncompressed_len;
228
267
  s->pos = 0;
268
+ s->flags &= ~FLAG_NOT_MINE;
229
269
  }
230
270
 
231
- VALUE result = sereal_to_rb_object(s);
232
- if (is_compressed)
233
- s_destroy(s);
234
- else
235
- free(s); // we do not destroy because it will free s->data which is RSTRING_PTR(payload)
271
+ VALUE result = sereal_to_rb_object(s);
272
+ if (is_compressed && rb_block_given_p()) {
273
+ free(s->data);
274
+ s->data = NULL;
275
+ rb_yield(result);
276
+ goto again;
277
+ }
278
+ s_destroy(s);
279
+
236
280
  return result;
237
281
  }
238
282
 
@@ -2,7 +2,8 @@
2
2
  #include "buffer.h"
3
3
  #include "encode.h"
4
4
  #include "snappy/csnappy_compress.c"
5
-
5
+ #include "lz4/lz4.h"
6
+ #include "lz4/lz4hc.h"
6
7
  #define W_SIZE 32
7
8
  #if T_FIXNUM > W_SIZE
8
9
  #define W_SIZE T_FIXNUM
@@ -70,7 +71,7 @@ void s_init_writers(void) {
70
71
  }
71
72
 
72
73
  static void s_default_writer(sereal_t *s, VALUE object) {
73
- rb_raise(rb_eTypeError, "invalid type for input %s",rb_obj_classname(object));
74
+ s_raise(s,rb_eTypeError, "invalid type for input %s",rb_obj_classname(object));
74
75
  }
75
76
 
76
77
 
@@ -222,46 +223,134 @@ static void rb_object_to_sereal(sereal_t *s, VALUE object) {
222
223
  S_RECURSE_DEC(s);
223
224
  }
224
225
 
226
+ // https://github.com/Sereal/Sereal/blob/master/Perl/Encoder/srl_encoder.c#L623
227
+ void fixup_varint_from_to(u8 *varint_start, u8 *varint_end, u32 number) {
228
+ while (number >= 0x80) {
229
+ *varint_start++ = (number & 0x7f) | 0x80;
230
+ number = number >> 7;
231
+ }
232
+ if ( varint_start == varint_end ) {
233
+ *varint_start = number;
234
+ } else {
235
+ *varint_start++ = (number & 0x7f) | 0x80;
236
+ while ( varint_start < varint_end )
237
+ *varint_start++ = 0x80;
238
+ *varint_start= 0;
239
+ }
240
+ }
241
+
225
242
  VALUE method_sereal_encode(VALUE self, VALUE args) {
226
- sereal_t *s = s_create();
227
243
  u32 argc = RARRAY_LEN(args);
228
244
  if (argc < 1)
229
245
  rb_raise(rb_eArgError,"need at least 1 argument (object)");
246
+
247
+ sereal_t *s = s_create();
230
248
  VALUE payload = rb_ary_shift(args);
231
249
  VALUE compress = Qfalse;
232
250
  if (argc == 2)
233
251
  compress = rb_ary_shift(args);
234
- int do_compress = (compress == Qtrue ? TRUE : FALSE);
252
+ u8 do_compress;
253
+ u8 version = SRL_PROTOCOL_VERSION;
235
254
 
255
+ if (TYPE(compress) == T_FIXNUM) {
256
+ do_compress = (u8) FIX2LONG(compress);
257
+ } else {
258
+ do_compress = (compress == Qtrue ? 1 : 0);
259
+ }
260
+
261
+ switch(do_compress) {
262
+ case __SNAPPY:
263
+ version |= SRL_PROTOCOL_ENCODING_SNAPPY;
264
+ break;
265
+ case __SNAPPY_INCR:
266
+ version |= SRL_PROTOCOL_ENCODING_SNAPPY_INCR;
267
+ break;
268
+ case __LZ4HC_INCR:
269
+ case __LZ4_INCR:
270
+ version |= SRL_PROTOCOL_ENCODING_LZ4_INCR;
271
+ break;
272
+ case __RAW:
273
+ default:
274
+ version |= SRL_PROTOCOL_ENCODING_RAW;
275
+ }
236
276
  // setup header
237
277
  s_append_u32(s,SRL_MAGIC_STRING_LILIPUTIAN);
238
- s_append_u8(s,SRL_PROTOCOL_VERSION | (do_compress ? SRL_PROTOCOL_ENCODING_SNAPPY : SRL_PROTOCOL_ENCODING_RAW));
278
+ s_append_u8(s,version);
239
279
  s_append_u8(s,0x0);
240
280
  u32 s_header_len = s->pos;
241
281
 
242
282
  // serialize
243
283
  rb_object_to_sereal(s,payload);
244
-
245
284
  // compress
246
285
  if (do_compress) {
286
+ u8 *start_compressed_varint = NULL, *start_un_compressed_varint= NULL, *compressed, *end;
287
+ u32 compressed_len = 0;
288
+ u32 compressed_len_varint = 0;
289
+ u32 un_compressed_len_varint = 0;
247
290
  u32 s_body_len = s->size - s_header_len;
248
- u32 compressed_len = csnappy_max_compressed_length(s_body_len);
291
+ // snappy <compressed blob>
292
+ // snappy incr <varint blob len><compressed blob>
293
+ // lz4 <varint uncompressed len><varint blob len><compressed blob>
294
+ if (do_compress == __SNAPPY || do_compress == __SNAPPY_INCR) {
295
+ 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
+ }
249
303
 
250
- u8 *working_buf = alloc_or_raise(CSNAPPY_WORKMEM_BYTES);
251
- u8 *compressed = alloc_or_raise(compressed_len + s_header_len);
304
+ if (do_compress == __SNAPPY_INCR || do_compress == __LZ4_INCR || do_compress == __LZ4HC_INCR) {
305
+ start_compressed_varint = s_get_p(s);
306
+ s_append_varint(s,compressed_len);
307
+ end = s_get_p(s);
308
+ compressed_len_varint = end - start_compressed_varint;
309
+ }
252
310
 
311
+ compressed = alloc_or_raise(compressed_len + s_header_len + compressed_len_varint + un_compressed_len_varint);
253
312
  COPY(s_get_p_at_pos(s,0,0),compressed,s_header_len);
254
-
255
- csnappy_compress(s_get_p_at_pos(s,s_header_len,1),
256
- s_body_len,
257
- (compressed + s_header_len),
258
- &compressed_len,
259
- working_buf,
260
- CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
313
+
314
+ if (start_un_compressed_varint)
315
+ COPY(start_un_compressed_varint,
316
+ compressed + s_header_len,
317
+ un_compressed_len_varint);
318
+ if (start_compressed_varint)
319
+ COPY(start_compressed_varint,
320
+ compressed + s_header_len + un_compressed_len_varint,
321
+ compressed_len_varint);
322
+
323
+ u8 *start = s_get_p_at_pos(s,s_header_len,1);
324
+ if (do_compress == __LZ4_INCR) {
325
+ compressed_len = LZ4_compress(start,
326
+ (compressed + s_header_len + compressed_len_varint + un_compressed_len_varint),
327
+ s_body_len);
328
+ } else if (do_compress == __LZ4HC_INCR) {
329
+ compressed_len = LZ4_compressHC(start,
330
+ (compressed + s_header_len + compressed_len_varint + un_compressed_len_varint),
331
+ s_body_len);
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
+ }
344
+ if (compressed_len == 0)
345
+ s_raise(s,rb_eTypeError,"failed to compress");
346
+ if (start_compressed_varint)
347
+ fixup_varint_from_to(compressed + s_header_len + un_compressed_len_varint,
348
+ compressed + s_header_len + un_compressed_len_varint + compressed_len_varint - 1,
349
+ compressed_len);
261
350
  free(s->data);
262
- free(working_buf);
263
351
  s->data = compressed;
264
- s->size = compressed_len + s_header_len;
352
+ s->size = compressed_len + s_header_len + compressed_len_varint + un_compressed_len_varint;
353
+ s->pos = s->size;
265
354
  }
266
355
 
267
356
  VALUE result = rb_str_new(s->data,s->size);
@@ -0,0 +1,822 @@
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
+