[PROPSYS]
[reactos.git] / reactos / dll / win32 / propsys / propvar.c
1 /*
2 * PropVariant implementation
3 *
4 * Copyright 2008 James Hawkins for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 //#include <stdarg.h>
26 #include <stdio.h>
27
28 #define NONAMELESSUNION
29
30 #include <windef.h>
31 #include <winbase.h>
32 //#include "winerror.h"
33 #include <winreg.h>
34 //#include "winuser.h"
35 #include <shlobj.h>
36 #include <propvarutil.h>
37
38 #include <wine/debug.h>
39 #include <wine/unicode.h>
40
41 WINE_DEFAULT_DEBUG_CHANNEL(propsys);
42
43 static HRESULT PROPVAR_ConvertFILETIME(PROPVARIANT *ppropvarDest,
44 REFPROPVARIANT propvarSrc, VARTYPE vt)
45 {
46 SYSTEMTIME time;
47
48 FileTimeToSystemTime(&propvarSrc->u.filetime, &time);
49
50 switch (vt)
51 {
52 case VT_LPSTR:
53 {
54 static const char format[] = "%04d/%02d/%02d:%02d:%02d:%02d.%03d";
55
56 ppropvarDest->u.pszVal = HeapAlloc(GetProcessHeap(), 0,
57 lstrlenA(format) + 1);
58 if (!ppropvarDest->u.pszVal)
59 return E_OUTOFMEMORY;
60
61 sprintf(ppropvarDest->u.pszVal, format, time.wYear, time.wMonth,
62 time.wDay, time.wHour, time.wMinute,
63 time.wSecond, time.wMilliseconds);
64
65 return S_OK;
66 }
67
68 default:
69 FIXME("Unhandled target type: %d\n", vt);
70 }
71
72 return E_FAIL;
73 }
74
75 static HRESULT PROPVAR_ConvertNumber(REFPROPVARIANT pv, int dest_bits,
76 int dest_signed, LONGLONG *res)
77 {
78 int src_signed;
79
80 switch (pv->vt)
81 {
82 case VT_I1:
83 src_signed = 1;
84 *res = pv->u.cVal;
85 break;
86 case VT_UI1:
87 src_signed = 0;
88 *res = pv->u.bVal;
89 break;
90 case VT_I2:
91 src_signed = 1;
92 *res = pv->u.iVal;
93 break;
94 case VT_UI2:
95 src_signed = 0;
96 *res = pv->u.uiVal;
97 break;
98 case VT_I4:
99 src_signed = 1;
100 *res = pv->u.lVal;
101 break;
102 case VT_UI4:
103 src_signed = 0;
104 *res = pv->u.ulVal;
105 break;
106 case VT_I8:
107 src_signed = 1;
108 *res = pv->u.hVal.QuadPart;
109 break;
110 case VT_UI8:
111 src_signed = 0;
112 *res = pv->u.uhVal.QuadPart;
113 break;
114 case VT_EMPTY:
115 src_signed = 0;
116 *res = 0;
117 break;
118 default:
119 FIXME("unhandled vt %d\n", pv->vt);
120 return E_NOTIMPL;
121 }
122
123 if (*res < 0 && src_signed != dest_signed)
124 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
125
126 if (dest_bits < 64)
127 {
128 if (dest_signed)
129 {
130 if (*res >= ((LONGLONG)1 << (dest_bits-1)) ||
131 *res < ((LONGLONG)-1 << (dest_bits-1)))
132 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
133 }
134 else
135 {
136 if ((ULONGLONG)(*res) >= ((ULONGLONG)1 << dest_bits))
137 return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
138 }
139 }
140
141 return S_OK;
142 }
143
144 HRESULT WINAPI PropVariantToInt16(REFPROPVARIANT propvarIn, SHORT *ret)
145 {
146 LONGLONG res;
147 HRESULT hr;
148
149 TRACE("%p,%p\n", propvarIn, ret);
150
151 hr = PROPVAR_ConvertNumber(propvarIn, 16, 1, &res);
152 if (SUCCEEDED(hr)) *ret = (SHORT)res;
153 return hr;
154 }
155
156 HRESULT WINAPI PropVariantToInt32(REFPROPVARIANT propvarIn, LONG *ret)
157 {
158 LONGLONG res;
159 HRESULT hr;
160
161 TRACE("%p,%p\n", propvarIn, ret);
162
163 hr = PROPVAR_ConvertNumber(propvarIn, 32, 1, &res);
164 if (SUCCEEDED(hr)) *ret = (LONG)res;
165 return hr;
166 }
167
168 HRESULT WINAPI PropVariantToInt64(REFPROPVARIANT propvarIn, LONGLONG *ret)
169 {
170 LONGLONG res;
171 HRESULT hr;
172
173 TRACE("%p,%p\n", propvarIn, ret);
174
175 hr = PROPVAR_ConvertNumber(propvarIn, 64, 1, &res);
176 if (SUCCEEDED(hr)) *ret = (LONGLONG)res;
177 return hr;
178 }
179
180 HRESULT WINAPI PropVariantToUInt16(REFPROPVARIANT propvarIn, USHORT *ret)
181 {
182 LONGLONG res;
183 HRESULT hr;
184
185 TRACE("%p,%p\n", propvarIn, ret);
186
187 hr = PROPVAR_ConvertNumber(propvarIn, 16, 0, &res);
188 if (SUCCEEDED(hr)) *ret = (USHORT)res;
189 return hr;
190 }
191
192 HRESULT WINAPI PropVariantToUInt32(REFPROPVARIANT propvarIn, ULONG *ret)
193 {
194 LONGLONG res;
195 HRESULT hr;
196
197 TRACE("%p,%p\n", propvarIn, ret);
198
199 hr = PROPVAR_ConvertNumber(propvarIn, 32, 0, &res);
200 if (SUCCEEDED(hr)) *ret = (ULONG)res;
201 return hr;
202 }
203
204 HRESULT WINAPI PropVariantToUInt64(REFPROPVARIANT propvarIn, ULONGLONG *ret)
205 {
206 LONGLONG res;
207 HRESULT hr;
208
209 TRACE("%p,%p\n", propvarIn, ret);
210
211 hr = PROPVAR_ConvertNumber(propvarIn, 64, 0, &res);
212 if (SUCCEEDED(hr)) *ret = (ULONGLONG)res;
213 return hr;
214 }
215
216 /******************************************************************
217 * PropVariantChangeType (PROPSYS.@)
218 */
219 HRESULT WINAPI PropVariantChangeType(PROPVARIANT *ppropvarDest, REFPROPVARIANT propvarSrc,
220 PROPVAR_CHANGE_FLAGS flags, VARTYPE vt)
221 {
222 HRESULT hr;
223
224 FIXME("(%p, %p, %d, %d, %d): semi-stub!\n", ppropvarDest, propvarSrc,
225 propvarSrc->vt, flags, vt);
226
227 switch (vt)
228 {
229 case VT_I2:
230 {
231 SHORT res;
232 hr = PropVariantToInt16(propvarSrc, &res);
233 if (SUCCEEDED(hr))
234 {
235 ppropvarDest->vt = VT_I2;
236 ppropvarDest->u.iVal = res;
237 }
238 return hr;
239 }
240 case VT_UI2:
241 {
242 USHORT res;
243 hr = PropVariantToUInt16(propvarSrc, &res);
244 if (SUCCEEDED(hr))
245 {
246 ppropvarDest->vt = VT_UI2;
247 ppropvarDest->u.uiVal = res;
248 }
249 return hr;
250 }
251 case VT_I4:
252 {
253 LONG res;
254 hr = PropVariantToInt32(propvarSrc, &res);
255 if (SUCCEEDED(hr))
256 {
257 ppropvarDest->vt = VT_I4;
258 ppropvarDest->u.lVal = res;
259 }
260 return hr;
261 }
262 case VT_UI4:
263 {
264 ULONG res;
265 hr = PropVariantToUInt32(propvarSrc, &res);
266 if (SUCCEEDED(hr))
267 {
268 ppropvarDest->vt = VT_UI4;
269 ppropvarDest->u.ulVal = res;
270 }
271 return hr;
272 }
273 case VT_I8:
274 {
275 LONGLONG res;
276 hr = PropVariantToInt64(propvarSrc, &res);
277 if (SUCCEEDED(hr))
278 {
279 ppropvarDest->vt = VT_I8;
280 ppropvarDest->u.hVal.QuadPart = res;
281 }
282 return hr;
283 }
284 case VT_UI8:
285 {
286 ULONGLONG res;
287 hr = PropVariantToUInt64(propvarSrc, &res);
288 if (SUCCEEDED(hr))
289 {
290 ppropvarDest->vt = VT_UI8;
291 ppropvarDest->u.uhVal.QuadPart = res;
292 }
293 return hr;
294 }
295 }
296
297 switch (propvarSrc->vt)
298 {
299 case VT_FILETIME:
300 return PROPVAR_ConvertFILETIME(ppropvarDest, propvarSrc, vt);
301 default:
302 FIXME("Unhandled source type: %d\n", propvarSrc->vt);
303 }
304
305 return E_FAIL;
306 }
307
308 static void PROPVAR_GUIDToWSTR(REFGUID guid, WCHAR *str)
309 {
310 static const WCHAR format[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X',
311 '-','%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X',
312 '%','0','2','X','%','0','2','X','%','0','2','X','}',0};
313
314 sprintfW(str, format, guid->Data1, guid->Data2, guid->Data3,
315 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
316 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
317 }
318
319 HRESULT WINAPI InitPropVariantFromGUIDAsString(REFGUID guid, PROPVARIANT *ppropvar)
320 {
321 TRACE("(%p %p)\n", guid, ppropvar);
322
323 if(!guid)
324 return E_FAIL;
325
326 ppropvar->vt = VT_LPWSTR;
327 ppropvar->u.pwszVal = CoTaskMemAlloc(39*sizeof(WCHAR));
328 if(!ppropvar->u.pwszVal)
329 return E_OUTOFMEMORY;
330
331 PROPVAR_GUIDToWSTR(guid, ppropvar->u.pwszVal);
332 return S_OK;
333 }
334
335 HRESULT WINAPI InitVariantFromGUIDAsString(REFGUID guid, VARIANT *pvar)
336 {
337 TRACE("(%p %p)\n", guid, pvar);
338
339 if(!guid) {
340 FIXME("guid == NULL\n");
341 return E_FAIL;
342 }
343
344 V_VT(pvar) = VT_BSTR;
345 V_BSTR(pvar) = SysAllocStringLen(NULL, 38);
346 if(!V_BSTR(pvar))
347 return E_OUTOFMEMORY;
348
349 PROPVAR_GUIDToWSTR(guid, V_BSTR(pvar));
350 return S_OK;
351 }
352
353 HRESULT WINAPI InitPropVariantFromBuffer(const VOID *pv, UINT cb, PROPVARIANT *ppropvar)
354 {
355 TRACE("(%p %u %p)\n", pv, cb, ppropvar);
356
357 ppropvar->u.caub.pElems = CoTaskMemAlloc(cb);
358 if(!ppropvar->u.caub.pElems)
359 return E_OUTOFMEMORY;
360
361 ppropvar->vt = VT_VECTOR|VT_UI1;
362 ppropvar->u.caub.cElems = cb;
363 memcpy(ppropvar->u.caub.pElems, pv, cb);
364 return S_OK;
365 }
366
367 HRESULT WINAPI InitVariantFromBuffer(const VOID *pv, UINT cb, VARIANT *pvar)
368 {
369 SAFEARRAY *arr;
370 void *data;
371 HRESULT hres;
372
373 TRACE("(%p %u %p)\n", pv, cb, pvar);
374
375 arr = SafeArrayCreateVector(VT_UI1, 0, cb);
376 if(!arr)
377 return E_OUTOFMEMORY;
378
379 hres = SafeArrayAccessData(arr, &data);
380 if(FAILED(hres)) {
381 SafeArrayDestroy(arr);
382 return hres;
383 }
384
385 memcpy(data, pv, cb);
386
387 hres = SafeArrayUnaccessData(arr);
388 if(FAILED(hres)) {
389 SafeArrayDestroy(arr);
390 return hres;
391 }
392
393 V_VT(pvar) = VT_ARRAY|VT_UI1;
394 V_ARRAY(pvar) = arr;
395 return S_OK;
396 }
397
398 static inline DWORD PROPVAR_HexToNum(const WCHAR *hex)
399 {
400 DWORD ret;
401
402 if(hex[0]>='0' && hex[0]<='9')
403 ret = hex[0]-'0';
404 else if(hex[0]>='a' && hex[0]<='f')
405 ret = hex[0]-'a'+10;
406 else if(hex[0]>='A' && hex[0]<='F')
407 ret = hex[0]-'A'+10;
408 else
409 return -1;
410
411 ret <<= 4;
412 if(hex[1]>='0' && hex[1]<='9')
413 return ret + hex[1]-'0';
414 else if(hex[1]>='a' && hex[1]<='f')
415 return ret + hex[1]-'a'+10;
416 else if(hex[1]>='A' && hex[1]<='F')
417 return ret + hex[1]-'A'+10;
418 else
419 return -1;
420 }
421
422 static inline HRESULT PROPVAR_WCHARToGUID(const WCHAR *str, int len, GUID *guid)
423 {
424 DWORD i, val=0;
425 const WCHAR *p;
426
427 memset(guid, 0, sizeof(GUID));
428
429 if(len!=38 || str[0]!='{' || str[9]!='-' || str[14]!='-'
430 || str[19]!='-' || str[24]!='-' || str[37]!='}') {
431 WARN("Error parsing %s\n", debugstr_w(str));
432 return E_INVALIDARG;
433 }
434
435 p = str+1;
436 for(i=0; i<4 && val!=-1; i++) {
437 val = PROPVAR_HexToNum(p);
438 guid->Data1 = (guid->Data1<<8) + val;
439 p += 2;
440 }
441 p++;
442 for(i=0; i<2 && val!=-1; i++) {
443 val = PROPVAR_HexToNum(p);
444 guid->Data2 = (guid->Data2<<8) + val;
445 p += 2;
446 }
447 p++;
448 for(i=0; i<2 && val!=-1; i++) {
449 val = PROPVAR_HexToNum(p);
450 guid->Data3 = (guid->Data3<<8) + val;
451 p += 2;
452 }
453 p++;
454 for(i=0; i<8 && val!=-1; i++) {
455 if(i == 2)
456 p++;
457
458 val = guid->Data4[i] = PROPVAR_HexToNum(p);
459 p += 2;
460 }
461
462 if(val == -1) {
463 WARN("Error parsing %s\n", debugstr_w(str));
464 memset(guid, 0, sizeof(GUID));
465 return E_INVALIDARG;
466 }
467 return S_OK;
468 }
469
470 HRESULT WINAPI PropVariantToGUID(const PROPVARIANT *ppropvar, GUID *guid)
471 {
472 TRACE("%p %p)\n", ppropvar, guid);
473
474 switch(ppropvar->vt) {
475 case VT_BSTR:
476 return PROPVAR_WCHARToGUID(ppropvar->u.bstrVal, SysStringLen(ppropvar->u.bstrVal), guid);
477 case VT_LPWSTR:
478 return PROPVAR_WCHARToGUID(ppropvar->u.pwszVal, strlenW(ppropvar->u.pwszVal), guid);
479
480 default:
481 FIXME("unsupported vt: %d\n", ppropvar->vt);
482 return E_NOTIMPL;
483 }
484 }
485
486 HRESULT WINAPI VariantToGUID(const VARIANT *pvar, GUID *guid)
487 {
488 TRACE("(%p %p)\n", pvar, guid);
489
490 switch(V_VT(pvar)) {
491 case VT_BSTR: {
492 HRESULT hres = PROPVAR_WCHARToGUID(V_BSTR(pvar), SysStringLen(V_BSTR(pvar)), guid);
493 if(hres == E_INVALIDARG)
494 return E_FAIL;
495 return hres;
496 }
497
498 default:
499 FIXME("unsupported vt: %d\n", V_VT(pvar));
500 return E_NOTIMPL;
501 }
502 }
503
504 static int isemptyornull(const PROPVARIANT *propvar)
505 {
506 if (propvar->vt == VT_EMPTY || propvar->vt == VT_NULL)
507 return 1;
508 if ((propvar->vt & VT_ARRAY) == VT_ARRAY)
509 {
510 int i;
511 for (i=0; i<propvar->u.parray->cDims; i++)
512 {
513 if (propvar->u.parray->rgsabound[i].cElements != 0)
514 break;
515 }
516 return i == propvar->u.parray->cDims;
517 }
518 /* FIXME: vectors, byrefs, errors? */
519 return 0;
520 }
521
522 INT WINAPI PropVariantCompareEx(REFPROPVARIANT propvar1, REFPROPVARIANT propvar2,
523 PROPVAR_COMPARE_UNIT unit, PROPVAR_COMPARE_FLAGS flags)
524 {
525 const PROPVARIANT *propvar2_converted;
526 PROPVARIANT propvar2_static;
527 HRESULT hr;
528 INT res=-1;
529
530 TRACE("%p,%p,%x,%x\n", propvar1, propvar2, unit, flags);
531
532 if (isemptyornull(propvar1))
533 {
534 if (isemptyornull(propvar2))
535 return 0;
536 return (flags & PVCF_TREATEMPTYASGREATERTHAN) ? 1 : -1;
537 }
538
539 if (isemptyornull(propvar2))
540 return (flags & PVCF_TREATEMPTYASGREATERTHAN) ? -1 : 1;
541
542 if (propvar1->vt != propvar2->vt)
543 {
544 hr = PropVariantChangeType(&propvar2_static, propvar2, 0, propvar1->vt);
545
546 if (FAILED(hr))
547 return -1;
548
549 propvar2_converted = &propvar2_static;
550 }
551 else
552 propvar2_converted = propvar2;
553
554 #define CMP_INT_VALUE(var) do { \
555 if (propvar1->u.var > propvar2_converted->u.var) \
556 res = 1; \
557 else if (propvar1->u.var < propvar2_converted->u.var) \
558 res = -1; \
559 else \
560 res = 0; \
561 } while (0)
562
563 switch (propvar1->vt)
564 {
565 case VT_I1:
566 CMP_INT_VALUE(cVal);
567 break;
568 case VT_UI1:
569 CMP_INT_VALUE(bVal);
570 break;
571 case VT_I2:
572 CMP_INT_VALUE(iVal);
573 break;
574 case VT_UI2:
575 CMP_INT_VALUE(uiVal);
576 break;
577 case VT_I4:
578 CMP_INT_VALUE(lVal);
579 break;
580 case VT_UI4:
581 CMP_INT_VALUE(uiVal);
582 break;
583 case VT_I8:
584 CMP_INT_VALUE(hVal.QuadPart);
585 break;
586 case VT_UI8:
587 CMP_INT_VALUE(uhVal.QuadPart);
588 break;
589 case VT_BSTR:
590 /* FIXME: Use string flags. */
591 res = lstrcmpW(propvar1->u.bstrVal, propvar2->u.bstrVal);
592 break;
593 default:
594 FIXME("vartype %d not handled\n", propvar1->vt);
595 res = -1;
596 break;
597 }
598
599 if (propvar2_converted == &propvar2_static)
600 PropVariantClear(&propvar2_static);
601
602 return res;
603 }