ffi-zlib 0.2.0
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.
- data/LICENSE +21 -0
- data/README.md +14 -0
- data/lib/ffi/zlib.rb +213 -0
- data/test/fixtures/pale_blue_dot.txt +35 -0
- data/test/test_checksums.rb +92 -0
- data/test/test_compress.rb +52 -0
- data/test/test_deflate.rb +71 -0
- data/test/test_gz.rb +3 -0
- data/test/test_misc.rb +38 -0
- metadata +92 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2010 Scott Chacon
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
ffi-zlib
|
2
|
+
========
|
3
|
+
|
4
|
+
A ruby-ffi wrapper for the zlib library.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
============
|
8
|
+
|
9
|
+
[sudo] gem install ffi-zlib
|
10
|
+
|
11
|
+
Usage
|
12
|
+
=====
|
13
|
+
|
14
|
+
ffi-zlib being a very thin wrapper roughly reproducing the zlib header(s), you can simply refer to the zlib [manual](http://www.zlib.net/manual.html) for details and usage.
|
data/lib/ffi/zlib.rb
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
# =============================================================================
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "ffi"
|
5
|
+
|
6
|
+
# =============================================================================
|
7
|
+
|
8
|
+
module FFI::Zlib
|
9
|
+
extend FFI::Library
|
10
|
+
ffi_lib("z")
|
11
|
+
|
12
|
+
# -------------------------------------------------------------------------
|
13
|
+
|
14
|
+
attach_function :zlibVersion, [ ], :string
|
15
|
+
|
16
|
+
# -------------------------------------------------------------------------
|
17
|
+
|
18
|
+
ZLIB_VERSION = zlibVersion()
|
19
|
+
|
20
|
+
# Allowed flush values.
|
21
|
+
|
22
|
+
Z_NO_FLUSH = 0
|
23
|
+
Z_PARTIAL_FLUSH = 1
|
24
|
+
Z_SYNC_FLUSH = 2
|
25
|
+
Z_FULL_FLUSH = 3
|
26
|
+
Z_FINISH = 4
|
27
|
+
Z_BLOCK = 5
|
28
|
+
|
29
|
+
# Return codes for the compression/decompression functions.
|
30
|
+
# Negative values are errors, positive values are used for special but
|
31
|
+
# normal events.
|
32
|
+
|
33
|
+
Z_OK = 0
|
34
|
+
Z_STREAM_END = 1
|
35
|
+
Z_NEED_DICT = 2
|
36
|
+
Z_ERRNO = -1
|
37
|
+
Z_STREAM_ERROR = -2
|
38
|
+
Z_DATA_ERROR = -3
|
39
|
+
Z_MEM_ERROR = -4
|
40
|
+
Z_BUF_ERROR = -5
|
41
|
+
Z_VERSION_ERROR = -6
|
42
|
+
|
43
|
+
# Compression levels.
|
44
|
+
|
45
|
+
Z_NO_COMPRESSION = 0
|
46
|
+
Z_BEST_SPEED = 1
|
47
|
+
Z_BEST_COMPRESSION = 9
|
48
|
+
Z_DEFAULT_COMPRESSION = -1
|
49
|
+
|
50
|
+
# Compression strategies.
|
51
|
+
|
52
|
+
Z_FILTERED = 1
|
53
|
+
Z_HUFFMAN_ONLY = 2
|
54
|
+
Z_RLE = 3
|
55
|
+
Z_FIXED = 4
|
56
|
+
Z_DEFAULT_STRATEGY = 0
|
57
|
+
|
58
|
+
# Possible values of the data_type field.
|
59
|
+
|
60
|
+
Z_BINARY = 0
|
61
|
+
Z_TEXT = 1
|
62
|
+
Z_UNKNOWN = 2
|
63
|
+
|
64
|
+
# The deflate compression method (the only one supported).
|
65
|
+
|
66
|
+
Z_DEFLATED = 8
|
67
|
+
|
68
|
+
# -------------------------------------------------------------------------
|
69
|
+
|
70
|
+
class Z_stream < FFI::Struct
|
71
|
+
layout :next_in, :pointer,
|
72
|
+
:avail_in, :uint,
|
73
|
+
:total_in, :ulong,
|
74
|
+
|
75
|
+
:next_out, :pointer,
|
76
|
+
:avail_out, :uint,
|
77
|
+
:total_out, :ulong,
|
78
|
+
|
79
|
+
:msg, :string,
|
80
|
+
:state, :pointer,
|
81
|
+
|
82
|
+
:alloc_func, :pointer,
|
83
|
+
:free_func, :pointer,
|
84
|
+
:opaque, :pointer,
|
85
|
+
|
86
|
+
:data_type, :int,
|
87
|
+
:adler, :ulong,
|
88
|
+
:reserved, :ulong
|
89
|
+
end
|
90
|
+
|
91
|
+
# -------------------------------------------------------------------------
|
92
|
+
|
93
|
+
class GZ_header < FFI::Struct
|
94
|
+
layout :text, :int,
|
95
|
+
:time, :ulong,
|
96
|
+
:xflags, :int,
|
97
|
+
:os, :int,
|
98
|
+
|
99
|
+
:extra, :pointer,
|
100
|
+
:extra_len, :uint,
|
101
|
+
:extra_max, :uint,
|
102
|
+
|
103
|
+
:name, :string,
|
104
|
+
:name_max, :uint,
|
105
|
+
:comment, :string,
|
106
|
+
:comm_max, :uint,
|
107
|
+
|
108
|
+
:hcrc, :int,
|
109
|
+
:done, :int
|
110
|
+
end
|
111
|
+
|
112
|
+
# -------------------------------------------------------------------------
|
113
|
+
|
114
|
+
def self.deflateInit(zstream, level)
|
115
|
+
deflateInit_(zstream, level, ZLIB_VERSION, zstream.size)
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.deflateInit2(zstream, level, method, window_bits, mem_level, strategy)
|
119
|
+
deflateInit2_(zstream, level, method, window_bits, mem_level, strategy, ZLIB_VERSION, zstream.size)
|
120
|
+
end
|
121
|
+
|
122
|
+
attach_function :deflateInit_, [:pointer, :int, :string, :int], :int
|
123
|
+
attach_function :deflateInit2_, [:pointer, :int, :int, :int, :int, :int, :string, :int], :int
|
124
|
+
attach_function :deflate, [:pointer, :int], :int
|
125
|
+
attach_function :deflateEnd, [:pointer], :int
|
126
|
+
|
127
|
+
attach_function :deflateSetDictionary, [:pointer, :pointer, :uint], :int
|
128
|
+
attach_function :deflateCopy, [:pointer, :pointer], :int
|
129
|
+
attach_function :deflateReset, [:pointer], :int
|
130
|
+
attach_function :deflateParams, [:pointer, :int, :int], :int
|
131
|
+
attach_function :deflateTune, [:pointer, :int, :int, :int, :int], :int
|
132
|
+
attach_function :deflateBound, [:pointer, :ulong], :ulong
|
133
|
+
attach_function :deflatePrime, [:pointer, :int, :int], :int
|
134
|
+
attach_function :deflateSetHeader, [:pointer, :pointer], :int
|
135
|
+
|
136
|
+
# -------------------------------------------------------------------------
|
137
|
+
|
138
|
+
def self.inflateInit(zstream)
|
139
|
+
inflateInit_(zstream, ZLIB_VERSION, zstream.size)
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.inflateInit2(zstream, window_bits)
|
143
|
+
inflateInit2_(zstream, window_bits, ZLIB_VERSION, zstream.size)
|
144
|
+
end
|
145
|
+
|
146
|
+
attach_function :inflateInit_, [:pointer, :string, :int], :int
|
147
|
+
attach_function :inflateInit2_, [:pointer, :int, :string, :int], :int
|
148
|
+
attach_function :inflate, [:pointer, :int], :int
|
149
|
+
attach_function :inflateEnd, [:pointer], :int
|
150
|
+
|
151
|
+
attach_function :inflateSetDictionary, [:pointer, :pointer, :uint], :int
|
152
|
+
attach_function :inflateSync, [:pointer], :int
|
153
|
+
attach_function :inflateCopy, [:pointer, :pointer], :int
|
154
|
+
attach_function :inflateReset, [:pointer], :int
|
155
|
+
attach_function :inflatePrime, [:pointer, :int, :int], :int
|
156
|
+
attach_function :inflateGetHeader, [:pointer, :pointer], :int
|
157
|
+
|
158
|
+
callback :in_func, [:pointer, :pointer], :uint
|
159
|
+
callback :out_func, [:pointer, :pointer, :uint], :int
|
160
|
+
|
161
|
+
attach_function :inflateBackInit_, [:pointer, :int, :pointer], :int
|
162
|
+
attach_function :inflateBack, [:pointer, :in_func, :pointer, :out_func, :pointer], :int
|
163
|
+
attach_function :inflateBackEnd, [:pointer], :int
|
164
|
+
|
165
|
+
# -------------------------------------------------------------------------
|
166
|
+
|
167
|
+
attach_function :compress, [:pointer, :buffer_inout, :pointer, :ulong], :int
|
168
|
+
attach_function :compress2, [:pointer, :pointer, :pointer, :ulong, :int], :int
|
169
|
+
attach_function :compressBound, [:ulong], :ulong
|
170
|
+
|
171
|
+
attach_function :uncompress, [:pointer, :buffer_inout, :pointer, :ulong], :int
|
172
|
+
|
173
|
+
# -------------------------------------------------------------------------
|
174
|
+
|
175
|
+
attach_function :gzopen, [:string, :string], :pointer
|
176
|
+
attach_function :gzdopen, [:int, :string], :pointer
|
177
|
+
attach_function :gzsetparams, [:pointer, :int, :int], :int
|
178
|
+
attach_function :gzread, [:pointer, :pointer, :uint], :int
|
179
|
+
attach_function :gzwrite, [:pointer, :pointer, :uint], :int
|
180
|
+
attach_function :gzprintf, [:pointer, :string, :varargs], :int
|
181
|
+
attach_function :gzputs, [:pointer, :string], :int
|
182
|
+
attach_function :gzgets, [:pointer, :pointer, :int], :pointer
|
183
|
+
attach_function :gzputc, [:pointer, :int], :int
|
184
|
+
attach_function :gzgetc, [:pointer, :pointer], :int
|
185
|
+
attach_function :gzungetc, [:int, :pointer], :int
|
186
|
+
attach_function :gzflush, [:pointer, :int], :int
|
187
|
+
attach_function :gzseek, [:pointer, :long, :int], :long
|
188
|
+
attach_function :gzrewind, [:pointer], :int
|
189
|
+
attach_function :gztell, [:pointer], :long
|
190
|
+
attach_function :gzeof, [:pointer], :int
|
191
|
+
attach_function :gzdirect, [:pointer], :int
|
192
|
+
attach_function :gzclose, [:pointer], :int
|
193
|
+
attach_function :gzerror, [:pointer, :buffer_out], :string
|
194
|
+
attach_function :gzclearerr, [:pointer], :void
|
195
|
+
|
196
|
+
# -------------------------------------------------------------------------
|
197
|
+
|
198
|
+
attach_function :adler32, [:ulong, :pointer, :uint], :ulong
|
199
|
+
attach_function :adler32_combine, [:ulong, :ulong, :long], :ulong
|
200
|
+
attach_function :crc32, [:ulong, :pointer, :uint], :ulong
|
201
|
+
attach_function :crc32_combine, [:ulong, :ulong, :long], :ulong
|
202
|
+
|
203
|
+
# -------------------------------------------------------------------------
|
204
|
+
|
205
|
+
attach_function :zlibCompileFlags, [ ], :ulong
|
206
|
+
attach_function :zError, [:int], :string
|
207
|
+
attach_function :inflateSyncPoint, [:pointer], :int
|
208
|
+
attach_function :get_crc_table, [ ], :pointer
|
209
|
+
|
210
|
+
# -------------------------------------------------------------------------
|
211
|
+
end
|
212
|
+
|
213
|
+
# =============================================================================
|
@@ -0,0 +1,35 @@
|
|
1
|
+
Look again at that dot. That's here. That's home. That's us. On it everyone you
|
2
|
+
love, everyone you know, everyone you ever heard of, every human being who ever
|
3
|
+
was, lived out their lives. The aggregate of our joy and suffering, thousands of
|
4
|
+
confident religions, ideologies, and economic doctrines, every hunter and
|
5
|
+
forager, every hero and coward, every creator and destroyer of civilization,
|
6
|
+
every king and peasant, every young couple in love, every mother and father,
|
7
|
+
hopeful child, inventor and explorer, every teacher of morals, every corrupt
|
8
|
+
politician, every "superstar," every "supreme leader," every saint and sinner in
|
9
|
+
the history of our species lived there – on a mote of dust suspended in a
|
10
|
+
sunbeam.
|
11
|
+
|
12
|
+
The Earth is a very small stage in a vast cosmic arena. Think of the rivers of
|
13
|
+
blood spilled by all those generals and emperors so that, in glory and triumph,
|
14
|
+
they could become the momentary masters of a fraction of a dot. Think of the
|
15
|
+
endless cruelties visited by the inhabitants of one corner of this pixel on the
|
16
|
+
scarcely distinguishable inhabitants of some other corner, how frequent their
|
17
|
+
misunderstandings, how eager they are to kill one another, how fervent their
|
18
|
+
hatreds.
|
19
|
+
|
20
|
+
Our posturings, our imagined self-importance, the delusion that we have some
|
21
|
+
privileged position in the Universe, are challenged by this point of pale light.
|
22
|
+
Our planet is a lonely speck in the great enveloping cosmic dark. In our
|
23
|
+
obscurity, in all this vastness, there is no hint that help will come from
|
24
|
+
elsewhere to save us from ourselves.
|
25
|
+
|
26
|
+
The Earth is the only world known so far to harbor life. There is nowhere else,
|
27
|
+
at least in the near future, to which our species could migrate. Visit, yes.
|
28
|
+
Settle, not yet. Like it or not, for the moment the Earth is where we make our
|
29
|
+
stand.
|
30
|
+
|
31
|
+
It has been said that astronomy is a humbling and character-building experience.
|
32
|
+
There is perhaps no better demonstration of the folly of human conceits than
|
33
|
+
this distant image of our tiny world. To me, it underscores our responsibility
|
34
|
+
to deal more kindly with one another, and to preserve and cherish the pale blue
|
35
|
+
dot, the only home we've ever known.
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "ffi/zlib"
|
3
|
+
require "test/helper.rb"
|
4
|
+
|
5
|
+
class TestAdler32 < Test::Unit::TestCase
|
6
|
+
include Setup
|
7
|
+
|
8
|
+
EXPECTED_ADLER32_1 = 3874980441
|
9
|
+
EXPECTED_ADLER32_2 = 1707704336
|
10
|
+
EXPECTED_ADLER32_FULL = 869204599
|
11
|
+
|
12
|
+
def adler32(data)
|
13
|
+
@buffer.put_string(0, data)
|
14
|
+
checksum = FFI::Zlib.adler32(0, nil, 0)
|
15
|
+
FFI::Zlib.adler32(checksum, @buffer, data.length)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_adler32
|
19
|
+
checksum = self.adler32(@data)
|
20
|
+
assert_equal checksum, EXPECTED_ADLER32_FULL
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_running_adler32
|
24
|
+
checksum = FFI::Zlib.adler32(0, nil, 0)
|
25
|
+
@data.each_line do |line|
|
26
|
+
@buffer.put_string(0, line)
|
27
|
+
checksum = FFI::Zlib.adler32(checksum, @buffer, line.length)
|
28
|
+
end
|
29
|
+
|
30
|
+
assert_equal checksum, EXPECTED_ADLER32_FULL
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_adler32_combine
|
34
|
+
middle, _ = @data.length.divmod(2)
|
35
|
+
piece1 = @data[0...middle]
|
36
|
+
piece2 = @data[middle..-1]
|
37
|
+
|
38
|
+
checksum1 = self.adler32(piece1)
|
39
|
+
checksum2 = self.adler32(piece2)
|
40
|
+
checksum = FFI::Zlib.adler32_combine(checksum1, checksum2, piece2.length)
|
41
|
+
|
42
|
+
assert_equal checksum1, EXPECTED_ADLER32_1
|
43
|
+
assert_equal checksum2, EXPECTED_ADLER32_2
|
44
|
+
assert_equal checksum, EXPECTED_ADLER32_FULL
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
class TestCRC32 < Test::Unit::TestCase
|
51
|
+
include Setup
|
52
|
+
|
53
|
+
EXPECTED_CRC32_1 = 4007132158
|
54
|
+
EXPECTED_CRC32_2 = 1117097248
|
55
|
+
EXPECTED_CRC32_FULL = 703194008
|
56
|
+
|
57
|
+
def crc32(data)
|
58
|
+
@buffer.put_string(0, data)
|
59
|
+
checksum = FFI::Zlib.crc32(0, nil, 0)
|
60
|
+
checksum = FFI::Zlib.crc32(checksum, @buffer, data.length)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_crc32
|
64
|
+
checksum = self.crc32(@data)
|
65
|
+
assert_equal checksum, EXPECTED_CRC32_FULL
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_running_crc32
|
69
|
+
checksum = FFI::Zlib.crc32(0, nil, 0)
|
70
|
+
@data.each_line do |line|
|
71
|
+
@buffer.put_string(0, line)
|
72
|
+
checksum = FFI::Zlib.crc32(checksum, @buffer, line.length)
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_equal checksum, EXPECTED_CRC32_FULL
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_crc32_combine
|
79
|
+
middle, _ = @data.length.divmod(2)
|
80
|
+
piece1 = @data[0...middle]
|
81
|
+
piece2 = @data[middle..-1]
|
82
|
+
|
83
|
+
checksum1 = self.crc32(piece1)
|
84
|
+
checksum2 = self.crc32(piece2)
|
85
|
+
checksum = FFI::Zlib.crc32_combine(checksum1, checksum2, piece2.length)
|
86
|
+
|
87
|
+
assert_equal checksum1, EXPECTED_CRC32_1
|
88
|
+
assert_equal checksum2, EXPECTED_CRC32_2
|
89
|
+
assert_equal checksum, EXPECTED_CRC32_FULL
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "ffi/zlib"
|
3
|
+
require "test/helper.rb"
|
4
|
+
|
5
|
+
class TestCompress < Test::Unit::TestCase
|
6
|
+
include Setup
|
7
|
+
|
8
|
+
def test_compress
|
9
|
+
do_test
|
10
|
+
|
11
|
+
s1 = do_test(FFI::Zlib::Z_NO_COMPRESSION)
|
12
|
+
s2 = do_test(FFI::Zlib::Z_BEST_SPEED)
|
13
|
+
s3 = do_test(FFI::Zlib::Z_DEFAULT_COMPRESSION)
|
14
|
+
s4 = do_test(FFI::Zlib::Z_BEST_COMPRESSION)
|
15
|
+
|
16
|
+
assert s1 > s2
|
17
|
+
assert s2 > s3
|
18
|
+
assert s3 >= s4
|
19
|
+
end
|
20
|
+
|
21
|
+
def do_test(level=nil)
|
22
|
+
@buffer.put_string(0, @data)
|
23
|
+
|
24
|
+
buffer_size = FFI::Zlib.compressBound(@data.length)
|
25
|
+
assert_not_equal buffer_size, 0
|
26
|
+
|
27
|
+
compressed_buffer_size = FFI::Buffer.alloc_inout(:ulong).put_ulong(0, buffer_size)
|
28
|
+
compressed_buffer = FFI::MemoryPointer.new(buffer_size)
|
29
|
+
|
30
|
+
if level.nil?
|
31
|
+
result = FFI::Zlib.compress(compressed_buffer, compressed_buffer_size, @buffer, @data.length)
|
32
|
+
else
|
33
|
+
result = FFI::Zlib.compress2(compressed_buffer, compressed_buffer_size, @buffer, @data.length, level)
|
34
|
+
end
|
35
|
+
|
36
|
+
compressed_size = compressed_buffer_size.get_ulong(0)
|
37
|
+
assert_equal result, FFI::Zlib::Z_OK
|
38
|
+
|
39
|
+
uncompressed_buffer_size = FFI::Buffer.alloc_inout(:ulong).put_ulong(0, @data.length)
|
40
|
+
uncompressed_buffer = FFI::MemoryPointer.new(@data.length)
|
41
|
+
result = FFI::Zlib.uncompress(uncompressed_buffer, uncompressed_buffer_size, compressed_buffer, compressed_size)
|
42
|
+
|
43
|
+
uncompressed_size = uncompressed_buffer_size.get_ulong(0)
|
44
|
+
uncompressed = uncompressed_buffer.get_string(0)
|
45
|
+
assert_equal result, FFI::Zlib::Z_OK
|
46
|
+
assert_equal uncompressed_size, @data.length
|
47
|
+
assert_equal uncompressed, @data
|
48
|
+
|
49
|
+
return compressed_size
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "ffi/zlib"
|
3
|
+
require "test/helper.rb"
|
4
|
+
|
5
|
+
class TestDeflate < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@data = File.read(File.join("test", "fixtures", "pale_blue_dot.txt"))
|
9
|
+
@buffer = FFI::MemoryPointer.new(@data.length)
|
10
|
+
@buffer.put_string(0, @data)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_deflate
|
14
|
+
s1 = do_test_deflate(FFI::Zlib::Z_NO_COMPRESSION)
|
15
|
+
s2 = do_test_deflate(FFI::Zlib::Z_BEST_SPEED)
|
16
|
+
s3 = do_test_deflate(FFI::Zlib::Z_DEFAULT_COMPRESSION)
|
17
|
+
s4 = do_test_deflate(FFI::Zlib::Z_BEST_COMPRESSION)
|
18
|
+
|
19
|
+
assert s1 > s2
|
20
|
+
assert s2 > s3
|
21
|
+
assert s3 >= s4
|
22
|
+
end
|
23
|
+
|
24
|
+
def do_test_deflate(level)
|
25
|
+
avail_out = @data.length + 64
|
26
|
+
compressed_buffer = FFI::MemoryPointer.new(avail_out)
|
27
|
+
|
28
|
+
zstream = FFI::Zlib::Z_stream.new
|
29
|
+
zstream[:next_in] = @buffer
|
30
|
+
zstream[:avail_in] = @data.length
|
31
|
+
zstream[:next_out] = compressed_buffer
|
32
|
+
zstream[:avail_out] = avail_out
|
33
|
+
|
34
|
+
result = FFI::Zlib.deflateInit(zstream, level)
|
35
|
+
assert_equal result, FFI::Zlib::Z_OK
|
36
|
+
|
37
|
+
result = FFI::Zlib.deflate(zstream, FFI::Zlib::Z_FINISH)
|
38
|
+
assert_equal result, FFI::Zlib::Z_STREAM_END
|
39
|
+
|
40
|
+
result = FFI::Zlib.deflateEnd(zstream)
|
41
|
+
assert_equal result, FFI::Zlib::Z_OK
|
42
|
+
|
43
|
+
compressed_size = zstream[:total_out]
|
44
|
+
assert compressed_size > 0
|
45
|
+
assert compressed_size <= @data.length if level != FFI::Zlib::Z_NO_COMPRESSION
|
46
|
+
|
47
|
+
decompressed_buffer = FFI::MemoryPointer.new(@data.length)
|
48
|
+
zstream = FFI::Zlib::Z_stream.new
|
49
|
+
zstream[:next_in] = compressed_buffer
|
50
|
+
zstream[:avail_in] = compressed_size
|
51
|
+
zstream[:next_out] = decompressed_buffer
|
52
|
+
zstream[:avail_out] = @data.length
|
53
|
+
|
54
|
+
result = FFI::Zlib.inflateInit(zstream)
|
55
|
+
assert_equal result, FFI::Zlib::Z_OK
|
56
|
+
|
57
|
+
result = FFI::Zlib.inflate(zstream, FFI::Zlib::Z_FINISH)
|
58
|
+
assert_equal result, FFI::Zlib::Z_STREAM_END
|
59
|
+
|
60
|
+
result = FFI::Zlib.inflateEnd(zstream)
|
61
|
+
assert_equal result, FFI::Zlib::Z_OK
|
62
|
+
|
63
|
+
decompressed_size = zstream[:total_out]
|
64
|
+
decompressed_data = decompressed_buffer.get_string(0, decompressed_size)
|
65
|
+
assert_equal decompressed_size, @data.length
|
66
|
+
assert_equal decompressed_data, @data
|
67
|
+
|
68
|
+
return compressed_size
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/test/test_gz.rb
ADDED
data/test/test_misc.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "ffi/zlib"
|
3
|
+
require "test/helper.rb"
|
4
|
+
|
5
|
+
class TestMisc < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_version
|
8
|
+
assert_not_nil FFI::Zlib.zlibVersion
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_compile_flags
|
12
|
+
assert_not_equal FFI::Zlib.zlibCompileFlags, 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_crc_table
|
16
|
+
table = FFI::Zlib.get_crc_table
|
17
|
+
assert_not_nil table
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_errors
|
21
|
+
do_test_error FFI::Zlib::Z_OK
|
22
|
+
do_test_error FFI::Zlib::Z_STREAM_END
|
23
|
+
do_test_error FFI::Zlib::Z_NEED_DICT
|
24
|
+
do_test_error FFI::Zlib::Z_ERRNO
|
25
|
+
do_test_error FFI::Zlib::Z_STREAM_ERROR
|
26
|
+
do_test_error FFI::Zlib::Z_DATA_ERROR
|
27
|
+
do_test_error FFI::Zlib::Z_MEM_ERROR
|
28
|
+
do_test_error FFI::Zlib::Z_BUF_ERROR
|
29
|
+
do_test_error FFI::Zlib::Z_VERSION_ERROR
|
30
|
+
end
|
31
|
+
|
32
|
+
def do_test_error(err)
|
33
|
+
err_str = FFI::Zlib.zError(err)
|
34
|
+
assert_not_nil err_str
|
35
|
+
assert_instance_of String, err_str
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ffi-zlib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Luc Heinrich
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-05-31 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: ffi
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: ffi-zlib provides a very thin wrapper around zlib using the Ruby FFI library.
|
36
|
+
email: luc@honk-honk.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/ffi/zlib.rb
|
45
|
+
- test/fixtures/pale_blue_dot.txt
|
46
|
+
- README.md
|
47
|
+
- LICENSE
|
48
|
+
- test/test_checksums.rb
|
49
|
+
- test/test_compress.rb
|
50
|
+
- test/test_deflate.rb
|
51
|
+
- test/test_gz.rb
|
52
|
+
- test/test_misc.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/lucsky/ffi-zlib
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: FFI based wrapper for zlib.
|
87
|
+
test_files:
|
88
|
+
- test/test_checksums.rb
|
89
|
+
- test/test_compress.rb
|
90
|
+
- test/test_deflate.rb
|
91
|
+
- test/test_gz.rb
|
92
|
+
- test/test_misc.rb
|