summaryrefslogtreecommitdiff
path: root/vendor/gimli/src/endianity.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/gimli/src/endianity.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/gimli/src/endianity.rs')
-rw-r--r--vendor/gimli/src/endianity.rs256
1 files changed, 256 insertions, 0 deletions
diff --git a/vendor/gimli/src/endianity.rs b/vendor/gimli/src/endianity.rs
new file mode 100644
index 0000000..3201551
--- /dev/null
+++ b/vendor/gimli/src/endianity.rs
@@ -0,0 +1,256 @@
+//! Types for compile-time and run-time endianity.
+
+use core::convert::TryInto;
+use core::fmt::Debug;
+
+/// A trait describing the endianity of some buffer.
+pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
+ /// Return true for big endian byte order.
+ fn is_big_endian(self) -> bool;
+
+ /// Return true for little endian byte order.
+ #[inline]
+ fn is_little_endian(self) -> bool {
+ !self.is_big_endian()
+ }
+
+ /// Reads an unsigned 16 bit integer from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 2`.
+ #[inline]
+ fn read_u16(self, buf: &[u8]) -> u16 {
+ let bytes: &[u8; 2] = buf[..2].try_into().unwrap();
+ if self.is_big_endian() {
+ u16::from_be_bytes(*bytes)
+ } else {
+ u16::from_le_bytes(*bytes)
+ }
+ }
+
+ /// Reads an unsigned 32 bit integer from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 4`.
+ #[inline]
+ fn read_u32(self, buf: &[u8]) -> u32 {
+ let bytes: &[u8; 4] = buf[..4].try_into().unwrap();
+ if self.is_big_endian() {
+ u32::from_be_bytes(*bytes)
+ } else {
+ u32::from_le_bytes(*bytes)
+ }
+ }
+
+ /// Reads an unsigned 64 bit integer from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 8`.
+ #[inline]
+ fn read_u64(self, buf: &[u8]) -> u64 {
+ let bytes: &[u8; 8] = buf[..8].try_into().unwrap();
+ if self.is_big_endian() {
+ u64::from_be_bytes(*bytes)
+ } else {
+ u64::from_le_bytes(*bytes)
+ }
+ }
+
+ /// Read an unsigned n-bytes integer u64.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 1` or `buf.len() > 8`.
+ #[inline]
+ fn read_uint(&mut self, buf: &[u8]) -> u64 {
+ let mut tmp = [0; 8];
+ if self.is_big_endian() {
+ tmp[8 - buf.len()..].copy_from_slice(buf);
+ } else {
+ tmp[..buf.len()].copy_from_slice(buf);
+ }
+ self.read_u64(&tmp)
+ }
+
+ /// Reads a signed 16 bit integer from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 2`.
+ #[inline]
+ fn read_i16(self, buf: &[u8]) -> i16 {
+ self.read_u16(buf) as i16
+ }
+
+ /// Reads a signed 32 bit integer from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 4`.
+ #[inline]
+ fn read_i32(self, buf: &[u8]) -> i32 {
+ self.read_u32(buf) as i32
+ }
+
+ /// Reads a signed 64 bit integer from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 8`.
+ #[inline]
+ fn read_i64(self, buf: &[u8]) -> i64 {
+ self.read_u64(buf) as i64
+ }
+
+ /// Reads a 32 bit floating point number from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 8`.
+ #[inline]
+ fn read_f32(self, buf: &[u8]) -> f32 {
+ f32::from_bits(self.read_u32(buf))
+ }
+
+ /// Reads a 32 bit floating point number from `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 8`.
+ #[inline]
+ fn read_f64(self, buf: &[u8]) -> f64 {
+ f64::from_bits(self.read_u64(buf))
+ }
+
+ /// Writes an unsigned 16 bit integer `n` to `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 2`.
+ #[inline]
+ fn write_u16(self, buf: &mut [u8], n: u16) {
+ let bytes = if self.is_big_endian() {
+ n.to_be_bytes()
+ } else {
+ n.to_le_bytes()
+ };
+ buf[..2].copy_from_slice(&bytes);
+ }
+
+ /// Writes an unsigned 32 bit integer `n` to `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 4`.
+ #[inline]
+ fn write_u32(self, buf: &mut [u8], n: u32) {
+ let bytes = if self.is_big_endian() {
+ n.to_be_bytes()
+ } else {
+ n.to_le_bytes()
+ };
+ buf[..4].copy_from_slice(&bytes);
+ }
+
+ /// Writes an unsigned 64 bit integer `n` to `buf`.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 8`.
+ #[inline]
+ fn write_u64(self, buf: &mut [u8], n: u64) {
+ let bytes = if self.is_big_endian() {
+ n.to_be_bytes()
+ } else {
+ n.to_le_bytes()
+ };
+ buf[..8].copy_from_slice(&bytes);
+ }
+}
+
+/// Byte order that is selectable at runtime.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub enum RunTimeEndian {
+ /// Little endian byte order.
+ Little,
+ /// Big endian byte order.
+ Big,
+}
+
+impl Default for RunTimeEndian {
+ #[cfg(target_endian = "little")]
+ #[inline]
+ fn default() -> RunTimeEndian {
+ RunTimeEndian::Little
+ }
+
+ #[cfg(target_endian = "big")]
+ #[inline]
+ fn default() -> RunTimeEndian {
+ RunTimeEndian::Big
+ }
+}
+
+impl Endianity for RunTimeEndian {
+ #[inline]
+ fn is_big_endian(self) -> bool {
+ self != RunTimeEndian::Little
+ }
+}
+
+/// Little endian byte order.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub struct LittleEndian;
+
+impl Default for LittleEndian {
+ #[inline]
+ fn default() -> LittleEndian {
+ LittleEndian
+ }
+}
+
+impl Endianity for LittleEndian {
+ #[inline]
+ fn is_big_endian(self) -> bool {
+ false
+ }
+}
+
+/// Big endian byte order.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub struct BigEndian;
+
+impl Default for BigEndian {
+ #[inline]
+ fn default() -> BigEndian {
+ BigEndian
+ }
+}
+
+impl Endianity for BigEndian {
+ #[inline]
+ fn is_big_endian(self) -> bool {
+ true
+ }
+}
+
+/// The native endianity for the target platform.
+#[cfg(target_endian = "little")]
+pub type NativeEndian = LittleEndian;
+
+#[cfg(target_endian = "little")]
+#[allow(non_upper_case_globals)]
+#[doc(hidden)]
+pub const NativeEndian: LittleEndian = LittleEndian;
+
+/// The native endianity for the target platform.
+#[cfg(target_endian = "big")]
+pub type NativeEndian = BigEndian;
+
+#[cfg(target_endian = "big")]
+#[allow(non_upper_case_globals)]
+#[doc(hidden)]
+pub const NativeEndian: BigEndian = BigEndian;