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
extern crate alloc;
type String = alloc::string::String;

use core::mem::size_of;

/// Creates new unicode string.
#[macro_export]
macro_rules! unicode_string {
    ($str:expr) => {
        $crate::types::UnicodeString {
            len: ($str.len() * 2) as _,
            maximum_len: ($str.len() * 2) as _,
            buffer: $crate::wide!($str).as_ptr() as _,
        }
    };
}

/// UTF16 String.
#[repr(C)]
pub struct UnicodeString {
    /// Length of the string, in bytes.
    pub len: u16,
    /// Allocated size, in bytes.
    pub maximum_len: u16,
    /// Pointer to actual UTF-16 data.
    pub buffer: *mut u16,
}

impl UnicodeString {
    /// Converts wide string into [`alloc::string::String`] by cloning data.
    /// # Safety
    /// This function doesn't check if `self.buffer` == 0.
    #[inline]
    pub unsafe fn decode_utf16_unchecked(&self) -> String {
        let utf16 = core::slice::from_raw_parts(self.buffer, self.len as usize / size_of::<u16>());
        alloc::string::String::from_utf16_lossy(utf16)
    }

    /// Converts wide string into [`alloc::string::String`] by cloning data.
    /// # Safety
    /// This function only checks if `self.buffer` != 0 and if `self.len` != 0.
    #[inline]
    pub unsafe fn decode_utf16(&self) -> Option<String> {
        if self.is_null() {
            None
        } else {
            Some(self.decode_utf16_unchecked())
        }
    }

    /// Checks if either `self.len`, `self.maximum_len` or `self.buffer` == 0.
    /// If this function returns `true` it doesn't guarantee that `self.buffer` points to valid memory.
    #[inline]
    pub fn is_null(&self) -> bool {
        self.len == 0 || self.maximum_len == 0 || self.buffer.is_null()
    }
}

impl PartialEq for UnicodeString {
    fn eq(&self, other: &Self) -> bool {
        if self.is_null() || other.is_null() || self.len != other.len {
            false
        } else {
            unsafe { libc::memcmp(self.buffer as _, other.buffer as _, self.len as _) == 0 }
        }
    }
}