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/writer.rs
CHANGED
|
@@ -21,6 +21,7 @@ use magnus::{
|
|
|
21
21
|
gc::Marker, typed_data::DataTypeFunctions, value::Opaque, Error, RArray, RHash, RString, Ruby,
|
|
22
22
|
TryConvert, TypedData, Value,
|
|
23
23
|
};
|
|
24
|
+
use zip::read::ZipFile;
|
|
24
25
|
use zip::result::{ZipError, ZipResult};
|
|
25
26
|
use zip::write::StreamWriter;
|
|
26
27
|
use zip::{CompressionMethod, ZipArchive, ZipWriter};
|
|
@@ -147,6 +148,22 @@ struct State {
|
|
|
147
148
|
const INLINE_WRITE: usize = 16 * 1024;
|
|
148
149
|
/// How much of a large write goes in between checks for an interrupt.
|
|
149
150
|
const WRITE_CHUNK: usize = 1024 * 1024;
|
|
151
|
+
/// `write_file` reads the file through a buffer of this size.
|
|
152
|
+
const FILE_READ_BUF: usize = 256 * 1024;
|
|
153
|
+
|
|
154
|
+
/// Encrypted with the traditional PKWARE cipher (anything but AES).
|
|
155
|
+
fn is_zipcrypto<R: Read>(file: &ZipFile<'_, R>) -> bool {
|
|
156
|
+
file.encrypted() && file.compression() != CompressionMethod::Aes
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/// What a raw copy of `file` adds: its sizes and CRC are the source's.
|
|
160
|
+
fn known_of<R: Read>(file: &ZipFile<'_, R>) -> Begun {
|
|
161
|
+
Begun::Known {
|
|
162
|
+
size: file.size(),
|
|
163
|
+
csize: file.compressed_size(),
|
|
164
|
+
crc: file.crc32(),
|
|
165
|
+
}
|
|
166
|
+
}
|
|
150
167
|
/// `add_files_parallel` hands the crate this many files per thread at a
|
|
151
168
|
/// time, checking for an interrupt in between.
|
|
152
169
|
const PARALLEL_PART_PER_THREAD: usize = 4;
|
|
@@ -293,9 +310,9 @@ impl Writer {
|
|
|
293
310
|
/// that no reader takes for a complete one. Any other writer is just
|
|
294
311
|
/// dropped.
|
|
295
312
|
pub fn abort(ruby: &Ruby, rb_self: &Self) -> Result<(), Error> {
|
|
296
|
-
let (zip, append) = {
|
|
313
|
+
let (zip, append, count) = {
|
|
297
314
|
let mut state = rb_self.borrow(ruby)?;
|
|
298
|
-
(state.zip.take(), state.append.take())
|
|
315
|
+
(state.zip.take(), state.append.take(), state.count)
|
|
299
316
|
};
|
|
300
317
|
if append.is_none() {
|
|
301
318
|
if let Some(attached) = &rb_self.attached {
|
|
@@ -309,8 +326,13 @@ impl Writer {
|
|
|
309
326
|
};
|
|
310
327
|
maybe_without_gvl(rb_self.needs_gvl(), || -> ZipResult<()> {
|
|
311
328
|
// The crate's `finish` is the only way to the sink; what it
|
|
312
|
-
// writes is overwritten or cut off just below.
|
|
313
|
-
|
|
329
|
+
// writes is overwritten or cut off just below. `abort_file`
|
|
330
|
+
// takes out the last entry whichever it is, so it is only for
|
|
331
|
+
// one added here: with none, it would rewind over an entry the
|
|
332
|
+
// archive already had, which the tail does not cover.
|
|
333
|
+
if count > append.base {
|
|
334
|
+
zip.abort_file()?;
|
|
335
|
+
}
|
|
314
336
|
let mut sink = zip.finish()?;
|
|
315
337
|
sink.seek(SeekFrom::Start(append.tail_start))?;
|
|
316
338
|
sink.write_all(&append.tail)?;
|
|
@@ -340,7 +362,7 @@ impl Writer {
|
|
|
340
362
|
));
|
|
341
363
|
}
|
|
342
364
|
let file_options = options.to_file_options(ruby)?;
|
|
343
|
-
let index = rb_self.add_entry(ruby, rb_self.needs_gvl(), &name, |
|
|
365
|
+
let index = rb_self.add_entry(ruby, rb_self.needs_gvl(), &name, |_, zip| {
|
|
344
366
|
if options.zipcrypto && zip.is_stream() {
|
|
345
367
|
return Err(stream_error(ruby, "ZipCrypto entries"));
|
|
346
368
|
}
|
|
@@ -348,7 +370,6 @@ impl Writer {
|
|
|
348
370
|
with_zip!(zip, |w| w.start_file_with_options(file_options.name(&name)))
|
|
349
371
|
})
|
|
350
372
|
.map_err(|e| entry_error(ruby, &name, e))?;
|
|
351
|
-
let _ = state;
|
|
352
373
|
Ok(Begun::Hashed)
|
|
353
374
|
})?;
|
|
354
375
|
let extra_flags = if options.efs {
|
|
@@ -505,7 +526,7 @@ impl Writer {
|
|
|
505
526
|
.rb(ruby)?;
|
|
506
527
|
rb_self.with_state(ruby, |state, zip| {
|
|
507
528
|
maybe_without_gvl(rb_self.needs_gvl(), || {
|
|
508
|
-
let mut buf = vec![0u8;
|
|
529
|
+
let mut buf = vec![0u8; FILE_READ_BUF];
|
|
509
530
|
let mut total = 0u64;
|
|
510
531
|
loop {
|
|
511
532
|
crate::gvl::check()?;
|
|
@@ -523,6 +544,38 @@ impl Writer {
|
|
|
523
544
|
})
|
|
524
545
|
}
|
|
525
546
|
|
|
547
|
+
/// `raw_copy` of entries `indices` of `archive` in turn, each under
|
|
548
|
+
/// its own name and fields, in one call: a commit that rewrites an
|
|
549
|
+
/// archive copies most entries this way, and one call each (with a
|
|
550
|
+
/// source and its read buffer set up for every one) cost more than
|
|
551
|
+
/// the copying. None may be a ZipCrypto entry (see `copy_zipcrypto`).
|
|
552
|
+
pub fn raw_copy_many(
|
|
553
|
+
ruby: &Ruby,
|
|
554
|
+
rb_self: &Self,
|
|
555
|
+
archive: &Archive,
|
|
556
|
+
indices: Vec<usize>,
|
|
557
|
+
) -> Result<(), Error> {
|
|
558
|
+
let needs_gvl = archive.needs_gvl() || rb_self.needs_gvl();
|
|
559
|
+
let mut source = archive.archive(ruby)?.clone();
|
|
560
|
+
for index in indices {
|
|
561
|
+
let file = source.by_index_raw(index).rb(ruby)?;
|
|
562
|
+
if is_zipcrypto(&file) {
|
|
563
|
+
return Err(Error::new(
|
|
564
|
+
ruby.exception_arg_error(),
|
|
565
|
+
format!("entry {index} is a ZipCrypto entry; copy it with copy_zipcrypto"),
|
|
566
|
+
));
|
|
567
|
+
}
|
|
568
|
+
let known = known_of(&file);
|
|
569
|
+
let name = file.name_raw().to_vec();
|
|
570
|
+
rb_self.add_entry(ruby, needs_gvl, &name, |_, zip| {
|
|
571
|
+
maybe_without_gvl(needs_gvl, || with_zip!(zip, |w| w.raw_copy_file(file)))
|
|
572
|
+
.rb(ruby)?;
|
|
573
|
+
Ok(known)
|
|
574
|
+
})?;
|
|
575
|
+
}
|
|
576
|
+
Ok(())
|
|
577
|
+
}
|
|
578
|
+
|
|
526
579
|
/// Copies entry `index` of `archive` without decompressing it, under
|
|
527
580
|
/// `new_name` if given. `touch` is an optional Hash with `dos_date`,
|
|
528
581
|
/// `dos_time` and `unix_perms` to write instead of the source's; the
|
|
@@ -540,12 +593,8 @@ impl Writer {
|
|
|
540
593
|
// A clone, so the archive's borrow is released before the GVL is.
|
|
541
594
|
let mut source = archive.archive(ruby)?.clone();
|
|
542
595
|
let file = source.by_index_raw(index).rb(ruby)?;
|
|
543
|
-
let zipcrypto =
|
|
544
|
-
let known =
|
|
545
|
-
size: file.size(),
|
|
546
|
-
csize: file.compressed_size(),
|
|
547
|
-
crc: file.crc32(),
|
|
548
|
-
};
|
|
596
|
+
let zipcrypto = is_zipcrypto(&file);
|
|
597
|
+
let known = known_of(&file);
|
|
549
598
|
let name = file.name_raw().to_vec();
|
|
550
599
|
let touch = touch
|
|
551
600
|
.map(|h| EntryOptions::from_hash(ruby, h))
|
|
@@ -680,6 +729,7 @@ impl Writer {
|
|
|
680
729
|
path: String,
|
|
681
730
|
options: EntryOptions,
|
|
682
731
|
}
|
|
732
|
+
let threads = threads.max(1);
|
|
683
733
|
let mut parsed = Vec::with_capacity(items.len());
|
|
684
734
|
for item in items.into_iter() {
|
|
685
735
|
let item = RArray::try_convert(item)?;
|
|
@@ -707,7 +757,7 @@ impl Writer {
|
|
|
707
757
|
})
|
|
708
758
|
.collect::<Result<Vec<_>, Error>>()?;
|
|
709
759
|
let pool = rayon::ThreadPoolBuilder::new()
|
|
710
|
-
.num_threads(threads
|
|
760
|
+
.num_threads(threads)
|
|
711
761
|
.build()
|
|
712
762
|
.map_err(|e| Error::new(ruby.exception_arg_error(), e.to_string()))?;
|
|
713
763
|
rb_self.with_state(ruby, |state, zip| {
|
|
@@ -719,8 +769,8 @@ impl Writer {
|
|
|
719
769
|
// interrupt (see `gvl`) between them. Should a part fail or the
|
|
720
770
|
// call be interrupted, the parts already in are taken back out
|
|
721
771
|
// again, last first, so the batch is added whole or not at all.
|
|
722
|
-
let part_len = threads
|
|
723
|
-
let part_bytes = threads
|
|
772
|
+
let part_len = threads * PARALLEL_PART_PER_THREAD;
|
|
773
|
+
let part_bytes = threads as u64 * PARALLEL_PART_BYTES_PER_THREAD;
|
|
724
774
|
maybe_without_gvl(rb_self.needs_gvl(), || -> ZipResult<()> {
|
|
725
775
|
state.crypto.seal_pending(w)?;
|
|
726
776
|
let mut added = 0;
|
|
@@ -730,9 +780,7 @@ impl Writer {
|
|
|
730
780
|
// files or `part_bytes`, whichever comes first.
|
|
731
781
|
let mut part = Vec::new();
|
|
732
782
|
let mut bytes = 0u64;
|
|
733
|
-
while part.len() < part_len
|
|
734
|
-
&& (part.len() < threads.max(1) || bytes < part_bytes)
|
|
735
|
-
{
|
|
783
|
+
while part.len() < part_len && (part.len() < threads || bytes < part_bytes) {
|
|
736
784
|
let Some(item) = rest.next() else { break };
|
|
737
785
|
bytes += fs::metadata(item.1).map_or(0, |m| m.len());
|
|
738
786
|
part.push(item);
|
|
@@ -769,12 +817,13 @@ impl Writer {
|
|
|
769
817
|
/// by deferring the start (see `OutputStream#rollback_entry`).
|
|
770
818
|
pub fn rollback_entry(ruby: &Ruby, rb_self: &Self) -> Result<(), Error> {
|
|
771
819
|
rb_self.with_state(ruby, |state, zip| {
|
|
772
|
-
|
|
820
|
+
let Zip::Seekable(w) = zip else {
|
|
773
821
|
return Err(Error::new(
|
|
774
822
|
ruby.get_inner(&crate::errors::ERROR),
|
|
775
|
-
"the entry's data has already gone out on a stream that cannot seek,
|
|
823
|
+
"the entry's data has already gone out on a stream that cannot seek, \
|
|
824
|
+
and cannot be taken back",
|
|
776
825
|
));
|
|
777
|
-
}
|
|
826
|
+
};
|
|
778
827
|
let base = state.append.as_ref().map_or(0, |a| a.base);
|
|
779
828
|
if state.count <= base {
|
|
780
829
|
return Err(Error::new(
|
|
@@ -787,9 +836,6 @@ impl Writer {
|
|
|
787
836
|
// no header patch of the entry may land on the next one.
|
|
788
837
|
state.crypto.pending = None;
|
|
789
838
|
state.crypto.patches.retain(|p| p.index != index);
|
|
790
|
-
let Zip::Seekable(w) = zip else {
|
|
791
|
-
unreachable!("a stream was refused above")
|
|
792
|
-
};
|
|
793
839
|
maybe_without_gvl(rb_self.needs_gvl(), || discard_last(w)).rb(ruby)?;
|
|
794
840
|
state.count = index;
|
|
795
841
|
Ok(())
|
data/ext/ziperu/src/zipcrypto.rs
CHANGED
|
@@ -25,7 +25,7 @@ use crate::sink::Sink;
|
|
|
25
25
|
use crate::stream_stats::EntryStats;
|
|
26
26
|
|
|
27
27
|
/// Size of the random header that precedes the data.
|
|
28
|
-
|
|
28
|
+
const HEADER_SIZE: usize = 12;
|
|
29
29
|
|
|
30
30
|
const fn crc_table() -> [u32; 256] {
|
|
31
31
|
let mut table = [0u32; 256];
|
|
@@ -50,14 +50,14 @@ const fn crc_table() -> [u32; 256] {
|
|
|
50
50
|
static CRC_TABLE: [u32; 256] = crc_table();
|
|
51
51
|
|
|
52
52
|
/// The three rolling keys; one per entry, seeded from the password.
|
|
53
|
-
|
|
53
|
+
struct Keys {
|
|
54
54
|
key_0: Wrapping<u32>,
|
|
55
55
|
key_1: Wrapping<u32>,
|
|
56
56
|
key_2: Wrapping<u32>,
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
impl Keys {
|
|
60
|
-
|
|
60
|
+
fn new(password: &[u8]) -> Self {
|
|
61
61
|
let mut keys = Self {
|
|
62
62
|
key_0: Wrapping(0x1234_5678),
|
|
63
63
|
key_1: Wrapping(0x2345_6789),
|
|
@@ -86,7 +86,7 @@ impl Keys {
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
/// Encrypts `buf` in place, advancing the keys with the plaintext.
|
|
89
|
-
|
|
89
|
+
fn encrypt(&mut self, buf: &mut [u8]) {
|
|
90
90
|
for byte in buf {
|
|
91
91
|
let plain = *byte;
|
|
92
92
|
*byte = plain ^ self.stream_byte();
|
|
@@ -97,7 +97,7 @@ impl Keys {
|
|
|
97
97
|
/// The encrypted 12-byte header that opens an entry: 11 random bytes
|
|
98
98
|
/// and a check byte (`check` is the high byte of the DOS time, which
|
|
99
99
|
/// is what readers compare against when general purpose bit 3 is set).
|
|
100
|
-
|
|
100
|
+
fn header(&mut self, check: u8) -> io::Result<[u8; HEADER_SIZE]> {
|
|
101
101
|
let mut header = [0u8; HEADER_SIZE];
|
|
102
102
|
getrandom::fill(&mut header[..HEADER_SIZE - 1]).map_err(|e| {
|
|
103
103
|
io::Error::other(format!("no randomness for the ZipCrypto header: {e}"))
|
|
@@ -115,8 +115,6 @@ pub(crate) const ENCRYPTED_FLAG: u16 = 1;
|
|
|
115
115
|
pub(crate) const DATA_DESCRIPTOR_FLAG: u16 = 8;
|
|
116
116
|
/// "PK\x07\x08", the optional signature that opens a data descriptor.
|
|
117
117
|
pub(crate) const DATA_DESCRIPTOR_SIGNATURE: u32 = 0x0807_4b50;
|
|
118
|
-
/// The ZipCrypto encryption header that precedes the ciphertext.
|
|
119
|
-
pub(crate) const HEADER_LEN: u64 = HEADER_SIZE as u64;
|
|
120
118
|
/// General purpose flag bit 11: the name and comment are UTF-8.
|
|
121
119
|
pub(crate) const LANGUAGE_ENCODING_FLAG: u16 = 1 << 11;
|
|
122
120
|
|
|
@@ -182,7 +180,7 @@ impl Pending {
|
|
|
182
180
|
deflate,
|
|
183
181
|
crc: crc32fast::Hasher::new(),
|
|
184
182
|
size: 0,
|
|
185
|
-
csize:
|
|
183
|
+
csize: HEADER_SIZE as u64,
|
|
186
184
|
};
|
|
187
185
|
Ok((header, pending))
|
|
188
186
|
}
|
|
@@ -307,14 +305,11 @@ mod tests {
|
|
|
307
305
|
use super::*;
|
|
308
306
|
|
|
309
307
|
#[test]
|
|
310
|
-
fn
|
|
311
|
-
// From the crate's tests: the keys after "test" and the first
|
|
312
|
-
// keystream bytes they produce.
|
|
308
|
+
fn encrypting_changes_the_bytes_and_decrypting_restores_them() {
|
|
313
309
|
let mut keys = Keys::new(b"test");
|
|
314
310
|
let mut data = [0u8; 4];
|
|
315
311
|
keys.encrypt(&mut data);
|
|
316
312
|
assert_ne!(data, [0u8; 4]);
|
|
317
|
-
// Encrypting then decrypting with fresh keys gives the plaintext.
|
|
318
313
|
let mut keys = Keys::new(b"pw");
|
|
319
314
|
let mut cipher = *b"hello world";
|
|
320
315
|
keys.encrypt(&mut cipher);
|
|
@@ -327,4 +322,37 @@ mod tests {
|
|
|
327
322
|
}
|
|
328
323
|
assert_eq!(&plain, b"hello world");
|
|
329
324
|
}
|
|
325
|
+
|
|
326
|
+
fn decrypt(keys: &mut Keys, buf: &mut [u8]) {
|
|
327
|
+
for byte in buf {
|
|
328
|
+
let plain = *byte ^ keys.stream_byte();
|
|
329
|
+
keys.update(plain);
|
|
330
|
+
*byte = plain;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
#[test]
|
|
335
|
+
fn property_any_data_and_password_round_trip_in_any_chunks() {
|
|
336
|
+
let mut rng = crate::test_rng::Rng::new(3);
|
|
337
|
+
for case in 0..crate::test_rng::CASES {
|
|
338
|
+
let password = rng.bytes(16);
|
|
339
|
+
let data = rng.bytes(300);
|
|
340
|
+
let check = rng.next() as u8;
|
|
341
|
+
let mut keys = Keys::new(&password);
|
|
342
|
+
let mut header = keys.header(check).expect("randomness");
|
|
343
|
+
let mut cipher = data.clone();
|
|
344
|
+
// Encrypting in pieces is the same as in one go.
|
|
345
|
+
let mut at = 0;
|
|
346
|
+
while at < cipher.len() {
|
|
347
|
+
let end = (at + 1 + rng.below(40)).min(cipher.len());
|
|
348
|
+
keys.encrypt(&mut cipher[at..end]);
|
|
349
|
+
at = end;
|
|
350
|
+
}
|
|
351
|
+
let mut keys = Keys::new(&password);
|
|
352
|
+
decrypt(&mut keys, &mut header);
|
|
353
|
+
assert_eq!(header[HEADER_SIZE - 1], check, "case {case}: check byte");
|
|
354
|
+
decrypt(&mut keys, &mut cipher);
|
|
355
|
+
assert_eq!(cipher, data, "case {case}");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
330
358
|
}
|
data/lib/zip.rb
CHANGED
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
# runs unchanged. If another library already defines `Zip` (rubyzip itself,
|
|
6
6
|
# most likely), the two cannot share the name: load ziperu with
|
|
7
7
|
# `require "ziperu"` and use `Ziperu::` directly instead.
|
|
8
|
-
if defined?(
|
|
8
|
+
if defined?(Zip) && !(defined?(Ziperu) && Zip.equal?(Ziperu))
|
|
9
9
|
raise LoadError, "ziperu cannot alias Zip = Ziperu: a Zip constant is already defined " \
|
|
10
|
-
"(by #{Object.const_source_location(:Zip)&.first ||
|
|
10
|
+
"(by #{Object.const_source_location(:Zip)&.first || "another library"}). " \
|
|
11
11
|
"Remove rubyzip, or require \"ziperu\" and use Ziperu:: instead."
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
require "ziperu"
|
|
15
|
-
Zip = Ziperu unless defined?(
|
|
15
|
+
Zip = Ziperu unless defined?(Zip)
|
|
16
16
|
|
|
17
17
|
# The `zip/...` paths rubyzip's files live at (`require "zip/filesystem"`
|
|
18
18
|
# is the common one) are kept under lib/zip/ as one-line shims onto the
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
# (Copyright (C) 2002-2026 the rubyzip authors).
|
|
5
5
|
|
|
6
6
|
module Ziperu
|
|
7
|
-
# Reads
|
|
7
|
+
# Reads the end-of-central-directory records of an archive, in Ruby, and
|
|
8
|
+
# lists an archive held in an IO (`read_from_stream`).
|
|
8
9
|
#
|
|
9
10
|
# The native core refuses archives whose EOCD declares more entries than
|
|
10
11
|
# the central directory holds, so `Ziperu::File.open` raises on them
|
|
@@ -37,7 +38,6 @@ module Ziperu
|
|
|
37
38
|
@entry_set = EntrySet.new
|
|
38
39
|
@native.entries.each { |meta| @entry_set.push_record(Entry.from_meta(@native, meta)) }
|
|
39
40
|
@size = @entry_set.size
|
|
40
|
-
@cdir_offset = @native.central_directory_start if @native.respond_to?(:central_directory_start)
|
|
41
41
|
self
|
|
42
42
|
end
|
|
43
43
|
|
|
@@ -59,9 +59,7 @@ module Ziperu
|
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
def count_entries(io)
|
|
62
|
-
if IOExtras.path_like?(io)
|
|
63
|
-
return ::File.open(IOExtras.path_of(io), 'rb') { |f| count_entries(f) }
|
|
64
|
-
end
|
|
62
|
+
return ::File.open(IOExtras.path_of(io), 'rb') { |f| count_entries(f) } if IOExtras.path_like?(io)
|
|
65
63
|
|
|
66
64
|
read_eocds(io)
|
|
67
65
|
@size
|
|
@@ -119,9 +117,7 @@ module Ziperu
|
|
|
119
117
|
return if unpack_e_o_c_d(data.slice(eocd_location..-1))
|
|
120
118
|
|
|
121
119
|
zip64_eocd_locator = data.rindex([ZIP64_EOCD_LOCATOR_SIG].pack('V'), eocd_location)
|
|
122
|
-
unless zip64_eocd_locator
|
|
123
|
-
raise Error, 'Zip64 end of central directory locator signature expected but not found'
|
|
124
|
-
end
|
|
120
|
+
raise Error, 'Zip64 end of central directory locator signature expected but not found' unless zip64_eocd_locator
|
|
125
121
|
|
|
126
122
|
zip64_eocd_location = data.rindex([ZIP64_END_OF_CD_SIG].pack('V'), zip64_eocd_locator)
|
|
127
123
|
|
data/lib/ziperu/constants.rb
CHANGED
|
@@ -20,6 +20,14 @@ module Ziperu
|
|
|
20
20
|
|
|
21
21
|
SPLIT_FILE_SIGNATURE = 0x08074b50
|
|
22
22
|
|
|
23
|
+
# The longest a name, comment or extra field can be: each length is a
|
|
24
|
+
# 16-bit field.
|
|
25
|
+
MAX_FIELD_LENGTH = 0xFFFF
|
|
26
|
+
|
|
27
|
+
# A size or offset from here up does not fit its 32-bit field and needs
|
|
28
|
+
# the zip64 form.
|
|
29
|
+
ZIP64_SIZE_LIMIT = 0xFFFFFFFF
|
|
30
|
+
|
|
23
31
|
FILE_TYPE_FILE = 0o10
|
|
24
32
|
FILE_TYPE_DIR = 0o04
|
|
25
33
|
FILE_TYPE_SYMLINK = 0o12
|
data/lib/ziperu/crypto.rb
CHANGED
|
@@ -12,9 +12,9 @@ module Ziperu
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
class TraditionalDecrypter < Decrypter; end
|
|
15
|
+
class TraditionalDecrypter < Decrypter; end # :nodoc:
|
|
16
16
|
|
|
17
|
-
class AESDecrypter < Decrypter
|
|
17
|
+
class AESDecrypter < Decrypter # :nodoc:
|
|
18
18
|
def initialize(password, _strength = nil)
|
|
19
19
|
super(password)
|
|
20
20
|
end
|
|
@@ -39,10 +39,17 @@ module Ziperu
|
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
# No encryption.
|
|
42
43
|
class NullEncrypter < Encrypter
|
|
43
44
|
def initialize
|
|
44
45
|
super(nil)
|
|
45
46
|
end
|
|
47
|
+
|
|
48
|
+
# rubyzip's compressors (Deflater, PassThruCompressor) pass their
|
|
49
|
+
# output through the encrypter.
|
|
50
|
+
def encrypt(data) # :nodoc:
|
|
51
|
+
data
|
|
52
|
+
end
|
|
46
53
|
end
|
|
47
54
|
|
|
48
55
|
# ZipCrypto, the weak legacy cipher that Windows Explorer and other
|
|
@@ -55,6 +62,7 @@ module Ziperu
|
|
|
55
62
|
end
|
|
56
63
|
end
|
|
57
64
|
|
|
65
|
+
# WinZip AES; 256-bit unless another strength is given.
|
|
58
66
|
class AESEncrypter < Encrypter
|
|
59
67
|
include AESEncryption
|
|
60
68
|
|
data/lib/ziperu/dos_time.rb
CHANGED
|
@@ -77,7 +77,7 @@ module Ziperu
|
|
|
77
77
|
hour = (0b1111100000000000 & bin_dos_time) >> 11
|
|
78
78
|
day = (0b11111 & bin_dos_date)
|
|
79
79
|
month = (0b111100000 & bin_dos_date) >> 5
|
|
80
|
-
year = ((0b1111111000000000 & bin_dos_date) >> 9) +
|
|
80
|
+
year = ((0b1111111000000000 & bin_dos_date) >> 9) + DOS_EPOCH_YEAR
|
|
81
81
|
|
|
82
82
|
time = local(year, month, day, hour, minute, second)
|
|
83
83
|
time.absolute_time = false
|
data/lib/ziperu/entry/source.rb
CHANGED
|
@@ -52,6 +52,11 @@ module Ziperu
|
|
|
52
52
|
stored&.native(offset)
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
+
# The password its owner was given for the archive, or nil.
|
|
56
|
+
def password
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
|
|
55
60
|
# The record's local-header extra fields, as stored.
|
|
56
61
|
def local_extra(offset)
|
|
57
62
|
stored.local_extra(offset)
|
|
@@ -69,8 +74,9 @@ module Ziperu
|
|
|
69
74
|
|
|
70
75
|
NONE = None.new.freeze
|
|
71
76
|
|
|
72
|
-
# A record of `native`, the archive `owner`
|
|
73
|
-
# for an entry
|
|
77
|
+
# A record of `native`, the archive `owner` has open: a Ziperu::File,
|
|
78
|
+
# a Reopener for an entry `File.foreach` yielded from a path, or nil
|
|
79
|
+
# for an entry read through an InputStream. `index` is nil
|
|
74
80
|
# for a record the native core cannot address by index (one of
|
|
75
81
|
# several under the same name), which is found by its offset.
|
|
76
82
|
class Archive
|
|
@@ -89,11 +95,14 @@ module Ziperu
|
|
|
89
95
|
self
|
|
90
96
|
end
|
|
91
97
|
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
def password
|
|
99
|
+
@owner.password if @owner.respond_to?(:password)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The native archive, reopened if its owner released it on close:
|
|
103
|
+
# rubyzip lets entries be read after `close`.
|
|
94
104
|
def native(offset)
|
|
95
|
-
if @native.closed? && @owner.
|
|
96
|
-
reopened = @owner.native_archive
|
|
105
|
+
if @native.closed? && (reopened = (@owner.native_archive if @owner.respond_to?(:native_archive)))
|
|
97
106
|
unless still_at_place?(reopened, offset)
|
|
98
107
|
where = @index ? "at index #{@index} of" : 'in'
|
|
99
108
|
raise EntryMovedError, "entry '#{@raw_name}' is no longer #{where} #{@owner.name}"
|
|
@@ -132,6 +141,32 @@ module Ziperu
|
|
|
132
141
|
end
|
|
133
142
|
end
|
|
134
143
|
|
|
144
|
+
# Stands in for a Ziperu::File as the owner of the entries that
|
|
145
|
+
# `File.foreach` yields: it has the password given for the archive,
|
|
146
|
+
# and for an archive at a path it reopens it, so the entries can be
|
|
147
|
+
# read once the walk is over (`foreach(path).find { ... }`, or
|
|
148
|
+
# entries collected in the block). One is shared by all the entries
|
|
149
|
+
# of a walk, and so is the archive it reopens.
|
|
150
|
+
class Reopener
|
|
151
|
+
attr_reader :name, :password
|
|
152
|
+
|
|
153
|
+
# `name` is the archive's path, or nil for one read from an IO,
|
|
154
|
+
# which cannot be opened again.
|
|
155
|
+
def initialize(name, settings, password = nil)
|
|
156
|
+
@name = name
|
|
157
|
+
@settings = settings
|
|
158
|
+
@password = password
|
|
159
|
+
@native = nil
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def native_archive
|
|
163
|
+
return if @name.nil?
|
|
164
|
+
|
|
165
|
+
@native = InputStream.open_archive(@name, @settings) if @native.nil? || @native.closed?
|
|
166
|
+
@native
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
135
170
|
# Data still outside the archive.
|
|
136
171
|
class Pending
|
|
137
172
|
include Defaults
|