Skip to content

Commit c0d573e

Browse files
expose vlen read/write functions
1 parent cee0d77 commit c0d573e

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

Cargo.lock

+12-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tls_codec/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tls_codec"
3-
version = "0.4.1"
3+
version = "0.4.2-pre.1"
44
authors = ["RustCrypto Developers"]
55
license = "Apache-2.0 OR MIT"
66
documentation = "https://docs.rs/tls_codec/"

tls_codec/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub use tls_vec::{
4848
};
4949

5050
#[cfg(feature = "std")]
51-
pub use quic_vec::{rw::read_variable_length, write_length, SecretVLBytes};
51+
pub use quic_vec::{rw as vlen, SecretVLBytes};
5252
pub use quic_vec::{VLByteSlice, VLBytes};
5353

5454
#[cfg(feature = "derive")]

tls_codec/src/quic_vec.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn length_encoding_bytes(length: u64) -> Result<usize, Error> {
105105
}
106106

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

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

420420
#[cfg(feature = "std")]
421-
pub(super) mod rw {
421+
pub mod rw {
422422
use super::*;
423423
use crate::{Deserialize, Serialize};
424424

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

461461
if length == 0 {
462462
// An empty vector.
@@ -475,11 +475,11 @@ pub(super) mod rw {
475475
}
476476

477477
#[inline(always)]
478-
pub(super) fn write_length<W: std::io::Write>(
478+
pub fn write_length<W: std::io::Write>(
479479
writer: &mut W,
480480
content_length: usize,
481481
) -> Result<usize, Error> {
482-
let buf = super::write_length(content_length)?;
482+
let buf = super::write_variable_length(content_length)?;
483483
let buf_len = buf.len();
484484
writer.write_all(&buf)?;
485485
Ok(buf_len)
@@ -523,9 +523,6 @@ pub(super) mod rw {
523523
}
524524
}
525525

526-
#[cfg(feature = "std")]
527-
use rw::*;
528-
529526
/// Read/Write (std) based (de)serialization for [`VLBytes`].
530527
#[cfg(feature = "std")]
531528
mod rw_bytes {
@@ -551,7 +548,7 @@ mod rw_bytes {
551548
return Err(Error::InvalidVectorLength);
552549
}
553550

554-
let length_bytes = write_length(content_length)?;
551+
let length_bytes = write_variable_length(content_length)?;
555552
let len_len = length_bytes.len();
556553
writer.write_all(&length_bytes)?;
557554

@@ -577,7 +574,7 @@ mod rw_bytes {
577574

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

0 commit comments

Comments
 (0)