Debian Codegraph @

src/base/read/io/entry.rs

raw symbols SCIP
1 // Copyright (c) 2022 Harry [Majored] [[email protected]]
2 // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4 use crate::base::read::io::{compressed::CompressedReader, hashed::HashedReader, owned::OwnedReader};
5 use crate::entry::ZipEntry;
6 use crate::error::{Result, ZipError};
7 use crate::spec::Compression;
8
9 use std::pin::Pin;
10 use std::task::{Context, Poll};
11
12 use futures_lite::io::{AsyncBufRead, AsyncRead, AsyncReadExt, Take};
13 use pin_project::pin_project;
14
15 /// A type which encodes that [`ZipEntryReader`] has associated entry data.
16 pub struct WithEntry<'a>(OwnedEntry<'a>);
17
18 /// A type which encodes that [`ZipEntryReader`] has no associated entry data.
19 pub struct WithoutEntry;
20
21 /// A ZIP entry reader which may implement decompression.
22 #[pin_project]
23 pub struct ZipEntryReader<'a, R, E> {
24 #[pin]
25 reader: HashedReader<CompressedReader<Take<OwnedReader<'a, R>>>>,
26 entry: E,
27 }
28
29 impl<'a, R> ZipEntryReader<'a, R, WithoutEntry>
30 where
31 R: AsyncBufRead + Unpin,
32 {
33 /// Constructs a new entry reader from its required parameters (incl. an owned R).
34 pub(crate) fn new_with_owned(reader: R, compression: Compression, size: u64) -> Self {
35 let reader = HashedReader::new(CompressedReader::new(OwnedReader::Owned(reader).take(size), compression));
36 Self { reader, entry: WithoutEntry }
37 }
38
39 /// Constructs a new entry reader from its required parameters (incl. a mutable borrow of an R).
40 pub(crate) fn new_with_borrow(reader: &'a mut R, compression: Compression, size: u64) -> Self {
41 let reader = HashedReader::new(CompressedReader::new(OwnedReader::Borrow(reader).take(size), compression));
42 Self { reader, entry: WithoutEntry }
43 }
44
45 pub(crate) fn into_with_entry(self, entry: &'a ZipEntry) -> ZipEntryReader<'a, R, WithEntry<'a>> {
46 ZipEntryReader { reader: self.reader, entry: WithEntry(OwnedEntry::Borrow(entry)) }
47 }
48
49 pub(crate) fn into_with_entry_owned(self, entry: ZipEntry) -> ZipEntryReader<'a, R, WithEntry<'a>> {
50 ZipEntryReader { reader: self.reader, entry: WithEntry(OwnedEntry::Owned(entry)) }
51 }
52 }
53
54 impl<'a, R, E> AsyncRead for ZipEntryReader<'a, R, E>
55 where
56 R: AsyncBufRead + Unpin,
57 {
58 fn poll_read(self: Pin<&mut Self>, c: &mut Context<'_>, b: &mut [u8]) -> Poll<std::io::Result<usize>> {
59 self.project().reader.poll_read(c, b)
60 }
61 }
62
63 impl<'a, R, E> ZipEntryReader<'a, R, E>
64 where
65 R: AsyncBufRead + Unpin,
66 {
67 /// Computes and returns the CRC32 hash of bytes read by this reader so far.
68 ///
69 /// This hash should only be computed once EOF has been reached.
70 pub fn compute_hash(&mut self) -> u32 {
71 self.reader.swap_and_compute_hash()
72 }
73
74 /// Consumes this reader and returns the inner value.
75 pub(crate) fn into_inner(self) -> R {
76 self.reader.into_inner().into_inner().into_inner().owned_into_inner()
77 }
78 }
79
80 impl<R> ZipEntryReader<'_, R, WithEntry<'_>>
81 where
82 R: AsyncBufRead + Unpin,
83 {
84 /// Returns an immutable reference to the associated entry data.
85 pub fn entry(&self) -> &'_ ZipEntry {
86 self.entry.0.entry()
87 }
88
89 /// Reads all bytes until EOF has been reached, appending them to buf, and verifies the CRC32 values.
90 ///
91 /// This is a helper function synonymous to [`AsyncReadExt::read_to_end()`].
92 pub async fn read_to_end_checked(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
93 let read = self.read_to_end(buf).await?;
94
95 if self.compute_hash() == self.entry.0.entry().crc32() {
96 Ok(read)
97 } else {
98 Err(ZipError::CRC32CheckError)
99 }
100 }
101
102 /// Reads all bytes until EOF has been reached, placing them into buf, and verifies the CRC32 values.
103 ///
104 /// This is a helper function synonymous to [`AsyncReadExt::read_to_string()`].
105 pub async fn read_to_string_checked(&mut self, buf: &mut String) -> Result<usize> {
106 let read = self.read_to_string(buf).await?;
107
108 if self.compute_hash() == self.entry.0.entry().crc32() {
109 Ok(read)
110 } else {
111 Err(ZipError::CRC32CheckError)
112 }
113 }
114 }
115
116 enum OwnedEntry<'a> {
117 Owned(ZipEntry),
118 Borrow(&'a ZipEntry),
119 }
120
121 impl<'a> OwnedEntry<'a> {
122 pub fn entry(&self) -> &'_ ZipEntry {
123 match self {
124 OwnedEntry::Owned(entry) => entry,
125 OwnedEntry::Borrow(entry) => entry,
126 }
127 }
128 }