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
use super::*;
use std::convert::*;

pub struct Blob<'a> {
    pub file: usize,
    pub slice: &'a [u8],
}

impl<'a> std::ops::Deref for Blob<'a> {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        self.slice
    }
}

impl<'a> Blob<'a> {
    pub fn new(file: usize, slice: &'a [u8]) -> Self {
        Self { file, slice }
    }
    pub fn peek_usize(&self) -> (usize, usize) {
        if self[0] & 0x80 == 0 {
            (self[0] as usize, 1)
        } else if self[0] & 0xC0 == 0x80 {
            ((((self[0] & 0x3F) as usize) << 8) | self[1] as usize, 2)
        } else {
            ((((self[0] & 0x1F) as usize) << 24) | (self[1] as usize) << 16 | (self[2] as usize) << 8 | self[3] as usize, 4)
        }
    }
    pub fn read_usize(&mut self) -> usize {
        let (value, offset) = self.peek_usize();
        self.offset(offset);
        value
    }
    pub fn read_expected(&mut self, expected: usize) -> bool {
        let (value, offset) = self.peek_usize();
        if value == expected {
            self.offset(offset);
            true
        } else {
            false
        }
    }
    pub fn read_modifiers(&mut self) -> Vec<TypeDefOrRef> {
        let mut mods = vec![];
        loop {
            let (value, offset) = self.peek_usize();
            if value != 32 && value != 31 {
                break;
            } else {
                self.offset(offset);
                mods.push(TypeDefOrRef::decode(self.file, self.read_usize()))
            }
        }
        mods
    }
    pub fn read_str(&mut self) -> &str {
        let len = self.read_usize();
        let value = unsafe { std::str::from_utf8_unchecked(&self.slice[..len]) };
        self.offset(len);
        value
    }
    pub fn read_string(self) -> String {
        let slice = self.slice;
        if slice.as_ptr().align_offset(std::mem::align_of::<u16>()) > 0 {
            let slice = slice.chunks_exact(2).take(slice.len() / 2).map(|chunk| u16::from_le_bytes(chunk.try_into().unwrap())).collect::<Vec<u16>>();
            String::from_utf16_lossy(&slice)
        } else {
            let slice = unsafe { std::slice::from_raw_parts(slice.as_ptr() as *const u16, slice.len() / 2) };
            String::from_utf16_lossy(slice)
        }
    }
    pub fn read_bool(&mut self) -> bool {
        // A bool is specified as "a single byte with value 0 (false) or 1 (true)".
        match self.read_u8() {
            0 => false,
            1 => true,
            _ => panic!("Illegal bool value"),
        }
    }
    pub fn read_i8(&mut self) -> i8 {
        let value = i8::from_le_bytes(self[..1].try_into().unwrap());
        self.offset(1);
        value
    }
    pub fn read_u8(&mut self) -> u8 {
        let value = u8::from_le_bytes(self[..1].try_into().unwrap());
        self.offset(1);
        value
    }
    pub fn read_i16(&mut self) -> i16 {
        let value = i16::from_le_bytes(self[..2].try_into().unwrap());
        self.offset(2);
        value
    }
    pub fn read_u16(&mut self) -> u16 {
        let value = u16::from_le_bytes(self[..2].try_into().unwrap());
        self.offset(2);
        value
    }
    pub fn read_i32(&mut self) -> i32 {
        let value = i32::from_le_bytes(self[..4].try_into().unwrap());
        self.offset(4);
        value
    }
    pub fn read_u32(&mut self) -> u32 {
        let value = u32::from_le_bytes(self[..4].try_into().unwrap());
        self.offset(4);
        value
    }
    pub fn read_i64(&mut self) -> i64 {
        let value = i64::from_le_bytes(self[..8].try_into().unwrap());
        self.offset(8);
        value
    }
    pub fn read_u64(&mut self) -> u64 {
        let value = u64::from_le_bytes(self[..8].try_into().unwrap());
        self.offset(8);
        value
    }
    pub fn read_f32(&mut self) -> f32 {
        let value = f32::from_le_bytes(self[..4].try_into().unwrap());
        self.offset(4);
        value
    }
    pub fn read_f64(&mut self) -> f64 {
        let value = f64::from_le_bytes(self[..8].try_into().unwrap());
        self.offset(8);
        value
    }
    pub fn read_integer(&mut self, ty: Type) -> Integer {
        match ty {
            Type::I8 => Integer::I8(self.read_i8()),
            Type::U8 => Integer::U8(self.read_u8()),
            Type::I16 => Integer::I16(self.read_i16()),
            Type::U16 => Integer::U16(self.read_u16()),
            Type::I32 => Integer::I32(self.read_i32()),
            Type::U32 => Integer::U32(self.read_u32()),
            Type::I64 => Integer::I64(self.read_i64()),
            Type::U64 => Integer::U64(self.read_u64()),
            _ => panic!("Type is not an integer"),
        }
    }
    fn offset(&mut self, offset: usize) {
        self.slice = &self.slice[offset..];
    }
}