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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Traits and types for converting between a source endianness and that of the
//! current host.
//!
//! Usually you will want [`Native`] endian if parsing data emitted by the
//! kernel otherwise you will likely want [`Dynamic`] endian if parsing data
//! from a file, although [`Little`] or [`Big`] endian may also work.

/// A trait containing the required conversion functions needed to parse perf
/// records.
///
/// # Safety
/// If [`is_native`](Endian::is_native) returns true when the source endianness
/// does not match the current host endianness then UB will occur.
pub unsafe trait Endian: Clone {
    /// Convert a `u16` from the source endian to the native endian.
    fn convert_u16(&self, bytes: [u8; 2]) -> u16;

    /// Convert a `u32` from the source endian to the native endian.
    fn convert_u32(&self, bytes: [u8; 4]) -> u32;

    /// Convert a `u64` from the source endian to the native endian.
    fn convert_u64(&self, bytes: [u8; 8]) -> u64;

    /// Whether the source endian is the same as the current native endian.
    ///
    /// If this returns `true` then the parser will attempt to avoid copying
    /// some data out of the source buffer, instead reinterpreting it when
    /// possible.
    fn is_native(&self) -> bool {
        false
    }
}

/// Native endian.
///
/// This type performs no endianness conversion.
#[derive(Copy, Clone, Debug, Default)]
pub struct Native;

unsafe impl Endian for Native {
    #[inline]
    fn convert_u16(&self, bytes: [u8; 2]) -> u16 {
        u16::from_ne_bytes(bytes)
    }

    #[inline]
    fn convert_u32(&self, bytes: [u8; 4]) -> u32 {
        u32::from_ne_bytes(bytes)
    }

    #[inline]
    fn convert_u64(&self, bytes: [u8; 8]) -> u64 {
        u64::from_ne_bytes(bytes)
    }

    #[inline]
    fn is_native(&self) -> bool {
        true
    }
}

/// Little endian.
#[derive(Copy, Clone, Debug, Default)]
pub struct Little;

unsafe impl Endian for Little {
    #[inline]
    fn convert_u16(&self, bytes: [u8; 2]) -> u16 {
        u16::from_le_bytes(bytes)
    }

    #[inline]
    fn convert_u32(&self, bytes: [u8; 4]) -> u32 {
        u32::from_le_bytes(bytes)
    }

    #[inline]
    fn convert_u64(&self, bytes: [u8; 8]) -> u64 {
        u64::from_le_bytes(bytes)
    }

    #[inline]
    fn is_native(&self) -> bool {
        let bytes = [b'a', b'b'];

        u16::from_le_bytes(bytes) == u16::from_ne_bytes(bytes)
    }
}

/// Big endian.
#[derive(Copy, Clone, Debug, Default)]
pub struct Big;

unsafe impl Endian for Big {
    #[inline]
    fn convert_u16(&self, bytes: [u8; 2]) -> u16 {
        u16::from_be_bytes(bytes)
    }

    #[inline]
    fn convert_u32(&self, bytes: [u8; 4]) -> u32 {
        u32::from_be_bytes(bytes)
    }

    #[inline]
    fn convert_u64(&self, bytes: [u8; 8]) -> u64 {
        u64::from_be_bytes(bytes)
    }

    #[inline]
    fn is_native(&self) -> bool {
        let bytes = [b'a', b'b'];

        u16::from_be_bytes(bytes) == u16::from_ne_bytes(bytes)
    }
}

/// Either big or little endian, chosen at runtime.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum Dynamic {
    /// Big endian.
    Big,

    /// Little endian.
    Little,
}

unsafe impl Endian for Dynamic {
    fn convert_u16(&self, bytes: [u8; 2]) -> u16 {
        match self {
            Self::Big => Big.convert_u16(bytes),
            Self::Little => Little.convert_u16(bytes),
        }
    }

    fn convert_u32(&self, bytes: [u8; 4]) -> u32 {
        match self {
            Self::Big => Big.convert_u32(bytes),
            Self::Little => Little.convert_u32(bytes),
        }
    }

    fn convert_u64(&self, bytes: [u8; 8]) -> u64 {
        match self {
            Self::Big => Big.convert_u64(bytes),
            Self::Little => Little.convert_u64(bytes),
        }
    }

    fn is_native(&self) -> bool {
        match self {
            Self::Big => Big.is_native(),
            Self::Little => Little.is_native(),
        }
    }
}