diff --git a/repak/src/lib.rs b/repak/src/lib.rs index 7c1f99b..e6ea5a1 100644 --- a/repak/src/lib.rs +++ b/repak/src/lib.rs @@ -131,12 +131,9 @@ pub(crate) enum Key { None, } -impl From> for Key { - fn from(value: Option) -> Self { - match value { - #[cfg(feature = "encryption")] - Some(key) => Self::Some(key), - _ => Self::None, - } +#[cfg(feature = "encryption")] +impl From for Key { + fn from(value: aes::Aes256) -> Self { + Self::Some(value) } } diff --git a/repak/src/pak.rs b/repak/src/pak.rs index c5de066..730f233 100644 --- a/repak/src/pak.rs +++ b/repak/src/pak.rs @@ -83,7 +83,6 @@ fn decrypt(key: &super::Key, bytes: &mut [u8]) -> Result<(), super::Error> { } impl PakReader { - #[cfg(not(feature = "encryption"))] pub fn new_any(reader: &mut R) -> Result { Self::new_any_inner(reader, super::Key::None) } @@ -91,11 +90,22 @@ impl PakReader { #[cfg(feature = "encryption")] pub fn new_any_with_key( reader: &mut R, - key: Option, + key: aes::Aes256, ) -> Result { Self::new_any_inner(reader, key.into()) } + #[cfg(feature = "encryption")] + pub fn new_any_with_optional_key( + reader: &mut R, + key: Option, + ) -> Result { + match key { + Some(key) => Self::new_any_with_key(reader, key), + None => Self::new_any(reader), + } + } + fn new_any_inner( reader: &mut R, key: super::Key, @@ -112,23 +122,34 @@ impl PakReader { Err(super::Error::UnsupportedOrEncrypted(log)) } - #[cfg(not(feature = "encryption"))] pub fn new( reader: &mut R, version: super::Version, ) -> Result { - Self::new_inner(reader, version) + Self::new_inner(reader, version, super::Key::None) } #[cfg(feature = "encryption")] pub fn new_with_key( reader: &mut R, version: super::Version, - key: Option, + key: aes::Aes256, ) -> Result { Self::new_inner(reader, version, key.into()) } + #[cfg(feature = "encryption")] + pub fn new_with_optional_key( + reader: &mut R, + version: super::Version, + key: Option, + ) -> Result { + match key { + Some(key) => Self::new_with_key(reader, version, key), + None => Self::new(reader, version), + } + } + fn new_inner( reader: &mut R, version: super::Version, @@ -195,7 +216,6 @@ impl PakReader { } impl PakWriter { - #[cfg(not(feature = "encryption"))] pub fn new( writer: W, version: Version, @@ -212,7 +232,7 @@ impl PakWriter { #[cfg(feature = "encryption")] pub fn new_with_key( writer: W, - key: Option, + key: aes::Aes256, version: Version, mount_point: String, path_hash_seed: Option, @@ -224,6 +244,20 @@ impl PakWriter { } } + #[cfg(feature = "encryption")] + pub fn new_with_optional_key( + writer: W, + key: Option, + version: Version, + mount_point: String, + path_hash_seed: Option, + ) -> Self { + match key { + Some(key) => Self::new_with_key(writer, key, version, mount_point, path_hash_seed), + None => Self::new(writer, version, mount_point, path_hash_seed), + } + } + fn new_inner( writer: W, key: super::Key, diff --git a/repak/tests/test.rs b/repak/tests/test.rs index 1355ac5..2c0c06a 100644 --- a/repak/tests/test.rs +++ b/repak/tests/test.rs @@ -96,7 +96,7 @@ fn test_read(version: repak::Version, _file_name: &str, bytes: &[u8]) { let len = inner_reader.seek(SeekFrom::End(0)).unwrap(); let mut reader = ReadCounter::new_size(inner_reader, len as usize); - let pak = repak::PakReader::new_any_with_key(&mut reader, Some(key)).unwrap(); + let pak = repak::PakReader::new_any_with_key(&mut reader, key).unwrap(); assert_eq!(pak.mount_point(), "../mount/point/root/"); assert_eq!(pak.version(), version); @@ -159,12 +159,11 @@ fn test_write(_version: repak::Version, _file_name: &str, bytes: &[u8]) { .unwrap(); let mut reader = std::io::Cursor::new(bytes); - let pak_reader = repak::PakReader::new_any_with_key(&mut reader, Some(key)).unwrap(); + let pak_reader = repak::PakReader::new_any_with_key(&mut reader, key).unwrap(); let writer = Cursor::new(vec![]); - let mut pak_writer = repak::PakWriter::new_with_key( + let mut pak_writer = repak::PakWriter::new( writer, - None, pak_reader.version(), pak_reader.mount_point().to_owned(), Some(0x205C5A7D), @@ -191,7 +190,7 @@ fn test_rewrite_index(_version: repak::Version, _file_name: &str, bytes: &[u8]) .unwrap(); let mut buf = std::io::Cursor::new(bytes.to_vec()); - let pak_reader = repak::PakReader::new_any_with_key(&mut buf, Some(key)).unwrap(); + let pak_reader = repak::PakReader::new_any_with_key(&mut buf, key).unwrap(); let rewrite = pak_reader .into_pakwriter(buf) diff --git a/repak_cli/src/main.rs b/repak_cli/src/main.rs index 78ddbd8..b1f33c8 100644 --- a/repak_cli/src/main.rs +++ b/repak_cli/src/main.rs @@ -175,7 +175,7 @@ fn main() -> Result<(), repak::Error> { } fn info(aes_key: Option, action: ActionInfo) -> Result<(), repak::Error> { - let pak = repak::PakReader::new_any_with_key( + let pak = repak::PakReader::new_any_with_optional_key( &mut BufReader::new(File::open(action.input)?), aes_key, )?; @@ -189,7 +189,7 @@ fn info(aes_key: Option, action: ActionInfo) -> Result<(), repak::E } fn list(aes_key: Option, action: ActionList) -> Result<(), repak::Error> { - let pak = repak::PakReader::new_any_with_key( + let pak = repak::PakReader::new_any_with_optional_key( &mut BufReader::new(File::open(action.input)?), aes_key, )?; @@ -221,7 +221,7 @@ fn list(aes_key: Option, action: ActionList) -> Result<(), repak::E } fn hash_list(aes_key: Option, action: ActionHashList) -> Result<(), repak::Error> { - let pak = repak::PakReader::new_any_with_key( + let pak = repak::PakReader::new_any_with_optional_key( &mut BufReader::new(File::open(&action.input)?), aes_key, )?; @@ -279,7 +279,7 @@ fn hash_list(aes_key: Option, action: ActionHashList) -> Result<(), const STYLE: &str = "[{elapsed_precise}] [{wide_bar}] {pos}/{len} ({eta})"; fn unpack(aes_key: Option, action: ActionUnpack) -> Result<(), repak::Error> { - let pak = repak::PakReader::new_any_with_key( + let pak = repak::PakReader::new_any_with_optional_key( &mut BufReader::new(File::open(&action.input)?), aes_key, )?; @@ -400,7 +400,7 @@ fn pack(args: ActionPack) -> Result<(), repak::Error> { collect_files(&mut paths, input_path)?; paths.sort(); - let mut pak = repak::PakWriter::new_with_key( + let mut pak = repak::PakWriter::new_with_optional_key( BufWriter::new(File::create(&output)?), None, args.version, @@ -435,7 +435,7 @@ fn pack(args: ActionPack) -> Result<(), repak::Error> { fn get(aes_key: Option, args: ActionGet) -> Result<(), repak::Error> { let mut reader = BufReader::new(File::open(&args.input)?); - let pak = repak::PakReader::new_any_with_key(&mut reader, aes_key)?; + let pak = repak::PakReader::new_any_with_optional_key(&mut reader, aes_key)?; let mount_point = PathBuf::from(pak.mount_point()); let prefix = Path::new(&args.strip_prefix);