| 1 | use byteorderrust-analyzer cargo byteorder 1.5.0 crate/::{BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#, ByteOrderrust-analyzer cargo byteorder 1.5.0 ByteOrder#}; |
| 2 | |
| 3 | /// A `Writer` is used for encoding a key in OpenSSH compatible format. |
| 4 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 5 | pub struct Writerrust-analyzer cargo sshkeys 0.3.4 writer/Writer# { |
| 6 | innerrust-analyzer cargo sshkeys 0.3.4 writer/Writer#inner.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 7 | } |
| 8 | |
| 9 | impl Writerrust-analyzer cargo sshkeys 0.3.4 writer/Writer# { |
| 10 | /// Creates a new `Writer` instance. |
| 11 | /// |
| 12 | /// # Example |
| 13 | /// ```rust |
| 14 | /// let writer = sshkeys::Writer::new(); |
| 15 | /// ``` |
| 16 | pub fn newrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]new().() -> Writerrust-analyzer cargo sshkeys 0.3.4 writer/Writer# { |
| 17 | Writerrust-analyzer cargo sshkeys 0.3.4 writer/Writer# { innerrust-analyzer cargo sshkeys 0.3.4 writer/Writer#inner.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().() } |
| 18 | } |
| 19 | |
| 20 | /// Writes a byte sequence to the underlying vector. |
| 21 | /// The value is represented as a the byte sequence length, |
| 22 | /// followed by the actual byte sequence. |
| 23 | /// |
| 24 | /// # Example |
| 25 | /// ```rust |
| 26 | /// # use sshkeys; |
| 27 | /// let mut writer = sshkeys::Writer::new(); |
| 28 | /// writer.write_bytes(&[0, 0, 0, 42]); |
| 29 | /// let bytes = writer.into_bytes(); |
| 30 | /// assert_eq!(bytes, vec![0, 0, 0, 4, 0, 0, 0, 42]); |
| 31 | /// ``` |
| 32 | pub fn write_bytesrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_bytes().(&mut self, val: &[u8]) { |
| 33 | let size = val.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() as u32; |
| 34 | let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; 4]; |
| 35 | BigEndianrust-analyzer cargo byteorder 1.5.0 BigEndian#::write_u32rust-analyzer cargo byteorder 1.5.0 impl#[BigEndian][ByteOrder]write_u32().(&mut buf, size); |
| 36 | self.innerrust-analyzer cargo sshkeys 0.3.4 writer/Writer#inner..appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut buf); |
| 37 | self.innerrust-analyzer cargo sshkeys 0.3.4 writer/Writer#inner..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&val); |
| 38 | } |
| 39 | |
| 40 | /// Writes a `string` value to the underlying byte sequence. |
| 41 | /// |
| 42 | /// # Example |
| 43 | /// ```rust |
| 44 | /// # use sshkeys; |
| 45 | /// let mut writer = sshkeys::Writer::new(); |
| 46 | /// writer.write_string("a test string"); |
| 47 | /// let bytes = writer.into_bytes(); |
| 48 | /// assert_eq!(bytes, [0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]); |
| 49 | /// ``` |
| 50 | pub fn write_stringrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_string().(&mut self, val: &str) { |
| 51 | self.write_bytesrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_bytes().(val.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 52 | } |
| 53 | |
| 54 | /// Writes an `mpint` value to the underlying byte sequence. |
| 55 | /// If the MSB bit of the first byte is set then the number is |
| 56 | /// negative, otherwise it is positive. |
| 57 | /// Positive numbers must be preceeded by a leading zero byte according to RFC 4251, section 5. |
| 58 | /// |
| 59 | /// # Example |
| 60 | /// ```rust |
| 61 | /// # use sshkeys; |
| 62 | /// let mut writer = sshkeys::Writer::new(); |
| 63 | /// writer.write_mpint(&[1, 0, 1]); |
| 64 | /// let bytes = writer.into_bytes(); |
| 65 | /// assert_eq!(bytes, [0, 0, 0, 3, 1, 0, 1]); |
| 66 | /// ``` |
| 67 | pub fn write_mpintrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_mpint().(&mut self, val: &[u8]) { |
| 68 | let mut bytes = val.to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(); |
| 69 | |
| 70 | // If most significant bit is set then prepend a zero byte to |
| 71 | // avoid interpretation as a negative number. |
| 72 | if val.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(0).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(&0) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[`&u8`][`BitAnd<u8>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[`&u8`][`BitAnd<u8>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[`&u8`][`BitAnd<u8>`]bitand().0x80 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().0 { |
| 73 | bytes.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]insert().(0, 0); |
| 74 | } |
| 75 | |
| 76 | self.write_bytesrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_bytes().(&bytes); |
| 77 | } |
| 78 | |
| 79 | /// Converts the `Writer` into a byte sequence. |
| 80 | /// This consumes the underlying byte sequence used by the `Writer`. |
| 81 | /// |
| 82 | /// # Example |
| 83 | /// ```rust |
| 84 | /// let mut writer = sshkeys::Writer::new(); |
| 85 | /// writer.write_string("some data"); |
| 86 | /// let bytes = writer.into_bytes(); |
| 87 | /// assert_eq!(bytes, [0, 0, 0, 9, 115, 111, 109, 101, 32, 100, 97, 116, 97]); |
| 88 | /// ``` |
| 89 | pub fn into_bytesrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]into_bytes().(self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 90 | self.innerrust-analyzer cargo sshkeys 0.3.4 writer/Writer#inner. |
| 91 | } |
| 92 | } |