/// Try to parse an EBML varint at the start of the given slice.
/// Returns an Err() if the format is corrupt.
/// Returns Ok(None) if more bytes are needed to get a result.
/// Returns Ok(Some((varint, size))) to return a varint value and
/// the size of the parsed varint.
Try to parse an EBML varint at the start of the given slice.
Returns an Err() if the format is corrupt.
Returns Ok(None) if more bytes are needed to get a result.
Returns Ok(Some((varint, size))) to return a varint value and
the size of the parsed varint.
Try to parse an EBML varint at the start of the given slice . Returns an Err () if the format is corrupt . Returns Ok ( None ) if more bytes are needed to get a result . Returns Ok ( Some (( varint size ))) to return a varint value and the size of the parsed varint .
pub fn decode_varint(bytes: &[u8]) -> Result<Option<(Varint, usize)>, EbmlError> {
    let mut value: u64 = 0;
    let mut value_length = 1;
    let mut mask: u8 = 0x80;
    let mut unknown_marker: u64 = !0;
    if bytes.len() == 0 {
        return Ok(None)
    }
    // get length marker bit from first byte & parse first byte
    while mask > 0 {
        if (mask & bytes[0]) != 0 {
            value = (bytes[0] & !mask) as u64;
            unknown_marker = (mask - 1) as u64;
            break
        }
        value_length += 1;
        mask = mask >> 1;
    }
    if mask == 0 {
        return Err(EbmlError::CorruptVarint)
    }
    // check we have enough data to parse
    if value_length > bytes.len() {
        return Ok(None)
    }
    // decode remaining bytes
    for i in 1..value_length {
        value = (value << 8) + (bytes[i] as u64);
        unknown_marker = (unknown_marker << 8) + 0xFF;
    }
    // determine result
    if value == unknown_marker {
        Ok(Some((Varint::Unknown, value_length)))
    } else {
        Ok(Some((Varint::Value(value), value_length)))
    }
}
pub fn decode_varint ( bytes : & [ u8 ] ) -> Result < Option < ( Varint , usize ) > , EbmlError > { let mut value : u64 = 0 ; let mut value_length = 1 ; let mut mask : u8 = 0x80 ; let mut unknown_marker : u64 = ! 0 ; if bytes . len ( ) == 0 { return Ok ( None ) } // get length marker bit from first byte & parse first byte while mask > 0 { if ( mask & bytes [ 0 ] ) != 0 { value = ( bytes [ 0 ] & ! mask ) as u64 ; unknown_marker = ( mask - 1 ) as u64 ; break } value_length += 1 ; mask = mask >> 1 ; } if mask == 0 { return Err ( EbmlError :: CorruptVarint ) } // check we have enough data to parse if value_length > bytes . len ( ) { return Ok ( None ) } // decode remaining bytes for i in 1 .. value_length { value = ( value << 8 ) + ( bytes [ i ] as u64 ) ; unknown_marker = ( unknown_marker << 8 ) + 0xFF ; } // determine result if value == unknown_marker { Ok ( Some ( ( Varint :: Unknown , value_length ) ) ) } else { Ok ( Some ( ( Varint :: Value ( value ) , value_length ) ) ) } }
Try to parse an EBML varint at the start of the given slice.
Try to parse an EBML varint at the start of the given slice .
bytes
&[u8]