ziperu 0.2.3-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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +151 -0
  3. data/README.md +35 -7
  4. data/lib/zip.rb +3 -3
  5. data/lib/ziperu/3.1/ziperu.so +0 -0
  6. data/lib/ziperu/3.2/ziperu.so +0 -0
  7. data/lib/ziperu/3.3/ziperu.so +0 -0
  8. data/lib/ziperu/3.4/ziperu.so +0 -0
  9. data/lib/ziperu/4.0/ziperu.so +0 -0
  10. data/lib/ziperu/central_directory.rb +4 -8
  11. data/lib/ziperu/constants.rb +8 -0
  12. data/lib/ziperu/crypto.rb +10 -2
  13. data/lib/ziperu/dos_time.rb +1 -1
  14. data/lib/ziperu/entry.rb +226 -109
  15. data/lib/ziperu/entry_set.rb +0 -5
  16. data/lib/ziperu/errors.rb +4 -2
  17. data/lib/ziperu/extra_field/zip64.rb +1 -3
  18. data/lib/ziperu/extra_field.rb +10 -10
  19. data/lib/ziperu/file/commit.rb +255 -0
  20. data/lib/ziperu/file/extraction.rb +280 -0
  21. data/lib/ziperu/file.rb +51 -398
  22. data/lib/ziperu/file_split.rb +1 -3
  23. data/lib/ziperu/filesystem/dir.rb +3 -9
  24. data/lib/ziperu/filesystem/file.rb +2 -6
  25. data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
  26. data/lib/ziperu/input_stream.rb +28 -32
  27. data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
  28. data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
  29. data/lib/ziperu/ioextras/fake_io.rb +1 -3
  30. data/lib/ziperu/ioextras.rb +6 -0
  31. data/lib/ziperu/limits.rb +48 -0
  32. data/lib/ziperu/null_input_stream.rb +1 -1
  33. data/lib/ziperu/output_stream.rb +118 -31
  34. data/lib/ziperu/pass_thru_decompressor.rb +1 -3
  35. data/lib/ziperu/rails_streaming.rb +5 -5
  36. data/lib/ziperu/remote_io.rb +181 -32
  37. data/lib/ziperu/size_estimator.rb +27 -19
  38. data/lib/ziperu/verify_report.rb +62 -0
  39. data/lib/ziperu/version.rb +2 -2
  40. data/lib/ziperu.rb +19 -17
  41. metadata +7 -2
@@ -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 `Ziperu::InputStream` does:
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,26 +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
- if @next_index.nil?
103
- raise Error, "no entry starts at offset #{offset} (Zip local header magic not found)"
104
- end
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
- @reader&.close
112
- @reader = nil
113
- @lazy_open = false
107
+ close_reader
114
108
  @stream&.close
115
109
  @stream = nil
116
110
  @archive&.close if @owns_archive
@@ -122,16 +116,15 @@ module Ziperu
122
116
  # the entry data. Returns nil when there are no more entries.
123
117
  def get_next_entry
124
118
  raise SplitArchiveError if @split
119
+
125
120
  check_open!
126
121
  return next_stream_entry if @stream
127
-
128
122
  return @current_entry = nil if @archive.nil? || @next_index.nil?
123
+
129
124
  if @next_index >= (@records ? @records.size : @archive.entry_count)
130
125
  # Past the last entry there is nothing to read, not the previous
131
126
  # entry again.
132
- @reader&.close
133
- @reader = nil
134
- @lazy_open = false
127
+ close_reader
135
128
  flush
136
129
  return @current_entry = nil
137
130
  end
@@ -175,7 +168,7 @@ module Ziperu
175
168
  @next_index =
176
169
  if @records
177
170
  at = @records.index { |meta| meta[:header_offset] == entry.local_header_offset }
178
- at && at + 1
171
+ at && (at + 1)
179
172
  else
180
173
  entry.index + 1
181
174
  end
@@ -261,7 +254,7 @@ module Ziperu
261
254
  begin
262
255
  yield zio
263
256
  ensure
264
- zio.close if zio
257
+ zio.close
265
258
  end
266
259
  end
267
260
 
@@ -320,7 +313,7 @@ module Ziperu
320
313
  context.seek(0, ::IO::SEEK_SET)
321
314
  context.read(4)
322
315
  end
323
- head.to_s.b.start_with?([LOCAL_ENTRY_SIGNATURE].pack("V"))
316
+ head.to_s.b.start_with?([LOCAL_ENTRY_SIGNATURE].pack('V'))
324
317
  rescue SystemCallError, IOError
