1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use byteorder::{LittleEndian, ReadBytesExt};

use std::io::{Cursor, Read};

use anyhow::{ensure, Result};

#[derive(Clone, Debug, thiserror::Error)]
#[error("FAT data has invalid size.")]
struct InvalidFatLen;

/// Represents an entry in the File Allocation Table.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct AllocInfo {
    /// The offset to the start of the file relative to the ROM start.
    pub start: u32,
    /// Length of the file.
    pub end: u32,
}

impl AllocInfo {
    pub fn new<R: Read>(reader: &mut R) -> Result<Self> {
        Ok(Self {
            start: reader.read_u32::<LittleEndian>()?,
            end: reader.read_u32::<LittleEndian>()?,
        })
    }

    pub fn len(&self) -> u32 {
        self.end - self.start
    }
}

/// Wrapper for handling File Allocation Table stuff
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FileAllocTable {
    list: Vec<AllocInfo>,
}

impl FileAllocTable {
    /// Takes a raw FAT and reads it into a list.
    /// 
    /// # Errors
    /// Will return an error if the length of the data is not
    /// divisible by 8. This is because each FAT entry is two
    /// 32-bit integers.
    /// 
    /// It will also return an error if reading from the data
    /// fails.
    pub fn new(fat: &[u8]) -> Result<Self> {
        // Each entry is 8 bytes, so if not divisible by 8
        // then there is an issue with the passed data.
        ensure!(fat.len() % 8 == 0, InvalidFatLen);

        let mut list = Vec::new();
        let mut cursor = Cursor::new(fat);
        let entries = fat.len() / 8;

        for _ in 0..entries {
            list.push(AllocInfo::new(&mut cursor)?);
        }

        Ok(Self {
            list
        })
    }

    /// Returns the allocation info for the given file ID.
    /// 
    /// If the given ID is not in the list, it will return `None`.
    pub fn get(&self, id: u16) -> Option<AllocInfo> {
        if self.list.len() >= id as usize {
            return Some(self.list[id as usize]);
        }

        None
    }
}