Refactor: Extract ZipFileData::enclosed_name
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
08c2e76705
commit
a614d1f226
3 changed files with 21 additions and 18 deletions
|
@ -8,7 +8,7 @@ keywords = ["zip", "archive"]
|
||||||
description = """
|
description = """
|
||||||
Library to support the reading and writing of zip files.
|
Library to support the reading and writing of zip files.
|
||||||
"""
|
"""
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aes = { version = "0.7.5", optional = true }
|
aes = { version = "0.7.5", optional = true }
|
||||||
|
|
17
src/read.rs
17
src/read.rs
|
@ -13,7 +13,7 @@ use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::{self, prelude::*};
|
use std::io::{self, prelude::*};
|
||||||
use std::path::{Component, Path};
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
|
@ -906,20 +906,7 @@ impl<'a> ZipFile<'a> {
|
||||||
/// to path-based exploits. It is recommended over
|
/// to path-based exploits. It is recommended over
|
||||||
/// [`ZipFile::mangled_name`].
|
/// [`ZipFile::mangled_name`].
|
||||||
pub fn enclosed_name(&self) -> Option<&Path> {
|
pub fn enclosed_name(&self) -> Option<&Path> {
|
||||||
if self.data.file_name.contains('\0') {
|
self.data.enclosed_name()
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let path = Path::new(&self.data.file_name);
|
|
||||||
let mut depth = 0usize;
|
|
||||||
for component in path.components() {
|
|
||||||
match component {
|
|
||||||
Component::Prefix(_) | Component::RootDir => return None,
|
|
||||||
Component::ParentDir => depth = depth.checked_sub(1)?,
|
|
||||||
Component::Normal(_) => depth += 1,
|
|
||||||
Component::CurDir => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the comment of the file
|
/// Get the comment of the file
|
||||||
|
|
20
src/types.rs
20
src/types.rs
|
@ -1,6 +1,5 @@
|
||||||
//! Types that specify what is contained in a ZIP.
|
//! Types that specify what is contained in a ZIP.
|
||||||
#[cfg(feature = "time")]
|
use std::path;
|
||||||
use std::convert::{TryFrom, TryInto};
|
|
||||||
#[cfg(not(any(
|
#[cfg(not(any(
|
||||||
all(target_arch = "arm", target_pointer_width = "32"),
|
all(target_arch = "arm", target_pointer_width = "32"),
|
||||||
target_arch = "mips",
|
target_arch = "mips",
|
||||||
|
@ -375,6 +374,23 @@ impl ZipFileData {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn enclosed_name(&self) -> Option<&path::Path> {
|
||||||
|
if self.file_name.contains('\0') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let path = path::Path::new(&self.file_name);
|
||||||
|
let mut depth = 0usize;
|
||||||
|
for component in path.components() {
|
||||||
|
match component {
|
||||||
|
path::Component::Prefix(_) | path::Component::RootDir => return None,
|
||||||
|
path::Component::ParentDir => depth = depth.checked_sub(1)?,
|
||||||
|
path::Component::Normal(_) => depth += 1,
|
||||||
|
path::Component::CurDir => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(path)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn zip64_extension(&self) -> bool {
|
pub fn zip64_extension(&self) -> bool {
|
||||||
self.uncompressed_size > 0xFFFFFFFF
|
self.uncompressed_size > 0xFFFFFFFF
|
||||||
|| self.compressed_size > 0xFFFFFFFF
|
|| self.compressed_size > 0xFFFFFFFF
|
||||||
|
|
Loading…
Add table
Reference in a new issue