Skip to content

Commit

Permalink
Factor out read_string and read_bytes into sister io crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Majored committed Apr 15, 2022
1 parent 57e324d commit d7f13cd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/read/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(crate) async fn read_cd<R: AsyncRead + AsyncSeek + Unpin>(reader: &mut R) ->
}

if eocdh.file_comm_length > 0 {
comment = Some(crate::utils::read_string(&mut reader, eocdh.file_comm_length as usize).await?);
comment = Some(async_io_utilities::read_string(&mut reader, eocdh.file_comm_length as usize).await?);
}

let reader = reader.into_inner();
Expand All @@ -130,9 +130,9 @@ pub(crate) async fn read_cd_entry<R: AsyncRead + Unpin>(reader: &mut R) -> Resul
crate::utils::assert_signature(reader, crate::spec::signature::CENTRAL_DIRECTORY_FILE_HEADER).await?;

let header = CentralDirectoryHeader::from_reader(reader).await?;
let filename = crate::utils::read_string(reader, header.file_name_length.into()).await?;
let extra = crate::utils::read_bytes(reader, header.extra_field_length.into()).await?;
let comment = crate::utils::read_string(reader, header.file_comment_length.into()).await?;
let filename = async_io_utilities::read_string(reader, header.file_name_length.into()).await?;
let extra = async_io_utilities::read_bytes(reader, header.extra_field_length.into()).await?;
let comment = async_io_utilities::read_string(reader, header.file_comment_length.into()).await?;

let entry = ZipEntry {
name: filename,
Expand Down
4 changes: 2 additions & 2 deletions src/read/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ pub(crate) async fn read_lfh<R: AsyncRead + Unpin>(reader: &mut R) -> Result<Opt
};

let header = LocalFileHeader::from_reader(reader).await?;
let filename = crate::utils::read_string(reader, header.file_name_length.into()).await?;
let extra = crate::utils::read_bytes(reader, header.extra_field_length.into()).await?;
let filename = async_io_utilities::read_string(reader, header.file_name_length.into()).await?;
let extra = async_io_utilities::read_bytes(reader, header.extra_field_length.into()).await?;

let entry = ZipEntry {
name: filename,
Expand Down
16 changes: 0 additions & 16 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@
use crate::error::{Result, ZipError};
use tokio::io::{AsyncRead, AsyncReadExt};

// Read and return a dynamic length string from a reader which impls AsyncRead.
pub async fn read_string<R: AsyncRead + Unpin>(reader: &mut R, length: usize) -> Result<String> {
let mut buffer = String::with_capacity(length);
reader.take(length as u64).read_to_string(&mut buffer).await?;

Ok(buffer)
}

// Read and return a dynamic length vector of bytes from a reader which impls AsyncRead.
pub async fn read_bytes<R: AsyncRead + Unpin>(reader: &mut R, length: usize) -> Result<Vec<u8>> {
let mut buffer = Vec::with_capacity(length);
reader.take(length as u64).read_to_end(&mut buffer).await?;

Ok(buffer)
}

// Assert that the next four-byte signature read by a reader which impls AsyncRead matches the expected signature.
pub(crate) async fn assert_signature<R: AsyncRead + Unpin>(reader: &mut R, expected: u32) -> Result<()> {
match reader.read_u32_le().await? {
Expand Down

0 comments on commit d7f13cd

Please sign in to comment.