]> git.nihav.org Git - nihav.git/blame - nihav-core/src/io/byteio.rs
mov: fix track duration rescaling
[nihav.git] / nihav-core / src / io / byteio.rs
CommitLineData
28884194 1//! Bytestream reading/writing functionality.
37f2396b 2pub use std::io::SeekFrom;
90aa4e6b 3use std::io::prelude::*;
e243ceb4 4use std::ptr;
90aa4e6b 5
28884194 6/// A list specifying general bytestream reading and writing errors.
90aa4e6b
KS
7#[derive(Debug)]
8pub enum ByteIOError {
28884194 9 /// End of stream.
90aa4e6b 10 EOF,
28884194 11 /// Wrong seek position was provided.
90aa4e6b 12 WrongRange,
28884194 13 /// Tried to call read() on bytestream writer or write() on bytestream reader.
90aa4e6b 14 WrongIOMode,
28884194 15 /// Functionality is not implemented.
90aa4e6b 16 NotImplemented,
28884194 17 /// Read error.
90aa4e6b 18 ReadError,
28884194 19 /// Write error.
90aa4e6b 20 WriteError,
28884194 21 /// Seeking failed.
90aa4e6b
KS
22 SeekError,
23}
24
28884194 25/// A specialised `Result` type for bytestream operations.
296d86b2 26pub type ByteIOResult<T> = Result<T, ByteIOError>;
90aa4e6b 27
28884194 28/// Common trait for bytestream operations.
90aa4e6b 29pub trait ByteIO {
28884194 30 /// Reads data into provided buffer. Fails if it cannot fill whole buffer.
90aa4e6b 31 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
28884194 32 /// Reads data into provided buffer. Partial read is treated as success.
89de616c 33 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
28884194 34 /// Reads data into provided buffer but does not advance read position.
794364cf 35 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize>;
28884194 36 /// Reads single byte from the stream.
90aa4e6b 37 fn read_byte(&mut self) -> ByteIOResult<u8>;
28884194 38 /// Returns the next byte value in the stream without advancing read position.
794364cf 39 fn peek_byte(&mut self) -> ByteIOResult<u8>;
28884194 40 /// Writes buffer to the stream.
d532000c 41 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()>;
28884194 42 /// Returns current read or write position.
90aa4e6b 43 fn tell(&mut self) -> u64;
28884194 44 /// Seeks to the provided position.
90aa4e6b 45 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64>;
28884194 46 /// Tells whether this is end of stream.
e243ceb4 47 fn is_eof(&self) -> bool;
28884194 48 /// Reports whether stream is seekable or not.
90aa4e6b 49 fn is_seekable(&mut self) -> bool;
28884194 50 /// Returns stream size or -1 if it is not known.
d532000c 51 fn size(&mut self) -> i64;
cf095d56
KS
52 /// Flushes output if possible.
53 fn flush(&mut self) -> ByteIOResult<()>;
90aa4e6b
KS
54}
55
28884194
KS
56/// High-level bytestream reader.
57///
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.
59///
60/// # Examples
61///
62/// ````
63/// use nihav_core::io::byteio::{MemoryReader,ByteReader};
64/// # use nihav_core::io::byteio::ByteIOResult;
65///
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
72/// # Ok(())
73/// # }
74/// ````
75///
76/// [`ByteIO`]: ./trait.ByteIO.html
77/// [`MemoryReader`]: ./struct.MemoryReader.html
90aa4e6b
KS
78#[allow(dead_code)]
79pub struct ByteReader<'a> {
ac818eac 80 io: &'a mut dyn ByteIO,
90aa4e6b
KS
81}
82
28884194 83/// Bytestream reader from memory.
90aa4e6b
KS
84pub struct MemoryReader<'a> {
85 buf: &'a [u8],
90aa4e6b 86 pos: usize,
90aa4e6b
KS
87}
88
789354a8
KS
89/// Bytestream reader from anything implementing `std::io::Read` and `std::io::Seek`.
90pub struct FileReader<T: Read+Seek> {
91 file: Box<T>,
90aa4e6b
KS
92 eof: bool,
93}
94
95macro_rules! read_int {
96 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
90aa4e6b 97 unsafe {
e243ceb4
KS
98 let mut buf: $inttype = 0;
99 $s.read_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
100 Ok(buf.$which())
90aa4e6b
KS
101 }
102 })
103}
104
794364cf
KS
105macro_rules! peek_int {
106 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
794364cf 107 unsafe {
e243ceb4
KS
108 let mut buf: $inttype = 0;
109 $s.peek_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
110 Ok(buf.$which())
794364cf
KS
111 }
112 })
113}
114
95058bc1
KS
115macro_rules! read_int_func {
116 ($s: ident, $inttype: ty, $size: expr, $which: ident) => {
28884194 117/// Reads integer of certain size and endianness.
95058bc1
KS
118 pub fn $s(src: &[u8]) -> ByteIOResult<$inttype> {
119 if src.len() < $size { return Err(ByteIOError::ReadError); }
120 unsafe {
e243ceb4 121 let mut buf: $inttype = 0;
c9087a4c 122 ptr::copy_nonoverlapping(src.as_ptr(), &mut buf as *mut $inttype as *mut u8, std::mem::size_of::<$inttype>());
e243ceb4 123 Ok(buf.$which())
95058bc1
KS
124 }
125 }
126 }
127}
128
129read_int_func!(read_u16be, u16, 2, to_be);
130read_int_func!(read_u16le, u16, 2, to_le);
131read_int_func!(read_u32be, u32, 4, to_be);
132read_int_func!(read_u32le, u32, 4, to_le);
133read_int_func!(read_u64be, u64, 8, to_be);
134read_int_func!(read_u64le, u64, 8, to_le);
135
28884194
KS
136/// Reads 24-bit big-endian integer.
137///
138/// # Example
139///
140/// ````
141/// use nihav_core::io::byteio::read_u24be;
142/// # use nihav_core::io::byteio::ByteIOResult;
143///
144/// # fn foo() -> ByteIOResult<()> {
145/// let src: [u8; 3] = [ 1, 2, 3];
146/// let value = read_u24be(&src)?; // should return 0x010203
147/// # Ok(())
148/// # }
149/// ````
95058bc1
KS
150pub fn read_u24be(src: &[u8]) -> ByteIOResult<u32> {
151 if src.len() < 3 { return Err(ByteIOError::ReadError); }
e243ceb4 152 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
95058bc1 153}
28884194 154/// Reads 24-bit little-endian integer.
95058bc1
KS
155pub fn read_u24le(src: &[u8]) -> ByteIOResult<u32> {
156 if src.len() < 3 { return Err(ByteIOError::ReadError); }
e243ceb4 157 Ok((u32::from(src[2]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[0]))
95058bc1 158}
28884194 159/// Reads 32-bit big-endian floating point number.
02a5bdaf 160pub fn read_f32be(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32be(src)?)) }
28884194 161/// Reads 32-bit little-endian floating point number.
02a5bdaf 162pub fn read_f32le(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32le(src)?)) }
28884194 163/// Reads 64-bit big-endian floating point number.
02a5bdaf 164pub fn read_f64be(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64be(src)?)) }
28884194 165/// Reads 64-bit little-endian floating point number.
02a5bdaf 166pub fn read_f64le(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64le(src)?)) }
95058bc1 167
95c7f2f6
KS
168macro_rules! write_int_func {
169 ($s: ident, $inttype: ty, $size: expr, $which: ident) => {
28884194 170/// Writes integer of certain size and endianness into byte buffer.
95c7f2f6
KS
171 pub fn $s(dst: &mut [u8], val: $inttype) -> ByteIOResult<()> {
172 if dst.len() < $size { return Err(ByteIOError::WriteError); }
173 unsafe {
174 let val = val.$which();
175 ptr::copy_nonoverlapping(&val as *const $inttype as *const u8, dst.as_mut_ptr(), std::mem::size_of::<$inttype>());
176 }
177 Ok(())
178 }
179 }
180}
181
182write_int_func!(write_u16be, u16, 2, to_be);
183write_int_func!(write_u16le, u16, 2, to_le);
184write_int_func!(write_u32be, u32, 4, to_be);
185write_int_func!(write_u32le, u32, 4, to_le);
186write_int_func!(write_u64be, u64, 8, to_be);
187write_int_func!(write_u64le, u64, 8, to_le);
188
28884194
KS
189/// Writes 24-bit big-endian integer to the provided buffer.
190///
191/// # Example
192///
193/// ````
194/// use nihav_core::io::byteio::write_u24be;
195/// # use nihav_core::io::byteio::ByteIOResult;
196///
197/// # fn foo() -> ByteIOResult<()> {
198/// let mut dst = [0u8; 3];
199/// write_u24be(&mut dst, 0x010203)?;
200/// // dst should contain [ 1, 2, 3] now
201/// # Ok(())
202/// # }
203/// ````
b36f412c 204#[allow(clippy::identity_op)]
95c7f2f6
KS
205pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> {
206 if dst.len() < 3 { return Err(ByteIOError::WriteError); }
207 dst[0] = (val >> 16) as u8;
208 dst[1] = (val >> 8) as u8;
209 dst[2] = (val >> 0) as u8;
210 Ok(())
211}
28884194 212/// Writes 24-bit little-endian integer to the provided buffer.
b36f412c 213#[allow(clippy::identity_op)]
95c7f2f6
KS
214pub fn write_u24le(dst: &mut [u8], val: u32) -> ByteIOResult<()> {
215 if dst.len() < 3 { return Err(ByteIOError::WriteError); }
216 dst[0] = (val >> 0) as u8;
217 dst[1] = (val >> 8) as u8;
218 dst[2] = (val >> 16) as u8;
219 Ok(())
220}
28884194 221/// Writes 32-bit big-endian floating point number to the provided buffer.
02a5bdaf 222pub fn write_f32be(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32be(dst, val.to_bits()) }
28884194 223/// Writes 32-bit little-endian floating point number to the provided buffer.
02a5bdaf 224pub fn write_f32le(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32le(dst, val.to_bits()) }
28884194 225/// Writes 64-bit big-endian floating point number to the provided buffer.
02a5bdaf 226pub fn write_f64be(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64be(dst, val.to_bits()) }
28884194 227/// Writes 64-bit little-endian floating point number to the provided buffer.
02a5bdaf 228pub fn write_f64le(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64le(dst, val.to_bits()) }
95c7f2f6 229
90aa4e6b 230impl<'a> ByteReader<'a> {
28884194
KS
231 /// Constructs a new instance of bytestream reader.
232 ///
233 /// # Examples
234 ///
235 /// ````
236 /// use nihav_core::io::byteio::{MemoryReader,ByteReader};
237 /// # use nihav_core::io::byteio::ByteIOResult;
238 ///
239 /// # fn foo() -> ByteIOResult<()> {
240 /// let memory: [u8; 4] = [ 0, 42, 42, 0 ];
241 /// let mut mr = MemoryReader::new_read(&memory);
242 /// let mut br = ByteReader::new(&mut mr);
243 /// # Ok(())
244 /// # }
245 /// ````
ac818eac 246 pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteReader { io } }
90aa4e6b 247
28884194 248 /// Reads data into provided buffer. Partial read is treated as success.
90aa4e6b
KS
249 pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
250 self.io.read_buf(buf)
251 }
252
28884194 253 /// Reads data into provided buffer. Partial read is treated as success.
89de616c
KS
254 pub fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
255 self.io.read_buf_some(buf)
256 }
257
28884194 258 /// Reads data into provided buffer but does not advance read position.
794364cf
KS
259 pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
260 self.io.peek_buf(buf)
261 }
262
28884194 263 /// Reads single byte from the stream.
90aa4e6b
KS
264 pub fn read_byte(&mut self) -> ByteIOResult<u8> {
265 self.io.read_byte()
266 }
267
28884194 268 /// Returns the next byte value in the stream without advancing read position.
794364cf
KS
269 pub fn peek_byte(&mut self) -> ByteIOResult<u8> {
270 self.io.peek_byte()
271 }
272
943d7b14
KS
273 /// Reads four-byte array from the stream.
274 pub fn read_tag(&mut self) -> ByteIOResult<[u8; 4]> {
275 let mut buf = [0u8; 4];
276 self.io.read_buf(&mut buf)?;
277 Ok(buf)
278 }
279
280 /// Reads four-byte array from the stream without advancing read position.
281 pub fn peek_tag(&mut self) -> ByteIOResult<[u8; 4]> {
282 let mut buf = [0u8; 4];
283 self.io.peek_buf(&mut buf)?;
284 Ok(buf)
285 }
286
28884194 287 /// Reads 16-bit big-endian integer from the stream.
90aa4e6b
KS
288 pub fn read_u16be(&mut self) -> ByteIOResult<u16> {
289 read_int!(self, u16, 2, to_be)
290 }
291
28884194 292 /// Reads 16-bit big-endian integer from the stream without advancing read position.
794364cf
KS
293 pub fn peek_u16be(&mut self) -> ByteIOResult<u16> {
294 peek_int!(self, u16, 2, to_be)
295 }
296
28884194 297 /// Reads 24-bit big-endian integer from the stream.
90aa4e6b 298 pub fn read_u24be(&mut self) -> ByteIOResult<u32> {
794364cf
KS
299 let p16 = self.read_u16be()?;
300 let p8 = self.read_byte()?;
e243ceb4 301 Ok((u32::from(p16) << 8) | u32::from(p8))
794364cf
KS
302 }
303
28884194 304 /// Reads 24-bit big-endian integer from the stream without advancing read position.
794364cf
KS
305 pub fn peek_u24be(&mut self) -> ByteIOResult<u32> {
306 let mut src: [u8; 3] = [0; 3];
307 self.peek_buf(&mut src)?;
e243ceb4 308 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
90aa4e6b
KS
309 }
310
28884194 311 /// Reads 32-bit big-endian integer from the stream.
90aa4e6b
KS
312 pub fn read_u32be(&mut self) -> ByteIOResult<u32> {
313 read_int!(self, u32, 4, to_be)
314 }
315
28884194 316 /// Reads 32-bit big-endian integer from the stream without advancing read position.
794364cf
KS
317 pub fn peek_u32be(&mut self) -> ByteIOResult<u32> {
318 peek_int!(self, u32, 4, to_be)
319 }
320
28884194 321 /// Reads 64-bit big-endian integer from the stream.
90aa4e6b
KS
322 pub fn read_u64be(&mut self) -> ByteIOResult<u64> {
323 read_int!(self, u64, 8, to_be)
324 }
325
28884194 326 /// Reads 64-bit big-endian integer from the stream without advancing read position.
794364cf
KS
327 pub fn peek_u64be(&mut self) -> ByteIOResult<u64> {
328 peek_int!(self, u64, 8, to_be)
329 }
330
28884194 331 /// Reads 32-bit big-endian floating point number from the stream.
02a5bdaf
KS
332 pub fn read_f32be(&mut self) -> ByteIOResult<f32> {
333 Ok(f32::from_bits(self.read_u32be()?))
334 }
335
28884194 336 /// Reads 32-bit big-endian floating point number from the stream without advancing read position.
02a5bdaf
KS
337 pub fn peek_f32be(&mut self) -> ByteIOResult<f32> {
338 Ok(f32::from_bits(self.peek_u32be()?))
339 }
340
28884194 341 /// Reads 64-bit big-endian floating point number from the stream.
02a5bdaf
KS
342 pub fn read_f64be(&mut self) -> ByteIOResult<f64> {
343 Ok(f64::from_bits(self.read_u64be()?))
344 }
345
28884194 346 /// Reads 64-bit big-endian floating point number from the stream without advancing read position.
02a5bdaf
KS
347 pub fn peek_f64be(&mut self) -> ByteIOResult<f64> {
348 Ok(f64::from_bits(self.peek_u64be()?))
349 }
350
28884194 351 /// Reads 16-bit little-endian integer from the stream.
90aa4e6b
KS
352 pub fn read_u16le(&mut self) -> ByteIOResult<u16> {
353 read_int!(self, u16, 2, to_le)
354 }
355
28884194 356 /// Reads 16-bit little-endian integer from the stream without advancing read position.
794364cf
KS
357 pub fn peek_u16le(&mut self) -> ByteIOResult<u16> {
358 peek_int!(self, u16, 2, to_le)
359 }
360
28884194 361 /// Reads 24-bit little-endian integer from the stream.
90aa4e6b 362 pub fn read_u24le(&mut self) -> ByteIOResult<u32> {
794364cf
KS
363 let p8 = self.read_byte()?;
364 let p16 = self.read_u16le()?;
e243ceb4 365 Ok((u32::from(p16) << 8) | u32::from(p8))
794364cf
KS
366 }
367
28884194 368 /// Reads 24-bit little-endian integer from the stream without advancing read position.
794364cf
KS
369 pub fn peek_u24le(&mut self) -> ByteIOResult<u32> {
370 let mut src: [u8; 3] = [0; 3];
371 self.peek_buf(&mut src)?;
e243ceb4 372 Ok(u32::from(src[0]) | (u32::from(src[1]) << 8) | (u32::from(src[2]) << 16))
90aa4e6b
KS
373 }
374
28884194 375 /// Reads 32-bit little-endian integer from the stream.
90aa4e6b
KS
376 pub fn read_u32le(&mut self) -> ByteIOResult<u32> {
377 read_int!(self, u32, 4, to_le)
378 }
379
28884194 380 /// Reads 32-bit little-endian integer from the stream without advancing read position.
794364cf
KS
381 pub fn peek_u32le(&mut self) -> ByteIOResult<u32> {
382 peek_int!(self, u32, 4, to_le)
383 }
384
28884194 385 /// Reads 64-bit little-endian integer from the stream.
90aa4e6b
KS
386 pub fn read_u64le(&mut self) -> ByteIOResult<u64> {
387 read_int!(self, u64, 8, to_le)
388 }
389
28884194 390 /// Reads 64-bit little-endian integer from the stream without advancing read position.
794364cf
KS
391 pub fn peek_u64le(&mut self) -> ByteIOResult<u64> {
392 peek_int!(self, u64, 8, to_le)
393 }
394
28884194 395 /// Reads 32-bit little-endian floating point number from the stream.
02a5bdaf
KS
396 pub fn read_f32le(&mut self) -> ByteIOResult<f32> {
397 Ok(f32::from_bits(self.read_u32le()?))
398 }
399
28884194 400 /// Reads 32-bit little-endian floating point number from the stream without advancing read position.
02a5bdaf
KS
401 pub fn peek_f32le(&mut self) -> ByteIOResult<f32> {
402 Ok(f32::from_bits(self.peek_u32le()?))
403 }
404
28884194 405 /// Reads 64-bit little-endian floating point number from the stream.
02a5bdaf
KS
406 pub fn read_f64le(&mut self) -> ByteIOResult<f64> {
407 Ok(f64::from_bits(self.read_u64le()?))
408 }
409
28884194 410 /// Reads 64-bit little-endian floating point number from the stream without advancing read position.
02a5bdaf
KS
411 pub fn peek_f64le(&mut self) -> ByteIOResult<f64> {
412 Ok(f64::from_bits(self.peek_u64le()?))
413 }
414
28884194 415 /// Skips requested number of bytes.
6b167b0c 416 pub fn read_skip(&mut self, len: usize) -> ByteIOResult<()> {
90aa4e6b
KS
417 if self.io.is_seekable() {
418 self.io.seek(SeekFrom::Current(len as i64))?;
419 } else {
420 let mut ssize = len;
421 let mut buf : [u8; 16] = [0; 16];
9037cf6b 422 let bref = &mut buf;
90aa4e6b
KS
423 while ssize > bref.len() {
424 self.io.read_buf(bref)?;
425 ssize -= bref.len();
426 }
427 while ssize > 0 {
428 self.io.read_byte()?;
e243ceb4 429 ssize -= 1;
90aa4e6b
KS
430 }
431 }
6b167b0c 432 Ok(())
90aa4e6b
KS
433 }
434
28884194 435 /// Returns current read position.
90aa4e6b
KS
436 pub fn tell(&mut self) -> u64 {
437 self.io.tell()
438 }
439
28884194 440 /// Seeks to the provided position.
90aa4e6b
KS
441 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
442 self.io.seek(pos)
443 }
444
28884194 445 /// Tells whether this is end of stream.
e243ceb4 446 pub fn is_eof(&self) -> bool {
90aa4e6b
KS
447 self.io.is_eof()
448 }
96c6be97 449
28884194 450 /// Returns stream size or -1 if it is not known.
96c6be97
KS
451 pub fn size(&mut self) -> i64 {
452 self.io.size()
453 }
454
28884194 455 /// Reports number of bytes left in the stream.
96c6be97
KS
456 pub fn left(&mut self) -> i64 {
457 let size = self.io.size();
458 if size == -1 { return -1; }
e243ceb4 459 size - (self.io.tell() as i64)
96c6be97 460 }
90aa4e6b
KS
461}
462
463impl<'a> MemoryReader<'a> {
28884194 464 /// Constructs a new instance of `MemoryReader`.
90aa4e6b 465 pub fn new_read(buf: &'a [u8]) -> Self {
1be85e00 466 MemoryReader { buf, pos: 0 }
90aa4e6b
KS
467 }
468
469 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
1be85e00 470 if pos < 0 || (pos as usize) > self.buf.len() {
e243ceb4 471 return Err(ByteIOError::WrongRange);
90aa4e6b
KS
472 }
473 self.pos = pos as usize;
474 Ok(pos as u64)
475 }
90aa4e6b
KS
476}
477
478impl<'a> ByteIO for MemoryReader<'a> {
479 fn read_byte(&mut self) -> ByteIOResult<u8> {
90aa4e6b
KS
480 if self.is_eof() { return Err(ByteIOError::EOF); }
481 let res = self.buf[self.pos];
e243ceb4 482 self.pos += 1;
90aa4e6b
KS
483 Ok(res)
484 }
485
794364cf
KS
486 fn peek_byte(&mut self) -> ByteIOResult<u8> {
487 if self.is_eof() { return Err(ByteIOError::EOF); }
488 Ok(self.buf[self.pos])
489 }
490
491 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
b2939bd8 492 let copy_size = if self.buf.len() - self.pos < buf.len() { self.buf.len() - self.pos } else { buf.len() };
90aa4e6b 493 if copy_size == 0 { return Err(ByteIOError::EOF); }
e243ceb4
KS
494 let dst = &mut buf[0..copy_size];
495 dst.copy_from_slice(&self.buf[self.pos..][..copy_size]);
90aa4e6b
KS
496 Ok(copy_size)
497 }
498
794364cf 499 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
89de616c
KS
500 let read_size = self.peek_buf(buf)?;
501 if read_size < buf.len() { return Err(ByteIOError::EOF); }
502 self.pos += read_size;
503 Ok(read_size)
504 }
505
506 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
794364cf
KS
507 let read_size = self.peek_buf(buf)?;
508 self.pos += read_size;
509 Ok(read_size)
510 }
511
90aa4e6b 512 #[allow(unused_variables)]
d532000c 513 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
90aa4e6b
KS
514 Err(ByteIOError::NotImplemented)
515 }
516
517 fn tell(&mut self) -> u64 {
518 self.pos as u64
519 }
520
521 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
1be85e00
KS
522 let cur_pos = self.pos as i64;
523 let cur_size = self.buf.len() as i64;
90aa4e6b
KS
524 match pos {
525 SeekFrom::Start(x) => self.real_seek(x as i64),
526 SeekFrom::Current(x) => self.real_seek(cur_pos + x),
527 SeekFrom::End(x) => self.real_seek(cur_size + x),
528 }
529 }
530
e243ceb4 531 fn is_eof(&self) -> bool {
1be85e00 532 self.pos >= self.buf.len()
90aa4e6b
KS
533 }
534
535 fn is_seekable(&mut self) -> bool {
536 true
537 }
d532000c
KS
538
539 fn size(&mut self) -> i64 {
540 self.buf.len() as i64
541 }
cf095d56
KS
542
543 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
90aa4e6b
KS
544}
545
789354a8 546impl<T: Read+Seek> FileReader<T> {
90aa4e6b 547
28884194 548 /// Constructs a new instance of `FileReader`.
789354a8
KS
549 pub fn new_read(file: T) -> Self {
550 FileReader { file: Box::new(file), eof : false }
90aa4e6b
KS
551 }
552}
553
789354a8 554impl<T: Read+Seek> ByteIO for FileReader<T> {
90aa4e6b
KS
555 fn read_byte(&mut self) -> ByteIOResult<u8> {
556 let mut byte : [u8; 1] = [0];
e243ceb4
KS
557 let ret = self.file.read(&mut byte);
558 if ret.is_err() { return Err(ByteIOError::ReadError); }
559 let sz = ret.unwrap();
90aa4e6b
KS
560 if sz == 0 { self.eof = true; return Err(ByteIOError::EOF); }
561 Ok (byte[0])
562 }
563
794364cf
KS
564 fn peek_byte(&mut self) -> ByteIOResult<u8> {
565 let b = self.read_byte()?;
566 self.seek(SeekFrom::Current(-1))?;
567 Ok(b)
568 }
569
90aa4e6b 570 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
6c0356da
KS
571 match self.file.read_exact(buf) {
572 Ok(()) => Ok(buf.len()),
573 Err(err) => {
574 if err.kind() == std::io::ErrorKind::UnexpectedEof {
575 self.eof = true;
576 Err(ByteIOError::EOF)
577 } else {
578 Err(ByteIOError::ReadError)
579 }
580 },
581 }
89de616c
KS
582 }
583
584 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
e243ceb4
KS
585 let ret = self.file.read(buf);
586 if ret.is_err() { return Err(ByteIOError::ReadError); }
587 let sz = ret.unwrap();
6c0356da
KS
588 if sz < buf.len() {
589 if let Err(_err) = self.file.read(&mut buf[sz..][..1]) {
590 self.eof = true;
591 } else {
592 return Ok(sz + 1);
593 }
594 }
90aa4e6b
KS
595 Ok(sz)
596 }
597
794364cf
KS
598 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
599 let size = self.read_buf(buf)?;
600 self.seek(SeekFrom::Current(-(size as i64)))?;
601 Ok(size)
602 }
603
90aa4e6b 604 #[allow(unused_variables)]
d532000c 605 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
90aa4e6b
KS
606 Err(ByteIOError::NotImplemented)
607 }
608
609 fn tell(&mut self) -> u64 {
610 self.file.seek(SeekFrom::Current(0)).unwrap()
611 }
612
613 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
614 let res = self.file.seek(pos);
615 match res {
616 Ok(r) => Ok(r),
617 Err(_) => Err(ByteIOError::SeekError),
618 }
619 }
620
e243ceb4 621 fn is_eof(&self) -> bool {
90aa4e6b
KS
622 self.eof
623 }
624
625 fn is_seekable(&mut self) -> bool {
626 true
627 }
d532000c
KS
628
629 fn size(&mut self) -> i64 {
630 -1
631 }
cf095d56
KS
632
633 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
d532000c
KS
634}
635
28884194
KS
636/// High-level bytestream writer.
637///
638/// 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.
639///
640/// # Examples
641///
642/// ````
643/// use nihav_core::io::byteio::{MemoryWriter,ByteWriter};
644/// # use nihav_core::io::byteio::ByteIOResult;
645///
646/// # fn foo() -> ByteIOResult<()> {
647/// let mut memory = [0u8; 4];
648/// let mut mw = MemoryWriter::new_write(&mut memory);
649/// let mut bw = ByteWriter::new(&mut mw);
650/// let val = bw.write_u16be(42)?; // memory should be [ 0, 42, 0, 0 ]
651/// let val = bw.write_u16le(42)?; // memory should be [ 0, 42, 42, 0 ]
652/// # Ok(())
653/// # }
654/// ````
655///
656/// [`ByteIO`]: ./trait.ByteIO.html
657/// [`MemoryWriter`]: ./struct.MemoryWriter.html
d532000c
KS
658#[allow(dead_code)]
659pub struct ByteWriter<'a> {
ac818eac 660 io: &'a mut dyn ByteIO,
d532000c
KS
661}
662
28884194 663/// Bytestream writer to memory.
d532000c
KS
664pub struct MemoryWriter<'a> {
665 buf: &'a mut [u8],
d532000c
KS
666 pos: usize,
667}
668
789354a8
KS
669/// Bytestream writer to anything implementing `std::io::Write` and `std::io::Seek`.
670pub struct FileWriter<T: Write+Seek> {
671 file: Box<T>,
d532000c
KS
672}
673
422e87e4
KS
674/// Bytestream writer to memory.
675///
676/// Unlike [`MemoryWriter`] which writes to an array of fixed size, `GrowableMemoryWriter` grows output size when output size exceeds capacity.
677///
678/// [`MemoryWriter`]: ./struct.MemoryWriter.html
679pub struct GrowableMemoryWriter<'a> {
680 buf: &'a mut Vec<u8>,
681 pos: usize,
682}
683
d532000c 684impl<'a> ByteWriter<'a> {
28884194 685 /// Constructs a new instance of `ByteWriter`.
ac818eac 686 pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteWriter { io } }
d532000c 687
28884194 688 /// Writes byte array to the output.
d532000c
KS
689 pub fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
690 self.io.write_buf(buf)
691 }
692
28884194 693 /// Writes single byte to the output.
d532000c
KS
694 pub fn write_byte(&mut self, val: u8) -> ByteIOResult<()> {
695 let buf: [u8; 1] = [val];
696 self.io.write_buf(&buf)
697 }
698
28884194 699 /// Writes 16-bit big-endian integer to the output.
d532000c
KS
700 pub fn write_u16be(&mut self, val: u16) -> ByteIOResult<()> {
701 let buf: [u8; 2] = [((val >> 8) & 0xFF) as u8, (val & 0xFF) as u8];
702 self.io.write_buf(&buf)
703 }
704
28884194 705 /// Writes 16-bit little-endian integer to the output.
d532000c
KS
706 pub fn write_u16le(&mut self, val: u16) -> ByteIOResult<()> {
707 let buf: [u8; 2] = [(val & 0xFF) as u8, ((val >> 8) & 0xFF) as u8];
708 self.io.write_buf(&buf)
709 }
710
28884194 711 /// Writes 24-bit big-endian integer to the output.
d532000c
KS
712 pub fn write_u24be(&mut self, val: u32) -> ByteIOResult<()> {
713 let buf: [u8; 3] = [((val >> 16) & 0xFF) as u8, ((val >> 8) & 0xFF) as u8, (val & 0xFF) as u8];
714 self.write_buf(&buf)
715 }
716
28884194 717 /// Writes 24-bit little-endian integer to the output.
d532000c
KS
718 pub fn write_u24le(&mut self, val: u32) -> ByteIOResult<()> {
719 let buf: [u8; 3] = [(val & 0xFF) as u8, ((val >> 8) & 0xFF) as u8, ((val >> 16) & 0xFF) as u8];
720 self.write_buf(&buf)
721 }
722
28884194 723 /// Writes 32-bit big-endian integer to the output.
d532000c
KS
724 pub fn write_u32be(&mut self, val: u32) -> ByteIOResult<()> {
725 self.write_u16be(((val >> 16) & 0xFFFF) as u16)?;
726 self.write_u16be((val & 0xFFFF) as u16)
727 }
728
28884194 729 /// Writes 32-bit little-endian integer to the output.
d532000c
KS
730 pub fn write_u32le(&mut self, val: u32) -> ByteIOResult<()> {
731 self.write_u16le((val & 0xFFFF) as u16)?;
732 self.write_u16le(((val >> 16) & 0xFFFF) as u16)
733 }
734
28884194 735 /// Writes 64-bit big-endian integer to the output.
d532000c 736 pub fn write_u64be(&mut self, val: u64) -> ByteIOResult<()> {
e243ceb4
KS
737 self.write_u32be((val >> 32) as u32)?;
738 self.write_u32be(val as u32)
d532000c
KS
739 }
740
28884194 741 /// Writes 64-bit little-endian integer to the output.
d532000c 742 pub fn write_u64le(&mut self, val: u64) -> ByteIOResult<()> {
e243ceb4
KS
743 self.write_u32le(val as u32)?;
744 self.write_u32le((val >> 32) as u32)
d532000c
KS
745 }
746
28884194 747 /// Writes 32-bit big-endian floating point number to the output.
02a5bdaf
KS
748 pub fn write_f32be(&mut self, val: f32) -> ByteIOResult<()> {
749 self.write_u32be(val.to_bits())
750 }
751
28884194 752 /// Writes 32-bit little-endian floating point number to the output.
02a5bdaf
KS
753 pub fn write_f32le(&mut self, val: f32) -> ByteIOResult<()> {
754 self.write_u32le(val.to_bits())
755 }
756
28884194 757 /// Writes 64-bit big-endian floating point number to the output.
02a5bdaf
KS
758 pub fn write_f64be(&mut self, val: f64) -> ByteIOResult<()> {
759 self.write_u64be(val.to_bits())
760 }
761
28884194 762 /// Writes 64-bit little-endian floating point number to the output.
02a5bdaf
KS
763 pub fn write_f64le(&mut self, val: f64) -> ByteIOResult<()> {
764 self.write_u64le(val.to_bits())
765 }
766
28884194 767 /// Reports the current write position.
d532000c
KS
768 pub fn tell(&mut self) -> u64 {
769 self.io.tell()
770 }
771
28884194 772 /// Seeks to the requested position.
d532000c
KS
773 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
774 self.io.seek(pos)
775 }
776
28884194 777 /// Reports the amount of bytes the writer can still write (-1 if unknown).
d532000c
KS
778 pub fn size_left(&mut self) -> i64 {
779 let sz = self.io.size();
780 if sz == -1 { return -1; }
781 sz - (self.tell() as i64)
782 }
cf095d56
KS
783
784 /// Flushes output stream if possible.
785 pub fn flush(&mut self) -> ByteIOResult<()> {
786 self.io.flush()
787 }
d532000c
KS
788}
789
790impl<'a> MemoryWriter<'a> {
791
28884194 792 /// Constructs a new instance of `MemoryWriter`.
d532000c 793 pub fn new_write(buf: &'a mut [u8]) -> Self {
1be85e00 794 MemoryWriter { buf, pos: 0 }
d532000c
KS
795 }
796
797 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
1be85e00 798 if pos < 0 || (pos as usize) > self.buf.len() {
d532000c
KS
799 return Err(ByteIOError::WrongRange)
800 }
801 self.pos = pos as usize;
802 Ok(pos as u64)
803 }
804}
805
806impl<'a> ByteIO for MemoryWriter<'a> {
807 #[allow(unused_variables)]
808 fn read_byte(&mut self) -> ByteIOResult<u8> {
809 Err(ByteIOError::NotImplemented)
810 }
811
812 #[allow(unused_variables)]
813 fn peek_byte(&mut self) -> ByteIOResult<u8> {
814 Err(ByteIOError::NotImplemented)
815 }
816
817 #[allow(unused_variables)]
818 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
819 Err(ByteIOError::NotImplemented)
820 }
821
822 #[allow(unused_variables)]
823 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
824 Err(ByteIOError::NotImplemented)
825 }
826
827 #[allow(unused_variables)]
828 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
829 Err(ByteIOError::NotImplemented)
830 }
831
832 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
1be85e00 833 if self.pos + buf.len() > self.buf.len() { return Err(ByteIOError::WriteError); }
b36f412c 834 self.buf[self.pos..][..buf.len()].copy_from_slice(buf);
d532000c
KS
835 self.pos += buf.len();
836 Ok(())
837 }
838
839 fn tell(&mut self) -> u64 {
840 self.pos as u64
841 }
842
843 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
422e87e4
KS
844 let cur_pos = self.pos as i64;
845 let cur_size = self.buf.len() as i64;
846 match pos {
847 SeekFrom::Start(x) => self.real_seek(x as i64),
848 SeekFrom::Current(x) => self.real_seek(cur_pos + x),
849 SeekFrom::End(x) => self.real_seek(cur_size + x),
850 }
851 }
852
853 fn is_eof(&self) -> bool {
854 self.pos >= self.buf.len()
855 }
856
857 fn is_seekable(&mut self) -> bool {
858 true
859 }
860
861 fn size(&mut self) -> i64 {
862 self.buf.len() as i64
863 }
cf095d56
KS
864
865 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
422e87e4
KS
866}
867
868impl<'a> GrowableMemoryWriter<'a> {
869
870 /// Constructs a new instance of `GrowableMemoryWriter`.
871 pub fn new_write(buf: &'a mut Vec<u8>) -> Self {
872 GrowableMemoryWriter { buf, pos: 0 }
873 }
874
875 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
876 if pos < 0 || (pos as usize) > self.buf.len() {
877 return Err(ByteIOError::WrongRange)
878 }
879 self.pos = pos as usize;
880 Ok(pos as u64)
881 }
882}
883
884impl<'a> ByteIO for GrowableMemoryWriter<'a> {
885 #[allow(unused_variables)]
886 fn read_byte(&mut self) -> ByteIOResult<u8> {
887 Err(ByteIOError::NotImplemented)
888 }
889
890 #[allow(unused_variables)]
891 fn peek_byte(&mut self) -> ByteIOResult<u8> {
892 Err(ByteIOError::NotImplemented)
893 }
894
895 #[allow(unused_variables)]
896 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
897 Err(ByteIOError::NotImplemented)
898 }
899
900 #[allow(unused_variables)]
901 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
902 Err(ByteIOError::NotImplemented)
903 }
904
905 #[allow(unused_variables)]
906 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
907 Err(ByteIOError::NotImplemented)
908 }
909
910 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
911 if self.pos + buf.len() > self.buf.len() {
912 self.buf.resize(self.pos + buf.len(), 0);
913 }
b36f412c 914 self.buf[self.pos..][..buf.len()].copy_from_slice(buf);
422e87e4
KS
915 self.pos += buf.len();
916 Ok(())
917 }
918
919 fn tell(&mut self) -> u64 {
920 self.pos as u64
921 }
922
923 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
1be85e00
KS
924 let cur_pos = self.pos as i64;
925 let cur_size = self.buf.len() as i64;
d532000c
KS
926 match pos {
927 SeekFrom::Start(x) => self.real_seek(x as i64),
928 SeekFrom::Current(x) => self.real_seek(cur_pos + x),
929 SeekFrom::End(x) => self.real_seek(cur_size + x),
930 }
931 }
932
e243ceb4 933 fn is_eof(&self) -> bool {
1be85e00 934 self.pos >= self.buf.len()
d532000c
KS
935 }
936
937 fn is_seekable(&mut self) -> bool {
938 true
939 }
940
941 fn size(&mut self) -> i64 {
942 self.buf.len() as i64
943 }
cf095d56
KS
944
945 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
d532000c
KS
946}
947
789354a8 948impl<T: Write+Seek> FileWriter<T> {
28884194 949 /// Constructs a new instance of `FileWriter`.
789354a8
KS
950 pub fn new_write(file: T) -> Self {
951 FileWriter { file: Box::new(file) }
d532000c
KS
952 }
953}
954
789354a8 955impl<T: Write+Seek> ByteIO for FileWriter<T> {
d532000c
KS
956 #[allow(unused_variables)]
957 fn read_byte(&mut self) -> ByteIOResult<u8> {
958 Err(ByteIOError::NotImplemented)
959 }
960
961 #[allow(unused_variables)]
962 fn peek_byte(&mut self) -> ByteIOResult<u8> {
963 Err(ByteIOError::NotImplemented)
964 }
965
966 #[allow(unused_variables)]
967 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
968 Err(ByteIOError::NotImplemented)
969 }
970
971 #[allow(unused_variables)]
972 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
973 Err(ByteIOError::NotImplemented)
974 }
975
976 #[allow(unused_variables)]
977 fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
978 Err(ByteIOError::NotImplemented)
979 }
980
981 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
982 match self.file.write_all(buf) {
983 Ok(()) => Ok(()),
984 Err(_) => Err(ByteIOError::WriteError),
985 }
986 }
987
988 fn tell(&mut self) -> u64 {
989 self.file.seek(SeekFrom::Current(0)).unwrap()
990 }
991
992 fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
993 let res = self.file.seek(pos);
994 match res {
995 Ok(r) => Ok(r),
996 Err(_) => Err(ByteIOError::SeekError),
997 }
998 }
999
e243ceb4 1000 fn is_eof(&self) -> bool {
d532000c
KS
1001 false
1002 }
1003
1004 fn is_seekable(&mut self) -> bool {
1005 true
1006 }
1007
1008 fn size(&mut self) -> i64 {
1009 -1
1010 }
cf095d56
KS
1011
1012 fn flush(&mut self) -> ByteIOResult<()> {
1013 match self.file.flush() {
1014 Ok(()) => Ok(()),
1015 Err(_) => Err(ByteIOError::WriteError),
1016 }
1017 }
90aa4e6b
KS
1018}
1019
1020#[cfg(test)]
1021mod test {
1022 use super::*;
1023 use std::fs::File;
1024
1025 #[test]
1026 fn test_read() {
1027 //const DATA : &'static [u8] = include_bytes!("../../assets/file");
1028 let buf: [u8; 64] = [1; 64];
1029 let mut mr = MemoryReader::new_read(&buf);
1030 let mut reader = ByteReader::new(&mut mr);
1031 assert_eq!(reader.read_byte().unwrap(), 0x01u8);
1032 assert_eq!(reader.read_u16le().unwrap(), 0x0101u16);
1033 assert_eq!(reader.read_u24le().unwrap(), 0x010101u32);
1034 assert_eq!(reader.read_u32le().unwrap(), 0x01010101u32);
1035 assert_eq!(reader.read_u64le().unwrap(), 0x0101010101010101u64);
1678d59a 1036 let mut file = File::open("assets/Misc/MaoMacha.asx").unwrap();
90aa4e6b
KS
1037 let mut fr = FileReader::new_read(&mut file);
1038 let mut br2 = ByteReader::new(&mut fr);
1039 assert_eq!(br2.read_byte().unwrap(), 0x30);
1040 assert_eq!(br2.read_u24be().unwrap(), 0x26B275);
1041 assert_eq!(br2.read_u24le().unwrap(), 0xCF668E);
1042 assert_eq!(br2.read_u32be().unwrap(), 0x11A6D900);
1043 assert_eq!(br2.read_u32le().unwrap(), 0xCE6200AA);
1044 }
1045 #[test]
1046 fn test_write() {
d532000c
KS
1047 let mut buf: [u8; 64] = [0; 64];
1048 {
1049 let mut mw = MemoryWriter::new_write(&mut buf);
1050 let mut bw = ByteWriter::new(&mut mw);
1051 bw.write_byte(0x00).unwrap();
1052 bw.write_u16be(0x0102).unwrap();
1053 bw.write_u24be(0x030405).unwrap();
1054 bw.write_u32be(0x06070809).unwrap();
1055 bw.write_u64be(0x0A0B0C0D0E0F1011).unwrap();
1056 bw.write_byte(0x00).unwrap();
1057 bw.write_u16le(0x0201).unwrap();
1058 bw.write_u24le(0x050403).unwrap();
1059 bw.write_u32le(0x09080706).unwrap();
1060 bw.write_u64le(0x11100F0E0D0C0B0A).unwrap();
1061 assert_eq!(bw.size_left(), 28);
1062 }
1063 for i in 0..0x12 {
1064 assert_eq!(buf[(i + 0x00) as usize], i);
1065 assert_eq!(buf[(i + 0x12) as usize], i);
1066 }
90aa4e6b
KS
1067 }
1068}