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
use std::io::Cursor;
use std::str;
use types::Endianness;

use byteorder::{self, ReadBytesExt};

pub fn read_i32(data: &[u8], endianness: Endianness) -> i32{
    let mut cursor = Cursor::new(data);
    match endianness{
        Endianness::LittleEndian => cursor.read_i32::<byteorder::LittleEndian>().unwrap(),
        Endianness::BigEndian => cursor.read_i32::<byteorder::BigEndian>().unwrap(),
    }
}

pub fn read_u16(data: &[u8], endianness: Endianness) -> u16{
    let mut cursor = Cursor::new(data);
    match endianness{
        Endianness::LittleEndian => cursor.read_u16::<byteorder::LittleEndian>().unwrap(),
        Endianness::BigEndian => cursor.read_u16::<byteorder::BigEndian>().unwrap(),
    }
}

pub fn read_u64(data: &[u8], endianness: Endianness) -> u64{
    let mut cursor = Cursor::new(data);
    match endianness{
        Endianness::LittleEndian => cursor.read_u64::<byteorder::LittleEndian>().unwrap(),
        Endianness::BigEndian => cursor.read_u64::<byteorder::BigEndian>().unwrap(),
    }
}

pub fn read_u32(data: &[u8], endianness: Endianness) -> u32{
    let mut cursor = Cursor::new(data);
    match endianness{
        Endianness::LittleEndian => cursor.read_u32::<byteorder::LittleEndian>().unwrap(),
        Endianness::BigEndian => cursor.read_u32::<byteorder::BigEndian>().unwrap(),
    }
}

pub fn read_f32(data: &[u8], endianness: Endianness) -> f32{
    let mut cursor = Cursor::new(data);
    match endianness{
        Endianness::LittleEndian => cursor.read_f32::<byteorder::LittleEndian>().unwrap(),
        Endianness::BigEndian => cursor.read_f32::<byteorder::BigEndian>().unwrap(),
    }
}

pub fn read_f64(data: &[u8], endianness: Endianness) -> f64{
    let mut cursor = Cursor::new(data);
    match endianness{
        Endianness::LittleEndian => cursor.read_f64::<byteorder::LittleEndian>().unwrap(),
        Endianness::BigEndian => cursor.read_f64::<byteorder::BigEndian>().unwrap(),
    }
}

pub fn next_multiple(myint: usize, multiple: usize) -> usize{
    let before = multiple - 1;
    (myint + before) & !before
}

pub struct ByteStringIter<'a>{
    data: &'a [u8],
    pos: usize,
    num: usize,
    total: usize,
}

impl<'a> Iterator for ByteStringIter<'a>{
    type Item = (&'a str, usize);
    fn next(&mut self) -> Option<(&'a str, usize)>{
        if self.num == self.total{
            return None
        }
        let prev = self.pos;
        while self.data[self.pos]!=0{
            self.pos += 1;
        }
        let next = self.pos;
        self.num += 1;
        self.pos += 1;
        Some((str::from_utf8(&self.data[prev..next]).unwrap(), self.pos))
    }
}

impl<'a> ByteStringIter<'a>{
    pub fn new(data: &'a[u8], num_elements: usize) -> ByteStringIter<'a>{
        ByteStringIter{ data: data,
            pos: 0,
            num: 0,
            total: num_elements
        }
    }
}