diff --git a/Cargo.toml b/Cargo.toml index 50ab298..8694982 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ openssl = "0.10" pin-project = {version = "1.0.6", optional = true} serde = {version = "1.0", features = ["derive"], optional = true} smallvec = {version = "1.6", features=["union"], optional = true} +stackalloc = "1.1.1" tokio = {version = "0.2", optional = true} [build-dependencies] diff --git a/src/stream/source.rs b/src/stream/source.rs index 1bbaf26..33a1ac7 100644 --- a/src/stream/source.rs +++ b/src/stream/source.rs @@ -1,6 +1,11 @@ //! Syncronous stream `Read` componant. use super::*; +/// Max number of bytes to stackalloc +/// +/// Only used with `UseBufferExternal`. +pub const STACK_MAX_BYTES: usize = 4096; + /// How buffers are used. 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 + { + 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 { - //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 _f = self.crypter.finalize(&mut output[..n])?; debug_assert_eq!(_f, 0); @@ -170,6 +186,7 @@ where R: Read Ok(n) } + /// 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). @@ -219,13 +236,16 @@ impl Read for Source where R: Read { fn read(&mut self, buf: &mut [u8]) -> io::Result { - (#[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!() - }, - #[cfg(not(feature="reuse-buffer"))] { - self.grow_to_fit(buf.len()); - let read = self.stream.read(&mut self.buffer[..buf.len()])?; - Ok(self.transform(read, &mut buf[..read])?) - },).0 + + } + else { + self.grow_to_fit(buf.len()); + let read = self.stream.read(&mut K::buffer_bytes_mut(&mut self.buffer)[..buf.len()])?; + Ok(self.transform(read, &mut buf[..read])?) + } } }