pr-zlib 1.0.0 → 1.0.1
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 +7 -0
- data/CHANGES +8 -0
- data/Rakefile +78 -47
- data/lib/pr/rbzlib.rb +811 -818
- data/lib/pr/zlib.rb +16 -16
- data/pr-zlib.gemspec +17 -23
- data/profile/bench_pr_zlib.rb +43 -0
- data/profile/bench_zlib.rb +43 -0
- data/profile/profile_pr_zlib_read.rb +28 -0
- data/profile/profile_pr_zlib_write.rb +26 -0
- data/test/test_rbzlib.rb +124 -127
- data/test/test_rbzlib_bytef.rb +2 -5
- data/test/test_rbzlib_posf.rb +1 -4
- data/test/test_zlib.rb +2 -5
- data/test/test_zlib_deflate.rb +1 -4
- data/test/test_zlib_gzip_file.rb +1 -4
- data/test/test_zlib_gzip_reader.rb +173 -174
- data/test/test_zlib_gzip_writer.rb +175 -179
- data/test/test_zlib_inflate.rb +1 -4
- data/test/test_zlib_zstream.rb +1 -4
- metadata +61 -45
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 942c045dcb34136c2bbf3b77d92f77735482350d
|
4
|
+
data.tar.gz: 0c2682879c45ee2f5aee7dce9d5fde4b20a00402
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 231ab3d027b37a370fc865d304f2f5a6d6779c829829d3c1d73ad65f635505aba3c7b6a436c8b7acd67f674645f5538ee23daf7e8d33472f4d89e31dc2c14102
|
7
|
+
data.tar.gz: adebebe6431c37c4705f7a94c86c2b67578a53a9885ab314001c07f2b7f18eecbd9829e9ddb35e9051a8610da15067d2a5c3af99e59ba91fb8d88b3897f73f9a
|
data/CHANGES
CHANGED
@@ -1,2 +1,10 @@
|
|
1
|
+
= 1.0.1 - 16-Oct-2014
|
2
|
+
* Added benchmark and profiler scripts and rake tasks.
|
3
|
+
* Switched profiling scripts to use ruby-prof.
|
4
|
+
* The test-unit and ruby-prof libraries are now development dependencies.
|
5
|
+
* Minor updates to eliminate warnings for Ruby 1.9.3 and 2.1.
|
6
|
+
* Some minor test suite updates, mostly for 1.9.3.
|
7
|
+
* Updated the gemspec, removed Rubyforge references.
|
8
|
+
|
1
9
|
= 1.0.0 - 12-Jun-2009
|
2
10
|
* Initial release
|
data/Rakefile
CHANGED
@@ -1,88 +1,119 @@
|
|
1
1
|
require 'rake'
|
2
|
+
require 'rake/clean'
|
2
3
|
require 'rake/testtask'
|
3
4
|
require 'rbconfig'
|
4
5
|
|
5
|
-
|
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
|
6
|
+
CLEAN.include("**/*.rbc", "**/*.gem", "**/*.txt", "**/*.gz")
|
14
7
|
|
15
8
|
desc 'Install the pr-zlib library as zlib'
|
16
9
|
task :install_as_zlib do
|
17
|
-
|
18
|
-
|
10
|
+
install_dir = File.join(Config::CONFIG['sitelibdir'], 'pr')
|
11
|
+
Dir.mkdir(install_dir) unless File.exists?(install_dir)
|
19
12
|
|
20
|
-
|
21
|
-
|
13
|
+
cp('lib/pr/zlib.rb', Config::CONFIG['sitelibdir'], :verbose => true)
|
14
|
+
cp('lib/pr/rbzlib.rb', install_dir, :verbose => true)
|
22
15
|
end
|
23
16
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
17
|
+
namespace :gem do
|
18
|
+
desc 'Create the pr-zlib gem'
|
19
|
+
task :create do
|
20
|
+
spec = eval(IO.read('pr-zlib.gemspec'))
|
21
|
+
if Gem::VERSION < "2.0"
|
22
|
+
Gem::Builder.new(spec).build
|
23
|
+
else
|
24
|
+
require 'rubygems/package'
|
25
|
+
Gem::Package.build(spec)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'Install the pr-zlib gem'
|
30
|
+
task :install => [:create] do
|
31
|
+
file = Dir["*.gem"].first
|
32
|
+
sh "gem install -l #{file}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
namespace :bench do
|
37
|
+
desc "Run the zlib benchmark"
|
38
|
+
task :zlib do
|
39
|
+
Dir.chdir('profile'){ ruby "bench_zlib.rb" }
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "Run the pr-zlib benchmark"
|
43
|
+
task :przlib do
|
44
|
+
sh "ruby -Ilib profile/bench_pr_zlib.rb"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
namespace :profile do
|
49
|
+
desc "Run the profiler on the write operation"
|
50
|
+
task :write do
|
51
|
+
sh "ruby -Ilib profile/profile_pr_zlib_write.rb"
|
52
|
+
end
|
53
|
+
|
54
|
+
desc "Run the profiler on the read operation"
|
55
|
+
task :read do
|
56
|
+
sh "ruby -Ilib profile/profile_pr_zlib_read.rb"
|
57
|
+
end
|
29
58
|
end
|
30
59
|
|
31
60
|
Rake::TestTask.new do |t|
|
32
|
-
|
33
|
-
|
61
|
+
t.warning = true
|
62
|
+
t.verbose = true
|
34
63
|
end
|
35
64
|
|
36
65
|
Rake::TestTask.new('test_zlib') do |t|
|
37
|
-
|
38
|
-
|
39
|
-
|
66
|
+
t.warning = true
|
67
|
+
t.verbose = true
|
68
|
+
t.test_files = FileList['test/test_zlib.rb']
|
40
69
|
end
|
41
70
|
|
42
71
|
Rake::TestTask.new('test_gzip_file') do |t|
|
43
|
-
|
44
|
-
|
45
|
-
|
72
|
+
t.warning = true
|
73
|
+
t.verbose = true
|
74
|
+
t.test_files = FileList['test/test_zlib_gzip_file.rb']
|
46
75
|
end
|
47
76
|
|
48
77
|
Rake::TestTask.new('test_gzip_reader') do |t|
|
49
|
-
|
50
|
-
|
51
|
-
|
78
|
+
t.warning = true
|
79
|
+
t.verbose = true
|
80
|
+
t.test_files = FileList['test/test_zlib_gzip_reader.rb']
|
52
81
|
end
|
53
82
|
|
54
83
|
Rake::TestTask.new('test_gzip_writer') do |t|
|
55
|
-
|
56
|
-
|
57
|
-
|
84
|
+
t.warning = true
|
85
|
+
t.verbose = true
|
86
|
+
t.test_files = FileList['test/test_zlib_gzip_writer.rb']
|
58
87
|
end
|
59
88
|
|
60
89
|
Rake::TestTask.new('test_deflate') do |t|
|
61
|
-
|
62
|
-
|
63
|
-
|
90
|
+
t.warning = true
|
91
|
+
t.verbose = true
|
92
|
+
t.test_files = FileList['test/test_zlib_deflate.rb']
|
64
93
|
end
|
65
94
|
|
66
95
|
Rake::TestTask.new('test_inflate') do |t|
|
67
|
-
|
68
|
-
|
69
|
-
|
96
|
+
t.warning = true
|
97
|
+
t.verbose = true
|
98
|
+
t.test_files = FileList['test/test_zlib_inflate.rb']
|
70
99
|
end
|
71
100
|
|
72
101
|
Rake::TestTask.new('test_rbzlib') do |t|
|
73
|
-
|
74
|
-
|
75
|
-
|
102
|
+
t.warning = true
|
103
|
+
t.verbose = true
|
104
|
+
t.test_files = FileList['test/test_rbzlib.rb']
|
76
105
|
end
|
77
106
|
|
78
107
|
Rake::TestTask.new('test_rbzlib_bytef') do |t|
|
79
|
-
|
80
|
-
|
81
|
-
|
108
|
+
t.warning = true
|
109
|
+
t.verbose = true
|
110
|
+
t.test_files = FileList['test/test_rbzlib_bytef.rb']
|
82
111
|
end
|
83
112
|
|
84
113
|
Rake::TestTask.new('test_rbzlib_posf') do |t|
|
85
|
-
|
86
|
-
|
87
|
-
|
114
|
+
t.warning = true
|
115
|
+
t.verbose = true
|
116
|
+
t.test_files = FileList['test/test_rbzlib_posf.rb']
|
88
117
|
end
|
118
|
+
|
119
|
+
task :default => :test
|
data/lib/pr/rbzlib.rb
CHANGED
@@ -31,927 +31,920 @@
|
|
31
31
|
#
|
32
32
|
|
33
33
|
class Fixnum
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
def ord
|
35
|
+
self
|
36
|
+
end
|
37
37
|
end
|
38
38
|
|
39
39
|
module Rbzlib
|
40
|
+
MAX_MEM_LEVEL = 9
|
41
|
+
DEF_MEM_LEVEL = 8
|
42
|
+
MAX_WBITS = 15
|
43
|
+
DEF_WBITS = MAX_WBITS
|
44
|
+
|
45
|
+
Z_stream = Struct.new(
|
46
|
+
:next_in,
|
47
|
+
:avail_in,
|
48
|
+
:total_in,
|
49
|
+
:next_out,
|
50
|
+
:avail_out,
|
51
|
+
:total_out,
|
52
|
+
:msg,
|
53
|
+
:state,
|
54
|
+
:data_type,
|
55
|
+
:adler,
|
56
|
+
:reserved
|
57
|
+
)
|
58
|
+
|
59
|
+
Gz_header = Struct.new(
|
60
|
+
:text,
|
61
|
+
:time,
|
62
|
+
:xflags,
|
63
|
+
:os,
|
64
|
+
:extra,
|
65
|
+
:extra_len,
|
66
|
+
:extra_max,
|
67
|
+
:name,
|
68
|
+
:name_max,
|
69
|
+
:comment,
|
70
|
+
:comm_max,
|
71
|
+
:hcrc,
|
72
|
+
:done
|
73
|
+
)
|
74
|
+
|
75
|
+
Z_NO_FLUSH = 0
|
76
|
+
Z_PARTIAL_FLUSH = 1
|
77
|
+
Z_SYNC_FLUSH = 2
|
78
|
+
Z_FULL_FLUSH = 3
|
79
|
+
Z_FINISH = 4
|
80
|
+
Z_BLOCK = 5
|
81
|
+
|
82
|
+
Z_OK = 0
|
83
|
+
Z_STREAM_END = 1
|
84
|
+
Z_NEED_DICT = 2
|
85
|
+
Z_ERRNO = (-1)
|
86
|
+
Z_STREAM_ERROR = (-2)
|
87
|
+
Z_DATA_ERROR = (-3)
|
88
|
+
Z_MEM_ERROR = (-4)
|
89
|
+
Z_BUF_ERROR = (-5)
|
90
|
+
Z_VERSION_ERROR = (-6)
|
91
|
+
|
92
|
+
Z_NO_COMPRESSION = 0
|
93
|
+
Z_BEST_SPEED = 1
|
94
|
+
Z_BEST_COMPRESSION = 9
|
95
|
+
Z_DEFAULT_COMPRESSION = (-1)
|
96
|
+
|
97
|
+
Z_FILTERED = 1
|
98
|
+
Z_HUFFMAN_ONLY = 2
|
99
|
+
Z_RLE = 3
|
100
|
+
Z_FIXED = 4
|
101
|
+
Z_DEFAULT_STRATEGY = 0
|
102
|
+
|
103
|
+
Z_BINARY = 0
|
104
|
+
Z_ASCII = 1
|
105
|
+
Z_TEXT = 1
|
106
|
+
Z_UNKNOWN = 2
|
107
|
+
|
108
|
+
Z_DEFLATED = 8
|
109
|
+
|
110
|
+
STORED_BLOCK = 0
|
111
|
+
STATIC_TREES = 1
|
112
|
+
DYN_TREES = 2
|
113
|
+
|
114
|
+
MIN_MATCH = 3
|
115
|
+
MAX_MATCH = 258
|
116
|
+
PRESET_DICT = 0x20
|
117
|
+
|
118
|
+
ZLIB_VERSION = '1.2.3'
|
119
|
+
|
120
|
+
Z_errbase = Z_NEED_DICT
|
121
|
+
|
122
|
+
@@z_errmsg = [
|
123
|
+
'need dictionary',
|
124
|
+
'stream end',
|
125
|
+
'',
|
126
|
+
'file error',
|
127
|
+
'stream error',
|
128
|
+
'data error',
|
129
|
+
'insufficient memory',
|
130
|
+
'buffer error',
|
131
|
+
'incompatible version',
|
132
|
+
''
|
133
|
+
]
|
134
|
+
|
135
|
+
@@z_verbose = 1
|
136
|
+
|
137
|
+
def zError(err)
|
138
|
+
@@z_errmsg[Z_NEED_DICT - err]
|
139
|
+
end
|
40
140
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
DEF_WBITS = MAX_WBITS
|
45
|
-
|
46
|
-
Z_stream = Struct.new(
|
47
|
-
:next_in,
|
48
|
-
:avail_in,
|
49
|
-
:total_in,
|
50
|
-
:next_out,
|
51
|
-
:avail_out,
|
52
|
-
:total_out,
|
53
|
-
:msg,
|
54
|
-
:state,
|
55
|
-
:data_type,
|
56
|
-
:adler,
|
57
|
-
:reserved
|
58
|
-
)
|
59
|
-
|
60
|
-
Gz_header = Struct.new(
|
61
|
-
:text,
|
62
|
-
:time,
|
63
|
-
:xflags,
|
64
|
-
:os,
|
65
|
-
:extra,
|
66
|
-
:extra_len,
|
67
|
-
:extra_max,
|
68
|
-
:name,
|
69
|
-
:name_max,
|
70
|
-
:comment,
|
71
|
-
:comm_max,
|
72
|
-
:hcrc,
|
73
|
-
:done
|
74
|
-
)
|
75
|
-
|
76
|
-
Z_NO_FLUSH = 0
|
77
|
-
Z_PARTIAL_FLUSH = 1
|
78
|
-
Z_SYNC_FLUSH = 2
|
79
|
-
Z_FULL_FLUSH = 3
|
80
|
-
Z_FINISH = 4
|
81
|
-
Z_BLOCK = 5
|
82
|
-
|
83
|
-
Z_OK = 0
|
84
|
-
Z_STREAM_END = 1
|
85
|
-
Z_NEED_DICT = 2
|
86
|
-
Z_ERRNO = (-1)
|
87
|
-
Z_STREAM_ERROR = (-2)
|
88
|
-
Z_DATA_ERROR = (-3)
|
89
|
-
Z_MEM_ERROR = (-4)
|
90
|
-
Z_BUF_ERROR = (-5)
|
91
|
-
Z_VERSION_ERROR = (-6)
|
92
|
-
|
93
|
-
Z_NO_COMPRESSION = 0
|
94
|
-
Z_BEST_SPEED = 1
|
95
|
-
Z_BEST_COMPRESSION = 9
|
96
|
-
Z_DEFAULT_COMPRESSION = (-1)
|
97
|
-
|
98
|
-
Z_FILTERED = 1
|
99
|
-
Z_HUFFMAN_ONLY = 2
|
100
|
-
Z_RLE = 3
|
101
|
-
Z_FIXED = 4
|
102
|
-
Z_DEFAULT_STRATEGY = 0
|
103
|
-
|
104
|
-
Z_BINARY = 0
|
105
|
-
Z_ASCII = 1
|
106
|
-
Z_TEXT = 1
|
107
|
-
Z_UNKNOWN = 2
|
108
|
-
|
109
|
-
Z_DEFLATED = 8
|
110
|
-
|
111
|
-
STORED_BLOCK = 0
|
112
|
-
STATIC_TREES = 1
|
113
|
-
DYN_TREES = 2
|
114
|
-
|
115
|
-
MIN_MATCH = 3
|
116
|
-
MAX_MATCH = 258
|
117
|
-
PRESET_DICT = 0x20
|
118
|
-
|
119
|
-
ZLIB_VERSION = '1.2.3'
|
120
|
-
|
121
|
-
Z_errbase = Z_NEED_DICT
|
122
|
-
|
123
|
-
@@z_errmsg = [
|
124
|
-
'need dictionary',
|
125
|
-
'stream end',
|
126
|
-
'',
|
127
|
-
'file error',
|
128
|
-
'stream error',
|
129
|
-
'data error',
|
130
|
-
'insufficient memory',
|
131
|
-
'buffer error',
|
132
|
-
'incompatible version',
|
133
|
-
''
|
134
|
-
]
|
135
|
-
|
136
|
-
@@z_verbose = 1
|
137
|
-
|
138
|
-
def zError(err)
|
139
|
-
@@z_errmsg[Z_NEED_DICT - err]
|
140
|
-
end
|
141
|
-
|
142
|
-
def zlibVersion()
|
143
|
-
ZLIB_VERSION
|
144
|
-
end
|
141
|
+
def zlibVersion
|
142
|
+
ZLIB_VERSION
|
143
|
+
end
|
145
144
|
|
146
|
-
|
147
|
-
|
148
|
-
|
145
|
+
def z_error(m)
|
146
|
+
raise RuntimeError, m
|
147
|
+
end
|
149
148
|
|
150
|
-
|
151
|
-
|
149
|
+
class Bytef
|
150
|
+
attr_accessor :buffer, :offset
|
152
151
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
end
|
152
|
+
def initialize(buffer, offset=0)
|
153
|
+
if [String, Array].include?(buffer.class)
|
154
|
+
@buffer = buffer
|
155
|
+
@offset = offset
|
156
|
+
else
|
157
|
+
@buffer = buffer.buffer
|
158
|
+
@offset = offset
|
161
159
|
end
|
160
|
+
end
|
162
161
|
|
163
|
-
|
164
|
-
|
165
|
-
|
162
|
+
def length
|
163
|
+
@buffer.length
|
164
|
+
end
|
166
165
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
166
|
+
def +(inc)
|
167
|
+
@offset += inc
|
168
|
+
self
|
169
|
+
end
|
171
170
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
171
|
+
def -(dec)
|
172
|
+
@offset -= dec
|
173
|
+
self
|
174
|
+
end
|
176
175
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
end
|
176
|
+
def [](idx)
|
177
|
+
if @buffer.is_a?(String)
|
178
|
+
@buffer[idx + @offset].ord
|
179
|
+
else
|
180
|
+
@buffer[idx + @offset]
|
183
181
|
end
|
182
|
+
end
|
184
183
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
end
|
184
|
+
def []=(idx, val)
|
185
|
+
if @buffer.is_a?(String) && val.is_a?(Fixnum)
|
186
|
+
@buffer[idx + @offset] = val.chr
|
187
|
+
else
|
188
|
+
@buffer[idx + @offset] = val
|
191
189
|
end
|
190
|
+
end
|
192
191
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
end
|
192
|
+
def get()
|
193
|
+
if @buffer.is_a?(String)
|
194
|
+
@buffer[@offset].ord
|
195
|
+
else
|
196
|
+
@buffer[@offset]
|
199
197
|
end
|
198
|
+
end
|
200
199
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
end
|
200
|
+
def set(val)
|
201
|
+
if @buffer.is_a?(String) && val.is_a?(Fixnum)
|
202
|
+
@buffer[@offset] = val.chr
|
203
|
+
else
|
204
|
+
@buffer[@offset] = val
|
207
205
|
end
|
206
|
+
end
|
208
207
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
208
|
+
def current
|
209
|
+
@buffer[@offset..-1]
|
210
|
+
end
|
211
|
+
end
|
213
212
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
213
|
+
class Posf < Bytef
|
214
|
+
def +(inc)
|
215
|
+
@offset += inc * 2
|
216
|
+
self
|
217
|
+
end
|
219
218
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
219
|
+
def -(dec)
|
220
|
+
@offset -= dec * 2
|
221
|
+
self
|
222
|
+
end
|
224
223
|
|
225
|
-
|
226
|
-
|
227
|
-
|
224
|
+
def [](idx)
|
225
|
+
@buffer[(idx * 2) + @offset, 2].unpack('S').first
|
226
|
+
end
|
228
227
|
|
229
|
-
|
230
|
-
|
231
|
-
|
228
|
+
def []=(idx, val)
|
229
|
+
@buffer[(idx * 2) + @offset, 2] = [val].pack('S')
|
230
|
+
end
|
232
231
|
|
233
|
-
|
234
|
-
|
235
|
-
|
232
|
+
def get()
|
233
|
+
@buffer[@offset, 2].unpack('S').first
|
234
|
+
end
|
236
235
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
236
|
+
def set(val)
|
237
|
+
@buffer[@offset, 2] = [val].pack('S')
|
238
|
+
end
|
239
|
+
end
|
241
240
|
|
242
|
-
|
243
|
-
|
241
|
+
BASE = 65521
|
242
|
+
NMAX = 5552
|
244
243
|
|
245
|
-
|
244
|
+
module_function
|
246
245
|
|
247
|
-
|
248
|
-
|
249
|
-
|
246
|
+
# Compute the Adler-32 checksum of a data stream
|
247
|
+
def adler32(adler, buf, len=0)
|
248
|
+
return 1 if buf.nil?
|
250
249
|
|
251
|
-
|
252
|
-
|
253
|
-
|
250
|
+
len = buf.length if len.zero?
|
251
|
+
sum2 = (adler >> 16) & 0xFFFF
|
252
|
+
adler &= 0xffff
|
254
253
|
|
255
|
-
|
256
|
-
|
257
|
-
if adler >= BASE
|
258
|
-
adler -= BASE
|
259
|
-
end
|
254
|
+
if len == 1
|
255
|
+
adler += buf[0].ord
|
260
256
|
|
261
|
-
|
257
|
+
if adler >= BASE
|
258
|
+
adler -= BASE
|
259
|
+
end
|
262
260
|
|
263
|
-
|
264
|
-
sum2 -= BASE
|
265
|
-
end
|
261
|
+
sum2 += adler
|
266
262
|
|
267
|
-
|
263
|
+
if sum2 >= BASE
|
264
|
+
sum2 -= BASE
|
268
265
|
end
|
269
266
|
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
while len > 0
|
274
|
-
len -= 1
|
275
|
-
adler += buf[i].ord
|
276
|
-
i += 1
|
277
|
-
sum2 += adler
|
278
|
-
end
|
267
|
+
return adler | (sum2 << 16)
|
268
|
+
end
|
279
269
|
|
280
|
-
|
281
|
-
|
282
|
-
end
|
270
|
+
if len < 16
|
271
|
+
i = 0
|
283
272
|
|
284
|
-
|
273
|
+
while len > 0
|
274
|
+
len -= 1
|
275
|
+
adler += buf[i].ord
|
276
|
+
i += 1
|
277
|
+
sum2 += adler
|
278
|
+
end
|
285
279
|
|
286
|
-
|
280
|
+
if adler >= BASE
|
281
|
+
adler -= BASE
|
287
282
|
end
|
288
283
|
|
289
|
-
|
284
|
+
sum2 %= BASE
|
290
285
|
|
291
|
-
|
292
|
-
|
293
|
-
n = NMAX / 16
|
286
|
+
return adler | (sum2 << 16)
|
287
|
+
end
|
294
288
|
|
295
|
-
|
296
|
-
for j in 0 .. 15
|
297
|
-
adler += buf[i+j].ord
|
298
|
-
sum2 += adler
|
299
|
-
end
|
300
|
-
i += 16
|
301
|
-
n -= 1
|
302
|
-
break if n.zero?
|
303
|
-
end
|
304
|
-
adler %= BASE
|
305
|
-
sum2 %= BASE
|
306
|
-
end
|
289
|
+
i = 0
|
307
290
|
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
291
|
+
while len >= NMAX
|
292
|
+
len -= NMAX
|
293
|
+
n = NMAX / 16
|
294
|
+
|
295
|
+
loop do
|
296
|
+
for j in 0 .. 15
|
297
|
+
adler += buf[i+j].ord
|
298
|
+
sum2 += adler
|
316
299
|
end
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
300
|
+
i += 16
|
301
|
+
n -= 1
|
302
|
+
break if n.zero?
|
303
|
+
end
|
304
|
+
adler %= BASE
|
305
|
+
sum2 %= BASE
|
306
|
+
end
|
307
|
+
|
308
|
+
if len.nonzero?
|
309
|
+
while (len >= 16)
|
310
|
+
len -= 16
|
311
|
+
for j in 0 .. 15
|
312
|
+
adler += buf[i+j].ord
|
321
313
|
sum2 += adler
|
322
314
|
end
|
323
|
-
|
324
|
-
sum2 %= BASE
|
315
|
+
i += 16
|
325
316
|
end
|
317
|
+
while len.nonzero?
|
318
|
+
len -= 1
|
319
|
+
adler += buf[i].ord
|
320
|
+
i += 1
|
321
|
+
sum2 += adler
|
322
|
+
end
|
323
|
+
adler %= BASE
|
324
|
+
sum2 %= BASE
|
325
|
+
end
|
326
326
|
|
327
|
-
|
328
|
-
|
327
|
+
return adler | (sum2 << 16)
|
328
|
+
end
|
329
329
|
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
330
|
+
@@crc_table = [
|
331
|
+
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
|
332
|
+
0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
|
333
|
+
0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
|
334
|
+
0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
|
335
|
+
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
|
336
|
+
0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
337
|
+
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
|
338
|
+
0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
|
339
|
+
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
|
340
|
+
0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
|
341
|
+
0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
|
342
|
+
0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
343
|
+
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
|
344
|
+
0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
|
345
|
+
0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
|
346
|
+
0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
|
347
|
+
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
|
348
|
+
0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
349
|
+
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
|
350
|
+
0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
|
351
|
+
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
|
352
|
+
0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
|
353
|
+
0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
|
354
|
+
0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
355
|
+
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
|
356
|
+
0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
|
357
|
+
0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
|
358
|
+
0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
|
359
|
+
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
|
360
|
+
0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
|
361
|
+
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
|
362
|
+
0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
|
363
|
+
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
|
364
|
+
0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
|
365
|
+
0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
|
366
|
+
0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
367
|
+
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
|
368
|
+
0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
|
369
|
+
0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
|
370
|
+
0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
|
371
|
+
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
|
372
|
+
0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
|
373
|
+
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
|
374
|
+
0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
|
375
|
+
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
|
376
|
+
0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
|
377
|
+
0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
|
378
|
+
0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
379
|
+
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
|
380
|
+
0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
|
381
|
+
0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
|
382
|
+
0x2d02ef8d
|
383
|
+
]
|
384
|
+
|
385
|
+
# Tables of CRC-32s of all single-byte values, made by make_crc_table().
|
386
|
+
#
|
387
|
+
def get_crc_table
|
388
|
+
@@crc_table
|
389
|
+
end
|
388
390
|
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
391
|
+
# Compute the CRC-32 of a data stream.
|
392
|
+
#
|
393
|
+
def crc32(crc, buf, len=0)
|
394
|
+
return 0 if buf.nil?
|
395
|
+
len = buf.length if len.zero?
|
396
|
+
crc = crc ^ 0xffffffff
|
397
|
+
i = 0
|
395
398
|
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
crc = @@crc_table[(crc ^ buf[i].ord) & 0xff] ^ (crc >> 8)
|
404
|
-
i+=1
|
405
|
-
crc = @@crc_table[(crc ^ buf[i].ord) & 0xff] ^ (crc >> 8)
|
406
|
-
i+=1
|
407
|
-
crc = @@crc_table[(crc ^ buf[i].ord) & 0xff] ^ (crc >> 8)
|
408
|
-
i+=1
|
409
|
-
crc = @@crc_table[(crc ^ buf[i].ord) & 0xff] ^ (crc >> 8)
|
410
|
-
i+=1
|
411
|
-
crc = @@crc_table[(crc ^ buf[i].ord) & 0xff] ^ (crc >> 8)
|
412
|
-
i+=1
|
413
|
-
len -= 8
|
414
|
-
end
|
399
|
+
while len >= 8
|
400
|
+
8.times{
|
401
|
+
crc = @@crc_table[(crc ^ buf[i].ord) & 0xff] ^ (crc >> 8)
|
402
|
+
i += 1
|
403
|
+
}
|
404
|
+
len -= 8
|
405
|
+
end
|
415
406
|
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
end
|
407
|
+
if len.nonzero?
|
408
|
+
loop do
|
409
|
+
crc = @@crc_table[(crc ^ buf[i].ord) & 0xff] ^ (crc >> 8)
|
410
|
+
i += 1
|
411
|
+
len -= 1
|
412
|
+
break if len.zero?
|
423
413
|
end
|
424
|
-
|
425
|
-
end
|
414
|
+
end
|
426
415
|
|
427
|
-
|
428
|
-
|
429
|
-
SEEK_SET = 0
|
430
|
-
SEEK_CUR = 1
|
431
|
-
SEEK_END = 2
|
432
|
-
Z_EOF = -1
|
433
|
-
Z_BUFSIZE = 16384
|
434
|
-
|
435
|
-
@@gz_magic = "\x1F\x8B"
|
436
|
-
|
437
|
-
ASCII_FLAG = 0x01
|
438
|
-
HEAD_CRC = 0x02
|
439
|
-
EXTRA_FIELD = 0x04
|
440
|
-
ORIG_NAME = 0x08
|
441
|
-
COMMENT_ = 0x10
|
442
|
-
RESERVED = 0xE0
|
443
|
-
|
444
|
-
Gz_stream = Struct.new(
|
445
|
-
:stream,
|
446
|
-
:z_err,
|
447
|
-
:z_eof,
|
448
|
-
:file,
|
449
|
-
:inbuf,
|
450
|
-
:outbuf,
|
451
|
-
:crc,
|
452
|
-
:msg,
|
453
|
-
:path,
|
454
|
-
:transparent,
|
455
|
-
:mode,
|
456
|
-
:start,
|
457
|
-
:in,
|
458
|
-
:out,
|
459
|
-
:back,
|
460
|
-
:last
|
461
|
-
)
|
462
|
-
|
463
|
-
# Opens a gzip (.gz) file for reading or writing. The mode parameter
|
464
|
-
# is as in fopen ("rb" or "wb"). The file is given either by file descriptor
|
465
|
-
# or path name (if fd == -1).
|
466
|
-
#
|
467
|
-
# gz_open returns NULL if the file could not be opened or if there was
|
468
|
-
# insufficient memory to allocate the (de)compression state; errno
|
469
|
-
# can be checked to distinguish the two cases (if errno is zero, the
|
470
|
-
# zlib error is Z_MEM_ERROR).
|
471
|
-
#
|
472
|
-
def gz_open(path, mode, fd)
|
473
|
-
return nil if path.nil? || mode.nil?
|
416
|
+
crc ^ 0xffffffff
|
417
|
+
end
|
474
418
|
|
475
|
-
|
476
|
-
|
419
|
+
OS_CODE = 0
|
420
|
+
|
421
|
+
SEEK_SET = 0
|
422
|
+
SEEK_CUR = 1
|
423
|
+
SEEK_END = 2
|
424
|
+
Z_EOF = -1
|
425
|
+
Z_BUFSIZE = 16384
|
426
|
+
|
427
|
+
@@gz_magic = "\x1F\x8B"
|
428
|
+
|
429
|
+
ASCII_FLAG = 0x01
|
430
|
+
HEAD_CRC = 0x02
|
431
|
+
EXTRA_FIELD = 0x04
|
432
|
+
ORIG_NAME = 0x08
|
433
|
+
COMMENT_ = 0x10
|
434
|
+
RESERVED = 0xE0
|
435
|
+
|
436
|
+
Gz_stream = Struct.new(
|
437
|
+
:stream,
|
438
|
+
:z_err,
|
439
|
+
:z_eof,
|
440
|
+
:file,
|
441
|
+
:inbuf,
|
442
|
+
:outbuf,
|
443
|
+
:crc,
|
444
|
+
:msg,
|
445
|
+
:path,
|
446
|
+
:transparent,
|
447
|
+
:mode,
|
448
|
+
:start,
|
449
|
+
:in,
|
450
|
+
:out,
|
451
|
+
:back,
|
452
|
+
:last
|
453
|
+
)
|
454
|
+
|
455
|
+
# Opens a gzip (.gz) file for reading or writing. The mode parameter
|
456
|
+
# is as in fopen ("rb" or "wb"). The file is given either by file descriptor
|
457
|
+
# or path name (if fd == -1).
|
458
|
+
#
|
459
|
+
# gz_open returns NULL if the file could not be opened or if there was
|
460
|
+
# insufficient memory to allocate the (de)compression state; errno
|
461
|
+
# can be checked to distinguish the two cases (if errno is zero, the
|
462
|
+
# zlib error is Z_MEM_ERROR).
|
463
|
+
#
|
464
|
+
def gz_open(path, mode, fd)
|
465
|
+
return nil if path.nil? || mode.nil?
|
477
466
|
|
478
|
-
|
479
|
-
|
467
|
+
s = Gz_stream.new
|
468
|
+
s.stream = Z_stream.new
|
480
469
|
|
481
|
-
|
482
|
-
|
483
|
-
s.stream.avail_in = 0
|
484
|
-
s.stream.avail_out = 0
|
485
|
-
s.stream.msg = ''
|
470
|
+
level = Z_DEFAULT_COMPRESSION
|
471
|
+
strategy = Z_DEFAULT_STRATEGY
|
486
472
|
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
s.in = 0
|
493
|
-
s.out = 0
|
494
|
-
s.back = Z_EOF
|
495
|
-
s.crc = crc32(0, nil)
|
496
|
-
s.msg = ''
|
497
|
-
s.transparent = false
|
498
|
-
s.path = path.dup
|
499
|
-
s.mode = nil
|
500
|
-
|
501
|
-
fmode = ''
|
502
|
-
|
503
|
-
mode.each_byte do |c|
|
504
|
-
s.mode = 'r' if c == ?r.ord
|
505
|
-
s.mode = 'w' if c == ?w.ord || c == ?a.ord
|
506
|
-
|
507
|
-
if c >= ?0.ord && c <= ?9.ord
|
508
|
-
level = c - ?0.ord
|
509
|
-
elsif c == ?f.ord
|
510
|
-
strategy = Z_FILTERED
|
511
|
-
elsif c == ?h.ord
|
512
|
-
strategy = Z_HUFFMAN_ONLY
|
513
|
-
elsif c == ?R.ord
|
514
|
-
strategy = Z_RLE
|
515
|
-
else
|
516
|
-
fmode += c.chr
|
517
|
-
end
|
518
|
-
end
|
473
|
+
s.stream.next_in = nil
|
474
|
+
s.stream.next_out = nil
|
475
|
+
s.stream.avail_in = 0
|
476
|
+
s.stream.avail_out = 0
|
477
|
+
s.stream.msg = ''
|
519
478
|
|
520
|
-
|
521
|
-
|
522
|
-
|
479
|
+
s.file = nil
|
480
|
+
s.z_err = Z_OK
|
481
|
+
s.z_eof = false
|
482
|
+
s.inbuf = nil
|
483
|
+
s.outbuf = nil
|
484
|
+
s.in = 0
|
485
|
+
s.out = 0
|
486
|
+
s.back = Z_EOF
|
487
|
+
s.crc = crc32(0, nil)
|
488
|
+
s.msg = ''
|
489
|
+
s.transparent = false
|
490
|
+
s.path = path.dup
|
491
|
+
s.mode = nil
|
492
|
+
|
493
|
+
fmode = ''
|
494
|
+
|
495
|
+
mode.each_byte do |c|
|
496
|
+
s.mode = 'r' if c == ?r.ord
|
497
|
+
s.mode = 'w' if c == ?w.ord || c == ?a.ord
|
498
|
+
|
499
|
+
if c >= ?0.ord && c <= ?9.ord
|
500
|
+
level = c - ?0.ord
|
501
|
+
elsif c == ?f.ord
|
502
|
+
strategy = Z_FILTERED
|
503
|
+
elsif c == ?h.ord
|
504
|
+
strategy = Z_HUFFMAN_ONLY
|
505
|
+
elsif c == ?R.ord
|
506
|
+
strategy = Z_RLE
|
507
|
+
else
|
508
|
+
fmode += c.chr
|
523
509
|
end
|
510
|
+
end
|
524
511
|
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
Z_DEFLATED,
|
530
|
-
-MAX_WBITS,
|
531
|
-
DEF_MEM_LEVEL,
|
532
|
-
strategy
|
533
|
-
)
|
534
|
-
|
535
|
-
s.outbuf = 0.chr * Z_BUFSIZE
|
536
|
-
s.stream.next_out = Bytef.new(s.outbuf)
|
537
|
-
|
538
|
-
if err != Z_OK || s.outbuf.nil?
|
539
|
-
destroy(s)
|
540
|
-
return nil
|
541
|
-
end
|
542
|
-
else
|
543
|
-
s.inbuf = 0.chr * Z_BUFSIZE
|
544
|
-
s.stream.next_in = Bytef.new(s.inbuf)
|
512
|
+
if s.mode.nil?
|
513
|
+
destroy(s)
|
514
|
+
return nil
|
515
|
+
end
|
545
516
|
|
546
|
-
|
517
|
+
if s.mode == 'w'
|
518
|
+
err = deflateInit2(
|
519
|
+
s.stream,
|
520
|
+
level,
|
521
|
+
Z_DEFLATED,
|
522
|
+
-MAX_WBITS,
|
523
|
+
DEF_MEM_LEVEL,
|
524
|
+
strategy
|
525
|
+
)
|
547
526
|
|
548
|
-
|
549
|
-
|
550
|
-
return nil
|
551
|
-
end
|
552
|
-
end
|
527
|
+
s.outbuf = 0.chr * Z_BUFSIZE
|
528
|
+
s.stream.next_out = Bytef.new(s.outbuf)
|
553
529
|
|
554
|
-
s.
|
530
|
+
if err != Z_OK || s.outbuf.nil?
|
531
|
+
destroy(s)
|
532
|
+
return nil
|
533
|
+
end
|
534
|
+
else
|
535
|
+
s.inbuf = 0.chr * Z_BUFSIZE
|
536
|
+
s.stream.next_in = Bytef.new(s.inbuf)
|
555
537
|
|
556
|
-
|
557
|
-
s.file = fd < 0 ? File.new(path, fmode) : IO.new(fd, fmode)
|
538
|
+
err = inflateInit2_(s.stream, -MAX_WBITS, ZLIB_VERSION, s.stream.size)
|
558
539
|
|
559
|
-
if s.
|
560
|
-
|
561
|
-
|
562
|
-
gzheader[1] = @@gz_magic[1]
|
563
|
-
gzheader[2] = Z_DEFLATED.chr
|
564
|
-
gzheader[3] = 0.chr
|
565
|
-
gzheader[4] = 0.chr
|
566
|
-
gzheader[5] = 0.chr
|
567
|
-
gzheader[6] = 0.chr
|
568
|
-
gzheader[7] = 0.chr
|
569
|
-
gzheader[8] = 0.chr
|
570
|
-
gzheader[9] = OS_CODE.chr
|
571
|
-
s.file.write(gzheader)
|
572
|
-
s.start = 10
|
573
|
-
else
|
574
|
-
check_header(s)
|
575
|
-
s.start = s.file.pos - s.stream.avail_in
|
540
|
+
if err != Z_OK || s.inbuf.nil?
|
541
|
+
destroy(s)
|
542
|
+
return nil
|
576
543
|
end
|
544
|
+
end
|
577
545
|
|
578
|
-
|
579
|
-
|
546
|
+
s.stream.avail_out = Z_BUFSIZE
|
547
|
+
|
548
|
+
s.file = fd < 0 ? File.new(path, fmode) : IO.new(fd, fmode)
|
549
|
+
|
550
|
+
if s.mode == 'w'
|
551
|
+
gzheader = 0.chr * 10
|
552
|
+
gzheader[0] = @@gz_magic[0]
|
553
|
+
gzheader[1] = @@gz_magic[1]
|
554
|
+
gzheader[2] = Z_DEFLATED.chr
|
555
|
+
gzheader[3] = 0.chr
|
556
|
+
gzheader[4] = 0.chr
|
557
|
+
gzheader[5] = 0.chr
|
558
|
+
gzheader[6] = 0.chr
|
559
|
+
gzheader[7] = 0.chr
|
560
|
+
gzheader[8] = 0.chr
|
561
|
+
gzheader[9] = OS_CODE.chr
|
562
|
+
s.file.write(gzheader)
|
563
|
+
s.start = 10
|
564
|
+
else
|
565
|
+
check_header(s)
|
566
|
+
s.start = s.file.pos - s.stream.avail_in
|
567
|
+
end
|
580
568
|
|
581
|
-
|
582
|
-
|
583
|
-
def gzopen(path,mode)
|
584
|
-
return gz_open(path, mode, -1)
|
585
|
-
end
|
569
|
+
return s
|
570
|
+
end
|
586
571
|
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
name = "<fd:#{fd}"
|
593
|
-
return gz_open(name, mode, fd)
|
594
|
-
end
|
572
|
+
# Opens a gzip (.gz) file for reading or writing.
|
573
|
+
#
|
574
|
+
def gzopen(path,mode)
|
575
|
+
return gz_open(path, mode, -1)
|
576
|
+
end
|
595
577
|
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
578
|
+
# Associate a gzFile with the file descriptor fd. fd is not dup'ed here
|
579
|
+
# to mimic the behavio(u)r of fdopen.
|
580
|
+
#
|
581
|
+
def gzdopen(fd,mode)
|
582
|
+
return nil if fd < 0
|
583
|
+
name = "<fd:#{fd}"
|
584
|
+
return gz_open(name, mode, fd)
|
585
|
+
end
|
600
586
|
|
601
|
-
|
602
|
-
|
603
|
-
|
587
|
+
# Update the compression level and strategy
|
588
|
+
#
|
589
|
+
def gzsetparams(file,level,strategy)
|
590
|
+
s = file
|
604
591
|
|
605
|
-
|
606
|
-
|
607
|
-
|
592
|
+
if s.nil? || s.mode != 'w'
|
593
|
+
return Z_STREAM_ERROR
|
594
|
+
end
|
608
595
|
|
609
|
-
|
610
|
-
|
611
|
-
|
596
|
+
if s.stream.avail_out.zero?
|
597
|
+
s.stream.next_out = Bytef.new(s.outbuf)
|
598
|
+
written = s.file.write(s.outbuf)
|
612
599
|
|
613
|
-
|
600
|
+
if written != Z_BUFSIZE
|
601
|
+
s.z_err = Z_ERRNO
|
614
602
|
end
|
615
603
|
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
# Read a byte from a gz_stream; update next_in and avail_in. Return EOF
|
620
|
-
# for end of file.
|
621
|
-
# IN assertion: the stream s has been sucessfully opened for reading.
|
622
|
-
#
|
623
|
-
def get_byte(s)
|
624
|
-
return Z_EOF if s.z_eof
|
625
|
-
|
626
|
-
if s.stream.avail_in.zero?
|
627
|
-
begin
|
628
|
-
s.inbuf = s.file.read(Z_BUFSIZE)
|
629
|
-
s.stream.avail_in = s.inbuf.length if s.inbuf
|
630
|
-
rescue
|
631
|
-
s.inbuf = nil
|
632
|
-
s.z_err = Z_ERRNO
|
633
|
-
end
|
604
|
+
s.stream.avail_out = Z_BUFSIZE
|
605
|
+
end
|
634
606
|
|
635
|
-
|
636
|
-
|
637
|
-
return Z_EOF
|
638
|
-
end
|
639
|
-
s.stream.next_in = Bytef.new(s.inbuf)
|
640
|
-
end
|
607
|
+
return deflateParams(s.stream, level, strategy)
|
608
|
+
end
|
641
609
|
|
642
|
-
|
643
|
-
|
644
|
-
|
610
|
+
# Read a byte from a gz_stream; update next_in and avail_in. Return EOF
|
611
|
+
# for end of file.
|
612
|
+
# IN assertion: the stream s has been sucessfully opened for reading.
|
613
|
+
#
|
614
|
+
def get_byte(s)
|
615
|
+
return Z_EOF if s.z_eof
|
616
|
+
|
617
|
+
if s.stream.avail_in.zero?
|
618
|
+
begin
|
619
|
+
s.inbuf = s.file.read(Z_BUFSIZE)
|
620
|
+
s.stream.avail_in = s.inbuf.length if s.inbuf
|
621
|
+
rescue
|
622
|
+
s.inbuf = nil
|
623
|
+
s.z_err = Z_ERRNO
|
624
|
+
end
|
645
625
|
|
646
|
-
|
647
|
-
|
626
|
+
if s.inbuf.nil?
|
627
|
+
s.z_eof = true
|
628
|
+
return Z_EOF
|
629
|
+
end
|
630
|
+
s.stream.next_in = Bytef.new(s.inbuf)
|
631
|
+
end
|
648
632
|
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
x = 0.chr * 4
|
653
|
-
x[0] = (get_byte(s)).chr
|
654
|
-
x[1] = (get_byte(s)).chr
|
655
|
-
x[2] = (get_byte(s)).chr
|
656
|
-
c = get_byte(s)
|
657
|
-
x[3] = (c).chr
|
633
|
+
s.stream.avail_in-=1
|
634
|
+
_get_byte = s.stream.next_in.get
|
635
|
+
s.stream.next_in+=1
|
658
636
|
|
659
|
-
|
637
|
+
return _get_byte
|
638
|
+
end
|
660
639
|
|
661
|
-
|
662
|
-
|
640
|
+
# Reads a long in LSB order from the given gz_stream. Sets z_err in case
|
641
|
+
# of error.
|
642
|
+
def getLong(s)
|
643
|
+
x = 0.chr * 4
|
644
|
+
x[0] = (get_byte(s)).chr
|
645
|
+
x[1] = (get_byte(s)).chr
|
646
|
+
x[2] = (get_byte(s)).chr
|
647
|
+
c = get_byte(s)
|
648
|
+
x[3] = (c).chr
|
663
649
|
|
664
|
-
|
665
|
-
# mode to transparent if the gzip magic header is not present; set s->err
|
666
|
-
# to Z_DATA_ERROR if the magic header is present but the rest of the header
|
667
|
-
# is incorrect.
|
668
|
-
#
|
669
|
-
# IN assertion: the stream s has already been created sucessfully;
|
670
|
-
# s->stream.avail_in is zero for the first time, but may be non-zero
|
671
|
-
# for concatenated .gz files.
|
672
|
-
#
|
673
|
-
def check_header(s)
|
674
|
-
len = s.stream.avail_in
|
650
|
+
s.z_err = Z_DATA_ERROR if (c == Z_EOF)
|
675
651
|
|
676
|
-
|
677
|
-
|
678
|
-
s.inbuf[0] = s.stream.next_in[0]
|
679
|
-
end
|
652
|
+
return (x.unpack('L').first)
|
653
|
+
end
|
680
654
|
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
end
|
655
|
+
# Check the gzip header of a gz_stream opened for reading. Set the stream
|
656
|
+
# mode to transparent if the gzip magic header is not present; set s->err
|
657
|
+
# to Z_DATA_ERROR if the magic header is present but the rest of the header
|
658
|
+
# is incorrect.
|
659
|
+
#
|
660
|
+
# IN assertion: the stream s has already been created sucessfully;
|
661
|
+
# s->stream.avail_in is zero for the first time, but may be non-zero
|
662
|
+
# for concatenated .gz files.
|
663
|
+
#
|
664
|
+
def check_header(s)
|
665
|
+
len = s.stream.avail_in
|
693
666
|
|
694
|
-
|
695
|
-
|
667
|
+
if len < 2
|
668
|
+
if len.nonzero?
|
669
|
+
s.inbuf[0] = s.stream.next_in[0]
|
670
|
+
end
|
696
671
|
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
672
|
+
begin
|
673
|
+
buf = s.file.read(Z_BUFSIZE >> len)
|
674
|
+
if buf
|
675
|
+
s.inbuf[len,buf.length] = buf
|
676
|
+
len = buf.length
|
677
|
+
else
|
678
|
+
len = 0
|
679
|
+
end
|
680
|
+
rescue
|
681
|
+
len = 0
|
682
|
+
s.z_err = Z_ERRNO
|
701
683
|
end
|
702
684
|
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
685
|
+
s.stream.avail_in += len
|
686
|
+
s.stream.next_in = Bytef.new(s.inbuf)
|
687
|
+
|
688
|
+
if s.stream.avail_in < 2
|
689
|
+
s.transparent = !s.stream.avail_in.zero?
|
690
|
+
return
|
708
691
|
end
|
692
|
+
end
|
709
693
|
|
710
|
-
|
711
|
-
s.stream.next_in
|
694
|
+
if s.stream.next_in[0] != @@gz_magic[0].ord ||
|
695
|
+
s.stream.next_in[1] != @@gz_magic[1].ord
|
696
|
+
then
|
697
|
+
s.transparent = true
|
698
|
+
return
|
699
|
+
end
|
712
700
|
|
713
|
-
|
714
|
-
|
701
|
+
s.stream.avail_in -= 2
|
702
|
+
s.stream.next_in += 2
|
715
703
|
|
716
|
-
|
717
|
-
|
718
|
-
return
|
719
|
-
end
|
704
|
+
method = get_byte(s)
|
705
|
+
flags = get_byte(s)
|
720
706
|
|
721
|
-
|
722
|
-
|
723
|
-
|
707
|
+
if (method != Z_DEFLATED) || (flags & RESERVED).nonzero?
|
708
|
+
s.z_err = Z_DATA_ERROR
|
709
|
+
return
|
710
|
+
end
|
724
711
|
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
while len.nonzero? || (get_byte(s) != Z_EOF)
|
729
|
-
len -= 1
|
730
|
-
end
|
731
|
-
end
|
712
|
+
for len in 0 .. 5
|
713
|
+
get_byte(s)
|
714
|
+
end
|
732
715
|
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
716
|
+
if (flags & EXTRA_FIELD).nonzero?
|
717
|
+
len = (get_byte(s))
|
718
|
+
len += ((get_byte(s)) << 8)
|
719
|
+
while len.nonzero? || (get_byte(s) != Z_EOF)
|
720
|
+
len -= 1
|
738
721
|
end
|
722
|
+
end
|
739
723
|
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
end
|
724
|
+
if (flags & ORIG_NAME).nonzero?
|
725
|
+
loop do
|
726
|
+
c = get_byte(s)
|
727
|
+
break if c.zero? || (c == Z_EOF)
|
745
728
|
end
|
729
|
+
end
|
746
730
|
|
747
|
-
|
748
|
-
|
749
|
-
|
731
|
+
if (flags & COMMENT_).nonzero?
|
732
|
+
loop do
|
733
|
+
c = get_byte(s)
|
734
|
+
break if c.zero? || (c == Z_EOF)
|
750
735
|
end
|
736
|
+
end
|
751
737
|
|
752
|
-
|
753
|
-
|
738
|
+
if (flags & HEAD_CRC).nonzero?
|
739
|
+
get_byte(s)
|
740
|
+
get_byte(s)
|
741
|
+
end
|
754
742
|
|
755
|
-
|
756
|
-
|
757
|
-
err = Z_OK
|
758
|
-
return Z_STREAM_ERROR if s.nil?
|
743
|
+
s.z_err = s.z_eof ? Z_DATA_ERROR : Z_OK
|
744
|
+
end
|
759
745
|
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
err = inflateEnd(s.stream)
|
765
|
-
end
|
766
|
-
end
|
746
|
+
# Cleanup then free the given gz_stream. Return a zlib error code.
|
747
|
+
def destroy(s)
|
748
|
+
err = Z_OK
|
749
|
+
return Z_STREAM_ERROR if s.nil?
|
767
750
|
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
751
|
+
if s.stream.state
|
752
|
+
if s.mode == 'w'
|
753
|
+
err = deflateEnd(s.stream)
|
754
|
+
elsif s.mode == 'r'
|
755
|
+
err = inflateEnd(s.stream)
|
773
756
|
end
|
757
|
+
end
|
774
758
|
|
775
|
-
|
776
|
-
|
777
|
-
|
759
|
+
begin
|
760
|
+
s.file.close if s.file
|
761
|
+
s.file = nil
|
762
|
+
rescue
|
763
|
+
err = Z_ERRNO
|
764
|
+
end
|
778
765
|
|
779
|
-
|
780
|
-
|
766
|
+
if s.z_err < 0
|
767
|
+
err = s.z_err
|
768
|
+
end
|
781
769
|
|
782
|
-
|
783
|
-
|
784
|
-
def gzread(file,buf,len)
|
785
|
-
s = file
|
786
|
-
start = Bytef.new(buf)
|
770
|
+
return err
|
771
|
+
end
|
787
772
|
|
788
|
-
|
789
|
-
|
790
|
-
|
773
|
+
# Reads the given number of uncompressed bytes from the compressed file.
|
774
|
+
# gzread returns the number of bytes actually read (0 for end of file).
|
775
|
+
def gzread(file,buf,len)
|
776
|
+
s = file
|
777
|
+
start = Bytef.new(buf)
|
791
778
|
|
792
|
-
|
793
|
-
return
|
794
|
-
|
795
|
-
next_out = Bytef.new(buf)
|
796
|
-
s.stream.next_out = Bytef.new(buf)
|
797
|
-
s.stream.avail_out = len
|
798
|
-
|
799
|
-
if s.stream.avail_out.nonzero? && s.back != Z_EOF
|
800
|
-
next_out.set(s.back)
|
801
|
-
next_out += 1
|
802
|
-
s.stream.next_out += 1
|
803
|
-
s.stream.avail_out -= 1
|
804
|
-
s.back = Z_EOF
|
805
|
-
s.out += 1
|
806
|
-
start += 1
|
807
|
-
|
808
|
-
if s.last
|
809
|
-
s.z_err = Z_STREAM_END
|
810
|
-
return 1
|
811
|
-
end
|
812
|
-
end
|
779
|
+
if s.nil? || s.mode != 'r'
|
780
|
+
return Z_STREAM_ERROR
|
781
|
+
end
|
813
782
|
|
814
|
-
|
815
|
-
|
816
|
-
n = s.stream.avail_in
|
783
|
+
return -1 if (s.z_err == Z_DATA_ERROR) || (s.z_err == Z_ERRNO)
|
784
|
+
return 0 if (s.z_err == Z_STREAM_END)
|
817
785
|
|
818
|
-
|
819
|
-
|
820
|
-
|
786
|
+
next_out = Bytef.new(buf)
|
787
|
+
s.stream.next_out = Bytef.new(buf)
|
788
|
+
s.stream.avail_out = len
|
821
789
|
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
790
|
+
if s.stream.avail_out.nonzero? && s.back != Z_EOF
|
791
|
+
next_out.set(s.back)
|
792
|
+
next_out += 1
|
793
|
+
s.stream.next_out += 1
|
794
|
+
s.stream.avail_out -= 1
|
795
|
+
s.back = Z_EOF
|
796
|
+
s.out += 1
|
797
|
+
start += 1
|
830
798
|
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
end
|
837
|
-
end
|
799
|
+
if s.last
|
800
|
+
s.z_err = Z_STREAM_END
|
801
|
+
return 1
|
802
|
+
end
|
803
|
+
end
|
838
804
|
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
if len.zero?
|
843
|
-
s.z_eof = true
|
844
|
-
end
|
845
|
-
return len
|
846
|
-
end
|
805
|
+
while s.stream.avail_out.nonzero?
|
806
|
+
if s.transparent
|
807
|
+
n = s.stream.avail_in
|
847
808
|
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
if buf
|
852
|
-
s.inbuf[0,buf.length] = buf
|
853
|
-
s.stream.avail_in = buf.length
|
854
|
-
else
|
855
|
-
s.stream.avail_in = 0
|
856
|
-
end
|
857
|
-
rescue
|
858
|
-
s.z_err = Z_ERRNO
|
859
|
-
end
|
809
|
+
if n > s.stream.avail_out
|
810
|
+
n = s.stream.avail_out
|
811
|
+
end
|
860
812
|
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
813
|
+
if n > 0
|
814
|
+
s.stream.next_out.buffer[s.stream.next_out.offset,n] = s.stream.next_in.current[0,n]
|
815
|
+
next_out += n
|
816
|
+
s.stream.next_out.offset = next_out.offset
|
817
|
+
s.stream.next_in += n
|
818
|
+
s.stream.avail_out -= n
|
819
|
+
s.stream.avail_in -= n
|
820
|
+
end
|
867
821
|
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
inflateReset(s.stream)
|
884
|
-
s.crc = crc32(0, nil)
|
885
|
-
end
|
886
|
-
end
|
887
|
-
end
|
822
|
+
if s.stream.avail_out > 0
|
823
|
+
buff = s.file.read(s.stream.avail_out)
|
824
|
+
if buff
|
825
|
+
next_out.buffer[next_out.offset,buff.length] = buff
|
826
|
+
s.stream.avail_out -= buff.length
|
827
|
+
end
|
828
|
+
end
|
829
|
+
|
830
|
+
len -= s.stream.avail_out
|
831
|
+
s.in += len
|
832
|
+
s.out += len
|
833
|
+
|
834
|
+
if len.zero?
|
835
|
+
s.z_eof = true
|
836
|
+
end
|
888
837
|
|
889
|
-
|
838
|
+
return len
|
890
839
|
end
|
891
840
|
|
892
|
-
|
841
|
+
if s.stream.avail_in.zero? && !s.z_eof
|
842
|
+
begin
|
843
|
+
buf = s.file.read(Z_BUFSIZE)
|
844
|
+
if buf
|
845
|
+
s.inbuf[0,buf.length] = buf
|
846
|
+
s.stream.avail_in = buf.length
|
847
|
+
else
|
848
|
+
s.stream.avail_in = 0
|
849
|
+
end
|
850
|
+
rescue
|
851
|
+
s.z_err = Z_ERRNO
|
852
|
+
end
|
893
853
|
|
894
|
-
|
895
|
-
|
854
|
+
if s.stream.avail_in.zero?
|
855
|
+
s.z_eof = true
|
856
|
+
break if(s.z_err == Z_ERRNO)
|
857
|
+
end
|
858
|
+
s.stream.next_in = Bytef.new(s.inbuf)
|
896
859
|
end
|
897
860
|
|
898
|
-
|
899
|
-
|
861
|
+
s.in += s.stream.avail_in
|
862
|
+
s.out += s.stream.avail_out
|
863
|
+
s.z_err = inflate(s.stream, Z_NO_FLUSH)
|
864
|
+
s.in -= s.stream.avail_in
|
865
|
+
s.out -= s.stream.avail_out
|
900
866
|
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
867
|
+
if s.z_err == Z_STREAM_END
|
868
|
+
s.crc = crc32(s.crc, start.current,s.stream.next_out.offset - start.offset)
|
869
|
+
start = s.stream.next_out.dup
|
870
|
+
if getLong(s) != s.crc
|
871
|
+
s.z_err = Z_DATA_ERROR
|
872
|
+
else
|
873
|
+
getLong(s)
|
874
|
+
check_header(s)
|
875
|
+
if s.z_err == Z_OK
|
876
|
+
inflateReset(s.stream)
|
877
|
+
s.crc = crc32(0, nil)
|
878
|
+
end
|
879
|
+
end
|
910
880
|
end
|
911
|
-
end
|
912
881
|
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
882
|
+
break if s.z_err != Z_OK || s.z_eof
|
883
|
+
end
|
884
|
+
|
885
|
+
s.crc = crc32(s.crc, start.current,s.stream.next_out.offset - start.offset)
|
886
|
+
|
887
|
+
if len == s.stream.avail_out && (s.z_err == Z_DATA_ERROR || s.z_err = Z_ERRNO)
|
888
|
+
return -1
|
889
|
+
end
|
890
|
+
|
891
|
+
return len - s.stream.avail_out
|
892
|
+
end
|
893
|
+
|
894
|
+
# Reads one byte from the compressed file. gzgetc returns this byte
|
895
|
+
# or -1 in case of end of file or error.
|
896
|
+
#
|
897
|
+
def gzgetc(file)
|
898
|
+
c = 0.chr
|
899
|
+
if (gzread(file,c,1) == 1)
|
923
900
|
return c
|
924
|
-
|
901
|
+
else
|
902
|
+
return -1
|
903
|
+
end
|
904
|
+
end
|
925
905
|
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
906
|
+
# Push one byte back onto the stream.
|
907
|
+
#
|
908
|
+
def gzungetc(c, file)
|
909
|
+
s = file
|
910
|
+
return Z_EOF if s.nil? || s.mode != 'r' || c == Z_EOF || s.back != Z_EOF
|
911
|
+
s.back = c
|
912
|
+
s.out -= 1
|
913
|
+
s.last = (s.z_err == Z_STREAM_END)
|
914
|
+
s.z_err = Z_OK if s.last
|
915
|
+
s.z_eof = false
|
916
|
+
return c
|
917
|
+
end
|
935
918
|
|
936
|
-
|
937
|
-
|
919
|
+
# Reads bytes from the compressed file until len-1 characters are
|
920
|
+
# read, or a newline character is read and transferred to buf, or an
|
921
|
+
# end-of-file condition is encountered. The string is then terminated
|
922
|
+
# with a null character.
|
923
|
+
#
|
924
|
+
# Returns buf, or Z_NULL in case of error.
|
925
|
+
#
|
926
|
+
def gzgets(file,buf,len)
|
927
|
+
return nil if buf.nil? || (len <= 0)
|
938
928
|
|
939
|
-
|
940
|
-
|
941
|
-
bytes = gzread(file, gzchar, 1)
|
942
|
-
buf[i] = gzchar[0]
|
943
|
-
i += 1
|
944
|
-
break if len.zero? || (bytes != 1) || (gzchar == (13).chr)
|
945
|
-
end
|
929
|
+
i = 0
|
930
|
+
gzchar = 0.chr
|
946
931
|
|
947
|
-
|
948
|
-
|
932
|
+
loop do
|
933
|
+
len-=1
|
934
|
+
bytes = gzread(file, gzchar, 1)
|
935
|
+
buf[i] = gzchar[0]
|
936
|
+
i += 1
|
937
|
+
break if len.zero? || (bytes != 1) || (gzchar == (13).chr)
|
938
|
+
end
|
949
939
|
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
940
|
+
buf[i..-1] = ''
|
941
|
+
buf.chomp!(0.chr)
|
942
|
+
|
943
|
+
if i.zero? && (len > 0)
|
944
|
+
return nil
|
945
|
+
else
|
946
|
+
return buf
|
947
|
+
end
|
955
948
|
end
|
956
949
|
|
957
950
|
# Writes the given number of uncompressed bytes into the compressed file.
|
@@ -1204,11 +1197,11 @@ module Rbzlib
|
|
1204
1197
|
|
1205
1198
|
# Outputs a long in LSB order to the given file
|
1206
1199
|
def putLong(s,x)
|
1207
|
-
|
1200
|
+
4.times{
|
1208
1201
|
c = x & 0xFF
|
1209
1202
|
s.putc(c)
|
1210
1203
|
x = x >> 8
|
1211
|
-
|
1204
|
+
}
|
1212
1205
|
end
|
1213
1206
|
|
1214
1207
|
# Flushes all pending output if necessary, closes the compressed file
|
@@ -2455,7 +2448,7 @@ module Rbzlib
|
|
2455
2448
|
end
|
2456
2449
|
end
|
2457
2450
|
if (s.match_available)
|
2458
|
-
|
2451
|
+
_tr_tally(s, 0, s.window[s.strstart-1].ord)
|
2459
2452
|
s.match_available = false
|
2460
2453
|
end
|
2461
2454
|
FLUSH_BLOCK_ONLY(s, flush == Z_FINISH)
|
@@ -2871,15 +2864,15 @@ module Rbzlib
|
|
2871
2864
|
end
|
2872
2865
|
|
2873
2866
|
h = HEAP_SIZE
|
2874
|
-
max_length.downto(1) do |
|
2875
|
-
n = s.bl_count[
|
2867
|
+
max_length.downto(1) do |nbits|
|
2868
|
+
n = s.bl_count[nbits]
|
2876
2869
|
while n.nonzero?
|
2877
2870
|
h -= 1
|
2878
2871
|
m = s.heap[h]
|
2879
2872
|
next if (m > max_code)
|
2880
|
-
if (tree[m].dl !=
|
2881
|
-
s.opt_len += (
|
2882
|
-
tree[m].dl =
|
2873
|
+
if (tree[m].dl != nbits)
|
2874
|
+
s.opt_len += (nbits - tree[m].dl) * tree[m].fc
|
2875
|
+
tree[m].dl = nbits
|
2883
2876
|
end
|
2884
2877
|
n-=1
|
2885
2878
|
end
|
@@ -2931,8 +2924,8 @@ module Rbzlib
|
|
2931
2924
|
end
|
2932
2925
|
desc.max_code = max_code
|
2933
2926
|
|
2934
|
-
(s.heap_len / 2).downto(1) do |
|
2935
|
-
pqdownheap(s, tree,
|
2927
|
+
(s.heap_len / 2).downto(1) do |nheap|
|
2928
|
+
pqdownheap(s, tree, nheap)
|
2936
2929
|
end
|
2937
2930
|
|
2938
2931
|
node = elems
|
@@ -3619,10 +3612,10 @@ module Rbzlib
|
|
3619
3612
|
dmask = (1 << state.distbits) - 1
|
3620
3613
|
status = nil
|
3621
3614
|
this = nil
|
3622
|
-
|
3615
|
+
|
3623
3616
|
op = -1
|
3624
3617
|
len = -1
|
3625
|
-
|
3618
|
+
|
3626
3619
|
loop do
|
3627
3620
|
if status.nil?
|
3628
3621
|
if (bits < 15)
|
@@ -3808,20 +3801,20 @@ module Rbzlib
|
|
3808
3801
|
state.mode = BAD
|
3809
3802
|
break
|
3810
3803
|
end
|
3811
|
-
|
3812
|
-
|
3813
|
-
|
3814
|
-
|
3815
|
-
|
3816
|
-
|
3817
|
-
|
3818
|
-
|
3819
|
-
|
3820
|
-
|
3821
|
-
|
3822
|
-
|
3823
|
-
|
3824
|
-
|
3804
|
+
elsif (op & 64).zero?
|
3805
|
+
this = lcode[this.val + (hold & ((1 << op) - 1))]
|
3806
|
+
status = :dolen
|
3807
|
+
redo
|
3808
|
+
elsif (op & 32).nonzero?
|
3809
|
+
state.mode = TYPE
|
3810
|
+
break
|
3811
|
+
else
|
3812
|
+
strm.msg = "invalid literal/length code"
|
3813
|
+
state.mode = BAD
|
3814
|
+
break
|
3815
|
+
end
|
3816
|
+
status = nil
|
3817
|
+
break unless (_in.offset < last.offset && out.offset < _end.offset)
|
3825
3818
|
end
|
3826
3819
|
len = bits >> 3
|
3827
3820
|
_in -= len
|
@@ -4159,12 +4152,12 @@ module Rbzlib
|
|
4159
4152
|
# Get a byte of input into the bit accumulator, or return from inflate()
|
4160
4153
|
# if there is no input available.
|
4161
4154
|
def PULLBYTE()
|
4162
|
-
|
4163
|
-
|
4164
|
-
|
4165
|
-
|
4166
|
-
|
4167
|
-
|
4155
|
+
throw :inf_leave if @@have.zero?
|
4156
|
+
@@have -= 1
|
4157
|
+
@@next.get
|
4158
|
+
@@hold += (@@next.get) << @@bits
|
4159
|
+
@@next += 1
|
4160
|
+
@@bits += 8
|
4168
4161
|
end
|
4169
4162
|
|
4170
4163
|
# Assure that there are at least n bits in the bit accumulator. If there is
|
@@ -4959,7 +4952,7 @@ module Rbzlib
|
|
4959
4952
|
state.bits -= 8
|
4960
4953
|
end
|
4961
4954
|
state.have = 0
|
4962
|
-
|
4955
|
+
_,state.have = syncsearch((state.have), buf, len)
|
4963
4956
|
end
|
4964
4957
|
|
4965
4958
|
len,state.have = syncsearch((state.have), strm.next_in, strm.avail_in)
|