From 6c1a66a97de6152e541f1bb8b70547cd379d66c9 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Sun, 17 Dec 2023 01:59:10 -0600 Subject: [PATCH] Cleanup --- minidisc-rs/Cargo.toml | 5 ++- minidisc-rs/src/netmd/base.rs | 6 +-- minidisc-rs/src/netmd/interface.rs | 69 +++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/minidisc-rs/Cargo.toml b/minidisc-rs/Cargo.toml index 00dbde5..377b35b 100644 --- a/minidisc-rs/Cargo.toml +++ b/minidisc-rs/Cargo.toml @@ -17,12 +17,13 @@ encoding_rs = "0.8.33" magic-crypt = "3.1.12" nofmt = "1.0.0" once_cell = "1.18.0" -regex = "1.9.5" -unicode-jp = "0.4.0" unicode-normalization = "0.1.22" hex = "0.4.3" des = "0.8.1" webusb = "0.5.0" +unicode-jp = "0.4.0" +regex = "1.10.2" + [lib] crate-type = ["cdylib", "rlib"] diff --git a/minidisc-rs/src/netmd/base.rs b/minidisc-rs/src/netmd/base.rs index eb7bc59..167f998 100644 --- a/minidisc-rs/src/netmd/base.rs +++ b/minidisc-rs/src/netmd/base.rs @@ -12,9 +12,9 @@ const BULK_READ_ENDPOINT: u8 = 0x81; pub const CHUNKSIZE: u32 = 0x10000; // TODO: I think this sucks, figure out a better way -pub static DEVICE_IDS: Lazy> = Lazy::new(|| { - nofmt::pls! { - Vec::from([ +pub static DEVICE_IDS: Lazy> = Lazy::new(|| { + nofmt::pls! {Box::new( + [ DeviceId {vendor_id: 0x04dd, product_id: 0x7202, name: Some(String::from("Sharp IM-MT899H"))}, DeviceId {vendor_id: 0x04dd, product_id: 0x9013, name: Some(String::from("Sharp IM-DR400"))}, DeviceId {vendor_id: 0x04dd, product_id: 0x9014, name: Some(String::from("Sharp IM-DR80"))}, diff --git a/minidisc-rs/src/netmd/interface.rs b/minidisc-rs/src/netmd/interface.rs index fd1e430..2d6919f 100644 --- a/minidisc-rs/src/netmd/interface.rs +++ b/minidisc-rs/src/netmd/interface.rs @@ -9,6 +9,7 @@ use std::collections::HashMap; use std::error::Error; use magic_crypt::{MagicCrypt, SecureBit, MagicCryptTrait, new_magic_crypt}; use hex; +use once_cell::sync::Lazy; use std::thread::sleep; use std::time::Duration; use webusb; @@ -35,6 +36,7 @@ pub enum DiscFormat { SPStereo = 6, } +#[derive(Clone, Hash, Eq, PartialEq)] enum WireFormat { PCM = 0x00, L105kbps = 0x90, @@ -151,6 +153,13 @@ enum Status { Interim = 0x0f, } +const FRAME_SIZE: Lazy> = Lazy::new(|| HashMap::from([ + (WireFormat::PCM, 2048), + (WireFormat::LP2, 192), + (WireFormat::L105kbps, 152), + (WireFormat::LP4, 96), +])); + impl std::convert::TryFrom for Status { type Error = Box; @@ -1544,8 +1553,14 @@ pub fn retailmac( Ok(()) } -struct EKBOpenSource { +const DISC_FOR_WIRE: Lazy> = Lazy::new(|| HashMap::from([ + (WireFormat::PCM, DiscFormat::SPStereo), + (WireFormat::LP2, DiscFormat::LP2), + (WireFormat::L105kbps, DiscFormat::LP2), + (WireFormat::LP4, DiscFormat::LP4), +])); +struct EKBOpenSource { } impl EKBOpenSource { @@ -1577,14 +1592,66 @@ impl EKBOpenSource { */ } +#[derive(Clone)] struct MDTrack { title: String, format: WireFormat, data: Vec, chunk_size: i32, full_width_title: Option, + encrypt_packets_iterator: EncryptPacketsIterator +} + +#[derive(Clone)] +struct EncryptPacketsIterator { + kek: Vec, + frame_size: i32, + data: Vec, + chunk_size: i32 } impl MDTrack { + pub fn full_width_title(self) -> String { + self.full_width_title.unwrap_or("".to_string()) + } + + pub fn title(&self) -> String { + self.title.clone() + } + + pub fn data_format(&self) -> WireFormat { + self.format.clone() + } + + pub fn frame_count(&self) -> usize { + self.total_size() / self.frame_size() + } + + pub fn frame_size(&self) -> usize { + *FRAME_SIZE.get(&self.format).unwrap() + } + + pub fn chunk_size(self) -> i32 { + self.chunk_size + } + + pub fn total_size(&self) -> usize { + let frame_size = self.frame_size(); + let mut len = self.data.len(); + if len % frame_size != 0 { + len = len + (frame_size - (len % frame_size)); + } + len + } + + pub fn content_id() -> [u8; 20] { + [0x01, 0x0f, 0x50, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x48, + 0xa2, 0x8d, 0x3e, 0x1a, 0x3b, 0x0c, 0x44, 0xaf, 0x2f, 0xa0] + } + + pub fn get_kek() -> [u8; 8] { + [0x14, 0xe3, 0x83, 0x4e, 0xe2, 0xd3, 0xcc, 0xa5] + } + }