4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
7 * Copyright 2006 Google (Benjamin Arai)
9 * The algorithm for conversion from Julian days to day/month/year is based on
10 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
11 * Copyright 1994-7 Regents of the University of California
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
40 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(variant
);
48 static const char * const variant_types
[] =
50 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
51 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
52 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
53 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
54 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
55 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
56 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
57 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
58 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID",
62 static const char * const variant_flags
[16] =
67 "|VT_VECTOR|VT_ARRAY",
69 "|VT_VECTOR|VT_ARRAY",
71 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
73 "|VT_VECTOR|VT_RESERVED",
74 "|VT_ARRAY|VT_RESERVED",
75 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
76 "|VT_BYREF|VT_RESERVED",
77 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
78 "|VT_ARRAY|VT_BYREF|VT_RESERVED",
79 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_RESERVED",
82 const char *debugstr_vt(VARTYPE vt
)
85 return wine_dbg_sprintf("%s%s", debugstr_vt(vt
&VT_TYPEMASK
), variant_flags
[vt
>>12]);
87 if(vt
< sizeof(variant_types
)/sizeof(*variant_types
))
88 return variant_types
[vt
];
90 if(vt
== VT_BSTR_BLOB
)
91 return "VT_BSTR_BLOB";
93 return wine_dbg_sprintf("vt(invalid %x)", vt
);
96 const char *debugstr_variant(const VARIANT
*v
)
101 if(V_VT(v
) & VT_BYREF
) {
102 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
103 return wine_dbg_sprintf("%p {VT_VARIANT|VT_BYREF: %s}", v
, debugstr_variant(V_VARIANTREF(v
)));
104 return wine_dbg_sprintf("%p {%s %p}", v
, debugstr_vt(V_VT(v
)), V_BYREF(v
));
107 if(V_ISARRAY(v
) || V_ISVECTOR(v
))
108 return wine_dbg_sprintf("%p {%s %p}", v
, debugstr_vt(V_VT(v
)), V_ARRAY(v
));
112 return wine_dbg_sprintf("%p {VT_EMPTY}", v
);
114 return wine_dbg_sprintf("%p {VT_NULL}", v
);
116 return wine_dbg_sprintf("%p {VT_I2: %d}", v
, V_I2(v
));
118 return wine_dbg_sprintf("%p {VT_I4: %d}", v
, V_I4(v
));
120 return wine_dbg_sprintf("%p {VT_R4: %f}", v
, V_R4(v
));
122 return wine_dbg_sprintf("%p {VT_R8: %lf}", v
, V_R8(v
));
124 return wine_dbg_sprintf("%p {VT_CY: %s}", v
, wine_dbgstr_longlong(V_CY(v
).int64
));
126 return wine_dbg_sprintf("%p {VT_DATE: %lf}", v
, V_DATE(v
));
128 return wine_dbg_sprintf("%p {VT_BSTR: %s}", v
, debugstr_w(V_BSTR(v
)));
130 return wine_dbg_sprintf("%p {VT_DISPATCH: %p}", v
, V_DISPATCH(v
));
132 return wine_dbg_sprintf("%p {VT_ERROR: %08x}", v
, V_ERROR(v
));
134 return wine_dbg_sprintf("%p {VT_BOOL: %x}", v
, V_BOOL(v
));
136 return wine_dbg_sprintf("%p {VT_UNKNOWN: %p}", v
, V_UNKNOWN(v
));
138 return wine_dbg_sprintf("%p {VT_I1: %d}", v
, V_I1(v
));
140 return wine_dbg_sprintf("%p {VT_UI1: %u}", v
, V_UI1(v
));
142 return wine_dbg_sprintf("%p {VT_UI2: %d}", v
, V_UI2(v
));
144 return wine_dbg_sprintf("%p {VT_UI4: %d}", v
, V_UI4(v
));
146 return wine_dbg_sprintf("%p {VT_I8: %s}", v
, wine_dbgstr_longlong(V_I8(v
)));
148 return wine_dbg_sprintf("%p {VT_UI8: %s}", v
, wine_dbgstr_longlong(V_UI8(v
)));
150 return wine_dbg_sprintf("%p {VT_INT: %d}", v
, V_INT(v
));
152 return wine_dbg_sprintf("%p {VT_UINT: %u}", v
, V_UINT(v
));
154 return wine_dbg_sprintf("%p {VT_VOID}", v
);
156 return wine_dbg_sprintf("%p {VT_RECORD: %p %p}", v
, V_UNION(v
,brecVal
).pvRecord
, V_UNION(v
,brecVal
).pRecInfo
);
158 return wine_dbg_sprintf("%p {vt %s}", v
, debugstr_vt(V_VT(v
)));
162 /* Convert a variant from one type to another */
163 static inline HRESULT
VARIANT_Coerce(VARIANTARG
* pd
, LCID lcid
, USHORT wFlags
,
164 VARIANTARG
* ps
, VARTYPE vt
)
166 HRESULT res
= DISP_E_TYPEMISMATCH
;
167 VARTYPE vtFrom
= V_TYPE(ps
);
170 TRACE("(%s,0x%08x,0x%04x,%s,%s)\n", debugstr_variant(pd
), lcid
, wFlags
,
171 debugstr_variant(ps
), debugstr_vt(vt
));
173 if (vt
== VT_BSTR
|| vtFrom
== VT_BSTR
)
175 /* All flags passed to low level function are only used for
176 * changing to or from strings. Map these here.
178 if (wFlags
& VARIANT_LOCALBOOL
)
179 dwFlags
|= VAR_LOCALBOOL
;
180 if (wFlags
& VARIANT_CALENDAR_HIJRI
)
181 dwFlags
|= VAR_CALENDAR_HIJRI
;
182 if (wFlags
& VARIANT_CALENDAR_THAI
)
183 dwFlags
|= VAR_CALENDAR_THAI
;
184 if (wFlags
& VARIANT_CALENDAR_GREGORIAN
)
185 dwFlags
|= VAR_CALENDAR_GREGORIAN
;
186 if (wFlags
& VARIANT_NOUSEROVERRIDE
)
187 dwFlags
|= LOCALE_NOUSEROVERRIDE
;
188 if (wFlags
& VARIANT_USE_NLS
)
189 dwFlags
|= LOCALE_USE_NLS
;
192 /* Map int/uint to i4/ui4 */
195 else if (vt
== VT_UINT
)
198 if (vtFrom
== VT_INT
)
200 else if (vtFrom
== VT_UINT
)
204 return VariantCopy(pd
, ps
);
206 if (wFlags
& VARIANT_NOVALUEPROP
&& vtFrom
== VT_DISPATCH
&& vt
!= VT_UNKNOWN
)
208 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
209 * accessing the default object property.
211 return DISP_E_TYPEMISMATCH
;
217 if (vtFrom
== VT_NULL
)
218 return DISP_E_TYPEMISMATCH
;
219 /* ... Fall through */
221 if (vtFrom
<= VT_UINT
&& vtFrom
!= (VARTYPE
)15 && vtFrom
!= VT_ERROR
)
223 res
= VariantClear( pd
);
224 if (vt
== VT_NULL
&& SUCCEEDED(res
))
232 case VT_EMPTY
: V_I1(pd
) = 0; return S_OK
;
233 case VT_I2
: return VarI1FromI2(V_I2(ps
), &V_I1(pd
));
234 case VT_I4
: return VarI1FromI4(V_I4(ps
), &V_I1(pd
));
235 case VT_UI1
: V_I1(pd
) = V_UI1(ps
); return S_OK
;
236 case VT_UI2
: return VarI1FromUI2(V_UI2(ps
), &V_I1(pd
));
237 case VT_UI4
: return VarI1FromUI4(V_UI4(ps
), &V_I1(pd
));
238 case VT_I8
: return VarI1FromI8(V_I8(ps
), &V_I1(pd
));
239 case VT_UI8
: return VarI1FromUI8(V_UI8(ps
), &V_I1(pd
));
240 case VT_R4
: return VarI1FromR4(V_R4(ps
), &V_I1(pd
));
241 case VT_R8
: return VarI1FromR8(V_R8(ps
), &V_I1(pd
));
242 case VT_DATE
: return VarI1FromDate(V_DATE(ps
), &V_I1(pd
));
243 case VT_BOOL
: return VarI1FromBool(V_BOOL(ps
), &V_I1(pd
));
244 case VT_CY
: return VarI1FromCy(V_CY(ps
), &V_I1(pd
));
245 case VT_DECIMAL
: return VarI1FromDec(&V_DECIMAL(ps
), &V_I1(pd
) );
246 case VT_DISPATCH
: return VarI1FromDisp(V_DISPATCH(ps
), lcid
, &V_I1(pd
) );
247 case VT_BSTR
: return VarI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I1(pd
) );
254 case VT_EMPTY
: V_I2(pd
) = 0; return S_OK
;
255 case VT_I1
: return VarI2FromI1(V_I1(ps
), &V_I2(pd
));
256 case VT_I4
: return VarI2FromI4(V_I4(ps
), &V_I2(pd
));
257 case VT_UI1
: return VarI2FromUI1(V_UI1(ps
), &V_I2(pd
));
258 case VT_UI2
: V_I2(pd
) = V_UI2(ps
); return S_OK
;
259 case VT_UI4
: return VarI2FromUI4(V_UI4(ps
), &V_I2(pd
));
260 case VT_I8
: return VarI2FromI8(V_I8(ps
), &V_I2(pd
));
261 case VT_UI8
: return VarI2FromUI8(V_UI8(ps
), &V_I2(pd
));
262 case VT_R4
: return VarI2FromR4(V_R4(ps
), &V_I2(pd
));
263 case VT_R8
: return VarI2FromR8(V_R8(ps
), &V_I2(pd
));
264 case VT_DATE
: return VarI2FromDate(V_DATE(ps
), &V_I2(pd
));
265 case VT_BOOL
: return VarI2FromBool(V_BOOL(ps
), &V_I2(pd
));
266 case VT_CY
: return VarI2FromCy(V_CY(ps
), &V_I2(pd
));
267 case VT_DECIMAL
: return VarI2FromDec(&V_DECIMAL(ps
), &V_I2(pd
));
268 case VT_DISPATCH
: return VarI2FromDisp(V_DISPATCH(ps
), lcid
, &V_I2(pd
));
269 case VT_BSTR
: return VarI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I2(pd
));
276 case VT_EMPTY
: V_I4(pd
) = 0; return S_OK
;
277 case VT_I1
: return VarI4FromI1(V_I1(ps
), &V_I4(pd
));
278 case VT_I2
: return VarI4FromI2(V_I2(ps
), &V_I4(pd
));
279 case VT_UI1
: return VarI4FromUI1(V_UI1(ps
), &V_I4(pd
));
280 case VT_UI2
: return VarI4FromUI2(V_UI2(ps
), &V_I4(pd
));
281 case VT_UI4
: V_I4(pd
) = V_UI4(ps
); return S_OK
;
282 case VT_I8
: return VarI4FromI8(V_I8(ps
), &V_I4(pd
));
283 case VT_UI8
: return VarI4FromUI8(V_UI8(ps
), &V_I4(pd
));
284 case VT_R4
: return VarI4FromR4(V_R4(ps
), &V_I4(pd
));
285 case VT_R8
: return VarI4FromR8(V_R8(ps
), &V_I4(pd
));
286 case VT_DATE
: return VarI4FromDate(V_DATE(ps
), &V_I4(pd
));
287 case VT_BOOL
: return VarI4FromBool(V_BOOL(ps
), &V_I4(pd
));
288 case VT_CY
: return VarI4FromCy(V_CY(ps
), &V_I4(pd
));
289 case VT_DECIMAL
: return VarI4FromDec(&V_DECIMAL(ps
), &V_I4(pd
));
290 case VT_DISPATCH
: return VarI4FromDisp(V_DISPATCH(ps
), lcid
, &V_I4(pd
));
291 case VT_BSTR
: return VarI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I4(pd
));
298 case VT_EMPTY
: V_UI1(pd
) = 0; return S_OK
;
299 case VT_I1
: V_UI1(pd
) = V_I1(ps
); return S_OK
;
300 case VT_I2
: return VarUI1FromI2(V_I2(ps
), &V_UI1(pd
));
301 case VT_I4
: return VarUI1FromI4(V_I4(ps
), &V_UI1(pd
));
302 case VT_UI2
: return VarUI1FromUI2(V_UI2(ps
), &V_UI1(pd
));
303 case VT_UI4
: return VarUI1FromUI4(V_UI4(ps
), &V_UI1(pd
));
304 case VT_I8
: return VarUI1FromI8(V_I8(ps
), &V_UI1(pd
));
305 case VT_UI8
: return VarUI1FromUI8(V_UI8(ps
), &V_UI1(pd
));
306 case VT_R4
: return VarUI1FromR4(V_R4(ps
), &V_UI1(pd
));
307 case VT_R8
: return VarUI1FromR8(V_R8(ps
), &V_UI1(pd
));
308 case VT_DATE
: return VarUI1FromDate(V_DATE(ps
), &V_UI1(pd
));
309 case VT_BOOL
: return VarUI1FromBool(V_BOOL(ps
), &V_UI1(pd
));
310 case VT_CY
: return VarUI1FromCy(V_CY(ps
), &V_UI1(pd
));
311 case VT_DECIMAL
: return VarUI1FromDec(&V_DECIMAL(ps
), &V_UI1(pd
));
312 case VT_DISPATCH
: return VarUI1FromDisp(V_DISPATCH(ps
), lcid
, &V_UI1(pd
));
313 case VT_BSTR
: return VarUI1FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI1(pd
));
320 case VT_EMPTY
: V_UI2(pd
) = 0; return S_OK
;
321 case VT_I1
: return VarUI2FromI1(V_I1(ps
), &V_UI2(pd
));
322 case VT_I2
: V_UI2(pd
) = V_I2(ps
); return S_OK
;
323 case VT_I4
: return VarUI2FromI4(V_I4(ps
), &V_UI2(pd
));
324 case VT_UI1
: return VarUI2FromUI1(V_UI1(ps
), &V_UI2(pd
));
325 case VT_UI4
: return VarUI2FromUI4(V_UI4(ps
), &V_UI2(pd
));
326 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
327 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
328 case VT_R4
: return VarUI2FromR4(V_R4(ps
), &V_UI2(pd
));
329 case VT_R8
: return VarUI2FromR8(V_R8(ps
), &V_UI2(pd
));
330 case VT_DATE
: return VarUI2FromDate(V_DATE(ps
), &V_UI2(pd
));
331 case VT_BOOL
: return VarUI2FromBool(V_BOOL(ps
), &V_UI2(pd
));
332 case VT_CY
: return VarUI2FromCy(V_CY(ps
), &V_UI2(pd
));
333 case VT_DECIMAL
: return VarUI2FromDec(&V_DECIMAL(ps
), &V_UI2(pd
));
334 case VT_DISPATCH
: return VarUI2FromDisp(V_DISPATCH(ps
), lcid
, &V_UI2(pd
));
335 case VT_BSTR
: return VarUI2FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI2(pd
));
342 case VT_EMPTY
: V_UI4(pd
) = 0; return S_OK
;
343 case VT_I1
: return VarUI4FromI1(V_I1(ps
), &V_UI4(pd
));
344 case VT_I2
: return VarUI4FromI2(V_I2(ps
), &V_UI4(pd
));
345 case VT_I4
: V_UI4(pd
) = V_I4(ps
); return S_OK
;
346 case VT_UI1
: return VarUI4FromUI1(V_UI1(ps
), &V_UI4(pd
));
347 case VT_UI2
: return VarUI4FromUI2(V_UI2(ps
), &V_UI4(pd
));
348 case VT_I8
: return VarUI4FromI8(V_I8(ps
), &V_UI4(pd
));
349 case VT_UI8
: return VarUI4FromUI8(V_UI8(ps
), &V_UI4(pd
));
350 case VT_R4
: return VarUI4FromR4(V_R4(ps
), &V_UI4(pd
));
351 case VT_R8
: return VarUI4FromR8(V_R8(ps
), &V_UI4(pd
));
352 case VT_DATE
: return VarUI4FromDate(V_DATE(ps
), &V_UI4(pd
));
353 case VT_BOOL
: return VarUI4FromBool(V_BOOL(ps
), &V_UI4(pd
));
354 case VT_CY
: return VarUI4FromCy(V_CY(ps
), &V_UI4(pd
));
355 case VT_DECIMAL
: return VarUI4FromDec(&V_DECIMAL(ps
), &V_UI4(pd
));
356 case VT_DISPATCH
: return VarUI4FromDisp(V_DISPATCH(ps
), lcid
, &V_UI4(pd
));
357 case VT_BSTR
: return VarUI4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI4(pd
));
364 case VT_EMPTY
: V_UI8(pd
) = 0; return S_OK
;
365 case VT_I4
: if (V_I4(ps
) < 0) return DISP_E_OVERFLOW
; V_UI8(pd
) = V_I4(ps
); return S_OK
;
366 case VT_I1
: return VarUI8FromI1(V_I1(ps
), &V_UI8(pd
));
367 case VT_I2
: return VarUI8FromI2(V_I2(ps
), &V_UI8(pd
));
368 case VT_UI1
: return VarUI8FromUI1(V_UI1(ps
), &V_UI8(pd
));
369 case VT_UI2
: return VarUI8FromUI2(V_UI2(ps
), &V_UI8(pd
));
370 case VT_UI4
: return VarUI8FromUI4(V_UI4(ps
), &V_UI8(pd
));
371 case VT_I8
: V_UI8(pd
) = V_I8(ps
); return S_OK
;
372 case VT_R4
: return VarUI8FromR4(V_R4(ps
), &V_UI8(pd
));
373 case VT_R8
: return VarUI8FromR8(V_R8(ps
), &V_UI8(pd
));
374 case VT_DATE
: return VarUI8FromDate(V_DATE(ps
), &V_UI8(pd
));
375 case VT_BOOL
: return VarUI8FromBool(V_BOOL(ps
), &V_UI8(pd
));
376 case VT_CY
: return VarUI8FromCy(V_CY(ps
), &V_UI8(pd
));
377 case VT_DECIMAL
: return VarUI8FromDec(&V_DECIMAL(ps
), &V_UI8(pd
));
378 case VT_DISPATCH
: return VarUI8FromDisp(V_DISPATCH(ps
), lcid
, &V_UI8(pd
));
379 case VT_BSTR
: return VarUI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_UI8(pd
));
386 case VT_EMPTY
: V_I8(pd
) = 0; return S_OK
;
387 case VT_I4
: V_I8(pd
) = V_I4(ps
); return S_OK
;
388 case VT_I1
: return VarI8FromI1(V_I1(ps
), &V_I8(pd
));
389 case VT_I2
: return VarI8FromI2(V_I2(ps
), &V_I8(pd
));
390 case VT_UI1
: return VarI8FromUI1(V_UI1(ps
), &V_I8(pd
));
391 case VT_UI2
: return VarI8FromUI2(V_UI2(ps
), &V_I8(pd
));
392 case VT_UI4
: return VarI8FromUI4(V_UI4(ps
), &V_I8(pd
));
393 case VT_UI8
: V_I8(pd
) = V_UI8(ps
); return S_OK
;
394 case VT_R4
: return VarI8FromR4(V_R4(ps
), &V_I8(pd
));
395 case VT_R8
: return VarI8FromR8(V_R8(ps
), &V_I8(pd
));
396 case VT_DATE
: return VarI8FromDate(V_DATE(ps
), &V_I8(pd
));
397 case VT_BOOL
: return VarI8FromBool(V_BOOL(ps
), &V_I8(pd
));
398 case VT_CY
: return VarI8FromCy(V_CY(ps
), &V_I8(pd
));
399 case VT_DECIMAL
: return VarI8FromDec(&V_DECIMAL(ps
), &V_I8(pd
));
400 case VT_DISPATCH
: return VarI8FromDisp(V_DISPATCH(ps
), lcid
, &V_I8(pd
));
401 case VT_BSTR
: return VarI8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_I8(pd
));
408 case VT_EMPTY
: V_R4(pd
) = 0.0f
; return S_OK
;
409 case VT_I1
: return VarR4FromI1(V_I1(ps
), &V_R4(pd
));
410 case VT_I2
: return VarR4FromI2(V_I2(ps
), &V_R4(pd
));
411 case VT_I4
: return VarR4FromI4(V_I4(ps
), &V_R4(pd
));
412 case VT_UI1
: return VarR4FromUI1(V_UI1(ps
), &V_R4(pd
));
413 case VT_UI2
: return VarR4FromUI2(V_UI2(ps
), &V_R4(pd
));
414 case VT_UI4
: return VarR4FromUI4(V_UI4(ps
), &V_R4(pd
));
415 case VT_I8
: return VarR4FromI8(V_I8(ps
), &V_R4(pd
));
416 case VT_UI8
: return VarR4FromUI8(V_UI8(ps
), &V_R4(pd
));
417 case VT_R8
: return VarR4FromR8(V_R8(ps
), &V_R4(pd
));
418 case VT_DATE
: return VarR4FromDate(V_DATE(ps
), &V_R4(pd
));
419 case VT_BOOL
: return VarR4FromBool(V_BOOL(ps
), &V_R4(pd
));
420 case VT_CY
: return VarR4FromCy(V_CY(ps
), &V_R4(pd
));
421 case VT_DECIMAL
: return VarR4FromDec(&V_DECIMAL(ps
), &V_R4(pd
));
422 case VT_DISPATCH
: return VarR4FromDisp(V_DISPATCH(ps
), lcid
, &V_R4(pd
));
423 case VT_BSTR
: return VarR4FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R4(pd
));
430 case VT_EMPTY
: V_R8(pd
) = 0.0; return S_OK
;
431 case VT_I1
: return VarR8FromI1(V_I1(ps
), &V_R8(pd
));
432 case VT_I2
: return VarR8FromI2(V_I2(ps
), &V_R8(pd
));
433 case VT_I4
: return VarR8FromI4(V_I4(ps
), &V_R8(pd
));
434 case VT_UI1
: return VarR8FromUI1(V_UI1(ps
), &V_R8(pd
));
435 case VT_UI2
: return VarR8FromUI2(V_UI2(ps
), &V_R8(pd
));
436 case VT_UI4
: return VarR8FromUI4(V_UI4(ps
), &V_R8(pd
));
437 case VT_I8
: return VarR8FromI8(V_I8(ps
), &V_R8(pd
));
438 case VT_UI8
: return VarR8FromUI8(V_UI8(ps
), &V_R8(pd
));
439 case VT_R4
: return VarR8FromR4(V_R4(ps
), &V_R8(pd
));
440 case VT_DATE
: return VarR8FromDate(V_DATE(ps
), &V_R8(pd
));
441 case VT_BOOL
: return VarR8FromBool(V_BOOL(ps
), &V_R8(pd
));
442 case VT_CY
: return VarR8FromCy(V_CY(ps
), &V_R8(pd
));
443 case VT_DECIMAL
: return VarR8FromDec(&V_DECIMAL(ps
), &V_R8(pd
));
444 case VT_DISPATCH
: return VarR8FromDisp(V_DISPATCH(ps
), lcid
, &V_R8(pd
));
445 case VT_BSTR
: return VarR8FromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_R8(pd
));
452 case VT_EMPTY
: V_DATE(pd
) = 0.0; return S_OK
;
453 case VT_I1
: return VarDateFromI1(V_I1(ps
), &V_DATE(pd
));
454 case VT_I2
: return VarDateFromI2(V_I2(ps
), &V_DATE(pd
));
455 case VT_I4
: return VarDateFromI4(V_I4(ps
), &V_DATE(pd
));
456 case VT_UI1
: return VarDateFromUI1(V_UI1(ps
), &V_DATE(pd
));
457 case VT_UI2
: return VarDateFromUI2(V_UI2(ps
), &V_DATE(pd
));
458 case VT_UI4
: return VarDateFromUI4(V_UI4(ps
), &V_DATE(pd
));
459 case VT_I8
: return VarDateFromI8(V_I8(ps
), &V_DATE(pd
));
460 case VT_UI8
: return VarDateFromUI8(V_UI8(ps
), &V_DATE(pd
));
461 case VT_R4
: return VarDateFromR4(V_R4(ps
), &V_DATE(pd
));
462 case VT_R8
: return VarDateFromR8(V_R8(ps
), &V_DATE(pd
));
463 case VT_BOOL
: return VarDateFromBool(V_BOOL(ps
), &V_DATE(pd
));
464 case VT_CY
: return VarDateFromCy(V_CY(ps
), &V_DATE(pd
));
465 case VT_DECIMAL
: return VarDateFromDec(&V_DECIMAL(ps
), &V_DATE(pd
));
466 case VT_DISPATCH
: return VarDateFromDisp(V_DISPATCH(ps
), lcid
, &V_DATE(pd
));
467 case VT_BSTR
: return VarDateFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DATE(pd
));
474 case VT_EMPTY
: V_BOOL(pd
) = 0; return S_OK
;
475 case VT_I1
: return VarBoolFromI1(V_I1(ps
), &V_BOOL(pd
));
476 case VT_I2
: return VarBoolFromI2(V_I2(ps
), &V_BOOL(pd
));
477 case VT_I4
: return VarBoolFromI4(V_I4(ps
), &V_BOOL(pd
));
478 case VT_UI1
: return VarBoolFromUI1(V_UI1(ps
), &V_BOOL(pd
));
479 case VT_UI2
: return VarBoolFromUI2(V_UI2(ps
), &V_BOOL(pd
));
480 case VT_UI4
: return VarBoolFromUI4(V_UI4(ps
), &V_BOOL(pd
));
481 case VT_I8
: return VarBoolFromI8(V_I8(ps
), &V_BOOL(pd
));
482 case VT_UI8
: return VarBoolFromUI8(V_UI8(ps
), &V_BOOL(pd
));
483 case VT_R4
: return VarBoolFromR4(V_R4(ps
), &V_BOOL(pd
));
484 case VT_R8
: return VarBoolFromR8(V_R8(ps
), &V_BOOL(pd
));
485 case VT_DATE
: return VarBoolFromDate(V_DATE(ps
), &V_BOOL(pd
));
486 case VT_CY
: return VarBoolFromCy(V_CY(ps
), &V_BOOL(pd
));
487 case VT_DECIMAL
: return VarBoolFromDec(&V_DECIMAL(ps
), &V_BOOL(pd
));
488 case VT_DISPATCH
: return VarBoolFromDisp(V_DISPATCH(ps
), lcid
, &V_BOOL(pd
));
489 case VT_BSTR
: return VarBoolFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_BOOL(pd
));
497 V_BSTR(pd
) = SysAllocStringLen(NULL
, 0);
498 return V_BSTR(pd
) ? S_OK
: E_OUTOFMEMORY
;
500 if (wFlags
& (VARIANT_ALPHABOOL
|VARIANT_LOCALBOOL
))
501 return VarBstrFromBool(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
502 return VarBstrFromI2(V_BOOL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
503 case VT_I1
: return VarBstrFromI1(V_I1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
504 case VT_I2
: return VarBstrFromI2(V_I2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
505 case VT_I4
: return VarBstrFromI4(V_I4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
506 case VT_UI1
: return VarBstrFromUI1(V_UI1(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
507 case VT_UI2
: return VarBstrFromUI2(V_UI2(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
508 case VT_UI4
: return VarBstrFromUI4(V_UI4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
509 case VT_I8
: return VarBstrFromI8(V_I8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
510 case VT_UI8
: return VarBstrFromUI8(V_UI8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
511 case VT_R4
: return VarBstrFromR4(V_R4(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
512 case VT_R8
: return VarBstrFromR8(V_R8(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
513 case VT_DATE
: return VarBstrFromDate(V_DATE(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
514 case VT_CY
: return VarBstrFromCy(V_CY(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
515 case VT_DECIMAL
: return VarBstrFromDec(&V_DECIMAL(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
516 case VT_DISPATCH
: return VarBstrFromDisp(V_DISPATCH(ps
), lcid
, dwFlags
, &V_BSTR(pd
));
523 case VT_EMPTY
: V_CY(pd
).int64
= 0; return S_OK
;
524 case VT_I1
: return VarCyFromI1(V_I1(ps
), &V_CY(pd
));
525 case VT_I2
: return VarCyFromI2(V_I2(ps
), &V_CY(pd
));
526 case VT_I4
: return VarCyFromI4(V_I4(ps
), &V_CY(pd
));
527 case VT_UI1
: return VarCyFromUI1(V_UI1(ps
), &V_CY(pd
));
528 case VT_UI2
: return VarCyFromUI2(V_UI2(ps
), &V_CY(pd
));
529 case VT_UI4
: return VarCyFromUI4(V_UI4(ps
), &V_CY(pd
));
530 case VT_I8
: return VarCyFromI8(V_I8(ps
), &V_CY(pd
));
531 case VT_UI8
: return VarCyFromUI8(V_UI8(ps
), &V_CY(pd
));
532 case VT_R4
: return VarCyFromR4(V_R4(ps
), &V_CY(pd
));
533 case VT_R8
: return VarCyFromR8(V_R8(ps
), &V_CY(pd
));
534 case VT_DATE
: return VarCyFromDate(V_DATE(ps
), &V_CY(pd
));
535 case VT_BOOL
: return VarCyFromBool(V_BOOL(ps
), &V_CY(pd
));
536 case VT_DECIMAL
: return VarCyFromDec(&V_DECIMAL(ps
), &V_CY(pd
));
537 case VT_DISPATCH
: return VarCyFromDisp(V_DISPATCH(ps
), lcid
, &V_CY(pd
));
538 case VT_BSTR
: return VarCyFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_CY(pd
));
547 DEC_SIGNSCALE(&V_DECIMAL(pd
)) = SIGNSCALE(DECIMAL_POS
,0);
548 DEC_HI32(&V_DECIMAL(pd
)) = 0;
549 DEC_MID32(&V_DECIMAL(pd
)) = 0;
550 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
551 * VT_NULL and VT_EMPTY always give a 0 value.
553 DEC_LO32(&V_DECIMAL(pd
)) = vtFrom
== VT_BOOL
&& V_BOOL(ps
) ? 1 : 0;
555 case VT_I1
: return VarDecFromI1(V_I1(ps
), &V_DECIMAL(pd
));
556 case VT_I2
: return VarDecFromI2(V_I2(ps
), &V_DECIMAL(pd
));
557 case VT_I4
: return VarDecFromI4(V_I4(ps
), &V_DECIMAL(pd
));
558 case VT_UI1
: return VarDecFromUI1(V_UI1(ps
), &V_DECIMAL(pd
));
559 case VT_UI2
: return VarDecFromUI2(V_UI2(ps
), &V_DECIMAL(pd
));
560 case VT_UI4
: return VarDecFromUI4(V_UI4(ps
), &V_DECIMAL(pd
));
561 case VT_I8
: return VarDecFromI8(V_I8(ps
), &V_DECIMAL(pd
));
562 case VT_UI8
: return VarDecFromUI8(V_UI8(ps
), &V_DECIMAL(pd
));
563 case VT_R4
: return VarDecFromR4(V_R4(ps
), &V_DECIMAL(pd
));
564 case VT_R8
: return VarDecFromR8(V_R8(ps
), &V_DECIMAL(pd
));
565 case VT_DATE
: return VarDecFromDate(V_DATE(ps
), &V_DECIMAL(pd
));
566 case VT_CY
: return VarDecFromCy(V_CY(ps
), &V_DECIMAL(pd
));
567 case VT_DISPATCH
: return VarDecFromDisp(V_DISPATCH(ps
), lcid
, &V_DECIMAL(pd
));
568 case VT_BSTR
: return VarDecFromStr(V_BSTR(ps
), lcid
, dwFlags
, &V_DECIMAL(pd
));
576 if (V_DISPATCH(ps
) == NULL
)
577 V_UNKNOWN(pd
) = NULL
;
579 res
= IDispatch_QueryInterface(V_DISPATCH(ps
), &IID_IUnknown
, (LPVOID
*)&V_UNKNOWN(pd
));
588 if (V_UNKNOWN(ps
) == NULL
)
589 V_DISPATCH(pd
) = NULL
;
591 res
= IUnknown_QueryInterface(V_UNKNOWN(ps
), &IID_IDispatch
, (LPVOID
*)&V_DISPATCH(pd
));
602 /* Coerce to/from an array */
603 static inline HRESULT
VARIANT_CoerceArray(VARIANTARG
* pd
, VARIANTARG
* ps
, VARTYPE vt
)
605 if (vt
== VT_BSTR
&& V_VT(ps
) == (VT_ARRAY
|VT_UI1
))
606 return BstrFromVector(V_ARRAY(ps
), &V_BSTR(pd
));
608 if (V_VT(ps
) == VT_BSTR
&& vt
== (VT_ARRAY
|VT_UI1
))
609 return VectorFromBstr(V_BSTR(ps
), &V_ARRAY(pd
));
612 return SafeArrayCopy(V_ARRAY(ps
), &V_ARRAY(pd
));
614 return DISP_E_TYPEMISMATCH
;
617 /******************************************************************************
618 * Check if a variants type is valid.
620 static inline HRESULT
VARIANT_ValidateType(VARTYPE vt
)
622 VARTYPE vtExtra
= vt
& VT_EXTRA_TYPE
;
626 if (!(vtExtra
& (VT_VECTOR
|VT_RESERVED
)))
628 if (vt
< VT_VOID
|| vt
== VT_RECORD
|| vt
== VT_CLSID
)
630 if ((vtExtra
& (VT_BYREF
|VT_ARRAY
)) && vt
<= VT_NULL
)
631 return DISP_E_BADVARTYPE
;
632 if (vt
!= (VARTYPE
)15)
636 return DISP_E_BADVARTYPE
;
639 /******************************************************************************
640 * VariantInit [OLEAUT32.8]
642 * Initialise a variant.
645 * pVarg [O] Variant to initialise
651 * This function simply sets the type of the variant to VT_EMPTY. It does not
652 * free any existing value, use VariantClear() for that.
654 void WINAPI
VariantInit(VARIANTARG
* pVarg
)
656 TRACE("(%p)\n", pVarg
);
658 /* Win8.1 zeroes whole struct. Previous implementations don't set any other fields. */
659 V_VT(pVarg
) = VT_EMPTY
;
662 HRESULT
VARIANT_ClearInd(VARIANTARG
*pVarg
)
666 TRACE("(%s)\n", debugstr_variant(pVarg
));
668 hres
= VARIANT_ValidateType(V_VT(pVarg
));
676 if (V_UNKNOWN(pVarg
))
677 IUnknown_Release(V_UNKNOWN(pVarg
));
679 case VT_UNKNOWN
| VT_BYREF
:
680 case VT_DISPATCH
| VT_BYREF
:
681 if(*V_UNKNOWNREF(pVarg
))
682 IUnknown_Release(*V_UNKNOWNREF(pVarg
));
685 SysFreeString(V_BSTR(pVarg
));
687 case VT_BSTR
| VT_BYREF
:
688 SysFreeString(*V_BSTRREF(pVarg
));
690 case VT_VARIANT
| VT_BYREF
:
691 VariantClear(V_VARIANTREF(pVarg
));
694 case VT_RECORD
| VT_BYREF
:
696 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
699 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
700 IRecordInfo_Release(pBr
->pRecInfo
);
705 if (V_ISARRAY(pVarg
) || (V_VT(pVarg
) & ~VT_BYREF
) == VT_SAFEARRAY
)
707 if (V_ISBYREF(pVarg
))
709 if (*V_ARRAYREF(pVarg
))
710 hres
= SafeArrayDestroy(*V_ARRAYREF(pVarg
));
712 else if (V_ARRAY(pVarg
))
713 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
718 V_VT(pVarg
) = VT_EMPTY
;
722 /******************************************************************************
723 * VariantClear [OLEAUT32.9]
728 * pVarg [I/O] Variant to clear
731 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
732 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
734 HRESULT WINAPI
VariantClear(VARIANTARG
* pVarg
)
738 TRACE("(%s)\n", debugstr_variant(pVarg
));
740 hres
= VARIANT_ValidateType(V_VT(pVarg
));
744 if (!V_ISBYREF(pVarg
))
746 if (V_ISARRAY(pVarg
) || V_VT(pVarg
) == VT_SAFEARRAY
)
748 hres
= SafeArrayDestroy(V_ARRAY(pVarg
));
750 else if (V_VT(pVarg
) == VT_BSTR
)
752 SysFreeString(V_BSTR(pVarg
));
754 else if (V_VT(pVarg
) == VT_RECORD
)
756 struct __tagBRECORD
* pBr
= &V_UNION(pVarg
,brecVal
);
759 IRecordInfo_RecordClear(pBr
->pRecInfo
, pBr
->pvRecord
);
760 IRecordInfo_Release(pBr
->pRecInfo
);
763 else if (V_VT(pVarg
) == VT_DISPATCH
||
764 V_VT(pVarg
) == VT_UNKNOWN
)
766 if (V_UNKNOWN(pVarg
))
767 IUnknown_Release(V_UNKNOWN(pVarg
));
770 V_VT(pVarg
) = VT_EMPTY
;
775 /******************************************************************************
776 * Copy an IRecordInfo object contained in a variant.
778 static HRESULT
VARIANT_CopyIRecordInfo(VARIANT
*dest
, VARIANT
*src
)
780 struct __tagBRECORD
*dest_rec
= &V_UNION(dest
, brecVal
);
781 struct __tagBRECORD
*src_rec
= &V_UNION(src
, brecVal
);
785 if (!src_rec
->pRecInfo
)
787 if (src_rec
->pvRecord
) return E_INVALIDARG
;
791 hr
= IRecordInfo_GetSize(src_rec
->pRecInfo
, &size
);
792 if (FAILED(hr
)) return hr
;
794 /* This could look cleaner if only RecordCreate() was used, but native doesn't use it.
795 Memory should be allocated in a same way as RecordCreate() does, so RecordDestroy()
796 could free it later. */
797 dest_rec
->pvRecord
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
798 if (!dest_rec
->pvRecord
) return E_OUTOFMEMORY
;
800 dest_rec
->pRecInfo
= src_rec
->pRecInfo
;
801 IRecordInfo_AddRef(src_rec
->pRecInfo
);
803 return IRecordInfo_RecordCopy(src_rec
->pRecInfo
, src_rec
->pvRecord
, dest_rec
->pvRecord
);
806 /******************************************************************************
807 * VariantCopy [OLEAUT32.10]
812 * pvargDest [O] Destination for copy
813 * pvargSrc [I] Source variant to copy
816 * Success: S_OK. pvargDest contains a copy of pvargSrc.
817 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
818 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
819 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
820 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
823 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
824 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
825 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
826 * fails, so does this function.
827 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
828 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
829 * is copied rather than just any pointers to it.
830 * - For by-value object types the object pointer is copied and the objects
831 * reference count increased using IUnknown_AddRef().
832 * - For all by-reference types, only the referencing pointer is copied.
834 HRESULT WINAPI
VariantCopy(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
)
838 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
840 if (V_TYPE(pvargSrc
) == VT_CLSID
|| /* VT_CLSID is a special case */
841 FAILED(VARIANT_ValidateType(V_VT(pvargSrc
))))
842 return DISP_E_BADVARTYPE
;
844 if (pvargSrc
!= pvargDest
&&
845 SUCCEEDED(hres
= VariantClear(pvargDest
)))
847 *pvargDest
= *pvargSrc
; /* Shallow copy the value */
849 if (!V_ISBYREF(pvargSrc
))
851 switch (V_VT(pvargSrc
))
854 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc
), SysStringByteLen(V_BSTR(pvargSrc
)));
855 if (!V_BSTR(pvargDest
))
856 hres
= E_OUTOFMEMORY
;
859 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
863 V_UNKNOWN(pvargDest
) = V_UNKNOWN(pvargSrc
);
864 if (V_UNKNOWN(pvargSrc
))
865 IUnknown_AddRef(V_UNKNOWN(pvargSrc
));
868 if (V_ISARRAY(pvargSrc
))
869 hres
= SafeArrayCopy(V_ARRAY(pvargSrc
), &V_ARRAY(pvargDest
));
876 /* Return the byte size of a variants data */
877 static inline size_t VARIANT_DataSize(const VARIANT
* pv
)
882 case VT_UI1
: return sizeof(BYTE
);
884 case VT_UI2
: return sizeof(SHORT
);
888 case VT_UI4
: return sizeof(LONG
);
890 case VT_UI8
: return sizeof(LONGLONG
);
891 case VT_R4
: return sizeof(float);
892 case VT_R8
: return sizeof(double);
893 case VT_DATE
: return sizeof(DATE
);
894 case VT_BOOL
: return sizeof(VARIANT_BOOL
);
897 case VT_BSTR
: return sizeof(void*);
898 case VT_CY
: return sizeof(CY
);
899 case VT_ERROR
: return sizeof(SCODE
);
901 TRACE("Shouldn't be called for variant %s!\n", debugstr_variant(pv
));
905 /******************************************************************************
906 * VariantCopyInd [OLEAUT32.11]
908 * Copy a variant, dereferencing it if it is by-reference.
911 * pvargDest [O] Destination for copy
912 * pvargSrc [I] Source variant to copy
915 * Success: S_OK. pvargDest contains a copy of pvargSrc.
916 * Failure: An HRESULT error code indicating the error.
919 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
920 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
921 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
922 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
923 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
926 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
927 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
929 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
930 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
931 * to it. If clearing pvargDest fails, so does this function.
933 HRESULT WINAPI
VariantCopyInd(VARIANT
* pvargDest
, VARIANTARG
* pvargSrc
)
935 VARIANTARG vTmp
, *pSrc
= pvargSrc
;
939 TRACE("(%s,%s)\n", debugstr_variant(pvargDest
), debugstr_variant(pvargSrc
));
941 if (!V_ISBYREF(pvargSrc
))
942 return VariantCopy(pvargDest
, pvargSrc
);
944 /* Argument checking is more lax than VariantCopy()... */
945 vt
= V_TYPE(pvargSrc
);
946 if (V_ISARRAY(pvargSrc
) || (V_VT(pvargSrc
) == (VT_RECORD
|VT_BYREF
)) ||
947 (vt
> VT_NULL
&& vt
!= (VARTYPE
)15 && vt
< VT_VOID
&&
948 !(V_VT(pvargSrc
) & (VT_VECTOR
|VT_RESERVED
))))
953 return E_INVALIDARG
; /* ...And the return value for invalid types differs too */
955 if (pvargSrc
== pvargDest
)
957 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
958 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
962 V_VT(pvargDest
) = VT_EMPTY
;
966 /* Copy into another variant. Free the variant in pvargDest */
967 if (FAILED(hres
= VariantClear(pvargDest
)))
969 TRACE("VariantClear() of destination failed\n");
976 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
977 hres
= SafeArrayCopy(*V_ARRAYREF(pSrc
), &V_ARRAY(pvargDest
));
979 else if (V_VT(pSrc
) == (VT_BSTR
|VT_BYREF
))
981 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
982 V_BSTR(pvargDest
) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc
), SysStringByteLen(*V_BSTRREF(pSrc
)));
984 else if (V_VT(pSrc
) == (VT_RECORD
|VT_BYREF
))
986 hres
= VARIANT_CopyIRecordInfo(pvargDest
, pvargSrc
);
988 else if (V_VT(pSrc
) == (VT_DISPATCH
|VT_BYREF
) ||
989 V_VT(pSrc
) == (VT_UNKNOWN
|VT_BYREF
))
991 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
992 V_UNKNOWN(pvargDest
) = *V_UNKNOWNREF(pSrc
);
993 if (*V_UNKNOWNREF(pSrc
))
994 IUnknown_AddRef(*V_UNKNOWNREF(pSrc
));
996 else if (V_VT(pSrc
) == (VT_VARIANT
|VT_BYREF
))
998 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
999 if (V_VT(V_VARIANTREF(pSrc
)) == (VT_VARIANT
|VT_BYREF
))
1000 hres
= E_INVALIDARG
; /* Don't dereference more than one level */
1002 hres
= VariantCopyInd(pvargDest
, V_VARIANTREF(pSrc
));
1004 /* Use the dereferenced variants type value, not VT_VARIANT */
1005 goto VariantCopyInd_Return
;
1007 else if (V_VT(pSrc
) == (VT_DECIMAL
|VT_BYREF
))
1009 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest
)), &DEC_SCALE(V_DECIMALREF(pSrc
)),
1010 sizeof(DECIMAL
) - sizeof(USHORT
));
1014 /* Copy the pointed to data into this variant */
1015 memcpy(&V_BYREF(pvargDest
), V_BYREF(pSrc
), VARIANT_DataSize(pSrc
));
1018 V_VT(pvargDest
) = V_VT(pSrc
) & ~VT_BYREF
;
1020 VariantCopyInd_Return
:
1022 if (pSrc
!= pvargSrc
)
1025 TRACE("returning 0x%08x, %s\n", hres
, debugstr_variant(pvargDest
));
1029 /******************************************************************************
1030 * VariantChangeType [OLEAUT32.12]
1032 * Change the type of a variant.
1035 * pvargDest [O] Destination for the converted variant
1036 * pvargSrc [O] Source variant to change the type of
1037 * wFlags [I] VARIANT_ flags from "oleauto.h"
1038 * vt [I] Variant type to change pvargSrc into
1041 * Success: S_OK. pvargDest contains the converted value.
1042 * Failure: An HRESULT error code describing the failure.
1045 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
1046 * See VariantChangeTypeEx.
1048 HRESULT WINAPI
VariantChangeType(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
1049 USHORT wFlags
, VARTYPE vt
)
1051 return VariantChangeTypeEx( pvargDest
, pvargSrc
, LOCALE_USER_DEFAULT
, wFlags
, vt
);
1054 /******************************************************************************
1055 * VariantChangeTypeEx [OLEAUT32.147]
1057 * Change the type of a variant.
1060 * pvargDest [O] Destination for the converted variant
1061 * pvargSrc [O] Source variant to change the type of
1062 * lcid [I] LCID for the conversion
1063 * wFlags [I] VARIANT_ flags from "oleauto.h"
1064 * vt [I] Variant type to change pvargSrc into
1067 * Success: S_OK. pvargDest contains the converted value.
1068 * Failure: An HRESULT error code describing the failure.
1071 * pvargDest and pvargSrc can point to the same variant to perform an in-place
1072 * conversion. If the conversion is successful, pvargSrc will be freed.
1074 HRESULT WINAPI
VariantChangeTypeEx(VARIANTARG
* pvargDest
, VARIANTARG
* pvargSrc
,
1075 LCID lcid
, USHORT wFlags
, VARTYPE vt
)
1079 TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest
),
1080 debugstr_variant(pvargSrc
), lcid
, wFlags
, debugstr_vt(vt
));
1083 res
= DISP_E_BADVARTYPE
;
1086 res
= VARIANT_ValidateType(V_VT(pvargSrc
));
1090 res
= VARIANT_ValidateType(vt
);
1094 VARIANTARG vTmp
, vSrcDeref
;
1096 if(V_ISBYREF(pvargSrc
) && !V_BYREF(pvargSrc
))
1097 res
= DISP_E_TYPEMISMATCH
;
1100 V_VT(&vTmp
) = VT_EMPTY
;
1101 V_VT(&vSrcDeref
) = VT_EMPTY
;
1102 VariantClear(&vTmp
);
1103 VariantClear(&vSrcDeref
);
1108 res
= VariantCopyInd(&vSrcDeref
, pvargSrc
);
1111 if (V_ISARRAY(&vSrcDeref
) || (vt
& VT_ARRAY
))
1112 res
= VARIANT_CoerceArray(&vTmp
, &vSrcDeref
, vt
);
1114 res
= VARIANT_Coerce(&vTmp
, lcid
, wFlags
, &vSrcDeref
, vt
);
1116 if (SUCCEEDED(res
)) {
1118 res
= VariantCopy(pvargDest
, &vTmp
);
1120 VariantClear(&vTmp
);
1121 VariantClear(&vSrcDeref
);
1128 TRACE("returning 0x%08x, %s\n", res
, debugstr_variant(pvargDest
));
1132 /* Date Conversions */
1134 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1136 /* Convert a VT_DATE value to a Julian Date */
1137 static inline int VARIANT_JulianFromDate(int dateIn
)
1139 int julianDays
= dateIn
;
1141 julianDays
-= DATE_MIN
; /* Convert to + days from 1 Jan 100 AD */
1142 julianDays
+= 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1146 /* Convert a Julian Date to a VT_DATE value */
1147 static inline int VARIANT_DateFromJulian(int dateIn
)
1149 int julianDays
= dateIn
;
1151 julianDays
-= 1757585; /* Convert to + days from 1 Jan 100 AD */
1152 julianDays
+= DATE_MIN
; /* Convert to +/- days from 1 Jan 1899 AD */
1156 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1157 static inline void VARIANT_DMYFromJulian(int jd
, USHORT
*year
, USHORT
*month
, USHORT
*day
)
1163 l
-= (n
* 146097 + 3) / 4;
1164 i
= (4000 * (l
+ 1)) / 1461001;
1165 l
+= 31 - (i
* 1461) / 4;
1166 j
= (l
* 80) / 2447;
1167 *day
= l
- (j
* 2447) / 80;
1169 *month
= (j
+ 2) - (12 * l
);
1170 *year
= 100 * (n
- 49) + i
+ l
;
1173 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1174 static inline double VARIANT_JulianFromDMY(USHORT year
, USHORT month
, USHORT day
)
1176 int m12
= (month
- 14) / 12;
1178 return ((1461 * (year
+ 4800 + m12
)) / 4 + (367 * (month
- 2 - 12 * m12
)) / 12 -
1179 (3 * ((year
+ 4900 + m12
) / 100)) / 4 + day
- 32075);
1182 /* Macros for accessing DOS format date/time fields */
1183 #define DOS_YEAR(x) (1980 + (x >> 9))
1184 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1185 #define DOS_DAY(x) (x & 0x1f)
1186 #define DOS_HOUR(x) (x >> 11)
1187 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1188 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1189 /* Create a DOS format date/time */
1190 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1191 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1193 /* Roll a date forwards or backwards to correct it */
1194 static HRESULT
VARIANT_RollUdate(UDATE
*lpUd
)
1196 static const BYTE days
[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1197 short iYear
, iMonth
, iDay
, iHour
, iMinute
, iSecond
;
1199 /* interpret values signed */
1200 iYear
= lpUd
->st
.wYear
;
1201 iMonth
= lpUd
->st
.wMonth
;
1202 iDay
= lpUd
->st
.wDay
;
1203 iHour
= lpUd
->st
.wHour
;
1204 iMinute
= lpUd
->st
.wMinute
;
1205 iSecond
= lpUd
->st
.wSecond
;
1207 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay
, iMonth
,
1208 iYear
, iHour
, iMinute
, iSecond
);
1210 if (iYear
> 9999 || iYear
< -9999)
1211 return E_INVALIDARG
; /* Invalid value */
1212 /* Year 0 to 29 are treated as 2000 + year */
1213 if (iYear
>= 0 && iYear
< 30)
1215 /* Remaining years < 100 are treated as 1900 + year */
1216 else if (iYear
>= 30 && iYear
< 100)
1219 iMinute
+= iSecond
/ 60;
1220 iSecond
= iSecond
% 60;
1221 iHour
+= iMinute
/ 60;
1222 iMinute
= iMinute
% 60;
1225 iYear
+= iMonth
/ 12;
1226 iMonth
= iMonth
% 12;
1227 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1228 while (iDay
> days
[iMonth
])
1230 if (iMonth
== 2 && IsLeapYear(iYear
))
1233 iDay
-= days
[iMonth
];
1235 iYear
+= iMonth
/ 12;
1236 iMonth
= iMonth
% 12;
1241 if (iMonth
<=0) {iMonth
+=12; iYear
--;}
1242 if (iMonth
== 2 && IsLeapYear(iYear
))
1245 iDay
+= days
[iMonth
];
1248 if (iSecond
<0){iSecond
+=60; iMinute
--;}
1249 if (iMinute
<0){iMinute
+=60; iHour
--;}
1250 if (iHour
<0) {iHour
+=24; iDay
--;}
1251 if (iYear
<=0) iYear
+=2000;
1253 lpUd
->st
.wYear
= iYear
;
1254 lpUd
->st
.wMonth
= iMonth
;
1255 lpUd
->st
.wDay
= iDay
;
1256 lpUd
->st
.wHour
= iHour
;
1257 lpUd
->st
.wMinute
= iMinute
;
1258 lpUd
->st
.wSecond
= iSecond
;
1260 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd
->st
.wDay
, lpUd
->st
.wMonth
,
1261 lpUd
->st
.wYear
, lpUd
->st
.wHour
, lpUd
->st
.wMinute
, lpUd
->st
.wSecond
);
1265 /**********************************************************************
1266 * DosDateTimeToVariantTime [OLEAUT32.14]
1268 * Convert a Dos format date and time into variant VT_DATE format.
1271 * wDosDate [I] Dos format date
1272 * wDosTime [I] Dos format time
1273 * pDateOut [O] Destination for VT_DATE format
1276 * Success: TRUE. pDateOut contains the converted time.
1277 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1280 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1281 * - Dos format times are accurate to only 2 second precision.
1282 * - The format of a Dos Date is:
1283 *| Bits Values Meaning
1284 *| ---- ------ -------
1285 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1286 *| the days in the month rolls forward the extra days.
1287 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1288 *| year. 13-15 are invalid.
1289 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1290 * - The format of a Dos Time is:
1291 *| Bits Values Meaning
1292 *| ---- ------ -------
1293 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1294 *| 5-10 0-59 Minutes. 60-63 are invalid.
1295 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1297 INT WINAPI
DosDateTimeToVariantTime(USHORT wDosDate
, USHORT wDosTime
,
1302 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1303 wDosDate
, DOS_YEAR(wDosDate
), DOS_MONTH(wDosDate
), DOS_DAY(wDosDate
),
1304 wDosTime
, DOS_HOUR(wDosTime
), DOS_MINUTE(wDosTime
), DOS_SECOND(wDosTime
),
1307 ud
.st
.wYear
= DOS_YEAR(wDosDate
);
1308 ud
.st
.wMonth
= DOS_MONTH(wDosDate
);
1309 if (ud
.st
.wYear
> 2099 || ud
.st
.wMonth
> 12)
1311 ud
.st
.wDay
= DOS_DAY(wDosDate
);
1312 ud
.st
.wHour
= DOS_HOUR(wDosTime
);
1313 ud
.st
.wMinute
= DOS_MINUTE(wDosTime
);
1314 ud
.st
.wSecond
= DOS_SECOND(wDosTime
);
1315 ud
.st
.wDayOfWeek
= ud
.st
.wMilliseconds
= 0;
1316 if (ud
.st
.wHour
> 23 || ud
.st
.wMinute
> 59 || ud
.st
.wSecond
> 59)
1317 return FALSE
; /* Invalid values in Dos*/
1319 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1322 /**********************************************************************
1323 * VariantTimeToDosDateTime [OLEAUT32.13]
1325 * Convert a variant format date into a Dos format date and time.
1327 * dateIn [I] VT_DATE time format
1328 * pwDosDate [O] Destination for Dos format date
1329 * pwDosTime [O] Destination for Dos format time
1332 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1333 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1336 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1338 INT WINAPI
VariantTimeToDosDateTime(double dateIn
, USHORT
*pwDosDate
, USHORT
*pwDosTime
)
1342 TRACE("(%g,%p,%p)\n", dateIn
, pwDosDate
, pwDosTime
);
1344 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1347 if (ud
.st
.wYear
< 1980 || ud
.st
.wYear
> 2099)
1350 *pwDosDate
= DOS_DATE(ud
.st
.wDay
, ud
.st
.wMonth
, ud
.st
.wYear
);
1351 *pwDosTime
= DOS_TIME(ud
.st
.wHour
, ud
.st
.wMinute
, ud
.st
.wSecond
);
1353 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1354 *pwDosDate
, DOS_YEAR(*pwDosDate
), DOS_MONTH(*pwDosDate
), DOS_DAY(*pwDosDate
),
1355 *pwDosTime
, DOS_HOUR(*pwDosTime
), DOS_MINUTE(*pwDosTime
), DOS_SECOND(*pwDosTime
));
1359 /***********************************************************************
1360 * SystemTimeToVariantTime [OLEAUT32.184]
1362 * Convert a System format date and time into variant VT_DATE format.
1365 * lpSt [I] System format date and time
1366 * pDateOut [O] Destination for VT_DATE format date
1369 * Success: TRUE. *pDateOut contains the converted value.
1370 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1372 INT WINAPI
SystemTimeToVariantTime(LPSYSTEMTIME lpSt
, double *pDateOut
)
1376 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt
, lpSt
->wDay
, lpSt
->wMonth
,
1377 lpSt
->wYear
, lpSt
->wHour
, lpSt
->wMinute
, lpSt
->wSecond
, pDateOut
);
1379 if (lpSt
->wMonth
> 12)
1381 if (lpSt
->wDay
> 31)
1383 if ((short)lpSt
->wYear
< 0)
1387 return VarDateFromUdate(&ud
, 0, pDateOut
) == S_OK
;
1390 /***********************************************************************
1391 * VariantTimeToSystemTime [OLEAUT32.185]
1393 * Convert a variant VT_DATE into a System format date and time.
1396 * datein [I] Variant VT_DATE format date
1397 * lpSt [O] Destination for System format date and time
1400 * Success: TRUE. *lpSt contains the converted value.
1401 * Failure: FALSE, if dateIn is too large or small.
1403 INT WINAPI
VariantTimeToSystemTime(double dateIn
, LPSYSTEMTIME lpSt
)
1407 TRACE("(%g,%p)\n", dateIn
, lpSt
);
1409 if (FAILED(VarUdateFromDate(dateIn
, 0, &ud
)))
1416 /***********************************************************************
1417 * VarDateFromUdateEx [OLEAUT32.319]
1419 * Convert an unpacked format date and time to a variant VT_DATE.
1422 * pUdateIn [I] Unpacked format date and time to convert
1423 * lcid [I] Locale identifier for the conversion
1424 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1425 * pDateOut [O] Destination for variant VT_DATE.
1428 * Success: S_OK. *pDateOut contains the converted value.
1429 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1431 HRESULT WINAPI
VarDateFromUdateEx(UDATE
*pUdateIn
, LCID lcid
, ULONG dwFlags
, DATE
*pDateOut
)
1434 double dateVal
, dateSign
;
1436 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn
,
1437 pUdateIn
->st
.wMonth
, pUdateIn
->st
.wDay
, pUdateIn
->st
.wYear
,
1438 pUdateIn
->st
.wHour
, pUdateIn
->st
.wMinute
, pUdateIn
->st
.wSecond
,
1439 pUdateIn
->st
.wMilliseconds
, pUdateIn
->st
.wDayOfWeek
,
1440 pUdateIn
->wDayOfYear
, lcid
, dwFlags
, pDateOut
);
1442 if (lcid
!= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
))
1443 FIXME("lcid possibly not handled, treating as en-us\n");
1447 if (dwFlags
& VAR_VALIDDATE
)
1448 WARN("Ignoring VAR_VALIDDATE\n");
1450 if (FAILED(VARIANT_RollUdate(&ud
)))
1451 return E_INVALIDARG
;
1454 dateVal
= VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud
.st
.wYear
, ud
.st
.wMonth
, ud
.st
.wDay
));
1457 dateSign
= (dateVal
< 0.0) ? -1.0 : 1.0;
1460 dateVal
+= ud
.st
.wHour
/ 24.0 * dateSign
;
1461 dateVal
+= ud
.st
.wMinute
/ 1440.0 * dateSign
;
1462 dateVal
+= ud
.st
.wSecond
/ 86400.0 * dateSign
;
1464 TRACE("Returning %g\n", dateVal
);
1465 *pDateOut
= dateVal
;
1469 /***********************************************************************
1470 * VarDateFromUdate [OLEAUT32.330]
1472 * Convert an unpacked format date and time to a variant VT_DATE.
1475 * pUdateIn [I] Unpacked format date and time to convert
1476 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1477 * pDateOut [O] Destination for variant VT_DATE.
1480 * Success: S_OK. *pDateOut contains the converted value.
1481 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1484 * This function uses the United States English locale for the conversion. Use
1485 * VarDateFromUdateEx() for alternate locales.
1487 HRESULT WINAPI
VarDateFromUdate(UDATE
*pUdateIn
, ULONG dwFlags
, DATE
*pDateOut
)
1489 LCID lcid
= MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
);
1491 return VarDateFromUdateEx(pUdateIn
, lcid
, dwFlags
, pDateOut
);
1494 /***********************************************************************
1495 * VarUdateFromDate [OLEAUT32.331]
1497 * Convert a variant VT_DATE into an unpacked format date and time.
1500 * datein [I] Variant VT_DATE format date
1501 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1502 * lpUdate [O] Destination for unpacked format date and time
1505 * Success: S_OK. *lpUdate contains the converted value.
1506 * Failure: E_INVALIDARG, if dateIn is too large or small.
1508 HRESULT WINAPI
VarUdateFromDate(DATE dateIn
, ULONG dwFlags
, UDATE
*lpUdate
)
1510 /* Cumulative totals of days per month */
1511 static const USHORT cumulativeDays
[] =
1513 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1515 double datePart
, timePart
;
1518 TRACE("(%g,0x%08x,%p)\n", dateIn
, dwFlags
, lpUdate
);
1520 if (dateIn
<= (DATE_MIN
- 1.0) || dateIn
>= (DATE_MAX
+ 1.0))
1521 return E_INVALIDARG
;
1523 datePart
= dateIn
< 0.0 ? ceil(dateIn
) : floor(dateIn
);
1524 /* Compensate for int truncation (always downwards) */
1525 timePart
= fabs(dateIn
- datePart
) + 0.00000000001;
1526 if (timePart
>= 1.0)
1527 timePart
-= 0.00000000001;
1530 julianDays
= VARIANT_JulianFromDate(dateIn
);
1531 VARIANT_DMYFromJulian(julianDays
, &lpUdate
->st
.wYear
, &lpUdate
->st
.wMonth
,
1534 datePart
= (datePart
+ 1.5) / 7.0;
1535 lpUdate
->st
.wDayOfWeek
= (datePart
- floor(datePart
)) * 7;
1536 if (lpUdate
->st
.wDayOfWeek
== 0)
1537 lpUdate
->st
.wDayOfWeek
= 5;
1538 else if (lpUdate
->st
.wDayOfWeek
== 1)
1539 lpUdate
->st
.wDayOfWeek
= 6;
1541 lpUdate
->st
.wDayOfWeek
-= 2;
1543 if (lpUdate
->st
.wMonth
> 2 && IsLeapYear(lpUdate
->st
.wYear
))
1544 lpUdate
->wDayOfYear
= 1; /* After February, in a leap year */
1546 lpUdate
->wDayOfYear
= 0;
1548 lpUdate
->wDayOfYear
+= cumulativeDays
[lpUdate
->st
.wMonth
];
1549 lpUdate
->wDayOfYear
+= lpUdate
->st
.wDay
;
1553 lpUdate
->st
.wHour
= timePart
;
1554 timePart
-= lpUdate
->st
.wHour
;
1556 lpUdate
->st
.wMinute
= timePart
;
1557 timePart
-= lpUdate
->st
.wMinute
;
1559 lpUdate
->st
.wSecond
= timePart
;
1560 timePart
-= lpUdate
->st
.wSecond
;
1561 lpUdate
->st
.wMilliseconds
= 0;
1564 /* Round the milliseconds, adjusting the time/date forward if needed */
1565 if (lpUdate
->st
.wSecond
< 59)
1566 lpUdate
->st
.wSecond
++;
1569 lpUdate
->st
.wSecond
= 0;
1570 if (lpUdate
->st
.wMinute
< 59)
1571 lpUdate
->st
.wMinute
++;
1574 lpUdate
->st
.wMinute
= 0;
1575 if (lpUdate
->st
.wHour
< 23)
1576 lpUdate
->st
.wHour
++;
1579 lpUdate
->st
.wHour
= 0;
1580 /* Roll over a whole day */
1581 if (++lpUdate
->st
.wDay
> 28)
1582 VARIANT_RollUdate(lpUdate
);
1590 #define GET_NUMBER_TEXT(fld,name) \
1592 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1593 WARN("buffer too small for " #fld "\n"); \
1595 if (buff[0]) lpChars->name = buff[0]; \
1596 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1598 /* Get the valid number characters for an lcid */
1599 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS
*lpChars
, LCID lcid
, DWORD dwFlags
)
1601 static const VARIANT_NUMBER_CHARS defaultChars
= { '-','+','.',',','$',0,'.',',' };
1602 static CRITICAL_SECTION csLastChars
= { NULL
, -1, 0, 0, 0, 0 };
1603 static VARIANT_NUMBER_CHARS lastChars
;
1604 static LCID lastLcid
= -1;
1605 static DWORD lastFlags
= 0;
1606 LCTYPE lctype
= dwFlags
& LOCALE_NOUSEROVERRIDE
;
1609 /* To make caching thread-safe, a critical section is needed */
1610 EnterCriticalSection(&csLastChars
);
1612 /* Asking for default locale entries is very expensive: It is a registry
1613 server call. So cache one locally, as Microsoft does it too */
1614 if(lcid
== lastLcid
&& dwFlags
== lastFlags
)
1616 memcpy(lpChars
, &lastChars
, sizeof(defaultChars
));
1617 LeaveCriticalSection(&csLastChars
);
1621 memcpy(lpChars
, &defaultChars
, sizeof(defaultChars
));
1622 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN
, cNegativeSymbol
);
1623 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN
, cPositiveSymbol
);
1624 GET_NUMBER_TEXT(LOCALE_SDECIMAL
, cDecimalPoint
);
1625 GET_NUMBER_TEXT(LOCALE_STHOUSAND
, cDigitSeparator
);
1626 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP
, cCurrencyDecimalPoint
);
1627 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP
, cCurrencyDigitSeparator
);
1629 /* Local currency symbols are often 2 characters */
1630 lpChars
->cCurrencyLocal2
= '\0';
1631 switch(GetLocaleInfoW(lcid
, lctype
|LOCALE_SCURRENCY
, buff
, sizeof(buff
)/sizeof(WCHAR
)))
1633 case 3: lpChars
->cCurrencyLocal2
= buff
[1]; /* Fall through */
1634 case 2: lpChars
->cCurrencyLocal
= buff
[0];
1636 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1638 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid
, lpChars
->cCurrencyLocal
,
1639 lpChars
->cCurrencyLocal2
, lpChars
->cCurrencyLocal
, lpChars
->cCurrencyLocal2
);
1641 memcpy(&lastChars
, lpChars
, sizeof(defaultChars
));
1643 lastFlags
= dwFlags
;
1644 LeaveCriticalSection(&csLastChars
);
1647 /* Number Parsing States */
1648 #define B_PROCESSING_EXPONENT 0x1
1649 #define B_NEGATIVE_EXPONENT 0x2
1650 #define B_EXPONENT_START 0x4
1651 #define B_INEXACT_ZEROS 0x8
1652 #define B_LEADING_ZERO 0x10
1653 #define B_PROCESSING_HEX 0x20
1654 #define B_PROCESSING_OCT 0x40
1656 /**********************************************************************
1657 * VarParseNumFromStr [OLEAUT32.46]
1659 * Parse a string containing a number into a NUMPARSE structure.
1662 * lpszStr [I] String to parse number from
1663 * lcid [I] Locale Id for the conversion
1664 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1665 * pNumprs [I/O] Destination for parsed number
1666 * rgbDig [O] Destination for digits read in
1669 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1671 * Failure: E_INVALIDARG, if any parameter is invalid.
1672 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1674 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1677 * pNumprs must have the following fields set:
1678 * cDig: Set to the size of rgbDig.
1679 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1683 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1684 * numerals, so this has not been implemented.
1686 HRESULT WINAPI
VarParseNumFromStr(OLECHAR
*lpszStr
, LCID lcid
, ULONG dwFlags
,
1687 NUMPARSE
*pNumprs
, BYTE
*rgbDig
)
1689 VARIANT_NUMBER_CHARS chars
;
1691 DWORD dwState
= B_EXPONENT_START
|B_INEXACT_ZEROS
;
1692 int iMaxDigits
= sizeof(rgbTmp
) / sizeof(BYTE
);
1695 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr
), lcid
, dwFlags
, pNumprs
, rgbDig
);
1697 if (!pNumprs
|| !rgbDig
)
1698 return E_INVALIDARG
;
1700 if (pNumprs
->cDig
< iMaxDigits
)
1701 iMaxDigits
= pNumprs
->cDig
;
1704 pNumprs
->dwOutFlags
= 0;
1705 pNumprs
->cchUsed
= 0;
1706 pNumprs
->nBaseShift
= 0;
1707 pNumprs
->nPwr10
= 0;
1710 return DISP_E_TYPEMISMATCH
;
1712 VARIANT_GetLocalisedNumberChars(&chars
, lcid
, dwFlags
);
1714 /* First consume all the leading symbols and space from the string */
1717 if (pNumprs
->dwInFlags
& NUMPRS_LEADING_WHITE
&& isspaceW(*lpszStr
))
1719 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_WHITE
;
1724 } while (isspaceW(*lpszStr
));
1726 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_PLUS
&&
1727 *lpszStr
== chars
.cPositiveSymbol
&&
1728 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
))
1730 pNumprs
->dwOutFlags
|= NUMPRS_LEADING_PLUS
;
1734 else if (pNumprs
->dwInFlags
& NUMPRS_LEADING_MINUS
&&
1735 *lpszStr
== chars
.cNegativeSymbol
&&
1736 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
))
1738 pNumprs
->dwOutFlags
|= (NUMPRS_LEADING_MINUS
|NUMPRS_NEG
);
1742 else if (pNumprs
->dwInFlags
& NUMPRS_CURRENCY
&&
1743 !(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
) &&
1744 *lpszStr
== chars
.cCurrencyLocal
&&
1745 (!chars
.cCurrencyLocal2
|| lpszStr
[1] == chars
.cCurrencyLocal2
))
1747 pNumprs
->dwOutFlags
|= NUMPRS_CURRENCY
;
1750 /* Only accept currency characters */
1751 chars
.cDecimalPoint
= chars
.cCurrencyDecimalPoint
;
1752 chars
.cDigitSeparator
= chars
.cCurrencyDigitSeparator
;
1754 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== '(' &&
1755 !(pNumprs
->dwOutFlags
& NUMPRS_PARENS
))
1757 pNumprs
->dwOutFlags
|= NUMPRS_PARENS
;
1765 if (!(pNumprs
->dwOutFlags
& NUMPRS_CURRENCY
))
1767 /* Only accept non-currency characters */
1768 chars
.cCurrencyDecimalPoint
= chars
.cDecimalPoint
;
1769 chars
.cCurrencyDigitSeparator
= chars
.cDigitSeparator
;
1772 if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'H' || *(lpszStr
+1) == 'h')) &&
1773 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1775 dwState
|= B_PROCESSING_HEX
;
1776 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1780 else if ((*lpszStr
== '&' && (*(lpszStr
+1) == 'O' || *(lpszStr
+1) == 'o')) &&
1781 pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1783 dwState
|= B_PROCESSING_OCT
;
1784 pNumprs
->dwOutFlags
|= NUMPRS_HEX_OCT
;
1789 /* Strip Leading zeros */
1790 while (*lpszStr
== '0')
1792 dwState
|= B_LEADING_ZERO
;
1799 if (isdigitW(*lpszStr
))
1801 if (dwState
& B_PROCESSING_EXPONENT
)
1803 int exponentSize
= 0;
1804 if (dwState
& B_EXPONENT_START
)
1806 if (!isdigitW(*lpszStr
))
1807 break; /* No exponent digits - invalid */
1808 while (*lpszStr
== '0')
1810 /* Skip leading zero's in the exponent */
1816 while (isdigitW(*lpszStr
))
1819 exponentSize
+= *lpszStr
- '0';
1823 if (dwState
& B_NEGATIVE_EXPONENT
)
1824 exponentSize
= -exponentSize
;
1825 /* Add the exponent into the powers of 10 */
1826 pNumprs
->nPwr10
+= exponentSize
;
1827 dwState
&= ~(B_PROCESSING_EXPONENT
|B_EXPONENT_START
);
1828 lpszStr
--; /* back up to allow processing of next char */
1832 if ((pNumprs
->cDig
>= iMaxDigits
) && !(dwState
& B_PROCESSING_HEX
)
1833 && !(dwState
& B_PROCESSING_OCT
))
1835 pNumprs
->dwOutFlags
|= NUMPRS_INEXACT
;
1837 if (*lpszStr
!= '0')
1838 dwState
&= ~B_INEXACT_ZEROS
; /* Inexact number with non-trailing zeros */
1840 /* This digit can't be represented, but count it in nPwr10 */
1841 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1848 if ((dwState
& B_PROCESSING_OCT
) && ((*lpszStr
== '8') || (*lpszStr
== '9'))) {
1849 return DISP_E_TYPEMISMATCH
;
1852 if (pNumprs
->dwOutFlags
& NUMPRS_DECIMAL
)
1853 pNumprs
->nPwr10
--; /* Count decimal points in nPwr10 */
1855 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- '0';
1861 else if (*lpszStr
== chars
.cDigitSeparator
&& pNumprs
->dwInFlags
& NUMPRS_THOUSANDS
)
1863 pNumprs
->dwOutFlags
|= NUMPRS_THOUSANDS
;
1866 else if (*lpszStr
== chars
.cDecimalPoint
&&
1867 pNumprs
->dwInFlags
& NUMPRS_DECIMAL
&&
1868 !(pNumprs
->dwOutFlags
& (NUMPRS_DECIMAL
|NUMPRS_EXPONENT
)))
1870 pNumprs
->dwOutFlags
|= NUMPRS_DECIMAL
;
1873 /* If we have no digits so far, skip leading zeros */
1876 while (lpszStr
[1] == '0')
1878 dwState
|= B_LEADING_ZERO
;
1885 else if (((*lpszStr
>= 'a' && *lpszStr
<= 'f') ||
1886 (*lpszStr
>= 'A' && *lpszStr
<= 'F')) &&
1887 dwState
& B_PROCESSING_HEX
)
1889 if (pNumprs
->cDig
>= iMaxDigits
)
1891 return DISP_E_OVERFLOW
;
1895 if (*lpszStr
>= 'a')
1896 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'a' + 10;
1898 rgbTmp
[pNumprs
->cDig
] = *lpszStr
- 'A' + 10;
1903 else if ((*lpszStr
== 'e' || *lpszStr
== 'E') &&
1904 pNumprs
->dwInFlags
& NUMPRS_EXPONENT
&&
1905 !(pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
))
1907 dwState
|= B_PROCESSING_EXPONENT
;
1908 pNumprs
->dwOutFlags
|= NUMPRS_EXPONENT
;
1911 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cPositiveSymbol
)
1913 cchUsed
++; /* Ignore positive exponent */
1915 else if (dwState
& B_PROCESSING_EXPONENT
&& *lpszStr
== chars
.cNegativeSymbol
)
1917 dwState
|= B_NEGATIVE_EXPONENT
;
1921 break; /* Stop at an unrecognised character */
1926 if (!pNumprs
->cDig
&& dwState
& B_LEADING_ZERO
)
1928 /* Ensure a 0 on its own gets stored */
1933 if (pNumprs
->dwOutFlags
& NUMPRS_EXPONENT
&& dwState
& B_PROCESSING_EXPONENT
)
1935 pNumprs
->cchUsed
= cchUsed
;
1936 WARN("didn't completely parse exponent\n");
1937 return DISP_E_TYPEMISMATCH
; /* Failed to completely parse the exponent */
1940 if (pNumprs
->dwOutFlags
& NUMPRS_INEXACT
)
1942 if (dwState
& B_INEXACT_ZEROS
)
1943 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* All zeros doesn't set NUMPRS_INEXACT */
1944 } else if(pNumprs
->dwInFlags
& NUMPRS_HEX_OCT
)
1946 /* copy all of the digits into the output digit buffer */
1947 /* this is exactly what windows does although it also returns */
1948 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1949 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1951 if (dwState
& B_PROCESSING_HEX
) {
1952 /* hex numbers have always the same format */
1954 pNumprs
->nBaseShift
=4;
1956 if (dwState
& B_PROCESSING_OCT
) {
1957 /* oct numbers have always the same format */
1959 pNumprs
->nBaseShift
=3;
1961 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1970 /* Remove trailing zeros from the last (whole number or decimal) part */
1971 while (pNumprs
->cDig
> 1 && !rgbTmp
[pNumprs
->cDig
- 1])
1978 if (pNumprs
->cDig
<= iMaxDigits
)
1979 pNumprs
->dwOutFlags
&= ~NUMPRS_INEXACT
; /* Ignore stripped zeros for NUMPRS_INEXACT */
1981 pNumprs
->cDig
= iMaxDigits
; /* Only return iMaxDigits worth of digits */
1983 /* Copy the digits we processed into rgbDig */
1984 memcpy(rgbDig
, rgbTmp
, pNumprs
->cDig
* sizeof(BYTE
));
1986 /* Consume any trailing symbols and space */
1989 if ((pNumprs
->dwInFlags
& NUMPRS_TRAILING_WHITE
) && isspaceW(*lpszStr
))
1991 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_WHITE
;
1996 } while (isspaceW(*lpszStr
));
1998 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_PLUS
&&
1999 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_PLUS
) &&
2000 *lpszStr
== chars
.cPositiveSymbol
)
2002 pNumprs
->dwOutFlags
|= NUMPRS_TRAILING_PLUS
;
2006 else if (pNumprs
->dwInFlags
& NUMPRS_TRAILING_MINUS
&&
2007 !(pNumprs
->dwOutFlags
& NUMPRS_LEADING_MINUS
) &&
2008 *lpszStr
== chars
.cNegativeSymbol
)
2010 pNumprs
->dwOutFlags
|= (NUMPRS_TRAILING_MINUS
|NUMPRS_NEG
);
2014 else if (pNumprs
->dwInFlags
& NUMPRS_PARENS
&& *lpszStr
== ')' &&
2015 pNumprs
->dwOutFlags
& NUMPRS_PARENS
)
2019 pNumprs
->dwOutFlags
|= NUMPRS_NEG
;
2025 if (pNumprs
->dwOutFlags
& NUMPRS_PARENS
&& !(pNumprs
->dwOutFlags
& NUMPRS_NEG
))
2027 pNumprs
->cchUsed
= cchUsed
;
2028 return DISP_E_TYPEMISMATCH
; /* Opening parenthesis not matched */
2031 if (pNumprs
->dwInFlags
& NUMPRS_USE_ALL
&& *lpszStr
!= '\0')
2032 return DISP_E_TYPEMISMATCH
; /* Not all chars were consumed */
2035 return DISP_E_TYPEMISMATCH
; /* No Number found */
2037 pNumprs
->cchUsed
= cchUsed
;
2041 /* VTBIT flags indicating an integer value */
2042 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
2043 /* VTBIT flags indicating a real number value */
2044 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
2046 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
2047 #define FITS_AS_I1(x) ((x) >> 8 == 0)
2048 #define FITS_AS_I2(x) ((x) >> 16 == 0)
2049 #define FITS_AS_I4(x) ((x) >> 32 == 0)
2051 /**********************************************************************
2052 * VarNumFromParseNum [OLEAUT32.47]
2054 * Convert a NUMPARSE structure into a numeric Variant type.
2057 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
2058 * rgbDig [I] Source for the numbers digits
2059 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
2060 * pVarDst [O] Destination for the converted Variant value.
2063 * Success: S_OK. pVarDst contains the converted value.
2064 * Failure: E_INVALIDARG, if any parameter is invalid.
2065 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
2068 * - The smallest favoured type present in dwVtBits that can represent the
2069 * number in pNumprs without losing precision is used.
2070 * - Signed types are preferred over unsigned types of the same size.
2071 * - Preferred types in order are: integer, float, double, currency then decimal.
2072 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
2073 * for details of the rounding method.
2074 * - pVarDst is not cleared before the result is stored in it.
2075 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
2076 * design?): If some other VTBIT's for integers are specified together
2077 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
2078 * the number to the smallest requested integer truncating this way the
2079 * number. Wine doesn't implement this "feature" (yet?).
2081 HRESULT WINAPI
VarNumFromParseNum(NUMPARSE
*pNumprs
, BYTE
*rgbDig
,
2082 ULONG dwVtBits
, VARIANT
*pVarDst
)
2084 /* Scale factors and limits for double arithmetic */
2085 static const double dblMultipliers
[11] = {
2086 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2087 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2089 static const double dblMinimums
[11] = {
2090 R8_MIN
, R8_MIN
*10.0, R8_MIN
*100.0, R8_MIN
*1000.0, R8_MIN
*10000.0,
2091 R8_MIN
*100000.0, R8_MIN
*1000000.0, R8_MIN
*10000000.0,
2092 R8_MIN
*100000000.0, R8_MIN
*1000000000.0, R8_MIN
*10000000000.0
2094 static const double dblMaximums
[11] = {
2095 R8_MAX
, R8_MAX
/10.0, R8_MAX
/100.0, R8_MAX
/1000.0, R8_MAX
/10000.0,
2096 R8_MAX
/100000.0, R8_MAX
/1000000.0, R8_MAX
/10000000.0,
2097 R8_MAX
/100000000.0, R8_MAX
/1000000000.0, R8_MAX
/10000000000.0
2100 int wholeNumberDigits
, fractionalDigits
, divisor10
= 0, multiplier10
= 0;
2102 TRACE("(%p,%p,0x%x,%p)\n", pNumprs
, rgbDig
, dwVtBits
, pVarDst
);
2104 if (pNumprs
->nBaseShift
)
2106 /* nBaseShift indicates a hex or octal number */
2111 /* Convert the hex or octal number string into a UI64 */
2112 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2114 if (ul64
> ((UI8_MAX
>>pNumprs
->nBaseShift
) - rgbDig
[i
]))
2116 TRACE("Overflow multiplying digits\n");
2117 return DISP_E_OVERFLOW
;
2119 ul64
= (ul64
<<pNumprs
->nBaseShift
) + rgbDig
[i
];
2122 /* also make a negative representation */
2125 /* Try signed and unsigned types in size order */
2126 if (dwVtBits
& VTBIT_I1
&& FITS_AS_I1(ul64
))
2128 V_VT(pVarDst
) = VT_I1
;
2129 V_I1(pVarDst
) = ul64
;
2132 else if (dwVtBits
& VTBIT_UI1
&& FITS_AS_I1(ul64
))
2134 V_VT(pVarDst
) = VT_UI1
;
2135 V_UI1(pVarDst
) = ul64
;
2138 else if (dwVtBits
& VTBIT_I2
&& FITS_AS_I2(ul64
))
2140 V_VT(pVarDst
) = VT_I2
;
2141 V_I2(pVarDst
) = ul64
;
2144 else if (dwVtBits
& VTBIT_UI2
&& FITS_AS_I2(ul64
))
2146 V_VT(pVarDst
) = VT_UI2
;
2147 V_UI2(pVarDst
) = ul64
;
2150 else if (dwVtBits
& VTBIT_I4
&& FITS_AS_I4(ul64
))
2152 V_VT(pVarDst
) = VT_I4
;
2153 V_I4(pVarDst
) = ul64
;
2156 else if (dwVtBits
& VTBIT_UI4
&& FITS_AS_I4(ul64
))
2158 V_VT(pVarDst
) = VT_UI4
;
2159 V_UI4(pVarDst
) = ul64
;
2162 else if (dwVtBits
& VTBIT_I8
&& ((ul64
<= I8_MAX
)||(l64
>=I8_MIN
)))
2164 V_VT(pVarDst
) = VT_I8
;
2165 V_I8(pVarDst
) = ul64
;
2168 else if (dwVtBits
& VTBIT_UI8
)
2170 V_VT(pVarDst
) = VT_UI8
;
2171 V_UI8(pVarDst
) = ul64
;
2174 else if ((dwVtBits
& VTBIT_DECIMAL
) == VTBIT_DECIMAL
)
2176 V_VT(pVarDst
) = VT_DECIMAL
;
2177 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2178 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2179 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2182 else if (dwVtBits
& VTBIT_R4
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2184 V_VT(pVarDst
) = VT_R4
;
2186 V_R4(pVarDst
) = ul64
;
2188 V_R4(pVarDst
) = l64
;
2191 else if (dwVtBits
& VTBIT_R8
&& ((ul64
<= I4_MAX
)||(l64
>= I4_MIN
)))
2193 V_VT(pVarDst
) = VT_R8
;
2195 V_R8(pVarDst
) = ul64
;
2197 V_R8(pVarDst
) = l64
;
2201 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits
, wine_dbgstr_longlong(ul64
));
2202 return DISP_E_OVERFLOW
;
2205 /* Count the number of relevant fractional and whole digits stored,
2206 * And compute the divisor/multiplier to scale the number by.
2208 if (pNumprs
->nPwr10
< 0)
2210 if (-pNumprs
->nPwr10
>= pNumprs
->cDig
)
2212 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2213 wholeNumberDigits
= 0;
2214 fractionalDigits
= pNumprs
->cDig
;
2215 divisor10
= -pNumprs
->nPwr10
;
2219 /* An exactly represented real number e.g. 1.024 */
2220 wholeNumberDigits
= pNumprs
->cDig
+ pNumprs
->nPwr10
;
2221 fractionalDigits
= pNumprs
->cDig
- wholeNumberDigits
;
2222 divisor10
= pNumprs
->cDig
- wholeNumberDigits
;
2225 else if (pNumprs
->nPwr10
== 0)
2227 /* An exactly represented whole number e.g. 1024 */
2228 wholeNumberDigits
= pNumprs
->cDig
;
2229 fractionalDigits
= 0;
2231 else /* pNumprs->nPwr10 > 0 */
2233 /* A whole number followed by nPwr10 0's e.g. 102400 */
2234 wholeNumberDigits
= pNumprs
->cDig
;
2235 fractionalDigits
= 0;
2236 multiplier10
= pNumprs
->nPwr10
;
2239 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2240 pNumprs
->cDig
, pNumprs
->nPwr10
, wholeNumberDigits
, fractionalDigits
,
2241 multiplier10
, divisor10
);
2243 if (dwVtBits
& (INTEGER_VTBITS
|VTBIT_DECIMAL
) &&
2244 (!fractionalDigits
|| !(dwVtBits
& (REAL_VTBITS
|VTBIT_CY
|VTBIT_DECIMAL
))))
2246 /* We have one or more integer output choices, and either:
2247 * 1) An integer input value, or
2248 * 2) A real number input value but no floating output choices.
2249 * Alternately, we have a DECIMAL output available and an integer input.
2251 * So, place the integer value into pVarDst, using the smallest type
2252 * possible and preferring signed over unsigned types.
2254 BOOL bOverflow
= FALSE
, bNegative
;
2258 /* Convert the integer part of the number into a UI8 */
2259 for (i
= 0; i
< wholeNumberDigits
; i
++)
2261 if (ul64
> UI8_MAX
/ 10 || (ul64
== UI8_MAX
/ 10 && rgbDig
[i
] > UI8_MAX
% 10))
2263 TRACE("Overflow multiplying digits\n");
2267 ul64
= ul64
* 10 + rgbDig
[i
];
2270 /* Account for the scale of the number */
2271 if (!bOverflow
&& multiplier10
)
2273 for (i
= 0; i
< multiplier10
; i
++)
2275 if (ul64
> (UI8_MAX
/ 10))
2277 TRACE("Overflow scaling number\n");
2285 /* If we have any fractional digits, round the value.
2286 * Note we don't have to do this if divisor10 is < 1,
2287 * because this means the fractional part must be < 0.5
2289 if (!bOverflow
&& fractionalDigits
&& divisor10
> 0)
2291 const BYTE
* fracDig
= rgbDig
+ wholeNumberDigits
;
2292 BOOL bAdjust
= FALSE
;
2294 TRACE("first decimal value is %d\n", *fracDig
);
2297 bAdjust
= TRUE
; /* > 0.5 */
2298 else if (*fracDig
== 5)
2300 for (i
= 1; i
< fractionalDigits
; i
++)
2304 bAdjust
= TRUE
; /* > 0.5 */
2308 /* If exactly 0.5, round only odd values */
2309 if (i
== fractionalDigits
&& (ul64
& 1))
2315 if (ul64
== UI8_MAX
)
2317 TRACE("Overflow after rounding\n");
2324 /* Zero is not a negative number */
2325 bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
&& ul64
;
2327 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64
), bNegative
);
2329 /* For negative integers, try the signed types in size order */
2330 if (!bOverflow
&& bNegative
)
2332 if (dwVtBits
& (VTBIT_I1
|VTBIT_I2
|VTBIT_I4
|VTBIT_I8
))
2334 if (dwVtBits
& VTBIT_I1
&& ul64
<= -I1_MIN
)
2336 V_VT(pVarDst
) = VT_I1
;
2337 V_I1(pVarDst
) = -ul64
;
2340 else if (dwVtBits
& VTBIT_I2
&& ul64
<= -I2_MIN
)
2342 V_VT(pVarDst
) = VT_I2
;
2343 V_I2(pVarDst
) = -ul64
;
2346 else if (dwVtBits
& VTBIT_I4
&& ul64
<= -((LONGLONG
)I4_MIN
))
2348 V_VT(pVarDst
) = VT_I4
;
2349 V_I4(pVarDst
) = -ul64
;
2352 else if (dwVtBits
& VTBIT_I8
&& ul64
<= (ULONGLONG
)I8_MAX
+ 1)
2354 V_VT(pVarDst
) = VT_I8
;
2355 V_I8(pVarDst
) = -ul64
;
2358 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2360 /* Decimal is only output choice left - fast path */
2361 V_VT(pVarDst
) = VT_DECIMAL
;
2362 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_NEG
,0);
2363 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2364 DEC_LO64(&V_DECIMAL(pVarDst
)) = -ul64
;
2369 else if (!bOverflow
)
2371 /* For positive integers, try signed then unsigned types in size order */
2372 if (dwVtBits
& VTBIT_I1
&& ul64
<= I1_MAX
)
2374 V_VT(pVarDst
) = VT_I1
;
2375 V_I1(pVarDst
) = ul64
;
2378 else if (dwVtBits
& VTBIT_UI1
&& ul64
<= UI1_MAX
)
2380 V_VT(pVarDst
) = VT_UI1
;
2381 V_UI1(pVarDst
) = ul64
;
2384 else if (dwVtBits
& VTBIT_I2
&& ul64
<= I2_MAX
)
2386 V_VT(pVarDst
) = VT_I2
;
2387 V_I2(pVarDst
) = ul64
;
2390 else if (dwVtBits
& VTBIT_UI2
&& ul64
<= UI2_MAX
)
2392 V_VT(pVarDst
) = VT_UI2
;
2393 V_UI2(pVarDst
) = ul64
;
2396 else if (dwVtBits
& VTBIT_I4
&& ul64
<= I4_MAX
)
2398 V_VT(pVarDst
) = VT_I4
;
2399 V_I4(pVarDst
) = ul64
;
2402 else if (dwVtBits
& VTBIT_UI4
&& ul64
<= UI4_MAX
)
2404 V_VT(pVarDst
) = VT_UI4
;
2405 V_UI4(pVarDst
) = ul64
;
2408 else if (dwVtBits
& VTBIT_I8
&& ul64
<= I8_MAX
)
2410 V_VT(pVarDst
) = VT_I8
;
2411 V_I8(pVarDst
) = ul64
;
2414 else if (dwVtBits
& VTBIT_UI8
)
2416 V_VT(pVarDst
) = VT_UI8
;
2417 V_UI8(pVarDst
) = ul64
;
2420 else if ((dwVtBits
& REAL_VTBITS
) == VTBIT_DECIMAL
)
2422 /* Decimal is only output choice left - fast path */
2423 V_VT(pVarDst
) = VT_DECIMAL
;
2424 DEC_SIGNSCALE(&V_DECIMAL(pVarDst
)) = SIGNSCALE(DECIMAL_POS
,0);
2425 DEC_HI32(&V_DECIMAL(pVarDst
)) = 0;
2426 DEC_LO64(&V_DECIMAL(pVarDst
)) = ul64
;
2432 if (dwVtBits
& REAL_VTBITS
)
2434 /* Try to put the number into a float or real */
2435 BOOL bOverflow
= FALSE
, bNegative
= pNumprs
->dwOutFlags
& NUMPRS_NEG
;
2439 /* Convert the number into a double */
2440 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2441 whole
= whole
* 10.0 + rgbDig
[i
];
2443 TRACE("Whole double value is %16.16g\n", whole
);
2445 /* Account for the scale */
2446 while (multiplier10
> 10)
2448 if (whole
> dblMaximums
[10])
2450 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2454 whole
= whole
* dblMultipliers
[10];
2457 if (multiplier10
&& !bOverflow
)
2459 if (whole
> dblMaximums
[multiplier10
])
2461 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
);
2465 whole
= whole
* dblMultipliers
[multiplier10
];
2469 TRACE("Scaled double value is %16.16g\n", whole
);
2471 while (divisor10
> 10 && !bOverflow
)
2473 if (whole
< dblMinimums
[10] && whole
!= 0)
2475 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2479 whole
= whole
/ dblMultipliers
[10];
2482 if (divisor10
&& !bOverflow
)
2484 if (whole
< dblMinimums
[divisor10
] && whole
!= 0)
2486 dwVtBits
&= ~(VTBIT_R4
|VTBIT_R8
|VTBIT_CY
); /* Underflow */
2490 whole
= whole
/ dblMultipliers
[divisor10
];
2493 TRACE("Final double value is %16.16g\n", whole
);
2495 if (dwVtBits
& VTBIT_R4
&&
2496 ((whole
<= R4_MAX
&& whole
>= R4_MIN
) || whole
== 0.0))
2498 TRACE("Set R4 to final value\n");
2499 V_VT(pVarDst
) = VT_R4
; /* Fits into a float */
2500 V_R4(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2504 if (dwVtBits
& VTBIT_R8
)
2506 TRACE("Set R8 to final value\n");
2507 V_VT(pVarDst
) = VT_R8
; /* Fits into a double */
2508 V_R8(pVarDst
) = pNumprs
->dwOutFlags
& NUMPRS_NEG
? -whole
: whole
;
2512 if (dwVtBits
& VTBIT_CY
)
2514 if (SUCCEEDED(VarCyFromR8(bNegative
? -whole
: whole
, &V_CY(pVarDst
))))
2516 V_VT(pVarDst
) = VT_CY
; /* Fits into a currency */
2517 TRACE("Set CY to final value\n");
2520 TRACE("Value Overflows CY\n");
2524 if (dwVtBits
& VTBIT_DECIMAL
)
2529 DECIMAL
* pDec
= &V_DECIMAL(pVarDst
);
2531 DECIMAL_SETZERO(*pDec
);
2534 if (pNumprs
->dwOutFlags
& NUMPRS_NEG
)
2535 DEC_SIGN(pDec
) = DECIMAL_NEG
;
2537 DEC_SIGN(pDec
) = DECIMAL_POS
;
2539 /* Factor the significant digits */
2540 for (i
= 0; i
< pNumprs
->cDig
; i
++)
2542 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10 + rgbDig
[i
];
2543 carry
= (ULONG
)(tmp
>> 32);
2544 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2545 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2546 carry
= (ULONG
)(tmp
>> 32);
2547 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2548 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2549 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2551 if (tmp
>> 32 & UI4_MAX
)
2553 VarNumFromParseNum_DecOverflow
:
2554 TRACE("Overflow\n");
2555 DEC_LO32(pDec
) = DEC_MID32(pDec
) = DEC_HI32(pDec
) = UI4_MAX
;
2556 return DISP_E_OVERFLOW
;
2560 /* Account for the scale of the number */
2561 while (multiplier10
> 0)
2563 tmp
= (ULONG64
)DEC_LO32(pDec
) * 10;
2564 carry
= (ULONG
)(tmp
>> 32);
2565 DEC_LO32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2566 tmp
= (ULONG64
)DEC_MID32(pDec
) * 10 + carry
;
2567 carry
= (ULONG
)(tmp
>> 32);
2568 DEC_MID32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2569 tmp
= (ULONG64
)DEC_HI32(pDec
) * 10 + carry
;
2570 DEC_HI32(pDec
) = (ULONG
)(tmp
& UI4_MAX
);
2572 if (tmp
>> 32 & UI4_MAX
)
2573 goto VarNumFromParseNum_DecOverflow
;
2576 DEC_SCALE(pDec
) = divisor10
;
2578 V_VT(pVarDst
) = VT_DECIMAL
;
2581 return DISP_E_OVERFLOW
; /* No more output choices */
2584 /**********************************************************************
2585 * VarCat [OLEAUT32.318]
2587 * Concatenates one variant onto another.
2590 * left [I] First variant
2591 * right [I] Second variant
2592 * result [O] Result variant
2596 * Failure: An HRESULT error code indicating the error.
2598 HRESULT WINAPI
VarCat(LPVARIANT left
, LPVARIANT right
, LPVARIANT out
)
2600 VARTYPE leftvt
,rightvt
,resultvt
;
2602 static WCHAR str_true
[32];
2603 static WCHAR str_false
[32];
2604 static const WCHAR sz_empty
[] = {'\0'};
2605 leftvt
= V_VT(left
);
2606 rightvt
= V_VT(right
);
2608 TRACE("%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), out
);
2611 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT
, IDS_FALSE
, str_false
);
2612 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT
, IDS_TRUE
, str_true
);
2615 /* when both left and right are NULL the result is NULL */
2616 if (leftvt
== VT_NULL
&& rightvt
== VT_NULL
)
2618 V_VT(out
) = VT_NULL
;
2623 resultvt
= VT_EMPTY
;
2625 /* There are many special case for errors and return types */
2626 if (leftvt
== VT_VARIANT
&& (rightvt
== VT_ERROR
||
2627 rightvt
== VT_DATE
|| rightvt
== VT_DECIMAL
))
2628 hres
= DISP_E_TYPEMISMATCH
;
2629 else if ((leftvt
== VT_I2
|| leftvt
== VT_I4
||
2630 leftvt
== VT_R4
|| leftvt
== VT_R8
||
2631 leftvt
== VT_CY
|| leftvt
== VT_BOOL
||
2632 leftvt
== VT_BSTR
|| leftvt
== VT_I1
||
2633 leftvt
== VT_UI1
|| leftvt
== VT_UI2
||
2634 leftvt
== VT_UI4
|| leftvt
== VT_I8
||
2635 leftvt
== VT_UI8
|| leftvt
== VT_INT
||
2636 leftvt
== VT_UINT
|| leftvt
== VT_EMPTY
||
2637 leftvt
== VT_NULL
|| leftvt
== VT_DATE
||
2638 leftvt
== VT_DECIMAL
|| leftvt
== VT_DISPATCH
)
2640 (rightvt
== VT_I2
|| rightvt
== VT_I4
||
2641 rightvt
== VT_R4
|| rightvt
== VT_R8
||
2642 rightvt
== VT_CY
|| rightvt
== VT_BOOL
||
2643 rightvt
== VT_BSTR
|| rightvt
== VT_I1
||
2644 rightvt
== VT_UI1
|| rightvt
== VT_UI2
||
2645 rightvt
== VT_UI4
|| rightvt
== VT_I8
||
2646 rightvt
== VT_UI8
|| rightvt
== VT_INT
||
2647 rightvt
== VT_UINT
|| rightvt
== VT_EMPTY
||
2648 rightvt
== VT_NULL
|| rightvt
== VT_DATE
||
2649 rightvt
== VT_DECIMAL
|| rightvt
== VT_DISPATCH
))
2651 else if (rightvt
== VT_ERROR
&& leftvt
< VT_VOID
)
2652 hres
= DISP_E_TYPEMISMATCH
;
2653 else if (leftvt
== VT_ERROR
&& (rightvt
== VT_DATE
||
2654 rightvt
== VT_ERROR
|| rightvt
== VT_DECIMAL
))
2655 hres
= DISP_E_TYPEMISMATCH
;
2656 else if (rightvt
== VT_DATE
|| rightvt
== VT_ERROR
||
2657 rightvt
== VT_DECIMAL
)
2658 hres
= DISP_E_BADVARTYPE
;
2659 else if (leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
2660 hres
= DISP_E_TYPEMISMATCH
;
2661 else if (leftvt
== VT_VARIANT
)
2662 hres
= DISP_E_TYPEMISMATCH
;
2663 else if (rightvt
== VT_VARIANT
&& (leftvt
== VT_EMPTY
||
2664 leftvt
== VT_NULL
|| leftvt
== VT_I2
||
2665 leftvt
== VT_I4
|| leftvt
== VT_R4
||
2666 leftvt
== VT_R8
|| leftvt
== VT_CY
||
2667 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
2668 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
2669 leftvt
== VT_I1
|| leftvt
== VT_UI1
||
2670 leftvt
== VT_UI2
|| leftvt
== VT_UI4
||
2671 leftvt
== VT_I8
|| leftvt
== VT_UI8
||
2672 leftvt
== VT_INT
|| leftvt
== VT_UINT
))
2673 hres
= DISP_E_TYPEMISMATCH
;
2675 hres
= DISP_E_BADVARTYPE
;
2677 /* if result type is not S_OK, then no need to go further */
2680 V_VT(out
) = resultvt
;
2683 /* Else proceed with formatting inputs to strings */
2686 VARIANT bstrvar_left
, bstrvar_right
;
2687 V_VT(out
) = VT_BSTR
;
2689 VariantInit(&bstrvar_left
);
2690 VariantInit(&bstrvar_right
);
2692 /* Convert left side variant to string */
2693 if (leftvt
!= VT_BSTR
)
2695 if (leftvt
== VT_BOOL
)
2697 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2698 V_VT(&bstrvar_left
) = VT_BSTR
;
2700 V_BSTR(&bstrvar_left
) = SysAllocString(str_true
);
2702 V_BSTR(&bstrvar_left
) = SysAllocString(str_false
);
2704 /* Fill with empty string for later concat with right side */
2705 else if (leftvt
== VT_NULL
)
2707 V_VT(&bstrvar_left
) = VT_BSTR
;
2708 V_BSTR(&bstrvar_left
) = SysAllocString(sz_empty
);
2712 hres
= VariantChangeTypeEx(&bstrvar_left
,left
,0,0,VT_BSTR
);
2714 VariantClear(&bstrvar_left
);
2715 VariantClear(&bstrvar_right
);
2716 if (leftvt
== VT_NULL
&& (rightvt
== VT_EMPTY
||
2717 rightvt
== VT_NULL
|| rightvt
== VT_I2
||
2718 rightvt
== VT_I4
|| rightvt
== VT_R4
||
2719 rightvt
== VT_R8
|| rightvt
== VT_CY
||
2720 rightvt
== VT_DATE
|| rightvt
== VT_BSTR
||
2721 rightvt
== VT_BOOL
|| rightvt
== VT_DECIMAL
||
2722 rightvt
== VT_I1
|| rightvt
== VT_UI1
||
2723 rightvt
== VT_UI2
|| rightvt
== VT_UI4
||
2724 rightvt
== VT_I8
|| rightvt
== VT_UI8
||
2725 rightvt
== VT_INT
|| rightvt
== VT_UINT
))
2726 return DISP_E_BADVARTYPE
;
2732 /* convert right side variant to string */
2733 if (rightvt
!= VT_BSTR
)
2735 if (rightvt
== VT_BOOL
)
2737 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2738 V_VT(&bstrvar_right
) = VT_BSTR
;
2740 V_BSTR(&bstrvar_right
) = SysAllocString(str_true
);
2742 V_BSTR(&bstrvar_right
) = SysAllocString(str_false
);
2744 /* Fill with empty string for later concat with right side */
2745 else if (rightvt
== VT_NULL
)
2747 V_VT(&bstrvar_right
) = VT_BSTR
;
2748 V_BSTR(&bstrvar_right
) = SysAllocString(sz_empty
);
2752 hres
= VariantChangeTypeEx(&bstrvar_right
,right
,0,0,VT_BSTR
);
2754 VariantClear(&bstrvar_left
);
2755 VariantClear(&bstrvar_right
);
2756 if (rightvt
== VT_NULL
&& (leftvt
== VT_EMPTY
||
2757 leftvt
== VT_NULL
|| leftvt
== VT_I2
||
2758 leftvt
== VT_I4
|| leftvt
== VT_R4
||
2759 leftvt
== VT_R8
|| leftvt
== VT_CY
||
2760 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
2761 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
2762 leftvt
== VT_I1
|| leftvt
== VT_UI1
||
2763 leftvt
== VT_UI2
|| leftvt
== VT_UI4
||
2764 leftvt
== VT_I8
|| leftvt
== VT_UI8
||
2765 leftvt
== VT_INT
|| leftvt
== VT_UINT
))
2766 return DISP_E_BADVARTYPE
;
2772 /* Concat the resulting strings together */
2773 if (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
)
2774 VarBstrCat (V_BSTR(left
), V_BSTR(right
), &V_BSTR(out
));
2775 else if (leftvt
!= VT_BSTR
&& rightvt
!= VT_BSTR
)
2776 VarBstrCat (V_BSTR(&bstrvar_left
), V_BSTR(&bstrvar_right
), &V_BSTR(out
));
2777 else if (leftvt
!= VT_BSTR
&& rightvt
== VT_BSTR
)
2778 VarBstrCat (V_BSTR(&bstrvar_left
), V_BSTR(right
), &V_BSTR(out
));
2779 else if (leftvt
== VT_BSTR
&& rightvt
!= VT_BSTR
)
2780 VarBstrCat (V_BSTR(left
), V_BSTR(&bstrvar_right
), &V_BSTR(out
));
2782 VariantClear(&bstrvar_left
);
2783 VariantClear(&bstrvar_right
);
2789 /* Wrapper around VariantChangeTypeEx() which permits changing a
2790 variant with VT_RESERVED flag set. Needed by VarCmp. */
2791 static HRESULT
_VarChangeTypeExWrap (VARIANTARG
* pvargDest
,
2792 VARIANTARG
* pvargSrc
, LCID lcid
, USHORT wFlags
, VARTYPE vt
)
2794 VARIANTARG vtmpsrc
= *pvargSrc
;
2796 V_VT(&vtmpsrc
) &= ~VT_RESERVED
;
2797 return VariantChangeTypeEx(pvargDest
,&vtmpsrc
,lcid
,wFlags
,vt
);
2800 /**********************************************************************
2801 * VarCmp [OLEAUT32.176]
2803 * Compare two variants.
2806 * left [I] First variant
2807 * right [I] Second variant
2808 * lcid [I] LCID (locale identifier) for the comparison
2809 * flags [I] Flags to be used in the comparison:
2810 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2811 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2814 * VARCMP_LT: left variant is less than right variant.
2815 * VARCMP_EQ: input variants are equal.
2816 * VARCMP_GT: left variant is greater than right variant.
2817 * VARCMP_NULL: either one of the input variants is NULL.
2818 * Failure: An HRESULT error code indicating the error.
2821 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2822 * UI8 and UINT as input variants. INT is accepted only as left variant.
2824 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2825 * an ERROR variant will trigger an error.
2827 * Both input variants can have VT_RESERVED flag set which is ignored
2828 * unless one and only one of the variants is a BSTR and the other one
2829 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2830 * different meaning:
2831 * - BSTR and other: BSTR is always greater than the other variant.
2832 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2833 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2834 * comparison will take place else the BSTR is always greater.
2835 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2836 * variant is ignored and the return value depends only on the sign
2837 * of the BSTR if it is a number else the BSTR is always greater. A
2838 * positive BSTR is greater, a negative one is smaller than the other
2842 * VarBstrCmp for the lcid and flags usage.
2844 HRESULT WINAPI
VarCmp(LPVARIANT left
, LPVARIANT right
, LCID lcid
, DWORD flags
)
2846 VARTYPE lvt
, rvt
, vt
;
2851 TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left
), debugstr_variant(right
), lcid
, flags
);
2853 lvt
= V_VT(left
) & VT_TYPEMASK
;
2854 rvt
= V_VT(right
) & VT_TYPEMASK
;
2855 xmask
= (1 << lvt
) | (1 << rvt
);
2857 /* If we have any flag set except VT_RESERVED bail out.
2858 Same for the left input variant type > VT_INT and for the
2859 right input variant type > VT_I8. Yes, VT_INT is only supported
2860 as left variant. Go figure */
2861 if (((V_VT(left
) | V_VT(right
)) & ~VT_TYPEMASK
& ~VT_RESERVED
) ||
2862 lvt
> VT_INT
|| rvt
> VT_I8
) {
2863 return DISP_E_BADVARTYPE
;
2866 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2867 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2868 if (rvt
== VT_INT
|| xmask
& (VTBIT_I1
| VTBIT_UI2
| VTBIT_UI4
| VTBIT_UI8
|
2869 VTBIT_DISPATCH
| VTBIT_VARIANT
| VTBIT_UNKNOWN
| VTBIT_15
))
2870 return DISP_E_TYPEMISMATCH
;
2872 /* If both variants are VT_ERROR return VARCMP_EQ */
2873 if (xmask
== VTBIT_ERROR
)
2875 else if (xmask
& VTBIT_ERROR
)
2876 return DISP_E_TYPEMISMATCH
;
2878 if (xmask
& VTBIT_NULL
)
2884 /* Two BSTRs, ignore VT_RESERVED */
2885 if (xmask
== VTBIT_BSTR
)
2886 return VarBstrCmp(V_BSTR(left
), V_BSTR(right
), lcid
, flags
);
2888 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2889 if (xmask
& VTBIT_BSTR
) {
2890 VARIANT
*bstrv
, *nonbv
;
2894 /* Swap the variants so the BSTR is always on the left */
2895 if (lvt
== VT_BSTR
) {
2906 /* BSTR and EMPTY: ignore VT_RESERVED */
2907 if (nonbvt
== VT_EMPTY
)
2908 rc
= (!V_BSTR(bstrv
) || !*V_BSTR(bstrv
)) ? VARCMP_EQ
: VARCMP_GT
;
2910 VARTYPE breserv
= V_VT(bstrv
) & ~VT_TYPEMASK
;
2911 VARTYPE nreserv
= V_VT(nonbv
) & ~VT_TYPEMASK
;
2913 if (!breserv
&& !nreserv
)
2914 /* No VT_RESERVED set ==> BSTR always greater */
2916 else if (breserv
&& !nreserv
) {
2917 /* BSTR has VT_RESERVED set. Do a string comparison */
2918 rc
= VariantChangeTypeEx(&rv
,nonbv
,lcid
,0,VT_BSTR
);
2921 rc
= VarBstrCmp(V_BSTR(bstrv
), V_BSTR(&rv
), lcid
, flags
);
2923 } else if (V_BSTR(bstrv
) && *V_BSTR(bstrv
)) {
2924 /* Non NULL nor empty BSTR */
2925 /* If the BSTR is not a number the BSTR is greater */
2926 rc
= _VarChangeTypeExWrap(&lv
,bstrv
,lcid
,0,VT_R8
);
2929 else if (breserv
&& nreserv
)
2930 /* FIXME: This is strange: with both VT_RESERVED set it
2931 looks like the result depends only on the sign of
2933 rc
= (V_R8(&lv
) >= 0) ? VARCMP_GT
: VARCMP_LT
;
2935 /* Numeric comparison, will be handled below.
2936 VARCMP_NULL used only to break out. */
2941 /* Empty or NULL BSTR */
2944 /* Fixup the return code if we swapped left and right */
2946 if (rc
== VARCMP_GT
)
2948 else if (rc
== VARCMP_LT
)
2951 if (rc
!= VARCMP_NULL
)
2955 if (xmask
& VTBIT_DECIMAL
)
2957 else if (xmask
& VTBIT_BSTR
)
2959 else if (xmask
& VTBIT_R4
)
2961 else if (xmask
& (VTBIT_R8
| VTBIT_DATE
))
2963 else if (xmask
& VTBIT_CY
)
2969 /* Coerce the variants */
2970 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2971 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2972 /* Overflow, change to R8 */
2974 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2978 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2979 if (rc
== DISP_E_OVERFLOW
&& vt
!= VT_R8
) {
2980 /* Overflow, change to R8 */
2982 rc
= _VarChangeTypeExWrap(&lv
,left
,lcid
,0,vt
);
2985 rc
= _VarChangeTypeExWrap(&rv
,right
,lcid
,0,vt
);
2990 #define _VARCMP(a,b) \
2991 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2995 return VarCyCmp(V_CY(&lv
), V_CY(&rv
));
2997 return VarDecCmp(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
));
2999 return _VARCMP(V_I8(&lv
), V_I8(&rv
));
3001 return _VARCMP(V_R4(&lv
), V_R4(&rv
));
3003 return _VARCMP(V_R8(&lv
), V_R8(&rv
));
3005 /* We should never get here */
3011 static HRESULT
VARIANT_FetchDispatchValue(LPVARIANT pvDispatch
, LPVARIANT pValue
)
3014 static DISPPARAMS emptyParams
= { NULL
, NULL
, 0, 0 };
3016 if ((V_VT(pvDispatch
) & VT_TYPEMASK
) == VT_DISPATCH
) {
3017 if (NULL
== V_DISPATCH(pvDispatch
)) return DISP_E_TYPEMISMATCH
;
3018 hres
= IDispatch_Invoke(V_DISPATCH(pvDispatch
), DISPID_VALUE
, &IID_NULL
,
3019 LOCALE_USER_DEFAULT
, DISPATCH_PROPERTYGET
, &emptyParams
, pValue
,
3022 hres
= DISP_E_TYPEMISMATCH
;
3027 /**********************************************************************
3028 * VarAnd [OLEAUT32.142]
3030 * Computes the logical AND of two variants.
3033 * left [I] First variant
3034 * right [I] Second variant
3035 * result [O] Result variant
3039 * Failure: An HRESULT error code indicating the error.
3041 HRESULT WINAPI
VarAnd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3043 HRESULT hres
= S_OK
;
3044 VARTYPE resvt
= VT_EMPTY
;
3045 VARTYPE leftvt
,rightvt
;
3046 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3047 VARIANT varLeft
, varRight
;
3048 VARIANT tempLeft
, tempRight
;
3050 VariantInit(&varLeft
);
3051 VariantInit(&varRight
);
3052 VariantInit(&tempLeft
);
3053 VariantInit(&tempRight
);
3055 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3057 /* Handle VT_DISPATCH by storing and taking address of returned value */
3058 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3060 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3061 if (FAILED(hres
)) goto VarAnd_Exit
;
3064 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3066 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3067 if (FAILED(hres
)) goto VarAnd_Exit
;
3071 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3072 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3073 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3074 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3076 if (leftExtraFlags
!= rightExtraFlags
)
3078 hres
= DISP_E_BADVARTYPE
;
3081 ExtraFlags
= leftExtraFlags
;
3083 /* Native VarAnd always returns an error when using extra
3084 * flags or if the variant combination is I8 and INT.
3086 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
3087 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
3090 hres
= DISP_E_BADVARTYPE
;
3094 /* Determine return type */
3095 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
3097 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
3098 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
3099 leftvt
== VT_INT
|| rightvt
== VT_INT
||
3100 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
3101 leftvt
== VT_R4
|| rightvt
== VT_R4
||
3102 leftvt
== VT_R8
|| rightvt
== VT_R8
||
3103 leftvt
== VT_CY
|| rightvt
== VT_CY
||
3104 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
3105 leftvt
== VT_I1
|| rightvt
== VT_I1
||
3106 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
3107 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
3108 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
3109 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3111 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
||
3112 leftvt
== VT_I2
|| rightvt
== VT_I2
||
3113 leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
)
3114 if ((leftvt
== VT_NULL
&& rightvt
== VT_UI1
) ||
3115 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
3116 (leftvt
== VT_UI1
&& rightvt
== VT_UI1
))
3120 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3121 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
3123 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
||
3124 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
3128 hres
= DISP_E_BADVARTYPE
;
3132 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3135 * Special cases for when left variant is VT_NULL
3136 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
3138 if (leftvt
== VT_NULL
)
3143 case VT_I1
: if (V_I1(right
)) resvt
= VT_NULL
; break;
3144 case VT_UI1
: if (V_UI1(right
)) resvt
= VT_NULL
; break;
3145 case VT_I2
: if (V_I2(right
)) resvt
= VT_NULL
; break;
3146 case VT_UI2
: if (V_UI2(right
)) resvt
= VT_NULL
; break;
3147 case VT_I4
: if (V_I4(right
)) resvt
= VT_NULL
; break;
3148 case VT_UI4
: if (V_UI4(right
)) resvt
= VT_NULL
; break;
3149 case VT_I8
: if (V_I8(right
)) resvt
= VT_NULL
; break;
3150 case VT_UI8
: if (V_UI8(right
)) resvt
= VT_NULL
; break;
3151 case VT_INT
: if (V_INT(right
)) resvt
= VT_NULL
; break;
3152 case VT_UINT
: if (V_UINT(right
)) resvt
= VT_NULL
; break;
3153 case VT_BOOL
: if (V_BOOL(right
)) resvt
= VT_NULL
; break;
3154 case VT_R4
: if (V_R4(right
)) resvt
= VT_NULL
; break;
3155 case VT_R8
: if (V_R8(right
)) resvt
= VT_NULL
; break;
3157 if(V_CY(right
).int64
)
3161 if (DEC_HI32(&V_DECIMAL(right
)) ||
3162 DEC_LO64(&V_DECIMAL(right
)))
3166 hres
= VarBoolFromStr(V_BSTR(right
),
3167 LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
3171 V_VT(result
) = VT_NULL
;
3174 V_VT(result
) = VT_BOOL
;
3180 V_VT(result
) = resvt
;
3184 hres
= VariantCopy(&varLeft
, left
);
3185 if (FAILED(hres
)) goto VarAnd_Exit
;
3187 hres
= VariantCopy(&varRight
, right
);
3188 if (FAILED(hres
)) goto VarAnd_Exit
;
3190 if (resvt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
3191 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
3196 if (V_VT(&varLeft
) == VT_BSTR
&&
3197 FAILED(VarR8FromStr(V_BSTR(&varLeft
),
3198 LOCALE_USER_DEFAULT
, 0, &d
)))
3199 hres
= VariantChangeType(&varLeft
,&varLeft
,
3200 VARIANT_LOCALBOOL
, VT_BOOL
);
3201 if (SUCCEEDED(hres
) && V_VT(&varLeft
) != resvt
)
3202 hres
= VariantChangeType(&varLeft
,&varLeft
,0,resvt
);
3203 if (FAILED(hres
)) goto VarAnd_Exit
;
3206 if (resvt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
3207 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
3212 if (V_VT(&varRight
) == VT_BSTR
&&
3213 FAILED(VarR8FromStr(V_BSTR(&varRight
),
3214 LOCALE_USER_DEFAULT
, 0, &d
)))
3215 hres
= VariantChangeType(&varRight
, &varRight
,
3216 VARIANT_LOCALBOOL
, VT_BOOL
);
3217 if (SUCCEEDED(hres
) && V_VT(&varRight
) != resvt
)
3218 hres
= VariantChangeType(&varRight
, &varRight
, 0, resvt
);
3219 if (FAILED(hres
)) goto VarAnd_Exit
;
3222 V_VT(result
) = resvt
;
3226 V_I8(result
) = V_I8(&varLeft
) & V_I8(&varRight
);
3229 V_I4(result
) = V_I4(&varLeft
) & V_I4(&varRight
);
3232 V_I2(result
) = V_I2(&varLeft
) & V_I2(&varRight
);
3235 V_UI1(result
) = V_UI1(&varLeft
) & V_UI1(&varRight
);
3238 V_BOOL(result
) = V_BOOL(&varLeft
) & V_BOOL(&varRight
);
3241 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3246 VariantClear(&varLeft
);
3247 VariantClear(&varRight
);
3248 VariantClear(&tempLeft
);
3249 VariantClear(&tempRight
);
3254 /**********************************************************************
3255 * VarAdd [OLEAUT32.141]
3260 * left [I] First variant
3261 * right [I] Second variant
3262 * result [O] Result variant
3266 * Failure: An HRESULT error code indicating the error.
3269 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3270 * UI8, INT and UINT as input variants.
3272 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3276 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3279 HRESULT WINAPI
VarAdd(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3282 VARTYPE lvt
, rvt
, resvt
, tvt
;
3284 VARIANT tempLeft
, tempRight
;
3287 /* Variant priority for coercion. Sorted from lowest to highest.
3288 VT_ERROR shows an invalid input variant type. */
3289 enum coerceprio
{ vt_EMPTY
, vt_UI1
, vt_I2
, vt_I4
, vt_I8
, vt_BSTR
,vt_R4
,
3290 vt_R8
, vt_CY
, vt_DATE
, vt_DECIMAL
, vt_DISPATCH
, vt_NULL
,
3292 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3293 static const VARTYPE prio2vt
[] = { VT_EMPTY
, VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_BSTR
, VT_R4
,
3294 VT_R8
, VT_CY
, VT_DATE
, VT_DECIMAL
, VT_DISPATCH
,
3295 VT_NULL
, VT_ERROR
};
3297 /* Mapping for coercion from input variant to priority of result variant. */
3298 static const VARTYPE coerce
[] = {
3299 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3300 vt_EMPTY
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3301 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3302 vt_R8
, vt_CY
, vt_DATE
, vt_BSTR
, vt_DISPATCH
,
3303 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3304 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3305 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3306 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3309 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3314 VariantInit(&tempLeft
);
3315 VariantInit(&tempRight
);
3317 /* Handle VT_DISPATCH by storing and taking address of returned value */
3318 if ((V_VT(left
) & VT_TYPEMASK
) != VT_NULL
&& (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3320 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3322 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3323 if (FAILED(hres
)) goto end
;
3326 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3328 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3329 if (FAILED(hres
)) goto end
;
3334 lvt
= V_VT(left
)&VT_TYPEMASK
;
3335 rvt
= V_VT(right
)&VT_TYPEMASK
;
3337 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3338 Same for any input variant type > VT_I8 */
3339 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3340 lvt
> VT_I8
|| rvt
> VT_I8
) {
3341 hres
= DISP_E_BADVARTYPE
;
3345 /* Determine the variant type to coerce to. */
3346 if (coerce
[lvt
] > coerce
[rvt
]) {
3347 resvt
= prio2vt
[coerce
[lvt
]];
3348 tvt
= prio2vt
[coerce
[rvt
]];
3350 resvt
= prio2vt
[coerce
[rvt
]];
3351 tvt
= prio2vt
[coerce
[lvt
]];
3354 /* Special cases where the result variant type is defined by both
3355 input variants and not only that with the highest priority */
3356 if (resvt
== VT_BSTR
) {
3357 if (tvt
== VT_EMPTY
|| tvt
== VT_BSTR
)
3362 if (resvt
== VT_R4
&& (tvt
== VT_BSTR
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3365 /* For overflow detection use the biggest compatible type for the
3369 hres
= DISP_E_BADVARTYPE
;
3373 V_VT(result
) = VT_NULL
;
3376 FIXME("cannot handle variant type VT_DISPATCH\n");
3377 hres
= DISP_E_TYPEMISMATCH
;
3396 /* Now coerce the variants */
3397 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3400 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3406 V_VT(result
) = resvt
;
3409 hres
= VarDecAdd(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3410 &V_DECIMAL(result
));
3413 hres
= VarCyAdd(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3416 /* We do not add those, we concatenate them. */
3417 hres
= VarBstrCat(V_BSTR(&lv
), V_BSTR(&rv
), &V_BSTR(result
));
3420 /* Overflow detection */
3421 r8res
= (double)V_I8(&lv
) + (double)V_I8(&rv
);
3422 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3423 V_VT(result
) = VT_R8
;
3424 V_R8(result
) = r8res
;
3428 V_I8(&tv
) = V_I8(&lv
) + V_I8(&rv
);
3433 /* FIXME: overflow detection */
3434 V_R8(&tv
) = V_R8(&lv
) + V_R8(&rv
);
3437 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3441 if ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3442 /* Overflow! Change to the vartype with the next higher priority.
3443 With one exception: I4 ==> R8 even if it would fit in I8 */
3447 resvt
= prio2vt
[coerce
[resvt
] + 1];
3448 hres
= VariantChangeType(result
, &tv
, 0, resvt
);
3451 hres
= VariantCopy(result
, &tv
);
3455 V_VT(result
) = VT_EMPTY
;
3456 V_I4(result
) = 0; /* No V_EMPTY */
3461 VariantClear(&tempLeft
);
3462 VariantClear(&tempRight
);
3463 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3467 /**********************************************************************
3468 * VarMul [OLEAUT32.156]
3470 * Multiply two variants.
3473 * left [I] First variant
3474 * right [I] Second variant
3475 * result [O] Result variant
3479 * Failure: An HRESULT error code indicating the error.
3482 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3483 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3485 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3489 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3492 HRESULT WINAPI
VarMul(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3495 VARTYPE lvt
, rvt
, resvt
, tvt
;
3497 VARIANT tempLeft
, tempRight
;
3500 /* Variant priority for coercion. Sorted from lowest to highest.
3501 VT_ERROR shows an invalid input variant type. */
3502 enum coerceprio
{ vt_UI1
= 0, vt_I2
, vt_I4
, vt_I8
, vt_CY
, vt_R4
, vt_R8
,
3503 vt_DECIMAL
, vt_NULL
, vt_ERROR
};
3504 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3505 static const VARTYPE prio2vt
[] = { VT_UI1
, VT_I2
, VT_I4
, VT_I8
, VT_CY
, VT_R4
, VT_R8
,
3506 VT_DECIMAL
, VT_NULL
, VT_ERROR
};
3508 /* Mapping for coercion from input variant to priority of result variant. */
3509 static const VARTYPE coerce
[] = {
3510 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3511 vt_UI1
, vt_NULL
, vt_I2
, vt_I4
, vt_R4
,
3512 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3513 vt_R8
, vt_CY
, vt_R8
, vt_R8
, vt_ERROR
,
3514 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3515 vt_ERROR
, vt_I2
, vt_ERROR
, vt_ERROR
, vt_DECIMAL
,
3516 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3517 vt_ERROR
, vt_ERROR
, vt_UI1
, vt_ERROR
, vt_ERROR
, vt_I8
3520 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3525 VariantInit(&tempLeft
);
3526 VariantInit(&tempRight
);
3528 /* Handle VT_DISPATCH by storing and taking address of returned value */
3529 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3531 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3532 if (FAILED(hres
)) goto end
;
3535 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3537 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3538 if (FAILED(hres
)) goto end
;
3542 lvt
= V_VT(left
)&VT_TYPEMASK
;
3543 rvt
= V_VT(right
)&VT_TYPEMASK
;
3545 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3546 Same for any input variant type > VT_I8 */
3547 if (V_VT(left
) & ~VT_TYPEMASK
|| V_VT(right
) & ~VT_TYPEMASK
||
3548 lvt
> VT_I8
|| rvt
> VT_I8
) {
3549 hres
= DISP_E_BADVARTYPE
;
3553 /* Determine the variant type to coerce to. */
3554 if (coerce
[lvt
] > coerce
[rvt
]) {
3555 resvt
= prio2vt
[coerce
[lvt
]];
3556 tvt
= prio2vt
[coerce
[rvt
]];
3558 resvt
= prio2vt
[coerce
[rvt
]];
3559 tvt
= prio2vt
[coerce
[lvt
]];
3562 /* Special cases where the result variant type is defined by both
3563 input variants and not only that with the highest priority */
3564 if (resvt
== VT_R4
&& (tvt
== VT_CY
|| tvt
== VT_I8
|| tvt
== VT_I4
))
3566 if (lvt
== VT_EMPTY
&& rvt
== VT_EMPTY
)
3569 /* For overflow detection use the biggest compatible type for the
3573 hres
= DISP_E_BADVARTYPE
;
3577 V_VT(result
) = VT_NULL
;
3592 /* Now coerce the variants */
3593 hres
= VariantChangeType(&lv
, left
, 0, tvt
);
3596 hres
= VariantChangeType(&rv
, right
, 0, tvt
);
3603 V_VT(result
) = resvt
;
3606 hres
= VarDecMul(&V_DECIMAL(&lv
), &V_DECIMAL(&rv
),
3607 &V_DECIMAL(result
));
3610 hres
= VarCyMul(V_CY(&lv
), V_CY(&rv
), &V_CY(result
));
3613 /* Overflow detection */
3614 r8res
= (double)V_I8(&lv
) * (double)V_I8(&rv
);
3615 if (r8res
> (double)I8_MAX
|| r8res
< (double)I8_MIN
) {
3616 V_VT(result
) = VT_R8
;
3617 V_R8(result
) = r8res
;
3620 V_I8(&tv
) = V_I8(&lv
) * V_I8(&rv
);
3623 /* FIXME: overflow detection */
3624 V_R8(&tv
) = V_R8(&lv
) * V_R8(&rv
);
3627 ERR("We shouldn't get here! tvt = %d!\n", tvt
);
3631 while ((hres
= VariantChangeType(result
, &tv
, 0, resvt
)) != S_OK
) {
3632 /* Overflow! Change to the vartype with the next higher priority.
3633 With one exception: I4 ==> R8 even if it would fit in I8 */
3637 resvt
= prio2vt
[coerce
[resvt
] + 1];
3640 hres
= VariantCopy(result
, &tv
);
3644 V_VT(result
) = VT_EMPTY
;
3645 V_I4(result
) = 0; /* No V_EMPTY */
3650 VariantClear(&tempLeft
);
3651 VariantClear(&tempRight
);
3652 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3656 /**********************************************************************
3657 * VarDiv [OLEAUT32.143]
3659 * Divides one variant with another.
3662 * left [I] First variant
3663 * right [I] Second variant
3664 * result [O] Result variant
3668 * Failure: An HRESULT error code indicating the error.
3670 HRESULT WINAPI
VarDiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3672 HRESULT hres
= S_OK
;
3673 VARTYPE resvt
= VT_EMPTY
;
3674 VARTYPE leftvt
,rightvt
;
3675 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3677 VARIANT tempLeft
, tempRight
;
3679 VariantInit(&tempLeft
);
3680 VariantInit(&tempRight
);
3684 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3686 /* Handle VT_DISPATCH by storing and taking address of returned value */
3687 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
3689 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3690 if (FAILED(hres
)) goto end
;
3693 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
3695 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3696 if (FAILED(hres
)) goto end
;
3700 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3701 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3702 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3703 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3705 if (leftExtraFlags
!= rightExtraFlags
)
3707 hres
= DISP_E_BADVARTYPE
;
3710 ExtraFlags
= leftExtraFlags
;
3712 /* Native VarDiv always returns an error when using extra flags */
3713 if (ExtraFlags
!= 0)
3715 hres
= DISP_E_BADVARTYPE
;
3719 /* Determine return type */
3720 if (!(rightvt
== VT_EMPTY
))
3722 if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3724 V_VT(result
) = VT_NULL
;
3728 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3730 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
||
3731 leftvt
== VT_CY
|| rightvt
== VT_CY
||
3732 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
3733 leftvt
== VT_I4
|| rightvt
== VT_I4
||
3734 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
3735 leftvt
== VT_I2
|| rightvt
== VT_I2
||
3736 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
3737 leftvt
== VT_R8
|| rightvt
== VT_R8
||
3738 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
3740 if ((leftvt
== VT_UI1
&& rightvt
== VT_R4
) ||
3741 (leftvt
== VT_R4
&& rightvt
== VT_UI1
))
3743 else if ((leftvt
== VT_R4
&& (rightvt
== VT_BOOL
||
3744 rightvt
== VT_I2
)) || (rightvt
== VT_R4
&&
3745 (leftvt
== VT_BOOL
|| leftvt
== VT_I2
)))
3750 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
3753 else if (leftvt
== VT_NULL
&& rightvt
== VT_EMPTY
)
3755 V_VT(result
) = VT_NULL
;
3761 hres
= DISP_E_BADVARTYPE
;
3765 /* coerce to the result type */
3766 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
3767 if (hres
!= S_OK
) goto end
;
3769 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
3770 if (hres
!= S_OK
) goto end
;
3773 V_VT(result
) = resvt
;
3777 if (V_R4(&lv
) == 0.0 && V_R4(&rv
) == 0.0)
3779 hres
= DISP_E_OVERFLOW
;
3780 V_VT(result
) = VT_EMPTY
;
3782 else if (V_R4(&rv
) == 0.0)
3784 hres
= DISP_E_DIVBYZERO
;
3785 V_VT(result
) = VT_EMPTY
;
3788 V_R4(result
) = V_R4(&lv
) / V_R4(&rv
);
3791 if (V_R8(&lv
) == 0.0 && V_R8(&rv
) == 0.0)
3793 hres
= DISP_E_OVERFLOW
;
3794 V_VT(result
) = VT_EMPTY
;
3796 else if (V_R8(&rv
) == 0.0)
3798 hres
= DISP_E_DIVBYZERO
;
3799 V_VT(result
) = VT_EMPTY
;
3802 V_R8(result
) = V_R8(&lv
) / V_R8(&rv
);
3805 hres
= VarDecDiv(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
3812 VariantClear(&tempLeft
);
3813 VariantClear(&tempRight
);
3814 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
3818 /**********************************************************************
3819 * VarSub [OLEAUT32.159]
3821 * Subtract two variants.
3824 * left [I] First variant
3825 * right [I] Second variant
3826 * result [O] Result variant
3830 * Failure: An HRESULT error code indicating the error.
3832 HRESULT WINAPI
VarSub(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
3834 HRESULT hres
= S_OK
;
3835 VARTYPE resvt
= VT_EMPTY
;
3836 VARTYPE leftvt
,rightvt
;
3837 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
3839 VARIANT tempLeft
, tempRight
;
3843 VariantInit(&tempLeft
);
3844 VariantInit(&tempRight
);
3846 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
3848 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3849 (V_VT(left
)&(~VT_TYPEMASK
)) == 0 &&
3850 (V_VT(right
) & VT_TYPEMASK
) != VT_NULL
)
3852 if (NULL
== V_DISPATCH(left
)) {
3853 if ((V_VT(right
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3854 hres
= DISP_E_BADVARTYPE
;
3855 else if ((V_VT(right
) & VT_TYPEMASK
) >= VT_UI8
&&
3856 (V_VT(right
) & VT_TYPEMASK
) < VT_RECORD
)
3857 hres
= DISP_E_BADVARTYPE
;
3858 else switch (V_VT(right
) & VT_TYPEMASK
)
3866 hres
= DISP_E_BADVARTYPE
;
3868 if (FAILED(hres
)) goto end
;
3870 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
3871 if (FAILED(hres
)) goto end
;
3874 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
&&
3875 (V_VT(right
)&(~VT_TYPEMASK
)) == 0 &&
3876 (V_VT(left
) & VT_TYPEMASK
) != VT_NULL
)
3878 if (NULL
== V_DISPATCH(right
))
3880 if ((V_VT(left
) & VT_TYPEMASK
) >= VT_INT_PTR
)
3881 hres
= DISP_E_BADVARTYPE
;
3882 else if ((V_VT(left
) & VT_TYPEMASK
) >= VT_UI8
&&
3883 (V_VT(left
) & VT_TYPEMASK
) < VT_RECORD
)
3884 hres
= DISP_E_BADVARTYPE
;
3885 else switch (V_VT(left
) & VT_TYPEMASK
)
3893 hres
= DISP_E_BADVARTYPE
;
3895 if (FAILED(hres
)) goto end
;
3897 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
3898 if (FAILED(hres
)) goto end
;
3902 leftvt
= V_VT(left
)&VT_TYPEMASK
;
3903 rightvt
= V_VT(right
)&VT_TYPEMASK
;
3904 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
3905 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
3907 if (leftExtraFlags
!= rightExtraFlags
)
3909 hres
= DISP_E_BADVARTYPE
;
3912 ExtraFlags
= leftExtraFlags
;
3914 /* determine return type and return code */
3915 /* All extra flags produce errors */
3916 if (ExtraFlags
== (VT_VECTOR
|VT_BYREF
|VT_RESERVED
) ||
3917 ExtraFlags
== (VT_VECTOR
|VT_RESERVED
) ||
3918 ExtraFlags
== (VT_VECTOR
|VT_BYREF
) ||
3919 ExtraFlags
== (VT_BYREF
|VT_RESERVED
) ||
3920 ExtraFlags
== VT_VECTOR
||
3921 ExtraFlags
== VT_BYREF
||
3922 ExtraFlags
== VT_RESERVED
)
3924 hres
= DISP_E_BADVARTYPE
;
3927 else if (ExtraFlags
>= VT_ARRAY
)
3929 hres
= DISP_E_TYPEMISMATCH
;
3932 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3933 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3934 else if (leftvt
== VT_CLSID
|| rightvt
== VT_CLSID
||
3935 leftvt
== VT_VARIANT
|| rightvt
== VT_VARIANT
||
3936 leftvt
== VT_I1
|| rightvt
== VT_I1
||
3937 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
3938 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
3939 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
3940 leftvt
== VT_INT
|| rightvt
== VT_INT
||
3941 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
3942 leftvt
== VT_UNKNOWN
|| rightvt
== VT_UNKNOWN
||
3943 leftvt
== VT_RECORD
|| rightvt
== VT_RECORD
)
3945 if (leftvt
== VT_RECORD
&& rightvt
== VT_I8
)
3946 hres
= DISP_E_TYPEMISMATCH
;
3947 else if (leftvt
< VT_UI1
&& rightvt
== VT_RECORD
)
3948 hres
= DISP_E_TYPEMISMATCH
;
3949 else if (leftvt
>= VT_UI1
&& rightvt
== VT_RECORD
)
3950 hres
= DISP_E_TYPEMISMATCH
;
3951 else if (leftvt
== VT_RECORD
&& rightvt
<= VT_UI1
)
3952 hres
= DISP_E_TYPEMISMATCH
;
3953 else if (leftvt
== VT_RECORD
&& rightvt
> VT_UI1
)
3954 hres
= DISP_E_BADVARTYPE
;
3956 hres
= DISP_E_BADVARTYPE
;
3959 /* The following flags/types are invalid for left variant */
3960 else if (!((leftvt
<= VT_LPWSTR
|| leftvt
== VT_RECORD
||
3961 leftvt
== VT_CLSID
) && leftvt
!= (VARTYPE
)15 /* undefined vt */ &&
3962 (leftvt
< VT_VOID
|| leftvt
> VT_LPWSTR
)))
3964 hres
= DISP_E_BADVARTYPE
;
3967 /* The following flags/types are invalid for right variant */
3968 else if (!((rightvt
<= VT_LPWSTR
|| rightvt
== VT_RECORD
||
3969 rightvt
== VT_CLSID
) && rightvt
!= (VARTYPE
)15 /* undefined vt */ &&
3970 (rightvt
< VT_VOID
|| rightvt
> VT_LPWSTR
)))
3972 hres
= DISP_E_BADVARTYPE
;
3975 else if ((leftvt
== VT_NULL
&& rightvt
== VT_DISPATCH
) ||
3976 (leftvt
== VT_DISPATCH
&& rightvt
== VT_NULL
))
3978 else if (leftvt
== VT_DISPATCH
|| rightvt
== VT_DISPATCH
||
3979 leftvt
== VT_ERROR
|| rightvt
== VT_ERROR
)
3981 hres
= DISP_E_TYPEMISMATCH
;
3984 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
3986 else if ((leftvt
== VT_EMPTY
&& rightvt
== VT_BSTR
) ||
3987 (leftvt
== VT_DATE
&& rightvt
== VT_DATE
) ||
3988 (leftvt
== VT_BSTR
&& rightvt
== VT_EMPTY
) ||
3989 (leftvt
== VT_BSTR
&& rightvt
== VT_BSTR
))
3991 else if (leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
)
3993 else if (leftvt
== VT_DATE
|| rightvt
== VT_DATE
)
3995 else if (leftvt
== VT_CY
|| rightvt
== VT_CY
)
3997 else if (leftvt
== VT_R8
|| rightvt
== VT_R8
)
3999 else if (leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
4001 else if (leftvt
== VT_R4
|| rightvt
== VT_R4
)
4003 if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
4004 leftvt
== VT_I8
|| rightvt
== VT_I8
)
4009 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
4011 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
)
4013 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
4014 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
4015 (leftvt
== VT_EMPTY
&& rightvt
== VT_EMPTY
))
4017 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
4021 hres
= DISP_E_TYPEMISMATCH
;
4025 /* coerce to the result type */
4026 if (leftvt
== VT_BSTR
&& rightvt
== VT_DATE
)
4027 hres
= VariantChangeType(&lv
, left
, 0, VT_R8
);
4029 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
4030 if (hres
!= S_OK
) goto end
;
4031 if (leftvt
== VT_DATE
&& rightvt
== VT_BSTR
)
4032 hres
= VariantChangeType(&rv
, right
, 0, VT_R8
);
4034 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
4035 if (hres
!= S_OK
) goto end
;
4038 V_VT(result
) = resvt
;
4044 V_DATE(result
) = V_DATE(&lv
) - V_DATE(&rv
);
4047 hres
= VarCySub(V_CY(&lv
), V_CY(&rv
), &(V_CY(result
)));
4050 V_R4(result
) = V_R4(&lv
) - V_R4(&rv
);
4053 V_I8(result
) = V_I8(&lv
) - V_I8(&rv
);
4056 V_I4(result
) = V_I4(&lv
) - V_I4(&rv
);
4059 V_I2(result
) = V_I2(&lv
) - V_I2(&rv
);
4062 V_I1(result
) = V_I1(&lv
) - V_I1(&rv
);
4065 V_UI1(result
) = V_UI2(&lv
) - V_UI1(&rv
);
4068 V_R8(result
) = V_R8(&lv
) - V_R8(&rv
);
4071 hres
= VarDecSub(&(V_DECIMAL(&lv
)), &(V_DECIMAL(&rv
)), &(V_DECIMAL(result
)));
4078 VariantClear(&tempLeft
);
4079 VariantClear(&tempRight
);
4080 TRACE("returning 0x%8x %s\n", hres
, debugstr_variant(result
));
4085 /**********************************************************************
4086 * VarOr [OLEAUT32.157]
4088 * Perform a logical or (OR) operation on two variants.
4091 * pVarLeft [I] First variant
4092 * pVarRight [I] Variant to OR with pVarLeft
4093 * pVarOut [O] Destination for OR result
4096 * Success: S_OK. pVarOut contains the result of the operation with its type
4097 * taken from the table listed under VarXor().
4098 * Failure: An HRESULT error code indicating the error.
4101 * See the Notes section of VarXor() for further information.
4103 HRESULT WINAPI
VarOr(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4106 VARIANT varLeft
, varRight
, varStr
;
4108 VARIANT tempLeft
, tempRight
;
4110 VariantInit(&tempLeft
);
4111 VariantInit(&tempRight
);
4112 VariantInit(&varLeft
);
4113 VariantInit(&varRight
);
4114 VariantInit(&varStr
);
4116 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4118 /* Handle VT_DISPATCH by storing and taking address of returned value */
4119 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
4121 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
4122 if (FAILED(hRet
)) goto VarOr_Exit
;
4123 pVarLeft
= &tempLeft
;
4125 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
4127 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
4128 if (FAILED(hRet
)) goto VarOr_Exit
;
4129 pVarRight
= &tempRight
;
4132 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
4133 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
4134 V_VT(pVarLeft
) == VT_DISPATCH
|| V_VT(pVarRight
) == VT_DISPATCH
||
4135 V_VT(pVarLeft
) == VT_RECORD
|| V_VT(pVarRight
) == VT_RECORD
)
4137 hRet
= DISP_E_BADVARTYPE
;
4141 V_VT(&varLeft
) = V_VT(&varRight
) = V_VT(&varStr
) = VT_EMPTY
;
4143 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
4145 /* NULL OR Zero is NULL, NULL OR value is value */
4146 if (V_VT(pVarLeft
) == VT_NULL
)
4147 pVarLeft
= pVarRight
; /* point to the non-NULL var */
4149 V_VT(pVarOut
) = VT_NULL
;
4152 switch (V_VT(pVarLeft
))
4154 case VT_DATE
: case VT_R8
:
4160 if (V_BOOL(pVarLeft
))
4161 *pVarOut
= *pVarLeft
;
4164 case VT_I2
: case VT_UI2
:
4175 if (V_UI1(pVarLeft
))
4176 *pVarOut
= *pVarLeft
;
4184 case VT_I4
: case VT_UI4
: case VT_INT
: case VT_UINT
:
4190 if (V_CY(pVarLeft
).int64
)
4194 case VT_I8
: case VT_UI8
:
4200 if (DEC_HI32(&V_DECIMAL(pVarLeft
)) || DEC_LO64(&V_DECIMAL(pVarLeft
)))
4208 if (!V_BSTR(pVarLeft
))
4210 hRet
= DISP_E_BADVARTYPE
;
4214 hRet
= VarBoolFromStr(V_BSTR(pVarLeft
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
4215 if (SUCCEEDED(hRet
) && b
)
4217 V_VT(pVarOut
) = VT_BOOL
;
4218 V_BOOL(pVarOut
) = b
;
4222 case VT_NULL
: case VT_EMPTY
:
4223 V_VT(pVarOut
) = VT_NULL
;
4227 hRet
= DISP_E_BADVARTYPE
;
4232 if (V_VT(pVarLeft
) == VT_EMPTY
|| V_VT(pVarRight
) == VT_EMPTY
)
4234 if (V_VT(pVarLeft
) == VT_EMPTY
)
4235 pVarLeft
= pVarRight
; /* point to the non-EMPTY var */
4238 /* Since one argument is empty (0), OR'ing it with the other simply
4239 * gives the others value (as 0|x => x). So just convert the other
4240 * argument to the required result type.
4242 switch (V_VT(pVarLeft
))
4245 if (!V_BSTR(pVarLeft
))
4247 hRet
= DISP_E_BADVARTYPE
;
4251 hRet
= VariantCopy(&varStr
, pVarLeft
);
4255 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4258 /* Fall Through ... */
4259 case VT_EMPTY
: case VT_UI1
: case VT_BOOL
: case VT_I2
:
4260 V_VT(pVarOut
) = VT_I2
;
4262 case VT_DATE
: case VT_CY
: case VT_DECIMAL
: case VT_R4
: case VT_R8
:
4263 case VT_I1
: case VT_UI2
: case VT_I4
: case VT_UI4
:
4264 case VT_INT
: case VT_UINT
: case VT_UI8
:
4265 V_VT(pVarOut
) = VT_I4
;
4268 V_VT(pVarOut
) = VT_I8
;
4271 hRet
= DISP_E_BADVARTYPE
;
4274 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4277 pVarLeft
= &varLeft
;
4278 hRet
= VariantChangeType(pVarOut
, pVarLeft
, 0, V_VT(pVarOut
));
4282 if (V_VT(pVarLeft
) == VT_BOOL
&& V_VT(pVarRight
) == VT_BOOL
)
4284 V_VT(pVarOut
) = VT_BOOL
;
4285 V_BOOL(pVarOut
) = V_BOOL(pVarLeft
) | V_BOOL(pVarRight
);
4290 if (V_VT(pVarLeft
) == VT_UI1
&& V_VT(pVarRight
) == VT_UI1
)
4292 V_VT(pVarOut
) = VT_UI1
;
4293 V_UI1(pVarOut
) = V_UI1(pVarLeft
) | V_UI1(pVarRight
);
4298 if (V_VT(pVarLeft
) == VT_BSTR
)
4300 hRet
= VariantCopy(&varStr
, pVarLeft
);
4304 hRet
= VariantChangeType(pVarLeft
, pVarLeft
, 0, VT_BOOL
);
4309 if (V_VT(pVarLeft
) == VT_BOOL
&&
4310 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_BSTR
))
4314 else if ((V_VT(pVarLeft
) == VT_BOOL
|| V_VT(pVarLeft
) == VT_UI1
||
4315 V_VT(pVarLeft
) == VT_I2
|| V_VT(pVarLeft
) == VT_BSTR
) &&
4316 (V_VT(pVarRight
) == VT_BOOL
|| V_VT(pVarRight
) == VT_UI1
||
4317 V_VT(pVarRight
) == VT_I2
|| V_VT(pVarRight
) == VT_BSTR
))
4321 else if (V_VT(pVarLeft
) == VT_I8
|| V_VT(pVarRight
) == VT_I8
)
4323 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4325 hRet
= DISP_E_TYPEMISMATCH
;
4331 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4335 hRet
= VariantCopy(&varRight
, pVarRight
);
4339 if (vt
== VT_I4
&& V_VT(&varLeft
) == VT_UI4
)
4340 V_VT(&varLeft
) = VT_I4
; /* Don't overflow */
4345 if (V_VT(&varLeft
) == VT_BSTR
&&
4346 FAILED(VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
)))
4347 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
, VT_BOOL
);
4348 if (SUCCEEDED(hRet
) && V_VT(&varLeft
) != vt
)
4349 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4354 if (vt
== VT_I4
&& V_VT(&varRight
) == VT_UI4
)
4355 V_VT(&varRight
) = VT_I4
; /* Don't overflow */
4360 if (V_VT(&varRight
) == VT_BSTR
&&
4361 FAILED(VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
)))
4362 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
, VT_BOOL
);
4363 if (SUCCEEDED(hRet
) && V_VT(&varRight
) != vt
)
4364 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4372 V_I8(pVarOut
) = V_I8(&varLeft
) | V_I8(&varRight
);
4374 else if (vt
== VT_I4
)
4376 V_I4(pVarOut
) = V_I4(&varLeft
) | V_I4(&varRight
);
4380 V_I2(pVarOut
) = V_I2(&varLeft
) | V_I2(&varRight
);
4384 VariantClear(&varStr
);
4385 VariantClear(&varLeft
);
4386 VariantClear(&varRight
);
4387 VariantClear(&tempLeft
);
4388 VariantClear(&tempRight
);
4392 /**********************************************************************
4393 * VarAbs [OLEAUT32.168]
4395 * Convert a variant to its absolute value.
4398 * pVarIn [I] Source variant
4399 * pVarOut [O] Destination for converted value
4402 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4403 * Failure: An HRESULT error code indicating the error.
4406 * - This function does not process by-reference variants.
4407 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4408 * according to the following table:
4409 *| Input Type Output Type
4410 *| ---------- -----------
4413 *| (All others) Unchanged
4415 HRESULT WINAPI
VarAbs(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4418 HRESULT hRet
= S_OK
;
4423 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4425 /* Handle VT_DISPATCH by storing and taking address of returned value */
4426 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4428 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4429 if (FAILED(hRet
)) goto VarAbs_Exit
;
4433 if (V_ISARRAY(pVarIn
) || V_VT(pVarIn
) == VT_UNKNOWN
||
4434 V_VT(pVarIn
) == VT_DISPATCH
|| V_VT(pVarIn
) == VT_RECORD
||
4435 V_VT(pVarIn
) == VT_ERROR
)
4437 hRet
= DISP_E_TYPEMISMATCH
;
4440 *pVarOut
= *pVarIn
; /* Shallow copy the value, and invert it if needed */
4442 #define ABS_CASE(typ,min) \
4443 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4444 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4447 switch (V_VT(pVarIn
))
4449 ABS_CASE(I1
,I1_MIN
);
4451 V_VT(pVarOut
) = VT_I2
;
4452 /* BOOL->I2, Fall through ... */
4453 ABS_CASE(I2
,I2_MIN
);
4455 ABS_CASE(I4
,I4_MIN
);
4456 ABS_CASE(I8
,I8_MIN
);
4457 ABS_CASE(R4
,R4_MIN
);
4459 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
4462 V_VT(pVarOut
) = VT_R8
;
4464 /* Fall through ... */
4466 ABS_CASE(R8
,R8_MIN
);
4468 hRet
= VarCyAbs(V_CY(pVarIn
), & V_CY(pVarOut
));
4471 DEC_SIGN(&V_DECIMAL(pVarOut
)) &= ~DECIMAL_NEG
;
4481 V_VT(pVarOut
) = VT_I2
;
4486 hRet
= DISP_E_BADVARTYPE
;
4490 VariantClear(&temp
);
4494 /**********************************************************************
4495 * VarFix [OLEAUT32.169]
4497 * Truncate a variants value to a whole number.
4500 * pVarIn [I] Source variant
4501 * pVarOut [O] Destination for converted value
4504 * Success: S_OK. pVarOut contains the converted value.
4505 * Failure: An HRESULT error code indicating the error.
4508 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4509 * according to the following table:
4510 *| Input Type Output Type
4511 *| ---------- -----------
4515 *| All Others Unchanged
4516 * - The difference between this function and VarInt() is that VarInt() rounds
4517 * negative numbers away from 0, while this function rounds them towards zero.
4519 HRESULT WINAPI
VarFix(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4521 HRESULT hRet
= S_OK
;
4526 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4528 /* Handle VT_DISPATCH by storing and taking address of returned value */
4529 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4531 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4532 if (FAILED(hRet
)) goto VarFix_Exit
;
4535 V_VT(pVarOut
) = V_VT(pVarIn
);
4537 switch (V_VT(pVarIn
))
4540 V_UI1(pVarOut
) = V_UI1(pVarIn
);
4543 V_VT(pVarOut
) = VT_I2
;
4546 V_I2(pVarOut
) = V_I2(pVarIn
);
4549 V_I4(pVarOut
) = V_I4(pVarIn
);
4552 V_I8(pVarOut
) = V_I8(pVarIn
);
4555 if (V_R4(pVarIn
) < 0.0f
)
4556 V_R4(pVarOut
) = (float)ceil(V_R4(pVarIn
));
4558 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4561 V_VT(pVarOut
) = VT_R8
;
4562 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4567 if (V_R8(pVarIn
) < 0.0)
4568 V_R8(pVarOut
) = ceil(V_R8(pVarIn
));
4570 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4573 hRet
= VarCyFix(V_CY(pVarIn
), &V_CY(pVarOut
));
4576 hRet
= VarDecFix(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4579 V_VT(pVarOut
) = VT_I2
;
4586 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
4587 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
4588 hRet
= DISP_E_BADVARTYPE
;
4590 hRet
= DISP_E_TYPEMISMATCH
;
4594 V_VT(pVarOut
) = VT_EMPTY
;
4595 VariantClear(&temp
);
4600 /**********************************************************************
4601 * VarInt [OLEAUT32.172]
4603 * Truncate a variants value to a whole number.
4606 * pVarIn [I] Source variant
4607 * pVarOut [O] Destination for converted value
4610 * Success: S_OK. pVarOut contains the converted value.
4611 * Failure: An HRESULT error code indicating the error.
4614 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4615 * according to the following table:
4616 *| Input Type Output Type
4617 *| ---------- -----------
4621 *| All Others Unchanged
4622 * - The difference between this function and VarFix() is that VarFix() rounds
4623 * negative numbers towards 0, while this function rounds them away from zero.
4625 HRESULT WINAPI
VarInt(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4627 HRESULT hRet
= S_OK
;
4632 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4634 /* Handle VT_DISPATCH by storing and taking address of returned value */
4635 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4637 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4638 if (FAILED(hRet
)) goto VarInt_Exit
;
4641 V_VT(pVarOut
) = V_VT(pVarIn
);
4643 switch (V_VT(pVarIn
))
4646 V_R4(pVarOut
) = (float)floor(V_R4(pVarIn
));
4649 V_VT(pVarOut
) = VT_R8
;
4650 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
4655 V_R8(pVarOut
) = floor(V_R8(pVarIn
));
4658 hRet
= VarCyInt(V_CY(pVarIn
), &V_CY(pVarOut
));
4661 hRet
= VarDecInt(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
4664 hRet
= VarFix(pVarIn
, pVarOut
);
4667 VariantClear(&temp
);
4672 /**********************************************************************
4673 * VarXor [OLEAUT32.167]
4675 * Perform a logical exclusive-or (XOR) operation on two variants.
4678 * pVarLeft [I] First variant
4679 * pVarRight [I] Variant to XOR with pVarLeft
4680 * pVarOut [O] Destination for XOR result
4683 * Success: S_OK. pVarOut contains the result of the operation with its type
4684 * taken from the table below).
4685 * Failure: An HRESULT error code indicating the error.
4688 * - Neither pVarLeft or pVarRight are modified by this function.
4689 * - This function does not process by-reference variants.
4690 * - Input types of VT_BSTR may be numeric strings or boolean text.
4691 * - The type of result stored in pVarOut depends on the types of pVarLeft
4692 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4693 * or VT_NULL if the function succeeds.
4694 * - Type promotion is inconsistent and as a result certain combinations of
4695 * values will return DISP_E_OVERFLOW even when they could be represented.
4696 * This matches the behaviour of native oleaut32.
4698 HRESULT WINAPI
VarXor(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4701 VARIANT varLeft
, varRight
;
4702 VARIANT tempLeft
, tempRight
;
4706 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4708 if (V_EXTRA_TYPE(pVarLeft
) || V_EXTRA_TYPE(pVarRight
) ||
4709 V_VT(pVarLeft
) > VT_UINT
|| V_VT(pVarRight
) > VT_UINT
||
4710 V_VT(pVarLeft
) == VT_VARIANT
|| V_VT(pVarRight
) == VT_VARIANT
||
4711 V_VT(pVarLeft
) == VT_UNKNOWN
|| V_VT(pVarRight
) == VT_UNKNOWN
||
4712 V_VT(pVarLeft
) == (VARTYPE
)15 || V_VT(pVarRight
) == (VARTYPE
)15 ||
4713 V_VT(pVarLeft
) == VT_ERROR
|| V_VT(pVarRight
) == VT_ERROR
)
4714 return DISP_E_BADVARTYPE
;
4716 if (V_VT(pVarLeft
) == VT_NULL
|| V_VT(pVarRight
) == VT_NULL
)
4718 /* NULL XOR anything valid is NULL */
4719 V_VT(pVarOut
) = VT_NULL
;
4723 VariantInit(&tempLeft
);
4724 VariantInit(&tempRight
);
4726 /* Handle VT_DISPATCH by storing and taking address of returned value */
4727 if ((V_VT(pVarLeft
) & VT_TYPEMASK
) == VT_DISPATCH
)
4729 hRet
= VARIANT_FetchDispatchValue(pVarLeft
, &tempLeft
);
4730 if (FAILED(hRet
)) goto VarXor_Exit
;
4731 pVarLeft
= &tempLeft
;
4733 if ((V_VT(pVarRight
) & VT_TYPEMASK
) == VT_DISPATCH
)
4735 hRet
= VARIANT_FetchDispatchValue(pVarRight
, &tempRight
);
4736 if (FAILED(hRet
)) goto VarXor_Exit
;
4737 pVarRight
= &tempRight
;
4740 /* Copy our inputs so we don't disturb anything */
4741 V_VT(&varLeft
) = V_VT(&varRight
) = VT_EMPTY
;
4743 hRet
= VariantCopy(&varLeft
, pVarLeft
);
4747 hRet
= VariantCopy(&varRight
, pVarRight
);
4751 /* Try any strings first as numbers, then as VT_BOOL */
4752 if (V_VT(&varLeft
) == VT_BSTR
)
4754 hRet
= VarR8FromStr(V_BSTR(&varLeft
), LOCALE_USER_DEFAULT
, 0, &d
);
4755 hRet
= VariantChangeType(&varLeft
, &varLeft
, VARIANT_LOCALBOOL
,
4756 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4761 if (V_VT(&varRight
) == VT_BSTR
)
4763 hRet
= VarR8FromStr(V_BSTR(&varRight
), LOCALE_USER_DEFAULT
, 0, &d
);
4764 hRet
= VariantChangeType(&varRight
, &varRight
, VARIANT_LOCALBOOL
,
4765 FAILED(hRet
) ? VT_BOOL
: VT_I4
);
4770 /* Determine the result type */
4771 if (V_VT(&varLeft
) == VT_I8
|| V_VT(&varRight
) == VT_I8
)
4773 if (V_VT(pVarLeft
) == VT_INT
|| V_VT(pVarRight
) == VT_INT
)
4775 hRet
= DISP_E_TYPEMISMATCH
;
4782 switch ((V_VT(&varLeft
) << 16) | V_VT(&varRight
))
4784 case (VT_BOOL
<< 16) | VT_BOOL
:
4787 case (VT_UI1
<< 16) | VT_UI1
:
4790 case (VT_EMPTY
<< 16) | VT_EMPTY
:
4791 case (VT_EMPTY
<< 16) | VT_UI1
:
4792 case (VT_EMPTY
<< 16) | VT_I2
:
4793 case (VT_EMPTY
<< 16) | VT_BOOL
:
4794 case (VT_UI1
<< 16) | VT_EMPTY
:
4795 case (VT_UI1
<< 16) | VT_I2
:
4796 case (VT_UI1
<< 16) | VT_BOOL
:
4797 case (VT_I2
<< 16) | VT_EMPTY
:
4798 case (VT_I2
<< 16) | VT_UI1
:
4799 case (VT_I2
<< 16) | VT_I2
:
4800 case (VT_I2
<< 16) | VT_BOOL
:
4801 case (VT_BOOL
<< 16) | VT_EMPTY
:
4802 case (VT_BOOL
<< 16) | VT_UI1
:
4803 case (VT_BOOL
<< 16) | VT_I2
:
4812 /* VT_UI4 does not overflow */
4815 if (V_VT(&varLeft
) == VT_UI4
)
4816 V_VT(&varLeft
) = VT_I4
;
4817 if (V_VT(&varRight
) == VT_UI4
)
4818 V_VT(&varRight
) = VT_I4
;
4821 /* Convert our input copies to the result type */
4822 if (V_VT(&varLeft
) != vt
)
4823 hRet
= VariantChangeType(&varLeft
, &varLeft
, 0, vt
);
4827 if (V_VT(&varRight
) != vt
)
4828 hRet
= VariantChangeType(&varRight
, &varRight
, 0, vt
);
4834 /* Calculate the result */
4838 V_I8(pVarOut
) = V_I8(&varLeft
) ^ V_I8(&varRight
);
4841 V_I4(pVarOut
) = V_I4(&varLeft
) ^ V_I4(&varRight
);
4845 V_I2(pVarOut
) = V_I2(&varLeft
) ^ V_I2(&varRight
);
4848 V_UI1(pVarOut
) = V_UI1(&varLeft
) ^ V_UI1(&varRight
);
4853 VariantClear(&varLeft
);
4854 VariantClear(&varRight
);
4855 VariantClear(&tempLeft
);
4856 VariantClear(&tempRight
);
4860 /**********************************************************************
4861 * VarEqv [OLEAUT32.172]
4863 * Determine if two variants contain the same value.
4866 * pVarLeft [I] First variant to compare
4867 * pVarRight [I] Variant to compare to pVarLeft
4868 * pVarOut [O] Destination for comparison result
4871 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4872 * if equivalent or non-zero otherwise.
4873 * Failure: An HRESULT error code indicating the error.
4876 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4879 HRESULT WINAPI
VarEqv(LPVARIANT pVarLeft
, LPVARIANT pVarRight
, LPVARIANT pVarOut
)
4883 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft
), debugstr_variant(pVarRight
), pVarOut
);
4885 hRet
= VarXor(pVarLeft
, pVarRight
, pVarOut
);
4886 if (SUCCEEDED(hRet
))
4888 if (V_VT(pVarOut
) == VT_I8
)
4889 V_I8(pVarOut
) = ~V_I8(pVarOut
);
4891 V_UI4(pVarOut
) = ~V_UI4(pVarOut
);
4896 /**********************************************************************
4897 * VarNeg [OLEAUT32.173]
4899 * Negate the value of a variant.
4902 * pVarIn [I] Source variant
4903 * pVarOut [O] Destination for converted value
4906 * Success: S_OK. pVarOut contains the converted value.
4907 * Failure: An HRESULT error code indicating the error.
4910 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4911 * according to the following table:
4912 *| Input Type Output Type
4913 *| ---------- -----------
4918 *| All Others Unchanged (unless promoted)
4919 * - Where the negated value of a variant does not fit in its base type, the type
4920 * is promoted according to the following table:
4921 *| Input Type Promoted To
4922 *| ---------- -----------
4926 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4927 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4928 * for types which are not valid. Since this is in contravention of the
4929 * meaning of those error codes and unlikely to be relied on by applications,
4930 * this implementation returns errors consistent with the other high level
4931 * variant math functions.
4933 HRESULT WINAPI
VarNeg(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
4935 HRESULT hRet
= S_OK
;
4940 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
4942 /* Handle VT_DISPATCH by storing and taking address of returned value */
4943 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
4945 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
4946 if (FAILED(hRet
)) goto VarNeg_Exit
;
4949 V_VT(pVarOut
) = V_VT(pVarIn
);
4951 switch (V_VT(pVarIn
))
4954 V_VT(pVarOut
) = VT_I2
;
4955 V_I2(pVarOut
) = -V_UI1(pVarIn
);
4958 V_VT(pVarOut
) = VT_I2
;
4961 if (V_I2(pVarIn
) == I2_MIN
)
4963 V_VT(pVarOut
) = VT_I4
;
4964 V_I4(pVarOut
) = -(int)V_I2(pVarIn
);
4967 V_I2(pVarOut
) = -V_I2(pVarIn
);
4970 if (V_I4(pVarIn
) == I4_MIN
)
4972 V_VT(pVarOut
) = VT_R8
;
4973 V_R8(pVarOut
) = -(double)V_I4(pVarIn
);
4976 V_I4(pVarOut
) = -V_I4(pVarIn
);
4979 if (V_I8(pVarIn
) == I8_MIN
)
4981 V_VT(pVarOut
) = VT_R8
;
4982 hRet
= VarR8FromI8(V_I8(pVarIn
), &V_R8(pVarOut
));
4983 V_R8(pVarOut
) *= -1.0;
4986 V_I8(pVarOut
) = -V_I8(pVarIn
);
4989 V_R4(pVarOut
) = -V_R4(pVarIn
);
4993 V_R8(pVarOut
) = -V_R8(pVarIn
);
4996 hRet
= VarCyNeg(V_CY(pVarIn
), &V_CY(pVarOut
));
4999 hRet
= VarDecNeg(&V_DECIMAL(pVarIn
), &V_DECIMAL(pVarOut
));
5002 V_VT(pVarOut
) = VT_R8
;
5003 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(pVarOut
));
5004 V_R8(pVarOut
) = -V_R8(pVarOut
);
5007 V_VT(pVarOut
) = VT_I2
;
5014 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
5015 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
5016 hRet
= DISP_E_BADVARTYPE
;
5018 hRet
= DISP_E_TYPEMISMATCH
;
5022 V_VT(pVarOut
) = VT_EMPTY
;
5023 VariantClear(&temp
);
5028 /**********************************************************************
5029 * VarNot [OLEAUT32.174]
5031 * Perform a not operation on a variant.
5034 * pVarIn [I] Source variant
5035 * pVarOut [O] Destination for converted value
5038 * Success: S_OK. pVarOut contains the converted value.
5039 * Failure: An HRESULT error code indicating the error.
5042 * - Strictly speaking, this function performs a bitwise ones complement
5043 * on the variants value (after possibly converting to VT_I4, see below).
5044 * This only behaves like a boolean not operation if the value in
5045 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
5046 * - To perform a genuine not operation, convert the variant to a VT_BOOL
5047 * before calling this function.
5048 * - This function does not process by-reference variants.
5049 * - The type of the value stored in pVarOut depends on the type of pVarIn,
5050 * according to the following table:
5051 *| Input Type Output Type
5052 *| ---------- -----------
5059 *| (All others) Unchanged
5061 HRESULT WINAPI
VarNot(LPVARIANT pVarIn
, LPVARIANT pVarOut
)
5064 HRESULT hRet
= S_OK
;
5069 TRACE("(%s,%p)\n", debugstr_variant(pVarIn
), pVarOut
);
5071 /* Handle VT_DISPATCH by storing and taking address of returned value */
5072 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
5074 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
5075 if (FAILED(hRet
)) goto VarNot_Exit
;
5079 if (V_VT(pVarIn
) == VT_BSTR
)
5081 V_VT(&varIn
) = VT_R8
;
5082 hRet
= VarR8FromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
) );
5085 V_VT(&varIn
) = VT_BOOL
;
5086 hRet
= VarBoolFromStr( V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &V_BOOL(&varIn
) );
5088 if (FAILED(hRet
)) goto VarNot_Exit
;
5092 V_VT(pVarOut
) = V_VT(pVarIn
);
5094 switch (V_VT(pVarIn
))
5097 V_I4(pVarOut
) = ~V_I1(pVarIn
);
5098 V_VT(pVarOut
) = VT_I4
;
5100 case VT_UI1
: V_UI1(pVarOut
) = ~V_UI1(pVarIn
); break;
5102 case VT_I2
: V_I2(pVarOut
) = ~V_I2(pVarIn
); break;
5104 V_I4(pVarOut
) = ~V_UI2(pVarIn
);
5105 V_VT(pVarOut
) = VT_I4
;
5108 hRet
= VarI4FromDec(&V_DECIMAL(pVarIn
), &V_I4(&varIn
));
5112 /* Fall through ... */
5114 V_VT(pVarOut
) = VT_I4
;
5115 /* Fall through ... */
5116 case VT_I4
: V_I4(pVarOut
) = ~V_I4(pVarIn
); break;
5119 V_I4(pVarOut
) = ~V_UI4(pVarIn
);
5120 V_VT(pVarOut
) = VT_I4
;
5122 case VT_I8
: V_I8(pVarOut
) = ~V_I8(pVarIn
); break;
5124 V_I4(pVarOut
) = ~V_UI8(pVarIn
);
5125 V_VT(pVarOut
) = VT_I4
;
5128 hRet
= VarI4FromR4(V_R4(pVarIn
), &V_I4(pVarOut
));
5129 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5130 V_VT(pVarOut
) = VT_I4
;
5134 hRet
= VarI4FromR8(V_R8(pVarIn
), &V_I4(pVarOut
));
5135 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5136 V_VT(pVarOut
) = VT_I4
;
5139 hRet
= VarI4FromCy(V_CY(pVarIn
), &V_I4(pVarOut
));
5140 V_I4(pVarOut
) = ~V_I4(pVarOut
);
5141 V_VT(pVarOut
) = VT_I4
;
5145 V_VT(pVarOut
) = VT_I2
;
5151 if (V_TYPE(pVarIn
) == VT_CLSID
|| /* VT_CLSID is a special case */
5152 FAILED(VARIANT_ValidateType(V_VT(pVarIn
))))
5153 hRet
= DISP_E_BADVARTYPE
;
5155 hRet
= DISP_E_TYPEMISMATCH
;
5159 V_VT(pVarOut
) = VT_EMPTY
;
5160 VariantClear(&temp
);
5165 /**********************************************************************
5166 * VarRound [OLEAUT32.175]
5168 * Perform a round operation on a variant.
5171 * pVarIn [I] Source variant
5172 * deci [I] Number of decimals to round to
5173 * pVarOut [O] Destination for converted value
5176 * Success: S_OK. pVarOut contains the converted value.
5177 * Failure: An HRESULT error code indicating the error.
5180 * - Floating point values are rounded to the desired number of decimals.
5181 * - Some integer types are just copied to the return variable.
5182 * - Some other integer types are not handled and fail.
5184 HRESULT WINAPI
VarRound(LPVARIANT pVarIn
, int deci
, LPVARIANT pVarOut
)
5187 HRESULT hRet
= S_OK
;
5193 TRACE("(%s,%d)\n", debugstr_variant(pVarIn
), deci
);
5195 /* Handle VT_DISPATCH by storing and taking address of returned value */
5196 if ((V_VT(pVarIn
) & VT_TYPEMASK
) == VT_DISPATCH
&& ((V_VT(pVarIn
) & ~VT_TYPEMASK
) == 0))
5198 hRet
= VARIANT_FetchDispatchValue(pVarIn
, &temp
);
5199 if (FAILED(hRet
)) goto VarRound_Exit
;
5203 switch (V_VT(pVarIn
))
5205 /* cases that fail on windows */
5210 hRet
= DISP_E_BADVARTYPE
;
5213 /* cases just copying in to out */
5215 V_VT(pVarOut
) = V_VT(pVarIn
);
5216 V_UI1(pVarOut
) = V_UI1(pVarIn
);
5219 V_VT(pVarOut
) = V_VT(pVarIn
);
5220 V_I2(pVarOut
) = V_I2(pVarIn
);
5223 V_VT(pVarOut
) = V_VT(pVarIn
);
5224 V_I4(pVarOut
) = V_I4(pVarIn
);
5227 V_VT(pVarOut
) = V_VT(pVarIn
);
5228 /* value unchanged */
5231 /* cases that change type */
5233 V_VT(pVarOut
) = VT_I2
;
5237 V_VT(pVarOut
) = VT_I2
;
5238 V_I2(pVarOut
) = V_BOOL(pVarIn
);
5241 hRet
= VarR8FromStr(V_BSTR(pVarIn
), LOCALE_USER_DEFAULT
, 0, &V_R8(&varIn
));
5246 /* Fall through ... */
5248 /* cases we need to do math */
5250 if (V_R8(pVarIn
)>0) {
5251 V_R8(pVarOut
)=floor(V_R8(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5253 V_R8(pVarOut
)=ceil(V_R8(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5255 V_VT(pVarOut
) = V_VT(pVarIn
);
5258 if (V_R4(pVarIn
)>0) {
5259 V_R4(pVarOut
)=floor(V_R4(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5261 V_R4(pVarOut
)=ceil(V_R4(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5263 V_VT(pVarOut
) = V_VT(pVarIn
);
5266 if (V_DATE(pVarIn
)>0) {
5267 V_DATE(pVarOut
)=floor(V_DATE(pVarIn
)*pow(10, deci
)+0.5)/pow(10, deci
);
5269 V_DATE(pVarOut
)=ceil(V_DATE(pVarIn
)*pow(10, deci
)-0.5)/pow(10, deci
);
5271 V_VT(pVarOut
) = V_VT(pVarIn
);
5277 factor
=pow(10, 4-deci
);
5279 if (V_CY(pVarIn
).int64
>0) {
5280 V_CY(pVarOut
).int64
=floor(V_CY(pVarIn
).int64
/factor
)*factor
;
5282 V_CY(pVarOut
).int64
=ceil(V_CY(pVarIn
).int64
/factor
)*factor
;
5284 V_VT(pVarOut
) = V_VT(pVarIn
);
5287 /* cases we don't know yet */
5289 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5290 V_VT(pVarIn
) & VT_TYPEMASK
, deci
);
5291 hRet
= DISP_E_BADVARTYPE
;
5295 V_VT(pVarOut
) = VT_EMPTY
;
5296 VariantClear(&temp
);
5298 TRACE("returning 0x%08x %s\n", hRet
, debugstr_variant(pVarOut
));
5302 /**********************************************************************
5303 * VarIdiv [OLEAUT32.153]
5305 * Converts input variants to integers and divides them.
5308 * left [I] Left hand variant
5309 * right [I] Right hand variant
5310 * result [O] Destination for quotient
5313 * Success: S_OK. result contains the quotient.
5314 * Failure: An HRESULT error code indicating the error.
5317 * If either expression is null, null is returned, as per MSDN
5319 HRESULT WINAPI
VarIdiv(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5321 HRESULT hres
= S_OK
;
5322 VARTYPE resvt
= VT_EMPTY
;
5323 VARTYPE leftvt
,rightvt
;
5324 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5326 VARIANT tempLeft
, tempRight
;
5328 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5332 VariantInit(&tempLeft
);
5333 VariantInit(&tempRight
);
5335 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5336 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5337 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5338 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5340 if (leftExtraFlags
!= rightExtraFlags
)
5342 hres
= DISP_E_BADVARTYPE
;
5345 ExtraFlags
= leftExtraFlags
;
5347 /* Native VarIdiv always returns an error when using extra
5348 * flags or if the variant combination is I8 and INT.
5350 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5351 (leftvt
== VT_INT
&& rightvt
== VT_I8
) ||
5352 (rightvt
== VT_EMPTY
&& leftvt
!= VT_NULL
) ||
5355 hres
= DISP_E_BADVARTYPE
;
5359 /* Determine variant type */
5360 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
)
5362 V_VT(result
) = VT_NULL
;
5366 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5368 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5369 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5370 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5371 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5372 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5373 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5374 leftvt
== VT_I1
|| rightvt
== VT_I1
||
5375 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
||
5376 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5377 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5378 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5379 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5380 leftvt
== VT_R4
|| rightvt
== VT_R4
)
5382 else if (leftvt
== VT_I2
|| rightvt
== VT_I2
||
5383 leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5386 else if (leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5390 hres
= DISP_E_BADVARTYPE
;
5394 /* coerce to the result type */
5395 hres
= VariantChangeType(&lv
, left
, 0, resvt
);
5396 if (hres
!= S_OK
) goto end
;
5397 hres
= VariantChangeType(&rv
, right
, 0, resvt
);
5398 if (hres
!= S_OK
) goto end
;
5401 V_VT(result
) = resvt
;
5405 if (V_UI1(&rv
) == 0)
5407 hres
= DISP_E_DIVBYZERO
;
5408 V_VT(result
) = VT_EMPTY
;
5411 V_UI1(result
) = V_UI1(&lv
) / V_UI1(&rv
);
5416 hres
= DISP_E_DIVBYZERO
;
5417 V_VT(result
) = VT_EMPTY
;
5420 V_I2(result
) = V_I2(&lv
) / V_I2(&rv
);
5425 hres
= DISP_E_DIVBYZERO
;
5426 V_VT(result
) = VT_EMPTY
;
5429 V_I4(result
) = V_I4(&lv
) / V_I4(&rv
);
5434 hres
= DISP_E_DIVBYZERO
;
5435 V_VT(result
) = VT_EMPTY
;
5438 V_I8(result
) = V_I8(&lv
) / V_I8(&rv
);
5441 FIXME("Couldn't integer divide variant types %d,%d\n",
5448 VariantClear(&tempLeft
);
5449 VariantClear(&tempRight
);
5455 /**********************************************************************
5456 * VarMod [OLEAUT32.155]
5458 * Perform the modulus operation of the right hand variant on the left
5461 * left [I] Left hand variant
5462 * right [I] Right hand variant
5463 * result [O] Destination for converted value
5466 * Success: S_OK. result contains the remainder.
5467 * Failure: An HRESULT error code indicating the error.
5470 * If an error occurs the type of result will be modified but the value will not be.
5471 * Doesn't support arrays or any special flags yet.
5473 HRESULT WINAPI
VarMod(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5476 HRESULT rc
= E_FAIL
;
5479 VARIANT tempLeft
, tempRight
;
5481 VariantInit(&tempLeft
);
5482 VariantInit(&tempRight
);
5486 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5488 /* Handle VT_DISPATCH by storing and taking address of returned value */
5489 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5491 rc
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5492 if (FAILED(rc
)) goto end
;
5495 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5497 rc
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5498 if (FAILED(rc
)) goto end
;
5502 /* check for invalid inputs */
5504 switch (V_VT(left
) & VT_TYPEMASK
) {
5526 V_VT(result
) = VT_EMPTY
;
5527 rc
= DISP_E_TYPEMISMATCH
;
5530 rc
= DISP_E_TYPEMISMATCH
;
5533 V_VT(result
) = VT_EMPTY
;
5534 rc
= DISP_E_TYPEMISMATCH
;
5539 V_VT(result
) = VT_EMPTY
;
5540 rc
= DISP_E_BADVARTYPE
;
5545 switch (V_VT(right
) & VT_TYPEMASK
) {
5551 if((V_VT(left
) == VT_INT
) && (V_VT(right
) == VT_I8
))
5553 V_VT(result
) = VT_EMPTY
;
5554 rc
= DISP_E_TYPEMISMATCH
;
5558 if((V_VT(right
) == VT_INT
) && (V_VT(left
) == VT_I8
))
5560 V_VT(result
) = VT_EMPTY
;
5561 rc
= DISP_E_TYPEMISMATCH
;
5572 if(V_VT(left
) == VT_EMPTY
)
5574 V_VT(result
) = VT_I4
;
5581 if(V_VT(left
) == VT_ERROR
)
5583 V_VT(result
) = VT_EMPTY
;
5584 rc
= DISP_E_TYPEMISMATCH
;
5588 if(V_VT(left
) == VT_NULL
)
5590 V_VT(result
) = VT_NULL
;
5597 V_VT(result
) = VT_EMPTY
;
5598 rc
= DISP_E_BADVARTYPE
;
5601 if(V_VT(left
) == VT_VOID
)
5603 V_VT(result
) = VT_EMPTY
;
5604 rc
= DISP_E_BADVARTYPE
;
5605 } else if((V_VT(left
) == VT_NULL
) || (V_VT(left
) == VT_EMPTY
) || (V_VT(left
) == VT_ERROR
) ||
5608 V_VT(result
) = VT_NULL
;
5612 V_VT(result
) = VT_NULL
;
5613 rc
= DISP_E_BADVARTYPE
;
5618 V_VT(result
) = VT_EMPTY
;
5619 rc
= DISP_E_TYPEMISMATCH
;
5622 rc
= DISP_E_TYPEMISMATCH
;
5625 if((V_VT(left
) == 15) || ((V_VT(left
) >= 24) && (V_VT(left
) <= 35)) || !lOk
)
5627 V_VT(result
) = VT_EMPTY
;
5628 rc
= DISP_E_BADVARTYPE
;
5631 V_VT(result
) = VT_EMPTY
;
5632 rc
= DISP_E_TYPEMISMATCH
;
5636 V_VT(result
) = VT_EMPTY
;
5637 rc
= DISP_E_BADVARTYPE
;
5641 /* determine the result type */
5642 if((V_VT(left
) == VT_I8
) || (V_VT(right
) == VT_I8
)) resT
= VT_I8
;
5643 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5644 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_UI1
)) resT
= VT_UI1
;
5645 else if((V_VT(left
) == VT_UI1
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5646 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5647 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5648 else if((V_VT(left
) == VT_I2
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5649 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_BOOL
)) resT
= VT_I2
;
5650 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_UI1
)) resT
= VT_I2
;
5651 else if((V_VT(left
) == VT_BOOL
) && (V_VT(right
) == VT_I2
)) resT
= VT_I2
;
5652 else resT
= VT_I4
; /* most outputs are I4 */
5654 /* convert to I8 for the modulo */
5655 rc
= VariantChangeType(&lv
, left
, 0, VT_I8
);
5658 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left
), VT_I8
, rc
);
5662 rc
= VariantChangeType(&rv
, right
, 0, VT_I8
);
5665 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right
), VT_I8
, rc
);
5669 /* if right is zero set VT_EMPTY and return divide by zero */
5672 V_VT(result
) = VT_EMPTY
;
5673 rc
= DISP_E_DIVBYZERO
;
5677 /* perform the modulo operation */
5678 V_VT(result
) = VT_I8
;
5679 V_I8(result
) = V_I8(&lv
) % V_I8(&rv
);
5681 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5682 wine_dbgstr_longlong(V_I8(&lv
)), wine_dbgstr_longlong(V_I8(&rv
)),
5683 wine_dbgstr_longlong(V_I8(result
)));
5685 /* convert left and right to the destination type */
5686 rc
= VariantChangeType(result
, result
, 0, resT
);
5689 FIXME("Could not convert 0x%x to %d?\n", V_VT(result
), resT
);
5690 /* fall to end of function */
5696 VariantClear(&tempLeft
);
5697 VariantClear(&tempRight
);
5701 /**********************************************************************
5702 * VarPow [OLEAUT32.158]
5704 * Computes the power of one variant to another variant.
5707 * left [I] First variant
5708 * right [I] Second variant
5709 * result [O] Result variant
5713 * Failure: An HRESULT error code indicating the error.
5715 HRESULT WINAPI
VarPow(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5719 VARTYPE resvt
= VT_EMPTY
;
5720 VARTYPE leftvt
,rightvt
;
5721 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5722 VARIANT tempLeft
, tempRight
;
5724 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5728 VariantInit(&tempLeft
);
5729 VariantInit(&tempRight
);
5731 /* Handle VT_DISPATCH by storing and taking address of returned value */
5732 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5734 hr
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5735 if (FAILED(hr
)) goto end
;
5738 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5740 hr
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5741 if (FAILED(hr
)) goto end
;
5745 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5746 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5747 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5748 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5750 if (leftExtraFlags
!= rightExtraFlags
)
5752 hr
= DISP_E_BADVARTYPE
;
5755 ExtraFlags
= leftExtraFlags
;
5757 /* Native VarPow always returns an error when using extra flags */
5758 if (ExtraFlags
!= 0)
5760 hr
= DISP_E_BADVARTYPE
;
5764 /* Determine return type */
5765 else if (leftvt
== VT_NULL
|| rightvt
== VT_NULL
) {
5766 V_VT(result
) = VT_NULL
;
5770 else if ((leftvt
== VT_EMPTY
|| leftvt
== VT_I2
||
5771 leftvt
== VT_I4
|| leftvt
== VT_R4
||
5772 leftvt
== VT_R8
|| leftvt
== VT_CY
||
5773 leftvt
== VT_DATE
|| leftvt
== VT_BSTR
||
5774 leftvt
== VT_BOOL
|| leftvt
== VT_DECIMAL
||
5775 (leftvt
>= VT_I1
&& leftvt
<= VT_UINT
)) &&
5776 (rightvt
== VT_EMPTY
|| rightvt
== VT_I2
||
5777 rightvt
== VT_I4
|| rightvt
== VT_R4
||
5778 rightvt
== VT_R8
|| rightvt
== VT_CY
||
5779 rightvt
== VT_DATE
|| rightvt
== VT_BSTR
||
5780 rightvt
== VT_BOOL
|| rightvt
== VT_DECIMAL
||
5781 (rightvt
>= VT_I1
&& rightvt
<= VT_UINT
)))
5785 hr
= DISP_E_BADVARTYPE
;
5789 hr
= VariantChangeType(&dl
,left
,0,resvt
);
5791 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5796 hr
= VariantChangeType(&dr
,right
,0,resvt
);
5798 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5803 V_VT(result
) = VT_R8
;
5804 V_R8(result
) = pow(V_R8(&dl
),V_R8(&dr
));
5809 VariantClear(&tempLeft
);
5810 VariantClear(&tempRight
);
5815 /**********************************************************************
5816 * VarImp [OLEAUT32.154]
5818 * Bitwise implication of two variants.
5821 * left [I] First variant
5822 * right [I] Second variant
5823 * result [O] Result variant
5827 * Failure: An HRESULT error code indicating the error.
5829 HRESULT WINAPI
VarImp(LPVARIANT left
, LPVARIANT right
, LPVARIANT result
)
5831 HRESULT hres
= S_OK
;
5832 VARTYPE resvt
= VT_EMPTY
;
5833 VARTYPE leftvt
,rightvt
;
5834 VARTYPE rightExtraFlags
,leftExtraFlags
,ExtraFlags
;
5837 VARIANT tempLeft
, tempRight
;
5841 VariantInit(&tempLeft
);
5842 VariantInit(&tempRight
);
5844 TRACE("(%s,%s,%p)\n", debugstr_variant(left
), debugstr_variant(right
), result
);
5846 /* Handle VT_DISPATCH by storing and taking address of returned value */
5847 if ((V_VT(left
) & VT_TYPEMASK
) == VT_DISPATCH
)
5849 hres
= VARIANT_FetchDispatchValue(left
, &tempLeft
);
5850 if (FAILED(hres
)) goto VarImp_Exit
;
5853 if ((V_VT(right
) & VT_TYPEMASK
) == VT_DISPATCH
)
5855 hres
= VARIANT_FetchDispatchValue(right
, &tempRight
);
5856 if (FAILED(hres
)) goto VarImp_Exit
;
5860 leftvt
= V_VT(left
)&VT_TYPEMASK
;
5861 rightvt
= V_VT(right
)&VT_TYPEMASK
;
5862 leftExtraFlags
= V_VT(left
)&(~VT_TYPEMASK
);
5863 rightExtraFlags
= V_VT(right
)&(~VT_TYPEMASK
);
5865 if (leftExtraFlags
!= rightExtraFlags
)
5867 hres
= DISP_E_BADVARTYPE
;
5870 ExtraFlags
= leftExtraFlags
;
5872 /* Native VarImp always returns an error when using extra
5873 * flags or if the variants are I8 and INT.
5875 if ((leftvt
== VT_I8
&& rightvt
== VT_INT
) ||
5878 hres
= DISP_E_BADVARTYPE
;
5882 /* Determine result type */
5883 else if ((leftvt
== VT_NULL
&& rightvt
== VT_NULL
) ||
5884 (leftvt
== VT_NULL
&& rightvt
== VT_EMPTY
))
5886 V_VT(result
) = VT_NULL
;
5890 else if (leftvt
== VT_I8
|| rightvt
== VT_I8
)
5892 else if (leftvt
== VT_I4
|| rightvt
== VT_I4
||
5893 leftvt
== VT_INT
|| rightvt
== VT_INT
||
5894 leftvt
== VT_UINT
|| rightvt
== VT_UINT
||
5895 leftvt
== VT_UI4
|| rightvt
== VT_UI4
||
5896 leftvt
== VT_UI8
|| rightvt
== VT_UI8
||
5897 leftvt
== VT_UI2
|| rightvt
== VT_UI2
||
5898 leftvt
== VT_DECIMAL
|| rightvt
== VT_DECIMAL
||
5899 leftvt
== VT_DATE
|| rightvt
== VT_DATE
||
5900 leftvt
== VT_CY
|| rightvt
== VT_CY
||
5901 leftvt
== VT_R8
|| rightvt
== VT_R8
||
5902 leftvt
== VT_R4
|| rightvt
== VT_R4
||
5903 leftvt
== VT_I1
|| rightvt
== VT_I1
)
5905 else if ((leftvt
== VT_UI1
&& rightvt
== VT_UI1
) ||
5906 (leftvt
== VT_UI1
&& rightvt
== VT_NULL
) ||
5907 (leftvt
== VT_NULL
&& rightvt
== VT_UI1
))
5909 else if (leftvt
== VT_EMPTY
|| rightvt
== VT_EMPTY
||
5910 leftvt
== VT_I2
|| rightvt
== VT_I2
||
5911 leftvt
== VT_UI1
|| rightvt
== VT_UI1
)
5913 else if (leftvt
== VT_BOOL
|| rightvt
== VT_BOOL
||
5914 leftvt
== VT_BSTR
|| rightvt
== VT_BSTR
)
5917 /* VT_NULL requires special handling for when the opposite
5918 * variant is equal to something other than -1.
5919 * (NULL Imp 0 = NULL, NULL Imp n = n)
5921 if (leftvt
== VT_NULL
)
5926 case VT_I1
: if (!V_I1(right
)) resvt
= VT_NULL
; break;
5927 case VT_UI1
: if (!V_UI1(right
)) resvt
= VT_NULL
; break;
5928 case VT_I2
: if (!V_I2(right
)) resvt
= VT_NULL
; break;
5929 case VT_UI2
: if (!V_UI2(right
)) resvt
= VT_NULL
; break;
5930 case VT_I4
: if (!V_I4(right
)) resvt
= VT_NULL
; break;
5931 case VT_UI4
: if (!V_UI4(right
)) resvt
= VT_NULL
; break;
5932 case VT_I8
: if (!V_I8(right
)) resvt
= VT_NULL
; break;
5933 case VT_UI8
: if (!V_UI8(right
)) resvt
= VT_NULL
; break;
5934 case VT_INT
: if (!V_INT(right
)) resvt
= VT_NULL
; break;
5935 case VT_UINT
: if (!V_UINT(right
)) resvt
= VT_NULL
; break;
5936 case VT_BOOL
: if (!V_BOOL(right
)) resvt
= VT_NULL
; break;
5937 case VT_R4
: if (!V_R4(right
)) resvt
= VT_NULL
; break;
5938 case VT_R8
: if (!V_R8(right
)) resvt
= VT_NULL
; break;
5939 case VT_DATE
: if (!V_DATE(right
)) resvt
= VT_NULL
; break;
5940 case VT_CY
: if (!V_CY(right
).int64
) resvt
= VT_NULL
; break;
5942 if (!(DEC_HI32(&V_DECIMAL(right
)) || DEC_LO64(&V_DECIMAL(right
))))
5946 hres
= VarBoolFromStr(V_BSTR(right
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5947 if (FAILED(hres
)) goto VarImp_Exit
;
5949 V_VT(result
) = VT_NULL
;
5952 V_VT(result
) = VT_BOOL
;
5957 if (resvt
== VT_NULL
)
5959 V_VT(result
) = resvt
;
5964 hres
= VariantChangeType(result
,right
,0,resvt
);
5969 /* Special handling is required when NULL is the right variant.
5970 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5972 else if (rightvt
== VT_NULL
)
5977 case VT_I1
: if (V_I1(left
) == -1) resvt
= VT_NULL
; break;
5978 case VT_UI1
: if (V_UI1(left
) == 0xff) resvt
= VT_NULL
; break;
5979 case VT_I2
: if (V_I2(left
) == -1) resvt
= VT_NULL
; break;
5980 case VT_UI2
: if (V_UI2(left
) == 0xffff) resvt
= VT_NULL
; break;
5981 case VT_INT
: if (V_INT(left
) == -1) resvt
= VT_NULL
; break;
5982 case VT_UINT
: if (V_UINT(left
) == ~0u) resvt
= VT_NULL
; break;
5983 case VT_I4
: if (V_I4(left
) == -1) resvt
= VT_NULL
; break;
5984 case VT_UI4
: if (V_UI4(left
) == ~0u) resvt
= VT_NULL
; break;
5985 case VT_I8
: if (V_I8(left
) == -1) resvt
= VT_NULL
; break;
5986 case VT_UI8
: if (V_UI8(left
) == ~(ULONGLONG
)0) resvt
= VT_NULL
; break;
5987 case VT_BOOL
: if (V_BOOL(left
) == VARIANT_TRUE
) resvt
= VT_NULL
; break;
5988 case VT_R4
: if (V_R4(left
) == -1.0) resvt
= VT_NULL
; break;
5989 case VT_R8
: if (V_R8(left
) == -1.0) resvt
= VT_NULL
; break;
5990 case VT_CY
: if (V_CY(left
).int64
== -1) resvt
= VT_NULL
; break;
5992 if (DEC_HI32(&V_DECIMAL(left
)) == 0xffffffff)
5996 hres
= VarBoolFromStr(V_BSTR(left
),LOCALE_USER_DEFAULT
, VAR_LOCALBOOL
, &b
);
5997 if (FAILED(hres
)) goto VarImp_Exit
;
5998 else if (b
== VARIANT_TRUE
)
6001 if (resvt
== VT_NULL
)
6003 V_VT(result
) = resvt
;
6008 hres
= VariantCopy(&lv
, left
);
6009 if (FAILED(hres
)) goto VarImp_Exit
;
6011 if (rightvt
== VT_NULL
)
6013 memset( &rv
, 0, sizeof(rv
) );
6018 hres
= VariantCopy(&rv
, right
);
6019 if (FAILED(hres
)) goto VarImp_Exit
;
6022 if (V_VT(&lv
) == VT_BSTR
&&
6023 FAILED(VarR8FromStr(V_BSTR(&lv
),LOCALE_USER_DEFAULT
, 0, &d
)))
6024 hres
= VariantChangeType(&lv
,&lv
,VARIANT_LOCALBOOL
, VT_BOOL
);
6025 if (SUCCEEDED(hres
) && V_VT(&lv
) != resvt
)
6026 hres
= VariantChangeType(&lv
,&lv
,0,resvt
);
6027 if (FAILED(hres
)) goto VarImp_Exit
;
6029 if (V_VT(&rv
) == VT_BSTR
&&
6030 FAILED(VarR8FromStr(V_BSTR(&rv
),LOCALE_USER_DEFAULT
, 0, &d
)))
6031 hres
= VariantChangeType(&rv
, &rv
,VARIANT_LOCALBOOL
, VT_BOOL
);
6032 if (SUCCEEDED(hres
) && V_VT(&rv
) != resvt
)
6033 hres
= VariantChangeType(&rv
, &rv
, 0, resvt
);
6034 if (FAILED(hres
)) goto VarImp_Exit
;
6037 V_VT(result
) = resvt
;
6041 V_I8(result
) = (~V_I8(&lv
)) | V_I8(&rv
);
6044 V_I4(result
) = (~V_I4(&lv
)) | V_I4(&rv
);
6047 V_I2(result
) = (~V_I2(&lv
)) | V_I2(&rv
);
6050 V_UI1(result
) = (~V_UI1(&lv
)) | V_UI1(&rv
);
6053 V_BOOL(result
) = (~V_BOOL(&lv
)) | V_BOOL(&rv
);
6056 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
6064 VariantClear(&tempLeft
);
6065 VariantClear(&tempRight
);