| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use crate::{
|
| 6 | alloc::{
|
| 7 | AllocError,
|
| 8 | KVec, |
| 9 | },
|
| 10 | error::{
|
| 11 | to_result,
|
| 12 | Result, |
| 13 | },
|
| 14 | fmt::{
|
| 15 | self,
|
| 16 | Write, |
| 17 | },
|
| 18 | prelude::*, |
| 19 | };
|
| 20 | use core::{
|
| 21 | marker::PhantomData,
|
| 22 | ops::{
|
| 23 | Deref,
|
| 24 | DerefMut,
|
| 25 | Index, |
| 26 | }, |
| 27 | };
|
| 28 |
|
| 29 | pub use crate::prelude::CStr;
|
| 30 |
|
| 31 | pub mod parse_int;
|
| 32 |
|
| 33 | |
| 34 | #[repr(transparent)]
|
| 35 | pub struct BStr([u8]);
|
| 36 |
|
| 37 | impl BStr {
|
| 38 | |
| 39 | #[inline]
|
| 40 | pub const fn len(&self) -> usize {
|
| 41 | self.0.len()
|
| 42 | }
|
| 43 |
|
| 44 | |
| 45 | #[inline]
|
| 46 | pub const fn is_empty(&self) -> bool {
|
| 47 | self.len() == 0
|
| 48 | }
|
| 49 |
|
| 50 | |
| 51 | #[inline]
|
| 52 | pub const fn from_bytes(bytes: &[u8]) -> &Self {
|
| 53 | |
| 54 | unsafe { &*(core::ptr::from_ref(bytes) as *const BStr) }
|
| 55 | }
|
| 56 |
|
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub fn strip_prefix(&self, pattern: impl AsRef<Self>) -> Option<&BStr> {
|
| 69 | self.deref()
|
| 70 | .strip_prefix(pattern.as_ref().deref())
|
| 71 | .map(Self::from_bytes)
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | impl fmt::Display for BStr {
|
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 90 | for &b in &self.0 {
|
| 91 | match b {
|
| 92 | |
| 93 | b'\t' => f.write_str("\\t")?,
|
| 94 | b'\n' => f.write_str("\\n")?,
|
| 95 | b'\r' => f.write_str("\\r")?,
|
| 96 | |
| 97 | 0x20..=0x7e => f.write_char(b as char)?,
|
| 98 | _ => write!(f, "\\x{b:02x}")?,
|
| 99 | }
|
| 100 | }
|
| 101 | Ok(())
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | impl fmt::Debug for BStr {
|
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 122 | f.write_char('"')?;
|
| 123 | for &b in &self.0 {
|
| 124 | match b {
|
| 125 | |
| 126 | b'\t' => f.write_str("\\t")?,
|
| 127 | b'\n' => f.write_str("\\n")?,
|
| 128 | b'\r' => f.write_str("\\r")?,
|
| 129 | |
| 130 | b'\"' => f.write_str("\\\"")?,
|
| 131 | b'\\' => f.write_str("\\\\")?,
|
| 132 | |
| 133 | 0x20..=0x7e => f.write_char(b as char)?,
|
| 134 | _ => write!(f, "\\x{b:02x}")?,
|
| 135 | }
|
| 136 | }
|
| 137 | f.write_char('"')
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | impl Deref for BStr {
|
| 142 | type Target = [u8];
|
| 143 |
|
| 144 | #[inline]
|
| 145 | fn deref(&self) -> &Self::Target {
|
| 146 | &self.0
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | impl PartialEq for BStr {
|
| 151 | fn eq(&self, other: &Self) -> bool {
|
| 152 | self.deref().eq(other.deref())
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | impl<Idx> Index<Idx> for BStr
|
| 157 | where
|
| 158 | [u8]: Index<Idx, Output = [u8]>,
|
| 159 | {
|
| 160 | type Output = Self;
|
| 161 |
|
| 162 | fn index(&self, index: Idx) -> &Self::Output {
|
| 163 | BStr::from_bytes(&self.0[index])
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | impl AsRef<BStr> for [u8] {
|
| 168 | fn as_ref(&self) -> &BStr {
|
| 169 | BStr::from_bytes(self)
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | impl AsRef<BStr> for BStr {
|
| 174 | fn as_ref(&self) -> &BStr {
|
| 175 | self
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | #[macro_export]
|
| 192 | macro_rules! b_str {
|
| 193 | ($str:literal) => {{
|
| 194 | const S: &'static str = $str;
|
| 195 | const C: &'static $crate::str::BStr = $crate::str::BStr::from_bytes(S.as_bytes());
|
| 196 | C
|
| 197 | }};
|
| 198 | }
|
| 199 |
|
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | #[inline]
|
| 205 | #[expect(clippy::disallowed_methods, reason = "internal implementation")]
|
| 206 | pub const fn as_char_ptr_in_const_context(c_str: &CStr) -> *const c_char {
|
| 207 | c_str.as_ptr().cast()
|
| 208 | }
|
| 209 |
|
| 210 | mod private {
|
| 211 | pub trait Sealed {}
|
| 212 |
|
| 213 | impl Sealed for super::CStr {}
|
| 214 | }
|
| 215 |
|
| 216 | |
| 217 | pub trait CStrExt: private::Sealed {
|
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | unsafe fn from_char_ptr<'a>(ptr: *const c_char) -> &'a Self;
|
| 228 |
|
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut Self;
|
| 237 |
|
| 238 | |
| 239 | |
| 240 | |
| 241 | fn as_char_ptr(&self) -> *const c_char;
|
| 242 |
|
| 243 | |
| 244 | |
| 245 | fn to_cstring(&self) -> Result<CString, AllocError>;
|
| 246 |
|
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | fn make_ascii_lowercase(&mut self);
|
| 257 |
|
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | fn make_ascii_uppercase(&mut self);
|
| 268 |
|
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | fn to_ascii_lowercase(&self) -> Result<CString, AllocError>;
|
| 279 |
|
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | fn to_ascii_uppercase(&self) -> Result<CString, AllocError>;
|
| 290 | }
|
| 291 |
|
| 292 | impl fmt::Display for CStr {
|
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 309 | for &c in self.to_bytes() {
|
| 310 | if (0x20..0x7f).contains(&c) {
|
| 311 | |
| 312 | f.write_char(c as char)?;
|
| 313 | } else {
|
| 314 | write!(f, "\\x{c:02x}")?;
|
| 315 | }
|
| 316 | }
|
| 317 | Ok(())
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | unsafe fn to_bytes_mut(s: &mut CStr) -> &mut [u8] {
|
| 328 | |
| 329 | |
| 330 | |
| 331 | unsafe { &mut *(core::ptr::from_mut(s) as *mut [u8]) }
|
| 332 | }
|
| 333 |
|
| 334 | impl CStrExt for CStr {
|
| 335 | #[inline]
|
| 336 | #[expect(clippy::disallowed_methods, reason = "internal implementation")]
|
| 337 | unsafe fn from_char_ptr<'a>(ptr: *const c_char) -> &'a Self {
|
| 338 | |
| 339 | unsafe { CStr::from_ptr(ptr.cast()) }
|
| 340 | }
|
| 341 |
|
| 342 | #[inline]
|
| 343 | unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut Self {
|
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | unsafe { &mut *(core::ptr::from_mut(bytes) as *mut CStr) }
|
| 349 | }
|
| 350 |
|
| 351 | #[inline]
|
| 352 | #[expect(clippy::disallowed_methods, reason = "internal implementation")]
|
| 353 | fn as_char_ptr(&self) -> *const c_char {
|
| 354 | self.as_ptr().cast()
|
| 355 | }
|
| 356 |
|
| 357 | fn to_cstring(&self) -> Result<CString, AllocError> {
|
| 358 | CString::try_from(self)
|
| 359 | }
|
| 360 |
|
| 361 | fn make_ascii_lowercase(&mut self) {
|
| 362 | |
| 363 | unsafe { to_bytes_mut(self) }.make_ascii_lowercase();
|
| 364 | }
|
| 365 |
|
| 366 | fn make_ascii_uppercase(&mut self) {
|
| 367 | |
| 368 | unsafe { to_bytes_mut(self) }.make_ascii_uppercase();
|
| 369 | }
|
| 370 |
|
| 371 | fn to_ascii_lowercase(&self) -> Result<CString, AllocError> {
|
| 372 | let mut s = self.to_cstring()?;
|
| 373 |
|
| 374 | s.make_ascii_lowercase();
|
| 375 |
|
| 376 | Ok(s)
|
| 377 | }
|
| 378 |
|
| 379 | fn to_ascii_uppercase(&self) -> Result<CString, AllocError> {
|
| 380 | let mut s = self.to_cstring()?;
|
| 381 |
|
| 382 | s.make_ascii_uppercase();
|
| 383 |
|
| 384 | Ok(s)
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | impl AsRef<BStr> for CStr {
|
| 389 | #[inline]
|
| 390 | fn as_ref(&self) -> &BStr {
|
| 391 | BStr::from_bytes(self.to_bytes())
|
| 392 | }
|
| 393 | }
|
| 394 |
|
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | #[macro_export]
|
| 417 | macro_rules! c_str {
|
| 418 | |
| 419 | |
| 420 | |
| 421 | ($str:expr) => {{
|
| 422 | const S: &str = concat!($str, "\0");
|
| 423 | const C: &$crate::str::CStr = match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) {
|
| 424 | Ok(v) => v,
|
| 425 | Err(_) => panic!("string contains interior NUL"),
|
| 426 | };
|
| 427 | C
|
| 428 | }};
|
| 429 | }
|
| 430 |
|
| 431 | #[cfg(CONFIG_RUST_STR_KUNIT_TEST)]
|
| 432 | #[kunit_tests(rust_kernel_str)]
|
| 433 | mod tests {
|
| 434 | use super::*;
|
| 435 |
|
| 436 | impl From<core::ffi::FromBytesWithNulError> for Error {
|
| 437 | #[inline]
|
| 438 | fn from(_: core::ffi::FromBytesWithNulError) -> Error {
|
| 439 | EINVAL
|
| 440 | }
|
| 441 | }
|
| 442 |
|
| 443 | macro_rules! format {
|
| 444 | ($($f:tt)*) => ({
|
| 445 | CString::try_from_fmt(fmt!($($f)*))?.to_str()?
|
| 446 | })
|
| 447 | }
|
| 448 |
|
| 449 | const ALL_ASCII_CHARS: &str =
|
| 450 | "\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\
|
| 451 | \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f \
|
| 452 | !\"#$%&'()*+,-./0123456789:;<=>?@\
|
| 453 | ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\
|
| 454 | \\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\
|
| 455 | \\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\
|
| 456 | \\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\
|
| 457 | \\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\
|
| 458 | \\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\
|
| 459 | \\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\
|
| 460 | \\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\
|
| 461 | \\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff";
|
| 462 |
|
| 463 | #[test]
|
| 464 | fn test_cstr_to_str() -> Result {
|
| 465 | let cstr = c"\xf0\x9f\xa6\x80";
|
| 466 | let checked_str = cstr.to_str()?;
|
| 467 | assert_eq!(checked_str, "🦀");
|
| 468 | Ok(())
|
| 469 | }
|
| 470 |
|
| 471 | #[test]
|
| 472 | fn test_cstr_to_str_invalid_utf8() -> Result {
|
| 473 | let cstr = c"\xc3\x28";
|
| 474 | assert!(cstr.to_str().is_err());
|
| 475 | Ok(())
|
| 476 | }
|
| 477 |
|
| 478 | #[test]
|
| 479 | fn test_cstr_display() -> Result {
|
| 480 | let hello_world = c"hello, world!";
|
| 481 | assert_eq!(format!("{hello_world}"), "hello, world!");
|
| 482 | let non_printables = c"\x01\x09\x0a";
|
| 483 | assert_eq!(format!("{non_printables}"), "\\x01\\x09\\x0a");
|
| 484 | let non_ascii = c"d\xe9j\xe0 vu";
|
| 485 | assert_eq!(format!("{non_ascii}"), "d\\xe9j\\xe0 vu");
|
| 486 | let good_bytes = c"\xf0\x9f\xa6\x80";
|
| 487 | assert_eq!(format!("{good_bytes}"), "\\xf0\\x9f\\xa6\\x80");
|
| 488 | Ok(())
|
| 489 | }
|
| 490 |
|
| 491 | #[test]
|
| 492 | fn test_cstr_display_all_bytes() -> Result {
|
| 493 | let mut bytes: [u8; 256] = [0; 256];
|
| 494 | |
| 495 | for i in u8::MIN..=u8::MAX {
|
| 496 | bytes[i as usize] = i.wrapping_add(1);
|
| 497 | }
|
| 498 | let cstr = CStr::from_bytes_with_nul(&bytes)?;
|
| 499 | assert_eq!(format!("{cstr}"), ALL_ASCII_CHARS);
|
| 500 | Ok(())
|
| 501 | }
|
| 502 |
|
| 503 | #[test]
|
| 504 | fn test_cstr_debug() -> Result {
|
| 505 | let hello_world = c"hello, world!";
|
| 506 | assert_eq!(format!("{hello_world:?}"), "\"hello, world!\"");
|
| 507 | let non_printables = c"\x01\x09\x0a";
|
| 508 | assert_eq!(format!("{non_printables:?}"), "\"\\x01\\t\\n\"");
|
| 509 | let non_ascii = c"d\xe9j\xe0 vu";
|
| 510 | assert_eq!(format!("{non_ascii:?}"), "\"d\\xe9j\\xe0 vu\"");
|
| 511 | Ok(())
|
| 512 | }
|
| 513 |
|
| 514 | #[test]
|
| 515 | fn test_bstr_display() -> Result {
|
| 516 | let hello_world = BStr::from_bytes(b"hello, world!");
|
| 517 | assert_eq!(format!("{hello_world}"), "hello, world!");
|
| 518 | let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_");
|
| 519 | assert_eq!(format!("{escapes}"), "_\\t_\\n_\\r_\\_'_\"_");
|
| 520 | let others = BStr::from_bytes(b"\x01");
|
| 521 | assert_eq!(format!("{others}"), "\\x01");
|
| 522 | let non_ascii = BStr::from_bytes(b"d\xe9j\xe0 vu");
|
| 523 | assert_eq!(format!("{non_ascii}"), "d\\xe9j\\xe0 vu");
|
| 524 | let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80");
|
| 525 | assert_eq!(format!("{good_bytes}"), "\\xf0\\x9f\\xa6\\x80");
|
| 526 | Ok(())
|
| 527 | }
|
| 528 |
|
| 529 | #[test]
|
| 530 | fn test_bstr_debug() -> Result {
|
| 531 | let hello_world = BStr::from_bytes(b"hello, world!");
|
| 532 | assert_eq!(format!("{hello_world:?}"), "\"hello, world!\"");
|
| 533 | let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_");
|
| 534 | assert_eq!(format!("{escapes:?}"), "\"_\\t_\\n_\\r_\\\\_'_\\\"_\"");
|
| 535 | let others = BStr::from_bytes(b"\x01");
|
| 536 | assert_eq!(format!("{others:?}"), "\"\\x01\"");
|
| 537 | let non_ascii = BStr::from_bytes(b"d\xe9j\xe0 vu");
|
| 538 | assert_eq!(format!("{non_ascii:?}"), "\"d\\xe9j\\xe0 vu\"");
|
| 539 | let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80");
|
| 540 | assert_eq!(format!("{good_bytes:?}"), "\"\\xf0\\x9f\\xa6\\x80\"");
|
| 541 | Ok(())
|
| 542 | }
|
| 543 | }
|
| 544 |
|
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | pub struct RawFormatter {
|
| 555 | |
| 556 | beg: usize,
|
| 557 | pos: usize,
|
| 558 | end: usize,
|
| 559 | }
|
| 560 |
|
| 561 | impl RawFormatter {
|
| 562 | |
| 563 | fn new() -> Self {
|
| 564 | |
| 565 | Self {
|
| 566 | beg: 0,
|
| 567 | pos: 0,
|
| 568 | end: 0,
|
| 569 | }
|
| 570 | }
|
| 571 |
|
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | pub(crate) unsafe fn from_ptrs(pos: *mut u8, end: *mut u8) -> Self {
|
| 579 | |
| 580 | Self {
|
| 581 | beg: pos as usize,
|
| 582 | pos: pos as usize,
|
| 583 | end: end as usize,
|
| 584 | }
|
| 585 | }
|
| 586 |
|
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
|
| 594 | let pos = buf as usize;
|
| 595 | |
| 596 | |
| 597 | Self {
|
| 598 | pos,
|
| 599 | beg: pos,
|
| 600 | end: pos.saturating_add(len),
|
| 601 | }
|
| 602 | }
|
| 603 |
|
| 604 | |
| 605 | |
| 606 | |
| 607 | pub(crate) fn pos(&self) -> *mut u8 {
|
| 608 | self.pos as *mut u8
|
| 609 | }
|
| 610 |
|
| 611 | |
| 612 | pub fn bytes_written(&self) -> usize {
|
| 613 | self.pos - self.beg
|
| 614 | }
|
| 615 | }
|
| 616 |
|
| 617 | impl fmt::Write for RawFormatter {
|
| 618 | fn write_str(&mut self, s: &str) -> fmt::Result {
|
| 619 | |
| 620 | |
| 621 | let pos_new = self.pos.saturating_add(s.len());
|
| 622 |
|
| 623 | |
| 624 | let len_to_copy = core::cmp::min(pos_new, self.end).saturating_sub(self.pos);
|
| 625 |
|
| 626 | if len_to_copy > 0 {
|
| 627 | |
| 628 | |
| 629 | unsafe {
|
| 630 | core::ptr::copy_nonoverlapping(
|
| 631 | s.as_bytes().as_ptr(),
|
| 632 | self.pos as *mut u8,
|
| 633 | len_to_copy,
|
| 634 | )
|
| 635 | };
|
| 636 | }
|
| 637 |
|
| 638 | self.pos = pos_new;
|
| 639 | Ok(())
|
| 640 | }
|
| 641 | }
|
| 642 |
|
| 643 | |
| 644 | |
| 645 | |
| 646 | pub struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>);
|
| 647 |
|
| 648 | impl Formatter<'_> {
|
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
|
| 656 | |
| 657 | Self(unsafe { RawFormatter::from_buffer(buf, len) }, PhantomData)
|
| 658 | }
|
| 659 |
|
| 660 | |
| 661 | pub fn new(buffer: &mut [u8]) -> Self {
|
| 662 | |
| 663 | |
| 664 | unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len()) }
|
| 665 | }
|
| 666 | }
|
| 667 |
|
| 668 | impl Deref for Formatter<'_> {
|
| 669 | type Target = RawFormatter;
|
| 670 |
|
| 671 | fn deref(&self) -> &Self::Target {
|
| 672 | &self.0
|
| 673 | }
|
| 674 | }
|
| 675 |
|
| 676 | impl fmt::Write for Formatter<'_> {
|
| 677 | fn write_str(&mut self, s: &str) -> fmt::Result {
|
| 678 | self.0.write_str(s)?;
|
| 679 |
|
| 680 | |
| 681 | if self.0.pos > self.0.end {
|
| 682 | Err(fmt::Error)
|
| 683 | } else {
|
| 684 | Ok(())
|
| 685 | }
|
| 686 | }
|
| 687 | }
|
| 688 |
|
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | pub struct NullTerminatedFormatter<'a> {
|
| 698 | buffer: &'a mut [u8],
|
| 699 | }
|
| 700 |
|
| 701 | impl<'a> NullTerminatedFormatter<'a> {
|
| 702 | |
| 703 | pub fn new(buffer: &'a mut [u8]) -> Option<NullTerminatedFormatter<'a>> {
|
| 704 | *(buffer.first_mut()?) = 0;
|
| 705 |
|
| 706 | |
| 707 | |
| 708 | |
| 709 | Some(Self { buffer })
|
| 710 | }
|
| 711 | }
|
| 712 |
|
| 713 | impl Write for NullTerminatedFormatter<'_> {
|
| 714 | fn write_str(&mut self, s: &str) -> fmt::Result {
|
| 715 | let bytes = s.as_bytes();
|
| 716 | let len = bytes.len();
|
| 717 |
|
| 718 | |
| 719 | |
| 720 | if len > self.buffer.len() - 1 {
|
| 721 | return Err(fmt::Error);
|
| 722 | }
|
| 723 |
|
| 724 | let buffer = core::mem::take(&mut self.buffer);
|
| 725 | |
| 726 | buffer[..len].copy_from_slice(bytes);
|
| 727 | |
| 728 | self.buffer = &mut buffer[len..];
|
| 729 |
|
| 730 | |
| 731 | self.buffer[0] = 0;
|
| 732 |
|
| 733 | Ok(())
|
| 734 | }
|
| 735 | }
|
| 736 |
|
| 737 | |
| 738 | |
| 739 | |
| 740 | unsafe fn kstrtobool_raw(string: *const u8) -> Result<bool> {
|
| 741 | let mut result: bool = false;
|
| 742 |
|
| 743 | |
| 744 | |
| 745 | |
| 746 | to_result(unsafe { bindings::kstrtobool(string, &mut result) })?;
|
| 747 | Ok(result)
|
| 748 | }
|
| 749 |
|
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | pub fn kstrtobool(string: &CStr) -> Result<bool> {
|
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | unsafe { kstrtobool_raw(string.as_char_ptr()) }
|
| 802 | }
|
| 803 |
|
| 804 | |
| 805 | |
| 806 | |
| 807 | pub fn kstrtobool_bytes(bytes: &[u8]) -> Result<bool> {
|
| 808 | |
| 809 | let stack_string = [*bytes.first().unwrap_or(&0), *bytes.get(1).unwrap_or(&0), 0];
|
| 810 | |
| 811 | |
| 812 | unsafe { kstrtobool_raw(stack_string.as_ptr()) }
|
| 813 | }
|
| 814 |
|
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | pub struct CString {
|
| 841 | buf: KVec<u8>,
|
| 842 | }
|
| 843 |
|
| 844 | impl CString {
|
| 845 | |
| 846 | pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {
|
| 847 | |
| 848 | let mut f = RawFormatter::new();
|
| 849 | f.write_fmt(args)?;
|
| 850 | f.write_str("\0")?;
|
| 851 | let size = f.bytes_written();
|
| 852 |
|
| 853 | |
| 854 | let mut buf = KVec::with_capacity(size, GFP_KERNEL)?;
|
| 855 | |
| 856 | let mut f = unsafe { Formatter::from_buffer(buf.as_mut_ptr(), size) };
|
| 857 | f.write_fmt(args)?;
|
| 858 | f.write_str("\0")?;
|
| 859 |
|
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | unsafe { buf.inc_len(f.bytes_written()) };
|
| 866 |
|
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | let ptr = unsafe { bindings::memchr(buf.as_ptr().cast(), 0, f.bytes_written() - 1) };
|
| 872 | if !ptr.is_null() {
|
| 873 | return Err(EINVAL);
|
| 874 | }
|
| 875 |
|
| 876 | |
| 877 | |
| 878 | Ok(Self { buf })
|
| 879 | }
|
| 880 | }
|
| 881 |
|
| 882 | impl Deref for CString {
|
| 883 | type Target = CStr;
|
| 884 |
|
| 885 | fn deref(&self) -> &Self::Target {
|
| 886 | |
| 887 | |
| 888 | unsafe { CStr::from_bytes_with_nul_unchecked(self.buf.as_slice()) }
|
| 889 | }
|
| 890 | }
|
| 891 |
|
| 892 | impl DerefMut for CString {
|
| 893 | fn deref_mut(&mut self) -> &mut Self::Target {
|
| 894 | |
| 895 | |
| 896 | unsafe { CStr::from_bytes_with_nul_unchecked_mut(self.buf.as_mut_slice()) }
|
| 897 | }
|
| 898 | }
|
| 899 |
|
| 900 | impl<'a> TryFrom<&'a CStr> for CString {
|
| 901 | type Error = AllocError;
|
| 902 |
|
| 903 | fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
|
| 904 | let mut buf = KVec::new();
|
| 905 |
|
| 906 | buf.extend_from_slice(cstr.to_bytes_with_nul(), GFP_KERNEL)?;
|
| 907 |
|
| 908 | |
| 909 | |
| 910 | Ok(CString { buf })
|
| 911 | }
|
| 912 | }
|
| 913 |
|
| 914 | impl fmt::Debug for CString {
|
| 915 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 916 | fmt::Debug::fmt(&**self, f)
|
| 917 | }
|
| 918 | }
|