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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::{error::Error};
pub struct KOFileReader {
current_index: usize,
contents: Vec<u8>
}
impl KOFileReader {
pub fn new(raw_contents: Vec<u8>) -> Result<KOFileReader, Box<dyn Error>> {
Ok(KOFileReader {
current_index: 0,
contents: raw_contents
})
}
pub fn get_current_index(&self) -> usize {
self.current_index
}
pub fn eof(&self) -> bool {
self.current_index >= (self.contents.len() - 1)
}
pub fn pop(&mut self, bytes: usize) -> Result<(), Box<dyn Error>> {
self.current_index += bytes;
if self.current_index <= self.contents.len() {
Ok(())
} else {
Err("Unexpected EOF reached".into())
}
}
pub fn next(&mut self) -> Result<u8, Box<dyn Error>> {
self.current_index += 1;
match self.contents.get(self.current_index - 1) {
Some(byte) => Ok(*byte),
None => Err("Unexpected EOF reached".into()),
}
}
pub fn next_count(&mut self, count: usize) -> Result<Vec<u8>, Box<dyn Error>> {
let mut read_bytes: Vec<u8> = Vec::with_capacity(count);
for _ in 0..count {
read_bytes.push(self.next()?);
}
Ok(read_bytes)
}
pub fn peek(&self) -> Result<u8, Box<dyn Error>> {
match self.contents.get(self.current_index) {
Some(byte) => Ok(*byte),
None => Err("Unexpected EOF reached".into()),
}
}
pub fn peek_count(&mut self, count: usize) -> Result<Vec<u8>, Box<dyn Error>> {
let original_index = self.current_index;
let mut peeked: Vec<u8> = Vec::with_capacity(count);
for _ in 0..count {
peeked.push(self.next()?);
}
self.current_index = original_index;
Ok(peeked)
}
pub fn read_bytes_into_u32(&mut self, bytes: u8) -> Result<u32, Box<dyn Error>> {
Ok(match bytes {
0 => panic!("One should never try to read 0 bytes."),
1 => self.next()? as u32,
2 => self.read_int16()? as u32,
3 => (self.read_int16()? as u32) + (self.next()? as u32) * 0x1_00_00u32,
4 => self.read_int32()? as u32,
_ => {
return Err(
"Currently reading more than 4 bytes at a time into an address is unsupported"
.into(),
)
}
})
}
pub fn read_boolean(&mut self) -> Result<bool, Box<dyn Error>> {
Ok(self.next()? != 0u8)
}
pub fn read_byte(&mut self) -> Result<i8, Box<dyn Error>> {
Ok(self.next()? as i8)
}
pub fn read_int16(&mut self) -> Result<i16, Box<dyn Error>> {
let mut arr: [u8; 2] = [0u8; 2];
for i in 0..2 {
arr[i] = self.next()?;
}
Ok(i16::from_le_bytes(arr))
}
pub fn read_int32(&mut self) -> Result<i32, Box<dyn Error>> {
let mut arr: [u8; 4] = [0u8; 4];
for i in 0..4 {
arr[i] = self.next()?;
}
Ok(i32::from_le_bytes(arr))
}
pub fn read_uint16(&mut self) -> Result<u16, Box<dyn Error>> {
let mut arr: [u8; 2] = [0u8; 2];
for i in 0..2 {
arr[i] = self.next()?;
}
Ok(u16::from_le_bytes(arr))
}
pub fn read_uint32(&mut self) -> Result<u32, Box<dyn Error>> {
let mut arr: [u8; 4] = [0u8; 4];
for i in 0..4 {
arr[i] = self.next()?;
}
Ok(u32::from_le_bytes(arr))
}
pub fn read_float(&mut self) -> Result<f32, Box<dyn Error>> {
let mut arr: [u8; 4] = [0u8; 4];
for i in 0..4 {
arr[i] = self.next()?;
}
Ok(f32::from_le_bytes(arr))
}
pub fn read_double(&mut self) -> Result<f64, Box<dyn Error>> {
let mut arr: [u8; 8] = [0u8; 8];
for i in 0..8 {
arr[i] = self.next()?;
}
Ok(f64::from_le_bytes(arr))
}
pub fn read_kos_string(&mut self) -> Result<String, Box<dyn Error>> {
let len = self.next()? as usize;
let mut internal = String::with_capacity(len);
for _ in 0..len {
internal.push(self.next()? as char);
}
Ok(internal)
}
pub fn read_string(&mut self) -> Result<String, Box<dyn Error>> {
let mut internal = String::new();
loop {
let c = self.next()? as char;
if c == '\0' {
break;
}
internal.push(c);
}
Ok(internal)
}
}