lzfruby 0.1.2-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt ADDED
@@ -0,0 +1,80 @@
1
+ = LZF/Ruby
2
+
3
+ Copyright (c) 2008 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
4
+
5
+ == Description
6
+
7
+ Ruby bindings for LibLZF.
8
+
9
+ LibLZF is a very fast compression library.
10
+
11
+ == Project Page
12
+
13
+ http://rubyforge.org/projects/lzfruby
14
+
15
+ == Install
16
+
17
+ gem install lzfruby
18
+
19
+ == Download
20
+
21
+ http://rubyforge.org/frs/?group_id=6750
22
+
23
+ == Example
24
+
25
+ require 'lzfruby'
26
+ require 'open-uri'
27
+ require 'stringio'
28
+
29
+ source = open('http://lzfruby.rubyforge.org/') {|f| f.read }
30
+ source = StringIO.new(source)
31
+
32
+ puts "uncompress size: #{source.length}"
33
+
34
+ # compress
35
+ comp_data = StringIO.new
36
+ LZF.compress(source, comp_data)
37
+ puts "compress size: #{comp_data.length}"
38
+
39
+ # decompress
40
+ comp_data.seek(0)
41
+ decomp_data = StringIO.new
42
+ LZF.decompress(comp_data, decomp_data)
43
+ puts "decompress size: #{decomp_data.length}"
44
+ puts "decompress success?: #{source.string == decomp_data.string}"
45
+
46
+ == License
47
+
48
+ Copyright (c) 2008 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
49
+ All rights reserved.
50
+
51
+ Redistribution and use in source and binary forms, with or without modification,
52
+ are permitted provided that the following conditions are met:
53
+
54
+ * Redistributions of source code must retain the above copyright notice,
55
+ this list of conditions and the following disclaimer.
56
+ * Redistributions in binary form must reproduce the above copyright notice,
57
+ this list of conditions and the following disclaimer in the documentation
58
+ and/or other materials provided with the distribution.
59
+ * The names of its contributors may be used to endorse or promote products
60
+ derived from this software without specific prior written permission.
61
+
62
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
63
+ ANY EXPRESS OR IMPLIED WARRANTIES,
64
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
65
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
66
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
68
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
70
+ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
72
+ DAMAGE.
73
+
74
+ === LibLZF
75
+ LZF/Ruby contains LibLZF.
76
+
77
+ LibLZF is a very fast compression library.
78
+
79
+ * http://www.goof.com/pcg/marc/liblzf.html
80
+ * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>
data/ext/lzfruby.c ADDED
@@ -0,0 +1,201 @@
1
+ #ifdef _WIN32
2
+ __declspec(dllexport) void Init_lzfruby(void);
3
+ typedef int ssize_t;
4
+ #define _CRT_SECURE_DEPRECATE_MEMORY
5
+ #include <memory.h>
6
+ #endif
7
+
8
+ #include <string.h>
9
+ #include "lzf.h"
10
+ #include "ruby.h"
11
+
12
+ #ifndef RSTRING_PTR
13
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
14
+ #endif
15
+
16
+ #ifndef RSTRING_LEN
17
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
18
+ #endif
19
+
20
+ #define Check_IO(x) do { \
21
+ const char *classname = rb_class2name(CLASS_OF(x)); \
22
+ if (rb_obj_is_instance_of((x), rb_cIO)) { \
23
+ rb_io_binmode(x); \
24
+ } else if (strcmp(classname, "StringIO") != 0) { \
25
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected IO or StringIO)", classname); \
26
+ } \
27
+ } while(0)
28
+
29
+ #define VERSION "0.1.2"
30
+ #define BLOCKSIZE (1024 * 64 - 1)
31
+ #define MAX_BLOCKSIZE BLOCKSIZE
32
+ #define TYPE0_HDR_SIZE 5
33
+ #define TYPE1_HDR_SIZE 7
34
+ #define MAX_HDR_SIZE 7
35
+ #define MIN_HDR_SIZE 5
36
+
37
+ static VALUE LZF;
38
+ static VALUE LZF_Error;
39
+
40
+ /* */
41
+ static VALUE lzfruby_compress(int argc, VALUE *argv, VALUE self) {
42
+ VALUE from, to, blocksize;
43
+ unsigned char buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
44
+ unsigned char buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
45
+ unsigned char *header;
46
+ int i_blocksize;
47
+
48
+ rb_scan_args(argc, argv, "21", &from, &to, &blocksize);
49
+ Check_IO(from);
50
+ Check_IO(to);
51
+
52
+ if (NIL_P(blocksize)) {
53
+ i_blocksize = BLOCKSIZE;
54
+ } else {
55
+ i_blocksize = NUM2INT(blocksize);
56
+
57
+ if (blocksize < 1 || MAX_BLOCKSIZE < blocksize) {
58
+ i_blocksize = BLOCKSIZE;
59
+ }
60
+ }
61
+
62
+ blocksize = INT2FIX(i_blocksize);
63
+
64
+ while (1) {
65
+ VALUE in = rb_funcall(from, rb_intern("read"), 1, blocksize);
66
+ ssize_t us, cs, len;
67
+
68
+ if (NIL_P(in) || (us = RSTRING_LEN(in)) < 1) {
69
+ break;
70
+ }
71
+
72
+ memcpy(&buf1[MAX_HDR_SIZE], RSTRING_PTR(in), us);
73
+ cs = lzf_compress(&buf1[MAX_HDR_SIZE], us, &buf2[MAX_HDR_SIZE], (us > 4) ? us - 4 : us);
74
+
75
+ if (cs) {
76
+ header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE];
77
+ header[0] = 'Z';
78
+ header[1] = 'V';
79
+ header[2] = 1;
80
+ header[3] = cs >> 8;
81
+ header[4] = cs & 0xff;
82
+ header[5] = us >> 8;
83
+ header[6] = us & 0xff;
84
+ len = cs + TYPE1_HDR_SIZE;
85
+ } else {
86
+ header = &buf1[MAX_HDR_SIZE - TYPE0_HDR_SIZE];
87
+ header[0] = 'Z';
88
+ header[1] = 'V';
89
+ header[2] = 0;
90
+ header[3] = us >> 8;
91
+ header[4] = us & 0xff;
92
+ len = us + TYPE0_HDR_SIZE;
93
+ }
94
+
95
+ rb_funcall(to, rb_intern("write"), 1, rb_str_new(header, len));
96
+ }
97
+
98
+ return Qnil;
99
+ }
100
+
101
+ /* */
102
+ static VALUE lzfruby_decompress(VALUE self, VALUE from, VALUE to) {
103
+ unsigned char header[MAX_HDR_SIZE];
104
+ unsigned char buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
105
+ unsigned char buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
106
+ ssize_t rc, cs, us, bytes, over = 0;
107
+ int l, rd;
108
+
109
+ Check_IO(from);
110
+ Check_IO(to);
111
+
112
+ while (1) {
113
+ VALUE in, in_header;
114
+ unsigned char *p;
115
+
116
+ in_header = rb_funcall(from, rb_intern("read"), 1, INT2FIX(MAX_HDR_SIZE - over));
117
+
118
+ if (NIL_P(in_header) || (rc = RSTRING_LEN(in_header)) < 1) {
119
+ break;
120
+ }
121
+
122
+ memcpy(header + over, RSTRING_PTR(in_header), MAX_HDR_SIZE - over);
123
+ rc += over;
124
+ over = 0;
125
+
126
+ if (header[0] == 0) {
127
+ break;
128
+ }
129
+
130
+ if (rc < MIN_HDR_SIZE || header[0] != 'Z' || header[1] != 'V') {
131
+ rb_raise(LZF_Error, "invalid data stream - magic not found or short header");
132
+ }
133
+
134
+ switch (header[2]) {
135
+ case 0:
136
+ cs = -1;
137
+ us = (header[3] << 8) | header[4];
138
+ p = &header[TYPE0_HDR_SIZE];
139
+ break;
140
+
141
+ case 1:
142
+ if (rc < TYPE1_HDR_SIZE) {
143
+ rb_raise(LZF_Error, "short data");
144
+ }
145
+
146
+ cs = (header[3] << 8) | header[4];
147
+ us = (header[5] << 8) | header[6];
148
+ p = &header[TYPE1_HDR_SIZE];
149
+ break;
150
+
151
+ default:
152
+ rb_raise(LZF_Error, "unknown blocktype");
153
+ }
154
+
155
+ bytes = (cs == -1) ? us : cs;
156
+ l = &header[rc] - p;
157
+
158
+ if (l > 0) {
159
+ memcpy(buf1, p, l);
160
+ }
161
+
162
+ if (l > bytes) {
163
+ over = l - bytes;
164
+ memmove(header, &p[bytes], over);
165
+ }
166
+
167
+ p = &buf1[l];
168
+ rd = bytes - l;
169
+
170
+ if (rd > 0) {
171
+ in = rb_funcall(from, rb_intern("read"), 1, INT2FIX(rd));
172
+
173
+ if (NIL_P(in) || (rc = RSTRING_LEN(in)) < 1 || rc != rd) {
174
+ rb_raise(LZF_Error, "short data");
175
+ }
176
+
177
+ memcpy(p, RSTRING_PTR(in), rc);
178
+ }
179
+
180
+ if (cs == -1) {
181
+ rb_funcall(to, rb_intern("write"), 1, rb_str_new(buf1, us));
182
+ } else {
183
+ if (lzf_decompress(buf1, cs, buf2, us) != us) {
184
+ rb_raise(LZF_Error, "decompress: invalid stream - data corrupted");
185
+ }
186
+
187
+ rb_funcall(to, rb_intern("write"), 1, rb_str_new(buf2, us));
188
+ }
189
+ }
190
+
191
+ return Qnil;
192
+ }
193
+
194
+ void Init_lzfruby() {
195
+ LZF = rb_define_module("LZF");
196
+ LZF_Error = rb_define_class_under(LZF, "Error", rb_eStandardError);
197
+
198
+ rb_define_const(LZF, "VERSION", rb_str_new2(VERSION));
199
+ rb_define_module_function(LZF, "compress", lzfruby_compress, -1);
200
+ rb_define_module_function(LZF, "decompress", lzfruby_decompress, 2);
201
+ }
Binary file
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lzfruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: mswin32
6
+ authors:
7
+ - winebarrel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-08 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: sgwr_dts@yahoo.co.jp
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ - ext/lzfruby.c
25
+ files:
26
+ - lib/i386-mswin32/lzfruby.so
27
+ - README.txt
28
+ - ext/lzfruby.c
29
+ has_rdoc: true
30
+ homepage: http://lzfruby.rubyforge.org
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --title
34
+ - LZF/Ruby - Ruby bindings for LibLZF.
35
+ require_paths:
36
+ - lib/i386-mswin32
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: lzfruby
52
+ rubygems_version: 1.1.1
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Ruby bindings for LibLZF.
56
+ test_files: []
57
+