pr-zlib 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +2 -0
- data/MANIFEST +18 -0
- data/README +42 -0
- data/Rakefile +88 -0
- data/bin/minizip.rb +173 -0
- data/examples/example_rbzlib.rb +385 -0
- data/lib/pr/rbzlib.rb +5067 -0
- data/lib/pr/zlib.rb +1550 -0
- data/pr-zlib.gemspec +28 -0
- data/test/test_rbzlib.rb +136 -0
- data/test/test_rbzlib_bytef.rb +79 -0
- data/test/test_rbzlib_posf.rb +59 -0
- data/test/test_zlib.rb +163 -0
- data/test/test_zlib_deflate.rb +58 -0
- data/test/test_zlib_gzip_file.rb +96 -0
- data/test/test_zlib_gzip_reader.rb +184 -0
- data/test/test_zlib_gzip_writer.rb +188 -0
- data/test/test_zlib_inflate.rb +58 -0
- data/test/test_zlib_zstream.rb +149 -0
- metadata +94 -0
data/CHANGES
ADDED
data/MANIFEST
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
CHANGES
|
2
|
+
MANIFEST
|
3
|
+
README
|
4
|
+
pr-zlib.gemspec
|
5
|
+
bin/minizip.rb
|
6
|
+
examples/example_rbzlib.rb
|
7
|
+
lib/pr/rbzlib.rb
|
8
|
+
lib/pr/zlib.rb
|
9
|
+
test/test_rbzlib_bytef.rb
|
10
|
+
test/test_rbzlib_posf.rb
|
11
|
+
test/test_rbzlib.rb
|
12
|
+
test/test_zlib_deflate.rb
|
13
|
+
test/test_zlib_gzip_file.rb
|
14
|
+
test/test_zlib_gzip_reader.rb
|
15
|
+
test/test_zlib_gzip_writer.rb
|
16
|
+
test/test_zlib_inflate.rb
|
17
|
+
test/test_zlib_zstream.rb
|
18
|
+
test/test_zlib.rb
|
data/README
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
= Description
|
2
|
+
The pr-zlib library is a pure Ruby version of the zlib compression library.
|
3
|
+
It consists of both a port of zlib.h and the Ruby zlib library that ships as
|
4
|
+
part of the standard library.
|
5
|
+
|
6
|
+
= Synopsis
|
7
|
+
|
8
|
+
# Imitating a bit of code used in rubygems
|
9
|
+
require 'pr/zlib'
|
10
|
+
require 'stringio'
|
11
|
+
|
12
|
+
data = StringIO.new(data)
|
13
|
+
Zlib::GzipReader.new(data).read
|
14
|
+
|
15
|
+
= Motivation
|
16
|
+
|
17
|
+
First, building the zlib C library on MS Windows with Visual C++ is very
|
18
|
+
difficult. However, certain libraries depend on zlib, most notably rubygems.
|
19
|
+
By providing a pure Ruby version we eliminate any compiler or platform
|
20
|
+
compatability issues.
|
21
|
+
|
22
|
+
Second, even some Unix distributions, such as Debian, do not ship with
|
23
|
+
the zlib library by default. By creating a pure Ruby version of the zlib
|
24
|
+
library we eliminate the need to install a 3rd party C library, and
|
25
|
+
eliminate a potential weak link in the dependency chain.
|
26
|
+
|
27
|
+
Third, by creating pure Ruby versions of the library and the interface we
|
28
|
+
are more likely to receive patches, feature requests, documentation updates,
|
29
|
+
etc, from the Ruby community since not everyone who knows Ruby also knows C.
|
30
|
+
|
31
|
+
Last, the zlib interface that ships as part of the stdlib is a little on the
|
32
|
+
clunky side. By providing a pure Ruby version, authors can create their own
|
33
|
+
interface as they see fit.
|
34
|
+
|
35
|
+
= TODO
|
36
|
+
|
37
|
+
More tests, and better tests, are needed for both Rbzlib and Zlib.
|
38
|
+
|
39
|
+
= Authors
|
40
|
+
|
41
|
+
* Park Heesob (C translation)
|
42
|
+
* Daniel Berger (Testing, packaging, deployment)
|
data/Rakefile
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rbconfig'
|
4
|
+
|
5
|
+
desc 'Install the pr-zlib library'
|
6
|
+
task :install do
|
7
|
+
install_dir = File.join(Config::CONFIG['sitelibdir'], 'pr')
|
8
|
+
Dir.mkdir(install_dir) unless File.exists?(install_dir)
|
9
|
+
files = ['lib/pr/zlib.rb', 'lib/pr/rbzlib.rb']
|
10
|
+
files.each{ |file|
|
11
|
+
FileUtils.cp(file, install_dir, :verbose => true)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Install the pr-zlib library as zlib'
|
16
|
+
task :install_as_zlib do
|
17
|
+
install_dir = File.join(Config::CONFIG['sitelibdir'], 'pr')
|
18
|
+
Dir.mkdir(install_dir) unless File.exists?(install_dir)
|
19
|
+
|
20
|
+
FileUtils.cp('lib/pr/zlib.rb', Config::CONFIG['sitelibdir'], :verbose => true)
|
21
|
+
FileUtils.cp('lib/pr/rbzlib.rb', install_dir, :verbose => true)
|
22
|
+
end
|
23
|
+
|
24
|
+
desc 'Install the pr-zlib library as a gem'
|
25
|
+
task :install_gem do
|
26
|
+
ruby 'pr-zlib.gemspec'
|
27
|
+
file = Dir["*.gem"].first
|
28
|
+
sh "gem install #{file}"
|
29
|
+
end
|
30
|
+
|
31
|
+
Rake::TestTask.new do |t|
|
32
|
+
t.warning = true
|
33
|
+
t.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
Rake::TestTask.new('test_zlib') do |t|
|
37
|
+
t.warning = true
|
38
|
+
t.verbose = true
|
39
|
+
t.test_files = FileList['test/test_zlib.rb']
|
40
|
+
end
|
41
|
+
|
42
|
+
Rake::TestTask.new('test_gzip_file') do |t|
|
43
|
+
t.warning = true
|
44
|
+
t.verbose = true
|
45
|
+
t.test_files = FileList['test/test_zlib_gzip_file.rb']
|
46
|
+
end
|
47
|
+
|
48
|
+
Rake::TestTask.new('test_gzip_reader') do |t|
|
49
|
+
t.warning = true
|
50
|
+
t.verbose = true
|
51
|
+
t.test_files = FileList['test/test_zlib_gzip_reader.rb']
|
52
|
+
end
|
53
|
+
|
54
|
+
Rake::TestTask.new('test_gzip_writer') do |t|
|
55
|
+
t.warning = true
|
56
|
+
t.verbose = true
|
57
|
+
t.test_files = FileList['test/test_zlib_gzip_writer.rb']
|
58
|
+
end
|
59
|
+
|
60
|
+
Rake::TestTask.new('test_deflate') do |t|
|
61
|
+
t.warning = true
|
62
|
+
t.verbose = true
|
63
|
+
t.test_files = FileList['test/test_zlib_deflate.rb']
|
64
|
+
end
|
65
|
+
|
66
|
+
Rake::TestTask.new('test_inflate') do |t|
|
67
|
+
t.warning = true
|
68
|
+
t.verbose = true
|
69
|
+
t.test_files = FileList['test/test_zlib_inflate.rb']
|
70
|
+
end
|
71
|
+
|
72
|
+
Rake::TestTask.new('test_rbzlib') do |t|
|
73
|
+
t.warning = true
|
74
|
+
t.verbose = true
|
75
|
+
t.test_files = FileList['test/test_rbzlib.rb']
|
76
|
+
end
|
77
|
+
|
78
|
+
Rake::TestTask.new('test_rbzlib_bytef') do |t|
|
79
|
+
t.warning = true
|
80
|
+
t.verbose = true
|
81
|
+
t.test_files = FileList['test/test_rbzlib_bytef.rb']
|
82
|
+
end
|
83
|
+
|
84
|
+
Rake::TestTask.new('test_rbzlib_posf') do |t|
|
85
|
+
t.warning = true
|
86
|
+
t.verbose = true
|
87
|
+
t.test_files = FileList['test/test_rbzlib_posf.rb']
|
88
|
+
end
|
data/bin/minizip.rb
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
# minigzip.rb -- simulate gzip using the zlib compression library
|
2
|
+
# Copyright (C) 1995-2005 Jean-loup Gailly.
|
3
|
+
# For conditions of distribution and use, see copyright notice in rbzlib.rb
|
4
|
+
#
|
5
|
+
#
|
6
|
+
#
|
7
|
+
# minigzip is a minimal implementation of the gzip utility. This is
|
8
|
+
# only an example of using zlib and isn't meant to replace the
|
9
|
+
# full-featured gzip. No attempt is made to deal with file systems
|
10
|
+
# limiting names to 14 or 8+3 characters, etc... Error checking is
|
11
|
+
# very limited. So use minigzip only for testing; use gzip for the
|
12
|
+
# real thing. On MSDOS, use only on file names without extension
|
13
|
+
# or in pipe mode.
|
14
|
+
#
|
15
|
+
# Ruby tralnslation By Park Heesob.
|
16
|
+
|
17
|
+
require 'rbzlib'
|
18
|
+
include Rbzlib
|
19
|
+
|
20
|
+
GZ_SUFFIX = ".gz"
|
21
|
+
SUFFIX_LEN = GZ_SUFFIX.length
|
22
|
+
|
23
|
+
BUFLEN = 16384
|
24
|
+
MAX_NAME_LEN = 1024
|
25
|
+
|
26
|
+
|
27
|
+
def error(msg)
|
28
|
+
puts("#{__FILE__}: #{msg}")
|
29
|
+
exit(1)
|
30
|
+
end
|
31
|
+
|
32
|
+
def gz_compress(_in, out)
|
33
|
+
while(true)
|
34
|
+
begin
|
35
|
+
buf = _in.read(BUFLEN)
|
36
|
+
rescue
|
37
|
+
raise RuntimeError,"read"
|
38
|
+
end
|
39
|
+
break if buf.nil?
|
40
|
+
err = 0
|
41
|
+
len = buf.length
|
42
|
+
if (gzwrite(out, buf, len) != len)
|
43
|
+
error(gzerror(out, err))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
_in.close
|
47
|
+
error("failed gzclose") if (gzclose(out) != Z_OK)
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def gz_uncompress(_in, out)
|
52
|
+
buf = 0.chr * BUFLEN
|
53
|
+
while true
|
54
|
+
len = gzread(_in, buf, buf.length)
|
55
|
+
err = 0
|
56
|
+
error(gzerror(_in, err)) if (len < 0)
|
57
|
+
break if len.zero?
|
58
|
+
if(out.write(buf[0,len]) != len)
|
59
|
+
error("failed write")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
begin
|
63
|
+
out.close
|
64
|
+
rescue
|
65
|
+
error("failed fclose")
|
66
|
+
end
|
67
|
+
|
68
|
+
error("failed gzclose") if (gzclose(_in) != Z_OK)
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def file_compress(file, mode)
|
73
|
+
outfile = file + GZ_SUFFIX
|
74
|
+
|
75
|
+
_in = File.open(file, "rb")
|
76
|
+
if _in.nil?
|
77
|
+
raise RuntimeError,file
|
78
|
+
end
|
79
|
+
out = gzopen(outfile, mode)
|
80
|
+
if out.nil?
|
81
|
+
puts("#{__FILE__}: can't gzopen #{outfile}")
|
82
|
+
exit(1)
|
83
|
+
end
|
84
|
+
gz_compress(_in, out)
|
85
|
+
|
86
|
+
File.unlink(file)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
def file_uncompress(file)
|
91
|
+
len = file.length
|
92
|
+
buf = file.dup
|
93
|
+
|
94
|
+
if (file[-SUFFIX_LEN..-1] == GZ_SUFFIX)
|
95
|
+
infile = file.dup
|
96
|
+
outfile = buf[0..(-SUFFIX_LEN-1)]
|
97
|
+
else
|
98
|
+
outfile = file.dup
|
99
|
+
infile = buf + GZ_SUFFIX
|
100
|
+
end
|
101
|
+
_in = gzopen(infile, "rb")
|
102
|
+
if _in.nil?
|
103
|
+
puts("#{__FILE__}: can't gzopen #{infile}")
|
104
|
+
exit(1)
|
105
|
+
end
|
106
|
+
out = File.open(outfile, "wb")
|
107
|
+
if out.nil?
|
108
|
+
raise RuntimeError,file
|
109
|
+
exit(1)
|
110
|
+
end
|
111
|
+
|
112
|
+
gz_uncompress(_in, out)
|
113
|
+
|
114
|
+
File.unlink(infile)
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
#===========================================================================
|
119
|
+
# Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
|
120
|
+
# -d : decompress
|
121
|
+
# -f : compress with Z_FILTERED
|
122
|
+
# -h : compress with Z_HUFFMAN_ONLY
|
123
|
+
# -r : compress with Z_RLE
|
124
|
+
# -1 to -9 : compression level
|
125
|
+
#
|
126
|
+
|
127
|
+
uncompr = false
|
128
|
+
|
129
|
+
outmode = "wb6 "
|
130
|
+
|
131
|
+
while !ARGV.empty?
|
132
|
+
argv = ARGV.shift
|
133
|
+
case argv
|
134
|
+
when "-d"
|
135
|
+
uncompr = true
|
136
|
+
when "-f"
|
137
|
+
outmode[3] = 'f'
|
138
|
+
when "-h"
|
139
|
+
outmode[3] = 'h'
|
140
|
+
when "-r"
|
141
|
+
outmode[3] = 'R'
|
142
|
+
when "-1".."-9"
|
143
|
+
outmode[2] = argv[1]
|
144
|
+
else
|
145
|
+
ARGV.unshift(argv)
|
146
|
+
break
|
147
|
+
end
|
148
|
+
end
|
149
|
+
if (outmode[3].chr == ' ')
|
150
|
+
outmode = outmode[0,3]
|
151
|
+
end
|
152
|
+
if (ARGV.empty?)
|
153
|
+
$stdin.binmode
|
154
|
+
$stdout.binmode
|
155
|
+
if (uncompr)
|
156
|
+
file = gzdopen($stdin.fileno, "rb")
|
157
|
+
error("can't gzdopen stdin") if file.nil?
|
158
|
+
gz_uncompress(file, $stdout)
|
159
|
+
else
|
160
|
+
file = gzdopen($stdout.fileno, outmode)
|
161
|
+
error("can't gzdopen stdout") if file.nil?
|
162
|
+
gz_compress($stdin, file)
|
163
|
+
end
|
164
|
+
else
|
165
|
+
while !ARGV.empty?
|
166
|
+
if (uncompr)
|
167
|
+
file_uncompress(ARGV.shift)
|
168
|
+
else
|
169
|
+
file_compress(ARGV.shift, outmode)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
@@ -0,0 +1,385 @@
|
|
1
|
+
# example.rb -- usage example of the zlib compression library
|
2
|
+
# Copyright (C) 1995-2004 Jean-loup Gailly.
|
3
|
+
# For conditions of distribution and use, see copyright notice in rbzlib.rb
|
4
|
+
#
|
5
|
+
# Ruby translation by Park Heesob
|
6
|
+
|
7
|
+
require 'rbzlib'
|
8
|
+
include Rbzlib
|
9
|
+
|
10
|
+
def CHECK_ERR(err,msg)
|
11
|
+
if(err != Z_OK)
|
12
|
+
raise RuntimeError,"#{msg} error: #{err}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
TESTFILE = "foo.gz"
|
17
|
+
@@hello = "hello, hello!\0"
|
18
|
+
|
19
|
+
@@dictionary = "hello"
|
20
|
+
|
21
|
+
def test_compress(compr, comprLen, uncompr, uncomprLen)
|
22
|
+
len = @@hello.length
|
23
|
+
|
24
|
+
err,comprLen = compress(compr, comprLen, @@hello, len)
|
25
|
+
CHECK_ERR(err, "compress")
|
26
|
+
compr = compr[0,comprLen]
|
27
|
+
uncompr[0,7] = "garbage"
|
28
|
+
err,uncomprLen = uncompress(uncompr, uncomprLen, compr, comprLen)
|
29
|
+
CHECK_ERR(err, "uncompress")
|
30
|
+
uncompr = uncompr[0,uncomprLen]
|
31
|
+
if uncompr != @@hello
|
32
|
+
puts("bad uncompress")
|
33
|
+
exit(1)
|
34
|
+
else
|
35
|
+
puts("uncompress(): #{uncompr}")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_gzio(fname, uncompr, uncomprLen)
|
40
|
+
len = @@hello.length
|
41
|
+
err = 0
|
42
|
+
file = gzopen(fname, "wb")
|
43
|
+
if file.nil?
|
44
|
+
puts("gzopen error")
|
45
|
+
exit(1)
|
46
|
+
end
|
47
|
+
gzputc(file, 'h')
|
48
|
+
if (gzputs(file, "ello") != 4)
|
49
|
+
puts("gzputs err: #{gzerror(file, err)}")
|
50
|
+
exit(1)
|
51
|
+
end
|
52
|
+
if (gzputs(file, ", hello!") != 8)
|
53
|
+
puts("gzputs err: #{gzerror(file, err)}")
|
54
|
+
exit(1)
|
55
|
+
end
|
56
|
+
gzseek(file, 1, SEEK_CUR)
|
57
|
+
gzclose(file)
|
58
|
+
|
59
|
+
file = gzopen(fname, "rb")
|
60
|
+
if file.nil?
|
61
|
+
puts("gzopen error")
|
62
|
+
exit(1)
|
63
|
+
end
|
64
|
+
uncompr[0,7] = "garbage"
|
65
|
+
if (gzread(file, uncompr, uncomprLen) != len)
|
66
|
+
puts("gzread err: #{gzerror(file, err)}")
|
67
|
+
exit(1)
|
68
|
+
end
|
69
|
+
uncompr = uncompr[0,len]
|
70
|
+
if uncompr != @@hello
|
71
|
+
puts("bad gzread: #{uncompr}")
|
72
|
+
exit(1)
|
73
|
+
else
|
74
|
+
puts("gzread(): #{uncompr}")
|
75
|
+
end
|
76
|
+
pos = gzseek(file, -8, SEEK_CUR)
|
77
|
+
if (pos != 6 || gztell(file) != pos)
|
78
|
+
puts("gzseek error, pos=#{pos}, gztell=#{gztell(file)}")
|
79
|
+
exit(1)
|
80
|
+
end
|
81
|
+
|
82
|
+
if (gzgetc(file) != ' ')
|
83
|
+
puts("gzgetc error")
|
84
|
+
exit(1)
|
85
|
+
end
|
86
|
+
|
87
|
+
if (gzungetc(' ', file) != ' ')
|
88
|
+
puts("gzungetc error")
|
89
|
+
exit(1)
|
90
|
+
end
|
91
|
+
|
92
|
+
gzgets(file, uncompr, uncomprLen)
|
93
|
+
uncompr.chop!
|
94
|
+
if uncompr.length != 7
|
95
|
+
puts("gzgets err after gzseek: #{gzerror(file, err)}")
|
96
|
+
exit(1)
|
97
|
+
end
|
98
|
+
|
99
|
+
if uncompr != @@hello[6..-2]
|
100
|
+
puts("bad gzgets after gzseek")
|
101
|
+
exit(1)
|
102
|
+
else
|
103
|
+
puts("gzgets() after gzseek: #{uncompr}")
|
104
|
+
end
|
105
|
+
|
106
|
+
gzclose(file)
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_deflate(compr, comprLen)
|
110
|
+
c_stream = Z_stream.new
|
111
|
+
len = @@hello.length
|
112
|
+
|
113
|
+
err = deflateInit(c_stream, Z_DEFAULT_COMPRESSION)
|
114
|
+
CHECK_ERR(err, "deflateInit")
|
115
|
+
|
116
|
+
c_stream.next_in = Bytef.new(@@hello)
|
117
|
+
c_stream.next_out = Bytef.new(compr)
|
118
|
+
|
119
|
+
while (c_stream.total_in != len && c_stream.total_out < comprLen)
|
120
|
+
c_stream.avail_in = c_stream.avail_out = 1
|
121
|
+
err = deflate(c_stream, Z_NO_FLUSH)
|
122
|
+
CHECK_ERR(err, "deflate")
|
123
|
+
end
|
124
|
+
while true
|
125
|
+
c_stream.avail_out = 1
|
126
|
+
err = deflate(c_stream, Z_FINISH)
|
127
|
+
break if (err == Z_STREAM_END)
|
128
|
+
CHECK_ERR(err, "deflate")
|
129
|
+
end
|
130
|
+
|
131
|
+
err = deflateEnd(c_stream)
|
132
|
+
CHECK_ERR(err, "deflateEnd")
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_inflate(compr, comprLen, uncompr, uncomprLen)
|
136
|
+
uncompr[0,7] = "garbage"
|
137
|
+
d_stream = Z_stream.new
|
138
|
+
|
139
|
+
d_stream.next_in = Bytef.new(compr)
|
140
|
+
d_stream.avail_in = 0
|
141
|
+
d_stream.next_out = Bytef.new(uncompr)
|
142
|
+
|
143
|
+
err = inflateInit(d_stream)
|
144
|
+
CHECK_ERR(err, "inflateInit")
|
145
|
+
|
146
|
+
while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen)
|
147
|
+
d_stream.avail_in = d_stream.avail_out = 1
|
148
|
+
err = inflate(d_stream, Z_NO_FLUSH)
|
149
|
+
break if (err == Z_STREAM_END)
|
150
|
+
CHECK_ERR(err, "inflate")
|
151
|
+
end
|
152
|
+
|
153
|
+
err = inflateEnd(d_stream)
|
154
|
+
CHECK_ERR(err, "inflateEnd")
|
155
|
+
uncompr = uncompr[0,d_stream.total_out]
|
156
|
+
if uncompr != @@hello
|
157
|
+
puts("bad inflate")
|
158
|
+
exit(1)
|
159
|
+
else
|
160
|
+
puts("inflate(): #{uncompr}")
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_large_deflate(compr, comprLen, uncompr, uncomprLen)
|
165
|
+
c_stream = Z_stream.new
|
166
|
+
err = deflateInit(c_stream, Z_BEST_SPEED)
|
167
|
+
CHECK_ERR(err, "deflateInit")
|
168
|
+
|
169
|
+
c_stream.next_out = Bytef.new(compr)
|
170
|
+
c_stream.avail_out = comprLen
|
171
|
+
c_stream.next_in = Bytef.new(uncompr)
|
172
|
+
c_stream.avail_in = uncomprLen
|
173
|
+
err = deflate(c_stream, Z_NO_FLUSH)
|
174
|
+
CHECK_ERR(err, "deflate")
|
175
|
+
if c_stream.avail_in.nonzero?
|
176
|
+
puts("deflate not greedy")
|
177
|
+
exit(1)
|
178
|
+
end
|
179
|
+
|
180
|
+
deflateParams(c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY)
|
181
|
+
c_stream.next_in = Bytef.new(compr)
|
182
|
+
c_stream.avail_in = comprLen/2
|
183
|
+
err = deflate(c_stream, Z_NO_FLUSH)
|
184
|
+
CHECK_ERR(err, "deflate")
|
185
|
+
|
186
|
+
deflateParams(c_stream, Z_BEST_COMPRESSION, Z_FILTERED)
|
187
|
+
c_stream.next_in = Bytef.new(uncompr)
|
188
|
+
c_stream.avail_in = uncomprLen
|
189
|
+
err = deflate(c_stream, Z_NO_FLUSH)
|
190
|
+
CHECK_ERR(err, "deflate")
|
191
|
+
|
192
|
+
err = deflate(c_stream, Z_FINISH)
|
193
|
+
if (err != Z_STREAM_END)
|
194
|
+
puts("deflate should report Z_STREAM_END")
|
195
|
+
exit(1)
|
196
|
+
end
|
197
|
+
err = deflateEnd(c_stream)
|
198
|
+
CHECK_ERR(err, "deflateEnd")
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_large_inflate(compr, comprLen, uncompr, uncomprLen)
|
202
|
+
d_stream = Z_stream.new
|
203
|
+
uncompr[0,7] = "garbage"
|
204
|
+
|
205
|
+
d_stream.next_in = Bytef.new(compr)
|
206
|
+
d_stream.avail_in = comprLen
|
207
|
+
|
208
|
+
err = inflateInit(d_stream)
|
209
|
+
CHECK_ERR(err, "inflateInit")
|
210
|
+
|
211
|
+
while true
|
212
|
+
d_stream.next_out = Bytef.new(uncompr)
|
213
|
+
d_stream.avail_out = uncomprLen
|
214
|
+
err = inflate(d_stream, Z_NO_FLUSH)
|
215
|
+
break if (err == Z_STREAM_END)
|
216
|
+
CHECK_ERR(err, "large inflate")
|
217
|
+
end
|
218
|
+
|
219
|
+
err = inflateEnd(d_stream)
|
220
|
+
CHECK_ERR(err, "inflateEnd")
|
221
|
+
|
222
|
+
if (d_stream.total_out != 2*uncomprLen + comprLen/2)
|
223
|
+
puts("bad large inflate: #{d_stream.total_out}")
|
224
|
+
exit(1)
|
225
|
+
else
|
226
|
+
puts("large_inflate(): OK")
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_flush(compr, comprLen)
|
231
|
+
c_stream = Z_stream.new
|
232
|
+
len = @@hello.length
|
233
|
+
|
234
|
+
err = deflateInit(c_stream, Z_DEFAULT_COMPRESSION)
|
235
|
+
CHECK_ERR(err, "deflateInit")
|
236
|
+
|
237
|
+
c_stream.next_in = Bytef.new(@@hello)
|
238
|
+
c_stream.next_out = Bytef.new(compr)
|
239
|
+
c_stream.avail_in = 3
|
240
|
+
c_stream.avail_out = comprLen
|
241
|
+
err = deflate(c_stream, Z_FULL_FLUSH)
|
242
|
+
CHECK_ERR(err, "deflate")
|
243
|
+
|
244
|
+
compr[3]=(compr[3].ord+1).chr
|
245
|
+
c_stream.avail_in = len - 3
|
246
|
+
|
247
|
+
err = deflate(c_stream, Z_FINISH)
|
248
|
+
if (err != Z_STREAM_END)
|
249
|
+
CHECK_ERR(err, "deflate")
|
250
|
+
end
|
251
|
+
err = deflateEnd(c_stream)
|
252
|
+
CHECK_ERR(err, "deflateEnd")
|
253
|
+
|
254
|
+
comprLen = c_stream.total_out
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_sync(compr, comprLen, uncompr, uncomprLen)
|
258
|
+
d_stream = Z_stream.new
|
259
|
+
uncompr[0,7] = "garbage"
|
260
|
+
|
261
|
+
d_stream.next_in = Bytef.new(compr)
|
262
|
+
d_stream.avail_in = 2
|
263
|
+
|
264
|
+
err = inflateInit(d_stream)
|
265
|
+
CHECK_ERR(err, "inflateInit")
|
266
|
+
|
267
|
+
d_stream.next_out = Bytef.new(uncompr)
|
268
|
+
d_stream.avail_out = uncomprLen
|
269
|
+
|
270
|
+
inflate(d_stream, Z_NO_FLUSH)
|
271
|
+
CHECK_ERR(err, "inflate")
|
272
|
+
|
273
|
+
d_stream.avail_in = comprLen-2
|
274
|
+
err = inflateSync(d_stream)
|
275
|
+
CHECK_ERR(err, "inflateSync")
|
276
|
+
|
277
|
+
err = inflate(d_stream, Z_FINISH)
|
278
|
+
if (err != Z_DATA_ERROR)
|
279
|
+
puts("inflate should report DATA_ERROR")
|
280
|
+
exit(1)
|
281
|
+
end
|
282
|
+
err = inflateEnd(d_stream)
|
283
|
+
uncompr = uncompr[0,d_stream.total_out]
|
284
|
+
CHECK_ERR(err, "inflateEnd")
|
285
|
+
puts("after inflateSync(): hel#{uncompr}")
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_dict_deflate(compr, comprLen)
|
289
|
+
c_stream = Z_stream.new
|
290
|
+
err = deflateInit(c_stream, Z_BEST_COMPRESSION)
|
291
|
+
CHECK_ERR(err, "deflateInit")
|
292
|
+
|
293
|
+
err = deflateSetDictionary(c_stream,@@dictionary, @@dictionary.length)
|
294
|
+
CHECK_ERR(err, "deflateSetDictionary")
|
295
|
+
|
296
|
+
@@dictId = c_stream.adler
|
297
|
+
c_stream.next_out = Bytef.new(compr)
|
298
|
+
c_stream.avail_out = comprLen
|
299
|
+
|
300
|
+
c_stream.next_in = Bytef.new(@@hello)
|
301
|
+
c_stream.avail_in = @@hello.length
|
302
|
+
|
303
|
+
err = deflate(c_stream, Z_FINISH)
|
304
|
+
if (err != Z_STREAM_END)
|
305
|
+
puts("deflate should report Z_STREAM_END")
|
306
|
+
exit(1)
|
307
|
+
end
|
308
|
+
err = deflateEnd(c_stream)
|
309
|
+
CHECK_ERR(err, "deflateEnd")
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
|
313
|
+
d_stream = Z_stream.new
|
314
|
+
uncompr[0,7] = "garbage"
|
315
|
+
|
316
|
+
d_stream.next_in = Bytef.new(compr)
|
317
|
+
d_stream.avail_in = comprLen
|
318
|
+
|
319
|
+
err = inflateInit(d_stream)
|
320
|
+
CHECK_ERR(err, "inflateInit")
|
321
|
+
d_stream.next_out = Bytef.new(uncompr)
|
322
|
+
d_stream.avail_out = uncomprLen
|
323
|
+
|
324
|
+
while true
|
325
|
+
err = inflate(d_stream, Z_NO_FLUSH)
|
326
|
+
break if (err == Z_STREAM_END)
|
327
|
+
if (err == Z_NEED_DICT)
|
328
|
+
if (d_stream.adler != @@dictId)
|
329
|
+
puts("unexpected dictionary")
|
330
|
+
exit(1)
|
331
|
+
end
|
332
|
+
err = inflateSetDictionary(d_stream, @@dictionary,@@dictionary.length)
|
333
|
+
|
334
|
+
end
|
335
|
+
CHECK_ERR(err, "inflate with dict")
|
336
|
+
end
|
337
|
+
|
338
|
+
err = inflateEnd(d_stream)
|
339
|
+
CHECK_ERR(err, "inflateEnd")
|
340
|
+
uncompr = uncompr[0,d_stream.total_out]
|
341
|
+
if uncompr != @@hello
|
342
|
+
puts("bad inflate with dict")
|
343
|
+
exit(1)
|
344
|
+
else
|
345
|
+
puts("inflate with dictionary: #{uncompr}")
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
comprLen = 10000*4
|
350
|
+
uncomprLen = comprLen
|
351
|
+
myVersion = ZLIB_VERSION
|
352
|
+
|
353
|
+
if (zlibVersion[0] != myVersion[0])
|
354
|
+
puts("incompatible zlib version")
|
355
|
+
exit(1)
|
356
|
+
elsif (zlibVersion != ZLIB_VERSION)
|
357
|
+
puts("warning: different zlib version")
|
358
|
+
end
|
359
|
+
|
360
|
+
compr = 0.chr * comprLen
|
361
|
+
uncompr = 0.chr * uncomprLen
|
362
|
+
if (compr.nil? || uncompr.nil?)
|
363
|
+
puts("out of memory")
|
364
|
+
exit(1)
|
365
|
+
end
|
366
|
+
test_compress(compr, comprLen, uncompr, uncomprLen)
|
367
|
+
|
368
|
+
test_gzio((ARGV.length > 0 ? ARGV[0] : TESTFILE),
|
369
|
+
uncompr, uncomprLen)
|
370
|
+
|
371
|
+
test_deflate(compr, comprLen)
|
372
|
+
test_inflate(compr, comprLen, uncompr, uncomprLen)
|
373
|
+
test_large_deflate(compr, comprLen, uncompr, uncomprLen)
|
374
|
+
test_large_inflate(compr, comprLen, uncompr, uncomprLen)
|
375
|
+
|
376
|
+
test_flush(compr, comprLen)
|
377
|
+
|
378
|
+
test_sync(compr, comprLen, uncompr, uncomprLen)
|
379
|
+
comprLen = uncomprLen
|
380
|
+
|
381
|
+
test_dict_deflate(compr, comprLen)
|
382
|
+
test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
|
383
|
+
|
384
|
+
|
385
|
+
|