Skip to content

Commit

Permalink
Ran cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
G2-Games committed Mar 10, 2024
1 parent 3f91302 commit e426805
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 118 deletions.
18 changes: 10 additions & 8 deletions src/netmd/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::time::Duration;

// USB stuff
//use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer};
use cross_usb::{UsbDevice, UsbInterface};
use cross_usb::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
use cross_usb::{UsbDevice, UsbInterface};

use super::utils::cross_sleep;
//use nusb::{Device, DeviceInfo, Interface};
Expand Down Expand Up @@ -191,9 +191,9 @@ impl NetMD {
// First poll to ensure the device is ready
match self.poll().await {
Ok(buffer) => match buffer.1[2] {
0 => 0,
_ => return Err("Device not ready!".into()),
},
0 => 0,
_ => return Err("Device not ready!".into()),
},
Err(error) => return Err(error),
};

Expand Down Expand Up @@ -249,12 +249,11 @@ impl NetMD {
length = self.poll().await?.0;

if length > 0 {
break
break;
}

// Back off while trying again
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL
* (u32::pow(2, attempt) - 1);
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL * (u32::pow(2, attempt) - 1);

cross_sleep(sleep_time).await;
}
Expand Down Expand Up @@ -323,6 +322,9 @@ impl NetMD {
}

pub async fn write_bulk(&mut self, data: &[u8]) -> Result<usize, Box<dyn Error>> {
Ok(self.usb_interface.bulk_out(BULK_WRITE_ENDPOINT, data).await?)
Ok(self
.usb_interface
.bulk_out(BULK_WRITE_ENDPOINT, data)
.await?)
}
}
40 changes: 29 additions & 11 deletions src/netmd/commands.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#![cfg_attr(debug_assertions, allow(dead_code))]
use std::error::Error;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::error::Error;

use super::interface::{NetMDInterface, MDTrack, MDSession};
use super::interface::{MDSession, MDTrack, NetMDInterface};
use super::utils::cross_sleep;

#[derive(FromPrimitive)]
#[derive(PartialEq)]
pub enum OperatingStatus{
#[derive(FromPrimitive, PartialEq)]
pub enum OperatingStatus {
Ready = 50687,
Playing = 50037,
Paused = 50045,
Expand Down Expand Up @@ -49,17 +48,27 @@ pub async fn device_status(interface: &mut NetMDInterface) -> Result<DeviceStatu
state = Some(OperatingStatus::Ready);
}

let time = Time{
let time = Time {
minute: position[2],
second: position[3],
frame: position[4],
};

Ok(DeviceStatus { disc_present, state, track, time })
Ok(DeviceStatus {
disc_present,
state,
track,
time,
})
}

pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<dyn Error>>{
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(&device_status(interface).await?.state.unwrap_or(OperatingStatus::NoDisc)) {
pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<dyn Error>> {
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(
&device_status(interface)
.await?
.state
.unwrap_or(OperatingStatus::NoDisc),
) {
cross_sleep(200).await;
}

Expand All @@ -72,11 +81,20 @@ pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<
Ok(())
}

pub async fn download<F>(interface: &mut NetMDInterface, track: MDTrack, progress_callback: F) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>> where F: Fn(usize, usize){
pub async fn download<F>(
interface: &mut NetMDInterface,
track: MDTrack,
progress_callback: F,
) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>>
where
F: Fn(usize, usize),
{
prepare_download(interface).await?;
let mut session = MDSession::new(interface);
session.init().await?;
let result = session.download_track(track, progress_callback, None).await?;
let result = session
.download_track(track, progress_callback, None)
.await?;
session.close().await?;
interface.release().await?;

Expand Down
35 changes: 23 additions & 12 deletions src/netmd/encryption.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::thread;
use cbc::cipher::block_padding::NoPadding;
use cbc::cipher::{KeyInit, BlockDecryptMut, KeyIvInit, BlockEncryptMut};
use tokio::sync::mpsc::{UnboundedReceiver, unbounded_channel};
use cbc::cipher::{BlockDecryptMut, BlockEncryptMut, KeyInit, KeyIvInit};
use rand::RngCore;
use std::thread;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};

use super::interface::DataEncryptorInput;

type DesEcbEnc = ecb::Decryptor<des::Des>;
type DesCbcEnc = cbc::Encryptor<des::Des>;

pub fn new_thread_encryptor(_input: DataEncryptorInput) -> UnboundedReceiver<(Vec<u8>, Vec<u8>, Vec<u8>)> {
pub fn new_thread_encryptor(
_input: DataEncryptorInput,
) -> UnboundedReceiver<(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 || {
Expand All @@ -21,14 +23,16 @@ pub fn new_thread_encryptor(_input: DataEncryptorInput) -> UnboundedReceiver<(Ve

// Encrypt it with the kek
let mut encrypted_random_key = random_key.clone();
match DesEcbEnc::new(&input.kek.into()).decrypt_padded_mut::<NoPadding>(&mut encrypted_random_key){
match DesEcbEnc::new(&input.kek.into())
.decrypt_padded_mut::<NoPadding>(&mut encrypted_random_key)
{
Err(x) => panic!("Cannot create main key {:?}", x),
Ok(_) => {}
};

let default_chunk_size = match input.chunk_size{
let default_chunk_size = match input.chunk_size {
0 => 0x00100000,
e => e
e => e,
};

let mut packet_count = 0u32;
Expand All @@ -40,7 +44,7 @@ pub fn new_thread_encryptor(_input: DataEncryptorInput) -> UnboundedReceiver<(Ve
input_data.extend(std::iter::repeat(0).take(padding_remaining));
}
let input_data_length = input_data.len();

let mut offset: usize = 0;
while offset < input_data_length {
if packet_count > 0 {
Expand All @@ -51,12 +55,19 @@ pub fn new_thread_encryptor(_input: DataEncryptorInput) -> UnboundedReceiver<(Ve

current_chunk_size = std::cmp::min(current_chunk_size, input_data_length - offset);

let this_data_chunk = &mut input_data[offset..offset+current_chunk_size];
DesCbcEnc::new(&random_key.into(), &iv.into()).encrypt_padded_mut::<NoPadding>(this_data_chunk, current_chunk_size).unwrap();
let this_data_chunk = &mut input_data[offset..offset + current_chunk_size];
DesCbcEnc::new(&random_key.into(), &iv.into())
.encrypt_padded_mut::<NoPadding>(this_data_chunk, current_chunk_size)
.unwrap();

tx.send((encrypted_random_key.to_vec(), iv.to_vec(), this_data_chunk.to_vec())).unwrap();
tx.send((
encrypted_random_key.to_vec(),
iv.to_vec(),
this_data_chunk.to_vec(),
))
.unwrap();

iv.copy_from_slice(&this_data_chunk[this_data_chunk.len()-8..]);
iv.copy_from_slice(&this_data_chunk[this_data_chunk.len() - 8..]);

packet_count += 1;
offset += current_chunk_size;
Expand Down
Loading

0 comments on commit e426805

Please sign in to comment.