-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce tracing, add util functions, add core raw macro, add core l…
…fh logic
- Loading branch information
Showing
5 changed files
with
145 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2024 Harry [Majored] [hello@majored.pw] | ||
// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) | ||
|
||
use crate::core::raw; | ||
use futures_lite::io::AsyncWriteExt; | ||
|
||
pub const SIGNATURE: u32 = 0x4034b50; | ||
|
||
raw! { | ||
RawLocalFileHeader { | ||
version_needed_to_extract, u16, crate::utils::read_u16, crate::utils::write_u16, | ||
general_purpose_flags, u16, crate::utils::read_u16, crate::utils::write_u16, | ||
compression_method, u16, crate::utils::read_u16, crate::utils::write_u16, | ||
last_mod_file_time, u16, crate::utils::read_u16, crate::utils::write_u16, | ||
last_mod_file_date, u16, crate::utils::read_u16, crate::utils::write_u16, | ||
crc_32, u32, crate::utils::read_u32, crate::utils::write_u32, | ||
compressed_size, u32, crate::utils::read_u32, crate::utils::write_u32, | ||
uncompressed_size, u32, crate::utils::read_u32, crate::utils::write_u32, | ||
file_name_length, u16, crate::utils::read_u16, crate::utils::write_u16, | ||
extra_field_length, u16, crate::utils::read_u16, crate::utils::write_u16 | ||
} | ||
} | ||
|
||
pub struct LocalFileHeader { | ||
pub raw: RawLocalFileHeader, | ||
pub file_name: Vec<u8>, | ||
pub extra_field: Vec<u8>, | ||
} | ||
|
||
/// Reads a local file header from the given reader. | ||
/// | ||
/// This function does so by: | ||
/// - asserting the signature of the local file header | ||
/// - reading the raw local file header | ||
/// - reading the file name | ||
/// - reading the extra field | ||
#[tracing::instrument(skip(reader))] | ||
pub async fn read(mut reader: impl AsyncRead + Unpin) -> Result<LocalFileHeader> { | ||
crate::utils::assert_signature(&mut reader, SIGNATURE).await?; | ||
|
||
let raw = raw_read(&mut reader).await?; | ||
let file_name = crate::utils::read_bytes(&mut reader, raw.file_name_length as usize).await?; | ||
let extra_field = crate::utils::read_bytes(&mut reader, raw.extra_field_length as usize).await?; | ||
|
||
Ok(LocalFileHeader { | ||
raw, | ||
file_name, | ||
extra_field, | ||
}) | ||
} | ||
|
||
/// Writes a local file header to the given writer. | ||
/// | ||
/// This function does so by: | ||
/// - writing the signature of the local file header | ||
/// - writing the raw local file header | ||
/// - writing the file name | ||
/// - writing the extra field | ||
pub async fn write(mut writer: impl AsyncWrite + Unpin, header: &LocalFileHeader) -> Result<()> { | ||
crate::utils::write_u32(&mut writer, SIGNATURE).await?; | ||
|
||
raw_write(&mut writer, &header.raw).await?; | ||
writer.write_all(&header.file_name).await?; | ||
writer.write_all(&header.extra_field).await?; | ||
|
||
Ok(()) | ||
} |
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,33 @@ | ||
// Copyright (c) 2024 Harry [Majored] [hello@majored.pw] | ||
// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) | ||
|
||
pub mod lfh; | ||
pub mod cdr; | ||
|
||
macro_rules! raw { | ||
($name:ident { $($field:ident, $type:ty, $read:expr, $write:expr),* }) => { | ||
use crate::error::Result; | ||
use futures_lite::io::{AsyncRead, AsyncWrite}; | ||
|
||
pub struct $name { | ||
$(pub $field : $type),* | ||
} | ||
|
||
/// Reads the raw underlying header from the given reader. | ||
#[tracing::instrument(skip(reader))] | ||
pub async fn raw_read(mut reader: impl AsyncRead + Unpin) -> Result<$name> { | ||
Ok($name { | ||
$($field : $read(&mut reader).await? ),* | ||
}) | ||
} | ||
|
||
/// Writes the raw underlying header to the given writer. | ||
#[tracing::instrument(skip(writer, raw))] | ||
pub async fn raw_write(mut writer: impl AsyncWrite + Unpin, raw: &$name) -> Result<()> { | ||
$($write(&mut writer, raw.$field).await?;)* | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
pub(crate) use raw; |
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 |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
pub mod base; | ||
pub mod error; | ||
pub mod core; | ||
|
||
#[cfg(feature = "tokio")] | ||
pub mod tokio; | ||
|
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