|
| 1 | +use super::chars::{Char16, Char8, NUL_16, NUL_8}; |
| 2 | +use core::convert::TryInto; |
| 3 | +use core::result::Result; |
| 4 | +use core::slice; |
| 5 | + |
| 6 | +/// Errors which can occur during checked [uN] -> CStrN conversions |
| 7 | +pub enum FromSliceWithNulError { |
| 8 | + /// An invalid character was encountered before the end of the slice |
| 9 | + InvalidChar(usize), |
| 10 | + |
| 11 | + /// A null character was encountered before the end of the slice |
| 12 | + InteriorNul(usize), |
| 13 | + |
| 14 | + /// The slice was not null-terminated |
| 15 | + NotNulTerminated, |
| 16 | +} |
| 17 | + |
| 18 | +/// A Latin-1 null-terminated string |
| 19 | +/// |
| 20 | +/// This type is largely inspired by std::ffi::CStr, see documentation of CStr |
| 21 | +/// for more details on its semantics. |
| 22 | +/// |
| 23 | +#[repr(transparent)] |
| 24 | +pub struct CStr8([Char8]); |
| 25 | + |
| 26 | +impl CStr8 { |
| 27 | + /// Wraps a raw UEFI string with a safe C string wrapper |
| 28 | + pub unsafe fn from_ptr<'a>(ptr: *const Char8) -> &'a Self { |
| 29 | + let mut len = 0; |
| 30 | + while *ptr.add(len) != NUL_8 { |
| 31 | + len += 1 |
| 32 | + } |
| 33 | + let ptr = ptr as *const u8; |
| 34 | + Self::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1)) |
| 35 | + } |
| 36 | + |
| 37 | + /// Creates a C string wrapper from bytes |
| 38 | + pub fn from_bytes_with_nul(chars: &[u8]) -> Result<&Self, FromSliceWithNulError> { |
| 39 | + let nul_pos = chars.iter().position(|&c| c == 0); |
| 40 | + if let Some(nul_pos) = nul_pos { |
| 41 | + if nul_pos + 1 != chars.len() { |
| 42 | + return Err(FromSliceWithNulError::InteriorNul(nul_pos)); |
| 43 | + } |
| 44 | + Ok(unsafe { Self::from_bytes_with_nul_unchecked(chars) }) |
| 45 | + } else { |
| 46 | + Err(FromSliceWithNulError::NotNulTerminated) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Unsafely creates a C string wrapper from bytes |
| 51 | + pub unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self { |
| 52 | + &*(chars as *const [u8] as *const Self) |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns the inner pointer to this C string |
| 56 | + pub fn as_ptr(&self) -> *const Char8 { |
| 57 | + self.0.as_ptr() |
| 58 | + } |
| 59 | + |
| 60 | + /// Converts this C string to a slice of bytes |
| 61 | + pub fn to_bytes(&self) -> &[u8] { |
| 62 | + let chars = self.to_bytes_with_nul(); |
| 63 | + &chars[..chars.len() - 1] |
| 64 | + } |
| 65 | + |
| 66 | + /// Converts this C string to a slice of bytes containing the trailing 0 char |
| 67 | + pub fn to_bytes_with_nul(&self) -> &[u8] { |
| 68 | + unsafe { &*(&self.0 as *const [Char8] as *const [u8]) } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// An UCS-2 null-terminated string |
| 73 | +/// |
| 74 | +/// This type is largely inspired by std::ffi::CStr, see documentation of CStr |
| 75 | +/// for more details on its semantics. |
| 76 | +/// |
| 77 | +#[repr(transparent)] |
| 78 | +pub struct CStr16([Char16]); |
| 79 | + |
| 80 | +impl CStr16 { |
| 81 | + /// Wraps a raw UEFI string with a safe C string wrapper |
| 82 | + pub unsafe fn from_ptr<'a>(ptr: *const Char16) -> &'a Self { |
| 83 | + let mut len = 0; |
| 84 | + while *ptr.add(len) != NUL_16 { |
| 85 | + len += 1 |
| 86 | + } |
| 87 | + let ptr = ptr as *const u16; |
| 88 | + Self::from_u16_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1)) |
| 89 | + } |
| 90 | + |
| 91 | + /// Creates a C string wrapper from a u16 slice |
| 92 | + /// |
| 93 | + /// Since not every u16 value is a valid UCS-2 code point, this function |
| 94 | + /// must do a bit more validity checking than CStr::from_bytes_with_nul |
| 95 | + pub fn from_u16_with_nul(codes: &[u16]) -> Result<&Self, FromSliceWithNulError> { |
| 96 | + for (pos, &code) in codes.iter().enumerate() { |
| 97 | + match code.try_into() { |
| 98 | + Ok(NUL_16) => { |
| 99 | + if pos != codes.len() - 1 { |
| 100 | + return Err(FromSliceWithNulError::InteriorNul(pos)); |
| 101 | + } else { |
| 102 | + return Ok(unsafe { Self::from_u16_with_nul_unchecked(codes) }); |
| 103 | + } |
| 104 | + } |
| 105 | + Err(_) => { |
| 106 | + return Err(FromSliceWithNulError::InvalidChar(pos)); |
| 107 | + } |
| 108 | + _ => {} |
| 109 | + } |
| 110 | + } |
| 111 | + Err(FromSliceWithNulError::NotNulTerminated) |
| 112 | + } |
| 113 | + |
| 114 | + /// Unsafely creates a C string wrapper from a u16 slice. |
| 115 | + pub unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self { |
| 116 | + &*(codes as *const [u16] as *const Self) |
| 117 | + } |
| 118 | + |
| 119 | + /// Returns the inner pointer to this C string |
| 120 | + pub fn as_ptr(&self) -> *const Char16 { |
| 121 | + self.0.as_ptr() |
| 122 | + } |
| 123 | + |
| 124 | + /// Converts this C string to a u16 slice |
| 125 | + pub fn to_u16_slice(&self) -> &[u16] { |
| 126 | + let chars = self.to_u16_slice_with_nul(); |
| 127 | + &chars[..chars.len() - 1] |
| 128 | + } |
| 129 | + |
| 130 | + /// Converts this C string to a u16 slice containing the trailing 0 char |
| 131 | + pub fn to_u16_slice_with_nul(&self) -> &[u16] { |
| 132 | + unsafe { &*(&self.0 as *const [Char16] as *const [u16]) } |
| 133 | + } |
| 134 | +} |
0 commit comments