Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / oleaut32 / variant.c
blob92a86fd6e7fa7b064d4ba1cab99609134dc9391f
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 = S_OK;
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 if (V_ARRAY(pVarg))
668 hres = SafeArrayDestroy(V_ARRAY(pVarg));
670 else if (V_VT(pVarg) == VT_BSTR)
672 SysFreeString(V_BSTR(pVarg));
674 else if (V_VT(pVarg) == VT_RECORD)
676 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
677 if (pBr->pRecInfo)
679 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
680 IRecordInfo_Release(pBr->pRecInfo);
683 else if (V_VT(pVarg) == VT_DISPATCH ||
684 V_VT(pVarg) == VT_UNKNOWN)
686 if (V_UNKNOWN(pVarg))
687 IUnknown_Release(V_UNKNOWN(pVarg));
690 V_VT(pVarg) = VT_EMPTY;
692 return hres;
695 /******************************************************************************
696 * Copy an IRecordInfo object contained in a variant.
698 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
700 HRESULT hres = S_OK;
702 if (pBr->pRecInfo)
704 ULONG ulSize;
706 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
707 if (SUCCEEDED(hres))
709 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
710 if (!pvRecord)
711 hres = E_OUTOFMEMORY;
712 else
714 memcpy(pvRecord, pBr->pvRecord, ulSize);
715 pBr->pvRecord = pvRecord;
717 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
718 if (SUCCEEDED(hres))
719 IRecordInfo_AddRef(pBr->pRecInfo);
723 else if (pBr->pvRecord)
724 hres = E_INVALIDARG;
725 return hres;
728 /******************************************************************************
729 * VariantCopy [OLEAUT32.10]
731 * Copy a variant.
733 * PARAMS
734 * pvargDest [O] Destination for copy
735 * pvargSrc [I] Source variant to copy
737 * RETURNS
738 * Success: S_OK. pvargDest contains a copy of pvargSrc.
739 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
740 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
741 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
742 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
744 * NOTES
745 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
746 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
747 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
748 * fails, so does this function.
749 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
750 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
751 * is copied rather than just any pointers to it.
752 * - For by-value object types the object pointer is copied and the objects
753 * reference count increased using IUnknown_AddRef().
754 * - For all by-reference types, only the referencing pointer is copied.
756 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
758 HRESULT hres = S_OK;
760 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
761 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
762 debugstr_VF(pvargSrc));
764 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
765 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
766 return DISP_E_BADVARTYPE;
768 if (pvargSrc != pvargDest &&
769 SUCCEEDED(hres = VariantClear(pvargDest)))
771 *pvargDest = *pvargSrc; /* Shallow copy the value */
773 if (!V_ISBYREF(pvargSrc))
775 if (V_ISARRAY(pvargSrc))
777 if (V_ARRAY(pvargSrc))
778 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
780 else if (V_VT(pvargSrc) == VT_BSTR)
782 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
783 if (!V_BSTR(pvargDest))
785 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
786 hres = E_OUTOFMEMORY;
789 else if (V_VT(pvargSrc) == VT_RECORD)
791 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
793 else if (V_VT(pvargSrc) == VT_DISPATCH ||
794 V_VT(pvargSrc) == VT_UNKNOWN)
796 if (V_UNKNOWN(pvargSrc))
797 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
801 return hres;
804 /* Return the byte size of a variants data */
805 static inline size_t VARIANT_DataSize(const VARIANT* pv)
807 switch (V_TYPE(pv))
809 case VT_I1:
810 case VT_UI1: return sizeof(BYTE);
811 case VT_I2:
812 case VT_UI2: return sizeof(SHORT);
813 case VT_INT:
814 case VT_UINT:
815 case VT_I4:
816 case VT_UI4: return sizeof(LONG);
817 case VT_I8:
818 case VT_UI8: return sizeof(LONGLONG);
819 case VT_R4: return sizeof(float);
820 case VT_R8: return sizeof(double);
821 case VT_DATE: return sizeof(DATE);
822 case VT_BOOL: return sizeof(VARIANT_BOOL);
823 case VT_DISPATCH:
824 case VT_UNKNOWN:
825 case VT_BSTR: return sizeof(void*);
826 case VT_CY: return sizeof(CY);
827 case VT_ERROR: return sizeof(SCODE);
829 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
830 return 0;
833 /******************************************************************************
834 * VariantCopyInd [OLEAUT32.11]
836 * Copy a variant, dereferencing it if it is by-reference.
838 * PARAMS
839 * pvargDest [O] Destination for copy
840 * pvargSrc [I] Source variant to copy
842 * RETURNS
843 * Success: S_OK. pvargDest contains a copy of pvargSrc.
844 * Failure: An HRESULT error code indicating the error.
846 * NOTES
847 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
848 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
849 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
850 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
851 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
853 * NOTES
854 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
855 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
856 * value.
857 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
858 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
859 * to it. If clearing pvargDest fails, so does this function.
861 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
863 VARIANTARG vTmp, *pSrc = pvargSrc;
864 VARTYPE vt;
865 HRESULT hres = S_OK;
867 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
868 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
869 debugstr_VF(pvargSrc));
871 if (!V_ISBYREF(pvargSrc))
872 return VariantCopy(pvargDest, pvargSrc);
874 /* Argument checking is more lax than VariantCopy()... */
875 vt = V_TYPE(pvargSrc);
876 if (V_ISARRAY(pvargSrc) ||
877 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
878 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
880 /* OK */
882 else
883 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
885 if (pvargSrc == pvargDest)
887 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
888 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
890 vTmp = *pvargSrc;
891 pSrc = &vTmp;
892 V_VT(pvargDest) = VT_EMPTY;
894 else
896 /* Copy into another variant. Free the variant in pvargDest */
897 if (FAILED(hres = VariantClear(pvargDest)))
899 TRACE("VariantClear() of destination failed\n");
900 return hres;
904 if (V_ISARRAY(pSrc))
906 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
907 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
909 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
911 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
912 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
914 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
916 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
917 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
919 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
920 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
922 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
923 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
924 if (*V_UNKNOWNREF(pSrc))
925 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
927 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
929 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
930 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
931 hres = E_INVALIDARG; /* Don't dereference more than one level */
932 else
933 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
935 /* Use the dereferenced variants type value, not VT_VARIANT */
936 goto VariantCopyInd_Return;
938 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
940 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
941 sizeof(DECIMAL) - sizeof(USHORT));
943 else
945 /* Copy the pointed to data into this variant */
946 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
949 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
951 VariantCopyInd_Return:
953 if (pSrc != pvargSrc)
954 VariantClear(pSrc);
956 TRACE("returning 0x%08x, %p->(%s%s)\n", hres, pvargDest,
957 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
958 return hres;
961 /******************************************************************************
962 * VariantChangeType [OLEAUT32.12]
964 * Change the type of a variant.
966 * PARAMS
967 * pvargDest [O] Destination for the converted variant
968 * pvargSrc [O] Source variant to change the type of
969 * wFlags [I] VARIANT_ flags from "oleauto.h"
970 * vt [I] Variant type to change pvargSrc into
972 * RETURNS
973 * Success: S_OK. pvargDest contains the converted value.
974 * Failure: An HRESULT error code describing the failure.
976 * NOTES
977 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
978 * See VariantChangeTypeEx.
980 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
981 USHORT wFlags, VARTYPE vt)
983 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
986 /******************************************************************************
987 * VariantChangeTypeEx [OLEAUT32.147]
989 * Change the type of a variant.
991 * PARAMS
992 * pvargDest [O] Destination for the converted variant
993 * pvargSrc [O] Source variant to change the type of
994 * lcid [I] LCID for the conversion
995 * wFlags [I] VARIANT_ flags from "oleauto.h"
996 * vt [I] Variant type to change pvargSrc into
998 * RETURNS
999 * Success: S_OK. pvargDest contains the converted value.
1000 * Failure: An HRESULT error code describing the failure.
1002 * NOTES
1003 * pvargDest and pvargSrc can point to the same variant to perform an in-place
1004 * conversion. If the conversion is successful, pvargSrc will be freed.
1006 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
1007 LCID lcid, USHORT wFlags, VARTYPE vt)
1009 HRESULT res = S_OK;
1011 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%04x,%s%s)\n", pvargDest,
1012 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
1013 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
1014 debugstr_vt(vt), debugstr_vf(vt));
1016 if (vt == VT_CLSID)
1017 res = DISP_E_BADVARTYPE;
1018 else
1020 res = VARIANT_ValidateType(V_VT(pvargSrc));
1022 if (SUCCEEDED(res))
1024 res = VARIANT_ValidateType(vt);
1026 if (SUCCEEDED(res))
1028 VARIANTARG vTmp, vSrcDeref;
1030 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
1031 res = DISP_E_TYPEMISMATCH;
1032 else
1034 V_VT(&vTmp) = VT_EMPTY;
1035 V_VT(&vSrcDeref) = VT_EMPTY;
1036 VariantClear(&vTmp);
1037 VariantClear(&vSrcDeref);
1040 if (SUCCEEDED(res))
1042 res = VariantCopyInd(&vSrcDeref, pvargSrc);
1043 if (SUCCEEDED(res))
1045 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1046 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1047 else
1048 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1050 if (SUCCEEDED(res)) {
1051 V_VT(&vTmp) = vt;
1052 VariantCopy(pvargDest, &vTmp);
1054 VariantClear(&vTmp);
1055 VariantClear(&vSrcDeref);
1062 TRACE("returning 0x%08x, %p->(%s%s)\n", res, pvargDest,
1063 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1064 return res;
1067 /* Date Conversions */
1069 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1071 /* Convert a VT_DATE value to a Julian Date */
1072 static inline int VARIANT_JulianFromDate(int dateIn)
1074 int julianDays = dateIn;
1076 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1077 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1078 return julianDays;
1081 /* Convert a Julian Date to a VT_DATE value */
1082 static inline int VARIANT_DateFromJulian(int dateIn)
1084 int julianDays = dateIn;
1086 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1087 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1088 return julianDays;
1091 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1092 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1094 int j, i, l, n;
1096 l = jd + 68569;
1097 n = l * 4 / 146097;
1098 l -= (n * 146097 + 3) / 4;
1099 i = (4000 * (l + 1)) / 1461001;
1100 l += 31 - (i * 1461) / 4;
1101 j = (l * 80) / 2447;
1102 *day = l - (j * 2447) / 80;
1103 l = j / 11;
1104 *month = (j + 2) - (12 * l);
1105 *year = 100 * (n - 49) + i + l;
1108 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1109 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1111 int m12 = (month - 14) / 12;
1113 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1114 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1117 /* Macros for accessing DOS format date/time fields */
1118 #define DOS_YEAR(x) (1980 + (x >> 9))
1119 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1120 #define DOS_DAY(x) (x & 0x1f)
1121 #define DOS_HOUR(x) (x >> 11)
1122 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1123 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1124 /* Create a DOS format date/time */
1125 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1126 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1128 /* Roll a date forwards or backwards to correct it */
1129 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1131 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1132 short iYear, iMonth, iDay, iHour, iMinute, iSecond;
1134 /* interpret values signed */
1135 iYear = lpUd->st.wYear;
1136 iMonth = lpUd->st.wMonth;
1137 iDay = lpUd->st.wDay;
1138 iHour = lpUd->st.wHour;
1139 iMinute = lpUd->st.wMinute;
1140 iSecond = lpUd->st.wSecond;
1142 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay, iMonth,
1143 iYear, iHour, iMinute, iSecond);
1145 if (iYear > 9999 || iYear < -9999)
1146 return E_INVALIDARG; /* Invalid value */
1147 /* Years < 100 are treated as 1900 + year */
1148 if (iYear > 0 && iYear < 100)
1149 iYear += 1900;
1151 iMinute += iSecond / 60;
1152 iSecond = iSecond % 60;
1153 iHour += iMinute / 60;
1154 iMinute = iMinute % 60;
1155 iDay += iHour / 24;
1156 iHour = iHour % 24;
1157 iYear += iMonth / 12;
1158 iMonth = iMonth % 12;
1159 if (iMonth<=0) {iMonth+=12; iYear--;}
1160 while (iDay > days[iMonth])
1162 if (iMonth == 2 && IsLeapYear(iYear))
1163 iDay -= 29;
1164 else
1165 iDay -= days[iMonth];
1166 iMonth++;
1167 iYear += iMonth / 12;
1168 iMonth = iMonth % 12;
1170 while (iDay <= 0)
1172 iMonth--;
1173 if (iMonth<=0) {iMonth+=12; iYear--;}
1174 if (iMonth == 2 && IsLeapYear(iYear))
1175 iDay += 29;
1176 else
1177 iDay += days[iMonth];
1180 if (iSecond<0){iSecond+=60; iMinute--;}
1181 if (iMinute<0){iMinute+=60; iHour--;}
1182 if (iHour<0) {iHour+=24; iDay--;}
1183 if (iYear<=0) iYear+=2000;
1185 lpUd->st.wYear = iYear;
1186 lpUd->st.wMonth = iMonth;
1187 lpUd->st.wDay = iDay;
1188 lpUd->st.wHour = iHour;
1189 lpUd->st.wMinute = iMinute;
1190 lpUd->st.wSecond = iSecond;
1192 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1193 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1194 return S_OK;
1197 /**********************************************************************
1198 * DosDateTimeToVariantTime [OLEAUT32.14]
1200 * Convert a Dos format date and time into variant VT_DATE format.
1202 * PARAMS
1203 * wDosDate [I] Dos format date
1204 * wDosTime [I] Dos format time
1205 * pDateOut [O] Destination for VT_DATE format
1207 * RETURNS
1208 * Success: TRUE. pDateOut contains the converted time.
1209 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1211 * NOTES
1212 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1213 * - Dos format times are accurate to only 2 second precision.
1214 * - The format of a Dos Date is:
1215 *| Bits Values Meaning
1216 *| ---- ------ -------
1217 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1218 *| the days in the month rolls forward the extra days.
1219 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1220 *| year. 13-15 are invalid.
1221 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1222 * - The format of a Dos Time is:
1223 *| Bits Values Meaning
1224 *| ---- ------ -------
1225 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1226 *| 5-10 0-59 Minutes. 60-63 are invalid.
1227 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1229 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1230 double *pDateOut)
1232 UDATE ud;
1234 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1235 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1236 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1237 pDateOut);
1239 ud.st.wYear = DOS_YEAR(wDosDate);
1240 ud.st.wMonth = DOS_MONTH(wDosDate);
1241 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1242 return FALSE;
1243 ud.st.wDay = DOS_DAY(wDosDate);
1244 ud.st.wHour = DOS_HOUR(wDosTime);
1245 ud.st.wMinute = DOS_MINUTE(wDosTime);
1246 ud.st.wSecond = DOS_SECOND(wDosTime);
1247 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1248 if (ud.st.wHour > 23 || ud.st.wMinute > 59 || ud.st.wSecond > 59)
1249 return FALSE; /* Invalid values in Dos*/
1251 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1254 /**********************************************************************
1255 * VariantTimeToDosDateTime [OLEAUT32.13]
1257 * Convert a variant format date into a Dos format date and time.
1259 * dateIn [I] VT_DATE time format
1260 * pwDosDate [O] Destination for Dos format date
1261 * pwDosTime [O] Destination for Dos format time
1263 * RETURNS
1264 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1265 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1267 * NOTES
1268 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1270 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1272 UDATE ud;
1274 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1276 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1277 return FALSE;
1279 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1280 return FALSE;
1282 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1283 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1285 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1286 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1287 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1288 return TRUE;
1291 /***********************************************************************
1292 * SystemTimeToVariantTime [OLEAUT32.184]
1294 * Convert a System format date and time into variant VT_DATE format.
1296 * PARAMS
1297 * lpSt [I] System format date and time
1298 * pDateOut [O] Destination for VT_DATE format date
1300 * RETURNS
1301 * Success: TRUE. *pDateOut contains the converted value.
1302 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1304 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1306 UDATE ud;
1308 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1309 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1311 if (lpSt->wMonth > 12)
1312 return FALSE;
1314 ud.st = *lpSt;
1315 return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1318 /***********************************************************************
1319 * VariantTimeToSystemTime [OLEAUT32.185]
1321 * Convert a variant VT_DATE into a System format date and time.
1323 * PARAMS
1324 * datein [I] Variant VT_DATE format date
1325 * lpSt [O] Destination for System format date and time
1327 * RETURNS
1328 * Success: TRUE. *lpSt contains the converted value.
1329 * Failure: FALSE, if dateIn is too large or small.
1331 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1333 UDATE ud;
1335 TRACE("(%g,%p)\n", dateIn, lpSt);
1337 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1338 return FALSE;
1340 *lpSt = ud.st;
1341 return TRUE;
1344 /***********************************************************************
1345 * VarDateFromUdateEx [OLEAUT32.319]
1347 * Convert an unpacked format date and time to a variant VT_DATE.
1349 * PARAMS
1350 * pUdateIn [I] Unpacked format date and time to convert
1351 * lcid [I] Locale identifier for the conversion
1352 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1353 * pDateOut [O] Destination for variant VT_DATE.
1355 * RETURNS
1356 * Success: S_OK. *pDateOut contains the converted value.
1357 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1359 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1361 UDATE ud;
1362 double dateVal, dateSign;
1364 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1365 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1366 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1367 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1368 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1370 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1371 FIXME("lcid possibly not handled, treating as en-us\n");
1373 ud = *pUdateIn;
1375 if (dwFlags & VAR_VALIDDATE)
1376 WARN("Ignoring VAR_VALIDDATE\n");
1378 if (FAILED(VARIANT_RollUdate(&ud)))
1379 return E_INVALIDARG;
1381 /* Date */
1382 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1384 /* Sign */
1385 dateSign = (dateVal < 0.0) ? -1.0 : 1.0;
1387 /* Time */
1388 dateVal += ud.st.wHour / 24.0 * dateSign;
1389 dateVal += ud.st.wMinute / 1440.0 * dateSign;
1390 dateVal += ud.st.wSecond / 86400.0 * dateSign;
1392 TRACE("Returning %g\n", dateVal);
1393 *pDateOut = dateVal;
1394 return S_OK;
1397 /***********************************************************************
1398 * VarDateFromUdate [OLEAUT32.330]
1400 * Convert an unpacked format date and time to a variant VT_DATE.
1402 * PARAMS
1403 * pUdateIn [I] Unpacked format date and time to convert
1404 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1405 * pDateOut [O] Destination for variant VT_DATE.
1407 * RETURNS
1408 * Success: S_OK. *pDateOut contains the converted value.
1409 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1411 * NOTES
1412 * This function uses the United States English locale for the conversion. Use
1413 * VarDateFromUdateEx() for alternate locales.
1415 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1417 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1419 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1422 /***********************************************************************
1423 * VarUdateFromDate [OLEAUT32.331]
1425 * Convert a variant VT_DATE into an unpacked format date and time.
1427 * PARAMS
1428 * datein [I] Variant VT_DATE format date
1429 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1430 * lpUdate [O] Destination for unpacked format date and time
1432 * RETURNS
1433 * Success: S_OK. *lpUdate contains the converted value.
1434 * Failure: E_INVALIDARG, if dateIn is too large or small.
1436 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1438 /* Cumulative totals of days per month */
1439 static const USHORT cumulativeDays[] =
1441 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1443 double datePart, timePart;
1444 int julianDays;
1446 TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1448 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1449 return E_INVALIDARG;
1451 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1452 /* Compensate for int truncation (always downwards) */
1453 timePart = fabs(dateIn - datePart) + 0.00000000001;
1454 if (timePart >= 1.0)
1455 timePart -= 0.00000000001;
1457 /* Date */
1458 julianDays = VARIANT_JulianFromDate(dateIn);
1459 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1460 &lpUdate->st.wDay);
1462 datePart = (datePart + 1.5) / 7.0;
1463 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1464 if (lpUdate->st.wDayOfWeek == 0)
1465 lpUdate->st.wDayOfWeek = 5;
1466 else if (lpUdate->st.wDayOfWeek == 1)
1467 lpUdate->st.wDayOfWeek = 6;
1468 else
1469 lpUdate->st.wDayOfWeek -= 2;
1471 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1472 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1473 else
1474 lpUdate->wDayOfYear = 0;
1476 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1477 lpUdate->wDayOfYear += lpUdate->st.wDay;
1479 /* Time */
1480 timePart *= 24.0;
1481 lpUdate->st.wHour = timePart;
1482 timePart -= lpUdate->st.wHour;
1483 timePart *= 60.0;
1484 lpUdate->st.wMinute = timePart;
1485 timePart -= lpUdate->st.wMinute;
1486 timePart *= 60.0;
1487 lpUdate->st.wSecond = timePart;
1488 timePart -= lpUdate->st.wSecond;
1489 lpUdate->st.wMilliseconds = 0;
1490 if (timePart > 0.5)
1492 /* Round the milliseconds, adjusting the time/date forward if needed */
1493 if (lpUdate->st.wSecond < 59)
1494 lpUdate->st.wSecond++;
1495 else
1497 lpUdate->st.wSecond = 0;
1498 if (lpUdate->st.wMinute < 59)
1499 lpUdate->st.wMinute++;
1500 else
1502 lpUdate->st.wMinute = 0;
1503 if (lpUdate->st.wHour < 23)
1504 lpUdate->st.wHour++;
1505 else
1507 lpUdate->st.wHour = 0;
1508 /* Roll over a whole day */
1509 if (++lpUdate->st.wDay > 28)
1510 VARIANT_RollUdate(lpUdate);
1515 return S_OK;
1518 #define GET_NUMBER_TEXT(fld,name) \
1519 buff[0] = 0; \
1520 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1521 WARN("buffer too small for " #fld "\n"); \
1522 else \
1523 if (buff[0]) lpChars->name = buff[0]; \
1524 TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1526 /* Get the valid number characters for an lcid */
1527 static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1529 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1530 static CRITICAL_SECTION csLastChars = { NULL, -1, 0, 0, 0, 0 };
1531 static VARIANT_NUMBER_CHARS lastChars;
1532 static LCID lastLcid = -1;
1533 static DWORD lastFlags = 0;
1534 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1535 WCHAR buff[4];
1537 /* To make caching thread-safe, a critical section is needed */
1538 EnterCriticalSection(&csLastChars);
1540 /* Asking for default locale entries is very expensive: It is a registry
1541 server call. So cache one locally, as Microsoft does it too */
1542 if(lcid == lastLcid && dwFlags == lastFlags)
1544 memcpy(lpChars, &lastChars, sizeof(defaultChars));
1545 LeaveCriticalSection(&csLastChars);
1546 return;
1549 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1550 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1551 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1552 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1553 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1554 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1555 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1557 /* Local currency symbols are often 2 characters */
1558 lpChars->cCurrencyLocal2 = '\0';
1559 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1561 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1562 case 2: lpChars->cCurrencyLocal = buff[0];
1563 break;
1564 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1566 TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1567 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1569 memcpy(&lastChars, lpChars, sizeof(defaultChars));
1570 lastLcid = lcid;
1571 lastFlags = dwFlags;
1572 LeaveCriticalSection(&csLastChars);
1575 /* Number Parsing States */
1576 #define B_PROCESSING_EXPONENT 0x1
1577 #define B_NEGATIVE_EXPONENT 0x2
1578 #define B_EXPONENT_START 0x4
1579 #define B_INEXACT_ZEROS 0x8
1580 #define B_LEADING_ZERO 0x10
1581 #define B_PROCESSING_HEX 0x20
1582 #define B_PROCESSING_OCT 0x40
1584 /**********************************************************************
1585 * VarParseNumFromStr [OLEAUT32.46]
1587 * Parse a string containing a number into a NUMPARSE structure.
1589 * PARAMS
1590 * lpszStr [I] String to parse number from
1591 * lcid [I] Locale Id for the conversion
1592 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1593 * pNumprs [I/O] Destination for parsed number
1594 * rgbDig [O] Destination for digits read in
1596 * RETURNS
1597 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1598 * the number.
1599 * Failure: E_INVALIDARG, if any parameter is invalid.
1600 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1601 * incorrectly.
1602 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1604 * NOTES
1605 * pNumprs must have the following fields set:
1606 * cDig: Set to the size of rgbDig.
1607 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1608 * from "oleauto.h".
1610 * FIXME
1611 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1612 * numerals, so this has not been implemented.
1614 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1615 NUMPARSE *pNumprs, BYTE *rgbDig)
1617 VARIANT_NUMBER_CHARS chars;
1618 BYTE rgbTmp[1024];
1619 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1620 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1621 int cchUsed = 0;
1623 TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1625 if (!pNumprs || !rgbDig)
1626 return E_INVALIDARG;
1628 if (pNumprs->cDig < iMaxDigits)
1629 iMaxDigits = pNumprs->cDig;
1631 pNumprs->cDig = 0;
1632 pNumprs->dwOutFlags = 0;
1633 pNumprs->cchUsed = 0;
1634 pNumprs->nBaseShift = 0;
1635 pNumprs->nPwr10 = 0;
1637 if (!lpszStr)
1638 return DISP_E_TYPEMISMATCH;
1640 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1642 /* First consume all the leading symbols and space from the string */
1643 while (1)
1645 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1647 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1650 cchUsed++;
1651 lpszStr++;
1652 } while (isspaceW(*lpszStr));
1654 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1655 *lpszStr == chars.cPositiveSymbol &&
1656 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1658 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1659 cchUsed++;
1660 lpszStr++;
1662 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1663 *lpszStr == chars.cNegativeSymbol &&
1664 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1666 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1667 cchUsed++;
1668 lpszStr++;
1670 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1671 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1672 *lpszStr == chars.cCurrencyLocal &&
1673 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1675 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1676 cchUsed++;
1677 lpszStr++;
1678 /* Only accept currency characters */
1679 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1680 chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1682 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1683 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1685 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1686 cchUsed++;
1687 lpszStr++;
1689 else
1690 break;
1693 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1695 /* Only accept non-currency characters */
1696 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1697 chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1700 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1701 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1703 dwState |= B_PROCESSING_HEX;
1704 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1705 cchUsed=cchUsed+2;
1706 lpszStr=lpszStr+2;
1708 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1709 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1711 dwState |= B_PROCESSING_OCT;
1712 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1713 cchUsed=cchUsed+2;
1714 lpszStr=lpszStr+2;
1717 /* Strip Leading zeros */
1718 while (*lpszStr == '0')
1720 dwState |= B_LEADING_ZERO;
1721 cchUsed++;
1722 lpszStr++;
1725 while (*lpszStr)
1727 if (isdigitW(*lpszStr))
1729 if (dwState & B_PROCESSING_EXPONENT)
1731 int exponentSize = 0;
1732 if (dwState & B_EXPONENT_START)
1734 if (!isdigitW(*lpszStr))
1735 break; /* No exponent digits - invalid */
1736 while (*lpszStr == '0')
1738 /* Skip leading zero's in the exponent */
1739 cchUsed++;
1740 lpszStr++;
1744 while (isdigitW(*lpszStr))
1746 exponentSize *= 10;
1747 exponentSize += *lpszStr - '0';
1748 cchUsed++;
1749 lpszStr++;
1751 if (dwState & B_NEGATIVE_EXPONENT)
1752 exponentSize = -exponentSize;
1753 /* Add the exponent into the powers of 10 */
1754 pNumprs->nPwr10 += exponentSize;
1755 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1756 lpszStr--; /* back up to allow processing of next char */
1758 else
1760 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1761 && !(dwState & B_PROCESSING_OCT))
1763 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1765 if (*lpszStr != '0')
1766 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1768 /* This digit can't be represented, but count it in nPwr10 */
1769 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1770 pNumprs->nPwr10--;
1771 else
1772 pNumprs->nPwr10++;
1774 else
1776 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1777 return DISP_E_TYPEMISMATCH;
1780 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1781 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1783 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1785 pNumprs->cDig++;
1786 cchUsed++;
1789 else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1791 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1792 cchUsed++;
1794 else if (*lpszStr == chars.cDecimalPoint &&
1795 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1796 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1798 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1799 cchUsed++;
1801 /* If we have no digits so far, skip leading zeros */
1802 if (!pNumprs->cDig)
1804 while (lpszStr[1] == '0')
1806 dwState |= B_LEADING_ZERO;
1807 cchUsed++;
1808 lpszStr++;
1809 pNumprs->nPwr10--;
1813 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1814 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1815 dwState & B_PROCESSING_HEX)
1817 if (pNumprs->cDig >= iMaxDigits)
1819 return DISP_E_OVERFLOW;
1821 else
1823 if (*lpszStr >= 'a')
1824 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1825 else
1826 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1828 pNumprs->cDig++;
1829 cchUsed++;
1831 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1832 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1833 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1835 dwState |= B_PROCESSING_EXPONENT;
1836 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1837 cchUsed++;
1839 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1841 cchUsed++; /* Ignore positive exponent */
1843 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1845 dwState |= B_NEGATIVE_EXPONENT;
1846 cchUsed++;
1848 else
1849 break; /* Stop at an unrecognised character */
1851 lpszStr++;
1854 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1856 /* Ensure a 0 on its own gets stored */
1857 pNumprs->cDig = 1;
1858 rgbTmp[0] = 0;
1861 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1863 pNumprs->cchUsed = cchUsed;
1864 WARN("didn't completely parse exponent\n");
1865 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1868 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1870 if (dwState & B_INEXACT_ZEROS)
1871 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1872 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1874 /* copy all of the digits into the output digit buffer */
1875 /* this is exactly what windows does although it also returns */
1876 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1877 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1879 if (dwState & B_PROCESSING_HEX) {
1880 /* hex numbers have always the same format */
1881 pNumprs->nPwr10=0;
1882 pNumprs->nBaseShift=4;
1883 } else {
1884 if (dwState & B_PROCESSING_OCT) {
1885 /* oct numbers have always the same format */
1886 pNumprs->nPwr10=0;
1887 pNumprs->nBaseShift=3;
1888 } else {
1889 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1891 pNumprs->nPwr10++;
1892 pNumprs->cDig--;
1896 } else
1898 /* Remove trailing zeros from the last (whole number or decimal) part */
1899 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1901 pNumprs->nPwr10++;
1902 pNumprs->cDig--;
1906 if (pNumprs->cDig <= iMaxDigits)
1907 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1908 else
1909 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1911 /* Copy the digits we processed into rgbDig */
1912 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1914 /* Consume any trailing symbols and space */
1915 while (1)
1917 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1919 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1922 cchUsed++;
1923 lpszStr++;
1924 } while (isspaceW(*lpszStr));
1926 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1927 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1928 *lpszStr == chars.cPositiveSymbol)
1930 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1931 cchUsed++;
1932 lpszStr++;
1934 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1935 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1936 *lpszStr == chars.cNegativeSymbol)
1938 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1939 cchUsed++;
1940 lpszStr++;
1942 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1943 pNumprs->dwOutFlags & NUMPRS_PARENS)
1945 cchUsed++;
1946 lpszStr++;
1947 pNumprs->dwOutFlags |= NUMPRS_NEG;
1949 else
1950 break;
1953 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1955 pNumprs->cchUsed = cchUsed;
1956 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1959 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1960 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1962 if (!pNumprs->cDig)
1963 return DISP_E_TYPEMISMATCH; /* No Number found */
1965 pNumprs->cchUsed = cchUsed;
1966 return S_OK;
1969 /* VTBIT flags indicating an integer value */
1970 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1971 /* VTBIT flags indicating a real number value */
1972 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1974 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1975 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1976 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1977 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1979 /**********************************************************************
1980 * VarNumFromParseNum [OLEAUT32.47]
1982 * Convert a NUMPARSE structure into a numeric Variant type.
1984 * PARAMS
1985 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1986 * rgbDig [I] Source for the numbers digits
1987 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1988 * pVarDst [O] Destination for the converted Variant value.
1990 * RETURNS
1991 * Success: S_OK. pVarDst contains the converted value.
1992 * Failure: E_INVALIDARG, if any parameter is invalid.
1993 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1995 * NOTES
1996 * - The smallest favoured type present in dwVtBits that can represent the
1997 * number in pNumprs without losing precision is used.
1998 * - Signed types are preferred over unsigned types of the same size.
1999 * - Preferred types in order are: integer, float, double, currency then decimal.
2000 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
2001 * for details of the rounding method.
2002 * - pVarDst is not cleared before the result is stored in it.
2003 * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
2004 * design?): If some other VTBIT's for integers are specified together
2005 * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
2006 * the number to the smallest requested integer truncating this way the
2007 * number. Wine doesn't implement this "feature" (yet?).
2009 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
2010 ULONG dwVtBits, VARIANT *pVarDst)
2012 /* Scale factors and limits for double arithmetic */
2013 static const double dblMultipliers[11] = {
2014 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2015 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2017 static const double dblMinimums[11] = {
2018 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
2019 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
2020 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
2022 static const double dblMaximums[11] = {
2023 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
2024 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
2025 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
2028 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
2030 TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
2032 if (pNumprs->nBaseShift)
2034 /* nBaseShift indicates a hex or octal number */
2035 ULONG64 ul64 = 0;
2036 LONG64 l64;
2037 int i;
2039 /* Convert the hex or octal number string into a UI64 */
2040 for (i = 0; i < pNumprs->cDig; i++)
2042 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
2044 TRACE("Overflow multiplying digits\n");
2045 return DISP_E_OVERFLOW;
2047 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
2050 /* also make a negative representation */
2051 l64=-ul64;
2053 /* Try signed and unsigned types in size order */
2054 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
2056 V_VT(pVarDst) = VT_I1;
2057 V_I1(pVarDst) = ul64;
2058 return S_OK;
2060 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2062 V_VT(pVarDst) = VT_UI1;
2063 V_UI1(pVarDst) = ul64;
2064 return S_OK;
2066 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2068 V_VT(pVarDst) = VT_I2;
2069 V_I2(pVarDst) = ul64;
2070 return S_OK;
2072 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2074 V_VT(pVarDst) = VT_UI2;
2075 V_UI2(pVarDst) = ul64;
2076 return S_OK;
2078 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2080 V_VT(pVarDst) = VT_I4;
2081 V_I4(pVarDst) = ul64;
2082 return S_OK;
2084 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2086 V_VT(pVarDst) = VT_UI4;
2087 V_UI4(pVarDst) = ul64;
2088 return S_OK;
2090 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2092 V_VT(pVarDst) = VT_I8;
2093 V_I8(pVarDst) = ul64;
2094 return S_OK;
2096 else if (dwVtBits & VTBIT_UI8)
2098 V_VT(pVarDst) = VT_UI8;
2099 V_UI8(pVarDst) = ul64;
2100 return S_OK;
2102 else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2104 V_VT(pVarDst) = VT_DECIMAL;
2105 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2106 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2107 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2108 return S_OK;
2110 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2112 V_VT(pVarDst) = VT_R4;
2113 if (ul64 <= I4_MAX)
2114 V_R4(pVarDst) = ul64;
2115 else
2116 V_R4(pVarDst) = l64;
2117 return S_OK;
2119 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2121 V_VT(pVarDst) = VT_R8;
2122 if (ul64 <= I4_MAX)
2123 V_R8(pVarDst) = ul64;
2124 else
2125 V_R8(pVarDst) = l64;
2126 return S_OK;
2129 TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2130 return DISP_E_OVERFLOW;
2133 /* Count the number of relevant fractional and whole digits stored,
2134 * And compute the divisor/multiplier to scale the number by.
2136 if (pNumprs->nPwr10 < 0)
2138 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2140 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2141 wholeNumberDigits = 0;
2142 fractionalDigits = pNumprs->cDig;
2143 divisor10 = -pNumprs->nPwr10;
2145 else
2147 /* An exactly represented real number e.g. 1.024 */
2148 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2149 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2150 divisor10 = pNumprs->cDig - wholeNumberDigits;
2153 else if (pNumprs->nPwr10 == 0)
2155 /* An exactly represented whole number e.g. 1024 */
2156 wholeNumberDigits = pNumprs->cDig;
2157 fractionalDigits = 0;
2159 else /* pNumprs->nPwr10 > 0 */
2161 /* A whole number followed by nPwr10 0's e.g. 102400 */
2162 wholeNumberDigits = pNumprs->cDig;
2163 fractionalDigits = 0;
2164 multiplier10 = pNumprs->nPwr10;
2167 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2168 pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2169 multiplier10, divisor10);
2171 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2172 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2174 /* We have one or more integer output choices, and either:
2175 * 1) An integer input value, or
2176 * 2) A real number input value but no floating output choices.
2177 * Alternately, we have a DECIMAL output available and an integer input.
2179 * So, place the integer value into pVarDst, using the smallest type
2180 * possible and preferring signed over unsigned types.
2182 BOOL bOverflow = FALSE, bNegative;
2183 ULONG64 ul64 = 0;
2184 int i;
2186 /* Convert the integer part of the number into a UI8 */
2187 for (i = 0; i < wholeNumberDigits; i++)
2189 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2191 TRACE("Overflow multiplying digits\n");
2192 bOverflow = TRUE;
2193 break;
2195 ul64 = ul64 * 10 + rgbDig[i];
2198 /* Account for the scale of the number */
2199 if (!bOverflow && multiplier10)
2201 for (i = 0; i < multiplier10; i++)
2203 if (ul64 > (UI8_MAX / 10))
2205 TRACE("Overflow scaling number\n");
2206 bOverflow = TRUE;
2207 break;
2209 ul64 = ul64 * 10;
2213 /* If we have any fractional digits, round the value.
2214 * Note we don't have to do this if divisor10 is < 1,
2215 * because this means the fractional part must be < 0.5
2217 if (!bOverflow && fractionalDigits && divisor10 > 0)
2219 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2220 BOOL bAdjust = FALSE;
2222 TRACE("first decimal value is %d\n", *fracDig);
2224 if (*fracDig > 5)
2225 bAdjust = TRUE; /* > 0.5 */
2226 else if (*fracDig == 5)
2228 for (i = 1; i < fractionalDigits; i++)
2230 if (fracDig[i])
2232 bAdjust = TRUE; /* > 0.5 */
2233 break;
2236 /* If exactly 0.5, round only odd values */
2237 if (i == fractionalDigits && (ul64 & 1))
2238 bAdjust = TRUE;
2241 if (bAdjust)
2243 if (ul64 == UI8_MAX)
2245 TRACE("Overflow after rounding\n");
2246 bOverflow = TRUE;
2248 ul64++;
2252 /* Zero is not a negative number */
2253 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2255 TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2257 /* For negative integers, try the signed types in size order */
2258 if (!bOverflow && bNegative)
2260 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2262 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2264 V_VT(pVarDst) = VT_I1;
2265 V_I1(pVarDst) = -ul64;
2266 return S_OK;
2268 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2270 V_VT(pVarDst) = VT_I2;
2271 V_I2(pVarDst) = -ul64;
2272 return S_OK;
2274 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2276 V_VT(pVarDst) = VT_I4;
2277 V_I4(pVarDst) = -ul64;
2278 return S_OK;
2280 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2282 V_VT(pVarDst) = VT_I8;
2283 V_I8(pVarDst) = -ul64;
2284 return S_OK;
2286 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2288 /* Decimal is only output choice left - fast path */
2289 V_VT(pVarDst) = VT_DECIMAL;
2290 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2291 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2292 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2293 return S_OK;
2297 else if (!bOverflow)
2299 /* For positive integers, try signed then unsigned types in size order */
2300 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2302 V_VT(pVarDst) = VT_I1;
2303 V_I1(pVarDst) = ul64;
2304 return S_OK;
2306 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2308 V_VT(pVarDst) = VT_UI1;
2309 V_UI1(pVarDst) = ul64;
2310 return S_OK;
2312 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2314 V_VT(pVarDst) = VT_I2;
2315 V_I2(pVarDst) = ul64;
2316 return S_OK;
2318 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2320 V_VT(pVarDst) = VT_UI2;
2321 V_UI2(pVarDst) = ul64;
2322 return S_OK;
2324 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2326 V_VT(pVarDst) = VT_I4;
2327 V_I4(pVarDst) = ul64;
2328 return S_OK;
2330 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2332 V_VT(pVarDst) = VT_UI4;
2333 V_UI4(pVarDst) = ul64;
2334 return S_OK;
2336 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2338 V_VT(pVarDst) = VT_I8;
2339 V_I8(pVarDst) = ul64;
2340 return S_OK;
2342 else if (dwVtBits & VTBIT_UI8)
2344 V_VT(pVarDst) = VT_UI8;
2345 V_UI8(pVarDst) = ul64;
2346 return S_OK;
2348 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2350 /* Decimal is only output choice left - fast path */
2351 V_VT(pVarDst) = VT_DECIMAL;
2352 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2353 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2354 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2355 return S_OK;
2360 if (dwVtBits & REAL_VTBITS)
2362 /* Try to put the number into a float or real */
2363 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2364 double whole = 0.0;
2365 int i;
2367 /* Convert the number into a double */
2368 for (i = 0; i < pNumprs->cDig; i++)
2369 whole = whole * 10.0 + rgbDig[i];
2371 TRACE("Whole double value is %16.16g\n", whole);
2373 /* Account for the scale */
2374 while (multiplier10 > 10)
2376 if (whole > dblMaximums[10])
2378 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2379 bOverflow = TRUE;
2380 break;
2382 whole = whole * dblMultipliers[10];
2383 multiplier10 -= 10;
2385 if (multiplier10 && !bOverflow)
2387 if (whole > dblMaximums[multiplier10])
2389 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2390 bOverflow = TRUE;
2392 else
2393 whole = whole * dblMultipliers[multiplier10];
2396 if (!bOverflow)
2397 TRACE("Scaled double value is %16.16g\n", whole);
2399 while (divisor10 > 10 && !bOverflow)
2401 if (whole < dblMinimums[10] && whole != 0)
2403 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2404 bOverflow = TRUE;
2405 break;
2407 whole = whole / dblMultipliers[10];
2408 divisor10 -= 10;
2410 if (divisor10 && !bOverflow)
2412 if (whole < dblMinimums[divisor10] && whole != 0)
2414 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2415 bOverflow = TRUE;
2417 else
2418 whole = whole / dblMultipliers[divisor10];
2420 if (!bOverflow)
2421 TRACE("Final double value is %16.16g\n", whole);
2423 if (dwVtBits & VTBIT_R4 &&
2424 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2426 TRACE("Set R4 to final value\n");
2427 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2428 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2429 return S_OK;
2432 if (dwVtBits & VTBIT_R8)
2434 TRACE("Set R8 to final value\n");
2435 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2436 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2437 return S_OK;
2440 if (dwVtBits & VTBIT_CY)
2442 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2444 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2445 TRACE("Set CY to final value\n");
2446 return S_OK;
2448 TRACE("Value Overflows CY\n");
2452 if (dwVtBits & VTBIT_DECIMAL)
2454 int i;
2455 ULONG carry;
2456 ULONG64 tmp;
2457 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2459 DECIMAL_SETZERO(*pDec);
2460 DEC_LO32(pDec) = 0;
2462 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2463 DEC_SIGN(pDec) = DECIMAL_NEG;
2464 else
2465 DEC_SIGN(pDec) = DECIMAL_POS;
2467 /* Factor the significant digits */
2468 for (i = 0; i < pNumprs->cDig; i++)
2470 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2471 carry = (ULONG)(tmp >> 32);
2472 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2473 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2474 carry = (ULONG)(tmp >> 32);
2475 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2476 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2477 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2479 if (tmp >> 32 & UI4_MAX)
2481 VarNumFromParseNum_DecOverflow:
2482 TRACE("Overflow\n");
2483 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2484 return DISP_E_OVERFLOW;
2488 /* Account for the scale of the number */
2489 while (multiplier10 > 0)
2491 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2492 carry = (ULONG)(tmp >> 32);
2493 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2494 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2495 carry = (ULONG)(tmp >> 32);
2496 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2497 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2498 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2500 if (tmp >> 32 & UI4_MAX)
2501 goto VarNumFromParseNum_DecOverflow;
2502 multiplier10--;
2504 DEC_SCALE(pDec) = divisor10;
2506 V_VT(pVarDst) = VT_DECIMAL;
2507 return S_OK;
2509 return DISP_E_OVERFLOW; /* No more output choices */
2512 /**********************************************************************
2513 * VarCat [OLEAUT32.318]
2515 * Concatenates one variant onto another.
2517 * PARAMS
2518 * left [I] First variant
2519 * right [I] Second variant
2520 * result [O] Result variant
2522 * RETURNS
2523 * Success: S_OK.
2524 * Failure: An HRESULT error code indicating the error.
2526 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2528 VARTYPE leftvt,rightvt,resultvt;
2529 HRESULT hres;
2530 static WCHAR str_true[32];
2531 static WCHAR str_false[32];
2532 static const WCHAR sz_empty[] = {'\0'};
2533 leftvt = V_VT(left);
2534 rightvt = V_VT(right);
2536 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2537 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2539 if (!str_true[0]) {
2540 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_FALSE, str_false);
2541 VARIANT_GetLocalisedText(LOCALE_USER_DEFAULT, IDS_TRUE, str_true);
2544 /* when both left and right are NULL the result is NULL */
2545 if (leftvt == VT_NULL && rightvt == VT_NULL)
2547 V_VT(out) = VT_NULL;
2548 return S_OK;
2551 hres = S_OK;
2552 resultvt = VT_EMPTY;
2554 /* There are many special case for errors and return types */
2555 if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2556 rightvt == VT_DATE || rightvt == VT_DECIMAL))
2557 hres = DISP_E_TYPEMISMATCH;
2558 else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2559 leftvt == VT_R4 || leftvt == VT_R8 ||
2560 leftvt == VT_CY || leftvt == VT_BOOL ||
2561 leftvt == VT_BSTR || leftvt == VT_I1 ||
2562 leftvt == VT_UI1 || leftvt == VT_UI2 ||
2563 leftvt == VT_UI4 || leftvt == VT_I8 ||
2564 leftvt == VT_UI8 || leftvt == VT_INT ||
2565 leftvt == VT_UINT || leftvt == VT_EMPTY ||
2566 leftvt == VT_NULL || leftvt == VT_DATE ||
2567 leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2569 (rightvt == VT_I2 || rightvt == VT_I4 ||
2570 rightvt == VT_R4 || rightvt == VT_R8 ||
2571 rightvt == VT_CY || rightvt == VT_BOOL ||
2572 rightvt == VT_BSTR || rightvt == VT_I1 ||
2573 rightvt == VT_UI1 || rightvt == VT_UI2 ||
2574 rightvt == VT_UI4 || rightvt == VT_I8 ||
2575 rightvt == VT_UI8 || rightvt == VT_INT ||
2576 rightvt == VT_UINT || rightvt == VT_EMPTY ||
2577 rightvt == VT_NULL || rightvt == VT_DATE ||
2578 rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2579 resultvt = VT_BSTR;
2580 else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2581 hres = DISP_E_TYPEMISMATCH;
2582 else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2583 rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2584 hres = DISP_E_TYPEMISMATCH;
2585 else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2586 rightvt == VT_DECIMAL)
2587 hres = DISP_E_BADVARTYPE;
2588 else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2589 hres = DISP_E_TYPEMISMATCH;
2590 else if (leftvt == VT_VARIANT)
2591 hres = DISP_E_TYPEMISMATCH;
2592 else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2593 leftvt == VT_NULL || leftvt == VT_I2 ||
2594 leftvt == VT_I4 || leftvt == VT_R4 ||
2595 leftvt == VT_R8 || leftvt == VT_CY ||
2596 leftvt == VT_DATE || leftvt == VT_BSTR ||
2597 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2598 leftvt == VT_I1 || leftvt == VT_UI1 ||
2599 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2600 leftvt == VT_I8 || leftvt == VT_UI8 ||
2601 leftvt == VT_INT || leftvt == VT_UINT))
2602 hres = DISP_E_TYPEMISMATCH;
2603 else
2604 hres = DISP_E_BADVARTYPE;
2606 /* if result type is not S_OK, then no need to go further */
2607 if (hres != S_OK)
2609 V_VT(out) = resultvt;
2610 return hres;
2612 /* Else proceed with formatting inputs to strings */
2613 else
2615 VARIANT bstrvar_left, bstrvar_right;
2616 V_VT(out) = VT_BSTR;
2618 VariantInit(&bstrvar_left);
2619 VariantInit(&bstrvar_right);
2621 /* Convert left side variant to string */
2622 if (leftvt != VT_BSTR)
2624 if (leftvt == VT_BOOL)
2626 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2627 V_VT(&bstrvar_left) = VT_BSTR;
2628 if (V_BOOL(left) == TRUE)
2629 V_BSTR(&bstrvar_left) = SysAllocString(str_true);
2630 else
2631 V_BSTR(&bstrvar_left) = SysAllocString(str_false);
2633 /* Fill with empty string for later concat with right side */
2634 else if (leftvt == VT_NULL)
2636 V_VT(&bstrvar_left) = VT_BSTR;
2637 V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
2639 else
2641 hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
2642 if (hres != S_OK) {
2643 VariantClear(&bstrvar_left);
2644 VariantClear(&bstrvar_right);
2645 if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
2646 rightvt == VT_NULL || rightvt == VT_I2 ||
2647 rightvt == VT_I4 || rightvt == VT_R4 ||
2648 rightvt == VT_R8 || rightvt == VT_CY ||
2649 rightvt == VT_DATE || rightvt == VT_BSTR ||
2650 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
2651 rightvt == VT_I1 || rightvt == VT_UI1 ||
2652 rightvt == VT_UI2 || rightvt == VT_UI4 ||
2653 rightvt == VT_I8 || rightvt == VT_UI8 ||
2654 rightvt == VT_INT || rightvt == VT_UINT))
2655 return DISP_E_BADVARTYPE;
2656 return hres;
2661 /* convert right side variant to string */
2662 if (rightvt != VT_BSTR)
2664 if (rightvt == VT_BOOL)
2666 /* Bools are handled as localized True/False strings instead of 0/-1 as in MSDN */
2667 V_VT(&bstrvar_right) = VT_BSTR;
2668 if (V_BOOL(right) == TRUE)
2669 V_BSTR(&bstrvar_right) = SysAllocString(str_true);
2670 else
2671 V_BSTR(&bstrvar_right) = SysAllocString(str_false);
2673 /* Fill with empty string for later concat with right side */
2674 else if (rightvt == VT_NULL)
2676 V_VT(&bstrvar_right) = VT_BSTR;
2677 V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
2679 else
2681 hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
2682 if (hres != S_OK) {
2683 VariantClear(&bstrvar_left);
2684 VariantClear(&bstrvar_right);
2685 if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
2686 leftvt == VT_NULL || leftvt == VT_I2 ||
2687 leftvt == VT_I4 || leftvt == VT_R4 ||
2688 leftvt == VT_R8 || leftvt == VT_CY ||
2689 leftvt == VT_DATE || leftvt == VT_BSTR ||
2690 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2691 leftvt == VT_I1 || leftvt == VT_UI1 ||
2692 leftvt == VT_UI2 || leftvt == VT_UI4 ||
2693 leftvt == VT_I8 || leftvt == VT_UI8 ||
2694 leftvt == VT_INT || leftvt == VT_UINT))
2695 return DISP_E_BADVARTYPE;
2696 return hres;
2701 /* Concat the resulting strings together */
2702 if (leftvt == VT_BSTR && rightvt == VT_BSTR)
2703 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2704 else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
2705 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2706 else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
2707 VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
2708 else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
2709 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));
2711 VariantClear(&bstrvar_left);
2712 VariantClear(&bstrvar_right);
2713 return S_OK;
2718 /* Wrapper around VariantChangeTypeEx() which permits changing a
2719 variant with VT_RESERVED flag set. Needed by VarCmp. */
2720 static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2721 VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2723 HRESULT res;
2724 VARTYPE flags;
2726 flags = V_VT(pvargSrc) & ~VT_TYPEMASK;
2727 V_VT(pvargSrc) &= ~VT_RESERVED;
2728 res = VariantChangeTypeEx(pvargDest,pvargSrc,lcid,wFlags,vt);
2729 V_VT(pvargSrc) |= flags;
2731 return res;
2734 /**********************************************************************
2735 * VarCmp [OLEAUT32.176]
2737 * Compare two variants.
2739 * PARAMS
2740 * left [I] First variant
2741 * right [I] Second variant
2742 * lcid [I] LCID (locale identifier) for the comparison
2743 * flags [I] Flags to be used in the comparison:
2744 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2745 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2747 * RETURNS
2748 * VARCMP_LT: left variant is less than right variant.
2749 * VARCMP_EQ: input variants are equal.
2750 * VARCMP_GT: left variant is greater than right variant.
2751 * VARCMP_NULL: either one of the input variants is NULL.
2752 * Failure: An HRESULT error code indicating the error.
2754 * NOTES
2755 * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2756 * UI8 and UINT as input variants. INT is accepted only as left variant.
2758 * If both input variants are ERROR then VARCMP_EQ will be returned, else
2759 * an ERROR variant will trigger an error.
2761 * Both input variants can have VT_RESERVED flag set which is ignored
2762 * unless one and only one of the variants is a BSTR and the other one
2763 * is not an EMPTY variant. All four VT_RESERVED combinations have a
2764 * different meaning:
2765 * - BSTR and other: BSTR is always greater than the other variant.
2766 * - BSTR|VT_RESERVED and other: a string comparison is performed.
2767 * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2768 * comparison will take place else the BSTR is always greater.
2769 * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2770 * variant is ignored and the return value depends only on the sign
2771 * of the BSTR if it is a number else the BSTR is always greater. A
2772 * positive BSTR is greater, a negative one is smaller than the other
2773 * variant.
2775 * SEE
2776 * VarBstrCmp for the lcid and flags usage.
2778 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2780 VARTYPE lvt, rvt, vt;
2781 VARIANT rv,lv;
2782 DWORD xmask;
2783 HRESULT rc;
2785 TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%08x)\n", left, debugstr_VT(left),
2786 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2788 lvt = V_VT(left) & VT_TYPEMASK;
2789 rvt = V_VT(right) & VT_TYPEMASK;
2790 xmask = (1 << lvt) | (1 << rvt);
2792 /* If we have any flag set except VT_RESERVED bail out.
2793 Same for the left input variant type > VT_INT and for the
2794 right input variant type > VT_I8. Yes, VT_INT is only supported
2795 as left variant. Go figure */
2796 if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2797 lvt > VT_INT || rvt > VT_I8) {
2798 return DISP_E_BADVARTYPE;
2801 /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2802 VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2803 if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2804 VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2805 return DISP_E_TYPEMISMATCH;
2807 /* If both variants are VT_ERROR return VARCMP_EQ */
2808 if (xmask == VTBIT_ERROR)
2809 return VARCMP_EQ;
2810 else if (xmask & VTBIT_ERROR)
2811 return DISP_E_TYPEMISMATCH;
2813 if (xmask & VTBIT_NULL)
2814 return VARCMP_NULL;
2816 VariantInit(&lv);
2817 VariantInit(&rv);
2819 /* Two BSTRs, ignore VT_RESERVED */
2820 if (xmask == VTBIT_BSTR)
2821 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2823 /* A BSTR and an other variant; we have to take care of VT_RESERVED */
2824 if (xmask & VTBIT_BSTR) {
2825 VARIANT *bstrv, *nonbv;
2826 VARTYPE nonbvt;
2827 int swap = 0;
2829 /* Swap the variants so the BSTR is always on the left */
2830 if (lvt == VT_BSTR) {
2831 bstrv = left;
2832 nonbv = right;
2833 nonbvt = rvt;
2834 } else {
2835 swap = 1;
2836 bstrv = right;
2837 nonbv = left;
2838 nonbvt = lvt;
2841 /* BSTR and EMPTY: ignore VT_RESERVED */
2842 if (nonbvt == VT_EMPTY)
2843 rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2844 else {
2845 VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2846 VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2848 if (!breserv && !nreserv)
2849 /* No VT_RESERVED set ==> BSTR always greater */
2850 rc = VARCMP_GT;
2851 else if (breserv && !nreserv) {
2852 /* BSTR has VT_RESERVED set. Do a string comparison */
2853 rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2854 if (FAILED(rc))
2855 return rc;
2856 rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2857 VariantClear(&rv);
2858 } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2859 /* Non NULL nor empty BSTR */
2860 /* If the BSTR is not a number the BSTR is greater */
2861 rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2862 if (FAILED(rc))
2863 rc = VARCMP_GT;
2864 else if (breserv && nreserv)
2865 /* FIXME: This is strange: with both VT_RESERVED set it
2866 looks like the result depends only on the sign of
2867 the BSTR number */
2868 rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2869 else
2870 /* Numeric comparison, will be handled below.
2871 VARCMP_NULL used only to break out. */
2872 rc = VARCMP_NULL;
2873 VariantClear(&lv);
2874 VariantClear(&rv);
2875 } else
2876 /* Empty or NULL BSTR */
2877 rc = VARCMP_GT;
2879 /* Fixup the return code if we swapped left and right */
2880 if (swap) {
2881 if (rc == VARCMP_GT)
2882 rc = VARCMP_LT;
2883 else if (rc == VARCMP_LT)
2884 rc = VARCMP_GT;
2886 if (rc != VARCMP_NULL)
2887 return rc;
2890 if (xmask & VTBIT_DECIMAL)
2891 vt = VT_DECIMAL;
2892 else if (xmask & VTBIT_BSTR)
2893 vt = VT_R8;
2894 else if (xmask & VTBIT_R4)
2895 vt = VT_R4;
2896 else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2897 vt = VT_R8;
2898 else if (xmask & VTBIT_CY)
2899 vt = VT_CY;
2900 else
2901 /* default to I8 */
2902 vt = VT_I8;
2904 /* Coerce the variants */
2905 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2906 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2907 /* Overflow, change to R8 */
2908 vt = VT_R8;
2909 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2911 if (FAILED(rc))
2912 return rc;
2913 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2914 if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2915 /* Overflow, change to R8 */
2916 vt = VT_R8;
2917 rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2918 if (FAILED(rc))
2919 return rc;
2920 rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2922 if (FAILED(rc))
2923 return rc;
2925 #define _VARCMP(a,b) \
2926 (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2928 switch (vt) {
2929 case VT_CY:
2930 return VarCyCmp(V_CY(&lv), V_CY(&rv));
2931 case VT_DECIMAL:
2932 return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2933 case VT_I8:
2934 return _VARCMP(V_I8(&lv), V_I8(&rv));
2935 case VT_R4:
2936 return _VARCMP(V_R4(&lv), V_R4(&rv));
2937 case VT_R8:
2938 return _VARCMP(V_R8(&lv), V_R8(&rv));
2939 default:
2940 /* We should never get here */
2941 return E_FAIL;
2943 #undef _VARCMP
2946 static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
2948 HRESULT hres;
2949 static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
2951 if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
2952 if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
2953 hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
2954 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
2955 NULL, NULL);
2956 } else {
2957 hres = DISP_E_TYPEMISMATCH;
2959 return hres;
2962 /**********************************************************************
2963 * VarAnd [OLEAUT32.142]
2965 * Computes the logical AND of two variants.
2967 * PARAMS
2968 * left [I] First variant
2969 * right [I] Second variant
2970 * result [O] Result variant
2972 * RETURNS
2973 * Success: S_OK.
2974 * Failure: An HRESULT error code indicating the error.
2976 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2978 HRESULT hres = S_OK;
2979 VARTYPE resvt = VT_EMPTY;
2980 VARTYPE leftvt,rightvt;
2981 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2982 VARIANT varLeft, varRight;
2983 VARIANT tempLeft, tempRight;
2985 VariantInit(&varLeft);
2986 VariantInit(&varRight);
2987 VariantInit(&tempLeft);
2988 VariantInit(&tempRight);
2990 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2991 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2993 /* Handle VT_DISPATCH by storing and taking address of returned value */
2994 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2996 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2997 if (FAILED(hres)) goto VarAnd_Exit;
2998 left = &tempLeft;
3000 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3002 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3003 if (FAILED(hres)) goto VarAnd_Exit;
3004 right = &tempRight;
3007 leftvt = V_VT(left)&VT_TYPEMASK;
3008 rightvt = V_VT(right)&VT_TYPEMASK;
3009 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3010 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3012 if (leftExtraFlags != rightExtraFlags)
3014 hres = DISP_E_BADVARTYPE;
3015 goto VarAnd_Exit;
3017 ExtraFlags = leftExtraFlags;
3019 /* Native VarAnd always returns an error when using extra
3020 * flags or if the variant combination is I8 and INT.
3022 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
3023 (leftvt == VT_INT && rightvt == VT_I8) ||
3024 ExtraFlags != 0)
3026 hres = DISP_E_BADVARTYPE;
3027 goto VarAnd_Exit;
3030 /* Determine return type */
3031 else if (leftvt == VT_I8 || rightvt == VT_I8)
3032 resvt = VT_I8;
3033 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
3034 leftvt == VT_UINT || rightvt == VT_UINT ||
3035 leftvt == VT_INT || rightvt == VT_INT ||
3036 leftvt == VT_UINT || rightvt == VT_UINT ||
3037 leftvt == VT_R4 || rightvt == VT_R4 ||
3038 leftvt == VT_R8 || rightvt == VT_R8 ||
3039 leftvt == VT_CY || rightvt == VT_CY ||
3040 leftvt == VT_DATE || rightvt == VT_DATE ||
3041 leftvt == VT_I1 || rightvt == VT_I1 ||
3042 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3043 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3044 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3045 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3046 resvt = VT_I4;
3047 else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
3048 leftvt == VT_I2 || rightvt == VT_I2 ||
3049 leftvt == VT_EMPTY || rightvt == VT_EMPTY)
3050 if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
3051 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
3052 (leftvt == VT_UI1 && rightvt == VT_UI1))
3053 resvt = VT_UI1;
3054 else
3055 resvt = VT_I2;
3056 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
3057 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3058 resvt = VT_BOOL;
3059 else if (leftvt == VT_NULL || rightvt == VT_NULL ||
3060 leftvt == VT_BSTR || rightvt == VT_BSTR)
3061 resvt = VT_NULL;
3062 else
3064 hres = DISP_E_BADVARTYPE;
3065 goto VarAnd_Exit;
3068 if (leftvt == VT_NULL || rightvt == VT_NULL)
3071 * Special cases for when left variant is VT_NULL
3072 * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
3074 if (leftvt == VT_NULL)
3076 VARIANT_BOOL b;
3077 switch(rightvt)
3079 case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
3080 case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
3081 case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
3082 case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
3083 case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
3084 case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3085 case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3086 case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3087 case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3088 case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3089 case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3090 case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3091 case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3092 case VT_CY:
3093 if(V_CY(right).int64)
3094 resvt = VT_NULL;
3095 break;
3096 case VT_DECIMAL:
3097 if (DEC_HI32(&V_DECIMAL(right)) ||
3098 DEC_LO64(&V_DECIMAL(right)))
3099 resvt = VT_NULL;
3100 break;
3101 case VT_BSTR:
3102 hres = VarBoolFromStr(V_BSTR(right),
3103 LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3104 if (FAILED(hres))
3105 return hres;
3106 else if (b)
3107 V_VT(result) = VT_NULL;
3108 else
3110 V_VT(result) = VT_BOOL;
3111 V_BOOL(result) = b;
3113 goto VarAnd_Exit;
3116 V_VT(result) = resvt;
3117 goto VarAnd_Exit;
3120 hres = VariantCopy(&varLeft, left);
3121 if (FAILED(hres)) goto VarAnd_Exit;
3123 hres = VariantCopy(&varRight, right);
3124 if (FAILED(hres)) goto VarAnd_Exit;
3126 if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3127 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3128 else
3130 double d;
3132 if (V_VT(&varLeft) == VT_BSTR &&
3133 FAILED(VarR8FromStr(V_BSTR(&varLeft),
3134 LOCALE_USER_DEFAULT, 0, &d)))
3135 hres = VariantChangeType(&varLeft,&varLeft,
3136 VARIANT_LOCALBOOL, VT_BOOL);
3137 if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3138 hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3139 if (FAILED(hres)) goto VarAnd_Exit;
3142 if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3143 V_VT(&varRight) = VT_I4; /* Don't overflow */
3144 else
3146 double d;
3148 if (V_VT(&varRight) == VT_BSTR &&
3149 FAILED(VarR8FromStr(V_BSTR(&varRight),
3150 LOCALE_USER_DEFAULT, 0, &d)))
3151 hres = VariantChangeType(&varRight, &varRight,
3152 VARIANT_LOCALBOOL, VT_BOOL);
3153 if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3154 hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3155 if (FAILED(hres)) goto VarAnd_Exit;
3158 V_VT(result) = resvt;
3159 switch(resvt)
3161 case VT_I8:
3162 V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3163 break;
3164 case VT_I4:
3165 V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3166 break;
3167 case VT_I2:
3168 V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3169 break;
3170 case VT_UI1:
3171 V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3172 break;
3173 case VT_BOOL:
3174 V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3175 break;
3176 default:
3177 FIXME("Couldn't bitwise AND variant types %d,%d\n",
3178 leftvt,rightvt);
3181 VarAnd_Exit:
3182 VariantClear(&varLeft);
3183 VariantClear(&varRight);
3184 VariantClear(&tempLeft);
3185 VariantClear(&tempRight);
3187 return hres;
3190 /**********************************************************************
3191 * VarAdd [OLEAUT32.141]
3193 * Add two variants.
3195 * PARAMS
3196 * left [I] First variant
3197 * right [I] Second variant
3198 * result [O] Result variant
3200 * RETURNS
3201 * Success: S_OK.
3202 * Failure: An HRESULT error code indicating the error.
3204 * NOTES
3205 * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3206 * UI8, INT and UINT as input variants.
3208 * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3209 * same here.
3211 * FIXME
3212 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3213 * case.
3215 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3217 HRESULT hres;
3218 VARTYPE lvt, rvt, resvt, tvt;
3219 VARIANT lv, rv, tv;
3220 VARIANT tempLeft, tempRight;
3221 double r8res;
3223 /* Variant priority for coercion. Sorted from lowest to highest.
3224 VT_ERROR shows an invalid input variant type. */
3225 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3226 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3227 vt_ERROR };
3228 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3229 static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3230 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3231 VT_NULL, VT_ERROR };
3233 /* Mapping for coercion from input variant to priority of result variant. */
3234 static const VARTYPE coerce[] = {
3235 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3236 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3237 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3238 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3239 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3240 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3241 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3242 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3245 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3246 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3247 result);
3249 VariantInit(&lv);
3250 VariantInit(&rv);
3251 VariantInit(&tv);
3252 VariantInit(&tempLeft);
3253 VariantInit(&tempRight);
3255 /* Handle VT_DISPATCH by storing and taking address of returned value */
3256 if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3258 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3260 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3261 if (FAILED(hres)) goto end;
3262 left = &tempLeft;
3264 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3266 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3267 if (FAILED(hres)) goto end;
3268 right = &tempRight;
3272 lvt = V_VT(left)&VT_TYPEMASK;
3273 rvt = V_VT(right)&VT_TYPEMASK;
3275 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3276 Same for any input variant type > VT_I8 */
3277 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3278 lvt > VT_I8 || rvt > VT_I8) {
3279 hres = DISP_E_BADVARTYPE;
3280 goto end;
3283 /* Determine the variant type to coerce to. */
3284 if (coerce[lvt] > coerce[rvt]) {
3285 resvt = prio2vt[coerce[lvt]];
3286 tvt = prio2vt[coerce[rvt]];
3287 } else {
3288 resvt = prio2vt[coerce[rvt]];
3289 tvt = prio2vt[coerce[lvt]];
3292 /* Special cases where the result variant type is defined by both
3293 input variants and not only that with the highest priority */
3294 if (resvt == VT_BSTR) {
3295 if (tvt == VT_EMPTY || tvt == VT_BSTR)
3296 resvt = VT_BSTR;
3297 else
3298 resvt = VT_R8;
3300 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3301 resvt = VT_R8;
3303 /* For overflow detection use the biggest compatible type for the
3304 addition */
3305 switch (resvt) {
3306 case VT_ERROR:
3307 hres = DISP_E_BADVARTYPE;
3308 goto end;
3309 case VT_NULL:
3310 hres = S_OK;
3311 V_VT(result) = VT_NULL;
3312 goto end;
3313 case VT_DISPATCH:
3314 FIXME("cannot handle variant type VT_DISPATCH\n");
3315 hres = DISP_E_TYPEMISMATCH;
3316 goto end;
3317 case VT_EMPTY:
3318 resvt = VT_I2;
3319 /* Fall through */
3320 case VT_UI1:
3321 case VT_I2:
3322 case VT_I4:
3323 case VT_I8:
3324 tvt = VT_I8;
3325 break;
3326 case VT_DATE:
3327 case VT_R4:
3328 tvt = VT_R8;
3329 break;
3330 default:
3331 tvt = resvt;
3334 /* Now coerce the variants */
3335 hres = VariantChangeType(&lv, left, 0, tvt);
3336 if (FAILED(hres))
3337 goto end;
3338 hres = VariantChangeType(&rv, right, 0, tvt);
3339 if (FAILED(hres))
3340 goto end;
3342 /* Do the math */
3343 hres = S_OK;
3344 V_VT(result) = resvt;
3345 switch (tvt) {
3346 case VT_DECIMAL:
3347 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3348 &V_DECIMAL(result));
3349 goto end;
3350 case VT_CY:
3351 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3352 goto end;
3353 case VT_BSTR:
3354 /* We do not add those, we concatenate them. */
3355 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3356 goto end;
3357 case VT_I8:
3358 /* Overflow detection */
3359 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3360 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3361 V_VT(result) = VT_R8;
3362 V_R8(result) = r8res;
3363 goto end;
3364 } else {
3365 V_VT(&tv) = tvt;
3366 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3368 break;
3369 case VT_R8:
3370 V_VT(&tv) = tvt;
3371 /* FIXME: overflow detection */
3372 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3373 break;
3374 default:
3375 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3376 break;
3378 if (resvt != tvt) {
3379 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3380 /* Overflow! Change to the vartype with the next higher priority.
3381 With one exception: I4 ==> R8 even if it would fit in I8 */
3382 if (resvt == VT_I4)
3383 resvt = VT_R8;
3384 else
3385 resvt = prio2vt[coerce[resvt] + 1];
3386 hres = VariantChangeType(result, &tv, 0, resvt);
3388 } else
3389 hres = VariantCopy(result, &tv);
3391 end:
3392 if (hres != S_OK) {
3393 V_VT(result) = VT_EMPTY;
3394 V_I4(result) = 0; /* No V_EMPTY */
3396 VariantClear(&lv);
3397 VariantClear(&rv);
3398 VariantClear(&tv);
3399 VariantClear(&tempLeft);
3400 VariantClear(&tempRight);
3401 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3402 return hres;
3405 /**********************************************************************
3406 * VarMul [OLEAUT32.156]
3408 * Multiply two variants.
3410 * PARAMS
3411 * left [I] First variant
3412 * right [I] Second variant
3413 * result [O] Result variant
3415 * RETURNS
3416 * Success: S_OK.
3417 * Failure: An HRESULT error code indicating the error.
3419 * NOTES
3420 * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3421 * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3423 * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3424 * same here.
3426 * FIXME
3427 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3428 * case.
3430 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3432 HRESULT hres;
3433 VARTYPE lvt, rvt, resvt, tvt;
3434 VARIANT lv, rv, tv;
3435 VARIANT tempLeft, tempRight;
3436 double r8res;
3438 /* Variant priority for coercion. Sorted from lowest to highest.
3439 VT_ERROR shows an invalid input variant type. */
3440 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3441 vt_DECIMAL, vt_NULL, vt_ERROR };
3442 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3443 static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3444 VT_DECIMAL, VT_NULL, VT_ERROR };
3446 /* Mapping for coercion from input variant to priority of result variant. */
3447 static const VARTYPE coerce[] = {
3448 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3449 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3450 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3451 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3452 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3453 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3454 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3455 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3458 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3459 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
3460 result);
3462 VariantInit(&lv);
3463 VariantInit(&rv);
3464 VariantInit(&tv);
3465 VariantInit(&tempLeft);
3466 VariantInit(&tempRight);
3468 /* Handle VT_DISPATCH by storing and taking address of returned value */
3469 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3471 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3472 if (FAILED(hres)) goto end;
3473 left = &tempLeft;
3475 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3477 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3478 if (FAILED(hres)) goto end;
3479 right = &tempRight;
3482 lvt = V_VT(left)&VT_TYPEMASK;
3483 rvt = V_VT(right)&VT_TYPEMASK;
3485 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3486 Same for any input variant type > VT_I8 */
3487 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3488 lvt > VT_I8 || rvt > VT_I8) {
3489 hres = DISP_E_BADVARTYPE;
3490 goto end;
3493 /* Determine the variant type to coerce to. */
3494 if (coerce[lvt] > coerce[rvt]) {
3495 resvt = prio2vt[coerce[lvt]];
3496 tvt = prio2vt[coerce[rvt]];
3497 } else {
3498 resvt = prio2vt[coerce[rvt]];
3499 tvt = prio2vt[coerce[lvt]];
3502 /* Special cases where the result variant type is defined by both
3503 input variants and not only that with the highest priority */
3504 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3505 resvt = VT_R8;
3506 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3507 resvt = VT_I2;
3509 /* For overflow detection use the biggest compatible type for the
3510 multiplication */
3511 switch (resvt) {
3512 case VT_ERROR:
3513 hres = DISP_E_BADVARTYPE;
3514 goto end;
3515 case VT_NULL:
3516 hres = S_OK;
3517 V_VT(result) = VT_NULL;
3518 goto end;
3519 case VT_UI1:
3520 case VT_I2:
3521 case VT_I4:
3522 case VT_I8:
3523 tvt = VT_I8;
3524 break;
3525 case VT_R4:
3526 tvt = VT_R8;
3527 break;
3528 default:
3529 tvt = resvt;
3532 /* Now coerce the variants */
3533 hres = VariantChangeType(&lv, left, 0, tvt);
3534 if (FAILED(hres))
3535 goto end;
3536 hres = VariantChangeType(&rv, right, 0, tvt);
3537 if (FAILED(hres))
3538 goto end;
3540 /* Do the math */
3541 hres = S_OK;
3542 V_VT(&tv) = tvt;
3543 V_VT(result) = resvt;
3544 switch (tvt) {
3545 case VT_DECIMAL:
3546 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3547 &V_DECIMAL(result));
3548 goto end;
3549 case VT_CY:
3550 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3551 goto end;
3552 case VT_I8:
3553 /* Overflow detection */
3554 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3555 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3556 V_VT(result) = VT_R8;
3557 V_R8(result) = r8res;
3558 goto end;
3559 } else
3560 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3561 break;
3562 case VT_R8:
3563 /* FIXME: overflow detection */
3564 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3565 break;
3566 default:
3567 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3568 break;
3570 if (resvt != tvt) {
3571 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3572 /* Overflow! Change to the vartype with the next higher priority.
3573 With one exception: I4 ==> R8 even if it would fit in I8 */
3574 if (resvt == VT_I4)
3575 resvt = VT_R8;
3576 else
3577 resvt = prio2vt[coerce[resvt] + 1];
3579 } else
3580 hres = VariantCopy(result, &tv);
3582 end:
3583 if (hres != S_OK) {
3584 V_VT(result) = VT_EMPTY;
3585 V_I4(result) = 0; /* No V_EMPTY */
3587 VariantClear(&lv);
3588 VariantClear(&rv);
3589 VariantClear(&tv);
3590 VariantClear(&tempLeft);
3591 VariantClear(&tempRight);
3592 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3593 return hres;
3596 /**********************************************************************
3597 * VarDiv [OLEAUT32.143]
3599 * Divides one variant with another.
3601 * PARAMS
3602 * left [I] First variant
3603 * right [I] Second variant
3604 * result [O] Result variant
3606 * RETURNS
3607 * Success: S_OK.
3608 * Failure: An HRESULT error code indicating the error.
3610 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3612 HRESULT hres = S_OK;
3613 VARTYPE resvt = VT_EMPTY;
3614 VARTYPE leftvt,rightvt;
3615 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3616 VARIANT lv,rv;
3617 VARIANT tempLeft, tempRight;
3619 VariantInit(&tempLeft);
3620 VariantInit(&tempRight);
3621 VariantInit(&lv);
3622 VariantInit(&rv);
3624 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3625 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3627 /* Handle VT_DISPATCH by storing and taking address of returned value */
3628 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3630 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3631 if (FAILED(hres)) goto end;
3632 left = &tempLeft;
3634 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3636 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3637 if (FAILED(hres)) goto end;
3638 right = &tempRight;
3641 leftvt = V_VT(left)&VT_TYPEMASK;
3642 rightvt = V_VT(right)&VT_TYPEMASK;
3643 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3644 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3646 if (leftExtraFlags != rightExtraFlags)
3648 hres = DISP_E_BADVARTYPE;
3649 goto end;
3651 ExtraFlags = leftExtraFlags;
3653 /* Native VarDiv always returns an error when using extra flags */
3654 if (ExtraFlags != 0)
3656 hres = DISP_E_BADVARTYPE;
3657 goto end;
3660 /* Determine return type */
3661 if (!(rightvt == VT_EMPTY))
3663 if (leftvt == VT_NULL || rightvt == VT_NULL)
3665 V_VT(result) = VT_NULL;
3666 hres = S_OK;
3667 goto end;
3669 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3670 resvt = VT_DECIMAL;
3671 else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3672 leftvt == VT_CY || rightvt == VT_CY ||
3673 leftvt == VT_DATE || rightvt == VT_DATE ||
3674 leftvt == VT_I4 || rightvt == VT_I4 ||
3675 leftvt == VT_BSTR || rightvt == VT_BSTR ||
3676 leftvt == VT_I2 || rightvt == VT_I2 ||
3677 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3678 leftvt == VT_R8 || rightvt == VT_R8 ||
3679 leftvt == VT_UI1 || rightvt == VT_UI1)
3681 if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3682 (leftvt == VT_R4 && rightvt == VT_UI1))
3683 resvt = VT_R4;
3684 else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3685 rightvt == VT_I2)) || (rightvt == VT_R4 &&
3686 (leftvt == VT_BOOL || leftvt == VT_I2)))
3687 resvt = VT_R4;
3688 else
3689 resvt = VT_R8;
3691 else if (leftvt == VT_R4 || rightvt == VT_R4)
3692 resvt = VT_R4;
3694 else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
3696 V_VT(result) = VT_NULL;
3697 hres = S_OK;
3698 goto end;
3700 else
3702 hres = DISP_E_BADVARTYPE;
3703 goto end;
3706 /* coerce to the result type */
3707 hres = VariantChangeType(&lv, left, 0, resvt);
3708 if (hres != S_OK) goto end;
3710 hres = VariantChangeType(&rv, right, 0, resvt);
3711 if (hres != S_OK) goto end;
3713 /* do the math */
3714 V_VT(result) = resvt;
3715 switch (resvt)
3717 case VT_R4:
3718 if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3720 hres = DISP_E_OVERFLOW;
3721 V_VT(result) = VT_EMPTY;
3723 else if (V_R4(&rv) == 0.0)
3725 hres = DISP_E_DIVBYZERO;
3726 V_VT(result) = VT_EMPTY;
3728 else
3729 V_R4(result) = V_R4(&lv) / V_R4(&rv);
3730 break;
3731 case VT_R8:
3732 if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3734 hres = DISP_E_OVERFLOW;
3735 V_VT(result) = VT_EMPTY;
3737 else if (V_R8(&rv) == 0.0)
3739 hres = DISP_E_DIVBYZERO;
3740 V_VT(result) = VT_EMPTY;
3742 else
3743 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3744 break;
3745 case VT_DECIMAL:
3746 hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3747 break;
3750 end:
3751 VariantClear(&lv);
3752 VariantClear(&rv);
3753 VariantClear(&tempLeft);
3754 VariantClear(&tempRight);
3755 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3756 return hres;
3759 /**********************************************************************
3760 * VarSub [OLEAUT32.159]
3762 * Subtract two variants.
3764 * PARAMS
3765 * left [I] First variant
3766 * right [I] Second variant
3767 * result [O] Result variant
3769 * RETURNS
3770 * Success: S_OK.
3771 * Failure: An HRESULT error code indicating the error.
3773 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3775 HRESULT hres = S_OK;
3776 VARTYPE resvt = VT_EMPTY;
3777 VARTYPE leftvt,rightvt;
3778 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3779 VARIANT lv,rv;
3780 VARIANT tempLeft, tempRight;
3782 VariantInit(&lv);
3783 VariantInit(&rv);
3784 VariantInit(&tempLeft);
3785 VariantInit(&tempRight);
3787 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3788 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3790 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3791 (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3792 (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3794 if (NULL == V_DISPATCH(left)) {
3795 if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3796 hres = DISP_E_BADVARTYPE;
3797 else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3798 (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3799 hres = DISP_E_BADVARTYPE;
3800 else switch (V_VT(right) & VT_TYPEMASK)
3802 case VT_VARIANT:
3803 case VT_UNKNOWN:
3804 case 15:
3805 case VT_I1:
3806 case VT_UI2:
3807 case VT_UI4:
3808 hres = DISP_E_BADVARTYPE;
3810 if (FAILED(hres)) goto end;
3812 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3813 if (FAILED(hres)) goto end;
3814 left = &tempLeft;
3816 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3817 (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3818 (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3820 if (NULL == V_DISPATCH(right))
3822 if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3823 hres = DISP_E_BADVARTYPE;
3824 else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3825 (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3826 hres = DISP_E_BADVARTYPE;
3827 else switch (V_VT(left) & VT_TYPEMASK)
3829 case VT_VARIANT:
3830 case VT_UNKNOWN:
3831 case 15:
3832 case VT_I1:
3833 case VT_UI2:
3834 case VT_UI4:
3835 hres = DISP_E_BADVARTYPE;
3837 if (FAILED(hres)) goto end;
3839 hres = VARIANT_FetchDispatchValue(right, &tempRight);
3840 if (FAILED(hres)) goto end;
3841 right = &tempRight;
3844 leftvt = V_VT(left)&VT_TYPEMASK;
3845 rightvt = V_VT(right)&VT_TYPEMASK;
3846 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3847 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3849 if (leftExtraFlags != rightExtraFlags)
3851 hres = DISP_E_BADVARTYPE;
3852 goto end;
3854 ExtraFlags = leftExtraFlags;
3856 /* determine return type and return code */
3857 /* All extra flags produce errors */
3858 if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3859 ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3860 ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3861 ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3862 ExtraFlags == VT_VECTOR ||
3863 ExtraFlags == VT_BYREF ||
3864 ExtraFlags == VT_RESERVED)
3866 hres = DISP_E_BADVARTYPE;
3867 goto end;
3869 else if (ExtraFlags >= VT_ARRAY)
3871 hres = DISP_E_TYPEMISMATCH;
3872 goto end;
3874 /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3875 VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3876 else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3877 leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3878 leftvt == VT_I1 || rightvt == VT_I1 ||
3879 leftvt == VT_UI2 || rightvt == VT_UI2 ||
3880 leftvt == VT_UI4 || rightvt == VT_UI4 ||
3881 leftvt == VT_UI8 || rightvt == VT_UI8 ||
3882 leftvt == VT_INT || rightvt == VT_INT ||
3883 leftvt == VT_UINT || rightvt == VT_UINT ||
3884 leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3885 leftvt == VT_RECORD || rightvt == VT_RECORD)
3887 if (leftvt == VT_RECORD && rightvt == VT_I8)
3888 hres = DISP_E_TYPEMISMATCH;
3889 else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3890 hres = DISP_E_TYPEMISMATCH;
3891 else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3892 hres = DISP_E_TYPEMISMATCH;
3893 else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3894 hres = DISP_E_TYPEMISMATCH;
3895 else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3896 hres = DISP_E_BADVARTYPE;
3897 else
3898 hres = DISP_E_BADVARTYPE;
3899 goto end;
3901 /* The following flags/types are invalid for left variant */
3902 else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3903 leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3904 (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3906 hres = DISP_E_BADVARTYPE;
3907 goto end;
3909 /* The following flags/types are invalid for right variant */
3910 else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3911 rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3912 (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3914 hres = DISP_E_BADVARTYPE;
3915 goto end;
3917 else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3918 (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3919 resvt = VT_NULL;
3920 else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3921 leftvt == VT_ERROR || rightvt == VT_ERROR)
3923 hres = DISP_E_TYPEMISMATCH;
3924 goto end;
3926 else if (leftvt == VT_NULL || rightvt == VT_NULL)
3927 resvt = VT_NULL;
3928 else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3929 (leftvt == VT_DATE && rightvt == VT_DATE) ||
3930 (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3931 (leftvt == VT_BSTR && rightvt == VT_BSTR))
3932 resvt = VT_R8;
3933 else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3934 resvt = VT_DECIMAL;
3935 else if (leftvt == VT_DATE || rightvt == VT_DATE)
3936 resvt = VT_DATE;
3937 else if (leftvt == VT_CY || rightvt == VT_CY)
3938 resvt = VT_CY;
3939 else if (leftvt == VT_R8 || rightvt == VT_R8)
3940 resvt = VT_R8;
3941 else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3942 resvt = VT_R8;
3943 else if (leftvt == VT_R4 || rightvt == VT_R4)
3945 if (leftvt == VT_I4 || rightvt == VT_I4 ||
3946 leftvt == VT_I8 || rightvt == VT_I8)
3947 resvt = VT_R8;
3948 else
3949 resvt = VT_R4;
3951 else if (leftvt == VT_I8 || rightvt == VT_I8)
3952 resvt = VT_I8;
3953 else if (leftvt == VT_I4 || rightvt == VT_I4)
3954 resvt = VT_I4;
3955 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3956 leftvt == VT_BOOL || rightvt == VT_BOOL ||
3957 (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3958 resvt = VT_I2;
3959 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3960 resvt = VT_UI1;
3961 else
3963 hres = DISP_E_TYPEMISMATCH;
3964 goto end;
3967 /* coerce to the result type */
3968 if (leftvt == VT_BSTR && rightvt == VT_DATE)
3969 hres = VariantChangeType(&lv, left, 0, VT_R8);
3970 else
3971 hres = VariantChangeType(&lv, left, 0, resvt);
3972 if (hres != S_OK) goto end;
3973 if (leftvt == VT_DATE && rightvt == VT_BSTR)
3974 hres = VariantChangeType(&rv, right, 0, VT_R8);
3975 else
3976 hres = VariantChangeType(&rv, right, 0, resvt);
3977 if (hres != S_OK) goto end;
3979 /* do the math */
3980 V_VT(result) = resvt;
3981 switch (resvt)
3983 case VT_NULL:
3984 break;
3985 case VT_DATE:
3986 V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3987 break;
3988 case VT_CY:
3989 hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3990 break;
3991 case VT_R4:
3992 V_R4(result) = V_R4(&lv) - V_R4(&rv);
3993 break;
3994 case VT_I8:
3995 V_I8(result) = V_I8(&lv) - V_I8(&rv);
3996 break;
3997 case VT_I4:
3998 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3999 break;
4000 case VT_I2:
4001 V_I2(result) = V_I2(&lv) - V_I2(&rv);
4002 break;
4003 case VT_I1:
4004 V_I1(result) = V_I1(&lv) - V_I1(&rv);
4005 break;
4006 case VT_UI1:
4007 V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
4008 break;
4009 case VT_R8:
4010 V_R8(result) = V_R8(&lv) - V_R8(&rv);
4011 break;
4012 case VT_DECIMAL:
4013 hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
4014 break;
4017 end:
4018 VariantClear(&lv);
4019 VariantClear(&rv);
4020 VariantClear(&tempLeft);
4021 VariantClear(&tempRight);
4022 TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
4023 return hres;
4027 /**********************************************************************
4028 * VarOr [OLEAUT32.157]
4030 * Perform a logical or (OR) operation on two variants.
4032 * PARAMS
4033 * pVarLeft [I] First variant
4034 * pVarRight [I] Variant to OR with pVarLeft
4035 * pVarOut [O] Destination for OR result
4037 * RETURNS
4038 * Success: S_OK. pVarOut contains the result of the operation with its type
4039 * taken from the table listed under VarXor().
4040 * Failure: An HRESULT error code indicating the error.
4042 * NOTES
4043 * See the Notes section of VarXor() for further information.
4045 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4047 VARTYPE vt = VT_I4;
4048 VARIANT varLeft, varRight, varStr;
4049 HRESULT hRet;
4050 VARIANT tempLeft, tempRight;
4052 VariantInit(&tempLeft);
4053 VariantInit(&tempRight);
4054 VariantInit(&varLeft);
4055 VariantInit(&varRight);
4056 VariantInit(&varStr);
4058 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4059 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4060 debugstr_VF(pVarRight), pVarOut);
4062 /* Handle VT_DISPATCH by storing and taking address of returned value */
4063 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4065 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4066 if (FAILED(hRet)) goto VarOr_Exit;
4067 pVarLeft = &tempLeft;
4069 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4071 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4072 if (FAILED(hRet)) goto VarOr_Exit;
4073 pVarRight = &tempRight;
4076 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4077 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4078 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
4079 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
4081 hRet = DISP_E_BADVARTYPE;
4082 goto VarOr_Exit;
4085 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
4087 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4089 /* NULL OR Zero is NULL, NULL OR value is value */
4090 if (V_VT(pVarLeft) == VT_NULL)
4091 pVarLeft = pVarRight; /* point to the non-NULL var */
4093 V_VT(pVarOut) = VT_NULL;
4094 V_I4(pVarOut) = 0;
4096 switch (V_VT(pVarLeft))
4098 case VT_DATE: case VT_R8:
4099 if (V_R8(pVarLeft))
4100 goto VarOr_AsEmpty;
4101 hRet = S_OK;
4102 goto VarOr_Exit;
4103 case VT_BOOL:
4104 if (V_BOOL(pVarLeft))
4105 *pVarOut = *pVarLeft;
4106 hRet = S_OK;
4107 goto VarOr_Exit;
4108 case VT_I2: case VT_UI2:
4109 if (V_I2(pVarLeft))
4110 goto VarOr_AsEmpty;
4111 hRet = S_OK;
4112 goto VarOr_Exit;
4113 case VT_I1:
4114 if (V_I1(pVarLeft))
4115 goto VarOr_AsEmpty;
4116 hRet = S_OK;
4117 goto VarOr_Exit;
4118 case VT_UI1:
4119 if (V_UI1(pVarLeft))
4120 *pVarOut = *pVarLeft;
4121 hRet = S_OK;
4122 goto VarOr_Exit;
4123 case VT_R4:
4124 if (V_R4(pVarLeft))
4125 goto VarOr_AsEmpty;
4126 hRet = S_OK;
4127 goto VarOr_Exit;
4128 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4129 if (V_I4(pVarLeft))
4130 goto VarOr_AsEmpty;
4131 hRet = S_OK;
4132 goto VarOr_Exit;
4133 case VT_CY:
4134 if (V_CY(pVarLeft).int64)
4135 goto VarOr_AsEmpty;
4136 hRet = S_OK;
4137 goto VarOr_Exit;
4138 case VT_I8: case VT_UI8:
4139 if (V_I8(pVarLeft))
4140 goto VarOr_AsEmpty;
4141 hRet = S_OK;
4142 goto VarOr_Exit;
4143 case VT_DECIMAL:
4144 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4145 goto VarOr_AsEmpty;
4146 hRet = S_OK;
4147 goto VarOr_Exit;
4148 case VT_BSTR:
4150 VARIANT_BOOL b;
4152 if (!V_BSTR(pVarLeft))
4154 hRet = DISP_E_BADVARTYPE;
4155 goto VarOr_Exit;
4158 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4159 if (SUCCEEDED(hRet) && b)
4161 V_VT(pVarOut) = VT_BOOL;
4162 V_BOOL(pVarOut) = b;
4164 goto VarOr_Exit;
4166 case VT_NULL: case VT_EMPTY:
4167 V_VT(pVarOut) = VT_NULL;
4168 hRet = S_OK;
4169 goto VarOr_Exit;
4170 default:
4171 hRet = DISP_E_BADVARTYPE;
4172 goto VarOr_Exit;
4176 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4178 if (V_VT(pVarLeft) == VT_EMPTY)
4179 pVarLeft = pVarRight; /* point to the non-EMPTY var */
4181 VarOr_AsEmpty:
4182 /* Since one argument is empty (0), OR'ing it with the other simply
4183 * gives the others value (as 0|x => x). So just convert the other
4184 * argument to the required result type.
4186 switch (V_VT(pVarLeft))
4188 case VT_BSTR:
4189 if (!V_BSTR(pVarLeft))
4191 hRet = DISP_E_BADVARTYPE;
4192 goto VarOr_Exit;
4195 hRet = VariantCopy(&varStr, pVarLeft);
4196 if (FAILED(hRet))
4197 goto VarOr_Exit;
4198 pVarLeft = &varStr;
4199 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4200 if (FAILED(hRet))
4201 goto VarOr_Exit;
4202 /* Fall Through ... */
4203 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4204 V_VT(pVarOut) = VT_I2;
4205 break;
4206 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4207 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4208 case VT_INT: case VT_UINT: case VT_UI8:
4209 V_VT(pVarOut) = VT_I4;
4210 break;
4211 case VT_I8:
4212 V_VT(pVarOut) = VT_I8;
4213 break;
4214 default:
4215 hRet = DISP_E_BADVARTYPE;
4216 goto VarOr_Exit;
4218 hRet = VariantCopy(&varLeft, pVarLeft);
4219 if (FAILED(hRet))
4220 goto VarOr_Exit;
4221 pVarLeft = &varLeft;
4222 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4223 goto VarOr_Exit;
4226 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4228 V_VT(pVarOut) = VT_BOOL;
4229 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4230 hRet = S_OK;
4231 goto VarOr_Exit;
4234 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4236 V_VT(pVarOut) = VT_UI1;
4237 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4238 hRet = S_OK;
4239 goto VarOr_Exit;
4242 if (V_VT(pVarLeft) == VT_BSTR)
4244 hRet = VariantCopy(&varStr, pVarLeft);
4245 if (FAILED(hRet))
4246 goto VarOr_Exit;
4247 pVarLeft = &varStr;
4248 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4249 if (FAILED(hRet))
4250 goto VarOr_Exit;
4253 if (V_VT(pVarLeft) == VT_BOOL &&
4254 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4256 vt = VT_BOOL;
4258 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4259 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4260 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4261 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4263 vt = VT_I2;
4265 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4267 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4269 hRet = DISP_E_TYPEMISMATCH;
4270 goto VarOr_Exit;
4272 vt = VT_I8;
4275 hRet = VariantCopy(&varLeft, pVarLeft);
4276 if (FAILED(hRet))
4277 goto VarOr_Exit;
4279 hRet = VariantCopy(&varRight, pVarRight);
4280 if (FAILED(hRet))
4281 goto VarOr_Exit;
4283 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4284 V_VT(&varLeft) = VT_I4; /* Don't overflow */
4285 else
4287 double d;
4289 if (V_VT(&varLeft) == VT_BSTR &&
4290 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4291 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4292 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4293 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4294 if (FAILED(hRet))
4295 goto VarOr_Exit;
4298 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4299 V_VT(&varRight) = VT_I4; /* Don't overflow */
4300 else
4302 double d;
4304 if (V_VT(&varRight) == VT_BSTR &&
4305 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4306 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4307 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4308 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4309 if (FAILED(hRet))
4310 goto VarOr_Exit;
4313 V_VT(pVarOut) = vt;
4314 if (vt == VT_I8)
4316 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4318 else if (vt == VT_I4)
4320 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4322 else
4324 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4327 VarOr_Exit:
4328 VariantClear(&varStr);
4329 VariantClear(&varLeft);
4330 VariantClear(&varRight);
4331 VariantClear(&tempLeft);
4332 VariantClear(&tempRight);
4333 return hRet;
4336 /**********************************************************************
4337 * VarAbs [OLEAUT32.168]
4339 * Convert a variant to its absolute value.
4341 * PARAMS
4342 * pVarIn [I] Source variant
4343 * pVarOut [O] Destination for converted value
4345 * RETURNS
4346 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4347 * Failure: An HRESULT error code indicating the error.
4349 * NOTES
4350 * - This function does not process by-reference variants.
4351 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4352 * according to the following table:
4353 *| Input Type Output Type
4354 *| ---------- -----------
4355 *| VT_BOOL VT_I2
4356 *| VT_BSTR VT_R8
4357 *| (All others) Unchanged
4359 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4361 VARIANT varIn;
4362 HRESULT hRet = S_OK;
4363 VARIANT temp;
4365 VariantInit(&temp);
4367 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4368 debugstr_VF(pVarIn), pVarOut);
4370 /* Handle VT_DISPATCH by storing and taking address of returned value */
4371 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4373 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4374 if (FAILED(hRet)) goto VarAbs_Exit;
4375 pVarIn = &temp;
4378 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4379 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4380 V_VT(pVarIn) == VT_ERROR)
4382 hRet = DISP_E_TYPEMISMATCH;
4383 goto VarAbs_Exit;
4385 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4387 #define ABS_CASE(typ,min) \
4388 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4389 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4390 break
4392 switch (V_VT(pVarIn))
4394 ABS_CASE(I1,I1_MIN);
4395 case VT_BOOL:
4396 V_VT(pVarOut) = VT_I2;
4397 /* BOOL->I2, Fall through ... */
4398 ABS_CASE(I2,I2_MIN);
4399 case VT_INT:
4400 ABS_CASE(I4,I4_MIN);
4401 ABS_CASE(I8,I8_MIN);
4402 ABS_CASE(R4,R4_MIN);
4403 case VT_BSTR:
4404 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4405 if (FAILED(hRet))
4406 break;
4407 V_VT(pVarOut) = VT_R8;
4408 pVarIn = &varIn;
4409 /* Fall through ... */
4410 case VT_DATE:
4411 ABS_CASE(R8,R8_MIN);
4412 case VT_CY:
4413 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4414 break;
4415 case VT_DECIMAL:
4416 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4417 break;
4418 case VT_UI1:
4419 case VT_UI2:
4420 case VT_UINT:
4421 case VT_UI4:
4422 case VT_UI8:
4423 /* No-Op */
4424 break;
4425 case VT_EMPTY:
4426 V_VT(pVarOut) = VT_I2;
4427 case VT_NULL:
4428 V_I2(pVarOut) = 0;
4429 break;
4430 default:
4431 hRet = DISP_E_BADVARTYPE;
4434 VarAbs_Exit:
4435 VariantClear(&temp);
4436 return hRet;
4439 /**********************************************************************
4440 * VarFix [OLEAUT32.169]
4442 * Truncate a variants value to a whole number.
4444 * PARAMS
4445 * pVarIn [I] Source variant
4446 * pVarOut [O] Destination for converted value
4448 * RETURNS
4449 * Success: S_OK. pVarOut contains the converted value.
4450 * Failure: An HRESULT error code indicating the error.
4452 * NOTES
4453 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4454 * according to the following table:
4455 *| Input Type Output Type
4456 *| ---------- -----------
4457 *| VT_BOOL VT_I2
4458 *| VT_EMPTY VT_I2
4459 *| VT_BSTR VT_R8
4460 *| All Others Unchanged
4461 * - The difference between this function and VarInt() is that VarInt() rounds
4462 * negative numbers away from 0, while this function rounds them towards zero.
4464 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4466 HRESULT hRet = S_OK;
4467 VARIANT temp;
4469 VariantInit(&temp);
4471 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4472 debugstr_VF(pVarIn), pVarOut);
4474 /* Handle VT_DISPATCH by storing and taking address of returned value */
4475 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4477 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4478 if (FAILED(hRet)) goto VarFix_Exit;
4479 pVarIn = &temp;
4481 V_VT(pVarOut) = V_VT(pVarIn);
4483 switch (V_VT(pVarIn))
4485 case VT_UI1:
4486 V_UI1(pVarOut) = V_UI1(pVarIn);
4487 break;
4488 case VT_BOOL:
4489 V_VT(pVarOut) = VT_I2;
4490 /* Fall through */
4491 case VT_I2:
4492 V_I2(pVarOut) = V_I2(pVarIn);
4493 break;
4494 case VT_I4:
4495 V_I4(pVarOut) = V_I4(pVarIn);
4496 break;
4497 case VT_I8:
4498 V_I8(pVarOut) = V_I8(pVarIn);
4499 break;
4500 case VT_R4:
4501 if (V_R4(pVarIn) < 0.0f)
4502 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4503 else
4504 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4505 break;
4506 case VT_BSTR:
4507 V_VT(pVarOut) = VT_R8;
4508 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4509 pVarIn = pVarOut;
4510 /* Fall through */
4511 case VT_DATE:
4512 case VT_R8:
4513 if (V_R8(pVarIn) < 0.0)
4514 V_R8(pVarOut) = ceil(V_R8(pVarIn));
4515 else
4516 V_R8(pVarOut) = floor(V_R8(pVarIn));
4517 break;
4518 case VT_CY:
4519 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4520 break;
4521 case VT_DECIMAL:
4522 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4523 break;
4524 case VT_EMPTY:
4525 V_VT(pVarOut) = VT_I2;
4526 V_I2(pVarOut) = 0;
4527 break;
4528 case VT_NULL:
4529 /* No-Op */
4530 break;
4531 default:
4532 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4533 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4534 hRet = DISP_E_BADVARTYPE;
4535 else
4536 hRet = DISP_E_TYPEMISMATCH;
4538 VarFix_Exit:
4539 if (FAILED(hRet))
4540 V_VT(pVarOut) = VT_EMPTY;
4541 VariantClear(&temp);
4543 return hRet;
4546 /**********************************************************************
4547 * VarInt [OLEAUT32.172]
4549 * Truncate a variants value to a whole number.
4551 * PARAMS
4552 * pVarIn [I] Source variant
4553 * pVarOut [O] Destination for converted value
4555 * RETURNS
4556 * Success: S_OK. pVarOut contains the converted value.
4557 * Failure: An HRESULT error code indicating the error.
4559 * NOTES
4560 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4561 * according to the following table:
4562 *| Input Type Output Type
4563 *| ---------- -----------
4564 *| VT_BOOL VT_I2
4565 *| VT_EMPTY VT_I2
4566 *| VT_BSTR VT_R8
4567 *| All Others Unchanged
4568 * - The difference between this function and VarFix() is that VarFix() rounds
4569 * negative numbers towards 0, while this function rounds them away from zero.
4571 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4573 HRESULT hRet = S_OK;
4574 VARIANT temp;
4576 VariantInit(&temp);
4578 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4579 debugstr_VF(pVarIn), pVarOut);
4581 /* Handle VT_DISPATCH by storing and taking address of returned value */
4582 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4584 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4585 if (FAILED(hRet)) goto VarInt_Exit;
4586 pVarIn = &temp;
4588 V_VT(pVarOut) = V_VT(pVarIn);
4590 switch (V_VT(pVarIn))
4592 case VT_R4:
4593 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4594 break;
4595 case VT_BSTR:
4596 V_VT(pVarOut) = VT_R8;
4597 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4598 pVarIn = pVarOut;
4599 /* Fall through */
4600 case VT_DATE:
4601 case VT_R8:
4602 V_R8(pVarOut) = floor(V_R8(pVarIn));
4603 break;
4604 case VT_CY:
4605 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4606 break;
4607 case VT_DECIMAL:
4608 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4609 break;
4610 default:
4611 hRet = VarFix(pVarIn, pVarOut);
4613 VarInt_Exit:
4614 VariantClear(&temp);
4616 return hRet;
4619 /**********************************************************************
4620 * VarXor [OLEAUT32.167]
4622 * Perform a logical exclusive-or (XOR) operation on two variants.
4624 * PARAMS
4625 * pVarLeft [I] First variant
4626 * pVarRight [I] Variant to XOR with pVarLeft
4627 * pVarOut [O] Destination for XOR result
4629 * RETURNS
4630 * Success: S_OK. pVarOut contains the result of the operation with its type
4631 * taken from the table below).
4632 * Failure: An HRESULT error code indicating the error.
4634 * NOTES
4635 * - Neither pVarLeft or pVarRight are modified by this function.
4636 * - This function does not process by-reference variants.
4637 * - Input types of VT_BSTR may be numeric strings or boolean text.
4638 * - The type of result stored in pVarOut depends on the types of pVarLeft
4639 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4640 * or VT_NULL if the function succeeds.
4641 * - Type promotion is inconsistent and as a result certain combinations of
4642 * values will return DISP_E_OVERFLOW even when they could be represented.
4643 * This matches the behaviour of native oleaut32.
4645 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4647 VARTYPE vt;
4648 VARIANT varLeft, varRight;
4649 VARIANT tempLeft, tempRight;
4650 double d;
4651 HRESULT hRet;
4653 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4654 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4655 debugstr_VF(pVarRight), pVarOut);
4657 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4658 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4659 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4660 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4661 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4662 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4663 return DISP_E_BADVARTYPE;
4665 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4667 /* NULL XOR anything valid is NULL */
4668 V_VT(pVarOut) = VT_NULL;
4669 return S_OK;
4672 VariantInit(&tempLeft);
4673 VariantInit(&tempRight);
4675 /* Handle VT_DISPATCH by storing and taking address of returned value */
4676 if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4678 hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4679 if (FAILED(hRet)) goto VarXor_Exit;
4680 pVarLeft = &tempLeft;
4682 if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4684 hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4685 if (FAILED(hRet)) goto VarXor_Exit;
4686 pVarRight = &tempRight;
4689 /* Copy our inputs so we don't disturb anything */
4690 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4692 hRet = VariantCopy(&varLeft, pVarLeft);
4693 if (FAILED(hRet))
4694 goto VarXor_Exit;
4696 hRet = VariantCopy(&varRight, pVarRight);
4697 if (FAILED(hRet))
4698 goto VarXor_Exit;
4700 /* Try any strings first as numbers, then as VT_BOOL */
4701 if (V_VT(&varLeft) == VT_BSTR)
4703 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4704 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4705 FAILED(hRet) ? VT_BOOL : VT_I4);
4706 if (FAILED(hRet))
4707 goto VarXor_Exit;
4710 if (V_VT(&varRight) == VT_BSTR)
4712 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4713 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4714 FAILED(hRet) ? VT_BOOL : VT_I4);
4715 if (FAILED(hRet))
4716 goto VarXor_Exit;
4719 /* Determine the result type */
4720 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4722 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4724 hRet = DISP_E_TYPEMISMATCH;
4725 goto VarXor_Exit;
4727 vt = VT_I8;
4729 else
4731 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4733 case (VT_BOOL << 16) | VT_BOOL:
4734 vt = VT_BOOL;
4735 break;
4736 case (VT_UI1 << 16) | VT_UI1:
4737 vt = VT_UI1;
4738 break;
4739 case (VT_EMPTY << 16) | VT_EMPTY:
4740 case (VT_EMPTY << 16) | VT_UI1:
4741 case (VT_EMPTY << 16) | VT_I2:
4742 case (VT_EMPTY << 16) | VT_BOOL:
4743 case (VT_UI1 << 16) | VT_EMPTY:
4744 case (VT_UI1 << 16) | VT_I2:
4745 case (VT_UI1 << 16) | VT_BOOL:
4746 case (VT_I2 << 16) | VT_EMPTY:
4747 case (VT_I2 << 16) | VT_UI1:
4748 case (VT_I2 << 16) | VT_I2:
4749 case (VT_I2 << 16) | VT_BOOL:
4750 case (VT_BOOL << 16) | VT_EMPTY:
4751 case (VT_BOOL << 16) | VT_UI1:
4752 case (VT_BOOL << 16) | VT_I2:
4753 vt = VT_I2;
4754 break;
4755 default:
4756 vt = VT_I4;
4757 break;
4761 /* VT_UI4 does not overflow */
4762 if (vt != VT_I8)
4764 if (V_VT(&varLeft) == VT_UI4)
4765 V_VT(&varLeft) = VT_I4;
4766 if (V_VT(&varRight) == VT_UI4)
4767 V_VT(&varRight) = VT_I4;
4770 /* Convert our input copies to the result type */
4771 if (V_VT(&varLeft) != vt)
4772 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4773 if (FAILED(hRet))
4774 goto VarXor_Exit;
4776 if (V_VT(&varRight) != vt)
4777 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4778 if (FAILED(hRet))
4779 goto VarXor_Exit;
4781 V_VT(pVarOut) = vt;
4783 /* Calculate the result */
4784 switch (vt)
4786 case VT_I8:
4787 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4788 break;
4789 case VT_I4:
4790 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4791 break;
4792 case VT_BOOL:
4793 case VT_I2:
4794 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4795 break;
4796 case VT_UI1:
4797 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4798 break;
4801 VarXor_Exit:
4802 VariantClear(&varLeft);
4803 VariantClear(&varRight);
4804 VariantClear(&tempLeft);
4805 VariantClear(&tempRight);
4806 return hRet;
4809 /**********************************************************************
4810 * VarEqv [OLEAUT32.172]
4812 * Determine if two variants contain the same value.
4814 * PARAMS
4815 * pVarLeft [I] First variant to compare
4816 * pVarRight [I] Variant to compare to pVarLeft
4817 * pVarOut [O] Destination for comparison result
4819 * RETURNS
4820 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4821 * if equivalent or non-zero otherwise.
4822 * Failure: An HRESULT error code indicating the error.
4824 * NOTES
4825 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4826 * the result.
4828 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4830 HRESULT hRet;
4832 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
4833 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
4834 debugstr_VF(pVarRight), pVarOut);
4836 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4837 if (SUCCEEDED(hRet))
4839 if (V_VT(pVarOut) == VT_I8)
4840 V_I8(pVarOut) = ~V_I8(pVarOut);
4841 else
4842 V_UI4(pVarOut) = ~V_UI4(pVarOut);
4844 return hRet;
4847 /**********************************************************************
4848 * VarNeg [OLEAUT32.173]
4850 * Negate the value of a variant.
4852 * PARAMS
4853 * pVarIn [I] Source variant
4854 * pVarOut [O] Destination for converted value
4856 * RETURNS
4857 * Success: S_OK. pVarOut contains the converted value.
4858 * Failure: An HRESULT error code indicating the error.
4860 * NOTES
4861 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4862 * according to the following table:
4863 *| Input Type Output Type
4864 *| ---------- -----------
4865 *| VT_EMPTY VT_I2
4866 *| VT_UI1 VT_I2
4867 *| VT_BOOL VT_I2
4868 *| VT_BSTR VT_R8
4869 *| All Others Unchanged (unless promoted)
4870 * - Where the negated value of a variant does not fit in its base type, the type
4871 * is promoted according to the following table:
4872 *| Input Type Promoted To
4873 *| ---------- -----------
4874 *| VT_I2 VT_I4
4875 *| VT_I4 VT_R8
4876 *| VT_I8 VT_R8
4877 * - The native version of this function returns DISP_E_BADVARTYPE for valid
4878 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4879 * for types which are not valid. Since this is in contravention of the
4880 * meaning of those error codes and unlikely to be relied on by applications,
4881 * this implementation returns errors consistent with the other high level
4882 * variant math functions.
4884 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4886 HRESULT hRet = S_OK;
4887 VARIANT temp;
4889 VariantInit(&temp);
4891 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4892 debugstr_VF(pVarIn), pVarOut);
4894 /* Handle VT_DISPATCH by storing and taking address of returned value */
4895 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4897 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4898 if (FAILED(hRet)) goto VarNeg_Exit;
4899 pVarIn = &temp;
4901 V_VT(pVarOut) = V_VT(pVarIn);
4903 switch (V_VT(pVarIn))
4905 case VT_UI1:
4906 V_VT(pVarOut) = VT_I2;
4907 V_I2(pVarOut) = -V_UI1(pVarIn);
4908 break;
4909 case VT_BOOL:
4910 V_VT(pVarOut) = VT_I2;
4911 /* Fall through */
4912 case VT_I2:
4913 if (V_I2(pVarIn) == I2_MIN)
4915 V_VT(pVarOut) = VT_I4;
4916 V_I4(pVarOut) = -(int)V_I2(pVarIn);
4918 else
4919 V_I2(pVarOut) = -V_I2(pVarIn);
4920 break;
4921 case VT_I4:
4922 if (V_I4(pVarIn) == I4_MIN)
4924 V_VT(pVarOut) = VT_R8;
4925 V_R8(pVarOut) = -(double)V_I4(pVarIn);
4927 else
4928 V_I4(pVarOut) = -V_I4(pVarIn);
4929 break;
4930 case VT_I8:
4931 if (V_I8(pVarIn) == I8_MIN)
4933 V_VT(pVarOut) = VT_R8;
4934 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4935 V_R8(pVarOut) *= -1.0;
4937 else
4938 V_I8(pVarOut) = -V_I8(pVarIn);
4939 break;
4940 case VT_R4:
4941 V_R4(pVarOut) = -V_R4(pVarIn);
4942 break;
4943 case VT_DATE:
4944 case VT_R8:
4945 V_R8(pVarOut) = -V_R8(pVarIn);
4946 break;
4947 case VT_CY:
4948 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4949 break;
4950 case VT_DECIMAL:
4951 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4952 break;
4953 case VT_BSTR:
4954 V_VT(pVarOut) = VT_R8;
4955 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4956 V_R8(pVarOut) = -V_R8(pVarOut);
4957 break;
4958 case VT_EMPTY:
4959 V_VT(pVarOut) = VT_I2;
4960 V_I2(pVarOut) = 0;
4961 break;
4962 case VT_NULL:
4963 /* No-Op */
4964 break;
4965 default:
4966 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4967 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4968 hRet = DISP_E_BADVARTYPE;
4969 else
4970 hRet = DISP_E_TYPEMISMATCH;
4972 VarNeg_Exit:
4973 if (FAILED(hRet))
4974 V_VT(pVarOut) = VT_EMPTY;
4975 VariantClear(&temp);
4977 return hRet;
4980 /**********************************************************************
4981 * VarNot [OLEAUT32.174]
4983 * Perform a not operation on a variant.
4985 * PARAMS
4986 * pVarIn [I] Source variant
4987 * pVarOut [O] Destination for converted value
4989 * RETURNS
4990 * Success: S_OK. pVarOut contains the converted value.
4991 * Failure: An HRESULT error code indicating the error.
4993 * NOTES
4994 * - Strictly speaking, this function performs a bitwise ones complement
4995 * on the variants value (after possibly converting to VT_I4, see below).
4996 * This only behaves like a boolean not operation if the value in
4997 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4998 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4999 * before calling this function.
5000 * - This function does not process by-reference variants.
5001 * - The type of the value stored in pVarOut depends on the type of pVarIn,
5002 * according to the following table:
5003 *| Input Type Output Type
5004 *| ---------- -----------
5005 *| VT_EMPTY VT_I2
5006 *| VT_R4 VT_I4
5007 *| VT_R8 VT_I4
5008 *| VT_BSTR VT_I4
5009 *| VT_DECIMAL VT_I4
5010 *| VT_CY VT_I4
5011 *| (All others) Unchanged
5013 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
5015 VARIANT varIn;
5016 HRESULT hRet = S_OK;
5017 VARIANT temp;
5019 VariantInit(&temp);
5021 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
5022 debugstr_VF(pVarIn), pVarOut);
5024 /* Handle VT_DISPATCH by storing and taking address of returned value */
5025 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5027 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5028 if (FAILED(hRet)) goto VarNot_Exit;
5029 pVarIn = &temp;
5032 V_VT(pVarOut) = V_VT(pVarIn);
5034 switch (V_VT(pVarIn))
5036 case VT_I1:
5037 V_I4(pVarOut) = ~V_I1(pVarIn);
5038 V_VT(pVarOut) = VT_I4;
5039 break;
5040 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
5041 case VT_BOOL:
5042 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
5043 case VT_UI2:
5044 V_I4(pVarOut) = ~V_UI2(pVarIn);
5045 V_VT(pVarOut) = VT_I4;
5046 break;
5047 case VT_DECIMAL:
5048 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
5049 if (FAILED(hRet))
5050 break;
5051 pVarIn = &varIn;
5052 /* Fall through ... */
5053 case VT_INT:
5054 V_VT(pVarOut) = VT_I4;
5055 /* Fall through ... */
5056 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
5057 case VT_UINT:
5058 case VT_UI4:
5059 V_I4(pVarOut) = ~V_UI4(pVarIn);
5060 V_VT(pVarOut) = VT_I4;
5061 break;
5062 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
5063 case VT_UI8:
5064 V_I4(pVarOut) = ~V_UI8(pVarIn);
5065 V_VT(pVarOut) = VT_I4;
5066 break;
5067 case VT_R4:
5068 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
5069 V_I4(pVarOut) = ~V_I4(pVarOut);
5070 V_VT(pVarOut) = VT_I4;
5071 break;
5072 case VT_BSTR:
5073 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5074 if (FAILED(hRet))
5075 break;
5076 pVarIn = &varIn;
5077 /* Fall through ... */
5078 case VT_DATE:
5079 case VT_R8:
5080 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
5081 V_I4(pVarOut) = ~V_I4(pVarOut);
5082 V_VT(pVarOut) = VT_I4;
5083 break;
5084 case VT_CY:
5085 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
5086 V_I4(pVarOut) = ~V_I4(pVarOut);
5087 V_VT(pVarOut) = VT_I4;
5088 break;
5089 case VT_EMPTY:
5090 V_I2(pVarOut) = ~0;
5091 V_VT(pVarOut) = VT_I2;
5092 break;
5093 case VT_NULL:
5094 /* No-Op */
5095 break;
5096 default:
5097 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5098 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5099 hRet = DISP_E_BADVARTYPE;
5100 else
5101 hRet = DISP_E_TYPEMISMATCH;
5103 VarNot_Exit:
5104 if (FAILED(hRet))
5105 V_VT(pVarOut) = VT_EMPTY;
5106 VariantClear(&temp);
5108 return hRet;
5111 /**********************************************************************
5112 * VarRound [OLEAUT32.175]
5114 * Perform a round operation on a variant.
5116 * PARAMS
5117 * pVarIn [I] Source variant
5118 * deci [I] Number of decimals to round to
5119 * pVarOut [O] Destination for converted value
5121 * RETURNS
5122 * Success: S_OK. pVarOut contains the converted value.
5123 * Failure: An HRESULT error code indicating the error.
5125 * NOTES
5126 * - Floating point values are rounded to the desired number of decimals.
5127 * - Some integer types are just copied to the return variable.
5128 * - Some other integer types are not handled and fail.
5130 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5132 VARIANT varIn;
5133 HRESULT hRet = S_OK;
5134 float factor;
5135 VARIANT temp;
5137 VariantInit(&temp);
5139 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
5141 /* Handle VT_DISPATCH by storing and taking address of returned value */
5142 if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5144 hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5145 if (FAILED(hRet)) goto VarRound_Exit;
5146 pVarIn = &temp;
5149 switch (V_VT(pVarIn))
5151 /* cases that fail on windows */
5152 case VT_I1:
5153 case VT_I8:
5154 case VT_UI2:
5155 case VT_UI4:
5156 hRet = DISP_E_BADVARTYPE;
5157 break;
5159 /* cases just copying in to out */
5160 case VT_UI1:
5161 V_VT(pVarOut) = V_VT(pVarIn);
5162 V_UI1(pVarOut) = V_UI1(pVarIn);
5163 break;
5164 case VT_I2:
5165 V_VT(pVarOut) = V_VT(pVarIn);
5166 V_I2(pVarOut) = V_I2(pVarIn);
5167 break;
5168 case VT_I4:
5169 V_VT(pVarOut) = V_VT(pVarIn);
5170 V_I4(pVarOut) = V_I4(pVarIn);
5171 break;
5172 case VT_NULL:
5173 V_VT(pVarOut) = V_VT(pVarIn);
5174 /* value unchanged */
5175 break;
5177 /* cases that change type */
5178 case VT_EMPTY:
5179 V_VT(pVarOut) = VT_I2;
5180 V_I2(pVarOut) = 0;
5181 break;
5182 case VT_BOOL:
5183 V_VT(pVarOut) = VT_I2;
5184 V_I2(pVarOut) = V_BOOL(pVarIn);
5185 break;
5186 case VT_BSTR:
5187 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5188 if (FAILED(hRet))
5189 break;
5190 V_VT(&varIn)=VT_R8;
5191 pVarIn = &varIn;
5192 /* Fall through ... */
5194 /* cases we need to do math */
5195 case VT_R8:
5196 if (V_R8(pVarIn)>0) {
5197 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5198 } else {
5199 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5201 V_VT(pVarOut) = V_VT(pVarIn);
5202 break;
5203 case VT_R4:
5204 if (V_R4(pVarIn)>0) {
5205 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5206 } else {
5207 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5209 V_VT(pVarOut) = V_VT(pVarIn);
5210 break;
5211 case VT_DATE:
5212 if (V_DATE(pVarIn)>0) {
5213 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5214 } else {
5215 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5217 V_VT(pVarOut) = V_VT(pVarIn);
5218 break;
5219 case VT_CY:
5220 if (deci>3)
5221 factor=1;
5222 else
5223 factor=pow(10, 4-deci);
5225 if (V_CY(pVarIn).int64>0) {
5226 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5227 } else {
5228 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5230 V_VT(pVarOut) = V_VT(pVarIn);
5231 break;
5233 /* cases we don't know yet */
5234 default:
5235 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5236 V_VT(pVarIn) & VT_TYPEMASK, deci);
5237 hRet = DISP_E_BADVARTYPE;
5239 VarRound_Exit:
5240 if (FAILED(hRet))
5241 V_VT(pVarOut) = VT_EMPTY;
5242 VariantClear(&temp);
5244 TRACE("returning 0x%08x (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
5245 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
5246 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
5248 return hRet;
5251 /**********************************************************************
5252 * VarIdiv [OLEAUT32.153]
5254 * Converts input variants to integers and divides them.
5256 * PARAMS
5257 * left [I] Left hand variant
5258 * right [I] Right hand variant
5259 * result [O] Destination for quotient
5261 * RETURNS
5262 * Success: S_OK. result contains the quotient.
5263 * Failure: An HRESULT error code indicating the error.
5265 * NOTES
5266 * If either expression is null, null is returned, as per MSDN
5268 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5270 HRESULT hres = S_OK;
5271 VARTYPE resvt = VT_EMPTY;
5272 VARTYPE leftvt,rightvt;
5273 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5274 VARIANT lv,rv;
5275 VARIANT tempLeft, tempRight;
5277 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5278 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5280 VariantInit(&lv);
5281 VariantInit(&rv);
5282 VariantInit(&tempLeft);
5283 VariantInit(&tempRight);
5285 leftvt = V_VT(left)&VT_TYPEMASK;
5286 rightvt = V_VT(right)&VT_TYPEMASK;
5287 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5288 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5290 if (leftExtraFlags != rightExtraFlags)
5292 hres = DISP_E_BADVARTYPE;
5293 goto end;
5295 ExtraFlags = leftExtraFlags;
5297 /* Native VarIdiv always returns an error when using extra
5298 * flags or if the variant combination is I8 and INT.
5300 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5301 (leftvt == VT_INT && rightvt == VT_I8) ||
5302 (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5303 ExtraFlags != 0)
5305 hres = DISP_E_BADVARTYPE;
5306 goto end;
5309 /* Determine variant type */
5310 else if (leftvt == VT_NULL || rightvt == VT_NULL)
5312 V_VT(result) = VT_NULL;
5313 hres = S_OK;
5314 goto end;
5316 else if (leftvt == VT_I8 || rightvt == VT_I8)
5317 resvt = VT_I8;
5318 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5319 leftvt == VT_INT || rightvt == VT_INT ||
5320 leftvt == VT_UINT || rightvt == VT_UINT ||
5321 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5322 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5323 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5324 leftvt == VT_I1 || rightvt == VT_I1 ||
5325 leftvt == VT_BSTR || rightvt == VT_BSTR ||
5326 leftvt == VT_DATE || rightvt == VT_DATE ||
5327 leftvt == VT_CY || rightvt == VT_CY ||
5328 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5329 leftvt == VT_R8 || rightvt == VT_R8 ||
5330 leftvt == VT_R4 || rightvt == VT_R4)
5331 resvt = VT_I4;
5332 else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5333 leftvt == VT_BOOL || rightvt == VT_BOOL ||
5334 leftvt == VT_EMPTY)
5335 resvt = VT_I2;
5336 else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5337 resvt = VT_UI1;
5338 else
5340 hres = DISP_E_BADVARTYPE;
5341 goto end;
5344 /* coerce to the result type */
5345 hres = VariantChangeType(&lv, left, 0, resvt);
5346 if (hres != S_OK) goto end;
5347 hres = VariantChangeType(&rv, right, 0, resvt);
5348 if (hres != S_OK) goto end;
5350 /* do the math */
5351 V_VT(result) = resvt;
5352 switch (resvt)
5354 case VT_UI1:
5355 if (V_UI1(&rv) == 0)
5357 hres = DISP_E_DIVBYZERO;
5358 V_VT(result) = VT_EMPTY;
5360 else
5361 V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5362 break;
5363 case VT_I2:
5364 if (V_I2(&rv) == 0)
5366 hres = DISP_E_DIVBYZERO;
5367 V_VT(result) = VT_EMPTY;
5369 else
5370 V_I2(result) = V_I2(&lv) / V_I2(&rv);
5371 break;
5372 case VT_I4:
5373 if (V_I4(&rv) == 0)
5375 hres = DISP_E_DIVBYZERO;
5376 V_VT(result) = VT_EMPTY;
5378 else
5379 V_I4(result) = V_I4(&lv) / V_I4(&rv);
5380 break;
5381 case VT_I8:
5382 if (V_I8(&rv) == 0)
5384 hres = DISP_E_DIVBYZERO;
5385 V_VT(result) = VT_EMPTY;
5387 else
5388 V_I8(result) = V_I8(&lv) / V_I8(&rv);
5389 break;
5390 default:
5391 FIXME("Couldn't integer divide variant types %d,%d\n",
5392 leftvt,rightvt);
5395 end:
5396 VariantClear(&lv);
5397 VariantClear(&rv);
5398 VariantClear(&tempLeft);
5399 VariantClear(&tempRight);
5401 return hres;
5405 /**********************************************************************
5406 * VarMod [OLEAUT32.155]
5408 * Perform the modulus operation of the right hand variant on the left
5410 * PARAMS
5411 * left [I] Left hand variant
5412 * right [I] Right hand variant
5413 * result [O] Destination for converted value
5415 * RETURNS
5416 * Success: S_OK. result contains the remainder.
5417 * Failure: An HRESULT error code indicating the error.
5419 * NOTE:
5420 * If an error occurs the type of result will be modified but the value will not be.
5421 * Doesn't support arrays or any special flags yet.
5423 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5425 BOOL lOk = TRUE;
5426 HRESULT rc = E_FAIL;
5427 int resT = 0;
5428 VARIANT lv,rv;
5429 VARIANT tempLeft, tempRight;
5431 VariantInit(&tempLeft);
5432 VariantInit(&tempRight);
5433 VariantInit(&lv);
5434 VariantInit(&rv);
5436 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5437 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5439 /* Handle VT_DISPATCH by storing and taking address of returned value */
5440 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5442 rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5443 if (FAILED(rc)) goto end;
5444 left = &tempLeft;
5446 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5448 rc = VARIANT_FetchDispatchValue(right, &tempRight);
5449 if (FAILED(rc)) goto end;
5450 right = &tempRight;
5453 /* check for invalid inputs */
5454 lOk = TRUE;
5455 switch (V_VT(left) & VT_TYPEMASK) {
5456 case VT_BOOL :
5457 case VT_I1 :
5458 case VT_I2 :
5459 case VT_I4 :
5460 case VT_I8 :
5461 case VT_INT :
5462 case VT_UI1 :
5463 case VT_UI2 :
5464 case VT_UI4 :
5465 case VT_UI8 :
5466 case VT_UINT :
5467 case VT_R4 :
5468 case VT_R8 :
5469 case VT_CY :
5470 case VT_EMPTY:
5471 case VT_DATE :
5472 case VT_BSTR :
5473 case VT_DECIMAL:
5474 break;
5475 case VT_VARIANT:
5476 case VT_UNKNOWN:
5477 V_VT(result) = VT_EMPTY;
5478 rc = DISP_E_TYPEMISMATCH;
5479 goto end;
5480 case VT_ERROR:
5481 rc = DISP_E_TYPEMISMATCH;
5482 goto end;
5483 case VT_RECORD:
5484 V_VT(result) = VT_EMPTY;
5485 rc = DISP_E_TYPEMISMATCH;
5486 goto end;
5487 case VT_NULL:
5488 break;
5489 default:
5490 V_VT(result) = VT_EMPTY;
5491 rc = DISP_E_BADVARTYPE;
5492 goto end;
5496 switch (V_VT(right) & VT_TYPEMASK) {
5497 case VT_BOOL :
5498 case VT_I1 :
5499 case VT_I2 :
5500 case VT_I4 :
5501 case VT_I8 :
5502 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5504 V_VT(result) = VT_EMPTY;
5505 rc = DISP_E_TYPEMISMATCH;
5506 goto end;
5508 case VT_INT :
5509 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5511 V_VT(result) = VT_EMPTY;
5512 rc = DISP_E_TYPEMISMATCH;
5513 goto end;
5515 case VT_UI1 :
5516 case VT_UI2 :
5517 case VT_UI4 :
5518 case VT_UI8 :
5519 case VT_UINT :
5520 case VT_R4 :
5521 case VT_R8 :
5522 case VT_CY :
5523 if(V_VT(left) == VT_EMPTY)
5525 V_VT(result) = VT_I4;
5526 rc = S_OK;
5527 goto end;
5529 case VT_EMPTY:
5530 case VT_DATE :
5531 case VT_DECIMAL:
5532 if(V_VT(left) == VT_ERROR)
5534 V_VT(result) = VT_EMPTY;
5535 rc = DISP_E_TYPEMISMATCH;
5536 goto end;
5538 case VT_BSTR:
5539 if(V_VT(left) == VT_NULL)
5541 V_VT(result) = VT_NULL;
5542 rc = S_OK;
5543 goto end;
5545 break;
5547 case VT_VOID:
5548 V_VT(result) = VT_EMPTY;
5549 rc = DISP_E_BADVARTYPE;
5550 goto end;
5551 case VT_NULL:
5552 if(V_VT(left) == VT_VOID)
5554 V_VT(result) = VT_EMPTY;
5555 rc = DISP_E_BADVARTYPE;
5556 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5557 lOk)
5559 V_VT(result) = VT_NULL;
5560 rc = S_OK;
5561 } else
5563 V_VT(result) = VT_NULL;
5564 rc = DISP_E_BADVARTYPE;
5566 goto end;
5567 case VT_VARIANT:
5568 case VT_UNKNOWN:
5569 V_VT(result) = VT_EMPTY;
5570 rc = DISP_E_TYPEMISMATCH;
5571 goto end;
5572 case VT_ERROR:
5573 rc = DISP_E_TYPEMISMATCH;
5574 goto end;
5575 case VT_RECORD:
5576 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5578 V_VT(result) = VT_EMPTY;
5579 rc = DISP_E_BADVARTYPE;
5580 } else
5582 V_VT(result) = VT_EMPTY;
5583 rc = DISP_E_TYPEMISMATCH;
5585 goto end;
5586 default:
5587 V_VT(result) = VT_EMPTY;
5588 rc = DISP_E_BADVARTYPE;
5589 goto end;
5592 /* determine the result type */
5593 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5594 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5595 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5596 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5597 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5598 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5599 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5600 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5601 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5602 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5603 else resT = VT_I4; /* most outputs are I4 */
5605 /* convert to I8 for the modulo */
5606 rc = VariantChangeType(&lv, left, 0, VT_I8);
5607 if(FAILED(rc))
5609 FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5610 goto end;
5613 rc = VariantChangeType(&rv, right, 0, VT_I8);
5614 if(FAILED(rc))
5616 FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5617 goto end;
5620 /* if right is zero set VT_EMPTY and return divide by zero */
5621 if(V_I8(&rv) == 0)
5623 V_VT(result) = VT_EMPTY;
5624 rc = DISP_E_DIVBYZERO;
5625 goto end;
5628 /* perform the modulo operation */
5629 V_VT(result) = VT_I8;
5630 V_I8(result) = V_I8(&lv) % V_I8(&rv);
5632 TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5633 wine_dbgstr_longlong(V_I8(&lv)), wine_dbgstr_longlong(V_I8(&rv)),
5634 wine_dbgstr_longlong(V_I8(result)));
5636 /* convert left and right to the destination type */
5637 rc = VariantChangeType(result, result, 0, resT);
5638 if(FAILED(rc))
5640 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5641 /* fall to end of function */
5644 end:
5645 VariantClear(&lv);
5646 VariantClear(&rv);
5647 VariantClear(&tempLeft);
5648 VariantClear(&tempRight);
5649 return rc;
5652 /**********************************************************************
5653 * VarPow [OLEAUT32.158]
5655 * Computes the power of one variant to another variant.
5657 * PARAMS
5658 * left [I] First variant
5659 * right [I] Second variant
5660 * result [O] Result variant
5662 * RETURNS
5663 * Success: S_OK.
5664 * Failure: An HRESULT error code indicating the error.
5666 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5668 HRESULT hr = S_OK;
5669 VARIANT dl,dr;
5670 VARTYPE resvt = VT_EMPTY;
5671 VARTYPE leftvt,rightvt;
5672 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5673 VARIANT tempLeft, tempRight;
5675 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
5676 right, debugstr_VT(right), debugstr_VF(right), result);
5678 VariantInit(&dl);
5679 VariantInit(&dr);
5680 VariantInit(&tempLeft);
5681 VariantInit(&tempRight);
5683 /* Handle VT_DISPATCH by storing and taking address of returned value */
5684 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5686 hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5687 if (FAILED(hr)) goto end;
5688 left = &tempLeft;
5690 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5692 hr = VARIANT_FetchDispatchValue(right, &tempRight);
5693 if (FAILED(hr)) goto end;
5694 right = &tempRight;
5697 leftvt = V_VT(left)&VT_TYPEMASK;
5698 rightvt = V_VT(right)&VT_TYPEMASK;
5699 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5700 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5702 if (leftExtraFlags != rightExtraFlags)
5704 hr = DISP_E_BADVARTYPE;
5705 goto end;
5707 ExtraFlags = leftExtraFlags;
5709 /* Native VarPow always returns an error when using extra flags */
5710 if (ExtraFlags != 0)
5712 hr = DISP_E_BADVARTYPE;
5713 goto end;
5716 /* Determine return type */
5717 else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5718 V_VT(result) = VT_NULL;
5719 hr = S_OK;
5720 goto end;
5722 else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5723 leftvt == VT_I4 || leftvt == VT_R4 ||
5724 leftvt == VT_R8 || leftvt == VT_CY ||
5725 leftvt == VT_DATE || leftvt == VT_BSTR ||
5726 leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5727 (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5728 (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5729 rightvt == VT_I4 || rightvt == VT_R4 ||
5730 rightvt == VT_R8 || rightvt == VT_CY ||
5731 rightvt == VT_DATE || rightvt == VT_BSTR ||
5732 rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5733 (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5734 resvt = VT_R8;
5735 else
5737 hr = DISP_E_BADVARTYPE;
5738 goto end;
5741 hr = VariantChangeType(&dl,left,0,resvt);
5742 if (FAILED(hr)) {
5743 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5744 hr = E_FAIL;
5745 goto end;
5748 hr = VariantChangeType(&dr,right,0,resvt);
5749 if (FAILED(hr)) {
5750 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5751 hr = E_FAIL;
5752 goto end;
5755 V_VT(result) = VT_R8;
5756 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5758 end:
5759 VariantClear(&dl);
5760 VariantClear(&dr);
5761 VariantClear(&tempLeft);
5762 VariantClear(&tempRight);
5764 return hr;
5767 /**********************************************************************
5768 * VarImp [OLEAUT32.154]
5770 * Bitwise implication of two variants.
5772 * PARAMS
5773 * left [I] First variant
5774 * right [I] Second variant
5775 * result [O] Result variant
5777 * RETURNS
5778 * Success: S_OK.
5779 * Failure: An HRESULT error code indicating the error.
5781 HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5783 HRESULT hres = S_OK;
5784 VARTYPE resvt = VT_EMPTY;
5785 VARTYPE leftvt,rightvt;
5786 VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5787 VARIANT lv,rv;
5788 double d;
5789 VARIANT tempLeft, tempRight;
5791 VariantInit(&lv);
5792 VariantInit(&rv);
5793 VariantInit(&tempLeft);
5794 VariantInit(&tempRight);
5796 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
5797 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
5799 /* Handle VT_DISPATCH by storing and taking address of returned value */
5800 if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5802 hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5803 if (FAILED(hres)) goto VarImp_Exit;
5804 left = &tempLeft;
5806 if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5808 hres = VARIANT_FetchDispatchValue(right, &tempRight);
5809 if (FAILED(hres)) goto VarImp_Exit;
5810 right = &tempRight;
5813 leftvt = V_VT(left)&VT_TYPEMASK;
5814 rightvt = V_VT(right)&VT_TYPEMASK;
5815 leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5816 rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5818 if (leftExtraFlags != rightExtraFlags)
5820 hres = DISP_E_BADVARTYPE;
5821 goto VarImp_Exit;
5823 ExtraFlags = leftExtraFlags;
5825 /* Native VarImp always returns an error when using extra
5826 * flags or if the variants are I8 and INT.
5828 if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5829 ExtraFlags != 0)
5831 hres = DISP_E_BADVARTYPE;
5832 goto VarImp_Exit;
5835 /* Determine result type */
5836 else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5837 (leftvt == VT_NULL && rightvt == VT_EMPTY))
5839 V_VT(result) = VT_NULL;
5840 hres = S_OK;
5841 goto VarImp_Exit;
5843 else if (leftvt == VT_I8 || rightvt == VT_I8)
5844 resvt = VT_I8;
5845 else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5846 leftvt == VT_INT || rightvt == VT_INT ||
5847 leftvt == VT_UINT || rightvt == VT_UINT ||
5848 leftvt == VT_UI4 || rightvt == VT_UI4 ||
5849 leftvt == VT_UI8 || rightvt == VT_UI8 ||
5850 leftvt == VT_UI2 || rightvt == VT_UI2 ||
5851 leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5852 leftvt == VT_DATE || rightvt == VT_DATE ||
5853 leftvt == VT_CY || rightvt == VT_CY ||
5854 leftvt == VT_R8 || rightvt == VT_R8 ||
5855 leftvt == VT_R4 || rightvt == VT_R4 ||
5856 leftvt == VT_I1 || rightvt == VT_I1)
5857 resvt = VT_I4;
5858 else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5859 (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5860 (leftvt == VT_NULL && rightvt == VT_UI1))
5861 resvt = VT_UI1;
5862 else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5863 leftvt == VT_I2 || rightvt == VT_I2 ||
5864 leftvt == VT_UI1 || rightvt == VT_UI1)
5865 resvt = VT_I2;
5866 else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5867 leftvt == VT_BSTR || rightvt == VT_BSTR)
5868 resvt = VT_BOOL;
5870 /* VT_NULL requires special handling for when the opposite
5871 * variant is equal to something other than -1.
5872 * (NULL Imp 0 = NULL, NULL Imp n = n)
5874 if (leftvt == VT_NULL)
5876 VARIANT_BOOL b;
5877 switch(rightvt)
5879 case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5880 case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5881 case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5882 case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5883 case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5884 case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5885 case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5886 case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5887 case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5888 case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5889 case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5890 case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5891 case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5892 case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5893 case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5894 case VT_DECIMAL:
5895 if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5896 resvt = VT_NULL;
5897 break;
5898 case VT_BSTR:
5899 hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5900 if (FAILED(hres)) goto VarImp_Exit;
5901 else if (!b)
5902 V_VT(result) = VT_NULL;
5903 else
5905 V_VT(result) = VT_BOOL;
5906 V_BOOL(result) = b;
5908 goto VarImp_Exit;
5910 if (resvt == VT_NULL)
5912 V_VT(result) = resvt;
5913 goto VarImp_Exit;
5915 else
5917 hres = VariantChangeType(result,right,0,resvt);
5918 goto VarImp_Exit;
5922 /* Special handling is required when NULL is the right variant.
5923 * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5925 else if (rightvt == VT_NULL)
5927 VARIANT_BOOL b;
5928 switch(leftvt)
5930 case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5931 case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5932 case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5933 case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5934 case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5935 case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5936 case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5937 case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5938 case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5939 case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5940 case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5941 case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5942 case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5943 case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5944 case VT_DECIMAL:
5945 if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5946 resvt = VT_NULL;
5947 break;
5948 case VT_BSTR:
5949 hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5950 if (FAILED(hres)) goto VarImp_Exit;
5951 else if (b == VARIANT_TRUE)
5952 resvt = VT_NULL;
5954 if (resvt == VT_NULL)
5956 V_VT(result) = resvt;
5957 goto VarImp_Exit;
5961 hres = VariantCopy(&lv, left);
5962 if (FAILED(hres)) goto VarImp_Exit;
5964 if (rightvt == VT_NULL)
5966 memset( &rv, 0, sizeof(rv) );
5967 V_VT(&rv) = resvt;
5969 else
5971 hres = VariantCopy(&rv, right);
5972 if (FAILED(hres)) goto VarImp_Exit;
5975 if (V_VT(&lv) == VT_BSTR &&
5976 FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5977 hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5978 if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5979 hres = VariantChangeType(&lv,&lv,0,resvt);
5980 if (FAILED(hres)) goto VarImp_Exit;
5982 if (V_VT(&rv) == VT_BSTR &&
5983 FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5984 hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5985 if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5986 hres = VariantChangeType(&rv, &rv, 0, resvt);
5987 if (FAILED(hres)) goto VarImp_Exit;
5989 /* do the math */
5990 V_VT(result) = resvt;
5991 switch (resvt)
5993 case VT_I8:
5994 V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5995 break;
5996 case VT_I4:
5997 V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5998 break;
5999 case VT_I2:
6000 V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
6001 break;
6002 case VT_UI1:
6003 V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
6004 break;
6005 case VT_BOOL:
6006 V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
6007 break;
6008 default:
6009 FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
6010 leftvt,rightvt);
6013 VarImp_Exit:
6015 VariantClear(&lv);
6016 VariantClear(&rv);
6017 VariantClear(&tempLeft);
6018 VariantClear(&tempRight);
6020 return hres;