Skip to content

Commit

Permalink
🐎 Avoid unnecessary heap allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Sep 28, 2023
1 parent a1dc687 commit 2d5ca27
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
19 changes: 5 additions & 14 deletions lib/src/archive/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,26 +387,17 @@ fn decrypt_reader<R: Read>(
Encryption::No => DecryptReader::No(reader),
encryption @ Encryption::Aes | encryption @ Encryption::Camellia => {
let s = phsf.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
String::from("Item is encrypted, but `PHSF` chunk not found"),
)
io::Error::new(io::ErrorKind::InvalidData, "`PHSF` chunk not found")
})?;
let phsf = verify_password(
s,
password.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
String::from("Item is encrypted, but password was not provided"),
)
io::Error::new(io::ErrorKind::InvalidInput, "Password was not provided")
})?,
)?;
let hash = phsf.hash.ok_or_else(|| {
io::Error::new(
io::ErrorKind::Unsupported,
String::from("Failed to get hash"),
)
})?;
let hash = phsf
.hash
.ok_or_else(|| io::Error::new(io::ErrorKind::Unsupported, "Failed to get hash"))?;
match (encryption, cipher_mode) {
(Encryption::Aes, CipherMode::CBC) => {
DecryptReader::CbcAes(DecryptCbcAes256Reader::new(reader, hash.as_bytes())?)
Expand Down
10 changes: 4 additions & 6 deletions lib/src/archive/entry/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ fn hash<'s, 'p: 's>(
))
}
}?;
let hash = password_hash.hash.take().ok_or_else(|| {
io::Error::new(
io::ErrorKind::Unsupported,
String::from("Failed to get hash"),
)
})?;
let hash = password_hash
.hash
.take()
.ok_or_else(|| io::Error::new(io::ErrorKind::Unsupported, "Failed to get hash"))?;
Ok((hash, password_hash.to_string()))
}

Expand Down
5 changes: 1 addition & 4 deletions lib/src/chunk/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ impl<R: Read> ChunkReader<R> {
let crc = u32::from_be_bytes(crc);

if crc != crc_hasher.finalize() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
String::from("crc check failed. broken chunk"),
));
return Err(io::Error::new(io::ErrorKind::InvalidData, "Broken chunk"));
}
Ok(RawChunk {
length,
Expand Down

0 comments on commit 2d5ca27

Please sign in to comment.