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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use core::convert::TryInto;
use crate::{Decodable, ErrorKind, Length, Result};

/// SIMPLE-TLV decoder.
#[derive(Debug)]
pub struct Decoder<'a> {
    /// Byte slice being decoded.
    ///
    /// In the event an error was previously encountered this will be set to
    /// `None` to prevent further decoding while in a bad state.
    bytes: Option<&'a [u8]>,

    /// Position within the decoded slice.
    position: Length,
}

impl<'a> Decoder<'a> {
    /// Create a new decoder for the given byte slice.
    pub fn new(bytes: &'a [u8]) -> Self {
        Self {
            bytes: Some(bytes),
            position: Length::zero(),
        }
    }

    /// Decode a value which impls the [`Decodable`] trait.
    pub fn decode<T: Decodable<'a>>(&mut self) -> Result<T> {
        if self.is_failed() {
            self.error(ErrorKind::Failed)?;
        }

        T::decode(self).map_err(|e| {
            self.bytes.take();
            e.nested(self.position)
        })
    }

    /// Decode a TaggedValue with tag checked to be as expected, returning the value
    pub fn decode_tagged_value<V: Decodable<'a>>(&mut self, tag: crate::Tag) -> Result<V> {
        let tagged: crate::TaggedSlice = self.decode()?;
        tagged.tag().assert_eq(tag)?;
        Self::new(tagged.as_bytes()).decode()
    }

    /// Decode a TaggedSlice with tag checked to be as expected, returning the value
    pub fn decode_tagged_slice(&mut self, tag: crate::Tag) -> Result<&'a [u8]> {
        let tagged: crate::TaggedSlice = self.decode()?;
        tagged.tag().assert_eq(tag)?;
        Ok(tagged.as_bytes())
    }

    /// Return an error with the given [`ErrorKind`], annotating it with
    /// context about where the error occurred.
    pub fn error<T>(&mut self, kind: ErrorKind) -> Result<T> {
        self.bytes.take();
        Err(kind.at(self.position))
    }

    /// Did the decoding operation fail due to an error?
    pub fn is_failed(&self) -> bool {
        self.bytes.is_none()
    }

    /// Finish decoding, returning the given value if there is no
    /// remaining data, or an error otherwise
    pub fn finish<T>(self, value: T) -> Result<T> {
        if self.is_failed() {
            Err(ErrorKind::Failed.at(self.position))
        } else if !self.is_finished() {
            Err(ErrorKind::TrailingData {
                decoded: self.position,
                remaining: self.remaining_len()?,
            }
            .at(self.position))
        } else {
            Ok(value)
        }
    }

    /// Have we decoded all of the bytes in this [`Decoder`]?
    ///
    /// Returns `false` if we're not finished decoding or if a fatal error
    /// has occurred.
    pub fn is_finished(&self) -> bool {
        self.remaining().map(|rem| rem.is_empty()).unwrap_or(false)
    }

    /// Decode a single byte, updating the internal cursor.
    pub(crate) fn byte(&mut self) -> Result<u8> {
        match self.bytes(1u8)? {
            [byte] => Ok(*byte),
            _ => self.error(ErrorKind::Truncated),
        }
    }

    /// Obtain a slice of bytes of the given length from the current cursor
    /// position, or return an error if we have insufficient data.
    pub(crate) fn bytes(&mut self, len: impl TryInto<Length>) -> Result<&'a [u8]> {
        if self.is_failed() {
            self.error(ErrorKind::Failed)?;
        }

        let len = len
            .try_into()
            .or_else(|_| self.error(ErrorKind::Overflow))?;

        let result = self
            .remaining()?
            .get(..len.to_usize())
            .ok_or(ErrorKind::Truncated)?;

        self.position = (self.position + len)?;
        Ok(result)
    }

    /// Obtain the remaining bytes in this decoder from the current cursor
    /// position.
    fn remaining(&self) -> Result<&'a [u8]> {
        self.bytes
            .and_then(|b| b.get(self.position.into()..))
            .ok_or_else(|| ErrorKind::Truncated.at(self.position))
    }

    /// Get the number of bytes still remaining in the buffer.
    fn remaining_len(&self) -> Result<Length> {
        self.remaining()?.len().try_into()
    }
}

impl<'a> From<&'a [u8]> for Decoder<'a> {
    fn from(bytes: &'a [u8]) -> Decoder<'a> {
        Decoder::new(bytes)
    }
}

#[cfg(test)]
mod tests {
    use core::convert::TryFrom;
    use crate::{Decodable, Tag, TaggedSlice};

    #[test]
    fn zero_length() {
        let buf: &[u8] = &[0x2A, 0x00];
        let ts = TaggedSlice::from_bytes(buf).unwrap();
        assert_eq!(ts, TaggedSlice::from(Tag::try_from(42).unwrap(), &[]).unwrap());
    }
}
// #[cfg(test)]
// mod tests {
//     use super::Decoder;
//     use crate::{Decodable, ErrorKind, Length, Tag};

//     #[test]
//     fn truncated_message() {
//         let mut decoder = Decoder::new(&[]);
//         let err = bool::decode(&mut decoder).err().unwrap();
//         assert_eq!(ErrorKind::Truncated, err.kind());
//         assert_eq!(Some(Length::zero()), err.position());
//     }

//     #[test]
//     fn invalid_field_length() {
//         let mut decoder = Decoder::new(&[0x02, 0x01]);
//         let err = i8::decode(&mut decoder).err().unwrap();
//         assert_eq!(ErrorKind::Length { tag: Tag::Integer }, err.kind());
//         assert_eq!(Some(Length::from(2u8)), err.position());
//     }

//     #[test]
//     fn trailing_data() {
//         let mut decoder = Decoder::new(&[0x02, 0x01, 0x2A, 0x00]);
//         let x = decoder.decode().unwrap();
//         assert_eq!(42i8, x);

//         let err = decoder.finish(x).err().unwrap();
//         assert_eq!(
//             ErrorKind::TrailingData {
//                 decoded: 3u8.into(),
//                 remaining: 1u8.into()
//             },
//             err.kind()
//         );
//         assert_eq!(Some(Length::from(3u8)), err.position());
//     }
// }