1 pub use std::io::SeekFrom;
3 use std::io::prelude::*;
17 pub type ByteIOResult<T> = Result<T, ByteIOError>;
20 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
21 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
22 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
23 fn read_byte(&mut self) -> ByteIOResult<u8>;
24 fn peek_byte(&mut self) -> ByteIOResult<u8>;
25 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()>;
26 fn tell(&mut self) -> u64;
27 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64>;
28 fn is_eof(&self) -> bool;
29 fn is_seekable(&mut self) -> bool;
30 fn size(&mut self) -> i64;
34 pub struct ByteReader<'a> {
38 pub struct MemoryReader<'a> {
44 pub struct FileReader<'a> {
49 macro_rules! read_int {
50 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
52 let mut buf: $inttype = 0;
53 $s.read_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
59 macro_rules! peek_int {
60 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
62 let mut buf: $inttype = 0;
63 $s.peek_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
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); }
74 let mut buf: $inttype = 0;
75 ptr::copy_nonoverlapping(src.as_ptr(), &mut buf as *mut $inttype as *mut u8, std::mem::size_of::<$inttype>());
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);
89 pub fn read_u24be(src: &[u8]) -> ByteIOResult<u32> {
90 if src.len() < 3 { return Err(ByteIOError::ReadError); }
91 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
93 pub fn read_u24le(src: &[u8]) -> ByteIOResult<u32> {
94 if src.len() < 3 { return Err(ByteIOError::ReadError); }
95 Ok((u32::from(src[2]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[0]))
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)?)) }
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); }
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>());
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);
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;
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;
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()) }
141 impl<'a> ByteReader<'a> {
142 pub fn new(io: &'a mut ByteIO) -> Self { ByteReader { io } }
144 pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
145 self.io.read_buf(buf)
148 pub fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
149 self.io.read_buf_some(buf)
152 pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
153 self.io.peek_buf(buf)
156 pub fn read_byte(&mut self) -> ByteIOResult<u8> {
160 pub fn peek_byte(&mut self) -> ByteIOResult<u8> {
164 pub fn read_u16be(&mut self) -> ByteIOResult<u16> {
165 read_int!(self, u16, 2, to_be)
168 pub fn peek_u16be(&mut self) -> ByteIOResult<u16> {
169 peek_int!(self, u16, 2, to_be)
172 pub fn read_u24be(&mut self) -> ByteIOResult<u32> {
173 let p16 = self.read_u16be()?;
174 let p8 = self.read_byte()?;
175 Ok((u32::from(p16) << 8) | u32::from(p8))
178 pub fn peek_u24be(&mut self) -> ByteIOResult<u32> {
179 let mut src: [u8; 3] = [0; 3];
180 self.peek_buf(&mut src)?;
181 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
184 pub fn read_u32be(&mut self) -> ByteIOResult<u32> {
185 read_int!(self, u32, 4, to_be)
188 pub fn peek_u32be(&mut self) -> ByteIOResult<u32> {
189 peek_int!(self, u32, 4, to_be)
192 pub fn read_u64be(&mut self) -> ByteIOResult<u64> {
193 read_int!(self, u64, 8, to_be)
196 pub fn peek_u64be(&mut self) -> ByteIOResult<u64> {
197 peek_int!(self, u64, 8, to_be)
200 pub fn read_f32be(&mut self) -> ByteIOResult<f32> {
201 Ok(f32::from_bits(self.read_u32be()?))
204 pub fn peek_f32be(&mut self) -> ByteIOResult<f32> {
205 Ok(f32::from_bits(self.peek_u32be()?))
208 pub fn read_f64be(&mut self) -> ByteIOResult<f64> {
209 Ok(f64::from_bits(self.read_u64be()?))
212 pub fn peek_f64be(&mut self) -> ByteIOResult<f64> {
213 Ok(f64::from_bits(self.peek_u64be()?))
216 pub fn read_u16le(&mut self) -> ByteIOResult<u16> {
217 read_int!(self, u16, 2, to_le)
220 pub fn peek_u16le(&mut self) -> ByteIOResult<u16> {
221 peek_int!(self, u16, 2, to_le)
224 pub fn read_u24le(&mut self) -> ByteIOResult<u32> {
225 let p8 = self.read_byte()?;
226 let p16 = self.read_u16le()?;
227 Ok((u32::from(p16) << 8) | u32::from(p8))
230 pub fn peek_u24le(&mut self) -> ByteIOResult<u32> {
231 let mut src: [u8; 3] = [0; 3];
232 self.peek_buf(&mut src)?;
233 Ok(u32::from(src[0]) | (u32::from(src[1]) << 8) | (u32::from(src[2]) << 16))
236 pub fn read_u32le(&mut self) -> ByteIOResult<u32> {
237 read_int!(self, u32, 4, to_le)
240 pub fn peek_u32le(&mut self) -> ByteIOResult<u32> {
241 peek_int!(self, u32, 4, to_le)
244 pub fn read_u64le(&mut self) -> ByteIOResult<u64> {
245 read_int!(self, u64, 8, to_le)
248 pub fn peek_u64le(&mut self) -> ByteIOResult<u64> {
249 peek_int!(self, u64, 8, to_le)
252 pub fn read_f32le(&mut self) -> ByteIOResult<f32> {
253 Ok(f32::from_bits(self.read_u32le()?))
256 pub fn peek_f32le(&mut self) -> ByteIOResult<f32> {
257 Ok(f32::from_bits(self.peek_u32le()?))
260 pub fn read_f64le(&mut self) -> ByteIOResult<f64> {
261 Ok(f64::from_bits(self.read_u64le()?))
264 pub fn peek_f64le(&mut self) -> ByteIOResult<f64> {
265 Ok(f64::from_bits(self.peek_u64le()?))
268 pub fn read_skip(&mut self, len: usize) -> ByteIOResult<()> {
269 if self.io.is_seekable() {
270 self.io.seek(SeekFrom::Current(len as i64))?;
273 let mut buf : [u8; 16] = [0; 16];
275 while ssize > bref.len() {
276 self.io.read_buf(bref)?;
280 self.io.read_byte()?;
287 pub fn tell(&mut self) -> u64 {
291 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
295 pub fn is_eof(&self) -> bool {
299 pub fn size(&mut self) -> i64 {
303 pub fn left(&mut self) -> i64 {
304 let size = self.io.size();
305 if size == -1 { return -1; }
306 size - (self.io.tell() as i64)
310 impl<'a> MemoryReader<'a> {
312 pub fn new_read(buf: &'a [u8]) -> Self {
313 MemoryReader { buf, size: buf.len(), pos: 0 }
316 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
317 if pos < 0 || (pos as usize) > self.size {
318 return Err(ByteIOError::WrongRange);
320 self.pos = pos as usize;
325 impl<'a> ByteIO for MemoryReader<'a> {
326 fn read_byte(&mut self) -> ByteIOResult<u8> {
327 if self.is_eof() { return Err(ByteIOError::EOF); }
328 let res = self.buf[self.pos];
333 fn peek_byte(&mut self) -> ByteIOResult<u8> {
334 if self.is_eof() { return Err(ByteIOError::EOF); }
335 Ok(self.buf[self.pos])
338 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
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); }
341 let dst = &mut buf[0..copy_size];
342 dst.copy_from_slice(&self.buf[self.pos..][..copy_size]);
346 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
347 let read_size = self.peek_buf(buf)?;
348 if read_size < buf.len() { return Err(ByteIOError::EOF); }
349 self.pos += read_size;
353 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
354 let read_size = self.peek_buf(buf)?;
355 self.pos += read_size;
359 #[allow(unused_variables)]
360 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
361 Err(ByteIOError::NotImplemented)
364 fn tell(&mut self) -> u64 {
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;
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),
378 fn is_eof(&self) -> bool {
379 self.pos >= self.size
382 fn is_seekable(&mut self) -> bool {
386 fn size(&mut self) -> i64 {
387 self.buf.len() as i64
391 impl<'a> FileReader<'a> {
393 pub fn new_read(file: &'a mut File) -> Self {
394 FileReader { file, eof : false }
398 impl<'a> ByteIO for FileReader<'a> {
399 fn read_byte(&mut self) -> ByteIOResult<u8> {
400 let mut byte : [u8; 1] = [0];
401 let ret = self.file.read(&mut byte);
402 if ret.is_err() { return Err(ByteIOError::ReadError); }
403 let sz = ret.unwrap();
404 if sz == 0 { self.eof = true; return Err(ByteIOError::EOF); }
408 fn peek_byte(&mut self) -> ByteIOResult<u8> {
409 let b = self.read_byte()?;
410 self.seek(SeekFrom::Current(-1))?;
414 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
415 let ret = self.file.read(buf);
416 if ret.is_err() { return Err(ByteIOError::ReadError); }
417 let sz = ret.unwrap();
418 if sz < buf.len() { self.eof = true; return Err(ByteIOError::EOF); }
422 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
423 let ret = self.file.read(buf);
424 if ret.is_err() { return Err(ByteIOError::ReadError); }
425 let sz = ret.unwrap();
426 if sz < buf.len() { self.eof = true; }
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)))?;
436 #[allow(unused_variables)]
437 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
438 Err(ByteIOError::NotImplemented)
441 fn tell(&mut self) -> u64 {
442 self.file.seek(SeekFrom::Current(0)).unwrap()
445 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
446 let res = self.file.seek(pos);
449 Err(_) => Err(ByteIOError::SeekError),
453 fn is_eof(&self) -> bool {
457 fn is_seekable(&mut self) -> bool {
461 fn size(&mut self) -> i64 {
467 pub struct ByteWriter<'a> {
471 pub struct MemoryWriter<'a> {
477 pub struct FileWriter {
481 impl<'a> ByteWriter<'a> {
482 pub fn new(io: &'a mut ByteIO) -> Self { ByteWriter { io } }
484 pub fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
485 self.io.write_buf(buf)
488 pub fn write_byte(&mut self, val: u8) -> ByteIOResult<()> {
489 let buf: [u8; 1] = [val];
490 self.io.write_buf(&buf)
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)
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)
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];
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];
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)
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)
523 pub fn write_u64be(&mut self, val: u64) -> ByteIOResult<()> {
524 self.write_u32be((val >> 32) as u32)?;
525 self.write_u32be(val as u32)
528 pub fn write_u64le(&mut self, val: u64) -> ByteIOResult<()> {
529 self.write_u32le(val as u32)?;
530 self.write_u32le((val >> 32) as u32)
533 pub fn write_f32be(&mut self, val: f32) -> ByteIOResult<()> {
534 self.write_u32be(val.to_bits())
537 pub fn write_f32le(&mut self, val: f32) -> ByteIOResult<()> {
538 self.write_u32le(val.to_bits())
541 pub fn write_f64be(&mut self, val: f64) -> ByteIOResult<()> {
542 self.write_u64be(val.to_bits())
545 pub fn write_f64le(&mut self, val: f64) -> ByteIOResult<()> {
546 self.write_u64le(val.to_bits())
549 pub fn tell(&mut self) -> u64 {
553 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
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)
564 impl<'a> MemoryWriter<'a> {
566 pub fn new_write(buf: &'a mut [u8]) -> Self {
568 MemoryWriter { buf, size: len, pos: 0 }
571 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
572 if pos < 0 || (pos as usize) > self.size {
573 return Err(ByteIOError::WrongRange)
575 self.pos = pos as usize;
580 impl<'a> ByteIO for MemoryWriter<'a> {
581 #[allow(unused_variables)]
582 fn read_byte(&mut self) -> ByteIOResult<u8> {
583 Err(ByteIOError::NotImplemented)
586 #[allow(unused_variables)]
587 fn peek_byte(&mut self) -> ByteIOResult<u8> {
588 Err(ByteIOError::NotImplemented)
591 #[allow(unused_variables)]
592 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
593 Err(ByteIOError::NotImplemented)
596 #[allow(unused_variables)]
597 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
598 Err(ByteIOError::NotImplemented)
601 #[allow(unused_variables)]
602 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
603 Err(ByteIOError::NotImplemented)
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];
611 self.pos += buf.len();
615 fn tell(&mut self) -> u64 {
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;
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),
629 fn is_eof(&self) -> bool {
630 self.pos >= self.size
633 fn is_seekable(&mut self) -> bool {
637 fn size(&mut self) -> i64 {
638 self.buf.len() as i64
643 pub fn new_write(file: File) -> Self {
648 impl ByteIO for FileWriter {
649 #[allow(unused_variables)]
650 fn read_byte(&mut self) -> ByteIOResult<u8> {
651 Err(ByteIOError::NotImplemented)
654 #[allow(unused_variables)]
655 fn peek_byte(&mut self) -> ByteIOResult<u8> {
656 Err(ByteIOError::NotImplemented)
659 #[allow(unused_variables)]
660 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
661 Err(ByteIOError::NotImplemented)
664 #[allow(unused_variables)]
665 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
666 Err(ByteIOError::NotImplemented)
669 #[allow(unused_variables)]
670 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
671 Err(ByteIOError::NotImplemented)
674 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
675 match self.file.write_all(buf) {
677 Err(_) => Err(ByteIOError::WriteError),
681 fn tell(&mut self) -> u64 {
682 self.file.seek(SeekFrom::Current(0)).unwrap()
685 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
686 let res = self.file.seek(pos);
689 Err(_) => Err(ByteIOError::SeekError),
693 fn is_eof(&self) -> bool {
697 fn is_seekable(&mut self) -> bool {
701 fn size(&mut self) -> i64 {
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);
722 let mut file = File::open("assets/Misc/MaoMacha.asx").unwrap();
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);
733 let mut buf: [u8; 64] = [0; 64];
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);
750 assert_eq!(buf[(i + 0x00) as usize], i);
751 assert_eq!(buf[(i + 0x12) as usize], i);