use alloc::boxed::Box;
use alloc::vec::Vec;
use crate::format::ar::ArReader;
use crate::format::cpio::CpioReader;
use crate::format::iso9660::IsoReader;
use crate::format::tar::TarReader;
use crate::meta::EntryMeta;
use crate::Result;
use core::fmt;
pub mod ar;
pub mod cpio;
pub mod iso9660;
pub mod tar;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Detection {
Match,
NoMatch,
NeedMore,
}
pub trait ArchiveFormat {
const NAME: &'static str;
fn sniff(prefix: &[u8]) -> Detection;
}
pub trait EntryData {
fn read_chunk(&mut self, out: &mut [u8]) -> Result<usize>;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct SliceData<'a> {
bytes: &'a [u8],
start: usize,
len: usize,
read: usize,
}
impl<'a> SliceData<'a> {
#[must_use]
pub fn new(bytes: &'a [u8], start: usize, len: usize) -> Self {
Self {
bytes,
start,
len,
read: 0,
}
}
}
impl EntryData for SliceData<'_> {
fn read_chunk(&mut self, out: &mut [u8]) -> Result<usize> {
let remaining = self.len - self.read;
if remaining == 0 || out.is_empty() {
return Ok(0);
}
let n = remaining.min(out.len());
let from = self.start + self.read;
out[..n].copy_from_slice(&self.bytes[from..from + n]);
self.read += n;
Ok(n)
}
}
#[derive(Debug, Default)]
pub struct OwnedData {
buf: Vec<u8>,
pos: usize,
}
impl OwnedData {
#[must_use]
pub fn new(buf: Vec<u8>) -> Self {
Self { buf, pos: 0 }
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.buf
}
}
impl EntryData for OwnedData {
fn read_chunk(&mut self, out: &mut [u8]) -> Result<usize> {
let remaining = self.buf.len() - self.pos;
if remaining == 0 || out.is_empty() {
return Ok(0);
}
let n = remaining.min(out.len());
out[..n].copy_from_slice(&self.buf[self.pos..self.pos + n]);
self.pos += n;
Ok(n)
}
}
pub trait EntryReader {
type Data: EntryData;
fn next_entry(&mut self) -> Result<Option<Entry<'_, Self::Data>>>;
}
pub struct Entry<'r, D: EntryData> {
meta: EntryMeta<'r>,
data: &'r mut D,
}
impl<D: EntryData> fmt::Debug for Entry<'_, D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Entry")
.field("meta", &self.meta)
.finish_non_exhaustive()
}
}
impl<'r, D: EntryData> Entry<'r, D> {
pub fn new(meta: EntryMeta<'r>, data: &'r mut D) -> Self {
Self { meta, data }
}
#[must_use]
pub fn meta(&self) -> &EntryMeta<'r> {
&self.meta
}
pub fn data(&mut self) -> &mut D {
self.data
}
}
#[derive(Debug)]
pub enum SourceEvent<'s> {
NeedInput,
Entry(EntryMeta<'s>),
Data(&'s [u8]),
EndEntry,
Done,
}
pub trait EntrySource {
fn feed(&mut self, input: &[u8]) -> Result<usize>;
fn finish_input(&mut self);
fn pull(&mut self) -> Result<SourceEvent<'_>>;
}
pub trait EntryDataSink {
fn write_chunk(&mut self, data: &[u8]) -> Result<()>;
fn close(&mut self) -> Result<()>;
}
pub trait EntryWriter {
type Sink: EntryDataSink + ?Sized;
fn start_entry(&mut self, meta: &EntryMeta<'_>) -> Result<EntrySink<'_, Self::Sink>>;
fn finish(&mut self) -> Result<()>;
}
pub struct EntrySink<'w, S: EntryDataSink + ?Sized> {
inner: &'w mut S,
}
impl<S: EntryDataSink + ?Sized> fmt::Debug for EntrySink<'_, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EntrySink").finish_non_exhaustive()
}
}
impl<'w, S: EntryDataSink + ?Sized> EntrySink<'w, S> {
pub fn new(inner: &'w mut S) -> Self {
Self { inner }
}
pub fn write_chunk(&mut self, data: &[u8]) -> Result<()> {
self.inner.write_chunk(data)
}
pub fn close(&mut self) -> Result<()> {
self.inner.close()
}
}
#[derive(Debug)]
pub enum AnyEntryData<'a> {
Slice(SliceData<'a>),
Owned(OwnedData),
}
impl Default for AnyEntryData<'_> {
fn default() -> Self {
Self::Owned(OwnedData::default())
}
}
impl EntryData for AnyEntryData<'_> {
fn read_chunk(&mut self, out: &mut [u8]) -> Result<usize> {
match self {
Self::Slice(d) => d.read_chunk(out),
Self::Owned(d) => d.read_chunk(out),
}
}
}
#[derive(Debug)]
enum AnyReaderKind<'a> {
Tar(Box<TarReader<'a>>),
Cpio(Box<CpioReader<'a>>),
Ar(Box<ArReader<'a>>),
Iso(Box<IsoReader<'a>>),
}
#[derive(Debug)]
pub struct AnyReader<'a> {
kind: AnyReaderKind<'a>,
slot: AnyEntryData<'a>,
}
impl<'a> AnyReader<'a> {
#[must_use]
pub fn tar(reader: TarReader<'a>) -> Self {
Self {
kind: AnyReaderKind::Tar(Box::new(reader)),
slot: AnyEntryData::default(),
}
}
#[must_use]
pub fn cpio(reader: CpioReader<'a>) -> Self {
Self {
kind: AnyReaderKind::Cpio(Box::new(reader)),
slot: AnyEntryData::default(),
}
}
#[must_use]
pub fn ar(reader: ArReader<'a>) -> Self {
Self {
kind: AnyReaderKind::Ar(Box::new(reader)),
slot: AnyEntryData::default(),
}
}
#[must_use]
pub fn iso(reader: IsoReader<'a>) -> Self {
Self {
kind: AnyReaderKind::Iso(Box::new(reader)),
slot: AnyEntryData::default(),
}
}
}
impl<'a> EntryReader for AnyReader<'a> {
type Data = AnyEntryData<'a>;
fn next_entry(&mut self) -> Result<Option<Entry<'_, AnyEntryData<'a>>>> {
let meta = match &mut self.kind {
AnyReaderKind::Tar(r) => match r.next_entry()? {
Some(mut e) => {
let meta = e.meta().to_static();
self.slot = AnyEntryData::Slice(*e.data());
meta
},
None => return Ok(None),
},
AnyReaderKind::Cpio(r) => match r.next_entry()? {
Some(mut e) => {
let meta = e.meta().to_static();
self.slot = AnyEntryData::Slice(*e.data());
meta
},
None => return Ok(None),
},
AnyReaderKind::Ar(r) => match r.next_entry()? {
Some(mut e) => {
let meta = e.meta().to_static();
self.slot = AnyEntryData::Slice(*e.data());
meta
},
None => return Ok(None),
},
AnyReaderKind::Iso(r) => match r.next_entry()? {
Some(mut e) => {
let meta = e.meta().to_static();
self.slot = AnyEntryData::Slice(*e.data());
meta
},
None => return Ok(None),
},
};
Ok(Some(Entry::new(meta, &mut self.slot)))
}
}