mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / oleaut32 / variant.c
blobace33a006f42d0c1e1ebbb69e2d64f33727b3f18
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 <string.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
32 #define COBJMACROS
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winerror.h"
39 #include "variant.h"
40 #include "resource.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(variant);
45 static CRITICAL_SECTION cache_cs;
46 static CRITICAL_SECTION_DEBUG critsect_debug =
48 0, 0, &cache_cs,
49 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
50 0, 0, { (DWORD_PTR)(__FILE__ ": cache_cs") }
52 static CRITICAL_SECTION cache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
54 /* Convert a variant from one type to another */
55 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
56 VARIANTARG* ps, VARTYPE vt)
58 HRESULT res = DISP_E_TYPEMISMATCH;
59 VARTYPE vtFrom = V_TYPE(ps);
60 DWORD dwFlags = 0;
62 TRACE("(%s,0x%08x,0x%04x,%s,%s)\n", debugstr_variant(pd), lcid, wFlags,
63 debugstr_variant(ps), debugstr_vt(vt));
65 if (vt == VT_BSTR || vtFrom == VT_BSTR)
67 /* All flags passed to low level function are only used for
68 * changing to or from strings. Map these here.
70 if (wFlags & VARIANT_LOCALBOOL)
71 dwFlags |= VAR_LOCALBOOL;
72 if (wFlags & VARIANT_CALENDAR_HIJRI)
73 dwFlags |= VAR_CALENDAR_HIJRI;
74 if (wFlags & VARIANT_CALENDAR_THAI)
75 dwFlags |= VAR_CALENDAR_THAI;
76 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
77 dwFlags |= VAR_CALENDAR_GREGORIAN;
78 if (wFlags & VARIANT_NOUSEROVERRIDE)
79 dwFlags |= LOCALE_NOUSEROVERRIDE;
80 if (wFlags & VARIANT_USE_NLS)
81 dwFlags |= LOCALE_USE_NLS;
84 /* Map int/uint to i4/ui4 */
85 if (vt == VT_INT)
86 vt = VT_I4;
87 else if (vt == VT_UINT)
88 vt = VT_UI4;
90 if (vtFrom == VT_INT)
91 vtFrom = VT_I4;
92 else if (vtFrom == VT_UINT)
93 vtFrom = VT_UI4;
95 if (vt == vtFrom)
96 return VariantCopy(pd, ps);
98 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
100 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
101 * accessing the default object property.
103 return DISP_E_TYPEMISMATCH;
106 switch (vt)
108 case VT_EMPTY:
109 if (vtFrom == VT_NULL)
110 return DISP_E_TYPEMISMATCH;
111 /* ... Fall through */
112 case VT_NULL:
113 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
115 res = VariantClear( pd );
116 if (vt == VT_NULL && SUCCEEDED(res))
117 V_VT(pd) = VT_NULL;
119 return res;
121 case VT_I1:
122 switch (vtFrom)
124 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
125 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
126 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
127 case VT_UI1: V_I1(pd) = V_UI1(ps); return S_OK;
128 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
129 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
130 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
131 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
132 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
133 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
134 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
135 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
136 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
137 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
138 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
139 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
141 break;
143 case VT_I2:
144 switch (vtFrom)
146 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
147 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
148 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
149 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
150 case VT_UI2: V_I2(pd) = V_UI2(ps); return S_OK;
151 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
152 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
153 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
154 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
155 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
156 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
157 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
158 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
159 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
160 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
161 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
163 break;
165 case VT_I4:
166 switch (vtFrom)
168 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
169 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
170 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
171 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
172 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
173 case VT_UI4: V_I4(pd) = V_UI4(ps); return S_OK;
174 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
175 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
176 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
177 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
178 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
179 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
180 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
181 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
182 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
183 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
185 break;
187 case VT_UI1:
188 switch (vtFrom)
190 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
191 case VT_I1: V_UI1(pd) = V_I1(ps); return S_OK;
192 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
193 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
194 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
195 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
196 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
197 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
198 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
199 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
200 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
201 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
202 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
203 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
204 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
205 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
207 break;
209 case VT_UI2:
210 switch (vtFrom)
212 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
213 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
214 case VT_I2: V_UI2(pd) = V_I2(ps); return S_OK;
215 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
216 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
217 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
218 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
219 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
220 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
221 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
222 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
223 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
224 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
225 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
226 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
227 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
229 break;
231 case VT_UI4:
232 switch (vtFrom)
234 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
235 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
236 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
237 case VT_I4: V_UI4(pd) = V_I4(ps); return S_OK;
238 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
239 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
240 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
241 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
242 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
243 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
244 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
245 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
246 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
247 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
248 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
249 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
251 break;
253 case VT_UI8:
254 switch (vtFrom)
256 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
257 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
258 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
259 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
260 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
261 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
262 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
263 case VT_I8: V_UI8(pd) = V_I8(ps); return S_OK;
264 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
265 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
266 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
267 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
268 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
269 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
270 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
271 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
273 break;
275 case VT_I8:
276 switch (vtFrom)
278 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
279 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
280 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
281 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
282 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
283 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
284 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
285 case VT_UI8: V_I8(pd) = V_UI8(ps); return S_OK;
286 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
287 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
288 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
289 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
290 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
291 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
292 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
293 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
295 break;
297 case VT_R4:
298 switch (vtFrom)
300 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
301 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
302 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
303 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
304 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
305 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
306 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
307 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
308 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
309 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
310 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
311 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
312 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
313 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
314 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
315 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
317 break;
319 case VT_R8:
320 switch (vtFrom)
322 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
323 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
324 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
325 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
326 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
327 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
328 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
329 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
330 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
331 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
332 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
333 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
334 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
335 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
336 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
337 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
339 break;
341 case VT_DATE:
342 switch (vtFrom)
344 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
345 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
346 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
347 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
348 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
349 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
350 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
351 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
352 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
353 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
354 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
355 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
356 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
357 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
358 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
359 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
361 break;
363 case VT_BOOL:
364 switch (vtFrom)
366 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
367 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
368 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
369 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
370 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
371 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
372 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
373 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
374 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
375 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
376 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
377 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
378 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
379 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
380 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
381 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
383 break;
385 case VT_BSTR:
386 switch (vtFrom)
388 case VT_EMPTY:
389 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
390 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
391 case VT_BOOL:
392 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
393 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
394 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
395 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
396 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
397 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
398 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
399 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
400 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
401 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
402 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
403 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
404 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
405 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
406 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
407 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
408 case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd));
410 break;
412 case VT_CY:
413 switch (vtFrom)
415 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
416 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
417 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
418 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
419 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
420 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
421 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
422 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
423 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
424 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
425 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
426 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
427 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
428 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
429 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
430 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
432 break;
434 case VT_DECIMAL:
435 switch (vtFrom)
437 case VT_EMPTY:
438 case VT_BOOL:
439 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
440 DEC_HI32(&V_DECIMAL(pd)) = 0;
441 DEC_MID32(&V_DECIMAL(pd)) = 0;
442 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
443 * VT_NULL and VT_EMPTY always give a 0 value.
445 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
446 return S_OK;
447 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
448 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
449 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
450 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
451 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
452 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
453 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
454 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
455 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
456 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
457 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
458 case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
459 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
460 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
462 break;
464 case VT_UNKNOWN:
465 switch (vtFrom)
467 case VT_DISPATCH:
468 if (V_DISPATCH(ps) == NULL)
470 V_UNKNOWN(pd) = NULL;
471 res = S_OK;
473 else
474 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
475 break;
477 break;
479 case VT_DISPATCH:
480 switch (vtFrom)
482 case VT_UNKNOWN:
483 if (V_UNKNOWN(ps) == NULL)
485 V_DISPATCH(pd) = NULL;
486 res = S_OK;
488 else
489 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
490 break;
492 break;
494 case VT_RECORD:
495 break;
497 return res;
500 /* Coerce to/from an array */
501 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
503 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
504 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
506 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
507 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(pd));
509 if (V_VT(ps) == vt)
510 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
512 return DISP_E_TYPEMISMATCH;
515 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
517 HRESULT hres;
518 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
520 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
521 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
522 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
523 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
524 NULL, NULL);
525 } else {
526 hres = DISP_E_TYPEMISMATCH;
528 return hres;
531 /******************************************************************************
532 * Check if a variants type is valid.
534 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
536 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
538 vt &= VT_TYPEMASK;
540 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
542 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
544 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
545 return DISP_E_BADVARTYPE;
546 if (vt != (VARTYPE)15)
547 return S_OK;
550 return DISP_E_BADVARTYPE;
553 /******************************************************************************
554 * VariantInit [OLEAUT32.8]
556 * Initialise a variant.
558 * PARAMS
559 * pVarg [O] Variant to initialise
561 * RETURNS
562 * Nothing.
564 * NOTES
565 * This function simply sets the type of the variant to VT_EMPTY. It does not
566 * free any existing value, use VariantClear() for that.
568 void WINAPI VariantInit(VARIANTARG* pVarg)
570 TRACE("(%p)\n", pVarg);
572 /* Win8.1 zeroes whole struct. Previous implementations don't set any other fields. */
573 V_VT(pVarg) = VT_EMPTY;
576 HRESULT VARIANT_ClearInd(VARIANTARG *pVarg)
578 HRESULT hres;
580 TRACE("(%s)\n", debugstr_variant(pVarg));
582 hres = VARIANT_ValidateType(V_VT(pVarg));
583 if (FAILED(hres))
584 return hres;
586 switch (V_VT(pVarg))
588 case VT_DISPATCH:
589 case VT_UNKNOWN:
590 if (V_UNKNOWN(pVarg))
591 IUnknown_Release(V_UNKNOWN(pVarg));
592 break;
593 case VT_UNKNOWN | VT_BYREF:
594 case VT_DISPATCH | VT_BYREF:
595 if(*V_UNKNOWNREF(pVarg))
596 IUnknown_Release(*V_UNKNOWNREF(pVarg));
597 break;
598 case VT_BSTR:
599 SysFreeString(V_BSTR(pVarg));
600 break;
601 case VT_BSTR | VT_BYREF:
602 SysFreeString(*V_BSTRREF(pVarg));
603 break;
604 case VT_VARIANT | VT_BYREF:
605 VariantClear(V_VARIANTREF(pVarg));
606 break;
607 case VT_RECORD:
608 case VT_RECORD | VT_BYREF:
610 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
611 if (pBr->pRecInfo)
613 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
614 IRecordInfo_Release(pBr->pRecInfo);
616 break;
618 default:
619 if (V_ISARRAY(pVarg) || (V_VT(pVarg) & ~VT_BYREF) == VT_SAFEARRAY)
621 if (V_ISBYREF(pVarg))
623 if (*V_ARRAYREF(pVarg))
624 hres = SafeArrayDestroy(*V_ARRAYREF(pVarg));
626 else if (V_ARRAY(pVarg))
627 hres = SafeArrayDestroy(V_ARRAY(pVarg));
629 break;
632 V_VT(pVarg) = VT_EMPTY;
633 return hres;
636 /******************************************************************************
637 * VariantClear [OLEAUT32.9]
639 * Clear a variant.
641 * PARAMS
642 * pVarg [I/O] Variant to clear
644 * RETURNS
645 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
646 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
648 HRESULT WINAPI DECLSPEC_HOTPATCH VariantClear(VARIANTARG* pVarg)
650 HRESULT hres;
652 TRACE("(%s)\n", debugstr_variant(pVarg));
654 hres = VARIANT_ValidateType(V_VT(pVarg));
656 if (SUCCEEDED(hres))
658 if (!V_ISBYREF(pVarg))
660 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
662 hres = SafeArrayDestroy(V_ARRAY(pVarg));
664 else if (V_VT(pVarg) == VT_BSTR)
666 SysFreeString(V_BSTR(pVarg));
668 else if (V_VT(pVarg) == VT_RECORD)
670 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
671 if (pBr->pRecInfo)
673 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
674 IRecordInfo_Release(pBr->pRecInfo);
677 else if (V_VT(pVarg) == VT_DISPATCH ||
678 V_VT(pVarg) == VT_UNKNOWN)
680 if (V_UNKNOWN(pVarg))
681 IUnknown_Release(V_UNKNOWN(pVarg));
684 V_VT(pVarg) = VT_EMPTY;
686 return hres;
689 /******************************************************************************
690 * Copy an IRecordInfo object contained in a variant.
692 static HRESULT VARIANT_CopyIRecordInfo(VARIANT *dest, const VARIANT *src)
694 struct __tagBRECORD *dest_rec = &V_UNION(dest, brecVal);
695 const struct __tagBRECORD *src_rec = &V_UNION(src, brecVal);
696 HRESULT hr = S_OK;
697 ULONG size;
699 if (!src_rec->pRecInfo)
701 if (src_rec->pvRecord) return E_INVALIDARG;
702 return S_OK;
705 hr = IRecordInfo_GetSize(src_rec->pRecInfo, &size);
706 if (FAILED(hr)) return hr;
708 /* This could look cleaner if only RecordCreate() was used, but native doesn't use it.
709 Memory should be allocated in a same way as RecordCreate() does, so RecordDestroy()
710 could free it later. */
711 dest_rec->pvRecord = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
712 if (!dest_rec->pvRecord) return E_OUTOFMEMORY;
714 dest_rec->pRecInfo = src_rec->pRecInfo;
715 IRecordInfo_AddRef(src_rec->pRecInfo);
717 return IRecordInfo_RecordCopy(src_rec->pRecInfo, src_rec->pvRecord, dest_rec->pvRecord);
720 /******************************************************************************
721 * VariantCopy [OLEAUT32.10]
723 * Copy a variant.
725 * PARAMS
726 * pvargDest [O] Destination for copy
727 * pvargSrc [I] Source variant to copy
729 * RETURNS
730 * Success: S_OK. pvargDest contains a copy of pvargSrc.
731 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
732 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
733 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
734 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
736 * NOTES
737 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
738 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
739 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
740 * fails, so does this function.
741 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
742 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
743 * is copied rather than just any pointers to it.
744 * - For by-value object types the object pointer is copied and the objects
745 * reference count increased using IUnknown_AddRef().
746 * - For all by-reference types, only the referencing pointer is copied.
748 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, const VARIANTARG* pvargSrc)
750 HRESULT hres = S_OK;
752 TRACE("(%s,%s)\n", debugstr_variant(pvargDest), debugstr_variant(pvargSrc));
754 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
755 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
756 return DISP_E_BADVARTYPE;
758 if (pvargSrc != pvargDest &&
759 SUCCEEDED(hres = VariantClear(pvargDest)))
761 *pvargDest = *pvargSrc; /* Shallow copy the value */
763 if (!V_ISBYREF(pvargSrc))
765 switch (V_VT(pvargSrc))
767 case VT_BSTR:
768 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
769 if (!V_BSTR(pvargDest))
770 hres = E_OUTOFMEMORY;
771 break;
772 case VT_RECORD:
773 hres = VARIANT_CopyIRecordInfo(pvargDest, pvargSrc);
774 break;
775 case VT_DISPATCH:
776 case VT_UNKNOWN:
777 V_UNKNOWN(pvargDest) = V_UNKNOWN(pvargSrc);
778 if (V_UNKNOWN(pvargSrc))
779 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
780 break;
781 default:
782 if (V_ISARRAY(pvargSrc))
783 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
787 return hres;
790 /* Return the byte size of a variants data */
791 static inline size_t VARIANT_DataSize(const VARIANT* pv)
793 switch (V_TYPE(pv))
795 case VT_I1:
796 case VT_UI1: return sizeof(BYTE);
797 case VT_I2:
798 case VT_UI2: return sizeof(SHORT);
799 case VT_INT:
800 case VT_UINT:
801 case VT_I4:
802 case VT_UI4: return sizeof(LONG);
803 case VT_I8:
804 case VT_UI8: return sizeof(LONGLONG);
805 case VT_R4: return sizeof(float);
806 case VT_R8: return sizeof(double);
807 case VT_DATE: return sizeof(DATE);
808 case VT_BOOL: return sizeof(VARIANT_BOOL);
809 case VT_DISPATCH:
810 case VT_UNKNOWN:
811 case VT_BSTR: return sizeof(void*);
812 case VT_CY: return sizeof(CY);
813 case VT_ERROR: return sizeof(SCODE);
815 TRACE("Shouldn't be called for variant %s!\n", debugstr_variant(pv));
816 return 0;
819 /******************************************************************************
820 * VariantCopyInd [OLEAUT32.11]
822 * Copy a variant, dereferencing it if it is by-reference.
824 * PARAMS
825 * pvargDest [O] Destination for copy
826 * pvargSrc [I] Source variant to copy
828 * RETURNS
829 * Success: S_OK. pvargDest contains a copy of pvargSrc.
830 * Failure: An HRESULT error code indicating the error.
832 * NOTES
833 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
834 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
835 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
836 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
837 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
839 * NOTES
840 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
841 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
842 * value.
843 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
844 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
845 * to it. If clearing pvargDest fails, so does this function.
847 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, const VARIANTARG* pvargSrc)
849 const VARIANTARG *pSrc = pvargSrc;
850 VARIANTARG vTmp;
851 VARTYPE vt;
852 HRESULT hres = S_OK;
854 TRACE("(%s,%s)\n", debugstr_variant(pvargDest), debugstr_variant(pvargSrc));
856 if (!V_ISBYREF(pvargSrc))
857 return VariantCopy(pvargDest, pvargSrc);
859 /* Argument checking is more lax than VariantCopy()... */
860 vt = V_TYPE(pvargSrc);
861 if (V_ISARRAY(pvargSrc) || (V_VT(pvargSrc) == (VT_RECORD|VT_BYREF)) ||
862 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
863 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
865 /* OK */
867 else
868 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
870 if (pvargSrc == pvargDest)
872 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
873 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
875 vTmp = *pvargSrc;
876 pSrc = &vTmp;
877 V_VT(pvargDest) = VT_EMPTY;
879 else
881 /* Copy into another variant. Free the variant in pvargDest */
882 if (FAILED(hres = VariantClear(pvargDest)))
884 TRACE("VariantClear() of destination failed\n");
885 return hres;
889 if (V_ISARRAY(pSrc))
891 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
892 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
894 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
896 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
897 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
899 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
901 hres = VARIANT_CopyIRecordInfo(pvargDest, pvargSrc);
903 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
904 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
906 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
907 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
908 if (*V_UNKNOWNREF(pSrc))
909 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
911 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
913 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
914 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
915 hres = E_INVALIDARG; /* Don't dereference more than one level */
916 else
917 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
919 /* Use the dereferenced variants type value, not VT_VARIANT */
920 goto VariantCopyInd_Return;
922 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
924 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
925 sizeof(DECIMAL) - sizeof(USHORT));
927 else
929 /* Copy the pointed to data into this variant */
930 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
933 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
935 VariantCopyInd_Return:
937 if (pSrc != pvargSrc)
938 VariantClear(&vTmp);
940 TRACE("returning 0x%08x, %s\n", hres, debugstr_variant(pvargDest));
941 return hres;
944 /******************************************************************************
945 * VariantChangeType [OLEAUT32.12]
947 * Change the type of a variant.
949 * PARAMS
950 * pvargDest [O] Destination for the converted variant
951 * pvargSrc [O] Source variant to change the type of
952 * wFlags [I] VARIANT_ flags from "oleauto.h"
953 * vt [I] Variant type to change pvargSrc into
955 * RETURNS
956 * Success: S_OK. pvargDest contains the converted value.
957 * Failure: An HRESULT error code describing the failure.
959 * NOTES
960 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
961 * See VariantChangeTypeEx.
963 HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG* pvargDest, const VARIANTARG* pvargSrc,
964 USHORT wFlags, VARTYPE vt)
966 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
969 /******************************************************************************
970 * VariantChangeTypeEx [OLEAUT32.147]
972 * Change the type of a variant.
974 * PARAMS
975 * pvargDest [O] Destination for the converted variant
976 * pvargSrc [O] Source variant to change the type of
977 * lcid [I] LCID for the conversion
978 * wFlags [I] VARIANT_ flags from "oleauto.h"
979 * vt [I] Variant type to change pvargSrc into
981 * RETURNS
982 * Success: S_OK. pvargDest contains the converted value.
983 * Failure: An HRESULT error code describing the failure.
985 * NOTES
986 * pvargDest and pvargSrc can point to the same variant to perform an in-place
987 * conversion. If the conversion is successful, pvargSrc will be freed.
989 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, const VARIANTARG* pvargSrc,
990 LCID lcid, USHORT wFlags, VARTYPE vt)
992 HRESULT res = S_OK;
994 TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest),
995 debugstr_variant(pvargSrc), lcid, wFlags, debugstr_vt(vt));
997 if (vt == VT_CLSID)
998 res = DISP_E_BADVARTYPE;
999 else
1001 res = VARIANT_ValidateType(V_VT(pvargSrc));
1003 if (SUCCEEDED(res))
1005 res = VARIANT_ValidateType(vt);
1007 if (SUCCEEDED(res))
1009 VARIANTARG vTmp, vSrcDeref;
1011 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
1012 res = DISP_E_TYPEMISMATCH;
1013 else
1015 V_VT(&vTmp) = VT_EMPTY;
1016 V_VT(&vSrcDeref) = VT_EMPTY;
1017 VariantClear(&vTmp);
1018 VariantClear(&vSrcDeref);
1021 if (SUCCEEDED(res))
1023 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1024 if (SUCCEEDED(res))
1026 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1027 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1028 else
1029 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1031 if (SUCCEEDED(res)) {
1032 V_VT(&vTmp) = vt;
1033 res = VariantCopy(pvargDest, &vTmp);
1035 VariantClear(&vTmp);
1036 VariantClear(&vSrcDeref);
1043 TRACE("returning 0x%08x, %s\n", res, debugstr_variant(pvargDest));
1044 return res;
1047 /* Date Conversions */
1049 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1051 /* Convert a VT_DATE value to a Julian Date */
1052 static inline int VARIANT_JulianFromDate(int dateIn)
1054 int julianDays = dateIn;
1056 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1057 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1058 return julianDays;
1061 /* Convert a Julian Date to a VT_DATE value */
1062 static inline int VARIANT_DateFromJulian(int dateIn)
1064 int julianDays = dateIn;
1066 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1067 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1068 return julianDays;
1071 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1072 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1074 int j, i, l, n;
1076 l = jd + 68569;
1077 n = l * 4 / 146097;
1078 l -= (n * 146097 + 3) / 4;
1079 i = (4000 * (l + 1)) / 1461001;
1080 l += 31 - (i * 1461) / 4;
1081 j = (l * 80) / 2447;
1082 *day = l - (j * 2447) / 80;
1083 l = j / 11;
1084 *month = (j + 2) - (12 * l);
1085 *year = 100 * (n - 49) + i + l;
1088 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1089 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1091 int m12 = (month - 14) / 12;
1093 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1094 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1097 /* Macros for accessing DOS format date/time fields */
1098 #define DOS_YEAR(x) (1980 + (x >> 9))
1099 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1100 #define DOS_DAY(x) (x & 0x1f)
1101 #define DOS_HOUR(x) (x >> 11)
1102 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1103 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1104 /* Create a DOS format date/time */
1105 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1106 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1108 /* Roll a date forwards or backwards to correct it */
1109 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1111 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1112 short iYear, iMonth, iDay, iHour, iMinute, iSecond;
1114 /* interpret values signed */
1115 iYear = lpUd->st.wYear;
1116 iMonth = lpUd->st.wMonth;
1117 iDay = lpUd->st.wDay;
1118 iHour = lpUd->st.wHour;
1119 iMinute = lpUd->st.wMinute;
1120 iSecond = lpUd->st.wSecond;
1122 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay, iMonth,
1123 iYear, iHour, iMinute, iSecond);
1125 if (iYear > 9999 || iYear < -9999)
1126 return E_INVALIDARG; /* Invalid value */
1127 /* Year 0 to 29 are treated as 2000 + year */
1128 if (iYear >= 0 && iYear < 30)
1129 iYear += 2000;
1130 /* Remaining years < 100 are treated as 1900 + year */
1131 else if (iYear >= 30 && iYear < 100)
1132 iYear += 1900;
1134 iMinute += iSecond / 60;
1135 iSecond = iSecond % 60;
1136 iHour += iMinute / 60;
1137 iMinute = iMinute % 60;
1138 iDay += iHour / 24;
1139 iHour = iHour % 24;
1140 iYear += iMonth / 12;
1141 iMonth = iMonth % 12;
1142 if (iMonth<=0) {iMonth+=12; iYear--;}
1143 while (iDay > days[iMonth])
1145 if (iMonth == 2 && IsLeapYear(iYear))
1146 iDay -= 29;
1147 else
1148 iDay -= days[iMonth];
1149 iMonth++;
1150 iYear += iMonth / 12;
1151 iMonth = iMonth % 12;
1153 while (iDay <= 0)
1155 iMonth--;
1156 if (iMonth<=0) {iMonth+=12; iYear--;}
1157 if (iMonth == 2 && IsLeapYear(iYear))
1158 iDay += 29;
1159 else
1160 iDay += days[iMonth];
1163 if (iSecond<0){iSecond+=60; iMinute--;}
1164 if (iMinute<0){iMinute+=60; iHour--;}
1165 if (iHour<0) {iHour+=24; iDay--;}
1166 if (iYear<=0) iYear+=2000;
1168 lpUd->st.wYear = iYear;
1169 lpUd->st.wMonth = iMonth;
1170 lpUd->st.wDay = iDay;
1171 lpUd->st.wHour = iHour;
1172 lpUd->st.wMinute = iMinute;
1173 lpUd->st.wSecond = iSecond;
1175 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1176 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1177 return S_OK;
1180 /**********************************************************************
1181 * DosDateTimeToVariantTime [OLEAUT32.14]
1183 * Convert a Dos format date and time into variant VT_DATE format.
1185 * PARAMS
1186 * wDosDate [I] Dos format date
1187 * wDosTime [I] Dos format time
1188 * pDateOut [O] Destination for VT_DATE format
1190 * RETURNS
1191 * Success: TRUE. pDateOut contains the converted time.
1192 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1194 * NOTES
1195 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1196 * - Dos format times are accurate to only 2 second precision.
1197 * - The format of a Dos Date is:
1198 *| Bits Values Meaning
1199 *| ---- ------ -------
1200 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1201 *| the days in the month rolls forward the extra days.
1202 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1203 *| year. 13-15 are invalid.
1204 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1205 * - The format of a Dos Time is:
1206 *| Bits Values Meaning
1207 *| ---- ------ -------
1208 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1209 *| 5-10 0-59 Minutes. 60-63 are invalid.
1210 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1212 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1213 double *pDateOut)
1215 UDATE ud;
1217 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1218 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1219 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1220 pDateOut);
1222 ud.st.wYear = DOS_YEAR(wDosDate);
1223 ud.st.wMonth = DOS_MONTH(wDosDate);
1224 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1225 return FALSE;
1226 ud.st.wDay = DOS_DAY(wDosDate);
1227 ud.st.wHour = DOS_HOUR(wDosTime);
1228 ud.st.wMinute = DOS_MINUTE(wDosTime);
1229 ud.st.wSecond = DOS_SECOND(wDosTime);
1230 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1231 if (ud.st.wHour > 23 || ud.st.wMinute > 59 || ud.st.wSecond > 59)
1232 return FALSE; /* Invalid values in Dos*/
1234 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1237 /**********************************************************************
1238 * VariantTimeToDosDateTime [OLEAUT32.13]
1240 * Convert a variant format date into a Dos format date and time.
1242 * dateIn [I] VT_DATE time format
1243 * pwDosDate [O] Destination for Dos format date
1244 * pwDosTime [O] Destination for Dos format time
1246 * RETURNS
1247 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1248 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1250 * NOTES
1251 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1253 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1255 UDATE ud;
1257 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1259 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1260 return FALSE;
1262 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1263 return FALSE;
1265 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1266 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1268 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1269 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1270 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1271 return TRUE;
1274 /***********************************************************************
1275 * SystemTimeToVariantTime [OLEAUT32.184]
1277 * Convert a System format date and time into variant VT_DATE format.
1279 * PARAMS
1280 * lpSt [I] System format date and time
1281 * pDateOut [O] Destination for VT_DATE format date
1283 * RETURNS
1284 * Success: TRUE. *pDateOut contains the converted value.
1285 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1287 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1289 UDATE ud;
1291 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1292 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1294 if (lpSt->wMonth > 12)
1295 return FALSE;
1296 if (lpSt->wDay > 31)
1297 return FALSE;
1298 if ((short)lpSt->wYear < 0)
1299 return FALSE;
1301 ud.st = *lpSt;
1302 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1305 /***********************************************************************
1306 * VariantTimeToSystemTime [OLEAUT32.185]
1308 * Convert a variant VT_DATE into a System format date and time.
1310 * PARAMS
1311 * datein [I] Variant VT_DATE format date
1312 * lpSt [O] Destination for System format date and time
1314 * RETURNS
1315 * Success: TRUE. *lpSt contains the converted value.
1316 * Failure: FALSE, if dateIn is too large or small.
1318 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1320 UDATE ud;
1322 TRACE("(%g,%p)\n", dateIn, lpSt);
1324 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1325 return FALSE;
1327 *lpSt = ud.st;
1328 return TRUE;
1331 /***********************************************************************
1332 * VarDateFromUdateEx [OLEAUT32.319]
1334 * Convert an unpacked format date and time to a variant VT_DATE.
1336 * PARAMS
1337 * pUdateIn [I] Unpacked format date and time to convert
1338 * lcid [I] Locale identifier for the conversion
1339 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1340 * pDateOut [O] Destination for variant VT_DATE.
1342 * RETURNS
1343 * Success: S_OK. *pDateOut contains the converted value.
1344 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1346 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1348 UDATE ud;
1349 double dateVal = 0;
1351 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1352 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1353 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1354 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1355 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1357 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1358 FIXME("lcid possibly not handled, treating as en-us\n");
1359 if (dwFlags & ~(VAR_TIMEVALUEONLY|VAR_DATEVALUEONLY))
1360 FIXME("unsupported flags: %x\n", dwFlags);
1362 ud = *pUdateIn;
1364 if (dwFlags & VAR_VALIDDATE)
1365 WARN("Ignoring VAR_VALIDDATE\n");
1367 if (FAILED(VARIANT_RollUdate(&ud)))
1368 return E_INVALIDARG;
1370 /* Date */
1371 if (!(dwFlags & VAR_TIMEVALUEONLY))
1372 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1374 if ((dwFlags & VAR_TIMEVALUEONLY) || !(dwFlags & VAR_DATEVALUEONLY))
1376 double 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;
1384 TRACE("Returning %g\n", dateVal);
1385 *pDateOut = dateVal;
1386 return S_OK;
1389 /***********************************************************************
1390 * VarDateFromUdate [OLEAUT32.330]
1392 * Convert an unpacked format date and time to a variant VT_DATE.
1394 * PARAMS
1395 * pUdateIn [I] Unpacked format date and time to convert
1396 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1397 * pDateOut [O] Destination for variant VT_DATE.
1399 * RETURNS
1400 * Success: S_OK. *pDateOut contains the converted value.
1401 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1403 * NOTES
1404 * This function uses the United States English locale for the conversion. Use
1405 * VarDateFromUdateEx() for alternate locales.
1407 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1409 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1411 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1414 /***********************************************************************
1415 * VarUdateFromDate [OLEAUT32.331]
1417 * Convert a variant VT_DATE into an unpacked format date and time.
1419 * PARAMS
1420 * datein [I] Variant VT_DATE format date
1421 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1422 * lpUdate [O] Destination for unpacked format date and time
1424 * RETURNS
1425 * Success: S_OK. *lpUdate contains the converted value.
1426 * Failure: E_INVALIDARG, if dateIn is too large or small.
1428 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1430 /* Cumulative totals of days per month */
1431 static const USHORT cumulativeDays[] =
1433 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1435 double datePart, timePart;
1436 int julianDays;
1438 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1440 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1441 return E_INVALIDARG;
1443 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1444 /* Compensate for int truncation (always downwards) */
1445 timePart = fabs(dateIn - datePart) + 0.00000000001;
1446 if (timePart >= 1.0)
1447 timePart -= 0.00000000001;
1449 /* Date */
1450 julianDays = VARIANT_JulianFromDate(dateIn);
1451 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1452 &lpUdate->st.wDay);
1454 datePart = (datePart + 1.5) / 7.0;
1455 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1456 if (lpUdate->st.wDayOfWeek == 0)
1457 lpUdate->st.wDayOfWeek = 5;
1458 else if (lpUdate->st.wDayOfWeek == 1)
1459 lpUdate->st.wDayOfWeek = 6;
1460 else
1461 lpUdate->st.wDayOfWeek -= 2;
1463 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1464 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1465 else
1466 lpUdate->wDayOfYear = 0;
1468 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1469 lpUdate->wDayOfYear += lpUdate->st.wDay;
1471 /* Time */
1472 timePart *= 24.0;
1473 lpUdate->st.wHour = timePart;
1474 timePart -= lpUdate->st.wHour;
1475 timePart *= 60.0;
1476 lpUdate->st.wMinute = timePart;
1477 timePart -= lpUdate->st.wMinute;
1478 timePart *= 60.0;
1479 lpUdate->st.wSecond = timePart;
1480 timePart -= lpUdate->st.wSecond;
1481 lpUdate->st.wMilliseconds = 0;
1482 if (timePart > 0.5)
1484 /* Round the milliseconds, adjusting the time/date forward if needed */
1485 if (lpUdate->st.wSecond < 59)
1486 lpUdate->st.wSecond++;
1487 else
1489 lpUdate->st.wSecond = 0;
1490 if (lpUdate->st.wMinute < 59)
1491 lpUdate->st.wMinute++;
1492 else
1494 lpUdate->st.wMinute = 0;
1495 if (lpUdate->st.wHour < 23)
1496 lpUdate->st.wHour++;
1497 else
1499 lpUdate->st.wHour = 0;
1500 /* Roll over a whole day */
1501 if (++lpUdate->st.wDay > 28)
1502 VARIANT_RollUdate(lpUdate);
1507 return S_OK;
1510 #define GET_NUMBER_TEXT(fld,name) \
1511 buff[0] = 0; \
1512 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1513 WARN("buffer too small for " #fld "\n"); \
1514 else \
1515 if (buff[0]) lpChars->name = buff[0]; \
1516 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1518 /* Get the valid number characters for an lcid */
1519 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1521 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',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(&cache_cs);
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(&cache_cs);
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, ARRAY_SIZE(buff)))
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(&cache_cs);
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 static inline BOOL is_digit(WCHAR c)
1577 return '0' <= c && c <= '9';
1580 /**********************************************************************
1581 * VarParseNumFromStr [OLEAUT32.46]
1583 * Parse a string containing a number into a NUMPARSE structure.
1585 * PARAMS
1586 * lpszStr [I] String to parse number from
1587 * lcid [I] Locale Id for the conversion
1588 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1589 * pNumprs [I/O] Destination for parsed number
1590 * rgbDig [O] Destination for digits read in
1592 * RETURNS
1593 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1594 * the number.
1595 * Failure: E_INVALIDARG, if any parameter is invalid.
1596 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1597 * incorrectly.
1598 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1600 * NOTES
1601 * pNumprs must have the following fields set:
1602 * cDig: Set to the size of rgbDig.
1603 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1604 * from "oleauto.h".
1606 * FIXME
1607 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1608 * numerals, so this has not been implemented.
1610 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1611 NUMPARSE *pNumprs, BYTE *rgbDig)
1613 VARIANT_NUMBER_CHARS chars;
1614 BYTE rgbTmp[1024];
1615 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1616 int iMaxDigits = ARRAY_SIZE(rgbTmp);
1617 int cchUsed = 0;
1619 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1621 if (!pNumprs || !rgbDig)
1622 return E_INVALIDARG;
1624 if (pNumprs->cDig < iMaxDigits)
1625 iMaxDigits = pNumprs->cDig;
1627 pNumprs->cDig = 0;
1628 pNumprs->dwOutFlags = 0;
1629 pNumprs->cchUsed = 0;
1630 pNumprs->nBaseShift = 0;
1631 pNumprs->nPwr10 = 0;
1633 if (!lpszStr)
1634 return DISP_E_TYPEMISMATCH;
1636 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1638 /* First consume all the leading symbols and space from the string */
1639 while (1)
1641 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && iswspace(*lpszStr))
1643 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1646 cchUsed++;
1647 lpszStr++;
1648 } while (iswspace(*lpszStr));
1650 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1651 *lpszStr == chars.cPositiveSymbol &&
1652 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1654 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1655 cchUsed++;
1656 lpszStr++;
1658 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1659 *lpszStr == chars.cNegativeSymbol &&
1660 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1662 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1663 cchUsed++;
1664 lpszStr++;
1666 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1667 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1668 *lpszStr == chars.cCurrencyLocal &&
1669 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1671 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1672 cchUsed++;
1673 lpszStr++;
1674 /* Only accept currency characters */
1675 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1676 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1678 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1679 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1681 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1682 cchUsed++;
1683 lpszStr++;
1685 else
1686 break;
1689 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1691 /* Only accept non-currency characters */
1692 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1693 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1696 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1697 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1699 dwState |= B_PROCESSING_HEX;
1700 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1701 cchUsed=cchUsed+2;
1702 lpszStr=lpszStr+2;
1704 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1705 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1707 dwState |= B_PROCESSING_OCT;
1708 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1709 cchUsed=cchUsed+2;
1710 lpszStr=lpszStr+2;
1713 /* Strip Leading zeros */
1714 while (*lpszStr == '0')
1716 dwState |= B_LEADING_ZERO;
1717 cchUsed++;
1718 lpszStr++;
1721 while (*lpszStr)
1723 if (is_digit(*lpszStr))
1725 if (dwState & B_PROCESSING_EXPONENT)
1727 int exponentSize = 0;
1728 if (dwState & B_EXPONENT_START)
1730 if (!is_digit(*lpszStr))
1731 break; /* No exponent digits - invalid */
1732 while (*lpszStr == '0')
1734 /* Skip leading zero's in the exponent */
1735 cchUsed++;
1736 lpszStr++;
1740 while (is_digit(*lpszStr))
1742 exponentSize *= 10;
1743 exponentSize += *lpszStr - '0';
1744 cchUsed++;
1745 lpszStr++;
1747 if (dwState & B_NEGATIVE_EXPONENT)
1748 exponentSize = -exponentSize;
1749 /* Add the exponent into the powers of 10 */
1750 pNumprs->nPwr10 += exponentSize;
1751 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1752 lpszStr--; /* back up to allow processing of next char */
1754 else
1756 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1757 && !(dwState & B_PROCESSING_OCT))
1759 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1761 if (*lpszStr != '0')
1762 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1764 /* This digit can't be represented, but count it in nPwr10 */
1765 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1766 pNumprs->nPwr10--;
1767 else
1768 pNumprs->nPwr10++;
1770 else
1772 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9')))
1773 break;
1775 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1776 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1778 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1780 pNumprs->cDig++;
1781 cchUsed++;
1784 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1786 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1787 cchUsed++;
1789 else if (*lpszStr == chars.cDecimalPoint &&
1790 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1791 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1793 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1794 cchUsed++;
1796 /* If we have no digits so far, skip leading zeros */
1797 if (!pNumprs->cDig)
1799 while (lpszStr[1] == '0')
1801 dwState |= B_LEADING_ZERO;
1802 cchUsed++;
1803 lpszStr++;
1804 pNumprs->nPwr10--;
1808 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1809 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1810 dwState & B_PROCESSING_HEX)
1812 if (pNumprs->cDig >= iMaxDigits)
1814 return DISP_E_OVERFLOW;
1816 else
1818 if (*lpszStr >= 'a')
1819 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1820 else
1821 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1823 pNumprs->cDig++;
1824 cchUsed++;
1826 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1827 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1828 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1830 dwState |= B_PROCESSING_EXPONENT;
1831 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1832 cchUsed++;
1834 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1836 cchUsed++; /* Ignore positive exponent */
1838 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1840 dwState |= B_NEGATIVE_EXPONENT;
1841 cchUsed++;
1843 else
1844 break; /* Stop at an unrecognised character */
1846 lpszStr++;
1849 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1851 /* Ensure a 0 on its own gets stored */
1852 pNumprs->cDig = 1;
1853 rgbTmp[0] = 0;
1856 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1858 pNumprs->cchUsed = cchUsed;
1859 WARN("didn't completely parse exponent\n");
1860 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1863 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1865 if (dwState & B_INEXACT_ZEROS)
1866 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1867 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1869 /* copy all of the digits into the output digit buffer */
1870 /* this is exactly what windows does although it also returns */
1871 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1872 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1874 if (dwState & B_PROCESSING_HEX) {
1875 /* hex numbers have always the same format */
1876 pNumprs->nPwr10=0;
1877 pNumprs->nBaseShift=4;
1878 } else {
1879 if (dwState & B_PROCESSING_OCT) {
1880 /* oct numbers have always the same format */
1881 pNumprs->nPwr10=0;
1882 pNumprs->nBaseShift=3;
1883 } else {
1884 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1886 pNumprs->nPwr10++;
1887 pNumprs->cDig--;
1891 } else
1893 /* Remove trailing zeros from the last (whole number or decimal) part */
1894 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1896 pNumprs->nPwr10++;
1897 pNumprs->cDig--;
1901 if (pNumprs->cDig <= iMaxDigits)
1902 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1903 else
1904 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1906 /* Copy the digits we processed into rgbDig */
1907 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1909 /* Consume any trailing symbols and space */
1910 while (1)
1912 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && iswspace(*lpszStr))
1914 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1917 cchUsed++;
1918 lpszStr++;
1919 } while (iswspace(*lpszStr));
1921 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1922 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1923 *lpszStr == chars.cPositiveSymbol)
1925 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1926 cchUsed++;
1927 lpszStr++;
1929 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1930 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1931 *lpszStr == chars.cNegativeSymbol)
1933 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1934 cchUsed++;
1935 lpszStr++;
1937 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1938 pNumprs->dwOutFlags & NUMPRS_PARENS)
1940 cchUsed++;
1941 lpszStr++;
1942 pNumprs->dwOutFlags |= NUMPRS_NEG;
1944 else
1945 break;
1948 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1950 pNumprs->cchUsed = cchUsed;
1951 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1954 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1955 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1957 if (!pNumprs->cDig)
1958 return DISP_E_TYPEMISMATCH; /* No Number found */
1960 pNumprs->cchUsed = cchUsed;
1961 return S_OK;
1964 /* VTBIT flags indicating an integer value */
1965 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1966 /* VTBIT flags indicating a real number value */
1967 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1969 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1970 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1971 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1972 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1974 /**********************************************************************
1975 * VarNumFromParseNum [OLEAUT32.47]
1977 * Convert a NUMPARSE structure into a numeric Variant type.
1979 * PARAMS
1980 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1981 * rgbDig [I] Source for the numbers digits
1982 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1983 * pVarDst [O] Destination for the converted Variant value.
1985 * RETURNS
1986 * Success: S_OK. pVarDst contains the converted value.
1987 * Failure: E_INVALIDARG, if any parameter is invalid.
1988 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1990 * NOTES
1991 * - The smallest favoured type present in dwVtBits that can represent the
1992 * number in pNumprs without losing precision is used.
1993 * - Signed types are preferred over unsigned types of the same size.
1994 * - Preferred types in order are: integer, float, double, currency then decimal.
1995 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1996 * for details of the rounding method.
1997 * - pVarDst is not cleared before the result is stored in it.
1998 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1999 * design?): If some other VTBIT's for integers are specified together
2000 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
2001 * the number to the smallest requested integer truncating this way the
2002 * number. Wine doesn't implement this "feature" (yet?).
2004 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
2005 ULONG dwVtBits, VARIANT *pVarDst)
2007 /* Scale factors and limits for double arithmetic */
2008 static const double dblMultipliers[11] = {
2009 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2010 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2012 static const double dblMinimums[11] = {
2013 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
2014 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
2015 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
2017 static const double dblMaximums[11] = {
2018 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
2019 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
2020 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
2023 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
2025 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
2027 if (pNumprs->nBaseShift)
2029 /* nBaseShift indicates a hex or octal number */
2030 ULONG64 ul64 = 0;
2031 LONG64 l64;
2032 int i;
2034 /* Convert the hex or octal number string into a UI64 */
2035 for (i = 0; i < pNumprs->cDig; i++)
2037 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
2039 TRACE("Overflow multiplying digits\n");
2040 return DISP_E_OVERFLOW;
2042 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
2045 /* also make a negative representation */
2046 l64=-ul64;
2048 /* Try signed and unsigned types in size order */
2049 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
2051 V_VT(pVarDst) = VT_I1;
2052 V_I1(pVarDst) = ul64;
2053 return S_OK;
2055 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2057 V_VT(pVarDst) = VT_UI1;
2058 V_UI1(pVarDst) = ul64;
2059 return S_OK;
2061 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2063 V_VT(pVarDst) = VT_I2;
2064 V_I2(pVarDst) = ul64;
2065 return S_OK;
2067 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2069 V_VT(pVarDst) = VT_UI2;
2070 V_UI2(pVarDst) = ul64;
2071 return S_OK;
2073 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2075 V_VT(pVarDst) = VT_I4;
2076 V_I4(pVarDst) = ul64;
2077 return S_OK;
2079 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2081 V_VT(pVarDst) = VT_UI4;
2082 V_UI4(pVarDst) = ul64;
2083 return S_OK;
2085 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2087 V_VT(pVarDst) = VT_I8;
2088 V_I8(pVarDst) = ul64;
2089 return S_OK;
2091 else if (dwVtBits & VTBIT_UI8)
2093 V_VT(pVarDst) = VT_UI8;
2094 V_UI8(pVarDst) = ul64;
2095 return S_OK;
2097 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2099 V_VT(pVarDst) = VT_DECIMAL;
2100 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2101 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2102 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2103 return S_OK;
2105 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2107 V_VT(pVarDst) = VT_R4;
2108 if (ul64 <= I4_MAX)
2109 V_R4(pVarDst) = ul64;
2110 else
2111 V_R4(pVarDst) = l64;
2112 return S_OK;
2114 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2116 V_VT(pVarDst) = VT_R8;
2117 if (ul64 <= I4_MAX)
2118 V_R8(pVarDst) = ul64;
2119 else
2120 V_R8(pVarDst) = l64;
2121 return S_OK;
2124 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2125 return DISP_E_OVERFLOW;
2128 /* Count the number of relevant fractional and whole digits stored,
2129 * And compute the divisor/multiplier to scale the number by.
2131 if (pNumprs->nPwr10 < 0)
2133 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2135 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2136 wholeNumberDigits = 0;
2137 fractionalDigits = pNumprs->cDig;
2138 divisor10 = -pNumprs->nPwr10;
2140 else
2142 /* An exactly represented real number e.g. 1.024 */
2143 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2144 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2145 divisor10 = pNumprs->cDig - wholeNumberDigits;
2148 else if (pNumprs->nPwr10 == 0)
2150 /* An exactly represented whole number e.g. 1024 */
2151 wholeNumberDigits = pNumprs->cDig;
2152 fractionalDigits = 0;
2154 else /* pNumprs->nPwr10 > 0 */
2156 /* A whole number followed by nPwr10 0's e.g. 102400 */
2157 wholeNumberDigits = pNumprs->cDig;
2158 fractionalDigits = 0;
2159 multiplier10 = pNumprs->nPwr10;
2162 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2163 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2164 multiplier10, divisor10);
2166 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2167 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL))))
2169 /* We have one or more integer output choices, and either:
2170 * 1) An integer input value, or
2171 * 2) A real number input value but no floating output choices.
2172 * Alternately, we have a DECIMAL output available and an integer input.
2174 * So, place the integer value into pVarDst, using the smallest type
2175 * possible and preferring signed over unsigned types.
2177 BOOL bOverflow = FALSE, bNegative;
2178 ULONG64 ul64 = 0;
2179 int i;
2181 /* Convert the integer part of the number into a UI8 */
2182 for (i = 0; i < wholeNumberDigits; i++)
2184 if (ul64 > UI8_MAX / 10 || (ul64 == UI8_MAX / 10 && rgbDig[i] > UI8_MAX % 10))
2186 TRACE("Overflow multiplying digits\n");
2187 bOverflow = TRUE;
2188 break;
2190 ul64 = ul64 * 10 + rgbDig[i];
2193 /* Account for the scale of the number */
2194 if (!bOverflow && multiplier10)
2196 for (i = 0; i < multiplier10; i++)
2198 if (ul64 > (UI8_MAX / 10))
2200 TRACE("Overflow scaling number\n");
2201 bOverflow = TRUE;
2202 break;
2204 ul64 = ul64 * 10;
2208 /* If we have any fractional digits, round the value.
2209 * Note we don't have to do this if divisor10 is < 1,
2210 * because this means the fractional part must be < 0.5
2212 if (!bOverflow && fractionalDigits && divisor10 > 0)
2214 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2215 BOOL bAdjust = FALSE;
2217 TRACE("first decimal value is %d\n", *fracDig);
2219 if (*fracDig > 5)
2220 bAdjust = TRUE; /* > 0.5 */
2221 else if (*fracDig == 5)
2223 for (i = 1; i < fractionalDigits; i++)
2225 if (fracDig[i])
2227 bAdjust = TRUE; /* > 0.5 */
2228 break;
2231 /* If exactly 0.5, round only odd values */
2232 if (i == fractionalDigits && (ul64 & 1))
2233 bAdjust = TRUE;
2236 if (bAdjust)
2238 if (ul64 == UI8_MAX)
2240 TRACE("Overflow after rounding\n");
2241 bOverflow = TRUE;
2243 ul64++;
2247 /* Zero is not a negative number */
2248 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64;
2250 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2252 /* For negative integers, try the signed types in size order */
2253 if (!bOverflow && bNegative)
2255 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2257 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2259 V_VT(pVarDst) = VT_I1;
2260 V_I1(pVarDst) = -ul64;
2261 return S_OK;
2263 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2265 V_VT(pVarDst) = VT_I2;
2266 V_I2(pVarDst) = -ul64;
2267 return S_OK;
2269 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2271 V_VT(pVarDst) = VT_I4;
2272 V_I4(pVarDst) = -ul64;
2273 return S_OK;
2275 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2277 V_VT(pVarDst) = VT_I8;
2278 V_I8(pVarDst) = -ul64;
2279 return S_OK;
2281 else if ((dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL)) == VTBIT_DECIMAL)
2283 /* Decimal is only output choice left - fast path */
2284 V_VT(pVarDst) = VT_DECIMAL;
2285 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2286 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2287 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2288 return S_OK;
2292 else if (!bOverflow)
2294 /* For positive integers, try signed then unsigned types in size order */
2295 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2297 V_VT(pVarDst) = VT_I1;
2298 V_I1(pVarDst) = ul64;
2299 return S_OK;
2301 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2303 V_VT(pVarDst) = VT_UI1;
2304 V_UI1(pVarDst) = ul64;
2305 return S_OK;
2307 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2309 V_VT(pVarDst) = VT_I2;
2310 V_I2(pVarDst) = ul64;
2311 return S_OK;
2313 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2315 V_VT(pVarDst) = VT_UI2;
2316 V_UI2(pVarDst) = ul64;
2317 return S_OK;
2319 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2321 V_VT(pVarDst) = VT_I4;
2322 V_I4(pVarDst) = ul64;
2323 return S_OK;
2325 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2327 V_VT(pVarDst) = VT_UI4;
2328 V_UI4(pVarDst) = ul64;
2329 return S_OK;
2331 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2333 V_VT(pVarDst) = VT_I8;
2334 V_I8(pVarDst) = ul64;
2335 return S_OK;
2337 else if (dwVtBits & VTBIT_UI8)
2339 V_VT(pVarDst) = VT_UI8;
2340 V_UI8(pVarDst) = ul64;
2341 return S_OK;
2343 else if ((dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL)) == VTBIT_DECIMAL)
2345 /* Decimal is only output choice left - fast path */
2346 V_VT(pVarDst) = VT_DECIMAL;
2347 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2348 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2349 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2350 return S_OK;
2355 if (dwVtBits & REAL_VTBITS)
2357 /* Try to put the number into a float or real */
2358 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2359 double whole = 0.0;
2360 int i;
2362 /* Convert the number into a double */
2363 for (i = 0; i < pNumprs->cDig; i++)
2364 whole = whole * 10.0 + rgbDig[i];
2366 TRACE("Whole double value is %16.16g\n", whole);
2368 /* Account for the scale */
2369 while (multiplier10 > 10)
2371 if (whole > dblMaximums[10])
2373 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2374 bOverflow = TRUE;
2375 break;
2377 whole = whole * dblMultipliers[10];
2378 multiplier10 -= 10;
2380 if (multiplier10 && !bOverflow)
2382 if (whole > dblMaximums[multiplier10])
2384 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2385 bOverflow = TRUE;
2387 else
2388 whole = whole * dblMultipliers[multiplier10];
2391 if (!bOverflow)
2392 TRACE("Scaled double value is %16.16g\n", whole);
2394 while (divisor10 > 10 && !bOverflow)
2396 if (whole < dblMinimums[10] && whole != 0)
2398 whole = 0; /* ignore underflow */
2399 divisor10 = 0;
2400 break;
2402 whole = whole / dblMultipliers[10];
2403 divisor10 -= 10;
2405 if (divisor10 && !bOverflow)
2407 if (whole < dblMinimums[divisor10] && whole != 0)
2409 whole = 0; /* ignore underflow */
2410 divisor10 = 0;
2412 else
2413 whole = whole / dblMultipliers[divisor10];
2415 if (!bOverflow)
2416 TRACE("Final double value is %16.16g\n", whole);
2418 if (dwVtBits & VTBIT_R4 &&
2419 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2421 TRACE("Set R4 to final value\n");
2422 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2423 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2424 return S_OK;
2427 if (dwVtBits & VTBIT_R8)
2429 TRACE("Set R8 to final value\n");
2430 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2431 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2432 return S_OK;
2435 if (dwVtBits & VTBIT_CY)
2437 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2439 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2440 TRACE("Set CY to final value\n");
2441 return S_OK;
2443 TRACE("Value Overflows CY\n");
2447 if (dwVtBits & VTBIT_DECIMAL)
2449 int i;
2450 ULONG carry;
2451 ULONG64 tmp;
2452 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2454 DECIMAL_SETZERO(*pDec);
2455 DEC_LO32(pDec) = 0;
2457 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2458 DEC_SIGN(pDec) = DECIMAL_NEG;
2459 else
2460 DEC_SIGN(pDec) = DECIMAL_POS;
2462 /* Factor the significant digits */
2463 for (i = 0; i < pNumprs->cDig; i++)
2465 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2466 carry = (ULONG)(tmp >> 32);
2467 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2468 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2469 carry = (ULONG)(tmp >> 32);
2470 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2471 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2472 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2474 if (tmp >> 32 & UI4_MAX)
2476 VarNumFromParseNum_DecOverflow:
2477 TRACE("Overflow\n");
2478 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2479 return DISP_E_OVERFLOW;
2483 /* Account for the scale of the number */
2484 while (multiplier10 > 0)
2486 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2487 carry = (ULONG)(tmp >> 32);
2488 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2489 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2490 carry = (ULONG)(tmp >> 32);
2491 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2492 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2493 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2495 if (tmp >> 32 & UI4_MAX)
2496 goto VarNumFromParseNum_DecOverflow;
2497 multiplier10--;
2499 DEC_SCALE(pDec) = divisor10;
2501 V_VT(pVarDst) = VT_DECIMAL;
2502 return S_OK;
2504 return DISP_E_OVERFLOW; /* No more output choices */
2507 /**********************************************************************
2508 * VarCat [OLEAUT32.318]
2510 * Concatenates one variant onto another.
2512 * PARAMS
2513 * left [I] First variant
2514 * right [I] Second variant
2515 * result [O] Result variant
2517 * RETURNS
2518 * Success: S_OK.
2519 * Failure: An HRESULT error code indicating the error.
2521 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2523 BSTR left_str = NULL, right_str = NULL;
2524 VARTYPE leftvt, rightvt;
2525 HRESULT hres;
2527 TRACE("%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), out);
2529 leftvt = V_VT(left);
2530 rightvt = V_VT(right);
2532 /* when both left and right are NULL the result is NULL */
2533 if (leftvt == VT_NULL && rightvt == VT_NULL)
2535 V_VT(out) = VT_NULL;
2536 return S_OK;
2539 /* There are many special case for errors and return types */
2540 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2541 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2542 hres = DISP_E_TYPEMISMATCH;
2543 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2544 leftvt == VT_R4 || leftvt == VT_R8 ||
2545 leftvt == VT_CY || leftvt == VT_BOOL ||
2546 leftvt == VT_BSTR || leftvt == VT_I1 ||
2547 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2548 leftvt == VT_UI4 || leftvt == VT_I8 ||
2549 leftvt == VT_UI8 || leftvt == VT_INT ||
2550 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2551 leftvt == VT_NULL || leftvt == VT_DATE ||
2552 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2554 (rightvt == VT_I2 || rightvt == VT_I4 ||
2555 rightvt == VT_R4 || rightvt == VT_R8 ||
2556 rightvt == VT_CY || rightvt == VT_BOOL ||
2557 rightvt == VT_BSTR || rightvt == VT_I1 ||
2558 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2559 rightvt == VT_UI4 || rightvt == VT_I8 ||
2560 rightvt == VT_UI8 || rightvt == VT_INT ||
2561 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2562 rightvt == VT_NULL || rightvt == VT_DATE ||
2563 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2564 hres = S_OK;
2565 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2566 hres = DISP_E_TYPEMISMATCH;
2567 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2568 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2569 hres = DISP_E_TYPEMISMATCH;
2570 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2571 rightvt == VT_DECIMAL)
2572 hres = DISP_E_BADVARTYPE;
2573 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2574 hres = DISP_E_TYPEMISMATCH;
2575 else if (leftvt == VT_VARIANT)
2576 hres = DISP_E_TYPEMISMATCH;
2577 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2578 leftvt == VT_NULL || leftvt == VT_I2 ||
2579 leftvt == VT_I4 || leftvt == VT_R4 ||
2580 leftvt == VT_R8 || leftvt == VT_CY ||
2581 leftvt == VT_DATE || leftvt == VT_BSTR ||
2582 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2583 leftvt == VT_I1 || leftvt == VT_UI1 ||
2584 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2585 leftvt == VT_I8 || leftvt == VT_UI8 ||
2586 leftvt == VT_INT || leftvt == VT_UINT))
2587 hres = DISP_E_TYPEMISMATCH;
2588 else
2589 hres = DISP_E_BADVARTYPE;
2591 /* if result type is not S_OK, then no need to go further */
2592 if (hres != S_OK)
2594 V_VT(out) = VT_EMPTY;
2595 return hres;
2598 if (leftvt == VT_BSTR)
2599 left_str = V_BSTR(left);
2600 else
2602 VARIANT converted, *tmp = left;
2604 VariantInit(&converted);
2605 if(leftvt == VT_DISPATCH)
2607 hres = VARIANT_FetchDispatchValue(left, &converted);
2608 if(FAILED(hres))
2609 goto failed;
2611 tmp = &converted;
2614 hres = VariantChangeTypeEx(&converted, tmp, 0, VARIANT_ALPHABOOL|VARIANT_LOCALBOOL, VT_BSTR);
2615 if (SUCCEEDED(hres))
2616 left_str = V_BSTR(&converted);
2617 else if (hres != DISP_E_TYPEMISMATCH)
2619 VariantClear(&converted);
2620 goto failed;
2624 if (rightvt == VT_BSTR)
2625 right_str = V_BSTR(right);
2626 else
2628 VARIANT converted, *tmp = right;
2630 VariantInit(&converted);
2631 if(rightvt == VT_DISPATCH)
2633 hres = VARIANT_FetchDispatchValue(right, &converted);
2634 if(FAILED(hres))
2635 goto failed;
2637 tmp = &converted;
2640 hres = VariantChangeTypeEx(&converted, tmp, 0, VARIANT_ALPHABOOL|VARIANT_LOCALBOOL, VT_BSTR);
2641 if (SUCCEEDED(hres))
2642 right_str = V_BSTR(&converted);
2643 else if (hres != DISP_E_TYPEMISMATCH)
2645 VariantClear(&converted);
2646 goto failed;
2651 V_VT(out) = VT_BSTR;
2652 hres = VarBstrCat(left_str, right_str, &V_BSTR(out));
2654 failed:
2655 if(V_VT(left) != VT_BSTR)
2656 SysFreeString(left_str);
2657 if(V_VT(right) != VT_BSTR)
2658 SysFreeString(right_str);
2659 return hres;
2663 /* Wrapper around VariantChangeTypeEx() which permits changing a
2664 variant with VT_RESERVED flag set. Needed by VarCmp. */
2665 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2666 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2668 VARIANTARG vtmpsrc = *pvargSrc;
2670 V_VT(&vtmpsrc) &= ~VT_RESERVED;
2671 return VariantChangeTypeEx(pvargDest,&vtmpsrc,lcid,wFlags,vt);
2674 /**********************************************************************
2675 * VarCmp [OLEAUT32.176]
2677 * Compare two variants.
2679 * PARAMS
2680 * left [I] First variant
2681 * right [I] Second variant
2682 * lcid [I] LCID (locale identifier) for the comparison
2683 * flags [I] Flags to be used in the comparison:
2684 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2685 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2687 * RETURNS
2688 * VARCMP_LT: left variant is less than right variant.
2689 * VARCMP_EQ: input variants are equal.
2690 * VARCMP_GT: left variant is greater than right variant.
2691 * VARCMP_NULL: either one of the input variants is NULL.
2692 * Failure: An HRESULT error code indicating the error.
2694 * NOTES
2695 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2696 * UI8 and UINT as input variants. INT is accepted only as left variant.
2698 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2699 * an ERROR variant will trigger an error.
2701 * Both input variants can have VT_RESERVED flag set which is ignored
2702 * unless one and only one of the variants is a BSTR and the other one
2703 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2704 * different meaning:
2705 * - BSTR and other: BSTR is always greater than the other variant.
2706 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2707 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2708 * comparison will take place else the BSTR is always greater.
2709 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2710 * variant is ignored and the return value depends only on the sign
2711 * of the BSTR if it is a number else the BSTR is always greater. A
2712 * positive BSTR is greater, a negative one is smaller than the other
2713 * variant.
2715 * SEE
2716 * VarBstrCmp for the lcid and flags usage.
2718 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2720 VARTYPE lvt, rvt, vt;
2721 VARIANT rv,lv;
2722 DWORD xmask;
2723 HRESULT rc;
2725 TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left), debugstr_variant(right), lcid, flags);
2727 lvt = V_VT(left) & VT_TYPEMASK;
2728 rvt = V_VT(right) & VT_TYPEMASK;
2729 xmask = (1 << lvt) | (1 << rvt);
2731 /* If we have any flag set except VT_RESERVED bail out.
2732 Same for the left input variant type > VT_INT and for the
2733 right input variant type > VT_I8. Yes, VT_INT is only supported
2734 as left variant. Go figure */
2735 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2736 lvt > VT_INT || rvt > VT_I8) {
2737 return DISP_E_BADVARTYPE;
2740 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2741 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2742 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2743 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2744 return DISP_E_TYPEMISMATCH;
2746 /* If both variants are VT_ERROR return VARCMP_EQ */
2747 if (xmask == VTBIT_ERROR)
2748 return VARCMP_EQ;
2749 else if (xmask & VTBIT_ERROR)
2750 return DISP_E_TYPEMISMATCH;
2752 if (xmask & VTBIT_NULL)
2753 return VARCMP_NULL;
2755 VariantInit(&lv);
2756 VariantInit(&rv);
2758 /* Two BSTRs, ignore VT_RESERVED */
2759 if (xmask == VTBIT_BSTR)
2760 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2762 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2763 if (xmask & VTBIT_BSTR) {
2764 VARIANT *bstrv, *nonbv;
2765 VARTYPE nonbvt;
2766 int swap = 0;
2768 /* Swap the variants so the BSTR is always on the left */
2769 if (lvt == VT_BSTR) {
2770 bstrv = left;
2771 nonbv = right;
2772 nonbvt = rvt;
2773 } else {
2774 swap = 1;
2775 bstrv = right;
2776 nonbv = left;
2777 nonbvt = lvt;
2780 /* BSTR and EMPTY: ignore VT_RESERVED */
2781 if (nonbvt == VT_EMPTY)
2782 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2783 else {
2784 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2785 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2787 if (!breserv && !nreserv)
2788 /* No VT_RESERVED set ==> BSTR always greater */
2789 rc = VARCMP_GT;
2790 else if (breserv && !nreserv) {
2791 /* BSTR has VT_RESERVED set. Do a string comparison */
2792 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2793 if (FAILED(rc))
2794 return rc;
2795 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2796 VariantClear(&rv);
2797 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2798 /* Non NULL nor empty BSTR */
2799 /* If the BSTR is not a number the BSTR is greater */
2800 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2801 if (FAILED(rc))
2802 rc = VARCMP_GT;
2803 else if (breserv && nreserv)
2804 /* FIXME: This is strange: with both VT_RESERVED set it
2805 looks like the result depends only on the sign of
2806 the BSTR number */
2807 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2808 else
2809 /* Numeric comparison, will be handled below.
2810 VARCMP_NULL used only to break out. */
2811 rc = VARCMP_NULL;
2812 VariantClear(&lv);
2813 VariantClear(&rv);
2814 } else
2815 /* Empty or NULL BSTR */
2816 rc = VARCMP_GT;
2818 /* Fixup the return code if we swapped left and right */
2819 if (swap) {
2820 if (rc == VARCMP_GT)
2821 rc = VARCMP_LT;
2822 else if (rc == VARCMP_LT)
2823 rc = VARCMP_GT;
2825 if (rc != VARCMP_NULL)
2826 return rc;
2829 if (xmask & VTBIT_DECIMAL)
2830 vt = VT_DECIMAL;
2831 else if (xmask & VTBIT_BSTR)
2832 vt = VT_R8;
2833 else if (xmask & VTBIT_R4)
2834 vt = VT_R4;
2835 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2836 vt = VT_R8;
2837 else if (xmask & VTBIT_CY)
2838 vt = VT_CY;
2839 else
2840 /* default to I8 */
2841 vt = VT_I8;
2843 /* Coerce the variants */
2844 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2845 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2846 /* Overflow, change to R8 */
2847 vt = VT_R8;
2848 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2850 if (FAILED(rc))
2851 return rc;
2852 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2853 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2854 /* Overflow, change to R8 */
2855 vt = VT_R8;
2856 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2857 if (FAILED(rc))
2858 return rc;
2859 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2861 if (FAILED(rc))
2862 return rc;
2864 #define _VARCMP(a,b) \
2865 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2867 switch (vt) {
2868 case VT_CY:
2869 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2870 case VT_DECIMAL:
2871 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2872 case VT_I8:
2873 return _VARCMP(V_I8(&lv), V_I8(&rv));
2874 case VT_R4:
2875 return _VARCMP(V_R4(&lv), V_R4(&rv));
2876 case VT_R8:
2877 return _VARCMP(V_R8(&lv), V_R8(&rv));
2878 default:
2879 /* We should never get here */
2880 return E_FAIL;
2882 #undef _VARCMP
2885 /**********************************************************************
2886 * VarAnd [OLEAUT32.142]
2888 * Computes the logical AND of two variants.
2890 * PARAMS
2891 * left [I] First variant
2892 * right [I] Second variant
2893 * result [O] Result variant
2895 * RETURNS
2896 * Success: S_OK.
2897 * Failure: An HRESULT error code indicating the error.
2899 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2901 HRESULT hres = S_OK;
2902 VARTYPE resvt = VT_EMPTY;
2903 VARTYPE leftvt,rightvt;
2904 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2905 VARIANT varLeft, varRight;
2906 VARIANT tempLeft, tempRight;
2908 VariantInit(&varLeft);
2909 VariantInit(&varRight);
2910 VariantInit(&tempLeft);
2911 VariantInit(&tempRight);
2913 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
2915 /* Handle VT_DISPATCH by storing and taking address of returned value */
2916 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2918 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2919 if (FAILED(hres)) goto VarAnd_Exit;
2920 left = &tempLeft;
2922 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2924 hres = VARIANT_FetchDispatchValue(right, &tempRight);
2925 if (FAILED(hres)) goto VarAnd_Exit;
2926 right = &tempRight;
2929 leftvt = V_VT(left)&VT_TYPEMASK;
2930 rightvt = V_VT(right)&VT_TYPEMASK;
2931 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
2932 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2934 if (leftExtraFlags != rightExtraFlags)
2936 hres = DISP_E_BADVARTYPE;
2937 goto VarAnd_Exit;
2939 ExtraFlags = leftExtraFlags;
2941 /* Native VarAnd always returns an error when using extra
2942 * flags or if the variant combination is I8 and INT.
2944 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
2945 (leftvt == VT_INT && rightvt == VT_I8) ||
2946 ExtraFlags != 0)
2948 hres = DISP_E_BADVARTYPE;
2949 goto VarAnd_Exit;
2952 /* Determine return type */
2953 else if (leftvt == VT_I8 || rightvt == VT_I8)
2954 resvt = VT_I8;
2955 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
2956 leftvt == VT_UINT || rightvt == VT_UINT ||
2957 leftvt == VT_INT || rightvt == VT_INT ||
2958 leftvt == VT_R4 || rightvt == VT_R4 ||
2959 leftvt == VT_R8 || rightvt == VT_R8 ||
2960 leftvt == VT_CY || rightvt == VT_CY ||
2961 leftvt == VT_DATE || rightvt == VT_DATE ||
2962 leftvt == VT_I1 || rightvt == VT_I1 ||
2963 leftvt == VT_UI2 || rightvt == VT_UI2 ||
2964 leftvt == VT_UI4 || rightvt == VT_UI4 ||
2965 leftvt == VT_UI8 || rightvt == VT_UI8 ||
2966 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
2967 resvt = VT_I4;
2968 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
2969 leftvt == VT_I2 || rightvt == VT_I2 ||
2970 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
2971 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
2972 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
2973 (leftvt == VT_UI1 && rightvt == VT_UI1))
2974 resvt = VT_UI1;
2975 else
2976 resvt = VT_I2;
2977 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
2978 (leftvt == VT_BSTR && rightvt == VT_BSTR))
2979 resvt = VT_BOOL;
2980 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
2981 leftvt == VT_BSTR || rightvt == VT_BSTR)
2982 resvt = VT_NULL;
2983 else
2985 hres = DISP_E_BADVARTYPE;
2986 goto VarAnd_Exit;
2989 if (leftvt == VT_NULL || rightvt == VT_NULL)
2992 * Special cases for when left variant is VT_NULL
2993 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
2995 if (leftvt == VT_NULL)
2997 VARIANT_BOOL b;
2998 switch(rightvt)
3000 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3001 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3002 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3003 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3004 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3005 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3006 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3007 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3008 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3009 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3010 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3011 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3012 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3013 case VT_CY:
3014 if(V_CY(right).int64)
3015 resvt = VT_NULL;
3016 break;
3017 case VT_DECIMAL:
3018 if (DEC_HI32(&V_DECIMAL(right)) ||
3019 DEC_LO64(&V_DECIMAL(right)))
3020 resvt = VT_NULL;
3021 break;
3022 case VT_BSTR:
3023 hres = VarBoolFromStr(V_BSTR(right),
3024 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3025 if (FAILED(hres))
3026 return hres;
3027 else if (b)
3028 V_VT(result) = VT_NULL;
3029 else
3031 V_VT(result) = VT_BOOL;
3032 V_BOOL(result) = b;
3034 goto VarAnd_Exit;
3037 V_VT(result) = resvt;
3038 goto VarAnd_Exit;
3041 hres = VariantCopy(&varLeft, left);
3042 if (FAILED(hres)) goto VarAnd_Exit;
3044 hres = VariantCopy(&varRight, right);
3045 if (FAILED(hres)) goto VarAnd_Exit;
3047 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3048 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3049 else
3051 double d;
3053 if (V_VT(&varLeft) == VT_BSTR &&
3054 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3055 LOCALE_USER_DEFAULT, 0, &d)))
3056 hres = VariantChangeType(&varLeft,&varLeft,
3057 VARIANT_LOCALBOOL, VT_BOOL);
3058 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3059 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3060 if (FAILED(hres)) goto VarAnd_Exit;
3063 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3064 V_VT(&varRight) = VT_I4; /* Don't overflow */
3065 else
3067 double d;
3069 if (V_VT(&varRight) == VT_BSTR &&
3070 FAILED(VarR8FromStr(V_BSTR(&varRight),
3071 LOCALE_USER_DEFAULT, 0, &d)))
3072 hres = VariantChangeType(&varRight, &varRight,
3073 VARIANT_LOCALBOOL, VT_BOOL);
3074 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3075 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3076 if (FAILED(hres)) goto VarAnd_Exit;
3079 V_VT(result) = resvt;
3080 switch(resvt)
3082 case VT_I8:
3083 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3084 break;
3085 case VT_I4:
3086 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3087 break;
3088 case VT_I2:
3089 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3090 break;
3091 case VT_UI1:
3092 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3093 break;
3094 case VT_BOOL:
3095 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3096 break;
3097 default:
3098 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3099 leftvt,rightvt);
3102 VarAnd_Exit:
3103 VariantClear(&varLeft);
3104 VariantClear(&varRight);
3105 VariantClear(&tempLeft);
3106 VariantClear(&tempRight);
3108 return hres;
3111 /**********************************************************************
3112 * VarAdd [OLEAUT32.141]
3114 * Add two variants.
3116 * PARAMS
3117 * left [I] First variant
3118 * right [I] Second variant
3119 * result [O] Result variant
3121 * RETURNS
3122 * Success: S_OK.
3123 * Failure: An HRESULT error code indicating the error.
3125 * NOTES
3126 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3127 * UI8, INT and UINT as input variants.
3129 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3130 * same here.
3132 * FIXME
3133 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3134 * case.
3136 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3138 HRESULT hres;
3139 VARTYPE lvt, rvt, resvt, tvt;
3140 VARIANT lv, rv, tv;
3141 VARIANT tempLeft, tempRight;
3142 double r8res;
3144 /* Variant priority for coercion. Sorted from lowest to highest.
3145 VT_ERROR shows an invalid input variant type. */
3146 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3147 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3148 vt_ERROR };
3149 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3150 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3151 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3152 VT_NULL, VT_ERROR };
3154 /* Mapping for coercion from input variant to priority of result variant. */
3155 static const VARTYPE coerce[] = {
3156 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3157 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3158 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3159 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3160 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3161 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3162 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3163 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3166 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3168 VariantInit(&lv);
3169 VariantInit(&rv);
3170 VariantInit(&tv);
3171 VariantInit(&tempLeft);
3172 VariantInit(&tempRight);
3174 /* Handle VT_DISPATCH by storing and taking address of returned value */
3175 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3177 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3179 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3180 if (FAILED(hres)) goto end;
3181 left = &tempLeft;
3183 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3185 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3186 if (FAILED(hres)) goto end;
3187 right = &tempRight;
3191 lvt = V_VT(left)&VT_TYPEMASK;
3192 rvt = V_VT(right)&VT_TYPEMASK;
3194 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3195 Same for any input variant type > VT_I8 */
3196 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3197 lvt > VT_I8 || rvt > VT_I8) {
3198 hres = DISP_E_BADVARTYPE;
3199 goto end;
3202 /* Determine the variant type to coerce to. */
3203 if (coerce[lvt] > coerce[rvt]) {
3204 resvt = prio2vt[coerce[lvt]];
3205 tvt = prio2vt[coerce[rvt]];
3206 } else {
3207 resvt = prio2vt[coerce[rvt]];
3208 tvt = prio2vt[coerce[lvt]];
3211 /* Special cases where the result variant type is defined by both
3212 input variants and not only that with the highest priority */
3213 if (resvt == VT_BSTR) {
3214 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3215 resvt = VT_BSTR;
3216 else
3217 resvt = VT_R8;
3219 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3220 resvt = VT_R8;
3222 /* For overflow detection use the biggest compatible type for the
3223 addition */
3224 switch (resvt) {
3225 case VT_ERROR:
3226 hres = DISP_E_BADVARTYPE;
3227 goto end;
3228 case VT_NULL:
3229 hres = S_OK;
3230 V_VT(result) = VT_NULL;
3231 goto end;
3232 case VT_DISPATCH:
3233 FIXME("cannot handle variant type VT_DISPATCH\n");
3234 hres = DISP_E_TYPEMISMATCH;
3235 goto end;
3236 case VT_EMPTY:
3237 resvt = VT_I2;
3238 /* Fall through */
3239 case VT_UI1:
3240 case VT_I2:
3241 case VT_I4:
3242 case VT_I8:
3243 tvt = VT_I8;
3244 break;
3245 case VT_DATE:
3246 case VT_R4:
3247 tvt = VT_R8;
3248 break;
3249 default:
3250 tvt = resvt;
3253 /* Now coerce the variants */
3254 hres = VariantChangeType(&lv, left, 0, tvt);
3255 if (FAILED(hres))
3256 goto end;
3257 hres = VariantChangeType(&rv, right, 0, tvt);
3258 if (FAILED(hres))
3259 goto end;
3261 /* Do the math */
3262 hres = S_OK;
3263 V_VT(result) = resvt;
3264 switch (tvt) {
3265 case VT_DECIMAL:
3266 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3267 &V_DECIMAL(result));
3268 goto end;
3269 case VT_CY:
3270 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3271 goto end;
3272 case VT_BSTR:
3273 /* We do not add those, we concatenate them. */
3274 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3275 goto end;
3276 case VT_I8:
3277 /* Overflow detection */
3278 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3279 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3280 V_VT(result) = VT_R8;
3281 V_R8(result) = r8res;
3282 goto end;
3283 } else {
3284 V_VT(&tv) = tvt;
3285 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3287 break;
3288 case VT_R8:
3289 V_VT(&tv) = tvt;
3290 /* FIXME: overflow detection */
3291 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3292 break;
3293 default:
3294 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3295 break;
3297 if (resvt != tvt) {
3298 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3299 /* Overflow! Change to the vartype with the next higher priority.
3300 With one exception: I4 ==> R8 even if it would fit in I8 */
3301 if (resvt == VT_I4)
3302 resvt = VT_R8;
3303 else
3304 resvt = prio2vt[coerce[resvt] + 1];
3305 hres = VariantChangeType(result, &tv, 0, resvt);
3307 } else
3308 hres = VariantCopy(result, &tv);
3310 end:
3311 if (hres != S_OK) {
3312 V_VT(result) = VT_EMPTY;
3313 V_I4(result) = 0; /* No V_EMPTY */
3315 VariantClear(&lv);
3316 VariantClear(&rv);
3317 VariantClear(&tv);
3318 VariantClear(&tempLeft);
3319 VariantClear(&tempRight);
3320 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3321 return hres;
3324 /**********************************************************************
3325 * VarMul [OLEAUT32.156]
3327 * Multiply two variants.
3329 * PARAMS
3330 * left [I] First variant
3331 * right [I] Second variant
3332 * result [O] Result variant
3334 * RETURNS
3335 * Success: S_OK.
3336 * Failure: An HRESULT error code indicating the error.
3338 * NOTES
3339 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3340 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3342 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3343 * same here.
3345 * FIXME
3346 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3347 * case.
3349 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3351 HRESULT hres;
3352 VARTYPE lvt, rvt, resvt, tvt;
3353 VARIANT lv, rv, tv;
3354 VARIANT tempLeft, tempRight;
3355 double r8res;
3357 /* Variant priority for coercion. Sorted from lowest to highest.
3358 VT_ERROR shows an invalid input variant type. */
3359 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3360 vt_DECIMAL, vt_NULL, vt_ERROR };
3361 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3362 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3363 VT_DECIMAL, VT_NULL, VT_ERROR };
3365 /* Mapping for coercion from input variant to priority of result variant. */
3366 static const VARTYPE coerce[] = {
3367 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3368 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3369 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3370 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3371 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3372 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3373 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3374 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3377 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3379 VariantInit(&lv);
3380 VariantInit(&rv);
3381 VariantInit(&tv);
3382 VariantInit(&tempLeft);
3383 VariantInit(&tempRight);
3385 /* Handle VT_DISPATCH by storing and taking address of returned value */
3386 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3388 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3389 if (FAILED(hres)) goto end;
3390 left = &tempLeft;
3392 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3394 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3395 if (FAILED(hres)) goto end;
3396 right = &tempRight;
3399 lvt = V_VT(left)&VT_TYPEMASK;
3400 rvt = V_VT(right)&VT_TYPEMASK;
3402 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3403 Same for any input variant type > VT_I8 */
3404 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3405 lvt > VT_I8 || rvt > VT_I8) {
3406 hres = DISP_E_BADVARTYPE;
3407 goto end;
3410 /* Determine the variant type to coerce to. */
3411 if (coerce[lvt] > coerce[rvt]) {
3412 resvt = prio2vt[coerce[lvt]];
3413 tvt = prio2vt[coerce[rvt]];
3414 } else {
3415 resvt = prio2vt[coerce[rvt]];
3416 tvt = prio2vt[coerce[lvt]];
3419 /* Special cases where the result variant type is defined by both
3420 input variants and not only that with the highest priority */
3421 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3422 resvt = VT_R8;
3423 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3424 resvt = VT_I2;
3426 /* For overflow detection use the biggest compatible type for the
3427 multiplication */
3428 switch (resvt) {
3429 case VT_ERROR:
3430 hres = DISP_E_BADVARTYPE;
3431 goto end;
3432 case VT_NULL:
3433 hres = S_OK;
3434 V_VT(result) = VT_NULL;
3435 goto end;
3436 case VT_UI1:
3437 case VT_I2:
3438 case VT_I4:
3439 case VT_I8:
3440 tvt = VT_I8;
3441 break;
3442 case VT_R4:
3443 tvt = VT_R8;
3444 break;
3445 default:
3446 tvt = resvt;
3449 /* Now coerce the variants */
3450 hres = VariantChangeType(&lv, left, 0, tvt);
3451 if (FAILED(hres))
3452 goto end;
3453 hres = VariantChangeType(&rv, right, 0, tvt);
3454 if (FAILED(hres))
3455 goto end;
3457 /* Do the math */
3458 hres = S_OK;
3459 V_VT(&tv) = tvt;
3460 V_VT(result) = resvt;
3461 switch (tvt) {
3462 case VT_DECIMAL:
3463 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3464 &V_DECIMAL(result));
3465 goto end;
3466 case VT_CY:
3467 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3468 goto end;
3469 case VT_I8:
3470 /* Overflow detection */
3471 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3472 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3473 V_VT(result) = VT_R8;
3474 V_R8(result) = r8res;
3475 goto end;
3476 } else
3477 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3478 break;
3479 case VT_R8:
3480 /* FIXME: overflow detection */
3481 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3482 break;
3483 default:
3484 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3485 break;
3487 if (resvt != tvt) {
3488 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3489 /* Overflow! Change to the vartype with the next higher priority.
3490 With one exception: I4 ==> R8 even if it would fit in I8 */
3491 if (resvt == VT_I4)
3492 resvt = VT_R8;
3493 else
3494 resvt = prio2vt[coerce[resvt] + 1];
3496 } else
3497 hres = VariantCopy(result, &tv);
3499 end:
3500 if (hres != S_OK) {
3501 V_VT(result) = VT_EMPTY;
3502 V_I4(result) = 0; /* No V_EMPTY */
3504 VariantClear(&lv);
3505 VariantClear(&rv);
3506 VariantClear(&tv);
3507 VariantClear(&tempLeft);
3508 VariantClear(&tempRight);
3509 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3510 return hres;
3513 /**********************************************************************
3514 * VarDiv [OLEAUT32.143]
3516 * Divides one variant with another.
3518 * PARAMS
3519 * left [I] First variant
3520 * right [I] Second variant
3521 * result [O] Result variant
3523 * RETURNS
3524 * Success: S_OK.
3525 * Failure: An HRESULT error code indicating the error.
3527 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3529 HRESULT hres = S_OK;
3530 VARTYPE resvt = VT_EMPTY;
3531 VARTYPE leftvt,rightvt;
3532 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3533 VARIANT lv,rv;
3534 VARIANT tempLeft, tempRight;
3536 VariantInit(&tempLeft);
3537 VariantInit(&tempRight);
3538 VariantInit(&lv);
3539 VariantInit(&rv);
3541 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3543 /* Handle VT_DISPATCH by storing and taking address of returned value */
3544 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3546 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3547 if (FAILED(hres)) goto end;
3548 left = &tempLeft;
3550 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3552 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3553 if (FAILED(hres)) goto end;
3554 right = &tempRight;
3557 leftvt = V_VT(left)&VT_TYPEMASK;
3558 rightvt = V_VT(right)&VT_TYPEMASK;
3559 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3560 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3562 if (leftExtraFlags != rightExtraFlags)
3564 hres = DISP_E_BADVARTYPE;
3565 goto end;
3567 ExtraFlags = leftExtraFlags;
3569 /* Native VarDiv always returns an error when using extra flags */
3570 if (ExtraFlags != 0)
3572 hres = DISP_E_BADVARTYPE;
3573 goto end;
3576 /* Determine return type */
3577 if (rightvt != VT_EMPTY)
3579 if (leftvt == VT_NULL || rightvt == VT_NULL)
3581 V_VT(result) = VT_NULL;
3582 hres = S_OK;
3583 goto end;
3585 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3586 resvt = VT_DECIMAL;
3587 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3588 leftvt == VT_CY || rightvt == VT_CY ||
3589 leftvt == VT_DATE || rightvt == VT_DATE ||
3590 leftvt == VT_I4 || rightvt == VT_I4 ||
3591 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3592 leftvt == VT_I2 || rightvt == VT_I2 ||
3593 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3594 leftvt == VT_R8 || rightvt == VT_R8 ||
3595 leftvt == VT_UI1 || rightvt == VT_UI1)
3597 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3598 (leftvt == VT_R4 && rightvt == VT_UI1))
3599 resvt = VT_R4;
3600 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3601 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3602 (leftvt == VT_BOOL || leftvt == VT_I2)))
3603 resvt = VT_R4;
3604 else
3605 resvt = VT_R8;
3607 else if (leftvt == VT_R4 || rightvt == VT_R4)
3608 resvt = VT_R4;
3610 else if (leftvt == VT_NULL)
3612 V_VT(result) = VT_NULL;
3613 hres = S_OK;
3614 goto end;
3616 else
3618 hres = DISP_E_BADVARTYPE;
3619 goto end;
3622 /* coerce to the result type */
3623 hres = VariantChangeType(&lv, left, 0, resvt);
3624 if (hres != S_OK) goto end;
3626 hres = VariantChangeType(&rv, right, 0, resvt);
3627 if (hres != S_OK) goto end;
3629 /* do the math */
3630 V_VT(result) = resvt;
3631 switch (resvt)
3633 case VT_R4:
3634 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3636 hres = DISP_E_OVERFLOW;
3637 V_VT(result) = VT_EMPTY;
3639 else if (V_R4(&rv) == 0.0)
3641 hres = DISP_E_DIVBYZERO;
3642 V_VT(result) = VT_EMPTY;
3644 else
3645 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3646 break;
3647 case VT_R8:
3648 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3650 hres = DISP_E_OVERFLOW;
3651 V_VT(result) = VT_EMPTY;
3653 else if (V_R8(&rv) == 0.0)
3655 hres = DISP_E_DIVBYZERO;
3656 V_VT(result) = VT_EMPTY;
3658 else
3659 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3660 break;
3661 case VT_DECIMAL:
3662 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3663 break;
3666 end:
3667 VariantClear(&lv);
3668 VariantClear(&rv);
3669 VariantClear(&tempLeft);
3670 VariantClear(&tempRight);
3671 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3672 return hres;
3675 /**********************************************************************
3676 * VarSub [OLEAUT32.159]
3678 * Subtract two variants.
3680 * PARAMS
3681 * left [I] First variant
3682 * right [I] Second variant
3683 * result [O] Result variant
3685 * RETURNS
3686 * Success: S_OK.
3687 * Failure: An HRESULT error code indicating the error.
3689 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3691 HRESULT hres = S_OK;
3692 VARTYPE resvt = VT_EMPTY;
3693 VARTYPE leftvt,rightvt;
3694 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3695 VARIANT lv,rv;
3696 VARIANT tempLeft, tempRight;
3698 VariantInit(&lv);
3699 VariantInit(&rv);
3700 VariantInit(&tempLeft);
3701 VariantInit(&tempRight);
3703 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3705 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3706 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3707 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3709 if (NULL == V_DISPATCH(left)) {
3710 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3711 hres = DISP_E_BADVARTYPE;
3712 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3713 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3714 hres = DISP_E_BADVARTYPE;
3715 else switch (V_VT(right) & VT_TYPEMASK)
3717 case VT_VARIANT:
3718 case VT_UNKNOWN:
3719 case 15:
3720 case VT_I1:
3721 case VT_UI2:
3722 case VT_UI4:
3723 hres = DISP_E_BADVARTYPE;
3725 if (FAILED(hres)) goto end;
3727 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3728 if (FAILED(hres)) goto end;
3729 left = &tempLeft;
3731 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3732 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3733 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3735 if (NULL == V_DISPATCH(right))
3737 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3738 hres = DISP_E_BADVARTYPE;
3739 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3740 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3741 hres = DISP_E_BADVARTYPE;
3742 else switch (V_VT(left) & VT_TYPEMASK)
3744 case VT_VARIANT:
3745 case VT_UNKNOWN:
3746 case 15:
3747 case VT_I1:
3748 case VT_UI2:
3749 case VT_UI4:
3750 hres = DISP_E_BADVARTYPE;
3752 if (FAILED(hres)) goto end;
3754 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3755 if (FAILED(hres)) goto end;
3756 right = &tempRight;
3759 leftvt = V_VT(left)&VT_TYPEMASK;
3760 rightvt = V_VT(right)&VT_TYPEMASK;
3761 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3762 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3764 if (leftExtraFlags != rightExtraFlags)
3766 hres = DISP_E_BADVARTYPE;
3767 goto end;
3769 ExtraFlags = leftExtraFlags;
3771 /* determine return type and return code */
3772 /* All extra flags produce errors */
3773 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3774 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3775 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3776 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3777 ExtraFlags == VT_VECTOR ||
3778 ExtraFlags == VT_BYREF ||
3779 ExtraFlags == VT_RESERVED)
3781 hres = DISP_E_BADVARTYPE;
3782 goto end;
3784 else if (ExtraFlags >= VT_ARRAY)
3786 hres = DISP_E_TYPEMISMATCH;
3787 goto end;
3789 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3790 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3791 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3792 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3793 leftvt == VT_I1 || rightvt == VT_I1 ||
3794 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3795 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3796 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3797 leftvt == VT_INT || rightvt == VT_INT ||
3798 leftvt == VT_UINT || rightvt == VT_UINT ||
3799 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3800 leftvt == VT_RECORD || rightvt == VT_RECORD)
3802 if (leftvt == VT_RECORD && rightvt == VT_I8)
3803 hres = DISP_E_TYPEMISMATCH;
3804 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3805 hres = DISP_E_TYPEMISMATCH;
3806 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3807 hres = DISP_E_TYPEMISMATCH;
3808 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3809 hres = DISP_E_TYPEMISMATCH;
3810 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3811 hres = DISP_E_BADVARTYPE;
3812 else
3813 hres = DISP_E_BADVARTYPE;
3814 goto end;
3816 /* The following flags/types are invalid for left variant */
3817 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3818 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3819 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3821 hres = DISP_E_BADVARTYPE;
3822 goto end;
3824 /* The following flags/types are invalid for right variant */
3825 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3826 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3827 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3829 hres = DISP_E_BADVARTYPE;
3830 goto end;
3832 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3833 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3834 resvt = VT_NULL;
3835 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3836 leftvt == VT_ERROR || rightvt == VT_ERROR)
3838 hres = DISP_E_TYPEMISMATCH;
3839 goto end;
3841 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3842 resvt = VT_NULL;
3843 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3844 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3845 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3846 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3847 resvt = VT_R8;
3848 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3849 resvt = VT_DECIMAL;
3850 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3851 resvt = VT_DATE;
3852 else if (leftvt == VT_CY || rightvt == VT_CY)
3853 resvt = VT_CY;
3854 else if (leftvt == VT_R8 || rightvt == VT_R8)
3855 resvt = VT_R8;
3856 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3857 resvt = VT_R8;
3858 else if (leftvt == VT_R4 || rightvt == VT_R4)
3860 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3861 leftvt == VT_I8 || rightvt == VT_I8)
3862 resvt = VT_R8;
3863 else
3864 resvt = VT_R4;
3866 else if (leftvt == VT_I8 || rightvt == VT_I8)
3867 resvt = VT_I8;
3868 else if (leftvt == VT_I4 || rightvt == VT_I4)
3869 resvt = VT_I4;
3870 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3871 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3872 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3873 resvt = VT_I2;
3874 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3875 resvt = VT_UI1;
3876 else
3878 hres = DISP_E_TYPEMISMATCH;
3879 goto end;
3882 /* coerce to the result type */
3883 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3884 hres = VariantChangeType(&lv, left, 0, VT_R8);
3885 else
3886 hres = VariantChangeType(&lv, left, 0, resvt);
3887 if (hres != S_OK) goto end;
3888 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3889 hres = VariantChangeType(&rv, right, 0, VT_R8);
3890 else
3891 hres = VariantChangeType(&rv, right, 0, resvt);
3892 if (hres != S_OK) goto end;
3894 /* do the math */
3895 V_VT(result) = resvt;
3896 switch (resvt)
3898 case VT_NULL:
3899 break;
3900 case VT_DATE:
3901 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3902 break;
3903 case VT_CY:
3904 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3905 break;
3906 case VT_R4:
3907 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3908 break;
3909 case VT_I8:
3910 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3911 break;
3912 case VT_I4:
3913 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3914 break;
3915 case VT_I2:
3916 V_I2(result) = V_I2(&lv) - V_I2(&rv);
3917 break;
3918 case VT_UI1:
3919 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
3920 break;
3921 case VT_R8:
3922 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3923 break;
3924 case VT_DECIMAL:
3925 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3926 break;
3929 end:
3930 VariantClear(&lv);
3931 VariantClear(&rv);
3932 VariantClear(&tempLeft);
3933 VariantClear(&tempRight);
3934 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3935 return hres;
3939 /**********************************************************************
3940 * VarOr [OLEAUT32.157]
3942 * Perform a logical or (OR) operation on two variants.
3944 * PARAMS
3945 * pVarLeft [I] First variant
3946 * pVarRight [I] Variant to OR with pVarLeft
3947 * pVarOut [O] Destination for OR result
3949 * RETURNS
3950 * Success: S_OK. pVarOut contains the result of the operation with its type
3951 * taken from the table listed under VarXor().
3952 * Failure: An HRESULT error code indicating the error.
3954 * NOTES
3955 * See the Notes section of VarXor() for further information.
3957 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3959 VARTYPE vt = VT_I4;
3960 VARIANT varLeft, varRight, varStr;
3961 HRESULT hRet;
3962 VARIANT tempLeft, tempRight;
3964 VariantInit(&tempLeft);
3965 VariantInit(&tempRight);
3966 VariantInit(&varLeft);
3967 VariantInit(&varRight);
3968 VariantInit(&varStr);
3970 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
3972 /* Handle VT_DISPATCH by storing and taking address of returned value */
3973 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
3975 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
3976 if (FAILED(hRet)) goto VarOr_Exit;
3977 pVarLeft = &tempLeft;
3979 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
3981 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
3982 if (FAILED(hRet)) goto VarOr_Exit;
3983 pVarRight = &tempRight;
3986 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3987 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3988 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3989 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3991 hRet = DISP_E_BADVARTYPE;
3992 goto VarOr_Exit;
3995 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
3997 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3999 /* NULL OR Zero is NULL, NULL OR value is value */
4000 if (V_VT(pVarLeft) == VT_NULL)
4001 pVarLeft = pVarRight; /* point to the non-NULL var */
4003 V_VT(pVarOut) = VT_NULL;
4004 V_I4(pVarOut) = 0;
4006 switch (V_VT(pVarLeft))
4008 case VT_DATE: case VT_R8:
4009 if (V_R8(pVarLeft))
4010 goto VarOr_AsEmpty;
4011 hRet = S_OK;
4012 goto VarOr_Exit;
4013 case VT_BOOL:
4014 if (V_BOOL(pVarLeft))
4015 *pVarOut = *pVarLeft;
4016 hRet = S_OK;
4017 goto VarOr_Exit;
4018 case VT_I2: case VT_UI2:
4019 if (V_I2(pVarLeft))
4020 goto VarOr_AsEmpty;
4021 hRet = S_OK;
4022 goto VarOr_Exit;
4023 case VT_I1:
4024 if (V_I1(pVarLeft))
4025 goto VarOr_AsEmpty;
4026 hRet = S_OK;
4027 goto VarOr_Exit;
4028 case VT_UI1:
4029 if (V_UI1(pVarLeft))
4030 *pVarOut = *pVarLeft;
4031 hRet = S_OK;
4032 goto VarOr_Exit;
4033 case VT_R4:
4034 if (V_R4(pVarLeft))
4035 goto VarOr_AsEmpty;
4036 hRet = S_OK;
4037 goto VarOr_Exit;
4038 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4039 if (V_I4(pVarLeft))
4040 goto VarOr_AsEmpty;
4041 hRet = S_OK;
4042 goto VarOr_Exit;
4043 case VT_CY:
4044 if (V_CY(pVarLeft).int64)
4045 goto VarOr_AsEmpty;
4046 hRet = S_OK;
4047 goto VarOr_Exit;
4048 case VT_I8: case VT_UI8:
4049 if (V_I8(pVarLeft))
4050 goto VarOr_AsEmpty;
4051 hRet = S_OK;
4052 goto VarOr_Exit;
4053 case VT_DECIMAL:
4054 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4055 goto VarOr_AsEmpty;
4056 hRet = S_OK;
4057 goto VarOr_Exit;
4058 case VT_BSTR:
4060 VARIANT_BOOL b;
4062 if (!V_BSTR(pVarLeft))
4064 hRet = DISP_E_BADVARTYPE;
4065 goto VarOr_Exit;
4068 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4069 if (SUCCEEDED(hRet) && b)
4071 V_VT(pVarOut) = VT_BOOL;
4072 V_BOOL(pVarOut) = b;
4074 goto VarOr_Exit;
4076 case VT_NULL: case VT_EMPTY:
4077 V_VT(pVarOut) = VT_NULL;
4078 hRet = S_OK;
4079 goto VarOr_Exit;
4080 default:
4081 hRet = DISP_E_BADVARTYPE;
4082 goto VarOr_Exit;
4086 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4088 if (V_VT(pVarLeft) == VT_EMPTY)
4089 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4091 VarOr_AsEmpty:
4092 /* Since one argument is empty (0), OR'ing it with the other simply
4093 * gives the others value (as 0|x => x). So just convert the other
4094 * argument to the required result type.
4096 switch (V_VT(pVarLeft))
4098 case VT_BSTR:
4099 if (!V_BSTR(pVarLeft))
4101 hRet = DISP_E_BADVARTYPE;
4102 goto VarOr_Exit;
4105 hRet = VariantCopy(&varStr, pVarLeft);
4106 if (FAILED(hRet))
4107 goto VarOr_Exit;
4108 pVarLeft = &varStr;
4109 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4110 if (FAILED(hRet))
4111 goto VarOr_Exit;
4112 /* Fall Through ... */
4113 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4114 V_VT(pVarOut) = VT_I2;
4115 break;
4116 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4117 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4118 case VT_INT: case VT_UINT: case VT_UI8:
4119 V_VT(pVarOut) = VT_I4;
4120 break;
4121 case VT_I8:
4122 V_VT(pVarOut) = VT_I8;
4123 break;
4124 default:
4125 hRet = DISP_E_BADVARTYPE;
4126 goto VarOr_Exit;
4128 hRet = VariantCopy(&varLeft, pVarLeft);
4129 if (FAILED(hRet))
4130 goto VarOr_Exit;
4131 pVarLeft = &varLeft;
4132 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4133 goto VarOr_Exit;
4136 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4138 V_VT(pVarOut) = VT_BOOL;
4139 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4140 hRet = S_OK;
4141 goto VarOr_Exit;
4144 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4146 V_VT(pVarOut) = VT_UI1;
4147 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4148 hRet = S_OK;
4149 goto VarOr_Exit;
4152 if (V_VT(pVarLeft) == VT_BSTR)
4154 hRet = VariantCopy(&varStr, pVarLeft);
4155 if (FAILED(hRet))
4156 goto VarOr_Exit;
4157 pVarLeft = &varStr;
4158 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4159 if (FAILED(hRet))
4160 goto VarOr_Exit;
4163 if (V_VT(pVarLeft) == VT_BOOL &&
4164 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4166 vt = VT_BOOL;
4168 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4169 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4170 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4171 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4173 vt = VT_I2;
4175 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4177 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4179 hRet = DISP_E_TYPEMISMATCH;
4180 goto VarOr_Exit;
4182 vt = VT_I8;
4185 hRet = VariantCopy(&varLeft, pVarLeft);
4186 if (FAILED(hRet))
4187 goto VarOr_Exit;
4189 hRet = VariantCopy(&varRight, pVarRight);
4190 if (FAILED(hRet))
4191 goto VarOr_Exit;
4193 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4194 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4195 else
4197 double d;
4199 if (V_VT(&varLeft) == VT_BSTR &&
4200 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4201 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4202 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4203 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4204 if (FAILED(hRet))
4205 goto VarOr_Exit;
4208 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4209 V_VT(&varRight) = VT_I4; /* Don't overflow */
4210 else
4212 double d;
4214 if (V_VT(&varRight) == VT_BSTR &&
4215 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4216 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4217 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4218 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4219 if (FAILED(hRet))
4220 goto VarOr_Exit;
4223 V_VT(pVarOut) = vt;
4224 if (vt == VT_I8)
4226 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4228 else if (vt == VT_I4)
4230 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4232 else
4234 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4237 VarOr_Exit:
4238 VariantClear(&varStr);
4239 VariantClear(&varLeft);
4240 VariantClear(&varRight);
4241 VariantClear(&tempLeft);
4242 VariantClear(&tempRight);
4243 return hRet;
4246 /**********************************************************************
4247 * VarAbs [OLEAUT32.168]
4249 * Convert a variant to its absolute value.
4251 * PARAMS
4252 * pVarIn [I] Source variant
4253 * pVarOut [O] Destination for converted value
4255 * RETURNS
4256 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4257 * Failure: An HRESULT error code indicating the error.
4259 * NOTES
4260 * - This function does not process by-reference variants.
4261 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4262 * according to the following table:
4263 *| Input Type Output Type
4264 *| ---------- -----------
4265 *| VT_BOOL VT_I2
4266 *| VT_BSTR VT_R8
4267 *| (All others) Unchanged
4269 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4271 VARIANT varIn;
4272 HRESULT hRet = S_OK;
4273 VARIANT temp;
4275 VariantInit(&temp);
4277 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4279 /* Handle VT_DISPATCH by storing and taking address of returned value */
4280 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4282 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4283 if (FAILED(hRet)) goto VarAbs_Exit;
4284 pVarIn = &temp;
4287 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4288 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4289 V_VT(pVarIn) == VT_ERROR)
4291 hRet = DISP_E_TYPEMISMATCH;
4292 goto VarAbs_Exit;
4294 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4296 #define ABS_CASE(typ,min) \
4297 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4298 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4299 break
4301 switch (V_VT(pVarIn))
4303 ABS_CASE(I1,I1_MIN);
4304 case VT_BOOL:
4305 V_VT(pVarOut) = VT_I2;
4306 /* BOOL->I2, Fall through ... */
4307 ABS_CASE(I2,I2_MIN);
4308 case VT_INT:
4309 ABS_CASE(I4,I4_MIN);
4310 ABS_CASE(I8,I8_MIN);
4311 ABS_CASE(R4,R4_MIN);
4312 case VT_BSTR:
4313 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4314 if (FAILED(hRet))
4315 break;
4316 V_VT(pVarOut) = VT_R8;
4317 pVarIn = &varIn;
4318 /* Fall through ... */
4319 case VT_DATE:
4320 ABS_CASE(R8,R8_MIN);
4321 case VT_CY:
4322 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4323 break;
4324 case VT_DECIMAL:
4325 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4326 break;
4327 case VT_UI1:
4328 case VT_UI2:
4329 case VT_UINT:
4330 case VT_UI4:
4331 case VT_UI8:
4332 /* No-Op */
4333 break;
4334 case VT_EMPTY:
4335 V_VT(pVarOut) = VT_I2;
4336 case VT_NULL:
4337 V_I2(pVarOut) = 0;
4338 break;
4339 default:
4340 hRet = DISP_E_BADVARTYPE;
4343 VarAbs_Exit:
4344 VariantClear(&temp);
4345 return hRet;
4348 /**********************************************************************
4349 * VarFix [OLEAUT32.169]
4351 * Truncate a variants value to a whole number.
4353 * PARAMS
4354 * pVarIn [I] Source variant
4355 * pVarOut [O] Destination for converted value
4357 * RETURNS
4358 * Success: S_OK. pVarOut contains the converted value.
4359 * Failure: An HRESULT error code indicating the error.
4361 * NOTES
4362 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4363 * according to the following table:
4364 *| Input Type Output Type
4365 *| ---------- -----------
4366 *| VT_BOOL VT_I2
4367 *| VT_EMPTY VT_I2
4368 *| VT_BSTR VT_R8
4369 *| All Others Unchanged
4370 * - The difference between this function and VarInt() is that VarInt() rounds
4371 * negative numbers away from 0, while this function rounds them towards zero.
4373 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4375 HRESULT hRet = S_OK;
4376 VARIANT temp;
4378 VariantInit(&temp);
4380 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4382 /* Handle VT_DISPATCH by storing and taking address of returned value */
4383 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4385 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4386 if (FAILED(hRet)) goto VarFix_Exit;
4387 pVarIn = &temp;
4389 V_VT(pVarOut) = V_VT(pVarIn);
4391 switch (V_VT(pVarIn))
4393 case VT_UI1:
4394 V_UI1(pVarOut) = V_UI1(pVarIn);
4395 break;
4396 case VT_BOOL:
4397 V_VT(pVarOut) = VT_I2;
4398 /* Fall through */
4399 case VT_I2:
4400 V_I2(pVarOut) = V_I2(pVarIn);
4401 break;
4402 case VT_I4:
4403 V_I4(pVarOut) = V_I4(pVarIn);
4404 break;
4405 case VT_I8:
4406 V_I8(pVarOut) = V_I8(pVarIn);
4407 break;
4408 case VT_R4:
4409 if (V_R4(pVarIn) < 0.0f)
4410 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4411 else
4412 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4413 break;
4414 case VT_BSTR:
4415 V_VT(pVarOut) = VT_R8;
4416 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4417 pVarIn = pVarOut;
4418 /* Fall through */
4419 case VT_DATE:
4420 case VT_R8:
4421 if (V_R8(pVarIn) < 0.0)
4422 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4423 else
4424 V_R8(pVarOut) = floor(V_R8(pVarIn));
4425 break;
4426 case VT_CY:
4427 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4428 break;
4429 case VT_DECIMAL:
4430 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4431 break;
4432 case VT_EMPTY:
4433 V_VT(pVarOut) = VT_I2;
4434 V_I2(pVarOut) = 0;
4435 break;
4436 case VT_NULL:
4437 /* No-Op */
4438 break;
4439 default:
4440 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4441 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4442 hRet = DISP_E_BADVARTYPE;
4443 else
4444 hRet = DISP_E_TYPEMISMATCH;
4446 VarFix_Exit:
4447 if (FAILED(hRet))
4448 V_VT(pVarOut) = VT_EMPTY;
4449 VariantClear(&temp);
4451 return hRet;
4454 /**********************************************************************
4455 * VarInt [OLEAUT32.172]
4457 * Truncate a variants value to a whole number.
4459 * PARAMS
4460 * pVarIn [I] Source variant
4461 * pVarOut [O] Destination for converted value
4463 * RETURNS
4464 * Success: S_OK. pVarOut contains the converted value.
4465 * Failure: An HRESULT error code indicating the error.
4467 * NOTES
4468 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4469 * according to the following table:
4470 *| Input Type Output Type
4471 *| ---------- -----------
4472 *| VT_BOOL VT_I2
4473 *| VT_EMPTY VT_I2
4474 *| VT_BSTR VT_R8
4475 *| All Others Unchanged
4476 * - The difference between this function and VarFix() is that VarFix() rounds
4477 * negative numbers towards 0, while this function rounds them away from zero.
4479 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4481 HRESULT hRet = S_OK;
4482 VARIANT temp;
4484 VariantInit(&temp);
4486 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4488 /* Handle VT_DISPATCH by storing and taking address of returned value */
4489 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4491 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4492 if (FAILED(hRet)) goto VarInt_Exit;
4493 pVarIn = &temp;
4495 V_VT(pVarOut) = V_VT(pVarIn);
4497 switch (V_VT(pVarIn))
4499 case VT_R4:
4500 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4501 break;
4502 case VT_BSTR:
4503 V_VT(pVarOut) = VT_R8;
4504 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4505 pVarIn = pVarOut;
4506 /* Fall through */
4507 case VT_DATE:
4508 case VT_R8:
4509 V_R8(pVarOut) = floor(V_R8(pVarIn));
4510 break;
4511 case VT_CY:
4512 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4513 break;
4514 case VT_DECIMAL:
4515 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4516 break;
4517 default:
4518 hRet = VarFix(pVarIn, pVarOut);
4520 VarInt_Exit:
4521 VariantClear(&temp);
4523 return hRet;
4526 /**********************************************************************
4527 * VarXor [OLEAUT32.167]
4529 * Perform a logical exclusive-or (XOR) operation on two variants.
4531 * PARAMS
4532 * pVarLeft [I] First variant
4533 * pVarRight [I] Variant to XOR with pVarLeft
4534 * pVarOut [O] Destination for XOR result
4536 * RETURNS
4537 * Success: S_OK. pVarOut contains the result of the operation with its type
4538 * taken from the table below).
4539 * Failure: An HRESULT error code indicating the error.
4541 * NOTES
4542 * - Neither pVarLeft or pVarRight are modified by this function.
4543 * - This function does not process by-reference variants.
4544 * - Input types of VT_BSTR may be numeric strings or boolean text.
4545 * - The type of result stored in pVarOut depends on the types of pVarLeft
4546 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4547 * or VT_NULL if the function succeeds.
4548 * - Type promotion is inconsistent and as a result certain combinations of
4549 * values will return DISP_E_OVERFLOW even when they could be represented.
4550 * This matches the behaviour of native oleaut32.
4552 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4554 VARTYPE vt;
4555 VARIANT varLeft, varRight;
4556 VARIANT tempLeft, tempRight;
4557 double d;
4558 HRESULT hRet;
4560 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4562 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4563 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4564 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4565 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4566 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4567 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4568 return DISP_E_BADVARTYPE;
4570 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4572 /* NULL XOR anything valid is NULL */
4573 V_VT(pVarOut) = VT_NULL;
4574 return S_OK;
4577 VariantInit(&tempLeft);
4578 VariantInit(&tempRight);
4580 /* Handle VT_DISPATCH by storing and taking address of returned value */
4581 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4583 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4584 if (FAILED(hRet)) goto VarXor_Exit;
4585 pVarLeft = &tempLeft;
4587 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4589 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4590 if (FAILED(hRet)) goto VarXor_Exit;
4591 pVarRight = &tempRight;
4594 /* Copy our inputs so we don't disturb anything */
4595 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4597 hRet = VariantCopy(&varLeft, pVarLeft);
4598 if (FAILED(hRet))
4599 goto VarXor_Exit;
4601 hRet = VariantCopy(&varRight, pVarRight);
4602 if (FAILED(hRet))
4603 goto VarXor_Exit;
4605 /* Try any strings first as numbers, then as VT_BOOL */
4606 if (V_VT(&varLeft) == VT_BSTR)
4608 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4609 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4610 FAILED(hRet) ? VT_BOOL : VT_I4);
4611 if (FAILED(hRet))
4612 goto VarXor_Exit;
4615 if (V_VT(&varRight) == VT_BSTR)
4617 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4618 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4619 FAILED(hRet) ? VT_BOOL : VT_I4);
4620 if (FAILED(hRet))
4621 goto VarXor_Exit;
4624 /* Determine the result type */
4625 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4627 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4629 hRet = DISP_E_TYPEMISMATCH;
4630 goto VarXor_Exit;
4632 vt = VT_I8;
4634 else
4636 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4638 case (VT_BOOL << 16) | VT_BOOL:
4639 vt = VT_BOOL;
4640 break;
4641 case (VT_UI1 << 16) | VT_UI1:
4642 vt = VT_UI1;
4643 break;
4644 case (VT_EMPTY << 16) | VT_EMPTY:
4645 case (VT_EMPTY << 16) | VT_UI1:
4646 case (VT_EMPTY << 16) | VT_I2:
4647 case (VT_EMPTY << 16) | VT_BOOL:
4648 case (VT_UI1 << 16) | VT_EMPTY:
4649 case (VT_UI1 << 16) | VT_I2:
4650 case (VT_UI1 << 16) | VT_BOOL:
4651 case (VT_I2 << 16) | VT_EMPTY:
4652 case (VT_I2 << 16) | VT_UI1:
4653 case (VT_I2 << 16) | VT_I2:
4654 case (VT_I2 << 16) | VT_BOOL:
4655 case (VT_BOOL << 16) | VT_EMPTY:
4656 case (VT_BOOL << 16) | VT_UI1:
4657 case (VT_BOOL << 16) | VT_I2:
4658 vt = VT_I2;
4659 break;
4660 default:
4661 vt = VT_I4;
4662 break;
4666 /* VT_UI4 does not overflow */
4667 if (vt != VT_I8)
4669 if (V_VT(&varLeft) == VT_UI4)
4670 V_VT(&varLeft) = VT_I4;
4671 if (V_VT(&varRight) == VT_UI4)
4672 V_VT(&varRight) = VT_I4;
4675 /* Convert our input copies to the result type */
4676 if (V_VT(&varLeft) != vt)
4677 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4678 if (FAILED(hRet))
4679 goto VarXor_Exit;
4681 if (V_VT(&varRight) != vt)
4682 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4683 if (FAILED(hRet))
4684 goto VarXor_Exit;
4686 V_VT(pVarOut) = vt;
4688 /* Calculate the result */
4689 switch (vt)
4691 case VT_I8:
4692 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4693 break;
4694 case VT_I4:
4695 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4696 break;
4697 case VT_BOOL:
4698 case VT_I2:
4699 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4700 break;
4701 case VT_UI1:
4702 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4703 break;
4706 VarXor_Exit:
4707 VariantClear(&varLeft);
4708 VariantClear(&varRight);
4709 VariantClear(&tempLeft);
4710 VariantClear(&tempRight);
4711 return hRet;
4714 /**********************************************************************
4715 * VarEqv [OLEAUT32.172]
4717 * Determine if two variants contain the same value.
4719 * PARAMS
4720 * pVarLeft [I] First variant to compare
4721 * pVarRight [I] Variant to compare to pVarLeft
4722 * pVarOut [O] Destination for comparison result
4724 * RETURNS
4725 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4726 * if equivalent or non-zero otherwise.
4727 * Failure: An HRESULT error code indicating the error.
4729 * NOTES
4730 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4731 * the result.
4733 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4735 HRESULT hRet;
4737 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4739 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4740 if (SUCCEEDED(hRet))
4742 if (V_VT(pVarOut) == VT_I8)
4743 V_I8(pVarOut) = ~V_I8(pVarOut);
4744 else
4745 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4747 return hRet;
4750 /**********************************************************************
4751 * VarNeg [OLEAUT32.173]
4753 * Negate the value of a variant.
4755 * PARAMS
4756 * pVarIn [I] Source variant
4757 * pVarOut [O] Destination for converted value
4759 * RETURNS
4760 * Success: S_OK. pVarOut contains the converted value.
4761 * Failure: An HRESULT error code indicating the error.
4763 * NOTES
4764 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4765 * according to the following table:
4766 *| Input Type Output Type
4767 *| ---------- -----------
4768 *| VT_EMPTY VT_I2
4769 *| VT_UI1 VT_I2
4770 *| VT_BOOL VT_I2
4771 *| VT_BSTR VT_R8
4772 *| All Others Unchanged (unless promoted)
4773 * - Where the negated value of a variant does not fit in its base type, the type
4774 * is promoted according to the following table:
4775 *| Input Type Promoted To
4776 *| ---------- -----------
4777 *| VT_I2 VT_I4
4778 *| VT_I4 VT_R8
4779 *| VT_I8 VT_R8
4780 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4781 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4782 * for types which are not valid. Since this is in contravention of the
4783 * meaning of those error codes and unlikely to be relied on by applications,
4784 * this implementation returns errors consistent with the other high level
4785 * variant math functions.
4787 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4789 HRESULT hRet = S_OK;
4790 VARIANT temp;
4792 VariantInit(&temp);
4794 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4796 /* Handle VT_DISPATCH by storing and taking address of returned value */
4797 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4799 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4800 if (FAILED(hRet)) goto VarNeg_Exit;
4801 pVarIn = &temp;
4803 V_VT(pVarOut) = V_VT(pVarIn);
4805 switch (V_VT(pVarIn))
4807 case VT_UI1:
4808 V_VT(pVarOut) = VT_I2;
4809 V_I2(pVarOut) = -V_UI1(pVarIn);
4810 break;
4811 case VT_BOOL:
4812 V_VT(pVarOut) = VT_I2;
4813 /* Fall through */
4814 case VT_I2:
4815 if (V_I2(pVarIn) == I2_MIN)
4817 V_VT(pVarOut) = VT_I4;
4818 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4820 else
4821 V_I2(pVarOut) = -V_I2(pVarIn);
4822 break;
4823 case VT_I4:
4824 if (V_I4(pVarIn) == I4_MIN)
4826 V_VT(pVarOut) = VT_R8;
4827 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4829 else
4830 V_I4(pVarOut) = -V_I4(pVarIn);
4831 break;
4832 case VT_I8:
4833 if (V_I8(pVarIn) == I8_MIN)
4835 V_VT(pVarOut) = VT_R8;
4836 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4837 V_R8(pVarOut) *= -1.0;
4839 else
4840 V_I8(pVarOut) = -V_I8(pVarIn);
4841 break;
4842 case VT_R4:
4843 V_R4(pVarOut) = -V_R4(pVarIn);
4844 break;
4845 case VT_DATE:
4846 case VT_R8:
4847 V_R8(pVarOut) = -V_R8(pVarIn);
4848 break;
4849 case VT_CY:
4850 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4851 break;
4852 case VT_DECIMAL:
4853 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4854 break;
4855 case VT_BSTR:
4856 V_VT(pVarOut) = VT_R8;
4857 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4858 V_R8(pVarOut) = -V_R8(pVarOut);
4859 break;
4860 case VT_EMPTY:
4861 V_VT(pVarOut) = VT_I2;
4862 V_I2(pVarOut) = 0;
4863 break;
4864 case VT_NULL:
4865 /* No-Op */
4866 break;
4867 default:
4868 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4869 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4870 hRet = DISP_E_BADVARTYPE;
4871 else
4872 hRet = DISP_E_TYPEMISMATCH;
4874 VarNeg_Exit:
4875 if (FAILED(hRet))
4876 V_VT(pVarOut) = VT_EMPTY;
4877 VariantClear(&temp);
4879 return hRet;
4882 /**********************************************************************
4883 * VarNot [OLEAUT32.174]
4885 * Perform a not operation on a variant.
4887 * PARAMS
4888 * pVarIn [I] Source variant
4889 * pVarOut [O] Destination for converted value
4891 * RETURNS
4892 * Success: S_OK. pVarOut contains the converted value.
4893 * Failure: An HRESULT error code indicating the error.
4895 * NOTES
4896 * - Strictly speaking, this function performs a bitwise ones complement
4897 * on the variants value (after possibly converting to VT_I4, see below).
4898 * This only behaves like a boolean not operation if the value in
4899 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4900 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4901 * before calling this function.
4902 * - This function does not process by-reference variants.
4903 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4904 * according to the following table:
4905 *| Input Type Output Type
4906 *| ---------- -----------
4907 *| VT_EMPTY VT_I2
4908 *| VT_R4 VT_I4
4909 *| VT_R8 VT_I4
4910 *| VT_BSTR VT_I4
4911 *| VT_DECIMAL VT_I4
4912 *| VT_CY VT_I4
4913 *| (All others) Unchanged
4915 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4917 VARIANT varIn;
4918 HRESULT hRet = S_OK;
4919 VARIANT temp;
4921 VariantInit(&temp);
4923 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4925 /* Handle VT_DISPATCH by storing and taking address of returned value */
4926 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4928 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4929 if (FAILED(hRet)) goto VarNot_Exit;
4930 pVarIn = &temp;
4933 if (V_VT(pVarIn) == VT_BSTR)
4935 V_VT(&varIn) = VT_R8;
4936 hRet = VarR8FromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn) );
4937 if (FAILED(hRet))
4939 V_VT(&varIn) = VT_BOOL;
4940 hRet = VarBoolFromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &V_BOOL(&varIn) );
4942 if (FAILED(hRet)) goto VarNot_Exit;
4943 pVarIn = &varIn;
4946 V_VT(pVarOut) = V_VT(pVarIn);
4948 switch (V_VT(pVarIn))
4950 case VT_I1:
4951 V_I4(pVarOut) = ~V_I1(pVarIn);
4952 V_VT(pVarOut) = VT_I4;
4953 break;
4954 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4955 case VT_BOOL:
4956 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4957 case VT_UI2:
4958 V_I4(pVarOut) = ~V_UI2(pVarIn);
4959 V_VT(pVarOut) = VT_I4;
4960 break;
4961 case VT_DECIMAL:
4962 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4963 if (FAILED(hRet))
4964 break;
4965 pVarIn = &varIn;
4966 /* Fall through ... */
4967 case VT_INT:
4968 V_VT(pVarOut) = VT_I4;
4969 /* Fall through ... */
4970 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4971 case VT_UINT:
4972 case VT_UI4:
4973 V_I4(pVarOut) = ~V_UI4(pVarIn);
4974 V_VT(pVarOut) = VT_I4;
4975 break;
4976 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
4977 case VT_UI8:
4978 V_I4(pVarOut) = ~V_UI8(pVarIn);
4979 V_VT(pVarOut) = VT_I4;
4980 break;
4981 case VT_R4:
4982 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
4983 V_I4(pVarOut) = ~V_I4(pVarOut);
4984 V_VT(pVarOut) = VT_I4;
4985 break;
4986 case VT_DATE:
4987 case VT_R8:
4988 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4989 V_I4(pVarOut) = ~V_I4(pVarOut);
4990 V_VT(pVarOut) = VT_I4;
4991 break;
4992 case VT_CY:
4993 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4994 V_I4(pVarOut) = ~V_I4(pVarOut);
4995 V_VT(pVarOut) = VT_I4;
4996 break;
4997 case VT_EMPTY:
4998 V_I2(pVarOut) = ~0;
4999 V_VT(pVarOut) = VT_I2;
5000 break;
5001 case VT_NULL:
5002 /* No-Op */
5003 break;
5004 default:
5005 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5006 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5007 hRet = DISP_E_BADVARTYPE;
5008 else
5009 hRet = DISP_E_TYPEMISMATCH;
5011 VarNot_Exit:
5012 if (FAILED(hRet))
5013 V_VT(pVarOut) = VT_EMPTY;
5014 VariantClear(&temp);
5016 return hRet;
5019 /**********************************************************************
5020 * VarRound [OLEAUT32.175]
5022 * Perform a round operation on a variant.
5024 * PARAMS
5025 * pVarIn [I] Source variant
5026 * deci [I] Number of decimals to round to
5027 * pVarOut [O] Destination for converted value
5029 * RETURNS
5030 * Success: S_OK. pVarOut contains the converted value.
5031 * Failure: An HRESULT error code indicating the error.
5033 * NOTES
5034 * - Floating point values are rounded to the desired number of decimals.
5035 * - Some integer types are just copied to the return variable.
5036 * - Some other integer types are not handled and fail.
5038 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5040 VARIANT varIn;
5041 HRESULT hRet = S_OK;
5042 float factor;
5043 VARIANT temp;
5045 VariantInit(&temp);
5047 TRACE("(%s,%d)\n", debugstr_variant(pVarIn), deci);
5049 /* Handle VT_DISPATCH by storing and taking address of returned value */
5050 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5052 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5053 if (FAILED(hRet)) goto VarRound_Exit;
5054 pVarIn = &temp;
5057 switch (V_VT(pVarIn))
5059 /* cases that fail on windows */
5060 case VT_I1:
5061 case VT_I8:
5062 case VT_UI2:
5063 case VT_UI4:
5064 hRet = DISP_E_BADVARTYPE;
5065 break;
5067 /* cases just copying in to out */
5068 case VT_UI1:
5069 V_VT(pVarOut) = V_VT(pVarIn);
5070 V_UI1(pVarOut) = V_UI1(pVarIn);
5071 break;
5072 case VT_I2:
5073 V_VT(pVarOut) = V_VT(pVarIn);
5074 V_I2(pVarOut) = V_I2(pVarIn);
5075 break;
5076 case VT_I4:
5077 V_VT(pVarOut) = V_VT(pVarIn);
5078 V_I4(pVarOut) = V_I4(pVarIn);
5079 break;
5080 case VT_NULL:
5081 V_VT(pVarOut) = V_VT(pVarIn);
5082 /* value unchanged */
5083 break;
5085 /* cases that change type */
5086 case VT_EMPTY:
5087 V_VT(pVarOut) = VT_I2;
5088 V_I2(pVarOut) = 0;
5089 break;
5090 case VT_BOOL:
5091 V_VT(pVarOut) = VT_I2;
5092 V_I2(pVarOut) = V_BOOL(pVarIn);
5093 break;
5094 case VT_BSTR:
5095 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5096 if (FAILED(hRet))
5097 break;
5098 V_VT(&varIn)=VT_R8;
5099 pVarIn = &varIn;
5100 /* Fall through ... */
5102 /* cases we need to do math */
5103 case VT_R8:
5104 if (V_R8(pVarIn)>0) {
5105 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5106 } else {
5107 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5109 V_VT(pVarOut) = V_VT(pVarIn);
5110 break;
5111 case VT_R4:
5112 if (V_R4(pVarIn)>0) {
5113 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5114 } else {
5115 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5117 V_VT(pVarOut) = V_VT(pVarIn);
5118 break;
5119 case VT_DATE:
5120 if (V_DATE(pVarIn)>0) {
5121 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5122 } else {
5123 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5125 V_VT(pVarOut) = V_VT(pVarIn);
5126 break;
5127 case VT_CY:
5128 if (deci>3)
5129 factor=1;
5130 else
5131 factor=pow(10, 4-deci);
5133 if (V_CY(pVarIn).int64>0) {
5134 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5135 } else {
5136 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5138 V_VT(pVarOut) = V_VT(pVarIn);
5139 break;
5140 case VT_DECIMAL:
5142 double dbl;
5144 hRet = VarR8FromDec(&V_DECIMAL(pVarIn), &dbl);
5145 if (FAILED(hRet))
5146 break;
5148 if (dbl>0.0f)
5149 dbl = floor(dbl*pow(10,deci)+0.5);
5150 else
5151 dbl = ceil(dbl*pow(10,deci)-0.5);
5153 V_VT(pVarOut)=VT_DECIMAL;
5154 hRet = VarDecFromR8(dbl, &V_DECIMAL(pVarOut));
5155 break;
5157 /* cases we don't know yet */
5158 default:
5159 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5160 V_VT(pVarIn) & VT_TYPEMASK, deci);
5161 hRet = DISP_E_BADVARTYPE;
5163 VarRound_Exit:
5164 if (FAILED(hRet))
5165 V_VT(pVarOut) = VT_EMPTY;
5166 VariantClear(&temp);
5168 TRACE("returning 0x%08x %s\n", hRet, debugstr_variant(pVarOut));
5169 return hRet;
5172 /**********************************************************************
5173 * VarIdiv [OLEAUT32.153]
5175 * Converts input variants to integers and divides them.
5177 * PARAMS
5178 * left [I] Left hand variant
5179 * right [I] Right hand variant
5180 * result [O] Destination for quotient
5182 * RETURNS
5183 * Success: S_OK. result contains the quotient.
5184 * Failure: An HRESULT error code indicating the error.
5186 * NOTES
5187 * If either expression is null, null is returned, as per MSDN
5189 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5191 HRESULT hres = S_OK;
5192 VARTYPE resvt = VT_EMPTY;
5193 VARTYPE leftvt,rightvt;
5194 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5195 VARIANT lv,rv;
5196 VARIANT tempLeft, tempRight;
5198 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5200 VariantInit(&lv);
5201 VariantInit(&rv);
5202 VariantInit(&tempLeft);
5203 VariantInit(&tempRight);
5205 leftvt = V_VT(left)&VT_TYPEMASK;
5206 rightvt = V_VT(right)&VT_TYPEMASK;
5207 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5208 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5210 if (leftExtraFlags != rightExtraFlags)
5212 hres = DISP_E_BADVARTYPE;
5213 goto end;
5215 ExtraFlags = leftExtraFlags;
5217 /* Native VarIdiv always returns an error when using extra
5218 * flags or if the variant combination is I8 and INT.
5220 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5221 (leftvt == VT_INT && rightvt == VT_I8) ||
5222 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5223 ExtraFlags != 0)
5225 hres = DISP_E_BADVARTYPE;
5226 goto end;
5229 /* Determine variant type */
5230 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5232 V_VT(result) = VT_NULL;
5233 hres = S_OK;
5234 goto end;
5236 else if (leftvt == VT_I8 || rightvt == VT_I8)
5237 resvt = VT_I8;
5238 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5239 leftvt == VT_INT || rightvt == VT_INT ||
5240 leftvt == VT_UINT || rightvt == VT_UINT ||
5241 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5242 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5243 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5244 leftvt == VT_I1 || rightvt == VT_I1 ||
5245 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5246 leftvt == VT_DATE || rightvt == VT_DATE ||
5247 leftvt == VT_CY || rightvt == VT_CY ||
5248 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5249 leftvt == VT_R8 || rightvt == VT_R8 ||
5250 leftvt == VT_R4 || rightvt == VT_R4)
5251 resvt = VT_I4;
5252 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5253 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5254 leftvt == VT_EMPTY)
5255 resvt = VT_I2;
5256 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5257 resvt = VT_UI1;
5258 else
5260 hres = DISP_E_BADVARTYPE;
5261 goto end;
5264 /* coerce to the result type */
5265 hres = VariantChangeType(&lv, left, 0, resvt);
5266 if (hres != S_OK) goto end;
5267 hres = VariantChangeType(&rv, right, 0, resvt);
5268 if (hres != S_OK) goto end;
5270 /* do the math */
5271 V_VT(result) = resvt;
5272 switch (resvt)
5274 case VT_UI1:
5275 if (V_UI1(&rv) == 0)
5277 hres = DISP_E_DIVBYZERO;
5278 V_VT(result) = VT_EMPTY;
5280 else
5281 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5282 break;
5283 case VT_I2:
5284 if (V_I2(&rv) == 0)
5286 hres = DISP_E_DIVBYZERO;
5287 V_VT(result) = VT_EMPTY;
5289 else
5290 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5291 break;
5292 case VT_I4:
5293 if (V_I4(&rv) == 0)
5295 hres = DISP_E_DIVBYZERO;
5296 V_VT(result) = VT_EMPTY;
5298 else
5299 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5300 break;
5301 case VT_I8:
5302 if (V_I8(&rv) == 0)
5304 hres = DISP_E_DIVBYZERO;
5305 V_VT(result) = VT_EMPTY;
5307 else
5308 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5309 break;
5310 default:
5311 FIXME("Couldn't integer divide variant types %d,%d\n",
5312 leftvt,rightvt);
5315 end:
5316 VariantClear(&lv);
5317 VariantClear(&rv);
5318 VariantClear(&tempLeft);
5319 VariantClear(&tempRight);
5321 return hres;
5325 /**********************************************************************
5326 * VarMod [OLEAUT32.155]
5328 * Perform the modulus operation of the right hand variant on the left
5330 * PARAMS
5331 * left [I] Left hand variant
5332 * right [I] Right hand variant
5333 * result [O] Destination for converted value
5335 * RETURNS
5336 * Success: S_OK. result contains the remainder.
5337 * Failure: An HRESULT error code indicating the error.
5339 * NOTE:
5340 * If an error occurs the type of result will be modified but the value will not be.
5341 * Doesn't support arrays or any special flags yet.
5343 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5345 BOOL lOk = TRUE;
5346 HRESULT rc = E_FAIL;
5347 int resT = 0;
5348 VARIANT lv,rv;
5349 VARIANT tempLeft, tempRight;
5351 VariantInit(&tempLeft);
5352 VariantInit(&tempRight);
5353 VariantInit(&lv);
5354 VariantInit(&rv);
5356 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5358 /* Handle VT_DISPATCH by storing and taking address of returned value */
5359 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5361 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5362 if (FAILED(rc)) goto end;
5363 left = &tempLeft;
5365 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5367 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5368 if (FAILED(rc)) goto end;
5369 right = &tempRight;
5372 /* check for invalid inputs */
5373 lOk = TRUE;
5374 switch (V_VT(left) & VT_TYPEMASK) {
5375 case VT_BOOL :
5376 case VT_I1 :
5377 case VT_I2 :
5378 case VT_I4 :
5379 case VT_I8 :
5380 case VT_INT :
5381 case VT_UI1 :
5382 case VT_UI2 :
5383 case VT_UI4 :
5384 case VT_UI8 :
5385 case VT_UINT :
5386 case VT_R4 :
5387 case VT_R8 :
5388 case VT_CY :
5389 case VT_EMPTY:
5390 case VT_DATE :
5391 case VT_BSTR :
5392 case VT_DECIMAL:
5393 break;
5394 case VT_VARIANT:
5395 case VT_UNKNOWN:
5396 V_VT(result) = VT_EMPTY;
5397 rc = DISP_E_TYPEMISMATCH;
5398 goto end;
5399 case VT_ERROR:
5400 rc = DISP_E_TYPEMISMATCH;
5401 goto end;
5402 case VT_RECORD:
5403 V_VT(result) = VT_EMPTY;
5404 rc = DISP_E_TYPEMISMATCH;
5405 goto end;
5406 case VT_NULL:
5407 break;
5408 default:
5409 V_VT(result) = VT_EMPTY;
5410 rc = DISP_E_BADVARTYPE;
5411 goto end;
5415 switch (V_VT(right) & VT_TYPEMASK) {
5416 case VT_BOOL :
5417 case VT_I1 :
5418 case VT_I2 :
5419 case VT_I4 :
5420 case VT_I8 :
5421 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5423 V_VT(result) = VT_EMPTY;
5424 rc = DISP_E_TYPEMISMATCH;
5425 goto end;
5427 case VT_INT :
5428 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5430 V_VT(result) = VT_EMPTY;
5431 rc = DISP_E_TYPEMISMATCH;
5432 goto end;
5434 case VT_UI1 :
5435 case VT_UI2 :
5436 case VT_UI4 :
5437 case VT_UI8 :
5438 case VT_UINT :
5439 case VT_R4 :
5440 case VT_R8 :
5441 case VT_CY :
5442 if(V_VT(left) == VT_EMPTY)
5444 V_VT(result) = VT_I4;
5445 rc = S_OK;
5446 goto end;
5448 case VT_EMPTY:
5449 case VT_DATE :
5450 case VT_DECIMAL:
5451 if(V_VT(left) == VT_ERROR)
5453 V_VT(result) = VT_EMPTY;
5454 rc = DISP_E_TYPEMISMATCH;
5455 goto end;
5457 case VT_BSTR:
5458 if(V_VT(left) == VT_NULL)
5460 V_VT(result) = VT_NULL;
5461 rc = S_OK;
5462 goto end;
5464 break;
5466 case VT_VOID:
5467 V_VT(result) = VT_EMPTY;
5468 rc = DISP_E_BADVARTYPE;
5469 goto end;
5470 case VT_NULL:
5471 if(V_VT(left) == VT_VOID)
5473 V_VT(result) = VT_EMPTY;
5474 rc = DISP_E_BADVARTYPE;
5475 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5476 lOk)
5478 V_VT(result) = VT_NULL;
5479 rc = S_OK;
5480 } else
5482 V_VT(result) = VT_NULL;
5483 rc = DISP_E_BADVARTYPE;
5485 goto end;
5486 case VT_VARIANT:
5487 case VT_UNKNOWN:
5488 V_VT(result) = VT_EMPTY;
5489 rc = DISP_E_TYPEMISMATCH;
5490 goto end;
5491 case VT_ERROR:
5492 rc = DISP_E_TYPEMISMATCH;
5493 goto end;
5494 case VT_RECORD:
5495 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5497 V_VT(result) = VT_EMPTY;
5498 rc = DISP_E_BADVARTYPE;
5499 } else
5501 V_VT(result) = VT_EMPTY;
5502 rc = DISP_E_TYPEMISMATCH;
5504 goto end;
5505 default:
5506 V_VT(result) = VT_EMPTY;
5507 rc = DISP_E_BADVARTYPE;
5508 goto end;
5511 /* determine the result type */
5512 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5513 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5514 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5515 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5516 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5517 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5518 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5519 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5520 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5521 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5522 else resT = VT_I4; /* most outputs are I4 */
5524 /* convert to I8 for the modulo */
5525 rc = VariantChangeType(&lv, left, 0, VT_I8);
5526 if(FAILED(rc))
5528 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5529 goto end;
5532 rc = VariantChangeType(&rv, right, 0, VT_I8);
5533 if(FAILED(rc))
5535 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5536 goto end;
5539 /* if right is zero set VT_EMPTY and return divide by zero */
5540 if(V_I8(&rv) == 0)
5542 V_VT(result) = VT_EMPTY;
5543 rc = DISP_E_DIVBYZERO;
5544 goto end;
5547 /* perform the modulo operation */
5548 V_VT(result) = VT_I8;
5549 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5551 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5552 wine_dbgstr_longlong(V_I8(&lv)), wine_dbgstr_longlong(V_I8(&rv)),
5553 wine_dbgstr_longlong(V_I8(result)));
5555 /* convert left and right to the destination type */
5556 rc = VariantChangeType(result, result, 0, resT);
5557 if(FAILED(rc))
5559 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5560 /* fall to end of function */
5563 end:
5564 VariantClear(&lv);
5565 VariantClear(&rv);
5566 VariantClear(&tempLeft);
5567 VariantClear(&tempRight);
5568 return rc;
5571 /**********************************************************************
5572 * VarPow [OLEAUT32.158]
5574 * Computes the power of one variant to another variant.
5576 * PARAMS
5577 * left [I] First variant
5578 * right [I] Second variant
5579 * result [O] Result variant
5581 * RETURNS
5582 * Success: S_OK.
5583 * Failure: An HRESULT error code indicating the error.
5585 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5587 HRESULT hr = S_OK;
5588 VARIANT dl,dr;
5589 VARTYPE resvt = VT_EMPTY;
5590 VARTYPE leftvt,rightvt;
5591 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5592 VARIANT tempLeft, tempRight;
5594 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5596 VariantInit(&dl);
5597 VariantInit(&dr);
5598 VariantInit(&tempLeft);
5599 VariantInit(&tempRight);
5601 /* Handle VT_DISPATCH by storing and taking address of returned value */
5602 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5604 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5605 if (FAILED(hr)) goto end;
5606 left = &tempLeft;
5608 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5610 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5611 if (FAILED(hr)) goto end;
5612 right = &tempRight;
5615 leftvt = V_VT(left)&VT_TYPEMASK;
5616 rightvt = V_VT(right)&VT_TYPEMASK;
5617 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5618 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5620 if (leftExtraFlags != rightExtraFlags)
5622 hr = DISP_E_BADVARTYPE;
5623 goto end;
5625 ExtraFlags = leftExtraFlags;
5627 /* Native VarPow always returns an error when using extra flags */
5628 if (ExtraFlags != 0)
5630 hr = DISP_E_BADVARTYPE;
5631 goto end;
5634 /* Determine return type */
5635 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5636 V_VT(result) = VT_NULL;
5637 hr = S_OK;
5638 goto end;
5640 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5641 leftvt == VT_I4 || leftvt == VT_R4 ||
5642 leftvt == VT_R8 || leftvt == VT_CY ||
5643 leftvt == VT_DATE || leftvt == VT_BSTR ||
5644 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5645 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5646 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5647 rightvt == VT_I4 || rightvt == VT_R4 ||
5648 rightvt == VT_R8 || rightvt == VT_CY ||
5649 rightvt == VT_DATE || rightvt == VT_BSTR ||
5650 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5651 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5652 resvt = VT_R8;
5653 else
5655 hr = DISP_E_BADVARTYPE;
5656 goto end;
5659 hr = VariantChangeType(&dl,left,0,resvt);
5660 if (FAILED(hr)) {
5661 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5662 hr = E_FAIL;
5663 goto end;
5666 hr = VariantChangeType(&dr,right,0,resvt);
5667 if (FAILED(hr)) {
5668 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5669 hr = E_FAIL;
5670 goto end;
5673 V_VT(result) = VT_R8;
5674 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5676 end:
5677 VariantClear(&dl);
5678 VariantClear(&dr);
5679 VariantClear(&tempLeft);
5680 VariantClear(&tempRight);
5682 return hr;
5685 /**********************************************************************
5686 * VarImp [OLEAUT32.154]
5688 * Bitwise implication of two variants.
5690 * PARAMS
5691 * left [I] First variant
5692 * right [I] Second variant
5693 * result [O] Result variant
5695 * RETURNS
5696 * Success: S_OK.
5697 * Failure: An HRESULT error code indicating the error.
5699 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5701 HRESULT hres = S_OK;
5702 VARTYPE resvt = VT_EMPTY;
5703 VARTYPE leftvt,rightvt;
5704 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5705 VARIANT lv,rv;
5706 double d;
5707 VARIANT tempLeft, tempRight;
5709 VariantInit(&lv);
5710 VariantInit(&rv);
5711 VariantInit(&tempLeft);
5712 VariantInit(&tempRight);
5714 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5716 /* Handle VT_DISPATCH by storing and taking address of returned value */
5717 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5719 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5720 if (FAILED(hres)) goto VarImp_Exit;
5721 left = &tempLeft;
5723 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5725 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5726 if (FAILED(hres)) goto VarImp_Exit;
5727 right = &tempRight;
5730 leftvt = V_VT(left)&VT_TYPEMASK;
5731 rightvt = V_VT(right)&VT_TYPEMASK;
5732 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5733 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5735 if (leftExtraFlags != rightExtraFlags)
5737 hres = DISP_E_BADVARTYPE;
5738 goto VarImp_Exit;
5740 ExtraFlags = leftExtraFlags;
5742 /* Native VarImp always returns an error when using extra
5743 * flags or if the variants are I8 and INT.
5745 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5746 ExtraFlags != 0)
5748 hres = DISP_E_BADVARTYPE;
5749 goto VarImp_Exit;
5752 /* Determine result type */
5753 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5754 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5756 V_VT(result) = VT_NULL;
5757 hres = S_OK;
5758 goto VarImp_Exit;
5760 else if (leftvt == VT_I8 || rightvt == VT_I8)
5761 resvt = VT_I8;
5762 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5763 leftvt == VT_INT || rightvt == VT_INT ||
5764 leftvt == VT_UINT || rightvt == VT_UINT ||
5765 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5766 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5767 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5768 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5769 leftvt == VT_DATE || rightvt == VT_DATE ||
5770 leftvt == VT_CY || rightvt == VT_CY ||
5771 leftvt == VT_R8 || rightvt == VT_R8 ||
5772 leftvt == VT_R4 || rightvt == VT_R4 ||
5773 leftvt == VT_I1 || rightvt == VT_I1)
5774 resvt = VT_I4;
5775 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5776 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5777 (leftvt == VT_NULL && rightvt == VT_UI1))
5778 resvt = VT_UI1;
5779 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5780 leftvt == VT_I2 || rightvt == VT_I2 ||
5781 leftvt == VT_UI1 || rightvt == VT_UI1)
5782 resvt = VT_I2;
5783 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5784 leftvt == VT_BSTR || rightvt == VT_BSTR)
5785 resvt = VT_BOOL;
5787 /* VT_NULL requires special handling for when the opposite
5788 * variant is equal to something other than -1.
5789 * (NULL Imp 0 = NULL, NULL Imp n = n)
5791 if (leftvt == VT_NULL)
5793 VARIANT_BOOL b;
5794 switch(rightvt)
5796 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5797 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5798 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5799 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5800 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5801 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5802 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5803 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5804 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5805 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5806 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5807 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5808 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5809 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5810 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5811 case VT_DECIMAL:
5812 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5813 resvt = VT_NULL;
5814 break;
5815 case VT_BSTR:
5816 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5817 if (FAILED(hres)) goto VarImp_Exit;
5818 else if (!b)
5819 V_VT(result) = VT_NULL;
5820 else
5822 V_VT(result) = VT_BOOL;
5823 V_BOOL(result) = b;
5825 goto VarImp_Exit;
5827 if (resvt == VT_NULL)
5829 V_VT(result) = resvt;
5830 goto VarImp_Exit;
5832 else
5834 hres = VariantChangeType(result,right,0,resvt);
5835 goto VarImp_Exit;
5839 /* Special handling is required when NULL is the right variant.
5840 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5842 else if (rightvt == VT_NULL)
5844 VARIANT_BOOL b;
5845 switch(leftvt)
5847 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5848 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5849 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5850 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5851 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5852 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5853 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5854 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5855 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5856 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5857 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5858 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5859 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5860 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5861 case VT_DECIMAL:
5862 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5863 resvt = VT_NULL;
5864 break;
5865 case VT_BSTR:
5866 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5867 if (FAILED(hres)) goto VarImp_Exit;
5868 else if (b == VARIANT_TRUE)
5869 resvt = VT_NULL;
5871 if (resvt == VT_NULL)
5873 V_VT(result) = resvt;
5874 goto VarImp_Exit;
5878 hres = VariantCopy(&lv, left);
5879 if (FAILED(hres)) goto VarImp_Exit;
5881 if (rightvt == VT_NULL)
5883 memset( &rv, 0, sizeof(rv) );
5884 V_VT(&rv) = resvt;
5886 else
5888 hres = VariantCopy(&rv, right);
5889 if (FAILED(hres)) goto VarImp_Exit;
5892 if (V_VT(&lv) == VT_BSTR &&
5893 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5894 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5895 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5896 hres = VariantChangeType(&lv,&lv,0,resvt);
5897 if (FAILED(hres)) goto VarImp_Exit;
5899 if (V_VT(&rv) == VT_BSTR &&
5900 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5901 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5902 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5903 hres = VariantChangeType(&rv, &rv, 0, resvt);
5904 if (FAILED(hres)) goto VarImp_Exit;
5906 /* do the math */
5907 V_VT(result) = resvt;
5908 switch (resvt)
5910 case VT_I8:
5911 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5912 break;
5913 case VT_I4:
5914 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5915 break;
5916 case VT_I2:
5917 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
5918 break;
5919 case VT_UI1:
5920 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
5921 break;
5922 case VT_BOOL:
5923 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
5924 break;
5925 default:
5926 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5927 leftvt,rightvt);
5930 VarImp_Exit:
5932 VariantClear(&lv);
5933 VariantClear(&rv);
5934 VariantClear(&tempLeft);
5935 VariantClear(&tempRight);
5937 return hres;