13 releases

0.0.14 Jan 4, 2020
0.0.13 Jul 26, 2019
0.0.11 Dec 9, 2018
0.0.10 Sep 5, 2017
0.0.4 Jul 5, 2017

#1580 in Parser implementations

Download history 1201/week @ 2024-03-14 1084/week @ 2024-03-21 834/week @ 2024-03-28 1329/week @ 2024-04-04 1273/week @ 2024-04-11 736/week @ 2024-04-18 1088/week @ 2024-04-25 830/week @ 2024-05-02 1099/week @ 2024-05-09 1125/week @ 2024-05-16 1074/week @ 2024-05-23 1072/week @ 2024-05-30 915/week @ 2024-06-06 926/week @ 2024-06-13 959/week @ 2024-06-20 1004/week @ 2024-06-27

3,991 downloads per month
Used in 18 crates (4 directly)

ISC license

28KB
507 lines

derp

derp is DER Parser.

derp doesn't panic. derp is minimal.

Example

extern crate derp;
extern crate untrusted;

use derp::{Tag, Der};
use untrusted::Input;

const MY_DATA: &'static [u8] = &[
    0x30, 0x18,                                             // sequence
        0x05, 0x00,                                         // null
        0x30, 0x0e,                                         // sequence
            0x02, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // x
            0x02, 0x04, 0x0a, 0x0b, 0x0c, 0x0d,             // y
        0x03, 0x04, 0x00, 0xff, 0xff, 0xff,                 // bits
];

fn main() {
    let input = Input::from(MY_DATA);
    let (x, y, bits) = input.read_all(derp::Error::Read, |input| {
        derp::nested(input, Tag::Sequence, |input| {
            derp::read_null(input)?;
            let (x, y) = derp::nested(input, Tag::Sequence, |input| {
                let x = derp::positive_integer(input)?;
                let y = derp::positive_integer(input)?;
                Ok((x.as_slice_less_safe(), y.as_slice_less_safe()))
            })?;
            let bits = derp::bit_string_with_no_unused_bits(input)?;
            Ok((x, y, bits.as_slice_less_safe()))
        })
    }).unwrap();

    assert_eq!(x, &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
    assert_eq!(y, &[0x0a, 0x0b, 0x0c, 0x0d]);
    assert_eq!(bits, &[0xff, 0xff, 0xff]);

    let mut buf = Vec::new();
    {
        let mut der = Der::new(&mut buf);
        der.sequence(|der| {
            der.null()?;
            der.sequence(|der| {
                der.integer(x)?;
                der.integer(y)
            })?;
            der.bit_string(0, bits)
        }).unwrap();
    }

    assert_eq!(buf.as_slice(), MY_DATA);
}

Credit

A significant portion of derp was pulled from the crypto library ring by @briansmith. If you like this lib, thank him.

License

This work is licensed under the ISC license. See LICENSE.

Dependencies

~29KB