forked from zip-rs/zip-old
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Decode Zip-Info UTF8 name and comment fields (#159)
- Loading branch information
1 parent
c74a811
commit e3c8102
Showing
6 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use core::mem::size_of; | ||
use std::io::Read; | ||
use crate::result::{ZipError, ZipResult}; | ||
use crate::unstable::LittleEndianReadExt; | ||
|
||
/// Info-ZIP Unicode Path Extra Field (0x7075) or Unicode Comment Extra Field (0x6375), as | ||
/// specified in APPNOTE 4.6.8 and 4.6.9 | ||
#[derive(Clone, Debug)] | ||
pub struct UnicodeExtraField { | ||
crc32: u32, | ||
content: Box<[u8]> | ||
} | ||
|
||
impl<'a> UnicodeExtraField { | ||
/// Verifies the checksum and returns the content. | ||
pub fn unwrap_valid(self, ascii_field: &[u8]) -> ZipResult<Box<[u8]>> { | ||
let mut crc32 = crc32fast::Hasher::new(); | ||
crc32.update(ascii_field); | ||
let actual_crc32 = crc32.finalize(); | ||
if self.crc32 != actual_crc32 { | ||
return Err(ZipError::InvalidArchive("CRC32 checksum failed on Unicode extra field")); | ||
} | ||
Ok(self.content) | ||
} | ||
} | ||
|
||
impl UnicodeExtraField { | ||
pub(crate) fn try_from_reader<R: Read>(reader: &mut R, len: u16) -> ZipResult<Self> { | ||
// Read and discard version byte | ||
reader.read_exact(&mut [0u8])?; | ||
|
||
let crc32 = reader.read_u32_le()?; | ||
let mut content = vec![0u8; len as usize - size_of::<u8>() - size_of::<u32>()].into_boxed_slice(); | ||
reader.read_exact(&mut content)?; | ||
Ok(Self { | ||
crc32, | ||
content | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters