fastlz 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,46 @@
1
+ = FastLZ/Ruby
2
+
3
+ Copyright (c) 2011 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
4
+
5
+ == Description
6
+
7
+ Ruby bindings for FastLZ.
8
+
9
+ FastLZ is very fast compression library.
10
+
11
+ == Source Code
12
+
13
+ https://bitbucket.org/winebarrel/fastlz-ruby
14
+
15
+ == Install
16
+
17
+ gem install fastlz
18
+
19
+ == Example
20
+
21
+ require 'fastlz'
22
+
23
+ source = <<-EOS
24
+ London Bridge Is falling down,
25
+ Falling down, Falling down.
26
+ London Bridge Is falling down,
27
+ My fair lady.
28
+ EOS
29
+
30
+ comp_data = FastLZ.compress(source)
31
+ decomp_data = FastLZ.decompress(comp_data)
32
+
33
+ puts <<-EOS
34
+ - block compress -
35
+ uncompress size: #{source.length}
36
+ compress size: #{comp_data.length}
37
+ decompress size: #{decomp_data.length}
38
+ decompress success?: #{source == decomp_data}
39
+ EOS
40
+
41
+ === FastLZ
42
+
43
+ FastLZ/Ruby contains FastLZ.
44
+
45
+ * http://fastlz.org/
46
+ * Copyright (C) 2006-2007 Ariya Hidayat
@@ -0,0 +1,24 @@
1
+ FastLZ - lightning-fast lossless compression library
2
+
3
+ Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
4
+ Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
5
+ Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
@@ -0,0 +1,75 @@
1
+ FastLZ - lightning-fast lossless compression library
2
+
3
+ Author: Ariya Hidayat
4
+ Official website: http://www.fastlz.org
5
+
6
+ FastLZ is distributed using the MIT license, see file LICENSE
7
+ for details.
8
+
9
+ FastLZ consists of two files: fastlz.h and fastlz.c. Just add these
10
+ files to your project in order to use FastLZ. For information on
11
+ compression and decompression routines, see fastlz.h.
12
+
13
+ A simple file compressor called 6pack is included as an example
14
+ on how to use FastLZ. The corresponding decompressor is 6unpack.
15
+
16
+ To compile using GCC:
17
+
18
+ gcc -o 6pack 6pack.c fastlz.c
19
+ gcc -o 6unpack 6unpack.c fastlz.c
20
+
21
+ To compile using MinGW:
22
+
23
+ mingw32-gcc -o 6pack 6pack.c fastlz.c
24
+ mingw32-gcc -o 6unpack 6unpack.c fastlz.c
25
+
26
+ To compile using Microsoft Visual C++:
27
+
28
+ cl 6pack.c fastlz.c
29
+ cl 6unpack.c fastlz.c
30
+
31
+ To compile using Borland C++:
32
+
33
+ bcc32 6pack.c fastlz.c
34
+ bcc32 6unpack.c fastlz.c
35
+
36
+ To compile using OpenWatcom C/C++:
37
+
38
+ cl386 6pack.c fastlz.c
39
+ cl386 6unpack.c fastlz.c
40
+
41
+ To compile using Intel C++ compiler for Windows:
42
+
43
+ icl 6pack.c fastlz.c
44
+ icl 6unpack.c fastlz.c
45
+
46
+ To compile using Intel C++ compiler for Linux:
47
+
48
+ icc -o 6pack 6pack.c fastlz.c
49
+ icc -o 6unpack 6unpack.c fastlz.c
50
+
51
+ To compile 6pack using LCC-Win32:
52
+
53
+ lc 6pack.c fastlz.c
54
+ lc 6unpack.c fastlz.c
55
+
56
+ To compile 6pack using Pelles C:
57
+
58
+ pocc 6pack.c
59
+ pocc 6unpack.c
60
+ pocc fastlz.c
61
+ polink 6pack.obj fastlz.obj
62
+ polink 6unpack.obj fastlz.obj
63
+
64
+ For speed optimization, always use proper compile flags for optimization options.
65
+ Typical compiler flags are given below:
66
+
67
+ * GCC (pre 4.2): -march=pentium -O3 -fomit-frame-pointer -mtune=pentium
68
+ * GCC 4.2 or later: -march=pentium -O3 -fomit-frame-pointer -mtune=generic
69
+ * Digital Mars C/C++: -o+all -5
70
+ * Intel C++ (Windows): /O3 /Qipo
71
+ * Intel C++ (Linux): -O2 -march=pentium -mtune=pentium
72
+ * Borland C++: -O2 -5
73
+ * LCC-Win32: -O
74
+ * Pelles C: /O2
75
+
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('fastlz')
@@ -0,0 +1,81 @@
1
+ #ifdef _WIN32
2
+ __declspec(dllexport) void Init_fastlz(void);
3
+ #include <malloc.h>
4
+ #else
5
+ #include <alloca.h>
6
+ #endif
7
+
8
+ #include "fastlz.h"
9
+ #include "ruby.h"
10
+
11
+ #ifndef RSTRING_PTR
12
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
13
+ #endif
14
+
15
+ #ifndef RSTRING_LEN
16
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
17
+ #endif
18
+
19
+ /* */
20
+ static VALUE rb_fastlz_compress(int argc, VALUE *argv, VALUE self) {
21
+ VALUE v_src, v_level;
22
+ char *src, *dst;
23
+ int slen, dlen, level;
24
+
25
+ rb_scan_args(argc, argv, "11", &v_src, &v_level);
26
+ Check_Type(v_src, T_STRING);
27
+
28
+ if (NIL_P(v_level)) {
29
+ level = 0;
30
+ } else {
31
+ level = NUM2INT(v_level);
32
+
33
+ if (level != 1 || level != 2) {
34
+ rb_raise(rb_eRuntimeError, "only level 1 and level 2 are supported");
35
+ }
36
+ }
37
+
38
+ src = RSTRING_PTR(v_src);
39
+ slen = RSTRING_LEN(v_src);
40
+
41
+ dst = alloca(slen * 2);
42
+
43
+ if (level) {
44
+ dlen = fastlz_compress_level(level, src, slen, dst);
45
+ } else {
46
+ dlen = fastlz_compress(src, slen, dst);
47
+ }
48
+
49
+ return rb_str_new(dst, dlen);
50
+ }
51
+
52
+ /* */
53
+ static VALUE rb_fastlz_decompress(int argc, VALUE *argv, VALUE self) {
54
+ VALUE v_src, v_max;
55
+ char *src, *dst;
56
+ int slen, dlen, max;
57
+
58
+ rb_scan_args(argc, argv, "11", &v_src, &v_max);
59
+ Check_Type(v_src, T_STRING);
60
+
61
+ src = RSTRING_PTR(v_src);
62
+ slen = RSTRING_LEN(v_src);
63
+
64
+ if (NIL_P(v_max)) {
65
+ max = slen * 4;
66
+ } else {
67
+ max = NUM2INT(v_max);
68
+ }
69
+
70
+ dst = alloca(max + 1);
71
+
72
+ dlen = fastlz_decompress(src, slen, dst, max);
73
+
74
+ return rb_str_new(dst, dlen);
75
+ }
76
+
77
+ void Init_fastlz() {
78
+ VALUE FastLZ = rb_define_module("FastLZ");
79
+ rb_define_module_function(FastLZ, "compress", rb_fastlz_compress, -1);
80
+ rb_define_module_function(FastLZ, "decompress", rb_fastlz_decompress, -1);
81
+ }
@@ -0,0 +1,551 @@
1
+ /*
2
+ FastLZ - lightning-fast lossless compression library
3
+
4
+ Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
5
+ Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
6
+ Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
25
+ */
26
+
27
+ #if !defined(FASTLZ__COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR)
28
+
29
+ /*
30
+ * Always check for bound when decompressing.
31
+ * Generally it is best to leave it defined.
32
+ */
33
+ #define FASTLZ_SAFE
34
+
35
+ /*
36
+ * Give hints to the compiler for branch prediction optimization.
37
+ */
38
+ #if defined(__GNUC__) && (__GNUC__ > 2)
39
+ #define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
40
+ #define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
41
+ #else
42
+ #define FASTLZ_EXPECT_CONDITIONAL(c) (c)
43
+ #define FASTLZ_UNEXPECT_CONDITIONAL(c) (c)
44
+ #endif
45
+
46
+ /*
47
+ * Use inlined functions for supported systems.
48
+ */
49
+ #if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C)
50
+ #define FASTLZ_INLINE inline
51
+ #elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__)
52
+ #define FASTLZ_INLINE __inline
53
+ #else
54
+ #define FASTLZ_INLINE
55
+ #endif
56
+
57
+ /*
58
+ * Prevent accessing more than 8-bit at once, except on x86 architectures.
59
+ */
60
+ #if !defined(FASTLZ_STRICT_ALIGN)
61
+ #define FASTLZ_STRICT_ALIGN
62
+ #if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */
63
+ #undef FASTLZ_STRICT_ALIGN
64
+ #elif defined(__i486__) || defined(__i586__) || defined(__i686__) /* GNU C */
65
+ #undef FASTLZ_STRICT_ALIGN
66
+ #elif defined(_M_IX86) /* Intel, MSVC */
67
+ #undef FASTLZ_STRICT_ALIGN
68
+ #elif defined(__386)
69
+ #undef FASTLZ_STRICT_ALIGN
70
+ #elif defined(_X86_) /* MinGW */
71
+ #undef FASTLZ_STRICT_ALIGN
72
+ #elif defined(__I86__) /* Digital Mars */
73
+ #undef FASTLZ_STRICT_ALIGN
74
+ #endif
75
+ #endif
76
+
77
+ /*
78
+ * FIXME: use preprocessor magic to set this on different platforms!
79
+ */
80
+ typedef unsigned char flzuint8;
81
+ typedef unsigned short flzuint16;
82
+ typedef unsigned int flzuint32;
83
+
84
+ /* prototypes */
85
+ int fastlz_compress(const void* input, int length, void* output);
86
+ int fastlz_compress_level(int level, const void* input, int length, void* output);
87
+ int fastlz_decompress(const void* input, int length, void* output, int maxout);
88
+
89
+ #define MAX_COPY 32
90
+ #define MAX_LEN 264 /* 256 + 8 */
91
+ #define MAX_DISTANCE 8192
92
+
93
+ #if !defined(FASTLZ_STRICT_ALIGN)
94
+ #define FASTLZ_READU16(p) *((const flzuint16*)(p))
95
+ #else
96
+ #define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8)
97
+ #endif
98
+
99
+ #define HASH_LOG 13
100
+ #define HASH_SIZE (1<< HASH_LOG)
101
+ #define HASH_MASK (HASH_SIZE-1)
102
+ #define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; }
103
+
104
+ #undef FASTLZ_LEVEL
105
+ #define FASTLZ_LEVEL 1
106
+
107
+ #undef FASTLZ_COMPRESSOR
108
+ #undef FASTLZ_DECOMPRESSOR
109
+ #define FASTLZ_COMPRESSOR fastlz1_compress
110
+ #define FASTLZ_DECOMPRESSOR fastlz1_decompress
111
+ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
112
+ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
113
+ #include "fastlz.c"
114
+
115
+ #undef FASTLZ_LEVEL
116
+ #define FASTLZ_LEVEL 2
117
+
118
+ #undef MAX_DISTANCE
119
+ #define MAX_DISTANCE 8191
120
+ #define MAX_FARDISTANCE (65535+MAX_DISTANCE-1)
121
+
122
+ #undef FASTLZ_COMPRESSOR
123
+ #undef FASTLZ_DECOMPRESSOR
124
+ #define FASTLZ_COMPRESSOR fastlz2_compress
125
+ #define FASTLZ_DECOMPRESSOR fastlz2_decompress
126
+ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
127
+ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
128
+ #include "fastlz.c"
129
+
130
+ int fastlz_compress(const void* input, int length, void* output)
131
+ {
132
+ /* for short block, choose fastlz1 */
133
+ if(length < 65536)
134
+ return fastlz1_compress(input, length, output);
135
+
136
+ /* else... */
137
+ return fastlz2_compress(input, length, output);
138
+ }
139
+
140
+ int fastlz_decompress(const void* input, int length, void* output, int maxout)
141
+ {
142
+ /* magic identifier for compression level */
143
+ int level = ((*(const flzuint8*)input) >> 5) + 1;
144
+
145
+ if(level == 1)
146
+ return fastlz1_decompress(input, length, output, maxout);
147
+ if(level == 2)
148
+ return fastlz2_decompress(input, length, output, maxout);
149
+
150
+ /* unknown level, trigger error */
151
+ return 0;
152
+ }
153
+
154
+ int fastlz_compress_level(int level, const void* input, int length, void* output)
155
+ {
156
+ if(level == 1)
157
+ return fastlz1_compress(input, length, output);
158
+ if(level == 2)
159
+ return fastlz2_compress(input, length, output);
160
+
161
+ return 0;
162
+ }
163
+
164
+ #else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
165
+
166
+ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output)
167
+ {
168
+ const flzuint8* ip = (const flzuint8*) input;
169
+ const flzuint8* ip_bound = ip + length - 2;
170
+ const flzuint8* ip_limit = ip + length - 12;
171
+ flzuint8* op = (flzuint8*) output;
172
+
173
+ const flzuint8* htab[HASH_SIZE];
174
+ const flzuint8** hslot;
175
+ flzuint32 hval;
176
+
177
+ flzuint32 copy;
178
+
179
+ /* sanity check */
180
+ if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4))
181
+ {
182
+ if(length)
183
+ {
184
+ /* create literal copy only */
185
+ *op++ = length-1;
186
+ ip_bound++;
187
+ while(ip <= ip_bound)
188
+ *op++ = *ip++;
189
+ return length+1;
190
+ }
191
+ else
192
+ return 0;
193
+ }
194
+
195
+ /* initializes hash table */
196
+ for (hslot = htab; hslot < htab + HASH_SIZE; hslot++)
197
+ *hslot = ip;
198
+
199
+ /* we start with literal copy */
200
+ copy = 2;
201
+ *op++ = MAX_COPY-1;
202
+ *op++ = *ip++;
203
+ *op++ = *ip++;
204
+
205
+ /* main loop */
206
+ while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
207
+ {
208
+ const flzuint8* ref;
209
+ flzuint32 distance;
210
+
211
+ /* minimum match length */
212
+ flzuint32 len = 3;
213
+
214
+ /* comparison starting-point */
215
+ const flzuint8* anchor = ip;
216
+
217
+ /* check for a run */
218
+ #if FASTLZ_LEVEL==2
219
+ if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1))
220
+ {
221
+ distance = 1;
222
+ ip += 3;
223
+ ref = anchor - 1 + 3;
224
+ goto match;
225
+ }
226
+ #endif
227
+
228
+ /* find potential match */
229
+ HASH_FUNCTION(hval,ip);
230
+ hslot = htab + hval;
231
+ ref = htab[hval];
232
+
233
+ /* calculate distance to the match */
234
+ distance = anchor - ref;
235
+
236
+ /* update hash table */
237
+ *hslot = anchor;
238
+
239
+ /* is this a match? check the first 3 bytes */
240
+ if(distance==0 ||
241
+ #if FASTLZ_LEVEL==1
242
+ (distance >= MAX_DISTANCE) ||
243
+ #else
244
+ (distance >= MAX_FARDISTANCE) ||
245
+ #endif
246
+ *ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++)
247
+ goto literal;
248
+
249
+ #if FASTLZ_LEVEL==2
250
+ /* far, needs at least 5-byte match */
251
+ if(distance >= MAX_DISTANCE)
252
+ {
253
+ if(*ip++ != *ref++ || *ip++!= *ref++)
254
+ goto literal;
255
+ len += 2;
256
+ }
257
+
258
+ match:
259
+ #endif
260
+
261
+ /* last matched byte */
262
+ ip = anchor + len;
263
+
264
+ /* distance is biased */
265
+ distance--;
266
+
267
+ if(!distance)
268
+ {
269
+ /* zero distance means a run */
270
+ flzuint8 x = ip[-1];
271
+ while(ip < ip_bound)
272
+ if(*ref++ != x) break; else ip++;
273
+ }
274
+ else
275
+ for(;;)
276
+ {
277
+ /* safe because the outer check against ip limit */
278
+ if(*ref++ != *ip++) break;
279
+ if(*ref++ != *ip++) break;
280
+ if(*ref++ != *ip++) break;
281
+ if(*ref++ != *ip++) break;
282
+ if(*ref++ != *ip++) break;
283
+ if(*ref++ != *ip++) break;
284
+ if(*ref++ != *ip++) break;
285
+ if(*ref++ != *ip++) break;
286
+ while(ip < ip_bound)
287
+ if(*ref++ != *ip++) break;
288
+ break;
289
+ }
290
+
291
+ /* if we have copied something, adjust the copy count */
292
+ if(copy)
293
+ /* copy is biased, '0' means 1 byte copy */
294
+ *(op-copy-1) = copy-1;
295
+ else
296
+ /* back, to overwrite the copy count */
297
+ op--;
298
+
299
+ /* reset literal counter */
300
+ copy = 0;
301
+
302
+ /* length is biased, '1' means a match of 3 bytes */
303
+ ip -= 3;
304
+ len = ip - anchor;
305
+
306
+ /* encode the match */
307
+ #if FASTLZ_LEVEL==2
308
+ if(distance < MAX_DISTANCE)
309
+ {
310
+ if(len < 7)
311
+ {
312
+ *op++ = (len << 5) + (distance >> 8);
313
+ *op++ = (distance & 255);
314
+ }
315
+ else
316
+ {
317
+ *op++ = (7 << 5) + (distance >> 8);
318
+ for(len-=7; len >= 255; len-= 255)
319
+ *op++ = 255;
320
+ *op++ = len;
321
+ *op++ = (distance & 255);
322
+ }
323
+ }
324
+ else
325
+ {
326
+ /* far away, but not yet in the another galaxy... */
327
+ if(len < 7)
328
+ {
329
+ distance -= MAX_DISTANCE;
330
+ *op++ = (len << 5) + 31;
331
+ *op++ = 255;
332
+ *op++ = distance >> 8;
333
+ *op++ = distance & 255;
334
+ }
335
+ else
336
+ {
337
+ distance -= MAX_DISTANCE;
338
+ *op++ = (7 << 5) + 31;
339
+ for(len-=7; len >= 255; len-= 255)
340
+ *op++ = 255;
341
+ *op++ = len;
342
+ *op++ = 255;
343
+ *op++ = distance >> 8;
344
+ *op++ = distance & 255;
345
+ }
346
+ }
347
+ #else
348
+
349
+ if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2))
350
+ while(len > MAX_LEN-2)
351
+ {
352
+ *op++ = (7 << 5) + (distance >> 8);
353
+ *op++ = MAX_LEN - 2 - 7 -2;
354
+ *op++ = (distance & 255);
355
+ len -= MAX_LEN-2;
356
+ }
357
+
358
+ if(len < 7)
359
+ {
360
+ *op++ = (len << 5) + (distance >> 8);
361
+ *op++ = (distance & 255);
362
+ }
363
+ else
364
+ {
365
+ *op++ = (7 << 5) + (distance >> 8);
366
+ *op++ = len - 7;
367
+ *op++ = (distance & 255);
368
+ }
369
+ #endif
370
+
371
+ /* update the hash at match boundary */
372
+ HASH_FUNCTION(hval,ip);
373
+ htab[hval] = ip++;
374
+ HASH_FUNCTION(hval,ip);
375
+ htab[hval] = ip++;
376
+
377
+ /* assuming literal copy */
378
+ *op++ = MAX_COPY-1;
379
+
380
+ continue;
381
+
382
+ literal:
383
+ *op++ = *anchor++;
384
+ ip = anchor;
385
+ copy++;
386
+ if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY))
387
+ {
388
+ copy = 0;
389
+ *op++ = MAX_COPY-1;
390
+ }
391
+ }
392
+
393
+ /* left-over as literal copy */
394
+ ip_bound++;
395
+ while(ip <= ip_bound)
396
+ {
397
+ *op++ = *ip++;
398
+ copy++;
399
+ if(copy == MAX_COPY)
400
+ {
401
+ copy = 0;
402
+ *op++ = MAX_COPY-1;
403
+ }
404
+ }
405
+
406
+ /* if we have copied something, adjust the copy length */
407
+ if(copy)
408
+ *(op-copy-1) = copy-1;
409
+ else
410
+ op--;
411
+
412
+ #if FASTLZ_LEVEL==2
413
+ /* marker for fastlz2 */
414
+ *(flzuint8*)output |= (1 << 5);
415
+ #endif
416
+
417
+ return op - (flzuint8*)output;
418
+ }
419
+
420
+ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout)
421
+ {
422
+ const flzuint8* ip = (const flzuint8*) input;
423
+ const flzuint8* ip_limit = ip + length;
424
+ flzuint8* op = (flzuint8*) output;
425
+ flzuint8* op_limit = op + maxout;
426
+ flzuint32 ctrl = (*ip++) & 31;
427
+ int loop = 1;
428
+
429
+ do
430
+ {
431
+ const flzuint8* ref = op;
432
+ flzuint32 len = ctrl >> 5;
433
+ flzuint32 ofs = (ctrl & 31) << 8;
434
+
435
+ if(ctrl >= 32)
436
+ {
437
+ #if FASTLZ_LEVEL==2
438
+ flzuint8 code;
439
+ #endif
440
+ len--;
441
+ ref -= ofs;
442
+ if (len == 7-1)
443
+ #if FASTLZ_LEVEL==1
444
+ len += *ip++;
445
+ ref -= *ip++;
446
+ #else
447
+ do
448
+ {
449
+ code = *ip++;
450
+ len += code;
451
+ } while (code==255);
452
+ code = *ip++;
453
+ ref -= code;
454
+
455
+ /* match from 16-bit distance */
456
+ if(FASTLZ_UNEXPECT_CONDITIONAL(code==255))
457
+ if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8)))
458
+ {
459
+ ofs = (*ip++) << 8;
460
+ ofs += *ip++;
461
+ ref = op - ofs - MAX_DISTANCE;
462
+ }
463
+ #endif
464
+
465
+ #ifdef FASTLZ_SAFE
466
+ if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit))
467
+ return 0;
468
+
469
+ if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output))
470
+ return 0;
471
+ #endif
472
+
473
+ if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
474
+ ctrl = *ip++;
475
+ else
476
+ loop = 0;
477
+
478
+ if(ref == op)
479
+ {
480
+ /* optimize copy for a run */
481
+ flzuint8 b = ref[-1];
482
+ *op++ = b;
483
+ *op++ = b;
484
+ *op++ = b;
485
+ for(; len; --len)
486
+ *op++ = b;
487
+ }
488
+ else
489
+ {
490
+ #if !defined(FASTLZ_STRICT_ALIGN)
491
+ const flzuint16* p;
492
+ flzuint16* q;
493
+ #endif
494
+ /* copy from reference */
495
+ ref--;
496
+ *op++ = *ref++;
497
+ *op++ = *ref++;
498
+ *op++ = *ref++;
499
+
500
+ #if !defined(FASTLZ_STRICT_ALIGN)
501
+ /* copy a byte, so that now it's word aligned */
502
+ if(len & 1)
503
+ {
504
+ *op++ = *ref++;
505
+ len--;
506
+ }
507
+
508
+ /* copy 16-bit at once */
509
+ q = (flzuint16*) op;
510
+ op += len;
511
+ p = (const flzuint16*) ref;
512
+ for(len>>=1; len > 4; len-=4)
513
+ {
514
+ *q++ = *p++;
515
+ *q++ = *p++;
516
+ *q++ = *p++;
517
+ *q++ = *p++;
518
+ }
519
+ for(; len; --len)
520
+ *q++ = *p++;
521
+ #else
522
+ for(; len; --len)
523
+ *op++ = *ref++;
524
+ #endif
525
+ }
526
+ }
527
+ else
528
+ {
529
+ ctrl++;
530
+ #ifdef FASTLZ_SAFE
531
+ if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit))
532
+ return 0;
533
+ if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit))
534
+ return 0;
535
+ #endif
536
+
537
+ *op++ = *ip++;
538
+ for(--ctrl; ctrl; ctrl--)
539
+ *op++ = *ip++;
540
+
541
+ loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit);
542
+ if(loop)
543
+ ctrl = *ip++;
544
+ }
545
+ }
546
+ while(FASTLZ_EXPECT_CONDITIONAL(loop));
547
+
548
+ return op - (flzuint8*)output;
549
+ }
550
+
551
+ #endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
@@ -0,0 +1,100 @@
1
+ /*
2
+ FastLZ - lightning-fast lossless compression library
3
+
4
+ Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
5
+ Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
6
+ Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in
16
+ all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ THE SOFTWARE.
25
+ */
26
+
27
+ #ifndef FASTLZ_H
28
+ #define FASTLZ_H
29
+
30
+ #define FASTLZ_VERSION 0x000100
31
+
32
+ #define FASTLZ_VERSION_MAJOR 0
33
+ #define FASTLZ_VERSION_MINOR 0
34
+ #define FASTLZ_VERSION_REVISION 0
35
+
36
+ #define FASTLZ_VERSION_STRING "0.1.0"
37
+
38
+ #if defined (__cplusplus)
39
+ extern "C" {
40
+ #endif
41
+
42
+ /**
43
+ Compress a block of data in the input buffer and returns the size of
44
+ compressed block. The size of input buffer is specified by length. The
45
+ minimum input buffer size is 16.
46
+
47
+ The output buffer must be at least 5% larger than the input buffer
48
+ and can not be smaller than 66 bytes.
49
+
50
+ If the input is not compressible, the return value might be larger than
51
+ length (input buffer size).
52
+
53
+ The input buffer and the output buffer can not overlap.
54
+ */
55
+
56
+ int fastlz_compress(const void* input, int length, void* output);
57
+
58
+ /**
59
+ Decompress a block of compressed data and returns the size of the
60
+ decompressed block. If error occurs, e.g. the compressed data is
61
+ corrupted or the output buffer is not large enough, then 0 (zero)
62
+ will be returned instead.
63
+
64
+ The input buffer and the output buffer can not overlap.
65
+
66
+ Decompression is memory safe and guaranteed not to write the output buffer
67
+ more than what is specified in maxout.
68
+ */
69
+
70
+ int fastlz_decompress(const void* input, int length, void* output, int maxout);
71
+
72
+ /**
73
+ Compress a block of data in the input buffer and returns the size of
74
+ compressed block. The size of input buffer is specified by length. The
75
+ minimum input buffer size is 16.
76
+
77
+ The output buffer must be at least 5% larger than the input buffer
78
+ and can not be smaller than 66 bytes.
79
+
80
+ If the input is not compressible, the return value might be larger than
81
+ length (input buffer size).
82
+
83
+ The input buffer and the output buffer can not overlap.
84
+
85
+ Compression level can be specified in parameter level. At the moment,
86
+ only level 1 and level 2 are supported.
87
+ Level 1 is the fastest compression and generally useful for short data.
88
+ Level 2 is slightly slower but it gives better compression ratio.
89
+
90
+ Note that the compressed data, regardless of the level, can always be
91
+ decompressed using the function fastlz_decompress above.
92
+ */
93
+
94
+ int fastlz_compress_level(int level, const void* input, int length, void* output);
95
+
96
+ #if defined (__cplusplus)
97
+ }
98
+ #endif
99
+
100
+ #endif /* FASTLZ_H */
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastlz
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - winebarrel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-13 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: sgwr_dts@yahoo.co.jp
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/extconf.rb
28
+ extra_rdoc_files:
29
+ - README
30
+ - ext/fastlz-ruby.c
31
+ files:
32
+ - ext/extconf.rb
33
+ - ext/fastlz-ruby.c
34
+ - ext/fastlz.c
35
+ - ext/fastlz.h
36
+ - ext/LICENSE
37
+ - ext/README.TXT
38
+ - README
39
+ has_rdoc: true
40
+ homepage: https://bitbucket.org/winebarrel/fastlz-ruby
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Ruby bindings for FastLZ.
73
+ test_files: []
74
+