From c16778797b2eeb118aa064aa5b483f90c3989627 Mon Sep 17 00:00:00 2001 From: Marek Kuskowski <50183564+nylonicious@users.noreply.github.com> Date: Wed, 8 Jan 2025 21:10:42 +0100 Subject: [PATCH] Remove byteorder, use bytes::Buf::get_uint instead (#477) * Remove byteorder, use bytes::Buf::get_uint instead * avoid unecessary copy inside bytes::Buf::get_uint * use assert! to verify length number --- Cargo.toml | 1 - src/protocol/frame/frame.rs | 17 ++++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1fe90f09..244c1a06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,6 @@ __rustls-tls = ["rustls", "rustls-pki-types"] [dependencies] data-encoding = { version = "2", optional = true } -byteorder = "1.3.2" bytes = "1.9.0" http = { version = "1.0", optional = true } httparse = { version = "1.3.4", optional = true } diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 31a4c004..baede5bf 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -1,4 +1,3 @@ -use byteorder::{NetworkEndian, ReadBytesExt}; use log::*; use std::{ default::Default, @@ -161,14 +160,14 @@ impl FrameHeader { let length_byte = second & 0x7F; let length_length = LengthFormat::for_byte(length_byte).extra_bytes(); if length_length > 0 { - match cursor.read_uint::(length_length) { - Err(ref err) if err.kind() == ErrorKind::UnexpectedEof => { - return Ok(None); - } - Err(err) => { - return Err(err.into()); - } - Ok(read) => read, + const SIZE: usize = mem::size_of::(); + assert!(length_length <= SIZE, "length exceeded size of u64"); + let start = SIZE - length_length; + let mut buffer = [0; SIZE]; + match cursor.read_exact(&mut buffer[start..]) { + Err(ref err) if err.kind() == ErrorKind::UnexpectedEof => return Ok(None), + Err(err) => return Err(err.into()), + Ok(()) => u64::from_be_bytes(buffer), } } else { u64::from(length_byte)