rocket::mtls::x509::ber

Function parse_ber_sequence_defined_g

Source
pub fn parse_ber_sequence_defined_g<'a, O, F, E>(f: F) -> impl FnMut(&'a [u8])
where F: FnMut(&'a [u8], Header<'a>) -> Result<(&'a [u8], O), Err<E>>, E: ParseError<&'a [u8]> + From<Error>,
Available on crate feature mtls only.
Expand description

Parse a defined SEQUENCE object (generic function)

Given a parser for sequence content, apply it to build a DER sequence and return the remaining bytes and the built object.

The remaining bytes point after the sequence: any bytes that are part of the sequence but not parsed are ignored.

Unlike parse_ber_sequence_defined, this function allows returning any object or error type, and also passes the object header to the callback.

ยงExamples

Parsing a defined sequence with different types:

pub struct MyObject<'a> {
    a: u32,
    b: &'a [u8],
}

/// Read a DER-encoded object:
/// SEQUENCE {
///     a INTEGER (0..4294967295),
///     b OCTETSTRING
/// }
fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
    parse_ber_sequence_defined_g(
        |i:&[u8], _| {
            let (i, a) = parse_ber_u32(i)?;
            let (i, obj) = parse_ber_octetstring(i)?;
            let b = obj.as_slice().unwrap();
            Ok((i, MyObject{ a, b }))
        }
    )(i)
}

let (rem, v) = parse_myobject(&bytes).expect("parsing failed");