Compare commits

..

14 commits
0.1.2 ... main

Author SHA1 Message Date
1bc2f5b18b Added small main.rs binary program as an example 2024-12-29 04:56:44 -06:00
G2
e859fd937f
Update README.md 2024-08-15 21:02:35 -05:00
8b193bd16c Fixed track_group_list to be consistent with netmd-js 2024-08-15 01:22:16 -05:00
4fa7b0ec30 Version bump, 0.1.3 2024-08-15 01:09:56 -05:00
ac29c6a0a5 Cleaned up clippy suggestions 2024-08-15 01:07:53 -05:00
c49a86ed75 Fixed documentation example 2024-08-15 00:49:02 -05:00
G2
391d0c8599
WASM track upload (#7)
* Initial working WASM encryption

* Improved encryption implementation
2024-08-15 00:46:12 -05:00
885e7508fd Updated cross-usb to v0.4.0 2024-08-14 20:49:04 -05:00
0a75b6a013 Fixed imports 2024-08-14 13:39:03 -05:00
3646a4ac05 Improved documentation, fixed error while retrieving track position on empty disc 2024-08-14 13:37:04 -05:00
dba4ba0df4 Fixed doc failure 2024-06-19 16:40:32 -05:00
66863aad1e Improved docs 2024-06-19 16:31:27 -05:00
2a1d975dce Fixed *more* clippy warnings 2024-06-19 11:57:22 -05:00
G2
8d4a842a37
Update README.md 2024-06-19 11:40:13 -05:00
11 changed files with 562 additions and 220 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "minidisc" name = "minidisc"
version = "0.1.2" version = "0.1.3"
edition = "2021" edition = "2021"
homepage = "https://github.com/G2-Games/minidisc-rs/" homepage = "https://github.com/G2-Games/minidisc-rs/"
repository = "https://github.com/G2-Games/minidisc-rs/" repository = "https://github.com/G2-Games/minidisc-rs/"
@ -27,6 +27,9 @@ targets = [
[lib] [lib]
crate-type = ["cdylib", "rlib"] crate-type = ["cdylib", "rlib"]
[dev-dependencies]
tokio-test = "0.4.3"
[dependencies] [dependencies]
diacritics = "0.2.0" diacritics = "0.2.0"
encoding_rs = "0.8.33" encoding_rs = "0.8.33"
@ -34,7 +37,7 @@ nofmt = "1.0.0"
once_cell = "1.18.0" once_cell = "1.18.0"
unicode-normalization = "0.1.22" unicode-normalization = "0.1.22"
regex = "1.10" regex = "1.10"
cross_usb = "0.3" cross_usb = "0.4"
num-derive = "0.4.2" num-derive = "0.4.2"
num-traits = "0.2.14" num-traits = "0.2.14"
rand = "0.8.5" rand = "0.8.5"
@ -42,11 +45,12 @@ getrandom = { version = "0.2", features = ["js"] }
des = "0.8" des = "0.8"
cbc = "0.1" cbc = "0.1"
ecb = "0.1" ecb = "0.1"
tokio = { version = "1.36", features = ["sync"] } tokio = { version = "1.36", features = ["full", "sync"] }
g2-unicode-jp = "0.4.1" g2-unicode-jp = "0.4.1"
thiserror = "1.0.57" thiserror = "1.0.57"
phf = { version = "0.11.2", features = ["phf_macros", "macros"] } phf = { version = "0.11.2", features = ["phf_macros", "macros"] }
byteorder = "1.5.0" byteorder = "1.5.0"
log = "0.4.22"
[target.'cfg(target_family = "wasm")'.dependencies] [target.'cfg(target_family = "wasm")'.dependencies]
gloo = { version = "0.11.0", features = ["futures", "worker"] } gloo = { version = "0.11.0", features = ["futures", "worker"] }

View file

@ -1,13 +1,13 @@
# Rust Minidisc # Rust Minidisc
![Crates.io Version](https://img.shields.io/crates/v/minidisc?style=for-the-badge) [![Lib.rs Version](https://img.shields.io/crates/v/minidisc?style=for-the-badge&logo=rust&label=lib.rs&color=%23a68bfc)](https://lib.rs/crates/minidisc)
![docs.rs](https://img.shields.io/docsrs/minidisc?style=for-the-badge) [![docs.rs](https://img.shields.io/docsrs/minidisc?style=for-the-badge)](https://docs.rs/minidisc/)
A library for controlling and interfacing with [MiniDisc](https://en.wikipedia.org/wiki/MiniDisc) devices from within Rust programs. Compatible with many cross platform targets (including Web Assembly!) by using [cross-usb](https://github.com/G2-Games/cross-usb). A library for controlling and interfacing with [MiniDisc](https://en.wikipedia.org/wiki/MiniDisc) devices from within Rust programs. Compatible with many cross platform targets (including Web Assembly!) by using [cross-usb](https://github.com/G2-Games/cross-usb).
The feature set is very similar to that of [netmd-js](https://github.com/cybercase/netmd-js) which this library is inspired by. Devlopment of this project was made much easier by the absolutely awesome [Web Minidisc project](https://github.com/asivery/webminidisc), [NetMD-exploits](https://github.com/asivery/netmd-exploits), and the C based [Linux Minidisc project](https://github.com/linux-minidisc/linux-minidisc). Go check those projects out! The feature set is very similar to that of [netmd-js](https://github.com/cybercase/netmd-js) which this library is inspired by. Development of this project was made much easier by the absolutely awesome [Web Minidisc project](https://github.com/asivery/webminidisc), [NetMD-exploits](https://github.com/asivery/netmd-exploits), and the C based [Linux Minidisc project](https://github.com/linux-minidisc/linux-minidisc). Go check those projects out!
> [!IMPORTANT] > [!IMPORTANT]
> Documentation has not been finished and is a work in progress. Any help with it would be appreciated! > Documentation has not been finished and is a work in progress. Any contributions would be appreciated!
## Current Features ## Current Features
### NetMD ### NetMD

View file

@ -1,4 +1,30 @@
/// A crate for controlling NetMD and Hi-MD devices. //! A crate for controlling NetMD and Hi-MD devices.
/// //!
/// To use this library, first you need to get a device from [cross-usb] and then open [netmd::interface::NetMDInterface] //! This crate is entirely `async` (a necessity because of USB in WASM), but
//! it can be used in programs which are not async by using a crate like
//! [futures_lite](https://docs.rs/futures-lite/) with the `block_on` function.
//!
//! To use this library, first you need to get a device from [`cross_usb`] and
//! then open a [`netmd::NetMDContext`].
//!
//! ```no_run
//! # tokio_test::block_on(async {
//! use cross_usb::get_device;
//! use minidisc::netmd::base::DEVICE_IDS_CROSSUSB;
//! use minidisc::netmd::NetMDContext;
//!
//! // Get a device using the built-in list of descriptors for minidisc devices
//! let dev_descriptor = cross_usb::get_device(DEVICE_IDS_CROSSUSB.to_vec()).await
//! .expect("Failed to find device");
//!
//! // Open a NetMD Context with the device
//! let mut context = NetMDContext::new(dev_descriptor).await
//! .expect("Could not create context");
//!
//! // Perform operations on it ...
//! context.list_content().await
//! .expect("Could not list disc contents");
//! # })
//! ```
pub mod netmd; pub mod netmd;

42
src/main.rs Normal file
View file

@ -0,0 +1,42 @@
use std::{process::exit, time::Duration};
#[tokio::main]
async fn main() {
let Ok(player) = cross_usb::get_device(minidisc::netmd::DEVICE_IDS_CROSSUSB.to_vec()).await else {
eprintln!("Could not find a MiniDisc device");
exit(1);
};
let Ok(mut player) = minidisc::netmd::NetMDContext::new(player).await else {
eprintln!("Could not open device!");
exit(1);
};
let disc = player.list_content().await.expect("Could not retrieve player's contents");
for track in disc.tracks() {
println!(
"{:02}:\n Title: {} | {}\n Duration: {}\n Encoding: {}\n",
track.index(),
track.title(), track.full_width_title(),
pretty_time(track.duration().as_duration()),
track.encoding(),
);
}
}
fn pretty_time(dur: Duration) -> String {
let mut string = String::new();
if dur >= Duration::from_secs(3600) {
string.push_str(&format!("{:02}", dur.as_secs() / 3600));
string.push(':');
}
if dur >= Duration::from_secs(60) {
string.push_str(&format!("{:02}", (dur.as_secs() / 60) % 3600).to_string());
string.push(':');
}
if dur >= Duration::from_secs(60) {
string.push_str(&format!("{:02}", dur.as_secs() % 60).to_string());
}
string
}

View file

@ -6,8 +6,8 @@ use thiserror::Error;
// USB stuff // USB stuff
use cross_usb::prelude::*; use cross_usb::prelude::*;
use cross_usb::usb::{ControlIn, ControlOut, ControlType, Recipient, UsbError}; use cross_usb::usb::{ControlIn, ControlOut, ControlType, Recipient, Error};
use cross_usb::{Descriptor, Interface}; use cross_usb::{DeviceInfo, Interface};
use super::utils::cross_sleep; use super::utils::cross_sleep;
@ -15,6 +15,7 @@ const BULK_WRITE_ENDPOINT: u8 = 0x02;
const BULK_READ_ENDPOINT: u8 = 0x81; const BULK_READ_ENDPOINT: u8 = 0x81;
nofmt::pls! { // Skip formatting the following info nofmt::pls! { // Skip formatting the following info
/// Device IDs for use in matching existing devices
pub static DEVICE_IDS: &[DeviceId] = &[ pub static DEVICE_IDS: &[DeviceId] = &[
DeviceId { vendor_id: 0x04dd, product_id: 0x7202, name: Some("Sharp IM-MT899H") }, DeviceId { vendor_id: 0x04dd, product_id: 0x7202, name: Some("Sharp IM-MT899H") },
DeviceId { vendor_id: 0x04dd, product_id: 0x9013, name: Some("Sharp IM-DR400") }, DeviceId { vendor_id: 0x04dd, product_id: 0x9013, name: Some("Sharp IM-DR400") },
@ -65,6 +66,7 @@ pub static DEVICE_IDS: &[DeviceId] = &[
]; ];
} }
/// Device IDs for use with [cross_usb]
pub static DEVICE_IDS_CROSSUSB: Lazy<Box<[cross_usb::DeviceFilter]>> = Lazy::new(|| { pub static DEVICE_IDS_CROSSUSB: Lazy<Box<[cross_usb::DeviceFilter]>> = Lazy::new(|| {
DEVICE_IDS DEVICE_IDS
.iter() .iter()
@ -112,10 +114,15 @@ pub enum NetMDError {
UnknownDevice(DeviceId), UnknownDevice(DeviceId),
#[error("usb connection error")] #[error("usb connection error")]
UsbError(#[from] UsbError), UsbError(#[from] Error),
} }
/// A USB connection to a NetMD device /// A low-level USB connection to a NetMD device.
///
/// With this you can send raw commands to the device and recieve raw data.
///
/// For simple communication with a NetMD device, you most likely want the
/// higher level [`super::NetMDInterface`] or [`super::NetMDContext`] interfaces
pub struct NetMD { pub struct NetMD {
usb_interface: Interface, usb_interface: Interface,
model: DeviceId, model: DeviceId,
@ -125,7 +132,7 @@ impl NetMD {
const READ_REPLY_RETRY_INTERVAL: u32 = 10; const READ_REPLY_RETRY_INTERVAL: u32 = 10;
/// Creates a new interface to a NetMD device /// Creates a new interface to a NetMD device
pub async fn new(usb_descriptor: Descriptor) -> Result<Self, NetMDError> { pub async fn new(usb_descriptor: DeviceInfo) -> Result<Self, NetMDError> {
let mut model = DeviceId { let mut model = DeviceId {
vendor_id: usb_descriptor.vendor_id().await, vendor_id: usb_descriptor.vendor_id().await,
product_id: usb_descriptor.product_id().await, product_id: usb_descriptor.product_id().await,
@ -136,7 +143,7 @@ impl NetMD {
if device_type.vendor_id == model.vendor_id if device_type.vendor_id == model.vendor_id
&& device_type.product_id == model.product_id && device_type.product_id == model.product_id
{ {
model.name = device_type.name.clone(); model.name = device_type.name;
break; break;
} }
} }
@ -200,15 +207,17 @@ impl NetMD {
Ok((length_bytes, poll_result)) Ok((length_bytes, poll_result))
} }
/// Send a control message to the device (Raw bytes)
pub async fn send_command(&mut self, command: Vec<u8>) -> Result<(), NetMDError> { pub async fn send_command(&mut self, command: Vec<u8>) -> Result<(), NetMDError> {
self._send_command(command, false).await self._send_command(command, false).await
} }
/// Send a factory control message to the device (Raw bytes)
pub async fn send_factory_command(&mut self, command: Vec<u8>) -> Result<(), NetMDError> { pub async fn send_factory_command(&mut self, command: Vec<u8>) -> Result<(), NetMDError> {
self._send_command(command, true).await self._send_command(command, true).await
} }
/// Send a control message to the device /// Send a control message to the device, can also send factory commands
async fn _send_command( async fn _send_command(
&mut self, &mut self,
command: Vec<u8>, command: Vec<u8>,

View file

@ -1,5 +1,5 @@
#![cfg_attr(debug_assertions, allow(dead_code))] #![cfg_attr(debug_assertions, allow(dead_code))]
use cross_usb::Descriptor; use cross_usb::DeviceInfo;
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use regex::Regex; use regex::Regex;
@ -18,6 +18,7 @@ use super::utils::{
sanitize_full_width_title, sanitize_half_width_title, sanitize_full_width_title, sanitize_half_width_title,
}; };
/// The current reported status from the device.
#[derive(Debug, Clone, Copy, FromPrimitive, PartialEq, Eq)] #[derive(Debug, Clone, Copy, FromPrimitive, PartialEq, Eq)]
pub enum OperatingStatus { pub enum OperatingStatus {
Ready = 50687, Ready = 50687,
@ -31,14 +32,16 @@ pub enum OperatingStatus {
ReadyForTransfer = 65319, ReadyForTransfer = 65319,
} }
#[derive(Debug, Clone)] /// A representation of time in the way a NetMD device uses internally.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Time { pub struct Time {
pub minute: u16, pub minute: u16,
pub second: u16, pub second: u16,
pub frame: u16, pub frame: u16,
} }
#[derive(Debug, Clone)] /// A representation of the current status of the device.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeviceStatus { pub struct DeviceStatus {
pub disc_present: bool, pub disc_present: bool,
pub state: Option<OperatingStatus>, pub state: Option<OperatingStatus>,
@ -46,7 +49,8 @@ pub struct DeviceStatus {
pub time: Time, pub time: Time,
} }
#[derive(Debug, Clone)] /// Information about a single track
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Track { pub struct Track {
index: u16, index: u16,
title: String, title: String,
@ -58,6 +62,7 @@ pub struct Track {
} }
impl Track { impl Track {
/// Get the number of title cells a title will take up.
pub fn cells_for_title(&self) -> (usize, usize) { pub fn cells_for_title(&self) -> (usize, usize) {
let encoding_name_correction = match self.encoding { let encoding_name_correction = match self.encoding {
Encoding::SP => 0, Encoding::SP => 0,
@ -72,8 +77,37 @@ impl Track {
usize::max(encoding_name_correction, full_width_length), usize::max(encoding_name_correction, full_width_length),
) )
} }
pub fn index(&self) -> u16 {
self.index
} }
pub fn title(&self) -> &String {
&self.title
}
pub fn full_width_title(&self) -> &String {
&self.full_width_title
}
pub fn duration(&self) -> RawTime {
self.duration
}
pub fn channels(&self) -> Channels {
self.channel
}
pub fn encoding(&self) -> Encoding {
self.encoding
}
pub fn protected(&self) -> TrackFlag {
self.protected
}
}
/// Information about a single group on the disc, containing [`Track`]s
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Group { pub struct Group {
index: u16, index: u16,
@ -82,6 +116,7 @@ pub struct Group {
tracks: Vec<Track>, tracks: Vec<Track>,
} }
/// Information about a MiniDisc complete with [`Track`]s, [`Group`]s, and metadata.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Disc { pub struct Disc {
title: String, title: String,
@ -260,13 +295,18 @@ impl Disc {
} }
} }
/// Context for interacting with a NetMD device as a wrapper around a [`NetMDInterface`].
///
/// This struct wraps a [`NetMDInterface`] and allows for some higher level
/// functions, but it is still necessary to interact with the [`NetMDInterface`]
/// when performing many operations.
pub struct NetMDContext { pub struct NetMDContext {
interface: NetMDInterface, interface: NetMDInterface,
} }
impl NetMDContext { impl NetMDContext {
/// Create a new context to control a NetMD device /// Create a new context to control a NetMD device
pub async fn new(device: Descriptor) -> Result<Self, InterfaceError> { pub async fn new(device: DeviceInfo) -> Result<Self, InterfaceError> {
let interface = NetMDInterface::new(device).await?; let interface = NetMDInterface::new(device).await?;
Ok(Self { interface }) Ok(Self { interface })
@ -287,6 +327,7 @@ impl NetMDContext {
self.interface.track_change(Direction::Restart).await self.interface.track_change(Direction::Restart).await
} }
/// Get the current status of the device
pub async fn device_status(&mut self) -> Result<DeviceStatus, Box<dyn Error>> { pub async fn device_status(&mut self) -> Result<DeviceStatus, Box<dyn Error>> {
let status = self.interface.status().await?; let status = self.interface.status().await?;
let playback_status = self.interface.playback_status2().await?; let playback_status = self.interface.playback_status2().await?;
@ -317,6 +358,7 @@ impl NetMDContext {
}) })
} }
/// Get a representation of the current disc inserted in the device.
pub async fn list_content(&mut self) -> Result<Disc, Box<dyn Error>> { pub async fn list_content(&mut self) -> Result<Disc, Box<dyn Error>> {
let flags = self.interface.disc_flags().await?; let flags = self.interface.disc_flags().await?;
let title = self.interface.disc_title(false).await?; let title = self.interface.disc_title(false).await?;
@ -392,17 +434,14 @@ impl NetMDContext {
Ok(()) Ok(())
} }
/// Rename a disc while preserving group titles
pub async fn rename_disc( pub async fn rename_disc(
&mut self, &mut self,
new_name: &str, new_name: &str,
new_fw_name: Option<&str>, new_fw_name: Option<&str>,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let new_name = sanitize_half_width_title(new_name); let new_name = sanitize_half_width_title(new_name);
let new_fw_name = if let Some(name) = new_fw_name { let new_fw_name = new_fw_name.map(sanitize_full_width_title);
Some(sanitize_full_width_title(name))
} else {
None
};
let old_name = self.interface.disc_title(false).await?; let old_name = self.interface.disc_title(false).await?;
let old_fw_name = self.interface.disc_title(true).await?; let old_fw_name = self.interface.disc_title(true).await?;
@ -475,6 +514,7 @@ impl NetMDContext {
Ok(()) Ok(())
} }
/// Get a track from the device. This only works with MZ-RH1 devices.
pub async fn upload<F: Fn(usize, usize)>( pub async fn upload<F: Fn(usize, usize)>(
&mut self, &mut self,
track: u16, track: u16,
@ -508,7 +548,7 @@ impl NetMDContext {
Ok((format, header)) Ok((format, header))
} }
pub async fn prepare_download(&mut self) -> Result<(), Box<dyn Error>> { async fn prepare_download(&mut self) -> Result<(), Box<dyn Error>> {
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains( while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(
&self &self
.device_status() .device_status()
@ -528,13 +568,50 @@ impl NetMDContext {
Ok(()) Ok(())
} }
pub async fn download<F>( /// Start downloading an [`MDTrack`] to the device.
///
/// Progress is updated in the `progress_callback` closure.
///
/// # Downloading a track:
/// ```no_run
/// # tokio_test::block_on(async {
/// use minidisc::netmd::DEVICE_IDS_CROSSUSB;
/// use minidisc::netmd::NetMDContext;
/// use minidisc::netmd::interface::{MDTrack, NetMDInterface};
///
/// // Get the minidisc device from cross_usb
/// let device = cross_usb::get_device(DEVICE_IDS_CROSSUSB.to_vec()).await.unwrap();
///
/// // Obtain a NetMDContext and acquire it
/// let mut context = NetMDContext::new(device).await.unwrap();
/// context.interface_mut().acquire().await.unwrap();
///
/// // Read in an audio file to a vec, for LP2 and LP4 this must be encoded properly
/// let track_contents: Vec<u8> =
/// std::fs::read("audio_file.wav")
/// .expect("Could not read track")[0x60..].to_vec();
///
/// // Construct the track
/// let track = MDTrack {
/// chunk_size: 0x400,
/// title: String::from("My Track Title"),
/// format: minidisc::netmd::interface::WireFormat::LP2,
/// full_width_title: None,
/// data: track_contents,
/// };
///
/// // Download it to the player!
/// context.download(
/// track,
/// |out_of: usize, done: usize| println!("Done {} / {}", done, out_of)
/// ).await.expect("Starting download failed");
/// # })
/// ```
pub async fn download<F: Fn(usize, usize)>(
&mut self, &mut self,
track: MDTrack, track: MDTrack,
progress_callback: F, progress_callback: F,
) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>> ) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>>
where
F: Fn(usize, usize),
{ {
self.prepare_download().await?; self.prepare_download().await?;
// Lock the interface by providing it to the session // Lock the interface by providing it to the session
@ -549,16 +626,22 @@ impl NetMDContext {
Ok(result) Ok(result)
} }
/// Get a reference to the underlying interface.
///
/// [`NetMDContext::interface_mut()`] is almost certainly more useful
/// in most cases.
pub fn interface(&self) -> &NetMDInterface { pub fn interface(&self) -> &NetMDInterface {
&self.interface &self.interface
} }
/// Get a mutable reference to the underlying interface.
pub fn interface_mut(&mut self) -> &mut NetMDInterface { pub fn interface_mut(&mut self) -> &mut NetMDInterface {
&mut self.interface &mut self.interface
} }
} }
impl From<NetMDInterface> for NetMDContext { impl From<NetMDInterface> for NetMDContext {
/// Create a context from an already opened interface.
fn from(value: NetMDInterface) -> Self { fn from(value: NetMDInterface) -> Self {
Self { interface: value } Self { interface: value }
} }

View file

@ -9,11 +9,27 @@ use super::interface::DataEncryptorInput;
type DesEcbEnc = ecb::Decryptor<des::Des>; type DesEcbEnc = ecb::Decryptor<des::Des>;
type DesCbcEnc = cbc::Encryptor<des::Des>; type DesCbcEnc = cbc::Encryptor<des::Des>;
pub fn new_thread_encryptor( pub struct Encryptor {
_input: DataEncryptorInput, #[allow(clippy::type_complexity)]
) -> UnboundedReceiver<(Vec<u8>, Vec<u8>, Vec<u8>)> { channel: Option<UnboundedReceiver<(Vec<u8>, Vec<u8>, Vec<u8>)>>,
state: Option<EncryptorState>,
}
struct EncryptorState {
input_data: Vec<u8>,
iv: [u8; 8],
random_key: [u8; 8],
encrypted_random_key: [u8; 8],
default_chunk_size: usize,
current_chunk_size: usize,
offset: usize,
packet_count: usize,
closed: bool,
}
impl Encryptor {
pub fn new_threaded(input: DataEncryptorInput) -> Self {
let (tx, rx) = unbounded_channel::<(Vec<u8>, Vec<u8>, Vec<u8>)>(); let (tx, rx) = unbounded_channel::<(Vec<u8>, Vec<u8>, Vec<u8>)>();
let input = Box::from(_input);
thread::spawn(move || { thread::spawn(move || {
let mut iv = [0u8; 8]; let mut iv = [0u8; 8];
@ -74,5 +90,108 @@ pub fn new_thread_encryptor(
} }
}); });
rx Self {
channel: Some(rx),
state: None
}
}
pub fn new(input: DataEncryptorInput) -> Self {
let iv = [0u8; 8];
// Create the random key
let mut random_key = [0u8; 8];
rand::thread_rng().fill_bytes(&mut random_key);
// Encrypt it with the kek
let mut encrypted_random_key = random_key;
if let Err(x) = DesEcbEnc::new(&input.kek.into())
.decrypt_padded_mut::<NoPadding>(&mut encrypted_random_key)
{
panic!("Cannot create main key {:?}", x)
};
let default_chunk_size = match input.chunk_size {
0 => 0x00100000,
e => e,
};
let packet_count = 0;
let current_chunk_size = 0;
let mut input_data = input.data.clone();
if (input_data.len() % input.frame_size) != 0 {
let padding_remaining = input.frame_size - (input_data.len() % input.frame_size);
input_data.extend(std::iter::repeat(0).take(padding_remaining));
}
let offset: usize = 0;
Encryptor {
channel: None,
state: Some(EncryptorState {
input_data,
iv,
random_key,
encrypted_random_key,
current_chunk_size,
offset,
default_chunk_size,
packet_count,
closed: false,
})
}
}
/// Get the next encrypted value
pub async fn next(&mut self) -> Option<(Vec<u8>, Vec<u8>, Vec<u8>)> {
let output;
if let Some(state) = self.state.as_mut() {
if state.closed {
return None
}
if state.packet_count > 0 {
state.current_chunk_size = state.default_chunk_size;
} else {
state.current_chunk_size = state.default_chunk_size - 24;
}
state.current_chunk_size = std::cmp::min(state.current_chunk_size, state.input_data.len() - state.offset);
let this_data_chunk = &mut state.input_data[state.offset..state.offset + state.current_chunk_size];
DesCbcEnc::new(&state.random_key.into(), &state.iv.into())
.encrypt_padded_mut::<NoPadding>(this_data_chunk, state.current_chunk_size)
.unwrap();
output = Some((
state.encrypted_random_key.to_vec(),
state.iv.to_vec(),
this_data_chunk.to_vec(),
));
state.iv.copy_from_slice(&this_data_chunk[this_data_chunk.len() - 8..]);
state.packet_count += 1;
state.offset += state.current_chunk_size;
} else if let Some(channel) = self.channel.as_mut() {
output = channel.recv().await
} else {
unreachable!("If you got here, this is bad!");
}
output
}
/// Call close to return none from subsequent calls
pub fn close(&mut self) {
if let Some(state) = self.state.as_mut() {
state.closed = true;
} else if let Some(channel) = self.channel.as_mut() {
channel.close()
} else {
unreachable!("If you got here, this is bad!");
}
}
} }

View file

@ -8,17 +8,19 @@ use crate::netmd::utils::{
use cbc::cipher::block_padding::NoPadding; use cbc::cipher::block_padding::NoPadding;
use cbc::cipher::{BlockDecryptMut, BlockEncryptMut, KeyInit, KeyIvInit}; use cbc::cipher::{BlockDecryptMut, BlockEncryptMut, KeyInit, KeyIvInit};
use encoding_rs::SHIFT_JIS; use encoding_rs::SHIFT_JIS;
use log::debug;
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use rand::RngCore; use rand::RngCore;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::time::Duration; use std::time::Duration;
use thiserror::Error; use thiserror::Error;
use tokio::sync::mpsc::UnboundedReceiver;
use super::base::NetMD; use super::base::NetMD;
use super::encryption::Encryptor;
use super::utils::{cross_sleep, to_sjis}; use super::utils::{cross_sleep, to_sjis};
/// An action to take on the player
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum Action { enum Action {
Play = 0x75, Play = 0x75,
@ -27,12 +29,14 @@ enum Action {
Rewind = 0x49, Rewind = 0x49,
} }
/// Direction of playback, or restart track
pub enum Direction { pub enum Direction {
Previous = 0x0002, Previous = 0x0002,
Next = 0x8001, Next = 0x8001,
Restart = 0x0001, Restart = 0x0001,
} }
/// The format of the disc
#[derive(Debug, Clone, Copy, FromPrimitive, PartialEq, Eq)] #[derive(Debug, Clone, Copy, FromPrimitive, PartialEq, Eq)]
pub enum DiscFormat { pub enum DiscFormat {
LP4 = 0, LP4 = 0,
@ -41,6 +45,7 @@ pub enum DiscFormat {
SPStereo = 6, SPStereo = 6,
} }
/// The format of the audio
#[derive(Clone, Hash, Eq, PartialEq, FromPrimitive)] #[derive(Clone, Hash, Eq, PartialEq, FromPrimitive)]
pub enum WireFormat { pub enum WireFormat {
Pcm = 0x00, Pcm = 0x00,
@ -69,34 +74,36 @@ impl WireFormat {
} }
} }
#[derive(Debug, Clone, Copy)] /// The encoding of the audio
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Encoding { pub enum Encoding {
SP = 0x90, SP = 0x90,
LP2 = 0x92, LP2 = 0x92,
LP4 = 0x93, LP4 = 0x93,
} }
impl ToString for Encoding { impl std::fmt::Display for Encoding {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Encoding::SP => String::from("sp"), Encoding::SP => write!(f, "sp"),
Encoding::LP2 => String::from("lp2"), Encoding::LP2 => write!(f, "lp2"),
Encoding::LP4 => String::from("lp4"), Encoding::LP4 => write!(f, "lp4"),
} }
} }
} }
#[derive(Debug, Clone, Copy)] /// The number of channels in the audio
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Channels { pub enum Channels {
Mono = 0x01, Mono = 0x01,
Stereo = 0x00, Stereo = 0x00,
} }
impl ToString for Channels { impl std::fmt::Display for Channels {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Channels::Mono => String::from("mono"), Channels::Mono => write!(f, "mono"),
Channels::Stereo => String::from("stereo"), Channels::Stereo => write!(f, "stereo"),
} }
} }
} }
@ -106,26 +113,29 @@ enum ChannelCount {
Stereo = 2, Stereo = 2,
} }
#[derive(Debug, Clone, Copy, FromPrimitive)] /// The protected flag on a track
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive)]
pub enum TrackFlag { pub enum TrackFlag {
Protected = 0x03, Protected = 0x03,
Unprotected = 0x00, Unprotected = 0x00,
} }
impl ToString for TrackFlag { impl std::fmt::Display for TrackFlag {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
TrackFlag::Protected => String::from("protected"), TrackFlag::Protected => write!(f, "protected"),
TrackFlag::Unprotected => String::from("unprotected"), TrackFlag::Unprotected => write!(f, "unprotected"),
} }
} }
} }
/// The write protect flag on a disc
pub enum DiscFlag { pub enum DiscFlag {
Writable = 0x10, Writable = 0x10,
WriteProtected = 0x40, WriteProtected = 0x40,
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum NetMDLevel { enum NetMDLevel {
Level1 = 0x20, // Network MD Level1 = 0x20, // Network MD
@ -180,6 +190,7 @@ enum DescriptorAction {
Close = 0, Close = 0,
} }
#[derive(Copy, Clone, Debug)]
#[repr(u8)] #[repr(u8)]
enum NetmdStatus { enum NetmdStatus {
// NetMD Protocol return status (first byte of request) // NetMD Protocol return status (first byte of request)
@ -199,8 +210,8 @@ enum NetmdStatus {
} }
#[derive(Error, Debug, Eq, PartialEq, PartialOrd, Ord)] #[derive(Error, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[error("invalid status code")] #[error("invalid status code: {}", 1)]
pub struct StatusError; pub struct StatusError(u16);
impl TryFrom<u8> for NetmdStatus { impl TryFrom<u8> for NetmdStatus {
type Error = StatusError; type Error = StatusError;
@ -219,7 +230,7 @@ impl TryFrom<u8> for NetmdStatus {
0x0c => Ok(NetmdStatus::Implemented), 0x0c => Ok(NetmdStatus::Implemented),
0x0d => Ok(NetmdStatus::Changed), 0x0d => Ok(NetmdStatus::Changed),
0x0f => Ok(NetmdStatus::Interim), 0x0f => Ok(NetmdStatus::Interim),
_ => Err(StatusError), code => Err(StatusError(code as u16)),
} }
} }
} }
@ -232,6 +243,7 @@ struct MediaInfo {
supports_md_clip: u8, supports_md_clip: u8,
} }
/// An error when encrypting packets
#[derive(Error, Debug, Eq, PartialEq, PartialOrd, Ord)] #[derive(Error, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum EncryptionError { pub enum EncryptionError {
#[error("supplied depth of {0} is invalid")] #[error("supplied depth of {0} is invalid")]
@ -241,6 +253,7 @@ pub enum EncryptionError {
InvalidLength(&'static str, usize), InvalidLength(&'static str, usize),
} }
/// An error for any action in the interface
#[derive(Error, Debug, Eq, PartialEq, PartialOrd, Ord)] #[derive(Error, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum InterfaceError { pub enum InterfaceError {
#[error("could not parse data from a device")] #[error("could not parse data from a device")]
@ -293,11 +306,14 @@ pub struct NetMDInterface {
#[allow(dead_code)] #[allow(dead_code)]
impl NetMDInterface { impl NetMDInterface {
/// The maximum number of times to retry after an interim response
const MAX_INTERIM_READ_ATTEMPTS: u8 = 4; const MAX_INTERIM_READ_ATTEMPTS: u8 = 4;
/// The amount of time to wait after an interim response (in milliseconds)
const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100; const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100;
/// Get a new interface to a NetMD device /// Get a new interface to a NetMD device
pub async fn new(device: cross_usb::Descriptor) -> Result<Self, InterfaceError> { pub async fn new(device: cross_usb::DeviceInfo) -> Result<Self, InterfaceError> {
let device = base::NetMD::new(device).await?; let device = base::NetMD::new(device).await?;
Ok(NetMDInterface { device }) Ok(NetMDInterface { device })
} }
@ -320,9 +336,9 @@ impl NetMDInterface {
) )
.await?; .await?;
let mut query = format_query("1809 00 ff00 0000 0000".to_string(), vec![])?; let query = format_query("1809 00 ff00 0000 0000".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query( let res = scan_query(
reply, reply,
@ -430,7 +446,7 @@ impl NetMDInterface {
query.push(0x00); query.push(0x00);
match self.send_query(&mut query, false, false).await { match self.send_query(&query, false, false).await {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(err) => Err(err), Err(err) => Err(err),
} }
@ -439,7 +455,7 @@ impl NetMDInterface {
/// Send a query to the NetMD player /// Send a query to the NetMD player
async fn send_query( async fn send_query(
&mut self, &mut self,
query: &mut Vec<u8>, query: &[u8],
test: bool, test: bool,
accept_interim: bool, accept_interim: bool,
) -> Result<Vec<u8>, InterfaceError> { ) -> Result<Vec<u8>, InterfaceError> {
@ -452,7 +468,7 @@ impl NetMDInterface {
async fn send_command( async fn send_command(
&mut self, &mut self,
query: &mut Vec<u8>, query: &[u8],
test: bool, test: bool,
) -> Result<(), InterfaceError> { ) -> Result<(), InterfaceError> {
let status_byte = match test { let status_byte = match test {
@ -463,7 +479,7 @@ impl NetMDInterface {
let mut new_query = Vec::new(); let mut new_query = Vec::new();
new_query.push(status_byte as u8); new_query.push(status_byte as u8);
new_query.append(query); new_query.extend_from_slice(query);
self.device.send_command(new_query).await?; self.device.send_command(new_query).await?;
@ -478,6 +494,7 @@ impl NetMDInterface {
data = self.device.read_reply(None).await?; data = self.device.read_reply(None).await?;
let status = NetmdStatus::try_from(data[0])?; let status = NetmdStatus::try_from(data[0])?;
debug!("Device status: {:?}", status);
match status { match status {
NetmdStatus::NotImplemented => { NetmdStatus::NotImplemented => {
@ -510,12 +527,12 @@ impl NetMDInterface {
} }
async fn playback_control(&mut self, action: Action) -> Result<(), InterfaceError> { async fn playback_control(&mut self, action: Action) -> Result<(), InterfaceError> {
let mut query = format_query( let query = format_query(
"18c3 ff %b 000000".to_string(), "18c3 ff %b 000000".to_string(),
vec![QueryValue::Number(action as i64)], vec![QueryValue::Number(action as i64)],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "18c3 00 %b 000000".to_string())?; scan_query(reply, "18c3 00 %b 000000".to_string())?;
@ -545,9 +562,9 @@ impl NetMDInterface {
//TODO: Implement fix for LAM-1 //TODO: Implement fix for LAM-1
/// Stop playback /// Stop playback
pub async fn stop(&mut self) -> Result<(), InterfaceError> { pub async fn stop(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("18c5 ff 00000000".to_string(), vec![])?; let query = format_query("18c5 ff 00000000".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "18c5 00 00000000".to_string())?; scan_query(reply, "18c5 00 00000000".to_string())?;
@ -555,8 +572,8 @@ impl NetMDInterface {
} }
pub async fn acquire(&mut self) -> Result<(), InterfaceError> { pub async fn acquire(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("ff 010c ffff ffff ffff ffff ffff ffff".to_string(), vec![])?; let query = format_query("ff 010c ffff ffff ffff ffff ffff ffff".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "ff 010c ffff ffff ffff ffff ffff ffff".to_string())?; scan_query(reply, "ff 010c ffff ffff ffff ffff ffff ffff".to_string())?;
@ -564,9 +581,9 @@ impl NetMDInterface {
} }
pub async fn release(&mut self) -> Result<(), InterfaceError> { pub async fn release(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("ff 0100 ffff ffff ffff ffff ffff ffff".to_string(), vec![])?; let query = format_query("ff 0100 ffff ffff ffff ffff ffff ffff".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "ff 0100 ffff ffff ffff ffff ffff ffff".to_string())?; scan_query(reply, "ff 0100 ffff ffff ffff ffff ffff ffff".to_string())?;
@ -580,12 +597,12 @@ impl NetMDInterface {
) )
.await?; .await?;
let mut query = format_query( let query = format_query(
"1809 8001 0230 8800 0030 8804 00 ff00 00000000".to_string(), "1809 8001 0230 8800 0030 8804 00 ff00 00000000".to_string(),
vec![], vec![],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query( let res = scan_query(
reply, reply,
@ -604,8 +621,6 @@ impl NetMDInterface {
pub async fn disc_present(&mut self) -> Result<bool, InterfaceError> { pub async fn disc_present(&mut self) -> Result<bool, InterfaceError> {
let status = self.status().await?; let status = self.status().await?;
println!("{:X?}", status);
Ok(status[4] == 0x40) Ok(status[4] == 0x40)
} }
@ -617,12 +632,12 @@ impl NetMDInterface {
) )
.await?; .await?;
let mut query = format_query( let query = format_query(
"1809 8001 0330 8802 0030 8805 0030 8806 00 ff00 00000000".to_string(), "1809 8001 0330 8802 0030 8805 0030 8806 00 ff00 00000000".to_string(),
vec![], vec![],
) )
.unwrap(); .unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let result = scan_query( let result = scan_query(
reply, reply,
@ -635,8 +650,8 @@ impl NetMDInterface {
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close) self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close)
.await?; .await?;
if operating_status.len() < 2 { if operating_status.len() < 2 && !operating_status.is_empty() {
return Err(InterfaceError::InvalidStatus(StatusError)); return Err(InterfaceError::InvalidStatus(StatusError(operating_status[0] as u16)));
} }
let operating_status_number = let operating_status_number =
@ -658,19 +673,22 @@ impl NetMDInterface {
) )
.await?; .await?;
let mut query = format_query( let query = format_query(
"1809 8001 0330 %w 0030 8805 0030 %w 00 ff00 00000000".to_string(), "1809 8001 0330 %w 0030 8805 0030 %w 00 ff00 00000000".to_string(),
vec![QueryValue::Number(p1 as i64), QueryValue::Number(p2 as i64)], vec![QueryValue::Number(p1 as i64), QueryValue::Number(p2 as i64)],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query( let res = scan_query(
reply, reply,
"1809 8001 0330 %?%? %?%? %?%? %?%? %?%? %? 1000 00%?0000 %x %?".to_string(), "1809 8001 0330 %?%? %?%? %?%? %?%? %?%? %? 1000 00%?0000 %x %?".to_string(),
)?; )?;
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close) self.change_descriptor_state(
&Descriptor::OperatingStatusBlock,
&DescriptorAction::Close
)
.await?; .await?;
Ok(res[0].to_vec().unwrap()) Ok(res[0].to_vec().unwrap())
@ -689,21 +707,18 @@ impl NetMDInterface {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::OperatingStatusBlock, &Descriptor::OperatingStatusBlock,
&DescriptorAction::OpenRead, &DescriptorAction::OpenRead,
) ).await?;
.await?;
let mut query = format_query( let query = format_query(
"1809 8001 0430 8802 0030 8805 0030 0003 0030 0002 00 ff00 00000000".to_string(), "1809 8001 0430 8802 0030 8805 0030 0003 0030 0002 00 ff00 00000000".to_string(),
vec![], vec![],
) )
.unwrap(); .unwrap();
let reply = match self.send_query(&mut query, false, false).await { //TODO: Ensure this is ok, we might want to do proper error handling
Ok(result) => result, let reply = match self.send_query(&query, false, false).await {
Err(e) => match e { Ok(d) => d,
InterfaceError::Rejected(_) => Vec::new(), Err(_) => return Ok([0u16; 5]),
_ => return Err(e),
},
}; };
let result = scan_query(reply, "1809 8001 0430 %?%? %?%? %?%? %?%? %?%? %?%? %?%? %? %?00 00%?0000 000b 0002 0007 00 %w %B %B %B %B".to_string())?; let result = scan_query(reply, "1809 8001 0430 %?%? %?%? %?%? %?%? %?%? %?%? %?%? %? %?00 00%?0000 000b 0002 0007 00 %w %B %B %B %B".to_string())?;
@ -716,26 +731,28 @@ impl NetMDInterface {
result[4].to_i64().unwrap() as u16, result[4].to_i64().unwrap() as u16,
]; ];
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close) self.change_descriptor_state(
.await?; &Descriptor::OperatingStatusBlock,
&DescriptorAction::Close
).await?;
Ok(final_result) Ok(final_result)
} }
/// Eject the disc from the player if supported /// Eject the disc from the player if supported
pub async fn eject_disc(&mut self) -> Result<(), InterfaceError> { pub async fn eject_disc(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap(); let query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap();
let _reply = self.send_query(&mut query, false, false).await?; let _reply = self.send_query(&query, false, false).await?;
Ok(()) Ok(())
} }
/// Check if the machine has the capability to eject a disc /// Check if the machine has the capability to eject a disc
pub async fn can_eject_disc(&mut self) -> Result<bool, InterfaceError> { pub async fn can_eject_disc(&mut self) -> Result<bool, InterfaceError> {
let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap(); let query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap();
match self.send_query(&mut query, true, false).await { match self.send_query(&query, true, false).await {
Ok(_) => Ok(true), Ok(_) => Ok(true),
Err(error) => Err(error), Err(error) => Err(error),
} }
@ -743,13 +760,13 @@ impl NetMDInterface {
/// Move the playback to a specific track /// Move the playback to a specific track
pub async fn go_to_track(&mut self, track_number: u16) -> Result<u16, InterfaceError> { pub async fn go_to_track(&mut self, track_number: u16) -> Result<u16, InterfaceError> {
let mut query = format_query( let query = format_query(
"1850 ff010000 0000 %w".to_string(), "1850 ff010000 0000 %w".to_string(),
vec![QueryValue::Number(track_number as i64)], vec![QueryValue::Number(track_number as i64)],
) )
.unwrap(); .unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1850 00010000 0000 %w".to_string())?; let res = scan_query(reply, "1850 00010000 0000 %w".to_string())?;
@ -767,7 +784,7 @@ impl NetMDInterface {
second: u8, second: u8,
frame: u8, frame: u8,
) -> Result<u16, InterfaceError> { ) -> Result<u16, InterfaceError> {
let mut query = format_query( let query = format_query(
"1850 ff000000 0000 %w %B%B%B%B".to_string(), "1850 ff000000 0000 %w %B%B%B%B".to_string(),
vec![ vec![
QueryValue::Number(track_number as i64), QueryValue::Number(track_number as i64),
@ -779,7 +796,7 @@ impl NetMDInterface {
) )
.unwrap(); .unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1850 00000000 %?%? %w %B%B%B%B".to_string())?; let res = scan_query(reply, "1850 00000000 %?%? %w %B%B%B%B".to_string())?;
@ -788,14 +805,15 @@ impl NetMDInterface {
Ok(value as u16) Ok(value as u16)
} }
/// Change track in a [`Direction`]
pub async fn track_change(&mut self, direction: Direction) -> Result<(), InterfaceError> { pub async fn track_change(&mut self, direction: Direction) -> Result<(), InterfaceError> {
let mut query = format_query( let query = format_query(
"1850 ff10 00000000 %w".to_string(), "1850 ff10 00000000 %w".to_string(),
vec![QueryValue::Number(direction as i64)], vec![QueryValue::Number(direction as i64)],
) )
.unwrap(); .unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "1850 0010 00000000 %?%?".to_string())?; scan_query(reply, "1850 0010 00000000 %?%?".to_string())?;
@ -804,21 +822,22 @@ impl NetMDInterface {
/// Erase the disc entirely /// Erase the disc entirely
pub async fn erase_disc(&mut self) -> Result<(), InterfaceError> { pub async fn erase_disc(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("1840 ff 0000".to_string(), vec![]).unwrap(); let query = format_query("1840 ff 0000".to_string(), vec![]).unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "1840 00 0000".to_string())?; scan_query(reply, "1840 00 0000".to_string())?;
Ok(()) Ok(())
} }
// TODO: Ensure this is returning the correct value, it // TODO: Ensure this is returning the correct value, it
// looks like it actually might be a 16 bit integer // looks like it actually might be a 16 bit integer
/// Get the flags from a disc
pub async fn disc_flags(&mut self) -> Result<u8, InterfaceError> { pub async fn disc_flags(&mut self) -> Result<u8, InterfaceError> {
self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead) self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead)
.await?; .await?;
let mut query = format_query("1806 01101000 ff00 0001000b".to_string(), vec![]).unwrap(); let query = format_query("1806 01101000 ff00 0001000b".to_string(), vec![]).unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1806 01101000 1000 0001000b %b".to_string()).unwrap(); let res = scan_query(reply, "1806 01101000 1000 0001000b %b".to_string()).unwrap();
@ -828,15 +847,15 @@ impl NetMDInterface {
Ok(res[0].to_i64().unwrap() as u8) Ok(res[0].to_i64().unwrap() as u8)
} }
/// The number of tracks on the disc /// Get the number of tracks on the disc
pub async fn track_count(&mut self) -> Result<u16, InterfaceError> { pub async fn track_count(&mut self) -> Result<u16, InterfaceError> {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead) self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead)
.await?; .await?;
let mut query = let query =
format_query("1806 02101001 3000 1000 ff00 00000000".to_string(), vec![]).unwrap(); format_query("1806 02101001 3000 1000 ff00 00000000".to_string(), vec![]).unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query( let res = scan_query(
reply, reply,
@ -870,7 +889,7 @@ impl NetMDInterface {
false => 0, false => 0,
}; };
let mut query = format_query( let query = format_query(
"1806 02201801 00%b 3000 0a00 ff00 %w%w".to_string(), "1806 02201801 00%b 3000 0a00 ff00 %w%w".to_string(),
vec![ vec![
QueryValue::Number(wchar_value), QueryValue::Number(wchar_value),
@ -880,7 +899,7 @@ impl NetMDInterface {
) )
.unwrap(); .unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
if remaining == 0 { if remaining == 0 {
let res = scan_query( let res = scan_query(
@ -917,7 +936,7 @@ impl NetMDInterface {
Ok(res) Ok(res)
} }
/// Gets the disc title /// Gets the disc title without group information
pub async fn disc_title(&mut self, wchar: bool) -> Result<String, InterfaceError> { pub async fn disc_title(&mut self, wchar: bool) -> Result<String, InterfaceError> {
let mut title = self.raw_disc_title(wchar).await?; let mut title = self.raw_disc_title(wchar).await?;
@ -1021,11 +1040,13 @@ impl NetMDInterface {
)); ));
} }
let mut remaining_tracks = Vec::new();
for i in 0..track_count { for i in 0..track_count {
if !track_dict.contains_key(&i) { if !track_dict.contains_key(&i) {
result.insert(0, (None, None, Vec::from([i]))) remaining_tracks.push(i);
} }
} }
result.insert(0, (None, None, remaining_tracks));
Ok(result) Ok(result)
} }
@ -1051,7 +1072,7 @@ impl NetMDInterface {
let mut track_titles: Vec<String> = vec![]; let mut track_titles: Vec<String> = vec![];
for i in tracks { for i in tracks {
let mut query = format_query( let query = format_query(
"1806 022018%b %w 3000 0a00 ff00 00000000".to_string(), "1806 022018%b %w 3000 0a00 ff00 00000000".to_string(),
vec![ vec![
QueryValue::Number(wchar_value), QueryValue::Number(wchar_value),
@ -1060,7 +1081,7 @@ impl NetMDInterface {
) )
.unwrap(); .unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query( let res = scan_query(
reply, reply,
@ -1095,6 +1116,9 @@ impl NetMDInterface {
} }
// Sets the title of the disc // Sets the title of the disc
//
// Caution: This does not respect groups. Use the functions available in
// NetMDContext to properly rename a disc.
pub async fn set_disc_title(&mut self, title: &str, wchar: bool) -> Result<(), InterfaceError> { pub async fn set_disc_title(&mut self, title: &str, wchar: bool) -> Result<(), InterfaceError> {
let current_title = self.raw_disc_title(wchar).await?; let current_title = self.raw_disc_title(wchar).await?;
if current_title == title { if current_title == title {
@ -1127,7 +1151,7 @@ impl NetMDInterface {
.await? .await?
} }
let mut query = format_query( let query = format_query(
"1807 02201801 00%b 3000 0a00 5000 %w 0000 %w %*".to_string(), "1807 02201801 00%b 3000 0a00 5000 %w 0000 %w %*".to_string(),
vec![ vec![
QueryValue::Number(wchar_value), QueryValue::Number(wchar_value),
@ -1137,7 +1161,7 @@ impl NetMDInterface {
], ],
)?; )?;
let _ = self.send_query(&mut query, false, false).await; let _ = self.send_query(&query, false, false).await;
if self.device.vendor_id() == 0x04dd { if self.device.vendor_id() == 0x04dd {
self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close) self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close)
@ -1191,7 +1215,7 @@ impl NetMDInterface {
self.change_descriptor_state(&descriptor, &DescriptorAction::OpenWrite) self.change_descriptor_state(&descriptor, &DescriptorAction::OpenWrite)
.await?; .await?;
let mut query = format_query( let query = format_query(
"1807 022018%b %w 3000 0a00 5000 %w 0000 %w %*".to_string(), "1807 022018%b %w 3000 0a00 5000 %w 0000 %w %*".to_string(),
vec![ vec![
QueryValue::Number(wchar_value), QueryValue::Number(wchar_value),
@ -1201,7 +1225,7 @@ impl NetMDInterface {
QueryValue::Array(new_title), QueryValue::Array(new_title),
], ],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let _ = scan_query( let _ = scan_query(
reply, reply,
@ -1215,19 +1239,19 @@ impl NetMDInterface {
/// Erases a track from the disc's UTOC /// Erases a track from the disc's UTOC
pub async fn erase_track(&mut self, track: u16) -> Result<(), InterfaceError> { pub async fn erase_track(&mut self, track: u16) -> Result<(), InterfaceError> {
let mut query = format_query( let query = format_query(
"1840 ff01 00 201001 %w".to_string(), "1840 ff01 00 201001 %w".to_string(),
vec![QueryValue::Number(track as i64)], vec![QueryValue::Number(track as i64)],
)?; )?;
let _result = self.send_query(&mut query, false, false).await; let _result = self.send_query(&query, false, false).await;
Ok(()) Ok(())
} }
/// Moves a track to another index on the disc /// Moves a track to another index on the disc
pub async fn move_track(&mut self, source: u16, dest: u16) -> Result<(), InterfaceError> { pub async fn move_track(&mut self, source: u16, dest: u16) -> Result<(), InterfaceError> {
let mut query = format_query( let query = format_query(
"1843 ff00 00 201001 %w 201001 %w".to_string(), "1843 ff00 00 201001 %w 201001 %w".to_string(),
vec![ vec![
QueryValue::Number(source as i64), QueryValue::Number(source as i64),
@ -1235,7 +1259,7 @@ impl NetMDInterface {
], ],
)?; )?;
let _result = self.send_query(&mut query, false, false).await; let _result = self.send_query(&query, false, false).await;
Ok(()) Ok(())
} }
@ -1250,7 +1274,7 @@ impl NetMDInterface {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead) self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead)
.await?; .await?;
let mut query = format_query( let query = format_query(
"1806 02201001 %w %w %w ff00 00000000".to_string(), "1806 02201001 %w %w %w ff00 00000000".to_string(),
vec![ vec![
QueryValue::Number(track as i64), QueryValue::Number(track as i64),
@ -1259,7 +1283,7 @@ impl NetMDInterface {
], ],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query( let res = scan_query(
reply, reply,
"1806 02201001 %?%? %?%? %?%? 1000 00%?0000 %x".to_string(), "1806 02201001 %?%? %?%? %?%? 1000 00%?0000 %x".to_string(),
@ -1282,7 +1306,7 @@ impl NetMDInterface {
.await?; .await?;
for track in tracks { for track in tracks {
let mut query = format_query( let query = format_query(
"1806 02201001 %w %w %w ff00 00000000".to_string(), "1806 02201001 %w %w %w ff00 00000000".to_string(),
vec![ vec![
QueryValue::Number(track as i64), QueryValue::Number(track as i64),
@ -1291,7 +1315,7 @@ impl NetMDInterface {
], ],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query( let res = scan_query(
reply, reply,
@ -1358,11 +1382,11 @@ impl NetMDInterface {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead) self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead)
.await?; .await?;
let mut query = format_query( let query = format_query(
"1806 01201001 %w ff00 00010008".to_string(), "1806 01201001 %w ff00 00010008".to_string(),
vec![QueryValue::Number(track as i64)], vec![QueryValue::Number(track as i64)],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1806 01201001 %?%? 10 00 00010008 %b".to_string())?; let res = scan_query(reply, "1806 01201001 %?%? 10 00 00010008 %b".to_string())?;
@ -1377,8 +1401,8 @@ impl NetMDInterface {
self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead) self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead)
.await?; .await?;
let mut query = format_query("1806 02101000 3080 0300 ff00 00000000".to_string(), vec![])?; let query = format_query("1806 02101000 3080 0300 ff00 00000000".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
// 8003 changed to %?03 - Panasonic returns 0803 instead. This byte's meaning is unknown // 8003 changed to %?03 - Panasonic returns 0803 instead. This byte's meaning is unknown
let res = scan_query( let res = scan_query(
@ -1410,12 +1434,12 @@ impl NetMDInterface {
) )
.await?; .await?;
let mut query = format_query( let query = format_query(
"1809 8001 0330 8801 0030 8805 0030 8807 00 ff00 00000000".to_string(), "1809 8001 0330 8801 0030 8805 0030 8807 00 ff00 00000000".to_string(),
vec![], vec![],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1809 8001 0330 8801 0030 8805 0030 8807 00 1000 000e0000 000c 8805 0008 80e0 0110 %b %b 4000".to_string())?; let res = scan_query(reply, "1809 8001 0330 8801 0030 8805 0030 8807 00 1000 000e0000 000c 8805 0008 80e0 0110 %b %b 4000".to_string())?;
@ -1433,12 +1457,12 @@ impl NetMDInterface {
track: u16, track: u16,
progress_callback: Option<F>, progress_callback: Option<F>,
) -> Result<(DiscFormat, u16, Vec<u8>), InterfaceError> { ) -> Result<(DiscFormat, u16, Vec<u8>), InterfaceError> {
let mut query = format_query( let query = format_query(
"1800 080046 f003010330 ff00 1001 %w".to_string(), "1800 080046 f003010330 ff00 1001 %w".to_string(),
vec![QueryValue::Number((track + 1) as i64)], vec![QueryValue::Number((track + 1) as i64)],
)?; )?;
let reply = self.send_query(&mut query, false, true).await?; let reply = self.send_query(&query, false, true).await?;
let res = scan_query( let res = scan_query(
reply, reply,
@ -1473,28 +1497,28 @@ impl NetMDInterface {
} }
pub async fn disable_new_track_protection(&mut self, val: u16) -> Result<(), InterfaceError> { pub async fn disable_new_track_protection(&mut self, val: u16) -> Result<(), InterfaceError> {
let mut query = format_query( let query = format_query(
"1800 080046 f0030103 2b ff %w".to_string(), "1800 080046 f0030103 2b ff %w".to_string(),
vec![QueryValue::Number(val as i64)], vec![QueryValue::Number(val as i64)],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "1800 080046 f0030103 2b 00 %?%?".to_string())?; scan_query(reply, "1800 080046 f0030103 2b 00 %?%?".to_string())?;
Ok(()) Ok(())
} }
pub async fn enter_secure_session(&mut self) -> Result<(), InterfaceError> { pub async fn enter_secure_session(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("1800 080046 f0030103 80 ff".to_string(), vec![])?; let query = format_query("1800 080046 f0030103 80 ff".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "1800 080046 f0030103 80 00".to_string())?; scan_query(reply, "1800 080046 f0030103 80 00".to_string())?;
Ok(()) Ok(())
} }
pub async fn leave_secure_session(&mut self) -> Result<(), InterfaceError> { pub async fn leave_secure_session(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("1800 080046 f0030103 81 ff".to_string(), vec![])?; let query = format_query("1800 080046 f0030103 81 ff".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "1800 080046 f0030103 81 00".to_string())?; scan_query(reply, "1800 080046 f0030103 81 00".to_string())?;
Ok(()) Ok(())
} }
@ -1506,9 +1530,9 @@ impl NetMDInterface {
/// ///
/// The leaf ID is a 8-byte constant /// The leaf ID is a 8-byte constant
pub async fn leaf_id(&mut self) -> Result<[u8; 8], InterfaceError> { pub async fn leaf_id(&mut self) -> Result<[u8; 8], InterfaceError> {
let mut query = format_query("1800 080046 f0030103 11 ff".to_string(), vec![])?; let query = format_query("1800 080046 f0030103 11 ff".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1800 080046 f0030103 11 00 %*".to_string())?; let res = scan_query(reply, "1800 080046 f0030103 11 00 %*".to_string())?;
Ok(res[0].to_vec().unwrap().try_into().unwrap()) Ok(res[0].to_vec().unwrap().try_into().unwrap())
@ -1530,7 +1554,7 @@ impl NetMDInterface {
let keychains = keychain.concat(); let keychains = keychain.concat();
let mut query = format_query( let query = format_query(
"1800 080046 f0030103 12 ff %w 0000 %w %d %d %d 00000000 %* %*".to_string(), "1800 080046 f0030103 12 ff %w 0000 %w %d %d %d 00000000 %* %*".to_string(),
vec![ vec![
QueryValue::Number(databytes as i64), QueryValue::Number(databytes as i64),
@ -1543,7 +1567,7 @@ impl NetMDInterface {
], ],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query( scan_query(
reply, reply,
@ -1564,12 +1588,12 @@ impl NetMDInterface {
))?; ))?;
} }
let mut query = format_query( let query = format_query(
"1800 080046 f0030103 20 ff 000000 %*".to_string(), "1800 080046 f0030103 20 ff 000000 %*".to_string(),
vec![QueryValue::Array(hostnonce)], vec![QueryValue::Array(hostnonce)],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1800 080046 f0030103 20 %? 000000 %#".to_string())?; let res = scan_query(reply, "1800 080046 f0030103 20 %? 000000 %#".to_string())?;
@ -1577,9 +1601,9 @@ impl NetMDInterface {
} }
pub async fn session_key_forget(&mut self) -> Result<(), InterfaceError> { pub async fn session_key_forget(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("1800 080046 f0030103 21 ff 000000".to_string(), vec![])?; let query = format_query("1800 080046 f0030103 21 ff 000000".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let _ = scan_query(reply, "1800 080046 f0030103 21 00 000000".to_string())?; let _ = scan_query(reply, "1800 080046 f0030103 21 00 000000".to_string())?;
Ok(()) Ok(())
@ -1615,12 +1639,12 @@ impl NetMDInterface {
.encrypt_padded_mut::<NoPadding>(message.as_mut_slice(), 32) .encrypt_padded_mut::<NoPadding>(message.as_mut_slice(), 32)
.unwrap(); .unwrap();
let mut query = format_query( let query = format_query(
"1800 080046 f0030103 22 ff 0000 %*".to_string(), "1800 080046 f0030103 22 ff 0000 %*".to_string(),
vec![QueryValue::Array(message)], vec![QueryValue::Array(message)],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "1800 080046 f0030103 22 00 0000".to_string())?; scan_query(reply, "1800 080046 f0030103 22 00 0000".to_string())?;
@ -1644,7 +1668,7 @@ impl NetMDInterface {
.encrypt_padded_mut::<NoPadding>(&mut message, 8) .encrypt_padded_mut::<NoPadding>(&mut message, 8)
.unwrap(); .unwrap();
let mut query = format_query( let query = format_query(
"1800 080046 f0030103 48 ff 00 1001 %w %*".to_string(), "1800 080046 f0030103 48 ff 00 1001 %w %*".to_string(),
vec![ vec![
QueryValue::Number(track_number as i64), QueryValue::Number(track_number as i64),
@ -1652,7 +1676,7 @@ impl NetMDInterface {
], ],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
scan_query(reply, "1800 080046 f0030103 48 00 00 1001 %?%?".to_string())?; scan_query(reply, "1800 080046 f0030103 48 00 00 1001 %?%?".to_string())?;
@ -1660,19 +1684,16 @@ impl NetMDInterface {
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub async fn send_track<F>( pub async fn send_track<F: Fn(usize, usize)>(
&mut self, &mut self,
wireformat: u8, wireformat: u8,
discformat: u8, discformat: u8,
frames: u32, frames: u32,
pkt_size: u32, pkt_size: u32,
// key, iv, data mut packets: Encryptor,
mut packets: UnboundedReceiver<(Vec<u8>, Vec<u8>, Vec<u8>)>,
hex_session_key: &[u8], hex_session_key: &[u8],
progress_callback: F, progress_callback: F,
) -> Result<(u16, Vec<u8>, Vec<u8>), InterfaceError> ) -> Result<(u16, Vec<u8>, Vec<u8>), InterfaceError>
where
F: Fn(usize, usize),
{ {
if hex_session_key.len() != 8 { if hex_session_key.len() != 8 {
return Err(EncryptionError::InvalidLength( return Err(EncryptionError::InvalidLength(
@ -1686,7 +1707,7 @@ impl NetMDInterface {
let total_bytes: usize = (pkt_size + 24) as usize; //framesizedict[wireformat] * frames + pktcount * 24; let total_bytes: usize = (pkt_size + 24) as usize; //framesizedict[wireformat] * frames + pktcount * 24;
let mut query = format_query( let query = format_query(
"1800 080046 f0030103 28 ff 000100 1001 ffff 00 %b %b %d %d".to_string(), "1800 080046 f0030103 28 ff 000100 1001 ffff 00 %b %b %d %d".to_string(),
vec![ vec![
QueryValue::Number(wireformat as i64), QueryValue::Number(wireformat as i64),
@ -1695,7 +1716,7 @@ impl NetMDInterface {
QueryValue::Number(total_bytes as i64), QueryValue::Number(total_bytes as i64),
], ],
)?; )?;
let mut reply = self.send_query(&mut query, false, true).await?; let mut reply = self.send_query(&query, false, true).await?;
scan_query( scan_query(
reply, reply,
"1800 080046 f0030103 28 00 000100 1001 %?%? 00 %*".to_string(), "1800 080046 f0030103 28 00 000100 1001 %?%? 00 %*".to_string(),
@ -1705,10 +1726,10 @@ impl NetMDInterface {
// Sharps are slow // Sharps are slow
cross_sleep(Duration::from_millis(200)).await; cross_sleep(Duration::from_millis(200)).await;
let mut _written_bytes = 0; let mut written_bytes = 0;
let mut packet_count = 0; let mut packet_count = 0;
while let Some((key, iv, data)) = packets.recv().await { while let Some((key, iv, data)) = packets.next().await {
let binpack = if packet_count == 0 { let binpack = if packet_count == 0 {
let packed_length: Vec<u8> = pkt_size.to_be_bytes().to_vec(); let packed_length: Vec<u8> = pkt_size.to_be_bytes().to_vec();
[vec![0, 0, 0, 0], packed_length, key, iv, data].concat() [vec![0, 0, 0, 0], packed_length, key, iv, data].concat()
@ -1716,10 +1737,10 @@ impl NetMDInterface {
data data
}; };
self.device.write_bulk(&binpack).await?; self.device.write_bulk(&binpack).await?;
_written_bytes += binpack.len(); written_bytes += binpack.len();
packet_count += 1; packet_count += 1;
(progress_callback)(total_bytes, _written_bytes); (progress_callback)(total_bytes, written_bytes);
if total_bytes == _written_bytes { if total_bytes == written_bytes {
packets.close(); packets.close();
break; break;
} }
@ -1744,11 +1765,11 @@ impl NetMDInterface {
} }
pub async fn track_uuid(&mut self, track: u16) -> Result<String, InterfaceError> { pub async fn track_uuid(&mut self, track: u16) -> Result<String, InterfaceError> {
let mut query = format_query( let query = format_query(
"1800 080046 f0030103 23 ff 1001 %w".to_string(), "1800 080046 f0030103 23 ff 1001 %w".to_string(),
vec![QueryValue::Number(track as i64)], vec![QueryValue::Number(track as i64)],
)?; )?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&query, false, false).await?;
let res = scan_query(reply, "1800 080046 f0030103 23 00 1001 %?%? %*".to_string())?; let res = scan_query(reply, "1800 080046 f0030103 23 00 1001 %?%? %*".to_string())?;
@ -1756,8 +1777,8 @@ impl NetMDInterface {
} }
pub async fn terminate(&mut self) -> Result<(), InterfaceError> { pub async fn terminate(&mut self) -> Result<(), InterfaceError> {
let mut query = format_query("1800 080046 f0030103 2a ff00".to_string(), vec![])?; let query = format_query("1800 080046 f0030103 2a ff00".to_string(), vec![])?;
self.send_query(&mut query, false, false).await?; self.send_query(&query, false, false).await?;
Ok(()) Ok(())
} }
@ -1768,7 +1789,7 @@ type DesCbcEnc = cbc::Encryptor<des::Des>;
type DesCbcDec = cbc::Decryptor<des::Des>; type DesCbcDec = cbc::Decryptor<des::Des>;
type TDesCbcEnc = cbc::Encryptor<des::TdesEde3>; type TDesCbcEnc = cbc::Encryptor<des::TdesEde3>;
pub fn retailmac(key: &[u8], value: &[u8], iv: &[u8; 8]) -> Vec<u8> { pub(super) fn retailmac(key: &[u8], value: &[u8], iv: &[u8; 8]) -> Vec<u8> {
let mut subkey_a = [0u8; 8]; let mut subkey_a = [0u8; 8];
subkey_a.copy_from_slice(&key[0..8]); subkey_a.copy_from_slice(&key[0..8]);
@ -1794,13 +1815,13 @@ pub fn retailmac(key: &[u8], value: &[u8], iv: &[u8; 8]) -> Vec<u8> {
end[..8].to_vec() end[..8].to_vec()
} }
pub struct EKBData { pub(super) struct EKBData {
chains: [[u8; 16]; 2], chains: [[u8; 16]; 2],
depth: i32, depth: i32,
signature: [u8; 24], signature: [u8; 24],
} }
pub struct EKBOpenSource; pub(super) struct EKBOpenSource;
impl EKBOpenSource { impl EKBOpenSource {
pub fn root_key(&self) -> [u8; 16] { pub fn root_key(&self) -> [u8; 16] {
@ -1841,10 +1862,6 @@ pub struct MDTrack {
pub data: Vec<u8>, pub data: Vec<u8>,
pub chunk_size: usize, pub chunk_size: usize,
pub full_width_title: Option<String>, pub full_width_title: Option<String>,
#[allow(clippy::type_complexity)]
pub encrypt_packets_iterator:
Box<dyn Fn(DataEncryptorInput) -> UnboundedReceiver<(Vec<u8>, Vec<u8>, Vec<u8>)>>,
} }
pub struct DataEncryptorInput { pub struct DataEncryptorInput {
@ -1899,8 +1916,19 @@ impl MDTrack {
[0x14, 0xe3, 0x83, 0x4e, 0xe2, 0xd3, 0xcc, 0xa5] [0x14, 0xe3, 0x83, 0x4e, 0xe2, 0xd3, 0xcc, 0xa5]
} }
pub fn get_encrypting_iterator(&mut self) -> UnboundedReceiver<(Vec<u8>, Vec<u8>, Vec<u8>)> { #[cfg(not(target_family = "wasm"))]
(self.encrypt_packets_iterator)(DataEncryptorInput { pub fn get_encrypting_iterator(&mut self) -> Encryptor {
Encryptor::new_threaded(DataEncryptorInput {
kek: self.get_kek(),
frame_size: self.frame_size(),
chunk_size: self.chunk_size(),
data: std::mem::take(&mut self.data),
})
}
#[cfg(target_family = "wasm")]
pub fn get_encrypting_iterator(&mut self) -> Encryptor {
Encryptor::new(DataEncryptorInput {
kek: self.get_kek(), kek: self.get_kek(),
frame_size: self.frame_size(), frame_size: self.frame_size(),
chunk_size: self.chunk_size(), chunk_size: self.chunk_size(),
@ -1909,7 +1937,7 @@ impl MDTrack {
} }
} }
pub struct MDSession<'a> { pub(super) struct MDSession<'a> {
pub md: &'a mut NetMDInterface, pub md: &'a mut NetMDInterface,
pub ekb_object: EKBOpenSource, pub ekb_object: EKBOpenSource,
pub hex_session_key: Option<Vec<u8>>, pub hex_session_key: Option<Vec<u8>>,
@ -1948,14 +1976,12 @@ impl<'a> MDSession<'a> {
Ok(()) Ok(())
} }
pub async fn download_track<F>( pub async fn download_track<F: Fn(usize, usize)>(
&mut self, &mut self,
mut track: MDTrack, mut track: MDTrack,
progress_callback: F, progress_callback: F,
disc_format: Option<DiscFormat>, disc_format: Option<DiscFormat>,
) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>> ) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>>
where
F: Fn(usize, usize),
{ {
if self.hex_session_key.is_none() { if self.hex_session_key.is_none() {
return Err("Cannot download a track using a non-init()'ed session!".into()); return Err("Cannot download a track using a non-init()'ed session!".into());

View file

@ -1,6 +1,7 @@
use phf::phf_map; use phf::phf_map;
pub static MAPPINGS_JP: phf::Map<&'static str, &'static str> = phf_map![ /// Mappings to convert to fullwidth from half width
pub static MAPPINGS_JP: phf::Map<&str, &str> = phf_map![
"!" =>"", "!" =>"",
"\"" =>"", "\"" =>"",
"#" =>"", "#" =>"",
@ -191,7 +192,9 @@ pub static MAPPINGS_JP: phf::Map<&'static str, &'static str> = phf_map![
"" =>"", "" =>"",
"" =>"" "" =>""
]; ];
pub static MAPPINGS_RU: phf::Map<&'static str, &'static str> = phf_map![
/// Mappings to romanize Russian
pub static MAPPINGS_RU: phf::Map<&str, &str> = phf_map![
"а" =>"a", "а" =>"a",
"б" =>"b", "б" =>"b",
"в" =>"v", "в" =>"v",
@ -259,7 +262,9 @@ pub static MAPPINGS_RU: phf::Map<&'static str, &'static str> = phf_map![
"Ю" =>"Iu", "Ю" =>"Iu",
"Я" =>"Ia" "Я" =>"Ia"
]; ];
pub static MAPPINGS_DE: phf::Map<&'static str, &'static str> = phf_map![
/// Mappings to remove accents for German
pub static MAPPINGS_DE: phf::Map<&str, &str> = phf_map![
"Ä" => "Ae", "Ä" => "Ae",
"ä" => "ae", "ä" => "ae",
"Ö" => "Oe", "Ö" => "Oe",
@ -268,7 +273,9 @@ pub static MAPPINGS_DE: phf::Map<&'static str, &'static str> = phf_map![
"ü" => "ue", "ü" => "ue",
"ß" => "ss" "ß" => "ss"
]; ];
pub static MAPPINGS_HW: phf::Map<&'static str, &'static str> = phf_map![
/// Mappings to make Japanese half width
pub static MAPPINGS_HW: phf::Map<&str, &str> = phf_map![
"" =>"-", "" =>"-",
"" =>"-", "" =>"-",
"" =>"", "" =>"",
@ -548,7 +555,9 @@ pub static MAPPINGS_HW: phf::Map<&'static str, &'static str> = phf_map![
"" =>"", "" =>"",
"" =>"", "" =>"",
]; ];
pub static ALLOWED_HW_KANA: &[&'static str] = &[
/// A list of allowed half width kana
pub static ALLOWED_HW_KANA: &[&str] = &[
"-", "-", "", "", "", "", "", "", "", "", "", "", "", "ガ", "", "ギ", "", "グ", "-", "-", "", "", "", "", "", "", "", "", "", "", "", "ガ", "", "ギ", "", "グ",
"", "ゲ", "", "ゴ", "", "ザ", "", "ジ", "", "ズ", "", "ゼ", "ソ", "ゾ", "", "ダ", "", "", "ゲ", "", "ゴ", "", "ザ", "", "ジ", "", "ズ", "", "ゼ", "ソ", "ゾ", "", "ダ", "",
"ヂ", "", "", "ヅ", "", "デ", "", "ド", "", "", "", "", "", "", "バ", "パ", "", "ヂ", "", "", "ヅ", "", "デ", "", "ド", "", "", "", "", "", "", "バ", "パ", "",
@ -566,6 +575,8 @@ pub static ALLOWED_HW_KANA: &[&'static str] = &[
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "ヴ", "", "", "", "", "", "", "", "ヴ", "", "",
]; ];
/// Characters which take up more than one byte
pub static MULTI_BYTE_CHARS: phf::Map<char, u8> = phf_map![ pub static MULTI_BYTE_CHARS: phf::Map<char, u8> = phf_map![
'ガ' => 1, 'ガ' => 1,
'ギ' => 1, 'ギ' => 1,
@ -634,6 +645,8 @@ pub static MULTI_BYTE_CHARS: phf::Map<char, u8> = phf_map![
'ゝ' => 1, 'ゝ' => 1,
'ゞ' => 1 'ゞ' => 1
]; ];
/// Half width to full width conversion for group ranges
pub static HW_TO_FW_RANGE_MAP: phf::Map<char, char> = phf_map![ pub static HW_TO_FW_RANGE_MAP: phf::Map<char, char> = phf_map![
'0' => '', '0' => '',
'1' => '', '1' => '',

View file

@ -1,8 +1,5 @@
/*! //! This module contains all functionality for interacting with NetMD minidisc
* This crate is an interface in rust to control NetMD and Hi-MD minidisc devices. //! devices.
*
* Documentation coming soon
*/
pub mod base; pub mod base;
pub mod commands; pub mod commands;
@ -11,3 +8,15 @@ pub mod interface;
mod mappings; mod mappings;
mod query_utils; mod query_utils;
mod utils; mod utils;
#[doc(inline)]
pub use base::DEVICE_IDS_CROSSUSB;
#[doc(inline)]
pub use commands::NetMDContext;
#[doc(inline)]
pub use interface::NetMDInterface;
#[doc(inline)]
pub use base::NetMD;

View file

@ -90,13 +90,16 @@ pub enum QueryError {
#[error("unrecognized format character: `{0}`")] #[error("unrecognized format character: `{0}`")]
UnrecognizedChar(char), UnrecognizedChar(char),
#[error("Format and input mismatch at {index}: expected {expected:#04x}, got {actual:#04x} (format {format_string})")] #[error("format and input mismatch at {index}: expected {expected:#04x}, got {actual:#04x} (format {format_string})")]
InputMismatch { InputMismatch {
index: usize, index: usize,
expected: u8, expected: u8,
actual: u8, actual: u8,
format_string: String, format_string: String,
}, },
#[error("input data is empty")]
EmptyData,
} }
/// Formats a query using a standard input to send to the player /// Formats a query using a standard input to send to the player
@ -196,6 +199,14 @@ pub fn format_query(format: String, args: Vec<QueryValue>) -> Result<Vec<u8>, Qu
/// Scans a result using a standard input to recieve from the player /// Scans a result using a standard input to recieve from the player
pub fn scan_query(query_result: Vec<u8>, format: String) -> Result<Vec<QueryValue>, QueryError> { pub fn scan_query(query_result: Vec<u8>, format: String) -> Result<Vec<QueryValue>, QueryError> {
if DEBUG {
println!("RECV<<< F: {}", format);
}
if query_result.is_empty() {
return Err(QueryError::EmptyData)
}
let mut result: Vec<QueryValue> = Vec::new(); let mut result: Vec<QueryValue> = Vec::new();
let initial_length = query_result.len(); let initial_length = query_result.len();