Function der_parser::der::parse_der_set_defined_g
source · pub fn parse_der_set_defined_g<'a, O, F, E>(
f: F
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>where
F: FnMut(&'a [u8], Header<'a>) -> IResult<&'a [u8], O, E>,
E: ParseError<&'a [u8]> + From<BerError>,
Expand description
Parse a defined SET object (generic version)
Given a parser for set content, apply it to build a DER set and return the remaining bytes and the built object.
The remaining bytes point after the set: any bytes that are part of the sequence but not
parsed are ignored.
The nom combinator all_consuming
can be used to ensure all the content is parsed.
Unlike parse_der_set_defined
, this function allows returning any object or error type,
and also passes the object header to the callback.
Examples
Parsing a defined set with different types:
pub struct MyObject<'a> {
a: u32,
b: &'a [u8],
}
/// Read a DER-encoded object:
/// SET {
/// a INTEGER (0..4294967295),
/// b OCTETSTRING
/// }
fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
parse_der_set_defined_g(
|i:&[u8], _| {
let (i, a) = parse_der_u32(i)?;
let (i, obj) = parse_der_octetstring(i)?;
let b = obj.as_slice().unwrap();
Ok((i, MyObject{ a, b }))
}
)(i)
}
let (rem, v) = parse_myobject(&bytes).expect("parsing failed");