4 * Copyright 1998 Jean-Claude Cote
5 * Copyright 2003 Jon Griffiths
6 * Copyright 2005 Daniel Remenak
8 * The alorithm for conversion from Julian days to day/month/year is based on
9 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
10 * Copyright 1994-7 Regents of the University of California
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
39 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(variant);
46 const char* wine_vtypes[VT_CLSID] =
48 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
49 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
50 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
51 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
52 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR""32","33","34","35",
53 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
54 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
55 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
56 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
59 const char* wine_vflags[16] =
64 "|VT_VECTOR|VT_ARRAY",
66 "|VT_VECTOR|VT_ARRAY",
68 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
70 "|VT_VECTOR|VT_HARDTYPE",
71 "|VT_ARRAY|VT_HARDTYPE",
72 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
73 "|VT_BYREF|VT_HARDTYPE",
74 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
75 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
76 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
79 /* Convert a variant from one type to another */
80 static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
81 VARIANTARG* ps, VARTYPE vt)
83 HRESULT res = DISP_E_TYPEMISMATCH;
84 VARTYPE vtFrom = V_TYPE(ps);
87 TRACE("(%p->(%s%s),0x%08lx,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
88 debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
89 debugstr_vt(vt), debugstr_vf(vt));
91 if (vt == VT_BSTR || vtFrom == VT_BSTR)
93 /* All flags passed to low level function are only used for
94 * changing to or from strings. Map these here.
96 if (wFlags & VARIANT_LOCALBOOL)
97 dwFlags |= VAR_LOCALBOOL;
98 if (wFlags & VARIANT_CALENDAR_HIJRI)
99 dwFlags |= VAR_CALENDAR_HIJRI;
100 if (wFlags & VARIANT_CALENDAR_THAI)
101 dwFlags |= VAR_CALENDAR_THAI;
102 if (wFlags & VARIANT_CALENDAR_GREGORIAN)
103 dwFlags |= VAR_CALENDAR_GREGORIAN;
104 if (wFlags & VARIANT_NOUSEROVERRIDE)
105 dwFlags |= LOCALE_NOUSEROVERRIDE;
106 if (wFlags & VARIANT_USE_NLS)
107 dwFlags |= LOCALE_USE_NLS;
110 /* Map int/uint to i4/ui4 */
113 else if (vt == VT_UINT)
116 if (vtFrom == VT_INT)
118 else if (vtFrom == VT_UINT)
122 return VariantCopy(pd, ps);
124 if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
126 /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
127 * accessing the default object property.
129 return DISP_E_TYPEMISMATCH;
135 if (vtFrom == VT_NULL)
136 return DISP_E_TYPEMISMATCH;
137 /* ... Fall through */
139 if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
141 res = VariantClear( pd );
142 if (vt == VT_NULL && SUCCEEDED(res))
150 case VT_EMPTY: V_I1(pd) = 0; return S_OK;
151 case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
152 case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
153 case VT_UI1: V_I1(pd) = V_UI1(ps); return S_OK;
154 case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
155 case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
156 case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
157 case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
158 case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
159 case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
160 case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
161 case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
162 case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
163 case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
164 case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
165 case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
172 case VT_EMPTY: V_I2(pd) = 0; return S_OK;
173 case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
174 case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
175 case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
176 case VT_UI2: V_I2(pd) = V_UI2(ps); return S_OK;
177 case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
178 case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
179 case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
180 case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
181 case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
182 case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
183 case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
184 case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
185 case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
186 case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
187 case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
194 case VT_EMPTY: V_I4(pd) = 0; return S_OK;
195 case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
196 case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
197 case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
198 case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
199 case VT_UI4: V_I4(pd) = V_UI4(ps); return S_OK;
200 case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
201 case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
202 case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
203 case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
204 case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
205 case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
206 case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
207 case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
208 case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
209 case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
216 case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
217 case VT_I1: V_UI1(pd) = V_I1(ps); return S_OK;
218 case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
219 case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
220 case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
221 case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
222 case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
223 case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
224 case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
225 case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
226 case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
227 case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
228 case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
229 case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
230 case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
231 case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
238 case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
239 case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
240 case VT_I2: V_UI2(pd) = V_I2(ps); return S_OK;
241 case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
242 case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
243 case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
244 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
245 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
246 case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
247 case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
248 case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
249 case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
250 case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
251 case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
252 case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
253 case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
260 case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
261 case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
262 case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
263 case VT_I4: V_UI4(pd) = V_I4(ps); return S_OK;
264 case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
265 case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
266 case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
267 case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
268 case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
269 case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
270 case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
271 case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
272 case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
273 case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
274 case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
275 case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
282 case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
283 case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
284 case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
285 case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
286 case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
287 case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
288 case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
289 case VT_I8: V_UI8(pd) = V_I8(ps); return S_OK;
290 case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
291 case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
292 case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
293 case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
294 case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
295 case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
296 case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
297 case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
304 case VT_EMPTY: V_I8(pd) = 0; return S_OK;
305 case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
306 case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
307 case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
308 case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
309 case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
310 case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
311 case VT_UI8: V_I8(pd) = V_UI8(ps); return S_OK;
312 case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
313 case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
314 case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
315 case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
316 case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
317 case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
318 case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
319 case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
326 case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
327 case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
328 case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
329 case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
330 case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
331 case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
332 case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
333 case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
334 case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
335 case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
336 case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
337 case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
338 case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
339 case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
340 case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
341 case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
348 case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
349 case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
350 case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
351 case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
352 case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
353 case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
354 case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
355 case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
356 case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
357 case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
358 case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
359 case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
360 case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
361 case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
362 case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
363 case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
370 case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
371 case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
372 case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
373 case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
374 case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
375 case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
376 case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
377 case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
378 case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
379 case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
380 case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
381 case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
382 case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
383 case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
384 case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
385 case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
392 case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
393 case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
394 case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
395 case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
396 case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
397 case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
398 case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
399 case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
400 case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
401 case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
402 case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
403 case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
404 case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
405 case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
406 case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
407 case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
415 V_BSTR(pd) = SysAllocStringLen(NULL, 0);
416 return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
418 if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
419 return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
420 return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
421 case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
422 case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
423 case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
424 case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
425 case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
426 case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
427 case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
428 case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
429 case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
430 case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
431 case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
432 case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
433 case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
434 /* case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd)); */
441 case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
442 case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
443 case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
444 case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
445 case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
446 case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
447 case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
448 case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
449 case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
450 case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
451 case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
452 case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
453 case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
454 case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
455 case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
456 case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
465 DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
466 DEC_HI32(&V_DECIMAL(pd)) = 0;
467 DEC_MID32(&V_DECIMAL(pd)) = 0;
468 /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
469 * VT_NULL and VT_EMPTY always give a 0 value.
471 DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
473 case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
474 case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
475 case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
476 case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
477 case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
478 case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
479 case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
480 case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
481 case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
482 case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
483 case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
484 case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
485 case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
486 case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
494 if (V_DISPATCH(ps) == NULL)
495 V_UNKNOWN(pd) = NULL;
497 res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
506 if (V_UNKNOWN(ps) == NULL)
507 V_DISPATCH(pd) = NULL;
509 res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
520 /* Coerce to/from an array */
521 static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
523 if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
524 return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
526 if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
527 return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));
530 return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
532 return DISP_E_TYPEMISMATCH;
535 /******************************************************************************
536 * Check if a variants type is valid.
538 static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
540 VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
544 if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
546 if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
548 if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
549 return DISP_E_BADVARTYPE;
550 if (vt != (VARTYPE)15)
554 return DISP_E_BADVARTYPE;
557 /******************************************************************************
558 * VariantInit [OLEAUT32.8]
560 * Initialise a variant.
563 * pVarg [O] Variant to initialise
569 * This function simply sets the type of the variant to VT_EMPTY. It does not
570 * free any existing value, use VariantClear() for that.
572 void WINAPI VariantInit(VARIANTARG* pVarg)
574 TRACE("(%p)\n", pVarg);
576 V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
579 /******************************************************************************
580 * VariantClear [OLEAUT32.9]
585 * pVarg [I/O] Variant to clear
588 * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
589 * Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
591 HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
595 TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
597 hres = VARIANT_ValidateType(V_VT(pVarg));
601 if (!V_ISBYREF(pVarg))
603 if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
606 hres = SafeArrayDestroy(V_ARRAY(pVarg));
608 else if (V_VT(pVarg) == VT_BSTR)
611 SysFreeString(V_BSTR(pVarg));
613 else if (V_VT(pVarg) == VT_RECORD)
615 struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
618 IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
619 IRecordInfo_Release(pBr->pRecInfo);
622 else if (V_VT(pVarg) == VT_DISPATCH ||
623 V_VT(pVarg) == VT_UNKNOWN)
625 if (V_UNKNOWN(pVarg))
626 IUnknown_Release(V_UNKNOWN(pVarg));
628 else if (V_VT(pVarg) == VT_VARIANT)
630 if (V_VARIANTREF(pVarg))
631 VariantClear(V_VARIANTREF(pVarg));
634 V_VT(pVarg) = VT_EMPTY;
639 /******************************************************************************
640 * Copy an IRecordInfo object contained in a variant.
642 static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
650 hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
653 PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
655 hres = E_OUTOFMEMORY;
658 memcpy(pvRecord, pBr->pvRecord, ulSize);
659 pBr->pvRecord = pvRecord;
661 hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
663 IRecordInfo_AddRef(pBr->pRecInfo);
667 else if (pBr->pvRecord)
672 /******************************************************************************
673 * VariantCopy [OLEAUT32.10]
678 * pvargDest [O] Destination for copy
679 * pvargSrc [I] Source variant to copy
682 * Success: S_OK. pvargDest contains a copy of pvargSrc.
683 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
684 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
685 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
686 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
689 * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
690 * pvargSrc is valid. Otherwise, pvargDest is always cleared using
691 * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
692 * fails, so does this function.
693 * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
694 * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
695 * is copied rather than just any pointers to it.
696 * - For by-value object types the object pointer is copied and the objects
697 * reference count increased using IUnknown_AddRef().
698 * - For all by-reference types, only the referencing pointer is copied.
700 HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
704 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
705 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
706 debugstr_VF(pvargSrc));
708 if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
709 FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
710 return DISP_E_BADVARTYPE;
712 if (pvargSrc != pvargDest &&
713 SUCCEEDED(hres = VariantClear(pvargDest)))
715 *pvargDest = *pvargSrc; /* Shallow copy the value */
717 if (!V_ISBYREF(pvargSrc))
719 if (V_ISARRAY(pvargSrc))
721 if (V_ARRAY(pvargSrc))
722 hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
724 else if (V_VT(pvargSrc) == VT_BSTR)
726 if (V_BSTR(pvargSrc))
728 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
729 if (!V_BSTR(pvargDest))
731 TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
732 hres = E_OUTOFMEMORY;
736 else if (V_VT(pvargSrc) == VT_RECORD)
738 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
740 else if (V_VT(pvargSrc) == VT_DISPATCH ||
741 V_VT(pvargSrc) == VT_UNKNOWN)
743 if (V_UNKNOWN(pvargSrc))
744 IUnknown_AddRef(V_UNKNOWN(pvargSrc));
751 /* Return the byte size of a variants data */
752 static inline size_t VARIANT_DataSize(const VARIANT* pv)
757 case VT_UI1: return sizeof(BYTE);
759 case VT_UI2: return sizeof(SHORT);
763 case VT_UI4: return sizeof(LONG);
765 case VT_UI8: return sizeof(LONGLONG);
766 case VT_R4: return sizeof(float);
767 case VT_R8: return sizeof(double);
768 case VT_DATE: return sizeof(DATE);
769 case VT_BOOL: return sizeof(VARIANT_BOOL);
772 case VT_BSTR: return sizeof(void*);
773 case VT_CY: return sizeof(CY);
774 case VT_ERROR: return sizeof(SCODE);
776 TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
780 /******************************************************************************
781 * VariantCopyInd [OLEAUT32.11]
783 * Copy a variant, dereferencing it it is by-reference.
786 * pvargDest [O] Destination for copy
787 * pvargSrc [I] Source variant to copy
790 * Success: S_OK. pvargDest contains a copy of pvargSrc.
791 * Failure: An HRESULT error code indicating the error.
794 * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
795 * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
796 * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
797 * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
798 * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
801 * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
802 * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
804 * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
805 * pvargDest is always cleared using VariantClear() before pvargSrc is copied
806 * to it. If clearing pvargDest fails, so does this function.
808 HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
810 VARIANTARG vTmp, *pSrc = pvargSrc;
814 TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
815 debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
816 debugstr_VF(pvargSrc));
818 if (!V_ISBYREF(pvargSrc))
819 return VariantCopy(pvargDest, pvargSrc);
821 /* Argument checking is more lax than VariantCopy()... */
822 vt = V_TYPE(pvargSrc);
823 if (V_ISARRAY(pvargSrc) ||
824 (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
825 !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
830 return E_INVALIDARG; /* ...And the return value for invalid types differs too */
832 if (pvargSrc == pvargDest)
834 /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
835 * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
839 V_VT(pvargDest) = VT_EMPTY;
843 /* Copy into another variant. Free the variant in pvargDest */
844 if (FAILED(hres = VariantClear(pvargDest)))
846 TRACE("VariantClear() of destination failed\n");
853 /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
854 hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
856 else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
858 /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
859 V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
861 else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
863 V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
864 hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
866 else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
867 V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
869 /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
870 V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
871 if (*V_UNKNOWNREF(pSrc))
872 IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
874 else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
876 /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
877 if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
878 hres = E_INVALIDARG; /* Don't dereference more than one level */
880 hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
882 /* Use the dereferenced variants type value, not VT_VARIANT */
883 goto VariantCopyInd_Return;
885 else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
887 memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
888 sizeof(DECIMAL) - sizeof(USHORT));
892 /* Copy the pointed to data into this variant */
893 memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
896 V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
898 VariantCopyInd_Return:
900 if (pSrc != pvargSrc)
903 TRACE("returning 0x%08lx, %p->(%s%s)\n", hres, pvargDest,
904 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
908 /******************************************************************************
909 * VariantChangeType [OLEAUT32.12]
911 * Change the type of a variant.
914 * pvargDest [O] Destination for the converted variant
915 * pvargSrc [O] Source variant to change the type of
916 * wFlags [I] VARIANT_ flags from "oleauto.h"
917 * vt [I] Variant type to change pvargSrc into
920 * Success: S_OK. pvargDest contains the converted value.
921 * Failure: An HRESULT error code describing the failure.
924 * The LCID used for the conversion is LOCALE_USER_DEFAULT.
925 * See VariantChangeTypeEx.
927 HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
928 USHORT wFlags, VARTYPE vt)
930 return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
933 /******************************************************************************
934 * VariantChangeTypeEx [OLEAUT32.147]
936 * Change the type of a variant.
939 * pvargDest [O] Destination for the converted variant
940 * pvargSrc [O] Source variant to change the type of
941 * lcid [I] LCID for the conversion
942 * wFlags [I] VARIANT_ flags from "oleauto.h"
943 * vt [I] Variant type to change pvargSrc into
946 * Success: S_OK. pvargDest contains the converted value.
947 * Failure: An HRESULT error code describing the failure.
950 * pvargDest and pvargSrc can point to the same variant to perform an in-place
951 * conversion. If the conversion is successful, pvargSrc will be freed.
953 HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
954 LCID lcid, USHORT wFlags, VARTYPE vt)
958 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%04x,%s%s)\n", pvargDest,
959 debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
960 debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
961 debugstr_vt(vt), debugstr_vf(vt));
964 res = DISP_E_BADVARTYPE;
967 res = VARIANT_ValidateType(V_VT(pvargSrc));
971 res = VARIANT_ValidateType(vt);
975 VARIANTARG vTmp, vSrcDeref;
977 if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
978 res = DISP_E_TYPEMISMATCH;
981 V_VT(&vTmp) = VT_EMPTY;
982 V_VT(&vSrcDeref) = VT_EMPTY;
984 VariantClear(&vSrcDeref);
989 res = VariantCopyInd(&vSrcDeref, pvargSrc);
992 if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
993 res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
995 res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
997 if (SUCCEEDED(res)) {
999 VariantCopy(pvargDest, &vTmp);
1001 VariantClear(&vTmp);
1002 VariantClear(&vSrcDeref);
1009 TRACE("returning 0x%08lx, %p->(%s%s)\n", res, pvargDest,
1010 debugstr_VT(pvargDest), debugstr_VF(pvargDest));
1014 /* Date Conversions */
1016 #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1018 /* Convert a VT_DATE value to a Julian Date */
1019 static inline int VARIANT_JulianFromDate(int dateIn)
1021 int julianDays = dateIn;
1023 julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1024 julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1028 /* Convert a Julian Date to a VT_DATE value */
1029 static inline int VARIANT_DateFromJulian(int dateIn)
1031 int julianDays = dateIn;
1033 julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1034 julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1038 /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
1039 static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1045 l -= (n * 146097 + 3) / 4;
1046 i = (4000 * (l + 1)) / 1461001;
1047 l += 31 - (i * 1461) / 4;
1048 j = (l * 80) / 2447;
1049 *day = l - (j * 2447) / 80;
1051 *month = (j + 2) - (12 * l);
1052 *year = 100 * (n - 49) + i + l;
1055 /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
1056 static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1058 int m12 = (month - 14) / 12;
1060 return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1061 (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1064 /* Macros for accessing DOS format date/time fields */
1065 #define DOS_YEAR(x) (1980 + (x >> 9))
1066 #define DOS_MONTH(x) ((x >> 5) & 0xf)
1067 #define DOS_DAY(x) (x & 0x1f)
1068 #define DOS_HOUR(x) (x >> 11)
1069 #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1070 #define DOS_SECOND(x) ((x & 0x1f) << 1)
1071 /* Create a DOS format date/time */
1072 #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1073 #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1075 /* Roll a date forwards or backwards to correct it */
1076 static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1078 static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1080 TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1081 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1083 /* Years < 100 are treated as 1900 + year */
1084 if (lpUd->st.wYear < 100)
1085 lpUd->st.wYear += 1900;
1087 if (!lpUd->st.wMonth)
1089 /* Roll back to December of the previous year */
1090 lpUd->st.wMonth = 12;
1093 else while (lpUd->st.wMonth > 12)
1095 /* Roll forward the correct number of months */
1097 lpUd->st.wMonth -= 12;
1100 if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
1101 lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
1102 return E_INVALIDARG; /* Invalid values */
1106 /* Roll back the date one day */
1107 if (lpUd->st.wMonth == 1)
1109 /* Roll back to December 31 of the previous year */
1111 lpUd->st.wMonth = 12;
1116 lpUd->st.wMonth--; /* Previous month */
1117 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1118 lpUd->st.wDay = 29; /* Februaury has 29 days on leap years */
1120 lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
1123 else if (lpUd->st.wDay > 28)
1125 int rollForward = 0;
1127 /* Possibly need to roll the date forward */
1128 if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
1129 rollForward = lpUd->st.wDay - 29; /* Februaury has 29 days on leap years */
1131 rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];
1133 if (rollForward > 0)
1135 lpUd->st.wDay = rollForward;
1137 if (lpUd->st.wMonth > 12)
1139 lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
1144 TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1145 lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1149 /**********************************************************************
1150 * DosDateTimeToVariantTime [OLEAUT32.14]
1152 * Convert a Dos format date and time into variant VT_DATE format.
1155 * wDosDate [I] Dos format date
1156 * wDosTime [I] Dos format time
1157 * pDateOut [O] Destination for VT_DATE format
1160 * Success: TRUE. pDateOut contains the converted time.
1161 * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1164 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1165 * - Dos format times are accurate to only 2 second precision.
1166 * - The format of a Dos Date is:
1167 *| Bits Values Meaning
1168 *| ---- ------ -------
1169 *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1170 *| the days in the month rolls forward the extra days.
1171 *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1172 *| year. 13-15 are invalid.
1173 *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1174 * - The format of a Dos Time is:
1175 *| Bits Values Meaning
1176 *| ---- ------ -------
1177 *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1178 *| 5-10 0-59 Minutes. 60-63 are invalid.
1179 *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1181 INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1186 TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1187 wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1188 wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1191 ud.st.wYear = DOS_YEAR(wDosDate);
1192 ud.st.wMonth = DOS_MONTH(wDosDate);
1193 if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1195 ud.st.wDay = DOS_DAY(wDosDate);
1196 ud.st.wHour = DOS_HOUR(wDosTime);
1197 ud.st.wMinute = DOS_MINUTE(wDosTime);
1198 ud.st.wSecond = DOS_SECOND(wDosTime);
1199 ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1201 return !VarDateFromUdate(&ud, 0, pDateOut);
1204 /**********************************************************************
1205 * VariantTimeToDosDateTime [OLEAUT32.13]
1207 * Convert a variant format date into a Dos format date and time.
1209 * dateIn [I] VT_DATE time format
1210 * pwDosDate [O] Destination for Dos format date
1211 * pwDosTime [O] Destination for Dos format time
1214 * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1215 * Failure: FALSE, if dateIn cannot be represented in Dos format.
1218 * See DosDateTimeToVariantTime() for Dos format details and bugs.
1220 INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1224 TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1226 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1229 if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1232 *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1233 *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1235 TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1236 *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1237 *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1241 /***********************************************************************
1242 * SystemTimeToVariantTime [OLEAUT32.184]
1244 * Convert a System format date and time into variant VT_DATE format.
1247 * lpSt [I] System format date and time
1248 * pDateOut [O] Destination for VT_DATE format date
1251 * Success: TRUE. *pDateOut contains the converted value.
1252 * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1254 INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1258 TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1259 lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1261 if (lpSt->wMonth > 12)
1264 memcpy(&ud.st, lpSt, sizeof(ud.st));
1265 return !VarDateFromUdate(&ud, 0, pDateOut);
1268 /***********************************************************************
1269 * VariantTimeToSystemTime [OLEAUT32.185]
1271 * Convert a variant VT_DATE into a System format date and time.
1274 * datein [I] Variant VT_DATE format date
1275 * lpSt [O] Destination for System format date and time
1278 * Success: TRUE. *lpSt contains the converted value.
1279 * Failure: FALSE, if dateIn is too large or small.
1281 INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1285 TRACE("(%g,%p)\n", dateIn, lpSt);
1287 if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1290 memcpy(lpSt, &ud.st, sizeof(ud.st));
1294 /***********************************************************************
1295 * VarDateFromUdateEx [OLEAUT32.319]
1297 * Convert an unpacked format date and time to a variant VT_DATE.
1300 * pUdateIn [I] Unpacked format date and time to convert
1301 * lcid [I] Locale identifier for the conversion
1302 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1303 * pDateOut [O] Destination for variant VT_DATE.
1306 * Success: S_OK. *pDateOut contains the converted value.
1307 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1309 HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1314 TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08lx,0x%08lx,%p)\n", pUdateIn,
1315 pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1316 pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1317 pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1318 pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1320 if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1321 FIXME("lcid possibly not handled, treating as en-us\n");
1323 memcpy(&ud, pUdateIn, sizeof(ud));
1325 if (dwFlags & VAR_VALIDDATE)
1326 WARN("Ignoring VAR_VALIDDATE\n");
1328 if (FAILED(VARIANT_RollUdate(&ud)))
1329 return E_INVALIDARG;
1332 dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1335 dateVal += ud.st.wHour / 24.0;
1336 dateVal += ud.st.wMinute / 1440.0;
1337 dateVal += ud.st.wSecond / 86400.0;
1338 dateVal += ud.st.wMilliseconds / 86400000.0;
1340 TRACE("Returning %g\n", dateVal);
1341 *pDateOut = dateVal;
1345 /***********************************************************************
1346 * VarDateFromUdate [OLEAUT32.330]
1348 * Convert an unpacked format date and time to a variant VT_DATE.
1351 * pUdateIn [I] Unpacked format date and time to convert
1352 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1353 * pDateOut [O] Destination for variant VT_DATE.
1356 * Success: S_OK. *pDateOut contains the converted value.
1357 * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1360 * This function uses the United States English locale for the conversion. Use
1361 * VarDateFromUdateEx() for alternate locales.
1363 HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1365 LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1367 return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1370 /***********************************************************************
1371 * VarUdateFromDate [OLEAUT32.331]
1373 * Convert a variant VT_DATE into an unpacked format date and time.
1376 * datein [I] Variant VT_DATE format date
1377 * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1378 * lpUdate [O] Destination for unpacked format date and time
1381 * Success: S_OK. *lpUdate contains the converted value.
1382 * Failure: E_INVALIDARG, if dateIn is too large or small.
1384 HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1386 /* Cumulative totals of days per month */
1387 static const USHORT cumulativeDays[] =
1389 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1391 double datePart, timePart;
1394 TRACE("(%g,0x%08lx,%p)\n", dateIn, dwFlags, lpUdate);
1396 if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1397 return E_INVALIDARG;
1399 datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1400 /* Compensate for int truncation (always downwards) */
1401 timePart = dateIn - datePart + 0.00000000001;
1402 if (timePart >= 1.0)
1403 timePart -= 0.00000000001;
1406 julianDays = VARIANT_JulianFromDate(dateIn);
1407 VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1410 datePart = (datePart + 1.5) / 7.0;
1411 lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1412 if (lpUdate->st.wDayOfWeek == 0)
1413 lpUdate->st.wDayOfWeek = 5;
1414 else if (lpUdate->st.wDayOfWeek == 1)
1415 lpUdate->st.wDayOfWeek = 6;
1417 lpUdate->st.wDayOfWeek -= 2;
1419 if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1420 lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1422 lpUdate->wDayOfYear = 0;
1424 lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1425 lpUdate->wDayOfYear += lpUdate->st.wDay;
1429 lpUdate->st.wHour = timePart;
1430 timePart -= lpUdate->st.wHour;
1432 lpUdate->st.wMinute = timePart;
1433 timePart -= lpUdate->st.wMinute;
1435 lpUdate->st.wSecond = timePart;
1436 timePart -= lpUdate->st.wSecond;
1437 lpUdate->st.wMilliseconds = 0;
1440 /* Round the milliseconds, adjusting the time/date forward if needed */
1441 if (lpUdate->st.wSecond < 59)
1442 lpUdate->st.wSecond++;
1445 lpUdate->st.wSecond = 0;
1446 if (lpUdate->st.wMinute < 59)
1447 lpUdate->st.wMinute++;
1450 lpUdate->st.wMinute = 0;
1451 if (lpUdate->st.wHour < 23)
1452 lpUdate->st.wHour++;
1455 lpUdate->st.wHour = 0;
1456 /* Roll over a whole day */
1457 if (++lpUdate->st.wDay > 28)
1458 VARIANT_RollUdate(lpUdate);
1466 #define GET_NUMBER_TEXT(fld,name) \
1468 if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1469 WARN("buffer too small for " #fld "\n"); \
1471 if (buff[0]) lpChars->name = buff[0]; \
1472 TRACE("lcid 0x%lx, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1474 /* Get the valid number characters for an lcid */
1475 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1477 static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1478 LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1481 memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1482 GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1483 GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1484 GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1485 GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeperator);
1486 GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1487 GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeperator);
1489 /* Local currency symbols are often 2 characters */
1490 lpChars->cCurrencyLocal2 = '\0';
1491 switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1493 case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1494 case 2: lpChars->cCurrencyLocal = buff[0];
1496 default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1498 TRACE("lcid 0x%lx, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1499 lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1502 /* Number Parsing States */
1503 #define B_PROCESSING_EXPONENT 0x1
1504 #define B_NEGATIVE_EXPONENT 0x2
1505 #define B_EXPONENT_START 0x4
1506 #define B_INEXACT_ZEROS 0x8
1507 #define B_LEADING_ZERO 0x10
1508 #define B_PROCESSING_HEX 0x20
1509 #define B_PROCESSING_OCT 0x40
1511 /**********************************************************************
1512 * VarParseNumFromStr [OLEAUT32.46]
1514 * Parse a string containing a number into a NUMPARSE structure.
1517 * lpszStr [I] String to parse number from
1518 * lcid [I] Locale Id for the conversion
1519 * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1520 * pNumprs [I/O] Destination for parsed number
1521 * rgbDig [O] Destination for digits read in
1524 * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1526 * Failure: E_INVALIDARG, if any parameter is invalid.
1527 * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1529 * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1532 * pNumprs must have the following fields set:
1533 * cDig: Set to the size of rgbDig.
1534 * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1538 * - I am unsure if this function should parse non-arabic (e.g. Thai)
1539 * numerals, so this has not been implemented.
1541 HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1542 NUMPARSE *pNumprs, BYTE *rgbDig)
1544 VARIANT_NUMBER_CHARS chars;
1546 DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
1547 int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
1550 TRACE("(%s,%ld,0x%08lx,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1552 if (!pNumprs || !rgbDig)
1553 return E_INVALIDARG;
1555 if (pNumprs->cDig < iMaxDigits)
1556 iMaxDigits = pNumprs->cDig;
1559 pNumprs->dwOutFlags = 0;
1560 pNumprs->cchUsed = 0;
1561 pNumprs->nBaseShift = 0;
1562 pNumprs->nPwr10 = 0;
1565 return DISP_E_TYPEMISMATCH;
1567 VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1569 /* First consume all the leading symbols and space from the string */
1572 if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
1574 pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1579 } while (isspaceW(*lpszStr));
1581 else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1582 *lpszStr == chars.cPositiveSymbol &&
1583 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1585 pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1589 else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1590 *lpszStr == chars.cNegativeSymbol &&
1591 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1593 pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1597 else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1598 !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1599 *lpszStr == chars.cCurrencyLocal &&
1600 (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1602 pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1605 /* Only accept currency characters */
1606 chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1607 chars.cDigitSeperator = chars.cCurrencyDigitSeperator;
1609 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1610 !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1612 pNumprs->dwOutFlags |= NUMPRS_PARENS;
1620 if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1622 /* Only accept non-currency characters */
1623 chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1624 chars.cCurrencyDigitSeperator = chars.cDigitSeperator;
1627 if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1628 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1630 dwState |= B_PROCESSING_HEX;
1631 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1635 else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1636 pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1638 dwState |= B_PROCESSING_OCT;
1639 pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1644 /* Strip Leading zeros */
1645 while (*lpszStr == '0')
1647 dwState |= B_LEADING_ZERO;
1654 if (isdigitW(*lpszStr))
1656 if (dwState & B_PROCESSING_EXPONENT)
1658 int exponentSize = 0;
1659 if (dwState & B_EXPONENT_START)
1661 if (!isdigitW(*lpszStr))
1662 break; /* No exponent digits - invalid */
1663 while (*lpszStr == '0')
1665 /* Skip leading zero's in the exponent */
1671 while (isdigitW(*lpszStr))
1674 exponentSize += *lpszStr - '0';
1678 if (dwState & B_NEGATIVE_EXPONENT)
1679 exponentSize = -exponentSize;
1680 /* Add the exponent into the powers of 10 */
1681 pNumprs->nPwr10 += exponentSize;
1682 dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1683 lpszStr--; /* back up to allow processing of next char */
1687 if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1688 && !(dwState & B_PROCESSING_OCT))
1690 pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1692 if (*lpszStr != '0')
1693 dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1695 /* This digit can't be represented, but count it in nPwr10 */
1696 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1703 if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
1704 return DISP_E_TYPEMISMATCH;
1707 if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1708 pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1710 rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1716 else if (*lpszStr == chars.cDigitSeperator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1718 pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1721 else if (*lpszStr == chars.cDecimalPoint &&
1722 pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1723 !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1725 pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1728 /* If we have no digits so far, skip leading zeros */
1731 while (lpszStr[1] == '0')
1733 dwState |= B_LEADING_ZERO;
1740 else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1741 pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1742 !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1744 dwState |= B_PROCESSING_EXPONENT;
1745 pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1748 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1750 cchUsed++; /* Ignore positive exponent */
1752 else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1754 dwState |= B_NEGATIVE_EXPONENT;
1757 else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1758 (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1759 dwState & B_PROCESSING_HEX)
1761 if (pNumprs->cDig >= iMaxDigits)
1763 return DISP_E_OVERFLOW;
1767 if (*lpszStr >= 'a')
1768 rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1770 rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1776 break; /* Stop at an unrecognised character */
1781 if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1783 /* Ensure a 0 on its own gets stored */
1788 if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1790 pNumprs->cchUsed = cchUsed;
1791 return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1794 if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1796 if (dwState & B_INEXACT_ZEROS)
1797 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1798 } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1800 /* copy all of the digits into the output digit buffer */
1801 /* this is exactly what windows does although it also returns */
1802 /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1803 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1805 if (dwState & B_PROCESSING_HEX) {
1806 /* hex numbers have always the same format */
1808 pNumprs->nBaseShift=4;
1810 if (dwState & B_PROCESSING_OCT) {
1811 /* oct numbers have always the same format */
1813 pNumprs->nBaseShift=3;
1815 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1824 /* Remove trailing zeros from the last (whole number or decimal) part */
1825 while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1832 if (pNumprs->cDig <= iMaxDigits)
1833 pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1835 pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1837 /* Copy the digits we processed into rgbDig */
1838 memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1840 /* Consume any trailing symbols and space */
1843 if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
1845 pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1850 } while (isspaceW(*lpszStr));
1852 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1853 !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1854 *lpszStr == chars.cPositiveSymbol)
1856 pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1860 else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1861 !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1862 *lpszStr == chars.cNegativeSymbol)
1864 pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1868 else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1869 pNumprs->dwOutFlags & NUMPRS_PARENS)
1873 pNumprs->dwOutFlags |= NUMPRS_NEG;
1879 if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1881 pNumprs->cchUsed = cchUsed;
1882 return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1885 if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1886 return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1889 return DISP_E_TYPEMISMATCH; /* No Number found */
1891 pNumprs->cchUsed = cchUsed;
1895 /* VTBIT flags indicating an integer value */
1896 #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1897 /* VTBIT flags indicating a real number value */
1898 #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1900 /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1901 #define FITS_AS_I1(x) ((x) >> 8 == 0)
1902 #define FITS_AS_I2(x) ((x) >> 16 == 0)
1903 #define FITS_AS_I4(x) ((x) >> 32 == 0)
1905 /**********************************************************************
1906 * VarNumFromParseNum [OLEAUT32.47]
1908 * Convert a NUMPARSE structure into a numeric Variant type.
1911 * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1912 * rgbDig [I] Source for the numbers digits
1913 * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1914 * pVarDst [O] Destination for the converted Variant value.
1917 * Success: S_OK. pVarDst contains the converted value.
1918 * Failure: E_INVALIDARG, if any parameter is invalid.
1919 * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1922 * - The smallest favoured type present in dwVtBits that can represent the
1923 * number in pNumprs without losing precision is used.
1924 * - Signed types are preferrred over unsigned types of the same size.
1925 * - Preferred types in order are: integer, float, double, currency then decimal.
1926 * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1927 * for details of the rounding method.
1928 * - pVarDst is not cleared before the result is stored in it.
1930 HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1931 ULONG dwVtBits, VARIANT *pVarDst)
1933 /* Scale factors and limits for double arithmetic */
1934 static const double dblMultipliers[11] = {
1935 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
1936 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
1938 static const double dblMinimums[11] = {
1939 R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
1940 R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
1941 R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
1943 static const double dblMaximums[11] = {
1944 R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
1945 R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
1946 R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
1949 int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
1951 TRACE("(%p,%p,0x%lx,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1953 if (pNumprs->nBaseShift)
1955 /* nBaseShift indicates a hex or octal number */
1960 /* Convert the hex or octal number string into a UI64 */
1961 for (i = 0; i < pNumprs->cDig; i++)
1963 if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
1965 TRACE("Overflow multiplying digits\n");
1966 return DISP_E_OVERFLOW;
1968 ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
1971 /* also make a negative representation */
1974 /* Try signed and unsigned types in size order */
1975 if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
1977 V_VT(pVarDst) = VT_I1;
1978 V_I1(pVarDst) = ul64;
1981 else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
1983 V_VT(pVarDst) = VT_UI1;
1984 V_UI1(pVarDst) = ul64;
1987 else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
1989 V_VT(pVarDst) = VT_I2;
1990 V_I2(pVarDst) = ul64;
1993 else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
1995 V_VT(pVarDst) = VT_UI2;
1996 V_UI2(pVarDst) = ul64;
1999 else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2001 V_VT(pVarDst) = VT_I4;
2002 V_I4(pVarDst) = ul64;
2005 else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2007 V_VT(pVarDst) = VT_UI4;
2008 V_UI4(pVarDst) = ul64;
2011 else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2013 V_VT(pVarDst) = VT_I8;
2014 V_I8(pVarDst) = ul64;
2017 else if (dwVtBits & VTBIT_UI8)
2019 V_VT(pVarDst) = VT_UI8;
2020 V_UI8(pVarDst) = ul64;
2023 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2025 V_VT(pVarDst) = VT_DECIMAL;
2026 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2027 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2028 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2031 else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2033 V_VT(pVarDst) = VT_R4;
2035 V_R4(pVarDst) = ul64;
2037 V_R4(pVarDst) = l64;
2040 else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2042 V_VT(pVarDst) = VT_R8;
2044 V_R8(pVarDst) = ul64;
2046 V_R8(pVarDst) = l64;
2050 TRACE("Overflow: possible return types: 0x%lx, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2051 return DISP_E_OVERFLOW;
2054 /* Count the number of relevant fractional and whole digits stored,
2055 * And compute the divisor/multiplier to scale the number by.
2057 if (pNumprs->nPwr10 < 0)
2059 if (-pNumprs->nPwr10 >= pNumprs->cDig)
2061 /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2062 wholeNumberDigits = 0;
2063 fractionalDigits = pNumprs->cDig;
2064 divisor10 = -pNumprs->nPwr10;
2068 /* An exactly represented real number e.g. 1.024 */
2069 wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2070 fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2071 divisor10 = pNumprs->cDig - wholeNumberDigits;
2074 else if (pNumprs->nPwr10 == 0)
2076 /* An exactly represented whole number e.g. 1024 */
2077 wholeNumberDigits = pNumprs->cDig;
2078 fractionalDigits = 0;
2080 else /* pNumprs->nPwr10 > 0 */
2082 /* A whole number followed by nPwr10 0's e.g. 102400 */
2083 wholeNumberDigits = pNumprs->cDig;
2084 fractionalDigits = 0;
2085 multiplier10 = pNumprs->nPwr10;
2088 TRACE("cDig %d; nPwr10 %d, whole %d, frac %d ", pNumprs->cDig,
2089 pNumprs->nPwr10, wholeNumberDigits, fractionalDigits);
2090 TRACE("mult %d; div %d\n", multiplier10, divisor10);
2092 if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2093 (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
2095 /* We have one or more integer output choices, and either:
2096 * 1) An integer input value, or
2097 * 2) A real number input value but no floating output choices.
2098 * Alternately, we have a DECIMAL output available and an integer input.
2100 * So, place the integer value into pVarDst, using the smallest type
2101 * possible and preferring signed over unsigned types.
2103 BOOL bOverflow = FALSE, bNegative;
2107 /* Convert the integer part of the number into a UI8 */
2108 for (i = 0; i < wholeNumberDigits; i++)
2110 if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
2112 TRACE("Overflow multiplying digits\n");
2116 ul64 = ul64 * 10 + rgbDig[i];
2119 /* Account for the scale of the number */
2120 if (!bOverflow && multiplier10)
2122 for (i = 0; i < multiplier10; i++)
2124 if (ul64 > (UI8_MAX / 10))
2126 TRACE("Overflow scaling number\n");
2134 /* If we have any fractional digits, round the value.
2135 * Note we don't have to do this if divisor10 is < 1,
2136 * because this means the fractional part must be < 0.5
2138 if (!bOverflow && fractionalDigits && divisor10 > 0)
2140 const BYTE* fracDig = rgbDig + wholeNumberDigits;
2141 BOOL bAdjust = FALSE;
2143 TRACE("first decimal value is %d\n", *fracDig);
2146 bAdjust = TRUE; /* > 0.5 */
2147 else if (*fracDig == 5)
2149 for (i = 1; i < fractionalDigits; i++)
2153 bAdjust = TRUE; /* > 0.5 */
2157 /* If exactly 0.5, round only odd values */
2158 if (i == fractionalDigits && (ul64 & 1))
2164 if (ul64 == UI8_MAX)
2166 TRACE("Overflow after rounding\n");
2173 /* Zero is not a negative number */
2174 bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;
2176 TRACE("Integer value is %lld, bNeg %d\n", ul64, bNegative);
2178 /* For negative integers, try the signed types in size order */
2179 if (!bOverflow && bNegative)
2181 if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2183 if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2185 V_VT(pVarDst) = VT_I1;
2186 V_I1(pVarDst) = -ul64;
2189 else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2191 V_VT(pVarDst) = VT_I2;
2192 V_I2(pVarDst) = -ul64;
2195 else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2197 V_VT(pVarDst) = VT_I4;
2198 V_I4(pVarDst) = -ul64;
2201 else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2203 V_VT(pVarDst) = VT_I8;
2204 V_I8(pVarDst) = -ul64;
2207 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2209 /* Decimal is only output choice left - fast path */
2210 V_VT(pVarDst) = VT_DECIMAL;
2211 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2212 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2213 DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2218 else if (!bOverflow)
2220 /* For positive integers, try signed then unsigned types in size order */
2221 if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2223 V_VT(pVarDst) = VT_I1;
2224 V_I1(pVarDst) = ul64;
2227 else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2229 V_VT(pVarDst) = VT_UI1;
2230 V_UI1(pVarDst) = ul64;
2233 else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2235 V_VT(pVarDst) = VT_I2;
2236 V_I2(pVarDst) = ul64;
2239 else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2241 V_VT(pVarDst) = VT_UI2;
2242 V_UI2(pVarDst) = ul64;
2245 else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2247 V_VT(pVarDst) = VT_I4;
2248 V_I4(pVarDst) = ul64;
2251 else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2253 V_VT(pVarDst) = VT_UI4;
2254 V_UI4(pVarDst) = ul64;
2257 else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2259 V_VT(pVarDst) = VT_I8;
2260 V_I8(pVarDst) = ul64;
2263 else if (dwVtBits & VTBIT_UI8)
2265 V_VT(pVarDst) = VT_UI8;
2266 V_UI8(pVarDst) = ul64;
2269 else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
2271 /* Decimal is only output choice left - fast path */
2272 V_VT(pVarDst) = VT_DECIMAL;
2273 DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2274 DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2275 DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2281 if (dwVtBits & REAL_VTBITS)
2283 /* Try to put the number into a float or real */
2284 BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2288 /* Convert the number into a double */
2289 for (i = 0; i < pNumprs->cDig; i++)
2290 whole = whole * 10.0 + rgbDig[i];
2292 TRACE("Whole double value is %16.16g\n", whole);
2294 /* Account for the scale */
2295 while (multiplier10 > 10)
2297 if (whole > dblMaximums[10])
2299 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2303 whole = whole * dblMultipliers[10];
2308 if (whole > dblMaximums[multiplier10])
2310 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2314 whole = whole * dblMultipliers[multiplier10];
2317 TRACE("Scaled double value is %16.16g\n", whole);
2319 while (divisor10 > 10)
2321 if (whole < dblMinimums[10] && whole != 0)
2323 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2327 whole = whole / dblMultipliers[10];
2332 if (whole < dblMinimums[divisor10] && whole != 0)
2334 dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
2338 whole = whole / dblMultipliers[divisor10];
2341 TRACE("Final double value is %16.16g\n", whole);
2343 if (dwVtBits & VTBIT_R4 &&
2344 ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2346 TRACE("Set R4 to final value\n");
2347 V_VT(pVarDst) = VT_R4; /* Fits into a float */
2348 V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2352 if (dwVtBits & VTBIT_R8)
2354 TRACE("Set R8 to final value\n");
2355 V_VT(pVarDst) = VT_R8; /* Fits into a double */
2356 V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2360 if (dwVtBits & VTBIT_CY)
2362 if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2364 V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2365 TRACE("Set CY to final value\n");
2368 TRACE("Value Overflows CY\n");
2372 if (dwVtBits & VTBIT_DECIMAL)
2377 DECIMAL* pDec = &V_DECIMAL(pVarDst);
2379 DECIMAL_SETZERO(*pDec);
2382 if (pNumprs->dwOutFlags & NUMPRS_NEG)
2383 DEC_SIGN(pDec) = DECIMAL_NEG;
2385 DEC_SIGN(pDec) = DECIMAL_POS;
2387 /* Factor the significant digits */
2388 for (i = 0; i < pNumprs->cDig; i++)
2390 tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2391 carry = (ULONG)(tmp >> 32);
2392 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2393 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2394 carry = (ULONG)(tmp >> 32);
2395 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2396 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2397 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2399 if (tmp >> 32 & UI4_MAX)
2401 VarNumFromParseNum_DecOverflow:
2402 TRACE("Overflow\n");
2403 DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2404 return DISP_E_OVERFLOW;
2408 /* Account for the scale of the number */
2409 while (multiplier10 > 0)
2411 tmp = (ULONG64)DEC_LO32(pDec) * 10;
2412 carry = (ULONG)(tmp >> 32);
2413 DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2414 tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2415 carry = (ULONG)(tmp >> 32);
2416 DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2417 tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2418 DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2420 if (tmp >> 32 & UI4_MAX)
2421 goto VarNumFromParseNum_DecOverflow;
2424 DEC_SCALE(pDec) = divisor10;
2426 V_VT(pVarDst) = VT_DECIMAL;
2429 return DISP_E_OVERFLOW; /* No more output choices */
2432 /**********************************************************************
2433 * VarCat [OLEAUT32.318]
2435 * Concatenates one variant onto another.
2438 * left [I] First variant
2439 * right [I] Second variant
2440 * result [O] Result variant
2444 * Failure: An HRESULT error code indicating the error.
2446 HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2448 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2449 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);
2451 /* Should we VariantClear out? */
2452 /* Can we handle array, vector, by ref etc. */
2453 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL &&
2454 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2456 V_VT(out) = VT_NULL;
2460 if (V_VT(left) == VT_BSTR && V_VT(right) == VT_BSTR)
2462 V_VT(out) = VT_BSTR;
2463 VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
2466 if (V_VT(left) == VT_BSTR) {
2470 V_VT(out) = VT_BSTR;
2471 VariantInit(&bstrvar);
2472 hres = VariantChangeTypeEx(&bstrvar,right,0,0,VT_BSTR);
2474 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2477 VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar), &V_BSTR(out));
2480 if (V_VT(right) == VT_BSTR) {
2484 V_VT(out) = VT_BSTR;
2485 VariantInit(&bstrvar);
2486 hres = VariantChangeTypeEx(&bstrvar,left,0,0,VT_BSTR);
2488 FIXME("Failed to convert right side from vt %d to VT_BSTR?\n",V_VT(right));
2491 VarBstrCat (V_BSTR(&bstrvar), V_BSTR(right), &V_BSTR(out));
2494 FIXME ("types %d / %d not supported\n",V_VT(left)&VT_TYPEMASK, V_VT(right)&VT_TYPEMASK);
2498 /**********************************************************************
2499 * VarCmp [OLEAUT32.176]
2502 * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS
2503 * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2506 HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2516 TRACE("(%p->(%s%s),%p->(%s%s),0x%08lx,0x%08lx)\n", left, debugstr_VT(left),
2517 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);
2519 VariantInit(&lv);VariantInit(&rv);
2520 V_VT(right) &= ~0x8000; /* hack since we sometime get this flag. */
2521 V_VT(left) &= ~0x8000; /* hack since we sometime get this flag. */
2523 /* If either are null, then return VARCMP_NULL */
2524 if ((V_VT(left)&VT_TYPEMASK) == VT_NULL ||
2525 (V_VT(right)&VT_TYPEMASK) == VT_NULL)
2528 /* Strings - use VarBstrCmp */
2529 if ((V_VT(left)&VT_TYPEMASK) == VT_BSTR &&
2530 (V_VT(right)&VT_TYPEMASK) == VT_BSTR) {
2531 return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2534 xmask = (1<<(V_VT(left)&VT_TYPEMASK))|(1<<(V_VT(right)&VT_TYPEMASK));
2535 if (xmask & VTBIT_R8) {
2536 rc = VariantChangeType(&lv,left,0,VT_R8);
2537 if (FAILED(rc)) return rc;
2538 rc = VariantChangeType(&rv,right,0,VT_R8);
2539 if (FAILED(rc)) return rc;
2541 if (V_R8(&lv) == V_R8(&rv)) return VARCMP_EQ;
2542 if (V_R8(&lv) < V_R8(&rv)) return VARCMP_LT;
2543 if (V_R8(&lv) > V_R8(&rv)) return VARCMP_GT;
2544 return E_FAIL; /* can't get here */
2546 if (xmask & VTBIT_R4) {
2547 rc = VariantChangeType(&lv,left,0,VT_R4);
2548 if (FAILED(rc)) return rc;
2549 rc = VariantChangeType(&rv,right,0,VT_R4);
2550 if (FAILED(rc)) return rc;
2552 if (V_R4(&lv) == V_R4(&rv)) return VARCMP_EQ;
2553 if (V_R4(&lv) < V_R4(&rv)) return VARCMP_LT;
2554 if (V_R4(&lv) > V_R4(&rv)) return VARCMP_GT;
2555 return E_FAIL; /* can't get here */
2558 /* Integers - Ideally like to use VarDecCmp, but no Dec support yet
2559 Use LONGLONG to maximize ranges */
2561 switch (V_VT(left)&VT_TYPEMASK) {
2562 case VT_I1 : lVal = V_I1(left); break;
2563 case VT_I2 : lVal = V_I2(left); break;
2565 case VT_INT : lVal = V_I4(left); break;
2566 case VT_UI1 : lVal = V_UI1(left); break;
2567 case VT_UI2 : lVal = V_UI2(left); break;
2569 case VT_UINT : lVal = V_UI4(left); break;
2570 case VT_BOOL : lVal = V_BOOL(left); break;
2571 case VT_EMPTY : lVal = 0; break;
2572 default: lOk = FALSE;
2576 switch (V_VT(right)&VT_TYPEMASK) {
2577 case VT_I1 : rVal = V_I1(right); break;
2578 case VT_I2 : rVal = V_I2(right); break;
2580 case VT_INT : rVal = V_I4(right); break;
2581 case VT_UI1 : rVal = V_UI1(right); break;
2582 case VT_UI2 : rVal = V_UI2(right); break;
2584 case VT_UINT : rVal = V_UI4(right); break;
2585 case VT_BOOL : rVal = V_BOOL(right); break;
2586 case VT_EMPTY : rVal = 0; break;
2587 default: rOk = FALSE;
2593 } else if (lVal > rVal) {
2601 if ((V_VT(left)&VT_TYPEMASK) == VT_DATE &&
2602 (V_VT(right)&VT_TYPEMASK) == VT_DATE) {
2604 if (floor(V_DATE(left)) == floor(V_DATE(right))) {
2605 /* Due to floating point rounding errors, calculate varDate in whole numbers) */
2606 double wholePart = 0.0;
2610 /* Get the fraction * 24*60*60 to make it into whole seconds */
2611 wholePart = (double) floor( V_DATE(left) );
2612 if (wholePart == 0) wholePart = 1;
2613 leftR = floor(fmod( V_DATE(left), wholePart ) * (24*60*60));
2615 wholePart = (double) floor( V_DATE(right) );
2616 if (wholePart == 0) wholePart = 1;
2617 rightR = floor(fmod( V_DATE(right), wholePart ) * (24*60*60));
2619 if (leftR < rightR) {
2621 } else if (leftR > rightR) {
2627 } else if (V_DATE(left) < V_DATE(right)) {
2629 } else if (V_DATE(left) > V_DATE(right)) {
2633 FIXME("VarCmp partial implementation, doesn't support vt 0x%x / 0x%x\n",V_VT(left), V_VT(right));
2637 /**********************************************************************
2638 * VarAnd [OLEAUT32.142]
2640 * Computes the logical AND of two variants.
2643 * left [I] First variant
2644 * right [I] Second variant
2645 * result [O] Result variant
2649 * Failure: An HRESULT error code indicating the error.
2651 HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2653 HRESULT rc = E_FAIL;
2655 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2656 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2658 if ((V_VT(left)&VT_TYPEMASK) == VT_BOOL &&
2659 (V_VT(right)&VT_TYPEMASK) == VT_BOOL) {
2661 V_VT(result) = VT_BOOL;
2662 if (V_BOOL(left) && V_BOOL(right)) {
2663 V_BOOL(result) = VARIANT_TRUE;
2665 V_BOOL(result) = VARIANT_FALSE;
2676 int resT = 0; /* Testing has shown I2 & I2 == I2, all else
2677 becomes I4, even unsigned ints (incl. UI2) */
2680 switch (V_VT(left)&VT_TYPEMASK) {
2681 case VT_I1 : lVal = V_I1(left); resT=VT_I4; break;
2682 case VT_I2 : lVal = V_I2(left); resT=VT_I2; break;
2684 case VT_INT : lVal = V_I4(left); resT=VT_I4; break;
2685 case VT_UI1 : lVal = V_UI1(left); resT=VT_I4; break;
2686 case VT_UI2 : lVal = V_UI2(left); resT=VT_I4; break;
2688 case VT_UINT : lVal = V_UI4(left); resT=VT_I4; break;
2689 case VT_BOOL : rVal = V_BOOL(left); resT=VT_I4; break;
2690 default: lOk = FALSE;
2694 switch (V_VT(right)&VT_TYPEMASK) {
2695 case VT_I1 : rVal = V_I1(right); resT=VT_I4; break;
2696 case VT_I2 : rVal = V_I2(right); resT=max(VT_I2, resT); break;
2698 case VT_INT : rVal = V_I4(right); resT=VT_I4; break;
2699 case VT_UI1 : rVal = V_UI1(right); resT=VT_I4; break;
2700 case VT_UI2 : rVal = V_UI2(right); resT=VT_I4; break;
2702 case VT_UINT : rVal = V_UI4(right); resT=VT_I4; break;
2703 case VT_BOOL : rVal = V_BOOL(right); resT=VT_I4; break;
2704 default: rOk = FALSE;
2708 res = (lVal & rVal);
2709 V_VT(result) = resT;
2711 case VT_I2 : V_I2(result) = res; break;
2712 case VT_I4 : V_I4(result) = res; break;
2714 FIXME("Unexpected result variant type %x\n", resT);
2720 FIXME("VarAnd stub\n");
2724 TRACE("returning 0x%8lx (%s%s),%ld\n", rc, debugstr_VT(result),
2725 debugstr_VF(result), V_VT(result) == VT_I4 ? V_I4(result) : V_I2(result));
2729 /**********************************************************************
2730 * VarAdd [OLEAUT32.141]
2735 * left [I] First variant
2736 * right [I] Second variant
2737 * result [O] Result variant
2741 * Failure: An HRESULT error code indicating the error.
2744 * Native VarAdd up to and including WinXP dosn't like as input variants
2745 * I1, UI2, UI4, UI8, INT and UINT.
2747 * Native VarAdd dosn't check for NULL in/out pointers and crashes. We do the
2751 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
2754 HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2757 VARTYPE lvt, rvt, resvt, tvt;
2761 /* Variant priority for coercion. Sorted from lowest to highest.
2762 VT_ERROR shows an invalid input variant type. */
2763 enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
2764 vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
2766 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
2767 VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
2768 VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
2769 VT_NULL, VT_ERROR };
2771 /* Mapping for coercion from input variant to priority of result variant. */
2772 static VARTYPE coerce[] = {
2773 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
2774 vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
2775 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
2776 vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
2777 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
2778 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
2779 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
2780 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
2783 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2784 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
2790 lvt = V_VT(left)&VT_TYPEMASK;
2791 rvt = V_VT(right)&VT_TYPEMASK;
2793 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
2794 Same for any input variant type > VT_I8 */
2795 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
2796 lvt > VT_I8 || rvt > VT_I8) {
2797 hres = DISP_E_BADVARTYPE;
2801 /* Determine the variant type to coerce to. */
2802 if (coerce[lvt] > coerce[rvt]) {
2803 resvt = prio2vt[coerce[lvt]];
2804 tvt = prio2vt[coerce[rvt]];
2806 resvt = prio2vt[coerce[rvt]];
2807 tvt = prio2vt[coerce[lvt]];
2810 /* Special cases where the result variant type is defined by both
2811 input variants and not only that with the highest priority */
2812 if (resvt == VT_BSTR) {
2813 if (tvt == VT_EMPTY || tvt == VT_BSTR)
2818 if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
2821 /* For overflow detection use the biggest compatible type for the
2825 hres = DISP_E_BADVARTYPE;
2829 V_VT(result) = VT_NULL;
2832 FIXME("cannot handle variant type VT_DISPATCH\n");
2833 hres = DISP_E_TYPEMISMATCH;
2852 /* Now coerce the variants */
2853 hres = VariantChangeType(&lv, left, 0, tvt);
2856 hres = VariantChangeType(&rv, right, 0, tvt);
2863 V_VT(result) = resvt;
2866 hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
2867 &V_DECIMAL(result));
2870 hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
2873 /* We do not add those, we concatenate them. */
2874 hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
2877 /* Overflow detection */
2878 r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
2879 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
2880 V_VT(result) = VT_R8;
2881 V_R8(result) = r8res;
2884 V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
2887 /* FIXME: overflow detection */
2888 V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
2891 ERR("We shouldn't get here! tvt = %d!\n", tvt);
2895 if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
2896 /* Overflow! Change to the vartype with the next higher priority */
2897 resvt = prio2vt[coerce[resvt] + 1];
2898 hres = VariantChangeType(result, &tv, 0, resvt);
2901 hres = VariantCopy(result, &tv);
2905 V_VT(result) = VT_EMPTY;
2906 V_I4(result) = 0; /* No V_EMPTY */
2911 TRACE("returning 0x%8lx (variant type %s)\n", hres, debugstr_VT(result));
2915 /**********************************************************************
2916 * VarMul [OLEAUT32.156]
2918 * Multiply two variants.
2921 * left [I] First variant
2922 * right [I] Second variant
2923 * result [O] Result variant
2927 * Failure: An HRESULT error code indicating the error.
2930 * Native VarMul up to and including WinXP dosn't like as input variants
2931 * I1, UI2, UI4, UI8, INT and UINT. But it can multiply apples with oranges.
2933 * Native VarMul dosn't check for NULL in/out pointers and crashes. We do the
2937 * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
2940 HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2943 VARTYPE lvt, rvt, resvt, tvt;
2947 /* Variant priority for coercion. Sorted from lowest to highest.
2948 VT_ERROR shows an invalid input variant type. */
2949 enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
2950 vt_DECIMAL, vt_NULL, vt_ERROR };
2951 /* Mapping from priority to variant type. Keep in sync with coerceprio! */
2952 VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
2953 VT_DECIMAL, VT_NULL, VT_ERROR };
2955 /* Mapping for coercion from input variant to priority of result variant. */
2956 static VARTYPE coerce[] = {
2957 /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
2958 vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
2959 /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
2960 vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
2961 /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
2962 vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
2963 /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
2964 vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
2967 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
2968 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
2974 lvt = V_VT(left)&VT_TYPEMASK;
2975 rvt = V_VT(right)&VT_TYPEMASK;
2977 /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
2978 Same for any input variant type > VT_I8 */
2979 if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
2980 lvt > VT_I8 || rvt > VT_I8) {
2981 hres = DISP_E_BADVARTYPE;
2985 /* Determine the variant type to coerce to. */
2986 if (coerce[lvt] > coerce[rvt]) {
2987 resvt = prio2vt[coerce[lvt]];
2988 tvt = prio2vt[coerce[rvt]];
2990 resvt = prio2vt[coerce[rvt]];
2991 tvt = prio2vt[coerce[lvt]];
2994 /* Special cases where the result variant type is defined by both
2995 input variants and not only that with the highest priority */
2996 if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
2998 if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3001 /* For overflow detection use the biggest compatible type for the
3005 hres = DISP_E_BADVARTYPE;
3009 V_VT(result) = VT_NULL;
3024 /* Now coerce the variants */
3025 hres = VariantChangeType(&lv, left, 0, tvt);
3028 hres = VariantChangeType(&rv, right, 0, tvt);
3035 V_VT(result) = resvt;
3038 hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3039 &V_DECIMAL(result));
3042 hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3045 /* Overflow detection */
3046 r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3047 if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3048 V_VT(result) = VT_R8;
3049 V_R8(result) = r8res;
3052 V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3055 /* FIXME: overflow detection */
3056 V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3059 ERR("We shouldn't get here! tvt = %d!\n", tvt);
3063 while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3064 /* Overflow! Change to the vartype with the next higher priority */
3065 resvt = prio2vt[coerce[resvt] + 1];
3068 hres = VariantCopy(result, &tv);
3072 V_VT(result) = VT_EMPTY;
3073 V_I4(result) = 0; /* No V_EMPTY */
3078 TRACE("returning 0x%8lx (variant type %s)\n", hres, debugstr_VT(result));
3082 /**********************************************************************
3083 * VarDiv [OLEAUT32.143]
3085 * Divides one variant with another.
3088 * left [I] First variant
3089 * right [I] Second variant
3090 * result [O] Result variant
3094 * Failure: An HRESULT error code indicating the error.
3096 HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3098 HRESULT rc = E_FAIL;
3099 VARTYPE lvt,rvt,resvt;
3103 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3104 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3106 VariantInit(&lv);VariantInit(&rv);
3107 lvt = V_VT(left)&VT_TYPEMASK;
3108 rvt = V_VT(right)&VT_TYPEMASK;
3109 found = FALSE;resvt = VT_VOID;
3110 if (((1<<lvt) | (1<<rvt)) & (VTBIT_R4|VTBIT_R8)) {
3114 if (!found && (((1<<lvt) | (1<<rvt)) & (VTBIT_I1|VTBIT_I2|VTBIT_UI1|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|(1<<VT_INT)|(1<<VT_UINT)))) {
3119 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3122 rc = VariantChangeType(&lv, left, 0, resvt);
3124 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3127 rc = VariantChangeType(&rv, right, 0, resvt);
3129 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3134 if (V_R8(&rv) == 0) return DISP_E_DIVBYZERO;
3135 V_VT(result) = resvt;
3136 V_R8(result) = V_R8(&lv) / V_R8(&rv);
3140 if (V_I4(&rv) == 0) return DISP_E_DIVBYZERO;
3141 V_VT(result) = resvt;
3142 V_I4(result) = V_I4(&lv) / V_I4(&rv);
3146 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3147 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3151 /**********************************************************************
3152 * VarSub [OLEAUT32.159]
3154 * Subtract two variants.
3157 * left [I] First variant
3158 * right [I] Second variant
3159 * result [O] Result variant
3163 * Failure: An HRESULT error code indicating the error.
3165 HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3167 HRESULT rc = E_FAIL;
3168 VARTYPE lvt,rvt,resvt;
3172 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3173 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3175 VariantInit(&lv);VariantInit(&rv);
3176 lvt = V_VT(left)&VT_TYPEMASK;
3177 rvt = V_VT(right)&VT_TYPEMASK;
3178 found = FALSE;resvt = VT_VOID;
3179 if (((1<<lvt) | (1<<rvt)) & ((1<<VT_DATE)|(1<<VT_R4)|(1<<VT_R8))) {
3183 if (!found && (((1<<lvt) | (1<<rvt)) & (VTBIT_I1|VTBIT_I2|VTBIT_UI1|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|(1<<VT_INT)|(1<<VT_UINT)))) {
3188 FIXME("can't expand vt %d vs %d to a target type.\n",lvt,rvt);
3191 rc = VariantChangeType(&lv, left, 0, resvt);
3193 FIXME("Could not convert 0x%x to %d?\n",V_VT(left),resvt);
3196 rc = VariantChangeType(&rv, right, 0, resvt);
3198 FIXME("Could not convert 0x%x to %d?\n",V_VT(right),resvt);
3203 V_VT(result) = resvt;
3204 V_R8(result) = V_R8(&lv) - V_R8(&rv);
3208 V_VT(result) = resvt;
3209 V_I4(result) = V_I4(&lv) - V_I4(&rv);
3213 TRACE("returning 0x%8lx (%s%s),%g\n", rc, debugstr_VT(result),
3214 debugstr_VF(result), V_VT(result) == VT_R8 ? V_R8(result) : (double)V_I4(result));
3218 /**********************************************************************
3219 * VarOr [OLEAUT32.157]
3221 * Perform a logical or (OR) operation on two variants.
3224 * pVarLeft [I] First variant
3225 * pVarRight [I] Variant to OR with pVarLeft
3226 * pVarOut [O] Destination for OR result
3229 * Success: S_OK. pVarOut contains the result of the operation with its type
3230 * taken from the table listed under VarXor().
3231 * Failure: An HRESULT error code indicating the error.
3234 * See the Notes section of VarXor() for further information.
3236 HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3239 VARIANT varLeft, varRight, varStr;
3242 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3243 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3244 debugstr_VF(pVarRight), pVarOut);
3246 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3247 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3248 V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3249 V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3250 return DISP_E_BADVARTYPE;
3252 V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
3254 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3256 /* NULL OR Zero is NULL, NULL OR value is value */
3257 if (V_VT(pVarLeft) == VT_NULL)
3258 pVarLeft = pVarRight; /* point to the non-NULL var */
3260 V_VT(pVarOut) = VT_NULL;
3263 switch (V_VT(pVarLeft))
3265 case VT_DATE: case VT_R8:
3270 if (V_BOOL(pVarLeft))
3271 *pVarOut = *pVarLeft;
3273 case VT_I2: case VT_UI2:
3282 if (V_UI1(pVarLeft))
3283 *pVarOut = *pVarLeft;
3289 case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
3294 if (V_CY(pVarLeft).int64)
3297 case VT_I8: case VT_UI8:
3302 if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
3309 if (!V_BSTR(pVarLeft))
3310 return DISP_E_BADVARTYPE;
3312 hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3313 if (SUCCEEDED(hRet) && b)
3315 V_VT(pVarOut) = VT_BOOL;
3316 V_BOOL(pVarOut) = b;
3320 case VT_NULL: case VT_EMPTY:
3321 V_VT(pVarOut) = VT_NULL;
3324 return DISP_E_BADVARTYPE;
3328 if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
3330 if (V_VT(pVarLeft) == VT_EMPTY)
3331 pVarLeft = pVarRight; /* point to the non-EMPTY var */
3334 /* Since one argument is empty (0), OR'ing it with the other simply
3335 * gives the others value (as 0|x => x). So just convert the other
3336 * argument to the required result type.
3338 switch (V_VT(pVarLeft))
3341 if (!V_BSTR(pVarLeft))
3342 return DISP_E_BADVARTYPE;
3344 hRet = VariantCopy(&varStr, pVarLeft);
3348 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3351 /* Fall Through ... */
3352 case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
3353 V_VT(pVarOut) = VT_I2;
3355 case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
3356 case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
3357 case VT_INT: case VT_UINT: case VT_UI8:
3358 V_VT(pVarOut) = VT_I4;
3361 V_VT(pVarOut) = VT_I8;
3364 return DISP_E_BADVARTYPE;
3366 hRet = VariantCopy(&varLeft, pVarLeft);
3369 pVarLeft = &varLeft;
3370 hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
3374 if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
3376 V_VT(pVarOut) = VT_BOOL;
3377 V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
3381 if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
3383 V_VT(pVarOut) = VT_UI1;
3384 V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
3388 if (V_VT(pVarLeft) == VT_BSTR)
3390 hRet = VariantCopy(&varStr, pVarLeft);
3394 hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
3399 if (V_VT(pVarLeft) == VT_BOOL &&
3400 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
3404 else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
3405 V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
3406 (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
3407 V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
3411 else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
3413 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3414 return DISP_E_TYPEMISMATCH;
3418 hRet = VariantCopy(&varLeft, pVarLeft);
3422 hRet = VariantCopy(&varRight, pVarRight);
3426 if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3427 V_VT(&varLeft) = VT_I4; /* Don't overflow */
3432 if (V_VT(&varLeft) == VT_BSTR &&
3433 FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
3434 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
3435 if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
3436 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3441 if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
3442 V_VT(&varRight) = VT_I4; /* Don't overflow */
3447 if (V_VT(&varRight) == VT_BSTR &&
3448 FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
3449 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
3450 if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
3451 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3459 V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
3461 else if (vt == VT_I4)
3463 V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
3467 V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
3471 VariantClear(&varStr);
3472 VariantClear(&varLeft);
3473 VariantClear(&varRight);
3477 /**********************************************************************
3478 * VarAbs [OLEAUT32.168]
3480 * Convert a variant to its absolute value.
3483 * pVarIn [I] Source variant
3484 * pVarOut [O] Destination for converted value
3487 * Success: S_OK. pVarOut contains the absolute value of pVarIn.
3488 * Failure: An HRESULT error code indicating the error.
3491 * - This function does not process by-reference variants.
3492 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3493 * according to the following table:
3494 *| Input Type Output Type
3495 *| ---------- -----------
3498 *| (All others) Unchanged
3500 HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
3503 HRESULT hRet = S_OK;
3505 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3506 debugstr_VF(pVarIn), pVarOut);
3508 if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
3509 V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
3510 V_VT(pVarIn) == VT_ERROR)
3511 return DISP_E_TYPEMISMATCH;
3513 *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
3515 #define ABS_CASE(typ,min) \
3516 case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
3517 else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
3520 switch (V_VT(pVarIn))
3522 ABS_CASE(I1,I1_MIN);
3524 V_VT(pVarOut) = VT_I2;
3525 /* BOOL->I2, Fall through ... */
3526 ABS_CASE(I2,I2_MIN);
3528 ABS_CASE(I4,I4_MIN);
3529 ABS_CASE(I8,I8_MIN);
3530 ABS_CASE(R4,R4_MIN);
3532 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
3535 V_VT(pVarOut) = VT_R8;
3537 /* Fall through ... */
3539 ABS_CASE(R8,R8_MIN);
3541 hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
3544 DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
3554 V_VT(pVarOut) = VT_I2;
3559 hRet = DISP_E_BADVARTYPE;
3565 /**********************************************************************
3566 * VarFix [OLEAUT32.169]
3568 * Truncate a variants value to a whole number.
3571 * pVarIn [I] Source variant
3572 * pVarOut [O] Destination for converted value
3575 * Success: S_OK. pVarOut contains the converted value.
3576 * Failure: An HRESULT error code indicating the error.
3579 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3580 * according to the following table:
3581 *| Input Type Output Type
3582 *| ---------- -----------
3586 *| All Others Unchanged
3587 * - The difference between this function and VarInt() is that VarInt() rounds
3588 * negative numbers away from 0, while this function rounds them towards zero.
3590 HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
3592 HRESULT hRet = S_OK;
3594 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3595 debugstr_VF(pVarIn), pVarOut);
3597 V_VT(pVarOut) = V_VT(pVarIn);
3599 switch (V_VT(pVarIn))
3602 V_UI1(pVarOut) = V_UI1(pVarIn);
3605 V_VT(pVarOut) = VT_I2;
3608 V_I2(pVarOut) = V_I2(pVarIn);
3611 V_I4(pVarOut) = V_I4(pVarIn);
3614 V_I8(pVarOut) = V_I8(pVarIn);
3617 if (V_R4(pVarIn) < 0.0f)
3618 V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
3620 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3623 V_VT(pVarOut) = VT_R8;
3624 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3629 if (V_R8(pVarIn) < 0.0)
3630 V_R8(pVarOut) = ceil(V_R8(pVarIn));
3632 V_R8(pVarOut) = floor(V_R8(pVarIn));
3635 hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
3638 hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3641 V_VT(pVarOut) = VT_I2;
3648 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
3649 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
3650 hRet = DISP_E_BADVARTYPE;
3652 hRet = DISP_E_TYPEMISMATCH;
3655 V_VT(pVarOut) = VT_EMPTY;
3660 /**********************************************************************
3661 * VarInt [OLEAUT32.172]
3663 * Truncate a variants value to a whole number.
3666 * pVarIn [I] Source variant
3667 * pVarOut [O] Destination for converted value
3670 * Success: S_OK. pVarOut contains the converted value.
3671 * Failure: An HRESULT error code indicating the error.
3674 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3675 * according to the following table:
3676 *| Input Type Output Type
3677 *| ---------- -----------
3681 *| All Others Unchanged
3682 * - The difference between this function and VarFix() is that VarFix() rounds
3683 * negative numbers towards 0, while this function rounds them away from zero.
3685 HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
3687 HRESULT hRet = S_OK;
3689 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3690 debugstr_VF(pVarIn), pVarOut);
3692 V_VT(pVarOut) = V_VT(pVarIn);
3694 switch (V_VT(pVarIn))
3697 V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
3700 V_VT(pVarOut) = VT_R8;
3701 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
3706 V_R8(pVarOut) = floor(V_R8(pVarIn));
3709 hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
3712 hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
3715 return VarFix(pVarIn, pVarOut);
3721 /**********************************************************************
3722 * VarXor [OLEAUT32.167]
3724 * Perform a logical exclusive-or (XOR) operation on two variants.
3727 * pVarLeft [I] First variant
3728 * pVarRight [I] Variant to XOR with pVarLeft
3729 * pVarOut [O] Destination for XOR result
3732 * Success: S_OK. pVarOut contains the result of the operation with its type
3733 * taken from the table below).
3734 * Failure: An HRESULT error code indicating the error.
3737 * - Neither pVarLeft or pVarRight are modified by this function.
3738 * - This function does not process by-reference variants.
3739 * - Input types of VT_BSTR may be numeric strings or boolean text.
3740 * - The type of result stored in pVarOut depends on the types of pVarLeft
3741 * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
3742 * or VT_NULL if the function succeeds.
3743 * - Type promotion is inconsistent and as a result certain combinations of
3744 * values will return DISP_E_OVERFLOW even when they could be represented.
3745 * This matches the behaviour of native oleaut32.
3747 HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3750 VARIANT varLeft, varRight;
3754 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3755 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3756 debugstr_VF(pVarRight), pVarOut);
3758 if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3759 V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
3760 V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
3761 V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3762 V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
3763 V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
3764 return DISP_E_BADVARTYPE;
3766 if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3768 /* NULL XOR anything valid is NULL */
3769 V_VT(pVarOut) = VT_NULL;
3773 /* Copy our inputs so we don't disturb anything */
3774 V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
3776 hRet = VariantCopy(&varLeft, pVarLeft);
3780 hRet = VariantCopy(&varRight, pVarRight);
3784 /* Try any strings first as numbers, then as VT_BOOL */
3785 if (V_VT(&varLeft) == VT_BSTR)
3787 hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
3788 hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
3789 FAILED(hRet) ? VT_BOOL : VT_I4);
3794 if (V_VT(&varRight) == VT_BSTR)
3796 hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
3797 hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
3798 FAILED(hRet) ? VT_BOOL : VT_I4);
3803 /* Determine the result type */
3804 if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
3806 if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
3807 return DISP_E_TYPEMISMATCH;
3812 switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
3814 case (VT_BOOL << 16) | VT_BOOL:
3817 case (VT_UI1 << 16) | VT_UI1:
3820 case (VT_EMPTY << 16) | VT_EMPTY:
3821 case (VT_EMPTY << 16) | VT_UI1:
3822 case (VT_EMPTY << 16) | VT_I2:
3823 case (VT_EMPTY << 16) | VT_BOOL:
3824 case (VT_UI1 << 16) | VT_EMPTY:
3825 case (VT_UI1 << 16) | VT_I2:
3826 case (VT_UI1 << 16) | VT_BOOL:
3827 case (VT_I2 << 16) | VT_EMPTY:
3828 case (VT_I2 << 16) | VT_UI1:
3829 case (VT_I2 << 16) | VT_I2:
3830 case (VT_I2 << 16) | VT_BOOL:
3831 case (VT_BOOL << 16) | VT_EMPTY:
3832 case (VT_BOOL << 16) | VT_UI1:
3833 case (VT_BOOL << 16) | VT_I2:
3842 /* VT_UI4 does not overflow */
3845 if (V_VT(&varLeft) == VT_UI4)
3846 V_VT(&varLeft) = VT_I4;
3847 if (V_VT(&varRight) == VT_UI4)
3848 V_VT(&varRight) = VT_I4;
3851 /* Convert our input copies to the result type */
3852 if (V_VT(&varLeft) != vt)
3853 hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
3857 if (V_VT(&varRight) != vt)
3858 hRet = VariantChangeType(&varRight, &varRight, 0, vt);
3864 /* Calculate the result */
3868 V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
3871 V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
3875 V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
3878 V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
3883 VariantClear(&varLeft);
3884 VariantClear(&varRight);
3888 /**********************************************************************
3889 * VarEqv [OLEAUT32.172]
3891 * Determine if two variants contain the same value.
3894 * pVarLeft [I] First variant to compare
3895 * pVarRight [I] Variant to compare to pVarLeft
3896 * pVarOut [O] Destination for comparison result
3899 * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
3900 * if equivalent or non-zero otherwise.
3901 * Failure: An HRESULT error code indicating the error.
3904 * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
3907 HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3911 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
3912 debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
3913 debugstr_VF(pVarRight), pVarOut);
3915 hRet = VarXor(pVarLeft, pVarRight, pVarOut);
3916 if (SUCCEEDED(hRet))
3918 if (V_VT(pVarOut) == VT_I8)
3919 V_I8(pVarOut) = ~V_I8(pVarOut);
3921 V_UI4(pVarOut) = ~V_UI4(pVarOut);
3926 /**********************************************************************
3927 * VarNeg [OLEAUT32.173]
3929 * Negate the value of a variant.
3932 * pVarIn [I] Source variant
3933 * pVarOut [O] Destination for converted value
3936 * Success: S_OK. pVarOut contains the converted value.
3937 * Failure: An HRESULT error code indicating the error.
3940 * - The type of the value stored in pVarOut depends on the type of pVarIn,
3941 * according to the following table:
3942 *| Input Type Output Type
3943 *| ---------- -----------
3948 *| All Others Unchanged (unless promoted)
3949 * - Where the negated value of a variant does not fit in its base type, the type
3950 * is promoted according to the following table:
3951 *| Input Type Promoted To
3952 *| ---------- -----------
3956 * - The native version of this function returns DISP_E_BADVARTYPE for valid
3957 * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
3958 * for types which are not valid. Since this is in contravention of the
3959 * meaning of those error codes and unlikely to be relied on by applications,
3960 * this implementation returns errors consistent with the other high level
3961 * variant math functions.
3963 HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
3965 HRESULT hRet = S_OK;
3967 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
3968 debugstr_VF(pVarIn), pVarOut);
3970 V_VT(pVarOut) = V_VT(pVarIn);
3972 switch (V_VT(pVarIn))
3975 V_VT(pVarOut) = VT_I2;
3976 V_I2(pVarOut) = -V_UI1(pVarIn);
3979 V_VT(pVarOut) = VT_I2;
3982 if (V_I2(pVarIn) == I2_MIN)
3984 V_VT(pVarOut) = VT_I4;
3985 V_I4(pVarOut) = -(int)V_I2(pVarIn);
3988 V_I2(pVarOut) = -V_I2(pVarIn);
3991 if (V_I4(pVarIn) == I4_MIN)
3993 V_VT(pVarOut) = VT_R8;
3994 V_R8(pVarOut) = -(double)V_I4(pVarIn);
3997 V_I4(pVarOut) = -V_I4(pVarIn);
4000 if (V_I8(pVarIn) == I8_MIN)
4002 V_VT(pVarOut) = VT_R8;
4003 hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4004 V_R8(pVarOut) *= -1.0;
4007 V_I8(pVarOut) = -V_I8(pVarIn);
4010 V_R4(pVarOut) = -V_R4(pVarIn);
4014 V_R8(pVarOut) = -V_R8(pVarIn);
4017 hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4020 hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4023 V_VT(pVarOut) = VT_R8;
4024 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4025 V_R8(pVarOut) = -V_R8(pVarOut);
4028 V_VT(pVarOut) = VT_I2;
4035 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4036 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4037 hRet = DISP_E_BADVARTYPE;
4039 hRet = DISP_E_TYPEMISMATCH;
4042 V_VT(pVarOut) = VT_EMPTY;
4047 /**********************************************************************
4048 * VarNot [OLEAUT32.174]
4050 * Perform a not operation on a variant.
4053 * pVarIn [I] Source variant
4054 * pVarOut [O] Destination for converted value
4057 * Success: S_OK. pVarOut contains the converted value.
4058 * Failure: An HRESULT error code indicating the error.
4061 * - Strictly speaking, this function performs a bitwise ones complement
4062 * on the variants value (after possibly converting to VT_I4, see below).
4063 * This only behaves like a boolean not operation if the value in
4064 * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4065 * - To perform a genuine not operation, convert the variant to a VT_BOOL
4066 * before calling this function.
4067 * - This function does not process by-reference variants.
4068 * - The type of the value stored in pVarOut depends on the type of pVarIn,
4069 * according to the following table:
4070 *| Input Type Output Type
4071 *| ---------- -----------
4078 *| (All others) Unchanged
4080 HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4083 HRESULT hRet = S_OK;
4085 TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
4086 debugstr_VF(pVarIn), pVarOut);
4088 V_VT(pVarOut) = V_VT(pVarIn);
4090 switch (V_VT(pVarIn))
4093 V_I4(pVarOut) = ~V_I1(pVarIn);
4094 V_VT(pVarOut) = VT_I4;
4096 case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4098 case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4100 V_I4(pVarOut) = ~V_UI2(pVarIn);
4101 V_VT(pVarOut) = VT_I4;
4104 hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4108 /* Fall through ... */
4110 V_VT(pVarOut) = VT_I4;
4111 /* Fall through ... */
4112 case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4115 V_I4(pVarOut) = ~V_UI4(pVarIn);
4116 V_VT(pVarOut) = VT_I4;
4118 case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
4120 V_I4(pVarOut) = ~V_UI8(pVarIn);
4121 V_VT(pVarOut) = VT_I4;
4124 hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
4125 V_I4(pVarOut) = ~V_I4(pVarOut);
4126 V_VT(pVarOut) = VT_I4;
4129 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4133 /* Fall through ... */
4136 hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4137 V_I4(pVarOut) = ~V_I4(pVarOut);
4138 V_VT(pVarOut) = VT_I4;
4141 hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4142 V_I4(pVarOut) = ~V_I4(pVarOut);
4143 V_VT(pVarOut) = VT_I4;
4147 V_VT(pVarOut) = VT_I2;
4153 if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4154 FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4155 hRet = DISP_E_BADVARTYPE;
4157 hRet = DISP_E_TYPEMISMATCH;
4160 V_VT(pVarOut) = VT_EMPTY;
4165 /**********************************************************************
4166 * VarRound [OLEAUT32.175]
4168 * Perform a round operation on a variant.
4171 * pVarIn [I] Source variant
4172 * deci [I] Number of decimals to round to
4173 * pVarOut [O] Destination for converted value
4176 * Success: S_OK. pVarOut contains the converted value.
4177 * Failure: An HRESULT error code indicating the error.
4180 * - Floating point values are rounded to the desired number of decimals.
4181 * - Some integer types are just copied to the return variable.
4182 * - Some other integer types are not handled and fail.
4184 HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
4187 HRESULT hRet = S_OK;
4190 TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);
4192 switch (V_VT(pVarIn))
4194 /* cases that fail on windows */
4199 hRet = DISP_E_BADVARTYPE;
4202 /* cases just copying in to out */
4204 V_VT(pVarOut) = V_VT(pVarIn);
4205 V_UI1(pVarOut) = V_UI1(pVarIn);
4208 V_VT(pVarOut) = V_VT(pVarIn);
4209 V_I2(pVarOut) = V_I2(pVarIn);
4212 V_VT(pVarOut) = V_VT(pVarIn);
4213 V_I4(pVarOut) = V_I4(pVarIn);
4216 V_VT(pVarOut) = V_VT(pVarIn);
4217 /* value unchanged */
4220 /* cases that change type */
4222 V_VT(pVarOut) = VT_I2;
4226 V_VT(pVarOut) = VT_I2;
4227 V_I2(pVarOut) = V_BOOL(pVarIn);
4230 hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4235 /* Fall through ... */
4237 /* cases we need to do math */
4239 if (V_R8(pVarIn)>0) {
4240 V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4242 V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4244 V_VT(pVarOut) = V_VT(pVarIn);
4247 if (V_R4(pVarIn)>0) {
4248 V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4250 V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4252 V_VT(pVarOut) = V_VT(pVarIn);
4255 if (V_DATE(pVarIn)>0) {
4256 V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
4258 V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
4260 V_VT(pVarOut) = V_VT(pVarIn);
4266 factor=pow(10, 4-deci);
4268 if (V_CY(pVarIn).int64>0) {
4269 V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
4271 V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
4273 V_VT(pVarOut) = V_VT(pVarIn);
4276 /* cases we don't know yet */
4278 FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
4279 V_VT(pVarIn) & VT_TYPEMASK, deci);
4280 hRet = DISP_E_BADVARTYPE;
4284 V_VT(pVarOut) = VT_EMPTY;
4286 TRACE("returning 0x%08lx (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
4287 debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
4288 (V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);
4293 /**********************************************************************
4294 * VarIdiv [OLEAUT32.153]
4296 * Converts input variants to integers and divides them.
4299 * left [I] Left hand variant
4300 * right [I] Right hand variant
4301 * result [O] Destination for quotient
4304 * Success: S_OK. result contains the quotient.
4305 * Failure: An HRESULT error code indicating the error.
4308 * If either expression is null, null is returned, as per MSDN
4310 HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4318 if ((V_VT(left) == VT_NULL) || (V_VT(right) == VT_NULL)) {
4319 hr = VariantChangeType(result, result, 0, VT_NULL);
4321 /* This should never happen */
4322 FIXME("Failed to convert return value to VT_NULL.\n");
4328 hr = VariantChangeType(&lv, left, 0, VT_I4);
4332 hr = VariantChangeType(&rv, right, 0, VT_I4);
4337 hr = VarDiv(&lv, &rv, result);
4342 /**********************************************************************
4343 * VarMod [OLEAUT32.155]
4345 * Perform the modulus operation of the right hand variant on the left
4348 * left [I] Left hand variant
4349 * right [I] Right hand variant
4350 * result [O] Destination for converted value
4353 * Success: S_OK. result contains the remainder.
4354 * Failure: An HRESULT error code indicating the error.
4357 * If an error occurs the type of result will be modified but the value will not be.
4358 * Doesn't support arrays or any special flags yet.
4360 HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4364 HRESULT rc = E_FAIL;
4371 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
4372 debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
4374 /* check for invalid inputs */
4376 switch (V_VT(left) & VT_TYPEMASK) {
4397 V_VT(result) = VT_EMPTY;
4398 return DISP_E_TYPEMISMATCH;
4400 V_VT(result) = VT_EMPTY;
4401 return DISP_E_OVERFLOW;
4403 return DISP_E_TYPEMISMATCH;
4405 V_VT(result) = VT_EMPTY;
4406 return DISP_E_TYPEMISMATCH;
4410 V_VT(result) = VT_EMPTY;
4411 return DISP_E_BADVARTYPE;
4416 switch (V_VT(right) & VT_TYPEMASK) {
4422 if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
4424 V_VT(result) = VT_EMPTY;
4425 return DISP_E_TYPEMISMATCH;
4428 if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
4430 V_VT(result) = VT_EMPTY;
4431 return DISP_E_TYPEMISMATCH;
4441 if(V_VT(left) == VT_EMPTY)
4443 V_VT(result) = VT_I4;
4449 if(V_VT(left) == VT_NULL)
4451 V_VT(result) = VT_NULL;
4457 V_VT(result) = VT_EMPTY;
4458 return DISP_E_BADVARTYPE;
4460 if(V_VT(left) == VT_VOID)
4462 V_VT(result) = VT_EMPTY;
4463 return DISP_E_BADVARTYPE;
4464 } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
4467 V_VT(result) = VT_NULL;
4471 V_VT(result) = VT_NULL;
4472 return DISP_E_BADVARTYPE;
4476 V_VT(result) = VT_EMPTY;
4477 return DISP_E_TYPEMISMATCH;
4479 if(V_VT(left) == VT_ERROR)
4481 V_VT(result) = VT_EMPTY;
4482 return DISP_E_TYPEMISMATCH;
4485 V_VT(result) = VT_EMPTY;
4486 return DISP_E_OVERFLOW;
4489 return DISP_E_TYPEMISMATCH;
4491 if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
4493 V_VT(result) = VT_EMPTY;
4494 return DISP_E_BADVARTYPE;
4497 V_VT(result) = VT_EMPTY;
4498 return DISP_E_TYPEMISMATCH;
4501 V_VT(result) = VT_EMPTY;
4502 return DISP_E_BADVARTYPE;
4505 /* determine the result type */
4506 if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
4507 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4508 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
4509 else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
4510 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4511 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4512 else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
4513 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
4514 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
4515 else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
4516 else resT = VT_I4; /* most outputs are I4 */
4518 /* convert to I8 for the modulo */
4519 rc = VariantChangeType(&lv, left, 0, VT_I8);
4522 FIXME("Could not convert left type %d to %d? rc == 0x%lX\n", V_VT(left), VT_I8, rc);
4526 rc = VariantChangeType(&rv, right, 0, VT_I8);
4529 FIXME("Could not convert right type %d to %d? rc == 0x%lX\n", V_VT(right), VT_I8, rc);
4533 /* if right is zero set VT_EMPTY and return divide by zero */
4536 V_VT(result) = VT_EMPTY;
4537 return DISP_E_DIVBYZERO;
4540 /* perform the modulo operation */
4541 V_VT(result) = VT_I8;
4542 V_I8(result) = V_I8(&lv) % V_I8(&rv);
4544 TRACE("V_I8(left) == %ld, V_I8(right) == %ld, V_I8(result) == %ld\n", (long)V_I8(&lv), (long)V_I8(&rv), (long)V_I8(result));
4546 /* convert left and right to the destination type */
4547 rc = VariantChangeType(result, result, 0, resT);
4550 FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
4557 /**********************************************************************
4558 * VarPow [OLEAUT32.158]
4560 * Computes the power of one variant to another variant.
4563 * left [I] First variant
4564 * right [I] Second variant
4565 * result [O] Result variant
4569 * Failure: An HRESULT error code indicating the error.
4571 HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
4576 TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
4577 right, debugstr_VT(right), debugstr_VF(right), result);
4579 hr = VariantChangeType(&dl,left,0,VT_R8);
4580 if (!SUCCEEDED(hr)) {
4581 ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
4584 hr = VariantChangeType(&dr,right,0,VT_R8);
4585 if (!SUCCEEDED(hr)) {
4586 ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
4589 V_VT(result) = VT_R8;
4590 V_R8(result) = pow(V_R8(&dl),V_R8(&dr));