atom_macho/load_command/
source_version.rs

1use crate::io::{Endian, ReadExt as _, WriteExt as _};
2use std::io::{Read, Write};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct SourceVersionCommand {
6    pub cmd: u32,
7    pub cmdsize: u32,
8    pub version: Version,
9}
10
11impl SourceVersionCommand {
12    pub const TYPE: u32 = 0x2a;
13
14    pub const SIZE: u32 = 0x10;
15
16    pub fn read_from_in<R: Read>(read: &mut R, endian: Endian) -> Self {
17        let cmd = read.read_u32_in(endian);
18        assert_eq!(cmd, Self::TYPE);
19
20        let cmdsize = read.read_u32_in(endian);
21        assert_eq!(cmdsize, Self::SIZE);
22
23        let version = Version(read.read_u64_in(endian));
24
25        SourceVersionCommand {
26            cmd,
27            cmdsize,
28            version,
29        }
30    }
31
32    pub fn write_into<W: Write>(&self, write: &mut W) {
33        write.write_u32_native(self.cmd);
34        write.write_u32_native(self.cmdsize);
35        write.write_u64_native(self.version.0);
36    }
37}
38
39/// A.B.C.D.E packed as a24.b10.c10.d10.e10
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct Version(u64);
42
43impl Version {
44    /// 40 ~ 64bit
45    pub fn a(&self) -> u64 {
46        self.0 & 0xFFFF_FF00_0000_0000
47    }
48
49    /// 30 ~ 39bit
50    pub fn b(&self) -> u64 {
51        self.0 & 0x0000_00FF_C000_0000
52    }
53
54    /// 20 ~ 29bit
55    pub fn c(&self) -> u64 {
56        self.0 & 0x0000_0000_3FF0_0000
57    }
58
59    /// 10 ~ 19bit
60    pub fn d(&self) -> u64 {
61        self.0 & 0x0000_0000_000F_FC00
62    }
63
64    /// 0 ~ 9bit
65    pub fn e(&self) -> u64 {
66        self.0 & 0x0000_0000_0000_03FF
67    }
68}