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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
///
/// Defines two types of endianness: relative endian (Native or
/// Swapped) and absolute endian (Little or Big).
///
/// Note: In this crate, the term `encast` means decoding a number of
/// bytes to one or more values, the term `decast` means encoding one
/// or more variables to a number of bytes, and the term `endian-flip`
/// means flipping the endianness of value(s).
///
/// # Description
///
/// It defines the following two types of endianness.
///
/// - Relative endian ([`Native`] or  [`Swapped`])
/// - Absolute endian ([`Little`] or  [`Big`])
///
/// Relative endinan is useful when encoding or decoding binaries
/// whose endiannesses are specified by some magic numbers indicating
/// whether native-endian or swapped-endian (e.g., [Mach-o], [PcapFile]).
/// Absolute endian is useful when encoding or decoding binaries whose
/// endiannesses are explicitly specified in some fields or in their
/// specification as little-endian or big-endian (e.g., [ELF], [TCP/IP]).
///
/// For convenience, their short aliases are exported as
/// [`NE`], [`SE`], [`LE`] and [`BE`], respectively.
///
/// The purpose of enum `Endian` is to specify the endianness of the
/// bytes to be `encast`ed or `decast`ed in the arguments of methods.
///
/// # Example
///
/// Enum `Endian` has four methods.  Here are examples of two of them.
///
/// ```
/// use castflip::Endian;
///
/// let abs_endian = Endian::Native.absolute();
/// let abs_name = abs_endian.name();
///
/// if cfg!(target_endian = "little") {
///     assert_eq!(abs_endian, Endian::Little);
///     assert_eq!(abs_name, "Little");
/// } else if cfg!(target_endian = "big") {
///     assert_eq!(abs_endian, Endian::Big);
///     assert_eq!(abs_name, "Big");
/// # } else {
/// #   panic!();
/// }
/// ```
///
/// [`Native`]: #variant.Native
/// [`Swapped`]: #variant.Swapped
/// [`Little`]: #variant.Little
/// [`Big`]: #variant.Big
///
/// [Mach-o]: https://en.wikipedia.org/wiki/Mach-O
/// [PcapFile]: https://wiki.wireshark.org/Development/LibpcapFileFormat
/// [ELF]: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
/// [TCP/IP]: https://en.wikipedia.org/wiki/Internet_protocol_suite
///
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Endian {
    /// Native-Endian.
    /// - The byte-order is the same as the one on the target system.
    /// - Values can be processed without byte-swapping on the target system.
    /// - Its antonym is `Swapped`.
    Native,

    /// Swapped-Endian.
    /// - The byte-order is different from the one on the target system.
    /// - Values needs to be byte-swapped before and after processing them
    ///   on the target system.
    /// - Its antonym is `Native`.
    Swapped,

    /// Little-Endian.
    /// - Values are stored as the least significant byte comes first.
    /// - The need for byte-swapping depends on the endianness of
    ///   the target system.
    /// - Its antonym is `Big`.
    Little,

    /// Big-Endian.
    /// - Values are stored as the most significant byte comes first.
    /// - The need for byte-swapping depends on the endianness of
    ///   the target system.
    /// - Its antonym is `Little`.
    Big,
}


impl Endian {
    /// Returns the corresponding relative endianness.
    ///
    /// ```
    /// use castflip::Endian;
    ///
    /// // Relative endian
    /// assert_eq!(Endian::Native.relative(), Endian::Native);
    /// assert_eq!(Endian::Swapped.relative(), Endian::Swapped);
    ///
    /// // Absolute endian
    /// if cfg!(target_endian = "little") {
    ///     assert_eq!(Endian::Little.relative(), Endian::Native);
    ///     assert_eq!(Endian::Big.relative(), Endian::Swapped);
    /// } else if cfg!(target_endian = "big") {
    ///     assert_eq!(Endian::Little.relative(), Endian::Swapped);
    ///     assert_eq!(Endian::Big.relative(), Endian::Native);
    /// # } else {
    /// #   panic!();
    /// }
    /// ```
    ///
    pub fn relative(&self) -> Endian {
	#[cfg(target_endian = "little")]
	match self {
	    Self::Native  => Self::Native,
	    Self::Swapped => Self::Swapped,
	    Self::Little  => Self::Native,
	    Self::Big     => Self::Swapped,
	}

	#[cfg(target_endian = "big")]
	match self {
	    Self::Native  => Self::Native,
	    Self::Swapped => Self::Swapped,
	    Self::Little  => Self::Swapped,
	    Self::Big     => Self::Native,
	}
    }

