-#[derive(Copy, Clone, Debug)]
-enum Compression {
- Simple,
- ZLib
-}
-
-#[derive(Copy, Clone, Debug)]
-enum Endianness {
- BigEndian,
- LittleEndian
-}
-use Endianness::*;
-
-trait Parse<T, const N: usize> {
- fn parse(self, bytes: [u8; N]) -> T;
-}
-impl Parse<u64, 8> for Endianness {
- fn parse(self, bytes: [u8; 8]) -> u64 {
- match self {
- BigEndian => u64::from_be_bytes(bytes),
- LittleEndian => u64::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<u32, 4> for Endianness {
- fn parse(self, bytes: [u8; 4]) -> u32 {
- match self {
- BigEndian => u32::from_be_bytes(bytes),
- LittleEndian => u32::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<u16, 2> for Endianness {
- fn parse(self, bytes: [u8; 2]) -> u16 {
- match self {
- BigEndian => u16::from_be_bytes(bytes),
- LittleEndian => u16::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<u8, 1> for Endianness {
- fn parse(self, bytes: [u8; 1]) -> u8 {
- match self {
- BigEndian => u8::from_be_bytes(bytes),
- LittleEndian => u8::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<i64, 8> for Endianness {
- fn parse(self, bytes: [u8; 8]) -> i64 {
- match self {
- BigEndian => i64::from_be_bytes(bytes),
- LittleEndian => i64::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<i32, 4> for Endianness {
- fn parse(self, bytes: [u8; 4]) -> i32 {
- match self {
- BigEndian => i32::from_be_bytes(bytes),
- LittleEndian => i32::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<i16, 2> for Endianness {
- fn parse(self, bytes: [u8; 2]) -> i16 {
- match self {
- BigEndian => i16::from_be_bytes(bytes),
- LittleEndian => i16::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<i8, 1> for Endianness {
- fn parse(self, bytes: [u8; 1]) -> i8 {
- match self {
- BigEndian => i8::from_be_bytes(bytes),
- LittleEndian => i8::from_le_bytes(bytes)
- }
- }
-}
-impl Parse<f64, 8> for Endianness {
- fn parse(self, bytes: [u8; 8]) -> f64 {
- match self {
- BigEndian => f64::from_be_bytes(bytes),
- LittleEndian => f64::from_le_bytes(bytes)
- }
- }
-}
-
-fn read_bytes<const N: usize>(r: &mut BufReader<File>) -> Result<[u8; N]> {
- let mut buf = [0; N];
- r.read_exact(&mut buf)?;
- Ok(buf)
-}
-
-fn read_vec(r: &mut BufReader<File>, n: usize) -> Result<Vec<u8>> {
- let mut vec = Vec::with_capacity(n);
- vec.resize(n, 0);
- r.read_exact(&mut vec)?;
- Ok(vec)
-}
-
-trait ReadSwap<T> {
- fn read_swap(&mut self) -> Result<T>;
-}
-
-impl ReadSwap<u32> for Dissector {
- fn read_swap(&mut self) -> Result<u32> {
- Ok(self.endianness.parse(read_bytes(&mut self.r)?))
- }
-}
-impl ReadSwap<u8> for Dissector {
- fn read_swap(&mut self) -> Result<u8> {
- Ok(self.endianness.parse(read_bytes(&mut self.r)?))
- }
-}
-
-impl ReadSwap<i32> for Dissector {
- fn read_swap(&mut self) -> Result<i32> {
- Ok(self.endianness.parse(read_bytes(&mut self.r)?))
- }
-}
-
-impl ReadSwap<f64> for Dissector {
- fn read_swap(&mut self) -> Result<f64> {
- Ok(self.endianness.parse(read_bytes(&mut self.r)?))
- }
-}