Skip to content

Instantly share code, notes, and snippets.

@ayuusweetfish
Created September 25, 2021 15:50
Show Gist options
  • Save ayuusweetfish/dc0051a0b0198d0eebcd1f3fd2beb1fe to your computer and use it in GitHub Desktop.
Save ayuusweetfish/dc0051a0b0198d0eebcd1f3fd2beb1fe to your computer and use it in GitHub Desktop.
NetEase Music cache uc! → mp3
// rustc neteasemusic.rs -O
use std::io::{Read, Write};
fn main() {
let args = std::env::args().collect::<Vec<_>>();
if args.len() == 1 {
println!("Usage: {} <uc file> [<uc file> ...]", args[0]);
return;
}
for in_path in args.iter().skip(1) {
let out_path = in_path
.trim_end_matches(".uc!").trim_end_matches(".mp3").to_string()
+ ".mp3";
println!("In {}", in_path);
println!("Out {}", out_path);
let mut in_file = match std::fs::OpenOptions::new()
.read(true).open(&in_path) {
Ok(f) => f,
Err(e) => { println!("Err opening input: {}", e); continue; },
};
let mut out_file = match std::fs::OpenOptions::new()
.write(true).create_new(true).open(&out_path) {
Ok(f) => f,
Err(e) => {
if e.kind() == std::io::ErrorKind::AlreadyExists {
print!(" Already exists, replace? (y/N) ");
let mut s = String::new();
let _ = std::io::stdout().flush();
match std::io::stdin().read_line(&mut s) {
Ok(_) => if s.trim().to_ascii_lowercase() != "y" {
println!(" Skipping");
continue;
},
_ => { println!(" Skipping"); continue; },
};
match std::fs::OpenOptions::new()
.write(true).create(true).truncate(true).open(&out_path) {
Ok(f) => f,
Err(e) => { println!("Err opening output: {}", e); continue; },
}
} else {
println!("Err opening output: {}", e);
continue;
}
},
};
const BUF_SIZE: usize = 1024;
let mut buf = [0u8; BUF_SIZE];
loop {
let n = match in_file.read(&mut buf) {
Ok(0) => break,
Ok(n) => n,
Err(e) => { println!("Err reading input: {}", e); break; },
};
for p in buf.iter_mut().take(n) { *p ^= 0xa3; }
if let Err(e) = out_file.write_all(&buf) {
println!("Err writing output: {}", e);
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment