1 //! Bytestream reading/writing functionality.
2 pub use std::io::SeekFrom;
3 use std::io::prelude::*;
6 /// A list specifying general bytestream reading and writing errors.
11 /// Wrong seek position was provided.
13 /// Tried to call read() on bytestream writer or write() on bytestream reader.
15 /// Functionality is not implemented.
25 /// A specialised `Result` type for bytestream operations.
26 pub type ByteIOResult<T> = Result<T, ByteIOError>;
28 /// Common trait for bytestream operations.
30 /// Reads data into provided buffer. Fails if it cannot fill whole buffer.
31 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
32 /// Reads data into provided buffer. Partial read is treated as success.
33 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
34 /// Reads data into provided buffer but does not advance read position.
35 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
36 /// Reads single byte from the stream.
37 fn read_byte(&mut self) -> ByteIOResult<u8>;
38 /// Returns the next byte value in the stream without advancing read position.
39 fn peek_byte(&mut self) -> ByteIOResult<u8>;
40 /// Writes buffer to the stream.
41 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()>;
42 /// Returns current read or write position.
43 fn tell(&mut self) -> u64;
44 /// Seeks to the provided position.
45 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64>;
46 /// Tells whether this is end of stream.
47 fn is_eof(&self) -> bool;
48 /// Reports whether stream is seekable or not.
49 fn is_seekable(&mut self) -> bool;
50 /// Returns stream size or -1 if it is not known.
51 fn size(&mut self) -> i64;
52 /// Flushes output if possible.
53 fn flush(&mut self) -> ByteIOResult<()>;
56 /// High-level bytestream reader.
58 /// User is supposed to create some reader implementing [`ByteIO`] trait e.g. [`MemoryReader`] and use it to create `ByteReader` which can be used for reading e.g. various integer types.
63 /// use nihav_core::io::byteio::{MemoryReader,ByteReader};
64 /// # use nihav_core::io::byteio::ByteIOResult;
66 /// # fn foo() -> ByteIOResult<()> {
67 /// let memory: [u8; 4] = [ 0, 42, 42, 0 ];
68 /// let mut mr = MemoryReader::new_read(&memory);
69 /// let mut br = ByteReader::new(&mut mr);
70 /// let val = br.read_u16be()?; // read 16-bit big-endian integer, should be 42
71 /// let val = br.read_u16le()?; // read 16-bit little-endian integer, should be 42 as well
76 /// [`ByteIO`]: ./trait.ByteIO.html
77 /// [`MemoryReader`]: ./struct.MemoryReader.html
79 pub struct ByteReader<'a> {
80 io: &'a mut dyn ByteIO,
83 /// Bytestream reader from memory.
84 pub struct MemoryReader<'a> {
89 /// Bytestream reader from anything implementing `std::io::Read` and `std::io::Seek`.
90 pub struct FileReader<T: Read+Seek> {
95 /// Bytestream reader from anything implementing `std::io::Read` and `std::io::Seek` that operates only on a part of the input.
96 pub struct BoundedFileReader<T: Read+Seek> {
103 macro_rules! read_int {
104 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
106 let mut buf: $inttype = 0;
107 $s.read_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
113 macro_rules! peek_int {
114 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
116 let mut buf: $inttype = 0;
117 $s.peek_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
123 macro_rules! read_int_func {
124 ($s: ident, $inttype: ty, $size: expr, $which: ident) => {
125 /// Reads integer of certain size and endianness.
126 pub fn $s(src: &[u8]) -> ByteIOResult<$inttype> {
127 if src.len() < $size { return Err(ByteIOError::ReadError); }
129 let mut buf: $inttype = 0;
130 ptr::copy_nonoverlapping(src.as_ptr(), &mut buf as *mut $inttype as *mut u8, std::mem::size_of::<$inttype>());
137 read_int_func!(read_u16be, u16, 2, to_be);
138 read_int_func!(read_u16le, u16, 2, to_le);
139 read_int_func!(read_u32be, u32, 4, to_be);
140 read_int_func!(read_u32le, u32, 4, to_le);
141 read_int_func!(read_u64be, u64, 8, to_be);
142 read_int_func!(read_u64le, u64, 8, to_le);
144 /// Reads 24-bit big-endian integer.
149 /// use nihav_core::io::byteio::read_u24be;
150 /// # use nihav_core::io::byteio::ByteIOResult;
152 /// # fn foo() -> ByteIOResult<()> {
153 /// let src: [u8; 3] = [ 1, 2, 3];
154 /// let value = read_u24be(&src)?; // should return 0x010203
158 pub fn read_u24be(src: &[u8]) -> ByteIOResult<u32> {
159 if src.len() < 3 { return Err(ByteIOError::ReadError); }
160 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
162 /// Reads 24-bit little-endian integer.
163 pub fn read_u24le(src: &[u8]) -> ByteIOResult<u32> {
164 if src.len() < 3 { return Err(ByteIOError::ReadError); }
165 Ok((u32::from(src[2]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[0]))
167 /// Reads 32-bit big-endian floating point number.
168 pub fn read_f32be(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32be(src)?)) }
169 /// Reads 32-bit little-endian floating point number.
170 pub fn read_f32le(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32le(src)?)) }
171 /// Reads 64-bit big-endian floating point number.
172 pub fn read_f64be(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64be(src)?)) }
173 /// Reads 64-bit little-endian floating point number.
174 pub fn read_f64le(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64le(src)?)) }
176 macro_rules! write_int_func {
177 ($s: ident, $inttype: ty, $size: expr, $which: ident) => {
178 /// Writes integer of certain size and endianness into byte buffer.
179 pub fn $s(dst: &mut [u8], val: $inttype) -> ByteIOResult<()> {
180 if dst.len() < $size { return Err(ByteIOError::WriteError); }
182 let val = val.$which();
183 ptr::copy_nonoverlapping(&val as *const $inttype as *const u8, dst.as_mut_ptr(), std::mem::size_of::<$inttype>());
190 write_int_func!(write_u16be, u16, 2, to_be);
191 write_int_func!(write_u16le, u16, 2, to_le);
192 write_int_func!(write_u32be, u32, 4, to_be);
193 write_int_func!(write_u32le, u32, 4, to_le);
194 write_int_func!(write_u64be, u64, 8, to_be);
195 write_int_func!(write_u64le, u64, 8, to_le);
197 /// Writes 24-bit big-endian integer to the provided buffer.
202 /// use nihav_core::io::byteio::write_u24be;
203 /// # use nihav_core::io::byteio::ByteIOResult;
205 /// # fn foo() -> ByteIOResult<()> {
206 /// let mut dst = [0u8; 3];
207 /// write_u24be(&mut dst, 0x010203)?;
208 /// // dst should contain [ 1, 2, 3] now
212 #[allow(clippy::identity_op)]
213 pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> {
214 if dst.len() < 3 { return Err(ByteIOError::WriteError); }
215 dst[0] = (val >> 16) as u8;
216 dst[1] = (val >> 8) as u8;
217 dst[2] = (val >> 0) as u8;
220 /// Writes 24-bit little-endian integer to the provided buffer.
221 #[allow(clippy::identity_op)]
222 pub fn write_u24le(dst: &mut [u8], val: u32) -> ByteIOResult<()> {
223 if dst.len() < 3 { return Err(ByteIOError::WriteError); }
224 dst[0] = (val >> 0) as u8;
225 dst[1] = (val >> 8) as u8;
226 dst[2] = (val >> 16) as u8;
229 /// Writes 32-bit big-endian floating point number to the provided buffer.
230 pub fn write_f32be(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32be(dst, val.to_bits()) }
231 /// Writes 32-bit little-endian floating point number to the provided buffer.
232 pub fn write_f32le(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32le(dst, val.to_bits()) }
233 /// Writes 64-bit big-endian floating point number to the provided buffer.
234 pub fn write_f64be(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64be(dst, val.to_bits()) }
235 /// Writes 64-bit little-endian floating point number to the provided buffer.
236 pub fn write_f64le(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64le(dst, val.to_bits()) }
238 impl<'a> ByteReader<'a> {
239 /// Constructs a new instance of bytestream reader.
244 /// use nihav_core::io::byteio::{MemoryReader,ByteReader};
245 /// # use nihav_core::io::byteio::ByteIOResult;
247 /// # fn foo() -> ByteIOResult<()> {
248 /// let memory: [u8; 4] = [ 0, 42, 42, 0 ];
249 /// let mut mr = MemoryReader::new_read(&memory);
250 /// let mut br = ByteReader::new(&mut mr);
254 pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteReader { io } }
256 /// Reads data into provided buffer. Partial read is treated as success.
257 pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
258 self.io.read_buf(buf)
261 /// Reads data into provided buffer. Partial read is treated as success.
262 pub fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
263 self.io.read_buf_some(buf)
266 /// Reads data into provided buffer but does not advance read position.
267 pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
268 self.io.peek_buf(buf)
271 /// Reads single byte from the stream.
272 pub fn read_byte(&mut self) -> ByteIOResult<u8> {
276 /// Returns the next byte value in the stream without advancing read position.
277 pub fn peek_byte(&mut self) -> ByteIOResult<u8> {
281 /// Reads four-byte array from the stream.
282 pub fn read_tag(&mut self) -> ByteIOResult<[u8; 4]> {
283 let mut buf = [0u8; 4];
284 self.io.read_buf(&mut buf)?;
288 /// Reads four-byte array from the stream without advancing read position.
289 pub fn peek_tag(&mut self) -> ByteIOResult<[u8; 4]> {
290 let mut buf = [0u8; 4];
291 self.io.peek_buf(&mut buf)?;
295 /// Reads 16-bit big-endian integer from the stream.
296 pub fn read_u16be(&mut self) -> ByteIOResult<u16> {
297 read_int!(self, u16, 2, to_be)
300 /// Reads 16-bit big-endian integer from the stream without advancing read position.
301 pub fn peek_u16be(&mut self) -> ByteIOResult<u16> {
302 peek_int!(self, u16, 2, to_be)
305 /// Reads 24-bit big-endian integer from the stream.
306 pub fn read_u24be(&mut self) -> ByteIOResult<u32> {
307 let p16 = self.read_u16be()?;
308 let p8 = self.read_byte()?;
309 Ok((u32::from(p16) << 8) | u32::from(p8))
312 /// Reads 24-bit big-endian integer from the stream without advancing read position.
313 pub fn peek_u24be(&mut self) -> ByteIOResult<u32> {
314 let mut src: [u8; 3] = [0; 3];
315 self.peek_buf(&mut src)?;
316 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
319 /// Reads 32-bit big-endian integer from the stream.
320 pub fn read_u32be(&mut self) -> ByteIOResult<u32> {
321 read_int!(self, u32, 4, to_be)
324 /// Reads 32-bit big-endian integer from the stream without advancing read position.
325 pub fn peek_u32be(&mut self) -> ByteIOResult<u32> {
326 peek_int!(self, u32, 4, to_be)
329 /// Reads 64-bit big-endian integer from the stream.
330 pub fn read_u64be(&mut self) -> ByteIOResult<u64> {
331 read_int!(self, u64, 8, to_be)
334 /// Reads 64-bit big-endian integer from the stream without advancing read position.
335 pub fn peek_u64be(&mut self) -> ByteIOResult<u64> {
336 peek_int!(self, u64, 8, to_be)
339 /// Reads 32-bit big-endian floating point number from the stream.
340 pub fn read_f32be(&mut self) -> ByteIOResult<f32> {
341 Ok(f32::from_bits(self.read_u32be()?))
344 /// Reads 32-bit big-endian floating point number from the stream without advancing read position.
345 pub fn peek_f32be(&mut self) -> ByteIOResult<f32> {
346 Ok(f32::from_bits(self.peek_u32be()?))
349 /// Reads 64-bit big-endian floating point number from the stream.
350 pub fn read_f64be(&mut self) -> ByteIOResult<f64> {
351 Ok(f64::from_bits(self.read_u64be()?))
354 /// Reads 64-bit big-endian floating point number from the stream without advancing read position.
355 pub fn peek_f64be(&mut self) -> ByteIOResult<f64> {
356 Ok(f64::from_bits(self.peek_u64be()?))
359 /// Reads 16-bit little-endian integer from the stream.
360 pub fn read_u16le(&mut self) -> ByteIOResult<u16> {
361 read_int!(self, u16, 2, to_le)
364 /// Reads 16-bit little-endian integer from the stream without advancing read position.
365 pub fn peek_u16le(&mut self) -> ByteIOResult<u16> {
366 peek_int!(self, u16, 2, to_le)
369 /// Reads 24-bit little-endian integer from the stream.
370 pub fn read_u24le(&mut self) -> ByteIOResult<u32> {
371 let p8 = self.read_byte()?;
372 let p16 = self.read_u16le()?;
373 Ok((u32::from(p16) << 8) | u32::from(p8))
376 /// Reads 24-bit little-endian integer from the stream without advancing read position.
377 pub fn peek_u24le(&mut self) -> ByteIOResult<u32> {
378 let mut src: [u8; 3] = [0; 3];
379 self.peek_buf(&mut src)?;
380 Ok(u32::from(src[0]) | (u32::from(src[1]) << 8) | (u32::from(src[2]) << 16))
383 /// Reads 32-bit little-endian integer from the stream.
384 pub fn read_u32le(&mut self) -> ByteIOResult<u32> {
385 read_int!(self, u32, 4, to_le)
388 /// Reads 32-bit little-endian integer from the stream without advancing read position.
389 pub fn peek_u32le(&mut self) -> ByteIOResult<u32> {
390 peek_int!(self, u32, 4, to_le)
393 /// Reads 64-bit little-endian integer from the stream.
394 pub fn read_u64le(&mut self) -> ByteIOResult<u64> {
395 read_int!(self, u64, 8, to_le)
398 /// Reads 64-bit little-endian integer from the stream without advancing read position.
399 pub fn peek_u64le(&mut self) -> ByteIOResult<u64> {
400 peek_int!(self, u64, 8, to_le)
403 /// Reads 32-bit little-endian floating point number from the stream.
404 pub fn read_f32le(&mut self) -> ByteIOResult<f32> {
405 Ok(f32::from_bits(self.read_u32le()?))
408 /// Reads 32-bit little-endian floating point number from the stream without advancing read position.
409 pub fn peek_f32le(&mut self) -> ByteIOResult<f32> {
410 Ok(f32::from_bits(self.peek_u32le()?))
413 /// Reads 64-bit little-endian floating point number from the stream.
414 pub fn read_f64le(&mut self) -> ByteIOResult<f64> {
415 Ok(f64::from_bits(self.read_u64le()?))
418 /// Reads 64-bit little-endian floating point number from the stream without advancing read position.
419 pub fn peek_f64le(&mut self) -> ByteIOResult<f64> {
420 Ok(f64::from_bits(self.peek_u64le()?))
423 /// Skips requested number of bytes.
424 pub fn read_skip(&mut self, len: usize) -> ByteIOResult<()> {
425 if self.io.is_seekable() {
426 self.io.seek(SeekFrom::Current(len as i64))?;
429 let mut buf : [u8; 16] = [0; 16];
431 while ssize > bref.len() {
432 self.io.read_buf(bref)?;
436 self.io.read_byte()?;
443 /// Returns current read position.
444 pub fn tell(&mut self) -> u64 {
448 /// Seeks to the provided position.
449 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
453 /// Tells whether this is end of stream.
454 pub fn is_eof(&self) -> bool {
458 /// Returns stream size or -1 if it is not known.
459 pub fn size(&mut self) -> i64 {
463 /// Reports number of bytes left in the stream.
464 pub fn left(&mut self) -> i64 {
465 let size = self.io.size();
466 if size == -1 { return -1; }
467 size - (self.io.tell() as i64)
471 impl<'a> MemoryReader<'a> {
472 /// Constructs a new instance of `MemoryReader`.
473 pub fn new_read(buf: &'a [u8]) -> Self {
474 MemoryReader { buf, pos: 0 }
477 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
478 if pos < 0 || (pos as usize) > self.buf.len() {
479 return Err(ByteIOError::WrongRange);
481 self.pos = pos as usize;
486 impl<'a> ByteIO for MemoryReader<'a> {
487 fn read_byte(&mut self) -> ByteIOResult<u8> {
488 if self.is_eof() { return Err(ByteIOError::EOF); }
489 let res = self.buf[self.pos];
494 fn peek_byte(&mut self) -> ByteIOResult<u8> {
495 if self.is_eof() { return Err(ByteIOError::EOF); }
496 Ok(self.buf[self.pos])
499 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
500 let copy_size = if self.buf.len() - self.pos < buf.len() { self.buf.len() - self.pos } else { buf.len() };
501 if copy_size == 0 { return Err(ByteIOError::EOF); }
502 let dst = &mut buf[0..copy_size];
503 dst.copy_from_slice(&self.buf[self.pos..][..copy_size]);
507 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
508 let read_size = self.peek_buf(buf)?;
509 if read_size < buf.len() { return Err(ByteIOError::EOF); }
510 self.pos += read_size;
514 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
515 let read_size = self.peek_buf(buf)?;
516 self.pos += read_size;
520 #[allow(unused_variables)]
521 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
522 Err(ByteIOError::NotImplemented)
525 fn tell(&mut self) -> u64 {
529 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
530 let cur_pos = self.pos as i64;
531 let cur_size = self.buf.len() as i64;
533 SeekFrom::Start(x) => self.real_seek(x as i64),
534 SeekFrom::Current(x) => self.real_seek(cur_pos + x),
535 SeekFrom::End(x) => self.real_seek(cur_size + x),
539 fn is_eof(&self) -> bool {
540 self.pos >= self.buf.len()
543 fn is_seekable(&mut self) -> bool {
547 fn size(&mut self) -> i64 {
548 self.buf.len() as i64
551 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
554 impl<T: Read+Seek> FileReader<T> {
556 /// Constructs a new instance of `FileReader`.
557 pub fn new_read(file: T) -> Self {
558 FileReader { file: Box::new(file), eof : false }
560 /// Constructs a new instance of `FileReader` using a boxed resource.
561 pub fn new_read_boxed(file: Box<T>) -> Self {
562 FileReader { file, eof : false }
564 /// Destroys the reader and releases the reader resource for a further use.
565 pub fn finish(self) -> Box<T> { self.file }
568 impl<T: Read+Seek> ByteIO for FileReader<T> {
569 fn read_byte(&mut self) -> ByteIOResult<u8> {
570 let mut byte : [u8; 1] = [0];
571 let ret = self.file.read(&mut byte);
572 if ret.is_err() { return Err(ByteIOError::ReadError); }
573 let sz = ret.unwrap();
574 if sz == 0 { self.eof = true; return Err(ByteIOError::EOF); }
578 fn peek_byte(&mut self) -> ByteIOResult<u8> {
579 let b = self.read_byte()?;
580 self.seek(SeekFrom::Current(-1))?;
584 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
585 match self.file.read_exact(buf) {
586 Ok(()) => Ok(buf.len()),
588 if err.kind() == std::io::ErrorKind::UnexpectedEof {
590 Err(ByteIOError::EOF)
592 Err(ByteIOError::ReadError)
598 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
599 let ret = self.file.read(buf);
600 if ret.is_err() { return Err(ByteIOError::ReadError); }
601 let sz = ret.unwrap();
603 if let Err(_err) = self.file.read_exact(&mut buf[sz..][..1]) {
606 return Err(ByteIOError::EOF);
615 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
616 let size = self.read_buf(buf)?;
617 self.seek(SeekFrom::Current(-(size as i64)))?;
621 #[allow(unused_variables)]
622 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
623 Err(ByteIOError::NotImplemented)
626 fn tell(&mut self) -> u64 {
627 self.file.seek(SeekFrom::Current(0)).unwrap()
630 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
631 let res = self.file.seek(pos);
634 Err(_) => Err(ByteIOError::SeekError),
638 fn is_eof(&self) -> bool {
642 fn is_seekable(&mut self) -> bool {
646 fn size(&mut self) -> i64 {
650 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
654 impl<T: Read+Seek> BoundedFileReader<T> {
656 /// Constructs a new instance of `BoundedFileReader`. The reader pretends that data before `start` and after `end` (if set) does not exist.
657 pub fn new_read(file: T, start: u64, end: Option<u64>) -> ByteIOResult<Self> {
658 let mut file = Box::new(file);
659 if let Some(epos) = end {
661 return Err(ByteIOError::WrongRange);
664 if start > 0 && file.seek(SeekFrom::Start(start)).is_err() {
665 return Err(ByteIOError::SeekError);
667 Ok(Self { file, start, end, eof : false })
669 /// Constructs a new instance of `BoundedFileReader` using a boxed resource. The reader pretends that data before `start` and after `end` (if set) does not exist.
670 pub fn new_read_boxed(mut file: Box<T>, start: u64, end: Option<u64>) -> ByteIOResult<Self> {
671 if let Some(epos) = end {
673 return Err(ByteIOError::WrongRange);
676 if start > 0 && file.seek(SeekFrom::Start(start)).is_err() {
677 return Err(ByteIOError::SeekError);
679 Ok(Self { file, start, end, eof : false })
681 /// Destroys the reader and releases the reader resource for a further use.
682 pub fn finish(self) -> Box<T> { self.file }
683 fn real_tell(&mut self) -> u64 {
684 self.file.seek(SeekFrom::Current(0)).unwrap()
686 fn max_read_len(&mut self, len: usize) -> usize {
687 if let Some(epos) = self.end {
688 (len as u64).min(epos - self.real_tell()) as usize
695 impl<T: Read+Seek> ByteIO for BoundedFileReader<T> {
696 fn read_byte(&mut self) -> ByteIOResult<u8> {
697 if let Some(epos) = self.end {
698 if self.real_tell() >= epos {
700 return Err(ByteIOError::EOF);
703 let mut byte : [u8; 1] = [0];
704 let ret = self.file.read(&mut byte);
705 if ret.is_err() { return Err(ByteIOError::ReadError); }
706 let sz = ret.unwrap();
707 if sz == 0 { self.eof = true; return Err(ByteIOError::EOF); }
711 fn peek_byte(&mut self) -> ByteIOResult<u8> {
712 let b = self.read_byte()?;
713 if self.file.seek(SeekFrom::Current(-1)).is_err() {
714 return Err(ByteIOError::SeekError);
719 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
720 if let Some(epos) = self.end {
721 if self.real_tell() >= epos {
723 return Err(ByteIOError::EOF);
726 let len = self.max_read_len(buf.len());
727 match self.file.read_exact(&mut buf[..len]) {
728 Ok(()) if len == buf.len() => Ok(buf.len()),
731 Err(ByteIOError::EOF)
734 if err.kind() == std::io::ErrorKind::UnexpectedEof {
736 Err(ByteIOError::EOF)
738 Err(ByteIOError::ReadError)
744 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
745 if let Some(epos) = self.end {
746 if self.real_tell() >= epos {
748 return Err(ByteIOError::EOF);
751 let len = self.max_read_len(buf.len());
752 let ret = self.file.read(&mut buf[..len]);
753 if ret.is_err() { return Err(ByteIOError::ReadError); }
754 let sz = ret.unwrap();
756 if let Ok(1) = self.file.read(&mut buf[sz..][..1]) {
761 return Err(ByteIOError::EOF);
767 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
768 let len = self.max_read_len(buf.len());
769 let size = self.read_buf(&mut buf[..len])?;
770 if self.file.seek(SeekFrom::Current(-(size as i64))).is_err() {
771 return Err(ByteIOError::SeekError);
776 #[allow(unused_variables)]
777 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
778 Err(ByteIOError::NotImplemented)
781 fn tell(&mut self) -> u64 {
782 self.file.seek(SeekFrom::Current(0)).unwrap() - self.start
785 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
786 let res = match pos {
787 SeekFrom::Start(off) => {
788 let dpos = self.start + off;
789 if let Some(epos) = self.end {
791 return Err(ByteIOError::WrongRange);
794 self.file.seek(SeekFrom::Start(dpos))
796 SeekFrom::Current(off) => {
797 let dpos = (self.real_tell() as i64) + off;
798 let end = self.end.unwrap_or(dpos as u64);
799 if dpos < 0 || ((dpos as u64) < self.start) || ((dpos as u64) > end) {
800 return Err(ByteIOError::WrongRange);
804 SeekFrom::End(off) => {
805 if let Some(epos) = self.end {
806 let dpos = (epos as i64) + off;
807 if dpos < (self.start as i64) || ((dpos as u64) > epos) {
808 return Err(ByteIOError::WrongRange);
810 self.file.seek(SeekFrom::Start(dpos as u64))
818 Err(_) => Err(ByteIOError::SeekError),
822 fn is_eof(&self) -> bool {
826 fn is_seekable(&mut self) -> bool {
830 fn size(&mut self) -> i64 {
834 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
838 /// High-level bytestream writer.
840 /// User is supposed to create some writer implementing [`ByteIO`] trait e.g. [`MemoryWriter`] and use it to create `ByteWriter` which can be used for writing e.g. various integer types.
845 /// use nihav_core::io::byteio::{MemoryWriter,ByteWriter};
846 /// # use nihav_core::io::byteio::ByteIOResult;
848 /// # fn foo() -> ByteIOResult<()> {
849 /// let mut memory = [0u8; 4];
850 /// let mut mw = MemoryWriter::new_write(&mut memory);
851 /// let mut bw = ByteWriter::new(&mut mw);
852 /// let val = bw.write_u16be(42)?; // memory should be [ 0, 42, 0, 0 ]
853 /// let val = bw.write_u16le(42)?; // memory should be [ 0, 42, 42, 0 ]
858 /// [`ByteIO`]: ./trait.ByteIO.html
859 /// [`MemoryWriter`]: ./struct.MemoryWriter.html
861 pub struct ByteWriter<'a> {
862 io: &'a mut dyn ByteIO,
865 /// Bytestream writer to memory.
866 pub struct MemoryWriter<'a> {
871 /// Bytestream writer to anything implementing `std::io::Write` and `std::io::Seek`.
872 pub struct FileWriter<T: Write+Seek> {
876 /// Bytestream writer to memory.
878 /// Unlike [`MemoryWriter`] which writes to an array of fixed size, `GrowableMemoryWriter` grows output size when output size exceeds capacity.
880 /// [`MemoryWriter`]: ./struct.MemoryWriter.html
881 pub struct GrowableMemoryWriter<'a> {
882 buf: &'a mut Vec<u8>,
886 impl<'a> ByteWriter<'a> {
887 /// Constructs a new instance of `ByteWriter`.
888 pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteWriter { io } }
890 /// Writes byte array to the output.
891 pub fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
892 self.io.write_buf(buf)
895 /// Writes single byte to the output.
896 pub fn write_byte(&mut self, val: u8) -> ByteIOResult<()> {
897 let buf: [u8; 1] = [val];
898 self.io.write_buf(&buf)
901 /// Writes 16-bit big-endian integer to the output.
902 pub fn write_u16be(&mut self, val: u16) -> ByteIOResult<()> {
903 let buf: [u8; 2] = [((val >> 8) & 0xFF) as u8, (val & 0xFF) as u8];
904 self.io.write_buf(&buf)
907 /// Writes 16-bit little-endian integer to the output.
908 pub fn write_u16le(&mut self, val: u16) -> ByteIOResult<()> {
909 let buf: [u8; 2] = [(val & 0xFF) as u8, ((val >> 8) & 0xFF) as u8];
910 self.io.write_buf(&buf)
913 /// Writes 24-bit big-endian integer to the output.
914 pub fn write_u24be(&mut self, val: u32) -> ByteIOResult<()> {
915 let buf: [u8; 3] = [((val >> 16) & 0xFF) as u8, ((val >> 8) & 0xFF) as u8, (val & 0xFF) as u8];
919 /// Writes 24-bit little-endian integer to the output.
920 pub fn write_u24le(&mut self, val: u32) -> ByteIOResult<()> {
921 let buf: [u8; 3] = [(val & 0xFF) as u8, ((val >> 8) & 0xFF) as u8, ((val >> 16) & 0xFF) as u8];
925 /// Writes 32-bit big-endian integer to the output.
926 pub fn write_u32be(&mut self, val: u32) -> ByteIOResult<()> {
927 self.write_u16be(((val >> 16) & 0xFFFF) as u16)?;
928 self.write_u16be((val & 0xFFFF) as u16)
931 /// Writes 32-bit little-endian integer to the output.
932 pub fn write_u32le(&mut self, val: u32) -> ByteIOResult<()> {
933 self.write_u16le((val & 0xFFFF) as u16)?;
934 self.write_u16le(((val >> 16) & 0xFFFF) as u16)
937 /// Writes 64-bit big-endian integer to the output.
938 pub fn write_u64be(&mut self, val: u64) -> ByteIOResult<()> {
939 self.write_u32be((val >> 32) as u32)?;
940 self.write_u32be(val as u32)
943 /// Writes 64-bit little-endian integer to the output.
944 pub fn write_u64le(&mut self, val: u64) -> ByteIOResult<()> {
945 self.write_u32le(val as u32)?;
946 self.write_u32le((val >> 32) as u32)
949 /// Writes 32-bit big-endian floating point number to the output.
950 pub fn write_f32be(&mut self, val: f32) -> ByteIOResult<()> {
951 self.write_u32be(val.to_bits())
954 /// Writes 32-bit little-endian floating point number to the output.
955 pub fn write_f32le(&mut self, val: f32) -> ByteIOResult<()> {
956 self.write_u32le(val.to_bits())
959 /// Writes 64-bit big-endian floating point number to the output.
960 pub fn write_f64be(&mut self, val: f64) -> ByteIOResult<()> {
961 self.write_u64be(val.to_bits())
964 /// Writes 64-bit little-endian floating point number to the output.
965 pub fn write_f64le(&mut self, val: f64) -> ByteIOResult<()> {
966 self.write_u64le(val.to_bits())
969 /// Reports the current write position.
970 pub fn tell(&mut self) -> u64 {
974 /// Seeks to the requested position.
975 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
979 /// Reports the amount of bytes the writer can still write (-1 if unknown).
980 pub fn size_left(&mut self) -> i64 {
981 let sz = self.io.size();
982 if sz == -1 { return -1; }
983 sz - (self.tell() as i64)
986 /// Flushes output stream if possible.
987 pub fn flush(&mut self) -> ByteIOResult<()> {
992 impl<'a> MemoryWriter<'a> {
994 /// Constructs a new instance of `MemoryWriter`.
995 pub fn new_write(buf: &'a mut [u8]) -> Self {
996 MemoryWriter { buf, pos: 0 }
999 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
1000 if pos < 0 || (pos as usize) > self.buf.len() {
1001 return Err(ByteIOError::WrongRange)
1003 self.pos = pos as usize;
1008 impl<'a> ByteIO for MemoryWriter<'a> {
1009 #[allow(unused_variables)]
1010 fn read_byte(&mut self) -> ByteIOResult<u8> {
1011 Err(ByteIOError::NotImplemented)
1014 #[allow(unused_variables)]
1015 fn peek_byte(&mut self) -> ByteIOResult<u8> {
1016 Err(ByteIOError::NotImplemented)
1019 #[allow(unused_variables)]
1020 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1021 Err(ByteIOError::NotImplemented)
1024 #[allow(unused_variables)]
1025 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1026 Err(ByteIOError::NotImplemented)
1029 #[allow(unused_variables)]
1030 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1031 Err(ByteIOError::NotImplemented)
1034 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
1035 if self.pos + buf.len() > self.buf.len() { return Err(ByteIOError::WriteError); }
1036 self.buf[self.pos..][..buf.len()].copy_from_slice(buf);
1037 self.pos += buf.len();
1041 fn tell(&mut self) -> u64 {
1045 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
1046 let cur_pos = self.pos as i64;
1047 let cur_size = self.buf.len() as i64;
1049 SeekFrom::Start(x) => self.real_seek(x as i64),
1050 SeekFrom::Current(x) => self.real_seek(cur_pos + x),
1051 SeekFrom::End(x) => self.real_seek(cur_size + x),
1055 fn is_eof(&self) -> bool {
1056 self.pos >= self.buf.len()
1059 fn is_seekable(&mut self) -> bool {
1063 fn size(&mut self) -> i64 {
1064 self.buf.len() as i64
1067 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
1070 impl<'a> GrowableMemoryWriter<'a> {
1072 /// Constructs a new instance of `GrowableMemoryWriter`.
1073 pub fn new_write(buf: &'a mut Vec<u8>) -> Self {
1074 GrowableMemoryWriter { buf, pos: 0 }
1077 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
1078 if pos < 0 || (pos as usize) > self.buf.len() {
1079 return Err(ByteIOError::WrongRange)
1081 self.pos = pos as usize;
1086 impl<'a> ByteIO for GrowableMemoryWriter<'a> {
1087 #[allow(unused_variables)]
1088 fn read_byte(&mut self) -> ByteIOResult<u8> {
1089 Err(ByteIOError::NotImplemented)
1092 #[allow(unused_variables)]
1093 fn peek_byte(&mut self) -> ByteIOResult<u8> {
1094 Err(ByteIOError::NotImplemented)
1097 #[allow(unused_variables)]
1098 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1099 Err(ByteIOError::NotImplemented)
1102 #[allow(unused_variables)]
1103 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1104 Err(ByteIOError::NotImplemented)
1107 #[allow(unused_variables)]
1108 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1109 Err(ByteIOError::NotImplemented)
1112 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
1113 if self.pos + buf.len() > self.buf.len() {
1114 self.buf.resize(self.pos + buf.len(), 0);
1116 self.buf[self.pos..][..buf.len()].copy_from_slice(buf);
1117 self.pos += buf.len();
1121 fn tell(&mut self) -> u64 {
1125 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
1126 let cur_pos = self.pos as i64;
1127 let cur_size = self.buf.len() as i64;
1129 SeekFrom::Start(x) => self.real_seek(x as i64),
1130 SeekFrom::Current(x) => self.real_seek(cur_pos + x),
1131 SeekFrom::End(x) => self.real_seek(cur_size + x),
1135 fn is_eof(&self) -> bool {
1136 self.pos >= self.buf.len()
1139 fn is_seekable(&mut self) -> bool {
1143 fn size(&mut self) -> i64 {
1144 self.buf.len() as i64
1147 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
1150 impl<T: Write+Seek> FileWriter<T> {
1151 /// Constructs a new instance of `FileWriter`.
1152 pub fn new_write(file: T) -> Self {
1153 FileWriter { file: Box::new(file) }
1157 impl<T: Write+Seek> ByteIO for FileWriter<T> {
1158 #[allow(unused_variables)]
1159 fn read_byte(&mut self) -> ByteIOResult<u8> {
1160 Err(ByteIOError::NotImplemented)
1163 #[allow(unused_variables)]
1164 fn peek_byte(&mut self) -> ByteIOResult<u8> {
1165 Err(ByteIOError::NotImplemented)
1168 #[allow(unused_variables)]
1169 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1170 Err(ByteIOError::NotImplemented)
1173 #[allow(unused_variables)]
1174 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1175 Err(ByteIOError::NotImplemented)
1178 #[allow(unused_variables)]
1179 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
1180 Err(ByteIOError::NotImplemented)
1183 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
1184 match self.file.write_all(buf) {
1186 Err(_) => Err(ByteIOError::WriteError),
1190 fn tell(&mut self) -> u64 {
1191 self.file.seek(SeekFrom::Current(0)).unwrap()
1194 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
1195 let res = self.file.seek(pos);
1198 Err(_) => Err(ByteIOError::SeekError),
1202 fn is_eof(&self) -> bool {
1206 fn is_seekable(&mut self) -> bool {
1210 fn size(&mut self) -> i64 {
1214 fn flush(&mut self) -> ByteIOResult<()> {
1215 match self.file.flush() {
1217 Err(_) => Err(ByteIOError::WriteError),
1229 //const DATA : &'static [u8] = include_bytes!("../../assets/file");
1230 let buf: [u8; 64] = [1; 64];
1231 let mut mr = MemoryReader::new_read(&buf);
1232 let mut reader = ByteReader::new(&mut mr);
1233 assert_eq!(reader.read_byte().unwrap(), 0x01u8);
1234 assert_eq!(reader.read_u16le().unwrap(), 0x0101u16);
1235 assert_eq!(reader.read_u24le().unwrap(), 0x010101u32);
1236 assert_eq!(reader.read_u32le().unwrap(), 0x01010101u32);
1237 assert_eq!(reader.read_u64le().unwrap(), 0x0101010101010101u64);
1238 let mut file = File::open("assets/Misc/MaoMacha.asx").unwrap();
1239 let mut fr = FileReader::new_read(&mut file);
1240 let mut br2 = ByteReader::new(&mut fr);
1241 assert_eq!(br2.read_byte().unwrap(), 0x30);
1242 assert_eq!(br2.read_u24be().unwrap(), 0x26B275);
1243 assert_eq!(br2.read_u24le().unwrap(), 0xCF668E);
1244 assert_eq!(br2.read_u32be().unwrap(), 0x11A6D900);
1245 assert_eq!(br2.read_u32le().unwrap(), 0xCE6200AA);
1249 let mut buf: [u8; 64] = [0; 64];
1251 let mut mw = MemoryWriter::new_write(&mut buf);
1252 let mut bw = ByteWriter::new(&mut mw);
1253 bw.write_byte(0x00).unwrap();
1254 bw.write_u16be(0x0102).unwrap();
1255 bw.write_u24be(0x030405).unwrap();
1256 bw.write_u32be(0x06070809).unwrap();
1257 bw.write_u64be(0x0A0B0C0D0E0F1011).unwrap();
1258 bw.write_byte(0x00).unwrap();
1259 bw.write_u16le(0x0201).unwrap();
1260 bw.write_u24le(0x050403).unwrap();
1261 bw.write_u32le(0x09080706).unwrap();
1262 bw.write_u64le(0x11100F0E0D0C0B0A).unwrap();
1263 assert_eq!(bw.size_left(), 28);
1266 assert_eq!(buf[(i + 0x00) as usize], i);
1267 assert_eq!(buf[(i + 0x12) as usize], i);