• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::ber::*;
2 use crate::error::*;
3 use nom::bytes::complete::take;
4 use nom::combinator::{all_consuming, complete, cut, map};
5 use nom::error::ParseError;
6 use nom::multi::many0;
7 use nom::{Err, IResult};
8 
9 /// Parse a SEQUENCE OF object
10 ///
11 /// Given a subparser for a BER type, parse a sequence of identical objects.
12 ///
13 /// ```rust
14 /// # use der_parser::ber::{parse_ber_integer, parse_ber_sequence_of, BerObject};
15 /// # use der_parser::error::BerResult;
16 /// #
17 /// /// Read a SEQUENCE OF INTEGER
18 /// fn parser(i:&[u8]) -> BerResult<BerObject> {
19 ///     parse_ber_sequence_of(parse_ber_integer)(i)
20 /// }
21 ///
22 /// # let empty = &b""[..];
23 /// # let bytes = [ 0x30, 0x0a,
24 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
25 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
26 /// # ];
27 /// # let expected  = BerObject::from_seq(vec![
28 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
29 /// #     BerObject::from_int_slice(b"\x01\x00\x00"),
30 /// # ]);
31 /// # assert_eq!(parser(&bytes), Ok((empty, expected)));
32 /// let (rem, v) = parser(&bytes).expect("parsing failed");
33 /// ```
parse_ber_sequence_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: Fn(&'a [u8]) -> BerResult,34 pub fn parse_ber_sequence_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult
35 where
36     F: Fn(&'a [u8]) -> BerResult,
37 {
38     map(parse_ber_sequence_of_v(f), BerObject::from_seq)
39 }
40 
41 /// Parse a SEQUENCE OF object (returning a vec)
42 ///
43 /// Given a subparser for a BER type, parse a sequence of identical objects.
44 ///
45 /// This differs from `parse_ber_sequence_of` in the parse function and return type.
46 ///
47 /// ```rust
48 /// # use der_parser::ber::{parse_ber_integer, parse_ber_sequence_of_v, BerObject};
49 /// # use der_parser::error::BerResult;
50 /// #
51 /// /// Read a SEQUENCE OF INTEGER
52 /// fn parser(i:&[u8]) -> BerResult<Vec<BerObject>> {
53 ///     parse_ber_sequence_of_v(parse_ber_integer)(i)
54 /// }
55 ///
56 /// # let empty = &b""[..];
57 /// # let bytes = [ 0x30, 0x0a,
58 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
59 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
60 /// # ];
61 /// # let expected  = vec![
62 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
63 /// #     BerObject::from_int_slice(b"\x01\x00\x00"),
64 /// # ];
65 /// let (rem, v) = parser(&bytes).expect("parsing failed");
66 /// # assert_eq!(v, expected);
67 /// ```
parse_ber_sequence_of_v<'a, T, F, E>( f: F, ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E> where F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>, E: ParseError<&'a [u8]> + From<BerError>,68 pub fn parse_ber_sequence_of_v<'a, T, F, E>(
69     f: F,
70 ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E>
71 where
72     F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
73     E: ParseError<&'a [u8]> + From<BerError>,
74 {
75     let mut subparser = all_consuming(many0(complete(cut(f))));
76     parse_ber_sequence_defined_g(move |data, _| subparser(data))
77 }
78 
79 /// Parse a defined sequence of DER elements (function version)
80 ///
81 /// Given a list of expected parsers, apply them to build a DER sequence and
82 /// return the remaining bytes and the built object.
83 ///
84 /// The remaining bytes point *after* the sequence: any bytes that are part of the sequence but not
85 /// parsed are ignored.
86 ///
87 /// The object header is not available to the parsing function, and the returned type is always a
88 /// `BerObject`.
89 /// For a generic version, see
90 /// [`parse_ber_sequence_defined_g`](fn.parse_ber_sequence_defined_g.html).
91 ///
92 /// # Examples
93 ///
94 /// Parsing a sequence of identical types (same as `parse_ber_sequence_of`):
95 ///
96 /// ```rust
97 /// # use der_parser::ber::{parse_ber_integer, parse_ber_sequence_defined, BerObject};
98 /// # use der_parser::error::BerResult;
99 /// use nom::combinator::complete;
100 /// use nom::multi::many1;
101 ///
102 /// fn localparse_seq(i:&[u8]) -> BerResult {
103 ///     parse_ber_sequence_defined(
104 ///         many1(complete(parse_ber_integer))
105 ///     )(i)
106 /// }
107 ///
108 /// # let empty = &b""[..];
109 /// # let bytes = [ 0x30, 0x0a,
110 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
111 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
112 /// # ];
113 /// # let expected  = BerObject::from_seq(vec![
114 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
115 /// #     BerObject::from_int_slice(b"\x01\x00\x00"),
116 /// # ]);
117 /// # assert_eq!(localparse_seq(&bytes), Ok((empty, expected)));
118 /// let (rem, v) = localparse_seq(&bytes).expect("parsing failed");
119 /// ```
120 ///
121 /// Parsing a defined sequence with different types:
122 ///
123 /// ```rust
124 /// # use der_parser::ber::*;
125 /// # use der_parser::error::BerResult;
126 /// use nom::combinator::map;
127 /// use nom::sequence::tuple;
128 ///
129 /// /// Read a DER-encoded object:
130 /// /// SEQUENCE {
131 /// ///     a INTEGER,
132 /// ///     b OCTETSTRING
133 /// /// }
134 /// fn localparse_seq(i:&[u8]) -> BerResult {
135 ///     parse_ber_sequence_defined(
136 ///         // the nom `tuple` combinator returns a tuple, so we have to map it
137 ///         // to a list
138 ///         map(
139 ///             tuple((parse_ber_integer, parse_ber_octetstring)),
140 ///             |(a, b)| vec![a, b]
141 ///         )
142 ///     )(i)
143 /// }
144 ///
145 /// # let empty = &b""[..];
146 /// # let bytes = [ 0x30, 0x0a,
147 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
148 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
149 /// # ];
150 /// # let expected  = BerObject::from_seq(vec![
151 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
152 /// #     BerObject::from_obj(BerObjectContent::OctetString(b"\x01\x00\x00")),
153 /// # ]);
154 /// # assert_eq!(localparse_seq(&bytes), Ok((empty, expected)));
155 /// let (rem, v) = localparse_seq(&bytes).expect("parsing failed");
156 /// ```
parse_ber_sequence_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: FnMut(&'a [u8]) -> BerResult<Vec<BerObject>>,157 pub fn parse_ber_sequence_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult
158 where
159     F: FnMut(&'a [u8]) -> BerResult<Vec<BerObject>>,
160 {
161     map(
162         parse_ber_sequence_defined_g(move |data, _| f(data)),
163         BerObject::from_seq,
164     )
165 }
166 
167 /// Parse a defined SEQUENCE object (generic function)
168 ///
169 /// Given a parser for sequence content, apply it to build a DER sequence and
170 /// return the remaining bytes and the built object.
171 ///
172 /// The remaining bytes point *after* the sequence: any bytes that are part of the sequence but not
173 /// parsed are ignored.
174 ///
175 /// Unlike `parse_ber_sequence_defined`, this function allows returning any object or error type,
176 /// and also passes the object header to the callback.
177 ///
178 /// # Examples
179 ///
180 /// Parsing a defined sequence with different types:
181 ///
182 /// ```rust
183 /// # use der_parser::ber::*;
184 /// # use der_parser::error::BerResult;
185 /// #
186 /// # #[derive(Debug, PartialEq)]
187 /// pub struct MyObject<'a> {
188 ///     a: u32,
189 ///     b: &'a [u8],
190 /// }
191 ///
192 /// /// Read a DER-encoded object:
193 /// /// SEQUENCE {
194 /// ///     a INTEGER (0..4294967295),
195 /// ///     b OCTETSTRING
196 /// /// }
197 /// fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
198 ///     parse_ber_sequence_defined_g(
199 ///         |i:&[u8], _| {
200 ///             let (i, a) = parse_ber_u32(i)?;
201 ///             let (i, obj) = parse_ber_octetstring(i)?;
202 ///             let b = obj.as_slice().unwrap();
203 ///             Ok((i, MyObject{ a, b }))
204 ///         }
205 ///     )(i)
206 /// }
207 ///
208 /// # let empty = &b""[..];
209 /// # let bytes = [ 0x30, 0x0a,
210 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
211 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
212 /// # ];
213 /// # let expected  = MyObject {
214 /// #   a: 0x010001,
215 /// #   b: &[01, 00, 00]
216 /// # };
217 /// # assert_eq!(parse_myobject(&bytes), Ok((empty, expected)));
218 /// let (rem, v) = parse_myobject(&bytes).expect("parsing failed");
219 /// ```
parse_ber_sequence_defined_g<'a, O, F, E>( mut f: F, ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E> where F: FnMut(&'a [u8], BerObjectHeader<'a>) -> IResult<&'a [u8], O, E>, E: ParseError<&'a [u8]> + From<BerError>,220 pub fn parse_ber_sequence_defined_g<'a, O, F, E>(
221     mut f: F,
222 ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
223 where
224     F: FnMut(&'a [u8], BerObjectHeader<'a>) -> IResult<&'a [u8], O, E>,
225     E: ParseError<&'a [u8]> + From<BerError>,
226 {
227     parse_ber_container(move |i, hdr| {
228         if hdr.tag != BerTag::Sequence {
229             return Err(Err::Error(BerError::InvalidTag.into()));
230         }
231         f(i, hdr)
232     })
233 }
234 
235 /// Parse a SET OF object
236 ///
237 /// Given a subparser for a BER type, parse a set of identical objects.
238 ///
239 /// ```rust
240 /// # use der_parser::ber::{parse_ber_integer, parse_ber_set_of, BerObject};
241 /// # use der_parser::error::BerResult;
242 /// #
243 /// /// Read a SET OF INTEGER
244 /// fn parser(i:&[u8]) -> BerResult<BerObject> {
245 ///     parse_ber_set_of(parse_ber_integer)(i)
246 /// }
247 ///
248 /// # let empty = &b""[..];
249 /// # let bytes = [ 0x31, 0x0a,
250 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
251 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
252 /// # ];
253 /// # let expected  = BerObject::from_set(vec![
254 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
255 /// #     BerObject::from_int_slice(b"\x01\x00\x00"),
256 /// # ]);
257 /// # assert_eq!(parser(&bytes), Ok((empty, expected)));
258 /// let (rem, v) = parser(&bytes).expect("parsing failed");
259 /// ```
parse_ber_set_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: Fn(&'a [u8]) -> BerResult,260 pub fn parse_ber_set_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult
261 where
262     F: Fn(&'a [u8]) -> BerResult,
263 {
264     map(parse_ber_set_of_v(f), BerObject::from_set)
265 }
266 
267 /// Parse a SET OF object (returning a vec)
268 ///
269 /// Given a subparser for a BER type, parse a set of identical objects.
270 ///
271 /// This differs from `parse_ber_set_of` in the parse function and return type.
272 ///
273 /// ```rust
274 /// # use der_parser::ber::{parse_ber_integer, parse_ber_set_of_v, BerObject};
275 /// # use der_parser::error::BerResult;
276 /// #
277 /// /// Read a SET OF INTEGER
278 /// fn parser(i:&[u8]) -> BerResult<Vec<BerObject>> {
279 ///     parse_ber_set_of_v(parse_ber_integer)(i)
280 /// }
281 ///
282 /// # let empty = &b""[..];
283 /// # let bytes = [ 0x31, 0x0a,
284 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
285 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
286 /// # ];
287 /// # let expected  = vec![
288 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
289 /// #     BerObject::from_int_slice(b"\x01\x00\x00"),
290 /// # ];
291 /// let (rem, v) = parser(&bytes).expect("parsing failed");
292 /// # assert_eq!(v, expected);
293 /// ```
parse_ber_set_of_v<'a, T, F, E>(f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E> where F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>, E: ParseError<&'a [u8]> + From<BerError>,294 pub fn parse_ber_set_of_v<'a, T, F, E>(f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E>
295 where
296     F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
297     E: ParseError<&'a [u8]> + From<BerError>,
298 {
299     let mut subparser = all_consuming(many0(complete(cut(f))));
300     parse_ber_set_defined_g(move |data, _| subparser(data))
301 }
302 
303 /// Parse a defined set of DER elements (function version)
304 ///
305 /// Given a list of expected parsers, apply them to build a DER set and
306 /// return the remaining bytes and the built object.
307 ///
308 /// The remaining bytes point *after* the set: any bytes that are part of the sequence but not
309 /// parsed are ignored.
310 /// The nom combinator `all_consuming` can be used to ensure all the content is parsed.
311 ///
312 /// The object header is not available to the parsing function, and the returned type is always a
313 /// `BerObject`.
314 /// For a generic version, see [`parse_ber_set_defined_g`](fn.parse_ber_set_defined_g.html).
315 ///
316 /// # Examples
317 ///
318 /// Parsing a set of identical types (same as `parse_ber_set_of`):
319 ///
320 /// ```rust
321 /// # use der_parser::ber::{parse_ber_integer, parse_ber_set_defined, BerObject};
322 /// # use der_parser::error::BerResult;
323 /// use nom::combinator::complete;
324 /// use nom::multi::many1;
325 ///
326 /// fn localparse_seq(i:&[u8]) -> BerResult {
327 ///     parse_ber_set_defined(
328 ///         many1(complete(parse_ber_integer))
329 ///     )(i)
330 /// }
331 ///
332 /// # let empty = &b""[..];
333 /// # let bytes = [ 0x31, 0x0a,
334 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
335 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
336 /// # ];
337 /// # let expected  = BerObject::from_set(vec![
338 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
339 /// #     BerObject::from_int_slice(b"\x01\x00\x00"),
340 /// # ]);
341 /// # assert_eq!(localparse_seq(&bytes), Ok((empty, expected)));
342 /// let (rem, v) = localparse_seq(&bytes).expect("parsing failed");
343 /// ```
344 ///
345 /// Parsing a defined set with different types:
346 ///
347 /// ```rust
348 /// # use der_parser::ber::*;
349 /// # use der_parser::error::BerResult;
350 /// use nom::combinator::map;
351 /// use nom::sequence::tuple;
352 ///
353 /// /// Read a DER-encoded object:
354 /// /// SET {
355 /// ///     a INTEGER,
356 /// ///     b OCTETSTRING
357 /// /// }
358 /// fn localparse_set(i:&[u8]) -> BerResult {
359 ///     parse_ber_set_defined(
360 ///         // the nom `tuple` combinator returns a tuple, so we have to map it
361 ///         // to a list
362 ///         map(
363 ///             tuple((parse_ber_integer, parse_ber_octetstring)),
364 ///             |(a, b)| vec![a, b]
365 ///         )
366 ///     )(i)
367 /// }
368 ///
369 /// # let empty = &b""[..];
370 /// # let bytes = [ 0x31, 0x0a,
371 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
372 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
373 /// # ];
374 /// # let expected  = BerObject::from_set(vec![
375 /// #     BerObject::from_int_slice(b"\x01\x00\x01"),
376 /// #     BerObject::from_obj(BerObjectContent::OctetString(b"\x01\x00\x00")),
377 /// # ]);
378 /// # assert_eq!(localparse_set(&bytes), Ok((empty, expected)));
379 /// let (rem, v) = localparse_set(&bytes).expect("parsing failed");
380 /// ```
parse_ber_set_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: FnMut(&'a [u8]) -> BerResult<Vec<BerObject>>,381 pub fn parse_ber_set_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult
382 where
383     F: FnMut(&'a [u8]) -> BerResult<Vec<BerObject>>,
384 {
385     map(
386         parse_ber_set_defined_g(move |data, _| f(data)),
387         BerObject::from_set,
388     )
389 }
390 
391 /// Parse a defined SET object (generic version)
392 ///
393 /// Given a parser for set content, apply it to build a DER set and
394 /// return the remaining bytes and the built object.
395 ///
396 /// The remaining bytes point *after* the set: any bytes that are part of the sequence but not
397 /// parsed are ignored.
398 /// The nom combinator `all_consuming` can be used to ensure all the content is parsed.
399 ///
400 /// Unlike `parse_ber_set_defined`, this function allows returning any object or error type,
401 /// and also passes the object header to the callback.
402 ///
403 /// # Examples
404 ///
405 /// Parsing a defined set with different types:
406 ///
407 /// ```rust
408 /// # use der_parser::ber::*;
409 /// # use der_parser::error::BerResult;
410 /// #
411 /// # #[derive(Debug, PartialEq)]
412 /// pub struct MyObject<'a> {
413 ///     a: u32,
414 ///     b: &'a [u8],
415 /// }
416 ///
417 /// /// Read a DER-encoded object:
418 /// /// SET {
419 /// ///     a INTEGER (0..4294967295),
420 /// ///     b OCTETSTRING
421 /// /// }
422 /// fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
423 ///     parse_ber_set_defined_g(
424 ///         |i:&[u8], _| {
425 ///             let (i, a) = parse_ber_u32(i)?;
426 ///             let (i, obj) = parse_ber_octetstring(i)?;
427 ///             let b = obj.as_slice().unwrap();
428 ///             Ok((i, MyObject{ a, b }))
429 ///         }
430 ///     )(i)
431 /// }
432 ///
433 /// # let empty = &b""[..];
434 /// # let bytes = [ 0x31, 0x0a,
435 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
436 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
437 /// # ];
438 /// # let expected  = MyObject {
439 /// #   a: 0x010001,
440 /// #   b: &[01, 00, 00]
441 /// # };
442 /// # assert_eq!(parse_myobject(&bytes), Ok((empty, expected)));
443 /// let (rem, v) = parse_myobject(&bytes).expect("parsing failed");
444 /// ```
parse_ber_set_defined_g<'a, O, F, E>( mut f: F, ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E> where F: FnMut(&'a [u8], BerObjectHeader<'a>) -> IResult<&'a [u8], O, E>, E: ParseError<&'a [u8]> + From<BerError>,445 pub fn parse_ber_set_defined_g<'a, O, F, E>(
446     mut f: F,
447 ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
448 where
449     F: FnMut(&'a [u8], BerObjectHeader<'a>) -> IResult<&'a [u8], O, E>,
450     E: ParseError<&'a [u8]> + From<BerError>,
451 {
452     parse_ber_container(move |i, hdr| {
453         if hdr.tag != BerTag::Set {
454             return Err(Err::Error(BerError::InvalidTag.into()));
455         }
456         f(i, hdr)
457     })
458 }
459 
460 /// Parse a BER object and apply provided function to content
461 ///
462 /// Given a parser for content, read BER object header and apply parser to
463 /// return the remaining bytes and the parser result.
464 ///
465 /// The remaining bytes point *after* the content: any bytes that are part of the content but not
466 /// parsed are ignored.
467 /// The nom combinator `all_consuming` can be used to ensure all the content is parsed.
468 ///
469 /// This function is mostly intended for structured objects, but can be used for any valid BER
470 /// object.
471 ///
472 /// # Examples
473 ///
474 /// Parsing a defined sequence with different types:
475 ///
476 /// ```rust
477 /// # use der_parser::ber::*;
478 /// # use der_parser::error::{BerError, BerResult};
479 /// #
480 /// # #[derive(Debug, PartialEq)]
481 /// pub struct MyObject<'a> {
482 ///     a: u32,
483 ///     b: &'a [u8],
484 /// }
485 ///
486 /// /// Read a DER-encoded object:
487 /// /// SEQUENCE {
488 /// ///     a INTEGER (0..4294967295),
489 /// ///     b OCTETSTRING
490 /// /// }
491 /// fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
492 ///     parse_ber_container(
493 ///         |i: &[u8], hdr: BerObjectHeader| {
494 ///             if hdr.tag != BerTag::Sequence {
495 ///                 return Err(nom::Err::Error(BerError::BerTypeError.into()));
496 ///             }
497 ///             let (i, a) = parse_ber_u32(i)?;
498 ///             let (i, obj) = parse_ber_octetstring(i)?;
499 ///             let b = obj.as_slice().unwrap();
500 ///             Ok((i, MyObject{ a, b }))
501 ///         }
502 ///     )(i)
503 /// }
504 ///
505 /// # let empty = &b""[..];
506 /// # let bytes = [ 0x30, 0x0a,
507 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
508 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
509 /// # ];
510 /// # let expected  = MyObject {
511 /// #   a: 0x010001,
512 /// #   b: &[01, 00, 00]
513 /// # };
514 /// # assert_eq!(parse_myobject(&bytes), Ok((empty, expected)));
515 /// let (rem, v) = parse_myobject(&bytes).expect("parsing failed");
516 /// ```
parse_ber_container<'a, O, F, E>(mut f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E> where F: FnMut(&'a [u8], BerObjectHeader<'a>) -> IResult<&'a [u8], O, E>, E: ParseError<&'a [u8]> + From<BerError>,517 pub fn parse_ber_container<'a, O, F, E>(mut f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
518 where
519     F: FnMut(&'a [u8], BerObjectHeader<'a>) -> IResult<&'a [u8], O, E>,
520     E: ParseError<&'a [u8]> + From<BerError>,
521 {
522     move |i: &[u8]| {
523         let (i, hdr) = ber_read_element_header(i).map_err(nom::Err::convert)?;
524         let (i, data) = match hdr.len {
525             BerSize::Definite(len) => take(len)(i)?,
526             BerSize::Indefinite => {
527                 ber_get_object_content(i, &hdr, MAX_RECURSION).map_err(nom::Err::convert)?
528             }
529         };
530         let (_rest, v) = f(data, hdr)?;
531         Ok((i, v))
532     }
533 }
534