Mercurial > crates > nonstick
diff src/libpam/memory.rs @ 77:351bdc13005e
Update the libpam module to work with the new structure.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 08 Jun 2025 01:03:46 -0400 |
parents | c30811b4afae |
children | 002adfb98c5c |
line wrap: on
line diff
--- a/src/libpam/memory.rs Sat Jun 07 18:55:27 2025 -0400 +++ b/src/libpam/memory.rs Sun Jun 08 01:03:46 2025 -0400 @@ -1,10 +1,10 @@ //! Things for dealing with memory. -use crate::ErrorCode; +use crate::conv::BorrowedBinaryData; use crate::Result; +use crate::{BinaryData, ErrorCode}; use std::ffi::{c_char, c_void, CStr, CString}; use std::marker::{PhantomData, PhantomPinned}; -use std::result::Result as StdResult; use std::{ptr, slice}; /// Makes whatever it's in not [`Send`], [`Sync`], or [`Unpin`]. @@ -66,10 +66,10 @@ /// /// - it allocates data on the C heap with [`libc::malloc`]. /// - it doesn't take ownership of the data passed in. -pub fn malloc_str(text: impl AsRef<str>) -> StdResult<*mut c_char, NulError> { - let data = text.as_ref().as_bytes(); - if let Some(nul) = data.iter().position(|x| *x == 0) { - return Err(NulError(nul)); +pub fn malloc_str(text: &str) -> Result<*mut c_char> { + let data = text.as_bytes(); + if data.contains(&0) { + return Err(ErrorCode::ConversationError); } unsafe { let data_alloc = libc::calloc(data.len() + 1, 1); @@ -110,11 +110,9 @@ impl CBinaryData { /// Copies the given data to a new BinaryData on the heap. - pub fn alloc(source: &[u8], data_type: u8) -> StdResult<*mut CBinaryData, TooBigError> { - let buffer_size = u32::try_from(source.len() + 5).map_err(|_| TooBigError { - max: (u32::MAX - 5) as usize, - actual: source.len(), - })?; + pub fn alloc(source: &[u8], data_type: u8) -> Result<*mut CBinaryData> { + let buffer_size = + u32::try_from(source.len() + 5).map_err(|_| ErrorCode::ConversationError)?; // SAFETY: We're only allocating here. let data = unsafe { let dest_buffer: *mut CBinaryData = libc::malloc(buffer_size as usize).cast(); @@ -132,13 +130,6 @@ u32::from_be_bytes(self.total_length).saturating_sub(5) as usize } - pub fn contents(&self) -> &[u8] { - unsafe { slice::from_raw_parts(self.data.as_ptr(), self.length()) } - } - pub fn data_type(&self) -> u8 { - self.data_type - } - /// Clears this data and frees it. pub unsafe fn zero_contents(&mut self) { let contents = slice::from_raw_parts_mut(self.data.as_mut_ptr(), self.length()); @@ -150,16 +141,19 @@ } } -#[derive(Debug, thiserror::Error)] -#[error("null byte within input at byte {0}")] -pub struct NulError(pub usize); +impl<'a> From<&'a CBinaryData> for BorrowedBinaryData<'a> { + fn from(value: &'a CBinaryData) -> Self { + BorrowedBinaryData::new( + unsafe { slice::from_raw_parts(value.data.as_ptr(), value.length()) }, + value.data_type, + ) + } +} -/// Returned when trying to fit too much data into a binary message. -#[derive(Debug, thiserror::Error)] -#[error("cannot create a message of {actual} bytes; maximum is {max}")] -pub struct TooBigError { - pub actual: usize, - pub max: usize, +impl From<Option<&'_ CBinaryData>> for BinaryData { + fn from(value: Option<&CBinaryData>) -> Self { + value.map(BorrowedBinaryData::from).map(Into::into).unwrap_or_default() + } } #[cfg(test)]