setupapi: Add a check of "#" for interface subkey names.
[wine/multimedia.git] / dlls / oleaut32 / variant.c
blobabfcfcc2abda31d66b1a03874bbbdee269a208d4
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 alorithm 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 if (V_BSTR(pVarg))
612 SysFreeString(V_BSTR(pVarg));
614 else if (V_VT(pVarg) == VT_RECORD)
616 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
617 if (pBr->pRecInfo)
619 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
620 IRecordInfo_Release(pBr->pRecInfo);
623 else if (V_VT(pVarg) == VT_DISPATCH ||
624 V_VT(pVarg) == VT_UNKNOWN)
626 if (V_UNKNOWN(pVarg))
627 IUnknown_Release(V_UNKNOWN(pVarg));
630 V_VT(pVarg) = VT_EMPTY;
632 return hres;
635 /******************************************************************************
636 * Copy an IRecordInfo object contained in a variant.
638 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
640 HRESULT hres = S_OK;
642 if (pBr->pRecInfo)
644 ULONG ulSize;
646 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
647 if (SUCCEEDED(hres))
649 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
650 if (!pvRecord)
651 hres = E_OUTOFMEMORY;
652 else
654 memcpy(pvRecord, pBr->pvRecord, ulSize);
655 pBr->pvRecord = pvRecord;
657 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
658 if (SUCCEEDED(hres))
659 IRecordInfo_AddRef(pBr->pRecInfo);
663 else if (pBr->pvRecord)
664 hres = E_INVALIDARG;
665 return hres;
668 /******************************************************************************
669 * VariantCopy [OLEAUT32.10]
671 * Copy a variant.
673 * PARAMS
674 * pvargDest [O] Destination for copy
675 * pvargSrc [I] Source variant to copy
677 * RETURNS
678 * Success: S_OK. pvargDest contains a copy of pvargSrc.
679 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
680 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
681 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
682 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
684 * NOTES
685 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
686 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
687 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
688 * fails, so does this function.
689 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
690 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
691 * is copied rather than just any pointers to it.
692 * - For by-value object types the object pointer is copied and the objects
693 * reference count increased using IUnknown_AddRef().
694 * - For all by-reference types, only the referencing pointer is copied.
696 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
698 HRESULT hres = S_OK;
700 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
701 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
702 debugstr_VF(pvargSrc));
704 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
705 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
706 return DISP_E_BADVARTYPE;
708 if (pvargSrc != pvargDest &&
709 SUCCEEDED(hres = VariantClear(pvargDest)))
711 *pvargDest = *pvargSrc; /* Shallow copy the value */
713 if (!V_ISBYREF(pvargSrc))
715 if (V_ISARRAY(pvargSrc))
717 if (V_ARRAY(pvargSrc))
718 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
720 else if (V_VT(pvargSrc) == VT_BSTR)
722 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
723 if (!V_BSTR(pvargDest))
725 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
726 hres = E_OUTOFMEMORY;
729 else if (V_VT(pvargSrc) == VT_RECORD)
731 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
733 else if (V_VT(pvargSrc) == VT_DISPATCH ||
734 V_VT(pvargSrc) == VT_UNKNOWN)
736 if (V_UNKNOWN(pvargSrc))
737 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
741 return hres;
744 /* Return the byte size of a variants data */
745 static inline size_t VARIANT_DataSize(const VARIANT* pv)
747 switch (V_TYPE(pv))
749 case VT_I1:
750 case VT_UI1: return sizeof(BYTE);
751 case VT_I2:
752 case VT_UI2: return sizeof(SHORT);
753 case VT_INT:
754 case VT_UINT:
755 case VT_I4:
756 case VT_UI4: return sizeof(LONG);
757 case VT_I8:
758 case VT_UI8: return sizeof(LONGLONG);
759 case VT_R4: return sizeof(float);
760 case VT_R8: return sizeof(double);
761 case VT_DATE: return sizeof(DATE);
762 case VT_BOOL: return sizeof(VARIANT_BOOL);
763 case VT_DISPATCH:
764 case VT_UNKNOWN:
765 case VT_BSTR: return sizeof(void*);
766 case VT_CY: return sizeof(CY);
767 case VT_ERROR: return sizeof(SCODE);
769 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
770 return 0;
773 /******************************************************************************
774 * VariantCopyInd [OLEAUT32.11]
776 * Copy a variant, dereferencing it if it is by-reference.
778 * PARAMS
779 * pvargDest [O] Destination for copy
780 * pvargSrc [I] Source variant to copy
782 * RETURNS
783 * Success: S_OK. pvargDest contains a copy of pvargSrc.
784 * Failure: An HRESULT error code indicating the error.
786 * NOTES
787 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
788 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
789 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
790 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
791 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
793 * NOTES
794 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
795 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
796 * value.
797 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
798 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
799 * to it. If clearing pvargDest fails, so does this function.
801 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
803 VARIANTARG vTmp, *pSrc = pvargSrc;
804 VARTYPE vt;
805 HRESULT hres = S_OK;
807 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
808 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
809 debugstr_VF(pvargSrc));
811 if (!V_ISBYREF(pvargSrc))
812 return VariantCopy(pvargDest, pvargSrc);
814 /* Argument checking is more lax than VariantCopy()... */
815 vt = V_TYPE(pvargSrc);
816 if (V_ISARRAY(pvargSrc) ||
817 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
818 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
820 /* OK */
822 else
823 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
825 if (pvargSrc == pvargDest)
827 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
828 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
830 vTmp = *pvargSrc;
831 pSrc = &vTmp;
832 V_VT(pvargDest) = VT_EMPTY;
834 else
836 /* Copy into another variant. Free the variant in pvargDest */
837 if (FAILED(hres = VariantClear(pvargDest)))
839 TRACE("VariantClear() of destination failed\n");
840 return hres;
844 if (V_ISARRAY(pSrc))
846 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
847 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
849 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
851 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
852 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
854 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
856 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
857 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
859 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
860 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
862 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
863 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
864 if (*V_UNKNOWNREF(pSrc))
865 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
867 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
869 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
870 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
871 hres = E_INVALIDARG; /* Don't dereference more than one level */
872 else
873 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
875 /* Use the dereferenced variants type value, not VT_VARIANT */
876 goto VariantCopyInd_Return;
878 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
880 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
881 sizeof(DECIMAL) - sizeof(USHORT));
883 else
885 /* Copy the pointed to data into this variant */
886 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
889 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
891 VariantCopyInd_Return:
893 if (pSrc != pvargSrc)
894 VariantClear(pSrc);
896 TRACE("returning 0x%08x, %p->(%s%s)\n", hres, pvargDest,
897 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
898 return hres;
901 /******************************************************************************
902 * VariantChangeType [OLEAUT32.12]
904 * Change the type of a variant.
906 * PARAMS
907 * pvargDest [O] Destination for the converted variant
908 * pvargSrc [O] Source variant to change the type of
909 * wFlags [I] VARIANT_ flags from "oleauto.h"
910 * vt [I] Variant type to change pvargSrc into
912 * RETURNS
913 * Success: S_OK. pvargDest contains the converted value.
914 * Failure: An HRESULT error code describing the failure.
916 * NOTES
917 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
918 * See VariantChangeTypeEx.
920 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
921 USHORT wFlags, VARTYPE vt)
923 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
926 /******************************************************************************
927 * VariantChangeTypeEx [OLEAUT32.147]
929 * Change the type of a variant.
931 * PARAMS
932 * pvargDest [O] Destination for the converted variant
933 * pvargSrc [O] Source variant to change the type of
934 * lcid [I] LCID for the conversion
935 * wFlags [I] VARIANT_ flags from "oleauto.h"
936 * vt [I] Variant type to change pvargSrc into
938 * RETURNS
939 * Success: S_OK. pvargDest contains the converted value.
940 * Failure: An HRESULT error code describing the failure.
942 * NOTES
943 * pvargDest and pvargSrc can point to the same variant to perform an in-place
944 * conversion. If the conversion is successful, pvargSrc will be freed.
946 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
947 LCID lcid, USHORT wFlags, VARTYPE vt)
949 HRESULT res = S_OK;
951 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%04x,%s%s)\n", pvargDest,
952 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
953 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
954 debugstr_vt(vt), debugstr_vf(vt));
956 if (vt == VT_CLSID)
957 res = DISP_E_BADVARTYPE;
958 else
960 res = VARIANT_ValidateType(V_VT(pvargSrc));
962 if (SUCCEEDED(res))
964 res = VARIANT_ValidateType(vt);
966 if (SUCCEEDED(res))
968 VARIANTARG vTmp, vSrcDeref;
970 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
971 res = DISP_E_TYPEMISMATCH;
972 else
974 V_VT(&vTmp) = VT_EMPTY;
975 V_VT(&vSrcDeref) = VT_EMPTY;
976 VariantClear(&vTmp);
977 VariantClear(&vSrcDeref);
980 if (SUCCEEDED(res))
982 res = VariantCopyInd(&vSrcDeref, pvargSrc);
983 if (SUCCEEDED(res))
985 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
986 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
987 else
988 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
990 if (SUCCEEDED(res)) {
991 V_VT(&vTmp) = vt;
992 VariantCopy(pvargDest, &vTmp);
994 VariantClear(&vTmp);
995 VariantClear(&vSrcDeref);
1002 TRACE("returning 0x%08x, %p->(%s%s)\n", res, pvargDest,
1003 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1004 return res;
1007 /* Date Conversions */
1009 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1011 /* Convert a VT_DATE value to a Julian Date */
1012 static inline int VARIANT_JulianFromDate(int dateIn)
1014 int julianDays = dateIn;
1016 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1017 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1018 return julianDays;
1021 /* Convert a Julian Date to a VT_DATE value */
1022 static inline int VARIANT_DateFromJulian(int dateIn)
1024 int julianDays = dateIn;
1026 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1027 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1028 return julianDays;
1031 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1032 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1034 int j, i, l, n;
1036 l = jd + 68569;
1037 n = l * 4 / 146097;
1038 l -= (n * 146097 + 3) / 4;
1039 i = (4000 * (l + 1)) / 1461001;
1040 l += 31 - (i * 1461) / 4;
1041 j = (l * 80) / 2447;
1042 *day = l - (j * 2447) / 80;
1043 l = j / 11;
1044 *month = (j + 2) - (12 * l);
1045 *year = 100 * (n - 49) + i + l;
1048 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1049 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1051 int m12 = (month - 14) / 12;
1053 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1054 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1057 /* Macros for accessing DOS format date/time fields */
1058 #define DOS_YEAR(x) (1980 + (x >> 9))
1059 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1060 #define DOS_DAY(x) (x & 0x1f)
1061 #define DOS_HOUR(x) (x >> 11)
1062 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1063 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1064 /* Create a DOS format date/time */
1065 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1066 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1068 /* Roll a date forwards or backwards to correct it */
1069 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1071 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1073 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1074 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1076 /* Years < 100 are treated as 1900 + year */
1077 if (lpUd->st.wYear < 100)
1078 lpUd->st.wYear += 1900;
1080 if (!lpUd->st.wMonth)
1082 /* Roll back to December of the previous year */
1083 lpUd->st.wMonth = 12;
1084 lpUd->st.wYear--;
1086 else while (lpUd->st.wMonth > 12)
1088 /* Roll forward the correct number of months */
1089 lpUd->st.wYear++;
1090 lpUd->st.wMonth -= 12;
1093 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1094 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1095 return E_INVALIDARG; /* Invalid values */
1097 if (!lpUd->st.wDay)
1099 /* Roll back the date one day */
1100 if (lpUd->st.wMonth == 1)
1102 /* Roll back to December 31 of the previous year */
1103 lpUd->st.wDay = 31;
1104 lpUd->st.wMonth = 12;
1105 lpUd->st.wYear--;
1107 else
1109 lpUd->st.wMonth--; /* Previous month */
1110 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1111 lpUd->st.wDay = 29; /* Februaury has 29 days on leap years */
1112 else
1113 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1116 else if (lpUd->st.wDay > 28)
1118 int rollForward = 0;
1120 /* Possibly need to roll the date forward */
1121 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1122 rollForward = lpUd->st.wDay - 29; /* Februaury has 29 days on leap years */
1123 else
1124 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1126 if (rollForward > 0)
1128 lpUd->st.wDay = rollForward;
1129 lpUd->st.wMonth++;
1130 if (lpUd->st.wMonth > 12)
1132 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1133 lpUd->st.wYear++;
1137 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1138 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1139 return S_OK;
1142 /**********************************************************************
1143 * DosDateTimeToVariantTime [OLEAUT32.14]
1145 * Convert a Dos format date and time into variant VT_DATE format.
1147 * PARAMS
1148 * wDosDate [I] Dos format date
1149 * wDosTime [I] Dos format time
1150 * pDateOut [O] Destination for VT_DATE format
1152 * RETURNS
1153 * Success: TRUE. pDateOut contains the converted time.
1154 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1156 * NOTES
1157 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1158 * - Dos format times are accurate to only 2 second precision.
1159 * - The format of a Dos Date is:
1160 *| Bits Values Meaning
1161 *| ---- ------ -------
1162 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1163 *| the days in the month rolls forward the extra days.
1164 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1165 *| year. 13-15 are invalid.
1166 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1167 * - The format of a Dos Time is:
1168 *| Bits Values Meaning
1169 *| ---- ------ -------
1170 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1171 *| 5-10 0-59 Minutes. 60-63 are invalid.
1172 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1174 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1175 double *pDateOut)
1177 UDATE ud;
1179 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1180 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1181 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1182 pDateOut);
1184 ud.st.wYear = DOS_YEAR(wDosDate);
1185 ud.st.wMonth = DOS_MONTH(wDosDate);
1186 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1187 return FALSE;
1188 ud.st.wDay = DOS_DAY(wDosDate);
1189 ud.st.wHour = DOS_HOUR(wDosTime);
1190 ud.st.wMinute = DOS_MINUTE(wDosTime);
1191 ud.st.wSecond = DOS_SECOND(wDosTime);
1192 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1194 return !VarDateFromUdate(&ud, 0, pDateOut);
1197 /**********************************************************************
1198 * VariantTimeToDosDateTime [OLEAUT32.13]
1200 * Convert a variant format date into a Dos format date and time.
1202 * dateIn [I] VT_DATE time format
1203 * pwDosDate [O] Destination for Dos format date
1204 * pwDosTime [O] Destination for Dos format time
1206 * RETURNS
1207 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1208 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1210 * NOTES
1211 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1213 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1215 UDATE ud;
1217 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1219 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1220 return FALSE;
1222 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1223 return FALSE;
1225 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1226 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1228 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1229 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1230 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1231 return TRUE;
1234 /***********************************************************************
1235 * SystemTimeToVariantTime [OLEAUT32.184]
1237 * Convert a System format date and time into variant VT_DATE format.
1239 * PARAMS
1240 * lpSt [I] System format date and time
1241 * pDateOut [O] Destination for VT_DATE format date
1243 * RETURNS
1244 * Success: TRUE. *pDateOut contains the converted value.
1245 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1247 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1249 UDATE ud;
1251 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1252 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1254 if (lpSt->wMonth > 12)
1255 return FALSE;
1257 ud.st = *lpSt;
1258 return !VarDateFromUdate(&ud, 0, pDateOut);
1261 /***********************************************************************
1262 * VariantTimeToSystemTime [OLEAUT32.185]
1264 * Convert a variant VT_DATE into a System format date and time.
1266 * PARAMS
1267 * datein [I] Variant VT_DATE format date
1268 * lpSt [O] Destination for System format date and time
1270 * RETURNS
1271 * Success: TRUE. *lpSt contains the converted value.
1272 * Failure: FALSE, if dateIn is too large or small.
1274 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1276 UDATE ud;
1278 TRACE("(%g,%p)\n", dateIn, lpSt);
1280 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1281 return FALSE;
1283 *lpSt = ud.st;
1284 return TRUE;
1287 /***********************************************************************
1288 * VarDateFromUdateEx [OLEAUT32.319]
1290 * Convert an unpacked format date and time to a variant VT_DATE.
1292 * PARAMS
1293 * pUdateIn [I] Unpacked format date and time to convert
1294 * lcid [I] Locale identifier for the conversion
1295 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1296 * pDateOut [O] Destination for variant VT_DATE.
1298 * RETURNS
1299 * Success: S_OK. *pDateOut contains the converted value.
1300 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1302 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1304 UDATE ud;
1305 double dateVal;
1307 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1308 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1309 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1310 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1311 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1313 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1314 FIXME("lcid possibly not handled, treating as en-us\n");
1316 ud = *pUdateIn;
1318 if (dwFlags & VAR_VALIDDATE)
1319 WARN("Ignoring VAR_VALIDDATE\n");
1321 if (FAILED(VARIANT_RollUdate(&ud)))
1322 return E_INVALIDARG;
1324 /* Date */
1325 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1327 /* Time */
1328 dateVal += ud.st.wHour / 24.0;
1329 dateVal += ud.st.wMinute / 1440.0;
1330 dateVal += ud.st.wSecond / 86400.0;
1331 dateVal += ud.st.wMilliseconds / 86400000.0;
1333 TRACE("Returning %g\n", dateVal);
1334 *pDateOut = dateVal;
1335 return S_OK;
1338 /***********************************************************************
1339 * VarDateFromUdate [OLEAUT32.330]
1341 * Convert an unpacked format date and time to a variant VT_DATE.
1343 * PARAMS
1344 * pUdateIn [I] Unpacked format date and time to convert
1345 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1346 * pDateOut [O] Destination for variant VT_DATE.
1348 * RETURNS
1349 * Success: S_OK. *pDateOut contains the converted value.
1350 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1352 * NOTES
1353 * This function uses the United States English locale for the conversion. Use
1354 * VarDateFromUdateEx() for alternate locales.
1356 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1358 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1360 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1363 /***********************************************************************
1364 * VarUdateFromDate [OLEAUT32.331]
1366 * Convert a variant VT_DATE into an unpacked format date and time.
1368 * PARAMS
1369 * datein [I] Variant VT_DATE format date
1370 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1371 * lpUdate [O] Destination for unpacked format date and time
1373 * RETURNS
1374 * Success: S_OK. *lpUdate contains the converted value.
1375 * Failure: E_INVALIDARG, if dateIn is too large or small.
1377 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1379 /* Cumulative totals of days per month */
1380 static const USHORT cumulativeDays[] =
1382 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1384 double datePart, timePart;
1385 int julianDays;
1387 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1389 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1390 return E_INVALIDARG;
1392 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1393 /* Compensate for int truncation (always downwards) */
1394 timePart = dateIn - datePart + 0.00000000001;
1395 if (timePart >= 1.0)
1396 timePart -= 0.00000000001;
1398 /* Date */
1399 julianDays = VARIANT_JulianFromDate(dateIn);
1400 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1401 &lpUdate->st.wDay);
1403 datePart = (datePart + 1.5) / 7.0;
1404 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1405 if (lpUdate->st.wDayOfWeek == 0)
1406 lpUdate->st.wDayOfWeek = 5;
1407 else if (lpUdate->st.wDayOfWeek == 1)
1408 lpUdate->st.wDayOfWeek = 6;
1409 else
1410 lpUdate->st.wDayOfWeek -= 2;
1412 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1413 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1414 else
1415 lpUdate->wDayOfYear = 0;
1417 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1418 lpUdate->wDayOfYear += lpUdate->st.wDay;
1420 /* Time */
1421 timePart *= 24.0;
1422 lpUdate->st.wHour = timePart;
1423 timePart -= lpUdate->st.wHour;
1424 timePart *= 60.0;
1425 lpUdate->st.wMinute = timePart;
1426 timePart -= lpUdate->st.wMinute;
1427 timePart *= 60.0;
1428 lpUdate->st.wSecond = timePart;
1429 timePart -= lpUdate->st.wSecond;
1430 lpUdate->st.wMilliseconds = 0;
1431 if (timePart > 0.5)
1433 /* Round the milliseconds, adjusting the time/date forward if needed */
1434 if (lpUdate->st.wSecond < 59)
1435 lpUdate->st.wSecond++;
1436 else
1438 lpUdate->st.wSecond = 0;
1439 if (lpUdate->st.wMinute < 59)
1440 lpUdate->st.wMinute++;
1441 else
1443 lpUdate->st.wMinute = 0;
1444 if (lpUdate->st.wHour < 23)
1445 lpUdate->st.wHour++;
1446 else
1448 lpUdate->st.wHour = 0;
1449 /* Roll over a whole day */
1450 if (++lpUdate->st.wDay > 28)
1451 VARIANT_RollUdate(lpUdate);
1456 return S_OK;
1459 #define GET_NUMBER_TEXT(fld,name) \
1460 buff[0] = 0; \
1461 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1462 WARN("buffer too small for " #fld "\n"); \
1463 else \
1464 if (buff[0]) lpChars->name = buff[0]; \
1465 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1467 /* Get the valid number characters for an lcid */
1468 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1470 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1471 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1472 WCHAR buff[4];
1474 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1475 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1476 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1477 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1478 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1479 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1480 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1482 /* Local currency symbols are often 2 characters */
1483 lpChars->cCurrencyLocal2 = '\0';
1484 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1486 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1487 case 2: lpChars->cCurrencyLocal = buff[0];
1488 break;
1489 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1491 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1492 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1495 /* Number Parsing States */
1496 #define B_PROCESSING_EXPONENT 0x1
1497 #define B_NEGATIVE_EXPONENT 0x2
1498 #define B_EXPONENT_START 0x4
1499 #define B_INEXACT_ZEROS 0x8
1500 #define B_LEADING_ZERO 0x10
1501 #define B_PROCESSING_HEX 0x20
1502 #define B_PROCESSING_OCT 0x40
1504 /**********************************************************************
1505 * VarParseNumFromStr [OLEAUT32.46]
1507 * Parse a string containing a number into a NUMPARSE structure.
1509 * PARAMS
1510 * lpszStr [I] String to parse number from
1511 * lcid [I] Locale Id for the conversion
1512 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1513 * pNumprs [I/O] Destination for parsed number
1514 * rgbDig [O] Destination for digits read in
1516 * RETURNS
1517 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1518 * the number.
1519 * Failure: E_INVALIDARG, if any parameter is invalid.
1520 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1521 * incorrectly.
1522 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1524 * NOTES
1525 * pNumprs must have the following fields set:
1526 * cDig: Set to the size of rgbDig.
1527 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1528 * from "oleauto.h".
1530 * FIXME
1531 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1532 * numerals, so this has not been implemented.
1534 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1535 NUMPARSE *pNumprs, BYTE *rgbDig)
1537 VARIANT_NUMBER_CHARS chars;
1538 BYTE rgbTmp[1024];
1539 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1540 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1541 int cchUsed = 0;
1543 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1545 if (!pNumprs || !rgbDig)
1546 return E_INVALIDARG;
1548 if (pNumprs->cDig < iMaxDigits)
1549 iMaxDigits = pNumprs->cDig;
1551 pNumprs->cDig = 0;
1552 pNumprs->dwOutFlags = 0;
1553 pNumprs->cchUsed = 0;
1554 pNumprs->nBaseShift = 0;
1555 pNumprs->nPwr10 = 0;
1557 if (!lpszStr)
1558 return DISP_E_TYPEMISMATCH;
1560 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1562 /* First consume all the leading symbols and space from the string */
1563 while (1)
1565 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1567 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1570 cchUsed++;
1571 lpszStr++;
1572 } while (isspaceW(*lpszStr));
1574 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1575 *lpszStr == chars.cPositiveSymbol &&
1576 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1578 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1579 cchUsed++;
1580 lpszStr++;
1582 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1583 *lpszStr == chars.cNegativeSymbol &&
1584 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1586 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1587 cchUsed++;
1588 lpszStr++;
1590 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1591 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1592 *lpszStr == chars.cCurrencyLocal &&
1593 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1595 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1596 cchUsed++;
1597 lpszStr++;
1598 /* Only accept currency characters */
1599 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1600 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1602 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1603 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1605 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1606 cchUsed++;
1607 lpszStr++;
1609 else
1610 break;
1613 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1615 /* Only accept non-currency characters */
1616 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1617 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1620 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1621 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1623 dwState |= B_PROCESSING_HEX;
1624 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1625 cchUsed=cchUsed+2;
1626 lpszStr=lpszStr+2;
1628 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1629 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1631 dwState |= B_PROCESSING_OCT;
1632 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1633 cchUsed=cchUsed+2;
1634 lpszStr=lpszStr+2;
1637 /* Strip Leading zeros */
1638 while (*lpszStr == '0')
1640 dwState |= B_LEADING_ZERO;
1641 cchUsed++;
1642 lpszStr++;
1645 while (*lpszStr)
1647 if (isdigitW(*lpszStr))
1649 if (dwState & B_PROCESSING_EXPONENT)
1651 int exponentSize = 0;
1652 if (dwState & B_EXPONENT_START)
1654 if (!isdigitW(*lpszStr))
1655 break; /* No exponent digits - invalid */
1656 while (*lpszStr == '0')
1658 /* Skip leading zero's in the exponent */
1659 cchUsed++;
1660 lpszStr++;
1664 while (isdigitW(*lpszStr))
1666 exponentSize *= 10;
1667 exponentSize += *lpszStr - '0';
1668 cchUsed++;
1669 lpszStr++;
1671 if (dwState & B_NEGATIVE_EXPONENT)
1672 exponentSize = -exponentSize;
1673 /* Add the exponent into the powers of 10 */
1674 pNumprs->nPwr10 += exponentSize;
1675 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1676 lpszStr--; /* back up to allow processing of next char */
1678 else
1680 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1681 && !(dwState & B_PROCESSING_OCT))
1683 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1685 if (*lpszStr != '0')
1686 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1688 /* This digit can't be represented, but count it in nPwr10 */
1689 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1690 pNumprs->nPwr10--;
1691 else
1692 pNumprs->nPwr10++;
1694 else
1696 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1697 return DISP_E_TYPEMISMATCH;
1700 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1701 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1703 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1705 pNumprs->cDig++;
1706 cchUsed++;
1709 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1711 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1712 cchUsed++;
1714 else if (*lpszStr == chars.cDecimalPoint &&
1715 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1716 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1718 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1719 cchUsed++;
1721 /* If we have no digits so far, skip leading zeros */
1722 if (!pNumprs->cDig)
1724 while (lpszStr[1] == '0')
1726 dwState |= B_LEADING_ZERO;
1727 cchUsed++;
1728 lpszStr++;
1729 pNumprs->nPwr10--;
1733 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1734 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1735 dwState & B_PROCESSING_HEX)
1737 if (pNumprs->cDig >= iMaxDigits)
1739 return DISP_E_OVERFLOW;
1741 else
1743 if (*lpszStr >= 'a')
1744 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1745 else
1746 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1748 pNumprs->cDig++;
1749 cchUsed++;
1751 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1752 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1753 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1755 dwState |= B_PROCESSING_EXPONENT;
1756 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1757 cchUsed++;
1759 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1761 cchUsed++; /* Ignore positive exponent */
1763 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1765 dwState |= B_NEGATIVE_EXPONENT;
1766 cchUsed++;
1768 else
1769 break; /* Stop at an unrecognised character */
1771 lpszStr++;
1774 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1776 /* Ensure a 0 on its own gets stored */
1777 pNumprs->cDig = 1;
1778 rgbTmp[0] = 0;
1781 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1783 pNumprs->cchUsed = cchUsed;
1784 WARN("didn't completely parse exponent\n");
1785 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1788 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1790 if (dwState & B_INEXACT_ZEROS)
1791 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1792 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1794 /* copy all of the digits into the output digit buffer */
1795 /* this is exactly what windows does although it also returns */
1796 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1797 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1799 if (dwState & B_PROCESSING_HEX) {
1800 /* hex numbers have always the same format */
1801 pNumprs->nPwr10=0;
1802 pNumprs->nBaseShift=4;
1803 } else {
1804 if (dwState & B_PROCESSING_OCT) {
1805 /* oct numbers have always the same format */
1806 pNumprs->nPwr10=0;
1807 pNumprs->nBaseShift=3;
1808 } else {
1809 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1811 pNumprs->nPwr10++;
1812 pNumprs->cDig--;
1816 } else
1818 /* Remove trailing zeros from the last (whole number or decimal) part */
1819 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1821 pNumprs->nPwr10++;
1822 pNumprs->cDig--;
1826 if (pNumprs->cDig <= iMaxDigits)
1827 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1828 else
1829 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1831 /* Copy the digits we processed into rgbDig */
1832 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1834 /* Consume any trailing symbols and space */
1835 while (1)
1837 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1839 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1842 cchUsed++;
1843 lpszStr++;
1844 } while (isspaceW(*lpszStr));
1846 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1847 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1848 *lpszStr == chars.cPositiveSymbol)
1850 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1851 cchUsed++;
1852 lpszStr++;
1854 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1855 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1856 *lpszStr == chars.cNegativeSymbol)
1858 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1859 cchUsed++;
1860 lpszStr++;
1862 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1863 pNumprs->dwOutFlags & NUMPRS_PARENS)
1865 cchUsed++;
1866 lpszStr++;
1867 pNumprs->dwOutFlags |= NUMPRS_NEG;
1869 else
1870 break;
1873 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1875 pNumprs->cchUsed = cchUsed;
1876 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1879 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1880 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1882 if (!pNumprs->cDig)
1883 return DISP_E_TYPEMISMATCH; /* No Number found */
1885 pNumprs->cchUsed = cchUsed;
1886 return S_OK;
1889 /* VTBIT flags indicating an integer value */
1890 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1891 /* VTBIT flags indicating a real number value */
1892 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1894 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1895 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1896 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1897 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1899 /**********************************************************************
1900 * VarNumFromParseNum [OLEAUT32.47]
1902 * Convert a NUMPARSE structure into a numeric Variant type.
1904 * PARAMS
1905 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1906 * rgbDig [I] Source for the numbers digits
1907 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1908 * pVarDst [O] Destination for the converted Variant value.
1910 * RETURNS
1911 * Success: S_OK. pVarDst contains the converted value.
1912 * Failure: E_INVALIDARG, if any parameter is invalid.
1913 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1915 * NOTES
1916 * - The smallest favoured type present in dwVtBits that can represent the
1917 * number in pNumprs without losing precision is used.
1918 * - Signed types are preferrred over unsigned types of the same size.
1919 * - Preferred types in order are: integer, float, double, currency then decimal.
1920 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1921 * for details of the rounding method.
1922 * - pVarDst is not cleared before the result is stored in it.
1923 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1924 * design?): If some other VTBIT's for integers are specified together
1925 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1926 * the number to the smallest requested integer truncating this way the
1927 * number. Wine dosn't implement this "feature" (yet?).
1929 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1930 ULONG dwVtBits, VARIANT *pVarDst)
1932 /* Scale factors and limits for double arithmetic */
1933 static const double dblMultipliers[11] = {
1934 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1935 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1937 static const double dblMinimums[11] = {
1938 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1939 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1940 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1942 static const double dblMaximums[11] = {
1943 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1944 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1945 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1948 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1950 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1952 if (pNumprs->nBaseShift)
1954 /* nBaseShift indicates a hex or octal number */
1955 ULONG64 ul64 = 0;
1956 LONG64 l64;
1957 int i;
1959 /* Convert the hex or octal number string into a UI64 */
1960 for (i = 0; i < pNumprs->cDig; i++)
1962 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1964 TRACE("Overflow multiplying digits\n");
1965 return DISP_E_OVERFLOW;
1967 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1970 /* also make a negative representation */
1971 l64=-ul64;
1973 /* Try signed and unsigned types in size order */
1974 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
1976 V_VT(pVarDst) = VT_I1;
1977 V_I1(pVarDst) = ul64;
1978 return S_OK;
1980 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
1982 V_VT(pVarDst) = VT_UI1;
1983 V_UI1(pVarDst) = ul64;
1984 return S_OK;
1986 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
1988 V_VT(pVarDst) = VT_I2;
1989 V_I2(pVarDst) = ul64;
1990 return S_OK;
1992 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
1994 V_VT(pVarDst) = VT_UI2;
1995 V_UI2(pVarDst) = ul64;
1996 return S_OK;
1998 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2000 V_VT(pVarDst) = VT_I4;
2001 V_I4(pVarDst) = ul64;
2002 return S_OK;
2004 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2006 V_VT(pVarDst) = VT_UI4;
2007 V_UI4(pVarDst) = ul64;
2008 return S_OK;
2010 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2012 V_VT(pVarDst) = VT_I8;
2013 V_I8(pVarDst) = ul64;
2014 return S_OK;
2016 else if (dwVtBits & VTBIT_UI8)
2018 V_VT(pVarDst) = VT_UI8;
2019 V_UI8(pVarDst) = ul64;
2020 return S_OK;
2022 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2024 V_VT(pVarDst) = VT_DECIMAL;
2025 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2026 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2027 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2028 return S_OK;
2030 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2032 V_VT(pVarDst) = VT_R4;
2033 if (ul64 <= I4_MAX)
2034 V_R4(pVarDst) = ul64;
2035 else
2036 V_R4(pVarDst) = l64;
2037 return S_OK;
2039 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2041 V_VT(pVarDst) = VT_R8;
2042 if (ul64 <= I4_MAX)
2043 V_R8(pVarDst) = ul64;
2044 else
2045 V_R8(pVarDst) = l64;
2046 return S_OK;
2049 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2050 return DISP_E_OVERFLOW;
2053 /* Count the number of relevant fractional and whole digits stored,
2054 * And compute the divisor/multiplier to scale the number by.
2056 if (pNumprs->nPwr10 < 0)
2058 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2060 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2061 wholeNumberDigits = 0;
2062 fractionalDigits = pNumprs->cDig;
2063 divisor10 = -pNumprs->nPwr10;
2065 else
2067 /* An exactly represented real number e.g. 1.024 */
2068 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2069 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2070 divisor10 = pNumprs->cDig - wholeNumberDigits;
2073 else if (pNumprs->nPwr10 == 0)
2075 /* An exactly represented whole number e.g. 1024 */
2076 wholeNumberDigits = pNumprs->cDig;
2077 fractionalDigits = 0;
2079 else /* pNumprs->nPwr10 > 0 */
2081 /* A whole number followed by nPwr10 0's e.g. 102400 */
2082 wholeNumberDigits = pNumprs->cDig;
2083 fractionalDigits = 0;
2084 multiplier10 = pNumprs->nPwr10;
2087 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2088 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2089 multiplier10, divisor10);
2091 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2092 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2094 /* We have one or more integer output choices, and either:
2095 * 1) An integer input value, or
2096 * 2) A real number input value but no floating output choices.
2097 * Alternately, we have a DECIMAL output available and an integer input.
2099 * So, place the integer value into pVarDst, using the smallest type
2100 * possible and preferring signed over unsigned types.
2102 BOOL bOverflow = FALSE, bNegative;
2103 ULONG64 ul64 = 0;
2104 int i;
2106 /* Convert the integer part of the number into a UI8 */
2107 for (i = 0; i < wholeNumberDigits; i++)
2109 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2111 TRACE("Overflow multiplying digits\n");
2112 bOverflow = TRUE;
2113 break;
2115 ul64 = ul64 * 10 + rgbDig[i];
2118 /* Account for the scale of the number */
2119 if (!bOverflow && multiplier10)
2121 for (i = 0; i < multiplier10; i++)
2123 if (ul64 > (UI8_MAX / 10))
2125 TRACE("Overflow scaling number\n");
2126 bOverflow = TRUE;
2127 break;
2129 ul64 = ul64 * 10;
2133 /* If we have any fractional digits, round the value.
2134 * Note we don't have to do this if divisor10 is < 1,
2135 * because this means the fractional part must be < 0.5
2137 if (!bOverflow && fractionalDigits && divisor10 > 0)
2139 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2140 BOOL bAdjust = FALSE;
2142 TRACE("first decimal value is %d\n", *fracDig);
2144 if (*fracDig > 5)
2145 bAdjust = TRUE; /* > 0.5 */
2146 else if (*fracDig == 5)
2148 for (i = 1; i < fractionalDigits; i++)
2150 if (fracDig[i])
2152 bAdjust = TRUE; /* > 0.5 */
2153 break;
2156 /* If exactly 0.5, round only odd values */
2157 if (i == fractionalDigits && (ul64 & 1))
2158 bAdjust = TRUE;
2161 if (bAdjust)
2163 if (ul64 == UI8_MAX)
2165 TRACE("Overflow after rounding\n");
2166 bOverflow = TRUE;
2168 ul64++;
2172 /* Zero is not a negative number */
2173 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2175 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2177 /* For negative integers, try the signed types in size order */
2178 if (!bOverflow && bNegative)
2180 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2182 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2184 V_VT(pVarDst) = VT_I1;
2185 V_I1(pVarDst) = -ul64;
2186 return S_OK;
2188 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2190 V_VT(pVarDst) = VT_I2;
2191 V_I2(pVarDst) = -ul64;
2192 return S_OK;
2194 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2196 V_VT(pVarDst) = VT_I4;
2197 V_I4(pVarDst) = -ul64;
2198 return S_OK;
2200 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2202 V_VT(pVarDst) = VT_I8;
2203 V_I8(pVarDst) = -ul64;
2204 return S_OK;
2206 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2208 /* Decimal is only output choice left - fast path */
2209 V_VT(pVarDst) = VT_DECIMAL;
2210 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2211 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2212 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2213 return S_OK;
2217 else if (!bOverflow)
2219 /* For positive integers, try signed then unsigned types in size order */
2220 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2222 V_VT(pVarDst) = VT_I1;
2223 V_I1(pVarDst) = ul64;
2224 return S_OK;
2226 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2228 V_VT(pVarDst) = VT_UI1;
2229 V_UI1(pVarDst) = ul64;
2230 return S_OK;
2232 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2234 V_VT(pVarDst) = VT_I2;
2235 V_I2(pVarDst) = ul64;
2236 return S_OK;
2238 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2240 V_VT(pVarDst) = VT_UI2;
2241 V_UI2(pVarDst) = ul64;
2242 return S_OK;
2244 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2246 V_VT(pVarDst) = VT_I4;
2247 V_I4(pVarDst) = ul64;
2248 return S_OK;
2250 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2252 V_VT(pVarDst) = VT_UI4;
2253 V_UI4(pVarDst) = ul64;
2254 return S_OK;
2256 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2258 V_VT(pVarDst) = VT_I8;
2259 V_I8(pVarDst) = ul64;
2260 return S_OK;
2262 else if (dwVtBits & VTBIT_UI8)
2264 V_VT(pVarDst) = VT_UI8;
2265 V_UI8(pVarDst) = ul64;
2266 return S_OK;
2268 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2270 /* Decimal is only output choice left - fast path */
2271 V_VT(pVarDst) = VT_DECIMAL;
2272 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2273 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2274 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2275 return S_OK;
2280 if (dwVtBits & REAL_VTBITS)
2282 /* Try to put the number into a float or real */
2283 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2284 double whole = 0.0;
2285 int i;
2287 /* Convert the number into a double */
2288 for (i = 0; i < pNumprs->cDig; i++)
2289 whole = whole * 10.0 + rgbDig[i];
2291 TRACE("Whole double value is %16.16g\n", whole);
2293 /* Account for the scale */
2294 while (multiplier10 > 10)
2296 if (whole > dblMaximums[10])
2298 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2299 bOverflow = TRUE;
2300 break;
2302 whole = whole * dblMultipliers[10];
2303 multiplier10 -= 10;
2305 if (multiplier10)
2307 if (whole > dblMaximums[multiplier10])
2309 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2310 bOverflow = TRUE;
2312 else
2313 whole = whole * dblMultipliers[multiplier10];
2316 TRACE("Scaled double value is %16.16g\n", whole);
2318 while (divisor10 > 10)
2320 if (whole < dblMinimums[10] && whole != 0)
2322 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2323 bOverflow = TRUE;
2324 break;
2326 whole = whole / dblMultipliers[10];
2327 divisor10 -= 10;
2329 if (divisor10)
2331 if (whole < dblMinimums[divisor10] && whole != 0)
2333 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2334 bOverflow = TRUE;
2336 else
2337 whole = whole / dblMultipliers[divisor10];
2339 if (!bOverflow)
2340 TRACE("Final double value is %16.16g\n", whole);
2342 if (dwVtBits & VTBIT_R4 &&
2343 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2345 TRACE("Set R4 to final value\n");
2346 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2347 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2348 return S_OK;
2351 if (dwVtBits & VTBIT_R8)
2353 TRACE("Set R8 to final value\n");
2354 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2355 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2356 return S_OK;
2359 if (dwVtBits & VTBIT_CY)
2361 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2363 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2364 TRACE("Set CY to final value\n");
2365 return S_OK;
2367 TRACE("Value Overflows CY\n");
2371 if (dwVtBits & VTBIT_DECIMAL)
2373 int i;
2374 ULONG carry;
2375 ULONG64 tmp;
2376 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2378 DECIMAL_SETZERO(*pDec);
2379 DEC_LO32(pDec) = 0;
2381 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2382 DEC_SIGN(pDec) = DECIMAL_NEG;
2383 else
2384 DEC_SIGN(pDec) = DECIMAL_POS;
2386 /* Factor the significant digits */
2387 for (i = 0; i < pNumprs->cDig; i++)
2389 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2390 carry = (ULONG)(tmp >> 32);
2391 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2392 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2393 carry = (ULONG)(tmp >> 32);
2394 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2395 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2396 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2398 if (tmp >> 32 & UI4_MAX)
2400 VarNumFromParseNum_DecOverflow:
2401 TRACE("Overflow\n");
2402 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2403 return DISP_E_OVERFLOW;
2407 /* Account for the scale of the number */
2408 while (multiplier10 > 0)
2410 tmp = (ULONG64)DEC_LO32(pDec) * 10;
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)
2420 goto VarNumFromParseNum_DecOverflow;
2421 multiplier10--;
2423 DEC_SCALE(pDec) = divisor10;
2425 V_VT(pVarDst) = VT_DECIMAL;
2426 return S_OK;
2428 return DISP_E_OVERFLOW; /* No more output choices */
2431 /**********************************************************************
2432 * VarCat [OLEAUT32.318]
2434 * Concatenates one variant onto another.
2436 * PARAMS
2437 * left [I] First variant
2438 * right [I] Second variant
2439 * result [O] Result variant
2441 * RETURNS
2442 * Success: S_OK.
2443 * Failure: An HRESULT error code indicating the error.
2445 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2447 VARTYPE leftvt,rightvt,resultvt;
2448 HRESULT hres;
2449 static const WCHAR str_true[] = {'T','r','u','e','\0'};
2450 static const WCHAR str_false[] = {'F','a','l','s','e','\0'};
2451 static const WCHAR sz_empty[] = {'\0'};
2452 leftvt = V_VT(left);
2453 rightvt = V_VT(right);
2455 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2456 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2458 /* when both left and right are NULL the result is NULL */
2459 if (leftvt == VT_NULL && rightvt == VT_NULL)
2461 V_VT(out) = VT_NULL;
2462 return S_OK;
2465 hres = S_OK;
2466 resultvt = VT_EMPTY;
2468 /* There are many special case for errors and return types */
2469 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2470 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2471 hres = DISP_E_TYPEMISMATCH;
2472 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2473 leftvt == VT_R4 || leftvt == VT_R8 ||
2474 leftvt == VT_CY || leftvt == VT_BOOL ||
2475 leftvt == VT_BSTR || leftvt == VT_I1 ||
2476 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2477 leftvt == VT_UI4 || leftvt == VT_I8 ||
2478 leftvt == VT_UI8 || leftvt == VT_INT ||
2479 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2480 leftvt == VT_NULL || leftvt == VT_DATE ||
2481 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2483 (rightvt == VT_I2 || rightvt == VT_I4 ||
2484 rightvt == VT_R4 || rightvt == VT_R8 ||
2485 rightvt == VT_CY || rightvt == VT_BOOL ||
2486 rightvt == VT_BSTR || rightvt == VT_I1 ||
2487 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2488 rightvt == VT_UI4 || rightvt == VT_I8 ||
2489 rightvt == VT_UI8 || rightvt == VT_INT ||
2490 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2491 rightvt == VT_NULL || rightvt == VT_DATE ||
2492 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2493 resultvt = VT_BSTR;
2494 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2495 hres = DISP_E_TYPEMISMATCH;
2496 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2497 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2498 hres = DISP_E_TYPEMISMATCH;
2499 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2500 rightvt == VT_DECIMAL)
2501 hres = DISP_E_BADVARTYPE;
2502 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2503 hres = DISP_E_TYPEMISMATCH;
2504 else if (leftvt == VT_VARIANT)
2505 hres = DISP_E_TYPEMISMATCH;
2506 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2507 leftvt == VT_NULL || leftvt == VT_I2 ||
2508 leftvt == VT_I4 || leftvt == VT_R4 ||
2509 leftvt == VT_R8 || leftvt == VT_CY ||
2510 leftvt == VT_DATE || leftvt == VT_BSTR ||
2511 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2512 leftvt == VT_I1 || leftvt == VT_UI1 ||
2513 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2514 leftvt == VT_I8 || leftvt == VT_UI8 ||
2515 leftvt == VT_INT || leftvt == VT_UINT))
2516 hres = DISP_E_TYPEMISMATCH;
2517 else
2518 hres = DISP_E_BADVARTYPE;
2520 /* if resutl type is not S_OK, then no need to go further */
2521 if (hres != S_OK)
2523 V_VT(out) = resultvt;
2524 return hres;
2526 /* Else proceed with formatting inputs to strings */
2527 else
2529 VARIANT bstrvar_left, bstrvar_right;
2530 V_VT(out) = VT_BSTR;
2532 VariantInit(&bstrvar_left);
2533 VariantInit(&bstrvar_right);
2535 /* Convert left side variant to string */
2536 if (leftvt != VT_BSTR)
2538 if (leftvt == VT_BOOL)
2540 /* Bools are handled as True/False strings instead of 0/-1 as in MSDN */
2541 V_VT(&bstrvar_left) = VT_BSTR;
2542 if (V_BOOL(left) == TRUE)
2543 V_BSTR(&bstrvar_left) = SysAllocString(str_true);
2544 else
2545 V_BSTR(&bstrvar_left) = SysAllocString(str_false);
2547 /* Fill with empty string for later concat with right side */
2548 else if (leftvt == VT_NULL)
2550 V_VT(&bstrvar_left) = VT_BSTR;
2551 V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
2553 else
2555 hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
2556 if (hres != S_OK) {
2557 VariantClear(&bstrvar_left);
2558 VariantClear(&bstrvar_right);
2559 if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
2560 rightvt == VT_NULL || rightvt == VT_I2 ||
2561 rightvt == VT_I4 || rightvt == VT_R4 ||
2562 rightvt == VT_R8 || rightvt == VT_CY ||
2563 rightvt == VT_DATE || rightvt == VT_BSTR ||
2564 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
2565 rightvt == VT_I1 || rightvt == VT_UI1 ||
2566 rightvt == VT_UI2 || rightvt == VT_UI4 ||
2567 rightvt == VT_I8 || rightvt == VT_UI8 ||
2568 rightvt == VT_INT || rightvt == VT_UINT))
2569 return DISP_E_BADVARTYPE;
2570 return hres;
2575 /* convert right side variant to string */
2576 if (rightvt != VT_BSTR)
2578 if (rightvt == VT_BOOL)
2580 /* Bools are handled as True/False strings instead of 0/-1 as in MSDN */
2581 V_VT(&bstrvar_right) = VT_BSTR;
2582 if (V_BOOL(right) == TRUE)
2583 V_BSTR(&bstrvar_right) = SysAllocString(str_true);
2584 else
2585 V_BSTR(&bstrvar_right) = SysAllocString(str_false);
2587 /* Fill with empty string for later concat with right side */
2588 else if (rightvt == VT_NULL)
2590 V_VT(&bstrvar_right) = VT_BSTR;
2591 V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
2593 else
2595 hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
2596 if (hres != S_OK) {
2597 VariantClear(&bstrvar_left);
2598 VariantClear(&bstrvar_right);
2599 if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
2600 leftvt == VT_NULL || leftvt == VT_I2 ||
2601 leftvt == VT_I4 || leftvt == VT_R4 ||
2602 leftvt == VT_R8 || leftvt == VT_CY ||
2603 leftvt == VT_DATE || leftvt == VT_BSTR ||
2604 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2605 leftvt == VT_I1 || leftvt == VT_UI1 ||
2606 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2607 leftvt == VT_I8 || leftvt == VT_UI8 ||
2608 leftvt == VT_INT || leftvt == VT_UINT))
2609 return DISP_E_BADVARTYPE;
2610 return hres;
2615 /* Concat the resulting strings together */
2616 if (leftvt == VT_BSTR && rightvt == VT_BSTR)
2617 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2618 else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
2619 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2620 else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
2621 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
2622 else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
2623 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2625 VariantClear(&bstrvar_left);
2626 VariantClear(&bstrvar_right);
2627 return S_OK;
2632 /* Wrapper around VariantChangeTypeEx() which permits changing a
2633 variant with VT_RESERVED flag set. Needed by VarCmp. */
2634 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2635 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2637 HRESULT res;
2638 VARTYPE flags;
2640 flags = V_VT(pvargSrc) & ~VT_TYPEMASK;
2641 V_VT(pvargSrc) &= ~VT_RESERVED;
2642 res = VariantChangeTypeEx(pvargDest,pvargSrc,lcid,wFlags,vt);
2643 V_VT(pvargSrc) |= flags;
2645 return res;
2648 /**********************************************************************
2649 * VarCmp [OLEAUT32.176]
2651 * Compare two variants.
2653 * PARAMS
2654 * left [I] First variant
2655 * right [I] Second variant
2656 * lcid [I] LCID (locale identifier) for the comparison
2657 * flags [I] Flags to be used in the comparison:
2658 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2659 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2661 * RETURNS
2662 * VARCMP_LT: left variant is less than right variant.
2663 * VARCMP_EQ: input variants are equal.
2664 * VARCMP_GT: left variant is greater than right variant.
2665 * VARCMP_NULL: either one of the input variants is NULL.
2666 * Failure: An HRESULT error code indicating the error.
2668 * NOTES
2669 * Native VarCmp up to and including WinXP dosn't like as input variants
2670 * I1, UI2, VT_UI4, UI8 and UINT. INT is accepted only as left variant.
2672 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2673 * an ERROR variant will trigger an error.
2675 * Both input variants can have VT_RESERVED flag set which is ignored
2676 * unless one and only one of the variants is a BSTR and the other one
2677 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2678 * different meaning:
2679 * - BSTR and other: BSTR is always greater than the other variant.
2680 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2681 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2682 * comparison will take place else the BSTR is always greater.
2683 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2684 * variant is ignored and the return value depends only on the sign
2685 * of the BSTR if it is a number else the BSTR is always greater. A
2686 * positive BSTR is greater, a negative one is smaller than the other
2687 * variant.
2689 * SEE
2690 * VarBstrCmp for the lcid and flags usage.
2692 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2694 VARTYPE lvt, rvt, vt;
2695 VARIANT rv,lv;
2696 DWORD xmask;
2697 HRESULT rc;
2699 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%08x)\n", left, debugstr_VT(left),
2700 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2702 lvt = V_VT(left) & VT_TYPEMASK;
2703 rvt = V_VT(right) & VT_TYPEMASK;
2704 xmask = (1 << lvt) | (1 << rvt);
2706 /* If we have any flag set except VT_RESERVED bail out.
2707 Same for the left input variant type > VT_INT and for the
2708 right input variant type > VT_I8. Yes, VT_INT is only supported
2709 as left variant. Go figure */
2710 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2711 lvt > VT_INT || rvt > VT_I8) {
2712 return DISP_E_BADVARTYPE;
2715 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2716 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2717 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2718 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2719 return DISP_E_TYPEMISMATCH;
2721 /* If both variants are VT_ERROR return VARCMP_EQ */
2722 if (xmask == VTBIT_ERROR)
2723 return VARCMP_EQ;
2724 else if (xmask & VTBIT_ERROR)
2725 return DISP_E_TYPEMISMATCH;
2727 if (xmask & VTBIT_NULL)
2728 return VARCMP_NULL;
2730 VariantInit(&lv);
2731 VariantInit(&rv);
2733 /* Two BSTRs, ignore VT_RESERVED */
2734 if (xmask == VTBIT_BSTR)
2735 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2737 /* A BSTR and an other variant; we have to take care of VT_RESERVED */
2738 if (xmask & VTBIT_BSTR) {
2739 VARIANT *bstrv, *nonbv;
2740 VARTYPE nonbvt;
2741 int swap = 0;
2743 /* Swap the variants so the BSTR is always on the left */
2744 if (lvt == VT_BSTR) {
2745 bstrv = left;
2746 nonbv = right;
2747 nonbvt = rvt;
2748 } else {
2749 swap = 1;
2750 bstrv = right;
2751 nonbv = left;
2752 nonbvt = lvt;
2755 /* BSTR and EMPTY: ignore VT_RESERVED */
2756 if (nonbvt == VT_EMPTY)
2757 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2758 else {
2759 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2760 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2762 if (!breserv && !nreserv)
2763 /* No VT_RESERVED set ==> BSTR always greater */
2764 rc = VARCMP_GT;
2765 else if (breserv && !nreserv) {
2766 /* BSTR has VT_RESERVED set. Do a string comparison */
2767 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2768 if (FAILED(rc))
2769 return rc;
2770 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2771 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2772 /* Non NULL nor empty BSTR */
2773 /* If the BSTR is not a number the BSTR is greater */
2774 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2775 if (FAILED(rc))
2776 rc = VARCMP_GT;
2777 else if (breserv && nreserv)
2778 /* FIXME: This is strange: with both VT_RESERVED set it
2779 looks like the result depends only on the sign of
2780 the BSTR number */
2781 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2782 else
2783 /* Numeric comparison, will be handled below.
2784 VARCMP_NULL used only to break out. */
2785 rc = VARCMP_NULL;
2786 VariantClear(&lv);
2787 VariantClear(&rv);
2788 } else
2789 /* Empty or NULL BSTR */
2790 rc = VARCMP_GT;
2792 /* Fixup the return code if we swapped left and right */
2793 if (swap) {
2794 if (rc == VARCMP_GT)
2795 rc = VARCMP_LT;
2796 else if (rc == VARCMP_LT)
2797 rc = VARCMP_GT;
2799 if (rc != VARCMP_NULL)
2800 return rc;
2803 if (xmask & VTBIT_DECIMAL)
2804 vt = VT_DECIMAL;
2805 else if (xmask & VTBIT_BSTR)
2806 vt = VT_R8;
2807 else if (xmask & VTBIT_R4)
2808 vt = VT_R4;
2809 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2810 vt = VT_R8;
2811 else if (xmask & VTBIT_CY)
2812 vt = VT_CY;
2813 else
2814 /* default to I8 */
2815 vt = VT_I8;
2817 /* Coerce the variants */
2818 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2819 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2820 /* Overflow, change to R8 */
2821 vt = VT_R8;
2822 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2824 if (FAILED(rc))
2825 return rc;
2826 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2827 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2828 /* Overflow, change to R8 */
2829 vt = VT_R8;
2830 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2831 if (FAILED(rc))
2832 return rc;
2833 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2835 if (FAILED(rc))
2836 return rc;
2838 #define _VARCMP(a,b) \
2839 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2841 switch (vt) {
2842 case VT_CY:
2843 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2844 case VT_DECIMAL:
2845 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2846 case VT_I8:
2847 return _VARCMP(V_I8(&lv), V_I8(&rv));
2848 case VT_R4:
2849 return _VARCMP(V_R4(&lv), V_R4(&rv));
2850 case VT_R8:
2851 return _VARCMP(V_R8(&lv), V_R8(&rv));
2852 default:
2853 /* We should never get here */
2854 return E_FAIL;
2856 #undef _VARCMP
2859 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
2861 HRESULT hres;
2862 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
2864 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
2865 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
2866 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
2867 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
2868 NULL, NULL);
2869 } else {
2870 hres = DISP_E_TYPEMISMATCH;
2872 return hres;
2875 /**********************************************************************
2876 * VarAnd [OLEAUT32.142]
2878 * Computes the logical AND of two variants.
2880 * PARAMS
2881 * left [I] First variant
2882 * right [I] Second variant
2883 * result [O] Result variant
2885 * RETURNS
2886 * Success: S_OK.
2887 * Failure: An HRESULT error code indicating the error.
2889 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2891 HRESULT hres = S_OK;
2892 VARTYPE resvt = VT_EMPTY;
2893 VARTYPE leftvt,rightvt;
2894 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2895 VARIANT varLeft, varRight;
2896 VARIANT tempLeft, tempRight;
2898 VariantInit(&varLeft);
2899 VariantInit(&varRight);
2900 VariantInit(&tempLeft);
2901 VariantInit(&tempRight);
2903 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2904 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2906 /* Handle VT_DISPATCH by storing and taking address of returned value */
2907 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2909 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2910 if (FAILED(hres)) goto VarAnd_Exit;
2911 left = &tempLeft;
2913 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2915 hres = VARIANT_FetchDispatchValue(right, &tempRight);
2916 if (FAILED(hres)) goto VarAnd_Exit;
2917 right = &tempRight;
2920 leftvt = V_VT(left)&VT_TYPEMASK;
2921 rightvt = V_VT(right)&VT_TYPEMASK;
2922 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
2923 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2925 if (leftExtraFlags != rightExtraFlags)
2927 hres = DISP_E_BADVARTYPE;
2928 goto VarAnd_Exit;
2930 ExtraFlags = leftExtraFlags;
2932 /* Native VarAnd always returns an error when using extra
2933 * flags or if the variant combination is I8 and INT.
2935 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
2936 (leftvt == VT_INT && rightvt == VT_I8) ||
2937 ExtraFlags != 0)
2939 hres = DISP_E_BADVARTYPE;
2940 goto VarAnd_Exit;
2943 /* Determine return type */
2944 else if (leftvt == VT_I8 || rightvt == VT_I8)
2945 resvt = VT_I8;
2946 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
2947 leftvt == VT_UINT || rightvt == VT_UINT ||
2948 leftvt == VT_INT || rightvt == VT_INT ||
2949 leftvt == VT_UINT || rightvt == VT_UINT ||
2950 leftvt == VT_R4 || rightvt == VT_R4 ||
2951 leftvt == VT_R8 || rightvt == VT_R8 ||
2952 leftvt == VT_CY || rightvt == VT_CY ||
2953 leftvt == VT_DATE || rightvt == VT_DATE ||
2954 leftvt == VT_I1 || rightvt == VT_I1 ||
2955 leftvt == VT_UI2 || rightvt == VT_UI2 ||
2956 leftvt == VT_UI4 || rightvt == VT_UI4 ||
2957 leftvt == VT_UI8 || rightvt == VT_UI8 ||
2958 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
2959 resvt = VT_I4;
2960 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
2961 leftvt == VT_I2 || rightvt == VT_I2 ||
2962 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
2963 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
2964 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
2965 (leftvt == VT_UI1 && rightvt == VT_UI1))
2966 resvt = VT_UI1;
2967 else
2968 resvt = VT_I2;
2969 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
2970 (leftvt == VT_BSTR && rightvt == VT_BSTR))
2971 resvt = VT_BOOL;
2972 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
2973 leftvt == VT_BSTR || rightvt == VT_BSTR)
2974 resvt = VT_NULL;
2975 else
2977 hres = DISP_E_BADVARTYPE;
2978 goto VarAnd_Exit;
2981 if (leftvt == VT_NULL || rightvt == VT_NULL)
2984 * Special cases for when left variant is VT_NULL
2985 * (NULL & 0 = NULL, NULL & value = value)
2987 if (leftvt == VT_NULL)
2989 VARIANT_BOOL b;
2990 switch(rightvt)
2992 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
2993 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
2994 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
2995 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
2996 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
2997 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
2998 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
2999 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3000 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3001 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3002 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3003 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3004 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3005 case VT_CY:
3006 if(V_CY(right).int64)
3007 resvt = VT_NULL;
3008 break;
3009 case VT_DECIMAL:
3010 if (DEC_HI32(&V_DECIMAL(right)) ||
3011 DEC_LO64(&V_DECIMAL(right)))
3012 resvt = VT_NULL;
3013 break;
3014 case VT_BSTR:
3015 hres = VarBoolFromStr(V_BSTR(right),
3016 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3017 if (FAILED(hres))
3018 return hres;
3019 else if (b)
3020 V_VT(result) = VT_NULL;
3021 else
3023 V_VT(result) = VT_BOOL;
3024 V_BOOL(result) = b;
3026 goto VarAnd_Exit;
3029 V_VT(result) = resvt;
3030 goto VarAnd_Exit;
3033 hres = VariantCopy(&varLeft, left);
3034 if (FAILED(hres)) goto VarAnd_Exit;
3036 hres = VariantCopy(&varRight, right);
3037 if (FAILED(hres)) goto VarAnd_Exit;
3039 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3040 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3041 else
3043 double d;
3045 if (V_VT(&varLeft) == VT_BSTR &&
3046 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3047 LOCALE_USER_DEFAULT, 0, &d)))
3048 hres = VariantChangeType(&varLeft,&varLeft,
3049 VARIANT_LOCALBOOL, VT_BOOL);
3050 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3051 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3052 if (FAILED(hres)) goto VarAnd_Exit;
3055 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3056 V_VT(&varRight) = VT_I4; /* Don't overflow */
3057 else
3059 double d;
3061 if (V_VT(&varRight) == VT_BSTR &&
3062 FAILED(VarR8FromStr(V_BSTR(&varRight),
3063 LOCALE_USER_DEFAULT, 0, &d)))
3064 hres = VariantChangeType(&varRight, &varRight,
3065 VARIANT_LOCALBOOL, VT_BOOL);
3066 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3067 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3068 if (FAILED(hres)) goto VarAnd_Exit;
3071 V_VT(result) = resvt;
3072 switch(resvt)
3074 case VT_I8:
3075 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3076 break;
3077 case VT_I4:
3078 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3079 break;
3080 case VT_I2:
3081 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3082 break;
3083 case VT_UI1:
3084 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3085 break;
3086 case VT_BOOL:
3087 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3088 break;
3089 default:
3090 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3091 leftvt,rightvt);
3094 VarAnd_Exit:
3095 VariantClear(&varLeft);
3096 VariantClear(&varRight);
3097 VariantClear(&tempLeft);
3098 VariantClear(&tempRight);
3100 return hres;
3103 /**********************************************************************
3104 * VarAdd [OLEAUT32.141]
3106 * Add two variants.
3108 * PARAMS
3109 * left [I] First variant
3110 * right [I] Second variant
3111 * result [O] Result variant
3113 * RETURNS
3114 * Success: S_OK.
3115 * Failure: An HRESULT error code indicating the error.
3117 * NOTES
3118 * Native VarAdd up to and including WinXP dosn't like as input variants
3119 * I1, UI2, UI4, UI8, INT and UINT.
3121 * Native VarAdd dosn't check for NULL in/out pointers and crashes. We do the
3122 * same here.
3124 * FIXME
3125 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3126 * case.
3128 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3130 HRESULT hres;
3131 VARTYPE lvt, rvt, resvt, tvt;
3132 VARIANT lv, rv, tv;
3133 VARIANT tempLeft, tempRight;
3134 double r8res;
3136 /* Variant priority for coercion. Sorted from lowest to highest.
3137 VT_ERROR shows an invalid input variant type. */
3138 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3139 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3140 vt_ERROR };
3141 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3142 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3143 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3144 VT_NULL, VT_ERROR };
3146 /* Mapping for coercion from input variant to priority of result variant. */
3147 static const VARTYPE coerce[] = {
3148 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3149 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3150 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3151 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3152 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3153 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3154 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3155 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3158 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3159 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3160 result);
3162 VariantInit(&lv);
3163 VariantInit(&rv);
3164 VariantInit(&tv);
3165 VariantInit(&tempLeft);
3166 VariantInit(&tempRight);
3168 /* Handle VT_DISPATCH by storing and taking address of returned value */
3169 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3171 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3173 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3174 if (FAILED(hres)) goto end;
3175 left = &tempLeft;
3177 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3179 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3180 if (FAILED(hres)) goto end;
3181 right = &tempRight;
3185 lvt = V_VT(left)&VT_TYPEMASK;
3186 rvt = V_VT(right)&VT_TYPEMASK;
3188 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3189 Same for any input variant type > VT_I8 */
3190 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3191 lvt > VT_I8 || rvt > VT_I8) {
3192 hres = DISP_E_BADVARTYPE;
3193 goto end;
3196 /* Determine the variant type to coerce to. */
3197 if (coerce[lvt] > coerce[rvt]) {
3198 resvt = prio2vt[coerce[lvt]];
3199 tvt = prio2vt[coerce[rvt]];
3200 } else {
3201 resvt = prio2vt[coerce[rvt]];
3202 tvt = prio2vt[coerce[lvt]];
3205 /* Special cases where the result variant type is defined by both
3206 input variants and not only that with the highest priority */
3207 if (resvt == VT_BSTR) {
3208 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3209 resvt = VT_BSTR;
3210 else
3211 resvt = VT_R8;
3213 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3214 resvt = VT_R8;
3216 /* For overflow detection use the biggest compatible type for the
3217 addition */
3218 switch (resvt) {
3219 case VT_ERROR:
3220 hres = DISP_E_BADVARTYPE;
3221 goto end;
3222 case VT_NULL:
3223 hres = S_OK;
3224 V_VT(result) = VT_NULL;
3225 goto end;
3226 case VT_DISPATCH:
3227 FIXME("cannot handle variant type VT_DISPATCH\n");
3228 hres = DISP_E_TYPEMISMATCH;
3229 goto end;
3230 case VT_EMPTY:
3231 resvt = VT_I2;
3232 /* Fall through */
3233 case VT_UI1:
3234 case VT_I2:
3235 case VT_I4:
3236 case VT_I8:
3237 tvt = VT_I8;
3238 break;
3239 case VT_DATE:
3240 case VT_R4:
3241 tvt = VT_R8;
3242 break;
3243 default:
3244 tvt = resvt;
3247 /* Now coerce the variants */
3248 hres = VariantChangeType(&lv, left, 0, tvt);
3249 if (FAILED(hres))
3250 goto end;
3251 hres = VariantChangeType(&rv, right, 0, tvt);
3252 if (FAILED(hres))
3253 goto end;
3255 /* Do the math */
3256 hres = S_OK;
3257 V_VT(result) = resvt;
3258 switch (tvt) {
3259 case VT_DECIMAL:
3260 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3261 &V_DECIMAL(result));
3262 goto end;
3263 case VT_CY:
3264 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3265 goto end;
3266 case VT_BSTR:
3267 /* We do not add those, we concatenate them. */
3268 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3269 goto end;
3270 case VT_I8:
3271 /* Overflow detection */
3272 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3273 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3274 V_VT(result) = VT_R8;
3275 V_R8(result) = r8res;
3276 goto end;
3277 } else {
3278 V_VT(&tv) = tvt;
3279 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3281 break;
3282 case VT_R8:
3283 V_VT(&tv) = tvt;
3284 /* FIXME: overflow detection */
3285 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3286 break;
3287 default:
3288 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3289 break;
3291 if (resvt != tvt) {
3292 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3293 /* Overflow! Change to the vartype with the next higher priority.
3294 With one exception: I4 ==> R8 even if it would fit in I8 */
3295 if (resvt == VT_I4)
3296 resvt = VT_R8;
3297 else
3298 resvt = prio2vt[coerce[resvt] + 1];
3299 hres = VariantChangeType(result, &tv, 0, resvt);
3301 } else
3302 hres = VariantCopy(result, &tv);
3304 end:
3305 if (hres != S_OK) {
3306 V_VT(result) = VT_EMPTY;
3307 V_I4(result) = 0; /* No V_EMPTY */
3309 VariantClear(&lv);
3310 VariantClear(&rv);
3311 VariantClear(&tv);
3312 VariantClear(&tempLeft);
3313 VariantClear(&tempRight);
3314 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3315 return hres;
3318 /**********************************************************************
3319 * VarMul [OLEAUT32.156]
3321 * Multiply two variants.
3323 * PARAMS
3324 * left [I] First variant
3325 * right [I] Second variant
3326 * result [O] Result variant
3328 * RETURNS
3329 * Success: S_OK.
3330 * Failure: An HRESULT error code indicating the error.
3332 * NOTES
3333 * Native VarMul up to and including WinXP dosn't like as input variants
3334 * I1, UI2, UI4, UI8, INT and UINT. But it can multiply apples with oranges.
3336 * Native VarMul dosn't check for NULL in/out pointers and crashes. We do the
3337 * same here.
3339 * FIXME
3340 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3341 * case.
3343 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3345 HRESULT hres;
3346 VARTYPE lvt, rvt, resvt, tvt;
3347 VARIANT lv, rv, tv;
3348 VARIANT tempLeft, tempRight;
3349 double r8res;
3351 /* Variant priority for coercion. Sorted from lowest to highest.
3352 VT_ERROR shows an invalid input variant type. */
3353 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3354 vt_DECIMAL, vt_NULL, vt_ERROR };
3355 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3356 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3357 VT_DECIMAL, VT_NULL, VT_ERROR };
3359 /* Mapping for coercion from input variant to priority of result variant. */
3360 static const VARTYPE coerce[] = {
3361 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3362 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3363 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3364 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3365 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3366 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3367 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3368 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3371 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3372 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3373 result);
3375 VariantInit(&lv);
3376 VariantInit(&rv);
3377 VariantInit(&tv);
3378 VariantInit(&tempLeft);
3379 VariantInit(&tempRight);
3381 /* Handle VT_DISPATCH by storing and taking address of returned value */
3382 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3384 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3385 if (FAILED(hres)) goto end;
3386 left = &tempLeft;
3388 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3390 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3391 if (FAILED(hres)) goto end;
3392 right = &tempRight;
3395 lvt = V_VT(left)&VT_TYPEMASK;
3396 rvt = V_VT(right)&VT_TYPEMASK;
3398 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3399 Same for any input variant type > VT_I8 */
3400 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3401 lvt > VT_I8 || rvt > VT_I8) {
3402 hres = DISP_E_BADVARTYPE;
3403 goto end;
3406 /* Determine the variant type to coerce to. */
3407 if (coerce[lvt] > coerce[rvt]) {
3408 resvt = prio2vt[coerce[lvt]];
3409 tvt = prio2vt[coerce[rvt]];
3410 } else {
3411 resvt = prio2vt[coerce[rvt]];
3412 tvt = prio2vt[coerce[lvt]];
3415 /* Special cases where the result variant type is defined by both
3416 input variants and not only that with the highest priority */
3417 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3418 resvt = VT_R8;
3419 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3420 resvt = VT_I2;
3422 /* For overflow detection use the biggest compatible type for the
3423 multiplication */
3424 switch (resvt) {
3425 case VT_ERROR:
3426 hres = DISP_E_BADVARTYPE;
3427 goto end;
3428 case VT_NULL:
3429 hres = S_OK;
3430 V_VT(result) = VT_NULL;
3431 goto end;
3432 case VT_UI1:
3433 case VT_I2:
3434 case VT_I4:
3435 case VT_I8:
3436 tvt = VT_I8;
3437 break;
3438 case VT_R4:
3439 tvt = VT_R8;
3440 break;
3441 default:
3442 tvt = resvt;
3445 /* Now coerce the variants */
3446 hres = VariantChangeType(&lv, left, 0, tvt);
3447 if (FAILED(hres))
3448 goto end;
3449 hres = VariantChangeType(&rv, right, 0, tvt);
3450 if (FAILED(hres))
3451 goto end;
3453 /* Do the math */
3454 hres = S_OK;
3455 V_VT(&tv) = tvt;
3456 V_VT(result) = resvt;
3457 switch (tvt) {
3458 case VT_DECIMAL:
3459 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3460 &V_DECIMAL(result));
3461 goto end;
3462 case VT_CY:
3463 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3464 goto end;
3465 case VT_I8:
3466 /* Overflow detection */
3467 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3468 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3469 V_VT(result) = VT_R8;
3470 V_R8(result) = r8res;
3471 goto end;
3472 } else
3473 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3474 break;
3475 case VT_R8:
3476 /* FIXME: overflow detection */
3477 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3478 break;
3479 default:
3480 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3481 break;
3483 if (resvt != tvt) {
3484 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3485 /* Overflow! Change to the vartype with the next higher priority.
3486 With one exception: I4 ==> R8 even if it would fit in I8 */
3487 if (resvt == VT_I4)
3488 resvt = VT_R8;
3489 else
3490 resvt = prio2vt[coerce[resvt] + 1];
3492 } else
3493 hres = VariantCopy(result, &tv);
3495 end:
3496 if (hres != S_OK) {
3497 V_VT(result) = VT_EMPTY;
3498 V_I4(result) = 0; /* No V_EMPTY */
3500 VariantClear(&lv);
3501 VariantClear(&rv);
3502 VariantClear(&tv);
3503 VariantClear(&tempLeft);
3504 VariantClear(&tempRight);
3505 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3506 return hres;
3509 /**********************************************************************
3510 * VarDiv [OLEAUT32.143]
3512 * Divides one variant with another.
3514 * PARAMS
3515 * left [I] First variant
3516 * right [I] Second variant
3517 * result [O] Result variant
3519 * RETURNS
3520 * Success: S_OK.
3521 * Failure: An HRESULT error code indicating the error.
3523 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3525 HRESULT hres = S_OK;
3526 VARTYPE resvt = VT_EMPTY;
3527 VARTYPE leftvt,rightvt;
3528 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3529 VARIANT lv,rv;
3530 VARIANT tempLeft, tempRight;
3532 VariantInit(&tempLeft);
3533 VariantInit(&tempRight);
3534 VariantInit(&lv);
3535 VariantInit(&rv);
3537 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3538 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3540 /* Handle VT_DISPATCH by storing and taking address of returned value */
3541 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3543 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3544 if (FAILED(hres)) goto end;
3545 left = &tempLeft;
3547 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3549 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3550 if (FAILED(hres)) goto end;
3551 right = &tempRight;
3554 leftvt = V_VT(left)&VT_TYPEMASK;
3555 rightvt = V_VT(right)&VT_TYPEMASK;
3556 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3557 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3559 if (leftExtraFlags != rightExtraFlags)
3561 hres = DISP_E_BADVARTYPE;
3562 goto end;
3564 ExtraFlags = leftExtraFlags;
3566 /* Native VarDiv always returns an error when using extra flags */
3567 if (ExtraFlags != 0)
3569 hres = DISP_E_BADVARTYPE;
3570 goto end;
3573 /* Determine return type */
3574 if (!(rightvt == VT_EMPTY))
3576 if (leftvt == VT_NULL || rightvt == VT_NULL)
3578 V_VT(result) = VT_NULL;
3579 hres = S_OK;
3580 goto end;
3582 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3583 resvt = VT_DECIMAL;
3584 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3585 leftvt == VT_CY || rightvt == VT_CY ||
3586 leftvt == VT_DATE || rightvt == VT_DATE ||
3587 leftvt == VT_I4 || rightvt == VT_I4 ||
3588 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3589 leftvt == VT_I2 || rightvt == VT_I2 ||
3590 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3591 leftvt == VT_R8 || rightvt == VT_R8 ||
3592 leftvt == VT_UI1 || rightvt == VT_UI1)
3594 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3595 (leftvt == VT_R4 && rightvt == VT_UI1))
3596 resvt = VT_R4;
3597 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3598 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3599 (leftvt == VT_BOOL || leftvt == VT_I2)))
3600 resvt = VT_R4;
3601 else
3602 resvt = VT_R8;
3604 else if (leftvt == VT_R4 || rightvt == VT_R4)
3605 resvt = VT_R4;
3607 else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
3609 V_VT(result) = VT_NULL;
3610 hres = S_OK;
3611 goto end;
3613 else
3615 hres = DISP_E_BADVARTYPE;
3616 goto end;
3619 /* coerce to the result type */
3620 hres = VariantChangeType(&lv, left, 0, resvt);
3621 if (hres != S_OK) goto end;
3623 hres = VariantChangeType(&rv, right, 0, resvt);
3624 if (hres != S_OK) goto end;
3626 /* do the math */
3627 V_VT(result) = resvt;
3628 switch (resvt)
3630 case VT_R4:
3631 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3633 hres = DISP_E_OVERFLOW;
3634 V_VT(result) = VT_EMPTY;
3636 else if (V_R4(&rv) == 0.0)
3638 hres = DISP_E_DIVBYZERO;
3639 V_VT(result) = VT_EMPTY;
3641 else
3642 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3643 break;
3644 case VT_R8:
3645 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3647 hres = DISP_E_OVERFLOW;
3648 V_VT(result) = VT_EMPTY;
3650 else if (V_R8(&rv) == 0.0)
3652 hres = DISP_E_DIVBYZERO;
3653 V_VT(result) = VT_EMPTY;
3655 else
3656 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3657 break;
3658 case VT_DECIMAL:
3659 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3660 break;
3663 end:
3664 VariantClear(&lv);
3665 VariantClear(&rv);
3666 VariantClear(&tempLeft);
3667 VariantClear(&tempRight);
3668 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3669 return hres;
3672 /**********************************************************************
3673 * VarSub [OLEAUT32.159]
3675 * Subtract two variants.
3677 * PARAMS
3678 * left [I] First variant
3679 * right [I] Second variant
3680 * result [O] Result variant
3682 * RETURNS
3683 * Success: S_OK.
3684 * Failure: An HRESULT error code indicating the error.
3686 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3688 HRESULT hres = S_OK;
3689 VARTYPE resvt = VT_EMPTY;
3690 VARTYPE leftvt,rightvt;
3691 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3692 VARIANT lv,rv;
3693 VARIANT tempLeft, tempRight;
3695 VariantInit(&lv);
3696 VariantInit(&rv);
3697 VariantInit(&tempLeft);
3698 VariantInit(&tempRight);
3700 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3701 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3703 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3704 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3705 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3707 if (NULL == V_DISPATCH(left)) {
3708 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3709 hres = DISP_E_BADVARTYPE;
3710 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3711 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3712 hres = DISP_E_BADVARTYPE;
3713 else switch (V_VT(right) & VT_TYPEMASK)
3715 case VT_VARIANT:
3716 case VT_UNKNOWN:
3717 case 15:
3718 case VT_I1:
3719 case VT_UI2:
3720 case VT_UI4:
3721 hres = DISP_E_BADVARTYPE;
3723 if (FAILED(hres)) goto end;
3725 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3726 if (FAILED(hres)) goto end;
3727 left = &tempLeft;
3729 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3730 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3731 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3733 if (NULL == V_DISPATCH(right))
3735 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3736 hres = DISP_E_BADVARTYPE;
3737 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3738 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3739 hres = DISP_E_BADVARTYPE;
3740 else switch (V_VT(left) & VT_TYPEMASK)
3742 case VT_VARIANT:
3743 case VT_UNKNOWN:
3744 case 15:
3745 case VT_I1:
3746 case VT_UI2:
3747 case VT_UI4:
3748 hres = DISP_E_BADVARTYPE;
3750 if (FAILED(hres)) goto end;
3752 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3753 if (FAILED(hres)) goto end;
3754 right = &tempRight;
3757 leftvt = V_VT(left)&VT_TYPEMASK;
3758 rightvt = V_VT(right)&VT_TYPEMASK;
3759 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3760 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3762 if (leftExtraFlags != rightExtraFlags)
3764 hres = DISP_E_BADVARTYPE;
3765 goto end;
3767 ExtraFlags = leftExtraFlags;
3769 /* determine return type and return code */
3770 /* All extra flags produce errors */
3771 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3772 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3773 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3774 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3775 ExtraFlags == VT_VECTOR ||
3776 ExtraFlags == VT_BYREF ||
3777 ExtraFlags == VT_RESERVED)
3779 hres = DISP_E_BADVARTYPE;
3780 goto end;
3782 else if (ExtraFlags >= VT_ARRAY)
3784 hres = DISP_E_TYPEMISMATCH;
3785 goto end;
3787 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3788 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3789 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3790 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3791 leftvt == VT_I1 || rightvt == VT_I1 ||
3792 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3793 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3794 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3795 leftvt == VT_INT || rightvt == VT_INT ||
3796 leftvt == VT_UINT || rightvt == VT_UINT ||
3797 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3798 leftvt == VT_RECORD || rightvt == VT_RECORD)
3800 if (leftvt == VT_RECORD && rightvt == VT_I8)
3801 hres = DISP_E_TYPEMISMATCH;
3802 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3803 hres = DISP_E_TYPEMISMATCH;
3804 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3805 hres = DISP_E_TYPEMISMATCH;
3806 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3807 hres = DISP_E_TYPEMISMATCH;
3808 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3809 hres = DISP_E_BADVARTYPE;
3810 else
3811 hres = DISP_E_BADVARTYPE;
3812 goto end;
3814 /* The following flags/types are invalid for left variant */
3815 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3816 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3817 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3819 hres = DISP_E_BADVARTYPE;
3820 goto end;
3822 /* The following flags/types are invalid for right variant */
3823 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3824 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3825 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3827 hres = DISP_E_BADVARTYPE;
3828 goto end;
3830 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3831 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3832 resvt = VT_NULL;
3833 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3834 leftvt == VT_ERROR || rightvt == VT_ERROR)
3836 hres = DISP_E_TYPEMISMATCH;
3837 goto end;
3839 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3840 resvt = VT_NULL;
3841 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3842 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3843 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3844 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3845 resvt = VT_R8;
3846 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3847 resvt = VT_DECIMAL;
3848 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3849 resvt = VT_DATE;
3850 else if (leftvt == VT_CY || rightvt == VT_CY)
3851 resvt = VT_CY;
3852 else if (leftvt == VT_R8 || rightvt == VT_R8)
3853 resvt = VT_R8;
3854 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3855 resvt = VT_R8;
3856 else if (leftvt == VT_R4 || rightvt == VT_R4)
3858 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3859 leftvt == VT_I8 || rightvt == VT_I8)
3860 resvt = VT_R8;
3861 else
3862 resvt = VT_R4;
3864 else if (leftvt == VT_I8 || rightvt == VT_I8)
3865 resvt = VT_I8;
3866 else if (leftvt == VT_I4 || rightvt == VT_I4)
3867 resvt = VT_I4;
3868 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3869 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3870 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3871 resvt = VT_I2;
3872 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3873 resvt = VT_UI1;
3874 else
3876 hres = DISP_E_TYPEMISMATCH;
3877 goto end;
3880 /* coerce to the result type */
3881 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3882 hres = VariantChangeType(&lv, left, 0, VT_R8);
3883 else
3884 hres = VariantChangeType(&lv, left, 0, resvt);
3885 if (hres != S_OK) goto end;
3886 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3887 hres = VariantChangeType(&rv, right, 0, VT_R8);
3888 else
3889 hres = VariantChangeType(&rv, right, 0, resvt);
3890 if (hres != S_OK) goto end;
3892 /* do the math */
3893 V_VT(result) = resvt;
3894 switch (resvt)
3896 case VT_NULL:
3897 break;
3898 case VT_DATE:
3899 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3900 break;
3901 case VT_CY:
3902 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3903 break;
3904 case VT_R4:
3905 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3906 break;
3907 case VT_I8:
3908 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3909 break;
3910 case VT_I4:
3911 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3912 break;
3913 case VT_I2:
3914 V_I2(result) = V_I2(&lv) - V_I2(&rv);
3915 break;
3916 case VT_I1:
3917 V_I1(result) = V_I1(&lv) - V_I1(&rv);
3918 break;
3919 case VT_UI1:
3920 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
3921 break;
3922 case VT_R8:
3923 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3924 break;
3925 case VT_DECIMAL:
3926 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3927 break;
3930 end:
3931 VariantClear(&lv);
3932 VariantClear(&rv);
3933 VariantClear(&tempLeft);
3934 VariantClear(&tempRight);
3935 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3936 return hres;
3940 /**********************************************************************
3941 * VarOr [OLEAUT32.157]
3943 * Perform a logical or (OR) operation on two variants.
3945 * PARAMS
3946 * pVarLeft [I] First variant
3947 * pVarRight [I] Variant to OR with pVarLeft
3948 * pVarOut [O] Destination for OR result
3950 * RETURNS
3951 * Success: S_OK. pVarOut contains the result of the operation with its type
3952 * taken from the table listed under VarXor().
3953 * Failure: An HRESULT error code indicating the error.
3955 * NOTES
3956 * See the Notes section of VarXor() for further information.
3958 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3960 VARTYPE vt = VT_I4;
3961 VARIANT varLeft, varRight, varStr;
3962 HRESULT hRet;
3963 VARIANT tempLeft, tempRight;
3965 VariantInit(&tempLeft);
3966 VariantInit(&tempRight);
3967 VariantInit(&varLeft);
3968 VariantInit(&varRight);
3969 VariantInit(&varStr);
3971 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3972 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3973 debugstr_VF(pVarRight), pVarOut);
3975 /* Handle VT_DISPATCH by storing and taking address of returned value */
3976 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
3978 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
3979 if (FAILED(hRet)) goto VarOr_Exit;
3980 pVarLeft = &tempLeft;
3982 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
3984 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
3985 if (FAILED(hRet)) goto VarOr_Exit;
3986 pVarRight = &tempRight;
3989 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3990 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3991 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3992 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3994 hRet = DISP_E_BADVARTYPE;
3995 goto VarOr_Exit;
3998 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
4000 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4002 /* NULL OR Zero is NULL, NULL OR value is value */
4003 if (V_VT(pVarLeft) == VT_NULL)
4004 pVarLeft = pVarRight; /* point to the non-NULL var */
4006 V_VT(pVarOut) = VT_NULL;
4007 V_I4(pVarOut) = 0;
4009 switch (V_VT(pVarLeft))
4011 case VT_DATE: case VT_R8:
4012 if (V_R8(pVarLeft))
4013 goto VarOr_AsEmpty;
4014 hRet = S_OK;
4015 goto VarOr_Exit;
4016 case VT_BOOL:
4017 if (V_BOOL(pVarLeft))
4018 *pVarOut = *pVarLeft;
4019 hRet = S_OK;
4020 goto VarOr_Exit;
4021 case VT_I2: case VT_UI2:
4022 if (V_I2(pVarLeft))
4023 goto VarOr_AsEmpty;
4024 hRet = S_OK;
4025 goto VarOr_Exit;
4026 case VT_I1:
4027 if (V_I1(pVarLeft))
4028 goto VarOr_AsEmpty;
4029 hRet = S_OK;
4030 goto VarOr_Exit;
4031 case VT_UI1:
4032 if (V_UI1(pVarLeft))
4033 *pVarOut = *pVarLeft;
4034 hRet = S_OK;
4035 goto VarOr_Exit;
4036 case VT_R4:
4037 if (V_R4(pVarLeft))
4038 goto VarOr_AsEmpty;
4039 hRet = S_OK;
4040 goto VarOr_Exit;
4041 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4042 if (V_I4(pVarLeft))
4043 goto VarOr_AsEmpty;
4044 hRet = S_OK;
4045 goto VarOr_Exit;
4046 case VT_CY:
4047 if (V_CY(pVarLeft).int64)
4048 goto VarOr_AsEmpty;
4049 hRet = S_OK;
4050 goto VarOr_Exit;
4051 case VT_I8: case VT_UI8:
4052 if (V_I8(pVarLeft))
4053 goto VarOr_AsEmpty;
4054 hRet = S_OK;
4055 goto VarOr_Exit;
4056 case VT_DECIMAL:
4057 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4058 goto VarOr_AsEmpty;
4059 hRet = S_OK;
4060 goto VarOr_Exit;
4061 case VT_BSTR:
4063 VARIANT_BOOL b;
4065 if (!V_BSTR(pVarLeft))
4067 hRet = DISP_E_BADVARTYPE;
4068 goto VarOr_Exit;
4071 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4072 if (SUCCEEDED(hRet) && b)
4074 V_VT(pVarOut) = VT_BOOL;
4075 V_BOOL(pVarOut) = b;
4077 goto VarOr_Exit;
4079 case VT_NULL: case VT_EMPTY:
4080 V_VT(pVarOut) = VT_NULL;
4081 hRet = S_OK;
4082 goto VarOr_Exit;
4083 default:
4084 hRet = DISP_E_BADVARTYPE;
4085 goto VarOr_Exit;
4089 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4091 if (V_VT(pVarLeft) == VT_EMPTY)
4092 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4094 VarOr_AsEmpty:
4095 /* Since one argument is empty (0), OR'ing it with the other simply
4096 * gives the others value (as 0|x => x). So just convert the other
4097 * argument to the required result type.
4099 switch (V_VT(pVarLeft))
4101 case VT_BSTR:
4102 if (!V_BSTR(pVarLeft))
4104 hRet = DISP_E_BADVARTYPE;
4105 goto VarOr_Exit;
4108 hRet = VariantCopy(&varStr, pVarLeft);
4109 if (FAILED(hRet))
4110 goto VarOr_Exit;
4111 pVarLeft = &varStr;
4112 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4113 if (FAILED(hRet))
4114 goto VarOr_Exit;
4115 /* Fall Through ... */
4116 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4117 V_VT(pVarOut) = VT_I2;
4118 break;
4119 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4120 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4121 case VT_INT: case VT_UINT: case VT_UI8:
4122 V_VT(pVarOut) = VT_I4;
4123 break;
4124 case VT_I8:
4125 V_VT(pVarOut) = VT_I8;
4126 break;
4127 default:
4128 hRet = DISP_E_BADVARTYPE;
4129 goto VarOr_Exit;
4131 hRet = VariantCopy(&varLeft, pVarLeft);
4132 if (FAILED(hRet))
4133 goto VarOr_Exit;
4134 pVarLeft = &varLeft;
4135 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4136 goto VarOr_Exit;
4139 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4141 V_VT(pVarOut) = VT_BOOL;
4142 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4143 hRet = S_OK;
4144 goto VarOr_Exit;
4147 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4149 V_VT(pVarOut) = VT_UI1;
4150 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4151 hRet = S_OK;
4152 goto VarOr_Exit;
4155 if (V_VT(pVarLeft) == VT_BSTR)
4157 hRet = VariantCopy(&varStr, pVarLeft);
4158 if (FAILED(hRet))
4159 goto VarOr_Exit;
4160 pVarLeft = &varStr;
4161 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4162 if (FAILED(hRet))
4163 goto VarOr_Exit;
4166 if (V_VT(pVarLeft) == VT_BOOL &&
4167 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4169 vt = VT_BOOL;
4171 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4172 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4173 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4174 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4176 vt = VT_I2;
4178 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4180 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4182 hRet = DISP_E_TYPEMISMATCH;
4183 goto VarOr_Exit;
4185 vt = VT_I8;
4188 hRet = VariantCopy(&varLeft, pVarLeft);
4189 if (FAILED(hRet))
4190 goto VarOr_Exit;
4192 hRet = VariantCopy(&varRight, pVarRight);
4193 if (FAILED(hRet))
4194 goto VarOr_Exit;
4196 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4197 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4198 else
4200 double d;
4202 if (V_VT(&varLeft) == VT_BSTR &&
4203 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4204 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4205 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4206 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4207 if (FAILED(hRet))
4208 goto VarOr_Exit;
4211 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4212 V_VT(&varRight) = VT_I4; /* Don't overflow */
4213 else
4215 double d;
4217 if (V_VT(&varRight) == VT_BSTR &&
4218 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4219 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4220 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4221 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4222 if (FAILED(hRet))
4223 goto VarOr_Exit;
4226 V_VT(pVarOut) = vt;
4227 if (vt == VT_I8)
4229 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4231 else if (vt == VT_I4)
4233 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4235 else
4237 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4240 VarOr_Exit:
4241 VariantClear(&varStr);
4242 VariantClear(&varLeft);
4243 VariantClear(&varRight);
4244 VariantClear(&tempLeft);
4245 VariantClear(&tempRight);
4246 return hRet;
4249 /**********************************************************************
4250 * VarAbs [OLEAUT32.168]
4252 * Convert a variant to its absolute value.
4254 * PARAMS
4255 * pVarIn [I] Source variant
4256 * pVarOut [O] Destination for converted value
4258 * RETURNS
4259 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4260 * Failure: An HRESULT error code indicating the error.
4262 * NOTES
4263 * - This function does not process by-reference variants.
4264 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4265 * according to the following table:
4266 *| Input Type Output Type
4267 *| ---------- -----------
4268 *| VT_BOOL VT_I2
4269 *| VT_BSTR VT_R8
4270 *| (All others) Unchanged
4272 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4274 VARIANT varIn;
4275 HRESULT hRet = S_OK;
4276 VARIANT temp;
4278 VariantInit(&temp);
4280 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4281 debugstr_VF(pVarIn), pVarOut);
4283 /* Handle VT_DISPATCH by storing and taking address of returned value */
4284 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4286 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4287 if (FAILED(hRet)) goto VarAbs_Exit;
4288 pVarIn = &temp;
4291 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4292 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4293 V_VT(pVarIn) == VT_ERROR)
4295 hRet = DISP_E_TYPEMISMATCH;
4296 goto VarAbs_Exit;
4298 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4300 #define ABS_CASE(typ,min) \
4301 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4302 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4303 break
4305 switch (V_VT(pVarIn))
4307 ABS_CASE(I1,I1_MIN);
4308 case VT_BOOL:
4309 V_VT(pVarOut) = VT_I2;
4310 /* BOOL->I2, Fall through ... */
4311 ABS_CASE(I2,I2_MIN);
4312 case VT_INT:
4313 ABS_CASE(I4,I4_MIN);
4314 ABS_CASE(I8,I8_MIN);
4315 ABS_CASE(R4,R4_MIN);
4316 case VT_BSTR:
4317 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4318 if (FAILED(hRet))
4319 break;
4320 V_VT(pVarOut) = VT_R8;
4321 pVarIn = &varIn;
4322 /* Fall through ... */
4323 case VT_DATE:
4324 ABS_CASE(R8,R8_MIN);
4325 case VT_CY:
4326 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4327 break;
4328 case VT_DECIMAL:
4329 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4330 break;
4331 case VT_UI1:
4332 case VT_UI2:
4333 case VT_UINT:
4334 case VT_UI4:
4335 case VT_UI8:
4336 /* No-Op */
4337 break;
4338 case VT_EMPTY:
4339 V_VT(pVarOut) = VT_I2;
4340 case VT_NULL:
4341 V_I2(pVarOut) = 0;
4342 break;
4343 default:
4344 hRet = DISP_E_BADVARTYPE;
4347 VarAbs_Exit:
4348 VariantClear(&temp);
4349 return hRet;
4352 /**********************************************************************
4353 * VarFix [OLEAUT32.169]
4355 * Truncate a variants value to a whole number.
4357 * PARAMS
4358 * pVarIn [I] Source variant
4359 * pVarOut [O] Destination for converted value
4361 * RETURNS
4362 * Success: S_OK. pVarOut contains the converted value.
4363 * Failure: An HRESULT error code indicating the error.
4365 * NOTES
4366 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4367 * according to the following table:
4368 *| Input Type Output Type
4369 *| ---------- -----------
4370 *| VT_BOOL VT_I2
4371 *| VT_EMPTY VT_I2
4372 *| VT_BSTR VT_R8
4373 *| All Others Unchanged
4374 * - The difference between this function and VarInt() is that VarInt() rounds
4375 * negative numbers away from 0, while this function rounds them towards zero.
4377 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4379 HRESULT hRet = S_OK;
4380 VARIANT temp;
4382 VariantInit(&temp);
4384 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4385 debugstr_VF(pVarIn), pVarOut);
4387 /* Handle VT_DISPATCH by storing and taking address of returned value */
4388 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4390 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4391 if (FAILED(hRet)) goto VarFix_Exit;
4392 pVarIn = &temp;
4394 V_VT(pVarOut) = V_VT(pVarIn);
4396 switch (V_VT(pVarIn))
4398 case VT_UI1:
4399 V_UI1(pVarOut) = V_UI1(pVarIn);
4400 break;
4401 case VT_BOOL:
4402 V_VT(pVarOut) = VT_I2;
4403 /* Fall through */
4404 case VT_I2:
4405 V_I2(pVarOut) = V_I2(pVarIn);
4406 break;
4407 case VT_I4:
4408 V_I4(pVarOut) = V_I4(pVarIn);
4409 break;
4410 case VT_I8:
4411 V_I8(pVarOut) = V_I8(pVarIn);
4412 break;
4413 case VT_R4:
4414 if (V_R4(pVarIn) < 0.0f)
4415 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4416 else
4417 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4418 break;
4419 case VT_BSTR:
4420 V_VT(pVarOut) = VT_R8;
4421 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4422 pVarIn = pVarOut;
4423 /* Fall through */
4424 case VT_DATE:
4425 case VT_R8:
4426 if (V_R8(pVarIn) < 0.0)
4427 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4428 else
4429 V_R8(pVarOut) = floor(V_R8(pVarIn));
4430 break;
4431 case VT_CY:
4432 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4433 break;
4434 case VT_DECIMAL:
4435 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4436 break;
4437 case VT_EMPTY:
4438 V_VT(pVarOut) = VT_I2;
4439 V_I2(pVarOut) = 0;
4440 break;
4441 case VT_NULL:
4442 /* No-Op */
4443 break;
4444 default:
4445 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4446 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4447 hRet = DISP_E_BADVARTYPE;
4448 else
4449 hRet = DISP_E_TYPEMISMATCH;
4451 VarFix_Exit:
4452 if (FAILED(hRet))
4453 V_VT(pVarOut) = VT_EMPTY;
4454 VariantClear(&temp);
4456 return hRet;
4459 /**********************************************************************
4460 * VarInt [OLEAUT32.172]
4462 * Truncate a variants value to a whole number.
4464 * PARAMS
4465 * pVarIn [I] Source variant
4466 * pVarOut [O] Destination for converted value
4468 * RETURNS
4469 * Success: S_OK. pVarOut contains the converted value.
4470 * Failure: An HRESULT error code indicating the error.
4472 * NOTES
4473 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4474 * according to the following table:
4475 *| Input Type Output Type
4476 *| ---------- -----------
4477 *| VT_BOOL VT_I2
4478 *| VT_EMPTY VT_I2
4479 *| VT_BSTR VT_R8
4480 *| All Others Unchanged
4481 * - The difference between this function and VarFix() is that VarFix() rounds
4482 * negative numbers towards 0, while this function rounds them away from zero.
4484 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4486 HRESULT hRet = S_OK;
4487 VARIANT temp;
4489 VariantInit(&temp);
4491 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4492 debugstr_VF(pVarIn), pVarOut);
4494 /* Handle VT_DISPATCH by storing and taking address of returned value */
4495 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4497 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4498 if (FAILED(hRet)) goto VarInt_Exit;
4499 pVarIn = &temp;
4501 V_VT(pVarOut) = V_VT(pVarIn);
4503 switch (V_VT(pVarIn))
4505 case VT_R4:
4506 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4507 break;
4508 case VT_BSTR:
4509 V_VT(pVarOut) = VT_R8;
4510 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4511 pVarIn = pVarOut;
4512 /* Fall through */
4513 case VT_DATE:
4514 case VT_R8:
4515 V_R8(pVarOut) = floor(V_R8(pVarIn));
4516 break;
4517 case VT_CY:
4518 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4519 break;
4520 case VT_DECIMAL:
4521 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4522 break;
4523 default:
4524 hRet = VarFix(pVarIn, pVarOut);
4526 VarInt_Exit:
4527 VariantClear(&temp);
4529 return hRet;
4532 /**********************************************************************
4533 * VarXor [OLEAUT32.167]
4535 * Perform a logical exclusive-or (XOR) operation on two variants.
4537 * PARAMS
4538 * pVarLeft [I] First variant
4539 * pVarRight [I] Variant to XOR with pVarLeft
4540 * pVarOut [O] Destination for XOR result
4542 * RETURNS
4543 * Success: S_OK. pVarOut contains the result of the operation with its type
4544 * taken from the table below).
4545 * Failure: An HRESULT error code indicating the error.
4547 * NOTES
4548 * - Neither pVarLeft or pVarRight are modified by this function.
4549 * - This function does not process by-reference variants.
4550 * - Input types of VT_BSTR may be numeric strings or boolean text.
4551 * - The type of result stored in pVarOut depends on the types of pVarLeft
4552 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4553 * or VT_NULL if the function succeeds.
4554 * - Type promotion is inconsistent and as a result certain combinations of
4555 * values will return DISP_E_OVERFLOW even when they could be represented.
4556 * This matches the behaviour of native oleaut32.
4558 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4560 VARTYPE vt;
4561 VARIANT varLeft, varRight;
4562 VARIANT tempLeft, tempRight;
4563 double d;
4564 HRESULT hRet;
4566 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4567 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4568 debugstr_VF(pVarRight), pVarOut);
4570 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4571 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4572 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4573 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4574 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4575 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4576 return DISP_E_BADVARTYPE;
4578 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4580 /* NULL XOR anything valid is NULL */
4581 V_VT(pVarOut) = VT_NULL;
4582 return S_OK;
4585 VariantInit(&tempLeft);
4586 VariantInit(&tempRight);
4588 /* Handle VT_DISPATCH by storing and taking address of returned value */
4589 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4591 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4592 if (FAILED(hRet)) goto VarXor_Exit;
4593 pVarLeft = &tempLeft;
4595 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4597 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4598 if (FAILED(hRet)) goto VarXor_Exit;
4599 pVarRight = &tempRight;
4602 /* Copy our inputs so we don't disturb anything */
4603 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4605 hRet = VariantCopy(&varLeft, pVarLeft);
4606 if (FAILED(hRet))
4607 goto VarXor_Exit;
4609 hRet = VariantCopy(&varRight, pVarRight);
4610 if (FAILED(hRet))
4611 goto VarXor_Exit;
4613 /* Try any strings first as numbers, then as VT_BOOL */
4614 if (V_VT(&varLeft) == VT_BSTR)
4616 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4617 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4618 FAILED(hRet) ? VT_BOOL : VT_I4);
4619 if (FAILED(hRet))
4620 goto VarXor_Exit;
4623 if (V_VT(&varRight) == VT_BSTR)
4625 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4626 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4627 FAILED(hRet) ? VT_BOOL : VT_I4);
4628 if (FAILED(hRet))
4629 goto VarXor_Exit;
4632 /* Determine the result type */
4633 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4635 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4637 hRet = DISP_E_TYPEMISMATCH;
4638 goto VarXor_Exit;
4640 vt = VT_I8;
4642 else
4644 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4646 case (VT_BOOL << 16) | VT_BOOL:
4647 vt = VT_BOOL;
4648 break;
4649 case (VT_UI1 << 16) | VT_UI1:
4650 vt = VT_UI1;
4651 break;
4652 case (VT_EMPTY << 16) | VT_EMPTY:
4653 case (VT_EMPTY << 16) | VT_UI1:
4654 case (VT_EMPTY << 16) | VT_I2:
4655 case (VT_EMPTY << 16) | VT_BOOL:
4656 case (VT_UI1 << 16) | VT_EMPTY:
4657 case (VT_UI1 << 16) | VT_I2:
4658 case (VT_UI1 << 16) | VT_BOOL:
4659 case (VT_I2 << 16) | VT_EMPTY:
4660 case (VT_I2 << 16) | VT_UI1:
4661 case (VT_I2 << 16) | VT_I2:
4662 case (VT_I2 << 16) | VT_BOOL:
4663 case (VT_BOOL << 16) | VT_EMPTY:
4664 case (VT_BOOL << 16) | VT_UI1:
4665 case (VT_BOOL << 16) | VT_I2:
4666 vt = VT_I2;
4667 break;
4668 default:
4669 vt = VT_I4;
4670 break;
4674 /* VT_UI4 does not overflow */
4675 if (vt != VT_I8)
4677 if (V_VT(&varLeft) == VT_UI4)
4678 V_VT(&varLeft) = VT_I4;
4679 if (V_VT(&varRight) == VT_UI4)
4680 V_VT(&varRight) = VT_I4;
4683 /* Convert our input copies to the result type */
4684 if (V_VT(&varLeft) != vt)
4685 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4686 if (FAILED(hRet))
4687 goto VarXor_Exit;
4689 if (V_VT(&varRight) != vt)
4690 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4691 if (FAILED(hRet))
4692 goto VarXor_Exit;
4694 V_VT(pVarOut) = vt;
4696 /* Calculate the result */
4697 switch (vt)
4699 case VT_I8:
4700 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4701 break;
4702 case VT_I4:
4703 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4704 break;
4705 case VT_BOOL:
4706 case VT_I2:
4707 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4708 break;
4709 case VT_UI1:
4710 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4711 break;
4714 VarXor_Exit:
4715 VariantClear(&varLeft);
4716 VariantClear(&varRight);
4717 VariantClear(&tempLeft);
4718 VariantClear(&tempRight);
4719 return hRet;
4722 /**********************************************************************
4723 * VarEqv [OLEAUT32.172]
4725 * Determine if two variants contain the same value.
4727 * PARAMS
4728 * pVarLeft [I] First variant to compare
4729 * pVarRight [I] Variant to compare to pVarLeft
4730 * pVarOut [O] Destination for comparison result
4732 * RETURNS
4733 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4734 * if equivalent or non-zero otherwise.
4735 * Failure: An HRESULT error code indicating the error.
4737 * NOTES
4738 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4739 * the result.
4741 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4743 HRESULT hRet;
4745 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4746 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4747 debugstr_VF(pVarRight), pVarOut);
4749 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4750 if (SUCCEEDED(hRet))
4752 if (V_VT(pVarOut) == VT_I8)
4753 V_I8(pVarOut) = ~V_I8(pVarOut);
4754 else
4755 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4757 return hRet;
4760 /**********************************************************************
4761 * VarNeg [OLEAUT32.173]
4763 * Negate the value of a variant.
4765 * PARAMS
4766 * pVarIn [I] Source variant
4767 * pVarOut [O] Destination for converted value
4769 * RETURNS
4770 * Success: S_OK. pVarOut contains the converted value.
4771 * Failure: An HRESULT error code indicating the error.
4773 * NOTES
4774 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4775 * according to the following table:
4776 *| Input Type Output Type
4777 *| ---------- -----------
4778 *| VT_EMPTY VT_I2
4779 *| VT_UI1 VT_I2
4780 *| VT_BOOL VT_I2
4781 *| VT_BSTR VT_R8
4782 *| All Others Unchanged (unless promoted)
4783 * - Where the negated value of a variant does not fit in its base type, the type
4784 * is promoted according to the following table:
4785 *| Input Type Promoted To
4786 *| ---------- -----------
4787 *| VT_I2 VT_I4
4788 *| VT_I4 VT_R8
4789 *| VT_I8 VT_R8
4790 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4791 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4792 * for types which are not valid. Since this is in contravention of the
4793 * meaning of those error codes and unlikely to be relied on by applications,
4794 * this implementation returns errors consistent with the other high level
4795 * variant math functions.
4797 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4799 HRESULT hRet = S_OK;
4800 VARIANT temp;
4802 VariantInit(&temp);
4804 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4805 debugstr_VF(pVarIn), pVarOut);
4807 /* Handle VT_DISPATCH by storing and taking address of returned value */
4808 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4810 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4811 if (FAILED(hRet)) goto VarNeg_Exit;
4812 pVarIn = &temp;
4814 V_VT(pVarOut) = V_VT(pVarIn);
4816 switch (V_VT(pVarIn))
4818 case VT_UI1:
4819 V_VT(pVarOut) = VT_I2;
4820 V_I2(pVarOut) = -V_UI1(pVarIn);
4821 break;
4822 case VT_BOOL:
4823 V_VT(pVarOut) = VT_I2;
4824 /* Fall through */
4825 case VT_I2:
4826 if (V_I2(pVarIn) == I2_MIN)
4828 V_VT(pVarOut) = VT_I4;
4829 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4831 else
4832 V_I2(pVarOut) = -V_I2(pVarIn);
4833 break;
4834 case VT_I4:
4835 if (V_I4(pVarIn) == I4_MIN)
4837 V_VT(pVarOut) = VT_R8;
4838 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4840 else
4841 V_I4(pVarOut) = -V_I4(pVarIn);
4842 break;
4843 case VT_I8:
4844 if (V_I8(pVarIn) == I8_MIN)
4846 V_VT(pVarOut) = VT_R8;
4847 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4848 V_R8(pVarOut) *= -1.0;
4850 else
4851 V_I8(pVarOut) = -V_I8(pVarIn);
4852 break;
4853 case VT_R4:
4854 V_R4(pVarOut) = -V_R4(pVarIn);
4855 break;
4856 case VT_DATE:
4857 case VT_R8:
4858 V_R8(pVarOut) = -V_R8(pVarIn);
4859 break;
4860 case VT_CY:
4861 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4862 break;
4863 case VT_DECIMAL:
4864 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4865 break;
4866 case VT_BSTR:
4867 V_VT(pVarOut) = VT_R8;
4868 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4869 V_R8(pVarOut) = -V_R8(pVarOut);
4870 break;
4871 case VT_EMPTY:
4872 V_VT(pVarOut) = VT_I2;
4873 V_I2(pVarOut) = 0;
4874 break;
4875 case VT_NULL:
4876 /* No-Op */
4877 break;
4878 default:
4879 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4880 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4881 hRet = DISP_E_BADVARTYPE;
4882 else
4883 hRet = DISP_E_TYPEMISMATCH;
4885 VarNeg_Exit:
4886 if (FAILED(hRet))
4887 V_VT(pVarOut) = VT_EMPTY;
4888 VariantClear(&temp);
4890 return hRet;
4893 /**********************************************************************
4894 * VarNot [OLEAUT32.174]
4896 * Perform a not operation on a variant.
4898 * PARAMS
4899 * pVarIn [I] Source variant
4900 * pVarOut [O] Destination for converted value
4902 * RETURNS
4903 * Success: S_OK. pVarOut contains the converted value.
4904 * Failure: An HRESULT error code indicating the error.
4906 * NOTES
4907 * - Strictly speaking, this function performs a bitwise ones complement
4908 * on the variants value (after possibly converting to VT_I4, see below).
4909 * This only behaves like a boolean not operation if the value in
4910 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4911 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4912 * before calling this function.
4913 * - This function does not process by-reference variants.
4914 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4915 * according to the following table:
4916 *| Input Type Output Type
4917 *| ---------- -----------
4918 *| VT_EMPTY VT_I2
4919 *| VT_R4 VT_I4
4920 *| VT_R8 VT_I4
4921 *| VT_BSTR VT_I4
4922 *| VT_DECIMAL VT_I4
4923 *| VT_CY VT_I4
4924 *| (All others) Unchanged
4926 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4928 VARIANT varIn;
4929 HRESULT hRet = S_OK;
4930 VARIANT temp;
4932 VariantInit(&temp);
4934 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4935 debugstr_VF(pVarIn), pVarOut);
4937 /* Handle VT_DISPATCH by storing and taking address of returned value */
4938 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4940 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4941 if (FAILED(hRet)) goto VarNot_Exit;
4942 pVarIn = &temp;
4945 V_VT(pVarOut) = V_VT(pVarIn);
4947 switch (V_VT(pVarIn))
4949 case VT_I1:
4950 V_I4(pVarOut) = ~V_I1(pVarIn);
4951 V_VT(pVarOut) = VT_I4;
4952 break;
4953 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4954 case VT_BOOL:
4955 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4956 case VT_UI2:
4957 V_I4(pVarOut) = ~V_UI2(pVarIn);
4958 V_VT(pVarOut) = VT_I4;
4959 break;
4960 case VT_DECIMAL:
4961 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4962 if (FAILED(hRet))
4963 break;
4964 pVarIn = &varIn;
4965 /* Fall through ... */
4966 case VT_INT:
4967 V_VT(pVarOut) = VT_I4;
4968 /* Fall through ... */
4969 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4970 case VT_UINT:
4971 case VT_UI4:
4972 V_I4(pVarOut) = ~V_UI4(pVarIn);
4973 V_VT(pVarOut) = VT_I4;
4974 break;
4975 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
4976 case VT_UI8:
4977 V_I4(pVarOut) = ~V_UI8(pVarIn);
4978 V_VT(pVarOut) = VT_I4;
4979 break;
4980 case VT_R4:
4981 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
4982 V_I4(pVarOut) = ~V_I4(pVarOut);
4983 V_VT(pVarOut) = VT_I4;
4984 break;
4985 case VT_BSTR:
4986 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4987 if (FAILED(hRet))
4988 break;
4989 pVarIn = &varIn;
4990 /* Fall through ... */
4991 case VT_DATE:
4992 case VT_R8:
4993 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4994 V_I4(pVarOut) = ~V_I4(pVarOut);
4995 V_VT(pVarOut) = VT_I4;
4996 break;
4997 case VT_CY:
4998 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4999 V_I4(pVarOut) = ~V_I4(pVarOut);
5000 V_VT(pVarOut) = VT_I4;
5001 break;
5002 case VT_EMPTY:
5003 V_I2(pVarOut) = ~0;
5004 V_VT(pVarOut) = VT_I2;
5005 break;
5006 case VT_NULL:
5007 /* No-Op */
5008 break;
5009 default:
5010 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5011 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5012 hRet = DISP_E_BADVARTYPE;
5013 else
5014 hRet = DISP_E_TYPEMISMATCH;
5016 VarNot_Exit:
5017 if (FAILED(hRet))
5018 V_VT(pVarOut) = VT_EMPTY;
5019 VariantClear(&temp);
5021 return hRet;
5024 /**********************************************************************
5025 * VarRound [OLEAUT32.175]
5027 * Perform a round operation on a variant.
5029 * PARAMS
5030 * pVarIn [I] Source variant
5031 * deci [I] Number of decimals to round to
5032 * pVarOut [O] Destination for converted value
5034 * RETURNS
5035 * Success: S_OK. pVarOut contains the converted value.
5036 * Failure: An HRESULT error code indicating the error.
5038 * NOTES
5039 * - Floating point values are rounded to the desired number of decimals.
5040 * - Some integer types are just copied to the return variable.
5041 * - Some other integer types are not handled and fail.
5043 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5045 VARIANT varIn;
5046 HRESULT hRet = S_OK;
5047 float factor;
5048 VARIANT temp;
5050 VariantInit(&temp);
5052 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
5054 /* Handle VT_DISPATCH by storing and taking address of returned value */
5055 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5057 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5058 if (FAILED(hRet)) goto VarRound_Exit;
5059 pVarIn = &temp;
5062 switch (V_VT(pVarIn))
5064 /* cases that fail on windows */
5065 case VT_I1:
5066 case VT_I8:
5067 case VT_UI2:
5068 case VT_UI4:
5069 hRet = DISP_E_BADVARTYPE;
5070 break;
5072 /* cases just copying in to out */
5073 case VT_UI1:
5074 V_VT(pVarOut) = V_VT(pVarIn);
5075 V_UI1(pVarOut) = V_UI1(pVarIn);
5076 break;
5077 case VT_I2:
5078 V_VT(pVarOut) = V_VT(pVarIn);
5079 V_I2(pVarOut) = V_I2(pVarIn);
5080 break;
5081 case VT_I4:
5082 V_VT(pVarOut) = V_VT(pVarIn);
5083 V_I4(pVarOut) = V_I4(pVarIn);
5084 break;
5085 case VT_NULL:
5086 V_VT(pVarOut) = V_VT(pVarIn);
5087 /* value unchanged */
5088 break;
5090 /* cases that change type */
5091 case VT_EMPTY:
5092 V_VT(pVarOut) = VT_I2;
5093 V_I2(pVarOut) = 0;
5094 break;
5095 case VT_BOOL:
5096 V_VT(pVarOut) = VT_I2;
5097 V_I2(pVarOut) = V_BOOL(pVarIn);
5098 break;
5099 case VT_BSTR:
5100 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5101 if (FAILED(hRet))
5102 break;
5103 V_VT(&varIn)=VT_R8;
5104 pVarIn = &varIn;
5105 /* Fall through ... */
5107 /* cases we need to do math */
5108 case VT_R8:
5109 if (V_R8(pVarIn)>0) {
5110 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5111 } else {
5112 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5114 V_VT(pVarOut) = V_VT(pVarIn);
5115 break;
5116 case VT_R4:
5117 if (V_R4(pVarIn)>0) {
5118 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5119 } else {
5120 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5122 V_VT(pVarOut) = V_VT(pVarIn);
5123 break;
5124 case VT_DATE:
5125 if (V_DATE(pVarIn)>0) {
5126 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5127 } else {
5128 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5130 V_VT(pVarOut) = V_VT(pVarIn);
5131 break;
5132 case VT_CY:
5133 if (deci>3)
5134 factor=1;
5135 else
5136 factor=pow(10, 4-deci);
5138 if (V_CY(pVarIn).int64>0) {
5139 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5140 } else {
5141 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5143 V_VT(pVarOut) = V_VT(pVarIn);
5144 break;
5146 /* cases we don't know yet */
5147 default:
5148 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5149 V_VT(pVarIn) & VT_TYPEMASK, deci);
5150 hRet = DISP_E_BADVARTYPE;
5152 VarRound_Exit:
5153 if (FAILED(hRet))
5154 V_VT(pVarOut) = VT_EMPTY;
5155 VariantClear(&temp);
5157 TRACE("returning 0x%08x (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
5158 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
5159 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
5161 return hRet;
5164 /**********************************************************************
5165 * VarIdiv [OLEAUT32.153]
5167 * Converts input variants to integers and divides them.
5169 * PARAMS
5170 * left [I] Left hand variant
5171 * right [I] Right hand variant
5172 * result [O] Destination for quotient
5174 * RETURNS
5175 * Success: S_OK. result contains the quotient.
5176 * Failure: An HRESULT error code indicating the error.
5178 * NOTES
5179 * If either expression is null, null is returned, as per MSDN
5181 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5183 HRESULT hres = S_OK;
5184 VARTYPE resvt = VT_EMPTY;
5185 VARTYPE leftvt,rightvt;
5186 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5187 VARIANT lv,rv;
5188 VARIANT tempLeft, tempRight;
5190 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5191 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5193 VariantInit(&lv);
5194 VariantInit(&rv);
5195 VariantInit(&tempLeft);
5196 VariantInit(&tempRight);
5198 leftvt = V_VT(left)&VT_TYPEMASK;
5199 rightvt = V_VT(right)&VT_TYPEMASK;
5200 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5201 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5203 if (leftExtraFlags != rightExtraFlags)
5205 hres = DISP_E_BADVARTYPE;
5206 goto end;
5208 ExtraFlags = leftExtraFlags;
5210 /* Native VarIdiv always returns an error when using extra
5211 * flags or if the variant combination is I8 and INT.
5213 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5214 (leftvt == VT_INT && rightvt == VT_I8) ||
5215 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5216 ExtraFlags != 0)
5218 hres = DISP_E_BADVARTYPE;
5219 goto end;
5222 /* Determine variant type */
5223 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5225 V_VT(result) = VT_NULL;
5226 hres = S_OK;
5227 goto end;
5229 else if (leftvt == VT_I8 || rightvt == VT_I8)
5230 resvt = VT_I8;
5231 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5232 leftvt == VT_INT || rightvt == VT_INT ||
5233 leftvt == VT_UINT || rightvt == VT_UINT ||
5234 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5235 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5236 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5237 leftvt == VT_I1 || rightvt == VT_I1 ||
5238 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5239 leftvt == VT_DATE || rightvt == VT_DATE ||
5240 leftvt == VT_CY || rightvt == VT_CY ||
5241 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5242 leftvt == VT_R8 || rightvt == VT_R8 ||
5243 leftvt == VT_R4 || rightvt == VT_R4)
5244 resvt = VT_I4;
5245 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5246 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5247 leftvt == VT_EMPTY)
5248 resvt = VT_I2;
5249 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5250 resvt = VT_UI1;
5251 else
5253 hres = DISP_E_BADVARTYPE;
5254 goto end;
5257 /* coerce to the result type */
5258 hres = VariantChangeType(&lv, left, 0, resvt);
5259 if (hres != S_OK) goto end;
5260 hres = VariantChangeType(&rv, right, 0, resvt);
5261 if (hres != S_OK) goto end;
5263 /* do the math */
5264 V_VT(result) = resvt;
5265 switch (resvt)
5267 case VT_UI1:
5268 if (V_UI1(&rv) == 0)
5270 hres = DISP_E_DIVBYZERO;
5271 V_VT(result) = VT_EMPTY;
5273 else
5274 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5275 break;
5276 case VT_I2:
5277 if (V_I2(&rv) == 0)
5279 hres = DISP_E_DIVBYZERO;
5280 V_VT(result) = VT_EMPTY;
5282 else
5283 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5284 break;
5285 case VT_I4:
5286 if (V_I4(&rv) == 0)
5288 hres = DISP_E_DIVBYZERO;
5289 V_VT(result) = VT_EMPTY;
5291 else
5292 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5293 break;
5294 case VT_I8:
5295 if (V_I8(&rv) == 0)
5297 hres = DISP_E_DIVBYZERO;
5298 V_VT(result) = VT_EMPTY;
5300 else
5301 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5302 break;
5303 default:
5304 FIXME("Couldn't integer divide variant types %d,%d\n",
5305 leftvt,rightvt);
5308 end:
5309 VariantClear(&lv);
5310 VariantClear(&rv);
5311 VariantClear(&tempLeft);
5312 VariantClear(&tempRight);
5314 return hres;
5318 /**********************************************************************
5319 * VarMod [OLEAUT32.155]
5321 * Perform the modulus operation of the right hand variant on the left
5323 * PARAMS
5324 * left [I] Left hand variant
5325 * right [I] Right hand variant
5326 * result [O] Destination for converted value
5328 * RETURNS
5329 * Success: S_OK. result contains the remainder.
5330 * Failure: An HRESULT error code indicating the error.
5332 * NOTE:
5333 * If an error occurs the type of result will be modified but the value will not be.
5334 * Doesn't support arrays or any special flags yet.
5336 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5338 BOOL lOk = TRUE;
5339 BOOL rOk = TRUE;
5340 HRESULT rc = E_FAIL;
5341 int resT = 0;
5342 VARIANT lv,rv;
5343 VARIANT tempLeft, tempRight;
5345 VariantInit(&tempLeft);
5346 VariantInit(&tempRight);
5347 VariantInit(&lv);
5348 VariantInit(&rv);
5350 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5351 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5353 /* Handle VT_DISPATCH by storing and taking address of returned value */
5354 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5356 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5357 if (FAILED(rc)) goto end;
5358 left = &tempLeft;
5360 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5362 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5363 if (FAILED(rc)) goto end;
5364 right = &tempRight;
5367 /* check for invalid inputs */
5368 lOk = TRUE;
5369 switch (V_VT(left) & VT_TYPEMASK) {
5370 case VT_BOOL :
5371 case VT_I1 :
5372 case VT_I2 :
5373 case VT_I4 :
5374 case VT_I8 :
5375 case VT_INT :
5376 case VT_UI1 :
5377 case VT_UI2 :
5378 case VT_UI4 :
5379 case VT_UI8 :
5380 case VT_UINT :
5381 case VT_R4 :
5382 case VT_R8 :
5383 case VT_CY :
5384 case VT_EMPTY:
5385 case VT_DATE :
5386 case VT_BSTR :
5387 case VT_DECIMAL:
5388 break;
5389 case VT_VARIANT:
5390 case VT_UNKNOWN:
5391 V_VT(result) = VT_EMPTY;
5392 rc = DISP_E_TYPEMISMATCH;
5393 goto end;
5394 case VT_ERROR:
5395 rc = DISP_E_TYPEMISMATCH;
5396 goto end;
5397 case VT_RECORD:
5398 V_VT(result) = VT_EMPTY;
5399 rc = DISP_E_TYPEMISMATCH;
5400 goto end;
5401 case VT_NULL:
5402 break;
5403 default:
5404 V_VT(result) = VT_EMPTY;
5405 rc = DISP_E_BADVARTYPE;
5406 goto end;
5410 rOk = TRUE;
5411 switch (V_VT(right) & VT_TYPEMASK) {
5412 case VT_BOOL :
5413 case VT_I1 :
5414 case VT_I2 :
5415 case VT_I4 :
5416 case VT_I8 :
5417 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5419 V_VT(result) = VT_EMPTY;
5420 rc = DISP_E_TYPEMISMATCH;
5421 goto end;
5423 case VT_INT :
5424 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5426 V_VT(result) = VT_EMPTY;
5427 rc = DISP_E_TYPEMISMATCH;
5428 goto end;
5430 case VT_UI1 :
5431 case VT_UI2 :
5432 case VT_UI4 :
5433 case VT_UI8 :
5434 case VT_UINT :
5435 case VT_R4 :
5436 case VT_R8 :
5437 case VT_CY :
5438 if(V_VT(left) == VT_EMPTY)
5440 V_VT(result) = VT_I4;
5441 rc = S_OK;
5442 goto end;
5444 case VT_EMPTY:
5445 case VT_DATE :
5446 case VT_DECIMAL:
5447 if(V_VT(left) == VT_ERROR)
5449 V_VT(result) = VT_EMPTY;
5450 rc = DISP_E_TYPEMISMATCH;
5451 goto end;
5453 case VT_BSTR:
5454 if(V_VT(left) == VT_NULL)
5456 V_VT(result) = VT_NULL;
5457 rc = S_OK;
5458 goto end;
5460 break;
5462 case VT_VOID:
5463 V_VT(result) = VT_EMPTY;
5464 rc = DISP_E_BADVARTYPE;
5465 goto end;
5466 case VT_NULL:
5467 if(V_VT(left) == VT_VOID)
5469 V_VT(result) = VT_EMPTY;
5470 rc = DISP_E_BADVARTYPE;
5471 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5472 lOk)
5474 V_VT(result) = VT_NULL;
5475 rc = S_OK;
5476 } else
5478 V_VT(result) = VT_NULL;
5479 rc = DISP_E_BADVARTYPE;
5481 goto end;
5482 case VT_VARIANT:
5483 case VT_UNKNOWN:
5484 V_VT(result) = VT_EMPTY;
5485 rc = DISP_E_TYPEMISMATCH;
5486 goto end;
5487 case VT_ERROR:
5488 rc = DISP_E_TYPEMISMATCH;
5489 goto end;
5490 case VT_RECORD:
5491 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5493 V_VT(result) = VT_EMPTY;
5494 rc = DISP_E_BADVARTYPE;
5495 } else
5497 V_VT(result) = VT_EMPTY;
5498 rc = DISP_E_TYPEMISMATCH;
5500 goto end;
5501 default:
5502 V_VT(result) = VT_EMPTY;
5503 rc = DISP_E_BADVARTYPE;
5504 goto end;
5507 /* determine the result type */
5508 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5509 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5510 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5511 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5512 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5513 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5514 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5515 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5516 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5517 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5518 else resT = VT_I4; /* most outputs are I4 */
5520 /* convert to I8 for the modulo */
5521 rc = VariantChangeType(&lv, left, 0, VT_I8);
5522 if(FAILED(rc))
5524 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5525 goto end;
5528 rc = VariantChangeType(&rv, right, 0, VT_I8);
5529 if(FAILED(rc))
5531 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5532 goto end;
5535 /* if right is zero set VT_EMPTY and return divide by zero */
5536 if(V_I8(&rv) == 0)
5538 V_VT(result) = VT_EMPTY;
5539 rc = DISP_E_DIVBYZERO;
5540 goto end;
5543 /* perform the modulo operation */
5544 V_VT(result) = VT_I8;
5545 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5547 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));
5549 /* convert left and right to the destination type */
5550 rc = VariantChangeType(result, result, 0, resT);
5551 if(FAILED(rc))
5553 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5554 /* fall to end of function */
5557 end:
5558 VariantClear(&lv);
5559 VariantClear(&rv);
5560 VariantClear(&tempLeft);
5561 VariantClear(&tempRight);
5562 return rc;
5565 /**********************************************************************
5566 * VarPow [OLEAUT32.158]
5568 * Computes the power of one variant to another variant.
5570 * PARAMS
5571 * left [I] First variant
5572 * right [I] Second variant
5573 * result [O] Result variant
5575 * RETURNS
5576 * Success: S_OK.
5577 * Failure: An HRESULT error code indicating the error.
5579 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5581 HRESULT hr = S_OK;
5582 VARIANT dl,dr;
5583 VARTYPE resvt = VT_EMPTY;
5584 VARTYPE leftvt,rightvt;
5585 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5586 VARIANT tempLeft, tempRight;
5588 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
5589 right, debugstr_VT(right), debugstr_VF(right), result);
5591 VariantInit(&dl);
5592 VariantInit(&dr);
5593 VariantInit(&tempLeft);
5594 VariantInit(&tempRight);
5596 /* Handle VT_DISPATCH by storing and taking address of returned value */
5597 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5599 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5600 if (FAILED(hr)) goto end;
5601 left = &tempLeft;
5603 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5605 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5606 if (FAILED(hr)) goto end;
5607 right = &tempRight;
5610 leftvt = V_VT(left)&VT_TYPEMASK;
5611 rightvt = V_VT(right)&VT_TYPEMASK;
5612 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5613 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5615 if (leftExtraFlags != rightExtraFlags)
5617 hr = DISP_E_BADVARTYPE;
5618 goto end;
5620 ExtraFlags = leftExtraFlags;
5622 /* Native VarPow always returns an error when using extra flags */
5623 if (ExtraFlags != 0)
5625 hr = DISP_E_BADVARTYPE;
5626 goto end;
5629 /* Determine return type */
5630 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5631 V_VT(result) = VT_NULL;
5632 hr = S_OK;
5633 goto end;
5635 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5636 leftvt == VT_I4 || leftvt == VT_R4 ||
5637 leftvt == VT_R8 || leftvt == VT_CY ||
5638 leftvt == VT_DATE || leftvt == VT_BSTR ||
5639 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5640 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5641 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5642 rightvt == VT_I4 || rightvt == VT_R4 ||
5643 rightvt == VT_R8 || rightvt == VT_CY ||
5644 rightvt == VT_DATE || rightvt == VT_BSTR ||
5645 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5646 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5647 resvt = VT_R8;
5648 else
5650 hr = DISP_E_BADVARTYPE;
5651 goto end;
5654 hr = VariantChangeType(&dl,left,0,resvt);
5655 if (!SUCCEEDED(hr)) {
5656 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5657 hr = E_FAIL;
5658 goto end;
5661 hr = VariantChangeType(&dr,right,0,resvt);
5662 if (!SUCCEEDED(hr)) {
5663 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5664 hr = E_FAIL;
5665 goto end;
5668 V_VT(result) = VT_R8;
5669 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5671 end:
5672 VariantClear(&dl);
5673 VariantClear(&dr);
5674 VariantClear(&tempLeft);
5675 VariantClear(&tempRight);
5677 return hr;
5680 /**********************************************************************
5681 * VarImp [OLEAUT32.154]
5683 * Bitwise implication of two variants.
5685 * PARAMS
5686 * left [I] First variant
5687 * right [I] Second variant
5688 * result [O] Result variant
5690 * RETURNS
5691 * Success: S_OK.
5692 * Failure: An HRESULT error code indicating the error.
5694 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5696 HRESULT hres = S_OK;
5697 VARTYPE resvt = VT_EMPTY;
5698 VARTYPE leftvt,rightvt;
5699 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5700 VARIANT lv,rv;
5701 double d;
5702 VARIANT tempLeft, tempRight;
5704 VariantInit(&lv);
5705 VariantInit(&rv);
5706 VariantInit(&tempLeft);
5707 VariantInit(&tempRight);
5709 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5710 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5712 /* Handle VT_DISPATCH by storing and taking address of returned value */
5713 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5715 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5716 if (FAILED(hres)) goto VarImp_Exit;
5717 left = &tempLeft;
5719 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5721 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5722 if (FAILED(hres)) goto VarImp_Exit;
5723 right = &tempRight;
5726 leftvt = V_VT(left)&VT_TYPEMASK;
5727 rightvt = V_VT(right)&VT_TYPEMASK;
5728 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5729 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5731 if (leftExtraFlags != rightExtraFlags)
5733 hres = DISP_E_BADVARTYPE;
5734 goto VarImp_Exit;
5736 ExtraFlags = leftExtraFlags;
5738 /* Native VarImp always returns an error when using extra
5739 * flags or if the variants are I8 and INT.
5741 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5742 ExtraFlags != 0)
5744 hres = DISP_E_BADVARTYPE;
5745 goto VarImp_Exit;
5748 /* Determine result type */
5749 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5750 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5752 V_VT(result) = VT_NULL;
5753 hres = S_OK;
5754 goto VarImp_Exit;
5756 else if (leftvt == VT_I8 || rightvt == VT_I8)
5757 resvt = VT_I8;
5758 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5759 leftvt == VT_INT || rightvt == VT_INT ||
5760 leftvt == VT_UINT || rightvt == VT_UINT ||
5761 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5762 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5763 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5764 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5765 leftvt == VT_DATE || rightvt == VT_DATE ||
5766 leftvt == VT_CY || rightvt == VT_CY ||
5767 leftvt == VT_R8 || rightvt == VT_R8 ||
5768 leftvt == VT_R4 || rightvt == VT_R4 ||
5769 leftvt == VT_I1 || rightvt == VT_I1)
5770 resvt = VT_I4;
5771 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5772 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5773 (leftvt == VT_NULL && rightvt == VT_UI1))
5774 resvt = VT_UI1;
5775 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5776 leftvt == VT_I2 || rightvt == VT_I2 ||
5777 leftvt == VT_UI1 || rightvt == VT_UI1)
5778 resvt = VT_I2;
5779 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5780 leftvt == VT_BSTR || rightvt == VT_BSTR)
5781 resvt = VT_BOOL;
5783 /* VT_NULL requires special handling for when the opposite
5784 * variant is equal to something other than -1.
5785 * (NULL Imp 0 = NULL, NULL Imp n = n)
5787 if (leftvt == VT_NULL)
5789 VARIANT_BOOL b;
5790 switch(rightvt)
5792 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5793 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5794 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5795 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5796 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5797 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5798 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5799 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5800 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5801 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5802 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5803 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5804 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5805 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5806 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5807 case VT_DECIMAL:
5808 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5809 resvt = VT_NULL;
5810 break;
5811 case VT_BSTR:
5812 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5813 if (FAILED(hres)) goto VarImp_Exit;
5814 else if (!b)
5815 V_VT(result) = VT_NULL;
5816 else
5818 V_VT(result) = VT_BOOL;
5819 V_BOOL(result) = b;
5821 goto VarImp_Exit;
5823 if (resvt == VT_NULL)
5825 V_VT(result) = resvt;
5826 goto VarImp_Exit;
5828 else
5830 hres = VariantChangeType(result,right,0,resvt);
5831 goto VarImp_Exit;
5835 /* Special handling is required when NULL is the right variant.
5836 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5838 else if (rightvt == VT_NULL)
5840 VARIANT_BOOL b;
5841 switch(leftvt)
5843 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5844 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5845 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5846 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5847 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5848 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5849 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5850 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5851 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5852 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5853 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5854 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5855 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5856 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5857 case VT_DECIMAL:
5858 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5859 resvt = VT_NULL;
5860 break;
5861 case VT_BSTR:
5862 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5863 if (FAILED(hres)) goto VarImp_Exit;
5864 else if (b == VARIANT_TRUE)
5865 resvt = VT_NULL;
5867 if (resvt == VT_NULL)
5869 V_VT(result) = resvt;
5870 goto VarImp_Exit;
5874 hres = VariantCopy(&lv, left);
5875 if (FAILED(hres)) goto VarImp_Exit;
5877 if (rightvt == VT_NULL)
5879 memset( &rv, 0, sizeof(rv) );
5880 V_VT(&rv) = resvt;
5882 else
5884 hres = VariantCopy(&rv, right);
5885 if (FAILED(hres)) goto VarImp_Exit;
5888 if (V_VT(&lv) == VT_BSTR &&
5889 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5890 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5891 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5892 hres = VariantChangeType(&lv,&lv,0,resvt);
5893 if (FAILED(hres)) goto VarImp_Exit;
5895 if (V_VT(&rv) == VT_BSTR &&
5896 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5897 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5898 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5899 hres = VariantChangeType(&rv, &rv, 0, resvt);
5900 if (FAILED(hres)) goto VarImp_Exit;
5902 /* do the math */
5903 V_VT(result) = resvt;
5904 switch (resvt)
5906 case VT_I8:
5907 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5908 break;
5909 case VT_I4:
5910 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5911 break;
5912 case VT_I2:
5913 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
5914 break;
5915 case VT_UI1:
5916 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
5917 break;
5918 case VT_BOOL:
5919 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
5920 break;
5921 default:
5922 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5923 leftvt,rightvt);
5926 VarImp_Exit:
5928 VariantClear(&lv);
5929 VariantClear(&rv);
5930 VariantClear(&tempLeft);
5931 VariantClear(&tempRight);
5933 return hres;