[PROPSYS]
[reactos.git] / reactos / dll / win32 / propsys / propsys_main.c
1 /*
2 * propsys main
3 *
4 * Copyright 1997, 2002 Alexandre Julliard
5 * Copyright 2008 James Hawkins
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "propsys_private.h"
23
24 #include <rpcproxy.h>
25
26 static HINSTANCE propsys_hInstance;
27
28 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
29 {
30 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
31
32 switch (fdwReason)
33 {
34 case DLL_WINE_PREATTACH:
35 return FALSE; /* prefer native version */
36 case DLL_PROCESS_ATTACH:
37 propsys_hInstance = hinstDLL;
38 DisableThreadLibraryCalls(hinstDLL);
39 break;
40 }
41
42 return TRUE;
43 }
44
45 HRESULT WINAPI DllRegisterServer(void)
46 {
47 return __wine_register_resources( propsys_hInstance );
48 }
49
50 HRESULT WINAPI DllUnregisterServer(void)
51 {
52 return __wine_unregister_resources( propsys_hInstance );
53 }
54
55 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
56 {
57 *ppv = NULL;
58
59 if(IsEqualGUID(&IID_IUnknown, riid)) {
60 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
61 *ppv = iface;
62 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
63 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
64 *ppv = iface;
65 }
66
67 if(*ppv) {
68 IUnknown_AddRef((IUnknown*)*ppv);
69 return S_OK;
70 }
71
72 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
73 return E_NOINTERFACE;
74 }
75
76 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
77 {
78 TRACE("(%p)\n", iface);
79 return 2;
80 }
81
82 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
83 {
84 TRACE("(%p)\n", iface);
85 return 1;
86 }
87
88 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
89 {
90 TRACE("(%p)->(%x)\n", iface, fLock);
91
92 return S_OK;
93 }
94
95 static HRESULT WINAPI InMemoryPropertyStoreFactory_CreateInstance(IClassFactory *iface, IUnknown *outer,
96 REFIID riid, void **ppv)
97 {
98 TRACE("(%p %s %p)\n", outer, debugstr_guid(riid), ppv);
99
100 return PropertyStore_CreateInstance(outer, riid, ppv);
101 }
102
103 static const IClassFactoryVtbl InMemoryPropertyStoreFactoryVtbl = {
104 ClassFactory_QueryInterface,
105 ClassFactory_AddRef,
106 ClassFactory_Release,
107 InMemoryPropertyStoreFactory_CreateInstance,
108 ClassFactory_LockServer
109 };
110
111 static IClassFactory InMemoryPropertyStoreFactory = { &InMemoryPropertyStoreFactoryVtbl };
112
113 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
114 {
115 if(IsEqualGUID(&CLSID_InMemoryPropertyStore, rclsid)) {
116 TRACE("(CLSID_InMemoryPropertyStore %s %p)\n", debugstr_guid(riid), ppv);
117 return IClassFactory_QueryInterface(&InMemoryPropertyStoreFactory, riid, ppv);
118 }
119
120 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
121 return CLASS_E_CLASSNOTAVAILABLE;
122 }
123
124 HRESULT WINAPI DllCanUnloadNow(void)
125 {
126 return S_FALSE;
127 }
128
129 HRESULT WINAPI PSRegisterPropertySchema(PCWSTR path)
130 {
131 FIXME("%s stub\n", debugstr_w(path));
132
133 return S_OK;
134 }
135
136 HRESULT WINAPI PSUnregisterPropertySchema(PCWSTR path)
137 {
138 FIXME("%s stub\n", debugstr_w(path));
139
140 return E_NOTIMPL;
141 }
142
143 HRESULT WINAPI PSGetPropertyDescription(REFPROPERTYKEY propkey, REFIID riid, void **ppv)
144 {
145 FIXME("%p, %p, %p\n", propkey, riid, ppv);
146 return E_NOTIMPL;
147 }
148
149 HRESULT WINAPI PSGetPropertyDescriptionListFromString(LPCWSTR proplist, REFIID riid, void **ppv)
150 {
151 FIXME("%s, %p, %p\n", debugstr_w(proplist), riid, ppv);
152 return E_NOTIMPL;
153 }
154
155 HRESULT WINAPI PSRefreshPropertySchema(void)
156 {
157 FIXME("\n");
158 return S_OK;
159 }
160
161 HRESULT WINAPI PSStringFromPropertyKey(REFPROPERTYKEY pkey, LPWSTR psz, UINT cch)
162 {
163 static const WCHAR guid_fmtW[] = {'{','%','0','8','X','-','%','0','4','X','-',
164 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
165 '%','0','2','X','%','0','2','X','%','0','2','X',
166 '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
167 static const WCHAR pid_fmtW[] = {'%','u',0};
168
169 WCHAR pidW[PKEY_PIDSTR_MAX + 1];
170 LPWSTR p = psz;
171 int len;
172
173 TRACE("(%p, %p, %u)\n", pkey, psz, cch);
174
175 if (!psz)
176 return E_POINTER;
177
178 /* GUIDSTRING_MAX accounts for null terminator, +1 for space character. */
179 if (cch <= GUIDSTRING_MAX + 1)
180 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
181
182 if (!pkey)
183 {
184 psz[0] = '\0';
185 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
186 }
187
188 sprintfW(psz, guid_fmtW, pkey->fmtid.Data1, pkey->fmtid.Data2,
189 pkey->fmtid.Data3, pkey->fmtid.Data4[0], pkey->fmtid.Data4[1],
190 pkey->fmtid.Data4[2], pkey->fmtid.Data4[3], pkey->fmtid.Data4[4],
191 pkey->fmtid.Data4[5], pkey->fmtid.Data4[6], pkey->fmtid.Data4[7]);
192
193 /* Overwrite the null terminator with the space character. */
194 p += GUIDSTRING_MAX - 1;
195 *p++ = ' ';
196 cch -= GUIDSTRING_MAX - 1 + 1;
197
198 len = sprintfW(pidW, pid_fmtW, pkey->pid);
199
200 if (cch >= len + 1)
201 {
202 strcpyW(p, pidW);
203 return S_OK;
204 }
205 else
206 {
207 WCHAR *ptr = pidW + len - 1;
208
209 psz[0] = '\0';
210 *p++ = '\0';
211 cch--;
212
213 /* Replicate a quirk of the native implementation where the contents
214 * of the property ID string are written backwards to the output
215 * buffer, skipping the rightmost digit. */
216 if (cch)
217 {
218 ptr--;
219 while (cch--)
220 *p++ = *ptr--;
221 }
222
223 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
224 }
225 }
226
227 static const BYTE hex2bin[] =
228 {
229 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
230 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
231 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
232 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
233 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
234 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
235 0,10,11,12,13,14,15 /* 0x60 */
236 };
237
238 static BOOL validate_indices(LPCWSTR s, int min, int max)
239 {
240 int i;
241
242 for (i = min; i <= max; i++)
243 {
244 if (!s[i])
245 return FALSE;
246
247 if (i == 0)
248 {
249 if (s[i] != '{')
250 return FALSE;
251 }
252 else if (i == 9 || i == 14 || i == 19 || i == 24)
253 {
254 if (s[i] != '-')
255 return FALSE;
256 }
257 else if (i == 37)
258 {
259 if (s[i] != '}')
260 return FALSE;
261 }
262 else
263 {
264 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0'))
265 return FALSE;
266 }
267 }
268
269 return TRUE;
270 }
271
272 /* Adapted from CLSIDFromString helper in dlls/ole32/compobj.c and
273 * UuidFromString in dlls/rpcrt4/rpcrt4_main.c. */
274 static BOOL string_to_guid(LPCWSTR s, LPGUID id)
275 {
276 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
277
278 if (!validate_indices(s, 0, 8)) return FALSE;
279 id->Data1 = (hex2bin[s[1]] << 28 | hex2bin[s[2]] << 24 | hex2bin[s[3]] << 20 | hex2bin[s[4]] << 16 |
280 hex2bin[s[5]] << 12 | hex2bin[s[6]] << 8 | hex2bin[s[7]] << 4 | hex2bin[s[8]]);
281 if (!validate_indices(s, 9, 14)) return FALSE;
282 id->Data2 = hex2bin[s[10]] << 12 | hex2bin[s[11]] << 8 | hex2bin[s[12]] << 4 | hex2bin[s[13]];
283 if (!validate_indices(s, 15, 19)) return FALSE;
284 id->Data3 = hex2bin[s[15]] << 12 | hex2bin[s[16]] << 8 | hex2bin[s[17]] << 4 | hex2bin[s[18]];
285
286 /* these are just sequential bytes */
287
288 if (!validate_indices(s, 20, 21)) return FALSE;
289 id->Data4[0] = hex2bin[s[20]] << 4 | hex2bin[s[21]];
290 if (!validate_indices(s, 22, 24)) return FALSE;
291 id->Data4[1] = hex2bin[s[22]] << 4 | hex2bin[s[23]];
292
293 if (!validate_indices(s, 25, 26)) return FALSE;
294 id->Data4[2] = hex2bin[s[25]] << 4 | hex2bin[s[26]];
295 if (!validate_indices(s, 27, 28)) return FALSE;
296 id->Data4[3] = hex2bin[s[27]] << 4 | hex2bin[s[28]];
297 if (!validate_indices(s, 29, 30)) return FALSE;
298 id->Data4[4] = hex2bin[s[29]] << 4 | hex2bin[s[30]];
299 if (!validate_indices(s, 31, 32)) return FALSE;
300 id->Data4[5] = hex2bin[s[31]] << 4 | hex2bin[s[32]];
301 if (!validate_indices(s, 33, 34)) return FALSE;
302 id->Data4[6] = hex2bin[s[33]] << 4 | hex2bin[s[34]];
303 if (!validate_indices(s, 35, 37)) return FALSE;
304 id->Data4[7] = hex2bin[s[35]] << 4 | hex2bin[s[36]];
305
306 return TRUE;
307 }
308
309 HRESULT WINAPI PSPropertyKeyFromString(LPCWSTR pszString, PROPERTYKEY *pkey)
310 {
311 BOOL has_minus = FALSE, has_comma = FALSE;
312
313 TRACE("(%s, %p)\n", debugstr_w(pszString), pkey);
314
315 if (!pszString || !pkey)
316 return E_POINTER;
317
318 memset(pkey, 0, sizeof(PROPERTYKEY));
319
320 if (!string_to_guid(pszString, &pkey->fmtid))
321 return E_INVALIDARG;
322
323 pszString += GUIDSTRING_MAX - 1;
324
325 if (!*pszString)
326 return E_INVALIDARG;
327
328 /* Only the space seems to be recognized as whitespace. The comma is only
329 * recognized once and processing terminates if another comma is found. */
330 while (*pszString == ' ' || *pszString == ',')
331 {
332 if (*pszString == ',')
333 {
334 if (has_comma)
335 return S_OK;
336 else
337 has_comma = TRUE;
338 }
339 pszString++;
340 }
341
342 if (!*pszString)
343 return E_INVALIDARG;
344
345 /* Only two minus signs are recognized if no comma is detected. The first
346 * sign is ignored, and the second is interpreted. If a comma is detected
347 * before the minus sign, then only one minus sign counts, and property ID
348 * interpretation begins with the next character. */
349 if (has_comma)
350 {
351 if (*pszString == '-')
352 {
353 has_minus = TRUE;
354 pszString++;
355 }
356 }
357 else
358 {
359 if (*pszString == '-')
360 pszString++;
361
362 /* Skip any intermediate spaces after the first minus sign. */
363 while (*pszString == ' ')
364 pszString++;
365
366 if (*pszString == '-')
367 {
368 has_minus = TRUE;
369 pszString++;
370 }
371
372 /* Skip any remaining spaces after minus sign. */
373 while (*pszString == ' ')
374 pszString++;
375 }
376
377 /* Overflow is not checked. */
378 while (isdigitW(*pszString))
379 {
380 pkey->pid *= 10;
381 pkey->pid += (*pszString - '0');
382 pszString++;
383 }
384
385 if (has_minus)
386 pkey->pid = ~pkey->pid + 1;
387
388 return S_OK;
389 }