| 1 | // Copyright (c) 2022 Harry [Majored] [[email protected]] |
| 2 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) |
| 3 | |
| 4 | //! <https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#4316> |
| 5 | //! |
| 6 | //! As with other ZIP libraries, we face the predicament that the end of central directory record may contain a |
| 7 | //! variable-length file comment. As a result, we cannot just make the assumption that the start of this record is |
| 8 | //! 18 bytes (the length of the EOCDR) offset from the end of the data - we must locate it ourselves. |
| 9 | //! |
| 10 | //! The `zip-rs` crate handles this by reading in reverse from the end of the data. This involves seeking backwards |
| 11 | //! by a single byte each iteration and reading 4 bytes into a u32. Whether this is performant/acceptable within a |
| 12 | //! a non-async context, I'm unsure, but it isn't desirable within an async context. Especially since we cannot just |
| 13 | //! place a [`BufReader`] infront of the upstream reader (as its internal buffer is invalidated on each seek). |
| 14 | //! |
| 15 | //! Reading in reverse is still desirable as the use of file comments is limited and they're unlikely to be large. |
| 16 | //! |
| 17 | //! The below method is one that compromises on these two contention points. Please submit an issue or PR if you know |
| 18 | //! of a better algorithm for this (and have tested/verified its performance). |
| 19 | |
| 20 | #[cfg(doc)] |
| 21 | use futures_literust-analyzer cargo futures-lite 2.1.0 crate/::iorust-analyzer cargo futures-lite 2.1.0 io/::BufReaderrust-analyzer cargo futures-lite 2.1.0 io/BufReader#; |
| 22 | |
| 23 | use craterust-analyzer cargo async_zip 0.0.18 crate/::errorrust-analyzer cargo async_zip 0.0.18 error/::{Resultrust-analyzer cargo async_zip 0.0.18 error/Result# as ZipResultrust-analyzer cargo async_zip 0.0.18 error/Result#, ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#}; |
| 24 | use craterust-analyzer cargo async_zip 0.0.18 crate/::specrust-analyzer cargo async_zip 0.0.18 spec/::constsrust-analyzer cargo async_zip 0.0.18 spec/consts/::{EOCDR_LENGTHrust-analyzer cargo async_zip 0.0.18 spec/consts/EOCDR_LENGTH., EOCDR_SIGNATURErust-analyzer cargo async_zip 0.0.18 spec/consts/EOCDR_SIGNATURE., SIGNATURE_LENGTHrust-analyzer cargo async_zip 0.0.18 spec/consts/SIGNATURE_LENGTH.}; |
| 25 | |
| 26 | use futures_literust-analyzer cargo futures-lite 2.1.0 crate/::iorust-analyzer cargo futures-lite 2.1.0 io/::{AsyncReadrust-analyzer cargo futures-io 0.3.25 if_std/AsyncRead#, AsyncReadExtrust-analyzer cargo futures-lite 2.1.0 io/AsyncReadExt#, AsyncSeekrust-analyzer cargo futures-io 0.3.25 if_std/AsyncSeek#, AsyncSeekExtrust-analyzer cargo futures-lite 2.1.0 io/AsyncSeekExt#, SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#}; |
| 27 | |
| 28 | /// The buffer size used when locating the EOCDR, equal to 2KiB. |
| 29 | const BUFFER_SIZErust-analyzer cargo async_zip 0.0.18 base/read/io/locator/BUFFER_SIZE.: usize = 2048; |
| 30 | |
| 31 | /// The upper bound of where the EOCDR signature cannot be located. |
| 32 | const EOCDR_UPPER_BOUNDrust-analyzer cargo async_zip 0.0.18 base/read/io/locator/EOCDR_UPPER_BOUND.: u64 = EOCDR_LENGTHrust-analyzer cargo async_zip 0.0.18 spec/consts/EOCDR_LENGTH. as u64; |
| 33 | |
| 34 | /// The lower bound of where the EOCDR signature cannot be located. |
| 35 | const EOCDR_LOWER_BOUNDrust-analyzer cargo async_zip 0.0.18 base/read/io/locator/EOCDR_LOWER_BOUND.: u64 = EOCDR_UPPER_BOUNDrust-analyzer cargo async_zip 0.0.18 base/read/io/locator/EOCDR_UPPER_BOUND. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().SIGNATURE_LENGTHrust-analyzer cargo async_zip 0.0.18 spec/consts/SIGNATURE_LENGTH. as u64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().u16::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]MAX. as u64; |
| 36 | |
| 37 | /// Locate the `end of central directory record` offset, if one exists. |
| 38 | /// The returned offset excludes the signature (4 bytes) |
| 39 | /// |
| 40 | /// This method involves buffered reading in reverse and reverse linear searching along those buffers for the EOCDR |
| 41 | /// signature. As a result of this buffered approach, we reduce seeks when compared to `zip-rs`'s method by a factor |
| 42 | /// of the buffer size. We also then don't have to do individual u32 reads against the upstream reader. |
| 43 | /// |
| 44 | /// Whilst I haven't done any in-depth benchmarks, when reading a ZIP file with the maximum length comment, this method |
| 45 | /// saw a reduction in location time by a factor of 500 when compared with the `zip-rs` method. |
| 46 | pub async fn eocdrrust-analyzer cargo async_zip 0.0.18 base/read/io/locator/eocdr().<R>(mut reader: R) -> ZipResultrust-analyzer cargo async_zip 0.0.18 error/Result#<u64> |
| 47 | where |
| 48 | R: AsyncReadrust-analyzer cargo futures-io 0.3.25 if_std/AsyncRead# + AsyncSeekrust-analyzer cargo futures-io 0.3.25 if_std/AsyncSeek# + Unpinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Unpin#, |
| 49 | { |
| 50 | let length = reader.seekrust-analyzer cargo futures-lite 2.1.0 io/AsyncSeekExt#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#End#(0)).rust-analyzer cargo futures-lite 2.1.0 io/impl#[`SeekFuture<'_, S>`][Future]poll().awaitrust-analyzer cargo futures-lite 2.1.0 io/impl#[`SeekFuture<'_, S>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 51 | let signature = &EOCDR_SIGNATURErust-analyzer cargo async_zip 0.0.18 spec/consts/EOCDR_SIGNATURE..to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_le_bytes().(); |
| 52 | let mut buffer: [u8; BUFFER_SIZErust-analyzer cargo async_zip 0.0.18 base/read/io/locator/BUFFER_SIZE.] = [0; BUFFER_SIZErust-analyzer cargo async_zip 0.0.18 base/read/io/locator/BUFFER_SIZE.]; |
| 53 | |
| 54 | let mut position = length.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(BUFFER_SIZErust-analyzer cargo async_zip 0.0.18 base/read/io/locator/BUFFER_SIZE. as u64); |
| 55 | reader.seekrust-analyzer cargo futures-lite 2.1.0 io/AsyncSeekExt#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(position)).rust-analyzer cargo futures-lite 2.1.0 io/impl#[`SeekFuture<'_, S>`][Future]poll().awaitrust-analyzer cargo futures-lite 2.1.0 io/impl#[`SeekFuture<'_, S>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 56 | |
| 57 | loop { |
| 58 | let mut read = 0; |
| 59 | |
| 60 | // Read repeatedly until eof or the buffer is full. |
| 61 | while read rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().BUFFER_SIZErust-analyzer cargo async_zip 0.0.18 base/read/io/locator/BUFFER_SIZE. { |
| 62 | let bytes_read = reader.readrust-analyzer cargo futures-lite 2.1.0 io/AsyncReadExt#read().(&mut buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().read..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().).rust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadFuture<'_, R>`][Future]poll().awaitrust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadFuture<'_, R>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 63 | if bytes_read rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 64 | break; |
| 65 | } |
| 66 | read rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().bytes_read; |
| 67 | } |
| 68 | |
| 69 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(match_index) = reverse_search_bufferrust-analyzer cargo async_zip 0.0.18 base/read/io/locator/reverse_search_buffer().(&buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#read]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., signature) { |
| 70 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(position rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().(match_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) as u64); |
| 71 | } |
| 72 | |
| 73 | // If we hit the start of the data or the lower bound, we're unable to locate the EOCDR. |
| 74 | if position rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().0 || position rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]le().length.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(EOCDR_LOWER_BOUNDrust-analyzer cargo async_zip 0.0.18 base/read/io/locator/EOCDR_LOWER_BOUND.) { |
| 75 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::UnableToLocateEOCDRrust-analyzer cargo async_zip 0.0.18 error/ZipError#UnableToLocateEOCDR#); |
| 76 | } |
| 77 | |
| 78 | // To handle the case where the EOCDR signature crosses buffer boundaries, we simply overlap reads by the |
| 79 | // signature length. This significantly reduces the complexity of handling partial matches with very little |
| 80 | // overhead. |
| 81 | position = position.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().((BUFFER_SIZErust-analyzer cargo async_zip 0.0.18 base/read/io/locator/BUFFER_SIZE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().SIGNATURE_LENGTHrust-analyzer cargo async_zip 0.0.18 spec/consts/SIGNATURE_LENGTH.) as u64); |
| 82 | reader.seekrust-analyzer cargo futures-lite 2.1.0 io/AsyncSeekExt#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(position)).rust-analyzer cargo futures-lite 2.1.0 io/impl#[`SeekFuture<'_, S>`][Future]poll().awaitrust-analyzer cargo futures-lite 2.1.0 io/impl#[`SeekFuture<'_, S>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// A naive reverse linear search along the buffer for the specified signature bytes. |
| 87 | /// |
| 88 | /// This is already surprisingly performant. For instance, using memchr::memchr() to match for the first byte of the |
| 89 | /// signature, and then manual byte comparisons for the remaining signature bytes was actually slower by a factor of |
| 90 | /// 2.25. This method was explored as tokio's `read_until()` implementation uses memchr::memchr(). |
| 91 | pub(craterust-analyzer cargo async_zip 0.0.18 crate/) fn reverse_search_bufferrust-analyzer cargo async_zip 0.0.18 base/read/io/locator/reverse_search_buffer().(buffer: &[u8], signature: &[u8]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> { |
| 92 | 'outer: for index in (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#buffer.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().() { |
| 93 | for (signature_index, signature_byte) in signature.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 94 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next_index) = index.checked_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_sub().(signature_index) { |
| 95 | if buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().next_index]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().signature_byte { |
| 96 | continue 'outer; |
| 97 | } |
| 98 | } else { |
| 99 | break 'outer; |
| 100 | } |
| 101 | } |
| 102 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(index); |
| 103 | } |
| 104 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 105 | } |