From 42f29abff3bb757bbc9a5f0b8016d908f879643a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Sun, 15 Sep 2024 16:19:28 +0100 Subject: [PATCH] [kwm] refactor: improve error display --- um_crypto/kuwo/src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/um_crypto/kuwo/src/lib.rs b/um_crypto/kuwo/src/lib.rs index 3d64679..04dc1a7 100644 --- a/um_crypto/kuwo/src/lib.rs +++ b/um_crypto/kuwo/src/lib.rs @@ -1,6 +1,7 @@ use crate::kwm_v1::CipherV1; use anyhow::Result; use byteorder::{ReadBytesExt, LE}; +use std::fmt; use std::io::{Cursor, Read}; use thiserror::Error; use umc_qmc::QMCv2Cipher; @@ -13,13 +14,16 @@ pub use umc_qmc::QMCv2Cipher as CipherV2; /// Commonly used secret key for Kuwo services. pub const SECRET_KEY: [u8; 8] = *b"ylzsxkwm"; +#[derive(Debug)] +pub struct HeaderMagicBytes(pub [u8; 16]); + #[derive(Error, Debug)] pub enum KuwoCryptoError { #[error("Invalid DES data size (expected: {0} mod 8 == 0)")] InvalidDesDataSize(usize), - #[error("Invalid KWM header magic bytes: {0:?}")] - InvalidHeaderMagic([u8; 16]), + #[error("Invalid KWM header magic bytes: {0}")] + InvalidHeaderMagic(HeaderMagicBytes), #[error("KWMv2: EKey required")] V2EKeyRequired, @@ -28,6 +32,15 @@ pub enum KuwoCryptoError { 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 enum Cipher {