1c2c66affSColin Finck /*
2c2c66affSColin Finck * VARIANT
3c2c66affSColin Finck *
4c2c66affSColin Finck * Copyright 1998 Jean-Claude Cote
5c2c66affSColin Finck * Copyright 2003 Jon Griffiths
6c2c66affSColin Finck * Copyright 2005 Daniel Remenak
7c2c66affSColin Finck * Copyright 2006 Google (Benjamin Arai)
8c2c66affSColin Finck *
9c2c66affSColin Finck * The algorithm for conversion from Julian days to day/month/year is based on
10c2c66affSColin Finck * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
11c2c66affSColin Finck * Copyright 1994-7 Regents of the University of California
12c2c66affSColin Finck *
13c2c66affSColin Finck * This library is free software; you can redistribute it and/or
14c2c66affSColin Finck * modify it under the terms of the GNU Lesser General Public
15c2c66affSColin Finck * License as published by the Free Software Foundation; either
16c2c66affSColin Finck * version 2.1 of the License, or (at your option) any later version.
17c2c66affSColin Finck *
18c2c66affSColin Finck * This library is distributed in the hope that it will be useful,
19c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
20c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21c2c66affSColin Finck * Lesser General Public License for more details.
22c2c66affSColin Finck *
23c2c66affSColin Finck * You should have received a copy of the GNU Lesser General Public
24c2c66affSColin Finck * License along with this library; if not, write to the Free Software
25c2c66affSColin Finck * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26c2c66affSColin Finck */
27c2c66affSColin Finck
28d506c2afSAmine Khaldi #include <string.h>
29d506c2afSAmine Khaldi #include <stdlib.h>
30d506c2afSAmine Khaldi #include <stdarg.h>
31d506c2afSAmine Khaldi
32d506c2afSAmine Khaldi #define COBJMACROS
33d506c2afSAmine Khaldi #define NONAMELESSUNION
34d506c2afSAmine Khaldi #define NONAMELESSSTRUCT
35d506c2afSAmine Khaldi
36d506c2afSAmine Khaldi #include "windef.h"
37d506c2afSAmine Khaldi #include "winbase.h"
38d506c2afSAmine Khaldi #include "winerror.h"
39d506c2afSAmine Khaldi #include "variant.h"
40d506c2afSAmine Khaldi #include "resource.h"
41d506c2afSAmine Khaldi #include "wine/debug.h"
42c2c66affSColin Finck
43c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(variant);
44c2c66affSColin Finck
45c2c66affSColin Finck static CRITICAL_SECTION cache_cs;
46c2c66affSColin Finck static CRITICAL_SECTION_DEBUG critsect_debug =
47c2c66affSColin Finck {
48c2c66affSColin Finck 0, 0, &cache_cs,
49c2c66affSColin Finck { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
50c2c66affSColin Finck 0, 0, { (DWORD_PTR)(__FILE__ ": cache_cs") }
51c2c66affSColin Finck };
52c2c66affSColin Finck static CRITICAL_SECTION cache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
53c2c66affSColin Finck
54c2c66affSColin Finck /* Convert a variant from one type to another */
VARIANT_Coerce(VARIANTARG * pd,LCID lcid,USHORT wFlags,VARIANTARG * ps,VARTYPE vt)55c2c66affSColin Finck static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
56c2c66affSColin Finck VARIANTARG* ps, VARTYPE vt)
57c2c66affSColin Finck {
58c2c66affSColin Finck HRESULT res = DISP_E_TYPEMISMATCH;
59c2c66affSColin Finck VARTYPE vtFrom = V_TYPE(ps);
60c2c66affSColin Finck DWORD dwFlags = 0;
61c2c66affSColin Finck
62c2c66affSColin Finck TRACE("(%s,0x%08x,0x%04x,%s,%s)\n", debugstr_variant(pd), lcid, wFlags,
63c2c66affSColin Finck debugstr_variant(ps), debugstr_vt(vt));
64c2c66affSColin Finck
65c2c66affSColin Finck if (vt == VT_BSTR || vtFrom == VT_BSTR)
66c2c66affSColin Finck {
67c2c66affSColin Finck /* All flags passed to low level function are only used for
68c2c66affSColin Finck * changing to or from strings. Map these here.
69c2c66affSColin Finck */
70c2c66affSColin Finck if (wFlags & VARIANT_LOCALBOOL)
71c2c66affSColin Finck dwFlags |= VAR_LOCALBOOL;
72c2c66affSColin Finck if (wFlags & VARIANT_CALENDAR_HIJRI)
73c2c66affSColin Finck dwFlags |= VAR_CALENDAR_HIJRI;
74c2c66affSColin Finck if (wFlags & VARIANT_CALENDAR_THAI)
75c2c66affSColin Finck dwFlags |= VAR_CALENDAR_THAI;
76c2c66affSColin Finck if (wFlags & VARIANT_CALENDAR_GREGORIAN)
77c2c66affSColin Finck dwFlags |= VAR_CALENDAR_GREGORIAN;
78c2c66affSColin Finck if (wFlags & VARIANT_NOUSEROVERRIDE)
79c2c66affSColin Finck dwFlags |= LOCALE_NOUSEROVERRIDE;
80c2c66affSColin Finck if (wFlags & VARIANT_USE_NLS)
81c2c66affSColin Finck dwFlags |= LOCALE_USE_NLS;
82c2c66affSColin Finck }
83c2c66affSColin Finck
84c2c66affSColin Finck /* Map int/uint to i4/ui4 */
85c2c66affSColin Finck if (vt == VT_INT)
86c2c66affSColin Finck vt = VT_I4;
87c2c66affSColin Finck else if (vt == VT_UINT)
88c2c66affSColin Finck vt = VT_UI4;
89c2c66affSColin Finck
90c2c66affSColin Finck if (vtFrom == VT_INT)
91c2c66affSColin Finck vtFrom = VT_I4;
92c2c66affSColin Finck else if (vtFrom == VT_UINT)
93c2c66affSColin Finck vtFrom = VT_UI4;
94c2c66affSColin Finck
95c2c66affSColin Finck if (vt == vtFrom)
96c2c66affSColin Finck return VariantCopy(pd, ps);
97c2c66affSColin Finck
98c2c66affSColin Finck if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
99c2c66affSColin Finck {
100c2c66affSColin Finck /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
101c2c66affSColin Finck * accessing the default object property.
102c2c66affSColin Finck */
103c2c66affSColin Finck return DISP_E_TYPEMISMATCH;
104c2c66affSColin Finck }
105c2c66affSColin Finck
106c2c66affSColin Finck switch (vt)
107c2c66affSColin Finck {
108c2c66affSColin Finck case VT_EMPTY:
109c2c66affSColin Finck if (vtFrom == VT_NULL)
110c2c66affSColin Finck return DISP_E_TYPEMISMATCH;
111c2c66affSColin Finck /* ... Fall through */
112c2c66affSColin Finck case VT_NULL:
113c2c66affSColin Finck if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
114c2c66affSColin Finck {
115c2c66affSColin Finck res = VariantClear( pd );
116c2c66affSColin Finck if (vt == VT_NULL && SUCCEEDED(res))
117c2c66affSColin Finck V_VT(pd) = VT_NULL;
118c2c66affSColin Finck }
119c2c66affSColin Finck return res;
120c2c66affSColin Finck
121c2c66affSColin Finck case VT_I1:
122c2c66affSColin Finck switch (vtFrom)
123c2c66affSColin Finck {
124c2c66affSColin Finck case VT_EMPTY: V_I1(pd) = 0; return S_OK;
125c2c66affSColin Finck case VT_I2: return VarI1FromI2(V_I2(ps), &V_I1(pd));
126c2c66affSColin Finck case VT_I4: return VarI1FromI4(V_I4(ps), &V_I1(pd));
127c2c66affSColin Finck case VT_UI1: V_I1(pd) = V_UI1(ps); return S_OK;
128c2c66affSColin Finck case VT_UI2: return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
129c2c66affSColin Finck case VT_UI4: return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
130c2c66affSColin Finck case VT_I8: return VarI1FromI8(V_I8(ps), &V_I1(pd));
131c2c66affSColin Finck case VT_UI8: return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
132c2c66affSColin Finck case VT_R4: return VarI1FromR4(V_R4(ps), &V_I1(pd));
133c2c66affSColin Finck case VT_R8: return VarI1FromR8(V_R8(ps), &V_I1(pd));
134c2c66affSColin Finck case VT_DATE: return VarI1FromDate(V_DATE(ps), &V_I1(pd));
135c2c66affSColin Finck case VT_BOOL: return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
136c2c66affSColin Finck case VT_CY: return VarI1FromCy(V_CY(ps), &V_I1(pd));
137c2c66affSColin Finck case VT_DECIMAL: return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
138c2c66affSColin Finck case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
139c2c66affSColin Finck case VT_BSTR: return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
140c2c66affSColin Finck }
141c2c66affSColin Finck break;
142c2c66affSColin Finck
143c2c66affSColin Finck case VT_I2:
144c2c66affSColin Finck switch (vtFrom)
145c2c66affSColin Finck {
146c2c66affSColin Finck case VT_EMPTY: V_I2(pd) = 0; return S_OK;
147c2c66affSColin Finck case VT_I1: return VarI2FromI1(V_I1(ps), &V_I2(pd));
148c2c66affSColin Finck case VT_I4: return VarI2FromI4(V_I4(ps), &V_I2(pd));
149c2c66affSColin Finck case VT_UI1: return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
150c2c66affSColin Finck case VT_UI2: V_I2(pd) = V_UI2(ps); return S_OK;
151c2c66affSColin Finck case VT_UI4: return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
152c2c66affSColin Finck case VT_I8: return VarI2FromI8(V_I8(ps), &V_I2(pd));
153c2c66affSColin Finck case VT_UI8: return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
154c2c66affSColin Finck case VT_R4: return VarI2FromR4(V_R4(ps), &V_I2(pd));
155c2c66affSColin Finck case VT_R8: return VarI2FromR8(V_R8(ps), &V_I2(pd));
156c2c66affSColin Finck case VT_DATE: return VarI2FromDate(V_DATE(ps), &V_I2(pd));
157c2c66affSColin Finck case VT_BOOL: return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
158c2c66affSColin Finck case VT_CY: return VarI2FromCy(V_CY(ps), &V_I2(pd));
159c2c66affSColin Finck case VT_DECIMAL: return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
160c2c66affSColin Finck case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
161c2c66affSColin Finck case VT_BSTR: return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
162c2c66affSColin Finck }
163c2c66affSColin Finck break;
164c2c66affSColin Finck
165c2c66affSColin Finck case VT_I4:
166c2c66affSColin Finck switch (vtFrom)
167c2c66affSColin Finck {
168c2c66affSColin Finck case VT_EMPTY: V_I4(pd) = 0; return S_OK;
169c2c66affSColin Finck case VT_I1: return VarI4FromI1(V_I1(ps), &V_I4(pd));
170c2c66affSColin Finck case VT_I2: return VarI4FromI2(V_I2(ps), &V_I4(pd));
171c2c66affSColin Finck case VT_UI1: return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
172c2c66affSColin Finck case VT_UI2: return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
173c2c66affSColin Finck case VT_UI4: V_I4(pd) = V_UI4(ps); return S_OK;
174c2c66affSColin Finck case VT_I8: return VarI4FromI8(V_I8(ps), &V_I4(pd));
175c2c66affSColin Finck case VT_UI8: return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
176c2c66affSColin Finck case VT_R4: return VarI4FromR4(V_R4(ps), &V_I4(pd));
177c2c66affSColin Finck case VT_R8: return VarI4FromR8(V_R8(ps), &V_I4(pd));
178c2c66affSColin Finck case VT_DATE: return VarI4FromDate(V_DATE(ps), &V_I4(pd));
179c2c66affSColin Finck case VT_BOOL: return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
180c2c66affSColin Finck case VT_CY: return VarI4FromCy(V_CY(ps), &V_I4(pd));
181c2c66affSColin Finck case VT_DECIMAL: return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
182c2c66affSColin Finck case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
183c2c66affSColin Finck case VT_BSTR: return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
184c2c66affSColin Finck }
185c2c66affSColin Finck break;
186c2c66affSColin Finck
187c2c66affSColin Finck case VT_UI1:
188c2c66affSColin Finck switch (vtFrom)
189c2c66affSColin Finck {
190c2c66affSColin Finck case VT_EMPTY: V_UI1(pd) = 0; return S_OK;
191c2c66affSColin Finck case VT_I1: V_UI1(pd) = V_I1(ps); return S_OK;
192c2c66affSColin Finck case VT_I2: return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
193c2c66affSColin Finck case VT_I4: return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
194c2c66affSColin Finck case VT_UI2: return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
195c2c66affSColin Finck case VT_UI4: return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
196c2c66affSColin Finck case VT_I8: return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
197c2c66affSColin Finck case VT_UI8: return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
198c2c66affSColin Finck case VT_R4: return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
199c2c66affSColin Finck case VT_R8: return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
200c2c66affSColin Finck case VT_DATE: return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
201c2c66affSColin Finck case VT_BOOL: return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
202c2c66affSColin Finck case VT_CY: return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
203c2c66affSColin Finck case VT_DECIMAL: return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
204c2c66affSColin Finck case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
205c2c66affSColin Finck case VT_BSTR: return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
206c2c66affSColin Finck }
207c2c66affSColin Finck break;
208c2c66affSColin Finck
209c2c66affSColin Finck case VT_UI2:
210c2c66affSColin Finck switch (vtFrom)
211c2c66affSColin Finck {
212c2c66affSColin Finck case VT_EMPTY: V_UI2(pd) = 0; return S_OK;
213c2c66affSColin Finck case VT_I1: return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
214c2c66affSColin Finck case VT_I2: V_UI2(pd) = V_I2(ps); return S_OK;
215c2c66affSColin Finck case VT_I4: return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
216c2c66affSColin Finck case VT_UI1: return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
217c2c66affSColin Finck case VT_UI4: return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
218c2c66affSColin Finck case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
219c2c66affSColin Finck case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
220c2c66affSColin Finck case VT_R4: return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
221c2c66affSColin Finck case VT_R8: return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
222c2c66affSColin Finck case VT_DATE: return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
223c2c66affSColin Finck case VT_BOOL: return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
224c2c66affSColin Finck case VT_CY: return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
225c2c66affSColin Finck case VT_DECIMAL: return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
226c2c66affSColin Finck case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
227c2c66affSColin Finck case VT_BSTR: return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
228c2c66affSColin Finck }
229c2c66affSColin Finck break;
230c2c66affSColin Finck
231c2c66affSColin Finck case VT_UI4:
232c2c66affSColin Finck switch (vtFrom)
233c2c66affSColin Finck {
234c2c66affSColin Finck case VT_EMPTY: V_UI4(pd) = 0; return S_OK;
235c2c66affSColin Finck case VT_I1: return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
236c2c66affSColin Finck case VT_I2: return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
237c2c66affSColin Finck case VT_I4: V_UI4(pd) = V_I4(ps); return S_OK;
238c2c66affSColin Finck case VT_UI1: return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
239c2c66affSColin Finck case VT_UI2: return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
240c2c66affSColin Finck case VT_I8: return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
241c2c66affSColin Finck case VT_UI8: return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
242c2c66affSColin Finck case VT_R4: return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
243c2c66affSColin Finck case VT_R8: return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
244c2c66affSColin Finck case VT_DATE: return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
245c2c66affSColin Finck case VT_BOOL: return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
246c2c66affSColin Finck case VT_CY: return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
247c2c66affSColin Finck case VT_DECIMAL: return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
248c2c66affSColin Finck case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
249c2c66affSColin Finck case VT_BSTR: return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
250c2c66affSColin Finck }
251c2c66affSColin Finck break;
252c2c66affSColin Finck
253c2c66affSColin Finck case VT_UI8:
254c2c66affSColin Finck switch (vtFrom)
255c2c66affSColin Finck {
256c2c66affSColin Finck case VT_EMPTY: V_UI8(pd) = 0; return S_OK;
257c2c66affSColin Finck case VT_I4: if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
258c2c66affSColin Finck case VT_I1: return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
259c2c66affSColin Finck case VT_I2: return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
260c2c66affSColin Finck case VT_UI1: return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
261c2c66affSColin Finck case VT_UI2: return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
262c2c66affSColin Finck case VT_UI4: return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
263c2c66affSColin Finck case VT_I8: V_UI8(pd) = V_I8(ps); return S_OK;
264c2c66affSColin Finck case VT_R4: return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
265c2c66affSColin Finck case VT_R8: return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
266c2c66affSColin Finck case VT_DATE: return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
267c2c66affSColin Finck case VT_BOOL: return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
268c2c66affSColin Finck case VT_CY: return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
269c2c66affSColin Finck case VT_DECIMAL: return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
270c2c66affSColin Finck case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
271c2c66affSColin Finck case VT_BSTR: return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
272c2c66affSColin Finck }
273c2c66affSColin Finck break;
274c2c66affSColin Finck
275c2c66affSColin Finck case VT_I8:
276c2c66affSColin Finck switch (vtFrom)
277c2c66affSColin Finck {
278c2c66affSColin Finck case VT_EMPTY: V_I8(pd) = 0; return S_OK;
279c2c66affSColin Finck case VT_I4: V_I8(pd) = V_I4(ps); return S_OK;
280c2c66affSColin Finck case VT_I1: return VarI8FromI1(V_I1(ps), &V_I8(pd));
281c2c66affSColin Finck case VT_I2: return VarI8FromI2(V_I2(ps), &V_I8(pd));
282c2c66affSColin Finck case VT_UI1: return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
283c2c66affSColin Finck case VT_UI2: return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
284c2c66affSColin Finck case VT_UI4: return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
285c2c66affSColin Finck case VT_UI8: V_I8(pd) = V_UI8(ps); return S_OK;
286c2c66affSColin Finck case VT_R4: return VarI8FromR4(V_R4(ps), &V_I8(pd));
287c2c66affSColin Finck case VT_R8: return VarI8FromR8(V_R8(ps), &V_I8(pd));
288c2c66affSColin Finck case VT_DATE: return VarI8FromDate(V_DATE(ps), &V_I8(pd));
289c2c66affSColin Finck case VT_BOOL: return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
290c2c66affSColin Finck case VT_CY: return VarI8FromCy(V_CY(ps), &V_I8(pd));
291c2c66affSColin Finck case VT_DECIMAL: return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
292c2c66affSColin Finck case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
293c2c66affSColin Finck case VT_BSTR: return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
294c2c66affSColin Finck }
295c2c66affSColin Finck break;
296c2c66affSColin Finck
297c2c66affSColin Finck case VT_R4:
298c2c66affSColin Finck switch (vtFrom)
299c2c66affSColin Finck {
300c2c66affSColin Finck case VT_EMPTY: V_R4(pd) = 0.0f; return S_OK;
301c2c66affSColin Finck case VT_I1: return VarR4FromI1(V_I1(ps), &V_R4(pd));
302c2c66affSColin Finck case VT_I2: return VarR4FromI2(V_I2(ps), &V_R4(pd));
303c2c66affSColin Finck case VT_I4: return VarR4FromI4(V_I4(ps), &V_R4(pd));
304c2c66affSColin Finck case VT_UI1: return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
305c2c66affSColin Finck case VT_UI2: return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
306c2c66affSColin Finck case VT_UI4: return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
307c2c66affSColin Finck case VT_I8: return VarR4FromI8(V_I8(ps), &V_R4(pd));
308c2c66affSColin Finck case VT_UI8: return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
309c2c66affSColin Finck case VT_R8: return VarR4FromR8(V_R8(ps), &V_R4(pd));
310c2c66affSColin Finck case VT_DATE: return VarR4FromDate(V_DATE(ps), &V_R4(pd));
311c2c66affSColin Finck case VT_BOOL: return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
312c2c66affSColin Finck case VT_CY: return VarR4FromCy(V_CY(ps), &V_R4(pd));
313c2c66affSColin Finck case VT_DECIMAL: return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
314c2c66affSColin Finck case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
315c2c66affSColin Finck case VT_BSTR: return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
316c2c66affSColin Finck }
317c2c66affSColin Finck break;
318c2c66affSColin Finck
319c2c66affSColin Finck case VT_R8:
320c2c66affSColin Finck switch (vtFrom)
321c2c66affSColin Finck {
322c2c66affSColin Finck case VT_EMPTY: V_R8(pd) = 0.0; return S_OK;
323c2c66affSColin Finck case VT_I1: return VarR8FromI1(V_I1(ps), &V_R8(pd));
324c2c66affSColin Finck case VT_I2: return VarR8FromI2(V_I2(ps), &V_R8(pd));
325c2c66affSColin Finck case VT_I4: return VarR8FromI4(V_I4(ps), &V_R8(pd));
326c2c66affSColin Finck case VT_UI1: return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
327c2c66affSColin Finck case VT_UI2: return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
328c2c66affSColin Finck case VT_UI4: return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
329c2c66affSColin Finck case VT_I8: return VarR8FromI8(V_I8(ps), &V_R8(pd));
330c2c66affSColin Finck case VT_UI8: return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
331c2c66affSColin Finck case VT_R4: return VarR8FromR4(V_R4(ps), &V_R8(pd));
332c2c66affSColin Finck case VT_DATE: return VarR8FromDate(V_DATE(ps), &V_R8(pd));
333c2c66affSColin Finck case VT_BOOL: return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
334c2c66affSColin Finck case VT_CY: return VarR8FromCy(V_CY(ps), &V_R8(pd));
335c2c66affSColin Finck case VT_DECIMAL: return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
336c2c66affSColin Finck case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
337c2c66affSColin Finck case VT_BSTR: return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
338c2c66affSColin Finck }
339c2c66affSColin Finck break;
340c2c66affSColin Finck
341c2c66affSColin Finck case VT_DATE:
342c2c66affSColin Finck switch (vtFrom)
343c2c66affSColin Finck {
344c2c66affSColin Finck case VT_EMPTY: V_DATE(pd) = 0.0; return S_OK;
345c2c66affSColin Finck case VT_I1: return VarDateFromI1(V_I1(ps), &V_DATE(pd));
346c2c66affSColin Finck case VT_I2: return VarDateFromI2(V_I2(ps), &V_DATE(pd));
347c2c66affSColin Finck case VT_I4: return VarDateFromI4(V_I4(ps), &V_DATE(pd));
348c2c66affSColin Finck case VT_UI1: return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
349c2c66affSColin Finck case VT_UI2: return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
350c2c66affSColin Finck case VT_UI4: return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
351c2c66affSColin Finck case VT_I8: return VarDateFromI8(V_I8(ps), &V_DATE(pd));
352c2c66affSColin Finck case VT_UI8: return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
353c2c66affSColin Finck case VT_R4: return VarDateFromR4(V_R4(ps), &V_DATE(pd));
354c2c66affSColin Finck case VT_R8: return VarDateFromR8(V_R8(ps), &V_DATE(pd));
355c2c66affSColin Finck case VT_BOOL: return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
356c2c66affSColin Finck case VT_CY: return VarDateFromCy(V_CY(ps), &V_DATE(pd));
357c2c66affSColin Finck case VT_DECIMAL: return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
358c2c66affSColin Finck case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
359c2c66affSColin Finck case VT_BSTR: return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
360c2c66affSColin Finck }
361c2c66affSColin Finck break;
362c2c66affSColin Finck
363c2c66affSColin Finck case VT_BOOL:
364c2c66affSColin Finck switch (vtFrom)
365c2c66affSColin Finck {
366c2c66affSColin Finck case VT_EMPTY: V_BOOL(pd) = 0; return S_OK;
367c2c66affSColin Finck case VT_I1: return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
368c2c66affSColin Finck case VT_I2: return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
369c2c66affSColin Finck case VT_I4: return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
370c2c66affSColin Finck case VT_UI1: return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
371c2c66affSColin Finck case VT_UI2: return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
372c2c66affSColin Finck case VT_UI4: return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
373c2c66affSColin Finck case VT_I8: return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
374c2c66affSColin Finck case VT_UI8: return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
375c2c66affSColin Finck case VT_R4: return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
376c2c66affSColin Finck case VT_R8: return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
377c2c66affSColin Finck case VT_DATE: return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
378c2c66affSColin Finck case VT_CY: return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
379c2c66affSColin Finck case VT_DECIMAL: return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
380c2c66affSColin Finck case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
381c2c66affSColin Finck case VT_BSTR: return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
382c2c66affSColin Finck }
383c2c66affSColin Finck break;
384c2c66affSColin Finck
385c2c66affSColin Finck case VT_BSTR:
386c2c66affSColin Finck switch (vtFrom)
387c2c66affSColin Finck {
388c2c66affSColin Finck case VT_EMPTY:
389c2c66affSColin Finck V_BSTR(pd) = SysAllocStringLen(NULL, 0);
390c2c66affSColin Finck return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
391c2c66affSColin Finck case VT_BOOL:
392c2c66affSColin Finck if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
393c2c66affSColin Finck return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
394c2c66affSColin Finck return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
395c2c66affSColin Finck case VT_I1: return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
396c2c66affSColin Finck case VT_I2: return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
397c2c66affSColin Finck case VT_I4: return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
398c2c66affSColin Finck case VT_UI1: return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
399c2c66affSColin Finck case VT_UI2: return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
400c2c66affSColin Finck case VT_UI4: return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
401c2c66affSColin Finck case VT_I8: return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
402c2c66affSColin Finck case VT_UI8: return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
403c2c66affSColin Finck case VT_R4: return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
404c2c66affSColin Finck case VT_R8: return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
405c2c66affSColin Finck case VT_DATE: return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
406c2c66affSColin Finck case VT_CY: return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
407c2c66affSColin Finck case VT_DECIMAL: return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
408c2c66affSColin Finck case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd));
409c2c66affSColin Finck }
410c2c66affSColin Finck break;
411c2c66affSColin Finck
412c2c66affSColin Finck case VT_CY:
413c2c66affSColin Finck switch (vtFrom)
414c2c66affSColin Finck {
415c2c66affSColin Finck case VT_EMPTY: V_CY(pd).int64 = 0; return S_OK;
416c2c66affSColin Finck case VT_I1: return VarCyFromI1(V_I1(ps), &V_CY(pd));
417c2c66affSColin Finck case VT_I2: return VarCyFromI2(V_I2(ps), &V_CY(pd));
418c2c66affSColin Finck case VT_I4: return VarCyFromI4(V_I4(ps), &V_CY(pd));
419c2c66affSColin Finck case VT_UI1: return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
420c2c66affSColin Finck case VT_UI2: return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
421c2c66affSColin Finck case VT_UI4: return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
422c2c66affSColin Finck case VT_I8: return VarCyFromI8(V_I8(ps), &V_CY(pd));
423c2c66affSColin Finck case VT_UI8: return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
424c2c66affSColin Finck case VT_R4: return VarCyFromR4(V_R4(ps), &V_CY(pd));
425c2c66affSColin Finck case VT_R8: return VarCyFromR8(V_R8(ps), &V_CY(pd));
426c2c66affSColin Finck case VT_DATE: return VarCyFromDate(V_DATE(ps), &V_CY(pd));
427c2c66affSColin Finck case VT_BOOL: return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
428c2c66affSColin Finck case VT_DECIMAL: return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
429c2c66affSColin Finck case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
430c2c66affSColin Finck case VT_BSTR: return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
431c2c66affSColin Finck }
432c2c66affSColin Finck break;
433c2c66affSColin Finck
434c2c66affSColin Finck case VT_DECIMAL:
435c2c66affSColin Finck switch (vtFrom)
436c2c66affSColin Finck {
437c2c66affSColin Finck case VT_EMPTY:
438c2c66affSColin Finck case VT_BOOL:
439c2c66affSColin Finck DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
440c2c66affSColin Finck DEC_HI32(&V_DECIMAL(pd)) = 0;
441c2c66affSColin Finck DEC_MID32(&V_DECIMAL(pd)) = 0;
442c2c66affSColin Finck /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
443c2c66affSColin Finck * VT_NULL and VT_EMPTY always give a 0 value.
444c2c66affSColin Finck */
445c2c66affSColin Finck DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
446c2c66affSColin Finck return S_OK;
447c2c66affSColin Finck case VT_I1: return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
448c2c66affSColin Finck case VT_I2: return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
449c2c66affSColin Finck case VT_I4: return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
450c2c66affSColin Finck case VT_UI1: return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
451c2c66affSColin Finck case VT_UI2: return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
452c2c66affSColin Finck case VT_UI4: return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
453c2c66affSColin Finck case VT_I8: return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
454c2c66affSColin Finck case VT_UI8: return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
455c2c66affSColin Finck case VT_R4: return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
456c2c66affSColin Finck case VT_R8: return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
457c2c66affSColin Finck case VT_DATE: return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
458c2c66affSColin Finck case VT_CY: return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
459c2c66affSColin Finck case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
460c2c66affSColin Finck case VT_BSTR: return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
461c2c66affSColin Finck }
462c2c66affSColin Finck break;
463c2c66affSColin Finck
464c2c66affSColin Finck case VT_UNKNOWN:
465c2c66affSColin Finck switch (vtFrom)
466c2c66affSColin Finck {
467c2c66affSColin Finck case VT_DISPATCH:
468c2c66affSColin Finck if (V_DISPATCH(ps) == NULL)
469c2c66affSColin Finck {
470c2c66affSColin Finck V_UNKNOWN(pd) = NULL;
471c2c66affSColin Finck res = S_OK;
472c2c66affSColin Finck }
473c2c66affSColin Finck else
474c2c66affSColin Finck res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
475c2c66affSColin Finck break;
476c2c66affSColin Finck }
477c2c66affSColin Finck break;
478c2c66affSColin Finck
479c2c66affSColin Finck case VT_DISPATCH:
480c2c66affSColin Finck switch (vtFrom)
481c2c66affSColin Finck {
482c2c66affSColin Finck case VT_UNKNOWN:
483c2c66affSColin Finck if (V_UNKNOWN(ps) == NULL)
484c2c66affSColin Finck {
485c2c66affSColin Finck V_DISPATCH(pd) = NULL;
486c2c66affSColin Finck res = S_OK;
487c2c66affSColin Finck }
488c2c66affSColin Finck else
489c2c66affSColin Finck res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
490c2c66affSColin Finck break;
491c2c66affSColin Finck }
492c2c66affSColin Finck break;
493c2c66affSColin Finck
494c2c66affSColin Finck case VT_RECORD:
495c2c66affSColin Finck break;
496c2c66affSColin Finck }
497c2c66affSColin Finck return res;
498c2c66affSColin Finck }
499c2c66affSColin Finck
500c2c66affSColin Finck /* Coerce to/from an array */
VARIANT_CoerceArray(VARIANTARG * pd,VARIANTARG * ps,VARTYPE vt)501c2c66affSColin Finck static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
502c2c66affSColin Finck {
503c2c66affSColin Finck if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
504c2c66affSColin Finck return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));
505c2c66affSColin Finck
506c2c66affSColin Finck if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
507c2c66affSColin Finck return VectorFromBstr(V_BSTR(ps), &V_ARRAY(pd));
508c2c66affSColin Finck
509c2c66affSColin Finck if (V_VT(ps) == vt)
510c2c66affSColin Finck return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));
511c2c66affSColin Finck
512c2c66affSColin Finck return DISP_E_TYPEMISMATCH;
513c2c66affSColin Finck }
514c2c66affSColin Finck
VARIANT_FetchDispatchValue(LPVARIANT pvDispatch,LPVARIANT pValue)515c2c66affSColin Finck static HRESULT VARIANT_FetchDispatchValue(LPVARIANT pvDispatch, LPVARIANT pValue)
516c2c66affSColin Finck {
517c2c66affSColin Finck HRESULT hres;
518c2c66affSColin Finck static DISPPARAMS emptyParams = { NULL, NULL, 0, 0 };
519c2c66affSColin Finck
520c2c66affSColin Finck if ((V_VT(pvDispatch) & VT_TYPEMASK) == VT_DISPATCH) {
521c2c66affSColin Finck if (NULL == V_DISPATCH(pvDispatch)) return DISP_E_TYPEMISMATCH;
522c2c66affSColin Finck hres = IDispatch_Invoke(V_DISPATCH(pvDispatch), DISPID_VALUE, &IID_NULL,
523c2c66affSColin Finck LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &emptyParams, pValue,
524c2c66affSColin Finck NULL, NULL);
525c2c66affSColin Finck } else {
526c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
527c2c66affSColin Finck }
528c2c66affSColin Finck return hres;
529c2c66affSColin Finck }
530c2c66affSColin Finck
531c2c66affSColin Finck /******************************************************************************
532c2c66affSColin Finck * Check if a variants type is valid.
533c2c66affSColin Finck */
VARIANT_ValidateType(VARTYPE vt)534c2c66affSColin Finck static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
535c2c66affSColin Finck {
536c2c66affSColin Finck VARTYPE vtExtra = vt & VT_EXTRA_TYPE;
537c2c66affSColin Finck
538c2c66affSColin Finck vt &= VT_TYPEMASK;
539c2c66affSColin Finck
540c2c66affSColin Finck if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
541c2c66affSColin Finck {
542c2c66affSColin Finck if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
543c2c66affSColin Finck {
544c2c66affSColin Finck if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
545c2c66affSColin Finck return DISP_E_BADVARTYPE;
546c2c66affSColin Finck if (vt != (VARTYPE)15)
547c2c66affSColin Finck return S_OK;
548c2c66affSColin Finck }
549c2c66affSColin Finck }
550c2c66affSColin Finck return DISP_E_BADVARTYPE;
551c2c66affSColin Finck }
552c2c66affSColin Finck
553c2c66affSColin Finck /******************************************************************************
554c2c66affSColin Finck * VariantInit [OLEAUT32.8]
555c2c66affSColin Finck *
556c2c66affSColin Finck * Initialise a variant.
557c2c66affSColin Finck *
558c2c66affSColin Finck * PARAMS
559c2c66affSColin Finck * pVarg [O] Variant to initialise
560c2c66affSColin Finck *
561c2c66affSColin Finck * RETURNS
562c2c66affSColin Finck * Nothing.
563c2c66affSColin Finck *
564c2c66affSColin Finck * NOTES
565c2c66affSColin Finck * This function simply sets the type of the variant to VT_EMPTY. It does not
566c2c66affSColin Finck * free any existing value, use VariantClear() for that.
567c2c66affSColin Finck */
VariantInit(VARIANTARG * pVarg)568c2c66affSColin Finck void WINAPI VariantInit(VARIANTARG* pVarg)
569c2c66affSColin Finck {
570c2c66affSColin Finck TRACE("(%p)\n", pVarg);
571c2c66affSColin Finck
572c2c66affSColin Finck /* Win8.1 zeroes whole struct. Previous implementations don't set any other fields. */
573c2c66affSColin Finck V_VT(pVarg) = VT_EMPTY;
574c2c66affSColin Finck }
575c2c66affSColin Finck
VARIANT_ClearInd(VARIANTARG * pVarg)576c2c66affSColin Finck HRESULT VARIANT_ClearInd(VARIANTARG *pVarg)
577c2c66affSColin Finck {
578c2c66affSColin Finck HRESULT hres;
579c2c66affSColin Finck
580c2c66affSColin Finck TRACE("(%s)\n", debugstr_variant(pVarg));
581c2c66affSColin Finck
582c2c66affSColin Finck hres = VARIANT_ValidateType(V_VT(pVarg));
583c2c66affSColin Finck if (FAILED(hres))
584c2c66affSColin Finck return hres;
585c2c66affSColin Finck
586c2c66affSColin Finck switch (V_VT(pVarg))
587c2c66affSColin Finck {
588c2c66affSColin Finck case VT_DISPATCH:
589c2c66affSColin Finck case VT_UNKNOWN:
590c2c66affSColin Finck if (V_UNKNOWN(pVarg))
591c2c66affSColin Finck IUnknown_Release(V_UNKNOWN(pVarg));
592c2c66affSColin Finck break;
593c2c66affSColin Finck case VT_UNKNOWN | VT_BYREF:
594c2c66affSColin Finck case VT_DISPATCH | VT_BYREF:
595c2c66affSColin Finck if(*V_UNKNOWNREF(pVarg))
596c2c66affSColin Finck IUnknown_Release(*V_UNKNOWNREF(pVarg));
597c2c66affSColin Finck break;
598c2c66affSColin Finck case VT_BSTR:
599c2c66affSColin Finck SysFreeString(V_BSTR(pVarg));
600c2c66affSColin Finck break;
601c2c66affSColin Finck case VT_BSTR | VT_BYREF:
602c2c66affSColin Finck SysFreeString(*V_BSTRREF(pVarg));
603c2c66affSColin Finck break;
604c2c66affSColin Finck case VT_VARIANT | VT_BYREF:
605c2c66affSColin Finck VariantClear(V_VARIANTREF(pVarg));
606c2c66affSColin Finck break;
607c2c66affSColin Finck case VT_RECORD:
608c2c66affSColin Finck case VT_RECORD | VT_BYREF:
609c2c66affSColin Finck {
610c2c66affSColin Finck struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
611c2c66affSColin Finck if (pBr->pRecInfo)
612c2c66affSColin Finck {
613c2c66affSColin Finck IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
614c2c66affSColin Finck IRecordInfo_Release(pBr->pRecInfo);
615c2c66affSColin Finck }
616c2c66affSColin Finck break;
617c2c66affSColin Finck }
618c2c66affSColin Finck default:
619c2c66affSColin Finck if (V_ISARRAY(pVarg) || (V_VT(pVarg) & ~VT_BYREF) == VT_SAFEARRAY)
620c2c66affSColin Finck {
621c2c66affSColin Finck if (V_ISBYREF(pVarg))
622c2c66affSColin Finck {
623c2c66affSColin Finck if (*V_ARRAYREF(pVarg))
624c2c66affSColin Finck hres = SafeArrayDestroy(*V_ARRAYREF(pVarg));
625c2c66affSColin Finck }
626c2c66affSColin Finck else if (V_ARRAY(pVarg))
627c2c66affSColin Finck hres = SafeArrayDestroy(V_ARRAY(pVarg));
628c2c66affSColin Finck }
629c2c66affSColin Finck break;
630c2c66affSColin Finck }
631c2c66affSColin Finck
632c2c66affSColin Finck V_VT(pVarg) = VT_EMPTY;
633c2c66affSColin Finck return hres;
634c2c66affSColin Finck }
635c2c66affSColin Finck
636c2c66affSColin Finck /******************************************************************************
637c2c66affSColin Finck * VariantClear [OLEAUT32.9]
638c2c66affSColin Finck *
639c2c66affSColin Finck * Clear a variant.
640c2c66affSColin Finck *
641c2c66affSColin Finck * PARAMS
642c2c66affSColin Finck * pVarg [I/O] Variant to clear
643c2c66affSColin Finck *
644c2c66affSColin Finck * RETURNS
645c2c66affSColin Finck * Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
646c2c66affSColin Finck * Failure: DISP_E_BADVARTYPE, if the variant is not a valid variant type.
647c2c66affSColin Finck */
VariantClear(VARIANTARG * pVarg)6485db885caSAmine Khaldi HRESULT WINAPI DECLSPEC_HOTPATCH VariantClear(VARIANTARG* pVarg)
649c2c66affSColin Finck {
650c2c66affSColin Finck HRESULT hres;
651c2c66affSColin Finck
652c2c66affSColin Finck TRACE("(%s)\n", debugstr_variant(pVarg));
653c2c66affSColin Finck
654c2c66affSColin Finck hres = VARIANT_ValidateType(V_VT(pVarg));
655c2c66affSColin Finck
656c2c66affSColin Finck if (SUCCEEDED(hres))
657c2c66affSColin Finck {
658c2c66affSColin Finck if (!V_ISBYREF(pVarg))
659c2c66affSColin Finck {
660c2c66affSColin Finck if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
661c2c66affSColin Finck {
662c2c66affSColin Finck hres = SafeArrayDestroy(V_ARRAY(pVarg));
663c2c66affSColin Finck }
664c2c66affSColin Finck else if (V_VT(pVarg) == VT_BSTR)
665c2c66affSColin Finck {
666c2c66affSColin Finck SysFreeString(V_BSTR(pVarg));
667c2c66affSColin Finck }
668c2c66affSColin Finck else if (V_VT(pVarg) == VT_RECORD)
669c2c66affSColin Finck {
670c2c66affSColin Finck struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
671c2c66affSColin Finck if (pBr->pRecInfo)
672c2c66affSColin Finck {
673c2c66affSColin Finck IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
674c2c66affSColin Finck IRecordInfo_Release(pBr->pRecInfo);
675c2c66affSColin Finck }
676c2c66affSColin Finck }
677c2c66affSColin Finck else if (V_VT(pVarg) == VT_DISPATCH ||
678c2c66affSColin Finck V_VT(pVarg) == VT_UNKNOWN)
679c2c66affSColin Finck {
680c2c66affSColin Finck if (V_UNKNOWN(pVarg))
681c2c66affSColin Finck IUnknown_Release(V_UNKNOWN(pVarg));
682c2c66affSColin Finck }
683c2c66affSColin Finck }
684c2c66affSColin Finck V_VT(pVarg) = VT_EMPTY;
685c2c66affSColin Finck }
686c2c66affSColin Finck return hres;
687c2c66affSColin Finck }
688c2c66affSColin Finck
689c2c66affSColin Finck /******************************************************************************
690c2c66affSColin Finck * Copy an IRecordInfo object contained in a variant.
691c2c66affSColin Finck */
VARIANT_CopyIRecordInfo(VARIANT * dest,VARIANT * src)692c2c66affSColin Finck static HRESULT VARIANT_CopyIRecordInfo(VARIANT *dest, VARIANT *src)
693c2c66affSColin Finck {
694c2c66affSColin Finck struct __tagBRECORD *dest_rec = &V_UNION(dest, brecVal);
695c2c66affSColin Finck struct __tagBRECORD *src_rec = &V_UNION(src, brecVal);
696c2c66affSColin Finck HRESULT hr = S_OK;
697c2c66affSColin Finck ULONG size;
698c2c66affSColin Finck
699c2c66affSColin Finck if (!src_rec->pRecInfo)
700c2c66affSColin Finck {
701c2c66affSColin Finck if (src_rec->pvRecord) return E_INVALIDARG;
702c2c66affSColin Finck return S_OK;
703c2c66affSColin Finck }
704c2c66affSColin Finck
705c2c66affSColin Finck hr = IRecordInfo_GetSize(src_rec->pRecInfo, &size);
706c2c66affSColin Finck if (FAILED(hr)) return hr;
707c2c66affSColin Finck
708c2c66affSColin Finck /* This could look cleaner if only RecordCreate() was used, but native doesn't use it.
709c2c66affSColin Finck Memory should be allocated in a same way as RecordCreate() does, so RecordDestroy()
710c2c66affSColin Finck could free it later. */
711c2c66affSColin Finck dest_rec->pvRecord = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
712c2c66affSColin Finck if (!dest_rec->pvRecord) return E_OUTOFMEMORY;
713c2c66affSColin Finck
714c2c66affSColin Finck dest_rec->pRecInfo = src_rec->pRecInfo;
715c2c66affSColin Finck IRecordInfo_AddRef(src_rec->pRecInfo);
716c2c66affSColin Finck
717c2c66affSColin Finck return IRecordInfo_RecordCopy(src_rec->pRecInfo, src_rec->pvRecord, dest_rec->pvRecord);
718c2c66affSColin Finck }
719c2c66affSColin Finck
720c2c66affSColin Finck /******************************************************************************
721c2c66affSColin Finck * VariantCopy [OLEAUT32.10]
722c2c66affSColin Finck *
723c2c66affSColin Finck * Copy a variant.
724c2c66affSColin Finck *
725c2c66affSColin Finck * PARAMS
726c2c66affSColin Finck * pvargDest [O] Destination for copy
727c2c66affSColin Finck * pvargSrc [I] Source variant to copy
728c2c66affSColin Finck *
729c2c66affSColin Finck * RETURNS
730c2c66affSColin Finck * Success: S_OK. pvargDest contains a copy of pvargSrc.
731c2c66affSColin Finck * Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
732c2c66affSColin Finck * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
733c2c66affSColin Finck * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
734c2c66affSColin Finck * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
735c2c66affSColin Finck *
736c2c66affSColin Finck * NOTES
737c2c66affSColin Finck * - If pvargSrc == pvargDest, this function does nothing, and succeeds if
738c2c66affSColin Finck * pvargSrc is valid. Otherwise, pvargDest is always cleared using
739c2c66affSColin Finck * VariantClear() before pvargSrc is copied to it. If clearing pvargDest
740c2c66affSColin Finck * fails, so does this function.
741c2c66affSColin Finck * - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
742c2c66affSColin Finck * - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
743c2c66affSColin Finck * is copied rather than just any pointers to it.
744c2c66affSColin Finck * - For by-value object types the object pointer is copied and the objects
745c2c66affSColin Finck * reference count increased using IUnknown_AddRef().
746c2c66affSColin Finck * - For all by-reference types, only the referencing pointer is copied.
747c2c66affSColin Finck */
VariantCopy(VARIANTARG * pvargDest,VARIANTARG * pvargSrc)748c2c66affSColin Finck HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
749c2c66affSColin Finck {
750c2c66affSColin Finck HRESULT hres = S_OK;
751c2c66affSColin Finck
752c2c66affSColin Finck TRACE("(%s,%s)\n", debugstr_variant(pvargDest), debugstr_variant(pvargSrc));
753c2c66affSColin Finck
754c2c66affSColin Finck if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
755c2c66affSColin Finck FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
756c2c66affSColin Finck return DISP_E_BADVARTYPE;
757c2c66affSColin Finck
758c2c66affSColin Finck if (pvargSrc != pvargDest &&
759c2c66affSColin Finck SUCCEEDED(hres = VariantClear(pvargDest)))
760c2c66affSColin Finck {
761c2c66affSColin Finck *pvargDest = *pvargSrc; /* Shallow copy the value */
762c2c66affSColin Finck
763c2c66affSColin Finck if (!V_ISBYREF(pvargSrc))
764c2c66affSColin Finck {
765c2c66affSColin Finck switch (V_VT(pvargSrc))
766c2c66affSColin Finck {
767c2c66affSColin Finck case VT_BSTR:
768c2c66affSColin Finck V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
769c2c66affSColin Finck if (!V_BSTR(pvargDest))
770c2c66affSColin Finck hres = E_OUTOFMEMORY;
771c2c66affSColin Finck break;
772c2c66affSColin Finck case VT_RECORD:
773c2c66affSColin Finck hres = VARIANT_CopyIRecordInfo(pvargDest, pvargSrc);
774c2c66affSColin Finck break;
775c2c66affSColin Finck case VT_DISPATCH:
776c2c66affSColin Finck case VT_UNKNOWN:
777c2c66affSColin Finck V_UNKNOWN(pvargDest) = V_UNKNOWN(pvargSrc);
778c2c66affSColin Finck if (V_UNKNOWN(pvargSrc))
779c2c66affSColin Finck IUnknown_AddRef(V_UNKNOWN(pvargSrc));
780c2c66affSColin Finck break;
781c2c66affSColin Finck default:
782c2c66affSColin Finck if (V_ISARRAY(pvargSrc))
783c2c66affSColin Finck hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
784c2c66affSColin Finck }
785c2c66affSColin Finck }
786c2c66affSColin Finck }
787c2c66affSColin Finck return hres;
788c2c66affSColin Finck }
789c2c66affSColin Finck
790c2c66affSColin Finck /* Return the byte size of a variants data */
VARIANT_DataSize(const VARIANT * pv)791c2c66affSColin Finck static inline size_t VARIANT_DataSize(const VARIANT* pv)
792c2c66affSColin Finck {
793c2c66affSColin Finck switch (V_TYPE(pv))
794c2c66affSColin Finck {
795c2c66affSColin Finck case VT_I1:
796c2c66affSColin Finck case VT_UI1: return sizeof(BYTE);
797c2c66affSColin Finck case VT_I2:
798c2c66affSColin Finck case VT_UI2: return sizeof(SHORT);
799c2c66affSColin Finck case VT_INT:
800c2c66affSColin Finck case VT_UINT:
801c2c66affSColin Finck case VT_I4:
802c2c66affSColin Finck case VT_UI4: return sizeof(LONG);
803c2c66affSColin Finck case VT_I8:
804c2c66affSColin Finck case VT_UI8: return sizeof(LONGLONG);
805c2c66affSColin Finck case VT_R4: return sizeof(float);
806c2c66affSColin Finck case VT_R8: return sizeof(double);
807c2c66affSColin Finck case VT_DATE: return sizeof(DATE);
808c2c66affSColin Finck case VT_BOOL: return sizeof(VARIANT_BOOL);
809c2c66affSColin Finck case VT_DISPATCH:
810c2c66affSColin Finck case VT_UNKNOWN:
811c2c66affSColin Finck case VT_BSTR: return sizeof(void*);
812c2c66affSColin Finck case VT_CY: return sizeof(CY);
813c2c66affSColin Finck case VT_ERROR: return sizeof(SCODE);
814c2c66affSColin Finck }
815c2c66affSColin Finck TRACE("Shouldn't be called for variant %s!\n", debugstr_variant(pv));
816c2c66affSColin Finck return 0;
817c2c66affSColin Finck }
818c2c66affSColin Finck
819c2c66affSColin Finck /******************************************************************************
820c2c66affSColin Finck * VariantCopyInd [OLEAUT32.11]
821c2c66affSColin Finck *
822c2c66affSColin Finck * Copy a variant, dereferencing it if it is by-reference.
823c2c66affSColin Finck *
824c2c66affSColin Finck * PARAMS
825c2c66affSColin Finck * pvargDest [O] Destination for copy
826c2c66affSColin Finck * pvargSrc [I] Source variant to copy
827c2c66affSColin Finck *
828c2c66affSColin Finck * RETURNS
829c2c66affSColin Finck * Success: S_OK. pvargDest contains a copy of pvargSrc.
830c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
831c2c66affSColin Finck *
832c2c66affSColin Finck * NOTES
833c2c66affSColin Finck * Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
834c2c66affSColin Finck * E_INVALIDARG, if pvargSrc is an invalid by-reference type.
835c2c66affSColin Finck * E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
836c2c66affSColin Finck * HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
837c2c66affSColin Finck * or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
838c2c66affSColin Finck *
839c2c66affSColin Finck * NOTES
840c2c66affSColin Finck * - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
841c2c66affSColin Finck * - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
842c2c66affSColin Finck * value.
843c2c66affSColin Finck * - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
844c2c66affSColin Finck * pvargDest is always cleared using VariantClear() before pvargSrc is copied
845c2c66affSColin Finck * to it. If clearing pvargDest fails, so does this function.
846c2c66affSColin Finck */
VariantCopyInd(VARIANT * pvargDest,VARIANTARG * pvargSrc)847c2c66affSColin Finck HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
848c2c66affSColin Finck {
849c2c66affSColin Finck VARIANTARG vTmp, *pSrc = pvargSrc;
850c2c66affSColin Finck VARTYPE vt;
851c2c66affSColin Finck HRESULT hres = S_OK;
852c2c66affSColin Finck
853c2c66affSColin Finck TRACE("(%s,%s)\n", debugstr_variant(pvargDest), debugstr_variant(pvargSrc));
854c2c66affSColin Finck
855c2c66affSColin Finck if (!V_ISBYREF(pvargSrc))
856c2c66affSColin Finck return VariantCopy(pvargDest, pvargSrc);
857c2c66affSColin Finck
858c2c66affSColin Finck /* Argument checking is more lax than VariantCopy()... */
859c2c66affSColin Finck vt = V_TYPE(pvargSrc);
860c2c66affSColin Finck if (V_ISARRAY(pvargSrc) || (V_VT(pvargSrc) == (VT_RECORD|VT_BYREF)) ||
861c2c66affSColin Finck (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
862c2c66affSColin Finck !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
863c2c66affSColin Finck {
864c2c66affSColin Finck /* OK */
865c2c66affSColin Finck }
866c2c66affSColin Finck else
867c2c66affSColin Finck return E_INVALIDARG; /* ...And the return value for invalid types differs too */
868c2c66affSColin Finck
869c2c66affSColin Finck if (pvargSrc == pvargDest)
870c2c66affSColin Finck {
871c2c66affSColin Finck /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
872c2c66affSColin Finck * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
873c2c66affSColin Finck */
874c2c66affSColin Finck vTmp = *pvargSrc;
875c2c66affSColin Finck pSrc = &vTmp;
876c2c66affSColin Finck V_VT(pvargDest) = VT_EMPTY;
877c2c66affSColin Finck }
878c2c66affSColin Finck else
879c2c66affSColin Finck {
880c2c66affSColin Finck /* Copy into another variant. Free the variant in pvargDest */
881c2c66affSColin Finck if (FAILED(hres = VariantClear(pvargDest)))
882c2c66affSColin Finck {
883c2c66affSColin Finck TRACE("VariantClear() of destination failed\n");
884c2c66affSColin Finck return hres;
885c2c66affSColin Finck }
886c2c66affSColin Finck }
887c2c66affSColin Finck
888c2c66affSColin Finck if (V_ISARRAY(pSrc))
889c2c66affSColin Finck {
890c2c66affSColin Finck /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
891c2c66affSColin Finck hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
892c2c66affSColin Finck }
893c2c66affSColin Finck else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
894c2c66affSColin Finck {
895c2c66affSColin Finck /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
896c2c66affSColin Finck V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
897c2c66affSColin Finck }
898c2c66affSColin Finck else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
899c2c66affSColin Finck {
900c2c66affSColin Finck hres = VARIANT_CopyIRecordInfo(pvargDest, pvargSrc);
901c2c66affSColin Finck }
902c2c66affSColin Finck else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
903c2c66affSColin Finck V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
904c2c66affSColin Finck {
905c2c66affSColin Finck /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
906c2c66affSColin Finck V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
907c2c66affSColin Finck if (*V_UNKNOWNREF(pSrc))
908c2c66affSColin Finck IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
909c2c66affSColin Finck }
910c2c66affSColin Finck else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
911c2c66affSColin Finck {
912c2c66affSColin Finck /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
913c2c66affSColin Finck if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
914c2c66affSColin Finck hres = E_INVALIDARG; /* Don't dereference more than one level */
915c2c66affSColin Finck else
916c2c66affSColin Finck hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
917c2c66affSColin Finck
918c2c66affSColin Finck /* Use the dereferenced variants type value, not VT_VARIANT */
919c2c66affSColin Finck goto VariantCopyInd_Return;
920c2c66affSColin Finck }
921c2c66affSColin Finck else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
922c2c66affSColin Finck {
923c2c66affSColin Finck memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
924c2c66affSColin Finck sizeof(DECIMAL) - sizeof(USHORT));
925c2c66affSColin Finck }
926c2c66affSColin Finck else
927c2c66affSColin Finck {
928c2c66affSColin Finck /* Copy the pointed to data into this variant */
929c2c66affSColin Finck memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
930c2c66affSColin Finck }
931c2c66affSColin Finck
932c2c66affSColin Finck V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
933c2c66affSColin Finck
934c2c66affSColin Finck VariantCopyInd_Return:
935c2c66affSColin Finck
936c2c66affSColin Finck if (pSrc != pvargSrc)
937c2c66affSColin Finck VariantClear(pSrc);
938c2c66affSColin Finck
939c2c66affSColin Finck TRACE("returning 0x%08x, %s\n", hres, debugstr_variant(pvargDest));
940c2c66affSColin Finck return hres;
941c2c66affSColin Finck }
942c2c66affSColin Finck
943c2c66affSColin Finck /******************************************************************************
944c2c66affSColin Finck * VariantChangeType [OLEAUT32.12]
945c2c66affSColin Finck *
946c2c66affSColin Finck * Change the type of a variant.
947c2c66affSColin Finck *
948c2c66affSColin Finck * PARAMS
949c2c66affSColin Finck * pvargDest [O] Destination for the converted variant
950c2c66affSColin Finck * pvargSrc [O] Source variant to change the type of
951c2c66affSColin Finck * wFlags [I] VARIANT_ flags from "oleauto.h"
952c2c66affSColin Finck * vt [I] Variant type to change pvargSrc into
953c2c66affSColin Finck *
954c2c66affSColin Finck * RETURNS
955c2c66affSColin Finck * Success: S_OK. pvargDest contains the converted value.
956c2c66affSColin Finck * Failure: An HRESULT error code describing the failure.
957c2c66affSColin Finck *
958c2c66affSColin Finck * NOTES
959c2c66affSColin Finck * The LCID used for the conversion is LOCALE_USER_DEFAULT.
960c2c66affSColin Finck * See VariantChangeTypeEx.
961c2c66affSColin Finck */
VariantChangeType(VARIANTARG * pvargDest,VARIANTARG * pvargSrc,USHORT wFlags,VARTYPE vt)9625db885caSAmine Khaldi HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
963c2c66affSColin Finck USHORT wFlags, VARTYPE vt)
964c2c66affSColin Finck {
965c2c66affSColin Finck return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
966c2c66affSColin Finck }
967c2c66affSColin Finck
968c2c66affSColin Finck /******************************************************************************
969c2c66affSColin Finck * VariantChangeTypeEx [OLEAUT32.147]
970c2c66affSColin Finck *
971c2c66affSColin Finck * Change the type of a variant.
972c2c66affSColin Finck *
973c2c66affSColin Finck * PARAMS
974c2c66affSColin Finck * pvargDest [O] Destination for the converted variant
975c2c66affSColin Finck * pvargSrc [O] Source variant to change the type of
976c2c66affSColin Finck * lcid [I] LCID for the conversion
977c2c66affSColin Finck * wFlags [I] VARIANT_ flags from "oleauto.h"
978c2c66affSColin Finck * vt [I] Variant type to change pvargSrc into
979c2c66affSColin Finck *
980c2c66affSColin Finck * RETURNS
981c2c66affSColin Finck * Success: S_OK. pvargDest contains the converted value.
982c2c66affSColin Finck * Failure: An HRESULT error code describing the failure.
983c2c66affSColin Finck *
984c2c66affSColin Finck * NOTES
985c2c66affSColin Finck * pvargDest and pvargSrc can point to the same variant to perform an in-place
986c2c66affSColin Finck * conversion. If the conversion is successful, pvargSrc will be freed.
987c2c66affSColin Finck */
VariantChangeTypeEx(VARIANTARG * pvargDest,VARIANTARG * pvargSrc,LCID lcid,USHORT wFlags,VARTYPE vt)988c2c66affSColin Finck HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
989c2c66affSColin Finck LCID lcid, USHORT wFlags, VARTYPE vt)
990c2c66affSColin Finck {
991c2c66affSColin Finck HRESULT res = S_OK;
992c2c66affSColin Finck
993c2c66affSColin Finck TRACE("(%s,%s,0x%08x,0x%04x,%s)\n", debugstr_variant(pvargDest),
994c2c66affSColin Finck debugstr_variant(pvargSrc), lcid, wFlags, debugstr_vt(vt));
995c2c66affSColin Finck
996c2c66affSColin Finck if (vt == VT_CLSID)
997c2c66affSColin Finck res = DISP_E_BADVARTYPE;
998c2c66affSColin Finck else
999c2c66affSColin Finck {
1000c2c66affSColin Finck res = VARIANT_ValidateType(V_VT(pvargSrc));
1001c2c66affSColin Finck
1002c2c66affSColin Finck if (SUCCEEDED(res))
1003c2c66affSColin Finck {
1004c2c66affSColin Finck res = VARIANT_ValidateType(vt);
1005c2c66affSColin Finck
1006c2c66affSColin Finck if (SUCCEEDED(res))
1007c2c66affSColin Finck {
1008c2c66affSColin Finck VARIANTARG vTmp, vSrcDeref;
1009c2c66affSColin Finck
1010c2c66affSColin Finck if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
1011c2c66affSColin Finck res = DISP_E_TYPEMISMATCH;
1012c2c66affSColin Finck else
1013c2c66affSColin Finck {
1014c2c66affSColin Finck V_VT(&vTmp) = VT_EMPTY;
1015c2c66affSColin Finck V_VT(&vSrcDeref) = VT_EMPTY;
1016c2c66affSColin Finck VariantClear(&vTmp);
1017c2c66affSColin Finck VariantClear(&vSrcDeref);
1018c2c66affSColin Finck }
1019c2c66affSColin Finck
1020c2c66affSColin Finck if (SUCCEEDED(res))
1021c2c66affSColin Finck {
1022c2c66affSColin Finck res = VariantCopyInd(&vSrcDeref, pvargSrc);
1023c2c66affSColin Finck if (SUCCEEDED(res))
1024c2c66affSColin Finck {
1025c2c66affSColin Finck if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
1026c2c66affSColin Finck res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
1027c2c66affSColin Finck else
1028c2c66affSColin Finck res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);
1029c2c66affSColin Finck
1030c2c66affSColin Finck if (SUCCEEDED(res)) {
1031c2c66affSColin Finck V_VT(&vTmp) = vt;
1032c2c66affSColin Finck res = VariantCopy(pvargDest, &vTmp);
1033c2c66affSColin Finck }
1034c2c66affSColin Finck VariantClear(&vTmp);
1035c2c66affSColin Finck VariantClear(&vSrcDeref);
1036c2c66affSColin Finck }
1037c2c66affSColin Finck }
1038c2c66affSColin Finck }
1039c2c66affSColin Finck }
1040c2c66affSColin Finck }
1041c2c66affSColin Finck
1042c2c66affSColin Finck TRACE("returning 0x%08x, %s\n", res, debugstr_variant(pvargDest));
1043c2c66affSColin Finck return res;
1044c2c66affSColin Finck }
1045c2c66affSColin Finck
1046c2c66affSColin Finck /* Date Conversions */
1047c2c66affSColin Finck
1048c2c66affSColin Finck #define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))
1049c2c66affSColin Finck
1050c2c66affSColin Finck /* Convert a VT_DATE value to a Julian Date */
VARIANT_JulianFromDate(int dateIn)1051c2c66affSColin Finck static inline int VARIANT_JulianFromDate(int dateIn)
1052c2c66affSColin Finck {
1053c2c66affSColin Finck int julianDays = dateIn;
1054c2c66affSColin Finck
1055c2c66affSColin Finck julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
1056c2c66affSColin Finck julianDays += 1757585; /* Convert to + days from 23 Nov 4713 BC (Julian) */
1057c2c66affSColin Finck return julianDays;
1058c2c66affSColin Finck }
1059c2c66affSColin Finck
1060c2c66affSColin Finck /* Convert a Julian Date to a VT_DATE value */
VARIANT_DateFromJulian(int dateIn)1061c2c66affSColin Finck static inline int VARIANT_DateFromJulian(int dateIn)
1062c2c66affSColin Finck {
1063c2c66affSColin Finck int julianDays = dateIn;
1064c2c66affSColin Finck
1065c2c66affSColin Finck julianDays -= 1757585; /* Convert to + days from 1 Jan 100 AD */
1066c2c66affSColin Finck julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
1067c2c66affSColin Finck return julianDays;
1068c2c66affSColin Finck }
1069c2c66affSColin Finck
1070c2c66affSColin Finck /* Convert a Julian date to Day/Month/Year - from PostgreSQL */
VARIANT_DMYFromJulian(int jd,USHORT * year,USHORT * month,USHORT * day)1071c2c66affSColin Finck static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
1072c2c66affSColin Finck {
1073c2c66affSColin Finck int j, i, l, n;
1074c2c66affSColin Finck
1075c2c66affSColin Finck l = jd + 68569;
1076c2c66affSColin Finck n = l * 4 / 146097;
1077c2c66affSColin Finck l -= (n * 146097 + 3) / 4;
1078c2c66affSColin Finck i = (4000 * (l + 1)) / 1461001;
1079c2c66affSColin Finck l += 31 - (i * 1461) / 4;
1080c2c66affSColin Finck j = (l * 80) / 2447;
1081c2c66affSColin Finck *day = l - (j * 2447) / 80;
1082c2c66affSColin Finck l = j / 11;
1083c2c66affSColin Finck *month = (j + 2) - (12 * l);
1084c2c66affSColin Finck *year = 100 * (n - 49) + i + l;
1085c2c66affSColin Finck }
1086c2c66affSColin Finck
1087c2c66affSColin Finck /* Convert Day/Month/Year to a Julian date - from PostgreSQL */
VARIANT_JulianFromDMY(USHORT year,USHORT month,USHORT day)1088c2c66affSColin Finck static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
1089c2c66affSColin Finck {
1090c2c66affSColin Finck int m12 = (month - 14) / 12;
1091c2c66affSColin Finck
1092c2c66affSColin Finck return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
1093c2c66affSColin Finck (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
1094c2c66affSColin Finck }
1095c2c66affSColin Finck
1096c2c66affSColin Finck /* Macros for accessing DOS format date/time fields */
1097c2c66affSColin Finck #define DOS_YEAR(x) (1980 + (x >> 9))
1098c2c66affSColin Finck #define DOS_MONTH(x) ((x >> 5) & 0xf)
1099c2c66affSColin Finck #define DOS_DAY(x) (x & 0x1f)
1100c2c66affSColin Finck #define DOS_HOUR(x) (x >> 11)
1101c2c66affSColin Finck #define DOS_MINUTE(x) ((x >> 5) & 0x3f)
1102c2c66affSColin Finck #define DOS_SECOND(x) ((x & 0x1f) << 1)
1103c2c66affSColin Finck /* Create a DOS format date/time */
1104c2c66affSColin Finck #define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
1105c2c66affSColin Finck #define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))
1106c2c66affSColin Finck
1107c2c66affSColin Finck /* Roll a date forwards or backwards to correct it */
VARIANT_RollUdate(UDATE * lpUd)1108c2c66affSColin Finck static HRESULT VARIANT_RollUdate(UDATE *lpUd)
1109c2c66affSColin Finck {
1110c2c66affSColin Finck static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1111c2c66affSColin Finck short iYear, iMonth, iDay, iHour, iMinute, iSecond;
1112c2c66affSColin Finck
1113c2c66affSColin Finck /* interpret values signed */
1114c2c66affSColin Finck iYear = lpUd->st.wYear;
1115c2c66affSColin Finck iMonth = lpUd->st.wMonth;
1116c2c66affSColin Finck iDay = lpUd->st.wDay;
1117c2c66affSColin Finck iHour = lpUd->st.wHour;
1118c2c66affSColin Finck iMinute = lpUd->st.wMinute;
1119c2c66affSColin Finck iSecond = lpUd->st.wSecond;
1120c2c66affSColin Finck
1121c2c66affSColin Finck TRACE("Raw date: %d/%d/%d %d:%d:%d\n", iDay, iMonth,
1122c2c66affSColin Finck iYear, iHour, iMinute, iSecond);
1123c2c66affSColin Finck
1124c2c66affSColin Finck if (iYear > 9999 || iYear < -9999)
1125c2c66affSColin Finck return E_INVALIDARG; /* Invalid value */
1126c2c66affSColin Finck /* Year 0 to 29 are treated as 2000 + year */
1127c2c66affSColin Finck if (iYear >= 0 && iYear < 30)
1128c2c66affSColin Finck iYear += 2000;
1129c2c66affSColin Finck /* Remaining years < 100 are treated as 1900 + year */
1130c2c66affSColin Finck else if (iYear >= 30 && iYear < 100)
1131c2c66affSColin Finck iYear += 1900;
1132c2c66affSColin Finck
1133c2c66affSColin Finck iMinute += iSecond / 60;
1134c2c66affSColin Finck iSecond = iSecond % 60;
1135c2c66affSColin Finck iHour += iMinute / 60;
1136c2c66affSColin Finck iMinute = iMinute % 60;
1137c2c66affSColin Finck iDay += iHour / 24;
1138c2c66affSColin Finck iHour = iHour % 24;
1139c2c66affSColin Finck iYear += iMonth / 12;
1140c2c66affSColin Finck iMonth = iMonth % 12;
1141c2c66affSColin Finck if (iMonth<=0) {iMonth+=12; iYear--;}
1142c2c66affSColin Finck while (iDay > days[iMonth])
1143c2c66affSColin Finck {
1144c2c66affSColin Finck if (iMonth == 2 && IsLeapYear(iYear))
1145c2c66affSColin Finck iDay -= 29;
1146c2c66affSColin Finck else
1147c2c66affSColin Finck iDay -= days[iMonth];
1148c2c66affSColin Finck iMonth++;
1149c2c66affSColin Finck iYear += iMonth / 12;
1150c2c66affSColin Finck iMonth = iMonth % 12;
1151c2c66affSColin Finck }
1152c2c66affSColin Finck while (iDay <= 0)
1153c2c66affSColin Finck {
1154c2c66affSColin Finck iMonth--;
1155c2c66affSColin Finck if (iMonth<=0) {iMonth+=12; iYear--;}
1156c2c66affSColin Finck if (iMonth == 2 && IsLeapYear(iYear))
1157c2c66affSColin Finck iDay += 29;
1158c2c66affSColin Finck else
1159c2c66affSColin Finck iDay += days[iMonth];
1160c2c66affSColin Finck }
1161c2c66affSColin Finck
1162c2c66affSColin Finck if (iSecond<0){iSecond+=60; iMinute--;}
1163c2c66affSColin Finck if (iMinute<0){iMinute+=60; iHour--;}
1164c2c66affSColin Finck if (iHour<0) {iHour+=24; iDay--;}
1165c2c66affSColin Finck if (iYear<=0) iYear+=2000;
1166c2c66affSColin Finck
1167c2c66affSColin Finck lpUd->st.wYear = iYear;
1168c2c66affSColin Finck lpUd->st.wMonth = iMonth;
1169c2c66affSColin Finck lpUd->st.wDay = iDay;
1170c2c66affSColin Finck lpUd->st.wHour = iHour;
1171c2c66affSColin Finck lpUd->st.wMinute = iMinute;
1172c2c66affSColin Finck lpUd->st.wSecond = iSecond;
1173c2c66affSColin Finck
1174c2c66affSColin Finck TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
1175c2c66affSColin Finck lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
1176c2c66affSColin Finck return S_OK;
1177c2c66affSColin Finck }
1178c2c66affSColin Finck
1179c2c66affSColin Finck /**********************************************************************
1180c2c66affSColin Finck * DosDateTimeToVariantTime [OLEAUT32.14]
1181c2c66affSColin Finck *
1182c2c66affSColin Finck * Convert a Dos format date and time into variant VT_DATE format.
1183c2c66affSColin Finck *
1184c2c66affSColin Finck * PARAMS
1185c2c66affSColin Finck * wDosDate [I] Dos format date
1186c2c66affSColin Finck * wDosTime [I] Dos format time
1187c2c66affSColin Finck * pDateOut [O] Destination for VT_DATE format
1188c2c66affSColin Finck *
1189c2c66affSColin Finck * RETURNS
1190c2c66affSColin Finck * Success: TRUE. pDateOut contains the converted time.
1191c2c66affSColin Finck * Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
1192c2c66affSColin Finck *
1193c2c66affSColin Finck * NOTES
1194c2c66affSColin Finck * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
1195c2c66affSColin Finck * - Dos format times are accurate to only 2 second precision.
1196c2c66affSColin Finck * - The format of a Dos Date is:
1197c2c66affSColin Finck *| Bits Values Meaning
1198c2c66affSColin Finck *| ---- ------ -------
1199c2c66affSColin Finck *| 0-4 1-31 Day of the week. 0 rolls back one day. A value greater than
1200c2c66affSColin Finck *| the days in the month rolls forward the extra days.
1201c2c66affSColin Finck *| 5-8 1-12 Month of the year. 0 rolls back to December of the previous
1202c2c66affSColin Finck *| year. 13-15 are invalid.
1203c2c66affSColin Finck *| 9-15 0-119 Year based from 1980 (Max 2099). 120-127 are invalid.
1204c2c66affSColin Finck * - The format of a Dos Time is:
1205c2c66affSColin Finck *| Bits Values Meaning
1206c2c66affSColin Finck *| ---- ------ -------
1207c2c66affSColin Finck *| 0-4 0-29 Seconds/2. 30 and 31 are invalid.
1208c2c66affSColin Finck *| 5-10 0-59 Minutes. 60-63 are invalid.
1209c2c66affSColin Finck *| 11-15 0-23 Hours (24 hour clock). 24-32 are invalid.
1210c2c66affSColin Finck */
DosDateTimeToVariantTime(USHORT wDosDate,USHORT wDosTime,double * pDateOut)1211c2c66affSColin Finck INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1212c2c66affSColin Finck double *pDateOut)
1213c2c66affSColin Finck {
1214c2c66affSColin Finck UDATE ud;
1215c2c66affSColin Finck
1216c2c66affSColin Finck TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
1217c2c66affSColin Finck wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
1218c2c66affSColin Finck wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
1219c2c66affSColin Finck pDateOut);
1220c2c66affSColin Finck
1221c2c66affSColin Finck ud.st.wYear = DOS_YEAR(wDosDate);
1222c2c66affSColin Finck ud.st.wMonth = DOS_MONTH(wDosDate);
1223c2c66affSColin Finck if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
1224c2c66affSColin Finck return FALSE;
1225c2c66affSColin Finck ud.st.wDay = DOS_DAY(wDosDate);
1226c2c66affSColin Finck ud.st.wHour = DOS_HOUR(wDosTime);
1227c2c66affSColin Finck ud.st.wMinute = DOS_MINUTE(wDosTime);
1228c2c66affSColin Finck ud.st.wSecond = DOS_SECOND(wDosTime);
1229c2c66affSColin Finck ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;
1230c2c66affSColin Finck if (ud.st.wHour > 23 || ud.st.wMinute > 59 || ud.st.wSecond > 59)
1231c2c66affSColin Finck return FALSE; /* Invalid values in Dos*/
1232c2c66affSColin Finck
1233c2c66affSColin Finck return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1234c2c66affSColin Finck }
1235c2c66affSColin Finck
1236c2c66affSColin Finck /**********************************************************************
1237c2c66affSColin Finck * VariantTimeToDosDateTime [OLEAUT32.13]
1238c2c66affSColin Finck *
1239c2c66affSColin Finck * Convert a variant format date into a Dos format date and time.
1240c2c66affSColin Finck *
1241c2c66affSColin Finck * dateIn [I] VT_DATE time format
1242c2c66affSColin Finck * pwDosDate [O] Destination for Dos format date
1243c2c66affSColin Finck * pwDosTime [O] Destination for Dos format time
1244c2c66affSColin Finck *
1245c2c66affSColin Finck * RETURNS
1246c2c66affSColin Finck * Success: TRUE. pwDosDate and pwDosTime contains the converted values.
1247c2c66affSColin Finck * Failure: FALSE, if dateIn cannot be represented in Dos format.
1248c2c66affSColin Finck *
1249c2c66affSColin Finck * NOTES
1250c2c66affSColin Finck * See DosDateTimeToVariantTime() for Dos format details and bugs.
1251c2c66affSColin Finck */
VariantTimeToDosDateTime(double dateIn,USHORT * pwDosDate,USHORT * pwDosTime)1252c2c66affSColin Finck INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
1253c2c66affSColin Finck {
1254c2c66affSColin Finck UDATE ud;
1255c2c66affSColin Finck
1256c2c66affSColin Finck TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);
1257c2c66affSColin Finck
1258c2c66affSColin Finck if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1259c2c66affSColin Finck return FALSE;
1260c2c66affSColin Finck
1261c2c66affSColin Finck if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
1262c2c66affSColin Finck return FALSE;
1263c2c66affSColin Finck
1264c2c66affSColin Finck *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
1265c2c66affSColin Finck *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);
1266c2c66affSColin Finck
1267c2c66affSColin Finck TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
1268c2c66affSColin Finck *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
1269c2c66affSColin Finck *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
1270c2c66affSColin Finck return TRUE;
1271c2c66affSColin Finck }
1272c2c66affSColin Finck
1273c2c66affSColin Finck /***********************************************************************
1274c2c66affSColin Finck * SystemTimeToVariantTime [OLEAUT32.184]
1275c2c66affSColin Finck *
1276c2c66affSColin Finck * Convert a System format date and time into variant VT_DATE format.
1277c2c66affSColin Finck *
1278c2c66affSColin Finck * PARAMS
1279c2c66affSColin Finck * lpSt [I] System format date and time
1280c2c66affSColin Finck * pDateOut [O] Destination for VT_DATE format date
1281c2c66affSColin Finck *
1282c2c66affSColin Finck * RETURNS
1283c2c66affSColin Finck * Success: TRUE. *pDateOut contains the converted value.
1284c2c66affSColin Finck * Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
1285c2c66affSColin Finck */
SystemTimeToVariantTime(LPSYSTEMTIME lpSt,double * pDateOut)1286c2c66affSColin Finck INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
1287c2c66affSColin Finck {
1288c2c66affSColin Finck UDATE ud;
1289c2c66affSColin Finck
1290c2c66affSColin Finck TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
1291c2c66affSColin Finck lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);
1292c2c66affSColin Finck
1293c2c66affSColin Finck if (lpSt->wMonth > 12)
1294c2c66affSColin Finck return FALSE;
1295c2c66affSColin Finck if (lpSt->wDay > 31)
1296c2c66affSColin Finck return FALSE;
1297c2c66affSColin Finck if ((short)lpSt->wYear < 0)
1298c2c66affSColin Finck return FALSE;
1299c2c66affSColin Finck
1300c2c66affSColin Finck ud.st = *lpSt;
1301c2c66affSColin Finck return VarDateFromUdate(&ud, 0, pDateOut) == S_OK;
1302c2c66affSColin Finck }
1303c2c66affSColin Finck
1304c2c66affSColin Finck /***********************************************************************
1305c2c66affSColin Finck * VariantTimeToSystemTime [OLEAUT32.185]
1306c2c66affSColin Finck *
1307c2c66affSColin Finck * Convert a variant VT_DATE into a System format date and time.
1308c2c66affSColin Finck *
1309c2c66affSColin Finck * PARAMS
1310c2c66affSColin Finck * datein [I] Variant VT_DATE format date
1311c2c66affSColin Finck * lpSt [O] Destination for System format date and time
1312c2c66affSColin Finck *
1313c2c66affSColin Finck * RETURNS
1314c2c66affSColin Finck * Success: TRUE. *lpSt contains the converted value.
1315c2c66affSColin Finck * Failure: FALSE, if dateIn is too large or small.
1316c2c66affSColin Finck */
VariantTimeToSystemTime(double dateIn,LPSYSTEMTIME lpSt)1317c2c66affSColin Finck INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
1318c2c66affSColin Finck {
1319c2c66affSColin Finck UDATE ud;
1320c2c66affSColin Finck
1321c2c66affSColin Finck TRACE("(%g,%p)\n", dateIn, lpSt);
1322c2c66affSColin Finck
1323c2c66affSColin Finck if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
1324c2c66affSColin Finck return FALSE;
1325c2c66affSColin Finck
1326c2c66affSColin Finck *lpSt = ud.st;
1327c2c66affSColin Finck return TRUE;
1328c2c66affSColin Finck }
1329c2c66affSColin Finck
1330c2c66affSColin Finck /***********************************************************************
1331c2c66affSColin Finck * VarDateFromUdateEx [OLEAUT32.319]
1332c2c66affSColin Finck *
1333c2c66affSColin Finck * Convert an unpacked format date and time to a variant VT_DATE.
1334c2c66affSColin Finck *
1335c2c66affSColin Finck * PARAMS
1336c2c66affSColin Finck * pUdateIn [I] Unpacked format date and time to convert
1337c2c66affSColin Finck * lcid [I] Locale identifier for the conversion
1338c2c66affSColin Finck * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1339c2c66affSColin Finck * pDateOut [O] Destination for variant VT_DATE.
1340c2c66affSColin Finck *
1341c2c66affSColin Finck * RETURNS
1342c2c66affSColin Finck * Success: S_OK. *pDateOut contains the converted value.
1343c2c66affSColin Finck * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1344c2c66affSColin Finck */
VarDateFromUdateEx(UDATE * pUdateIn,LCID lcid,ULONG dwFlags,DATE * pDateOut)1345c2c66affSColin Finck HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1346c2c66affSColin Finck {
1347c2c66affSColin Finck UDATE ud;
1348c2c66affSColin Finck double dateVal = 0;
1349c2c66affSColin Finck
1350c2c66affSColin Finck TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1351c2c66affSColin Finck pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
1352c2c66affSColin Finck pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
1353c2c66affSColin Finck pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1354c2c66affSColin Finck pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1355c2c66affSColin Finck
1356c2c66affSColin Finck if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
1357c2c66affSColin Finck FIXME("lcid possibly not handled, treating as en-us\n");
1358c2c66affSColin Finck if (dwFlags & ~(VAR_TIMEVALUEONLY|VAR_DATEVALUEONLY))
1359c2c66affSColin Finck FIXME("unsupported flags: %x\n", dwFlags);
1360c2c66affSColin Finck
1361c2c66affSColin Finck ud = *pUdateIn;
1362c2c66affSColin Finck
1363c2c66affSColin Finck if (dwFlags & VAR_VALIDDATE)
1364c2c66affSColin Finck WARN("Ignoring VAR_VALIDDATE\n");
1365c2c66affSColin Finck
1366c2c66affSColin Finck if (FAILED(VARIANT_RollUdate(&ud)))
1367c2c66affSColin Finck return E_INVALIDARG;
1368c2c66affSColin Finck
1369c2c66affSColin Finck /* Date */
1370c2c66affSColin Finck if (!(dwFlags & VAR_TIMEVALUEONLY))
1371c2c66affSColin Finck dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));
1372c2c66affSColin Finck
1373c2c66affSColin Finck if ((dwFlags & VAR_TIMEVALUEONLY) || !(dwFlags & VAR_DATEVALUEONLY))
1374c2c66affSColin Finck {
1375c2c66affSColin Finck double dateSign = (dateVal < 0.0) ? -1.0 : 1.0;
1376c2c66affSColin Finck
1377c2c66affSColin Finck /* Time */
1378c2c66affSColin Finck dateVal += ud.st.wHour / 24.0 * dateSign;
1379c2c66affSColin Finck dateVal += ud.st.wMinute / 1440.0 * dateSign;
1380c2c66affSColin Finck dateVal += ud.st.wSecond / 86400.0 * dateSign;
1381c2c66affSColin Finck }
1382c2c66affSColin Finck
1383c2c66affSColin Finck TRACE("Returning %g\n", dateVal);
1384c2c66affSColin Finck *pDateOut = dateVal;
1385c2c66affSColin Finck return S_OK;
1386c2c66affSColin Finck }
1387c2c66affSColin Finck
1388c2c66affSColin Finck /***********************************************************************
1389c2c66affSColin Finck * VarDateFromUdate [OLEAUT32.330]
1390c2c66affSColin Finck *
1391c2c66affSColin Finck * Convert an unpacked format date and time to a variant VT_DATE.
1392c2c66affSColin Finck *
1393c2c66affSColin Finck * PARAMS
1394c2c66affSColin Finck * pUdateIn [I] Unpacked format date and time to convert
1395c2c66affSColin Finck * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1396c2c66affSColin Finck * pDateOut [O] Destination for variant VT_DATE.
1397c2c66affSColin Finck *
1398c2c66affSColin Finck * RETURNS
1399c2c66affSColin Finck * Success: S_OK. *pDateOut contains the converted value.
1400c2c66affSColin Finck * Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
1401c2c66affSColin Finck *
1402c2c66affSColin Finck * NOTES
1403c2c66affSColin Finck * This function uses the United States English locale for the conversion. Use
1404c2c66affSColin Finck * VarDateFromUdateEx() for alternate locales.
1405c2c66affSColin Finck */
VarDateFromUdate(UDATE * pUdateIn,ULONG dwFlags,DATE * pDateOut)1406c2c66affSColin Finck HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
1407c2c66affSColin Finck {
1408c2c66affSColin Finck LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1409c2c66affSColin Finck
1410c2c66affSColin Finck return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
1411c2c66affSColin Finck }
1412c2c66affSColin Finck
1413c2c66affSColin Finck /***********************************************************************
1414c2c66affSColin Finck * VarUdateFromDate [OLEAUT32.331]
1415c2c66affSColin Finck *
1416c2c66affSColin Finck * Convert a variant VT_DATE into an unpacked format date and time.
1417c2c66affSColin Finck *
1418c2c66affSColin Finck * PARAMS
1419c2c66affSColin Finck * datein [I] Variant VT_DATE format date
1420c2c66affSColin Finck * dwFlags [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1421c2c66affSColin Finck * lpUdate [O] Destination for unpacked format date and time
1422c2c66affSColin Finck *
1423c2c66affSColin Finck * RETURNS
1424c2c66affSColin Finck * Success: S_OK. *lpUdate contains the converted value.
1425c2c66affSColin Finck * Failure: E_INVALIDARG, if dateIn is too large or small.
1426c2c66affSColin Finck */
VarUdateFromDate(DATE dateIn,ULONG dwFlags,UDATE * lpUdate)1427c2c66affSColin Finck HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1428c2c66affSColin Finck {
1429c2c66affSColin Finck /* Cumulative totals of days per month */
1430c2c66affSColin Finck static const USHORT cumulativeDays[] =
1431c2c66affSColin Finck {
1432c2c66affSColin Finck 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
1433c2c66affSColin Finck };
1434c2c66affSColin Finck double datePart, timePart;
1435c2c66affSColin Finck int julianDays;
1436c2c66affSColin Finck
1437c2c66affSColin Finck TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1438c2c66affSColin Finck
1439c2c66affSColin Finck if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
1440c2c66affSColin Finck return E_INVALIDARG;
1441c2c66affSColin Finck
1442c2c66affSColin Finck datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
1443c2c66affSColin Finck /* Compensate for int truncation (always downwards) */
1444c2c66affSColin Finck timePart = fabs(dateIn - datePart) + 0.00000000001;
1445c2c66affSColin Finck if (timePart >= 1.0)
1446c2c66affSColin Finck timePart -= 0.00000000001;
1447c2c66affSColin Finck
1448c2c66affSColin Finck /* Date */
1449c2c66affSColin Finck julianDays = VARIANT_JulianFromDate(dateIn);
1450c2c66affSColin Finck VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
1451c2c66affSColin Finck &lpUdate->st.wDay);
1452c2c66affSColin Finck
1453c2c66affSColin Finck datePart = (datePart + 1.5) / 7.0;
1454c2c66affSColin Finck lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
1455c2c66affSColin Finck if (lpUdate->st.wDayOfWeek == 0)
1456c2c66affSColin Finck lpUdate->st.wDayOfWeek = 5;
1457c2c66affSColin Finck else if (lpUdate->st.wDayOfWeek == 1)
1458c2c66affSColin Finck lpUdate->st.wDayOfWeek = 6;
1459c2c66affSColin Finck else
1460c2c66affSColin Finck lpUdate->st.wDayOfWeek -= 2;
1461c2c66affSColin Finck
1462c2c66affSColin Finck if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
1463c2c66affSColin Finck lpUdate->wDayOfYear = 1; /* After February, in a leap year */
1464c2c66affSColin Finck else
1465c2c66affSColin Finck lpUdate->wDayOfYear = 0;
1466c2c66affSColin Finck
1467c2c66affSColin Finck lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
1468c2c66affSColin Finck lpUdate->wDayOfYear += lpUdate->st.wDay;
1469c2c66affSColin Finck
1470c2c66affSColin Finck /* Time */
1471c2c66affSColin Finck timePart *= 24.0;
1472c2c66affSColin Finck lpUdate->st.wHour = timePart;
1473c2c66affSColin Finck timePart -= lpUdate->st.wHour;
1474c2c66affSColin Finck timePart *= 60.0;
1475c2c66affSColin Finck lpUdate->st.wMinute = timePart;
1476c2c66affSColin Finck timePart -= lpUdate->st.wMinute;
1477c2c66affSColin Finck timePart *= 60.0;
1478c2c66affSColin Finck lpUdate->st.wSecond = timePart;
1479c2c66affSColin Finck timePart -= lpUdate->st.wSecond;
1480c2c66affSColin Finck lpUdate->st.wMilliseconds = 0;
1481c2c66affSColin Finck if (timePart > 0.5)
1482c2c66affSColin Finck {
1483c2c66affSColin Finck /* Round the milliseconds, adjusting the time/date forward if needed */
1484c2c66affSColin Finck if (lpUdate->st.wSecond < 59)
1485c2c66affSColin Finck lpUdate->st.wSecond++;
1486c2c66affSColin Finck else
1487c2c66affSColin Finck {
1488c2c66affSColin Finck lpUdate->st.wSecond = 0;
1489c2c66affSColin Finck if (lpUdate->st.wMinute < 59)
1490c2c66affSColin Finck lpUdate->st.wMinute++;
1491c2c66affSColin Finck else
1492c2c66affSColin Finck {
1493c2c66affSColin Finck lpUdate->st.wMinute = 0;
1494c2c66affSColin Finck if (lpUdate->st.wHour < 23)
1495c2c66affSColin Finck lpUdate->st.wHour++;
1496c2c66affSColin Finck else
1497c2c66affSColin Finck {
1498c2c66affSColin Finck lpUdate->st.wHour = 0;
1499c2c66affSColin Finck /* Roll over a whole day */
1500c2c66affSColin Finck if (++lpUdate->st.wDay > 28)
1501c2c66affSColin Finck VARIANT_RollUdate(lpUdate);
1502c2c66affSColin Finck }
1503c2c66affSColin Finck }
1504c2c66affSColin Finck }
1505c2c66affSColin Finck }
1506c2c66affSColin Finck return S_OK;
1507c2c66affSColin Finck }
1508c2c66affSColin Finck
1509c2c66affSColin Finck #define GET_NUMBER_TEXT(fld,name) \
1510c2c66affSColin Finck buff[0] = 0; \
1511c2c66affSColin Finck if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1512c2c66affSColin Finck WARN("buffer too small for " #fld "\n"); \
1513c2c66affSColin Finck else \
1514c2c66affSColin Finck if (buff[0]) lpChars->name = buff[0]; \
1515c2c66affSColin Finck TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1516c2c66affSColin Finck
1517c2c66affSColin Finck /* Get the valid number characters for an lcid */
VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS * lpChars,LCID lcid,DWORD dwFlags)1518c2c66affSColin Finck static void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
1519c2c66affSColin Finck {
1520c2c66affSColin Finck static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1521c2c66affSColin Finck static VARIANT_NUMBER_CHARS lastChars;
1522c2c66affSColin Finck static LCID lastLcid = -1;
1523c2c66affSColin Finck static DWORD lastFlags = 0;
1524c2c66affSColin Finck LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1525c2c66affSColin Finck WCHAR buff[4];
1526c2c66affSColin Finck
1527c2c66affSColin Finck /* To make caching thread-safe, a critical section is needed */
1528c2c66affSColin Finck EnterCriticalSection(&cache_cs);
1529c2c66affSColin Finck
1530c2c66affSColin Finck /* Asking for default locale entries is very expensive: It is a registry
1531c2c66affSColin Finck server call. So cache one locally, as Microsoft does it too */
1532c2c66affSColin Finck if(lcid == lastLcid && dwFlags == lastFlags)
1533c2c66affSColin Finck {
1534c2c66affSColin Finck memcpy(lpChars, &lastChars, sizeof(defaultChars));
1535c2c66affSColin Finck LeaveCriticalSection(&cache_cs);
1536c2c66affSColin Finck return;
1537c2c66affSColin Finck }
1538c2c66affSColin Finck
1539c2c66affSColin Finck memcpy(lpChars, &defaultChars, sizeof(defaultChars));
1540c2c66affSColin Finck GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
1541c2c66affSColin Finck GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
1542c2c66affSColin Finck GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1543c2c66affSColin Finck GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1544c2c66affSColin Finck GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1545c2c66affSColin Finck GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1546c2c66affSColin Finck
1547c2c66affSColin Finck /* Local currency symbols are often 2 characters */
1548c2c66affSColin Finck lpChars->cCurrencyLocal2 = '\0';
15495db885caSAmine Khaldi switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, ARRAY_SIZE(buff)))
1550c2c66affSColin Finck {
1551c2c66affSColin Finck case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
1552c2c66affSColin Finck case 2: lpChars->cCurrencyLocal = buff[0];
1553c2c66affSColin Finck break;
1554c2c66affSColin Finck default: WARN("buffer too small for LOCALE_SCURRENCY\n");
1555c2c66affSColin Finck }
1556c2c66affSColin Finck TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1557c2c66affSColin Finck lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
1558c2c66affSColin Finck
1559c2c66affSColin Finck memcpy(&lastChars, lpChars, sizeof(defaultChars));
1560c2c66affSColin Finck lastLcid = lcid;
1561c2c66affSColin Finck lastFlags = dwFlags;
1562c2c66affSColin Finck LeaveCriticalSection(&cache_cs);
1563c2c66affSColin Finck }
1564c2c66affSColin Finck
1565c2c66affSColin Finck /* Number Parsing States */
1566c2c66affSColin Finck #define B_PROCESSING_EXPONENT 0x1
1567c2c66affSColin Finck #define B_NEGATIVE_EXPONENT 0x2
1568c2c66affSColin Finck #define B_EXPONENT_START 0x4
1569c2c66affSColin Finck #define B_INEXACT_ZEROS 0x8
1570c2c66affSColin Finck #define B_LEADING_ZERO 0x10
1571c2c66affSColin Finck #define B_PROCESSING_HEX 0x20
1572c2c66affSColin Finck #define B_PROCESSING_OCT 0x40
1573c2c66affSColin Finck
1574c2c66affSColin Finck /**********************************************************************
1575c2c66affSColin Finck * VarParseNumFromStr [OLEAUT32.46]
1576c2c66affSColin Finck *
1577c2c66affSColin Finck * Parse a string containing a number into a NUMPARSE structure.
1578c2c66affSColin Finck *
1579c2c66affSColin Finck * PARAMS
1580c2c66affSColin Finck * lpszStr [I] String to parse number from
1581c2c66affSColin Finck * lcid [I] Locale Id for the conversion
1582c2c66affSColin Finck * dwFlags [I] 0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1583c2c66affSColin Finck * pNumprs [I/O] Destination for parsed number
1584c2c66affSColin Finck * rgbDig [O] Destination for digits read in
1585c2c66affSColin Finck *
1586c2c66affSColin Finck * RETURNS
1587c2c66affSColin Finck * Success: S_OK. pNumprs and rgbDig contain the parsed representation of
1588c2c66affSColin Finck * the number.
1589c2c66affSColin Finck * Failure: E_INVALIDARG, if any parameter is invalid.
1590c2c66affSColin Finck * DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
1591c2c66affSColin Finck * incorrectly.
1592c2c66affSColin Finck * DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
1593c2c66affSColin Finck *
1594c2c66affSColin Finck * NOTES
1595c2c66affSColin Finck * pNumprs must have the following fields set:
1596c2c66affSColin Finck * cDig: Set to the size of rgbDig.
1597c2c66affSColin Finck * dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
1598c2c66affSColin Finck * from "oleauto.h".
1599c2c66affSColin Finck *
1600c2c66affSColin Finck * FIXME
1601c2c66affSColin Finck * - I am unsure if this function should parse non-Arabic (e.g. Thai)
1602c2c66affSColin Finck * numerals, so this has not been implemented.
1603c2c66affSColin Finck */
VarParseNumFromStr(OLECHAR * lpszStr,LCID lcid,ULONG dwFlags,NUMPARSE * pNumprs,BYTE * rgbDig)1604c2c66affSColin Finck HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
1605c2c66affSColin Finck NUMPARSE *pNumprs, BYTE *rgbDig)
1606c2c66affSColin Finck {
1607c2c66affSColin Finck VARIANT_NUMBER_CHARS chars;
1608c2c66affSColin Finck BYTE rgbTmp[1024];
1609c2c66affSColin Finck DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
16105db885caSAmine Khaldi int iMaxDigits = ARRAY_SIZE(rgbTmp);
1611c2c66affSColin Finck int cchUsed = 0;
1612c2c66affSColin Finck
1613c2c66affSColin Finck TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1614c2c66affSColin Finck
1615c2c66affSColin Finck if (!pNumprs || !rgbDig)
1616c2c66affSColin Finck return E_INVALIDARG;
1617c2c66affSColin Finck
1618c2c66affSColin Finck if (pNumprs->cDig < iMaxDigits)
1619c2c66affSColin Finck iMaxDigits = pNumprs->cDig;
1620c2c66affSColin Finck
1621c2c66affSColin Finck pNumprs->cDig = 0;
1622c2c66affSColin Finck pNumprs->dwOutFlags = 0;
1623c2c66affSColin Finck pNumprs->cchUsed = 0;
1624c2c66affSColin Finck pNumprs->nBaseShift = 0;
1625c2c66affSColin Finck pNumprs->nPwr10 = 0;
1626c2c66affSColin Finck
1627c2c66affSColin Finck if (!lpszStr)
1628c2c66affSColin Finck return DISP_E_TYPEMISMATCH;
1629c2c66affSColin Finck
1630c2c66affSColin Finck VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);
1631c2c66affSColin Finck
1632c2c66affSColin Finck /* First consume all the leading symbols and space from the string */
1633c2c66affSColin Finck while (1)
1634c2c66affSColin Finck {
1635*ae24453eSAmine Khaldi if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && iswspace(*lpszStr))
1636c2c66affSColin Finck {
1637c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
1638c2c66affSColin Finck do
1639c2c66affSColin Finck {
1640c2c66affSColin Finck cchUsed++;
1641c2c66affSColin Finck lpszStr++;
1642*ae24453eSAmine Khaldi } while (iswspace(*lpszStr));
1643c2c66affSColin Finck }
1644c2c66affSColin Finck else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
1645c2c66affSColin Finck *lpszStr == chars.cPositiveSymbol &&
1646c2c66affSColin Finck !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
1647c2c66affSColin Finck {
1648c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
1649c2c66affSColin Finck cchUsed++;
1650c2c66affSColin Finck lpszStr++;
1651c2c66affSColin Finck }
1652c2c66affSColin Finck else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
1653c2c66affSColin Finck *lpszStr == chars.cNegativeSymbol &&
1654c2c66affSColin Finck !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
1655c2c66affSColin Finck {
1656c2c66affSColin Finck pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
1657c2c66affSColin Finck cchUsed++;
1658c2c66affSColin Finck lpszStr++;
1659c2c66affSColin Finck }
1660c2c66affSColin Finck else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
1661c2c66affSColin Finck !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
1662c2c66affSColin Finck *lpszStr == chars.cCurrencyLocal &&
1663c2c66affSColin Finck (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
1664c2c66affSColin Finck {
1665c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
1666c2c66affSColin Finck cchUsed++;
1667c2c66affSColin Finck lpszStr++;
1668c2c66affSColin Finck /* Only accept currency characters */
1669c2c66affSColin Finck chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1670c2c66affSColin Finck chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1671c2c66affSColin Finck }
1672c2c66affSColin Finck else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
1673c2c66affSColin Finck !(pNumprs->dwOutFlags & NUMPRS_PARENS))
1674c2c66affSColin Finck {
1675c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_PARENS;
1676c2c66affSColin Finck cchUsed++;
1677c2c66affSColin Finck lpszStr++;
1678c2c66affSColin Finck }
1679c2c66affSColin Finck else
1680c2c66affSColin Finck break;
1681c2c66affSColin Finck }
1682c2c66affSColin Finck
1683c2c66affSColin Finck if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
1684c2c66affSColin Finck {
1685c2c66affSColin Finck /* Only accept non-currency characters */
1686c2c66affSColin Finck chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1687c2c66affSColin Finck chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1688c2c66affSColin Finck }
1689c2c66affSColin Finck
1690c2c66affSColin Finck if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
1691c2c66affSColin Finck pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1692c2c66affSColin Finck {
1693c2c66affSColin Finck dwState |= B_PROCESSING_HEX;
1694c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1695c2c66affSColin Finck cchUsed=cchUsed+2;
1696c2c66affSColin Finck lpszStr=lpszStr+2;
1697c2c66affSColin Finck }
1698c2c66affSColin Finck else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
1699c2c66affSColin Finck pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1700c2c66affSColin Finck {
1701c2c66affSColin Finck dwState |= B_PROCESSING_OCT;
1702c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
1703c2c66affSColin Finck cchUsed=cchUsed+2;
1704c2c66affSColin Finck lpszStr=lpszStr+2;
1705c2c66affSColin Finck }
1706c2c66affSColin Finck
1707c2c66affSColin Finck /* Strip Leading zeros */
1708c2c66affSColin Finck while (*lpszStr == '0')
1709c2c66affSColin Finck {
1710c2c66affSColin Finck dwState |= B_LEADING_ZERO;
1711c2c66affSColin Finck cchUsed++;
1712c2c66affSColin Finck lpszStr++;
1713c2c66affSColin Finck }
1714c2c66affSColin Finck
1715c2c66affSColin Finck while (*lpszStr)
1716c2c66affSColin Finck {
1717*ae24453eSAmine Khaldi if (iswdigit(*lpszStr))
1718c2c66affSColin Finck {
1719c2c66affSColin Finck if (dwState & B_PROCESSING_EXPONENT)
1720c2c66affSColin Finck {
1721c2c66affSColin Finck int exponentSize = 0;
1722c2c66affSColin Finck if (dwState & B_EXPONENT_START)
1723c2c66affSColin Finck {
1724*ae24453eSAmine Khaldi if (!iswdigit(*lpszStr))
1725c2c66affSColin Finck break; /* No exponent digits - invalid */
1726c2c66affSColin Finck while (*lpszStr == '0')
1727c2c66affSColin Finck {
1728c2c66affSColin Finck /* Skip leading zero's in the exponent */
1729c2c66affSColin Finck cchUsed++;
1730c2c66affSColin Finck lpszStr++;
1731c2c66affSColin Finck }
1732c2c66affSColin Finck }
1733c2c66affSColin Finck
1734*ae24453eSAmine Khaldi while (iswdigit(*lpszStr))
1735c2c66affSColin Finck {
1736c2c66affSColin Finck exponentSize *= 10;
1737c2c66affSColin Finck exponentSize += *lpszStr - '0';
1738c2c66affSColin Finck cchUsed++;
1739c2c66affSColin Finck lpszStr++;
1740c2c66affSColin Finck }
1741c2c66affSColin Finck if (dwState & B_NEGATIVE_EXPONENT)
1742c2c66affSColin Finck exponentSize = -exponentSize;
1743c2c66affSColin Finck /* Add the exponent into the powers of 10 */
1744c2c66affSColin Finck pNumprs->nPwr10 += exponentSize;
1745c2c66affSColin Finck dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
1746c2c66affSColin Finck lpszStr--; /* back up to allow processing of next char */
1747c2c66affSColin Finck }
1748c2c66affSColin Finck else
1749c2c66affSColin Finck {
1750c2c66affSColin Finck if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
1751c2c66affSColin Finck && !(dwState & B_PROCESSING_OCT))
1752c2c66affSColin Finck {
1753c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_INEXACT;
1754c2c66affSColin Finck
1755c2c66affSColin Finck if (*lpszStr != '0')
1756c2c66affSColin Finck dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */
1757c2c66affSColin Finck
1758c2c66affSColin Finck /* This digit can't be represented, but count it in nPwr10 */
1759c2c66affSColin Finck if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1760c2c66affSColin Finck pNumprs->nPwr10--;
1761c2c66affSColin Finck else
1762c2c66affSColin Finck pNumprs->nPwr10++;
1763c2c66affSColin Finck }
1764c2c66affSColin Finck else
1765c2c66affSColin Finck {
1766c2c66affSColin Finck if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9')))
1767c2c66affSColin Finck break;
1768c2c66affSColin Finck
1769c2c66affSColin Finck if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
1770c2c66affSColin Finck pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1771c2c66affSColin Finck
1772c2c66affSColin Finck rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1773c2c66affSColin Finck }
1774c2c66affSColin Finck pNumprs->cDig++;
1775c2c66affSColin Finck cchUsed++;
1776c2c66affSColin Finck }
1777c2c66affSColin Finck }
1778c2c66affSColin Finck else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1779c2c66affSColin Finck {
1780c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
1781c2c66affSColin Finck cchUsed++;
1782c2c66affSColin Finck }
1783c2c66affSColin Finck else if (*lpszStr == chars.cDecimalPoint &&
1784c2c66affSColin Finck pNumprs->dwInFlags & NUMPRS_DECIMAL &&
1785c2c66affSColin Finck !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
1786c2c66affSColin Finck {
1787c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
1788c2c66affSColin Finck cchUsed++;
1789c2c66affSColin Finck
1790c2c66affSColin Finck /* If we have no digits so far, skip leading zeros */
1791c2c66affSColin Finck if (!pNumprs->cDig)
1792c2c66affSColin Finck {
1793c2c66affSColin Finck while (lpszStr[1] == '0')
1794c2c66affSColin Finck {
1795c2c66affSColin Finck dwState |= B_LEADING_ZERO;
1796c2c66affSColin Finck cchUsed++;
1797c2c66affSColin Finck lpszStr++;
1798c2c66affSColin Finck pNumprs->nPwr10--;
1799c2c66affSColin Finck }
1800c2c66affSColin Finck }
1801c2c66affSColin Finck }
1802c2c66affSColin Finck else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
1803c2c66affSColin Finck (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
1804c2c66affSColin Finck dwState & B_PROCESSING_HEX)
1805c2c66affSColin Finck {
1806c2c66affSColin Finck if (pNumprs->cDig >= iMaxDigits)
1807c2c66affSColin Finck {
1808c2c66affSColin Finck return DISP_E_OVERFLOW;
1809c2c66affSColin Finck }
1810c2c66affSColin Finck else
1811c2c66affSColin Finck {
1812c2c66affSColin Finck if (*lpszStr >= 'a')
1813c2c66affSColin Finck rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
1814c2c66affSColin Finck else
1815c2c66affSColin Finck rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
1816c2c66affSColin Finck }
1817c2c66affSColin Finck pNumprs->cDig++;
1818c2c66affSColin Finck cchUsed++;
1819c2c66affSColin Finck }
1820c2c66affSColin Finck else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
1821c2c66affSColin Finck pNumprs->dwInFlags & NUMPRS_EXPONENT &&
1822c2c66affSColin Finck !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
1823c2c66affSColin Finck {
1824c2c66affSColin Finck dwState |= B_PROCESSING_EXPONENT;
1825c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
1826c2c66affSColin Finck cchUsed++;
1827c2c66affSColin Finck }
1828c2c66affSColin Finck else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
1829c2c66affSColin Finck {
1830c2c66affSColin Finck cchUsed++; /* Ignore positive exponent */
1831c2c66affSColin Finck }
1832c2c66affSColin Finck else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
1833c2c66affSColin Finck {
1834c2c66affSColin Finck dwState |= B_NEGATIVE_EXPONENT;
1835c2c66affSColin Finck cchUsed++;
1836c2c66affSColin Finck }
1837c2c66affSColin Finck else
1838c2c66affSColin Finck break; /* Stop at an unrecognised character */
1839c2c66affSColin Finck
1840c2c66affSColin Finck lpszStr++;
1841c2c66affSColin Finck }
1842c2c66affSColin Finck
1843c2c66affSColin Finck if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
1844c2c66affSColin Finck {
1845c2c66affSColin Finck /* Ensure a 0 on its own gets stored */
1846c2c66affSColin Finck pNumprs->cDig = 1;
1847c2c66affSColin Finck rgbTmp[0] = 0;
1848c2c66affSColin Finck }
1849c2c66affSColin Finck
1850c2c66affSColin Finck if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
1851c2c66affSColin Finck {
1852c2c66affSColin Finck pNumprs->cchUsed = cchUsed;
1853c2c66affSColin Finck WARN("didn't completely parse exponent\n");
1854c2c66affSColin Finck return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
1855c2c66affSColin Finck }
1856c2c66affSColin Finck
1857c2c66affSColin Finck if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
1858c2c66affSColin Finck {
1859c2c66affSColin Finck if (dwState & B_INEXACT_ZEROS)
1860c2c66affSColin Finck pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
1861c2c66affSColin Finck } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
1862c2c66affSColin Finck {
1863c2c66affSColin Finck /* copy all of the digits into the output digit buffer */
1864c2c66affSColin Finck /* this is exactly what windows does although it also returns */
1865c2c66affSColin Finck /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
1866c2c66affSColin Finck memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1867c2c66affSColin Finck
1868c2c66affSColin Finck if (dwState & B_PROCESSING_HEX) {
1869c2c66affSColin Finck /* hex numbers have always the same format */
1870c2c66affSColin Finck pNumprs->nPwr10=0;
1871c2c66affSColin Finck pNumprs->nBaseShift=4;
1872c2c66affSColin Finck } else {
1873c2c66affSColin Finck if (dwState & B_PROCESSING_OCT) {
1874c2c66affSColin Finck /* oct numbers have always the same format */
1875c2c66affSColin Finck pNumprs->nPwr10=0;
1876c2c66affSColin Finck pNumprs->nBaseShift=3;
1877c2c66affSColin Finck } else {
1878c2c66affSColin Finck while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1879c2c66affSColin Finck {
1880c2c66affSColin Finck pNumprs->nPwr10++;
1881c2c66affSColin Finck pNumprs->cDig--;
1882c2c66affSColin Finck }
1883c2c66affSColin Finck }
1884c2c66affSColin Finck }
1885c2c66affSColin Finck } else
1886c2c66affSColin Finck {
1887c2c66affSColin Finck /* Remove trailing zeros from the last (whole number or decimal) part */
1888c2c66affSColin Finck while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
1889c2c66affSColin Finck {
1890c2c66affSColin Finck pNumprs->nPwr10++;
1891c2c66affSColin Finck pNumprs->cDig--;
1892c2c66affSColin Finck }
1893c2c66affSColin Finck }
1894c2c66affSColin Finck
1895c2c66affSColin Finck if (pNumprs->cDig <= iMaxDigits)
1896c2c66affSColin Finck pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
1897c2c66affSColin Finck else
1898c2c66affSColin Finck pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */
1899c2c66affSColin Finck
1900c2c66affSColin Finck /* Copy the digits we processed into rgbDig */
1901c2c66affSColin Finck memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));
1902c2c66affSColin Finck
1903c2c66affSColin Finck /* Consume any trailing symbols and space */
1904c2c66affSColin Finck while (1)
1905c2c66affSColin Finck {
1906*ae24453eSAmine Khaldi if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && iswspace(*lpszStr))
1907c2c66affSColin Finck {
1908c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
1909c2c66affSColin Finck do
1910c2c66affSColin Finck {
1911c2c66affSColin Finck cchUsed++;
1912c2c66affSColin Finck lpszStr++;
1913*ae24453eSAmine Khaldi } while (iswspace(*lpszStr));
1914c2c66affSColin Finck }
1915c2c66affSColin Finck else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
1916c2c66affSColin Finck !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
1917c2c66affSColin Finck *lpszStr == chars.cPositiveSymbol)
1918c2c66affSColin Finck {
1919c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
1920c2c66affSColin Finck cchUsed++;
1921c2c66affSColin Finck lpszStr++;
1922c2c66affSColin Finck }
1923c2c66affSColin Finck else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
1924c2c66affSColin Finck !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
1925c2c66affSColin Finck *lpszStr == chars.cNegativeSymbol)
1926c2c66affSColin Finck {
1927c2c66affSColin Finck pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
1928c2c66affSColin Finck cchUsed++;
1929c2c66affSColin Finck lpszStr++;
1930c2c66affSColin Finck }
1931c2c66affSColin Finck else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
1932c2c66affSColin Finck pNumprs->dwOutFlags & NUMPRS_PARENS)
1933c2c66affSColin Finck {
1934c2c66affSColin Finck cchUsed++;
1935c2c66affSColin Finck lpszStr++;
1936c2c66affSColin Finck pNumprs->dwOutFlags |= NUMPRS_NEG;
1937c2c66affSColin Finck }
1938c2c66affSColin Finck else
1939c2c66affSColin Finck break;
1940c2c66affSColin Finck }
1941c2c66affSColin Finck
1942c2c66affSColin Finck if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
1943c2c66affSColin Finck {
1944c2c66affSColin Finck pNumprs->cchUsed = cchUsed;
1945c2c66affSColin Finck return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
1946c2c66affSColin Finck }
1947c2c66affSColin Finck
1948c2c66affSColin Finck if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
1949c2c66affSColin Finck return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */
1950c2c66affSColin Finck
1951c2c66affSColin Finck if (!pNumprs->cDig)
1952c2c66affSColin Finck return DISP_E_TYPEMISMATCH; /* No Number found */
1953c2c66affSColin Finck
1954c2c66affSColin Finck pNumprs->cchUsed = cchUsed;
1955c2c66affSColin Finck return S_OK;
1956c2c66affSColin Finck }
1957c2c66affSColin Finck
1958c2c66affSColin Finck /* VTBIT flags indicating an integer value */
1959c2c66affSColin Finck #define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
1960c2c66affSColin Finck /* VTBIT flags indicating a real number value */
1961c2c66affSColin Finck #define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1962c2c66affSColin Finck
1963c2c66affSColin Finck /* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
1964c2c66affSColin Finck #define FITS_AS_I1(x) ((x) >> 8 == 0)
1965c2c66affSColin Finck #define FITS_AS_I2(x) ((x) >> 16 == 0)
1966c2c66affSColin Finck #define FITS_AS_I4(x) ((x) >> 32 == 0)
1967c2c66affSColin Finck
1968c2c66affSColin Finck /**********************************************************************
1969c2c66affSColin Finck * VarNumFromParseNum [OLEAUT32.47]
1970c2c66affSColin Finck *
1971c2c66affSColin Finck * Convert a NUMPARSE structure into a numeric Variant type.
1972c2c66affSColin Finck *
1973c2c66affSColin Finck * PARAMS
1974c2c66affSColin Finck * pNumprs [I] Source for parsed number. cDig must be set to the size of rgbDig
1975c2c66affSColin Finck * rgbDig [I] Source for the numbers digits
1976c2c66affSColin Finck * dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
1977c2c66affSColin Finck * pVarDst [O] Destination for the converted Variant value.
1978c2c66affSColin Finck *
1979c2c66affSColin Finck * RETURNS
1980c2c66affSColin Finck * Success: S_OK. pVarDst contains the converted value.
1981c2c66affSColin Finck * Failure: E_INVALIDARG, if any parameter is invalid.
1982c2c66affSColin Finck * DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
1983c2c66affSColin Finck *
1984c2c66affSColin Finck * NOTES
1985c2c66affSColin Finck * - The smallest favoured type present in dwVtBits that can represent the
1986c2c66affSColin Finck * number in pNumprs without losing precision is used.
1987c2c66affSColin Finck * - Signed types are preferred over unsigned types of the same size.
1988c2c66affSColin Finck * - Preferred types in order are: integer, float, double, currency then decimal.
1989c2c66affSColin Finck * - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
1990c2c66affSColin Finck * for details of the rounding method.
1991c2c66affSColin Finck * - pVarDst is not cleared before the result is stored in it.
1992c2c66affSColin Finck * - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
1993c2c66affSColin Finck * design?): If some other VTBIT's for integers are specified together
1994c2c66affSColin Finck * with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
1995c2c66affSColin Finck * the number to the smallest requested integer truncating this way the
1996c2c66affSColin Finck * number. Wine doesn't implement this "feature" (yet?).
1997c2c66affSColin Finck */
VarNumFromParseNum(NUMPARSE * pNumprs,BYTE * rgbDig,ULONG dwVtBits,VARIANT * pVarDst)1998c2c66affSColin Finck HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
1999c2c66affSColin Finck ULONG dwVtBits, VARIANT *pVarDst)
2000c2c66affSColin Finck {
2001c2c66affSColin Finck /* Scale factors and limits for double arithmetic */
2002c2c66affSColin Finck static const double dblMultipliers[11] = {
2003c2c66affSColin Finck 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
2004c2c66affSColin Finck 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
2005c2c66affSColin Finck };
2006c2c66affSColin Finck static const double dblMinimums[11] = {
2007c2c66affSColin Finck R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
2008c2c66affSColin Finck R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
2009c2c66affSColin Finck R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
2010c2c66affSColin Finck };
2011c2c66affSColin Finck static const double dblMaximums[11] = {
2012c2c66affSColin Finck R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
2013c2c66affSColin Finck R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
2014c2c66affSColin Finck R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
2015c2c66affSColin Finck };
2016c2c66affSColin Finck
2017c2c66affSColin Finck int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;
2018c2c66affSColin Finck
2019c2c66affSColin Finck TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
2020c2c66affSColin Finck
2021c2c66affSColin Finck if (pNumprs->nBaseShift)
2022c2c66affSColin Finck {
2023c2c66affSColin Finck /* nBaseShift indicates a hex or octal number */
2024c2c66affSColin Finck ULONG64 ul64 = 0;
2025c2c66affSColin Finck LONG64 l64;
2026c2c66affSColin Finck int i;
2027c2c66affSColin Finck
2028c2c66affSColin Finck /* Convert the hex or octal number string into a UI64 */
2029c2c66affSColin Finck for (i = 0; i < pNumprs->cDig; i++)
2030c2c66affSColin Finck {
2031c2c66affSColin Finck if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
2032c2c66affSColin Finck {
2033c2c66affSColin Finck TRACE("Overflow multiplying digits\n");
2034c2c66affSColin Finck return DISP_E_OVERFLOW;
2035c2c66affSColin Finck }
2036c2c66affSColin Finck ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
2037c2c66affSColin Finck }
2038c2c66affSColin Finck
2039c2c66affSColin Finck /* also make a negative representation */
2040c2c66affSColin Finck l64=-ul64;
2041c2c66affSColin Finck
2042c2c66affSColin Finck /* Try signed and unsigned types in size order */
2043c2c66affSColin Finck if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
2044c2c66affSColin Finck {
2045c2c66affSColin Finck V_VT(pVarDst) = VT_I1;
2046c2c66affSColin Finck V_I1(pVarDst) = ul64;
2047c2c66affSColin Finck return S_OK;
2048c2c66affSColin Finck }
2049c2c66affSColin Finck else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
2050c2c66affSColin Finck {
2051c2c66affSColin Finck V_VT(pVarDst) = VT_UI1;
2052c2c66affSColin Finck V_UI1(pVarDst) = ul64;
2053c2c66affSColin Finck return S_OK;
2054c2c66affSColin Finck }
2055c2c66affSColin Finck else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
2056c2c66affSColin Finck {
2057c2c66affSColin Finck V_VT(pVarDst) = VT_I2;
2058c2c66affSColin Finck V_I2(pVarDst) = ul64;
2059c2c66affSColin Finck return S_OK;
2060c2c66affSColin Finck }
2061c2c66affSColin Finck else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
2062c2c66affSColin Finck {
2063c2c66affSColin Finck V_VT(pVarDst) = VT_UI2;
2064c2c66affSColin Finck V_UI2(pVarDst) = ul64;
2065c2c66affSColin Finck return S_OK;
2066c2c66affSColin Finck }
2067c2c66affSColin Finck else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
2068c2c66affSColin Finck {
2069c2c66affSColin Finck V_VT(pVarDst) = VT_I4;
2070c2c66affSColin Finck V_I4(pVarDst) = ul64;
2071c2c66affSColin Finck return S_OK;
2072c2c66affSColin Finck }
2073c2c66affSColin Finck else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2074c2c66affSColin Finck {
2075c2c66affSColin Finck V_VT(pVarDst) = VT_UI4;
2076c2c66affSColin Finck V_UI4(pVarDst) = ul64;
2077c2c66affSColin Finck return S_OK;
2078c2c66affSColin Finck }
2079c2c66affSColin Finck else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2080c2c66affSColin Finck {
2081c2c66affSColin Finck V_VT(pVarDst) = VT_I8;
2082c2c66affSColin Finck V_I8(pVarDst) = ul64;
2083c2c66affSColin Finck return S_OK;
2084c2c66affSColin Finck }
2085c2c66affSColin Finck else if (dwVtBits & VTBIT_UI8)
2086c2c66affSColin Finck {
2087c2c66affSColin Finck V_VT(pVarDst) = VT_UI8;
2088c2c66affSColin Finck V_UI8(pVarDst) = ul64;
2089c2c66affSColin Finck return S_OK;
2090c2c66affSColin Finck }
2091c2c66affSColin Finck else if ((dwVtBits & VTBIT_DECIMAL) == VTBIT_DECIMAL)
2092c2c66affSColin Finck {
2093c2c66affSColin Finck V_VT(pVarDst) = VT_DECIMAL;
2094c2c66affSColin Finck DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2095c2c66affSColin Finck DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2096c2c66affSColin Finck DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2097c2c66affSColin Finck return S_OK;
2098c2c66affSColin Finck }
2099c2c66affSColin Finck else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2100c2c66affSColin Finck {
2101c2c66affSColin Finck V_VT(pVarDst) = VT_R4;
2102c2c66affSColin Finck if (ul64 <= I4_MAX)
2103c2c66affSColin Finck V_R4(pVarDst) = ul64;
2104c2c66affSColin Finck else
2105c2c66affSColin Finck V_R4(pVarDst) = l64;
2106c2c66affSColin Finck return S_OK;
2107c2c66affSColin Finck }
2108c2c66affSColin Finck else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
2109c2c66affSColin Finck {
2110c2c66affSColin Finck V_VT(pVarDst) = VT_R8;
2111c2c66affSColin Finck if (ul64 <= I4_MAX)
2112c2c66affSColin Finck V_R8(pVarDst) = ul64;
2113c2c66affSColin Finck else
2114c2c66affSColin Finck V_R8(pVarDst) = l64;
2115c2c66affSColin Finck return S_OK;
2116c2c66affSColin Finck }
2117c2c66affSColin Finck
2118c2c66affSColin Finck TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2119c2c66affSColin Finck return DISP_E_OVERFLOW;
2120c2c66affSColin Finck }
2121c2c66affSColin Finck
2122c2c66affSColin Finck /* Count the number of relevant fractional and whole digits stored,
2123c2c66affSColin Finck * And compute the divisor/multiplier to scale the number by.
2124c2c66affSColin Finck */
2125c2c66affSColin Finck if (pNumprs->nPwr10 < 0)
2126c2c66affSColin Finck {
2127c2c66affSColin Finck if (-pNumprs->nPwr10 >= pNumprs->cDig)
2128c2c66affSColin Finck {
2129c2c66affSColin Finck /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
2130c2c66affSColin Finck wholeNumberDigits = 0;
2131c2c66affSColin Finck fractionalDigits = pNumprs->cDig;
2132c2c66affSColin Finck divisor10 = -pNumprs->nPwr10;
2133c2c66affSColin Finck }
2134c2c66affSColin Finck else
2135c2c66affSColin Finck {
2136c2c66affSColin Finck /* An exactly represented real number e.g. 1.024 */
2137c2c66affSColin Finck wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
2138c2c66affSColin Finck fractionalDigits = pNumprs->cDig - wholeNumberDigits;
2139c2c66affSColin Finck divisor10 = pNumprs->cDig - wholeNumberDigits;
2140c2c66affSColin Finck }
2141c2c66affSColin Finck }
2142c2c66affSColin Finck else if (pNumprs->nPwr10 == 0)
2143c2c66affSColin Finck {
2144c2c66affSColin Finck /* An exactly represented whole number e.g. 1024 */
2145c2c66affSColin Finck wholeNumberDigits = pNumprs->cDig;
2146c2c66affSColin Finck fractionalDigits = 0;
2147c2c66affSColin Finck }
2148c2c66affSColin Finck else /* pNumprs->nPwr10 > 0 */
2149c2c66affSColin Finck {
2150c2c66affSColin Finck /* A whole number followed by nPwr10 0's e.g. 102400 */
2151c2c66affSColin Finck wholeNumberDigits = pNumprs->cDig;
2152c2c66affSColin Finck fractionalDigits = 0;
2153c2c66affSColin Finck multiplier10 = pNumprs->nPwr10;
2154c2c66affSColin Finck }
2155c2c66affSColin Finck
2156c2c66affSColin Finck TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
2157c2c66affSColin Finck pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
2158c2c66affSColin Finck multiplier10, divisor10);
2159c2c66affSColin Finck
2160c2c66affSColin Finck if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2161d506c2afSAmine Khaldi (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL))))
2162c2c66affSColin Finck {
2163c2c66affSColin Finck /* We have one or more integer output choices, and either:
2164c2c66affSColin Finck * 1) An integer input value, or
2165c2c66affSColin Finck * 2) A real number input value but no floating output choices.
2166c2c66affSColin Finck * Alternately, we have a DECIMAL output available and an integer input.
2167c2c66affSColin Finck *
2168c2c66affSColin Finck * So, place the integer value into pVarDst, using the smallest type
2169c2c66affSColin Finck * possible and preferring signed over unsigned types.
2170c2c66affSColin Finck */
2171c2c66affSColin Finck BOOL bOverflow = FALSE, bNegative;
2172c2c66affSColin Finck ULONG64 ul64 = 0;
2173c2c66affSColin Finck int i;
2174c2c66affSColin Finck
2175c2c66affSColin Finck /* Convert the integer part of the number into a UI8 */
2176c2c66affSColin Finck for (i = 0; i < wholeNumberDigits; i++)
2177c2c66affSColin Finck {
2178c2c66affSColin Finck if (ul64 > UI8_MAX / 10 || (ul64 == UI8_MAX / 10 && rgbDig[i] > UI8_MAX % 10))
2179c2c66affSColin Finck {
2180c2c66affSColin Finck TRACE("Overflow multiplying digits\n");
2181c2c66affSColin Finck bOverflow = TRUE;
2182c2c66affSColin Finck break;
2183c2c66affSColin Finck }
2184c2c66affSColin Finck ul64 = ul64 * 10 + rgbDig[i];
2185c2c66affSColin Finck }
2186c2c66affSColin Finck
2187c2c66affSColin Finck /* Account for the scale of the number */
2188c2c66affSColin Finck if (!bOverflow && multiplier10)
2189c2c66affSColin Finck {
2190c2c66affSColin Finck for (i = 0; i < multiplier10; i++)
2191c2c66affSColin Finck {
2192c2c66affSColin Finck if (ul64 > (UI8_MAX / 10))
2193c2c66affSColin Finck {
2194c2c66affSColin Finck TRACE("Overflow scaling number\n");
2195c2c66affSColin Finck bOverflow = TRUE;
2196c2c66affSColin Finck break;
2197c2c66affSColin Finck }
2198c2c66affSColin Finck ul64 = ul64 * 10;
2199c2c66affSColin Finck }
2200c2c66affSColin Finck }
2201c2c66affSColin Finck
2202c2c66affSColin Finck /* If we have any fractional digits, round the value.
2203c2c66affSColin Finck * Note we don't have to do this if divisor10 is < 1,
2204c2c66affSColin Finck * because this means the fractional part must be < 0.5
2205c2c66affSColin Finck */
2206c2c66affSColin Finck if (!bOverflow && fractionalDigits && divisor10 > 0)
2207c2c66affSColin Finck {
2208c2c66affSColin Finck const BYTE* fracDig = rgbDig + wholeNumberDigits;
2209c2c66affSColin Finck BOOL bAdjust = FALSE;
2210c2c66affSColin Finck
2211c2c66affSColin Finck TRACE("first decimal value is %d\n", *fracDig);
2212c2c66affSColin Finck
2213c2c66affSColin Finck if (*fracDig > 5)
2214c2c66affSColin Finck bAdjust = TRUE; /* > 0.5 */
2215c2c66affSColin Finck else if (*fracDig == 5)
2216c2c66affSColin Finck {
2217c2c66affSColin Finck for (i = 1; i < fractionalDigits; i++)
2218c2c66affSColin Finck {
2219c2c66affSColin Finck if (fracDig[i])
2220c2c66affSColin Finck {
2221c2c66affSColin Finck bAdjust = TRUE; /* > 0.5 */
2222c2c66affSColin Finck break;
2223c2c66affSColin Finck }
2224c2c66affSColin Finck }
2225c2c66affSColin Finck /* If exactly 0.5, round only odd values */
2226c2c66affSColin Finck if (i == fractionalDigits && (ul64 & 1))
2227c2c66affSColin Finck bAdjust = TRUE;
2228c2c66affSColin Finck }
2229c2c66affSColin Finck
2230c2c66affSColin Finck if (bAdjust)
2231c2c66affSColin Finck {
2232c2c66affSColin Finck if (ul64 == UI8_MAX)
2233c2c66affSColin Finck {
2234c2c66affSColin Finck TRACE("Overflow after rounding\n");
2235c2c66affSColin Finck bOverflow = TRUE;
2236c2c66affSColin Finck }
2237c2c66affSColin Finck ul64++;
2238c2c66affSColin Finck }
2239c2c66affSColin Finck }
2240c2c66affSColin Finck
2241c2c66affSColin Finck /* Zero is not a negative number */
2242c2c66affSColin Finck bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64;
2243c2c66affSColin Finck
2244c2c66affSColin Finck TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2245c2c66affSColin Finck
2246c2c66affSColin Finck /* For negative integers, try the signed types in size order */
2247c2c66affSColin Finck if (!bOverflow && bNegative)
2248c2c66affSColin Finck {
2249c2c66affSColin Finck if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
2250c2c66affSColin Finck {
2251c2c66affSColin Finck if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
2252c2c66affSColin Finck {
2253c2c66affSColin Finck V_VT(pVarDst) = VT_I1;
2254c2c66affSColin Finck V_I1(pVarDst) = -ul64;
2255c2c66affSColin Finck return S_OK;
2256c2c66affSColin Finck }
2257c2c66affSColin Finck else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
2258c2c66affSColin Finck {
2259c2c66affSColin Finck V_VT(pVarDst) = VT_I2;
2260c2c66affSColin Finck V_I2(pVarDst) = -ul64;
2261c2c66affSColin Finck return S_OK;
2262c2c66affSColin Finck }
2263c2c66affSColin Finck else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
2264c2c66affSColin Finck {
2265c2c66affSColin Finck V_VT(pVarDst) = VT_I4;
2266c2c66affSColin Finck V_I4(pVarDst) = -ul64;
2267c2c66affSColin Finck return S_OK;
2268c2c66affSColin Finck }
2269c2c66affSColin Finck else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
2270c2c66affSColin Finck {
2271c2c66affSColin Finck V_VT(pVarDst) = VT_I8;
2272c2c66affSColin Finck V_I8(pVarDst) = -ul64;
2273c2c66affSColin Finck return S_OK;
2274c2c66affSColin Finck }
2275d506c2afSAmine Khaldi else if ((dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL)) == VTBIT_DECIMAL)
2276c2c66affSColin Finck {
2277c2c66affSColin Finck /* Decimal is only output choice left - fast path */
2278c2c66affSColin Finck V_VT(pVarDst) = VT_DECIMAL;
2279c2c66affSColin Finck DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
2280c2c66affSColin Finck DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2281c2c66affSColin Finck DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
2282c2c66affSColin Finck return S_OK;
2283c2c66affSColin Finck }
2284c2c66affSColin Finck }
2285c2c66affSColin Finck }
2286c2c66affSColin Finck else if (!bOverflow)
2287c2c66affSColin Finck {
2288c2c66affSColin Finck /* For positive integers, try signed then unsigned types in size order */
2289c2c66affSColin Finck if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
2290c2c66affSColin Finck {
2291c2c66affSColin Finck V_VT(pVarDst) = VT_I1;
2292c2c66affSColin Finck V_I1(pVarDst) = ul64;
2293c2c66affSColin Finck return S_OK;
2294c2c66affSColin Finck }
2295c2c66affSColin Finck else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2296c2c66affSColin Finck {
2297c2c66affSColin Finck V_VT(pVarDst) = VT_UI1;
2298c2c66affSColin Finck V_UI1(pVarDst) = ul64;
2299c2c66affSColin Finck return S_OK;
2300c2c66affSColin Finck }
2301c2c66affSColin Finck else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2302c2c66affSColin Finck {
2303c2c66affSColin Finck V_VT(pVarDst) = VT_I2;
2304c2c66affSColin Finck V_I2(pVarDst) = ul64;
2305c2c66affSColin Finck return S_OK;
2306c2c66affSColin Finck }
2307c2c66affSColin Finck else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2308c2c66affSColin Finck {
2309c2c66affSColin Finck V_VT(pVarDst) = VT_UI2;
2310c2c66affSColin Finck V_UI2(pVarDst) = ul64;
2311c2c66affSColin Finck return S_OK;
2312c2c66affSColin Finck }
2313c2c66affSColin Finck else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2314c2c66affSColin Finck {
2315c2c66affSColin Finck V_VT(pVarDst) = VT_I4;
2316c2c66affSColin Finck V_I4(pVarDst) = ul64;
2317c2c66affSColin Finck return S_OK;
2318c2c66affSColin Finck }
2319c2c66affSColin Finck else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2320c2c66affSColin Finck {
2321c2c66affSColin Finck V_VT(pVarDst) = VT_UI4;
2322c2c66affSColin Finck V_UI4(pVarDst) = ul64;
2323c2c66affSColin Finck return S_OK;
2324c2c66affSColin Finck }
2325c2c66affSColin Finck else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2326c2c66affSColin Finck {
2327c2c66affSColin Finck V_VT(pVarDst) = VT_I8;
2328c2c66affSColin Finck V_I8(pVarDst) = ul64;
2329c2c66affSColin Finck return S_OK;
2330c2c66affSColin Finck }
2331c2c66affSColin Finck else if (dwVtBits & VTBIT_UI8)
2332c2c66affSColin Finck {
2333c2c66affSColin Finck V_VT(pVarDst) = VT_UI8;
2334c2c66affSColin Finck V_UI8(pVarDst) = ul64;
2335c2c66affSColin Finck return S_OK;
2336c2c66affSColin Finck }
2337d506c2afSAmine Khaldi else if ((dwVtBits & (REAL_VTBITS|VTBIT_DECIMAL)) == VTBIT_DECIMAL)
2338c2c66affSColin Finck {
2339c2c66affSColin Finck /* Decimal is only output choice left - fast path */
2340c2c66affSColin Finck V_VT(pVarDst) = VT_DECIMAL;
2341c2c66affSColin Finck DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
2342c2c66affSColin Finck DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
2343c2c66affSColin Finck DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
2344c2c66affSColin Finck return S_OK;
2345c2c66affSColin Finck }
2346c2c66affSColin Finck }
2347c2c66affSColin Finck }
2348c2c66affSColin Finck
2349c2c66affSColin Finck if (dwVtBits & REAL_VTBITS)
2350c2c66affSColin Finck {
2351c2c66affSColin Finck /* Try to put the number into a float or real */
2352c2c66affSColin Finck BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
2353c2c66affSColin Finck double whole = 0.0;
2354c2c66affSColin Finck int i;
2355c2c66affSColin Finck
2356c2c66affSColin Finck /* Convert the number into a double */
2357c2c66affSColin Finck for (i = 0; i < pNumprs->cDig; i++)
2358c2c66affSColin Finck whole = whole * 10.0 + rgbDig[i];
2359c2c66affSColin Finck
2360c2c66affSColin Finck TRACE("Whole double value is %16.16g\n", whole);
2361c2c66affSColin Finck
2362c2c66affSColin Finck /* Account for the scale */
2363c2c66affSColin Finck while (multiplier10 > 10)
2364c2c66affSColin Finck {
2365c2c66affSColin Finck if (whole > dblMaximums[10])
2366c2c66affSColin Finck {
2367c2c66affSColin Finck dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2368c2c66affSColin Finck bOverflow = TRUE;
2369c2c66affSColin Finck break;
2370c2c66affSColin Finck }
2371c2c66affSColin Finck whole = whole * dblMultipliers[10];
2372c2c66affSColin Finck multiplier10 -= 10;
2373c2c66affSColin Finck }
2374c2c66affSColin Finck if (multiplier10 && !bOverflow)
2375c2c66affSColin Finck {
2376c2c66affSColin Finck if (whole > dblMaximums[multiplier10])
2377c2c66affSColin Finck {
2378c2c66affSColin Finck dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
2379c2c66affSColin Finck bOverflow = TRUE;
2380c2c66affSColin Finck }
2381c2c66affSColin Finck else
2382c2c66affSColin Finck whole = whole * dblMultipliers[multiplier10];
2383c2c66affSColin Finck }
2384c2c66affSColin Finck
2385c2c66affSColin Finck if (!bOverflow)
2386c2c66affSColin Finck TRACE("Scaled double value is %16.16g\n", whole);
2387c2c66affSColin Finck
2388c2c66affSColin Finck while (divisor10 > 10 && !bOverflow)
2389c2c66affSColin Finck {
2390c2c66affSColin Finck if (whole < dblMinimums[10] && whole != 0)
2391c2c66affSColin Finck {
2392d506c2afSAmine Khaldi whole = 0; /* ignore underflow */
2393d506c2afSAmine Khaldi divisor10 = 0;
2394c2c66affSColin Finck break;
2395c2c66affSColin Finck }
2396c2c66affSColin Finck whole = whole / dblMultipliers[10];
2397c2c66affSColin Finck divisor10 -= 10;
2398c2c66affSColin Finck }
2399c2c66affSColin Finck if (divisor10 && !bOverflow)
2400c2c66affSColin Finck {
2401c2c66affSColin Finck if (whole < dblMinimums[divisor10] && whole != 0)
2402c2c66affSColin Finck {
2403d506c2afSAmine Khaldi whole = 0; /* ignore underflow */
2404d506c2afSAmine Khaldi divisor10 = 0;
2405c2c66affSColin Finck }
2406c2c66affSColin Finck else
2407c2c66affSColin Finck whole = whole / dblMultipliers[divisor10];
2408c2c66affSColin Finck }
2409c2c66affSColin Finck if (!bOverflow)
2410c2c66affSColin Finck TRACE("Final double value is %16.16g\n", whole);
2411c2c66affSColin Finck
2412c2c66affSColin Finck if (dwVtBits & VTBIT_R4 &&
2413c2c66affSColin Finck ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
2414c2c66affSColin Finck {
2415c2c66affSColin Finck TRACE("Set R4 to final value\n");
2416c2c66affSColin Finck V_VT(pVarDst) = VT_R4; /* Fits into a float */
2417c2c66affSColin Finck V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2418c2c66affSColin Finck return S_OK;
2419c2c66affSColin Finck }
2420c2c66affSColin Finck
2421c2c66affSColin Finck if (dwVtBits & VTBIT_R8)
2422c2c66affSColin Finck {
2423c2c66affSColin Finck TRACE("Set R8 to final value\n");
2424c2c66affSColin Finck V_VT(pVarDst) = VT_R8; /* Fits into a double */
2425c2c66affSColin Finck V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
2426c2c66affSColin Finck return S_OK;
2427c2c66affSColin Finck }
2428c2c66affSColin Finck
2429c2c66affSColin Finck if (dwVtBits & VTBIT_CY)
2430c2c66affSColin Finck {
2431c2c66affSColin Finck if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
2432c2c66affSColin Finck {
2433c2c66affSColin Finck V_VT(pVarDst) = VT_CY; /* Fits into a currency */
2434c2c66affSColin Finck TRACE("Set CY to final value\n");
2435c2c66affSColin Finck return S_OK;
2436c2c66affSColin Finck }
2437c2c66affSColin Finck TRACE("Value Overflows CY\n");
2438c2c66affSColin Finck }
2439c2c66affSColin Finck }
2440c2c66affSColin Finck
2441c2c66affSColin Finck if (dwVtBits & VTBIT_DECIMAL)
2442c2c66affSColin Finck {
2443c2c66affSColin Finck int i;
2444c2c66affSColin Finck ULONG carry;
2445c2c66affSColin Finck ULONG64 tmp;
2446c2c66affSColin Finck DECIMAL* pDec = &V_DECIMAL(pVarDst);
2447c2c66affSColin Finck
2448c2c66affSColin Finck DECIMAL_SETZERO(*pDec);
2449c2c66affSColin Finck DEC_LO32(pDec) = 0;
2450c2c66affSColin Finck
2451c2c66affSColin Finck if (pNumprs->dwOutFlags & NUMPRS_NEG)
2452c2c66affSColin Finck DEC_SIGN(pDec) = DECIMAL_NEG;
2453c2c66affSColin Finck else
2454c2c66affSColin Finck DEC_SIGN(pDec) = DECIMAL_POS;
2455c2c66affSColin Finck
2456c2c66affSColin Finck /* Factor the significant digits */
2457c2c66affSColin Finck for (i = 0; i < pNumprs->cDig; i++)
2458c2c66affSColin Finck {
2459c2c66affSColin Finck tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
2460c2c66affSColin Finck carry = (ULONG)(tmp >> 32);
2461c2c66affSColin Finck DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2462c2c66affSColin Finck tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2463c2c66affSColin Finck carry = (ULONG)(tmp >> 32);
2464c2c66affSColin Finck DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2465c2c66affSColin Finck tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2466c2c66affSColin Finck DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2467c2c66affSColin Finck
2468c2c66affSColin Finck if (tmp >> 32 & UI4_MAX)
2469c2c66affSColin Finck {
2470c2c66affSColin Finck VarNumFromParseNum_DecOverflow:
2471c2c66affSColin Finck TRACE("Overflow\n");
2472c2c66affSColin Finck DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
2473c2c66affSColin Finck return DISP_E_OVERFLOW;
2474c2c66affSColin Finck }
2475c2c66affSColin Finck }
2476c2c66affSColin Finck
2477c2c66affSColin Finck /* Account for the scale of the number */
2478c2c66affSColin Finck while (multiplier10 > 0)
2479c2c66affSColin Finck {
2480c2c66affSColin Finck tmp = (ULONG64)DEC_LO32(pDec) * 10;
2481c2c66affSColin Finck carry = (ULONG)(tmp >> 32);
2482c2c66affSColin Finck DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
2483c2c66affSColin Finck tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
2484c2c66affSColin Finck carry = (ULONG)(tmp >> 32);
2485c2c66affSColin Finck DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
2486c2c66affSColin Finck tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
2487c2c66affSColin Finck DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2488c2c66affSColin Finck
2489c2c66affSColin Finck if (tmp >> 32 & UI4_MAX)
2490c2c66affSColin Finck goto VarNumFromParseNum_DecOverflow;
2491c2c66affSColin Finck multiplier10--;
2492c2c66affSColin Finck }
2493c2c66affSColin Finck DEC_SCALE(pDec) = divisor10;
2494c2c66affSColin Finck
2495c2c66affSColin Finck V_VT(pVarDst) = VT_DECIMAL;
2496c2c66affSColin Finck return S_OK;
2497c2c66affSColin Finck }
2498c2c66affSColin Finck return DISP_E_OVERFLOW; /* No more output choices */
2499c2c66affSColin Finck }
2500c2c66affSColin Finck
2501c2c66affSColin Finck /**********************************************************************
2502c2c66affSColin Finck * VarCat [OLEAUT32.318]
2503c2c66affSColin Finck *
2504c2c66affSColin Finck * Concatenates one variant onto another.
2505c2c66affSColin Finck *
2506c2c66affSColin Finck * PARAMS
2507c2c66affSColin Finck * left [I] First variant
2508c2c66affSColin Finck * right [I] Second variant
2509c2c66affSColin Finck * result [O] Result variant
2510c2c66affSColin Finck *
2511c2c66affSColin Finck * RETURNS
2512c2c66affSColin Finck * Success: S_OK.
2513c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
2514c2c66affSColin Finck */
VarCat(LPVARIANT left,LPVARIANT right,LPVARIANT out)2515c2c66affSColin Finck HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
2516c2c66affSColin Finck {
2517c2c66affSColin Finck BSTR left_str = NULL, right_str = NULL;
2518c2c66affSColin Finck VARTYPE leftvt, rightvt;
2519c2c66affSColin Finck HRESULT hres;
2520c2c66affSColin Finck
2521c2c66affSColin Finck TRACE("%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), out);
2522c2c66affSColin Finck
2523c2c66affSColin Finck leftvt = V_VT(left);
2524c2c66affSColin Finck rightvt = V_VT(right);
2525c2c66affSColin Finck
2526c2c66affSColin Finck /* when both left and right are NULL the result is NULL */
2527c2c66affSColin Finck if (leftvt == VT_NULL && rightvt == VT_NULL)
2528c2c66affSColin Finck {
2529c2c66affSColin Finck V_VT(out) = VT_NULL;
2530c2c66affSColin Finck return S_OK;
2531c2c66affSColin Finck }
2532c2c66affSColin Finck
2533c2c66affSColin Finck /* There are many special case for errors and return types */
2534c2c66affSColin Finck if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
2535c2c66affSColin Finck rightvt == VT_DATE || rightvt == VT_DECIMAL))
2536c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
2537c2c66affSColin Finck else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2538c2c66affSColin Finck leftvt == VT_R4 || leftvt == VT_R8 ||
2539c2c66affSColin Finck leftvt == VT_CY || leftvt == VT_BOOL ||
2540c2c66affSColin Finck leftvt == VT_BSTR || leftvt == VT_I1 ||
2541c2c66affSColin Finck leftvt == VT_UI1 || leftvt == VT_UI2 ||
2542c2c66affSColin Finck leftvt == VT_UI4 || leftvt == VT_I8 ||
2543c2c66affSColin Finck leftvt == VT_UI8 || leftvt == VT_INT ||
2544c2c66affSColin Finck leftvt == VT_UINT || leftvt == VT_EMPTY ||
2545c2c66affSColin Finck leftvt == VT_NULL || leftvt == VT_DATE ||
2546c2c66affSColin Finck leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2547c2c66affSColin Finck &&
2548c2c66affSColin Finck (rightvt == VT_I2 || rightvt == VT_I4 ||
2549c2c66affSColin Finck rightvt == VT_R4 || rightvt == VT_R8 ||
2550c2c66affSColin Finck rightvt == VT_CY || rightvt == VT_BOOL ||
2551c2c66affSColin Finck rightvt == VT_BSTR || rightvt == VT_I1 ||
2552c2c66affSColin Finck rightvt == VT_UI1 || rightvt == VT_UI2 ||
2553c2c66affSColin Finck rightvt == VT_UI4 || rightvt == VT_I8 ||
2554c2c66affSColin Finck rightvt == VT_UI8 || rightvt == VT_INT ||
2555c2c66affSColin Finck rightvt == VT_UINT || rightvt == VT_EMPTY ||
2556c2c66affSColin Finck rightvt == VT_NULL || rightvt == VT_DATE ||
2557c2c66affSColin Finck rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2558c2c66affSColin Finck hres = S_OK;
2559c2c66affSColin Finck else if (rightvt == VT_ERROR && leftvt < VT_VOID)
2560c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
2561c2c66affSColin Finck else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
2562c2c66affSColin Finck rightvt == VT_ERROR || rightvt == VT_DECIMAL))
2563c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
2564c2c66affSColin Finck else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
2565c2c66affSColin Finck rightvt == VT_DECIMAL)
2566c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
2567c2c66affSColin Finck else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
2568c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
2569c2c66affSColin Finck else if (leftvt == VT_VARIANT)
2570c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
2571c2c66affSColin Finck else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
2572c2c66affSColin Finck leftvt == VT_NULL || leftvt == VT_I2 ||
2573c2c66affSColin Finck leftvt == VT_I4 || leftvt == VT_R4 ||
2574c2c66affSColin Finck leftvt == VT_R8 || leftvt == VT_CY ||
2575c2c66affSColin Finck leftvt == VT_DATE || leftvt == VT_BSTR ||
2576c2c66affSColin Finck leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
2577c2c66affSColin Finck leftvt == VT_I1 || leftvt == VT_UI1 ||
2578c2c66affSColin Finck leftvt == VT_UI2 || leftvt == VT_UI4 ||
2579c2c66affSColin Finck leftvt == VT_I8 || leftvt == VT_UI8 ||
2580c2c66affSColin Finck leftvt == VT_INT || leftvt == VT_UINT))
2581c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
2582c2c66affSColin Finck else
2583c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
2584c2c66affSColin Finck
2585c2c66affSColin Finck /* if result type is not S_OK, then no need to go further */
2586c2c66affSColin Finck if (hres != S_OK)
2587c2c66affSColin Finck {
2588c2c66affSColin Finck V_VT(out) = VT_EMPTY;
2589c2c66affSColin Finck return hres;
2590c2c66affSColin Finck }
2591c2c66affSColin Finck
2592c2c66affSColin Finck if (leftvt == VT_BSTR)
2593c2c66affSColin Finck left_str = V_BSTR(left);
2594c2c66affSColin Finck else
2595c2c66affSColin Finck {
2596c2c66affSColin Finck VARIANT converted, *tmp = left;
2597c2c66affSColin Finck
2598c2c66affSColin Finck VariantInit(&converted);
2599c2c66affSColin Finck if(leftvt == VT_DISPATCH)
2600c2c66affSColin Finck {
2601c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(left, &converted);
2602c2c66affSColin Finck if(FAILED(hres))
2603c2c66affSColin Finck goto failed;
2604c2c66affSColin Finck
2605c2c66affSColin Finck tmp = &converted;
2606c2c66affSColin Finck }
2607c2c66affSColin Finck
2608c2c66affSColin Finck hres = VariantChangeTypeEx(&converted, tmp, 0, VARIANT_ALPHABOOL|VARIANT_LOCALBOOL, VT_BSTR);
2609c2c66affSColin Finck if (SUCCEEDED(hres))
2610c2c66affSColin Finck left_str = V_BSTR(&converted);
2611c2c66affSColin Finck else if (hres != DISP_E_TYPEMISMATCH)
2612c2c66affSColin Finck {
2613c2c66affSColin Finck VariantClear(&converted);
2614c2c66affSColin Finck goto failed;
2615c2c66affSColin Finck }
2616c2c66affSColin Finck }
2617c2c66affSColin Finck
2618c2c66affSColin Finck if (rightvt == VT_BSTR)
2619c2c66affSColin Finck right_str = V_BSTR(right);
2620c2c66affSColin Finck else
2621c2c66affSColin Finck {
2622c2c66affSColin Finck VARIANT converted, *tmp = right;
2623c2c66affSColin Finck
2624c2c66affSColin Finck VariantInit(&converted);
2625c2c66affSColin Finck if(rightvt == VT_DISPATCH)
2626c2c66affSColin Finck {
2627c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(right, &converted);
2628c2c66affSColin Finck if(FAILED(hres))
2629c2c66affSColin Finck goto failed;
2630c2c66affSColin Finck
2631c2c66affSColin Finck tmp = &converted;
2632c2c66affSColin Finck }
2633c2c66affSColin Finck
2634c2c66affSColin Finck hres = VariantChangeTypeEx(&converted, tmp, 0, VARIANT_ALPHABOOL|VARIANT_LOCALBOOL, VT_BSTR);
2635c2c66affSColin Finck if (SUCCEEDED(hres))
2636c2c66affSColin Finck right_str = V_BSTR(&converted);
2637c2c66affSColin Finck else if (hres != DISP_E_TYPEMISMATCH)
2638c2c66affSColin Finck {
2639c2c66affSColin Finck VariantClear(&converted);
2640c2c66affSColin Finck goto failed;
2641c2c66affSColin Finck }
2642c2c66affSColin Finck }
2643c2c66affSColin Finck
2644c2c66affSColin Finck
2645c2c66affSColin Finck V_VT(out) = VT_BSTR;
2646c2c66affSColin Finck hres = VarBstrCat(left_str, right_str, &V_BSTR(out));
2647c2c66affSColin Finck
2648c2c66affSColin Finck failed:
2649c2c66affSColin Finck if(V_VT(left) != VT_BSTR)
2650c2c66affSColin Finck SysFreeString(left_str);
2651c2c66affSColin Finck if(V_VT(right) != VT_BSTR)
2652c2c66affSColin Finck SysFreeString(right_str);
2653c2c66affSColin Finck return hres;
2654c2c66affSColin Finck }
2655c2c66affSColin Finck
2656c2c66affSColin Finck
2657c2c66affSColin Finck /* Wrapper around VariantChangeTypeEx() which permits changing a
2658c2c66affSColin Finck variant with VT_RESERVED flag set. Needed by VarCmp. */
_VarChangeTypeExWrap(VARIANTARG * pvargDest,VARIANTARG * pvargSrc,LCID lcid,USHORT wFlags,VARTYPE vt)2659c2c66affSColin Finck static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
2660c2c66affSColin Finck VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
2661c2c66affSColin Finck {
2662c2c66affSColin Finck VARIANTARG vtmpsrc = *pvargSrc;
2663c2c66affSColin Finck
2664c2c66affSColin Finck V_VT(&vtmpsrc) &= ~VT_RESERVED;
2665c2c66affSColin Finck return VariantChangeTypeEx(pvargDest,&vtmpsrc,lcid,wFlags,vt);
2666c2c66affSColin Finck }
2667c2c66affSColin Finck
2668c2c66affSColin Finck /**********************************************************************
2669c2c66affSColin Finck * VarCmp [OLEAUT32.176]
2670c2c66affSColin Finck *
2671c2c66affSColin Finck * Compare two variants.
2672c2c66affSColin Finck *
2673c2c66affSColin Finck * PARAMS
2674c2c66affSColin Finck * left [I] First variant
2675c2c66affSColin Finck * right [I] Second variant
2676c2c66affSColin Finck * lcid [I] LCID (locale identifier) for the comparison
2677c2c66affSColin Finck * flags [I] Flags to be used in the comparison:
2678c2c66affSColin Finck * NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
2679c2c66affSColin Finck * NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
2680c2c66affSColin Finck *
2681c2c66affSColin Finck * RETURNS
2682c2c66affSColin Finck * VARCMP_LT: left variant is less than right variant.
2683c2c66affSColin Finck * VARCMP_EQ: input variants are equal.
2684c2c66affSColin Finck * VARCMP_GT: left variant is greater than right variant.
2685c2c66affSColin Finck * VARCMP_NULL: either one of the input variants is NULL.
2686c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
2687c2c66affSColin Finck *
2688c2c66affSColin Finck * NOTES
2689c2c66affSColin Finck * Native VarCmp up to and including WinXP doesn't like I1, UI2, VT_UI4,
2690c2c66affSColin Finck * UI8 and UINT as input variants. INT is accepted only as left variant.
2691c2c66affSColin Finck *
2692c2c66affSColin Finck * If both input variants are ERROR then VARCMP_EQ will be returned, else
2693c2c66affSColin Finck * an ERROR variant will trigger an error.
2694c2c66affSColin Finck *
2695c2c66affSColin Finck * Both input variants can have VT_RESERVED flag set which is ignored
2696c2c66affSColin Finck * unless one and only one of the variants is a BSTR and the other one
2697c2c66affSColin Finck * is not an EMPTY variant. All four VT_RESERVED combinations have a
2698c2c66affSColin Finck * different meaning:
2699c2c66affSColin Finck * - BSTR and other: BSTR is always greater than the other variant.
2700c2c66affSColin Finck * - BSTR|VT_RESERVED and other: a string comparison is performed.
2701c2c66affSColin Finck * - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
2702c2c66affSColin Finck * comparison will take place else the BSTR is always greater.
2703c2c66affSColin Finck * - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
2704c2c66affSColin Finck * variant is ignored and the return value depends only on the sign
2705c2c66affSColin Finck * of the BSTR if it is a number else the BSTR is always greater. A
2706c2c66affSColin Finck * positive BSTR is greater, a negative one is smaller than the other
2707c2c66affSColin Finck * variant.
2708c2c66affSColin Finck *
2709c2c66affSColin Finck * SEE
2710c2c66affSColin Finck * VarBstrCmp for the lcid and flags usage.
2711c2c66affSColin Finck */
VarCmp(LPVARIANT left,LPVARIANT right,LCID lcid,DWORD flags)2712c2c66affSColin Finck HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
2713c2c66affSColin Finck {
2714c2c66affSColin Finck VARTYPE lvt, rvt, vt;
2715c2c66affSColin Finck VARIANT rv,lv;
2716c2c66affSColin Finck DWORD xmask;
2717c2c66affSColin Finck HRESULT rc;
2718c2c66affSColin Finck
2719c2c66affSColin Finck TRACE("(%s,%s,0x%08x,0x%08x)\n", debugstr_variant(left), debugstr_variant(right), lcid, flags);
2720c2c66affSColin Finck
2721c2c66affSColin Finck lvt = V_VT(left) & VT_TYPEMASK;
2722c2c66affSColin Finck rvt = V_VT(right) & VT_TYPEMASK;
2723c2c66affSColin Finck xmask = (1 << lvt) | (1 << rvt);
2724c2c66affSColin Finck
2725c2c66affSColin Finck /* If we have any flag set except VT_RESERVED bail out.
2726c2c66affSColin Finck Same for the left input variant type > VT_INT and for the
2727c2c66affSColin Finck right input variant type > VT_I8. Yes, VT_INT is only supported
2728c2c66affSColin Finck as left variant. Go figure */
2729c2c66affSColin Finck if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
2730c2c66affSColin Finck lvt > VT_INT || rvt > VT_I8) {
2731c2c66affSColin Finck return DISP_E_BADVARTYPE;
2732c2c66affSColin Finck }
2733c2c66affSColin Finck
2734c2c66affSColin Finck /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
2735c2c66affSColin Finck VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
2736c2c66affSColin Finck if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
2737c2c66affSColin Finck VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
2738c2c66affSColin Finck return DISP_E_TYPEMISMATCH;
2739c2c66affSColin Finck
2740c2c66affSColin Finck /* If both variants are VT_ERROR return VARCMP_EQ */
2741c2c66affSColin Finck if (xmask == VTBIT_ERROR)
2742c2c66affSColin Finck return VARCMP_EQ;
2743c2c66affSColin Finck else if (xmask & VTBIT_ERROR)
2744c2c66affSColin Finck return DISP_E_TYPEMISMATCH;
2745c2c66affSColin Finck
2746c2c66affSColin Finck if (xmask & VTBIT_NULL)
2747c2c66affSColin Finck return VARCMP_NULL;
2748c2c66affSColin Finck
2749c2c66affSColin Finck VariantInit(&lv);
2750c2c66affSColin Finck VariantInit(&rv);
2751c2c66affSColin Finck
2752c2c66affSColin Finck /* Two BSTRs, ignore VT_RESERVED */
2753c2c66affSColin Finck if (xmask == VTBIT_BSTR)
2754c2c66affSColin Finck return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);
2755c2c66affSColin Finck
2756c2c66affSColin Finck /* A BSTR and another variant; we have to take care of VT_RESERVED */
2757c2c66affSColin Finck if (xmask & VTBIT_BSTR) {
2758c2c66affSColin Finck VARIANT *bstrv, *nonbv;
2759c2c66affSColin Finck VARTYPE nonbvt;
2760c2c66affSColin Finck int swap = 0;
2761c2c66affSColin Finck
2762c2c66affSColin Finck /* Swap the variants so the BSTR is always on the left */
2763c2c66affSColin Finck if (lvt == VT_BSTR) {
2764c2c66affSColin Finck bstrv = left;
2765c2c66affSColin Finck nonbv = right;
2766c2c66affSColin Finck nonbvt = rvt;
2767c2c66affSColin Finck } else {
2768c2c66affSColin Finck swap = 1;
2769c2c66affSColin Finck bstrv = right;
2770c2c66affSColin Finck nonbv = left;
2771c2c66affSColin Finck nonbvt = lvt;
2772c2c66affSColin Finck }
2773c2c66affSColin Finck
2774c2c66affSColin Finck /* BSTR and EMPTY: ignore VT_RESERVED */
2775c2c66affSColin Finck if (nonbvt == VT_EMPTY)
2776c2c66affSColin Finck rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
2777c2c66affSColin Finck else {
2778c2c66affSColin Finck VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
2779c2c66affSColin Finck VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;
2780c2c66affSColin Finck
2781c2c66affSColin Finck if (!breserv && !nreserv)
2782c2c66affSColin Finck /* No VT_RESERVED set ==> BSTR always greater */
2783c2c66affSColin Finck rc = VARCMP_GT;
2784c2c66affSColin Finck else if (breserv && !nreserv) {
2785c2c66affSColin Finck /* BSTR has VT_RESERVED set. Do a string comparison */
2786c2c66affSColin Finck rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
2787c2c66affSColin Finck if (FAILED(rc))
2788c2c66affSColin Finck return rc;
2789c2c66affSColin Finck rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
2790c2c66affSColin Finck VariantClear(&rv);
2791c2c66affSColin Finck } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
2792c2c66affSColin Finck /* Non NULL nor empty BSTR */
2793c2c66affSColin Finck /* If the BSTR is not a number the BSTR is greater */
2794c2c66affSColin Finck rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
2795c2c66affSColin Finck if (FAILED(rc))
2796c2c66affSColin Finck rc = VARCMP_GT;
2797c2c66affSColin Finck else if (breserv && nreserv)
2798c2c66affSColin Finck /* FIXME: This is strange: with both VT_RESERVED set it
2799c2c66affSColin Finck looks like the result depends only on the sign of
2800c2c66affSColin Finck the BSTR number */
2801c2c66affSColin Finck rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
2802c2c66affSColin Finck else
2803c2c66affSColin Finck /* Numeric comparison, will be handled below.
2804c2c66affSColin Finck VARCMP_NULL used only to break out. */
2805c2c66affSColin Finck rc = VARCMP_NULL;
2806c2c66affSColin Finck VariantClear(&lv);
2807c2c66affSColin Finck VariantClear(&rv);
2808c2c66affSColin Finck } else
2809c2c66affSColin Finck /* Empty or NULL BSTR */
2810c2c66affSColin Finck rc = VARCMP_GT;
2811c2c66affSColin Finck }
2812c2c66affSColin Finck /* Fixup the return code if we swapped left and right */
2813c2c66affSColin Finck if (swap) {
2814c2c66affSColin Finck if (rc == VARCMP_GT)
2815c2c66affSColin Finck rc = VARCMP_LT;
2816c2c66affSColin Finck else if (rc == VARCMP_LT)
2817c2c66affSColin Finck rc = VARCMP_GT;
2818c2c66affSColin Finck }
2819c2c66affSColin Finck if (rc != VARCMP_NULL)
2820c2c66affSColin Finck return rc;
2821c2c66affSColin Finck }
2822c2c66affSColin Finck
2823c2c66affSColin Finck if (xmask & VTBIT_DECIMAL)
2824c2c66affSColin Finck vt = VT_DECIMAL;
2825c2c66affSColin Finck else if (xmask & VTBIT_BSTR)
2826c2c66affSColin Finck vt = VT_R8;
2827c2c66affSColin Finck else if (xmask & VTBIT_R4)
2828c2c66affSColin Finck vt = VT_R4;
2829c2c66affSColin Finck else if (xmask & (VTBIT_R8 | VTBIT_DATE))
2830c2c66affSColin Finck vt = VT_R8;
2831c2c66affSColin Finck else if (xmask & VTBIT_CY)
2832c2c66affSColin Finck vt = VT_CY;
2833c2c66affSColin Finck else
2834c2c66affSColin Finck /* default to I8 */
2835c2c66affSColin Finck vt = VT_I8;
2836c2c66affSColin Finck
2837c2c66affSColin Finck /* Coerce the variants */
2838c2c66affSColin Finck rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2839c2c66affSColin Finck if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2840c2c66affSColin Finck /* Overflow, change to R8 */
2841c2c66affSColin Finck vt = VT_R8;
2842c2c66affSColin Finck rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2843c2c66affSColin Finck }
2844c2c66affSColin Finck if (FAILED(rc))
2845c2c66affSColin Finck return rc;
2846c2c66affSColin Finck rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2847c2c66affSColin Finck if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
2848c2c66affSColin Finck /* Overflow, change to R8 */
2849c2c66affSColin Finck vt = VT_R8;
2850c2c66affSColin Finck rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
2851c2c66affSColin Finck if (FAILED(rc))
2852c2c66affSColin Finck return rc;
2853c2c66affSColin Finck rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
2854c2c66affSColin Finck }
2855c2c66affSColin Finck if (FAILED(rc))
2856c2c66affSColin Finck return rc;
2857c2c66affSColin Finck
2858c2c66affSColin Finck #define _VARCMP(a,b) \
2859c2c66affSColin Finck (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))
2860c2c66affSColin Finck
2861c2c66affSColin Finck switch (vt) {
2862c2c66affSColin Finck case VT_CY:
2863c2c66affSColin Finck return VarCyCmp(V_CY(&lv), V_CY(&rv));
2864c2c66affSColin Finck case VT_DECIMAL:
2865c2c66affSColin Finck return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
2866c2c66affSColin Finck case VT_I8:
2867c2c66affSColin Finck return _VARCMP(V_I8(&lv), V_I8(&rv));
2868c2c66affSColin Finck case VT_R4:
2869c2c66affSColin Finck return _VARCMP(V_R4(&lv), V_R4(&rv));
2870c2c66affSColin Finck case VT_R8:
2871c2c66affSColin Finck return _VARCMP(V_R8(&lv), V_R8(&rv));
2872c2c66affSColin Finck default:
2873c2c66affSColin Finck /* We should never get here */
2874c2c66affSColin Finck return E_FAIL;
2875c2c66affSColin Finck }
2876c2c66affSColin Finck #undef _VARCMP
2877c2c66affSColin Finck }
2878c2c66affSColin Finck
2879c2c66affSColin Finck /**********************************************************************
2880c2c66affSColin Finck * VarAnd [OLEAUT32.142]
2881c2c66affSColin Finck *
2882c2c66affSColin Finck * Computes the logical AND of two variants.
2883c2c66affSColin Finck *
2884c2c66affSColin Finck * PARAMS
2885c2c66affSColin Finck * left [I] First variant
2886c2c66affSColin Finck * right [I] Second variant
2887c2c66affSColin Finck * result [O] Result variant
2888c2c66affSColin Finck *
2889c2c66affSColin Finck * RETURNS
2890c2c66affSColin Finck * Success: S_OK.
2891c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
2892c2c66affSColin Finck */
VarAnd(LPVARIANT left,LPVARIANT right,LPVARIANT result)2893c2c66affSColin Finck HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
2894c2c66affSColin Finck {
2895c2c66affSColin Finck HRESULT hres = S_OK;
2896c2c66affSColin Finck VARTYPE resvt = VT_EMPTY;
2897c2c66affSColin Finck VARTYPE leftvt,rightvt;
2898c2c66affSColin Finck VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
2899c2c66affSColin Finck VARIANT varLeft, varRight;
2900c2c66affSColin Finck VARIANT tempLeft, tempRight;
2901c2c66affSColin Finck
2902c2c66affSColin Finck VariantInit(&varLeft);
2903c2c66affSColin Finck VariantInit(&varRight);
2904c2c66affSColin Finck VariantInit(&tempLeft);
2905c2c66affSColin Finck VariantInit(&tempRight);
2906c2c66affSColin Finck
2907c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
2908c2c66affSColin Finck
2909c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
2910c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
2911c2c66affSColin Finck {
2912c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(left, &tempLeft);
2913c2c66affSColin Finck if (FAILED(hres)) goto VarAnd_Exit;
2914c2c66affSColin Finck left = &tempLeft;
2915c2c66affSColin Finck }
2916c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
2917c2c66affSColin Finck {
2918c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(right, &tempRight);
2919c2c66affSColin Finck if (FAILED(hres)) goto VarAnd_Exit;
2920c2c66affSColin Finck right = &tempRight;
2921c2c66affSColin Finck }
2922c2c66affSColin Finck
2923c2c66affSColin Finck leftvt = V_VT(left)&VT_TYPEMASK;
2924c2c66affSColin Finck rightvt = V_VT(right)&VT_TYPEMASK;
2925c2c66affSColin Finck leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
2926c2c66affSColin Finck rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2927c2c66affSColin Finck
2928c2c66affSColin Finck if (leftExtraFlags != rightExtraFlags)
2929c2c66affSColin Finck {
2930c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
2931c2c66affSColin Finck goto VarAnd_Exit;
2932c2c66affSColin Finck }
2933c2c66affSColin Finck ExtraFlags = leftExtraFlags;
2934c2c66affSColin Finck
2935c2c66affSColin Finck /* Native VarAnd always returns an error when using extra
2936c2c66affSColin Finck * flags or if the variant combination is I8 and INT.
2937c2c66affSColin Finck */
2938c2c66affSColin Finck if ((leftvt == VT_I8 && rightvt == VT_INT) ||
2939c2c66affSColin Finck (leftvt == VT_INT && rightvt == VT_I8) ||
2940c2c66affSColin Finck ExtraFlags != 0)
2941c2c66affSColin Finck {
2942c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
2943c2c66affSColin Finck goto VarAnd_Exit;
2944c2c66affSColin Finck }
2945c2c66affSColin Finck
2946c2c66affSColin Finck /* Determine return type */
2947c2c66affSColin Finck else if (leftvt == VT_I8 || rightvt == VT_I8)
2948c2c66affSColin Finck resvt = VT_I8;
2949c2c66affSColin Finck else if (leftvt == VT_I4 || rightvt == VT_I4 ||
2950c2c66affSColin Finck leftvt == VT_UINT || rightvt == VT_UINT ||
2951c2c66affSColin Finck leftvt == VT_INT || rightvt == VT_INT ||
2952c2c66affSColin Finck leftvt == VT_R4 || rightvt == VT_R4 ||
2953c2c66affSColin Finck leftvt == VT_R8 || rightvt == VT_R8 ||
2954c2c66affSColin Finck leftvt == VT_CY || rightvt == VT_CY ||
2955c2c66affSColin Finck leftvt == VT_DATE || rightvt == VT_DATE ||
2956c2c66affSColin Finck leftvt == VT_I1 || rightvt == VT_I1 ||
2957c2c66affSColin Finck leftvt == VT_UI2 || rightvt == VT_UI2 ||
2958c2c66affSColin Finck leftvt == VT_UI4 || rightvt == VT_UI4 ||
2959c2c66affSColin Finck leftvt == VT_UI8 || rightvt == VT_UI8 ||
2960c2c66affSColin Finck leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
2961c2c66affSColin Finck resvt = VT_I4;
2962c2c66affSColin Finck else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
2963c2c66affSColin Finck leftvt == VT_I2 || rightvt == VT_I2 ||
2964c2c66affSColin Finck leftvt == VT_EMPTY || rightvt == VT_EMPTY)
2965c2c66affSColin Finck if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
2966c2c66affSColin Finck (leftvt == VT_UI1 && rightvt == VT_NULL) ||
2967c2c66affSColin Finck (leftvt == VT_UI1 && rightvt == VT_UI1))
2968c2c66affSColin Finck resvt = VT_UI1;
2969c2c66affSColin Finck else
2970c2c66affSColin Finck resvt = VT_I2;
2971c2c66affSColin Finck else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
2972c2c66affSColin Finck (leftvt == VT_BSTR && rightvt == VT_BSTR))
2973c2c66affSColin Finck resvt = VT_BOOL;
2974c2c66affSColin Finck else if (leftvt == VT_NULL || rightvt == VT_NULL ||
2975c2c66affSColin Finck leftvt == VT_BSTR || rightvt == VT_BSTR)
2976c2c66affSColin Finck resvt = VT_NULL;
2977c2c66affSColin Finck else
2978c2c66affSColin Finck {
2979c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
2980c2c66affSColin Finck goto VarAnd_Exit;
2981c2c66affSColin Finck }
2982c2c66affSColin Finck
2983c2c66affSColin Finck if (leftvt == VT_NULL || rightvt == VT_NULL)
2984c2c66affSColin Finck {
2985c2c66affSColin Finck /*
2986c2c66affSColin Finck * Special cases for when left variant is VT_NULL
2987c2c66affSColin Finck * (VT_NULL & 0 = VT_NULL, VT_NULL & value = value)
2988c2c66affSColin Finck */
2989c2c66affSColin Finck if (leftvt == VT_NULL)
2990c2c66affSColin Finck {
2991c2c66affSColin Finck VARIANT_BOOL b;
2992c2c66affSColin Finck switch(rightvt)
2993c2c66affSColin Finck {
2994c2c66affSColin Finck case VT_I1: if (V_I1(right)) resvt = VT_NULL; break;
2995c2c66affSColin Finck case VT_UI1: if (V_UI1(right)) resvt = VT_NULL; break;
2996c2c66affSColin Finck case VT_I2: if (V_I2(right)) resvt = VT_NULL; break;
2997c2c66affSColin Finck case VT_UI2: if (V_UI2(right)) resvt = VT_NULL; break;
2998c2c66affSColin Finck case VT_I4: if (V_I4(right)) resvt = VT_NULL; break;
2999c2c66affSColin Finck case VT_UI4: if (V_UI4(right)) resvt = VT_NULL; break;
3000c2c66affSColin Finck case VT_I8: if (V_I8(right)) resvt = VT_NULL; break;
3001c2c66affSColin Finck case VT_UI8: if (V_UI8(right)) resvt = VT_NULL; break;
3002c2c66affSColin Finck case VT_INT: if (V_INT(right)) resvt = VT_NULL; break;
3003c2c66affSColin Finck case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
3004c2c66affSColin Finck case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
3005c2c66affSColin Finck case VT_R4: if (V_R4(right)) resvt = VT_NULL; break;
3006c2c66affSColin Finck case VT_R8: if (V_R8(right)) resvt = VT_NULL; break;
3007c2c66affSColin Finck case VT_CY:
3008c2c66affSColin Finck if(V_CY(right).int64)
3009c2c66affSColin Finck resvt = VT_NULL;
3010c2c66affSColin Finck break;
3011c2c66affSColin Finck case VT_DECIMAL:
3012c2c66affSColin Finck if (DEC_HI32(&V_DECIMAL(right)) ||
3013c2c66affSColin Finck DEC_LO64(&V_DECIMAL(right)))
3014c2c66affSColin Finck resvt = VT_NULL;
3015c2c66affSColin Finck break;
3016c2c66affSColin Finck case VT_BSTR:
3017c2c66affSColin Finck hres = VarBoolFromStr(V_BSTR(right),
3018c2c66affSColin Finck LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
3019c2c66affSColin Finck if (FAILED(hres))
3020c2c66affSColin Finck return hres;
3021c2c66affSColin Finck else if (b)
3022c2c66affSColin Finck V_VT(result) = VT_NULL;
3023c2c66affSColin Finck else
3024c2c66affSColin Finck {
3025c2c66affSColin Finck V_VT(result) = VT_BOOL;
3026c2c66affSColin Finck V_BOOL(result) = b;
3027c2c66affSColin Finck }
3028c2c66affSColin Finck goto VarAnd_Exit;
3029c2c66affSColin Finck }
3030c2c66affSColin Finck }
3031c2c66affSColin Finck V_VT(result) = resvt;
3032c2c66affSColin Finck goto VarAnd_Exit;
3033c2c66affSColin Finck }
3034c2c66affSColin Finck
3035c2c66affSColin Finck hres = VariantCopy(&varLeft, left);
3036c2c66affSColin Finck if (FAILED(hres)) goto VarAnd_Exit;
3037c2c66affSColin Finck
3038c2c66affSColin Finck hres = VariantCopy(&varRight, right);
3039c2c66affSColin Finck if (FAILED(hres)) goto VarAnd_Exit;
3040c2c66affSColin Finck
3041c2c66affSColin Finck if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
3042c2c66affSColin Finck V_VT(&varLeft) = VT_I4; /* Don't overflow */
3043c2c66affSColin Finck else
3044c2c66affSColin Finck {
3045c2c66affSColin Finck double d;
3046c2c66affSColin Finck
3047c2c66affSColin Finck if (V_VT(&varLeft) == VT_BSTR &&
3048c2c66affSColin Finck FAILED(VarR8FromStr(V_BSTR(&varLeft),
3049c2c66affSColin Finck LOCALE_USER_DEFAULT, 0, &d)))
3050c2c66affSColin Finck hres = VariantChangeType(&varLeft,&varLeft,
3051c2c66affSColin Finck VARIANT_LOCALBOOL, VT_BOOL);
3052c2c66affSColin Finck if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
3053c2c66affSColin Finck hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
3054c2c66affSColin Finck if (FAILED(hres)) goto VarAnd_Exit;
3055c2c66affSColin Finck }
3056c2c66affSColin Finck
3057c2c66affSColin Finck if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
3058c2c66affSColin Finck V_VT(&varRight) = VT_I4; /* Don't overflow */
3059c2c66affSColin Finck else
3060c2c66affSColin Finck {
3061c2c66affSColin Finck double d;
3062c2c66affSColin Finck
3063c2c66affSColin Finck if (V_VT(&varRight) == VT_BSTR &&
3064c2c66affSColin Finck FAILED(VarR8FromStr(V_BSTR(&varRight),
3065c2c66affSColin Finck LOCALE_USER_DEFAULT, 0, &d)))
3066c2c66affSColin Finck hres = VariantChangeType(&varRight, &varRight,
3067c2c66affSColin Finck VARIANT_LOCALBOOL, VT_BOOL);
3068c2c66affSColin Finck if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
3069c2c66affSColin Finck hres = VariantChangeType(&varRight, &varRight, 0, resvt);
3070c2c66affSColin Finck if (FAILED(hres)) goto VarAnd_Exit;
3071c2c66affSColin Finck }
3072c2c66affSColin Finck
3073c2c66affSColin Finck V_VT(result) = resvt;
3074c2c66affSColin Finck switch(resvt)
3075c2c66affSColin Finck {
3076c2c66affSColin Finck case VT_I8:
3077c2c66affSColin Finck V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
3078c2c66affSColin Finck break;
3079c2c66affSColin Finck case VT_I4:
3080c2c66affSColin Finck V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
3081c2c66affSColin Finck break;
3082c2c66affSColin Finck case VT_I2:
3083c2c66affSColin Finck V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
3084c2c66affSColin Finck break;
3085c2c66affSColin Finck case VT_UI1:
3086c2c66affSColin Finck V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
3087c2c66affSColin Finck break;
3088c2c66affSColin Finck case VT_BOOL:
3089c2c66affSColin Finck V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
3090c2c66affSColin Finck break;
3091c2c66affSColin Finck default:
3092c2c66affSColin Finck FIXME("Couldn't bitwise AND variant types %d,%d\n",
3093c2c66affSColin Finck leftvt,rightvt);
3094c2c66affSColin Finck }
3095c2c66affSColin Finck
3096c2c66affSColin Finck VarAnd_Exit:
3097c2c66affSColin Finck VariantClear(&varLeft);
3098c2c66affSColin Finck VariantClear(&varRight);
3099c2c66affSColin Finck VariantClear(&tempLeft);
3100c2c66affSColin Finck VariantClear(&tempRight);
3101c2c66affSColin Finck
3102c2c66affSColin Finck return hres;
3103c2c66affSColin Finck }
3104c2c66affSColin Finck
3105c2c66affSColin Finck /**********************************************************************
3106c2c66affSColin Finck * VarAdd [OLEAUT32.141]
3107c2c66affSColin Finck *
3108c2c66affSColin Finck * Add two variants.
3109c2c66affSColin Finck *
3110c2c66affSColin Finck * PARAMS
3111c2c66affSColin Finck * left [I] First variant
3112c2c66affSColin Finck * right [I] Second variant
3113c2c66affSColin Finck * result [O] Result variant
3114c2c66affSColin Finck *
3115c2c66affSColin Finck * RETURNS
3116c2c66affSColin Finck * Success: S_OK.
3117c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
3118c2c66affSColin Finck *
3119c2c66affSColin Finck * NOTES
3120c2c66affSColin Finck * Native VarAdd up to and including WinXP doesn't like I1, UI2, UI4,
3121c2c66affSColin Finck * UI8, INT and UINT as input variants.
3122c2c66affSColin Finck *
3123c2c66affSColin Finck * Native VarAdd doesn't check for NULL in/out pointers and crashes. We do the
3124c2c66affSColin Finck * same here.
3125c2c66affSColin Finck *
3126c2c66affSColin Finck * FIXME
3127c2c66affSColin Finck * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3128c2c66affSColin Finck * case.
3129c2c66affSColin Finck */
VarAdd(LPVARIANT left,LPVARIANT right,LPVARIANT result)3130c2c66affSColin Finck HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3131c2c66affSColin Finck {
3132c2c66affSColin Finck HRESULT hres;
3133c2c66affSColin Finck VARTYPE lvt, rvt, resvt, tvt;
3134c2c66affSColin Finck VARIANT lv, rv, tv;
3135c2c66affSColin Finck VARIANT tempLeft, tempRight;
3136c2c66affSColin Finck double r8res;
3137c2c66affSColin Finck
3138c2c66affSColin Finck /* Variant priority for coercion. Sorted from lowest to highest.
3139c2c66affSColin Finck VT_ERROR shows an invalid input variant type. */
3140c2c66affSColin Finck enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
3141c2c66affSColin Finck vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
3142c2c66affSColin Finck vt_ERROR };
3143c2c66affSColin Finck /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3144c2c66affSColin Finck static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3145c2c66affSColin Finck VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
3146c2c66affSColin Finck VT_NULL, VT_ERROR };
3147c2c66affSColin Finck
3148c2c66affSColin Finck /* Mapping for coercion from input variant to priority of result variant. */
3149c2c66affSColin Finck static const VARTYPE coerce[] = {
3150c2c66affSColin Finck /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3151c2c66affSColin Finck vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
3152c2c66affSColin Finck /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3153c2c66affSColin Finck vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
3154c2c66affSColin Finck /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3155c2c66affSColin Finck vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3156c2c66affSColin Finck /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3157c2c66affSColin Finck vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3158c2c66affSColin Finck };
3159c2c66affSColin Finck
3160c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3161c2c66affSColin Finck
3162c2c66affSColin Finck VariantInit(&lv);
3163c2c66affSColin Finck VariantInit(&rv);
3164c2c66affSColin Finck VariantInit(&tv);
3165c2c66affSColin Finck VariantInit(&tempLeft);
3166c2c66affSColin Finck VariantInit(&tempRight);
3167c2c66affSColin Finck
3168c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
3169c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) != VT_NULL && (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3170c2c66affSColin Finck {
3171c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3172c2c66affSColin Finck {
3173c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3174c2c66affSColin Finck if (FAILED(hres)) goto end;
3175c2c66affSColin Finck left = &tempLeft;
3176c2c66affSColin Finck }
3177c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3178c2c66affSColin Finck {
3179c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(right, &tempRight);
3180c2c66affSColin Finck if (FAILED(hres)) goto end;
3181c2c66affSColin Finck right = &tempRight;
3182c2c66affSColin Finck }
3183c2c66affSColin Finck }
3184c2c66affSColin Finck
3185c2c66affSColin Finck lvt = V_VT(left)&VT_TYPEMASK;
3186c2c66affSColin Finck rvt = V_VT(right)&VT_TYPEMASK;
3187c2c66affSColin Finck
3188c2c66affSColin Finck /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3189c2c66affSColin Finck Same for any input variant type > VT_I8 */
3190c2c66affSColin Finck if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3191c2c66affSColin Finck lvt > VT_I8 || rvt > VT_I8) {
3192c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3193c2c66affSColin Finck goto end;
3194c2c66affSColin Finck }
3195c2c66affSColin Finck
3196c2c66affSColin Finck /* Determine the variant type to coerce to. */
3197c2c66affSColin Finck if (coerce[lvt] > coerce[rvt]) {
3198c2c66affSColin Finck resvt = prio2vt[coerce[lvt]];
3199c2c66affSColin Finck tvt = prio2vt[coerce[rvt]];
3200c2c66affSColin Finck } else {
3201c2c66affSColin Finck resvt = prio2vt[coerce[rvt]];
3202c2c66affSColin Finck tvt = prio2vt[coerce[lvt]];
3203c2c66affSColin Finck }
3204c2c66affSColin Finck
3205c2c66affSColin Finck /* Special cases where the result variant type is defined by both
3206c2c66affSColin Finck input variants and not only that with the highest priority */
3207c2c66affSColin Finck if (resvt == VT_BSTR) {
3208c2c66affSColin Finck if (tvt == VT_EMPTY || tvt == VT_BSTR)
3209c2c66affSColin Finck resvt = VT_BSTR;
3210c2c66affSColin Finck else
3211c2c66affSColin Finck resvt = VT_R8;
3212c2c66affSColin Finck }
3213c2c66affSColin Finck if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
3214c2c66affSColin Finck resvt = VT_R8;
3215c2c66affSColin Finck
3216c2c66affSColin Finck /* For overflow detection use the biggest compatible type for the
3217c2c66affSColin Finck addition */
3218c2c66affSColin Finck switch (resvt) {
3219c2c66affSColin Finck case VT_ERROR:
3220c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3221c2c66affSColin Finck goto end;
3222c2c66affSColin Finck case VT_NULL:
3223c2c66affSColin Finck hres = S_OK;
3224c2c66affSColin Finck V_VT(result) = VT_NULL;
3225c2c66affSColin Finck goto end;
3226c2c66affSColin Finck case VT_DISPATCH:
3227c2c66affSColin Finck FIXME("cannot handle variant type VT_DISPATCH\n");
3228c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3229c2c66affSColin Finck goto end;
3230c2c66affSColin Finck case VT_EMPTY:
3231c2c66affSColin Finck resvt = VT_I2;
3232c2c66affSColin Finck /* Fall through */
3233c2c66affSColin Finck case VT_UI1:
3234c2c66affSColin Finck case VT_I2:
3235c2c66affSColin Finck case VT_I4:
3236c2c66affSColin Finck case VT_I8:
3237c2c66affSColin Finck tvt = VT_I8;
3238c2c66affSColin Finck break;
3239c2c66affSColin Finck case VT_DATE:
3240c2c66affSColin Finck case VT_R4:
3241c2c66affSColin Finck tvt = VT_R8;
3242c2c66affSColin Finck break;
3243c2c66affSColin Finck default:
3244c2c66affSColin Finck tvt = resvt;
3245c2c66affSColin Finck }
3246c2c66affSColin Finck
3247c2c66affSColin Finck /* Now coerce the variants */
3248c2c66affSColin Finck hres = VariantChangeType(&lv, left, 0, tvt);
3249c2c66affSColin Finck if (FAILED(hres))
3250c2c66affSColin Finck goto end;
3251c2c66affSColin Finck hres = VariantChangeType(&rv, right, 0, tvt);
3252c2c66affSColin Finck if (FAILED(hres))
3253c2c66affSColin Finck goto end;
3254c2c66affSColin Finck
3255c2c66affSColin Finck /* Do the math */
3256c2c66affSColin Finck hres = S_OK;
3257c2c66affSColin Finck V_VT(result) = resvt;
3258c2c66affSColin Finck switch (tvt) {
3259c2c66affSColin Finck case VT_DECIMAL:
3260c2c66affSColin Finck hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3261c2c66affSColin Finck &V_DECIMAL(result));
3262c2c66affSColin Finck goto end;
3263c2c66affSColin Finck case VT_CY:
3264c2c66affSColin Finck hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
3265c2c66affSColin Finck goto end;
3266c2c66affSColin Finck case VT_BSTR:
3267c2c66affSColin Finck /* We do not add those, we concatenate them. */
3268c2c66affSColin Finck hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
3269c2c66affSColin Finck goto end;
3270c2c66affSColin Finck case VT_I8:
3271c2c66affSColin Finck /* Overflow detection */
3272c2c66affSColin Finck r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
3273c2c66affSColin Finck if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3274c2c66affSColin Finck V_VT(result) = VT_R8;
3275c2c66affSColin Finck V_R8(result) = r8res;
3276c2c66affSColin Finck goto end;
3277c2c66affSColin Finck } else {
3278c2c66affSColin Finck V_VT(&tv) = tvt;
3279c2c66affSColin Finck V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3280c2c66affSColin Finck }
3281c2c66affSColin Finck break;
3282c2c66affSColin Finck case VT_R8:
3283c2c66affSColin Finck V_VT(&tv) = tvt;
3284c2c66affSColin Finck /* FIXME: overflow detection */
3285c2c66affSColin Finck V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
3286c2c66affSColin Finck break;
3287c2c66affSColin Finck default:
3288c2c66affSColin Finck ERR("We shouldn't get here! tvt = %d!\n", tvt);
3289c2c66affSColin Finck break;
3290c2c66affSColin Finck }
3291c2c66affSColin Finck if (resvt != tvt) {
3292c2c66affSColin Finck if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3293c2c66affSColin Finck /* Overflow! Change to the vartype with the next higher priority.
3294c2c66affSColin Finck With one exception: I4 ==> R8 even if it would fit in I8 */
3295c2c66affSColin Finck if (resvt == VT_I4)
3296c2c66affSColin Finck resvt = VT_R8;
3297c2c66affSColin Finck else
3298c2c66affSColin Finck resvt = prio2vt[coerce[resvt] + 1];
3299c2c66affSColin Finck hres = VariantChangeType(result, &tv, 0, resvt);
3300c2c66affSColin Finck }
3301c2c66affSColin Finck } else
3302c2c66affSColin Finck hres = VariantCopy(result, &tv);
3303c2c66affSColin Finck
3304c2c66affSColin Finck end:
3305c2c66affSColin Finck if (hres != S_OK) {
3306c2c66affSColin Finck V_VT(result) = VT_EMPTY;
3307c2c66affSColin Finck V_I4(result) = 0; /* No V_EMPTY */
3308c2c66affSColin Finck }
3309c2c66affSColin Finck VariantClear(&lv);
3310c2c66affSColin Finck VariantClear(&rv);
3311c2c66affSColin Finck VariantClear(&tv);
3312c2c66affSColin Finck VariantClear(&tempLeft);
3313c2c66affSColin Finck VariantClear(&tempRight);
3314c2c66affSColin Finck TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3315c2c66affSColin Finck return hres;
3316c2c66affSColin Finck }
3317c2c66affSColin Finck
3318c2c66affSColin Finck /**********************************************************************
3319c2c66affSColin Finck * VarMul [OLEAUT32.156]
3320c2c66affSColin Finck *
3321c2c66affSColin Finck * Multiply two variants.
3322c2c66affSColin Finck *
3323c2c66affSColin Finck * PARAMS
3324c2c66affSColin Finck * left [I] First variant
3325c2c66affSColin Finck * right [I] Second variant
3326c2c66affSColin Finck * result [O] Result variant
3327c2c66affSColin Finck *
3328c2c66affSColin Finck * RETURNS
3329c2c66affSColin Finck * Success: S_OK.
3330c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
3331c2c66affSColin Finck *
3332c2c66affSColin Finck * NOTES
3333c2c66affSColin Finck * Native VarMul up to and including WinXP doesn't like I1, UI2, UI4,
3334c2c66affSColin Finck * UI8, INT and UINT as input variants. But it can multiply apples with oranges.
3335c2c66affSColin Finck *
3336c2c66affSColin Finck * Native VarMul doesn't check for NULL in/out pointers and crashes. We do the
3337c2c66affSColin Finck * same here.
3338c2c66affSColin Finck *
3339c2c66affSColin Finck * FIXME
3340c2c66affSColin Finck * Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
3341c2c66affSColin Finck * case.
3342c2c66affSColin Finck */
VarMul(LPVARIANT left,LPVARIANT right,LPVARIANT result)3343c2c66affSColin Finck HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3344c2c66affSColin Finck {
3345c2c66affSColin Finck HRESULT hres;
3346c2c66affSColin Finck VARTYPE lvt, rvt, resvt, tvt;
3347c2c66affSColin Finck VARIANT lv, rv, tv;
3348c2c66affSColin Finck VARIANT tempLeft, tempRight;
3349c2c66affSColin Finck double r8res;
3350c2c66affSColin Finck
3351c2c66affSColin Finck /* Variant priority for coercion. Sorted from lowest to highest.
3352c2c66affSColin Finck VT_ERROR shows an invalid input variant type. */
3353c2c66affSColin Finck enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
3354c2c66affSColin Finck vt_DECIMAL, vt_NULL, vt_ERROR };
3355c2c66affSColin Finck /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3356c2c66affSColin Finck static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3357c2c66affSColin Finck VT_DECIMAL, VT_NULL, VT_ERROR };
3358c2c66affSColin Finck
3359c2c66affSColin Finck /* Mapping for coercion from input variant to priority of result variant. */
3360c2c66affSColin Finck static const VARTYPE coerce[] = {
3361c2c66affSColin Finck /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
3362c2c66affSColin Finck vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
3363c2c66affSColin Finck /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
3364c2c66affSColin Finck vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
3365c2c66affSColin Finck /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
3366c2c66affSColin Finck vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
3367c2c66affSColin Finck /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
3368c2c66affSColin Finck vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
3369c2c66affSColin Finck };
3370c2c66affSColin Finck
3371c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3372c2c66affSColin Finck
3373c2c66affSColin Finck VariantInit(&lv);
3374c2c66affSColin Finck VariantInit(&rv);
3375c2c66affSColin Finck VariantInit(&tv);
3376c2c66affSColin Finck VariantInit(&tempLeft);
3377c2c66affSColin Finck VariantInit(&tempRight);
3378c2c66affSColin Finck
3379c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
3380c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3381c2c66affSColin Finck {
3382c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3383c2c66affSColin Finck if (FAILED(hres)) goto end;
3384c2c66affSColin Finck left = &tempLeft;
3385c2c66affSColin Finck }
3386c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3387c2c66affSColin Finck {
3388c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(right, &tempRight);
3389c2c66affSColin Finck if (FAILED(hres)) goto end;
3390c2c66affSColin Finck right = &tempRight;
3391c2c66affSColin Finck }
3392c2c66affSColin Finck
3393c2c66affSColin Finck lvt = V_VT(left)&VT_TYPEMASK;
3394c2c66affSColin Finck rvt = V_VT(right)&VT_TYPEMASK;
3395c2c66affSColin Finck
3396c2c66affSColin Finck /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
3397c2c66affSColin Finck Same for any input variant type > VT_I8 */
3398c2c66affSColin Finck if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
3399c2c66affSColin Finck lvt > VT_I8 || rvt > VT_I8) {
3400c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3401c2c66affSColin Finck goto end;
3402c2c66affSColin Finck }
3403c2c66affSColin Finck
3404c2c66affSColin Finck /* Determine the variant type to coerce to. */
3405c2c66affSColin Finck if (coerce[lvt] > coerce[rvt]) {
3406c2c66affSColin Finck resvt = prio2vt[coerce[lvt]];
3407c2c66affSColin Finck tvt = prio2vt[coerce[rvt]];
3408c2c66affSColin Finck } else {
3409c2c66affSColin Finck resvt = prio2vt[coerce[rvt]];
3410c2c66affSColin Finck tvt = prio2vt[coerce[lvt]];
3411c2c66affSColin Finck }
3412c2c66affSColin Finck
3413c2c66affSColin Finck /* Special cases where the result variant type is defined by both
3414c2c66affSColin Finck input variants and not only that with the highest priority */
3415c2c66affSColin Finck if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
3416c2c66affSColin Finck resvt = VT_R8;
3417c2c66affSColin Finck if (lvt == VT_EMPTY && rvt == VT_EMPTY)
3418c2c66affSColin Finck resvt = VT_I2;
3419c2c66affSColin Finck
3420c2c66affSColin Finck /* For overflow detection use the biggest compatible type for the
3421c2c66affSColin Finck multiplication */
3422c2c66affSColin Finck switch (resvt) {
3423c2c66affSColin Finck case VT_ERROR:
3424c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3425c2c66affSColin Finck goto end;
3426c2c66affSColin Finck case VT_NULL:
3427c2c66affSColin Finck hres = S_OK;
3428c2c66affSColin Finck V_VT(result) = VT_NULL;
3429c2c66affSColin Finck goto end;
3430c2c66affSColin Finck case VT_UI1:
3431c2c66affSColin Finck case VT_I2:
3432c2c66affSColin Finck case VT_I4:
3433c2c66affSColin Finck case VT_I8:
3434c2c66affSColin Finck tvt = VT_I8;
3435c2c66affSColin Finck break;
3436c2c66affSColin Finck case VT_R4:
3437c2c66affSColin Finck tvt = VT_R8;
3438c2c66affSColin Finck break;
3439c2c66affSColin Finck default:
3440c2c66affSColin Finck tvt = resvt;
3441c2c66affSColin Finck }
3442c2c66affSColin Finck
3443c2c66affSColin Finck /* Now coerce the variants */
3444c2c66affSColin Finck hres = VariantChangeType(&lv, left, 0, tvt);
3445c2c66affSColin Finck if (FAILED(hres))
3446c2c66affSColin Finck goto end;
3447c2c66affSColin Finck hres = VariantChangeType(&rv, right, 0, tvt);
3448c2c66affSColin Finck if (FAILED(hres))
3449c2c66affSColin Finck goto end;
3450c2c66affSColin Finck
3451c2c66affSColin Finck /* Do the math */
3452c2c66affSColin Finck hres = S_OK;
3453c2c66affSColin Finck V_VT(&tv) = tvt;
3454c2c66affSColin Finck V_VT(result) = resvt;
3455c2c66affSColin Finck switch (tvt) {
3456c2c66affSColin Finck case VT_DECIMAL:
3457c2c66affSColin Finck hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
3458c2c66affSColin Finck &V_DECIMAL(result));
3459c2c66affSColin Finck goto end;
3460c2c66affSColin Finck case VT_CY:
3461c2c66affSColin Finck hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
3462c2c66affSColin Finck goto end;
3463c2c66affSColin Finck case VT_I8:
3464c2c66affSColin Finck /* Overflow detection */
3465c2c66affSColin Finck r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
3466c2c66affSColin Finck if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
3467c2c66affSColin Finck V_VT(result) = VT_R8;
3468c2c66affSColin Finck V_R8(result) = r8res;
3469c2c66affSColin Finck goto end;
3470c2c66affSColin Finck } else
3471c2c66affSColin Finck V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
3472c2c66affSColin Finck break;
3473c2c66affSColin Finck case VT_R8:
3474c2c66affSColin Finck /* FIXME: overflow detection */
3475c2c66affSColin Finck V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
3476c2c66affSColin Finck break;
3477c2c66affSColin Finck default:
3478c2c66affSColin Finck ERR("We shouldn't get here! tvt = %d!\n", tvt);
3479c2c66affSColin Finck break;
3480c2c66affSColin Finck }
3481c2c66affSColin Finck if (resvt != tvt) {
3482c2c66affSColin Finck while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3483c2c66affSColin Finck /* Overflow! Change to the vartype with the next higher priority.
3484c2c66affSColin Finck With one exception: I4 ==> R8 even if it would fit in I8 */
3485c2c66affSColin Finck if (resvt == VT_I4)
3486c2c66affSColin Finck resvt = VT_R8;
3487c2c66affSColin Finck else
3488c2c66affSColin Finck resvt = prio2vt[coerce[resvt] + 1];
3489c2c66affSColin Finck }
3490c2c66affSColin Finck } else
3491c2c66affSColin Finck hres = VariantCopy(result, &tv);
3492c2c66affSColin Finck
3493c2c66affSColin Finck end:
3494c2c66affSColin Finck if (hres != S_OK) {
3495c2c66affSColin Finck V_VT(result) = VT_EMPTY;
3496c2c66affSColin Finck V_I4(result) = 0; /* No V_EMPTY */
3497c2c66affSColin Finck }
3498c2c66affSColin Finck VariantClear(&lv);
3499c2c66affSColin Finck VariantClear(&rv);
3500c2c66affSColin Finck VariantClear(&tv);
3501c2c66affSColin Finck VariantClear(&tempLeft);
3502c2c66affSColin Finck VariantClear(&tempRight);
3503c2c66affSColin Finck TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3504c2c66affSColin Finck return hres;
3505c2c66affSColin Finck }
3506c2c66affSColin Finck
3507c2c66affSColin Finck /**********************************************************************
3508c2c66affSColin Finck * VarDiv [OLEAUT32.143]
3509c2c66affSColin Finck *
3510c2c66affSColin Finck * Divides one variant with another.
3511c2c66affSColin Finck *
3512c2c66affSColin Finck * PARAMS
3513c2c66affSColin Finck * left [I] First variant
3514c2c66affSColin Finck * right [I] Second variant
3515c2c66affSColin Finck * result [O] Result variant
3516c2c66affSColin Finck *
3517c2c66affSColin Finck * RETURNS
3518c2c66affSColin Finck * Success: S_OK.
3519c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
3520c2c66affSColin Finck */
VarDiv(LPVARIANT left,LPVARIANT right,LPVARIANT result)3521c2c66affSColin Finck HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3522c2c66affSColin Finck {
3523c2c66affSColin Finck HRESULT hres = S_OK;
3524c2c66affSColin Finck VARTYPE resvt = VT_EMPTY;
3525c2c66affSColin Finck VARTYPE leftvt,rightvt;
3526c2c66affSColin Finck VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3527c2c66affSColin Finck VARIANT lv,rv;
3528c2c66affSColin Finck VARIANT tempLeft, tempRight;
3529c2c66affSColin Finck
3530c2c66affSColin Finck VariantInit(&tempLeft);
3531c2c66affSColin Finck VariantInit(&tempRight);
3532c2c66affSColin Finck VariantInit(&lv);
3533c2c66affSColin Finck VariantInit(&rv);
3534c2c66affSColin Finck
3535c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3536c2c66affSColin Finck
3537c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
3538c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
3539c2c66affSColin Finck {
3540c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3541c2c66affSColin Finck if (FAILED(hres)) goto end;
3542c2c66affSColin Finck left = &tempLeft;
3543c2c66affSColin Finck }
3544c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
3545c2c66affSColin Finck {
3546c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(right, &tempRight);
3547c2c66affSColin Finck if (FAILED(hres)) goto end;
3548c2c66affSColin Finck right = &tempRight;
3549c2c66affSColin Finck }
3550c2c66affSColin Finck
3551c2c66affSColin Finck leftvt = V_VT(left)&VT_TYPEMASK;
3552c2c66affSColin Finck rightvt = V_VT(right)&VT_TYPEMASK;
3553c2c66affSColin Finck leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3554c2c66affSColin Finck rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3555c2c66affSColin Finck
3556c2c66affSColin Finck if (leftExtraFlags != rightExtraFlags)
3557c2c66affSColin Finck {
3558c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3559c2c66affSColin Finck goto end;
3560c2c66affSColin Finck }
3561c2c66affSColin Finck ExtraFlags = leftExtraFlags;
3562c2c66affSColin Finck
3563c2c66affSColin Finck /* Native VarDiv always returns an error when using extra flags */
3564c2c66affSColin Finck if (ExtraFlags != 0)
3565c2c66affSColin Finck {
3566c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3567c2c66affSColin Finck goto end;
3568c2c66affSColin Finck }
3569c2c66affSColin Finck
3570c2c66affSColin Finck /* Determine return type */
3571c2c66affSColin Finck if (rightvt != VT_EMPTY)
3572c2c66affSColin Finck {
3573c2c66affSColin Finck if (leftvt == VT_NULL || rightvt == VT_NULL)
3574c2c66affSColin Finck {
3575c2c66affSColin Finck V_VT(result) = VT_NULL;
3576c2c66affSColin Finck hres = S_OK;
3577c2c66affSColin Finck goto end;
3578c2c66affSColin Finck }
3579c2c66affSColin Finck else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3580c2c66affSColin Finck resvt = VT_DECIMAL;
3581c2c66affSColin Finck else if (leftvt == VT_I8 || rightvt == VT_I8 ||
3582c2c66affSColin Finck leftvt == VT_CY || rightvt == VT_CY ||
3583c2c66affSColin Finck leftvt == VT_DATE || rightvt == VT_DATE ||
3584c2c66affSColin Finck leftvt == VT_I4 || rightvt == VT_I4 ||
3585c2c66affSColin Finck leftvt == VT_BSTR || rightvt == VT_BSTR ||
3586c2c66affSColin Finck leftvt == VT_I2 || rightvt == VT_I2 ||
3587c2c66affSColin Finck leftvt == VT_BOOL || rightvt == VT_BOOL ||
3588c2c66affSColin Finck leftvt == VT_R8 || rightvt == VT_R8 ||
3589c2c66affSColin Finck leftvt == VT_UI1 || rightvt == VT_UI1)
3590c2c66affSColin Finck {
3591c2c66affSColin Finck if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
3592c2c66affSColin Finck (leftvt == VT_R4 && rightvt == VT_UI1))
3593c2c66affSColin Finck resvt = VT_R4;
3594c2c66affSColin Finck else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
3595c2c66affSColin Finck rightvt == VT_I2)) || (rightvt == VT_R4 &&
3596c2c66affSColin Finck (leftvt == VT_BOOL || leftvt == VT_I2)))
3597c2c66affSColin Finck resvt = VT_R4;
3598c2c66affSColin Finck else
3599c2c66affSColin Finck resvt = VT_R8;
3600c2c66affSColin Finck }
3601c2c66affSColin Finck else if (leftvt == VT_R4 || rightvt == VT_R4)
3602c2c66affSColin Finck resvt = VT_R4;
3603c2c66affSColin Finck }
3604c2c66affSColin Finck else if (leftvt == VT_NULL)
3605c2c66affSColin Finck {
3606c2c66affSColin Finck V_VT(result) = VT_NULL;
3607c2c66affSColin Finck hres = S_OK;
3608c2c66affSColin Finck goto end;
3609c2c66affSColin Finck }
3610c2c66affSColin Finck else
3611c2c66affSColin Finck {
3612c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3613c2c66affSColin Finck goto end;
3614c2c66affSColin Finck }
3615c2c66affSColin Finck
3616c2c66affSColin Finck /* coerce to the result type */
3617c2c66affSColin Finck hres = VariantChangeType(&lv, left, 0, resvt);
3618c2c66affSColin Finck if (hres != S_OK) goto end;
3619c2c66affSColin Finck
3620c2c66affSColin Finck hres = VariantChangeType(&rv, right, 0, resvt);
3621c2c66affSColin Finck if (hres != S_OK) goto end;
3622c2c66affSColin Finck
3623c2c66affSColin Finck /* do the math */
3624c2c66affSColin Finck V_VT(result) = resvt;
3625c2c66affSColin Finck switch (resvt)
3626c2c66affSColin Finck {
3627c2c66affSColin Finck case VT_R4:
3628c2c66affSColin Finck if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
3629c2c66affSColin Finck {
3630c2c66affSColin Finck hres = DISP_E_OVERFLOW;
3631c2c66affSColin Finck V_VT(result) = VT_EMPTY;
3632c2c66affSColin Finck }
3633c2c66affSColin Finck else if (V_R4(&rv) == 0.0)
3634c2c66affSColin Finck {
3635c2c66affSColin Finck hres = DISP_E_DIVBYZERO;
3636c2c66affSColin Finck V_VT(result) = VT_EMPTY;
3637c2c66affSColin Finck }
3638c2c66affSColin Finck else
3639c2c66affSColin Finck V_R4(result) = V_R4(&lv) / V_R4(&rv);
3640c2c66affSColin Finck break;
3641c2c66affSColin Finck case VT_R8:
3642c2c66affSColin Finck if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
3643c2c66affSColin Finck {
3644c2c66affSColin Finck hres = DISP_E_OVERFLOW;
3645c2c66affSColin Finck V_VT(result) = VT_EMPTY;
3646c2c66affSColin Finck }
3647c2c66affSColin Finck else if (V_R8(&rv) == 0.0)
3648c2c66affSColin Finck {
3649c2c66affSColin Finck hres = DISP_E_DIVBYZERO;
3650c2c66affSColin Finck V_VT(result) = VT_EMPTY;
3651c2c66affSColin Finck }
3652c2c66affSColin Finck else
3653c2c66affSColin Finck V_R8(result) = V_R8(&lv) / V_R8(&rv);
3654c2c66affSColin Finck break;
3655c2c66affSColin Finck case VT_DECIMAL:
3656c2c66affSColin Finck hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3657c2c66affSColin Finck break;
3658c2c66affSColin Finck }
3659c2c66affSColin Finck
3660c2c66affSColin Finck end:
3661c2c66affSColin Finck VariantClear(&lv);
3662c2c66affSColin Finck VariantClear(&rv);
3663c2c66affSColin Finck VariantClear(&tempLeft);
3664c2c66affSColin Finck VariantClear(&tempRight);
3665c2c66affSColin Finck TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3666c2c66affSColin Finck return hres;
3667c2c66affSColin Finck }
3668c2c66affSColin Finck
3669c2c66affSColin Finck /**********************************************************************
3670c2c66affSColin Finck * VarSub [OLEAUT32.159]
3671c2c66affSColin Finck *
3672c2c66affSColin Finck * Subtract two variants.
3673c2c66affSColin Finck *
3674c2c66affSColin Finck * PARAMS
3675c2c66affSColin Finck * left [I] First variant
3676c2c66affSColin Finck * right [I] Second variant
3677c2c66affSColin Finck * result [O] Result variant
3678c2c66affSColin Finck *
3679c2c66affSColin Finck * RETURNS
3680c2c66affSColin Finck * Success: S_OK.
3681c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
3682c2c66affSColin Finck */
VarSub(LPVARIANT left,LPVARIANT right,LPVARIANT result)3683c2c66affSColin Finck HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
3684c2c66affSColin Finck {
3685c2c66affSColin Finck HRESULT hres = S_OK;
3686c2c66affSColin Finck VARTYPE resvt = VT_EMPTY;
3687c2c66affSColin Finck VARTYPE leftvt,rightvt;
3688c2c66affSColin Finck VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3689c2c66affSColin Finck VARIANT lv,rv;
3690c2c66affSColin Finck VARIANT tempLeft, tempRight;
3691c2c66affSColin Finck
3692c2c66affSColin Finck VariantInit(&lv);
3693c2c66affSColin Finck VariantInit(&rv);
3694c2c66affSColin Finck VariantInit(&tempLeft);
3695c2c66affSColin Finck VariantInit(&tempRight);
3696c2c66affSColin Finck
3697c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
3698c2c66affSColin Finck
3699c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH &&
3700c2c66affSColin Finck (V_VT(left)&(~VT_TYPEMASK)) == 0 &&
3701c2c66affSColin Finck (V_VT(right) & VT_TYPEMASK) != VT_NULL)
3702c2c66affSColin Finck {
3703c2c66affSColin Finck if (NULL == V_DISPATCH(left)) {
3704c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) >= VT_INT_PTR)
3705c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3706c2c66affSColin Finck else if ((V_VT(right) & VT_TYPEMASK) >= VT_UI8 &&
3707c2c66affSColin Finck (V_VT(right) & VT_TYPEMASK) < VT_RECORD)
3708c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3709c2c66affSColin Finck else switch (V_VT(right) & VT_TYPEMASK)
3710c2c66affSColin Finck {
3711c2c66affSColin Finck case VT_VARIANT:
3712c2c66affSColin Finck case VT_UNKNOWN:
3713c2c66affSColin Finck case 15:
3714c2c66affSColin Finck case VT_I1:
3715c2c66affSColin Finck case VT_UI2:
3716c2c66affSColin Finck case VT_UI4:
3717c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3718c2c66affSColin Finck }
3719c2c66affSColin Finck if (FAILED(hres)) goto end;
3720c2c66affSColin Finck }
3721c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(left, &tempLeft);
3722c2c66affSColin Finck if (FAILED(hres)) goto end;
3723c2c66affSColin Finck left = &tempLeft;
3724c2c66affSColin Finck }
3725c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH &&
3726c2c66affSColin Finck (V_VT(right)&(~VT_TYPEMASK)) == 0 &&
3727c2c66affSColin Finck (V_VT(left) & VT_TYPEMASK) != VT_NULL)
3728c2c66affSColin Finck {
3729c2c66affSColin Finck if (NULL == V_DISPATCH(right))
3730c2c66affSColin Finck {
3731c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) >= VT_INT_PTR)
3732c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3733c2c66affSColin Finck else if ((V_VT(left) & VT_TYPEMASK) >= VT_UI8 &&
3734c2c66affSColin Finck (V_VT(left) & VT_TYPEMASK) < VT_RECORD)
3735c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3736c2c66affSColin Finck else switch (V_VT(left) & VT_TYPEMASK)
3737c2c66affSColin Finck {
3738c2c66affSColin Finck case VT_VARIANT:
3739c2c66affSColin Finck case VT_UNKNOWN:
3740c2c66affSColin Finck case 15:
3741c2c66affSColin Finck case VT_I1:
3742c2c66affSColin Finck case VT_UI2:
3743c2c66affSColin Finck case VT_UI4:
3744c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3745c2c66affSColin Finck }
3746c2c66affSColin Finck if (FAILED(hres)) goto end;
3747c2c66affSColin Finck }
3748c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(right, &tempRight);
3749c2c66affSColin Finck if (FAILED(hres)) goto end;
3750c2c66affSColin Finck right = &tempRight;
3751c2c66affSColin Finck }
3752c2c66affSColin Finck
3753c2c66affSColin Finck leftvt = V_VT(left)&VT_TYPEMASK;
3754c2c66affSColin Finck rightvt = V_VT(right)&VT_TYPEMASK;
3755c2c66affSColin Finck leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
3756c2c66affSColin Finck rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
3757c2c66affSColin Finck
3758c2c66affSColin Finck if (leftExtraFlags != rightExtraFlags)
3759c2c66affSColin Finck {
3760c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3761c2c66affSColin Finck goto end;
3762c2c66affSColin Finck }
3763c2c66affSColin Finck ExtraFlags = leftExtraFlags;
3764c2c66affSColin Finck
3765c2c66affSColin Finck /* determine return type and return code */
3766c2c66affSColin Finck /* All extra flags produce errors */
3767c2c66affSColin Finck if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
3768c2c66affSColin Finck ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
3769c2c66affSColin Finck ExtraFlags == (VT_VECTOR|VT_BYREF) ||
3770c2c66affSColin Finck ExtraFlags == (VT_BYREF|VT_RESERVED) ||
3771c2c66affSColin Finck ExtraFlags == VT_VECTOR ||
3772c2c66affSColin Finck ExtraFlags == VT_BYREF ||
3773c2c66affSColin Finck ExtraFlags == VT_RESERVED)
3774c2c66affSColin Finck {
3775c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3776c2c66affSColin Finck goto end;
3777c2c66affSColin Finck }
3778c2c66affSColin Finck else if (ExtraFlags >= VT_ARRAY)
3779c2c66affSColin Finck {
3780c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3781c2c66affSColin Finck goto end;
3782c2c66affSColin Finck }
3783c2c66affSColin Finck /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
3784c2c66affSColin Finck VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
3785c2c66affSColin Finck else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
3786c2c66affSColin Finck leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
3787c2c66affSColin Finck leftvt == VT_I1 || rightvt == VT_I1 ||
3788c2c66affSColin Finck leftvt == VT_UI2 || rightvt == VT_UI2 ||
3789c2c66affSColin Finck leftvt == VT_UI4 || rightvt == VT_UI4 ||
3790c2c66affSColin Finck leftvt == VT_UI8 || rightvt == VT_UI8 ||
3791c2c66affSColin Finck leftvt == VT_INT || rightvt == VT_INT ||
3792c2c66affSColin Finck leftvt == VT_UINT || rightvt == VT_UINT ||
3793c2c66affSColin Finck leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
3794c2c66affSColin Finck leftvt == VT_RECORD || rightvt == VT_RECORD)
3795c2c66affSColin Finck {
3796c2c66affSColin Finck if (leftvt == VT_RECORD && rightvt == VT_I8)
3797c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3798c2c66affSColin Finck else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
3799c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3800c2c66affSColin Finck else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
3801c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3802c2c66affSColin Finck else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
3803c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3804c2c66affSColin Finck else if (leftvt == VT_RECORD && rightvt > VT_UI1)
3805c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3806c2c66affSColin Finck else
3807c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3808c2c66affSColin Finck goto end;
3809c2c66affSColin Finck }
3810c2c66affSColin Finck /* The following flags/types are invalid for left variant */
3811c2c66affSColin Finck else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
3812c2c66affSColin Finck leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
3813c2c66affSColin Finck (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
3814c2c66affSColin Finck {
3815c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3816c2c66affSColin Finck goto end;
3817c2c66affSColin Finck }
3818c2c66affSColin Finck /* The following flags/types are invalid for right variant */
3819c2c66affSColin Finck else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
3820c2c66affSColin Finck rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
3821c2c66affSColin Finck (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
3822c2c66affSColin Finck {
3823c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
3824c2c66affSColin Finck goto end;
3825c2c66affSColin Finck }
3826c2c66affSColin Finck else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
3827c2c66affSColin Finck (leftvt == VT_DISPATCH && rightvt == VT_NULL))
3828c2c66affSColin Finck resvt = VT_NULL;
3829c2c66affSColin Finck else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
3830c2c66affSColin Finck leftvt == VT_ERROR || rightvt == VT_ERROR)
3831c2c66affSColin Finck {
3832c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3833c2c66affSColin Finck goto end;
3834c2c66affSColin Finck }
3835c2c66affSColin Finck else if (leftvt == VT_NULL || rightvt == VT_NULL)
3836c2c66affSColin Finck resvt = VT_NULL;
3837c2c66affSColin Finck else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
3838c2c66affSColin Finck (leftvt == VT_DATE && rightvt == VT_DATE) ||
3839c2c66affSColin Finck (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
3840c2c66affSColin Finck (leftvt == VT_BSTR && rightvt == VT_BSTR))
3841c2c66affSColin Finck resvt = VT_R8;
3842c2c66affSColin Finck else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
3843c2c66affSColin Finck resvt = VT_DECIMAL;
3844c2c66affSColin Finck else if (leftvt == VT_DATE || rightvt == VT_DATE)
3845c2c66affSColin Finck resvt = VT_DATE;
3846c2c66affSColin Finck else if (leftvt == VT_CY || rightvt == VT_CY)
3847c2c66affSColin Finck resvt = VT_CY;
3848c2c66affSColin Finck else if (leftvt == VT_R8 || rightvt == VT_R8)
3849c2c66affSColin Finck resvt = VT_R8;
3850c2c66affSColin Finck else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
3851c2c66affSColin Finck resvt = VT_R8;
3852c2c66affSColin Finck else if (leftvt == VT_R4 || rightvt == VT_R4)
3853c2c66affSColin Finck {
3854c2c66affSColin Finck if (leftvt == VT_I4 || rightvt == VT_I4 ||
3855c2c66affSColin Finck leftvt == VT_I8 || rightvt == VT_I8)
3856c2c66affSColin Finck resvt = VT_R8;
3857c2c66affSColin Finck else
3858c2c66affSColin Finck resvt = VT_R4;
3859c2c66affSColin Finck }
3860c2c66affSColin Finck else if (leftvt == VT_I8 || rightvt == VT_I8)
3861c2c66affSColin Finck resvt = VT_I8;
3862c2c66affSColin Finck else if (leftvt == VT_I4 || rightvt == VT_I4)
3863c2c66affSColin Finck resvt = VT_I4;
3864c2c66affSColin Finck else if (leftvt == VT_I2 || rightvt == VT_I2 ||
3865c2c66affSColin Finck leftvt == VT_BOOL || rightvt == VT_BOOL ||
3866c2c66affSColin Finck (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
3867c2c66affSColin Finck resvt = VT_I2;
3868c2c66affSColin Finck else if (leftvt == VT_UI1 || rightvt == VT_UI1)
3869c2c66affSColin Finck resvt = VT_UI1;
3870c2c66affSColin Finck else
3871c2c66affSColin Finck {
3872c2c66affSColin Finck hres = DISP_E_TYPEMISMATCH;
3873c2c66affSColin Finck goto end;
3874c2c66affSColin Finck }
3875c2c66affSColin Finck
3876c2c66affSColin Finck /* coerce to the result type */
3877c2c66affSColin Finck if (leftvt == VT_BSTR && rightvt == VT_DATE)
3878c2c66affSColin Finck hres = VariantChangeType(&lv, left, 0, VT_R8);
3879c2c66affSColin Finck else
3880c2c66affSColin Finck hres = VariantChangeType(&lv, left, 0, resvt);
3881c2c66affSColin Finck if (hres != S_OK) goto end;
3882c2c66affSColin Finck if (leftvt == VT_DATE && rightvt == VT_BSTR)
3883c2c66affSColin Finck hres = VariantChangeType(&rv, right, 0, VT_R8);
3884c2c66affSColin Finck else
3885c2c66affSColin Finck hres = VariantChangeType(&rv, right, 0, resvt);
3886c2c66affSColin Finck if (hres != S_OK) goto end;
3887c2c66affSColin Finck
3888c2c66affSColin Finck /* do the math */
3889c2c66affSColin Finck V_VT(result) = resvt;
3890c2c66affSColin Finck switch (resvt)
3891c2c66affSColin Finck {
3892c2c66affSColin Finck case VT_NULL:
3893c2c66affSColin Finck break;
3894c2c66affSColin Finck case VT_DATE:
3895c2c66affSColin Finck V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
3896c2c66affSColin Finck break;
3897c2c66affSColin Finck case VT_CY:
3898c2c66affSColin Finck hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
3899c2c66affSColin Finck break;
3900c2c66affSColin Finck case VT_R4:
3901c2c66affSColin Finck V_R4(result) = V_R4(&lv) - V_R4(&rv);
3902c2c66affSColin Finck break;
3903c2c66affSColin Finck case VT_I8:
3904c2c66affSColin Finck V_I8(result) = V_I8(&lv) - V_I8(&rv);
3905c2c66affSColin Finck break;
3906c2c66affSColin Finck case VT_I4:
3907c2c66affSColin Finck V_I4(result) = V_I4(&lv) - V_I4(&rv);
3908c2c66affSColin Finck break;
3909c2c66affSColin Finck case VT_I2:
3910c2c66affSColin Finck V_I2(result) = V_I2(&lv) - V_I2(&rv);
3911c2c66affSColin Finck break;
3912c2c66affSColin Finck case VT_UI1:
3913c2c66affSColin Finck V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
3914c2c66affSColin Finck break;
3915c2c66affSColin Finck case VT_R8:
3916c2c66affSColin Finck V_R8(result) = V_R8(&lv) - V_R8(&rv);
3917c2c66affSColin Finck break;
3918c2c66affSColin Finck case VT_DECIMAL:
3919c2c66affSColin Finck hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
3920c2c66affSColin Finck break;
3921c2c66affSColin Finck }
3922c2c66affSColin Finck
3923c2c66affSColin Finck end:
3924c2c66affSColin Finck VariantClear(&lv);
3925c2c66affSColin Finck VariantClear(&rv);
3926c2c66affSColin Finck VariantClear(&tempLeft);
3927c2c66affSColin Finck VariantClear(&tempRight);
3928c2c66affSColin Finck TRACE("returning 0x%8x %s\n", hres, debugstr_variant(result));
3929c2c66affSColin Finck return hres;
3930c2c66affSColin Finck }
3931c2c66affSColin Finck
3932c2c66affSColin Finck
3933c2c66affSColin Finck /**********************************************************************
3934c2c66affSColin Finck * VarOr [OLEAUT32.157]
3935c2c66affSColin Finck *
3936c2c66affSColin Finck * Perform a logical or (OR) operation on two variants.
3937c2c66affSColin Finck *
3938c2c66affSColin Finck * PARAMS
3939c2c66affSColin Finck * pVarLeft [I] First variant
3940c2c66affSColin Finck * pVarRight [I] Variant to OR with pVarLeft
3941c2c66affSColin Finck * pVarOut [O] Destination for OR result
3942c2c66affSColin Finck *
3943c2c66affSColin Finck * RETURNS
3944c2c66affSColin Finck * Success: S_OK. pVarOut contains the result of the operation with its type
3945c2c66affSColin Finck * taken from the table listed under VarXor().
3946c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
3947c2c66affSColin Finck *
3948c2c66affSColin Finck * NOTES
3949c2c66affSColin Finck * See the Notes section of VarXor() for further information.
3950c2c66affSColin Finck */
VarOr(LPVARIANT pVarLeft,LPVARIANT pVarRight,LPVARIANT pVarOut)3951c2c66affSColin Finck HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
3952c2c66affSColin Finck {
3953c2c66affSColin Finck VARTYPE vt = VT_I4;
3954c2c66affSColin Finck VARIANT varLeft, varRight, varStr;
3955c2c66affSColin Finck HRESULT hRet;
3956c2c66affSColin Finck VARIANT tempLeft, tempRight;
3957c2c66affSColin Finck
3958c2c66affSColin Finck VariantInit(&tempLeft);
3959c2c66affSColin Finck VariantInit(&tempRight);
3960c2c66affSColin Finck VariantInit(&varLeft);
3961c2c66affSColin Finck VariantInit(&varRight);
3962c2c66affSColin Finck VariantInit(&varStr);
3963c2c66affSColin Finck
3964c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
3965c2c66affSColin Finck
3966c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
3967c2c66affSColin Finck if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
3968c2c66affSColin Finck {
3969c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
3970c2c66affSColin Finck if (FAILED(hRet)) goto VarOr_Exit;
3971c2c66affSColin Finck pVarLeft = &tempLeft;
3972c2c66affSColin Finck }
3973c2c66affSColin Finck if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
3974c2c66affSColin Finck {
3975c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
3976c2c66affSColin Finck if (FAILED(hRet)) goto VarOr_Exit;
3977c2c66affSColin Finck pVarRight = &tempRight;
3978c2c66affSColin Finck }
3979c2c66affSColin Finck
3980c2c66affSColin Finck if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
3981c2c66affSColin Finck V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
3982c2c66affSColin Finck V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
3983c2c66affSColin Finck V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
3984c2c66affSColin Finck {
3985c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
3986c2c66affSColin Finck goto VarOr_Exit;
3987c2c66affSColin Finck }
3988c2c66affSColin Finck
3989c2c66affSColin Finck V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
3990c2c66affSColin Finck
3991c2c66affSColin Finck if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
3992c2c66affSColin Finck {
3993c2c66affSColin Finck /* NULL OR Zero is NULL, NULL OR value is value */
3994c2c66affSColin Finck if (V_VT(pVarLeft) == VT_NULL)
3995c2c66affSColin Finck pVarLeft = pVarRight; /* point to the non-NULL var */
3996c2c66affSColin Finck
3997c2c66affSColin Finck V_VT(pVarOut) = VT_NULL;
3998c2c66affSColin Finck V_I4(pVarOut) = 0;
3999c2c66affSColin Finck
4000c2c66affSColin Finck switch (V_VT(pVarLeft))
4001c2c66affSColin Finck {
4002c2c66affSColin Finck case VT_DATE: case VT_R8:
4003c2c66affSColin Finck if (V_R8(pVarLeft))
4004c2c66affSColin Finck goto VarOr_AsEmpty;
4005c2c66affSColin Finck hRet = S_OK;
4006c2c66affSColin Finck goto VarOr_Exit;
4007c2c66affSColin Finck case VT_BOOL:
4008c2c66affSColin Finck if (V_BOOL(pVarLeft))
4009c2c66affSColin Finck *pVarOut = *pVarLeft;
4010c2c66affSColin Finck hRet = S_OK;
4011c2c66affSColin Finck goto VarOr_Exit;
4012c2c66affSColin Finck case VT_I2: case VT_UI2:
4013c2c66affSColin Finck if (V_I2(pVarLeft))
4014c2c66affSColin Finck goto VarOr_AsEmpty;
4015c2c66affSColin Finck hRet = S_OK;
4016c2c66affSColin Finck goto VarOr_Exit;
4017c2c66affSColin Finck case VT_I1:
4018c2c66affSColin Finck if (V_I1(pVarLeft))
4019c2c66affSColin Finck goto VarOr_AsEmpty;
4020c2c66affSColin Finck hRet = S_OK;
4021c2c66affSColin Finck goto VarOr_Exit;
4022c2c66affSColin Finck case VT_UI1:
4023c2c66affSColin Finck if (V_UI1(pVarLeft))
4024c2c66affSColin Finck *pVarOut = *pVarLeft;
4025c2c66affSColin Finck hRet = S_OK;
4026c2c66affSColin Finck goto VarOr_Exit;
4027c2c66affSColin Finck case VT_R4:
4028c2c66affSColin Finck if (V_R4(pVarLeft))
4029c2c66affSColin Finck goto VarOr_AsEmpty;
4030c2c66affSColin Finck hRet = S_OK;
4031c2c66affSColin Finck goto VarOr_Exit;
4032c2c66affSColin Finck case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
4033c2c66affSColin Finck if (V_I4(pVarLeft))
4034c2c66affSColin Finck goto VarOr_AsEmpty;
4035c2c66affSColin Finck hRet = S_OK;
4036c2c66affSColin Finck goto VarOr_Exit;
4037c2c66affSColin Finck case VT_CY:
4038c2c66affSColin Finck if (V_CY(pVarLeft).int64)
4039c2c66affSColin Finck goto VarOr_AsEmpty;
4040c2c66affSColin Finck hRet = S_OK;
4041c2c66affSColin Finck goto VarOr_Exit;
4042c2c66affSColin Finck case VT_I8: case VT_UI8:
4043c2c66affSColin Finck if (V_I8(pVarLeft))
4044c2c66affSColin Finck goto VarOr_AsEmpty;
4045c2c66affSColin Finck hRet = S_OK;
4046c2c66affSColin Finck goto VarOr_Exit;
4047c2c66affSColin Finck case VT_DECIMAL:
4048c2c66affSColin Finck if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
4049c2c66affSColin Finck goto VarOr_AsEmpty;
4050c2c66affSColin Finck hRet = S_OK;
4051c2c66affSColin Finck goto VarOr_Exit;
4052c2c66affSColin Finck case VT_BSTR:
4053c2c66affSColin Finck {
4054c2c66affSColin Finck VARIANT_BOOL b;
4055c2c66affSColin Finck
4056c2c66affSColin Finck if (!V_BSTR(pVarLeft))
4057c2c66affSColin Finck {
4058c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
4059c2c66affSColin Finck goto VarOr_Exit;
4060c2c66affSColin Finck }
4061c2c66affSColin Finck
4062c2c66affSColin Finck hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
4063c2c66affSColin Finck if (SUCCEEDED(hRet) && b)
4064c2c66affSColin Finck {
4065c2c66affSColin Finck V_VT(pVarOut) = VT_BOOL;
4066c2c66affSColin Finck V_BOOL(pVarOut) = b;
4067c2c66affSColin Finck }
4068c2c66affSColin Finck goto VarOr_Exit;
4069c2c66affSColin Finck }
4070c2c66affSColin Finck case VT_NULL: case VT_EMPTY:
4071c2c66affSColin Finck V_VT(pVarOut) = VT_NULL;
4072c2c66affSColin Finck hRet = S_OK;
4073c2c66affSColin Finck goto VarOr_Exit;
4074c2c66affSColin Finck default:
4075c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
4076c2c66affSColin Finck goto VarOr_Exit;
4077c2c66affSColin Finck }
4078c2c66affSColin Finck }
4079c2c66affSColin Finck
4080c2c66affSColin Finck if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
4081c2c66affSColin Finck {
4082c2c66affSColin Finck if (V_VT(pVarLeft) == VT_EMPTY)
4083c2c66affSColin Finck pVarLeft = pVarRight; /* point to the non-EMPTY var */
4084c2c66affSColin Finck
4085c2c66affSColin Finck VarOr_AsEmpty:
4086c2c66affSColin Finck /* Since one argument is empty (0), OR'ing it with the other simply
4087c2c66affSColin Finck * gives the others value (as 0|x => x). So just convert the other
4088c2c66affSColin Finck * argument to the required result type.
4089c2c66affSColin Finck */
4090c2c66affSColin Finck switch (V_VT(pVarLeft))
4091c2c66affSColin Finck {
4092c2c66affSColin Finck case VT_BSTR:
4093c2c66affSColin Finck if (!V_BSTR(pVarLeft))
4094c2c66affSColin Finck {
4095c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
4096c2c66affSColin Finck goto VarOr_Exit;
4097c2c66affSColin Finck }
4098c2c66affSColin Finck
4099c2c66affSColin Finck hRet = VariantCopy(&varStr, pVarLeft);
4100c2c66affSColin Finck if (FAILED(hRet))
4101c2c66affSColin Finck goto VarOr_Exit;
4102c2c66affSColin Finck pVarLeft = &varStr;
4103c2c66affSColin Finck hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4104c2c66affSColin Finck if (FAILED(hRet))
4105c2c66affSColin Finck goto VarOr_Exit;
4106c2c66affSColin Finck /* Fall Through ... */
4107c2c66affSColin Finck case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
4108c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4109c2c66affSColin Finck break;
4110c2c66affSColin Finck case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
4111c2c66affSColin Finck case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
4112c2c66affSColin Finck case VT_INT: case VT_UINT: case VT_UI8:
4113c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4114c2c66affSColin Finck break;
4115c2c66affSColin Finck case VT_I8:
4116c2c66affSColin Finck V_VT(pVarOut) = VT_I8;
4117c2c66affSColin Finck break;
4118c2c66affSColin Finck default:
4119c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
4120c2c66affSColin Finck goto VarOr_Exit;
4121c2c66affSColin Finck }
4122c2c66affSColin Finck hRet = VariantCopy(&varLeft, pVarLeft);
4123c2c66affSColin Finck if (FAILED(hRet))
4124c2c66affSColin Finck goto VarOr_Exit;
4125c2c66affSColin Finck pVarLeft = &varLeft;
4126c2c66affSColin Finck hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
4127c2c66affSColin Finck goto VarOr_Exit;
4128c2c66affSColin Finck }
4129c2c66affSColin Finck
4130c2c66affSColin Finck if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
4131c2c66affSColin Finck {
4132c2c66affSColin Finck V_VT(pVarOut) = VT_BOOL;
4133c2c66affSColin Finck V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
4134c2c66affSColin Finck hRet = S_OK;
4135c2c66affSColin Finck goto VarOr_Exit;
4136c2c66affSColin Finck }
4137c2c66affSColin Finck
4138c2c66affSColin Finck if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
4139c2c66affSColin Finck {
4140c2c66affSColin Finck V_VT(pVarOut) = VT_UI1;
4141c2c66affSColin Finck V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
4142c2c66affSColin Finck hRet = S_OK;
4143c2c66affSColin Finck goto VarOr_Exit;
4144c2c66affSColin Finck }
4145c2c66affSColin Finck
4146c2c66affSColin Finck if (V_VT(pVarLeft) == VT_BSTR)
4147c2c66affSColin Finck {
4148c2c66affSColin Finck hRet = VariantCopy(&varStr, pVarLeft);
4149c2c66affSColin Finck if (FAILED(hRet))
4150c2c66affSColin Finck goto VarOr_Exit;
4151c2c66affSColin Finck pVarLeft = &varStr;
4152c2c66affSColin Finck hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
4153c2c66affSColin Finck if (FAILED(hRet))
4154c2c66affSColin Finck goto VarOr_Exit;
4155c2c66affSColin Finck }
4156c2c66affSColin Finck
4157c2c66affSColin Finck if (V_VT(pVarLeft) == VT_BOOL &&
4158c2c66affSColin Finck (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
4159c2c66affSColin Finck {
4160c2c66affSColin Finck vt = VT_BOOL;
4161c2c66affSColin Finck }
4162c2c66affSColin Finck else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
4163c2c66affSColin Finck V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
4164c2c66affSColin Finck (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
4165c2c66affSColin Finck V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
4166c2c66affSColin Finck {
4167c2c66affSColin Finck vt = VT_I2;
4168c2c66affSColin Finck }
4169c2c66affSColin Finck else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
4170c2c66affSColin Finck {
4171c2c66affSColin Finck if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4172c2c66affSColin Finck {
4173c2c66affSColin Finck hRet = DISP_E_TYPEMISMATCH;
4174c2c66affSColin Finck goto VarOr_Exit;
4175c2c66affSColin Finck }
4176c2c66affSColin Finck vt = VT_I8;
4177c2c66affSColin Finck }
4178c2c66affSColin Finck
4179c2c66affSColin Finck hRet = VariantCopy(&varLeft, pVarLeft);
4180c2c66affSColin Finck if (FAILED(hRet))
4181c2c66affSColin Finck goto VarOr_Exit;
4182c2c66affSColin Finck
4183c2c66affSColin Finck hRet = VariantCopy(&varRight, pVarRight);
4184c2c66affSColin Finck if (FAILED(hRet))
4185c2c66affSColin Finck goto VarOr_Exit;
4186c2c66affSColin Finck
4187c2c66affSColin Finck if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
4188c2c66affSColin Finck V_VT(&varLeft) = VT_I4; /* Don't overflow */
4189c2c66affSColin Finck else
4190c2c66affSColin Finck {
4191c2c66affSColin Finck double d;
4192c2c66affSColin Finck
4193c2c66affSColin Finck if (V_VT(&varLeft) == VT_BSTR &&
4194c2c66affSColin Finck FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
4195c2c66affSColin Finck hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
4196c2c66affSColin Finck if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
4197c2c66affSColin Finck hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4198c2c66affSColin Finck if (FAILED(hRet))
4199c2c66affSColin Finck goto VarOr_Exit;
4200c2c66affSColin Finck }
4201c2c66affSColin Finck
4202c2c66affSColin Finck if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
4203c2c66affSColin Finck V_VT(&varRight) = VT_I4; /* Don't overflow */
4204c2c66affSColin Finck else
4205c2c66affSColin Finck {
4206c2c66affSColin Finck double d;
4207c2c66affSColin Finck
4208c2c66affSColin Finck if (V_VT(&varRight) == VT_BSTR &&
4209c2c66affSColin Finck FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
4210c2c66affSColin Finck hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
4211c2c66affSColin Finck if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
4212c2c66affSColin Finck hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4213c2c66affSColin Finck if (FAILED(hRet))
4214c2c66affSColin Finck goto VarOr_Exit;
4215c2c66affSColin Finck }
4216c2c66affSColin Finck
4217c2c66affSColin Finck V_VT(pVarOut) = vt;
4218c2c66affSColin Finck if (vt == VT_I8)
4219c2c66affSColin Finck {
4220c2c66affSColin Finck V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
4221c2c66affSColin Finck }
4222c2c66affSColin Finck else if (vt == VT_I4)
4223c2c66affSColin Finck {
4224c2c66affSColin Finck V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
4225c2c66affSColin Finck }
4226c2c66affSColin Finck else
4227c2c66affSColin Finck {
4228c2c66affSColin Finck V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
4229c2c66affSColin Finck }
4230c2c66affSColin Finck
4231c2c66affSColin Finck VarOr_Exit:
4232c2c66affSColin Finck VariantClear(&varStr);
4233c2c66affSColin Finck VariantClear(&varLeft);
4234c2c66affSColin Finck VariantClear(&varRight);
4235c2c66affSColin Finck VariantClear(&tempLeft);
4236c2c66affSColin Finck VariantClear(&tempRight);
4237c2c66affSColin Finck return hRet;
4238c2c66affSColin Finck }
4239c2c66affSColin Finck
4240c2c66affSColin Finck /**********************************************************************
4241c2c66affSColin Finck * VarAbs [OLEAUT32.168]
4242c2c66affSColin Finck *
4243c2c66affSColin Finck * Convert a variant to its absolute value.
4244c2c66affSColin Finck *
4245c2c66affSColin Finck * PARAMS
4246c2c66affSColin Finck * pVarIn [I] Source variant
4247c2c66affSColin Finck * pVarOut [O] Destination for converted value
4248c2c66affSColin Finck *
4249c2c66affSColin Finck * RETURNS
4250c2c66affSColin Finck * Success: S_OK. pVarOut contains the absolute value of pVarIn.
4251c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
4252c2c66affSColin Finck *
4253c2c66affSColin Finck * NOTES
4254c2c66affSColin Finck * - This function does not process by-reference variants.
4255c2c66affSColin Finck * - The type of the value stored in pVarOut depends on the type of pVarIn,
4256c2c66affSColin Finck * according to the following table:
4257c2c66affSColin Finck *| Input Type Output Type
4258c2c66affSColin Finck *| ---------- -----------
4259c2c66affSColin Finck *| VT_BOOL VT_I2
4260c2c66affSColin Finck *| VT_BSTR VT_R8
4261c2c66affSColin Finck *| (All others) Unchanged
4262c2c66affSColin Finck */
VarAbs(LPVARIANT pVarIn,LPVARIANT pVarOut)4263c2c66affSColin Finck HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4264c2c66affSColin Finck {
4265c2c66affSColin Finck VARIANT varIn;
4266c2c66affSColin Finck HRESULT hRet = S_OK;
4267c2c66affSColin Finck VARIANT temp;
4268c2c66affSColin Finck
4269c2c66affSColin Finck VariantInit(&temp);
4270c2c66affSColin Finck
4271c2c66affSColin Finck TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4272c2c66affSColin Finck
4273c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
4274c2c66affSColin Finck if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4275c2c66affSColin Finck {
4276c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4277c2c66affSColin Finck if (FAILED(hRet)) goto VarAbs_Exit;
4278c2c66affSColin Finck pVarIn = &temp;
4279c2c66affSColin Finck }
4280c2c66affSColin Finck
4281c2c66affSColin Finck if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
4282c2c66affSColin Finck V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
4283c2c66affSColin Finck V_VT(pVarIn) == VT_ERROR)
4284c2c66affSColin Finck {
4285c2c66affSColin Finck hRet = DISP_E_TYPEMISMATCH;
4286c2c66affSColin Finck goto VarAbs_Exit;
4287c2c66affSColin Finck }
4288c2c66affSColin Finck *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4289c2c66affSColin Finck
4290c2c66affSColin Finck #define ABS_CASE(typ,min) \
4291c2c66affSColin Finck case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
4292c2c66affSColin Finck else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
4293c2c66affSColin Finck break
4294c2c66affSColin Finck
4295c2c66affSColin Finck switch (V_VT(pVarIn))
4296c2c66affSColin Finck {
4297c2c66affSColin Finck ABS_CASE(I1,I1_MIN);
4298c2c66affSColin Finck case VT_BOOL:
4299c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4300c2c66affSColin Finck /* BOOL->I2, Fall through ... */
4301c2c66affSColin Finck ABS_CASE(I2,I2_MIN);
4302c2c66affSColin Finck case VT_INT:
4303c2c66affSColin Finck ABS_CASE(I4,I4_MIN);
4304c2c66affSColin Finck ABS_CASE(I8,I8_MIN);
4305c2c66affSColin Finck ABS_CASE(R4,R4_MIN);
4306c2c66affSColin Finck case VT_BSTR:
4307c2c66affSColin Finck hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
4308c2c66affSColin Finck if (FAILED(hRet))
4309c2c66affSColin Finck break;
4310c2c66affSColin Finck V_VT(pVarOut) = VT_R8;
4311c2c66affSColin Finck pVarIn = &varIn;
4312c2c66affSColin Finck /* Fall through ... */
4313c2c66affSColin Finck case VT_DATE:
4314c2c66affSColin Finck ABS_CASE(R8,R8_MIN);
4315c2c66affSColin Finck case VT_CY:
4316c2c66affSColin Finck hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
4317c2c66affSColin Finck break;
4318c2c66affSColin Finck case VT_DECIMAL:
4319c2c66affSColin Finck DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
4320c2c66affSColin Finck break;
4321c2c66affSColin Finck case VT_UI1:
4322c2c66affSColin Finck case VT_UI2:
4323c2c66affSColin Finck case VT_UINT:
4324c2c66affSColin Finck case VT_UI4:
4325c2c66affSColin Finck case VT_UI8:
4326c2c66affSColin Finck /* No-Op */
4327c2c66affSColin Finck break;
4328c2c66affSColin Finck case VT_EMPTY:
4329c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4330c2c66affSColin Finck case VT_NULL:
4331c2c66affSColin Finck V_I2(pVarOut) = 0;
4332c2c66affSColin Finck break;
4333c2c66affSColin Finck default:
4334c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
4335c2c66affSColin Finck }
4336c2c66affSColin Finck
4337c2c66affSColin Finck VarAbs_Exit:
4338c2c66affSColin Finck VariantClear(&temp);
4339c2c66affSColin Finck return hRet;
4340c2c66affSColin Finck }
4341c2c66affSColin Finck
4342c2c66affSColin Finck /**********************************************************************
4343c2c66affSColin Finck * VarFix [OLEAUT32.169]
4344c2c66affSColin Finck *
4345c2c66affSColin Finck * Truncate a variants value to a whole number.
4346c2c66affSColin Finck *
4347c2c66affSColin Finck * PARAMS
4348c2c66affSColin Finck * pVarIn [I] Source variant
4349c2c66affSColin Finck * pVarOut [O] Destination for converted value
4350c2c66affSColin Finck *
4351c2c66affSColin Finck * RETURNS
4352c2c66affSColin Finck * Success: S_OK. pVarOut contains the converted value.
4353c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
4354c2c66affSColin Finck *
4355c2c66affSColin Finck * NOTES
4356c2c66affSColin Finck * - The type of the value stored in pVarOut depends on the type of pVarIn,
4357c2c66affSColin Finck * according to the following table:
4358c2c66affSColin Finck *| Input Type Output Type
4359c2c66affSColin Finck *| ---------- -----------
4360c2c66affSColin Finck *| VT_BOOL VT_I2
4361c2c66affSColin Finck *| VT_EMPTY VT_I2
4362c2c66affSColin Finck *| VT_BSTR VT_R8
4363c2c66affSColin Finck *| All Others Unchanged
4364c2c66affSColin Finck * - The difference between this function and VarInt() is that VarInt() rounds
4365c2c66affSColin Finck * negative numbers away from 0, while this function rounds them towards zero.
4366c2c66affSColin Finck */
VarFix(LPVARIANT pVarIn,LPVARIANT pVarOut)4367c2c66affSColin Finck HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
4368c2c66affSColin Finck {
4369c2c66affSColin Finck HRESULT hRet = S_OK;
4370c2c66affSColin Finck VARIANT temp;
4371c2c66affSColin Finck
4372c2c66affSColin Finck VariantInit(&temp);
4373c2c66affSColin Finck
4374c2c66affSColin Finck TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4375c2c66affSColin Finck
4376c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
4377c2c66affSColin Finck if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4378c2c66affSColin Finck {
4379c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4380c2c66affSColin Finck if (FAILED(hRet)) goto VarFix_Exit;
4381c2c66affSColin Finck pVarIn = &temp;
4382c2c66affSColin Finck }
4383c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
4384c2c66affSColin Finck
4385c2c66affSColin Finck switch (V_VT(pVarIn))
4386c2c66affSColin Finck {
4387c2c66affSColin Finck case VT_UI1:
4388c2c66affSColin Finck V_UI1(pVarOut) = V_UI1(pVarIn);
4389c2c66affSColin Finck break;
4390c2c66affSColin Finck case VT_BOOL:
4391c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4392c2c66affSColin Finck /* Fall through */
4393c2c66affSColin Finck case VT_I2:
4394c2c66affSColin Finck V_I2(pVarOut) = V_I2(pVarIn);
4395c2c66affSColin Finck break;
4396c2c66affSColin Finck case VT_I4:
4397c2c66affSColin Finck V_I4(pVarOut) = V_I4(pVarIn);
4398c2c66affSColin Finck break;
4399c2c66affSColin Finck case VT_I8:
4400c2c66affSColin Finck V_I8(pVarOut) = V_I8(pVarIn);
4401c2c66affSColin Finck break;
4402c2c66affSColin Finck case VT_R4:
4403c2c66affSColin Finck if (V_R4(pVarIn) < 0.0f)
4404c2c66affSColin Finck V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
4405c2c66affSColin Finck else
4406c2c66affSColin Finck V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4407c2c66affSColin Finck break;
4408c2c66affSColin Finck case VT_BSTR:
4409c2c66affSColin Finck V_VT(pVarOut) = VT_R8;
4410c2c66affSColin Finck hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4411c2c66affSColin Finck pVarIn = pVarOut;
4412c2c66affSColin Finck /* Fall through */
4413c2c66affSColin Finck case VT_DATE:
4414c2c66affSColin Finck case VT_R8:
4415c2c66affSColin Finck if (V_R8(pVarIn) < 0.0)
4416c2c66affSColin Finck V_R8(pVarOut) = ceil(V_R8(pVarIn));
4417c2c66affSColin Finck else
4418c2c66affSColin Finck V_R8(pVarOut) = floor(V_R8(pVarIn));
4419c2c66affSColin Finck break;
4420c2c66affSColin Finck case VT_CY:
4421c2c66affSColin Finck hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
4422c2c66affSColin Finck break;
4423c2c66affSColin Finck case VT_DECIMAL:
4424c2c66affSColin Finck hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4425c2c66affSColin Finck break;
4426c2c66affSColin Finck case VT_EMPTY:
4427c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4428c2c66affSColin Finck V_I2(pVarOut) = 0;
4429c2c66affSColin Finck break;
4430c2c66affSColin Finck case VT_NULL:
4431c2c66affSColin Finck /* No-Op */
4432c2c66affSColin Finck break;
4433c2c66affSColin Finck default:
4434c2c66affSColin Finck if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4435c2c66affSColin Finck FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4436c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
4437c2c66affSColin Finck else
4438c2c66affSColin Finck hRet = DISP_E_TYPEMISMATCH;
4439c2c66affSColin Finck }
4440c2c66affSColin Finck VarFix_Exit:
4441c2c66affSColin Finck if (FAILED(hRet))
4442c2c66affSColin Finck V_VT(pVarOut) = VT_EMPTY;
4443c2c66affSColin Finck VariantClear(&temp);
4444c2c66affSColin Finck
4445c2c66affSColin Finck return hRet;
4446c2c66affSColin Finck }
4447c2c66affSColin Finck
4448c2c66affSColin Finck /**********************************************************************
4449c2c66affSColin Finck * VarInt [OLEAUT32.172]
4450c2c66affSColin Finck *
4451c2c66affSColin Finck * Truncate a variants value to a whole number.
4452c2c66affSColin Finck *
4453c2c66affSColin Finck * PARAMS
4454c2c66affSColin Finck * pVarIn [I] Source variant
4455c2c66affSColin Finck * pVarOut [O] Destination for converted value
4456c2c66affSColin Finck *
4457c2c66affSColin Finck * RETURNS
4458c2c66affSColin Finck * Success: S_OK. pVarOut contains the converted value.
4459c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
4460c2c66affSColin Finck *
4461c2c66affSColin Finck * NOTES
4462c2c66affSColin Finck * - The type of the value stored in pVarOut depends on the type of pVarIn,
4463c2c66affSColin Finck * according to the following table:
4464c2c66affSColin Finck *| Input Type Output Type
4465c2c66affSColin Finck *| ---------- -----------
4466c2c66affSColin Finck *| VT_BOOL VT_I2
4467c2c66affSColin Finck *| VT_EMPTY VT_I2
4468c2c66affSColin Finck *| VT_BSTR VT_R8
4469c2c66affSColin Finck *| All Others Unchanged
4470c2c66affSColin Finck * - The difference between this function and VarFix() is that VarFix() rounds
4471c2c66affSColin Finck * negative numbers towards 0, while this function rounds them away from zero.
4472c2c66affSColin Finck */
VarInt(LPVARIANT pVarIn,LPVARIANT pVarOut)4473c2c66affSColin Finck HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
4474c2c66affSColin Finck {
4475c2c66affSColin Finck HRESULT hRet = S_OK;
4476c2c66affSColin Finck VARIANT temp;
4477c2c66affSColin Finck
4478c2c66affSColin Finck VariantInit(&temp);
4479c2c66affSColin Finck
4480c2c66affSColin Finck TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4481c2c66affSColin Finck
4482c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
4483c2c66affSColin Finck if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4484c2c66affSColin Finck {
4485c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4486c2c66affSColin Finck if (FAILED(hRet)) goto VarInt_Exit;
4487c2c66affSColin Finck pVarIn = &temp;
4488c2c66affSColin Finck }
4489c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
4490c2c66affSColin Finck
4491c2c66affSColin Finck switch (V_VT(pVarIn))
4492c2c66affSColin Finck {
4493c2c66affSColin Finck case VT_R4:
4494c2c66affSColin Finck V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
4495c2c66affSColin Finck break;
4496c2c66affSColin Finck case VT_BSTR:
4497c2c66affSColin Finck V_VT(pVarOut) = VT_R8;
4498c2c66affSColin Finck hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4499c2c66affSColin Finck pVarIn = pVarOut;
4500c2c66affSColin Finck /* Fall through */
4501c2c66affSColin Finck case VT_DATE:
4502c2c66affSColin Finck case VT_R8:
4503c2c66affSColin Finck V_R8(pVarOut) = floor(V_R8(pVarIn));
4504c2c66affSColin Finck break;
4505c2c66affSColin Finck case VT_CY:
4506c2c66affSColin Finck hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
4507c2c66affSColin Finck break;
4508c2c66affSColin Finck case VT_DECIMAL:
4509c2c66affSColin Finck hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4510c2c66affSColin Finck break;
4511c2c66affSColin Finck default:
4512c2c66affSColin Finck hRet = VarFix(pVarIn, pVarOut);
4513c2c66affSColin Finck }
4514c2c66affSColin Finck VarInt_Exit:
4515c2c66affSColin Finck VariantClear(&temp);
4516c2c66affSColin Finck
4517c2c66affSColin Finck return hRet;
4518c2c66affSColin Finck }
4519c2c66affSColin Finck
4520c2c66affSColin Finck /**********************************************************************
4521c2c66affSColin Finck * VarXor [OLEAUT32.167]
4522c2c66affSColin Finck *
4523c2c66affSColin Finck * Perform a logical exclusive-or (XOR) operation on two variants.
4524c2c66affSColin Finck *
4525c2c66affSColin Finck * PARAMS
4526c2c66affSColin Finck * pVarLeft [I] First variant
4527c2c66affSColin Finck * pVarRight [I] Variant to XOR with pVarLeft
4528c2c66affSColin Finck * pVarOut [O] Destination for XOR result
4529c2c66affSColin Finck *
4530c2c66affSColin Finck * RETURNS
4531c2c66affSColin Finck * Success: S_OK. pVarOut contains the result of the operation with its type
4532c2c66affSColin Finck * taken from the table below).
4533c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
4534c2c66affSColin Finck *
4535c2c66affSColin Finck * NOTES
4536c2c66affSColin Finck * - Neither pVarLeft or pVarRight are modified by this function.
4537c2c66affSColin Finck * - This function does not process by-reference variants.
4538c2c66affSColin Finck * - Input types of VT_BSTR may be numeric strings or boolean text.
4539c2c66affSColin Finck * - The type of result stored in pVarOut depends on the types of pVarLeft
4540c2c66affSColin Finck * and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
4541c2c66affSColin Finck * or VT_NULL if the function succeeds.
4542c2c66affSColin Finck * - Type promotion is inconsistent and as a result certain combinations of
4543c2c66affSColin Finck * values will return DISP_E_OVERFLOW even when they could be represented.
4544c2c66affSColin Finck * This matches the behaviour of native oleaut32.
4545c2c66affSColin Finck */
VarXor(LPVARIANT pVarLeft,LPVARIANT pVarRight,LPVARIANT pVarOut)4546c2c66affSColin Finck HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4547c2c66affSColin Finck {
4548c2c66affSColin Finck VARTYPE vt;
4549c2c66affSColin Finck VARIANT varLeft, varRight;
4550c2c66affSColin Finck VARIANT tempLeft, tempRight;
4551c2c66affSColin Finck double d;
4552c2c66affSColin Finck HRESULT hRet;
4553c2c66affSColin Finck
4554c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4555c2c66affSColin Finck
4556c2c66affSColin Finck if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
4557c2c66affSColin Finck V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
4558c2c66affSColin Finck V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
4559c2c66affSColin Finck V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
4560c2c66affSColin Finck V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
4561c2c66affSColin Finck V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
4562c2c66affSColin Finck return DISP_E_BADVARTYPE;
4563c2c66affSColin Finck
4564c2c66affSColin Finck if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
4565c2c66affSColin Finck {
4566c2c66affSColin Finck /* NULL XOR anything valid is NULL */
4567c2c66affSColin Finck V_VT(pVarOut) = VT_NULL;
4568c2c66affSColin Finck return S_OK;
4569c2c66affSColin Finck }
4570c2c66affSColin Finck
4571c2c66affSColin Finck VariantInit(&tempLeft);
4572c2c66affSColin Finck VariantInit(&tempRight);
4573c2c66affSColin Finck
4574c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
4575c2c66affSColin Finck if ((V_VT(pVarLeft) & VT_TYPEMASK) == VT_DISPATCH)
4576c2c66affSColin Finck {
4577c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarLeft, &tempLeft);
4578c2c66affSColin Finck if (FAILED(hRet)) goto VarXor_Exit;
4579c2c66affSColin Finck pVarLeft = &tempLeft;
4580c2c66affSColin Finck }
4581c2c66affSColin Finck if ((V_VT(pVarRight) & VT_TYPEMASK) == VT_DISPATCH)
4582c2c66affSColin Finck {
4583c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarRight, &tempRight);
4584c2c66affSColin Finck if (FAILED(hRet)) goto VarXor_Exit;
4585c2c66affSColin Finck pVarRight = &tempRight;
4586c2c66affSColin Finck }
4587c2c66affSColin Finck
4588c2c66affSColin Finck /* Copy our inputs so we don't disturb anything */
4589c2c66affSColin Finck V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
4590c2c66affSColin Finck
4591c2c66affSColin Finck hRet = VariantCopy(&varLeft, pVarLeft);
4592c2c66affSColin Finck if (FAILED(hRet))
4593c2c66affSColin Finck goto VarXor_Exit;
4594c2c66affSColin Finck
4595c2c66affSColin Finck hRet = VariantCopy(&varRight, pVarRight);
4596c2c66affSColin Finck if (FAILED(hRet))
4597c2c66affSColin Finck goto VarXor_Exit;
4598c2c66affSColin Finck
4599c2c66affSColin Finck /* Try any strings first as numbers, then as VT_BOOL */
4600c2c66affSColin Finck if (V_VT(&varLeft) == VT_BSTR)
4601c2c66affSColin Finck {
4602c2c66affSColin Finck hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
4603c2c66affSColin Finck hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
4604c2c66affSColin Finck FAILED(hRet) ? VT_BOOL : VT_I4);
4605c2c66affSColin Finck if (FAILED(hRet))
4606c2c66affSColin Finck goto VarXor_Exit;
4607c2c66affSColin Finck }
4608c2c66affSColin Finck
4609c2c66affSColin Finck if (V_VT(&varRight) == VT_BSTR)
4610c2c66affSColin Finck {
4611c2c66affSColin Finck hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
4612c2c66affSColin Finck hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
4613c2c66affSColin Finck FAILED(hRet) ? VT_BOOL : VT_I4);
4614c2c66affSColin Finck if (FAILED(hRet))
4615c2c66affSColin Finck goto VarXor_Exit;
4616c2c66affSColin Finck }
4617c2c66affSColin Finck
4618c2c66affSColin Finck /* Determine the result type */
4619c2c66affSColin Finck if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4620c2c66affSColin Finck {
4621c2c66affSColin Finck if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
4622c2c66affSColin Finck {
4623c2c66affSColin Finck hRet = DISP_E_TYPEMISMATCH;
4624c2c66affSColin Finck goto VarXor_Exit;
4625c2c66affSColin Finck }
4626c2c66affSColin Finck vt = VT_I8;
4627c2c66affSColin Finck }
4628c2c66affSColin Finck else
4629c2c66affSColin Finck {
4630c2c66affSColin Finck switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
4631c2c66affSColin Finck {
4632c2c66affSColin Finck case (VT_BOOL << 16) | VT_BOOL:
4633c2c66affSColin Finck vt = VT_BOOL;
4634c2c66affSColin Finck break;
4635c2c66affSColin Finck case (VT_UI1 << 16) | VT_UI1:
4636c2c66affSColin Finck vt = VT_UI1;
4637c2c66affSColin Finck break;
4638c2c66affSColin Finck case (VT_EMPTY << 16) | VT_EMPTY:
4639c2c66affSColin Finck case (VT_EMPTY << 16) | VT_UI1:
4640c2c66affSColin Finck case (VT_EMPTY << 16) | VT_I2:
4641c2c66affSColin Finck case (VT_EMPTY << 16) | VT_BOOL:
4642c2c66affSColin Finck case (VT_UI1 << 16) | VT_EMPTY:
4643c2c66affSColin Finck case (VT_UI1 << 16) | VT_I2:
4644c2c66affSColin Finck case (VT_UI1 << 16) | VT_BOOL:
4645c2c66affSColin Finck case (VT_I2 << 16) | VT_EMPTY:
4646c2c66affSColin Finck case (VT_I2 << 16) | VT_UI1:
4647c2c66affSColin Finck case (VT_I2 << 16) | VT_I2:
4648c2c66affSColin Finck case (VT_I2 << 16) | VT_BOOL:
4649c2c66affSColin Finck case (VT_BOOL << 16) | VT_EMPTY:
4650c2c66affSColin Finck case (VT_BOOL << 16) | VT_UI1:
4651c2c66affSColin Finck case (VT_BOOL << 16) | VT_I2:
4652c2c66affSColin Finck vt = VT_I2;
4653c2c66affSColin Finck break;
4654c2c66affSColin Finck default:
4655c2c66affSColin Finck vt = VT_I4;
4656c2c66affSColin Finck break;
4657c2c66affSColin Finck }
4658c2c66affSColin Finck }
4659c2c66affSColin Finck
4660c2c66affSColin Finck /* VT_UI4 does not overflow */
4661c2c66affSColin Finck if (vt != VT_I8)
4662c2c66affSColin Finck {
4663c2c66affSColin Finck if (V_VT(&varLeft) == VT_UI4)
4664c2c66affSColin Finck V_VT(&varLeft) = VT_I4;
4665c2c66affSColin Finck if (V_VT(&varRight) == VT_UI4)
4666c2c66affSColin Finck V_VT(&varRight) = VT_I4;
4667c2c66affSColin Finck }
4668c2c66affSColin Finck
4669c2c66affSColin Finck /* Convert our input copies to the result type */
4670c2c66affSColin Finck if (V_VT(&varLeft) != vt)
4671c2c66affSColin Finck hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
4672c2c66affSColin Finck if (FAILED(hRet))
4673c2c66affSColin Finck goto VarXor_Exit;
4674c2c66affSColin Finck
4675c2c66affSColin Finck if (V_VT(&varRight) != vt)
4676c2c66affSColin Finck hRet = VariantChangeType(&varRight, &varRight, 0, vt);
4677c2c66affSColin Finck if (FAILED(hRet))
4678c2c66affSColin Finck goto VarXor_Exit;
4679c2c66affSColin Finck
4680c2c66affSColin Finck V_VT(pVarOut) = vt;
4681c2c66affSColin Finck
4682c2c66affSColin Finck /* Calculate the result */
4683c2c66affSColin Finck switch (vt)
4684c2c66affSColin Finck {
4685c2c66affSColin Finck case VT_I8:
4686c2c66affSColin Finck V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
4687c2c66affSColin Finck break;
4688c2c66affSColin Finck case VT_I4:
4689c2c66affSColin Finck V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
4690c2c66affSColin Finck break;
4691c2c66affSColin Finck case VT_BOOL:
4692c2c66affSColin Finck case VT_I2:
4693c2c66affSColin Finck V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
4694c2c66affSColin Finck break;
4695c2c66affSColin Finck case VT_UI1:
4696c2c66affSColin Finck V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
4697c2c66affSColin Finck break;
4698c2c66affSColin Finck }
4699c2c66affSColin Finck
4700c2c66affSColin Finck VarXor_Exit:
4701c2c66affSColin Finck VariantClear(&varLeft);
4702c2c66affSColin Finck VariantClear(&varRight);
4703c2c66affSColin Finck VariantClear(&tempLeft);
4704c2c66affSColin Finck VariantClear(&tempRight);
4705c2c66affSColin Finck return hRet;
4706c2c66affSColin Finck }
4707c2c66affSColin Finck
4708c2c66affSColin Finck /**********************************************************************
4709c2c66affSColin Finck * VarEqv [OLEAUT32.172]
4710c2c66affSColin Finck *
4711c2c66affSColin Finck * Determine if two variants contain the same value.
4712c2c66affSColin Finck *
4713c2c66affSColin Finck * PARAMS
4714c2c66affSColin Finck * pVarLeft [I] First variant to compare
4715c2c66affSColin Finck * pVarRight [I] Variant to compare to pVarLeft
4716c2c66affSColin Finck * pVarOut [O] Destination for comparison result
4717c2c66affSColin Finck *
4718c2c66affSColin Finck * RETURNS
4719c2c66affSColin Finck * Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4720c2c66affSColin Finck * if equivalent or non-zero otherwise.
4721c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
4722c2c66affSColin Finck *
4723c2c66affSColin Finck * NOTES
4724c2c66affSColin Finck * - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
4725c2c66affSColin Finck * the result.
4726c2c66affSColin Finck */
VarEqv(LPVARIANT pVarLeft,LPVARIANT pVarRight,LPVARIANT pVarOut)4727c2c66affSColin Finck HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
4728c2c66affSColin Finck {
4729c2c66affSColin Finck HRESULT hRet;
4730c2c66affSColin Finck
4731c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(pVarLeft), debugstr_variant(pVarRight), pVarOut);
4732c2c66affSColin Finck
4733c2c66affSColin Finck hRet = VarXor(pVarLeft, pVarRight, pVarOut);
4734c2c66affSColin Finck if (SUCCEEDED(hRet))
4735c2c66affSColin Finck {
4736c2c66affSColin Finck if (V_VT(pVarOut) == VT_I8)
4737c2c66affSColin Finck V_I8(pVarOut) = ~V_I8(pVarOut);
4738c2c66affSColin Finck else
4739c2c66affSColin Finck V_UI4(pVarOut) = ~V_UI4(pVarOut);
4740c2c66affSColin Finck }
4741c2c66affSColin Finck return hRet;
4742c2c66affSColin Finck }
4743c2c66affSColin Finck
4744c2c66affSColin Finck /**********************************************************************
4745c2c66affSColin Finck * VarNeg [OLEAUT32.173]
4746c2c66affSColin Finck *
4747c2c66affSColin Finck * Negate the value of a variant.
4748c2c66affSColin Finck *
4749c2c66affSColin Finck * PARAMS
4750c2c66affSColin Finck * pVarIn [I] Source variant
4751c2c66affSColin Finck * pVarOut [O] Destination for converted value
4752c2c66affSColin Finck *
4753c2c66affSColin Finck * RETURNS
4754c2c66affSColin Finck * Success: S_OK. pVarOut contains the converted value.
4755c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
4756c2c66affSColin Finck *
4757c2c66affSColin Finck * NOTES
4758c2c66affSColin Finck * - The type of the value stored in pVarOut depends on the type of pVarIn,
4759c2c66affSColin Finck * according to the following table:
4760c2c66affSColin Finck *| Input Type Output Type
4761c2c66affSColin Finck *| ---------- -----------
4762c2c66affSColin Finck *| VT_EMPTY VT_I2
4763c2c66affSColin Finck *| VT_UI1 VT_I2
4764c2c66affSColin Finck *| VT_BOOL VT_I2
4765c2c66affSColin Finck *| VT_BSTR VT_R8
4766c2c66affSColin Finck *| All Others Unchanged (unless promoted)
4767c2c66affSColin Finck * - Where the negated value of a variant does not fit in its base type, the type
4768c2c66affSColin Finck * is promoted according to the following table:
4769c2c66affSColin Finck *| Input Type Promoted To
4770c2c66affSColin Finck *| ---------- -----------
4771c2c66affSColin Finck *| VT_I2 VT_I4
4772c2c66affSColin Finck *| VT_I4 VT_R8
4773c2c66affSColin Finck *| VT_I8 VT_R8
4774c2c66affSColin Finck * - The native version of this function returns DISP_E_BADVARTYPE for valid
4775c2c66affSColin Finck * variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
4776c2c66affSColin Finck * for types which are not valid. Since this is in contravention of the
4777c2c66affSColin Finck * meaning of those error codes and unlikely to be relied on by applications,
4778c2c66affSColin Finck * this implementation returns errors consistent with the other high level
4779c2c66affSColin Finck * variant math functions.
4780c2c66affSColin Finck */
VarNeg(LPVARIANT pVarIn,LPVARIANT pVarOut)4781c2c66affSColin Finck HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
4782c2c66affSColin Finck {
4783c2c66affSColin Finck HRESULT hRet = S_OK;
4784c2c66affSColin Finck VARIANT temp;
4785c2c66affSColin Finck
4786c2c66affSColin Finck VariantInit(&temp);
4787c2c66affSColin Finck
4788c2c66affSColin Finck TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4789c2c66affSColin Finck
4790c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
4791c2c66affSColin Finck if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4792c2c66affSColin Finck {
4793c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4794c2c66affSColin Finck if (FAILED(hRet)) goto VarNeg_Exit;
4795c2c66affSColin Finck pVarIn = &temp;
4796c2c66affSColin Finck }
4797c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
4798c2c66affSColin Finck
4799c2c66affSColin Finck switch (V_VT(pVarIn))
4800c2c66affSColin Finck {
4801c2c66affSColin Finck case VT_UI1:
4802c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4803c2c66affSColin Finck V_I2(pVarOut) = -V_UI1(pVarIn);
4804c2c66affSColin Finck break;
4805c2c66affSColin Finck case VT_BOOL:
4806c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4807c2c66affSColin Finck /* Fall through */
4808c2c66affSColin Finck case VT_I2:
4809c2c66affSColin Finck if (V_I2(pVarIn) == I2_MIN)
4810c2c66affSColin Finck {
4811c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4812c2c66affSColin Finck V_I4(pVarOut) = -(int)V_I2(pVarIn);
4813c2c66affSColin Finck }
4814c2c66affSColin Finck else
4815c2c66affSColin Finck V_I2(pVarOut) = -V_I2(pVarIn);
4816c2c66affSColin Finck break;
4817c2c66affSColin Finck case VT_I4:
4818c2c66affSColin Finck if (V_I4(pVarIn) == I4_MIN)
4819c2c66affSColin Finck {
4820c2c66affSColin Finck V_VT(pVarOut) = VT_R8;
4821c2c66affSColin Finck V_R8(pVarOut) = -(double)V_I4(pVarIn);
4822c2c66affSColin Finck }
4823c2c66affSColin Finck else
4824c2c66affSColin Finck V_I4(pVarOut) = -V_I4(pVarIn);
4825c2c66affSColin Finck break;
4826c2c66affSColin Finck case VT_I8:
4827c2c66affSColin Finck if (V_I8(pVarIn) == I8_MIN)
4828c2c66affSColin Finck {
4829c2c66affSColin Finck V_VT(pVarOut) = VT_R8;
4830c2c66affSColin Finck hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
4831c2c66affSColin Finck V_R8(pVarOut) *= -1.0;
4832c2c66affSColin Finck }
4833c2c66affSColin Finck else
4834c2c66affSColin Finck V_I8(pVarOut) = -V_I8(pVarIn);
4835c2c66affSColin Finck break;
4836c2c66affSColin Finck case VT_R4:
4837c2c66affSColin Finck V_R4(pVarOut) = -V_R4(pVarIn);
4838c2c66affSColin Finck break;
4839c2c66affSColin Finck case VT_DATE:
4840c2c66affSColin Finck case VT_R8:
4841c2c66affSColin Finck V_R8(pVarOut) = -V_R8(pVarIn);
4842c2c66affSColin Finck break;
4843c2c66affSColin Finck case VT_CY:
4844c2c66affSColin Finck hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
4845c2c66affSColin Finck break;
4846c2c66affSColin Finck case VT_DECIMAL:
4847c2c66affSColin Finck hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
4848c2c66affSColin Finck break;
4849c2c66affSColin Finck case VT_BSTR:
4850c2c66affSColin Finck V_VT(pVarOut) = VT_R8;
4851c2c66affSColin Finck hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
4852c2c66affSColin Finck V_R8(pVarOut) = -V_R8(pVarOut);
4853c2c66affSColin Finck break;
4854c2c66affSColin Finck case VT_EMPTY:
4855c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4856c2c66affSColin Finck V_I2(pVarOut) = 0;
4857c2c66affSColin Finck break;
4858c2c66affSColin Finck case VT_NULL:
4859c2c66affSColin Finck /* No-Op */
4860c2c66affSColin Finck break;
4861c2c66affSColin Finck default:
4862c2c66affSColin Finck if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
4863c2c66affSColin Finck FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
4864c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
4865c2c66affSColin Finck else
4866c2c66affSColin Finck hRet = DISP_E_TYPEMISMATCH;
4867c2c66affSColin Finck }
4868c2c66affSColin Finck VarNeg_Exit:
4869c2c66affSColin Finck if (FAILED(hRet))
4870c2c66affSColin Finck V_VT(pVarOut) = VT_EMPTY;
4871c2c66affSColin Finck VariantClear(&temp);
4872c2c66affSColin Finck
4873c2c66affSColin Finck return hRet;
4874c2c66affSColin Finck }
4875c2c66affSColin Finck
4876c2c66affSColin Finck /**********************************************************************
4877c2c66affSColin Finck * VarNot [OLEAUT32.174]
4878c2c66affSColin Finck *
4879c2c66affSColin Finck * Perform a not operation on a variant.
4880c2c66affSColin Finck *
4881c2c66affSColin Finck * PARAMS
4882c2c66affSColin Finck * pVarIn [I] Source variant
4883c2c66affSColin Finck * pVarOut [O] Destination for converted value
4884c2c66affSColin Finck *
4885c2c66affSColin Finck * RETURNS
4886c2c66affSColin Finck * Success: S_OK. pVarOut contains the converted value.
4887c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
4888c2c66affSColin Finck *
4889c2c66affSColin Finck * NOTES
4890c2c66affSColin Finck * - Strictly speaking, this function performs a bitwise ones complement
4891c2c66affSColin Finck * on the variants value (after possibly converting to VT_I4, see below).
4892c2c66affSColin Finck * This only behaves like a boolean not operation if the value in
4893c2c66affSColin Finck * pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
4894c2c66affSColin Finck * - To perform a genuine not operation, convert the variant to a VT_BOOL
4895c2c66affSColin Finck * before calling this function.
4896c2c66affSColin Finck * - This function does not process by-reference variants.
4897c2c66affSColin Finck * - The type of the value stored in pVarOut depends on the type of pVarIn,
4898c2c66affSColin Finck * according to the following table:
4899c2c66affSColin Finck *| Input Type Output Type
4900c2c66affSColin Finck *| ---------- -----------
4901c2c66affSColin Finck *| VT_EMPTY VT_I2
4902c2c66affSColin Finck *| VT_R4 VT_I4
4903c2c66affSColin Finck *| VT_R8 VT_I4
4904c2c66affSColin Finck *| VT_BSTR VT_I4
4905c2c66affSColin Finck *| VT_DECIMAL VT_I4
4906c2c66affSColin Finck *| VT_CY VT_I4
4907c2c66affSColin Finck *| (All others) Unchanged
4908c2c66affSColin Finck */
VarNot(LPVARIANT pVarIn,LPVARIANT pVarOut)4909c2c66affSColin Finck HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
4910c2c66affSColin Finck {
4911c2c66affSColin Finck VARIANT varIn;
4912c2c66affSColin Finck HRESULT hRet = S_OK;
4913c2c66affSColin Finck VARIANT temp;
4914c2c66affSColin Finck
4915c2c66affSColin Finck VariantInit(&temp);
4916c2c66affSColin Finck
4917c2c66affSColin Finck TRACE("(%s,%p)\n", debugstr_variant(pVarIn), pVarOut);
4918c2c66affSColin Finck
4919c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
4920c2c66affSColin Finck if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
4921c2c66affSColin Finck {
4922c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
4923c2c66affSColin Finck if (FAILED(hRet)) goto VarNot_Exit;
4924c2c66affSColin Finck pVarIn = &temp;
4925c2c66affSColin Finck }
4926c2c66affSColin Finck
4927c2c66affSColin Finck if (V_VT(pVarIn) == VT_BSTR)
4928c2c66affSColin Finck {
4929c2c66affSColin Finck V_VT(&varIn) = VT_R8;
4930c2c66affSColin Finck hRet = VarR8FromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn) );
4931c2c66affSColin Finck if (FAILED(hRet))
4932c2c66affSColin Finck {
4933c2c66affSColin Finck V_VT(&varIn) = VT_BOOL;
4934c2c66affSColin Finck hRet = VarBoolFromStr( V_BSTR(pVarIn), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &V_BOOL(&varIn) );
4935c2c66affSColin Finck }
4936c2c66affSColin Finck if (FAILED(hRet)) goto VarNot_Exit;
4937c2c66affSColin Finck pVarIn = &varIn;
4938c2c66affSColin Finck }
4939c2c66affSColin Finck
4940c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
4941c2c66affSColin Finck
4942c2c66affSColin Finck switch (V_VT(pVarIn))
4943c2c66affSColin Finck {
4944c2c66affSColin Finck case VT_I1:
4945c2c66affSColin Finck V_I4(pVarOut) = ~V_I1(pVarIn);
4946c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4947c2c66affSColin Finck break;
4948c2c66affSColin Finck case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
4949c2c66affSColin Finck case VT_BOOL:
4950c2c66affSColin Finck case VT_I2: V_I2(pVarOut) = ~V_I2(pVarIn); break;
4951c2c66affSColin Finck case VT_UI2:
4952c2c66affSColin Finck V_I4(pVarOut) = ~V_UI2(pVarIn);
4953c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4954c2c66affSColin Finck break;
4955c2c66affSColin Finck case VT_DECIMAL:
4956c2c66affSColin Finck hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4957c2c66affSColin Finck if (FAILED(hRet))
4958c2c66affSColin Finck break;
4959c2c66affSColin Finck pVarIn = &varIn;
4960c2c66affSColin Finck /* Fall through ... */
4961c2c66affSColin Finck case VT_INT:
4962c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4963c2c66affSColin Finck /* Fall through ... */
4964c2c66affSColin Finck case VT_I4: V_I4(pVarOut) = ~V_I4(pVarIn); break;
4965c2c66affSColin Finck case VT_UINT:
4966c2c66affSColin Finck case VT_UI4:
4967c2c66affSColin Finck V_I4(pVarOut) = ~V_UI4(pVarIn);
4968c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4969c2c66affSColin Finck break;
4970c2c66affSColin Finck case VT_I8: V_I8(pVarOut) = ~V_I8(pVarIn); break;
4971c2c66affSColin Finck case VT_UI8:
4972c2c66affSColin Finck V_I4(pVarOut) = ~V_UI8(pVarIn);
4973c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4974c2c66affSColin Finck break;
4975c2c66affSColin Finck case VT_R4:
4976c2c66affSColin Finck hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
4977c2c66affSColin Finck V_I4(pVarOut) = ~V_I4(pVarOut);
4978c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4979c2c66affSColin Finck break;
4980c2c66affSColin Finck case VT_DATE:
4981c2c66affSColin Finck case VT_R8:
4982c2c66affSColin Finck hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
4983c2c66affSColin Finck V_I4(pVarOut) = ~V_I4(pVarOut);
4984c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4985c2c66affSColin Finck break;
4986c2c66affSColin Finck case VT_CY:
4987c2c66affSColin Finck hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
4988c2c66affSColin Finck V_I4(pVarOut) = ~V_I4(pVarOut);
4989c2c66affSColin Finck V_VT(pVarOut) = VT_I4;
4990c2c66affSColin Finck break;
4991c2c66affSColin Finck case VT_EMPTY:
4992c2c66affSColin Finck V_I2(pVarOut) = ~0;
4993c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
4994c2c66affSColin Finck break;
4995c2c66affSColin Finck case VT_NULL:
4996c2c66affSColin Finck /* No-Op */
4997c2c66affSColin Finck break;
4998c2c66affSColin Finck default:
4999c2c66affSColin Finck if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
5000c2c66affSColin Finck FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
5001c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
5002c2c66affSColin Finck else
5003c2c66affSColin Finck hRet = DISP_E_TYPEMISMATCH;
5004c2c66affSColin Finck }
5005c2c66affSColin Finck VarNot_Exit:
5006c2c66affSColin Finck if (FAILED(hRet))
5007c2c66affSColin Finck V_VT(pVarOut) = VT_EMPTY;
5008c2c66affSColin Finck VariantClear(&temp);
5009c2c66affSColin Finck
5010c2c66affSColin Finck return hRet;
5011c2c66affSColin Finck }
5012c2c66affSColin Finck
5013c2c66affSColin Finck /**********************************************************************
5014c2c66affSColin Finck * VarRound [OLEAUT32.175]
5015c2c66affSColin Finck *
5016c2c66affSColin Finck * Perform a round operation on a variant.
5017c2c66affSColin Finck *
5018c2c66affSColin Finck * PARAMS
5019c2c66affSColin Finck * pVarIn [I] Source variant
5020c2c66affSColin Finck * deci [I] Number of decimals to round to
5021c2c66affSColin Finck * pVarOut [O] Destination for converted value
5022c2c66affSColin Finck *
5023c2c66affSColin Finck * RETURNS
5024c2c66affSColin Finck * Success: S_OK. pVarOut contains the converted value.
5025c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
5026c2c66affSColin Finck *
5027c2c66affSColin Finck * NOTES
5028c2c66affSColin Finck * - Floating point values are rounded to the desired number of decimals.
5029c2c66affSColin Finck * - Some integer types are just copied to the return variable.
5030c2c66affSColin Finck * - Some other integer types are not handled and fail.
5031c2c66affSColin Finck */
VarRound(LPVARIANT pVarIn,int deci,LPVARIANT pVarOut)5032c2c66affSColin Finck HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
5033c2c66affSColin Finck {
5034c2c66affSColin Finck VARIANT varIn;
5035c2c66affSColin Finck HRESULT hRet = S_OK;
5036c2c66affSColin Finck float factor;
5037c2c66affSColin Finck VARIANT temp;
5038c2c66affSColin Finck
5039c2c66affSColin Finck VariantInit(&temp);
5040c2c66affSColin Finck
5041c2c66affSColin Finck TRACE("(%s,%d)\n", debugstr_variant(pVarIn), deci);
5042c2c66affSColin Finck
5043c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
5044c2c66affSColin Finck if ((V_VT(pVarIn) & VT_TYPEMASK) == VT_DISPATCH && ((V_VT(pVarIn) & ~VT_TYPEMASK) == 0))
5045c2c66affSColin Finck {
5046c2c66affSColin Finck hRet = VARIANT_FetchDispatchValue(pVarIn, &temp);
5047c2c66affSColin Finck if (FAILED(hRet)) goto VarRound_Exit;
5048c2c66affSColin Finck pVarIn = &temp;
5049c2c66affSColin Finck }
5050c2c66affSColin Finck
5051c2c66affSColin Finck switch (V_VT(pVarIn))
5052c2c66affSColin Finck {
5053c2c66affSColin Finck /* cases that fail on windows */
5054c2c66affSColin Finck case VT_I1:
5055c2c66affSColin Finck case VT_I8:
5056c2c66affSColin Finck case VT_UI2:
5057c2c66affSColin Finck case VT_UI4:
5058c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
5059c2c66affSColin Finck break;
5060c2c66affSColin Finck
5061c2c66affSColin Finck /* cases just copying in to out */
5062c2c66affSColin Finck case VT_UI1:
5063c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5064c2c66affSColin Finck V_UI1(pVarOut) = V_UI1(pVarIn);
5065c2c66affSColin Finck break;
5066c2c66affSColin Finck case VT_I2:
5067c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5068c2c66affSColin Finck V_I2(pVarOut) = V_I2(pVarIn);
5069c2c66affSColin Finck break;
5070c2c66affSColin Finck case VT_I4:
5071c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5072c2c66affSColin Finck V_I4(pVarOut) = V_I4(pVarIn);
5073c2c66affSColin Finck break;
5074c2c66affSColin Finck case VT_NULL:
5075c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5076c2c66affSColin Finck /* value unchanged */
5077c2c66affSColin Finck break;
5078c2c66affSColin Finck
5079c2c66affSColin Finck /* cases that change type */
5080c2c66affSColin Finck case VT_EMPTY:
5081c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
5082c2c66affSColin Finck V_I2(pVarOut) = 0;
5083c2c66affSColin Finck break;
5084c2c66affSColin Finck case VT_BOOL:
5085c2c66affSColin Finck V_VT(pVarOut) = VT_I2;
5086c2c66affSColin Finck V_I2(pVarOut) = V_BOOL(pVarIn);
5087c2c66affSColin Finck break;
5088c2c66affSColin Finck case VT_BSTR:
5089c2c66affSColin Finck hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
5090c2c66affSColin Finck if (FAILED(hRet))
5091c2c66affSColin Finck break;
5092c2c66affSColin Finck V_VT(&varIn)=VT_R8;
5093c2c66affSColin Finck pVarIn = &varIn;
5094c2c66affSColin Finck /* Fall through ... */
5095c2c66affSColin Finck
5096c2c66affSColin Finck /* cases we need to do math */
5097c2c66affSColin Finck case VT_R8:
5098c2c66affSColin Finck if (V_R8(pVarIn)>0) {
5099c2c66affSColin Finck V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5100c2c66affSColin Finck } else {
5101c2c66affSColin Finck V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5102c2c66affSColin Finck }
5103c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5104c2c66affSColin Finck break;
5105c2c66affSColin Finck case VT_R4:
5106c2c66affSColin Finck if (V_R4(pVarIn)>0) {
5107c2c66affSColin Finck V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5108c2c66affSColin Finck } else {
5109c2c66affSColin Finck V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5110c2c66affSColin Finck }
5111c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5112c2c66affSColin Finck break;
5113c2c66affSColin Finck case VT_DATE:
5114c2c66affSColin Finck if (V_DATE(pVarIn)>0) {
5115c2c66affSColin Finck V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
5116c2c66affSColin Finck } else {
5117c2c66affSColin Finck V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
5118c2c66affSColin Finck }
5119c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5120c2c66affSColin Finck break;
5121c2c66affSColin Finck case VT_CY:
5122c2c66affSColin Finck if (deci>3)
5123c2c66affSColin Finck factor=1;
5124c2c66affSColin Finck else
5125c2c66affSColin Finck factor=pow(10, 4-deci);
5126c2c66affSColin Finck
5127c2c66affSColin Finck if (V_CY(pVarIn).int64>0) {
5128c2c66affSColin Finck V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
5129c2c66affSColin Finck } else {
5130c2c66affSColin Finck V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
5131c2c66affSColin Finck }
5132c2c66affSColin Finck V_VT(pVarOut) = V_VT(pVarIn);
5133c2c66affSColin Finck break;
51345db885caSAmine Khaldi case VT_DECIMAL:
51355db885caSAmine Khaldi {
51365db885caSAmine Khaldi double dbl;
5137c2c66affSColin Finck
5138*ae24453eSAmine Khaldi hRet = VarR8FromDec(&V_DECIMAL(pVarIn), &dbl);
5139*ae24453eSAmine Khaldi if (FAILED(hRet))
5140*ae24453eSAmine Khaldi break;
51415db885caSAmine Khaldi
51425db885caSAmine Khaldi if (dbl>0.0f)
51435db885caSAmine Khaldi dbl = floor(dbl*pow(10,deci)+0.5);
51445db885caSAmine Khaldi else
51455db885caSAmine Khaldi dbl = ceil(dbl*pow(10,deci)-0.5);
51465db885caSAmine Khaldi
51475db885caSAmine Khaldi V_VT(pVarOut)=VT_DECIMAL;
5148*ae24453eSAmine Khaldi hRet = VarDecFromR8(dbl, &V_DECIMAL(pVarOut));
51495db885caSAmine Khaldi break;
51505db885caSAmine Khaldi }
5151c2c66affSColin Finck /* cases we don't know yet */
5152c2c66affSColin Finck default:
5153c2c66affSColin Finck FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
5154c2c66affSColin Finck V_VT(pVarIn) & VT_TYPEMASK, deci);
5155c2c66affSColin Finck hRet = DISP_E_BADVARTYPE;
5156c2c66affSColin Finck }
5157c2c66affSColin Finck VarRound_Exit:
5158c2c66affSColin Finck if (FAILED(hRet))
5159c2c66affSColin Finck V_VT(pVarOut) = VT_EMPTY;
5160c2c66affSColin Finck VariantClear(&temp);
5161c2c66affSColin Finck
5162c2c66affSColin Finck TRACE("returning 0x%08x %s\n", hRet, debugstr_variant(pVarOut));
5163c2c66affSColin Finck return hRet;
5164c2c66affSColin Finck }
5165c2c66affSColin Finck
5166c2c66affSColin Finck /**********************************************************************
5167c2c66affSColin Finck * VarIdiv [OLEAUT32.153]
5168c2c66affSColin Finck *
5169c2c66affSColin Finck * Converts input variants to integers and divides them.
5170c2c66affSColin Finck *
5171c2c66affSColin Finck * PARAMS
5172c2c66affSColin Finck * left [I] Left hand variant
5173c2c66affSColin Finck * right [I] Right hand variant
5174c2c66affSColin Finck * result [O] Destination for quotient
5175c2c66affSColin Finck *
5176c2c66affSColin Finck * RETURNS
5177c2c66affSColin Finck * Success: S_OK. result contains the quotient.
5178c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
5179c2c66affSColin Finck *
5180c2c66affSColin Finck * NOTES
5181c2c66affSColin Finck * If either expression is null, null is returned, as per MSDN
5182c2c66affSColin Finck */
VarIdiv(LPVARIANT left,LPVARIANT right,LPVARIANT result)5183c2c66affSColin Finck HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5184c2c66affSColin Finck {
5185c2c66affSColin Finck HRESULT hres = S_OK;
5186c2c66affSColin Finck VARTYPE resvt = VT_EMPTY;
5187c2c66affSColin Finck VARTYPE leftvt,rightvt;
5188c2c66affSColin Finck VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5189c2c66affSColin Finck VARIANT lv,rv;
5190c2c66affSColin Finck VARIANT tempLeft, tempRight;
5191c2c66affSColin Finck
5192c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5193c2c66affSColin Finck
5194c2c66affSColin Finck VariantInit(&lv);
5195c2c66affSColin Finck VariantInit(&rv);
5196c2c66affSColin Finck VariantInit(&tempLeft);
5197c2c66affSColin Finck VariantInit(&tempRight);
5198c2c66affSColin Finck
5199c2c66affSColin Finck leftvt = V_VT(left)&VT_TYPEMASK;
5200c2c66affSColin Finck rightvt = V_VT(right)&VT_TYPEMASK;
5201c2c66affSColin Finck leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5202c2c66affSColin Finck rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5203c2c66affSColin Finck
5204c2c66affSColin Finck if (leftExtraFlags != rightExtraFlags)
5205c2c66affSColin Finck {
5206c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
5207c2c66affSColin Finck goto end;
5208c2c66affSColin Finck }
5209c2c66affSColin Finck ExtraFlags = leftExtraFlags;
5210c2c66affSColin Finck
5211c2c66affSColin Finck /* Native VarIdiv always returns an error when using extra
5212c2c66affSColin Finck * flags or if the variant combination is I8 and INT.
5213c2c66affSColin Finck */
5214c2c66affSColin Finck if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5215c2c66affSColin Finck (leftvt == VT_INT && rightvt == VT_I8) ||
5216c2c66affSColin Finck (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
5217c2c66affSColin Finck ExtraFlags != 0)
5218c2c66affSColin Finck {
5219c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
5220c2c66affSColin Finck goto end;
5221c2c66affSColin Finck }
5222c2c66affSColin Finck
5223c2c66affSColin Finck /* Determine variant type */
5224c2c66affSColin Finck else if (leftvt == VT_NULL || rightvt == VT_NULL)
5225c2c66affSColin Finck {
5226c2c66affSColin Finck V_VT(result) = VT_NULL;
5227c2c66affSColin Finck hres = S_OK;
5228c2c66affSColin Finck goto end;
5229c2c66affSColin Finck }
5230c2c66affSColin Finck else if (leftvt == VT_I8 || rightvt == VT_I8)
5231c2c66affSColin Finck resvt = VT_I8;
5232c2c66affSColin Finck else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5233c2c66affSColin Finck leftvt == VT_INT || rightvt == VT_INT ||
5234c2c66affSColin Finck leftvt == VT_UINT || rightvt == VT_UINT ||
5235c2c66affSColin Finck leftvt == VT_UI8 || rightvt == VT_UI8 ||
5236c2c66affSColin Finck leftvt == VT_UI4 || rightvt == VT_UI4 ||
5237c2c66affSColin Finck leftvt == VT_UI2 || rightvt == VT_UI2 ||
5238c2c66affSColin Finck leftvt == VT_I1 || rightvt == VT_I1 ||
5239c2c66affSColin Finck leftvt == VT_BSTR || rightvt == VT_BSTR ||
5240c2c66affSColin Finck leftvt == VT_DATE || rightvt == VT_DATE ||
5241c2c66affSColin Finck leftvt == VT_CY || rightvt == VT_CY ||
5242c2c66affSColin Finck leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5243c2c66affSColin Finck leftvt == VT_R8 || rightvt == VT_R8 ||
5244c2c66affSColin Finck leftvt == VT_R4 || rightvt == VT_R4)
5245c2c66affSColin Finck resvt = VT_I4;
5246c2c66affSColin Finck else if (leftvt == VT_I2 || rightvt == VT_I2 ||
5247c2c66affSColin Finck leftvt == VT_BOOL || rightvt == VT_BOOL ||
5248c2c66affSColin Finck leftvt == VT_EMPTY)
5249c2c66affSColin Finck resvt = VT_I2;
5250c2c66affSColin Finck else if (leftvt == VT_UI1 || rightvt == VT_UI1)
5251c2c66affSColin Finck resvt = VT_UI1;
5252c2c66affSColin Finck else
5253c2c66affSColin Finck {
5254c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
5255c2c66affSColin Finck goto end;
5256c2c66affSColin Finck }
5257c2c66affSColin Finck
5258c2c66affSColin Finck /* coerce to the result type */
5259c2c66affSColin Finck hres = VariantChangeType(&lv, left, 0, resvt);
5260c2c66affSColin Finck if (hres != S_OK) goto end;
5261c2c66affSColin Finck hres = VariantChangeType(&rv, right, 0, resvt);
5262c2c66affSColin Finck if (hres != S_OK) goto end;
5263c2c66affSColin Finck
5264c2c66affSColin Finck /* do the math */
5265c2c66affSColin Finck V_VT(result) = resvt;
5266c2c66affSColin Finck switch (resvt)
5267c2c66affSColin Finck {
5268c2c66affSColin Finck case VT_UI1:
5269c2c66affSColin Finck if (V_UI1(&rv) == 0)
5270c2c66affSColin Finck {
5271c2c66affSColin Finck hres = DISP_E_DIVBYZERO;
5272c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5273c2c66affSColin Finck }
5274c2c66affSColin Finck else
5275c2c66affSColin Finck V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
5276c2c66affSColin Finck break;
5277c2c66affSColin Finck case VT_I2:
5278c2c66affSColin Finck if (V_I2(&rv) == 0)
5279c2c66affSColin Finck {
5280c2c66affSColin Finck hres = DISP_E_DIVBYZERO;
5281c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5282c2c66affSColin Finck }
5283c2c66affSColin Finck else
5284c2c66affSColin Finck V_I2(result) = V_I2(&lv) / V_I2(&rv);
5285c2c66affSColin Finck break;
5286c2c66affSColin Finck case VT_I4:
5287c2c66affSColin Finck if (V_I4(&rv) == 0)
5288c2c66affSColin Finck {
5289c2c66affSColin Finck hres = DISP_E_DIVBYZERO;
5290c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5291c2c66affSColin Finck }
5292c2c66affSColin Finck else
5293c2c66affSColin Finck V_I4(result) = V_I4(&lv) / V_I4(&rv);
5294c2c66affSColin Finck break;
5295c2c66affSColin Finck case VT_I8:
5296c2c66affSColin Finck if (V_I8(&rv) == 0)
5297c2c66affSColin Finck {
5298c2c66affSColin Finck hres = DISP_E_DIVBYZERO;
5299c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5300c2c66affSColin Finck }
5301c2c66affSColin Finck else
5302c2c66affSColin Finck V_I8(result) = V_I8(&lv) / V_I8(&rv);
5303c2c66affSColin Finck break;
5304c2c66affSColin Finck default:
5305c2c66affSColin Finck FIXME("Couldn't integer divide variant types %d,%d\n",
5306c2c66affSColin Finck leftvt,rightvt);
5307c2c66affSColin Finck }
5308c2c66affSColin Finck
5309c2c66affSColin Finck end:
5310c2c66affSColin Finck VariantClear(&lv);
5311c2c66affSColin Finck VariantClear(&rv);
5312c2c66affSColin Finck VariantClear(&tempLeft);
5313c2c66affSColin Finck VariantClear(&tempRight);
5314c2c66affSColin Finck
5315c2c66affSColin Finck return hres;
5316c2c66affSColin Finck }
5317c2c66affSColin Finck
5318c2c66affSColin Finck
5319c2c66affSColin Finck /**********************************************************************
5320c2c66affSColin Finck * VarMod [OLEAUT32.155]
5321c2c66affSColin Finck *
5322c2c66affSColin Finck * Perform the modulus operation of the right hand variant on the left
5323c2c66affSColin Finck *
5324c2c66affSColin Finck * PARAMS
5325c2c66affSColin Finck * left [I] Left hand variant
5326c2c66affSColin Finck * right [I] Right hand variant
5327c2c66affSColin Finck * result [O] Destination for converted value
5328c2c66affSColin Finck *
5329c2c66affSColin Finck * RETURNS
5330c2c66affSColin Finck * Success: S_OK. result contains the remainder.
5331c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
5332c2c66affSColin Finck *
5333c2c66affSColin Finck * NOTE:
5334c2c66affSColin Finck * If an error occurs the type of result will be modified but the value will not be.
5335c2c66affSColin Finck * Doesn't support arrays or any special flags yet.
5336c2c66affSColin Finck */
VarMod(LPVARIANT left,LPVARIANT right,LPVARIANT result)5337c2c66affSColin Finck HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5338c2c66affSColin Finck {
5339c2c66affSColin Finck BOOL lOk = TRUE;
5340c2c66affSColin Finck HRESULT rc = E_FAIL;
5341c2c66affSColin Finck int resT = 0;
5342c2c66affSColin Finck VARIANT lv,rv;
5343c2c66affSColin Finck VARIANT tempLeft, tempRight;
5344c2c66affSColin Finck
5345c2c66affSColin Finck VariantInit(&tempLeft);
5346c2c66affSColin Finck VariantInit(&tempRight);
5347c2c66affSColin Finck VariantInit(&lv);
5348c2c66affSColin Finck VariantInit(&rv);
5349c2c66affSColin Finck
5350c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5351c2c66affSColin Finck
5352c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
5353c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5354c2c66affSColin Finck {
5355c2c66affSColin Finck rc = VARIANT_FetchDispatchValue(left, &tempLeft);
5356c2c66affSColin Finck if (FAILED(rc)) goto end;
5357c2c66affSColin Finck left = &tempLeft;
5358c2c66affSColin Finck }
5359c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5360c2c66affSColin Finck {
5361c2c66affSColin Finck rc = VARIANT_FetchDispatchValue(right, &tempRight);
5362c2c66affSColin Finck if (FAILED(rc)) goto end;
5363c2c66affSColin Finck right = &tempRight;
5364c2c66affSColin Finck }
5365c2c66affSColin Finck
5366c2c66affSColin Finck /* check for invalid inputs */
5367c2c66affSColin Finck lOk = TRUE;
5368c2c66affSColin Finck switch (V_VT(left) & VT_TYPEMASK) {
5369c2c66affSColin Finck case VT_BOOL :
5370c2c66affSColin Finck case VT_I1 :
5371c2c66affSColin Finck case VT_I2 :
5372c2c66affSColin Finck case VT_I4 :
5373c2c66affSColin Finck case VT_I8 :
5374c2c66affSColin Finck case VT_INT :
5375c2c66affSColin Finck case VT_UI1 :
5376c2c66affSColin Finck case VT_UI2 :
5377c2c66affSColin Finck case VT_UI4 :
5378c2c66affSColin Finck case VT_UI8 :
5379c2c66affSColin Finck case VT_UINT :
5380c2c66affSColin Finck case VT_R4 :
5381c2c66affSColin Finck case VT_R8 :
5382c2c66affSColin Finck case VT_CY :
5383c2c66affSColin Finck case VT_EMPTY:
5384c2c66affSColin Finck case VT_DATE :
5385c2c66affSColin Finck case VT_BSTR :
5386c2c66affSColin Finck case VT_DECIMAL:
5387c2c66affSColin Finck break;
5388c2c66affSColin Finck case VT_VARIANT:
5389c2c66affSColin Finck case VT_UNKNOWN:
5390c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5391c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5392c2c66affSColin Finck goto end;
5393c2c66affSColin Finck case VT_ERROR:
5394c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5395c2c66affSColin Finck goto end;
5396c2c66affSColin Finck case VT_RECORD:
5397c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5398c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5399c2c66affSColin Finck goto end;
5400c2c66affSColin Finck case VT_NULL:
5401c2c66affSColin Finck break;
5402c2c66affSColin Finck default:
5403c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5404c2c66affSColin Finck rc = DISP_E_BADVARTYPE;
5405c2c66affSColin Finck goto end;
5406c2c66affSColin Finck }
5407c2c66affSColin Finck
5408c2c66affSColin Finck
5409c2c66affSColin Finck switch (V_VT(right) & VT_TYPEMASK) {
5410c2c66affSColin Finck case VT_BOOL :
5411c2c66affSColin Finck case VT_I1 :
5412c2c66affSColin Finck case VT_I2 :
5413c2c66affSColin Finck case VT_I4 :
5414c2c66affSColin Finck case VT_I8 :
5415c2c66affSColin Finck if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
5416c2c66affSColin Finck {
5417c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5418c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5419c2c66affSColin Finck goto end;
5420c2c66affSColin Finck }
5421c2c66affSColin Finck case VT_INT :
5422c2c66affSColin Finck if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
5423c2c66affSColin Finck {
5424c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5425c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5426c2c66affSColin Finck goto end;
5427c2c66affSColin Finck }
5428c2c66affSColin Finck case VT_UI1 :
5429c2c66affSColin Finck case VT_UI2 :
5430c2c66affSColin Finck case VT_UI4 :
5431c2c66affSColin Finck case VT_UI8 :
5432c2c66affSColin Finck case VT_UINT :
5433c2c66affSColin Finck case VT_R4 :
5434c2c66affSColin Finck case VT_R8 :
5435c2c66affSColin Finck case VT_CY :
5436c2c66affSColin Finck if(V_VT(left) == VT_EMPTY)
5437c2c66affSColin Finck {
5438c2c66affSColin Finck V_VT(result) = VT_I4;
5439c2c66affSColin Finck rc = S_OK;
5440c2c66affSColin Finck goto end;
5441c2c66affSColin Finck }
5442c2c66affSColin Finck case VT_EMPTY:
5443c2c66affSColin Finck case VT_DATE :
5444c2c66affSColin Finck case VT_DECIMAL:
5445c2c66affSColin Finck if(V_VT(left) == VT_ERROR)
5446c2c66affSColin Finck {
5447c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5448c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5449c2c66affSColin Finck goto end;
5450c2c66affSColin Finck }
5451c2c66affSColin Finck case VT_BSTR:
5452c2c66affSColin Finck if(V_VT(left) == VT_NULL)
5453c2c66affSColin Finck {
5454c2c66affSColin Finck V_VT(result) = VT_NULL;
5455c2c66affSColin Finck rc = S_OK;
5456c2c66affSColin Finck goto end;
5457c2c66affSColin Finck }
5458c2c66affSColin Finck break;
5459c2c66affSColin Finck
5460c2c66affSColin Finck case VT_VOID:
5461c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5462c2c66affSColin Finck rc = DISP_E_BADVARTYPE;
5463c2c66affSColin Finck goto end;
5464c2c66affSColin Finck case VT_NULL:
5465c2c66affSColin Finck if(V_VT(left) == VT_VOID)
5466c2c66affSColin Finck {
5467c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5468c2c66affSColin Finck rc = DISP_E_BADVARTYPE;
5469c2c66affSColin Finck } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
5470c2c66affSColin Finck lOk)
5471c2c66affSColin Finck {
5472c2c66affSColin Finck V_VT(result) = VT_NULL;
5473c2c66affSColin Finck rc = S_OK;
5474c2c66affSColin Finck } else
5475c2c66affSColin Finck {
5476c2c66affSColin Finck V_VT(result) = VT_NULL;
5477c2c66affSColin Finck rc = DISP_E_BADVARTYPE;
5478c2c66affSColin Finck }
5479c2c66affSColin Finck goto end;
5480c2c66affSColin Finck case VT_VARIANT:
5481c2c66affSColin Finck case VT_UNKNOWN:
5482c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5483c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5484c2c66affSColin Finck goto end;
5485c2c66affSColin Finck case VT_ERROR:
5486c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5487c2c66affSColin Finck goto end;
5488c2c66affSColin Finck case VT_RECORD:
5489c2c66affSColin Finck if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
5490c2c66affSColin Finck {
5491c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5492c2c66affSColin Finck rc = DISP_E_BADVARTYPE;
5493c2c66affSColin Finck } else
5494c2c66affSColin Finck {
5495c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5496c2c66affSColin Finck rc = DISP_E_TYPEMISMATCH;
5497c2c66affSColin Finck }
5498c2c66affSColin Finck goto end;
5499c2c66affSColin Finck default:
5500c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5501c2c66affSColin Finck rc = DISP_E_BADVARTYPE;
5502c2c66affSColin Finck goto end;
5503c2c66affSColin Finck }
5504c2c66affSColin Finck
5505c2c66affSColin Finck /* determine the result type */
5506c2c66affSColin Finck if((V_VT(left) == VT_I8) || (V_VT(right) == VT_I8)) resT = VT_I8;
5507c2c66affSColin Finck else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5508c2c66affSColin Finck else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_UI1)) resT = VT_UI1;
5509c2c66affSColin Finck else if((V_VT(left) == VT_UI1) && (V_VT(right) == VT_I2)) resT = VT_I2;
5510c2c66affSColin Finck else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5511c2c66affSColin Finck else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5512c2c66affSColin Finck else if((V_VT(left) == VT_I2) && (V_VT(right) == VT_I2)) resT = VT_I2;
5513c2c66affSColin Finck else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
5514c2c66affSColin Finck else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1)) resT = VT_I2;
5515c2c66affSColin Finck else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2)) resT = VT_I2;
5516c2c66affSColin Finck else resT = VT_I4; /* most outputs are I4 */
5517c2c66affSColin Finck
5518c2c66affSColin Finck /* convert to I8 for the modulo */
5519c2c66affSColin Finck rc = VariantChangeType(&lv, left, 0, VT_I8);
5520c2c66affSColin Finck if(FAILED(rc))
5521c2c66affSColin Finck {
5522c2c66affSColin Finck FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
5523c2c66affSColin Finck goto end;
5524c2c66affSColin Finck }
5525c2c66affSColin Finck
5526c2c66affSColin Finck rc = VariantChangeType(&rv, right, 0, VT_I8);
5527c2c66affSColin Finck if(FAILED(rc))
5528c2c66affSColin Finck {
5529c2c66affSColin Finck FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
5530c2c66affSColin Finck goto end;
5531c2c66affSColin Finck }
5532c2c66affSColin Finck
5533c2c66affSColin Finck /* if right is zero set VT_EMPTY and return divide by zero */
5534c2c66affSColin Finck if(V_I8(&rv) == 0)
5535c2c66affSColin Finck {
5536c2c66affSColin Finck V_VT(result) = VT_EMPTY;
5537c2c66affSColin Finck rc = DISP_E_DIVBYZERO;
5538c2c66affSColin Finck goto end;
5539c2c66affSColin Finck }
5540c2c66affSColin Finck
5541c2c66affSColin Finck /* perform the modulo operation */
5542c2c66affSColin Finck V_VT(result) = VT_I8;
5543c2c66affSColin Finck V_I8(result) = V_I8(&lv) % V_I8(&rv);
5544c2c66affSColin Finck
5545c2c66affSColin Finck TRACE("V_I8(left) == %s, V_I8(right) == %s, V_I8(result) == %s\n",
5546c2c66affSColin Finck wine_dbgstr_longlong(V_I8(&lv)), wine_dbgstr_longlong(V_I8(&rv)),
5547c2c66affSColin Finck wine_dbgstr_longlong(V_I8(result)));
5548c2c66affSColin Finck
5549c2c66affSColin Finck /* convert left and right to the destination type */
5550c2c66affSColin Finck rc = VariantChangeType(result, result, 0, resT);
5551c2c66affSColin Finck if(FAILED(rc))
5552c2c66affSColin Finck {
5553c2c66affSColin Finck FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
5554c2c66affSColin Finck /* fall to end of function */
5555c2c66affSColin Finck }
5556c2c66affSColin Finck
5557c2c66affSColin Finck end:
5558c2c66affSColin Finck VariantClear(&lv);
5559c2c66affSColin Finck VariantClear(&rv);
5560c2c66affSColin Finck VariantClear(&tempLeft);
5561c2c66affSColin Finck VariantClear(&tempRight);
5562c2c66affSColin Finck return rc;
5563c2c66affSColin Finck }
5564c2c66affSColin Finck
5565c2c66affSColin Finck /**********************************************************************
5566c2c66affSColin Finck * VarPow [OLEAUT32.158]
5567c2c66affSColin Finck *
5568c2c66affSColin Finck * Computes the power of one variant to another variant.
5569c2c66affSColin Finck *
5570c2c66affSColin Finck * PARAMS
5571c2c66affSColin Finck * left [I] First variant
5572c2c66affSColin Finck * right [I] Second variant
5573c2c66affSColin Finck * result [O] Result variant
5574c2c66affSColin Finck *
5575c2c66affSColin Finck * RETURNS
5576c2c66affSColin Finck * Success: S_OK.
5577c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
5578c2c66affSColin Finck */
VarPow(LPVARIANT left,LPVARIANT right,LPVARIANT result)5579c2c66affSColin Finck HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5580c2c66affSColin Finck {
5581c2c66affSColin Finck HRESULT hr = S_OK;
5582c2c66affSColin Finck VARIANT dl,dr;
5583c2c66affSColin Finck VARTYPE resvt = VT_EMPTY;
5584c2c66affSColin Finck VARTYPE leftvt,rightvt;
5585c2c66affSColin Finck VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5586c2c66affSColin Finck VARIANT tempLeft, tempRight;
5587c2c66affSColin Finck
5588c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5589c2c66affSColin Finck
5590c2c66affSColin Finck VariantInit(&dl);
5591c2c66affSColin Finck VariantInit(&dr);
5592c2c66affSColin Finck VariantInit(&tempLeft);
5593c2c66affSColin Finck VariantInit(&tempRight);
5594c2c66affSColin Finck
5595c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
5596c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5597c2c66affSColin Finck {
5598c2c66affSColin Finck hr = VARIANT_FetchDispatchValue(left, &tempLeft);
5599c2c66affSColin Finck if (FAILED(hr)) goto end;
5600c2c66affSColin Finck left = &tempLeft;
5601c2c66affSColin Finck }
5602c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5603c2c66affSColin Finck {
5604c2c66affSColin Finck hr = VARIANT_FetchDispatchValue(right, &tempRight);
5605c2c66affSColin Finck if (FAILED(hr)) goto end;
5606c2c66affSColin Finck right = &tempRight;
5607c2c66affSColin Finck }
5608c2c66affSColin Finck
5609c2c66affSColin Finck leftvt = V_VT(left)&VT_TYPEMASK;
5610c2c66affSColin Finck rightvt = V_VT(right)&VT_TYPEMASK;
5611c2c66affSColin Finck leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5612c2c66affSColin Finck rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5613c2c66affSColin Finck
5614c2c66affSColin Finck if (leftExtraFlags != rightExtraFlags)
5615c2c66affSColin Finck {
5616c2c66affSColin Finck hr = DISP_E_BADVARTYPE;
5617c2c66affSColin Finck goto end;
5618c2c66affSColin Finck }
5619c2c66affSColin Finck ExtraFlags = leftExtraFlags;
5620c2c66affSColin Finck
5621c2c66affSColin Finck /* Native VarPow always returns an error when using extra flags */
5622c2c66affSColin Finck if (ExtraFlags != 0)
5623c2c66affSColin Finck {
5624c2c66affSColin Finck hr = DISP_E_BADVARTYPE;
5625c2c66affSColin Finck goto end;
5626c2c66affSColin Finck }
5627c2c66affSColin Finck
5628c2c66affSColin Finck /* Determine return type */
5629c2c66affSColin Finck else if (leftvt == VT_NULL || rightvt == VT_NULL) {
5630c2c66affSColin Finck V_VT(result) = VT_NULL;
5631c2c66affSColin Finck hr = S_OK;
5632c2c66affSColin Finck goto end;
5633c2c66affSColin Finck }
5634c2c66affSColin Finck else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
5635c2c66affSColin Finck leftvt == VT_I4 || leftvt == VT_R4 ||
5636c2c66affSColin Finck leftvt == VT_R8 || leftvt == VT_CY ||
5637c2c66affSColin Finck leftvt == VT_DATE || leftvt == VT_BSTR ||
5638c2c66affSColin Finck leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
5639c2c66affSColin Finck (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
5640c2c66affSColin Finck (rightvt == VT_EMPTY || rightvt == VT_I2 ||
5641c2c66affSColin Finck rightvt == VT_I4 || rightvt == VT_R4 ||
5642c2c66affSColin Finck rightvt == VT_R8 || rightvt == VT_CY ||
5643c2c66affSColin Finck rightvt == VT_DATE || rightvt == VT_BSTR ||
5644c2c66affSColin Finck rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
5645c2c66affSColin Finck (rightvt >= VT_I1 && rightvt <= VT_UINT)))
5646c2c66affSColin Finck resvt = VT_R8;
5647c2c66affSColin Finck else
5648c2c66affSColin Finck {
5649c2c66affSColin Finck hr = DISP_E_BADVARTYPE;
5650c2c66affSColin Finck goto end;
5651c2c66affSColin Finck }
5652c2c66affSColin Finck
5653c2c66affSColin Finck hr = VariantChangeType(&dl,left,0,resvt);
5654c2c66affSColin Finck if (FAILED(hr)) {
5655c2c66affSColin Finck ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5656c2c66affSColin Finck hr = E_FAIL;
5657c2c66affSColin Finck goto end;
5658c2c66affSColin Finck }
5659c2c66affSColin Finck
5660c2c66affSColin Finck hr = VariantChangeType(&dr,right,0,resvt);
5661c2c66affSColin Finck if (FAILED(hr)) {
5662c2c66affSColin Finck ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5663c2c66affSColin Finck hr = E_FAIL;
5664c2c66affSColin Finck goto end;
5665c2c66affSColin Finck }
5666c2c66affSColin Finck
5667c2c66affSColin Finck V_VT(result) = VT_R8;
5668c2c66affSColin Finck V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5669c2c66affSColin Finck
5670c2c66affSColin Finck end:
5671c2c66affSColin Finck VariantClear(&dl);
5672c2c66affSColin Finck VariantClear(&dr);
5673c2c66affSColin Finck VariantClear(&tempLeft);
5674c2c66affSColin Finck VariantClear(&tempRight);
5675c2c66affSColin Finck
5676c2c66affSColin Finck return hr;
5677c2c66affSColin Finck }
5678c2c66affSColin Finck
5679c2c66affSColin Finck /**********************************************************************
5680c2c66affSColin Finck * VarImp [OLEAUT32.154]
5681c2c66affSColin Finck *
5682c2c66affSColin Finck * Bitwise implication of two variants.
5683c2c66affSColin Finck *
5684c2c66affSColin Finck * PARAMS
5685c2c66affSColin Finck * left [I] First variant
5686c2c66affSColin Finck * right [I] Second variant
5687c2c66affSColin Finck * result [O] Result variant
5688c2c66affSColin Finck *
5689c2c66affSColin Finck * RETURNS
5690c2c66affSColin Finck * Success: S_OK.
5691c2c66affSColin Finck * Failure: An HRESULT error code indicating the error.
5692c2c66affSColin Finck */
VarImp(LPVARIANT left,LPVARIANT right,LPVARIANT result)5693c2c66affSColin Finck HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5694c2c66affSColin Finck {
5695c2c66affSColin Finck HRESULT hres = S_OK;
5696c2c66affSColin Finck VARTYPE resvt = VT_EMPTY;
5697c2c66affSColin Finck VARTYPE leftvt,rightvt;
5698c2c66affSColin Finck VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5699c2c66affSColin Finck VARIANT lv,rv;
5700c2c66affSColin Finck double d;
5701c2c66affSColin Finck VARIANT tempLeft, tempRight;
5702c2c66affSColin Finck
5703c2c66affSColin Finck VariantInit(&lv);
5704c2c66affSColin Finck VariantInit(&rv);
5705c2c66affSColin Finck VariantInit(&tempLeft);
5706c2c66affSColin Finck VariantInit(&tempRight);
5707c2c66affSColin Finck
5708c2c66affSColin Finck TRACE("(%s,%s,%p)\n", debugstr_variant(left), debugstr_variant(right), result);
5709c2c66affSColin Finck
5710c2c66affSColin Finck /* Handle VT_DISPATCH by storing and taking address of returned value */
5711c2c66affSColin Finck if ((V_VT(left) & VT_TYPEMASK) == VT_DISPATCH)
5712c2c66affSColin Finck {
5713c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(left, &tempLeft);
5714c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5715c2c66affSColin Finck left = &tempLeft;
5716c2c66affSColin Finck }
5717c2c66affSColin Finck if ((V_VT(right) & VT_TYPEMASK) == VT_DISPATCH)
5718c2c66affSColin Finck {
5719c2c66affSColin Finck hres = VARIANT_FetchDispatchValue(right, &tempRight);
5720c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5721c2c66affSColin Finck right = &tempRight;
5722c2c66affSColin Finck }
5723c2c66affSColin Finck
5724c2c66affSColin Finck leftvt = V_VT(left)&VT_TYPEMASK;
5725c2c66affSColin Finck rightvt = V_VT(right)&VT_TYPEMASK;
5726c2c66affSColin Finck leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
5727c2c66affSColin Finck rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
5728c2c66affSColin Finck
5729c2c66affSColin Finck if (leftExtraFlags != rightExtraFlags)
5730c2c66affSColin Finck {
5731c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
5732c2c66affSColin Finck goto VarImp_Exit;
5733c2c66affSColin Finck }
5734c2c66affSColin Finck ExtraFlags = leftExtraFlags;
5735c2c66affSColin Finck
5736c2c66affSColin Finck /* Native VarImp always returns an error when using extra
5737c2c66affSColin Finck * flags or if the variants are I8 and INT.
5738c2c66affSColin Finck */
5739c2c66affSColin Finck if ((leftvt == VT_I8 && rightvt == VT_INT) ||
5740c2c66affSColin Finck ExtraFlags != 0)
5741c2c66affSColin Finck {
5742c2c66affSColin Finck hres = DISP_E_BADVARTYPE;
5743c2c66affSColin Finck goto VarImp_Exit;
5744c2c66affSColin Finck }
5745c2c66affSColin Finck
5746c2c66affSColin Finck /* Determine result type */
5747c2c66affSColin Finck else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
5748c2c66affSColin Finck (leftvt == VT_NULL && rightvt == VT_EMPTY))
5749c2c66affSColin Finck {
5750c2c66affSColin Finck V_VT(result) = VT_NULL;
5751c2c66affSColin Finck hres = S_OK;
5752c2c66affSColin Finck goto VarImp_Exit;
5753c2c66affSColin Finck }
5754c2c66affSColin Finck else if (leftvt == VT_I8 || rightvt == VT_I8)
5755c2c66affSColin Finck resvt = VT_I8;
5756c2c66affSColin Finck else if (leftvt == VT_I4 || rightvt == VT_I4 ||
5757c2c66affSColin Finck leftvt == VT_INT || rightvt == VT_INT ||
5758c2c66affSColin Finck leftvt == VT_UINT || rightvt == VT_UINT ||
5759c2c66affSColin Finck leftvt == VT_UI4 || rightvt == VT_UI4 ||
5760c2c66affSColin Finck leftvt == VT_UI8 || rightvt == VT_UI8 ||
5761c2c66affSColin Finck leftvt == VT_UI2 || rightvt == VT_UI2 ||
5762c2c66affSColin Finck leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
5763c2c66affSColin Finck leftvt == VT_DATE || rightvt == VT_DATE ||
5764c2c66affSColin Finck leftvt == VT_CY || rightvt == VT_CY ||
5765c2c66affSColin Finck leftvt == VT_R8 || rightvt == VT_R8 ||
5766c2c66affSColin Finck leftvt == VT_R4 || rightvt == VT_R4 ||
5767c2c66affSColin Finck leftvt == VT_I1 || rightvt == VT_I1)
5768c2c66affSColin Finck resvt = VT_I4;
5769c2c66affSColin Finck else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
5770c2c66affSColin Finck (leftvt == VT_UI1 && rightvt == VT_NULL) ||
5771c2c66affSColin Finck (leftvt == VT_NULL && rightvt == VT_UI1))
5772c2c66affSColin Finck resvt = VT_UI1;
5773c2c66affSColin Finck else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
5774c2c66affSColin Finck leftvt == VT_I2 || rightvt == VT_I2 ||
5775c2c66affSColin Finck leftvt == VT_UI1 || rightvt == VT_UI1)
5776c2c66affSColin Finck resvt = VT_I2;
5777c2c66affSColin Finck else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
5778c2c66affSColin Finck leftvt == VT_BSTR || rightvt == VT_BSTR)
5779c2c66affSColin Finck resvt = VT_BOOL;
5780c2c66affSColin Finck
5781c2c66affSColin Finck /* VT_NULL requires special handling for when the opposite
5782c2c66affSColin Finck * variant is equal to something other than -1.
5783c2c66affSColin Finck * (NULL Imp 0 = NULL, NULL Imp n = n)
5784c2c66affSColin Finck */
5785c2c66affSColin Finck if (leftvt == VT_NULL)
5786c2c66affSColin Finck {
5787c2c66affSColin Finck VARIANT_BOOL b;
5788c2c66affSColin Finck switch(rightvt)
5789c2c66affSColin Finck {
5790c2c66affSColin Finck case VT_I1: if (!V_I1(right)) resvt = VT_NULL; break;
5791c2c66affSColin Finck case VT_UI1: if (!V_UI1(right)) resvt = VT_NULL; break;
5792c2c66affSColin Finck case VT_I2: if (!V_I2(right)) resvt = VT_NULL; break;
5793c2c66affSColin Finck case VT_UI2: if (!V_UI2(right)) resvt = VT_NULL; break;
5794c2c66affSColin Finck case VT_I4: if (!V_I4(right)) resvt = VT_NULL; break;
5795c2c66affSColin Finck case VT_UI4: if (!V_UI4(right)) resvt = VT_NULL; break;
5796c2c66affSColin Finck case VT_I8: if (!V_I8(right)) resvt = VT_NULL; break;
5797c2c66affSColin Finck case VT_UI8: if (!V_UI8(right)) resvt = VT_NULL; break;
5798c2c66affSColin Finck case VT_INT: if (!V_INT(right)) resvt = VT_NULL; break;
5799c2c66affSColin Finck case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
5800c2c66affSColin Finck case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
5801c2c66affSColin Finck case VT_R4: if (!V_R4(right)) resvt = VT_NULL; break;
5802c2c66affSColin Finck case VT_R8: if (!V_R8(right)) resvt = VT_NULL; break;
5803c2c66affSColin Finck case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
5804c2c66affSColin Finck case VT_CY: if (!V_CY(right).int64) resvt = VT_NULL; break;
5805c2c66affSColin Finck case VT_DECIMAL:
5806c2c66affSColin Finck if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
5807c2c66affSColin Finck resvt = VT_NULL;
5808c2c66affSColin Finck break;
5809c2c66affSColin Finck case VT_BSTR:
5810c2c66affSColin Finck hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5811c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5812c2c66affSColin Finck else if (!b)
5813c2c66affSColin Finck V_VT(result) = VT_NULL;
5814c2c66affSColin Finck else
5815c2c66affSColin Finck {
5816c2c66affSColin Finck V_VT(result) = VT_BOOL;
5817c2c66affSColin Finck V_BOOL(result) = b;
5818c2c66affSColin Finck }
5819c2c66affSColin Finck goto VarImp_Exit;
5820c2c66affSColin Finck }
5821c2c66affSColin Finck if (resvt == VT_NULL)
5822c2c66affSColin Finck {
5823c2c66affSColin Finck V_VT(result) = resvt;
5824c2c66affSColin Finck goto VarImp_Exit;
5825c2c66affSColin Finck }
5826c2c66affSColin Finck else
5827c2c66affSColin Finck {
5828c2c66affSColin Finck hres = VariantChangeType(result,right,0,resvt);
5829c2c66affSColin Finck goto VarImp_Exit;
5830c2c66affSColin Finck }
5831c2c66affSColin Finck }
5832c2c66affSColin Finck
5833c2c66affSColin Finck /* Special handling is required when NULL is the right variant.
5834c2c66affSColin Finck * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
5835c2c66affSColin Finck */
5836c2c66affSColin Finck else if (rightvt == VT_NULL)
5837c2c66affSColin Finck {
5838c2c66affSColin Finck VARIANT_BOOL b;
5839c2c66affSColin Finck switch(leftvt)
5840c2c66affSColin Finck {
5841c2c66affSColin Finck case VT_I1: if (V_I1(left) == -1) resvt = VT_NULL; break;
5842c2c66affSColin Finck case VT_UI1: if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
5843c2c66affSColin Finck case VT_I2: if (V_I2(left) == -1) resvt = VT_NULL; break;
5844c2c66affSColin Finck case VT_UI2: if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
5845c2c66affSColin Finck case VT_INT: if (V_INT(left) == -1) resvt = VT_NULL; break;
5846c2c66affSColin Finck case VT_UINT: if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
5847c2c66affSColin Finck case VT_I4: if (V_I4(left) == -1) resvt = VT_NULL; break;
5848c2c66affSColin Finck case VT_UI4: if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
5849c2c66affSColin Finck case VT_I8: if (V_I8(left) == -1) resvt = VT_NULL; break;
5850c2c66affSColin Finck case VT_UI8: if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
5851c2c66affSColin Finck case VT_BOOL: if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
5852c2c66affSColin Finck case VT_R4: if (V_R4(left) == -1.0) resvt = VT_NULL; break;
5853c2c66affSColin Finck case VT_R8: if (V_R8(left) == -1.0) resvt = VT_NULL; break;
5854c2c66affSColin Finck case VT_CY: if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
5855c2c66affSColin Finck case VT_DECIMAL:
5856c2c66affSColin Finck if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
5857c2c66affSColin Finck resvt = VT_NULL;
5858c2c66affSColin Finck break;
5859c2c66affSColin Finck case VT_BSTR:
5860c2c66affSColin Finck hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
5861c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5862c2c66affSColin Finck else if (b == VARIANT_TRUE)
5863c2c66affSColin Finck resvt = VT_NULL;
5864c2c66affSColin Finck }
5865c2c66affSColin Finck if (resvt == VT_NULL)
5866c2c66affSColin Finck {
5867c2c66affSColin Finck V_VT(result) = resvt;
5868c2c66affSColin Finck goto VarImp_Exit;
5869c2c66affSColin Finck }
5870c2c66affSColin Finck }
5871c2c66affSColin Finck
5872c2c66affSColin Finck hres = VariantCopy(&lv, left);
5873c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5874c2c66affSColin Finck
5875c2c66affSColin Finck if (rightvt == VT_NULL)
5876c2c66affSColin Finck {
5877c2c66affSColin Finck memset( &rv, 0, sizeof(rv) );
5878c2c66affSColin Finck V_VT(&rv) = resvt;
5879c2c66affSColin Finck }
5880c2c66affSColin Finck else
5881c2c66affSColin Finck {
5882c2c66affSColin Finck hres = VariantCopy(&rv, right);
5883c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5884c2c66affSColin Finck }
5885c2c66affSColin Finck
5886c2c66affSColin Finck if (V_VT(&lv) == VT_BSTR &&
5887c2c66affSColin Finck FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
5888c2c66affSColin Finck hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
5889c2c66affSColin Finck if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
5890c2c66affSColin Finck hres = VariantChangeType(&lv,&lv,0,resvt);
5891c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5892c2c66affSColin Finck
5893c2c66affSColin Finck if (V_VT(&rv) == VT_BSTR &&
5894c2c66affSColin Finck FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
5895c2c66affSColin Finck hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
5896c2c66affSColin Finck if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
5897c2c66affSColin Finck hres = VariantChangeType(&rv, &rv, 0, resvt);
5898c2c66affSColin Finck if (FAILED(hres)) goto VarImp_Exit;
5899c2c66affSColin Finck
5900c2c66affSColin Finck /* do the math */
5901c2c66affSColin Finck V_VT(result) = resvt;
5902c2c66affSColin Finck switch (resvt)
5903c2c66affSColin Finck {
5904c2c66affSColin Finck case VT_I8:
5905c2c66affSColin Finck V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
5906c2c66affSColin Finck break;
5907c2c66affSColin Finck case VT_I4:
5908c2c66affSColin Finck V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
5909c2c66affSColin Finck break;
5910c2c66affSColin Finck case VT_I2:
5911c2c66affSColin Finck V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
5912c2c66affSColin Finck break;
5913c2c66affSColin Finck case VT_UI1:
5914c2c66affSColin Finck V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
5915c2c66affSColin Finck break;
5916c2c66affSColin Finck case VT_BOOL:
5917c2c66affSColin Finck V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
5918c2c66affSColin Finck break;
5919c2c66affSColin Finck default:
5920c2c66affSColin Finck FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
5921c2c66affSColin Finck leftvt,rightvt);
5922c2c66affSColin Finck }
5923c2c66affSColin Finck
5924c2c66affSColin Finck VarImp_Exit:
5925c2c66affSColin Finck
5926c2c66affSColin Finck VariantClear(&lv);
5927c2c66affSColin Finck VariantClear(&rv);
5928c2c66affSColin Finck VariantClear(&tempLeft);
5929c2c66affSColin Finck VariantClear(&tempRight);
5930c2c66affSColin Finck
5931c2c66affSColin Finck return hres;
5932c2c66affSColin Finck }
5933