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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
//! Helper macros

/// Helper macro for nom parsers: raise error if the condition is false
#[macro_export]
macro_rules! error_if (
  ($i:expr, $cond:expr, $err:expr) => (
    {
      if $cond {
        IResult::Error($err)
      } else {
        IResult::Done($i, ())
      }
    }
  );
  ($i:expr, $cond:expr, $err:expr) => (
    error!($i, $cond, $err);
  );
);


/// Read an entire slice as a big-endian value.
///
/// Returns the value as `u64`. This function checks for integer overflows, and returns a
/// `Result::Err` value if the value is too big.
pub fn bytes_to_u64(s: &[u8]) -> Result<u64, &'static str> {
    let mut u : u64 = 0;

    if s.len() == 0 { return Err("empty"); };
    for &c in s {
        let (u1,f1) = u.overflowing_mul(256);
        let (u2,f2) = u1.overflowing_add(c as u64);
        if f1 || f2 { return Err("overflow"); }
        u = u2;
    }

    Ok(u)
}

/// Read a slice as a big-endian value.
#[macro_export]
macro_rules! parse_hex_to_u64 (
    ( $i:expr, $size:expr ) => (
        map_res!($i, take!(($size as usize)), $crate::bytes_to_u64)
    );
);

named_attr!(#[doc = "Read 3 bytes as an unsigned integer"],
            pub parse_uint24<&[u8], u64>, parse_hex_to_u64!(3));

//named!(parse_hex4<&[u8], u64>, parse_hex_to_u64!(4));


/// Parse a slice and return a fixed-sized array of bytes
///
/// This creates a copy of input data
/// Uses unsafe code
#[macro_export]
macro_rules! slice_fixed(
    ( $i:expr, $count:expr ) => (
        {
            let cnt = $count;
            let ires: IResult<_,_> = if $i.len() < cnt {
                IResult::Incomplete(Needed::Size(cnt))
            } else {
                let mut res: [u8; $count] = unsafe{[::std::mem::uninitialized(); $count as usize]};
                unsafe{::std::ptr::copy($i.as_ptr(), res.as_mut_ptr(), cnt)};
                IResult::Done(&$i[cnt..],res)
            };
            ires
        }
    );
);



#[cfg(test)]
mod tests{

    use nom::{be_u8,IResult,Needed};

#[test]
#[allow(unsafe_code)]
fn test_slice_fixed() {
    let empty = &b""[..];
    let b = &[0x01, 0x02, 0x03, 0x04, 0x05];

    let res = slice_fixed!(b, 4);
    assert_eq!(res, IResult::Done(&b[4..], [1, 2, 3, 4]));

    // can we still use the result ?
    match res {
        IResult::Done(rem, _) => {
            let res2 = be_u8(rem);
            assert_eq!(res2, IResult::Done(empty,5));
        },
        _ => (),
    }
}

#[test]
#[allow(unsafe_code)]
fn test_slice_fixed_incomplete() {
    let b = &[0x01, 0x02, 0x03, 0x04, 0x05];
    let res = slice_fixed!(b, 8);
    assert_eq!(res, IResult::Incomplete(Needed::Size(8)));
}

}