|
95 | 95 | //! // "heapless" usage when the `alloc` feature is disabled. |
96 | 96 | //! use der::{ |
97 | 97 | //! asn1::{AnyRef, ObjectIdentifier}, |
98 | | -//! DecodeValue, Decode, SliceReader, Encode, Header, Reader, Sequence |
| 98 | +//! Decode, DecodeValue, Encode, EncodeValue, Header, Length, |
| 99 | +//! Reader, Sequence, SliceReader, Writer |
99 | 100 | //! }; |
100 | 101 | //! |
101 | 102 | //! /// X.509 `AlgorithmIdentifier`. |
|
113 | 114 | //! type Error = der::Error; |
114 | 115 | //! |
115 | 116 | //! fn decode_value<R: Reader<'a>>(reader: &mut R, _header: Header) -> der::Result<Self> { |
116 | | -//! // The `der::Decoder::Decode` method can be used to decode any |
| 117 | +//! // The `der::Decode::decode` method can be used to decode any |
117 | 118 | //! // type which impls the `Decode` trait, which is impl'd for |
118 | 119 | //! // all of the ASN.1 built-in types in the `der` crate. |
119 | | -//! // |
120 | | -//! // Note that if your struct's fields don't contain an ASN.1 |
121 | | -//! // built-in type specifically, there are also helper methods |
122 | | -//! // for all of the built-in types supported by this library |
123 | | -//! // which can be used to select a specific type. |
124 | | -//! // |
125 | | -//! // For example, another way of decoding this particular field, |
126 | | -//! // which contains an ASN.1 `OBJECT IDENTIFIER`, is by calling |
127 | | -//! // `decoder.oid()`. Similar methods are defined for other |
128 | | -//! // ASN.1 built-in types. |
129 | 120 | //! let algorithm = reader.decode()?; |
130 | 121 | //! |
131 | 122 | //! // This field contains an ASN.1 `OPTIONAL` type. The `der` crate |
132 | 123 | //! // maps this directly to Rust's `Option` type and provides |
133 | 124 | //! // impls of the `Decode` and `Encode` traits for `Option`. |
134 | | -//! // To explicitly request an `OPTIONAL` type be decoded, use the |
135 | | -//! // `decoder.optional()` method. |
136 | 125 | //! let parameters = reader.decode()?; |
137 | 126 | //! |
138 | | -//! // The value returned from the provided `FnOnce` will be |
139 | | -//! // returned from the `any.sequence(...)` call above. |
| 127 | +//! // The value returned from this `decode_value` will be |
| 128 | +//! // returned from the `AlgorithmIdentifier::decode` call, unchanged. |
140 | 129 | //! // Note that the entire sequence body *MUST* be consumed |
141 | 130 | //! // or an error will be returned. |
142 | 131 | //! Ok(Self { algorithm, parameters }) |
143 | 132 | //! } |
144 | 133 | //! } |
145 | 134 | //! |
146 | | -//! impl<'a> ::der::EncodeValue for AlgorithmIdentifier<'a> { |
147 | | -//! fn value_len(&self) -> ::der::Result<::der::Length> { |
| 135 | +//! impl<'a> EncodeValue for AlgorithmIdentifier<'a> { |
| 136 | +//! fn value_len(&self) -> der::Result<Length> { |
| 137 | +//! // Length of the Value part in Tag-Length-Value structure |
| 138 | +//! // is calculated for every TLV header in the tree. |
| 139 | +//! // Therefore, in this example `AlgorithmIdentifier::value_len` |
| 140 | +//! // will be called once, originating from `Encode::to_der()`. |
148 | 141 | //! self.algorithm.encoded_len()? + self.parameters.encoded_len()? |
149 | 142 | //! } |
150 | 143 | //! |
151 | | -//! fn encode_value(&self, writer: &mut impl ::der::Writer) -> ::der::Result<()> { |
| 144 | +//! fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> { |
152 | 145 | //! self.algorithm.encode(writer)?; |
153 | 146 | //! self.parameters.encode(writer)?; |
154 | 147 | //! Ok(()) |
|
171 | 164 | //! // which impls `Sequence` can be serialized by calling `Encode::to_der()`. |
172 | 165 | //! // |
173 | 166 | //! // If you would prefer to avoid allocations, you can create a byte array |
174 | | -//! // as backing storage instead, pass that to `der::Encoder::new`, and then |
175 | | -//! // encode the `parameters` value using `encoder.encode(parameters)`. |
| 167 | +//! // as backing storage instead, pass that to `der::SliceWriter::new`, and then |
| 168 | +//! // encode the `parameters` value using `writer.encode(parameters)`. |
176 | 169 | //! let der_encoded_parameters = parameters.to_der().unwrap(); |
177 | 170 | //! |
178 | 171 | //! let algorithm_identifier = AlgorithmIdentifier { |
179 | 172 | //! // OID for `id-ecPublicKey`, if you're curious |
180 | 173 | //! algorithm: "1.2.840.10045.2.1".parse().unwrap(), |
181 | 174 | //! |
182 | | -//! // `Any<'a>` impls `TryFrom<&'a [u8]>`, which parses the provided |
| 175 | +//! // `AnyRef<'a>` impls `TryFrom<&'a [u8]>`, which parses the provided |
183 | 176 | //! // slice as an ASN.1 DER-encoded message. |
184 | 177 | //! parameters: Some(der_encoded_parameters.as_slice().try_into().unwrap()) |
185 | 178 | //! }; |
|
188 | 181 | //! // allocating a `Vec<u8>` for storage. |
189 | 182 | //! // |
190 | 183 | //! // As mentioned earlier, if you don't have the `alloc` feature enabled you |
191 | | -//! // can create a fix-sized array instead, then call `Encoder::new` with a |
| 184 | +//! // can create a fix-sized array instead, then call `SliceWriter::new` with a |
192 | 185 | //! // reference to it, then encode the message using |
193 | | -//! // `encoder.encode(algorithm_identifier)`, then finally `encoder.finish()` |
| 186 | +//! // `writer.encode(algorithm_identifier)`, then finally `writer.finish()` |
194 | 187 | //! // to obtain a byte slice containing the encoded message. |
195 | 188 | //! let der_encoded_algorithm_identifier = algorithm_identifier.to_der().unwrap(); |
196 | 189 | //! |
197 | | -//! // Deserialize the `AlgorithmIdentifier` we just serialized from ASN.1 DER |
198 | | -//! // using `der::Decode::from_bytes`. |
| 190 | +//! // Deserialize the `AlgorithmIdentifier` bytes we just serialized from ASN.1 DER |
| 191 | +//! // using `der::Decode::from_der`. |
199 | 192 | //! let decoded_algorithm_identifier = AlgorithmIdentifier::from_der( |
200 | 193 | //! &der_encoded_algorithm_identifier |
201 | 194 | //! ).unwrap(); |
|
0 commit comments