feat: v0.4.4, add radio outputs when compressed

This commit is contained in:
2023-10-19 21:55:19 +08:00
parent aa6c4f325d
commit 94a6cf18b3
5 changed files with 30 additions and 13 deletions

View File

@@ -44,16 +44,16 @@ pub struct CmdDecrypt {
#[arg(long, short = 'R')]
pub remove_file: bool,
/// Skip decrypt file
#[arg(long)]
#[arg(long, short = 'S')]
pub skip_decrypt_file: bool,
/// Direct print to the console, file must less than 10K
#[arg(long)]
#[arg(long, short = 'P')]
pub direct_print: bool,
/// Digest file
#[arg(long)]
#[arg(long, short = 'D')]
pub digest_file: bool,
/// Digest algorithm (sha1, sha256[default], sha384, sha512 ...)
#[arg(long)]
#[arg(long, short = 'A')]
pub digest_algorithm: Option<String>,
}

View File

@@ -200,6 +200,7 @@ fn process_compatible_with_1_0(cmd_encrypt: &CmdEncrypt, mut encrypt_meta: TinyE
fn encrypt_file(file_in: &mut File, file_len: u64, file_out: &mut File,
key: &[u8], nonce: &[u8], compress: bool, compress_level: &Option<u32>) -> XResult<u64> {
let mut total_len = 0_u64;
let mut write_len = 0_u64;
let mut buffer = [0u8; 1024 * 8];
let key = opt_result!(key.try_into(), "Key is not 32 bytes: {}");
let mut gz_encoder = match compress_level {
@@ -216,21 +217,28 @@ fn encrypt_file(file_in: &mut File, file_len: u64, file_out: &mut File,
loop {
let len = opt_result!(file_in.read(&mut buffer), "Read file failed: {}");
if len == 0 {
let last_block = if compress {
let last_block_and_tag = if compress {
let last_compressed_buffer = opt_result!(gz_encoder.finalize(), "Decompress file failed: {}");
let mut encrypted_block = encryptor.update(&last_compressed_buffer);
let (last_block, tag) = encryptor.finalize();
write_len += encrypted_block.len() as u64;
write_len += last_block.len() as u64;
encrypted_block.extend_from_slice(&last_block);
encrypted_block.extend_from_slice(&tag);
encrypted_block
} else {
let (mut last_block, tag) = encryptor.finalize();
write_len += last_block.len() as u64;
last_block.extend_from_slice(&tag);
last_block
};
opt_result!(file_out.write_all(&last_block), "Write file failed: {}");
debugging!("Encrypt finished, total bytes: {}", total_len);
opt_result!(file_out.write_all(&last_block_and_tag), "Write file failed: {}");
progress.finish();
debugging!("Encrypt finished, total bytes: {}", total_len);
if compress {
information!("File is compressed: {} byte(s) -> {} byte(s), ratio: {}%",
total_len, write_len, util::ratio(write_len, total_len));
}
break;
} else {
total_len += len as u64;
@@ -240,6 +248,7 @@ fn encrypt_file(file_in: &mut File, file_len: u64, file_out: &mut File,
} else {
encryptor.update(&buffer[0..len])
};
write_len += encrypted.len() as u64;
opt_result!(file_out.write_all(&encrypted), "Write file failed: {}");
progress.position(total_len);
}

View File

@@ -170,3 +170,10 @@ pub fn read_line(ln: &str) {
let _ = io::stdin().read_line(&mut buff).expect("Read line from stdin");
}
pub fn ratio(numerator: u64, denominator: u64) -> String {
if denominator == 0 {
return "".to_string();
}
let r = (numerator * 10000) / denominator;
format!("{:.2}", r as f64 / 100f64)
}