Macro der_parser::parse_der_tagged[][src]

macro_rules! parse_der_tagged {
    ($i : expr, EXPLICIT $tag : expr, $f : ident) => { ... };
    ($i : expr, EXPLICIT $tag : expr, $submac : ident! ($($args : tt) *)) => { ... };
    ($i : expr, IMPLICIT $tag : expr, $type : expr) => { ... };
    ($i : expr, $tag : expr, $f : ident) => { ... };
}
Expand description

Parse a tagged DER element

Read a tagged DER element using the provided function.

The returned object is either the object returned by the subparser, or a nom error. Unlike parse_der_explicit or parse_der_implicit, the returned values are not encapsulated in a BerObject (they are directly returned, without the tag).

To specify the kind of tag, use the EXPLICIT or IMPLICIT keyword. If no keyword is specified, the parsing is EXPLICIT by default.

When parsing IMPLICIT values, the third argument is a DerTag defining the subtype of the object.

Examples

The following parses [2] INTEGER:

fn parse_int_explicit(i:&[u8]) -> BerResult<u32> {
    map_res!(
        i,
        parse_der_tagged!(EXPLICIT 2, parse_ber_integer),
        |x: BerObject| x.as_tagged()?.2.as_u32()
    )
}

let bytes = &[0xa2, 0x05, 0x02, 0x03, 0x01, 0x00, 0x01];
let res = parse_int_explicit(bytes);
match res {
    Ok((rem,val)) => {
        assert!(rem.is_empty());
        assert_eq!(val, 0x10001);
    },
    Err(e) => panic!("parse_int_explicit failed: {:?}", e),
}

The following parses [2] IMPLICIT INTEGER:

fn parse_int_implicit(i:&[u8]) -> BerResult<u32> {
    map_res!(
        i,
        parse_der_tagged!(IMPLICIT 2, BerTag::Integer),
        |x: BerObject| x.as_u32()
    )
}

let bytes = &[0x82, 0x03, 0x01, 0x00, 0x01];
let res = parse_int_implicit(bytes);
match res {
    Ok((rem,val)) => {
        assert!(rem.is_empty());
        assert_eq!(val, 0x10001);
    },
    _ => assert!(false)
}