ziperu 0.2.1 → 0.2.3
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 +119 -0
- data/Cargo.lock +1 -1
- data/README.md +78 -35
- data/ext/ziperu/Cargo.toml +1 -1
- data/ext/ziperu/src/archive.rs +98 -55
- data/ext/ziperu/src/errors.rs +79 -14
- data/ext/ziperu/src/extract.rs +92 -33
- data/ext/ziperu/src/gvl.rs +86 -15
- data/ext/ziperu/src/lib.rs +13 -9
- data/ext/ziperu/src/rb.rs +114 -0
- data/ext/ziperu/src/reader.rs +279 -18
- data/ext/ziperu/src/sink.rs +336 -20
- data/ext/ziperu/src/source.rs +2 -2
- data/ext/ziperu/src/stream.rs +541 -0
- data/ext/ziperu/src/writer.rs +189 -31
- data/lib/ziperu/entry/source.rb +7 -1
- data/lib/ziperu/entry.rb +66 -8
- data/lib/ziperu/errors.rb +5 -0
- data/lib/ziperu/file.rb +64 -6
- data/lib/ziperu/input_stream.rb +189 -17
- data/lib/ziperu/output_stream.rb +82 -1
- data/lib/ziperu/remote_io.rb +278 -0
- data/lib/ziperu/replace_rubyzip.rb +67 -0
- data/lib/ziperu/streamable_stream.rb +110 -10
- data/lib/ziperu/version.rb +1 -1
- data/lib/ziperu.rb +4 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b5e977083967283ed8594aa131fccbbea2b8b766458c6e6fa8a3006356af57e1
|
|
4
|
+
data.tar.gz: 62d85e136fc36d7618c8b58aa2fb8ed910f403a994c9a6f1e06a1e812cd13310
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d57553d22be136d860dda97bade7f0a8bffe3ea28ab0f0befc2b7d2c14ef674dc219522bd02884bb62e556f5c77f93374ed3a25dd7cb2af8b54136616a2f96e1
|
|
7
|
+
data.tar.gz: 293a8363e37ac330bdf5bfef408d7b572f35d06254188a7e6795ce5da1cf64a01f18b45cc5434cf9a9c524b91d249599c3220fe67adbe06afafd80939cca6763
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,124 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.3 (2026-11-11)
|
|
4
|
+
|
|
5
|
+
Faster (Windows, against 0.2.2):
|
|
6
|
+
|
|
7
|
+
- Writing many small entries to a file: the header each entry patches
|
|
8
|
+
at its end is now still in the write buffer, so it costs no system
|
|
9
|
+
call. 1000 entries of 8 KB: 46 to 32 ms; 10,000 tiny entries: 315 to
|
|
10
|
+
180 ms. A seekable Ruby IO (`write_buffer`) gets the same, in 64 KB
|
|
11
|
+
chunks.
|
|
12
|
+
- `File#get_output_stream` keeps an entry in memory until the commit
|
|
13
|
+
rather than opening a tempfile for each one; past 8 MB held between
|
|
14
|
+
an archive's pending entries, the rest go to tempfiles as before.
|
|
15
|
+
1000 entries and a commit: 981 to 58 ms.
|
|
16
|
+
- `Entry#time` reads the modification time from the central directory,
|
|
17
|
+
as rubyzip does, rather than the entry's local header; the access and
|
|
18
|
+
creation times still come from there. Listing names and times of
|
|
19
|
+
10,000 entries: 153 to 78 ms.
|
|
20
|
+
- Appending to a large archive no longer reads every entry's local
|
|
21
|
+
header, reads the central directory through a buffer, and keeps the
|
|
22
|
+
entries it had after the commit (same objects) rather than building
|
|
23
|
+
them all again. One entry added to a 10,000-entry archive: 337 to
|
|
24
|
+
98 ms.
|
|
25
|
+
- `InputStream#get_next_entry` opens the entry's data when it is first
|
|
26
|
+
read, so walking only the names reads no local header: 103 to 40 ms
|
|
27
|
+
for 10,000 entries.
|
|
28
|
+
- Reading a whole entry of 64 KB or more (`File#read`, and
|
|
29
|
+
`get_input_stream`'s `read` of the rest of one, which is what roo and
|
|
30
|
+
caxlsx do) decompresses straight into the returned String: no second
|
|
31
|
+
copy, and half the peak memory. 64 MB: 58 to 17 ms; a 64 MB Stored
|
|
32
|
+
entry: 62 to 18 ms. An interrupted read rescued still loses nothing.
|
|
33
|
+
- Files added by path (`add`, `add_stored`, `replace`) are read by the
|
|
34
|
+
native core at commit instead of through Ruby: a 64 MB `add_stored`
|
|
35
|
+
and commit, 141 to 34 ms.
|
|
36
|
+
|
|
37
|
+
Fixed:
|
|
38
|
+
|
|
39
|
+
- Native work could not be interrupted, and kept every other thread
|
|
40
|
+
waiting, whenever Ruby declined to release the GVL for it because an
|
|
41
|
+
interrupt flag was set (often just its thread-scheduling timer): it
|
|
42
|
+
then ran to the end with the GVL held. Now a pending `Thread#raise`,
|
|
43
|
+
`Timeout` or Ctrl-C stops it at once, and otherwise the GVL is
|
|
44
|
+
released on another try.
|
|
45
|
+
- `get_input_stream.read` of an entry whose central record declares it
|
|
46
|
+
empty but gives a CRC of some data returned `""`; it raises, as
|
|
47
|
+
`File#read` does.
|
|
48
|
+
|
|
49
|
+
## 0.2.2 (2026-11-11)
|
|
50
|
+
|
|
51
|
+
Fixed:
|
|
52
|
+
|
|
53
|
+
- Duplicate entry names no longer get around the open-time limits. The
|
|
54
|
+
checks for `max_entries`, `max_total_size`, `max_entry_size` and
|
|
55
|
+
`max_compression_ratio` counted only the last record under each name,
|
|
56
|
+
while listing, `InputStream` and `extract_all` reach every one: a
|
|
57
|
+
zip bomb behind a harmless entry of the same name went through. Every
|
|
58
|
+
record now counts, in one pass.
|
|
59
|
+
- A large `read` interrupted (`Timeout`, `Thread#raise`) and then
|
|
60
|
+
rescued no longer loses data: what it had decompressed is returned by
|
|
61
|
+
the next read. After any other failure part-way through an entry,
|
|
62
|
+
further reads raise until `rewind`, instead of returning a short entry
|
|
63
|
+
with no error.
|
|
64
|
+
- `OutputStream.new(io, stream: true)` whose IO raises, with the error
|
|
65
|
+
rescued and writing carried on: the writer refuses every later call
|
|
66
|
+
rather than lose track of which entries the archive holds (which gave
|
|
67
|
+
a `NoMethodError` on close, or an entry silently overwritten), and no
|
|
68
|
+
longer writes again what it had handed the IO before it raised.
|
|
69
|
+
- `File#add`, `add_stored` and `replace` take a `Pathname` as the path it
|
|
70
|
+
is, not an IO (`Rails.root.join(...)` raised `NoMethodError`).
|
|
71
|
+
- On Windows, `File.open(dir, create: true)` and a commit no longer move
|
|
72
|
+
a directory at that path aside to put the archive in its place; the
|
|
73
|
+
commit raises, as rubyzip's does.
|
|
74
|
+
- Adding to an archive that is read-only (or has other hard links)
|
|
75
|
+
rewrites it instead of failing to append in place.
|
|
76
|
+
- Errors that came out as a bare `IOError` are `Ziperu::Error`s: corrupt
|
|
77
|
+
zstd and LZMA data (`DecompressionError`, as for Deflate), a local
|
|
78
|
+
header past the end of the archive or with a bad signature, a forged
|
|
79
|
+
zip64 offset no file can seek to. A NUL in a name raises
|
|
80
|
+
`ArgumentError` from the native `extract_all`, as from Ruby's own file
|
|
81
|
+
calls.
|
|
82
|
+
- `add_files(threads:)` (and the parallel commit) can be interrupted: it
|
|
83
|
+
stopped only once the whole batch, up to 256 MB, was compressed. It
|
|
84
|
+
still adds the batch whole or not at all.
|
|
85
|
+
- A local header that names a different compression method from the
|
|
86
|
+
central directory is refused, rather than decoded one way or the other
|
|
87
|
+
depending on `validate_entry_sizes`.
|
|
88
|
+
- An interrupted `extract_all` removes the file it was writing, so a
|
|
89
|
+
second run is not stopped by `DestinationExistsError`.
|
|
90
|
+
- Directory times restored by `extract_all` are set once the files in
|
|
91
|
+
the directory are written, which would otherwise move them (and every
|
|
92
|
+
directory entry is made before any file, wherever it is listed).
|
|
93
|
+
- An entry kept across a commit that moved it (the one `remove` returns,
|
|
94
|
+
say) still answers `time`, `extra` and the like from what it holds.
|
|
95
|
+
- `InputStream.open("missing.zip")` names the file in its `ENOENT`;
|
|
96
|
+
`File.open(nil)` raises `TypeError`.
|
|
97
|
+
|
|
98
|
+
Added:
|
|
99
|
+
|
|
100
|
+
- `require "ziperu/replace_rubyzip"`: ziperu answers for rubyzip
|
|
101
|
+
(`require "zip"`, `"rubyzip"`, `"zip/..."`) even without Bundler, where
|
|
102
|
+
RubyGems would otherwise load a rubyzip that a dependency asks for.
|
|
103
|
+
- `InputStream.open(pipe, streaming: true)`: a pipe or socket read in one
|
|
104
|
+
pass, local header by local header, in an entry's worth of memory, with
|
|
105
|
+
the limits counted as the bytes come. Entries whose sizes follow their
|
|
106
|
+
data (how streaming writers write) are read when Deflated.
|
|
107
|
+
- `Ziperu::RemoteIO`: a seekable IO over HTTP Range requests (or a block
|
|
108
|
+
that fetches byte ranges), so an archive on S3 or a web server can be
|
|
109
|
+
listed and have entries read without downloading it.
|
|
110
|
+
- `OutputStream#rollback_entry`: takes the entry being written back out
|
|
111
|
+
of the archive, so one whose data could not be had can be skipped. On
|
|
112
|
+
a stream it works until the entry's first bytes are written (the entry
|
|
113
|
+
is begun only then); on a file or seekable IO, at any point.
|
|
114
|
+
- `InputStream#seek` and `pos=` in a Stored entry that is not encrypted
|
|
115
|
+
(`get_input_stream(...).seek`): random access without reading what
|
|
116
|
+
comes before.
|
|
117
|
+
|
|
118
|
+
Changed:
|
|
119
|
+
|
|
120
|
+
- `open_buffer` on a String copies the archive once, not twice.
|
|
121
|
+
|
|
3
122
|
## 0.2.1 (2026-11-10)
|
|
4
123
|
|
|
5
124
|
Fixed:
|
data/Cargo.lock
CHANGED
data/README.md
CHANGED
|
@@ -19,6 +19,19 @@ the [`zip`](https://github.com/zip-rs/zip2) crate through
|
|
|
19
19
|
[Magnus](https://github.com/matsadler/magnus). `require "zip"` aliases
|
|
20
20
|
`Zip = Ziperu`, so existing code runs unchanged.
|
|
21
21
|
|
|
22
|
+
On GitHub's Linux runners (Ruby 3.4, median of 5), against rubyzip 3.7.0:
|
|
23
|
+
|
|
24
|
+
| scenario | rubyzip | ziperu | speedup |
|
|
25
|
+
|---|---:|---:|---:|
|
|
26
|
+
| read 1000 x 8 KB deflated entries | 80 ms | 14 ms | 5.8x |
|
|
27
|
+
| open and list 10000 entries | 135 ms | 58 ms | 2.3x |
|
|
28
|
+
| deflate one 64 MB entry | 188 ms | 77 ms | 2.4x |
|
|
29
|
+
| extract_all 1000 entries | 169 ms | 80 ms | 2.1x |
|
|
30
|
+
| add 1000 files from disk (`threads: 4`) | 214 ms | 60 ms | 3.6x |
|
|
31
|
+
|
|
32
|
+
macOS and Windows, every scenario, and which calls release the GVL (what
|
|
33
|
+
Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
|
|
34
|
+
|
|
22
35
|
- **Precompiled** for Linux (glibc and musl), macOS and Windows, Ruby 3.1 to 4.0. No toolchain, no runtime dependencies.
|
|
23
36
|
- **Fast**: entries inflate and deflate in Rust with the GVL released, the central directory is parsed once, unchanged entries are copied byte for byte on commit, and a commit that only adds entries appends them in place.
|
|
24
37
|
- **Reads everything**: Stored, Deflate, Deflate64, bzip2, zstd, LZMA, XZ, PPMd and the PKZIP 1.x methods; AES and ZipCrypto. Writes Deflate, Stored, bzip2, zstd, XZ, PPMd, AES and ZipCrypto.
|
|
@@ -40,25 +53,8 @@ Or on its own:
|
|
|
40
53
|
gem install ziperu
|
|
41
54
|
```
|
|
42
55
|
|
|
43
|
-
|
|
44
|
-
(
|
|
45
|
-
`aarch64-linux-musl`, `arm-linux-musl`, `x86_64-darwin`, `arm64-darwin`,
|
|
46
|
-
`x64-mingw-ucrt`, `aarch64-mingw-ucrt`), each holding the extension for
|
|
47
|
-
Ruby 3.1 through 4.0. The glibc gems need RubyGems 3.3.22 or later (Ruby
|
|
48
|
-
3.1.4 ships it); the musl gems need `libgcc` (`apk add libgcc`, already in
|
|
49
|
-
the `ruby:3.2-alpine` and later images). Anywhere else the source gem
|
|
50
|
-
builds with Rust 1.88+.
|
|
51
|
-
|
|
52
|
-
Which Rubies, and how firmly:
|
|
53
|
-
|
|
54
|
-
| Ruby | Status |
|
|
55
|
-
| --- | --- |
|
|
56
|
-
| 3.3, 3.4, 4.0 | Supported. The suite runs on Linux, macOS and Windows on every change; binaries ship. |
|
|
57
|
-
| 3.1, 3.2 | Best effort. Past end of life upstream; binaries still ship and the suite runs on 3.1, but a fix that needs a newer Ruby may drop them, in a minor release and with a changelog entry. |
|
|
58
|
-
| ruby-head | Watched. A non-blocking CI job runs the suite nightly, so a new Ruby gets binaries the day it is released. |
|
|
59
|
-
|
|
60
|
-
JRuby and TruffleRuby are not supported: the core is a native extension
|
|
61
|
-
against the CRuby API. rubyzip remains the library for those.
|
|
56
|
+
Precompiled gems ship for Linux, macOS and Windows; see
|
|
57
|
+
[Platforms](#platforms) for the details.
|
|
62
58
|
|
|
63
59
|
## Quick start
|
|
64
60
|
|
|
@@ -165,6 +161,51 @@ Ziperu::InputStream.open(socket) do |zis| # any IO, no seeking needed
|
|
|
165
161
|
process(entry.name, zis.read)
|
|
166
162
|
end
|
|
167
163
|
end
|
|
164
|
+
|
|
165
|
+
# A pipe or an upload read in one pass, an entry's worth of memory at a
|
|
166
|
+
# time, rather than taken in whole to reach the central directory:
|
|
167
|
+
Ziperu::InputStream.open(s3_object.body, streaming: true) do |zis|
|
|
168
|
+
while (entry = zis.get_next_entry)
|
|
169
|
+
zis.each_line { |line| ... }
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Read an archive in place on a server, without downloading it: only the
|
|
175
|
+
central directory and the entries read are fetched, with HTTP Range
|
|
176
|
+
requests (S3, GCS, a CDN; or a block that returns byte ranges):
|
|
177
|
+
|
|
178
|
+
```ruby
|
|
179
|
+
io = Ziperu::RemoteIO.new(url) # "https://example.com/dataset.zip"
|
|
180
|
+
Ziperu::File.open_buffer(io, write: false) do |zf|
|
|
181
|
+
zf.extract("docs/guide.txt", "guide.txt") # a few requests, not 5 GB
|
|
182
|
+
end
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Leave out an entry whose data could not be had, and go on with the rest
|
|
186
|
+
(a download that failed half-way through a streamed zip, say):
|
|
187
|
+
|
|
188
|
+
```ruby
|
|
189
|
+
body = Ziperu::OutputStream.body do |zos|
|
|
190
|
+
files.each do |name, url|
|
|
191
|
+
zos.put_next_entry(name)
|
|
192
|
+
begin
|
|
193
|
+
fetch(url) { |chunk| zos << chunk }
|
|
194
|
+
rescue Net::HTTPError
|
|
195
|
+
zos.rollback_entry # before its first byte on a stream; any time on a file
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
[200, { "content-type" => "application/zip" }, body]
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Seek about in a Stored entry, for readers that want random access (a PDF
|
|
203
|
+
stored in an archive):
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
Ziperu::File.open("scans.zip") do |zf|
|
|
207
|
+
zf.get_input_stream("scan.pdf") { |io| io.seek(-1024, IO::SEEK_END); io.read(1024) }
|
|
208
|
+
end
|
|
168
209
|
```
|
|
169
210
|
|
|
170
211
|
Passwords, both ways:
|
|
@@ -209,11 +250,17 @@ path, so `require "zip"` loads ziperu and those gems run on it; rubyzip
|
|
|
209
250
|
stays installed and unused. Checked with Bundler 2.3, 2.5 and 4.0, in
|
|
210
251
|
either Gemfile order.
|
|
211
252
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
253
|
+
Without Bundler, RubyGems loads whichever gem asks for `zip` first. To
|
|
254
|
+
have ziperu answer for rubyzip there too, require this before anything
|
|
255
|
+
else loads it (the top of a script, or `config/boot.rb`):
|
|
256
|
+
|
|
257
|
+
```ruby
|
|
258
|
+
require "ziperu/replace_rubyzip" # every require "zip" / "zip/..." loads ziperu
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
It raises if rubyzip is already loaded, rather than mix the two. Short of
|
|
262
|
+
that, use `require "ziperu"` and `Ziperu::` for your own code and leave
|
|
263
|
+
`Zip::` to rubyzip.
|
|
217
264
|
|
|
218
265
|
ziperu tracks the rubyzip 3.x API (`Ziperu::RUBYZIP_COMPAT_VERSION`), and
|
|
219
266
|
diverges on purpose where rubyzip is lax: `InputStream` reads through the
|
|
@@ -233,18 +280,14 @@ dump, reads entries in large chunks, copies unchanged entries byte for
|
|
|
233
280
|
byte on `commit`, and releases the GVL while inflating from a file or
|
|
234
281
|
String buffer and while deflating to a file, so other Ruby threads keep running.
|
|
235
282
|
|
|
236
|
-
|
|
283
|
+
## Platforms
|
|
237
284
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
| add 1000 files from disk (`threads: 4`) | 214 ms | 60 ms | 3.6x |
|
|
245
|
-
|
|
246
|
-
macOS and Windows, every scenario, and which calls release the GVL (what
|
|
247
|
-
Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
|
|
285
|
+
- **Ruby 3.3 to 4.0** are supported and tested on Linux, macOS and Windows.
|
|
286
|
+
3.1 and 3.2 still get binaries, best effort.
|
|
287
|
+
- **Precompiled** for x86_64 and aarch64 Linux (glibc and musl; musl needs
|
|
288
|
+
`libgcc`), Intel and Apple Silicon macOS, and x64 and arm64 Windows.
|
|
289
|
+
Elsewhere the source gem builds with Rust 1.88+.
|
|
290
|
+
- **CRuby only**: on JRuby or TruffleRuby, use rubyzip.
|
|
248
291
|
|
|
249
292
|
## Development
|
|
250
293
|
|
data/ext/ziperu/Cargo.toml
CHANGED
data/ext/ziperu/src/archive.rs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
use std::cell::{RefCell, RefMut};
|
|
8
8
|
use std::collections::HashMap;
|
|
9
9
|
use std::fs;
|
|
10
|
-
use std::io::{self, Read
|
|
10
|
+
use std::io::{self, Read};
|
|
11
11
|
|
|
12
12
|
use magnus::{
|
|
13
13
|
gc::Marker, typed_data::DataTypeFunctions, value::Opaque, Error, RArray, RHash, RString, Ruby,
|
|
@@ -17,9 +17,9 @@ use zip::read::{ZipFileEntry, ZipReadOptions};
|
|
|
17
17
|
use zip::result::{ZipError, ZipResult};
|
|
18
18
|
use zip::ZipArchive;
|
|
19
19
|
|
|
20
|
-
use crate::errors::{busy_error, read_error, zip_read_error, IntoRuby};
|
|
20
|
+
use crate::errors::{busy_error, header_error, read_error, zip_read_error, IntoRuby};
|
|
21
21
|
use crate::gvl::{maybe_without_gvl, Cancellable};
|
|
22
|
-
use crate::rb::bytes_of;
|
|
22
|
+
use crate::rb::{bytes_of, read_into_string, size_believable};
|
|
23
23
|
use crate::reader::{open_local_entry, EntryInfo, EntryReader};
|
|
24
24
|
use crate::source::Source;
|
|
25
25
|
|
|
@@ -56,7 +56,11 @@ impl Archive {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
pub fn open_bytes(ruby: &Ruby, bytes: RString) -> Result<Self, Error> {
|
|
59
|
-
|
|
59
|
+
// SAFETY: copied out at once into the Arc, with the GVL held and
|
|
60
|
+
// no Ruby call in between: one copy, rather than a Vec and then
|
|
61
|
+
// the Arc it is moved into (twice the archive's size, briefly).
|
|
62
|
+
let shared: std::sync::Arc<[u8]> = std::sync::Arc::from(unsafe { bytes.as_slice() });
|
|
63
|
+
Self::from_source(ruby, Source::memory(shared))
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
/// Any seekable IO-like Ruby object; read lazily, never copied.
|
|
@@ -130,50 +134,45 @@ impl Archive {
|
|
|
130
134
|
Ok(rb_self.archive(ruby)?.has_duplicate_names())
|
|
131
135
|
}
|
|
132
136
|
|
|
133
|
-
///
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
///
|
|
145
|
-
|
|
146
|
-
pub fn max_entry_size(ruby: &Ruby, rb_self: &Self) -> Result<u64, Error> {
|
|
147
|
-
let archive = rb_self.archive(ruby)?;
|
|
148
|
-
let meta = archive.metadata();
|
|
149
|
-
let mut largest = 0;
|
|
150
|
-
for i in 0..meta.len() {
|
|
151
|
-
largest = largest.max(meta.entry(i).rb(ruby)?.size());
|
|
152
|
-
}
|
|
153
|
-
Ok(largest)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/// The highest declared uncompressed-to-compressed size ratio among
|
|
157
|
-
/// the entries, for `Ziperu.max_compression_ratio`. An entry declaring
|
|
158
|
-
/// data but no compressed bytes is infinite. Also the bound on the
|
|
159
|
-
/// archive's overall ratio, which can be no higher than its highest
|
|
160
|
-
/// entry's.
|
|
161
|
-
pub fn max_compression_ratio(ruby: &Ruby, rb_self: &Self) -> Result<f64, Error> {
|
|
137
|
+
/// What the open-time limits look at, in one pass:
|
|
138
|
+
/// `[records, total_size, largest_size, highest_ratio]`.
|
|
139
|
+
///
|
|
140
|
+
/// Every record counts, including several under one name. The crate's
|
|
141
|
+
/// parsed view keeps only the last record of each name, but `entries`,
|
|
142
|
+
/// `InputStream` and `extract_all` reach every one of them, so the
|
|
143
|
+
/// limits must too: otherwise a bomb hides behind a harmless entry of
|
|
144
|
+
/// the same name.
|
|
145
|
+
///
|
|
146
|
+
/// An entry declaring data but no compressed bytes has an infinite
|
|
147
|
+
/// ratio. The archive's overall ratio can be no higher than its
|
|
148
|
+
/// highest entry's, so that bounds it too.
|
|
149
|
+
pub fn limits(ruby: &Ruby, rb_self: &Self) -> Result<(usize, u64, u64, f64), Error> {
|
|
162
150
|
let archive = rb_self.archive(ruby)?;
|
|
163
|
-
let
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
let
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
151
|
+
let mut summary = LimitSummary::default();
|
|
152
|
+
if archive.has_duplicate_names() {
|
|
153
|
+
let dir_start = archive.central_directory_start();
|
|
154
|
+
let mut raw = archive.clone().into_inner();
|
|
155
|
+
drop(archive);
|
|
156
|
+
let records = maybe_without_gvl(rb_self.needs_gvl, || {
|
|
157
|
+
walk_central_directory(&mut raw, dir_start)
|
|
158
|
+
})
|
|
159
|
+
.map_err(|e| header_error(ruby, e))?;
|
|
160
|
+
for record in &records {
|
|
161
|
+
summary.add(record.size, record.compressed_size);
|
|
170
162
|
}
|
|
171
|
-
|
|
172
|
-
|
|
163
|
+
} else {
|
|
164
|
+
let meta = archive.metadata();
|
|
165
|
+
for i in 0..meta.len() {
|
|
166
|
+
let entry = meta.entry(i).rb(ruby)?;
|
|
167
|
+
summary.add(entry.size(), entry.compressed_size());
|
|
173
168
|
}
|
|
174
|
-
worst = worst.max(size as f64 / csize as f64);
|
|
175
169
|
}
|
|
176
|
-
Ok(
|
|
170
|
+
Ok((
|
|
171
|
+
summary.records,
|
|
172
|
+
summary.total,
|
|
173
|
+
summary.largest,
|
|
174
|
+
summary.ratio,
|
|
175
|
+
))
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
/// Inflates entry `index` to nowhere so the crate checks its CRC (and
|
|
@@ -216,6 +215,7 @@ impl Archive {
|
|
|
216
215
|
size,
|
|
217
216
|
compressed_size,
|
|
218
217
|
crc32: crc,
|
|
218
|
+
method: None,
|
|
219
219
|
};
|
|
220
220
|
maybe_without_gvl(rb_self.needs_gvl, || {
|
|
221
221
|
let mut entry = open_local_entry(&mut source, &info, password.as_deref(), size)?;
|
|
@@ -267,7 +267,7 @@ impl Archive {
|
|
|
267
267
|
let records = maybe_without_gvl(rb_self.needs_gvl, || {
|
|
268
268
|
walk_central_directory(&mut raw, dir_start)
|
|
269
269
|
})
|
|
270
|
-
.
|
|
270
|
+
.map_err(|e| header_error(ruby, e))?;
|
|
271
271
|
let mut by_offset = HashMap::with_capacity(meta.len());
|
|
272
272
|
for i in 0..meta.len() {
|
|
273
273
|
let entry = meta.entry(i).rb(ruby)?;
|
|
@@ -288,7 +288,10 @@ impl Archive {
|
|
|
288
288
|
})
|
|
289
289
|
.collect::<ZipResult<Vec<_>>>()
|
|
290
290
|
})
|
|
291
|
-
.
|
|
291
|
+
.map_err(|e| match e {
|
|
292
|
+
ZipError::Io(e) => header_error(ruby, e),
|
|
293
|
+
other => crate::errors::zip_error(ruby, other),
|
|
294
|
+
})?;
|
|
292
295
|
for (i, record) in records.iter().enumerate() {
|
|
293
296
|
let entry = meta.entry(i).rb(ruby)?;
|
|
294
297
|
emit(entry_hash(ruby, &keys, i, &entry, record)?)?;
|
|
@@ -303,7 +306,8 @@ impl Archive {
|
|
|
303
306
|
let mut raw = small_source(&archive);
|
|
304
307
|
drop(archive);
|
|
305
308
|
let entry = meta.entry(index).rb(ruby)?;
|
|
306
|
-
let record = raw_central_record(&mut raw, entry.central_header_start())
|
|
309
|
+
let record = raw_central_record(&mut raw, entry.central_header_start())
|
|
310
|
+
.map_err(|e| header_error(ruby, e))?;
|
|
307
311
|
entry_hash(ruby, &MetaKeys::new(ruby), index, &entry, &record)
|
|
308
312
|
}
|
|
309
313
|
|
|
@@ -325,7 +329,8 @@ impl Archive {
|
|
|
325
329
|
let out = ruby.ary_new_capa(end.saturating_sub(index));
|
|
326
330
|
for i in index..end {
|
|
327
331
|
let entry = meta.entry(i).rb(ruby)?;
|
|
328
|
-
let record = raw_central_record(&mut raw, entry.central_header_start())
|
|
332
|
+
let record = raw_central_record(&mut raw, entry.central_header_start())
|
|
333
|
+
.map_err(|e| header_error(ruby, e))?;
|
|
329
334
|
out.push(entry_hash(ruby, &keys, i, &entry, &record)?)?;
|
|
330
335
|
}
|
|
331
336
|
Ok(out)
|
|
@@ -339,7 +344,7 @@ impl Archive {
|
|
|
339
344
|
let header_start = archive.metadata().entry(index).rb(ruby)?.header_start();
|
|
340
345
|
let mut raw = small_source(&archive);
|
|
341
346
|
drop(archive);
|
|
342
|
-
let extra = raw_local_extra(&mut raw, header_start).
|
|
347
|
+
let extra = raw_local_extra(&mut raw, header_start).map_err(|e| header_error(ruby, e))?;
|
|
343
348
|
Ok(ruby.str_from_slice(&extra))
|
|
344
349
|
}
|
|
345
350
|
|
|
@@ -350,7 +355,7 @@ impl Archive {
|
|
|
350
355
|
header_offset: u64,
|
|
351
356
|
) -> Result<RString, Error> {
|
|
352
357
|
let mut raw = rb_self.small_source(ruby)?;
|
|
353
|
-
let extra = raw_local_extra(&mut raw, header_offset).
|
|
358
|
+
let extra = raw_local_extra(&mut raw, header_offset).map_err(|e| header_error(ruby, e))?;
|
|
354
359
|
Ok(ruby.str_from_slice(&extra))
|
|
355
360
|
}
|
|
356
361
|
|
|
@@ -399,6 +404,7 @@ impl Archive {
|
|
|
399
404
|
size,
|
|
400
405
|
compressed_size,
|
|
401
406
|
crc32: crc,
|
|
407
|
+
method: None,
|
|
402
408
|
};
|
|
403
409
|
EntryReader::open_at(
|
|
404
410
|
ruby,
|
|
@@ -416,7 +422,19 @@ impl Archive {
|
|
|
416
422
|
// A clone (the central directory is shared behind an Arc) so the
|
|
417
423
|
// borrow is released before the GVL is.
|
|
418
424
|
let mut archive = rb_self.archive(ruby)?.clone();
|
|
425
|
+
let data_end = archive.central_directory_start();
|
|
419
426
|
let mut entry = archive.by_index(index).rb(ruby)?;
|
|
427
|
+
let size = entry.size();
|
|
428
|
+
let method = compression_code(entry.compression());
|
|
429
|
+
if !entry.encrypted() && size_believable(method, size, entry.compressed_size(), data_end) {
|
|
430
|
+
return read_into_string(
|
|
431
|
+
ruby,
|
|
432
|
+
rb_self.needs_gvl,
|
|
433
|
+
size as usize,
|
|
434
|
+
&mut Cancellable(&mut entry),
|
|
435
|
+
)
|
|
436
|
+
.map_err(|e| read_error(ruby, e));
|
|
437
|
+
}
|
|
420
438
|
// A hint only: the declared size is untrusted and may be a lie.
|
|
421
439
|
let mut buf = Vec::with_capacity(entry.size().min(CAPACITY_HINT_MAX) as usize);
|
|
422
440
|
maybe_without_gvl(rb_self.needs_gvl, || {
|
|
@@ -446,6 +464,31 @@ impl Archive {
|
|
|
446
464
|
}
|
|
447
465
|
}
|
|
448
466
|
|
|
467
|
+
/// The figures `Archive::limits` returns.
|
|
468
|
+
#[derive(Default)]
|
|
469
|
+
struct LimitSummary {
|
|
470
|
+
records: usize,
|
|
471
|
+
total: u64,
|
|
472
|
+
largest: u64,
|
|
473
|
+
ratio: f64,
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
impl LimitSummary {
|
|
477
|
+
fn add(&mut self, size: u64, compressed_size: u64) {
|
|
478
|
+
self.records += 1;
|
|
479
|
+
self.total = self.total.saturating_add(size);
|
|
480
|
+
self.largest = self.largest.max(size);
|
|
481
|
+
if size > 0 {
|
|
482
|
+
let ratio = if compressed_size == 0 {
|
|
483
|
+
f64::INFINITY
|
|
484
|
+
} else {
|
|
485
|
+
size as f64 / compressed_size as f64
|
|
486
|
+
};
|
|
487
|
+
self.ratio = self.ratio.max(ratio);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
449
492
|
/// A central directory record read straight from the bytes.
|
|
450
493
|
///
|
|
451
494
|
/// Two fields are read this way for every entry, where the crate's parsed
|
|
@@ -496,7 +539,7 @@ fn walk_central_directory(
|
|
|
496
539
|
source: &mut Source,
|
|
497
540
|
dir_start: u64,
|
|
498
541
|
) -> io::Result<Vec<RawCentralRecord>> {
|
|
499
|
-
source.
|
|
542
|
+
source.seek_to(dir_start)?;
|
|
500
543
|
let mut records = Vec::new();
|
|
501
544
|
let mut offset = dir_start;
|
|
502
545
|
while let Some(record) = read_central_record(source, offset)? {
|
|
@@ -604,7 +647,7 @@ fn raw_local_extra(source: &mut Source, header_offset: u64) -> io::Result<Vec<u8
|
|
|
604
647
|
const SIGNATURE: u32 = 0x0403_4b50;
|
|
605
648
|
const HEADER_LEN: usize = 30;
|
|
606
649
|
|
|
607
|
-
source.
|
|
650
|
+
source.seek_to(header_offset)?;
|
|
608
651
|
let mut header = [0u8; HEADER_LEN];
|
|
609
652
|
source.read_exact(&mut header)?;
|
|
610
653
|
if u32::from_le_bytes([header[0], header[1], header[2], header[3]]) != SIGNATURE {
|
|
@@ -613,9 +656,9 @@ fn raw_local_extra(source: &mut Source, header_offset: u64) -> io::Result<Vec<u8
|
|
|
613
656
|
"local file header signature not found",
|
|
614
657
|
));
|
|
615
658
|
}
|
|
616
|
-
let name_len = u16::from_le_bytes([header[26], header[27]]) as
|
|
659
|
+
let name_len = u16::from_le_bytes([header[26], header[27]]) as u64;
|
|
617
660
|
let extra_len = u16::from_le_bytes([header[28], header[29]]) as usize;
|
|
618
|
-
source.
|
|
661
|
+
source.seek_to(header_offset + HEADER_LEN as u64 + name_len)?;
|
|
619
662
|
let mut extra = vec![0u8; extra_len];
|
|
620
663
|
source.read_exact(&mut extra)?;
|
|
621
664
|
Ok(extra)
|
|
@@ -738,7 +781,7 @@ fn record_hash(
|
|
|
738
781
|
/// `to_u16` is deprecated in favour of matching on constants, but it is the
|
|
739
782
|
/// only public way to recover the raw code, which is what we hand to Ruby.
|
|
740
783
|
#[allow(deprecated)]
|
|
741
|
-
fn compression_code(method: zip::CompressionMethod) -> u16 {
|
|
784
|
+
pub(crate) fn compression_code(method: zip::CompressionMethod) -> u16 {
|
|
742
785
|
method.to_u16()
|
|
743
786
|
}
|
|
744
787
|
|