make features more clearly additive

This commit is contained in:
spuds 2023-08-27 21:51:42 +01:00
parent 50f54837ca
commit f166ee685f
No known key found for this signature in database
GPG key ID: 0B6CA6068E827C8F
4 changed files with 55 additions and 25 deletions

View file

@ -131,12 +131,9 @@ pub(crate) enum Key {
None, None,
} }
impl From<Option<aes::Aes256>> for Key { #[cfg(feature = "encryption")]
fn from(value: Option<aes::Aes256>) -> Self { impl From<aes::Aes256> for Key {
match value { fn from(value: aes::Aes256) -> Self {
#[cfg(feature = "encryption")] Self::Some(value)
Some(key) => Self::Some(key),
_ => Self::None,
}
} }
} }

View file

@ -83,7 +83,6 @@ fn decrypt(key: &super::Key, bytes: &mut [u8]) -> Result<(), super::Error> {
} }
impl PakReader { impl PakReader {
#[cfg(not(feature = "encryption"))]
pub fn new_any<R: Read + Seek>(reader: &mut R) -> Result<Self, super::Error> { pub fn new_any<R: Read + Seek>(reader: &mut R) -> Result<Self, super::Error> {
Self::new_any_inner(reader, super::Key::None) Self::new_any_inner(reader, super::Key::None)
} }
@ -91,11 +90,22 @@ impl PakReader {
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
pub fn new_any_with_key<R: Read + Seek>( pub fn new_any_with_key<R: Read + Seek>(
reader: &mut R, reader: &mut R,
key: Option<aes::Aes256>, key: aes::Aes256,
) -> Result<Self, super::Error> { ) -> Result<Self, super::Error> {
Self::new_any_inner(reader, key.into()) Self::new_any_inner(reader, key.into())
} }
#[cfg(feature = "encryption")]
pub fn new_any_with_optional_key<R: Read + Seek>(
reader: &mut R,
key: Option<aes::Aes256>,
) -> Result<Self, super::Error> {
match key {
Some(key) => Self::new_any_with_key(reader, key),
None => Self::new_any(reader),
}
}
fn new_any_inner<R: Read + Seek>( fn new_any_inner<R: Read + Seek>(
reader: &mut R, reader: &mut R,
key: super::Key, key: super::Key,
@ -112,23 +122,34 @@ impl PakReader {
Err(super::Error::UnsupportedOrEncrypted(log)) Err(super::Error::UnsupportedOrEncrypted(log))
} }
#[cfg(not(feature = "encryption"))]
pub fn new<R: Read + Seek>( pub fn new<R: Read + Seek>(
reader: &mut R, reader: &mut R,
version: super::Version, version: super::Version,
) -> Result<Self, super::Error> { ) -> Result<Self, super::Error> {
Self::new_inner(reader, version) Self::new_inner(reader, version, super::Key::None)
} }
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
pub fn new_with_key<R: Read + Seek>( pub fn new_with_key<R: Read + Seek>(
reader: &mut R, reader: &mut R,
version: super::Version, version: super::Version,
key: Option<aes::Aes256>, key: aes::Aes256,
) -> Result<Self, super::Error> { ) -> Result<Self, super::Error> {
Self::new_inner(reader, version, key.into()) Self::new_inner(reader, version, key.into())
} }
#[cfg(feature = "encryption")]
pub fn new_with_optional_key<R: Read + Seek>(
reader: &mut R,
version: super::Version,
key: Option<aes::Aes256>,
) -> Result<Self, super::Error> {
match key {
Some(key) => Self::new_with_key(reader, version, key),
None => Self::new(reader, version),
}
}
fn new_inner<R: Read + Seek>( fn new_inner<R: Read + Seek>(
reader: &mut R, reader: &mut R,
version: super::Version, version: super::Version,
@ -195,7 +216,6 @@ impl PakReader {
} }
impl<W: Write + Seek> PakWriter<W> { impl<W: Write + Seek> PakWriter<W> {
#[cfg(not(feature = "encryption"))]
pub fn new( pub fn new(
writer: W, writer: W,
version: Version, version: Version,
@ -212,7 +232,7 @@ impl<W: Write + Seek> PakWriter<W> {
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
pub fn new_with_key( pub fn new_with_key(
writer: W, writer: W,
key: Option<aes::Aes256>, key: aes::Aes256,
version: Version, version: Version,
mount_point: String, mount_point: String,
path_hash_seed: Option<u64>, path_hash_seed: Option<u64>,
@ -224,6 +244,20 @@ impl<W: Write + Seek> PakWriter<W> {
} }
} }
#[cfg(feature = "encryption")]
pub fn new_with_optional_key(
writer: W,
key: Option<aes::Aes256>,
version: Version,
mount_point: String,
path_hash_seed: Option<u64>,
) -> 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( fn new_inner(
writer: W, writer: W,
key: super::Key, key: super::Key,

View file

@ -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 len = inner_reader.seek(SeekFrom::End(0)).unwrap();
let mut reader = ReadCounter::new_size(inner_reader, len as usize); 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.mount_point(), "../mount/point/root/");
assert_eq!(pak.version(), version); assert_eq!(pak.version(), version);
@ -159,12 +159,11 @@ fn test_write(_version: repak::Version, _file_name: &str, bytes: &[u8]) {
.unwrap(); .unwrap();
let mut reader = std::io::Cursor::new(bytes); 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 writer = Cursor::new(vec![]);
let mut pak_writer = repak::PakWriter::new_with_key( let mut pak_writer = repak::PakWriter::new(
writer, writer,
None,
pak_reader.version(), pak_reader.version(),
pak_reader.mount_point().to_owned(), pak_reader.mount_point().to_owned(),
Some(0x205C5A7D), Some(0x205C5A7D),
@ -191,7 +190,7 @@ fn test_rewrite_index(_version: repak::Version, _file_name: &str, bytes: &[u8])
.unwrap(); .unwrap();
let mut buf = std::io::Cursor::new(bytes.to_vec()); 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 let rewrite = pak_reader
.into_pakwriter(buf) .into_pakwriter(buf)

View file

@ -175,7 +175,7 @@ fn main() -> Result<(), repak::Error> {
} }
fn info(aes_key: Option<aes::Aes256>, action: ActionInfo) -> Result<(), repak::Error> { fn info(aes_key: Option<aes::Aes256>, 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)?), &mut BufReader::new(File::open(action.input)?),
aes_key, aes_key,
)?; )?;
@ -189,7 +189,7 @@ fn info(aes_key: Option<aes::Aes256>, action: ActionInfo) -> Result<(), repak::E
} }
fn list(aes_key: Option<aes::Aes256>, action: ActionList) -> Result<(), repak::Error> { fn list(aes_key: Option<aes::Aes256>, 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)?), &mut BufReader::new(File::open(action.input)?),
aes_key, aes_key,
)?; )?;
@ -221,7 +221,7 @@ fn list(aes_key: Option<aes::Aes256>, action: ActionList) -> Result<(), repak::E
} }
fn hash_list(aes_key: Option<aes::Aes256>, action: ActionHashList) -> Result<(), repak::Error> { fn hash_list(aes_key: Option<aes::Aes256>, 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)?), &mut BufReader::new(File::open(&action.input)?),
aes_key, aes_key,
)?; )?;
@ -279,7 +279,7 @@ fn hash_list(aes_key: Option<aes::Aes256>, action: ActionHashList) -> Result<(),
const STYLE: &str = "[{elapsed_precise}] [{wide_bar}] {pos}/{len} ({eta})"; const STYLE: &str = "[{elapsed_precise}] [{wide_bar}] {pos}/{len} ({eta})";
fn unpack(aes_key: Option<aes::Aes256>, action: ActionUnpack) -> Result<(), repak::Error> { fn unpack(aes_key: Option<aes::Aes256>, 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)?), &mut BufReader::new(File::open(&action.input)?),
aes_key, aes_key,
)?; )?;
@ -400,7 +400,7 @@ fn pack(args: ActionPack) -> Result<(), repak::Error> {
collect_files(&mut paths, input_path)?; collect_files(&mut paths, input_path)?;
paths.sort(); paths.sort();
let mut pak = repak::PakWriter::new_with_key( let mut pak = repak::PakWriter::new_with_optional_key(
BufWriter::new(File::create(&output)?), BufWriter::new(File::create(&output)?),
None, None,
args.version, args.version,
@ -435,7 +435,7 @@ fn pack(args: ActionPack) -> Result<(), repak::Error> {
fn get(aes_key: Option<aes::Aes256>, args: ActionGet) -> Result<(), repak::Error> { fn get(aes_key: Option<aes::Aes256>, args: ActionGet) -> Result<(), repak::Error> {
let mut reader = BufReader::new(File::open(&args.input)?); 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 mount_point = PathBuf::from(pak.mount_point());
let prefix = Path::new(&args.strip_prefix); let prefix = Path::new(&args.strip_prefix);