refactor: move padding module to ironrdp-core (#716)

This commit is contained in:
Benoît Cortier
2025-03-24 22:52:46 +02:00
committed by GitHub
parent bceb6c1492
commit b72e0857bf
31 changed files with 109 additions and 82 deletions
+2
View File
@@ -17,6 +17,7 @@ mod decode;
mod encode;
mod error;
mod into_owned;
mod padding;
#[cfg(feature = "alloc")]
mod write_buf;
@@ -28,5 +29,6 @@ pub use self::decode::*;
pub use self::encode::*;
pub use self::error::*;
pub use self::into_owned::*;
pub use self::padding::*;
#[cfg(feature = "alloc")]
pub use self::write_buf::*;
+33
View File
@@ -391,3 +391,36 @@ macro_rules! cast_int {
$crate::cast_int!($crate::function!(), $field, $len)
}};
}
/// Writes zeroes using as few `write_u*` calls as possible.
///
/// This is similar to `ironrdp_core::padding::write`, but the loop is optimized out when a single
/// operation is enough.
#[macro_export]
macro_rules! write_padding {
($dst:expr, 1) => {
$dst.write_u8(0)
};
($dst:expr, 2) => {
$dst.write_u16(0)
};
($dst:expr, 4) => {
$dst.write_u32(0)
};
($dst:expr, 8) => {
$dst.write_u64(0)
};
($dst:expr, $n:expr) => {
$crate::write_padding($dst, $n)
};
}
/// Moves read cursor, ignoring padding bytes.
///
/// This is similar to `ironrdp_pdu::padding::read`, only exists for consistency with `write_padding!`.
#[macro_export]
macro_rules! read_padding {
($src:expr, $n:expr) => {
$crate::read_padding($src, $n)
};
}
@@ -4,10 +4,10 @@
//! and message recipients should not assume padding has any particular
//! value.
use ironrdp_core::{ReadCursor, WriteCursor};
use crate::{ReadCursor, WriteCursor};
/// Writes zeroes using as few `write_u*` calls as possible.
pub fn write(dst: &mut WriteCursor<'_>, mut n: usize) {
pub fn write_padding(dst: &mut WriteCursor<'_>, mut n: usize) {
loop {
match n {
0 => break,
@@ -33,6 +33,6 @@ pub fn write(dst: &mut WriteCursor<'_>, mut n: usize) {
/// Moves read cursor, ignoring padding bytes.
#[inline]
pub fn read(src: &mut ReadCursor<'_>, n: usize) {
pub fn read_padding(src: &mut ReadCursor<'_>, n: usize) {
src.advance(n);
}
@@ -1,7 +1,7 @@
use bitflags::bitflags;
use ironrdp_core::{
ensure_fixed_part_size, ensure_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
ensure_fixed_part_size, ensure_size, invalid_field_err, write_padding, Decode, DecodeResult, Encode, EncodeResult,
ReadCursor, WriteCursor,
};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
+6 -6
View File
@@ -3,8 +3,8 @@ use std::{io, str};
use bitflags::bitflags;
use ironrdp_core::{
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult,
ReadCursor, WriteCursor,
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, read_padding, write_padding, Decode,
DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
};
use num_integer::Integer;
use thiserror::Error;
@@ -157,7 +157,7 @@ impl ServerNetworkData {
const FIXED_PART_SIZE: usize = SERVER_IO_CHANNEL_SIZE + SERVER_CHANNEL_COUNT_SIZE;
fn write_padding(&self) -> bool {
fn padding_needed(&self) -> bool {
self.channel_ids.len().is_odd()
}
}
@@ -178,8 +178,8 @@ impl Encode for ServerNetworkData {
// (and by implication the entire Server Network Data structure) will not be a multiple of 4.
// In this scenario, the Pad field MUST be present and it is used to add an additional
// 2 bytes to the size of the Server Network Data structure.
if self.write_padding() {
dst.write_u16(0); // pad
if self.padding_needed() {
write_padding!(dst, 2);
}
Ok(())
@@ -190,7 +190,7 @@ impl Encode for ServerNetworkData {
}
fn size(&self) -> usize {
let padding_size = if self.write_padding() { 2 } else { 0 };
let padding_size = if self.padding_needed() { 2 } else { 0 };
Self::FIXED_PART_SIZE + self.channel_ids.len() * SERVER_CHANNEL_SIZE + padding_size
}
+2 -2
View File
@@ -1,8 +1,8 @@
use std::io;
use ironrdp_core::{
ensure_fixed_part_size, ensure_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
ensure_fixed_part_size, ensure_size, invalid_field_err, read_padding, write_padding, Decode, DecodeResult, Encode,
EncodeResult, ReadCursor, WriteCursor,
};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
+4 -1
View File
@@ -1,5 +1,8 @@
use bitflags::bitflags;
use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
use ironrdp_core::{
ensure_fixed_part_size, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScanCodePdu {
+4 -1
View File
@@ -1,5 +1,8 @@
use bitflags::bitflags;
use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
use ironrdp_core::{
ensure_fixed_part_size, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyncPdu {
+4 -1
View File
@@ -1,5 +1,8 @@
use bitflags::bitflags;
use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
use ironrdp_core::{
ensure_fixed_part_size, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnicodePdu {
+4 -1
View File
@@ -1,4 +1,7 @@
use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
use ironrdp_core::{
ensure_fixed_part_size, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnusedPdu;
-1
View File
@@ -21,7 +21,6 @@ pub mod geometry;
pub mod input;
pub mod mcs;
pub mod nego;
pub mod padding;
pub mod pcb;
pub mod rdp;
pub mod tpdu;
-33
View File
@@ -118,39 +118,6 @@ macro_rules! impl_x224_pdu_borrowing {
};
}
/// Writes zeroes using as few `write_u*` calls as possible.
///
/// This is similar to `ironrdp_pdu::padding::write`, but the loop is optimized out when a single
/// operation is enough.
#[macro_export]
macro_rules! write_padding {
($dst:expr, 1) => {
$dst.write_u8(0)
};
($dst:expr, 2) => {
$dst.write_u16(0)
};
($dst:expr, 4) => {
$dst.write_u32(0)
};
($dst:expr, 8) => {
$dst.write_u64(0)
};
($dst:expr, $n:expr) => {
$crate::padding::write($dst, $n)
};
}
/// Moves read cursor, ignoring padding bytes.
///
/// This is similar to `ironrdp_pdu::padding::read`, only exists for consistency with `write_padding!`.
#[macro_export]
macro_rules! read_padding {
($src:expr, $n:expr) => {
$crate::padding::read($src, $n)
};
}
// FIXME: legacy macros below
#[macro_export]
+2 -2
View File
@@ -1,8 +1,8 @@
use std::borrow::Cow;
use ironrdp_core::{
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, other_err, unexpected_message_type_err,
IntoOwned, ReadCursor, WriteCursor,
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, other_err, read_padding,
unexpected_message_type_err, IntoOwned, ReadCursor, WriteCursor,
};
use crate::gcc::{ChannelDef, ClientGccBlocks, ConferenceCreateRequest, ConferenceCreateResponse};
+2 -2
View File
@@ -1,8 +1,8 @@
//! This module contains the RDP_PRECONNECTION_PDU_V1 and RDP_PRECONNECTION_PDU_V2 structures.
use ironrdp_core::{
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, invalid_field_err_with_source, Decode,
DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, invalid_field_err_with_source, read_padding,
write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
};
use crate::Pdu;
@@ -1,8 +1,8 @@
use std::io;
use ironrdp_core::{
cast_length, decode, ensure_fixed_part_size, ensure_size, invalid_field_err, unsupported_value_err, Decode,
DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
cast_length, decode, ensure_fixed_part_size, ensure_size, invalid_field_err, unsupported_value_err, write_padding,
Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive as _, ToPrimitive as _};
@@ -3,7 +3,8 @@ mod tests;
use bitflags::bitflags;
use ironrdp_core::{
ensure_fixed_part_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
ensure_fixed_part_size, invalid_field_err, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult,
ReadCursor, WriteCursor,
};
const BITMAP_LENGTH: usize = 24;
@@ -2,7 +2,10 @@
mod tests;
use bitflags::bitflags;
use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
use ironrdp_core::{
ensure_fixed_part_size, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
pub const BITMAP_CACHE_ENTRIES_NUM: usize = 3;
@@ -2,7 +2,8 @@
mod tests;
use ironrdp_core::{
ensure_fixed_part_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
ensure_fixed_part_size, invalid_field_err, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
@@ -2,7 +2,10 @@
mod tests;
use bitflags::bitflags;
use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
use ironrdp_core::{
ensure_fixed_part_size, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
use num_traits::{FromPrimitive, ToPrimitive};
use crate::gcc::{KeyboardType, IME_FILE_NAME_SIZE};
@@ -2,7 +2,10 @@
mod tests;
use bitflags::bitflags;
use ironrdp_core::{ensure_fixed_part_size, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor};
use ironrdp_core::{
ensure_fixed_part_size, read_padding, write_padding, Decode, DecodeResult, Encode, EncodeResult, ReadCursor,
WriteCursor,
};
const SOUND_LENGTH: usize = 4;

Some files were not shown because too many files have changed in this diff Show More