1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Reader type for consuming nested TLV records within a DER document.

use crate::{reader::Reader, Error, ErrorKind, Header, Length, Result};

/// Reader type used by [`Reader::read_nested`].
pub struct NestedReader<'i, R> {
    /// Inner reader type.
    inner: &'i mut R,

    /// Nested input length.
    input_len: Length,

    /// Position within the nested input.
    position: Length,
}

impl<'i, 'r, R: Reader<'r>> NestedReader<'i, R> {
    /// Create a new nested reader which can read the given [`Length`].
    pub(crate) fn new(inner: &'i mut R, len: Length) -> Result<Self> {
        if len <= inner.remaining_len() {
            Ok(Self {
                inner,
                input_len: len,
                position: Length::ZERO,
            })
        } else {
            Err(ErrorKind::Incomplete {
                expected_len: (inner.offset() + len)?,
                actual_len: (inner.offset() + inner.remaining_len())?,
            }
            .at(inner.offset()))
        }
    }

    /// Move the position cursor the given length, returning an error if there
    /// isn't enough remaining data in the nested input.
    fn advance_position(&mut self, len: Length) -> Result<()> {
        let new_position = (self.position + len)?;

        if new_position <= self.input_len {
            self.position = new_position;
            Ok(())
        } else {
            Err(ErrorKind::Incomplete {
                expected_len: (self.inner.offset() + len)?,
                actual_len: (self.inner.offset() + self.remaining_len())?,
            }
            .at(self.inner.offset()))
        }
    }
}

impl<'i, 'r, R: Reader<'r>> Reader<'r> for NestedReader<'i, R> {
    fn input_len(&self) -> Length {
        self.input_len
    }

    fn peek_byte(&self) -> Option<u8> {
        if self.is_finished() {
            None
        } else {
            self.inner.peek_byte()
        }
    }

    fn peek_header(&self) -> Result<Header> {
        if self.is_finished() {
            Err(Error::incomplete(self.offset()))
        } else {
            // TODO(tarcieri): handle peeking past nested length
            self.inner.peek_header()
        }
    }

    fn position(&self) -> Length {
        self.position
    }

    fn read_slice(&mut self, len: Length) -> Result<&'r [u8]> {
        self.advance_position(len)?;
        self.inner.read_slice(len)
    }

    fn error(&mut self, kind: ErrorKind) -> Error {
        self.inner.error(kind)
    }

    fn offset(&self) -> Length {
        self.inner.offset()
    }

    fn read_into<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]> {
        self.advance_position(Length::try_from(out.len())?)?;
        self.inner.read_into(out)
    }
}