Skip to main content

disk_core/
sgi.rs

1//! 16-bit word order of SGI (MIPS / 68020) disk images, shared by the volume label and EFS.
2
3/// Which way round the 16-bit words of an image sit. Byte-swapped images come from
4/// controllers that wired the data bus the other way round.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum SgiLabelByteOrder {
8    /// Big-endian as the 68020 saw it; no fixup needed.
9    Native,
10    /// Every 16-bit word has its two bytes reversed.
11    Swabbed,
12}
13
14impl SgiLabelByteOrder {
15    /// Plain-ASCII name for logs and the inspect view.
16    pub fn display_name(self) -> &'static str {
17        match self {
18            Self::Native => "native",
19            Self::Swabbed => "byte-swapped",
20        }
21    }
22}
23
24/// Reverse the two bytes of every 16-bit word in `buf`. A trailing odd byte is
25/// left alone, which only matters for callers that read unaligned tails.
26pub fn swab16_in_place(buf: &mut [u8]) {
27    for pair in buf.chunks_exact_mut(2) {
28        pair.swap(0, 1);
29    }
30}
31
32/// Apply `order` to a freshly-read buffer so its 16-bit words read as the
33/// 68020 wrote them.
34pub fn apply_byte_order(order: SgiLabelByteOrder, buf: &mut [u8]) {
35    if order == SgiLabelByteOrder::Swabbed {
36        swab16_in_place(buf);
37    }
38}