XXX: Can"t update crypter with an in-place buffer...

Fortune for chacha20stream's current commit: Small curse − 小凶
read-stream-wrapper
Avril 3 years ago
parent 404d4511f8
commit 6cbbf2bd2c
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -44,6 +44,7 @@ openssl = "0.10"
pin-project = {version = "1.0.6", optional = true} pin-project = {version = "1.0.6", optional = true}
serde = {version = "1.0", features = ["derive"], optional = true} serde = {version = "1.0", features = ["derive"], optional = true}
smallvec = {version = "1.6", features=["union"], optional = true} smallvec = {version = "1.6", features=["union"], optional = true}
stackalloc = "1.1.1"
tokio = {version = "0.2", optional = true} tokio = {version = "0.2", optional = true}
[build-dependencies] [build-dependencies]

@ -1,6 +1,11 @@
//! Syncronous stream `Read` componant. //! Syncronous stream `Read` componant.
use super::*; use super::*;
/// Max number of bytes to stackalloc
///
/// Only used with `UseBufferExternal`.
pub const STACK_MAX_BYTES: usize = 4096;
/// How buffers are used. /// How buffers are used.
pub trait BufferKind : private::Sealed pub trait BufferKind : private::Sealed
{ {
@ -158,11 +163,22 @@ where R: Read
} }
} }
/// Perform the cipher transform on this input to the inner buffer, returning the number of bytes updated. /// Perform the cipher transform on this `buffer` to the output buffer, returning the number of bytes updated.
fn transform_into(&mut self, buffer: &[u8], output: &mut [u8]) -> Result<usize, ErrorStack>
{
let n = self.crypter.update(buffer, &mut output[..])?;
let _f = self.crypter.finalize(&mut output[..n])?;
debug_assert_eq!(_f, 0);
Ok(n)
}
/// Perform the cipher transform on the inner buffer bytes to the `output` buffer, returning the number of bytes updated.
///
/// # Panics
/// If the inner buffer is phantom
fn transform(&mut self, bufsz: usize, output: &mut [u8]) -> Result<usize, ErrorStack> fn transform(&mut self, bufsz: usize, output: &mut [u8]) -> Result<usize, ErrorStack>
{ {
//self.grow_to_fix(output.len());
//let bufsz = self.stream.read(&mut self.buffer[..bufsz])?;
let n = self.crypter.update(& K::buffer_bytes(&self.buffer)[..bufsz], &mut output[..])?; let n = self.crypter.update(& K::buffer_bytes(&self.buffer)[..bufsz], &mut output[..])?;
let _f = self.crypter.finalize(&mut output[..n])?; let _f = self.crypter.finalize(&mut output[..n])?;
debug_assert_eq!(_f, 0); debug_assert_eq!(_f, 0);
@ -170,6 +186,7 @@ where R: Read
Ok(n) Ok(n)
} }
/// Clear the internal buffer while keeping it allocated for further use. /// Clear the internal buffer while keeping it allocated for further use.
/// ///
/// This does not affect operations at all, all it does is 0 out the left-over temporary buffer from the last operation(s). /// This does not affect operations at all, all it does is 0 out the left-over temporary buffer from the last operation(s).
@ -219,13 +236,16 @@ impl<R: ?Sized, K: ?Sized + BufferKind> Read for Source<R, K>
where R: Read where R: Read
{ {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(#[cfg(feature="reuse-buffer")] { if cfg!(feature="reuse-buffer") {
//XXX: FUck, we can't `crypter.update()` in place....
let read = self.stream.read(buf)?;
todo!() todo!()
},
#[cfg(not(feature="reuse-buffer"))] { }
else {
self.grow_to_fit(buf.len()); self.grow_to_fit(buf.len());
let read = self.stream.read(&mut self.buffer[..buf.len()])?; let read = self.stream.read(&mut K::buffer_bytes_mut(&mut self.buffer)[..buf.len()])?;
Ok(self.transform(read, &mut buf[..read])?) Ok(self.transform(read, &mut buf[..read])?)
},).0 }
} }
} }

Loading…
Cancel
Save