macbinary-rs 0.2.0

Transparent access to MacBinary-encoded files
Documentation
#![feature(seek_stream_len)]
#![doc=include_str!("../README.md")]

use std::{fmt, fs, io, path::Path};

use binrw::BinReaderExt;
use macintosh_utils::FourCC;

mod reader;
pub use reader::Reader;

use crate::structs::Header;

/// On-disk structures
pub mod structs;

pub type Error = binrw::Error;
pub type Fork = macintosh_utils::Fork;

#[derive(Debug, Eq, PartialEq)]
pub enum Version {
    None,
    MacBinaryI,
    MacBinaryII,
    MacBinaryIII,
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Version::None => write!(f, "None"),
            Version::MacBinaryI => write!(f, "MacBinary I"),
            Version::MacBinaryII => write!(f, "MacBinary II"),
            Version::MacBinaryIII => write!(f, "Mac Binary III"),
        }
    }
}

#[derive(Debug, Copy, Clone, Default)]
pub struct Config {
    strat: ResourceForkDetectionStrategy,
}

/// Strategy to detect resource fork on the disk
///
/// Since (most) modern file systems and operating systems don't support resource forks,
/// the resource data is often stored in a separate file.
///
/// There are several conventions. _SheepShaver_ for example places the resource fork in a directory called `.rsrc` next to
/// data fork. While `resource_dasm` choses to append `.rsrc` to the filename to store the resource
/// fork.
///
/// MacOS is still capable of managing proper resource forks and uses special syntax
/// (e.g. `path/to/file.ext/..namedfork/rsrc`) to access its contents.
#[derive(Debug, Copy, Clone, Default)]
pub enum ResourceForkDetectionStrategy {
    #[default]
    /// Try each of the subsequent strategies until a resource fork is found
    All,
    /// Don't try to find any resource forks
    None,
    /// Look for a file with the same name in a directory called `.rsrc`
    HiddenDirectory,
    /// On macos, open the actual resource fork using the `/..namedfork/rsrc` syntax
    NamedFork,
    /// Look for a file with the same name and an additional extension called `.rsrc`
    Suffix,
}

/// Wrapper for transparently reading MacBinary encoded files
#[derive(Debug)]
pub struct MacBinary<R> {
    inner: R,
    _config: Config,
    header: Option<Header>,
}

impl<R> MacBinary<R> {
    /// Return the initial reader
    pub fn into_inner(self) -> R {
        self.inner
    }

    pub fn header(&self) -> Option<&Header> {
        self.header.as_ref()
    }

    pub fn version(&self) -> Version {
        let Some(header) = self.header.as_ref() else {
            return Version::None;
        };

        if header.downloader_min_version == 0x81 {
            return Version::MacBinaryII;
        }

        if header.downloader_min_version == 0x82 {
            return Version::MacBinaryIII;
        }

        Version::MacBinaryI
    }

    /// Original file name
    pub fn name(&self) -> &str {
        self.header
            .as_ref()
            .map(|h| h.name.as_str())
            .unwrap_or_default()
    }

    pub fn creator_code(&self) -> FourCC {
        self.header
            .as_ref()
            .map(|h| h.creator_code)
            .unwrap_or_default()
    }

    pub fn type_code(&self) -> FourCC {
        self.header
            .as_ref()
            .map(|h| h.type_code)
            .unwrap_or_default()
    }
}

impl<R: io::Read + io::Seek> MacBinary<R> {
    pub fn try_new(value: R) -> Result<Self, Error> {
        Self::try_new_with_config(value, Config::default())
    }

    pub fn try_new_with_config(mut value: R, config: Config) -> Result<Self, Error> {
        let initial_position = value.stream_position()?;
        Ok(match value.read_be() {
            Ok(header) => MacBinary {
                _config: config,
                inner: value,
                header: Some(header),
            },
            Err(_) => {
                let _ = value.seek(std::io::SeekFrom::Start(initial_position))?;
                MacBinary {
                    _config: config,
                    inner: value,
                    header: None,
                }
            }
        })
    }

    pub fn open_fork(&mut self, fork: Fork) -> Result<Reader<&mut R>, io::Error> {
        match fork {
            Fork::Resource => {
                if let Some(header) = self.header.as_ref() {
                    let len = header.resource_fork_len as u64;
                    let position = header.resource_fork_location();
                    Ok(Reader::try_new(&mut self.inner, position, position + len)?)
                } else {
                    match self._config.strat {
                        ResourceForkDetectionStrategy::All => todo!(),
                        ResourceForkDetectionStrategy::None => {
                            Ok(Reader::try_new(&mut self.inner, 0, 0)?)
                        }
                        ResourceForkDetectionStrategy::HiddenDirectory => todo!(),
                        ResourceForkDetectionStrategy::NamedFork => todo!(),
                        ResourceForkDetectionStrategy::Suffix => todo!(),
                    }
                }
            }

            Fork::Data => {
                if let Some(header) = self.header.as_ref() {
                    let len = header.data_fork_len as u64;
                    let position = header.data_fork_location();
                    Ok(Reader::try_new(&mut self.inner, position, position + len)?)
                } else {
                    let len = self.inner.stream_len()?;
                    Ok(Reader::try_new(&mut self.inner, 0, len)?)
                }
            }
        }
    }

