1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | // |
5 | |
6 | // |
7 | // =========================================================================== |
8 | // File: coguid.cpp |
9 | // |
10 | // misc guid functions for PALRT |
11 | // =========================================================================== |
12 | |
13 | #include "common.h" |
14 | |
15 | STDAPI_(int) StringFromGUID2(REFGUID rguid, LPOLESTR lptsz, int cchMax) |
16 | { |
17 | if (cchMax < 39) |
18 | return 0; |
19 | |
20 | return swprintf_s(lptsz, cchMax, W("{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}" ), |
21 | rguid.Data1, rguid.Data2, rguid.Data3, |
22 | rguid.Data4[0], rguid.Data4[1], |
23 | rguid.Data4[2], rguid.Data4[3], |
24 | rguid.Data4[4], rguid.Data4[5], |
25 | rguid.Data4[6], rguid.Data4[7]) + 1; |
26 | } |
27 | |
28 | static BOOL wUUIDFromString(LPCWSTR lpsz, GUID * pguid); |
29 | static BOOL wGUIDFromString(LPCWSTR lpsz, GUID * pguid); |
30 | |
31 | static BOOL HexStringToDword(LPCWSTR FAR& lpsz, DWORD FAR& Value, |
32 | int cDigits, WCHAR chDelim); |
33 | |
34 | //+------------------------------------------------------------------------- |
35 | // |
36 | // Function: IIDFromString |
37 | // |
38 | // Synopsis: converts string {...} form int guid |
39 | // |
40 | // Arguments: [lpsz] - ptr to buffer for results |
41 | // [lpclsid] - the guid to convert |
42 | // |
43 | // Returns: NOERROR |
44 | // CO_E_CLASSSTRING |
45 | // |
46 | //-------------------------------------------------------------------------- |
47 | STDAPI IIDFromString(LPWSTR lpsz, CLSID * lpclsid) |
48 | { |
49 | if (lpsz == NULL) |
50 | { |
51 | *lpclsid = CLSID_NULL; |
52 | return NOERROR; |
53 | } |
54 | |
55 | if (*lpsz == 0) |
56 | { |
57 | return(CO_E_CLASSSTRING); |
58 | } |
59 | |
60 | return wGUIDFromString(lpsz,lpclsid) |
61 | ? NOERROR : CO_E_CLASSSTRING; |
62 | } |
63 | |
64 | //+------------------------------------------------------------------------- |
65 | // |
66 | // Function: wGUIDFromString (internal) |
67 | // |
68 | // Synopsis: Parse GUID such as {00000000-0000-0000-0000-000000000000} |
69 | // |
70 | // Arguments: [lpsz] - the guid string to convert |
71 | // [pguid] - guid to return |
72 | // |
73 | // Returns: TRUE if successful |
74 | // |
75 | //-------------------------------------------------------------------------- |
76 | static BOOL wGUIDFromString(LPCWSTR lpsz, GUID * pguid) |
77 | { |
78 | if (*lpsz++ != '{' ) |
79 | return FALSE; |
80 | |
81 | if (wUUIDFromString(lpsz, pguid) != TRUE) |
82 | return FALSE; |
83 | |
84 | lpsz +=36; |
85 | |
86 | if (*lpsz++ != '}' ) |
87 | return FALSE; |
88 | |
89 | if (*lpsz != '\0') |
90 | return FALSE; |
91 | |
92 | return TRUE; |
93 | } |
94 | |
95 | //+------------------------------------------------------------------------- |
96 | // |
97 | // Function: wUUIDFromString (internal) |
98 | // |
99 | // Synopsis: Parse UUID such as 00000000-0000-0000-0000-000000000000 |
100 | // |
101 | // Arguments: [lpsz] - Supplies the UUID string to convert |
102 | // [pguid] - Returns the GUID. |
103 | // |
104 | // Returns: TRUE if successful |
105 | // |
106 | //-------------------------------------------------------------------------- |
107 | static BOOL wUUIDFromString(LPCWSTR lpsz, GUID * pguid) |
108 | { |
109 | DWORD dw; |
110 | |
111 | if (!HexStringToDword(lpsz, pguid->Data1, sizeof(DWORD)*2, '-')) |
112 | return FALSE; |
113 | |
114 | if (!HexStringToDword(lpsz, dw, sizeof(WORD)*2, '-')) |
115 | return FALSE; |
116 | pguid->Data2 = (WORD)dw; |
117 | |
118 | if (!HexStringToDword(lpsz, dw, sizeof(WORD)*2, '-')) |
119 | return FALSE; |
120 | pguid->Data3 = (WORD)dw; |
121 | |
122 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, 0)) |
123 | return FALSE; |
124 | pguid->Data4[0] = (BYTE)dw; |
125 | |
126 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, '-')) |
127 | return FALSE; |
128 | pguid->Data4[1] = (BYTE)dw; |
129 | |
130 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, 0)) |
131 | return FALSE; |
132 | pguid->Data4[2] = (BYTE)dw; |
133 | |
134 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, 0)) |
135 | return FALSE; |
136 | pguid->Data4[3] = (BYTE)dw; |
137 | |
138 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, 0)) |
139 | return FALSE; |
140 | pguid->Data4[4] = (BYTE)dw; |
141 | |
142 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, 0)) |
143 | return FALSE; |
144 | pguid->Data4[5] = (BYTE)dw; |
145 | |
146 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, 0)) |
147 | return FALSE; |
148 | pguid->Data4[6] = (BYTE)dw; |
149 | |
150 | if (!HexStringToDword(lpsz, dw, sizeof(BYTE)*2, 0)) |
151 | return FALSE; |
152 | pguid->Data4[7] = (BYTE)dw; |
153 | |
154 | return TRUE; |
155 | } |
156 | |
157 | //+------------------------------------------------------------------------- |
158 | // |
159 | // Function: HexStringToDword (private) |
160 | // |
161 | // Synopsis: scan lpsz for a number of hex digits (at most 8); update lpsz |
162 | // return value in Value; check for chDelim; |
163 | // |
164 | // Arguments: [lpsz] - the hex string to convert |
165 | // [Value] - the returned value |
166 | // [cDigits] - count of digits |
167 | // |
168 | // Returns: TRUE for success |
169 | // |
170 | //-------------------------------------------------------------------------- |
171 | static BOOL HexStringToDword(LPCWSTR FAR& lpsz, DWORD FAR& Value, |
172 | int cDigits, WCHAR chDelim) |
173 | { |
174 | int Count; |
175 | |
176 | Value = 0; |
177 | for (Count = 0; Count < cDigits; Count++, lpsz++) |
178 | { |
179 | if (*lpsz >= '0' && *lpsz <= '9') |
180 | Value = (Value << 4) + *lpsz - '0'; |
181 | else if (*lpsz >= 'A' && *lpsz <= 'F') |
182 | Value = (Value << 4) + *lpsz - 'A' + 10; |
183 | else if (*lpsz >= 'a' && *lpsz <= 'f') |
184 | Value = (Value << 4) + *lpsz - 'a' + 10; |
185 | else |
186 | return(FALSE); |
187 | } |
188 | |
189 | if (chDelim != 0) |
190 | return *lpsz++ == chDelim; |
191 | else |
192 | return TRUE; |
193 | } |
194 | |