pr-zlib 1.0.5 → 1.1.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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/{CHANGES → CHANGES.md} +19 -6
- data/Gemfile +6 -0
- data/MANIFEST.md +20 -0
- data/{README → README.md} +29 -12
- data/Rakefile +11 -61
- data/bin/{minizip.rb → minirbgzip} +77 -65
- data/lib/pr/rbzlib/bytef.rb +16 -0
- data/lib/pr/rbzlib/bytef_arr.rb +29 -0
- data/lib/pr/rbzlib/bytef_str.rb +52 -0
- data/lib/pr/rbzlib/posf.rb +33 -0
- data/lib/pr/rbzlib.rb +1758 -1855
- data/lib/pr/zlib/deflate.rb +99 -0
- data/lib/pr/zlib/errors.rb +27 -0
- data/lib/pr/zlib/gzipfile.rb +315 -0
- data/lib/pr/zlib/gzipfile_errors.rb +19 -0
- data/lib/pr/zlib/gzipreader.rb +338 -0
- data/lib/pr/zlib/gzipwriter.rb +167 -0
- data/lib/pr/zlib/inflate.rb +114 -0
- data/lib/pr/zlib/zstream.rb +417 -0
- data/lib/pr/zlib.rb +25 -1466
- data/pr-zlib.gemspec +15 -15
- data/profile/bench_pr_zlib.rb +7 -5
- data/profile/bench_zlib.rb +7 -5
- data/profile/profile_pr_zlib_read.rb +3 -3
- data/profile/profile_pr_zlib_write.rb +3 -3
- data/spec/README.md +46 -0
- data/spec/rbzlib/bytef_arr_spec.rb +82 -0
- data/spec/rbzlib/bytef_spec.rb +72 -0
- data/spec/rbzlib/bytef_str_spec.rb +113 -0
- data/spec/rbzlib/posf_spec.rb +83 -0
- data/spec/rbzlib_spec.rb +170 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/zlib/deflate_spec.rb +44 -0
- data/spec/zlib/gzip_file_spec.rb +86 -0
- data/spec/zlib/gzip_reader_spec.rb +276 -0
- data/spec/zlib/gzip_writer_spec.rb +275 -0
- data/spec/zlib/inflate_spec.rb +44 -0
- data/spec/zlib/zstream_spec.rb +156 -0
- data/spec/zlib_spec.rb +195 -0
- data.tar.gz.sig +0 -0
- metadata +68 -55
- metadata.gz.sig +0 -0
- data/MANIFEST +0 -20
- data/test/test_rbzlib.rb +0 -133
- data/test/test_rbzlib_bytef.rb +0 -76
- data/test/test_rbzlib_posf.rb +0 -56
- data/test/test_zlib.rb +0 -162
- data/test/test_zlib_deflate.rb +0 -55
- data/test/test_zlib_gzip_file.rb +0 -93
- data/test/test_zlib_gzip_reader.rb +0 -183
- data/test/test_zlib_gzip_writer.rb +0 -186
- data/test/test_zlib_inflate.rb +0 -55
- data/test/test_zlib_zstream.rb +0 -146
@@ -0,0 +1,417 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'errors'
|
4
|
+
|
5
|
+
module Zlib
|
6
|
+
class ZStream
|
7
|
+
attr_accessor :flags, :buf, :input, :stream, :func
|
8
|
+
|
9
|
+
def raise_zlib_error(err, msg)
|
10
|
+
msg = zError(err) if msg.nil? || msg == ''
|
11
|
+
|
12
|
+
case err
|
13
|
+
when Z_STREAM_END
|
14
|
+
raise StreamEnd, msg
|
15
|
+
when Z_NEED_DICT
|
16
|
+
raise NeedDict, msg
|
17
|
+
when Z_STREAM_ERROR
|
18
|
+
raise StreamError, msg
|
19
|
+
when Z_DATA_ERROR
|
20
|
+
raise DataError, msg
|
21
|
+
when Z_BUF_ERROR
|
22
|
+
raise BufError, msg
|
23
|
+
when Z_VERSION_ERROR
|
24
|
+
raise VersionError, msg
|
25
|
+
when Z_MEM_ERROR
|
26
|
+
raise MemError, msg
|
27
|
+
when Z_ERRNO
|
28
|
+
raise SystemCallError, msg
|
29
|
+
else
|
30
|
+
raise Error, 'unknown zlib error #errend: #msgend'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def zstream_expand_buffer
|
35
|
+
if @buf.nil?
|
36
|
+
@buf = Bytef.new(0.chr * ZSTREAM_INITIAL_BUFSIZE)
|
37
|
+
@stream.next_out = Bytef.new(@buf)
|
38
|
+
@stream.avail_out = ZSTREAM_INITIAL_BUFSIZE
|
39
|
+
return
|
40
|
+
end
|
41
|
+
|
42
|
+
if @buf.length - @buf.offset >= ZSTREAM_AVAIL_OUT_STEP_MAX
|
43
|
+
@stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX
|
44
|
+
else
|
45
|
+
inc = @buf.offset / 2
|
46
|
+
if inc < ZSTREAM_AVAIL_OUT_STEP_MIN
|
47
|
+
inc = ZSTREAM_AVAIL_OUT_STEP_MIN
|
48
|
+
end
|
49
|
+
if @buf.length < @buf.offset + inc
|
50
|
+
@buf.buffer << 0.chr * (@buf.offset + inc - @buf.length)
|
51
|
+
end
|
52
|
+
@stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
|
53
|
+
inc : ZSTREAM_AVAIL_OUT_STEP_MAX
|
54
|
+
end
|
55
|
+
@stream.next_out = Bytef.new(@buf, @buf.offset)
|
56
|
+
end
|
57
|
+
|
58
|
+
def zstream_append_buffer(src, len)
|
59
|
+
if @buf.nil?
|
60
|
+
@buf = Bytef.new(src[0, len], len)
|
61
|
+
@stream.next_out = Bytef.new(@buf)
|
62
|
+
@stream.avail_out = 0
|
63
|
+
return
|
64
|
+
end
|
65
|
+
if @buf.length < @buf.offset + len
|
66
|
+
@buf.buffer << (0.chr * (@buf.offset + len - @buf.length))
|
67
|
+
@stream.avail_out = 0
|
68
|
+
else
|
69
|
+
if @stream.avail_out >= len
|
70
|
+
@stream.avail_out -= len
|
71
|
+
else
|
72
|
+
@stream.avail_out = 0
|
73
|
+
end
|
74
|
+
end
|
75
|
+
@buf.buffer[@buf.offset, len] = src[0, len]
|
76
|
+
@buf += len
|
77
|
+
@stream.next_out = Bytef.new(@buf, @buf.offset)
|
78
|
+
end
|
79
|
+
|
80
|
+
def zstream_detach_buffer
|
81
|
+
if @buf.nil?
|
82
|
+
dst = ''
|
83
|
+
else
|
84
|
+
dst = @buf.buffer[0, @buf.offset]
|
85
|
+
end
|
86
|
+
|
87
|
+
@buf = Bytef.new(0.chr * ZSTREAM_INITIAL_BUFSIZE)
|
88
|
+
@stream.next_out = Bytef.new(@buf)
|
89
|
+
@stream.avail_out = ZSTREAM_INITIAL_BUFSIZE
|
90
|
+
@buf_filled = 0
|
91
|
+
|
92
|
+
dst
|
93
|
+
end
|
94
|
+
|
95
|
+
def zstream_shift_buffer(len)
|
96
|
+
if @buf.offset <= len
|
97
|
+
return zstream_detach_buffer()
|
98
|
+
end
|
99
|
+
|
100
|
+
dst = @buf.buffer[0, len]
|
101
|
+
@buf -= len
|
102
|
+
@buf.buffer[0, @buf.offset] = @buf.buffer[len, @buf.offset]
|
103
|
+
@stream.next_out = Bytef.new(@buf, @buf.offset)
|
104
|
+
@stream.avail_out = @buf.length - @buf.offset
|
105
|
+
if @stream.avail_out > ZSTREAM_AVAIL_OUT_STEP_MAX
|
106
|
+
@stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX
|
107
|
+
end
|
108
|
+
dst
|
109
|
+
end
|
110
|
+
|
111
|
+
def zstream_buffer_ungetc(c)
|
112
|
+
if @buf.nil? || (@buf.length - @buf.offset).zero?
|
113
|
+
zstream_expand_buffer()
|
114
|
+
end
|
115
|
+
@buf.buffer[0, 0] = c.chr
|
116
|
+
@buf += 1
|
117
|
+
if @stream.avail_out > 0
|
118
|
+
@stream.next_out += 1
|
119
|
+
@stream.avail_out -= 1
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def zstream_append_input(src, len)
|
124
|
+
return if len <= 0
|
125
|
+
src = src.current if src.class != String
|
126
|
+
if @input.nil?
|
127
|
+
@input = src[0, len]
|
128
|
+
else
|
129
|
+
@input << src[0, len]
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def zstream_discard_input(len)
|
134
|
+
if @input.nil? || @input.length <= len
|
135
|
+
@input = nil
|
136
|
+
else
|
137
|
+
@input[0, len] = ''
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def zstream_reset_input
|
142
|
+
@input = nil
|
143
|
+
end
|
144
|
+
|
145
|
+
def zstream_passthrough_input
|
146
|
+
if @input
|
147
|
+
zstream_append_buffer(@input, @input.length)
|
148
|
+
@input = nil
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def zstream_detach_input
|
153
|
+
if @input.nil?
|
154
|
+
dst = ''
|
155
|
+
else
|
156
|
+
dst = @input
|
157
|
+
end
|
158
|
+
@input = nil
|
159
|
+
dst
|
160
|
+
end
|
161
|
+
|
162
|
+
def zstream_reset
|
163
|
+
err = send(@func.reset, @stream)
|
164
|
+
if err != Z_OK
|
165
|
+
raise_zlib_error(err, @stream.msg)
|
166
|
+
end
|
167
|
+
@flags = ZSTREAM_FLAG_READY
|
168
|
+
@buf = nil
|
169
|
+
@buf_filled = 0
|
170
|
+
@stream.next_out = 0
|
171
|
+
@stream.avail_out = 0
|
172
|
+
zstream_reset_input()
|
173
|
+
end
|
174
|
+
|
175
|
+
def zstream_end
|
176
|
+
if !ZSTREAM_IS_READY()
|
177
|
+
warn('attempt to close uninitialized zstream; ignored.')
|
178
|
+
return nil
|
179
|
+
end
|
180
|
+
if (@flags & ZSTREAM_FLAG_IN_STREAM).nonzero?
|
181
|
+
warn('attempt to close unfinished zstream; reset forced.')
|
182
|
+
zstream_reset()
|
183
|
+
end
|
184
|
+
|
185
|
+
zstream_reset_input()
|
186
|
+
err = send(@func.end, @stream)
|
187
|
+
if err != Z_OK
|
188
|
+
raise_zlib_error(err, @stream.msg)
|
189
|
+
end
|
190
|
+
@flags = 0
|
191
|
+
nil
|
192
|
+
end
|
193
|
+
|
194
|
+
def zstream_sync(src, len)
|
195
|
+
if @input
|
196
|
+
@stream.next_in = Bytef.new(@input)
|
197
|
+
@stream.avail_in = @input.length
|
198
|
+
err = inflateSync(@stream)
|
199
|
+
if err == Z_OK
|
200
|
+
zstream_discard_input(@input.length - @stream.avail_in)
|
201
|
+
zstream_append_input(src, len)
|
202
|
+
return true
|
203
|
+
end
|
204
|
+
zstream_reset_input()
|
205
|
+
if err != Z_DATA_ERROR
|
206
|
+
@stream.next_in.buffer[0, @stream.avail_in]
|
207
|
+
raise_zlib_error(err, @stream.msg)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
return false if len <= 0
|
212
|
+
|
213
|
+
@stream.next_in = src
|
214
|
+
@stream.avail_in = len
|
215
|
+
err = inflateSync(@stream)
|
216
|
+
if err == Z_OK
|
217
|
+
zstream_append_input(@stream.next_in, @stream.avail_in)
|
218
|
+
return true
|
219
|
+
end
|
220
|
+
if err != Z_DATA_ERROR
|
221
|
+
@stream.next_in.buffer[0, @stream.avail_in]
|
222
|
+
raise_zlib_error(err, @stream.msg)
|
223
|
+
end
|
224
|
+
false
|
225
|
+
end
|
226
|
+
|
227
|
+
def zstream_init(func)
|
228
|
+
@flags = 0
|
229
|
+
@buf = nil
|
230
|
+
@input = nil
|
231
|
+
@stream = Z_stream.new
|
232
|
+
@stream.msg = ''
|
233
|
+
@stream.next_in = nil
|
234
|
+
@stream.avail_in = 0
|
235
|
+
@stream.next_out = nil
|
236
|
+
@stream.avail_out = 0
|
237
|
+
@func = func
|
238
|
+
end
|
239
|
+
|
240
|
+
def zstream_run(src, len, flush)
|
241
|
+
if @input.nil? && len == 0
|
242
|
+
@stream.next_in = ''
|
243
|
+
@stream.avail_in = 0
|
244
|
+
else
|
245
|
+
zstream_append_input(src, len)
|
246
|
+
@stream.next_in = Bytef.new(@input)
|
247
|
+
@stream.avail_in = @input.length
|
248
|
+
end
|
249
|
+
if @stream.avail_out.zero?
|
250
|
+
zstream_expand_buffer()
|
251
|
+
end
|
252
|
+
|
253
|
+
loop do
|
254
|
+
n = @stream.avail_out
|
255
|
+
err = send(@func.run, @stream, flush)
|
256
|
+
@buf += n - @stream.avail_out
|
257
|
+
if err == Z_STREAM_END
|
258
|
+
@flags &= ~ZSTREAM_FLAG_IN_STREAM
|
259
|
+
@flags |= ZSTREAM_FLAG_FINISHED
|
260
|
+
break
|
261
|
+
end
|
262
|
+
if err != Z_OK
|
263
|
+
if flush != Z_FINISH && err == Z_BUF_ERROR && @stream.avail_out > 0
|
264
|
+
@flags |= ZSTREAM_FLAG_IN_STREAM
|
265
|
+
break
|
266
|
+
end
|
267
|
+
@input = nil
|
268
|
+
if @stream.avail_in > 0
|
269
|
+
zstream_append_input(@stream.next_in, @stream.avail_in)
|
270
|
+
end
|
271
|
+
raise_zlib_error(err, @stream.msg)
|
272
|
+
end
|
273
|
+
if @stream.avail_out > 0
|
274
|
+
@flags |= ZSTREAM_FLAG_IN_STREAM
|
275
|
+
break
|
276
|
+
end
|
277
|
+
zstream_expand_buffer()
|
278
|
+
end
|
279
|
+
|
280
|
+
@input = nil
|
281
|
+
if @stream.avail_in > 0
|
282
|
+
zstream_append_input(@stream.next_in, @stream.avail_in)
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
def ZSTREAM_READY
|
287
|
+
(@flags |= ZSTREAM_FLAG_READY)
|
288
|
+
end
|
289
|
+
|
290
|
+
def ZSTREAM_IS_READY
|
291
|
+
!(@flags & ZSTREAM_FLAG_READY).zero?
|
292
|
+
end
|
293
|
+
|
294
|
+
def ZSTREAM_IS_FINISHED
|
295
|
+
!(@flags & ZSTREAM_FLAG_FINISHED).zero?
|
296
|
+
end
|
297
|
+
|
298
|
+
def ZSTREAM_IS_CLOSING
|
299
|
+
!(@flags & ZSTREAM_FLAG_CLOSING).zero?
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
@@final = proc do |z|
|
304
|
+
proc do
|
305
|
+
if z && z.ZSTREAM_IS_READY()
|
306
|
+
err = send(z.func.end, z.stream)
|
307
|
+
if err == Z_STREAM_ERROR
|
308
|
+
warn('the stream state was inconsistent.')
|
309
|
+
end
|
310
|
+
if err == Z_DATA_ERROR
|
311
|
+
warn('the stream was freed prematurely.')
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
attr_reader :z
|
318
|
+
|
319
|
+
def avail_out
|
320
|
+
@z.stream.avail_out
|
321
|
+
end
|
322
|
+
|
323
|
+
def avail_out=(size)
|
324
|
+
if @z.buf.nil?
|
325
|
+
@z.buf = Bytef.new(0.chr * size)
|
326
|
+
@z.stream.next_out = Bytef.new(@z.buf)
|
327
|
+
@z.stream.avail_out = size
|
328
|
+
elsif @z.stream.avail_out != size
|
329
|
+
if @z.buf.offset + size > @z.buf.length
|
330
|
+
@z.buf.buffer << 0.chr * (@z.buf.offset + size - @z.buf.length)
|
331
|
+
end
|
332
|
+
@z.stream.next_out = Bytef.new(@z.buf, @z.buf.offset)
|
333
|
+
@z.stream.avail_out = size
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def avail_in
|
338
|
+
@z.input.nil? ? 0 : @z.input.length
|
339
|
+
end
|
340
|
+
|
341
|
+
def total_in
|
342
|
+
raise GzipFile::Error, 'closed gzip stream' unless @gz.z.ZSTREAM_IS_READY()
|
343
|
+
@z.stream.total_in
|
344
|
+
end
|
345
|
+
|
346
|
+
def total_out
|
347
|
+
raise GzipFile::Error, 'closed gzip stream' unless @gz.z.ZSTREAM_IS_READY()
|
348
|
+
@z.stream.total_out
|
349
|
+
end
|
350
|
+
|
351
|
+
def data_type
|
352
|
+
@z.stream.data_type
|
353
|
+
end
|
354
|
+
|
355
|
+
def adler
|
356
|
+
@z.stream.adler
|
357
|
+
end
|
358
|
+
|
359
|
+
def finished?
|
360
|
+
@z.ZSTREAM_IS_FINISHED()
|
361
|
+
end
|
362
|
+
alias stream_end? :finished?
|
363
|
+
|
364
|
+
def closed?
|
365
|
+
@z.ZSTREAM_IS_READY()
|
366
|
+
end
|
367
|
+
alias ended? :closed?
|
368
|
+
|
369
|
+
def close
|
370
|
+
if !@z.ZSTREAM_IS_READY()
|
371
|
+
warn('attempt to close uninitialized zstream ignored.')
|
372
|
+
return nil
|
373
|
+
end
|
374
|
+
if (@z.flags & ZSTREAM_FLAG_IN_STREAM).nonzero?
|
375
|
+
warn('attempt to close unfinished zstream reset forced.')
|
376
|
+
@z.input = nil
|
377
|
+
end
|
378
|
+
|
379
|
+
@z.input = nil
|
380
|
+
err = send(@z.func.end, @z.stream)
|
381
|
+
if err != Z_OK
|
382
|
+
raise_zlib_error(err, @z.stream.msg)
|
383
|
+
end
|
384
|
+
@z.flags = 0
|
385
|
+
end
|
386
|
+
alias end :close
|
387
|
+
|
388
|
+
def reset
|
389
|
+
err = send(@z.func.reset, @z.stream)
|
390
|
+
if err != Z_OK
|
391
|
+
raise_zlib_error(err, @z.stream.msg)
|
392
|
+
end
|
393
|
+
@z.flags = ZSTREAM_FLAG_READY
|
394
|
+
@z.buf = nil
|
395
|
+
@z.stream.next_out = 0
|
396
|
+
@z.stream.avail_out = 0
|
397
|
+
@z.input = nil
|
398
|
+
end
|
399
|
+
|
400
|
+
def finish
|
401
|
+
@z.zstream_run('', 0, Z_FINISH)
|
402
|
+
@z.zstream_detach_buffer()
|
403
|
+
end
|
404
|
+
|
405
|
+
def flush_next_in
|
406
|
+
@z.zstream_detach_input
|
407
|
+
end
|
408
|
+
|
409
|
+
def flush_next_out
|
410
|
+
@z.zstream_detach_buffer
|
411
|
+
end
|
412
|
+
|
413
|
+
def initialize
|
414
|
+
@z = nil
|
415
|
+
ObjectSpace.define_finalizer self, @@final.call(@z)
|
416
|
+
end
|
417
|
+
end
|