Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls_codec: expose vlen functions #1345

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading