]>
Commit | Line | Data |
---|---|---|
37f2396b | 1 | pub use std::io::SeekFrom; |
90aa4e6b KS |
2 | use std::fs::File; |
3 | use std::io::prelude::*; | |
e243ceb4 | 4 | use std::ptr; |
90aa4e6b KS |
5 | |
6 | #[derive(Debug)] | |
7 | pub enum ByteIOError { | |
8 | EOF, | |
9 | WrongRange, | |
10 | WrongIOMode, | |
11 | NotImplemented, | |
12 | ReadError, | |
13 | WriteError, | |
14 | SeekError, | |
15 | } | |
16 | ||
296d86b2 | 17 | pub type ByteIOResult<T> = Result<T, ByteIOError>; |
90aa4e6b KS |
18 | |
19 | pub trait ByteIO { | |
20 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>; | |
89de616c | 21 | fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>; |
794364cf | 22 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>; |
90aa4e6b | 23 | fn read_byte(&mut self) -> ByteIOResult<u8>; |
794364cf | 24 | fn peek_byte(&mut self) -> ByteIOResult<u8>; |
d532000c | 25 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()>; |
90aa4e6b KS |
26 | fn tell(&mut self) -> u64; |
27 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64>; | |
e243ceb4 | 28 | fn is_eof(&self) -> bool; |
90aa4e6b | 29 | fn is_seekable(&mut self) -> bool; |
d532000c | 30 | fn size(&mut self) -> i64; |
90aa4e6b KS |
31 | } |
32 | ||
33 | #[allow(dead_code)] | |
34 | pub struct ByteReader<'a> { | |
35 | io: &'a mut ByteIO, | |
36 | } | |
37 | ||
38 | pub struct MemoryReader<'a> { | |
39 | buf: &'a [u8], | |
40 | size: usize, | |
41 | pos: usize, | |
90aa4e6b KS |
42 | } |
43 | ||
44 | pub struct FileReader<'a> { | |
45 | file: &'a File, | |
46 | eof: bool, | |
47 | } | |
48 | ||
49 | macro_rules! read_int { | |
50 | ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({ | |
90aa4e6b | 51 | unsafe { |
e243ceb4 KS |
52 | let mut buf: $inttype = 0; |
53 | $s.read_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?; | |
54 | Ok(buf.$which()) | |
90aa4e6b KS |
55 | } |
56 | }) | |
57 | } | |
58 | ||
794364cf KS |
59 | macro_rules! peek_int { |
60 | ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({ | |
794364cf | 61 | unsafe { |
e243ceb4 KS |
62 | let mut buf: $inttype = 0; |
63 | $s.peek_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?; | |
64 | Ok(buf.$which()) | |
794364cf KS |
65 | } |
66 | }) | |
67 | } | |
68 | ||
95058bc1 KS |
69 | macro_rules! read_int_func { |
70 | ($s: ident, $inttype: ty, $size: expr, $which: ident) => { | |
71 | pub fn $s(src: &[u8]) -> ByteIOResult<$inttype> { | |
72 | if src.len() < $size { return Err(ByteIOError::ReadError); } | |
73 | unsafe { | |
e243ceb4 | 74 | let mut buf: $inttype = 0; |
c9087a4c | 75 | ptr::copy_nonoverlapping(src.as_ptr(), &mut buf as *mut $inttype as *mut u8, std::mem::size_of::<$inttype>()); |
e243ceb4 | 76 | Ok(buf.$which()) |
95058bc1 KS |
77 | } |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | read_int_func!(read_u16be, u16, 2, to_be); | |
83 | read_int_func!(read_u16le, u16, 2, to_le); | |
84 | read_int_func!(read_u32be, u32, 4, to_be); | |
85 | read_int_func!(read_u32le, u32, 4, to_le); | |
86 | read_int_func!(read_u64be, u64, 8, to_be); | |
87 | read_int_func!(read_u64le, u64, 8, to_le); | |
88 | ||
89 | pub fn read_u24be(src: &[u8]) -> ByteIOResult<u32> { | |
90 | if src.len() < 3 { return Err(ByteIOError::ReadError); } | |
e243ceb4 | 91 | Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2])) |
95058bc1 KS |
92 | } |
93 | pub fn read_u24le(src: &[u8]) -> ByteIOResult<u32> { | |
94 | if src.len() < 3 { return Err(ByteIOError::ReadError); } | |
e243ceb4 | 95 | Ok((u32::from(src[2]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[0])) |
95058bc1 | 96 | } |
02a5bdaf KS |
97 | pub fn read_f32be(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32be(src)?)) } |
98 | pub fn read_f32le(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32le(src)?)) } | |
99 | pub fn read_f64be(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64be(src)?)) } | |
100 | pub fn read_f64le(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64le(src)?)) } | |
95058bc1 | 101 | |
95c7f2f6 KS |
102 | macro_rules! write_int_func { |
103 | ($s: ident, $inttype: ty, $size: expr, $which: ident) => { | |
104 | pub fn $s(dst: &mut [u8], val: $inttype) -> ByteIOResult<()> { | |
105 | if dst.len() < $size { return Err(ByteIOError::WriteError); } | |
106 | unsafe { | |
107 | let val = val.$which(); | |
108 | ptr::copy_nonoverlapping(&val as *const $inttype as *const u8, dst.as_mut_ptr(), std::mem::size_of::<$inttype>()); | |
109 | } | |
110 | Ok(()) | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | write_int_func!(write_u16be, u16, 2, to_be); | |
116 | write_int_func!(write_u16le, u16, 2, to_le); | |
117 | write_int_func!(write_u32be, u32, 4, to_be); | |
118 | write_int_func!(write_u32le, u32, 4, to_le); | |
119 | write_int_func!(write_u64be, u64, 8, to_be); | |
120 | write_int_func!(write_u64le, u64, 8, to_le); | |
121 | ||
122 | pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> { | |
123 | if dst.len() < 3 { return Err(ByteIOError::WriteError); } | |
124 | dst[0] = (val >> 16) as u8; | |
125 | dst[1] = (val >> 8) as u8; | |
126 | dst[2] = (val >> 0) as u8; | |
127 | Ok(()) | |
128 | } | |
129 | pub fn write_u24le(dst: &mut [u8], val: u32) -> ByteIOResult<()> { | |
130 | if dst.len() < 3 { return Err(ByteIOError::WriteError); } | |
131 | dst[0] = (val >> 0) as u8; | |
132 | dst[1] = (val >> 8) as u8; | |
133 | dst[2] = (val >> 16) as u8; | |
134 | Ok(()) | |
135 | } | |
02a5bdaf KS |
136 | pub fn write_f32be(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32be(dst, val.to_bits()) } |
137 | pub fn write_f32le(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32le(dst, val.to_bits()) } | |
138 | pub fn write_f64be(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64be(dst, val.to_bits()) } | |
139 | pub fn write_f64le(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64le(dst, val.to_bits()) } | |
95c7f2f6 | 140 | |
90aa4e6b | 141 | impl<'a> ByteReader<'a> { |
e243ceb4 | 142 | pub fn new(io: &'a mut ByteIO) -> Self { ByteReader { io } } |
90aa4e6b KS |
143 | |
144 | pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
145 | self.io.read_buf(buf) | |
146 | } | |
147 | ||
89de616c KS |
148 | pub fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
149 | self.io.read_buf_some(buf) | |
150 | } | |
151 | ||
794364cf KS |
152 | pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
153 | self.io.peek_buf(buf) | |
154 | } | |
155 | ||
90aa4e6b KS |
156 | pub fn read_byte(&mut self) -> ByteIOResult<u8> { |
157 | self.io.read_byte() | |
158 | } | |
159 | ||
794364cf KS |
160 | pub fn peek_byte(&mut self) -> ByteIOResult<u8> { |
161 | self.io.peek_byte() | |
162 | } | |
163 | ||
90aa4e6b KS |
164 | pub fn read_u16be(&mut self) -> ByteIOResult<u16> { |
165 | read_int!(self, u16, 2, to_be) | |
166 | } | |
167 | ||
794364cf KS |
168 | pub fn peek_u16be(&mut self) -> ByteIOResult<u16> { |
169 | peek_int!(self, u16, 2, to_be) | |
170 | } | |
171 | ||
90aa4e6b | 172 | pub fn read_u24be(&mut self) -> ByteIOResult<u32> { |
794364cf KS |
173 | let p16 = self.read_u16be()?; |
174 | let p8 = self.read_byte()?; | |
e243ceb4 | 175 | Ok((u32::from(p16) << 8) | u32::from(p8)) |
794364cf KS |
176 | } |
177 | ||
178 | pub fn peek_u24be(&mut self) -> ByteIOResult<u32> { | |
179 | let mut src: [u8; 3] = [0; 3]; | |
180 | self.peek_buf(&mut src)?; | |
e243ceb4 | 181 | Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2])) |
90aa4e6b KS |
182 | } |
183 | ||
184 | pub fn read_u32be(&mut self) -> ByteIOResult<u32> { | |
185 | read_int!(self, u32, 4, to_be) | |
186 | } | |
187 | ||
794364cf KS |
188 | pub fn peek_u32be(&mut self) -> ByteIOResult<u32> { |
189 | peek_int!(self, u32, 4, to_be) | |
190 | } | |
191 | ||
90aa4e6b KS |
192 | pub fn read_u64be(&mut self) -> ByteIOResult<u64> { |
193 | read_int!(self, u64, 8, to_be) | |
194 | } | |
195 | ||
794364cf KS |
196 | pub fn peek_u64be(&mut self) -> ByteIOResult<u64> { |
197 | peek_int!(self, u64, 8, to_be) | |
198 | } | |
199 | ||
02a5bdaf KS |
200 | pub fn read_f32be(&mut self) -> ByteIOResult<f32> { |
201 | Ok(f32::from_bits(self.read_u32be()?)) | |
202 | } | |
203 | ||
204 | pub fn peek_f32be(&mut self) -> ByteIOResult<f32> { | |
205 | Ok(f32::from_bits(self.peek_u32be()?)) | |
206 | } | |
207 | ||
208 | pub fn read_f64be(&mut self) -> ByteIOResult<f64> { | |
209 | Ok(f64::from_bits(self.read_u64be()?)) | |
210 | } | |
211 | ||
212 | pub fn peek_f64be(&mut self) -> ByteIOResult<f64> { | |
213 | Ok(f64::from_bits(self.peek_u64be()?)) | |
214 | } | |
215 | ||
90aa4e6b KS |
216 | pub fn read_u16le(&mut self) -> ByteIOResult<u16> { |
217 | read_int!(self, u16, 2, to_le) | |
218 | } | |
219 | ||
794364cf KS |
220 | pub fn peek_u16le(&mut self) -> ByteIOResult<u16> { |
221 | peek_int!(self, u16, 2, to_le) | |
222 | } | |
223 | ||
90aa4e6b | 224 | pub fn read_u24le(&mut self) -> ByteIOResult<u32> { |
794364cf KS |
225 | let p8 = self.read_byte()?; |
226 | let p16 = self.read_u16le()?; | |
e243ceb4 | 227 | Ok((u32::from(p16) << 8) | u32::from(p8)) |
794364cf KS |
228 | } |
229 | ||
230 | pub fn peek_u24le(&mut self) -> ByteIOResult<u32> { | |
231 | let mut src: [u8; 3] = [0; 3]; | |
232 | self.peek_buf(&mut src)?; | |
e243ceb4 | 233 | Ok(u32::from(src[0]) | (u32::from(src[1]) << 8) | (u32::from(src[2]) << 16)) |
90aa4e6b KS |
234 | } |
235 | ||
236 | pub fn read_u32le(&mut self) -> ByteIOResult<u32> { | |
237 | read_int!(self, u32, 4, to_le) | |
238 | } | |
239 | ||
794364cf KS |
240 | pub fn peek_u32le(&mut self) -> ByteIOResult<u32> { |
241 | peek_int!(self, u32, 4, to_le) | |
242 | } | |
243 | ||
90aa4e6b KS |
244 | pub fn read_u64le(&mut self) -> ByteIOResult<u64> { |
245 | read_int!(self, u64, 8, to_le) | |
246 | } | |
247 | ||
794364cf KS |
248 | pub fn peek_u64le(&mut self) -> ByteIOResult<u64> { |
249 | peek_int!(self, u64, 8, to_le) | |
250 | } | |
251 | ||
02a5bdaf KS |
252 | pub fn read_f32le(&mut self) -> ByteIOResult<f32> { |
253 | Ok(f32::from_bits(self.read_u32le()?)) | |
254 | } | |
255 | ||
256 | pub fn peek_f32le(&mut self) -> ByteIOResult<f32> { | |
257 | Ok(f32::from_bits(self.peek_u32le()?)) | |
258 | } | |
259 | ||
260 | pub fn read_f64le(&mut self) -> ByteIOResult<f64> { | |
261 | Ok(f64::from_bits(self.read_u64le()?)) | |
262 | } | |
263 | ||
264 | pub fn peek_f64le(&mut self) -> ByteIOResult<f64> { | |
265 | Ok(f64::from_bits(self.peek_u64le()?)) | |
266 | } | |
267 | ||
6b167b0c | 268 | pub fn read_skip(&mut self, len: usize) -> ByteIOResult<()> { |
90aa4e6b KS |
269 | if self.io.is_seekable() { |
270 | self.io.seek(SeekFrom::Current(len as i64))?; | |
271 | } else { | |
272 | let mut ssize = len; | |
273 | let mut buf : [u8; 16] = [0; 16]; | |
9037cf6b | 274 | let bref = &mut buf; |
90aa4e6b KS |
275 | while ssize > bref.len() { |
276 | self.io.read_buf(bref)?; | |
277 | ssize -= bref.len(); | |
278 | } | |
279 | while ssize > 0 { | |
280 | self.io.read_byte()?; | |
e243ceb4 | 281 | ssize -= 1; |
90aa4e6b KS |
282 | } |
283 | } | |
6b167b0c | 284 | Ok(()) |
90aa4e6b KS |
285 | } |
286 | ||
287 | pub fn tell(&mut self) -> u64 { | |
288 | self.io.tell() | |
289 | } | |
290 | ||
291 | pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
292 | self.io.seek(pos) | |
293 | } | |
294 | ||
e243ceb4 | 295 | pub fn is_eof(&self) -> bool { |
90aa4e6b KS |
296 | self.io.is_eof() |
297 | } | |
96c6be97 KS |
298 | |
299 | pub fn size(&mut self) -> i64 { | |
300 | self.io.size() | |
301 | } | |
302 | ||
303 | pub fn left(&mut self) -> i64 { | |
304 | let size = self.io.size(); | |
305 | if size == -1 { return -1; } | |
e243ceb4 | 306 | size - (self.io.tell() as i64) |
96c6be97 | 307 | } |
90aa4e6b KS |
308 | } |
309 | ||
310 | impl<'a> MemoryReader<'a> { | |
311 | ||
312 | pub fn new_read(buf: &'a [u8]) -> Self { | |
e243ceb4 | 313 | MemoryReader { buf, size: buf.len(), pos: 0 } |
90aa4e6b KS |
314 | } |
315 | ||
316 | fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> { | |
317 | if pos < 0 || (pos as usize) > self.size { | |
e243ceb4 | 318 | return Err(ByteIOError::WrongRange); |
90aa4e6b KS |
319 | } |
320 | self.pos = pos as usize; | |
321 | Ok(pos as u64) | |
322 | } | |
90aa4e6b KS |
323 | } |
324 | ||
325 | impl<'a> ByteIO for MemoryReader<'a> { | |
326 | fn read_byte(&mut self) -> ByteIOResult<u8> { | |
90aa4e6b KS |
327 | if self.is_eof() { return Err(ByteIOError::EOF); } |
328 | let res = self.buf[self.pos]; | |
e243ceb4 | 329 | self.pos += 1; |
90aa4e6b KS |
330 | Ok(res) |
331 | } | |
332 | ||
794364cf KS |
333 | fn peek_byte(&mut self) -> ByteIOResult<u8> { |
334 | if self.is_eof() { return Err(ByteIOError::EOF); } | |
335 | Ok(self.buf[self.pos]) | |
336 | } | |
337 | ||
338 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
90aa4e6b KS |
339 | let copy_size = if self.size - self.pos < buf.len() { self.size } else { buf.len() }; |
340 | if copy_size == 0 { return Err(ByteIOError::EOF); } | |
e243ceb4 KS |
341 | let dst = &mut buf[0..copy_size]; |
342 | dst.copy_from_slice(&self.buf[self.pos..][..copy_size]); | |
90aa4e6b KS |
343 | Ok(copy_size) |
344 | } | |
345 | ||
794364cf | 346 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
89de616c KS |
347 | let read_size = self.peek_buf(buf)?; |
348 | if read_size < buf.len() { return Err(ByteIOError::EOF); } | |
349 | self.pos += read_size; | |
350 | Ok(read_size) | |
351 | } | |
352 | ||
353 | fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
794364cf KS |
354 | let read_size = self.peek_buf(buf)?; |
355 | self.pos += read_size; | |
356 | Ok(read_size) | |
357 | } | |
358 | ||
90aa4e6b | 359 | #[allow(unused_variables)] |
d532000c | 360 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { |
90aa4e6b KS |
361 | Err(ByteIOError::NotImplemented) |
362 | } | |
363 | ||
364 | fn tell(&mut self) -> u64 { | |
365 | self.pos as u64 | |
366 | } | |
367 | ||
368 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
369 | let cur_pos = self.pos as i64; | |
370 | let cur_size = self.size as i64; | |
371 | match pos { | |
372 | SeekFrom::Start(x) => self.real_seek(x as i64), | |
373 | SeekFrom::Current(x) => self.real_seek(cur_pos + x), | |
374 | SeekFrom::End(x) => self.real_seek(cur_size + x), | |
375 | } | |
376 | } | |
377 | ||
e243ceb4 | 378 | fn is_eof(&self) -> bool { |
90aa4e6b KS |
379 | self.pos >= self.size |
380 | } | |
381 | ||
382 | fn is_seekable(&mut self) -> bool { | |
383 | true | |
384 | } | |
d532000c KS |
385 | |
386 | fn size(&mut self) -> i64 { | |
387 | self.buf.len() as i64 | |
388 | } | |
90aa4e6b KS |
389 | } |
390 | ||
391 | impl<'a> FileReader<'a> { | |
392 | ||
393 | pub fn new_read(file: &'a mut File) -> Self { | |
e243ceb4 | 394 | FileReader { file, eof : false } |
90aa4e6b KS |
395 | } |
396 | } | |
397 | ||
398 | impl<'a> ByteIO for FileReader<'a> { | |
399 | fn read_byte(&mut self) -> ByteIOResult<u8> { | |
400 | let mut byte : [u8; 1] = [0]; | |
e243ceb4 KS |
401 | let ret = self.file.read(&mut byte); |
402 | if ret.is_err() { return Err(ByteIOError::ReadError); } | |
403 | let sz = ret.unwrap(); | |
90aa4e6b KS |
404 | if sz == 0 { self.eof = true; return Err(ByteIOError::EOF); } |
405 | Ok (byte[0]) | |
406 | } | |
407 | ||
794364cf KS |
408 | fn peek_byte(&mut self) -> ByteIOResult<u8> { |
409 | let b = self.read_byte()?; | |
410 | self.seek(SeekFrom::Current(-1))?; | |
411 | Ok(b) | |
412 | } | |
413 | ||
90aa4e6b | 414 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
e243ceb4 KS |
415 | let ret = self.file.read(buf); |
416 | if ret.is_err() { return Err(ByteIOError::ReadError); } | |
417 | let sz = ret.unwrap(); | |
89de616c KS |
418 | if sz < buf.len() { self.eof = true; return Err(ByteIOError::EOF); } |
419 | Ok(sz) | |
420 | } | |
421 | ||
422 | fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
e243ceb4 KS |
423 | let ret = self.file.read(buf); |
424 | if ret.is_err() { return Err(ByteIOError::ReadError); } | |
425 | let sz = ret.unwrap(); | |
90aa4e6b KS |
426 | if sz < buf.len() { self.eof = true; } |
427 | Ok(sz) | |
428 | } | |
429 | ||
794364cf KS |
430 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { |
431 | let size = self.read_buf(buf)?; | |
432 | self.seek(SeekFrom::Current(-(size as i64)))?; | |
433 | Ok(size) | |
434 | } | |
435 | ||
90aa4e6b | 436 | #[allow(unused_variables)] |
d532000c | 437 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { |
90aa4e6b KS |
438 | Err(ByteIOError::NotImplemented) |
439 | } | |
440 | ||
441 | fn tell(&mut self) -> u64 { | |
442 | self.file.seek(SeekFrom::Current(0)).unwrap() | |
443 | } | |
444 | ||
445 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
446 | let res = self.file.seek(pos); | |
447 | match res { | |
448 | Ok(r) => Ok(r), | |
449 | Err(_) => Err(ByteIOError::SeekError), | |
450 | } | |
451 | } | |
452 | ||
e243ceb4 | 453 | fn is_eof(&self) -> bool { |
90aa4e6b KS |
454 | self.eof |
455 | } | |
456 | ||
457 | fn is_seekable(&mut self) -> bool { | |
458 | true | |
459 | } | |
d532000c KS |
460 | |
461 | fn size(&mut self) -> i64 { | |
462 | -1 | |
463 | } | |
464 | } | |
465 | ||
466 | #[allow(dead_code)] | |
467 | pub struct ByteWriter<'a> { | |
468 | io: &'a mut ByteIO, | |
469 | } | |
470 | ||
471 | pub struct MemoryWriter<'a> { | |
472 | buf: &'a mut [u8], | |
473 | size: usize, | |
474 | pos: usize, | |
475 | } | |
476 | ||
5eb3cc60 KS |
477 | pub struct FileWriter { |
478 | file: File, | |
d532000c KS |
479 | } |
480 | ||
481 | impl<'a> ByteWriter<'a> { | |
e243ceb4 | 482 | pub fn new(io: &'a mut ByteIO) -> Self { ByteWriter { io } } |
d532000c KS |
483 | |
484 | pub fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { | |
485 | self.io.write_buf(buf) | |
486 | } | |
487 | ||
488 | pub fn write_byte(&mut self, val: u8) -> ByteIOResult<()> { | |
489 | let buf: [u8; 1] = [val]; | |
490 | self.io.write_buf(&buf) | |
491 | } | |
492 | ||
493 | pub fn write_u16be(&mut self, val: u16) -> ByteIOResult<()> { | |
494 | let buf: [u8; 2] = [((val >> 8) & 0xFF) as u8, (val & 0xFF) as u8]; | |
495 | self.io.write_buf(&buf) | |
496 | } | |
497 | ||
498 | pub fn write_u16le(&mut self, val: u16) -> ByteIOResult<()> { | |
499 | let buf: [u8; 2] = [(val & 0xFF) as u8, ((val >> 8) & 0xFF) as u8]; | |
500 | self.io.write_buf(&buf) | |
501 | } | |
502 | ||
503 | pub fn write_u24be(&mut self, val: u32) -> ByteIOResult<()> { | |
504 | let buf: [u8; 3] = [((val >> 16) & 0xFF) as u8, ((val >> 8) & 0xFF) as u8, (val & 0xFF) as u8]; | |
505 | self.write_buf(&buf) | |
506 | } | |
507 | ||
508 | pub fn write_u24le(&mut self, val: u32) -> ByteIOResult<()> { | |
509 | let buf: [u8; 3] = [(val & 0xFF) as u8, ((val >> 8) & 0xFF) as u8, ((val >> 16) & 0xFF) as u8]; | |
510 | self.write_buf(&buf) | |
511 | } | |
512 | ||
513 | pub fn write_u32be(&mut self, val: u32) -> ByteIOResult<()> { | |
514 | self.write_u16be(((val >> 16) & 0xFFFF) as u16)?; | |
515 | self.write_u16be((val & 0xFFFF) as u16) | |
516 | } | |
517 | ||
518 | pub fn write_u32le(&mut self, val: u32) -> ByteIOResult<()> { | |
519 | self.write_u16le((val & 0xFFFF) as u16)?; | |
520 | self.write_u16le(((val >> 16) & 0xFFFF) as u16) | |
521 | } | |
522 | ||
523 | pub fn write_u64be(&mut self, val: u64) -> ByteIOResult<()> { | |
e243ceb4 KS |
524 | self.write_u32be((val >> 32) as u32)?; |
525 | self.write_u32be(val as u32) | |
d532000c KS |
526 | } |
527 | ||
528 | pub fn write_u64le(&mut self, val: u64) -> ByteIOResult<()> { | |
e243ceb4 KS |
529 | self.write_u32le(val as u32)?; |
530 | self.write_u32le((val >> 32) as u32) | |
d532000c KS |
531 | } |
532 | ||
02a5bdaf KS |
533 | pub fn write_f32be(&mut self, val: f32) -> ByteIOResult<()> { |
534 | self.write_u32be(val.to_bits()) | |
535 | } | |
536 | ||
537 | pub fn write_f32le(&mut self, val: f32) -> ByteIOResult<()> { | |
538 | self.write_u32le(val.to_bits()) | |
539 | } | |
540 | ||
541 | pub fn write_f64be(&mut self, val: f64) -> ByteIOResult<()> { | |
542 | self.write_u64be(val.to_bits()) | |
543 | } | |
544 | ||
545 | pub fn write_f64le(&mut self, val: f64) -> ByteIOResult<()> { | |
546 | self.write_u64le(val.to_bits()) | |
547 | } | |
548 | ||
d532000c KS |
549 | pub fn tell(&mut self) -> u64 { |
550 | self.io.tell() | |
551 | } | |
552 | ||
553 | pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
554 | self.io.seek(pos) | |
555 | } | |
556 | ||
557 | pub fn size_left(&mut self) -> i64 { | |
558 | let sz = self.io.size(); | |
559 | if sz == -1 { return -1; } | |
560 | sz - (self.tell() as i64) | |
561 | } | |
562 | } | |
563 | ||
564 | impl<'a> MemoryWriter<'a> { | |
565 | ||
566 | pub fn new_write(buf: &'a mut [u8]) -> Self { | |
567 | let len = buf.len(); | |
e243ceb4 | 568 | MemoryWriter { buf, size: len, pos: 0 } |
d532000c KS |
569 | } |
570 | ||
571 | fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> { | |
572 | if pos < 0 || (pos as usize) > self.size { | |
573 | return Err(ByteIOError::WrongRange) | |
574 | } | |
575 | self.pos = pos as usize; | |
576 | Ok(pos as u64) | |
577 | } | |
578 | } | |
579 | ||
580 | impl<'a> ByteIO for MemoryWriter<'a> { | |
581 | #[allow(unused_variables)] | |
582 | fn read_byte(&mut self) -> ByteIOResult<u8> { | |
583 | Err(ByteIOError::NotImplemented) | |
584 | } | |
585 | ||
586 | #[allow(unused_variables)] | |
587 | fn peek_byte(&mut self) -> ByteIOResult<u8> { | |
588 | Err(ByteIOError::NotImplemented) | |
589 | } | |
590 | ||
591 | #[allow(unused_variables)] | |
592 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
593 | Err(ByteIOError::NotImplemented) | |
594 | } | |
595 | ||
596 | #[allow(unused_variables)] | |
597 | fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
598 | Err(ByteIOError::NotImplemented) | |
599 | } | |
600 | ||
601 | #[allow(unused_variables)] | |
602 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
603 | Err(ByteIOError::NotImplemented) | |
604 | } | |
605 | ||
606 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { | |
607 | if self.pos + buf.len() > self.size { return Err(ByteIOError::WriteError); } | |
608 | for i in 0..buf.len() { | |
609 | self.buf[self.pos + i] = buf[i]; | |
610 | } | |
611 | self.pos += buf.len(); | |
612 | Ok(()) | |
613 | } | |
614 | ||
615 | fn tell(&mut self) -> u64 { | |
616 | self.pos as u64 | |
617 | } | |
618 | ||
619 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
620 | let cur_pos = self.pos as i64; | |
621 | let cur_size = self.size as i64; | |
622 | match pos { | |
623 | SeekFrom::Start(x) => self.real_seek(x as i64), | |
624 | SeekFrom::Current(x) => self.real_seek(cur_pos + x), | |
625 | SeekFrom::End(x) => self.real_seek(cur_size + x), | |
626 | } | |
627 | } | |
628 | ||
e243ceb4 | 629 | fn is_eof(&self) -> bool { |
d532000c KS |
630 | self.pos >= self.size |
631 | } | |
632 | ||
633 | fn is_seekable(&mut self) -> bool { | |
634 | true | |
635 | } | |
636 | ||
637 | fn size(&mut self) -> i64 { | |
638 | self.buf.len() as i64 | |
639 | } | |
640 | } | |
641 | ||
5eb3cc60 KS |
642 | impl FileWriter { |
643 | pub fn new_write(file: File) -> Self { | |
e243ceb4 | 644 | FileWriter { file } |
d532000c KS |
645 | } |
646 | } | |
647 | ||
5eb3cc60 | 648 | impl ByteIO for FileWriter { |
d532000c KS |
649 | #[allow(unused_variables)] |
650 | fn read_byte(&mut self) -> ByteIOResult<u8> { | |
651 | Err(ByteIOError::NotImplemented) | |
652 | } | |
653 | ||
654 | #[allow(unused_variables)] | |
655 | fn peek_byte(&mut self) -> ByteIOResult<u8> { | |
656 | Err(ByteIOError::NotImplemented) | |
657 | } | |
658 | ||
659 | #[allow(unused_variables)] | |
660 | fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
661 | Err(ByteIOError::NotImplemented) | |
662 | } | |
663 | ||
664 | #[allow(unused_variables)] | |
665 | fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
666 | Err(ByteIOError::NotImplemented) | |
667 | } | |
668 | ||
669 | #[allow(unused_variables)] | |
670 | fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> { | |
671 | Err(ByteIOError::NotImplemented) | |
672 | } | |
673 | ||
674 | fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { | |
675 | match self.file.write_all(buf) { | |
676 | Ok(()) => Ok(()), | |
677 | Err(_) => Err(ByteIOError::WriteError), | |
678 | } | |
679 | } | |
680 | ||
681 | fn tell(&mut self) -> u64 { | |
682 | self.file.seek(SeekFrom::Current(0)).unwrap() | |
683 | } | |
684 | ||
685 | fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> { | |
686 | let res = self.file.seek(pos); | |
687 | match res { | |
688 | Ok(r) => Ok(r), | |
689 | Err(_) => Err(ByteIOError::SeekError), | |
690 | } | |
691 | } | |
692 | ||
e243ceb4 | 693 | fn is_eof(&self) -> bool { |
d532000c KS |
694 | false |
695 | } | |
696 | ||
697 | fn is_seekable(&mut self) -> bool { | |
698 | true | |
699 | } | |
700 | ||
701 | fn size(&mut self) -> i64 { | |
702 | -1 | |
703 | } | |
90aa4e6b KS |
704 | } |
705 | ||
706 | #[cfg(test)] | |
707 | mod test { | |
708 | use super::*; | |
709 | use std::fs::File; | |
710 | ||
711 | #[test] | |
712 | fn test_read() { | |
713 | //const DATA : &'static [u8] = include_bytes!("../../assets/file"); | |
714 | let buf: [u8; 64] = [1; 64]; | |
715 | let mut mr = MemoryReader::new_read(&buf); | |
716 | let mut reader = ByteReader::new(&mut mr); | |
717 | assert_eq!(reader.read_byte().unwrap(), 0x01u8); | |
718 | assert_eq!(reader.read_u16le().unwrap(), 0x0101u16); | |
719 | assert_eq!(reader.read_u24le().unwrap(), 0x010101u32); | |
720 | assert_eq!(reader.read_u32le().unwrap(), 0x01010101u32); | |
721 | assert_eq!(reader.read_u64le().unwrap(), 0x0101010101010101u64); | |
1678d59a | 722 | let mut file = File::open("assets/Misc/MaoMacha.asx").unwrap(); |
90aa4e6b KS |
723 | let mut fr = FileReader::new_read(&mut file); |
724 | let mut br2 = ByteReader::new(&mut fr); | |
725 | assert_eq!(br2.read_byte().unwrap(), 0x30); | |
726 | assert_eq!(br2.read_u24be().unwrap(), 0x26B275); | |
727 | assert_eq!(br2.read_u24le().unwrap(), 0xCF668E); | |
728 | assert_eq!(br2.read_u32be().unwrap(), 0x11A6D900); | |
729 | assert_eq!(br2.read_u32le().unwrap(), 0xCE6200AA); | |
730 | } | |
731 | #[test] | |
732 | fn test_write() { | |
d532000c KS |
733 | let mut buf: [u8; 64] = [0; 64]; |
734 | { | |
735 | let mut mw = MemoryWriter::new_write(&mut buf); | |
736 | let mut bw = ByteWriter::new(&mut mw); | |
737 | bw.write_byte(0x00).unwrap(); | |
738 | bw.write_u16be(0x0102).unwrap(); | |
739 | bw.write_u24be(0x030405).unwrap(); | |
740 | bw.write_u32be(0x06070809).unwrap(); | |
741 | bw.write_u64be(0x0A0B0C0D0E0F1011).unwrap(); | |
742 | bw.write_byte(0x00).unwrap(); | |
743 | bw.write_u16le(0x0201).unwrap(); | |
744 | bw.write_u24le(0x050403).unwrap(); | |
745 | bw.write_u32le(0x09080706).unwrap(); | |
746 | bw.write_u64le(0x11100F0E0D0C0B0A).unwrap(); | |
747 | assert_eq!(bw.size_left(), 28); | |
748 | } | |
749 | for i in 0..0x12 { | |
750 | assert_eq!(buf[(i + 0x00) as usize], i); | |
751 | assert_eq!(buf[(i + 0x12) as usize], i); | |
752 | } | |
90aa4e6b KS |
753 | } |
754 | } |