mirror of
https://git.um-react.app/um/lib_um_crypto_rust.git
synced 2026-03-08 04:29:54 +00:00
[kgm] feat #2: basic kgm support
This commit is contained in:
44
um_cli/src/cmd/kgm.rs
Normal file
44
um_cli/src/cmd/kgm.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use crate::Cli;
|
||||
use clap::Args;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
use std::path::PathBuf;
|
||||
use umc_kgm::header::Header;
|
||||
|
||||
/// Decrypt a KGM/VPR file (Kugou Music)
|
||||
#[derive(Args)]
|
||||
pub struct ArgsKGM {
|
||||
/// Path to output file, e.g. /export/Music/song.flac
|
||||
#[arg(short, long)]
|
||||
output: PathBuf,
|
||||
|
||||
/// Path to input file, e.g. /export/Music/song.kgm
|
||||
#[arg(name = "input")]
|
||||
input: PathBuf,
|
||||
}
|
||||
|
||||
impl ArgsKGM {
|
||||
pub fn run(&self, cli: &Cli) -> anyhow::Result<i32> {
|
||||
let mut file_input = File::open(&self.input)?;
|
||||
let mut header = [0u8; 0x40];
|
||||
file_input.read_exact(&mut header)?;
|
||||
let kgm_header = Header::from_buffer(&mut header)?;
|
||||
let decipher = kgm_header.make_decipher()?;
|
||||
file_input.seek(SeekFrom::Start(kgm_header.offset_to_data as u64))?;
|
||||
|
||||
let mut offset = 0usize;
|
||||
let mut buffer = vec![0u8; cli.buffer_size].into_boxed_slice();
|
||||
let mut file_output = File::create(&self.output)?;
|
||||
while let Ok(n) = file_input.read(&mut buffer) {
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
decipher.decrypt(&mut buffer[..n], offset);
|
||||
file_output.write_all(&buffer[..n])?;
|
||||
offset += n;
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use clap::Subcommand;
|
||||
|
||||
pub mod kgm;
|
||||
pub mod ncm;
|
||||
pub mod qmc1;
|
||||
pub mod qmc2;
|
||||
@@ -12,4 +13,6 @@ pub enum Commands {
|
||||
QMCv2(qmc2::ArgsQMCv2),
|
||||
#[command(name = "ncm")]
|
||||
NCM(ncm::ArgsNCM),
|
||||
#[command(name = "kgm")]
|
||||
KGM(kgm::ArgsKGM),
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ pub struct ArgsQMCv2 {
|
||||
info_only: bool,
|
||||
}
|
||||
|
||||
fn read_ekey(ekey: &str) -> Result<Box<[u8]>> {
|
||||
fn read_ekey(ekey: &str) -> Result<Vec<u8>> {
|
||||
let mut external_file = false;
|
||||
let mut decrypt_ekey = true;
|
||||
|
||||
@@ -54,7 +54,7 @@ fn read_ekey(ekey: &str) -> Result<Box<[u8]>> {
|
||||
let ekey = ekey.trim();
|
||||
let ekey = match decrypt_ekey {
|
||||
true => umc_qmc::ekey::decrypt(ekey)?,
|
||||
false => base64::decode(ekey)?.into_boxed_slice(),
|
||||
false => base64::decode(ekey)?,
|
||||
};
|
||||
Ok(ekey)
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ fn run_command(cli: &Cli) -> Result<i32> {
|
||||
Some(Commands::QMCv1(cmd)) => cmd.run(&cli),
|
||||
Some(Commands::QMCv2(cmd)) => cmd.run(&cli),
|
||||
Some(Commands::NCM(cmd)) => cmd.run(&cli),
|
||||
Some(Commands::KGM(cmd)) => cmd.run(&cli),
|
||||
None => {
|
||||
// https://github.com/clap-rs/clap/issues/3857#issuecomment-1161796261
|
||||
todo!("implement a sensible default command, similar to um/cli");
|
||||
|
||||
Reference in New Issue
Block a user