# [RustCrypto]: SSH Encoding
[![crate][crate-image]][crate-link]
[![Docs][docs-image]][docs-link]
[![Build Status][build-image]][build-link]
![Apache2/MIT licensed][license-image]
![Rust Version][rustc-image]
[![Project Chat][chat-image]][chat-link]
[Documentation][docs-link]
## About
Pure Rust implementation of SSH data type decoders/encoders as described
in [RFC4251].
## Decoding
Use `Decoder::from_bytes` to decode binary SSH data.
```rust
use ssh_encoding::{Decode, Decoder};
let mut decoder = Decoder::from_bytes(&[0, 0, 0, 4, 0, 0, 0, 42]).expect("input too long");
let value = decoder.read_prefixed(u32::decode).expect("invalid u32");
assert_eq!(decoder.finish(value).expect("remaining data"), 42);
```
`Reader` supplies bytes and their remaining length. `Decoder<R>` owns the reader and
handles nested bounds, complete consumption, and failure state. Nested callbacks use
the same decoder type and store enclosing bounds on the call stack without allocation.
`Decode` implementations accept `&mut Decoder<impl Reader>`.
Use `Decoder::from_base64` for contiguous Base64 text or `Decoder::from_pem` for a
PEM document. Both accept borrowed strings or bytes and decode the body incrementally.
The corresponding `base64` and `pem` features also provide the `Base64Decoder<'a>`
and `PemDecoder<'a>` type aliases for use in signatures. A PEM decoder exposes the
document's type label through `pem_label()`. For a type with a known label,
`DecodePem::decode_pem` checks the label before decoding the body.
To supply a reader directly, use `Decoder::new(reader)` or `reader.into_decoder()`.
All constructors return an error if the remaining binary input exceeds 128 MiB, matching
OpenSSH's hard buffer limit. `Decoder::read_prefixed` checks that the declared
body fits in the remaining bytes of the current scope before decoding it.
For Base64 and PEM, the limit applies to the decoded binary length. Applications
must separately bound textual input and file reads before constructing a reader.
## License
Licensed under either of:
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
* [MIT license](http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
[//]: # (badges)
[crate-image]: https://img.shields.io/crates/v/ssh-encoding?logo=rust
[crate-link]: https://crates.io/crates/ssh-encoding
[docs-image]: https://docs.rs/ssh-encoding/badge.svg
[docs-link]: https://docs.rs/ssh-encoding/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.87+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/346919-SSH
[build-image]: https://github.com/RustCrypto/SSH/actions/workflows/ssh-encoding.yml/badge.svg
[build-link]: https://github.com/RustCrypto/SSH/actions/workflows/ssh-encoding.yml
[//]: # (links)
[RustCrypto]: https://github.com/rustcrypto
[RFC4251]: https://datatracker.ietf.org/doc/html/rfc4251