diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
| commit | 8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch) | |
| tree | 22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/windows-strings | |
| parent | 4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff) | |
chore: add vendor directory
Diffstat (limited to 'vendor/windows-strings')
20 files changed, 1753 insertions, 0 deletions
diff --git a/vendor/windows-strings/.cargo-checksum.json b/vendor/windows-strings/.cargo-checksum.json new file mode 100644 index 00000000..eb43a9db --- /dev/null +++ b/vendor/windows-strings/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"337351cb49204feac9d7d7297ab344322f14ef94fc1ad005d8ceca972b2dccbf","Cargo.toml":"22709084fde58a11249eeae779b2708377a49958b65180020d712a8c5aafb5a9","license-apache-2.0":"c16f8dcf1a368b83be78d826ea23de4079fe1b4469a0ab9ee20563f37ff3d44b","license-mit":"c2cfccb812fe482101a8f04597dfc5a9991a6b2748266c47ac91b6a5aae15383","readme.md":"99623bfb30a59574e53029cd8c5061c3654d84a4f5f23e59f5340df9e8ebb440","src/bindings.rs":"2cf1f4aa078dfa35ab15e4f6985ccafe2815eb9e2e7d4de68ef91043bc2a2c7d","src/bstr.rs":"a87165fd8c4def7bdb7a159e81102317ea748d4d6b2a99ead0f598561cfdc51c","src/decode.rs":"cecdb73a3b54fb8f3d24a245dddbe790a5e7889771e5c3301cbc5ac375abc7bd","src/hstring.rs":"002facafc3af02f5e53a660e9b67be4f224435ce9d97f5f32e77a840c4e80e34","src/hstring_builder.rs":"6a531711e5b17d87430a54dbef4265744dff3d9aef1185ecdd3dbe04ddd00712","src/hstring_header.rs":"4efd8626e11a7b220898a9616396e4746d66083dd226846ac6bfe30be75c75e9","src/lib.rs":"5bd7b18aeb3fc74e30f1544b12dcd335edfeec801bc8b6ea362ec434bda8c0fb","src/literals.rs":"e8dd34a8d776933c41709a0f901c65321a4aaab15bd06c4fcb049133f0ab3fe2","src/pcstr.rs":"463e7297aa16d7bb8d6df44c8e1588dbd9996a563144d9b4dafef5ebf294773d","src/pcwstr.rs":"853ed6f20afeb45d8a3c56506945a06a26d94e0194e4194c627c0e06a84c5b4f","src/pstr.rs":"1516a072f24692a5bb27d025bd97cd338bc5a104305f7b8418acd6f943a5941b","src/pwstr.rs":"c1e9cd11d41e1875ffa15fa4e29805e6f99aca99a5126aacbe7f169e8b68d790","src/ref_count.rs":"e1617f090d56948719730c81e34449c3d36f288b86a74182719a291c340f65d6","windows-strings.natvis":"e1461f43c6789279ca04cc34e0d614f26162265c862e9c111d251efb843a3798"},"package":"56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57"}
\ No newline at end of file diff --git a/vendor/windows-strings/Cargo.lock b/vendor/windows-strings/Cargo.lock new file mode 100644 index 00000000..39ac3c9f --- /dev/null +++ b/vendor/windows-strings/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "windows-link" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" + +[[package]] +name = "windows-strings" +version = "0.4.2" +dependencies = [ + "windows-link", +] diff --git a/vendor/windows-strings/Cargo.toml b/vendor/windows-strings/Cargo.toml new file mode 100644 index 00000000..66297620 --- /dev/null +++ b/vendor/windows-strings/Cargo.toml @@ -0,0 +1,57 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2021" +rust-version = "1.74" +name = "windows-strings" +version = "0.4.2" +authors = ["Microsoft"] +build = false +autolib = false +autobins = false +autoexamples = false +autotests = false +autobenches = false +description = "Windows string types" +readme = "readme.md" +categories = ["os::windows-apis"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/microsoft/windows-rs" + +[package.metadata.docs.rs] +default-target = "x86_64-pc-windows-msvc" +targets = [] + +[features] +default = ["std"] +std = [] + +[lib] +name = "windows_strings" +path = "src/lib.rs" + +[dependencies.windows-link] +version = "0.1.1" +default-features = false + +[lints.rust] +missing_docs = "warn" +unsafe_op_in_unsafe_fn = "warn" + +[lints.rust.rust_2018_idioms] +level = "warn" +priority = -1 + +[lints.rust.unexpected_cfgs] +level = "warn" +priority = 0 +check-cfg = ["cfg(windows_raw_dylib, windows_slim_errors)"] diff --git a/vendor/windows-strings/license-apache-2.0 b/vendor/windows-strings/license-apache-2.0 new file mode 100644 index 00000000..b5ed4ece --- /dev/null +++ b/vendor/windows-strings/license-apache-2.0 @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (c) Microsoft Corporation. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/windows-strings/license-mit b/vendor/windows-strings/license-mit new file mode 100644 index 00000000..9e841e7a --- /dev/null +++ b/vendor/windows-strings/license-mit @@ -0,0 +1,21 @@ + MIT License + + Copyright (c) Microsoft Corporation. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE diff --git a/vendor/windows-strings/readme.md b/vendor/windows-strings/readme.md new file mode 100644 index 00000000..1e26e77b --- /dev/null +++ b/vendor/windows-strings/readme.md @@ -0,0 +1,34 @@ +## Windows string types + +The [windows-strings](https://crates.io/crates/windows-strings) crate provides common Windows string types used by various Windows APIs. + +* [Getting started](https://kennykerr.ca/rust-getting-started/) +* [Samples](https://github.com/microsoft/windows-rs/tree/master/crates/samples) +* [Releases](https://github.com/microsoft/windows-rs/releases) + +Start by adding the following to your Cargo.toml file: + +```toml +[dependencies.windows-strings] +version = "0.4" +``` + +Use the Windows string types as needed: + +```rust +use windows_strings::*; + +const A: PCSTR = s!("ansi"); +const W: PCWSTR = w!("wide"); + +fn main() { + let b = BSTR::from("bstr"); + let h = HSTRING::from("hstring"); + + assert_eq!(b, "bstr"); + assert_eq!(h, "hstring"); + + assert_eq!(unsafe { A.to_string().unwrap() }, "ansi"); + assert_eq!(unsafe { W.to_string().unwrap() }, "wide"); +} +``` diff --git a/vendor/windows-strings/src/bindings.rs b/vendor/windows-strings/src/bindings.rs new file mode 100644 index 00000000..2e0ad17d --- /dev/null +++ b/vendor/windows-strings/src/bindings.rs @@ -0,0 +1,19 @@ +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] + +windows_link::link!("kernel32.dll" "system" fn GetProcessHeap() -> HANDLE); +windows_link::link!("kernel32.dll" "system" fn HeapAlloc(hheap : HANDLE, dwflags : HEAP_FLAGS, dwbytes : usize) -> *mut core::ffi::c_void); +windows_link::link!("kernel32.dll" "system" fn HeapFree(hheap : HANDLE, dwflags : HEAP_FLAGS, lpmem : *const core::ffi::c_void) -> BOOL); +windows_link::link!("oleaut32.dll" "system" fn SysAllocStringLen(strin : PCWSTR, ui : u32) -> BSTR); +windows_link::link!("oleaut32.dll" "system" fn SysFreeString(bstrstring : BSTR)); +windows_link::link!("oleaut32.dll" "system" fn SysStringLen(pbstr : BSTR) -> u32); +pub type BOOL = i32; +pub type BSTR = *const u16; +pub type HANDLE = *mut core::ffi::c_void; +pub type HEAP_FLAGS = u32; +pub type PCWSTR = *const u16; diff --git a/vendor/windows-strings/src/bstr.rs b/vendor/windows-strings/src/bstr.rs new file mode 100644 index 00000000..486b2602 --- /dev/null +++ b/vendor/windows-strings/src/bstr.rs @@ -0,0 +1,166 @@ +use super::*; +use core::ops::Deref; + +/// A BSTR string ([BSTR](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/automat/string-manipulation-functions)) +/// is a length-prefixed wide string. +#[repr(transparent)] +pub struct BSTR(*const u16); + +impl BSTR { + /// Create an empty `BSTR`. + /// + /// This function does not allocate memory. + pub const fn new() -> Self { + Self(core::ptr::null_mut()) + } + + /// Create a `BSTR` from a slice of 16 bit characters (wchars). + pub fn from_wide(value: &[u16]) -> Self { + if value.is_empty() { + return Self::new(); + } + + let result = unsafe { + Self(bindings::SysAllocStringLen( + value.as_ptr(), + value.len().try_into().unwrap(), + )) + }; + + if result.is_empty() { + panic!("allocation failed"); + } + + result + } + + /// # Safety + #[doc(hidden)] + pub unsafe fn from_raw(raw: *const u16) -> Self { + Self(raw) + } + + /// # Safety + #[doc(hidden)] + pub fn into_raw(self) -> *const u16 { + unsafe { core::mem::transmute(self) } + } +} + +impl Deref for BSTR { + type Target = [u16]; + + fn deref(&self) -> &[u16] { + let len = if self.0.is_null() { + 0 + } else { + unsafe { bindings::SysStringLen(self.0) as usize } + }; + + if len > 0 { + unsafe { core::slice::from_raw_parts(self.0, len) } + } else { + // This ensures that if `as_ptr` is called on the slice that the resulting pointer + // will still refer to a null-terminated string. + const EMPTY: [u16; 1] = [0]; + &EMPTY[..0] + } + } +} + +impl Clone for BSTR { + fn clone(&self) -> Self { + Self::from_wide(self) + } +} + +impl From<&str> for BSTR { + fn from(value: &str) -> Self { + let value: alloc::vec::Vec<u16> = value.encode_utf16().collect(); + Self::from_wide(&value) + } +} + +impl From<String> for BSTR { + fn from(value: String) -> Self { + value.as_str().into() + } +} + +impl From<&String> for BSTR { + fn from(value: &String) -> Self { + value.as_str().into() + } +} + +impl TryFrom<&BSTR> for String { + type Error = alloc::string::FromUtf16Error; + + fn try_from(value: &BSTR) -> core::result::Result<Self, Self::Error> { + String::from_utf16(value) + } +} + +impl TryFrom<BSTR> for String { + type Error = alloc::string::FromUtf16Error; + + fn try_from(value: BSTR) -> core::result::Result<Self, Self::Error> { + String::try_from(&value) + } +} + +impl Default for BSTR { + fn default() -> Self { + Self(core::ptr::null_mut()) + } +} + +impl core::fmt::Display for BSTR { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + core::write!( + f, + "{}", + Decode(|| core::char::decode_utf16(self.iter().cloned())) + ) + } +} + +impl core::fmt::Debug for BSTR { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + core::write!(f, "{self}") + } +} + +impl PartialEq for BSTR { + fn eq(&self, other: &Self) -> bool { + self.deref() == other.deref() + } +} + +impl Eq for BSTR {} + +impl PartialEq<BSTR> for &str { + fn eq(&self, other: &BSTR) -> bool { + other == self + } +} + +impl PartialEq<BSTR> for String { + fn eq(&self, other: &BSTR) -> bool { + other == self + } +} + +impl<T: AsRef<str> + ?Sized> PartialEq<T> for BSTR { + fn eq(&self, other: &T) -> bool { + self.iter().copied().eq(other.as_ref().encode_utf16()) + } +} + +impl Drop for BSTR { + fn drop(&mut self) { + if !self.0.is_null() { + unsafe { bindings::SysFreeString(self.0) } + } + } +} diff --git a/vendor/windows-strings/src/decode.rs b/vendor/windows-strings/src/decode.rs new file mode 100644 index 00000000..dbc9e2ea --- /dev/null +++ b/vendor/windows-strings/src/decode.rs @@ -0,0 +1,58 @@ +/// An internal helper for decoding an iterator of chars and displaying them +pub struct Decode<F>(pub F); + +impl<F, R, E> core::fmt::Display for Decode<F> +where + F: Clone + FnOnce() -> R, + R: IntoIterator<Item = core::result::Result<char, E>>, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + use core::fmt::Write; + let iter = self.0.clone(); + for c in iter().into_iter() { + f.write_char(c.unwrap_or(core::char::REPLACEMENT_CHARACTER))? + } + Ok(()) + } +} + +/// Mirror of `std::char::decode_utf16` for utf-8. +pub fn decode_utf8( + mut buffer: &[u8], +) -> impl Iterator<Item = core::result::Result<char, core::str::Utf8Error>> + '_ { + let mut current = "".chars(); + let mut previous_error = None; + core::iter::from_fn(move || { + loop { + match (current.next(), previous_error) { + (Some(c), _) => return Some(Ok(c)), + // Return the previous error + (None, Some(e)) => { + previous_error = None; + return Some(Err(e)); + } + // We're completely done + (None, None) if buffer.is_empty() => return None, + (None, None) => { + match core::str::from_utf8(buffer) { + Ok(s) => { + current = s.chars(); + buffer = &[]; + } + Err(e) => { + let (valid, rest) = buffer.split_at(e.valid_up_to()); + // Skip the invalid sequence and stop completely if we ended early + let invalid_sequence_length = e.error_len()?; + buffer = &rest[invalid_sequence_length..]; + + // Set the current iterator to the valid section and indicate previous error + // SAFETY: `valid` is known to be valid utf-8 from error + current = unsafe { core::str::from_utf8_unchecked(valid) }.chars(); + previous_error = Some(e); + } + } + } + } + } + }) +} diff --git a/vendor/windows-strings/src/hstring.rs b/vendor/windows-strings/src/hstring.rs new file mode 100644 index 00000000..a8e359f0 --- /dev/null +++ b/vendor/windows-strings/src/hstring.rs @@ -0,0 +1,392 @@ +use super::*; +use core::ops::Deref; + +/// An ([HSTRING](https://docs.microsoft.com/en-us/windows/win32/winrt/hstring)) +/// is a reference-counted and immutable UTF-16 string type. +#[repr(transparent)] +pub struct HSTRING(pub(crate) *mut HStringHeader); + +impl HSTRING { + /// Create an empty `HSTRING`. + /// + /// This function does not allocate memory. + pub const fn new() -> Self { + Self(core::ptr::null_mut()) + } + + /// Create a `HSTRING` from a slice of 16 bit characters (wchars). + pub fn from_wide(value: &[u16]) -> Self { + unsafe { Self::from_wide_iter(value.iter().copied(), value.len()) } + } + + /// Get the contents of this `HSTRING` as a String lossily. + pub fn to_string_lossy(&self) -> String { + String::from_utf16_lossy(self) + } + + /// Get the contents of this `HSTRING` as a OsString. + #[cfg(feature = "std")] + pub fn to_os_string(&self) -> std::ffi::OsString { + std::os::windows::ffi::OsStringExt::from_wide(self) + } + + /// # Safety + /// len must not be less than the number of items in the iterator. + unsafe fn from_wide_iter<I: Iterator<Item = u16>>(iter: I, len: usize) -> Self { + if len == 0 { + return Self::new(); + } + + let ptr = HStringHeader::alloc(len.try_into().unwrap()); + + // Place each utf-16 character into the buffer and + // increase len as we go along. + for (index, wide) in iter.enumerate() { + debug_assert!(index < len); + + unsafe { + (*ptr).data.add(index).write(wide); + (*ptr).len = index as u32 + 1; + } + } + + unsafe { + // Write a 0 byte to the end of the buffer. + (*ptr).data.offset((*ptr).len as isize).write(0); + } + Self(ptr) + } + + fn as_header(&self) -> Option<&HStringHeader> { + unsafe { self.0.as_ref() } + } +} + +impl Deref for HSTRING { + type Target = [u16]; + + fn deref(&self) -> &[u16] { + if let Some(header) = self.as_header() { + unsafe { core::slice::from_raw_parts(header.data, header.len as usize) } + } else { + // This ensures that if `as_ptr` is called on the slice that the resulting pointer + // will still refer to a null-terminated string. + const EMPTY: [u16; 1] = [0]; + &EMPTY[..0] + } + } +} + +impl Default for HSTRING { + fn default() -> Self { + Self::new() + } +} + +impl Clone for HSTRING { + fn clone(&self) -> Self { + if let Some(header) = self.as_header() { + Self(header.duplicate()) + } else { + Self::new() + } + } +} + +impl Drop for HSTRING { + fn drop(&mut self) { + if let Some(header) = self.as_header() { + // HSTRING_REFERENCE_FLAG indicates a string backed by static or stack memory that is + // thus not reference-counted and does not need to be freed. + unsafe { + if header.flags & HSTRING_REFERENCE_FLAG == 0 && header.count.release() == 0 { + HStringHeader::free(self.0); + } + } + } + } +} + +unsafe impl Send for HSTRING {} +unsafe impl Sync for HSTRING {} + +impl core::fmt::Display for HSTRING { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "{}", + Decode(|| core::char::decode_utf16(self.iter().cloned())) + ) + } +} + +impl core::fmt::Debug for HSTRING { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "\"{self}\"") + } +} + +impl From<&str> for HSTRING { + fn from(value: &str) -> Self { + unsafe { Self::from_wide_iter(value.encode_utf16(), value.len()) } + } +} + +impl From<String> for HSTRING { + fn from(value: String) -> Self { + value.as_str().into() + } +} + +impl From<&String> for HSTRING { + fn from(value: &String) -> Self { + value.as_str().into() + } +} + +#[cfg(feature = "std")] +impl From<&std::path::Path> for HSTRING { + fn from(value: &std::path::Path) -> Self { + value.as_os_str().into() + } +} + +#[cfg(feature = "std")] +impl From<&std::ffi::OsStr> for HSTRING { + fn from(value: &std::ffi::OsStr) -> Self { + unsafe { + Self::from_wide_iter( + std::os::windows::ffi::OsStrExt::encode_wide(value), + value.len(), + ) + } + } +} + +#[cfg(feature = "std")] +impl From<std::ffi::OsString> for HSTRING { + fn from(value: std::ffi::OsString) -> Self { + value.as_os_str().into() + } +} + +#[cfg(feature = "std")] +impl From<&std::ffi::OsString> for HSTRING { + fn from(value: &std::ffi::OsString) -> Self { + value.as_os_str().into() + } +} + +impl Eq for HSTRING {} + +impl Ord for HSTRING { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + self.deref().cmp(other) + } +} + +impl core::hash::Hash for HSTRING { + fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) { + self.deref().hash(hasher) + } +} + +impl PartialOrd for HSTRING { + fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { + Some(self.cmp(other)) + } +} + +impl PartialEq for HSTRING { + fn eq(&self, other: &Self) -> bool { + self.deref() == other.deref() + } +} + +impl PartialEq<String> for HSTRING { + fn eq(&self, other: &String) -> bool { + *self == **other + } +} + +impl PartialEq<String> for &HSTRING { + fn eq(&self, other: &String) -> bool { + **self == **other + } +} + +impl PartialEq<&String> for HSTRING { + fn eq(&self, other: &&String) -> bool { + *self == ***other + } +} + +impl PartialEq<str> for HSTRING { + fn eq(&self, other: &str) -> bool { + self.iter().copied().eq(other.encode_utf16()) + } +} + +impl PartialEq<str> for &HSTRING { + fn eq(&self, other: &str) -> bool { + **self == *other + } +} + +impl PartialEq<&str> for HSTRING { + fn eq(&self, other: &&str) -> bool { + *self == **other + } +} + +impl PartialEq<HSTRING> for str { + fn eq(&self, other: &HSTRING) -> bool { + *other == *self + } +} + +impl PartialEq<HSTRING> for &str { + fn eq(&self, other: &HSTRING) -> bool { + *other == **self + } +} + +impl PartialEq<&HSTRING> for str { + fn eq(&self, other: &&HSTRING) -> bool { + **other == *self + } +} + +impl PartialEq<HSTRING> for String { + fn eq(&self, other: &HSTRING) -> bool { + *other == **self + } +} + +impl PartialEq<HSTRING> for &String { + fn eq(&self, other: &HSTRING) -> bool { + *other == ***self + } +} + +impl PartialEq<&HSTRING> for String { + fn eq(&self, other: &&HSTRING) -> bool { + **other == **self + } +} + +#[cfg(feature = "std")] +impl PartialEq<std::ffi::OsString> for HSTRING { + fn eq(&self, other: &std::ffi::OsString) -> bool { + *self == **other + } +} + +#[cfg(feature = "std")] +impl PartialEq<std::ffi::OsString> for &HSTRING { + fn eq(&self, other: &std::ffi::OsString) -> bool { + **self == **other + } +} + +#[cfg(feature = "std")] +impl PartialEq<&std::ffi::OsString> for HSTRING { + fn eq(&self, other: &&std::ffi::OsString) -> bool { + *self == ***other + } +} + +#[cfg(feature = "std")] +impl PartialEq<std::ffi::OsStr> for HSTRING { + fn eq(&self, other: &std::ffi::OsStr) -> bool { + self.iter() + .copied() + .eq(std::os::windows::ffi::OsStrExt::encode_wide(other)) + } +} + +#[cfg(feature = "std")] +impl PartialEq<std::ffi::OsStr> for &HSTRING { + fn eq(&self, other: &std::ffi::OsStr) -> bool { + **self == *other + } +} + +#[cfg(feature = "std")] +impl PartialEq<&std::ffi::OsStr> for HSTRING { + fn eq(&self, other: &&std::ffi::OsStr) -> bool { + *self == **other + } +} + +#[cfg(feature = "std")] +impl PartialEq<HSTRING> for std::ffi::OsStr { + fn eq(&self, other: &HSTRING) -> bool { + *other == *self + } +} + +#[cfg(feature = "std")] +impl PartialEq<HSTRING> for &std::ffi::OsStr { + fn eq(&self, other: &HSTRING) -> bool { + *other == **self + } +} + +#[cfg(feature = "std")] +impl PartialEq<&HSTRING> for std::ffi::OsStr { + fn eq(&self, other: &&HSTRING) -> bool { + **other == *self + } +} + +#[cfg(feature = "std")] +impl PartialEq<HSTRING> for std::ffi::OsString { + fn eq(&self, other: &HSTRING) -> bool { + *other == **self + } +} + +#[cfg(feature = "std")] +impl PartialEq<HSTRING> for &std::ffi::OsString { + fn eq(&self, other: &HSTRING) -> bool { + *other == ***self + } +} + +#[cfg(feature = "std")] +impl PartialEq<&HSTRING> for std::ffi::OsString { + fn eq(&self, other: &&HSTRING) -> bool { + **other == **self + } +} + +impl TryFrom<&HSTRING> for String { + type Error = alloc::string::FromUtf16Error; + + fn try_from(hstring: &HSTRING) -> core::result::Result<Self, Self::Error> { + String::from_utf16(hstring) + } +} + +impl TryFrom<HSTRING> for String { + type Error = alloc::string::FromUtf16Error; + + fn try_from(hstring: HSTRING) -> core::result::Result<Self, Self::Error> { + String::try_from(&hstring) + } +} + +#[cfg(feature = "std")] +impl From<&HSTRING> for std::ffi::OsString { + fn from(hstring: &HSTRING) -> Self { + hstring.to_os_string() + } +} + +#[cfg(feature = "std")] +impl From<HSTRING> for std::ffi::OsString { + fn from(hstring: HSTRING) -> Self { + Self::from(&hstring) + } +} diff --git a/vendor/windows-strings/src/hstring_builder.rs b/vendor/windows-strings/src/hstring_builder.rs new file mode 100644 index 00000000..fcbe4d98 --- /dev/null +++ b/vendor/windows-strings/src/hstring_builder.rs @@ -0,0 +1,100 @@ +use super::*; + +/// An [HSTRING] builder that supports preallocating the `HSTRING` to avoid extra allocations and copies. +/// +/// This is similar to the `WindowsPreallocateStringBuffer` function but implemented directly in Rust for efficiency. +/// It is implemented as a separate type since [HSTRING] values are immutable. +pub struct HStringBuilder(*mut HStringHeader); + +impl HStringBuilder { + /// Creates a preallocated `HSTRING` value. + pub fn new(len: usize) -> Self { + let header = HStringHeader::alloc(len.try_into().unwrap()); + + if len > 0 { + unsafe { core::ptr::write_bytes((*header).data, 0, len) }; + } + + Self(header) + } + + /// Shortens the string by removing any trailing 0 characters. + pub fn trim_end(&mut self) { + if let Some(header) = self.as_header_mut() { + while header.len > 0 + && unsafe { header.data.offset(header.len as isize - 1).read() == 0 } + { + header.len -= 1; + } + + if header.len == 0 { + unsafe { + HStringHeader::free(self.0); + } + self.0 = core::ptr::null_mut(); + } + } + } + + /// Allows the `HSTRING` to be constructed from bytes. + pub fn as_bytes_mut(&mut self) -> &mut [u8] { + if let Some(header) = self.as_header() { + unsafe { + core::slice::from_raw_parts_mut(header.data as *mut _, header.len as usize * 2) + } + } else { + &mut [] + } + } + + fn as_header(&self) -> Option<&HStringHeader> { + unsafe { self.0.as_ref() } + } + + fn as_header_mut(&mut self) -> Option<&mut HStringHeader> { + unsafe { self.0.as_mut() } + } +} + +impl From<HStringBuilder> for HSTRING { + fn from(value: HStringBuilder) -> Self { + if let Some(header) = value.as_header() { + unsafe { header.data.offset(header.len as isize).write(0) }; + let result = Self(value.0); + core::mem::forget(value); + result + } else { + Self::new() + } + } +} + +impl core::ops::Deref for HStringBuilder { + type Target = [u16]; + + fn deref(&self) -> &[u16] { + if let Some(header) = self.as_header() { + unsafe { core::slice::from_raw_parts(header.data, header.len as usize) } + } else { + &[] + } + } +} + +impl core::ops::DerefMut for HStringBuilder { + fn deref_mut(&mut self) -> &mut [u16] { + if let Some(header) = self.as_header() { + unsafe { core::slice::from_raw_parts_mut(header.data, header.len as usize) } + } else { + &mut [] + } + } +} + +impl Drop for HStringBuilder { + fn drop(&mut self) { + unsafe { + HStringHeader::free(self.0); + } + } +} diff --git a/vendor/windows-strings/src/hstring_header.rs b/vendor/windows-strings/src/hstring_header.rs new file mode 100644 index 00000000..39401102 --- /dev/null +++ b/vendor/windows-strings/src/hstring_header.rs @@ -0,0 +1,70 @@ +use super::*; + +pub const HSTRING_REFERENCE_FLAG: u32 = 1; + +#[repr(C)] +pub struct HStringHeader { + pub flags: u32, + pub len: u32, + pub _0: u32, + pub _1: u32, + pub data: *mut u16, + pub count: RefCount, + pub buffer_start: u16, +} + +impl HStringHeader { + pub fn alloc(len: u32) -> *mut Self { + if len == 0 { + return core::ptr::null_mut(); + } + + // Allocate enough space for header and two bytes per character. + // The space for the terminating null character is already accounted for inside of `HStringHeader`. + let bytes = core::mem::size_of::<Self>() + 2 * len as usize; + + let header = + unsafe { bindings::HeapAlloc(bindings::GetProcessHeap(), 0, bytes) } as *mut Self; + + if header.is_null() { + panic!("allocation failed"); + } + + unsafe { + // Use `ptr::write` (since `header` is uninitialized). `HStringHeader` is safe to be all zeros. + header.write(core::mem::MaybeUninit::<Self>::zeroed().assume_init()); + (*header).len = len; + (*header).count = RefCount::new(1); + (*header).data = &mut (*header).buffer_start; + } + + header + } + + pub unsafe fn free(header: *mut Self) { + if header.is_null() { + return; + } + + unsafe { + bindings::HeapFree(bindings::GetProcessHeap(), 0, header as *mut _); + } + } + + pub fn duplicate(&self) -> *mut Self { + if self.flags & HSTRING_REFERENCE_FLAG == 0 { + // If this is not a "fast pass" string then simply increment the reference count. + self.count.add_ref(); + self as *const Self as *mut Self + } else { + // Otherwise, allocate a new string and copy the value into the new string. + let copy = Self::alloc(self.len); + // SAFETY: since we are duplicating the string it is safe to copy all data from self to the initialized `copy`. + // We copy `len + 1` characters since `len` does not account for the terminating null character. + unsafe { + core::ptr::copy_nonoverlapping(self.data, (*copy).data, self.len as usize + 1); + } + copy + } + } +} diff --git a/vendor/windows-strings/src/lib.rs b/vendor/windows-strings/src/lib.rs new file mode 100644 index 00000000..ba12bb17 --- /dev/null +++ b/vendor/windows-strings/src/lib.rs @@ -0,0 +1,47 @@ +#![doc = include_str!("../readme.md")] +#![cfg(windows)] +#![allow(non_snake_case)] +#![debugger_visualizer(natvis_file = "../windows-strings.natvis")] +#![cfg_attr(all(not(feature = "std")), no_std)] + +extern crate alloc; +use alloc::string::String; + +mod bstr; +pub use bstr::*; + +mod hstring; +pub use hstring::*; + +mod hstring_builder; +pub use hstring_builder::*; + +mod hstring_header; +use hstring_header::*; + +mod bindings; + +mod decode; +use decode::*; + +mod ref_count; +use ref_count::*; + +mod literals; +pub use literals::*; + +mod pcstr; +pub use pcstr::*; + +mod pcwstr; +pub use pcwstr::*; + +mod pstr; +pub use pstr::*; + +mod pwstr; +pub use pwstr::*; + +extern "C" { + fn strlen(s: PCSTR) -> usize; +} diff --git a/vendor/windows-strings/src/literals.rs b/vendor/windows-strings/src/literals.rs new file mode 100644 index 00000000..bb27e79a --- /dev/null +++ b/vendor/windows-strings/src/literals.rs @@ -0,0 +1,169 @@ +/// A literal UTF-8 string with a trailing null terminator. +#[macro_export] +macro_rules! s { + ($s:literal) => { + $crate::PCSTR::from_raw(::core::concat!($s, '\0').as_ptr()) + }; +} + +/// A literal UTF-16 wide string with a trailing null terminator. +#[macro_export] +macro_rules! w { + ($s:literal) => {{ + const INPUT: &[u8] = $s.as_bytes(); + const OUTPUT_LEN: usize = $crate::utf16_len(INPUT) + 1; + const OUTPUT: &[u16; OUTPUT_LEN] = { + let mut buffer = [0; OUTPUT_LEN]; + let mut input_pos = 0; + let mut output_pos = 0; + while let Some((mut code_point, new_pos)) = $crate::decode_utf8_char(INPUT, input_pos) { + input_pos = new_pos; + if code_point <= 0xffff { + buffer[output_pos] = code_point as u16; + output_pos += 1; + } else { + code_point -= 0x10000; + buffer[output_pos] = 0xd800 + (code_point >> 10) as u16; + output_pos += 1; + buffer[output_pos] = 0xdc00 + (code_point & 0x3ff) as u16; + output_pos += 1; + } + } + &{ buffer } + }; + $crate::PCWSTR::from_raw(OUTPUT.as_ptr()) + }}; +} + +/// A literal HSTRING, length-prefixed wide string with a trailing null terminator. +#[macro_export] +macro_rules! h { + ($s:literal) => {{ + const INPUT: &[u8] = $s.as_bytes(); + const OUTPUT_LEN: usize = $crate::utf16_len(INPUT) + 1; + static RESULT: $crate::HSTRING = { + if OUTPUT_LEN == 1 { + unsafe { ::core::mem::transmute(::core::ptr::null::<u16>()) } + } else { + const OUTPUT: $crate::PCWSTR = $crate::w!($s); + const HEADER: $crate::HSTRING_HEADER = $crate::HSTRING_HEADER { + flags: 0x11, + len: (OUTPUT_LEN - 1) as u32, + padding1: 0, + padding2: 0, + ptr: OUTPUT.as_ptr(), + padding3: 0, + padding4: 0, + }; + // SAFETY: an `HSTRING` is exactly equivalent to a pointer to an `HSTRING_HEADER` + unsafe { + ::core::mem::transmute::<&$crate::HSTRING_HEADER, $crate::HSTRING>(&HEADER) + } + } + }; + &RESULT + }}; +} + +#[doc(hidden)] +pub const fn decode_utf8_char(bytes: &[u8], mut pos: usize) -> Option<(u32, usize)> { + if bytes.len() == pos { + return None; + } + let ch = bytes[pos] as u32; + pos += 1; + if ch <= 0x7f { + return Some((ch, pos)); + } + if (ch & 0xe0) == 0xc0 { + if bytes.len() - pos < 1 { + return None; + } + let ch2 = bytes[pos] as u32; + pos += 1; + if (ch2 & 0xc0) != 0x80 { + return None; + } + let result: u32 = ((ch & 0x1f) << 6) | (ch2 & 0x3f); + if result <= 0x7f { + return None; + } + return Some((result, pos)); + } + if (ch & 0xf0) == 0xe0 { + if bytes.len() - pos < 2 { + return None; + } + let ch2 = bytes[pos] as u32; + pos += 1; + let ch3 = bytes[pos] as u32; + pos += 1; + if (ch2 & 0xc0) != 0x80 || (ch3 & 0xc0) != 0x80 { + return None; + } + let result = ((ch & 0x0f) << 12) | ((ch2 & 0x3f) << 6) | (ch3 & 0x3f); + if result <= 0x7ff || (0xd800 <= result && result <= 0xdfff) { + return None; + } + return Some((result, pos)); + } + if (ch & 0xf8) == 0xf0 { + if bytes.len() - pos < 3 { + return None; + } + let ch2 = bytes[pos] as u32; + pos += 1; + let ch3 = bytes[pos] as u32; + pos += 1; + let ch4 = bytes[pos] as u32; + pos += 1; + if (ch2 & 0xc0) != 0x80 || (ch3 & 0xc0) != 0x80 || (ch4 & 0xc0) != 0x80 { + return None; + } + let result = + ((ch & 0x07) << 18) | ((ch2 & 0x3f) << 12) | ((ch3 & 0x3f) << 6) | (ch4 & 0x3f); + if result <= 0xffff || 0x10ffff < result { + return None; + } + return Some((result, pos)); + } + None +} + +#[doc(hidden)] +#[repr(C)] +pub struct HSTRING_HEADER { + pub flags: u32, + pub len: u32, + pub padding1: u32, + pub padding2: u32, + pub ptr: *const u16, + pub padding3: i32, + pub padding4: u16, +} + +#[doc(hidden)] +pub const fn utf16_len(bytes: &[u8]) -> usize { + let mut pos = 0; + let mut len = 0; + while let Some((code_point, new_pos)) = decode_utf8_char(bytes, pos) { + pos = new_pos; + len += if code_point <= 0xffff { 1 } else { 2 }; + } + len +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test() { + assert_eq!(decode_utf8_char(b"123", 0), Some((0x31, 1))); + assert_eq!(decode_utf8_char(b"123", 1), Some((0x32, 2))); + assert_eq!(decode_utf8_char(b"123", 2), Some((0x33, 3))); + assert_eq!(decode_utf8_char(b"123", 3), None); + assert_eq!(utf16_len(b"123"), 3); + assert_eq!(utf16_len("α & ω".as_bytes()), 5); + } +} diff --git a/vendor/windows-strings/src/pcstr.rs b/vendor/windows-strings/src/pcstr.rs new file mode 100644 index 00000000..cd752854 --- /dev/null +++ b/vendor/windows-strings/src/pcstr.rs @@ -0,0 +1,64 @@ +use super::*; + +/// A pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct PCSTR(pub *const u8); + +impl PCSTR { + /// Construct a new `PCSTR` from a raw pointer + pub const fn from_raw(ptr: *const u8) -> Self { + Self(ptr) + } + + /// Construct a null `PCSTR` + pub const fn null() -> Self { + Self(core::ptr::null()) + } + + /// Returns a raw pointer to the `PCSTR` + pub const fn as_ptr(&self) -> *const u8 { + self.0 + } + + /// Checks whether the `PCSTR` is null + pub fn is_null(&self) -> bool { + self.0.is_null() + } + + /// String data without the trailing 0 + /// + /// # Safety + /// + /// The `PCSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn as_bytes(&self) -> &[u8] { + unsafe { + let len = strlen(*self); + core::slice::from_raw_parts(self.0, len) + } + } + + /// Copy the `PCSTR` into a Rust `String`. + /// + /// # Safety + /// + /// See the safety information for `PCSTR::as_bytes`. + pub unsafe fn to_string(&self) -> core::result::Result<String, alloc::string::FromUtf8Error> { + unsafe { String::from_utf8(self.as_bytes().into()) } + } + + /// Allow this string to be displayed. + /// + /// # Safety + /// + /// See the safety information for `PCSTR::as_bytes`. + pub unsafe fn display(&self) -> impl core::fmt::Display + '_ { + unsafe { Decode(move || decode_utf8(self.as_bytes())) } + } +} + +impl Default for PCSTR { + fn default() -> Self { + Self::null() + } +} diff --git a/vendor/windows-strings/src/pcwstr.rs b/vendor/windows-strings/src/pcwstr.rs new file mode 100644 index 00000000..221bfd9b --- /dev/null +++ b/vendor/windows-strings/src/pcwstr.rs @@ -0,0 +1,97 @@ +use super::*; + +/// A pointer to a constant null-terminated string of 16-bit Unicode characters. +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct PCWSTR(pub *const u16); + +impl PCWSTR { + /// Construct a new `PCWSTR` from a raw pointer + pub const fn from_raw(ptr: *const u16) -> Self { + Self(ptr) + } + + /// Construct a null `PCWSTR` + pub const fn null() -> Self { + Self(core::ptr::null()) + } + + /// Returns a raw pointer to the `PCWSTR` + pub const fn as_ptr(&self) -> *const u16 { + self.0 + } + + /// Checks whether the `PCWSTR` is null + pub fn is_null(&self) -> bool { + self.0.is_null() + } + + /// String length without the trailing 0 + /// + /// # Safety + /// + /// The `PCWSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn len(&self) -> usize { + extern "C" { + fn wcslen(s: *const u16) -> usize; + } + unsafe { wcslen(self.0) } + } + + /// Returns `true` if the string length is zero, and `false` otherwise. + /// + /// # Safety + /// + /// The `PCWSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn is_empty(&self) -> bool { + unsafe { self.len() == 0 } + } + + /// String data without the trailing 0 + /// + /// # Safety + /// + /// The `PCWSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn as_wide(&self) -> &[u16] { + unsafe { core::slice::from_raw_parts(self.0, self.len()) } + } + + /// Copy the `PCWSTR` into a Rust `String`. + /// + /// # Safety + /// + /// See the safety information for `PCWSTR::as_wide`. + pub unsafe fn to_string(&self) -> core::result::Result<String, alloc::string::FromUtf16Error> { + unsafe { String::from_utf16(self.as_wide()) } + } + + /// Copy the `PCWSTR` into an `HSTRING`. + /// + /// # Safety + /// + /// See the safety information for `PCWSTR::as_wide`. + pub unsafe fn to_hstring(&self) -> HSTRING { + unsafe { HSTRING::from_wide(self.as_wide()) } + } + + /// Allow this string to be displayed. + /// + /// # Safety + /// + /// See the safety information for `PCWSTR::as_wide`. + pub unsafe fn display(&self) -> impl core::fmt::Display + '_ { + unsafe { Decode(move || core::char::decode_utf16(self.as_wide().iter().cloned())) } + } +} + +impl Default for PCWSTR { + fn default() -> Self { + Self::null() + } +} + +impl AsRef<PCWSTR> for PCWSTR { + fn as_ref(&self) -> &Self { + self + } +} diff --git a/vendor/windows-strings/src/pstr.rs b/vendor/windows-strings/src/pstr.rs new file mode 100644 index 00000000..141d481b --- /dev/null +++ b/vendor/windows-strings/src/pstr.rs @@ -0,0 +1,64 @@ +use super::*; + +/// A pointer to a null-terminated string of 8-bit Windows (ANSI) characters. +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct PSTR(pub *mut u8); + +impl PSTR { + /// Construct a new `PSTR` from a raw pointer + pub const fn from_raw(ptr: *mut u8) -> Self { + Self(ptr) + } + + /// Construct a null `PSTR` + pub const fn null() -> Self { + Self(core::ptr::null_mut()) + } + + /// Returns a raw pointer to the `PSTR` + pub const fn as_ptr(&self) -> *mut u8 { + self.0 + } + + /// Checks whether the `PSTR` is null + pub fn is_null(&self) -> bool { + self.0.is_null() + } + + /// String data without the trailing 0 + /// + /// # Safety + /// + /// The `PSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn as_bytes(&self) -> &[u8] { + unsafe { + let len = strlen(PCSTR::from_raw(self.0)); + core::slice::from_raw_parts(self.0, len) + } + } + + /// Copy the `PSTR` into a Rust `String`. + /// + /// # Safety + /// + /// See the safety information for `PSTR::as_bytes`. + pub unsafe fn to_string(&self) -> core::result::Result<String, alloc::string::FromUtf8Error> { + unsafe { String::from_utf8(self.as_bytes().into()) } + } + + /// Allow this string to be displayed. + /// + /// # Safety + /// + /// See the safety information for `PSTR::as_bytes`. + pub unsafe fn display(&self) -> impl core::fmt::Display + '_ { + unsafe { Decode(move || decode_utf8(self.as_bytes())) } + } +} + +impl Default for PSTR { + fn default() -> Self { + Self::null() + } +} diff --git a/vendor/windows-strings/src/pwstr.rs b/vendor/windows-strings/src/pwstr.rs new file mode 100644 index 00000000..db4d74ac --- /dev/null +++ b/vendor/windows-strings/src/pwstr.rs @@ -0,0 +1,88 @@ +use super::*; + +/// A pointer to a null-terminated string of 16-bit Unicode characters. +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct PWSTR(pub *mut u16); + +impl PWSTR { + /// Construct a new `PWSTR` from a raw pointer. + pub const fn from_raw(ptr: *mut u16) -> Self { + Self(ptr) + } + + /// Construct a null `PWSTR`. + pub const fn null() -> Self { + Self(core::ptr::null_mut()) + } + + /// Returns a raw pointer to the `PWSTR`. + pub const fn as_ptr(&self) -> *mut u16 { + self.0 + } + + /// Checks whether the `PWSTR` is null. + pub fn is_null(&self) -> bool { + self.0.is_null() + } + + /// String length without the trailing 0 + /// + /// # Safety + /// + /// The `PWSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn len(&self) -> usize { + unsafe { PCWSTR(self.0).len() } + } + + /// Returns `true` if the string length is zero, and `false` otherwise. + /// + /// # Safety + /// + /// The `PWSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn is_empty(&self) -> bool { + unsafe { self.len() == 0 } + } + + /// String data without the trailing 0. + /// + /// # Safety + /// + /// The `PWSTR`'s pointer needs to be valid for reads up until and including the next `\0`. + pub unsafe fn as_wide(&self) -> &[u16] { + unsafe { core::slice::from_raw_parts(self.0, self.len()) } + } + + /// Copy the `PWSTR` into a Rust `String`. + /// + /// # Safety + /// + /// See the safety information for `PWSTR::as_wide`. + pub unsafe fn to_string(&self) -> core::result::Result<String, alloc::string::FromUtf16Error> { + unsafe { String::from_utf16(self.as_wide()) } + } + + /// Copy the `PWSTR` into an `HSTRING`. + /// + /// # Safety + /// + /// See the safety information for `PWSTR::as_wide`. + pub unsafe fn to_hstring(&self) -> HSTRING { + unsafe { HSTRING::from_wide(self.as_wide()) } + } + + /// Allow this string to be displayed. + /// + /// # Safety + /// + /// See the safety information for `PWSTR::as_wide`. + pub unsafe fn display(&self) -> impl core::fmt::Display + '_ { + unsafe { Decode(move || core::char::decode_utf16(self.as_wide().iter().cloned())) } + } +} + +impl Default for PWSTR { + fn default() -> Self { + Self::null() + } +} diff --git a/vendor/windows-strings/src/ref_count.rs b/vendor/windows-strings/src/ref_count.rs new file mode 100644 index 00000000..c6309f9f --- /dev/null +++ b/vendor/windows-strings/src/ref_count.rs @@ -0,0 +1,27 @@ +use core::sync::atomic::{fence, AtomicI32, Ordering}; + +#[repr(transparent)] +#[derive(Default)] +pub struct RefCount(pub(crate) AtomicI32); + +impl RefCount { + pub fn new(count: u32) -> Self { + Self(AtomicI32::new(count as i32)) + } + + pub fn add_ref(&self) -> u32 { + (self.0.fetch_add(1, Ordering::Relaxed) + 1) as u32 + } + + pub fn release(&self) -> u32 { + let remaining = self.0.fetch_sub(1, Ordering::Release) - 1; + + match remaining.cmp(&0) { + core::cmp::Ordering::Equal => fence(Ordering::Acquire), + core::cmp::Ordering::Less => panic!("Object has been over-released."), + core::cmp::Ordering::Greater => {} + } + + remaining as u32 + } +} diff --git a/vendor/windows-strings/windows-strings.natvis b/vendor/windows-strings/windows-strings.natvis new file mode 100644 index 00000000..ae1dc31c --- /dev/null +++ b/vendor/windows-strings/windows-strings.natvis @@ -0,0 +1,62 @@ +<?xml version="1.0" encoding="utf-8"?> +<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> + <Type Name="windows_strings::hstring::HSTRING"> + <Intrinsic Name="header" Expression="(windows_strings::hstring_header::HStringHeader *)__0" /> + <Intrinsic Name="is_empty" Expression="__0 == 0" /> + <DisplayString Condition="is_empty()">""</DisplayString> + <DisplayString>{header()->data,[header()->len]su}</DisplayString> + + <Expand> + <Item Name="[len]">is_empty() ? (unsigned int)0 : header()->len</Item> + <Item Name="[ref_count]" Condition="!is_empty()">header()->count</Item> + <Item Name="[flags]" Condition="!is_empty()">header()->flags</Item> + <Synthetic Name="[chars]" Condition="!is_empty()"> + <Expand> + <ArrayItems> + <Size>header()->len</Size> + <ValuePointer>(char16_t*)header()->data</ValuePointer> + </ArrayItems> + </Expand> + </Synthetic> + </Expand> + </Type> + + <Type Name="windows_strings::pstr::PSTR"> + <AlternativeType Name="windows_strings::pcstr::PCSTR" /> + <Intrinsic Name="len" Expression="strlen(((char*)__0))" /> + <DisplayString>{(char*)__0,[len()]s8}</DisplayString> + <Expand> + <Item Name="[len]">len()</Item> + <Synthetic Name="[chars]"> + <Expand> + <ArrayItems> + <Size>len()</Size> + <ValuePointer>(char*)__0</ValuePointer> + </ArrayItems> + </Expand> + </Synthetic> + </Expand> + </Type> + + <Type Name="windows_strings::pwstr::PWSTR"> + <AlternativeType Name="windows_strings::pcwstr::PCWSTR" /> + <Intrinsic Name="len" Expression="wcslen(((WCHAR*)__0))" /> + <DisplayString>{(char16_t*)__0,[len()]su}</DisplayString> + + <Expand> + <Item Name="[len]">len()</Item> + <Synthetic Name="[chars]"> + <Expand> + <ArrayItems> + <Size>len()</Size> + <ValuePointer>(char16_t*)__0</ValuePointer> + </ArrayItems> + </Expand> + </Synthetic> + </Expand> + </Type> + + <Type Name="windows_strings::ref_count::RefCount"> + <DisplayString>{__0}</DisplayString> + </Type> +</AutoVisualizer> |
