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