From 84f1ec4daf7846703e2c9d48255e368393e8211b Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Fri, 6 Sep 2024 15:54:37 +0000 Subject: [PATCH] refactor(zip): remove redundant password byte conversion Simplify password handling by removing platform-specific code and utilizing ByteSlice for all conversions. --- src/archive/zip.rs | 5 ++--- src/commands/mod.rs | 11 +++-------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 823d172..39b640e 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -10,7 +10,6 @@ use std::{ thread, }; -use bstr::ByteSlice; use filetime_creation::{set_file_mtime, FileTime}; use fs_err as fs; use same_file::Handle; @@ -45,7 +44,7 @@ where for idx in 0..archive.len() { let mut file = match password { Some(password) => archive - .by_index_decrypt(idx, password.as_bytes())? + .by_index_decrypt(idx, password)? .map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file"))?, None => archive.by_index(idx)?, }; @@ -127,7 +126,7 @@ where let file_in_archive = (|| { let zip_result = match password.clone() { Some(password) => archive - .by_index_decrypt(idx, password.as_bytes())? + .by_index_decrypt(idx, &password)? .map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file")), None => archive.by_index(idx), }; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7d2ab71..202ff1b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -7,12 +7,7 @@ mod list; use std::{ops::ControlFlow, path::PathBuf}; use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; use utils::colors; - -// OsStrExt for password as_bytes() conversion -#[cfg(unix)] -use std::os::unix::prelude::OsStrExt; -#[cfg(windows)] -use std::os::windows::prelude::OsStrExt; +use bstr::ByteSlice; use crate::{ check, @@ -193,7 +188,7 @@ pub fn run( output_file_path, question_policy, args.quiet, - args.password.as_deref().map(|str| str.as_bytes()), + args.password.as_deref().map(|str| <[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed")), ) }) } @@ -232,7 +227,7 @@ pub fn run( formats, list_options, question_policy, - args.password.as_deref().map(|str|str.as_bytes()), + args.password.as_deref().map(|str| <[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed")), )?; }