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
//! Utility types
#![deny(unsafe_code)]
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use windows::core::PCWSTR;
/// A null-terminated "wide" (i.e. potentially ill-formed UTF16-encoded) string for use with the Windows API
#[derive(Debug)]
pub(crate) struct CWideString(Vec<u16>);
impl CWideString {
/// Creates a new [`CWideString`] from the given OS string
pub(crate) fn new<S>(s: &S) -> Self
where
S: AsRef<OsStr> + ?Sized,
{
Self(OsStr::new(s).encode_wide().chain(Some(0)).collect())
}
/// Returns a raw pointer to the underlying `u16` slice as a
/// [`PCWSTR`](https://docs.rs/windows/0/windows/core/struct.PCWSTR.html) that can be passed to Windows API
/// functions
///
/// The returned pointer is guaranteed to point to a valid null-terminated wide string as long as the instance of
/// [`CWideString`] is live.
pub(crate) fn as_pcwstr(&self) -> PCWSTR {
PCWSTR::from_raw(self.0.as_ptr())
}
}
#[cfg(test)]
mod tests {
#![allow(unsafe_code)]
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::slice;
use super::*;
#[test]
fn as_pcwstr_returns_valid_pointer() {
let c_wide_string = CWideString::new("test");
let PCWSTR(ptr) = c_wide_string.as_pcwstr();
let len = (0..(isize::MAX as usize) / 2)
.find(|&idx| {
// SAFETY: By the guarantees of `as_pcwstr` and because we haven't found a `NULL` element yet,
// - both `ptr` and the offset pointer are in bounds of the same allocated object
// - the computed offset `idx * 2` does not overflow an `isize`
// - the computed sum does not overflow a `usize`
let element_ptr = unsafe { ptr.add(idx) };
// SAFETY:
// By the guarantees of `as_pcwstr` and because we haven't found a `NULL` element yet, `element_ptr`
// points to a valid `u16`
let element = unsafe { *element_ptr };
element == 0
})
.unwrap();
// SAFETY:
// All safety conditions of `slice::from_raw_parts` follow from the guarantees of `as_pcwstr` and the fact that
// `len` is the offset relative to `ptr` of the first null element (in particular, `len >= 0`, so the cast to
// `usize` doesn't change its value)
let slice = unsafe { slice::from_raw_parts(ptr, len) };
assert_eq!(OsString::from_wide(slice), "test");
}
}