-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add support to iterator over encrypted zip #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alandeev
wants to merge
5
commits into
OSSystems:master
Choose a base branch
from
alandeev:feat/add-support-to-iterate-over-encrypted-zip
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c911c4f
feat: add support to iterator over encrypted zip
alandeev 1f3a8ac
fixup! feat: add support to iterator over encrypted zip
otavio f01be1a
fixup! feat: add support to iterator over encrypted zip
otavio a6098c8
fixup! feat: add support to iterator over encrypted zip
otavio c775c93
fixup! feat: add support to iterator over encrypted zip
otavio 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -44,6 +44,20 @@ pub enum ArchiveContents { | |
/// The entry is processed on a return value of `true` and ignored on `false`. | ||
pub type EntryFilterCallbackFn = dyn Fn(&str, &libc::stat) -> bool; | ||
|
||
pub struct Password(Vec<u8>); | ||
|
||
impl Password { | ||
pub fn extract(&self) -> *const i8 { | ||
self.0.as_ptr() as *const i8 | ||
} | ||
} | ||
|
||
impl<T> From<T> for Password where T: AsRef<str> { | ||
fn from(s: T) -> Self { | ||
Self(s.as_ref().as_bytes().to_vec()) | ||
} | ||
} | ||
|
||
/// An iterator over the contents of an archive. | ||
#[allow(clippy::module_name_repetitions)] | ||
pub struct ArchiveIterator<R: Read + Seek> { | ||
|
@@ -119,6 +133,7 @@ impl<R: Read + Seek> ArchiveIterator<R> { | |
source: R, | ||
decode: DecodeCallback, | ||
filter: Option<Box<EntryFilterCallbackFn>>, | ||
password: Option<Password>, | ||
) -> Result<ArchiveIterator<R>> | ||
where | ||
R: Read + Seek, | ||
|
@@ -132,6 +147,10 @@ impl<R: Read + Seek> ArchiveIterator<R> { | |
let archive_entry: *mut ffi::archive_entry = std::ptr::null_mut(); | ||
let archive_reader = ffi::archive_read_new(); | ||
|
||
if let Some(password) = password { | ||
ffi::archive_read_add_passphrase(archive_reader, password.extract()); | ||
} | ||
|
||
let res = (|| { | ||
archive_result( | ||
ffi::archive_read_support_filter_all(archive_reader), | ||
|
@@ -230,7 +249,7 @@ impl<R: Read + Seek> ArchiveIterator<R> { | |
where | ||
R: Read + Seek, | ||
{ | ||
Self::new(source, decode, None) | ||
Self::new(source, decode, None, None) | ||
} | ||
|
||
/// Iterate over the contents of an archive, streaming the contents of each | ||
|
@@ -245,7 +264,7 @@ impl<R: Read + Seek> ArchiveIterator<R> { | |
/// | ||
/// let mut name = String::default(); | ||
/// let mut size = 0; | ||
/// let mut iter = ArchiveIterator::from_read(file)?; | ||
/// let mut iter = ArchiveIterator::from_read(file, None)?; | ||
/// | ||
/// for content in &mut iter { | ||
/// match content { | ||
|
@@ -265,11 +284,11 @@ impl<R: Read + Seek> ArchiveIterator<R> { | |
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn from_read(source: R) -> Result<ArchiveIterator<R>> | ||
pub fn from_read(source: R, password: Option<Password>) -> Result<ArchiveIterator<R>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to have two methods:
|
||
where | ||
R: Read + Seek, | ||
{ | ||
Self::new(source, crate::decode_utf8, None) | ||
Self::new(source, crate::decode_utf8, None, password) | ||
} | ||
|
||
/// Close the iterator, freeing up the associated resources. | ||
|
@@ -392,6 +411,7 @@ where | |
source: R, | ||
decoder: DecodeCallback, | ||
filter: Option<Box<EntryFilterCallbackFn>>, | ||
password: Option<Password>, | ||
} | ||
|
||
/// A builder to generate an archive iterator over the contents of an | ||
|
@@ -430,6 +450,7 @@ where | |
source, | ||
decoder: crate::decode_utf8, | ||
filter: None, | ||
password: None, | ||
} | ||
} | ||
|
||
|
@@ -450,8 +471,14 @@ where | |
self | ||
} | ||
|
||
/// Set a custom password to decode content of archive entries. | ||
pub fn with_password(mut self, password: Password) -> ArchiveIteratorBuilder<R> { | ||
self.password = Some(password); | ||
self | ||
} | ||
|
||
/// Finish the builder and generate the configured `ArchiveIterator`. | ||
pub fn build(self) -> Result<ArchiveIterator<R>> { | ||
ArchiveIterator::new(self.source, self.decoder, self.filter) | ||
ArchiveIterator::new(self.source, self.decoder, self.filter, self.password) | ||
} | ||
} |
This file contains hidden or 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
Binary file not shown.
This file contains hidden or 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
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.
Needs documentation.
The type would be better named as
ArchivePassword
?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.
I renamed, doc is still pending.