kernel32/tests: Remove a redundant check for ERROR_CALL_NOT_IMPLEMENTED.
[wine/hacks.git] / dlls / oleaut32 / variant.c
blob26a19bbbe8551b7e370210a7901b198b93585772
1 /*
2 * VARIANT
4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
7 * Copyright 2006 Google (Benjamin Arai)
9 * The algorithm for conversion from Julian days to day/month/year is based on
10 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
11 * Copyright 1994-7 Regents of the University of California
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "config.h"
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wine/unicode.h"
41 #include "winerror.h"
42 #include "variant.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(variant);
47 const char * const wine_vtypes[VT_CLSID+1] =
49 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
50 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
51 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
52 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
53 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
54 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
55 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
56 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
57 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
60 const char * const wine_vflags[16] =
62 "",
63 "|VT_VECTOR",
64 "|VT_ARRAY",
65 "|VT_VECTOR|VT_ARRAY",
66 "|VT_BYREF",
67 "|VT_VECTOR|VT_ARRAY",
68 "|VT_ARRAY|VT_BYREF",
69 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
70 "|VT_HARDTYPE",
71 "|VT_VECTOR|VT_HARDTYPE",
72 "|VT_ARRAY|VT_HARDTYPE",
73 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
74 "|VT_BYREF|VT_HARDTYPE",
75 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
76 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
77 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
80 /* Convert a variant from one type to another */
81 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
82 VARIANTARG* ps, VARTYPE vt)
84 HRESULT res = DISP_E_TYPEMISMATCH;
85 VARTYPE vtFrom = V_TYPE(ps);
86 DWORD dwFlags = 0;
88 TRACE("(%p->(%s%s),0x%08x,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
89 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
90 debugstr_vt(vt), debugstr_vf(vt));
92 if (vt == VT_BSTR || vtFrom == VT_BSTR)
94 /* All flags passed to low level function are only used for
95 * changing to or from strings. Map these here.
97 if (wFlags & VARIANT_LOCALBOOL)
98 dwFlags |= VAR_LOCALBOOL;
99 if (wFlags & VARIANT_CALENDAR_HIJRI)
100 dwFlags |= VAR_CALENDAR_HIJRI;
101 if (wFlags & VARIANT_CALENDAR_THAI)
102 dwFlags |= VAR_CALENDAR_THAI;
103 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
104 dwFlags |= VAR_CALENDAR_GREGORIAN;
105 if (wFlags & VARIANT_NOUSEROVERRIDE)
106 dwFlags |= LOCALE_NOUSEROVERRIDE;
107 if (wFlags & VARIANT_USE_NLS)
108 dwFlags |= LOCALE_USE_NLS;
111 /* Map int/uint to i4/ui4 */
112 if (vt == VT_INT)
113 vt = VT_I4;
114 else if (vt == VT_UINT)
115 vt = VT_UI4;
117 if (vtFrom == VT_INT)
118 vtFrom = VT_I4;
119 else if (vtFrom == VT_UINT)
120 vtFrom = VT_UI4;
122 if (vt == vtFrom)
123 return VariantCopy(pd, ps);
125 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
127 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
128 * accessing the default object property.
130 return DISP_E_TYPEMISMATCH;
133 switch (vt)
135 case VT_EMPTY:
136 if (vtFrom == VT_NULL)
137 return DISP_E_TYPEMISMATCH;
138 /* ... Fall through */
139 case VT_NULL:
140 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
142 res = VariantClear( pd );
143 if (vt == VT_NULL && SUCCEEDED(res))
144 V_VT(pd) = VT_NULL;
146 return res;
148 case VT_I1:
149 switch (vtFrom)
151 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
152 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
153 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
154 case VT_UI1: V_I1(pd) = V_UI1(ps); return S_OK;
155 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
156 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
157 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
158 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
159 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
160 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
161 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
162 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
163 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
164 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
165 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
166 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
168 break;
170 case VT_I2:
171 switch (vtFrom)
173 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
174 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
175 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
176 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
177 case VT_UI2: V_I2(pd) = V_UI2(ps); return S_OK;
178 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
179 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
180 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
181 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
182 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
183 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
184 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
185 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
186 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
187 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
188 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
190 break;
192 case VT_I4:
193 switch (vtFrom)
195 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
196 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
197 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
198 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
199 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
200 case VT_UI4: V_I4(pd) = V_UI4(ps); return S_OK;
201 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
202 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
203 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
204 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
205 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
206 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
207 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
208 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
209 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
210 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
212 break;
214 case VT_UI1:
215 switch (vtFrom)
217 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
218 case VT_I1: V_UI1(pd) = V_I1(ps); return S_OK;
219 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
220 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
221 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
222 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
223 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
224 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
225 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
226 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
227 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
228 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
229 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
230 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
231 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
232 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
234 break;
236 case VT_UI2:
237 switch (vtFrom)
239 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
240 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
241 case VT_I2: V_UI2(pd) = V_I2(ps); return S_OK;
242 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
243 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
244 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
245 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
246 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
247 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
248 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
249 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
250 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
251 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
252 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
253 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
254 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
256 break;
258 case VT_UI4:
259 switch (vtFrom)
261 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
262 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
263 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
264 case VT_I4: V_UI4(pd) = V_I4(ps); return S_OK;
265 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
266 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
267 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
268 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
269 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
270 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
271 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
272 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
273 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
274 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
275 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
276 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
278 break;
280 case VT_UI8:
281 switch (vtFrom)
283 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
284 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
285 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
286 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
287 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
288 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
289 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
290 case VT_I8: V_UI8(pd) = V_I8(ps); return S_OK;
291 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
292 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
293 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
294 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
295 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
296 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
297 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
298 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
300 break;
302 case VT_I8:
303 switch (vtFrom)
305 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
306 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
307 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
308 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
309 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
310 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
311 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
312 case VT_UI8: V_I8(pd) = V_UI8(ps); return S_OK;
313 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
314 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
315 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
316 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
317 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
318 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
319 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
320 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
322 break;
324 case VT_R4:
325 switch (vtFrom)
327 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
328 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
329 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
330 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
331 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
332 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
333 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
334 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
335 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
336 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
337 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
338 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
339 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
340 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
341 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
342 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
344 break;
346 case VT_R8:
347 switch (vtFrom)
349 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
350 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
351 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
352 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
353 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
354 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
355 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
356 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
357 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
358 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
359 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
360 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
361 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
362 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
363 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
364 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
366 break;
368 case VT_DATE:
369 switch (vtFrom)
371 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
372 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
373 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
374 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
375 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
376 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
377 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
378 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
379 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
380 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
381 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
382 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
383 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
384 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
385 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
386 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
388 break;
390 case VT_BOOL:
391 switch (vtFrom)
393 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
394 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
395 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
396 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
397 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
398 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
399 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
400 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
401 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
402 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
403 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
404 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
405 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
406 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
407 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
408 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
410 break;
412 case VT_BSTR:
413 switch (vtFrom)
415 case VT_EMPTY:
416 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
417 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
418 case VT_BOOL:
419 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
420 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
421 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
422 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
423 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
424 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
425 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
426 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
427 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
428 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
429 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
430 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd));
437 break;
439 case VT_CY:
440 switch (vtFrom)
442 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
443 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
444 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
445 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
446 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
447 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
448 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
449 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
450 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
451 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
452 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
453 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
454 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
455 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
456 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
457 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
459 break;
461 case VT_DECIMAL:
462 switch (vtFrom)
464 case VT_EMPTY:
465 case VT_BOOL:
466 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
467 DEC_HI32(&V_DECIMAL(pd)) = 0;
468 DEC_MID32(&V_DECIMAL(pd)) = 0;
469 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
470 * VT_NULL and VT_EMPTY always give a 0 value.
472 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
473 return S_OK;
474 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
475 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
476 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
477 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
478 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
479 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
480 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
481 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
482 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
483 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
484 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
485 case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
486 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
487 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
489 break;
491 case VT_UNKNOWN:
492 switch (vtFrom)
494 case VT_DISPATCH:
495 if (V_DISPATCH(ps) == NULL)
496 V_UNKNOWN(pd) = NULL;
497 else
498 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
499 break;
501 break;
503 case VT_DISPATCH:
504 switch (vtFrom)
506 case VT_UNKNOWN:
507 if (V_UNKNOWN(ps) == NULL)
508 V_DISPATCH(pd) = NULL;
509 else
510 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
511 break;
513 break;
515 case VT_RECORD:
516 break;
518 return res;
521 /* Coerce to/from an array */
522 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
524 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
525 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
527 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
528 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
530 if (V_VT(ps) == vt)
531 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
533 return DISP_E_TYPEMISMATCH;
536 /******************************************************************************
537 * Check if a variants type is valid.
539 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
541 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
543 vt &= VT_TYPEMASK;
545 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
547 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
549 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
550 return DISP_E_BADVARTYPE;
551 if (vt != (VARTYPE)15)
552 return S_OK;
555 return DISP_E_BADVARTYPE;
558 /******************************************************************************
559 * VariantInit [OLEAUT32.8]
561 * Initialise a variant.
563 * PARAMS
564 * pVarg [O] Variant to initialise
566 * RETURNS
567 * Nothing.
569 * NOTES
570 * This function simply sets the type of the variant to VT_EMPTY. It does not
571 * free any existing value, use VariantClear() for that.
573 void WINAPI VariantInit(VARIANTARG* pVarg)
575 TRACE("(%p)\n", pVarg);
577 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
580 /******************************************************************************
581 * VariantClear [OLEAUT32.9]
583 * Clear a variant.
585 * PARAMS
586 * pVarg [I/O] Variant to clear
588 * RETURNS
589 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
590 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
592 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
594 HRESULT hres = S_OK;
596 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
598 hres = VARIANT_ValidateType(V_VT(pVarg));
600 if (SUCCEEDED(hres))
602 if (!V_ISBYREF(pVarg))
604 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
606 if (V_ARRAY(pVarg))
607 hres = SafeArrayDestroy(V_ARRAY(pVarg));
609 else if (V_VT(pVarg) == VT_BSTR)
611 SysFreeString(V_BSTR(pVarg));
613 else if (V_VT(pVarg) == VT_RECORD)
615 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
616 if (pBr->pRecInfo)
618 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
619 IRecordInfo_Release(pBr->pRecInfo);
622 else if (V_VT(pVarg) == VT_DISPATCH ||
623 V_VT(pVarg) == VT_UNKNOWN)
625 if (V_UNKNOWN(pVarg))
626 IUnknown_Release(V_UNKNOWN(pVarg));
629 V_VT(pVarg) = VT_EMPTY;
631 return hres;
634 /******************************************************************************
635 * Copy an IRecordInfo object contained in a variant.
637 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
639 HRESULT hres = S_OK;
641 if (pBr->pRecInfo)
643 ULONG ulSize;
645 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
646 if (SUCCEEDED(hres))
648 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
649 if (!pvRecord)
650 hres = E_OUTOFMEMORY;
651 else
653 memcpy(pvRecord, pBr->pvRecord, ulSize);
654 pBr->pvRecord = pvRecord;
656 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
657 if (SUCCEEDED(hres))
658 IRecordInfo_AddRef(pBr->pRecInfo);
662 else if (pBr->pvRecord)
663 hres = E_INVALIDARG;
664 return hres;
667 /******************************************************************************
668 * VariantCopy [OLEAUT32.10]
670 * Copy a variant.
672 * PARAMS
673 * pvargDest [O] Destination for copy
674 * pvargSrc [I] Source variant to copy
676 * RETURNS
677 * Success: S_OK. pvargDest contains a copy of pvargSrc.
678 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
679 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
680 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
681 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
683 * NOTES
684 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
685 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
686 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
687 * fails, so does this function.
688 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
689 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
690 * is copied rather than just any pointers to it.
691 * - For by-value object types the object pointer is copied and the objects
692 * reference count increased using IUnknown_AddRef().
693 * - For all by-reference types, only the referencing pointer is copied.
695 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
697 HRESULT hres = S_OK;
699 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
700 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
701 debugstr_VF(pvargSrc));
703 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
704 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
705 return DISP_E_BADVARTYPE;
707 if (pvargSrc != pvargDest &&
708 SUCCEEDED(hres = VariantClear(pvargDest)))
710 *pvargDest = *pvargSrc; /* Shallow copy the value */
712 if (!V_ISBYREF(pvargSrc))
714 if (V_ISARRAY(pvargSrc))
716 if (V_ARRAY(pvargSrc))
717 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
719 else if (V_VT(pvargSrc) == VT_BSTR)
721 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
722 if (!V_BSTR(pvargDest))
724 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
725 hres = E_OUTOFMEMORY;
728 else if (V_VT(pvargSrc) == VT_RECORD)
730 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
732 else if (V_VT(pvargSrc) == VT_DISPATCH ||
733 V_VT(pvargSrc) == VT_UNKNOWN)
735 if (V_UNKNOWN(pvargSrc))
736 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
740 return hres;
743 /* Return the byte size of a variants data */
744 static inline size_t VARIANT_DataSize(const VARIANT* pv)
746 switch (V_TYPE(pv))
748 case VT_I1:
749 case VT_UI1: return sizeof(BYTE);
750 case VT_I2:
751 case VT_UI2: return sizeof(SHORT);
752 case VT_INT:
753 case VT_UINT:
754 case VT_I4:
755 case VT_UI4: return sizeof(LONG);
756 case VT_I8:
757 case VT_UI8: return sizeof(LONGLONG);
758 case VT_R4: return sizeof(float);
759 case VT_R8: return sizeof(double);
760 case VT_DATE: return sizeof(DATE);
761 case VT_BOOL: return sizeof(VARIANT_BOOL);
762 case VT_DISPATCH:
763 case VT_UNKNOWN:
764 case VT_BSTR: return sizeof(void*);
765 case VT_CY: return sizeof(CY);
766 case VT_ERROR: return sizeof(SCODE);
768 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
769 return 0;
772 /******************************************************************************
773 * VariantCopyInd [OLEAUT32.11]
775 * Copy a variant, dereferencing it if it is by-reference.
777 * PARAMS
778 * pvargDest [O] Destination for copy
779 * pvargSrc [I] Source variant to copy
781 * RETURNS
782 * Success: S_OK. pvargDest contains a copy of pvargSrc.
783 * Failure: An HRESULT error code indicating the error.
785 * NOTES
786 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
787 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
788 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
789 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
790 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
792 * NOTES
793 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
794 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
795 * value.
796 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
797 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
798 * to it. If clearing pvargDest fails, so does this function.
800 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
802 VARIANTARG vTmp, *pSrc = pvargSrc;
803 VARTYPE vt;
804 HRESULT hres = S_OK;
806 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
807 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
808 debugstr_VF(pvargSrc));
810 if (!V_ISBYREF(pvargSrc))
811 return VariantCopy(pvargDest, pvargSrc);
813 /* Argument checking is more lax than VariantCopy()... */
814 vt = V_TYPE(pvargSrc);
815 if (V_ISARRAY(pvargSrc) ||
816 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
817 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
819 /* OK */
821 else
822 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
824 if (pvargSrc == pvargDest)
826 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
827 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
829 vTmp = *pvargSrc;
830 pSrc = &vTmp;
831 V_VT(pvargDest) = VT_EMPTY;
833 else
835 /* Copy into another variant. Free the variant in pvargDest */
836 if (FAILED(hres = VariantClear(pvargDest)))
838 TRACE("VariantClear() of destination failed\n");
839 return hres;
843 if (V_ISARRAY(pSrc))
845 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
846 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
848 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
850 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
851 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
853 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
855 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
856 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
858 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
859 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
861 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
862 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
863 if (*V_UNKNOWNREF(pSrc))
864 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
866 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
868 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
869 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
870 hres = E_INVALIDARG; /* Don't dereference more than one level */
871 else
872 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
874 /* Use the dereferenced variants type value, not VT_VARIANT */
875 goto VariantCopyInd_Return;
877 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
879 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
880 sizeof(DECIMAL) - sizeof(USHORT));
882 else
884 /* Copy the pointed to data into this variant */
885 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
888 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
890 VariantCopyInd_Return:
892 if (pSrc != pvargSrc)
893 VariantClear(pSrc);
895 TRACE("returning 0x%08x, %p->(%s%s)\n", hres, pvargDest,
896 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
897 return hres;
900 /******************************************************************************
901 * VariantChangeType [OLEAUT32.12]
903 * Change the type of a variant.
905 * PARAMS
906 * pvargDest [O] Destination for the converted variant
907 * pvargSrc [O] Source variant to change the type of
908 * wFlags [I] VARIANT_ flags from "oleauto.h"
909 * vt [I] Variant type to change pvargSrc into
911 * RETURNS
912 * Success: S_OK. pvargDest contains the converted value.
913 * Failure: An HRESULT error code describing the failure.
915 * NOTES
916 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
917 * See VariantChangeTypeEx.
919 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
920 USHORT wFlags, VARTYPE vt)
922 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
925 /******************************************************************************
926 * VariantChangeTypeEx [OLEAUT32.147]
928 * Change the type of a variant.
930 * PARAMS
931 * pvargDest [O] Destination for the converted variant
932 * pvargSrc [O] Source variant to change the type of
933 * lcid [I] LCID for the conversion
934 * wFlags [I] VARIANT_ flags from "oleauto.h"
935 * vt [I] Variant type to change pvargSrc into
937 * RETURNS
938 * Success: S_OK. pvargDest contains the converted value.
939 * Failure: An HRESULT error code describing the failure.
941 * NOTES
942 * pvargDest and pvargSrc can point to the same variant to perform an in-place
943 * conversion. If the conversion is successful, pvargSrc will be freed.
945 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
946 LCID lcid, USHORT wFlags, VARTYPE vt)
948 HRESULT res = S_OK;
950 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%04x,%s%s)\n", pvargDest,
951 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
952 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
953 debugstr_vt(vt), debugstr_vf(vt));
955 if (vt == VT_CLSID)
956 res = DISP_E_BADVARTYPE;
957 else
959 res = VARIANT_ValidateType(V_VT(pvargSrc));
961 if (SUCCEEDED(res))
963 res = VARIANT_ValidateType(vt);
965 if (SUCCEEDED(res))
967 VARIANTARG vTmp, vSrcDeref;
969 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
970 res = DISP_E_TYPEMISMATCH;
971 else
973 V_VT(&vTmp) = VT_EMPTY;
974 V_VT(&vSrcDeref) = VT_EMPTY;
975 VariantClear(&vTmp);
976 VariantClear(&vSrcDeref);
979 if (SUCCEEDED(res))
981 res = VariantCopyInd(&vSrcDeref, pvargSrc);
982 if (SUCCEEDED(res))
984 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
985 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
986 else
987 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
989 if (SUCCEEDED(res)) {
990 V_VT(&vTmp) = vt;
991 VariantCopy(pvargDest, &vTmp);
993 VariantClear(&vTmp);
994 VariantClear(&vSrcDeref);
1001 TRACE("returning 0x%08x, %p->(%s%s)\n", res, pvargDest,
1002 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1003 return res;
1006 /* Date Conversions */
1008 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1010 /* Convert a VT_DATE value to a Julian Date */
1011 static inline int VARIANT_JulianFromDate(int dateIn)
1013 int julianDays = dateIn;
1015 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1016 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1017 return julianDays;
1020 /* Convert a Julian Date to a VT_DATE value */
1021 static inline int VARIANT_DateFromJulian(int dateIn)
1023 int julianDays = dateIn;
1025 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1026 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1027 return julianDays;
1030 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1031 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1033 int j, i, l, n;
1035 l = jd + 68569;
1036 n = l * 4 / 146097;
1037 l -= (n * 146097 + 3) / 4;
1038 i = (4000 * (l + 1)) / 1461001;
1039 l += 31 - (i * 1461) / 4;
1040 j = (l * 80) / 2447;
1041 *day = l - (j * 2447) / 80;
1042 l = j / 11;
1043 *month = (j + 2) - (12 * l);
1044 *year = 100 * (n - 49) + i + l;
1047 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1048 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1050 int m12 = (month - 14) / 12;
1052 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1053 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1056 /* Macros for accessing DOS format date/time fields */
1057 #define DOS_YEAR(x) (1980 + (x >> 9))
1058 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1059 #define DOS_DAY(x) (x & 0x1f)
1060 #define DOS_HOUR(x) (x >> 11)
1061 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1062 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1063 /* Create a DOS format date/time */
1064 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1065 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1067 /* Roll a date forwards or backwards to correct it */
1068 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1070 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1072 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1073 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1075 /* Years < 100 are treated as 1900 + year */
1076 if (lpUd->st.wYear < 100)
1077 lpUd->st.wYear += 1900;
1079 if (!lpUd->st.wMonth)
1081 /* Roll back to December of the previous year */
1082 lpUd->st.wMonth = 12;
1083 lpUd->st.wYear--;
1085 else while (lpUd->st.wMonth > 12)
1087 /* Roll forward the correct number of months */
1088 lpUd->st.wYear++;
1089 lpUd->st.wMonth -= 12;
1092 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1093 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1094 return E_INVALIDARG; /* Invalid values */
1096 if (!lpUd->st.wDay)
1098 /* Roll back the date one day */
1099 if (lpUd->st.wMonth == 1)
1101 /* Roll back to December 31 of the previous year */
1102 lpUd->st.wDay = 31;
1103 lpUd->st.wMonth = 12;
1104 lpUd->st.wYear--;
1106 else
1108 lpUd->st.wMonth--; /* Previous month */
1109 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1110 lpUd->st.wDay = 29; /* February has 29 days on leap years */
1111 else
1112 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1115 else if (lpUd->st.wDay > 28)
1117 int rollForward = 0;
1119 /* Possibly need to roll the date forward */
1120 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1121 rollForward = lpUd->st.wDay - 29; /* February has 29 days on leap years */
1122 else
1123 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1125 if (rollForward > 0)
1127 lpUd->st.wDay = rollForward;
1128 lpUd->st.wMonth++;
1129 if (lpUd->st.wMonth > 12)
1131 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1132 lpUd->st.wYear++;
1136 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1137 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1138 return S_OK;
1141 /**********************************************************************
1142 * DosDateTimeToVariantTime [OLEAUT32.14]
1144 * Convert a Dos format date and time into variant VT_DATE format.
1146 * PARAMS
1147 * wDosDate [I] Dos format date
1148 * wDosTime [I] Dos format time
1149 * pDateOut [O] Destination for VT_DATE format
1151 * RETURNS
1152 * Success: TRUE. pDateOut contains the converted time.
1153 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1155 * NOTES
1156 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1157 * - Dos format times are accurate to only 2 second precision.
1158 * - The format of a Dos Date is:
1159 *| Bits Values Meaning
1160 *| ---- ------ -------
1161 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1162 *| the days in the month rolls forward the extra days.
1163 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1164 *| year. 13-15 are invalid.
1165 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1166 * - The format of a Dos Time is:
1167 *| Bits Values Meaning
1168 *| ---- ------ -------
1169 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1170 *| 5-10 0-59 Minutes. 60-63 are invalid.
1171 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1173 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1174 double *pDateOut)
1176 UDATE ud;
1178 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1179 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1180 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1181 pDateOut);
1183 ud.st.wYear = DOS_YEAR(wDosDate);
1184 ud.st.wMonth = DOS_MONTH(wDosDate);
1185 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1186 return FALSE;
1187 ud.st.wDay = DOS_DAY(wDosDate);
1188 ud.st.wHour = DOS_HOUR(wDosTime);
1189 ud.st.wMinute = DOS_MINUTE(wDosTime);
1190 ud.st.wSecond = DOS_SECOND(wDosTime);
1191 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1193 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1196 /**********************************************************************
1197 * VariantTimeToDosDateTime [OLEAUT32.13]
1199 * Convert a variant format date into a Dos format date and time.
1201 * dateIn [I] VT_DATE time format
1202 * pwDosDate [O] Destination for Dos format date
1203 * pwDosTime [O] Destination for Dos format time
1205 * RETURNS
1206 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1207 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1209 * NOTES
1210 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1212 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1214 UDATE ud;
1216 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1218 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1219 return FALSE;
1221 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1222 return FALSE;
1224 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1225 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1227 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1228 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1229 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1230 return TRUE;
1233 /***********************************************************************
1234 * SystemTimeToVariantTime [OLEAUT32.184]
1236 * Convert a System format date and time into variant VT_DATE format.
1238 * PARAMS
1239 * lpSt [I] System format date and time
1240 * pDateOut [O] Destination for VT_DATE format date
1242 * RETURNS
1243 * Success: TRUE. *pDateOut contains the converted value.
1244 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1246 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1248 UDATE ud;
1250 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1251 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1253 if (lpSt->wMonth > 12)
1254 return FALSE;
1256 ud.st = *lpSt;
1257 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1260 /***********************************************************************
1261 * VariantTimeToSystemTime [OLEAUT32.185]
1263 * Convert a variant VT_DATE into a System format date and time.
1265 * PARAMS
1266 * datein [I] Variant VT_DATE format date
1267 * lpSt [O] Destination for System format date and time
1269 * RETURNS
1270 * Success: TRUE. *lpSt contains the converted value.
1271 * Failure: FALSE, if dateIn is too large or small.
1273 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1275 UDATE ud;
1277 TRACE("(%g,%p)\n", dateIn, lpSt);
1279 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1280 return FALSE;
1282 *lpSt = ud.st;
1283 return TRUE;
1286 /***********************************************************************
1287 * VarDateFromUdateEx [OLEAUT32.319]
1289 * Convert an unpacked format date and time to a variant VT_DATE.
1291 * PARAMS
1292 * pUdateIn [I] Unpacked format date and time to convert
1293 * lcid [I] Locale identifier for the conversion
1294 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1295 * pDateOut [O] Destination for variant VT_DATE.
1297 * RETURNS
1298 * Success: S_OK. *pDateOut contains the converted value.
1299 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1301 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1303 UDATE ud;
1304 double dateVal;
1306 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1307 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1308 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1309 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1310 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1312 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1313 FIXME("lcid possibly not handled, treating as en-us\n");
1315 ud = *pUdateIn;
1317 if (dwFlags & VAR_VALIDDATE)
1318 WARN("Ignoring VAR_VALIDDATE\n");
1320 if (FAILED(VARIANT_RollUdate(&ud)))
1321 return E_INVALIDARG;
1323 /* Date */
1324 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1326 /* Time */
1327 dateVal += ud.st.wHour / 24.0;
1328 dateVal += ud.st.wMinute / 1440.0;
1329 dateVal += ud.st.wSecond / 86400.0;
1330 dateVal += ud.st.wMilliseconds / 86400000.0;
1332 TRACE("Returning %g\n", dateVal);
1333 *pDateOut = dateVal;
1334 return S_OK;
1337 /***********************************************************************
1338 * VarDateFromUdate [OLEAUT32.330]
1340 * Convert an unpacked format date and time to a variant VT_DATE.
1342 * PARAMS
1343 * pUdateIn [I] Unpacked format date and time to convert
1344 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1345 * pDateOut [O] Destination for variant VT_DATE.
1347 * RETURNS
1348 * Success: S_OK. *pDateOut contains the converted value.
1349 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1351 * NOTES
1352 * This function uses the United States English locale for the conversion. Use
1353 * VarDateFromUdateEx() for alternate locales.
1355 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1357 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1359 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1362 /***********************************************************************
1363 * VarUdateFromDate [OLEAUT32.331]
1365 * Convert a variant VT_DATE into an unpacked format date and time.
1367 * PARAMS
1368 * datein [I] Variant VT_DATE format date
1369 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1370 * lpUdate [O] Destination for unpacked format date and time
1372 * RETURNS
1373 * Success: S_OK. *lpUdate contains the converted value.
1374 * Failure: E_INVALIDARG, if dateIn is too large or small.
1376 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1378 /* Cumulative totals of days per month */
1379 static const USHORT cumulativeDays[] =
1381 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1383 double datePart, timePart;
1384 int julianDays;
1386 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1388 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1389 return E_INVALIDARG;
1391 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1392 /* Compensate for int truncation (always downwards) */
1393 timePart = dateIn - datePart + 0.00000000001;
1394 if (timePart >= 1.0)
1395 timePart -= 0.00000000001;
1397 /* Date */
1398 julianDays = VARIANT_JulianFromDate(dateIn);
1399 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1400 &lpUdate->st.wDay);
1402 datePart = (datePart + 1.5) / 7.0;
1403 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1404 if (lpUdate->st.wDayOfWeek == 0)
1405 lpUdate->st.wDayOfWeek = 5;
1406 else if (lpUdate->st.wDayOfWeek == 1)
1407 lpUdate->st.wDayOfWeek = 6;
1408 else
1409 lpUdate->st.wDayOfWeek -= 2;
1411 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1412 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1413 else
1414 lpUdate->wDayOfYear = 0;
1416 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1417 lpUdate->wDayOfYear += lpUdate->st.wDay;
1419 /* Time */
1420 timePart *= 24.0;
1421 lpUdate->st.wHour = timePart;
1422 timePart -= lpUdate->st.wHour;
1423 timePart *= 60.0;
1424 lpUdate->st.wMinute = timePart;
1425 timePart -= lpUdate->st.wMinute;
1426 timePart *= 60.0;
1427 lpUdate->st.wSecond = timePart;
1428 timePart -= lpUdate->st.wSecond;
1429 lpUdate->st.wMilliseconds = 0;
1430 if (timePart > 0.5)
1432 /* Round the milliseconds, adjusting the time/date forward if needed */
1433 if (lpUdate->st.wSecond < 59)
1434 lpUdate->st.wSecond++;
1435 else
1437 lpUdate->st.wSecond = 0;
1438 if (lpUdate->st.wMinute < 59)
1439 lpUdate->st.wMinute++;
1440 else
1442 lpUdate->st.wMinute = 0;
1443 if (lpUdate->st.wHour < 23)
1444 lpUdate->st.wHour++;
1445 else
1447 lpUdate->st.wHour = 0;
1448 /* Roll over a whole day */
1449 if (++lpUdate->st.wDay > 28)
1450 VARIANT_RollUdate(lpUdate);
1455 return S_OK;
1458 #define GET_NUMBER_TEXT(fld,name) \
1459 buff[0] = 0; \
1460 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1461 WARN("buffer too small for " #fld "\n"); \
1462 else \
1463 if (buff[0]) lpChars->name = buff[0]; \
1464 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1466 /* Get the valid number characters for an lcid */
1467 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1469 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1470 static CRITICAL_SECTION csLastChars = { NULL, -1, 0, 0, 0, 0 };
1471 static VARIANT_NUMBER_CHARS lastChars;
1472 static LCID lastLcid = -1;
1473 static DWORD lastFlags = 0;
1474 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1475 WCHAR buff[4];
1477 /* To make caching thread-safe, a critical section is needed */
1478 EnterCriticalSection(&csLastChars);
1480 /* Asking for default locale entries is very expensive: It is a registry
1481 server call. So cache one locally, as Microsoft does it too */
1482 if(lcid == lastLcid && dwFlags == lastFlags)
1484 memcpy(lpChars, &lastChars, sizeof(defaultChars));
1485 LeaveCriticalSection(&csLastChars);
1486 return;
1489 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1490 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1491 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1492 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1493 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1494 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1495 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1497 /* Local currency symbols are often 2 characters */
1498 lpChars->cCurrencyLocal2 = '\0';
1499 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1501 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1502 case 2: lpChars->cCurrencyLocal = buff[0];
1503 break;
1504 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1506 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1507 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1509 memcpy(&lastChars, lpChars, sizeof(defaultChars));
1510 lastLcid = lcid;
1511 lastFlags = dwFlags;
1512 LeaveCriticalSection(&csLastChars);
1515 /* Number Parsing States */
1516 #define B_PROCESSING_EXPONENT 0x1
1517 #define B_NEGATIVE_EXPONENT 0x2
1518 #define B_EXPONENT_START 0x4
1519 #define B_INEXACT_ZEROS 0x8
1520 #define B_LEADING_ZERO 0x10
1521 #define B_PROCESSING_HEX 0x20
1522 #define B_PROCESSING_OCT 0x40
1524 /**********************************************************************
1525 * VarParseNumFromStr [OLEAUT32.46]
1527 * Parse a string containing a number into a NUMPARSE structure.
1529 * PARAMS
1530 * lpszStr [I] String to parse number from
1531 * lcid [I] Locale Id for the conversion
1532 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1533 * pNumprs [I/O] Destination for parsed number
1534 * rgbDig [O] Destination for digits read in
1536 * RETURNS
1537 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1538 * the number.
1539 * Failure: E_INVALIDARG, if any parameter is invalid.
1540 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1541 * incorrectly.
1542 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1544 * NOTES
1545 * pNumprs must have the following fields set:
1546 * cDig: Set to the size of rgbDig.
1547 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1548 * from "oleauto.h".
1550 * FIXME
1551 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1552 * numerals, so this has not been implemented.
1554 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1555 NUMPARSE *pNumprs, BYTE *rgbDig)
1557 VARIANT_NUMBER_CHARS chars;
1558 BYTE rgbTmp[1024];
1559 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1560 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1561 int cchUsed = 0;
1563 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1565 if (!pNumprs || !rgbDig)
1566 return E_INVALIDARG;
1568 if (pNumprs->cDig < iMaxDigits)
1569 iMaxDigits = pNumprs->cDig;
1571 pNumprs->cDig = 0;
1572 pNumprs->dwOutFlags = 0;
1573 pNumprs->cchUsed = 0;
1574 pNumprs->nBaseShift = 0;
1575 pNumprs->nPwr10 = 0;
1577 if (!lpszStr)
1578 return DISP_E_TYPEMISMATCH;
1580 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1582 /* First consume all the leading symbols and space from the string */
1583 while (1)
1585 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1587 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1590 cchUsed++;
1591 lpszStr++;
1592 } while (isspaceW(*lpszStr));
1594 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1595 *lpszStr == chars.cPositiveSymbol &&
1596 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1598 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1599 cchUsed++;
1600 lpszStr++;
1602 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1603 *lpszStr == chars.cNegativeSymbol &&
1604 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1606 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1607 cchUsed++;
1608 lpszStr++;
1610 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1611 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1612 *lpszStr == chars.cCurrencyLocal &&
1613 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1615 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1616 cchUsed++;
1617 lpszStr++;
1618 /* Only accept currency characters */
1619 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1620 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1622 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1623 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1625 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1626 cchUsed++;
1627 lpszStr++;
1629 else
1630 break;
1633 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1635 /* Only accept non-currency characters */
1636 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1637 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1640 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1641 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1643 dwState |= B_PROCESSING_HEX;
1644 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1645 cchUsed=cchUsed+2;
1646 lpszStr=lpszStr+2;
1648 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1649 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1651 dwState |= B_PROCESSING_OCT;
1652 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1653 cchUsed=cchUsed+2;
1654 lpszStr=lpszStr+2;
1657 /* Strip Leading zeros */
1658 while (*lpszStr == '0')
1660 dwState |= B_LEADING_ZERO;
1661 cchUsed++;
1662 lpszStr++;
1665 while (*lpszStr)
1667 if (isdigitW(*lpszStr))
1669 if (dwState & B_PROCESSING_EXPONENT)
1671 int exponentSize = 0;
1672 if (dwState & B_EXPONENT_START)
1674 if (!isdigitW(*lpszStr))
1675 break; /* No exponent digits - invalid */
1676 while (*lpszStr == '0')
1678 /* Skip leading zero's in the exponent */
1679 cchUsed++;
1680 lpszStr++;
1684 while (isdigitW(*lpszStr))
1686 exponentSize *= 10;
1687 exponentSize += *lpszStr - '0';
1688 cchUsed++;
1689 lpszStr++;
1691 if (dwState & B_NEGATIVE_EXPONENT)
1692 exponentSize = -exponentSize;
1693 /* Add the exponent into the powers of 10 */
1694 pNumprs->nPwr10 += exponentSize;
1695 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1696 lpszStr--; /* back up to allow processing of next char */
1698 else
1700 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1701 && !(dwState & B_PROCESSING_OCT))
1703 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1705 if (*lpszStr != '0')
1706 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1708 /* This digit can't be represented, but count it in nPwr10 */
1709 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1710 pNumprs->nPwr10--;
1711 else
1712 pNumprs->nPwr10++;
1714 else
1716 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1717 return DISP_E_TYPEMISMATCH;
1720 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1721 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1723 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1725 pNumprs->cDig++;
1726 cchUsed++;
1729 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1731 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1732 cchUsed++;
1734 else if (*lpszStr == chars.cDecimalPoint &&
1735 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1736 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1738 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1739 cchUsed++;
1741 /* If we have no digits so far, skip leading zeros */
1742 if (!pNumprs->cDig)
1744 while (lpszStr[1] == '0')
1746 dwState |= B_LEADING_ZERO;
1747 cchUsed++;
1748 lpszStr++;
1749 pNumprs->nPwr10--;
1753 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1754 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1755 dwState & B_PROCESSING_HEX)
1757 if (pNumprs->cDig >= iMaxDigits)
1759 return DISP_E_OVERFLOW;
1761 else
1763 if (*lpszStr >= 'a')
1764 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1765 else
1766 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1768 pNumprs->cDig++;
1769 cchUsed++;
1771 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1772 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1773 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1775 dwState |= B_PROCESSING_EXPONENT;
1776 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1777 cchUsed++;
1779 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1781 cchUsed++; /* Ignore positive exponent */
1783 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1785 dwState |= B_NEGATIVE_EXPONENT;
1786 cchUsed++;
1788 else
1789 break; /* Stop at an unrecognised character */
1791 lpszStr++;
1794 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1796 /* Ensure a 0 on its own gets stored */
1797 pNumprs->cDig = 1;
1798 rgbTmp[0] = 0;
1801 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1803 pNumprs->cchUsed = cchUsed;
1804 WARN("didn't completely parse exponent\n");
1805 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1808 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1810 if (dwState & B_INEXACT_ZEROS)
1811 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1812 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1814 /* copy all of the digits into the output digit buffer */
1815 /* this is exactly what windows does although it also returns */
1816 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1817 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1819 if (dwState & B_PROCESSING_HEX) {
1820 /* hex numbers have always the same format */
1821 pNumprs->nPwr10=0;
1822 pNumprs->nBaseShift=4;
1823 } else {
1824 if (dwState & B_PROCESSING_OCT) {
1825 /* oct numbers have always the same format */
1826 pNumprs->nPwr10=0;
1827 pNumprs->nBaseShift=3;
1828 } else {
1829 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1831 pNumprs->nPwr10++;
1832 pNumprs->cDig--;
1836 } else
1838 /* Remove trailing zeros from the last (whole number or decimal) part */
1839 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1841 pNumprs->nPwr10++;
1842 pNumprs->cDig--;
1846 if (pNumprs->cDig <= iMaxDigits)
1847 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1848 else
1849 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1851 /* Copy the digits we processed into rgbDig */
1852 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1854 /* Consume any trailing symbols and space */
1855 while (1)
1857 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1859 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1862 cchUsed++;
1863 lpszStr++;
1864 } while (isspaceW(*lpszStr));
1866 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1867 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1868 *lpszStr == chars.cPositiveSymbol)
1870 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1871 cchUsed++;
1872 lpszStr++;
1874 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1875 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1876 *lpszStr == chars.cNegativeSymbol)
1878 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1879 cchUsed++;
1880 lpszStr++;
1882 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1883 pNumprs->dwOutFlags & NUMPRS_PARENS)
1885 cchUsed++;
1886 lpszStr++;
1887 pNumprs->dwOutFlags |= NUMPRS_NEG;
1889 else
1890 break;
1893 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1895 pNumprs->cchUsed = cchUsed;
1896 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1899 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1900 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1902 if (!pNumprs->cDig)
1903 return DISP_E_TYPEMISMATCH; /* No Number found */
1905 pNumprs->cchUsed = cchUsed;
1906 return S_OK;
1909 /* VTBIT flags indicating an integer value */
1910 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1911 /* VTBIT flags indicating a real number value */
1912 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1914 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1915 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1916 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1917 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1919 /**********************************************************************
1920 * VarNumFromParseNum [OLEAUT32.47]
1922 * Convert a NUMPARSE structure into a numeric Variant type.
1924 * PARAMS
1925 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1926 * rgbDig [I] Source for the numbers digits
1927 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1928 * pVarDst [O] Destination for the converted Variant value.
1930 * RETURNS
1931 * Success: S_OK. pVarDst contains the converted value.
1932 * Failure: E_INVALIDARG, if any parameter is invalid.
1933 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1935 * NOTES
1936 * - The smallest favoured type present in dwVtBits that can represent the
1937 * number in pNumprs without losing precision is used.
1938 * - Signed types are preferred over unsigned types of the same size.
1939 * - Preferred types in order are: integer, float, double, currency then decimal.
1940 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1941 * for details of the rounding method.
1942 * - pVarDst is not cleared before the result is stored in it.
1943 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1944 * design?): If some other VTBIT's for integers are specified together
1945 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1946 * the number to the smallest requested integer truncating this way the
1947 * number. Wine doesn't implement this "feature" (yet?).
1949 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1950 ULONG dwVtBits, VARIANT *pVarDst)
1952 /* Scale factors and limits for double arithmetic */
1953 static const double dblMultipliers[11] = {
1954 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1955 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1957 static const double dblMinimums[11] = {
1958 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1959 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1960 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1962 static const double dblMaximums[11] = {
1963 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1964 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1965 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1968 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1970 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1972 if (pNumprs->nBaseShift)
1974 /* nBaseShift indicates a hex or octal number */
1975 ULONG64 ul64 = 0;
1976 LONG64 l64;
1977 int i;
1979 /* Convert the hex or octal number string into a UI64 */
1980 for (i = 0; i < pNumprs->cDig; i++)
1982 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1984 TRACE("Overflow multiplying digits\n");
1985 return DISP_E_OVERFLOW;
1987 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1990 /* also make a negative representation */
1991 l64=-ul64;
1993 /* Try signed and unsigned types in size order */
1994 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
1996 V_VT(pVarDst) = VT_I1;
1997 V_I1(pVarDst) = ul64;
1998 return S_OK;
2000 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2002 V_VT(pVarDst) = VT_UI1;
2003 V_UI1(pVarDst) = ul64;
2004 return S_OK;
2006 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2008 V_VT(pVarDst) = VT_I2;
2009 V_I2(pVarDst) = ul64;
2010 return S_OK;
2012 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2014 V_VT(pVarDst) = VT_UI2;
2015 V_UI2(pVarDst) = ul64;
2016 return S_OK;
2018 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2020 V_VT(pVarDst) = VT_I4;
2021 V_I4(pVarDst) = ul64;
2022 return S_OK;
2024 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2026 V_VT(pVarDst) = VT_UI4;
2027 V_UI4(pVarDst) = ul64;
2028 return S_OK;
2030 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2032 V_VT(pVarDst) = VT_I8;
2033 V_I8(pVarDst) = ul64;
2034 return S_OK;
2036 else if (dwVtBits & VTBIT_UI8)
2038 V_VT(pVarDst) = VT_UI8;
2039 V_UI8(pVarDst) = ul64;
2040 return S_OK;
2042 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2044 V_VT(pVarDst) = VT_DECIMAL;
2045 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2046 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2047 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2048 return S_OK;
2050 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2052 V_VT(pVarDst) = VT_R4;
2053 if (ul64 <= I4_MAX)
2054 V_R4(pVarDst) = ul64;
2055 else
2056 V_R4(pVarDst) = l64;
2057 return S_OK;
2059 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2061 V_VT(pVarDst) = VT_R8;
2062 if (ul64 <= I4_MAX)
2063 V_R8(pVarDst) = ul64;
2064 else
2065 V_R8(pVarDst) = l64;
2066 return S_OK;
2069 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2070 return DISP_E_OVERFLOW;
2073 /* Count the number of relevant fractional and whole digits stored,
2074 * And compute the divisor/multiplier to scale the number by.
2076 if (pNumprs->nPwr10 < 0)
2078 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2080 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2081 wholeNumberDigits = 0;
2082 fractionalDigits = pNumprs->cDig;
2083 divisor10 = -pNumprs->nPwr10;
2085 else
2087 /* An exactly represented real number e.g. 1.024 */
2088 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2089 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2090 divisor10 = pNumprs->cDig - wholeNumberDigits;
2093 else if (pNumprs->nPwr10 == 0)
2095 /* An exactly represented whole number e.g. 1024 */
2096 wholeNumberDigits = pNumprs->cDig;
2097 fractionalDigits = 0;
2099 else /* pNumprs->nPwr10 > 0 */
2101 /* A whole number followed by nPwr10 0's e.g. 102400 */
2102 wholeNumberDigits = pNumprs->cDig;
2103 fractionalDigits = 0;
2104 multiplier10 = pNumprs->nPwr10;
2107 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2108 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2109 multiplier10, divisor10);
2111 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2112 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2114 /* We have one or more integer output choices, and either:
2115 * 1) An integer input value, or
2116 * 2) A real number input value but no floating output choices.
2117 * Alternately, we have a DECIMAL output available and an integer input.
2119 * So, place the integer value into pVarDst, using the smallest type
2120 * possible and preferring signed over unsigned types.
2122 BOOL bOverflow = FALSE, bNegative;
2123 ULONG64 ul64 = 0;
2124 int i;
2126 /* Convert the integer part of the number into a UI8 */
2127 for (i = 0; i < wholeNumberDigits; i++)
2129 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2131 TRACE("Overflow multiplying digits\n");
2132 bOverflow = TRUE;
2133 break;
2135 ul64 = ul64 * 10 + rgbDig[i];
2138 /* Account for the scale of the number */
2139 if (!bOverflow && multiplier10)
2141 for (i = 0; i < multiplier10; i++)
2143 if (ul64 > (UI8_MAX / 10))
2145 TRACE("Overflow scaling number\n");
2146 bOverflow = TRUE;
2147 break;
2149 ul64 = ul64 * 10;
2153 /* If we have any fractional digits, round the value.
2154 * Note we don't have to do this if divisor10 is < 1,
2155 * because this means the fractional part must be < 0.5
2157 if (!bOverflow && fractionalDigits && divisor10 > 0)
2159 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2160 BOOL bAdjust = FALSE;
2162 TRACE("first decimal value is %d\n", *fracDig);
2164 if (*fracDig > 5)
2165 bAdjust = TRUE; /* > 0.5 */
2166 else if (*fracDig == 5)
2168 for (i = 1; i < fractionalDigits; i++)
2170 if (fracDig[i])
2172 bAdjust = TRUE; /* > 0.5 */
2173 break;
2176 /* If exactly 0.5, round only odd values */
2177 if (i == fractionalDigits && (ul64 & 1))
2178 bAdjust = TRUE;
2181 if (bAdjust)
2183 if (ul64 == UI8_MAX)
2185 TRACE("Overflow after rounding\n");
2186 bOverflow = TRUE;
2188 ul64++;
2192 /* Zero is not a negative number */
2193 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2195 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2197 /* For negative integers, try the signed types in size order */
2198 if (!bOverflow && bNegative)
2200 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2202 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2204 V_VT(pVarDst) = VT_I1;
2205 V_I1(pVarDst) = -ul64;
2206 return S_OK;
2208 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2210 V_VT(pVarDst) = VT_I2;
2211 V_I2(pVarDst) = -ul64;
2212 return S_OK;
2214 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2216 V_VT(pVarDst) = VT_I4;
2217 V_I4(pVarDst) = -ul64;
2218 return S_OK;
2220 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2222 V_VT(pVarDst) = VT_I8;
2223 V_I8(pVarDst) = -ul64;
2224 return S_OK;
2226 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2228 /* Decimal is only output choice left - fast path */
2229 V_VT(pVarDst) = VT_DECIMAL;
2230 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2231 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2232 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2233 return S_OK;
2237 else if (!bOverflow)
2239 /* For positive integers, try signed then unsigned types in size order */
2240 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2242 V_VT(pVarDst) = VT_I1;
2243 V_I1(pVarDst) = ul64;
2244 return S_OK;
2246 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2248 V_VT(pVarDst) = VT_UI1;
2249 V_UI1(pVarDst) = ul64;
2250 return S_OK;
2252 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2254 V_VT(pVarDst) = VT_I2;
2255 V_I2(pVarDst) = ul64;
2256 return S_OK;
2258 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2260 V_VT(pVarDst) = VT_UI2;
2261 V_UI2(pVarDst) = ul64;
2262 return S_OK;
2264 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2266 V_VT(pVarDst) = VT_I4;
2267 V_I4(pVarDst) = ul64;
2268 return S_OK;
2270 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2272 V_VT(pVarDst) = VT_UI4;
2273 V_UI4(pVarDst) = ul64;
2274 return S_OK;
2276 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2278 V_VT(pVarDst) = VT_I8;
2279 V_I8(pVarDst) = ul64;
2280 return S_OK;
2282 else if (dwVtBits & VTBIT_UI8)
2284 V_VT(pVarDst) = VT_UI8;
2285 V_UI8(pVarDst) = ul64;
2286 return S_OK;
2288 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2290 /* Decimal is only output choice left - fast path */
2291 V_VT(pVarDst) = VT_DECIMAL;
2292 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2293 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2294 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2295 return S_OK;
2300 if (dwVtBits & REAL_VTBITS)
2302 /* Try to put the number into a float or real */
2303 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2304 double whole = 0.0;
2305 int i;
2307 /* Convert the number into a double */
2308 for (i = 0; i < pNumprs->cDig; i++)
2309 whole = whole * 10.0 + rgbDig[i];
2311 TRACE("Whole double value is %16.16g\n", whole);
2313 /* Account for the scale */
2314 while (multiplier10 > 10)
2316 if (whole > dblMaximums[10])
2318 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2319 bOverflow = TRUE;
2320 break;
2322 whole = whole * dblMultipliers[10];
2323 multiplier10 -= 10;
2325 if (multiplier10 && !bOverflow)
2327 if (whole > dblMaximums[multiplier10])
2329 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2330 bOverflow = TRUE;
2332 else
2333 whole = whole * dblMultipliers[multiplier10];
2336 if (!bOverflow)
2337 TRACE("Scaled double value is %16.16g\n", whole);
2339 while (divisor10 > 10 && !bOverflow)
2341 if (whole < dblMinimums[10] && whole != 0)
2343 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2344 bOverflow = TRUE;
2345 break;
2347 whole = whole / dblMultipliers[10];
2348 divisor10 -= 10;
2350 if (divisor10 && !bOverflow)
2352 if (whole < dblMinimums[divisor10] && whole != 0)
2354 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2355 bOverflow = TRUE;
2357 else
2358 whole = whole / dblMultipliers[divisor10];
2360 if (!bOverflow)
2361 TRACE("Final double value is %16.16g\n", whole);
2363 if (dwVtBits & VTBIT_R4 &&
2364 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2366 TRACE("Set R4 to final value\n");
2367 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2368 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2369 return S_OK;
2372 if (dwVtBits & VTBIT_R8)
2374 TRACE("Set R8 to final value\n");
2375 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2376 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2377 return S_OK;
2380 if (dwVtBits & VTBIT_CY)
2382 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2384 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2385 TRACE("Set CY to final value\n");
2386 return S_OK;
2388 TRACE("Value Overflows CY\n");
2392 if (dwVtBits & VTBIT_DECIMAL)
2394 int i;
2395 ULONG carry;
2396 ULONG64 tmp;
2397 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2399 DECIMAL_SETZERO(*pDec);
2400 DEC_LO32(pDec) = 0;
2402 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2403 DEC_SIGN(pDec) = DECIMAL_NEG;
2404 else
2405 DEC_SIGN(pDec) = DECIMAL_POS;
2407 /* Factor the significant digits */
2408 for (i = 0; i < pNumprs->cDig; i++)
2410 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2411 carry = (ULONG)(tmp >> 32);
2412 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2413 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2414 carry = (ULONG)(tmp >> 32);
2415 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2416 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2417 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2419 if (tmp >> 32 & UI4_MAX)
2421 VarNumFromParseNum_DecOverflow:
2422 TRACE("Overflow\n");
2423 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2424 return DISP_E_OVERFLOW;
2428 /* Account for the scale of the number */
2429 while (multiplier10 > 0)
2431 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2432 carry = (ULONG)(tmp >> 32);
2433 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2434 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2435 carry = (ULONG)(tmp >> 32);
2436 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2437 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2438 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2440 if (tmp >> 32 & UI4_MAX)
2441 goto VarNumFromParseNum_DecOverflow;
2442 multiplier10--;
2444 DEC_SCALE(pDec) = divisor10;
2446 V_VT(pVarDst) = VT_DECIMAL;
2447 return S_OK;
2449 return DISP_E_OVERFLOW; /* No more output choices */
2452 /**********************************************************************
2453 * VarCat [OLEAUT32.318]
2455 * Concatenates one variant onto another.
2457 * PARAMS
2458 * left [I] First variant
2459 * right [I] Second variant
2460 * result [O] Result variant
2462 * RETURNS
2463 * Success: S_OK.
2464 * Failure: An HRESULT error code indicating the error.
2466 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2468 VARTYPE leftvt,rightvt,resultvt;
2469 HRESULT hres;
2470 static const WCHAR str_true[] = {'T','r','u','e','\0'};
2471 static const WCHAR str_false[] = {'F','a','l','s','e','\0'};
2472 static const WCHAR sz_empty[] = {'\0'};
2473 leftvt = V_VT(left);
2474 rightvt = V_VT(right);
2476 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2477 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2479 /* when both left and right are NULL the result is NULL */
2480 if (leftvt == VT_NULL && rightvt == VT_NULL)
2482 V_VT(out) = VT_NULL;
2483 return S_OK;
2486 hres = S_OK;
2487 resultvt = VT_EMPTY;
2489 /* There are many special case for errors and return types */
2490 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2491 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2492 hres = DISP_E_TYPEMISMATCH;
2493 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2494 leftvt == VT_R4 || leftvt == VT_R8 ||
2495 leftvt == VT_CY || leftvt == VT_BOOL ||
2496 leftvt == VT_BSTR || leftvt == VT_I1 ||
2497 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2498 leftvt == VT_UI4 || leftvt == VT_I8 ||
2499 leftvt == VT_UI8 || leftvt == VT_INT ||
2500 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2501 leftvt == VT_NULL || leftvt == VT_DATE ||
2502 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2504 (rightvt == VT_I2 || rightvt == VT_I4 ||
2505 rightvt == VT_R4 || rightvt == VT_R8 ||
2506 rightvt == VT_CY || rightvt == VT_BOOL ||
2507 rightvt == VT_BSTR || rightvt == VT_I1 ||
2508 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2509 rightvt == VT_UI4 || rightvt == VT_I8 ||
2510 rightvt == VT_UI8 || rightvt == VT_INT ||
2511 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2512 rightvt == VT_NULL || rightvt == VT_DATE ||
2513 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2514 resultvt = VT_BSTR;
2515 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2516 hres = DISP_E_TYPEMISMATCH;
2517 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2518 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2519 hres = DISP_E_TYPEMISMATCH;
2520 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2521 rightvt == VT_DECIMAL)
2522 hres = DISP_E_BADVARTYPE;
2523 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2524 hres = DISP_E_TYPEMISMATCH;
2525 else if (leftvt == VT_VARIANT)
2526 hres = DISP_E_TYPEMISMATCH;
2527 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2528 leftvt == VT_NULL || leftvt == VT_I2 ||
2529 leftvt == VT_I4 || leftvt == VT_R4 ||
2530 leftvt == VT_R8 || leftvt == VT_CY ||
2531 leftvt == VT_DATE || leftvt == VT_BSTR ||
2532 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2533 leftvt == VT_I1 || leftvt == VT_UI1 ||
2534 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2535 leftvt == VT_I8 || leftvt == VT_UI8 ||
2536 leftvt == VT_INT || leftvt == VT_UINT))
2537 hres = DISP_E_TYPEMISMATCH;
2538 else
2539 hres = DISP_E_BADVARTYPE;
2541 /* if result type is not S_OK, then no need to go further */
2542 if (hres != S_OK)
2544 V_VT(out) = resultvt;
2545 return hres;
2547 /* Else proceed with formatting inputs to strings */
2548 else
2550 VARIANT bstrvar_left, bstrvar_right;
2551 V_VT(out) = VT_BSTR;
2553 VariantInit(&bstrvar_left);
2554 VariantInit(&bstrvar_right);
2556 /* Convert left side variant to string */
2557 if (leftvt != VT_BSTR)
2559 if (leftvt == VT_BOOL)
2561 /* Bools are handled as True/False strings instead of 0/-1 as in MSDN */
2562 V_VT(&bstrvar_left) = VT_BSTR;
2563 if (V_BOOL(left) == TRUE)
2564 V_BSTR(&bstrvar_left) = SysAllocString(str_true);
2565 else
2566 V_BSTR(&bstrvar_left) = SysAllocString(str_false);
2568 /* Fill with empty string for later concat with right side */
2569 else if (leftvt == VT_NULL)
2571 V_VT(&bstrvar_left) = VT_BSTR;
2572 V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
2574 else
2576 hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
2577 if (hres != S_OK) {
2578 VariantClear(&bstrvar_left);
2579 VariantClear(&bstrvar_right);
2580 if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
2581 rightvt == VT_NULL || rightvt == VT_I2 ||
2582 rightvt == VT_I4 || rightvt == VT_R4 ||
2583 rightvt == VT_R8 || rightvt == VT_CY ||
2584 rightvt == VT_DATE || rightvt == VT_BSTR ||
2585 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
2586 rightvt == VT_I1 || rightvt == VT_UI1 ||
2587 rightvt == VT_UI2 || rightvt == VT_UI4 ||
2588 rightvt == VT_I8 || rightvt == VT_UI8 ||
2589 rightvt == VT_INT || rightvt == VT_UINT))
2590 return DISP_E_BADVARTYPE;
2591 return hres;
2596 /* convert right side variant to string */
2597 if (rightvt != VT_BSTR)
2599 if (rightvt == VT_BOOL)
2601 /* Bools are handled as True/False strings instead of 0/-1 as in MSDN */
2602 V_VT(&bstrvar_right) = VT_BSTR;
2603 if (V_BOOL(right) == TRUE)
2604 V_BSTR(&bstrvar_right) = SysAllocString(str_true);
2605 else
2606 V_BSTR(&bstrvar_right) = SysAllocString(str_false);
2608 /* Fill with empty string for later concat with right side */
2609 else if (rightvt == VT_NULL)
2611 V_VT(&bstrvar_right) = VT_BSTR;
2612 V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
2614 else
2616 hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
2617 if (hres != S_OK) {
2618 VariantClear(&bstrvar_left);
2619 VariantClear(&bstrvar_right);
2620 if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
2621 leftvt == VT_NULL || leftvt == VT_I2 ||
2622 leftvt == VT_I4 || leftvt == VT_R4 ||
2623 leftvt == VT_R8 || leftvt == VT_CY ||
2624 leftvt == VT_DATE || leftvt == VT_BSTR ||
2625 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2626 leftvt == VT_I1 || leftvt == VT_UI1 ||
2627 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2628 leftvt == VT_I8 || leftvt == VT_UI8 ||
2629 leftvt == VT_INT || leftvt == VT_UINT))
2630 return DISP_E_BADVARTYPE;
2631 return hres;
2636 /* Concat the resulting strings together */
2637 if (leftvt == VT_BSTR && rightvt == VT_BSTR)
2638 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2639 else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
2640 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2641 else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
2642 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
2643 else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
2644 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2646 VariantClear(&bstrvar_left);
2647 VariantClear(&bstrvar_right);
2648 return S_OK;
2653 /* Wrapper around VariantChangeTypeEx() which permits changing a
2654 variant with VT_RESERVED flag set. Needed by VarCmp. */
2655 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2656 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2658 HRESULT res;
2659 VARTYPE flags;
2661 flags = V_VT(pvargSrc) & ~VT_TYPEMASK;
2662 V_VT(pvargSrc) &= ~VT_RESERVED;
2663 res = VariantChangeTypeEx(pvargDest,pvargSrc,lcid,wFlags,vt);
2664 V_VT(pvargSrc) |= flags;
2666 return res;
2669 /**********************************************************************
2670 * VarCmp [OLEAUT32.176]
2672 * Compare two variants.
2674 * PARAMS
2675 * left [I] First variant
2676 * right [I] Second variant
2677 * lcid [I] LCID (locale identifier) for the comparison
2678 * flags [I] Flags to be used in the comparison:
2679 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2680 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2682 * RETURNS
2683 * VARCMP_LT: left variant is less than right variant.
2684 * VARCMP_EQ: input variants are equal.
2685 * VARCMP_GT: left variant is greater than right variant.
2686 * VARCMP_NULL: either one of the input variants is NULL.
2687 * Failure: An HRESULT error code indicating the error.
2689 * NOTES
2690 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2691 * UI8 and UINT as input variants. INT is accepted only as left variant.
2693 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2694 * an ERROR variant will trigger an error.
2696 * Both input variants can have VT_RESERVED flag set which is ignored
2697 * unless one and only one of the variants is a BSTR and the other one
2698 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2699 * different meaning:
2700 * - BSTR and other: BSTR is always greater than the other variant.
2701 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2702 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2703 * comparison will take place else the BSTR is always greater.
2704 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2705 * variant is ignored and the return value depends only on the sign
2706 * of the BSTR if it is a number else the BSTR is always greater. A
2707 * positive BSTR is greater, a negative one is smaller than the other
2708 * variant.
2710 * SEE
2711 * VarBstrCmp for the lcid and flags usage.
2713 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2715 VARTYPE lvt, rvt, vt;
2716 VARIANT rv,lv;
2717 DWORD xmask;
2718 HRESULT rc;
2720 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%08x)\n", left, debugstr_VT(left),
2721 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2723 lvt = V_VT(left) & VT_TYPEMASK;
2724 rvt = V_VT(right) & VT_TYPEMASK;
2725 xmask = (1 << lvt) | (1 << rvt);
2727 /* If we have any flag set except VT_RESERVED bail out.
2728 Same for the left input variant type > VT_INT and for the
2729 right input variant type > VT_I8. Yes, VT_INT is only supported
2730 as left variant. Go figure */
2731 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2732 lvt > VT_INT || rvt > VT_I8) {
2733 return DISP_E_BADVARTYPE;
2736 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2737 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2738 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2739 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2740 return DISP_E_TYPEMISMATCH;
2742 /* If both variants are VT_ERROR return VARCMP_EQ */
2743 if (xmask == VTBIT_ERROR)
2744 return VARCMP_EQ;
2745 else if (xmask & VTBIT_ERROR)
2746 return DISP_E_TYPEMISMATCH;
2748 if (xmask & VTBIT_NULL)
2749 return VARCMP_NULL;
2751 VariantInit(&lv);
2752 VariantInit(&rv);
2754 /* Two BSTRs, ignore VT_RESERVED */
2755 if (xmask == VTBIT_BSTR)
2756 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2758 /* A BSTR and an other variant; we have to take care of VT_RESERVED */
2759 if (xmask & VTBIT_BSTR) {
2760 VARIANT *bstrv, *nonbv;
2761 VARTYPE nonbvt;
2762 int swap = 0;
2764 /* Swap the variants so the BSTR is always on the left */
2765 if (lvt == VT_BSTR) {
2766 bstrv = left;
2767 nonbv = right;
2768 nonbvt = rvt;
2769 } else {
2770 swap = 1;
2771 bstrv = right;
2772 nonbv = left;
2773 nonbvt = lvt;
2776 /* BSTR and EMPTY: ignore VT_RESERVED */
2777 if (nonbvt == VT_EMPTY)
2778 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2779 else {
2780 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2781 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2783 if (!breserv && !nreserv)
2784 /* No VT_RESERVED set ==> BSTR always greater */
2785 rc = VARCMP_GT;
2786 else if (breserv && !nreserv) {
2787 /* BSTR has VT_RESERVED set. Do a string comparison */
2788 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2789 if (FAILED(rc))
2790 return rc;
2791 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2792 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2793 /* Non NULL nor empty BSTR */
2794 /* If the BSTR is not a number the BSTR is greater */
2795 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2796 if (FAILED(rc))
2797 rc = VARCMP_GT;
2798 else if (breserv && nreserv)
2799 /* FIXME: This is strange: with both VT_RESERVED set it
2800 looks like the result depends only on the sign of
2801 the BSTR number */
2802 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2803 else
2804 /* Numeric comparison, will be handled below.
2805 VARCMP_NULL used only to break out. */
2806 rc = VARCMP_NULL;
2807 VariantClear(&lv);
2808 VariantClear(&rv);
2809 } else
2810 /* Empty or NULL BSTR */
2811 rc = VARCMP_GT;
2813 /* Fixup the return code if we swapped left and right */
2814 if (swap) {
2815 if (rc == VARCMP_GT)
2816 rc = VARCMP_LT;
2817 else if (rc == VARCMP_LT)
2818 rc = VARCMP_GT;
2820 if (rc != VARCMP_NULL)
2821 return rc;
2824 if (xmask & VTBIT_DECIMAL)
2825 vt = VT_DECIMAL;
2826 else if (xmask & VTBIT_BSTR)
2827 vt = VT_R8;
2828 else if (xmask & VTBIT_R4)
2829 vt = VT_R4;
2830 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2831 vt = VT_R8;
2832 else if (xmask & VTBIT_CY)
2833 vt = VT_CY;
2834 else
2835 /* default to I8 */
2836 vt = VT_I8;
2838 /* Coerce the variants */
2839 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2840 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2841 /* Overflow, change to R8 */
2842 vt = VT_R8;
2843 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2845 if (FAILED(rc))
2846 return rc;
2847 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2848 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2849 /* Overflow, change to R8 */
2850 vt = VT_R8;
2851 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2852 if (FAILED(rc))
2853 return rc;
2854 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2856 if (FAILED(rc))
2857 return rc;
2859 #define _VARCMP(a,b) \
2860 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2862 switch (vt) {
2863 case VT_CY:
2864 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2865 case VT_DECIMAL:
2866 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2867 case VT_I8:
2868 return _VARCMP(V_I8(&lv), V_I8(&rv));
2869 case VT_R4:
2870 return _VARCMP(V_R4(&lv), V_R4(&rv));
2871 case VT_R8:
2872 return _VARCMP(V_R8(&lv), V_R8(&rv));
2873 default:
2874 /* We should never get here */
2875 return E_FAIL;
2877 #undef _VARCMP
2880 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
2882 HRESULT hres;
2883 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
2885 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
2886 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
2887 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
2888 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
2889 NULL, NULL);
2890 } else {
2891 hres = DISP_E_TYPEMISMATCH;
2893 return hres;
2896 /**********************************************************************
2897 * VarAnd [OLEAUT32.142]
2899 * Computes the logical AND of two variants.
2901 * PARAMS
2902 * left [I] First variant
2903 * right [I] Second variant
2904 * result [O] Result variant
2906 * RETURNS
2907 * Success: S_OK.
2908 * Failure: An HRESULT error code indicating the error.
2910 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2912 HRESULT hres = S_OK;
2913 VARTYPE resvt = VT_EMPTY;
2914 VARTYPE leftvt,rightvt;
2915 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2916 VARIANT varLeft, varRight;
2917 VARIANT tempLeft, tempRight;
2919 VariantInit(&varLeft);
2920 VariantInit(&varRight);
2921 VariantInit(&tempLeft);
2922 VariantInit(&tempRight);
2924 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2925 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2927 /* Handle VT_DISPATCH by storing and taking address of returned value */
2928 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2930 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2931 if (FAILED(hres)) goto VarAnd_Exit;
2932 left = &tempLeft;
2934 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2936 hres = VARIANT_FetchDispatchValue(right, &tempRight);
2937 if (FAILED(hres)) goto VarAnd_Exit;
2938 right = &tempRight;
2941 leftvt = V_VT(left)&VT_TYPEMASK;
2942 rightvt = V_VT(right)&VT_TYPEMASK;
2943 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
2944 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2946 if (leftExtraFlags != rightExtraFlags)
2948 hres = DISP_E_BADVARTYPE;
2949 goto VarAnd_Exit;
2951 ExtraFlags = leftExtraFlags;
2953 /* Native VarAnd always returns an error when using extra
2954 * flags or if the variant combination is I8 and INT.
2956 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
2957 (leftvt == VT_INT && rightvt == VT_I8) ||
2958 ExtraFlags != 0)
2960 hres = DISP_E_BADVARTYPE;
2961 goto VarAnd_Exit;
2964 /* Determine return type */
2965 else if (leftvt == VT_I8 || rightvt == VT_I8)
2966 resvt = VT_I8;
2967 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
2968 leftvt == VT_UINT || rightvt == VT_UINT ||
2969 leftvt == VT_INT || rightvt == VT_INT ||
2970 leftvt == VT_UINT || rightvt == VT_UINT ||
2971 leftvt == VT_R4 || rightvt == VT_R4 ||
2972 leftvt == VT_R8 || rightvt == VT_R8 ||
2973 leftvt == VT_CY || rightvt == VT_CY ||
2974 leftvt == VT_DATE || rightvt == VT_DATE ||
2975 leftvt == VT_I1 || rightvt == VT_I1 ||
2976 leftvt == VT_UI2 || rightvt == VT_UI2 ||
2977 leftvt == VT_UI4 || rightvt == VT_UI4 ||
2978 leftvt == VT_UI8 || rightvt == VT_UI8 ||
2979 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
2980 resvt = VT_I4;
2981 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
2982 leftvt == VT_I2 || rightvt == VT_I2 ||
2983 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
2984 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
2985 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
2986 (leftvt == VT_UI1 && rightvt == VT_UI1))
2987 resvt = VT_UI1;
2988 else
2989 resvt = VT_I2;
2990 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
2991 (leftvt == VT_BSTR && rightvt == VT_BSTR))
2992 resvt = VT_BOOL;
2993 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
2994 leftvt == VT_BSTR || rightvt == VT_BSTR)
2995 resvt = VT_NULL;
2996 else
2998 hres = DISP_E_BADVARTYPE;
2999 goto VarAnd_Exit;
3002 if (leftvt == VT_NULL || rightvt == VT_NULL)
3005 * Special cases for when left variant is VT_NULL
3006 * (NULL & 0 = NULL, NULL & value = value)
3008 if (leftvt == VT_NULL)
3010 VARIANT_BOOL b;
3011 switch(rightvt)
3013 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3014 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3015 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3016 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3017 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3018 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3019 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3020 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3021 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3022 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3023 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3024 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3025 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3026 case VT_CY:
3027 if(V_CY(right).int64)
3028 resvt = VT_NULL;
3029 break;
3030 case VT_DECIMAL:
3031 if (DEC_HI32(&V_DECIMAL(right)) ||
3032 DEC_LO64(&V_DECIMAL(right)))
3033 resvt = VT_NULL;
3034 break;
3035 case VT_BSTR:
3036 hres = VarBoolFromStr(V_BSTR(right),
3037 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3038 if (FAILED(hres))
3039 return hres;
3040 else if (b)
3041 V_VT(result) = VT_NULL;
3042 else
3044 V_VT(result) = VT_BOOL;
3045 V_BOOL(result) = b;
3047 goto VarAnd_Exit;
3050 V_VT(result) = resvt;
3051 goto VarAnd_Exit;
3054 hres = VariantCopy(&varLeft, left);
3055 if (FAILED(hres)) goto VarAnd_Exit;
3057 hres = VariantCopy(&varRight, right);
3058 if (FAILED(hres)) goto VarAnd_Exit;
3060 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3061 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3062 else
3064 double d;
3066 if (V_VT(&varLeft) == VT_BSTR &&
3067 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3068 LOCALE_USER_DEFAULT, 0, &d)))
3069 hres = VariantChangeType(&varLeft,&varLeft,
3070 VARIANT_LOCALBOOL, VT_BOOL);
3071 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3072 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3073 if (FAILED(hres)) goto VarAnd_Exit;
3076 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3077 V_VT(&varRight) = VT_I4; /* Don't overflow */
3078 else
3080 double d;
3082 if (V_VT(&varRight) == VT_BSTR &&
3083 FAILED(VarR8FromStr(V_BSTR(&varRight),
3084 LOCALE_USER_DEFAULT, 0, &d)))
3085 hres = VariantChangeType(&varRight, &varRight,
3086 VARIANT_LOCALBOOL, VT_BOOL);
3087 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3088 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3089 if (FAILED(hres)) goto VarAnd_Exit;
3092 V_VT(result) = resvt;
3093 switch(resvt)
3095 case VT_I8:
3096 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3097 break;
3098 case VT_I4:
3099 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3100 break;
3101 case VT_I2:
3102 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3103 break;
3104 case VT_UI1:
3105 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3106 break;
3107 case VT_BOOL:
3108 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3109 break;
3110 default:
3111 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3112 leftvt,rightvt);
3115 VarAnd_Exit:
3116 VariantClear(&varLeft);
3117 VariantClear(&varRight);
3118 VariantClear(&tempLeft);
3119 VariantClear(&tempRight);
3121 return hres;
3124 /**********************************************************************
3125 * VarAdd [OLEAUT32.141]
3127 * Add two variants.
3129 * PARAMS
3130 * left [I] First variant
3131 * right [I] Second variant
3132 * result [O] Result variant
3134 * RETURNS
3135 * Success: S_OK.
3136 * Failure: An HRESULT error code indicating the error.
3138 * NOTES
3139 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3140 * UI8, INT and UINT as input variants.
3142 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3143 * same here.
3145 * FIXME
3146 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3147 * case.
3149 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3151 HRESULT hres;
3152 VARTYPE lvt, rvt, resvt, tvt;
3153 VARIANT lv, rv, tv;
3154 VARIANT tempLeft, tempRight;
3155 double r8res;
3157 /* Variant priority for coercion. Sorted from lowest to highest.
3158 VT_ERROR shows an invalid input variant type. */
3159 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3160 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3161 vt_ERROR };
3162 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3163 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3164 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3165 VT_NULL, VT_ERROR };
3167 /* Mapping for coercion from input variant to priority of result variant. */
3168 static const VARTYPE coerce[] = {
3169 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3170 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3171 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3172 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3173 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3174 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3175 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3176 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3179 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3180 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3181 result);
3183 VariantInit(&lv);
3184 VariantInit(&rv);
3185 VariantInit(&tv);
3186 VariantInit(&tempLeft);
3187 VariantInit(&tempRight);
3189 /* Handle VT_DISPATCH by storing and taking address of returned value */
3190 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3192 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3194 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3195 if (FAILED(hres)) goto end;
3196 left = &tempLeft;
3198 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3200 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3201 if (FAILED(hres)) goto end;
3202 right = &tempRight;
3206 lvt = V_VT(left)&VT_TYPEMASK;
3207 rvt = V_VT(right)&VT_TYPEMASK;
3209 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3210 Same for any input variant type > VT_I8 */
3211 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3212 lvt > VT_I8 || rvt > VT_I8) {
3213 hres = DISP_E_BADVARTYPE;
3214 goto end;
3217 /* Determine the variant type to coerce to. */
3218 if (coerce[lvt] > coerce[rvt]) {
3219 resvt = prio2vt[coerce[lvt]];
3220 tvt = prio2vt[coerce[rvt]];
3221 } else {
3222 resvt = prio2vt[coerce[rvt]];
3223 tvt = prio2vt[coerce[lvt]];
3226 /* Special cases where the result variant type is defined by both
3227 input variants and not only that with the highest priority */
3228 if (resvt == VT_BSTR) {
3229 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3230 resvt = VT_BSTR;
3231 else
3232 resvt = VT_R8;
3234 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3235 resvt = VT_R8;
3237 /* For overflow detection use the biggest compatible type for the
3238 addition */
3239 switch (resvt) {
3240 case VT_ERROR:
3241 hres = DISP_E_BADVARTYPE;
3242 goto end;
3243 case VT_NULL:
3244 hres = S_OK;
3245 V_VT(result) = VT_NULL;
3246 goto end;
3247 case VT_DISPATCH:
3248 FIXME("cannot handle variant type VT_DISPATCH\n");
3249 hres = DISP_E_TYPEMISMATCH;
3250 goto end;
3251 case VT_EMPTY:
3252 resvt = VT_I2;
3253 /* Fall through */
3254 case VT_UI1:
3255 case VT_I2:
3256 case VT_I4:
3257 case VT_I8:
3258 tvt = VT_I8;
3259 break;
3260 case VT_DATE:
3261 case VT_R4:
3262 tvt = VT_R8;
3263 break;
3264 default:
3265 tvt = resvt;
3268 /* Now coerce the variants */
3269 hres = VariantChangeType(&lv, left, 0, tvt);
3270 if (FAILED(hres))
3271 goto end;
3272 hres = VariantChangeType(&rv, right, 0, tvt);
3273 if (FAILED(hres))
3274 goto end;
3276 /* Do the math */
3277 hres = S_OK;
3278 V_VT(result) = resvt;
3279 switch (tvt) {
3280 case VT_DECIMAL:
3281 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3282 &V_DECIMAL(result));
3283 goto end;
3284 case VT_CY:
3285 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3286 goto end;
3287 case VT_BSTR:
3288 /* We do not add those, we concatenate them. */
3289 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3290 goto end;
3291 case VT_I8:
3292 /* Overflow detection */
3293 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3294 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3295 V_VT(result) = VT_R8;
3296 V_R8(result) = r8res;
3297 goto end;
3298 } else {
3299 V_VT(&tv) = tvt;
3300 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3302 break;
3303 case VT_R8:
3304 V_VT(&tv) = tvt;
3305 /* FIXME: overflow detection */
3306 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3307 break;
3308 default:
3309 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3310 break;
3312 if (resvt != tvt) {
3313 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3314 /* Overflow! Change to the vartype with the next higher priority.
3315 With one exception: I4 ==> R8 even if it would fit in I8 */
3316 if (resvt == VT_I4)
3317 resvt = VT_R8;
3318 else
3319 resvt = prio2vt[coerce[resvt] + 1];
3320 hres = VariantChangeType(result, &tv, 0, resvt);
3322 } else
3323 hres = VariantCopy(result, &tv);
3325 end:
3326 if (hres != S_OK) {
3327 V_VT(result) = VT_EMPTY;
3328 V_I4(result) = 0; /* No V_EMPTY */
3330 VariantClear(&lv);
3331 VariantClear(&rv);
3332 VariantClear(&tv);
3333 VariantClear(&tempLeft);
3334 VariantClear(&tempRight);
3335 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3336 return hres;
3339 /**********************************************************************
3340 * VarMul [OLEAUT32.156]
3342 * Multiply two variants.
3344 * PARAMS
3345 * left [I] First variant
3346 * right [I] Second variant
3347 * result [O] Result variant
3349 * RETURNS
3350 * Success: S_OK.
3351 * Failure: An HRESULT error code indicating the error.
3353 * NOTES
3354 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3355 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3357 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3358 * same here.
3360 * FIXME
3361 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3362 * case.
3364 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3366 HRESULT hres;
3367 VARTYPE lvt, rvt, resvt, tvt;
3368 VARIANT lv, rv, tv;
3369 VARIANT tempLeft, tempRight;
3370 double r8res;
3372 /* Variant priority for coercion. Sorted from lowest to highest.
3373 VT_ERROR shows an invalid input variant type. */
3374 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3375 vt_DECIMAL, vt_NULL, vt_ERROR };
3376 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3377 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3378 VT_DECIMAL, VT_NULL, VT_ERROR };
3380 /* Mapping for coercion from input variant to priority of result variant. */
3381 static const VARTYPE coerce[] = {
3382 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3383 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3384 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3385 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3386 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3387 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3388 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3389 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3392 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3393 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3394 result);
3396 VariantInit(&lv);
3397 VariantInit(&rv);
3398 VariantInit(&tv);
3399 VariantInit(&tempLeft);
3400 VariantInit(&tempRight);
3402 /* Handle VT_DISPATCH by storing and taking address of returned value */
3403 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3405 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3406 if (FAILED(hres)) goto end;
3407 left = &tempLeft;
3409 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3411 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3412 if (FAILED(hres)) goto end;
3413 right = &tempRight;
3416 lvt = V_VT(left)&VT_TYPEMASK;
3417 rvt = V_VT(right)&VT_TYPEMASK;
3419 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3420 Same for any input variant type > VT_I8 */
3421 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3422 lvt > VT_I8 || rvt > VT_I8) {
3423 hres = DISP_E_BADVARTYPE;
3424 goto end;
3427 /* Determine the variant type to coerce to. */
3428 if (coerce[lvt] > coerce[rvt]) {
3429 resvt = prio2vt[coerce[lvt]];
3430 tvt = prio2vt[coerce[rvt]];
3431 } else {
3432 resvt = prio2vt[coerce[rvt]];
3433 tvt = prio2vt[coerce[lvt]];
3436 /* Special cases where the result variant type is defined by both
3437 input variants and not only that with the highest priority */
3438 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3439 resvt = VT_R8;
3440 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3441 resvt = VT_I2;
3443 /* For overflow detection use the biggest compatible type for the
3444 multiplication */
3445 switch (resvt) {
3446 case VT_ERROR:
3447 hres = DISP_E_BADVARTYPE;
3448 goto end;
3449 case VT_NULL:
3450 hres = S_OK;
3451 V_VT(result) = VT_NULL;
3452 goto end;
3453 case VT_UI1:
3454 case VT_I2:
3455 case VT_I4:
3456 case VT_I8:
3457 tvt = VT_I8;
3458 break;
3459 case VT_R4:
3460 tvt = VT_R8;
3461 break;
3462 default:
3463 tvt = resvt;
3466 /* Now coerce the variants */
3467 hres = VariantChangeType(&lv, left, 0, tvt);
3468 if (FAILED(hres))
3469 goto end;
3470 hres = VariantChangeType(&rv, right, 0, tvt);
3471 if (FAILED(hres))
3472 goto end;
3474 /* Do the math */
3475 hres = S_OK;
3476 V_VT(&tv) = tvt;
3477 V_VT(result) = resvt;
3478 switch (tvt) {
3479 case VT_DECIMAL:
3480 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3481 &V_DECIMAL(result));
3482 goto end;
3483 case VT_CY:
3484 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3485 goto end;
3486 case VT_I8:
3487 /* Overflow detection */
3488 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3489 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3490 V_VT(result) = VT_R8;
3491 V_R8(result) = r8res;
3492 goto end;
3493 } else
3494 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3495 break;
3496 case VT_R8:
3497 /* FIXME: overflow detection */
3498 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3499 break;
3500 default:
3501 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3502 break;
3504 if (resvt != tvt) {
3505 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3506 /* Overflow! Change to the vartype with the next higher priority.
3507 With one exception: I4 ==> R8 even if it would fit in I8 */
3508 if (resvt == VT_I4)
3509 resvt = VT_R8;
3510 else
3511 resvt = prio2vt[coerce[resvt] + 1];
3513 } else
3514 hres = VariantCopy(result, &tv);
3516 end:
3517 if (hres != S_OK) {
3518 V_VT(result) = VT_EMPTY;
3519 V_I4(result) = 0; /* No V_EMPTY */
3521 VariantClear(&lv);
3522 VariantClear(&rv);
3523 VariantClear(&tv);
3524 VariantClear(&tempLeft);
3525 VariantClear(&tempRight);
3526 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3527 return hres;
3530 /**********************************************************************
3531 * VarDiv [OLEAUT32.143]
3533 * Divides one variant with another.
3535 * PARAMS
3536 * left [I] First variant
3537 * right [I] Second variant
3538 * result [O] Result variant
3540 * RETURNS
3541 * Success: S_OK.
3542 * Failure: An HRESULT error code indicating the error.
3544 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3546 HRESULT hres = S_OK;
3547 VARTYPE resvt = VT_EMPTY;
3548 VARTYPE leftvt,rightvt;
3549 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3550 VARIANT lv,rv;
3551 VARIANT tempLeft, tempRight;
3553 VariantInit(&tempLeft);
3554 VariantInit(&tempRight);
3555 VariantInit(&lv);
3556 VariantInit(&rv);
3558 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3559 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3561 /* Handle VT_DISPATCH by storing and taking address of returned value */
3562 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3564 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3565 if (FAILED(hres)) goto end;
3566 left = &tempLeft;
3568 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3570 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3571 if (FAILED(hres)) goto end;
3572 right = &tempRight;
3575 leftvt = V_VT(left)&VT_TYPEMASK;
3576 rightvt = V_VT(right)&VT_TYPEMASK;
3577 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3578 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3580 if (leftExtraFlags != rightExtraFlags)
3582 hres = DISP_E_BADVARTYPE;
3583 goto end;
3585 ExtraFlags = leftExtraFlags;
3587 /* Native VarDiv always returns an error when using extra flags */
3588 if (ExtraFlags != 0)
3590 hres = DISP_E_BADVARTYPE;
3591 goto end;
3594 /* Determine return type */
3595 if (!(rightvt == VT_EMPTY))
3597 if (leftvt == VT_NULL || rightvt == VT_NULL)
3599 V_VT(result) = VT_NULL;
3600 hres = S_OK;
3601 goto end;
3603 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3604 resvt = VT_DECIMAL;
3605 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3606 leftvt == VT_CY || rightvt == VT_CY ||
3607 leftvt == VT_DATE || rightvt == VT_DATE ||
3608 leftvt == VT_I4 || rightvt == VT_I4 ||
3609 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3610 leftvt == VT_I2 || rightvt == VT_I2 ||
3611 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3612 leftvt == VT_R8 || rightvt == VT_R8 ||
3613 leftvt == VT_UI1 || rightvt == VT_UI1)
3615 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3616 (leftvt == VT_R4 && rightvt == VT_UI1))
3617 resvt = VT_R4;
3618 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3619 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3620 (leftvt == VT_BOOL || leftvt == VT_I2)))
3621 resvt = VT_R4;
3622 else
3623 resvt = VT_R8;
3625 else if (leftvt == VT_R4 || rightvt == VT_R4)
3626 resvt = VT_R4;
3628 else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
3630 V_VT(result) = VT_NULL;
3631 hres = S_OK;
3632 goto end;
3634 else
3636 hres = DISP_E_BADVARTYPE;
3637 goto end;
3640 /* coerce to the result type */
3641 hres = VariantChangeType(&lv, left, 0, resvt);
3642 if (hres != S_OK) goto end;
3644 hres = VariantChangeType(&rv, right, 0, resvt);
3645 if (hres != S_OK) goto end;
3647 /* do the math */
3648 V_VT(result) = resvt;
3649 switch (resvt)
3651 case VT_R4:
3652 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3654 hres = DISP_E_OVERFLOW;
3655 V_VT(result) = VT_EMPTY;
3657 else if (V_R4(&rv) == 0.0)
3659 hres = DISP_E_DIVBYZERO;
3660 V_VT(result) = VT_EMPTY;
3662 else
3663 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3664 break;
3665 case VT_R8:
3666 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3668 hres = DISP_E_OVERFLOW;
3669 V_VT(result) = VT_EMPTY;
3671 else if (V_R8(&rv) == 0.0)
3673 hres = DISP_E_DIVBYZERO;
3674 V_VT(result) = VT_EMPTY;
3676 else
3677 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3678 break;
3679 case VT_DECIMAL:
3680 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3681 break;
3684 end:
3685 VariantClear(&lv);
3686 VariantClear(&rv);
3687 VariantClear(&tempLeft);
3688 VariantClear(&tempRight);
3689 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3690 return hres;
3693 /**********************************************************************
3694 * VarSub [OLEAUT32.159]
3696 * Subtract two variants.
3698 * PARAMS
3699 * left [I] First variant
3700 * right [I] Second variant
3701 * result [O] Result variant
3703 * RETURNS
3704 * Success: S_OK.
3705 * Failure: An HRESULT error code indicating the error.
3707 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3709 HRESULT hres = S_OK;
3710 VARTYPE resvt = VT_EMPTY;
3711 VARTYPE leftvt,rightvt;
3712 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3713 VARIANT lv,rv;
3714 VARIANT tempLeft, tempRight;
3716 VariantInit(&lv);
3717 VariantInit(&rv);
3718 VariantInit(&tempLeft);
3719 VariantInit(&tempRight);
3721 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3722 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3724 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3725 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3726 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3728 if (NULL == V_DISPATCH(left)) {
3729 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3730 hres = DISP_E_BADVARTYPE;
3731 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3732 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3733 hres = DISP_E_BADVARTYPE;
3734 else switch (V_VT(right) & VT_TYPEMASK)
3736 case VT_VARIANT:
3737 case VT_UNKNOWN:
3738 case 15:
3739 case VT_I1:
3740 case VT_UI2:
3741 case VT_UI4:
3742 hres = DISP_E_BADVARTYPE;
3744 if (FAILED(hres)) goto end;
3746 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3747 if (FAILED(hres)) goto end;
3748 left = &tempLeft;
3750 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3751 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3752 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3754 if (NULL == V_DISPATCH(right))
3756 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3757 hres = DISP_E_BADVARTYPE;
3758 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3759 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3760 hres = DISP_E_BADVARTYPE;
3761 else switch (V_VT(left) & VT_TYPEMASK)
3763 case VT_VARIANT:
3764 case VT_UNKNOWN:
3765 case 15:
3766 case VT_I1:
3767 case VT_UI2:
3768 case VT_UI4:
3769 hres = DISP_E_BADVARTYPE;
3771 if (FAILED(hres)) goto end;
3773 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3774 if (FAILED(hres)) goto end;
3775 right = &tempRight;
3778 leftvt = V_VT(left)&VT_TYPEMASK;
3779 rightvt = V_VT(right)&VT_TYPEMASK;
3780 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3781 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3783 if (leftExtraFlags != rightExtraFlags)
3785 hres = DISP_E_BADVARTYPE;
3786 goto end;
3788 ExtraFlags = leftExtraFlags;
3790 /* determine return type and return code */
3791 /* All extra flags produce errors */
3792 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3793 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3794 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3795 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3796 ExtraFlags == VT_VECTOR ||
3797 ExtraFlags == VT_BYREF ||
3798 ExtraFlags == VT_RESERVED)
3800 hres = DISP_E_BADVARTYPE;
3801 goto end;
3803 else if (ExtraFlags >= VT_ARRAY)
3805 hres = DISP_E_TYPEMISMATCH;
3806 goto end;
3808 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3809 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3810 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3811 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3812 leftvt == VT_I1 || rightvt == VT_I1 ||
3813 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3814 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3815 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3816 leftvt == VT_INT || rightvt == VT_INT ||
3817 leftvt == VT_UINT || rightvt == VT_UINT ||
3818 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3819 leftvt == VT_RECORD || rightvt == VT_RECORD)
3821 if (leftvt == VT_RECORD && rightvt == VT_I8)
3822 hres = DISP_E_TYPEMISMATCH;
3823 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3824 hres = DISP_E_TYPEMISMATCH;
3825 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3826 hres = DISP_E_TYPEMISMATCH;
3827 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3828 hres = DISP_E_TYPEMISMATCH;
3829 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3830 hres = DISP_E_BADVARTYPE;
3831 else
3832 hres = DISP_E_BADVARTYPE;
3833 goto end;
3835 /* The following flags/types are invalid for left variant */
3836 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3837 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3838 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3840 hres = DISP_E_BADVARTYPE;
3841 goto end;
3843 /* The following flags/types are invalid for right variant */
3844 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3845 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3846 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3848 hres = DISP_E_BADVARTYPE;
3849 goto end;
3851 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3852 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3853 resvt = VT_NULL;
3854 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3855 leftvt == VT_ERROR || rightvt == VT_ERROR)
3857 hres = DISP_E_TYPEMISMATCH;
3858 goto end;
3860 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3861 resvt = VT_NULL;
3862 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3863 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3864 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3865 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3866 resvt = VT_R8;
3867 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3868 resvt = VT_DECIMAL;
3869 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3870 resvt = VT_DATE;
3871 else if (leftvt == VT_CY || rightvt == VT_CY)
3872 resvt = VT_CY;
3873 else if (leftvt == VT_R8 || rightvt == VT_R8)
3874 resvt = VT_R8;
3875 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3876 resvt = VT_R8;
3877 else if (leftvt == VT_R4 || rightvt == VT_R4)
3879 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3880 leftvt == VT_I8 || rightvt == VT_I8)
3881 resvt = VT_R8;
3882 else
3883 resvt = VT_R4;
3885 else if (leftvt == VT_I8 || rightvt == VT_I8)
3886 resvt = VT_I8;
3887 else if (leftvt == VT_I4 || rightvt == VT_I4)
3888 resvt = VT_I4;
3889 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3890 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3891 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3892 resvt = VT_I2;
3893 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3894 resvt = VT_UI1;
3895 else
3897 hres = DISP_E_TYPEMISMATCH;
3898 goto end;
3901 /* coerce to the result type */
3902 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3903 hres = VariantChangeType(&lv, left, 0, VT_R8);
3904 else
3905 hres = VariantChangeType(&lv, left, 0, resvt);
3906 if (hres != S_OK) goto end;
3907 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3908 hres = VariantChangeType(&rv, right, 0, VT_R8);
3909 else
3910 hres = VariantChangeType(&rv, right, 0, resvt);
3911 if (hres != S_OK) goto end;
3913 /* do the math */
3914 V_VT(result) = resvt;
3915 switch (resvt)
3917 case VT_NULL:
3918 break;
3919 case VT_DATE:
3920 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3921 break;
3922 case VT_CY:
3923 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3924 break;
3925 case VT_R4:
3926 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3927 break;
3928 case VT_I8:
3929 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3930 break;
3931 case VT_I4:
3932 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3933 break;
3934 case VT_I2:
3935 V_I2(result) = V_I2(&lv) - V_I2(&rv);
3936 break;
3937 case VT_I1:
3938 V_I1(result) = V_I1(&lv) - V_I1(&rv);
3939 break;
3940 case VT_UI1:
3941 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
3942 break;
3943 case VT_R8:
3944 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3945 break;
3946 case VT_DECIMAL:
3947 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3948 break;
3951 end:
3952 VariantClear(&lv);
3953 VariantClear(&rv);
3954 VariantClear(&tempLeft);
3955 VariantClear(&tempRight);
3956 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3957 return hres;
3961 /**********************************************************************
3962 * VarOr [OLEAUT32.157]
3964 * Perform a logical or (OR) operation on two variants.
3966 * PARAMS
3967 * pVarLeft [I] First variant
3968 * pVarRight [I] Variant to OR with pVarLeft
3969 * pVarOut [O] Destination for OR result
3971 * RETURNS
3972 * Success: S_OK. pVarOut contains the result of the operation with its type
3973 * taken from the table listed under VarXor().
3974 * Failure: An HRESULT error code indicating the error.
3976 * NOTES
3977 * See the Notes section of VarXor() for further information.
3979 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3981 VARTYPE vt = VT_I4;
3982 VARIANT varLeft, varRight, varStr;
3983 HRESULT hRet;
3984 VARIANT tempLeft, tempRight;
3986 VariantInit(&tempLeft);
3987 VariantInit(&tempRight);
3988 VariantInit(&varLeft);
3989 VariantInit(&varRight);
3990 VariantInit(&varStr);
3992 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3993 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3994 debugstr_VF(pVarRight), pVarOut);
3996 /* Handle VT_DISPATCH by storing and taking address of returned value */
3997 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
3999 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4000 if (FAILED(hRet)) goto VarOr_Exit;
4001 pVarLeft = &tempLeft;
4003 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4005 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4006 if (FAILED(hRet)) goto VarOr_Exit;
4007 pVarRight = &tempRight;
4010 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4011 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4012 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
4013 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
4015 hRet = DISP_E_BADVARTYPE;
4016 goto VarOr_Exit;
4019 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
4021 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4023 /* NULL OR Zero is NULL, NULL OR value is value */
4024 if (V_VT(pVarLeft) == VT_NULL)
4025 pVarLeft = pVarRight; /* point to the non-NULL var */
4027 V_VT(pVarOut) = VT_NULL;
4028 V_I4(pVarOut) = 0;
4030 switch (V_VT(pVarLeft))
4032 case VT_DATE: case VT_R8:
4033 if (V_R8(pVarLeft))
4034 goto VarOr_AsEmpty;
4035 hRet = S_OK;
4036 goto VarOr_Exit;
4037 case VT_BOOL:
4038 if (V_BOOL(pVarLeft))
4039 *pVarOut = *pVarLeft;
4040 hRet = S_OK;
4041 goto VarOr_Exit;
4042 case VT_I2: case VT_UI2:
4043 if (V_I2(pVarLeft))
4044 goto VarOr_AsEmpty;
4045 hRet = S_OK;
4046 goto VarOr_Exit;
4047 case VT_I1:
4048 if (V_I1(pVarLeft))
4049 goto VarOr_AsEmpty;
4050 hRet = S_OK;
4051 goto VarOr_Exit;
4052 case VT_UI1:
4053 if (V_UI1(pVarLeft))
4054 *pVarOut = *pVarLeft;
4055 hRet = S_OK;
4056 goto VarOr_Exit;
4057 case VT_R4:
4058 if (V_R4(pVarLeft))
4059 goto VarOr_AsEmpty;
4060 hRet = S_OK;
4061 goto VarOr_Exit;
4062 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4063 if (V_I4(pVarLeft))
4064 goto VarOr_AsEmpty;
4065 hRet = S_OK;
4066 goto VarOr_Exit;
4067 case VT_CY:
4068 if (V_CY(pVarLeft).int64)
4069 goto VarOr_AsEmpty;
4070 hRet = S_OK;
4071 goto VarOr_Exit;
4072 case VT_I8: case VT_UI8:
4073 if (V_I8(pVarLeft))
4074 goto VarOr_AsEmpty;
4075 hRet = S_OK;
4076 goto VarOr_Exit;
4077 case VT_DECIMAL:
4078 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4079 goto VarOr_AsEmpty;
4080 hRet = S_OK;
4081 goto VarOr_Exit;
4082 case VT_BSTR:
4084 VARIANT_BOOL b;
4086 if (!V_BSTR(pVarLeft))
4088 hRet = DISP_E_BADVARTYPE;
4089 goto VarOr_Exit;
4092 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4093 if (SUCCEEDED(hRet) && b)
4095 V_VT(pVarOut) = VT_BOOL;
4096 V_BOOL(pVarOut) = b;
4098 goto VarOr_Exit;
4100 case VT_NULL: case VT_EMPTY:
4101 V_VT(pVarOut) = VT_NULL;
4102 hRet = S_OK;
4103 goto VarOr_Exit;
4104 default:
4105 hRet = DISP_E_BADVARTYPE;
4106 goto VarOr_Exit;
4110 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4112 if (V_VT(pVarLeft) == VT_EMPTY)
4113 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4115 VarOr_AsEmpty:
4116 /* Since one argument is empty (0), OR'ing it with the other simply
4117 * gives the others value (as 0|x => x). So just convert the other
4118 * argument to the required result type.
4120 switch (V_VT(pVarLeft))
4122 case VT_BSTR:
4123 if (!V_BSTR(pVarLeft))
4125 hRet = DISP_E_BADVARTYPE;
4126 goto VarOr_Exit;
4129 hRet = VariantCopy(&varStr, pVarLeft);
4130 if (FAILED(hRet))
4131 goto VarOr_Exit;
4132 pVarLeft = &varStr;
4133 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4134 if (FAILED(hRet))
4135 goto VarOr_Exit;
4136 /* Fall Through ... */
4137 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4138 V_VT(pVarOut) = VT_I2;
4139 break;
4140 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4141 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4142 case VT_INT: case VT_UINT: case VT_UI8:
4143 V_VT(pVarOut) = VT_I4;
4144 break;
4145 case VT_I8:
4146 V_VT(pVarOut) = VT_I8;
4147 break;
4148 default:
4149 hRet = DISP_E_BADVARTYPE;
4150 goto VarOr_Exit;
4152 hRet = VariantCopy(&varLeft, pVarLeft);
4153 if (FAILED(hRet))
4154 goto VarOr_Exit;
4155 pVarLeft = &varLeft;
4156 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4157 goto VarOr_Exit;
4160 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4162 V_VT(pVarOut) = VT_BOOL;
4163 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4164 hRet = S_OK;
4165 goto VarOr_Exit;
4168 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4170 V_VT(pVarOut) = VT_UI1;
4171 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4172 hRet = S_OK;
4173 goto VarOr_Exit;
4176 if (V_VT(pVarLeft) == VT_BSTR)
4178 hRet = VariantCopy(&varStr, pVarLeft);
4179 if (FAILED(hRet))
4180 goto VarOr_Exit;
4181 pVarLeft = &varStr;
4182 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4183 if (FAILED(hRet))
4184 goto VarOr_Exit;
4187 if (V_VT(pVarLeft) == VT_BOOL &&
4188 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4190 vt = VT_BOOL;
4192 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4193 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4194 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4195 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4197 vt = VT_I2;
4199 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4201 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4203 hRet = DISP_E_TYPEMISMATCH;
4204 goto VarOr_Exit;
4206 vt = VT_I8;
4209 hRet = VariantCopy(&varLeft, pVarLeft);
4210 if (FAILED(hRet))
4211 goto VarOr_Exit;
4213 hRet = VariantCopy(&varRight, pVarRight);
4214 if (FAILED(hRet))
4215 goto VarOr_Exit;
4217 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4218 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4219 else
4221 double d;
4223 if (V_VT(&varLeft) == VT_BSTR &&
4224 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4225 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4226 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4227 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4228 if (FAILED(hRet))
4229 goto VarOr_Exit;
4232 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4233 V_VT(&varRight) = VT_I4; /* Don't overflow */
4234 else
4236 double d;
4238 if (V_VT(&varRight) == VT_BSTR &&
4239 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4240 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4241 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4242 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4243 if (FAILED(hRet))
4244 goto VarOr_Exit;
4247 V_VT(pVarOut) = vt;
4248 if (vt == VT_I8)
4250 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4252 else if (vt == VT_I4)
4254 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4256 else
4258 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4261 VarOr_Exit:
4262 VariantClear(&varStr);
4263 VariantClear(&varLeft);
4264 VariantClear(&varRight);
4265 VariantClear(&tempLeft);
4266 VariantClear(&tempRight);
4267 return hRet;
4270 /**********************************************************************
4271 * VarAbs [OLEAUT32.168]
4273 * Convert a variant to its absolute value.
4275 * PARAMS
4276 * pVarIn [I] Source variant
4277 * pVarOut [O] Destination for converted value
4279 * RETURNS
4280 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4281 * Failure: An HRESULT error code indicating the error.
4283 * NOTES
4284 * - This function does not process by-reference variants.
4285 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4286 * according to the following table:
4287 *| Input Type Output Type
4288 *| ---------- -----------
4289 *| VT_BOOL VT_I2
4290 *| VT_BSTR VT_R8
4291 *| (All others) Unchanged
4293 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4295 VARIANT varIn;
4296 HRESULT hRet = S_OK;
4297 VARIANT temp;
4299 VariantInit(&temp);
4301 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4302 debugstr_VF(pVarIn), pVarOut);
4304 /* Handle VT_DISPATCH by storing and taking address of returned value */
4305 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4307 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4308 if (FAILED(hRet)) goto VarAbs_Exit;
4309 pVarIn = &temp;
4312 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4313 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4314 V_VT(pVarIn) == VT_ERROR)
4316 hRet = DISP_E_TYPEMISMATCH;
4317 goto VarAbs_Exit;
4319 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4321 #define ABS_CASE(typ,min) \
4322 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4323 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4324 break
4326 switch (V_VT(pVarIn))
4328 ABS_CASE(I1,I1_MIN);
4329 case VT_BOOL:
4330 V_VT(pVarOut) = VT_I2;
4331 /* BOOL->I2, Fall through ... */
4332 ABS_CASE(I2,I2_MIN);
4333 case VT_INT:
4334 ABS_CASE(I4,I4_MIN);
4335 ABS_CASE(I8,I8_MIN);
4336 ABS_CASE(R4,R4_MIN);
4337 case VT_BSTR:
4338 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4339 if (FAILED(hRet))
4340 break;
4341 V_VT(pVarOut) = VT_R8;
4342 pVarIn = &varIn;
4343 /* Fall through ... */
4344 case VT_DATE:
4345 ABS_CASE(R8,R8_MIN);
4346 case VT_CY:
4347 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4348 break;
4349 case VT_DECIMAL:
4350 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4351 break;
4352 case VT_UI1:
4353 case VT_UI2:
4354 case VT_UINT:
4355 case VT_UI4:
4356 case VT_UI8:
4357 /* No-Op */
4358 break;
4359 case VT_EMPTY:
4360 V_VT(pVarOut) = VT_I2;
4361 case VT_NULL:
4362 V_I2(pVarOut) = 0;
4363 break;
4364 default:
4365 hRet = DISP_E_BADVARTYPE;
4368 VarAbs_Exit:
4369 VariantClear(&temp);
4370 return hRet;
4373 /**********************************************************************
4374 * VarFix [OLEAUT32.169]
4376 * Truncate a variants value to a whole number.
4378 * PARAMS
4379 * pVarIn [I] Source variant
4380 * pVarOut [O] Destination for converted value
4382 * RETURNS
4383 * Success: S_OK. pVarOut contains the converted value.
4384 * Failure: An HRESULT error code indicating the error.
4386 * NOTES
4387 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4388 * according to the following table:
4389 *| Input Type Output Type
4390 *| ---------- -----------
4391 *| VT_BOOL VT_I2
4392 *| VT_EMPTY VT_I2
4393 *| VT_BSTR VT_R8
4394 *| All Others Unchanged
4395 * - The difference between this function and VarInt() is that VarInt() rounds
4396 * negative numbers away from 0, while this function rounds them towards zero.
4398 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4400 HRESULT hRet = S_OK;
4401 VARIANT temp;
4403 VariantInit(&temp);
4405 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4406 debugstr_VF(pVarIn), pVarOut);
4408 /* Handle VT_DISPATCH by storing and taking address of returned value */
4409 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4411 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4412 if (FAILED(hRet)) goto VarFix_Exit;
4413 pVarIn = &temp;
4415 V_VT(pVarOut) = V_VT(pVarIn);
4417 switch (V_VT(pVarIn))
4419 case VT_UI1:
4420 V_UI1(pVarOut) = V_UI1(pVarIn);
4421 break;
4422 case VT_BOOL:
4423 V_VT(pVarOut) = VT_I2;
4424 /* Fall through */
4425 case VT_I2:
4426 V_I2(pVarOut) = V_I2(pVarIn);
4427 break;
4428 case VT_I4:
4429 V_I4(pVarOut) = V_I4(pVarIn);
4430 break;
4431 case VT_I8:
4432 V_I8(pVarOut) = V_I8(pVarIn);
4433 break;
4434 case VT_R4:
4435 if (V_R4(pVarIn) < 0.0f)
4436 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4437 else
4438 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4439 break;
4440 case VT_BSTR:
4441 V_VT(pVarOut) = VT_R8;
4442 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4443 pVarIn = pVarOut;
4444 /* Fall through */
4445 case VT_DATE:
4446 case VT_R8:
4447 if (V_R8(pVarIn) < 0.0)
4448 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4449 else
4450 V_R8(pVarOut) = floor(V_R8(pVarIn));
4451 break;
4452 case VT_CY:
4453 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4454 break;
4455 case VT_DECIMAL:
4456 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4457 break;
4458 case VT_EMPTY:
4459 V_VT(pVarOut) = VT_I2;
4460 V_I2(pVarOut) = 0;
4461 break;
4462 case VT_NULL:
4463 /* No-Op */
4464 break;
4465 default:
4466 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4467 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4468 hRet = DISP_E_BADVARTYPE;
4469 else
4470 hRet = DISP_E_TYPEMISMATCH;
4472 VarFix_Exit:
4473 if (FAILED(hRet))
4474 V_VT(pVarOut) = VT_EMPTY;
4475 VariantClear(&temp);
4477 return hRet;
4480 /**********************************************************************
4481 * VarInt [OLEAUT32.172]
4483 * Truncate a variants value to a whole number.
4485 * PARAMS
4486 * pVarIn [I] Source variant
4487 * pVarOut [O] Destination for converted value
4489 * RETURNS
4490 * Success: S_OK. pVarOut contains the converted value.
4491 * Failure: An HRESULT error code indicating the error.
4493 * NOTES
4494 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4495 * according to the following table:
4496 *| Input Type Output Type
4497 *| ---------- -----------
4498 *| VT_BOOL VT_I2
4499 *| VT_EMPTY VT_I2
4500 *| VT_BSTR VT_R8
4501 *| All Others Unchanged
4502 * - The difference between this function and VarFix() is that VarFix() rounds
4503 * negative numbers towards 0, while this function rounds them away from zero.
4505 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4507 HRESULT hRet = S_OK;
4508 VARIANT temp;
4510 VariantInit(&temp);
4512 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4513 debugstr_VF(pVarIn), pVarOut);
4515 /* Handle VT_DISPATCH by storing and taking address of returned value */
4516 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4518 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4519 if (FAILED(hRet)) goto VarInt_Exit;
4520 pVarIn = &temp;
4522 V_VT(pVarOut) = V_VT(pVarIn);
4524 switch (V_VT(pVarIn))
4526 case VT_R4:
4527 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4528 break;
4529 case VT_BSTR:
4530 V_VT(pVarOut) = VT_R8;
4531 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4532 pVarIn = pVarOut;
4533 /* Fall through */
4534 case VT_DATE:
4535 case VT_R8:
4536 V_R8(pVarOut) = floor(V_R8(pVarIn));
4537 break;
4538 case VT_CY:
4539 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4540 break;
4541 case VT_DECIMAL:
4542 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4543 break;
4544 default:
4545 hRet = VarFix(pVarIn, pVarOut);
4547 VarInt_Exit:
4548 VariantClear(&temp);
4550 return hRet;
4553 /**********************************************************************
4554 * VarXor [OLEAUT32.167]
4556 * Perform a logical exclusive-or (XOR) operation on two variants.
4558 * PARAMS
4559 * pVarLeft [I] First variant
4560 * pVarRight [I] Variant to XOR with pVarLeft
4561 * pVarOut [O] Destination for XOR result
4563 * RETURNS
4564 * Success: S_OK. pVarOut contains the result of the operation with its type
4565 * taken from the table below).
4566 * Failure: An HRESULT error code indicating the error.
4568 * NOTES
4569 * - Neither pVarLeft or pVarRight are modified by this function.
4570 * - This function does not process by-reference variants.
4571 * - Input types of VT_BSTR may be numeric strings or boolean text.
4572 * - The type of result stored in pVarOut depends on the types of pVarLeft
4573 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4574 * or VT_NULL if the function succeeds.
4575 * - Type promotion is inconsistent and as a result certain combinations of
4576 * values will return DISP_E_OVERFLOW even when they could be represented.
4577 * This matches the behaviour of native oleaut32.
4579 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4581 VARTYPE vt;
4582 VARIANT varLeft, varRight;
4583 VARIANT tempLeft, tempRight;
4584 double d;
4585 HRESULT hRet;
4587 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4588 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4589 debugstr_VF(pVarRight), pVarOut);
4591 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4592 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4593 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4594 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4595 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4596 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4597 return DISP_E_BADVARTYPE;
4599 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4601 /* NULL XOR anything valid is NULL */
4602 V_VT(pVarOut) = VT_NULL;
4603 return S_OK;
4606 VariantInit(&tempLeft);
4607 VariantInit(&tempRight);
4609 /* Handle VT_DISPATCH by storing and taking address of returned value */
4610 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4612 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4613 if (FAILED(hRet)) goto VarXor_Exit;
4614 pVarLeft = &tempLeft;
4616 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4618 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4619 if (FAILED(hRet)) goto VarXor_Exit;
4620 pVarRight = &tempRight;
4623 /* Copy our inputs so we don't disturb anything */
4624 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4626 hRet = VariantCopy(&varLeft, pVarLeft);
4627 if (FAILED(hRet))
4628 goto VarXor_Exit;
4630 hRet = VariantCopy(&varRight, pVarRight);
4631 if (FAILED(hRet))
4632 goto VarXor_Exit;
4634 /* Try any strings first as numbers, then as VT_BOOL */
4635 if (V_VT(&varLeft) == VT_BSTR)
4637 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4638 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4639 FAILED(hRet) ? VT_BOOL : VT_I4);
4640 if (FAILED(hRet))
4641 goto VarXor_Exit;
4644 if (V_VT(&varRight) == VT_BSTR)
4646 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4647 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4648 FAILED(hRet) ? VT_BOOL : VT_I4);
4649 if (FAILED(hRet))
4650 goto VarXor_Exit;
4653 /* Determine the result type */
4654 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4656 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4658 hRet = DISP_E_TYPEMISMATCH;
4659 goto VarXor_Exit;
4661 vt = VT_I8;
4663 else
4665 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4667 case (VT_BOOL << 16) | VT_BOOL:
4668 vt = VT_BOOL;
4669 break;
4670 case (VT_UI1 << 16) | VT_UI1:
4671 vt = VT_UI1;
4672 break;
4673 case (VT_EMPTY << 16) | VT_EMPTY:
4674 case (VT_EMPTY << 16) | VT_UI1:
4675 case (VT_EMPTY << 16) | VT_I2:
4676 case (VT_EMPTY << 16) | VT_BOOL:
4677 case (VT_UI1 << 16) | VT_EMPTY:
4678 case (VT_UI1 << 16) | VT_I2:
4679 case (VT_UI1 << 16) | VT_BOOL:
4680 case (VT_I2 << 16) | VT_EMPTY:
4681 case (VT_I2 << 16) | VT_UI1:
4682 case (VT_I2 << 16) | VT_I2:
4683 case (VT_I2 << 16) | VT_BOOL:
4684 case (VT_BOOL << 16) | VT_EMPTY:
4685 case (VT_BOOL << 16) | VT_UI1:
4686 case (VT_BOOL << 16) | VT_I2:
4687 vt = VT_I2;
4688 break;
4689 default:
4690 vt = VT_I4;
4691 break;
4695 /* VT_UI4 does not overflow */
4696 if (vt != VT_I8)
4698 if (V_VT(&varLeft) == VT_UI4)
4699 V_VT(&varLeft) = VT_I4;
4700 if (V_VT(&varRight) == VT_UI4)
4701 V_VT(&varRight) = VT_I4;
4704 /* Convert our input copies to the result type */
4705 if (V_VT(&varLeft) != vt)
4706 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4707 if (FAILED(hRet))
4708 goto VarXor_Exit;
4710 if (V_VT(&varRight) != vt)
4711 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4712 if (FAILED(hRet))
4713 goto VarXor_Exit;
4715 V_VT(pVarOut) = vt;
4717 /* Calculate the result */
4718 switch (vt)
4720 case VT_I8:
4721 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4722 break;
4723 case VT_I4:
4724 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4725 break;
4726 case VT_BOOL:
4727 case VT_I2:
4728 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4729 break;
4730 case VT_UI1:
4731 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4732 break;
4735 VarXor_Exit:
4736 VariantClear(&varLeft);
4737 VariantClear(&varRight);
4738 VariantClear(&tempLeft);
4739 VariantClear(&tempRight);
4740 return hRet;
4743 /**********************************************************************
4744 * VarEqv [OLEAUT32.172]
4746 * Determine if two variants contain the same value.
4748 * PARAMS
4749 * pVarLeft [I] First variant to compare
4750 * pVarRight [I] Variant to compare to pVarLeft
4751 * pVarOut [O] Destination for comparison result
4753 * RETURNS
4754 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4755 * if equivalent or non-zero otherwise.
4756 * Failure: An HRESULT error code indicating the error.
4758 * NOTES
4759 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4760 * the result.
4762 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4764 HRESULT hRet;
4766 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4767 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4768 debugstr_VF(pVarRight), pVarOut);
4770 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4771 if (SUCCEEDED(hRet))
4773 if (V_VT(pVarOut) == VT_I8)
4774 V_I8(pVarOut) = ~V_I8(pVarOut);
4775 else
4776 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4778 return hRet;
4781 /**********************************************************************
4782 * VarNeg [OLEAUT32.173]
4784 * Negate the value of a variant.
4786 * PARAMS
4787 * pVarIn [I] Source variant
4788 * pVarOut [O] Destination for converted value
4790 * RETURNS
4791 * Success: S_OK. pVarOut contains the converted value.
4792 * Failure: An HRESULT error code indicating the error.
4794 * NOTES
4795 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4796 * according to the following table:
4797 *| Input Type Output Type
4798 *| ---------- -----------
4799 *| VT_EMPTY VT_I2
4800 *| VT_UI1 VT_I2
4801 *| VT_BOOL VT_I2
4802 *| VT_BSTR VT_R8
4803 *| All Others Unchanged (unless promoted)
4804 * - Where the negated value of a variant does not fit in its base type, the type
4805 * is promoted according to the following table:
4806 *| Input Type Promoted To
4807 *| ---------- -----------
4808 *| VT_I2 VT_I4
4809 *| VT_I4 VT_R8
4810 *| VT_I8 VT_R8
4811 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4812 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4813 * for types which are not valid. Since this is in contravention of the
4814 * meaning of those error codes and unlikely to be relied on by applications,
4815 * this implementation returns errors consistent with the other high level
4816 * variant math functions.
4818 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4820 HRESULT hRet = S_OK;
4821 VARIANT temp;
4823 VariantInit(&temp);
4825 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4826 debugstr_VF(pVarIn), pVarOut);
4828 /* Handle VT_DISPATCH by storing and taking address of returned value */
4829 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4831 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4832 if (FAILED(hRet)) goto VarNeg_Exit;
4833 pVarIn = &temp;
4835 V_VT(pVarOut) = V_VT(pVarIn);
4837 switch (V_VT(pVarIn))
4839 case VT_UI1:
4840 V_VT(pVarOut) = VT_I2;
4841 V_I2(pVarOut) = -V_UI1(pVarIn);
4842 break;
4843 case VT_BOOL:
4844 V_VT(pVarOut) = VT_I2;
4845 /* Fall through */
4846 case VT_I2:
4847 if (V_I2(pVarIn) == I2_MIN)
4849 V_VT(pVarOut) = VT_I4;
4850 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4852 else
4853 V_I2(pVarOut) = -V_I2(pVarIn);
4854 break;
4855 case VT_I4:
4856 if (V_I4(pVarIn) == I4_MIN)
4858 V_VT(pVarOut) = VT_R8;
4859 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4861 else
4862 V_I4(pVarOut) = -V_I4(pVarIn);
4863 break;
4864 case VT_I8:
4865 if (V_I8(pVarIn) == I8_MIN)
4867 V_VT(pVarOut) = VT_R8;
4868 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4869 V_R8(pVarOut) *= -1.0;
4871 else
4872 V_I8(pVarOut) = -V_I8(pVarIn);
4873 break;
4874 case VT_R4:
4875 V_R4(pVarOut) = -V_R4(pVarIn);
4876 break;
4877 case VT_DATE:
4878 case VT_R8:
4879 V_R8(pVarOut) = -V_R8(pVarIn);
4880 break;
4881 case VT_CY:
4882 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4883 break;
4884 case VT_DECIMAL:
4885 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4886 break;
4887 case VT_BSTR:
4888 V_VT(pVarOut) = VT_R8;
4889 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4890 V_R8(pVarOut) = -V_R8(pVarOut);
4891 break;
4892 case VT_EMPTY:
4893 V_VT(pVarOut) = VT_I2;
4894 V_I2(pVarOut) = 0;
4895 break;
4896 case VT_NULL:
4897 /* No-Op */
4898 break;
4899 default:
4900 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4901 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4902 hRet = DISP_E_BADVARTYPE;
4903 else
4904 hRet = DISP_E_TYPEMISMATCH;
4906 VarNeg_Exit:
4907 if (FAILED(hRet))
4908 V_VT(pVarOut) = VT_EMPTY;
4909 VariantClear(&temp);
4911 return hRet;
4914 /**********************************************************************
4915 * VarNot [OLEAUT32.174]
4917 * Perform a not operation on a variant.
4919 * PARAMS
4920 * pVarIn [I] Source variant
4921 * pVarOut [O] Destination for converted value
4923 * RETURNS
4924 * Success: S_OK. pVarOut contains the converted value.
4925 * Failure: An HRESULT error code indicating the error.
4927 * NOTES
4928 * - Strictly speaking, this function performs a bitwise ones complement
4929 * on the variants value (after possibly converting to VT_I4, see below).
4930 * This only behaves like a boolean not operation if the value in
4931 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4932 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4933 * before calling this function.
4934 * - This function does not process by-reference variants.
4935 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4936 * according to the following table:
4937 *| Input Type Output Type
4938 *| ---------- -----------
4939 *| VT_EMPTY VT_I2
4940 *| VT_R4 VT_I4
4941 *| VT_R8 VT_I4
4942 *| VT_BSTR VT_I4
4943 *| VT_DECIMAL VT_I4
4944 *| VT_CY VT_I4
4945 *| (All others) Unchanged
4947 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4949 VARIANT varIn;
4950 HRESULT hRet = S_OK;
4951 VARIANT temp;
4953 VariantInit(&temp);
4955 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4956 debugstr_VF(pVarIn), pVarOut);
4958 /* Handle VT_DISPATCH by storing and taking address of returned value */
4959 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4961 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4962 if (FAILED(hRet)) goto VarNot_Exit;
4963 pVarIn = &temp;
4966 V_VT(pVarOut) = V_VT(pVarIn);
4968 switch (V_VT(pVarIn))
4970 case VT_I1:
4971 V_I4(pVarOut) = ~V_I1(pVarIn);
4972 V_VT(pVarOut) = VT_I4;
4973 break;
4974 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4975 case VT_BOOL:
4976 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4977 case VT_UI2:
4978 V_I4(pVarOut) = ~V_UI2(pVarIn);
4979 V_VT(pVarOut) = VT_I4;
4980 break;
4981 case VT_DECIMAL:
4982 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4983 if (FAILED(hRet))
4984 break;
4985 pVarIn = &varIn;
4986 /* Fall through ... */
4987 case VT_INT:
4988 V_VT(pVarOut) = VT_I4;
4989 /* Fall through ... */
4990 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4991 case VT_UINT:
4992 case VT_UI4:
4993 V_I4(pVarOut) = ~V_UI4(pVarIn);
4994 V_VT(pVarOut) = VT_I4;
4995 break;
4996 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
4997 case VT_UI8:
4998 V_I4(pVarOut) = ~V_UI8(pVarIn);
4999 V_VT(pVarOut) = VT_I4;
5000 break;
5001 case VT_R4:
5002 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
5003 V_I4(pVarOut) = ~V_I4(pVarOut);
5004 V_VT(pVarOut) = VT_I4;
5005 break;
5006 case VT_BSTR:
5007 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5008 if (FAILED(hRet))
5009 break;
5010 pVarIn = &varIn;
5011 /* Fall through ... */
5012 case VT_DATE:
5013 case VT_R8:
5014 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
5015 V_I4(pVarOut) = ~V_I4(pVarOut);
5016 V_VT(pVarOut) = VT_I4;
5017 break;
5018 case VT_CY:
5019 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
5020 V_I4(pVarOut) = ~V_I4(pVarOut);
5021 V_VT(pVarOut) = VT_I4;
5022 break;
5023 case VT_EMPTY:
5024 V_I2(pVarOut) = ~0;
5025 V_VT(pVarOut) = VT_I2;
5026 break;
5027 case VT_NULL:
5028 /* No-Op */
5029 break;
5030 default:
5031 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5032 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5033 hRet = DISP_E_BADVARTYPE;
5034 else
5035 hRet = DISP_E_TYPEMISMATCH;
5037 VarNot_Exit:
5038 if (FAILED(hRet))
5039 V_VT(pVarOut) = VT_EMPTY;
5040 VariantClear(&temp);
5042 return hRet;
5045 /**********************************************************************
5046 * VarRound [OLEAUT32.175]
5048 * Perform a round operation on a variant.
5050 * PARAMS
5051 * pVarIn [I] Source variant
5052 * deci [I] Number of decimals to round to
5053 * pVarOut [O] Destination for converted value
5055 * RETURNS
5056 * Success: S_OK. pVarOut contains the converted value.
5057 * Failure: An HRESULT error code indicating the error.
5059 * NOTES
5060 * - Floating point values are rounded to the desired number of decimals.
5061 * - Some integer types are just copied to the return variable.
5062 * - Some other integer types are not handled and fail.
5064 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5066 VARIANT varIn;
5067 HRESULT hRet = S_OK;
5068 float factor;
5069 VARIANT temp;
5071 VariantInit(&temp);
5073 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
5075 /* Handle VT_DISPATCH by storing and taking address of returned value */
5076 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5078 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5079 if (FAILED(hRet)) goto VarRound_Exit;
5080 pVarIn = &temp;
5083 switch (V_VT(pVarIn))
5085 /* cases that fail on windows */
5086 case VT_I1:
5087 case VT_I8:
5088 case VT_UI2:
5089 case VT_UI4:
5090 hRet = DISP_E_BADVARTYPE;
5091 break;
5093 /* cases just copying in to out */
5094 case VT_UI1:
5095 V_VT(pVarOut) = V_VT(pVarIn);
5096 V_UI1(pVarOut) = V_UI1(pVarIn);
5097 break;
5098 case VT_I2:
5099 V_VT(pVarOut) = V_VT(pVarIn);
5100 V_I2(pVarOut) = V_I2(pVarIn);
5101 break;
5102 case VT_I4:
5103 V_VT(pVarOut) = V_VT(pVarIn);
5104 V_I4(pVarOut) = V_I4(pVarIn);
5105 break;
5106 case VT_NULL:
5107 V_VT(pVarOut) = V_VT(pVarIn);
5108 /* value unchanged */
5109 break;
5111 /* cases that change type */
5112 case VT_EMPTY:
5113 V_VT(pVarOut) = VT_I2;
5114 V_I2(pVarOut) = 0;
5115 break;
5116 case VT_BOOL:
5117 V_VT(pVarOut) = VT_I2;
5118 V_I2(pVarOut) = V_BOOL(pVarIn);
5119 break;
5120 case VT_BSTR:
5121 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5122 if (FAILED(hRet))
5123 break;
5124 V_VT(&varIn)=VT_R8;
5125 pVarIn = &varIn;
5126 /* Fall through ... */
5128 /* cases we need to do math */
5129 case VT_R8:
5130 if (V_R8(pVarIn)>0) {
5131 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5132 } else {
5133 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5135 V_VT(pVarOut) = V_VT(pVarIn);
5136 break;
5137 case VT_R4:
5138 if (V_R4(pVarIn)>0) {
5139 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5140 } else {
5141 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5143 V_VT(pVarOut) = V_VT(pVarIn);
5144 break;
5145 case VT_DATE:
5146 if (V_DATE(pVarIn)>0) {
5147 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5148 } else {
5149 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5151 V_VT(pVarOut) = V_VT(pVarIn);
5152 break;
5153 case VT_CY:
5154 if (deci>3)
5155 factor=1;
5156 else
5157 factor=pow(10, 4-deci);
5159 if (V_CY(pVarIn).int64>0) {
5160 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5161 } else {
5162 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5164 V_VT(pVarOut) = V_VT(pVarIn);
5165 break;
5167 /* cases we don't know yet */
5168 default:
5169 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5170 V_VT(pVarIn) & VT_TYPEMASK, deci);
5171 hRet = DISP_E_BADVARTYPE;
5173 VarRound_Exit:
5174 if (FAILED(hRet))
5175 V_VT(pVarOut) = VT_EMPTY;
5176 VariantClear(&temp);
5178 TRACE("returning 0x%08x (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
5179 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
5180 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
5182 return hRet;
5185 /**********************************************************************
5186 * VarIdiv [OLEAUT32.153]
5188 * Converts input variants to integers and divides them.
5190 * PARAMS
5191 * left [I] Left hand variant
5192 * right [I] Right hand variant
5193 * result [O] Destination for quotient
5195 * RETURNS
5196 * Success: S_OK. result contains the quotient.
5197 * Failure: An HRESULT error code indicating the error.
5199 * NOTES
5200 * If either expression is null, null is returned, as per MSDN
5202 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5204 HRESULT hres = S_OK;
5205 VARTYPE resvt = VT_EMPTY;
5206 VARTYPE leftvt,rightvt;
5207 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5208 VARIANT lv,rv;
5209 VARIANT tempLeft, tempRight;
5211 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5212 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5214 VariantInit(&lv);
5215 VariantInit(&rv);
5216 VariantInit(&tempLeft);
5217 VariantInit(&tempRight);
5219 leftvt = V_VT(left)&VT_TYPEMASK;
5220 rightvt = V_VT(right)&VT_TYPEMASK;
5221 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5222 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5224 if (leftExtraFlags != rightExtraFlags)
5226 hres = DISP_E_BADVARTYPE;
5227 goto end;
5229 ExtraFlags = leftExtraFlags;
5231 /* Native VarIdiv always returns an error when using extra
5232 * flags or if the variant combination is I8 and INT.
5234 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5235 (leftvt == VT_INT && rightvt == VT_I8) ||
5236 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5237 ExtraFlags != 0)
5239 hres = DISP_E_BADVARTYPE;
5240 goto end;
5243 /* Determine variant type */
5244 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5246 V_VT(result) = VT_NULL;
5247 hres = S_OK;
5248 goto end;
5250 else if (leftvt == VT_I8 || rightvt == VT_I8)
5251 resvt = VT_I8;
5252 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5253 leftvt == VT_INT || rightvt == VT_INT ||
5254 leftvt == VT_UINT || rightvt == VT_UINT ||
5255 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5256 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5257 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5258 leftvt == VT_I1 || rightvt == VT_I1 ||
5259 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5260 leftvt == VT_DATE || rightvt == VT_DATE ||
5261 leftvt == VT_CY || rightvt == VT_CY ||
5262 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5263 leftvt == VT_R8 || rightvt == VT_R8 ||
5264 leftvt == VT_R4 || rightvt == VT_R4)
5265 resvt = VT_I4;
5266 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5267 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5268 leftvt == VT_EMPTY)
5269 resvt = VT_I2;
5270 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5271 resvt = VT_UI1;
5272 else
5274 hres = DISP_E_BADVARTYPE;
5275 goto end;
5278 /* coerce to the result type */
5279 hres = VariantChangeType(&lv, left, 0, resvt);
5280 if (hres != S_OK) goto end;
5281 hres = VariantChangeType(&rv, right, 0, resvt);
5282 if (hres != S_OK) goto end;
5284 /* do the math */
5285 V_VT(result) = resvt;
5286 switch (resvt)
5288 case VT_UI1:
5289 if (V_UI1(&rv) == 0)
5291 hres = DISP_E_DIVBYZERO;
5292 V_VT(result) = VT_EMPTY;
5294 else
5295 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5296 break;
5297 case VT_I2:
5298 if (V_I2(&rv) == 0)
5300 hres = DISP_E_DIVBYZERO;
5301 V_VT(result) = VT_EMPTY;
5303 else
5304 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5305 break;
5306 case VT_I4:
5307 if (V_I4(&rv) == 0)
5309 hres = DISP_E_DIVBYZERO;
5310 V_VT(result) = VT_EMPTY;
5312 else
5313 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5314 break;
5315 case VT_I8:
5316 if (V_I8(&rv) == 0)
5318 hres = DISP_E_DIVBYZERO;
5319 V_VT(result) = VT_EMPTY;
5321 else
5322 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5323 break;
5324 default:
5325 FIXME("Couldn't integer divide variant types %d,%d\n",
5326 leftvt,rightvt);
5329 end:
5330 VariantClear(&lv);
5331 VariantClear(&rv);
5332 VariantClear(&tempLeft);
5333 VariantClear(&tempRight);
5335 return hres;
5339 /**********************************************************************
5340 * VarMod [OLEAUT32.155]
5342 * Perform the modulus operation of the right hand variant on the left
5344 * PARAMS
5345 * left [I] Left hand variant
5346 * right [I] Right hand variant
5347 * result [O] Destination for converted value
5349 * RETURNS
5350 * Success: S_OK. result contains the remainder.
5351 * Failure: An HRESULT error code indicating the error.
5353 * NOTE:
5354 * If an error occurs the type of result will be modified but the value will not be.
5355 * Doesn't support arrays or any special flags yet.
5357 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5359 BOOL lOk = TRUE;
5360 BOOL rOk = TRUE;
5361 HRESULT rc = E_FAIL;
5362 int resT = 0;
5363 VARIANT lv,rv;
5364 VARIANT tempLeft, tempRight;
5366 VariantInit(&tempLeft);
5367 VariantInit(&tempRight);
5368 VariantInit(&lv);
5369 VariantInit(&rv);
5371 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5372 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5374 /* Handle VT_DISPATCH by storing and taking address of returned value */
5375 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5377 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5378 if (FAILED(rc)) goto end;
5379 left = &tempLeft;
5381 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5383 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5384 if (FAILED(rc)) goto end;
5385 right = &tempRight;
5388 /* check for invalid inputs */
5389 lOk = TRUE;
5390 switch (V_VT(left) & VT_TYPEMASK) {
5391 case VT_BOOL :
5392 case VT_I1 :
5393 case VT_I2 :
5394 case VT_I4 :
5395 case VT_I8 :
5396 case VT_INT :
5397 case VT_UI1 :
5398 case VT_UI2 :
5399 case VT_UI4 :
5400 case VT_UI8 :
5401 case VT_UINT :
5402 case VT_R4 :
5403 case VT_R8 :
5404 case VT_CY :
5405 case VT_EMPTY:
5406 case VT_DATE :
5407 case VT_BSTR :
5408 case VT_DECIMAL:
5409 break;
5410 case VT_VARIANT:
5411 case VT_UNKNOWN:
5412 V_VT(result) = VT_EMPTY;
5413 rc = DISP_E_TYPEMISMATCH;
5414 goto end;
5415 case VT_ERROR:
5416 rc = DISP_E_TYPEMISMATCH;
5417 goto end;
5418 case VT_RECORD:
5419 V_VT(result) = VT_EMPTY;
5420 rc = DISP_E_TYPEMISMATCH;
5421 goto end;
5422 case VT_NULL:
5423 break;
5424 default:
5425 V_VT(result) = VT_EMPTY;
5426 rc = DISP_E_BADVARTYPE;
5427 goto end;
5431 rOk = TRUE;
5432 switch (V_VT(right) & VT_TYPEMASK) {
5433 case VT_BOOL :
5434 case VT_I1 :
5435 case VT_I2 :
5436 case VT_I4 :
5437 case VT_I8 :
5438 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5440 V_VT(result) = VT_EMPTY;
5441 rc = DISP_E_TYPEMISMATCH;
5442 goto end;
5444 case VT_INT :
5445 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5447 V_VT(result) = VT_EMPTY;
5448 rc = DISP_E_TYPEMISMATCH;
5449 goto end;
5451 case VT_UI1 :
5452 case VT_UI2 :
5453 case VT_UI4 :
5454 case VT_UI8 :
5455 case VT_UINT :
5456 case VT_R4 :
5457 case VT_R8 :
5458 case VT_CY :
5459 if(V_VT(left) == VT_EMPTY)
5461 V_VT(result) = VT_I4;
5462 rc = S_OK;
5463 goto end;
5465 case VT_EMPTY:
5466 case VT_DATE :
5467 case VT_DECIMAL:
5468 if(V_VT(left) == VT_ERROR)
5470 V_VT(result) = VT_EMPTY;
5471 rc = DISP_E_TYPEMISMATCH;
5472 goto end;
5474 case VT_BSTR:
5475 if(V_VT(left) == VT_NULL)
5477 V_VT(result) = VT_NULL;
5478 rc = S_OK;
5479 goto end;
5481 break;
5483 case VT_VOID:
5484 V_VT(result) = VT_EMPTY;
5485 rc = DISP_E_BADVARTYPE;
5486 goto end;
5487 case VT_NULL:
5488 if(V_VT(left) == VT_VOID)
5490 V_VT(result) = VT_EMPTY;
5491 rc = DISP_E_BADVARTYPE;
5492 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5493 lOk)
5495 V_VT(result) = VT_NULL;
5496 rc = S_OK;
5497 } else
5499 V_VT(result) = VT_NULL;
5500 rc = DISP_E_BADVARTYPE;
5502 goto end;
5503 case VT_VARIANT:
5504 case VT_UNKNOWN:
5505 V_VT(result) = VT_EMPTY;
5506 rc = DISP_E_TYPEMISMATCH;
5507 goto end;
5508 case VT_ERROR:
5509 rc = DISP_E_TYPEMISMATCH;
5510 goto end;
5511 case VT_RECORD:
5512 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5514 V_VT(result) = VT_EMPTY;
5515 rc = DISP_E_BADVARTYPE;
5516 } else
5518 V_VT(result) = VT_EMPTY;
5519 rc = DISP_E_TYPEMISMATCH;
5521 goto end;
5522 default:
5523 V_VT(result) = VT_EMPTY;
5524 rc = DISP_E_BADVARTYPE;
5525 goto end;
5528 /* determine the result type */
5529 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5530 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5531 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5532 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5533 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5534 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5535 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5536 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5537 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5538 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5539 else resT = VT_I4; /* most outputs are I4 */
5541 /* convert to I8 for the modulo */
5542 rc = VariantChangeType(&lv, left, 0, VT_I8);
5543 if(FAILED(rc))
5545 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5546 goto end;
5549 rc = VariantChangeType(&rv, right, 0, VT_I8);
5550 if(FAILED(rc))
5552 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5553 goto end;
5556 /* if right is zero set VT_EMPTY and return divide by zero */
5557 if(V_I8(&rv) == 0)
5559 V_VT(result) = VT_EMPTY;
5560 rc = DISP_E_DIVBYZERO;
5561 goto end;
5564 /* perform the modulo operation */
5565 V_VT(result) = VT_I8;
5566 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5568 TRACE("V_I8(left) == %ld, V_I8(right) == %ld, V_I8(result) == %ld\n", (long)V_I8(&lv), (long)V_I8(&rv), (long)V_I8(result));
5570 /* convert left and right to the destination type */
5571 rc = VariantChangeType(result, result, 0, resT);
5572 if(FAILED(rc))
5574 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5575 /* fall to end of function */
5578 end:
5579 VariantClear(&lv);
5580 VariantClear(&rv);
5581 VariantClear(&tempLeft);
5582 VariantClear(&tempRight);
5583 return rc;
5586 /**********************************************************************
5587 * VarPow [OLEAUT32.158]
5589 * Computes the power of one variant to another variant.
5591 * PARAMS
5592 * left [I] First variant
5593 * right [I] Second variant
5594 * result [O] Result variant
5596 * RETURNS
5597 * Success: S_OK.
5598 * Failure: An HRESULT error code indicating the error.
5600 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5602 HRESULT hr = S_OK;
5603 VARIANT dl,dr;
5604 VARTYPE resvt = VT_EMPTY;
5605 VARTYPE leftvt,rightvt;
5606 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5607 VARIANT tempLeft, tempRight;
5609 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
5610 right, debugstr_VT(right), debugstr_VF(right), result);
5612 VariantInit(&dl);
5613 VariantInit(&dr);
5614 VariantInit(&tempLeft);
5615 VariantInit(&tempRight);
5617 /* Handle VT_DISPATCH by storing and taking address of returned value */
5618 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5620 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5621 if (FAILED(hr)) goto end;
5622 left = &tempLeft;
5624 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5626 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5627 if (FAILED(hr)) goto end;
5628 right = &tempRight;
5631 leftvt = V_VT(left)&VT_TYPEMASK;
5632 rightvt = V_VT(right)&VT_TYPEMASK;
5633 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5634 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5636 if (leftExtraFlags != rightExtraFlags)
5638 hr = DISP_E_BADVARTYPE;
5639 goto end;
5641 ExtraFlags = leftExtraFlags;
5643 /* Native VarPow always returns an error when using extra flags */
5644 if (ExtraFlags != 0)
5646 hr = DISP_E_BADVARTYPE;
5647 goto end;
5650 /* Determine return type */
5651 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5652 V_VT(result) = VT_NULL;
5653 hr = S_OK;
5654 goto end;
5656 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5657 leftvt == VT_I4 || leftvt == VT_R4 ||
5658 leftvt == VT_R8 || leftvt == VT_CY ||
5659 leftvt == VT_DATE || leftvt == VT_BSTR ||
5660 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5661 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5662 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5663 rightvt == VT_I4 || rightvt == VT_R4 ||
5664 rightvt == VT_R8 || rightvt == VT_CY ||
5665 rightvt == VT_DATE || rightvt == VT_BSTR ||
5666 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5667 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5668 resvt = VT_R8;
5669 else
5671 hr = DISP_E_BADVARTYPE;
5672 goto end;
5675 hr = VariantChangeType(&dl,left,0,resvt);
5676 if (FAILED(hr)) {
5677 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5678 hr = E_FAIL;
5679 goto end;
5682 hr = VariantChangeType(&dr,right,0,resvt);
5683 if (FAILED(hr)) {
5684 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5685 hr = E_FAIL;
5686 goto end;
5689 V_VT(result) = VT_R8;
5690 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5692 end:
5693 VariantClear(&dl);
5694 VariantClear(&dr);
5695 VariantClear(&tempLeft);
5696 VariantClear(&tempRight);
5698 return hr;
5701 /**********************************************************************
5702 * VarImp [OLEAUT32.154]
5704 * Bitwise implication of two variants.
5706 * PARAMS
5707 * left [I] First variant
5708 * right [I] Second variant
5709 * result [O] Result variant
5711 * RETURNS
5712 * Success: S_OK.
5713 * Failure: An HRESULT error code indicating the error.
5715 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5717 HRESULT hres = S_OK;
5718 VARTYPE resvt = VT_EMPTY;
5719 VARTYPE leftvt,rightvt;
5720 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5721 VARIANT lv,rv;
5722 double d;
5723 VARIANT tempLeft, tempRight;
5725 VariantInit(&lv);
5726 VariantInit(&rv);
5727 VariantInit(&tempLeft);
5728 VariantInit(&tempRight);
5730 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5731 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5733 /* Handle VT_DISPATCH by storing and taking address of returned value */
5734 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5736 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5737 if (FAILED(hres)) goto VarImp_Exit;
5738 left = &tempLeft;
5740 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5742 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5743 if (FAILED(hres)) goto VarImp_Exit;
5744 right = &tempRight;
5747 leftvt = V_VT(left)&VT_TYPEMASK;
5748 rightvt = V_VT(right)&VT_TYPEMASK;
5749 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5750 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5752 if (leftExtraFlags != rightExtraFlags)
5754 hres = DISP_E_BADVARTYPE;
5755 goto VarImp_Exit;
5757 ExtraFlags = leftExtraFlags;
5759 /* Native VarImp always returns an error when using extra
5760 * flags or if the variants are I8 and INT.
5762 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5763 ExtraFlags != 0)
5765 hres = DISP_E_BADVARTYPE;
5766 goto VarImp_Exit;
5769 /* Determine result type */
5770 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5771 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5773 V_VT(result) = VT_NULL;
5774 hres = S_OK;
5775 goto VarImp_Exit;
5777 else if (leftvt == VT_I8 || rightvt == VT_I8)
5778 resvt = VT_I8;
5779 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5780 leftvt == VT_INT || rightvt == VT_INT ||
5781 leftvt == VT_UINT || rightvt == VT_UINT ||
5782 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5783 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5784 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5785 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5786 leftvt == VT_DATE || rightvt == VT_DATE ||
5787 leftvt == VT_CY || rightvt == VT_CY ||
5788 leftvt == VT_R8 || rightvt == VT_R8 ||
5789 leftvt == VT_R4 || rightvt == VT_R4 ||
5790 leftvt == VT_I1 || rightvt == VT_I1)
5791 resvt = VT_I4;
5792 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5793 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5794 (leftvt == VT_NULL && rightvt == VT_UI1))
5795 resvt = VT_UI1;
5796 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5797 leftvt == VT_I2 || rightvt == VT_I2 ||
5798 leftvt == VT_UI1 || rightvt == VT_UI1)
5799 resvt = VT_I2;
5800 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5801 leftvt == VT_BSTR || rightvt == VT_BSTR)
5802 resvt = VT_BOOL;
5804 /* VT_NULL requires special handling for when the opposite
5805 * variant is equal to something other than -1.
5806 * (NULL Imp 0 = NULL, NULL Imp n = n)
5808 if (leftvt == VT_NULL)
5810 VARIANT_BOOL b;
5811 switch(rightvt)
5813 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5814 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5815 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5816 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5817 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5818 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5819 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5820 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5821 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5822 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5823 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5824 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5825 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5826 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5827 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5828 case VT_DECIMAL:
5829 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5830 resvt = VT_NULL;
5831 break;
5832 case VT_BSTR:
5833 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5834 if (FAILED(hres)) goto VarImp_Exit;
5835 else if (!b)
5836 V_VT(result) = VT_NULL;
5837 else
5839 V_VT(result) = VT_BOOL;
5840 V_BOOL(result) = b;
5842 goto VarImp_Exit;
5844 if (resvt == VT_NULL)
5846 V_VT(result) = resvt;
5847 goto VarImp_Exit;
5849 else
5851 hres = VariantChangeType(result,right,0,resvt);
5852 goto VarImp_Exit;
5856 /* Special handling is required when NULL is the right variant.
5857 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5859 else if (rightvt == VT_NULL)
5861 VARIANT_BOOL b;
5862 switch(leftvt)
5864 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5865 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5866 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5867 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5868 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5869 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5870 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5871 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5872 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5873 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5874 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5875 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5876 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5877 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5878 case VT_DECIMAL:
5879 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5880 resvt = VT_NULL;
5881 break;
5882 case VT_BSTR:
5883 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5884 if (FAILED(hres)) goto VarImp_Exit;
5885 else if (b == VARIANT_TRUE)
5886 resvt = VT_NULL;
5888 if (resvt == VT_NULL)
5890 V_VT(result) = resvt;
5891 goto VarImp_Exit;
5895 hres = VariantCopy(&lv, left);
5896 if (FAILED(hres)) goto VarImp_Exit;
5898 if (rightvt == VT_NULL)
5900 memset( &rv, 0, sizeof(rv) );
5901 V_VT(&rv) = resvt;
5903 else
5905 hres = VariantCopy(&rv, right);
5906 if (FAILED(hres)) goto VarImp_Exit;
5909 if (V_VT(&lv) == VT_BSTR &&
5910 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5911 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5912 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5913 hres = VariantChangeType(&lv,&lv,0,resvt);
5914 if (FAILED(hres)) goto VarImp_Exit;
5916 if (V_VT(&rv) == VT_BSTR &&
5917 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5918 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5919 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5920 hres = VariantChangeType(&rv, &rv, 0, resvt);
5921 if (FAILED(hres)) goto VarImp_Exit;
5923 /* do the math */
5924 V_VT(result) = resvt;
5925 switch (resvt)
5927 case VT_I8:
5928 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5929 break;
5930 case VT_I4:
5931 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5932 break;
5933 case VT_I2:
5934 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
5935 break;
5936 case VT_UI1:
5937 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
5938 break;
5939 case VT_BOOL:
5940 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
5941 break;
5942 default:
5943 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5944 leftvt,rightvt);
5947 VarImp_Exit:
5949 VariantClear(&lv);
5950 VariantClear(&rv);
5951 VariantClear(&tempLeft);
5952 VariantClear(&tempRight);
5954 return hres;