Skip to content

Commit 0e27599

Browse files
committed
Make marshall and unmarshall generic
Since the `marshall` and `unmarshall` methods can now fall back on the more generic `_offset` methods, we can rely on default implementations directly. Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
1 parent f0f2906 commit 0e27599

File tree

3 files changed

+18
-89
lines changed

3 files changed

+18
-89
lines changed

tss-esapi/src/constants/command_code.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,6 @@ impl From<CommandCode> for TPM2_CC {
158158
impl Marshall for CommandCode {
159159
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2_CC>();
160160

161-
/// Produce a marshalled [TPM2_CC]
162-
fn marshall(&self) -> Result<Vec<u8>> {
163-
let mut buffer = vec![0; Self::BUFFER_SIZE];
164-
let mut offset = 0;
165-
166-
self.marshall_offset(&mut buffer, &mut offset)?;
167-
168-
let checked_offset = usize::try_from(offset).map_err(|e| {
169-
error!("Failed to parse offset as usize: {}", e);
170-
Error::local_error(WrapperErrorKind::InvalidParam)
171-
})?;
172-
173-
buffer.truncate(checked_offset);
174-
175-
Ok(buffer)
176-
}
177-
178161
fn marshall_offset(
179162
&self,
180163
marshalled_data: &mut [u8],
@@ -201,11 +184,6 @@ impl Marshall for CommandCode {
201184
}
202185

203186
impl UnMarshall for CommandCode {
204-
/// Unmarshall the structure from [`TPM2_CC`]
205-
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
206-
CommandCode::unmarshall_offset(marshalled_data, &mut 0)
207-
}
208-
209187
fn unmarshall_offset(
210188
marshalled_data: &[u8],
211189
offset: &mut std::os::raw::c_ulong,

tss-esapi/src/interface_types/structure_tags.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,6 @@ impl TryFrom<TPMI_ST_ATTEST> for AttestationType {
7878
impl Marshall for AttestationType {
7979
const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_ATTEST>();
8080

81-
/// Produce a marshalled [`TPMI_ST_ATTEST`]
82-
fn marshall(&self) -> Result<Vec<u8>> {
83-
let mut buffer = vec![0; Self::BUFFER_SIZE];
84-
let mut offset = 0;
85-
86-
self.marshall_offset(&mut buffer, &mut offset)?;
87-
88-
let checked_offset = usize::try_from(offset).map_err(|e| {
89-
error!("Failed to parse offset as usize: {}", e);
90-
Error::local_error(WrapperErrorKind::InvalidParam)
91-
})?;
92-
93-
buffer.truncate(checked_offset);
94-
95-
Ok(buffer)
96-
}
97-
9881
fn marshall_offset(
9982
&self,
10083
marshalled_data: &mut [u8],
@@ -122,11 +105,6 @@ impl Marshall for AttestationType {
122105
}
123106

124107
impl UnMarshall for AttestationType {
125-
/// Unmarshall the structure from [`TPMI_ST_ATTEST`]
126-
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
127-
AttestationType::unmarshall_offset(marshalled_data, &mut 0)
128-
}
129-
130108
fn unmarshall_offset(
131109
marshalled_data: &[u8],
132110
offset: &mut std::os::raw::c_ulong,
@@ -200,23 +178,6 @@ impl TryFrom<TPMI_ST_COMMAND_TAG> for CommandTag {
200178
impl Marshall for CommandTag {
201179
const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_COMMAND_TAG>();
202180

203-
/// Produce a marshalled [`TPMI_ST_COMMAND_TAG`]
204-
fn marshall(&self) -> Result<Vec<u8>> {
205-
let mut buffer = vec![0; Self::BUFFER_SIZE];
206-
let mut offset = 0;
207-
208-
self.marshall_offset(&mut buffer, &mut offset)?;
209-
210-
let checked_offset = usize::try_from(offset).map_err(|e| {
211-
error!("Failed to parse offset as usize: {}", e);
212-
Error::local_error(WrapperErrorKind::InvalidParam)
213-
})?;
214-
215-
buffer.truncate(checked_offset);
216-
217-
Ok(buffer)
218-
}
219-
220181
fn marshall_offset(
221182
&self,
222183
marshalled_data: &mut [u8],
@@ -244,11 +205,6 @@ impl Marshall for CommandTag {
244205
}
245206

246207
impl UnMarshall for CommandTag {
247-
/// Unmarshall the structure from [`TPMI_ST_COMMAND_TAG`]
248-
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
249-
CommandTag::unmarshall_offset(marshalled_data, &mut 0)
250-
}
251-
252208
fn unmarshall_offset(
253209
marshalled_data: &[u8],
254210
offset: &mut std::os::raw::c_ulong,

tss-esapi/src/traits.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ use crate::{Error, Result, ReturnCode, WrapperErrorKind};
1212
pub trait Marshall: Sized {
1313
const BUFFER_SIZE: usize;
1414
/// Returns the type in the form of marshalled data
15-
fn marshall(&self) -> Result<Vec<u8>>;
15+
fn marshall(&self) -> Result<Vec<u8>> {
16+
let mut buffer = vec![0; Self::BUFFER_SIZE];
17+
let mut offset = 0;
18+
19+
self.marshall_offset(&mut buffer, &mut offset)?;
20+
21+
let checked_offset = usize::try_from(offset).map_err(|e| {
22+
error!("Failed to parse offset as usize: {}", e);
23+
Error::local_error(WrapperErrorKind::InvalidParam)
24+
})?;
25+
26+
buffer.truncate(checked_offset);
27+
28+
Ok(buffer)
29+
}
1630

1731
/// Writes the type in the form of marshalled data to `marshalled_data`,
1832
/// and modifies the `offset` to point to the first byte in the buffer
@@ -30,7 +44,9 @@ pub trait Marshall: Sized {
3044
/// TPM marshalled data.
3145
pub trait UnMarshall: Sized {
3246
/// Creates the type from marshalled data.
33-
fn unmarshall(marshalled_data: &[u8]) -> Result<Self>;
47+
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
48+
Self::unmarshall_offset(marshalled_data, &mut 0)
49+
}
3450

3551
/// Creates the type from the marshalled data, and modifies
3652
/// the `offset` to point to the first byte in the `marshalled_data`
@@ -46,23 +62,6 @@ pub trait UnMarshall: Sized {
4662
impl Marshall for u32 {
4763
const BUFFER_SIZE: usize = std::mem::size_of::<UINT32>();
4864

49-
/// Produce a marshalled [UINT32]
50-
fn marshall(&self) -> Result<Vec<u8>> {
51-
let mut buffer = vec![0; Self::BUFFER_SIZE];
52-
let mut offset = 0;
53-
54-
self.marshall_offset(&mut buffer, &mut offset)?;
55-
56-
let checked_offset = usize::try_from(offset).map_err(|e| {
57-
error!("Failed to parse offset as usize: {}", e);
58-
Error::local_error(WrapperErrorKind::InvalidParam)
59-
})?;
60-
61-
buffer.truncate(checked_offset);
62-
63-
Ok(buffer)
64-
}
65-
6665
fn marshall_offset(
6766
&self,
6867
marshalled_data: &mut [u8],
@@ -90,10 +89,6 @@ impl Marshall for u32 {
9089
}
9190

9291
impl UnMarshall for u32 {
93-
fn unmarshall(marshalled_data: &[u8]) -> Result<Self> {
94-
u32::unmarshall_offset(marshalled_data, &mut 0)
95-
}
96-
9792
fn unmarshall_offset(
9893
marshalled_data: &[u8],
9994
offset: &mut std::os::raw::c_ulong,

0 commit comments

Comments
 (0)