#[must_use]
pub fn tpm_offset(base: &[u8], cursor: &[u8]) -> usize {
let base_addr = base.as_ptr() as usize;
let cursor_addr = cursor.as_ptr() as usize;
cursor_addr.saturating_sub(base_addr).min(base.len())
}
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub const fn tpm_value(value: usize) -> u64 {
value as u64
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[non_exhaustive]
pub enum TpmError {
BufferOverflow {
offset: usize,
needed: usize,
available: usize,
},
IntegerTooLarge {
offset: usize,
value: u64,
},
InvalidBoolean {
offset: usize,
value: u64,
},
InvalidCc {
offset: usize,
value: u64,
},
InvalidMagicNumber {
offset: usize,
value: u64,
},
InvalidRc {
offset: usize,
value: u64,
},
InvalidTag {
offset: usize,
value: u64,
},
TooManyBytes {
offset: usize,
limit: usize,
actual: usize,
},
TooManyItems {
offset: usize,
limit: usize,
actual: usize,
},
TrailingData {
offset: usize,
actual: usize,
},
UnexpectedEnd {
offset: usize,
needed: usize,
available: usize,
},
VariantNotAvailable {
offset: usize,
value: u64,
},
}
impl TpmError {
#[must_use]
pub const fn offset(&self) -> usize {
match *self {
Self::BufferOverflow { offset, .. }
| Self::IntegerTooLarge { offset, .. }
| Self::InvalidBoolean { offset, .. }
| Self::InvalidCc { offset, .. }
| Self::InvalidMagicNumber { offset, .. }
| Self::InvalidRc { offset, .. }
| Self::InvalidTag { offset, .. }
| Self::TooManyBytes { offset, .. }
| Self::TooManyItems { offset, .. }
| Self::TrailingData { offset, .. }
| Self::UnexpectedEnd { offset, .. }
| Self::VariantNotAvailable { offset, .. } => offset,
}
}
#[must_use]
pub const fn rebase(self, delta: usize) -> Self {
match self {
Self::BufferOverflow {
offset,
needed,
available,
} => Self::BufferOverflow {
offset: offset.saturating_add(delta),
needed,
available,
},
Self::IntegerTooLarge { offset, value } => Self::IntegerTooLarge {
offset: offset.saturating_add(delta),
value,
},
Self::InvalidBoolean { offset, value } => Self::InvalidBoolean {
offset: offset.saturating_add(delta),
value,
},
Self::InvalidCc { offset, value } => Self::InvalidCc {
offset: offset.saturating_add(delta),
value,
},
Self::InvalidMagicNumber { offset, value } => Self::InvalidMagicNumber {
offset: offset.saturating_add(delta),
value,
},
Self::InvalidRc { offset, value } => Self::InvalidRc {
offset: offset.saturating_add(delta),
value,
},
Self::InvalidTag { offset, value } => Self::InvalidTag {
offset: offset.saturating_add(delta),
value,
},
Self::TooManyBytes {
offset,
limit,
actual,
} => Self::TooManyBytes {
offset: offset.saturating_add(delta),
limit,
actual,
},
Self::TooManyItems {
offset,
limit,
actual,
} => Self::TooManyItems {
offset: offset.saturating_add(delta),
limit,
actual,
},
Self::TrailingData { offset, actual } => Self::TrailingData {
offset: offset.saturating_add(delta),
actual,
},
Self::UnexpectedEnd {
offset,
needed,
available,
} => Self::UnexpectedEnd {
offset: offset.saturating_add(delta),
needed,
available,
},
Self::VariantNotAvailable { offset, value } => Self::VariantNotAvailable {
offset: offset.saturating_add(delta),
value,
},
}
}
}
impl core::fmt::Display for TpmError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let name = match self {
Self::BufferOverflow { .. } => "buffer overflow",
Self::IntegerTooLarge { .. } => "integer too large",
Self::InvalidBoolean { .. } => "invalid boolean",
Self::InvalidCc { .. } => "invalid cc",
Self::InvalidMagicNumber { .. } => "invalid magic number",
Self::InvalidRc { .. } => "invalid rc",
Self::InvalidTag { .. } => "invalid tag",
Self::TooManyBytes { .. } => "too many bytes",
Self::TooManyItems { .. } => "too many items",
Self::TrailingData { .. } => "trailing data",
Self::UnexpectedEnd { .. } => "unexpected end",
Self::VariantNotAvailable { .. } => "variant not available",
};
f.write_str(name)
}
}
impl core::error::Error for TpmError {}
pub type TpmResult<T> = Result<T, TpmError>;