[kwm] refactor: improve error display

This commit is contained in:
鲁树人 2024-09-15 16:19:28 +01:00
parent 07fe0263d2
commit 42f29abff3

View File

@ -1,6 +1,7 @@
use crate::kwm_v1::CipherV1; use crate::kwm_v1::CipherV1;
use anyhow::Result; use anyhow::Result;
use byteorder::{ReadBytesExt, LE}; use byteorder::{ReadBytesExt, LE};
use std::fmt;
use std::io::{Cursor, Read}; use std::io::{Cursor, Read};
use thiserror::Error; use thiserror::Error;
use umc_qmc::QMCv2Cipher; use umc_qmc::QMCv2Cipher;
@ -13,13 +14,16 @@ pub use umc_qmc::QMCv2Cipher as CipherV2;
/// Commonly used secret key for Kuwo services. /// Commonly used secret key for Kuwo services.
pub const SECRET_KEY: [u8; 8] = *b"ylzsxkwm"; pub const SECRET_KEY: [u8; 8] = *b"ylzsxkwm";
#[derive(Debug)]
pub struct HeaderMagicBytes(pub [u8; 16]);
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum KuwoCryptoError { pub enum KuwoCryptoError {
#[error("Invalid DES data size (expected: {0} mod 8 == 0)")] #[error("Invalid DES data size (expected: {0} mod 8 == 0)")]
InvalidDesDataSize(usize), InvalidDesDataSize(usize),
#[error("Invalid KWM header magic bytes: {0:?}")] #[error("Invalid KWM header magic bytes: {0}")]
InvalidHeaderMagic([u8; 16]), InvalidHeaderMagic(HeaderMagicBytes),
#[error("KWMv2: EKey required")] #[error("KWMv2: EKey required")]
V2EKeyRequired, V2EKeyRequired,
@ -28,6 +32,15 @@ pub enum KuwoCryptoError {
UnsupportedVersion(usize), UnsupportedVersion(usize),
} }
impl fmt::Display for HeaderMagicBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.0.iter() {
write!(f, "{:02x} ", byte)?;
}
Ok(())
}
}
pub const DATA_START_OFFSET: usize = 0x400; pub const DATA_START_OFFSET: usize = 0x400;
pub enum Cipher { pub enum Cipher {