oleaut32: Remove duplicated expression (PVS-Studio).
[wine/multimedia.git] / dlls / oleaut32 / variant.c
bloba00e16f9d9c2a0c7c9884a4408e768be09c4c063
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);
47 static const char * const variant_types[] =
49 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
50 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
51 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
52 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
53 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
54 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
55 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
56 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
57 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID",
58 "VT_VERSIONED_STREAM"
61 static const char * const variant_flags[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_RESERVED",
72 "|VT_VECTOR|VT_RESERVED",
73 "|VT_ARRAY|VT_RESERVED",
74 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
75 "|VT_BYREF|VT_RESERVED",
76 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
77 "|VT_ARRAY|VT_BYREF|VT_RESERVED",
78 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_RESERVED",
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("(%s,0x%08x,0x%04x,%s,%s)\n", debugstr_variant(pd), lcid, wFlags,
90 debugstr_variant(ps), debugstr_vt(vt));
92 if (vt == VT_BSTR || vtFrom == VT_BSTR)
94 /* All flags passed to low level function are only used for
95 * changing to or from strings. Map these here.
97 if (wFlags & VARIANT_LOCALBOOL)
98 dwFlags |= VAR_LOCALBOOL;
99 if (wFlags & VARIANT_CALENDAR_HIJRI)
100 dwFlags |= VAR_CALENDAR_HIJRI;
101 if (wFlags & VARIANT_CALENDAR_THAI)
102 dwFlags |= VAR_CALENDAR_THAI;
103 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
104 dwFlags |= VAR_CALENDAR_GREGORIAN;
105 if (wFlags & VARIANT_NOUSEROVERRIDE)
106 dwFlags |= LOCALE_NOUSEROVERRIDE;
107 if (wFlags & VARIANT_USE_NLS)
108 dwFlags |= LOCALE_USE_NLS;
111 /* Map int/uint to i4/ui4 */
112 if (vt == VT_INT)
113 vt = VT_I4;
114 else if (vt == VT_UINT)
115 vt = VT_UI4;
117 if (vtFrom == VT_INT)
118 vtFrom = VT_I4;
119 else if (vtFrom == VT_UINT)
120 vtFrom = VT_UI4;
122 if (vt == vtFrom)
123 return VariantCopy(pd, ps);
125 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
127 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
128 * accessing the default object property.
130 return DISP_E_TYPEMISMATCH;
133 switch (vt)
135 case VT_EMPTY:
136 if (vtFrom == VT_NULL)
137 return DISP_E_TYPEMISMATCH;
138 /* ... Fall through */
139 case VT_NULL:
140 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
142 res = VariantClear( pd );
143 if (vt == VT_NULL && SUCCEEDED(res))
144 V_VT(pd) = VT_NULL;
146 return res;
148 case VT_I1:
149 switch (vtFrom)
151 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
152 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
153 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
154 case VT_UI1: V_I1(pd) = V_UI1(ps); return S_OK;
155 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
156 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
157 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
158 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
159 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
160 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
161 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
162 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
163 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
164 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
165 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
166 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
168 break;
170 case VT_I2:
171 switch (vtFrom)
173 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
174 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
175 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
176 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
177 case VT_UI2: V_I2(pd) = V_UI2(ps); return S_OK;
178 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
179 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
180 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
181 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
182 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
183 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
184 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
185 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
186 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
187 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
188 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
190 break;
192 case VT_I4:
193 switch (vtFrom)
195 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
196 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
197 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
198 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
199 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
200 case VT_UI4: V_I4(pd) = V_UI4(ps); return S_OK;
201 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
202 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
203 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
204 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
205 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
206 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
207 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
208 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
209 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
210 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
212 break;
214 case VT_UI1:
215 switch (vtFrom)
217 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
218 case VT_I1: V_UI1(pd) = V_I1(ps); return S_OK;
219 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
220 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
221 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
222 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
223 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
224 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
225 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
226 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
227 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
228 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
229 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
230 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
231 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
232 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
234 break;
236 case VT_UI2:
237 switch (vtFrom)
239 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
240 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
241 case VT_I2: V_UI2(pd) = V_I2(ps); return S_OK;
242 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
243 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
244 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
245 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
246 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
247 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
248 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
249 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
250 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
251 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
252 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
253 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
254 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
256 break;
258 case VT_UI4:
259 switch (vtFrom)
261 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
262 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
263 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
264 case VT_I4: V_UI4(pd) = V_I4(ps); return S_OK;
265 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
266 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
267 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
268 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
269 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
270 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
271 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
272 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
273 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
274 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
275 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
276 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
278 break;
280 case VT_UI8:
281 switch (vtFrom)
283 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
284 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
285 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
286 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
287 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
288 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
289 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
290 case VT_I8: V_UI8(pd) = V_I8(ps); return S_OK;
291 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
292 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
293 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
294 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
295 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
296 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
297 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
298 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
300 break;
302 case VT_I8:
303 switch (vtFrom)
305 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
306 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
307 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
308 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
309 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
310 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
311 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
312 case VT_UI8: V_I8(pd) = V_UI8(ps); return S_OK;
313 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
314 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
315 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
316 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
317 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
318 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
319 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
320 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
322 break;
324 case VT_R4:
325 switch (vtFrom)
327 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
328 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
329 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
330 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
331 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
332 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
333 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
334 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
335 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
336 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
337 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
338 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
339 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
340 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
341 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
342 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
344 break;
346 case VT_R8:
347 switch (vtFrom)
349 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
350 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
351 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
352 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
353 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
354 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
355 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
356 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
357 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
358 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
359 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
360 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
361 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
362 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
363 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
364 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
366 break;
368 case VT_DATE:
369 switch (vtFrom)
371 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
372 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
373 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
374 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
375 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
376 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
377 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
378 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
379 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
380 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
381 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
382 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
383 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
384 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
385 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
386 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
388 break;
390 case VT_BOOL:
391 switch (vtFrom)
393 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
394 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
395 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
396 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
397 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
398 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
399 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
400 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
401 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
402 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
403 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
404 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
405 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
406 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
407 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
408 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
410 break;
412 case VT_BSTR:
413 switch (vtFrom)
415 case VT_EMPTY:
416 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
417 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
418 case VT_BOOL:
419 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
420 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
421 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
422 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
423 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
424 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
425 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
426 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
427 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
428 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
429 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
430 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd));
437 break;
439 case VT_CY:
440 switch (vtFrom)
442 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
443 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
444 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
445 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
446 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
447 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
448 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
449 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
450 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
451 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
452 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
453 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
454 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
455 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
456 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
457 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
459 break;
461 case VT_DECIMAL:
462 switch (vtFrom)
464 case VT_EMPTY:
465 case VT_BOOL:
466 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
467 DEC_HI32(&V_DECIMAL(pd)) = 0;
468 DEC_MID32(&V_DECIMAL(pd)) = 0;
469 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
470 * VT_NULL and VT_EMPTY always give a 0 value.
472 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
473 return S_OK;
474 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
475 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
476 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
477 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
478 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
479 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
480 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
481 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
482 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
483 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
484 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
485 case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
486 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
487 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
489 break;
491 case VT_UNKNOWN:
492 switch (vtFrom)
494 case VT_DISPATCH:
495 if (V_DISPATCH(ps) == NULL)
496 V_UNKNOWN(pd) = NULL;
497 else
498 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
499 break;
501 break;
503 case VT_DISPATCH:
504 switch (vtFrom)
506 case VT_UNKNOWN:
507 if (V_UNKNOWN(ps) == NULL)
508 V_DISPATCH(pd) = NULL;
509 else
510 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
511 break;
513 break;
515 case VT_RECORD:
516 break;
518 return res;
521 /* Coerce to/from an array */
522 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
524 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
525 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
527 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
528 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(pd));
530 if (V_VT(ps) == vt)
531 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
533 return DISP_E_TYPEMISMATCH;
536 /******************************************************************************
537 * Check if a variants type is valid.
539 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
541 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
543 vt &= VT_TYPEMASK;
545 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
547 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
549 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
550 return DISP_E_BADVARTYPE;
551 if (vt != (VARTYPE)15)
552 return S_OK;
555 return DISP_E_BADVARTYPE;
558 /******************************************************************************
559 * VariantInit [OLEAUT32.8]
561 * Initialise a variant.
563 * PARAMS
564 * pVarg [O] Variant to initialise
566 * RETURNS
567 * Nothing.
569 * NOTES
570 * This function simply sets the type of the variant to VT_EMPTY. It does not
571 * free any existing value, use VariantClear() for that.
573 void WINAPI VariantInit(VARIANTARG* pVarg)
575 TRACE("(%p)\n", pVarg);
577 /* Win8.1 zeroes whole struct. Previous implementations don't set any other fields. */
578 V_VT(pVarg) = VT_EMPTY;
581 HRESULT VARIANT_ClearInd(VARIANTARG *pVarg)
583 HRESULT hres;
585 TRACE("(%s)\n", debugstr_variant(pVarg));
587 hres = VARIANT_ValidateType(V_VT(pVarg));
588 if (FAILED(hres))
589 return hres;
591 switch (V_VT(pVarg))
593 case VT_DISPATCH:
594 case VT_UNKNOWN:
595 if (V_UNKNOWN(pVarg))
596 IUnknown_Release(V_UNKNOWN(pVarg));
597 break;
598 case VT_UNKNOWN | VT_BYREF:
599 case VT_DISPATCH | VT_BYREF:
600 if(*V_UNKNOWNREF(pVarg))
601 IUnknown_Release(*V_UNKNOWNREF(pVarg));
602 break;
603 case VT_BSTR:
604 SysFreeString(V_BSTR(pVarg));
605 break;
606 case VT_BSTR | VT_BYREF:
607 SysFreeString(*V_BSTRREF(pVarg));
608 break;
609 case VT_VARIANT | VT_BYREF:
610 VariantClear(V_VARIANTREF(pVarg));
611 break;
612 case VT_RECORD:
613 case VT_RECORD | VT_BYREF:
615 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
616 if (pBr->pRecInfo)
618 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
619 IRecordInfo_Release(pBr->pRecInfo);
621 break;
623 default:
624 if (V_ISARRAY(pVarg) || (V_VT(pVarg) & ~VT_BYREF) == VT_SAFEARRAY)
626 if (V_ISBYREF(pVarg))
628 if (*V_ARRAYREF(pVarg))
629 hres = SafeArrayDestroy(*V_ARRAYREF(pVarg));
631 else if (V_ARRAY(pVarg))
632 hres = SafeArrayDestroy(V_ARRAY(pVarg));
634 break;
637 V_VT(pVarg) = VT_EMPTY;
638 return hres;
641 /******************************************************************************
642 * VariantClear [OLEAUT32.9]
644 * Clear a variant.
646 * PARAMS
647 * pVarg [I/O] Variant to clear
649 * RETURNS
650 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
651 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
653 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
655 HRESULT hres;
657 TRACE("(%s)\n", debugstr_variant(pVarg));
659 hres = VARIANT_ValidateType(V_VT(pVarg));
661 if (SUCCEEDED(hres))
663 if (!V_ISBYREF(pVarg))
665 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
667 hres = SafeArrayDestroy(V_ARRAY(pVarg));
669 else if (V_VT(pVarg) == VT_BSTR)
671 SysFreeString(V_BSTR(pVarg));
673 else if (V_VT(pVarg) == VT_RECORD)
675 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
676 if (pBr->pRecInfo)
678 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
679 IRecordInfo_Release(pBr->pRecInfo);
682 else if (V_VT(pVarg) == VT_DISPATCH ||
683 V_VT(pVarg) == VT_UNKNOWN)
685 if (V_UNKNOWN(pVarg))
686 IUnknown_Release(V_UNKNOWN(pVarg));
689 V_VT(pVarg) = VT_EMPTY;
691 return hres;
694 /******************************************************************************
695 * Copy an IRecordInfo object contained in a variant.
697 static HRESULT VARIANT_CopyIRecordInfo(VARIANT *dest, VARIANT *src)
699 struct __tagBRECORD *dest_rec = &V_UNION(dest, brecVal);
700 struct __tagBRECORD *src_rec = &V_UNION(src, brecVal);
701 HRESULT hr = S_OK;
702 ULONG size;
704 if (!src_rec->pRecInfo)
706 if (src_rec->pvRecord) return E_INVALIDARG;
707 return S_OK;
710 hr = IRecordInfo_GetSize(src_rec->pRecInfo, &size);
711 if (FAILED(hr)) return hr;
713 /* This could look cleaner if only RecordCreate() was used, but native doesn't use it.
714 Memory should be allocated in a same way as RecordCreate() does, so RecordDestroy()
715 could free it later. */
716 dest_rec->pvRecord = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
717 if (!dest_rec->pvRecord) return E_OUTOFMEMORY;
719 dest_rec->pRecInfo = src_rec->pRecInfo;
720 IRecordInfo_AddRef(src_rec->pRecInfo);
722 return IRecordInfo_RecordCopy(src_rec->pRecInfo, src_rec->pvRecord, dest_rec->pvRecord);
725 /******************************************************************************
726 * VariantCopy [OLEAUT32.10]
728 * Copy a variant.
730 * PARAMS
731 * pvargDest [O] Destination for copy
732 * pvargSrc [I] Source variant to copy
734 * RETURNS
735 * Success: S_OK. pvargDest contains a copy of pvargSrc.
736 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
737 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
738 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
739 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
741 * NOTES
742 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
743 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
744 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
745 * fails, so does this function.
746 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
747 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
748 * is copied rather than just any pointers to it.
749 * - For by-value object types the object pointer is copied and the objects
750 * reference count increased using IUnknown_AddRef().
751 * - For all by-reference types, only the referencing pointer is copied.
753 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
755 HRESULT hres = S_OK;
757 TRACE("(%s,%s)\n", debugstr_variant(pvargDest), debugstr_variant(pvargSrc));
759 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
760 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
761 return DISP_E_BADVARTYPE;
763 if (pvargSrc != pvargDest &&
764 SUCCEEDED(hres = VariantClear(pvargDest)))
766 *pvargDest = *pvargSrc; /* Shallow copy the value */
768 if (!V_ISBYREF(pvargSrc))
770 switch (V_VT(pvargSrc))
772 case VT_BSTR:
773 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
774 if (!V_BSTR(pvargDest))
775 hres = E_OUTOFMEMORY;
776 break;
777 case VT_RECORD:
778 hres = VARIANT_CopyIRecordInfo(pvargDest, pvargSrc);
779 break;
780 case VT_DISPATCH:
781 case VT_UNKNOWN:
782 V_UNKNOWN(pvargDest) = V_UNKNOWN(pvargSrc);
783 if (V_UNKNOWN(pvargSrc))
784 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
785 break;
786 default:
787 if (V_ISARRAY(pvargSrc))
788 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
792 return hres;
795 /* Return the byte size of a variants data */
796 static inline size_t VARIANT_DataSize(const VARIANT* pv)
798 switch (V_TYPE(pv))
800 case VT_I1:
801 case VT_UI1: return sizeof(BYTE);
802 case VT_I2:
803 case VT_UI2: return sizeof(SHORT);
804 case VT_INT:
805 case VT_UINT:
806 case VT_I4:
807 case VT_UI4: return sizeof(LONG);
808 case VT_I8:
809 case VT_UI8: return sizeof(LONGLONG);
810 case VT_R4: return sizeof(float);
811 case VT_R8: return sizeof(double);
812 case VT_DATE: return sizeof(DATE);
813 case VT_BOOL: return sizeof(VARIANT_BOOL);
814 case VT_DISPATCH:
815 case VT_UNKNOWN:
816 case VT_BSTR: return sizeof(void*);
817 case VT_CY: return sizeof(CY);
818 case VT_ERROR: return sizeof(SCODE);
820 TRACE("Shouldn't be called for variant %s!\n", debugstr_variant(pv));
821 return 0;
824 /******************************************************************************
825 * VariantCopyInd [OLEAUT32.11]
827 * Copy a variant, dereferencing it if it is by-reference.
829 * PARAMS
830 * pvargDest [O] Destination for copy
831 * pvargSrc [I] Source variant to copy
833 * RETURNS
834 * Success: S_OK. pvargDest contains a copy of pvargSrc.
835 * Failure: An HRESULT error code indicating the error.
837 * NOTES
838 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
839 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
840 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
841 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
842 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
844 * NOTES
845 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
846 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
847 * value.
848 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
849 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
850 * to it. If clearing pvargDest fails, so does this function.
852 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
854 VARIANTARG vTmp, *pSrc = pvargSrc;
855 VARTYPE vt;
856 HRESULT hres = S_OK;
858 TRACE("(%s,%s)\n", debugstr_variant(pvargDest), debugstr_variant(pvargSrc));
860 if (!V_ISBYREF(pvargSrc))
861 return VariantCopy(pvargDest, pvargSrc);
863 /* Argument checking is more lax than VariantCopy()... */
864 vt = V_TYPE(pvargSrc);
865 if (V_ISARRAY(pvargSrc) || (V_VT(pvargSrc) == (VT_RECORD|VT_BYREF)) ||
866 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
867 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
869 /* OK */
871 else
872 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
874 if (pvargSrc == pvargDest)
876 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
877 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
879 vTmp = *pvargSrc;
880 pSrc = &vTmp;
881 V_VT(pvargDest) = VT_EMPTY;
883 else
885 /* Copy into another variant. Free the variant in pvargDest */
886 if (FAILED(hres = VariantClear(pvargDest)))
888 TRACE("VariantClear() of destination failed\n");
889 return hres;
893 if (V_ISARRAY(pSrc))
895 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
896 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
898 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
900 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
901 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
903 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
905 hres = VARIANT_CopyIRecordInfo(pvargDest, pvargSrc);
907 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
908 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
910 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
911 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
912 if (*V_UNKNOWNREF(pSrc))
913 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
915 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
917 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
918 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
919 hres = E_INVALIDARG; /* Don't dereference more than one level */
920 else
921 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
923 /* Use the dereferenced variants type value, not VT_VARIANT */
924 goto VariantCopyInd_Return;
926 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
928 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
929 sizeof(DECIMAL) - sizeof(USHORT));
931 else
933 /* Copy the pointed to data into this variant */
934 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
937 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
939 VariantCopyInd_Return:
941 if (pSrc != pvargSrc)
942 VariantClear(pSrc);
944 TRACE("returning 0x%08x, %s\n", hres, debugstr_variant(pvargDest));
945 return hres;
948 /******************************************************************************
949 * VariantChangeType [OLEAUT32.12]
951 * Change the type of a variant.
953 * PARAMS
954 * pvargDest [O] Destination for the converted variant
955 * pvargSrc [O] Source variant to change the type of
956 * wFlags [I] VARIANT_ flags from "oleauto.h"
957 * vt [I] Variant type to change pvargSrc into
959 * RETURNS
960 * Success: S_OK. pvargDest contains the converted value.
961 * Failure: An HRESULT error code describing the failure.
963 * NOTES
964 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
965 * See VariantChangeTypeEx.
967 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
968 USHORT wFlags, VARTYPE vt)
970 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
973 /******************************************************************************
974 * VariantChangeTypeEx [OLEAUT32.147]
976 * Change the type of a variant.
978 * PARAMS
979 * pvargDest [O] Destination for the converted variant
980 * pvargSrc [O] Source variant to change the type of
981 * lcid [I] LCID for the conversion
982 * wFlags [I] VARIANT_ flags from "oleauto.h"
983 * vt [I] Variant type to change pvargSrc into
985 * RETURNS
986 * Success: S_OK. pvargDest contains the converted value.
987 * Failure: An HRESULT error code describing the failure.
989 * NOTES
990 * pvargDest and pvargSrc can point to the same variant to perform an in-place
991 * conversion. If the conversion is successful, pvargSrc will be freed.
993 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
994 LCID lcid, USHORT wFlags, VARTYPE vt)
996 HRESULT res = S_OK;
998 TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest),
999 debugstr_variant(pvargSrc), lcid, wFlags, debugstr_vt(vt));
1001 if (vt == VT_CLSID)
1002 res = DISP_E_BADVARTYPE;
1003 else
1005 res = VARIANT_ValidateType(V_VT(pvargSrc));
1007 if (SUCCEEDED(res))
1009 res = VARIANT_ValidateType(vt);
1011 if (SUCCEEDED(res))
1013 VARIANTARG vTmp, vSrcDeref;
1015 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
1016 res = DISP_E_TYPEMISMATCH;
1017 else
1019 V_VT(&vTmp) = VT_EMPTY;
1020 V_VT(&vSrcDeref) = VT_EMPTY;
1021 VariantClear(&vTmp);
1022 VariantClear(&vSrcDeref);
1025 if (SUCCEEDED(res))
1027 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1028 if (SUCCEEDED(res))
1030 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1031 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1032 else
1033 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1035 if (SUCCEEDED(res)) {
1036 V_VT(&vTmp) = vt;
1037 res = VariantCopy(pvargDest, &vTmp);
1039 VariantClear(&vTmp);
1040 VariantClear(&vSrcDeref);
1047 TRACE("returning 0x%08x, %s\n", res, debugstr_variant(pvargDest));
1048 return res;
1051 /* Date Conversions */
1053 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1055 /* Convert a VT_DATE value to a Julian Date */
1056 static inline int VARIANT_JulianFromDate(int dateIn)
1058 int julianDays = dateIn;
1060 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1061 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1062 return julianDays;
1065 /* Convert a Julian Date to a VT_DATE value */
1066 static inline int VARIANT_DateFromJulian(int dateIn)
1068 int julianDays = dateIn;
1070 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1071 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1072 return julianDays;
1075 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1076 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1078 int j, i, l, n;
1080 l = jd + 68569;
1081 n = l * 4 / 146097;
1082 l -= (n * 146097 + 3) / 4;
1083 i = (4000 * (l + 1)) / 1461001;
1084 l += 31 - (i * 1461) / 4;
1085 j = (l * 80) / 2447;
1086 *day = l - (j * 2447) / 80;
1087 l = j / 11;
1088 *month = (j + 2) - (12 * l);
1089 *year = 100 * (n - 49) + i + l;
1092 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1093 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1095 int m12 = (month - 14) / 12;
1097 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1098 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1101 /* Macros for accessing DOS format date/time fields */
1102 #define DOS_YEAR(x) (1980 + (x >> 9))
1103 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1104 #define DOS_DAY(x) (x & 0x1f)
1105 #define DOS_HOUR(x) (x >> 11)
1106 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1107 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1108 /* Create a DOS format date/time */
1109 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1110 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1112 /* Roll a date forwards or backwards to correct it */
1113 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1115 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1116 short iYear, iMonth, iDay, iHour, iMinute, iSecond;
1118 /* interpret values signed */
1119 iYear = lpUd->st.wYear;
1120 iMonth = lpUd->st.wMonth;
1121 iDay = lpUd->st.wDay;
1122 iHour = lpUd->st.wHour;
1123 iMinute = lpUd->st.wMinute;
1124 iSecond = lpUd->st.wSecond;
1126 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay, iMonth,
1127 iYear, iHour, iMinute, iSecond);
1129 if (iYear > 9999 || iYear < -9999)
1130 return E_INVALIDARG; /* Invalid value */
1131 /* Year 0 to 29 are treated as 2000 + year */
1132 if (iYear >= 0 && iYear < 30)
1133 iYear += 2000;
1134 /* Remaining years < 100 are treated as 1900 + year */
1135 else if (iYear >= 30 && iYear < 100)
1136 iYear += 1900;
1138 iMinute += iSecond / 60;
1139 iSecond = iSecond % 60;
1140 iHour += iMinute / 60;
1141 iMinute = iMinute % 60;
1142 iDay += iHour / 24;
1143 iHour = iHour % 24;
1144 iYear += iMonth / 12;
1145 iMonth = iMonth % 12;
1146 if (iMonth<=0) {iMonth+=12; iYear--;}
1147 while (iDay > days[iMonth])
1149 if (iMonth == 2 && IsLeapYear(iYear))
1150 iDay -= 29;
1151 else
1152 iDay -= days[iMonth];
1153 iMonth++;
1154 iYear += iMonth / 12;
1155 iMonth = iMonth % 12;
1157 while (iDay <= 0)
1159 iMonth--;
1160 if (iMonth<=0) {iMonth+=12; iYear--;}
1161 if (iMonth == 2 && IsLeapYear(iYear))
1162 iDay += 29;
1163 else
1164 iDay += days[iMonth];
1167 if (iSecond<0){iSecond+=60; iMinute--;}
1168 if (iMinute<0){iMinute+=60; iHour--;}
1169 if (iHour<0) {iHour+=24; iDay--;}
1170 if (iYear<=0) iYear+=2000;
1172 lpUd->st.wYear = iYear;
1173 lpUd->st.wMonth = iMonth;
1174 lpUd->st.wDay = iDay;
1175 lpUd->st.wHour = iHour;
1176 lpUd->st.wMinute = iMinute;
1177 lpUd->st.wSecond = iSecond;
1179 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1180 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1181 return S_OK;
1184 /**********************************************************************
1185 * DosDateTimeToVariantTime [OLEAUT32.14]
1187 * Convert a Dos format date and time into variant VT_DATE format.
1189 * PARAMS
1190 * wDosDate [I] Dos format date
1191 * wDosTime [I] Dos format time
1192 * pDateOut [O] Destination for VT_DATE format
1194 * RETURNS
1195 * Success: TRUE. pDateOut contains the converted time.
1196 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1198 * NOTES
1199 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1200 * - Dos format times are accurate to only 2 second precision.
1201 * - The format of a Dos Date is:
1202 *| Bits Values Meaning
1203 *| ---- ------ -------
1204 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1205 *| the days in the month rolls forward the extra days.
1206 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1207 *| year. 13-15 are invalid.
1208 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1209 * - The format of a Dos Time is:
1210 *| Bits Values Meaning
1211 *| ---- ------ -------
1212 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1213 *| 5-10 0-59 Minutes. 60-63 are invalid.
1214 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1216 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1217 double *pDateOut)
1219 UDATE ud;
1221 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1222 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1223 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1224 pDateOut);
1226 ud.st.wYear = DOS_YEAR(wDosDate);
1227 ud.st.wMonth = DOS_MONTH(wDosDate);
1228 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1229 return FALSE;
1230 ud.st.wDay = DOS_DAY(wDosDate);
1231 ud.st.wHour = DOS_HOUR(wDosTime);
1232 ud.st.wMinute = DOS_MINUTE(wDosTime);
1233 ud.st.wSecond = DOS_SECOND(wDosTime);
1234 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1235 if (ud.st.wHour > 23 || ud.st.wMinute > 59 || ud.st.wSecond > 59)
1236 return FALSE; /* Invalid values in Dos*/
1238 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1241 /**********************************************************************
1242 * VariantTimeToDosDateTime [OLEAUT32.13]
1244 * Convert a variant format date into a Dos format date and time.
1246 * dateIn [I] VT_DATE time format
1247 * pwDosDate [O] Destination for Dos format date
1248 * pwDosTime [O] Destination for Dos format time
1250 * RETURNS
1251 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1252 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1254 * NOTES
1255 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1257 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1259 UDATE ud;
1261 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1263 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1264 return FALSE;
1266 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1267 return FALSE;
1269 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1270 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1272 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1273 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1274 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1275 return TRUE;
1278 /***********************************************************************
1279 * SystemTimeToVariantTime [OLEAUT32.184]
1281 * Convert a System format date and time into variant VT_DATE format.
1283 * PARAMS
1284 * lpSt [I] System format date and time
1285 * pDateOut [O] Destination for VT_DATE format date
1287 * RETURNS
1288 * Success: TRUE. *pDateOut contains the converted value.
1289 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1291 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1293 UDATE ud;
1295 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1296 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1298 if (lpSt->wMonth > 12)
1299 return FALSE;
1300 if (lpSt->wDay > 31)
1301 return FALSE;
1302 if ((short)lpSt->wYear < 0)
1303 return FALSE;
1305 ud.st = *lpSt;
1306 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1309 /***********************************************************************
1310 * VariantTimeToSystemTime [OLEAUT32.185]
1312 * Convert a variant VT_DATE into a System format date and time.
1314 * PARAMS
1315 * datein [I] Variant VT_DATE format date
1316 * lpSt [O] Destination for System format date and time
1318 * RETURNS
1319 * Success: TRUE. *lpSt contains the converted value.
1320 * Failure: FALSE, if dateIn is too large or small.
1322 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1324 UDATE ud;
1326 TRACE("(%g,%p)\n", dateIn, lpSt);
1328 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1329 return FALSE;
1331 *lpSt = ud.st;
1332 return TRUE;
1335 /***********************************************************************
1336 * VarDateFromUdateEx [OLEAUT32.319]
1338 * Convert an unpacked format date and time to a variant VT_DATE.
1340 * PARAMS
1341 * pUdateIn [I] Unpacked format date and time to convert
1342 * lcid [I] Locale identifier for the conversion
1343 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1344 * pDateOut [O] Destination for variant VT_DATE.
1346 * RETURNS
1347 * Success: S_OK. *pDateOut contains the converted value.
1348 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1350 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1352 UDATE ud;
1353 double dateVal, dateSign;
1355 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1356 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1357 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1358 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1359 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1361 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1362 FIXME("lcid possibly not handled, treating as en-us\n");
1364 ud = *pUdateIn;
1366 if (dwFlags & VAR_VALIDDATE)
1367 WARN("Ignoring VAR_VALIDDATE\n");
1369 if (FAILED(VARIANT_RollUdate(&ud)))
1370 return E_INVALIDARG;
1372 /* Date */
1373 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1375 /* Sign */
1376 dateSign = (dateVal < 0.0) ? -1.0 : 1.0;
1378 /* Time */
1379 dateVal += ud.st.wHour / 24.0 * dateSign;
1380 dateVal += ud.st.wMinute / 1440.0 * dateSign;
1381 dateVal += ud.st.wSecond / 86400.0 * dateSign;
1383 TRACE("Returning %g\n", dateVal);
1384 *pDateOut = dateVal;
1385 return S_OK;
1388 /***********************************************************************
1389 * VarDateFromUdate [OLEAUT32.330]
1391 * Convert an unpacked format date and time to a variant VT_DATE.
1393 * PARAMS
1394 * pUdateIn [I] Unpacked format date and time to convert
1395 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1396 * pDateOut [O] Destination for variant VT_DATE.
1398 * RETURNS
1399 * Success: S_OK. *pDateOut contains the converted value.
1400 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1402 * NOTES
1403 * This function uses the United States English locale for the conversion. Use
1404 * VarDateFromUdateEx() for alternate locales.
1406 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1408 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1410 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1413 /***********************************************************************
1414 * VarUdateFromDate [OLEAUT32.331]
1416 * Convert a variant VT_DATE into an unpacked format date and time.
1418 * PARAMS
1419 * datein [I] Variant VT_DATE format date
1420 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1421 * lpUdate [O] Destination for unpacked format date and time
1423 * RETURNS
1424 * Success: S_OK. *lpUdate contains the converted value.
1425 * Failure: E_INVALIDARG, if dateIn is too large or small.
1427 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1429 /* Cumulative totals of days per month */
1430 static const USHORT cumulativeDays[] =
1432 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1434 double datePart, timePart;
1435 int julianDays;
1437 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1439 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1440 return E_INVALIDARG;
1442 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1443 /* Compensate for int truncation (always downwards) */
1444 timePart = fabs(dateIn - datePart) + 0.00000000001;
1445 if (timePart >= 1.0)
1446 timePart -= 0.00000000001;
1448 /* Date */
1449 julianDays = VARIANT_JulianFromDate(dateIn);
1450 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1451 &lpUdate->st.wDay);
1453 datePart = (datePart + 1.5) / 7.0;
1454 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1455 if (lpUdate->st.wDayOfWeek == 0)
1456 lpUdate->st.wDayOfWeek = 5;
1457 else if (lpUdate->st.wDayOfWeek == 1)
1458 lpUdate->st.wDayOfWeek = 6;
1459 else
1460 lpUdate->st.wDayOfWeek -= 2;
1462 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1463 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1464 else
1465 lpUdate->wDayOfYear = 0;
1467 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1468 lpUdate->wDayOfYear += lpUdate->st.wDay;
1470 /* Time */
1471 timePart *= 24.0;
1472 lpUdate->st.wHour = timePart;
1473 timePart -= lpUdate->st.wHour;
1474 timePart *= 60.0;
1475 lpUdate->st.wMinute = timePart;
1476 timePart -= lpUdate->st.wMinute;
1477 timePart *= 60.0;
1478 lpUdate->st.wSecond = timePart;
1479 timePart -= lpUdate->st.wSecond;
1480 lpUdate->st.wMilliseconds = 0;
1481 if (timePart > 0.5)
1483 /* Round the milliseconds, adjusting the time/date forward if needed */
1484 if (lpUdate->st.wSecond < 59)
1485 lpUdate->st.wSecond++;
1486 else
1488 lpUdate->st.wSecond = 0;
1489 if (lpUdate->st.wMinute < 59)
1490 lpUdate->st.wMinute++;
1491 else
1493 lpUdate->st.wMinute = 0;
1494 if (lpUdate->st.wHour < 23)
1495 lpUdate->st.wHour++;
1496 else
1498 lpUdate->st.wHour = 0;
1499 /* Roll over a whole day */
1500 if (++lpUdate->st.wDay > 28)
1501 VARIANT_RollUdate(lpUdate);
1506 return S_OK;
1509 #define GET_NUMBER_TEXT(fld,name) \
1510 buff[0] = 0; \
1511 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1512 WARN("buffer too small for " #fld "\n"); \
1513 else \
1514 if (buff[0]) lpChars->name = buff[0]; \
1515 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1517 /* Get the valid number characters for an lcid */
1518 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1520 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1521 static CRITICAL_SECTION csLastChars = { NULL, -1, 0, 0, 0, 0 };
1522 static VARIANT_NUMBER_CHARS lastChars;
1523 static LCID lastLcid = -1;
1524 static DWORD lastFlags = 0;
1525 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1526 WCHAR buff[4];
1528 /* To make caching thread-safe, a critical section is needed */
1529 EnterCriticalSection(&csLastChars);
1531 /* Asking for default locale entries is very expensive: It is a registry
1532 server call. So cache one locally, as Microsoft does it too */
1533 if(lcid == lastLcid && dwFlags == lastFlags)
1535 memcpy(lpChars, &lastChars, sizeof(defaultChars));
1536 LeaveCriticalSection(&csLastChars);
1537 return;
1540 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1541 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1542 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1543 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1544 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1545 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1546 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1548 /* Local currency symbols are often 2 characters */
1549 lpChars->cCurrencyLocal2 = '\0';
1550 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1552 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1553 case 2: lpChars->cCurrencyLocal = buff[0];
1554 break;
1555 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1557 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1558 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1560 memcpy(&lastChars, lpChars, sizeof(defaultChars));
1561 lastLcid = lcid;
1562 lastFlags = dwFlags;
1563 LeaveCriticalSection(&csLastChars);
1566 /* Number Parsing States */
1567 #define B_PROCESSING_EXPONENT 0x1
1568 #define B_NEGATIVE_EXPONENT 0x2
1569 #define B_EXPONENT_START 0x4
1570 #define B_INEXACT_ZEROS 0x8
1571 #define B_LEADING_ZERO 0x10
1572 #define B_PROCESSING_HEX 0x20
1573 #define B_PROCESSING_OCT 0x40
1575 /**********************************************************************
1576 * VarParseNumFromStr [OLEAUT32.46]
1578 * Parse a string containing a number into a NUMPARSE structure.
1580 * PARAMS
1581 * lpszStr [I] String to parse number from
1582 * lcid [I] Locale Id for the conversion
1583 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1584 * pNumprs [I/O] Destination for parsed number
1585 * rgbDig [O] Destination for digits read in
1587 * RETURNS
1588 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1589 * the number.
1590 * Failure: E_INVALIDARG, if any parameter is invalid.
1591 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1592 * incorrectly.
1593 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1595 * NOTES
1596 * pNumprs must have the following fields set:
1597 * cDig: Set to the size of rgbDig.
1598 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1599 * from "oleauto.h".
1601 * FIXME
1602 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1603 * numerals, so this has not been implemented.
1605 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1606 NUMPARSE *pNumprs, BYTE *rgbDig)
1608 VARIANT_NUMBER_CHARS chars;
1609 BYTE rgbTmp[1024];
1610 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1611 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1612 int cchUsed = 0;
1614 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1616 if (!pNumprs || !rgbDig)
1617 return E_INVALIDARG;
1619 if (pNumprs->cDig < iMaxDigits)
1620 iMaxDigits = pNumprs->cDig;
1622 pNumprs->cDig = 0;
1623 pNumprs->dwOutFlags = 0;
1624 pNumprs->cchUsed = 0;
1625 pNumprs->nBaseShift = 0;
1626 pNumprs->nPwr10 = 0;
1628 if (!lpszStr)
1629 return DISP_E_TYPEMISMATCH;
1631 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1633 /* First consume all the leading symbols and space from the string */
1634 while (1)
1636 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1638 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1641 cchUsed++;
1642 lpszStr++;
1643 } while (isspaceW(*lpszStr));
1645 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1646 *lpszStr == chars.cPositiveSymbol &&
1647 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1649 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1650 cchUsed++;
1651 lpszStr++;
1653 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1654 *lpszStr == chars.cNegativeSymbol &&
1655 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1657 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1658 cchUsed++;
1659 lpszStr++;
1661 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1662 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1663 *lpszStr == chars.cCurrencyLocal &&
1664 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1666 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1667 cchUsed++;
1668 lpszStr++;
1669 /* Only accept currency characters */
1670 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1671 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1673 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1674 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1676 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1677 cchUsed++;
1678 lpszStr++;
1680 else
1681 break;
1684 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1686 /* Only accept non-currency characters */
1687 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1688 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1691 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1692 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1694 dwState |= B_PROCESSING_HEX;
1695 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1696 cchUsed=cchUsed+2;
1697 lpszStr=lpszStr+2;
1699 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1700 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1702 dwState |= B_PROCESSING_OCT;
1703 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1704 cchUsed=cchUsed+2;
1705 lpszStr=lpszStr+2;
1708 /* Strip Leading zeros */
1709 while (*lpszStr == '0')
1711 dwState |= B_LEADING_ZERO;
1712 cchUsed++;
1713 lpszStr++;
1716 while (*lpszStr)
1718 if (isdigitW(*lpszStr))
1720 if (dwState & B_PROCESSING_EXPONENT)
1722 int exponentSize = 0;
1723 if (dwState & B_EXPONENT_START)
1725 if (!isdigitW(*lpszStr))
1726 break; /* No exponent digits - invalid */
1727 while (*lpszStr == '0')
1729 /* Skip leading zero's in the exponent */
1730 cchUsed++;
1731 lpszStr++;
1735 while (isdigitW(*lpszStr))
1737 exponentSize *= 10;
1738 exponentSize += *lpszStr - '0';
1739 cchUsed++;
1740 lpszStr++;
1742 if (dwState & B_NEGATIVE_EXPONENT)
1743 exponentSize = -exponentSize;
1744 /* Add the exponent into the powers of 10 */
1745 pNumprs->nPwr10 += exponentSize;
1746 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1747 lpszStr--; /* back up to allow processing of next char */
1749 else
1751 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1752 && !(dwState & B_PROCESSING_OCT))
1754 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1756 if (*lpszStr != '0')
1757 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1759 /* This digit can't be represented, but count it in nPwr10 */
1760 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1761 pNumprs->nPwr10--;
1762 else
1763 pNumprs->nPwr10++;
1765 else
1767 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1768 return DISP_E_TYPEMISMATCH;
1771 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1772 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1774 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1776 pNumprs->cDig++;
1777 cchUsed++;
1780 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1782 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1783 cchUsed++;
1785 else if (*lpszStr == chars.cDecimalPoint &&
1786 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1787 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1789 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1790 cchUsed++;
1792 /* If we have no digits so far, skip leading zeros */
1793 if (!pNumprs->cDig)
1795 while (lpszStr[1] == '0')
1797 dwState |= B_LEADING_ZERO;
1798 cchUsed++;
1799 lpszStr++;
1800 pNumprs->nPwr10--;
1804 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1805 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1806 dwState & B_PROCESSING_HEX)
1808 if (pNumprs->cDig >= iMaxDigits)
1810 return DISP_E_OVERFLOW;
1812 else
1814 if (*lpszStr >= 'a')
1815 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1816 else
1817 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1819 pNumprs->cDig++;
1820 cchUsed++;
1822 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1823 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1824 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1826 dwState |= B_PROCESSING_EXPONENT;
1827 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1828 cchUsed++;
1830 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1832 cchUsed++; /* Ignore positive exponent */
1834 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1836 dwState |= B_NEGATIVE_EXPONENT;
1837 cchUsed++;
1839 else
1840 break; /* Stop at an unrecognised character */
1842 lpszStr++;
1845 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1847 /* Ensure a 0 on its own gets stored */
1848 pNumprs->cDig = 1;
1849 rgbTmp[0] = 0;
1852 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1854 pNumprs->cchUsed = cchUsed;
1855 WARN("didn't completely parse exponent\n");
1856 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1859 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1861 if (dwState & B_INEXACT_ZEROS)
1862 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1863 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1865 /* copy all of the digits into the output digit buffer */
1866 /* this is exactly what windows does although it also returns */
1867 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1868 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1870 if (dwState & B_PROCESSING_HEX) {
1871 /* hex numbers have always the same format */
1872 pNumprs->nPwr10=0;
1873 pNumprs->nBaseShift=4;
1874 } else {
1875 if (dwState & B_PROCESSING_OCT) {
1876 /* oct numbers have always the same format */
1877 pNumprs->nPwr10=0;
1878 pNumprs->nBaseShift=3;
1879 } else {
1880 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1882 pNumprs->nPwr10++;
1883 pNumprs->cDig--;
1887 } else
1889 /* Remove trailing zeros from the last (whole number or decimal) part */
1890 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1892 pNumprs->nPwr10++;
1893 pNumprs->cDig--;
1897 if (pNumprs->cDig <= iMaxDigits)
1898 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1899 else
1900 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1902 /* Copy the digits we processed into rgbDig */
1903 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1905 /* Consume any trailing symbols and space */
1906 while (1)
1908 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1910 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1913 cchUsed++;
1914 lpszStr++;
1915 } while (isspaceW(*lpszStr));
1917 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1918 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1919 *lpszStr == chars.cPositiveSymbol)
1921 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1922 cchUsed++;
1923 lpszStr++;
1925 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1926 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1927 *lpszStr == chars.cNegativeSymbol)
1929 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1930 cchUsed++;
1931 lpszStr++;
1933 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1934 pNumprs->dwOutFlags & NUMPRS_PARENS)
1936 cchUsed++;
1937 lpszStr++;
1938 pNumprs->dwOutFlags |= NUMPRS_NEG;
1940 else
1941 break;
1944 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1946 pNumprs->cchUsed = cchUsed;
1947 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1950 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1951 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1953 if (!pNumprs->cDig)
1954 return DISP_E_TYPEMISMATCH; /* No Number found */
1956 pNumprs->cchUsed = cchUsed;
1957 return S_OK;
1960 /* VTBIT flags indicating an integer value */
1961 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1962 /* VTBIT flags indicating a real number value */
1963 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1965 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1966 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1967 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1968 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1970 /**********************************************************************
1971 * VarNumFromParseNum [OLEAUT32.47]
1973 * Convert a NUMPARSE structure into a numeric Variant type.
1975 * PARAMS
1976 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1977 * rgbDig [I] Source for the numbers digits
1978 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1979 * pVarDst [O] Destination for the converted Variant value.
1981 * RETURNS
1982 * Success: S_OK. pVarDst contains the converted value.
1983 * Failure: E_INVALIDARG, if any parameter is invalid.
1984 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1986 * NOTES
1987 * - The smallest favoured type present in dwVtBits that can represent the
1988 * number in pNumprs without losing precision is used.
1989 * - Signed types are preferred over unsigned types of the same size.
1990 * - Preferred types in order are: integer, float, double, currency then decimal.
1991 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1992 * for details of the rounding method.
1993 * - pVarDst is not cleared before the result is stored in it.
1994 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1995 * design?): If some other VTBIT's for integers are specified together
1996 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1997 * the number to the smallest requested integer truncating this way the
1998 * number. Wine doesn't implement this "feature" (yet?).
2000 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
2001 ULONG dwVtBits, VARIANT *pVarDst)
2003 /* Scale factors and limits for double arithmetic */
2004 static const double dblMultipliers[11] = {
2005 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2006 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2008 static const double dblMinimums[11] = {
2009 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
2010 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
2011 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
2013 static const double dblMaximums[11] = {
2014 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
2015 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
2016 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
2019 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
2021 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
2023 if (pNumprs->nBaseShift)
2025 /* nBaseShift indicates a hex or octal number */
2026 ULONG64 ul64 = 0;
2027 LONG64 l64;
2028 int i;
2030 /* Convert the hex or octal number string into a UI64 */
2031 for (i = 0; i < pNumprs->cDig; i++)
2033 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
2035 TRACE("Overflow multiplying digits\n");
2036 return DISP_E_OVERFLOW;
2038 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
2041 /* also make a negative representation */
2042 l64=-ul64;
2044 /* Try signed and unsigned types in size order */
2045 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
2047 V_VT(pVarDst) = VT_I1;
2048 V_I1(pVarDst) = ul64;
2049 return S_OK;
2051 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2053 V_VT(pVarDst) = VT_UI1;
2054 V_UI1(pVarDst) = ul64;
2055 return S_OK;
2057 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2059 V_VT(pVarDst) = VT_I2;
2060 V_I2(pVarDst) = ul64;
2061 return S_OK;
2063 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2065 V_VT(pVarDst) = VT_UI2;
2066 V_UI2(pVarDst) = ul64;
2067 return S_OK;
2069 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2071 V_VT(pVarDst) = VT_I4;
2072 V_I4(pVarDst) = ul64;
2073 return S_OK;
2075 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2077 V_VT(pVarDst) = VT_UI4;
2078 V_UI4(pVarDst) = ul64;
2079 return S_OK;
2081 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2083 V_VT(pVarDst) = VT_I8;
2084 V_I8(pVarDst) = ul64;
2085 return S_OK;
2087 else if (dwVtBits & VTBIT_UI8)
2089 V_VT(pVarDst) = VT_UI8;
2090 V_UI8(pVarDst) = ul64;
2091 return S_OK;
2093 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2095 V_VT(pVarDst) = VT_DECIMAL;
2096 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2097 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2098 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2099 return S_OK;
2101 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2103 V_VT(pVarDst) = VT_R4;
2104 if (ul64 <= I4_MAX)
2105 V_R4(pVarDst) = ul64;
2106 else
2107 V_R4(pVarDst) = l64;
2108 return S_OK;
2110 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2112 V_VT(pVarDst) = VT_R8;
2113 if (ul64 <= I4_MAX)
2114 V_R8(pVarDst) = ul64;
2115 else
2116 V_R8(pVarDst) = l64;
2117 return S_OK;
2120 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2121 return DISP_E_OVERFLOW;
2124 /* Count the number of relevant fractional and whole digits stored,
2125 * And compute the divisor/multiplier to scale the number by.
2127 if (pNumprs->nPwr10 < 0)
2129 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2131 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2132 wholeNumberDigits = 0;
2133 fractionalDigits = pNumprs->cDig;
2134 divisor10 = -pNumprs->nPwr10;
2136 else
2138 /* An exactly represented real number e.g. 1.024 */
2139 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2140 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2141 divisor10 = pNumprs->cDig - wholeNumberDigits;
2144 else if (pNumprs->nPwr10 == 0)
2146 /* An exactly represented whole number e.g. 1024 */
2147 wholeNumberDigits = pNumprs->cDig;
2148 fractionalDigits = 0;
2150 else /* pNumprs->nPwr10 > 0 */
2152 /* A whole number followed by nPwr10 0's e.g. 102400 */
2153 wholeNumberDigits = pNumprs->cDig;
2154 fractionalDigits = 0;
2155 multiplier10 = pNumprs->nPwr10;
2158 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2159 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2160 multiplier10, divisor10);
2162 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2163 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2165 /* We have one or more integer output choices, and either:
2166 * 1) An integer input value, or
2167 * 2) A real number input value but no floating output choices.
2168 * Alternately, we have a DECIMAL output available and an integer input.
2170 * So, place the integer value into pVarDst, using the smallest type
2171 * possible and preferring signed over unsigned types.
2173 BOOL bOverflow = FALSE, bNegative;
2174 ULONG64 ul64 = 0;
2175 int i;
2177 /* Convert the integer part of the number into a UI8 */
2178 for (i = 0; i < wholeNumberDigits; i++)
2180 if (ul64 > UI8_MAX / 10 || (ul64 == UI8_MAX / 10 && rgbDig[i] > UI8_MAX % 10))
2182 TRACE("Overflow multiplying digits\n");
2183 bOverflow = TRUE;
2184 break;
2186 ul64 = ul64 * 10 + rgbDig[i];
2189 /* Account for the scale of the number */
2190 if (!bOverflow && multiplier10)
2192 for (i = 0; i < multiplier10; i++)
2194 if (ul64 > (UI8_MAX / 10))
2196 TRACE("Overflow scaling number\n");
2197 bOverflow = TRUE;
2198 break;
2200 ul64 = ul64 * 10;
2204 /* If we have any fractional digits, round the value.
2205 * Note we don't have to do this if divisor10 is < 1,
2206 * because this means the fractional part must be < 0.5
2208 if (!bOverflow && fractionalDigits && divisor10 > 0)
2210 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2211 BOOL bAdjust = FALSE;
2213 TRACE("first decimal value is %d\n", *fracDig);
2215 if (*fracDig > 5)
2216 bAdjust = TRUE; /* > 0.5 */
2217 else if (*fracDig == 5)
2219 for (i = 1; i < fractionalDigits; i++)
2221 if (fracDig[i])
2223 bAdjust = TRUE; /* > 0.5 */
2224 break;
2227 /* If exactly 0.5, round only odd values */
2228 if (i == fractionalDigits && (ul64 & 1))
2229 bAdjust = TRUE;
2232 if (bAdjust)
2234 if (ul64 == UI8_MAX)
2236 TRACE("Overflow after rounding\n");
2237 bOverflow = TRUE;
2239 ul64++;
2243 /* Zero is not a negative number */
2244 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64;
2246 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2248 /* For negative integers, try the signed types in size order */
2249 if (!bOverflow && bNegative)
2251 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2253 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2255 V_VT(pVarDst) = VT_I1;
2256 V_I1(pVarDst) = -ul64;
2257 return S_OK;
2259 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2261 V_VT(pVarDst) = VT_I2;
2262 V_I2(pVarDst) = -ul64;
2263 return S_OK;
2265 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2267 V_VT(pVarDst) = VT_I4;
2268 V_I4(pVarDst) = -ul64;
2269 return S_OK;
2271 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2273 V_VT(pVarDst) = VT_I8;
2274 V_I8(pVarDst) = -ul64;
2275 return S_OK;
2277 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2279 /* Decimal is only output choice left - fast path */
2280 V_VT(pVarDst) = VT_DECIMAL;
2281 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2282 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2283 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2284 return S_OK;
2288 else if (!bOverflow)
2290 /* For positive integers, try signed then unsigned types in size order */
2291 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2293 V_VT(pVarDst) = VT_I1;
2294 V_I1(pVarDst) = ul64;
2295 return S_OK;
2297 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2299 V_VT(pVarDst) = VT_UI1;
2300 V_UI1(pVarDst) = ul64;
2301 return S_OK;
2303 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2305 V_VT(pVarDst) = VT_I2;
2306 V_I2(pVarDst) = ul64;
2307 return S_OK;
2309 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2311 V_VT(pVarDst) = VT_UI2;
2312 V_UI2(pVarDst) = ul64;
2313 return S_OK;
2315 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2317 V_VT(pVarDst) = VT_I4;
2318 V_I4(pVarDst) = ul64;
2319 return S_OK;
2321 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2323 V_VT(pVarDst) = VT_UI4;
2324 V_UI4(pVarDst) = ul64;
2325 return S_OK;
2327 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2329 V_VT(pVarDst) = VT_I8;
2330 V_I8(pVarDst) = ul64;
2331 return S_OK;
2333 else if (dwVtBits & VTBIT_UI8)
2335 V_VT(pVarDst) = VT_UI8;
2336 V_UI8(pVarDst) = ul64;
2337 return S_OK;
2339 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2341 /* Decimal is only output choice left - fast path */
2342 V_VT(pVarDst) = VT_DECIMAL;
2343 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2344 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2345 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2346 return S_OK;
2351 if (dwVtBits & REAL_VTBITS)
2353 /* Try to put the number into a float or real */
2354 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2355 double whole = 0.0;
2356 int i;
2358 /* Convert the number into a double */
2359 for (i = 0; i < pNumprs->cDig; i++)
2360 whole = whole * 10.0 + rgbDig[i];
2362 TRACE("Whole double value is %16.16g\n", whole);
2364 /* Account for the scale */
2365 while (multiplier10 > 10)
2367 if (whole > dblMaximums[10])
2369 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2370 bOverflow = TRUE;
2371 break;
2373 whole = whole * dblMultipliers[10];
2374 multiplier10 -= 10;
2376 if (multiplier10 && !bOverflow)
2378 if (whole > dblMaximums[multiplier10])
2380 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2381 bOverflow = TRUE;
2383 else
2384 whole = whole * dblMultipliers[multiplier10];
2387 if (!bOverflow)
2388 TRACE("Scaled double value is %16.16g\n", whole);
2390 while (divisor10 > 10 && !bOverflow)
2392 if (whole < dblMinimums[10] && whole != 0)
2394 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2395 bOverflow = TRUE;
2396 break;
2398 whole = whole / dblMultipliers[10];
2399 divisor10 -= 10;
2401 if (divisor10 && !bOverflow)
2403 if (whole < dblMinimums[divisor10] && whole != 0)
2405 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2406 bOverflow = TRUE;
2408 else
2409 whole = whole / dblMultipliers[divisor10];
2411 if (!bOverflow)
2412 TRACE("Final double value is %16.16g\n", whole);
2414 if (dwVtBits & VTBIT_R4 &&
2415 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2417 TRACE("Set R4 to final value\n");
2418 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2419 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2420 return S_OK;
2423 if (dwVtBits & VTBIT_R8)
2425 TRACE("Set R8 to final value\n");
2426 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2427 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2428 return S_OK;
2431 if (dwVtBits & VTBIT_CY)
2433 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2435 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2436 TRACE("Set CY to final value\n");
2437 return S_OK;
2439 TRACE("Value Overflows CY\n");
2443 if (dwVtBits & VTBIT_DECIMAL)
2445 int i;
2446 ULONG carry;
2447 ULONG64 tmp;
2448 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2450 DECIMAL_SETZERO(*pDec);
2451 DEC_LO32(pDec) = 0;
2453 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2454 DEC_SIGN(pDec) = DECIMAL_NEG;
2455 else
2456 DEC_SIGN(pDec) = DECIMAL_POS;
2458 /* Factor the significant digits */
2459 for (i = 0; i < pNumprs->cDig; i++)
2461 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2462 carry = (ULONG)(tmp >> 32);
2463 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2464 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2465 carry = (ULONG)(tmp >> 32);
2466 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2467 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2468 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2470 if (tmp >> 32 & UI4_MAX)
2472 VarNumFromParseNum_DecOverflow:
2473 TRACE("Overflow\n");
2474 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2475 return DISP_E_OVERFLOW;
2479 /* Account for the scale of the number */
2480 while (multiplier10 > 0)
2482 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2483 carry = (ULONG)(tmp >> 32);
2484 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2485 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2486 carry = (ULONG)(tmp >> 32);
2487 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2488 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2489 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2491 if (tmp >> 32 & UI4_MAX)
2492 goto VarNumFromParseNum_DecOverflow;
2493 multiplier10--;
2495 DEC_SCALE(pDec) = divisor10;
2497 V_VT(pVarDst) = VT_DECIMAL;
2498 return S_OK;
2500 return DISP_E_OVERFLOW; /* No more output choices */
2503 /**********************************************************************
2504 * VarCat [OLEAUT32.318]
2506 * Concatenates one variant onto another.
2508 * PARAMS
2509 * left [I] First variant
2510 * right [I] Second variant
2511 * result [O] Result variant
2513 * RETURNS
2514 * Success: S_OK.
2515 * Failure: An HRESULT error code indicating the error.
2517 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2519 VARTYPE leftvt,rightvt,resultvt;
2520 HRESULT hres;
2521 static WCHAR str_true[32];
2522 static WCHAR str_false[32];
2523 static const WCHAR sz_empty[] = {'\0'};
2524 leftvt = V_VT(left);
2525 rightvt = V_VT(right);
2527 TRACE("%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), out);
2529 if (!str_true[0]) {
2530 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_FALSE, str_false);
2531 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_TRUE, str_true);
2534 /* when both left and right are NULL the result is NULL */
2535 if (leftvt == VT_NULL && rightvt == VT_NULL)
2537 V_VT(out) = VT_NULL;
2538 return S_OK;
2541 hres = S_OK;
2542 resultvt = VT_EMPTY;
2544 /* There are many special case for errors and return types */
2545 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2546 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2547 hres = DISP_E_TYPEMISMATCH;
2548 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2549 leftvt == VT_R4 || leftvt == VT_R8 ||
2550 leftvt == VT_CY || leftvt == VT_BOOL ||
2551 leftvt == VT_BSTR || leftvt == VT_I1 ||
2552 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2553 leftvt == VT_UI4 || leftvt == VT_I8 ||
2554 leftvt == VT_UI8 || leftvt == VT_INT ||
2555 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2556 leftvt == VT_NULL || leftvt == VT_DATE ||
2557 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2559 (rightvt == VT_I2 || rightvt == VT_I4 ||
2560 rightvt == VT_R4 || rightvt == VT_R8 ||
2561 rightvt == VT_CY || rightvt == VT_BOOL ||
2562 rightvt == VT_BSTR || rightvt == VT_I1 ||
2563 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2564 rightvt == VT_UI4 || rightvt == VT_I8 ||
2565 rightvt == VT_UI8 || rightvt == VT_INT ||
2566 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2567 rightvt == VT_NULL || rightvt == VT_DATE ||
2568 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2569 resultvt = VT_BSTR;
2570 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2571 hres = DISP_E_TYPEMISMATCH;
2572 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2573 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2574 hres = DISP_E_TYPEMISMATCH;
2575 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2576 rightvt == VT_DECIMAL)
2577 hres = DISP_E_BADVARTYPE;
2578 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2579 hres = DISP_E_TYPEMISMATCH;
2580 else if (leftvt == VT_VARIANT)
2581 hres = DISP_E_TYPEMISMATCH;
2582 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2583 leftvt == VT_NULL || leftvt == VT_I2 ||
2584 leftvt == VT_I4 || leftvt == VT_R4 ||
2585 leftvt == VT_R8 || leftvt == VT_CY ||
2586 leftvt == VT_DATE || leftvt == VT_BSTR ||
2587 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2588 leftvt == VT_I1 || leftvt == VT_UI1 ||
2589 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2590 leftvt == VT_I8 || leftvt == VT_UI8 ||
2591 leftvt == VT_INT || leftvt == VT_UINT))
2592 hres = DISP_E_TYPEMISMATCH;
2593 else
2594 hres = DISP_E_BADVARTYPE;
2596 /* if result type is not S_OK, then no need to go further */
2597 if (hres != S_OK)
2599 V_VT(out) = resultvt;
2600 return hres;
2602 /* Else proceed with formatting inputs to strings */
2603 else
2605 VARIANT bstrvar_left, bstrvar_right;
2606 V_VT(out) = VT_BSTR;
2608 VariantInit(&bstrvar_left);
2609 VariantInit(&bstrvar_right);
2611 /* Convert left side variant to string */
2612 if (leftvt != VT_BSTR)
2614 if (leftvt == VT_BOOL)
2616 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2617 V_VT(&bstrvar_left) = VT_BSTR;
2618 if (V_BOOL(left))
2619 V_BSTR(&bstrvar_left) = SysAllocString(str_true);
2620 else
2621 V_BSTR(&bstrvar_left) = SysAllocString(str_false);
2623 /* Fill with empty string for later concat with right side */
2624 else if (leftvt == VT_NULL)
2626 V_VT(&bstrvar_left) = VT_BSTR;
2627 V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
2629 else
2631 hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
2632 if (hres != S_OK) {
2633 VariantClear(&bstrvar_left);
2634 VariantClear(&bstrvar_right);
2635 if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
2636 rightvt == VT_NULL || rightvt == VT_I2 ||
2637 rightvt == VT_I4 || rightvt == VT_R4 ||
2638 rightvt == VT_R8 || rightvt == VT_CY ||
2639 rightvt == VT_DATE || rightvt == VT_BSTR ||
2640 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
2641 rightvt == VT_I1 || rightvt == VT_UI1 ||
2642 rightvt == VT_UI2 || rightvt == VT_UI4 ||
2643 rightvt == VT_I8 || rightvt == VT_UI8 ||
2644 rightvt == VT_INT || rightvt == VT_UINT))
2645 return DISP_E_BADVARTYPE;
2646 return hres;
2651 /* convert right side variant to string */
2652 if (rightvt != VT_BSTR)
2654 if (rightvt == VT_BOOL)
2656 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2657 V_VT(&bstrvar_right) = VT_BSTR;
2658 if (V_BOOL(right))
2659 V_BSTR(&bstrvar_right) = SysAllocString(str_true);
2660 else
2661 V_BSTR(&bstrvar_right) = SysAllocString(str_false);
2663 /* Fill with empty string for later concat with right side */
2664 else if (rightvt == VT_NULL)
2666 V_VT(&bstrvar_right) = VT_BSTR;
2667 V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
2669 else
2671 hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
2672 if (hres != S_OK) {
2673 VariantClear(&bstrvar_left);
2674 VariantClear(&bstrvar_right);
2675 if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
2676 leftvt == VT_NULL || leftvt == VT_I2 ||
2677 leftvt == VT_I4 || leftvt == VT_R4 ||
2678 leftvt == VT_R8 || leftvt == VT_CY ||
2679 leftvt == VT_DATE || leftvt == VT_BSTR ||
2680 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2681 leftvt == VT_I1 || leftvt == VT_UI1 ||
2682 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2683 leftvt == VT_I8 || leftvt == VT_UI8 ||
2684 leftvt == VT_INT || leftvt == VT_UINT))
2685 return DISP_E_BADVARTYPE;
2686 return hres;
2691 /* Concat the resulting strings together */
2692 if (leftvt == VT_BSTR && rightvt == VT_BSTR)
2693 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2694 else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
2695 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2696 else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
2697 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
2698 else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
2699 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2701 VariantClear(&bstrvar_left);
2702 VariantClear(&bstrvar_right);
2703 return S_OK;
2708 /* Wrapper around VariantChangeTypeEx() which permits changing a
2709 variant with VT_RESERVED flag set. Needed by VarCmp. */
2710 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2711 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2713 VARIANTARG vtmpsrc = *pvargSrc;
2715 V_VT(&vtmpsrc) &= ~VT_RESERVED;
2716 return VariantChangeTypeEx(pvargDest,&vtmpsrc,lcid,wFlags,vt);
2719 /**********************************************************************
2720 * VarCmp [OLEAUT32.176]
2722 * Compare two variants.
2724 * PARAMS
2725 * left [I] First variant
2726 * right [I] Second variant
2727 * lcid [I] LCID (locale identifier) for the comparison
2728 * flags [I] Flags to be used in the comparison:
2729 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2730 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2732 * RETURNS
2733 * VARCMP_LT: left variant is less than right variant.
2734 * VARCMP_EQ: input variants are equal.
2735 * VARCMP_GT: left variant is greater than right variant.
2736 * VARCMP_NULL: either one of the input variants is NULL.
2737 * Failure: An HRESULT error code indicating the error.
2739 * NOTES
2740 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2741 * UI8 and UINT as input variants. INT is accepted only as left variant.
2743 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2744 * an ERROR variant will trigger an error.
2746 * Both input variants can have VT_RESERVED flag set which is ignored
2747 * unless one and only one of the variants is a BSTR and the other one
2748 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2749 * different meaning:
2750 * - BSTR and other: BSTR is always greater than the other variant.
2751 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2752 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2753 * comparison will take place else the BSTR is always greater.
2754 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2755 * variant is ignored and the return value depends only on the sign
2756 * of the BSTR if it is a number else the BSTR is always greater. A
2757 * positive BSTR is greater, a negative one is smaller than the other
2758 * variant.
2760 * SEE
2761 * VarBstrCmp for the lcid and flags usage.
2763 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2765 VARTYPE lvt, rvt, vt;
2766 VARIANT rv,lv;
2767 DWORD xmask;
2768 HRESULT rc;
2770 TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left), debugstr_variant(right), lcid, flags);
2772 lvt = V_VT(left) & VT_TYPEMASK;
2773 rvt = V_VT(right) & VT_TYPEMASK;
2774 xmask = (1 << lvt) | (1 << rvt);
2776 /* If we have any flag set except VT_RESERVED bail out.
2777 Same for the left input variant type > VT_INT and for the
2778 right input variant type > VT_I8. Yes, VT_INT is only supported
2779 as left variant. Go figure */
2780 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2781 lvt > VT_INT || rvt > VT_I8) {
2782 return DISP_E_BADVARTYPE;
2785 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2786 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2787 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2788 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2789 return DISP_E_TYPEMISMATCH;
2791 /* If both variants are VT_ERROR return VARCMP_EQ */
2792 if (xmask == VTBIT_ERROR)
2793 return VARCMP_EQ;
2794 else if (xmask & VTBIT_ERROR)
2795 return DISP_E_TYPEMISMATCH;
2797 if (xmask & VTBIT_NULL)
2798 return VARCMP_NULL;
2800 VariantInit(&lv);
2801 VariantInit(&rv);
2803 /* Two BSTRs, ignore VT_RESERVED */
2804 if (xmask == VTBIT_BSTR)
2805 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2807 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2808 if (xmask & VTBIT_BSTR) {
2809 VARIANT *bstrv, *nonbv;
2810 VARTYPE nonbvt;
2811 int swap = 0;
2813 /* Swap the variants so the BSTR is always on the left */
2814 if (lvt == VT_BSTR) {
2815 bstrv = left;
2816 nonbv = right;
2817 nonbvt = rvt;
2818 } else {
2819 swap = 1;
2820 bstrv = right;
2821 nonbv = left;
2822 nonbvt = lvt;
2825 /* BSTR and EMPTY: ignore VT_RESERVED */
2826 if (nonbvt == VT_EMPTY)
2827 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2828 else {
2829 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2830 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2832 if (!breserv && !nreserv)
2833 /* No VT_RESERVED set ==> BSTR always greater */
2834 rc = VARCMP_GT;
2835 else if (breserv && !nreserv) {
2836 /* BSTR has VT_RESERVED set. Do a string comparison */
2837 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2838 if (FAILED(rc))
2839 return rc;
2840 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2841 VariantClear(&rv);
2842 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2843 /* Non NULL nor empty BSTR */
2844 /* If the BSTR is not a number the BSTR is greater */
2845 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2846 if (FAILED(rc))
2847 rc = VARCMP_GT;
2848 else if (breserv && nreserv)
2849 /* FIXME: This is strange: with both VT_RESERVED set it
2850 looks like the result depends only on the sign of
2851 the BSTR number */
2852 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2853 else
2854 /* Numeric comparison, will be handled below.
2855 VARCMP_NULL used only to break out. */
2856 rc = VARCMP_NULL;
2857 VariantClear(&lv);
2858 VariantClear(&rv);
2859 } else
2860 /* Empty or NULL BSTR */
2861 rc = VARCMP_GT;
2863 /* Fixup the return code if we swapped left and right */
2864 if (swap) {
2865 if (rc == VARCMP_GT)
2866 rc = VARCMP_LT;
2867 else if (rc == VARCMP_LT)
2868 rc = VARCMP_GT;
2870 if (rc != VARCMP_NULL)
2871 return rc;
2874 if (xmask & VTBIT_DECIMAL)
2875 vt = VT_DECIMAL;
2876 else if (xmask & VTBIT_BSTR)
2877 vt = VT_R8;
2878 else if (xmask & VTBIT_R4)
2879 vt = VT_R4;
2880 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2881 vt = VT_R8;
2882 else if (xmask & VTBIT_CY)
2883 vt = VT_CY;
2884 else
2885 /* default to I8 */
2886 vt = VT_I8;
2888 /* Coerce the variants */
2889 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2890 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2891 /* Overflow, change to R8 */
2892 vt = VT_R8;
2893 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2895 if (FAILED(rc))
2896 return rc;
2897 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2898 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2899 /* Overflow, change to R8 */
2900 vt = VT_R8;
2901 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2902 if (FAILED(rc))
2903 return rc;
2904 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2906 if (FAILED(rc))
2907 return rc;
2909 #define _VARCMP(a,b) \
2910 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2912 switch (vt) {
2913 case VT_CY:
2914 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2915 case VT_DECIMAL:
2916 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2917 case VT_I8:
2918 return _VARCMP(V_I8(&lv), V_I8(&rv));
2919 case VT_R4:
2920 return _VARCMP(V_R4(&lv), V_R4(&rv));
2921 case VT_R8:
2922 return _VARCMP(V_R8(&lv), V_R8(&rv));
2923 default:
2924 /* We should never get here */
2925 return E_FAIL;
2927 #undef _VARCMP
2930 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
2932 HRESULT hres;
2933 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
2935 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
2936 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
2937 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
2938 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
2939 NULL, NULL);
2940 } else {
2941 hres = DISP_E_TYPEMISMATCH;
2943 return hres;
2946 /**********************************************************************
2947 * VarAnd [OLEAUT32.142]
2949 * Computes the logical AND of two variants.
2951 * PARAMS
2952 * left [I] First variant
2953 * right [I] Second variant
2954 * result [O] Result variant
2956 * RETURNS
2957 * Success: S_OK.
2958 * Failure: An HRESULT error code indicating the error.
2960 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2962 HRESULT hres = S_OK;
2963 VARTYPE resvt = VT_EMPTY;
2964 VARTYPE leftvt,rightvt;
2965 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2966 VARIANT varLeft, varRight;
2967 VARIANT tempLeft, tempRight;
2969 VariantInit(&varLeft);
2970 VariantInit(&varRight);
2971 VariantInit(&tempLeft);
2972 VariantInit(&tempRight);
2974 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
2976 /* Handle VT_DISPATCH by storing and taking address of returned value */
2977 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2979 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2980 if (FAILED(hres)) goto VarAnd_Exit;
2981 left = &tempLeft;
2983 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2985 hres = VARIANT_FetchDispatchValue(right, &tempRight);
2986 if (FAILED(hres)) goto VarAnd_Exit;
2987 right = &tempRight;
2990 leftvt = V_VT(left)&VT_TYPEMASK;
2991 rightvt = V_VT(right)&VT_TYPEMASK;
2992 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
2993 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2995 if (leftExtraFlags != rightExtraFlags)
2997 hres = DISP_E_BADVARTYPE;
2998 goto VarAnd_Exit;
3000 ExtraFlags = leftExtraFlags;
3002 /* Native VarAnd always returns an error when using extra
3003 * flags or if the variant combination is I8 and INT.
3005 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
3006 (leftvt == VT_INT && rightvt == VT_I8) ||
3007 ExtraFlags != 0)
3009 hres = DISP_E_BADVARTYPE;
3010 goto VarAnd_Exit;
3013 /* Determine return type */
3014 else if (leftvt == VT_I8 || rightvt == VT_I8)
3015 resvt = VT_I8;
3016 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
3017 leftvt == VT_UINT || rightvt == VT_UINT ||
3018 leftvt == VT_INT || rightvt == VT_INT ||
3019 leftvt == VT_R4 || rightvt == VT_R4 ||
3020 leftvt == VT_R8 || rightvt == VT_R8 ||
3021 leftvt == VT_CY || rightvt == VT_CY ||
3022 leftvt == VT_DATE || rightvt == VT_DATE ||
3023 leftvt == VT_I1 || rightvt == VT_I1 ||
3024 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3025 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3026 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3027 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3028 resvt = VT_I4;
3029 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
3030 leftvt == VT_I2 || rightvt == VT_I2 ||
3031 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
3032 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
3033 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
3034 (leftvt == VT_UI1 && rightvt == VT_UI1))
3035 resvt = VT_UI1;
3036 else
3037 resvt = VT_I2;
3038 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
3039 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3040 resvt = VT_BOOL;
3041 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
3042 leftvt == VT_BSTR || rightvt == VT_BSTR)
3043 resvt = VT_NULL;
3044 else
3046 hres = DISP_E_BADVARTYPE;
3047 goto VarAnd_Exit;
3050 if (leftvt == VT_NULL || rightvt == VT_NULL)
3053 * Special cases for when left variant is VT_NULL
3054 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
3056 if (leftvt == VT_NULL)
3058 VARIANT_BOOL b;
3059 switch(rightvt)
3061 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3062 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3063 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3064 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3065 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3066 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3067 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3068 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3069 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3070 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3071 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3072 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3073 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3074 case VT_CY:
3075 if(V_CY(right).int64)
3076 resvt = VT_NULL;
3077 break;
3078 case VT_DECIMAL:
3079 if (DEC_HI32(&V_DECIMAL(right)) ||
3080 DEC_LO64(&V_DECIMAL(right)))
3081 resvt = VT_NULL;
3082 break;
3083 case VT_BSTR:
3084 hres = VarBoolFromStr(V_BSTR(right),
3085 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3086 if (FAILED(hres))
3087 return hres;
3088 else if (b)
3089 V_VT(result) = VT_NULL;
3090 else
3092 V_VT(result) = VT_BOOL;
3093 V_BOOL(result) = b;
3095 goto VarAnd_Exit;
3098 V_VT(result) = resvt;
3099 goto VarAnd_Exit;
3102 hres = VariantCopy(&varLeft, left);
3103 if (FAILED(hres)) goto VarAnd_Exit;
3105 hres = VariantCopy(&varRight, right);
3106 if (FAILED(hres)) goto VarAnd_Exit;
3108 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3109 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3110 else
3112 double d;
3114 if (V_VT(&varLeft) == VT_BSTR &&
3115 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3116 LOCALE_USER_DEFAULT, 0, &d)))
3117 hres = VariantChangeType(&varLeft,&varLeft,
3118 VARIANT_LOCALBOOL, VT_BOOL);
3119 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3120 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3121 if (FAILED(hres)) goto VarAnd_Exit;
3124 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3125 V_VT(&varRight) = VT_I4; /* Don't overflow */
3126 else
3128 double d;
3130 if (V_VT(&varRight) == VT_BSTR &&
3131 FAILED(VarR8FromStr(V_BSTR(&varRight),
3132 LOCALE_USER_DEFAULT, 0, &d)))
3133 hres = VariantChangeType(&varRight, &varRight,
3134 VARIANT_LOCALBOOL, VT_BOOL);
3135 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3136 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3137 if (FAILED(hres)) goto VarAnd_Exit;
3140 V_VT(result) = resvt;
3141 switch(resvt)
3143 case VT_I8:
3144 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3145 break;
3146 case VT_I4:
3147 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3148 break;
3149 case VT_I2:
3150 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3151 break;
3152 case VT_UI1:
3153 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3154 break;
3155 case VT_BOOL:
3156 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3157 break;
3158 default:
3159 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3160 leftvt,rightvt);
3163 VarAnd_Exit:
3164 VariantClear(&varLeft);
3165 VariantClear(&varRight);
3166 VariantClear(&tempLeft);
3167 VariantClear(&tempRight);
3169 return hres;
3172 /**********************************************************************
3173 * VarAdd [OLEAUT32.141]
3175 * Add two variants.
3177 * PARAMS
3178 * left [I] First variant
3179 * right [I] Second variant
3180 * result [O] Result variant
3182 * RETURNS
3183 * Success: S_OK.
3184 * Failure: An HRESULT error code indicating the error.
3186 * NOTES
3187 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3188 * UI8, INT and UINT as input variants.
3190 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3191 * same here.
3193 * FIXME
3194 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3195 * case.
3197 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3199 HRESULT hres;
3200 VARTYPE lvt, rvt, resvt, tvt;
3201 VARIANT lv, rv, tv;
3202 VARIANT tempLeft, tempRight;
3203 double r8res;
3205 /* Variant priority for coercion. Sorted from lowest to highest.
3206 VT_ERROR shows an invalid input variant type. */
3207 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3208 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3209 vt_ERROR };
3210 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3211 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3212 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3213 VT_NULL, VT_ERROR };
3215 /* Mapping for coercion from input variant to priority of result variant. */
3216 static const VARTYPE coerce[] = {
3217 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3218 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3219 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3220 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3221 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3222 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3223 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3224 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3227 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3229 VariantInit(&lv);
3230 VariantInit(&rv);
3231 VariantInit(&tv);
3232 VariantInit(&tempLeft);
3233 VariantInit(&tempRight);
3235 /* Handle VT_DISPATCH by storing and taking address of returned value */
3236 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3238 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3240 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3241 if (FAILED(hres)) goto end;
3242 left = &tempLeft;
3244 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3246 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3247 if (FAILED(hres)) goto end;
3248 right = &tempRight;
3252 lvt = V_VT(left)&VT_TYPEMASK;
3253 rvt = V_VT(right)&VT_TYPEMASK;
3255 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3256 Same for any input variant type > VT_I8 */
3257 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3258 lvt > VT_I8 || rvt > VT_I8) {
3259 hres = DISP_E_BADVARTYPE;
3260 goto end;
3263 /* Determine the variant type to coerce to. */
3264 if (coerce[lvt] > coerce[rvt]) {
3265 resvt = prio2vt[coerce[lvt]];
3266 tvt = prio2vt[coerce[rvt]];
3267 } else {
3268 resvt = prio2vt[coerce[rvt]];
3269 tvt = prio2vt[coerce[lvt]];
3272 /* Special cases where the result variant type is defined by both
3273 input variants and not only that with the highest priority */
3274 if (resvt == VT_BSTR) {
3275 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3276 resvt = VT_BSTR;
3277 else
3278 resvt = VT_R8;
3280 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3281 resvt = VT_R8;
3283 /* For overflow detection use the biggest compatible type for the
3284 addition */
3285 switch (resvt) {
3286 case VT_ERROR:
3287 hres = DISP_E_BADVARTYPE;
3288 goto end;
3289 case VT_NULL:
3290 hres = S_OK;
3291 V_VT(result) = VT_NULL;
3292 goto end;
3293 case VT_DISPATCH:
3294 FIXME("cannot handle variant type VT_DISPATCH\n");
3295 hres = DISP_E_TYPEMISMATCH;
3296 goto end;
3297 case VT_EMPTY:
3298 resvt = VT_I2;
3299 /* Fall through */
3300 case VT_UI1:
3301 case VT_I2:
3302 case VT_I4:
3303 case VT_I8:
3304 tvt = VT_I8;
3305 break;
3306 case VT_DATE:
3307 case VT_R4:
3308 tvt = VT_R8;
3309 break;
3310 default:
3311 tvt = resvt;
3314 /* Now coerce the variants */
3315 hres = VariantChangeType(&lv, left, 0, tvt);
3316 if (FAILED(hres))
3317 goto end;
3318 hres = VariantChangeType(&rv, right, 0, tvt);
3319 if (FAILED(hres))
3320 goto end;
3322 /* Do the math */
3323 hres = S_OK;
3324 V_VT(result) = resvt;
3325 switch (tvt) {
3326 case VT_DECIMAL:
3327 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3328 &V_DECIMAL(result));
3329 goto end;
3330 case VT_CY:
3331 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3332 goto end;
3333 case VT_BSTR:
3334 /* We do not add those, we concatenate them. */
3335 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3336 goto end;
3337 case VT_I8:
3338 /* Overflow detection */
3339 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3340 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3341 V_VT(result) = VT_R8;
3342 V_R8(result) = r8res;
3343 goto end;
3344 } else {
3345 V_VT(&tv) = tvt;
3346 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3348 break;
3349 case VT_R8:
3350 V_VT(&tv) = tvt;
3351 /* FIXME: overflow detection */
3352 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3353 break;
3354 default:
3355 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3356 break;
3358 if (resvt != tvt) {
3359 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3360 /* Overflow! Change to the vartype with the next higher priority.
3361 With one exception: I4 ==> R8 even if it would fit in I8 */
3362 if (resvt == VT_I4)
3363 resvt = VT_R8;
3364 else
3365 resvt = prio2vt[coerce[resvt] + 1];
3366 hres = VariantChangeType(result, &tv, 0, resvt);
3368 } else
3369 hres = VariantCopy(result, &tv);
3371 end:
3372 if (hres != S_OK) {
3373 V_VT(result) = VT_EMPTY;
3374 V_I4(result) = 0; /* No V_EMPTY */
3376 VariantClear(&lv);
3377 VariantClear(&rv);
3378 VariantClear(&tv);
3379 VariantClear(&tempLeft);
3380 VariantClear(&tempRight);
3381 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3382 return hres;
3385 /**********************************************************************
3386 * VarMul [OLEAUT32.156]
3388 * Multiply two variants.
3390 * PARAMS
3391 * left [I] First variant
3392 * right [I] Second variant
3393 * result [O] Result variant
3395 * RETURNS
3396 * Success: S_OK.
3397 * Failure: An HRESULT error code indicating the error.
3399 * NOTES
3400 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3401 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3403 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3404 * same here.
3406 * FIXME
3407 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3408 * case.
3410 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3412 HRESULT hres;
3413 VARTYPE lvt, rvt, resvt, tvt;
3414 VARIANT lv, rv, tv;
3415 VARIANT tempLeft, tempRight;
3416 double r8res;
3418 /* Variant priority for coercion. Sorted from lowest to highest.
3419 VT_ERROR shows an invalid input variant type. */
3420 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3421 vt_DECIMAL, vt_NULL, vt_ERROR };
3422 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3423 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3424 VT_DECIMAL, VT_NULL, VT_ERROR };
3426 /* Mapping for coercion from input variant to priority of result variant. */
3427 static const VARTYPE coerce[] = {
3428 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3429 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3430 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3431 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3432 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3433 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3434 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3435 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3438 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3440 VariantInit(&lv);
3441 VariantInit(&rv);
3442 VariantInit(&tv);
3443 VariantInit(&tempLeft);
3444 VariantInit(&tempRight);
3446 /* Handle VT_DISPATCH by storing and taking address of returned value */
3447 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3449 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3450 if (FAILED(hres)) goto end;
3451 left = &tempLeft;
3453 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3455 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3456 if (FAILED(hres)) goto end;
3457 right = &tempRight;
3460 lvt = V_VT(left)&VT_TYPEMASK;
3461 rvt = V_VT(right)&VT_TYPEMASK;
3463 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3464 Same for any input variant type > VT_I8 */
3465 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3466 lvt > VT_I8 || rvt > VT_I8) {
3467 hres = DISP_E_BADVARTYPE;
3468 goto end;
3471 /* Determine the variant type to coerce to. */
3472 if (coerce[lvt] > coerce[rvt]) {
3473 resvt = prio2vt[coerce[lvt]];
3474 tvt = prio2vt[coerce[rvt]];
3475 } else {
3476 resvt = prio2vt[coerce[rvt]];
3477 tvt = prio2vt[coerce[lvt]];
3480 /* Special cases where the result variant type is defined by both
3481 input variants and not only that with the highest priority */
3482 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3483 resvt = VT_R8;
3484 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3485 resvt = VT_I2;
3487 /* For overflow detection use the biggest compatible type for the
3488 multiplication */
3489 switch (resvt) {
3490 case VT_ERROR:
3491 hres = DISP_E_BADVARTYPE;
3492 goto end;
3493 case VT_NULL:
3494 hres = S_OK;
3495 V_VT(result) = VT_NULL;
3496 goto end;
3497 case VT_UI1:
3498 case VT_I2:
3499 case VT_I4:
3500 case VT_I8:
3501 tvt = VT_I8;
3502 break;
3503 case VT_R4:
3504 tvt = VT_R8;
3505 break;
3506 default:
3507 tvt = resvt;
3510 /* Now coerce the variants */
3511 hres = VariantChangeType(&lv, left, 0, tvt);
3512 if (FAILED(hres))
3513 goto end;
3514 hres = VariantChangeType(&rv, right, 0, tvt);
3515 if (FAILED(hres))
3516 goto end;
3518 /* Do the math */
3519 hres = S_OK;
3520 V_VT(&tv) = tvt;
3521 V_VT(result) = resvt;
3522 switch (tvt) {
3523 case VT_DECIMAL:
3524 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3525 &V_DECIMAL(result));
3526 goto end;
3527 case VT_CY:
3528 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3529 goto end;
3530 case VT_I8:
3531 /* Overflow detection */
3532 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3533 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3534 V_VT(result) = VT_R8;
3535 V_R8(result) = r8res;
3536 goto end;
3537 } else
3538 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3539 break;
3540 case VT_R8:
3541 /* FIXME: overflow detection */
3542 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3543 break;
3544 default:
3545 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3546 break;
3548 if (resvt != tvt) {
3549 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3550 /* Overflow! Change to the vartype with the next higher priority.
3551 With one exception: I4 ==> R8 even if it would fit in I8 */
3552 if (resvt == VT_I4)
3553 resvt = VT_R8;
3554 else
3555 resvt = prio2vt[coerce[resvt] + 1];
3557 } else
3558 hres = VariantCopy(result, &tv);
3560 end:
3561 if (hres != S_OK) {
3562 V_VT(result) = VT_EMPTY;
3563 V_I4(result) = 0; /* No V_EMPTY */
3565 VariantClear(&lv);
3566 VariantClear(&rv);
3567 VariantClear(&tv);
3568 VariantClear(&tempLeft);
3569 VariantClear(&tempRight);
3570 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3571 return hres;
3574 /**********************************************************************
3575 * VarDiv [OLEAUT32.143]
3577 * Divides one variant with another.
3579 * PARAMS
3580 * left [I] First variant
3581 * right [I] Second variant
3582 * result [O] Result variant
3584 * RETURNS
3585 * Success: S_OK.
3586 * Failure: An HRESULT error code indicating the error.
3588 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3590 HRESULT hres = S_OK;
3591 VARTYPE resvt = VT_EMPTY;
3592 VARTYPE leftvt,rightvt;
3593 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3594 VARIANT lv,rv;
3595 VARIANT tempLeft, tempRight;
3597 VariantInit(&tempLeft);
3598 VariantInit(&tempRight);
3599 VariantInit(&lv);
3600 VariantInit(&rv);
3602 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3604 /* Handle VT_DISPATCH by storing and taking address of returned value */
3605 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3607 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3608 if (FAILED(hres)) goto end;
3609 left = &tempLeft;
3611 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3613 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3614 if (FAILED(hres)) goto end;
3615 right = &tempRight;
3618 leftvt = V_VT(left)&VT_TYPEMASK;
3619 rightvt = V_VT(right)&VT_TYPEMASK;
3620 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3621 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3623 if (leftExtraFlags != rightExtraFlags)
3625 hres = DISP_E_BADVARTYPE;
3626 goto end;
3628 ExtraFlags = leftExtraFlags;
3630 /* Native VarDiv always returns an error when using extra flags */
3631 if (ExtraFlags != 0)
3633 hres = DISP_E_BADVARTYPE;
3634 goto end;
3637 /* Determine return type */
3638 if (!(rightvt == VT_EMPTY))
3640 if (leftvt == VT_NULL || rightvt == VT_NULL)
3642 V_VT(result) = VT_NULL;
3643 hres = S_OK;
3644 goto end;
3646 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3647 resvt = VT_DECIMAL;
3648 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3649 leftvt == VT_CY || rightvt == VT_CY ||
3650 leftvt == VT_DATE || rightvt == VT_DATE ||
3651 leftvt == VT_I4 || rightvt == VT_I4 ||
3652 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3653 leftvt == VT_I2 || rightvt == VT_I2 ||
3654 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3655 leftvt == VT_R8 || rightvt == VT_R8 ||
3656 leftvt == VT_UI1 || rightvt == VT_UI1)
3658 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3659 (leftvt == VT_R4 && rightvt == VT_UI1))
3660 resvt = VT_R4;
3661 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3662 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3663 (leftvt == VT_BOOL || leftvt == VT_I2)))
3664 resvt = VT_R4;
3665 else
3666 resvt = VT_R8;
3668 else if (leftvt == VT_R4 || rightvt == VT_R4)
3669 resvt = VT_R4;
3671 else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
3673 V_VT(result) = VT_NULL;
3674 hres = S_OK;
3675 goto end;
3677 else
3679 hres = DISP_E_BADVARTYPE;
3680 goto end;
3683 /* coerce to the result type */
3684 hres = VariantChangeType(&lv, left, 0, resvt);
3685 if (hres != S_OK) goto end;
3687 hres = VariantChangeType(&rv, right, 0, resvt);
3688 if (hres != S_OK) goto end;
3690 /* do the math */
3691 V_VT(result) = resvt;
3692 switch (resvt)
3694 case VT_R4:
3695 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3697 hres = DISP_E_OVERFLOW;
3698 V_VT(result) = VT_EMPTY;
3700 else if (V_R4(&rv) == 0.0)
3702 hres = DISP_E_DIVBYZERO;
3703 V_VT(result) = VT_EMPTY;
3705 else
3706 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3707 break;
3708 case VT_R8:
3709 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3711 hres = DISP_E_OVERFLOW;
3712 V_VT(result) = VT_EMPTY;
3714 else if (V_R8(&rv) == 0.0)
3716 hres = DISP_E_DIVBYZERO;
3717 V_VT(result) = VT_EMPTY;
3719 else
3720 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3721 break;
3722 case VT_DECIMAL:
3723 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3724 break;
3727 end:
3728 VariantClear(&lv);
3729 VariantClear(&rv);
3730 VariantClear(&tempLeft);
3731 VariantClear(&tempRight);
3732 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3733 return hres;
3736 /**********************************************************************
3737 * VarSub [OLEAUT32.159]
3739 * Subtract two variants.
3741 * PARAMS
3742 * left [I] First variant
3743 * right [I] Second variant
3744 * result [O] Result variant
3746 * RETURNS
3747 * Success: S_OK.
3748 * Failure: An HRESULT error code indicating the error.
3750 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3752 HRESULT hres = S_OK;
3753 VARTYPE resvt = VT_EMPTY;
3754 VARTYPE leftvt,rightvt;
3755 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3756 VARIANT lv,rv;
3757 VARIANT tempLeft, tempRight;
3759 VariantInit(&lv);
3760 VariantInit(&rv);
3761 VariantInit(&tempLeft);
3762 VariantInit(&tempRight);
3764 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3766 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3767 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3768 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3770 if (NULL == V_DISPATCH(left)) {
3771 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3772 hres = DISP_E_BADVARTYPE;
3773 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3774 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3775 hres = DISP_E_BADVARTYPE;
3776 else switch (V_VT(right) & VT_TYPEMASK)
3778 case VT_VARIANT:
3779 case VT_UNKNOWN:
3780 case 15:
3781 case VT_I1:
3782 case VT_UI2:
3783 case VT_UI4:
3784 hres = DISP_E_BADVARTYPE;
3786 if (FAILED(hres)) goto end;
3788 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3789 if (FAILED(hres)) goto end;
3790 left = &tempLeft;
3792 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3793 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3794 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3796 if (NULL == V_DISPATCH(right))
3798 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3799 hres = DISP_E_BADVARTYPE;
3800 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3801 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3802 hres = DISP_E_BADVARTYPE;
3803 else switch (V_VT(left) & VT_TYPEMASK)
3805 case VT_VARIANT:
3806 case VT_UNKNOWN:
3807 case 15:
3808 case VT_I1:
3809 case VT_UI2:
3810 case VT_UI4:
3811 hres = DISP_E_BADVARTYPE;
3813 if (FAILED(hres)) goto end;
3815 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3816 if (FAILED(hres)) goto end;
3817 right = &tempRight;
3820 leftvt = V_VT(left)&VT_TYPEMASK;
3821 rightvt = V_VT(right)&VT_TYPEMASK;
3822 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3823 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3825 if (leftExtraFlags != rightExtraFlags)
3827 hres = DISP_E_BADVARTYPE;
3828 goto end;
3830 ExtraFlags = leftExtraFlags;
3832 /* determine return type and return code */
3833 /* All extra flags produce errors */
3834 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3835 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3836 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3837 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3838 ExtraFlags == VT_VECTOR ||
3839 ExtraFlags == VT_BYREF ||
3840 ExtraFlags == VT_RESERVED)
3842 hres = DISP_E_BADVARTYPE;
3843 goto end;
3845 else if (ExtraFlags >= VT_ARRAY)
3847 hres = DISP_E_TYPEMISMATCH;
3848 goto end;
3850 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3851 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3852 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3853 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3854 leftvt == VT_I1 || rightvt == VT_I1 ||
3855 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3856 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3857 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3858 leftvt == VT_INT || rightvt == VT_INT ||
3859 leftvt == VT_UINT || rightvt == VT_UINT ||
3860 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3861 leftvt == VT_RECORD || rightvt == VT_RECORD)
3863 if (leftvt == VT_RECORD && rightvt == VT_I8)
3864 hres = DISP_E_TYPEMISMATCH;
3865 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3866 hres = DISP_E_TYPEMISMATCH;
3867 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3868 hres = DISP_E_TYPEMISMATCH;
3869 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3870 hres = DISP_E_TYPEMISMATCH;
3871 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3872 hres = DISP_E_BADVARTYPE;
3873 else
3874 hres = DISP_E_BADVARTYPE;
3875 goto end;
3877 /* The following flags/types are invalid for left variant */
3878 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3879 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3880 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3882 hres = DISP_E_BADVARTYPE;
3883 goto end;
3885 /* The following flags/types are invalid for right variant */
3886 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3887 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3888 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3890 hres = DISP_E_BADVARTYPE;
3891 goto end;
3893 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3894 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3895 resvt = VT_NULL;
3896 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3897 leftvt == VT_ERROR || rightvt == VT_ERROR)
3899 hres = DISP_E_TYPEMISMATCH;
3900 goto end;
3902 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3903 resvt = VT_NULL;
3904 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3905 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3906 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3907 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3908 resvt = VT_R8;
3909 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3910 resvt = VT_DECIMAL;
3911 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3912 resvt = VT_DATE;
3913 else if (leftvt == VT_CY || rightvt == VT_CY)
3914 resvt = VT_CY;
3915 else if (leftvt == VT_R8 || rightvt == VT_R8)
3916 resvt = VT_R8;
3917 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3918 resvt = VT_R8;
3919 else if (leftvt == VT_R4 || rightvt == VT_R4)
3921 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3922 leftvt == VT_I8 || rightvt == VT_I8)
3923 resvt = VT_R8;
3924 else
3925 resvt = VT_R4;
3927 else if (leftvt == VT_I8 || rightvt == VT_I8)
3928 resvt = VT_I8;
3929 else if (leftvt == VT_I4 || rightvt == VT_I4)
3930 resvt = VT_I4;
3931 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3932 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3933 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3934 resvt = VT_I2;
3935 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3936 resvt = VT_UI1;
3937 else
3939 hres = DISP_E_TYPEMISMATCH;
3940 goto end;
3943 /* coerce to the result type */
3944 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3945 hres = VariantChangeType(&lv, left, 0, VT_R8);
3946 else
3947 hres = VariantChangeType(&lv, left, 0, resvt);
3948 if (hres != S_OK) goto end;
3949 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3950 hres = VariantChangeType(&rv, right, 0, VT_R8);
3951 else
3952 hres = VariantChangeType(&rv, right, 0, resvt);
3953 if (hres != S_OK) goto end;
3955 /* do the math */
3956 V_VT(result) = resvt;
3957 switch (resvt)
3959 case VT_NULL:
3960 break;
3961 case VT_DATE:
3962 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3963 break;
3964 case VT_CY:
3965 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3966 break;
3967 case VT_R4:
3968 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3969 break;
3970 case VT_I8:
3971 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3972 break;
3973 case VT_I4:
3974 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3975 break;
3976 case VT_I2:
3977 V_I2(result) = V_I2(&lv) - V_I2(&rv);
3978 break;
3979 case VT_I1:
3980 V_I1(result) = V_I1(&lv) - V_I1(&rv);
3981 break;
3982 case VT_UI1:
3983 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
3984 break;
3985 case VT_R8:
3986 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3987 break;
3988 case VT_DECIMAL:
3989 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3990 break;
3993 end:
3994 VariantClear(&lv);
3995 VariantClear(&rv);
3996 VariantClear(&tempLeft);
3997 VariantClear(&tempRight);
3998 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3999 return hres;
4003 /**********************************************************************
4004 * VarOr [OLEAUT32.157]
4006 * Perform a logical or (OR) operation on two variants.
4008 * PARAMS
4009 * pVarLeft [I] First variant
4010 * pVarRight [I] Variant to OR with pVarLeft
4011 * pVarOut [O] Destination for OR result
4013 * RETURNS
4014 * Success: S_OK. pVarOut contains the result of the operation with its type
4015 * taken from the table listed under VarXor().
4016 * Failure: An HRESULT error code indicating the error.
4018 * NOTES
4019 * See the Notes section of VarXor() for further information.
4021 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4023 VARTYPE vt = VT_I4;
4024 VARIANT varLeft, varRight, varStr;
4025 HRESULT hRet;
4026 VARIANT tempLeft, tempRight;
4028 VariantInit(&tempLeft);
4029 VariantInit(&tempRight);
4030 VariantInit(&varLeft);
4031 VariantInit(&varRight);
4032 VariantInit(&varStr);
4034 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4036 /* Handle VT_DISPATCH by storing and taking address of returned value */
4037 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4039 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4040 if (FAILED(hRet)) goto VarOr_Exit;
4041 pVarLeft = &tempLeft;
4043 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4045 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4046 if (FAILED(hRet)) goto VarOr_Exit;
4047 pVarRight = &tempRight;
4050 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4051 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4052 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
4053 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
4055 hRet = DISP_E_BADVARTYPE;
4056 goto VarOr_Exit;
4059 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
4061 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4063 /* NULL OR Zero is NULL, NULL OR value is value */
4064 if (V_VT(pVarLeft) == VT_NULL)
4065 pVarLeft = pVarRight; /* point to the non-NULL var */
4067 V_VT(pVarOut) = VT_NULL;
4068 V_I4(pVarOut) = 0;
4070 switch (V_VT(pVarLeft))
4072 case VT_DATE: case VT_R8:
4073 if (V_R8(pVarLeft))
4074 goto VarOr_AsEmpty;
4075 hRet = S_OK;
4076 goto VarOr_Exit;
4077 case VT_BOOL:
4078 if (V_BOOL(pVarLeft))
4079 *pVarOut = *pVarLeft;
4080 hRet = S_OK;
4081 goto VarOr_Exit;
4082 case VT_I2: case VT_UI2:
4083 if (V_I2(pVarLeft))
4084 goto VarOr_AsEmpty;
4085 hRet = S_OK;
4086 goto VarOr_Exit;
4087 case VT_I1:
4088 if (V_I1(pVarLeft))
4089 goto VarOr_AsEmpty;
4090 hRet = S_OK;
4091 goto VarOr_Exit;
4092 case VT_UI1:
4093 if (V_UI1(pVarLeft))
4094 *pVarOut = *pVarLeft;
4095 hRet = S_OK;
4096 goto VarOr_Exit;
4097 case VT_R4:
4098 if (V_R4(pVarLeft))
4099 goto VarOr_AsEmpty;
4100 hRet = S_OK;
4101 goto VarOr_Exit;
4102 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4103 if (V_I4(pVarLeft))
4104 goto VarOr_AsEmpty;
4105 hRet = S_OK;
4106 goto VarOr_Exit;
4107 case VT_CY:
4108 if (V_CY(pVarLeft).int64)
4109 goto VarOr_AsEmpty;
4110 hRet = S_OK;
4111 goto VarOr_Exit;
4112 case VT_I8: case VT_UI8:
4113 if (V_I8(pVarLeft))
4114 goto VarOr_AsEmpty;
4115 hRet = S_OK;
4116 goto VarOr_Exit;
4117 case VT_DECIMAL:
4118 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4119 goto VarOr_AsEmpty;
4120 hRet = S_OK;
4121 goto VarOr_Exit;
4122 case VT_BSTR:
4124 VARIANT_BOOL b;
4126 if (!V_BSTR(pVarLeft))
4128 hRet = DISP_E_BADVARTYPE;
4129 goto VarOr_Exit;
4132 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4133 if (SUCCEEDED(hRet) && b)
4135 V_VT(pVarOut) = VT_BOOL;
4136 V_BOOL(pVarOut) = b;
4138 goto VarOr_Exit;
4140 case VT_NULL: case VT_EMPTY:
4141 V_VT(pVarOut) = VT_NULL;
4142 hRet = S_OK;
4143 goto VarOr_Exit;
4144 default:
4145 hRet = DISP_E_BADVARTYPE;
4146 goto VarOr_Exit;
4150 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4152 if (V_VT(pVarLeft) == VT_EMPTY)
4153 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4155 VarOr_AsEmpty:
4156 /* Since one argument is empty (0), OR'ing it with the other simply
4157 * gives the others value (as 0|x => x). So just convert the other
4158 * argument to the required result type.
4160 switch (V_VT(pVarLeft))
4162 case VT_BSTR:
4163 if (!V_BSTR(pVarLeft))
4165 hRet = DISP_E_BADVARTYPE;
4166 goto VarOr_Exit;
4169 hRet = VariantCopy(&varStr, pVarLeft);
4170 if (FAILED(hRet))
4171 goto VarOr_Exit;
4172 pVarLeft = &varStr;
4173 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4174 if (FAILED(hRet))
4175 goto VarOr_Exit;
4176 /* Fall Through ... */
4177 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4178 V_VT(pVarOut) = VT_I2;
4179 break;
4180 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4181 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4182 case VT_INT: case VT_UINT: case VT_UI8:
4183 V_VT(pVarOut) = VT_I4;
4184 break;
4185 case VT_I8:
4186 V_VT(pVarOut) = VT_I8;
4187 break;
4188 default:
4189 hRet = DISP_E_BADVARTYPE;
4190 goto VarOr_Exit;
4192 hRet = VariantCopy(&varLeft, pVarLeft);
4193 if (FAILED(hRet))
4194 goto VarOr_Exit;
4195 pVarLeft = &varLeft;
4196 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4197 goto VarOr_Exit;
4200 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4202 V_VT(pVarOut) = VT_BOOL;
4203 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4204 hRet = S_OK;
4205 goto VarOr_Exit;
4208 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4210 V_VT(pVarOut) = VT_UI1;
4211 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4212 hRet = S_OK;
4213 goto VarOr_Exit;
4216 if (V_VT(pVarLeft) == VT_BSTR)
4218 hRet = VariantCopy(&varStr, pVarLeft);
4219 if (FAILED(hRet))
4220 goto VarOr_Exit;
4221 pVarLeft = &varStr;
4222 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4223 if (FAILED(hRet))
4224 goto VarOr_Exit;
4227 if (V_VT(pVarLeft) == VT_BOOL &&
4228 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4230 vt = VT_BOOL;
4232 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4233 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4234 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4235 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4237 vt = VT_I2;
4239 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4241 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4243 hRet = DISP_E_TYPEMISMATCH;
4244 goto VarOr_Exit;
4246 vt = VT_I8;
4249 hRet = VariantCopy(&varLeft, pVarLeft);
4250 if (FAILED(hRet))
4251 goto VarOr_Exit;
4253 hRet = VariantCopy(&varRight, pVarRight);
4254 if (FAILED(hRet))
4255 goto VarOr_Exit;
4257 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4258 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4259 else
4261 double d;
4263 if (V_VT(&varLeft) == VT_BSTR &&
4264 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4265 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4266 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4267 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4268 if (FAILED(hRet))
4269 goto VarOr_Exit;
4272 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4273 V_VT(&varRight) = VT_I4; /* Don't overflow */
4274 else
4276 double d;
4278 if (V_VT(&varRight) == VT_BSTR &&
4279 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4280 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4281 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4282 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4283 if (FAILED(hRet))
4284 goto VarOr_Exit;
4287 V_VT(pVarOut) = vt;
4288 if (vt == VT_I8)
4290 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4292 else if (vt == VT_I4)
4294 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4296 else
4298 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4301 VarOr_Exit:
4302 VariantClear(&varStr);
4303 VariantClear(&varLeft);
4304 VariantClear(&varRight);
4305 VariantClear(&tempLeft);
4306 VariantClear(&tempRight);
4307 return hRet;
4310 /**********************************************************************
4311 * VarAbs [OLEAUT32.168]
4313 * Convert a variant to its absolute value.
4315 * PARAMS
4316 * pVarIn [I] Source variant
4317 * pVarOut [O] Destination for converted value
4319 * RETURNS
4320 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4321 * Failure: An HRESULT error code indicating the error.
4323 * NOTES
4324 * - This function does not process by-reference variants.
4325 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4326 * according to the following table:
4327 *| Input Type Output Type
4328 *| ---------- -----------
4329 *| VT_BOOL VT_I2
4330 *| VT_BSTR VT_R8
4331 *| (All others) Unchanged
4333 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4335 VARIANT varIn;
4336 HRESULT hRet = S_OK;
4337 VARIANT temp;
4339 VariantInit(&temp);
4341 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4343 /* Handle VT_DISPATCH by storing and taking address of returned value */
4344 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4346 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4347 if (FAILED(hRet)) goto VarAbs_Exit;
4348 pVarIn = &temp;
4351 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4352 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4353 V_VT(pVarIn) == VT_ERROR)
4355 hRet = DISP_E_TYPEMISMATCH;
4356 goto VarAbs_Exit;
4358 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4360 #define ABS_CASE(typ,min) \
4361 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4362 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4363 break
4365 switch (V_VT(pVarIn))
4367 ABS_CASE(I1,I1_MIN);
4368 case VT_BOOL:
4369 V_VT(pVarOut) = VT_I2;
4370 /* BOOL->I2, Fall through ... */
4371 ABS_CASE(I2,I2_MIN);
4372 case VT_INT:
4373 ABS_CASE(I4,I4_MIN);
4374 ABS_CASE(I8,I8_MIN);
4375 ABS_CASE(R4,R4_MIN);
4376 case VT_BSTR:
4377 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4378 if (FAILED(hRet))
4379 break;
4380 V_VT(pVarOut) = VT_R8;
4381 pVarIn = &varIn;
4382 /* Fall through ... */
4383 case VT_DATE:
4384 ABS_CASE(R8,R8_MIN);
4385 case VT_CY:
4386 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4387 break;
4388 case VT_DECIMAL:
4389 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4390 break;
4391 case VT_UI1:
4392 case VT_UI2:
4393 case VT_UINT:
4394 case VT_UI4:
4395 case VT_UI8:
4396 /* No-Op */
4397 break;
4398 case VT_EMPTY:
4399 V_VT(pVarOut) = VT_I2;
4400 case VT_NULL:
4401 V_I2(pVarOut) = 0;
4402 break;
4403 default:
4404 hRet = DISP_E_BADVARTYPE;
4407 VarAbs_Exit:
4408 VariantClear(&temp);
4409 return hRet;
4412 /**********************************************************************
4413 * VarFix [OLEAUT32.169]
4415 * Truncate a variants value to a whole number.
4417 * PARAMS
4418 * pVarIn [I] Source variant
4419 * pVarOut [O] Destination for converted value
4421 * RETURNS
4422 * Success: S_OK. pVarOut contains the converted value.
4423 * Failure: An HRESULT error code indicating the error.
4425 * NOTES
4426 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4427 * according to the following table:
4428 *| Input Type Output Type
4429 *| ---------- -----------
4430 *| VT_BOOL VT_I2
4431 *| VT_EMPTY VT_I2
4432 *| VT_BSTR VT_R8
4433 *| All Others Unchanged
4434 * - The difference between this function and VarInt() is that VarInt() rounds
4435 * negative numbers away from 0, while this function rounds them towards zero.
4437 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4439 HRESULT hRet = S_OK;
4440 VARIANT temp;
4442 VariantInit(&temp);
4444 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4446 /* Handle VT_DISPATCH by storing and taking address of returned value */
4447 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4449 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4450 if (FAILED(hRet)) goto VarFix_Exit;
4451 pVarIn = &temp;
4453 V_VT(pVarOut) = V_VT(pVarIn);
4455 switch (V_VT(pVarIn))
4457 case VT_UI1:
4458 V_UI1(pVarOut) = V_UI1(pVarIn);
4459 break;
4460 case VT_BOOL:
4461 V_VT(pVarOut) = VT_I2;
4462 /* Fall through */
4463 case VT_I2:
4464 V_I2(pVarOut) = V_I2(pVarIn);
4465 break;
4466 case VT_I4:
4467 V_I4(pVarOut) = V_I4(pVarIn);
4468 break;
4469 case VT_I8:
4470 V_I8(pVarOut) = V_I8(pVarIn);
4471 break;
4472 case VT_R4:
4473 if (V_R4(pVarIn) < 0.0f)
4474 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4475 else
4476 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4477 break;
4478 case VT_BSTR:
4479 V_VT(pVarOut) = VT_R8;
4480 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4481 pVarIn = pVarOut;
4482 /* Fall through */
4483 case VT_DATE:
4484 case VT_R8:
4485 if (V_R8(pVarIn) < 0.0)
4486 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4487 else
4488 V_R8(pVarOut) = floor(V_R8(pVarIn));
4489 break;
4490 case VT_CY:
4491 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4492 break;
4493 case VT_DECIMAL:
4494 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4495 break;
4496 case VT_EMPTY:
4497 V_VT(pVarOut) = VT_I2;
4498 V_I2(pVarOut) = 0;
4499 break;
4500 case VT_NULL:
4501 /* No-Op */
4502 break;
4503 default:
4504 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4505 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4506 hRet = DISP_E_BADVARTYPE;
4507 else
4508 hRet = DISP_E_TYPEMISMATCH;
4510 VarFix_Exit:
4511 if (FAILED(hRet))
4512 V_VT(pVarOut) = VT_EMPTY;
4513 VariantClear(&temp);
4515 return hRet;
4518 /**********************************************************************
4519 * VarInt [OLEAUT32.172]
4521 * Truncate a variants value to a whole number.
4523 * PARAMS
4524 * pVarIn [I] Source variant
4525 * pVarOut [O] Destination for converted value
4527 * RETURNS
4528 * Success: S_OK. pVarOut contains the converted value.
4529 * Failure: An HRESULT error code indicating the error.
4531 * NOTES
4532 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4533 * according to the following table:
4534 *| Input Type Output Type
4535 *| ---------- -----------
4536 *| VT_BOOL VT_I2
4537 *| VT_EMPTY VT_I2
4538 *| VT_BSTR VT_R8
4539 *| All Others Unchanged
4540 * - The difference between this function and VarFix() is that VarFix() rounds
4541 * negative numbers towards 0, while this function rounds them away from zero.
4543 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4545 HRESULT hRet = S_OK;
4546 VARIANT temp;
4548 VariantInit(&temp);
4550 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4552 /* Handle VT_DISPATCH by storing and taking address of returned value */
4553 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4555 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4556 if (FAILED(hRet)) goto VarInt_Exit;
4557 pVarIn = &temp;
4559 V_VT(pVarOut) = V_VT(pVarIn);
4561 switch (V_VT(pVarIn))
4563 case VT_R4:
4564 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4565 break;
4566 case VT_BSTR:
4567 V_VT(pVarOut) = VT_R8;
4568 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4569 pVarIn = pVarOut;
4570 /* Fall through */
4571 case VT_DATE:
4572 case VT_R8:
4573 V_R8(pVarOut) = floor(V_R8(pVarIn));
4574 break;
4575 case VT_CY:
4576 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4577 break;
4578 case VT_DECIMAL:
4579 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4580 break;
4581 default:
4582 hRet = VarFix(pVarIn, pVarOut);
4584 VarInt_Exit:
4585 VariantClear(&temp);
4587 return hRet;
4590 /**********************************************************************
4591 * VarXor [OLEAUT32.167]
4593 * Perform a logical exclusive-or (XOR) operation on two variants.
4595 * PARAMS
4596 * pVarLeft [I] First variant
4597 * pVarRight [I] Variant to XOR with pVarLeft
4598 * pVarOut [O] Destination for XOR result
4600 * RETURNS
4601 * Success: S_OK. pVarOut contains the result of the operation with its type
4602 * taken from the table below).
4603 * Failure: An HRESULT error code indicating the error.
4605 * NOTES
4606 * - Neither pVarLeft or pVarRight are modified by this function.
4607 * - This function does not process by-reference variants.
4608 * - Input types of VT_BSTR may be numeric strings or boolean text.
4609 * - The type of result stored in pVarOut depends on the types of pVarLeft
4610 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4611 * or VT_NULL if the function succeeds.
4612 * - Type promotion is inconsistent and as a result certain combinations of
4613 * values will return DISP_E_OVERFLOW even when they could be represented.
4614 * This matches the behaviour of native oleaut32.
4616 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4618 VARTYPE vt;
4619 VARIANT varLeft, varRight;
4620 VARIANT tempLeft, tempRight;
4621 double d;
4622 HRESULT hRet;
4624 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4626 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4627 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4628 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4629 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4630 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4631 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4632 return DISP_E_BADVARTYPE;
4634 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4636 /* NULL XOR anything valid is NULL */
4637 V_VT(pVarOut) = VT_NULL;
4638 return S_OK;
4641 VariantInit(&tempLeft);
4642 VariantInit(&tempRight);
4644 /* Handle VT_DISPATCH by storing and taking address of returned value */
4645 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4647 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4648 if (FAILED(hRet)) goto VarXor_Exit;
4649 pVarLeft = &tempLeft;
4651 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4653 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4654 if (FAILED(hRet)) goto VarXor_Exit;
4655 pVarRight = &tempRight;
4658 /* Copy our inputs so we don't disturb anything */
4659 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4661 hRet = VariantCopy(&varLeft, pVarLeft);
4662 if (FAILED(hRet))
4663 goto VarXor_Exit;
4665 hRet = VariantCopy(&varRight, pVarRight);
4666 if (FAILED(hRet))
4667 goto VarXor_Exit;
4669 /* Try any strings first as numbers, then as VT_BOOL */
4670 if (V_VT(&varLeft) == VT_BSTR)
4672 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4673 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4674 FAILED(hRet) ? VT_BOOL : VT_I4);
4675 if (FAILED(hRet))
4676 goto VarXor_Exit;
4679 if (V_VT(&varRight) == VT_BSTR)
4681 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4682 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4683 FAILED(hRet) ? VT_BOOL : VT_I4);
4684 if (FAILED(hRet))
4685 goto VarXor_Exit;
4688 /* Determine the result type */
4689 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4691 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4693 hRet = DISP_E_TYPEMISMATCH;
4694 goto VarXor_Exit;
4696 vt = VT_I8;
4698 else
4700 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4702 case (VT_BOOL << 16) | VT_BOOL:
4703 vt = VT_BOOL;
4704 break;
4705 case (VT_UI1 << 16) | VT_UI1:
4706 vt = VT_UI1;
4707 break;
4708 case (VT_EMPTY << 16) | VT_EMPTY:
4709 case (VT_EMPTY << 16) | VT_UI1:
4710 case (VT_EMPTY << 16) | VT_I2:
4711 case (VT_EMPTY << 16) | VT_BOOL:
4712 case (VT_UI1 << 16) | VT_EMPTY:
4713 case (VT_UI1 << 16) | VT_I2:
4714 case (VT_UI1 << 16) | VT_BOOL:
4715 case (VT_I2 << 16) | VT_EMPTY:
4716 case (VT_I2 << 16) | VT_UI1:
4717 case (VT_I2 << 16) | VT_I2:
4718 case (VT_I2 << 16) | VT_BOOL:
4719 case (VT_BOOL << 16) | VT_EMPTY:
4720 case (VT_BOOL << 16) | VT_UI1:
4721 case (VT_BOOL << 16) | VT_I2:
4722 vt = VT_I2;
4723 break;
4724 default:
4725 vt = VT_I4;
4726 break;
4730 /* VT_UI4 does not overflow */
4731 if (vt != VT_I8)
4733 if (V_VT(&varLeft) == VT_UI4)
4734 V_VT(&varLeft) = VT_I4;
4735 if (V_VT(&varRight) == VT_UI4)
4736 V_VT(&varRight) = VT_I4;
4739 /* Convert our input copies to the result type */
4740 if (V_VT(&varLeft) != vt)
4741 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4742 if (FAILED(hRet))
4743 goto VarXor_Exit;
4745 if (V_VT(&varRight) != vt)
4746 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4747 if (FAILED(hRet))
4748 goto VarXor_Exit;
4750 V_VT(pVarOut) = vt;
4752 /* Calculate the result */
4753 switch (vt)
4755 case VT_I8:
4756 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4757 break;
4758 case VT_I4:
4759 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4760 break;
4761 case VT_BOOL:
4762 case VT_I2:
4763 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4764 break;
4765 case VT_UI1:
4766 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4767 break;
4770 VarXor_Exit:
4771 VariantClear(&varLeft);
4772 VariantClear(&varRight);
4773 VariantClear(&tempLeft);
4774 VariantClear(&tempRight);
4775 return hRet;
4778 /**********************************************************************
4779 * VarEqv [OLEAUT32.172]
4781 * Determine if two variants contain the same value.
4783 * PARAMS
4784 * pVarLeft [I] First variant to compare
4785 * pVarRight [I] Variant to compare to pVarLeft
4786 * pVarOut [O] Destination for comparison result
4788 * RETURNS
4789 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4790 * if equivalent or non-zero otherwise.
4791 * Failure: An HRESULT error code indicating the error.
4793 * NOTES
4794 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4795 * the result.
4797 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4799 HRESULT hRet;
4801 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4803 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4804 if (SUCCEEDED(hRet))
4806 if (V_VT(pVarOut) == VT_I8)
4807 V_I8(pVarOut) = ~V_I8(pVarOut);
4808 else
4809 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4811 return hRet;
4814 /**********************************************************************
4815 * VarNeg [OLEAUT32.173]
4817 * Negate the value of a variant.
4819 * PARAMS
4820 * pVarIn [I] Source variant
4821 * pVarOut [O] Destination for converted value
4823 * RETURNS
4824 * Success: S_OK. pVarOut contains the converted value.
4825 * Failure: An HRESULT error code indicating the error.
4827 * NOTES
4828 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4829 * according to the following table:
4830 *| Input Type Output Type
4831 *| ---------- -----------
4832 *| VT_EMPTY VT_I2
4833 *| VT_UI1 VT_I2
4834 *| VT_BOOL VT_I2
4835 *| VT_BSTR VT_R8
4836 *| All Others Unchanged (unless promoted)
4837 * - Where the negated value of a variant does not fit in its base type, the type
4838 * is promoted according to the following table:
4839 *| Input Type Promoted To
4840 *| ---------- -----------
4841 *| VT_I2 VT_I4
4842 *| VT_I4 VT_R8
4843 *| VT_I8 VT_R8
4844 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4845 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4846 * for types which are not valid. Since this is in contravention of the
4847 * meaning of those error codes and unlikely to be relied on by applications,
4848 * this implementation returns errors consistent with the other high level
4849 * variant math functions.
4851 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4853 HRESULT hRet = S_OK;
4854 VARIANT temp;
4856 VariantInit(&temp);
4858 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4860 /* Handle VT_DISPATCH by storing and taking address of returned value */
4861 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4863 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4864 if (FAILED(hRet)) goto VarNeg_Exit;
4865 pVarIn = &temp;
4867 V_VT(pVarOut) = V_VT(pVarIn);
4869 switch (V_VT(pVarIn))
4871 case VT_UI1:
4872 V_VT(pVarOut) = VT_I2;
4873 V_I2(pVarOut) = -V_UI1(pVarIn);
4874 break;
4875 case VT_BOOL:
4876 V_VT(pVarOut) = VT_I2;
4877 /* Fall through */
4878 case VT_I2:
4879 if (V_I2(pVarIn) == I2_MIN)
4881 V_VT(pVarOut) = VT_I4;
4882 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4884 else
4885 V_I2(pVarOut) = -V_I2(pVarIn);
4886 break;
4887 case VT_I4:
4888 if (V_I4(pVarIn) == I4_MIN)
4890 V_VT(pVarOut) = VT_R8;
4891 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4893 else
4894 V_I4(pVarOut) = -V_I4(pVarIn);
4895 break;
4896 case VT_I8:
4897 if (V_I8(pVarIn) == I8_MIN)
4899 V_VT(pVarOut) = VT_R8;
4900 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4901 V_R8(pVarOut) *= -1.0;
4903 else
4904 V_I8(pVarOut) = -V_I8(pVarIn);
4905 break;
4906 case VT_R4:
4907 V_R4(pVarOut) = -V_R4(pVarIn);
4908 break;
4909 case VT_DATE:
4910 case VT_R8:
4911 V_R8(pVarOut) = -V_R8(pVarIn);
4912 break;
4913 case VT_CY:
4914 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4915 break;
4916 case VT_DECIMAL:
4917 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4918 break;
4919 case VT_BSTR:
4920 V_VT(pVarOut) = VT_R8;
4921 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4922 V_R8(pVarOut) = -V_R8(pVarOut);
4923 break;
4924 case VT_EMPTY:
4925 V_VT(pVarOut) = VT_I2;
4926 V_I2(pVarOut) = 0;
4927 break;
4928 case VT_NULL:
4929 /* No-Op */
4930 break;
4931 default:
4932 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4933 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4934 hRet = DISP_E_BADVARTYPE;
4935 else
4936 hRet = DISP_E_TYPEMISMATCH;
4938 VarNeg_Exit:
4939 if (FAILED(hRet))
4940 V_VT(pVarOut) = VT_EMPTY;
4941 VariantClear(&temp);
4943 return hRet;
4946 /**********************************************************************
4947 * VarNot [OLEAUT32.174]
4949 * Perform a not operation on a variant.
4951 * PARAMS
4952 * pVarIn [I] Source variant
4953 * pVarOut [O] Destination for converted value
4955 * RETURNS
4956 * Success: S_OK. pVarOut contains the converted value.
4957 * Failure: An HRESULT error code indicating the error.
4959 * NOTES
4960 * - Strictly speaking, this function performs a bitwise ones complement
4961 * on the variants value (after possibly converting to VT_I4, see below).
4962 * This only behaves like a boolean not operation if the value in
4963 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4964 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4965 * before calling this function.
4966 * - This function does not process by-reference variants.
4967 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4968 * according to the following table:
4969 *| Input Type Output Type
4970 *| ---------- -----------
4971 *| VT_EMPTY VT_I2
4972 *| VT_R4 VT_I4
4973 *| VT_R8 VT_I4
4974 *| VT_BSTR VT_I4
4975 *| VT_DECIMAL VT_I4
4976 *| VT_CY VT_I4
4977 *| (All others) Unchanged
4979 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4981 VARIANT varIn;
4982 HRESULT hRet = S_OK;
4983 VARIANT temp;
4985 VariantInit(&temp);
4987 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4989 /* Handle VT_DISPATCH by storing and taking address of returned value */
4990 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4992 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4993 if (FAILED(hRet)) goto VarNot_Exit;
4994 pVarIn = &temp;
4997 if (V_VT(pVarIn) == VT_BSTR)
4999 V_VT(&varIn) = VT_R8;
5000 hRet = VarR8FromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn) );
5001 if (FAILED(hRet))
5003 V_VT(&varIn) = VT_BOOL;
5004 hRet = VarBoolFromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &V_BOOL(&varIn) );
5006 if (FAILED(hRet)) goto VarNot_Exit;
5007 pVarIn = &varIn;
5010 V_VT(pVarOut) = V_VT(pVarIn);
5012 switch (V_VT(pVarIn))
5014 case VT_I1:
5015 V_I4(pVarOut) = ~V_I1(pVarIn);
5016 V_VT(pVarOut) = VT_I4;
5017 break;
5018 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
5019 case VT_BOOL:
5020 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
5021 case VT_UI2:
5022 V_I4(pVarOut) = ~V_UI2(pVarIn);
5023 V_VT(pVarOut) = VT_I4;
5024 break;
5025 case VT_DECIMAL:
5026 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
5027 if (FAILED(hRet))
5028 break;
5029 pVarIn = &varIn;
5030 /* Fall through ... */
5031 case VT_INT:
5032 V_VT(pVarOut) = VT_I4;
5033 /* Fall through ... */
5034 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
5035 case VT_UINT:
5036 case VT_UI4:
5037 V_I4(pVarOut) = ~V_UI4(pVarIn);
5038 V_VT(pVarOut) = VT_I4;
5039 break;
5040 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
5041 case VT_UI8:
5042 V_I4(pVarOut) = ~V_UI8(pVarIn);
5043 V_VT(pVarOut) = VT_I4;
5044 break;
5045 case VT_R4:
5046 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
5047 V_I4(pVarOut) = ~V_I4(pVarOut);
5048 V_VT(pVarOut) = VT_I4;
5049 break;
5050 case VT_DATE:
5051 case VT_R8:
5052 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
5053 V_I4(pVarOut) = ~V_I4(pVarOut);
5054 V_VT(pVarOut) = VT_I4;
5055 break;
5056 case VT_CY:
5057 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
5058 V_I4(pVarOut) = ~V_I4(pVarOut);
5059 V_VT(pVarOut) = VT_I4;
5060 break;
5061 case VT_EMPTY:
5062 V_I2(pVarOut) = ~0;
5063 V_VT(pVarOut) = VT_I2;
5064 break;
5065 case VT_NULL:
5066 /* No-Op */
5067 break;
5068 default:
5069 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5070 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5071 hRet = DISP_E_BADVARTYPE;
5072 else
5073 hRet = DISP_E_TYPEMISMATCH;
5075 VarNot_Exit:
5076 if (FAILED(hRet))
5077 V_VT(pVarOut) = VT_EMPTY;
5078 VariantClear(&temp);
5080 return hRet;
5083 /**********************************************************************
5084 * VarRound [OLEAUT32.175]
5086 * Perform a round operation on a variant.
5088 * PARAMS
5089 * pVarIn [I] Source variant
5090 * deci [I] Number of decimals to round to
5091 * pVarOut [O] Destination for converted value
5093 * RETURNS
5094 * Success: S_OK. pVarOut contains the converted value.
5095 * Failure: An HRESULT error code indicating the error.
5097 * NOTES
5098 * - Floating point values are rounded to the desired number of decimals.
5099 * - Some integer types are just copied to the return variable.
5100 * - Some other integer types are not handled and fail.
5102 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5104 VARIANT varIn;
5105 HRESULT hRet = S_OK;
5106 float factor;
5107 VARIANT temp;
5109 VariantInit(&temp);
5111 TRACE("(%s,%d)\n", debugstr_variant(pVarIn), deci);
5113 /* Handle VT_DISPATCH by storing and taking address of returned value */
5114 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5116 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5117 if (FAILED(hRet)) goto VarRound_Exit;
5118 pVarIn = &temp;
5121 switch (V_VT(pVarIn))
5123 /* cases that fail on windows */
5124 case VT_I1:
5125 case VT_I8:
5126 case VT_UI2:
5127 case VT_UI4:
5128 hRet = DISP_E_BADVARTYPE;
5129 break;
5131 /* cases just copying in to out */
5132 case VT_UI1:
5133 V_VT(pVarOut) = V_VT(pVarIn);
5134 V_UI1(pVarOut) = V_UI1(pVarIn);
5135 break;
5136 case VT_I2:
5137 V_VT(pVarOut) = V_VT(pVarIn);
5138 V_I2(pVarOut) = V_I2(pVarIn);
5139 break;
5140 case VT_I4:
5141 V_VT(pVarOut) = V_VT(pVarIn);
5142 V_I4(pVarOut) = V_I4(pVarIn);
5143 break;
5144 case VT_NULL:
5145 V_VT(pVarOut) = V_VT(pVarIn);
5146 /* value unchanged */
5147 break;
5149 /* cases that change type */
5150 case VT_EMPTY:
5151 V_VT(pVarOut) = VT_I2;
5152 V_I2(pVarOut) = 0;
5153 break;
5154 case VT_BOOL:
5155 V_VT(pVarOut) = VT_I2;
5156 V_I2(pVarOut) = V_BOOL(pVarIn);
5157 break;
5158 case VT_BSTR:
5159 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5160 if (FAILED(hRet))
5161 break;
5162 V_VT(&varIn)=VT_R8;
5163 pVarIn = &varIn;
5164 /* Fall through ... */
5166 /* cases we need to do math */
5167 case VT_R8:
5168 if (V_R8(pVarIn)>0) {
5169 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5170 } else {
5171 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5173 V_VT(pVarOut) = V_VT(pVarIn);
5174 break;
5175 case VT_R4:
5176 if (V_R4(pVarIn)>0) {
5177 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5178 } else {
5179 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5181 V_VT(pVarOut) = V_VT(pVarIn);
5182 break;
5183 case VT_DATE:
5184 if (V_DATE(pVarIn)>0) {
5185 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5186 } else {
5187 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5189 V_VT(pVarOut) = V_VT(pVarIn);
5190 break;
5191 case VT_CY:
5192 if (deci>3)
5193 factor=1;
5194 else
5195 factor=pow(10, 4-deci);
5197 if (V_CY(pVarIn).int64>0) {
5198 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5199 } else {
5200 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5202 V_VT(pVarOut) = V_VT(pVarIn);
5203 break;
5205 /* cases we don't know yet */
5206 default:
5207 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5208 V_VT(pVarIn) & VT_TYPEMASK, deci);
5209 hRet = DISP_E_BADVARTYPE;
5211 VarRound_Exit:
5212 if (FAILED(hRet))
5213 V_VT(pVarOut) = VT_EMPTY;
5214 VariantClear(&temp);
5216 TRACE("returning 0x%08x %s\n", hRet, debugstr_variant(pVarOut));
5217 return hRet;
5220 /**********************************************************************
5221 * VarIdiv [OLEAUT32.153]
5223 * Converts input variants to integers and divides them.
5225 * PARAMS
5226 * left [I] Left hand variant
5227 * right [I] Right hand variant
5228 * result [O] Destination for quotient
5230 * RETURNS
5231 * Success: S_OK. result contains the quotient.
5232 * Failure: An HRESULT error code indicating the error.
5234 * NOTES
5235 * If either expression is null, null is returned, as per MSDN
5237 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5239 HRESULT hres = S_OK;
5240 VARTYPE resvt = VT_EMPTY;
5241 VARTYPE leftvt,rightvt;
5242 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5243 VARIANT lv,rv;
5244 VARIANT tempLeft, tempRight;
5246 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5248 VariantInit(&lv);
5249 VariantInit(&rv);
5250 VariantInit(&tempLeft);
5251 VariantInit(&tempRight);
5253 leftvt = V_VT(left)&VT_TYPEMASK;
5254 rightvt = V_VT(right)&VT_TYPEMASK;
5255 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5256 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5258 if (leftExtraFlags != rightExtraFlags)
5260 hres = DISP_E_BADVARTYPE;
5261 goto end;
5263 ExtraFlags = leftExtraFlags;
5265 /* Native VarIdiv always returns an error when using extra
5266 * flags or if the variant combination is I8 and INT.
5268 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5269 (leftvt == VT_INT && rightvt == VT_I8) ||
5270 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5271 ExtraFlags != 0)
5273 hres = DISP_E_BADVARTYPE;
5274 goto end;
5277 /* Determine variant type */
5278 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5280 V_VT(result) = VT_NULL;
5281 hres = S_OK;
5282 goto end;
5284 else if (leftvt == VT_I8 || rightvt == VT_I8)
5285 resvt = VT_I8;
5286 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5287 leftvt == VT_INT || rightvt == VT_INT ||
5288 leftvt == VT_UINT || rightvt == VT_UINT ||
5289 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5290 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5291 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5292 leftvt == VT_I1 || rightvt == VT_I1 ||
5293 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5294 leftvt == VT_DATE || rightvt == VT_DATE ||
5295 leftvt == VT_CY || rightvt == VT_CY ||
5296 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5297 leftvt == VT_R8 || rightvt == VT_R8 ||
5298 leftvt == VT_R4 || rightvt == VT_R4)
5299 resvt = VT_I4;
5300 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5301 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5302 leftvt == VT_EMPTY)
5303 resvt = VT_I2;
5304 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5305 resvt = VT_UI1;
5306 else
5308 hres = DISP_E_BADVARTYPE;
5309 goto end;
5312 /* coerce to the result type */
5313 hres = VariantChangeType(&lv, left, 0, resvt);
5314 if (hres != S_OK) goto end;
5315 hres = VariantChangeType(&rv, right, 0, resvt);
5316 if (hres != S_OK) goto end;
5318 /* do the math */
5319 V_VT(result) = resvt;
5320 switch (resvt)
5322 case VT_UI1:
5323 if (V_UI1(&rv) == 0)
5325 hres = DISP_E_DIVBYZERO;
5326 V_VT(result) = VT_EMPTY;
5328 else
5329 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5330 break;
5331 case VT_I2:
5332 if (V_I2(&rv) == 0)
5334 hres = DISP_E_DIVBYZERO;
5335 V_VT(result) = VT_EMPTY;
5337 else
5338 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5339 break;
5340 case VT_I4:
5341 if (V_I4(&rv) == 0)
5343 hres = DISP_E_DIVBYZERO;
5344 V_VT(result) = VT_EMPTY;
5346 else
5347 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5348 break;
5349 case VT_I8:
5350 if (V_I8(&rv) == 0)
5352 hres = DISP_E_DIVBYZERO;
5353 V_VT(result) = VT_EMPTY;
5355 else
5356 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5357 break;
5358 default:
5359 FIXME("Couldn't integer divide variant types %d,%d\n",
5360 leftvt,rightvt);
5363 end:
5364 VariantClear(&lv);
5365 VariantClear(&rv);
5366 VariantClear(&tempLeft);
5367 VariantClear(&tempRight);
5369 return hres;
5373 /**********************************************************************
5374 * VarMod [OLEAUT32.155]
5376 * Perform the modulus operation of the right hand variant on the left
5378 * PARAMS
5379 * left [I] Left hand variant
5380 * right [I] Right hand variant
5381 * result [O] Destination for converted value
5383 * RETURNS
5384 * Success: S_OK. result contains the remainder.
5385 * Failure: An HRESULT error code indicating the error.
5387 * NOTE:
5388 * If an error occurs the type of result will be modified but the value will not be.
5389 * Doesn't support arrays or any special flags yet.
5391 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5393 BOOL lOk = TRUE;
5394 HRESULT rc = E_FAIL;
5395 int resT = 0;
5396 VARIANT lv,rv;
5397 VARIANT tempLeft, tempRight;
5399 VariantInit(&tempLeft);
5400 VariantInit(&tempRight);
5401 VariantInit(&lv);
5402 VariantInit(&rv);
5404 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5406 /* Handle VT_DISPATCH by storing and taking address of returned value */
5407 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5409 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5410 if (FAILED(rc)) goto end;
5411 left = &tempLeft;
5413 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5415 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5416 if (FAILED(rc)) goto end;
5417 right = &tempRight;
5420 /* check for invalid inputs */
5421 lOk = TRUE;
5422 switch (V_VT(left) & VT_TYPEMASK) {
5423 case VT_BOOL :
5424 case VT_I1 :
5425 case VT_I2 :
5426 case VT_I4 :
5427 case VT_I8 :
5428 case VT_INT :
5429 case VT_UI1 :
5430 case VT_UI2 :
5431 case VT_UI4 :
5432 case VT_UI8 :
5433 case VT_UINT :
5434 case VT_R4 :
5435 case VT_R8 :
5436 case VT_CY :
5437 case VT_EMPTY:
5438 case VT_DATE :
5439 case VT_BSTR :
5440 case VT_DECIMAL:
5441 break;
5442 case VT_VARIANT:
5443 case VT_UNKNOWN:
5444 V_VT(result) = VT_EMPTY;
5445 rc = DISP_E_TYPEMISMATCH;
5446 goto end;
5447 case VT_ERROR:
5448 rc = DISP_E_TYPEMISMATCH;
5449 goto end;
5450 case VT_RECORD:
5451 V_VT(result) = VT_EMPTY;
5452 rc = DISP_E_TYPEMISMATCH;
5453 goto end;
5454 case VT_NULL:
5455 break;
5456 default:
5457 V_VT(result) = VT_EMPTY;
5458 rc = DISP_E_BADVARTYPE;
5459 goto end;
5463 switch (V_VT(right) & VT_TYPEMASK) {
5464 case VT_BOOL :
5465 case VT_I1 :
5466 case VT_I2 :
5467 case VT_I4 :
5468 case VT_I8 :
5469 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5471 V_VT(result) = VT_EMPTY;
5472 rc = DISP_E_TYPEMISMATCH;
5473 goto end;
5475 case VT_INT :
5476 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5478 V_VT(result) = VT_EMPTY;
5479 rc = DISP_E_TYPEMISMATCH;
5480 goto end;
5482 case VT_UI1 :
5483 case VT_UI2 :
5484 case VT_UI4 :
5485 case VT_UI8 :
5486 case VT_UINT :
5487 case VT_R4 :
5488 case VT_R8 :
5489 case VT_CY :
5490 if(V_VT(left) == VT_EMPTY)
5492 V_VT(result) = VT_I4;
5493 rc = S_OK;
5494 goto end;
5496 case VT_EMPTY:
5497 case VT_DATE :
5498 case VT_DECIMAL:
5499 if(V_VT(left) == VT_ERROR)
5501 V_VT(result) = VT_EMPTY;
5502 rc = DISP_E_TYPEMISMATCH;
5503 goto end;
5505 case VT_BSTR:
5506 if(V_VT(left) == VT_NULL)
5508 V_VT(result) = VT_NULL;
5509 rc = S_OK;
5510 goto end;
5512 break;
5514 case VT_VOID:
5515 V_VT(result) = VT_EMPTY;
5516 rc = DISP_E_BADVARTYPE;
5517 goto end;
5518 case VT_NULL:
5519 if(V_VT(left) == VT_VOID)
5521 V_VT(result) = VT_EMPTY;
5522 rc = DISP_E_BADVARTYPE;
5523 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5524 lOk)
5526 V_VT(result) = VT_NULL;
5527 rc = S_OK;
5528 } else
5530 V_VT(result) = VT_NULL;
5531 rc = DISP_E_BADVARTYPE;
5533 goto end;
5534 case VT_VARIANT:
5535 case VT_UNKNOWN:
5536 V_VT(result) = VT_EMPTY;
5537 rc = DISP_E_TYPEMISMATCH;
5538 goto end;
5539 case VT_ERROR:
5540 rc = DISP_E_TYPEMISMATCH;
5541 goto end;
5542 case VT_RECORD:
5543 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5545 V_VT(result) = VT_EMPTY;
5546 rc = DISP_E_BADVARTYPE;
5547 } else
5549 V_VT(result) = VT_EMPTY;
5550 rc = DISP_E_TYPEMISMATCH;
5552 goto end;
5553 default:
5554 V_VT(result) = VT_EMPTY;
5555 rc = DISP_E_BADVARTYPE;
5556 goto end;
5559 /* determine the result type */
5560 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5561 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5562 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5563 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5564 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5565 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5566 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5567 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5568 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5569 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5570 else resT = VT_I4; /* most outputs are I4 */
5572 /* convert to I8 for the modulo */
5573 rc = VariantChangeType(&lv, left, 0, VT_I8);
5574 if(FAILED(rc))
5576 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5577 goto end;
5580 rc = VariantChangeType(&rv, right, 0, VT_I8);
5581 if(FAILED(rc))
5583 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5584 goto end;
5587 /* if right is zero set VT_EMPTY and return divide by zero */
5588 if(V_I8(&rv) == 0)
5590 V_VT(result) = VT_EMPTY;
5591 rc = DISP_E_DIVBYZERO;
5592 goto end;
5595 /* perform the modulo operation */
5596 V_VT(result) = VT_I8;
5597 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5599 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5600 wine_dbgstr_longlong(V_I8(&lv)), wine_dbgstr_longlong(V_I8(&rv)),
5601 wine_dbgstr_longlong(V_I8(result)));
5603 /* convert left and right to the destination type */
5604 rc = VariantChangeType(result, result, 0, resT);
5605 if(FAILED(rc))
5607 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5608 /* fall to end of function */
5611 end:
5612 VariantClear(&lv);
5613 VariantClear(&rv);
5614 VariantClear(&tempLeft);
5615 VariantClear(&tempRight);
5616 return rc;
5619 /**********************************************************************
5620 * VarPow [OLEAUT32.158]
5622 * Computes the power of one variant to another variant.
5624 * PARAMS
5625 * left [I] First variant
5626 * right [I] Second variant
5627 * result [O] Result variant
5629 * RETURNS
5630 * Success: S_OK.
5631 * Failure: An HRESULT error code indicating the error.
5633 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5635 HRESULT hr = S_OK;
5636 VARIANT dl,dr;
5637 VARTYPE resvt = VT_EMPTY;
5638 VARTYPE leftvt,rightvt;
5639 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5640 VARIANT tempLeft, tempRight;
5642 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5644 VariantInit(&dl);
5645 VariantInit(&dr);
5646 VariantInit(&tempLeft);
5647 VariantInit(&tempRight);
5649 /* Handle VT_DISPATCH by storing and taking address of returned value */
5650 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5652 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5653 if (FAILED(hr)) goto end;
5654 left = &tempLeft;
5656 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5658 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5659 if (FAILED(hr)) goto end;
5660 right = &tempRight;
5663 leftvt = V_VT(left)&VT_TYPEMASK;
5664 rightvt = V_VT(right)&VT_TYPEMASK;
5665 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5666 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5668 if (leftExtraFlags != rightExtraFlags)
5670 hr = DISP_E_BADVARTYPE;
5671 goto end;
5673 ExtraFlags = leftExtraFlags;
5675 /* Native VarPow always returns an error when using extra flags */
5676 if (ExtraFlags != 0)
5678 hr = DISP_E_BADVARTYPE;
5679 goto end;
5682 /* Determine return type */
5683 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5684 V_VT(result) = VT_NULL;
5685 hr = S_OK;
5686 goto end;
5688 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5689 leftvt == VT_I4 || leftvt == VT_R4 ||
5690 leftvt == VT_R8 || leftvt == VT_CY ||
5691 leftvt == VT_DATE || leftvt == VT_BSTR ||
5692 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5693 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5694 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5695 rightvt == VT_I4 || rightvt == VT_R4 ||
5696 rightvt == VT_R8 || rightvt == VT_CY ||
5697 rightvt == VT_DATE || rightvt == VT_BSTR ||
5698 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5699 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5700 resvt = VT_R8;
5701 else
5703 hr = DISP_E_BADVARTYPE;
5704 goto end;
5707 hr = VariantChangeType(&dl,left,0,resvt);
5708 if (FAILED(hr)) {
5709 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5710 hr = E_FAIL;
5711 goto end;
5714 hr = VariantChangeType(&dr,right,0,resvt);
5715 if (FAILED(hr)) {
5716 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5717 hr = E_FAIL;
5718 goto end;
5721 V_VT(result) = VT_R8;
5722 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5724 end:
5725 VariantClear(&dl);
5726 VariantClear(&dr);
5727 VariantClear(&tempLeft);
5728 VariantClear(&tempRight);
5730 return hr;
5733 /**********************************************************************
5734 * VarImp [OLEAUT32.154]
5736 * Bitwise implication of two variants.
5738 * PARAMS
5739 * left [I] First variant
5740 * right [I] Second variant
5741 * result [O] Result variant
5743 * RETURNS
5744 * Success: S_OK.
5745 * Failure: An HRESULT error code indicating the error.
5747 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5749 HRESULT hres = S_OK;
5750 VARTYPE resvt = VT_EMPTY;
5751 VARTYPE leftvt,rightvt;
5752 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5753 VARIANT lv,rv;
5754 double d;
5755 VARIANT tempLeft, tempRight;
5757 VariantInit(&lv);
5758 VariantInit(&rv);
5759 VariantInit(&tempLeft);
5760 VariantInit(&tempRight);
5762 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5764 /* Handle VT_DISPATCH by storing and taking address of returned value */
5765 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5767 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5768 if (FAILED(hres)) goto VarImp_Exit;
5769 left = &tempLeft;
5771 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5773 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5774 if (FAILED(hres)) goto VarImp_Exit;
5775 right = &tempRight;
5778 leftvt = V_VT(left)&VT_TYPEMASK;
5779 rightvt = V_VT(right)&VT_TYPEMASK;
5780 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5781 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5783 if (leftExtraFlags != rightExtraFlags)
5785 hres = DISP_E_BADVARTYPE;
5786 goto VarImp_Exit;
5788 ExtraFlags = leftExtraFlags;
5790 /* Native VarImp always returns an error when using extra
5791 * flags or if the variants are I8 and INT.
5793 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5794 ExtraFlags != 0)
5796 hres = DISP_E_BADVARTYPE;
5797 goto VarImp_Exit;
5800 /* Determine result type */
5801 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5802 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5804 V_VT(result) = VT_NULL;
5805 hres = S_OK;
5806 goto VarImp_Exit;
5808 else if (leftvt == VT_I8 || rightvt == VT_I8)
5809 resvt = VT_I8;
5810 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5811 leftvt == VT_INT || rightvt == VT_INT ||
5812 leftvt == VT_UINT || rightvt == VT_UINT ||
5813 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5814 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5815 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5816 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5817 leftvt == VT_DATE || rightvt == VT_DATE ||
5818 leftvt == VT_CY || rightvt == VT_CY ||
5819 leftvt == VT_R8 || rightvt == VT_R8 ||
5820 leftvt == VT_R4 || rightvt == VT_R4 ||
5821 leftvt == VT_I1 || rightvt == VT_I1)
5822 resvt = VT_I4;
5823 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5824 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5825 (leftvt == VT_NULL && rightvt == VT_UI1))
5826 resvt = VT_UI1;
5827 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5828 leftvt == VT_I2 || rightvt == VT_I2 ||
5829 leftvt == VT_UI1 || rightvt == VT_UI1)
5830 resvt = VT_I2;
5831 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5832 leftvt == VT_BSTR || rightvt == VT_BSTR)
5833 resvt = VT_BOOL;
5835 /* VT_NULL requires special handling for when the opposite
5836 * variant is equal to something other than -1.
5837 * (NULL Imp 0 = NULL, NULL Imp n = n)
5839 if (leftvt == VT_NULL)
5841 VARIANT_BOOL b;
5842 switch(rightvt)
5844 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5845 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5846 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5847 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5848 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5849 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5850 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5851 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5852 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5853 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5854 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5855 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5856 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5857 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5858 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5859 case VT_DECIMAL:
5860 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5861 resvt = VT_NULL;
5862 break;
5863 case VT_BSTR:
5864 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5865 if (FAILED(hres)) goto VarImp_Exit;
5866 else if (!b)
5867 V_VT(result) = VT_NULL;
5868 else
5870 V_VT(result) = VT_BOOL;
5871 V_BOOL(result) = b;
5873 goto VarImp_Exit;
5875 if (resvt == VT_NULL)
5877 V_VT(result) = resvt;
5878 goto VarImp_Exit;
5880 else
5882 hres = VariantChangeType(result,right,0,resvt);
5883 goto VarImp_Exit;
5887 /* Special handling is required when NULL is the right variant.
5888 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5890 else if (rightvt == VT_NULL)
5892 VARIANT_BOOL b;
5893 switch(leftvt)
5895 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5896 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5897 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5898 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5899 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5900 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5901 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5902 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5903 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5904 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5905 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5906 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5907 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5908 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5909 case VT_DECIMAL:
5910 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5911 resvt = VT_NULL;
5912 break;
5913 case VT_BSTR:
5914 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5915 if (FAILED(hres)) goto VarImp_Exit;
5916 else if (b == VARIANT_TRUE)
5917 resvt = VT_NULL;
5919 if (resvt == VT_NULL)
5921 V_VT(result) = resvt;
5922 goto VarImp_Exit;
5926 hres = VariantCopy(&lv, left);
5927 if (FAILED(hres)) goto VarImp_Exit;
5929 if (rightvt == VT_NULL)
5931 memset( &rv, 0, sizeof(rv) );
5932 V_VT(&rv) = resvt;
5934 else
5936 hres = VariantCopy(&rv, right);
5937 if (FAILED(hres)) goto VarImp_Exit;
5940 if (V_VT(&lv) == VT_BSTR &&
5941 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5942 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5943 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5944 hres = VariantChangeType(&lv,&lv,0,resvt);
5945 if (FAILED(hres)) goto VarImp_Exit;
5947 if (V_VT(&rv) == VT_BSTR &&
5948 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5949 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5950 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5951 hres = VariantChangeType(&rv, &rv, 0, resvt);
5952 if (FAILED(hres)) goto VarImp_Exit;
5954 /* do the math */
5955 V_VT(result) = resvt;
5956 switch (resvt)
5958 case VT_I8:
5959 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5960 break;
5961 case VT_I4:
5962 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5963 break;
5964 case VT_I2:
5965 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
5966 break;
5967 case VT_UI1:
5968 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
5969 break;
5970 case VT_BOOL:
5971 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
5972 break;
5973 default:
5974 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5975 leftvt,rightvt);
5978 VarImp_Exit:
5980 VariantClear(&lv);
5981 VariantClear(&rv);
5982 VariantClear(&tempLeft);
5983 VariantClear(&tempRight);
5985 return hres;