ziperu 0.2.3 → 0.3.1
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/Cargo.lock +2 -1
- data/README.md +35 -7
- data/ext/ziperu/Cargo.toml +9 -1
- data/ext/ziperu/src/archive.rs +246 -144
- data/ext/ziperu/src/entry_options.rs +1 -1
- data/ext/ziperu/src/errors.rs +39 -11
- data/ext/ziperu/src/extract.rs +332 -104
- data/ext/ziperu/src/gvl.rs +45 -26
- data/ext/ziperu/src/lib.rs +17 -0
- data/ext/ziperu/src/place.rs +379 -0
- data/ext/ziperu/src/rb.rs +14 -1
- data/ext/ziperu/src/reader.rs +100 -29
- data/ext/ziperu/src/sink.rs +2 -7
- data/ext/ziperu/src/source.rs +4 -7
- data/ext/ziperu/src/stream.rs +89 -11
- data/ext/ziperu/src/test_rng.rs +37 -0
- data/ext/ziperu/src/writer.rs +71 -25
- data/ext/ziperu/src/zipcrypto.rs +40 -12
- data/lib/zip.rb +3 -3
- 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 +8 -1
data/ext/ziperu/src/reader.rs
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
//! the data, which is what rubyzip does when `Ziperu.validate_entry_sizes` is
|
|
9
9
|
//! off.
|
|
10
10
|
|
|
11
|
-
use std::cell::{RefCell, RefMut};
|
|
11
|
+
use std::cell::{Cell, RefCell, RefMut};
|
|
12
12
|
use std::io::{self, Read, Seek, SeekFrom};
|
|
13
13
|
|
|
14
14
|
use magnus::{
|
|
15
15
|
gc::Marker, typed_data::DataTypeFunctions, value::Opaque, Error, RString, Ruby, TypedData,
|
|
16
16
|
Value,
|
|
17
17
|
};
|
|
18
|
-
use zip::read::{read_zipfile_from_stream_with_options, ZipFile, ZipReadOptions};
|
|
18
|
+
use zip::read::{read_zipfile_from_stream_with_options, ZipFile, ZipFileEntry, ZipReadOptions};
|
|
19
19
|
use zip::result::{ZipError, ZipResult};
|
|
20
20
|
use zip::ZipArchive;
|
|
21
21
|
|
|
@@ -64,6 +64,45 @@ pub(crate) struct EntryInfo {
|
|
|
64
64
|
pub(crate) method: Option<u16>,
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
impl EntryInfo {
|
|
68
|
+
/// For the entry the crate has at `index`.
|
|
69
|
+
pub(crate) fn from_entry(index: usize, entry: &ZipFileEntry<'_>) -> Self {
|
|
70
|
+
Self {
|
|
71
|
+
index: Some(index),
|
|
72
|
+
header_start: entry.header_start(),
|
|
73
|
+
size: entry.size(),
|
|
74
|
+
compressed_size: entry.compressed_size(),
|
|
75
|
+
crc32: entry.crc32(),
|
|
76
|
+
method: Some(crate::archive::compression_code(entry.compression())),
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/// For a record the crate cannot address by index, as its central
|
|
81
|
+
/// record describes it.
|
|
82
|
+
pub(crate) fn at_offset(
|
|
83
|
+
header_start: u64,
|
|
84
|
+
size: u64,
|
|
85
|
+
compressed_size: u64,
|
|
86
|
+
crc32: u32,
|
|
87
|
+
) -> Self {
|
|
88
|
+
Self {
|
|
89
|
+
index: None,
|
|
90
|
+
header_start,
|
|
91
|
+
size,
|
|
92
|
+
compressed_size,
|
|
93
|
+
crc32,
|
|
94
|
+
method: None,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Declared to hold nothing: no data, and the CRC of no data. An entry
|
|
99
|
+
/// declared empty but holding data, or with another CRC, is a lie the
|
|
100
|
+
/// crate's checks should catch.
|
|
101
|
+
fn is_empty(&self) -> bool {
|
|
102
|
+
self.size == 0 && self.compressed_size == 0 && self.crc32 == 0
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
67
106
|
/// The compression method in the local header at the source's position,
|
|
68
107
|
/// which is left where it was.
|
|
69
108
|
fn local_method(source: &mut Source, header_start: u64) -> ZipResult<u16> {
|
|
@@ -137,11 +176,13 @@ impl OwnedEntry {
|
|
|
137
176
|
let options = ZipReadOptions::new().password(password);
|
|
138
177
|
let file = match self.owner {
|
|
139
178
|
Owner::Archive(archive) => {
|
|
179
|
+
// SAFETY: as above.
|
|
140
180
|
let archive: &'static mut ZipArchive<Source> = unsafe { &mut *archive };
|
|
141
181
|
let index = info.index.ok_or(ZipError::FileNotFound)?;
|
|
142
182
|
archive.by_index_with_options(index, options)?
|
|
143
183
|
}
|
|
144
184
|
Owner::Source(source) => {
|
|
185
|
+
// SAFETY: as above.
|
|
145
186
|
let source: &'static mut Source = unsafe { &mut *source };
|
|
146
187
|
open_local_entry(source, info, password, u64::MAX)?
|
|
147
188
|
}
|
|
@@ -205,6 +246,12 @@ pub struct EntryReader {
|
|
|
205
246
|
inner: RefCell<Inner>,
|
|
206
247
|
/// The Ruby IO behind a `Source::Ruby`, kept alive by `mark`.
|
|
207
248
|
io: Option<Opaque<Value>>,
|
|
249
|
+
/// The String an interrupted `read_direct` was filling, kept alive by
|
|
250
|
+
/// `mark`. Its bytes become `Inner::pending` only if another read
|
|
251
|
+
/// comes (`settle`): copying them at the interrupt held up a
|
|
252
|
+
/// `Timeout` by the time it takes to copy most of a large entry.
|
|
253
|
+
/// Set and taken with the GVL held only.
|
|
254
|
+
partial: Cell<Option<Opaque<RString>>>,
|
|
208
255
|
}
|
|
209
256
|
|
|
210
257
|
impl DataTypeFunctions for EntryReader {
|
|
@@ -212,11 +259,16 @@ impl DataTypeFunctions for EntryReader {
|
|
|
212
259
|
if let Some(io) = self.io {
|
|
213
260
|
marker.mark(io);
|
|
214
261
|
}
|
|
262
|
+
if let Some(partial) = self.partial.get() {
|
|
263
|
+
marker.mark(partial);
|
|
264
|
+
}
|
|
215
265
|
}
|
|
216
266
|
}
|
|
217
267
|
|
|
218
|
-
// SAFETY: the raw pointer in `OwnedEntry` is only dereferenced
|
|
219
|
-
//
|
|
268
|
+
// SAFETY: the raw pointer in `OwnedEntry` is only dereferenced through the
|
|
269
|
+
// reader's `RefCell`, whose exclusive borrow is held by one thread for the
|
|
270
|
+
// whole of a call (with or without the GVL), so the reader is never used
|
|
271
|
+
// concurrently.
|
|
220
272
|
unsafe impl Send for EntryReader {}
|
|
221
273
|
|
|
222
274
|
impl EntryReader {
|
|
@@ -236,14 +288,7 @@ impl EntryReader {
|
|
|
236
288
|
let meta = archive.metadata();
|
|
237
289
|
let raw = meta.entry(index).rb(ruby)?;
|
|
238
290
|
seekable = raw.compression() == zip::CompressionMethod::Stored && !raw.encrypted();
|
|
239
|
-
EntryInfo
|
|
240
|
-
index: Some(index),
|
|
241
|
-
header_start: raw.header_start(),
|
|
242
|
-
size: raw.size(),
|
|
243
|
-
compressed_size: raw.compressed_size(),
|
|
244
|
-
crc32: raw.crc32(),
|
|
245
|
-
method: Some(crate::archive::compression_code(raw.compression())),
|
|
246
|
-
}
|
|
291
|
+
EntryInfo::from_entry(index, &raw)
|
|
247
292
|
};
|
|
248
293
|
let raw_source =
|
|
249
294
|
(seekable && enforce_size).then(|| archive.clone().into_inner().clone_small());
|
|
@@ -281,6 +326,7 @@ impl EntryReader {
|
|
|
281
326
|
let pos = pos.min(size);
|
|
282
327
|
inner.raw = Some((data_start + pos, data_start + size));
|
|
283
328
|
inner.consumed = pos;
|
|
329
|
+
rb_self.partial.set(None);
|
|
284
330
|
inner.pending.clear();
|
|
285
331
|
inner.failed = None;
|
|
286
332
|
inner.finished = pos >= size;
|
|
@@ -352,10 +398,7 @@ impl EntryReader {
|
|
|
352
398
|
});
|
|
353
399
|
let entry = OwnedEntry::open(archive, enforce_size, password, &info).rb(ruby)?;
|
|
354
400
|
let size = enforce_size.then_some(info.size);
|
|
355
|
-
|
|
356
|
-
// is that of no data: an entry declared empty but holding data, or
|
|
357
|
-
// with another CRC, is a lie the crate's checks should catch.
|
|
358
|
-
let finished = size == Some(0) && info.compressed_size == 0 && info.crc32 == 0;
|
|
401
|
+
let finished = size.is_some() && info.is_empty();
|
|
359
402
|
Ok(Self {
|
|
360
403
|
inner: RefCell::new(Inner {
|
|
361
404
|
entry: Some(entry),
|
|
@@ -372,9 +415,20 @@ impl EntryReader {
|
|
|
372
415
|
direct,
|
|
373
416
|
}),
|
|
374
417
|
io,
|
|
418
|
+
partial: Cell::new(None),
|
|
375
419
|
})
|
|
376
420
|
}
|
|
377
421
|
|
|
422
|
+
/// Moves what an interrupted `read_direct` had read into `pending`,
|
|
423
|
+
/// for the read now starting to serve first.
|
|
424
|
+
fn settle(&self, ruby: &Ruby, inner: &mut Inner) {
|
|
425
|
+
if let Some(partial) = self.partial.take() {
|
|
426
|
+
let partial = ruby.get_inner(partial);
|
|
427
|
+
// SAFETY: copied out at once, with the GVL held.
|
|
428
|
+
inner.pending = unsafe { partial.as_slice() }.to_vec();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
378
432
|
fn inner(&self, ruby: &Ruby) -> Result<RefMut<'_, Inner>, Error> {
|
|
379
433
|
self.inner
|
|
380
434
|
.try_borrow_mut()
|
|
@@ -400,6 +454,7 @@ impl EntryReader {
|
|
|
400
454
|
let mut inner = rb_self.inner(ruby)?;
|
|
401
455
|
inner.entry = None;
|
|
402
456
|
inner.finished = true;
|
|
457
|
+
rb_self.partial.set(None);
|
|
403
458
|
Ok(())
|
|
404
459
|
}
|
|
405
460
|
|
|
@@ -414,6 +469,7 @@ impl EntryReader {
|
|
|
414
469
|
if let Some(reason) = &inner.failed {
|
|
415
470
|
return Err(failed_error(ruby, reason));
|
|
416
471
|
}
|
|
472
|
+
rb_self.settle(ruby, &mut inner);
|
|
417
473
|
if inner.finished && inner.pending.is_empty() {
|
|
418
474
|
return Ok(None);
|
|
419
475
|
}
|
|
@@ -425,7 +481,7 @@ impl EntryReader {
|
|
|
425
481
|
if inner.raw.is_some() {
|
|
426
482
|
return Self::read_raw(ruby, &mut inner, maxlen);
|
|
427
483
|
}
|
|
428
|
-
let remaining = inner.size.
|
|
484
|
+
let remaining = inner.size.map_or(u64::MAX, |s| s - inner.consumed);
|
|
429
485
|
let want = match maxlen {
|
|
430
486
|
Some(n) => (n as u64).min(remaining),
|
|
431
487
|
None => remaining,
|
|
@@ -446,7 +502,7 @@ impl EntryReader {
|
|
|
446
502
|
return Ok(Some(ruby.str_from_slice(&buf)));
|
|
447
503
|
}
|
|
448
504
|
if buf.is_empty() && probe && inner.direct && want >= crate::rb::DIRECT_READ_MIN {
|
|
449
|
-
return Self::read_direct(ruby, &mut inner, want as usize);
|
|
505
|
+
return Self::read_direct(ruby, rb_self, &mut inner, want as usize);
|
|
450
506
|
}
|
|
451
507
|
let Inner {
|
|
452
508
|
entry, finished, ..
|
|
@@ -504,9 +560,14 @@ impl EntryReader {
|
|
|
504
560
|
/// The rest of the entry, `want` bytes to its declared size, read
|
|
505
561
|
/// straight into the String handed back (see `rb::fill_new_string`),
|
|
506
562
|
/// with the same outcomes as the chunked way: an interrupt leaves what
|
|
507
|
-
/// was read
|
|
508
|
-
/// reader, and data past the declared size is a size-limit error.
|
|
509
|
-
fn read_direct(
|
|
563
|
+
/// was read for the next read (`partial`), any other error poisons
|
|
564
|
+
/// the reader, and data past the declared size is a size-limit error.
|
|
565
|
+
fn read_direct(
|
|
566
|
+
ruby: &Ruby,
|
|
567
|
+
rb_self: &Self,
|
|
568
|
+
inner: &mut Inner,
|
|
569
|
+
want: usize,
|
|
570
|
+
) -> Result<Option<RString>, Error> {
|
|
510
571
|
let needs_gvl = inner.needs_gvl;
|
|
511
572
|
if inner.finished {
|
|
512
573
|
return Ok(None);
|
|
@@ -537,8 +598,7 @@ impl EntryReader {
|
|
|
537
598
|
}
|
|
538
599
|
Err(e) => {
|
|
539
600
|
if crate::gvl::is_cancelled(&e) {
|
|
540
|
-
|
|
541
|
-
inner.pending = unsafe { out.as_slice() }.to_vec();
|
|
601
|
+
rb_self.partial.set(Some(out.into()));
|
|
542
602
|
} else {
|
|
543
603
|
inner.failed = Some(e.to_string());
|
|
544
604
|
}
|
|
@@ -567,7 +627,8 @@ impl EntryReader {
|
|
|
567
627
|
return Err(zip_error(ruby, e));
|
|
568
628
|
}
|
|
569
629
|
*consumed = 0;
|
|
570
|
-
*finished =
|
|
630
|
+
*finished = size.is_some() && info.is_empty();
|
|
631
|
+
rb_self.partial.set(None);
|
|
571
632
|
inner.pending.clear();
|
|
572
633
|
inner.failed = None;
|
|
573
634
|
inner.raw = None;
|
|
@@ -578,12 +639,22 @@ impl EntryReader {
|
|
|
578
639
|
/// Where the data of the entry whose local header is at `header_start`
|
|
579
640
|
/// begins: after the header's fixed part, the name and the extra field.
|
|
580
641
|
fn data_offset(source: &mut Source, header_start: u64) -> io::Result<u64> {
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
642
|
+
let (name_len, extra_len) = local_header_lens(source, header_start)?;
|
|
643
|
+
Ok(header_start + LOCAL_HEADER_LEN + name_len + extra_len)
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/// The fixed part of a local file header, before the name.
|
|
647
|
+
pub(crate) const LOCAL_HEADER_LEN: u64 = 30;
|
|
648
|
+
const LOCAL_HEADER_SIGNATURE: u32 = 0x0403_4b50;
|
|
649
|
+
|
|
650
|
+
/// The lengths of the name and of the extra field that the local header
|
|
651
|
+
/// at `header_start` declares. The source is left just after the header's
|
|
652
|
+
/// fixed part.
|
|
653
|
+
pub(crate) fn local_header_lens(source: &mut Source, header_start: u64) -> io::Result<(u64, u64)> {
|
|
654
|
+
let mut header = [0u8; LOCAL_HEADER_LEN as usize];
|
|
584
655
|
source.seek_to(header_start)?;
|
|
585
656
|
source.read_exact(&mut header)?;
|
|
586
|
-
if u32::from_le_bytes([header[0], header[1], header[2], header[3]]) !=
|
|
657
|
+
if u32::from_le_bytes([header[0], header[1], header[2], header[3]]) != LOCAL_HEADER_SIGNATURE {
|
|
587
658
|
return Err(io::Error::new(
|
|
588
659
|
io::ErrorKind::InvalidData,
|
|
589
660
|
"local file header signature not found",
|
|
@@ -591,7 +662,7 @@ fn data_offset(source: &mut Source, header_start: u64) -> io::Result<u64> {
|
|
|
591
662
|
}
|
|
592
663
|
let name_len = u64::from(u16::from_le_bytes([header[26], header[27]]));
|
|
593
664
|
let extra_len = u64::from(u16::from_le_bytes([header[28], header[29]]));
|
|
594
|
-
Ok(
|
|
665
|
+
Ok((name_len, extra_len))
|
|
595
666
|
}
|
|
596
667
|
|
|
597
668
|
/// The error every read raises after one failed part-way.
|
data/ext/ziperu/src/sink.rs
CHANGED
|
@@ -8,6 +8,7 @@ use std::sync::Arc;
|
|
|
8
8
|
|
|
9
9
|
use magnus::{prelude::*, value::Opaque, Error, RString, Ruby, Value};
|
|
10
10
|
|
|
11
|
+
use crate::rb::copy_chunk;
|
|
11
12
|
use crate::source::{resolve_seek, ruby_io_error};
|
|
12
13
|
|
|
13
14
|
pub(crate) const BUF_SIZE: usize = 64 * 1024;
|
|
@@ -473,13 +474,7 @@ impl Read for RubyOut {
|
|
|
473
474
|
return Ok(0);
|
|
474
475
|
}
|
|
475
476
|
let chunk: Option<RString> = io.funcall("read", (buf.len(),)).map_err(|e| self.fail(e))?;
|
|
476
|
-
let
|
|
477
|
-
return Ok(0);
|
|
478
|
-
};
|
|
479
|
-
// SAFETY: the slice is copied out before any other Ruby call.
|
|
480
|
-
let bytes = unsafe { chunk.as_slice() };
|
|
481
|
-
let n = bytes.len().min(buf.len());
|
|
482
|
-
buf[..n].copy_from_slice(&bytes[..n]);
|
|
477
|
+
let n = copy_chunk(chunk, buf);
|
|
483
478
|
self.pos += n as u64;
|
|
484
479
|
Ok(n)
|
|
485
480
|
}
|
data/ext/ziperu/src/source.rs
CHANGED
|
@@ -21,6 +21,8 @@ use magnus::{
|
|
|
21
21
|
Error, RString, Ruby, Value,
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
+
use crate::rb::copy_chunk;
|
|
25
|
+
|
|
24
26
|
const BUF_SIZE: usize = 64 * 1024;
|
|
25
27
|
|
|
26
28
|
pub enum Source {
|
|
@@ -258,6 +260,7 @@ impl RubyError {
|
|
|
258
260
|
// touched, nor the root registered or released, from another thread; the
|
|
259
261
|
// bounds are only there to fit in `io::Error`.
|
|
260
262
|
unsafe impl Send for RubyError {}
|
|
263
|
+
// SAFETY: as for `Send` above.
|
|
261
264
|
unsafe impl Sync for RubyError {}
|
|
262
265
|
|
|
263
266
|
impl fmt::Debug for RubyError {
|
|
@@ -287,13 +290,7 @@ impl Read for RubyIo {
|
|
|
287
290
|
io.funcall::<_, _, Value>("seek", (self.pos as i64, SEEK_SET))
|
|
288
291
|
.map_err(ruby_io_error)?;
|
|
289
292
|
let chunk: Option<RString> = io.funcall("read", (buf.len(),)).map_err(ruby_io_error)?;
|
|
290
|
-
let
|
|
291
|
-
return Ok(0);
|
|
292
|
-
};
|
|
293
|
-
// SAFETY: the slice is copied out before any other Ruby call.
|
|
294
|
-
let bytes = unsafe { chunk.as_slice() };
|
|
295
|
-
let n = bytes.len().min(buf.len());
|
|
296
|
-
buf[..n].copy_from_slice(&bytes[..n]);
|
|
293
|
+
let n = copy_chunk(chunk, buf);
|
|
297
294
|
self.pos += n as u64;
|
|
298
295
|
Ok(n)
|
|
299
296
|
}
|
data/ext/ziperu/src/stream.rs
CHANGED
|
@@ -25,8 +25,9 @@ use magnus::{
|
|
|
25
25
|
use zip::read::{read_zipfile_from_stream_with_options, ZipReadOptions};
|
|
26
26
|
|
|
27
27
|
use crate::errors::{busy_error, read_error, size_limit_error, ERROR};
|
|
28
|
-
use crate::rb::bytes_of;
|
|
28
|
+
use crate::rb::{bytes_of, copy_chunk};
|
|
29
29
|
use crate::source::ruby_io_error;
|
|
30
|
+
use crate::zipcrypto::{DATA_DESCRIPTOR_FLAG, ENCRYPTED_FLAG};
|
|
30
31
|
|
|
31
32
|
const LOCAL_SIGNATURE: u32 = 0x0403_4b50;
|
|
32
33
|
const CENTRAL_SIGNATURE: u32 = 0x0201_4b50;
|
|
@@ -57,14 +58,7 @@ impl Read for RubyRead {
|
|
|
57
58
|
let ruby = Ruby::get().map_err(|e| io::Error::other(e.to_string()))?;
|
|
58
59
|
let io = ruby.get_inner(self.io);
|
|
59
60
|
let chunk: Option<RString> = io.funcall("read", (buf.len(),)).map_err(ruby_io_error)?;
|
|
60
|
-
|
|
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)
|
|
61
|
+
Ok(copy_chunk(chunk, buf))
|
|
68
62
|
}
|
|
69
63
|
}
|
|
70
64
|
|
|
@@ -282,8 +276,8 @@ impl State {
|
|
|
282
276
|
.read_vec(u16_at(24) as usize)
|
|
283
277
|
.map_err(|e| stream_error(ruby, e))?;
|
|
284
278
|
let zip64 = apply_zip64(&extra, &mut size, &mut csize);
|
|
285
|
-
let descriptor = flags &
|
|
286
|
-
let encrypted = flags &
|
|
279
|
+
let descriptor = flags & DATA_DESCRIPTOR_FLAG != 0;
|
|
280
|
+
let encrypted = flags & ENCRYPTED_FLAG != 0;
|
|
287
281
|
|
|
288
282
|
let unsupported = |what: &str| {
|
|
289
283
|
Error::new(
|
|
@@ -384,6 +378,10 @@ impl State {
|
|
|
384
378
|
if current.finished {
|
|
385
379
|
return Ok(false);
|
|
386
380
|
}
|
|
381
|
+
// A step at a time, whatever the caller asked for: the buffer is
|
|
382
|
+
// grown (and zeroed) by `want` before each read, and the caller
|
|
383
|
+
// comes back for the rest.
|
|
384
|
+
let want = want.min(CHUNK);
|
|
387
385
|
let start = out.len();
|
|
388
386
|
let ended = match &mut current.body {
|
|
389
387
|
Body::Stored { remaining } => {
|
|
@@ -539,3 +537,83 @@ fn apply_zip64(extra: &[u8], size: &mut u64, csize: &mut u64) -> bool {
|
|
|
539
537
|
fn stream_error(ruby: &Ruby, err: io::Error) -> Error {
|
|
540
538
|
crate::errors::header_error(ruby, err)
|
|
541
539
|
}
|
|
540
|
+
|
|
541
|
+
#[cfg(test)]
|
|
542
|
+
mod tests {
|
|
543
|
+
use super::*;
|
|
544
|
+
use crate::test_rng::{Rng, CASES};
|
|
545
|
+
|
|
546
|
+
const SATURATED: u64 = u32::MAX as u64;
|
|
547
|
+
|
|
548
|
+
fn field(id: u16, data: &[u8]) -> Vec<u8> {
|
|
549
|
+
let mut out = id.to_le_bytes().to_vec();
|
|
550
|
+
out.extend_from_slice(&(data.len() as u16).to_le_bytes());
|
|
551
|
+
out.extend_from_slice(data);
|
|
552
|
+
out
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
#[test]
|
|
556
|
+
fn property_any_extra_bytes_are_survived() {
|
|
557
|
+
let mut rng = Rng::new(4);
|
|
558
|
+
for _ in 0..CASES {
|
|
559
|
+
let mut extra = rng.bytes(64);
|
|
560
|
+
if rng.below(2) == 0 && extra.len() >= 2 {
|
|
561
|
+
extra[..2].copy_from_slice(&ZIP64_EXTRA_ID.to_le_bytes());
|
|
562
|
+
}
|
|
563
|
+
let (mut size, mut csize) = (SATURATED, SATURATED);
|
|
564
|
+
apply_zip64(&extra, &mut size, &mut csize);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
#[test]
|
|
569
|
+
fn property_the_zip64_field_fills_only_saturated_sizes() {
|
|
570
|
+
let mut rng = Rng::new(5);
|
|
571
|
+
for case in 0..CASES {
|
|
572
|
+
let (true_size, true_csize) = (rng.next(), rng.next());
|
|
573
|
+
let size_saturated = rng.below(2) == 0;
|
|
574
|
+
let csize_saturated = rng.below(2) == 0;
|
|
575
|
+
let mut data = Vec::new();
|
|
576
|
+
if size_saturated {
|
|
577
|
+
data.extend_from_slice(&true_size.to_le_bytes());
|
|
578
|
+
}
|
|
579
|
+
if csize_saturated {
|
|
580
|
+
data.extend_from_slice(&true_csize.to_le_bytes());
|
|
581
|
+
}
|
|
582
|
+
// Other fields before it, as writers put them.
|
|
583
|
+
let mut extra = Vec::new();
|
|
584
|
+
for _ in 0..rng.below(3) {
|
|
585
|
+
let other = rng.bytes(12);
|
|
586
|
+
extra.extend(field(0x5455 + rng.below(4) as u16, &other));
|
|
587
|
+
}
|
|
588
|
+
extra.extend(field(ZIP64_EXTRA_ID, &data));
|
|
589
|
+
let mut size = if size_saturated { SATURATED } else { 10 };
|
|
590
|
+
let mut csize = if csize_saturated { SATURATED } else { 20 };
|
|
591
|
+
assert!(apply_zip64(&extra, &mut size, &mut csize), "case {case}");
|
|
592
|
+
assert_eq!(
|
|
593
|
+
size,
|
|
594
|
+
if size_saturated { true_size } else { 10 },
|
|
595
|
+
"case {case}"
|
|
596
|
+
);
|
|
597
|
+
assert_eq!(
|
|
598
|
+
csize,
|
|
599
|
+
if csize_saturated { true_csize } else { 20 },
|
|
600
|
+
"case {case}"
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
#[test]
|
|
606
|
+
fn property_without_a_zip64_field_nothing_changes() {
|
|
607
|
+
let mut rng = Rng::new(6);
|
|
608
|
+
for case in 0..CASES {
|
|
609
|
+
let mut extra = Vec::new();
|
|
610
|
+
for _ in 0..rng.below(4) {
|
|
611
|
+
let other = rng.bytes(20);
|
|
612
|
+
extra.extend(field(0x000A + rng.below(0x100) as u16, &other));
|
|
613
|
+
}
|
|
614
|
+
let (mut size, mut csize) = (SATURATED, 7);
|
|
615
|
+
assert!(!apply_zip64(&extra, &mut size, &mut csize), "case {case}");
|
|
616
|
+
assert_eq!((size, csize), (SATURATED, 7), "case {case}");
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//! A small seeded generator for the property tests: the same cases on
|
|
2
|
+
//! every run and every machine, without a dependency. A failing case
|
|
3
|
+
//! prints its seed and input, which is all the shrinking these targets
|
|
4
|
+
//! need.
|
|
5
|
+
|
|
6
|
+
/// xorshift64*.
|
|
7
|
+
pub(crate) struct Rng(u64);
|
|
8
|
+
|
|
9
|
+
impl Rng {
|
|
10
|
+
pub(crate) fn new(seed: u64) -> Self {
|
|
11
|
+
Self(seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
pub(crate) fn next(&mut self) -> u64 {
|
|
15
|
+
self.0 ^= self.0 >> 12;
|
|
16
|
+
self.0 ^= self.0 << 25;
|
|
17
|
+
self.0 ^= self.0 >> 27;
|
|
18
|
+
self.0.wrapping_mul(0x2545_F491_4F6C_DD1D)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// A number in `0..n` (`n` above zero).
|
|
22
|
+
pub(crate) fn below(&mut self, n: usize) -> usize {
|
|
23
|
+
(self.next() % n as u64) as usize
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
pub(crate) fn pick<'a, T>(&mut self, items: &'a [T]) -> &'a T {
|
|
27
|
+
&items[self.below(items.len())]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
pub(crate) fn bytes(&mut self, max_len: usize) -> Vec<u8> {
|
|
31
|
+
let len = self.below(max_len + 1);
|
|
32
|
+
(0..len).map(|_| self.next() as u8).collect()
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// How many cases each property runs.
|
|
37
|
+
pub(crate) const CASES: u64 = 5_000;
|