Created
October 8, 2015 22:32
-
-
Save tedsta/ed7965aa27f401067583 to your computer and use it in GitHub Desktop.
decoding xdr encoded nvpairs in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn decode_nv_list_embedded(xdr: &mut xdr::Xdr) -> xdr::XdrResult<NvList> { | |
// Decode version and nvflag | |
let version = try!(xdr.decode_i32()); | |
let nvflag = try!(xdr.decode_u32()); | |
// TODO: Give an actual error | |
if version != NV_VERSION { | |
return Err(xdr::XdrError); | |
} | |
let mut nv_list = NvList::new(nvflag); | |
// Decode the pairs | |
loop { | |
// Decode decoded/decoded size | |
let encoded_size = try!(xdr.decode_u32()); | |
let decoded_size = try!(xdr.decode_u32()); | |
// Check for 2 terminating zeros | |
if (encoded_size == 0 && decoded_size == 0) { | |
break; | |
} | |
// Decode name | |
let name = try!(xdr.decode_string()); | |
// Decode data type | |
let data_type = | |
match DataType::from_u8(try!(xdr.decode_u8())) { | |
Some(dt) => dt, | |
None => { return Err(xdr::XdrError); }, | |
}; | |
// Decode the number of elements | |
let num_elements = try!(xdr.decode_i32()) as usize; | |
// Decode the value | |
let value = try!(decode_nv_value(xdr, data_type, num_elements)); | |
// Add the value to the list | |
nv_list.pairs.push((name, value)); | |
} | |
Ok(nv_list) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment