This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Zero copy central directory parsing #91
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
05e9702
Read files inside ZipArchive lazily.
2e5b29e
Don't need to seek on every iteration.
a1eb92c
Reduce number of seeks.
72ae3e3
Poison the archive if reading a central file header fails.
8ad6bd4
zero-copy central directory parsing
srijs 5d5491d
improve bounds checking for central directory header parsing
srijs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use std::borrow::{Borrow, Cow}; | ||
use std::io::Read; | ||
use std::ops::Deref; | ||
use std::hash::{Hash, Hasher}; | ||
|
||
use bytes::{Buf, Bytes}; | ||
use string; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
pub(crate) struct StrBuf(string::String<Bytes>); | ||
|
||
impl StrBuf { | ||
pub(crate) fn from_str(s: &str) -> Self { | ||
StrBuf(string::String::from_str(s)) | ||
} | ||
|
||
pub(crate) fn from_utf8(bytes: ByteBuf) -> Result<Self, ::std::str::Utf8Error> { | ||
Ok(StrBuf(string::TryFrom::try_from(bytes.0)?)) | ||
} | ||
|
||
pub(crate) fn from_utf8_lossy(bytes: ByteBuf) -> Self { | ||
match String::from_utf8_lossy(bytes.as_ref()) { | ||
Cow::Owned(s) => s.into(), | ||
Cow::Borrowed(s) => { | ||
// SAFETY: We know that `bytes` only contains valid utf-8, | ||
// since the `from_utf8_lossy` operation returned the | ||
// input verbatim. | ||
debug_assert_eq!(s.len(), bytes.len()); | ||
StrBuf(unsafe { string::String::from_utf8_unchecked(bytes.clone().0) }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl From<String> for StrBuf { | ||
fn from(s: String) -> Self { | ||
let bytes = s.into_bytes().into(); | ||
// SAFETY: We know that `bytes` only contains valid utf-8, | ||
// since the underlying data comes from the input string. | ||
StrBuf(unsafe { string::String::from_utf8_unchecked(bytes) }) | ||
} | ||
} | ||
|
||
impl Hash for StrBuf { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
// Because of the impl Borrow<str> for StrBuf, we need to make sure that the Hash | ||
// implementations behave identically between str and StrBuf. | ||
// | ||
// Quoting the documentation for the Borrow trait: | ||
// | ||
// > Further, when providing implementations for additional traits, it needs to be | ||
// > considered whether they should behave identical to those of the underlying type as a | ||
// > consequence of acting as a representation of that underlying type. | ||
// > Generic code typically uses Borrow<T> when it relies on the identical behavior of | ||
// > these additional trait implementations. | ||
// > These traits will likely appear as additional trait bounds. | ||
// | ||
// Without this, it would be impossible to look up an entry from the names_map by &str, | ||
// since the str and StrBuf would evaluate to different hashes, even if they represent the | ||
// same sequence of characters. | ||
str::hash(&*self, state) | ||
} | ||
} | ||
|
||
impl Borrow<str> for StrBuf { | ||
fn borrow(&self) -> &str { | ||
self.0.borrow() | ||
} | ||
} | ||
|
||
impl Deref for StrBuf { | ||
type Target = str; | ||
|
||
fn deref(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct ByteBuf(Bytes); | ||
|
||
impl ByteBuf { | ||
pub(crate) fn len(&self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
pub(crate) fn split_to(&mut self, at: usize) -> ByteBuf { | ||
ByteBuf(self.0.split_to(at)) | ||
} | ||
} | ||
|
||
impl Buf for ByteBuf { | ||
fn remaining(&self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
fn bytes(&self) -> &[u8] { | ||
self.0.as_ref() | ||
} | ||
|
||
fn advance(&mut self, cnt: usize) { | ||
self.0.advance(cnt) | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for ByteBuf { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl Read for ByteBuf { | ||
fn read(&mut self, buf: &mut [u8]) -> ::std::io::Result<usize> { | ||
self.reader().read(buf) | ||
} | ||
} | ||
|
||
impl From<Vec<u8>> for ByteBuf { | ||
fn from(vec: Vec<u8>) -> Self { | ||
ByteBuf(vec.into()) | ||
} | ||
} | ||
|
||
impl From<Bytes> for ByteBuf { | ||
fn from(bytes: Bytes) -> Self { | ||
ByteBuf(bytes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
#[derive]
it? Is there a benefit to matching thestr
's hash rather than theBytes
's hash (whichString<Bytes>
'sHash
forwards to)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the
impl Borrow<str> for StrBuf
, I need to make sure that the hash implementations behave identically betweenstr
andStrBuf
.Quoting the documentation for the
Borrow
trait:Without this, it would be impossible to look up an entry from the
names_map
by&str
, since thestr
andStrBuf
would evaluate to different hashes, even if they represent the same sequence of chars.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't notice the
Borrow<str>
impl.