    pub fn data_fork_len(&mut self) -> Result<u64, io::Error> {
        match self.version() {
            Version::None => self.inner.stream_len(),
            _ => Ok(self.header.as_ref().unwrap().data_fork_len as u64),
        }
    }

    pub fn resource_fork_len(&mut self) -> Result<u64, io::Error> {
        match self.version() {
            // TODO: apply resource fork detection strategy
            Version::None => Ok(0),
            _ => Ok(self.header.as_ref().unwrap().resource_fork_len as u64),
        }
    }

    pub fn data_fork(&mut self) -> Result<Reader<&mut R>, io::Error> {
        self.open_fork(Fork::Data)
    }

    pub fn resource_fork(&mut self) -> Result<Reader<&mut R>, io::Error> {
        self.open_fork(Fork::Resource)
    }

    pub fn into_fork(self, fork: Fork) -> Result<Reader<R>, io::Error> {
        let Self {
            header,
            mut inner,
            _config,
        } = self;

        match fork {
            Fork::Resource => {
                if let Some(header) = header {
                    let len = header.resource_fork_len as u64;
                    let position = header.resource_fork_location();

                    Ok(Reader::try_new(inner, position, position + len)?)
                } else {
                    // TODO: respect config
                    Ok(Reader::try_new(inner, 0, 0)?)
                }
            }
            Fork::Data => {
                if let Some(header) = header.as_ref() {
                    let len = header.data_fork_len as u64;
                    let position = header.data_fork_location();

                    Ok(Reader::try_new(inner, position, position + len)?)
                } else {
                    let len = inner.stream_len()?;
                    Ok(Reader::try_new(inner, 0, len)?)
                }
            }
        }
    }

    pub fn comment(&mut self) -> Result<String, io::Error> {
        if let Some(header) = self.header.as_ref()
            && header.comment_len != 0
        {
            let position = self.inner.stream_position()?;
            self.inner
                .seek(io::SeekFrom::Start(header.file_comment_location()))?;
            let mut data = vec![0u8; header.comment_len as usize];
            self.inner.read_exact(&mut data)?;

            let comment = macintosh_utils::decode_string(data);
            self.inner.seek(io::SeekFrom::Start(position))?;
            return Ok(comment);
        }

        // TODO: Consider looking for .finfo directory to achieve SheepShaver compatibility
        Ok(String::new())
    }

    pub fn into_data_fork(self) -> Result<Reader<R>, io::Error> {
        self.into_fork(Fork::Data)
    }

    pub fn into_resource_fork(self) -> Result<Reader<R>, io::Error> {
        self.into_fork(Fork::Resource)
    }
}

impl MacBinary<fs::File> {
    /// Open file at path
    pub fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
        MacBinary::try_new(fs::File::open(path)?)
    }
}

/// Determine the MacBinary version used by the given file
pub fn probe_file(p: impl AsRef<Path>) -> Result<Version, Error> {
    Ok(MacBinary::open(p)?.version())
}

/// Determine the MacBinary version used by the reader
pub fn probe(r: impl io::Read + io::Seek) -> Result<Version, Error> {
    Ok(MacBinary::try_new(r)?.version())
}

/// Create a [`MacBinary`] by opening the given path
pub fn open_file(p: impl AsRef<Path>) -> Result<MacBinary<fs::File>, Error> {
    MacBinary::open(p)
}

#[cfg(test)]
mod tests {
    use std::{
        fs::{File, exists},
        io::Read,
        path::PathBuf,
    };

    use crate::MacBinary;
    use macintosh_utils::fourcc;

    #[test]
    fn read_macbinary_ii_header() {
        let file = open_fixture("FRED.CPT");
        let header = file.header().unwrap();
        assert_eq!(header.name, "Freddie 1.0.cpt");
        assert_eq!(header.resource_fork_len, 0);
        assert_eq!(header.data_fork_len, 303472);
        assert_eq!(header.magic, fourcc!("\0\0\0\0"));
        assert_eq!(header.uploader_version, 0x81);
        assert_eq!(header.downloader_min_version, 0x81);
    }

    #[test]
    fn read_data_fork() {
        let mut file = open_fixture("jpeg2gif.cpt");
        let header = file.header().unwrap();
        let mut buffer = vec![0u8; header.data_fork_len as usize];
        let mut data_fork = file.data_fork().unwrap();
        assert!(data_fork.read_exact(&mut buffer).is_ok());
    }

    fn open_fixture_raw(name: &'static str) -> File {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("test/")
            .join(name);

        if !exists(&path).unwrap() {
            panic!("Test fixture {name} does not exist!");
        }

        std::fs::File::open(path).unwrap()
    }

    fn open_fixture(name: &'static str) -> MacBinary<File> {
        let file = open_fixture_raw(name);
        MacBinary::try_new(file).unwrap()
    }
}