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