|
| 1 | +const DISK_HEADER_SIZE: usize = 4096; |
| 2 | +const DEFAULT_DISK_HEADER: [u8; 146] = [ |
| 3 | + // The magic number (`TFS fmt `). |
| 4 | + b'T', b'F', b'S', b' ', b'f', b'm', b't', b' ', |
| 5 | + // The version number. |
| 6 | + 0x00, 0x00, 0x00, 0x00, |
| 7 | + 0xFF, 0xFF, 0xFF, 0xFF, |
| 8 | + // The implementation ID (`official`). |
| 9 | + b'o', b'f', b'f', b'i', b'c', b'i', b'a', b'l', |
| 10 | + !b'o', !b'f', !b'f', !b'i', !b'c', !b'i', !b'a', !b'l', |
| 11 | + // Encryption algorithm. |
| 12 | + 0x00, 0x00, |
| 13 | + 0xFF, 0xFF, |
| 14 | + // Encryption parameters. |
| 15 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 16 | + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 17 | + // State block address (uninitialized). |
| 18 | + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 19 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 20 | + // Consistency flag. |
| 21 | + 0x03, 0xFC, |
| 22 | +]; |
| 23 | + |
| 24 | +enum Error { |
| 25 | + CorruptConsistencyFlag, |
| 26 | + CorruptEncryptionAlgorithm, |
| 27 | + CorruptEncryptionParameters, |
| 28 | + CorruptImplementationId, |
| 29 | + CorruptStateBlockAddress, |
| 30 | + CorruptVersionNumber, |
| 31 | + IncompatibleVersion, |
| 32 | + UnknownCipher, |
| 33 | + UnknownConsistencyFlag, |
| 34 | + UnknownFormat, |
| 35 | + Disk(disk::Error), |
| 36 | +} |
| 37 | + |
| 38 | +impl fmt::Display for Error { |
| 39 | + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), Error> { |
| 40 | + write!(f, ) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +enum MagicNumber { |
| 45 | + PartialCompatibility, |
| 46 | + TotalCompatibility, |
| 47 | +} |
| 48 | + |
| 49 | +enum EncryptionAlgorithm { |
| 50 | + Identity = 0, |
| 51 | + Speck128 = 1, |
| 52 | +} |
| 53 | + |
| 54 | +enum ConsistencyFlag { |
| 55 | + Closed, |
| 56 | + StillActive, |
| 57 | + Inconsistent, |
| 58 | + Uninitialized, |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Default)] |
| 62 | +struct DiskHeader { |
| 63 | + magic_number: MagicNumber, |
| 64 | + version_number: u32, |
| 65 | + implementation_id: u32, |
| 66 | + encryption_algorithm: EncryptionAlgorithm, |
| 67 | + encryption_parameters: [u8; 16], |
| 68 | + state_block_address: ClusterPointer, |
| 69 | + consistency_flag: ConsistencyFlag, |
| 70 | +} |
| 71 | + |
| 72 | +impl DiskHeader { |
| 73 | + fn flush_encryption_algorithm<D: Disk>(disk: &mut D) -> Result<DiskHeader, Error> { |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + /// Load the disk header from some disk. |
| 78 | + /// |
| 79 | + /// This will construct it into memory while performing error checks on the header to ensure |
| 80 | + /// correctness. |
| 81 | + fn load<D: Disk>(disk: &mut D) -> Result<DiskHeader, Error> { |
| 82 | + // Load the disk header into a buffer in memory. |
| 83 | + let mut buf = [0; DISK_HEADER_SIZE]; |
| 84 | + disk.read_all(0, 0, &mut buf)?; |
| 85 | + // Start with some default value, which will be filled out later. |
| 86 | + let mut ret = DiskHeader::default(); |
| 87 | + |
| 88 | + // # Introducer Section |
| 89 | + // |
| 90 | + // This section has the purpose of defining the implementation, version, and type of the |
| 91 | + // disk image. It is rarely changed unless updates or reformatting happens. |
| 92 | + |
| 93 | + // Load the magic number. |
| 94 | + ret.magic_number = match buf[..8] { |
| 95 | + // Total compatibility. |
| 96 | + b"TFS fmt " => MagicNumber::TotalCompatibility, |
| 97 | + // Partial compatibility. |
| 98 | + b"~TFS fmt" => MagicNumber::PartialCompatibility, |
| 99 | + // Unknown format; abort. |
| 100 | + _ => return Err(Error::UnknownFormat), |
| 101 | + }; |
| 102 | + |
| 103 | + // Load the version number. |
| 104 | + ret.version_number = LittleEndian::read(buf[8..12]); |
| 105 | + // Right after the version number, the same number follows, but bitwise negated. Make sure |
| 106 | + // that these numbers match (if it is bitwise negated). The reason for using this form of |
| 107 | + // code rather than just repeating it as-is is that if one overwrites all bytes with a |
| 108 | + // constant value, like zero, it won't be detected. |
| 109 | + if ret.version_number == !LittleEndian::read(buf[12..16]) { |
| 110 | + // Check if the version is compatible. |
| 111 | + if ret.version_number >> 16 > 0 { |
| 112 | + // The version is not compatible; abort. |
| 113 | + return Err(Error::IncompatibleVersion); |
| 114 | + } |
| 115 | + } else { |
| 116 | + // The version number is corrupt; abort. |
| 117 | + return Err(Error::CorruptVersionNumber); |
| 118 | + } |
| 119 | + |
| 120 | + // Load the implementation ID. |
| 121 | + ret.implementation_id = LittleEndian::read(buf[16..24]); |
| 122 | + // Similarly to the version number, a bitwise negated repetition follows. Make sure it |
| 123 | + // matches. |
| 124 | + if ret.implementation_id != !LittleEndian::read(buf[24..32]) { |
| 125 | + // The implementation ID is corrupt; abort. |
| 126 | + return Err(Error::CorruptImplementationId); |
| 127 | + } |
| 128 | + |
| 129 | + // == Encryption Section == |
| 130 | + |
| 131 | + // Load the encryption algorithm choice. |
| 132 | + ret.encryption_algorithm = EncryptionAlgorithm::from(LittleEndian::read(buf[64..66]))?; |
| 133 | + // Repeat the bitwise negation. |
| 134 | + if ret.encryption_algorithm as u16 != !LittleEndian::read(buf[66..68]) { |
| 135 | + // The implementation ID is corrupt; abort. |
| 136 | + return Err(Error::CorruptEncryptionAlgorithm); |
| 137 | + } |
| 138 | + |
| 139 | + // Load the encryption parameters (e.g. salt). |
| 140 | + self.encryption_parameters.copy_from_slice(&buf[68..84]); |
| 141 | + // Repeat the bitwise negation. |
| 142 | + if self.encryption_parameters.iter().eq(buf[84..100].iter().map(|x| !x)) { |
| 143 | + // The encryption parameters are corrupt; abort. |
| 144 | + return Err(Error::CorruptEncryptionParameters); |
| 145 | + } |
| 146 | + |
| 147 | + // == State == |
| 148 | + |
| 149 | + // Load the state block pointer. |
| 150 | + ret.state_block_address = ClusterPointer::new(LittleEndian::read(buf[128..136])); |
| 151 | + // Repeat the bitwise negation. |
| 152 | + if ret.state_block_address as u64 != !LittleEndian::read(buf[136..144]) { |
| 153 | + // The state block address is corrupt; abort. |
| 154 | + return Err(Error::CorruptStateBlockAddress); |
| 155 | + } |
| 156 | + |
| 157 | + // Load the consistency flag. |
| 158 | + self.consistency_flag = ConsistencyFlag::from(buf[144])?; |
| 159 | + // Repeat the bitwise negation. |
| 160 | + if self.consistency_flag as u8 != !buf[145] { |
| 161 | + // The consistency flag is corrupt; abort. |
| 162 | + return Err(Error::CorruptConsistencyFlag); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments