1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::*;
use std::io::*;

pub fn read_u1(r: &mut impl Read) -> Result<u8> {
    let mut buffer = [0u8; 1];
    r.read_exact(&mut buffer)?;
    Ok(buffer[0])
}

pub fn read_u2(r: &mut impl Read) -> Result<u16> {
    let mut buffer = [0u8; 2];
    r.read_exact(&mut buffer)?;
    Ok(u16::from_be_bytes(buffer))
}

pub fn read_u4(r: &mut impl Read) -> Result<u32> {
    let mut buffer = [0u8; 4];
    r.read_exact(&mut buffer)?;
    Ok(u32::from_be_bytes(buffer))
}

pub fn read_u8(r: &mut impl Read) -> Result<u64> {
    let mut buffer = [0u8; 8];
    r.read_exact(&mut buffer)?;
    Ok(u64::from_be_bytes(buffer))
}

//pub fn read_i1(r: &mut impl Read) -> Result< i8> { read_u1(r).map(|u| u as  i8) }
//pub fn read_i2(r: &mut impl Read) -> Result<i16> { read_u2(r).map(|u| u as i16) }
pub fn read_i4(r: &mut impl Read) -> Result<i32> { read_u4(r).map(|u| u as i32) }
pub fn read_i8(r: &mut impl Read) -> Result<i64> { read_u8(r).map(|u| u as i64) }

pub fn read_ignore(read: &mut impl Read, bytes: usize) -> io::Result<()> {
    let mut info = Vec::new();
    info.resize(bytes, 0u8);
    read.read_exact(&mut info[..])?;
    Ok(())
}



// I/O errors here "probably" indicate bugs in class parsing - break at the callsite for ease of debugging.
// The other alternative is you're parsing bad/corrupt classes, so good luck with that.

#[macro_export]
macro_rules! io_data_error {
    ($($arg:tt)*) => {{
        use bugsalot::*;
        let message = format!($($arg)*);
        bug!("{}", &message);
        std::io::Error::new(std::io::ErrorKind::InvalidData, message)
    }};
}

#[macro_export]
macro_rules! io_data_err {
    ($($arg:tt)*) => { Err($crate::io_data_error!($($arg)*)) };
}

macro_rules! io_assert {
    ($condition:expr) => {
        if !$condition {
            return $crate::io_data_err!("Assertion failed: {}", stringify!($condition));
        }
    };
    ($condition:expr, $($arg:tt)*) => {
        if !$condition {
            return $crate::io_data_err!($($arg)*);
        }
    };
}