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