pub struct Reader<'a> { /* private fields */ }Expand description
A Reader is used for reading from a byte sequence
representing an encoded OpenSSH public key or certificate.
Implementations§
Source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
Sourcepub fn new<T: ?Sized + AsRef<[u8]>>(inner: &T) -> Reader<'_>
pub fn new<T: ?Sized + AsRef<[u8]>>(inner: &T) -> Reader<'_>
Creates a new Reader instance from the given byte sequence.
§Example
let data = vec![0, 0, 0, 42];
let mut reader = Reader::new(&data);
let num = reader.read_u32().unwrap();
assert_eq!(num, 42);Sourcepub fn set_offset(&mut self, offset: usize) -> Result<()>
pub fn set_offset(&mut self, offset: usize) -> Result<()>
Sets the Reader current offset to a given position.
§Example
let data = vec![0, 0, 0, 42];
let mut reader = Reader::new(&data);
let num = reader.read_u32().unwrap();
assert_eq!(num, 42);
reader.set_offset(0);
let num_42_again = reader.read_u32().unwrap();
assert_eq!(num_42_again, 42);Sourcepub fn get_offset(&self) -> usize
pub fn get_offset(&self) -> usize
Gets the Reader current offset.
§Example
let data = vec![0, 0, 0, 42];
let mut reader = Reader::new(&data);
let num = reader.read_u32().unwrap();
assert_eq!(num, 42);
assert_eq!(reader.get_offset(), 4);Sourcepub fn read_bytes(&mut self) -> Result<Vec<u8>>
pub fn read_bytes(&mut self) -> Result<Vec<u8>>
Reads a byte buffer from the wrapped byte sequence and
returns it as a Vec<u8>.
The buffer is represented by it’s length as u32 value
followed by the actual bytes to read.
§Example
let data = vec![0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103];
let mut reader = Reader::new(&data);
let bytes = reader.read_bytes().unwrap();
assert_eq!(bytes, [97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]);Sourcepub fn read_raw_bytes(&mut self, len: usize) -> Result<Vec<u8>>
pub fn read_raw_bytes(&mut self, len: usize) -> Result<Vec<u8>>
Reads len bytes from the wrapped buffer as raw data
§Example
let data = vec![0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103];
let mut reader = Reader::new(&data);
let bytes = reader.read_raw_bytes(4).unwrap();
assert_eq!(bytes, [0, 0, 0, 13]);Sourcepub fn read_mpint(&mut self) -> Result<Vec<u8>>
pub fn read_mpint(&mut self) -> Result<Vec<u8>>
Reads an mpint value from the wrapped byte sequence.
Drops the leading byte if it’s value is zero according to the RFC 4251, section 5.
§Example
let data = vec![0, 0, 0, 3, 1, 0, 1];
let mut reader = Reader::new(&data);
let mpint = reader.read_mpint().unwrap();
assert_eq!(mpint, [1, 0, 1]);Sourcepub fn read_string(&mut self) -> Result<String>
pub fn read_string(&mut self) -> Result<String>
Reads a string value from the wrapped byte sequence and
returns it as a String. The value that we read should be a valid UTF-8.
§Example
let data = vec![0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103];
let mut reader = Reader::new(&data);
let result = reader.read_string().unwrap();
assert_eq!(result, "a test string");Sourcepub fn read_cstring(&mut self) -> Result<String>
pub fn read_cstring(&mut self) -> Result<String>
Read a null terminated string from the reader’s buffer. This is different than read_string in that the length is unknown and will continue until it reads a null byte or reaches the end of the data.
In the event the buffer runs out before a null byte, the offset will be reset and an error returned.