Create EncryptionStream struct

This commit is contained in:
femsci 2023-01-18 16:46:20 +01:00
parent 4003be7485
commit a769a07c37
Signed by: femsci
GPG key ID: 08F7911F0E650C67

View file

@ -1,3 +1,5 @@
use std::io::{Read, Seek, SeekFrom};
pub(crate) mod local; pub(crate) mod local;
pub(crate) mod server; pub(crate) mod server;
@ -86,3 +88,61 @@ impl From<u8> for HashType {
} }
} }
} }
struct EncryptionStream {
data: Vec<u8>,
pos: usize,
}
impl Read for EncryptionStream {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let range = self.pos..(self.pos + buf.len()).clamp(0, self.data.len() - buf.len());
let size = range.len();
buf.copy_from_slice(&self.data[range]);
Ok(size)
}
}
impl Seek for EncryptionStream {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
let cur = self.pos;
let newpos = match pos {
SeekFrom::Start(x) => {
if x > self.data.len() as u64 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek position out of buffer bounds.",
));
}
x
}
SeekFrom::End(x) => {
if x > self.data.len() as i64 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek position out of buffer bounds.",
));
}
(self.data.len() as i64 - x) as u64
}
SeekFrom::Current(x) => {
let new = self.pos as i64 + x;
if new < 0 || new > self.data.len() as i64 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Seek position out of buffer bounds.",
));
}
new as u64
}
};
self.pos = newpos as usize;
Ok(cur as u64)
}
}