• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Basic Encoding Rules (BER) objects and parser
2 //!
3 //! # BER Objects
4 //!
5 //! The main object of this crate is [`BerObject`]. It contains a header (ber tag, class, and size)
6 //! and content.
7 //!
8 //! To parse primitive objects (for ex. integers or strings), use the `parse_ber_` set of
9 //! functions.
10 //!
11 //! Constructed objects (like sequences, sets or tagged objects) require to use a combinator. This
12 //! combinator takes a function or closure as input, and returns a new, specialized parser.
13 //! See the [nom](https://github.com/geal/nom) parser combinator library for more details on
14 //! combinators.
15 //!
16 //! # Examples
17 //!
18 //! Parse two BER integers:
19 //!
20 //! ```rust
21 //! use der_parser::ber::parse_ber_integer;
22 //!
23 //! let bytes = [ 0x02, 0x03, 0x01, 0x00, 0x01,
24 //!               0x02, 0x03, 0x01, 0x00, 0x00,
25 //! ];
26 //!
27 //! let (rem, obj1) = parse_ber_integer(&bytes).expect("parsing failed");
28 //! let (rem, obj2) = parse_ber_integer(&bytes).expect("parsing failed");
29 //! ```
30 //!
31 //! Parse a BER sequence containing one integer and an octetstring:
32 //!
33 //! ```rust
34 //! use der_parser::ber::*;
35 //!
36 //! let bytes = [ 0x30, 0x0a,
37 //!               0x02, 0x03, 0x01, 0x00, 0x01,
38 //!               0x04, 0x03, 0x62, 0x61, 0x64,
39 //! ];
40 //!
41 //! let (rem, seq) = parse_ber_sequence_defined(|content| {
42 //!         let (rem, obj1) = parse_ber_integer(content)?;
43 //!         let (rem, obj2) = parse_ber_octetstring(rem)?;
44 //!         Ok((rem, vec![obj1, obj2]))
45 //!     })(&bytes)
46 //!     .expect("parsing failed");
47 //! ```
48 
49 mod ber;
50 mod integer;
51 mod multi;
52 mod parser;
53 mod print;
54 #[cfg(feature = "serialize")]
55 mod serialize;
56 mod tagged;
57 
58 pub use crate::ber::ber::*;
59 pub use crate::ber::multi::*;
60 pub use crate::ber::parser::*;
61 pub use crate::ber::print::*;
62 #[cfg(feature = "serialize")]
63 pub use crate::ber::serialize::*;
64 pub use crate::ber::tagged::*;
65 
66 use alloc::borrow::Cow;
67 use alloc::boxed::Box;
68 use alloc::vec::Vec;
69 use core::convert::{Into, TryFrom};
70