]> git.nihav.org Git - nihav.git/blob - nihav-core/src/io/byteio.rs
add raw RGB support for AVI
[nihav.git] / nihav-core / src / io / byteio.rs
1 //! Bytestream reading/writing functionality.
2 pub use std::io::SeekFrom;
3 use std::io::prelude::*;
4 use std::ptr;
5
6 /// A list specifying general bytestream reading and writing errors.
7 #[derive(Debug)]
8 pub enum ByteIOError {
9 /// End of stream.
10 EOF,
11 /// Wrong seek position was provided.
12 WrongRange,
13 /// Tried to call read() on bytestream writer or write() on bytestream reader.
14 WrongIOMode,
15 /// Functionality is not implemented.
16 NotImplemented,
17 /// Read error.
18 ReadError,
19 /// Write error.
20 WriteError,
21 /// Seeking failed.
22 SeekError,
23 }
24
25 /// A specialised `Result` type for bytestream operations.
26 pub type ByteIOResult<T> = Result<T, ByteIOError>;
27
28 /// Common trait for bytestream operations.
29 pub trait ByteIO {
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<()>;
54 }
55
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
78 #[allow(dead_code)]
79 pub struct ByteReader<'a> {
80 io: &'a mut dyn ByteIO,
81 }
82
83 /// Bytestream reader from memory.
84 pub struct MemoryReader<'a> {
85 buf: &'a [u8],
86 pos: usize,
87 }
88
89 /// Bytestream reader from anything implementing `std::io::Read` and `std::io::Seek`.
90 pub struct FileReader<T: Read+Seek> {
91 file: Box<T>,
92 eof: bool,
93 }
94
95 macro_rules! read_int {
96 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
97 unsafe {
98 let mut buf: $inttype = 0;
99 $s.read_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
100 Ok(buf.$which())
101 }
102 })
103 }
104
105 macro_rules! peek_int {
106 ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({
107 unsafe {
108 let mut buf: $inttype = 0;
109 $s.peek_buf(&mut *(&mut buf as *mut $inttype as *mut [u8; $size]))?;
110 Ok(buf.$which())
111 }
112 })
113 }
114
115 macro_rules! read_int_func {
116 ($s: ident, $inttype: ty, $size: expr, $which: ident) => {
117 /// Reads integer of certain size and endianness.
118 pub fn $s(src: &[u8]) -> ByteIOResult<$inttype> {
119 if src.len() < $size { return Err(ByteIOError::ReadError); }
120 unsafe {
121 let mut buf: $inttype = 0;
122 ptr::copy_nonoverlapping(src.as_ptr(), &mut buf as *mut $inttype as *mut u8, std::mem::size_of::<$inttype>());
123 Ok(buf.$which())
124 }
125 }
126 }
127 }
128
129 read_int_func!(read_u16be, u16, 2, to_be);
130 read_int_func!(read_u16le, u16, 2, to_le);
131 read_int_func!(read_u32be, u32, 4, to_be);
132 read_int_func!(read_u32le, u32, 4, to_le);
133 read_int_func!(read_u64be, u64, 8, to_be);
134 read_int_func!(read_u64le, u64, 8, to_le);
135
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 /// ````
150 pub fn read_u24be(src: &[u8]) -> ByteIOResult<u32> {
151 if src.len() < 3 { return Err(ByteIOError::ReadError); }
152 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
153 }
154 /// Reads 24-bit little-endian integer.
155 pub fn read_u24le(src: &[u8]) -> ByteIOResult<u32> {
156 if src.len() < 3 { return Err(ByteIOError::ReadError); }
157 Ok((u32::from(src[2]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[0]))
158 }
159 /// Reads 32-bit big-endian floating point number.
160 pub fn read_f32be(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32be(src)?)) }
161 /// Reads 32-bit little-endian floating point number.
162 pub fn read_f32le(src: &[u8]) -> ByteIOResult<f32> { Ok(f32::from_bits(read_u32le(src)?)) }
163 /// Reads 64-bit big-endian floating point number.
164 pub fn read_f64be(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64be(src)?)) }
165 /// Reads 64-bit little-endian floating point number.
166 pub fn read_f64le(src: &[u8]) -> ByteIOResult<f64> { Ok(f64::from_bits(read_u64le(src)?)) }
167
168 macro_rules! write_int_func {
169 ($s: ident, $inttype: ty, $size: expr, $which: ident) => {
170 /// Writes integer of certain size and endianness into byte buffer.
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
182 write_int_func!(write_u16be, u16, 2, to_be);
183 write_int_func!(write_u16le, u16, 2, to_le);
184 write_int_func!(write_u32be, u32, 4, to_be);
185 write_int_func!(write_u32le, u32, 4, to_le);
186 write_int_func!(write_u64be, u64, 8, to_be);
187 write_int_func!(write_u64le, u64, 8, to_le);
188
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 /// ````
204 #[allow(clippy::identity_op)]
205 pub 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 }
212 /// Writes 24-bit little-endian integer to the provided buffer.
213 #[allow(clippy::identity_op)]
214 pub 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 }
221 /// Writes 32-bit big-endian floating point number to the provided buffer.
222 pub fn write_f32be(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32be(dst, val.to_bits()) }
223 /// Writes 32-bit little-endian floating point number to the provided buffer.
224 pub fn write_f32le(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32le(dst, val.to_bits()) }
225 /// Writes 64-bit big-endian floating point number to the provided buffer.
226 pub fn write_f64be(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64be(dst, val.to_bits()) }
227 /// Writes 64-bit little-endian floating point number to the provided buffer.
228 pub fn write_f64le(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64le(dst, val.to_bits()) }
229
230 impl<'a> ByteReader<'a> {
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 /// ````
246 pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteReader { io } }
247
248 /// Reads data into provided buffer. Partial read is treated as success.
249 pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
250 self.io.read_buf(buf)
251 }
252
253 /// Reads data into provided buffer. Partial read is treated as success.
254 pub fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
255 self.io.read_buf_some(buf)
256 }
257
258 /// Reads data into provided buffer but does not advance read position.
259 pub fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
260 self.io.peek_buf(buf)
261 }
262
263 /// Reads single byte from the stream.
264 pub fn read_byte(&mut self) -> ByteIOResult<u8> {
265 self.io.read_byte()
266 }
267
268 /// Returns the next byte value in the stream without advancing read position.
269 pub fn peek_byte(&mut self) -> ByteIOResult<u8> {
270 self.io.peek_byte()
271 }
272
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
287 /// Reads 16-bit big-endian integer from the stream.
288 pub fn read_u16be(&mut self) -> ByteIOResult<u16> {
289 read_int!(self, u16, 2, to_be)
290 }
291
292 /// Reads 16-bit big-endian integer from the stream without advancing read position.
293 pub fn peek_u16be(&mut self) -> ByteIOResult<u16> {
294 peek_int!(self, u16, 2, to_be)
295 }
296
297 /// Reads 24-bit big-endian integer from the stream.
298 pub fn read_u24be(&mut self) -> ByteIOResult<u32> {
299 let p16 = self.read_u16be()?;
300 let p8 = self.read_byte()?;
301 Ok((u32::from(p16) << 8) | u32::from(p8))
302 }
303
304 /// Reads 24-bit big-endian integer from the stream without advancing read position.
305 pub fn peek_u24be(&mut self) -> ByteIOResult<u32> {
306 let mut src: [u8; 3] = [0; 3];
307 self.peek_buf(&mut src)?;
308 Ok((u32::from(src[0]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[2]))
309 }
310
311 /// Reads 32-bit big-endian integer from the stream.
312 pub fn read_u32be(&mut self) -> ByteIOResult<u32> {
313 read_int!(self, u32, 4, to_be)
314 }
315
316 /// Reads 32-bit big-endian integer from the stream without advancing read position.
317 pub fn peek_u32be(&mut self) -> ByteIOResult<u32> {
318 peek_int!(self, u32, 4, to_be)
319 }
320
321 /// Reads 64-bit big-endian integer from the stream.
322 pub fn read_u64be(&mut self) -> ByteIOResult<u64> {
323 read_int!(self, u64, 8, to_be)
324 }
325
326 /// Reads 64-bit big-endian integer from the stream without advancing read position.
327 pub fn peek_u64be(&mut self) -> ByteIOResult<u64> {
328 peek_int!(self, u64, 8, to_be)
329 }
330
331 /// Reads 32-bit big-endian floating point number from the stream.
332 pub fn read_f32be(&mut self) -> ByteIOResult<f32> {
333 Ok(f32::from_bits(self.read_u32be()?))
334 }
335
336 /// Reads 32-bit big-endian floating point number from the stream without advancing read position.
337 pub fn peek_f32be(&mut self) -> ByteIOResult<f32> {
338 Ok(f32::from_bits(self.peek_u32be()?))
339 }
340
341 /// Reads 64-bit big-endian floating point number from the stream.
342 pub fn read_f64be(&mut self) -> ByteIOResult<f64> {
343 Ok(f64::from_bits(self.read_u64be()?))
344 }
345
346 /// Reads 64-bit big-endian floating point number from the stream without advancing read position.
347 pub fn peek_f64be(&mut self) -> ByteIOResult<f64> {
348 Ok(f64::from_bits(self.peek_u64be()?))
349 }
350
351 /// Reads 16-bit little-endian integer from the stream.
352 pub fn read_u16le(&mut self) -> ByteIOResult<u16> {
353 read_int!(self, u16, 2, to_le)
354 }
355
356 /// Reads 16-bit little-endian integer from the stream without advancing read position.
357 pub fn peek_u16le(&mut self) -> ByteIOResult<u16> {
358 peek_int!(self, u16, 2, to_le)
359 }
360
361 /// Reads 24-bit little-endian integer from the stream.
362 pub fn read_u24le(&mut self) -> ByteIOResult<u32> {
363 let p8 = self.read_byte()?;
364 let p16 = self.read_u16le()?;
365 Ok((u32::from(p16) << 8) | u32::from(p8))
366 }
367
368 /// Reads 24-bit little-endian integer from the stream without advancing read position.
369 pub fn peek_u24le(&mut self) -> ByteIOResult<u32> {
370 let mut src: [u8; 3] = [0; 3];
371 self.peek_buf(&mut src)?;
372 Ok(u32::from(src[0]) | (u32::from(src[1]) << 8) | (u32::from(src[2]) << 16))
373 }
374
375 /// Reads 32-bit little-endian integer from the stream.
376 pub fn read_u32le(&mut self) -> ByteIOResult<u32> {
377 read_int!(self, u32, 4, to_le)
378 }
379
380 /// Reads 32-bit little-endian integer from the stream without advancing read position.
381 pub fn peek_u32le(&mut self) -> ByteIOResult<u32> {
382 peek_int!(self, u32, 4, to_le)
383 }
384
385 /// Reads 64-bit little-endian integer from the stream.
386 pub fn read_u64le(&mut self) -> ByteIOResult<u64> {
387 read_int!(self, u64, 8, to_le)
388 }
389
390 /// Reads 64-bit little-endian integer from the stream without advancing read position.
391 pub fn peek_u64le(&mut self) -> ByteIOResult<u64> {
392 peek_int!(self, u64, 8, to_le)
393 }
394
395 /// Reads 32-bit little-endian floating point number from the stream.
396 pub fn read_f32le(&mut self) -> ByteIOResult<f32> {
397 Ok(f32::from_bits(self.read_u32le()?))
398 }
399
400 /// Reads 32-bit little-endian floating point number from the stream without advancing read position.
401 pub fn peek_f32le(&mut self) -> ByteIOResult<f32> {
402 Ok(f32::from_bits(self.peek_u32le()?))
403 }
404
405 /// Reads 64-bit little-endian floating point number from the stream.
406 pub fn read_f64le(&mut self) -> ByteIOResult<f64> {
407 Ok(f64::from_bits(self.read_u64le()?))
408 }
409
410 /// Reads 64-bit little-endian floating point number from the stream without advancing read position.
411 pub fn peek_f64le(&mut self) -> ByteIOResult<f64> {
412 Ok(f64::from_bits(self.peek_u64le()?))
413 }
414
415 /// Skips requested number of bytes.
416 pub fn read_skip(&mut self, len: usize) -> ByteIOResult<()> {
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];
422 let bref = &mut buf;
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()?;
429 ssize -= 1;
430 }
431 }
432 Ok(())
433 }
434
435 /// Returns current read position.
436 pub fn tell(&mut self) -> u64 {
437 self.io.tell()
438 }
439
440 /// Seeks to the provided position.
441 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
442 self.io.seek(pos)
443 }
444
445 /// Tells whether this is end of stream.
446 pub fn is_eof(&self) -> bool {
447 self.io.is_eof()
448 }
449
450 /// Returns stream size or -1 if it is not known.
451 pub fn size(&mut self) -> i64 {
452 self.io.size()
453 }
454
455 /// Reports number of bytes left in the stream.
456 pub fn left(&mut self) -> i64 {
457 let size = self.io.size();
458 if size == -1 { return -1; }
459 size - (self.io.tell() as i64)
460 }
461 }
462
463 impl<'a> MemoryReader<'a> {
464 /// Constructs a new instance of `MemoryReader`.
465 pub fn new_read(buf: &'a [u8]) -> Self {
466 MemoryReader { buf, pos: 0 }
467 }
468
469 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
470 if pos < 0 || (pos as usize) > self.buf.len() {
471 return Err(ByteIOError::WrongRange);
472 }
473 self.pos = pos as usize;
474 Ok(pos as u64)
475 }
476 }
477
478 impl<'a> ByteIO for MemoryReader<'a> {
479 fn read_byte(&mut self) -> ByteIOResult<u8> {
480 if self.is_eof() { return Err(ByteIOError::EOF); }
481 let res = self.buf[self.pos];
482 self.pos += 1;
483 Ok(res)
484 }
485
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> {
492 let copy_size = if self.buf.len() - self.pos < buf.len() { self.buf.len() - self.pos } else { buf.len() };
493 if copy_size == 0 { return Err(ByteIOError::EOF); }
494 let dst = &mut buf[0..copy_size];
495 dst.copy_from_slice(&self.buf[self.pos..][..copy_size]);
496 Ok(copy_size)
497 }
498
499 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
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> {
507 let read_size = self.peek_buf(buf)?;
508 self.pos += read_size;
509 Ok(read_size)
510 }
511
512 #[allow(unused_variables)]
513 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
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> {
522 let cur_pos = self.pos as i64;
523 let cur_size = self.buf.len() as i64;
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
531 fn is_eof(&self) -> bool {
532 self.pos >= self.buf.len()
533 }
534
535 fn is_seekable(&mut self) -> bool {
536 true
537 }
538
539 fn size(&mut self) -> i64 {
540 self.buf.len() as i64
541 }
542
543 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
544 }
545
546 impl<T: Read+Seek> FileReader<T> {
547
548 /// Constructs a new instance of `FileReader`.
549 pub fn new_read(file: T) -> Self {
550 FileReader { file: Box::new(file), eof : false }
551 }
552 }
553
554 impl<T: Read+Seek> ByteIO for FileReader<T> {
555 fn read_byte(&mut self) -> ByteIOResult<u8> {
556 let mut byte : [u8; 1] = [0];
557 let ret = self.file.read(&mut byte);
558 if ret.is_err() { return Err(ByteIOError::ReadError); }
559 let sz = ret.unwrap();
560 if sz == 0 { self.eof = true; return Err(ByteIOError::EOF); }
561 Ok (byte[0])
562 }
563
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
570 fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
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 }
582 }
583
584 fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
585 let ret = self.file.read(buf);
586 if ret.is_err() { return Err(ByteIOError::ReadError); }
587 let sz = ret.unwrap();
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 }
595 Ok(sz)
596 }
597
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
604 #[allow(unused_variables)]
605 fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
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
621 fn is_eof(&self) -> bool {
622 self.eof
623 }
624
625 fn is_seekable(&mut self) -> bool {
626 true
627 }
628
629 fn size(&mut self) -> i64 {
630 -1
631 }
632
633 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
634 }
635
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
658 #[allow(dead_code)]
659 pub struct ByteWriter<'a> {
660 io: &'a mut dyn ByteIO,
661 }
662
663 /// Bytestream writer to memory.
664 pub struct MemoryWriter<'a> {
665 buf: &'a mut [u8],
666 pos: usize,
667 }
668
669 /// Bytestream writer to anything implementing `std::io::Write` and `std::io::Seek`.
670 pub struct FileWriter<T: Write+Seek> {
671 file: Box<T>,
672 }
673
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
679 pub struct GrowableMemoryWriter<'a> {
680 buf: &'a mut Vec<u8>,
681 pos: usize,
682 }
683
684 impl<'a> ByteWriter<'a> {
685 /// Constructs a new instance of `ByteWriter`.
686 pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteWriter { io } }
687
688 /// Writes byte array to the output.
689 pub fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
690 self.io.write_buf(buf)
691 }
692
693 /// Writes single byte to the output.
694 pub fn write_byte(&mut self, val: u8) -> ByteIOResult<()> {
695 let buf: [u8; 1] = [val];
696 self.io.write_buf(&buf)
697 }
698
699 /// Writes 16-bit big-endian integer to the output.
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
705 /// Writes 16-bit little-endian integer to the output.
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
711 /// Writes 24-bit big-endian integer to the output.
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
717 /// Writes 24-bit little-endian integer to the output.
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
723 /// Writes 32-bit big-endian integer to the output.
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
729 /// Writes 32-bit little-endian integer to the output.
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
735 /// Writes 64-bit big-endian integer to the output.
736 pub fn write_u64be(&mut self, val: u64) -> ByteIOResult<()> {
737 self.write_u32be((val >> 32) as u32)?;
738 self.write_u32be(val as u32)
739 }
740
741 /// Writes 64-bit little-endian integer to the output.
742 pub fn write_u64le(&mut self, val: u64) -> ByteIOResult<()> {
743 self.write_u32le(val as u32)?;
744 self.write_u32le((val >> 32) as u32)
745 }
746
747 /// Writes 32-bit big-endian floating point number to the output.
748 pub fn write_f32be(&mut self, val: f32) -> ByteIOResult<()> {
749 self.write_u32be(val.to_bits())
750 }
751
752 /// Writes 32-bit little-endian floating point number to the output.
753 pub fn write_f32le(&mut self, val: f32) -> ByteIOResult<()> {
754 self.write_u32le(val.to_bits())
755 }
756
757 /// Writes 64-bit big-endian floating point number to the output.
758 pub fn write_f64be(&mut self, val: f64) -> ByteIOResult<()> {
759 self.write_u64be(val.to_bits())
760 }
761
762 /// Writes 64-bit little-endian floating point number to the output.
763 pub fn write_f64le(&mut self, val: f64) -> ByteIOResult<()> {
764 self.write_u64le(val.to_bits())
765 }
766
767 /// Reports the current write position.
768 pub fn tell(&mut self) -> u64 {
769 self.io.tell()
770 }
771
772 /// Seeks to the requested position.
773 pub fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
774 self.io.seek(pos)
775 }
776
777 /// Reports the amount of bytes the writer can still write (-1 if unknown).
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 }
783
784 /// Flushes output stream if possible.
785 pub fn flush(&mut self) -> ByteIOResult<()> {
786 self.io.flush()
787 }
788 }
789
790 impl<'a> MemoryWriter<'a> {
791
792 /// Constructs a new instance of `MemoryWriter`.
793 pub fn new_write(buf: &'a mut [u8]) -> Self {
794 MemoryWriter { buf, pos: 0 }
795 }
796
797 fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
798 if pos < 0 || (pos as usize) > self.buf.len() {
799 return Err(ByteIOError::WrongRange)
800 }
801 self.pos = pos as usize;
802 Ok(pos as u64)
803 }
804 }
805
806 impl<'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<()> {
833 if self.pos + buf.len() > self.buf.len() { return Err(ByteIOError::WriteError); }
834 self.buf[self.pos..][..buf.len()].copy_from_slice(buf);
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> {
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 }
864
865 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
866 }
867
868 impl<'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
884 impl<'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 }
914 self.buf[self.pos..][..buf.len()].copy_from_slice(buf);
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> {
924 let cur_pos = self.pos as i64;
925 let cur_size = self.buf.len() as i64;
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
933 fn is_eof(&self) -> bool {
934 self.pos >= self.buf.len()
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 }
944
945 fn flush(&mut self) -> ByteIOResult<()> { Ok(()) }
946 }
947
948 impl<T: Write+Seek> FileWriter<T> {
949 /// Constructs a new instance of `FileWriter`.
950 pub fn new_write(file: T) -> Self {
951 FileWriter { file: Box::new(file) }
952 }
953 }
954
955 impl<T: Write+Seek> ByteIO for FileWriter<T> {
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
1000 fn is_eof(&self) -> bool {
1001 false
1002 }
1003
1004 fn is_seekable(&mut self) -> bool {
1005 true
1006 }
1007
1008 fn size(&mut self) -> i64 {
1009 -1
1010 }
1011
1012 fn flush(&mut self) -> ByteIOResult<()> {
1013 match self.file.flush() {
1014 Ok(()) => Ok(()),
1015 Err(_) => Err(ByteIOError::WriteError),
1016 }
1017 }
1018 }
1019
1020 #[cfg(test)]
1021 mod 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);
1036 let mut file = File::open("assets/Misc/MaoMacha.asx").unwrap();
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() {
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 }
1067 }
1068 }