| 1 |
#![allow(missing_docs)] |
| 2 |
|
| 3 |
use std::borrow::Cow; |
| 4 |
use std::io; |
| 5 |
use std::io::{Read, Write}; |
| 6 |
use std::path::{Component, MAIN_SEPARATOR, Path}; |
| 7 |
|
| 8 |
/// Provides high level API for reading from a stream. |
| 9 |
pub mod stream { |
| 10 |
pub use crate::read::stream::{ZipStreamFileMetadata, ZipStreamReader, ZipStreamVisitor}; |
| 11 |
} |
| 12 |
/// Types for creating ZIP archives. |
| 13 |
pub mod write { |
| 14 |
use crate::result::{ZipError, ZipResult}; |
| 15 |
use crate::write::{FileOptionExtension, FileOptions}; |
| 16 |
/// Unstable methods for [`FileOptions`]. |
| 17 |
pub trait FileOptionsExt { |
| 18 |
/// Write the file with the given password using the deprecated `ZipCrypto` algorithm. |
| 19 |
/// |
| 20 |
/// <div class="warning">This is not recommended for new archives, as `ZipCrypto` is not |
| 21 |
/// secure (it can be cracked given 12 bytes of known plaintext after compression). It is |
| 22 |
/// provided only for backward compatibility with older software that doesn't support |
| 23 |
/// AES-encrypted archives.</div> |
| 24 |
fn with_deprecated_encryption(self, password: &[u8]) -> ZipResult<Self> |
| 25 |
where |
| 26 |
Self: Sized; |
| 27 |
} |
| 28 |
impl<T: FileOptionExtension> FileOptionsExt for FileOptions<'_, T> { |
| 29 |
fn with_deprecated_encryption(self, password: &[u8]) -> ZipResult<FileOptions<'static, T>> { |
| 30 |
if password.is_empty() { |
| 31 |
// Forbid empty passwords to avoid deriving a predictable key from a |
| 32 |
// fixed, public value. Callers must ensure passwords are non-empty. |
| 33 |
// This check isn't in with_deprecated_encryption itself because that would also |
| 34 |
// affect reading. |
| 35 |
Err(ZipError::InvalidPassword) |
| 36 |
} else { |
| 37 |
Ok(self.with_deprecated_encryption(password)) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/// Helper methods for writing unsigned integers in little-endian form. |
| 44 |
pub trait LittleEndianWriteExt: Write { |
| 45 |
fn write_u16_le(&mut self, input: u16) -> io::Result<()> { |
| 46 |
self.write_all(&input.to_le_bytes()) |
| 47 |
} |
| 48 |
|
| 49 |
fn write_u32_le(&mut self, input: u32) -> io::Result<()> { |
| 50 |
self.write_all(&input.to_le_bytes()) |
| 51 |
} |
| 52 |
|
| 53 |
fn write_u64_le(&mut self, input: u64) -> io::Result<()> { |
| 54 |
self.write_all(&input.to_le_bytes()) |
| 55 |
} |
| 56 |
|
| 57 |
fn write_u128_le(&mut self, input: u128) -> io::Result<()> { |
| 58 |
self.write_all(&input.to_le_bytes()) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl<W: Write + ?Sized> LittleEndianWriteExt for W {} |
| 63 |
|
| 64 |
/// Helper methods for reading unsigned integers in little-endian form. |
| 65 |
pub trait LittleEndianReadExt: Read { |
| 66 |
fn read_u16_le(&mut self) -> io::Result<u16> { |
| 67 |
let mut out = [0u8; 2]; |
| 68 |
self.read_exact(&mut out)?; |
| 69 |
Ok(u16::from_le_bytes(out)) |
| 70 |
} |
| 71 |
|
| 72 |
fn read_u32_le(&mut self) -> io::Result<u32> { |
| 73 |
let mut out = [0u8; 4]; |
| 74 |
self.read_exact(&mut out)?; |
| 75 |
Ok(u32::from_le_bytes(out)) |
| 76 |
} |
| 77 |
|
| 78 |
fn read_u64_le(&mut self) -> io::Result<u64> { |
| 79 |
let mut out = [0u8; 8]; |
| 80 |
self.read_exact(&mut out)?; |
| 81 |
Ok(u64::from_le_bytes(out)) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl<R: Read> LittleEndianReadExt for R {} |
| 86 |
|
| 87 |
/// Converts a path to the ZIP format (forward-slash-delimited and normalized). |
| 88 |
pub fn path_to_string<T: AsRef<Path>>(path: T) -> Result<Box<str>, std::io::Error> { |
| 89 |
let mut maybe_original = None; |
| 90 |
if let Some(original) = path.as_ref().to_str() { |
| 91 |
if original.is_empty() || original == "." || original == ".." { |
| 92 |
return Ok(String::new().into_boxed_str()); |
| 93 |
} |
| 94 |
if original.starts_with(MAIN_SEPARATOR) { |
| 95 |
if original.len() == 1 { |
| 96 |
return Ok(MAIN_SEPARATOR.to_string().into_boxed_str()); |
| 97 |
} else if (MAIN_SEPARATOR == '/' || !original[1..].contains(MAIN_SEPARATOR)) |
| 98 |
&& !original.ends_with('.') |
| 99 |
&& !original.contains([MAIN_SEPARATOR, MAIN_SEPARATOR]) |
| 100 |
&& !original.contains([MAIN_SEPARATOR, '.', MAIN_SEPARATOR]) |
| 101 |
&& !original.contains([MAIN_SEPARATOR, '.', '.', MAIN_SEPARATOR]) |
| 102 |
{ |
| 103 |
maybe_original = Some(&original[1..]); |
| 104 |
} |
| 105 |
} else if !original.contains(MAIN_SEPARATOR) { |
| 106 |
return Ok(original.into()); |
| 107 |
} |
| 108 |
} |
| 109 |
let mut recreate = maybe_original.is_none(); |
| 110 |
let mut normalized_components = Vec::new(); |
| 111 |
|
| 112 |
for component in path.as_ref().components() { |
| 113 |
match component { |
| 114 |
Component::Normal(os_str) => { |
| 115 |
if let Some(valid_str) = os_str.to_str() { |
| 116 |
normalized_components.push(Cow::Borrowed(valid_str)); |
| 117 |
} else { |
| 118 |
recreate = true; |
| 119 |
normalized_components.push(os_str.to_string_lossy()); |
| 120 |
} |
| 121 |
} |
| 122 |
Component::ParentDir => { |
| 123 |
recreate = true; |
| 124 |
normalized_components.pop(); |
| 125 |
} |
| 126 |
_ => { |
| 127 |
recreate = true; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
if recreate { |
| 132 |
Ok(normalized_components.join("/").into()) |
| 133 |
} else { |
| 134 |
Ok(maybe_original |
| 135 |
.ok_or_else(|| std::io::Error::other("Original path is empty"))? |
| 136 |
.into()) |
| 137 |
} |
| 138 |
} |