mshtml: Clean up InvokeEx implementation by using get_dispid_type.
[wine/multimedia.git] / dlls / oleaut32 / variant.c
blob2045eab4b1cc9aedf5b1f8b5cc9c2053a48b305b
1 /*
2 * VARIANT
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
28 #include "config.h"
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wine/unicode.h"
41 #include "winerror.h"
42 #include "variant.h"
43 #include "resource.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(variant);
48 const char * const wine_vtypes[VT_CLSID+1] =
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"
61 const char * const wine_vflags[16] =
63 "",
64 "|VT_VECTOR",
65 "|VT_ARRAY",
66 "|VT_VECTOR|VT_ARRAY",
67 "|VT_BYREF",
68 "|VT_VECTOR|VT_ARRAY",
69 "|VT_ARRAY|VT_BYREF",
70 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
71 "|VT_HARDTYPE",
72 "|VT_VECTOR|VT_HARDTYPE",
73 "|VT_ARRAY|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
75 "|VT_BYREF|VT_HARDTYPE",
76 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
77 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
78 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
81 /* Convert a variant from one type to another */
82 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
83 VARIANTARG* ps, VARTYPE vt)
85 HRESULT res = DISP_E_TYPEMISMATCH;
86 VARTYPE vtFrom = V_TYPE(ps);
87 DWORD dwFlags = 0;
89 TRACE("(%p->(%s%s),0x%08x,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
90 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
91 debugstr_vt(vt), debugstr_vf(vt));
93 if (vt == VT_BSTR || vtFrom == VT_BSTR)
95 /* All flags passed to low level function are only used for
96 * changing to or from strings. Map these here.
98 if (wFlags & VARIANT_LOCALBOOL)
99 dwFlags |= VAR_LOCALBOOL;
100 if (wFlags & VARIANT_CALENDAR_HIJRI)
101 dwFlags |= VAR_CALENDAR_HIJRI;
102 if (wFlags & VARIANT_CALENDAR_THAI)
103 dwFlags |= VAR_CALENDAR_THAI;
104 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
105 dwFlags |= VAR_CALENDAR_GREGORIAN;
106 if (wFlags & VARIANT_NOUSEROVERRIDE)
107 dwFlags |= LOCALE_NOUSEROVERRIDE;
108 if (wFlags & VARIANT_USE_NLS)
109 dwFlags |= LOCALE_USE_NLS;
112 /* Map int/uint to i4/ui4 */
113 if (vt == VT_INT)
114 vt = VT_I4;
115 else if (vt == VT_UINT)
116 vt = VT_UI4;
118 if (vtFrom == VT_INT)
119 vtFrom = VT_I4;
120 else if (vtFrom == VT_UINT)
121 vtFrom = VT_UI4;
123 if (vt == vtFrom)
124 return VariantCopy(pd, ps);
126 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
128 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
129 * accessing the default object property.
131 return DISP_E_TYPEMISMATCH;
134 switch (vt)
136 case VT_EMPTY:
137 if (vtFrom == VT_NULL)
138 return DISP_E_TYPEMISMATCH;
139 /* ... Fall through */
140 case VT_NULL:
141 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
143 res = VariantClear( pd );
144 if (vt == VT_NULL && SUCCEEDED(res))
145 V_VT(pd) = VT_NULL;
147 return res;
149 case VT_I1:
150 switch (vtFrom)
152 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
153 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
154 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
155 case VT_UI1: V_I1(pd) = V_UI1(ps); return S_OK;
156 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
157 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
158 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
159 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
160 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
161 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
162 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
163 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
164 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
165 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
166 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
167 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
169 break;
171 case VT_I2:
172 switch (vtFrom)
174 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
175 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
176 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
177 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
178 case VT_UI2: V_I2(pd) = V_UI2(ps); return S_OK;
179 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
180 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
181 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
182 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
183 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
184 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
185 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
186 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
187 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
188 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
189 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
191 break;
193 case VT_I4:
194 switch (vtFrom)
196 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
197 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
198 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
199 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
200 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
201 case VT_UI4: V_I4(pd) = V_UI4(ps); return S_OK;
202 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
203 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
204 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
205 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
206 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
207 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
208 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
209 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
210 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
211 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
213 break;
215 case VT_UI1:
216 switch (vtFrom)
218 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
219 case VT_I1: V_UI1(pd) = V_I1(ps); return S_OK;
220 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
221 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
222 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
223 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
224 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
225 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
226 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
227 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
228 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
229 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
230 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
231 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
232 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
233 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
235 break;
237 case VT_UI2:
238 switch (vtFrom)
240 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
241 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
242 case VT_I2: V_UI2(pd) = V_I2(ps); return S_OK;
243 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
244 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
245 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
246 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
247 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
248 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
249 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
250 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
251 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
252 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
253 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
254 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
255 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
257 break;
259 case VT_UI4:
260 switch (vtFrom)
262 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
263 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
264 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
265 case VT_I4: V_UI4(pd) = V_I4(ps); return S_OK;
266 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
267 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
268 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
269 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
270 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
271 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
272 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
273 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
274 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
275 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
276 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
277 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
279 break;
281 case VT_UI8:
282 switch (vtFrom)
284 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
285 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
286 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
287 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
288 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
289 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
290 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
291 case VT_I8: V_UI8(pd) = V_I8(ps); return S_OK;
292 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
293 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
294 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
295 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
296 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
297 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
298 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
299 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
301 break;
303 case VT_I8:
304 switch (vtFrom)
306 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
307 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
308 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
309 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
310 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
311 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
312 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
313 case VT_UI8: V_I8(pd) = V_UI8(ps); return S_OK;
314 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
315 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
316 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
317 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
318 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
319 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
320 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
321 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
323 break;
325 case VT_R4:
326 switch (vtFrom)
328 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
329 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
330 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
331 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
332 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
333 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
334 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
335 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
336 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
337 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
338 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
339 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
340 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
341 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
342 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
343 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
345 break;
347 case VT_R8:
348 switch (vtFrom)
350 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
351 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
352 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
353 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
354 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
355 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
356 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
357 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
358 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
359 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
360 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
361 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
362 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
363 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
364 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
365 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
367 break;
369 case VT_DATE:
370 switch (vtFrom)
372 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
373 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
374 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
375 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
376 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
377 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
378 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
379 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
380 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
381 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
382 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
383 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
384 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
385 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
386 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
387 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
389 break;
391 case VT_BOOL:
392 switch (vtFrom)
394 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
395 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
396 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
397 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
398 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
399 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
400 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
401 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
402 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
403 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
404 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
405 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
406 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
407 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
408 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
409 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
411 break;
413 case VT_BSTR:
414 switch (vtFrom)
416 case VT_EMPTY:
417 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
418 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
419 case VT_BOOL:
420 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
421 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
422 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
423 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
424 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
425 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
426 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
427 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
428 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
429 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
430 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
436 case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd));
438 break;
440 case VT_CY:
441 switch (vtFrom)
443 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
444 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
445 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
446 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
447 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
448 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
449 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
450 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
451 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
452 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
453 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
454 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
455 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
456 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
457 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
458 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
460 break;
462 case VT_DECIMAL:
463 switch (vtFrom)
465 case VT_EMPTY:
466 case VT_BOOL:
467 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
468 DEC_HI32(&V_DECIMAL(pd)) = 0;
469 DEC_MID32(&V_DECIMAL(pd)) = 0;
470 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
471 * VT_NULL and VT_EMPTY always give a 0 value.
473 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
474 return S_OK;
475 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
476 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
477 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
478 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
479 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
480 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
481 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
482 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
483 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
484 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
485 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
486 case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
487 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
488 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
490 break;
492 case VT_UNKNOWN:
493 switch (vtFrom)
495 case VT_DISPATCH:
496 if (V_DISPATCH(ps) == NULL)
497 V_UNKNOWN(pd) = NULL;
498 else
499 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
500 break;
502 break;
504 case VT_DISPATCH:
505 switch (vtFrom)
507 case VT_UNKNOWN:
508 if (V_UNKNOWN(ps) == NULL)
509 V_DISPATCH(pd) = NULL;
510 else
511 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
512 break;
514 break;
516 case VT_RECORD:
517 break;
519 return res;
522 /* Coerce to/from an array */
523 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
525 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
526 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
528 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
529 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
531 if (V_VT(ps) == vt)
532 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
534 return DISP_E_TYPEMISMATCH;
537 /******************************************************************************
538 * Check if a variants type is valid.
540 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
542 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
544 vt &= VT_TYPEMASK;
546 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
548 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
550 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
551 return DISP_E_BADVARTYPE;
552 if (vt != (VARTYPE)15)
553 return S_OK;
556 return DISP_E_BADVARTYPE;
559 /******************************************************************************
560 * VariantInit [OLEAUT32.8]
562 * Initialise a variant.
564 * PARAMS
565 * pVarg [O] Variant to initialise
567 * RETURNS
568 * Nothing.
570 * NOTES
571 * This function simply sets the type of the variant to VT_EMPTY. It does not
572 * free any existing value, use VariantClear() for that.
574 void WINAPI VariantInit(VARIANTARG* pVarg)
576 TRACE("(%p)\n", pVarg);
578 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
581 HRESULT VARIANT_ClearInd(VARIANTARG *pVarg)
583 HRESULT hres;
585 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
587 hres = VARIANT_ValidateType(V_VT(pVarg));
588 if (FAILED(hres))
589 return hres;
591 switch (V_VT(pVarg))
593 case VT_DISPATCH:
594 case VT_UNKNOWN:
595 if (V_UNKNOWN(pVarg))
596 IUnknown_Release(V_UNKNOWN(pVarg));
597 break;
598 case VT_UNKNOWN | VT_BYREF:
599 case VT_DISPATCH | VT_BYREF:
600 if(*V_UNKNOWNREF(pVarg))
601 IUnknown_Release(*V_UNKNOWNREF(pVarg));
602 break;
603 case VT_BSTR:
604 SysFreeString(V_BSTR(pVarg));
605 break;
606 case VT_BSTR | VT_BYREF:
607 SysFreeString(*V_BSTRREF(pVarg));
608 break;
609 case VT_VARIANT | VT_BYREF:
610 VariantClear(V_VARIANTREF(pVarg));
611 break;
612 case VT_RECORD:
613 case VT_RECORD | VT_BYREF:
615 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
616 if (pBr->pRecInfo)
618 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
619 IRecordInfo_Release(pBr->pRecInfo);
621 break;
623 default:
624 if (V_ISARRAY(pVarg) || (V_VT(pVarg) & ~VT_BYREF) == VT_SAFEARRAY)
626 if (V_ISBYREF(pVarg))
628 if (*V_ARRAYREF(pVarg))
629 hres = SafeArrayDestroy(*V_ARRAYREF(pVarg));
631 else if (V_ARRAY(pVarg))
632 hres = SafeArrayDestroy(V_ARRAY(pVarg));
634 break;
637 V_VT(pVarg) = VT_EMPTY;
638 return hres;
641 /******************************************************************************
642 * VariantClear [OLEAUT32.9]
644 * Clear a variant.
646 * PARAMS
647 * pVarg [I/O] Variant to clear
649 * RETURNS
650 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
651 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
653 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
655 HRESULT hres;
657 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
659 hres = VARIANT_ValidateType(V_VT(pVarg));
661 if (SUCCEEDED(hres))
663 if (!V_ISBYREF(pVarg))
665 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
667 hres = SafeArrayDestroy(V_ARRAY(pVarg));
669 else if (V_VT(pVarg) == VT_BSTR)
671 SysFreeString(V_BSTR(pVarg));
673 else if (V_VT(pVarg) == VT_RECORD)
675 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
676 if (pBr->pRecInfo)
678 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
679 IRecordInfo_Release(pBr->pRecInfo);
682 else if (V_VT(pVarg) == VT_DISPATCH ||
683 V_VT(pVarg) == VT_UNKNOWN)
685 if (V_UNKNOWN(pVarg))
686 IUnknown_Release(V_UNKNOWN(pVarg));
689 V_VT(pVarg) = VT_EMPTY;
691 return hres;
694 /******************************************************************************
695 * Copy an IRecordInfo object contained in a variant.
697 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
699 HRESULT hres = S_OK;
701 if (pBr->pRecInfo)
703 ULONG ulSize;
705 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
706 if (SUCCEEDED(hres))
708 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
709 if (!pvRecord)
710 hres = E_OUTOFMEMORY;
711 else
713 memcpy(pvRecord, pBr->pvRecord, ulSize);
714 pBr->pvRecord = pvRecord;
716 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
717 if (SUCCEEDED(hres))
718 IRecordInfo_AddRef(pBr->pRecInfo);
722 else if (pBr->pvRecord)
723 hres = E_INVALIDARG;
724 return hres;
727 /******************************************************************************
728 * VariantCopy [OLEAUT32.10]
730 * Copy a variant.
732 * PARAMS
733 * pvargDest [O] Destination for copy
734 * pvargSrc [I] Source variant to copy
736 * RETURNS
737 * Success: S_OK. pvargDest contains a copy of pvargSrc.
738 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
739 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
740 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
741 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
743 * NOTES
744 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
745 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
746 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
747 * fails, so does this function.
748 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
749 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
750 * is copied rather than just any pointers to it.
751 * - For by-value object types the object pointer is copied and the objects
752 * reference count increased using IUnknown_AddRef().
753 * - For all by-reference types, only the referencing pointer is copied.
755 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
757 HRESULT hres = S_OK;
759 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
760 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
761 debugstr_VF(pvargSrc));
763 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
764 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
765 return DISP_E_BADVARTYPE;
767 if (pvargSrc != pvargDest &&
768 SUCCEEDED(hres = VariantClear(pvargDest)))
770 *pvargDest = *pvargSrc; /* Shallow copy the value */
772 if (!V_ISBYREF(pvargSrc))
774 if (V_ISARRAY(pvargSrc))
776 if (V_ARRAY(pvargSrc))
777 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
779 else if (V_VT(pvargSrc) == VT_BSTR)
781 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
782 if (!V_BSTR(pvargDest))
784 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
785 hres = E_OUTOFMEMORY;
788 else if (V_VT(pvargSrc) == VT_RECORD)
790 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
792 else if (V_VT(pvargSrc) == VT_DISPATCH ||
793 V_VT(pvargSrc) == VT_UNKNOWN)
795 if (V_UNKNOWN(pvargSrc))
796 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
800 return hres;
803 /* Return the byte size of a variants data */
804 static inline size_t VARIANT_DataSize(const VARIANT* pv)
806 switch (V_TYPE(pv))
808 case VT_I1:
809 case VT_UI1: return sizeof(BYTE);
810 case VT_I2:
811 case VT_UI2: return sizeof(SHORT);
812 case VT_INT:
813 case VT_UINT:
814 case VT_I4:
815 case VT_UI4: return sizeof(LONG);
816 case VT_I8:
817 case VT_UI8: return sizeof(LONGLONG);
818 case VT_R4: return sizeof(float);
819 case VT_R8: return sizeof(double);
820 case VT_DATE: return sizeof(DATE);
821 case VT_BOOL: return sizeof(VARIANT_BOOL);
822 case VT_DISPATCH:
823 case VT_UNKNOWN:
824 case VT_BSTR: return sizeof(void*);
825 case VT_CY: return sizeof(CY);
826 case VT_ERROR: return sizeof(SCODE);
828 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
829 return 0;
832 /******************************************************************************
833 * VariantCopyInd [OLEAUT32.11]
835 * Copy a variant, dereferencing it if it is by-reference.
837 * PARAMS
838 * pvargDest [O] Destination for copy
839 * pvargSrc [I] Source variant to copy
841 * RETURNS
842 * Success: S_OK. pvargDest contains a copy of pvargSrc.
843 * Failure: An HRESULT error code indicating the error.
845 * NOTES
846 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
847 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
848 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
849 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
850 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
852 * NOTES
853 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
854 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
855 * value.
856 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
857 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
858 * to it. If clearing pvargDest fails, so does this function.
860 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
862 VARIANTARG vTmp, *pSrc = pvargSrc;
863 VARTYPE vt;
864 HRESULT hres = S_OK;
866 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
867 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
868 debugstr_VF(pvargSrc));
870 if (!V_ISBYREF(pvargSrc))
871 return VariantCopy(pvargDest, pvargSrc);
873 /* Argument checking is more lax than VariantCopy()... */
874 vt = V_TYPE(pvargSrc);
875 if (V_ISARRAY(pvargSrc) || (V_VT(pvargSrc) == (VT_RECORD|VT_BYREF)) ||
876 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
877 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
879 /* OK */
881 else
882 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
884 if (pvargSrc == pvargDest)
886 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
887 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
889 vTmp = *pvargSrc;
890 pSrc = &vTmp;
891 V_VT(pvargDest) = VT_EMPTY;
893 else
895 /* Copy into another variant. Free the variant in pvargDest */
896 if (FAILED(hres = VariantClear(pvargDest)))
898 TRACE("VariantClear() of destination failed\n");
899 return hres;
903 if (V_ISARRAY(pSrc))
905 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
906 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
908 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
910 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
911 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
913 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
915 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
916 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
918 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
919 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
921 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
922 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
923 if (*V_UNKNOWNREF(pSrc))
924 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
926 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
928 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
929 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
930 hres = E_INVALIDARG; /* Don't dereference more than one level */
931 else
932 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
934 /* Use the dereferenced variants type value, not VT_VARIANT */
935 goto VariantCopyInd_Return;
937 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
939 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
940 sizeof(DECIMAL) - sizeof(USHORT));
942 else
944 /* Copy the pointed to data into this variant */
945 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
948 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
950 VariantCopyInd_Return:
952 if (pSrc != pvargSrc)
953 VariantClear(pSrc);
955 TRACE("returning 0x%08x, %p->(%s%s)\n", hres, pvargDest,
956 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
957 return hres;
960 /******************************************************************************
961 * VariantChangeType [OLEAUT32.12]
963 * Change the type of a variant.
965 * PARAMS
966 * pvargDest [O] Destination for the converted variant
967 * pvargSrc [O] Source variant to change the type of
968 * wFlags [I] VARIANT_ flags from "oleauto.h"
969 * vt [I] Variant type to change pvargSrc into
971 * RETURNS
972 * Success: S_OK. pvargDest contains the converted value.
973 * Failure: An HRESULT error code describing the failure.
975 * NOTES
976 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
977 * See VariantChangeTypeEx.
979 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
980 USHORT wFlags, VARTYPE vt)
982 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
985 /******************************************************************************
986 * VariantChangeTypeEx [OLEAUT32.147]
988 * Change the type of a variant.
990 * PARAMS
991 * pvargDest [O] Destination for the converted variant
992 * pvargSrc [O] Source variant to change the type of
993 * lcid [I] LCID for the conversion
994 * wFlags [I] VARIANT_ flags from "oleauto.h"
995 * vt [I] Variant type to change pvargSrc into
997 * RETURNS
998 * Success: S_OK. pvargDest contains the converted value.
999 * Failure: An HRESULT error code describing the failure.
1001 * NOTES
1002 * pvargDest and pvargSrc can point to the same variant to perform an in-place
1003 * conversion. If the conversion is successful, pvargSrc will be freed.
1005 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
1006 LCID lcid, USHORT wFlags, VARTYPE vt)
1008 HRESULT res = S_OK;
1010 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%04x,%s%s)\n", pvargDest,
1011 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
1012 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
1013 debugstr_vt(vt), debugstr_vf(vt));
1015 if (vt == VT_CLSID)
1016 res = DISP_E_BADVARTYPE;
1017 else
1019 res = VARIANT_ValidateType(V_VT(pvargSrc));
1021 if (SUCCEEDED(res))
1023 res = VARIANT_ValidateType(vt);
1025 if (SUCCEEDED(res))
1027 VARIANTARG vTmp, vSrcDeref;
1029 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
1030 res = DISP_E_TYPEMISMATCH;
1031 else
1033 V_VT(&vTmp) = VT_EMPTY;
1034 V_VT(&vSrcDeref) = VT_EMPTY;
1035 VariantClear(&vTmp);
1036 VariantClear(&vSrcDeref);
1039 if (SUCCEEDED(res))
1041 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1042 if (SUCCEEDED(res))
1044 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1045 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1046 else
1047 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1049 if (SUCCEEDED(res)) {
1050 V_VT(&vTmp) = vt;
1051 VariantCopy(pvargDest, &vTmp);
1053 VariantClear(&vTmp);
1054 VariantClear(&vSrcDeref);
1061 TRACE("returning 0x%08x, %p->(%s%s)\n", res, pvargDest,
1062 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1063 return res;
1066 /* Date Conversions */
1068 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1070 /* Convert a VT_DATE value to a Julian Date */
1071 static inline int VARIANT_JulianFromDate(int dateIn)
1073 int julianDays = dateIn;
1075 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1076 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1077 return julianDays;
1080 /* Convert a Julian Date to a VT_DATE value */
1081 static inline int VARIANT_DateFromJulian(int dateIn)
1083 int julianDays = dateIn;
1085 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1086 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1087 return julianDays;
1090 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1091 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1093 int j, i, l, n;
1095 l = jd + 68569;
1096 n = l * 4 / 146097;
1097 l -= (n * 146097 + 3) / 4;
1098 i = (4000 * (l + 1)) / 1461001;
1099 l += 31 - (i * 1461) / 4;
1100 j = (l * 80) / 2447;
1101 *day = l - (j * 2447) / 80;
1102 l = j / 11;
1103 *month = (j + 2) - (12 * l);
1104 *year = 100 * (n - 49) + i + l;
1107 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1108 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1110 int m12 = (month - 14) / 12;
1112 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1113 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1116 /* Macros for accessing DOS format date/time fields */
1117 #define DOS_YEAR(x) (1980 + (x >> 9))
1118 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1119 #define DOS_DAY(x) (x & 0x1f)
1120 #define DOS_HOUR(x) (x >> 11)
1121 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1122 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1123 /* Create a DOS format date/time */
1124 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1125 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1127 /* Roll a date forwards or backwards to correct it */
1128 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1130 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1131 short iYear, iMonth, iDay, iHour, iMinute, iSecond;
1133 /* interpret values signed */
1134 iYear = lpUd->st.wYear;
1135 iMonth = lpUd->st.wMonth;
1136 iDay = lpUd->st.wDay;
1137 iHour = lpUd->st.wHour;
1138 iMinute = lpUd->st.wMinute;
1139 iSecond = lpUd->st.wSecond;
1141 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay, iMonth,
1142 iYear, iHour, iMinute, iSecond);
1144 if (iYear > 9999 || iYear < -9999)
1145 return E_INVALIDARG; /* Invalid value */
1146 /* Year 0 to 29 are treated as 2000 + year */
1147 if (iYear >= 0 && iYear < 30)
1148 iYear += 2000;
1149 /* Remaining years < 100 are treated as 1900 + year */
1150 else if (iYear >= 30 && iYear < 100)
1151 iYear += 1900;
1153 iMinute += iSecond / 60;
1154 iSecond = iSecond % 60;
1155 iHour += iMinute / 60;
1156 iMinute = iMinute % 60;
1157 iDay += iHour / 24;
1158 iHour = iHour % 24;
1159 iYear += iMonth / 12;
1160 iMonth = iMonth % 12;
1161 if (iMonth<=0) {iMonth+=12; iYear--;}
1162 while (iDay > days[iMonth])
1164 if (iMonth == 2 && IsLeapYear(iYear))
1165 iDay -= 29;
1166 else
1167 iDay -= days[iMonth];
1168 iMonth++;
1169 iYear += iMonth / 12;
1170 iMonth = iMonth % 12;
1172 while (iDay <= 0)
1174 iMonth--;
1175 if (iMonth<=0) {iMonth+=12; iYear--;}
1176 if (iMonth == 2 && IsLeapYear(iYear))
1177 iDay += 29;
1178 else
1179 iDay += days[iMonth];
1182 if (iSecond<0){iSecond+=60; iMinute--;}
1183 if (iMinute<0){iMinute+=60; iHour--;}
1184 if (iHour<0) {iHour+=24; iDay--;}
1185 if (iYear<=0) iYear+=2000;
1187 lpUd->st.wYear = iYear;
1188 lpUd->st.wMonth = iMonth;
1189 lpUd->st.wDay = iDay;
1190 lpUd->st.wHour = iHour;
1191 lpUd->st.wMinute = iMinute;
1192 lpUd->st.wSecond = iSecond;
1194 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1195 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1196 return S_OK;
1199 /**********************************************************************
1200 * DosDateTimeToVariantTime [OLEAUT32.14]
1202 * Convert a Dos format date and time into variant VT_DATE format.
1204 * PARAMS
1205 * wDosDate [I] Dos format date
1206 * wDosTime [I] Dos format time
1207 * pDateOut [O] Destination for VT_DATE format
1209 * RETURNS
1210 * Success: TRUE. pDateOut contains the converted time.
1211 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1213 * NOTES
1214 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1215 * - Dos format times are accurate to only 2 second precision.
1216 * - The format of a Dos Date is:
1217 *| Bits Values Meaning
1218 *| ---- ------ -------
1219 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1220 *| the days in the month rolls forward the extra days.
1221 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1222 *| year. 13-15 are invalid.
1223 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1224 * - The format of a Dos Time is:
1225 *| Bits Values Meaning
1226 *| ---- ------ -------
1227 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1228 *| 5-10 0-59 Minutes. 60-63 are invalid.
1229 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1231 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1232 double *pDateOut)
1234 UDATE ud;
1236 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1237 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1238 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1239 pDateOut);
1241 ud.st.wYear = DOS_YEAR(wDosDate);
1242 ud.st.wMonth = DOS_MONTH(wDosDate);
1243 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1244 return FALSE;
1245 ud.st.wDay = DOS_DAY(wDosDate);
1246 ud.st.wHour = DOS_HOUR(wDosTime);
1247 ud.st.wMinute = DOS_MINUTE(wDosTime);
1248 ud.st.wSecond = DOS_SECOND(wDosTime);
1249 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1250 if (ud.st.wHour > 23 || ud.st.wMinute > 59 || ud.st.wSecond > 59)
1251 return FALSE; /* Invalid values in Dos*/
1253 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1256 /**********************************************************************
1257 * VariantTimeToDosDateTime [OLEAUT32.13]
1259 * Convert a variant format date into a Dos format date and time.
1261 * dateIn [I] VT_DATE time format
1262 * pwDosDate [O] Destination for Dos format date
1263 * pwDosTime [O] Destination for Dos format time
1265 * RETURNS
1266 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1267 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1269 * NOTES
1270 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1272 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1274 UDATE ud;
1276 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1278 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1279 return FALSE;
1281 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1282 return FALSE;
1284 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1285 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1287 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1288 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1289 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1290 return TRUE;
1293 /***********************************************************************
1294 * SystemTimeToVariantTime [OLEAUT32.184]
1296 * Convert a System format date and time into variant VT_DATE format.
1298 * PARAMS
1299 * lpSt [I] System format date and time
1300 * pDateOut [O] Destination for VT_DATE format date
1302 * RETURNS
1303 * Success: TRUE. *pDateOut contains the converted value.
1304 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1306 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1308 UDATE ud;
1310 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1311 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1313 if (lpSt->wMonth > 12)
1314 return FALSE;
1316 ud.st = *lpSt;
1317 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1320 /***********************************************************************
1321 * VariantTimeToSystemTime [OLEAUT32.185]
1323 * Convert a variant VT_DATE into a System format date and time.
1325 * PARAMS
1326 * datein [I] Variant VT_DATE format date
1327 * lpSt [O] Destination for System format date and time
1329 * RETURNS
1330 * Success: TRUE. *lpSt contains the converted value.
1331 * Failure: FALSE, if dateIn is too large or small.
1333 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1335 UDATE ud;
1337 TRACE("(%g,%p)\n", dateIn, lpSt);
1339 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1340 return FALSE;
1342 *lpSt = ud.st;
1343 return TRUE;
1346 /***********************************************************************
1347 * VarDateFromUdateEx [OLEAUT32.319]
1349 * Convert an unpacked format date and time to a variant VT_DATE.
1351 * PARAMS
1352 * pUdateIn [I] Unpacked format date and time to convert
1353 * lcid [I] Locale identifier for the conversion
1354 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1355 * pDateOut [O] Destination for variant VT_DATE.
1357 * RETURNS
1358 * Success: S_OK. *pDateOut contains the converted value.
1359 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1361 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1363 UDATE ud;
1364 double dateVal, dateSign;
1366 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1367 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1368 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1369 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1370 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1372 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1373 FIXME("lcid possibly not handled, treating as en-us\n");
1375 ud = *pUdateIn;
1377 if (dwFlags & VAR_VALIDDATE)
1378 WARN("Ignoring VAR_VALIDDATE\n");
1380 if (FAILED(VARIANT_RollUdate(&ud)))
1381 return E_INVALIDARG;
1383 /* Date */
1384 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1386 /* Sign */
1387 dateSign = (dateVal < 0.0) ? -1.0 : 1.0;
1389 /* Time */
1390 dateVal += ud.st.wHour / 24.0 * dateSign;
1391 dateVal += ud.st.wMinute / 1440.0 * dateSign;
1392 dateVal += ud.st.wSecond / 86400.0 * dateSign;
1394 TRACE("Returning %g\n", dateVal);
1395 *pDateOut = dateVal;
1396 return S_OK;
1399 /***********************************************************************
1400 * VarDateFromUdate [OLEAUT32.330]
1402 * Convert an unpacked format date and time to a variant VT_DATE.
1404 * PARAMS
1405 * pUdateIn [I] Unpacked format date and time to convert
1406 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1407 * pDateOut [O] Destination for variant VT_DATE.
1409 * RETURNS
1410 * Success: S_OK. *pDateOut contains the converted value.
1411 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1413 * NOTES
1414 * This function uses the United States English locale for the conversion. Use
1415 * VarDateFromUdateEx() for alternate locales.
1417 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1419 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1421 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1424 /***********************************************************************
1425 * VarUdateFromDate [OLEAUT32.331]
1427 * Convert a variant VT_DATE into an unpacked format date and time.
1429 * PARAMS
1430 * datein [I] Variant VT_DATE format date
1431 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1432 * lpUdate [O] Destination for unpacked format date and time
1434 * RETURNS
1435 * Success: S_OK. *lpUdate contains the converted value.
1436 * Failure: E_INVALIDARG, if dateIn is too large or small.
1438 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1440 /* Cumulative totals of days per month */
1441 static const USHORT cumulativeDays[] =
1443 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1445 double datePart, timePart;
1446 int julianDays;
1448 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1450 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1451 return E_INVALIDARG;
1453 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1454 /* Compensate for int truncation (always downwards) */
1455 timePart = fabs(dateIn - datePart) + 0.00000000001;
1456 if (timePart >= 1.0)
1457 timePart -= 0.00000000001;
1459 /* Date */
1460 julianDays = VARIANT_JulianFromDate(dateIn);
1461 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1462 &lpUdate->st.wDay);
1464 datePart = (datePart + 1.5) / 7.0;
1465 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1466 if (lpUdate->st.wDayOfWeek == 0)
1467 lpUdate->st.wDayOfWeek = 5;
1468 else if (lpUdate->st.wDayOfWeek == 1)
1469 lpUdate->st.wDayOfWeek = 6;
1470 else
1471 lpUdate->st.wDayOfWeek -= 2;
1473 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1474 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1475 else
1476 lpUdate->wDayOfYear = 0;
1478 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1479 lpUdate->wDayOfYear += lpUdate->st.wDay;
1481 /* Time */
1482 timePart *= 24.0;
1483 lpUdate->st.wHour = timePart;
1484 timePart -= lpUdate->st.wHour;
1485 timePart *= 60.0;
1486 lpUdate->st.wMinute = timePart;
1487 timePart -= lpUdate->st.wMinute;
1488 timePart *= 60.0;
1489 lpUdate->st.wSecond = timePart;
1490 timePart -= lpUdate->st.wSecond;
1491 lpUdate->st.wMilliseconds = 0;
1492 if (timePart > 0.5)
1494 /* Round the milliseconds, adjusting the time/date forward if needed */
1495 if (lpUdate->st.wSecond < 59)
1496 lpUdate->st.wSecond++;
1497 else
1499 lpUdate->st.wSecond = 0;
1500 if (lpUdate->st.wMinute < 59)
1501 lpUdate->st.wMinute++;
1502 else
1504 lpUdate->st.wMinute = 0;
1505 if (lpUdate->st.wHour < 23)
1506 lpUdate->st.wHour++;
1507 else
1509 lpUdate->st.wHour = 0;
1510 /* Roll over a whole day */
1511 if (++lpUdate->st.wDay > 28)
1512 VARIANT_RollUdate(lpUdate);
1517 return S_OK;
1520 #define GET_NUMBER_TEXT(fld,name) \
1521 buff[0] = 0; \
1522 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1523 WARN("buffer too small for " #fld "\n"); \
1524 else \
1525 if (buff[0]) lpChars->name = buff[0]; \
1526 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1528 /* Get the valid number characters for an lcid */
1529 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1531 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1532 static CRITICAL_SECTION csLastChars = { NULL, -1, 0, 0, 0, 0 };
1533 static VARIANT_NUMBER_CHARS lastChars;
1534 static LCID lastLcid = -1;
1535 static DWORD lastFlags = 0;
1536 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1537 WCHAR buff[4];
1539 /* To make caching thread-safe, a critical section is needed */
1540 EnterCriticalSection(&csLastChars);
1542 /* Asking for default locale entries is very expensive: It is a registry
1543 server call. So cache one locally, as Microsoft does it too */
1544 if(lcid == lastLcid && dwFlags == lastFlags)
1546 memcpy(lpChars, &lastChars, sizeof(defaultChars));
1547 LeaveCriticalSection(&csLastChars);
1548 return;
1551 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1552 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1553 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1554 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1555 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1556 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1557 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1559 /* Local currency symbols are often 2 characters */
1560 lpChars->cCurrencyLocal2 = '\0';
1561 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1563 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1564 case 2: lpChars->cCurrencyLocal = buff[0];
1565 break;
1566 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1568 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1569 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1571 memcpy(&lastChars, lpChars, sizeof(defaultChars));
1572 lastLcid = lcid;
1573 lastFlags = dwFlags;
1574 LeaveCriticalSection(&csLastChars);
1577 /* Number Parsing States */
1578 #define B_PROCESSING_EXPONENT 0x1
1579 #define B_NEGATIVE_EXPONENT 0x2
1580 #define B_EXPONENT_START 0x4
1581 #define B_INEXACT_ZEROS 0x8
1582 #define B_LEADING_ZERO 0x10
1583 #define B_PROCESSING_HEX 0x20
1584 #define B_PROCESSING_OCT 0x40
1586 /**********************************************************************
1587 * VarParseNumFromStr [OLEAUT32.46]
1589 * Parse a string containing a number into a NUMPARSE structure.
1591 * PARAMS
1592 * lpszStr [I] String to parse number from
1593 * lcid [I] Locale Id for the conversion
1594 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1595 * pNumprs [I/O] Destination for parsed number
1596 * rgbDig [O] Destination for digits read in
1598 * RETURNS
1599 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1600 * the number.
1601 * Failure: E_INVALIDARG, if any parameter is invalid.
1602 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1603 * incorrectly.
1604 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1606 * NOTES
1607 * pNumprs must have the following fields set:
1608 * cDig: Set to the size of rgbDig.
1609 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1610 * from "oleauto.h".
1612 * FIXME
1613 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1614 * numerals, so this has not been implemented.
1616 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1617 NUMPARSE *pNumprs, BYTE *rgbDig)
1619 VARIANT_NUMBER_CHARS chars;
1620 BYTE rgbTmp[1024];
1621 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1622 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1623 int cchUsed = 0;
1625 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1627 if (!pNumprs || !rgbDig)
1628 return E_INVALIDARG;
1630 if (pNumprs->cDig < iMaxDigits)
1631 iMaxDigits = pNumprs->cDig;
1633 pNumprs->cDig = 0;
1634 pNumprs->dwOutFlags = 0;
1635 pNumprs->cchUsed = 0;
1636 pNumprs->nBaseShift = 0;
1637 pNumprs->nPwr10 = 0;
1639 if (!lpszStr)
1640 return DISP_E_TYPEMISMATCH;
1642 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1644 /* First consume all the leading symbols and space from the string */
1645 while (1)
1647 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1649 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1652 cchUsed++;
1653 lpszStr++;
1654 } while (isspaceW(*lpszStr));
1656 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1657 *lpszStr == chars.cPositiveSymbol &&
1658 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1660 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1661 cchUsed++;
1662 lpszStr++;
1664 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1665 *lpszStr == chars.cNegativeSymbol &&
1666 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1668 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1669 cchUsed++;
1670 lpszStr++;
1672 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1673 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1674 *lpszStr == chars.cCurrencyLocal &&
1675 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1677 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1678 cchUsed++;
1679 lpszStr++;
1680 /* Only accept currency characters */
1681 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1682 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1684 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1685 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1687 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1688 cchUsed++;
1689 lpszStr++;
1691 else
1692 break;
1695 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1697 /* Only accept non-currency characters */
1698 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1699 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1702 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1703 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1705 dwState |= B_PROCESSING_HEX;
1706 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1707 cchUsed=cchUsed+2;
1708 lpszStr=lpszStr+2;
1710 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1711 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1713 dwState |= B_PROCESSING_OCT;
1714 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1715 cchUsed=cchUsed+2;
1716 lpszStr=lpszStr+2;
1719 /* Strip Leading zeros */
1720 while (*lpszStr == '0')
1722 dwState |= B_LEADING_ZERO;
1723 cchUsed++;
1724 lpszStr++;
1727 while (*lpszStr)
1729 if (isdigitW(*lpszStr))
1731 if (dwState & B_PROCESSING_EXPONENT)
1733 int exponentSize = 0;
1734 if (dwState & B_EXPONENT_START)
1736 if (!isdigitW(*lpszStr))
1737 break; /* No exponent digits - invalid */
1738 while (*lpszStr == '0')
1740 /* Skip leading zero's in the exponent */
1741 cchUsed++;
1742 lpszStr++;
1746 while (isdigitW(*lpszStr))
1748 exponentSize *= 10;
1749 exponentSize += *lpszStr - '0';
1750 cchUsed++;
1751 lpszStr++;
1753 if (dwState & B_NEGATIVE_EXPONENT)
1754 exponentSize = -exponentSize;
1755 /* Add the exponent into the powers of 10 */
1756 pNumprs->nPwr10 += exponentSize;
1757 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1758 lpszStr--; /* back up to allow processing of next char */
1760 else
1762 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1763 && !(dwState & B_PROCESSING_OCT))
1765 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1767 if (*lpszStr != '0')
1768 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1770 /* This digit can't be represented, but count it in nPwr10 */
1771 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1772 pNumprs->nPwr10--;
1773 else
1774 pNumprs->nPwr10++;
1776 else
1778 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1779 return DISP_E_TYPEMISMATCH;
1782 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1783 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1785 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1787 pNumprs->cDig++;
1788 cchUsed++;
1791 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1793 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1794 cchUsed++;
1796 else if (*lpszStr == chars.cDecimalPoint &&
1797 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1798 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1800 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1801 cchUsed++;
1803 /* If we have no digits so far, skip leading zeros */
1804 if (!pNumprs->cDig)
1806 while (lpszStr[1] == '0')
1808 dwState |= B_LEADING_ZERO;
1809 cchUsed++;
1810 lpszStr++;
1811 pNumprs->nPwr10--;
1815 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1816 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1817 dwState & B_PROCESSING_HEX)
1819 if (pNumprs->cDig >= iMaxDigits)
1821 return DISP_E_OVERFLOW;
1823 else
1825 if (*lpszStr >= 'a')
1826 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1827 else
1828 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1830 pNumprs->cDig++;
1831 cchUsed++;
1833 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1834 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1835 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1837 dwState |= B_PROCESSING_EXPONENT;
1838 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1839 cchUsed++;
1841 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1843 cchUsed++; /* Ignore positive exponent */
1845 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1847 dwState |= B_NEGATIVE_EXPONENT;
1848 cchUsed++;
1850 else
1851 break; /* Stop at an unrecognised character */
1853 lpszStr++;
1856 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1858 /* Ensure a 0 on its own gets stored */
1859 pNumprs->cDig = 1;
1860 rgbTmp[0] = 0;
1863 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1865 pNumprs->cchUsed = cchUsed;
1866 WARN("didn't completely parse exponent\n");
1867 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1870 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1872 if (dwState & B_INEXACT_ZEROS)
1873 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1874 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1876 /* copy all of the digits into the output digit buffer */
1877 /* this is exactly what windows does although it also returns */
1878 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1879 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1881 if (dwState & B_PROCESSING_HEX) {
1882 /* hex numbers have always the same format */
1883 pNumprs->nPwr10=0;
1884 pNumprs->nBaseShift=4;
1885 } else {
1886 if (dwState & B_PROCESSING_OCT) {
1887 /* oct numbers have always the same format */
1888 pNumprs->nPwr10=0;
1889 pNumprs->nBaseShift=3;
1890 } else {
1891 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1893 pNumprs->nPwr10++;
1894 pNumprs->cDig--;
1898 } else
1900 /* Remove trailing zeros from the last (whole number or decimal) part */
1901 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1903 pNumprs->nPwr10++;
1904 pNumprs->cDig--;
1908 if (pNumprs->cDig <= iMaxDigits)
1909 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1910 else
1911 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1913 /* Copy the digits we processed into rgbDig */
1914 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1916 /* Consume any trailing symbols and space */
1917 while (1)
1919 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1921 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1924 cchUsed++;
1925 lpszStr++;
1926 } while (isspaceW(*lpszStr));
1928 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1929 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1930 *lpszStr == chars.cPositiveSymbol)
1932 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1933 cchUsed++;
1934 lpszStr++;
1936 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1937 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1938 *lpszStr == chars.cNegativeSymbol)
1940 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1941 cchUsed++;
1942 lpszStr++;
1944 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1945 pNumprs->dwOutFlags & NUMPRS_PARENS)
1947 cchUsed++;
1948 lpszStr++;
1949 pNumprs->dwOutFlags |= NUMPRS_NEG;
1951 else
1952 break;
1955 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1957 pNumprs->cchUsed = cchUsed;
1958 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1961 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1962 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1964 if (!pNumprs->cDig)
1965 return DISP_E_TYPEMISMATCH; /* No Number found */
1967 pNumprs->cchUsed = cchUsed;
1968 return S_OK;
1971 /* VTBIT flags indicating an integer value */
1972 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1973 /* VTBIT flags indicating a real number value */
1974 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1976 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1977 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1978 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1979 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1981 /**********************************************************************
1982 * VarNumFromParseNum [OLEAUT32.47]
1984 * Convert a NUMPARSE structure into a numeric Variant type.
1986 * PARAMS
1987 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1988 * rgbDig [I] Source for the numbers digits
1989 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1990 * pVarDst [O] Destination for the converted Variant value.
1992 * RETURNS
1993 * Success: S_OK. pVarDst contains the converted value.
1994 * Failure: E_INVALIDARG, if any parameter is invalid.
1995 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1997 * NOTES
1998 * - The smallest favoured type present in dwVtBits that can represent the
1999 * number in pNumprs without losing precision is used.
2000 * - Signed types are preferred over unsigned types of the same size.
2001 * - Preferred types in order are: integer, float, double, currency then decimal.
2002 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
2003 * for details of the rounding method.
2004 * - pVarDst is not cleared before the result is stored in it.
2005 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
2006 * design?): If some other VTBIT's for integers are specified together
2007 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
2008 * the number to the smallest requested integer truncating this way the
2009 * number. Wine doesn't implement this "feature" (yet?).
2011 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
2012 ULONG dwVtBits, VARIANT *pVarDst)
2014 /* Scale factors and limits for double arithmetic */
2015 static const double dblMultipliers[11] = {
2016 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2017 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2019 static const double dblMinimums[11] = {
2020 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
2021 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
2022 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
2024 static const double dblMaximums[11] = {
2025 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
2026 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
2027 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
2030 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
2032 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
2034 if (pNumprs->nBaseShift)
2036 /* nBaseShift indicates a hex or octal number */
2037 ULONG64 ul64 = 0;
2038 LONG64 l64;
2039 int i;
2041 /* Convert the hex or octal number string into a UI64 */
2042 for (i = 0; i < pNumprs->cDig; i++)
2044 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
2046 TRACE("Overflow multiplying digits\n");
2047 return DISP_E_OVERFLOW;
2049 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
2052 /* also make a negative representation */
2053 l64=-ul64;
2055 /* Try signed and unsigned types in size order */
2056 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
2058 V_VT(pVarDst) = VT_I1;
2059 V_I1(pVarDst) = ul64;
2060 return S_OK;
2062 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2064 V_VT(pVarDst) = VT_UI1;
2065 V_UI1(pVarDst) = ul64;
2066 return S_OK;
2068 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2070 V_VT(pVarDst) = VT_I2;
2071 V_I2(pVarDst) = ul64;
2072 return S_OK;
2074 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2076 V_VT(pVarDst) = VT_UI2;
2077 V_UI2(pVarDst) = ul64;
2078 return S_OK;
2080 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2082 V_VT(pVarDst) = VT_I4;
2083 V_I4(pVarDst) = ul64;
2084 return S_OK;
2086 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2088 V_VT(pVarDst) = VT_UI4;
2089 V_UI4(pVarDst) = ul64;
2090 return S_OK;
2092 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2094 V_VT(pVarDst) = VT_I8;
2095 V_I8(pVarDst) = ul64;
2096 return S_OK;
2098 else if (dwVtBits & VTBIT_UI8)
2100 V_VT(pVarDst) = VT_UI8;
2101 V_UI8(pVarDst) = ul64;
2102 return S_OK;
2104 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2106 V_VT(pVarDst) = VT_DECIMAL;
2107 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2108 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2109 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2110 return S_OK;
2112 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2114 V_VT(pVarDst) = VT_R4;
2115 if (ul64 <= I4_MAX)
2116 V_R4(pVarDst) = ul64;
2117 else
2118 V_R4(pVarDst) = l64;
2119 return S_OK;
2121 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2123 V_VT(pVarDst) = VT_R8;
2124 if (ul64 <= I4_MAX)
2125 V_R8(pVarDst) = ul64;
2126 else
2127 V_R8(pVarDst) = l64;
2128 return S_OK;
2131 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2132 return DISP_E_OVERFLOW;
2135 /* Count the number of relevant fractional and whole digits stored,
2136 * And compute the divisor/multiplier to scale the number by.
2138 if (pNumprs->nPwr10 < 0)
2140 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2142 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2143 wholeNumberDigits = 0;
2144 fractionalDigits = pNumprs->cDig;
2145 divisor10 = -pNumprs->nPwr10;
2147 else
2149 /* An exactly represented real number e.g. 1.024 */
2150 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2151 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2152 divisor10 = pNumprs->cDig - wholeNumberDigits;
2155 else if (pNumprs->nPwr10 == 0)
2157 /* An exactly represented whole number e.g. 1024 */
2158 wholeNumberDigits = pNumprs->cDig;
2159 fractionalDigits = 0;
2161 else /* pNumprs->nPwr10 > 0 */
2163 /* A whole number followed by nPwr10 0's e.g. 102400 */
2164 wholeNumberDigits = pNumprs->cDig;
2165 fractionalDigits = 0;
2166 multiplier10 = pNumprs->nPwr10;
2169 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2170 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2171 multiplier10, divisor10);
2173 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2174 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2176 /* We have one or more integer output choices, and either:
2177 * 1) An integer input value, or
2178 * 2) A real number input value but no floating output choices.
2179 * Alternately, we have a DECIMAL output available and an integer input.
2181 * So, place the integer value into pVarDst, using the smallest type
2182 * possible and preferring signed over unsigned types.
2184 BOOL bOverflow = FALSE, bNegative;
2185 ULONG64 ul64 = 0;
2186 int i;
2188 /* Convert the integer part of the number into a UI8 */
2189 for (i = 0; i < wholeNumberDigits; i++)
2191 if (ul64 > UI8_MAX / 10 || (ul64 == UI8_MAX / 10 && rgbDig[i] > UI8_MAX % 10))
2193 TRACE("Overflow multiplying digits\n");
2194 bOverflow = TRUE;
2195 break;
2197 ul64 = ul64 * 10 + rgbDig[i];
2200 /* Account for the scale of the number */
2201 if (!bOverflow && multiplier10)
2203 for (i = 0; i < multiplier10; i++)
2205 if (ul64 > (UI8_MAX / 10))
2207 TRACE("Overflow scaling number\n");
2208 bOverflow = TRUE;
2209 break;
2211 ul64 = ul64 * 10;
2215 /* If we have any fractional digits, round the value.
2216 * Note we don't have to do this if divisor10 is < 1,
2217 * because this means the fractional part must be < 0.5
2219 if (!bOverflow && fractionalDigits && divisor10 > 0)
2221 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2222 BOOL bAdjust = FALSE;
2224 TRACE("first decimal value is %d\n", *fracDig);
2226 if (*fracDig > 5)
2227 bAdjust = TRUE; /* > 0.5 */
2228 else if (*fracDig == 5)
2230 for (i = 1; i < fractionalDigits; i++)
2232 if (fracDig[i])
2234 bAdjust = TRUE; /* > 0.5 */
2235 break;
2238 /* If exactly 0.5, round only odd values */
2239 if (i == fractionalDigits && (ul64 & 1))
2240 bAdjust = TRUE;
2243 if (bAdjust)
2245 if (ul64 == UI8_MAX)
2247 TRACE("Overflow after rounding\n");
2248 bOverflow = TRUE;
2250 ul64++;
2254 /* Zero is not a negative number */
2255 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2257 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2259 /* For negative integers, try the signed types in size order */
2260 if (!bOverflow && bNegative)
2262 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2264 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2266 V_VT(pVarDst) = VT_I1;
2267 V_I1(pVarDst) = -ul64;
2268 return S_OK;
2270 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2272 V_VT(pVarDst) = VT_I2;
2273 V_I2(pVarDst) = -ul64;
2274 return S_OK;
2276 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2278 V_VT(pVarDst) = VT_I4;
2279 V_I4(pVarDst) = -ul64;
2280 return S_OK;
2282 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2284 V_VT(pVarDst) = VT_I8;
2285 V_I8(pVarDst) = -ul64;
2286 return S_OK;
2288 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2290 /* Decimal is only output choice left - fast path */
2291 V_VT(pVarDst) = VT_DECIMAL;
2292 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2293 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2294 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2295 return S_OK;
2299 else if (!bOverflow)
2301 /* For positive integers, try signed then unsigned types in size order */
2302 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2304 V_VT(pVarDst) = VT_I1;
2305 V_I1(pVarDst) = ul64;
2306 return S_OK;
2308 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2310 V_VT(pVarDst) = VT_UI1;
2311 V_UI1(pVarDst) = ul64;
2312 return S_OK;
2314 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2316 V_VT(pVarDst) = VT_I2;
2317 V_I2(pVarDst) = ul64;
2318 return S_OK;
2320 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2322 V_VT(pVarDst) = VT_UI2;
2323 V_UI2(pVarDst) = ul64;
2324 return S_OK;
2326 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2328 V_VT(pVarDst) = VT_I4;
2329 V_I4(pVarDst) = ul64;
2330 return S_OK;
2332 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2334 V_VT(pVarDst) = VT_UI4;
2335 V_UI4(pVarDst) = ul64;
2336 return S_OK;
2338 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2340 V_VT(pVarDst) = VT_I8;
2341 V_I8(pVarDst) = ul64;
2342 return S_OK;
2344 else if (dwVtBits & VTBIT_UI8)
2346 V_VT(pVarDst) = VT_UI8;
2347 V_UI8(pVarDst) = ul64;
2348 return S_OK;
2350 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2352 /* Decimal is only output choice left - fast path */
2353 V_VT(pVarDst) = VT_DECIMAL;
2354 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2355 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2356 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2357 return S_OK;
2362 if (dwVtBits & REAL_VTBITS)
2364 /* Try to put the number into a float or real */
2365 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2366 double whole = 0.0;
2367 int i;
2369 /* Convert the number into a double */
2370 for (i = 0; i < pNumprs->cDig; i++)
2371 whole = whole * 10.0 + rgbDig[i];
2373 TRACE("Whole double value is %16.16g\n", whole);
2375 /* Account for the scale */
2376 while (multiplier10 > 10)
2378 if (whole > dblMaximums[10])
2380 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2381 bOverflow = TRUE;
2382 break;
2384 whole = whole * dblMultipliers[10];
2385 multiplier10 -= 10;
2387 if (multiplier10 && !bOverflow)
2389 if (whole > dblMaximums[multiplier10])
2391 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2392 bOverflow = TRUE;
2394 else
2395 whole = whole * dblMultipliers[multiplier10];
2398 if (!bOverflow)
2399 TRACE("Scaled double value is %16.16g\n", whole);
2401 while (divisor10 > 10 && !bOverflow)
2403 if (whole < dblMinimums[10] && whole != 0)
2405 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2406 bOverflow = TRUE;
2407 break;
2409 whole = whole / dblMultipliers[10];
2410 divisor10 -= 10;
2412 if (divisor10 && !bOverflow)
2414 if (whole < dblMinimums[divisor10] && whole != 0)
2416 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2417 bOverflow = TRUE;
2419 else
2420 whole = whole / dblMultipliers[divisor10];
2422 if (!bOverflow)
2423 TRACE("Final double value is %16.16g\n", whole);
2425 if (dwVtBits & VTBIT_R4 &&
2426 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2428 TRACE("Set R4 to final value\n");
2429 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2430 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2431 return S_OK;
2434 if (dwVtBits & VTBIT_R8)
2436 TRACE("Set R8 to final value\n");
2437 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2438 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2439 return S_OK;
2442 if (dwVtBits & VTBIT_CY)
2444 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2446 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2447 TRACE("Set CY to final value\n");
2448 return S_OK;
2450 TRACE("Value Overflows CY\n");
2454 if (dwVtBits & VTBIT_DECIMAL)
2456 int i;
2457 ULONG carry;
2458 ULONG64 tmp;
2459 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2461 DECIMAL_SETZERO(*pDec);
2462 DEC_LO32(pDec) = 0;
2464 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2465 DEC_SIGN(pDec) = DECIMAL_NEG;
2466 else
2467 DEC_SIGN(pDec) = DECIMAL_POS;
2469 /* Factor the significant digits */
2470 for (i = 0; i < pNumprs->cDig; i++)
2472 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2473 carry = (ULONG)(tmp >> 32);
2474 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2475 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2476 carry = (ULONG)(tmp >> 32);
2477 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2478 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2479 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2481 if (tmp >> 32 & UI4_MAX)
2483 VarNumFromParseNum_DecOverflow:
2484 TRACE("Overflow\n");
2485 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2486 return DISP_E_OVERFLOW;
2490 /* Account for the scale of the number */
2491 while (multiplier10 > 0)
2493 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2494 carry = (ULONG)(tmp >> 32);
2495 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2496 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2497 carry = (ULONG)(tmp >> 32);
2498 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2499 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2500 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2502 if (tmp >> 32 & UI4_MAX)
2503 goto VarNumFromParseNum_DecOverflow;
2504 multiplier10--;
2506 DEC_SCALE(pDec) = divisor10;
2508 V_VT(pVarDst) = VT_DECIMAL;
2509 return S_OK;
2511 return DISP_E_OVERFLOW; /* No more output choices */
2514 /**********************************************************************
2515 * VarCat [OLEAUT32.318]
2517 * Concatenates one variant onto another.
2519 * PARAMS
2520 * left [I] First variant
2521 * right [I] Second variant
2522 * result [O] Result variant
2524 * RETURNS
2525 * Success: S_OK.
2526 * Failure: An HRESULT error code indicating the error.
2528 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2530 VARTYPE leftvt,rightvt,resultvt;
2531 HRESULT hres;
2532 static WCHAR str_true[32];
2533 static WCHAR str_false[32];
2534 static const WCHAR sz_empty[] = {'\0'};
2535 leftvt = V_VT(left);
2536 rightvt = V_VT(right);
2538 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2539 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2541 if (!str_true[0]) {
2542 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_FALSE, str_false);
2543 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_TRUE, str_true);
2546 /* when both left and right are NULL the result is NULL */
2547 if (leftvt == VT_NULL && rightvt == VT_NULL)
2549 V_VT(out) = VT_NULL;
2550 return S_OK;
2553 hres = S_OK;
2554 resultvt = VT_EMPTY;
2556 /* There are many special case for errors and return types */
2557 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2558 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2559 hres = DISP_E_TYPEMISMATCH;
2560 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2561 leftvt == VT_R4 || leftvt == VT_R8 ||
2562 leftvt == VT_CY || leftvt == VT_BOOL ||
2563 leftvt == VT_BSTR || leftvt == VT_I1 ||
2564 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2565 leftvt == VT_UI4 || leftvt == VT_I8 ||
2566 leftvt == VT_UI8 || leftvt == VT_INT ||
2567 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2568 leftvt == VT_NULL || leftvt == VT_DATE ||
2569 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2571 (rightvt == VT_I2 || rightvt == VT_I4 ||
2572 rightvt == VT_R4 || rightvt == VT_R8 ||
2573 rightvt == VT_CY || rightvt == VT_BOOL ||
2574 rightvt == VT_BSTR || rightvt == VT_I1 ||
2575 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2576 rightvt == VT_UI4 || rightvt == VT_I8 ||
2577 rightvt == VT_UI8 || rightvt == VT_INT ||
2578 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2579 rightvt == VT_NULL || rightvt == VT_DATE ||
2580 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2581 resultvt = VT_BSTR;
2582 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2583 hres = DISP_E_TYPEMISMATCH;
2584 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2585 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2586 hres = DISP_E_TYPEMISMATCH;
2587 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2588 rightvt == VT_DECIMAL)
2589 hres = DISP_E_BADVARTYPE;
2590 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2591 hres = DISP_E_TYPEMISMATCH;
2592 else if (leftvt == VT_VARIANT)
2593 hres = DISP_E_TYPEMISMATCH;
2594 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2595 leftvt == VT_NULL || leftvt == VT_I2 ||
2596 leftvt == VT_I4 || leftvt == VT_R4 ||
2597 leftvt == VT_R8 || leftvt == VT_CY ||
2598 leftvt == VT_DATE || leftvt == VT_BSTR ||
2599 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2600 leftvt == VT_I1 || leftvt == VT_UI1 ||
2601 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2602 leftvt == VT_I8 || leftvt == VT_UI8 ||
2603 leftvt == VT_INT || leftvt == VT_UINT))
2604 hres = DISP_E_TYPEMISMATCH;
2605 else
2606 hres = DISP_E_BADVARTYPE;
2608 /* if result type is not S_OK, then no need to go further */
2609 if (hres != S_OK)
2611 V_VT(out) = resultvt;
2612 return hres;
2614 /* Else proceed with formatting inputs to strings */
2615 else
2617 VARIANT bstrvar_left, bstrvar_right;
2618 V_VT(out) = VT_BSTR;
2620 VariantInit(&bstrvar_left);
2621 VariantInit(&bstrvar_right);
2623 /* Convert left side variant to string */
2624 if (leftvt != VT_BSTR)
2626 if (leftvt == VT_BOOL)
2628 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2629 V_VT(&bstrvar_left) = VT_BSTR;
2630 if (V_BOOL(left))
2631 V_BSTR(&bstrvar_left) = SysAllocString(str_true);
2632 else
2633 V_BSTR(&bstrvar_left) = SysAllocString(str_false);
2635 /* Fill with empty string for later concat with right side */
2636 else if (leftvt == VT_NULL)
2638 V_VT(&bstrvar_left) = VT_BSTR;
2639 V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
2641 else
2643 hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
2644 if (hres != S_OK) {
2645 VariantClear(&bstrvar_left);
2646 VariantClear(&bstrvar_right);
2647 if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
2648 rightvt == VT_NULL || rightvt == VT_I2 ||
2649 rightvt == VT_I4 || rightvt == VT_R4 ||
2650 rightvt == VT_R8 || rightvt == VT_CY ||
2651 rightvt == VT_DATE || rightvt == VT_BSTR ||
2652 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
2653 rightvt == VT_I1 || rightvt == VT_UI1 ||
2654 rightvt == VT_UI2 || rightvt == VT_UI4 ||
2655 rightvt == VT_I8 || rightvt == VT_UI8 ||
2656 rightvt == VT_INT || rightvt == VT_UINT))
2657 return DISP_E_BADVARTYPE;
2658 return hres;
2663 /* convert right side variant to string */
2664 if (rightvt != VT_BSTR)
2666 if (rightvt == VT_BOOL)
2668 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2669 V_VT(&bstrvar_right) = VT_BSTR;
2670 if (V_BOOL(right))
2671 V_BSTR(&bstrvar_right) = SysAllocString(str_true);
2672 else
2673 V_BSTR(&bstrvar_right) = SysAllocString(str_false);
2675 /* Fill with empty string for later concat with right side */
2676 else if (rightvt == VT_NULL)
2678 V_VT(&bstrvar_right) = VT_BSTR;
2679 V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
2681 else
2683 hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
2684 if (hres != S_OK) {
2685 VariantClear(&bstrvar_left);
2686 VariantClear(&bstrvar_right);
2687 if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
2688 leftvt == VT_NULL || leftvt == VT_I2 ||
2689 leftvt == VT_I4 || leftvt == VT_R4 ||
2690 leftvt == VT_R8 || leftvt == VT_CY ||
2691 leftvt == VT_DATE || leftvt == VT_BSTR ||
2692 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2693 leftvt == VT_I1 || leftvt == VT_UI1 ||
2694 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2695 leftvt == VT_I8 || leftvt == VT_UI8 ||
2696 leftvt == VT_INT || leftvt == VT_UINT))
2697 return DISP_E_BADVARTYPE;
2698 return hres;
2703 /* Concat the resulting strings together */
2704 if (leftvt == VT_BSTR && rightvt == VT_BSTR)
2705 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2706 else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
2707 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2708 else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
2709 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
2710 else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
2711 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2713 VariantClear(&bstrvar_left);
2714 VariantClear(&bstrvar_right);
2715 return S_OK;
2720 /* Wrapper around VariantChangeTypeEx() which permits changing a
2721 variant with VT_RESERVED flag set. Needed by VarCmp. */
2722 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2723 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2725 VARIANTARG vtmpsrc = *pvargSrc;
2727 V_VT(&vtmpsrc) &= ~VT_RESERVED;
2728 return VariantChangeTypeEx(pvargDest,&vtmpsrc,lcid,wFlags,vt);
2731 /**********************************************************************
2732 * VarCmp [OLEAUT32.176]
2734 * Compare two variants.
2736 * PARAMS
2737 * left [I] First variant
2738 * right [I] Second variant
2739 * lcid [I] LCID (locale identifier) for the comparison
2740 * flags [I] Flags to be used in the comparison:
2741 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2742 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2744 * RETURNS
2745 * VARCMP_LT: left variant is less than right variant.
2746 * VARCMP_EQ: input variants are equal.
2747 * VARCMP_GT: left variant is greater than right variant.
2748 * VARCMP_NULL: either one of the input variants is NULL.
2749 * Failure: An HRESULT error code indicating the error.
2751 * NOTES
2752 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2753 * UI8 and UINT as input variants. INT is accepted only as left variant.
2755 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2756 * an ERROR variant will trigger an error.
2758 * Both input variants can have VT_RESERVED flag set which is ignored
2759 * unless one and only one of the variants is a BSTR and the other one
2760 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2761 * different meaning:
2762 * - BSTR and other: BSTR is always greater than the other variant.
2763 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2764 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2765 * comparison will take place else the BSTR is always greater.
2766 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2767 * variant is ignored and the return value depends only on the sign
2768 * of the BSTR if it is a number else the BSTR is always greater. A
2769 * positive BSTR is greater, a negative one is smaller than the other
2770 * variant.
2772 * SEE
2773 * VarBstrCmp for the lcid and flags usage.
2775 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2777 VARTYPE lvt, rvt, vt;
2778 VARIANT rv,lv;
2779 DWORD xmask;
2780 HRESULT rc;
2782 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%08x)\n", left, debugstr_VT(left),
2783 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2785 lvt = V_VT(left) & VT_TYPEMASK;
2786 rvt = V_VT(right) & VT_TYPEMASK;
2787 xmask = (1 << lvt) | (1 << rvt);
2789 /* If we have any flag set except VT_RESERVED bail out.
2790 Same for the left input variant type > VT_INT and for the
2791 right input variant type > VT_I8. Yes, VT_INT is only supported
2792 as left variant. Go figure */
2793 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2794 lvt > VT_INT || rvt > VT_I8) {
2795 return DISP_E_BADVARTYPE;
2798 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2799 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2800 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2801 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2802 return DISP_E_TYPEMISMATCH;
2804 /* If both variants are VT_ERROR return VARCMP_EQ */
2805 if (xmask == VTBIT_ERROR)
2806 return VARCMP_EQ;
2807 else if (xmask & VTBIT_ERROR)
2808 return DISP_E_TYPEMISMATCH;
2810 if (xmask & VTBIT_NULL)
2811 return VARCMP_NULL;
2813 VariantInit(&lv);
2814 VariantInit(&rv);
2816 /* Two BSTRs, ignore VT_RESERVED */
2817 if (xmask == VTBIT_BSTR)
2818 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2820 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2821 if (xmask & VTBIT_BSTR) {
2822 VARIANT *bstrv, *nonbv;
2823 VARTYPE nonbvt;
2824 int swap = 0;
2826 /* Swap the variants so the BSTR is always on the left */
2827 if (lvt == VT_BSTR) {
2828 bstrv = left;
2829 nonbv = right;
2830 nonbvt = rvt;
2831 } else {
2832 swap = 1;
2833 bstrv = right;
2834 nonbv = left;
2835 nonbvt = lvt;
2838 /* BSTR and EMPTY: ignore VT_RESERVED */
2839 if (nonbvt == VT_EMPTY)
2840 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2841 else {
2842 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2843 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2845 if (!breserv && !nreserv)
2846 /* No VT_RESERVED set ==> BSTR always greater */
2847 rc = VARCMP_GT;
2848 else if (breserv && !nreserv) {
2849 /* BSTR has VT_RESERVED set. Do a string comparison */
2850 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2851 if (FAILED(rc))
2852 return rc;
2853 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2854 VariantClear(&rv);
2855 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2856 /* Non NULL nor empty BSTR */
2857 /* If the BSTR is not a number the BSTR is greater */
2858 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2859 if (FAILED(rc))
2860 rc = VARCMP_GT;
2861 else if (breserv && nreserv)
2862 /* FIXME: This is strange: with both VT_RESERVED set it
2863 looks like the result depends only on the sign of
2864 the BSTR number */
2865 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2866 else
2867 /* Numeric comparison, will be handled below.
2868 VARCMP_NULL used only to break out. */
2869 rc = VARCMP_NULL;
2870 VariantClear(&lv);
2871 VariantClear(&rv);
2872 } else
2873 /* Empty or NULL BSTR */
2874 rc = VARCMP_GT;
2876 /* Fixup the return code if we swapped left and right */
2877 if (swap) {
2878 if (rc == VARCMP_GT)
2879 rc = VARCMP_LT;
2880 else if (rc == VARCMP_LT)
2881 rc = VARCMP_GT;
2883 if (rc != VARCMP_NULL)
2884 return rc;
2887 if (xmask & VTBIT_DECIMAL)
2888 vt = VT_DECIMAL;
2889 else if (xmask & VTBIT_BSTR)
2890 vt = VT_R8;
2891 else if (xmask & VTBIT_R4)
2892 vt = VT_R4;
2893 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2894 vt = VT_R8;
2895 else if (xmask & VTBIT_CY)
2896 vt = VT_CY;
2897 else
2898 /* default to I8 */
2899 vt = VT_I8;
2901 /* Coerce the variants */
2902 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2903 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2904 /* Overflow, change to R8 */
2905 vt = VT_R8;
2906 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2908 if (FAILED(rc))
2909 return rc;
2910 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2911 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2912 /* Overflow, change to R8 */
2913 vt = VT_R8;
2914 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2915 if (FAILED(rc))
2916 return rc;
2917 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2919 if (FAILED(rc))
2920 return rc;
2922 #define _VARCMP(a,b) \
2923 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2925 switch (vt) {
2926 case VT_CY:
2927 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2928 case VT_DECIMAL:
2929 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2930 case VT_I8:
2931 return _VARCMP(V_I8(&lv), V_I8(&rv));
2932 case VT_R4:
2933 return _VARCMP(V_R4(&lv), V_R4(&rv));
2934 case VT_R8:
2935 return _VARCMP(V_R8(&lv), V_R8(&rv));
2936 default:
2937 /* We should never get here */
2938 return E_FAIL;
2940 #undef _VARCMP
2943 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
2945 HRESULT hres;
2946 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
2948 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
2949 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
2950 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
2951 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
2952 NULL, NULL);
2953 } else {
2954 hres = DISP_E_TYPEMISMATCH;
2956 return hres;
2959 /**********************************************************************
2960 * VarAnd [OLEAUT32.142]
2962 * Computes the logical AND of two variants.
2964 * PARAMS
2965 * left [I] First variant
2966 * right [I] Second variant
2967 * result [O] Result variant
2969 * RETURNS
2970 * Success: S_OK.
2971 * Failure: An HRESULT error code indicating the error.
2973 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2975 HRESULT hres = S_OK;
2976 VARTYPE resvt = VT_EMPTY;
2977 VARTYPE leftvt,rightvt;
2978 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2979 VARIANT varLeft, varRight;
2980 VARIANT tempLeft, tempRight;
2982 VariantInit(&varLeft);
2983 VariantInit(&varRight);
2984 VariantInit(&tempLeft);
2985 VariantInit(&tempRight);
2987 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2988 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2990 /* Handle VT_DISPATCH by storing and taking address of returned value */
2991 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2993 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2994 if (FAILED(hres)) goto VarAnd_Exit;
2995 left = &tempLeft;
2997 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2999 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3000 if (FAILED(hres)) goto VarAnd_Exit;
3001 right = &tempRight;
3004 leftvt = V_VT(left)&VT_TYPEMASK;
3005 rightvt = V_VT(right)&VT_TYPEMASK;
3006 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3007 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3009 if (leftExtraFlags != rightExtraFlags)
3011 hres = DISP_E_BADVARTYPE;
3012 goto VarAnd_Exit;
3014 ExtraFlags = leftExtraFlags;
3016 /* Native VarAnd always returns an error when using extra
3017 * flags or if the variant combination is I8 and INT.
3019 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
3020 (leftvt == VT_INT && rightvt == VT_I8) ||
3021 ExtraFlags != 0)
3023 hres = DISP_E_BADVARTYPE;
3024 goto VarAnd_Exit;
3027 /* Determine return type */
3028 else if (leftvt == VT_I8 || rightvt == VT_I8)
3029 resvt = VT_I8;
3030 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
3031 leftvt == VT_UINT || rightvt == VT_UINT ||
3032 leftvt == VT_INT || rightvt == VT_INT ||
3033 leftvt == VT_UINT || rightvt == VT_UINT ||
3034 leftvt == VT_R4 || rightvt == VT_R4 ||
3035 leftvt == VT_R8 || rightvt == VT_R8 ||
3036 leftvt == VT_CY || rightvt == VT_CY ||
3037 leftvt == VT_DATE || rightvt == VT_DATE ||
3038 leftvt == VT_I1 || rightvt == VT_I1 ||
3039 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3040 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3041 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3042 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3043 resvt = VT_I4;
3044 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
3045 leftvt == VT_I2 || rightvt == VT_I2 ||
3046 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
3047 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
3048 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
3049 (leftvt == VT_UI1 && rightvt == VT_UI1))
3050 resvt = VT_UI1;
3051 else
3052 resvt = VT_I2;
3053 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
3054 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3055 resvt = VT_BOOL;
3056 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
3057 leftvt == VT_BSTR || rightvt == VT_BSTR)
3058 resvt = VT_NULL;
3059 else
3061 hres = DISP_E_BADVARTYPE;
3062 goto VarAnd_Exit;
3065 if (leftvt == VT_NULL || rightvt == VT_NULL)
3068 * Special cases for when left variant is VT_NULL
3069 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
3071 if (leftvt == VT_NULL)
3073 VARIANT_BOOL b;
3074 switch(rightvt)
3076 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3077 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3078 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3079 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3080 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3081 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3082 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3083 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3084 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3085 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3086 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3087 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3088 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3089 case VT_CY:
3090 if(V_CY(right).int64)
3091 resvt = VT_NULL;
3092 break;
3093 case VT_DECIMAL:
3094 if (DEC_HI32(&V_DECIMAL(right)) ||
3095 DEC_LO64(&V_DECIMAL(right)))
3096 resvt = VT_NULL;
3097 break;
3098 case VT_BSTR:
3099 hres = VarBoolFromStr(V_BSTR(right),
3100 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3101 if (FAILED(hres))
3102 return hres;
3103 else if (b)
3104 V_VT(result) = VT_NULL;
3105 else
3107 V_VT(result) = VT_BOOL;
3108 V_BOOL(result) = b;
3110 goto VarAnd_Exit;
3113 V_VT(result) = resvt;
3114 goto VarAnd_Exit;
3117 hres = VariantCopy(&varLeft, left);
3118 if (FAILED(hres)) goto VarAnd_Exit;
3120 hres = VariantCopy(&varRight, right);
3121 if (FAILED(hres)) goto VarAnd_Exit;
3123 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3124 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3125 else
3127 double d;
3129 if (V_VT(&varLeft) == VT_BSTR &&
3130 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3131 LOCALE_USER_DEFAULT, 0, &d)))
3132 hres = VariantChangeType(&varLeft,&varLeft,
3133 VARIANT_LOCALBOOL, VT_BOOL);
3134 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3135 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3136 if (FAILED(hres)) goto VarAnd_Exit;
3139 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3140 V_VT(&varRight) = VT_I4; /* Don't overflow */
3141 else
3143 double d;
3145 if (V_VT(&varRight) == VT_BSTR &&
3146 FAILED(VarR8FromStr(V_BSTR(&varRight),
3147 LOCALE_USER_DEFAULT, 0, &d)))
3148 hres = VariantChangeType(&varRight, &varRight,
3149 VARIANT_LOCALBOOL, VT_BOOL);
3150 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3151 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3152 if (FAILED(hres)) goto VarAnd_Exit;
3155 V_VT(result) = resvt;
3156 switch(resvt)
3158 case VT_I8:
3159 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3160 break;
3161 case VT_I4:
3162 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3163 break;
3164 case VT_I2:
3165 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3166 break;
3167 case VT_UI1:
3168 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3169 break;
3170 case VT_BOOL:
3171 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3172 break;
3173 default:
3174 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3175 leftvt,rightvt);
3178 VarAnd_Exit:
3179 VariantClear(&varLeft);
3180 VariantClear(&varRight);
3181 VariantClear(&tempLeft);
3182 VariantClear(&tempRight);
3184 return hres;
3187 /**********************************************************************
3188 * VarAdd [OLEAUT32.141]
3190 * Add two variants.
3192 * PARAMS
3193 * left [I] First variant
3194 * right [I] Second variant
3195 * result [O] Result variant
3197 * RETURNS
3198 * Success: S_OK.
3199 * Failure: An HRESULT error code indicating the error.
3201 * NOTES
3202 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3203 * UI8, INT and UINT as input variants.
3205 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3206 * same here.
3208 * FIXME
3209 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3210 * case.
3212 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3214 HRESULT hres;
3215 VARTYPE lvt, rvt, resvt, tvt;
3216 VARIANT lv, rv, tv;
3217 VARIANT tempLeft, tempRight;
3218 double r8res;
3220 /* Variant priority for coercion. Sorted from lowest to highest.
3221 VT_ERROR shows an invalid input variant type. */
3222 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3223 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3224 vt_ERROR };
3225 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3226 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3227 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3228 VT_NULL, VT_ERROR };
3230 /* Mapping for coercion from input variant to priority of result variant. */
3231 static const VARTYPE coerce[] = {
3232 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3233 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3234 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3235 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3236 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3237 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3238 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3239 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3242 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3243 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3244 result);
3246 VariantInit(&lv);
3247 VariantInit(&rv);
3248 VariantInit(&tv);
3249 VariantInit(&tempLeft);
3250 VariantInit(&tempRight);
3252 /* Handle VT_DISPATCH by storing and taking address of returned value */
3253 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3255 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3257 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3258 if (FAILED(hres)) goto end;
3259 left = &tempLeft;
3261 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3263 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3264 if (FAILED(hres)) goto end;
3265 right = &tempRight;
3269 lvt = V_VT(left)&VT_TYPEMASK;
3270 rvt = V_VT(right)&VT_TYPEMASK;
3272 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3273 Same for any input variant type > VT_I8 */
3274 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3275 lvt > VT_I8 || rvt > VT_I8) {
3276 hres = DISP_E_BADVARTYPE;
3277 goto end;
3280 /* Determine the variant type to coerce to. */
3281 if (coerce[lvt] > coerce[rvt]) {
3282 resvt = prio2vt[coerce[lvt]];
3283 tvt = prio2vt[coerce[rvt]];
3284 } else {
3285 resvt = prio2vt[coerce[rvt]];
3286 tvt = prio2vt[coerce[lvt]];
3289 /* Special cases where the result variant type is defined by both
3290 input variants and not only that with the highest priority */
3291 if (resvt == VT_BSTR) {
3292 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3293 resvt = VT_BSTR;
3294 else
3295 resvt = VT_R8;
3297 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3298 resvt = VT_R8;
3300 /* For overflow detection use the biggest compatible type for the
3301 addition */
3302 switch (resvt) {
3303 case VT_ERROR:
3304 hres = DISP_E_BADVARTYPE;
3305 goto end;
3306 case VT_NULL:
3307 hres = S_OK;
3308 V_VT(result) = VT_NULL;
3309 goto end;
3310 case VT_DISPATCH:
3311 FIXME("cannot handle variant type VT_DISPATCH\n");
3312 hres = DISP_E_TYPEMISMATCH;
3313 goto end;
3314 case VT_EMPTY:
3315 resvt = VT_I2;
3316 /* Fall through */
3317 case VT_UI1:
3318 case VT_I2:
3319 case VT_I4:
3320 case VT_I8:
3321 tvt = VT_I8;
3322 break;
3323 case VT_DATE:
3324 case VT_R4:
3325 tvt = VT_R8;
3326 break;
3327 default:
3328 tvt = resvt;
3331 /* Now coerce the variants */
3332 hres = VariantChangeType(&lv, left, 0, tvt);
3333 if (FAILED(hres))
3334 goto end;
3335 hres = VariantChangeType(&rv, right, 0, tvt);
3336 if (FAILED(hres))
3337 goto end;
3339 /* Do the math */
3340 hres = S_OK;
3341 V_VT(result) = resvt;
3342 switch (tvt) {
3343 case VT_DECIMAL:
3344 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3345 &V_DECIMAL(result));
3346 goto end;
3347 case VT_CY:
3348 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3349 goto end;
3350 case VT_BSTR:
3351 /* We do not add those, we concatenate them. */
3352 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3353 goto end;
3354 case VT_I8:
3355 /* Overflow detection */
3356 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3357 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3358 V_VT(result) = VT_R8;
3359 V_R8(result) = r8res;
3360 goto end;
3361 } else {
3362 V_VT(&tv) = tvt;
3363 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3365 break;
3366 case VT_R8:
3367 V_VT(&tv) = tvt;
3368 /* FIXME: overflow detection */
3369 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3370 break;
3371 default:
3372 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3373 break;
3375 if (resvt != tvt) {
3376 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3377 /* Overflow! Change to the vartype with the next higher priority.
3378 With one exception: I4 ==> R8 even if it would fit in I8 */
3379 if (resvt == VT_I4)
3380 resvt = VT_R8;
3381 else
3382 resvt = prio2vt[coerce[resvt] + 1];
3383 hres = VariantChangeType(result, &tv, 0, resvt);
3385 } else
3386 hres = VariantCopy(result, &tv);
3388 end:
3389 if (hres != S_OK) {
3390 V_VT(result) = VT_EMPTY;
3391 V_I4(result) = 0; /* No V_EMPTY */
3393 VariantClear(&lv);
3394 VariantClear(&rv);
3395 VariantClear(&tv);
3396 VariantClear(&tempLeft);
3397 VariantClear(&tempRight);
3398 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3399 return hres;
3402 /**********************************************************************
3403 * VarMul [OLEAUT32.156]
3405 * Multiply two variants.
3407 * PARAMS
3408 * left [I] First variant
3409 * right [I] Second variant
3410 * result [O] Result variant
3412 * RETURNS
3413 * Success: S_OK.
3414 * Failure: An HRESULT error code indicating the error.
3416 * NOTES
3417 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3418 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3420 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3421 * same here.
3423 * FIXME
3424 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3425 * case.
3427 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3429 HRESULT hres;
3430 VARTYPE lvt, rvt, resvt, tvt;
3431 VARIANT lv, rv, tv;
3432 VARIANT tempLeft, tempRight;
3433 double r8res;
3435 /* Variant priority for coercion. Sorted from lowest to highest.
3436 VT_ERROR shows an invalid input variant type. */
3437 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3438 vt_DECIMAL, vt_NULL, vt_ERROR };
3439 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3440 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3441 VT_DECIMAL, VT_NULL, VT_ERROR };
3443 /* Mapping for coercion from input variant to priority of result variant. */
3444 static const VARTYPE coerce[] = {
3445 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3446 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3447 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3448 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3449 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3450 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3451 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3452 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3455 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3456 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3457 result);
3459 VariantInit(&lv);
3460 VariantInit(&rv);
3461 VariantInit(&tv);
3462 VariantInit(&tempLeft);
3463 VariantInit(&tempRight);
3465 /* Handle VT_DISPATCH by storing and taking address of returned value */
3466 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3468 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3469 if (FAILED(hres)) goto end;
3470 left = &tempLeft;
3472 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3474 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3475 if (FAILED(hres)) goto end;
3476 right = &tempRight;
3479 lvt = V_VT(left)&VT_TYPEMASK;
3480 rvt = V_VT(right)&VT_TYPEMASK;
3482 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3483 Same for any input variant type > VT_I8 */
3484 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3485 lvt > VT_I8 || rvt > VT_I8) {
3486 hres = DISP_E_BADVARTYPE;
3487 goto end;
3490 /* Determine the variant type to coerce to. */
3491 if (coerce[lvt] > coerce[rvt]) {
3492 resvt = prio2vt[coerce[lvt]];
3493 tvt = prio2vt[coerce[rvt]];
3494 } else {
3495 resvt = prio2vt[coerce[rvt]];
3496 tvt = prio2vt[coerce[lvt]];
3499 /* Special cases where the result variant type is defined by both
3500 input variants and not only that with the highest priority */
3501 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3502 resvt = VT_R8;
3503 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3504 resvt = VT_I2;
3506 /* For overflow detection use the biggest compatible type for the
3507 multiplication */
3508 switch (resvt) {
3509 case VT_ERROR:
3510 hres = DISP_E_BADVARTYPE;
3511 goto end;
3512 case VT_NULL:
3513 hres = S_OK;
3514 V_VT(result) = VT_NULL;
3515 goto end;
3516 case VT_UI1:
3517 case VT_I2:
3518 case VT_I4:
3519 case VT_I8:
3520 tvt = VT_I8;
3521 break;
3522 case VT_R4:
3523 tvt = VT_R8;
3524 break;
3525 default:
3526 tvt = resvt;
3529 /* Now coerce the variants */
3530 hres = VariantChangeType(&lv, left, 0, tvt);
3531 if (FAILED(hres))
3532 goto end;
3533 hres = VariantChangeType(&rv, right, 0, tvt);
3534 if (FAILED(hres))
3535 goto end;
3537 /* Do the math */
3538 hres = S_OK;
3539 V_VT(&tv) = tvt;
3540 V_VT(result) = resvt;
3541 switch (tvt) {
3542 case VT_DECIMAL:
3543 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3544 &V_DECIMAL(result));
3545 goto end;
3546 case VT_CY:
3547 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3548 goto end;
3549 case VT_I8:
3550 /* Overflow detection */
3551 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3552 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3553 V_VT(result) = VT_R8;
3554 V_R8(result) = r8res;
3555 goto end;
3556 } else
3557 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3558 break;
3559 case VT_R8:
3560 /* FIXME: overflow detection */
3561 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3562 break;
3563 default:
3564 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3565 break;
3567 if (resvt != tvt) {
3568 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3569 /* Overflow! Change to the vartype with the next higher priority.
3570 With one exception: I4 ==> R8 even if it would fit in I8 */
3571 if (resvt == VT_I4)
3572 resvt = VT_R8;
3573 else
3574 resvt = prio2vt[coerce[resvt] + 1];
3576 } else
3577 hres = VariantCopy(result, &tv);
3579 end:
3580 if (hres != S_OK) {
3581 V_VT(result) = VT_EMPTY;
3582 V_I4(result) = 0; /* No V_EMPTY */
3584 VariantClear(&lv);
3585 VariantClear(&rv);
3586 VariantClear(&tv);
3587 VariantClear(&tempLeft);
3588 VariantClear(&tempRight);
3589 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3590 return hres;
3593 /**********************************************************************
3594 * VarDiv [OLEAUT32.143]
3596 * Divides one variant with another.
3598 * PARAMS
3599 * left [I] First variant
3600 * right [I] Second variant
3601 * result [O] Result variant
3603 * RETURNS
3604 * Success: S_OK.
3605 * Failure: An HRESULT error code indicating the error.
3607 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3609 HRESULT hres = S_OK;
3610 VARTYPE resvt = VT_EMPTY;
3611 VARTYPE leftvt,rightvt;
3612 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3613 VARIANT lv,rv;
3614 VARIANT tempLeft, tempRight;
3616 VariantInit(&tempLeft);
3617 VariantInit(&tempRight);
3618 VariantInit(&lv);
3619 VariantInit(&rv);
3621 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3622 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3624 /* Handle VT_DISPATCH by storing and taking address of returned value */
3625 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3627 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3628 if (FAILED(hres)) goto end;
3629 left = &tempLeft;
3631 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3633 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3634 if (FAILED(hres)) goto end;
3635 right = &tempRight;
3638 leftvt = V_VT(left)&VT_TYPEMASK;
3639 rightvt = V_VT(right)&VT_TYPEMASK;
3640 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3641 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3643 if (leftExtraFlags != rightExtraFlags)
3645 hres = DISP_E_BADVARTYPE;
3646 goto end;
3648 ExtraFlags = leftExtraFlags;
3650 /* Native VarDiv always returns an error when using extra flags */
3651 if (ExtraFlags != 0)
3653 hres = DISP_E_BADVARTYPE;
3654 goto end;
3657 /* Determine return type */
3658 if (!(rightvt == VT_EMPTY))
3660 if (leftvt == VT_NULL || rightvt == VT_NULL)
3662 V_VT(result) = VT_NULL;
3663 hres = S_OK;
3664 goto end;
3666 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3667 resvt = VT_DECIMAL;
3668 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3669 leftvt == VT_CY || rightvt == VT_CY ||
3670 leftvt == VT_DATE || rightvt == VT_DATE ||
3671 leftvt == VT_I4 || rightvt == VT_I4 ||
3672 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3673 leftvt == VT_I2 || rightvt == VT_I2 ||
3674 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3675 leftvt == VT_R8 || rightvt == VT_R8 ||
3676 leftvt == VT_UI1 || rightvt == VT_UI1)
3678 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3679 (leftvt == VT_R4 && rightvt == VT_UI1))
3680 resvt = VT_R4;
3681 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3682 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3683 (leftvt == VT_BOOL || leftvt == VT_I2)))
3684 resvt = VT_R4;
3685 else
3686 resvt = VT_R8;
3688 else if (leftvt == VT_R4 || rightvt == VT_R4)
3689 resvt = VT_R4;
3691 else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
3693 V_VT(result) = VT_NULL;
3694 hres = S_OK;
3695 goto end;
3697 else
3699 hres = DISP_E_BADVARTYPE;
3700 goto end;
3703 /* coerce to the result type */
3704 hres = VariantChangeType(&lv, left, 0, resvt);
3705 if (hres != S_OK) goto end;
3707 hres = VariantChangeType(&rv, right, 0, resvt);
3708 if (hres != S_OK) goto end;
3710 /* do the math */
3711 V_VT(result) = resvt;
3712 switch (resvt)
3714 case VT_R4:
3715 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3717 hres = DISP_E_OVERFLOW;
3718 V_VT(result) = VT_EMPTY;
3720 else if (V_R4(&rv) == 0.0)
3722 hres = DISP_E_DIVBYZERO;
3723 V_VT(result) = VT_EMPTY;
3725 else
3726 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3727 break;
3728 case VT_R8:
3729 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3731 hres = DISP_E_OVERFLOW;
3732 V_VT(result) = VT_EMPTY;
3734 else if (V_R8(&rv) == 0.0)
3736 hres = DISP_E_DIVBYZERO;
3737 V_VT(result) = VT_EMPTY;
3739 else
3740 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3741 break;
3742 case VT_DECIMAL:
3743 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3744 break;
3747 end:
3748 VariantClear(&lv);
3749 VariantClear(&rv);
3750 VariantClear(&tempLeft);
3751 VariantClear(&tempRight);
3752 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3753 return hres;
3756 /**********************************************************************
3757 * VarSub [OLEAUT32.159]
3759 * Subtract two variants.
3761 * PARAMS
3762 * left [I] First variant
3763 * right [I] Second variant
3764 * result [O] Result variant
3766 * RETURNS
3767 * Success: S_OK.
3768 * Failure: An HRESULT error code indicating the error.
3770 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3772 HRESULT hres = S_OK;
3773 VARTYPE resvt = VT_EMPTY;
3774 VARTYPE leftvt,rightvt;
3775 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3776 VARIANT lv,rv;
3777 VARIANT tempLeft, tempRight;
3779 VariantInit(&lv);
3780 VariantInit(&rv);
3781 VariantInit(&tempLeft);
3782 VariantInit(&tempRight);
3784 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3785 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3787 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3788 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3789 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3791 if (NULL == V_DISPATCH(left)) {
3792 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3793 hres = DISP_E_BADVARTYPE;
3794 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3795 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3796 hres = DISP_E_BADVARTYPE;
3797 else switch (V_VT(right) & VT_TYPEMASK)
3799 case VT_VARIANT:
3800 case VT_UNKNOWN:
3801 case 15:
3802 case VT_I1:
3803 case VT_UI2:
3804 case VT_UI4:
3805 hres = DISP_E_BADVARTYPE;
3807 if (FAILED(hres)) goto end;
3809 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3810 if (FAILED(hres)) goto end;
3811 left = &tempLeft;
3813 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3814 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3815 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3817 if (NULL == V_DISPATCH(right))
3819 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3820 hres = DISP_E_BADVARTYPE;
3821 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3822 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3823 hres = DISP_E_BADVARTYPE;
3824 else switch (V_VT(left) & VT_TYPEMASK)
3826 case VT_VARIANT:
3827 case VT_UNKNOWN:
3828 case 15:
3829 case VT_I1:
3830 case VT_UI2:
3831 case VT_UI4:
3832 hres = DISP_E_BADVARTYPE;
3834 if (FAILED(hres)) goto end;
3836 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3837 if (FAILED(hres)) goto end;
3838 right = &tempRight;
3841 leftvt = V_VT(left)&VT_TYPEMASK;
3842 rightvt = V_VT(right)&VT_TYPEMASK;
3843 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3844 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3846 if (leftExtraFlags != rightExtraFlags)
3848 hres = DISP_E_BADVARTYPE;
3849 goto end;
3851 ExtraFlags = leftExtraFlags;
3853 /* determine return type and return code */
3854 /* All extra flags produce errors */
3855 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3856 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3857 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3858 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3859 ExtraFlags == VT_VECTOR ||
3860 ExtraFlags == VT_BYREF ||
3861 ExtraFlags == VT_RESERVED)
3863 hres = DISP_E_BADVARTYPE;
3864 goto end;
3866 else if (ExtraFlags >= VT_ARRAY)
3868 hres = DISP_E_TYPEMISMATCH;
3869 goto end;
3871 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3872 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3873 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3874 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3875 leftvt == VT_I1 || rightvt == VT_I1 ||
3876 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3877 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3878 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3879 leftvt == VT_INT || rightvt == VT_INT ||
3880 leftvt == VT_UINT || rightvt == VT_UINT ||
3881 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3882 leftvt == VT_RECORD || rightvt == VT_RECORD)
3884 if (leftvt == VT_RECORD && rightvt == VT_I8)
3885 hres = DISP_E_TYPEMISMATCH;
3886 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3887 hres = DISP_E_TYPEMISMATCH;
3888 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3889 hres = DISP_E_TYPEMISMATCH;
3890 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3891 hres = DISP_E_TYPEMISMATCH;
3892 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3893 hres = DISP_E_BADVARTYPE;
3894 else
3895 hres = DISP_E_BADVARTYPE;
3896 goto end;
3898 /* The following flags/types are invalid for left variant */
3899 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3900 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3901 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3903 hres = DISP_E_BADVARTYPE;
3904 goto end;
3906 /* The following flags/types are invalid for right variant */
3907 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3908 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3909 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3911 hres = DISP_E_BADVARTYPE;
3912 goto end;
3914 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3915 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3916 resvt = VT_NULL;
3917 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3918 leftvt == VT_ERROR || rightvt == VT_ERROR)
3920 hres = DISP_E_TYPEMISMATCH;
3921 goto end;
3923 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3924 resvt = VT_NULL;
3925 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3926 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3927 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3928 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3929 resvt = VT_R8;
3930 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3931 resvt = VT_DECIMAL;
3932 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3933 resvt = VT_DATE;
3934 else if (leftvt == VT_CY || rightvt == VT_CY)
3935 resvt = VT_CY;
3936 else if (leftvt == VT_R8 || rightvt == VT_R8)
3937 resvt = VT_R8;
3938 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3939 resvt = VT_R8;
3940 else if (leftvt == VT_R4 || rightvt == VT_R4)
3942 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3943 leftvt == VT_I8 || rightvt == VT_I8)
3944 resvt = VT_R8;
3945 else
3946 resvt = VT_R4;
3948 else if (leftvt == VT_I8 || rightvt == VT_I8)
3949 resvt = VT_I8;
3950 else if (leftvt == VT_I4 || rightvt == VT_I4)
3951 resvt = VT_I4;
3952 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3953 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3954 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3955 resvt = VT_I2;
3956 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3957 resvt = VT_UI1;
3958 else
3960 hres = DISP_E_TYPEMISMATCH;
3961 goto end;
3964 /* coerce to the result type */
3965 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3966 hres = VariantChangeType(&lv, left, 0, VT_R8);
3967 else
3968 hres = VariantChangeType(&lv, left, 0, resvt);
3969 if (hres != S_OK) goto end;
3970 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3971 hres = VariantChangeType(&rv, right, 0, VT_R8);
3972 else
3973 hres = VariantChangeType(&rv, right, 0, resvt);
3974 if (hres != S_OK) goto end;
3976 /* do the math */
3977 V_VT(result) = resvt;
3978 switch (resvt)
3980 case VT_NULL:
3981 break;
3982 case VT_DATE:
3983 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3984 break;
3985 case VT_CY:
3986 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3987 break;
3988 case VT_R4:
3989 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3990 break;
3991 case VT_I8:
3992 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3993 break;
3994 case VT_I4:
3995 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3996 break;
3997 case VT_I2:
3998 V_I2(result) = V_I2(&lv) - V_I2(&rv);
3999 break;
4000 case VT_I1:
4001 V_I1(result) = V_I1(&lv) - V_I1(&rv);
4002 break;
4003 case VT_UI1:
4004 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
4005 break;
4006 case VT_R8:
4007 V_R8(result) = V_R8(&lv) - V_R8(&rv);
4008 break;
4009 case VT_DECIMAL:
4010 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
4011 break;
4014 end:
4015 VariantClear(&lv);
4016 VariantClear(&rv);
4017 VariantClear(&tempLeft);
4018 VariantClear(&tempRight);
4019 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
4020 return hres;
4024 /**********************************************************************
4025 * VarOr [OLEAUT32.157]
4027 * Perform a logical or (OR) operation on two variants.
4029 * PARAMS
4030 * pVarLeft [I] First variant
4031 * pVarRight [I] Variant to OR with pVarLeft
4032 * pVarOut [O] Destination for OR result
4034 * RETURNS
4035 * Success: S_OK. pVarOut contains the result of the operation with its type
4036 * taken from the table listed under VarXor().
4037 * Failure: An HRESULT error code indicating the error.
4039 * NOTES
4040 * See the Notes section of VarXor() for further information.
4042 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4044 VARTYPE vt = VT_I4;
4045 VARIANT varLeft, varRight, varStr;
4046 HRESULT hRet;
4047 VARIANT tempLeft, tempRight;
4049 VariantInit(&tempLeft);
4050 VariantInit(&tempRight);
4051 VariantInit(&varLeft);
4052 VariantInit(&varRight);
4053 VariantInit(&varStr);
4055 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4056 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4057 debugstr_VF(pVarRight), pVarOut);
4059 /* Handle VT_DISPATCH by storing and taking address of returned value */
4060 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4062 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4063 if (FAILED(hRet)) goto VarOr_Exit;
4064 pVarLeft = &tempLeft;
4066 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4068 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4069 if (FAILED(hRet)) goto VarOr_Exit;
4070 pVarRight = &tempRight;
4073 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4074 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4075 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
4076 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
4078 hRet = DISP_E_BADVARTYPE;
4079 goto VarOr_Exit;
4082 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
4084 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4086 /* NULL OR Zero is NULL, NULL OR value is value */
4087 if (V_VT(pVarLeft) == VT_NULL)
4088 pVarLeft = pVarRight; /* point to the non-NULL var */
4090 V_VT(pVarOut) = VT_NULL;
4091 V_I4(pVarOut) = 0;
4093 switch (V_VT(pVarLeft))
4095 case VT_DATE: case VT_R8:
4096 if (V_R8(pVarLeft))
4097 goto VarOr_AsEmpty;
4098 hRet = S_OK;
4099 goto VarOr_Exit;
4100 case VT_BOOL:
4101 if (V_BOOL(pVarLeft))
4102 *pVarOut = *pVarLeft;
4103 hRet = S_OK;
4104 goto VarOr_Exit;
4105 case VT_I2: case VT_UI2:
4106 if (V_I2(pVarLeft))
4107 goto VarOr_AsEmpty;
4108 hRet = S_OK;
4109 goto VarOr_Exit;
4110 case VT_I1:
4111 if (V_I1(pVarLeft))
4112 goto VarOr_AsEmpty;
4113 hRet = S_OK;
4114 goto VarOr_Exit;
4115 case VT_UI1:
4116 if (V_UI1(pVarLeft))
4117 *pVarOut = *pVarLeft;
4118 hRet = S_OK;
4119 goto VarOr_Exit;
4120 case VT_R4:
4121 if (V_R4(pVarLeft))
4122 goto VarOr_AsEmpty;
4123 hRet = S_OK;
4124 goto VarOr_Exit;
4125 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4126 if (V_I4(pVarLeft))
4127 goto VarOr_AsEmpty;
4128 hRet = S_OK;
4129 goto VarOr_Exit;
4130 case VT_CY:
4131 if (V_CY(pVarLeft).int64)
4132 goto VarOr_AsEmpty;
4133 hRet = S_OK;
4134 goto VarOr_Exit;
4135 case VT_I8: case VT_UI8:
4136 if (V_I8(pVarLeft))
4137 goto VarOr_AsEmpty;
4138 hRet = S_OK;
4139 goto VarOr_Exit;
4140 case VT_DECIMAL:
4141 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4142 goto VarOr_AsEmpty;
4143 hRet = S_OK;
4144 goto VarOr_Exit;
4145 case VT_BSTR:
4147 VARIANT_BOOL b;
4149 if (!V_BSTR(pVarLeft))
4151 hRet = DISP_E_BADVARTYPE;
4152 goto VarOr_Exit;
4155 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4156 if (SUCCEEDED(hRet) && b)
4158 V_VT(pVarOut) = VT_BOOL;
4159 V_BOOL(pVarOut) = b;
4161 goto VarOr_Exit;
4163 case VT_NULL: case VT_EMPTY:
4164 V_VT(pVarOut) = VT_NULL;
4165 hRet = S_OK;
4166 goto VarOr_Exit;
4167 default:
4168 hRet = DISP_E_BADVARTYPE;
4169 goto VarOr_Exit;
4173 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4175 if (V_VT(pVarLeft) == VT_EMPTY)
4176 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4178 VarOr_AsEmpty:
4179 /* Since one argument is empty (0), OR'ing it with the other simply
4180 * gives the others value (as 0|x => x). So just convert the other
4181 * argument to the required result type.
4183 switch (V_VT(pVarLeft))
4185 case VT_BSTR:
4186 if (!V_BSTR(pVarLeft))
4188 hRet = DISP_E_BADVARTYPE;
4189 goto VarOr_Exit;
4192 hRet = VariantCopy(&varStr, pVarLeft);
4193 if (FAILED(hRet))
4194 goto VarOr_Exit;
4195 pVarLeft = &varStr;
4196 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4197 if (FAILED(hRet))
4198 goto VarOr_Exit;
4199 /* Fall Through ... */
4200 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4201 V_VT(pVarOut) = VT_I2;
4202 break;
4203 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4204 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4205 case VT_INT: case VT_UINT: case VT_UI8:
4206 V_VT(pVarOut) = VT_I4;
4207 break;
4208 case VT_I8:
4209 V_VT(pVarOut) = VT_I8;
4210 break;
4211 default:
4212 hRet = DISP_E_BADVARTYPE;
4213 goto VarOr_Exit;
4215 hRet = VariantCopy(&varLeft, pVarLeft);
4216 if (FAILED(hRet))
4217 goto VarOr_Exit;
4218 pVarLeft = &varLeft;
4219 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4220 goto VarOr_Exit;
4223 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4225 V_VT(pVarOut) = VT_BOOL;
4226 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4227 hRet = S_OK;
4228 goto VarOr_Exit;
4231 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4233 V_VT(pVarOut) = VT_UI1;
4234 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4235 hRet = S_OK;
4236 goto VarOr_Exit;
4239 if (V_VT(pVarLeft) == VT_BSTR)
4241 hRet = VariantCopy(&varStr, pVarLeft);
4242 if (FAILED(hRet))
4243 goto VarOr_Exit;
4244 pVarLeft = &varStr;
4245 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4246 if (FAILED(hRet))
4247 goto VarOr_Exit;
4250 if (V_VT(pVarLeft) == VT_BOOL &&
4251 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4253 vt = VT_BOOL;
4255 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4256 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4257 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4258 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4260 vt = VT_I2;
4262 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4264 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4266 hRet = DISP_E_TYPEMISMATCH;
4267 goto VarOr_Exit;
4269 vt = VT_I8;
4272 hRet = VariantCopy(&varLeft, pVarLeft);
4273 if (FAILED(hRet))
4274 goto VarOr_Exit;
4276 hRet = VariantCopy(&varRight, pVarRight);
4277 if (FAILED(hRet))
4278 goto VarOr_Exit;
4280 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4281 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4282 else
4284 double d;
4286 if (V_VT(&varLeft) == VT_BSTR &&
4287 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4288 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4289 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4290 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4291 if (FAILED(hRet))
4292 goto VarOr_Exit;
4295 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4296 V_VT(&varRight) = VT_I4; /* Don't overflow */
4297 else
4299 double d;
4301 if (V_VT(&varRight) == VT_BSTR &&
4302 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4303 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4304 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4305 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4306 if (FAILED(hRet))
4307 goto VarOr_Exit;
4310 V_VT(pVarOut) = vt;
4311 if (vt == VT_I8)
4313 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4315 else if (vt == VT_I4)
4317 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4319 else
4321 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4324 VarOr_Exit:
4325 VariantClear(&varStr);
4326 VariantClear(&varLeft);
4327 VariantClear(&varRight);
4328 VariantClear(&tempLeft);
4329 VariantClear(&tempRight);
4330 return hRet;
4333 /**********************************************************************
4334 * VarAbs [OLEAUT32.168]
4336 * Convert a variant to its absolute value.
4338 * PARAMS
4339 * pVarIn [I] Source variant
4340 * pVarOut [O] Destination for converted value
4342 * RETURNS
4343 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4344 * Failure: An HRESULT error code indicating the error.
4346 * NOTES
4347 * - This function does not process by-reference variants.
4348 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4349 * according to the following table:
4350 *| Input Type Output Type
4351 *| ---------- -----------
4352 *| VT_BOOL VT_I2
4353 *| VT_BSTR VT_R8
4354 *| (All others) Unchanged
4356 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4358 VARIANT varIn;
4359 HRESULT hRet = S_OK;
4360 VARIANT temp;
4362 VariantInit(&temp);
4364 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4365 debugstr_VF(pVarIn), pVarOut);
4367 /* Handle VT_DISPATCH by storing and taking address of returned value */
4368 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4370 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4371 if (FAILED(hRet)) goto VarAbs_Exit;
4372 pVarIn = &temp;
4375 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4376 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4377 V_VT(pVarIn) == VT_ERROR)
4379 hRet = DISP_E_TYPEMISMATCH;
4380 goto VarAbs_Exit;
4382 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4384 #define ABS_CASE(typ,min) \
4385 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4386 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4387 break
4389 switch (V_VT(pVarIn))
4391 ABS_CASE(I1,I1_MIN);
4392 case VT_BOOL:
4393 V_VT(pVarOut) = VT_I2;
4394 /* BOOL->I2, Fall through ... */
4395 ABS_CASE(I2,I2_MIN);
4396 case VT_INT:
4397 ABS_CASE(I4,I4_MIN);
4398 ABS_CASE(I8,I8_MIN);
4399 ABS_CASE(R4,R4_MIN);
4400 case VT_BSTR:
4401 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4402 if (FAILED(hRet))
4403 break;
4404 V_VT(pVarOut) = VT_R8;
4405 pVarIn = &varIn;
4406 /* Fall through ... */
4407 case VT_DATE:
4408 ABS_CASE(R8,R8_MIN);
4409 case VT_CY:
4410 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4411 break;
4412 case VT_DECIMAL:
4413 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4414 break;
4415 case VT_UI1:
4416 case VT_UI2:
4417 case VT_UINT:
4418 case VT_UI4:
4419 case VT_UI8:
4420 /* No-Op */
4421 break;
4422 case VT_EMPTY:
4423 V_VT(pVarOut) = VT_I2;
4424 case VT_NULL:
4425 V_I2(pVarOut) = 0;
4426 break;
4427 default:
4428 hRet = DISP_E_BADVARTYPE;
4431 VarAbs_Exit:
4432 VariantClear(&temp);
4433 return hRet;
4436 /**********************************************************************
4437 * VarFix [OLEAUT32.169]
4439 * Truncate a variants value to a whole number.
4441 * PARAMS
4442 * pVarIn [I] Source variant
4443 * pVarOut [O] Destination for converted value
4445 * RETURNS
4446 * Success: S_OK. pVarOut contains the converted value.
4447 * Failure: An HRESULT error code indicating the error.
4449 * NOTES
4450 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4451 * according to the following table:
4452 *| Input Type Output Type
4453 *| ---------- -----------
4454 *| VT_BOOL VT_I2
4455 *| VT_EMPTY VT_I2
4456 *| VT_BSTR VT_R8
4457 *| All Others Unchanged
4458 * - The difference between this function and VarInt() is that VarInt() rounds
4459 * negative numbers away from 0, while this function rounds them towards zero.
4461 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4463 HRESULT hRet = S_OK;
4464 VARIANT temp;
4466 VariantInit(&temp);
4468 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4469 debugstr_VF(pVarIn), pVarOut);
4471 /* Handle VT_DISPATCH by storing and taking address of returned value */
4472 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4474 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4475 if (FAILED(hRet)) goto VarFix_Exit;
4476 pVarIn = &temp;
4478 V_VT(pVarOut) = V_VT(pVarIn);
4480 switch (V_VT(pVarIn))
4482 case VT_UI1:
4483 V_UI1(pVarOut) = V_UI1(pVarIn);
4484 break;
4485 case VT_BOOL:
4486 V_VT(pVarOut) = VT_I2;
4487 /* Fall through */
4488 case VT_I2:
4489 V_I2(pVarOut) = V_I2(pVarIn);
4490 break;
4491 case VT_I4:
4492 V_I4(pVarOut) = V_I4(pVarIn);
4493 break;
4494 case VT_I8:
4495 V_I8(pVarOut) = V_I8(pVarIn);
4496 break;
4497 case VT_R4:
4498 if (V_R4(pVarIn) < 0.0f)
4499 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4500 else
4501 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4502 break;
4503 case VT_BSTR:
4504 V_VT(pVarOut) = VT_R8;
4505 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4506 pVarIn = pVarOut;
4507 /* Fall through */
4508 case VT_DATE:
4509 case VT_R8:
4510 if (V_R8(pVarIn) < 0.0)
4511 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4512 else
4513 V_R8(pVarOut) = floor(V_R8(pVarIn));
4514 break;
4515 case VT_CY:
4516 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4517 break;
4518 case VT_DECIMAL:
4519 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4520 break;
4521 case VT_EMPTY:
4522 V_VT(pVarOut) = VT_I2;
4523 V_I2(pVarOut) = 0;
4524 break;
4525 case VT_NULL:
4526 /* No-Op */
4527 break;
4528 default:
4529 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4530 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4531 hRet = DISP_E_BADVARTYPE;
4532 else
4533 hRet = DISP_E_TYPEMISMATCH;
4535 VarFix_Exit:
4536 if (FAILED(hRet))
4537 V_VT(pVarOut) = VT_EMPTY;
4538 VariantClear(&temp);
4540 return hRet;
4543 /**********************************************************************
4544 * VarInt [OLEAUT32.172]
4546 * Truncate a variants value to a whole number.
4548 * PARAMS
4549 * pVarIn [I] Source variant
4550 * pVarOut [O] Destination for converted value
4552 * RETURNS
4553 * Success: S_OK. pVarOut contains the converted value.
4554 * Failure: An HRESULT error code indicating the error.
4556 * NOTES
4557 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4558 * according to the following table:
4559 *| Input Type Output Type
4560 *| ---------- -----------
4561 *| VT_BOOL VT_I2
4562 *| VT_EMPTY VT_I2
4563 *| VT_BSTR VT_R8
4564 *| All Others Unchanged
4565 * - The difference between this function and VarFix() is that VarFix() rounds
4566 * negative numbers towards 0, while this function rounds them away from zero.
4568 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4570 HRESULT hRet = S_OK;
4571 VARIANT temp;
4573 VariantInit(&temp);
4575 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4576 debugstr_VF(pVarIn), pVarOut);
4578 /* Handle VT_DISPATCH by storing and taking address of returned value */
4579 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4581 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4582 if (FAILED(hRet)) goto VarInt_Exit;
4583 pVarIn = &temp;
4585 V_VT(pVarOut) = V_VT(pVarIn);
4587 switch (V_VT(pVarIn))
4589 case VT_R4:
4590 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4591 break;
4592 case VT_BSTR:
4593 V_VT(pVarOut) = VT_R8;
4594 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4595 pVarIn = pVarOut;
4596 /* Fall through */
4597 case VT_DATE:
4598 case VT_R8:
4599 V_R8(pVarOut) = floor(V_R8(pVarIn));
4600 break;
4601 case VT_CY:
4602 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4603 break;
4604 case VT_DECIMAL:
4605 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4606 break;
4607 default:
4608 hRet = VarFix(pVarIn, pVarOut);
4610 VarInt_Exit:
4611 VariantClear(&temp);
4613 return hRet;
4616 /**********************************************************************
4617 * VarXor [OLEAUT32.167]
4619 * Perform a logical exclusive-or (XOR) operation on two variants.
4621 * PARAMS
4622 * pVarLeft [I] First variant
4623 * pVarRight [I] Variant to XOR with pVarLeft
4624 * pVarOut [O] Destination for XOR result
4626 * RETURNS
4627 * Success: S_OK. pVarOut contains the result of the operation with its type
4628 * taken from the table below).
4629 * Failure: An HRESULT error code indicating the error.
4631 * NOTES
4632 * - Neither pVarLeft or pVarRight are modified by this function.
4633 * - This function does not process by-reference variants.
4634 * - Input types of VT_BSTR may be numeric strings or boolean text.
4635 * - The type of result stored in pVarOut depends on the types of pVarLeft
4636 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4637 * or VT_NULL if the function succeeds.
4638 * - Type promotion is inconsistent and as a result certain combinations of
4639 * values will return DISP_E_OVERFLOW even when they could be represented.
4640 * This matches the behaviour of native oleaut32.
4642 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4644 VARTYPE vt;
4645 VARIANT varLeft, varRight;
4646 VARIANT tempLeft, tempRight;
4647 double d;
4648 HRESULT hRet;
4650 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4651 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4652 debugstr_VF(pVarRight), pVarOut);
4654 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4655 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4656 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4657 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4658 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4659 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4660 return DISP_E_BADVARTYPE;
4662 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4664 /* NULL XOR anything valid is NULL */
4665 V_VT(pVarOut) = VT_NULL;
4666 return S_OK;
4669 VariantInit(&tempLeft);
4670 VariantInit(&tempRight);
4672 /* Handle VT_DISPATCH by storing and taking address of returned value */
4673 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4675 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4676 if (FAILED(hRet)) goto VarXor_Exit;
4677 pVarLeft = &tempLeft;
4679 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4681 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4682 if (FAILED(hRet)) goto VarXor_Exit;
4683 pVarRight = &tempRight;
4686 /* Copy our inputs so we don't disturb anything */
4687 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4689 hRet = VariantCopy(&varLeft, pVarLeft);
4690 if (FAILED(hRet))
4691 goto VarXor_Exit;
4693 hRet = VariantCopy(&varRight, pVarRight);
4694 if (FAILED(hRet))
4695 goto VarXor_Exit;
4697 /* Try any strings first as numbers, then as VT_BOOL */
4698 if (V_VT(&varLeft) == VT_BSTR)
4700 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4701 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4702 FAILED(hRet) ? VT_BOOL : VT_I4);
4703 if (FAILED(hRet))
4704 goto VarXor_Exit;
4707 if (V_VT(&varRight) == VT_BSTR)
4709 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4710 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4711 FAILED(hRet) ? VT_BOOL : VT_I4);
4712 if (FAILED(hRet))
4713 goto VarXor_Exit;
4716 /* Determine the result type */
4717 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4719 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4721 hRet = DISP_E_TYPEMISMATCH;
4722 goto VarXor_Exit;
4724 vt = VT_I8;
4726 else
4728 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4730 case (VT_BOOL << 16) | VT_BOOL:
4731 vt = VT_BOOL;
4732 break;
4733 case (VT_UI1 << 16) | VT_UI1:
4734 vt = VT_UI1;
4735 break;
4736 case (VT_EMPTY << 16) | VT_EMPTY:
4737 case (VT_EMPTY << 16) | VT_UI1:
4738 case (VT_EMPTY << 16) | VT_I2:
4739 case (VT_EMPTY << 16) | VT_BOOL:
4740 case (VT_UI1 << 16) | VT_EMPTY:
4741 case (VT_UI1 << 16) | VT_I2:
4742 case (VT_UI1 << 16) | VT_BOOL:
4743 case (VT_I2 << 16) | VT_EMPTY:
4744 case (VT_I2 << 16) | VT_UI1:
4745 case (VT_I2 << 16) | VT_I2:
4746 case (VT_I2 << 16) | VT_BOOL:
4747 case (VT_BOOL << 16) | VT_EMPTY:
4748 case (VT_BOOL << 16) | VT_UI1:
4749 case (VT_BOOL << 16) | VT_I2:
4750 vt = VT_I2;
4751 break;
4752 default:
4753 vt = VT_I4;
4754 break;
4758 /* VT_UI4 does not overflow */
4759 if (vt != VT_I8)
4761 if (V_VT(&varLeft) == VT_UI4)
4762 V_VT(&varLeft) = VT_I4;
4763 if (V_VT(&varRight) == VT_UI4)
4764 V_VT(&varRight) = VT_I4;
4767 /* Convert our input copies to the result type */
4768 if (V_VT(&varLeft) != vt)
4769 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4770 if (FAILED(hRet))
4771 goto VarXor_Exit;
4773 if (V_VT(&varRight) != vt)
4774 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4775 if (FAILED(hRet))
4776 goto VarXor_Exit;
4778 V_VT(pVarOut) = vt;
4780 /* Calculate the result */
4781 switch (vt)
4783 case VT_I8:
4784 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4785 break;
4786 case VT_I4:
4787 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4788 break;
4789 case VT_BOOL:
4790 case VT_I2:
4791 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4792 break;
4793 case VT_UI1:
4794 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4795 break;
4798 VarXor_Exit:
4799 VariantClear(&varLeft);
4800 VariantClear(&varRight);
4801 VariantClear(&tempLeft);
4802 VariantClear(&tempRight);
4803 return hRet;
4806 /**********************************************************************
4807 * VarEqv [OLEAUT32.172]
4809 * Determine if two variants contain the same value.
4811 * PARAMS
4812 * pVarLeft [I] First variant to compare
4813 * pVarRight [I] Variant to compare to pVarLeft
4814 * pVarOut [O] Destination for comparison result
4816 * RETURNS
4817 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4818 * if equivalent or non-zero otherwise.
4819 * Failure: An HRESULT error code indicating the error.
4821 * NOTES
4822 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4823 * the result.
4825 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4827 HRESULT hRet;
4829 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4830 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4831 debugstr_VF(pVarRight), pVarOut);
4833 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4834 if (SUCCEEDED(hRet))
4836 if (V_VT(pVarOut) == VT_I8)
4837 V_I8(pVarOut) = ~V_I8(pVarOut);
4838 else
4839 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4841 return hRet;
4844 /**********************************************************************
4845 * VarNeg [OLEAUT32.173]
4847 * Negate the value of a variant.
4849 * PARAMS
4850 * pVarIn [I] Source variant
4851 * pVarOut [O] Destination for converted value
4853 * RETURNS
4854 * Success: S_OK. pVarOut contains the converted value.
4855 * Failure: An HRESULT error code indicating the error.
4857 * NOTES
4858 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4859 * according to the following table:
4860 *| Input Type Output Type
4861 *| ---------- -----------
4862 *| VT_EMPTY VT_I2
4863 *| VT_UI1 VT_I2
4864 *| VT_BOOL VT_I2
4865 *| VT_BSTR VT_R8
4866 *| All Others Unchanged (unless promoted)
4867 * - Where the negated value of a variant does not fit in its base type, the type
4868 * is promoted according to the following table:
4869 *| Input Type Promoted To
4870 *| ---------- -----------
4871 *| VT_I2 VT_I4
4872 *| VT_I4 VT_R8
4873 *| VT_I8 VT_R8
4874 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4875 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4876 * for types which are not valid. Since this is in contravention of the
4877 * meaning of those error codes and unlikely to be relied on by applications,
4878 * this implementation returns errors consistent with the other high level
4879 * variant math functions.
4881 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4883 HRESULT hRet = S_OK;
4884 VARIANT temp;
4886 VariantInit(&temp);
4888 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4889 debugstr_VF(pVarIn), pVarOut);
4891 /* Handle VT_DISPATCH by storing and taking address of returned value */
4892 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4894 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4895 if (FAILED(hRet)) goto VarNeg_Exit;
4896 pVarIn = &temp;
4898 V_VT(pVarOut) = V_VT(pVarIn);
4900 switch (V_VT(pVarIn))
4902 case VT_UI1:
4903 V_VT(pVarOut) = VT_I2;
4904 V_I2(pVarOut) = -V_UI1(pVarIn);
4905 break;
4906 case VT_BOOL:
4907 V_VT(pVarOut) = VT_I2;
4908 /* Fall through */
4909 case VT_I2:
4910 if (V_I2(pVarIn) == I2_MIN)
4912 V_VT(pVarOut) = VT_I4;
4913 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4915 else
4916 V_I2(pVarOut) = -V_I2(pVarIn);
4917 break;
4918 case VT_I4:
4919 if (V_I4(pVarIn) == I4_MIN)
4921 V_VT(pVarOut) = VT_R8;
4922 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4924 else
4925 V_I4(pVarOut) = -V_I4(pVarIn);
4926 break;
4927 case VT_I8:
4928 if (V_I8(pVarIn) == I8_MIN)
4930 V_VT(pVarOut) = VT_R8;
4931 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4932 V_R8(pVarOut) *= -1.0;
4934 else
4935 V_I8(pVarOut) = -V_I8(pVarIn);
4936 break;
4937 case VT_R4:
4938 V_R4(pVarOut) = -V_R4(pVarIn);
4939 break;
4940 case VT_DATE:
4941 case VT_R8:
4942 V_R8(pVarOut) = -V_R8(pVarIn);
4943 break;
4944 case VT_CY:
4945 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4946 break;
4947 case VT_DECIMAL:
4948 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4949 break;
4950 case VT_BSTR:
4951 V_VT(pVarOut) = VT_R8;
4952 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4953 V_R8(pVarOut) = -V_R8(pVarOut);
4954 break;
4955 case VT_EMPTY:
4956 V_VT(pVarOut) = VT_I2;
4957 V_I2(pVarOut) = 0;
4958 break;
4959 case VT_NULL:
4960 /* No-Op */
4961 break;
4962 default:
4963 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4964 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4965 hRet = DISP_E_BADVARTYPE;
4966 else
4967 hRet = DISP_E_TYPEMISMATCH;
4969 VarNeg_Exit:
4970 if (FAILED(hRet))
4971 V_VT(pVarOut) = VT_EMPTY;
4972 VariantClear(&temp);
4974 return hRet;
4977 /**********************************************************************
4978 * VarNot [OLEAUT32.174]
4980 * Perform a not operation on a variant.
4982 * PARAMS
4983 * pVarIn [I] Source variant
4984 * pVarOut [O] Destination for converted value
4986 * RETURNS
4987 * Success: S_OK. pVarOut contains the converted value.
4988 * Failure: An HRESULT error code indicating the error.
4990 * NOTES
4991 * - Strictly speaking, this function performs a bitwise ones complement
4992 * on the variants value (after possibly converting to VT_I4, see below).
4993 * This only behaves like a boolean not operation if the value in
4994 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4995 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4996 * before calling this function.
4997 * - This function does not process by-reference variants.
4998 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4999 * according to the following table:
5000 *| Input Type Output Type
5001 *| ---------- -----------
5002 *| VT_EMPTY VT_I2
5003 *| VT_R4 VT_I4
5004 *| VT_R8 VT_I4
5005 *| VT_BSTR VT_I4
5006 *| VT_DECIMAL VT_I4
5007 *| VT_CY VT_I4
5008 *| (All others) Unchanged
5010 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
5012 VARIANT varIn;
5013 HRESULT hRet = S_OK;
5014 VARIANT temp;
5016 VariantInit(&temp);
5018 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
5019 debugstr_VF(pVarIn), pVarOut);
5021 /* Handle VT_DISPATCH by storing and taking address of returned value */
5022 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5024 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5025 if (FAILED(hRet)) goto VarNot_Exit;
5026 pVarIn = &temp;
5029 if (V_VT(pVarIn) == VT_BSTR)
5031 V_VT(&varIn) = VT_R8;
5032 hRet = VarR8FromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn) );
5033 if (FAILED(hRet))
5035 V_VT(&varIn) = VT_BOOL;
5036 hRet = VarBoolFromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &V_BOOL(&varIn) );
5038 if (FAILED(hRet)) goto VarNot_Exit;
5039 pVarIn = &varIn;
5042 V_VT(pVarOut) = V_VT(pVarIn);
5044 switch (V_VT(pVarIn))
5046 case VT_I1:
5047 V_I4(pVarOut) = ~V_I1(pVarIn);
5048 V_VT(pVarOut) = VT_I4;
5049 break;
5050 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
5051 case VT_BOOL:
5052 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
5053 case VT_UI2:
5054 V_I4(pVarOut) = ~V_UI2(pVarIn);
5055 V_VT(pVarOut) = VT_I4;
5056 break;
5057 case VT_DECIMAL:
5058 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
5059 if (FAILED(hRet))
5060 break;
5061 pVarIn = &varIn;
5062 /* Fall through ... */
5063 case VT_INT:
5064 V_VT(pVarOut) = VT_I4;
5065 /* Fall through ... */
5066 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
5067 case VT_UINT:
5068 case VT_UI4:
5069 V_I4(pVarOut) = ~V_UI4(pVarIn);
5070 V_VT(pVarOut) = VT_I4;
5071 break;
5072 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
5073 case VT_UI8:
5074 V_I4(pVarOut) = ~V_UI8(pVarIn);
5075 V_VT(pVarOut) = VT_I4;
5076 break;
5077 case VT_R4:
5078 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
5079 V_I4(pVarOut) = ~V_I4(pVarOut);
5080 V_VT(pVarOut) = VT_I4;
5081 break;
5082 case VT_DATE:
5083 case VT_R8:
5084 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
5085 V_I4(pVarOut) = ~V_I4(pVarOut);
5086 V_VT(pVarOut) = VT_I4;
5087 break;
5088 case VT_CY:
5089 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
5090 V_I4(pVarOut) = ~V_I4(pVarOut);
5091 V_VT(pVarOut) = VT_I4;
5092 break;
5093 case VT_EMPTY:
5094 V_I2(pVarOut) = ~0;
5095 V_VT(pVarOut) = VT_I2;
5096 break;
5097 case VT_NULL:
5098 /* No-Op */
5099 break;
5100 default:
5101 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5102 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5103 hRet = DISP_E_BADVARTYPE;
5104 else
5105 hRet = DISP_E_TYPEMISMATCH;
5107 VarNot_Exit:
5108 if (FAILED(hRet))
5109 V_VT(pVarOut) = VT_EMPTY;
5110 VariantClear(&temp);
5112 return hRet;
5115 /**********************************************************************
5116 * VarRound [OLEAUT32.175]
5118 * Perform a round operation on a variant.
5120 * PARAMS
5121 * pVarIn [I] Source variant
5122 * deci [I] Number of decimals to round to
5123 * pVarOut [O] Destination for converted value
5125 * RETURNS
5126 * Success: S_OK. pVarOut contains the converted value.
5127 * Failure: An HRESULT error code indicating the error.
5129 * NOTES
5130 * - Floating point values are rounded to the desired number of decimals.
5131 * - Some integer types are just copied to the return variable.
5132 * - Some other integer types are not handled and fail.
5134 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5136 VARIANT varIn;
5137 HRESULT hRet = S_OK;
5138 float factor;
5139 VARIANT temp;
5141 VariantInit(&temp);
5143 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
5145 /* Handle VT_DISPATCH by storing and taking address of returned value */
5146 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5148 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5149 if (FAILED(hRet)) goto VarRound_Exit;
5150 pVarIn = &temp;
5153 switch (V_VT(pVarIn))
5155 /* cases that fail on windows */
5156 case VT_I1:
5157 case VT_I8:
5158 case VT_UI2:
5159 case VT_UI4:
5160 hRet = DISP_E_BADVARTYPE;
5161 break;
5163 /* cases just copying in to out */
5164 case VT_UI1:
5165 V_VT(pVarOut) = V_VT(pVarIn);
5166 V_UI1(pVarOut) = V_UI1(pVarIn);
5167 break;
5168 case VT_I2:
5169 V_VT(pVarOut) = V_VT(pVarIn);
5170 V_I2(pVarOut) = V_I2(pVarIn);
5171 break;
5172 case VT_I4:
5173 V_VT(pVarOut) = V_VT(pVarIn);
5174 V_I4(pVarOut) = V_I4(pVarIn);
5175 break;
5176 case VT_NULL:
5177 V_VT(pVarOut) = V_VT(pVarIn);
5178 /* value unchanged */
5179 break;
5181 /* cases that change type */
5182 case VT_EMPTY:
5183 V_VT(pVarOut) = VT_I2;
5184 V_I2(pVarOut) = 0;
5185 break;
5186 case VT_BOOL:
5187 V_VT(pVarOut) = VT_I2;
5188 V_I2(pVarOut) = V_BOOL(pVarIn);
5189 break;
5190 case VT_BSTR:
5191 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5192 if (FAILED(hRet))
5193 break;
5194 V_VT(&varIn)=VT_R8;
5195 pVarIn = &varIn;
5196 /* Fall through ... */
5198 /* cases we need to do math */
5199 case VT_R8:
5200 if (V_R8(pVarIn)>0) {
5201 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5202 } else {
5203 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5205 V_VT(pVarOut) = V_VT(pVarIn);
5206 break;
5207 case VT_R4:
5208 if (V_R4(pVarIn)>0) {
5209 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5210 } else {
5211 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5213 V_VT(pVarOut) = V_VT(pVarIn);
5214 break;
5215 case VT_DATE:
5216 if (V_DATE(pVarIn)>0) {
5217 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5218 } else {
5219 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5221 V_VT(pVarOut) = V_VT(pVarIn);
5222 break;
5223 case VT_CY:
5224 if (deci>3)
5225 factor=1;
5226 else
5227 factor=pow(10, 4-deci);
5229 if (V_CY(pVarIn).int64>0) {
5230 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5231 } else {
5232 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5234 V_VT(pVarOut) = V_VT(pVarIn);
5235 break;
5237 /* cases we don't know yet */
5238 default:
5239 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5240 V_VT(pVarIn) & VT_TYPEMASK, deci);
5241 hRet = DISP_E_BADVARTYPE;
5243 VarRound_Exit:
5244 if (FAILED(hRet))
5245 V_VT(pVarOut) = VT_EMPTY;
5246 VariantClear(&temp);
5248 TRACE("returning 0x%08x (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
5249 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
5250 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
5252 return hRet;
5255 /**********************************************************************
5256 * VarIdiv [OLEAUT32.153]
5258 * Converts input variants to integers and divides them.
5260 * PARAMS
5261 * left [I] Left hand variant
5262 * right [I] Right hand variant
5263 * result [O] Destination for quotient
5265 * RETURNS
5266 * Success: S_OK. result contains the quotient.
5267 * Failure: An HRESULT error code indicating the error.
5269 * NOTES
5270 * If either expression is null, null is returned, as per MSDN
5272 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5274 HRESULT hres = S_OK;
5275 VARTYPE resvt = VT_EMPTY;
5276 VARTYPE leftvt,rightvt;
5277 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5278 VARIANT lv,rv;
5279 VARIANT tempLeft, tempRight;
5281 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5282 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5284 VariantInit(&lv);
5285 VariantInit(&rv);
5286 VariantInit(&tempLeft);
5287 VariantInit(&tempRight);
5289 leftvt = V_VT(left)&VT_TYPEMASK;
5290 rightvt = V_VT(right)&VT_TYPEMASK;
5291 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5292 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5294 if (leftExtraFlags != rightExtraFlags)
5296 hres = DISP_E_BADVARTYPE;
5297 goto end;
5299 ExtraFlags = leftExtraFlags;
5301 /* Native VarIdiv always returns an error when using extra
5302 * flags or if the variant combination is I8 and INT.
5304 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5305 (leftvt == VT_INT && rightvt == VT_I8) ||
5306 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5307 ExtraFlags != 0)
5309 hres = DISP_E_BADVARTYPE;
5310 goto end;
5313 /* Determine variant type */
5314 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5316 V_VT(result) = VT_NULL;
5317 hres = S_OK;
5318 goto end;
5320 else if (leftvt == VT_I8 || rightvt == VT_I8)
5321 resvt = VT_I8;
5322 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5323 leftvt == VT_INT || rightvt == VT_INT ||
5324 leftvt == VT_UINT || rightvt == VT_UINT ||
5325 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5326 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5327 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5328 leftvt == VT_I1 || rightvt == VT_I1 ||
5329 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5330 leftvt == VT_DATE || rightvt == VT_DATE ||
5331 leftvt == VT_CY || rightvt == VT_CY ||
5332 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5333 leftvt == VT_R8 || rightvt == VT_R8 ||
5334 leftvt == VT_R4 || rightvt == VT_R4)
5335 resvt = VT_I4;
5336 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5337 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5338 leftvt == VT_EMPTY)
5339 resvt = VT_I2;
5340 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5341 resvt = VT_UI1;
5342 else
5344 hres = DISP_E_BADVARTYPE;
5345 goto end;
5348 /* coerce to the result type */
5349 hres = VariantChangeType(&lv, left, 0, resvt);
5350 if (hres != S_OK) goto end;
5351 hres = VariantChangeType(&rv, right, 0, resvt);
5352 if (hres != S_OK) goto end;
5354 /* do the math */
5355 V_VT(result) = resvt;
5356 switch (resvt)
5358 case VT_UI1:
5359 if (V_UI1(&rv) == 0)
5361 hres = DISP_E_DIVBYZERO;
5362 V_VT(result) = VT_EMPTY;
5364 else
5365 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5366 break;
5367 case VT_I2:
5368 if (V_I2(&rv) == 0)
5370 hres = DISP_E_DIVBYZERO;
5371 V_VT(result) = VT_EMPTY;
5373 else
5374 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5375 break;
5376 case VT_I4:
5377 if (V_I4(&rv) == 0)
5379 hres = DISP_E_DIVBYZERO;
5380 V_VT(result) = VT_EMPTY;
5382 else
5383 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5384 break;
5385 case VT_I8:
5386 if (V_I8(&rv) == 0)
5388 hres = DISP_E_DIVBYZERO;
5389 V_VT(result) = VT_EMPTY;
5391 else
5392 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5393 break;
5394 default:
5395 FIXME("Couldn't integer divide variant types %d,%d\n",
5396 leftvt,rightvt);
5399 end:
5400 VariantClear(&lv);
5401 VariantClear(&rv);
5402 VariantClear(&tempLeft);
5403 VariantClear(&tempRight);
5405 return hres;
5409 /**********************************************************************
5410 * VarMod [OLEAUT32.155]
5412 * Perform the modulus operation of the right hand variant on the left
5414 * PARAMS
5415 * left [I] Left hand variant
5416 * right [I] Right hand variant
5417 * result [O] Destination for converted value
5419 * RETURNS
5420 * Success: S_OK. result contains the remainder.
5421 * Failure: An HRESULT error code indicating the error.
5423 * NOTE:
5424 * If an error occurs the type of result will be modified but the value will not be.
5425 * Doesn't support arrays or any special flags yet.
5427 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5429 BOOL lOk = TRUE;
5430 HRESULT rc = E_FAIL;
5431 int resT = 0;
5432 VARIANT lv,rv;
5433 VARIANT tempLeft, tempRight;
5435 VariantInit(&tempLeft);
5436 VariantInit(&tempRight);
5437 VariantInit(&lv);
5438 VariantInit(&rv);
5440 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5441 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5443 /* Handle VT_DISPATCH by storing and taking address of returned value */
5444 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5446 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5447 if (FAILED(rc)) goto end;
5448 left = &tempLeft;
5450 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5452 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5453 if (FAILED(rc)) goto end;
5454 right = &tempRight;
5457 /* check for invalid inputs */
5458 lOk = TRUE;
5459 switch (V_VT(left) & VT_TYPEMASK) {
5460 case VT_BOOL :
5461 case VT_I1 :
5462 case VT_I2 :
5463 case VT_I4 :
5464 case VT_I8 :
5465 case VT_INT :
5466 case VT_UI1 :
5467 case VT_UI2 :
5468 case VT_UI4 :
5469 case VT_UI8 :
5470 case VT_UINT :
5471 case VT_R4 :
5472 case VT_R8 :
5473 case VT_CY :
5474 case VT_EMPTY:
5475 case VT_DATE :
5476 case VT_BSTR :
5477 case VT_DECIMAL:
5478 break;
5479 case VT_VARIANT:
5480 case VT_UNKNOWN:
5481 V_VT(result) = VT_EMPTY;
5482 rc = DISP_E_TYPEMISMATCH;
5483 goto end;
5484 case VT_ERROR:
5485 rc = DISP_E_TYPEMISMATCH;
5486 goto end;
5487 case VT_RECORD:
5488 V_VT(result) = VT_EMPTY;
5489 rc = DISP_E_TYPEMISMATCH;
5490 goto end;
5491 case VT_NULL:
5492 break;
5493 default:
5494 V_VT(result) = VT_EMPTY;
5495 rc = DISP_E_BADVARTYPE;
5496 goto end;
5500 switch (V_VT(right) & VT_TYPEMASK) {
5501 case VT_BOOL :
5502 case VT_I1 :
5503 case VT_I2 :
5504 case VT_I4 :
5505 case VT_I8 :
5506 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5508 V_VT(result) = VT_EMPTY;
5509 rc = DISP_E_TYPEMISMATCH;
5510 goto end;
5512 case VT_INT :
5513 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5515 V_VT(result) = VT_EMPTY;
5516 rc = DISP_E_TYPEMISMATCH;
5517 goto end;
5519 case VT_UI1 :
5520 case VT_UI2 :
5521 case VT_UI4 :
5522 case VT_UI8 :
5523 case VT_UINT :
5524 case VT_R4 :
5525 case VT_R8 :
5526 case VT_CY :
5527 if(V_VT(left) == VT_EMPTY)
5529 V_VT(result) = VT_I4;
5530 rc = S_OK;
5531 goto end;
5533 case VT_EMPTY:
5534 case VT_DATE :
5535 case VT_DECIMAL:
5536 if(V_VT(left) == VT_ERROR)
5538 V_VT(result) = VT_EMPTY;
5539 rc = DISP_E_TYPEMISMATCH;
5540 goto end;
5542 case VT_BSTR:
5543 if(V_VT(left) == VT_NULL)
5545 V_VT(result) = VT_NULL;
5546 rc = S_OK;
5547 goto end;
5549 break;
5551 case VT_VOID:
5552 V_VT(result) = VT_EMPTY;
5553 rc = DISP_E_BADVARTYPE;
5554 goto end;
5555 case VT_NULL:
5556 if(V_VT(left) == VT_VOID)
5558 V_VT(result) = VT_EMPTY;
5559 rc = DISP_E_BADVARTYPE;
5560 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5561 lOk)
5563 V_VT(result) = VT_NULL;
5564 rc = S_OK;
5565 } else
5567 V_VT(result) = VT_NULL;
5568 rc = DISP_E_BADVARTYPE;
5570 goto end;
5571 case VT_VARIANT:
5572 case VT_UNKNOWN:
5573 V_VT(result) = VT_EMPTY;
5574 rc = DISP_E_TYPEMISMATCH;
5575 goto end;
5576 case VT_ERROR:
5577 rc = DISP_E_TYPEMISMATCH;
5578 goto end;
5579 case VT_RECORD:
5580 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5582 V_VT(result) = VT_EMPTY;
5583 rc = DISP_E_BADVARTYPE;
5584 } else
5586 V_VT(result) = VT_EMPTY;
5587 rc = DISP_E_TYPEMISMATCH;
5589 goto end;
5590 default:
5591 V_VT(result) = VT_EMPTY;
5592 rc = DISP_E_BADVARTYPE;
5593 goto end;
5596 /* determine the result type */
5597 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5598 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5599 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5600 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5601 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5602 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5603 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5604 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5605 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5606 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5607 else resT = VT_I4; /* most outputs are I4 */
5609 /* convert to I8 for the modulo */
5610 rc = VariantChangeType(&lv, left, 0, VT_I8);
5611 if(FAILED(rc))
5613 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5614 goto end;
5617 rc = VariantChangeType(&rv, right, 0, VT_I8);
5618 if(FAILED(rc))
5620 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5621 goto end;
5624 /* if right is zero set VT_EMPTY and return divide by zero */
5625 if(V_I8(&rv) == 0)
5627 V_VT(result) = VT_EMPTY;
5628 rc = DISP_E_DIVBYZERO;
5629 goto end;
5632 /* perform the modulo operation */
5633 V_VT(result) = VT_I8;
5634 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5636 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5637 wine_dbgstr_longlong(V_I8(&lv)), wine_dbgstr_longlong(V_I8(&rv)),
5638 wine_dbgstr_longlong(V_I8(result)));
5640 /* convert left and right to the destination type */
5641 rc = VariantChangeType(result, result, 0, resT);
5642 if(FAILED(rc))
5644 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5645 /* fall to end of function */
5648 end:
5649 VariantClear(&lv);
5650 VariantClear(&rv);
5651 VariantClear(&tempLeft);
5652 VariantClear(&tempRight);
5653 return rc;
5656 /**********************************************************************
5657 * VarPow [OLEAUT32.158]
5659 * Computes the power of one variant to another variant.
5661 * PARAMS
5662 * left [I] First variant
5663 * right [I] Second variant
5664 * result [O] Result variant
5666 * RETURNS
5667 * Success: S_OK.
5668 * Failure: An HRESULT error code indicating the error.
5670 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5672 HRESULT hr = S_OK;
5673 VARIANT dl,dr;
5674 VARTYPE resvt = VT_EMPTY;
5675 VARTYPE leftvt,rightvt;
5676 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5677 VARIANT tempLeft, tempRight;
5679 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
5680 right, debugstr_VT(right), debugstr_VF(right), result);
5682 VariantInit(&dl);
5683 VariantInit(&dr);
5684 VariantInit(&tempLeft);
5685 VariantInit(&tempRight);
5687 /* Handle VT_DISPATCH by storing and taking address of returned value */
5688 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5690 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5691 if (FAILED(hr)) goto end;
5692 left = &tempLeft;
5694 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5696 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5697 if (FAILED(hr)) goto end;
5698 right = &tempRight;
5701 leftvt = V_VT(left)&VT_TYPEMASK;
5702 rightvt = V_VT(right)&VT_TYPEMASK;
5703 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5704 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5706 if (leftExtraFlags != rightExtraFlags)
5708 hr = DISP_E_BADVARTYPE;
5709 goto end;
5711 ExtraFlags = leftExtraFlags;
5713 /* Native VarPow always returns an error when using extra flags */
5714 if (ExtraFlags != 0)
5716 hr = DISP_E_BADVARTYPE;
5717 goto end;
5720 /* Determine return type */
5721 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5722 V_VT(result) = VT_NULL;
5723 hr = S_OK;
5724 goto end;
5726 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5727 leftvt == VT_I4 || leftvt == VT_R4 ||
5728 leftvt == VT_R8 || leftvt == VT_CY ||
5729 leftvt == VT_DATE || leftvt == VT_BSTR ||
5730 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5731 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5732 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5733 rightvt == VT_I4 || rightvt == VT_R4 ||
5734 rightvt == VT_R8 || rightvt == VT_CY ||
5735 rightvt == VT_DATE || rightvt == VT_BSTR ||
5736 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5737 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5738 resvt = VT_R8;
5739 else
5741 hr = DISP_E_BADVARTYPE;
5742 goto end;
5745 hr = VariantChangeType(&dl,left,0,resvt);
5746 if (FAILED(hr)) {
5747 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5748 hr = E_FAIL;
5749 goto end;
5752 hr = VariantChangeType(&dr,right,0,resvt);
5753 if (FAILED(hr)) {
5754 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5755 hr = E_FAIL;
5756 goto end;
5759 V_VT(result) = VT_R8;
5760 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5762 end:
5763 VariantClear(&dl);
5764 VariantClear(&dr);
5765 VariantClear(&tempLeft);
5766 VariantClear(&tempRight);
5768 return hr;
5771 /**********************************************************************
5772 * VarImp [OLEAUT32.154]
5774 * Bitwise implication of two variants.
5776 * PARAMS
5777 * left [I] First variant
5778 * right [I] Second variant
5779 * result [O] Result variant
5781 * RETURNS
5782 * Success: S_OK.
5783 * Failure: An HRESULT error code indicating the error.
5785 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5787 HRESULT hres = S_OK;
5788 VARTYPE resvt = VT_EMPTY;
5789 VARTYPE leftvt,rightvt;
5790 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5791 VARIANT lv,rv;
5792 double d;
5793 VARIANT tempLeft, tempRight;
5795 VariantInit(&lv);
5796 VariantInit(&rv);
5797 VariantInit(&tempLeft);
5798 VariantInit(&tempRight);
5800 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5801 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5803 /* Handle VT_DISPATCH by storing and taking address of returned value */
5804 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5806 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5807 if (FAILED(hres)) goto VarImp_Exit;
5808 left = &tempLeft;
5810 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5812 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5813 if (FAILED(hres)) goto VarImp_Exit;
5814 right = &tempRight;
5817 leftvt = V_VT(left)&VT_TYPEMASK;
5818 rightvt = V_VT(right)&VT_TYPEMASK;
5819 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5820 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5822 if (leftExtraFlags != rightExtraFlags)
5824 hres = DISP_E_BADVARTYPE;
5825 goto VarImp_Exit;
5827 ExtraFlags = leftExtraFlags;
5829 /* Native VarImp always returns an error when using extra
5830 * flags or if the variants are I8 and INT.
5832 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5833 ExtraFlags != 0)
5835 hres = DISP_E_BADVARTYPE;
5836 goto VarImp_Exit;
5839 /* Determine result type */
5840 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5841 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5843 V_VT(result) = VT_NULL;
5844 hres = S_OK;
5845 goto VarImp_Exit;
5847 else if (leftvt == VT_I8 || rightvt == VT_I8)
5848 resvt = VT_I8;
5849 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5850 leftvt == VT_INT || rightvt == VT_INT ||
5851 leftvt == VT_UINT || rightvt == VT_UINT ||
5852 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5853 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5854 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5855 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5856 leftvt == VT_DATE || rightvt == VT_DATE ||
5857 leftvt == VT_CY || rightvt == VT_CY ||
5858 leftvt == VT_R8 || rightvt == VT_R8 ||
5859 leftvt == VT_R4 || rightvt == VT_R4 ||
5860 leftvt == VT_I1 || rightvt == VT_I1)
5861 resvt = VT_I4;
5862 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5863 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5864 (leftvt == VT_NULL && rightvt == VT_UI1))
5865 resvt = VT_UI1;
5866 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5867 leftvt == VT_I2 || rightvt == VT_I2 ||
5868 leftvt == VT_UI1 || rightvt == VT_UI1)
5869 resvt = VT_I2;
5870 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5871 leftvt == VT_BSTR || rightvt == VT_BSTR)
5872 resvt = VT_BOOL;
5874 /* VT_NULL requires special handling for when the opposite
5875 * variant is equal to something other than -1.
5876 * (NULL Imp 0 = NULL, NULL Imp n = n)
5878 if (leftvt == VT_NULL)
5880 VARIANT_BOOL b;
5881 switch(rightvt)
5883 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5884 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5885 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5886 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5887 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5888 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5889 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5890 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5891 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5892 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5893 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5894 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5895 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5896 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5897 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5898 case VT_DECIMAL:
5899 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5900 resvt = VT_NULL;
5901 break;
5902 case VT_BSTR:
5903 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5904 if (FAILED(hres)) goto VarImp_Exit;
5905 else if (!b)
5906 V_VT(result) = VT_NULL;
5907 else
5909 V_VT(result) = VT_BOOL;
5910 V_BOOL(result) = b;
5912 goto VarImp_Exit;
5914 if (resvt == VT_NULL)
5916 V_VT(result) = resvt;
5917 goto VarImp_Exit;
5919 else
5921 hres = VariantChangeType(result,right,0,resvt);
5922 goto VarImp_Exit;
5926 /* Special handling is required when NULL is the right variant.
5927 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5929 else if (rightvt == VT_NULL)
5931 VARIANT_BOOL b;
5932 switch(leftvt)
5934 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5935 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5936 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5937 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5938 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5939 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5940 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5941 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5942 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5943 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5944 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5945 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5946 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5947 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5948 case VT_DECIMAL:
5949 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5950 resvt = VT_NULL;
5951 break;
5952 case VT_BSTR:
5953 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5954 if (FAILED(hres)) goto VarImp_Exit;
5955 else if (b == VARIANT_TRUE)
5956 resvt = VT_NULL;
5958 if (resvt == VT_NULL)
5960 V_VT(result) = resvt;
5961 goto VarImp_Exit;
5965 hres = VariantCopy(&lv, left);
5966 if (FAILED(hres)) goto VarImp_Exit;
5968 if (rightvt == VT_NULL)
5970 memset( &rv, 0, sizeof(rv) );
5971 V_VT(&rv) = resvt;
5973 else
5975 hres = VariantCopy(&rv, right);
5976 if (FAILED(hres)) goto VarImp_Exit;
5979 if (V_VT(&lv) == VT_BSTR &&
5980 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5981 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5982 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5983 hres = VariantChangeType(&lv,&lv,0,resvt);
5984 if (FAILED(hres)) goto VarImp_Exit;
5986 if (V_VT(&rv) == VT_BSTR &&
5987 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5988 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5989 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5990 hres = VariantChangeType(&rv, &rv, 0, resvt);
5991 if (FAILED(hres)) goto VarImp_Exit;
5993 /* do the math */
5994 V_VT(result) = resvt;
5995 switch (resvt)
5997 case VT_I8:
5998 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5999 break;
6000 case VT_I4:
6001 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
6002 break;
6003 case VT_I2:
6004 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
6005 break;
6006 case VT_UI1:
6007 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
6008 break;
6009 case VT_BOOL:
6010 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
6011 break;
6012 default:
6013 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
6014 leftvt,rightvt);
6017 VarImp_Exit:
6019 VariantClear(&lv);
6020 VariantClear(&rv);
6021 VariantClear(&tempLeft);
6022 VariantClear(&tempRight);
6024 return hres;