[xmly] chore: rename streams

This commit is contained in:
鲁树人 2024-09-18 23:00:51 +01:00
parent 4ca1bfe2c8
commit 6a60dec89b

View File

@ -44,9 +44,8 @@ impl ArgsXimalaya {
_ => bail!("ext not found"),
},
};
let mut read_stream = BufReader::with_capacity(cli.buffer_size, File::open(&self.input)?);
let mut write_stream =
BufWriter::with_capacity(cli.buffer_size, File::create(&self.output)?);
let mut reader = BufReader::with_capacity(cli.buffer_size, File::open(&self.input)?);
let mut writer = BufWriter::with_capacity(cli.buffer_size, File::create(&self.output)?);
match file_type {
XimalayaType::X2M | XimalayaType::X3M => {
@ -56,36 +55,36 @@ impl ArgsXimalaya {
_ => bail!("this should not happen"),
};
let mut header = [0u8; 0x400];
read_stream.read_exact(&mut header)?;
reader.read_exact(&mut header)?;
umc_xmly::android::decrypt_android(android_type, &mut header);
write_stream.write_all(&header)?;
io::copy(&mut read_stream, &mut write_stream)?;
writer.write_all(&header)?;
io::copy(&mut reader, &mut writer)?;
}
XimalayaType::XM => {
let mut header = vec![0u8; 1024];
read_stream.read_exact(&mut header)?;
reader.read_exact(&mut header)?;
let xm_file = match umc_xmly::pc::Header::from_buffer(&header) {
Ok(hdr) => hdr,
Err(umc_xmly::XmlyError::MetadataTooSmall(n)) => {
let old_size = header.len();
header.resize(n, 0);
read_stream.read_exact(&mut header[old_size..])?;
reader.read_exact(&mut header[old_size..])?;
umc_xmly::pc::Header::from_buffer(&header)?
}
Err(err) => bail!("failed to parse file: {err}"),
};
// Copy header
write_stream.write_all(xm_file.copy_m4a_header().as_slice())?;
writer.write_all(xm_file.copy_m4a_header().as_slice())?;
// Process encrypted data
read_stream.seek(SeekFrom::Start(xm_file.data_start_offset as u64))?;
reader.seek(SeekFrom::Start(xm_file.data_start_offset as u64))?;
let mut header = vec![0u8; xm_file.encrypted_header_size];
read_stream.read_exact(&mut header[..])?;
write_stream.write_all(xm_file.decrypt(&mut header[..])?)?;
reader.read_exact(&mut header[..])?;
writer.write_all(xm_file.decrypt(&mut header[..])?)?;
// Copy rest of the file
io::copy(&mut read_stream, &mut write_stream)?;
io::copy(&mut reader, &mut writer)?;
}
}