325
318
  false
326
319
  end
@@ -364,7 +357,7 @@ module Ziperu
364
357
  return io.read.to_s.b unless limit
365
358
 
366
359
  bytes = +''.b
367
- while (chunk = io.read(IOExtras::CHUNK_SIZE))
360
+ while (chunk = io.read(CHUNK_SIZE))
368
361
  bytes << chunk
369
362
  raise LimitError.new('an archive size', bytes.bytesize, limit) if bytes.bytesize > limit
370
363
  end
@@ -402,12 +395,9 @@ module Ziperu
402
395
  raise CompressionMethodError, entry.compression_method
403
396
  end
404
397
 
405
- if entry.encrypted? && @decrypter.nil?
406
- raise Error, PASSWORD_REQUIRED_MESSAGE
407
- end
398
+ raise Error, PASSWORD_REQUIRED_MESSAGE if entry.encrypted? && @decrypter.nil?
408
399
 
409
- @reader&.close
410
- @reader = nil
400
+ close_reader
411
401
  @lazy_open = true
412
402
  reader unless lazy
413
403
  flush
@@ -451,6 +441,12 @@ module Ziperu
451
441
 
452
442
  private
453
443
 
444
+ def close_reader
445
+ @reader&.close
446
+ @reader = nil
447
+ @lazy_open = false
448
+ end
449
+
454
450
  # Nil when no entry starts there, which reads as "no more entries".
455
451
  def index_at_offset(offset)
456
452
  return @records.index { |meta| meta[:header_offset] == offset } if @records
@@ -477,6 +473,7 @@ module Ziperu
477
473
  if (limit = @settings.max_entries) && @stream_entries > limit
478
474
  raise LimitError.new('an entry count', @stream_entries, limit)
479
475
  end
476
+
480
477
  if meta[:size]
481
478
  check_stream_entry_size(meta[:size])
482
479
  if (limit = @settings.max_compression_ratio) && meta[:size].positive?
@@ -488,7 +485,7 @@ module Ziperu
488
485
  meta[:crc] ||= 0
489
486
  meta[:compressed_size] ||= 0
490
487
  @current_entry = Entry.from_meta(nil, meta, metadata_encoding: @metadata_encoding, settings: @settings)
491
- @current_entry.instance_variable_set(:@size, nil) if meta[:size].nil?
488
+ @current_entry.learn_data(nil, 0) if meta[:size].nil?
492
489
  @stream_entry_bytes = 0
493
490
  @reader = @stream
494
491
  @lineno = 0
@@ -534,8 +531,7 @@ module Ziperu
534
531
  crc, size = @stream.trailer
535
532
  return unless crc && !@current_entry.size_known?
536
533
 
537
- @current_entry.instance_variable_set(:@size, size)
538
- @current_entry.instance_variable_set(:@crc, crc)
534
+ @current_entry.learn_data(size, crc)
539
535
  end
540
536
 
541
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) # rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
37
+ def read(maxlen = nil, out_string = nil)
27
38
  return (maxlen.nil? || maxlen.zero? ? '' : nil) if eof?
28
39
 
29
- tbuf = if @output_buffer.bytesize > 0
30
- if maxlen && maxlen <= @output_buffer.bytesize
31
- @output_buffer.slice!(0, maxlen)
40
+ available = buffered_size
41
+ tbuf = if available > 0
42
+ if maxlen && maxlen <= available
43
+ take_buffered(maxlen)
32
44
  else
33
- maxlen -= @output_buffer.bytesize if maxlen
45
+ maxlen -= available if maxlen
34
46
  rbuf = produce_input(maxlen)
35
- out = @output_buffer
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) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
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
- if sep.respond_to?(:to_int)
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
- buffer_index = 0
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 && @output_buffer.bytesize >= 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 ||= @output_buffer.bytesize
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 = @output_buffer.slice!(0, cut_index)
105
- data&.chomp!(sep) if chomp
106
- data&.force_encoding(encoding)
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
- @output_buffer.slice!(0..)
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
- @output_buffer.empty? && input_finished?
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
@@ -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
@@ -26,7 +26,7 @@ module Ziperu
26
26
  end
27
27
 
28
28
  def each(*_args, **_opts)
29
- return to_enum(:each) unless block_given?
29
+ to_enum(:each) unless block_given? # rubocop:disable Lint/ToEnumArguments -- nothing to enumerate
30
30
  end
31
31
  alias each_line each
32
32