| 1 |
//! Proxy `I/O` tools. |
| 2 |
use core::cmp; |
| 3 |
#[cfg(not(feature = "std"))] |
| 4 |
use core::fmt; |
| 5 |
#[cfg(feature = "std")] |
| 6 |
use std::io; |
| 7 |
|
| 8 |
/// This trait is an implementation vessel that bridges the `std` and `no_std` version |
| 9 |
/// of this library. |
| 10 |
/// |
| 11 |
/// With `std` feature enabled this trait is implemented for all types that implement |
| 12 |
/// [`io::Read`]. |
| 13 |
/// |
| 14 |
/// Without `std` this trait is only implemented for `&[u8]` and can be implemented for |
| 15 |
/// other types as well. |
| 16 |
/// |
| 17 |
/// When using with `std` this trait should be ignored by user code and instead the callers |
| 18 |
/// should rely on the `std` [`io::Read`] trait. |
| 19 |
/// |
| 20 |
/// When using without `std` this trait should be imported and used with [`crate::LhaDecodeReader`] |
| 21 |
/// as an interface for reading decompressed files. |
| 22 |
pub trait Read { |
| 23 |
/// The error type returned by implementations. |
| 24 |
type Error; |
| 25 |
|
| 26 |
/// This method shall produce the "Unexpected EOF" error. |
| 27 |
fn unexpected_eof() -> Self::Error; |
| 28 |
/// Similar to [`io::Read::read`] but continue on [`io::ErrorKind::Interrupted`]. |
| 29 |
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error>; |
| 30 |
/// Exactly like [`io::Read::read_exact`]. |
| 31 |
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { |
| 32 |
if buf.len() != self.read_all(buf)? { |
| 33 |
Err(Self::unexpected_eof()) |
| 34 |
} |
| 35 |
else { |
| 36 |
Ok(()) |
| 37 |
} |
| 38 |
} |
| 39 |
/// Similar to [`io::Read::take`] but return a replacement `Take` struct. |
| 40 |
fn take(self, limit: u64) -> Take<Self> |
| 41 |
where Self: Sized |
| 42 |
{ |
| 43 |
Take { inner: self, limit } |
| 44 |
} |
| 45 |
/// Creates a "by reference" adaptor for this instance of `Read`. |
| 46 |
#[inline] |
| 47 |
fn by_ref(&mut self) -> &mut Self { |
| 48 |
self |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
pub(crate) fn discard_to_end<R: Read, const BUF: usize>(rd: &mut R) -> Result<(), R::Error> { |
| 53 |
use core::mem::{self, MaybeUninit}; |
| 54 |
assert!(BUF != 0); |
| 55 |
// Create an uninitialized array of `MaybeUninit`. The `assume_init` is |
| 56 |
// safe because the type we are claiming to have initialized here is a |
| 57 |
// bunch of `MaybeUninit`s, which do not require initialization. |
| 58 |
let mut data: [MaybeUninit<u8>; BUF] = unsafe { |
| 59 |
MaybeUninit::uninit().assume_init() |
| 60 |
}; |
| 61 |
let buf = { |
| 62 |
// TODO: use BorrowedBuf once it stablizes |
| 63 |
// We never read data and u8 doesn't implement Drop. |
| 64 |
unsafe { mem::transmute::<_, &mut[u8]>(&mut data[..]) } |
| 65 |
}; |
| 66 |
while 0 != rd.read_all(buf)? {} |
| 67 |
Ok(()) |
| 68 |
} |
| 69 |
|
| 70 |
/// A replacement of [`io::Take`] that is used internally by `delharc`. |
| 71 |
#[derive(Debug)] |
| 72 |
pub struct Take<R> { |
| 73 |
limit: u64, |
| 74 |
inner: R, |
| 75 |
} |
| 76 |
|
| 77 |
impl<R> Take<R> { |
| 78 |
#[inline] |
| 79 |
pub fn limit(&self) -> u64 { |
| 80 |
self.limit |
| 81 |
} |
| 82 |
|
| 83 |
pub fn into_inner(self) -> R { |
| 84 |
self.inner |
| 85 |
} |
| 86 |
|
| 87 |
pub fn get_ref(&self) -> &R { |
| 88 |
&self.inner |
| 89 |
} |
| 90 |
|
| 91 |
pub fn get_mut(&mut self) -> &mut R { |
| 92 |
&mut self.inner |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl<R: Read> Read for Take<R> { |
| 97 |
type Error = R::Error; |
| 98 |
|
| 99 |
#[inline] |
| 100 |
fn unexpected_eof() -> Self::Error { |
| 101 |
R::unexpected_eof() |
| 102 |
} |
| 103 |
|
| 104 |
#[inline] |
| 105 |
fn read_all(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 106 |
// Don't call into inner reader at all at EOF because it may still block |
| 107 |
if self.limit == 0 { |
| 108 |
return Ok(0); |
| 109 |
} |
| 110 |
|
| 111 |
let max = cmp::min(buf.len() as u64, self.limit) as usize; |
| 112 |
let n = self.inner.read_all(&mut buf[..max])?; |
| 113 |
self.limit = self.limit.checked_sub(n as u64).expect("number of read bytes exceeds limit"); |
| 114 |
Ok(n) |
| 115 |
} |
| 116 |
|
| 117 |
#[inline] |
| 118 |
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { |
| 119 |
let len = buf.len(); |
| 120 |
if len as u64 > self.limit { |
| 121 |
return Err(Self::unexpected_eof()); |
| 122 |
} |
| 123 |
self.inner.read_exact(buf)?; |
| 124 |
self.limit -= len as u64; |
| 125 |
Ok(()) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg(feature = "std")] |
| 130 |
impl<R: io::Read> Read for R { |
| 131 |
type Error = io::Error; |
| 132 |
|
| 133 |
fn unexpected_eof() -> Self::Error { |
| 134 |
io::Error::new(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer") |
| 135 |
} |
| 136 |
|
| 137 |
fn read_all(&mut self, mut buf: &mut[u8]) -> Result<usize, Self::Error> { |
| 138 |
let orig_len = buf.len(); |
| 139 |
while !buf.is_empty() { |
| 140 |
match self.read(buf) { |
| 141 |
Ok(0) => break, |
| 142 |
Ok(n) => buf = &mut buf[n..], |
| 143 |
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} |
| 144 |
Err(e) => return Err(e) |
| 145 |
} |
| 146 |
} |
| 147 |
Ok(orig_len - buf.len()) |
| 148 |
} |
| 149 |
|
| 150 |
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { |
| 151 |
io::Read::read_exact(self, buf) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/// An error when reading from slice without `std`. |
| 156 |
#[cfg(not(feature = "std"))] |
| 157 |
#[derive(Debug)] |
| 158 |
pub struct UnexpectedEofError; |
| 159 |
|
| 160 |
#[cfg(not(feature = "std"))] |
| 161 |
impl<R: Read + ?Sized> Read for &mut R { |
| 162 |
type Error = R::Error; |
| 163 |
|
| 164 |
fn unexpected_eof() -> Self::Error { |
| 165 |
R::unexpected_eof() |
| 166 |
} |
| 167 |
|
| 168 |
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error> { |
| 169 |
R::read_all(*self, buf) |
| 170 |
} |
| 171 |
|
| 172 |
fn read_exact(&mut self, buf: &mut[u8]) -> Result<(), Self::Error> { |
| 173 |
R::read_exact(*self, buf) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[cfg(not(feature = "std"))] |
| 178 |
impl<R: Read + ?Sized> Read for alloc::boxed::Box<R> { |
| 179 |
type Error = R::Error; |
| 180 |
|
| 181 |
fn unexpected_eof() -> Self::Error { |
| 182 |
R::unexpected_eof() |
| 183 |
} |
| 184 |
|
| 185 |
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error> { |
| 186 |
R::read_all(self, buf) |
| 187 |
} |
| 188 |
|
| 189 |
fn read_exact(&mut self, buf: &mut[u8]) -> Result<(), Self::Error> { |
| 190 |
R::read_exact(self, buf) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
#[cfg(not(feature = "std"))] |
| 195 |
impl fmt::Display for UnexpectedEofError { |
| 196 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 197 |
f.write_str("failed to fill whole buffer") |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
#[cfg(not(feature = "std"))] |
| 202 |
impl Read for &'_[u8] { |
| 203 |
type Error = UnexpectedEofError; |
| 204 |
|
| 205 |
#[inline] |
| 206 |
fn unexpected_eof() -> Self::Error { |
| 207 |
UnexpectedEofError |
| 208 |
} |
| 209 |
|
| 210 |
#[inline] |
| 211 |
fn read_all(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error> { |
| 212 |
let amt = cmp::min(buf.len(), self.len()); |
| 213 |
let (a, b) = self.split_at(amt); |
| 214 |
|
| 215 |
// First check if the amount of bytes we want to read is small: |
| 216 |
// `copy_from_slice` will generally expand to a call to `memcpy`, and |
| 217 |
// for a single byte the overhead is significant. |
| 218 |
if amt == 1 { |
| 219 |
buf[0] = a[0]; |
| 220 |
} else { |
| 221 |
buf[..amt].copy_from_slice(a); |
| 222 |
} |
| 223 |
|
| 224 |
*self = b; |
| 225 |
Ok(amt) |
| 226 |
} |
| 227 |
|
| 228 |
#[inline] |
| 229 |
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { |
| 230 |
if buf.len() > self.len() { |
| 231 |
return Err(UnexpectedEofError); |
| 232 |
} |
| 233 |
self.read_all(buf)?; |
| 234 |
Ok(()) |
| 235 |
} |
| 236 |
} |