    /// Returns the corresponding absolute endianness.
    ///
    /// ```
    /// use castflip::Endian;
    ///
    /// // Relative endian
    /// if cfg!(target_endian = "little") {
    ///     assert_eq!(Endian::Native.absolute(), Endian::Little);
    ///     assert_eq!(Endian::Swapped.absolute(), Endian::Big);
    /// } else if cfg!(target_endian = "big") {
    ///     assert_eq!(Endian::Native.absolute(), Endian::Big);
    ///     assert_eq!(Endian::Swapped.absolute(), Endian::Little);
    /// # } else {
    /// #   panic!();
    /// }
    ///
    /// // Absolute endian
    /// assert_eq!(Endian::Little.absolute(), Endian::Little);
    /// assert_eq!(Endian::Big.absolute(), Endian::Big);
    /// ```
    ///
    pub fn absolute(&self) -> Endian {
	#[cfg(target_endian = "little")]
	match self {
	    Self::Native  => Self::Little,
	    Self::Swapped => Self::Big,
	    Self::Little  => Self::Little,
	    Self::Big     => Self::Big,
	}

	#[cfg(target_endian = "big")]
	match self {
	    Self::Native  => Self::Big,
	    Self::Swapped => Self::Little,
	    Self::Little  => Self::Little,
	    Self::Big     => Self::Big,
	}
    }

    /// Returns `true` if the endianness (self) is different from the
    /// one on the target system.
    ///
    /// ```
    /// use castflip::Endian;
    ///
    /// // Relative endian
    /// assert_eq!(Endian::Native.need_swap(), false);
    /// assert_eq!(Endian::Swapped.need_swap(), true);
    ///
    /// // Absolute endian
    /// if cfg!(target_endian = "little") {
    ///     assert_eq!(Endian::Little.need_swap(), false);
    ///     assert_eq!(Endian::Big.need_swap(), true);
    /// } else if cfg!(target_endian = "big") {
    ///     assert_eq!(Endian::Little.need_swap(), true);
    ///     assert_eq!(Endian::Big.need_swap(), false);
    /// # } else {
    /// #   panic!();
    /// }
    /// ```
    ///
    pub fn need_swap(self) -> bool {
	#[cfg(target_endian = "little")]
	return self == Self::Swapped || self == Self::Big;

	#[cfg(target_endian = "big")]
	return self == Self::Swapped || self == Self::Little;
    }

    /// Returns the name of the endianness (self).
    ///
    /// ```
    /// use castflip::Endian;
    ///
    /// assert_eq!(Endian::Native.name(), "Native");
    /// assert_eq!(Endian::Swapped.name(), "Swapped");
    /// assert_eq!(Endian::Little.name(), "Little");
    /// assert_eq!(Endian::Big.name(), "Big");
    /// ```
    ///
    pub fn name(self) -> &'static str {
	match self {
	    Self::Native	=> "Native",
	    Self::Swapped	=> "Swapped",
	    Self::Little	=> "Little",
	    Self::Big		=> "Big",
	}
    }
}


/// An alias of [`Endian::Native`], which means Native-Endian.
pub const NE: Endian = Endian::Native;

/// An alias of [`Endian::Swapped`], which means Swapped-Endian.
pub const SE: Endian = Endian::Swapped;

/// An alias of [`Endian::Little`], which means Little-Endian.
pub const LE: Endian = Endian::Little;

/// An alias of [`Endian::Big`], which means Big-Endian.
pub const BE: Endian = Endian::Big;