user32/tests: Fix a couple of dimension checks on W2k3 and Vista.
[wine/multimedia.git] / dlls / oleaut32 / variant.c
blob909cf8a830c7a2e17baa0bd378525e275d61d85e
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 /******************************************************************************
582 * VariantClear [OLEAUT32.9]
584 * Clear a variant.
586 * PARAMS
587 * pVarg [I/O] Variant to clear
589 * RETURNS
590 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
591 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
593 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
595 HRESULT hres = S_OK;
597 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
599 hres = VARIANT_ValidateType(V_VT(pVarg));
601 if (SUCCEEDED(hres))
603 if (!V_ISBYREF(pVarg))
605 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
607 if (V_ARRAY(pVarg))
608 hres = SafeArrayDestroy(V_ARRAY(pVarg));
610 else if (V_VT(pVarg) == VT_BSTR)
612 SysFreeString(V_BSTR(pVarg));
614 else if (V_VT(pVarg) == VT_RECORD)
616 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
617 if (pBr->pRecInfo)
619 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
620 IRecordInfo_Release(pBr->pRecInfo);
623 else if (V_VT(pVarg) == VT_DISPATCH ||
624 V_VT(pVarg) == VT_UNKNOWN)
626 if (V_UNKNOWN(pVarg))
627 IUnknown_Release(V_UNKNOWN(pVarg));
630 V_VT(pVarg) = VT_EMPTY;
632 return hres;
635 /******************************************************************************
636 * Copy an IRecordInfo object contained in a variant.
638 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
640 HRESULT hres = S_OK;
642 if (pBr->pRecInfo)
644 ULONG ulSize;
646 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
647 if (SUCCEEDED(hres))
649 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
650 if (!pvRecord)
651 hres = E_OUTOFMEMORY;
652 else
654 memcpy(pvRecord, pBr->pvRecord, ulSize);
655 pBr->pvRecord = pvRecord;
657 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
658 if (SUCCEEDED(hres))
659 IRecordInfo_AddRef(pBr->pRecInfo);
663 else if (pBr->pvRecord)
664 hres = E_INVALIDARG;
665 return hres;
668 /******************************************************************************
669 * VariantCopy [OLEAUT32.10]
671 * Copy a variant.
673 * PARAMS
674 * pvargDest [O] Destination for copy
675 * pvargSrc [I] Source variant to copy
677 * RETURNS
678 * Success: S_OK. pvargDest contains a copy of pvargSrc.
679 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
680 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
681 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
682 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
684 * NOTES
685 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
686 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
687 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
688 * fails, so does this function.
689 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
690 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
691 * is copied rather than just any pointers to it.
692 * - For by-value object types the object pointer is copied and the objects
693 * reference count increased using IUnknown_AddRef().
694 * - For all by-reference types, only the referencing pointer is copied.
696 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
698 HRESULT hres = S_OK;
700 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
701 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
702 debugstr_VF(pvargSrc));
704 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
705 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
706 return DISP_E_BADVARTYPE;
708 if (pvargSrc != pvargDest &&
709 SUCCEEDED(hres = VariantClear(pvargDest)))
711 *pvargDest = *pvargSrc; /* Shallow copy the value */
713 if (!V_ISBYREF(pvargSrc))
715 if (V_ISARRAY(pvargSrc))
717 if (V_ARRAY(pvargSrc))
718 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
720 else if (V_VT(pvargSrc) == VT_BSTR)
722 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
723 if (!V_BSTR(pvargDest))
725 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
726 hres = E_OUTOFMEMORY;
729 else if (V_VT(pvargSrc) == VT_RECORD)
731 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
733 else if (V_VT(pvargSrc) == VT_DISPATCH ||
734 V_VT(pvargSrc) == VT_UNKNOWN)
736 if (V_UNKNOWN(pvargSrc))
737 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
741 return hres;
744 /* Return the byte size of a variants data */
745 static inline size_t VARIANT_DataSize(const VARIANT* pv)
747 switch (V_TYPE(pv))
749 case VT_I1:
750 case VT_UI1: return sizeof(BYTE);
751 case VT_I2:
752 case VT_UI2: return sizeof(SHORT);
753 case VT_INT:
754 case VT_UINT:
755 case VT_I4:
756 case VT_UI4: return sizeof(LONG);
757 case VT_I8:
758 case VT_UI8: return sizeof(LONGLONG);
759 case VT_R4: return sizeof(float);
760 case VT_R8: return sizeof(double);
761 case VT_DATE: return sizeof(DATE);
762 case VT_BOOL: return sizeof(VARIANT_BOOL);
763 case VT_DISPATCH:
764 case VT_UNKNOWN:
765 case VT_BSTR: return sizeof(void*);
766 case VT_CY: return sizeof(CY);
767 case VT_ERROR: return sizeof(SCODE);
769 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
770 return 0;
773 /******************************************************************************
774 * VariantCopyInd [OLEAUT32.11]
776 * Copy a variant, dereferencing it if it is by-reference.
778 * PARAMS
779 * pvargDest [O] Destination for copy
780 * pvargSrc [I] Source variant to copy
782 * RETURNS
783 * Success: S_OK. pvargDest contains a copy of pvargSrc.
784 * Failure: An HRESULT error code indicating the error.
786 * NOTES
787 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
788 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
789 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
790 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
791 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
793 * NOTES
794 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
795 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
796 * value.
797 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
798 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
799 * to it. If clearing pvargDest fails, so does this function.
801 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
803 VARIANTARG vTmp, *pSrc = pvargSrc;
804 VARTYPE vt;
805 HRESULT hres = S_OK;
807 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
808 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
809 debugstr_VF(pvargSrc));
811 if (!V_ISBYREF(pvargSrc))
812 return VariantCopy(pvargDest, pvargSrc);
814 /* Argument checking is more lax than VariantCopy()... */
815 vt = V_TYPE(pvargSrc);
816 if (V_ISARRAY(pvargSrc) ||
817 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
818 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
820 /* OK */
822 else
823 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
825 if (pvargSrc == pvargDest)
827 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
828 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
830 vTmp = *pvargSrc;
831 pSrc = &vTmp;
832 V_VT(pvargDest) = VT_EMPTY;
834 else
836 /* Copy into another variant. Free the variant in pvargDest */
837 if (FAILED(hres = VariantClear(pvargDest)))
839 TRACE("VariantClear() of destination failed\n");
840 return hres;
844 if (V_ISARRAY(pSrc))
846 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
847 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
849 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
851 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
852 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
854 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
856 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
857 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
859 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
860 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
862 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
863 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
864 if (*V_UNKNOWNREF(pSrc))
865 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
867 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
869 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
870 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
871 hres = E_INVALIDARG; /* Don't dereference more than one level */
872 else
873 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
875 /* Use the dereferenced variants type value, not VT_VARIANT */
876 goto VariantCopyInd_Return;
878 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
880 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
881 sizeof(DECIMAL) - sizeof(USHORT));
883 else
885 /* Copy the pointed to data into this variant */
886 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
889 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
891 VariantCopyInd_Return:
893 if (pSrc != pvargSrc)
894 VariantClear(pSrc);
896 TRACE("returning 0x%08x, %p->(%s%s)\n", hres, pvargDest,
897 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
898 return hres;
901 /******************************************************************************
902 * VariantChangeType [OLEAUT32.12]
904 * Change the type of a variant.
906 * PARAMS
907 * pvargDest [O] Destination for the converted variant
908 * pvargSrc [O] Source variant to change the type of
909 * wFlags [I] VARIANT_ flags from "oleauto.h"
910 * vt [I] Variant type to change pvargSrc into
912 * RETURNS
913 * Success: S_OK. pvargDest contains the converted value.
914 * Failure: An HRESULT error code describing the failure.
916 * NOTES
917 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
918 * See VariantChangeTypeEx.
920 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
921 USHORT wFlags, VARTYPE vt)
923 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
926 /******************************************************************************
927 * VariantChangeTypeEx [OLEAUT32.147]
929 * Change the type of a variant.
931 * PARAMS
932 * pvargDest [O] Destination for the converted variant
933 * pvargSrc [O] Source variant to change the type of
934 * lcid [I] LCID for the conversion
935 * wFlags [I] VARIANT_ flags from "oleauto.h"
936 * vt [I] Variant type to change pvargSrc into
938 * RETURNS
939 * Success: S_OK. pvargDest contains the converted value.
940 * Failure: An HRESULT error code describing the failure.
942 * NOTES
943 * pvargDest and pvargSrc can point to the same variant to perform an in-place
944 * conversion. If the conversion is successful, pvargSrc will be freed.
946 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
947 LCID lcid, USHORT wFlags, VARTYPE vt)
949 HRESULT res = S_OK;
951 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%04x,%s%s)\n", pvargDest,
952 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
953 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
954 debugstr_vt(vt), debugstr_vf(vt));
956 if (vt == VT_CLSID)
957 res = DISP_E_BADVARTYPE;
958 else
960 res = VARIANT_ValidateType(V_VT(pvargSrc));
962 if (SUCCEEDED(res))
964 res = VARIANT_ValidateType(vt);
966 if (SUCCEEDED(res))
968 VARIANTARG vTmp, vSrcDeref;
970 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
971 res = DISP_E_TYPEMISMATCH;
972 else
974 V_VT(&vTmp) = VT_EMPTY;
975 V_VT(&vSrcDeref) = VT_EMPTY;
976 VariantClear(&vTmp);
977 VariantClear(&vSrcDeref);
980 if (SUCCEEDED(res))
982 res = VariantCopyInd(&vSrcDeref, pvargSrc);
983 if (SUCCEEDED(res))
985 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
986 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
987 else
988 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
990 if (SUCCEEDED(res)) {
991 V_VT(&vTmp) = vt;
992 VariantCopy(pvargDest, &vTmp);
994 VariantClear(&vTmp);
995 VariantClear(&vSrcDeref);
1002 TRACE("returning 0x%08x, %p->(%s%s)\n", res, pvargDest,
1003 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1004 return res;
1007 /* Date Conversions */
1009 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1011 /* Convert a VT_DATE value to a Julian Date */
1012 static inline int VARIANT_JulianFromDate(int dateIn)
1014 int julianDays = dateIn;
1016 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1017 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1018 return julianDays;
1021 /* Convert a Julian Date to a VT_DATE value */
1022 static inline int VARIANT_DateFromJulian(int dateIn)
1024 int julianDays = dateIn;
1026 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1027 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1028 return julianDays;
1031 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1032 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1034 int j, i, l, n;
1036 l = jd + 68569;
1037 n = l * 4 / 146097;
1038 l -= (n * 146097 + 3) / 4;
1039 i = (4000 * (l + 1)) / 1461001;
1040 l += 31 - (i * 1461) / 4;
1041 j = (l * 80) / 2447;
1042 *day = l - (j * 2447) / 80;
1043 l = j / 11;
1044 *month = (j + 2) - (12 * l);
1045 *year = 100 * (n - 49) + i + l;
1048 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1049 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1051 int m12 = (month - 14) / 12;
1053 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1054 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1057 /* Macros for accessing DOS format date/time fields */
1058 #define DOS_YEAR(x) (1980 + (x >> 9))
1059 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1060 #define DOS_DAY(x) (x & 0x1f)
1061 #define DOS_HOUR(x) (x >> 11)
1062 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1063 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1064 /* Create a DOS format date/time */
1065 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1066 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1068 /* Roll a date forwards or backwards to correct it */
1069 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1071 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1073 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1074 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1076 /* Years < 100 are treated as 1900 + year */
1077 if (lpUd->st.wYear < 100)
1078 lpUd->st.wYear += 1900;
1080 if (!lpUd->st.wMonth)
1082 /* Roll back to December of the previous year */
1083 lpUd->st.wMonth = 12;
1084 lpUd->st.wYear--;
1086 else while (lpUd->st.wMonth > 12)
1088 /* Roll forward the correct number of months */
1089 lpUd->st.wYear++;
1090 lpUd->st.wMonth -= 12;
1093 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1094 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1095 return E_INVALIDARG; /* Invalid values */
1097 if (!lpUd->st.wDay)
1099 /* Roll back the date one day */
1100 if (lpUd->st.wMonth == 1)
1102 /* Roll back to December 31 of the previous year */
1103 lpUd->st.wDay = 31;
1104 lpUd->st.wMonth = 12;
1105 lpUd->st.wYear--;
1107 else
1109 lpUd->st.wMonth--; /* Previous month */
1110 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1111 lpUd->st.wDay = 29; /* February has 29 days on leap years */
1112 else
1113 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1116 else if (lpUd->st.wDay > 28)
1118 int rollForward = 0;
1120 /* Possibly need to roll the date forward */
1121 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1122 rollForward = lpUd->st.wDay - 29; /* February has 29 days on leap years */
1123 else
1124 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1126 if (rollForward > 0)
1128 lpUd->st.wDay = rollForward;
1129 lpUd->st.wMonth++;
1130 if (lpUd->st.wMonth > 12)
1132 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1133 lpUd->st.wYear++;
1137 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1138 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1139 return S_OK;
1142 /**********************************************************************
1143 * DosDateTimeToVariantTime [OLEAUT32.14]
1145 * Convert a Dos format date and time into variant VT_DATE format.
1147 * PARAMS
1148 * wDosDate [I] Dos format date
1149 * wDosTime [I] Dos format time
1150 * pDateOut [O] Destination for VT_DATE format
1152 * RETURNS
1153 * Success: TRUE. pDateOut contains the converted time.
1154 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1156 * NOTES
1157 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1158 * - Dos format times are accurate to only 2 second precision.
1159 * - The format of a Dos Date is:
1160 *| Bits Values Meaning
1161 *| ---- ------ -------
1162 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1163 *| the days in the month rolls forward the extra days.
1164 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1165 *| year. 13-15 are invalid.
1166 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1167 * - The format of a Dos Time is:
1168 *| Bits Values Meaning
1169 *| ---- ------ -------
1170 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1171 *| 5-10 0-59 Minutes. 60-63 are invalid.
1172 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1174 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1175 double *pDateOut)
1177 UDATE ud;
1179 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1180 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1181 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1182 pDateOut);
1184 ud.st.wYear = DOS_YEAR(wDosDate);
1185 ud.st.wMonth = DOS_MONTH(wDosDate);
1186 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1187 return FALSE;
1188 ud.st.wDay = DOS_DAY(wDosDate);
1189 ud.st.wHour = DOS_HOUR(wDosTime);
1190 ud.st.wMinute = DOS_MINUTE(wDosTime);
1191 ud.st.wSecond = DOS_SECOND(wDosTime);
1192 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1194 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1197 /**********************************************************************
1198 * VariantTimeToDosDateTime [OLEAUT32.13]
1200 * Convert a variant format date into a Dos format date and time.
1202 * dateIn [I] VT_DATE time format
1203 * pwDosDate [O] Destination for Dos format date
1204 * pwDosTime [O] Destination for Dos format time
1206 * RETURNS
1207 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1208 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1210 * NOTES
1211 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1213 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1215 UDATE ud;
1217 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1219 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1220 return FALSE;
1222 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1223 return FALSE;
1225 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1226 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1228 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1229 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1230 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1231 return TRUE;
1234 /***********************************************************************
1235 * SystemTimeToVariantTime [OLEAUT32.184]
1237 * Convert a System format date and time into variant VT_DATE format.
1239 * PARAMS
1240 * lpSt [I] System format date and time
1241 * pDateOut [O] Destination for VT_DATE format date
1243 * RETURNS
1244 * Success: TRUE. *pDateOut contains the converted value.
1245 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1247 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1249 UDATE ud;
1251 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1252 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1254 if (lpSt->wMonth > 12)
1255 return FALSE;
1257 ud.st = *lpSt;
1258 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1261 /***********************************************************************
1262 * VariantTimeToSystemTime [OLEAUT32.185]
1264 * Convert a variant VT_DATE into a System format date and time.
1266 * PARAMS
1267 * datein [I] Variant VT_DATE format date
1268 * lpSt [O] Destination for System format date and time
1270 * RETURNS
1271 * Success: TRUE. *lpSt contains the converted value.
1272 * Failure: FALSE, if dateIn is too large or small.
1274 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1276 UDATE ud;
1278 TRACE("(%g,%p)\n", dateIn, lpSt);
1280 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1281 return FALSE;
1283 *lpSt = ud.st;
1284 return TRUE;
1287 /***********************************************************************
1288 * VarDateFromUdateEx [OLEAUT32.319]
1290 * Convert an unpacked format date and time to a variant VT_DATE.
1292 * PARAMS
1293 * pUdateIn [I] Unpacked format date and time to convert
1294 * lcid [I] Locale identifier for the conversion
1295 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1296 * pDateOut [O] Destination for variant VT_DATE.
1298 * RETURNS
1299 * Success: S_OK. *pDateOut contains the converted value.
1300 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1302 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1304 UDATE ud;
1305 double dateVal;
1307 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1308 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1309 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1310 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1311 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1313 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1314 FIXME("lcid possibly not handled, treating as en-us\n");
1316 ud = *pUdateIn;
1318 if (dwFlags & VAR_VALIDDATE)
1319 WARN("Ignoring VAR_VALIDDATE\n");
1321 if (FAILED(VARIANT_RollUdate(&ud)))
1322 return E_INVALIDARG;
1324 /* Date */
1325 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1327 /* Time */
1328 dateVal += ud.st.wHour / 24.0;
1329 dateVal += ud.st.wMinute / 1440.0;
1330 dateVal += ud.st.wSecond / 86400.0;
1331 dateVal += ud.st.wMilliseconds / 86400000.0;
1333 TRACE("Returning %g\n", dateVal);
1334 *pDateOut = dateVal;
1335 return S_OK;
1338 /***********************************************************************
1339 * VarDateFromUdate [OLEAUT32.330]
1341 * Convert an unpacked format date and time to a variant VT_DATE.
1343 * PARAMS
1344 * pUdateIn [I] Unpacked format date and time to convert
1345 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1346 * pDateOut [O] Destination for variant VT_DATE.
1348 * RETURNS
1349 * Success: S_OK. *pDateOut contains the converted value.
1350 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1352 * NOTES
1353 * This function uses the United States English locale for the conversion. Use
1354 * VarDateFromUdateEx() for alternate locales.
1356 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1358 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1360 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1363 /***********************************************************************
1364 * VarUdateFromDate [OLEAUT32.331]
1366 * Convert a variant VT_DATE into an unpacked format date and time.
1368 * PARAMS
1369 * datein [I] Variant VT_DATE format date
1370 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1371 * lpUdate [O] Destination for unpacked format date and time
1373 * RETURNS
1374 * Success: S_OK. *lpUdate contains the converted value.
1375 * Failure: E_INVALIDARG, if dateIn is too large or small.
1377 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1379 /* Cumulative totals of days per month */
1380 static const USHORT cumulativeDays[] =
1382 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1384 double datePart, timePart;
1385 int julianDays;
1387 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1389 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1390 return E_INVALIDARG;
1392 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1393 /* Compensate for int truncation (always downwards) */
1394 timePart = dateIn - datePart + 0.00000000001;
1395 if (timePart >= 1.0)
1396 timePart -= 0.00000000001;
1398 /* Date */
1399 julianDays = VARIANT_JulianFromDate(dateIn);
1400 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1401 &lpUdate->st.wDay);
1403 datePart = (datePart + 1.5) / 7.0;
1404 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1405 if (lpUdate->st.wDayOfWeek == 0)
1406 lpUdate->st.wDayOfWeek = 5;
1407 else if (lpUdate->st.wDayOfWeek == 1)
1408 lpUdate->st.wDayOfWeek = 6;
1409 else
1410 lpUdate->st.wDayOfWeek -= 2;
1412 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1413 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1414 else
1415 lpUdate->wDayOfYear = 0;
1417 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1418 lpUdate->wDayOfYear += lpUdate->st.wDay;
1420 /* Time */
1421 timePart *= 24.0;
1422 lpUdate->st.wHour = timePart;
1423 timePart -= lpUdate->st.wHour;
1424 timePart *= 60.0;
1425 lpUdate->st.wMinute = timePart;
1426 timePart -= lpUdate->st.wMinute;
1427 timePart *= 60.0;
1428 lpUdate->st.wSecond = timePart;
1429 timePart -= lpUdate->st.wSecond;
1430 lpUdate->st.wMilliseconds = 0;
1431 if (timePart > 0.5)
1433 /* Round the milliseconds, adjusting the time/date forward if needed */
1434 if (lpUdate->st.wSecond < 59)
1435 lpUdate->st.wSecond++;
1436 else
1438 lpUdate->st.wSecond = 0;
1439 if (lpUdate->st.wMinute < 59)
1440 lpUdate->st.wMinute++;
1441 else
1443 lpUdate->st.wMinute = 0;
1444 if (lpUdate->st.wHour < 23)
1445 lpUdate->st.wHour++;
1446 else
1448 lpUdate->st.wHour = 0;
1449 /* Roll over a whole day */
1450 if (++lpUdate->st.wDay > 28)
1451 VARIANT_RollUdate(lpUdate);
1456 return S_OK;
1459 #define GET_NUMBER_TEXT(fld,name) \
1460 buff[0] = 0; \
1461 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1462 WARN("buffer too small for " #fld "\n"); \
1463 else \
1464 if (buff[0]) lpChars->name = buff[0]; \
1465 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1467 /* Get the valid number characters for an lcid */
1468 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1470 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1471 static CRITICAL_SECTION csLastChars = { NULL, -1, 0, 0, 0, 0 };
1472 static VARIANT_NUMBER_CHARS lastChars;
1473 static LCID lastLcid = -1;
1474 static DWORD lastFlags = 0;
1475 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1476 WCHAR buff[4];
1478 /* To make caching thread-safe, a critical section is needed */
1479 EnterCriticalSection(&csLastChars);
1481 /* Asking for default locale entries is very expensive: It is a registry
1482 server call. So cache one locally, as Microsoft does it too */
1483 if(lcid == lastLcid && dwFlags == lastFlags)
1485 memcpy(lpChars, &lastChars, sizeof(defaultChars));
1486 LeaveCriticalSection(&csLastChars);
1487 return;
1490 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1491 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1492 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1493 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1494 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1495 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1496 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1498 /* Local currency symbols are often 2 characters */
1499 lpChars->cCurrencyLocal2 = '\0';
1500 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1502 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1503 case 2: lpChars->cCurrencyLocal = buff[0];
1504 break;
1505 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1507 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1508 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1510 memcpy(&lastChars, lpChars, sizeof(defaultChars));
1511 lastLcid = lcid;
1512 lastFlags = dwFlags;
1513 LeaveCriticalSection(&csLastChars);
1516 /* Number Parsing States */
1517 #define B_PROCESSING_EXPONENT 0x1
1518 #define B_NEGATIVE_EXPONENT 0x2
1519 #define B_EXPONENT_START 0x4
1520 #define B_INEXACT_ZEROS 0x8
1521 #define B_LEADING_ZERO 0x10
1522 #define B_PROCESSING_HEX 0x20
1523 #define B_PROCESSING_OCT 0x40
1525 /**********************************************************************
1526 * VarParseNumFromStr [OLEAUT32.46]
1528 * Parse a string containing a number into a NUMPARSE structure.
1530 * PARAMS
1531 * lpszStr [I] String to parse number from
1532 * lcid [I] Locale Id for the conversion
1533 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1534 * pNumprs [I/O] Destination for parsed number
1535 * rgbDig [O] Destination for digits read in
1537 * RETURNS
1538 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1539 * the number.
1540 * Failure: E_INVALIDARG, if any parameter is invalid.
1541 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1542 * incorrectly.
1543 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1545 * NOTES
1546 * pNumprs must have the following fields set:
1547 * cDig: Set to the size of rgbDig.
1548 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1549 * from "oleauto.h".
1551 * FIXME
1552 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1553 * numerals, so this has not been implemented.
1555 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1556 NUMPARSE *pNumprs, BYTE *rgbDig)
1558 VARIANT_NUMBER_CHARS chars;
1559 BYTE rgbTmp[1024];
1560 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1561 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1562 int cchUsed = 0;
1564 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1566 if (!pNumprs || !rgbDig)
1567 return E_INVALIDARG;
1569 if (pNumprs->cDig < iMaxDigits)
1570 iMaxDigits = pNumprs->cDig;
1572 pNumprs->cDig = 0;
1573 pNumprs->dwOutFlags = 0;
1574 pNumprs->cchUsed = 0;
1575 pNumprs->nBaseShift = 0;
1576 pNumprs->nPwr10 = 0;
1578 if (!lpszStr)
1579 return DISP_E_TYPEMISMATCH;
1581 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1583 /* First consume all the leading symbols and space from the string */
1584 while (1)
1586 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1588 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1591 cchUsed++;
1592 lpszStr++;
1593 } while (isspaceW(*lpszStr));
1595 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1596 *lpszStr == chars.cPositiveSymbol &&
1597 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1599 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1600 cchUsed++;
1601 lpszStr++;
1603 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1604 *lpszStr == chars.cNegativeSymbol &&
1605 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1607 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1608 cchUsed++;
1609 lpszStr++;
1611 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1612 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1613 *lpszStr == chars.cCurrencyLocal &&
1614 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1616 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1617 cchUsed++;
1618 lpszStr++;
1619 /* Only accept currency characters */
1620 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1621 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1623 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1624 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1626 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1627 cchUsed++;
1628 lpszStr++;
1630 else
1631 break;
1634 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1636 /* Only accept non-currency characters */
1637 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1638 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1641 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1642 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1644 dwState |= B_PROCESSING_HEX;
1645 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1646 cchUsed=cchUsed+2;
1647 lpszStr=lpszStr+2;
1649 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1650 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1652 dwState |= B_PROCESSING_OCT;
1653 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1654 cchUsed=cchUsed+2;
1655 lpszStr=lpszStr+2;
1658 /* Strip Leading zeros */
1659 while (*lpszStr == '0')
1661 dwState |= B_LEADING_ZERO;
1662 cchUsed++;
1663 lpszStr++;
1666 while (*lpszStr)
1668 if (isdigitW(*lpszStr))
1670 if (dwState & B_PROCESSING_EXPONENT)
1672 int exponentSize = 0;
1673 if (dwState & B_EXPONENT_START)
1675 if (!isdigitW(*lpszStr))
1676 break; /* No exponent digits - invalid */
1677 while (*lpszStr == '0')
1679 /* Skip leading zero's in the exponent */
1680 cchUsed++;
1681 lpszStr++;
1685 while (isdigitW(*lpszStr))
1687 exponentSize *= 10;
1688 exponentSize += *lpszStr - '0';
1689 cchUsed++;
1690 lpszStr++;
1692 if (dwState & B_NEGATIVE_EXPONENT)
1693 exponentSize = -exponentSize;
1694 /* Add the exponent into the powers of 10 */
1695 pNumprs->nPwr10 += exponentSize;
1696 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1697 lpszStr--; /* back up to allow processing of next char */
1699 else
1701 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1702 && !(dwState & B_PROCESSING_OCT))
1704 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1706 if (*lpszStr != '0')
1707 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1709 /* This digit can't be represented, but count it in nPwr10 */
1710 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1711 pNumprs->nPwr10--;
1712 else
1713 pNumprs->nPwr10++;
1715 else
1717 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1718 return DISP_E_TYPEMISMATCH;
1721 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1722 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1724 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1726 pNumprs->cDig++;
1727 cchUsed++;
1730 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1732 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1733 cchUsed++;
1735 else if (*lpszStr == chars.cDecimalPoint &&
1736 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1737 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1739 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1740 cchUsed++;
1742 /* If we have no digits so far, skip leading zeros */
1743 if (!pNumprs->cDig)
1745 while (lpszStr[1] == '0')
1747 dwState |= B_LEADING_ZERO;
1748 cchUsed++;
1749 lpszStr++;
1750 pNumprs->nPwr10--;
1754 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1755 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1756 dwState & B_PROCESSING_HEX)
1758 if (pNumprs->cDig >= iMaxDigits)
1760 return DISP_E_OVERFLOW;
1762 else
1764 if (*lpszStr >= 'a')
1765 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1766 else
1767 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1769 pNumprs->cDig++;
1770 cchUsed++;
1772 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1773 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1774 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1776 dwState |= B_PROCESSING_EXPONENT;
1777 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1778 cchUsed++;
1780 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1782 cchUsed++; /* Ignore positive exponent */
1784 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1786 dwState |= B_NEGATIVE_EXPONENT;
1787 cchUsed++;
1789 else
1790 break; /* Stop at an unrecognised character */
1792 lpszStr++;
1795 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1797 /* Ensure a 0 on its own gets stored */
1798 pNumprs->cDig = 1;
1799 rgbTmp[0] = 0;
1802 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1804 pNumprs->cchUsed = cchUsed;
1805 WARN("didn't completely parse exponent\n");
1806 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1809 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1811 if (dwState & B_INEXACT_ZEROS)
1812 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1813 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1815 /* copy all of the digits into the output digit buffer */
1816 /* this is exactly what windows does although it also returns */
1817 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1818 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1820 if (dwState & B_PROCESSING_HEX) {
1821 /* hex numbers have always the same format */
1822 pNumprs->nPwr10=0;
1823 pNumprs->nBaseShift=4;
1824 } else {
1825 if (dwState & B_PROCESSING_OCT) {
1826 /* oct numbers have always the same format */
1827 pNumprs->nPwr10=0;
1828 pNumprs->nBaseShift=3;
1829 } else {
1830 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1832 pNumprs->nPwr10++;
1833 pNumprs->cDig--;
1837 } else
1839 /* Remove trailing zeros from the last (whole number or decimal) part */
1840 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1842 pNumprs->nPwr10++;
1843 pNumprs->cDig--;
1847 if (pNumprs->cDig <= iMaxDigits)
1848 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1849 else
1850 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1852 /* Copy the digits we processed into rgbDig */
1853 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1855 /* Consume any trailing symbols and space */
1856 while (1)
1858 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1860 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1863 cchUsed++;
1864 lpszStr++;
1865 } while (isspaceW(*lpszStr));
1867 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1868 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1869 *lpszStr == chars.cPositiveSymbol)
1871 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1872 cchUsed++;
1873 lpszStr++;
1875 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1876 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1877 *lpszStr == chars.cNegativeSymbol)
1879 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1880 cchUsed++;
1881 lpszStr++;
1883 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1884 pNumprs->dwOutFlags & NUMPRS_PARENS)
1886 cchUsed++;
1887 lpszStr++;
1888 pNumprs->dwOutFlags |= NUMPRS_NEG;
1890 else
1891 break;
1894 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1896 pNumprs->cchUsed = cchUsed;
1897 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1900 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1901 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1903 if (!pNumprs->cDig)
1904 return DISP_E_TYPEMISMATCH; /* No Number found */
1906 pNumprs->cchUsed = cchUsed;
1907 return S_OK;
1910 /* VTBIT flags indicating an integer value */
1911 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1912 /* VTBIT flags indicating a real number value */
1913 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1915 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1916 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1917 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1918 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1920 /**********************************************************************
1921 * VarNumFromParseNum [OLEAUT32.47]
1923 * Convert a NUMPARSE structure into a numeric Variant type.
1925 * PARAMS
1926 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1927 * rgbDig [I] Source for the numbers digits
1928 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1929 * pVarDst [O] Destination for the converted Variant value.
1931 * RETURNS
1932 * Success: S_OK. pVarDst contains the converted value.
1933 * Failure: E_INVALIDARG, if any parameter is invalid.
1934 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1936 * NOTES
1937 * - The smallest favoured type present in dwVtBits that can represent the
1938 * number in pNumprs without losing precision is used.
1939 * - Signed types are preferred over unsigned types of the same size.
1940 * - Preferred types in order are: integer, float, double, currency then decimal.
1941 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1942 * for details of the rounding method.
1943 * - pVarDst is not cleared before the result is stored in it.
1944 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1945 * design?): If some other VTBIT's for integers are specified together
1946 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1947 * the number to the smallest requested integer truncating this way the
1948 * number. Wine doesn't implement this "feature" (yet?).
1950 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1951 ULONG dwVtBits, VARIANT *pVarDst)
1953 /* Scale factors and limits for double arithmetic */
1954 static const double dblMultipliers[11] = {
1955 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1956 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1958 static const double dblMinimums[11] = {
1959 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1960 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1961 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1963 static const double dblMaximums[11] = {
1964 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1965 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1966 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1969 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1971 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1973 if (pNumprs->nBaseShift)
1975 /* nBaseShift indicates a hex or octal number */
1976 ULONG64 ul64 = 0;
1977 LONG64 l64;
1978 int i;
1980 /* Convert the hex or octal number string into a UI64 */
1981 for (i = 0; i < pNumprs->cDig; i++)
1983 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1985 TRACE("Overflow multiplying digits\n");
1986 return DISP_E_OVERFLOW;
1988 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1991 /* also make a negative representation */
1992 l64=-ul64;
1994 /* Try signed and unsigned types in size order */
1995 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
1997 V_VT(pVarDst) = VT_I1;
1998 V_I1(pVarDst) = ul64;
1999 return S_OK;
2001 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2003 V_VT(pVarDst) = VT_UI1;
2004 V_UI1(pVarDst) = ul64;
2005 return S_OK;
2007 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2009 V_VT(pVarDst) = VT_I2;
2010 V_I2(pVarDst) = ul64;
2011 return S_OK;
2013 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2015 V_VT(pVarDst) = VT_UI2;
2016 V_UI2(pVarDst) = ul64;
2017 return S_OK;
2019 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2021 V_VT(pVarDst) = VT_I4;
2022 V_I4(pVarDst) = ul64;
2023 return S_OK;
2025 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2027 V_VT(pVarDst) = VT_UI4;
2028 V_UI4(pVarDst) = ul64;
2029 return S_OK;
2031 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2033 V_VT(pVarDst) = VT_I8;
2034 V_I8(pVarDst) = ul64;
2035 return S_OK;
2037 else if (dwVtBits & VTBIT_UI8)
2039 V_VT(pVarDst) = VT_UI8;
2040 V_UI8(pVarDst) = ul64;
2041 return S_OK;
2043 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2045 V_VT(pVarDst) = VT_DECIMAL;
2046 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2047 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2048 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2049 return S_OK;
2051 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2053 V_VT(pVarDst) = VT_R4;
2054 if (ul64 <= I4_MAX)
2055 V_R4(pVarDst) = ul64;
2056 else
2057 V_R4(pVarDst) = l64;
2058 return S_OK;
2060 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2062 V_VT(pVarDst) = VT_R8;
2063 if (ul64 <= I4_MAX)
2064 V_R8(pVarDst) = ul64;
2065 else
2066 V_R8(pVarDst) = l64;
2067 return S_OK;
2070 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2071 return DISP_E_OVERFLOW;
2074 /* Count the number of relevant fractional and whole digits stored,
2075 * And compute the divisor/multiplier to scale the number by.
2077 if (pNumprs->nPwr10 < 0)
2079 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2081 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2082 wholeNumberDigits = 0;
2083 fractionalDigits = pNumprs->cDig;
2084 divisor10 = -pNumprs->nPwr10;
2086 else
2088 /* An exactly represented real number e.g. 1.024 */
2089 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2090 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2091 divisor10 = pNumprs->cDig - wholeNumberDigits;
2094 else if (pNumprs->nPwr10 == 0)
2096 /* An exactly represented whole number e.g. 1024 */
2097 wholeNumberDigits = pNumprs->cDig;
2098 fractionalDigits = 0;
2100 else /* pNumprs->nPwr10 > 0 */
2102 /* A whole number followed by nPwr10 0's e.g. 102400 */
2103 wholeNumberDigits = pNumprs->cDig;
2104 fractionalDigits = 0;
2105 multiplier10 = pNumprs->nPwr10;
2108 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2109 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2110 multiplier10, divisor10);
2112 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2113 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2115 /* We have one or more integer output choices, and either:
2116 * 1) An integer input value, or
2117 * 2) A real number input value but no floating output choices.
2118 * Alternately, we have a DECIMAL output available and an integer input.
2120 * So, place the integer value into pVarDst, using the smallest type
2121 * possible and preferring signed over unsigned types.
2123 BOOL bOverflow = FALSE, bNegative;
2124 ULONG64 ul64 = 0;
2125 int i;
2127 /* Convert the integer part of the number into a UI8 */
2128 for (i = 0; i < wholeNumberDigits; i++)
2130 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2132 TRACE("Overflow multiplying digits\n");
2133 bOverflow = TRUE;
2134 break;
2136 ul64 = ul64 * 10 + rgbDig[i];
2139 /* Account for the scale of the number */
2140 if (!bOverflow && multiplier10)
2142 for (i = 0; i < multiplier10; i++)
2144 if (ul64 > (UI8_MAX / 10))
2146 TRACE("Overflow scaling number\n");
2147 bOverflow = TRUE;
2148 break;
2150 ul64 = ul64 * 10;
2154 /* If we have any fractional digits, round the value.
2155 * Note we don't have to do this if divisor10 is < 1,
2156 * because this means the fractional part must be < 0.5
2158 if (!bOverflow && fractionalDigits && divisor10 > 0)
2160 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2161 BOOL bAdjust = FALSE;
2163 TRACE("first decimal value is %d\n", *fracDig);
2165 if (*fracDig > 5)
2166 bAdjust = TRUE; /* > 0.5 */
2167 else if (*fracDig == 5)
2169 for (i = 1; i < fractionalDigits; i++)
2171 if (fracDig[i])
2173 bAdjust = TRUE; /* > 0.5 */
2174 break;
2177 /* If exactly 0.5, round only odd values */
2178 if (i == fractionalDigits && (ul64 & 1))
2179 bAdjust = TRUE;
2182 if (bAdjust)
2184 if (ul64 == UI8_MAX)
2186 TRACE("Overflow after rounding\n");
2187 bOverflow = TRUE;
2189 ul64++;
2193 /* Zero is not a negative number */
2194 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2196 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2198 /* For negative integers, try the signed types in size order */
2199 if (!bOverflow && bNegative)
2201 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2203 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2205 V_VT(pVarDst) = VT_I1;
2206 V_I1(pVarDst) = -ul64;
2207 return S_OK;
2209 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2211 V_VT(pVarDst) = VT_I2;
2212 V_I2(pVarDst) = -ul64;
2213 return S_OK;
2215 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2217 V_VT(pVarDst) = VT_I4;
2218 V_I4(pVarDst) = -ul64;
2219 return S_OK;
2221 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2223 V_VT(pVarDst) = VT_I8;
2224 V_I8(pVarDst) = -ul64;
2225 return S_OK;
2227 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2229 /* Decimal is only output choice left - fast path */
2230 V_VT(pVarDst) = VT_DECIMAL;
2231 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2232 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2233 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2234 return S_OK;
2238 else if (!bOverflow)
2240 /* For positive integers, try signed then unsigned types in size order */
2241 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2243 V_VT(pVarDst) = VT_I1;
2244 V_I1(pVarDst) = ul64;
2245 return S_OK;
2247 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2249 V_VT(pVarDst) = VT_UI1;
2250 V_UI1(pVarDst) = ul64;
2251 return S_OK;
2253 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2255 V_VT(pVarDst) = VT_I2;
2256 V_I2(pVarDst) = ul64;
2257 return S_OK;
2259 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2261 V_VT(pVarDst) = VT_UI2;
2262 V_UI2(pVarDst) = ul64;
2263 return S_OK;
2265 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2267 V_VT(pVarDst) = VT_I4;
2268 V_I4(pVarDst) = ul64;
2269 return S_OK;
2271 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2273 V_VT(pVarDst) = VT_UI4;
2274 V_UI4(pVarDst) = ul64;
2275 return S_OK;
2277 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2279 V_VT(pVarDst) = VT_I8;
2280 V_I8(pVarDst) = ul64;
2281 return S_OK;
2283 else if (dwVtBits & VTBIT_UI8)
2285 V_VT(pVarDst) = VT_UI8;
2286 V_UI8(pVarDst) = ul64;
2287 return S_OK;
2289 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2291 /* Decimal is only output choice left - fast path */
2292 V_VT(pVarDst) = VT_DECIMAL;
2293 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2294 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2295 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2296 return S_OK;
2301 if (dwVtBits & REAL_VTBITS)
2303 /* Try to put the number into a float or real */
2304 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2305 double whole = 0.0;
2306 int i;
2308 /* Convert the number into a double */
2309 for (i = 0; i < pNumprs->cDig; i++)
2310 whole = whole * 10.0 + rgbDig[i];
2312 TRACE("Whole double value is %16.16g\n", whole);
2314 /* Account for the scale */
2315 while (multiplier10 > 10)
2317 if (whole > dblMaximums[10])
2319 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2320 bOverflow = TRUE;
2321 break;
2323 whole = whole * dblMultipliers[10];
2324 multiplier10 -= 10;
2326 if (multiplier10 && !bOverflow)
2328 if (whole > dblMaximums[multiplier10])
2330 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2331 bOverflow = TRUE;
2333 else
2334 whole = whole * dblMultipliers[multiplier10];
2337 if (!bOverflow)
2338 TRACE("Scaled double value is %16.16g\n", whole);
2340 while (divisor10 > 10 && !bOverflow)
2342 if (whole < dblMinimums[10] && whole != 0)
2344 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2345 bOverflow = TRUE;
2346 break;
2348 whole = whole / dblMultipliers[10];
2349 divisor10 -= 10;
2351 if (divisor10 && !bOverflow)
2353 if (whole < dblMinimums[divisor10] && whole != 0)
2355 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2356 bOverflow = TRUE;
2358 else
2359 whole = whole / dblMultipliers[divisor10];
2361 if (!bOverflow)
2362 TRACE("Final double value is %16.16g\n", whole);
2364 if (dwVtBits & VTBIT_R4 &&
2365 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2367 TRACE("Set R4 to final value\n");
2368 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2369 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2370 return S_OK;
2373 if (dwVtBits & VTBIT_R8)
2375 TRACE("Set R8 to final value\n");
2376 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2377 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2378 return S_OK;
2381 if (dwVtBits & VTBIT_CY)
2383 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2385 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2386 TRACE("Set CY to final value\n");
2387 return S_OK;
2389 TRACE("Value Overflows CY\n");
2393 if (dwVtBits & VTBIT_DECIMAL)
2395 int i;
2396 ULONG carry;
2397 ULONG64 tmp;
2398 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2400 DECIMAL_SETZERO(*pDec);
2401 DEC_LO32(pDec) = 0;
2403 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2404 DEC_SIGN(pDec) = DECIMAL_NEG;
2405 else
2406 DEC_SIGN(pDec) = DECIMAL_POS;
2408 /* Factor the significant digits */
2409 for (i = 0; i < pNumprs->cDig; i++)
2411 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2412 carry = (ULONG)(tmp >> 32);
2413 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2414 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2415 carry = (ULONG)(tmp >> 32);
2416 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2417 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2418 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2420 if (tmp >> 32 & UI4_MAX)
2422 VarNumFromParseNum_DecOverflow:
2423 TRACE("Overflow\n");
2424 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2425 return DISP_E_OVERFLOW;
2429 /* Account for the scale of the number */
2430 while (multiplier10 > 0)
2432 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2433 carry = (ULONG)(tmp >> 32);
2434 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2435 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2436 carry = (ULONG)(tmp >> 32);
2437 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2438 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2439 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2441 if (tmp >> 32 & UI4_MAX)
2442 goto VarNumFromParseNum_DecOverflow;
2443 multiplier10--;
2445 DEC_SCALE(pDec) = divisor10;
2447 V_VT(pVarDst) = VT_DECIMAL;
2448 return S_OK;
2450 return DISP_E_OVERFLOW; /* No more output choices */
2453 /**********************************************************************
2454 * VarCat [OLEAUT32.318]
2456 * Concatenates one variant onto another.
2458 * PARAMS
2459 * left [I] First variant
2460 * right [I] Second variant
2461 * result [O] Result variant
2463 * RETURNS
2464 * Success: S_OK.
2465 * Failure: An HRESULT error code indicating the error.
2467 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2469 VARTYPE leftvt,rightvt,resultvt;
2470 HRESULT hres;
2471 static WCHAR str_true[32];
2472 static WCHAR str_false[32];
2473 static const WCHAR sz_empty[] = {'\0'};
2474 leftvt = V_VT(left);
2475 rightvt = V_VT(right);
2477 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2478 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2480 if (!str_true[0]) {
2481 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_FALSE, str_false);
2482 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_TRUE, str_true);
2485 /* when both left and right are NULL the result is NULL */
2486 if (leftvt == VT_NULL && rightvt == VT_NULL)
2488 V_VT(out) = VT_NULL;
2489 return S_OK;
2492 hres = S_OK;
2493 resultvt = VT_EMPTY;
2495 /* There are many special case for errors and return types */
2496 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2497 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2498 hres = DISP_E_TYPEMISMATCH;
2499 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2500 leftvt == VT_R4 || leftvt == VT_R8 ||
2501 leftvt == VT_CY || leftvt == VT_BOOL ||
2502 leftvt == VT_BSTR || leftvt == VT_I1 ||
2503 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2504 leftvt == VT_UI4 || leftvt == VT_I8 ||
2505 leftvt == VT_UI8 || leftvt == VT_INT ||
2506 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2507 leftvt == VT_NULL || leftvt == VT_DATE ||
2508 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2510 (rightvt == VT_I2 || rightvt == VT_I4 ||
2511 rightvt == VT_R4 || rightvt == VT_R8 ||
2512 rightvt == VT_CY || rightvt == VT_BOOL ||
2513 rightvt == VT_BSTR || rightvt == VT_I1 ||
2514 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2515 rightvt == VT_UI4 || rightvt == VT_I8 ||
2516 rightvt == VT_UI8 || rightvt == VT_INT ||
2517 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2518 rightvt == VT_NULL || rightvt == VT_DATE ||
2519 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2520 resultvt = VT_BSTR;
2521 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2522 hres = DISP_E_TYPEMISMATCH;
2523 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2524 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2525 hres = DISP_E_TYPEMISMATCH;
2526 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2527 rightvt == VT_DECIMAL)
2528 hres = DISP_E_BADVARTYPE;
2529 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2530 hres = DISP_E_TYPEMISMATCH;
2531 else if (leftvt == VT_VARIANT)
2532 hres = DISP_E_TYPEMISMATCH;
2533 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2534 leftvt == VT_NULL || leftvt == VT_I2 ||
2535 leftvt == VT_I4 || leftvt == VT_R4 ||
2536 leftvt == VT_R8 || leftvt == VT_CY ||
2537 leftvt == VT_DATE || leftvt == VT_BSTR ||
2538 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2539 leftvt == VT_I1 || leftvt == VT_UI1 ||
2540 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2541 leftvt == VT_I8 || leftvt == VT_UI8 ||
2542 leftvt == VT_INT || leftvt == VT_UINT))
2543 hres = DISP_E_TYPEMISMATCH;
2544 else
2545 hres = DISP_E_BADVARTYPE;
2547 /* if result type is not S_OK, then no need to go further */
2548 if (hres != S_OK)
2550 V_VT(out) = resultvt;
2551 return hres;
2553 /* Else proceed with formatting inputs to strings */
2554 else
2556 VARIANT bstrvar_left, bstrvar_right;
2557 V_VT(out) = VT_BSTR;
2559 VariantInit(&bstrvar_left);
2560 VariantInit(&bstrvar_right);
2562 /* Convert left side variant to string */
2563 if (leftvt != VT_BSTR)
2565 if (leftvt == VT_BOOL)
2567 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2568 V_VT(&bstrvar_left) = VT_BSTR;
2569 if (V_BOOL(left) == TRUE)
2570 V_BSTR(&bstrvar_left) = SysAllocString(str_true);
2571 else
2572 V_BSTR(&bstrvar_left) = SysAllocString(str_false);
2574 /* Fill with empty string for later concat with right side */
2575 else if (leftvt == VT_NULL)
2577 V_VT(&bstrvar_left) = VT_BSTR;
2578 V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
2580 else
2582 hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
2583 if (hres != S_OK) {
2584 VariantClear(&bstrvar_left);
2585 VariantClear(&bstrvar_right);
2586 if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
2587 rightvt == VT_NULL || rightvt == VT_I2 ||
2588 rightvt == VT_I4 || rightvt == VT_R4 ||
2589 rightvt == VT_R8 || rightvt == VT_CY ||
2590 rightvt == VT_DATE || rightvt == VT_BSTR ||
2591 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
2592 rightvt == VT_I1 || rightvt == VT_UI1 ||
2593 rightvt == VT_UI2 || rightvt == VT_UI4 ||
2594 rightvt == VT_I8 || rightvt == VT_UI8 ||
2595 rightvt == VT_INT || rightvt == VT_UINT))
2596 return DISP_E_BADVARTYPE;
2597 return hres;
2602 /* convert right side variant to string */
2603 if (rightvt != VT_BSTR)
2605 if (rightvt == VT_BOOL)
2607 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2608 V_VT(&bstrvar_right) = VT_BSTR;
2609 if (V_BOOL(right) == TRUE)
2610 V_BSTR(&bstrvar_right) = SysAllocString(str_true);
2611 else
2612 V_BSTR(&bstrvar_right) = SysAllocString(str_false);
2614 /* Fill with empty string for later concat with right side */
2615 else if (rightvt == VT_NULL)
2617 V_VT(&bstrvar_right) = VT_BSTR;
2618 V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
2620 else
2622 hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
2623 if (hres != S_OK) {
2624 VariantClear(&bstrvar_left);
2625 VariantClear(&bstrvar_right);
2626 if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
2627 leftvt == VT_NULL || leftvt == VT_I2 ||
2628 leftvt == VT_I4 || leftvt == VT_R4 ||
2629 leftvt == VT_R8 || leftvt == VT_CY ||
2630 leftvt == VT_DATE || leftvt == VT_BSTR ||
2631 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2632 leftvt == VT_I1 || leftvt == VT_UI1 ||
2633 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2634 leftvt == VT_I8 || leftvt == VT_UI8 ||
2635 leftvt == VT_INT || leftvt == VT_UINT))
2636 return DISP_E_BADVARTYPE;
2637 return hres;
2642 /* Concat the resulting strings together */
2643 if (leftvt == VT_BSTR && rightvt == VT_BSTR)
2644 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2645 else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
2646 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2647 else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
2648 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
2649 else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
2650 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2652 VariantClear(&bstrvar_left);
2653 VariantClear(&bstrvar_right);
2654 return S_OK;
2659 /* Wrapper around VariantChangeTypeEx() which permits changing a
2660 variant with VT_RESERVED flag set. Needed by VarCmp. */
2661 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2662 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2664 HRESULT res;
2665 VARTYPE flags;
2667 flags = V_VT(pvargSrc) & ~VT_TYPEMASK;
2668 V_VT(pvargSrc) &= ~VT_RESERVED;
2669 res = VariantChangeTypeEx(pvargDest,pvargSrc,lcid,wFlags,vt);
2670 V_VT(pvargSrc) |= flags;
2672 return res;
2675 /**********************************************************************
2676 * VarCmp [OLEAUT32.176]
2678 * Compare two variants.
2680 * PARAMS
2681 * left [I] First variant
2682 * right [I] Second variant
2683 * lcid [I] LCID (locale identifier) for the comparison
2684 * flags [I] Flags to be used in the comparison:
2685 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2686 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2688 * RETURNS
2689 * VARCMP_LT: left variant is less than right variant.
2690 * VARCMP_EQ: input variants are equal.
2691 * VARCMP_GT: left variant is greater than right variant.
2692 * VARCMP_NULL: either one of the input variants is NULL.
2693 * Failure: An HRESULT error code indicating the error.
2695 * NOTES
2696 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2697 * UI8 and UINT as input variants. INT is accepted only as left variant.
2699 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2700 * an ERROR variant will trigger an error.
2702 * Both input variants can have VT_RESERVED flag set which is ignored
2703 * unless one and only one of the variants is a BSTR and the other one
2704 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2705 * different meaning:
2706 * - BSTR and other: BSTR is always greater than the other variant.
2707 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2708 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2709 * comparison will take place else the BSTR is always greater.
2710 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2711 * variant is ignored and the return value depends only on the sign
2712 * of the BSTR if it is a number else the BSTR is always greater. A
2713 * positive BSTR is greater, a negative one is smaller than the other
2714 * variant.
2716 * SEE
2717 * VarBstrCmp for the lcid and flags usage.
2719 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2721 VARTYPE lvt, rvt, vt;
2722 VARIANT rv,lv;
2723 DWORD xmask;
2724 HRESULT rc;
2726 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%08x)\n", left, debugstr_VT(left),
2727 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2729 lvt = V_VT(left) & VT_TYPEMASK;
2730 rvt = V_VT(right) & VT_TYPEMASK;
2731 xmask = (1 << lvt) | (1 << rvt);
2733 /* If we have any flag set except VT_RESERVED bail out.
2734 Same for the left input variant type > VT_INT and for the
2735 right input variant type > VT_I8. Yes, VT_INT is only supported
2736 as left variant. Go figure */
2737 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2738 lvt > VT_INT || rvt > VT_I8) {
2739 return DISP_E_BADVARTYPE;
2742 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2743 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2744 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2745 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2746 return DISP_E_TYPEMISMATCH;
2748 /* If both variants are VT_ERROR return VARCMP_EQ */
2749 if (xmask == VTBIT_ERROR)
2750 return VARCMP_EQ;
2751 else if (xmask & VTBIT_ERROR)
2752 return DISP_E_TYPEMISMATCH;
2754 if (xmask & VTBIT_NULL)
2755 return VARCMP_NULL;
2757 VariantInit(&lv);
2758 VariantInit(&rv);
2760 /* Two BSTRs, ignore VT_RESERVED */
2761 if (xmask == VTBIT_BSTR)
2762 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2764 /* A BSTR and an other variant; we have to take care of VT_RESERVED */
2765 if (xmask & VTBIT_BSTR) {
2766 VARIANT *bstrv, *nonbv;
2767 VARTYPE nonbvt;
2768 int swap = 0;
2770 /* Swap the variants so the BSTR is always on the left */
2771 if (lvt == VT_BSTR) {
2772 bstrv = left;
2773 nonbv = right;
2774 nonbvt = rvt;
2775 } else {
2776 swap = 1;
2777 bstrv = right;
2778 nonbv = left;
2779 nonbvt = lvt;
2782 /* BSTR and EMPTY: ignore VT_RESERVED */
2783 if (nonbvt == VT_EMPTY)
2784 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2785 else {
2786 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2787 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2789 if (!breserv && !nreserv)
2790 /* No VT_RESERVED set ==> BSTR always greater */
2791 rc = VARCMP_GT;
2792 else if (breserv && !nreserv) {
2793 /* BSTR has VT_RESERVED set. Do a string comparison */
2794 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2795 if (FAILED(rc))
2796 return rc;
2797 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2798 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2799 /* Non NULL nor empty BSTR */
2800 /* If the BSTR is not a number the BSTR is greater */
2801 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2802 if (FAILED(rc))
2803 rc = VARCMP_GT;
2804 else if (breserv && nreserv)
2805 /* FIXME: This is strange: with both VT_RESERVED set it
2806 looks like the result depends only on the sign of
2807 the BSTR number */
2808 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2809 else
2810 /* Numeric comparison, will be handled below.
2811 VARCMP_NULL used only to break out. */
2812 rc = VARCMP_NULL;
2813 VariantClear(&lv);
2814 VariantClear(&rv);
2815 } else
2816 /* Empty or NULL BSTR */
2817 rc = VARCMP_GT;
2819 /* Fixup the return code if we swapped left and right */
2820 if (swap) {
2821 if (rc == VARCMP_GT)
2822 rc = VARCMP_LT;
2823 else if (rc == VARCMP_LT)
2824 rc = VARCMP_GT;
2826 if (rc != VARCMP_NULL)
2827 return rc;
2830 if (xmask & VTBIT_DECIMAL)
2831 vt = VT_DECIMAL;
2832 else if (xmask & VTBIT_BSTR)
2833 vt = VT_R8;
2834 else if (xmask & VTBIT_R4)
2835 vt = VT_R4;
2836 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2837 vt = VT_R8;
2838 else if (xmask & VTBIT_CY)
2839 vt = VT_CY;
2840 else
2841 /* default to I8 */
2842 vt = VT_I8;
2844 /* Coerce the variants */
2845 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2846 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2847 /* Overflow, change to R8 */
2848 vt = VT_R8;
2849 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2851 if (FAILED(rc))
2852 return rc;
2853 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2854 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2855 /* Overflow, change to R8 */
2856 vt = VT_R8;
2857 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2858 if (FAILED(rc))
2859 return rc;
2860 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2862 if (FAILED(rc))
2863 return rc;
2865 #define _VARCMP(a,b) \
2866 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2868 switch (vt) {
2869 case VT_CY:
2870 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2871 case VT_DECIMAL:
2872 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2873 case VT_I8:
2874 return _VARCMP(V_I8(&lv), V_I8(&rv));
2875 case VT_R4:
2876 return _VARCMP(V_R4(&lv), V_R4(&rv));
2877 case VT_R8:
2878 return _VARCMP(V_R8(&lv), V_R8(&rv));
2879 default:
2880 /* We should never get here */
2881 return E_FAIL;
2883 #undef _VARCMP
2886 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
2888 HRESULT hres;
2889 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
2891 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
2892 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
2893 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
2894 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
2895 NULL, NULL);
2896 } else {
2897 hres = DISP_E_TYPEMISMATCH;
2899 return hres;
2902 /**********************************************************************
2903 * VarAnd [OLEAUT32.142]
2905 * Computes the logical AND of two variants.
2907 * PARAMS
2908 * left [I] First variant
2909 * right [I] Second variant
2910 * result [O] Result variant
2912 * RETURNS
2913 * Success: S_OK.
2914 * Failure: An HRESULT error code indicating the error.
2916 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2918 HRESULT hres = S_OK;
2919 VARTYPE resvt = VT_EMPTY;
2920 VARTYPE leftvt,rightvt;
2921 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2922 VARIANT varLeft, varRight;
2923 VARIANT tempLeft, tempRight;
2925 VariantInit(&varLeft);
2926 VariantInit(&varRight);
2927 VariantInit(&tempLeft);
2928 VariantInit(&tempRight);
2930 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2931 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2933 /* Handle VT_DISPATCH by storing and taking address of returned value */
2934 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2936 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2937 if (FAILED(hres)) goto VarAnd_Exit;
2938 left = &tempLeft;
2940 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2942 hres = VARIANT_FetchDispatchValue(right, &tempRight);
2943 if (FAILED(hres)) goto VarAnd_Exit;
2944 right = &tempRight;
2947 leftvt = V_VT(left)&VT_TYPEMASK;
2948 rightvt = V_VT(right)&VT_TYPEMASK;
2949 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
2950 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2952 if (leftExtraFlags != rightExtraFlags)
2954 hres = DISP_E_BADVARTYPE;
2955 goto VarAnd_Exit;
2957 ExtraFlags = leftExtraFlags;
2959 /* Native VarAnd always returns an error when using extra
2960 * flags or if the variant combination is I8 and INT.
2962 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
2963 (leftvt == VT_INT && rightvt == VT_I8) ||
2964 ExtraFlags != 0)
2966 hres = DISP_E_BADVARTYPE;
2967 goto VarAnd_Exit;
2970 /* Determine return type */
2971 else if (leftvt == VT_I8 || rightvt == VT_I8)
2972 resvt = VT_I8;
2973 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
2974 leftvt == VT_UINT || rightvt == VT_UINT ||
2975 leftvt == VT_INT || rightvt == VT_INT ||
2976 leftvt == VT_UINT || rightvt == VT_UINT ||
2977 leftvt == VT_R4 || rightvt == VT_R4 ||
2978 leftvt == VT_R8 || rightvt == VT_R8 ||
2979 leftvt == VT_CY || rightvt == VT_CY ||
2980 leftvt == VT_DATE || rightvt == VT_DATE ||
2981 leftvt == VT_I1 || rightvt == VT_I1 ||
2982 leftvt == VT_UI2 || rightvt == VT_UI2 ||
2983 leftvt == VT_UI4 || rightvt == VT_UI4 ||
2984 leftvt == VT_UI8 || rightvt == VT_UI8 ||
2985 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
2986 resvt = VT_I4;
2987 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
2988 leftvt == VT_I2 || rightvt == VT_I2 ||
2989 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
2990 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
2991 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
2992 (leftvt == VT_UI1 && rightvt == VT_UI1))
2993 resvt = VT_UI1;
2994 else
2995 resvt = VT_I2;
2996 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
2997 (leftvt == VT_BSTR && rightvt == VT_BSTR))
2998 resvt = VT_BOOL;
2999 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
3000 leftvt == VT_BSTR || rightvt == VT_BSTR)
3001 resvt = VT_NULL;
3002 else
3004 hres = DISP_E_BADVARTYPE;
3005 goto VarAnd_Exit;
3008 if (leftvt == VT_NULL || rightvt == VT_NULL)
3011 * Special cases for when left variant is VT_NULL
3012 * (NULL & 0 = NULL, NULL & value = value)
3014 if (leftvt == VT_NULL)
3016 VARIANT_BOOL b;
3017 switch(rightvt)
3019 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3020 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3021 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3022 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3023 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3024 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3025 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3026 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3027 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3028 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3029 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3030 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3031 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3032 case VT_CY:
3033 if(V_CY(right).int64)
3034 resvt = VT_NULL;
3035 break;
3036 case VT_DECIMAL:
3037 if (DEC_HI32(&V_DECIMAL(right)) ||
3038 DEC_LO64(&V_DECIMAL(right)))
3039 resvt = VT_NULL;
3040 break;
3041 case VT_BSTR:
3042 hres = VarBoolFromStr(V_BSTR(right),
3043 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3044 if (FAILED(hres))
3045 return hres;
3046 else if (b)
3047 V_VT(result) = VT_NULL;
3048 else
3050 V_VT(result) = VT_BOOL;
3051 V_BOOL(result) = b;
3053 goto VarAnd_Exit;
3056 V_VT(result) = resvt;
3057 goto VarAnd_Exit;
3060 hres = VariantCopy(&varLeft, left);
3061 if (FAILED(hres)) goto VarAnd_Exit;
3063 hres = VariantCopy(&varRight, right);
3064 if (FAILED(hres)) goto VarAnd_Exit;
3066 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3067 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3068 else
3070 double d;
3072 if (V_VT(&varLeft) == VT_BSTR &&
3073 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3074 LOCALE_USER_DEFAULT, 0, &d)))
3075 hres = VariantChangeType(&varLeft,&varLeft,
3076 VARIANT_LOCALBOOL, VT_BOOL);
3077 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3078 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3079 if (FAILED(hres)) goto VarAnd_Exit;
3082 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3083 V_VT(&varRight) = VT_I4; /* Don't overflow */
3084 else
3086 double d;
3088 if (V_VT(&varRight) == VT_BSTR &&
3089 FAILED(VarR8FromStr(V_BSTR(&varRight),
3090 LOCALE_USER_DEFAULT, 0, &d)))
3091 hres = VariantChangeType(&varRight, &varRight,
3092 VARIANT_LOCALBOOL, VT_BOOL);
3093 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3094 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3095 if (FAILED(hres)) goto VarAnd_Exit;
3098 V_VT(result) = resvt;
3099 switch(resvt)
3101 case VT_I8:
3102 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3103 break;
3104 case VT_I4:
3105 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3106 break;
3107 case VT_I2:
3108 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3109 break;
3110 case VT_UI1:
3111 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3112 break;
3113 case VT_BOOL:
3114 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3115 break;
3116 default:
3117 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3118 leftvt,rightvt);
3121 VarAnd_Exit:
3122 VariantClear(&varLeft);
3123 VariantClear(&varRight);
3124 VariantClear(&tempLeft);
3125 VariantClear(&tempRight);
3127 return hres;
3130 /**********************************************************************
3131 * VarAdd [OLEAUT32.141]
3133 * Add two variants.
3135 * PARAMS
3136 * left [I] First variant
3137 * right [I] Second variant
3138 * result [O] Result variant
3140 * RETURNS
3141 * Success: S_OK.
3142 * Failure: An HRESULT error code indicating the error.
3144 * NOTES
3145 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3146 * UI8, INT and UINT as input variants.
3148 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3149 * same here.
3151 * FIXME
3152 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3153 * case.
3155 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3157 HRESULT hres;
3158 VARTYPE lvt, rvt, resvt, tvt;
3159 VARIANT lv, rv, tv;
3160 VARIANT tempLeft, tempRight;
3161 double r8res;
3163 /* Variant priority for coercion. Sorted from lowest to highest.
3164 VT_ERROR shows an invalid input variant type. */
3165 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3166 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3167 vt_ERROR };
3168 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3169 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3170 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3171 VT_NULL, VT_ERROR };
3173 /* Mapping for coercion from input variant to priority of result variant. */
3174 static const VARTYPE coerce[] = {
3175 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3176 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3177 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3178 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3179 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3180 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3181 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3182 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3185 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3186 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3187 result);
3189 VariantInit(&lv);
3190 VariantInit(&rv);
3191 VariantInit(&tv);
3192 VariantInit(&tempLeft);
3193 VariantInit(&tempRight);
3195 /* Handle VT_DISPATCH by storing and taking address of returned value */
3196 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3198 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3200 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3201 if (FAILED(hres)) goto end;
3202 left = &tempLeft;
3204 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3206 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3207 if (FAILED(hres)) goto end;
3208 right = &tempRight;
3212 lvt = V_VT(left)&VT_TYPEMASK;
3213 rvt = V_VT(right)&VT_TYPEMASK;
3215 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3216 Same for any input variant type > VT_I8 */
3217 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3218 lvt > VT_I8 || rvt > VT_I8) {
3219 hres = DISP_E_BADVARTYPE;
3220 goto end;
3223 /* Determine the variant type to coerce to. */
3224 if (coerce[lvt] > coerce[rvt]) {
3225 resvt = prio2vt[coerce[lvt]];
3226 tvt = prio2vt[coerce[rvt]];
3227 } else {
3228 resvt = prio2vt[coerce[rvt]];
3229 tvt = prio2vt[coerce[lvt]];
3232 /* Special cases where the result variant type is defined by both
3233 input variants and not only that with the highest priority */
3234 if (resvt == VT_BSTR) {
3235 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3236 resvt = VT_BSTR;
3237 else
3238 resvt = VT_R8;
3240 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3241 resvt = VT_R8;
3243 /* For overflow detection use the biggest compatible type for the
3244 addition */
3245 switch (resvt) {
3246 case VT_ERROR:
3247 hres = DISP_E_BADVARTYPE;
3248 goto end;
3249 case VT_NULL:
3250 hres = S_OK;
3251 V_VT(result) = VT_NULL;
3252 goto end;
3253 case VT_DISPATCH:
3254 FIXME("cannot handle variant type VT_DISPATCH\n");
3255 hres = DISP_E_TYPEMISMATCH;
3256 goto end;
3257 case VT_EMPTY:
3258 resvt = VT_I2;
3259 /* Fall through */
3260 case VT_UI1:
3261 case VT_I2:
3262 case VT_I4:
3263 case VT_I8:
3264 tvt = VT_I8;
3265 break;
3266 case VT_DATE:
3267 case VT_R4:
3268 tvt = VT_R8;
3269 break;
3270 default:
3271 tvt = resvt;
3274 /* Now coerce the variants */
3275 hres = VariantChangeType(&lv, left, 0, tvt);
3276 if (FAILED(hres))
3277 goto end;
3278 hres = VariantChangeType(&rv, right, 0, tvt);
3279 if (FAILED(hres))
3280 goto end;
3282 /* Do the math */
3283 hres = S_OK;
3284 V_VT(result) = resvt;
3285 switch (tvt) {
3286 case VT_DECIMAL:
3287 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3288 &V_DECIMAL(result));
3289 goto end;
3290 case VT_CY:
3291 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3292 goto end;
3293 case VT_BSTR:
3294 /* We do not add those, we concatenate them. */
3295 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3296 goto end;
3297 case VT_I8:
3298 /* Overflow detection */
3299 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3300 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3301 V_VT(result) = VT_R8;
3302 V_R8(result) = r8res;
3303 goto end;
3304 } else {
3305 V_VT(&tv) = tvt;
3306 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3308 break;
3309 case VT_R8:
3310 V_VT(&tv) = tvt;
3311 /* FIXME: overflow detection */
3312 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3313 break;
3314 default:
3315 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3316 break;
3318 if (resvt != tvt) {
3319 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3320 /* Overflow! Change to the vartype with the next higher priority.
3321 With one exception: I4 ==> R8 even if it would fit in I8 */
3322 if (resvt == VT_I4)
3323 resvt = VT_R8;
3324 else
3325 resvt = prio2vt[coerce[resvt] + 1];
3326 hres = VariantChangeType(result, &tv, 0, resvt);
3328 } else
3329 hres = VariantCopy(result, &tv);
3331 end:
3332 if (hres != S_OK) {
3333 V_VT(result) = VT_EMPTY;
3334 V_I4(result) = 0; /* No V_EMPTY */
3336 VariantClear(&lv);
3337 VariantClear(&rv);
3338 VariantClear(&tv);
3339 VariantClear(&tempLeft);
3340 VariantClear(&tempRight);
3341 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3342 return hres;
3345 /**********************************************************************
3346 * VarMul [OLEAUT32.156]
3348 * Multiply two variants.
3350 * PARAMS
3351 * left [I] First variant
3352 * right [I] Second variant
3353 * result [O] Result variant
3355 * RETURNS
3356 * Success: S_OK.
3357 * Failure: An HRESULT error code indicating the error.
3359 * NOTES
3360 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3361 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3363 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3364 * same here.
3366 * FIXME
3367 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3368 * case.
3370 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3372 HRESULT hres;
3373 VARTYPE lvt, rvt, resvt, tvt;
3374 VARIANT lv, rv, tv;
3375 VARIANT tempLeft, tempRight;
3376 double r8res;
3378 /* Variant priority for coercion. Sorted from lowest to highest.
3379 VT_ERROR shows an invalid input variant type. */
3380 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3381 vt_DECIMAL, vt_NULL, vt_ERROR };
3382 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3383 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3384 VT_DECIMAL, VT_NULL, VT_ERROR };
3386 /* Mapping for coercion from input variant to priority of result variant. */
3387 static const VARTYPE coerce[] = {
3388 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3389 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3390 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3391 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3392 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3393 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3394 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3395 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3398 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3399 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3400 result);
3402 VariantInit(&lv);
3403 VariantInit(&rv);
3404 VariantInit(&tv);
3405 VariantInit(&tempLeft);
3406 VariantInit(&tempRight);
3408 /* Handle VT_DISPATCH by storing and taking address of returned value */
3409 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3411 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3412 if (FAILED(hres)) goto end;
3413 left = &tempLeft;
3415 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3417 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3418 if (FAILED(hres)) goto end;
3419 right = &tempRight;
3422 lvt = V_VT(left)&VT_TYPEMASK;
3423 rvt = V_VT(right)&VT_TYPEMASK;
3425 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3426 Same for any input variant type > VT_I8 */
3427 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3428 lvt > VT_I8 || rvt > VT_I8) {
3429 hres = DISP_E_BADVARTYPE;
3430 goto end;
3433 /* Determine the variant type to coerce to. */
3434 if (coerce[lvt] > coerce[rvt]) {
3435 resvt = prio2vt[coerce[lvt]];
3436 tvt = prio2vt[coerce[rvt]];
3437 } else {
3438 resvt = prio2vt[coerce[rvt]];
3439 tvt = prio2vt[coerce[lvt]];
3442 /* Special cases where the result variant type is defined by both
3443 input variants and not only that with the highest priority */
3444 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3445 resvt = VT_R8;
3446 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3447 resvt = VT_I2;
3449 /* For overflow detection use the biggest compatible type for the
3450 multiplication */
3451 switch (resvt) {
3452 case VT_ERROR:
3453 hres = DISP_E_BADVARTYPE;
3454 goto end;
3455 case VT_NULL:
3456 hres = S_OK;
3457 V_VT(result) = VT_NULL;
3458 goto end;
3459 case VT_UI1:
3460 case VT_I2:
3461 case VT_I4:
3462 case VT_I8:
3463 tvt = VT_I8;
3464 break;
3465 case VT_R4:
3466 tvt = VT_R8;
3467 break;
3468 default:
3469 tvt = resvt;
3472 /* Now coerce the variants */
3473 hres = VariantChangeType(&lv, left, 0, tvt);
3474 if (FAILED(hres))
3475 goto end;
3476 hres = VariantChangeType(&rv, right, 0, tvt);
3477 if (FAILED(hres))
3478 goto end;
3480 /* Do the math */
3481 hres = S_OK;
3482 V_VT(&tv) = tvt;
3483 V_VT(result) = resvt;
3484 switch (tvt) {
3485 case VT_DECIMAL:
3486 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3487 &V_DECIMAL(result));
3488 goto end;
3489 case VT_CY:
3490 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3491 goto end;
3492 case VT_I8:
3493 /* Overflow detection */
3494 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3495 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3496 V_VT(result) = VT_R8;
3497 V_R8(result) = r8res;
3498 goto end;
3499 } else
3500 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3501 break;
3502 case VT_R8:
3503 /* FIXME: overflow detection */
3504 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3505 break;
3506 default:
3507 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3508 break;
3510 if (resvt != tvt) {
3511 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3512 /* Overflow! Change to the vartype with the next higher priority.
3513 With one exception: I4 ==> R8 even if it would fit in I8 */
3514 if (resvt == VT_I4)
3515 resvt = VT_R8;
3516 else
3517 resvt = prio2vt[coerce[resvt] + 1];
3519 } else
3520 hres = VariantCopy(result, &tv);
3522 end:
3523 if (hres != S_OK) {
3524 V_VT(result) = VT_EMPTY;
3525 V_I4(result) = 0; /* No V_EMPTY */
3527 VariantClear(&lv);
3528 VariantClear(&rv);
3529 VariantClear(&tv);
3530 VariantClear(&tempLeft);
3531 VariantClear(&tempRight);
3532 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3533 return hres;
3536 /**********************************************************************
3537 * VarDiv [OLEAUT32.143]
3539 * Divides one variant with another.
3541 * PARAMS
3542 * left [I] First variant
3543 * right [I] Second variant
3544 * result [O] Result variant
3546 * RETURNS
3547 * Success: S_OK.
3548 * Failure: An HRESULT error code indicating the error.
3550 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3552 HRESULT hres = S_OK;
3553 VARTYPE resvt = VT_EMPTY;
3554 VARTYPE leftvt,rightvt;
3555 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3556 VARIANT lv,rv;
3557 VARIANT tempLeft, tempRight;
3559 VariantInit(&tempLeft);
3560 VariantInit(&tempRight);
3561 VariantInit(&lv);
3562 VariantInit(&rv);
3564 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3565 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3567 /* Handle VT_DISPATCH by storing and taking address of returned value */
3568 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3570 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3571 if (FAILED(hres)) goto end;
3572 left = &tempLeft;
3574 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3576 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3577 if (FAILED(hres)) goto end;
3578 right = &tempRight;
3581 leftvt = V_VT(left)&VT_TYPEMASK;
3582 rightvt = V_VT(right)&VT_TYPEMASK;
3583 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3584 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3586 if (leftExtraFlags != rightExtraFlags)
3588 hres = DISP_E_BADVARTYPE;
3589 goto end;
3591 ExtraFlags = leftExtraFlags;
3593 /* Native VarDiv always returns an error when using extra flags */
3594 if (ExtraFlags != 0)
3596 hres = DISP_E_BADVARTYPE;
3597 goto end;
3600 /* Determine return type */
3601 if (!(rightvt == VT_EMPTY))
3603 if (leftvt == VT_NULL || rightvt == VT_NULL)
3605 V_VT(result) = VT_NULL;
3606 hres = S_OK;
3607 goto end;
3609 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3610 resvt = VT_DECIMAL;
3611 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3612 leftvt == VT_CY || rightvt == VT_CY ||
3613 leftvt == VT_DATE || rightvt == VT_DATE ||
3614 leftvt == VT_I4 || rightvt == VT_I4 ||
3615 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3616 leftvt == VT_I2 || rightvt == VT_I2 ||
3617 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3618 leftvt == VT_R8 || rightvt == VT_R8 ||
3619 leftvt == VT_UI1 || rightvt == VT_UI1)
3621 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3622 (leftvt == VT_R4 && rightvt == VT_UI1))
3623 resvt = VT_R4;
3624 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3625 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3626 (leftvt == VT_BOOL || leftvt == VT_I2)))
3627 resvt = VT_R4;
3628 else
3629 resvt = VT_R8;
3631 else if (leftvt == VT_R4 || rightvt == VT_R4)
3632 resvt = VT_R4;
3634 else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
3636 V_VT(result) = VT_NULL;
3637 hres = S_OK;
3638 goto end;
3640 else
3642 hres = DISP_E_BADVARTYPE;
3643 goto end;
3646 /* coerce to the result type */
3647 hres = VariantChangeType(&lv, left, 0, resvt);
3648 if (hres != S_OK) goto end;
3650 hres = VariantChangeType(&rv, right, 0, resvt);
3651 if (hres != S_OK) goto end;
3653 /* do the math */
3654 V_VT(result) = resvt;
3655 switch (resvt)
3657 case VT_R4:
3658 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3660 hres = DISP_E_OVERFLOW;
3661 V_VT(result) = VT_EMPTY;
3663 else if (V_R4(&rv) == 0.0)
3665 hres = DISP_E_DIVBYZERO;
3666 V_VT(result) = VT_EMPTY;
3668 else
3669 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3670 break;
3671 case VT_R8:
3672 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3674 hres = DISP_E_OVERFLOW;
3675 V_VT(result) = VT_EMPTY;
3677 else if (V_R8(&rv) == 0.0)
3679 hres = DISP_E_DIVBYZERO;
3680 V_VT(result) = VT_EMPTY;
3682 else
3683 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3684 break;
3685 case VT_DECIMAL:
3686 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3687 break;
3690 end:
3691 VariantClear(&lv);
3692 VariantClear(&rv);
3693 VariantClear(&tempLeft);
3694 VariantClear(&tempRight);
3695 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3696 return hres;
3699 /**********************************************************************
3700 * VarSub [OLEAUT32.159]
3702 * Subtract two variants.
3704 * PARAMS
3705 * left [I] First variant
3706 * right [I] Second variant
3707 * result [O] Result variant
3709 * RETURNS
3710 * Success: S_OK.
3711 * Failure: An HRESULT error code indicating the error.
3713 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3715 HRESULT hres = S_OK;
3716 VARTYPE resvt = VT_EMPTY;
3717 VARTYPE leftvt,rightvt;
3718 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3719 VARIANT lv,rv;
3720 VARIANT tempLeft, tempRight;
3722 VariantInit(&lv);
3723 VariantInit(&rv);
3724 VariantInit(&tempLeft);
3725 VariantInit(&tempRight);
3727 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3728 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3730 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3731 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3732 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3734 if (NULL == V_DISPATCH(left)) {
3735 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3736 hres = DISP_E_BADVARTYPE;
3737 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3738 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3739 hres = DISP_E_BADVARTYPE;
3740 else switch (V_VT(right) & VT_TYPEMASK)
3742 case VT_VARIANT:
3743 case VT_UNKNOWN:
3744 case 15:
3745 case VT_I1:
3746 case VT_UI2:
3747 case VT_UI4:
3748 hres = DISP_E_BADVARTYPE;
3750 if (FAILED(hres)) goto end;
3752 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3753 if (FAILED(hres)) goto end;
3754 left = &tempLeft;
3756 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3757 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3758 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3760 if (NULL == V_DISPATCH(right))
3762 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3763 hres = DISP_E_BADVARTYPE;
3764 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3765 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3766 hres = DISP_E_BADVARTYPE;
3767 else switch (V_VT(left) & VT_TYPEMASK)
3769 case VT_VARIANT:
3770 case VT_UNKNOWN:
3771 case 15:
3772 case VT_I1:
3773 case VT_UI2:
3774 case VT_UI4:
3775 hres = DISP_E_BADVARTYPE;
3777 if (FAILED(hres)) goto end;
3779 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3780 if (FAILED(hres)) goto end;
3781 right = &tempRight;
3784 leftvt = V_VT(left)&VT_TYPEMASK;
3785 rightvt = V_VT(right)&VT_TYPEMASK;
3786 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3787 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3789 if (leftExtraFlags != rightExtraFlags)
3791 hres = DISP_E_BADVARTYPE;
3792 goto end;
3794 ExtraFlags = leftExtraFlags;
3796 /* determine return type and return code */
3797 /* All extra flags produce errors */
3798 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3799 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3800 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3801 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3802 ExtraFlags == VT_VECTOR ||
3803 ExtraFlags == VT_BYREF ||
3804 ExtraFlags == VT_RESERVED)
3806 hres = DISP_E_BADVARTYPE;
3807 goto end;
3809 else if (ExtraFlags >= VT_ARRAY)
3811 hres = DISP_E_TYPEMISMATCH;
3812 goto end;
3814 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3815 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3816 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3817 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3818 leftvt == VT_I1 || rightvt == VT_I1 ||
3819 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3820 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3821 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3822 leftvt == VT_INT || rightvt == VT_INT ||
3823 leftvt == VT_UINT || rightvt == VT_UINT ||
3824 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3825 leftvt == VT_RECORD || rightvt == VT_RECORD)
3827 if (leftvt == VT_RECORD && rightvt == VT_I8)
3828 hres = DISP_E_TYPEMISMATCH;
3829 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3830 hres = DISP_E_TYPEMISMATCH;
3831 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3832 hres = DISP_E_TYPEMISMATCH;
3833 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3834 hres = DISP_E_TYPEMISMATCH;
3835 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3836 hres = DISP_E_BADVARTYPE;
3837 else
3838 hres = DISP_E_BADVARTYPE;
3839 goto end;
3841 /* The following flags/types are invalid for left variant */
3842 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3843 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3844 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3846 hres = DISP_E_BADVARTYPE;
3847 goto end;
3849 /* The following flags/types are invalid for right variant */
3850 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3851 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3852 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3854 hres = DISP_E_BADVARTYPE;
3855 goto end;
3857 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3858 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3859 resvt = VT_NULL;
3860 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3861 leftvt == VT_ERROR || rightvt == VT_ERROR)
3863 hres = DISP_E_TYPEMISMATCH;
3864 goto end;
3866 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3867 resvt = VT_NULL;
3868 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3869 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3870 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3871 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3872 resvt = VT_R8;
3873 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3874 resvt = VT_DECIMAL;
3875 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3876 resvt = VT_DATE;
3877 else if (leftvt == VT_CY || rightvt == VT_CY)
3878 resvt = VT_CY;
3879 else if (leftvt == VT_R8 || rightvt == VT_R8)
3880 resvt = VT_R8;
3881 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3882 resvt = VT_R8;
3883 else if (leftvt == VT_R4 || rightvt == VT_R4)
3885 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3886 leftvt == VT_I8 || rightvt == VT_I8)
3887 resvt = VT_R8;
3888 else
3889 resvt = VT_R4;
3891 else if (leftvt == VT_I8 || rightvt == VT_I8)
3892 resvt = VT_I8;
3893 else if (leftvt == VT_I4 || rightvt == VT_I4)
3894 resvt = VT_I4;
3895 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3896 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3897 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3898 resvt = VT_I2;
3899 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3900 resvt = VT_UI1;
3901 else
3903 hres = DISP_E_TYPEMISMATCH;
3904 goto end;
3907 /* coerce to the result type */
3908 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3909 hres = VariantChangeType(&lv, left, 0, VT_R8);
3910 else
3911 hres = VariantChangeType(&lv, left, 0, resvt);
3912 if (hres != S_OK) goto end;
3913 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3914 hres = VariantChangeType(&rv, right, 0, VT_R8);
3915 else
3916 hres = VariantChangeType(&rv, right, 0, resvt);
3917 if (hres != S_OK) goto end;
3919 /* do the math */
3920 V_VT(result) = resvt;
3921 switch (resvt)
3923 case VT_NULL:
3924 break;
3925 case VT_DATE:
3926 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3927 break;
3928 case VT_CY:
3929 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3930 break;
3931 case VT_R4:
3932 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3933 break;
3934 case VT_I8:
3935 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3936 break;
3937 case VT_I4:
3938 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3939 break;
3940 case VT_I2:
3941 V_I2(result) = V_I2(&lv) - V_I2(&rv);
3942 break;
3943 case VT_I1:
3944 V_I1(result) = V_I1(&lv) - V_I1(&rv);
3945 break;
3946 case VT_UI1:
3947 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
3948 break;
3949 case VT_R8:
3950 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3951 break;
3952 case VT_DECIMAL:
3953 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3954 break;
3957 end:
3958 VariantClear(&lv);
3959 VariantClear(&rv);
3960 VariantClear(&tempLeft);
3961 VariantClear(&tempRight);
3962 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3963 return hres;
3967 /**********************************************************************
3968 * VarOr [OLEAUT32.157]
3970 * Perform a logical or (OR) operation on two variants.
3972 * PARAMS
3973 * pVarLeft [I] First variant
3974 * pVarRight [I] Variant to OR with pVarLeft
3975 * pVarOut [O] Destination for OR result
3977 * RETURNS
3978 * Success: S_OK. pVarOut contains the result of the operation with its type
3979 * taken from the table listed under VarXor().
3980 * Failure: An HRESULT error code indicating the error.
3982 * NOTES
3983 * See the Notes section of VarXor() for further information.
3985 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3987 VARTYPE vt = VT_I4;
3988 VARIANT varLeft, varRight, varStr;
3989 HRESULT hRet;
3990 VARIANT tempLeft, tempRight;
3992 VariantInit(&tempLeft);
3993 VariantInit(&tempRight);
3994 VariantInit(&varLeft);
3995 VariantInit(&varRight);
3996 VariantInit(&varStr);
3998 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3999 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4000 debugstr_VF(pVarRight), pVarOut);
4002 /* Handle VT_DISPATCH by storing and taking address of returned value */
4003 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4005 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4006 if (FAILED(hRet)) goto VarOr_Exit;
4007 pVarLeft = &tempLeft;
4009 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4011 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4012 if (FAILED(hRet)) goto VarOr_Exit;
4013 pVarRight = &tempRight;
4016 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4017 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4018 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
4019 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
4021 hRet = DISP_E_BADVARTYPE;
4022 goto VarOr_Exit;
4025 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
4027 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4029 /* NULL OR Zero is NULL, NULL OR value is value */
4030 if (V_VT(pVarLeft) == VT_NULL)
4031 pVarLeft = pVarRight; /* point to the non-NULL var */
4033 V_VT(pVarOut) = VT_NULL;
4034 V_I4(pVarOut) = 0;
4036 switch (V_VT(pVarLeft))
4038 case VT_DATE: case VT_R8:
4039 if (V_R8(pVarLeft))
4040 goto VarOr_AsEmpty;
4041 hRet = S_OK;
4042 goto VarOr_Exit;
4043 case VT_BOOL:
4044 if (V_BOOL(pVarLeft))
4045 *pVarOut = *pVarLeft;
4046 hRet = S_OK;
4047 goto VarOr_Exit;
4048 case VT_I2: case VT_UI2:
4049 if (V_I2(pVarLeft))
4050 goto VarOr_AsEmpty;
4051 hRet = S_OK;
4052 goto VarOr_Exit;
4053 case VT_I1:
4054 if (V_I1(pVarLeft))
4055 goto VarOr_AsEmpty;
4056 hRet = S_OK;
4057 goto VarOr_Exit;
4058 case VT_UI1:
4059 if (V_UI1(pVarLeft))
4060 *pVarOut = *pVarLeft;
4061 hRet = S_OK;
4062 goto VarOr_Exit;
4063 case VT_R4:
4064 if (V_R4(pVarLeft))
4065 goto VarOr_AsEmpty;
4066 hRet = S_OK;
4067 goto VarOr_Exit;
4068 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4069 if (V_I4(pVarLeft))
4070 goto VarOr_AsEmpty;
4071 hRet = S_OK;
4072 goto VarOr_Exit;
4073 case VT_CY:
4074 if (V_CY(pVarLeft).int64)
4075 goto VarOr_AsEmpty;
4076 hRet = S_OK;
4077 goto VarOr_Exit;
4078 case VT_I8: case VT_UI8:
4079 if (V_I8(pVarLeft))
4080 goto VarOr_AsEmpty;
4081 hRet = S_OK;
4082 goto VarOr_Exit;
4083 case VT_DECIMAL:
4084 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4085 goto VarOr_AsEmpty;
4086 hRet = S_OK;
4087 goto VarOr_Exit;
4088 case VT_BSTR:
4090 VARIANT_BOOL b;
4092 if (!V_BSTR(pVarLeft))
4094 hRet = DISP_E_BADVARTYPE;
4095 goto VarOr_Exit;
4098 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4099 if (SUCCEEDED(hRet) && b)
4101 V_VT(pVarOut) = VT_BOOL;
4102 V_BOOL(pVarOut) = b;
4104 goto VarOr_Exit;
4106 case VT_NULL: case VT_EMPTY:
4107 V_VT(pVarOut) = VT_NULL;
4108 hRet = S_OK;
4109 goto VarOr_Exit;
4110 default:
4111 hRet = DISP_E_BADVARTYPE;
4112 goto VarOr_Exit;
4116 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4118 if (V_VT(pVarLeft) == VT_EMPTY)
4119 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4121 VarOr_AsEmpty:
4122 /* Since one argument is empty (0), OR'ing it with the other simply
4123 * gives the others value (as 0|x => x). So just convert the other
4124 * argument to the required result type.
4126 switch (V_VT(pVarLeft))
4128 case VT_BSTR:
4129 if (!V_BSTR(pVarLeft))
4131 hRet = DISP_E_BADVARTYPE;
4132 goto VarOr_Exit;
4135 hRet = VariantCopy(&varStr, pVarLeft);
4136 if (FAILED(hRet))
4137 goto VarOr_Exit;
4138 pVarLeft = &varStr;
4139 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4140 if (FAILED(hRet))
4141 goto VarOr_Exit;
4142 /* Fall Through ... */
4143 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4144 V_VT(pVarOut) = VT_I2;
4145 break;
4146 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4147 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4148 case VT_INT: case VT_UINT: case VT_UI8:
4149 V_VT(pVarOut) = VT_I4;
4150 break;
4151 case VT_I8:
4152 V_VT(pVarOut) = VT_I8;
4153 break;
4154 default:
4155 hRet = DISP_E_BADVARTYPE;
4156 goto VarOr_Exit;
4158 hRet = VariantCopy(&varLeft, pVarLeft);
4159 if (FAILED(hRet))
4160 goto VarOr_Exit;
4161 pVarLeft = &varLeft;
4162 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4163 goto VarOr_Exit;
4166 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4168 V_VT(pVarOut) = VT_BOOL;
4169 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4170 hRet = S_OK;
4171 goto VarOr_Exit;
4174 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4176 V_VT(pVarOut) = VT_UI1;
4177 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4178 hRet = S_OK;
4179 goto VarOr_Exit;
4182 if (V_VT(pVarLeft) == VT_BSTR)
4184 hRet = VariantCopy(&varStr, pVarLeft);
4185 if (FAILED(hRet))
4186 goto VarOr_Exit;
4187 pVarLeft = &varStr;
4188 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4189 if (FAILED(hRet))
4190 goto VarOr_Exit;
4193 if (V_VT(pVarLeft) == VT_BOOL &&
4194 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4196 vt = VT_BOOL;
4198 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4199 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4200 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4201 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4203 vt = VT_I2;
4205 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4207 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4209 hRet = DISP_E_TYPEMISMATCH;
4210 goto VarOr_Exit;
4212 vt = VT_I8;
4215 hRet = VariantCopy(&varLeft, pVarLeft);
4216 if (FAILED(hRet))
4217 goto VarOr_Exit;
4219 hRet = VariantCopy(&varRight, pVarRight);
4220 if (FAILED(hRet))
4221 goto VarOr_Exit;
4223 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4224 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4225 else
4227 double d;
4229 if (V_VT(&varLeft) == VT_BSTR &&
4230 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4231 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4232 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4233 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4234 if (FAILED(hRet))
4235 goto VarOr_Exit;
4238 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4239 V_VT(&varRight) = VT_I4; /* Don't overflow */
4240 else
4242 double d;
4244 if (V_VT(&varRight) == VT_BSTR &&
4245 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4246 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4247 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4248 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4249 if (FAILED(hRet))
4250 goto VarOr_Exit;
4253 V_VT(pVarOut) = vt;
4254 if (vt == VT_I8)
4256 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4258 else if (vt == VT_I4)
4260 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4262 else
4264 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4267 VarOr_Exit:
4268 VariantClear(&varStr);
4269 VariantClear(&varLeft);
4270 VariantClear(&varRight);
4271 VariantClear(&tempLeft);
4272 VariantClear(&tempRight);
4273 return hRet;
4276 /**********************************************************************
4277 * VarAbs [OLEAUT32.168]
4279 * Convert a variant to its absolute value.
4281 * PARAMS
4282 * pVarIn [I] Source variant
4283 * pVarOut [O] Destination for converted value
4285 * RETURNS
4286 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4287 * Failure: An HRESULT error code indicating the error.
4289 * NOTES
4290 * - This function does not process by-reference variants.
4291 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4292 * according to the following table:
4293 *| Input Type Output Type
4294 *| ---------- -----------
4295 *| VT_BOOL VT_I2
4296 *| VT_BSTR VT_R8
4297 *| (All others) Unchanged
4299 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4301 VARIANT varIn;
4302 HRESULT hRet = S_OK;
4303 VARIANT temp;
4305 VariantInit(&temp);
4307 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4308 debugstr_VF(pVarIn), pVarOut);
4310 /* Handle VT_DISPATCH by storing and taking address of returned value */
4311 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4313 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4314 if (FAILED(hRet)) goto VarAbs_Exit;
4315 pVarIn = &temp;
4318 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4319 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4320 V_VT(pVarIn) == VT_ERROR)
4322 hRet = DISP_E_TYPEMISMATCH;
4323 goto VarAbs_Exit;
4325 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4327 #define ABS_CASE(typ,min) \
4328 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4329 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4330 break
4332 switch (V_VT(pVarIn))
4334 ABS_CASE(I1,I1_MIN);
4335 case VT_BOOL:
4336 V_VT(pVarOut) = VT_I2;
4337 /* BOOL->I2, Fall through ... */
4338 ABS_CASE(I2,I2_MIN);
4339 case VT_INT:
4340 ABS_CASE(I4,I4_MIN);
4341 ABS_CASE(I8,I8_MIN);
4342 ABS_CASE(R4,R4_MIN);
4343 case VT_BSTR:
4344 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4345 if (FAILED(hRet))
4346 break;
4347 V_VT(pVarOut) = VT_R8;
4348 pVarIn = &varIn;
4349 /* Fall through ... */
4350 case VT_DATE:
4351 ABS_CASE(R8,R8_MIN);
4352 case VT_CY:
4353 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4354 break;
4355 case VT_DECIMAL:
4356 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4357 break;
4358 case VT_UI1:
4359 case VT_UI2:
4360 case VT_UINT:
4361 case VT_UI4:
4362 case VT_UI8:
4363 /* No-Op */
4364 break;
4365 case VT_EMPTY:
4366 V_VT(pVarOut) = VT_I2;
4367 case VT_NULL:
4368 V_I2(pVarOut) = 0;
4369 break;
4370 default:
4371 hRet = DISP_E_BADVARTYPE;
4374 VarAbs_Exit:
4375 VariantClear(&temp);
4376 return hRet;
4379 /**********************************************************************
4380 * VarFix [OLEAUT32.169]
4382 * Truncate a variants value to a whole number.
4384 * PARAMS
4385 * pVarIn [I] Source variant
4386 * pVarOut [O] Destination for converted value
4388 * RETURNS
4389 * Success: S_OK. pVarOut contains the converted value.
4390 * Failure: An HRESULT error code indicating the error.
4392 * NOTES
4393 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4394 * according to the following table:
4395 *| Input Type Output Type
4396 *| ---------- -----------
4397 *| VT_BOOL VT_I2
4398 *| VT_EMPTY VT_I2
4399 *| VT_BSTR VT_R8
4400 *| All Others Unchanged
4401 * - The difference between this function and VarInt() is that VarInt() rounds
4402 * negative numbers away from 0, while this function rounds them towards zero.
4404 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4406 HRESULT hRet = S_OK;
4407 VARIANT temp;
4409 VariantInit(&temp);
4411 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4412 debugstr_VF(pVarIn), pVarOut);
4414 /* Handle VT_DISPATCH by storing and taking address of returned value */
4415 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4417 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4418 if (FAILED(hRet)) goto VarFix_Exit;
4419 pVarIn = &temp;
4421 V_VT(pVarOut) = V_VT(pVarIn);
4423 switch (V_VT(pVarIn))
4425 case VT_UI1:
4426 V_UI1(pVarOut) = V_UI1(pVarIn);
4427 break;
4428 case VT_BOOL:
4429 V_VT(pVarOut) = VT_I2;
4430 /* Fall through */
4431 case VT_I2:
4432 V_I2(pVarOut) = V_I2(pVarIn);
4433 break;
4434 case VT_I4:
4435 V_I4(pVarOut) = V_I4(pVarIn);
4436 break;
4437 case VT_I8:
4438 V_I8(pVarOut) = V_I8(pVarIn);
4439 break;
4440 case VT_R4:
4441 if (V_R4(pVarIn) < 0.0f)
4442 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4443 else
4444 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4445 break;
4446 case VT_BSTR:
4447 V_VT(pVarOut) = VT_R8;
4448 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4449 pVarIn = pVarOut;
4450 /* Fall through */
4451 case VT_DATE:
4452 case VT_R8:
4453 if (V_R8(pVarIn) < 0.0)
4454 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4455 else
4456 V_R8(pVarOut) = floor(V_R8(pVarIn));
4457 break;
4458 case VT_CY:
4459 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4460 break;
4461 case VT_DECIMAL:
4462 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4463 break;
4464 case VT_EMPTY:
4465 V_VT(pVarOut) = VT_I2;
4466 V_I2(pVarOut) = 0;
4467 break;
4468 case VT_NULL:
4469 /* No-Op */
4470 break;
4471 default:
4472 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4473 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4474 hRet = DISP_E_BADVARTYPE;
4475 else
4476 hRet = DISP_E_TYPEMISMATCH;
4478 VarFix_Exit:
4479 if (FAILED(hRet))
4480 V_VT(pVarOut) = VT_EMPTY;
4481 VariantClear(&temp);
4483 return hRet;
4486 /**********************************************************************
4487 * VarInt [OLEAUT32.172]
4489 * Truncate a variants value to a whole number.
4491 * PARAMS
4492 * pVarIn [I] Source variant
4493 * pVarOut [O] Destination for converted value
4495 * RETURNS
4496 * Success: S_OK. pVarOut contains the converted value.
4497 * Failure: An HRESULT error code indicating the error.
4499 * NOTES
4500 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4501 * according to the following table:
4502 *| Input Type Output Type
4503 *| ---------- -----------
4504 *| VT_BOOL VT_I2
4505 *| VT_EMPTY VT_I2
4506 *| VT_BSTR VT_R8
4507 *| All Others Unchanged
4508 * - The difference between this function and VarFix() is that VarFix() rounds
4509 * negative numbers towards 0, while this function rounds them away from zero.
4511 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4513 HRESULT hRet = S_OK;
4514 VARIANT temp;
4516 VariantInit(&temp);
4518 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4519 debugstr_VF(pVarIn), pVarOut);
4521 /* Handle VT_DISPATCH by storing and taking address of returned value */
4522 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4524 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4525 if (FAILED(hRet)) goto VarInt_Exit;
4526 pVarIn = &temp;
4528 V_VT(pVarOut) = V_VT(pVarIn);
4530 switch (V_VT(pVarIn))
4532 case VT_R4:
4533 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4534 break;
4535 case VT_BSTR:
4536 V_VT(pVarOut) = VT_R8;
4537 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4538 pVarIn = pVarOut;
4539 /* Fall through */
4540 case VT_DATE:
4541 case VT_R8:
4542 V_R8(pVarOut) = floor(V_R8(pVarIn));
4543 break;
4544 case VT_CY:
4545 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4546 break;
4547 case VT_DECIMAL:
4548 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4549 break;
4550 default:
4551 hRet = VarFix(pVarIn, pVarOut);
4553 VarInt_Exit:
4554 VariantClear(&temp);
4556 return hRet;
4559 /**********************************************************************
4560 * VarXor [OLEAUT32.167]
4562 * Perform a logical exclusive-or (XOR) operation on two variants.
4564 * PARAMS
4565 * pVarLeft [I] First variant
4566 * pVarRight [I] Variant to XOR with pVarLeft
4567 * pVarOut [O] Destination for XOR result
4569 * RETURNS
4570 * Success: S_OK. pVarOut contains the result of the operation with its type
4571 * taken from the table below).
4572 * Failure: An HRESULT error code indicating the error.
4574 * NOTES
4575 * - Neither pVarLeft or pVarRight are modified by this function.
4576 * - This function does not process by-reference variants.
4577 * - Input types of VT_BSTR may be numeric strings or boolean text.
4578 * - The type of result stored in pVarOut depends on the types of pVarLeft
4579 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4580 * or VT_NULL if the function succeeds.
4581 * - Type promotion is inconsistent and as a result certain combinations of
4582 * values will return DISP_E_OVERFLOW even when they could be represented.
4583 * This matches the behaviour of native oleaut32.
4585 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4587 VARTYPE vt;
4588 VARIANT varLeft, varRight;
4589 VARIANT tempLeft, tempRight;
4590 double d;
4591 HRESULT hRet;
4593 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4594 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4595 debugstr_VF(pVarRight), pVarOut);
4597 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4598 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4599 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4600 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4601 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4602 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4603 return DISP_E_BADVARTYPE;
4605 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4607 /* NULL XOR anything valid is NULL */
4608 V_VT(pVarOut) = VT_NULL;
4609 return S_OK;
4612 VariantInit(&tempLeft);
4613 VariantInit(&tempRight);
4615 /* Handle VT_DISPATCH by storing and taking address of returned value */
4616 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4618 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4619 if (FAILED(hRet)) goto VarXor_Exit;
4620 pVarLeft = &tempLeft;
4622 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4624 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4625 if (FAILED(hRet)) goto VarXor_Exit;
4626 pVarRight = &tempRight;
4629 /* Copy our inputs so we don't disturb anything */
4630 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4632 hRet = VariantCopy(&varLeft, pVarLeft);
4633 if (FAILED(hRet))
4634 goto VarXor_Exit;
4636 hRet = VariantCopy(&varRight, pVarRight);
4637 if (FAILED(hRet))
4638 goto VarXor_Exit;
4640 /* Try any strings first as numbers, then as VT_BOOL */
4641 if (V_VT(&varLeft) == VT_BSTR)
4643 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4644 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4645 FAILED(hRet) ? VT_BOOL : VT_I4);
4646 if (FAILED(hRet))
4647 goto VarXor_Exit;
4650 if (V_VT(&varRight) == VT_BSTR)
4652 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4653 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4654 FAILED(hRet) ? VT_BOOL : VT_I4);
4655 if (FAILED(hRet))
4656 goto VarXor_Exit;
4659 /* Determine the result type */
4660 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4662 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4664 hRet = DISP_E_TYPEMISMATCH;
4665 goto VarXor_Exit;
4667 vt = VT_I8;
4669 else
4671 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4673 case (VT_BOOL << 16) | VT_BOOL:
4674 vt = VT_BOOL;
4675 break;
4676 case (VT_UI1 << 16) | VT_UI1:
4677 vt = VT_UI1;
4678 break;
4679 case (VT_EMPTY << 16) | VT_EMPTY:
4680 case (VT_EMPTY << 16) | VT_UI1:
4681 case (VT_EMPTY << 16) | VT_I2:
4682 case (VT_EMPTY << 16) | VT_BOOL:
4683 case (VT_UI1 << 16) | VT_EMPTY:
4684 case (VT_UI1 << 16) | VT_I2:
4685 case (VT_UI1 << 16) | VT_BOOL:
4686 case (VT_I2 << 16) | VT_EMPTY:
4687 case (VT_I2 << 16) | VT_UI1:
4688 case (VT_I2 << 16) | VT_I2:
4689 case (VT_I2 << 16) | VT_BOOL:
4690 case (VT_BOOL << 16) | VT_EMPTY:
4691 case (VT_BOOL << 16) | VT_UI1:
4692 case (VT_BOOL << 16) | VT_I2:
4693 vt = VT_I2;
4694 break;
4695 default:
4696 vt = VT_I4;
4697 break;
4701 /* VT_UI4 does not overflow */
4702 if (vt != VT_I8)
4704 if (V_VT(&varLeft) == VT_UI4)
4705 V_VT(&varLeft) = VT_I4;
4706 if (V_VT(&varRight) == VT_UI4)
4707 V_VT(&varRight) = VT_I4;
4710 /* Convert our input copies to the result type */
4711 if (V_VT(&varLeft) != vt)
4712 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4713 if (FAILED(hRet))
4714 goto VarXor_Exit;
4716 if (V_VT(&varRight) != vt)
4717 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4718 if (FAILED(hRet))
4719 goto VarXor_Exit;
4721 V_VT(pVarOut) = vt;
4723 /* Calculate the result */
4724 switch (vt)
4726 case VT_I8:
4727 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4728 break;
4729 case VT_I4:
4730 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4731 break;
4732 case VT_BOOL:
4733 case VT_I2:
4734 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4735 break;
4736 case VT_UI1:
4737 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4738 break;
4741 VarXor_Exit:
4742 VariantClear(&varLeft);
4743 VariantClear(&varRight);
4744 VariantClear(&tempLeft);
4745 VariantClear(&tempRight);
4746 return hRet;
4749 /**********************************************************************
4750 * VarEqv [OLEAUT32.172]
4752 * Determine if two variants contain the same value.
4754 * PARAMS
4755 * pVarLeft [I] First variant to compare
4756 * pVarRight [I] Variant to compare to pVarLeft
4757 * pVarOut [O] Destination for comparison result
4759 * RETURNS
4760 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4761 * if equivalent or non-zero otherwise.
4762 * Failure: An HRESULT error code indicating the error.
4764 * NOTES
4765 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4766 * the result.
4768 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4770 HRESULT hRet;
4772 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4773 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4774 debugstr_VF(pVarRight), pVarOut);
4776 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4777 if (SUCCEEDED(hRet))
4779 if (V_VT(pVarOut) == VT_I8)
4780 V_I8(pVarOut) = ~V_I8(pVarOut);
4781 else
4782 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4784 return hRet;
4787 /**********************************************************************
4788 * VarNeg [OLEAUT32.173]
4790 * Negate the value of a variant.
4792 * PARAMS
4793 * pVarIn [I] Source variant
4794 * pVarOut [O] Destination for converted value
4796 * RETURNS
4797 * Success: S_OK. pVarOut contains the converted value.
4798 * Failure: An HRESULT error code indicating the error.
4800 * NOTES
4801 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4802 * according to the following table:
4803 *| Input Type Output Type
4804 *| ---------- -----------
4805 *| VT_EMPTY VT_I2
4806 *| VT_UI1 VT_I2
4807 *| VT_BOOL VT_I2
4808 *| VT_BSTR VT_R8
4809 *| All Others Unchanged (unless promoted)
4810 * - Where the negated value of a variant does not fit in its base type, the type
4811 * is promoted according to the following table:
4812 *| Input Type Promoted To
4813 *| ---------- -----------
4814 *| VT_I2 VT_I4
4815 *| VT_I4 VT_R8
4816 *| VT_I8 VT_R8
4817 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4818 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4819 * for types which are not valid. Since this is in contravention of the
4820 * meaning of those error codes and unlikely to be relied on by applications,
4821 * this implementation returns errors consistent with the other high level
4822 * variant math functions.
4824 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4826 HRESULT hRet = S_OK;
4827 VARIANT temp;
4829 VariantInit(&temp);
4831 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4832 debugstr_VF(pVarIn), pVarOut);
4834 /* Handle VT_DISPATCH by storing and taking address of returned value */
4835 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4837 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4838 if (FAILED(hRet)) goto VarNeg_Exit;
4839 pVarIn = &temp;
4841 V_VT(pVarOut) = V_VT(pVarIn);
4843 switch (V_VT(pVarIn))
4845 case VT_UI1:
4846 V_VT(pVarOut) = VT_I2;
4847 V_I2(pVarOut) = -V_UI1(pVarIn);
4848 break;
4849 case VT_BOOL:
4850 V_VT(pVarOut) = VT_I2;
4851 /* Fall through */
4852 case VT_I2:
4853 if (V_I2(pVarIn) == I2_MIN)
4855 V_VT(pVarOut) = VT_I4;
4856 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4858 else
4859 V_I2(pVarOut) = -V_I2(pVarIn);
4860 break;
4861 case VT_I4:
4862 if (V_I4(pVarIn) == I4_MIN)
4864 V_VT(pVarOut) = VT_R8;
4865 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4867 else
4868 V_I4(pVarOut) = -V_I4(pVarIn);
4869 break;
4870 case VT_I8:
4871 if (V_I8(pVarIn) == I8_MIN)
4873 V_VT(pVarOut) = VT_R8;
4874 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4875 V_R8(pVarOut) *= -1.0;
4877 else
4878 V_I8(pVarOut) = -V_I8(pVarIn);
4879 break;
4880 case VT_R4:
4881 V_R4(pVarOut) = -V_R4(pVarIn);
4882 break;
4883 case VT_DATE:
4884 case VT_R8:
4885 V_R8(pVarOut) = -V_R8(pVarIn);
4886 break;
4887 case VT_CY:
4888 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4889 break;
4890 case VT_DECIMAL:
4891 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4892 break;
4893 case VT_BSTR:
4894 V_VT(pVarOut) = VT_R8;
4895 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4896 V_R8(pVarOut) = -V_R8(pVarOut);
4897 break;
4898 case VT_EMPTY:
4899 V_VT(pVarOut) = VT_I2;
4900 V_I2(pVarOut) = 0;
4901 break;
4902 case VT_NULL:
4903 /* No-Op */
4904 break;
4905 default:
4906 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4907 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4908 hRet = DISP_E_BADVARTYPE;
4909 else
4910 hRet = DISP_E_TYPEMISMATCH;
4912 VarNeg_Exit:
4913 if (FAILED(hRet))
4914 V_VT(pVarOut) = VT_EMPTY;
4915 VariantClear(&temp);
4917 return hRet;
4920 /**********************************************************************
4921 * VarNot [OLEAUT32.174]
4923 * Perform a not operation on a variant.
4925 * PARAMS
4926 * pVarIn [I] Source variant
4927 * pVarOut [O] Destination for converted value
4929 * RETURNS
4930 * Success: S_OK. pVarOut contains the converted value.
4931 * Failure: An HRESULT error code indicating the error.
4933 * NOTES
4934 * - Strictly speaking, this function performs a bitwise ones complement
4935 * on the variants value (after possibly converting to VT_I4, see below).
4936 * This only behaves like a boolean not operation if the value in
4937 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4938 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4939 * before calling this function.
4940 * - This function does not process by-reference variants.
4941 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4942 * according to the following table:
4943 *| Input Type Output Type
4944 *| ---------- -----------
4945 *| VT_EMPTY VT_I2
4946 *| VT_R4 VT_I4
4947 *| VT_R8 VT_I4
4948 *| VT_BSTR VT_I4
4949 *| VT_DECIMAL VT_I4
4950 *| VT_CY VT_I4
4951 *| (All others) Unchanged
4953 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4955 VARIANT varIn;
4956 HRESULT hRet = S_OK;
4957 VARIANT temp;
4959 VariantInit(&temp);
4961 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4962 debugstr_VF(pVarIn), pVarOut);
4964 /* Handle VT_DISPATCH by storing and taking address of returned value */
4965 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4967 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4968 if (FAILED(hRet)) goto VarNot_Exit;
4969 pVarIn = &temp;
4972 V_VT(pVarOut) = V_VT(pVarIn);
4974 switch (V_VT(pVarIn))
4976 case VT_I1:
4977 V_I4(pVarOut) = ~V_I1(pVarIn);
4978 V_VT(pVarOut) = VT_I4;
4979 break;
4980 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4981 case VT_BOOL:
4982 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4983 case VT_UI2:
4984 V_I4(pVarOut) = ~V_UI2(pVarIn);
4985 V_VT(pVarOut) = VT_I4;
4986 break;
4987 case VT_DECIMAL:
4988 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4989 if (FAILED(hRet))
4990 break;
4991 pVarIn = &varIn;
4992 /* Fall through ... */
4993 case VT_INT:
4994 V_VT(pVarOut) = VT_I4;
4995 /* Fall through ... */
4996 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4997 case VT_UINT:
4998 case VT_UI4:
4999 V_I4(pVarOut) = ~V_UI4(pVarIn);
5000 V_VT(pVarOut) = VT_I4;
5001 break;
5002 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
5003 case VT_UI8:
5004 V_I4(pVarOut) = ~V_UI8(pVarIn);
5005 V_VT(pVarOut) = VT_I4;
5006 break;
5007 case VT_R4:
5008 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
5009 V_I4(pVarOut) = ~V_I4(pVarOut);
5010 V_VT(pVarOut) = VT_I4;
5011 break;
5012 case VT_BSTR:
5013 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5014 if (FAILED(hRet))
5015 break;
5016 pVarIn = &varIn;
5017 /* Fall through ... */
5018 case VT_DATE:
5019 case VT_R8:
5020 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
5021 V_I4(pVarOut) = ~V_I4(pVarOut);
5022 V_VT(pVarOut) = VT_I4;
5023 break;
5024 case VT_CY:
5025 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
5026 V_I4(pVarOut) = ~V_I4(pVarOut);
5027 V_VT(pVarOut) = VT_I4;
5028 break;
5029 case VT_EMPTY:
5030 V_I2(pVarOut) = ~0;
5031 V_VT(pVarOut) = VT_I2;
5032 break;
5033 case VT_NULL:
5034 /* No-Op */
5035 break;
5036 default:
5037 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5038 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5039 hRet = DISP_E_BADVARTYPE;
5040 else
5041 hRet = DISP_E_TYPEMISMATCH;
5043 VarNot_Exit:
5044 if (FAILED(hRet))
5045 V_VT(pVarOut) = VT_EMPTY;
5046 VariantClear(&temp);
5048 return hRet;
5051 /**********************************************************************
5052 * VarRound [OLEAUT32.175]
5054 * Perform a round operation on a variant.
5056 * PARAMS
5057 * pVarIn [I] Source variant
5058 * deci [I] Number of decimals to round to
5059 * pVarOut [O] Destination for converted value
5061 * RETURNS
5062 * Success: S_OK. pVarOut contains the converted value.
5063 * Failure: An HRESULT error code indicating the error.
5065 * NOTES
5066 * - Floating point values are rounded to the desired number of decimals.
5067 * - Some integer types are just copied to the return variable.
5068 * - Some other integer types are not handled and fail.
5070 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5072 VARIANT varIn;
5073 HRESULT hRet = S_OK;
5074 float factor;
5075 VARIANT temp;
5077 VariantInit(&temp);
5079 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
5081 /* Handle VT_DISPATCH by storing and taking address of returned value */
5082 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5084 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5085 if (FAILED(hRet)) goto VarRound_Exit;
5086 pVarIn = &temp;
5089 switch (V_VT(pVarIn))
5091 /* cases that fail on windows */
5092 case VT_I1:
5093 case VT_I8:
5094 case VT_UI2:
5095 case VT_UI4:
5096 hRet = DISP_E_BADVARTYPE;
5097 break;
5099 /* cases just copying in to out */
5100 case VT_UI1:
5101 V_VT(pVarOut) = V_VT(pVarIn);
5102 V_UI1(pVarOut) = V_UI1(pVarIn);
5103 break;
5104 case VT_I2:
5105 V_VT(pVarOut) = V_VT(pVarIn);
5106 V_I2(pVarOut) = V_I2(pVarIn);
5107 break;
5108 case VT_I4:
5109 V_VT(pVarOut) = V_VT(pVarIn);
5110 V_I4(pVarOut) = V_I4(pVarIn);
5111 break;
5112 case VT_NULL:
5113 V_VT(pVarOut) = V_VT(pVarIn);
5114 /* value unchanged */
5115 break;
5117 /* cases that change type */
5118 case VT_EMPTY:
5119 V_VT(pVarOut) = VT_I2;
5120 V_I2(pVarOut) = 0;
5121 break;
5122 case VT_BOOL:
5123 V_VT(pVarOut) = VT_I2;
5124 V_I2(pVarOut) = V_BOOL(pVarIn);
5125 break;
5126 case VT_BSTR:
5127 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5128 if (FAILED(hRet))
5129 break;
5130 V_VT(&varIn)=VT_R8;
5131 pVarIn = &varIn;
5132 /* Fall through ... */
5134 /* cases we need to do math */
5135 case VT_R8:
5136 if (V_R8(pVarIn)>0) {
5137 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5138 } else {
5139 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5141 V_VT(pVarOut) = V_VT(pVarIn);
5142 break;
5143 case VT_R4:
5144 if (V_R4(pVarIn)>0) {
5145 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5146 } else {
5147 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5149 V_VT(pVarOut) = V_VT(pVarIn);
5150 break;
5151 case VT_DATE:
5152 if (V_DATE(pVarIn)>0) {
5153 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5154 } else {
5155 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5157 V_VT(pVarOut) = V_VT(pVarIn);
5158 break;
5159 case VT_CY:
5160 if (deci>3)
5161 factor=1;
5162 else
5163 factor=pow(10, 4-deci);
5165 if (V_CY(pVarIn).int64>0) {
5166 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5167 } else {
5168 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5170 V_VT(pVarOut) = V_VT(pVarIn);
5171 break;
5173 /* cases we don't know yet */
5174 default:
5175 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5176 V_VT(pVarIn) & VT_TYPEMASK, deci);
5177 hRet = DISP_E_BADVARTYPE;
5179 VarRound_Exit:
5180 if (FAILED(hRet))
5181 V_VT(pVarOut) = VT_EMPTY;
5182 VariantClear(&temp);
5184 TRACE("returning 0x%08x (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
5185 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
5186 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
5188 return hRet;
5191 /**********************************************************************
5192 * VarIdiv [OLEAUT32.153]
5194 * Converts input variants to integers and divides them.
5196 * PARAMS
5197 * left [I] Left hand variant
5198 * right [I] Right hand variant
5199 * result [O] Destination for quotient
5201 * RETURNS
5202 * Success: S_OK. result contains the quotient.
5203 * Failure: An HRESULT error code indicating the error.
5205 * NOTES
5206 * If either expression is null, null is returned, as per MSDN
5208 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5210 HRESULT hres = S_OK;
5211 VARTYPE resvt = VT_EMPTY;
5212 VARTYPE leftvt,rightvt;
5213 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5214 VARIANT lv,rv;
5215 VARIANT tempLeft, tempRight;
5217 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5218 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5220 VariantInit(&lv);
5221 VariantInit(&rv);
5222 VariantInit(&tempLeft);
5223 VariantInit(&tempRight);
5225 leftvt = V_VT(left)&VT_TYPEMASK;
5226 rightvt = V_VT(right)&VT_TYPEMASK;
5227 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5228 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5230 if (leftExtraFlags != rightExtraFlags)
5232 hres = DISP_E_BADVARTYPE;
5233 goto end;
5235 ExtraFlags = leftExtraFlags;
5237 /* Native VarIdiv always returns an error when using extra
5238 * flags or if the variant combination is I8 and INT.
5240 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5241 (leftvt == VT_INT && rightvt == VT_I8) ||
5242 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5243 ExtraFlags != 0)
5245 hres = DISP_E_BADVARTYPE;
5246 goto end;
5249 /* Determine variant type */
5250 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5252 V_VT(result) = VT_NULL;
5253 hres = S_OK;
5254 goto end;
5256 else if (leftvt == VT_I8 || rightvt == VT_I8)
5257 resvt = VT_I8;
5258 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5259 leftvt == VT_INT || rightvt == VT_INT ||
5260 leftvt == VT_UINT || rightvt == VT_UINT ||
5261 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5262 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5263 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5264 leftvt == VT_I1 || rightvt == VT_I1 ||
5265 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5266 leftvt == VT_DATE || rightvt == VT_DATE ||
5267 leftvt == VT_CY || rightvt == VT_CY ||
5268 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5269 leftvt == VT_R8 || rightvt == VT_R8 ||
5270 leftvt == VT_R4 || rightvt == VT_R4)
5271 resvt = VT_I4;
5272 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5273 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5274 leftvt == VT_EMPTY)
5275 resvt = VT_I2;
5276 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5277 resvt = VT_UI1;
5278 else
5280 hres = DISP_E_BADVARTYPE;
5281 goto end;
5284 /* coerce to the result type */
5285 hres = VariantChangeType(&lv, left, 0, resvt);
5286 if (hres != S_OK) goto end;
5287 hres = VariantChangeType(&rv, right, 0, resvt);
5288 if (hres != S_OK) goto end;
5290 /* do the math */
5291 V_VT(result) = resvt;
5292 switch (resvt)
5294 case VT_UI1:
5295 if (V_UI1(&rv) == 0)
5297 hres = DISP_E_DIVBYZERO;
5298 V_VT(result) = VT_EMPTY;
5300 else
5301 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5302 break;
5303 case VT_I2:
5304 if (V_I2(&rv) == 0)
5306 hres = DISP_E_DIVBYZERO;
5307 V_VT(result) = VT_EMPTY;
5309 else
5310 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5311 break;
5312 case VT_I4:
5313 if (V_I4(&rv) == 0)
5315 hres = DISP_E_DIVBYZERO;
5316 V_VT(result) = VT_EMPTY;
5318 else
5319 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5320 break;
5321 case VT_I8:
5322 if (V_I8(&rv) == 0)
5324 hres = DISP_E_DIVBYZERO;
5325 V_VT(result) = VT_EMPTY;
5327 else
5328 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5329 break;
5330 default:
5331 FIXME("Couldn't integer divide variant types %d,%d\n",
5332 leftvt,rightvt);
5335 end:
5336 VariantClear(&lv);
5337 VariantClear(&rv);
5338 VariantClear(&tempLeft);
5339 VariantClear(&tempRight);
5341 return hres;
5345 /**********************************************************************
5346 * VarMod [OLEAUT32.155]
5348 * Perform the modulus operation of the right hand variant on the left
5350 * PARAMS
5351 * left [I] Left hand variant
5352 * right [I] Right hand variant
5353 * result [O] Destination for converted value
5355 * RETURNS
5356 * Success: S_OK. result contains the remainder.
5357 * Failure: An HRESULT error code indicating the error.
5359 * NOTE:
5360 * If an error occurs the type of result will be modified but the value will not be.
5361 * Doesn't support arrays or any special flags yet.
5363 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5365 BOOL lOk = TRUE;
5366 BOOL rOk = TRUE;
5367 HRESULT rc = E_FAIL;
5368 int resT = 0;
5369 VARIANT lv,rv;
5370 VARIANT tempLeft, tempRight;
5372 VariantInit(&tempLeft);
5373 VariantInit(&tempRight);
5374 VariantInit(&lv);
5375 VariantInit(&rv);
5377 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5378 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5380 /* Handle VT_DISPATCH by storing and taking address of returned value */
5381 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5383 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5384 if (FAILED(rc)) goto end;
5385 left = &tempLeft;
5387 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5389 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5390 if (FAILED(rc)) goto end;
5391 right = &tempRight;
5394 /* check for invalid inputs */
5395 lOk = TRUE;
5396 switch (V_VT(left) & VT_TYPEMASK) {
5397 case VT_BOOL :
5398 case VT_I1 :
5399 case VT_I2 :
5400 case VT_I4 :
5401 case VT_I8 :
5402 case VT_INT :
5403 case VT_UI1 :
5404 case VT_UI2 :
5405 case VT_UI4 :
5406 case VT_UI8 :
5407 case VT_UINT :
5408 case VT_R4 :
5409 case VT_R8 :
5410 case VT_CY :
5411 case VT_EMPTY:
5412 case VT_DATE :
5413 case VT_BSTR :
5414 case VT_DECIMAL:
5415 break;
5416 case VT_VARIANT:
5417 case VT_UNKNOWN:
5418 V_VT(result) = VT_EMPTY;
5419 rc = DISP_E_TYPEMISMATCH;
5420 goto end;
5421 case VT_ERROR:
5422 rc = DISP_E_TYPEMISMATCH;
5423 goto end;
5424 case VT_RECORD:
5425 V_VT(result) = VT_EMPTY;
5426 rc = DISP_E_TYPEMISMATCH;
5427 goto end;
5428 case VT_NULL:
5429 break;
5430 default:
5431 V_VT(result) = VT_EMPTY;
5432 rc = DISP_E_BADVARTYPE;
5433 goto end;
5437 rOk = TRUE;
5438 switch (V_VT(right) & VT_TYPEMASK) {
5439 case VT_BOOL :
5440 case VT_I1 :
5441 case VT_I2 :
5442 case VT_I4 :
5443 case VT_I8 :
5444 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5446 V_VT(result) = VT_EMPTY;
5447 rc = DISP_E_TYPEMISMATCH;
5448 goto end;
5450 case VT_INT :
5451 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5453 V_VT(result) = VT_EMPTY;
5454 rc = DISP_E_TYPEMISMATCH;
5455 goto end;
5457 case VT_UI1 :
5458 case VT_UI2 :
5459 case VT_UI4 :
5460 case VT_UI8 :
5461 case VT_UINT :
5462 case VT_R4 :
5463 case VT_R8 :
5464 case VT_CY :
5465 if(V_VT(left) == VT_EMPTY)
5467 V_VT(result) = VT_I4;
5468 rc = S_OK;
5469 goto end;
5471 case VT_EMPTY:
5472 case VT_DATE :
5473 case VT_DECIMAL:
5474 if(V_VT(left) == VT_ERROR)
5476 V_VT(result) = VT_EMPTY;
5477 rc = DISP_E_TYPEMISMATCH;
5478 goto end;
5480 case VT_BSTR:
5481 if(V_VT(left) == VT_NULL)
5483 V_VT(result) = VT_NULL;
5484 rc = S_OK;
5485 goto end;
5487 break;
5489 case VT_VOID:
5490 V_VT(result) = VT_EMPTY;
5491 rc = DISP_E_BADVARTYPE;
5492 goto end;
5493 case VT_NULL:
5494 if(V_VT(left) == VT_VOID)
5496 V_VT(result) = VT_EMPTY;
5497 rc = DISP_E_BADVARTYPE;
5498 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5499 lOk)
5501 V_VT(result) = VT_NULL;
5502 rc = S_OK;
5503 } else
5505 V_VT(result) = VT_NULL;
5506 rc = DISP_E_BADVARTYPE;
5508 goto end;
5509 case VT_VARIANT:
5510 case VT_UNKNOWN:
5511 V_VT(result) = VT_EMPTY;
5512 rc = DISP_E_TYPEMISMATCH;
5513 goto end;
5514 case VT_ERROR:
5515 rc = DISP_E_TYPEMISMATCH;
5516 goto end;
5517 case VT_RECORD:
5518 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5520 V_VT(result) = VT_EMPTY;
5521 rc = DISP_E_BADVARTYPE;
5522 } else
5524 V_VT(result) = VT_EMPTY;
5525 rc = DISP_E_TYPEMISMATCH;
5527 goto end;
5528 default:
5529 V_VT(result) = VT_EMPTY;
5530 rc = DISP_E_BADVARTYPE;
5531 goto end;
5534 /* determine the result type */
5535 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5536 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5537 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5538 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5539 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5540 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5541 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5542 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5543 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5544 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5545 else resT = VT_I4; /* most outputs are I4 */
5547 /* convert to I8 for the modulo */
5548 rc = VariantChangeType(&lv, left, 0, VT_I8);
5549 if(FAILED(rc))
5551 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5552 goto end;
5555 rc = VariantChangeType(&rv, right, 0, VT_I8);
5556 if(FAILED(rc))
5558 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5559 goto end;
5562 /* if right is zero set VT_EMPTY and return divide by zero */
5563 if(V_I8(&rv) == 0)
5565 V_VT(result) = VT_EMPTY;
5566 rc = DISP_E_DIVBYZERO;
5567 goto end;
5570 /* perform the modulo operation */
5571 V_VT(result) = VT_I8;
5572 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5574 TRACE("V_I8(left) == %ld, V_I8(right) == %ld, V_I8(result) == %ld\n", (long)V_I8(&lv), (long)V_I8(&rv), (long)V_I8(result));
5576 /* convert left and right to the destination type */
5577 rc = VariantChangeType(result, result, 0, resT);
5578 if(FAILED(rc))
5580 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5581 /* fall to end of function */
5584 end:
5585 VariantClear(&lv);
5586 VariantClear(&rv);
5587 VariantClear(&tempLeft);
5588 VariantClear(&tempRight);
5589 return rc;
5592 /**********************************************************************
5593 * VarPow [OLEAUT32.158]
5595 * Computes the power of one variant to another variant.
5597 * PARAMS
5598 * left [I] First variant
5599 * right [I] Second variant
5600 * result [O] Result variant
5602 * RETURNS
5603 * Success: S_OK.
5604 * Failure: An HRESULT error code indicating the error.
5606 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5608 HRESULT hr = S_OK;
5609 VARIANT dl,dr;
5610 VARTYPE resvt = VT_EMPTY;
5611 VARTYPE leftvt,rightvt;
5612 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5613 VARIANT tempLeft, tempRight;
5615 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
5616 right, debugstr_VT(right), debugstr_VF(right), result);
5618 VariantInit(&dl);
5619 VariantInit(&dr);
5620 VariantInit(&tempLeft);
5621 VariantInit(&tempRight);
5623 /* Handle VT_DISPATCH by storing and taking address of returned value */
5624 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5626 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5627 if (FAILED(hr)) goto end;
5628 left = &tempLeft;
5630 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5632 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5633 if (FAILED(hr)) goto end;
5634 right = &tempRight;
5637 leftvt = V_VT(left)&VT_TYPEMASK;
5638 rightvt = V_VT(right)&VT_TYPEMASK;
5639 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5640 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5642 if (leftExtraFlags != rightExtraFlags)
5644 hr = DISP_E_BADVARTYPE;
5645 goto end;
5647 ExtraFlags = leftExtraFlags;
5649 /* Native VarPow always returns an error when using extra flags */
5650 if (ExtraFlags != 0)
5652 hr = DISP_E_BADVARTYPE;
5653 goto end;
5656 /* Determine return type */
5657 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5658 V_VT(result) = VT_NULL;
5659 hr = S_OK;
5660 goto end;
5662 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5663 leftvt == VT_I4 || leftvt == VT_R4 ||
5664 leftvt == VT_R8 || leftvt == VT_CY ||
5665 leftvt == VT_DATE || leftvt == VT_BSTR ||
5666 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5667 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5668 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5669 rightvt == VT_I4 || rightvt == VT_R4 ||
5670 rightvt == VT_R8 || rightvt == VT_CY ||
5671 rightvt == VT_DATE || rightvt == VT_BSTR ||
5672 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5673 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5674 resvt = VT_R8;
5675 else
5677 hr = DISP_E_BADVARTYPE;
5678 goto end;
5681 hr = VariantChangeType(&dl,left,0,resvt);
5682 if (FAILED(hr)) {
5683 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5684 hr = E_FAIL;
5685 goto end;
5688 hr = VariantChangeType(&dr,right,0,resvt);
5689 if (FAILED(hr)) {
5690 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5691 hr = E_FAIL;
5692 goto end;
5695 V_VT(result) = VT_R8;
5696 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5698 end:
5699 VariantClear(&dl);
5700 VariantClear(&dr);
5701 VariantClear(&tempLeft);
5702 VariantClear(&tempRight);
5704 return hr;
5707 /**********************************************************************
5708 * VarImp [OLEAUT32.154]
5710 * Bitwise implication of two variants.
5712 * PARAMS
5713 * left [I] First variant
5714 * right [I] Second variant
5715 * result [O] Result variant
5717 * RETURNS
5718 * Success: S_OK.
5719 * Failure: An HRESULT error code indicating the error.
5721 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5723 HRESULT hres = S_OK;
5724 VARTYPE resvt = VT_EMPTY;
5725 VARTYPE leftvt,rightvt;
5726 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5727 VARIANT lv,rv;
5728 double d;
5729 VARIANT tempLeft, tempRight;
5731 VariantInit(&lv);
5732 VariantInit(&rv);
5733 VariantInit(&tempLeft);
5734 VariantInit(&tempRight);
5736 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5737 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5739 /* Handle VT_DISPATCH by storing and taking address of returned value */
5740 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5742 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5743 if (FAILED(hres)) goto VarImp_Exit;
5744 left = &tempLeft;
5746 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5748 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5749 if (FAILED(hres)) goto VarImp_Exit;
5750 right = &tempRight;
5753 leftvt = V_VT(left)&VT_TYPEMASK;
5754 rightvt = V_VT(right)&VT_TYPEMASK;
5755 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5756 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5758 if (leftExtraFlags != rightExtraFlags)
5760 hres = DISP_E_BADVARTYPE;
5761 goto VarImp_Exit;
5763 ExtraFlags = leftExtraFlags;
5765 /* Native VarImp always returns an error when using extra
5766 * flags or if the variants are I8 and INT.
5768 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5769 ExtraFlags != 0)
5771 hres = DISP_E_BADVARTYPE;
5772 goto VarImp_Exit;
5775 /* Determine result type */
5776 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5777 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5779 V_VT(result) = VT_NULL;
5780 hres = S_OK;
5781 goto VarImp_Exit;
5783 else if (leftvt == VT_I8 || rightvt == VT_I8)
5784 resvt = VT_I8;
5785 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5786 leftvt == VT_INT || rightvt == VT_INT ||
5787 leftvt == VT_UINT || rightvt == VT_UINT ||
5788 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5789 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5790 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5791 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5792 leftvt == VT_DATE || rightvt == VT_DATE ||
5793 leftvt == VT_CY || rightvt == VT_CY ||
5794 leftvt == VT_R8 || rightvt == VT_R8 ||
5795 leftvt == VT_R4 || rightvt == VT_R4 ||
5796 leftvt == VT_I1 || rightvt == VT_I1)
5797 resvt = VT_I4;
5798 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5799 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5800 (leftvt == VT_NULL && rightvt == VT_UI1))
5801 resvt = VT_UI1;
5802 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5803 leftvt == VT_I2 || rightvt == VT_I2 ||
5804 leftvt == VT_UI1 || rightvt == VT_UI1)
5805 resvt = VT_I2;
5806 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5807 leftvt == VT_BSTR || rightvt == VT_BSTR)
5808 resvt = VT_BOOL;
5810 /* VT_NULL requires special handling for when the opposite
5811 * variant is equal to something other than -1.
5812 * (NULL Imp 0 = NULL, NULL Imp n = n)
5814 if (leftvt == VT_NULL)
5816 VARIANT_BOOL b;
5817 switch(rightvt)
5819 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5820 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5821 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5822 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5823 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5824 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5825 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5826 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5827 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5828 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5829 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5830 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5831 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5832 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5833 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5834 case VT_DECIMAL:
5835 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5836 resvt = VT_NULL;
5837 break;
5838 case VT_BSTR:
5839 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5840 if (FAILED(hres)) goto VarImp_Exit;
5841 else if (!b)
5842 V_VT(result) = VT_NULL;
5843 else
5845 V_VT(result) = VT_BOOL;
5846 V_BOOL(result) = b;
5848 goto VarImp_Exit;
5850 if (resvt == VT_NULL)
5852 V_VT(result) = resvt;
5853 goto VarImp_Exit;
5855 else
5857 hres = VariantChangeType(result,right,0,resvt);
5858 goto VarImp_Exit;
5862 /* Special handling is required when NULL is the right variant.
5863 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5865 else if (rightvt == VT_NULL)
5867 VARIANT_BOOL b;
5868 switch(leftvt)
5870 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5871 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5872 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5873 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5874 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5875 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5876 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5877 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5878 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5879 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5880 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5881 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5882 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5883 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5884 case VT_DECIMAL:
5885 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5886 resvt = VT_NULL;
5887 break;
5888 case VT_BSTR:
5889 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5890 if (FAILED(hres)) goto VarImp_Exit;
5891 else if (b == VARIANT_TRUE)
5892 resvt = VT_NULL;
5894 if (resvt == VT_NULL)
5896 V_VT(result) = resvt;
5897 goto VarImp_Exit;
5901 hres = VariantCopy(&lv, left);
5902 if (FAILED(hres)) goto VarImp_Exit;
5904 if (rightvt == VT_NULL)
5906 memset( &rv, 0, sizeof(rv) );
5907 V_VT(&rv) = resvt;
5909 else
5911 hres = VariantCopy(&rv, right);
5912 if (FAILED(hres)) goto VarImp_Exit;
5915 if (V_VT(&lv) == VT_BSTR &&
5916 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5917 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5918 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5919 hres = VariantChangeType(&lv,&lv,0,resvt);
5920 if (FAILED(hres)) goto VarImp_Exit;
5922 if (V_VT(&rv) == VT_BSTR &&
5923 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5924 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5925 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5926 hres = VariantChangeType(&rv, &rv, 0, resvt);
5927 if (FAILED(hres)) goto VarImp_Exit;
5929 /* do the math */
5930 V_VT(result) = resvt;
5931 switch (resvt)
5933 case VT_I8:
5934 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5935 break;
5936 case VT_I4:
5937 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5938 break;
5939 case VT_I2:
5940 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
5941 break;
5942 case VT_UI1:
5943 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
5944 break;
5945 case VT_BOOL:
5946 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
5947 break;
5948 default:
5949 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5950 leftvt,rightvt);
5953 VarImp_Exit:
5955 VariantClear(&lv);
5956 VariantClear(&rv);
5957 VariantClear(&tempLeft);
5958 VariantClear(&tempRight);
5960 return hres;