ziperu 0.2.1 → 0.2.2
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 +73 -0
- data/Cargo.lock +1 -1
- data/README.md +56 -5
- data/ext/ziperu/Cargo.toml +1 -1
- data/ext/ziperu/src/archive.rs +85 -54
- data/ext/ziperu/src/errors.rs +76 -14
- data/ext/ziperu/src/extract.rs +92 -33
- data/ext/ziperu/src/lib.rs +12 -9
- data/ext/ziperu/src/reader.rs +211 -13
- data/ext/ziperu/src/sink.rs +48 -8
- data/ext/ziperu/src/source.rs +2 -2
- data/ext/ziperu/src/stream.rs +541 -0
- data/ext/ziperu/src/writer.rs +128 -5
- data/lib/ziperu/entry/source.rb +1 -1
- data/lib/ziperu/entry.rb +8 -1
- data/lib/ziperu/errors.rb +5 -0
- data/lib/ziperu/file.rb +24 -2
- data/lib/ziperu/input_stream.rb +167 -11
- data/lib/ziperu/output_stream.rb +72 -1
- data/lib/ziperu/remote_io.rb +278 -0
- data/lib/ziperu/replace_rubyzip.rb +67 -0
- data/lib/ziperu/version.rb +1 -1
- data/lib/ziperu.rb +4 -0
- metadata +4 -1
data/ext/ziperu/src/sink.rs
CHANGED
|
@@ -34,6 +34,24 @@ impl Sink {
|
|
|
34
34
|
self.write_all(bytes)
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/// Makes `len` the end of what was written: an entry taken back out
|
|
38
|
+
/// (`Writer::rollback_entry`) can leave bytes past the central
|
|
39
|
+
/// directory that a shorter archive did not overwrite.
|
|
40
|
+
pub(crate) fn end_at(&mut self, len: u64) -> io::Result<()> {
|
|
41
|
+
match self {
|
|
42
|
+
Sink::File(_) => self.set_len(len),
|
|
43
|
+
Sink::Memory(c) => {
|
|
44
|
+
c.get_mut().truncate(len as usize);
|
|
45
|
+
Ok(())
|
|
46
|
+
}
|
|
47
|
+
Sink::Ruby(w) => {
|
|
48
|
+
w.flush()?;
|
|
49
|
+
w.get_mut().end = len;
|
|
50
|
+
Ok(())
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
37
55
|
/// Cuts a file off at `len`; a no-op for the other kinds.
|
|
38
56
|
pub(crate) fn set_len(&mut self, len: u64) -> io::Result<()> {
|
|
39
57
|
if let Sink::File(f) = self {
|
|
@@ -105,10 +123,15 @@ impl Seek for Sink {
|
|
|
105
123
|
pub struct RubyOut {
|
|
106
124
|
io: Opaque<Value>,
|
|
107
125
|
attached: Arc<AtomicBool>,
|
|
126
|
+
/// Set once a call on the IO has raised. From then on the sink takes
|
|
127
|
+
/// nothing more: the crate, recovering from the error, would write
|
|
128
|
+
/// again what the IO may already have half taken, and the Writer
|
|
129
|
+
/// refuses every later call (see `Writer::with_state`).
|
|
130
|
+
broken: Arc<AtomicBool>,
|
|
108
131
|
/// Where the next write or read goes.
|
|
109
132
|
pos: u64,
|
|
110
133
|
/// One past the furthest byte written: the archive's end.
|
|
111
|
-
end: u64,
|
|
134
|
+
pub(crate) end: u64,
|
|
112
135
|
flushable: bool,
|
|
113
136
|
/// False for a pipe, socket or response body: writes go out in order
|
|
114
137
|
/// and nothing is ever seeked to.
|
|
@@ -118,11 +141,17 @@ pub struct RubyOut {
|
|
|
118
141
|
const SEEK_SET: i64 = 0;
|
|
119
142
|
|
|
120
143
|
impl RubyOut {
|
|
121
|
-
pub(crate) fn new(
|
|
144
|
+
pub(crate) fn new(
|
|
145
|
+
io: Value,
|
|
146
|
+
attached: Arc<AtomicBool>,
|
|
147
|
+
broken: Arc<AtomicBool>,
|
|
148
|
+
seekable: bool,
|
|
149
|
+
) -> Result<Self, Error> {
|
|
122
150
|
Ok(Self {
|
|
123
151
|
flushable: io.respond_to("flush", false)?,
|
|
124
152
|
io: Opaque::from(io),
|
|
125
153
|
attached,
|
|
154
|
+
broken,
|
|
126
155
|
pos: 0,
|
|
127
156
|
end: 0,
|
|
128
157
|
seekable,
|
|
@@ -135,6 +164,9 @@ impl RubyOut {
|
|
|
135
164
|
if !self.attached.load(Ordering::Acquire) {
|
|
136
165
|
return Ok(None);
|
|
137
166
|
}
|
|
167
|
+
if self.broken.load(Ordering::Acquire) {
|
|
168
|
+
return Err(io::Error::other("the IO raised earlier"));
|
|
169
|
+
}
|
|
138
170
|
let ruby = Ruby::get().map_err(|e| io::Error::other(e.to_string()))?;
|
|
139
171
|
Ok(Some(ruby.get_inner(self.io)))
|
|
140
172
|
}
|
|
@@ -149,7 +181,7 @@ impl RubyOut {
|
|
|
149
181
|
};
|
|
150
182
|
if self.seekable {
|
|
151
183
|
io.funcall::<_, _, Value>("seek", (self.pos as i64, SEEK_SET))
|
|
152
|
-
.map_err(
|
|
184
|
+
.map_err(|e| self.fail(e))?;
|
|
153
185
|
}
|
|
154
186
|
Ok(Some(io))
|
|
155
187
|
}
|
|
@@ -164,9 +196,9 @@ impl RubyOut {
|
|
|
164
196
|
if !self.seekable {
|
|
165
197
|
return Ok(());
|
|
166
198
|
}
|
|
167
|
-
if io.respond_to("truncate", false).map_err(
|
|
199
|
+
if io.respond_to("truncate", false).map_err(|e| self.fail(e))? {
|
|
168
200
|
io.funcall::<_, _, Value>("truncate", (self.end as i64,))
|
|
169
|
-
.map_err(
|
|
201
|
+
.map_err(|e| self.fail(e))?;
|
|
170
202
|
}
|
|
171
203
|
Ok(())
|
|
172
204
|
}
|
|
@@ -179,6 +211,14 @@ impl RubyOut {
|
|
|
179
211
|
}
|
|
180
212
|
}
|
|
181
213
|
|
|
214
|
+
impl RubyOut {
|
|
215
|
+
/// The error a call on the IO raised, noting that the IO is broken.
|
|
216
|
+
fn fail(&self, err: Error) -> io::Error {
|
|
217
|
+
self.broken.store(true, Ordering::Release);
|
|
218
|
+
ruby_io_error(err)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
182
222
|
impl Write for RubyOut {
|
|
183
223
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
184
224
|
if buf.is_empty() {
|
|
@@ -188,7 +228,7 @@ impl Write for RubyOut {
|
|
|
188
228
|
Some(io) => {
|
|
189
229
|
let ruby = Ruby::get().map_err(|e| io::Error::other(e.to_string()))?;
|
|
190
230
|
let chunk = ruby.str_from_slice(buf);
|
|
191
|
-
let written: Value = io.funcall("write", (chunk,)).map_err(
|
|
231
|
+
let written: Value = io.funcall("write", (chunk,)).map_err(|e| self.fail(e))?;
|
|
192
232
|
// `IO#write` returns the byte count; an IO-like object may
|
|
193
233
|
// return anything else, in which case the whole buffer
|
|
194
234
|
// counts as written.
|
|
@@ -213,7 +253,7 @@ impl Write for RubyOut {
|
|
|
213
253
|
fn flush(&mut self) -> io::Result<()> {
|
|
214
254
|
if let (true, Some(io)) = (self.flushable, self.value()?) {
|
|
215
255
|
io.funcall::<_, _, Value>("flush", ())
|
|
216
|
-
.map_err(
|
|
256
|
+
.map_err(|e| self.fail(e))?;
|
|
217
257
|
}
|
|
218
258
|
Ok(())
|
|
219
259
|
}
|
|
@@ -227,7 +267,7 @@ impl Read for RubyOut {
|
|
|
227
267
|
if buf.is_empty() {
|
|
228
268
|
return Ok(0);
|
|
229
269
|
}
|
|
230
|
-
let chunk: Option<RString> = io.funcall("read", (buf.len(),)).map_err(
|
|
270
|
+
let chunk: Option<RString> = io.funcall("read", (buf.len(),)).map_err(|e| self.fail(e))?;
|
|
231
271
|
let Some(chunk) = chunk else {
|
|
232
272
|
return Ok(0);
|
|
233
273
|
};
|
data/ext/ziperu/src/source.rs
CHANGED
|
@@ -34,8 +34,8 @@ impl Source {
|
|
|
34
34
|
Source::File(BufReader::with_capacity(BUF_SIZE, SharedFile::new(file)))
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
pub fn memory(bytes:
|
|
38
|
-
Source::Memory(Cursor::new(
|
|
37
|
+
pub fn memory(bytes: Arc<[u8]>) -> Self {
|
|
38
|
+
Source::Memory(Cursor::new(bytes))
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
pub fn ruby(io: Value) -> Result<Self, Error> {
|
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
//! `Ziperu::Native::StreamReader`: reads an archive from a pipe or socket
|
|
2
|
+
//! in one pass, local header by local header, never holding more than an
|
|
3
|
+
//! entry's worth of it.
|
|
4
|
+
//!
|
|
5
|
+
//! The central directory is at the end, out of reach until everything
|
|
6
|
+
//! before it has gone by, so this is what can be known from the local
|
|
7
|
+
//! headers alone (no comments, no external attributes). Most streamed
|
|
8
|
+
//! archives put an entry's sizes in a data descriptor after its data
|
|
9
|
+
//! (general-purpose bit 3), which the zip crate's own stream reader does
|
|
10
|
+
//! not take; here a Deflated entry is inflated until the deflate stream
|
|
11
|
+
//! ends, and its descriptor read after it. An entry whose sizes are in its
|
|
12
|
+
//! local header is read for any method the crate knows, through the crate,
|
|
13
|
+
//! with its compressed bytes in memory. A Stored or other entry with its
|
|
14
|
+
//! sizes only after the data cannot be delimited in one pass: that raises
|
|
15
|
+
//! `Ziperu::Native::UnsupportedError`.
|
|
16
|
+
|
|
17
|
+
use std::cell::{RefCell, RefMut};
|
|
18
|
+
use std::io::{self, BufRead, BufReader, Cursor, Read};
|
|
19
|
+
|
|
20
|
+
use flate2::{Decompress, FlushDecompress, Status};
|
|
21
|
+
use magnus::{
|
|
22
|
+
gc::Marker, prelude::*, typed_data::DataTypeFunctions, value::Opaque, Error, RHash, RString,
|
|
23
|
+
Ruby, TypedData, Value,
|
|
24
|
+
};
|
|
25
|
+
use zip::read::{read_zipfile_from_stream_with_options, ZipReadOptions};
|
|
26
|
+
|
|
27
|
+
use crate::errors::{busy_error, read_error, size_limit_error, ERROR};
|
|
28
|
+
use crate::rb::bytes_of;
|
|
29
|
+
use crate::source::ruby_io_error;
|
|
30
|
+
|
|
31
|
+
const LOCAL_SIGNATURE: u32 = 0x0403_4b50;
|
|
32
|
+
const CENTRAL_SIGNATURE: u32 = 0x0201_4b50;
|
|
33
|
+
const END_SIGNATURE: u32 = 0x0605_4b50;
|
|
34
|
+
const ZIP64_END_SIGNATURE: u32 = 0x0606_4b50;
|
|
35
|
+
const DESCRIPTOR_SIGNATURE: u32 = 0x0807_4b50;
|
|
36
|
+
/// A split archive's first segment starts with this; one written in a
|
|
37
|
+
/// single piece sometimes does too, and it is skipped.
|
|
38
|
+
const SPANNING_SIGNATURE: u32 = 0x3030_4b50;
|
|
39
|
+
const ZIP64_EXTRA_ID: u16 = 0x0001;
|
|
40
|
+
const STORED: u16 = 0;
|
|
41
|
+
const DEFLATED: u16 = 8;
|
|
42
|
+
const CHUNK: usize = 64 * 1024;
|
|
43
|
+
/// Entries read through the crate have their compressed bytes in memory;
|
|
44
|
+
/// never reserve more than this on the strength of a header field.
|
|
45
|
+
const CAPACITY_HINT_MAX: u64 = 1 << 20;
|
|
46
|
+
|
|
47
|
+
/// A Ruby IO read through `read(n)`, forward only.
|
|
48
|
+
struct RubyRead {
|
|
49
|
+
io: Opaque<Value>,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
impl Read for RubyRead {
|
|
53
|
+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
54
|
+
if buf.is_empty() {
|
|
55
|
+
return Ok(0);
|
|
56
|
+
}
|
|
57
|
+
let ruby = Ruby::get().map_err(|e| io::Error::other(e.to_string()))?;
|
|
58
|
+
let io = ruby.get_inner(self.io);
|
|
59
|
+
let chunk: Option<RString> = io.funcall("read", (buf.len(),)).map_err(ruby_io_error)?;
|
|
60
|
+
let Some(chunk) = chunk else {
|
|
61
|
+
return Ok(0);
|
|
62
|
+
};
|
|
63
|
+
// SAFETY: copied out at once, with no Ruby call in between.
|
|
64
|
+
let bytes = unsafe { chunk.as_slice() };
|
|
65
|
+
let n = bytes.len().min(buf.len());
|
|
66
|
+
buf[..n].copy_from_slice(&bytes[..n]);
|
|
67
|
+
Ok(n)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/// How the current entry's data is being read.
|
|
72
|
+
enum Body {
|
|
73
|
+
/// Its sizes are known: the bytes as they come (Stored), or inflated.
|
|
74
|
+
Stored {
|
|
75
|
+
remaining: u64,
|
|
76
|
+
},
|
|
77
|
+
Inflate(Box<Decompress>),
|
|
78
|
+
/// Decoded through the crate from its compressed bytes in memory.
|
|
79
|
+
Buffered(Cursor<Vec<u8>>),
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
struct Current {
|
|
83
|
+
body: Body,
|
|
84
|
+
/// The CRC and sizes from the local header, or from the descriptor
|
|
85
|
+
/// once read; `None` until known.
|
|
86
|
+
crc: Option<u32>,
|
|
87
|
+
size: Option<u64>,
|
|
88
|
+
/// Bit 3: a data descriptor follows the data.
|
|
89
|
+
descriptor: bool,
|
|
90
|
+
zip64: bool,
|
|
91
|
+
hasher: crc32fast::Hasher,
|
|
92
|
+
produced: u64,
|
|
93
|
+
/// The data has been read to its end and checked.
|
|
94
|
+
finished: bool,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
struct State {
|
|
98
|
+
input: BufReader<RubyRead>,
|
|
99
|
+
current: Option<Current>,
|
|
100
|
+
/// The central directory (or the end of the input) was reached.
|
|
101
|
+
done: bool,
|
|
102
|
+
/// Bytes taken from the input so far, which is where the next local
|
|
103
|
+
/// header starts.
|
|
104
|
+
offset: u64,
|
|
105
|
+
password: Option<Vec<u8>>,
|
|
106
|
+
enforce_size: bool,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#[derive(TypedData)]
|
|
110
|
+
#[magnus(class = "Ziperu::Native::StreamReader", free_immediately, mark)]
|
|
111
|
+
pub struct StreamReader {
|
|
112
|
+
inner: RefCell<Option<State>>,
|
|
113
|
+
io: Opaque<Value>,
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
impl DataTypeFunctions for StreamReader {
|
|
117
|
+
fn mark(&self, marker: &Marker) {
|
|
118
|
+
marker.mark(self.io);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
impl StreamReader {
|
|
123
|
+
pub fn new(io: Value, password: Option<RString>, enforce_size: bool) -> Result<Self, Error> {
|
|
124
|
+
let opaque = Opaque::from(io);
|
|
125
|
+
Ok(Self {
|
|
126
|
+
inner: RefCell::new(Some(State {
|
|
127
|
+
input: BufReader::with_capacity(CHUNK, RubyRead { io: opaque }),
|
|
128
|
+
current: None,
|
|
129
|
+
done: false,
|
|
130
|
+
offset: 0,
|
|
131
|
+
password: password.map(bytes_of),
|
|
132
|
+
enforce_size,
|
|
133
|
+
})),
|
|
134
|
+
io: opaque,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn state(&self, ruby: &Ruby) -> Result<RefMut<'_, State>, Error> {
|
|
139
|
+
let guard = self
|
|
140
|
+
.inner
|
|
141
|
+
.try_borrow_mut()
|
|
142
|
+
.map_err(|_| busy_error(ruby, "stream reader"))?;
|
|
143
|
+
RefMut::filter_map(guard, Option::as_mut)
|
|
144
|
+
.map_err(|_| Error::new(ruby.exception_io_error(), "closed stream"))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
pub fn close(ruby: &Ruby, rb_self: &Self) -> Result<(), Error> {
|
|
148
|
+
rb_self
|
|
149
|
+
.inner
|
|
150
|
+
.try_borrow_mut()
|
|
151
|
+
.map_err(|_| busy_error(ruby, "stream reader"))?
|
|
152
|
+
.take();
|
|
153
|
+
Ok(())
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Skips what is left of the current entry and reads the next local
|
|
157
|
+
/// header: a Hash shaped as `Archive#entries` gives (sizes nil where
|
|
158
|
+
/// the header defers them to a descriptor), or nil at the central
|
|
159
|
+
/// directory.
|
|
160
|
+
pub fn next_entry(ruby: &Ruby, rb_self: &Self) -> Result<Option<RHash>, Error> {
|
|
161
|
+
let mut state = rb_self.state(ruby)?;
|
|
162
|
+
if state.current.is_some() {
|
|
163
|
+
// The rest of the entry, read and checked: nothing is skipped
|
|
164
|
+
// unseen, so a corrupt entry fails here as it would when read.
|
|
165
|
+
let mut sink = Vec::new();
|
|
166
|
+
loop {
|
|
167
|
+
sink.clear();
|
|
168
|
+
if !state.fill(ruby, &mut sink, CHUNK)? {
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
state.current = None;
|
|
173
|
+
}
|
|
174
|
+
if state.done {
|
|
175
|
+
return Ok(None);
|
|
176
|
+
}
|
|
177
|
+
state.read_header(ruby)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// Up to `maxlen` bytes of the current entry (CHUNK when nil), or nil
|
|
181
|
+
/// at its end.
|
|
182
|
+
pub fn read(
|
|
183
|
+
ruby: &Ruby,
|
|
184
|
+
rb_self: &Self,
|
|
185
|
+
maxlen: Option<usize>,
|
|
186
|
+
) -> Result<Option<RString>, Error> {
|
|
187
|
+
let mut state = rb_self.state(ruby)?;
|
|
188
|
+
let want = maxlen.unwrap_or(CHUNK);
|
|
189
|
+
if want == 0 {
|
|
190
|
+
return Ok(Some(ruby.str_new("")));
|
|
191
|
+
}
|
|
192
|
+
let mut out = Vec::with_capacity(want.min(CHUNK));
|
|
193
|
+
while out.len() < want {
|
|
194
|
+
let left = want - out.len();
|
|
195
|
+
if !state.fill(ruby, &mut out, left)? {
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if out.is_empty() {
|
|
200
|
+
return Ok(None);
|
|
201
|
+
}
|
|
202
|
+
Ok(Some(ruby.str_from_slice(&out)))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
pub fn is_eof(ruby: &Ruby, rb_self: &Self) -> Result<bool, Error> {
|
|
206
|
+
let state = rb_self.state(ruby)?;
|
|
207
|
+
Ok(state.current.as_ref().is_none_or(|c| c.finished))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// The current entry's `[crc, size]` as read, once its data has been
|
|
211
|
+
/// read to the end, or nil before: for an entry whose header deferred
|
|
212
|
+
/// them to a descriptor, what it came to.
|
|
213
|
+
pub fn trailer(ruby: &Ruby, rb_self: &Self) -> Result<Option<(u32, u64)>, Error> {
|
|
214
|
+
let state = rb_self.state(ruby)?;
|
|
215
|
+
Ok(state
|
|
216
|
+
.current
|
|
217
|
+
.as_ref()
|
|
218
|
+
.filter(|c| c.finished)
|
|
219
|
+
.map(|c| (c.hasher.clone().finalize(), c.produced)))
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
impl State {
|
|
224
|
+
fn read_u32(&mut self) -> io::Result<u32> {
|
|
225
|
+
let mut b = [0u8; 4];
|
|
226
|
+
self.input.read_exact(&mut b)?;
|
|
227
|
+
self.offset += 4;
|
|
228
|
+
Ok(u32::from_le_bytes(b))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
fn read_vec(&mut self, len: usize) -> io::Result<Vec<u8>> {
|
|
232
|
+
let mut v = vec![0u8; len];
|
|
233
|
+
self.input.read_exact(&mut v)?;
|
|
234
|
+
self.offset += len as u64;
|
|
235
|
+
Ok(v)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
fn read_header(&mut self, ruby: &Ruby) -> Result<Option<RHash>, Error> {
|
|
239
|
+
let header_offset = self.offset;
|
|
240
|
+
let signature = match self.read_u32() {
|
|
241
|
+
Ok(s) => s,
|
|
242
|
+
// An input that ends where an entry could start has no more.
|
|
243
|
+
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof && self.offset == 0 => {
|
|
244
|
+
self.done = true;
|
|
245
|
+
return Ok(None);
|
|
246
|
+
}
|
|
247
|
+
Err(e) => return Err(stream_error(ruby, e)),
|
|
248
|
+
};
|
|
249
|
+
let signature = if signature == SPANNING_SIGNATURE && header_offset == 0 {
|
|
250
|
+
self.read_u32().map_err(|e| stream_error(ruby, e))?
|
|
251
|
+
} else {
|
|
252
|
+
signature
|
|
253
|
+
};
|
|
254
|
+
match signature {
|
|
255
|
+
LOCAL_SIGNATURE => {}
|
|
256
|
+
CENTRAL_SIGNATURE | END_SIGNATURE | ZIP64_END_SIGNATURE => {
|
|
257
|
+
self.done = true;
|
|
258
|
+
return Ok(None);
|
|
259
|
+
}
|
|
260
|
+
_ => {
|
|
261
|
+
return Err(Error::new(
|
|
262
|
+
ruby.get_inner(&ERROR),
|
|
263
|
+
format!("invalid Zip archive: no local header at offset {header_offset}"),
|
|
264
|
+
))
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
let fixed = self.read_vec(26).map_err(|e| stream_error(ruby, e))?;
|
|
268
|
+
let u16_at = |i: usize| u16::from_le_bytes([fixed[i], fixed[i + 1]]);
|
|
269
|
+
let u32_at =
|
|
270
|
+
|i: usize| u32::from_le_bytes([fixed[i], fixed[i + 1], fixed[i + 2], fixed[i + 3]]);
|
|
271
|
+
let flags = u16_at(2);
|
|
272
|
+
let method = u16_at(4);
|
|
273
|
+
let dos_time = u16_at(6);
|
|
274
|
+
let dos_date = u16_at(8);
|
|
275
|
+
let crc = u32_at(10);
|
|
276
|
+
let mut csize = u64::from(u32_at(14));
|
|
277
|
+
let mut size = u64::from(u32_at(18));
|
|
278
|
+
let name = self
|
|
279
|
+
.read_vec(u16_at(22) as usize)
|
|
280
|
+
.map_err(|e| stream_error(ruby, e))?;
|
|
281
|
+
let extra = self
|
|
282
|
+
.read_vec(u16_at(24) as usize)
|
|
283
|
+
.map_err(|e| stream_error(ruby, e))?;
|
|
284
|
+
let zip64 = apply_zip64(&extra, &mut size, &mut csize);
|
|
285
|
+
let descriptor = flags & 8 != 0;
|
|
286
|
+
let encrypted = flags & 1 != 0;
|
|
287
|
+
|
|
288
|
+
let unsupported = |what: &str| {
|
|
289
|
+
Error::new(
|
|
290
|
+
ruby.get_inner(&crate::errors::NATIVE_UNSUPPORTED_ERROR),
|
|
291
|
+
format!(
|
|
292
|
+
"entry '{}': {what} cannot be read from a stream in one pass; \
|
|
293
|
+
read the archive from a file or a seekable IO",
|
|
294
|
+
String::from_utf8_lossy(&name)
|
|
295
|
+
),
|
|
296
|
+
)
|
|
297
|
+
};
|
|
298
|
+
let body = if descriptor {
|
|
299
|
+
if method != DEFLATED || encrypted {
|
|
300
|
+
return Err(unsupported(
|
|
301
|
+
"an entry whose sizes follow its data, unless Deflated and unencrypted,",
|
|
302
|
+
));
|
|
303
|
+
}
|
|
304
|
+
Body::Inflate(Box::new(Decompress::new(false)))
|
|
305
|
+
} else if method == STORED && !encrypted {
|
|
306
|
+
Body::Stored { remaining: csize }
|
|
307
|
+
} else if method == DEFLATED && !encrypted {
|
|
308
|
+
Body::Inflate(Box::new(Decompress::new(false)))
|
|
309
|
+
} else {
|
|
310
|
+
// Any other method, or encryption: the crate decodes it from
|
|
311
|
+
// the compressed bytes, given a local header to start from.
|
|
312
|
+
let mut raw = Vec::with_capacity(30 + name.len() + extra.len());
|
|
313
|
+
raw.extend_from_slice(&LOCAL_SIGNATURE.to_le_bytes());
|
|
314
|
+
raw.extend_from_slice(&fixed);
|
|
315
|
+
raw.extend_from_slice(&name);
|
|
316
|
+
raw.extend_from_slice(&extra);
|
|
317
|
+
raw.reserve(csize.min(CAPACITY_HINT_MAX) as usize);
|
|
318
|
+
let copied = io::copy(&mut (&mut self.input).take(csize), &mut raw)
|
|
319
|
+
.map_err(|e| stream_error(ruby, e))?;
|
|
320
|
+
self.offset += copied;
|
|
321
|
+
if copied != csize {
|
|
322
|
+
return Err(stream_error(ruby, io::ErrorKind::UnexpectedEof.into()));
|
|
323
|
+
}
|
|
324
|
+
let mut cursor = Cursor::new(raw);
|
|
325
|
+
let options = ZipReadOptions::new().password(self.password.as_deref());
|
|
326
|
+
let mut file = read_zipfile_from_stream_with_options(&mut cursor, options)
|
|
327
|
+
.map_err(|e| crate::errors::zip_read_error(ruby, e))?
|
|
328
|
+
.ok_or_else(|| Error::new(ruby.get_inner(&ERROR), "invalid Zip archive"))?;
|
|
329
|
+
let mut decoded = Vec::with_capacity(size.min(CAPACITY_HINT_MAX) as usize);
|
|
330
|
+
file.read_to_end(&mut decoded)
|
|
331
|
+
.map_err(|e| read_error(ruby, e))?;
|
|
332
|
+
drop(file);
|
|
333
|
+
Body::Buffered(Cursor::new(decoded))
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
let h = ruby.hash_new();
|
|
337
|
+
h.aset(ruby.to_symbol("index"), ())?;
|
|
338
|
+
h.aset(ruby.to_symbol("name"), ruby.str_from_slice(&name))?;
|
|
339
|
+
let (known_size, known_csize, known_crc) = if descriptor {
|
|
340
|
+
(None, None, None)
|
|
341
|
+
} else {
|
|
342
|
+
(Some(size), Some(csize), Some(crc))
|
|
343
|
+
};
|
|
344
|
+
h.aset(ruby.to_symbol("size"), known_size)?;
|
|
345
|
+
h.aset(ruby.to_symbol("compressed_size"), known_csize)?;
|
|
346
|
+
h.aset(ruby.to_symbol("crc"), known_crc)?;
|
|
347
|
+
h.aset(ruby.to_symbol("compression_method"), method)?;
|
|
348
|
+
h.aset(ruby.to_symbol("flags"), flags)?;
|
|
349
|
+
h.aset(ruby.to_symbol("external_attributes"), 0)?;
|
|
350
|
+
h.aset(ruby.to_symbol("fstype"), 0)?;
|
|
351
|
+
h.aset(ruby.to_symbol("version_made_by"), u16_at(0) & 0xFF)?;
|
|
352
|
+
h.aset(ruby.to_symbol("dos_date"), dos_date)?;
|
|
353
|
+
h.aset(ruby.to_symbol("dos_time"), dos_time)?;
|
|
354
|
+
h.aset(ruby.to_symbol("comment"), ruby.str_new(""))?;
|
|
355
|
+
h.aset(ruby.to_symbol("extra"), ruby.str_from_slice(&extra))?;
|
|
356
|
+
h.aset(ruby.to_symbol("header_offset"), header_offset)?;
|
|
357
|
+
|
|
358
|
+
let buffered = matches!(body, Body::Buffered(_));
|
|
359
|
+
self.current = Some(Current {
|
|
360
|
+
body,
|
|
361
|
+
crc: (!descriptor).then_some(crc),
|
|
362
|
+
size: (!descriptor).then_some(size),
|
|
363
|
+
descriptor,
|
|
364
|
+
zip64,
|
|
365
|
+
hasher: crc32fast::Hasher::new(),
|
|
366
|
+
produced: 0,
|
|
367
|
+
finished: false,
|
|
368
|
+
});
|
|
369
|
+
if buffered {
|
|
370
|
+
// The crate has checked the CRC already; nothing to compare.
|
|
371
|
+
if let Some(c) = &mut self.current {
|
|
372
|
+
c.crc = None;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
Ok(Some(h))
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/// Appends up to `want` bytes of the current entry to `out`; false
|
|
379
|
+
/// once its data has ended (and been checked).
|
|
380
|
+
fn fill(&mut self, ruby: &Ruby, out: &mut Vec<u8>, want: usize) -> Result<bool, Error> {
|
|
381
|
+
let Some(current) = self.current.as_mut() else {
|
|
382
|
+
return Ok(false);
|
|
383
|
+
};
|
|
384
|
+
if current.finished {
|
|
385
|
+
return Ok(false);
|
|
386
|
+
}
|
|
387
|
+
let start = out.len();
|
|
388
|
+
let ended = match &mut current.body {
|
|
389
|
+
Body::Stored { remaining } => {
|
|
390
|
+
let step = (*remaining).min(want as u64) as usize;
|
|
391
|
+
if step == 0 {
|
|
392
|
+
true
|
|
393
|
+
} else {
|
|
394
|
+
out.resize(start + step, 0);
|
|
395
|
+
let n = self
|
|
396
|
+
.input
|
|
397
|
+
.read(&mut out[start..])
|
|
398
|
+
.map_err(|e| stream_error(ruby, e))?;
|
|
399
|
+
out.truncate(start + n);
|
|
400
|
+
if n == 0 {
|
|
401
|
+
return Err(stream_error(ruby, io::ErrorKind::UnexpectedEof.into()));
|
|
402
|
+
}
|
|
403
|
+
*remaining -= n as u64;
|
|
404
|
+
self.offset += n as u64;
|
|
405
|
+
*remaining == 0
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
Body::Inflate(d) => {
|
|
409
|
+
let input = self.input.fill_buf().map_err(|e| stream_error(ruby, e))?;
|
|
410
|
+
if input.is_empty() {
|
|
411
|
+
return Err(stream_error(ruby, io::ErrorKind::UnexpectedEof.into()));
|
|
412
|
+
}
|
|
413
|
+
out.resize(start + want, 0);
|
|
414
|
+
let (before_in, before_out) = (d.total_in(), d.total_out());
|
|
415
|
+
let status = d
|
|
416
|
+
.decompress(input, &mut out[start..], FlushDecompress::None)
|
|
417
|
+
.map_err(|e| read_error(ruby, io::Error::new(io::ErrorKind::InvalidData, e)))?;
|
|
418
|
+
let consumed = (d.total_in() - before_in) as usize;
|
|
419
|
+
let produced = d.total_out() - before_out;
|
|
420
|
+
out.truncate(start + produced as usize);
|
|
421
|
+
self.input.consume(consumed);
|
|
422
|
+
self.offset += consumed as u64;
|
|
423
|
+
if produced == 0 && consumed == 0 && status != Status::StreamEnd {
|
|
424
|
+
return Err(read_error(
|
|
425
|
+
ruby,
|
|
426
|
+
io::Error::new(
|
|
427
|
+
io::ErrorKind::InvalidData,
|
|
428
|
+
"deflate stream makes no progress",
|
|
429
|
+
),
|
|
430
|
+
));
|
|
431
|
+
}
|
|
432
|
+
status == Status::StreamEnd
|
|
433
|
+
}
|
|
434
|
+
Body::Buffered(c) => {
|
|
435
|
+
out.resize(start + want, 0);
|
|
436
|
+
let n = c.read(&mut out[start..]).map_err(|e| read_error(ruby, e))?;
|
|
437
|
+
out.truncate(start + n);
|
|
438
|
+
n == 0
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
let current = self.current.as_mut().expect("checked above");
|
|
442
|
+
let fresh = &out[start..];
|
|
443
|
+
current.hasher.update(fresh);
|
|
444
|
+
current.produced += fresh.len() as u64;
|
|
445
|
+
if let Some(size) = current.size {
|
|
446
|
+
if self.enforce_size && current.produced > size {
|
|
447
|
+
return Err(size_limit_error(ruby));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if ended {
|
|
451
|
+
self.finish_entry(ruby)?;
|
|
452
|
+
}
|
|
453
|
+
Ok(!fresh.is_empty() || !ended)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/// The entry's data has ended: read its descriptor, if it has one, and
|
|
457
|
+
/// check the CRC and size.
|
|
458
|
+
fn finish_entry(&mut self, ruby: &Ruby) -> Result<(), Error> {
|
|
459
|
+
let (descriptor, zip64) = {
|
|
460
|
+
let c = self.current.as_ref().expect("an entry in progress");
|
|
461
|
+
(c.descriptor, c.zip64)
|
|
462
|
+
};
|
|
463
|
+
if descriptor {
|
|
464
|
+
let first = self.read_u32().map_err(|e| stream_error(ruby, e))?;
|
|
465
|
+
let crc = if first == DESCRIPTOR_SIGNATURE {
|
|
466
|
+
self.read_u32().map_err(|e| stream_error(ruby, e))?
|
|
467
|
+
} else {
|
|
468
|
+
first
|
|
469
|
+
};
|
|
470
|
+
let sizes = self
|
|
471
|
+
.read_vec(if zip64 { 16 } else { 8 })
|
|
472
|
+
.map_err(|e| stream_error(ruby, e))?;
|
|
473
|
+
let size = if zip64 {
|
|
474
|
+
u64::from_le_bytes(sizes[8..16].try_into().expect("8 bytes"))
|
|
475
|
+
} else {
|
|
476
|
+
u64::from(u32::from_le_bytes(sizes[4..8].try_into().expect("4 bytes")))
|
|
477
|
+
};
|
|
478
|
+
let c = self.current.as_mut().expect("an entry in progress");
|
|
479
|
+
c.crc = Some(crc);
|
|
480
|
+
c.size = Some(size);
|
|
481
|
+
}
|
|
482
|
+
let c = self.current.as_mut().expect("an entry in progress");
|
|
483
|
+
c.finished = true;
|
|
484
|
+
if let Some(expected) = c.crc {
|
|
485
|
+
if c.hasher.clone().finalize() != expected {
|
|
486
|
+
return Err(read_error(
|
|
487
|
+
ruby,
|
|
488
|
+
io::Error::new(io::ErrorKind::InvalidData, "invalid checksum"),
|
|
489
|
+
));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if let Some(size) = c.size {
|
|
493
|
+
if c.produced < size {
|
|
494
|
+
return Err(read_error(
|
|
495
|
+
ruby,
|
|
496
|
+
io::Error::new(
|
|
497
|
+
io::ErrorKind::UnexpectedEof,
|
|
498
|
+
"entry is shorter than its declared size",
|
|
499
|
+
),
|
|
500
|
+
));
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
Ok(())
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/// The 64-bit sizes of a zip64 extra field, where the header saturated
|
|
508
|
+
/// them; whether there was one (which makes the descriptor's sizes 64-bit).
|
|
509
|
+
fn apply_zip64(extra: &[u8], size: &mut u64, csize: &mut u64) -> bool {
|
|
510
|
+
let mut rest = extra;
|
|
511
|
+
while rest.len() >= 4 {
|
|
512
|
+
let id = u16::from_le_bytes([rest[0], rest[1]]);
|
|
513
|
+
let len = u16::from_le_bytes([rest[2], rest[3]]) as usize;
|
|
514
|
+
rest = &rest[4..];
|
|
515
|
+
if rest.len() < len {
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
let (mut data, tail) = rest.split_at(len);
|
|
519
|
+
rest = tail;
|
|
520
|
+
if id != ZIP64_EXTRA_ID {
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
for field in [size, csize] {
|
|
524
|
+
if *field != u64::from(u32::MAX) {
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
let Some((value, more)) = data.split_first_chunk::<8>() else {
|
|
528
|
+
return true;
|
|
529
|
+
};
|
|
530
|
+
*field = u64::from_le_bytes(*value);
|
|
531
|
+
data = more;
|
|
532
|
+
}
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
false
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/// An error reading the input: an archive cut short is a broken archive.
|
|
539
|
+
fn stream_error(ruby: &Ruby, err: io::Error) -> Error {
|
|
540
|
+
crate::errors::header_error(ruby, err)
|
|
541
|
+
}
|