Skip to content

Commit

Permalink
tls_codec: expose read/write of variable-length encoding (#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer authored Feb 8, 2024
1 parent 0bde794 commit ed868a1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tls_codec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tls_codec"
version = "0.4.1"
version = "0.4.2-pre.1"
authors = ["RustCrypto Developers"]
license = "Apache-2.0 OR MIT"
documentation = "https://docs.rs/tls_codec/"
Expand Down
2 changes: 1 addition & 1 deletion tls_codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub use tls_vec::{
};

#[cfg(feature = "std")]
pub use quic_vec::SecretVLBytes;
pub use quic_vec::{rw as vlen, SecretVLBytes};
pub use quic_vec::{VLByteSlice, VLBytes};

#[cfg(feature = "derive")]
Expand Down
23 changes: 9 additions & 14 deletions tls_codec/src/quic_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn length_encoding_bytes(length: u64) -> Result<usize, Error> {
}

#[inline(always)]
fn write_length(content_length: usize) -> Result<Vec<u8>, Error> {
pub fn write_variable_length(content_length: usize) -> Result<Vec<u8>, Error> {
let len_len = length_encoding_bytes(content_length.try_into()?)?;
if !cfg!(fuzzing) {
debug_assert!(len_len <= 8, "Invalid vector len_len {len_len}");
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<T: SerializeBytes> SerializeBytes for &[T] {
// This requires more computations but the other option would be to buffer
// the entire content, which can end up requiring a lot of memory.
let content_length = self.iter().fold(0, |acc, e| acc + e.tls_serialized_len());
let mut length = write_length(content_length)?;
let mut length = write_variable_length(content_length)?;
let len_len = length.len();

let mut out = Vec::with_capacity(content_length + len_len);
Expand Down Expand Up @@ -418,7 +418,7 @@ impl<'a> Size for VLByteSlice<'a> {
}

#[cfg(feature = "std")]
mod rw {
pub mod rw {
use super::*;
use crate::{Deserialize, Serialize};

Expand All @@ -429,9 +429,7 @@ mod rw {
///
/// The length and number of bytes read are returned.
#[inline]
pub(super) fn read_variable_length<R: std::io::Read>(
bytes: &mut R,
) -> Result<(usize, usize), Error> {
pub fn read_length<R: std::io::Read>(bytes: &mut R) -> Result<(usize, usize), Error> {
// The length is encoded in the first two bits of the first byte.
let mut len_len_byte = [0u8; 1];
if bytes.read(&mut len_len_byte)? == 0 {
Expand All @@ -458,7 +456,7 @@ mod rw {
impl<T: Deserialize> Deserialize for Vec<T> {
#[inline(always)]
fn tls_deserialize<R: std::io::Read>(bytes: &mut R) -> Result<Self, Error> {
let (length, len_len) = read_variable_length(bytes)?;
let (length, len_len) = read_length(bytes)?;

if length == 0 {
// An empty vector.
Expand All @@ -477,11 +475,11 @@ mod rw {
}

#[inline(always)]
pub(super) fn write_length<W: std::io::Write>(
pub fn write_length<W: std::io::Write>(
writer: &mut W,
content_length: usize,
) -> Result<usize, Error> {
let buf = super::write_length(content_length)?;
let buf = super::write_variable_length(content_length)?;
let buf_len = buf.len();
writer.write_all(&buf)?;
Ok(buf_len)
Expand Down Expand Up @@ -525,9 +523,6 @@ mod rw {
}
}

#[cfg(feature = "std")]
use rw::*;

/// Read/Write (std) based (de)serialization for [`VLBytes`].
#[cfg(feature = "std")]
mod rw_bytes {
Expand All @@ -553,7 +548,7 @@ mod rw_bytes {
return Err(Error::InvalidVectorLength);
}

let length_bytes = write_length(content_length)?;
let length_bytes = write_variable_length(content_length)?;
let len_len = length_bytes.len();
writer.write_all(&length_bytes)?;

Expand All @@ -579,7 +574,7 @@ mod rw_bytes {

impl Deserialize for VLBytes {
fn tls_deserialize<R: std::io::Read>(bytes: &mut R) -> Result<Self, Error> {
let (length, _) = read_variable_length(bytes)?;
let (length, _) = rw::read_length(bytes)?;
if length == 0 {
return Ok(Self::new(vec![]));
}
Expand Down

0 comments on commit ed868a1

Please sign in to comment.