pub(crate) mod kdf;
#[cfg(feature = "encryption")]
mod password;
use std::fmt;
#[cfg(feature = "encryption")]
use std::sync::Arc;
#[cfg(feature = "encryption")]
pub use password::Password;
#[cfg(feature = "encryption")]
pub use password::PasswordError;
#[derive(Clone, Default)]
pub(crate) struct Credentials {
#[cfg(feature = "encryption")]
password: Option<Arc<Password>>,
}
impl fmt::Debug for Credentials {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Credentials(..)")
}
}
impl Credentials {
pub(crate) const NONE: Self = Self {
#[cfg(feature = "encryption")]
password: None,
};
#[cfg(feature = "encryption")]
pub(crate) const fn new(password: Option<Arc<Password>>) -> Self {
Self { password }
}
#[cfg_attr(
not(feature = "encryption"),
expect(
clippy::unused_self,
reason = "without the feature there is no password to read, but callers are the same"
)
)]
pub(crate) const fn is_supplied(&self) -> bool {
#[cfg(feature = "encryption")]
{
self.password.is_some()
}
#[cfg(not(feature = "encryption"))]
{
false
}
}
#[cfg_attr(
not(feature = "encryption"),
expect(
clippy::unused_self,
reason = "without the feature there is no password to read, but callers are the same"
)
)]
pub(crate) fn zip_secret(&self) -> Option<&[u8]> {
#[cfg(feature = "encryption")]
{
self.password.as_deref().map(Password::expose)
}
#[cfg(not(feature = "encryption"))]
{
None
}
}
#[cfg_attr(
not(feature = "encryption"),
expect(
clippy::unused_self,
clippy::unnecessary_wraps,
reason = "without the feature there is no password to read, but callers are the same"
)
)]
pub(crate) fn sevenz_secret(&self) -> Result<Option<sevenz_rust2::Password>, UnusablePassword> {
#[cfg(feature = "encryption")]
{
let Some(password) = self.password.as_deref() else {
return Ok(None);
};
let text = std::str::from_utf8(password.expose()).map_err(|_| UnusablePassword)?;
Ok(Some(sevenz_rust2::Password::new(text)))
}
#[cfg(not(feature = "encryption"))]
{
Ok(None)
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct UnusablePassword;
impl From<UnusablePassword> for crate::ArchiveError {
fn from(_: UnusablePassword) -> Self {
crate::Violation::Encrypted {
reason: crate::EncryptionRefusal::UnusablePassword,
name: None,
}
.into()
}
}