Debian Codegraph @

src/platform/windows_winusb/util.rs

raw symbols SCIP
1 use std::{
2 borrow::Borrow,
3 ffi::{OsStr, OsString},
4 fmt::{Display, Write},
5 io,
6 ops::Deref,
7 os::windows::prelude::{
8 AsHandle, AsRawHandle, HandleOrInvalid, OsStrExt, OsStringExt, OwnedHandle, RawHandle,
9 },
10 ptr::null,
11 slice,
12 };
13
14 use windows_sys::Win32::{
15 Foundation::{GENERIC_READ, GENERIC_WRITE, HANDLE},
16 Storage::FileSystem::{
17 CreateFileW, FILE_FLAG_OVERLAPPED, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
18 },
19 };
20
21 /// Wrapper around `CreateFile`
22 pub fn create_file(path: &WCStr) -> Result<OwnedHandle, io::Error> {
23 unsafe {
24 let r = CreateFileW(
25 path.as_ptr(),
26 GENERIC_READ | GENERIC_WRITE,
27 FILE_SHARE_READ | FILE_SHARE_WRITE,
28 null(),
29 OPEN_EXISTING,
30 FILE_FLAG_OVERLAPPED,
31 0,
32 );
33 HandleOrInvalid::from_raw_handle(r as RawHandle)
34 .try_into()
35 .map_err(|_| io::Error::last_os_error())
36 }
37 }
38
39 pub fn raw_handle(h: impl AsHandle) -> HANDLE {
40 h.as_handle().as_raw_handle() as HANDLE
41 }
42
43 /// A utf-16 owned null-terminated string
44 #[repr(transparent)]
45 pub struct WCString(Vec<u16>);
46
47 impl From<&OsStr> for WCString {
48 fn from(s: &OsStr) -> Self {
49 WCString(s.encode_wide().chain(Some(0)).collect())
50 }
51 }
52
53 impl Borrow<WCStr> for WCString {
54 fn borrow(&self) -> &WCStr {
55 &self
56 }
57 }
58
59 impl Deref for WCString {
60 type Target = WCStr;
61
62 fn deref(&self) -> &Self::Target {
63 unsafe { WCStr::from_slice_unchecked(&self.0) }
64 }
65 }
66
67 impl From<WCString> for OsString {
68 fn from(s: WCString) -> Self {
69 (&*s).into()
70 }
71 }
72
73 impl Display for WCString {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 self.deref().fmt(f)
76 }
77 }
78
79 /// A utf-16 borrowed null-terminated string
80 #[repr(transparent)]
81 pub struct WCStr([u16]);
82
83 impl WCStr {
84 pub unsafe fn from_ptr<'a>(ptr: *const u16) -> &'a WCStr {
85 let mut len = 0;
86 while ptr.add(len).read() != 0 {
87 len += 1;
88 }
89 Self::from_slice_unchecked(slice::from_raw_parts(ptr, len + 1))
90 }
91
92 unsafe fn from_slice_unchecked(s: &[u16]) -> &WCStr {
93 debug_assert_eq!(
94 s.last().copied(),
95 Some(0),
96 "string should be null-terminated"
97 );
98 let p: *const [u16] = s;
99 unsafe { &*(p as *const WCStr) }
100 }
101
102 pub fn from_slice_until_nul(s: &[u16]) -> &WCStr {
103 let nul = s
104 .iter()
105 .copied()
106 .position(|x| x == 0)
107 .expect("string should be null-terminated");
108 unsafe { Self::from_slice_unchecked(&s[..nul + 1]) }
109 }
110
111 pub fn as_slice(&self) -> &[u16] {
112 &self.0
113 }
114
115 pub fn as_slice_without_nul(&self) -> &[u16] {
116 &self.0[..self.0.len() - 1]
117 }
118
119 pub fn as_ptr(&self) -> *const u16 {
120 self.0.as_ptr()
121 }
122 }
123
124 impl ToOwned for WCStr {
125 type Owned = WCString;
126
127 fn to_owned(&self) -> Self::Owned {
128 WCString(self.0.to_owned())
129 }
130 }
131
132 impl From<&WCStr> for OsString {
133 fn from(s: &WCStr) -> Self {
134 debug_assert_eq!(
135 s.0.last().copied(),
136 Some(0),
137 "string should be null-terminated"
138 );
139 OsString::from_wide(&s.0[..s.0.len() - 1])
140 }
141 }
142
143 impl Display for WCStr {
144 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145 let s = self.as_slice_without_nul();
146 for c in char::decode_utf16(s.iter().copied()) {
147 f.write_char(c.unwrap_or(char::REPLACEMENT_CHARACTER))?;
148 }
149 Ok(())
150 }
151 }
152
153 pub struct NulSepList(pub Vec<u16>);
154
155 impl NulSepList {
156 pub fn iter(&self) -> NulSepListIter {
157 NulSepListIter(&self.0)
158 }
159 }
160
161 pub struct NulSepListIter<'a>(pub &'a [u16]);
162
163 impl<'a> Iterator for NulSepListIter<'a> {
164 type Item = &'a WCStr;
165
166 fn next(&mut self) -> Option<Self::Item> {
167 if let Some(next_nul) = self.0.iter().copied().position(|x| x == 0) {
168 let (i, next) = self.0.split_at(next_nul + 1);
169 self.0 = next;
170 Some(unsafe { WCStr::from_slice_unchecked(i) })
171 } else {
172 None
173 }
174 }
175 }