Debian Codegraph @

third_party/rust/windows-core/src/guid.rs

raw
1 #![allow(clippy::many_single_char_names)]
2
3 use super::*;
4
5 /// A globally unique identifier ([GUID](https://docs.microsoft.com/en-us/windows/win32/api/guiddef/ns-guiddef-guid))
6 /// used to identify COM and WinRT interfaces.
7 #[repr(C)]
8 #[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
9 pub struct GUID {
10 /// Specifies the first 8 hexadecimal digits.
11 pub data1: u32,
12
13 /// Specifies the first group of 4 hexadecimal digits.
14 pub data2: u16,
15
16 /// Specifies the second group of 4 hexadecimal digits.
17 pub data3: u16,
18
19 /// The first 2 bytes contain the third group of 4 hexadecimal digits. The remaining 6 bytes contain the final 12 hexadecimal digits.
20 pub data4: [u8; 8],
21 }
22
23 impl GUID {
24 /// Creates a unique `GUID` value.
25 pub fn new() -> Result<Self> {
26 unsafe { imp::CoCreateGuid() }
27 }
28
29 /// Creates a `GUID` represented by the all-zero byte-pattern.
30 pub const fn zeroed() -> Self {
31 Self {
32 data1: 0,
33 data2: 0,
34 data3: 0,
35 data4: [0, 0, 0, 0, 0, 0, 0, 0],
36 }
37 }
38
39 /// Creates a `GUID` with the given constant values.
40 pub const fn from_values(data1: u32, data2: u16, data3: u16, data4: [u8; 8]) -> Self {
41 Self {
42 data1,
43 data2,
44 data3,
45 data4,
46 }
47 }
48
49 /// Creates a `GUID` from a `u128` value.
50 pub const fn from_u128(uuid: u128) -> Self {
51 Self {
52 data1: (uuid >> 96) as u32,
53 data2: (uuid >> 80 & 0xffff) as u16,
54 data3: (uuid >> 64 & 0xffff) as u16,
55 data4: (uuid as u64).to_be_bytes(),
56 }
57 }
58
59 /// Converts a `GUID` to a `u128` value.
60 pub const fn to_u128(&self) -> u128 {
61 ((self.data1 as u128) << 96)
62 + ((self.data2 as u128) << 80)
63 + ((self.data3 as u128) << 64)
64 + u64::from_be_bytes(self.data4) as u128
65 }
66
67 /// Creates a `GUID` for a "generic" WinRT type.
68 pub const fn from_signature(signature: imp::ConstBuffer) -> Self {
69 let data = imp::ConstBuffer::from_slice(&[
70 0x11, 0xf4, 0x7a, 0xd5, 0x7b, 0x73, 0x42, 0xc0, 0xab, 0xae, 0x87, 0x8b, 0x1e, 0x16,
71 0xad, 0xee,
72 ]);
73
74 let data = data.push_other(signature);
75
76 let bytes = imp::sha1(&data).bytes();
77 let first = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
78
79 let second = u16::from_be_bytes([bytes[4], bytes[5]]);
80 let mut third = u16::from_be_bytes([bytes[6], bytes[7]]);
81 third = (third & 0x0fff) | (5 << 12);
82 let fourth = (bytes[8] & 0x3f) | 0x80;
83
84 Self::from_values(
85 first,
86 second,
87 third,
88 [
89 fourth, bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
90 ],
91 )
92 }
93 }
94
95 impl RuntimeType for GUID {
96 const SIGNATURE: imp::ConstBuffer = imp::ConstBuffer::from_slice(b"g16");
97 }
98
99 impl TypeKind for GUID {
100 type TypeKind = CopyType;
101 }
102
103 impl core::fmt::Debug for GUID {
104 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
105 write!(
106 f,
107 "{:08X?}-{:04X?}-{:04X?}-{:02X?}{:02X?}-{:02X?}{:02X?}{:02X?}{:02X?}{:02X?}{:02X?}",
108 self.data1,
109 self.data2,
110 self.data3,
111 self.data4[0],
112 self.data4[1],
113 self.data4[2],
114 self.data4[3],
115 self.data4[4],
116 self.data4[5],
117 self.data4[6],
118 self.data4[7]
119 )
120 }
121 }
122
123 impl From<&str> for GUID {
124 fn from(value: &str) -> Self {
125 assert!(value.len() == 36, "Invalid GUID string");
126 let mut bytes = value.bytes();
127
128 let a = ((bytes.next_u32() * 16 + bytes.next_u32()) << 24)
129 + ((bytes.next_u32() * 16 + bytes.next_u32()) << 16)
130 + ((bytes.next_u32() * 16 + bytes.next_u32()) << 8)
131 + bytes.next_u32() * 16
132 + bytes.next_u32();
133 assert!(bytes.next().unwrap() == b'-', "Invalid GUID string");
134 let b = ((bytes.next_u16() * 16 + (bytes.next_u16())) << 8)
135 + bytes.next_u16() * 16
136 + bytes.next_u16();
137 assert!(bytes.next().unwrap() == b'-', "Invalid GUID string");
138 let c = ((bytes.next_u16() * 16 + bytes.next_u16()) << 8)
139 + bytes.next_u16() * 16
140 + bytes.next_u16();
141 assert!(bytes.next().unwrap() == b'-', "Invalid GUID string");
142 let d = bytes.next_u8() * 16 + bytes.next_u8();
143 let e = bytes.next_u8() * 16 + bytes.next_u8();
144 assert!(bytes.next().unwrap() == b'-', "Invalid GUID string");
145
146 let f = bytes.next_u8() * 16 + bytes.next_u8();
147 let g = bytes.next_u8() * 16 + bytes.next_u8();
148 let h = bytes.next_u8() * 16 + bytes.next_u8();
149 let i = bytes.next_u8() * 16 + bytes.next_u8();
150 let j = bytes.next_u8() * 16 + bytes.next_u8();
151 let k = bytes.next_u8() * 16 + bytes.next_u8();
152
153 Self::from_values(a, b, c, [d, e, f, g, h, i, j, k])
154 }
155 }
156
157 impl From<u128> for GUID {
158 fn from(value: u128) -> Self {
159 Self::from_u128(value)
160 }
161 }
162
163 impl From<GUID> for u128 {
164 fn from(value: GUID) -> Self {
165 value.to_u128()
166 }
167 }
168
169 trait HexReader {
170 fn next_u8(&mut self) -> u8;
171 fn next_u16(&mut self) -> u16;
172 fn next_u32(&mut self) -> u32;
173 }
174
175 impl HexReader for core::str::Bytes<'_> {
176 fn next_u8(&mut self) -> u8 {
177 let value = self.next().unwrap();
178 match value {
179 b'0'..=b'9' => value - b'0',
180 b'A'..=b'F' => 10 + value - b'A',
181 b'a'..=b'f' => 10 + value - b'a',
182 _ => panic!(),
183 }
184 }
185
186 fn next_u16(&mut self) -> u16 {
187 self.next_u8().into()
188 }
189
190 fn next_u32(&mut self) -> u32 {
191 self.next_u8().into()
192 }
193 }