ziperu 0.2.2-x86_64-linux-gnu → 0.3.0-x86_64-linux-gnu
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
- data/CHANGELOG.md +197 -0
- data/README.md +51 -31
- data/lib/zip.rb +3 -3
- data/lib/ziperu/3.1/ziperu.so +0 -0
- data/lib/ziperu/3.2/ziperu.so +0 -0
- data/lib/ziperu/3.3/ziperu.so +0 -0
- data/lib/ziperu/3.4/ziperu.so +0 -0
- data/lib/ziperu/4.0/ziperu.so +0 -0
- data/lib/ziperu/central_directory.rb +4 -8
- data/lib/ziperu/constants.rb +8 -0
- data/lib/ziperu/crypto.rb +10 -2
- data/lib/ziperu/dos_time.rb +1 -1
- data/lib/ziperu/entry/source.rb +6 -0
- data/lib/ziperu/entry.rb +275 -107
- data/lib/ziperu/entry_set.rb +0 -5
- data/lib/ziperu/errors.rb +4 -2
- data/lib/ziperu/extra_field/zip64.rb +1 -3
- data/lib/ziperu/extra_field.rb +10 -10
- data/lib/ziperu/file/commit.rb +255 -0
- data/lib/ziperu/file/extraction.rb +280 -0
- data/lib/ziperu/file.rb +52 -363
- data/lib/ziperu/file_split.rb +1 -3
- data/lib/ziperu/filesystem/dir.rb +3 -9
- data/lib/ziperu/filesystem/file.rb +2 -6
- data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
- data/lib/ziperu/input_stream.rb +48 -36
- data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
- data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
- data/lib/ziperu/ioextras/fake_io.rb +1 -3
- data/lib/ziperu/ioextras.rb +6 -0
- data/lib/ziperu/limits.rb +48 -0
- data/lib/ziperu/null_input_stream.rb +1 -1
- data/lib/ziperu/output_stream.rb +127 -30
- data/lib/ziperu/pass_thru_decompressor.rb +1 -3
- data/lib/ziperu/rails_streaming.rb +5 -5
- data/lib/ziperu/remote_io.rb +181 -32
- data/lib/ziperu/size_estimator.rb +27 -19
- data/lib/ziperu/streamable_stream.rb +110 -10
- data/lib/ziperu/verify_report.rb +62 -0
- data/lib/ziperu/version.rb +2 -2
- data/lib/ziperu.rb +19 -17
- metadata +7 -2
data/lib/ziperu/input_stream.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Ziperu
|
|
4
4
|
# Reads the entries of an archive one after another, exposing each as an
|
|
5
|
-
# IO-like stream, exactly as rubyzip's `
|
|
5
|
+
# IO-like stream, exactly as rubyzip's `Zip::InputStream` does:
|
|
6
6
|
#
|
|
7
7
|
# Ziperu::InputStream.open("my.zip") do |io|
|
|
8
8
|
# while (entry = io.get_next_entry)
|
|
@@ -53,25 +53,25 @@ module Ziperu
|
|
|
53
53
|
# AES from ZipCrypto by the entry, so one password serves both.
|
|
54
54
|
decrypter ||= TraditionalDecrypter.new(password) if password
|
|
55
55
|
super(**opts)
|
|
56
|
+
@decrypter = decrypter
|
|
57
|
+
@enforce_size = enforce_size
|
|
58
|
+
@reader = nil
|
|
59
|
+
@current_entry = nil
|
|
56
60
|
if streaming && self.class.pipe_like?(context)
|
|
57
61
|
IOExtras.binmode(context)
|
|
58
62
|
@stream = Native::StreamReader.open(context, decrypter&.password&.b, enforce_size)
|
|
59
63
|
@stream_entries = 0
|
|
60
64
|
@stream_total = 0
|
|
61
|
-
@decrypter = decrypter
|
|
62
|
-
@enforce_size = enforce_size
|
|
63
65
|
@owns_archive = false
|
|
64
66
|
@archive = nil
|
|
65
|
-
@reader = nil
|
|
66
|
-
@current_entry = nil
|
|
67
67
|
return
|
|
68
68
|
end
|
|
69
|
-
@split = !context.kind_of?(Native::Archive) && self.class.split_archive?(context)
|
|
70
69
|
# An archive handed in (by Entry#get_input_stream) stays open for its
|
|
71
70
|
# owner; one opened here is closed with the stream. A split segment
|
|
72
71
|
# has no central directory to open; rubyzip fails on the first entry,
|
|
73
72
|
# so defer the error to `get_next_entry`.
|
|
74
73
|
@owns_archive = !context.kind_of?(Native::Archive)
|
|
74
|
+
@split = @owns_archive && self.class.split_archive?(context)
|
|
75
75
|
@archive =
|
|
76
76
|
if !@owns_archive
|
|
77
77
|
context
|
|
@@ -91,25 +91,20 @@ module Ziperu
|
|
|
91
91
|
nil
|
|
92
92
|
end
|
|
93
93
|
end
|
|
94
|
-
@decrypter = decrypter
|
|
95
|
-
@enforce_size = enforce_size
|
|
96
94
|
# An archive with several records under one name is walked from its
|
|
97
95
|
# listing, since the native core addresses only the last of each name
|
|
98
96
|
# by index; every record is yielded, as rubyzip's walk of the local
|
|
99
97
|
# headers would.
|
|
100
98
|
@records = @archive.entries if @archive&.duplicate_names?
|
|
101
99
|
@next_index = offset.zero? || @archive.nil? ? 0 : index_at_offset(offset)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
@reader = nil
|
|
106
|
-
@current_entry = nil
|
|
100
|
+
return unless @next_index.nil?
|
|
101
|
+
|
|
102
|
+
raise Error, "no entry starts at offset #{offset} (Zip local header magic not found)"
|
|
107
103
|
end
|
|
108
104
|
|
|
109
105
|
# Close this InputStream. All further IO will raise an IOError.
|
|
110
106
|
def close
|
|
111
|
-
|
|
112
|
-
@reader = nil
|
|
107
|
+
close_reader
|
|
113
108
|
@stream&.close
|
|
114
109
|
@stream = nil
|
|
115
110
|
@archive&.close if @owns_archive
|
|
@@ -121,15 +116,15 @@ module Ziperu
|
|
|
121
116
|
# the entry data. Returns nil when there are no more entries.
|
|
122
117
|
def get_next_entry
|
|
123
118
|
raise SplitArchiveError if @split
|
|
119
|
+
|
|
124
120
|
check_open!
|
|
125
121
|
return next_stream_entry if @stream
|
|
126
|
-
|
|
127
122
|
return @current_entry = nil if @archive.nil? || @next_index.nil?
|
|
123
|
+
|
|
128
124
|
if @next_index >= (@records ? @records.size : @archive.entry_count)
|
|
129
125
|
# Past the last entry there is nothing to read, not the previous
|
|
130
126
|
# entry again.
|
|
131
|
-
|
|
132
|
-
@reader = nil
|
|
127
|
+
close_reader
|
|
133
128
|
flush
|
|
134
129
|
return @current_entry = nil
|
|
135
130
|
end
|
|
@@ -137,7 +132,9 @@ module Ziperu
|
|
|
137
132
|
@current_entry = Entry.from_meta(@archive, meta_at(@next_index),
|
|
138
133
|
metadata_encoding: @metadata_encoding, settings: @settings)
|
|
139
134
|
@next_index += 1
|
|
140
|
-
|
|
135
|
+
# The entry's data is opened when it is first read: a walk that only
|
|
136
|
+
# looks at the entries reads no local header.
|
|
137
|
+
open_entry(lazy: true)
|
|
141
138
|
@current_entry
|
|
142
139
|
end
|
|
143
140
|
|
|
@@ -171,7 +168,7 @@ module Ziperu
|
|
|
171
168
|
@next_index =
|
|
172
169
|
if @records
|
|
173
170
|
at = @records.index { |meta| meta[:header_offset] == entry.local_header_offset }
|
|
174
|
-
at && at + 1
|
|
171
|
+
at && (at + 1)
|
|
175
172
|
else
|
|
176
173
|
entry.index + 1
|
|
177
174
|
end
|
|
@@ -187,7 +184,7 @@ module Ziperu
|
|
|
187
184
|
|
|
188
185
|
@lineno = 0
|
|
189
186
|
@pos = 0
|
|
190
|
-
wrap_errors {
|
|
187
|
+
wrap_errors { reader.rewind }
|
|
191
188
|
flush
|
|
192
189
|
end
|
|
193
190
|
|
|
@@ -200,7 +197,7 @@ module Ziperu
|
|
|
200
197
|
# Not in rubyzip.
|
|
201
198
|
def seek(offset, whence = ::IO::SEEK_SET)
|
|
202
199
|
check_open!
|
|
203
|
-
raise Error, 'no entry to seek in' if
|
|
200
|
+
raise Error, 'no entry to seek in' if reader.nil?
|
|
204
201
|
raise Error, 'an entry read with streaming: true cannot be seeked in' if @stream
|
|
205
202
|
|
|
206
203
|
target =
|
|
@@ -212,7 +209,7 @@ module Ziperu
|
|
|
212
209
|
end
|
|
213
210
|
raise Errno::EINVAL, "seek to #{target}" if target.negative?
|
|
214
211
|
|
|
215
|
-
wrap_errors {
|
|
212
|
+
wrap_errors { reader.seek(target) }
|
|
216
213
|
flush
|
|
217
214
|
@pos = [target, @current_entry.size].min
|
|
218
215
|
0
|
|
@@ -257,7 +254,7 @@ module Ziperu
|
|
|
257
254
|
begin
|
|
258
255
|
yield zio
|
|
259
256
|
ensure
|
|
260
|
-
zio.close
|
|
257
|
+
zio.close
|
|
261
258
|
end
|
|
262
259
|
end
|
|
263
260
|
|
|
@@ -316,7 +313,7 @@ module Ziperu
|
|
|
316
313
|
context.seek(0, ::IO::SEEK_SET)
|
|
317
314
|
context.read(4)
|
|
318
315
|
end
|
|
319
|
-
head.to_s.b.start_with?([LOCAL_ENTRY_SIGNATURE].pack(
|
|
316
|
+
head.to_s.b.start_with?([LOCAL_ENTRY_SIGNATURE].pack('V'))
|
|
320
317
|
rescue SystemCallError, IOError
|
|
321
318
|
false
|
|
322
319
|
end
|
|
@@ -360,7 +357,7 @@ module Ziperu
|
|
|
360
357
|
return io.read.to_s.b unless limit
|
|
361
358
|
|
|
362
359
|
bytes = +''.b
|
|
363
|
-
while (chunk = io.read(
|
|
360
|
+
while (chunk = io.read(CHUNK_SIZE))
|
|
364
361
|
bytes << chunk
|
|
365
362
|
raise LimitError.new('an archive size', bytes.bytesize, limit) if bytes.bytesize > limit
|
|
366
363
|
end
|
|
@@ -392,16 +389,26 @@ module Ziperu
|
|
|
392
389
|
|
|
393
390
|
protected
|
|
394
391
|
|
|
395
|
-
def open_entry # :nodoc:
|
|
392
|
+
def open_entry(lazy: false) # :nodoc:
|
|
396
393
|
entry = @current_entry
|
|
397
394
|
unless READABLE_COMPRESSION_METHODS.include?(entry.compression_method)
|
|
398
395
|
raise CompressionMethodError, entry.compression_method
|
|
399
396
|
end
|
|
400
397
|
|
|
401
|
-
if entry.encrypted? && @decrypter.nil?
|
|
402
|
-
|
|
403
|
-
|
|
398
|
+
raise Error, PASSWORD_REQUIRED_MESSAGE if entry.encrypted? && @decrypter.nil?
|
|
399
|
+
|
|
400
|
+
close_reader
|
|
401
|
+
@lazy_open = true
|
|
402
|
+
reader unless lazy
|
|
403
|
+
flush
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
# The native reader of the current entry, opened on first use.
|
|
407
|
+
def reader # :nodoc:
|
|
408
|
+
return @reader unless @lazy_open
|
|
404
409
|
|
|
410
|
+
@lazy_open = false
|
|
411
|
+
entry = @current_entry
|
|
405
412
|
password = @decrypter&.password&.b
|
|
406
413
|
@reader = wrap_errors do
|
|
407
414
|
if entry.index
|
|
@@ -411,7 +418,6 @@ module Ziperu
|
|
|
411
418
|
entry.crc, password, @enforce_size)
|
|
412
419
|
end
|
|
413
420
|
end
|
|
414
|
-
flush
|
|
415
421
|
end
|
|
416
422
|
|
|
417
423
|
def produce_input(maxlen = CHUNK_SIZE) # :nodoc:
|
|
@@ -420,8 +426,8 @@ module Ziperu
|
|
|
420
426
|
raise TypeError, "no implicit conversion of #{maxlen.class} into Integer"
|
|
421
427
|
end
|
|
422
428
|
raise ArgumentError, "negative length #{maxlen} given" if maxlen&.negative?
|
|
423
|
-
return nil if @reader.nil?
|
|
424
429
|
return produce_stream_input(maxlen) if @stream
|
|
430
|
+
return nil if reader.nil?
|
|
425
431
|
|
|
426
432
|
wrap_errors { @reader.read(maxlen) }
|
|
427
433
|
end
|
|
@@ -430,11 +436,17 @@ module Ziperu
|
|
|
430
436
|
# A closed stream is not "at EOF": reading it raises, as with an IO.
|
|
431
437
|
return false if closed?
|
|
432
438
|
|
|
433
|
-
|
|
439
|
+
reader.nil? || @reader.eof?
|
|
434
440
|
end
|
|
435
441
|
|
|
436
442
|
private
|
|
437
443
|
|
|
444
|
+
def close_reader
|
|
445
|
+
@reader&.close
|
|
446
|
+
@reader = nil
|
|
447
|
+
@lazy_open = false
|
|
448
|
+
end
|
|
449
|
+
|
|
438
450
|
# Nil when no entry starts there, which reads as "no more entries".
|
|
439
451
|
def index_at_offset(offset)
|
|
440
452
|
return @records.index { |meta| meta[:header_offset] == offset } if @records
|
|
@@ -461,6 +473,7 @@ module Ziperu
|
|
|
461
473
|
if (limit = @settings.max_entries) && @stream_entries > limit
|
|
462
474
|
raise LimitError.new('an entry count', @stream_entries, limit)
|
|
463
475
|
end
|
|
476
|
+
|
|
464
477
|
if meta[:size]
|
|
465
478
|
check_stream_entry_size(meta[:size])
|
|
466
479
|
if (limit = @settings.max_compression_ratio) && meta[:size].positive?
|
|
@@ -472,7 +485,7 @@ module Ziperu
|
|
|
472
485
|
meta[:crc] ||= 0
|
|
473
486
|
meta[:compressed_size] ||= 0
|
|
474
487
|
@current_entry = Entry.from_meta(nil, meta, metadata_encoding: @metadata_encoding, settings: @settings)
|
|
475
|
-
@current_entry.
|
|
488
|
+
@current_entry.learn_data(nil, 0) if meta[:size].nil?
|
|
476
489
|
@stream_entry_bytes = 0
|
|
477
490
|
@reader = @stream
|
|
478
491
|
@lineno = 0
|
|
@@ -518,8 +531,7 @@ module Ziperu
|
|
|
518
531
|
crc, size = @stream.trailer
|
|
519
532
|
return unless crc && !@current_entry.size_known?
|
|
520
533
|
|
|
521
|
-
@current_entry.
|
|
522
|
-
@current_entry.instance_variable_set(:@crc, crc)
|
|
534
|
+
@current_entry.learn_data(size, crc)
|
|
523
535
|
end
|
|
524
536
|
|
|
525
537
|
# Re-raise the native core's errors as the rubyzip-shaped ones.
|
|
@@ -13,28 +13,39 @@ module Ziperu
|
|
|
13
13
|
include Enumerable
|
|
14
14
|
include FakeIO
|
|
15
15
|
|
|
16
|
+
# Past this many handed-out bytes (and half the buffer), they are
|
|
17
|
+
# dropped from it.
|
|
18
|
+
COMPACT_AT = 64 * 1024
|
|
19
|
+
private_constant :COMPACT_AT
|
|
20
|
+
|
|
16
21
|
def initialize(**opts)
|
|
17
22
|
super
|
|
18
23
|
@lineno = 0
|
|
19
24
|
@pos = 0
|
|
25
|
+
# Bytes produced but not yet handed out are those of
|
|
26
|
+
# `@output_buffer` from `@out_pos` on. Handing some out moves the
|
|
27
|
+
# offset rather than shifting the rest down (`slice!`), which for a
|
|
28
|
+
# line at a time from a 64 KB chunk copied most of the chunk per
|
|
29
|
+
# line; the consumed part is dropped once it is large.
|
|
20
30
|
@output_buffer = +''.b
|
|
31
|
+
@out_pos = 0
|
|
21
32
|
end
|
|
22
33
|
|
|
23
34
|
attr_accessor :lineno
|
|
24
35
|
attr_reader :pos
|
|
25
36
|
|
|
26
|
-
def read(maxlen = nil, out_string = nil)
|
|
37
|
+
def read(maxlen = nil, out_string = nil)
|
|
27
38
|
return (maxlen.nil? || maxlen.zero? ? '' : nil) if eof?
|
|
28
39
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
40
|
+
available = buffered_size
|
|
41
|
+
tbuf = if available > 0
|
|
42
|
+
if maxlen && maxlen <= available
|
|
43
|
+
take_buffered(maxlen)
|
|
32
44
|
else
|
|
33
|
-
maxlen -=
|
|
45
|
+
maxlen -= available if maxlen
|
|
34
46
|
rbuf = produce_input(maxlen)
|
|
35
|
-
out =
|
|
47
|
+
out = take_buffered(available)
|
|
36
48
|
out << rbuf if rbuf
|
|
37
|
-
@output_buffer = +''.b
|
|
38
49
|
out
|
|
39
50
|
end
|
|
40
51
|
else
|
|
@@ -61,7 +72,7 @@ module Ziperu
|
|
|
61
72
|
each(sep, limit, chomp: chomp).to_a
|
|
62
73
|
end
|
|
63
74
|
|
|
64
|
-
def gets(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
|
|
75
|
+
def gets(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
|
|
65
76
|
if sep.nil?
|
|
66
77
|
return nil if eof?
|
|
67
78
|
|
|
@@ -69,41 +80,34 @@ module Ziperu
|
|
|
69
80
|
return read(limit)
|
|
70
81
|
end
|
|
71
82
|
|
|
72
|
-
|
|
73
|
-
limit = sep.to_int
|
|
74
|
-
sep = $INPUT_RECORD_SEPARATOR
|
|
75
|
-
elsif sep&.empty?
|
|
76
|
-
sep = "#{$INPUT_RECORD_SEPARATOR}#{$INPUT_RECORD_SEPARATOR}"
|
|
77
|
-
end
|
|
78
|
-
|
|
83
|
+
sep, limit = separator_and_limit(sep, limit)
|
|
79
84
|
encoding = @internal_encoding || @external_encoding
|
|
80
85
|
|
|
81
|
-
|
|
86
|
+
# The buffer is binary, so `index` counts bytes.
|
|
87
|
+
buffer_index = @out_pos
|
|
82
88
|
while (sep_index = @output_buffer.index(sep, buffer_index)).nil?
|
|
83
|
-
break if limit &&
|
|
89
|
+
break if limit && buffered_size >= limit
|
|
84
90
|
|
|
85
91
|
# The input ends either before a read or, for an entry whose
|
|
86
92
|
# data turns out empty, on the read itself (nil).
|
|
87
93
|
chunk = input_finished? ? nil : produce_input
|
|
88
|
-
if chunk.nil?
|
|
89
|
-
return nil if @output_buffer.empty?
|
|
90
|
-
|
|
91
|
-
@lineno = @lineno.next
|
|
92
|
-
@pos += @output_buffer.bytesize
|
|
93
|
-
return @output_buffer.slice!(0..).force_encoding(encoding)
|
|
94
|
-
end
|
|
94
|
+
return take_last_line(encoding) if chunk.nil?
|
|
95
95
|
|
|
96
|
+
# A line runs on into the next chunk: drop what was handed out
|
|
97
|
+
# before appending, so the buffer holds only the unread bytes.
|
|
98
|
+
buffer_index -= @out_pos
|
|
99
|
+
drop_consumed
|
|
96
100
|
buffer_index = [buffer_index, @output_buffer.bytesize - sep.bytesize].max
|
|
97
101
|
@output_buffer << chunk
|
|
98
102
|
end
|
|
99
103
|
|
|
100
|
-
limit ||=
|
|
101
|
-
cut_index = sep_index ? [sep_index + sep.bytesize, limit].min : limit
|
|
104
|
+
limit ||= buffered_size
|
|
105
|
+
cut_index = sep_index ? [sep_index - @out_pos + sep.bytesize, limit].min : limit
|
|
102
106
|
@lineno = @lineno.next
|
|
103
107
|
@pos += cut_index
|
|
104
|
-
data =
|
|
105
|
-
data
|
|
106
|
-
data
|
|
108
|
+
data = take_buffered(cut_index)
|
|
109
|
+
data.chomp!(sep) if chomp
|
|
110
|
+
data.force_encoding(encoding)
|
|
107
111
|
end
|
|
108
112
|
|
|
109
113
|
# Half-open style: the bytes read here are what read(maxlen) gives, so
|
|
@@ -158,7 +162,9 @@ module Ziperu
|
|
|
158
162
|
def ungetc(char)
|
|
159
163
|
return if char.nil?
|
|
160
164
|
|
|
165
|
+
drop_consumed
|
|
161
166
|
@output_buffer = pushback_bytes(char) + @output_buffer
|
|
167
|
+
nil
|
|
162
168
|
end
|
|
163
169
|
|
|
164
170
|
alias ungetbyte ungetc
|
|
@@ -188,7 +194,7 @@ module Ziperu
|
|
|
188
194
|
end
|
|
189
195
|
|
|
190
196
|
def flush # :nodoc:
|
|
191
|
-
|
|
197
|
+
take_buffered(buffered_size)
|
|
192
198
|
end
|
|
193
199
|
|
|
194
200
|
def readline(sep = $INPUT_RECORD_SEPARATOR, limit = nil, chomp: false)
|
|
@@ -208,13 +214,59 @@ module Ziperu
|
|
|
208
214
|
alias each_line each
|
|
209
215
|
|
|
210
216
|
def eof?
|
|
211
|
-
|
|
217
|
+
buffered_size.zero? && input_finished?
|
|
212
218
|
end
|
|
213
219
|
|
|
214
220
|
alias eof eof? # :nodoc:
|
|
215
221
|
|
|
216
222
|
private
|
|
217
223
|
|
|
224
|
+
# `gets(limit)` and `gets('')` (paragraphs), as IO#gets reads them.
|
|
225
|
+
def separator_and_limit(sep, limit)
|
|
226
|
+
if sep.respond_to?(:to_int)
|
|
227
|
+
[$INPUT_RECORD_SEPARATOR, sep.to_int]
|
|
228
|
+
elsif sep.empty?
|
|
229
|
+
["#{$INPUT_RECORD_SEPARATOR}#{$INPUT_RECORD_SEPARATOR}", limit]
|
|
230
|
+
else
|
|
231
|
+
[sep, limit]
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# What is left when the input ends without a separator; nil when
|
|
236
|
+
# nothing is.
|
|
237
|
+
def take_last_line(encoding)
|
|
238
|
+
return nil if buffered_size.zero?
|
|
239
|
+
|
|
240
|
+
@lineno = @lineno.next
|
|
241
|
+
@pos += buffered_size
|
|
242
|
+
take_buffered(buffered_size).force_encoding(encoding)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Bytes produced and not yet handed out.
|
|
246
|
+
def buffered_size
|
|
247
|
+
@output_buffer.bytesize - @out_pos
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# The next `count` buffered bytes, as a String of their own.
|
|
251
|
+
def take_buffered(count)
|
|
252
|
+
data = @output_buffer.byteslice(@out_pos, count) || +''.b
|
|
253
|
+
@out_pos += data.bytesize
|
|
254
|
+
if @out_pos == @output_buffer.bytesize
|
|
255
|
+
@output_buffer = +''.b
|
|
256
|
+
@out_pos = 0
|
|
257
|
+
elsif @out_pos > COMPACT_AT && @out_pos * 2 > @output_buffer.bytesize
|
|
258
|
+
drop_consumed
|
|
259
|
+
end
|
|
260
|
+
data
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def drop_consumed
|
|
264
|
+
return if @out_pos.zero?
|
|
265
|
+
|
|
266
|
+
@output_buffer = @output_buffer.byteslice(@out_pos..)
|
|
267
|
+
@out_pos = 0
|
|
268
|
+
end
|
|
269
|
+
|
|
218
270
|
def pushback_bytes(char)
|
|
219
271
|
char.kind_of?(::Integer) ? char.chr.b : char.to_str.b
|
|
220
272
|
end
|
|
@@ -20,9 +20,7 @@ module Ziperu
|
|
|
20
20
|
self << params.join
|
|
21
21
|
|
|
22
22
|
# Deflate doesn't like `nil`s or empty strings!
|
|
23
|
-
unless $OUTPUT_RECORD_SEPARATOR.nil? || $OUTPUT_RECORD_SEPARATOR.empty?
|
|
24
|
-
self << $OUTPUT_RECORD_SEPARATOR
|
|
25
|
-
end
|
|
23
|
+
self << $OUTPUT_RECORD_SEPARATOR unless $OUTPUT_RECORD_SEPARATOR.nil? || $OUTPUT_RECORD_SEPARATOR.empty?
|
|
26
24
|
|
|
27
25
|
nil
|
|
28
26
|
end
|
|
@@ -19,9 +19,7 @@ module Ziperu
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def set_encoding(ext_enc, int_enc = nil)
|
|
22
|
-
if ext_enc.kind_of?(String) && ext_enc.include?(':')
|
|
23
|
-
_ext_enc, int_enc = ext_enc.split(':', 2)
|
|
24
|
-
end
|
|
22
|
+
_ext_enc, int_enc = ext_enc.split(':', 2) if ext_enc.kind_of?(String) && ext_enc.include?(':')
|
|
25
23
|
|
|
26
24
|
begin
|
|
27
25
|
@internal_encoding = Encoding.find(int_enc) if int_enc
|
data/lib/ziperu/ioextras.rb
CHANGED
|
@@ -38,6 +38,12 @@ module Ziperu
|
|
|
38
38
|
obj.respond_to?(:to_path) ? obj.to_path : obj.to_s
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
# Whether the native core can take `str` as a name or path: it wants
|
|
42
|
+
# valid UTF-8.
|
|
43
|
+
def native_text?(str)
|
|
44
|
+
str.ascii_only? || (str.encoding == ::Encoding::UTF_8 && str.valid_encoding?)
|
|
45
|
+
end
|
|
46
|
+
|
|
41
47
|
# Puts an IO in binary mode when it has one (a StringIO does not).
|
|
42
48
|
def binmode(io)
|
|
43
49
|
io.binmode if io.respond_to?(:binmode)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ziperu
|
|
4
|
+
# Ready-made sets of the limit settings (`max_entries`,
|
|
5
|
+
# `max_total_size`, `max_entry_size`, `max_compression_ratio`,
|
|
6
|
+
# `max_archive_size`), as keywords for one archive or for every one:
|
|
7
|
+
#
|
|
8
|
+
# Ziperu::File.open(upload, **Ziperu::Limits::UPLOAD) { |zf| ... }
|
|
9
|
+
# Ziperu::Limits.apply(Ziperu::Limits::UPLOAD) # the Ziperu.* globals
|
|
10
|
+
#
|
|
11
|
+
# Every limit is checked before an entry is read, and raises
|
|
12
|
+
# Ziperu::LimitError. A profile is a frozen Hash: merge it to change one
|
|
13
|
+
# value (`Ziperu::Limits::UPLOAD.merge(max_entries: 50_000)`).
|
|
14
|
+
# Not in rubyzip.
|
|
15
|
+
module Limits
|
|
16
|
+
# For archives users upload: room for real projects, spreadsheets and
|
|
17
|
+
# photo sets, not for a zip bomb (which runs to a ratio of a million).
|
|
18
|
+
UPLOAD = {
|
|
19
|
+
max_entries: 10_000,
|
|
20
|
+
max_total_size: 2 << 30, # 2 GiB uncompressed in all
|
|
21
|
+
max_entry_size: 1 << 30, # 1 GiB for any one entry
|
|
22
|
+
max_compression_ratio: 1_000,
|
|
23
|
+
max_archive_size: 2 << 30 # 2 GiB read from a pipe or IO
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
# For small documents from anyone (an office file, a config bundle).
|
|
27
|
+
STRICT = {
|
|
28
|
+
max_entries: 1_000,
|
|
29
|
+
max_total_size: 256 << 20, # 256 MiB
|
|
30
|
+
max_entry_size: 64 << 20, # 64 MiB
|
|
31
|
+
max_compression_ratio: 100,
|
|
32
|
+
max_archive_size: 256 << 20
|
|
33
|
+
}.freeze
|
|
34
|
+
|
|
35
|
+
# No limits: the default.
|
|
36
|
+
NONE = UPLOAD.transform_values { nil }.freeze
|
|
37
|
+
|
|
38
|
+
# Sets the `Ziperu.*` limit globals from `profile` (one of the above,
|
|
39
|
+
# or a Hash with any of their keys; a key left out is left alone).
|
|
40
|
+
def self.apply(profile)
|
|
41
|
+
unknown = profile.keys - NONE.keys
|
|
42
|
+
raise ArgumentError, "not a limit: #{unknown.join(', ')}" unless unknown.empty?
|
|
43
|
+
|
|
44
|
+
profile.each { |name, value| ::Ziperu.public_send(:"#{name}=", value) }
|
|
45
|
+
profile
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|