1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 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 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 797
// Copyright 2023 Colin Finck <colin@reactos.org>
// SPDX-License-Identifier: MIT OR Apache-2.0
use ::alloc::alloc::{self, Layout};
use ::alloc::string::String;
use widestring::{U16CStr, U16Str};
use core::cmp::Ordering;
use core::iter::once;
use core::ops::{Add, AddAssign, Deref, DerefMut};
use core::{fmt, mem, ptr};
use crate::error::{NtStringError, Result};
use crate::helpers::RawNtString;
use crate::traits::TryExtend;
use super::{impl_eq, impl_partial_cmp, NtUnicodeStr, NtUnicodeStrMut};
/// An allocated, owned, and growable variant of `UNICODE_STRING` (equivalent of [`String`]).
///
/// See the [module-level documentation](super) for more details.
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[derive(Debug)]
#[repr(transparent)]
pub struct NtUnicodeString {
raw: RawNtString<*mut u16>,
}
impl NtUnicodeString {
/// Creates an empty [`NtUnicodeString`].
///
/// This operation won't allocate any buffer.
/// Therefore, length and capacity will both be zero.
pub fn new() -> Self {
Self {
raw: RawNtString {
length: 0,
maximum_length: 0,
buffer: ptr::null_mut(),
},
}
}
/// Returns a mutable [`NtUnicodeStrMut`] reference for this string.
pub fn as_unicode_str_mut(&mut self) -> &mut NtUnicodeStrMut<'static> {
self.deref_mut()
}
fn layout(&self) -> Layout {
Layout::array::<u16>(self.capacity_in_elements()).unwrap()
}
/// Creates an [`NtUnicodeString`] from an existing [`u16`] string buffer without a terminating NUL character.
///
/// The string is expected to consist of valid UTF-16 characters.
///
/// The given buffer becomes the internal buffer of the [`NtUnicodeString`] and therefore won't be NUL-terminated.
/// See the [module-level documentation](super) for the implications of that.
///
/// This function has *O*(1) complexity.
///
/// If you have a NUL-terminated buffer, either use [`try_from_u16_until_nul`] or convert from a [`U16CStr`]
/// using the corresponding [`TryFrom`] implementation.
///
/// [`try_from_u16_until_nul`]: Self::try_from_u16_until_nul
pub fn try_from_u16(buffer: &[u16]) -> Result<Self> {
let unicode_str = NtUnicodeStr::try_from_u16(buffer)?;
Ok(Self::from(&unicode_str))
}
/// Creates an [`NtUnicodeString`] from an existing [`u16`] string buffer that contains at least one NUL character.
///
/// The string is expected to consist of valid UTF-16 characters.
///
/// The string will be terminated at the NUL character.
/// An [`NtStringError::NulNotFound`] error is returned if no NUL character could be found.
/// As a consequence, this function has *O*(*n*) complexity.
///
/// The resulting internal `buffer` of [`NtUnicodeString`] will be NUL-terminated.
/// See the [module-level documentation](super) for the implications of that.
///
/// Use [`try_from_u16`] if you have a buffer that is not NUL-terminated.
/// You can also convert from a NUL-terminated [`U16CStr`] in *O*(1) via the corresponding [`TryFrom`] implementation.
///
/// [`try_from_u16`]: Self::try_from_u16
pub fn try_from_u16_until_nul(buffer: &[u16]) -> Result<Self> {
let unicode_str = NtUnicodeStr::try_from_u16_until_nul(buffer)?;
Ok(Self::from(&unicode_str))
}
/// Appends the given [`char`] to the end of this string.
///
/// Returns an [`NtStringError::BufferSizeExceedsU16`] error if the resulting string would exceed
/// 65532 bytes.
/// This is due to the fact that a `UNICODE_STRING` internally uses a 16-bit field to store the length.
/// Additionally, this function allocates one more character for NUL termination of the internal
/// buffer.
/// See the [module-level documentation](super) for the implications of that.
///
/// Note that every UTF-16 character consumes 2 or 4 bytes.
pub fn try_push(&mut self, c: char) -> Result<()> {
// Determine the required additional capacity.
//
// Add one element for the terminating NUL character
// (for applications that mistakenly treat the buffer of a Unicode String as a NUL-terminated string).
let encoded_length = c.len_utf16();
let additional_elements = encoded_length + 1;
// Reserve the additional capacity.
let additional = (additional_elements * mem::size_of::<u16>()) as u16;
self.try_reserve(additional)?;
// Encode the character as UTF-16 at the end of the buffer.
let end_index = self.len_in_elements();
self.raw.length += additional;
let dest_slice = &mut self.as_mut_slice()[end_index..];
c.encode_utf16(dest_slice);
// NUL-terminate it.
dest_slice[encoded_length] = 0;
// Set the final length (without including the terminating NUL character).
self.raw.length -= mem::size_of::<u16>() as u16;
Ok(())
}
/// Appends the given string slice to the end of this string.
///
/// Returns an [`NtStringError::BufferSizeExceedsU16`] error if the resulting string would exceed
/// 65532 bytes.
/// This is due to the fact that a `UNICODE_STRING` internally uses a 16-bit field to store the length.
/// Additionally, this function allocates one more character for NUL termination of the internal
/// buffer.
/// See the [module-level documentation](super) for the implications of that.
///
/// Note that every UTF-16 character consumes 2 or 4 bytes.
pub fn try_push_str(&mut self, s: &str) -> Result<()> {
// Determine the required additional capacity.
//
// Add one element for the terminating NUL character
// (for applications that mistakenly treat the buffer of a Unicode String as a NUL-terminated string).
let additional_elements = s
.encode_utf16()
.count()
.checked_add(1)
.ok_or(NtStringError::BufferSizeExceedsU16)?;
// Reserve the additional capacity.
let additional_bytes = additional_elements
.checked_mul(mem::size_of::<u16>())
.ok_or(NtStringError::BufferSizeExceedsU16)?;
let additional =
u16::try_from(additional_bytes).map_err(|_| NtStringError::BufferSizeExceedsU16)?;
self.try_reserve(additional)?;
// Copy over the string and NUL-terminate it.
let end_index = self.len_in_elements();
self.raw.length += additional;
for (string_item, utf16_item) in self.as_mut_slice()[end_index..]
.iter_mut()
.zip(s.encode_utf16().chain(once(0)))
{
*string_item = utf16_item;
}
// Set the final length (without the terminating NUL character).
self.raw.length -= mem::size_of::<u16>() as u16;
Ok(())
}
/// Appends the given [`u16`] string buffer (without a terminating NUL character) to the end of this string.
///
/// The string is expected to consist of valid UTF-16 characters.
///
/// Returns an [`NtStringError::BufferSizeExceedsU16`] error if the resulting string would exceed
/// 65532 bytes.
/// This is due to the fact that a `UNICODE_STRING` internally uses a 16-bit field to store the length.
/// Additionally, this function allocates one more character for NUL termination of the internal
/// buffer.
/// See the [module-level documentation](super) for the implications of that.
///
/// Note that every UTF-16 character consumes 2 or 4 bytes.
///
/// This function has *O*(1) complexity.
///
/// See [`try_push_u16_until_nul`] or [`try_push_u16cstr`] if you have a NUL-terminated buffer.
///
/// [`try_push_u16_until_nul`]: Self::try_push_u16_until_nul
/// [`try_push_u16cstr`]: Self::try_push_u16cstr
pub fn try_push_u16(&mut self, buffer: &[u16]) -> Result<()> {
// Determine the required additional capacity.
//
// Add one element for the terminating NUL character
// (for applications that mistakenly treat the buffer of a Unicode String as a NUL-terminated string).
let additional_elements = buffer
.len()
.checked_add(1)
.ok_or(NtStringError::BufferSizeExceedsU16)?;
// Reserve the additional capacity.
let additional_bytes = additional_elements
.checked_mul(mem::size_of::<u16>())
.ok_or(NtStringError::BufferSizeExceedsU16)?;
let additional =
u16::try_from(additional_bytes).map_err(|_| NtStringError::BufferSizeExceedsU16)?;
self.try_reserve(additional)?;
// Copy over the string.
let end_index = self.len_in_elements();
self.raw.length += additional;
let dest_slice = &mut self.as_mut_slice()[end_index..];
dest_slice[..buffer.len()].copy_from_slice(buffer);
// NUL-terminate it.
dest_slice[buffer.len()] = 0;
// Set the final length (without the terminating NUL character).
self.raw.length -= mem::size_of::<u16>() as u16;
Ok(())
}
/// Appends the given [`u16`] string buffer, which contains at least one NUL character,
/// to the end of this string.
///
/// The string is expected to consist of valid UTF-16 characters.
///
/// The string will be terminated at the NUL character.
/// An [`NtStringError::NulNotFound`] error is returned if no NUL character could be found.
/// As a consequence, this function has *O*(*n*) complexity.
///
/// Returns an [`NtStringError::BufferSizeExceedsU16`] error if the resulting string would exceed
/// 65532 bytes.
/// This is due to the fact that a `UNICODE_STRING` internally uses a 16-bit field to store the length.
/// Additionally, this function allocates one more character for NUL termination of the internal
/// buffer.
/// See the [module-level documentation](super) for the implications of that.
///
/// Note that every UTF-16 character consumes 2 or 4 bytes.
///
/// Use [`try_push_u16`] if you have a buffer that is not NUL-terminated.
/// You can also push a NUL-terminated [`U16CStr`] in *O*(1) via [`try_push_u16cstr`].
///
/// [`try_push_u16`]: Self::try_push_u16
/// [`try_push_u16cstr`]: Self::try_push_u16cstr
pub fn try_push_u16_until_nul(&mut self, buffer: &[u16]) -> Result<()> {
match buffer.iter().position(|x| *x == 0) {
Some(nul_pos) => self.try_push_u16(&buffer[..nul_pos]),
None => Err(NtStringError::NulNotFound),
}
}
/// Appends the given [`U16CStr`] to the end of this string.
///
/// Returns an [`NtStringError::BufferSizeExceedsU16`] error if the resulting string would exceed
/// 65532 bytes.
/// This is due to the fact that a `UNICODE_STRING` internally uses a 16-bit field to store the length.
/// Additionally, this function allocates one more character for NUL termination of the internal
/// buffer.
/// See the [module-level documentation](super) for the implications of that.
///
/// Note that every UTF-16 character consumes 2 or 4 bytes.
///
/// This function has *O*(1) complexity.
pub fn try_push_u16cstr(&mut self, u16cstr: &U16CStr) -> Result<()> {
self.try_push_u16(u16cstr.as_slice())
}
/// Appends the given [`U16Str`] to the end of this string.
///
/// Returns an [`NtStringError::BufferSizeExceedsU16`] error if the resulting string would exceed
/// 65532 bytes.
/// This is due to the fact that a `UNICODE_STRING` internally uses a 16-bit field to store the length.
/// Additionally, this function allocates one more character for NUL termination of the internal
/// buffer.
/// See the [module-level documentation](super) for the implications of that.
///
/// Note that every UTF-16 character consumes 2 or 4 bytes.
///
/// This function has *O*(1) complexity.
pub fn try_push_u16str(&mut self, u16str: &U16Str) -> Result<()> {
self.try_push_u16(u16str.as_slice())
}
/// Reserves capacity for `additional` bytes more than the current length.
///
/// Returns an [`NtStringError::BufferSizeExceedsU16`] error if the resulting capacity would exceed
/// 65535 bytes.
///
/// Note that every UTF-16 character consumes 2 or 4 bytes.
pub fn try_reserve(&mut self, additional: u16) -> Result<()> {
if self.remaining_capacity() >= additional {
return Ok(());
}
let new_capacity = self
.len()
.checked_add(additional)
.ok_or(NtStringError::BufferSizeExceedsU16)?;
if self.raw.buffer.is_null() {
self.raw.maximum_length = new_capacity;
let new_layout = self.layout();
self.raw.buffer = unsafe { alloc::alloc(new_layout) } as *mut u16;
} else {
let old_layout = self.layout();
self.raw.buffer = unsafe {
alloc::realloc(
self.raw.buffer as *mut u8,
old_layout,
usize::from(new_capacity),
)
} as *mut u16;
self.raw.maximum_length = new_capacity;
}
Ok(())
}
/// Creates an empty [`NtUnicodeString`] with at least the specified capacity.
///
/// This will preallocate a buffer with the given capacity.
/// If the given capacity is `0`, no allocation will occur, and this method is identical to the [`new`] method.
///
/// [`new`]: Self::new
pub fn with_capacity(capacity: u16) -> Self {
let mut string = Self::new();
string.try_reserve(capacity).unwrap();
string
}
}
impl Add<&str> for NtUnicodeString {
type Output = NtUnicodeString;
fn add(mut self, rhs: &str) -> Self::Output {
if let Err(e) = self.try_push_str(rhs) {
panic!("{e}");
}
self
}
}
impl Add<&U16CStr> for NtUnicodeString {
type Output = NtUnicodeString;
fn add(mut self, rhs: &U16CStr) -> Self::Output {
if let Err(e) = self.try_push_u16cstr(rhs) {
panic!("{e}");
}
self
}
}
impl Add<&U16Str> for NtUnicodeString {
type Output = NtUnicodeString;
fn add(mut self, rhs: &U16Str) -> Self::Output {
if let Err(e) = self.try_push_u16str(rhs) {
panic!("{e}");
}
self
}
}
impl AddAssign<&str> for NtUnicodeString {
fn add_assign(&mut self, rhs: &str) {
if let Err(e) = self.try_push_str(rhs) {
panic!("{e}");
}
}
}
impl AddAssign<&U16CStr> for NtUnicodeString {
fn add_assign(&mut self, rhs: &U16CStr) {
if let Err(e) = self.try_push_u16cstr(rhs) {
panic!("{e}");
}
}
}
impl AddAssign<&U16Str> for NtUnicodeString {
fn add_assign(&mut self, rhs: &U16Str) {
if let Err(e) = self.try_push_u16str(rhs) {
panic!("{e}");
}
}
}
impl Clone for NtUnicodeString {
/// Creates a copy of this [`NtUnicodeString`].
///
/// This implementation keeps the original capacity.
fn clone(&self) -> Self {
NtUnicodeString::from(self.as_unicode_str())
}
}
impl Default for NtUnicodeString {
fn default() -> Self {
Self::new()
}
}
impl Deref for NtUnicodeString {
// The NtUnicodeString can't be dropped while someone still holds a reference to it,
// so the `buffer` lifetime is effectively 'static for the duration of the reference.
type Target = NtUnicodeStrMut<'static>;
fn deref(&self) -> &Self::Target {
// SAFETY: `NtUnicodeStrMut` and `NtUnicodeString` have the same memory layout,
// so we can safely transmute `NtUnicodeString` to `NtUnicodeStrMut`.
unsafe { mem::transmute(self) }
}
}
impl DerefMut for NtUnicodeString {
fn deref_mut(&mut self) -> &mut Self::Target {
// SAFETY: `NtUnicodeStrMut` and `NtUnicodeString` have the same memory layout,
// so we can safely transmute `NtUnicodeString` to `NtUnicodeStrMut`.
unsafe { mem::transmute(self) }
}
}
impl fmt::Display for NtUnicodeString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.deref(), f)
}
}
impl Drop for NtUnicodeString {
fn drop(&mut self) {
if !self.raw.buffer.is_null() {
let layout = self.layout();
unsafe { alloc::dealloc(self.raw.buffer as *mut u8, layout) }
}
}
}
impl Eq for NtUnicodeString {}
impl From<char> for NtUnicodeString {
/// Creates an [`NtUnicodeString`] from a single [`char`].
fn from(c: char) -> Self {
let mut string = Self::new();
string.try_push(c).unwrap();
string
}
}
impl<'a> From<&NtUnicodeStr<'a>> for NtUnicodeString {
/// Creates an [`NtUnicodeString`] from an existing [`NtUnicodeStr`].
///
/// This implementation keeps the original capacity.
fn from(unicode_str: &NtUnicodeStr) -> Self {
let mut new_string = Self::with_capacity(unicode_str.capacity());
if !unicode_str.is_empty() {
new_string.raw.length = unicode_str.len();
new_string
.as_mut_slice()
.copy_from_slice(unicode_str.as_slice());
}
new_string
}
}
impl Ord for NtUnicodeString {
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(self.deref(), other.deref())
}
}
impl_eq! { NtUnicodeString, NtUnicodeString }
impl_eq! { NtUnicodeStr<'a>, NtUnicodeString }
impl_eq! { NtUnicodeString, NtUnicodeStr<'a> }
impl_eq! { NtUnicodeStrMut<'a>, NtUnicodeString }
impl_eq! { NtUnicodeString, NtUnicodeStrMut<'a> }
impl_eq! { NtUnicodeString, str }
impl_eq! { str, NtUnicodeString }
impl_eq! { NtUnicodeString, &str }
impl_eq! { &str, NtUnicodeString }
impl_partial_cmp! { NtUnicodeString, NtUnicodeString }
impl_partial_cmp! { NtUnicodeStr<'a>, NtUnicodeString }
impl_partial_cmp! { NtUnicodeString, NtUnicodeStr<'a> }
impl_partial_cmp! { NtUnicodeStrMut<'a>, NtUnicodeString }
impl_partial_cmp! { NtUnicodeString, NtUnicodeStrMut<'a> }
impl_partial_cmp! { NtUnicodeString, str }
impl_partial_cmp! { str, NtUnicodeString }
impl_partial_cmp! { NtUnicodeString, &str }
impl_partial_cmp! { &str, NtUnicodeString }
impl TryExtend<char> for NtUnicodeString {
type Error = NtStringError;
fn try_extend<I: IntoIterator<Item = char>>(&mut self, iter: I) -> Result<()> {
let iterator = iter.into_iter();
let (lower_bound, _) = iterator.size_hint();
// Add one element for the terminating NUL character
// (for applications that mistakenly treat the buffer of a Unicode String as a NUL-terminated string).
let additional_elements = lower_bound + 1;
// Reserve the additional capacity once to save on allocations.
let additional_bytes = u16::try_from(additional_elements * mem::size_of::<u16>())
.map_err(|_| NtStringError::BufferSizeExceedsU16)?;
self.try_reserve(additional_bytes)?;
for ch in iterator {
self.try_push(ch)?;
}
Ok(())
}
fn try_extend_one(&mut self, item: char) -> Result<()> {
self.try_push(item)
}
}
impl<'a> TryExtend<&'a str> for NtUnicodeString {
type Error = NtStringError;
fn try_extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) -> Result<()> {
for s in iter.into_iter() {
self.try_push_str(s)?;
}
Ok(())
}
}
impl<'a> TryExtend<&'a U16CStr> for NtUnicodeString {
type Error = NtStringError;
fn try_extend<I: IntoIterator<Item = &'a U16CStr>>(&mut self, iter: I) -> Result<()> {
for s in iter.into_iter() {
self.try_push_u16cstr(s)?;
}
Ok(())
}
}
impl<'a> TryExtend<&'a U16Str> for NtUnicodeString {
type Error = NtStringError;
fn try_extend<I: IntoIterator<Item = &'a U16Str>>(&mut self, iter: I) -> Result<()> {
for s in iter.into_iter() {
self.try_push_u16str(s)?;
}
Ok(())
}
}
impl TryFrom<&str> for NtUnicodeString {
type Error = NtStringError;
/// Converts a string slice into an owned [`NtUnicodeString`].
///
/// This allocates a buffer of matching size on the heap and NUL-terminates it internally.
/// See the [module-level documentation](super) for the implications of that.
fn try_from(s: &str) -> Result<Self> {
let mut string = Self::new();
string.try_push_str(s)?;
Ok(string)
}
}
impl TryFrom<String> for NtUnicodeString {
type Error = NtStringError;
/// Converts a [`String`] into an owned [`NtUnicodeString`].
///
/// This allocates a buffer of matching size on the heap and NUL-terminates it internally.
/// See the [module-level documentation](super) for the implications of that.
fn try_from(s: String) -> Result<Self> {
NtUnicodeString::try_from(s.as_str())
}
}
impl TryFrom<&String> for NtUnicodeString {
type Error = NtStringError;
/// Converts a [`String`] reference into an owned [`NtUnicodeString`].
///
/// This allocates a buffer of matching size on the heap and NUL-terminates it internally.
/// See the [module-level documentation](super) for the implications of that.
fn try_from(s: &String) -> Result<Self> {
NtUnicodeString::try_from(s.as_str())
}
}
impl TryFrom<&U16CStr> for NtUnicodeString {
type Error = NtStringError;
/// Converts a [`U16CStr`] reference into an owned [`NtUnicodeString`].
///
/// The internal buffer will be NUL-terminated.
/// See the [module-level documentation](super) for the implications of that.
fn try_from(value: &U16CStr) -> Result<Self> {
let unicode_str = NtUnicodeStr::try_from(value)?;
Ok(Self::from(&unicode_str))
}
}
impl TryFrom<&U16Str> for NtUnicodeString {
type Error = NtStringError;
/// Converts a [`U16Str`] reference into an owned [`NtUnicodeString`].
///
/// The internal buffer will NOT be NUL-terminated.
/// See the [module-level documentation](super) for the implications of that.
fn try_from(value: &U16Str) -> Result<Self> {
let unicode_str = NtUnicodeStr::try_from(value)?;
Ok(Self::from(&unicode_str))
}
}
#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use crate::error::NtStringError;
use crate::traits::TryExtend;
use crate::unicode_string::NtUnicodeString;
#[test]
fn test_add() {
let mut string = NtUnicodeString::new();
string += "👍";
assert_eq!(string, "👍");
let string2 = string + "👎";
assert_eq!(string2, "👍👎");
}
#[test]
fn test_chars() {
// Verify that ASCII characters work and allocate 2 bytes per character.
// Verify that one more element (2 bytes) is allocated for the terminating NUL character.
let moin = NtUnicodeString::try_from("Moin").unwrap();
assert_eq!(moin.capacity(), 10);
assert_eq!(moin.len(), 8);
let vec = moin.chars_lossy().collect::<Vec<char>>();
assert_eq!(vec, ['M', 'o', 'i', 'n']);
// Verify that Unicode characters inside the Basic Multilingual Plane work and allocate 2 bytes per character.
// Verify that one more element (2 bytes) is allocated for the terminating NUL character.
let 今日は = NtUnicodeString::try_from("今日は").unwrap();
assert_eq!(今日は.capacity(), 8);
assert_eq!(今日は.len(), 6);
let vec = 今日は.chars_lossy().collect::<Vec<char>>();
assert_eq!(vec, ['今', '日', 'は']);
// Verify that Unicode characters outside the Basic Multilingual Plane (e.g. emojis) work and allocate 4 bytes per character.
// Verify that one more element (2 bytes) is allocated for the terminating NUL character.
let smile = NtUnicodeString::try_from("😀").unwrap();
assert_eq!(smile.capacity(), 6);
assert_eq!(smile.len(), 4);
let vec = smile.chars_lossy().collect::<Vec<char>>();
assert_eq!(vec, ['😀']);
}
#[test]
fn test_cmp() {
let a = NtUnicodeString::try_from("a").unwrap();
let b = NtUnicodeString::try_from("b").unwrap();
assert!(a < b);
}
#[test]
fn test_eq() {
let hello = NtUnicodeString::try_from("Hello").unwrap();
let hello_again = NtUnicodeString::try_from("Hello again").unwrap();
assert_ne!(hello, hello_again);
let mut hello_clone = hello.clone();
assert_eq!(hello, hello_clone);
hello_clone.try_reserve(42).unwrap();
assert_eq!(hello, hello_clone);
}
#[test]
fn test_extend_and_pop() {
// Verify that 32766 characters still work.
// Verify that one more element (2 bytes) is allocated for the terminating NUL character.
let a_string = "a".repeat(32766);
let mut string = NtUnicodeString::try_from(a_string).unwrap();
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65532);
// Verify that extending by a single character fails.
assert_eq!(
string.try_extend(Some('b')),
Err(NtStringError::BufferSizeExceedsU16)
);
// Pop a character to append a new one.
assert_eq!(string.pop(), Some(Ok('a')));
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65530);
string.try_extend_one('c').unwrap();
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65532);
// Pop two characters to append an emoji.
assert_eq!(string.pop(), Some(Ok('c')));
assert_eq!(string.pop(), Some(Ok('a')));
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65528);
string.try_extend_one('😀').unwrap();
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65532);
// Pop the emoji and another character to append 3 ASCII characters.
assert_eq!(string.pop(), Some(Ok('😀')));
assert_eq!(string.pop(), Some(Ok('a')));
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65526);
string.try_extend("def".chars()).unwrap();
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65532);
}
#[test]
fn test_from_u16() {
// Verify that `try_from_u16` fails if we exceed a `u16`.
let mut a_vec = "a".repeat(32768).encode_utf16().collect::<Vec<u16>>();
assert_eq!(
NtUnicodeString::try_from_u16(&a_vec),
Err(NtStringError::BufferSizeExceedsU16)
);
// Verify that `try_from_u16` does not reserve any space for a terminating NUL character.
a_vec.pop();
let string = NtUnicodeString::try_from_u16(&a_vec).unwrap();
assert_eq!(string.capacity(), 65534);
assert_eq!(string.len(), 65534);
// Verify that `try_from_u16_until_nul` does reserve space for a terminating NUL character.
a_vec[4] = 0;
let string = NtUnicodeString::try_from_u16_until_nul(&a_vec).unwrap();
assert_eq!(string.capacity(), 10);
assert_eq!(string.len(), 8);
assert_eq!(string, "aaaa");
}
#[test]
fn test_push_str() {
let mut string = NtUnicodeString::new();
string.try_push_str("Hey").unwrap();
assert_eq!(string, "Hey");
assert_eq!(string.capacity(), 8);
assert_eq!(string.len(), 6);
string.try_push_str("Ho").unwrap();
assert_eq!(string, "HeyHo");
assert_eq!(string.capacity(), 12);
assert_eq!(string.len(), 10);
}
#[test]
fn test_reserve() {
let mut string = NtUnicodeString::new();
assert_eq!(string.capacity(), 0);
string.try_reserve(5).unwrap();
assert_eq!(string.capacity(), 5);
string.try_reserve(3).unwrap();
assert_eq!(string.capacity(), 5);
string.try_push_str("a").unwrap();
assert_eq!(string, "a");
assert_eq!(string.capacity(), 5);
string.try_push_str("b").unwrap();
assert_eq!(string, "ab");
assert_eq!(string.capacity(), 6);
}
}