wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / oleaut32 / variant.c
blob5d32c3b07a6db8399deb90f3aed867fa66ae2716
1 /*
2 * VARIANT
4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
7 * Copyright 2006 Google (Benjamin Arai)
9 * The algorithm for conversion from Julian days to day/month/year is based on
10 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
11 * Copyright 1994-7 Regents of the University of California
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "config.h"
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wine/unicode.h"
41 #include "winerror.h"
42 #include "variant.h"
43 #include "resource.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(variant);
48 const char * const wine_vtypes[VT_CLSID+1] =
50 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
51 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
52 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
53 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
54 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
55 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
56 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
57 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
58 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
61 const char * const wine_vflags[16] =
63 "",
64 "|VT_VECTOR",
65 "|VT_ARRAY",
66 "|VT_VECTOR|VT_ARRAY",
67 "|VT_BYREF",
68 "|VT_VECTOR|VT_ARRAY",
69 "|VT_ARRAY|VT_BYREF",
70 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
71 "|VT_HARDTYPE",
72 "|VT_VECTOR|VT_HARDTYPE",
73 "|VT_ARRAY|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
75 "|VT_BYREF|VT_HARDTYPE",
76 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
77 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
78 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
81 /* Convert a variant from one type to another */
82 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
83 VARIANTARG* ps, VARTYPE vt)
85 HRESULT res = DISP_E_TYPEMISMATCH;
86 VARTYPE vtFrom = V_TYPE(ps);
87 DWORD dwFlags = 0;
89 TRACE("(%p->(%s%s),0x%08x,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
90 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
91 debugstr_vt(vt), debugstr_vf(vt));
93 if (vt == VT_BSTR || vtFrom == VT_BSTR)
95 /* All flags passed to low level function are only used for
96 * changing to or from strings. Map these here.
98 if (wFlags & VARIANT_LOCALBOOL)
99 dwFlags |= VAR_LOCALBOOL;
100 if (wFlags & VARIANT_CALENDAR_HIJRI)
101 dwFlags |= VAR_CALENDAR_HIJRI;
102 if (wFlags & VARIANT_CALENDAR_THAI)
103 dwFlags |= VAR_CALENDAR_THAI;
104 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
105 dwFlags |= VAR_CALENDAR_GREGORIAN;
106 if (wFlags & VARIANT_NOUSEROVERRIDE)
107 dwFlags |= LOCALE_NOUSEROVERRIDE;
108 if (wFlags & VARIANT_USE_NLS)
109 dwFlags |= LOCALE_USE_NLS;
112 /* Map int/uint to i4/ui4 */
113 if (vt == VT_INT)
114 vt = VT_I4;
115 else if (vt == VT_UINT)
116 vt = VT_UI4;
118 if (vtFrom == VT_INT)
119 vtFrom = VT_I4;
120 else if (vtFrom == VT_UINT)
121 vtFrom = VT_UI4;
123 if (vt == vtFrom)
124 return VariantCopy(pd, ps);
126 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
128 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
129 * accessing the default object property.
131 return DISP_E_TYPEMISMATCH;
134 switch (vt)
136 case VT_EMPTY:
137 if (vtFrom == VT_NULL)
138 return DISP_E_TYPEMISMATCH;
139 /* ... Fall through */
140 case VT_NULL:
141 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
143 res = VariantClear( pd );
144 if (vt == VT_NULL && SUCCEEDED(res))
145 V_VT(pd) = VT_NULL;
147 return res;
149 case VT_I1:
150 switch (vtFrom)
152 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
153 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
154 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
155 case VT_UI1: V_I1(pd) = V_UI1(ps); return S_OK;
156 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
157 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
158 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
159 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
160 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
161 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
162 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
163 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
164 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
165 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
166 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
167 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
169 break;
171 case VT_I2:
172 switch (vtFrom)
174 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
175 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
176 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
177 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
178 case VT_UI2: V_I2(pd) = V_UI2(ps); return S_OK;
179 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
180 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
181 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
182 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
183 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
184 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
185 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
186 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
187 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
188 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
189 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
191 break;
193 case VT_I4:
194 switch (vtFrom)
196 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
197 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
198 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
199 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
200 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
201 case VT_UI4: V_I4(pd) = V_UI4(ps); return S_OK;
202 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
203 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
204 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
205 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
206 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
207 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
208 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
209 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
210 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
211 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
213 break;
215 case VT_UI1:
216 switch (vtFrom)
218 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
219 case VT_I1: V_UI1(pd) = V_I1(ps); return S_OK;
220 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
221 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
222 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
223 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
224 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
225 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
226 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
227 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
228 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
229 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
230 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
231 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
232 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
233 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
235 break;
237 case VT_UI2:
238 switch (vtFrom)
240 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
241 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
242 case VT_I2: V_UI2(pd) = V_I2(ps); return S_OK;
243 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
244 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
245 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
246 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
247 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
248 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
249 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
250 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
251 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
252 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
253 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
254 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
255 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
257 break;
259 case VT_UI4:
260 switch (vtFrom)
262 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
263 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
264 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
265 case VT_I4: V_UI4(pd) = V_I4(ps); return S_OK;
266 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
267 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
268 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
269 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
270 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
271 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
272 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
273 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
274 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
275 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
276 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
277 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
279 break;
281 case VT_UI8:
282 switch (vtFrom)
284 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
285 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
286 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
287 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
288 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
289 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
290 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
291 case VT_I8: V_UI8(pd) = V_I8(ps); return S_OK;
292 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
293 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
294 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
295 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
296 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
297 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
298 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
299 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
301 break;
303 case VT_I8:
304 switch (vtFrom)
306 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
307 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
308 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
309 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
310 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
311 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
312 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
313 case VT_UI8: V_I8(pd) = V_UI8(ps); return S_OK;
314 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
315 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
316 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
317 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
318 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
319 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
320 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
321 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
323 break;
325 case VT_R4:
326 switch (vtFrom)
328 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
329 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
330 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
331 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
332 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
333 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
334 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
335 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
336 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
337 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
338 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
339 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
340 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
341 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
342 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
343 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
345 break;
347 case VT_R8:
348 switch (vtFrom)
350 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
351 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
352 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
353 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
354 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
355 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
356 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
357 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
358 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
359 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
360 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
361 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
362 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
363 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
364 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
365 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
367 break;
369 case VT_DATE:
370 switch (vtFrom)
372 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
373 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
374 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
375 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
376 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
377 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
378 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
379 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
380 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
381 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
382 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
383 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
384 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
385 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
386 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
387 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
389 break;
391 case VT_BOOL:
392 switch (vtFrom)
394 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
395 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
396 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
397 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
398 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
399 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
400 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
401 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
402 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
403 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
404 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
405 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
406 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
407 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
408 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
409 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
411 break;
413 case VT_BSTR:
414 switch (vtFrom)
416 case VT_EMPTY:
417 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
418 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
419 case VT_BOOL:
420 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
421 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
422 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
423 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
424 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
425 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
426 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
427 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
428 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
429 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
430 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
434 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
435 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
436 case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd));
438 break;
440 case VT_CY:
441 switch (vtFrom)
443 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
444 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
445 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
446 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
447 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
448 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
449 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
450 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
451 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
452 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
453 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
454 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
455 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
456 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
457 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
458 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
460 break;
462 case VT_DECIMAL:
463 switch (vtFrom)
465 case VT_EMPTY:
466 case VT_BOOL:
467 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
468 DEC_HI32(&V_DECIMAL(pd)) = 0;
469 DEC_MID32(&V_DECIMAL(pd)) = 0;
470 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
471 * VT_NULL and VT_EMPTY always give a 0 value.
473 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
474 return S_OK;
475 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
476 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
477 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
478 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
479 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
480 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
481 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
482 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
483 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
484 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
485 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
486 case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
487 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
488 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
490 break;
492 case VT_UNKNOWN:
493 switch (vtFrom)
495 case VT_DISPATCH:
496 if (V_DISPATCH(ps) == NULL)
497 V_UNKNOWN(pd) = NULL;
498 else
499 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
500 break;
502 break;
504 case VT_DISPATCH:
505 switch (vtFrom)
507 case VT_UNKNOWN:
508 if (V_UNKNOWN(ps) == NULL)
509 V_DISPATCH(pd) = NULL;
510 else
511 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
512 break;
514 break;
516 case VT_RECORD:
517 break;
519 return res;
522 /* Coerce to/from an array */
523 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
525 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
526 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
528 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
529 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
531 if (V_VT(ps) == vt)
532 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
534 return DISP_E_TYPEMISMATCH;
537 /******************************************************************************
538 * Check if a variants type is valid.
540 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
542 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
544 vt &= VT_TYPEMASK;
546 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
548 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
550 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
551 return DISP_E_BADVARTYPE;
552 if (vt != (VARTYPE)15)
553 return S_OK;
556 return DISP_E_BADVARTYPE;
559 /******************************************************************************
560 * VariantInit [OLEAUT32.8]
562 * Initialise a variant.
564 * PARAMS
565 * pVarg [O] Variant to initialise
567 * RETURNS
568 * Nothing.
570 * NOTES
571 * This function simply sets the type of the variant to VT_EMPTY. It does not
572 * free any existing value, use VariantClear() for that.
574 void WINAPI VariantInit(VARIANTARG* pVarg)
576 TRACE("(%p)\n", pVarg);
578 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
581 HRESULT VARIANT_ClearInd(VARIANTARG *pVarg)
583 HRESULT hres;
585 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
587 hres = VARIANT_ValidateType(V_VT(pVarg));
588 if (FAILED(hres))
589 return hres;
591 switch (V_VT(pVarg))
593 case VT_DISPATCH:
594 case VT_UNKNOWN:
595 if (V_UNKNOWN(pVarg))
596 IUnknown_Release(V_UNKNOWN(pVarg));
597 break;
598 case VT_UNKNOWN | VT_BYREF:
599 case VT_DISPATCH | VT_BYREF:
600 if(*V_UNKNOWNREF(pVarg))
601 IUnknown_Release(*V_UNKNOWNREF(pVarg));
602 break;
603 case VT_BSTR:
604 SysFreeString(V_BSTR(pVarg));
605 break;
606 case VT_BSTR | VT_BYREF:
607 SysFreeString(*V_BSTRREF(pVarg));
608 break;
609 case VT_VARIANT | VT_BYREF:
610 VariantClear(V_VARIANTREF(pVarg));
611 break;
612 case VT_RECORD:
613 case VT_RECORD | VT_BYREF:
615 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
616 if (pBr->pRecInfo)
618 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
619 IRecordInfo_Release(pBr->pRecInfo);
621 break;
623 default:
624 if (V_ISARRAY(pVarg) || (V_VT(pVarg) & ~VT_BYREF) == VT_SAFEARRAY)
626 if (V_ISBYREF(pVarg))
628 if (*V_ARRAYREF(pVarg))
629 hres = SafeArrayDestroy(*V_ARRAYREF(pVarg));
631 else if (V_ARRAY(pVarg))
632 hres = SafeArrayDestroy(V_ARRAY(pVarg));
634 break;
637 V_VT(pVarg) = VT_EMPTY;
638 return hres;
641 /******************************************************************************
642 * VariantClear [OLEAUT32.9]
644 * Clear a variant.
646 * PARAMS
647 * pVarg [I/O] Variant to clear
649 * RETURNS
650 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
651 * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
653 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
655 HRESULT hres;
657 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
659 hres = VARIANT_ValidateType(V_VT(pVarg));
661 if (SUCCEEDED(hres))
663 if (!V_ISBYREF(pVarg))
665 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
667 hres = SafeArrayDestroy(V_ARRAY(pVarg));
669 else if (V_VT(pVarg) == VT_BSTR)
671 SysFreeString(V_BSTR(pVarg));
673 else if (V_VT(pVarg) == VT_RECORD)
675 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
676 if (pBr->pRecInfo)
678 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
679 IRecordInfo_Release(pBr->pRecInfo);
682 else if (V_VT(pVarg) == VT_DISPATCH ||
683 V_VT(pVarg) == VT_UNKNOWN)
685 if (V_UNKNOWN(pVarg))
686 IUnknown_Release(V_UNKNOWN(pVarg));
689 V_VT(pVarg) = VT_EMPTY;
691 return hres;
694 /******************************************************************************
695 * Copy an IRecordInfo object contained in a variant.
697 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
699 HRESULT hres = S_OK;
701 if (pBr->pRecInfo)
703 ULONG ulSize;
705 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
706 if (SUCCEEDED(hres))
708 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
709 if (!pvRecord)
710 hres = E_OUTOFMEMORY;
711 else
713 memcpy(pvRecord, pBr->pvRecord, ulSize);
714 pBr->pvRecord = pvRecord;
716 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
717 if (SUCCEEDED(hres))
718 IRecordInfo_AddRef(pBr->pRecInfo);
722 else if (pBr->pvRecord)
723 hres = E_INVALIDARG;
724 return hres;
727 /******************************************************************************
728 * VariantCopy [OLEAUT32.10]
730 * Copy a variant.
732 * PARAMS
733 * pvargDest [O] Destination for copy
734 * pvargSrc [I] Source variant to copy
736 * RETURNS
737 * Success: S_OK. pvargDest contains a copy of pvargSrc.
738 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
739 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
740 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
741 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
743 * NOTES
744 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
745 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
746 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
747 * fails, so does this function.
748 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
749 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
750 * is copied rather than just any pointers to it.
751 * - For by-value object types the object pointer is copied and the objects
752 * reference count increased using IUnknown_AddRef().
753 * - For all by-reference types, only the referencing pointer is copied.
755 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
757 HRESULT hres = S_OK;
759 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
760 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
761 debugstr_VF(pvargSrc));
763 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
764 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
765 return DISP_E_BADVARTYPE;
767 if (pvargSrc != pvargDest &&
768 SUCCEEDED(hres = VariantClear(pvargDest)))
770 *pvargDest = *pvargSrc; /* Shallow copy the value */
772 if (!V_ISBYREF(pvargSrc))
774 if (V_ISARRAY(pvargSrc))
776 if (V_ARRAY(pvargSrc))
777 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
779 else if (V_VT(pvargSrc) == VT_BSTR)
781 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
782 if (!V_BSTR(pvargDest))
784 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
785 hres = E_OUTOFMEMORY;
788 else if (V_VT(pvargSrc) == VT_RECORD)
790 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
792 else if (V_VT(pvargSrc) == VT_DISPATCH ||
793 V_VT(pvargSrc) == VT_UNKNOWN)
795 if (V_UNKNOWN(pvargSrc))
796 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
800 return hres;
803 /* Return the byte size of a variants data */
804 static inline size_t VARIANT_DataSize(const VARIANT* pv)
806 switch (V_TYPE(pv))
808 case VT_I1:
809 case VT_UI1: return sizeof(BYTE);
810 case VT_I2:
811 case VT_UI2: return sizeof(SHORT);
812 case VT_INT:
813 case VT_UINT:
814 case VT_I4:
815 case VT_UI4: return sizeof(LONG);
816 case VT_I8:
817 case VT_UI8: return sizeof(LONGLONG);
818 case VT_R4: return sizeof(float);
819 case VT_R8: return sizeof(double);
820 case VT_DATE: return sizeof(DATE);
821 case VT_BOOL: return sizeof(VARIANT_BOOL);
822 case VT_DISPATCH:
823 case VT_UNKNOWN:
824 case VT_BSTR: return sizeof(void*);
825 case VT_CY: return sizeof(CY);
826 case VT_ERROR: return sizeof(SCODE);
828 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
829 return 0;
832 /******************************************************************************
833 * VariantCopyInd [OLEAUT32.11]
835 * Copy a variant, dereferencing it if it is by-reference.
837 * PARAMS
838 * pvargDest [O] Destination for copy
839 * pvargSrc [I] Source variant to copy
841 * RETURNS
842 * Success: S_OK. pvargDest contains a copy of pvargSrc.
843 * Failure: An HRESULT error code indicating the error.
845 * NOTES
846 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
847 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
848 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
849 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
850 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
852 * NOTES
853 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
854 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
855 * value.
856 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
857 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
858 * to it. If clearing pvargDest fails, so does this function.
860 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
862 VARIANTARG vTmp, *pSrc = pvargSrc;
863 VARTYPE vt;
864 HRESULT hres = S_OK;
866 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
867 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
868 debugstr_VF(pvargSrc));
870 if (!V_ISBYREF(pvargSrc))
871 return VariantCopy(pvargDest, pvargSrc);
873 /* Argument checking is more lax than VariantCopy()... */
874 vt = V_TYPE(pvargSrc);
875 if (V_ISARRAY(pvargSrc) ||
876 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
877 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
879 /* OK */
881 else
882 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
884 if (pvargSrc == pvargDest)
886 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
887 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
889 vTmp = *pvargSrc;
890 pSrc = &vTmp;
891 V_VT(pvargDest) = VT_EMPTY;
893 else
895 /* Copy into another variant. Free the variant in pvargDest */
896 if (FAILED(hres = VariantClear(pvargDest)))
898 TRACE("VariantClear() of destination failed\n");
899 return hres;
903 if (V_ISARRAY(pSrc))
905 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
906 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
908 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
910 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
911 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
913 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
915 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
916 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
918 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
919 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
921 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
922 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
923 if (*V_UNKNOWNREF(pSrc))
924 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
926 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
928 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
929 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
930 hres = E_INVALIDARG; /* Don't dereference more than one level */
931 else
932 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
934 /* Use the dereferenced variants type value, not VT_VARIANT */
935 goto VariantCopyInd_Return;
937 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
939 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
940 sizeof(DECIMAL) - sizeof(USHORT));
942 else
944 /* Copy the pointed to data into this variant */
945 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
948 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
950 VariantCopyInd_Return:
952 if (pSrc != pvargSrc)
953 VariantClear(pSrc);
955 TRACE("returning 0x%08x, %p->(%s%s)\n", hres, pvargDest,
956 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
957 return hres;
960 /******************************************************************************
961 * VariantChangeType [OLEAUT32.12]
963 * Change the type of a variant.
965 * PARAMS
966 * pvargDest [O] Destination for the converted variant
967 * pvargSrc [O] Source variant to change the type of
968 * wFlags [I] VARIANT_ flags from "oleauto.h"
969 * vt [I] Variant type to change pvargSrc into
971 * RETURNS
972 * Success: S_OK. pvargDest contains the converted value.
973 * Failure: An HRESULT error code describing the failure.
975 * NOTES
976 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
977 * See VariantChangeTypeEx.
979 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
980 USHORT wFlags, VARTYPE vt)
982 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
985 /******************************************************************************
986 * VariantChangeTypeEx [OLEAUT32.147]
988 * Change the type of a variant.
990 * PARAMS
991 * pvargDest [O] Destination for the converted variant
992 * pvargSrc [O] Source variant to change the type of
993 * lcid [I] LCID for the conversion
994 * wFlags [I] VARIANT_ flags from "oleauto.h"
995 * vt [I] Variant type to change pvargSrc into
997 * RETURNS
998 * Success: S_OK. pvargDest contains the converted value.
999 * Failure: An HRESULT error code describing the failure.
1001 * NOTES
1002 * pvargDest and pvargSrc can point to the same variant to perform an in-place
1003 * conversion. If the conversion is successful, pvargSrc will be freed.
1005 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
1006 LCID lcid, USHORT wFlags, VARTYPE vt)
1008 HRESULT res = S_OK;
1010 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%04x,%s%s)\n", pvargDest,
1011 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
1012 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
1013 debugstr_vt(vt), debugstr_vf(vt));
1015 if (vt == VT_CLSID)
1016 res = DISP_E_BADVARTYPE;
1017 else
1019 res = VARIANT_ValidateType(V_VT(pvargSrc));
1021 if (SUCCEEDED(res))
1023 res = VARIANT_ValidateType(vt);
1025 if (SUCCEEDED(res))
1027 VARIANTARG vTmp, vSrcDeref;
1029 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
1030 res = DISP_E_TYPEMISMATCH;
1031 else
1033 V_VT(&vTmp) = VT_EMPTY;
1034 V_VT(&vSrcDeref) = VT_EMPTY;
1035 VariantClear(&vTmp);
1036 VariantClear(&vSrcDeref);
1039 if (SUCCEEDED(res))
1041 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1042 if (SUCCEEDED(res))
1044 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1045 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1046 else
1047 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1049 if (SUCCEEDED(res)) {
1050 V_VT(&vTmp) = vt;
1051 VariantCopy(pvargDest, &vTmp);
1053 VariantClear(&vTmp);
1054 VariantClear(&vSrcDeref);
1061 TRACE("returning 0x%08x, %p->(%s%s)\n", res, pvargDest,
1062 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1063 return res;
1066 /* Date Conversions */
1068 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1070 /* Convert a VT_DATE value to a Julian Date */
1071 static inline int VARIANT_JulianFromDate(int dateIn)
1073 int julianDays = dateIn;
1075 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1076 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1077 return julianDays;
1080 /* Convert a Julian Date to a VT_DATE value */
1081 static inline int VARIANT_DateFromJulian(int dateIn)
1083 int julianDays = dateIn;
1085 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1086 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1087 return julianDays;
1090 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1091 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1093 int j, i, l, n;
1095 l = jd + 68569;
1096 n = l * 4 / 146097;
1097 l -= (n * 146097 + 3) / 4;
1098 i = (4000 * (l + 1)) / 1461001;
1099 l += 31 - (i * 1461) / 4;
1100 j = (l * 80) / 2447;
1101 *day = l - (j * 2447) / 80;
1102 l = j / 11;
1103 *month = (j + 2) - (12 * l);
1104 *year = 100 * (n - 49) + i + l;
1107 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1108 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1110 int m12 = (month - 14) / 12;
1112 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1113 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1116 /* Macros for accessing DOS format date/time fields */
1117 #define DOS_YEAR(x) (1980 + (x >> 9))
1118 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1119 #define DOS_DAY(x) (x & 0x1f)
1120 #define DOS_HOUR(x) (x >> 11)
1121 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1122 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1123 /* Create a DOS format date/time */
1124 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1125 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1127 /* Roll a date forwards or backwards to correct it */
1128 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1130 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1131 short iYear, iMonth, iDay, iHour, iMinute, iSecond;
1133 /* interpret values signed */
1134 iYear = lpUd->st.wYear;
1135 iMonth = lpUd->st.wMonth;
1136 iDay = lpUd->st.wDay;
1137 iHour = lpUd->st.wHour;
1138 iMinute = lpUd->st.wMinute;
1139 iSecond = lpUd->st.wSecond;
1141 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay, iMonth,
1142 iYear, iHour, iMinute, iSecond);
1144 if (iYear > 9999 || iYear < -9999)
1145 return E_INVALIDARG; /* Invalid value */
1146 /* Years < 100 are treated as 1900 + year */
1147 if (iYear > 0 && iYear < 100)
1148 iYear += 1900;
1150 iMinute += iSecond / 60;
1151 iSecond = iSecond % 60;
1152 iHour += iMinute / 60;
1153 iMinute = iMinute % 60;
1154 iDay += iHour / 24;
1155 iHour = iHour % 24;
1156 iYear += iMonth / 12;
1157 iMonth = iMonth % 12;
1158 if (iMonth<=0) {iMonth+=12; iYear--;}
1159 while (iDay > days[iMonth])
1161 if (iMonth == 2 && IsLeapYear(iYear))
1162 iDay -= 29;
1163 else
1164 iDay -= days[iMonth];
1165 iMonth++;
1166 iYear += iMonth / 12;
1167 iMonth = iMonth % 12;
1169 while (iDay <= 0)
1171 iMonth--;
1172 if (iMonth<=0) {iMonth+=12; iYear--;}
1173 if (iMonth == 2 && IsLeapYear(iYear))
1174 iDay += 29;
1175 else
1176 iDay += days[iMonth];
1179 if (iSecond<0){iSecond+=60; iMinute--;}
1180 if (iMinute<0){iMinute+=60; iHour--;}
1181 if (iHour<0) {iHour+=24; iDay--;}
1182 if (iYear<=0) iYear+=2000;
1184 lpUd->st.wYear = iYear;
1185 lpUd->st.wMonth = iMonth;
1186 lpUd->st.wDay = iDay;
1187 lpUd->st.wHour = iHour;
1188 lpUd->st.wMinute = iMinute;
1189 lpUd->st.wSecond = iSecond;
1191 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1192 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1193 return S_OK;
1196 /**********************************************************************
1197 * DosDateTimeToVariantTime [OLEAUT32.14]
1199 * Convert a Dos format date and time into variant VT_DATE format.
1201 * PARAMS
1202 * wDosDate [I] Dos format date
1203 * wDosTime [I] Dos format time
1204 * pDateOut [O] Destination for VT_DATE format
1206 * RETURNS
1207 * Success: TRUE. pDateOut contains the converted time.
1208 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1210 * NOTES
1211 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1212 * - Dos format times are accurate to only 2 second precision.
1213 * - The format of a Dos Date is:
1214 *| Bits Values Meaning
1215 *| ---- ------ -------
1216 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1217 *| the days in the month rolls forward the extra days.
1218 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1219 *| year. 13-15 are invalid.
1220 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1221 * - The format of a Dos Time is:
1222 *| Bits Values Meaning
1223 *| ---- ------ -------
1224 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1225 *| 5-10 0-59 Minutes. 60-63 are invalid.
1226 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1228 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1229 double *pDateOut)
1231 UDATE ud;
1233 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1234 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1235 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1236 pDateOut);
1238 ud.st.wYear = DOS_YEAR(wDosDate);
1239 ud.st.wMonth = DOS_MONTH(wDosDate);
1240 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1241 return FALSE;
1242 ud.st.wDay = DOS_DAY(wDosDate);
1243 ud.st.wHour = DOS_HOUR(wDosTime);
1244 ud.st.wMinute = DOS_MINUTE(wDosTime);
1245 ud.st.wSecond = DOS_SECOND(wDosTime);
1246 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1247 if (ud.st.wHour > 23 || ud.st.wMinute > 59 || ud.st.wSecond > 59)
1248 return FALSE; /* Invalid values in Dos*/
1250 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1253 /**********************************************************************
1254 * VariantTimeToDosDateTime [OLEAUT32.13]
1256 * Convert a variant format date into a Dos format date and time.
1258 * dateIn [I] VT_DATE time format
1259 * pwDosDate [O] Destination for Dos format date
1260 * pwDosTime [O] Destination for Dos format time
1262 * RETURNS
1263 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1264 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1266 * NOTES
1267 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1269 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1271 UDATE ud;
1273 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1275 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1276 return FALSE;
1278 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1279 return FALSE;
1281 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1282 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1284 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1285 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1286 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1287 return TRUE;
1290 /***********************************************************************
1291 * SystemTimeToVariantTime [OLEAUT32.184]
1293 * Convert a System format date and time into variant VT_DATE format.
1295 * PARAMS
1296 * lpSt [I] System format date and time
1297 * pDateOut [O] Destination for VT_DATE format date
1299 * RETURNS
1300 * Success: TRUE. *pDateOut contains the converted value.
1301 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1303 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1305 UDATE ud;
1307 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1308 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1310 if (lpSt->wMonth > 12)
1311 return FALSE;
1313 ud.st = *lpSt;
1314 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1317 /***********************************************************************
1318 * VariantTimeToSystemTime [OLEAUT32.185]
1320 * Convert a variant VT_DATE into a System format date and time.
1322 * PARAMS
1323 * datein [I] Variant VT_DATE format date
1324 * lpSt [O] Destination for System format date and time
1326 * RETURNS
1327 * Success: TRUE. *lpSt contains the converted value.
1328 * Failure: FALSE, if dateIn is too large or small.
1330 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1332 UDATE ud;
1334 TRACE("(%g,%p)\n", dateIn, lpSt);
1336 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1337 return FALSE;
1339 *lpSt = ud.st;
1340 return TRUE;
1343 /***********************************************************************
1344 * VarDateFromUdateEx [OLEAUT32.319]
1346 * Convert an unpacked format date and time to a variant VT_DATE.
1348 * PARAMS
1349 * pUdateIn [I] Unpacked format date and time to convert
1350 * lcid [I] Locale identifier for the conversion
1351 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1352 * pDateOut [O] Destination for variant VT_DATE.
1354 * RETURNS
1355 * Success: S_OK. *pDateOut contains the converted value.
1356 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1358 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1360 UDATE ud;
1361 double dateVal, dateSign;
1363 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1364 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1365 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1366 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1367 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1369 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1370 FIXME("lcid possibly not handled, treating as en-us\n");
1372 ud = *pUdateIn;
1374 if (dwFlags & VAR_VALIDDATE)
1375 WARN("Ignoring VAR_VALIDDATE\n");
1377 if (FAILED(VARIANT_RollUdate(&ud)))
1378 return E_INVALIDARG;
1380 /* Date */
1381 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1383 /* Sign */
1384 dateSign = (dateVal < 0.0) ? -1.0 : 1.0;
1386 /* Time */
1387 dateVal += ud.st.wHour / 24.0 * dateSign;
1388 dateVal += ud.st.wMinute / 1440.0 * dateSign;
1389 dateVal += ud.st.wSecond / 86400.0 * dateSign;
1391 TRACE("Returning %g\n", dateVal);
1392 *pDateOut = dateVal;
1393 return S_OK;
1396 /***********************************************************************
1397 * VarDateFromUdate [OLEAUT32.330]
1399 * Convert an unpacked format date and time to a variant VT_DATE.
1401 * PARAMS
1402 * pUdateIn [I] Unpacked format date and time to convert
1403 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1404 * pDateOut [O] Destination for variant VT_DATE.
1406 * RETURNS
1407 * Success: S_OK. *pDateOut contains the converted value.
1408 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1410 * NOTES
1411 * This function uses the United States English locale for the conversion. Use
1412 * VarDateFromUdateEx() for alternate locales.
1414 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1416 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1418 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1421 /***********************************************************************
1422 * VarUdateFromDate [OLEAUT32.331]
1424 * Convert a variant VT_DATE into an unpacked format date and time.
1426 * PARAMS
1427 * datein [I] Variant VT_DATE format date
1428 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1429 * lpUdate [O] Destination for unpacked format date and time
1431 * RETURNS
1432 * Success: S_OK. *lpUdate contains the converted value.
1433 * Failure: E_INVALIDARG, if dateIn is too large or small.
1435 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1437 /* Cumulative totals of days per month */
1438 static const USHORT cumulativeDays[] =
1440 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1442 double datePart, timePart;
1443 int julianDays;
1445 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1447 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1448 return E_INVALIDARG;
1450 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1451 /* Compensate for int truncation (always downwards) */
1452 timePart = fabs(dateIn - datePart) + 0.00000000001;
1453 if (timePart >= 1.0)
1454 timePart -= 0.00000000001;
1456 /* Date */
1457 julianDays = VARIANT_JulianFromDate(dateIn);
1458 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1459 &lpUdate->st.wDay);
1461 datePart = (datePart + 1.5) / 7.0;
1462 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1463 if (lpUdate->st.wDayOfWeek == 0)
1464 lpUdate->st.wDayOfWeek = 5;
1465 else if (lpUdate->st.wDayOfWeek == 1)
1466 lpUdate->st.wDayOfWeek = 6;
1467 else
1468 lpUdate->st.wDayOfWeek -= 2;
1470 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1471 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1472 else
1473 lpUdate->wDayOfYear = 0;
1475 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1476 lpUdate->wDayOfYear += lpUdate->st.wDay;
1478 /* Time */
1479 timePart *= 24.0;
1480 lpUdate->st.wHour = timePart;
1481 timePart -= lpUdate->st.wHour;
1482 timePart *= 60.0;
1483 lpUdate->st.wMinute = timePart;
1484 timePart -= lpUdate->st.wMinute;
1485 timePart *= 60.0;
1486 lpUdate->st.wSecond = timePart;
1487 timePart -= lpUdate->st.wSecond;
1488 lpUdate->st.wMilliseconds = 0;
1489 if (timePart > 0.5)
1491 /* Round the milliseconds, adjusting the time/date forward if needed */
1492 if (lpUdate->st.wSecond < 59)
1493 lpUdate->st.wSecond++;
1494 else
1496 lpUdate->st.wSecond = 0;
1497 if (lpUdate->st.wMinute < 59)
1498 lpUdate->st.wMinute++;
1499 else
1501 lpUdate->st.wMinute = 0;
1502 if (lpUdate->st.wHour < 23)
1503 lpUdate->st.wHour++;
1504 else
1506 lpUdate->st.wHour = 0;
1507 /* Roll over a whole day */
1508 if (++lpUdate->st.wDay > 28)
1509 VARIANT_RollUdate(lpUdate);
1514 return S_OK;
1517 #define GET_NUMBER_TEXT(fld,name) \
1518 buff[0] = 0; \
1519 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1520 WARN("buffer too small for " #fld "\n"); \
1521 else \
1522 if (buff[0]) lpChars->name = buff[0]; \
1523 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1525 /* Get the valid number characters for an lcid */
1526 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1528 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1529 static CRITICAL_SECTION csLastChars = { NULL, -1, 0, 0, 0, 0 };
1530 static VARIANT_NUMBER_CHARS lastChars;
1531 static LCID lastLcid = -1;
1532 static DWORD lastFlags = 0;
1533 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1534 WCHAR buff[4];
1536 /* To make caching thread-safe, a critical section is needed */
1537 EnterCriticalSection(&csLastChars);
1539 /* Asking for default locale entries is very expensive: It is a registry
1540 server call. So cache one locally, as Microsoft does it too */
1541 if(lcid == lastLcid && dwFlags == lastFlags)
1543 memcpy(lpChars, &lastChars, sizeof(defaultChars));
1544 LeaveCriticalSection(&csLastChars);
1545 return;
1548 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1549 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1550 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1551 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1552 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1553 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1554 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1556 /* Local currency symbols are often 2 characters */
1557 lpChars->cCurrencyLocal2 = '\0';
1558 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1560 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1561 case 2: lpChars->cCurrencyLocal = buff[0];
1562 break;
1563 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1565 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1566 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1568 memcpy(&lastChars, lpChars, sizeof(defaultChars));
1569 lastLcid = lcid;
1570 lastFlags = dwFlags;
1571 LeaveCriticalSection(&csLastChars);
1574 /* Number Parsing States */
1575 #define B_PROCESSING_EXPONENT 0x1
1576 #define B_NEGATIVE_EXPONENT 0x2
1577 #define B_EXPONENT_START 0x4
1578 #define B_INEXACT_ZEROS 0x8
1579 #define B_LEADING_ZERO 0x10
1580 #define B_PROCESSING_HEX 0x20
1581 #define B_PROCESSING_OCT 0x40
1583 /**********************************************************************
1584 * VarParseNumFromStr [OLEAUT32.46]
1586 * Parse a string containing a number into a NUMPARSE structure.
1588 * PARAMS
1589 * lpszStr [I] String to parse number from
1590 * lcid [I] Locale Id for the conversion
1591 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1592 * pNumprs [I/O] Destination for parsed number
1593 * rgbDig [O] Destination for digits read in
1595 * RETURNS
1596 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1597 * the number.
1598 * Failure: E_INVALIDARG, if any parameter is invalid.
1599 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1600 * incorrectly.
1601 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1603 * NOTES
1604 * pNumprs must have the following fields set:
1605 * cDig: Set to the size of rgbDig.
1606 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1607 * from "oleauto.h".
1609 * FIXME
1610 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1611 * numerals, so this has not been implemented.
1613 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1614 NUMPARSE *pNumprs, BYTE *rgbDig)
1616 VARIANT_NUMBER_CHARS chars;
1617 BYTE rgbTmp[1024];
1618 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1619 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1620 int cchUsed = 0;
1622 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1624 if (!pNumprs || !rgbDig)
1625 return E_INVALIDARG;
1627 if (pNumprs->cDig < iMaxDigits)
1628 iMaxDigits = pNumprs->cDig;
1630 pNumprs->cDig = 0;
1631 pNumprs->dwOutFlags = 0;
1632 pNumprs->cchUsed = 0;
1633 pNumprs->nBaseShift = 0;
1634 pNumprs->nPwr10 = 0;
1636 if (!lpszStr)
1637 return DISP_E_TYPEMISMATCH;
1639 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1641 /* First consume all the leading symbols and space from the string */
1642 while (1)
1644 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1646 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1649 cchUsed++;
1650 lpszStr++;
1651 } while (isspaceW(*lpszStr));
1653 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1654 *lpszStr == chars.cPositiveSymbol &&
1655 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1657 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1658 cchUsed++;
1659 lpszStr++;
1661 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1662 *lpszStr == chars.cNegativeSymbol &&
1663 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1665 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1666 cchUsed++;
1667 lpszStr++;
1669 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1670 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1671 *lpszStr == chars.cCurrencyLocal &&
1672 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1674 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1675 cchUsed++;
1676 lpszStr++;
1677 /* Only accept currency characters */
1678 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1679 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1681 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1682 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1684 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1685 cchUsed++;
1686 lpszStr++;
1688 else
1689 break;
1692 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1694 /* Only accept non-currency characters */
1695 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1696 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1699 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1700 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1702 dwState |= B_PROCESSING_HEX;
1703 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1704 cchUsed=cchUsed+2;
1705 lpszStr=lpszStr+2;
1707 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1708 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1710 dwState |= B_PROCESSING_OCT;
1711 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1712 cchUsed=cchUsed+2;
1713 lpszStr=lpszStr+2;
1716 /* Strip Leading zeros */
1717 while (*lpszStr == '0')
1719 dwState |= B_LEADING_ZERO;
1720 cchUsed++;
1721 lpszStr++;
1724 while (*lpszStr)
1726 if (isdigitW(*lpszStr))
1728 if (dwState & B_PROCESSING_EXPONENT)
1730 int exponentSize = 0;
1731 if (dwState & B_EXPONENT_START)
1733 if (!isdigitW(*lpszStr))
1734 break; /* No exponent digits - invalid */
1735 while (*lpszStr == '0')
1737 /* Skip leading zero's in the exponent */
1738 cchUsed++;
1739 lpszStr++;
1743 while (isdigitW(*lpszStr))
1745 exponentSize *= 10;
1746 exponentSize += *lpszStr - '0';
1747 cchUsed++;
1748 lpszStr++;
1750 if (dwState & B_NEGATIVE_EXPONENT)
1751 exponentSize = -exponentSize;
1752 /* Add the exponent into the powers of 10 */
1753 pNumprs->nPwr10 += exponentSize;
1754 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1755 lpszStr--; /* back up to allow processing of next char */
1757 else
1759 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1760 && !(dwState & B_PROCESSING_OCT))
1762 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1764 if (*lpszStr != '0')
1765 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1767 /* This digit can't be represented, but count it in nPwr10 */
1768 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1769 pNumprs->nPwr10--;
1770 else
1771 pNumprs->nPwr10++;
1773 else
1775 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1776 return DISP_E_TYPEMISMATCH;
1779 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1780 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1782 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1784 pNumprs->cDig++;
1785 cchUsed++;
1788 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1790 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1791 cchUsed++;
1793 else if (*lpszStr == chars.cDecimalPoint &&
1794 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1795 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1797 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1798 cchUsed++;
1800 /* If we have no digits so far, skip leading zeros */
1801 if (!pNumprs->cDig)
1803 while (lpszStr[1] == '0')
1805 dwState |= B_LEADING_ZERO;
1806 cchUsed++;
1807 lpszStr++;
1808 pNumprs->nPwr10--;
1812 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1813 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1814 dwState & B_PROCESSING_HEX)
1816 if (pNumprs->cDig >= iMaxDigits)
1818 return DISP_E_OVERFLOW;
1820 else
1822 if (*lpszStr >= 'a')
1823 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1824 else
1825 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1827 pNumprs->cDig++;
1828 cchUsed++;
1830 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1831 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1832 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1834 dwState |= B_PROCESSING_EXPONENT;
1835 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1836 cchUsed++;
1838 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1840 cchUsed++; /* Ignore positive exponent */
1842 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1844 dwState |= B_NEGATIVE_EXPONENT;
1845 cchUsed++;
1847 else
1848 break; /* Stop at an unrecognised character */
1850 lpszStr++;
1853 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1855 /* Ensure a 0 on its own gets stored */
1856 pNumprs->cDig = 1;
1857 rgbTmp[0] = 0;
1860 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1862 pNumprs->cchUsed = cchUsed;
1863 WARN("didn't completely parse exponent\n");
1864 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1867 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1869 if (dwState & B_INEXACT_ZEROS)
1870 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1871 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1873 /* copy all of the digits into the output digit buffer */
1874 /* this is exactly what windows does although it also returns */
1875 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1876 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1878 if (dwState & B_PROCESSING_HEX) {
1879 /* hex numbers have always the same format */
1880 pNumprs->nPwr10=0;
1881 pNumprs->nBaseShift=4;
1882 } else {
1883 if (dwState & B_PROCESSING_OCT) {
1884 /* oct numbers have always the same format */
1885 pNumprs->nPwr10=0;
1886 pNumprs->nBaseShift=3;
1887 } else {
1888 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1890 pNumprs->nPwr10++;
1891 pNumprs->cDig--;
1895 } else
1897 /* Remove trailing zeros from the last (whole number or decimal) part */
1898 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1900 pNumprs->nPwr10++;
1901 pNumprs->cDig--;
1905 if (pNumprs->cDig <= iMaxDigits)
1906 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1907 else
1908 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1910 /* Copy the digits we processed into rgbDig */
1911 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1913 /* Consume any trailing symbols and space */
1914 while (1)
1916 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1918 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1921 cchUsed++;
1922 lpszStr++;
1923 } while (isspaceW(*lpszStr));
1925 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1926 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1927 *lpszStr == chars.cPositiveSymbol)
1929 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1930 cchUsed++;
1931 lpszStr++;
1933 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1934 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1935 *lpszStr == chars.cNegativeSymbol)
1937 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1938 cchUsed++;
1939 lpszStr++;
1941 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1942 pNumprs->dwOutFlags & NUMPRS_PARENS)
1944 cchUsed++;
1945 lpszStr++;
1946 pNumprs->dwOutFlags |= NUMPRS_NEG;
1948 else
1949 break;
1952 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1954 pNumprs->cchUsed = cchUsed;
1955 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1958 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1959 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1961 if (!pNumprs->cDig)
1962 return DISP_E_TYPEMISMATCH; /* No Number found */
1964 pNumprs->cchUsed = cchUsed;
1965 return S_OK;
1968 /* VTBIT flags indicating an integer value */
1969 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1970 /* VTBIT flags indicating a real number value */
1971 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1973 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1974 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1975 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1976 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1978 /**********************************************************************
1979 * VarNumFromParseNum [OLEAUT32.47]
1981 * Convert a NUMPARSE structure into a numeric Variant type.
1983 * PARAMS
1984 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1985 * rgbDig [I] Source for the numbers digits
1986 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1987 * pVarDst [O] Destination for the converted Variant value.
1989 * RETURNS
1990 * Success: S_OK. pVarDst contains the converted value.
1991 * Failure: E_INVALIDARG, if any parameter is invalid.
1992 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1994 * NOTES
1995 * - The smallest favoured type present in dwVtBits that can represent the
1996 * number in pNumprs without losing precision is used.
1997 * - Signed types are preferred over unsigned types of the same size.
1998 * - Preferred types in order are: integer, float, double, currency then decimal.
1999 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
2000 * for details of the rounding method.
2001 * - pVarDst is not cleared before the result is stored in it.
2002 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
2003 * design?): If some other VTBIT's for integers are specified together
2004 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
2005 * the number to the smallest requested integer truncating this way the
2006 * number. Wine doesn't implement this "feature" (yet?).
2008 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
2009 ULONG dwVtBits, VARIANT *pVarDst)
2011 /* Scale factors and limits for double arithmetic */
2012 static const double dblMultipliers[11] = {
2013 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2014 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2016 static const double dblMinimums[11] = {
2017 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
2018 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
2019 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
2021 static const double dblMaximums[11] = {
2022 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
2023 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
2024 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
2027 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
2029 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
2031 if (pNumprs->nBaseShift)
2033 /* nBaseShift indicates a hex or octal number */
2034 ULONG64 ul64 = 0;
2035 LONG64 l64;
2036 int i;
2038 /* Convert the hex or octal number string into a UI64 */
2039 for (i = 0; i < pNumprs->cDig; i++)
2041 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
2043 TRACE("Overflow multiplying digits\n");
2044 return DISP_E_OVERFLOW;
2046 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
2049 /* also make a negative representation */
2050 l64=-ul64;
2052 /* Try signed and unsigned types in size order */
2053 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
2055 V_VT(pVarDst) = VT_I1;
2056 V_I1(pVarDst) = ul64;
2057 return S_OK;
2059 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2061 V_VT(pVarDst) = VT_UI1;
2062 V_UI1(pVarDst) = ul64;
2063 return S_OK;
2065 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2067 V_VT(pVarDst) = VT_I2;
2068 V_I2(pVarDst) = ul64;
2069 return S_OK;
2071 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2073 V_VT(pVarDst) = VT_UI2;
2074 V_UI2(pVarDst) = ul64;
2075 return S_OK;
2077 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2079 V_VT(pVarDst) = VT_I4;
2080 V_I4(pVarDst) = ul64;
2081 return S_OK;
2083 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2085 V_VT(pVarDst) = VT_UI4;
2086 V_UI4(pVarDst) = ul64;
2087 return S_OK;
2089 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2091 V_VT(pVarDst) = VT_I8;
2092 V_I8(pVarDst) = ul64;
2093 return S_OK;
2095 else if (dwVtBits & VTBIT_UI8)
2097 V_VT(pVarDst) = VT_UI8;
2098 V_UI8(pVarDst) = ul64;
2099 return S_OK;
2101 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2103 V_VT(pVarDst) = VT_DECIMAL;
2104 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2105 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2106 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2107 return S_OK;
2109 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2111 V_VT(pVarDst) = VT_R4;
2112 if (ul64 <= I4_MAX)
2113 V_R4(pVarDst) = ul64;
2114 else
2115 V_R4(pVarDst) = l64;
2116 return S_OK;
2118 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2120 V_VT(pVarDst) = VT_R8;
2121 if (ul64 <= I4_MAX)
2122 V_R8(pVarDst) = ul64;
2123 else
2124 V_R8(pVarDst) = l64;
2125 return S_OK;
2128 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2129 return DISP_E_OVERFLOW;
2132 /* Count the number of relevant fractional and whole digits stored,
2133 * And compute the divisor/multiplier to scale the number by.
2135 if (pNumprs->nPwr10 < 0)
2137 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2139 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2140 wholeNumberDigits = 0;
2141 fractionalDigits = pNumprs->cDig;
2142 divisor10 = -pNumprs->nPwr10;
2144 else
2146 /* An exactly represented real number e.g. 1.024 */
2147 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2148 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2149 divisor10 = pNumprs->cDig - wholeNumberDigits;
2152 else if (pNumprs->nPwr10 == 0)
2154 /* An exactly represented whole number e.g. 1024 */
2155 wholeNumberDigits = pNumprs->cDig;
2156 fractionalDigits = 0;
2158 else /* pNumprs->nPwr10 > 0 */
2160 /* A whole number followed by nPwr10 0's e.g. 102400 */
2161 wholeNumberDigits = pNumprs->cDig;
2162 fractionalDigits = 0;
2163 multiplier10 = pNumprs->nPwr10;
2166 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2167 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2168 multiplier10, divisor10);
2170 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2171 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2173 /* We have one or more integer output choices, and either:
2174 * 1) An integer input value, or
2175 * 2) A real number input value but no floating output choices.
2176 * Alternately, we have a DECIMAL output available and an integer input.
2178 * So, place the integer value into pVarDst, using the smallest type
2179 * possible and preferring signed over unsigned types.
2181 BOOL bOverflow = FALSE, bNegative;
2182 ULONG64 ul64 = 0;
2183 int i;
2185 /* Convert the integer part of the number into a UI8 */
2186 for (i = 0; i < wholeNumberDigits; i++)
2188 if (ul64 > UI8_MAX / 10 || (ul64 == UI8_MAX / 10 && rgbDig[i] > UI8_MAX % 10))
2190 TRACE("Overflow multiplying digits\n");
2191 bOverflow = TRUE;
2192 break;
2194 ul64 = ul64 * 10 + rgbDig[i];
2197 /* Account for the scale of the number */
2198 if (!bOverflow && multiplier10)
2200 for (i = 0; i < multiplier10; i++)
2202 if (ul64 > (UI8_MAX / 10))
2204 TRACE("Overflow scaling number\n");
2205 bOverflow = TRUE;
2206 break;
2208 ul64 = ul64 * 10;
2212 /* If we have any fractional digits, round the value.
2213 * Note we don't have to do this if divisor10 is < 1,
2214 * because this means the fractional part must be < 0.5
2216 if (!bOverflow && fractionalDigits && divisor10 > 0)
2218 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2219 BOOL bAdjust = FALSE;
2221 TRACE("first decimal value is %d\n", *fracDig);
2223 if (*fracDig > 5)
2224 bAdjust = TRUE; /* > 0.5 */
2225 else if (*fracDig == 5)
2227 for (i = 1; i < fractionalDigits; i++)
2229 if (fracDig[i])
2231 bAdjust = TRUE; /* > 0.5 */
2232 break;
2235 /* If exactly 0.5, round only odd values */
2236 if (i == fractionalDigits && (ul64 & 1))
2237 bAdjust = TRUE;
2240 if (bAdjust)
2242 if (ul64 == UI8_MAX)
2244 TRACE("Overflow after rounding\n");
2245 bOverflow = TRUE;
2247 ul64++;
2251 /* Zero is not a negative number */
2252 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2254 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2256 /* For negative integers, try the signed types in size order */
2257 if (!bOverflow && bNegative)
2259 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2261 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2263 V_VT(pVarDst) = VT_I1;
2264 V_I1(pVarDst) = -ul64;
2265 return S_OK;
2267 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2269 V_VT(pVarDst) = VT_I2;
2270 V_I2(pVarDst) = -ul64;
2271 return S_OK;
2273 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2275 V_VT(pVarDst) = VT_I4;
2276 V_I4(pVarDst) = -ul64;
2277 return S_OK;
2279 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2281 V_VT(pVarDst) = VT_I8;
2282 V_I8(pVarDst) = -ul64;
2283 return S_OK;
2285 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2287 /* Decimal is only output choice left - fast path */
2288 V_VT(pVarDst) = VT_DECIMAL;
2289 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2290 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2291 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2292 return S_OK;
2296 else if (!bOverflow)
2298 /* For positive integers, try signed then unsigned types in size order */
2299 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2301 V_VT(pVarDst) = VT_I1;
2302 V_I1(pVarDst) = ul64;
2303 return S_OK;
2305 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2307 V_VT(pVarDst) = VT_UI1;
2308 V_UI1(pVarDst) = ul64;
2309 return S_OK;
2311 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2313 V_VT(pVarDst) = VT_I2;
2314 V_I2(pVarDst) = ul64;
2315 return S_OK;
2317 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2319 V_VT(pVarDst) = VT_UI2;
2320 V_UI2(pVarDst) = ul64;
2321 return S_OK;
2323 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2325 V_VT(pVarDst) = VT_I4;
2326 V_I4(pVarDst) = ul64;
2327 return S_OK;
2329 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2331 V_VT(pVarDst) = VT_UI4;
2332 V_UI4(pVarDst) = ul64;
2333 return S_OK;
2335 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2337 V_VT(pVarDst) = VT_I8;
2338 V_I8(pVarDst) = ul64;
2339 return S_OK;
2341 else if (dwVtBits & VTBIT_UI8)
2343 V_VT(pVarDst) = VT_UI8;
2344 V_UI8(pVarDst) = ul64;
2345 return S_OK;
2347 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2349 /* Decimal is only output choice left - fast path */
2350 V_VT(pVarDst) = VT_DECIMAL;
2351 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2352 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2353 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2354 return S_OK;
2359 if (dwVtBits & REAL_VTBITS)
2361 /* Try to put the number into a float or real */
2362 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2363 double whole = 0.0;
2364 int i;
2366 /* Convert the number into a double */
2367 for (i = 0; i < pNumprs->cDig; i++)
2368 whole = whole * 10.0 + rgbDig[i];
2370 TRACE("Whole double value is %16.16g\n", whole);
2372 /* Account for the scale */
2373 while (multiplier10 > 10)
2375 if (whole > dblMaximums[10])
2377 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2378 bOverflow = TRUE;
2379 break;
2381 whole = whole * dblMultipliers[10];
2382 multiplier10 -= 10;
2384 if (multiplier10 && !bOverflow)
2386 if (whole > dblMaximums[multiplier10])
2388 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2389 bOverflow = TRUE;
2391 else
2392 whole = whole * dblMultipliers[multiplier10];
2395 if (!bOverflow)
2396 TRACE("Scaled double value is %16.16g\n", whole);
2398 while (divisor10 > 10 && !bOverflow)
2400 if (whole < dblMinimums[10] && whole != 0)
2402 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2403 bOverflow = TRUE;
2404 break;
2406 whole = whole / dblMultipliers[10];
2407 divisor10 -= 10;
2409 if (divisor10 && !bOverflow)
2411 if (whole < dblMinimums[divisor10] && whole != 0)
2413 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2414 bOverflow = TRUE;
2416 else
2417 whole = whole / dblMultipliers[divisor10];
2419 if (!bOverflow)
2420 TRACE("Final double value is %16.16g\n", whole);
2422 if (dwVtBits & VTBIT_R4 &&
2423 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2425 TRACE("Set R4 to final value\n");
2426 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2427 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2428 return S_OK;
2431 if (dwVtBits & VTBIT_R8)
2433 TRACE("Set R8 to final value\n");
2434 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2435 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2436 return S_OK;
2439 if (dwVtBits & VTBIT_CY)
2441 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2443 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2444 TRACE("Set CY to final value\n");
2445 return S_OK;
2447 TRACE("Value Overflows CY\n");
2451 if (dwVtBits & VTBIT_DECIMAL)
2453 int i;
2454 ULONG carry;
2455 ULONG64 tmp;
2456 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2458 DECIMAL_SETZERO(*pDec);
2459 DEC_LO32(pDec) = 0;
2461 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2462 DEC_SIGN(pDec) = DECIMAL_NEG;
2463 else
2464 DEC_SIGN(pDec) = DECIMAL_POS;
2466 /* Factor the significant digits */
2467 for (i = 0; i < pNumprs->cDig; i++)
2469 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2470 carry = (ULONG)(tmp >> 32);
2471 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2472 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2473 carry = (ULONG)(tmp >> 32);
2474 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2475 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2476 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2478 if (tmp >> 32 & UI4_MAX)
2480 VarNumFromParseNum_DecOverflow:
2481 TRACE("Overflow\n");
2482 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2483 return DISP_E_OVERFLOW;
2487 /* Account for the scale of the number */
2488 while (multiplier10 > 0)
2490 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2491 carry = (ULONG)(tmp >> 32);
2492 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2493 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2494 carry = (ULONG)(tmp >> 32);
2495 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2496 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2497 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2499 if (tmp >> 32 & UI4_MAX)
2500 goto VarNumFromParseNum_DecOverflow;
2501 multiplier10--;
2503 DEC_SCALE(pDec) = divisor10;
2505 V_VT(pVarDst) = VT_DECIMAL;
2506 return S_OK;
2508 return DISP_E_OVERFLOW; /* No more output choices */
2511 /**********************************************************************
2512 * VarCat [OLEAUT32.318]
2514 * Concatenates one variant onto another.
2516 * PARAMS
2517 * left [I] First variant
2518 * right [I] Second variant
2519 * result [O] Result variant
2521 * RETURNS
2522 * Success: S_OK.
2523 * Failure: An HRESULT error code indicating the error.
2525 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2527 VARTYPE leftvt,rightvt,resultvt;
2528 HRESULT hres;
2529 static WCHAR str_true[32];
2530 static WCHAR str_false[32];
2531 static const WCHAR sz_empty[] = {'\0'};
2532 leftvt = V_VT(left);
2533 rightvt = V_VT(right);
2535 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2536 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2538 if (!str_true[0]) {
2539 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_FALSE, str_false);
2540 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_TRUE, str_true);
2543 /* when both left and right are NULL the result is NULL */
2544 if (leftvt == VT_NULL && rightvt == VT_NULL)
2546 V_VT(out) = VT_NULL;
2547 return S_OK;
2550 hres = S_OK;
2551 resultvt = VT_EMPTY;
2553 /* There are many special case for errors and return types */
2554 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2555 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2556 hres = DISP_E_TYPEMISMATCH;
2557 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2558 leftvt == VT_R4 || leftvt == VT_R8 ||
2559 leftvt == VT_CY || leftvt == VT_BOOL ||
2560 leftvt == VT_BSTR || leftvt == VT_I1 ||
2561 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2562 leftvt == VT_UI4 || leftvt == VT_I8 ||
2563 leftvt == VT_UI8 || leftvt == VT_INT ||
2564 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2565 leftvt == VT_NULL || leftvt == VT_DATE ||
2566 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2568 (rightvt == VT_I2 || rightvt == VT_I4 ||
2569 rightvt == VT_R4 || rightvt == VT_R8 ||
2570 rightvt == VT_CY || rightvt == VT_BOOL ||
2571 rightvt == VT_BSTR || rightvt == VT_I1 ||
2572 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2573 rightvt == VT_UI4 || rightvt == VT_I8 ||
2574 rightvt == VT_UI8 || rightvt == VT_INT ||
2575 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2576 rightvt == VT_NULL || rightvt == VT_DATE ||
2577 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2578 resultvt = VT_BSTR;
2579 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2580 hres = DISP_E_TYPEMISMATCH;
2581 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2582 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2583 hres = DISP_E_TYPEMISMATCH;
2584 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2585 rightvt == VT_DECIMAL)
2586 hres = DISP_E_BADVARTYPE;
2587 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2588 hres = DISP_E_TYPEMISMATCH;
2589 else if (leftvt == VT_VARIANT)
2590 hres = DISP_E_TYPEMISMATCH;
2591 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2592 leftvt == VT_NULL || leftvt == VT_I2 ||
2593 leftvt == VT_I4 || leftvt == VT_R4 ||
2594 leftvt == VT_R8 || leftvt == VT_CY ||
2595 leftvt == VT_DATE || leftvt == VT_BSTR ||
2596 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2597 leftvt == VT_I1 || leftvt == VT_UI1 ||
2598 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2599 leftvt == VT_I8 || leftvt == VT_UI8 ||
2600 leftvt == VT_INT || leftvt == VT_UINT))
2601 hres = DISP_E_TYPEMISMATCH;
2602 else
2603 hres = DISP_E_BADVARTYPE;
2605 /* if result type is not S_OK, then no need to go further */
2606 if (hres != S_OK)
2608 V_VT(out) = resultvt;
2609 return hres;
2611 /* Else proceed with formatting inputs to strings */
2612 else
2614 VARIANT bstrvar_left, bstrvar_right;
2615 V_VT(out) = VT_BSTR;
2617 VariantInit(&bstrvar_left);
2618 VariantInit(&bstrvar_right);
2620 /* Convert left side variant to string */
2621 if (leftvt != VT_BSTR)
2623 if (leftvt == VT_BOOL)
2625 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2626 V_VT(&bstrvar_left) = VT_BSTR;
2627 if (V_BOOL(left) == TRUE)
2628 V_BSTR(&bstrvar_left) = SysAllocString(str_true);
2629 else
2630 V_BSTR(&bstrvar_left) = SysAllocString(str_false);
2632 /* Fill with empty string for later concat with right side */
2633 else if (leftvt == VT_NULL)
2635 V_VT(&bstrvar_left) = VT_BSTR;
2636 V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
2638 else
2640 hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
2641 if (hres != S_OK) {
2642 VariantClear(&bstrvar_left);
2643 VariantClear(&bstrvar_right);
2644 if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
2645 rightvt == VT_NULL || rightvt == VT_I2 ||
2646 rightvt == VT_I4 || rightvt == VT_R4 ||
2647 rightvt == VT_R8 || rightvt == VT_CY ||
2648 rightvt == VT_DATE || rightvt == VT_BSTR ||
2649 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
2650 rightvt == VT_I1 || rightvt == VT_UI1 ||
2651 rightvt == VT_UI2 || rightvt == VT_UI4 ||
2652 rightvt == VT_I8 || rightvt == VT_UI8 ||
2653 rightvt == VT_INT || rightvt == VT_UINT))
2654 return DISP_E_BADVARTYPE;
2655 return hres;
2660 /* convert right side variant to string */
2661 if (rightvt != VT_BSTR)
2663 if (rightvt == VT_BOOL)
2665 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2666 V_VT(&bstrvar_right) = VT_BSTR;
2667 if (V_BOOL(right) == TRUE)
2668 V_BSTR(&bstrvar_right) = SysAllocString(str_true);
2669 else
2670 V_BSTR(&bstrvar_right) = SysAllocString(str_false);
2672 /* Fill with empty string for later concat with right side */
2673 else if (rightvt == VT_NULL)
2675 V_VT(&bstrvar_right) = VT_BSTR;
2676 V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
2678 else
2680 hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
2681 if (hres != S_OK) {
2682 VariantClear(&bstrvar_left);
2683 VariantClear(&bstrvar_right);
2684 if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
2685 leftvt == VT_NULL || leftvt == VT_I2 ||
2686 leftvt == VT_I4 || leftvt == VT_R4 ||
2687 leftvt == VT_R8 || leftvt == VT_CY ||
2688 leftvt == VT_DATE || leftvt == VT_BSTR ||
2689 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2690 leftvt == VT_I1 || leftvt == VT_UI1 ||
2691 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2692 leftvt == VT_I8 || leftvt == VT_UI8 ||
2693 leftvt == VT_INT || leftvt == VT_UINT))
2694 return DISP_E_BADVARTYPE;
2695 return hres;
2700 /* Concat the resulting strings together */
2701 if (leftvt == VT_BSTR && rightvt == VT_BSTR)
2702 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2703 else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
2704 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2705 else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
2706 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
2707 else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
2708 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2710 VariantClear(&bstrvar_left);
2711 VariantClear(&bstrvar_right);
2712 return S_OK;
2717 /* Wrapper around VariantChangeTypeEx() which permits changing a
2718 variant with VT_RESERVED flag set. Needed by VarCmp. */
2719 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2720 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2722 HRESULT res;
2723 VARTYPE flags;
2725 flags = V_VT(pvargSrc) & ~VT_TYPEMASK;
2726 V_VT(pvargSrc) &= ~VT_RESERVED;
2727 res = VariantChangeTypeEx(pvargDest,pvargSrc,lcid,wFlags,vt);
2728 V_VT(pvargSrc) |= flags;
2730 return res;
2733 /**********************************************************************
2734 * VarCmp [OLEAUT32.176]
2736 * Compare two variants.
2738 * PARAMS
2739 * left [I] First variant
2740 * right [I] Second variant
2741 * lcid [I] LCID (locale identifier) for the comparison
2742 * flags [I] Flags to be used in the comparison:
2743 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2744 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2746 * RETURNS
2747 * VARCMP_LT: left variant is less than right variant.
2748 * VARCMP_EQ: input variants are equal.
2749 * VARCMP_GT: left variant is greater than right variant.
2750 * VARCMP_NULL: either one of the input variants is NULL.
2751 * Failure: An HRESULT error code indicating the error.
2753 * NOTES
2754 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2755 * UI8 and UINT as input variants. INT is accepted only as left variant.
2757 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2758 * an ERROR variant will trigger an error.
2760 * Both input variants can have VT_RESERVED flag set which is ignored
2761 * unless one and only one of the variants is a BSTR and the other one
2762 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2763 * different meaning:
2764 * - BSTR and other: BSTR is always greater than the other variant.
2765 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2766 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2767 * comparison will take place else the BSTR is always greater.
2768 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2769 * variant is ignored and the return value depends only on the sign
2770 * of the BSTR if it is a number else the BSTR is always greater. A
2771 * positive BSTR is greater, a negative one is smaller than the other
2772 * variant.
2774 * SEE
2775 * VarBstrCmp for the lcid and flags usage.
2777 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2779 VARTYPE lvt, rvt, vt;
2780 VARIANT rv,lv;
2781 DWORD xmask;
2782 HRESULT rc;
2784 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%08x)\n", left, debugstr_VT(left),
2785 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2787 lvt = V_VT(left) & VT_TYPEMASK;
2788 rvt = V_VT(right) & VT_TYPEMASK;
2789 xmask = (1 << lvt) | (1 << rvt);
2791 /* If we have any flag set except VT_RESERVED bail out.
2792 Same for the left input variant type > VT_INT and for the
2793 right input variant type > VT_I8. Yes, VT_INT is only supported
2794 as left variant. Go figure */
2795 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2796 lvt > VT_INT || rvt > VT_I8) {
2797 return DISP_E_BADVARTYPE;
2800 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2801 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2802 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2803 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2804 return DISP_E_TYPEMISMATCH;
2806 /* If both variants are VT_ERROR return VARCMP_EQ */
2807 if (xmask == VTBIT_ERROR)
2808 return VARCMP_EQ;
2809 else if (xmask & VTBIT_ERROR)
2810 return DISP_E_TYPEMISMATCH;
2812 if (xmask & VTBIT_NULL)
2813 return VARCMP_NULL;
2815 VariantInit(&lv);
2816 VariantInit(&rv);
2818 /* Two BSTRs, ignore VT_RESERVED */
2819 if (xmask == VTBIT_BSTR)
2820 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2822 /* A BSTR and an other variant; we have to take care of VT_RESERVED */
2823 if (xmask & VTBIT_BSTR) {
2824 VARIANT *bstrv, *nonbv;
2825 VARTYPE nonbvt;
2826 int swap = 0;
2828 /* Swap the variants so the BSTR is always on the left */
2829 if (lvt == VT_BSTR) {
2830 bstrv = left;
2831 nonbv = right;
2832 nonbvt = rvt;
2833 } else {
2834 swap = 1;
2835 bstrv = right;
2836 nonbv = left;
2837 nonbvt = lvt;
2840 /* BSTR and EMPTY: ignore VT_RESERVED */
2841 if (nonbvt == VT_EMPTY)
2842 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2843 else {
2844 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2845 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2847 if (!breserv && !nreserv)
2848 /* No VT_RESERVED set ==> BSTR always greater */
2849 rc = VARCMP_GT;
2850 else if (breserv && !nreserv) {
2851 /* BSTR has VT_RESERVED set. Do a string comparison */
2852 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2853 if (FAILED(rc))
2854 return rc;
2855 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2856 VariantClear(&rv);
2857 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2858 /* Non NULL nor empty BSTR */
2859 /* If the BSTR is not a number the BSTR is greater */
2860 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2861 if (FAILED(rc))
2862 rc = VARCMP_GT;
2863 else if (breserv && nreserv)
2864 /* FIXME: This is strange: with both VT_RESERVED set it
2865 looks like the result depends only on the sign of
2866 the BSTR number */
2867 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2868 else
2869 /* Numeric comparison, will be handled below.
2870 VARCMP_NULL used only to break out. */
2871 rc = VARCMP_NULL;
2872 VariantClear(&lv);
2873 VariantClear(&rv);
2874 } else
2875 /* Empty or NULL BSTR */
2876 rc = VARCMP_GT;
2878 /* Fixup the return code if we swapped left and right */
2879 if (swap) {
2880 if (rc == VARCMP_GT)
2881 rc = VARCMP_LT;
2882 else if (rc == VARCMP_LT)
2883 rc = VARCMP_GT;
2885 if (rc != VARCMP_NULL)
2886 return rc;
2889 if (xmask & VTBIT_DECIMAL)
2890 vt = VT_DECIMAL;
2891 else if (xmask & VTBIT_BSTR)
2892 vt = VT_R8;
2893 else if (xmask & VTBIT_R4)
2894 vt = VT_R4;
2895 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2896 vt = VT_R8;
2897 else if (xmask & VTBIT_CY)
2898 vt = VT_CY;
2899 else
2900 /* default to I8 */
2901 vt = VT_I8;
2903 /* Coerce the variants */
2904 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2905 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2906 /* Overflow, change to R8 */
2907 vt = VT_R8;
2908 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2910 if (FAILED(rc))
2911 return rc;
2912 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2913 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2914 /* Overflow, change to R8 */
2915 vt = VT_R8;
2916 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2917 if (FAILED(rc))
2918 return rc;
2919 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2921 if (FAILED(rc))
2922 return rc;
2924 #define _VARCMP(a,b) \
2925 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2927 switch (vt) {
2928 case VT_CY:
2929 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2930 case VT_DECIMAL:
2931 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2932 case VT_I8:
2933 return _VARCMP(V_I8(&lv), V_I8(&rv));
2934 case VT_R4:
2935 return _VARCMP(V_R4(&lv), V_R4(&rv));
2936 case VT_R8:
2937 return _VARCMP(V_R8(&lv), V_R8(&rv));
2938 default:
2939 /* We should never get here */
2940 return E_FAIL;
2942 #undef _VARCMP
2945 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
2947 HRESULT hres;
2948 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
2950 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
2951 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
2952 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
2953 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
2954 NULL, NULL);
2955 } else {
2956 hres = DISP_E_TYPEMISMATCH;
2958 return hres;
2961 /**********************************************************************
2962 * VarAnd [OLEAUT32.142]
2964 * Computes the logical AND of two variants.
2966 * PARAMS
2967 * left [I] First variant
2968 * right [I] Second variant
2969 * result [O] Result variant
2971 * RETURNS
2972 * Success: S_OK.
2973 * Failure: An HRESULT error code indicating the error.
2975 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2977 HRESULT hres = S_OK;
2978 VARTYPE resvt = VT_EMPTY;
2979 VARTYPE leftvt,rightvt;
2980 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2981 VARIANT varLeft, varRight;
2982 VARIANT tempLeft, tempRight;
2984 VariantInit(&varLeft);
2985 VariantInit(&varRight);
2986 VariantInit(&tempLeft);
2987 VariantInit(&tempRight);
2989 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2990 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2992 /* Handle VT_DISPATCH by storing and taking address of returned value */
2993 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2995 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2996 if (FAILED(hres)) goto VarAnd_Exit;
2997 left = &tempLeft;
2999 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3001 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3002 if (FAILED(hres)) goto VarAnd_Exit;
3003 right = &tempRight;
3006 leftvt = V_VT(left)&VT_TYPEMASK;
3007 rightvt = V_VT(right)&VT_TYPEMASK;
3008 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3009 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3011 if (leftExtraFlags != rightExtraFlags)
3013 hres = DISP_E_BADVARTYPE;
3014 goto VarAnd_Exit;
3016 ExtraFlags = leftExtraFlags;
3018 /* Native VarAnd always returns an error when using extra
3019 * flags or if the variant combination is I8 and INT.
3021 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
3022 (leftvt == VT_INT && rightvt == VT_I8) ||
3023 ExtraFlags != 0)
3025 hres = DISP_E_BADVARTYPE;
3026 goto VarAnd_Exit;
3029 /* Determine return type */
3030 else if (leftvt == VT_I8 || rightvt == VT_I8)
3031 resvt = VT_I8;
3032 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
3033 leftvt == VT_UINT || rightvt == VT_UINT ||
3034 leftvt == VT_INT || rightvt == VT_INT ||
3035 leftvt == VT_UINT || rightvt == VT_UINT ||
3036 leftvt == VT_R4 || rightvt == VT_R4 ||
3037 leftvt == VT_R8 || rightvt == VT_R8 ||
3038 leftvt == VT_CY || rightvt == VT_CY ||
3039 leftvt == VT_DATE || rightvt == VT_DATE ||
3040 leftvt == VT_I1 || rightvt == VT_I1 ||
3041 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3042 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3043 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3044 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3045 resvt = VT_I4;
3046 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
3047 leftvt == VT_I2 || rightvt == VT_I2 ||
3048 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
3049 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
3050 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
3051 (leftvt == VT_UI1 && rightvt == VT_UI1))
3052 resvt = VT_UI1;
3053 else
3054 resvt = VT_I2;
3055 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
3056 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3057 resvt = VT_BOOL;
3058 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
3059 leftvt == VT_BSTR || rightvt == VT_BSTR)
3060 resvt = VT_NULL;
3061 else
3063 hres = DISP_E_BADVARTYPE;
3064 goto VarAnd_Exit;
3067 if (leftvt == VT_NULL || rightvt == VT_NULL)
3070 * Special cases for when left variant is VT_NULL
3071 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
3073 if (leftvt == VT_NULL)
3075 VARIANT_BOOL b;
3076 switch(rightvt)
3078 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3079 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3080 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3081 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3082 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3083 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3084 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3085 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3086 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3087 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3088 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3089 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3090 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3091 case VT_CY:
3092 if(V_CY(right).int64)
3093 resvt = VT_NULL;
3094 break;
3095 case VT_DECIMAL:
3096 if (DEC_HI32(&V_DECIMAL(right)) ||
3097 DEC_LO64(&V_DECIMAL(right)))
3098 resvt = VT_NULL;
3099 break;
3100 case VT_BSTR:
3101 hres = VarBoolFromStr(V_BSTR(right),
3102 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3103 if (FAILED(hres))
3104 return hres;
3105 else if (b)
3106 V_VT(result) = VT_NULL;
3107 else
3109 V_VT(result) = VT_BOOL;
3110 V_BOOL(result) = b;
3112 goto VarAnd_Exit;
3115 V_VT(result) = resvt;
3116 goto VarAnd_Exit;
3119 hres = VariantCopy(&varLeft, left);
3120 if (FAILED(hres)) goto VarAnd_Exit;
3122 hres = VariantCopy(&varRight, right);
3123 if (FAILED(hres)) goto VarAnd_Exit;
3125 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3126 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3127 else
3129 double d;
3131 if (V_VT(&varLeft) == VT_BSTR &&
3132 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3133 LOCALE_USER_DEFAULT, 0, &d)))
3134 hres = VariantChangeType(&varLeft,&varLeft,
3135 VARIANT_LOCALBOOL, VT_BOOL);
3136 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3137 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3138 if (FAILED(hres)) goto VarAnd_Exit;
3141 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3142 V_VT(&varRight) = VT_I4; /* Don't overflow */
3143 else
3145 double d;
3147 if (V_VT(&varRight) == VT_BSTR &&
3148 FAILED(VarR8FromStr(V_BSTR(&varRight),
3149 LOCALE_USER_DEFAULT, 0, &d)))
3150 hres = VariantChangeType(&varRight, &varRight,
3151 VARIANT_LOCALBOOL, VT_BOOL);
3152 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3153 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3154 if (FAILED(hres)) goto VarAnd_Exit;
3157 V_VT(result) = resvt;
3158 switch(resvt)
3160 case VT_I8:
3161 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3162 break;
3163 case VT_I4:
3164 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3165 break;
3166 case VT_I2:
3167 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3168 break;
3169 case VT_UI1:
3170 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3171 break;
3172 case VT_BOOL:
3173 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3174 break;
3175 default:
3176 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3177 leftvt,rightvt);
3180 VarAnd_Exit:
3181 VariantClear(&varLeft);
3182 VariantClear(&varRight);
3183 VariantClear(&tempLeft);
3184 VariantClear(&tempRight);
3186 return hres;
3189 /**********************************************************************
3190 * VarAdd [OLEAUT32.141]
3192 * Add two variants.
3194 * PARAMS
3195 * left [I] First variant
3196 * right [I] Second variant
3197 * result [O] Result variant
3199 * RETURNS
3200 * Success: S_OK.
3201 * Failure: An HRESULT error code indicating the error.
3203 * NOTES
3204 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3205 * UI8, INT and UINT as input variants.
3207 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3208 * same here.
3210 * FIXME
3211 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3212 * case.
3214 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3216 HRESULT hres;
3217 VARTYPE lvt, rvt, resvt, tvt;
3218 VARIANT lv, rv, tv;
3219 VARIANT tempLeft, tempRight;
3220 double r8res;
3222 /* Variant priority for coercion. Sorted from lowest to highest.
3223 VT_ERROR shows an invalid input variant type. */
3224 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3225 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3226 vt_ERROR };
3227 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3228 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3229 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3230 VT_NULL, VT_ERROR };
3232 /* Mapping for coercion from input variant to priority of result variant. */
3233 static const VARTYPE coerce[] = {
3234 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3235 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3236 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3237 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3238 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3239 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3240 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3241 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3244 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3245 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3246 result);
3248 VariantInit(&lv);
3249 VariantInit(&rv);
3250 VariantInit(&tv);
3251 VariantInit(&tempLeft);
3252 VariantInit(&tempRight);
3254 /* Handle VT_DISPATCH by storing and taking address of returned value */
3255 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3257 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3259 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3260 if (FAILED(hres)) goto end;
3261 left = &tempLeft;
3263 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3265 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3266 if (FAILED(hres)) goto end;
3267 right = &tempRight;
3271 lvt = V_VT(left)&VT_TYPEMASK;
3272 rvt = V_VT(right)&VT_TYPEMASK;
3274 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3275 Same for any input variant type > VT_I8 */
3276 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3277 lvt > VT_I8 || rvt > VT_I8) {
3278 hres = DISP_E_BADVARTYPE;
3279 goto end;
3282 /* Determine the variant type to coerce to. */
3283 if (coerce[lvt] > coerce[rvt]) {
3284 resvt = prio2vt[coerce[lvt]];
3285 tvt = prio2vt[coerce[rvt]];
3286 } else {
3287 resvt = prio2vt[coerce[rvt]];
3288 tvt = prio2vt[coerce[lvt]];
3291 /* Special cases where the result variant type is defined by both
3292 input variants and not only that with the highest priority */
3293 if (resvt == VT_BSTR) {
3294 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3295 resvt = VT_BSTR;
3296 else
3297 resvt = VT_R8;
3299 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3300 resvt = VT_R8;
3302 /* For overflow detection use the biggest compatible type for the
3303 addition */
3304 switch (resvt) {
3305 case VT_ERROR:
3306 hres = DISP_E_BADVARTYPE;
3307 goto end;
3308 case VT_NULL:
3309 hres = S_OK;
3310 V_VT(result) = VT_NULL;
3311 goto end;
3312 case VT_DISPATCH:
3313 FIXME("cannot handle variant type VT_DISPATCH\n");
3314 hres = DISP_E_TYPEMISMATCH;
3315 goto end;
3316 case VT_EMPTY:
3317 resvt = VT_I2;
3318 /* Fall through */
3319 case VT_UI1:
3320 case VT_I2:
3321 case VT_I4:
3322 case VT_I8:
3323 tvt = VT_I8;
3324 break;
3325 case VT_DATE:
3326 case VT_R4:
3327 tvt = VT_R8;
3328 break;
3329 default:
3330 tvt = resvt;
3333 /* Now coerce the variants */
3334 hres = VariantChangeType(&lv, left, 0, tvt);
3335 if (FAILED(hres))
3336 goto end;
3337 hres = VariantChangeType(&rv, right, 0, tvt);
3338 if (FAILED(hres))
3339 goto end;
3341 /* Do the math */
3342 hres = S_OK;
3343 V_VT(result) = resvt;
3344 switch (tvt) {
3345 case VT_DECIMAL:
3346 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3347 &V_DECIMAL(result));
3348 goto end;
3349 case VT_CY:
3350 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3351 goto end;
3352 case VT_BSTR:
3353 /* We do not add those, we concatenate them. */
3354 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3355 goto end;
3356 case VT_I8:
3357 /* Overflow detection */
3358 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3359 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3360 V_VT(result) = VT_R8;
3361 V_R8(result) = r8res;
3362 goto end;
3363 } else {
3364 V_VT(&tv) = tvt;
3365 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3367 break;
3368 case VT_R8:
3369 V_VT(&tv) = tvt;
3370 /* FIXME: overflow detection */
3371 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3372 break;
3373 default:
3374 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3375 break;
3377 if (resvt != tvt) {
3378 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3379 /* Overflow! Change to the vartype with the next higher priority.
3380 With one exception: I4 ==> R8 even if it would fit in I8 */
3381 if (resvt == VT_I4)
3382 resvt = VT_R8;
3383 else
3384 resvt = prio2vt[coerce[resvt] + 1];
3385 hres = VariantChangeType(result, &tv, 0, resvt);
3387 } else
3388 hres = VariantCopy(result, &tv);
3390 end:
3391 if (hres != S_OK) {
3392 V_VT(result) = VT_EMPTY;
3393 V_I4(result) = 0; /* No V_EMPTY */
3395 VariantClear(&lv);
3396 VariantClear(&rv);
3397 VariantClear(&tv);
3398 VariantClear(&tempLeft);
3399 VariantClear(&tempRight);
3400 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3401 return hres;
3404 /**********************************************************************
3405 * VarMul [OLEAUT32.156]
3407 * Multiply two variants.
3409 * PARAMS
3410 * left [I] First variant
3411 * right [I] Second variant
3412 * result [O] Result variant
3414 * RETURNS
3415 * Success: S_OK.
3416 * Failure: An HRESULT error code indicating the error.
3418 * NOTES
3419 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3420 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3422 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3423 * same here.
3425 * FIXME
3426 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3427 * case.
3429 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3431 HRESULT hres;
3432 VARTYPE lvt, rvt, resvt, tvt;
3433 VARIANT lv, rv, tv;
3434 VARIANT tempLeft, tempRight;
3435 double r8res;
3437 /* Variant priority for coercion. Sorted from lowest to highest.
3438 VT_ERROR shows an invalid input variant type. */
3439 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3440 vt_DECIMAL, vt_NULL, vt_ERROR };
3441 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3442 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3443 VT_DECIMAL, VT_NULL, VT_ERROR };
3445 /* Mapping for coercion from input variant to priority of result variant. */
3446 static const VARTYPE coerce[] = {
3447 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3448 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3449 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3450 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3451 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3452 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3453 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3454 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3457 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3458 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3459 result);
3461 VariantInit(&lv);
3462 VariantInit(&rv);
3463 VariantInit(&tv);
3464 VariantInit(&tempLeft);
3465 VariantInit(&tempRight);
3467 /* Handle VT_DISPATCH by storing and taking address of returned value */
3468 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3470 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3471 if (FAILED(hres)) goto end;
3472 left = &tempLeft;
3474 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3476 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3477 if (FAILED(hres)) goto end;
3478 right = &tempRight;
3481 lvt = V_VT(left)&VT_TYPEMASK;
3482 rvt = V_VT(right)&VT_TYPEMASK;
3484 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3485 Same for any input variant type > VT_I8 */
3486 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3487 lvt > VT_I8 || rvt > VT_I8) {
3488 hres = DISP_E_BADVARTYPE;
3489 goto end;
3492 /* Determine the variant type to coerce to. */
3493 if (coerce[lvt] > coerce[rvt]) {
3494 resvt = prio2vt[coerce[lvt]];
3495 tvt = prio2vt[coerce[rvt]];
3496 } else {
3497 resvt = prio2vt[coerce[rvt]];
3498 tvt = prio2vt[coerce[lvt]];
3501 /* Special cases where the result variant type is defined by both
3502 input variants and not only that with the highest priority */
3503 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3504 resvt = VT_R8;
3505 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3506 resvt = VT_I2;
3508 /* For overflow detection use the biggest compatible type for the
3509 multiplication */
3510 switch (resvt) {
3511 case VT_ERROR:
3512 hres = DISP_E_BADVARTYPE;
3513 goto end;
3514 case VT_NULL:
3515 hres = S_OK;
3516 V_VT(result) = VT_NULL;
3517 goto end;
3518 case VT_UI1:
3519 case VT_I2:
3520 case VT_I4:
3521 case VT_I8:
3522 tvt = VT_I8;
3523 break;
3524 case VT_R4:
3525 tvt = VT_R8;
3526 break;
3527 default:
3528 tvt = resvt;
3531 /* Now coerce the variants */
3532 hres = VariantChangeType(&lv, left, 0, tvt);
3533 if (FAILED(hres))
3534 goto end;
3535 hres = VariantChangeType(&rv, right, 0, tvt);
3536 if (FAILED(hres))
3537 goto end;
3539 /* Do the math */
3540 hres = S_OK;
3541 V_VT(&tv) = tvt;
3542 V_VT(result) = resvt;
3543 switch (tvt) {
3544 case VT_DECIMAL:
3545 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3546 &V_DECIMAL(result));
3547 goto end;
3548 case VT_CY:
3549 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3550 goto end;
3551 case VT_I8:
3552 /* Overflow detection */
3553 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3554 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3555 V_VT(result) = VT_R8;
3556 V_R8(result) = r8res;
3557 goto end;
3558 } else
3559 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3560 break;
3561 case VT_R8:
3562 /* FIXME: overflow detection */
3563 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3564 break;
3565 default:
3566 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3567 break;
3569 if (resvt != tvt) {
3570 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3571 /* Overflow! Change to the vartype with the next higher priority.
3572 With one exception: I4 ==> R8 even if it would fit in I8 */
3573 if (resvt == VT_I4)
3574 resvt = VT_R8;
3575 else
3576 resvt = prio2vt[coerce[resvt] + 1];
3578 } else
3579 hres = VariantCopy(result, &tv);
3581 end:
3582 if (hres != S_OK) {
3583 V_VT(result) = VT_EMPTY;
3584 V_I4(result) = 0; /* No V_EMPTY */
3586 VariantClear(&lv);
3587 VariantClear(&rv);
3588 VariantClear(&tv);
3589 VariantClear(&tempLeft);
3590 VariantClear(&tempRight);
3591 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3592 return hres;
3595 /**********************************************************************
3596 * VarDiv [OLEAUT32.143]
3598 * Divides one variant with another.
3600 * PARAMS
3601 * left [I] First variant
3602 * right [I] Second variant
3603 * result [O] Result variant
3605 * RETURNS
3606 * Success: S_OK.
3607 * Failure: An HRESULT error code indicating the error.
3609 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3611 HRESULT hres = S_OK;
3612 VARTYPE resvt = VT_EMPTY;
3613 VARTYPE leftvt,rightvt;
3614 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3615 VARIANT lv,rv;
3616 VARIANT tempLeft, tempRight;
3618 VariantInit(&tempLeft);
3619 VariantInit(&tempRight);
3620 VariantInit(&lv);
3621 VariantInit(&rv);
3623 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3624 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3626 /* Handle VT_DISPATCH by storing and taking address of returned value */
3627 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3629 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3630 if (FAILED(hres)) goto end;
3631 left = &tempLeft;
3633 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3635 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3636 if (FAILED(hres)) goto end;
3637 right = &tempRight;
3640 leftvt = V_VT(left)&VT_TYPEMASK;
3641 rightvt = V_VT(right)&VT_TYPEMASK;
3642 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3643 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3645 if (leftExtraFlags != rightExtraFlags)
3647 hres = DISP_E_BADVARTYPE;
3648 goto end;
3650 ExtraFlags = leftExtraFlags;
3652 /* Native VarDiv always returns an error when using extra flags */
3653 if (ExtraFlags != 0)
3655 hres = DISP_E_BADVARTYPE;
3656 goto end;
3659 /* Determine return type */
3660 if (!(rightvt == VT_EMPTY))
3662 if (leftvt == VT_NULL || rightvt == VT_NULL)
3664 V_VT(result) = VT_NULL;
3665 hres = S_OK;
3666 goto end;
3668 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3669 resvt = VT_DECIMAL;
3670 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3671 leftvt == VT_CY || rightvt == VT_CY ||
3672 leftvt == VT_DATE || rightvt == VT_DATE ||
3673 leftvt == VT_I4 || rightvt == VT_I4 ||
3674 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3675 leftvt == VT_I2 || rightvt == VT_I2 ||
3676 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3677 leftvt == VT_R8 || rightvt == VT_R8 ||
3678 leftvt == VT_UI1 || rightvt == VT_UI1)
3680 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3681 (leftvt == VT_R4 && rightvt == VT_UI1))
3682 resvt = VT_R4;
3683 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3684 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3685 (leftvt == VT_BOOL || leftvt == VT_I2)))
3686 resvt = VT_R4;
3687 else
3688 resvt = VT_R8;
3690 else if (leftvt == VT_R4 || rightvt == VT_R4)
3691 resvt = VT_R4;
3693 else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
3695 V_VT(result) = VT_NULL;
3696 hres = S_OK;
3697 goto end;
3699 else
3701 hres = DISP_E_BADVARTYPE;
3702 goto end;
3705 /* coerce to the result type */
3706 hres = VariantChangeType(&lv, left, 0, resvt);
3707 if (hres != S_OK) goto end;
3709 hres = VariantChangeType(&rv, right, 0, resvt);
3710 if (hres != S_OK) goto end;
3712 /* do the math */
3713 V_VT(result) = resvt;
3714 switch (resvt)
3716 case VT_R4:
3717 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3719 hres = DISP_E_OVERFLOW;
3720 V_VT(result) = VT_EMPTY;
3722 else if (V_R4(&rv) == 0.0)
3724 hres = DISP_E_DIVBYZERO;
3725 V_VT(result) = VT_EMPTY;
3727 else
3728 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3729 break;
3730 case VT_R8:
3731 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3733 hres = DISP_E_OVERFLOW;
3734 V_VT(result) = VT_EMPTY;
3736 else if (V_R8(&rv) == 0.0)
3738 hres = DISP_E_DIVBYZERO;
3739 V_VT(result) = VT_EMPTY;
3741 else
3742 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3743 break;
3744 case VT_DECIMAL:
3745 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3746 break;
3749 end:
3750 VariantClear(&lv);
3751 VariantClear(&rv);
3752 VariantClear(&tempLeft);
3753 VariantClear(&tempRight);
3754 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3755 return hres;
3758 /**********************************************************************
3759 * VarSub [OLEAUT32.159]
3761 * Subtract two variants.
3763 * PARAMS
3764 * left [I] First variant
3765 * right [I] Second variant
3766 * result [O] Result variant
3768 * RETURNS
3769 * Success: S_OK.
3770 * Failure: An HRESULT error code indicating the error.
3772 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3774 HRESULT hres = S_OK;
3775 VARTYPE resvt = VT_EMPTY;
3776 VARTYPE leftvt,rightvt;
3777 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3778 VARIANT lv,rv;
3779 VARIANT tempLeft, tempRight;
3781 VariantInit(&lv);
3782 VariantInit(&rv);
3783 VariantInit(&tempLeft);
3784 VariantInit(&tempRight);
3786 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3787 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3789 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3790 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3791 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3793 if (NULL == V_DISPATCH(left)) {
3794 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3795 hres = DISP_E_BADVARTYPE;
3796 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3797 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3798 hres = DISP_E_BADVARTYPE;
3799 else switch (V_VT(right) & VT_TYPEMASK)
3801 case VT_VARIANT:
3802 case VT_UNKNOWN:
3803 case 15:
3804 case VT_I1:
3805 case VT_UI2:
3806 case VT_UI4:
3807 hres = DISP_E_BADVARTYPE;
3809 if (FAILED(hres)) goto end;
3811 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3812 if (FAILED(hres)) goto end;
3813 left = &tempLeft;
3815 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3816 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3817 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3819 if (NULL == V_DISPATCH(right))
3821 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3822 hres = DISP_E_BADVARTYPE;
3823 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3824 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3825 hres = DISP_E_BADVARTYPE;
3826 else switch (V_VT(left) & VT_TYPEMASK)
3828 case VT_VARIANT:
3829 case VT_UNKNOWN:
3830 case 15:
3831 case VT_I1:
3832 case VT_UI2:
3833 case VT_UI4:
3834 hres = DISP_E_BADVARTYPE;
3836 if (FAILED(hres)) goto end;
3838 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3839 if (FAILED(hres)) goto end;
3840 right = &tempRight;
3843 leftvt = V_VT(left)&VT_TYPEMASK;
3844 rightvt = V_VT(right)&VT_TYPEMASK;
3845 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3846 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3848 if (leftExtraFlags != rightExtraFlags)
3850 hres = DISP_E_BADVARTYPE;
3851 goto end;
3853 ExtraFlags = leftExtraFlags;
3855 /* determine return type and return code */
3856 /* All extra flags produce errors */
3857 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3858 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3859 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3860 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3861 ExtraFlags == VT_VECTOR ||
3862 ExtraFlags == VT_BYREF ||
3863 ExtraFlags == VT_RESERVED)
3865 hres = DISP_E_BADVARTYPE;
3866 goto end;
3868 else if (ExtraFlags >= VT_ARRAY)
3870 hres = DISP_E_TYPEMISMATCH;
3871 goto end;
3873 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3874 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3875 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3876 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3877 leftvt == VT_I1 || rightvt == VT_I1 ||
3878 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3879 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3880 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3881 leftvt == VT_INT || rightvt == VT_INT ||
3882 leftvt == VT_UINT || rightvt == VT_UINT ||
3883 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3884 leftvt == VT_RECORD || rightvt == VT_RECORD)
3886 if (leftvt == VT_RECORD && rightvt == VT_I8)
3887 hres = DISP_E_TYPEMISMATCH;
3888 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3889 hres = DISP_E_TYPEMISMATCH;
3890 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3891 hres = DISP_E_TYPEMISMATCH;
3892 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3893 hres = DISP_E_TYPEMISMATCH;
3894 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3895 hres = DISP_E_BADVARTYPE;
3896 else
3897 hres = DISP_E_BADVARTYPE;
3898 goto end;
3900 /* The following flags/types are invalid for left variant */
3901 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3902 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3903 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3905 hres = DISP_E_BADVARTYPE;
3906 goto end;
3908 /* The following flags/types are invalid for right variant */
3909 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3910 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3911 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3913 hres = DISP_E_BADVARTYPE;
3914 goto end;
3916 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3917 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3918 resvt = VT_NULL;
3919 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3920 leftvt == VT_ERROR || rightvt == VT_ERROR)
3922 hres = DISP_E_TYPEMISMATCH;
3923 goto end;
3925 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3926 resvt = VT_NULL;
3927 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3928 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3929 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3930 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3931 resvt = VT_R8;
3932 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3933 resvt = VT_DECIMAL;
3934 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3935 resvt = VT_DATE;
3936 else if (leftvt == VT_CY || rightvt == VT_CY)
3937 resvt = VT_CY;
3938 else if (leftvt == VT_R8 || rightvt == VT_R8)
3939 resvt = VT_R8;
3940 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3941 resvt = VT_R8;
3942 else if (leftvt == VT_R4 || rightvt == VT_R4)
3944 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3945 leftvt == VT_I8 || rightvt == VT_I8)
3946 resvt = VT_R8;
3947 else
3948 resvt = VT_R4;
3950 else if (leftvt == VT_I8 || rightvt == VT_I8)
3951 resvt = VT_I8;
3952 else if (leftvt == VT_I4 || rightvt == VT_I4)
3953 resvt = VT_I4;
3954 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3955 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3956 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3957 resvt = VT_I2;
3958 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3959 resvt = VT_UI1;
3960 else
3962 hres = DISP_E_TYPEMISMATCH;
3963 goto end;
3966 /* coerce to the result type */
3967 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3968 hres = VariantChangeType(&lv, left, 0, VT_R8);
3969 else
3970 hres = VariantChangeType(&lv, left, 0, resvt);
3971 if (hres != S_OK) goto end;
3972 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3973 hres = VariantChangeType(&rv, right, 0, VT_R8);
3974 else
3975 hres = VariantChangeType(&rv, right, 0, resvt);
3976 if (hres != S_OK) goto end;
3978 /* do the math */
3979 V_VT(result) = resvt;
3980 switch (resvt)
3982 case VT_NULL:
3983 break;
3984 case VT_DATE:
3985 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3986 break;
3987 case VT_CY:
3988 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3989 break;
3990 case VT_R4:
3991 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3992 break;
3993 case VT_I8:
3994 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3995 break;
3996 case VT_I4:
3997 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3998 break;
3999 case VT_I2:
4000 V_I2(result) = V_I2(&lv) - V_I2(&rv);
4001 break;
4002 case VT_I1:
4003 V_I1(result) = V_I1(&lv) - V_I1(&rv);
4004 break;
4005 case VT_UI1:
4006 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
4007 break;
4008 case VT_R8:
4009 V_R8(result) = V_R8(&lv) - V_R8(&rv);
4010 break;
4011 case VT_DECIMAL:
4012 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
4013 break;
4016 end:
4017 VariantClear(&lv);
4018 VariantClear(&rv);
4019 VariantClear(&tempLeft);
4020 VariantClear(&tempRight);
4021 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
4022 return hres;
4026 /**********************************************************************
4027 * VarOr [OLEAUT32.157]
4029 * Perform a logical or (OR) operation on two variants.
4031 * PARAMS
4032 * pVarLeft [I] First variant
4033 * pVarRight [I] Variant to OR with pVarLeft
4034 * pVarOut [O] Destination for OR result
4036 * RETURNS
4037 * Success: S_OK. pVarOut contains the result of the operation with its type
4038 * taken from the table listed under VarXor().
4039 * Failure: An HRESULT error code indicating the error.
4041 * NOTES
4042 * See the Notes section of VarXor() for further information.
4044 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4046 VARTYPE vt = VT_I4;
4047 VARIANT varLeft, varRight, varStr;
4048 HRESULT hRet;
4049 VARIANT tempLeft, tempRight;
4051 VariantInit(&tempLeft);
4052 VariantInit(&tempRight);
4053 VariantInit(&varLeft);
4054 VariantInit(&varRight);
4055 VariantInit(&varStr);
4057 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4058 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4059 debugstr_VF(pVarRight), pVarOut);
4061 /* Handle VT_DISPATCH by storing and taking address of returned value */
4062 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4064 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4065 if (FAILED(hRet)) goto VarOr_Exit;
4066 pVarLeft = &tempLeft;
4068 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4070 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4071 if (FAILED(hRet)) goto VarOr_Exit;
4072 pVarRight = &tempRight;
4075 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4076 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4077 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
4078 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
4080 hRet = DISP_E_BADVARTYPE;
4081 goto VarOr_Exit;
4084 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
4086 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4088 /* NULL OR Zero is NULL, NULL OR value is value */
4089 if (V_VT(pVarLeft) == VT_NULL)
4090 pVarLeft = pVarRight; /* point to the non-NULL var */
4092 V_VT(pVarOut) = VT_NULL;
4093 V_I4(pVarOut) = 0;
4095 switch (V_VT(pVarLeft))
4097 case VT_DATE: case VT_R8:
4098 if (V_R8(pVarLeft))
4099 goto VarOr_AsEmpty;
4100 hRet = S_OK;
4101 goto VarOr_Exit;
4102 case VT_BOOL:
4103 if (V_BOOL(pVarLeft))
4104 *pVarOut = *pVarLeft;
4105 hRet = S_OK;
4106 goto VarOr_Exit;
4107 case VT_I2: case VT_UI2:
4108 if (V_I2(pVarLeft))
4109 goto VarOr_AsEmpty;
4110 hRet = S_OK;
4111 goto VarOr_Exit;
4112 case VT_I1:
4113 if (V_I1(pVarLeft))
4114 goto VarOr_AsEmpty;
4115 hRet = S_OK;
4116 goto VarOr_Exit;
4117 case VT_UI1:
4118 if (V_UI1(pVarLeft))
4119 *pVarOut = *pVarLeft;
4120 hRet = S_OK;
4121 goto VarOr_Exit;
4122 case VT_R4:
4123 if (V_R4(pVarLeft))
4124 goto VarOr_AsEmpty;
4125 hRet = S_OK;
4126 goto VarOr_Exit;
4127 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4128 if (V_I4(pVarLeft))
4129 goto VarOr_AsEmpty;
4130 hRet = S_OK;
4131 goto VarOr_Exit;
4132 case VT_CY:
4133 if (V_CY(pVarLeft).int64)
4134 goto VarOr_AsEmpty;
4135 hRet = S_OK;
4136 goto VarOr_Exit;
4137 case VT_I8: case VT_UI8:
4138 if (V_I8(pVarLeft))
4139 goto VarOr_AsEmpty;
4140 hRet = S_OK;
4141 goto VarOr_Exit;
4142 case VT_DECIMAL:
4143 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4144 goto VarOr_AsEmpty;
4145 hRet = S_OK;
4146 goto VarOr_Exit;
4147 case VT_BSTR:
4149 VARIANT_BOOL b;
4151 if (!V_BSTR(pVarLeft))
4153 hRet = DISP_E_BADVARTYPE;
4154 goto VarOr_Exit;
4157 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4158 if (SUCCEEDED(hRet) && b)
4160 V_VT(pVarOut) = VT_BOOL;
4161 V_BOOL(pVarOut) = b;
4163 goto VarOr_Exit;
4165 case VT_NULL: case VT_EMPTY:
4166 V_VT(pVarOut) = VT_NULL;
4167 hRet = S_OK;
4168 goto VarOr_Exit;
4169 default:
4170 hRet = DISP_E_BADVARTYPE;
4171 goto VarOr_Exit;
4175 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4177 if (V_VT(pVarLeft) == VT_EMPTY)
4178 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4180 VarOr_AsEmpty:
4181 /* Since one argument is empty (0), OR'ing it with the other simply
4182 * gives the others value (as 0|x => x). So just convert the other
4183 * argument to the required result type.
4185 switch (V_VT(pVarLeft))
4187 case VT_BSTR:
4188 if (!V_BSTR(pVarLeft))
4190 hRet = DISP_E_BADVARTYPE;
4191 goto VarOr_Exit;
4194 hRet = VariantCopy(&varStr, pVarLeft);
4195 if (FAILED(hRet))
4196 goto VarOr_Exit;
4197 pVarLeft = &varStr;
4198 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4199 if (FAILED(hRet))
4200 goto VarOr_Exit;
4201 /* Fall Through ... */
4202 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4203 V_VT(pVarOut) = VT_I2;
4204 break;
4205 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4206 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4207 case VT_INT: case VT_UINT: case VT_UI8:
4208 V_VT(pVarOut) = VT_I4;
4209 break;
4210 case VT_I8:
4211 V_VT(pVarOut) = VT_I8;
4212 break;
4213 default:
4214 hRet = DISP_E_BADVARTYPE;
4215 goto VarOr_Exit;
4217 hRet = VariantCopy(&varLeft, pVarLeft);
4218 if (FAILED(hRet))
4219 goto VarOr_Exit;
4220 pVarLeft = &varLeft;
4221 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4222 goto VarOr_Exit;
4225 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4227 V_VT(pVarOut) = VT_BOOL;
4228 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4229 hRet = S_OK;
4230 goto VarOr_Exit;
4233 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4235 V_VT(pVarOut) = VT_UI1;
4236 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4237 hRet = S_OK;
4238 goto VarOr_Exit;
4241 if (V_VT(pVarLeft) == VT_BSTR)
4243 hRet = VariantCopy(&varStr, pVarLeft);
4244 if (FAILED(hRet))
4245 goto VarOr_Exit;
4246 pVarLeft = &varStr;
4247 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4248 if (FAILED(hRet))
4249 goto VarOr_Exit;
4252 if (V_VT(pVarLeft) == VT_BOOL &&
4253 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4255 vt = VT_BOOL;
4257 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4258 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4259 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4260 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4262 vt = VT_I2;
4264 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4266 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4268 hRet = DISP_E_TYPEMISMATCH;
4269 goto VarOr_Exit;
4271 vt = VT_I8;
4274 hRet = VariantCopy(&varLeft, pVarLeft);
4275 if (FAILED(hRet))
4276 goto VarOr_Exit;
4278 hRet = VariantCopy(&varRight, pVarRight);
4279 if (FAILED(hRet))
4280 goto VarOr_Exit;
4282 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4283 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4284 else
4286 double d;
4288 if (V_VT(&varLeft) == VT_BSTR &&
4289 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4290 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4291 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4292 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4293 if (FAILED(hRet))
4294 goto VarOr_Exit;
4297 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4298 V_VT(&varRight) = VT_I4; /* Don't overflow */
4299 else
4301 double d;
4303 if (V_VT(&varRight) == VT_BSTR &&
4304 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4305 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4306 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4307 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4308 if (FAILED(hRet))
4309 goto VarOr_Exit;
4312 V_VT(pVarOut) = vt;
4313 if (vt == VT_I8)
4315 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4317 else if (vt == VT_I4)
4319 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4321 else
4323 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4326 VarOr_Exit:
4327 VariantClear(&varStr);
4328 VariantClear(&varLeft);
4329 VariantClear(&varRight);
4330 VariantClear(&tempLeft);
4331 VariantClear(&tempRight);
4332 return hRet;
4335 /**********************************************************************
4336 * VarAbs [OLEAUT32.168]
4338 * Convert a variant to its absolute value.
4340 * PARAMS
4341 * pVarIn [I] Source variant
4342 * pVarOut [O] Destination for converted value
4344 * RETURNS
4345 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4346 * Failure: An HRESULT error code indicating the error.
4348 * NOTES
4349 * - This function does not process by-reference variants.
4350 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4351 * according to the following table:
4352 *| Input Type Output Type
4353 *| ---------- -----------
4354 *| VT_BOOL VT_I2
4355 *| VT_BSTR VT_R8
4356 *| (All others) Unchanged
4358 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4360 VARIANT varIn;
4361 HRESULT hRet = S_OK;
4362 VARIANT temp;
4364 VariantInit(&temp);
4366 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4367 debugstr_VF(pVarIn), pVarOut);
4369 /* Handle VT_DISPATCH by storing and taking address of returned value */
4370 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4372 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4373 if (FAILED(hRet)) goto VarAbs_Exit;
4374 pVarIn = &temp;
4377 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4378 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4379 V_VT(pVarIn) == VT_ERROR)
4381 hRet = DISP_E_TYPEMISMATCH;
4382 goto VarAbs_Exit;
4384 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4386 #define ABS_CASE(typ,min) \
4387 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4388 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4389 break
4391 switch (V_VT(pVarIn))
4393 ABS_CASE(I1,I1_MIN);
4394 case VT_BOOL:
4395 V_VT(pVarOut) = VT_I2;
4396 /* BOOL->I2, Fall through ... */
4397 ABS_CASE(I2,I2_MIN);
4398 case VT_INT:
4399 ABS_CASE(I4,I4_MIN);
4400 ABS_CASE(I8,I8_MIN);
4401 ABS_CASE(R4,R4_MIN);
4402 case VT_BSTR:
4403 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4404 if (FAILED(hRet))
4405 break;
4406 V_VT(pVarOut) = VT_R8;
4407 pVarIn = &varIn;
4408 /* Fall through ... */
4409 case VT_DATE:
4410 ABS_CASE(R8,R8_MIN);
4411 case VT_CY:
4412 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4413 break;
4414 case VT_DECIMAL:
4415 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4416 break;
4417 case VT_UI1:
4418 case VT_UI2:
4419 case VT_UINT:
4420 case VT_UI4:
4421 case VT_UI8:
4422 /* No-Op */
4423 break;
4424 case VT_EMPTY:
4425 V_VT(pVarOut) = VT_I2;
4426 case VT_NULL:
4427 V_I2(pVarOut) = 0;
4428 break;
4429 default:
4430 hRet = DISP_E_BADVARTYPE;
4433 VarAbs_Exit:
4434 VariantClear(&temp);
4435 return hRet;
4438 /**********************************************************************
4439 * VarFix [OLEAUT32.169]
4441 * Truncate a variants value to a whole number.
4443 * PARAMS
4444 * pVarIn [I] Source variant
4445 * pVarOut [O] Destination for converted value
4447 * RETURNS
4448 * Success: S_OK. pVarOut contains the converted value.
4449 * Failure: An HRESULT error code indicating the error.
4451 * NOTES
4452 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4453 * according to the following table:
4454 *| Input Type Output Type
4455 *| ---------- -----------
4456 *| VT_BOOL VT_I2
4457 *| VT_EMPTY VT_I2
4458 *| VT_BSTR VT_R8
4459 *| All Others Unchanged
4460 * - The difference between this function and VarInt() is that VarInt() rounds
4461 * negative numbers away from 0, while this function rounds them towards zero.
4463 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4465 HRESULT hRet = S_OK;
4466 VARIANT temp;
4468 VariantInit(&temp);
4470 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4471 debugstr_VF(pVarIn), pVarOut);
4473 /* Handle VT_DISPATCH by storing and taking address of returned value */
4474 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4476 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4477 if (FAILED(hRet)) goto VarFix_Exit;
4478 pVarIn = &temp;
4480 V_VT(pVarOut) = V_VT(pVarIn);
4482 switch (V_VT(pVarIn))
4484 case VT_UI1:
4485 V_UI1(pVarOut) = V_UI1(pVarIn);
4486 break;
4487 case VT_BOOL:
4488 V_VT(pVarOut) = VT_I2;
4489 /* Fall through */
4490 case VT_I2:
4491 V_I2(pVarOut) = V_I2(pVarIn);
4492 break;
4493 case VT_I4:
4494 V_I4(pVarOut) = V_I4(pVarIn);
4495 break;
4496 case VT_I8:
4497 V_I8(pVarOut) = V_I8(pVarIn);
4498 break;
4499 case VT_R4:
4500 if (V_R4(pVarIn) < 0.0f)
4501 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4502 else
4503 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4504 break;
4505 case VT_BSTR:
4506 V_VT(pVarOut) = VT_R8;
4507 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4508 pVarIn = pVarOut;
4509 /* Fall through */
4510 case VT_DATE:
4511 case VT_R8:
4512 if (V_R8(pVarIn) < 0.0)
4513 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4514 else
4515 V_R8(pVarOut) = floor(V_R8(pVarIn));
4516 break;
4517 case VT_CY:
4518 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4519 break;
4520 case VT_DECIMAL:
4521 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4522 break;
4523 case VT_EMPTY:
4524 V_VT(pVarOut) = VT_I2;
4525 V_I2(pVarOut) = 0;
4526 break;
4527 case VT_NULL:
4528 /* No-Op */
4529 break;
4530 default:
4531 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4532 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4533 hRet = DISP_E_BADVARTYPE;
4534 else
4535 hRet = DISP_E_TYPEMISMATCH;
4537 VarFix_Exit:
4538 if (FAILED(hRet))
4539 V_VT(pVarOut) = VT_EMPTY;
4540 VariantClear(&temp);
4542 return hRet;
4545 /**********************************************************************
4546 * VarInt [OLEAUT32.172]
4548 * Truncate a variants value to a whole number.
4550 * PARAMS
4551 * pVarIn [I] Source variant
4552 * pVarOut [O] Destination for converted value
4554 * RETURNS
4555 * Success: S_OK. pVarOut contains the converted value.
4556 * Failure: An HRESULT error code indicating the error.
4558 * NOTES
4559 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4560 * according to the following table:
4561 *| Input Type Output Type
4562 *| ---------- -----------
4563 *| VT_BOOL VT_I2
4564 *| VT_EMPTY VT_I2
4565 *| VT_BSTR VT_R8
4566 *| All Others Unchanged
4567 * - The difference between this function and VarFix() is that VarFix() rounds
4568 * negative numbers towards 0, while this function rounds them away from zero.
4570 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4572 HRESULT hRet = S_OK;
4573 VARIANT temp;
4575 VariantInit(&temp);
4577 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4578 debugstr_VF(pVarIn), pVarOut);
4580 /* Handle VT_DISPATCH by storing and taking address of returned value */
4581 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4583 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4584 if (FAILED(hRet)) goto VarInt_Exit;
4585 pVarIn = &temp;
4587 V_VT(pVarOut) = V_VT(pVarIn);
4589 switch (V_VT(pVarIn))
4591 case VT_R4:
4592 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4593 break;
4594 case VT_BSTR:
4595 V_VT(pVarOut) = VT_R8;
4596 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4597 pVarIn = pVarOut;
4598 /* Fall through */
4599 case VT_DATE:
4600 case VT_R8:
4601 V_R8(pVarOut) = floor(V_R8(pVarIn));
4602 break;
4603 case VT_CY:
4604 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4605 break;
4606 case VT_DECIMAL:
4607 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4608 break;
4609 default:
4610 hRet = VarFix(pVarIn, pVarOut);
4612 VarInt_Exit:
4613 VariantClear(&temp);
4615 return hRet;
4618 /**********************************************************************
4619 * VarXor [OLEAUT32.167]
4621 * Perform a logical exclusive-or (XOR) operation on two variants.
4623 * PARAMS
4624 * pVarLeft [I] First variant
4625 * pVarRight [I] Variant to XOR with pVarLeft
4626 * pVarOut [O] Destination for XOR result
4628 * RETURNS
4629 * Success: S_OK. pVarOut contains the result of the operation with its type
4630 * taken from the table below).
4631 * Failure: An HRESULT error code indicating the error.
4633 * NOTES
4634 * - Neither pVarLeft or pVarRight are modified by this function.
4635 * - This function does not process by-reference variants.
4636 * - Input types of VT_BSTR may be numeric strings or boolean text.
4637 * - The type of result stored in pVarOut depends on the types of pVarLeft
4638 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4639 * or VT_NULL if the function succeeds.
4640 * - Type promotion is inconsistent and as a result certain combinations of
4641 * values will return DISP_E_OVERFLOW even when they could be represented.
4642 * This matches the behaviour of native oleaut32.
4644 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4646 VARTYPE vt;
4647 VARIANT varLeft, varRight;
4648 VARIANT tempLeft, tempRight;
4649 double d;
4650 HRESULT hRet;
4652 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4653 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4654 debugstr_VF(pVarRight), pVarOut);
4656 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4657 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4658 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4659 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4660 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4661 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4662 return DISP_E_BADVARTYPE;
4664 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4666 /* NULL XOR anything valid is NULL */
4667 V_VT(pVarOut) = VT_NULL;
4668 return S_OK;
4671 VariantInit(&tempLeft);
4672 VariantInit(&tempRight);
4674 /* Handle VT_DISPATCH by storing and taking address of returned value */
4675 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4677 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4678 if (FAILED(hRet)) goto VarXor_Exit;
4679 pVarLeft = &tempLeft;
4681 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4683 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4684 if (FAILED(hRet)) goto VarXor_Exit;
4685 pVarRight = &tempRight;
4688 /* Copy our inputs so we don't disturb anything */
4689 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4691 hRet = VariantCopy(&varLeft, pVarLeft);
4692 if (FAILED(hRet))
4693 goto VarXor_Exit;
4695 hRet = VariantCopy(&varRight, pVarRight);
4696 if (FAILED(hRet))
4697 goto VarXor_Exit;
4699 /* Try any strings first as numbers, then as VT_BOOL */
4700 if (V_VT(&varLeft) == VT_BSTR)
4702 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4703 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4704 FAILED(hRet) ? VT_BOOL : VT_I4);
4705 if (FAILED(hRet))
4706 goto VarXor_Exit;
4709 if (V_VT(&varRight) == VT_BSTR)
4711 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4712 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4713 FAILED(hRet) ? VT_BOOL : VT_I4);
4714 if (FAILED(hRet))
4715 goto VarXor_Exit;
4718 /* Determine the result type */
4719 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4721 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4723 hRet = DISP_E_TYPEMISMATCH;
4724 goto VarXor_Exit;
4726 vt = VT_I8;
4728 else
4730 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4732 case (VT_BOOL << 16) | VT_BOOL:
4733 vt = VT_BOOL;
4734 break;
4735 case (VT_UI1 << 16) | VT_UI1:
4736 vt = VT_UI1;
4737 break;
4738 case (VT_EMPTY << 16) | VT_EMPTY:
4739 case (VT_EMPTY << 16) | VT_UI1:
4740 case (VT_EMPTY << 16) | VT_I2:
4741 case (VT_EMPTY << 16) | VT_BOOL:
4742 case (VT_UI1 << 16) | VT_EMPTY:
4743 case (VT_UI1 << 16) | VT_I2:
4744 case (VT_UI1 << 16) | VT_BOOL:
4745 case (VT_I2 << 16) | VT_EMPTY:
4746 case (VT_I2 << 16) | VT_UI1:
4747 case (VT_I2 << 16) | VT_I2:
4748 case (VT_I2 << 16) | VT_BOOL:
4749 case (VT_BOOL << 16) | VT_EMPTY:
4750 case (VT_BOOL << 16) | VT_UI1:
4751 case (VT_BOOL << 16) | VT_I2:
4752 vt = VT_I2;
4753 break;
4754 default:
4755 vt = VT_I4;
4756 break;
4760 /* VT_UI4 does not overflow */
4761 if (vt != VT_I8)
4763 if (V_VT(&varLeft) == VT_UI4)
4764 V_VT(&varLeft) = VT_I4;
4765 if (V_VT(&varRight) == VT_UI4)
4766 V_VT(&varRight) = VT_I4;
4769 /* Convert our input copies to the result type */
4770 if (V_VT(&varLeft) != vt)
4771 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4772 if (FAILED(hRet))
4773 goto VarXor_Exit;
4775 if (V_VT(&varRight) != vt)
4776 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4777 if (FAILED(hRet))
4778 goto VarXor_Exit;
4780 V_VT(pVarOut) = vt;
4782 /* Calculate the result */
4783 switch (vt)
4785 case VT_I8:
4786 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4787 break;
4788 case VT_I4:
4789 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4790 break;
4791 case VT_BOOL:
4792 case VT_I2:
4793 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4794 break;
4795 case VT_UI1:
4796 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4797 break;
4800 VarXor_Exit:
4801 VariantClear(&varLeft);
4802 VariantClear(&varRight);
4803 VariantClear(&tempLeft);
4804 VariantClear(&tempRight);
4805 return hRet;
4808 /**********************************************************************
4809 * VarEqv [OLEAUT32.172]
4811 * Determine if two variants contain the same value.
4813 * PARAMS
4814 * pVarLeft [I] First variant to compare
4815 * pVarRight [I] Variant to compare to pVarLeft
4816 * pVarOut [O] Destination for comparison result
4818 * RETURNS
4819 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4820 * if equivalent or non-zero otherwise.
4821 * Failure: An HRESULT error code indicating the error.
4823 * NOTES
4824 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4825 * the result.
4827 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4829 HRESULT hRet;
4831 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4832 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4833 debugstr_VF(pVarRight), pVarOut);
4835 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4836 if (SUCCEEDED(hRet))
4838 if (V_VT(pVarOut) == VT_I8)
4839 V_I8(pVarOut) = ~V_I8(pVarOut);
4840 else
4841 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4843 return hRet;
4846 /**********************************************************************
4847 * VarNeg [OLEAUT32.173]
4849 * Negate the value of a variant.
4851 * PARAMS
4852 * pVarIn [I] Source variant
4853 * pVarOut [O] Destination for converted value
4855 * RETURNS
4856 * Success: S_OK. pVarOut contains the converted value.
4857 * Failure: An HRESULT error code indicating the error.
4859 * NOTES
4860 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4861 * according to the following table:
4862 *| Input Type Output Type
4863 *| ---------- -----------
4864 *| VT_EMPTY VT_I2
4865 *| VT_UI1 VT_I2
4866 *| VT_BOOL VT_I2
4867 *| VT_BSTR VT_R8
4868 *| All Others Unchanged (unless promoted)
4869 * - Where the negated value of a variant does not fit in its base type, the type
4870 * is promoted according to the following table:
4871 *| Input Type Promoted To
4872 *| ---------- -----------
4873 *| VT_I2 VT_I4
4874 *| VT_I4 VT_R8
4875 *| VT_I8 VT_R8
4876 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4877 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4878 * for types which are not valid. Since this is in contravention of the
4879 * meaning of those error codes and unlikely to be relied on by applications,
4880 * this implementation returns errors consistent with the other high level
4881 * variant math functions.
4883 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4885 HRESULT hRet = S_OK;
4886 VARIANT temp;
4888 VariantInit(&temp);
4890 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4891 debugstr_VF(pVarIn), pVarOut);
4893 /* Handle VT_DISPATCH by storing and taking address of returned value */
4894 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4896 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4897 if (FAILED(hRet)) goto VarNeg_Exit;
4898 pVarIn = &temp;
4900 V_VT(pVarOut) = V_VT(pVarIn);
4902 switch (V_VT(pVarIn))
4904 case VT_UI1:
4905 V_VT(pVarOut) = VT_I2;
4906 V_I2(pVarOut) = -V_UI1(pVarIn);
4907 break;
4908 case VT_BOOL:
4909 V_VT(pVarOut) = VT_I2;
4910 /* Fall through */
4911 case VT_I2:
4912 if (V_I2(pVarIn) == I2_MIN)
4914 V_VT(pVarOut) = VT_I4;
4915 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4917 else
4918 V_I2(pVarOut) = -V_I2(pVarIn);
4919 break;
4920 case VT_I4:
4921 if (V_I4(pVarIn) == I4_MIN)
4923 V_VT(pVarOut) = VT_R8;
4924 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4926 else
4927 V_I4(pVarOut) = -V_I4(pVarIn);
4928 break;
4929 case VT_I8:
4930 if (V_I8(pVarIn) == I8_MIN)
4932 V_VT(pVarOut) = VT_R8;
4933 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4934 V_R8(pVarOut) *= -1.0;
4936 else
4937 V_I8(pVarOut) = -V_I8(pVarIn);
4938 break;
4939 case VT_R4:
4940 V_R4(pVarOut) = -V_R4(pVarIn);
4941 break;
4942 case VT_DATE:
4943 case VT_R8:
4944 V_R8(pVarOut) = -V_R8(pVarIn);
4945 break;
4946 case VT_CY:
4947 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4948 break;
4949 case VT_DECIMAL:
4950 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4951 break;
4952 case VT_BSTR:
4953 V_VT(pVarOut) = VT_R8;
4954 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4955 V_R8(pVarOut) = -V_R8(pVarOut);
4956 break;
4957 case VT_EMPTY:
4958 V_VT(pVarOut) = VT_I2;
4959 V_I2(pVarOut) = 0;
4960 break;
4961 case VT_NULL:
4962 /* No-Op */
4963 break;
4964 default:
4965 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4966 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4967 hRet = DISP_E_BADVARTYPE;
4968 else
4969 hRet = DISP_E_TYPEMISMATCH;
4971 VarNeg_Exit:
4972 if (FAILED(hRet))
4973 V_VT(pVarOut) = VT_EMPTY;
4974 VariantClear(&temp);
4976 return hRet;
4979 /**********************************************************************
4980 * VarNot [OLEAUT32.174]
4982 * Perform a not operation on a variant.
4984 * PARAMS
4985 * pVarIn [I] Source variant
4986 * pVarOut [O] Destination for converted value
4988 * RETURNS
4989 * Success: S_OK. pVarOut contains the converted value.
4990 * Failure: An HRESULT error code indicating the error.
4992 * NOTES
4993 * - Strictly speaking, this function performs a bitwise ones complement
4994 * on the variants value (after possibly converting to VT_I4, see below).
4995 * This only behaves like a boolean not operation if the value in
4996 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4997 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4998 * before calling this function.
4999 * - This function does not process by-reference variants.
5000 * - The type of the value stored in pVarOut depends on the type of pVarIn,
5001 * according to the following table:
5002 *| Input Type Output Type
5003 *| ---------- -----------
5004 *| VT_EMPTY VT_I2
5005 *| VT_R4 VT_I4
5006 *| VT_R8 VT_I4
5007 *| VT_BSTR VT_I4
5008 *| VT_DECIMAL VT_I4
5009 *| VT_CY VT_I4
5010 *| (All others) Unchanged
5012 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
5014 VARIANT varIn;
5015 HRESULT hRet = S_OK;
5016 VARIANT temp;
5018 VariantInit(&temp);
5020 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
5021 debugstr_VF(pVarIn), pVarOut);
5023 /* Handle VT_DISPATCH by storing and taking address of returned value */
5024 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5026 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5027 if (FAILED(hRet)) goto VarNot_Exit;
5028 pVarIn = &temp;
5031 V_VT(pVarOut) = V_VT(pVarIn);
5033 switch (V_VT(pVarIn))
5035 case VT_I1:
5036 V_I4(pVarOut) = ~V_I1(pVarIn);
5037 V_VT(pVarOut) = VT_I4;
5038 break;
5039 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
5040 case VT_BOOL:
5041 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
5042 case VT_UI2:
5043 V_I4(pVarOut) = ~V_UI2(pVarIn);
5044 V_VT(pVarOut) = VT_I4;
5045 break;
5046 case VT_DECIMAL:
5047 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
5048 if (FAILED(hRet))
5049 break;
5050 pVarIn = &varIn;
5051 /* Fall through ... */
5052 case VT_INT:
5053 V_VT(pVarOut) = VT_I4;
5054 /* Fall through ... */
5055 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
5056 case VT_UINT:
5057 case VT_UI4:
5058 V_I4(pVarOut) = ~V_UI4(pVarIn);
5059 V_VT(pVarOut) = VT_I4;
5060 break;
5061 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
5062 case VT_UI8:
5063 V_I4(pVarOut) = ~V_UI8(pVarIn);
5064 V_VT(pVarOut) = VT_I4;
5065 break;
5066 case VT_R4:
5067 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
5068 V_I4(pVarOut) = ~V_I4(pVarOut);
5069 V_VT(pVarOut) = VT_I4;
5070 break;
5071 case VT_BSTR:
5072 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5073 if (FAILED(hRet))
5074 break;
5075 pVarIn = &varIn;
5076 /* Fall through ... */
5077 case VT_DATE:
5078 case VT_R8:
5079 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
5080 V_I4(pVarOut) = ~V_I4(pVarOut);
5081 V_VT(pVarOut) = VT_I4;
5082 break;
5083 case VT_CY:
5084 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
5085 V_I4(pVarOut) = ~V_I4(pVarOut);
5086 V_VT(pVarOut) = VT_I4;
5087 break;
5088 case VT_EMPTY:
5089 V_I2(pVarOut) = ~0;
5090 V_VT(pVarOut) = VT_I2;
5091 break;
5092 case VT_NULL:
5093 /* No-Op */
5094 break;
5095 default:
5096 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5097 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5098 hRet = DISP_E_BADVARTYPE;
5099 else
5100 hRet = DISP_E_TYPEMISMATCH;
5102 VarNot_Exit:
5103 if (FAILED(hRet))
5104 V_VT(pVarOut) = VT_EMPTY;
5105 VariantClear(&temp);
5107 return hRet;
5110 /**********************************************************************
5111 * VarRound [OLEAUT32.175]
5113 * Perform a round operation on a variant.
5115 * PARAMS
5116 * pVarIn [I] Source variant
5117 * deci [I] Number of decimals to round to
5118 * pVarOut [O] Destination for converted value
5120 * RETURNS
5121 * Success: S_OK. pVarOut contains the converted value.
5122 * Failure: An HRESULT error code indicating the error.
5124 * NOTES
5125 * - Floating point values are rounded to the desired number of decimals.
5126 * - Some integer types are just copied to the return variable.
5127 * - Some other integer types are not handled and fail.
5129 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5131 VARIANT varIn;
5132 HRESULT hRet = S_OK;
5133 float factor;
5134 VARIANT temp;
5136 VariantInit(&temp);
5138 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
5140 /* Handle VT_DISPATCH by storing and taking address of returned value */
5141 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5143 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5144 if (FAILED(hRet)) goto VarRound_Exit;
5145 pVarIn = &temp;
5148 switch (V_VT(pVarIn))
5150 /* cases that fail on windows */
5151 case VT_I1:
5152 case VT_I8:
5153 case VT_UI2:
5154 case VT_UI4:
5155 hRet = DISP_E_BADVARTYPE;
5156 break;
5158 /* cases just copying in to out */
5159 case VT_UI1:
5160 V_VT(pVarOut) = V_VT(pVarIn);
5161 V_UI1(pVarOut) = V_UI1(pVarIn);
5162 break;
5163 case VT_I2:
5164 V_VT(pVarOut) = V_VT(pVarIn);
5165 V_I2(pVarOut) = V_I2(pVarIn);
5166 break;
5167 case VT_I4:
5168 V_VT(pVarOut) = V_VT(pVarIn);
5169 V_I4(pVarOut) = V_I4(pVarIn);
5170 break;
5171 case VT_NULL:
5172 V_VT(pVarOut) = V_VT(pVarIn);
5173 /* value unchanged */
5174 break;
5176 /* cases that change type */
5177 case VT_EMPTY:
5178 V_VT(pVarOut) = VT_I2;
5179 V_I2(pVarOut) = 0;
5180 break;
5181 case VT_BOOL:
5182 V_VT(pVarOut) = VT_I2;
5183 V_I2(pVarOut) = V_BOOL(pVarIn);
5184 break;
5185 case VT_BSTR:
5186 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5187 if (FAILED(hRet))
5188 break;
5189 V_VT(&varIn)=VT_R8;
5190 pVarIn = &varIn;
5191 /* Fall through ... */
5193 /* cases we need to do math */
5194 case VT_R8:
5195 if (V_R8(pVarIn)>0) {
5196 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5197 } else {
5198 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5200 V_VT(pVarOut) = V_VT(pVarIn);
5201 break;
5202 case VT_R4:
5203 if (V_R4(pVarIn)>0) {
5204 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5205 } else {
5206 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5208 V_VT(pVarOut) = V_VT(pVarIn);
5209 break;
5210 case VT_DATE:
5211 if (V_DATE(pVarIn)>0) {
5212 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5213 } else {
5214 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5216 V_VT(pVarOut) = V_VT(pVarIn);
5217 break;
5218 case VT_CY:
5219 if (deci>3)
5220 factor=1;
5221 else
5222 factor=pow(10, 4-deci);
5224 if (V_CY(pVarIn).int64>0) {
5225 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5226 } else {
5227 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5229 V_VT(pVarOut) = V_VT(pVarIn);
5230 break;
5232 /* cases we don't know yet */
5233 default:
5234 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5235 V_VT(pVarIn) & VT_TYPEMASK, deci);
5236 hRet = DISP_E_BADVARTYPE;
5238 VarRound_Exit:
5239 if (FAILED(hRet))
5240 V_VT(pVarOut) = VT_EMPTY;
5241 VariantClear(&temp);
5243 TRACE("returning 0x%08x (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
5244 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
5245 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
5247 return hRet;
5250 /**********************************************************************
5251 * VarIdiv [OLEAUT32.153]
5253 * Converts input variants to integers and divides them.
5255 * PARAMS
5256 * left [I] Left hand variant
5257 * right [I] Right hand variant
5258 * result [O] Destination for quotient
5260 * RETURNS
5261 * Success: S_OK. result contains the quotient.
5262 * Failure: An HRESULT error code indicating the error.
5264 * NOTES
5265 * If either expression is null, null is returned, as per MSDN
5267 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5269 HRESULT hres = S_OK;
5270 VARTYPE resvt = VT_EMPTY;
5271 VARTYPE leftvt,rightvt;
5272 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5273 VARIANT lv,rv;
5274 VARIANT tempLeft, tempRight;
5276 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5277 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5279 VariantInit(&lv);
5280 VariantInit(&rv);
5281 VariantInit(&tempLeft);
5282 VariantInit(&tempRight);
5284 leftvt = V_VT(left)&VT_TYPEMASK;
5285 rightvt = V_VT(right)&VT_TYPEMASK;
5286 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5287 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5289 if (leftExtraFlags != rightExtraFlags)
5291 hres = DISP_E_BADVARTYPE;
5292 goto end;
5294 ExtraFlags = leftExtraFlags;
5296 /* Native VarIdiv always returns an error when using extra
5297 * flags or if the variant combination is I8 and INT.
5299 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5300 (leftvt == VT_INT && rightvt == VT_I8) ||
5301 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5302 ExtraFlags != 0)
5304 hres = DISP_E_BADVARTYPE;
5305 goto end;
5308 /* Determine variant type */
5309 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5311 V_VT(result) = VT_NULL;
5312 hres = S_OK;
5313 goto end;
5315 else if (leftvt == VT_I8 || rightvt == VT_I8)
5316 resvt = VT_I8;
5317 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5318 leftvt == VT_INT || rightvt == VT_INT ||
5319 leftvt == VT_UINT || rightvt == VT_UINT ||
5320 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5321 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5322 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5323 leftvt == VT_I1 || rightvt == VT_I1 ||
5324 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5325 leftvt == VT_DATE || rightvt == VT_DATE ||
5326 leftvt == VT_CY || rightvt == VT_CY ||
5327 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5328 leftvt == VT_R8 || rightvt == VT_R8 ||
5329 leftvt == VT_R4 || rightvt == VT_R4)
5330 resvt = VT_I4;
5331 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5332 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5333 leftvt == VT_EMPTY)
5334 resvt = VT_I2;
5335 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5336 resvt = VT_UI1;
5337 else
5339 hres = DISP_E_BADVARTYPE;
5340 goto end;
5343 /* coerce to the result type */
5344 hres = VariantChangeType(&lv, left, 0, resvt);
5345 if (hres != S_OK) goto end;
5346 hres = VariantChangeType(&rv, right, 0, resvt);
5347 if (hres != S_OK) goto end;
5349 /* do the math */
5350 V_VT(result) = resvt;
5351 switch (resvt)
5353 case VT_UI1:
5354 if (V_UI1(&rv) == 0)
5356 hres = DISP_E_DIVBYZERO;
5357 V_VT(result) = VT_EMPTY;
5359 else
5360 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5361 break;
5362 case VT_I2:
5363 if (V_I2(&rv) == 0)
5365 hres = DISP_E_DIVBYZERO;
5366 V_VT(result) = VT_EMPTY;
5368 else
5369 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5370 break;
5371 case VT_I4:
5372 if (V_I4(&rv) == 0)
5374 hres = DISP_E_DIVBYZERO;
5375 V_VT(result) = VT_EMPTY;
5377 else
5378 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5379 break;
5380 case VT_I8:
5381 if (V_I8(&rv) == 0)
5383 hres = DISP_E_DIVBYZERO;
5384 V_VT(result) = VT_EMPTY;
5386 else
5387 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5388 break;
5389 default:
5390 FIXME("Couldn't integer divide variant types %d,%d\n",
5391 leftvt,rightvt);
5394 end:
5395 VariantClear(&lv);
5396 VariantClear(&rv);
5397 VariantClear(&tempLeft);
5398 VariantClear(&tempRight);
5400 return hres;
5404 /**********************************************************************
5405 * VarMod [OLEAUT32.155]
5407 * Perform the modulus operation of the right hand variant on the left
5409 * PARAMS
5410 * left [I] Left hand variant
5411 * right [I] Right hand variant
5412 * result [O] Destination for converted value
5414 * RETURNS
5415 * Success: S_OK. result contains the remainder.
5416 * Failure: An HRESULT error code indicating the error.
5418 * NOTE:
5419 * If an error occurs the type of result will be modified but the value will not be.
5420 * Doesn't support arrays or any special flags yet.
5422 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5424 BOOL lOk = TRUE;
5425 HRESULT rc = E_FAIL;
5426 int resT = 0;
5427 VARIANT lv,rv;
5428 VARIANT tempLeft, tempRight;
5430 VariantInit(&tempLeft);
5431 VariantInit(&tempRight);
5432 VariantInit(&lv);
5433 VariantInit(&rv);
5435 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5436 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5438 /* Handle VT_DISPATCH by storing and taking address of returned value */
5439 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5441 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5442 if (FAILED(rc)) goto end;
5443 left = &tempLeft;
5445 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5447 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5448 if (FAILED(rc)) goto end;
5449 right = &tempRight;
5452 /* check for invalid inputs */
5453 lOk = TRUE;
5454 switch (V_VT(left) & VT_TYPEMASK) {
5455 case VT_BOOL :
5456 case VT_I1 :
5457 case VT_I2 :
5458 case VT_I4 :
5459 case VT_I8 :
5460 case VT_INT :
5461 case VT_UI1 :
5462 case VT_UI2 :
5463 case VT_UI4 :
5464 case VT_UI8 :
5465 case VT_UINT :
5466 case VT_R4 :
5467 case VT_R8 :
5468 case VT_CY :
5469 case VT_EMPTY:
5470 case VT_DATE :
5471 case VT_BSTR :
5472 case VT_DECIMAL:
5473 break;
5474 case VT_VARIANT:
5475 case VT_UNKNOWN:
5476 V_VT(result) = VT_EMPTY;
5477 rc = DISP_E_TYPEMISMATCH;
5478 goto end;
5479 case VT_ERROR:
5480 rc = DISP_E_TYPEMISMATCH;
5481 goto end;
5482 case VT_RECORD:
5483 V_VT(result) = VT_EMPTY;
5484 rc = DISP_E_TYPEMISMATCH;
5485 goto end;
5486 case VT_NULL:
5487 break;
5488 default:
5489 V_VT(result) = VT_EMPTY;
5490 rc = DISP_E_BADVARTYPE;
5491 goto end;
5495 switch (V_VT(right) & VT_TYPEMASK) {
5496 case VT_BOOL :
5497 case VT_I1 :
5498 case VT_I2 :
5499 case VT_I4 :
5500 case VT_I8 :
5501 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5503 V_VT(result) = VT_EMPTY;
5504 rc = DISP_E_TYPEMISMATCH;
5505 goto end;
5507 case VT_INT :
5508 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5510 V_VT(result) = VT_EMPTY;
5511 rc = DISP_E_TYPEMISMATCH;
5512 goto end;
5514 case VT_UI1 :
5515 case VT_UI2 :
5516 case VT_UI4 :
5517 case VT_UI8 :
5518 case VT_UINT :
5519 case VT_R4 :
5520 case VT_R8 :
5521 case VT_CY :
5522 if(V_VT(left) == VT_EMPTY)
5524 V_VT(result) = VT_I4;
5525 rc = S_OK;
5526 goto end;
5528 case VT_EMPTY:
5529 case VT_DATE :
5530 case VT_DECIMAL:
5531 if(V_VT(left) == VT_ERROR)
5533 V_VT(result) = VT_EMPTY;
5534 rc = DISP_E_TYPEMISMATCH;
5535 goto end;
5537 case VT_BSTR:
5538 if(V_VT(left) == VT_NULL)
5540 V_VT(result) = VT_NULL;
5541 rc = S_OK;
5542 goto end;
5544 break;
5546 case VT_VOID:
5547 V_VT(result) = VT_EMPTY;
5548 rc = DISP_E_BADVARTYPE;
5549 goto end;
5550 case VT_NULL:
5551 if(V_VT(left) == VT_VOID)
5553 V_VT(result) = VT_EMPTY;
5554 rc = DISP_E_BADVARTYPE;
5555 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5556 lOk)
5558 V_VT(result) = VT_NULL;
5559 rc = S_OK;
5560 } else
5562 V_VT(result) = VT_NULL;
5563 rc = DISP_E_BADVARTYPE;
5565 goto end;
5566 case VT_VARIANT:
5567 case VT_UNKNOWN:
5568 V_VT(result) = VT_EMPTY;
5569 rc = DISP_E_TYPEMISMATCH;
5570 goto end;
5571 case VT_ERROR:
5572 rc = DISP_E_TYPEMISMATCH;
5573 goto end;
5574 case VT_RECORD:
5575 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5577 V_VT(result) = VT_EMPTY;
5578 rc = DISP_E_BADVARTYPE;
5579 } else
5581 V_VT(result) = VT_EMPTY;
5582 rc = DISP_E_TYPEMISMATCH;
5584 goto end;
5585 default:
5586 V_VT(result) = VT_EMPTY;
5587 rc = DISP_E_BADVARTYPE;
5588 goto end;
5591 /* determine the result type */
5592 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5593 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5594 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5595 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5596 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5597 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5598 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5599 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5600 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5601 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5602 else resT = VT_I4; /* most outputs are I4 */
5604 /* convert to I8 for the modulo */
5605 rc = VariantChangeType(&lv, left, 0, VT_I8);
5606 if(FAILED(rc))
5608 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5609 goto end;
5612 rc = VariantChangeType(&rv, right, 0, VT_I8);
5613 if(FAILED(rc))
5615 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5616 goto end;
5619 /* if right is zero set VT_EMPTY and return divide by zero */
5620 if(V_I8(&rv) == 0)
5622 V_VT(result) = VT_EMPTY;
5623 rc = DISP_E_DIVBYZERO;
5624 goto end;
5627 /* perform the modulo operation */
5628 V_VT(result) = VT_I8;
5629 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5631 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5632 wine_dbgstr_longlong(V_I8(&lv)), wine_dbgstr_longlong(V_I8(&rv)),
5633 wine_dbgstr_longlong(V_I8(result)));
5635 /* convert left and right to the destination type */
5636 rc = VariantChangeType(result, result, 0, resT);
5637 if(FAILED(rc))
5639 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5640 /* fall to end of function */
5643 end:
5644 VariantClear(&lv);
5645 VariantClear(&rv);
5646 VariantClear(&tempLeft);
5647 VariantClear(&tempRight);
5648 return rc;
5651 /**********************************************************************
5652 * VarPow [OLEAUT32.158]
5654 * Computes the power of one variant to another variant.
5656 * PARAMS
5657 * left [I] First variant
5658 * right [I] Second variant
5659 * result [O] Result variant
5661 * RETURNS
5662 * Success: S_OK.
5663 * Failure: An HRESULT error code indicating the error.
5665 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5667 HRESULT hr = S_OK;
5668 VARIANT dl,dr;
5669 VARTYPE resvt = VT_EMPTY;
5670 VARTYPE leftvt,rightvt;
5671 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5672 VARIANT tempLeft, tempRight;
5674 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
5675 right, debugstr_VT(right), debugstr_VF(right), result);
5677 VariantInit(&dl);
5678 VariantInit(&dr);
5679 VariantInit(&tempLeft);
5680 VariantInit(&tempRight);
5682 /* Handle VT_DISPATCH by storing and taking address of returned value */
5683 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5685 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5686 if (FAILED(hr)) goto end;
5687 left = &tempLeft;
5689 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5691 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5692 if (FAILED(hr)) goto end;
5693 right = &tempRight;
5696 leftvt = V_VT(left)&VT_TYPEMASK;
5697 rightvt = V_VT(right)&VT_TYPEMASK;
5698 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5699 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5701 if (leftExtraFlags != rightExtraFlags)
5703 hr = DISP_E_BADVARTYPE;
5704 goto end;
5706 ExtraFlags = leftExtraFlags;
5708 /* Native VarPow always returns an error when using extra flags */
5709 if (ExtraFlags != 0)
5711 hr = DISP_E_BADVARTYPE;
5712 goto end;
5715 /* Determine return type */
5716 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5717 V_VT(result) = VT_NULL;
5718 hr = S_OK;
5719 goto end;
5721 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5722 leftvt == VT_I4 || leftvt == VT_R4 ||
5723 leftvt == VT_R8 || leftvt == VT_CY ||
5724 leftvt == VT_DATE || leftvt == VT_BSTR ||
5725 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5726 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5727 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5728 rightvt == VT_I4 || rightvt == VT_R4 ||
5729 rightvt == VT_R8 || rightvt == VT_CY ||
5730 rightvt == VT_DATE || rightvt == VT_BSTR ||
5731 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5732 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5733 resvt = VT_R8;
5734 else
5736 hr = DISP_E_BADVARTYPE;
5737 goto end;
5740 hr = VariantChangeType(&dl,left,0,resvt);
5741 if (FAILED(hr)) {
5742 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5743 hr = E_FAIL;
5744 goto end;
5747 hr = VariantChangeType(&dr,right,0,resvt);
5748 if (FAILED(hr)) {
5749 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5750 hr = E_FAIL;
5751 goto end;
5754 V_VT(result) = VT_R8;
5755 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5757 end:
5758 VariantClear(&dl);
5759 VariantClear(&dr);
5760 VariantClear(&tempLeft);
5761 VariantClear(&tempRight);
5763 return hr;
5766 /**********************************************************************
5767 * VarImp [OLEAUT32.154]
5769 * Bitwise implication of two variants.
5771 * PARAMS
5772 * left [I] First variant
5773 * right [I] Second variant
5774 * result [O] Result variant
5776 * RETURNS
5777 * Success: S_OK.
5778 * Failure: An HRESULT error code indicating the error.
5780 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5782 HRESULT hres = S_OK;
5783 VARTYPE resvt = VT_EMPTY;
5784 VARTYPE leftvt,rightvt;
5785 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5786 VARIANT lv,rv;
5787 double d;
5788 VARIANT tempLeft, tempRight;
5790 VariantInit(&lv);
5791 VariantInit(&rv);
5792 VariantInit(&tempLeft);
5793 VariantInit(&tempRight);
5795 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5796 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5798 /* Handle VT_DISPATCH by storing and taking address of returned value */
5799 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5801 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5802 if (FAILED(hres)) goto VarImp_Exit;
5803 left = &tempLeft;
5805 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5807 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5808 if (FAILED(hres)) goto VarImp_Exit;
5809 right = &tempRight;
5812 leftvt = V_VT(left)&VT_TYPEMASK;
5813 rightvt = V_VT(right)&VT_TYPEMASK;
5814 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5815 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5817 if (leftExtraFlags != rightExtraFlags)
5819 hres = DISP_E_BADVARTYPE;
5820 goto VarImp_Exit;
5822 ExtraFlags = leftExtraFlags;
5824 /* Native VarImp always returns an error when using extra
5825 * flags or if the variants are I8 and INT.
5827 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5828 ExtraFlags != 0)
5830 hres = DISP_E_BADVARTYPE;
5831 goto VarImp_Exit;
5834 /* Determine result type */
5835 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5836 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5838 V_VT(result) = VT_NULL;
5839 hres = S_OK;
5840 goto VarImp_Exit;
5842 else if (leftvt == VT_I8 || rightvt == VT_I8)
5843 resvt = VT_I8;
5844 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5845 leftvt == VT_INT || rightvt == VT_INT ||
5846 leftvt == VT_UINT || rightvt == VT_UINT ||
5847 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5848 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5849 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5850 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5851 leftvt == VT_DATE || rightvt == VT_DATE ||
5852 leftvt == VT_CY || rightvt == VT_CY ||
5853 leftvt == VT_R8 || rightvt == VT_R8 ||
5854 leftvt == VT_R4 || rightvt == VT_R4 ||
5855 leftvt == VT_I1 || rightvt == VT_I1)
5856 resvt = VT_I4;
5857 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5858 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5859 (leftvt == VT_NULL && rightvt == VT_UI1))
5860 resvt = VT_UI1;
5861 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5862 leftvt == VT_I2 || rightvt == VT_I2 ||
5863 leftvt == VT_UI1 || rightvt == VT_UI1)
5864 resvt = VT_I2;
5865 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5866 leftvt == VT_BSTR || rightvt == VT_BSTR)
5867 resvt = VT_BOOL;
5869 /* VT_NULL requires special handling for when the opposite
5870 * variant is equal to something other than -1.
5871 * (NULL Imp 0 = NULL, NULL Imp n = n)
5873 if (leftvt == VT_NULL)
5875 VARIANT_BOOL b;
5876 switch(rightvt)
5878 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5879 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5880 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5881 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5882 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5883 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5884 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5885 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5886 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5887 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5888 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5889 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5890 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5891 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5892 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5893 case VT_DECIMAL:
5894 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5895 resvt = VT_NULL;
5896 break;
5897 case VT_BSTR:
5898 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5899 if (FAILED(hres)) goto VarImp_Exit;
5900 else if (!b)
5901 V_VT(result) = VT_NULL;
5902 else
5904 V_VT(result) = VT_BOOL;
5905 V_BOOL(result) = b;
5907 goto VarImp_Exit;
5909 if (resvt == VT_NULL)
5911 V_VT(result) = resvt;
5912 goto VarImp_Exit;
5914 else
5916 hres = VariantChangeType(result,right,0,resvt);
5917 goto VarImp_Exit;
5921 /* Special handling is required when NULL is the right variant.
5922 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5924 else if (rightvt == VT_NULL)
5926 VARIANT_BOOL b;
5927 switch(leftvt)
5929 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5930 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5931 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5932 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5933 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5934 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5935 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5936 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5937 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5938 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5939 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5940 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5941 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5942 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5943 case VT_DECIMAL:
5944 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5945 resvt = VT_NULL;
5946 break;
5947 case VT_BSTR:
5948 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5949 if (FAILED(hres)) goto VarImp_Exit;
5950 else if (b == VARIANT_TRUE)
5951 resvt = VT_NULL;
5953 if (resvt == VT_NULL)
5955 V_VT(result) = resvt;
5956 goto VarImp_Exit;
5960 hres = VariantCopy(&lv, left);
5961 if (FAILED(hres)) goto VarImp_Exit;
5963 if (rightvt == VT_NULL)
5965 memset( &rv, 0, sizeof(rv) );
5966 V_VT(&rv) = resvt;
5968 else
5970 hres = VariantCopy(&rv, right);
5971 if (FAILED(hres)) goto VarImp_Exit;
5974 if (V_VT(&lv) == VT_BSTR &&
5975 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5976 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5977 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5978 hres = VariantChangeType(&lv,&lv,0,resvt);
5979 if (FAILED(hres)) goto VarImp_Exit;
5981 if (V_VT(&rv) == VT_BSTR &&
5982 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5983 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5984 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5985 hres = VariantChangeType(&rv, &rv, 0, resvt);
5986 if (FAILED(hres)) goto VarImp_Exit;
5988 /* do the math */
5989 V_VT(result) = resvt;
5990 switch (resvt)
5992 case VT_I8:
5993 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5994 break;
5995 case VT_I4:
5996 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5997 break;
5998 case VT_I2:
5999 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
6000 break;
6001 case VT_UI1:
6002 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
6003 break;
6004 case VT_BOOL:
6005 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
6006 break;
6007 default:
6008 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
6009 leftvt,rightvt);
6012 VarImp_Exit:
6014 VariantClear(&lv);
6015 VariantClear(&rv);
6016 VariantClear(&tempLeft);
6017 VariantClear(&tempRight);
6019 return hres;