ctr_cart 0.4.0

3DS file header library and utilities.
Documentation
// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR MPL-2.0
// SPDX-FileCopyrightText: 2024 Gabriel Marcano <gabemarcano@yahoo.com>

use crate::card_info::CardInfo;
use crate::card_info::CardInfoRead;
use crate::crypto::AESCtr;
use crate::error::Error;
use crate::ncch::NCCH;
use crate::ncsd::NCSD;
use crate::ncsd::NCSDRead;

use std::io::Read;
use std::io::Seek;

/// Represents the metadata associated with a 3DS cartridge.
pub struct Metadata {
    /// The main [`NCSD`] header for the cartridge.
    pub ncsd: NCSD,
    /// Information about the cartridge.
    pub card_info: CardInfo,
    /// Manages AES CTR block mode decryption to read `ExeFS` info partitions.
    pub exefs_ctr: Option<AESCtr>,
}

impl Metadata {
    /// Returns a new Metadata instance, using the provided Readable and Seekable object as the
    /// underlying data source representing the cart.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Parse`] if the required headers cannot be found or if a field in the
    /// headers contains an unexpected value.
    /// Returns [`Error::Io`] if an IO error took place while reading from the file.
    pub fn try_from<T: Read + Seek>(io: &mut T) -> Result<Self, Error> {
        let ncsd = io.read_ncsd()?;
        let card_info = io.read_card_info()?;
        let exefs_ctr = ncsd.find_exefs_ncch().map(NCCH::exefs_ctr);
        Ok(Self {
            ncsd,
            card_info,
            exefs_ctr,
        })
    }
}