Struct delharc::decode::LhaDecodeReader

source ·
pub struct LhaDecodeReader<R> { /* private fields */ }
Expand description

LhaDecodeReader provides a convenient way to parse and decode LHA/LZH files.

To read the current archived file’s content use the std::io::Read trait methods on the instance of this type. After reading the whole file (until EOF), the calculated checksum should be verified using LhaDecodeReader::crc_check.

To parse and decode the next archive file, invoke LhaDecodeReader::next_file.

After parsing the LHA header, a decompressed content of a file can be simply read from the LhaDecodeReader<R>, which decompresses it using a proper decoder, designated in the header, while reading data from the underlying stream.

If the compression method is not supported by the decoder, but otherwise the header has been parsed successfully, invoke LhaDecodeReader::is_decoder_supported to ensure you can actually read the file. Otherwise, trying to read from an unsupported decoder will result in an error.

§no_std

Without the std feature in the absence of std::io the crate’s Read trait methods should be used instead to read the content of the decompressed files.

Implementations§

source§

impl<R: Read> LhaDecodeReader<R>
where R::Error: Debug,

source

pub fn new(rd: R) -> Result<LhaDecodeReader<R>, LhaDecodeError<R>>

Return a new instance of LhaDecodeReader<R> after reading and parsing the first header from source.

Provide a stream reader as rd.

§Errors

Return an error if the header could not be read or parsed.

source

pub fn begin_new(&mut self, rd: R) -> Result<bool, LhaDecodeError<R>>

Attempt to read the first file header from a new source stream and initialize a decoder returning Ok(true) on success. Return Ok(false) if there are no more headers in the stream.

Provide a stream reader as rd.

When Ok is returned, regardles of the retuned boolean value, the inner reader is being always replaced with the given rd.

When Ok(false) has been returned, trying to read from the decoder will result in an error.

§Errors

Returns an error if the header could not be read or parsed. In this instance the inner stream reader is not being replaced by a new one and the provided source stream can be retrieved from the returned error.

source

pub fn begin_with_header_and_decoder( &mut self, header: LhaHeader, decoder: DecoderAny<Take<R>> )

Assign externally parsed header and decoder to this instance of LhaDecodeReader<R>.

It is up to the caller to make sure the decoder and the header are matching each other.

The decoder should be initialized with the reader limited by the Take wrapper with its limit set to the LhaHeader::compressed_size number of bytes.

This method assumes the file will be read and decoded from its beginning.

source

pub fn next_file(&mut self) -> Result<bool, LhaDecodeError<R>>

Attempt to parse the next file’s header.

The remaining content of the previous file is being skipped if the current file’s content has not been read entirely.

On success returns Ok(true) if the next header has been read and parsed successfully. If there are no more headers, returns Ok(false).

§Errors

Returns an error if the header could not be read or parsed. In this instance the underlying stream source will be taken and returned with the error.

§Panic

Panics if called when the underlying stream reader has been already taken.

§no_std

To skip the remaining file’s content this function uses 8 KB stack-allocated buffer when using with std feature enabled. Without std the buffer size is 512 bytes. See also LhaDecodeReader::next_file_with_sink.

source

pub fn next_file_with_sink<const BUF: usize>( &mut self ) -> Result<bool, LhaDecodeError<R>>

Attempt to parse the next file’s header.

Exactly like LhaDecodeReader::next_file but allows to specify the sink buffer size as BUF.

§Panics

Panics when BUF = 0.

source

pub fn header(&self) -> &LhaHeader

Return a reference to the last parsed file’s LhaHeader.

source

pub fn into_inner(self) -> R

Unwrap the underlying stream reader and return it.

§Panics

Panics if the reader has been already taken.

source

pub fn take_inner(&mut self) -> Option<R>

Take the inner stream reader value out of the decoder, leaving a none in its place.

After this call, reading from this instance will result in a panic.

source

pub fn len(&self) -> u64

Return the number of remaining bytes of the currently decompressed file to be read.

source

pub fn is_empty(&self) -> bool

Return whether the current file has been finished reading or if the file was empty.

source

pub fn is_present(&self) -> bool

Return whether an underlying stream reader is present in the decoder.

source

pub fn is_absent(&self) -> bool

Return whether an underlying stream reader is absent from the decoder.

An attempt to read file’s content in this state will result in a panic.

source

pub fn crc_is_ok(&self) -> bool

Return whether the computed CRC-16 matches the checksum in the header.

This should be called after the whole file has been read.

source

pub fn crc_check(&self) -> LhaResult<u16, R>

Return CRC-16 checksum if the computed checksum matches the one in the header. Otherwise return an LhaError::Checksum error.

This should be called after the whole file has been read.

source

pub fn is_decoder_supported(&self) -> bool

Return whether the current file’s compression method is supported.

If this method returns false, trying to read from the decoder will result in an error. In this instance it is still ok to skip to the next file.

§Note

If the variant of compression is CompressionMethod::Lhd this method will return false. In this instance check the result from header’s LhaHeader::is_directory to determine what steps should be taken next.

Trait Implementations§

source§

impl<R: Debug> Debug for LhaDecodeReader<R>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: Read> Default for LhaDecodeReader<R>

A default implementation creates an instance of LhaDecodeReader<R> with no reader present and with a phony header.

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<R: Read<Error = Error>> Read for LhaDecodeReader<R>

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Read all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more

Auto Trait Implementations§

§

impl<R> Freeze for LhaDecodeReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for LhaDecodeReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for LhaDecodeReader<R>
where R: Send,

§

impl<R> Sync for LhaDecodeReader<R>
where R: Sync,

§

impl<R> Unpin for LhaDecodeReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for LhaDecodeReader<R>
where R: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<R> Read for R
where R: Read,

§

type Error = Error

The error type returned by implementations.
source§

fn unexpected_eof() -> <R as Read>::Error

This method shall produce the “Unexpected EOF” error.
source§

fn read_all(&mut self, buf: &mut [u8]) -> Result<usize, <R as Read>::Error>

Similar to io::Read::read but continue on io::ErrorKind::Interrupted.
source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), <R as Read>::Error>

Exactly like io::Read::read_exact.
source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Similar to io::Read::take but return a replacement Take struct.
source§

fn by_ref(&mut self) -> &mut Self

Creates a “by reference” adaptor for this instance of Read.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.