ssh/algorithm/compression/
mod.rs

1use super::Compress;
2use crate::SshResult;
3
4mod zlib;
5/// <https://www.rfc-editor.org/rfc/rfc4253#section-6.2>
6pub(crate) trait Compression: Send + Sync {
7    fn new() -> Self
8    where
9        Self: Sized;
10    // The "zlib@openssh.com" method operates identically to the "zlib"
11    // method described in [RFC4252] except that packet compression does not
12    // start until the server sends a SSH_MSG_USERAUTH_SUCCESS packet
13    // so
14    // fn start();
15    fn compress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>>;
16    fn decompress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>>;
17}
18
19pub(crate) fn from(comp: &Compress) -> Box<dyn Compression> {
20    match comp {
21        Compress::None => Box::new(CompressNone::new()),
22        #[cfg(feature = "deprecated-zlib")]
23        Compress::Zlib => Box::new(zlib::CompressZlib::new()),
24        Compress::ZlibOpenSsh => Box::new(zlib::CompressZlib::new()),
25    }
26}
27
28#[derive(Default)]
29pub(crate) struct CompressNone {}
30
31impl Compression for CompressNone {
32    fn new() -> Self {
33        Self {}
34    }
35
36    fn compress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>> {
37        Ok(buf.to_vec())
38    }
39
40    fn decompress(&mut self, buf: &[u8]) -> SshResult<Vec<u8>> {
41        Ok(buf.to_vec())
42    }
43}