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