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
#[macro_export]
macro_rules! read_u8 {
    ($elf:ident, $io:ident) => ({
        use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
        match $elf.ehdr.data {
            types::ELFDATA2LSB => { $io.read_u8::<LittleEndian>() }
            types::ELFDATA2MSB => { $io.read_u8::<BigEndian>() }
            types::ELFDATANONE => { panic!("Unable to resolve file endianness"); }
            _ => { panic!("Unable to resolve file endianness"); }
        }
    });
}

#[macro_export]
macro_rules! read_u16 {
    ($elf:ident, $io:ident) => ({
        use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
        match $elf.ehdr.data {
            types::ELFDATA2LSB => { $io.read_u16::<LittleEndian>() }
            types::ELFDATA2MSB => { $io.read_u16::<BigEndian>() }
            types::ELFDATANONE => { panic!("Unable to resolve file endianness"); }
            _ => { panic!("Unable to resolve file endianness"); }
        }
    });
}

#[macro_export]
macro_rules! read_u32 {
    ($elf:ident, $io:ident) => ({
        use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
        match $elf.ehdr.data {
            types::ELFDATA2LSB => { $io.read_u32::<LittleEndian>() }
            types::ELFDATA2MSB => { $io.read_u32::<BigEndian>() }
            types::ELFDATANONE => { panic!("Unable to resolve file endianness"); }
            _ => { panic!("Unable to resolve file endianness"); }
        }
    });
}

#[macro_export]
macro_rules! read_u64 {
    ($elf:ident, $io:ident) => ({
        use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
        match $elf.ehdr.data {
            types::ELFDATA2LSB => { $io.read_u64::<LittleEndian>() }
            types::ELFDATA2MSB => { $io.read_u64::<BigEndian>() }
            types::ELFDATANONE => { panic!("Unable to resolve file endianness"); }
            _ => { panic!("Unable to resolve file endianness"); }
        }
    });
}

use std;
pub fn get_string(data: &Vec<u8>, start: usize) -> Result<String, std::string::FromUtf8Error> {
    let mut end: usize = 0;
    for i in start..data.len() {
        if data[i] == 0u8 {
            end = i;
            break;
        }
    }
    let mut rtn = String::with_capacity(end - start);
    for i in start..end {
        rtn.push(data[i] as char);
    }
    Ok(rtn)
}