| 1 | use super::error::{Error, ErrorKind, Result}; |
| 2 | |
| 3 | use byteorder::{BigEndian, ByteOrder}; |
| 4 | |
| 5 | /// A `Reader` is used for reading from a byte sequence |
| 6 | /// representing an encoded OpenSSH public key or certificate. |
| 7 | #[derive(Debug)] |
| 8 | pub struct Reader<'a> { |
| 9 | inner: &'a [u8], |
| 10 | offset: usize, |
| 11 | } |
| 12 | |
| 13 | impl<'a> Reader<'a> { |
| 14 | /// Creates a new `Reader` instance from the given byte sequence. |
| 15 | /// |
| 16 | /// # Example |
| 17 | /// ```rust |
| 18 | /// # use sshkeys; |
| 19 | /// let data = vec![0, 0, 0, 42]; |
| 20 | /// let mut reader = sshkeys::Reader::new(&data); |
| 21 | /// let num = reader.read_u32().unwrap(); |
| 22 | /// assert_eq!(num, 42); |
| 23 | /// ``` |
| 24 | pub fn new<T: ?Sized + AsRef<[u8]>>(inner: &T) -> Reader<'_> { |
| 25 | Reader { |
| 26 | inner: inner.as_ref(), |
| 27 | offset: 0, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Sets the `Reader` current offset to a given position. |
| 32 | /// |
| 33 | /// # Example |
| 34 | /// ```rust |
| 35 | /// # use sshkeys; |
| 36 | /// let data = vec![0, 0, 0, 42]; |
| 37 | /// let mut reader = sshkeys::Reader::new(&data); |
| 38 | /// let num = reader.read_u32().unwrap(); |
| 39 | /// assert_eq!(num, 42); |
| 40 | /// reader.set_offset(0); |
| 41 | /// let num_42_again = reader.read_u32().unwrap(); |
| 42 | /// assert_eq!(num_42_again, 42); |
| 43 | /// ``` |
| 44 | pub fn set_offset(&mut self, offset: usize) -> Result<()> { |
| 45 | self.offset = offset; |
| 46 | |
| 47 | Ok(()) |
| 48 | } |
| 49 | |
| 50 | /// Reads a byte buffer from the wrapped byte sequence and |
| 51 | /// returns it as a `Vec<u8>`. |
| 52 | /// The buffer is represented by it's length as `u32` value |
| 53 | /// followed by the actual bytes to read. |
| 54 | /// |
| 55 | /// # Example |
| 56 | /// ```rust |
| 57 | /// # use sshkeys; |
| 58 | /// let data = vec![0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]; |
| 59 | /// let mut reader = sshkeys::Reader::new(&data); |
| 60 | /// let bytes = reader.read_bytes().unwrap(); |
| 61 | /// assert_eq!(bytes, [97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]); |
| 62 | /// ``` |
| 63 | pub fn read_bytes(&mut self) -> Result<Vec<u8>> { |
| 64 | if self.offset >= self.inner.len() { |
| 65 | return Err(Error::with_kind(ErrorKind::UnexpectedEof)); |
| 66 | } |
| 67 | |
| 68 | let slice = &self.inner[self.offset..]; |
| 69 | |
| 70 | if slice.len() < 4 { |
| 71 | return Err(Error::with_kind(ErrorKind::InvalidFormat)); |
| 72 | } |
| 73 | |
| 74 | let size = BigEndian::read_u32(&slice[..4]) as usize; |
| 75 | |
| 76 | if slice.len() < size + 4 { |
| 77 | return Err(Error::with_kind(ErrorKind::InvalidFormat)); |
| 78 | } |
| 79 | |
| 80 | self.offset += size + 4; |
| 81 | let result = slice[4..size + 4].to_vec(); |
| 82 | |
| 83 | Ok(result) |
| 84 | } |
| 85 | |
| 86 | /// Reads an `mpint` value from the wrapped byte sequence. |
| 87 | /// |
| 88 | /// Drops the leading byte if it's value is zero according to the RFC 4251, section 5. |
| 89 | /// |
| 90 | /// # Example |
| 91 | /// ```rust |
| 92 | /// # use sshkeys; |
| 93 | /// let data = vec![0, 0, 0, 3, 1, 0, 1]; |
| 94 | /// let mut reader = sshkeys::Reader::new(&data); |
| 95 | /// let mpint = reader.read_mpint().unwrap(); |
| 96 | /// assert_eq!(mpint, [1, 0, 1]); |
| 97 | /// ``` |
| 98 | pub fn read_mpint(&mut self) -> Result<Vec<u8>> { |
| 99 | let bytes = self.read_bytes()?; |
| 100 | |
| 101 | if bytes[0] == 0 { |
| 102 | return Ok(bytes[1..].to_vec()); |
| 103 | } |
| 104 | |
| 105 | Ok(bytes) |
| 106 | } |
| 107 | |
| 108 | /// Reads a `string` value from the wrapped byte sequence and |
| 109 | /// returns it as a `String`. The value that we read should be a valid UTF-8. |
| 110 | /// |
| 111 | /// # Example |
| 112 | /// ```rust |
| 113 | /// # use sshkeys; |
| 114 | /// let data = vec![0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]; |
| 115 | /// let mut reader = sshkeys::Reader::new(&data); |
| 116 | /// let result = reader.read_string().unwrap(); |
| 117 | /// assert_eq!(result, "a test string"); |
| 118 | /// ``` |
| 119 | pub fn read_string(&mut self) -> Result<String> { |
| 120 | let bytes = self.read_bytes()?; |
| 121 | let result = String::from_utf8(bytes)?; |
| 122 | |
| 123 | Ok(result) |
| 124 | } |
| 125 | |
| 126 | /// Reads an `u32` value from the wrapped byte sequence and returns it. |
| 127 | /// |
| 128 | /// # Example |
| 129 | /// ```rust |
| 130 | /// # use sshkeys; |
| 131 | /// let data = vec![0, 0, 0, 42]; |
| 132 | /// let mut reader = sshkeys::Reader::new(&data); |
| 133 | /// let num = reader.read_u32().unwrap(); |
| 134 | /// assert_eq!(num, 42); |
| 135 | /// ``` |
| 136 | pub fn read_u32(&mut self) -> Result<u32> { |
| 137 | if self.offset >= self.inner.len() { |
| 138 | return Err(Error::with_kind(ErrorKind::UnexpectedEof)); |
| 139 | } |
| 140 | |
| 141 | let slice = &self.inner[self.offset..]; |
| 142 | if slice.len() < 4 { |
| 143 | return Err(Error::with_kind(ErrorKind::InvalidFormat)); |
| 144 | } |
| 145 | |
| 146 | self.offset += 4; |
| 147 | let value = BigEndian::read_u32(&slice[..4]); |
| 148 | |
| 149 | Ok(value) |
| 150 | } |
| 151 | |
| 152 | /// Reads an `u64` value from the wrapped byte sequence and returns it. |
| 153 | /// |
| 154 | /// # Example |
| 155 | /// ```rust |
| 156 | /// # use sshkeys; |
| 157 | /// let data = vec![0, 0, 0, 0, 0, 0, 0, 42]; |
| 158 | /// let mut reader = sshkeys::Reader::new(&data); |
| 159 | /// let num = reader.read_u64().unwrap(); |
| 160 | /// assert_eq!(num, 42); |
| 161 | /// ``` |
| 162 | pub fn read_u64(&mut self) -> Result<u64> { |
| 163 | if self.offset >= self.inner.len() { |
| 164 | return Err(Error::with_kind(ErrorKind::UnexpectedEof)); |
| 165 | } |
| 166 | |
| 167 | let slice = &self.inner[self.offset..]; |
| 168 | if slice.len() < 8 { |
| 169 | return Err(Error::with_kind(ErrorKind::InvalidFormat)); |
| 170 | } |
| 171 | |
| 172 | self.offset += 8; |
| 173 | let value = BigEndian::read_u64(&slice[..8]); |
| 174 | |
| 175 | Ok(value) |
| 176 | } |
| 177 | } |