1 // SPDX-License-Identifier: GPL-2.0
3 //! String representations.
6 use core::fmt::{self, Write};
7 use core::ops::{self, Deref, Index};
11 error::{code::*, Error},
14 /// Byte string without UTF-8 validity guarantee.
16 /// `BStr` is simply an alias to `[u8]`, but has a more evident semantical meaning.
19 /// Creates a new [`BStr`] from a string literal.
21 /// `b_str!` converts the supplied string literal to byte string, so non-ASCII
22 /// characters can be included.
27 /// # use kernel::b_str;
28 /// # use kernel::str::BStr;
29 /// const MY_BSTR: &BStr = b_str!("My awesome BStr!");
34 const S: &'static str = $str;
35 const C: &'static $crate::str::BStr = S.as_bytes();
40 /// Possible errors when using conversion functions in [`CStr`].
41 #[derive(Debug, Clone, Copy)]
42 pub enum CStrConvertError {
43 /// Supplied bytes contain an interior `NUL`.
46 /// Supplied bytes are not terminated by `NUL`.
50 impl From<CStrConvertError> for Error {
52 fn from(_: CStrConvertError) -> Error {
57 /// A string that is guaranteed to have exactly one `NUL` byte, which is at the
60 /// Used for interoperability with kernel APIs that take C strings.
62 pub struct CStr([u8]);
65 /// Returns the length of this string excluding `NUL`.
67 pub const fn len(&self) -> usize {
68 self.len_with_nul() - 1
71 /// Returns the length of this string with `NUL`.
73 pub const fn len_with_nul(&self) -> usize {
74 // SAFETY: This is one of the invariant of `CStr`.
75 // We add a `unreachable_unchecked` here to hint the optimizer that
76 // the value returned from this function is non-zero.
77 if self.0.is_empty() {
78 unsafe { core::hint::unreachable_unchecked() };
83 /// Returns `true` if the string only includes `NUL`.
85 pub const fn is_empty(&self) -> bool {
89 /// Wraps a raw C string pointer.
93 /// `ptr` must be a valid pointer to a `NUL`-terminated C string, and it must
94 /// last at least `'a`. When `CStr` is alive, the memory pointed by `ptr`
95 /// must not be mutated.
97 pub unsafe fn from_char_ptr<'a>(ptr: *const core::ffi::c_char) -> &'a Self {
98 // SAFETY: The safety precondition guarantees `ptr` is a valid pointer
99 // to a `NUL`-terminated C string.
100 let len = unsafe { bindings::strlen(ptr) } + 1;
101 // SAFETY: Lifetime guaranteed by the safety precondition.
102 let bytes = unsafe { core::slice::from_raw_parts(ptr as _, len as _) };
103 // SAFETY: As `len` is returned by `strlen`, `bytes` does not contain interior `NUL`.
104 // As we have added 1 to `len`, the last byte is known to be `NUL`.
105 unsafe { Self::from_bytes_with_nul_unchecked(bytes) }
108 /// Creates a [`CStr`] from a `[u8]`.
110 /// The provided slice must be `NUL`-terminated, does not contain any
111 /// interior `NUL` bytes.
112 pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, CStrConvertError> {
113 if bytes.is_empty() {
114 return Err(CStrConvertError::NotNulTerminated);
116 if bytes[bytes.len() - 1] != 0 {
117 return Err(CStrConvertError::NotNulTerminated);
120 // `i + 1 < bytes.len()` allows LLVM to optimize away bounds checking,
121 // while it couldn't optimize away bounds checks for `i < bytes.len() - 1`.
122 while i + 1 < bytes.len() {
124 return Err(CStrConvertError::InteriorNul);
128 // SAFETY: We just checked that all properties hold.
129 Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
132 /// Creates a [`CStr`] from a `[u8]` without performing any additional
137 /// `bytes` *must* end with a `NUL` byte, and should only have a single
138 /// `NUL` byte (or the string will be truncated).
140 pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
141 // SAFETY: Properties of `bytes` guaranteed by the safety precondition.
142 unsafe { core::mem::transmute(bytes) }
145 /// Returns a C pointer to the string.
147 pub const fn as_char_ptr(&self) -> *const core::ffi::c_char {
151 /// Convert the string to a byte slice without the trailing 0 byte.
153 pub fn as_bytes(&self) -> &[u8] {
154 &self.0[..self.len()]
157 /// Convert the string to a byte slice containing the trailing 0 byte.
159 pub const fn as_bytes_with_nul(&self) -> &[u8] {
163 /// Yields a [`&str`] slice if the [`CStr`] contains valid UTF-8.
165 /// If the contents of the [`CStr`] are valid UTF-8 data, this
166 /// function will return the corresponding [`&str`] slice. Otherwise,
167 /// it will return an error with details of where UTF-8 validation failed.
172 /// # use kernel::str::CStr;
173 /// let cstr = CStr::from_bytes_with_nul(b"foo\0").unwrap();
174 /// assert_eq!(cstr.to_str(), Ok("foo"));
177 pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
178 core::str::from_utf8(self.as_bytes())
181 /// Unsafely convert this [`CStr`] into a [`&str`], without checking for
186 /// The contents must be valid UTF-8.
191 /// # use kernel::c_str;
192 /// # use kernel::str::CStr;
193 /// // SAFETY: String literals are guaranteed to be valid UTF-8
194 /// // by the Rust compiler.
195 /// let bar = c_str!("ツ");
196 /// assert_eq!(unsafe { bar.as_str_unchecked() }, "ツ");
199 pub unsafe fn as_str_unchecked(&self) -> &str {
200 unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
204 impl fmt::Display for CStr {
205 /// Formats printable ASCII characters, escaping the rest.
208 /// # use kernel::c_str;
209 /// # use kernel::str::CStr;
210 /// # use kernel::str::CString;
211 /// let penguin = c_str!("🐧");
212 /// let s = CString::try_from_fmt(fmt!("{}", penguin)).unwrap();
213 /// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
215 /// let ascii = c_str!("so \"cool\"");
216 /// let s = CString::try_from_fmt(fmt!("{}", ascii)).unwrap();
217 /// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes());
219 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220 for &c in self.as_bytes() {
221 if (0x20..0x7f).contains(&c) {
222 // Printable character.
223 f.write_char(c as char)?;
225 write!(f, "\\x{:02x}", c)?;
232 impl fmt::Debug for CStr {
233 /// Formats printable ASCII characters with a double quote on either end, escaping the rest.
236 /// # use kernel::c_str;
237 /// # use kernel::str::CStr;
238 /// # use kernel::str::CString;
239 /// let penguin = c_str!("🐧");
240 /// let s = CString::try_from_fmt(fmt!("{:?}", penguin)).unwrap();
241 /// assert_eq!(s.as_bytes_with_nul(), "\"\\xf0\\x9f\\x90\\xa7\"\0".as_bytes());
243 /// // Embedded double quotes are escaped.
244 /// let ascii = c_str!("so \"cool\"");
245 /// let s = CString::try_from_fmt(fmt!("{:?}", ascii)).unwrap();
246 /// assert_eq!(s.as_bytes_with_nul(), "\"so \\\"cool\\\"\"\0".as_bytes());
248 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250 for &c in self.as_bytes() {
252 // Printable characters.
253 b'\"' => f.write_str("\\\"")?,
254 0x20..=0x7e => f.write_char(c as char)?,
255 _ => write!(f, "\\x{:02x}", c)?,
262 impl AsRef<BStr> for CStr {
264 fn as_ref(&self) -> &BStr {
269 impl Deref for CStr {
273 fn deref(&self) -> &Self::Target {
278 impl Index<ops::RangeFrom<usize>> for CStr {
282 fn index(&self, index: ops::RangeFrom<usize>) -> &Self::Output {
283 // Delegate bounds checking to slice.
284 // Assign to _ to mute clippy's unnecessary operation warning.
285 let _ = &self.as_bytes()[index.start..];
286 // SAFETY: We just checked the bounds.
287 unsafe { Self::from_bytes_with_nul_unchecked(&self.0[index.start..]) }
291 impl Index<ops::RangeFull> for CStr {
295 fn index(&self, _index: ops::RangeFull) -> &Self::Output {
303 // Marker trait for index types that can be forward to `BStr`.
304 pub trait CStrIndex {}
306 impl CStrIndex for usize {}
307 impl CStrIndex for ops::Range<usize> {}
308 impl CStrIndex for ops::RangeInclusive<usize> {}
309 impl CStrIndex for ops::RangeToInclusive<usize> {}
312 impl<Idx> Index<Idx> for CStr
314 Idx: private::CStrIndex,
317 type Output = <BStr as Index<Idx>>::Output;
320 fn index(&self, index: Idx) -> &Self::Output {
321 &self.as_bytes()[index]
325 /// Creates a new [`CStr`] from a string literal.
327 /// The string literal should not contain any `NUL` bytes.
332 /// # use kernel::c_str;
333 /// # use kernel::str::CStr;
334 /// const MY_CSTR: &CStr = c_str!("My awesome CStr!");
339 const S: &str = concat!($str, "\0");
340 const C: &$crate::str::CStr = match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) {
342 Err(_) => panic!("string contains interior NUL"),
353 fn test_cstr_to_str() {
354 let good_bytes = b"\xf0\x9f\xa6\x80\0";
355 let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
356 let checked_str = checked_cstr.to_str().unwrap();
357 assert_eq!(checked_str, "🦀");
362 fn test_cstr_to_str_panic() {
363 let bad_bytes = b"\xc3\x28\0";
364 let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap();
365 checked_cstr.to_str().unwrap();
369 fn test_cstr_as_str_unchecked() {
370 let good_bytes = b"\xf0\x9f\x90\xA7\0";
371 let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
372 let unchecked_str = unsafe { checked_cstr.as_str_unchecked() };
373 assert_eq!(unchecked_str, "🐧");
377 /// Allows formatting of [`fmt::Arguments`] into a raw buffer.
379 /// It does not fail if callers write past the end of the buffer so that they can calculate the
380 /// size required to fit everything.
384 /// The memory region between `pos` (inclusive) and `end` (exclusive) is valid for writes if `pos`
385 /// is less than `end`.
386 pub(crate) struct RawFormatter {
387 // Use `usize` to use `saturating_*` functions.
394 /// Creates a new instance of [`RawFormatter`] with an empty buffer.
396 // INVARIANT: The buffer is empty, so the region that needs to be writable is empty.
404 /// Creates a new instance of [`RawFormatter`] with the given buffer pointers.
408 /// If `pos` is less than `end`, then the region between `pos` (inclusive) and `end`
409 /// (exclusive) must be valid for writes for the lifetime of the returned [`RawFormatter`].
410 pub(crate) unsafe fn from_ptrs(pos: *mut u8, end: *mut u8) -> Self {
411 // INVARIANT: The safety requirements guarantee the type invariants.
419 /// Creates a new instance of [`RawFormatter`] with the given buffer.
423 /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
424 /// for the lifetime of the returned [`RawFormatter`].
425 pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
426 let pos = buf as usize;
427 // INVARIANT: We ensure that `end` is never less then `buf`, and the safety requirements
428 // guarantees that the memory region is valid for writes.
432 end: pos.saturating_add(len),
436 /// Returns the current insert position.
438 /// N.B. It may point to invalid memory.
439 pub(crate) fn pos(&self) -> *mut u8 {
443 /// Return the number of bytes written to the formatter.
444 pub(crate) fn bytes_written(&self) -> usize {
449 impl fmt::Write for RawFormatter {
450 fn write_str(&mut self, s: &str) -> fmt::Result {
451 // `pos` value after writing `len` bytes. This does not have to be bounded by `end`, but we
452 // don't want it to wrap around to 0.
453 let pos_new = self.pos.saturating_add(s.len());
455 // Amount that we can copy. `saturating_sub` ensures we get 0 if `pos` goes past `end`.
456 let len_to_copy = core::cmp::min(pos_new, self.end).saturating_sub(self.pos);
459 // SAFETY: If `len_to_copy` is non-zero, then we know `pos` has not gone past `end`
460 // yet, so it is valid for write per the type invariants.
462 core::ptr::copy_nonoverlapping(
463 s.as_bytes().as_ptr(),
475 /// Allows formatting of [`fmt::Arguments`] into a raw buffer.
477 /// Fails if callers attempt to write more than will fit in the buffer.
478 pub(crate) struct Formatter(RawFormatter);
481 /// Creates a new instance of [`Formatter`] with the given buffer.
485 /// The memory region starting at `buf` and extending for `len` bytes must be valid for writes
486 /// for the lifetime of the returned [`Formatter`].
487 pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
488 // SAFETY: The safety requirements of this function satisfy those of the callee.
489 Self(unsafe { RawFormatter::from_buffer(buf, len) })
493 impl Deref for Formatter {
494 type Target = RawFormatter;
496 fn deref(&self) -> &Self::Target {
501 impl fmt::Write for Formatter {
502 fn write_str(&mut self, s: &str) -> fmt::Result {
503 self.0.write_str(s)?;
505 // Fail the request if we go past the end of the buffer.
506 if self.0.pos > self.0.end {
514 /// An owned string that is guaranteed to have exactly one `NUL` byte, which is at the end.
516 /// Used for interoperability with kernel APIs that take C strings.
520 /// The string is always `NUL`-terminated and contains no other `NUL` bytes.
525 /// use kernel::str::CString;
527 /// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20)).unwrap();
528 /// assert_eq!(s.as_bytes_with_nul(), "abc1020\0".as_bytes());
530 /// let tmp = "testing";
531 /// let s = CString::try_from_fmt(fmt!("{tmp}{}", 123)).unwrap();
532 /// assert_eq!(s.as_bytes_with_nul(), "testing123\0".as_bytes());
534 /// // This fails because it has an embedded `NUL` byte.
535 /// let s = CString::try_from_fmt(fmt!("a\0b{}", 123));
536 /// assert_eq!(s.is_ok(), false);
543 /// Creates an instance of [`CString`] from the given formatted arguments.
544 pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {
545 // Calculate the size needed (formatted string plus `NUL` terminator).
546 let mut f = RawFormatter::new();
549 let size = f.bytes_written();
551 // Allocate a vector with the required number of bytes, and write to it.
552 let mut buf = Vec::try_with_capacity(size)?;
553 // SAFETY: The buffer stored in `buf` is at least of size `size` and is valid for writes.
554 let mut f = unsafe { Formatter::from_buffer(buf.as_mut_ptr(), size) };
558 // SAFETY: The number of bytes that can be written to `f` is bounded by `size`, which is
559 // `buf`'s capacity. The contents of the buffer have been initialised by writes to `f`.
560 unsafe { buf.set_len(f.bytes_written()) };
562 // Check that there are no `NUL` bytes before the end.
563 // SAFETY: The buffer is valid for read because `f.bytes_written()` is bounded by `size`
564 // (which the minimum buffer size) and is non-zero (we wrote at least the `NUL` terminator)
565 // so `f.bytes_written() - 1` doesn't underflow.
566 let ptr = unsafe { bindings::memchr(buf.as_ptr().cast(), 0, (f.bytes_written() - 1) as _) };
571 // INVARIANT: We wrote the `NUL` terminator and checked above that no other `NUL` bytes
572 // exist in the buffer.
577 impl Deref for CString {
580 fn deref(&self) -> &Self::Target {
581 // SAFETY: The type invariants guarantee that the string is `NUL`-terminated and that no
582 // other `NUL` bytes exist.
583 unsafe { CStr::from_bytes_with_nul_unchecked(self.buf.as_slice()) }
587 /// A convenience alias for [`core::format_args`].
590 ($($f:tt)*) => ( core::format_args!($($f)*) )