makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / oleaut32 / variant.c
blob36191b6b42486c627158f0a7accec4dcf26c431b
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, VARIANT *src)
694 struct __tagBRECORD *dest_rec = &V_UNION(dest, brecVal);
695 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, 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, VARIANTARG* pvargSrc)
849 VARIANTARG vTmp, *pSrc = pvargSrc;
850 VARTYPE vt;
851 HRESULT hres = S_OK;
853 TRACE("(%s,%s)\n", debugstr_variant(pvargDest), debugstr_variant(pvargSrc));
855 if (!V_ISBYREF(pvargSrc))
856 return VariantCopy(pvargDest, pvargSrc);
858 /* Argument checking is more lax than VariantCopy()... */
859 vt = V_TYPE(pvargSrc);
860 if (V_ISARRAY(pvargSrc) || (V_VT(pvargSrc) == (VT_RECORD|VT_BYREF)) ||
861 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
862 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
864 /* OK */
866 else
867 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
869 if (pvargSrc == pvargDest)
871 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
872 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
874 vTmp = *pvargSrc;
875 pSrc = &vTmp;
876 V_VT(pvargDest) = VT_EMPTY;
878 else
880 /* Copy into another variant. Free the variant in pvargDest */
881 if (FAILED(hres = VariantClear(pvargDest)))
883 TRACE("VariantClear() of destination failed\n");
884 return hres;
888 if (V_ISARRAY(pSrc))
890 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
891 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
893 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
895 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
896 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
898 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
900 hres = VARIANT_CopyIRecordInfo(pvargDest, pvargSrc);
902 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
903 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
905 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
906 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
907 if (*V_UNKNOWNREF(pSrc))
908 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
910 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
912 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
913 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
914 hres = E_INVALIDARG; /* Don't dereference more than one level */
915 else
916 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
918 /* Use the dereferenced variants type value, not VT_VARIANT */
919 goto VariantCopyInd_Return;
921 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
923 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
924 sizeof(DECIMAL) - sizeof(USHORT));
926 else
928 /* Copy the pointed to data into this variant */
929 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
932 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
934 VariantCopyInd_Return:
936 if (pSrc != pvargSrc)
937 VariantClear(pSrc);
939 TRACE("returning 0x%08x, %s\n", hres, debugstr_variant(pvargDest));
940 return hres;
943 /******************************************************************************
944 * VariantChangeType [OLEAUT32.12]
946 * Change the type of a variant.
948 * PARAMS
949 * pvargDest [O] Destination for the converted variant
950 * pvargSrc [O] Source variant to change the type of
951 * wFlags [I] VARIANT_ flags from "oleauto.h"
952 * vt [I] Variant type to change pvargSrc into
954 * RETURNS
955 * Success: S_OK. pvargDest contains the converted value.
956 * Failure: An HRESULT error code describing the failure.
958 * NOTES
959 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
960 * See VariantChangeTypeEx.
962 HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
963 USHORT wFlags, VARTYPE vt)
965 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
968 /******************************************************************************
969 * VariantChangeTypeEx [OLEAUT32.147]
971 * Change the type of a variant.
973 * PARAMS
974 * pvargDest [O] Destination for the converted variant
975 * pvargSrc [O] Source variant to change the type of
976 * lcid [I] LCID for the conversion
977 * wFlags [I] VARIANT_ flags from "oleauto.h"
978 * vt [I] Variant type to change pvargSrc into
980 * RETURNS
981 * Success: S_OK. pvargDest contains the converted value.
982 * Failure: An HRESULT error code describing the failure.
984 * NOTES
985 * pvargDest and pvargSrc can point to the same variant to perform an in-place
986 * conversion. If the conversion is successful, pvargSrc will be freed.
988 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
989 LCID lcid, USHORT wFlags, VARTYPE vt)
991 HRESULT res = S_OK;
993 TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest),
994 debugstr_variant(pvargSrc), lcid, wFlags, debugstr_vt(vt));
996 if (vt == VT_CLSID)
997 res = DISP_E_BADVARTYPE;
998 else
1000 res = VARIANT_ValidateType(V_VT(pvargSrc));
1002 if (SUCCEEDED(res))
1004 res = VARIANT_ValidateType(vt);
1006 if (SUCCEEDED(res))
1008 VARIANTARG vTmp, vSrcDeref;
1010 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
1011 res = DISP_E_TYPEMISMATCH;
1012 else
1014 V_VT(&vTmp) = VT_EMPTY;
1015 V_VT(&vSrcDeref) = VT_EMPTY;
1016 VariantClear(&vTmp);
1017 VariantClear(&vSrcDeref);
1020 if (SUCCEEDED(res))
1022 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1023 if (SUCCEEDED(res))
1025 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1026 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1027 else
1028 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1030 if (SUCCEEDED(res)) {
1031 V_VT(&vTmp) = vt;
1032 res = VariantCopy(pvargDest, &vTmp);
1034 VariantClear(&vTmp);
1035 VariantClear(&vSrcDeref);
1042 TRACE("returning 0x%08x, %s\n", res, debugstr_variant(pvargDest));
1043 return res;
1046 /* Date Conversions */
1048 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1050 /* Convert a VT_DATE value to a Julian Date */
1051 static inline int VARIANT_JulianFromDate(int dateIn)
1053 int julianDays = dateIn;
1055 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1056 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1057 return julianDays;
1060 /* Convert a Julian Date to a VT_DATE value */
1061 static inline int VARIANT_DateFromJulian(int dateIn)
1063 int julianDays = dateIn;
1065 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1066 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1067 return julianDays;
1070 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1071 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1073 int j, i, l, n;
1075 l = jd + 68569;
1076 n = l * 4 / 146097;
1077 l -= (n * 146097 + 3) / 4;
1078 i = (4000 * (l + 1)) / 1461001;
1079 l += 31 - (i * 1461) / 4;
1080 j = (l * 80) / 2447;
1081 *day = l - (j * 2447) / 80;
1082 l = j / 11;
1083 *month = (j + 2) - (12 * l);
1084 *year = 100 * (n - 49) + i + l;
1087 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1088 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1090 int m12 = (month - 14) / 12;
1092 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1093 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1096 /* Macros for accessing DOS format date/time fields */
1097 #define DOS_YEAR(x) (1980 + (x >> 9))
1098 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1099 #define DOS_DAY(x) (x & 0x1f)
1100 #define DOS_HOUR(x) (x >> 11)
1101 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1102 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1103 /* Create a DOS format date/time */
1104 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1105 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1107 /* Roll a date forwards or backwards to correct it */
1108 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1110 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1111 short iYear, iMonth, iDay, iHour, iMinute, iSecond;
1113 /* interpret values signed */
1114 iYear = lpUd->st.wYear;
1115 iMonth = lpUd->st.wMonth;
1116 iDay = lpUd->st.wDay;
1117 iHour = lpUd->st.wHour;
1118 iMinute = lpUd->st.wMinute;
1119 iSecond = lpUd->st.wSecond;
1121 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay, iMonth,
1122 iYear, iHour, iMinute, iSecond);
1124 if (iYear > 9999 || iYear < -9999)
1125 return E_INVALIDARG; /* Invalid value */
1126 /* Year 0 to 29 are treated as 2000 + year */
1127 if (iYear >= 0 && iYear < 30)
1128 iYear += 2000;
1129 /* Remaining years < 100 are treated as 1900 + year */
1130 else if (iYear >= 30 && iYear < 100)
1131 iYear += 1900;
1133 iMinute += iSecond / 60;
1134 iSecond = iSecond % 60;
1135 iHour += iMinute / 60;
1136 iMinute = iMinute % 60;
1137 iDay += iHour / 24;
1138 iHour = iHour % 24;
1139 iYear += iMonth / 12;
1140 iMonth = iMonth % 12;
1141 if (iMonth<=0) {iMonth+=12; iYear--;}
1142 while (iDay > days[iMonth])
1144 if (iMonth == 2 && IsLeapYear(iYear))
1145 iDay -= 29;
1146 else
1147 iDay -= days[iMonth];
1148 iMonth++;
1149 iYear += iMonth / 12;
1150 iMonth = iMonth % 12;
1152 while (iDay <= 0)
1154 iMonth--;
1155 if (iMonth<=0) {iMonth+=12; iYear--;}
1156 if (iMonth == 2 && IsLeapYear(iYear))
1157 iDay += 29;
1158 else
1159 iDay += days[iMonth];
1162 if (iSecond<0){iSecond+=60; iMinute--;}
1163 if (iMinute<0){iMinute+=60; iHour--;}
1164 if (iHour<0) {iHour+=24; iDay--;}
1165 if (iYear<=0) iYear+=2000;
1167 lpUd->st.wYear = iYear;
1168 lpUd->st.wMonth = iMonth;
1169 lpUd->st.wDay = iDay;
1170 lpUd->st.wHour = iHour;
1171 lpUd->st.wMinute = iMinute;
1172 lpUd->st.wSecond = iSecond;
1174 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1175 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1176 return S_OK;
1179 /**********************************************************************
1180 * DosDateTimeToVariantTime [OLEAUT32.14]
1182 * Convert a Dos format date and time into variant VT_DATE format.
1184 * PARAMS
1185 * wDosDate [I] Dos format date
1186 * wDosTime [I] Dos format time
1187 * pDateOut [O] Destination for VT_DATE format
1189 * RETURNS
1190 * Success: TRUE. pDateOut contains the converted time.
1191 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1193 * NOTES
1194 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1195 * - Dos format times are accurate to only 2 second precision.
1196 * - The format of a Dos Date is:
1197 *| Bits Values Meaning
1198 *| ---- ------ -------
1199 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1200 *| the days in the month rolls forward the extra days.
1201 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1202 *| year. 13-15 are invalid.
1203 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1204 * - The format of a Dos Time is:
1205 *| Bits Values Meaning
1206 *| ---- ------ -------
1207 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1208 *| 5-10 0-59 Minutes. 60-63 are invalid.
1209 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1211 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1212 double *pDateOut)
1214 UDATE ud;
1216 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1217 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1218 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1219 pDateOut);
1221 ud.st.wYear = DOS_YEAR(wDosDate);
1222 ud.st.wMonth = DOS_MONTH(wDosDate);
1223 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1224 return FALSE;
1225 ud.st.wDay = DOS_DAY(wDosDate);
1226 ud.st.wHour = DOS_HOUR(wDosTime);
1227 ud.st.wMinute = DOS_MINUTE(wDosTime);
1228 ud.st.wSecond = DOS_SECOND(wDosTime);
1229 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1230 if (ud.st.wHour > 23 || ud.st.wMinute > 59 || ud.st.wSecond > 59)
1231 return FALSE; /* Invalid values in Dos*/
1233 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1236 /**********************************************************************
1237 * VariantTimeToDosDateTime [OLEAUT32.13]
1239 * Convert a variant format date into a Dos format date and time.
1241 * dateIn [I] VT_DATE time format
1242 * pwDosDate [O] Destination for Dos format date
1243 * pwDosTime [O] Destination for Dos format time
1245 * RETURNS
1246 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1247 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1249 * NOTES
1250 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1252 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1254 UDATE ud;
1256 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1258 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1259 return FALSE;
1261 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1262 return FALSE;
1264 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1265 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1267 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1268 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1269 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1270 return TRUE;
1273 /***********************************************************************
1274 * SystemTimeToVariantTime [OLEAUT32.184]
1276 * Convert a System format date and time into variant VT_DATE format.
1278 * PARAMS
1279 * lpSt [I] System format date and time
1280 * pDateOut [O] Destination for VT_DATE format date
1282 * RETURNS
1283 * Success: TRUE. *pDateOut contains the converted value.
1284 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1286 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1288 UDATE ud;
1290 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1291 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1293 if (lpSt->wMonth > 12)
1294 return FALSE;
1295 if (lpSt->wDay > 31)
1296 return FALSE;
1297 if ((short)lpSt->wYear < 0)
1298 return FALSE;
1300 ud.st = *lpSt;
1301 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1304 /***********************************************************************
1305 * VariantTimeToSystemTime [OLEAUT32.185]
1307 * Convert a variant VT_DATE into a System format date and time.
1309 * PARAMS
1310 * datein [I] Variant VT_DATE format date
1311 * lpSt [O] Destination for System format date and time
1313 * RETURNS
1314 * Success: TRUE. *lpSt contains the converted value.
1315 * Failure: FALSE, if dateIn is too large or small.
1317 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1319 UDATE ud;
1321 TRACE("(%g,%p)\n", dateIn, lpSt);
1323 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1324 return FALSE;
1326 *lpSt = ud.st;
1327 return TRUE;
1330 /***********************************************************************
1331 * VarDateFromUdateEx [OLEAUT32.319]
1333 * Convert an unpacked format date and time to a variant VT_DATE.
1335 * PARAMS
1336 * pUdateIn [I] Unpacked format date and time to convert
1337 * lcid [I] Locale identifier for the conversion
1338 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1339 * pDateOut [O] Destination for variant VT_DATE.
1341 * RETURNS
1342 * Success: S_OK. *pDateOut contains the converted value.
1343 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1345 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1347 UDATE ud;
1348 double dateVal = 0;
1350 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1351 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1352 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1353 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1354 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1356 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1357 FIXME("lcid possibly not handled, treating as en-us\n");
1358 if (dwFlags & ~(VAR_TIMEVALUEONLY|VAR_DATEVALUEONLY))
1359 FIXME("unsupported flags: %x\n", dwFlags);
1361 ud = *pUdateIn;
1363 if (dwFlags & VAR_VALIDDATE)
1364 WARN("Ignoring VAR_VALIDDATE\n");
1366 if (FAILED(VARIANT_RollUdate(&ud)))
1367 return E_INVALIDARG;
1369 /* Date */
1370 if (!(dwFlags & VAR_TIMEVALUEONLY))
1371 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1373 if ((dwFlags & VAR_TIMEVALUEONLY) || !(dwFlags & VAR_DATEVALUEONLY))
1375 double dateSign = (dateVal < 0.0) ? -1.0 : 1.0;
1377 /* Time */
1378 dateVal += ud.st.wHour / 24.0 * dateSign;
1379 dateVal += ud.st.wMinute / 1440.0 * dateSign;
1380 dateVal += ud.st.wSecond / 86400.0 * dateSign;
1383 TRACE("Returning %g\n", dateVal);
1384 *pDateOut = dateVal;
1385 return S_OK;
1388 /***********************************************************************
1389 * VarDateFromUdate [OLEAUT32.330]
1391 * Convert an unpacked format date and time to a variant VT_DATE.
1393 * PARAMS
1394 * pUdateIn [I] Unpacked format date and time to convert
1395 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1396 * pDateOut [O] Destination for variant VT_DATE.
1398 * RETURNS
1399 * Success: S_OK. *pDateOut contains the converted value.
1400 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1402 * NOTES
1403 * This function uses the United States English locale for the conversion. Use
1404 * VarDateFromUdateEx() for alternate locales.
1406 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1408 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1410 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1413 /***********************************************************************
1414 * VarUdateFromDate [OLEAUT32.331]
1416 * Convert a variant VT_DATE into an unpacked format date and time.
1418 * PARAMS
1419 * datein [I] Variant VT_DATE format date
1420 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1421 * lpUdate [O] Destination for unpacked format date and time
1423 * RETURNS
1424 * Success: S_OK. *lpUdate contains the converted value.
1425 * Failure: E_INVALIDARG, if dateIn is too large or small.
1427 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1429 /* Cumulative totals of days per month */
1430 static const USHORT cumulativeDays[] =
1432 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1434 double datePart, timePart;
1435 int julianDays;
1437 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1439 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1440 return E_INVALIDARG;
1442 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1443 /* Compensate for int truncation (always downwards) */
1444 timePart = fabs(dateIn - datePart) + 0.00000000001;
1445 if (timePart >= 1.0)
1446 timePart -= 0.00000000001;
1448 /* Date */
1449 julianDays = VARIANT_JulianFromDate(dateIn);
1450 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1451 &lpUdate->st.wDay);
1453 datePart = (datePart + 1.5) / 7.0;
1454 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1455 if (lpUdate->st.wDayOfWeek == 0)
1456 lpUdate->st.wDayOfWeek = 5;
1457 else if (lpUdate->st.wDayOfWeek == 1)
1458 lpUdate->st.wDayOfWeek = 6;
1459 else
1460 lpUdate->st.wDayOfWeek -= 2;
1462 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1463 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1464 else
1465 lpUdate->wDayOfYear = 0;
1467 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1468 lpUdate->wDayOfYear += lpUdate->st.wDay;
1470 /* Time */
1471 timePart *= 24.0;
1472 lpUdate->st.wHour = timePart;
1473 timePart -= lpUdate->st.wHour;
1474 timePart *= 60.0;
1475 lpUdate->st.wMinute = timePart;
1476 timePart -= lpUdate->st.wMinute;
1477 timePart *= 60.0;
1478 lpUdate->st.wSecond = timePart;
1479 timePart -= lpUdate->st.wSecond;
1480 lpUdate->st.wMilliseconds = 0;
1481 if (timePart > 0.5)
1483 /* Round the milliseconds, adjusting the time/date forward if needed */
1484 if (lpUdate->st.wSecond < 59)
1485 lpUdate->st.wSecond++;
1486 else
1488 lpUdate->st.wSecond = 0;
1489 if (lpUdate->st.wMinute < 59)
1490 lpUdate->st.wMinute++;
1491 else
1493 lpUdate->st.wMinute = 0;
1494 if (lpUdate->st.wHour < 23)
1495 lpUdate->st.wHour++;
1496 else
1498 lpUdate->st.wHour = 0;
1499 /* Roll over a whole day */
1500 if (++lpUdate->st.wDay > 28)
1501 VARIANT_RollUdate(lpUdate);
1506 return S_OK;
1509 #define GET_NUMBER_TEXT(fld,name) \
1510 buff[0] = 0; \
1511 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1512 WARN("buffer too small for " #fld "\n"); \
1513 else \
1514 if (buff[0]) lpChars->name = buff[0]; \
1515 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1517 /* Get the valid number characters for an lcid */
1518 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1520 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1521 static VARIANT_NUMBER_CHARS lastChars;
1522 static LCID lastLcid = -1;
1523 static DWORD lastFlags = 0;
1524 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1525 WCHAR buff[4];
1527 /* To make caching thread-safe, a critical section is needed */
1528 EnterCriticalSection(&cache_cs);
1530 /* Asking for default locale entries is very expensive: It is a registry
1531 server call. So cache one locally, as Microsoft does it too */
1532 if(lcid == lastLcid && dwFlags == lastFlags)
1534 memcpy(lpChars, &lastChars, sizeof(defaultChars));
1535 LeaveCriticalSection(&cache_cs);
1536 return;
1539 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1540 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1541 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1542 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1543 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1544 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1545 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1547 /* Local currency symbols are often 2 characters */
1548 lpChars->cCurrencyLocal2 = '\0';
1549 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, ARRAY_SIZE(buff)))
1551 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1552 case 2: lpChars->cCurrencyLocal = buff[0];
1553 break;
1554 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1556 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1557 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1559 memcpy(&lastChars, lpChars, sizeof(defaultChars));
1560 lastLcid = lcid;
1561 lastFlags = dwFlags;
1562 LeaveCriticalSection(&cache_cs);
1565 /* Number Parsing States */
1566 #define B_PROCESSING_EXPONENT 0x1
1567 #define B_NEGATIVE_EXPONENT 0x2
1568 #define B_EXPONENT_START 0x4
1569 #define B_INEXACT_ZEROS 0x8
1570 #define B_LEADING_ZERO 0x10
1571 #define B_PROCESSING_HEX 0x20
1572 #define B_PROCESSING_OCT 0x40
1574 static inline BOOL is_digit(WCHAR c)
1576 return '0' <= c && c <= '9';
1579 /**********************************************************************
1580 * VarParseNumFromStr [OLEAUT32.46]
1582 * Parse a string containing a number into a NUMPARSE structure.
1584 * PARAMS
1585 * lpszStr [I] String to parse number from
1586 * lcid [I] Locale Id for the conversion
1587 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1588 * pNumprs [I/O] Destination for parsed number
1589 * rgbDig [O] Destination for digits read in
1591 * RETURNS
1592 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1593 * the number.
1594 * Failure: E_INVALIDARG, if any parameter is invalid.
1595 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1596 * incorrectly.
1597 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1599 * NOTES
1600 * pNumprs must have the following fields set:
1601 * cDig: Set to the size of rgbDig.
1602 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1603 * from "oleauto.h".
1605 * FIXME
1606 * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1607 * numerals, so this has not been implemented.
1609 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1610 NUMPARSE *pNumprs, BYTE *rgbDig)
1612 VARIANT_NUMBER_CHARS chars;
1613 BYTE rgbTmp[1024];
1614 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1615 int iMaxDigits = ARRAY_SIZE(rgbTmp);
1616 int cchUsed = 0;
1618 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1620 if (!pNumprs || !rgbDig)
1621 return E_INVALIDARG;
1623 if (pNumprs->cDig < iMaxDigits)
1624 iMaxDigits = pNumprs->cDig;
1626 pNumprs->cDig = 0;
1627 pNumprs->dwOutFlags = 0;
1628 pNumprs->cchUsed = 0;
1629 pNumprs->nBaseShift = 0;
1630 pNumprs->nPwr10 = 0;
1632 if (!lpszStr)
1633 return DISP_E_TYPEMISMATCH;
1635 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1637 /* First consume all the leading symbols and space from the string */
1638 while (1)
1640 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && iswspace(*lpszStr))
1642 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1645 cchUsed++;
1646 lpszStr++;
1647 } while (iswspace(*lpszStr));
1649 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1650 *lpszStr == chars.cPositiveSymbol &&
1651 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1653 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1654 cchUsed++;
1655 lpszStr++;
1657 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1658 *lpszStr == chars.cNegativeSymbol &&
1659 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1661 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1662 cchUsed++;
1663 lpszStr++;
1665 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1666 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1667 *lpszStr == chars.cCurrencyLocal &&
1668 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1670 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1671 cchUsed++;
1672 lpszStr++;
1673 /* Only accept currency characters */
1674 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1675 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1677 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1678 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1680 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1681 cchUsed++;
1682 lpszStr++;
1684 else
1685 break;
1688 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1690 /* Only accept non-currency characters */
1691 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1692 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1695 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1696 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1698 dwState |= B_PROCESSING_HEX;
1699 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1700 cchUsed=cchUsed+2;
1701 lpszStr=lpszStr+2;
1703 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1704 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1706 dwState |= B_PROCESSING_OCT;
1707 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1708 cchUsed=cchUsed+2;
1709 lpszStr=lpszStr+2;
1712 /* Strip Leading zeros */
1713 while (*lpszStr == '0')
1715 dwState |= B_LEADING_ZERO;
1716 cchUsed++;
1717 lpszStr++;
1720 while (*lpszStr)
1722 if (is_digit(*lpszStr))
1724 if (dwState & B_PROCESSING_EXPONENT)
1726 int exponentSize = 0;
1727 if (dwState & B_EXPONENT_START)
1729 if (!is_digit(*lpszStr))
1730 break; /* No exponent digits - invalid */
1731 while (*lpszStr == '0')
1733 /* Skip leading zero's in the exponent */
1734 cchUsed++;
1735 lpszStr++;
1739 while (is_digit(*lpszStr))
1741 exponentSize *= 10;
1742 exponentSize += *lpszStr - '0';
1743 cchUsed++;
1744 lpszStr++;
1746 if (dwState & B_NEGATIVE_EXPONENT)
1747 exponentSize = -exponentSize;
1748 /* Add the exponent into the powers of 10 */
1749 pNumprs->nPwr10 += exponentSize;
1750 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1751 lpszStr--; /* back up to allow processing of next char */
1753 else
1755 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1756 && !(dwState & B_PROCESSING_OCT))
1758 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1760 if (*lpszStr != '0')
1761 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1763 /* This digit can't be represented, but count it in nPwr10 */
1764 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1765 pNumprs->nPwr10--;
1766 else
1767 pNumprs->nPwr10++;
1769 else
1771 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9')))
1772 break;
1774 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1775 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1777 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1779 pNumprs->cDig++;
1780 cchUsed++;
1783 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1785 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1786 cchUsed++;
1788 else if (*lpszStr == chars.cDecimalPoint &&
1789 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1790 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1792 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1793 cchUsed++;
1795 /* If we have no digits so far, skip leading zeros */
1796 if (!pNumprs->cDig)
1798 while (lpszStr[1] == '0')
1800 dwState |= B_LEADING_ZERO;
1801 cchUsed++;
1802 lpszStr++;
1803 pNumprs->nPwr10--;
1807 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1808 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1809 dwState & B_PROCESSING_HEX)
1811 if (pNumprs->cDig >= iMaxDigits)
1813 return DISP_E_OVERFLOW;
1815 else
1817 if (*lpszStr >= 'a')
1818 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1819 else
1820 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1822 pNumprs->cDig++;
1823 cchUsed++;
1825 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1826 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1827 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1829 dwState |= B_PROCESSING_EXPONENT;
1830 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1831 cchUsed++;
1833 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1835 cchUsed++; /* Ignore positive exponent */
1837 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1839 dwState |= B_NEGATIVE_EXPONENT;
1840 cchUsed++;
1842 else
1843 break; /* Stop at an unrecognised character */
1845 lpszStr++;
1848 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1850 /* Ensure a 0 on its own gets stored */
1851 pNumprs->cDig = 1;
1852 rgbTmp[0] = 0;
1855 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1857 pNumprs->cchUsed = cchUsed;
1858 WARN("didn't completely parse exponent\n");
1859 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1862 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1864 if (dwState & B_INEXACT_ZEROS)
1865 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1866 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1868 /* copy all of the digits into the output digit buffer */
1869 /* this is exactly what windows does although it also returns */
1870 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1871 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1873 if (dwState & B_PROCESSING_HEX) {
1874 /* hex numbers have always the same format */
1875 pNumprs->nPwr10=0;
1876 pNumprs->nBaseShift=4;
1877 } else {
1878 if (dwState & B_PROCESSING_OCT) {
1879 /* oct numbers have always the same format */
1880 pNumprs->nPwr10=0;
1881 pNumprs->nBaseShift=3;
1882 } else {
1883 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1885 pNumprs->nPwr10++;
1886 pNumprs->cDig--;
1890 } else
1892 /* Remove trailing zeros from the last (whole number or decimal) part */
1893 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1895 pNumprs->nPwr10++;
1896 pNumprs->cDig--;
1900 if (pNumprs->cDig <= iMaxDigits)
1901 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1902 else
1903 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1905 /* Copy the digits we processed into rgbDig */
1906 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1908 /* Consume any trailing symbols and space */
1909 while (1)
1911 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && iswspace(*lpszStr))
1913 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1916 cchUsed++;
1917 lpszStr++;
1918 } while (iswspace(*lpszStr));
1920 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1921 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1922 *lpszStr == chars.cPositiveSymbol)
1924 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1925 cchUsed++;
1926 lpszStr++;
1928 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1929 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1930 *lpszStr == chars.cNegativeSymbol)
1932 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1933 cchUsed++;
1934 lpszStr++;
1936 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1937 pNumprs->dwOutFlags & NUMPRS_PARENS)
1939 cchUsed++;
1940 lpszStr++;
1941 pNumprs->dwOutFlags |= NUMPRS_NEG;
1943 else
1944 break;
1947 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1949 pNumprs->cchUsed = cchUsed;
1950 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1953 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1954 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1956 if (!pNumprs->cDig)
1957 return DISP_E_TYPEMISMATCH; /* No Number found */
1959 pNumprs->cchUsed = cchUsed;
1960 return S_OK;
1963 /* VTBIT flags indicating an integer value */
1964 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1965 /* VTBIT flags indicating a real number value */
1966 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1968 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1969 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1970 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1971 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1973 /**********************************************************************
1974 * VarNumFromParseNum [OLEAUT32.47]
1976 * Convert a NUMPARSE structure into a numeric Variant type.
1978 * PARAMS
1979 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1980 * rgbDig [I] Source for the numbers digits
1981 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1982 * pVarDst [O] Destination for the converted Variant value.
1984 * RETURNS
1985 * Success: S_OK. pVarDst contains the converted value.
1986 * Failure: E_INVALIDARG, if any parameter is invalid.
1987 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1989 * NOTES
1990 * - The smallest favoured type present in dwVtBits that can represent the
1991 * number in pNumprs without losing precision is used.
1992 * - Signed types are preferred over unsigned types of the same size.
1993 * - Preferred types in order are: integer, float, double, currency then decimal.
1994 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1995 * for details of the rounding method.
1996 * - pVarDst is not cleared before the result is stored in it.
1997 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1998 * design?): If some other VTBIT's for integers are specified together
1999 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
2000 * the number to the smallest requested integer truncating this way the
2001 * number. Wine doesn't implement this "feature" (yet?).
2003 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
2004 ULONG dwVtBits, VARIANT *pVarDst)
2006 /* Scale factors and limits for double arithmetic */
2007 static const double dblMultipliers[11] = {
2008 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2009 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2011 static const double dblMinimums[11] = {
2012 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
2013 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
2014 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
2016 static const double dblMaximums[11] = {
2017 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
2018 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
2019 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
2022 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
2024 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
2026 if (pNumprs->nBaseShift)
2028 /* nBaseShift indicates a hex or octal number */
2029 ULONG64 ul64 = 0;
2030 LONG64 l64;
2031 int i;
2033 /* Convert the hex or octal number string into a UI64 */
2034 for (i = 0; i < pNumprs->cDig; i++)
2036 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
2038 TRACE("Overflow multiplying digits\n");
2039 return DISP_E_OVERFLOW;
2041 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
2044 /* also make a negative representation */
2045 l64=-ul64;
2047 /* Try signed and unsigned types in size order */
2048 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
2050 V_VT(pVarDst) = VT_I1;
2051 V_I1(pVarDst) = ul64;
2052 return S_OK;
2054 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2056 V_VT(pVarDst) = VT_UI1;
2057 V_UI1(pVarDst) = ul64;
2058 return S_OK;
2060 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2062 V_VT(pVarDst) = VT_I2;
2063 V_I2(pVarDst) = ul64;
2064 return S_OK;
2066 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2068 V_VT(pVarDst) = VT_UI2;
2069 V_UI2(pVarDst) = ul64;
2070 return S_OK;
2072 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2074 V_VT(pVarDst) = VT_I4;
2075 V_I4(pVarDst) = ul64;
2076 return S_OK;
2078 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2080 V_VT(pVarDst) = VT_UI4;
2081 V_UI4(pVarDst) = ul64;
2082 return S_OK;
2084 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2086 V_VT(pVarDst) = VT_I8;
2087 V_I8(pVarDst) = ul64;
2088 return S_OK;
2090 else if (dwVtBits & VTBIT_UI8)
2092 V_VT(pVarDst) = VT_UI8;
2093 V_UI8(pVarDst) = ul64;
2094 return S_OK;
2096 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2098 V_VT(pVarDst) = VT_DECIMAL;
2099 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2100 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2101 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2102 return S_OK;
2104 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2106 V_VT(pVarDst) = VT_R4;
2107 if (ul64 <= I4_MAX)
2108 V_R4(pVarDst) = ul64;
2109 else
2110 V_R4(pVarDst) = l64;
2111 return S_OK;
2113 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2115 V_VT(pVarDst) = VT_R8;
2116 if (ul64 <= I4_MAX)
2117 V_R8(pVarDst) = ul64;
2118 else
2119 V_R8(pVarDst) = l64;
2120 return S_OK;
2123 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2124 return DISP_E_OVERFLOW;
2127 /* Count the number of relevant fractional and whole digits stored,
2128 * And compute the divisor/multiplier to scale the number by.
2130 if (pNumprs->nPwr10 < 0)
2132 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2134 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2135 wholeNumberDigits = 0;
2136 fractionalDigits = pNumprs->cDig;
2137 divisor10 = -pNumprs->nPwr10;
2139 else
2141 /* An exactly represented real number e.g. 1.024 */
2142 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2143 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2144 divisor10 = pNumprs->cDig - wholeNumberDigits;
2147 else if (pNumprs->nPwr10 == 0)
2149 /* An exactly represented whole number e.g. 1024 */
2150 wholeNumberDigits = pNumprs->cDig;
2151 fractionalDigits = 0;
2153 else /* pNumprs->nPwr10 > 0 */
2155 /* A whole number followed by nPwr10 0's e.g. 102400 */
2156 wholeNumberDigits = pNumprs->cDig;
2157 fractionalDigits = 0;
2158 multiplier10 = pNumprs->nPwr10;
2161 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2162 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2163 multiplier10, divisor10);
2165 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2166 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL))))
2168 /* We have one or more integer output choices, and either:
2169 * 1) An integer input value, or
2170 * 2) A real number input value but no floating output choices.
2171 * Alternately, we have a DECIMAL output available and an integer input.
2173 * So, place the integer value into pVarDst, using the smallest type
2174 * possible and preferring signed over unsigned types.
2176 BOOL bOverflow = FALSE, bNegative;
2177 ULONG64 ul64 = 0;
2178 int i;
2180 /* Convert the integer part of the number into a UI8 */
2181 for (i = 0; i < wholeNumberDigits; i++)
2183 if (ul64 > UI8_MAX / 10 || (ul64 == UI8_MAX / 10 && rgbDig[i] > UI8_MAX % 10))
2185 TRACE("Overflow multiplying digits\n");
2186 bOverflow = TRUE;
2187 break;
2189 ul64 = ul64 * 10 + rgbDig[i];
2192 /* Account for the scale of the number */
2193 if (!bOverflow && multiplier10)
2195 for (i = 0; i < multiplier10; i++)
2197 if (ul64 > (UI8_MAX / 10))
2199 TRACE("Overflow scaling number\n");
2200 bOverflow = TRUE;
2201 break;
2203 ul64 = ul64 * 10;
2207 /* If we have any fractional digits, round the value.
2208 * Note we don't have to do this if divisor10 is < 1,
2209 * because this means the fractional part must be < 0.5
2211 if (!bOverflow && fractionalDigits && divisor10 > 0)
2213 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2214 BOOL bAdjust = FALSE;
2216 TRACE("first decimal value is %d\n", *fracDig);
2218 if (*fracDig > 5)
2219 bAdjust = TRUE; /* > 0.5 */
2220 else if (*fracDig == 5)
2222 for (i = 1; i < fractionalDigits; i++)
2224 if (fracDig[i])
2226 bAdjust = TRUE; /* > 0.5 */
2227 break;
2230 /* If exactly 0.5, round only odd values */
2231 if (i == fractionalDigits && (ul64 & 1))
2232 bAdjust = TRUE;
2235 if (bAdjust)
2237 if (ul64 == UI8_MAX)
2239 TRACE("Overflow after rounding\n");
2240 bOverflow = TRUE;
2242 ul64++;
2246 /* Zero is not a negative number */
2247 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64;
2249 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2251 /* For negative integers, try the signed types in size order */
2252 if (!bOverflow && bNegative)
2254 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2256 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2258 V_VT(pVarDst) = VT_I1;
2259 V_I1(pVarDst) = -ul64;
2260 return S_OK;
2262 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2264 V_VT(pVarDst) = VT_I2;
2265 V_I2(pVarDst) = -ul64;
2266 return S_OK;
2268 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2270 V_VT(pVarDst) = VT_I4;
2271 V_I4(pVarDst) = -ul64;
2272 return S_OK;
2274 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2276 V_VT(pVarDst) = VT_I8;
2277 V_I8(pVarDst) = -ul64;
2278 return S_OK;
2280 else if ((dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL)) == VTBIT_DECIMAL)
2282 /* Decimal is only output choice left - fast path */
2283 V_VT(pVarDst) = VT_DECIMAL;
2284 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2285 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2286 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2287 return S_OK;
2291 else if (!bOverflow)
2293 /* For positive integers, try signed then unsigned types in size order */
2294 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2296 V_VT(pVarDst) = VT_I1;
2297 V_I1(pVarDst) = ul64;
2298 return S_OK;
2300 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2302 V_VT(pVarDst) = VT_UI1;
2303 V_UI1(pVarDst) = ul64;
2304 return S_OK;
2306 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2308 V_VT(pVarDst) = VT_I2;
2309 V_I2(pVarDst) = ul64;
2310 return S_OK;
2312 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2314 V_VT(pVarDst) = VT_UI2;
2315 V_UI2(pVarDst) = ul64;
2316 return S_OK;
2318 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2320 V_VT(pVarDst) = VT_I4;
2321 V_I4(pVarDst) = ul64;
2322 return S_OK;
2324 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2326 V_VT(pVarDst) = VT_UI4;
2327 V_UI4(pVarDst) = ul64;
2328 return S_OK;
2330 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2332 V_VT(pVarDst) = VT_I8;
2333 V_I8(pVarDst) = ul64;
2334 return S_OK;
2336 else if (dwVtBits & VTBIT_UI8)
2338 V_VT(pVarDst) = VT_UI8;
2339 V_UI8(pVarDst) = ul64;
2340 return S_OK;
2342 else if ((dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL)) == VTBIT_DECIMAL)
2344 /* Decimal is only output choice left - fast path */
2345 V_VT(pVarDst) = VT_DECIMAL;
2346 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2347 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2348 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2349 return S_OK;
2354 if (dwVtBits & REAL_VTBITS)
2356 /* Try to put the number into a float or real */
2357 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2358 double whole = 0.0;
2359 int i;
2361 /* Convert the number into a double */
2362 for (i = 0; i < pNumprs->cDig; i++)
2363 whole = whole * 10.0 + rgbDig[i];
2365 TRACE("Whole double value is %16.16g\n", whole);
2367 /* Account for the scale */
2368 while (multiplier10 > 10)
2370 if (whole > dblMaximums[10])
2372 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2373 bOverflow = TRUE;
2374 break;
2376 whole = whole * dblMultipliers[10];
2377 multiplier10 -= 10;
2379 if (multiplier10 && !bOverflow)
2381 if (whole > dblMaximums[multiplier10])
2383 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2384 bOverflow = TRUE;
2386 else
2387 whole = whole * dblMultipliers[multiplier10];
2390 if (!bOverflow)
2391 TRACE("Scaled double value is %16.16g\n", whole);
2393 while (divisor10 > 10 && !bOverflow)
2395 if (whole < dblMinimums[10] && whole != 0)
2397 whole = 0; /* ignore underflow */
2398 divisor10 = 0;
2399 break;
2401 whole = whole / dblMultipliers[10];
2402 divisor10 -= 10;
2404 if (divisor10 && !bOverflow)
2406 if (whole < dblMinimums[divisor10] && whole != 0)
2408 whole = 0; /* ignore underflow */
2409 divisor10 = 0;
2411 else
2412 whole = whole / dblMultipliers[divisor10];
2414 if (!bOverflow)
2415 TRACE("Final double value is %16.16g\n", whole);
2417 if (dwVtBits & VTBIT_R4 &&
2418 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2420 TRACE("Set R4 to final value\n");
2421 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2422 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2423 return S_OK;
2426 if (dwVtBits & VTBIT_R8)
2428 TRACE("Set R8 to final value\n");
2429 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2430 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2431 return S_OK;
2434 if (dwVtBits & VTBIT_CY)
2436 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2438 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2439 TRACE("Set CY to final value\n");
2440 return S_OK;
2442 TRACE("Value Overflows CY\n");
2446 if (dwVtBits & VTBIT_DECIMAL)
2448 int i;
2449 ULONG carry;
2450 ULONG64 tmp;
2451 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2453 DECIMAL_SETZERO(*pDec);
2454 DEC_LO32(pDec) = 0;
2456 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2457 DEC_SIGN(pDec) = DECIMAL_NEG;
2458 else
2459 DEC_SIGN(pDec) = DECIMAL_POS;
2461 /* Factor the significant digits */
2462 for (i = 0; i < pNumprs->cDig; i++)
2464 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2465 carry = (ULONG)(tmp >> 32);
2466 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2467 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2468 carry = (ULONG)(tmp >> 32);
2469 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2470 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2471 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2473 if (tmp >> 32 & UI4_MAX)
2475 VarNumFromParseNum_DecOverflow:
2476 TRACE("Overflow\n");
2477 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2478 return DISP_E_OVERFLOW;
2482 /* Account for the scale of the number */
2483 while (multiplier10 > 0)
2485 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2486 carry = (ULONG)(tmp >> 32);
2487 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2488 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2489 carry = (ULONG)(tmp >> 32);
2490 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2491 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2492 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2494 if (tmp >> 32 & UI4_MAX)
2495 goto VarNumFromParseNum_DecOverflow;
2496 multiplier10--;
2498 DEC_SCALE(pDec) = divisor10;
2500 V_VT(pVarDst) = VT_DECIMAL;
2501 return S_OK;
2503 return DISP_E_OVERFLOW; /* No more output choices */
2506 /**********************************************************************
2507 * VarCat [OLEAUT32.318]
2509 * Concatenates one variant onto another.
2511 * PARAMS
2512 * left [I] First variant
2513 * right [I] Second variant
2514 * result [O] Result variant
2516 * RETURNS
2517 * Success: S_OK.
2518 * Failure: An HRESULT error code indicating the error.
2520 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2522 BSTR left_str = NULL, right_str = NULL;
2523 VARTYPE leftvt, rightvt;
2524 HRESULT hres;
2526 TRACE("%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), out);
2528 leftvt = V_VT(left);
2529 rightvt = V_VT(right);
2531 /* when both left and right are NULL the result is NULL */
2532 if (leftvt == VT_NULL && rightvt == VT_NULL)
2534 V_VT(out) = VT_NULL;
2535 return S_OK;
2538 /* There are many special case for errors and return types */
2539 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2540 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2541 hres = DISP_E_TYPEMISMATCH;
2542 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2543 leftvt == VT_R4 || leftvt == VT_R8 ||
2544 leftvt == VT_CY || leftvt == VT_BOOL ||
2545 leftvt == VT_BSTR || leftvt == VT_I1 ||
2546 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2547 leftvt == VT_UI4 || leftvt == VT_I8 ||
2548 leftvt == VT_UI8 || leftvt == VT_INT ||
2549 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2550 leftvt == VT_NULL || leftvt == VT_DATE ||
2551 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2553 (rightvt == VT_I2 || rightvt == VT_I4 ||
2554 rightvt == VT_R4 || rightvt == VT_R8 ||
2555 rightvt == VT_CY || rightvt == VT_BOOL ||
2556 rightvt == VT_BSTR || rightvt == VT_I1 ||
2557 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2558 rightvt == VT_UI4 || rightvt == VT_I8 ||
2559 rightvt == VT_UI8 || rightvt == VT_INT ||
2560 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2561 rightvt == VT_NULL || rightvt == VT_DATE ||
2562 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2563 hres = S_OK;
2564 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2565 hres = DISP_E_TYPEMISMATCH;
2566 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2567 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2568 hres = DISP_E_TYPEMISMATCH;
2569 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2570 rightvt == VT_DECIMAL)
2571 hres = DISP_E_BADVARTYPE;
2572 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2573 hres = DISP_E_TYPEMISMATCH;
2574 else if (leftvt == VT_VARIANT)
2575 hres = DISP_E_TYPEMISMATCH;
2576 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2577 leftvt == VT_NULL || leftvt == VT_I2 ||
2578 leftvt == VT_I4 || leftvt == VT_R4 ||
2579 leftvt == VT_R8 || leftvt == VT_CY ||
2580 leftvt == VT_DATE || leftvt == VT_BSTR ||
2581 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2582 leftvt == VT_I1 || leftvt == VT_UI1 ||
2583 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2584 leftvt == VT_I8 || leftvt == VT_UI8 ||
2585 leftvt == VT_INT || leftvt == VT_UINT))
2586 hres = DISP_E_TYPEMISMATCH;
2587 else
2588 hres = DISP_E_BADVARTYPE;
2590 /* if result type is not S_OK, then no need to go further */
2591 if (hres != S_OK)
2593 V_VT(out) = VT_EMPTY;
2594 return hres;
2597 if (leftvt == VT_BSTR)
2598 left_str = V_BSTR(left);
2599 else
2601 VARIANT converted, *tmp = left;
2603 VariantInit(&converted);
2604 if(leftvt == VT_DISPATCH)
2606 hres = VARIANT_FetchDispatchValue(left, &converted);
2607 if(FAILED(hres))
2608 goto failed;
2610 tmp = &converted;
2613 hres = VariantChangeTypeEx(&converted, tmp, 0, VARIANT_ALPHABOOL|VARIANT_LOCALBOOL, VT_BSTR);
2614 if (SUCCEEDED(hres))
2615 left_str = V_BSTR(&converted);
2616 else if (hres != DISP_E_TYPEMISMATCH)
2618 VariantClear(&converted);
2619 goto failed;
2623 if (rightvt == VT_BSTR)
2624 right_str = V_BSTR(right);
2625 else
2627 VARIANT converted, *tmp = right;
2629 VariantInit(&converted);
2630 if(rightvt == VT_DISPATCH)
2632 hres = VARIANT_FetchDispatchValue(right, &converted);
2633 if(FAILED(hres))
2634 goto failed;
2636 tmp = &converted;
2639 hres = VariantChangeTypeEx(&converted, tmp, 0, VARIANT_ALPHABOOL|VARIANT_LOCALBOOL, VT_BSTR);
2640 if (SUCCEEDED(hres))
2641 right_str = V_BSTR(&converted);
2642 else if (hres != DISP_E_TYPEMISMATCH)
2644 VariantClear(&converted);
2645 goto failed;
2650 V_VT(out) = VT_BSTR;
2651 hres = VarBstrCat(left_str, right_str, &V_BSTR(out));
2653 failed:
2654 if(V_VT(left) != VT_BSTR)
2655 SysFreeString(left_str);
2656 if(V_VT(right) != VT_BSTR)
2657 SysFreeString(right_str);
2658 return hres;
2662 /* Wrapper around VariantChangeTypeEx() which permits changing a
2663 variant with VT_RESERVED flag set. Needed by VarCmp. */
2664 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2665 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2667 VARIANTARG vtmpsrc = *pvargSrc;
2669 V_VT(&vtmpsrc) &= ~VT_RESERVED;
2670 return VariantChangeTypeEx(pvargDest,&vtmpsrc,lcid,wFlags,vt);
2673 /**********************************************************************
2674 * VarCmp [OLEAUT32.176]
2676 * Compare two variants.
2678 * PARAMS
2679 * left [I] First variant
2680 * right [I] Second variant
2681 * lcid [I] LCID (locale identifier) for the comparison
2682 * flags [I] Flags to be used in the comparison:
2683 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2684 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2686 * RETURNS
2687 * VARCMP_LT: left variant is less than right variant.
2688 * VARCMP_EQ: input variants are equal.
2689 * VARCMP_GT: left variant is greater than right variant.
2690 * VARCMP_NULL: either one of the input variants is NULL.
2691 * Failure: An HRESULT error code indicating the error.
2693 * NOTES
2694 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2695 * UI8 and UINT as input variants. INT is accepted only as left variant.
2697 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2698 * an ERROR variant will trigger an error.
2700 * Both input variants can have VT_RESERVED flag set which is ignored
2701 * unless one and only one of the variants is a BSTR and the other one
2702 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2703 * different meaning:
2704 * - BSTR and other: BSTR is always greater than the other variant.
2705 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2706 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2707 * comparison will take place else the BSTR is always greater.
2708 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2709 * variant is ignored and the return value depends only on the sign
2710 * of the BSTR if it is a number else the BSTR is always greater. A
2711 * positive BSTR is greater, a negative one is smaller than the other
2712 * variant.
2714 * SEE
2715 * VarBstrCmp for the lcid and flags usage.
2717 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2719 VARTYPE lvt, rvt, vt;
2720 VARIANT rv,lv;
2721 DWORD xmask;
2722 HRESULT rc;
2724 TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left), debugstr_variant(right), lcid, flags);
2726 lvt = V_VT(left) & VT_TYPEMASK;
2727 rvt = V_VT(right) & VT_TYPEMASK;
2728 xmask = (1 << lvt) | (1 << rvt);
2730 /* If we have any flag set except VT_RESERVED bail out.
2731 Same for the left input variant type > VT_INT and for the
2732 right input variant type > VT_I8. Yes, VT_INT is only supported
2733 as left variant. Go figure */
2734 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2735 lvt > VT_INT || rvt > VT_I8) {
2736 return DISP_E_BADVARTYPE;
2739 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2740 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2741 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2742 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2743 return DISP_E_TYPEMISMATCH;
2745 /* If both variants are VT_ERROR return VARCMP_EQ */
2746 if (xmask == VTBIT_ERROR)
2747 return VARCMP_EQ;
2748 else if (xmask & VTBIT_ERROR)
2749 return DISP_E_TYPEMISMATCH;
2751 if (xmask & VTBIT_NULL)
2752 return VARCMP_NULL;
2754 VariantInit(&lv);
2755 VariantInit(&rv);
2757 /* Two BSTRs, ignore VT_RESERVED */
2758 if (xmask == VTBIT_BSTR)
2759 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2761 /* A BSTR and another variant; we have to take care of VT_RESERVED */
2762 if (xmask & VTBIT_BSTR) {
2763 VARIANT *bstrv, *nonbv;
2764 VARTYPE nonbvt;
2765 int swap = 0;
2767 /* Swap the variants so the BSTR is always on the left */
2768 if (lvt == VT_BSTR) {
2769 bstrv = left;
2770 nonbv = right;
2771 nonbvt = rvt;
2772 } else {
2773 swap = 1;
2774 bstrv = right;
2775 nonbv = left;
2776 nonbvt = lvt;
2779 /* BSTR and EMPTY: ignore VT_RESERVED */
2780 if (nonbvt == VT_EMPTY)
2781 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2782 else {
2783 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2784 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2786 if (!breserv && !nreserv)
2787 /* No VT_RESERVED set ==> BSTR always greater */
2788 rc = VARCMP_GT;
2789 else if (breserv && !nreserv) {
2790 /* BSTR has VT_RESERVED set. Do a string comparison */
2791 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2792 if (FAILED(rc))
2793 return rc;
2794 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2795 VariantClear(&rv);
2796 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2797 /* Non NULL nor empty BSTR */
2798 /* If the BSTR is not a number the BSTR is greater */
2799 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2800 if (FAILED(rc))
2801 rc = VARCMP_GT;
2802 else if (breserv && nreserv)
2803 /* FIXME: This is strange: with both VT_RESERVED set it
2804 looks like the result depends only on the sign of
2805 the BSTR number */
2806 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2807 else
2808 /* Numeric comparison, will be handled below.
2809 VARCMP_NULL used only to break out. */
2810 rc = VARCMP_NULL;
2811 VariantClear(&lv);
2812 VariantClear(&rv);
2813 } else
2814 /* Empty or NULL BSTR */
2815 rc = VARCMP_GT;
2817 /* Fixup the return code if we swapped left and right */
2818 if (swap) {
2819 if (rc == VARCMP_GT)
2820 rc = VARCMP_LT;
2821 else if (rc == VARCMP_LT)
2822 rc = VARCMP_GT;
2824 if (rc != VARCMP_NULL)
2825 return rc;
2828 if (xmask & VTBIT_DECIMAL)
2829 vt = VT_DECIMAL;
2830 else if (xmask & VTBIT_BSTR)
2831 vt = VT_R8;
2832 else if (xmask & VTBIT_R4)
2833 vt = VT_R4;
2834 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2835 vt = VT_R8;
2836 else if (xmask & VTBIT_CY)
2837 vt = VT_CY;
2838 else
2839 /* default to I8 */
2840 vt = VT_I8;
2842 /* Coerce the variants */
2843 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2844 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2845 /* Overflow, change to R8 */
2846 vt = VT_R8;
2847 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2849 if (FAILED(rc))
2850 return rc;
2851 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2852 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2853 /* Overflow, change to R8 */
2854 vt = VT_R8;
2855 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2856 if (FAILED(rc))
2857 return rc;
2858 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2860 if (FAILED(rc))
2861 return rc;
2863 #define _VARCMP(a,b) \
2864 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2866 switch (vt) {
2867 case VT_CY:
2868 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2869 case VT_DECIMAL:
2870 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2871 case VT_I8:
2872 return _VARCMP(V_I8(&lv), V_I8(&rv));
2873 case VT_R4:
2874 return _VARCMP(V_R4(&lv), V_R4(&rv));
2875 case VT_R8:
2876 return _VARCMP(V_R8(&lv), V_R8(&rv));
2877 default:
2878 /* We should never get here */
2879 return E_FAIL;
2881 #undef _VARCMP
2884 /**********************************************************************
2885 * VarAnd [OLEAUT32.142]
2887 * Computes the logical AND of two variants.
2889 * PARAMS
2890 * left [I] First variant
2891 * right [I] Second variant
2892 * result [O] Result variant
2894 * RETURNS
2895 * Success: S_OK.
2896 * Failure: An HRESULT error code indicating the error.
2898 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2900 HRESULT hres = S_OK;
2901 VARTYPE resvt = VT_EMPTY;
2902 VARTYPE leftvt,rightvt;
2903 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2904 VARIANT varLeft, varRight;
2905 VARIANT tempLeft, tempRight;
2907 VariantInit(&varLeft);
2908 VariantInit(&varRight);
2909 VariantInit(&tempLeft);
2910 VariantInit(&tempRight);
2912 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
2914 /* Handle VT_DISPATCH by storing and taking address of returned value */
2915 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2917 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2918 if (FAILED(hres)) goto VarAnd_Exit;
2919 left = &tempLeft;
2921 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2923 hres = VARIANT_FetchDispatchValue(right, &tempRight);
2924 if (FAILED(hres)) goto VarAnd_Exit;
2925 right = &tempRight;
2928 leftvt = V_VT(left)&VT_TYPEMASK;
2929 rightvt = V_VT(right)&VT_TYPEMASK;
2930 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
2931 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2933 if (leftExtraFlags != rightExtraFlags)
2935 hres = DISP_E_BADVARTYPE;
2936 goto VarAnd_Exit;
2938 ExtraFlags = leftExtraFlags;
2940 /* Native VarAnd always returns an error when using extra
2941 * flags or if the variant combination is I8 and INT.
2943 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
2944 (leftvt == VT_INT && rightvt == VT_I8) ||
2945 ExtraFlags != 0)
2947 hres = DISP_E_BADVARTYPE;
2948 goto VarAnd_Exit;
2951 /* Determine return type */
2952 else if (leftvt == VT_I8 || rightvt == VT_I8)
2953 resvt = VT_I8;
2954 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
2955 leftvt == VT_UINT || rightvt == VT_UINT ||
2956 leftvt == VT_INT || rightvt == VT_INT ||
2957 leftvt == VT_R4 || rightvt == VT_R4 ||
2958 leftvt == VT_R8 || rightvt == VT_R8 ||
2959 leftvt == VT_CY || rightvt == VT_CY ||
2960 leftvt == VT_DATE || rightvt == VT_DATE ||
2961 leftvt == VT_I1 || rightvt == VT_I1 ||
2962 leftvt == VT_UI2 || rightvt == VT_UI2 ||
2963 leftvt == VT_UI4 || rightvt == VT_UI4 ||
2964 leftvt == VT_UI8 || rightvt == VT_UI8 ||
2965 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
2966 resvt = VT_I4;
2967 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
2968 leftvt == VT_I2 || rightvt == VT_I2 ||
2969 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
2970 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
2971 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
2972 (leftvt == VT_UI1 && rightvt == VT_UI1))
2973 resvt = VT_UI1;
2974 else
2975 resvt = VT_I2;
2976 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
2977 (leftvt == VT_BSTR && rightvt == VT_BSTR))
2978 resvt = VT_BOOL;
2979 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
2980 leftvt == VT_BSTR || rightvt == VT_BSTR)
2981 resvt = VT_NULL;
2982 else
2984 hres = DISP_E_BADVARTYPE;
2985 goto VarAnd_Exit;
2988 if (leftvt == VT_NULL || rightvt == VT_NULL)
2991 * Special cases for when left variant is VT_NULL
2992 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
2994 if (leftvt == VT_NULL)
2996 VARIANT_BOOL b;
2997 switch(rightvt)
2999 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3000 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3001 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3002 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3003 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3004 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3005 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3006 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3007 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3008 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3009 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3010 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3011 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3012 case VT_CY:
3013 if(V_CY(right).int64)
3014 resvt = VT_NULL;
3015 break;
3016 case VT_DECIMAL:
3017 if (DEC_HI32(&V_DECIMAL(right)) ||
3018 DEC_LO64(&V_DECIMAL(right)))
3019 resvt = VT_NULL;
3020 break;
3021 case VT_BSTR:
3022 hres = VarBoolFromStr(V_BSTR(right),
3023 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3024 if (FAILED(hres))
3025 return hres;
3026 else if (b)
3027 V_VT(result) = VT_NULL;
3028 else
3030 V_VT(result) = VT_BOOL;
3031 V_BOOL(result) = b;
3033 goto VarAnd_Exit;
3036 V_VT(result) = resvt;
3037 goto VarAnd_Exit;
3040 hres = VariantCopy(&varLeft, left);
3041 if (FAILED(hres)) goto VarAnd_Exit;
3043 hres = VariantCopy(&varRight, right);
3044 if (FAILED(hres)) goto VarAnd_Exit;
3046 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3047 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3048 else
3050 double d;
3052 if (V_VT(&varLeft) == VT_BSTR &&
3053 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3054 LOCALE_USER_DEFAULT, 0, &d)))
3055 hres = VariantChangeType(&varLeft,&varLeft,
3056 VARIANT_LOCALBOOL, VT_BOOL);
3057 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3058 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3059 if (FAILED(hres)) goto VarAnd_Exit;
3062 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3063 V_VT(&varRight) = VT_I4; /* Don't overflow */
3064 else
3066 double d;
3068 if (V_VT(&varRight) == VT_BSTR &&
3069 FAILED(VarR8FromStr(V_BSTR(&varRight),
3070 LOCALE_USER_DEFAULT, 0, &d)))
3071 hres = VariantChangeType(&varRight, &varRight,
3072 VARIANT_LOCALBOOL, VT_BOOL);
3073 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3074 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3075 if (FAILED(hres)) goto VarAnd_Exit;
3078 V_VT(result) = resvt;
3079 switch(resvt)
3081 case VT_I8:
3082 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3083 break;
3084 case VT_I4:
3085 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3086 break;
3087 case VT_I2:
3088 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3089 break;
3090 case VT_UI1:
3091 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3092 break;
3093 case VT_BOOL:
3094 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3095 break;
3096 default:
3097 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3098 leftvt,rightvt);
3101 VarAnd_Exit:
3102 VariantClear(&varLeft);
3103 VariantClear(&varRight);
3104 VariantClear(&tempLeft);
3105 VariantClear(&tempRight);
3107 return hres;
3110 /**********************************************************************
3111 * VarAdd [OLEAUT32.141]
3113 * Add two variants.
3115 * PARAMS
3116 * left [I] First variant
3117 * right [I] Second variant
3118 * result [O] Result variant
3120 * RETURNS
3121 * Success: S_OK.
3122 * Failure: An HRESULT error code indicating the error.
3124 * NOTES
3125 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3126 * UI8, INT and UINT as input variants.
3128 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3129 * same here.
3131 * FIXME
3132 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3133 * case.
3135 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3137 HRESULT hres;
3138 VARTYPE lvt, rvt, resvt, tvt;
3139 VARIANT lv, rv, tv;
3140 VARIANT tempLeft, tempRight;
3141 double r8res;
3143 /* Variant priority for coercion. Sorted from lowest to highest.
3144 VT_ERROR shows an invalid input variant type. */
3145 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3146 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3147 vt_ERROR };
3148 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3149 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3150 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3151 VT_NULL, VT_ERROR };
3153 /* Mapping for coercion from input variant to priority of result variant. */
3154 static const VARTYPE coerce[] = {
3155 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3156 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3157 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3158 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3159 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3160 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3161 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3162 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3165 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3167 VariantInit(&lv);
3168 VariantInit(&rv);
3169 VariantInit(&tv);
3170 VariantInit(&tempLeft);
3171 VariantInit(&tempRight);
3173 /* Handle VT_DISPATCH by storing and taking address of returned value */
3174 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3176 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3178 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3179 if (FAILED(hres)) goto end;
3180 left = &tempLeft;
3182 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3184 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3185 if (FAILED(hres)) goto end;
3186 right = &tempRight;
3190 lvt = V_VT(left)&VT_TYPEMASK;
3191 rvt = V_VT(right)&VT_TYPEMASK;
3193 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3194 Same for any input variant type > VT_I8 */
3195 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3196 lvt > VT_I8 || rvt > VT_I8) {
3197 hres = DISP_E_BADVARTYPE;
3198 goto end;
3201 /* Determine the variant type to coerce to. */
3202 if (coerce[lvt] > coerce[rvt]) {
3203 resvt = prio2vt[coerce[lvt]];
3204 tvt = prio2vt[coerce[rvt]];
3205 } else {
3206 resvt = prio2vt[coerce[rvt]];
3207 tvt = prio2vt[coerce[lvt]];
3210 /* Special cases where the result variant type is defined by both
3211 input variants and not only that with the highest priority */
3212 if (resvt == VT_BSTR) {
3213 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3214 resvt = VT_BSTR;
3215 else
3216 resvt = VT_R8;
3218 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3219 resvt = VT_R8;
3221 /* For overflow detection use the biggest compatible type for the
3222 addition */
3223 switch (resvt) {
3224 case VT_ERROR:
3225 hres = DISP_E_BADVARTYPE;
3226 goto end;
3227 case VT_NULL:
3228 hres = S_OK;
3229 V_VT(result) = VT_NULL;
3230 goto end;
3231 case VT_DISPATCH:
3232 FIXME("cannot handle variant type VT_DISPATCH\n");
3233 hres = DISP_E_TYPEMISMATCH;
3234 goto end;
3235 case VT_EMPTY:
3236 resvt = VT_I2;
3237 /* Fall through */
3238 case VT_UI1:
3239 case VT_I2:
3240 case VT_I4:
3241 case VT_I8:
3242 tvt = VT_I8;
3243 break;
3244 case VT_DATE:
3245 case VT_R4:
3246 tvt = VT_R8;
3247 break;
3248 default:
3249 tvt = resvt;
3252 /* Now coerce the variants */
3253 hres = VariantChangeType(&lv, left, 0, tvt);
3254 if (FAILED(hres))
3255 goto end;
3256 hres = VariantChangeType(&rv, right, 0, tvt);
3257 if (FAILED(hres))
3258 goto end;
3260 /* Do the math */
3261 hres = S_OK;
3262 V_VT(result) = resvt;
3263 switch (tvt) {
3264 case VT_DECIMAL:
3265 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3266 &V_DECIMAL(result));
3267 goto end;
3268 case VT_CY:
3269 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3270 goto end;
3271 case VT_BSTR:
3272 /* We do not add those, we concatenate them. */
3273 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3274 goto end;
3275 case VT_I8:
3276 /* Overflow detection */
3277 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3278 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3279 V_VT(result) = VT_R8;
3280 V_R8(result) = r8res;
3281 goto end;
3282 } else {
3283 V_VT(&tv) = tvt;
3284 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3286 break;
3287 case VT_R8:
3288 V_VT(&tv) = tvt;
3289 /* FIXME: overflow detection */
3290 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3291 break;
3292 default:
3293 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3294 break;
3296 if (resvt != tvt) {
3297 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3298 /* Overflow! Change to the vartype with the next higher priority.
3299 With one exception: I4 ==> R8 even if it would fit in I8 */
3300 if (resvt == VT_I4)
3301 resvt = VT_R8;
3302 else
3303 resvt = prio2vt[coerce[resvt] + 1];
3304 hres = VariantChangeType(result, &tv, 0, resvt);
3306 } else
3307 hres = VariantCopy(result, &tv);
3309 end:
3310 if (hres != S_OK) {
3311 V_VT(result) = VT_EMPTY;
3312 V_I4(result) = 0; /* No V_EMPTY */
3314 VariantClear(&lv);
3315 VariantClear(&rv);
3316 VariantClear(&tv);
3317 VariantClear(&tempLeft);
3318 VariantClear(&tempRight);
3319 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3320 return hres;
3323 /**********************************************************************
3324 * VarMul [OLEAUT32.156]
3326 * Multiply two variants.
3328 * PARAMS
3329 * left [I] First variant
3330 * right [I] Second variant
3331 * result [O] Result variant
3333 * RETURNS
3334 * Success: S_OK.
3335 * Failure: An HRESULT error code indicating the error.
3337 * NOTES
3338 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3339 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3341 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3342 * same here.
3344 * FIXME
3345 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3346 * case.
3348 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3350 HRESULT hres;
3351 VARTYPE lvt, rvt, resvt, tvt;
3352 VARIANT lv, rv, tv;
3353 VARIANT tempLeft, tempRight;
3354 double r8res;
3356 /* Variant priority for coercion. Sorted from lowest to highest.
3357 VT_ERROR shows an invalid input variant type. */
3358 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3359 vt_DECIMAL, vt_NULL, vt_ERROR };
3360 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3361 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3362 VT_DECIMAL, VT_NULL, VT_ERROR };
3364 /* Mapping for coercion from input variant to priority of result variant. */
3365 static const VARTYPE coerce[] = {
3366 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3367 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3368 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3369 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3370 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3371 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3372 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3373 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3376 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3378 VariantInit(&lv);
3379 VariantInit(&rv);
3380 VariantInit(&tv);
3381 VariantInit(&tempLeft);
3382 VariantInit(&tempRight);
3384 /* Handle VT_DISPATCH by storing and taking address of returned value */
3385 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3387 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3388 if (FAILED(hres)) goto end;
3389 left = &tempLeft;
3391 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3393 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3394 if (FAILED(hres)) goto end;
3395 right = &tempRight;
3398 lvt = V_VT(left)&VT_TYPEMASK;
3399 rvt = V_VT(right)&VT_TYPEMASK;
3401 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3402 Same for any input variant type > VT_I8 */
3403 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3404 lvt > VT_I8 || rvt > VT_I8) {
3405 hres = DISP_E_BADVARTYPE;
3406 goto end;
3409 /* Determine the variant type to coerce to. */
3410 if (coerce[lvt] > coerce[rvt]) {
3411 resvt = prio2vt[coerce[lvt]];
3412 tvt = prio2vt[coerce[rvt]];
3413 } else {
3414 resvt = prio2vt[coerce[rvt]];
3415 tvt = prio2vt[coerce[lvt]];
3418 /* Special cases where the result variant type is defined by both
3419 input variants and not only that with the highest priority */
3420 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3421 resvt = VT_R8;
3422 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3423 resvt = VT_I2;
3425 /* For overflow detection use the biggest compatible type for the
3426 multiplication */
3427 switch (resvt) {
3428 case VT_ERROR:
3429 hres = DISP_E_BADVARTYPE;
3430 goto end;
3431 case VT_NULL:
3432 hres = S_OK;
3433 V_VT(result) = VT_NULL;
3434 goto end;
3435 case VT_UI1:
3436 case VT_I2:
3437 case VT_I4:
3438 case VT_I8:
3439 tvt = VT_I8;
3440 break;
3441 case VT_R4:
3442 tvt = VT_R8;
3443 break;
3444 default:
3445 tvt = resvt;
3448 /* Now coerce the variants */
3449 hres = VariantChangeType(&lv, left, 0, tvt);
3450 if (FAILED(hres))
3451 goto end;
3452 hres = VariantChangeType(&rv, right, 0, tvt);
3453 if (FAILED(hres))
3454 goto end;
3456 /* Do the math */
3457 hres = S_OK;
3458 V_VT(&tv) = tvt;
3459 V_VT(result) = resvt;
3460 switch (tvt) {
3461 case VT_DECIMAL:
3462 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3463 &V_DECIMAL(result));
3464 goto end;
3465 case VT_CY:
3466 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3467 goto end;
3468 case VT_I8:
3469 /* Overflow detection */
3470 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3471 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3472 V_VT(result) = VT_R8;
3473 V_R8(result) = r8res;
3474 goto end;
3475 } else
3476 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3477 break;
3478 case VT_R8:
3479 /* FIXME: overflow detection */
3480 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3481 break;
3482 default:
3483 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3484 break;
3486 if (resvt != tvt) {
3487 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3488 /* Overflow! Change to the vartype with the next higher priority.
3489 With one exception: I4 ==> R8 even if it would fit in I8 */
3490 if (resvt == VT_I4)
3491 resvt = VT_R8;
3492 else
3493 resvt = prio2vt[coerce[resvt] + 1];
3495 } else
3496 hres = VariantCopy(result, &tv);
3498 end:
3499 if (hres != S_OK) {
3500 V_VT(result) = VT_EMPTY;
3501 V_I4(result) = 0; /* No V_EMPTY */
3503 VariantClear(&lv);
3504 VariantClear(&rv);
3505 VariantClear(&tv);
3506 VariantClear(&tempLeft);
3507 VariantClear(&tempRight);
3508 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3509 return hres;
3512 /**********************************************************************
3513 * VarDiv [OLEAUT32.143]
3515 * Divides one variant with another.
3517 * PARAMS
3518 * left [I] First variant
3519 * right [I] Second variant
3520 * result [O] Result variant
3522 * RETURNS
3523 * Success: S_OK.
3524 * Failure: An HRESULT error code indicating the error.
3526 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3528 HRESULT hres = S_OK;
3529 VARTYPE resvt = VT_EMPTY;
3530 VARTYPE leftvt,rightvt;
3531 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3532 VARIANT lv,rv;
3533 VARIANT tempLeft, tempRight;
3535 VariantInit(&tempLeft);
3536 VariantInit(&tempRight);
3537 VariantInit(&lv);
3538 VariantInit(&rv);
3540 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3542 /* Handle VT_DISPATCH by storing and taking address of returned value */
3543 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3545 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3546 if (FAILED(hres)) goto end;
3547 left = &tempLeft;
3549 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3551 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3552 if (FAILED(hres)) goto end;
3553 right = &tempRight;
3556 leftvt = V_VT(left)&VT_TYPEMASK;
3557 rightvt = V_VT(right)&VT_TYPEMASK;
3558 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3559 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3561 if (leftExtraFlags != rightExtraFlags)
3563 hres = DISP_E_BADVARTYPE;
3564 goto end;
3566 ExtraFlags = leftExtraFlags;
3568 /* Native VarDiv always returns an error when using extra flags */
3569 if (ExtraFlags != 0)
3571 hres = DISP_E_BADVARTYPE;
3572 goto end;
3575 /* Determine return type */
3576 if (rightvt != VT_EMPTY)
3578 if (leftvt == VT_NULL || rightvt == VT_NULL)
3580 V_VT(result) = VT_NULL;
3581 hres = S_OK;
3582 goto end;
3584 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3585 resvt = VT_DECIMAL;
3586 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3587 leftvt == VT_CY || rightvt == VT_CY ||
3588 leftvt == VT_DATE || rightvt == VT_DATE ||
3589 leftvt == VT_I4 || rightvt == VT_I4 ||
3590 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3591 leftvt == VT_I2 || rightvt == VT_I2 ||
3592 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3593 leftvt == VT_R8 || rightvt == VT_R8 ||
3594 leftvt == VT_UI1 || rightvt == VT_UI1)
3596 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3597 (leftvt == VT_R4 && rightvt == VT_UI1))
3598 resvt = VT_R4;
3599 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3600 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3601 (leftvt == VT_BOOL || leftvt == VT_I2)))
3602 resvt = VT_R4;
3603 else
3604 resvt = VT_R8;
3606 else if (leftvt == VT_R4 || rightvt == VT_R4)
3607 resvt = VT_R4;
3609 else if (leftvt == VT_NULL)
3611 V_VT(result) = VT_NULL;
3612 hres = S_OK;
3613 goto end;
3615 else
3617 hres = DISP_E_BADVARTYPE;
3618 goto end;
3621 /* coerce to the result type */
3622 hres = VariantChangeType(&lv, left, 0, resvt);
3623 if (hres != S_OK) goto end;
3625 hres = VariantChangeType(&rv, right, 0, resvt);
3626 if (hres != S_OK) goto end;
3628 /* do the math */
3629 V_VT(result) = resvt;
3630 switch (resvt)
3632 case VT_R4:
3633 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3635 hres = DISP_E_OVERFLOW;
3636 V_VT(result) = VT_EMPTY;
3638 else if (V_R4(&rv) == 0.0)
3640 hres = DISP_E_DIVBYZERO;
3641 V_VT(result) = VT_EMPTY;
3643 else
3644 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3645 break;
3646 case VT_R8:
3647 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3649 hres = DISP_E_OVERFLOW;
3650 V_VT(result) = VT_EMPTY;
3652 else if (V_R8(&rv) == 0.0)
3654 hres = DISP_E_DIVBYZERO;
3655 V_VT(result) = VT_EMPTY;
3657 else
3658 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3659 break;
3660 case VT_DECIMAL:
3661 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3662 break;
3665 end:
3666 VariantClear(&lv);
3667 VariantClear(&rv);
3668 VariantClear(&tempLeft);
3669 VariantClear(&tempRight);
3670 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3671 return hres;
3674 /**********************************************************************
3675 * VarSub [OLEAUT32.159]
3677 * Subtract two variants.
3679 * PARAMS
3680 * left [I] First variant
3681 * right [I] Second variant
3682 * result [O] Result variant
3684 * RETURNS
3685 * Success: S_OK.
3686 * Failure: An HRESULT error code indicating the error.
3688 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3690 HRESULT hres = S_OK;
3691 VARTYPE resvt = VT_EMPTY;
3692 VARTYPE leftvt,rightvt;
3693 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3694 VARIANT lv,rv;
3695 VARIANT tempLeft, tempRight;
3697 VariantInit(&lv);
3698 VariantInit(&rv);
3699 VariantInit(&tempLeft);
3700 VariantInit(&tempRight);
3702 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3704 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3705 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3706 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3708 if (NULL == V_DISPATCH(left)) {
3709 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3710 hres = DISP_E_BADVARTYPE;
3711 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3712 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3713 hres = DISP_E_BADVARTYPE;
3714 else switch (V_VT(right) & VT_TYPEMASK)
3716 case VT_VARIANT:
3717 case VT_UNKNOWN:
3718 case 15:
3719 case VT_I1:
3720 case VT_UI2:
3721 case VT_UI4:
3722 hres = DISP_E_BADVARTYPE;
3724 if (FAILED(hres)) goto end;
3726 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3727 if (FAILED(hres)) goto end;
3728 left = &tempLeft;
3730 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3731 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3732 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3734 if (NULL == V_DISPATCH(right))
3736 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3737 hres = DISP_E_BADVARTYPE;
3738 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3739 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3740 hres = DISP_E_BADVARTYPE;
3741 else switch (V_VT(left) & VT_TYPEMASK)
3743 case VT_VARIANT:
3744 case VT_UNKNOWN:
3745 case 15:
3746 case VT_I1:
3747 case VT_UI2:
3748 case VT_UI4:
3749 hres = DISP_E_BADVARTYPE;
3751 if (FAILED(hres)) goto end;
3753 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3754 if (FAILED(hres)) goto end;
3755 right = &tempRight;
3758 leftvt = V_VT(left)&VT_TYPEMASK;
3759 rightvt = V_VT(right)&VT_TYPEMASK;
3760 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3761 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3763 if (leftExtraFlags != rightExtraFlags)
3765 hres = DISP_E_BADVARTYPE;
3766 goto end;
3768 ExtraFlags = leftExtraFlags;
3770 /* determine return type and return code */
3771 /* All extra flags produce errors */
3772 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3773 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3774 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3775 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3776 ExtraFlags == VT_VECTOR ||
3777 ExtraFlags == VT_BYREF ||
3778 ExtraFlags == VT_RESERVED)
3780 hres = DISP_E_BADVARTYPE;
3781 goto end;
3783 else if (ExtraFlags >= VT_ARRAY)
3785 hres = DISP_E_TYPEMISMATCH;
3786 goto end;
3788 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3789 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3790 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3791 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3792 leftvt == VT_I1 || rightvt == VT_I1 ||
3793 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3794 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3795 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3796 leftvt == VT_INT || rightvt == VT_INT ||
3797 leftvt == VT_UINT || rightvt == VT_UINT ||
3798 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3799 leftvt == VT_RECORD || rightvt == VT_RECORD)
3801 if (leftvt == VT_RECORD && rightvt == VT_I8)
3802 hres = DISP_E_TYPEMISMATCH;
3803 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3804 hres = DISP_E_TYPEMISMATCH;
3805 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3806 hres = DISP_E_TYPEMISMATCH;
3807 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3808 hres = DISP_E_TYPEMISMATCH;
3809 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3810 hres = DISP_E_BADVARTYPE;
3811 else
3812 hres = DISP_E_BADVARTYPE;
3813 goto end;
3815 /* The following flags/types are invalid for left variant */
3816 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3817 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3818 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3820 hres = DISP_E_BADVARTYPE;
3821 goto end;
3823 /* The following flags/types are invalid for right variant */
3824 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3825 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3826 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3828 hres = DISP_E_BADVARTYPE;
3829 goto end;
3831 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3832 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3833 resvt = VT_NULL;
3834 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3835 leftvt == VT_ERROR || rightvt == VT_ERROR)
3837 hres = DISP_E_TYPEMISMATCH;
3838 goto end;
3840 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3841 resvt = VT_NULL;
3842 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3843 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3844 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3845 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3846 resvt = VT_R8;
3847 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3848 resvt = VT_DECIMAL;
3849 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3850 resvt = VT_DATE;
3851 else if (leftvt == VT_CY || rightvt == VT_CY)
3852 resvt = VT_CY;
3853 else if (leftvt == VT_R8 || rightvt == VT_R8)
3854 resvt = VT_R8;
3855 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3856 resvt = VT_R8;
3857 else if (leftvt == VT_R4 || rightvt == VT_R4)
3859 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3860 leftvt == VT_I8 || rightvt == VT_I8)
3861 resvt = VT_R8;
3862 else
3863 resvt = VT_R4;
3865 else if (leftvt == VT_I8 || rightvt == VT_I8)
3866 resvt = VT_I8;
3867 else if (leftvt == VT_I4 || rightvt == VT_I4)
3868 resvt = VT_I4;
3869 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3870 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3871 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3872 resvt = VT_I2;
3873 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3874 resvt = VT_UI1;
3875 else
3877 hres = DISP_E_TYPEMISMATCH;
3878 goto end;
3881 /* coerce to the result type */
3882 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3883 hres = VariantChangeType(&lv, left, 0, VT_R8);
3884 else
3885 hres = VariantChangeType(&lv, left, 0, resvt);
3886 if (hres != S_OK) goto end;
3887 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3888 hres = VariantChangeType(&rv, right, 0, VT_R8);
3889 else
3890 hres = VariantChangeType(&rv, right, 0, resvt);
3891 if (hres != S_OK) goto end;
3893 /* do the math */
3894 V_VT(result) = resvt;
3895 switch (resvt)
3897 case VT_NULL:
3898 break;
3899 case VT_DATE:
3900 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3901 break;
3902 case VT_CY:
3903 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3904 break;
3905 case VT_R4:
3906 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3907 break;
3908 case VT_I8:
3909 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3910 break;
3911 case VT_I4:
3912 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3913 break;
3914 case VT_I2:
3915 V_I2(result) = V_I2(&lv) - V_I2(&rv);
3916 break;
3917 case VT_UI1:
3918 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
3919 break;
3920 case VT_R8:
3921 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3922 break;
3923 case VT_DECIMAL:
3924 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3925 break;
3928 end:
3929 VariantClear(&lv);
3930 VariantClear(&rv);
3931 VariantClear(&tempLeft);
3932 VariantClear(&tempRight);
3933 TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3934 return hres;
3938 /**********************************************************************
3939 * VarOr [OLEAUT32.157]
3941 * Perform a logical or (OR) operation on two variants.
3943 * PARAMS
3944 * pVarLeft [I] First variant
3945 * pVarRight [I] Variant to OR with pVarLeft
3946 * pVarOut [O] Destination for OR result
3948 * RETURNS
3949 * Success: S_OK. pVarOut contains the result of the operation with its type
3950 * taken from the table listed under VarXor().
3951 * Failure: An HRESULT error code indicating the error.
3953 * NOTES
3954 * See the Notes section of VarXor() for further information.
3956 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3958 VARTYPE vt = VT_I4;
3959 VARIANT varLeft, varRight, varStr;
3960 HRESULT hRet;
3961 VARIANT tempLeft, tempRight;
3963 VariantInit(&tempLeft);
3964 VariantInit(&tempRight);
3965 VariantInit(&varLeft);
3966 VariantInit(&varRight);
3967 VariantInit(&varStr);
3969 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
3971 /* Handle VT_DISPATCH by storing and taking address of returned value */
3972 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
3974 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
3975 if (FAILED(hRet)) goto VarOr_Exit;
3976 pVarLeft = &tempLeft;
3978 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
3980 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
3981 if (FAILED(hRet)) goto VarOr_Exit;
3982 pVarRight = &tempRight;
3985 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3986 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3987 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3988 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3990 hRet = DISP_E_BADVARTYPE;
3991 goto VarOr_Exit;
3994 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
3996 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3998 /* NULL OR Zero is NULL, NULL OR value is value */
3999 if (V_VT(pVarLeft) == VT_NULL)
4000 pVarLeft = pVarRight; /* point to the non-NULL var */
4002 V_VT(pVarOut) = VT_NULL;
4003 V_I4(pVarOut) = 0;
4005 switch (V_VT(pVarLeft))
4007 case VT_DATE: case VT_R8:
4008 if (V_R8(pVarLeft))
4009 goto VarOr_AsEmpty;
4010 hRet = S_OK;
4011 goto VarOr_Exit;
4012 case VT_BOOL:
4013 if (V_BOOL(pVarLeft))
4014 *pVarOut = *pVarLeft;
4015 hRet = S_OK;
4016 goto VarOr_Exit;
4017 case VT_I2: case VT_UI2:
4018 if (V_I2(pVarLeft))
4019 goto VarOr_AsEmpty;
4020 hRet = S_OK;
4021 goto VarOr_Exit;
4022 case VT_I1:
4023 if (V_I1(pVarLeft))
4024 goto VarOr_AsEmpty;
4025 hRet = S_OK;
4026 goto VarOr_Exit;
4027 case VT_UI1:
4028 if (V_UI1(pVarLeft))
4029 *pVarOut = *pVarLeft;
4030 hRet = S_OK;
4031 goto VarOr_Exit;
4032 case VT_R4:
4033 if (V_R4(pVarLeft))
4034 goto VarOr_AsEmpty;
4035 hRet = S_OK;
4036 goto VarOr_Exit;
4037 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4038 if (V_I4(pVarLeft))
4039 goto VarOr_AsEmpty;
4040 hRet = S_OK;
4041 goto VarOr_Exit;
4042 case VT_CY:
4043 if (V_CY(pVarLeft).int64)
4044 goto VarOr_AsEmpty;
4045 hRet = S_OK;
4046 goto VarOr_Exit;
4047 case VT_I8: case VT_UI8:
4048 if (V_I8(pVarLeft))
4049 goto VarOr_AsEmpty;
4050 hRet = S_OK;
4051 goto VarOr_Exit;
4052 case VT_DECIMAL:
4053 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4054 goto VarOr_AsEmpty;
4055 hRet = S_OK;
4056 goto VarOr_Exit;
4057 case VT_BSTR:
4059 VARIANT_BOOL b;
4061 if (!V_BSTR(pVarLeft))
4063 hRet = DISP_E_BADVARTYPE;
4064 goto VarOr_Exit;
4067 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4068 if (SUCCEEDED(hRet) && b)
4070 V_VT(pVarOut) = VT_BOOL;
4071 V_BOOL(pVarOut) = b;
4073 goto VarOr_Exit;
4075 case VT_NULL: case VT_EMPTY:
4076 V_VT(pVarOut) = VT_NULL;
4077 hRet = S_OK;
4078 goto VarOr_Exit;
4079 default:
4080 hRet = DISP_E_BADVARTYPE;
4081 goto VarOr_Exit;
4085 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4087 if (V_VT(pVarLeft) == VT_EMPTY)
4088 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4090 VarOr_AsEmpty:
4091 /* Since one argument is empty (0), OR'ing it with the other simply
4092 * gives the others value (as 0|x => x). So just convert the other
4093 * argument to the required result type.
4095 switch (V_VT(pVarLeft))
4097 case VT_BSTR:
4098 if (!V_BSTR(pVarLeft))
4100 hRet = DISP_E_BADVARTYPE;
4101 goto VarOr_Exit;
4104 hRet = VariantCopy(&varStr, pVarLeft);
4105 if (FAILED(hRet))
4106 goto VarOr_Exit;
4107 pVarLeft = &varStr;
4108 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4109 if (FAILED(hRet))
4110 goto VarOr_Exit;
4111 /* Fall Through ... */
4112 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4113 V_VT(pVarOut) = VT_I2;
4114 break;
4115 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4116 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4117 case VT_INT: case VT_UINT: case VT_UI8:
4118 V_VT(pVarOut) = VT_I4;
4119 break;
4120 case VT_I8:
4121 V_VT(pVarOut) = VT_I8;
4122 break;
4123 default:
4124 hRet = DISP_E_BADVARTYPE;
4125 goto VarOr_Exit;
4127 hRet = VariantCopy(&varLeft, pVarLeft);
4128 if (FAILED(hRet))
4129 goto VarOr_Exit;
4130 pVarLeft = &varLeft;
4131 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4132 goto VarOr_Exit;
4135 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4137 V_VT(pVarOut) = VT_BOOL;
4138 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4139 hRet = S_OK;
4140 goto VarOr_Exit;
4143 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4145 V_VT(pVarOut) = VT_UI1;
4146 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4147 hRet = S_OK;
4148 goto VarOr_Exit;
4151 if (V_VT(pVarLeft) == VT_BSTR)
4153 hRet = VariantCopy(&varStr, pVarLeft);
4154 if (FAILED(hRet))
4155 goto VarOr_Exit;
4156 pVarLeft = &varStr;
4157 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4158 if (FAILED(hRet))
4159 goto VarOr_Exit;
4162 if (V_VT(pVarLeft) == VT_BOOL &&
4163 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4165 vt = VT_BOOL;
4167 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4168 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4169 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4170 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4172 vt = VT_I2;
4174 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4176 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4178 hRet = DISP_E_TYPEMISMATCH;
4179 goto VarOr_Exit;
4181 vt = VT_I8;
4184 hRet = VariantCopy(&varLeft, pVarLeft);
4185 if (FAILED(hRet))
4186 goto VarOr_Exit;
4188 hRet = VariantCopy(&varRight, pVarRight);
4189 if (FAILED(hRet))
4190 goto VarOr_Exit;
4192 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4193 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4194 else
4196 double d;
4198 if (V_VT(&varLeft) == VT_BSTR &&
4199 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4200 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4201 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4202 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4203 if (FAILED(hRet))
4204 goto VarOr_Exit;
4207 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4208 V_VT(&varRight) = VT_I4; /* Don't overflow */
4209 else
4211 double d;
4213 if (V_VT(&varRight) == VT_BSTR &&
4214 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4215 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4216 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4217 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4218 if (FAILED(hRet))
4219 goto VarOr_Exit;
4222 V_VT(pVarOut) = vt;
4223 if (vt == VT_I8)
4225 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4227 else if (vt == VT_I4)
4229 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4231 else
4233 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4236 VarOr_Exit:
4237 VariantClear(&varStr);
4238 VariantClear(&varLeft);
4239 VariantClear(&varRight);
4240 VariantClear(&tempLeft);
4241 VariantClear(&tempRight);
4242 return hRet;
4245 /**********************************************************************
4246 * VarAbs [OLEAUT32.168]
4248 * Convert a variant to its absolute value.
4250 * PARAMS
4251 * pVarIn [I] Source variant
4252 * pVarOut [O] Destination for converted value
4254 * RETURNS
4255 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4256 * Failure: An HRESULT error code indicating the error.
4258 * NOTES
4259 * - This function does not process by-reference variants.
4260 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4261 * according to the following table:
4262 *| Input Type Output Type
4263 *| ---------- -----------
4264 *| VT_BOOL VT_I2
4265 *| VT_BSTR VT_R8
4266 *| (All others) Unchanged
4268 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4270 VARIANT varIn;
4271 HRESULT hRet = S_OK;
4272 VARIANT temp;
4274 VariantInit(&temp);
4276 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4278 /* Handle VT_DISPATCH by storing and taking address of returned value */
4279 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4281 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4282 if (FAILED(hRet)) goto VarAbs_Exit;
4283 pVarIn = &temp;
4286 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4287 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4288 V_VT(pVarIn) == VT_ERROR)
4290 hRet = DISP_E_TYPEMISMATCH;
4291 goto VarAbs_Exit;
4293 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4295 #define ABS_CASE(typ,min) \
4296 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4297 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4298 break
4300 switch (V_VT(pVarIn))
4302 ABS_CASE(I1,I1_MIN);
4303 case VT_BOOL:
4304 V_VT(pVarOut) = VT_I2;
4305 /* BOOL->I2, Fall through ... */
4306 ABS_CASE(I2,I2_MIN);
4307 case VT_INT:
4308 ABS_CASE(I4,I4_MIN);
4309 ABS_CASE(I8,I8_MIN);
4310 ABS_CASE(R4,R4_MIN);
4311 case VT_BSTR:
4312 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4313 if (FAILED(hRet))
4314 break;
4315 V_VT(pVarOut) = VT_R8;
4316 pVarIn = &varIn;
4317 /* Fall through ... */
4318 case VT_DATE:
4319 ABS_CASE(R8,R8_MIN);
4320 case VT_CY:
4321 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4322 break;
4323 case VT_DECIMAL:
4324 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4325 break;
4326 case VT_UI1:
4327 case VT_UI2:
4328 case VT_UINT:
4329 case VT_UI4:
4330 case VT_UI8:
4331 /* No-Op */
4332 break;
4333 case VT_EMPTY:
4334 V_VT(pVarOut) = VT_I2;
4335 case VT_NULL:
4336 V_I2(pVarOut) = 0;
4337 break;
4338 default:
4339 hRet = DISP_E_BADVARTYPE;
4342 VarAbs_Exit:
4343 VariantClear(&temp);
4344 return hRet;
4347 /**********************************************************************
4348 * VarFix [OLEAUT32.169]
4350 * Truncate a variants value to a whole number.
4352 * PARAMS
4353 * pVarIn [I] Source variant
4354 * pVarOut [O] Destination for converted value
4356 * RETURNS
4357 * Success: S_OK. pVarOut contains the converted value.
4358 * Failure: An HRESULT error code indicating the error.
4360 * NOTES
4361 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4362 * according to the following table:
4363 *| Input Type Output Type
4364 *| ---------- -----------
4365 *| VT_BOOL VT_I2
4366 *| VT_EMPTY VT_I2
4367 *| VT_BSTR VT_R8
4368 *| All Others Unchanged
4369 * - The difference between this function and VarInt() is that VarInt() rounds
4370 * negative numbers away from 0, while this function rounds them towards zero.
4372 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4374 HRESULT hRet = S_OK;
4375 VARIANT temp;
4377 VariantInit(&temp);
4379 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4381 /* Handle VT_DISPATCH by storing and taking address of returned value */
4382 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4384 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4385 if (FAILED(hRet)) goto VarFix_Exit;
4386 pVarIn = &temp;
4388 V_VT(pVarOut) = V_VT(pVarIn);
4390 switch (V_VT(pVarIn))
4392 case VT_UI1:
4393 V_UI1(pVarOut) = V_UI1(pVarIn);
4394 break;
4395 case VT_BOOL:
4396 V_VT(pVarOut) = VT_I2;
4397 /* Fall through */
4398 case VT_I2:
4399 V_I2(pVarOut) = V_I2(pVarIn);
4400 break;
4401 case VT_I4:
4402 V_I4(pVarOut) = V_I4(pVarIn);
4403 break;
4404 case VT_I8:
4405 V_I8(pVarOut) = V_I8(pVarIn);
4406 break;
4407 case VT_R4:
4408 if (V_R4(pVarIn) < 0.0f)
4409 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4410 else
4411 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4412 break;
4413 case VT_BSTR:
4414 V_VT(pVarOut) = VT_R8;
4415 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4416 pVarIn = pVarOut;
4417 /* Fall through */
4418 case VT_DATE:
4419 case VT_R8:
4420 if (V_R8(pVarIn) < 0.0)
4421 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4422 else
4423 V_R8(pVarOut) = floor(V_R8(pVarIn));
4424 break;
4425 case VT_CY:
4426 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4427 break;
4428 case VT_DECIMAL:
4429 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4430 break;
4431 case VT_EMPTY:
4432 V_VT(pVarOut) = VT_I2;
4433 V_I2(pVarOut) = 0;
4434 break;
4435 case VT_NULL:
4436 /* No-Op */
4437 break;
4438 default:
4439 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4440 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4441 hRet = DISP_E_BADVARTYPE;
4442 else
4443 hRet = DISP_E_TYPEMISMATCH;
4445 VarFix_Exit:
4446 if (FAILED(hRet))
4447 V_VT(pVarOut) = VT_EMPTY;
4448 VariantClear(&temp);
4450 return hRet;
4453 /**********************************************************************
4454 * VarInt [OLEAUT32.172]
4456 * Truncate a variants value to a whole number.
4458 * PARAMS
4459 * pVarIn [I] Source variant
4460 * pVarOut [O] Destination for converted value
4462 * RETURNS
4463 * Success: S_OK. pVarOut contains the converted value.
4464 * Failure: An HRESULT error code indicating the error.
4466 * NOTES
4467 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4468 * according to the following table:
4469 *| Input Type Output Type
4470 *| ---------- -----------
4471 *| VT_BOOL VT_I2
4472 *| VT_EMPTY VT_I2
4473 *| VT_BSTR VT_R8
4474 *| All Others Unchanged
4475 * - The difference between this function and VarFix() is that VarFix() rounds
4476 * negative numbers towards 0, while this function rounds them away from zero.
4478 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4480 HRESULT hRet = S_OK;
4481 VARIANT temp;
4483 VariantInit(&temp);
4485 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4487 /* Handle VT_DISPATCH by storing and taking address of returned value */
4488 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4490 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4491 if (FAILED(hRet)) goto VarInt_Exit;
4492 pVarIn = &temp;
4494 V_VT(pVarOut) = V_VT(pVarIn);
4496 switch (V_VT(pVarIn))
4498 case VT_R4:
4499 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4500 break;
4501 case VT_BSTR:
4502 V_VT(pVarOut) = VT_R8;
4503 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4504 pVarIn = pVarOut;
4505 /* Fall through */
4506 case VT_DATE:
4507 case VT_R8:
4508 V_R8(pVarOut) = floor(V_R8(pVarIn));
4509 break;
4510 case VT_CY:
4511 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4512 break;
4513 case VT_DECIMAL:
4514 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4515 break;
4516 default:
4517 hRet = VarFix(pVarIn, pVarOut);
4519 VarInt_Exit:
4520 VariantClear(&temp);
4522 return hRet;
4525 /**********************************************************************
4526 * VarXor [OLEAUT32.167]
4528 * Perform a logical exclusive-or (XOR) operation on two variants.
4530 * PARAMS
4531 * pVarLeft [I] First variant
4532 * pVarRight [I] Variant to XOR with pVarLeft
4533 * pVarOut [O] Destination for XOR result
4535 * RETURNS
4536 * Success: S_OK. pVarOut contains the result of the operation with its type
4537 * taken from the table below).
4538 * Failure: An HRESULT error code indicating the error.
4540 * NOTES
4541 * - Neither pVarLeft or pVarRight are modified by this function.
4542 * - This function does not process by-reference variants.
4543 * - Input types of VT_BSTR may be numeric strings or boolean text.
4544 * - The type of result stored in pVarOut depends on the types of pVarLeft
4545 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4546 * or VT_NULL if the function succeeds.
4547 * - Type promotion is inconsistent and as a result certain combinations of
4548 * values will return DISP_E_OVERFLOW even when they could be represented.
4549 * This matches the behaviour of native oleaut32.
4551 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4553 VARTYPE vt;
4554 VARIANT varLeft, varRight;
4555 VARIANT tempLeft, tempRight;
4556 double d;
4557 HRESULT hRet;
4559 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4561 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4562 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4563 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4564 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4565 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4566 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4567 return DISP_E_BADVARTYPE;
4569 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4571 /* NULL XOR anything valid is NULL */
4572 V_VT(pVarOut) = VT_NULL;
4573 return S_OK;
4576 VariantInit(&tempLeft);
4577 VariantInit(&tempRight);
4579 /* Handle VT_DISPATCH by storing and taking address of returned value */
4580 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4582 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4583 if (FAILED(hRet)) goto VarXor_Exit;
4584 pVarLeft = &tempLeft;
4586 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4588 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4589 if (FAILED(hRet)) goto VarXor_Exit;
4590 pVarRight = &tempRight;
4593 /* Copy our inputs so we don't disturb anything */
4594 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4596 hRet = VariantCopy(&varLeft, pVarLeft);
4597 if (FAILED(hRet))
4598 goto VarXor_Exit;
4600 hRet = VariantCopy(&varRight, pVarRight);
4601 if (FAILED(hRet))
4602 goto VarXor_Exit;
4604 /* Try any strings first as numbers, then as VT_BOOL */
4605 if (V_VT(&varLeft) == VT_BSTR)
4607 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4608 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4609 FAILED(hRet) ? VT_BOOL : VT_I4);
4610 if (FAILED(hRet))
4611 goto VarXor_Exit;
4614 if (V_VT(&varRight) == VT_BSTR)
4616 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4617 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4618 FAILED(hRet) ? VT_BOOL : VT_I4);
4619 if (FAILED(hRet))
4620 goto VarXor_Exit;
4623 /* Determine the result type */
4624 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4626 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4628 hRet = DISP_E_TYPEMISMATCH;
4629 goto VarXor_Exit;
4631 vt = VT_I8;
4633 else
4635 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4637 case (VT_BOOL << 16) | VT_BOOL:
4638 vt = VT_BOOL;
4639 break;
4640 case (VT_UI1 << 16) | VT_UI1:
4641 vt = VT_UI1;
4642 break;
4643 case (VT_EMPTY << 16) | VT_EMPTY:
4644 case (VT_EMPTY << 16) | VT_UI1:
4645 case (VT_EMPTY << 16) | VT_I2:
4646 case (VT_EMPTY << 16) | VT_BOOL:
4647 case (VT_UI1 << 16) | VT_EMPTY:
4648 case (VT_UI1 << 16) | VT_I2:
4649 case (VT_UI1 << 16) | VT_BOOL:
4650 case (VT_I2 << 16) | VT_EMPTY:
4651 case (VT_I2 << 16) | VT_UI1:
4652 case (VT_I2 << 16) | VT_I2:
4653 case (VT_I2 << 16) | VT_BOOL:
4654 case (VT_BOOL << 16) | VT_EMPTY:
4655 case (VT_BOOL << 16) | VT_UI1:
4656 case (VT_BOOL << 16) | VT_I2:
4657 vt = VT_I2;
4658 break;
4659 default:
4660 vt = VT_I4;
4661 break;
4665 /* VT_UI4 does not overflow */
4666 if (vt != VT_I8)
4668 if (V_VT(&varLeft) == VT_UI4)
4669 V_VT(&varLeft) = VT_I4;
4670 if (V_VT(&varRight) == VT_UI4)
4671 V_VT(&varRight) = VT_I4;
4674 /* Convert our input copies to the result type */
4675 if (V_VT(&varLeft) != vt)
4676 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4677 if (FAILED(hRet))
4678 goto VarXor_Exit;
4680 if (V_VT(&varRight) != vt)
4681 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4682 if (FAILED(hRet))
4683 goto VarXor_Exit;
4685 V_VT(pVarOut) = vt;
4687 /* Calculate the result */
4688 switch (vt)
4690 case VT_I8:
4691 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4692 break;
4693 case VT_I4:
4694 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4695 break;
4696 case VT_BOOL:
4697 case VT_I2:
4698 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4699 break;
4700 case VT_UI1:
4701 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4702 break;
4705 VarXor_Exit:
4706 VariantClear(&varLeft);
4707 VariantClear(&varRight);
4708 VariantClear(&tempLeft);
4709 VariantClear(&tempRight);
4710 return hRet;
4713 /**********************************************************************
4714 * VarEqv [OLEAUT32.172]
4716 * Determine if two variants contain the same value.
4718 * PARAMS
4719 * pVarLeft [I] First variant to compare
4720 * pVarRight [I] Variant to compare to pVarLeft
4721 * pVarOut [O] Destination for comparison result
4723 * RETURNS
4724 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4725 * if equivalent or non-zero otherwise.
4726 * Failure: An HRESULT error code indicating the error.
4728 * NOTES
4729 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4730 * the result.
4732 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4734 HRESULT hRet;
4736 TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4738 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4739 if (SUCCEEDED(hRet))
4741 if (V_VT(pVarOut) == VT_I8)
4742 V_I8(pVarOut) = ~V_I8(pVarOut);
4743 else
4744 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4746 return hRet;
4749 /**********************************************************************
4750 * VarNeg [OLEAUT32.173]
4752 * Negate the value of a variant.
4754 * PARAMS
4755 * pVarIn [I] Source variant
4756 * pVarOut [O] Destination for converted value
4758 * RETURNS
4759 * Success: S_OK. pVarOut contains the converted value.
4760 * Failure: An HRESULT error code indicating the error.
4762 * NOTES
4763 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4764 * according to the following table:
4765 *| Input Type Output Type
4766 *| ---------- -----------
4767 *| VT_EMPTY VT_I2
4768 *| VT_UI1 VT_I2
4769 *| VT_BOOL VT_I2
4770 *| VT_BSTR VT_R8
4771 *| All Others Unchanged (unless promoted)
4772 * - Where the negated value of a variant does not fit in its base type, the type
4773 * is promoted according to the following table:
4774 *| Input Type Promoted To
4775 *| ---------- -----------
4776 *| VT_I2 VT_I4
4777 *| VT_I4 VT_R8
4778 *| VT_I8 VT_R8
4779 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4780 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4781 * for types which are not valid. Since this is in contravention of the
4782 * meaning of those error codes and unlikely to be relied on by applications,
4783 * this implementation returns errors consistent with the other high level
4784 * variant math functions.
4786 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4788 HRESULT hRet = S_OK;
4789 VARIANT temp;
4791 VariantInit(&temp);
4793 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4795 /* Handle VT_DISPATCH by storing and taking address of returned value */
4796 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4798 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4799 if (FAILED(hRet)) goto VarNeg_Exit;
4800 pVarIn = &temp;
4802 V_VT(pVarOut) = V_VT(pVarIn);
4804 switch (V_VT(pVarIn))
4806 case VT_UI1:
4807 V_VT(pVarOut) = VT_I2;
4808 V_I2(pVarOut) = -V_UI1(pVarIn);
4809 break;
4810 case VT_BOOL:
4811 V_VT(pVarOut) = VT_I2;
4812 /* Fall through */
4813 case VT_I2:
4814 if (V_I2(pVarIn) == I2_MIN)
4816 V_VT(pVarOut) = VT_I4;
4817 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4819 else
4820 V_I2(pVarOut) = -V_I2(pVarIn);
4821 break;
4822 case VT_I4:
4823 if (V_I4(pVarIn) == I4_MIN)
4825 V_VT(pVarOut) = VT_R8;
4826 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4828 else
4829 V_I4(pVarOut) = -V_I4(pVarIn);
4830 break;
4831 case VT_I8:
4832 if (V_I8(pVarIn) == I8_MIN)
4834 V_VT(pVarOut) = VT_R8;
4835 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4836 V_R8(pVarOut) *= -1.0;
4838 else
4839 V_I8(pVarOut) = -V_I8(pVarIn);
4840 break;
4841 case VT_R4:
4842 V_R4(pVarOut) = -V_R4(pVarIn);
4843 break;
4844 case VT_DATE:
4845 case VT_R8:
4846 V_R8(pVarOut) = -V_R8(pVarIn);
4847 break;
4848 case VT_CY:
4849 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4850 break;
4851 case VT_DECIMAL:
4852 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4853 break;
4854 case VT_BSTR:
4855 V_VT(pVarOut) = VT_R8;
4856 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4857 V_R8(pVarOut) = -V_R8(pVarOut);
4858 break;
4859 case VT_EMPTY:
4860 V_VT(pVarOut) = VT_I2;
4861 V_I2(pVarOut) = 0;
4862 break;
4863 case VT_NULL:
4864 /* No-Op */
4865 break;
4866 default:
4867 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4868 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4869 hRet = DISP_E_BADVARTYPE;
4870 else
4871 hRet = DISP_E_TYPEMISMATCH;
4873 VarNeg_Exit:
4874 if (FAILED(hRet))
4875 V_VT(pVarOut) = VT_EMPTY;
4876 VariantClear(&temp);
4878 return hRet;
4881 /**********************************************************************
4882 * VarNot [OLEAUT32.174]
4884 * Perform a not operation on a variant.
4886 * PARAMS
4887 * pVarIn [I] Source variant
4888 * pVarOut [O] Destination for converted value
4890 * RETURNS
4891 * Success: S_OK. pVarOut contains the converted value.
4892 * Failure: An HRESULT error code indicating the error.
4894 * NOTES
4895 * - Strictly speaking, this function performs a bitwise ones complement
4896 * on the variants value (after possibly converting to VT_I4, see below).
4897 * This only behaves like a boolean not operation if the value in
4898 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4899 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4900 * before calling this function.
4901 * - This function does not process by-reference variants.
4902 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4903 * according to the following table:
4904 *| Input Type Output Type
4905 *| ---------- -----------
4906 *| VT_EMPTY VT_I2
4907 *| VT_R4 VT_I4
4908 *| VT_R8 VT_I4
4909 *| VT_BSTR VT_I4
4910 *| VT_DECIMAL VT_I4
4911 *| VT_CY VT_I4
4912 *| (All others) Unchanged
4914 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4916 VARIANT varIn;
4917 HRESULT hRet = S_OK;
4918 VARIANT temp;
4920 VariantInit(&temp);
4922 TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4924 /* Handle VT_DISPATCH by storing and taking address of returned value */
4925 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4927 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4928 if (FAILED(hRet)) goto VarNot_Exit;
4929 pVarIn = &temp;
4932 if (V_VT(pVarIn) == VT_BSTR)
4934 V_VT(&varIn) = VT_R8;
4935 hRet = VarR8FromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn) );
4936 if (FAILED(hRet))
4938 V_VT(&varIn) = VT_BOOL;
4939 hRet = VarBoolFromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &V_BOOL(&varIn) );
4941 if (FAILED(hRet)) goto VarNot_Exit;
4942 pVarIn = &varIn;
4945 V_VT(pVarOut) = V_VT(pVarIn);
4947 switch (V_VT(pVarIn))
4949 case VT_I1:
4950 V_I4(pVarOut) = ~V_I1(pVarIn);
4951 V_VT(pVarOut) = VT_I4;
4952 break;
4953 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4954 case VT_BOOL:
4955 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4956 case VT_UI2:
4957 V_I4(pVarOut) = ~V_UI2(pVarIn);
4958 V_VT(pVarOut) = VT_I4;
4959 break;
4960 case VT_DECIMAL:
4961 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4962 if (FAILED(hRet))
4963 break;
4964 pVarIn = &varIn;
4965 /* Fall through ... */
4966 case VT_INT:
4967 V_VT(pVarOut) = VT_I4;
4968 /* Fall through ... */
4969 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4970 case VT_UINT:
4971 case VT_UI4:
4972 V_I4(pVarOut) = ~V_UI4(pVarIn);
4973 V_VT(pVarOut) = VT_I4;
4974 break;
4975 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
4976 case VT_UI8:
4977 V_I4(pVarOut) = ~V_UI8(pVarIn);
4978 V_VT(pVarOut) = VT_I4;
4979 break;
4980 case VT_R4:
4981 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
4982 V_I4(pVarOut) = ~V_I4(pVarOut);
4983 V_VT(pVarOut) = VT_I4;
4984 break;
4985 case VT_DATE:
4986 case VT_R8:
4987 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4988 V_I4(pVarOut) = ~V_I4(pVarOut);
4989 V_VT(pVarOut) = VT_I4;
4990 break;
4991 case VT_CY:
4992 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4993 V_I4(pVarOut) = ~V_I4(pVarOut);
4994 V_VT(pVarOut) = VT_I4;
4995 break;
4996 case VT_EMPTY:
4997 V_I2(pVarOut) = ~0;
4998 V_VT(pVarOut) = VT_I2;
4999 break;
5000 case VT_NULL:
5001 /* No-Op */
5002 break;
5003 default:
5004 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5005 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5006 hRet = DISP_E_BADVARTYPE;
5007 else
5008 hRet = DISP_E_TYPEMISMATCH;
5010 VarNot_Exit:
5011 if (FAILED(hRet))
5012 V_VT(pVarOut) = VT_EMPTY;
5013 VariantClear(&temp);
5015 return hRet;
5018 /**********************************************************************
5019 * VarRound [OLEAUT32.175]
5021 * Perform a round operation on a variant.
5023 * PARAMS
5024 * pVarIn [I] Source variant
5025 * deci [I] Number of decimals to round to
5026 * pVarOut [O] Destination for converted value
5028 * RETURNS
5029 * Success: S_OK. pVarOut contains the converted value.
5030 * Failure: An HRESULT error code indicating the error.
5032 * NOTES
5033 * - Floating point values are rounded to the desired number of decimals.
5034 * - Some integer types are just copied to the return variable.
5035 * - Some other integer types are not handled and fail.
5037 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5039 VARIANT varIn;
5040 HRESULT hRet = S_OK;
5041 float factor;
5042 VARIANT temp;
5044 VariantInit(&temp);
5046 TRACE("(%s,%d)\n", debugstr_variant(pVarIn), deci);
5048 /* Handle VT_DISPATCH by storing and taking address of returned value */
5049 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5051 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5052 if (FAILED(hRet)) goto VarRound_Exit;
5053 pVarIn = &temp;
5056 switch (V_VT(pVarIn))
5058 /* cases that fail on windows */
5059 case VT_I1:
5060 case VT_I8:
5061 case VT_UI2:
5062 case VT_UI4:
5063 hRet = DISP_E_BADVARTYPE;
5064 break;
5066 /* cases just copying in to out */
5067 case VT_UI1:
5068 V_VT(pVarOut) = V_VT(pVarIn);
5069 V_UI1(pVarOut) = V_UI1(pVarIn);
5070 break;
5071 case VT_I2:
5072 V_VT(pVarOut) = V_VT(pVarIn);
5073 V_I2(pVarOut) = V_I2(pVarIn);
5074 break;
5075 case VT_I4:
5076 V_VT(pVarOut) = V_VT(pVarIn);
5077 V_I4(pVarOut) = V_I4(pVarIn);
5078 break;
5079 case VT_NULL:
5080 V_VT(pVarOut) = V_VT(pVarIn);
5081 /* value unchanged */
5082 break;
5084 /* cases that change type */
5085 case VT_EMPTY:
5086 V_VT(pVarOut) = VT_I2;
5087 V_I2(pVarOut) = 0;
5088 break;
5089 case VT_BOOL:
5090 V_VT(pVarOut) = VT_I2;
5091 V_I2(pVarOut) = V_BOOL(pVarIn);
5092 break;
5093 case VT_BSTR:
5094 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5095 if (FAILED(hRet))
5096 break;
5097 V_VT(&varIn)=VT_R8;
5098 pVarIn = &varIn;
5099 /* Fall through ... */
5101 /* cases we need to do math */
5102 case VT_R8:
5103 if (V_R8(pVarIn)>0) {
5104 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5105 } else {
5106 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5108 V_VT(pVarOut) = V_VT(pVarIn);
5109 break;
5110 case VT_R4:
5111 if (V_R4(pVarIn)>0) {
5112 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5113 } else {
5114 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5116 V_VT(pVarOut) = V_VT(pVarIn);
5117 break;
5118 case VT_DATE:
5119 if (V_DATE(pVarIn)>0) {
5120 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5121 } else {
5122 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5124 V_VT(pVarOut) = V_VT(pVarIn);
5125 break;
5126 case VT_CY:
5127 if (deci>3)
5128 factor=1;
5129 else
5130 factor=pow(10, 4-deci);
5132 if (V_CY(pVarIn).int64>0) {
5133 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5134 } else {
5135 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5137 V_VT(pVarOut) = V_VT(pVarIn);
5138 break;
5139 case VT_DECIMAL:
5141 double dbl;
5143 hRet = VarR8FromDec(&V_DECIMAL(pVarIn), &dbl);
5144 if (FAILED(hRet))
5145 break;
5147 if (dbl>0.0f)
5148 dbl = floor(dbl*pow(10,deci)+0.5);
5149 else
5150 dbl = ceil(dbl*pow(10,deci)-0.5);
5152 V_VT(pVarOut)=VT_DECIMAL;
5153 hRet = VarDecFromR8(dbl, &V_DECIMAL(pVarOut));
5154 break;
5156 /* cases we don't know yet */
5157 default:
5158 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5159 V_VT(pVarIn) & VT_TYPEMASK, deci);
5160 hRet = DISP_E_BADVARTYPE;
5162 VarRound_Exit:
5163 if (FAILED(hRet))
5164 V_VT(pVarOut) = VT_EMPTY;
5165 VariantClear(&temp);
5167 TRACE("returning 0x%08x %s\n", hRet, debugstr_variant(pVarOut));
5168 return hRet;
5171 /**********************************************************************
5172 * VarIdiv [OLEAUT32.153]
5174 * Converts input variants to integers and divides them.
5176 * PARAMS
5177 * left [I] Left hand variant
5178 * right [I] Right hand variant
5179 * result [O] Destination for quotient
5181 * RETURNS
5182 * Success: S_OK. result contains the quotient.
5183 * Failure: An HRESULT error code indicating the error.
5185 * NOTES
5186 * If either expression is null, null is returned, as per MSDN
5188 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5190 HRESULT hres = S_OK;
5191 VARTYPE resvt = VT_EMPTY;
5192 VARTYPE leftvt,rightvt;
5193 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5194 VARIANT lv,rv;
5195 VARIANT tempLeft, tempRight;
5197 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5199 VariantInit(&lv);
5200 VariantInit(&rv);
5201 VariantInit(&tempLeft);
5202 VariantInit(&tempRight);
5204 leftvt = V_VT(left)&VT_TYPEMASK;
5205 rightvt = V_VT(right)&VT_TYPEMASK;
5206 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5207 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5209 if (leftExtraFlags != rightExtraFlags)
5211 hres = DISP_E_BADVARTYPE;
5212 goto end;
5214 ExtraFlags = leftExtraFlags;
5216 /* Native VarIdiv always returns an error when using extra
5217 * flags or if the variant combination is I8 and INT.
5219 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5220 (leftvt == VT_INT && rightvt == VT_I8) ||
5221 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5222 ExtraFlags != 0)
5224 hres = DISP_E_BADVARTYPE;
5225 goto end;
5228 /* Determine variant type */
5229 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5231 V_VT(result) = VT_NULL;
5232 hres = S_OK;
5233 goto end;
5235 else if (leftvt == VT_I8 || rightvt == VT_I8)
5236 resvt = VT_I8;
5237 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5238 leftvt == VT_INT || rightvt == VT_INT ||
5239 leftvt == VT_UINT || rightvt == VT_UINT ||
5240 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5241 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5242 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5243 leftvt == VT_I1 || rightvt == VT_I1 ||
5244 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5245 leftvt == VT_DATE || rightvt == VT_DATE ||
5246 leftvt == VT_CY || rightvt == VT_CY ||
5247 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5248 leftvt == VT_R8 || rightvt == VT_R8 ||
5249 leftvt == VT_R4 || rightvt == VT_R4)
5250 resvt = VT_I4;
5251 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5252 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5253 leftvt == VT_EMPTY)
5254 resvt = VT_I2;
5255 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5256 resvt = VT_UI1;
5257 else
5259 hres = DISP_E_BADVARTYPE;
5260 goto end;
5263 /* coerce to the result type */
5264 hres = VariantChangeType(&lv, left, 0, resvt);
5265 if (hres != S_OK) goto end;
5266 hres = VariantChangeType(&rv, right, 0, resvt);
5267 if (hres != S_OK) goto end;
5269 /* do the math */
5270 V_VT(result) = resvt;
5271 switch (resvt)
5273 case VT_UI1:
5274 if (V_UI1(&rv) == 0)
5276 hres = DISP_E_DIVBYZERO;
5277 V_VT(result) = VT_EMPTY;
5279 else
5280 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5281 break;
5282 case VT_I2:
5283 if (V_I2(&rv) == 0)
5285 hres = DISP_E_DIVBYZERO;
5286 V_VT(result) = VT_EMPTY;
5288 else
5289 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5290 break;
5291 case VT_I4:
5292 if (V_I4(&rv) == 0)
5294 hres = DISP_E_DIVBYZERO;
5295 V_VT(result) = VT_EMPTY;
5297 else
5298 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5299 break;
5300 case VT_I8:
5301 if (V_I8(&rv) == 0)
5303 hres = DISP_E_DIVBYZERO;
5304 V_VT(result) = VT_EMPTY;
5306 else
5307 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5308 break;
5309 default:
5310 FIXME("Couldn't integer divide variant types %d,%d\n",
5311 leftvt,rightvt);
5314 end:
5315 VariantClear(&lv);
5316 VariantClear(&rv);
5317 VariantClear(&tempLeft);
5318 VariantClear(&tempRight);
5320 return hres;
5324 /**********************************************************************
5325 * VarMod [OLEAUT32.155]
5327 * Perform the modulus operation of the right hand variant on the left
5329 * PARAMS
5330 * left [I] Left hand variant
5331 * right [I] Right hand variant
5332 * result [O] Destination for converted value
5334 * RETURNS
5335 * Success: S_OK. result contains the remainder.
5336 * Failure: An HRESULT error code indicating the error.
5338 * NOTE:
5339 * If an error occurs the type of result will be modified but the value will not be.
5340 * Doesn't support arrays or any special flags yet.
5342 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5344 BOOL lOk = TRUE;
5345 HRESULT rc = E_FAIL;
5346 int resT = 0;
5347 VARIANT lv,rv;
5348 VARIANT tempLeft, tempRight;
5350 VariantInit(&tempLeft);
5351 VariantInit(&tempRight);
5352 VariantInit(&lv);
5353 VariantInit(&rv);
5355 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5357 /* Handle VT_DISPATCH by storing and taking address of returned value */
5358 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5360 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5361 if (FAILED(rc)) goto end;
5362 left = &tempLeft;
5364 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5366 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5367 if (FAILED(rc)) goto end;
5368 right = &tempRight;
5371 /* check for invalid inputs */
5372 lOk = TRUE;
5373 switch (V_VT(left) & VT_TYPEMASK) {
5374 case VT_BOOL :
5375 case VT_I1 :
5376 case VT_I2 :
5377 case VT_I4 :
5378 case VT_I8 :
5379 case VT_INT :
5380 case VT_UI1 :
5381 case VT_UI2 :
5382 case VT_UI4 :
5383 case VT_UI8 :
5384 case VT_UINT :
5385 case VT_R4 :
5386 case VT_R8 :
5387 case VT_CY :
5388 case VT_EMPTY:
5389 case VT_DATE :
5390 case VT_BSTR :
5391 case VT_DECIMAL:
5392 break;
5393 case VT_VARIANT:
5394 case VT_UNKNOWN:
5395 V_VT(result) = VT_EMPTY;
5396 rc = DISP_E_TYPEMISMATCH;
5397 goto end;
5398 case VT_ERROR:
5399 rc = DISP_E_TYPEMISMATCH;
5400 goto end;
5401 case VT_RECORD:
5402 V_VT(result) = VT_EMPTY;
5403 rc = DISP_E_TYPEMISMATCH;
5404 goto end;
5405 case VT_NULL:
5406 break;
5407 default:
5408 V_VT(result) = VT_EMPTY;
5409 rc = DISP_E_BADVARTYPE;
5410 goto end;
5414 switch (V_VT(right) & VT_TYPEMASK) {
5415 case VT_BOOL :
5416 case VT_I1 :
5417 case VT_I2 :
5418 case VT_I4 :
5419 case VT_I8 :
5420 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5422 V_VT(result) = VT_EMPTY;
5423 rc = DISP_E_TYPEMISMATCH;
5424 goto end;
5426 case VT_INT :
5427 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5429 V_VT(result) = VT_EMPTY;
5430 rc = DISP_E_TYPEMISMATCH;
5431 goto end;
5433 case VT_UI1 :
5434 case VT_UI2 :
5435 case VT_UI4 :
5436 case VT_UI8 :
5437 case VT_UINT :
5438 case VT_R4 :
5439 case VT_R8 :
5440 case VT_CY :
5441 if(V_VT(left) == VT_EMPTY)
5443 V_VT(result) = VT_I4;
5444 rc = S_OK;
5445 goto end;
5447 case VT_EMPTY:
5448 case VT_DATE :
5449 case VT_DECIMAL:
5450 if(V_VT(left) == VT_ERROR)
5452 V_VT(result) = VT_EMPTY;
5453 rc = DISP_E_TYPEMISMATCH;
5454 goto end;
5456 case VT_BSTR:
5457 if(V_VT(left) == VT_NULL)
5459 V_VT(result) = VT_NULL;
5460 rc = S_OK;
5461 goto end;
5463 break;
5465 case VT_VOID:
5466 V_VT(result) = VT_EMPTY;
5467 rc = DISP_E_BADVARTYPE;
5468 goto end;
5469 case VT_NULL:
5470 if(V_VT(left) == VT_VOID)
5472 V_VT(result) = VT_EMPTY;
5473 rc = DISP_E_BADVARTYPE;
5474 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5475 lOk)
5477 V_VT(result) = VT_NULL;
5478 rc = S_OK;
5479 } else
5481 V_VT(result) = VT_NULL;
5482 rc = DISP_E_BADVARTYPE;
5484 goto end;
5485 case VT_VARIANT:
5486 case VT_UNKNOWN:
5487 V_VT(result) = VT_EMPTY;
5488 rc = DISP_E_TYPEMISMATCH;
5489 goto end;
5490 case VT_ERROR:
5491 rc = DISP_E_TYPEMISMATCH;
5492 goto end;
5493 case VT_RECORD:
5494 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5496 V_VT(result) = VT_EMPTY;
5497 rc = DISP_E_BADVARTYPE;
5498 } else
5500 V_VT(result) = VT_EMPTY;
5501 rc = DISP_E_TYPEMISMATCH;
5503 goto end;
5504 default:
5505 V_VT(result) = VT_EMPTY;
5506 rc = DISP_E_BADVARTYPE;
5507 goto end;
5510 /* determine the result type */
5511 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5512 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5513 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5514 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5515 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5516 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5517 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5518 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5519 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5520 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5521 else resT = VT_I4; /* most outputs are I4 */
5523 /* convert to I8 for the modulo */
5524 rc = VariantChangeType(&lv, left, 0, VT_I8);
5525 if(FAILED(rc))
5527 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5528 goto end;
5531 rc = VariantChangeType(&rv, right, 0, VT_I8);
5532 if(FAILED(rc))
5534 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5535 goto end;
5538 /* if right is zero set VT_EMPTY and return divide by zero */
5539 if(V_I8(&rv) == 0)
5541 V_VT(result) = VT_EMPTY;
5542 rc = DISP_E_DIVBYZERO;
5543 goto end;
5546 /* perform the modulo operation */
5547 V_VT(result) = VT_I8;
5548 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5550 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5551 wine_dbgstr_longlong(V_I8(&lv)), wine_dbgstr_longlong(V_I8(&rv)),
5552 wine_dbgstr_longlong(V_I8(result)));
5554 /* convert left and right to the destination type */
5555 rc = VariantChangeType(result, result, 0, resT);
5556 if(FAILED(rc))
5558 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5559 /* fall to end of function */
5562 end:
5563 VariantClear(&lv);
5564 VariantClear(&rv);
5565 VariantClear(&tempLeft);
5566 VariantClear(&tempRight);
5567 return rc;
5570 /**********************************************************************
5571 * VarPow [OLEAUT32.158]
5573 * Computes the power of one variant to another variant.
5575 * PARAMS
5576 * left [I] First variant
5577 * right [I] Second variant
5578 * result [O] Result variant
5580 * RETURNS
5581 * Success: S_OK.
5582 * Failure: An HRESULT error code indicating the error.
5584 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5586 HRESULT hr = S_OK;
5587 VARIANT dl,dr;
5588 VARTYPE resvt = VT_EMPTY;
5589 VARTYPE leftvt,rightvt;
5590 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5591 VARIANT tempLeft, tempRight;
5593 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5595 VariantInit(&dl);
5596 VariantInit(&dr);
5597 VariantInit(&tempLeft);
5598 VariantInit(&tempRight);
5600 /* Handle VT_DISPATCH by storing and taking address of returned value */
5601 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5603 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5604 if (FAILED(hr)) goto end;
5605 left = &tempLeft;
5607 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5609 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5610 if (FAILED(hr)) goto end;
5611 right = &tempRight;
5614 leftvt = V_VT(left)&VT_TYPEMASK;
5615 rightvt = V_VT(right)&VT_TYPEMASK;
5616 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5617 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5619 if (leftExtraFlags != rightExtraFlags)
5621 hr = DISP_E_BADVARTYPE;
5622 goto end;
5624 ExtraFlags = leftExtraFlags;
5626 /* Native VarPow always returns an error when using extra flags */
5627 if (ExtraFlags != 0)
5629 hr = DISP_E_BADVARTYPE;
5630 goto end;
5633 /* Determine return type */
5634 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5635 V_VT(result) = VT_NULL;
5636 hr = S_OK;
5637 goto end;
5639 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5640 leftvt == VT_I4 || leftvt == VT_R4 ||
5641 leftvt == VT_R8 || leftvt == VT_CY ||
5642 leftvt == VT_DATE || leftvt == VT_BSTR ||
5643 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5644 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5645 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5646 rightvt == VT_I4 || rightvt == VT_R4 ||
5647 rightvt == VT_R8 || rightvt == VT_CY ||
5648 rightvt == VT_DATE || rightvt == VT_BSTR ||
5649 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5650 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5651 resvt = VT_R8;
5652 else
5654 hr = DISP_E_BADVARTYPE;
5655 goto end;
5658 hr = VariantChangeType(&dl,left,0,resvt);
5659 if (FAILED(hr)) {
5660 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5661 hr = E_FAIL;
5662 goto end;
5665 hr = VariantChangeType(&dr,right,0,resvt);
5666 if (FAILED(hr)) {
5667 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5668 hr = E_FAIL;
5669 goto end;
5672 V_VT(result) = VT_R8;
5673 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5675 end:
5676 VariantClear(&dl);
5677 VariantClear(&dr);
5678 VariantClear(&tempLeft);
5679 VariantClear(&tempRight);
5681 return hr;
5684 /**********************************************************************
5685 * VarImp [OLEAUT32.154]
5687 * Bitwise implication of two variants.
5689 * PARAMS
5690 * left [I] First variant
5691 * right [I] Second variant
5692 * result [O] Result variant
5694 * RETURNS
5695 * Success: S_OK.
5696 * Failure: An HRESULT error code indicating the error.
5698 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5700 HRESULT hres = S_OK;
5701 VARTYPE resvt = VT_EMPTY;
5702 VARTYPE leftvt,rightvt;
5703 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5704 VARIANT lv,rv;
5705 double d;
5706 VARIANT tempLeft, tempRight;
5708 VariantInit(&lv);
5709 VariantInit(&rv);
5710 VariantInit(&tempLeft);
5711 VariantInit(&tempRight);
5713 TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5715 /* Handle VT_DISPATCH by storing and taking address of returned value */
5716 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5718 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5719 if (FAILED(hres)) goto VarImp_Exit;
5720 left = &tempLeft;
5722 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5724 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5725 if (FAILED(hres)) goto VarImp_Exit;
5726 right = &tempRight;
5729 leftvt = V_VT(left)&VT_TYPEMASK;
5730 rightvt = V_VT(right)&VT_TYPEMASK;
5731 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5732 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5734 if (leftExtraFlags != rightExtraFlags)
5736 hres = DISP_E_BADVARTYPE;
5737 goto VarImp_Exit;
5739 ExtraFlags = leftExtraFlags;
5741 /* Native VarImp always returns an error when using extra
5742 * flags or if the variants are I8 and INT.
5744 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5745 ExtraFlags != 0)
5747 hres = DISP_E_BADVARTYPE;
5748 goto VarImp_Exit;
5751 /* Determine result type */
5752 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5753 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5755 V_VT(result) = VT_NULL;
5756 hres = S_OK;
5757 goto VarImp_Exit;
5759 else if (leftvt == VT_I8 || rightvt == VT_I8)
5760 resvt = VT_I8;
5761 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5762 leftvt == VT_INT || rightvt == VT_INT ||
5763 leftvt == VT_UINT || rightvt == VT_UINT ||
5764 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5765 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5766 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5767 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5768 leftvt == VT_DATE || rightvt == VT_DATE ||
5769 leftvt == VT_CY || rightvt == VT_CY ||
5770 leftvt == VT_R8 || rightvt == VT_R8 ||
5771 leftvt == VT_R4 || rightvt == VT_R4 ||
5772 leftvt == VT_I1 || rightvt == VT_I1)
5773 resvt = VT_I4;
5774 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5775 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5776 (leftvt == VT_NULL && rightvt == VT_UI1))
5777 resvt = VT_UI1;
5778 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5779 leftvt == VT_I2 || rightvt == VT_I2 ||
5780 leftvt == VT_UI1 || rightvt == VT_UI1)
5781 resvt = VT_I2;
5782 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5783 leftvt == VT_BSTR || rightvt == VT_BSTR)
5784 resvt = VT_BOOL;
5786 /* VT_NULL requires special handling for when the opposite
5787 * variant is equal to something other than -1.
5788 * (NULL Imp 0 = NULL, NULL Imp n = n)
5790 if (leftvt == VT_NULL)
5792 VARIANT_BOOL b;
5793 switch(rightvt)
5795 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5796 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5797 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5798 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5799 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5800 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5801 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5802 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5803 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5804 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5805 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5806 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5807 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5808 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5809 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5810 case VT_DECIMAL:
5811 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5812 resvt = VT_NULL;
5813 break;
5814 case VT_BSTR:
5815 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5816 if (FAILED(hres)) goto VarImp_Exit;
5817 else if (!b)
5818 V_VT(result) = VT_NULL;
5819 else
5821 V_VT(result) = VT_BOOL;
5822 V_BOOL(result) = b;
5824 goto VarImp_Exit;
5826 if (resvt == VT_NULL)
5828 V_VT(result) = resvt;
5829 goto VarImp_Exit;
5831 else
5833 hres = VariantChangeType(result,right,0,resvt);
5834 goto VarImp_Exit;
5838 /* Special handling is required when NULL is the right variant.
5839 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5841 else if (rightvt == VT_NULL)
5843 VARIANT_BOOL b;
5844 switch(leftvt)
5846 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5847 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5848 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5849 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5850 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5851 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5852 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5853 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5854 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5855 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5856 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5857 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5858 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5859 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5860 case VT_DECIMAL:
5861 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5862 resvt = VT_NULL;
5863 break;
5864 case VT_BSTR:
5865 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5866 if (FAILED(hres)) goto VarImp_Exit;
5867 else if (b == VARIANT_TRUE)
5868 resvt = VT_NULL;
5870 if (resvt == VT_NULL)
5872 V_VT(result) = resvt;
5873 goto VarImp_Exit;
5877 hres = VariantCopy(&lv, left);
5878 if (FAILED(hres)) goto VarImp_Exit;
5880 if (rightvt == VT_NULL)
5882 memset( &rv, 0, sizeof(rv) );
5883 V_VT(&rv) = resvt;
5885 else
5887 hres = VariantCopy(&rv, right);
5888 if (FAILED(hres)) goto VarImp_Exit;
5891 if (V_VT(&lv) == VT_BSTR &&
5892 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5893 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5894 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5895 hres = VariantChangeType(&lv,&lv,0,resvt);
5896 if (FAILED(hres)) goto VarImp_Exit;
5898 if (V_VT(&rv) == VT_BSTR &&
5899 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5900 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5901 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5902 hres = VariantChangeType(&rv, &rv, 0, resvt);
5903 if (FAILED(hres)) goto VarImp_Exit;
5905 /* do the math */
5906 V_VT(result) = resvt;
5907 switch (resvt)
5909 case VT_I8:
5910 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5911 break;
5912 case VT_I4:
5913 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5914 break;
5915 case VT_I2:
5916 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
5917 break;
5918 case VT_UI1:
5919 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
5920 break;
5921 case VT_BOOL:
5922 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
5923 break;
5924 default:
5925 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5926 leftvt,rightvt);
5929 VarImp_Exit:
5931 VariantClear(&lv);
5932 VariantClear(&rv);
5933 VariantClear(&tempLeft);
5934 VariantClear(&tempRight);
5936 return hres;