-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
428 additions
and
172 deletions.
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
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
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
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,44 @@ | ||
use std::io::{BufReader, BufWriter, Read, Seek, Write}; | ||
|
||
use zip::result::ZipError; | ||
use zip::{ZipArchive, ZipWriter}; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub(crate) enum CopyZipError { | ||
#[error("could not read input ZIP")] | ||
InputZip(#[source] ZipError), | ||
#[error("could not read file #{1} inside input ZIP")] | ||
InputZipIndex(#[source] ZipError, usize), | ||
#[error("could not write to output")] | ||
OutputWrite(#[source] std::io::Error), | ||
#[error("could not write ZIP file #{1} to output")] | ||
OutputZip(#[source] ZipError, usize), | ||
#[error("could not finish writing output ZIP")] | ||
OutputZipFinish(#[source] ZipError), | ||
} | ||
|
||
pub(crate) fn copy_zip<I, O>(input: &mut I, output: &mut O) -> Result<(), CopyZipError> | ||
where | ||
I: ?Sized + Seek + Read, | ||
O: ?Sized + Seek + Write, | ||
{ | ||
let mut input = ZipArchive::new(BufReader::new(input)).map_err(CopyZipError::InputZip)?; | ||
let mut output = ZipWriter::new(BufWriter::new(output)); | ||
|
||
output.set_raw_comment(input.comment().to_owned()); | ||
for idx in 0..input.len() { | ||
let file = input | ||
.by_index_raw(idx) | ||
.map_err(|err| CopyZipError::InputZipIndex(err, idx))?; | ||
output | ||
.raw_copy_file(file) | ||
.map_err(|err| CopyZipError::OutputZip(err, idx))?; | ||
} | ||
output | ||
.finish() | ||
.map_err(CopyZipError::OutputZipFinish)? | ||
.flush() | ||
.map_err(CopyZipError::OutputWrite)?; | ||
|
||
Ok(()) | ||
} |
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,11 @@ | ||
//! Functions to remove signatures from a file | ||
|
||
#[cfg(feature = "unsign-tar")] | ||
mod tar; | ||
#[cfg(feature = "unsign-zip")] | ||
mod zip; | ||
|
||
#[cfg(feature = "unsign-tar")] | ||
pub use self::tar::{copy_and_unsign_tar, UnsignTarError}; | ||
#[cfg(feature = "unsign-zip")] | ||
pub use self::zip::{copy_and_unsign_zip, UnsignZipError}; |
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,42 @@ | ||
#![cfg_attr(docsrs, doc(cfg(feature = "unsign-tar")))] | ||
|
||
use std::io::{copy, Read, Seek, Write}; | ||
|
||
use crate::verify_unsign_tar::{ | ||
tar_find_data_start_and_len, tar_read_signatures, TarFindDataStartAndLenError, | ||
TarReadSignaturesError, | ||
}; | ||
|
||
crate::Error! { | ||
/// An error returned by [`copy_and_unsign_tar()`] | ||
pub struct UnsignTarError(Error) { | ||
#[error("could not copy data")] | ||
Copy(#[source] std::io::Error), | ||
#[error("could not find read signatures in .tar.gz file")] | ||
FindDataStartAndLen(#[source] TarFindDataStartAndLenError), | ||
#[error("could not find read signatures in .tar.gz file")] | ||
ReadSignatures(#[source] TarReadSignaturesError), | ||
#[error("could not seek inside the input")] | ||
Seek(#[source] std::io::Error), | ||
} | ||
} | ||
|
||
/// Copy a signed `.tar.gz` file without the signatures | ||
pub fn copy_and_unsign_tar<I, O>(input: &mut I, output: &mut O) -> Result<(), UnsignTarError> | ||
where | ||
I: ?Sized + Seek + Read, | ||
O: ?Sized + Seek + Write, | ||
{ | ||
// seek to start of base64 encoded signatures | ||
let (data_start, data_len) = | ||
tar_find_data_start_and_len(input).map_err(Error::FindDataStartAndLen)?; | ||
|
||
// read base64 encoded signatures | ||
let _ = tar_read_signatures(data_start, data_len, input).map_err(Error::ReadSignatures)?; | ||
|
||
// copy data | ||
input.rewind().map_err(Error::Seek)?; | ||
let _ = copy(&mut input.take(data_start), output).map_err(Error::Copy)?; | ||
|
||
Ok(()) | ||
} |
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,23 @@ | ||
#![cfg_attr(docsrs, doc(cfg(feature = "unsign-zip")))] | ||
|
||
use std::io::{Read, Seek, Write}; | ||
|
||
use crate::sign_unsign_zip::{copy_zip, CopyZipError}; | ||
|
||
crate::Error! { | ||
/// An error returned by [`copy_and_unsign_zip()`] | ||
pub struct UnsignZipError(Error) { | ||
#[error("could not copy ZIP data")] | ||
Copy(#[source] CopyZipError), | ||
} | ||
} | ||
|
||
/// Copy a signed `.zip` file without the signatures | ||
pub fn copy_and_unsign_zip<I, O>(input: &mut I, output: &mut O) -> Result<(), UnsignZipError> | ||
where | ||
I: ?Sized + Read + Seek, | ||
O: ?Sized + Read + Seek + Write, | ||
{ | ||
copy_zip(input, output).map_err(Error::Copy)?; | ||
Ok(()) | ||
} |
Oops, something went wrong.