]>
Commit | Line | Data |
---|---|---|
90aa4e6b KS |
1 | use std::io::SeekFrom; |
2 | use std::fs::File; | |
3 | use std::io::prelude::*; | |
4 | ||
5 | #[derive(Debug)] | |
6 | pub enum ByteIOError { | |
7 | EOF, | |
8 | WrongRange, | |
9 | WrongIOMode, | |
10 | NotImplemented, | |
11 | ReadError, | |
12 | WriteError, | |
13 | SeekError, | |
14 | } | |
15 | ||
16 | type ByteIOResult<T> = Result<T, ByteIOError>; | |
17 | ||
18 | pub trait ByteIO { | |
19 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>; | |
794364cf | 20 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>; |
90aa4e6b | 21 | fn read_byte(&mut self) -> ByteIOResult<u8>; |
794364cf | 22 | fn peek_byte(&mut self) -> ByteIOResult<u8>; |
90aa4e6b KS |
23 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<usize>; |
24 | fn tell(&mut self) -> u64; | |
25 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64>; | |
26 | fn is_eof(&mut self) -> bool; | |
27 | fn is_seekable(&mut self) -> bool; | |
28 | } | |
29 | ||
30 | #[allow(dead_code)] | |
31 | pub struct ByteReader<'a> { | |
32 | io: &'a mut ByteIO, | |
33 | } | |
34 | ||
35 | pub struct MemoryReader<'a> { | |
36 | buf: &'a [u8], | |
37 | size: usize, | |
38 | pos: usize, | |
90aa4e6b KS |
39 | } |
40 | ||
41 | pub struct FileReader<'a> { | |
42 | file: &'a File, | |
43 | eof: bool, | |
44 | } | |
45 | ||
46 | macro_rules! read_int { | |
47 | ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({ | |
48 | let mut buf = [0; $size]; | |
49 | try!($s.read_buf(&mut buf)); | |
50 | unsafe { | |
51 | Ok((*(buf.as_ptr() as *const $inttype)).$which()) | |
52 | } | |
53 | }) | |
54 | } | |
55 | ||
794364cf KS |
56 | macro_rules! peek_int { |
57 | ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({ | |
58 | let mut buf = [0; $size]; | |
59 | try!($s.peek_buf(&mut buf)); | |
60 | unsafe { | |
61 | Ok((*(buf.as_ptr() as *const $inttype)).$which()) | |
62 | } | |
63 | }) | |
64 | } | |
65 | ||
90aa4e6b | 66 | impl<'a> ByteReader<'a> { |
5a5a3ecb | 67 | pub fn new(io: &'a mut ByteIO) -> Self { ByteReader { io: io } } |
90aa4e6b KS |
68 | |
69 | pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
70 | self.io.read_buf(buf) | |
71 | } | |
72 | ||
794364cf KS |
73 | pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
74 | self.io.peek_buf(buf) | |
75 | } | |
76 | ||
90aa4e6b KS |
77 | pub fn read_byte(&mut self) -> ByteIOResult<u8> { |
78 | self.io.read_byte() | |
79 | } | |
80 | ||
794364cf KS |
81 | pub fn peek_byte(&mut self) -> ByteIOResult<u8> { |
82 | self.io.peek_byte() | |
83 | } | |
84 | ||
90aa4e6b KS |
85 | pub fn read_u16be(&mut self) -> ByteIOResult<u16> { |
86 | read_int!(self, u16, 2, to_be) | |
87 | } | |
88 | ||
794364cf KS |
89 | pub fn peek_u16be(&mut self) -> ByteIOResult<u16> { |
90 | peek_int!(self, u16, 2, to_be) | |
91 | } | |
92 | ||
90aa4e6b | 93 | pub fn read_u24be(&mut self) -> ByteIOResult<u32> { |
794364cf KS |
94 | let p16 = self.read_u16be()?; |
95 | let p8 = self.read_byte()?; | |
96 | Ok(((p16 as u32) << 8) | (p8 as u32)) | |
97 | } | |
98 | ||
99 | pub fn peek_u24be(&mut self) -> ByteIOResult<u32> { | |
100 | let mut src: [u8; 3] = [0; 3]; | |
101 | self.peek_buf(&mut src)?; | |
102 | Ok(((src[0] as u32) << 16) | ((src[1] as u32) << 8) | (src[2] as u32)) | |
90aa4e6b KS |
103 | } |
104 | ||
105 | pub fn read_u32be(&mut self) -> ByteIOResult<u32> { | |
106 | read_int!(self, u32, 4, to_be) | |
107 | } | |
108 | ||
794364cf KS |
109 | pub fn peek_u32be(&mut self) -> ByteIOResult<u32> { |
110 | peek_int!(self, u32, 4, to_be) | |
111 | } | |
112 | ||
90aa4e6b KS |
113 | pub fn read_u64be(&mut self) -> ByteIOResult<u64> { |
114 | read_int!(self, u64, 8, to_be) | |
115 | } | |
116 | ||
794364cf KS |
117 | pub fn peek_u64be(&mut self) -> ByteIOResult<u64> { |
118 | peek_int!(self, u64, 8, to_be) | |
119 | } | |
120 | ||
90aa4e6b KS |
121 | pub fn read_u16le(&mut self) -> ByteIOResult<u16> { |
122 | read_int!(self, u16, 2, to_le) | |
123 | } | |
124 | ||
794364cf KS |
125 | pub fn peek_u16le(&mut self) -> ByteIOResult<u16> { |
126 | peek_int!(self, u16, 2, to_le) | |
127 | } | |
128 | ||
90aa4e6b | 129 | pub fn read_u24le(&mut self) -> ByteIOResult<u32> { |
794364cf KS |
130 | let p8 = self.read_byte()?; |
131 | let p16 = self.read_u16le()?; | |
132 | Ok(((p16 as u32) << 8) | (p8 as u32)) | |
133 | } | |
134 | ||
135 | pub fn peek_u24le(&mut self) -> ByteIOResult<u32> { | |
136 | let mut src: [u8; 3] = [0; 3]; | |
137 | self.peek_buf(&mut src)?; | |
138 | Ok((src[0] as u32) | ((src[1] as u32) << 8) | ((src[2] as u32) << 16)) | |
90aa4e6b KS |
139 | } |
140 | ||
141 | pub fn read_u32le(&mut self) -> ByteIOResult<u32> { | |
142 | read_int!(self, u32, 4, to_le) | |
143 | } | |
144 | ||
794364cf KS |
145 | pub fn peek_u32le(&mut self) -> ByteIOResult<u32> { |
146 | peek_int!(self, u32, 4, to_le) | |
147 | } | |
148 | ||
90aa4e6b KS |
149 | pub fn read_u64le(&mut self) -> ByteIOResult<u64> { |
150 | read_int!(self, u64, 8, to_le) | |
151 | } | |
152 | ||
794364cf KS |
153 | pub fn peek_u64le(&mut self) -> ByteIOResult<u64> { |
154 | peek_int!(self, u64, 8, to_le) | |
155 | } | |
156 | ||
6b167b0c | 157 | pub fn read_skip(&mut self, len: usize) -> ByteIOResult<()> { |
90aa4e6b KS |
158 | if self.io.is_seekable() { |
159 | self.io.seek(SeekFrom::Current(len as i64))?; | |
160 | } else { | |
161 | let mut ssize = len; | |
162 | let mut buf : [u8; 16] = [0; 16]; | |
163 | let mut bref = &mut buf; | |
164 | while ssize > bref.len() { | |
165 | self.io.read_buf(bref)?; | |
166 | ssize -= bref.len(); | |
167 | } | |
168 | while ssize > 0 { | |
169 | self.io.read_byte()?; | |
170 | ssize = ssize - 1; | |
171 | } | |
172 | } | |
6b167b0c | 173 | Ok(()) |
90aa4e6b KS |
174 | } |
175 | ||
176 | pub fn tell(&mut self) -> u64 { | |
177 | self.io.tell() | |
178 | } | |
179 | ||
180 | pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
181 | self.io.seek(pos) | |
182 | } | |
183 | ||
184 | pub fn is_eof(&mut self) -> bool { | |
185 | self.io.is_eof() | |
186 | } | |
187 | } | |
188 | ||
189 | impl<'a> MemoryReader<'a> { | |
190 | ||
191 | pub fn new_read(buf: &'a [u8]) -> Self { | |
794364cf | 192 | MemoryReader { buf: buf, size: buf.len(), pos: 0 } |
90aa4e6b KS |
193 | } |
194 | ||
195 | fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> { | |
196 | if pos < 0 || (pos as usize) > self.size { | |
197 | return Err(ByteIOError::WrongRange) | |
198 | } | |
199 | self.pos = pos as usize; | |
200 | Ok(pos as u64) | |
201 | } | |
90aa4e6b KS |
202 | } |
203 | ||
204 | impl<'a> ByteIO for MemoryReader<'a> { | |
205 | fn read_byte(&mut self) -> ByteIOResult<u8> { | |
90aa4e6b KS |
206 | if self.is_eof() { return Err(ByteIOError::EOF); } |
207 | let res = self.buf[self.pos]; | |
208 | self.pos = self.pos + 1; | |
209 | Ok(res) | |
210 | } | |
211 | ||
794364cf KS |
212 | fn peek_byte(&mut self) -> ByteIOResult<u8> { |
213 | if self.is_eof() { return Err(ByteIOError::EOF); } | |
214 | Ok(self.buf[self.pos]) | |
215 | } | |
216 | ||
217 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
90aa4e6b KS |
218 | let copy_size = if self.size - self.pos < buf.len() { self.size } else { buf.len() }; |
219 | if copy_size == 0 { return Err(ByteIOError::EOF); } | |
220 | for i in 0..copy_size { | |
221 | buf[i] = self.buf[self.pos + i]; | |
222 | } | |
90aa4e6b KS |
223 | Ok(copy_size) |
224 | } | |
225 | ||
794364cf KS |
226 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
227 | let read_size = self.peek_buf(buf)?; | |
228 | self.pos += read_size; | |
229 | Ok(read_size) | |
230 | } | |
231 | ||
90aa4e6b KS |
232 | #[allow(unused_variables)] |
233 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<usize> { | |
90aa4e6b KS |
234 | Err(ByteIOError::NotImplemented) |
235 | } | |
236 | ||
237 | fn tell(&mut self) -> u64 { | |
238 | self.pos as u64 | |
239 | } | |
240 | ||
241 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
242 | let cur_pos = self.pos as i64; | |
243 | let cur_size = self.size as i64; | |
244 | match pos { | |
245 | SeekFrom::Start(x) => self.real_seek(x as i64), | |
246 | SeekFrom::Current(x) => self.real_seek(cur_pos + x), | |
247 | SeekFrom::End(x) => self.real_seek(cur_size + x), | |
248 | } | |
249 | } | |
250 | ||
251 | fn is_eof(&mut self) -> bool { | |
252 | self.pos >= self.size | |
253 | } | |
254 | ||
255 | fn is_seekable(&mut self) -> bool { | |
256 | true | |
257 | } | |
258 | } | |
259 | ||
260 | impl<'a> FileReader<'a> { | |
261 | ||
262 | pub fn new_read(file: &'a mut File) -> Self { | |
263 | FileReader { file: file, eof : false } | |
264 | } | |
265 | } | |
266 | ||
267 | impl<'a> ByteIO for FileReader<'a> { | |
268 | fn read_byte(&mut self) -> ByteIOResult<u8> { | |
269 | let mut byte : [u8; 1] = [0]; | |
270 | let err = self.file.read(&mut byte); | |
271 | if let Err(_) = err { return Err(ByteIOError::ReadError); } | |
272 | let sz = err.unwrap(); | |
273 | if sz == 0 { self.eof = true; return Err(ByteIOError::EOF); } | |
274 | Ok (byte[0]) | |
275 | } | |
276 | ||
794364cf KS |
277 | fn peek_byte(&mut self) -> ByteIOResult<u8> { |
278 | let b = self.read_byte()?; | |
279 | self.seek(SeekFrom::Current(-1))?; | |
280 | Ok(b) | |
281 | } | |
282 | ||
90aa4e6b KS |
283 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
284 | let res = self.file.read(buf); | |
285 | if let Err(_) = res { return Err(ByteIOError::ReadError); } | |
286 | let sz = res.unwrap(); | |
287 | if sz < buf.len() { self.eof = true; } | |
288 | Ok(sz) | |
289 | } | |
290 | ||
794364cf KS |
291 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
292 | let size = self.read_buf(buf)?; | |
293 | self.seek(SeekFrom::Current(-(size as i64)))?; | |
294 | Ok(size) | |
295 | } | |
296 | ||
90aa4e6b KS |
297 | #[allow(unused_variables)] |
298 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<usize> { | |
299 | Err(ByteIOError::NotImplemented) | |
300 | } | |
301 | ||
302 | fn tell(&mut self) -> u64 { | |
303 | self.file.seek(SeekFrom::Current(0)).unwrap() | |
304 | } | |
305 | ||
306 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
307 | let res = self.file.seek(pos); | |
308 | match res { | |
309 | Ok(r) => Ok(r), | |
310 | Err(_) => Err(ByteIOError::SeekError), | |
311 | } | |
312 | } | |
313 | ||
314 | fn is_eof(&mut self) -> bool { | |
315 | self.eof | |
316 | } | |
317 | ||
318 | fn is_seekable(&mut self) -> bool { | |
319 | true | |
320 | } | |
321 | } | |
322 | ||
323 | #[cfg(test)] | |
324 | mod test { | |
325 | use super::*; | |
326 | use std::fs::File; | |
327 | ||
328 | #[test] | |
329 | fn test_read() { | |
330 | //const DATA : &'static [u8] = include_bytes!("../../assets/file"); | |
331 | let buf: [u8; 64] = [1; 64]; | |
332 | let mut mr = MemoryReader::new_read(&buf); | |
333 | let mut reader = ByteReader::new(&mut mr); | |
334 | assert_eq!(reader.read_byte().unwrap(), 0x01u8); | |
335 | assert_eq!(reader.read_u16le().unwrap(), 0x0101u16); | |
336 | assert_eq!(reader.read_u24le().unwrap(), 0x010101u32); | |
337 | assert_eq!(reader.read_u32le().unwrap(), 0x01010101u32); | |
338 | assert_eq!(reader.read_u64le().unwrap(), 0x0101010101010101u64); | |
339 | let mut file = File::open("assets/MaoMacha.asx").unwrap(); | |
340 | let mut fr = FileReader::new_read(&mut file); | |
341 | let mut br2 = ByteReader::new(&mut fr); | |
342 | assert_eq!(br2.read_byte().unwrap(), 0x30); | |
343 | assert_eq!(br2.read_u24be().unwrap(), 0x26B275); | |
344 | assert_eq!(br2.read_u24le().unwrap(), 0xCF668E); | |
345 | assert_eq!(br2.read_u32be().unwrap(), 0x11A6D900); | |
346 | assert_eq!(br2.read_u32le().unwrap(), 0xCE6200AA); | |
347 | } | |
348 | #[test] | |
349 | fn test_write() { | |
350 | } | |
351 | } |