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