Lines Matching full:oid
1 //! Object ID (OID) representations.
3 //! The parser does not copy oids when parsing. The [Oid struct](struct.Oid.html)
4 //! only has a reference to the DER encoded form of the oid.
6 //! # The `der_parser::oid!` macro
9 //! procedural macro `oid!`. The macro can be used the following ways:
11 //! - `oid!(1.4.42.23)`: Create a const expression for the corresponding `Oid<'static>`
12 //! - `oid!(rel 42.23)`: Create a const expression for the corresponding relative `Oid<'static>`
13 //! - `oid!(raw 1.4.42.23)`/`oid!(raw rel 42.23)`: Obtain the DER encoded form as a byte array.
17 //! Comparing a parsed oid to a static oid is probably the most common
18 //! thing done with oids in your code. The `oid!` macro can be used in expression positions for
21 //! use der_parser::{oid, oid::Oid};
23 //! # let some_oid: Oid<'static> = oid!(1.2.456);
24 //! const SOME_STATIC_OID: Oid<'static> = oid!(1.2.456);
27 //! To get a relative Oid use `oid!(rel 1.2)`.
31 //! the `oid` macro can not directly be used in patterns, also not through constants.
34 //! # use der_parser::{oid, oid::Oid};
35 //! # let some_oid: Oid<'static> = oid!(1.2.456);
36 //! const SOME_OID: Oid<'static> = oid!(1.2.456);
37 //! if some_oid == SOME_OID || some_oid == oid!(1.2.456) {
42 //! const SOME_OID_RAW: &[u8] = &oid!(raw 1.2.456);
48 //! *Attention*, be aware that the latter version might not handle the case of a relative oid corre…
76 /// Object ID (OID) representation which can be relative or non-relative.
77 /// An example for an oid in string representation is "1.2.840.113549.1.1.5".
81 /// This library contains a procedural macro `oid` which can be used to
82 /// create oids. For example `oid!(1.2.44.233)` or `oid!(rel 44.233)`
85 pub struct Oid<'a> { struct
103 impl<'a> Oid<'a> { argument
104 /// Create an OID from the ASN.1 DER encoded form. See the [module documentation](index.html)
106 pub const fn new(asn1: Cow<'a, [u8]>) -> Oid { in new() argument
107 Oid { in new()
113 …/// Create a relative OID from the ASN.1 DER encoded form. See the [module documentation](index.ht…
115 pub const fn new_relative(asn1: Cow<'a, [u8]>) -> Oid { in new_relative() argument
116 Oid { in new_relative()
122 /// Build an OID from an array of object identifier components.
126 pub fn from<'b>(s: &'b [u64]) -> Result<Oid<'static>, ParseError> { in from()
129 return Ok(Oid { in from()
144 Ok(Oid { in from()
150 /// Build a relative OID from an array of object identifier components.
151 pub fn from_relative<'b>(s: &'b [u64]) -> Result<Oid<'static>, ParseError> { in from_relative()
156 Ok(Oid { in from_relative()
162 /// Create a deep copy of the oid.
164 /// This method allocates data on the heap. The returned oid
167 /// Cloning the returned oid does again allocate data.
168 pub fn to_owned(&self) -> Oid<'static> { in to_owned()
169 Oid { in to_owned()
175 /// Get the encoded oid without the header.
180 /// Convert the OID to a string representation.
189 /// Convert the OID to a string representation.
217 oid: self, in iter_bigint()
253 oid: self, in iter()
265 oid: &'a Oid<'a>, field
277 if self.pos == self.oid.asn1.len() { in next()
280 if !self.oid.relative { in next()
284 return Some((self.oid.asn1[0] / 40).into()); in next()
287 if self.oid.asn1[0] == 0 && self.oid.asn1.len() == 1 { in next()
290 return Some((self.oid.asn1[0] % 40).into()); in next()
295 for o in self.oid.asn1[self.pos..].iter() { in next()
311 if self.oid.relative { in len()
312 self.oid.asn1.iter().filter(|o| (*o >> 7) == 0u8).count() in len()
313 } else if self.oid.asn1.len() == 0 { in len()
315 } else if self.oid.asn1.len() == 1 { in len()
316 if self.oid.asn1[0] == 0 { in len()
322 2 + self.oid.asn1[2..] in len()
331 self.oid.asn1.is_empty() in is_empty()
335 impl<'a> fmt::Display for Oid<'a> { implementation
344 impl<'a> fmt::Debug for Oid<'a> { implementation
346 f.write_str("OID(")?; in fmt()
347 <Oid as fmt::Display>::fmt(self, f)?; in fmt()
352 impl<'a> FromStr for Oid<'a> { implementation
358 .and_then(|v| Oid::from(&v)) in from_str()
364 use crate::oid::Oid;
371 let oid = Oid::from(&[1, 2, 840, 113_549, 1, 1, 5]).unwrap(); in test_oid_fmt() localVariable
372 assert_eq!(format!("{}", oid), "1.2.840.113549.1.1.5".to_owned()); in test_oid_fmt()
373 assert_eq!(format!("{:?}", oid), "OID(1.2.840.113549.1.1.5)".to_owned()); in test_oid_fmt()
375 let oid = Oid::from_relative(&[840, 113_549, 1, 1, 5]).unwrap(); in test_oid_fmt() localVariable
377 assert_eq!(byte_ref.as_ref(), oid.asn1.as_ref()); in test_oid_fmt()
378 assert_eq!(format!("{}", oid), "rel. 840.113549.1.1.5".to_owned()); in test_oid_fmt()
380 format!("{:?}", oid), in test_oid_fmt()
381 "OID(rel. 840.113549.1.1.5)".to_owned() in test_oid_fmt()
389 assert_eq!(Oid::new(Cow::Borrowed(&[])).iter_bigint().len(), 0); in test_iter_len()
390 assert_eq!(Oid::from(&[0]).unwrap().iter_bigint().len(), 1); in test_iter_len()
391 assert_eq!(Oid::from(&[1, 2]).unwrap().iter_bigint().len(), 2); in test_iter_len()
393 Oid::from(&[1, 29, 459, 342]).unwrap().iter_bigint().len(), in test_iter_len()
397 Oid::from_relative(&[459, 342]).unwrap().iter_bigint().len(), in test_iter_len()
402 assert_eq!(Oid::new(Cow::Borrowed(&[])).iter().unwrap().len(), 0); in test_iter_len()
403 assert_eq!(Oid::from(&[0]).unwrap().iter().unwrap().len(), 1); in test_iter_len()
404 assert_eq!(Oid::from(&[1, 2]).unwrap().iter().unwrap().len(), 2); in test_iter_len()
406 Oid::from(&[1, 29, 459, 342]).unwrap().iter().unwrap().len(), in test_iter_len()
410 Oid::from_relative(&[459, 342]) in test_iter_len()
422 let oid_ref = Oid::from(&[1, 2, 840, 113_549, 1, 1, 5]).unwrap(); in test_oid_from_str()
424 let oid = Oid::from_str("1.2.840.113549.1.1.5").unwrap(); in test_oid_from_str() localVariable
425 assert_eq!(byte_ref.as_ref(), oid.asn1.as_ref()); in test_oid_from_str()
426 assert_eq!(oid_ref, oid); in test_oid_from_str()
429 /// This test case will test an OID beginning with two zero
435 let oid = Oid::from(&[0, 0, 17, 773, 1, 1, 1]).unwrap(); in test_itu_t_rec_oid() localVariable
436 assert_eq!(format!("{}", oid), "0.0.17.773.1.1.1".to_owned()); in test_itu_t_rec_oid()
437 assert_eq!(format!("{:?}", oid), "OID(0.0.17.773.1.1.1)".to_owned()); in test_itu_t_rec_oid()
448 let oid_raw = Oid::new(Cow::Borrowed(&[0])); in test_zero_oid()
455 let oid_raw = Oid::new(Cow::Borrowed(&[0])); in test_zero_oid()
460 let oid_from = Oid::from(&[0]).unwrap(); in test_zero_oid()