ziperu 0.2.3-x86_64-linux-gnu → 0.3.1-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 +190 -0
- data/README.md +35 -7
- 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 +41 -6
- data/lib/ziperu/entry.rb +252 -112
- 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 +285 -0
- data/lib/ziperu/file.rb +77 -405
- 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 +30 -32
- 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 +118 -31
- 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/verify_report.rb +62 -0
- data/lib/ziperu/version.rb +2 -2
- data/lib/ziperu.rb +19 -17
- metadata +7 -2
|
@@ -34,9 +34,7 @@ module Ziperu
|
|
|
34
34
|
alias getwd pwd
|
|
35
35
|
|
|
36
36
|
def chdir(directory_name)
|
|
37
|
-
unless @file.stat(directory_name).directory?
|
|
38
|
-
raise Errno::EINVAL, "Invalid argument - #{directory_name}"
|
|
39
|
-
end
|
|
37
|
+
raise Errno::EINVAL, "Invalid argument - #{directory_name}" unless @file.stat(directory_name).directory?
|
|
40
38
|
|
|
41
39
|
@mapped_zip.pwd = @file.expand_path(directory_name)
|
|
42
40
|
end
|
|
@@ -52,9 +50,7 @@ module Ziperu
|
|
|
52
50
|
end
|
|
53
51
|
|
|
54
52
|
def foreach(directory_name)
|
|
55
|
-
unless @file.stat(directory_name).directory?
|
|
56
|
-
raise Errno::ENOTDIR, directory_name
|
|
57
|
-
end
|
|
53
|
+
raise Errno::ENOTDIR, directory_name unless @file.stat(directory_name).directory?
|
|
58
54
|
|
|
59
55
|
path = @file.expand_path(directory_name)
|
|
60
56
|
path << '/' unless path.end_with?('/')
|
|
@@ -67,9 +63,7 @@ module Ziperu
|
|
|
67
63
|
end
|
|
68
64
|
|
|
69
65
|
def delete(entry_name)
|
|
70
|
-
unless @file.stat(entry_name).directory?
|
|
71
|
-
raise Errno::EINVAL, "Invalid argument - #{entry_name}"
|
|
72
|
-
end
|
|
66
|
+
raise Errno::EINVAL, "Invalid argument - #{entry_name}" unless @file.stat(entry_name).directory?
|
|
73
67
|
|
|
74
68
|
@mapped_zip.remove(entry_name)
|
|
75
69
|
end
|
|
@@ -20,9 +20,7 @@ module Ziperu
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def find_entry(filename)
|
|
23
|
-
unless exists?(filename)
|
|
24
|
-
raise Errno::ENOENT, "No such file or directory - #{filename}"
|
|
25
|
-
end
|
|
23
|
+
raise Errno::ENOENT, "No such file or directory - #{filename}" unless exists?(filename)
|
|
26
24
|
|
|
27
25
|
@mapped_zip.find_entry(filename)
|
|
28
26
|
end
|
|
@@ -243,9 +241,7 @@ module Ziperu
|
|
|
243
241
|
|
|
244
242
|
def delete(*args)
|
|
245
243
|
args.each do |filename|
|
|
246
|
-
if directory?(filename)
|
|
247
|
-
raise Errno::EISDIR, "Is a directory - \"#{filename}\""
|
|
248
|
-
end
|
|
244
|
+
raise Errno::EISDIR, "Is a directory - \"#{filename}\"" if directory?(filename)
|
|
249
245
|
|
|
250
246
|
@mapped_zip.remove(filename)
|
|
251
247
|
end
|
|
@@ -68,8 +68,8 @@ module Ziperu
|
|
|
68
68
|
|
|
69
69
|
def expand_path(path)
|
|
70
70
|
expanded = path.start_with?('/') ? path.dup : ::File.join(@pwd, path)
|
|
71
|
-
expanded.gsub!(
|
|
72
|
-
expanded.gsub!(
|
|
71
|
+
expanded.gsub!(%r{/\.(/|$)}, '')
|
|
72
|
+
expanded.gsub!(%r{[^/]+/\.\.(/|$)}, '')
|
|
73
73
|
expanded.empty? ? '/' : expanded
|
|
74
74
|
end
|
|
75
75
|
|
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)
|
|
@@ -44,6 +44,7 @@ module Ziperu
|
|
|
44
44
|
enforce_size: nil, metadata_encoding: nil, streaming: false, **opts)
|
|
45
45
|
@settings = Settings.take!(opts)
|
|
46
46
|
enforce_size = @settings.validate_entry_sizes if enforce_size.nil?
|
|
47
|
+
Entry.check_metadata_encoding!(metadata_encoding)
|
|
47
48
|
@metadata_encoding = metadata_encoding
|
|
48
49
|
# rubyzip 2.x's `new(io, offset, decrypter)`.
|
|
49
50
|
offset, decrypter = Compat.positional(legacy, %i[offset decrypter],
|
|
@@ -53,25 +54,25 @@ module Ziperu
|
|
|
53
54
|
# AES from ZipCrypto by the entry, so one password serves both.
|
|
54
55
|
decrypter ||= TraditionalDecrypter.new(password) if password
|
|
55
56
|
super(**opts)
|
|
57
|
+
@decrypter = decrypter
|
|
58
|
+
@enforce_size = enforce_size
|
|
59
|
+
@reader = nil
|
|
60
|
+
@current_entry = nil
|
|
56
61
|
if streaming && self.class.pipe_like?(context)
|
|
57
62
|
IOExtras.binmode(context)
|
|
58
63
|
@stream = Native::StreamReader.open(context, decrypter&.password&.b, enforce_size)
|
|
59
64
|
@stream_entries = 0
|
|
60
65
|
@stream_total = 0
|
|
61
|
-
@decrypter = decrypter
|
|
62
|
-
@enforce_size = enforce_size
|
|
63
66
|
@owns_archive = false
|
|
64
67
|
@archive = nil
|
|
65
|
-
@reader = nil
|
|
66
|
-
@current_entry = nil
|
|
67
68
|
return
|
|
68
69
|
end
|
|
69
|
-
@split = !context.kind_of?(Native::Archive) && self.class.split_archive?(context)
|
|
70
70
|
# An archive handed in (by Entry#get_input_stream) stays open for its
|
|
71
71
|
# owner; one opened here is closed with the stream. A split segment
|
|
72
72
|
# has no central directory to open; rubyzip fails on the first entry,
|
|
73
73
|
# so defer the error to `get_next_entry`.
|
|
74
74
|
@owns_archive = !context.kind_of?(Native::Archive)
|
|
75
|
+
@split = @owns_archive && self.class.split_archive?(context)
|
|
75
76
|
@archive =
|
|
76
77
|
if !@owns_archive
|
|
77
78
|
context
|
|
@@ -91,26 +92,21 @@ module Ziperu
|
|
|
91
92
|
nil
|
|
92
93
|
end
|
|
93
94
|
end
|
|
94
|
-
@decrypter = decrypter
|
|
95
|
-
@enforce_size = enforce_size
|
|
96
95
|
# An archive with several records under one name is walked from its
|
|
97
96
|
# listing, since the native core addresses only the last of each name
|
|
98
97
|
# by index; every record is yielded, as rubyzip's walk of the local
|
|
99
98
|
# headers would.
|
|
100
99
|
@records = @archive.entries if @archive&.duplicate_names?
|
|
101
100
|
@next_index = offset.zero? || @archive.nil? ? 0 : index_at_offset(offset)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
@current_entry = nil
|
|
101
|
+
return unless @next_index.nil?
|
|
102
|
+
|
|
103
|
+
@archive.close
|
|
104
|
+
raise Error, "no entry starts at offset #{offset} (Zip local header magic not found)"
|
|
107
105
|
end
|
|
108
106
|
|
|
109
107
|
# Close this InputStream. All further IO will raise an IOError.
|
|
110
108
|
def close
|
|
111
|
-
|
|
112
|
-
@reader = nil
|
|
113
|
-
@lazy_open = false
|
|
109
|
+
close_reader
|
|
114
110
|
@stream&.close
|
|
115
111
|
@stream = nil
|
|
116
112
|
@archive&.close if @owns_archive
|
|
@@ -122,16 +118,15 @@ module Ziperu
|
|
|
122
118
|
# the entry data. Returns nil when there are no more entries.
|
|
123
119
|
def get_next_entry
|
|
124
120
|
raise SplitArchiveError if @split
|
|
121
|
+
|
|
125
122
|
check_open!
|
|
126
123
|
return next_stream_entry if @stream
|
|
127
|
-
|
|
128
124
|
return @current_entry = nil if @archive.nil? || @next_index.nil?
|
|
125
|
+
|
|
129
126
|
if @next_index >= (@records ? @records.size : @archive.entry_count)
|
|
130
127
|
# Past the last entry there is nothing to read, not the previous
|
|
131
128
|
# entry again.
|
|
132
|
-
|
|
133
|
-
@reader = nil
|
|
134
|
-
@lazy_open = false
|
|
129
|
+
close_reader
|
|
135
130
|
flush
|
|
136
131
|
return @current_entry = nil
|
|
137
132
|
end
|
|
@@ -175,7 +170,7 @@ module Ziperu
|
|
|
175
170
|
@next_index =
|
|
176
171
|
if @records
|
|
177
172
|
at = @records.index { |meta| meta[:header_offset] == entry.local_header_offset }
|
|
178
|
-
at && at + 1
|
|
173
|
+
at && (at + 1)
|
|
179
174
|
else
|
|
180
175
|
entry.index + 1
|
|
181
176
|
end
|
|
@@ -261,7 +256,7 @@ module Ziperu
|
|
|
261
256
|
begin
|
|
262
257
|
yield zio
|
|
263
258
|
ensure
|
|
264
|
-
zio.close
|
|
259
|
+
zio.close
|
|
265
260
|
end
|
|
266
261
|
end
|
|
267
262
|
|
|
@@ -320,7 +315,7 @@ module Ziperu
|
|
|
320
315
|
context.seek(0, ::IO::SEEK_SET)
|
|
321
316
|
context.read(4)
|
|
322
317
|
end
|
|
323
|
-
head.to_s.b.start_with?([LOCAL_ENTRY_SIGNATURE].pack(
|
|
318
|
+
head.to_s.b.start_with?([LOCAL_ENTRY_SIGNATURE].pack('V'))
|
|
324
319
|
rescue SystemCallError, IOError
|
|
325
320
|
false
|
|
326
321
|
end
|
|
@@ -364,7 +359,7 @@ module Ziperu
|
|
|
364
359
|
return io.read.to_s.b unless limit
|
|
365
360
|
|
|
366
361
|
bytes = +''.b
|
|
367
|
-
while (chunk = io.read(
|
|
362
|
+
while (chunk = io.read(CHUNK_SIZE))
|
|
368
363
|
bytes << chunk
|
|
369
364
|
raise LimitError.new('an archive size', bytes.bytesize, limit) if bytes.bytesize > limit
|
|
370
365
|
end
|
|
@@ -402,12 +397,9 @@ module Ziperu
|
|
|
402
397
|
raise CompressionMethodError, entry.compression_method
|
|
403
398
|
end
|
|
404
399
|
|
|
405
|
-
if entry.encrypted? && @decrypter.nil?
|
|
406
|
-
raise Error, PASSWORD_REQUIRED_MESSAGE
|
|
407
|
-
end
|
|
400
|
+
raise Error, PASSWORD_REQUIRED_MESSAGE if entry.encrypted? && @decrypter.nil?
|
|
408
401
|
|
|
409
|
-
|
|
410
|
-
@reader = nil
|
|
402
|
+
close_reader
|
|
411
403
|
@lazy_open = true
|
|
412
404
|
reader unless lazy
|
|
413
405
|
flush
|
|
@@ -451,6 +443,12 @@ module Ziperu
|
|
|
451
443
|
|
|
452
444
|
private
|
|
453
445
|
|
|
446
|
+
def close_reader
|
|
447
|
+
@reader&.close
|
|
448
|
+
@reader = nil
|
|
449
|
+
@lazy_open = false
|
|
450
|
+
end
|
|
451
|
+
|
|
454
452
|
# Nil when no entry starts there, which reads as "no more entries".
|
|
455
453
|
def index_at_offset(offset)
|
|
456
454
|
return @records.index { |meta| meta[:header_offset] == offset } if @records
|
|
@@ -477,6 +475,7 @@ module Ziperu
|
|
|
477
475
|
if (limit = @settings.max_entries) && @stream_entries > limit
|
|
478
476
|
raise LimitError.new('an entry count', @stream_entries, limit)
|
|
479
477
|
end
|
|
478
|
+
|
|
480
479
|
if meta[:size]
|
|
481
480
|
check_stream_entry_size(meta[:size])
|
|
482
481
|
if (limit = @settings.max_compression_ratio) && meta[:size].positive?
|
|
@@ -488,7 +487,7 @@ module Ziperu
|
|
|
488
487
|
meta[:crc] ||= 0
|
|
489
488
|
meta[:compressed_size] ||= 0
|
|
490
489
|
@current_entry = Entry.from_meta(nil, meta, metadata_encoding: @metadata_encoding, settings: @settings)
|
|
491
|
-
@current_entry.
|
|
490
|
+
@current_entry.learn_data(nil, 0) if meta[:size].nil?
|
|
492
491
|
@stream_entry_bytes = 0
|
|
493
492
|
@reader = @stream
|
|
494
493
|
@lineno = 0
|
|
@@ -534,8 +533,7 @@ module Ziperu
|
|
|
534
533
|
crc, size = @stream.trailer
|
|
535
534
|
return unless crc && !@current_entry.size_known?
|
|
536
535
|
|
|
537
|
-
@current_entry.
|
|
538
|
-
@current_entry.instance_variable_set(:@crc, crc)
|
|
536
|
+
@current_entry.learn_data(size, crc)
|
|
539
537
|
end
|
|
540
538
|
|
|
541
539
|
# 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
|