variant.c 175 KB
Newer Older
1 2 3 4
/*
 * VARIANT
 *
 * Copyright 1998 Jean-Claude Cote
5
 * Copyright 2003 Jon Griffiths
Daniel Remenak's avatar
Daniel Remenak committed
6
 * Copyright 2005 Daniel Remenak
7
 * Copyright 2006 Google (Benjamin Arai)
Daniel Remenak's avatar
Daniel Remenak committed
8
 *
9 10 11
 * The alorithm for conversion from Julian days to day/month/year is based on
 * that devised by Henry Fliegel, as implemented in PostgreSQL, which is
 * Copyright 1994-7 Regents of the University of California
12
 *
13 14 15 16 17 18 19 20 21 22 23 24
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26
 */
27 28

#include "config.h"
29

30 31
#include <string.h>
#include <stdlib.h>
32
#include <stdarg.h>
33

34
#define COBJMACROS
35 36
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
37

38
#include "windef.h"
39
#include "winbase.h"
40
#include "wine/unicode.h"
41
#include "winerror.h"
42
#include "variant.h"
43
#include "wine/debug.h"
44

45
WINE_DEFAULT_DEBUG_CHANNEL(variant);
46

47
const char * const wine_vtypes[VT_CLSID+1] =
48 49 50 51 52
{
  "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
  "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
  "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
  "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
53
  "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
54 55 56 57 58
  "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
  "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
  "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
  "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
};
59

60
const char * const wine_vflags[16] =
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
{
 "",
 "|VT_VECTOR",
 "|VT_ARRAY",
 "|VT_VECTOR|VT_ARRAY",
 "|VT_BYREF",
 "|VT_VECTOR|VT_ARRAY",
 "|VT_ARRAY|VT_BYREF",
 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
 "|VT_HARDTYPE",
 "|VT_VECTOR|VT_HARDTYPE",
 "|VT_ARRAY|VT_HARDTYPE",
 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
 "|VT_BYREF|VT_HARDTYPE",
 "|VT_VECTOR|VT_ARRAY|VT_HARDTYPE",
 "|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_HARDTYPE",
};
79

80 81 82
/* Convert a variant from one type to another */
static inline HRESULT VARIANT_Coerce(VARIANTARG* pd, LCID lcid, USHORT wFlags,
                                     VARIANTARG* ps, VARTYPE vt)
83
{
84 85 86
  HRESULT res = DISP_E_TYPEMISMATCH;
  VARTYPE vtFrom =  V_TYPE(ps);
  DWORD dwFlags = 0;
87

88
  TRACE("(%p->(%s%s),0x%08x,0x%04x,%p->(%s%s),%s%s)\n", pd, debugstr_VT(pd),
89 90
        debugstr_VF(pd), lcid, wFlags, ps, debugstr_VT(ps), debugstr_VF(ps),
        debugstr_vt(vt), debugstr_vf(vt));
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  if (vt == VT_BSTR || vtFrom == VT_BSTR)
  {
    /* All flags passed to low level function are only used for
     * changing to or from strings. Map these here.
     */
    if (wFlags & VARIANT_LOCALBOOL)
      dwFlags |= VAR_LOCALBOOL;
    if (wFlags & VARIANT_CALENDAR_HIJRI)
      dwFlags |= VAR_CALENDAR_HIJRI;
    if (wFlags & VARIANT_CALENDAR_THAI)
      dwFlags |= VAR_CALENDAR_THAI;
    if (wFlags & VARIANT_CALENDAR_GREGORIAN)
      dwFlags |= VAR_CALENDAR_GREGORIAN;
    if (wFlags & VARIANT_NOUSEROVERRIDE)
      dwFlags |= LOCALE_NOUSEROVERRIDE;
    if (wFlags & VARIANT_USE_NLS)
      dwFlags |= LOCALE_USE_NLS;
  }
110

111 112 113 114 115
  /* Map int/uint to i4/ui4 */
  if (vt == VT_INT)
    vt = VT_I4;
  else if (vt == VT_UINT)
    vt = VT_UI4;
116

117 118 119
  if (vtFrom == VT_INT)
    vtFrom = VT_I4;
  else if (vtFrom == VT_UINT)
120
    vtFrom = VT_UI4;
121 122 123

  if (vt == vtFrom)
     return VariantCopy(pd, ps);
124

125 126 127 128
  if (wFlags & VARIANT_NOVALUEPROP && vtFrom == VT_DISPATCH && vt != VT_UNKNOWN)
  {
    /* VARIANT_NOVALUEPROP prevents IDispatch objects from being coerced by
     * accessing the default object property.
129
     */
130 131
    return DISP_E_TYPEMISMATCH;
  }
132

133 134 135 136 137 138 139 140
  switch (vt)
  {
  case VT_EMPTY:
    if (vtFrom == VT_NULL)
      return DISP_E_TYPEMISMATCH;
    /* ... Fall through */
  case VT_NULL:
    if (vtFrom <= VT_UINT && vtFrom != (VARTYPE)15 && vtFrom != VT_ERROR)
141
    {
142 143 144
      res = VariantClear( pd );
      if (vt == VT_NULL && SUCCEEDED(res))
        V_VT(pd) = VT_NULL;
145
    }
146 147 148 149 150 151 152 153
    return res;

  case VT_I1:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_I1(pd) = 0; return S_OK;
    case VT_I2:       return VarI1FromI2(V_I2(ps), &V_I1(pd));
    case VT_I4:       return VarI1FromI4(V_I4(ps), &V_I1(pd));
154
    case VT_UI1:      V_I1(pd) = V_UI1(ps); return S_OK;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    case VT_UI2:      return VarI1FromUI2(V_UI2(ps), &V_I1(pd));
    case VT_UI4:      return VarI1FromUI4(V_UI4(ps), &V_I1(pd));
    case VT_I8:       return VarI1FromI8(V_I8(ps), &V_I1(pd));
    case VT_UI8:      return VarI1FromUI8(V_UI8(ps), &V_I1(pd));
    case VT_R4:       return VarI1FromR4(V_R4(ps), &V_I1(pd));
    case VT_R8:       return VarI1FromR8(V_R8(ps), &V_I1(pd));
    case VT_DATE:     return VarI1FromDate(V_DATE(ps), &V_I1(pd));
    case VT_BOOL:     return VarI1FromBool(V_BOOL(ps), &V_I1(pd));
    case VT_CY:       return VarI1FromCy(V_CY(ps), &V_I1(pd));
    case VT_DECIMAL:  return VarI1FromDec(&V_DECIMAL(ps), &V_I1(pd) );
    case VT_DISPATCH: return VarI1FromDisp(V_DISPATCH(ps), lcid, &V_I1(pd) );
    case VT_BSTR:     return VarI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_I1(pd) );
    }
    break;

  case VT_I2:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_I2(pd) = 0; return S_OK;
    case VT_I1:       return VarI2FromI1(V_I1(ps), &V_I2(pd));
    case VT_I4:       return VarI2FromI4(V_I4(ps), &V_I2(pd));
    case VT_UI1:      return VarI2FromUI1(V_UI1(ps), &V_I2(pd));
177
    case VT_UI2:      V_I2(pd) = V_UI2(ps); return S_OK;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    case VT_UI4:      return VarI2FromUI4(V_UI4(ps), &V_I2(pd));
    case VT_I8:       return VarI2FromI8(V_I8(ps), &V_I2(pd));
    case VT_UI8:      return VarI2FromUI8(V_UI8(ps), &V_I2(pd));
    case VT_R4:       return VarI2FromR4(V_R4(ps), &V_I2(pd));
    case VT_R8:       return VarI2FromR8(V_R8(ps), &V_I2(pd));
    case VT_DATE:     return VarI2FromDate(V_DATE(ps), &V_I2(pd));
    case VT_BOOL:     return VarI2FromBool(V_BOOL(ps), &V_I2(pd));
    case VT_CY:       return VarI2FromCy(V_CY(ps), &V_I2(pd));
    case VT_DECIMAL:  return VarI2FromDec(&V_DECIMAL(ps), &V_I2(pd));
    case VT_DISPATCH: return VarI2FromDisp(V_DISPATCH(ps), lcid, &V_I2(pd));
    case VT_BSTR:     return VarI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_I2(pd));
    }
    break;

  case VT_I4:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_I4(pd) = 0; return S_OK;
    case VT_I1:       return VarI4FromI1(V_I1(ps), &V_I4(pd));
    case VT_I2:       return VarI4FromI2(V_I2(ps), &V_I4(pd));
    case VT_UI1:      return VarI4FromUI1(V_UI1(ps), &V_I4(pd));
    case VT_UI2:      return VarI4FromUI2(V_UI2(ps), &V_I4(pd));
200
    case VT_UI4:      V_I4(pd) = V_UI4(ps); return S_OK;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    case VT_I8:       return VarI4FromI8(V_I8(ps), &V_I4(pd));
    case VT_UI8:      return VarI4FromUI8(V_UI8(ps), &V_I4(pd));
    case VT_R4:       return VarI4FromR4(V_R4(ps), &V_I4(pd));
    case VT_R8:       return VarI4FromR8(V_R8(ps), &V_I4(pd));
    case VT_DATE:     return VarI4FromDate(V_DATE(ps), &V_I4(pd));
    case VT_BOOL:     return VarI4FromBool(V_BOOL(ps), &V_I4(pd));
    case VT_CY:       return VarI4FromCy(V_CY(ps), &V_I4(pd));
    case VT_DECIMAL:  return VarI4FromDec(&V_DECIMAL(ps), &V_I4(pd));
    case VT_DISPATCH: return VarI4FromDisp(V_DISPATCH(ps), lcid, &V_I4(pd));
    case VT_BSTR:     return VarI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_I4(pd));
    }
    break;

  case VT_UI1:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_UI1(pd) = 0; return S_OK;
218
    case VT_I1:       V_UI1(pd) = V_I1(ps); return S_OK;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    case VT_I2:       return VarUI1FromI2(V_I2(ps), &V_UI1(pd));
    case VT_I4:       return VarUI1FromI4(V_I4(ps), &V_UI1(pd));
    case VT_UI2:      return VarUI1FromUI2(V_UI2(ps), &V_UI1(pd));
    case VT_UI4:      return VarUI1FromUI4(V_UI4(ps), &V_UI1(pd));
    case VT_I8:       return VarUI1FromI8(V_I8(ps), &V_UI1(pd));
    case VT_UI8:      return VarUI1FromUI8(V_UI8(ps), &V_UI1(pd));
    case VT_R4:       return VarUI1FromR4(V_R4(ps), &V_UI1(pd));
    case VT_R8:       return VarUI1FromR8(V_R8(ps), &V_UI1(pd));
    case VT_DATE:     return VarUI1FromDate(V_DATE(ps), &V_UI1(pd));
    case VT_BOOL:     return VarUI1FromBool(V_BOOL(ps), &V_UI1(pd));
    case VT_CY:       return VarUI1FromCy(V_CY(ps), &V_UI1(pd));
    case VT_DECIMAL:  return VarUI1FromDec(&V_DECIMAL(ps), &V_UI1(pd));
    case VT_DISPATCH: return VarUI1FromDisp(V_DISPATCH(ps), lcid, &V_UI1(pd));
    case VT_BSTR:     return VarUI1FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI1(pd));
    }
    break;

  case VT_UI2:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_UI2(pd) = 0; return S_OK;
    case VT_I1:       return VarUI2FromI1(V_I1(ps), &V_UI2(pd));
241
    case VT_I2:       V_UI2(pd) = V_I2(ps); return S_OK;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    case VT_I4:       return VarUI2FromI4(V_I4(ps), &V_UI2(pd));
    case VT_UI1:      return VarUI2FromUI1(V_UI1(ps), &V_UI2(pd));
    case VT_UI4:      return VarUI2FromUI4(V_UI4(ps), &V_UI2(pd));
    case VT_I8:       return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
    case VT_UI8:      return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
    case VT_R4:       return VarUI2FromR4(V_R4(ps), &V_UI2(pd));
    case VT_R8:       return VarUI2FromR8(V_R8(ps), &V_UI2(pd));
    case VT_DATE:     return VarUI2FromDate(V_DATE(ps), &V_UI2(pd));
    case VT_BOOL:     return VarUI2FromBool(V_BOOL(ps), &V_UI2(pd));
    case VT_CY:       return VarUI2FromCy(V_CY(ps), &V_UI2(pd));
    case VT_DECIMAL:  return VarUI2FromDec(&V_DECIMAL(ps), &V_UI2(pd));
    case VT_DISPATCH: return VarUI2FromDisp(V_DISPATCH(ps), lcid, &V_UI2(pd));
    case VT_BSTR:     return VarUI2FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI2(pd));
    }
    break;

  case VT_UI4:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_UI4(pd) = 0; return S_OK;
    case VT_I1:       return VarUI4FromI1(V_I1(ps), &V_UI4(pd));
    case VT_I2:       return VarUI4FromI2(V_I2(ps), &V_UI4(pd));
264
    case VT_I4:       V_UI4(pd) = V_I4(ps); return S_OK;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    case VT_UI1:      return VarUI4FromUI1(V_UI1(ps), &V_UI4(pd));
    case VT_UI2:      return VarUI4FromUI2(V_UI2(ps), &V_UI4(pd));
    case VT_I8:       return VarUI4FromI8(V_I8(ps), &V_UI4(pd));
    case VT_UI8:      return VarUI4FromUI8(V_UI8(ps), &V_UI4(pd));
    case VT_R4:       return VarUI4FromR4(V_R4(ps), &V_UI4(pd));
    case VT_R8:       return VarUI4FromR8(V_R8(ps), &V_UI4(pd));
    case VT_DATE:     return VarUI4FromDate(V_DATE(ps), &V_UI4(pd));
    case VT_BOOL:     return VarUI4FromBool(V_BOOL(ps), &V_UI4(pd));
    case VT_CY:       return VarUI4FromCy(V_CY(ps), &V_UI4(pd));
    case VT_DECIMAL:  return VarUI4FromDec(&V_DECIMAL(ps), &V_UI4(pd));
    case VT_DISPATCH: return VarUI4FromDisp(V_DISPATCH(ps), lcid, &V_UI4(pd));
    case VT_BSTR:     return VarUI4FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI4(pd));
    }
    break;

  case VT_UI8:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_UI8(pd) = 0; return S_OK;
    case VT_I4:       if (V_I4(ps) < 0) return DISP_E_OVERFLOW; V_UI8(pd) = V_I4(ps); return S_OK;
    case VT_I1:       return VarUI8FromI1(V_I1(ps), &V_UI8(pd));
    case VT_I2:       return VarUI8FromI2(V_I2(ps), &V_UI8(pd));
    case VT_UI1:      return VarUI8FromUI1(V_UI1(ps), &V_UI8(pd));
    case VT_UI2:      return VarUI8FromUI2(V_UI2(ps), &V_UI8(pd));
    case VT_UI4:      return VarUI8FromUI4(V_UI4(ps), &V_UI8(pd));
290
    case VT_I8:       V_UI8(pd) = V_I8(ps); return S_OK;
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    case VT_R4:       return VarUI8FromR4(V_R4(ps), &V_UI8(pd));
    case VT_R8:       return VarUI8FromR8(V_R8(ps), &V_UI8(pd));
    case VT_DATE:     return VarUI8FromDate(V_DATE(ps), &V_UI8(pd));
    case VT_BOOL:     return VarUI8FromBool(V_BOOL(ps), &V_UI8(pd));
    case VT_CY:       return VarUI8FromCy(V_CY(ps), &V_UI8(pd));
    case VT_DECIMAL:  return VarUI8FromDec(&V_DECIMAL(ps), &V_UI8(pd));
    case VT_DISPATCH: return VarUI8FromDisp(V_DISPATCH(ps), lcid, &V_UI8(pd));
    case VT_BSTR:     return VarUI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_UI8(pd));
    }
    break;

  case VT_I8:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_I8(pd) = 0; return S_OK;
    case VT_I4:       V_I8(pd) = V_I4(ps); return S_OK;
    case VT_I1:       return VarI8FromI1(V_I1(ps), &V_I8(pd));
    case VT_I2:       return VarI8FromI2(V_I2(ps), &V_I8(pd));
    case VT_UI1:      return VarI8FromUI1(V_UI1(ps), &V_I8(pd));
    case VT_UI2:      return VarI8FromUI2(V_UI2(ps), &V_I8(pd));
    case VT_UI4:      return VarI8FromUI4(V_UI4(ps), &V_I8(pd));
312
    case VT_UI8:      V_I8(pd) = V_UI8(ps); return S_OK;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    case VT_R4:       return VarI8FromR4(V_R4(ps), &V_I8(pd));
    case VT_R8:       return VarI8FromR8(V_R8(ps), &V_I8(pd));
    case VT_DATE:     return VarI8FromDate(V_DATE(ps), &V_I8(pd));
    case VT_BOOL:     return VarI8FromBool(V_BOOL(ps), &V_I8(pd));
    case VT_CY:       return VarI8FromCy(V_CY(ps), &V_I8(pd));
    case VT_DECIMAL:  return VarI8FromDec(&V_DECIMAL(ps), &V_I8(pd));
    case VT_DISPATCH: return VarI8FromDisp(V_DISPATCH(ps), lcid, &V_I8(pd));
    case VT_BSTR:     return VarI8FromStr(V_BSTR(ps), lcid, dwFlags, &V_I8(pd));
    }
    break;

  case VT_R4:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_R4(pd) = 0.0f; return S_OK;
    case VT_I1:       return VarR4FromI1(V_I1(ps), &V_R4(pd));
    case VT_I2:       return VarR4FromI2(V_I2(ps), &V_R4(pd));
    case VT_I4:       return VarR4FromI4(V_I4(ps), &V_R4(pd));
    case VT_UI1:      return VarR4FromUI1(V_UI1(ps), &V_R4(pd));
    case VT_UI2:      return VarR4FromUI2(V_UI2(ps), &V_R4(pd));
    case VT_UI4:      return VarR4FromUI4(V_UI4(ps), &V_R4(pd));
    case VT_I8:       return VarR4FromI8(V_I8(ps), &V_R4(pd));
    case VT_UI8:      return VarR4FromUI8(V_UI8(ps), &V_R4(pd));
    case VT_R8:       return VarR4FromR8(V_R8(ps), &V_R4(pd));
    case VT_DATE:     return VarR4FromDate(V_DATE(ps), &V_R4(pd));
    case VT_BOOL:     return VarR4FromBool(V_BOOL(ps), &V_R4(pd));
    case VT_CY:       return VarR4FromCy(V_CY(ps), &V_R4(pd));
    case VT_DECIMAL:  return VarR4FromDec(&V_DECIMAL(ps), &V_R4(pd));
    case VT_DISPATCH: return VarR4FromDisp(V_DISPATCH(ps), lcid, &V_R4(pd));
    case VT_BSTR:     return VarR4FromStr(V_BSTR(ps), lcid, dwFlags, &V_R4(pd));
    }
    break;

  case VT_R8:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_R8(pd) = 0.0; return S_OK;
    case VT_I1:       return VarR8FromI1(V_I1(ps), &V_R8(pd));
    case VT_I2:       return VarR8FromI2(V_I2(ps), &V_R8(pd));
    case VT_I4:       return VarR8FromI4(V_I4(ps), &V_R8(pd));
    case VT_UI1:      return VarR8FromUI1(V_UI1(ps), &V_R8(pd));
    case VT_UI2:      return VarR8FromUI2(V_UI2(ps), &V_R8(pd));
    case VT_UI4:      return VarR8FromUI4(V_UI4(ps), &V_R8(pd));
    case VT_I8:       return VarR8FromI8(V_I8(ps), &V_R8(pd));
    case VT_UI8:      return VarR8FromUI8(V_UI8(ps), &V_R8(pd));
    case VT_R4:       return VarR8FromR4(V_R4(ps), &V_R8(pd));
    case VT_DATE:     return VarR8FromDate(V_DATE(ps), &V_R8(pd));
    case VT_BOOL:     return VarR8FromBool(V_BOOL(ps), &V_R8(pd));
    case VT_CY:       return VarR8FromCy(V_CY(ps), &V_R8(pd));
    case VT_DECIMAL:  return VarR8FromDec(&V_DECIMAL(ps), &V_R8(pd));
    case VT_DISPATCH: return VarR8FromDisp(V_DISPATCH(ps), lcid, &V_R8(pd));
    case VT_BSTR:     return VarR8FromStr(V_BSTR(ps), lcid, dwFlags, &V_R8(pd));
    }
    break;

  case VT_DATE:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_DATE(pd) = 0.0; return S_OK;
    case VT_I1:       return VarDateFromI1(V_I1(ps), &V_DATE(pd));
    case VT_I2:       return VarDateFromI2(V_I2(ps), &V_DATE(pd));
    case VT_I4:       return VarDateFromI4(V_I4(ps), &V_DATE(pd));
    case VT_UI1:      return VarDateFromUI1(V_UI1(ps), &V_DATE(pd));
    case VT_UI2:      return VarDateFromUI2(V_UI2(ps), &V_DATE(pd));
    case VT_UI4:      return VarDateFromUI4(V_UI4(ps), &V_DATE(pd));
    case VT_I8:       return VarDateFromI8(V_I8(ps), &V_DATE(pd));
    case VT_UI8:      return VarDateFromUI8(V_UI8(ps), &V_DATE(pd));
    case VT_R4:       return VarDateFromR4(V_R4(ps), &V_DATE(pd));
    case VT_R8:       return VarDateFromR8(V_R8(ps), &V_DATE(pd));
    case VT_BOOL:     return VarDateFromBool(V_BOOL(ps), &V_DATE(pd));
    case VT_CY:       return VarDateFromCy(V_CY(ps), &V_DATE(pd));
    case VT_DECIMAL:  return VarDateFromDec(&V_DECIMAL(ps), &V_DATE(pd));
    case VT_DISPATCH: return VarDateFromDisp(V_DISPATCH(ps), lcid, &V_DATE(pd));
    case VT_BSTR:     return VarDateFromStr(V_BSTR(ps), lcid, dwFlags, &V_DATE(pd));
    }
    break;

  case VT_BOOL:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_BOOL(pd) = 0; return S_OK;
    case VT_I1:       return VarBoolFromI1(V_I1(ps), &V_BOOL(pd));
    case VT_I2:       return VarBoolFromI2(V_I2(ps), &V_BOOL(pd));
    case VT_I4:       return VarBoolFromI4(V_I4(ps), &V_BOOL(pd));
    case VT_UI1:      return VarBoolFromUI1(V_UI1(ps), &V_BOOL(pd));
    case VT_UI2:      return VarBoolFromUI2(V_UI2(ps), &V_BOOL(pd));
    case VT_UI4:      return VarBoolFromUI4(V_UI4(ps), &V_BOOL(pd));
    case VT_I8:       return VarBoolFromI8(V_I8(ps), &V_BOOL(pd));
    case VT_UI8:      return VarBoolFromUI8(V_UI8(ps), &V_BOOL(pd));
    case VT_R4:       return VarBoolFromR4(V_R4(ps), &V_BOOL(pd));
    case VT_R8:       return VarBoolFromR8(V_R8(ps), &V_BOOL(pd));
    case VT_DATE:     return VarBoolFromDate(V_DATE(ps), &V_BOOL(pd));
    case VT_CY:       return VarBoolFromCy(V_CY(ps), &V_BOOL(pd));
    case VT_DECIMAL:  return VarBoolFromDec(&V_DECIMAL(ps), &V_BOOL(pd));
    case VT_DISPATCH: return VarBoolFromDisp(V_DISPATCH(ps), lcid, &V_BOOL(pd));
    case VT_BSTR:     return VarBoolFromStr(V_BSTR(ps), lcid, dwFlags, &V_BOOL(pd));
    }
    break;

  case VT_BSTR:
    switch (vtFrom)
    {
    case VT_EMPTY:
      V_BSTR(pd) = SysAllocStringLen(NULL, 0);
      return V_BSTR(pd) ? S_OK : E_OUTOFMEMORY;
    case VT_BOOL:
      if (wFlags & (VARIANT_ALPHABOOL|VARIANT_LOCALBOOL))
         return VarBstrFromBool(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
      return VarBstrFromI2(V_BOOL(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_I1:       return VarBstrFromI1(V_I1(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_I2:       return VarBstrFromI2(V_I2(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_I4:       return VarBstrFromI4(V_I4(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_UI1:      return VarBstrFromUI1(V_UI1(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_UI2:      return VarBstrFromUI2(V_UI2(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_UI4:      return VarBstrFromUI4(V_UI4(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_I8:       return VarBstrFromI8(V_I8(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_UI8:      return VarBstrFromUI8(V_UI8(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_R4:       return VarBstrFromR4(V_R4(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_R8:       return VarBstrFromR8(V_R8(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_DATE:     return VarBstrFromDate(V_DATE(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_CY:       return VarBstrFromCy(V_CY(ps), lcid, dwFlags, &V_BSTR(pd));
    case VT_DECIMAL:  return VarBstrFromDec(&V_DECIMAL(ps), lcid, dwFlags, &V_BSTR(pd));
435
    case VT_DISPATCH: return VarBstrFromDisp(V_DISPATCH(ps), lcid, dwFlags, &V_BSTR(pd));
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    }
    break;

  case VT_CY:
    switch (vtFrom)
    {
    case VT_EMPTY:    V_CY(pd).int64 = 0; return S_OK;
    case VT_I1:       return VarCyFromI1(V_I1(ps), &V_CY(pd));
    case VT_I2:       return VarCyFromI2(V_I2(ps), &V_CY(pd));
    case VT_I4:       return VarCyFromI4(V_I4(ps), &V_CY(pd));
    case VT_UI1:      return VarCyFromUI1(V_UI1(ps), &V_CY(pd));
    case VT_UI2:      return VarCyFromUI2(V_UI2(ps), &V_CY(pd));
    case VT_UI4:      return VarCyFromUI4(V_UI4(ps), &V_CY(pd));
    case VT_I8:       return VarCyFromI8(V_I8(ps), &V_CY(pd));
    case VT_UI8:      return VarCyFromUI8(V_UI8(ps), &V_CY(pd));
    case VT_R4:       return VarCyFromR4(V_R4(ps), &V_CY(pd));
    case VT_R8:       return VarCyFromR8(V_R8(ps), &V_CY(pd));
    case VT_DATE:     return VarCyFromDate(V_DATE(ps), &V_CY(pd));
    case VT_BOOL:     return VarCyFromBool(V_BOOL(ps), &V_CY(pd));
    case VT_DECIMAL:  return VarCyFromDec(&V_DECIMAL(ps), &V_CY(pd));
    case VT_DISPATCH: return VarCyFromDisp(V_DISPATCH(ps), lcid, &V_CY(pd));
    case VT_BSTR:     return VarCyFromStr(V_BSTR(ps), lcid, dwFlags, &V_CY(pd));
    }
    break;

  case VT_DECIMAL:
    switch (vtFrom)
463
    {
464 465 466 467 468 469 470
    case VT_EMPTY:
    case VT_BOOL:
       DEC_SIGNSCALE(&V_DECIMAL(pd)) = SIGNSCALE(DECIMAL_POS,0);
       DEC_HI32(&V_DECIMAL(pd)) = 0;
       DEC_MID32(&V_DECIMAL(pd)) = 0;
        /* VarDecFromBool() coerces to -1/0, ChangeTypeEx() coerces to 1/0.
         * VT_NULL and VT_EMPTY always give a 0 value.
471
         */
472 473 474 475 476 477 478 479 480 481 482 483 484
       DEC_LO32(&V_DECIMAL(pd)) = vtFrom == VT_BOOL && V_BOOL(ps) ? 1 : 0;
       return S_OK;
    case VT_I1:       return VarDecFromI1(V_I1(ps), &V_DECIMAL(pd));
    case VT_I2:       return VarDecFromI2(V_I2(ps), &V_DECIMAL(pd));
    case VT_I4:       return VarDecFromI4(V_I4(ps), &V_DECIMAL(pd));
    case VT_UI1:      return VarDecFromUI1(V_UI1(ps), &V_DECIMAL(pd));
    case VT_UI2:      return VarDecFromUI2(V_UI2(ps), &V_DECIMAL(pd));
    case VT_UI4:      return VarDecFromUI4(V_UI4(ps), &V_DECIMAL(pd));
    case VT_I8:       return VarDecFromI8(V_I8(ps), &V_DECIMAL(pd));
    case VT_UI8:      return VarDecFromUI8(V_UI8(ps), &V_DECIMAL(pd));
    case VT_R4:       return VarDecFromR4(V_R4(ps), &V_DECIMAL(pd));
    case VT_R8:       return VarDecFromR8(V_R8(ps), &V_DECIMAL(pd));
    case VT_DATE:     return VarDecFromDate(V_DATE(ps), &V_DECIMAL(pd));
485 486
    case VT_CY:       return VarDecFromCy(V_CY(ps), &V_DECIMAL(pd));
    case VT_DISPATCH: return VarDecFromDisp(V_DISPATCH(ps), lcid, &V_DECIMAL(pd));
487 488 489 490 491 492 493 494 495 496 497 498 499
    case VT_BSTR:     return VarDecFromStr(V_BSTR(ps), lcid, dwFlags, &V_DECIMAL(pd));
    }
    break;

  case VT_UNKNOWN:
    switch (vtFrom)
    {
    case VT_DISPATCH:
      if (V_DISPATCH(ps) == NULL)
        V_UNKNOWN(pd) = NULL;
      else
        res = IDispatch_QueryInterface(V_DISPATCH(ps), &IID_IUnknown, (LPVOID*)&V_UNKNOWN(pd));
      break;
500
    }
501 502 503 504
    break;

  case VT_DISPATCH:
    switch (vtFrom)
505
    {
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    case VT_UNKNOWN:
      if (V_UNKNOWN(ps) == NULL)
        V_DISPATCH(pd) = NULL;
      else
        res = IUnknown_QueryInterface(V_UNKNOWN(ps), &IID_IDispatch, (LPVOID*)&V_DISPATCH(pd));
      break;
    }
    break;

  case VT_RECORD:
    break;
  }
  return res;
}

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
/* Coerce to/from an array */
static inline HRESULT VARIANT_CoerceArray(VARIANTARG* pd, VARIANTARG* ps, VARTYPE vt)
{
  if (vt == VT_BSTR && V_VT(ps) == (VT_ARRAY|VT_UI1))
    return BstrFromVector(V_ARRAY(ps), &V_BSTR(pd));

  if (V_VT(ps) == VT_BSTR && vt == (VT_ARRAY|VT_UI1))
    return VectorFromBstr(V_BSTR(ps), &V_ARRAY(ps));

  if (V_VT(ps) == vt)
    return SafeArrayCopy(V_ARRAY(ps), &V_ARRAY(pd));

  return DISP_E_TYPEMISMATCH;
}

536 537 538 539 540 541 542 543
/******************************************************************************
 * Check if a variants type is valid.
 */
static inline HRESULT VARIANT_ValidateType(VARTYPE vt)
{
  VARTYPE vtExtra = vt & VT_EXTRA_TYPE;

  vt &= VT_TYPEMASK;
544

545 546 547 548 549 550 551 552 553 554 555
  if (!(vtExtra & (VT_VECTOR|VT_RESERVED)))
  {
    if (vt < VT_VOID || vt == VT_RECORD || vt == VT_CLSID)
    {
      if ((vtExtra & (VT_BYREF|VT_ARRAY)) && vt <= VT_NULL)
        return DISP_E_BADVARTYPE;
      if (vt != (VARTYPE)15)
        return S_OK;
    }
  }
  return DISP_E_BADVARTYPE;
556 557 558
}

/******************************************************************************
559
 *		VariantInit	[OLEAUT32.8]
560
 *
561 562 563 564 565 566 567
 * Initialise a variant.
 *
 * PARAMS
 *  pVarg [O] Variant to initialise
 *
 * RETURNS
 *  Nothing.
568
 *
569 570 571
 * NOTES
 *  This function simply sets the type of the variant to VT_EMPTY. It does not
 *  free any existing value, use VariantClear() for that.
572
 */
573
void WINAPI VariantInit(VARIANTARG* pVarg)
574
{
575
  TRACE("(%p)\n", pVarg);
576

577 578
  V_VT(pVarg) = VT_EMPTY; /* Native doesn't set any other fields */
}
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
/******************************************************************************
 *		VariantClear	[OLEAUT32.9]
 *
 * Clear a variant.
 *
 * PARAMS
 *  pVarg [I/O] Variant to clear
 *
 * RETURNS
 *  Success: S_OK. Any previous value in pVarg is freed and its type is set to VT_EMPTY.
 *  Failure: DISP_E_BADVARTYPE, if the variant is a not a valid variant type.
 */
HRESULT WINAPI VariantClear(VARIANTARG* pVarg)
{
  HRESULT hres = S_OK;
595

596
  TRACE("(%p->(%s%s))\n", pVarg, debugstr_VT(pVarg), debugstr_VF(pVarg));
597

598
  hres = VARIANT_ValidateType(V_VT(pVarg));
599

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  if (SUCCEEDED(hres))
  {
    if (!V_ISBYREF(pVarg))
    {
      if (V_ISARRAY(pVarg) || V_VT(pVarg) == VT_SAFEARRAY)
      {
        if (V_ARRAY(pVarg))
          hres = SafeArrayDestroy(V_ARRAY(pVarg));
      }
      else if (V_VT(pVarg) == VT_BSTR)
      {
        if (V_BSTR(pVarg))
          SysFreeString(V_BSTR(pVarg));
      }
      else if (V_VT(pVarg) == VT_RECORD)
      {
        struct __tagBRECORD* pBr = &V_UNION(pVarg,brecVal);
        if (pBr->pRecInfo)
618
        {
619 620
          IRecordInfo_RecordClear(pBr->pRecInfo, pBr->pvRecord);
          IRecordInfo_Release(pBr->pRecInfo);
621
        }
622 623 624 625 626 627 628 629 630 631 632 633
      }
      else if (V_VT(pVarg) == VT_DISPATCH ||
               V_VT(pVarg) == VT_UNKNOWN)
      {
        if (V_UNKNOWN(pVarg))
          IUnknown_Release(V_UNKNOWN(pVarg));
      }
    }
    V_VT(pVarg) = VT_EMPTY;
  }
  return hres;
}
634

635 636 637 638 639 640
/******************************************************************************
 * Copy an IRecordInfo object contained in a variant.
 */
static HRESULT VARIANT_CopyIRecordInfo(struct __tagBRECORD* pBr)
{
  HRESULT hres = S_OK;
641

642 643 644
  if (pBr->pRecInfo)
  {
    ULONG ulSize;
645

646 647 648 649 650 651 652 653 654 655
    hres = IRecordInfo_GetSize(pBr->pRecInfo, &ulSize);
    if (SUCCEEDED(hres))
    {
      PVOID pvRecord = HeapAlloc(GetProcessHeap(), 0, ulSize);
      if (!pvRecord)
        hres = E_OUTOFMEMORY;
      else
      {
        memcpy(pvRecord, pBr->pvRecord, ulSize);
        pBr->pvRecord = pvRecord;
656

657 658 659 660 661 662 663 664 665
        hres = IRecordInfo_RecordCopy(pBr->pRecInfo, pvRecord, pvRecord);
        if (SUCCEEDED(hres))
          IRecordInfo_AddRef(pBr->pRecInfo);
      }
    }
  }
  else if (pBr->pvRecord)
    hres = E_INVALIDARG;
  return hres;
666 667
}

668
/******************************************************************************
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
 *    VariantCopy  [OLEAUT32.10]
 *
 * Copy a variant.
 *
 * PARAMS
 *  pvargDest [O] Destination for copy
 *  pvargSrc  [I] Source variant to copy
 *
 * RETURNS
 *  Success: S_OK. pvargDest contains a copy of pvargSrc.
 *  Failure: DISP_E_BADVARTYPE, if either variant has an invalid type.
 *           E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
 *           HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
 *           or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
 *
 * NOTES
 *  - If pvargSrc == pvargDest, this function does nothing, and succeeds if
 *    pvargSrc is valid. Otherwise, pvargDest is always cleared using
 *    VariantClear() before pvargSrc is copied to it. If clearing pvargDest
 *    fails, so does this function.
 *  - VT_CLSID is a valid type type for pvargSrc, but not for pvargDest.
 *  - For by-value non-intrinsic types, a deep copy is made, i.e. The whole value
 *    is copied rather than just any pointers to it.
 *  - For by-value object types the object pointer is copied and the objects
 *    reference count increased using IUnknown_AddRef().
 *  - For all by-reference types, only the referencing pointer is copied.
695
 */
696
HRESULT WINAPI VariantCopy(VARIANTARG* pvargDest, VARIANTARG* pvargSrc)
697
{
698
  HRESULT hres = S_OK;
699

700 701 702
  TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
        debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
        debugstr_VF(pvargSrc));
703

704 705 706
  if (V_TYPE(pvargSrc) == VT_CLSID || /* VT_CLSID is a special case */
      FAILED(VARIANT_ValidateType(V_VT(pvargSrc))))
    return DISP_E_BADVARTYPE;
707

708 709 710 711
  if (pvargSrc != pvargDest &&
      SUCCEEDED(hres = VariantClear(pvargDest)))
  {
    *pvargDest = *pvargSrc; /* Shallow copy the value */
712

713 714 715 716 717 718 719 720 721
    if (!V_ISBYREF(pvargSrc))
    {
      if (V_ISARRAY(pvargSrc))
      {
        if (V_ARRAY(pvargSrc))
          hres = SafeArrayCopy(V_ARRAY(pvargSrc), &V_ARRAY(pvargDest));
      }
      else if (V_VT(pvargSrc) == VT_BSTR)
      {
722 723 724 725 726 727
        V_BSTR(pvargDest) = SysAllocStringByteLen((char*)V_BSTR(pvargSrc), SysStringByteLen(V_BSTR(pvargSrc)));
        if (!V_BSTR(pvargDest))
	{
	  TRACE("!V_BSTR(pvargDest), SysAllocStringByteLen() failed to allocate %d bytes\n", SysStringByteLen(V_BSTR(pvargSrc)));
          hres = E_OUTOFMEMORY;
	}
728 729 730 731 732 733 734 735 736 737 738 739 740 741
      }
      else if (V_VT(pvargSrc) == VT_RECORD)
      {
        hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
      }
      else if (V_VT(pvargSrc) == VT_DISPATCH ||
               V_VT(pvargSrc) == VT_UNKNOWN)
      {
        if (V_UNKNOWN(pvargSrc))
          IUnknown_AddRef(V_UNKNOWN(pvargSrc));
      }
    }
  }
  return hres;
742 743
}

744 745
/* Return the byte size of a variants data */
static inline size_t VARIANT_DataSize(const VARIANT* pv)
746
{
747 748 749
  switch (V_TYPE(pv))
  {
  case VT_I1:
750
  case VT_UI1:   return sizeof(BYTE);
751
  case VT_I2:
752
  case VT_UI2:   return sizeof(SHORT);
753 754 755
  case VT_INT:
  case VT_UINT:
  case VT_I4:
756
  case VT_UI4:   return sizeof(LONG);
757
  case VT_I8:
758 759 760 761 762
  case VT_UI8:   return sizeof(LONGLONG);
  case VT_R4:    return sizeof(float);
  case VT_R8:    return sizeof(double);
  case VT_DATE:  return sizeof(DATE);
  case VT_BOOL:  return sizeof(VARIANT_BOOL);
763 764
  case VT_DISPATCH:
  case VT_UNKNOWN:
765 766 767
  case VT_BSTR:  return sizeof(void*);
  case VT_CY:    return sizeof(CY);
  case VT_ERROR: return sizeof(SCODE);
768 769 770
  }
  TRACE("Shouldn't be called for vt %s%s!\n", debugstr_VT(pv), debugstr_VF(pv));
  return 0;
771 772 773
}

/******************************************************************************
774 775
 *    VariantCopyInd  [OLEAUT32.11]
 *
776
 * Copy a variant, dereferencing it if it is by-reference.
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
 *
 * PARAMS
 *  pvargDest [O] Destination for copy
 *  pvargSrc  [I] Source variant to copy
 *
 * RETURNS
 *  Success: S_OK. pvargDest contains a copy of pvargSrc.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  Failure: DISP_E_BADVARTYPE, if either variant has an invalid by-value type.
 *           E_INVALIDARG, if pvargSrc  is an invalid by-reference type.
 *           E_OUTOFMEMORY, if memory cannot be allocated. Otherwise an
 *           HRESULT error code from SafeArrayCopy(), IRecordInfo_GetSize(),
 *           or IRecordInfo_RecordCopy(), depending on the type of pvargSrc.
 *
 * NOTES
 *  - If pvargSrc is by-value, this function behaves exactly as VariantCopy().
 *  - If pvargSrc is by-reference, the value copied to pvargDest is the pointed-to
 *    value.
 *  - if pvargSrc == pvargDest, this function dereferences in place. Otherwise,
 *    pvargDest is always cleared using VariantClear() before pvargSrc is copied
 *    to it. If clearing pvargDest fails, so does this function.
800
 */
801
HRESULT WINAPI VariantCopyInd(VARIANT* pvargDest, VARIANTARG* pvargSrc)
802
{
803 804 805
  VARIANTARG vTmp, *pSrc = pvargSrc;
  VARTYPE vt;
  HRESULT hres = S_OK;
806

807 808 809
  TRACE("(%p->(%s%s),%p->(%s%s))\n", pvargDest, debugstr_VT(pvargDest),
        debugstr_VF(pvargDest), pvargSrc, debugstr_VT(pvargSrc),
        debugstr_VF(pvargSrc));
810

811 812
  if (!V_ISBYREF(pvargSrc))
    return VariantCopy(pvargDest, pvargSrc);
813

814 815 816 817 818 819 820 821 822 823
  /* Argument checking is more lax than VariantCopy()... */
  vt = V_TYPE(pvargSrc);
  if (V_ISARRAY(pvargSrc) ||
     (vt > VT_NULL && vt != (VARTYPE)15 && vt < VT_VOID &&
     !(V_VT(pvargSrc) & (VT_VECTOR|VT_RESERVED))))
  {
    /* OK */
  }
  else
    return E_INVALIDARG; /* ...And the return value for invalid types differs too */
824

825 826 827 828 829 830 831 832 833 834 835 836 837
  if (pvargSrc == pvargDest)
  {
    /* In place copy. Use a shallow copy of pvargSrc & init pvargDest.
     * This avoids an expensive VariantCopy() call - e.g. SafeArrayCopy().
     */
    vTmp = *pvargSrc;
    pSrc = &vTmp;
    V_VT(pvargDest) = VT_EMPTY;
  }
  else
  {
    /* Copy into another variant. Free the variant in pvargDest */
    if (FAILED(hres = VariantClear(pvargDest)))
Chris Morgan's avatar
Chris Morgan committed
838 839
    {
      TRACE("VariantClear() of destination failed\n");
840
      return hres;
Chris Morgan's avatar
Chris Morgan committed
841
    }
842
  }
843

844 845 846 847 848 849 850 851
  if (V_ISARRAY(pSrc))
  {
    /* Native doesn't check that *V_ARRAYREF(pSrc) is valid */
    hres = SafeArrayCopy(*V_ARRAYREF(pSrc), &V_ARRAY(pvargDest));
  }
  else if (V_VT(pSrc) == (VT_BSTR|VT_BYREF))
  {
    /* Native doesn't check that *V_BSTRREF(pSrc) is valid */
Marcus Meissner's avatar
Marcus Meissner committed
852
    V_BSTR(pvargDest) = SysAllocStringByteLen((char*)*V_BSTRREF(pSrc), SysStringByteLen(*V_BSTRREF(pSrc)));
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
  }
  else if (V_VT(pSrc) == (VT_RECORD|VT_BYREF))
  {
    V_UNION(pvargDest,brecVal) = V_UNION(pvargSrc,brecVal);
    hres = VARIANT_CopyIRecordInfo(&V_UNION(pvargDest,brecVal));
  }
  else if (V_VT(pSrc) == (VT_DISPATCH|VT_BYREF) ||
           V_VT(pSrc) == (VT_UNKNOWN|VT_BYREF))
  {
    /* Native doesn't check that *V_UNKNOWNREF(pSrc) is valid */
    V_UNKNOWN(pvargDest) = *V_UNKNOWNREF(pSrc);
    if (*V_UNKNOWNREF(pSrc))
      IUnknown_AddRef(*V_UNKNOWNREF(pSrc));
  }
  else if (V_VT(pSrc) == (VT_VARIANT|VT_BYREF))
  {
    /* Native doesn't check that *V_VARIANTREF(pSrc) is valid */
    if (V_VT(V_VARIANTREF(pSrc)) == (VT_VARIANT|VT_BYREF))
      hres = E_INVALIDARG; /* Don't dereference more than one level */
    else
      hres = VariantCopyInd(pvargDest, V_VARIANTREF(pSrc));
874

875 876 877 878 879 880 881 882 883 884 885 886 887
    /* Use the dereferenced variants type value, not VT_VARIANT */
    goto VariantCopyInd_Return;
  }
  else if (V_VT(pSrc) == (VT_DECIMAL|VT_BYREF))
  {
    memcpy(&DEC_SCALE(&V_DECIMAL(pvargDest)), &DEC_SCALE(V_DECIMALREF(pSrc)),
           sizeof(DECIMAL) - sizeof(USHORT));
  }
  else
  {
    /* Copy the pointed to data into this variant */
    memcpy(&V_BYREF(pvargDest), V_BYREF(pSrc), VARIANT_DataSize(pSrc));
  }
888

889
  V_VT(pvargDest) = V_VT(pSrc) & ~VT_BYREF;
890

891
VariantCopyInd_Return:
892

893 894
  if (pSrc != pvargSrc)
    VariantClear(pSrc);
895

896
  TRACE("returning 0x%08x, %p->(%s%s)\n", hres, pvargDest,
897 898
        debugstr_VT(pvargDest), debugstr_VF(pvargDest));
  return hres;
899 900 901
}

/******************************************************************************
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
 *    VariantChangeType  [OLEAUT32.12]
 *
 * Change the type of a variant.
 *
 * PARAMS
 *  pvargDest [O] Destination for the converted variant
 *  pvargSrc  [O] Source variant to change the type of
 *  wFlags    [I] VARIANT_ flags from "oleauto.h"
 *  vt        [I] Variant type to change pvargSrc into
 *
 * RETURNS
 *  Success: S_OK. pvargDest contains the converted value.
 *  Failure: An HRESULT error code describing the failure.
 *
 * NOTES
 *  The LCID used for the conversion is LOCALE_USER_DEFAULT.
 *  See VariantChangeTypeEx.
919
 */
920 921
HRESULT WINAPI VariantChangeType(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
                                 USHORT wFlags, VARTYPE vt)
922
{
923
  return VariantChangeTypeEx( pvargDest, pvargSrc, LOCALE_USER_DEFAULT, wFlags, vt );
924 925 926
}

/******************************************************************************
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
 *    VariantChangeTypeEx  [OLEAUT32.147]
 *
 * Change the type of a variant.
 *
 * PARAMS
 *  pvargDest [O] Destination for the converted variant
 *  pvargSrc  [O] Source variant to change the type of
 *  lcid      [I] LCID for the conversion
 *  wFlags    [I] VARIANT_ flags from "oleauto.h"
 *  vt        [I] Variant type to change pvargSrc into
 *
 * RETURNS
 *  Success: S_OK. pvargDest contains the converted value.
 *  Failure: An HRESULT error code describing the failure.
 *
 * NOTES
 *  pvargDest and pvargSrc can point to the same variant to perform an in-place
 *  conversion. If the conversion is successful, pvargSrc will be freed.
945
 */
946 947
HRESULT WINAPI VariantChangeTypeEx(VARIANTARG* pvargDest, VARIANTARG* pvargSrc,
                                   LCID lcid, USHORT wFlags, VARTYPE vt)
948
{
949
  HRESULT res = S_OK;
950

951
  TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%04x,%s%s)\n", pvargDest,
952 953 954
        debugstr_VT(pvargDest), debugstr_VF(pvargDest), pvargSrc,
        debugstr_VT(pvargSrc), debugstr_VF(pvargSrc), lcid, wFlags,
        debugstr_vt(vt), debugstr_vf(vt));
955

956 957 958
  if (vt == VT_CLSID)
    res = DISP_E_BADVARTYPE;
  else
959
  {
960
    res = VARIANT_ValidateType(V_VT(pvargSrc));
961

962 963 964
    if (SUCCEEDED(res))
    {
      res = VARIANT_ValidateType(vt);
965

966 967
      if (SUCCEEDED(res))
      {
968
        VARIANTARG vTmp, vSrcDeref;
969

970
        if(V_ISBYREF(pvargSrc) && !V_BYREF(pvargSrc))
971 972 973 974
          res = DISP_E_TYPEMISMATCH;
        else
        {
          V_VT(&vTmp) = VT_EMPTY;
975 976 977
          V_VT(&vSrcDeref) = VT_EMPTY;
          VariantClear(&vTmp);
          VariantClear(&vSrcDeref);
978
        }
979

980 981
        if (SUCCEEDED(res))
        {
982
          res = VariantCopyInd(&vSrcDeref, pvargSrc);
983 984
          if (SUCCEEDED(res))
          {
985 986
            if (V_ISARRAY(&vSrcDeref) || (vt & VT_ARRAY))
              res = VARIANT_CoerceArray(&vTmp, &vSrcDeref, vt);
987
            else
988 989 990 991 992 993 994 995
              res = VARIANT_Coerce(&vTmp, lcid, wFlags, &vSrcDeref, vt);

            if (SUCCEEDED(res)) {
                V_VT(&vTmp) = vt;
                VariantCopy(pvargDest, &vTmp);
            }
            VariantClear(&vTmp);
            VariantClear(&vSrcDeref);
996
          }
997 998 999 1000
        }
      }
    }
  }
1001

1002
  TRACE("returning 0x%08x, %p->(%s%s)\n", res, pvargDest,
1003 1004
        debugstr_VT(pvargDest), debugstr_VF(pvargDest));
  return res;
1005
}
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
/* Date Conversions */

#define IsLeapYear(y) (((y % 4) == 0) && (((y % 100) != 0) || ((y % 400) == 0)))

/* Convert a VT_DATE value to a Julian Date */
static inline int VARIANT_JulianFromDate(int dateIn)
{
  int julianDays = dateIn;

  julianDays -= DATE_MIN; /* Convert to + days from 1 Jan 100 AD */
  julianDays += 1757585;  /* Convert to + days from 23 Nov 4713 BC (Julian) */
  return julianDays;
}

/* Convert a Julian Date to a VT_DATE value */
static inline int VARIANT_DateFromJulian(int dateIn)
{
  int julianDays = dateIn;

  julianDays -= 1757585;  /* Convert to + days from 1 Jan 100 AD */
  julianDays += DATE_MIN; /* Convert to +/- days from 1 Jan 1899 AD */
  return julianDays;
}

/* Convert a Julian date to Day/Month/Year - from PostgreSQL */
static inline void VARIANT_DMYFromJulian(int jd, USHORT *year, USHORT *month, USHORT *day)
{
  int j, i, l, n;

  l = jd + 68569;
  n = l * 4 / 146097;
  l -= (n * 146097 + 3) / 4;
  i = (4000 * (l + 1)) / 1461001;
  l += 31 - (i * 1461) / 4;
  j = (l * 80) / 2447;
  *day = l - (j * 2447) / 80;
  l = j / 11;
  *month = (j + 2) - (12 * l);
  *year = 100 * (n - 49) + i + l;
}

/* Convert Day/Month/Year to a Julian date - from PostgreSQL */
static inline double VARIANT_JulianFromDMY(USHORT year, USHORT month, USHORT day)
{
  int m12 = (month - 14) / 12;

  return ((1461 * (year + 4800 + m12)) / 4 + (367 * (month - 2 - 12 * m12)) / 12 -
           (3 * ((year + 4900 + m12) / 100)) / 4 + day - 32075);
}

/* Macros for accessing DOS format date/time fields */
#define DOS_YEAR(x)   (1980 + (x >> 9))
#define DOS_MONTH(x)  ((x >> 5) & 0xf)
#define DOS_DAY(x)    (x & 0x1f)
#define DOS_HOUR(x)   (x >> 11)
#define DOS_MINUTE(x) ((x >> 5) & 0x3f)
#define DOS_SECOND(x) ((x & 0x1f) << 1)
/* Create a DOS format date/time */
#define DOS_DATE(d,m,y) (d | (m << 5) | ((y-1980) << 9))
#define DOS_TIME(h,m,s) ((s >> 1) | (m << 5) | (h << 11))

/* Roll a date forwards or backwards to correct it */
static HRESULT VARIANT_RollUdate(UDATE *lpUd)
{
  static const BYTE days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  TRACE("Raw date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
        lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);

  /* Years < 100 are treated as 1900 + year */
  if (lpUd->st.wYear < 100)
    lpUd->st.wYear += 1900;

  if (!lpUd->st.wMonth)
  {
    /* Roll back to December of the previous year */
    lpUd->st.wMonth = 12;
    lpUd->st.wYear--;
  }
  else while (lpUd->st.wMonth > 12)
  {
    /* Roll forward the correct number of months */
    lpUd->st.wYear++;
    lpUd->st.wMonth -= 12;
  }

  if (lpUd->st.wYear > 9999 || lpUd->st.wHour > 23 ||
      lpUd->st.wMinute > 59 || lpUd->st.wSecond > 59)
    return E_INVALIDARG; /* Invalid values */

  if (!lpUd->st.wDay)
  {
    /* Roll back the date one day */
    if (lpUd->st.wMonth == 1)
    {
      /* Roll back to December 31 of the previous year */
      lpUd->st.wDay   = 31;
      lpUd->st.wMonth = 12;
      lpUd->st.wYear--;
    }
    else
    {
      lpUd->st.wMonth--; /* Previous month */
      if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
        lpUd->st.wDay = 29; /* Februaury has 29 days on leap years */
      else
        lpUd->st.wDay = days[lpUd->st.wMonth]; /* Last day of the month */
    }
  }
  else if (lpUd->st.wDay > 28)
  {
    int rollForward = 0;

    /* Possibly need to roll the date forward */
    if (lpUd->st.wMonth == 2 && IsLeapYear(lpUd->st.wYear))
      rollForward = lpUd->st.wDay - 29; /* Februaury has 29 days on leap years */
    else
      rollForward = lpUd->st.wDay - days[lpUd->st.wMonth];

    if (rollForward > 0)
    {
      lpUd->st.wDay = rollForward;
      lpUd->st.wMonth++;
      if (lpUd->st.wMonth > 12)
      {
        lpUd->st.wMonth = 1; /* Roll forward into January of the next year */
        lpUd->st.wYear++;
      }
    }
  }
  TRACE("Rolled date: %d/%d/%d %d:%d:%d\n", lpUd->st.wDay, lpUd->st.wMonth,
        lpUd->st.wYear, lpUd->st.wHour, lpUd->st.wMinute, lpUd->st.wSecond);
  return S_OK;
}

1142 1143
/**********************************************************************
 *              DosDateTimeToVariantTime [OLEAUT32.14]
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
 *
 * Convert a Dos format date and time into variant VT_DATE format.
 *
 * PARAMS
 *  wDosDate [I] Dos format date
 *  wDosTime [I] Dos format time
 *  pDateOut [O] Destination for VT_DATE format
 *
 * RETURNS
 *  Success: TRUE. pDateOut contains the converted time.
 *  Failure: FALSE, if wDosDate or wDosTime are invalid (see notes).
 *
 * NOTES
 * - Dos format dates can only hold dates from 1-Jan-1980 to 31-Dec-2099.
 * - Dos format times are accurate to only 2 second precision.
 * - The format of a Dos Date is:
 *| Bits   Values  Meaning
 *| ----   ------  -------
 *| 0-4    1-31    Day of the week. 0 rolls back one day. A value greater than
 *|                the days in the month rolls forward the extra days.
 *| 5-8    1-12    Month of the year. 0 rolls back to December of the previous
 *|                year. 13-15 are invalid.
 *| 9-15   0-119   Year based from 1980 (Max 2099). 120-127 are invalid.
 * - The format of a Dos Time is:
 *| Bits   Values  Meaning
 *| ----   ------  -------
 *| 0-4    0-29    Seconds/2. 30 and 31 are invalid.
 *| 5-10   0-59    Minutes. 60-63 are invalid.
 *| 11-15  0-23    Hours (24 hour clock). 24-32 are invalid.
1173 1174
 */
INT WINAPI DosDateTimeToVariantTime(USHORT wDosDate, USHORT wDosTime,
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
                                    double *pDateOut)
{
  UDATE ud;

  TRACE("(0x%x(%d/%d/%d),0x%x(%d:%d:%d),%p)\n",
        wDosDate, DOS_YEAR(wDosDate), DOS_MONTH(wDosDate), DOS_DAY(wDosDate),
        wDosTime, DOS_HOUR(wDosTime), DOS_MINUTE(wDosTime), DOS_SECOND(wDosTime),
        pDateOut);

  ud.st.wYear = DOS_YEAR(wDosDate);
  ud.st.wMonth = DOS_MONTH(wDosDate);
  if (ud.st.wYear > 2099 || ud.st.wMonth > 12)
    return FALSE;
  ud.st.wDay = DOS_DAY(wDosDate);
  ud.st.wHour = DOS_HOUR(wDosTime);
  ud.st.wMinute = DOS_MINUTE(wDosTime);
  ud.st.wSecond = DOS_SECOND(wDosTime);
  ud.st.wDayOfWeek = ud.st.wMilliseconds = 0;

  return !VarDateFromUdate(&ud, 0, pDateOut);
}

/**********************************************************************
 *              VariantTimeToDosDateTime [OLEAUT32.13]
 *
 * Convert a variant format date into a Dos format date and time.
 *
 *  dateIn    [I] VT_DATE time format
 *  pwDosDate [O] Destination for Dos format date
 *  pwDosTime [O] Destination for Dos format time
 *
 * RETURNS
 *  Success: TRUE. pwDosDate and pwDosTime contains the converted values.
 *  Failure: FALSE, if dateIn cannot be represented in Dos format.
 *
 * NOTES
 *   See DosDateTimeToVariantTime() for Dos format details and bugs.
 */
INT WINAPI VariantTimeToDosDateTime(double dateIn, USHORT *pwDosDate, USHORT *pwDosTime)
{
  UDATE ud;

  TRACE("(%g,%p,%p)\n", dateIn, pwDosDate, pwDosTime);

  if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
    return FALSE;

  if (ud.st.wYear < 1980 || ud.st.wYear > 2099)
    return FALSE;

  *pwDosDate = DOS_DATE(ud.st.wDay, ud.st.wMonth, ud.st.wYear);
  *pwDosTime = DOS_TIME(ud.st.wHour, ud.st.wMinute, ud.st.wSecond);

  TRACE("Returning 0x%x(%d/%d/%d), 0x%x(%d:%d:%d)\n",
        *pwDosDate, DOS_YEAR(*pwDosDate), DOS_MONTH(*pwDosDate), DOS_DAY(*pwDosDate),
        *pwDosTime, DOS_HOUR(*pwDosTime), DOS_MINUTE(*pwDosTime), DOS_SECOND(*pwDosTime));
  return TRUE;
}

/***********************************************************************
 *              SystemTimeToVariantTime [OLEAUT32.184]
 *
 * Convert a System format date and time into variant VT_DATE format.
 *
 * PARAMS
 *  lpSt     [I] System format date and time
 *  pDateOut [O] Destination for VT_DATE format date
 *
 * RETURNS
 *  Success: TRUE. *pDateOut contains the converted value.
 *  Failure: FALSE, if lpSt cannot be represented in VT_DATE format.
 */
INT WINAPI SystemTimeToVariantTime(LPSYSTEMTIME lpSt, double *pDateOut)
{
  UDATE ud;

  TRACE("(%p->%d/%d/%d %d:%d:%d,%p)\n", lpSt, lpSt->wDay, lpSt->wMonth,
        lpSt->wYear, lpSt->wHour, lpSt->wMinute, lpSt->wSecond, pDateOut);

  if (lpSt->wMonth > 12)
    return FALSE;

  memcpy(&ud.st, lpSt, sizeof(ud.st));
  return !VarDateFromUdate(&ud, 0, pDateOut);
}

/***********************************************************************
 *              VariantTimeToSystemTime [OLEAUT32.185]
 *
 * Convert a variant VT_DATE into a System format date and time.
 *
 * PARAMS
 *  datein [I] Variant VT_DATE format date
 *  lpSt   [O] Destination for System format date and time
 *
 * RETURNS
 *  Success: TRUE. *lpSt contains the converted value.
 *  Failure: FALSE, if dateIn is too large or small.
 */
INT WINAPI VariantTimeToSystemTime(double dateIn, LPSYSTEMTIME lpSt)
{
  UDATE ud;

  TRACE("(%g,%p)\n", dateIn, lpSt);

  if (FAILED(VarUdateFromDate(dateIn, 0, &ud)))
    return FALSE;

  memcpy(lpSt, &ud.st, sizeof(ud.st));
  return TRUE;
}

/***********************************************************************
1288
 *              VarDateFromUdateEx [OLEAUT32.319]
1289 1290 1291 1292 1293
 *
 * Convert an unpacked format date and time to a variant VT_DATE.
 *
 * PARAMS
 *  pUdateIn [I] Unpacked format date and time to convert
1294
 *  lcid     [I] Locale identifier for the conversion
1295 1296 1297 1298 1299 1300 1301
 *  dwFlags  [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
 *  pDateOut [O] Destination for variant VT_DATE.
 *
 * RETURNS
 *  Success: S_OK. *pDateOut contains the converted value.
 *  Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
 */
1302
HRESULT WINAPI VarDateFromUdateEx(UDATE *pUdateIn, LCID lcid, ULONG dwFlags, DATE *pDateOut)
1303 1304 1305 1306
{
  UDATE ud;
  double dateVal;

1307
  TRACE("(%p->%d/%d/%d %d:%d:%d:%d %d %d,0x%08x,0x%08x,%p)\n", pUdateIn,
1308 1309 1310
        pUdateIn->st.wMonth, pUdateIn->st.wDay, pUdateIn->st.wYear,
        pUdateIn->st.wHour, pUdateIn->st.wMinute, pUdateIn->st.wSecond,
        pUdateIn->st.wMilliseconds, pUdateIn->st.wDayOfWeek,
1311
        pUdateIn->wDayOfYear, lcid, dwFlags, pDateOut);
1312

1313 1314 1315
  if (lcid != MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT))
    FIXME("lcid possibly not handled, treating as en-us\n");
      
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
  memcpy(&ud, pUdateIn, sizeof(ud));

  if (dwFlags & VAR_VALIDDATE)
    WARN("Ignoring VAR_VALIDDATE\n");

  if (FAILED(VARIANT_RollUdate(&ud)))
    return E_INVALIDARG;

  /* Date */
  dateVal = VARIANT_DateFromJulian(VARIANT_JulianFromDMY(ud.st.wYear, ud.st.wMonth, ud.st.wDay));

  /* Time */
  dateVal += ud.st.wHour / 24.0;
  dateVal += ud.st.wMinute / 1440.0;
  dateVal += ud.st.wSecond / 86400.0;
  dateVal += ud.st.wMilliseconds / 86400000.0;

  TRACE("Returning %g\n", dateVal);
  *pDateOut = dateVal;
  return S_OK;
}

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
/***********************************************************************
 *              VarDateFromUdate [OLEAUT32.330]
 *
 * Convert an unpacked format date and time to a variant VT_DATE.
 *
 * PARAMS
 *  pUdateIn [I] Unpacked format date and time to convert
 *  dwFlags  [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
 *  pDateOut [O] Destination for variant VT_DATE.
 *
 * RETURNS
 *  Success: S_OK. *pDateOut contains the converted value.
 *  Failure: E_INVALIDARG, if pUdateIn cannot be represented in VT_DATE format.
 *
 * NOTES
 *  This function uses the United States English locale for the conversion. Use
 *  VarDateFromUdateEx() for alternate locales.
 */
HRESULT WINAPI VarDateFromUdate(UDATE *pUdateIn, ULONG dwFlags, DATE *pDateOut)
{
  LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  
  return VarDateFromUdateEx(pUdateIn, lcid, dwFlags, pDateOut);
}

1363 1364 1365 1366 1367 1368 1369 1370
/***********************************************************************
 *              VarUdateFromDate [OLEAUT32.331]
 *
 * Convert a variant VT_DATE into an unpacked format date and time.
 *
 * PARAMS
 *  datein    [I] Variant VT_DATE format date
 *  dwFlags   [I] Flags controlling the conversion (VAR_ flags from "oleauto.h")
1371
 *  lpUdate   [O] Destination for unpacked format date and time
1372 1373 1374 1375 1376 1377
 *
 * RETURNS
 *  Success: S_OK. *lpUdate contains the converted value.
 *  Failure: E_INVALIDARG, if dateIn is too large or small.
 */
HRESULT WINAPI VarUdateFromDate(DATE dateIn, ULONG dwFlags, UDATE *lpUdate)
1378
{
1379 1380 1381 1382 1383 1384 1385
  /* Cumulative totals of days per month */
  static const USHORT cumulativeDays[] =
  {
    0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  };
  double datePart, timePart;
  int julianDays;
1386

1387
  TRACE("(%g,0x%08x,%p)\n", dateIn, dwFlags, lpUdate);
1388

1389 1390
  if (dateIn <= (DATE_MIN - 1.0) || dateIn >= (DATE_MAX + 1.0))
    return E_INVALIDARG;
1391

1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
  datePart = dateIn < 0.0 ? ceil(dateIn) : floor(dateIn);
  /* Compensate for int truncation (always downwards) */
  timePart = dateIn - datePart + 0.00000000001;
  if (timePart >= 1.0)
    timePart -= 0.00000000001;

  /* Date */
  julianDays = VARIANT_JulianFromDate(dateIn);
  VARIANT_DMYFromJulian(julianDays, &lpUdate->st.wYear, &lpUdate->st.wMonth,
                        &lpUdate->st.wDay);

  datePart = (datePart + 1.5) / 7.0;
  lpUdate->st.wDayOfWeek = (datePart - floor(datePart)) * 7;
  if (lpUdate->st.wDayOfWeek == 0)
    lpUdate->st.wDayOfWeek = 5;
  else if (lpUdate->st.wDayOfWeek == 1)
    lpUdate->st.wDayOfWeek = 6;
  else
    lpUdate->st.wDayOfWeek -= 2;
1411

1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  if (lpUdate->st.wMonth > 2 && IsLeapYear(lpUdate->st.wYear))
    lpUdate->wDayOfYear = 1; /* After February, in a leap year */
  else
    lpUdate->wDayOfYear = 0;

  lpUdate->wDayOfYear += cumulativeDays[lpUdate->st.wMonth];
  lpUdate->wDayOfYear += lpUdate->st.wDay;

  /* Time */
  timePart *= 24.0;
  lpUdate->st.wHour = timePart;
  timePart -= lpUdate->st.wHour;
  timePart *= 60.0;
  lpUdate->st.wMinute = timePart;
  timePart -= lpUdate->st.wMinute;
  timePart *= 60.0;
  lpUdate->st.wSecond = timePart;
  timePart -= lpUdate->st.wSecond;
  lpUdate->st.wMilliseconds = 0;
1431
  if (timePart > 0.5)
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
  {
    /* Round the milliseconds, adjusting the time/date forward if needed */
    if (lpUdate->st.wSecond < 59)
      lpUdate->st.wSecond++;
    else
    {
      lpUdate->st.wSecond = 0;
      if (lpUdate->st.wMinute < 59)
        lpUdate->st.wMinute++;
      else
      {
        lpUdate->st.wMinute = 0;
        if (lpUdate->st.wHour < 23)
          lpUdate->st.wHour++;
        else
        {
          lpUdate->st.wHour = 0;
          /* Roll over a whole day */
          if (++lpUdate->st.wDay > 28)
            VARIANT_RollUdate(lpUdate);
        }
      }
    }
  }
  return S_OK;
1457 1458
}

1459 1460
#define GET_NUMBER_TEXT(fld,name) \
  buff[0] = 0; \
1461
  if (!GetLocaleInfoW(lcid, lctype|fld, buff, 2)) \
1462 1463 1464
    WARN("buffer too small for " #fld "\n"); \
  else \
    if (buff[0]) lpChars->name = buff[0]; \
1465
  TRACE("lcid 0x%x, " #name "=%d '%c'\n", lcid, lpChars->name, lpChars->name)
1466 1467 1468 1469 1470

/* Get the valid number characters for an lcid */
void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS *lpChars, LCID lcid, DWORD dwFlags)
{
  static const VARIANT_NUMBER_CHARS defaultChars = { '-','+','.',',','$',0,'.',',' };
1471
  LCTYPE lctype = dwFlags & LOCALE_NOUSEROVERRIDE;
1472 1473 1474 1475 1476 1477
  WCHAR buff[4];

  memcpy(lpChars, &defaultChars, sizeof(defaultChars));
  GET_NUMBER_TEXT(LOCALE_SNEGATIVESIGN, cNegativeSymbol);
  GET_NUMBER_TEXT(LOCALE_SPOSITIVESIGN, cPositiveSymbol);
  GET_NUMBER_TEXT(LOCALE_SDECIMAL, cDecimalPoint);
1478
  GET_NUMBER_TEXT(LOCALE_STHOUSAND, cDigitSeparator);
1479
  GET_NUMBER_TEXT(LOCALE_SMONDECIMALSEP, cCurrencyDecimalPoint);
1480
  GET_NUMBER_TEXT(LOCALE_SMONTHOUSANDSEP, cCurrencyDigitSeparator);
1481 1482 1483

  /* Local currency symbols are often 2 characters */
  lpChars->cCurrencyLocal2 = '\0';
1484
  switch(GetLocaleInfoW(lcid, lctype|LOCALE_SCURRENCY, buff, sizeof(buff)/sizeof(WCHAR)))
1485 1486 1487 1488 1489 1490
  {
    case 3: lpChars->cCurrencyLocal2 = buff[1]; /* Fall through */
    case 2: lpChars->cCurrencyLocal  = buff[0];
            break;
    default: WARN("buffer too small for LOCALE_SCURRENCY\n");
  }
1491
  TRACE("lcid 0x%x, cCurrencyLocal =%d,%d '%c','%c'\n", lcid, lpChars->cCurrencyLocal,
1492 1493 1494 1495 1496 1497 1498 1499 1500
        lpChars->cCurrencyLocal2, lpChars->cCurrencyLocal, lpChars->cCurrencyLocal2);
}

/* Number Parsing States */
#define B_PROCESSING_EXPONENT 0x1
#define B_NEGATIVE_EXPONENT   0x2
#define B_EXPONENT_START      0x4
#define B_INEXACT_ZEROS       0x8
#define B_LEADING_ZERO        0x10
1501 1502
#define B_PROCESSING_HEX      0x20
#define B_PROCESSING_OCT      0x40
1503 1504 1505

/**********************************************************************
 *              VarParseNumFromStr [OLEAUT32.46]
1506 1507 1508 1509 1510 1511
 *
 * Parse a string containing a number into a NUMPARSE structure.
 *
 * PARAMS
 *  lpszStr [I]   String to parse number from
 *  lcid    [I]   Locale Id for the conversion
1512
 *  dwFlags [I]   0, or LOCALE_NOUSEROVERRIDE to use system default number chars
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
 *  pNumprs [I/O] Destination for parsed number
 *  rgbDig  [O]   Destination for digits read in
 *
 * RETURNS
 *  Success: S_OK. pNumprs and rgbDig contain the parsed representation of
 *           the number.
 *  Failure: E_INVALIDARG, if any parameter is invalid.
 *           DISP_E_TYPEMISMATCH, if the string is not a number or is formatted
 *           incorrectly.
 *           DISP_E_OVERFLOW, if rgbDig is too small to hold the number.
 *
 * NOTES
 *  pNumprs must have the following fields set:
 *   cDig: Set to the size of rgbDig.
 *   dwInFlags: Set to the allowable syntax of the number using NUMPRS_ flags
 *            from "oleauto.h".
 *
 * FIXME
 *  - I am unsure if this function should parse non-arabic (e.g. Thai)
 *   numerals, so this has not been implemented.
1533
 */
1534 1535 1536 1537 1538 1539 1540 1541 1542
HRESULT WINAPI VarParseNumFromStr(OLECHAR *lpszStr, LCID lcid, ULONG dwFlags,
                                  NUMPARSE *pNumprs, BYTE *rgbDig)
{
  VARIANT_NUMBER_CHARS chars;
  BYTE rgbTmp[1024];
  DWORD dwState = B_EXPONENT_START|B_INEXACT_ZEROS;
  int iMaxDigits = sizeof(rgbTmp) / sizeof(BYTE);
  int cchUsed = 0;

1543
  TRACE("(%s,%d,0x%08x,%p,%p)\n", debugstr_w(lpszStr), lcid, dwFlags, pNumprs, rgbDig);
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

  if (!pNumprs || !rgbDig)
    return E_INVALIDARG;

  if (pNumprs->cDig < iMaxDigits)
    iMaxDigits = pNumprs->cDig;

  pNumprs->cDig = 0;
  pNumprs->dwOutFlags = 0;
  pNumprs->cchUsed = 0;
  pNumprs->nBaseShift = 0;
  pNumprs->nPwr10 = 0;

  if (!lpszStr)
    return DISP_E_TYPEMISMATCH;

  VARIANT_GetLocalisedNumberChars(&chars, lcid, dwFlags);

  /* First consume all the leading symbols and space from the string */
  while (1)
  {
    if (pNumprs->dwInFlags & NUMPRS_LEADING_WHITE && isspaceW(*lpszStr))
    {
      pNumprs->dwOutFlags |= NUMPRS_LEADING_WHITE;
      do
      {
        cchUsed++;
        lpszStr++;
      } while (isspaceW(*lpszStr));
    }
    else if (pNumprs->dwInFlags & NUMPRS_LEADING_PLUS &&
             *lpszStr == chars.cPositiveSymbol &&
             !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS))
    {
      pNumprs->dwOutFlags |= NUMPRS_LEADING_PLUS;
      cchUsed++;
      lpszStr++;
    }
    else if (pNumprs->dwInFlags & NUMPRS_LEADING_MINUS &&
             *lpszStr == chars.cNegativeSymbol &&
             !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS))
    {
      pNumprs->dwOutFlags |= (NUMPRS_LEADING_MINUS|NUMPRS_NEG);
      cchUsed++;
      lpszStr++;
    }
    else if (pNumprs->dwInFlags & NUMPRS_CURRENCY &&
             !(pNumprs->dwOutFlags & NUMPRS_CURRENCY) &&
             *lpszStr == chars.cCurrencyLocal &&
             (!chars.cCurrencyLocal2 || lpszStr[1] == chars.cCurrencyLocal2))
    {
      pNumprs->dwOutFlags |= NUMPRS_CURRENCY;
      cchUsed++;
      lpszStr++;
      /* Only accept currency characters */
      chars.cDecimalPoint = chars.cCurrencyDecimalPoint;
1600
      chars.cDigitSeparator = chars.cCurrencyDigitSeparator;
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
    }
    else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == '(' &&
             !(pNumprs->dwOutFlags & NUMPRS_PARENS))
    {
      pNumprs->dwOutFlags |= NUMPRS_PARENS;
      cchUsed++;
      lpszStr++;
    }
    else
      break;
  }

  if (!(pNumprs->dwOutFlags & NUMPRS_CURRENCY))
  {
    /* Only accept non-currency characters */
    chars.cCurrencyDecimalPoint = chars.cDecimalPoint;
1617
    chars.cCurrencyDigitSeparator = chars.cDigitSeparator;
1618 1619
  }

1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
  if ((*lpszStr == '&' && (*(lpszStr+1) == 'H' || *(lpszStr+1) == 'h')) &&
    pNumprs->dwInFlags & NUMPRS_HEX_OCT)
  {
      dwState |= B_PROCESSING_HEX;
      pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
      cchUsed=cchUsed+2;
      lpszStr=lpszStr+2;
  }
  else if ((*lpszStr == '&' && (*(lpszStr+1) == 'O' || *(lpszStr+1) == 'o')) &&
    pNumprs->dwInFlags & NUMPRS_HEX_OCT)
  {
      dwState |= B_PROCESSING_OCT;
      pNumprs->dwOutFlags |= NUMPRS_HEX_OCT;
      cchUsed=cchUsed+2;
      lpszStr=lpszStr+2;
  }

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
  /* Strip Leading zeros */
  while (*lpszStr == '0')
  {
    dwState |= B_LEADING_ZERO;
    cchUsed++;
    lpszStr++;
  }

  while (*lpszStr)
  {
    if (isdigitW(*lpszStr))
    {
      if (dwState & B_PROCESSING_EXPONENT)
      {
        int exponentSize = 0;
        if (dwState & B_EXPONENT_START)
        {
1654 1655
          if (!isdigitW(*lpszStr))
            break; /* No exponent digits - invalid */
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
          while (*lpszStr == '0')
          {
            /* Skip leading zero's in the exponent */
            cchUsed++;
            lpszStr++;
          }
        }

        while (isdigitW(*lpszStr))
        {
          exponentSize *= 10;
          exponentSize += *lpszStr - '0';
          cchUsed++;
          lpszStr++;
        }
        if (dwState & B_NEGATIVE_EXPONENT)
          exponentSize = -exponentSize;
        /* Add the exponent into the powers of 10 */
        pNumprs->nPwr10 += exponentSize;
        dwState &= ~(B_PROCESSING_EXPONENT|B_EXPONENT_START);
        lpszStr--; /* back up to allow processing of next char */
      }
      else
      {
1680 1681
        if ((pNumprs->cDig >= iMaxDigits) && !(dwState & B_PROCESSING_HEX)
          && !(dwState & B_PROCESSING_OCT))
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
        {
          pNumprs->dwOutFlags |= NUMPRS_INEXACT;

          if (*lpszStr != '0')
            dwState &= ~B_INEXACT_ZEROS; /* Inexact number with non-trailing zeros */

          /* This digit can't be represented, but count it in nPwr10 */
          if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
            pNumprs->nPwr10--;
          else
            pNumprs->nPwr10++;
        }
        else
        {
1696 1697 1698 1699
          if ((dwState & B_PROCESSING_OCT) && ((*lpszStr == '8') || (*lpszStr == '9'))) {
            return DISP_E_TYPEMISMATCH;
          }

1700 1701
          if (pNumprs->dwOutFlags & NUMPRS_DECIMAL)
            pNumprs->nPwr10--; /* Count decimal points in nPwr10 */
1702

1703
          rgbTmp[pNumprs->cDig] = *lpszStr - '0';
1704
        }
1705 1706 1707
        pNumprs->cDig++;
        cchUsed++;
      }
1708
    }
1709
    else if (*lpszStr == chars.cDigitSeparator && pNumprs->dwInFlags & NUMPRS_THOUSANDS)
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
    {
      pNumprs->dwOutFlags |= NUMPRS_THOUSANDS;
      cchUsed++;
    }
    else if (*lpszStr == chars.cDecimalPoint &&
             pNumprs->dwInFlags & NUMPRS_DECIMAL &&
             !(pNumprs->dwOutFlags & (NUMPRS_DECIMAL|NUMPRS_EXPONENT)))
    {
      pNumprs->dwOutFlags |= NUMPRS_DECIMAL;
      cchUsed++;

      /* If we have no digits so far, skip leading zeros */
      if (!pNumprs->cDig)
      {
        while (lpszStr[1] == '0')
        {
          dwState |= B_LEADING_ZERO;
          cchUsed++;
          lpszStr++;
1729
          pNumprs->nPwr10--;
1730 1731 1732
        }
      }
    }
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
    else if (((*lpszStr >= 'a' && *lpszStr <= 'f') ||
             (*lpszStr >= 'A' && *lpszStr <= 'F')) &&
             dwState & B_PROCESSING_HEX)
    {
      if (pNumprs->cDig >= iMaxDigits)
      {
        return DISP_E_OVERFLOW;
      }
      else
      {
        if (*lpszStr >= 'a')
          rgbTmp[pNumprs->cDig] = *lpszStr - 'a' + 10;
        else
          rgbTmp[pNumprs->cDig] = *lpszStr - 'A' + 10;
      }
      pNumprs->cDig++;
      cchUsed++;
    }
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
    else if ((*lpszStr == 'e' || *lpszStr == 'E') &&
             pNumprs->dwInFlags & NUMPRS_EXPONENT &&
             !(pNumprs->dwOutFlags & NUMPRS_EXPONENT))
    {
      dwState |= B_PROCESSING_EXPONENT;
      pNumprs->dwOutFlags |= NUMPRS_EXPONENT;
      cchUsed++;
    }
    else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cPositiveSymbol)
    {
      cchUsed++; /* Ignore positive exponent */
    }
    else if (dwState & B_PROCESSING_EXPONENT && *lpszStr == chars.cNegativeSymbol)
    {
      dwState |= B_NEGATIVE_EXPONENT;
      cchUsed++;
    }
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
    else
      break; /* Stop at an unrecognised character */

    lpszStr++;
  }

  if (!pNumprs->cDig && dwState & B_LEADING_ZERO)
  {
    /* Ensure a 0 on its own gets stored */
    pNumprs->cDig = 1;
    rgbTmp[0] = 0;
  }

  if (pNumprs->dwOutFlags & NUMPRS_EXPONENT && dwState & B_PROCESSING_EXPONENT)
  {
    pNumprs->cchUsed = cchUsed;
1784
    WARN("didn't completely parse exponent\n");
1785 1786 1787 1788 1789 1790 1791
    return DISP_E_TYPEMISMATCH; /* Failed to completely parse the exponent */
  }

  if (pNumprs->dwOutFlags & NUMPRS_INEXACT)
  {
    if (dwState & B_INEXACT_ZEROS)
      pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* All zeros doesn't set NUMPRS_INEXACT */
Chris Morgan's avatar
Chris Morgan committed
1792 1793 1794 1795 1796 1797 1798
  } else if(pNumprs->dwInFlags & NUMPRS_HEX_OCT)
  {
    /* copy all of the digits into the output digit buffer */
    /* this is exactly what windows does although it also returns */
    /* cDig of X and writes X+Y where Y>=0 number of digits to rgbDig */
    memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));

1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
    if (dwState & B_PROCESSING_HEX) {
      /* hex numbers have always the same format */
      pNumprs->nPwr10=0;
      pNumprs->nBaseShift=4;
    } else {
      if (dwState & B_PROCESSING_OCT) {
        /* oct numbers have always the same format */
        pNumprs->nPwr10=0;
        pNumprs->nBaseShift=3;
      } else {
        while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
        {
1811
          pNumprs->nPwr10++;
1812 1813 1814
          pNumprs->cDig--;
        }
      }
Chris Morgan's avatar
Chris Morgan committed
1815 1816
    }
  } else
1817 1818 1819 1820
  {
    /* Remove trailing zeros from the last (whole number or decimal) part */
    while (pNumprs->cDig > 1 && !rgbTmp[pNumprs->cDig - 1])
    {
1821
      pNumprs->nPwr10++;
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
      pNumprs->cDig--;
    }
  }

  if (pNumprs->cDig <= iMaxDigits)
    pNumprs->dwOutFlags &= ~NUMPRS_INEXACT; /* Ignore stripped zeros for NUMPRS_INEXACT */
  else
    pNumprs->cDig = iMaxDigits; /* Only return iMaxDigits worth of digits */

  /* Copy the digits we processed into rgbDig */
  memcpy(rgbDig, rgbTmp, pNumprs->cDig * sizeof(BYTE));

  /* Consume any trailing symbols and space */
  while (1)
  {
    if ((pNumprs->dwInFlags & NUMPRS_TRAILING_WHITE) && isspaceW(*lpszStr))
    {
      pNumprs->dwOutFlags |= NUMPRS_TRAILING_WHITE;
      do
      {
        cchUsed++;
        lpszStr++;
      } while (isspaceW(*lpszStr));
    }
    else if (pNumprs->dwInFlags & NUMPRS_TRAILING_PLUS &&
             !(pNumprs->dwOutFlags & NUMPRS_LEADING_PLUS) &&
             *lpszStr == chars.cPositiveSymbol)
    {
      pNumprs->dwOutFlags |= NUMPRS_TRAILING_PLUS;
      cchUsed++;
      lpszStr++;
    }
    else if (pNumprs->dwInFlags & NUMPRS_TRAILING_MINUS &&
             !(pNumprs->dwOutFlags & NUMPRS_LEADING_MINUS) &&
             *lpszStr == chars.cNegativeSymbol)
    {
      pNumprs->dwOutFlags |= (NUMPRS_TRAILING_MINUS|NUMPRS_NEG);
      cchUsed++;
      lpszStr++;
    }
    else if (pNumprs->dwInFlags & NUMPRS_PARENS && *lpszStr == ')' &&
             pNumprs->dwOutFlags & NUMPRS_PARENS)
    {
      cchUsed++;
      lpszStr++;
      pNumprs->dwOutFlags |= NUMPRS_NEG;
    }
    else
      break;
  }

  if (pNumprs->dwOutFlags & NUMPRS_PARENS && !(pNumprs->dwOutFlags & NUMPRS_NEG))
  {
    pNumprs->cchUsed = cchUsed;
    return DISP_E_TYPEMISMATCH; /* Opening parenthesis not matched */
  }

  if (pNumprs->dwInFlags & NUMPRS_USE_ALL && *lpszStr != '\0')
    return DISP_E_TYPEMISMATCH; /* Not all chars were consumed */

  if (!pNumprs->cDig)
    return DISP_E_TYPEMISMATCH; /* No Number found */

  pNumprs->cchUsed = cchUsed;
  return S_OK;
1887 1888
}

1889 1890 1891
/* VTBIT flags indicating an integer value */
#define INTEGER_VTBITS (VTBIT_I1|VTBIT_UI1|VTBIT_I2|VTBIT_UI2|VTBIT_I4|VTBIT_UI4|VTBIT_I8|VTBIT_UI8)
/* VTBIT flags indicating a real number value */
1892
#define REAL_VTBITS (VTBIT_R4|VTBIT_R8|VTBIT_CY)
1893

1894 1895 1896 1897 1898
/* Helper macros to check whether bit pattern fits in VARIANT (x is a ULONG64 ) */
#define FITS_AS_I1(x) ((x) >> 8 == 0)
#define FITS_AS_I2(x) ((x) >> 16 == 0)
#define FITS_AS_I4(x) ((x) >> 32 == 0)

1899 1900
/**********************************************************************
 *              VarNumFromParseNum [OLEAUT32.47]
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
 *
 * Convert a NUMPARSE structure into a numeric Variant type.
 *
 * PARAMS
 *  pNumprs  [I] Source for parsed number. cDig must be set to the size of rgbDig
 *  rgbDig   [I] Source for the numbers digits
 *  dwVtBits [I] VTBIT_ flags from "oleauto.h" indicating the acceptable dest types
 *  pVarDst  [O] Destination for the converted Variant value.
 *
 * RETURNS
 *  Success: S_OK. pVarDst contains the converted value.
 *  Failure: E_INVALIDARG, if any parameter is invalid.
 *           DISP_E_OVERFLOW, if the number is too big for the types set in dwVtBits.
 *
 * NOTES
 *  - The smallest favoured type present in dwVtBits that can represent the
 *    number in pNumprs without losing precision is used.
 *  - Signed types are preferrred over unsigned types of the same size.
 *  - Preferred types in order are: integer, float, double, currency then decimal.
 *  - Rounding (dropping of decimal points) occurs without error. See VarI8FromR8()
 *    for details of the rounding method.
 *  - pVarDst is not cleared before the result is stored in it.
1923 1924 1925 1926 1927
 *  - WinXP and Win2003 support VTBIT_I8, VTBIT_UI8 but that's buggy (by
 *    design?): If some other VTBIT's for integers are specified together
 *    with VTBIT_I8 and the number will fit only in a VT_I8 Windows will "cast"
 *    the number to the smallest requested integer truncating this way the
 *    number.  Wine dosn't implement this "feature" (yet?).
1928 1929 1930 1931
 */
HRESULT WINAPI VarNumFromParseNum(NUMPARSE *pNumprs, BYTE *rgbDig,
                                  ULONG dwVtBits, VARIANT *pVarDst)
{
1932
  /* Scale factors and limits for double arithmetic */
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
  static const double dblMultipliers[11] = {
    1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0,
    1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0
  };
  static const double dblMinimums[11] = {
    R8_MIN, R8_MIN*10.0, R8_MIN*100.0, R8_MIN*1000.0, R8_MIN*10000.0,
    R8_MIN*100000.0, R8_MIN*1000000.0, R8_MIN*10000000.0,
    R8_MIN*100000000.0, R8_MIN*1000000000.0, R8_MIN*10000000000.0
  };
  static const double dblMaximums[11] = {
    R8_MAX, R8_MAX/10.0, R8_MAX/100.0, R8_MAX/1000.0, R8_MAX/10000.0,
    R8_MAX/100000.0, R8_MAX/1000000.0, R8_MAX/10000000.0,
    R8_MAX/100000000.0, R8_MAX/1000000000.0, R8_MAX/10000000000.0
  };

  int wholeNumberDigits, fractionalDigits, divisor10 = 0, multiplier10 = 0;

1950
  TRACE("(%p,%p,0x%x,%p)\n", pNumprs, rgbDig, dwVtBits, pVarDst);
1951 1952 1953 1954

  if (pNumprs->nBaseShift)
  {
    /* nBaseShift indicates a hex or octal number */
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
    ULONG64 ul64 = 0;
    LONG64 l64;
    int i;

    /* Convert the hex or octal number string into a UI64 */
    for (i = 0; i < pNumprs->cDig; i++)
    {
      if (ul64 > ((UI8_MAX>>pNumprs->nBaseShift) - rgbDig[i]))
      {
        TRACE("Overflow multiplying digits\n");
        return DISP_E_OVERFLOW;
      }
      ul64 = (ul64<<pNumprs->nBaseShift) + rgbDig[i];
    }

    /* also make a negative representation */
    l64=-ul64;

    /* Try signed and unsigned types in size order */
1974
    if (dwVtBits & VTBIT_I1 && FITS_AS_I1(ul64))
1975 1976
    {
      V_VT(pVarDst) = VT_I1;
1977
      V_I1(pVarDst) = ul64;
1978 1979
      return S_OK;
    }
1980
    else if (dwVtBits & VTBIT_UI1 && FITS_AS_I1(ul64))
1981 1982 1983 1984 1985
    {
      V_VT(pVarDst) = VT_UI1;
      V_UI1(pVarDst) = ul64;
      return S_OK;
    }
1986
    else if (dwVtBits & VTBIT_I2 && FITS_AS_I2(ul64))
1987 1988
    {
      V_VT(pVarDst) = VT_I2;
1989
      V_I2(pVarDst) = ul64;
1990 1991
      return S_OK;
    }
1992
    else if (dwVtBits & VTBIT_UI2 && FITS_AS_I2(ul64))
1993 1994 1995 1996 1997
    {
      V_VT(pVarDst) = VT_UI2;
      V_UI2(pVarDst) = ul64;
      return S_OK;
    }
1998
    else if (dwVtBits & VTBIT_I4 && FITS_AS_I4(ul64))
1999 2000
    {
      V_VT(pVarDst) = VT_I4;
2001
      V_I4(pVarDst) = ul64;
2002 2003
      return S_OK;
    }
2004
    else if (dwVtBits & VTBIT_UI4 && FITS_AS_I4(ul64))
2005 2006 2007 2008 2009
    {
      V_VT(pVarDst) = VT_UI4;
      V_UI4(pVarDst) = ul64;
      return S_OK;
    }
2010
    else if (dwVtBits & VTBIT_I8 && ((ul64 <= I8_MAX)||(l64>=I8_MIN)))
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
    {
      V_VT(pVarDst) = VT_I8;
      V_I8(pVarDst) = ul64;
      return S_OK;
    }
    else if (dwVtBits & VTBIT_UI8)
    {
      V_VT(pVarDst) = VT_UI8;
      V_UI8(pVarDst) = ul64;
      return S_OK;
    }
    else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
    {
      V_VT(pVarDst) = VT_DECIMAL;
      DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
      DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
      DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
      return S_OK;
    }
    else if (dwVtBits & VTBIT_R4 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
    {
      V_VT(pVarDst) = VT_R4;
      if (ul64 <= I4_MAX)
          V_R4(pVarDst) = ul64;
      else
          V_R4(pVarDst) = l64;
      return S_OK;
    }
    else if (dwVtBits & VTBIT_R8 && ((ul64 <= I4_MAX)||(l64 >= I4_MIN)))
    {
      V_VT(pVarDst) = VT_R8;
      if (ul64 <= I4_MAX)
          V_R8(pVarDst) = ul64;
      else
          V_R8(pVarDst) = l64;
      return S_OK;
    }

2049
    TRACE("Overflow: possible return types: 0x%x, value: %s\n", dwVtBits, wine_dbgstr_longlong(ul64));
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
    return DISP_E_OVERFLOW;
  }

  /* Count the number of relevant fractional and whole digits stored,
   * And compute the divisor/multiplier to scale the number by.
   */
  if (pNumprs->nPwr10 < 0)
  {
    if (-pNumprs->nPwr10 >= pNumprs->cDig)
    {
      /* A real number < +/- 1.0 e.g. 0.1024 or 0.01024 */
      wholeNumberDigits = 0;
      fractionalDigits = pNumprs->cDig;
      divisor10 = -pNumprs->nPwr10;
    }
    else
    {
      /* An exactly represented real number e.g. 1.024 */
      wholeNumberDigits = pNumprs->cDig + pNumprs->nPwr10;
      fractionalDigits = pNumprs->cDig - wholeNumberDigits;
      divisor10 = pNumprs->cDig - wholeNumberDigits;
    }
  }
  else if (pNumprs->nPwr10 == 0)
  {
    /* An exactly represented whole number e.g. 1024 */
    wholeNumberDigits = pNumprs->cDig;
    fractionalDigits = 0;
  }
  else /* pNumprs->nPwr10 > 0 */
  {
    /* A whole number followed by nPwr10 0's e.g. 102400 */
    wholeNumberDigits = pNumprs->cDig;
    fractionalDigits = 0;
    multiplier10 = pNumprs->nPwr10;
  }

2087 2088 2089
  TRACE("cDig %d; nPwr10 %d, whole %d, frac %d mult %d; div %d\n",
        pNumprs->cDig, pNumprs->nPwr10, wholeNumberDigits, fractionalDigits,
        multiplier10, divisor10);
2090

2091
  if (dwVtBits & (INTEGER_VTBITS|VTBIT_DECIMAL) &&
2092 2093 2094 2095 2096
      (!fractionalDigits || !(dwVtBits & (REAL_VTBITS|VTBIT_CY|VTBIT_DECIMAL))))
  {
    /* We have one or more integer output choices, and either:
     *  1) An integer input value, or
     *  2) A real number input value but no floating output choices.
2097 2098
     * Alternately, we have a DECIMAL output available and an integer input.
     *
2099 2100 2101 2102 2103
     * So, place the integer value into pVarDst, using the smallest type
     * possible and preferring signed over unsigned types.
     */
    BOOL bOverflow = FALSE, bNegative;
    ULONG64 ul64 = 0;
2104 2105
    int i;

2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
    /* Convert the integer part of the number into a UI8 */
    for (i = 0; i < wholeNumberDigits; i++)
    {
      if (ul64 > (UI8_MAX / 10 - rgbDig[i]))
      {
        TRACE("Overflow multiplying digits\n");
        bOverflow = TRUE;
        break;
      }
      ul64 = ul64 * 10 + rgbDig[i];
    }
2117

2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
    /* Account for the scale of the number */
    if (!bOverflow && multiplier10)
    {
      for (i = 0; i < multiplier10; i++)
      {
        if (ul64 > (UI8_MAX / 10))
        {
          TRACE("Overflow scaling number\n");
          bOverflow = TRUE;
          break;
        }
        ul64 = ul64 * 10;
      }
2131 2132
    }

2133
    /* If we have any fractional digits, round the value.
2134
     * Note we don't have to do this if divisor10 is < 1,
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
     * because this means the fractional part must be < 0.5
     */
    if (!bOverflow && fractionalDigits && divisor10 > 0)
    {
      const BYTE* fracDig = rgbDig + wholeNumberDigits;
      BOOL bAdjust = FALSE;

      TRACE("first decimal value is %d\n", *fracDig);

      if (*fracDig > 5)
        bAdjust = TRUE; /* > 0.5 */
      else if (*fracDig == 5)
      {
        for (i = 1; i < fractionalDigits; i++)
        {
          if (fracDig[i])
          {
            bAdjust = TRUE; /* > 0.5 */
            break;
          }
        }
        /* If exactly 0.5, round only odd values */
        if (i == fractionalDigits && (ul64 & 1))
          bAdjust = TRUE;
      }

      if (bAdjust)
      {
        if (ul64 == UI8_MAX)
        {
          TRACE("Overflow after rounding\n");
          bOverflow = TRUE;
        }
        ul64++;
      }
2170
    }
2171 2172 2173 2174

    /* Zero is not a negative number */
    bNegative = pNumprs->dwOutFlags & NUMPRS_NEG && ul64 ? TRUE : FALSE;

2175
    TRACE("Integer value is 0x%s, bNeg %d\n", wine_dbgstr_longlong(ul64), bNegative);
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205

    /* For negative integers, try the signed types in size order */
    if (!bOverflow && bNegative)
    {
      if (dwVtBits & (VTBIT_I1|VTBIT_I2|VTBIT_I4|VTBIT_I8))
      {
        if (dwVtBits & VTBIT_I1 && ul64 <= -I1_MIN)
        {
          V_VT(pVarDst) = VT_I1;
          V_I1(pVarDst) = -ul64;
          return S_OK;
        }
        else if (dwVtBits & VTBIT_I2 && ul64 <= -I2_MIN)
        {
          V_VT(pVarDst) = VT_I2;
          V_I2(pVarDst) = -ul64;
          return S_OK;
        }
        else if (dwVtBits & VTBIT_I4 && ul64 <= -((LONGLONG)I4_MIN))
        {
          V_VT(pVarDst) = VT_I4;
          V_I4(pVarDst) = -ul64;
          return S_OK;
        }
        else if (dwVtBits & VTBIT_I8 && ul64 <= (ULONGLONG)I8_MAX + 1)
        {
          V_VT(pVarDst) = VT_I8;
          V_I8(pVarDst) = -ul64;
          return S_OK;
        }
2206 2207 2208 2209 2210 2211 2212 2213 2214
        else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
        {
          /* Decimal is only output choice left - fast path */
          V_VT(pVarDst) = VT_DECIMAL;
          DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_NEG,0);
          DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
          DEC_LO64(&V_DECIMAL(pVarDst)) = -ul64;
          return S_OK;
        }
2215
      }
2216
    }
2217 2218 2219 2220 2221 2222 2223 2224 2225
    else if (!bOverflow)
    {
      /* For positive integers, try signed then unsigned types in size order */
      if (dwVtBits & VTBIT_I1 && ul64 <= I1_MAX)
      {
        V_VT(pVarDst) = VT_I1;
        V_I1(pVarDst) = ul64;
        return S_OK;
      }
2226
      else if (dwVtBits & VTBIT_UI1 && ul64 <= UI1_MAX)
2227 2228 2229 2230 2231
      {
        V_VT(pVarDst) = VT_UI1;
        V_UI1(pVarDst) = ul64;
        return S_OK;
      }
2232
      else if (dwVtBits & VTBIT_I2 && ul64 <= I2_MAX)
2233 2234 2235 2236 2237
      {
        V_VT(pVarDst) = VT_I2;
        V_I2(pVarDst) = ul64;
        return S_OK;
      }
2238
      else if (dwVtBits & VTBIT_UI2 && ul64 <= UI2_MAX)
2239 2240 2241 2242 2243
      {
        V_VT(pVarDst) = VT_UI2;
        V_UI2(pVarDst) = ul64;
        return S_OK;
      }
2244
      else if (dwVtBits & VTBIT_I4 && ul64 <= I4_MAX)
2245 2246 2247 2248 2249
      {
        V_VT(pVarDst) = VT_I4;
        V_I4(pVarDst) = ul64;
        return S_OK;
      }
2250
      else if (dwVtBits & VTBIT_UI4 && ul64 <= UI4_MAX)
2251 2252 2253 2254 2255
      {
        V_VT(pVarDst) = VT_UI4;
        V_UI4(pVarDst) = ul64;
        return S_OK;
      }
2256
      else if (dwVtBits & VTBIT_I8 && ul64 <= I8_MAX)
2257 2258 2259 2260 2261
      {
        V_VT(pVarDst) = VT_I8;
        V_I8(pVarDst) = ul64;
        return S_OK;
      }
2262
      else if (dwVtBits & VTBIT_UI8)
2263 2264 2265 2266 2267
      {
        V_VT(pVarDst) = VT_UI8;
        V_UI8(pVarDst) = ul64;
        return S_OK;
      }
2268 2269 2270 2271 2272 2273 2274 2275 2276
      else if ((dwVtBits & REAL_VTBITS) == VTBIT_DECIMAL)
      {
        /* Decimal is only output choice left - fast path */
        V_VT(pVarDst) = VT_DECIMAL;
        DEC_SIGNSCALE(&V_DECIMAL(pVarDst)) = SIGNSCALE(DECIMAL_POS,0);
        DEC_HI32(&V_DECIMAL(pVarDst)) = 0;
        DEC_LO64(&V_DECIMAL(pVarDst)) = ul64;
        return S_OK;
      }
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
    }
  }

  if (dwVtBits & REAL_VTBITS)
  {
    /* Try to put the number into a float or real */
    BOOL bOverflow = FALSE, bNegative = pNumprs->dwOutFlags & NUMPRS_NEG;
    double whole = 0.0;
    int i;

    /* Convert the number into a double */
    for (i = 0; i < pNumprs->cDig; i++)
      whole = whole * 10.0 + rgbDig[i];

    TRACE("Whole double value is %16.16g\n", whole);

    /* Account for the scale */
    while (multiplier10 > 10)
    {
      if (whole > dblMaximums[10])
      {
        dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
        bOverflow = TRUE;
        break;
      }
      whole = whole * dblMultipliers[10];
      multiplier10 -= 10;
    }
    if (multiplier10)
    {
      if (whole > dblMaximums[multiplier10])
      {
        dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY);
        bOverflow = TRUE;
      }
      else
        whole = whole * dblMultipliers[multiplier10];
    }

    TRACE("Scaled double value is %16.16g\n", whole);

    while (divisor10 > 10)
    {
2320
      if (whole < dblMinimums[10] && whole != 0)
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
      {
        dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
        bOverflow = TRUE;
        break;
      }
      whole = whole / dblMultipliers[10];
      divisor10 -= 10;
    }
    if (divisor10)
    {
2331
      if (whole < dblMinimums[divisor10] && whole != 0)
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
      {
        dwVtBits &= ~(VTBIT_R4|VTBIT_R8|VTBIT_CY); /* Underflow */
        bOverflow = TRUE;
      }
      else
        whole = whole / dblMultipliers[divisor10];
    }
    if (!bOverflow)
      TRACE("Final double value is %16.16g\n", whole);

    if (dwVtBits & VTBIT_R4 &&
        ((whole <= R4_MAX && whole >= R4_MIN) || whole == 0.0))
    {
      TRACE("Set R4 to final value\n");
      V_VT(pVarDst) = VT_R4; /* Fits into a float */
      V_R4(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
      return S_OK;
2349
    }
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

    if (dwVtBits & VTBIT_R8)
    {
      TRACE("Set R8 to final value\n");
      V_VT(pVarDst) = VT_R8; /* Fits into a double */
      V_R8(pVarDst) = pNumprs->dwOutFlags & NUMPRS_NEG ? -whole : whole;
      return S_OK;
    }

    if (dwVtBits & VTBIT_CY)
    {
      if (SUCCEEDED(VarCyFromR8(bNegative ? -whole : whole, &V_CY(pVarDst))))
      {
        V_VT(pVarDst) = VT_CY; /* Fits into a currency */
        TRACE("Set CY to final value\n");
2365
        return S_OK;
2366 2367
      }
      TRACE("Value Overflows CY\n");
2368
    }
2369
  }
2370

2371 2372
  if (dwVtBits & VTBIT_DECIMAL)
  {
2373 2374 2375 2376
    int i;
    ULONG carry;
    ULONG64 tmp;
    DECIMAL* pDec = &V_DECIMAL(pVarDst);
2377

2378
    DECIMAL_SETZERO(*pDec);
2379
    DEC_LO32(pDec) = 0;
2380

2381 2382
    if (pNumprs->dwOutFlags & NUMPRS_NEG)
      DEC_SIGN(pDec) = DECIMAL_NEG;
2383
    else
2384
      DEC_SIGN(pDec) = DECIMAL_POS;
2385

2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
    /* Factor the significant digits */
    for (i = 0; i < pNumprs->cDig; i++)
    {
      tmp = (ULONG64)DEC_LO32(pDec) * 10 + rgbDig[i];
      carry = (ULONG)(tmp >> 32);
      DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
      tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
      carry = (ULONG)(tmp >> 32);
      DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
      tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
      DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2397

2398 2399 2400 2401 2402 2403 2404 2405
      if (tmp >> 32 & UI4_MAX)
      {
VarNumFromParseNum_DecOverflow:
        TRACE("Overflow\n");
        DEC_LO32(pDec) = DEC_MID32(pDec) = DEC_HI32(pDec) = UI4_MAX;
        return DISP_E_OVERFLOW;
      }
    }
2406

2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
    /* Account for the scale of the number */
    while (multiplier10 > 0)
    {
      tmp = (ULONG64)DEC_LO32(pDec) * 10;
      carry = (ULONG)(tmp >> 32);
      DEC_LO32(pDec) = (ULONG)(tmp & UI4_MAX);
      tmp = (ULONG64)DEC_MID32(pDec) * 10 + carry;
      carry = (ULONG)(tmp >> 32);
      DEC_MID32(pDec) = (ULONG)(tmp & UI4_MAX);
      tmp = (ULONG64)DEC_HI32(pDec) * 10 + carry;
      DEC_HI32(pDec) = (ULONG)(tmp & UI4_MAX);
2418

2419 2420 2421
      if (tmp >> 32 & UI4_MAX)
        goto VarNumFromParseNum_DecOverflow;
      multiplier10--;
2422
    }
2423 2424 2425
    DEC_SCALE(pDec) = divisor10;

    V_VT(pVarDst) = VT_DECIMAL;
2426
    return S_OK;
2427 2428
  }
  return DISP_E_OVERFLOW; /* No more output choices */
2429 2430
}

Bill Medland's avatar
Bill Medland committed
2431
/**********************************************************************
2432
 *              VarCat [OLEAUT32.318]
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
 *
 * Concatenates one variant onto another.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
Bill Medland's avatar
Bill Medland committed
2444 2445 2446
 */
HRESULT WINAPI VarCat(LPVARIANT left, LPVARIANT right, LPVARIANT out)
{
2447
    VARTYPE leftvt,rightvt,resultvt;
2448 2449 2450
    HRESULT hres;
    static const WCHAR str_true[] = {'T','r','u','e','\0'};
    static const WCHAR str_false[] = {'F','a','l','s','e','\0'};
2451
    static const WCHAR sz_empty[] = {'\0'};
2452 2453 2454
    leftvt = V_VT(left);
    rightvt = V_VT(right);

2455 2456 2457
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), out);

2458
    /* when both left and right are NULL the result is NULL */
2459
    if (leftvt == VT_NULL && rightvt == VT_NULL)
Bill Medland's avatar
Bill Medland committed
2460 2461 2462 2463
    {
        V_VT(out) = VT_NULL;
        return S_OK;
    }
2464

2465 2466
    hres = S_OK;
    resultvt = VT_EMPTY;
2467

2468 2469 2470 2471 2472
    /* There are many special case for errors and return types */
    if (leftvt == VT_VARIANT && (rightvt == VT_ERROR ||
        rightvt == VT_DATE || rightvt == VT_DECIMAL))
        hres = DISP_E_TYPEMISMATCH;
    else if ((leftvt == VT_I2 || leftvt == VT_I4 ||
2473 2474 2475 2476 2477 2478 2479 2480
        leftvt == VT_R4 || leftvt == VT_R8 ||
        leftvt == VT_CY || leftvt == VT_BOOL ||
        leftvt == VT_BSTR || leftvt == VT_I1 ||
        leftvt == VT_UI1 || leftvt == VT_UI2 ||
        leftvt == VT_UI4 || leftvt == VT_I8 ||
        leftvt == VT_UI8 || leftvt == VT_INT ||
        leftvt == VT_UINT || leftvt == VT_EMPTY ||
        leftvt == VT_NULL || leftvt == VT_DATE ||
2481
        leftvt == VT_DECIMAL || leftvt == VT_DISPATCH)
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
        &&
        (rightvt == VT_I2 || rightvt == VT_I4 ||
        rightvt == VT_R4 || rightvt == VT_R8 ||
        rightvt == VT_CY || rightvt == VT_BOOL ||
        rightvt == VT_BSTR || rightvt == VT_I1 ||
        rightvt == VT_UI1 || rightvt == VT_UI2 ||
        rightvt == VT_UI4 || rightvt == VT_I8 ||
        rightvt == VT_UI8 || rightvt == VT_INT ||
        rightvt == VT_UINT || rightvt == VT_EMPTY ||
        rightvt == VT_NULL || rightvt == VT_DATE ||
2492
        rightvt == VT_DECIMAL || rightvt == VT_DISPATCH))
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
        resultvt = VT_BSTR;
    else if (rightvt == VT_ERROR && leftvt < VT_VOID)
        hres = DISP_E_TYPEMISMATCH;
    else if (leftvt == VT_ERROR && (rightvt == VT_DATE ||
        rightvt == VT_ERROR || rightvt == VT_DECIMAL))
        hres = DISP_E_TYPEMISMATCH;
    else if (rightvt == VT_DATE || rightvt == VT_ERROR ||
        rightvt == VT_DECIMAL)
        hres = DISP_E_BADVARTYPE;
    else if (leftvt == VT_ERROR || rightvt == VT_ERROR)
        hres = DISP_E_TYPEMISMATCH;
    else if (leftvt == VT_VARIANT)
        hres = DISP_E_TYPEMISMATCH;
    else if (rightvt == VT_VARIANT && (leftvt == VT_EMPTY ||
        leftvt == VT_NULL || leftvt ==  VT_I2 ||
        leftvt == VT_I4 || leftvt == VT_R4 ||
        leftvt == VT_R8 || leftvt == VT_CY ||
        leftvt == VT_DATE || leftvt == VT_BSTR ||
        leftvt == VT_BOOL ||  leftvt == VT_DECIMAL ||
        leftvt == VT_I1 || leftvt == VT_UI1 ||
        leftvt == VT_UI2 || leftvt == VT_UI4 ||
        leftvt == VT_I8 || leftvt == VT_UI8 ||
        leftvt == VT_INT || leftvt == VT_UINT))
        hres = DISP_E_TYPEMISMATCH;
    else
        hres = DISP_E_BADVARTYPE;

    /* if resutl type is not S_OK, then no need to go further */
    if (hres != S_OK)
    {
        V_VT(out) = resultvt;
        return hres;
    }
    /* Else proceed with formatting inputs to strings */
    else
2528 2529
    {
        VARIANT bstrvar_left, bstrvar_right;
2530
        V_VT(out) = VT_BSTR;
2531

2532 2533 2534
        VariantInit(&bstrvar_left);
        VariantInit(&bstrvar_right);

2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
        /* Convert left side variant to string */
        if (leftvt != VT_BSTR)
        {
            if (leftvt == VT_BOOL)
            {
                /* Bools are handled as True/False strings instead of 0/-1 as in MSDN */
                V_VT(&bstrvar_left) = VT_BSTR;
                if (V_BOOL(left) == TRUE)
                    V_BSTR(&bstrvar_left) = SysAllocString(str_true);
                else
                    V_BSTR(&bstrvar_left) = SysAllocString(str_false);
            }
2547 2548 2549 2550 2551 2552
            /* Fill with empty string for later concat with right side */
            else if (leftvt == VT_NULL)
            {
                V_VT(&bstrvar_left) = VT_BSTR;
                V_BSTR(&bstrvar_left) = SysAllocString(sz_empty);
            }
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
            else
            {
                hres = VariantChangeTypeEx(&bstrvar_left,left,0,0,VT_BSTR);
                if (hres != S_OK) {
                    VariantClear(&bstrvar_left);
                    VariantClear(&bstrvar_right);
                    if (leftvt == VT_NULL && (rightvt == VT_EMPTY ||
                        rightvt == VT_NULL || rightvt ==  VT_I2 ||
                        rightvt == VT_I4 || rightvt == VT_R4 ||
                        rightvt == VT_R8 || rightvt == VT_CY ||
                        rightvt == VT_DATE || rightvt == VT_BSTR ||
                        rightvt == VT_BOOL ||  rightvt == VT_DECIMAL ||
                        rightvt == VT_I1 || rightvt == VT_UI1 ||
                        rightvt == VT_UI2 || rightvt == VT_UI4 ||
                        rightvt == VT_I8 || rightvt == VT_UI8 ||
                        rightvt == VT_INT || rightvt == VT_UINT))
                        return DISP_E_BADVARTYPE;
                    return hres;
                }
            }
2573 2574
        }

2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
        /* convert right side variant to string */
        if (rightvt != VT_BSTR)
        {
            if (rightvt == VT_BOOL)
            {
                /* Bools are handled as True/False strings instead of 0/-1 as in MSDN */
                V_VT(&bstrvar_right) = VT_BSTR;
                if (V_BOOL(right) == TRUE)
                    V_BSTR(&bstrvar_right) = SysAllocString(str_true);
                else
                    V_BSTR(&bstrvar_right) = SysAllocString(str_false);
            }
2587 2588 2589 2590 2591 2592
            /* Fill with empty string for later concat with right side */
            else if (rightvt == VT_NULL)
            {
                V_VT(&bstrvar_right) = VT_BSTR;
                V_BSTR(&bstrvar_right) = SysAllocString(sz_empty);
            }
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
            else
            {
                hres = VariantChangeTypeEx(&bstrvar_right,right,0,0,VT_BSTR);
                if (hres != S_OK) {
                    VariantClear(&bstrvar_left);
                    VariantClear(&bstrvar_right);
                    if (rightvt == VT_NULL && (leftvt == VT_EMPTY ||
                        leftvt == VT_NULL || leftvt ==  VT_I2 ||
                        leftvt == VT_I4 || leftvt == VT_R4 ||
                        leftvt == VT_R8 || leftvt == VT_CY ||
                        leftvt == VT_DATE || leftvt == VT_BSTR ||
                        leftvt == VT_BOOL ||  leftvt == VT_DECIMAL ||
                        leftvt == VT_I1 || leftvt == VT_UI1 ||
                        leftvt == VT_UI2 || leftvt == VT_UI4 ||
                        leftvt == VT_I8 || leftvt == VT_UI8 ||
                        leftvt == VT_INT || leftvt == VT_UINT))
                        return DISP_E_BADVARTYPE;
                    return hres;
                }
            }
2613
        }
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626

        /* Concat the resulting strings together */
        if (leftvt == VT_BSTR && rightvt == VT_BSTR)
            VarBstrCat (V_BSTR(left), V_BSTR(right), &V_BSTR(out));
        else if (leftvt != VT_BSTR && rightvt != VT_BSTR)
            VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(&bstrvar_right), &V_BSTR(out));
        else if (leftvt != VT_BSTR && rightvt == VT_BSTR)
            VarBstrCat (V_BSTR(&bstrvar_left), V_BSTR(right), &V_BSTR(out));
        else if (leftvt == VT_BSTR && rightvt != VT_BSTR)
            VarBstrCat (V_BSTR(left), V_BSTR(&bstrvar_right), &V_BSTR(out));

        VariantClear(&bstrvar_left);
        VariantClear(&bstrvar_right);
2627 2628
        return S_OK;
    }
Bill Medland's avatar
Bill Medland committed
2629
}
2630

2631

2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
/* Wrapper around VariantChangeTypeEx() which permits changing a
   variant with VT_RESERVED flag set. Needed by VarCmp. */
static HRESULT _VarChangeTypeExWrap (VARIANTARG* pvargDest,
                    VARIANTARG* pvargSrc, LCID lcid, USHORT wFlags, VARTYPE vt)
{
    HRESULT res;
    VARTYPE flags;

    flags = V_VT(pvargSrc) & ~VT_TYPEMASK;
    V_VT(pvargSrc) &= ~VT_RESERVED;
    res = VariantChangeTypeEx(pvargDest,pvargSrc,lcid,wFlags,vt);
    V_VT(pvargSrc) |= flags;

    return res;
}

2648
/**********************************************************************
2649
 *              VarCmp [OLEAUT32.176]
2650
 *
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
 * Compare two variants.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  lcid    [I] LCID (locale identifier) for the comparison
 *  flags   [I] Flags to be used in the comparision:
 *              NORM_IGNORECASE, NORM_IGNORENONSPACE, NORM_IGNORESYMBOLS,
 *              NORM_IGNOREWIDTH, NORM_IGNOREKANATYPE, NORM_IGNOREKASHIDA
 *
 * RETURNS
 *  VARCMP_LT:   left variant is less than right variant.
 *  VARCMP_EQ:   input variants are equal.
2664
 *  VARCMP_GT:   left variant is greater than right variant.
2665 2666
 *  VARCMP_NULL: either one of the input variants is NULL.
 *  Failure:     An HRESULT error code indicating the error.
2667
 *
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
 * NOTES
 *  Native VarCmp up to and including WinXP dosn't like as input variants
 *  I1, UI2, VT_UI4, UI8 and UINT. INT is accepted only as left variant.
 *
 *  If both input variants are ERROR then VARCMP_EQ will be returned, else
 *  an ERROR variant will trigger an error.
 *
 *  Both input variants can have VT_RESERVED flag set which is ignored
 *  unless one and only one of the variants is a BSTR and the other one
 *  is not an EMPTY variant. All four VT_RESERVED combinations have a
 *  different meaning:
 *   - BSTR and other: BSTR is always greater than the other variant.
 *   - BSTR|VT_RESERVED and other: a string comparision is performed.
 *   - BSTR and other|VT_RESERVED: If the BSTR is a number a numeric
 *     comparision will take place else the BSTR is always greater.
 *   - BSTR|VT_RESERVED and other|VT_RESERVED: It seems that the other
 *     variant is ignored and the return value depends only on the sign
 *     of the BSTR if it is a number else the BSTR is always greater. A
 *     positive BSTR is greater, a negative one is smaller than the other
 *     variant.
 *
 * SEE
 *  VarBstrCmp for the lcid and flags usage.
2691 2692 2693
 */
HRESULT WINAPI VarCmp(LPVARIANT left, LPVARIANT right, LCID lcid, DWORD flags)
{
2694 2695 2696 2697
    VARTYPE     lvt, rvt, vt;
    VARIANT     rv,lv;
    DWORD       xmask;
    HRESULT     rc;
2698

2699
    TRACE("(%p->(%s%s),%p->(%s%s),0x%08x,0x%08x)\n", left, debugstr_VT(left),
2700 2701
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), lcid, flags);

2702 2703 2704
    lvt = V_VT(left) & VT_TYPEMASK;
    rvt = V_VT(right) & VT_TYPEMASK;
    xmask = (1 << lvt) | (1 << rvt);
2705

2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727
    /* If we have any flag set except VT_RESERVED bail out.
       Same for the left input variant type > VT_INT and for the
       right input variant type > VT_I8. Yes, VT_INT is only supported
       as left variant. Go figure */
    if (((V_VT(left) | V_VT(right)) & ~VT_TYPEMASK & ~VT_RESERVED) ||
            lvt > VT_INT || rvt > VT_I8) {
        return DISP_E_BADVARTYPE;
    }

    /* Don't ask me why but native VarCmp cannot handle: VT_I1, VT_UI2, VT_UI4,
       VT_UINT and VT_UI8. Tested with DCOM98, Win2k, WinXP */
    if (rvt == VT_INT || xmask & (VTBIT_I1 | VTBIT_UI2 | VTBIT_UI4 | VTBIT_UI8 |
                VTBIT_DISPATCH | VTBIT_VARIANT | VTBIT_UNKNOWN | VTBIT_15))
        return DISP_E_TYPEMISMATCH;

    /* If both variants are VT_ERROR return VARCMP_EQ */
    if (xmask == VTBIT_ERROR)
        return VARCMP_EQ;
    else if (xmask & VTBIT_ERROR)
        return DISP_E_TYPEMISMATCH;

    if (xmask & VTBIT_NULL)
2728 2729
        return VARCMP_NULL;

2730 2731 2732 2733 2734
    VariantInit(&lv);
    VariantInit(&rv);

    /* Two BSTRs, ignore VT_RESERVED */
    if (xmask == VTBIT_BSTR)
2735 2736
        return VarBstrCmp(V_BSTR(left), V_BSTR(right), lcid, flags);

2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
    /* A BSTR and an other variant; we have to take care of VT_RESERVED */
    if (xmask & VTBIT_BSTR) {
        VARIANT *bstrv, *nonbv;
        VARTYPE nonbvt;
        int swap = 0;

        /* Swap the variants so the BSTR is always on the left */
        if (lvt == VT_BSTR) {
            bstrv = left;
            nonbv = right;
            nonbvt = rvt;
2748
        } else {
2749 2750 2751 2752
            swap = 1;
            bstrv = right;
            nonbv = left;
            nonbvt = lvt;
2753
        }
2754

2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790
        /* BSTR and EMPTY: ignore VT_RESERVED */
        if (nonbvt == VT_EMPTY)
            rc = (!V_BSTR(bstrv) || !*V_BSTR(bstrv)) ? VARCMP_EQ : VARCMP_GT;
        else {
            VARTYPE breserv = V_VT(bstrv) & ~VT_TYPEMASK;
            VARTYPE nreserv = V_VT(nonbv) & ~VT_TYPEMASK;

            if (!breserv && !nreserv) 
                /* No VT_RESERVED set ==> BSTR always greater */
                rc = VARCMP_GT;
            else if (breserv && !nreserv) {
                /* BSTR has VT_RESERVED set. Do a string comparision */
                rc = VariantChangeTypeEx(&rv,nonbv,lcid,0,VT_BSTR);
                if (FAILED(rc))
                    return rc;
                rc = VarBstrCmp(V_BSTR(bstrv), V_BSTR(&rv), lcid, flags);
            } else if (V_BSTR(bstrv) && *V_BSTR(bstrv)) {
            /* Non NULL nor empty BSTR */
                /* If the BSTR is not a number the BSTR is greater */
                rc = _VarChangeTypeExWrap(&lv,bstrv,lcid,0,VT_R8);
                if (FAILED(rc))
                    rc = VARCMP_GT;
                else if (breserv && nreserv)
                    /* FIXME: This is strange: with both VT_RESERVED set it
                       looks like the result depends only on the sign of
                       the BSTR number */
                    rc = (V_R8(&lv) >= 0) ? VARCMP_GT : VARCMP_LT;
                else
                    /* Numeric comparision, will be handled below.
                       VARCMP_NULL used only to break out. */
                    rc = VARCMP_NULL;
            VariantClear(&lv);
            VariantClear(&rv);
            } else
                /* Empty or NULL BSTR */
                rc = VARCMP_GT;
2791
        }
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
        /* Fixup the return code if we swapped left and right */
        if (swap) {
            if (rc == VARCMP_GT)
                rc = VARCMP_LT;
            else if (rc == VARCMP_LT)
                rc = VARCMP_GT;
        }
        if (rc != VARCMP_NULL)
            return rc;
    }

    if (xmask & VTBIT_DECIMAL)
        vt = VT_DECIMAL;
    else if (xmask & VTBIT_BSTR)
        vt = VT_R8;
    else if (xmask & VTBIT_R4)
        vt = VT_R4;
    else if (xmask & (VTBIT_R8 | VTBIT_DATE))
        vt = VT_R8;
    else if (xmask & VTBIT_CY)
        vt = VT_CY;
    else
        /* default to I8 */
        vt = VT_I8;

    /* Coerce the variants */
    rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
    if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
        /* Overflow, change to R8 */
        vt = VT_R8;
        rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
    }
    if (FAILED(rc))
        return rc;
    rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
    if (rc == DISP_E_OVERFLOW && vt != VT_R8) {
        /* Overflow, change to R8 */
        vt = VT_R8;
        rc = _VarChangeTypeExWrap(&lv,left,lcid,0,vt);
        if (FAILED(rc))
            return rc;
        rc = _VarChangeTypeExWrap(&rv,right,lcid,0,vt);
    }
    if (FAILED(rc))
        return rc;

#define _VARCMP(a,b) \
    (((a) == (b)) ? VARCMP_EQ : (((a) < (b)) ? VARCMP_LT : VARCMP_GT))

    switch (vt) {
        case VT_CY:
            return VarCyCmp(V_CY(&lv), V_CY(&rv));
        case VT_DECIMAL:
            return VarDecCmp(&V_DECIMAL(&lv), &V_DECIMAL(&rv));
        case VT_I8:
            return _VARCMP(V_I8(&lv), V_I8(&rv));
        case VT_R4:
            return _VARCMP(V_R4(&lv), V_R4(&rv));
        case VT_R8:
            return _VARCMP(V_R8(&lv), V_R8(&rv));
        default:
            /* We should never get here */
            return E_FAIL;
    }
#undef _VARCMP
2857 2858 2859
}

/**********************************************************************
2860
 *              VarAnd [OLEAUT32.142]
2861
 *
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
 * Computes the logical AND of two variants.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
2872 2873 2874
 */
HRESULT WINAPI VarAnd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
2875 2876 2877 2878 2879 2880 2881 2882
    HRESULT hres = S_OK;
    VARTYPE resvt = VT_EMPTY;
    VARTYPE leftvt,rightvt;
    VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
    VARIANT varLeft, varRight;

    VariantInit(&varLeft);
    VariantInit(&varRight);
2883

2884 2885
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
2886

2887 2888 2889 2890
    leftvt = V_VT(left)&VT_TYPEMASK;
    rightvt = V_VT(right)&VT_TYPEMASK;
    leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
    rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);
2891

2892 2893 2894
    if (leftExtraFlags != rightExtraFlags)
        return DISP_E_BADVARTYPE;
    ExtraFlags = leftExtraFlags;
2895

2896 2897 2898 2899 2900 2901 2902
    /* Native VarAnd always returns a error when using any extra
     * flags or if the variant combination is I8 and INT.
     */
    if ((leftvt == VT_I8 && rightvt == VT_INT) ||
        (leftvt == VT_INT && rightvt == VT_I8) ||
        ExtraFlags != 0)
        return DISP_E_BADVARTYPE;
2903

2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
    /* Determine return type */
    else if (leftvt == VT_I8 || rightvt == VT_I8)
        resvt = VT_I8;
    else if (leftvt == VT_I4 || rightvt == VT_I4 ||
        leftvt == VT_UINT || rightvt == VT_UINT ||
        leftvt == VT_INT || rightvt == VT_INT ||
        leftvt == VT_UINT || rightvt == VT_UINT ||
        leftvt == VT_R4 || rightvt == VT_R4 ||
        leftvt == VT_R8 || rightvt == VT_R8 ||
        leftvt == VT_CY || rightvt == VT_CY ||
        leftvt == VT_DATE || rightvt == VT_DATE ||
        leftvt == VT_I1 || rightvt == VT_I1 ||
        leftvt == VT_UI2 || rightvt == VT_UI2 ||
        leftvt == VT_UI4 || rightvt == VT_UI4 ||
        leftvt == VT_UI8 || rightvt == VT_UI8 ||
        leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
        resvt = VT_I4;
    else if (leftvt == VT_UI1 || rightvt == VT_UI1 ||
        leftvt == VT_I2 || rightvt == VT_I2 ||
        leftvt == VT_EMPTY || rightvt == VT_EMPTY)
        if ((leftvt == VT_NULL && rightvt == VT_UI1) ||
            (leftvt == VT_UI1 && rightvt == VT_NULL) ||
            (leftvt == VT_UI1 && rightvt == VT_UI1))
            resvt = VT_UI1;
        else
            resvt = VT_I2;
    else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
        (leftvt == VT_BSTR && rightvt == VT_BSTR))
        resvt = VT_BOOL;
    else if (leftvt == VT_NULL || rightvt == VT_NULL ||
        leftvt == VT_BSTR || rightvt == VT_BSTR)
        resvt = VT_NULL;
    else
        return DISP_E_BADVARTYPE;
2938

2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
    if (leftvt == VT_NULL || rightvt == VT_NULL)
    {
        /*
         * Special cases for when left variant is VT_NULL
         * (NULL & 0 = NULL, NULL & value = value)
         */
        if (leftvt == VT_NULL)
        {
            VARIANT_BOOL b;
            switch(rightvt)
            {
            case VT_I1:   if (V_I1(right)) resvt = VT_NULL; break;
            case VT_UI1:  if (V_UI1(right)) resvt = VT_NULL; break;
            case VT_I2:   if (V_I2(right)) resvt = VT_NULL; break;
            case VT_UI2:  if (V_UI2(right)) resvt = VT_NULL; break;
            case VT_I4:   if (V_I4(right)) resvt = VT_NULL; break;
            case VT_UI4:  if (V_UI4(right)) resvt = VT_NULL; break;
            case VT_I8:   if (V_I8(right)) resvt = VT_NULL; break;
            case VT_UI8:  if (V_UI8(right)) resvt = VT_NULL; break;
            case VT_INT:  if (V_INT(right)) resvt = VT_NULL; break;
            case VT_UINT: if (V_UINT(right)) resvt = VT_NULL; break;
            case VT_BOOL: if (V_BOOL(right)) resvt = VT_NULL; break;
            case VT_R4:   if (V_R4(right)) resvt = VT_NULL; break;
            case VT_R8:   if (V_R8(right)) resvt = VT_NULL; break;
            case VT_CY:
                if(V_CY(right).int64)
                    resvt = VT_NULL;
                break;
            case VT_DECIMAL:
                if (DEC_HI32(&V_DECIMAL(right)) ||
                    DEC_LO64(&V_DECIMAL(right)))
                    resvt = VT_NULL;
                break;
            case VT_BSTR:
                hres = VarBoolFromStr(V_BSTR(right),
                LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
                if (FAILED(hres))
                    return hres;
                else if (b)
                    V_VT(result) = VT_NULL;
                else
                {
                    V_VT(result) = VT_BOOL;
                    V_BOOL(result) = b;
                }
                goto VarAnd_Exit;
2985 2986
            }
        }
2987 2988
        V_VT(result) = resvt;
        goto VarAnd_Exit;
2989 2990
    }

2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
    hres = VariantCopy(&varLeft, left);
    if (FAILED(hres)) goto VarAnd_Exit;

    hres = VariantCopy(&varRight, right);
    if (FAILED(hres)) goto VarAnd_Exit;

    if (resvt == VT_I4 && V_VT(&varLeft) == VT_UI4)
        V_VT(&varLeft) = VT_I4; /* Don't overflow */
    else
    {
        double d;

        if (V_VT(&varLeft) == VT_BSTR &&
            FAILED(VarR8FromStr(V_BSTR(&varLeft),
            LOCALE_USER_DEFAULT, 0, &d)))
            hres = VariantChangeType(&varLeft,&varLeft,
            VARIANT_LOCALBOOL, VT_BOOL);
            if (SUCCEEDED(hres) && V_VT(&varLeft) != resvt)
                hres = VariantChangeType(&varLeft,&varLeft,0,resvt);
            if (FAILED(hres)) goto VarAnd_Exit;
    }

    if (resvt == VT_I4 && V_VT(&varRight) == VT_UI4)
        V_VT(&varRight) = VT_I4; /* Don't overflow */
    else
    {
        double d;

        if (V_VT(&varRight) == VT_BSTR &&
            FAILED(VarR8FromStr(V_BSTR(&varRight),
            LOCALE_USER_DEFAULT, 0, &d)))
            hres = VariantChangeType(&varRight, &varRight,
                VARIANT_LOCALBOOL, VT_BOOL);
        if (SUCCEEDED(hres) && V_VT(&varRight) != resvt)
            hres = VariantChangeType(&varRight, &varRight, 0, resvt);
        if (FAILED(hres)) goto VarAnd_Exit;
    }

    V_VT(result) = resvt;
    switch(resvt)
    {
    case VT_I8:
        V_I8(result) = V_I8(&varLeft) & V_I8(&varRight);
        break;
    case VT_I4:
        V_I4(result) = V_I4(&varLeft) & V_I4(&varRight);
        break;
    case VT_I2:
        V_I2(result) = V_I2(&varLeft) & V_I2(&varRight);
        break;
    case VT_UI1:
        V_UI1(result) = V_UI1(&varLeft) & V_UI1(&varRight);
        break;
    case VT_BOOL:
        V_BOOL(result) = V_BOOL(&varLeft) & V_BOOL(&varRight);
        break;
    default:
        FIXME("Couldn't bitwise AND variant types %d,%d\n",
            leftvt,rightvt);
    }

VarAnd_Exit:
    VariantClear(&varLeft);
    VariantClear(&varRight);

    return hres;
3057 3058
}

3059 3060 3061
/**********************************************************************
 *              VarAdd [OLEAUT32.141]
 *
3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082
 * Add two variants.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  Native VarAdd up to and including WinXP dosn't like as input variants
 *  I1, UI2, UI4, UI8, INT and UINT.
 *
 *  Native VarAdd dosn't check for NULL in/out pointers and crashes. We do the
 *  same here.
 *
 * FIXME
 *  Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
 *  case.
3083 3084 3085
 */
HRESULT WINAPI VarAdd(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
3086 3087 3088 3089
    HRESULT hres;
    VARTYPE lvt, rvt, resvt, tvt;
    VARIANT lv, rv, tv;
    double r8res;
3090

3091 3092 3093 3094 3095 3096
    /* Variant priority for coercion. Sorted from lowest to highest.
       VT_ERROR shows an invalid input variant type. */
    enum coerceprio { vt_EMPTY, vt_UI1, vt_I2, vt_I4, vt_I8, vt_BSTR,vt_R4,
                      vt_R8, vt_CY, vt_DATE, vt_DECIMAL, vt_DISPATCH, vt_NULL,
                      vt_ERROR };
    /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3097
    static const VARTYPE prio2vt[] = { VT_EMPTY, VT_UI1, VT_I2, VT_I4, VT_I8, VT_BSTR, VT_R4,
3098 3099
                          VT_R8, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH,
                          VT_NULL, VT_ERROR };
3100

3101
    /* Mapping for coercion from input variant to priority of result variant. */
3102
    static const VARTYPE coerce[] = {
3103 3104 3105 3106 3107 3108 3109 3110 3111
        /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
        vt_EMPTY, vt_NULL, vt_I2, vt_I4, vt_R4,
        /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
        vt_R8, vt_CY, vt_DATE, vt_BSTR, vt_DISPATCH,
        /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
        vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
        /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
        vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
    };
3112

3113 3114 3115
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
          result);
3116

3117 3118 3119 3120 3121
    VariantInit(&lv);
    VariantInit(&rv);
    VariantInit(&tv);
    lvt = V_VT(left)&VT_TYPEMASK;
    rvt = V_VT(right)&VT_TYPEMASK;
3122

3123 3124 3125 3126 3127 3128
    /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
       Same for any input variant type > VT_I8 */
    if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
        lvt > VT_I8 || rvt > VT_I8) {
        hres = DISP_E_BADVARTYPE;
        goto end;
3129 3130
    }

3131 3132 3133 3134 3135 3136 3137
    /* Determine the variant type to coerce to. */
    if (coerce[lvt] > coerce[rvt]) {
        resvt = prio2vt[coerce[lvt]];
        tvt = prio2vt[coerce[rvt]];
    } else {
        resvt = prio2vt[coerce[rvt]];
        tvt = prio2vt[coerce[lvt]];
3138 3139
    }

3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
    /* Special cases where the result variant type is defined by both
       input variants and not only that with the highest priority */
    if (resvt == VT_BSTR) {
        if (tvt == VT_EMPTY || tvt == VT_BSTR)
            resvt = VT_BSTR;
        else
            resvt = VT_R8;
    }
    if (resvt == VT_R4 && (tvt == VT_BSTR || tvt == VT_I8 || tvt == VT_I4))
        resvt = VT_R8;
3150

3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161
    /* For overflow detection use the biggest compatible type for the
       addition */
    switch (resvt) {
        case VT_ERROR:
            hres = DISP_E_BADVARTYPE;
            goto end;
        case VT_NULL:
            hres = S_OK;
            V_VT(result) = VT_NULL;
            goto end;
        case VT_DISPATCH:
3162
            FIXME("cannot handle variant type VT_DISPATCH\n");
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180
            hres = DISP_E_TYPEMISMATCH;
            goto end;
        case VT_EMPTY:
            resvt = VT_I2;
            /* Fall through */
        case VT_UI1:
        case VT_I2:
        case VT_I4:
        case VT_I8:
            tvt = VT_I8;
            break;
        case VT_DATE:
        case VT_R4:
            tvt = VT_R8;
            break;
        default:
            tvt = resvt;
    }
3181

3182 3183 3184 3185 3186 3187 3188
    /* Now coerce the variants */
    hres = VariantChangeType(&lv, left, 0, tvt);
    if (FAILED(hres))
        goto end;
    hres = VariantChangeType(&rv, right, 0, tvt);
    if (FAILED(hres))
        goto end;
3189

3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
    /* Do the math */
    hres = S_OK;
    V_VT(result) = resvt;
    switch (tvt) {
        case VT_DECIMAL:
            hres = VarDecAdd(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
                             &V_DECIMAL(result));
            goto end;
        case VT_CY:
            hres = VarCyAdd(V_CY(&lv), V_CY(&rv), &V_CY(result));
            goto end;
        case VT_BSTR:
            /* We do not add those, we concatenate them. */
            hres = VarBstrCat(V_BSTR(&lv), V_BSTR(&rv), &V_BSTR(result));
            goto end;
        case VT_I8:
            /* Overflow detection */
            r8res = (double)V_I8(&lv) + (double)V_I8(&rv);
            if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
                V_VT(result) = VT_R8;
                V_R8(result) = r8res;
                goto end;
3212 3213
            } else {
                V_VT(&tv) = tvt;
3214
                V_I8(&tv) = V_I8(&lv) + V_I8(&rv);
3215
            }
3216 3217
            break;
        case VT_R8:
3218
            V_VT(&tv) = tvt;
3219 3220 3221 3222 3223 3224 3225 3226 3227
            /* FIXME: overflow detection */
            V_R8(&tv) = V_R8(&lv) + V_R8(&rv);
            break;
        default:
            ERR("We shouldn't get here! tvt = %d!\n", tvt);
            break;
    }
    if (resvt != tvt) {
        if ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3228 3229 3230 3231 3232 3233
            /* Overflow! Change to the vartype with the next higher priority.
               With one exception: I4 ==> R8 even if it would fit in I8 */
            if (resvt == VT_I4)
                resvt = VT_R8;
            else
                resvt = prio2vt[coerce[resvt] + 1];
3234
            hres = VariantChangeType(result, &tv, 0, resvt);
3235
        }
3236 3237
    } else
        hres = VariantCopy(result, &tv);
3238

3239 3240 3241 3242
end:
    if (hres != S_OK) {
        V_VT(result) = VT_EMPTY;
        V_I4(result) = 0;       /* No V_EMPTY */
Andreas Mohr's avatar
Andreas Mohr committed
3243
    }
3244 3245 3246
    VariantClear(&lv);
    VariantClear(&rv);
    VariantClear(&tv);
3247
    TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3248
    return hres;
Andreas Mohr's avatar
Andreas Mohr committed
3249 3250
}

3251 3252 3253
/**********************************************************************
 *              VarMul [OLEAUT32.156]
 *
3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
 * Multiply two variants.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  Native VarMul up to and including WinXP dosn't like as input variants
 *  I1, UI2, UI4, UI8, INT and UINT. But it can multiply apples with oranges.
 *
 *  Native VarMul dosn't check for NULL in/out pointers and crashes. We do the
 *  same here.
 *
 * FIXME
 *  Overflow checking for R8 (double) overflow. Return DISP_E_OVERFLOW in that
 *  case.
3275 3276 3277
 */
HRESULT WINAPI VarMul(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
    HRESULT hres;
    VARTYPE lvt, rvt, resvt, tvt;
    VARIANT lv, rv, tv;
    double r8res;

    /* Variant priority for coercion. Sorted from lowest to highest.
       VT_ERROR shows an invalid input variant type. */
    enum coerceprio { vt_UI1 = 0, vt_I2, vt_I4, vt_I8, vt_CY, vt_R4, vt_R8,
                      vt_DECIMAL, vt_NULL, vt_ERROR };
    /* Mapping from priority to variant type. Keep in sync with coerceprio! */
3288
    static const VARTYPE prio2vt[] = { VT_UI1, VT_I2, VT_I4, VT_I8, VT_CY, VT_R4, VT_R8,
3289 3290 3291
                          VT_DECIMAL, VT_NULL, VT_ERROR };

    /* Mapping for coercion from input variant to priority of result variant. */
3292
    static const VARTYPE coerce[] = {
3293 3294 3295 3296 3297 3298 3299 3300 3301
        /* VT_EMPTY, VT_NULL, VT_I2, VT_I4, VT_R4 */
        vt_UI1, vt_NULL, vt_I2, vt_I4, vt_R4,
        /* VT_R8, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH */
        vt_R8, vt_CY, vt_R8, vt_R8, vt_ERROR,
        /* VT_ERROR, VT_BOOL, VT_VARIANT, VT_UNKNOWN, VT_DECIMAL */
        vt_ERROR, vt_I2, vt_ERROR, vt_ERROR, vt_DECIMAL,
        /* 15, VT_I1, VT_UI1, VT_UI2, VT_UI4 VT_I8 */
        vt_ERROR, vt_ERROR, vt_UI1, vt_ERROR, vt_ERROR, vt_I8
    };
3302

3303
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
3304 3305
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right),
          result);
3306

3307 3308 3309
    VariantInit(&lv);
    VariantInit(&rv);
    VariantInit(&tv);
3310 3311
    lvt = V_VT(left)&VT_TYPEMASK;
    rvt = V_VT(right)&VT_TYPEMASK;
3312 3313 3314 3315 3316 3317 3318

    /* If we have any flag set (VT_ARRAY, VT_VECTOR, etc.) bail out.
       Same for any input variant type > VT_I8 */
    if (V_VT(left) & ~VT_TYPEMASK || V_VT(right) & ~VT_TYPEMASK ||
        lvt > VT_I8 || rvt > VT_I8) {
        hres = DISP_E_BADVARTYPE;
        goto end;
3319
    }
3320 3321 3322 3323 3324 3325 3326 3327

    /* Determine the variant type to coerce to. */
    if (coerce[lvt] > coerce[rvt]) {
        resvt = prio2vt[coerce[lvt]];
        tvt = prio2vt[coerce[rvt]];
    } else {
        resvt = prio2vt[coerce[rvt]];
        tvt = prio2vt[coerce[lvt]];
3328
    }
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338

    /* Special cases where the result variant type is defined by both
       input variants and not only that with the highest priority */
    if (resvt == VT_R4 && (tvt == VT_CY || tvt == VT_I8 || tvt == VT_I4))
        resvt = VT_R8;
    if (lvt == VT_EMPTY && rvt == VT_EMPTY)
        resvt = VT_I2;

    /* For overflow detection use the biggest compatible type for the
       multiplication */
3339
    switch (resvt) {
3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
        case VT_ERROR:
            hres = DISP_E_BADVARTYPE;
            goto end;
        case VT_NULL:
            hres = S_OK;
            V_VT(result) = VT_NULL;
            goto end;
        case VT_UI1:
        case VT_I2:
        case VT_I4:
        case VT_I8:
            tvt = VT_I8;
            break;
        case VT_R4:
            tvt = VT_R8;
            break;
        default:
            tvt = resvt;
    }

    /* Now coerce the variants */
    hres = VariantChangeType(&lv, left, 0, tvt);
    if (FAILED(hres))
        goto end;
    hres = VariantChangeType(&rv, right, 0, tvt);
    if (FAILED(hres))
        goto end;

    /* Do the math */
    hres = S_OK;
    V_VT(&tv) = tvt;
    V_VT(result) = resvt;
    switch (tvt) {
        case VT_DECIMAL:
            hres = VarDecMul(&V_DECIMAL(&lv), &V_DECIMAL(&rv),
                             &V_DECIMAL(result));
            goto end;
        case VT_CY:
            hres = VarCyMul(V_CY(&lv), V_CY(&rv), &V_CY(result));
            goto end;
        case VT_I8:
            /* Overflow detection */
            r8res = (double)V_I8(&lv) * (double)V_I8(&rv);
            if (r8res > (double)I8_MAX || r8res < (double)I8_MIN) {
                V_VT(result) = VT_R8;
                V_R8(result) = r8res;
                goto end;
            } else
                V_I8(&tv) = V_I8(&lv) * V_I8(&rv);
            break;
        case VT_R8:
            /* FIXME: overflow detection */
            V_R8(&tv) = V_R8(&lv) * V_R8(&rv);
            break;
        default:
            ERR("We shouldn't get here! tvt = %d!\n", tvt);
            break;
    }
3398
    if (resvt != tvt) {
3399
        while ((hres = VariantChangeType(result, &tv, 0, resvt)) != S_OK) {
3400 3401 3402 3403 3404 3405
            /* Overflow! Change to the vartype with the next higher priority.
               With one exception: I4 ==> R8 even if it would fit in I8 */
            if (resvt == VT_I4)
                resvt = VT_R8;
            else
                resvt = prio2vt[coerce[resvt] + 1];
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417
        }
    } else
        hres = VariantCopy(result, &tv);

end:
    if (hres != S_OK) {
        V_VT(result) = VT_EMPTY;
        V_I4(result) = 0;       /* No V_EMPTY */
    }
    VariantClear(&lv);
    VariantClear(&rv);
    VariantClear(&tv);
3418
    TRACE("returning 0x%8x (variant type %s)\n", hres, debugstr_VT(result));
3419
    return hres;
3420 3421 3422 3423 3424
}

/**********************************************************************
 *              VarDiv [OLEAUT32.143]
 *
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
 * Divides one variant with another.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
3435 3436 3437
 */
HRESULT WINAPI VarDiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
3438 3439 3440 3441
    HRESULT hres = S_OK;
    VARTYPE resvt = VT_EMPTY;
    VARTYPE leftvt,rightvt;
    VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3442
    VARIANT lv,rv;
3443 3444 3445 3446 3447 3448 3449 3450 3451

    leftvt = V_VT(left)&VT_TYPEMASK;
    rightvt = V_VT(right)&VT_TYPEMASK;
    leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
    rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);

    if (leftExtraFlags != rightExtraFlags)
        return DISP_E_BADVARTYPE;
    ExtraFlags = leftExtraFlags;
3452

3453 3454
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3455

3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491
    /* Native VarPow always returns a error when using any extra flags */
    if (ExtraFlags != 0)
        return DISP_E_BADVARTYPE;

    /* Determine return type */
    if (!(rightvt == VT_EMPTY))
    {
        if (leftvt == VT_NULL || rightvt == VT_NULL)
        {
            V_VT(result) = VT_NULL;
            return S_OK;
        }
        else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
            resvt = VT_DECIMAL;
        else if (leftvt == VT_I8 || rightvt == VT_I8 ||
            leftvt == VT_CY || rightvt == VT_CY ||
            leftvt == VT_DATE || rightvt == VT_DATE ||
            leftvt == VT_I4 || rightvt == VT_I4 ||
            leftvt == VT_BSTR || rightvt == VT_BSTR ||
            leftvt == VT_I2 || rightvt == VT_I2 ||
            leftvt == VT_BOOL || rightvt == VT_BOOL ||
            leftvt == VT_R8 || rightvt == VT_R8 ||
            leftvt == VT_UI1 || rightvt == VT_UI1)
        {
            if ((leftvt == VT_UI1 && rightvt == VT_R4) ||
                (leftvt == VT_R4 && rightvt == VT_UI1))
                resvt = VT_R4;
            else if ((leftvt == VT_R4 && (rightvt == VT_BOOL ||
                rightvt == VT_I2)) || (rightvt == VT_R4 &&
                (leftvt == VT_BOOL || leftvt == VT_I2)))
                resvt = VT_R4;
            else
                resvt = VT_R8;
        }
        else if (leftvt == VT_R4 || rightvt == VT_R4)
            resvt = VT_R4;
3492
    }
3493 3494 3495 3496
    else if (leftvt == VT_NULL && rightvt == VT_EMPTY)
    {
        V_VT(result) = VT_NULL;
        return S_OK;
3497
    }
3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510
    else
        return DISP_E_BADVARTYPE;

    VariantInit(&lv);
    VariantInit(&rv);

    /* coerce to the result type */
    hres = VariantChangeType(&lv, left, 0, resvt);
    if (hres != S_OK)
    {
        VariantClear(&lv);
        VariantClear(&rv);
        return hres;
3511
    }
3512 3513 3514 3515 3516 3517 3518

    hres = VariantChangeType(&rv, right, 0, resvt);
    if (hres != S_OK)
    {
        VariantClear(&lv);
        VariantClear(&rv);
        return hres;
3519
    }
3520 3521 3522 3523 3524 3525 3526 3527 3528 3529

    /* do the math */
    V_VT(result) = resvt;
    switch (resvt)
    {
    case VT_R4:
    if (V_R4(&lv) == 0.0 && V_R4(&rv) == 0.0)
    {
        hres = DISP_E_OVERFLOW;
        V_VT(result) = VT_EMPTY;
3530
    }
3531 3532 3533 3534
    else if (V_R4(&rv) == 0.0)
    {
        hres = DISP_E_DIVBYZERO;
        V_VT(result) = VT_EMPTY;
3535
    }
3536 3537 3538
    else
        V_R4(result) = V_R4(&lv) / V_R4(&rv);
    break;
3539
    case VT_R8:
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
    if (V_R8(&lv) == 0.0 && V_R8(&rv) == 0.0)
    {
        hres = DISP_E_OVERFLOW;
        V_VT(result) = VT_EMPTY;
    }
    else if (V_R8(&rv) == 0.0)
    {
        hres = DISP_E_DIVBYZERO;
        V_VT(result) = VT_EMPTY;
    }
    else
        V_R8(result) = V_R8(&lv) / V_R8(&rv);
    break;
3553
    case VT_DECIMAL:
3554 3555 3556 3557 3558 3559 3560 3561
    hres = VarDecDiv(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
    break;
    }

    VariantClear(&lv);
    VariantClear(&rv);

    return hres;
3562 3563 3564 3565 3566
}

/**********************************************************************
 *              VarSub [OLEAUT32.159]
 *
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
 * Subtract two variants.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
3577 3578 3579
 */
HRESULT WINAPI VarSub(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
3580 3581 3582 3583
    HRESULT hres = S_OK;
    VARTYPE resvt = VT_EMPTY;
    VARTYPE leftvt,rightvt;
    VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
3584 3585
    VARIANT lv,rv;

3586 3587
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);
3588

3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
    leftvt = V_VT(left)&VT_TYPEMASK;
    rightvt = V_VT(right)&VT_TYPEMASK;
    leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
    rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);

    if (leftExtraFlags != rightExtraFlags)
        return DISP_E_BADVARTYPE;
    ExtraFlags = leftExtraFlags;

    /* determine return type and return code */
    /* All extra flags produce errors */
    if (ExtraFlags == (VT_VECTOR|VT_BYREF|VT_RESERVED) ||
        ExtraFlags == (VT_VECTOR|VT_RESERVED) ||
        ExtraFlags == (VT_VECTOR|VT_BYREF) ||
        ExtraFlags == (VT_BYREF|VT_RESERVED) ||
        ExtraFlags == VT_VECTOR ||
        ExtraFlags == VT_BYREF ||
        ExtraFlags == VT_RESERVED)
        return DISP_E_BADVARTYPE;
    else if (ExtraFlags >= VT_ARRAY)
        return DISP_E_TYPEMISMATCH;
    /* Native VarSub cannot handle: VT_I1, VT_UI2, VT_UI4,
       VT_INT, VT_UINT and VT_UI8. Tested with WinXP */
    else if (leftvt == VT_CLSID || rightvt == VT_CLSID ||
        leftvt == VT_VARIANT || rightvt == VT_VARIANT ||
        leftvt == VT_I1 || rightvt == VT_I1 ||
        leftvt == VT_UI2 || rightvt == VT_UI2 ||
        leftvt == VT_UI4 || rightvt == VT_UI4 ||
        leftvt == VT_UI8 || rightvt == VT_UI8 ||
        leftvt == VT_INT || rightvt == VT_INT ||
        leftvt == VT_UINT || rightvt == VT_UINT ||
        leftvt == VT_UNKNOWN || rightvt == VT_UNKNOWN ||
        leftvt == VT_RECORD || rightvt == VT_RECORD)
    {
        if (leftvt == VT_RECORD && rightvt == VT_I8)
            return DISP_E_TYPEMISMATCH;
        else if (leftvt < VT_UI1 && rightvt == VT_RECORD)
            return DISP_E_TYPEMISMATCH;
        else if (leftvt >= VT_UI1 && rightvt == VT_RECORD)
            return DISP_E_TYPEMISMATCH;
        else if (leftvt == VT_RECORD && rightvt <= VT_UI1)
            return DISP_E_TYPEMISMATCH;
        else if (leftvt == VT_RECORD && rightvt > VT_UI1)
            return DISP_E_BADVARTYPE;
        else
            return DISP_E_BADVARTYPE;
3635
    }
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702
    /*  The following flags/types are invalid for left variant */
    else if (!((leftvt <= VT_LPWSTR || leftvt == VT_RECORD ||
        leftvt == VT_CLSID) && leftvt != (VARTYPE)15 /* undefined vt */ &&
        (leftvt < VT_VOID || leftvt > VT_LPWSTR)))
        return DISP_E_BADVARTYPE;
    /*  The following flags/types are invalid for right variant */
    else if (!((rightvt <= VT_LPWSTR || rightvt == VT_RECORD ||
        rightvt == VT_CLSID) && rightvt != (VARTYPE)15 /* undefined vt */ &&
        (rightvt < VT_VOID || rightvt > VT_LPWSTR)))
        return DISP_E_BADVARTYPE;
    else if ((leftvt == VT_NULL && rightvt == VT_DISPATCH) ||
        (leftvt == VT_DISPATCH && rightvt == VT_NULL))
        resvt = VT_NULL;
    else if (leftvt == VT_DISPATCH || rightvt == VT_DISPATCH ||
        leftvt == VT_ERROR || rightvt == VT_ERROR)
        return DISP_E_TYPEMISMATCH;
    else if (leftvt == VT_NULL || rightvt == VT_NULL)
        resvt = VT_NULL;
    else if ((leftvt == VT_EMPTY && rightvt == VT_BSTR) ||
        (leftvt == VT_DATE && rightvt == VT_DATE) ||
        (leftvt == VT_BSTR && rightvt == VT_EMPTY) ||
        (leftvt == VT_BSTR && rightvt == VT_BSTR))
        resvt = VT_R8;
    else if (leftvt == VT_DECIMAL || rightvt == VT_DECIMAL)
        resvt = VT_DECIMAL;
    else if (leftvt == VT_DATE || rightvt == VT_DATE)
        resvt = VT_DATE;
    else if (leftvt == VT_CY || rightvt == VT_CY)
        resvt = VT_CY;
    else if (leftvt == VT_R8 || rightvt == VT_R8)
        resvt = VT_R8;
    else if (leftvt == VT_BSTR || rightvt == VT_BSTR)
        resvt = VT_R8;
    else if (leftvt == VT_R4 || rightvt == VT_R4)
    {
        if (leftvt == VT_I4 || rightvt == VT_I4 ||
            leftvt == VT_I8 || rightvt == VT_I8)
            resvt = VT_R8;
        else
            resvt = VT_R4;
    }
    else if (leftvt == VT_I8 || rightvt == VT_I8)
        resvt = VT_I8;
    else if (leftvt == VT_I4 || rightvt == VT_I4)
        resvt = VT_I4;
    else if (leftvt == VT_I2 || rightvt == VT_I2 ||
        leftvt == VT_BOOL || rightvt == VT_BOOL ||
        (leftvt == VT_EMPTY && rightvt == VT_EMPTY))
        resvt = VT_I2;
    else if (leftvt == VT_UI1 || rightvt == VT_UI1)
        resvt = VT_UI1;
    else
        return DISP_E_TYPEMISMATCH;

    VariantInit(&lv);
    VariantInit(&rv);

    /* coerce to the result type */
    if (leftvt == VT_BSTR && rightvt == VT_DATE)
        hres = VariantChangeType(&lv, left, 0, VT_R8);
    else
        hres = VariantChangeType(&lv, left, 0, resvt);
    if (hres != S_OK)
    {
        VariantClear(&lv);
        VariantClear(&rv);
        return hres;
3703
    }
3704 3705 3706 3707 3708 3709 3710 3711 3712
    if (leftvt == VT_DATE && rightvt == VT_BSTR)
        hres = VariantChangeType(&rv, right, 0, VT_R8);
    else
        hres = VariantChangeType(&rv, right, 0, resvt);
    if (hres != S_OK)
    {
        VariantClear(&lv);
        VariantClear(&rv);
        return hres;
3713
    }
3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744

    /* do the math */
    V_VT(result) = resvt;
    switch (resvt)
    {
    case VT_NULL:
    break;
    case VT_DATE:
    V_DATE(result) = V_DATE(&lv) - V_DATE(&rv);
    break;
    case VT_CY:
    hres = VarCySub(V_CY(&lv), V_CY(&rv), &(V_CY(result)));
    break;
    case VT_R4:
    V_R4(result) = V_R4(&lv) - V_R4(&rv);
    break;
    case VT_I8:
    V_I8(result) = V_I8(&lv) - V_I8(&rv);
    break;
    case VT_I4:
    V_I4(result) = V_I4(&lv) - V_I4(&rv);
    break;
    case VT_I2:
    V_I2(result) = V_I2(&lv) - V_I2(&rv);
    break;
    case VT_I1:
    V_I1(result) = V_I1(&lv) - V_I1(&rv);
    break;
    case VT_UI1:
    V_UI1(result) = V_UI2(&lv) - V_UI1(&rv);
    break;
3745
    case VT_R8:
3746 3747
    V_R8(result) = V_R8(&lv) - V_R8(&rv);
    break;
3748
    case VT_DECIMAL:
3749 3750
    hres = VarDecSub(&(V_DECIMAL(&lv)), &(V_DECIMAL(&rv)), &(V_DECIMAL(result)));
    break;
3751
    }
3752 3753 3754 3755 3756

    VariantClear(&lv);
    VariantClear(&rv);

    return hres;
3757 3758
}

3759

Andreas Mohr's avatar
Andreas Mohr committed
3760 3761 3762
/**********************************************************************
 *              VarOr [OLEAUT32.157]
 *
Jon Griffiths's avatar
Jon Griffiths committed
3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776
 * Perform a logical or (OR) operation on two variants.
 *
 * PARAMS
 *  pVarLeft  [I] First variant
 *  pVarRight [I] Variant to OR with pVarLeft
 *  pVarOut   [O] Destination for OR result
 *
 * RETURNS
 *  Success: S_OK. pVarOut contains the result of the operation with its type
 *           taken from the table listed under VarXor().
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  See the Notes section of VarXor() for further information.
Andreas Mohr's avatar
Andreas Mohr committed
3777
 */
Jon Griffiths's avatar
Jon Griffiths committed
3778
HRESULT WINAPI VarOr(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
Andreas Mohr's avatar
Andreas Mohr committed
3779
{
Jon Griffiths's avatar
Jon Griffiths committed
3780 3781 3782
    VARTYPE vt = VT_I4;
    VARIANT varLeft, varRight, varStr;
    HRESULT hRet;
Andreas Mohr's avatar
Andreas Mohr committed
3783

Jon Griffiths's avatar
Jon Griffiths committed
3784 3785 3786
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
          debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
          debugstr_VF(pVarRight), pVarOut);
Andreas Mohr's avatar
Andreas Mohr committed
3787

Jon Griffiths's avatar
Jon Griffiths committed
3788 3789 3790 3791 3792
    if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
        V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
        V_VT(pVarLeft) == VT_DISPATCH || V_VT(pVarRight) == VT_DISPATCH ||
        V_VT(pVarLeft) == VT_RECORD || V_VT(pVarRight) == VT_RECORD)
        return DISP_E_BADVARTYPE;
Andreas Mohr's avatar
Andreas Mohr committed
3793

Jon Griffiths's avatar
Jon Griffiths committed
3794
    V_VT(&varLeft) = V_VT(&varRight) = V_VT(&varStr) = VT_EMPTY;
Andreas Mohr's avatar
Andreas Mohr committed
3795

Jon Griffiths's avatar
Jon Griffiths committed
3796 3797 3798 3799 3800
    if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
    {
        /* NULL OR Zero is NULL, NULL OR value is value */
        if (V_VT(pVarLeft) == VT_NULL)
            pVarLeft = pVarRight; /* point to the non-NULL var */
Andreas Mohr's avatar
Andreas Mohr committed
3801

Jon Griffiths's avatar
Jon Griffiths committed
3802 3803
        V_VT(pVarOut) = VT_NULL;
        V_I4(pVarOut) = 0;
Andreas Mohr's avatar
Andreas Mohr committed
3804

Jon Griffiths's avatar
Jon Griffiths committed
3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
        switch (V_VT(pVarLeft))
        {
        case VT_DATE: case VT_R8:
            if (V_R8(pVarLeft))
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_BOOL:
            if (V_BOOL(pVarLeft))
                *pVarOut = *pVarLeft;
            return S_OK;
         case VT_I2: case VT_UI2:
            if (V_I2(pVarLeft))
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_I1:
            if (V_I1(pVarLeft))
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_UI1:
            if (V_UI1(pVarLeft))
                *pVarOut = *pVarLeft;
            return S_OK;
        case VT_R4:
            if (V_R4(pVarLeft))
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_I4: case VT_UI4: case VT_INT: case VT_UINT:
            if (V_I4(pVarLeft))
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_CY:
            if (V_CY(pVarLeft).int64)
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_I8: case VT_UI8:
            if (V_I8(pVarLeft))
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_DECIMAL:
            if (DEC_HI32(&V_DECIMAL(pVarLeft)) || DEC_LO64(&V_DECIMAL(pVarLeft)))
                goto VarOr_AsEmpty;
            return S_OK;
        case VT_BSTR:
        {
            VARIANT_BOOL b;
Andreas Mohr's avatar
Andreas Mohr committed
3850

Jon Griffiths's avatar
Jon Griffiths committed
3851 3852 3853 3854 3855 3856 3857 3858
            if (!V_BSTR(pVarLeft))
                return DISP_E_BADVARTYPE;

            hRet = VarBoolFromStr(V_BSTR(pVarLeft), LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
            if (SUCCEEDED(hRet) && b)
            {
                V_VT(pVarOut) = VT_BOOL;
                V_BOOL(pVarOut) = b;
Andreas Mohr's avatar
Andreas Mohr committed
3859
            }
Jon Griffiths's avatar
Jon Griffiths committed
3860 3861 3862 3863 3864 3865 3866 3867 3868
            return hRet;
        }
        case VT_NULL: case VT_EMPTY:
            V_VT(pVarOut) = VT_NULL;
            return S_OK;
        default:
            return DISP_E_BADVARTYPE;
        }
    }
Andreas Mohr's avatar
Andreas Mohr committed
3869

Jon Griffiths's avatar
Jon Griffiths committed
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906
    if (V_VT(pVarLeft) == VT_EMPTY || V_VT(pVarRight) == VT_EMPTY)
    {
        if (V_VT(pVarLeft) == VT_EMPTY)
            pVarLeft = pVarRight; /* point to the non-EMPTY var */

VarOr_AsEmpty:
        /* Since one argument is empty (0), OR'ing it with the other simply
         * gives the others value (as 0|x => x). So just convert the other
         * argument to the required result type.
         */
        switch (V_VT(pVarLeft))
        {
        case VT_BSTR:
            if (!V_BSTR(pVarLeft))
                return DISP_E_BADVARTYPE;

            hRet = VariantCopy(&varStr, pVarLeft);
            if (FAILED(hRet))
                goto VarOr_Exit;
            pVarLeft = &varStr;
            hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
            if (FAILED(hRet))
                goto VarOr_Exit;
            /* Fall Through ... */
        case VT_EMPTY: case VT_UI1: case VT_BOOL: case VT_I2:
            V_VT(pVarOut) = VT_I2;
            break;
        case VT_DATE: case VT_CY: case VT_DECIMAL: case VT_R4: case VT_R8:
        case VT_I1: case VT_UI2: case VT_I4: case VT_UI4:
        case VT_INT: case VT_UINT: case VT_UI8:
            V_VT(pVarOut) = VT_I4;
            break;
        case VT_I8:
            V_VT(pVarOut) = VT_I8;
            break;
        default:
            return DISP_E_BADVARTYPE;
3907
        }
Jon Griffiths's avatar
Jon Griffiths committed
3908 3909 3910 3911 3912 3913
        hRet = VariantCopy(&varLeft, pVarLeft);
        if (FAILED(hRet))
            goto VarOr_Exit;
        pVarLeft = &varLeft;
        hRet = VariantChangeType(pVarOut, pVarLeft, 0, V_VT(pVarOut));
        goto VarOr_Exit;
3914 3915
    }

Jon Griffiths's avatar
Jon Griffiths committed
3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016
    if (V_VT(pVarLeft) == VT_BOOL && V_VT(pVarRight) == VT_BOOL)
    {
        V_VT(pVarOut) = VT_BOOL;
        V_BOOL(pVarOut) = V_BOOL(pVarLeft) | V_BOOL(pVarRight);
        return S_OK;
    }

    if (V_VT(pVarLeft) == VT_UI1 && V_VT(pVarRight) == VT_UI1)
    {
        V_VT(pVarOut) = VT_UI1;
        V_UI1(pVarOut) = V_UI1(pVarLeft) | V_UI1(pVarRight);
        return S_OK;
    }

    if (V_VT(pVarLeft) == VT_BSTR)
    {
        hRet = VariantCopy(&varStr, pVarLeft);
        if (FAILED(hRet))
            goto VarOr_Exit;
        pVarLeft = &varStr;
        hRet = VariantChangeType(pVarLeft, pVarLeft, 0, VT_BOOL);
        if (FAILED(hRet))
            goto VarOr_Exit;
    }

    if (V_VT(pVarLeft) == VT_BOOL &&
        (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_BSTR))
    {
        vt = VT_BOOL;
    }
    else if ((V_VT(pVarLeft) == VT_BOOL || V_VT(pVarLeft) == VT_UI1 ||
        V_VT(pVarLeft) == VT_I2 || V_VT(pVarLeft) == VT_BSTR) &&
        (V_VT(pVarRight) == VT_BOOL || V_VT(pVarRight) == VT_UI1 ||
        V_VT(pVarRight) == VT_I2 || V_VT(pVarRight) == VT_BSTR))
    {
        vt = VT_I2;
    }
    else if (V_VT(pVarLeft) == VT_I8 || V_VT(pVarRight) == VT_I8)
    {
        if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
            return DISP_E_TYPEMISMATCH;
        vt = VT_I8;
    }

    hRet = VariantCopy(&varLeft, pVarLeft);
    if (FAILED(hRet))
        goto VarOr_Exit;

    hRet = VariantCopy(&varRight, pVarRight);
    if (FAILED(hRet))
        goto VarOr_Exit;

    if (vt == VT_I4 && V_VT(&varLeft) == VT_UI4)
        V_VT(&varLeft) = VT_I4; /* Don't overflow */
    else
    {
        double d;

        if (V_VT(&varLeft) == VT_BSTR &&
            FAILED(VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d)))
            hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL, VT_BOOL);
        if (SUCCEEDED(hRet) && V_VT(&varLeft) != vt)
            hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
        if (FAILED(hRet))
            goto VarOr_Exit;
    }

    if (vt == VT_I4 && V_VT(&varRight) == VT_UI4)
        V_VT(&varRight) = VT_I4; /* Don't overflow */
    else
    {
        double d;

        if (V_VT(&varRight) == VT_BSTR &&
            FAILED(VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d)))
            hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL, VT_BOOL);
        if (SUCCEEDED(hRet) && V_VT(&varRight) != vt)
            hRet = VariantChangeType(&varRight, &varRight, 0, vt);
        if (FAILED(hRet))
            goto VarOr_Exit;
    }

    V_VT(pVarOut) = vt;
    if (vt == VT_I8)
    {
        V_I8(pVarOut) = V_I8(&varLeft) | V_I8(&varRight);
    }
    else if (vt == VT_I4)
    {
        V_I4(pVarOut) = V_I4(&varLeft) | V_I4(&varRight);
    }
    else
    {
        V_I2(pVarOut) = V_I2(&varLeft) | V_I2(&varRight);
    }

VarOr_Exit:
    VariantClear(&varStr);
    VariantClear(&varLeft);
    VariantClear(&varRight);
    return hRet;
4017 4018
}

4019
/**********************************************************************
4020 4021 4022 4023 4024 4025 4026
 * VarAbs [OLEAUT32.168]
 *
 * Convert a variant to its absolute value.
 *
 * PARAMS
 *  pVarIn  [I] Source variant
 *  pVarOut [O] Destination for converted value
4027
 *
4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040
 * RETURNS
 *  Success: S_OK. pVarOut contains the absolute value of pVarIn.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  - This function does not process by-reference variants.
 *  - The type of the value stored in pVarOut depends on the type of pVarIn,
 *    according to the following table:
 *| Input Type       Output Type
 *| ----------       -----------
 *| VT_BOOL          VT_I2
 *| VT_BSTR          VT_R8
 *| (All others)     Unchanged
4041
 */
4042
HRESULT WINAPI VarAbs(LPVARIANT pVarIn, LPVARIANT pVarOut)
4043
{
4044 4045
    VARIANT varIn;
    HRESULT hRet = S_OK;
4046

4047 4048
    TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
          debugstr_VF(pVarIn), pVarOut);
4049

4050 4051 4052 4053
    if (V_ISARRAY(pVarIn) || V_VT(pVarIn) == VT_UNKNOWN ||
        V_VT(pVarIn) == VT_DISPATCH || V_VT(pVarIn) == VT_RECORD ||
        V_VT(pVarIn) == VT_ERROR)
        return DISP_E_TYPEMISMATCH;
4054

4055
    *pVarOut = *pVarIn; /* Shallow copy the value, and invert it if needed */
4056

4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092
#define ABS_CASE(typ,min) \
    case VT_##typ: if (V_##typ(pVarIn) == min) hRet = DISP_E_OVERFLOW; \
                  else if (V_##typ(pVarIn) < 0) V_##typ(pVarOut) = -V_##typ(pVarIn); \
                  break

    switch (V_VT(pVarIn))
    {
    ABS_CASE(I1,I1_MIN);
    case VT_BOOL:
        V_VT(pVarOut) = VT_I2;
        /* BOOL->I2, Fall through ... */
    ABS_CASE(I2,I2_MIN);
    case VT_INT:
    ABS_CASE(I4,I4_MIN);
    ABS_CASE(I8,I8_MIN);
    ABS_CASE(R4,R4_MIN);
    case VT_BSTR:
        hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
        if (FAILED(hRet))
            break;
        V_VT(pVarOut) = VT_R8;
        pVarIn = &varIn;
        /* Fall through ... */
    case VT_DATE:
    ABS_CASE(R8,R8_MIN);
    case VT_CY:
        hRet = VarCyAbs(V_CY(pVarIn), & V_CY(pVarOut));
        break;
    case VT_DECIMAL:
        DEC_SIGN(&V_DECIMAL(pVarOut)) &= ~DECIMAL_NEG;
        break;
    case VT_UI1:
    case VT_UI2:
    case VT_UINT:
    case VT_UI4:
    case VT_UI8:
Jon Griffiths's avatar
Jon Griffiths committed
4093 4094
        /* No-Op */
        break;
4095
    case VT_EMPTY:
Jon Griffiths's avatar
Jon Griffiths committed
4096
        V_VT(pVarOut) = VT_I2;
4097
    case VT_NULL:
Jon Griffiths's avatar
Jon Griffiths committed
4098
        V_I2(pVarOut) = 0;
4099 4100 4101
        break;
    default:
        hRet = DISP_E_BADVARTYPE;
4102 4103
    }

4104 4105 4106
    return hRet;
}

4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262
/**********************************************************************
 *              VarFix [OLEAUT32.169]
 *
 * Truncate a variants value to a whole number.
 *
 * PARAMS
 *  pVarIn  [I] Source variant
 *  pVarOut [O] Destination for converted value
 *
 * RETURNS
 *  Success: S_OK. pVarOut contains the converted value.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  - The type of the value stored in pVarOut depends on the type of pVarIn,
 *    according to the following table:
 *| Input Type       Output Type
 *| ----------       -----------
 *|  VT_BOOL          VT_I2
 *|  VT_EMPTY         VT_I2
 *|  VT_BSTR          VT_R8
 *|  All Others       Unchanged
 *  - The difference between this function and VarInt() is that VarInt() rounds
 *    negative numbers away from 0, while this function rounds them towards zero.
 */
HRESULT WINAPI VarFix(LPVARIANT pVarIn, LPVARIANT pVarOut)
{
    HRESULT hRet = S_OK;

    TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
          debugstr_VF(pVarIn), pVarOut);

    V_VT(pVarOut) = V_VT(pVarIn);

    switch (V_VT(pVarIn))
    {
    case VT_UI1:
        V_UI1(pVarOut) = V_UI1(pVarIn);
        break;
    case VT_BOOL:
        V_VT(pVarOut) = VT_I2;
        /* Fall through */
     case VT_I2:
        V_I2(pVarOut) = V_I2(pVarIn);
        break;
     case VT_I4:
        V_I4(pVarOut) = V_I4(pVarIn);
        break;
     case VT_I8:
        V_I8(pVarOut) = V_I8(pVarIn);
        break;
    case VT_R4:
        if (V_R4(pVarIn) < 0.0f)
            V_R4(pVarOut) = (float)ceil(V_R4(pVarIn));
        else
            V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
        break;
    case VT_BSTR:
        V_VT(pVarOut) = VT_R8;
        hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
        pVarIn = pVarOut;
        /* Fall through */
    case VT_DATE:
    case VT_R8:
        if (V_R8(pVarIn) < 0.0)
            V_R8(pVarOut) = ceil(V_R8(pVarIn));
        else
            V_R8(pVarOut) = floor(V_R8(pVarIn));
        break;
    case VT_CY:
        hRet = VarCyFix(V_CY(pVarIn), &V_CY(pVarOut));
        break;
    case VT_DECIMAL:
        hRet = VarDecFix(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
        break;
    case VT_EMPTY:
        V_VT(pVarOut) = VT_I2;
        V_I2(pVarOut) = 0;
        break;
    case VT_NULL:
        /* No-Op */
        break;
    default:
        if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
            FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
            hRet = DISP_E_BADVARTYPE;
        else
            hRet = DISP_E_TYPEMISMATCH;
    }
    if (FAILED(hRet))
      V_VT(pVarOut) = VT_EMPTY;

    return hRet;
}

/**********************************************************************
 *              VarInt [OLEAUT32.172]
 *
 * Truncate a variants value to a whole number.
 *
 * PARAMS
 *  pVarIn  [I] Source variant
 *  pVarOut [O] Destination for converted value
 *
 * RETURNS
 *  Success: S_OK. pVarOut contains the converted value.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  - The type of the value stored in pVarOut depends on the type of pVarIn,
 *    according to the following table:
 *| Input Type       Output Type
 *| ----------       -----------
 *|  VT_BOOL          VT_I2
 *|  VT_EMPTY         VT_I2
 *|  VT_BSTR          VT_R8
 *|  All Others       Unchanged
 *  - The difference between this function and VarFix() is that VarFix() rounds
 *    negative numbers towards 0, while this function rounds them away from zero.
 */
HRESULT WINAPI VarInt(LPVARIANT pVarIn, LPVARIANT pVarOut)
{
    HRESULT hRet = S_OK;

    TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
          debugstr_VF(pVarIn), pVarOut);

    V_VT(pVarOut) = V_VT(pVarIn);

    switch (V_VT(pVarIn))
    {
    case VT_R4:
        V_R4(pVarOut) = (float)floor(V_R4(pVarIn));
        break;
    case VT_BSTR:
        V_VT(pVarOut) = VT_R8;
        hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
        pVarIn = pVarOut;
        /* Fall through */
    case VT_DATE:
    case VT_R8:
        V_R8(pVarOut) = floor(V_R8(pVarIn));
        break;
    case VT_CY:
        hRet = VarCyInt(V_CY(pVarIn), &V_CY(pVarOut));
        break;
    case VT_DECIMAL:
        hRet = VarDecInt(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
        break;
    default:
        return VarFix(pVarIn, pVarOut);
    }

    return hRet;
}

Jon Griffiths's avatar
Jon Griffiths committed
4263
/**********************************************************************
4264
 *              VarXor [OLEAUT32.167]
Jon Griffiths's avatar
Jon Griffiths committed
4265
 *
4266
 * Perform a logical exclusive-or (XOR) operation on two variants.
Jon Griffiths's avatar
Jon Griffiths committed
4267 4268
 *
 * PARAMS
4269 4270 4271
 *  pVarLeft  [I] First variant
 *  pVarRight [I] Variant to XOR with pVarLeft
 *  pVarOut   [O] Destination for XOR result
Jon Griffiths's avatar
Jon Griffiths committed
4272 4273
 *
 * RETURNS
4274 4275
 *  Success: S_OK. pVarOut contains the result of the operation with its type
 *           taken from the table below).
Jon Griffiths's avatar
Jon Griffiths committed
4276 4277 4278
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
4279 4280 4281 4282 4283 4284 4285 4286 4287
 *  - Neither pVarLeft or pVarRight are modified by this function.
 *  - This function does not process by-reference variants.
 *  - Input types of VT_BSTR may be numeric strings or boolean text.
 *  - The type of result stored in pVarOut depends on the types of pVarLeft
 *    and pVarRight, and will be one of VT_UI1, VT_I2, VT_I4, VT_I8, VT_BOOL,
 *    or VT_NULL if the function succeeds.
 *  - Type promotion is inconsistent and as a result certain combinations of
 *    values will return DISP_E_OVERFLOW even when they could be represented.
 *    This matches the behaviour of native oleaut32.
Jon Griffiths's avatar
Jon Griffiths committed
4288
 */
4289
HRESULT WINAPI VarXor(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
Jon Griffiths's avatar
Jon Griffiths committed
4290
{
Jon Griffiths's avatar
Jon Griffiths committed
4291
    VARTYPE vt;
Jon Griffiths's avatar
Jon Griffiths committed
4292
    VARIANT varLeft, varRight;
Jon Griffiths's avatar
Jon Griffiths committed
4293
    double d;
Jon Griffiths's avatar
Jon Griffiths committed
4294 4295 4296 4297 4298 4299 4300
    HRESULT hRet;

    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
          debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
          debugstr_VF(pVarRight), pVarOut);

    if (V_EXTRA_TYPE(pVarLeft) || V_EXTRA_TYPE(pVarRight) ||
Jon Griffiths's avatar
Jon Griffiths committed
4301 4302
        V_VT(pVarLeft) > VT_UINT || V_VT(pVarRight) > VT_UINT ||
        V_VT(pVarLeft) == VT_VARIANT || V_VT(pVarRight) == VT_VARIANT ||
Jon Griffiths's avatar
Jon Griffiths committed
4303
        V_VT(pVarLeft) == VT_UNKNOWN || V_VT(pVarRight) == VT_UNKNOWN ||
Jon Griffiths's avatar
Jon Griffiths committed
4304 4305
        V_VT(pVarLeft) == (VARTYPE)15 || V_VT(pVarRight) == (VARTYPE)15 ||
        V_VT(pVarLeft) == VT_ERROR || V_VT(pVarRight) == VT_ERROR)
Jon Griffiths's avatar
Jon Griffiths committed
4306 4307 4308 4309
        return DISP_E_BADVARTYPE;

    if (V_VT(pVarLeft) == VT_NULL || V_VT(pVarRight) == VT_NULL)
    {
Jon Griffiths's avatar
Jon Griffiths committed
4310 4311 4312
        /* NULL XOR anything valid is NULL */
        V_VT(pVarOut) = VT_NULL;
        return S_OK;
Jon Griffiths's avatar
Jon Griffiths committed
4313 4314
    }

Jon Griffiths's avatar
Jon Griffiths committed
4315 4316
    /* Copy our inputs so we don't disturb anything */
    V_VT(&varLeft) = V_VT(&varRight) = VT_EMPTY;
Jon Griffiths's avatar
Jon Griffiths committed
4317

Jon Griffiths's avatar
Jon Griffiths committed
4318 4319 4320
    hRet = VariantCopy(&varLeft, pVarLeft);
    if (FAILED(hRet))
        goto VarXor_Exit;
Jon Griffiths's avatar
Jon Griffiths committed
4321

Jon Griffiths's avatar
Jon Griffiths committed
4322 4323 4324
    hRet = VariantCopy(&varRight, pVarRight);
    if (FAILED(hRet))
        goto VarXor_Exit;
Jon Griffiths's avatar
Jon Griffiths committed
4325

Jon Griffiths's avatar
Jon Griffiths committed
4326 4327
    /* Try any strings first as numbers, then as VT_BOOL */
    if (V_VT(&varLeft) == VT_BSTR)
Jon Griffiths's avatar
Jon Griffiths committed
4328
    {
Jon Griffiths's avatar
Jon Griffiths committed
4329 4330 4331 4332 4333
        hRet = VarR8FromStr(V_BSTR(&varLeft), LOCALE_USER_DEFAULT, 0, &d);
        hRet = VariantChangeType(&varLeft, &varLeft, VARIANT_LOCALBOOL,
                                 FAILED(hRet) ? VT_BOOL : VT_I4);
        if (FAILED(hRet))
            goto VarXor_Exit;
Jon Griffiths's avatar
Jon Griffiths committed
4334 4335
    }

Jon Griffiths's avatar
Jon Griffiths committed
4336
    if (V_VT(&varRight) == VT_BSTR)
Jon Griffiths's avatar
Jon Griffiths committed
4337
    {
Jon Griffiths's avatar
Jon Griffiths committed
4338 4339 4340 4341 4342
        hRet = VarR8FromStr(V_BSTR(&varRight), LOCALE_USER_DEFAULT, 0, &d);
        hRet = VariantChangeType(&varRight, &varRight, VARIANT_LOCALBOOL,
                                 FAILED(hRet) ? VT_BOOL : VT_I4);
        if (FAILED(hRet))
            goto VarXor_Exit;
Jon Griffiths's avatar
Jon Griffiths committed
4343 4344
    }

Jon Griffiths's avatar
Jon Griffiths committed
4345 4346
    /* Determine the result type */
    if (V_VT(&varLeft) == VT_I8 || V_VT(&varRight) == VT_I8)
4347 4348 4349 4350 4351
    {
        if (V_VT(pVarLeft) == VT_INT || V_VT(pVarRight) == VT_INT)
            return DISP_E_TYPEMISMATCH;
        vt = VT_I8;
    }
Jon Griffiths's avatar
Jon Griffiths committed
4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382
    else
    {
        switch ((V_VT(&varLeft) << 16) | V_VT(&varRight))
        {
        case (VT_BOOL  << 16) | VT_BOOL:
            vt = VT_BOOL;
            break;
        case (VT_UI1   << 16) | VT_UI1:
            vt = VT_UI1;
            break;
        case (VT_EMPTY << 16) | VT_EMPTY:
        case (VT_EMPTY << 16) | VT_UI1:
        case (VT_EMPTY << 16) | VT_I2:
        case (VT_EMPTY << 16) | VT_BOOL:
        case (VT_UI1   << 16) | VT_EMPTY:
        case (VT_UI1   << 16) | VT_I2:
        case (VT_UI1   << 16) | VT_BOOL:
        case (VT_I2    << 16) | VT_EMPTY:
        case (VT_I2    << 16) | VT_UI1:
        case (VT_I2    << 16) | VT_I2:
        case (VT_I2    << 16) | VT_BOOL:
        case (VT_BOOL  << 16) | VT_EMPTY:
        case (VT_BOOL  << 16) | VT_UI1:
        case (VT_BOOL  << 16) | VT_I2:
            vt = VT_I2;
            break;
        default:
            vt = VT_I4;
            break;
        }
    }
Jon Griffiths's avatar
Jon Griffiths committed
4383

Jon Griffiths's avatar
Jon Griffiths committed
4384 4385 4386 4387 4388 4389 4390 4391
    /* VT_UI4 does not overflow */
    if (vt != VT_I8)
    {
        if (V_VT(&varLeft) == VT_UI4)
            V_VT(&varLeft) = VT_I4;
        if (V_VT(&varRight) == VT_UI4)
            V_VT(&varRight) = VT_I4;
    }
Jon Griffiths's avatar
Jon Griffiths committed
4392

Jon Griffiths's avatar
Jon Griffiths committed
4393 4394 4395
    /* Convert our input copies to the result type */
    if (V_VT(&varLeft) != vt)
        hRet = VariantChangeType(&varLeft, &varLeft, 0, vt);
Jon Griffiths's avatar
Jon Griffiths committed
4396
    if (FAILED(hRet))
4397
        goto VarXor_Exit;
Jon Griffiths's avatar
Jon Griffiths committed
4398

Jon Griffiths's avatar
Jon Griffiths committed
4399 4400
    if (V_VT(&varRight) != vt)
        hRet = VariantChangeType(&varRight, &varRight, 0, vt);
Jon Griffiths's avatar
Jon Griffiths committed
4401
    if (FAILED(hRet))
4402
        goto VarXor_Exit;
Jon Griffiths's avatar
Jon Griffiths committed
4403 4404

    V_VT(pVarOut) = vt;
Jon Griffiths's avatar
Jon Griffiths committed
4405 4406 4407

    /* Calculate the result */
    switch (vt)
Jon Griffiths's avatar
Jon Griffiths committed
4408
    {
Jon Griffiths's avatar
Jon Griffiths committed
4409
    case VT_I8:
4410
        V_I8(pVarOut) = V_I8(&varLeft) ^ V_I8(&varRight);
Jon Griffiths's avatar
Jon Griffiths committed
4411 4412
        break;
    case VT_I4:
4413
        V_I4(pVarOut) = V_I4(&varLeft) ^ V_I4(&varRight);
Jon Griffiths's avatar
Jon Griffiths committed
4414 4415 4416
        break;
    case VT_BOOL:
    case VT_I2:
4417
        V_I2(pVarOut) = V_I2(&varLeft) ^ V_I2(&varRight);
Jon Griffiths's avatar
Jon Griffiths committed
4418 4419 4420 4421
        break;
    case VT_UI1:
        V_UI1(pVarOut) = V_UI1(&varLeft) ^ V_UI1(&varRight);
        break;
Jon Griffiths's avatar
Jon Griffiths committed
4422 4423
    }

4424
VarXor_Exit:
Jon Griffiths's avatar
Jon Griffiths committed
4425 4426 4427 4428 4429
    VariantClear(&varLeft);
    VariantClear(&varRight);
    return hRet;
}

4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440
/**********************************************************************
 *              VarEqv [OLEAUT32.172]
 *
 * Determine if two variants contain the same value.
 *
 * PARAMS
 *  pVarLeft  [I] First variant to compare
 *  pVarRight [I] Variant to compare to pVarLeft
 *  pVarOut   [O] Destination for comparison result
 *
 * RETURNS
4441
 *  Success: S_OK. pVarOut contains the result of the comparison (VARIANT_TRUE
4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467
 *           if equivalent or non-zero otherwise.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  - This function simply calls VarXor() on pVarLeft and pVarRight and inverts
 *    the result.
 */
HRESULT WINAPI VarEqv(LPVARIANT pVarLeft, LPVARIANT pVarRight, LPVARIANT pVarOut)
{
    HRESULT hRet;

    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", pVarLeft, debugstr_VT(pVarLeft),
          debugstr_VF(pVarLeft), pVarRight, debugstr_VT(pVarRight),
          debugstr_VF(pVarRight), pVarOut);

    hRet = VarXor(pVarLeft, pVarRight, pVarOut);
    if (SUCCEEDED(hRet))
    {
        if (V_VT(pVarOut) == VT_I8)
            V_I8(pVarOut) = ~V_I8(pVarOut);
        else
            V_UI4(pVarOut) = ~V_UI4(pVarOut);
    }
    return hRet;
}

4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588
/**********************************************************************
 *              VarNeg [OLEAUT32.173]
 *
 * Negate the value of a variant.
 *
 * PARAMS
 *  pVarIn  [I] Source variant
 *  pVarOut [O] Destination for converted value
 *
 * RETURNS
 *  Success: S_OK. pVarOut contains the converted value.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  - The type of the value stored in pVarOut depends on the type of pVarIn,
 *    according to the following table:
 *| Input Type       Output Type
 *| ----------       -----------
 *|  VT_EMPTY         VT_I2
 *|  VT_UI1           VT_I2
 *|  VT_BOOL          VT_I2
 *|  VT_BSTR          VT_R8
 *|  All Others       Unchanged (unless promoted)
 *  - Where the negated value of a variant does not fit in its base type, the type
 *    is promoted according to the following table:
 *| Input Type       Promoted To
 *| ----------       -----------
 *|   VT_I2            VT_I4
 *|   VT_I4            VT_R8
 *|   VT_I8            VT_R8
 *  - The native version of this function returns DISP_E_BADVARTYPE for valid
 *    variant types that cannot be negated, and returns DISP_E_TYPEMISMATCH
 *    for types which are not valid. Since this is in contravention of the
 *    meaning of those error codes and unlikely to be relied on by applications,
 *    this implementation returns errors consistent with the other high level
 *    variant math functions.
 */
HRESULT WINAPI VarNeg(LPVARIANT pVarIn, LPVARIANT pVarOut)
{
    HRESULT hRet = S_OK;

    TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
          debugstr_VF(pVarIn), pVarOut);

    V_VT(pVarOut) = V_VT(pVarIn);

    switch (V_VT(pVarIn))
    {
    case VT_UI1:
        V_VT(pVarOut) = VT_I2;
        V_I2(pVarOut) = -V_UI1(pVarIn);
        break;
    case VT_BOOL:
        V_VT(pVarOut) = VT_I2;
        /* Fall through */
    case VT_I2:
        if (V_I2(pVarIn) == I2_MIN)
        {
            V_VT(pVarOut) = VT_I4;
            V_I4(pVarOut) = -(int)V_I2(pVarIn);
        }
        else
            V_I2(pVarOut) = -V_I2(pVarIn);
        break;
    case VT_I4:
        if (V_I4(pVarIn) == I4_MIN)
        {
            V_VT(pVarOut) = VT_R8;
            V_R8(pVarOut) = -(double)V_I4(pVarIn);
        }
        else
            V_I4(pVarOut) = -V_I4(pVarIn);
        break;
    case VT_I8:
        if (V_I8(pVarIn) == I8_MIN)
        {
            V_VT(pVarOut) = VT_R8;
            hRet = VarR8FromI8(V_I8(pVarIn), &V_R8(pVarOut));
            V_R8(pVarOut) *= -1.0;
        }
        else
            V_I8(pVarOut) = -V_I8(pVarIn);
        break;
    case VT_R4:
        V_R4(pVarOut) = -V_R4(pVarIn);
        break;
    case VT_DATE:
    case VT_R8:
        V_R8(pVarOut) = -V_R8(pVarIn);
        break;
    case VT_CY:
        hRet = VarCyNeg(V_CY(pVarIn), &V_CY(pVarOut));
        break;
    case VT_DECIMAL:
        hRet = VarDecNeg(&V_DECIMAL(pVarIn), &V_DECIMAL(pVarOut));
        break;
    case VT_BSTR:
        V_VT(pVarOut) = VT_R8;
        hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(pVarOut));
        V_R8(pVarOut) = -V_R8(pVarOut);
        break;
    case VT_EMPTY:
        V_VT(pVarOut) = VT_I2;
        V_I2(pVarOut) = 0;
        break;
    case VT_NULL:
        /* No-Op */
        break;
    default:
        if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
            FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
            hRet = DISP_E_BADVARTYPE;
        else
            hRet = DISP_E_TYPEMISMATCH;
    }
    if (FAILED(hRet))
      V_VT(pVarOut) = VT_EMPTY;

    return hRet;
}

4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602
/**********************************************************************
 *              VarNot [OLEAUT32.174]
 *
 * Perform a not operation on a variant.
 *
 * PARAMS
 *  pVarIn  [I] Source variant
 *  pVarOut [O] Destination for converted value
 *
 * RETURNS
 *  Success: S_OK. pVarOut contains the converted value.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
4603
 *  - Strictly speaking, this function performs a bitwise ones complement
4604 4605 4606 4607 4608 4609 4610 4611 4612 4613
 *    on the variants value (after possibly converting to VT_I4, see below).
 *    This only behaves like a boolean not operation if the value in
 *    pVarIn is either VARIANT_TRUE or VARIANT_FALSE and the type is signed.
 *  - To perform a genuine not operation, convert the variant to a VT_BOOL
 *    before calling this function.
 *  - This function does not process by-reference variants.
 *  - The type of the value stored in pVarOut depends on the type of pVarIn,
 *    according to the following table:
 *| Input Type       Output Type
 *| ----------       -----------
Jon Griffiths's avatar
Jon Griffiths committed
4614
 *| VT_EMPTY         VT_I2
4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633
 *| VT_R4            VT_I4
 *| VT_R8            VT_I4
 *| VT_BSTR          VT_I4
 *| VT_DECIMAL       VT_I4
 *| VT_CY            VT_I4
 *| (All others)     Unchanged
 */
HRESULT WINAPI VarNot(LPVARIANT pVarIn, LPVARIANT pVarOut)
{
    VARIANT varIn;
    HRESULT hRet = S_OK;

    TRACE("(%p->(%s%s),%p)\n", pVarIn, debugstr_VT(pVarIn),
          debugstr_VF(pVarIn), pVarOut);

    V_VT(pVarOut) = V_VT(pVarIn);

    switch (V_VT(pVarIn))
    {
Jon Griffiths's avatar
Jon Griffiths committed
4634 4635 4636 4637
    case VT_I1:
        V_I4(pVarOut) = ~V_I1(pVarIn);
        V_VT(pVarOut) = VT_I4;
        break;
4638 4639 4640
    case VT_UI1: V_UI1(pVarOut) = ~V_UI1(pVarIn); break;
    case VT_BOOL:
    case VT_I2:  V_I2(pVarOut) = ~V_I2(pVarIn); break;
Jon Griffiths's avatar
Jon Griffiths committed
4641 4642 4643 4644
    case VT_UI2:
        V_I4(pVarOut) = ~V_UI2(pVarIn);
        V_VT(pVarOut) = VT_I4;
        break;
4645
    case VT_DECIMAL:
4646
        hRet = VarI4FromDec(&V_DECIMAL(pVarIn), &V_I4(&varIn));
4647 4648 4649 4650 4651
        if (FAILED(hRet))
            break;
        pVarIn = &varIn;
        /* Fall through ... */
    case VT_INT:
Jon Griffiths's avatar
Jon Griffiths committed
4652 4653
        V_VT(pVarOut) = VT_I4;
        /* Fall through ... */
4654 4655
    case VT_I4:  V_I4(pVarOut) = ~V_I4(pVarIn); break;
    case VT_UINT:
Jon Griffiths's avatar
Jon Griffiths committed
4656 4657 4658 4659
    case VT_UI4:
        V_I4(pVarOut) = ~V_UI4(pVarIn);
        V_VT(pVarOut) = VT_I4;
        break;
4660
    case VT_I8:  V_I8(pVarOut) = ~V_I8(pVarIn); break;
Jon Griffiths's avatar
Jon Griffiths committed
4661 4662 4663 4664
    case VT_UI8:
        V_I4(pVarOut) = ~V_UI8(pVarIn);
        V_VT(pVarOut) = VT_I4;
        break;
4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682
    case VT_R4:
        hRet = VarI4FromR4(V_R4(pVarIn), &V_I4(pVarOut));
        V_I4(pVarOut) = ~V_I4(pVarOut);
        V_VT(pVarOut) = VT_I4;
        break;
    case VT_BSTR:
        hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
        if (FAILED(hRet))
            break;
        pVarIn = &varIn;
        /* Fall through ... */
    case VT_DATE:
    case VT_R8:
        hRet = VarI4FromR8(V_R8(pVarIn), &V_I4(pVarOut));
        V_I4(pVarOut) = ~V_I4(pVarOut);
        V_VT(pVarOut) = VT_I4;
        break;
    case VT_CY:
Chris Morgan's avatar
Chris Morgan committed
4683 4684 4685
        hRet = VarI4FromCy(V_CY(pVarIn), &V_I4(pVarOut));
        V_I4(pVarOut) = ~V_I4(pVarOut);
        V_VT(pVarOut) = VT_I4;
4686 4687
        break;
    case VT_EMPTY:
Jon Griffiths's avatar
Jon Griffiths committed
4688 4689 4690
        V_I2(pVarOut) = ~0;
        V_VT(pVarOut) = VT_I2;
        break;
4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704
    case VT_NULL:
        /* No-Op */
        break;
    default:
        if (V_TYPE(pVarIn) == VT_CLSID || /* VT_CLSID is a special case */
            FAILED(VARIANT_ValidateType(V_VT(pVarIn))))
            hRet = DISP_E_BADVARTYPE;
        else
            hRet = DISP_E_TYPEMISMATCH;
    }
    if (FAILED(hRet))
      V_VT(pVarOut) = VT_EMPTY;

    return hRet;
4705 4706
}

4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827
/**********************************************************************
 *              VarRound [OLEAUT32.175]
 *
 * Perform a round operation on a variant.
 *
 * PARAMS
 *  pVarIn  [I] Source variant
 *  deci    [I] Number of decimals to round to
 *  pVarOut [O] Destination for converted value
 *
 * RETURNS
 *  Success: S_OK. pVarOut contains the converted value.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  - Floating point values are rounded to the desired number of decimals.
 *  - Some integer types are just copied to the return variable.
 *  - Some other integer types are not handled and fail.
 */
HRESULT WINAPI VarRound(LPVARIANT pVarIn, int deci, LPVARIANT pVarOut)
{
    VARIANT varIn;
    HRESULT hRet = S_OK;
    float factor;

    TRACE("(%p->(%s%s),%d)\n", pVarIn, debugstr_VT(pVarIn), debugstr_VF(pVarIn), deci);

    switch (V_VT(pVarIn))
    {
    /* cases that fail on windows */
    case VT_I1:
    case VT_I8:
    case VT_UI2:
    case VT_UI4:
	hRet = DISP_E_BADVARTYPE;
	break;

    /* cases just copying in to out */
    case VT_UI1:
	V_VT(pVarOut) = V_VT(pVarIn);
	V_UI1(pVarOut) = V_UI1(pVarIn);
	break;
    case VT_I2:
	V_VT(pVarOut) = V_VT(pVarIn);
	V_I2(pVarOut) = V_I2(pVarIn);
	break;
    case VT_I4:
	V_VT(pVarOut) = V_VT(pVarIn);
	V_I4(pVarOut) = V_I4(pVarIn);
	break;
    case VT_NULL:
	V_VT(pVarOut) = V_VT(pVarIn);
	/* value unchanged */
	break;

    /* cases that change type */
    case VT_EMPTY:
	V_VT(pVarOut) = VT_I2;
	V_I2(pVarOut) = 0;
	break;
    case VT_BOOL:
	V_VT(pVarOut) = VT_I2;
	V_I2(pVarOut) = V_BOOL(pVarIn);
	break;
    case VT_BSTR:
	hRet = VarR8FromStr(V_BSTR(pVarIn), LOCALE_USER_DEFAULT, 0, &V_R8(&varIn));
	if (FAILED(hRet))
	    break;
	V_VT(&varIn)=VT_R8;
	pVarIn = &varIn;
	/* Fall through ... */

    /* cases we need to do math */
    case VT_R8:
	if (V_R8(pVarIn)>0) {
	    V_R8(pVarOut)=floor(V_R8(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
	} else {
	    V_R8(pVarOut)=ceil(V_R8(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
	}
	V_VT(pVarOut) = V_VT(pVarIn);
	break;
    case VT_R4:
	if (V_R4(pVarIn)>0) {
	    V_R4(pVarOut)=floor(V_R4(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
	} else {
	    V_R4(pVarOut)=ceil(V_R4(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
	}
	V_VT(pVarOut) = V_VT(pVarIn);
	break;
    case VT_DATE:
	if (V_DATE(pVarIn)>0) {
	    V_DATE(pVarOut)=floor(V_DATE(pVarIn)*pow(10, deci)+0.5)/pow(10, deci);
	} else {
	    V_DATE(pVarOut)=ceil(V_DATE(pVarIn)*pow(10, deci)-0.5)/pow(10, deci);
	}
	V_VT(pVarOut) = V_VT(pVarIn);
	break;
    case VT_CY:
	if (deci>3)
	    factor=1;
	else
	    factor=pow(10, 4-deci);

	if (V_CY(pVarIn).int64>0) {
	    V_CY(pVarOut).int64=floor(V_CY(pVarIn).int64/factor)*factor;
	} else {
	    V_CY(pVarOut).int64=ceil(V_CY(pVarIn).int64/factor)*factor;
	}
	V_VT(pVarOut) = V_VT(pVarIn);
	break;

    /* cases we don't know yet */
    default:
	FIXME("unimplemented part, V_VT(pVarIn) == 0x%X, deci == %d\n",
		V_VT(pVarIn) & VT_TYPEMASK, deci);
	hRet = DISP_E_BADVARTYPE;
    }

    if (FAILED(hRet))
      V_VT(pVarOut) = VT_EMPTY;

4828
    TRACE("returning 0x%08x (%s%s),%f\n", hRet, debugstr_VT(pVarOut),
4829 4830 4831 4832 4833 4834
	debugstr_VF(pVarOut), (V_VT(pVarOut) == VT_R4) ? V_R4(pVarOut) :
	(V_VT(pVarOut) == VT_R8) ? V_R8(pVarOut) : 0);

    return hRet;
}

Daniel Remenak's avatar
Daniel Remenak committed
4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853
/**********************************************************************
 *              VarIdiv [OLEAUT32.153]
 *
 * Converts input variants to integers and divides them. 
 *
 * PARAMS
 *  left     [I] Left hand variant
 *  right    [I] Right hand variant
 *  result   [O] Destination for quotient
 *
 * RETURNS
 *  Success: S_OK.  result contains the quotient.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTES
 *  If either expression is null, null is returned, as per MSDN
 */
HRESULT WINAPI VarIdiv(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911
    HRESULT hres = S_OK;
    VARTYPE resvt = VT_EMPTY;
    VARTYPE leftvt,rightvt;
    VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
    VARIANT lv,rv;

    leftvt = V_VT(left)&VT_TYPEMASK;
    rightvt = V_VT(right)&VT_TYPEMASK;
    leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
    rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);

    if (leftExtraFlags != rightExtraFlags)
        return DISP_E_BADVARTYPE;
    ExtraFlags = leftExtraFlags;

    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);

    /* Native VarIdiv always returns a error when using any extra
     * flags or if the variant combination is I8 and INT.
     */
    if ((leftvt == VT_I8 && rightvt == VT_INT) ||
        (leftvt == VT_INT && rightvt == VT_I8) ||
        (rightvt == VT_EMPTY && leftvt != VT_NULL) ||
        ExtraFlags != 0)
        return DISP_E_BADVARTYPE;

    /* Determine variant type */
    else if (leftvt == VT_NULL || rightvt == VT_NULL)
    {
        V_VT(result) = VT_NULL;
        return S_OK;
    }
    else if (leftvt == VT_I8 || rightvt == VT_I8)
        resvt = VT_I8;
    else if (leftvt == VT_I4 || rightvt == VT_I4 ||
        leftvt == VT_INT || rightvt == VT_INT ||
        leftvt == VT_UINT || rightvt == VT_UINT ||
        leftvt == VT_UI8 || rightvt == VT_UI8 ||
        leftvt == VT_UI4 || rightvt == VT_UI4 ||
        leftvt == VT_UI2 || rightvt == VT_UI2 ||
        leftvt == VT_I1 || rightvt == VT_I1 ||
        leftvt == VT_BSTR || rightvt == VT_BSTR ||
        leftvt == VT_DATE || rightvt == VT_DATE ||
        leftvt == VT_CY || rightvt == VT_CY ||
        leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
        leftvt == VT_R8 || rightvt == VT_R8 ||
        leftvt == VT_R4 || rightvt == VT_R4)
        resvt = VT_I4;
    else if (leftvt == VT_I2 || rightvt == VT_I2 ||
        leftvt == VT_BOOL || rightvt == VT_BOOL ||
        leftvt == VT_EMPTY)
        resvt = VT_I2;
    else if (leftvt == VT_UI1 || rightvt == VT_UI1)
        resvt = VT_UI1;
    else
        return DISP_E_BADVARTYPE;

Daniel Remenak's avatar
Daniel Remenak committed
4912 4913 4914
    VariantInit(&lv);
    VariantInit(&rv);

4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928
    /* coerce to the result type */
    hres = VariantChangeType(&lv, left, 0, resvt);
    if (hres != S_OK)
    {
        VariantClear(&lv);
        VariantClear(&rv);
        return hres;
    }
    hres = VariantChangeType(&rv, right, 0, resvt);
    if (hres != S_OK)
    {
        VariantClear(&lv);
        VariantClear(&rv);
        return hres;
Daniel Remenak's avatar
Daniel Remenak committed
4929 4930
    }

4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966
    /* do the math */
    V_VT(result) = resvt;
    switch (resvt)
    {
    case VT_UI1:
    if (V_UI1(&rv) == 0)
    {
        hres = DISP_E_DIVBYZERO;
        V_VT(result) = VT_EMPTY;
    }
    else
        V_UI1(result) = V_UI1(&lv) / V_UI1(&rv);
    break;
    case VT_I2:
    if (V_I2(&rv) == 0)
    {
        hres = DISP_E_DIVBYZERO;
        V_VT(result) = VT_EMPTY;
    }
    else
        V_I2(result) = V_I2(&lv) / V_I2(&rv);
    break;
    case VT_I4:
    if (V_I4(&rv) == 0)
    {
        hres = DISP_E_DIVBYZERO;
        V_VT(result) = VT_EMPTY;
    }
    else
        V_I4(result) = V_I4(&lv) / V_I4(&rv);
    break;
    case VT_I8:
    if (V_I8(&rv) == 0)
    {
        hres = DISP_E_DIVBYZERO;
        V_VT(result) = VT_EMPTY;
Daniel Remenak's avatar
Daniel Remenak committed
4967
    }
4968 4969 4970 4971 4972 4973
    else
        V_I8(result) = V_I8(&lv) / V_I8(&rv);
    break;
    default:
        FIXME("Couldn't integer divide variant types %d,%d\n",
            leftvt,rightvt);
Daniel Remenak's avatar
Daniel Remenak committed
4974 4975
    }

4976 4977 4978 4979
    VariantClear(&lv);
    VariantClear(&rv);

    return hres;
Daniel Remenak's avatar
Daniel Remenak committed
4980 4981
}

4982

4983
/**********************************************************************
Daniel Remenak's avatar
Daniel Remenak committed
4984
 *              VarMod [OLEAUT32.155]
4985
 *
Chris Morgan's avatar
Chris Morgan committed
4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999
 * Perform the modulus operation of the right hand variant on the left
 *
 * PARAMS
 *  left     [I] Left hand variant
 *  right    [I] Right hand variant
 *  result   [O] Destination for converted value
 *
 * RETURNS
 *  Success: S_OK. result contains the remainder.
 *  Failure: An HRESULT error code indicating the error.
 *
 * NOTE:
 *   If an error occurs the type of result will be modified but the value will not be.
 *   Doesn't support arrays or any special flags yet.
5000
 */
Chris Morgan's avatar
Chris Morgan committed
5001
HRESULT WINAPI VarMod(LPVARIANT left, LPVARIANT right, LPVARIANT result)
5002
{
Chris Morgan's avatar
Chris Morgan committed
5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041
    BOOL         lOk        = TRUE;
    BOOL         rOk        = TRUE;
    HRESULT      rc         = E_FAIL;
    int          resT = 0;
    VARIANT      lv,rv;

    VariantInit(&lv);
    VariantInit(&rv);

    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
		  debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);

    /* check for invalid inputs */
    lOk = TRUE;
    switch (V_VT(left) & VT_TYPEMASK) {
    case VT_BOOL :
    case VT_I1   :
    case VT_I2   :
    case VT_I4   :
    case VT_I8   :
    case VT_INT  :
    case VT_UI1  :
    case VT_UI2  :
    case VT_UI4  :
    case VT_UI8  :
    case VT_UINT :
    case VT_R4   :
    case VT_R8   :
    case VT_CY   :
    case VT_EMPTY:
    case VT_DATE :
    case VT_BSTR :
      break;
    case VT_VARIANT:
    case VT_UNKNOWN:
      V_VT(result) = VT_EMPTY;
      return DISP_E_TYPEMISMATCH;
    case VT_DECIMAL:
      V_VT(result) = VT_EMPTY;
5042
      return E_INVALIDARG;
Chris Morgan's avatar
Chris Morgan committed
5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126
    case VT_ERROR:
      return DISP_E_TYPEMISMATCH;
    case VT_RECORD:
      V_VT(result) = VT_EMPTY;
      return DISP_E_TYPEMISMATCH;
    case VT_NULL:
      break;
    default:
      V_VT(result) = VT_EMPTY;
      return DISP_E_BADVARTYPE;
    }


    rOk = TRUE;
    switch (V_VT(right) & VT_TYPEMASK) {
    case VT_BOOL :
    case VT_I1   :
    case VT_I2   :
    case VT_I4   :
    case VT_I8   :
      if((V_VT(left) == VT_INT) && (V_VT(right) == VT_I8))
      {
	V_VT(result) = VT_EMPTY;
	return DISP_E_TYPEMISMATCH;
      }
    case VT_INT  :
      if((V_VT(right) == VT_INT) && (V_VT(left) == VT_I8))
      {
	V_VT(result) = VT_EMPTY;
	return DISP_E_TYPEMISMATCH;
      }
    case VT_UI1  :
    case VT_UI2  :
    case VT_UI4  :
    case VT_UI8  :
    case VT_UINT :
    case VT_R4   :
    case VT_R8   :
    case VT_CY   :
      if(V_VT(left) == VT_EMPTY)
      {
	V_VT(result) = VT_I4;
	return S_OK;
      }
    case VT_EMPTY:
    case VT_DATE :
    case VT_BSTR:
      if(V_VT(left) == VT_NULL)
      {
	V_VT(result) = VT_NULL;
	return S_OK;
      }
      break;

    case VT_VOID:
      V_VT(result) = VT_EMPTY;
      return DISP_E_BADVARTYPE;
    case VT_NULL:
      if(V_VT(left) == VT_VOID)
      {
	V_VT(result) = VT_EMPTY;
	return DISP_E_BADVARTYPE;
      } else if((V_VT(left) == VT_NULL) || (V_VT(left) == VT_EMPTY) || (V_VT(left) == VT_ERROR) ||
		lOk)
      {
        V_VT(result) = VT_NULL;
	return S_OK;
      } else
      {
	V_VT(result) = VT_NULL;
	return DISP_E_BADVARTYPE;
      }
    case VT_VARIANT:
    case VT_UNKNOWN:
      V_VT(result) = VT_EMPTY;
      return DISP_E_TYPEMISMATCH;
    case VT_DECIMAL:
      if(V_VT(left) == VT_ERROR)
      {
	V_VT(result) = VT_EMPTY;
	return DISP_E_TYPEMISMATCH;
      } else
      {
	V_VT(result) = VT_EMPTY;
5127
        return E_INVALIDARG;
Chris Morgan's avatar
Chris Morgan committed
5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162
      }
    case VT_ERROR:
      return DISP_E_TYPEMISMATCH;
    case VT_RECORD:
      if((V_VT(left) == 15) || ((V_VT(left) >= 24) && (V_VT(left) <= 35)) || !lOk)
      {
	V_VT(result) = VT_EMPTY;
	return DISP_E_BADVARTYPE;
      } else
      {
	V_VT(result) = VT_EMPTY;
	return DISP_E_TYPEMISMATCH;
      }
    default:
      V_VT(result) = VT_EMPTY;
      return DISP_E_BADVARTYPE;
    }

    /* determine the result type */
    if((V_VT(left) == VT_I8)        || (V_VT(right) == VT_I8))   resT = VT_I8;
    else if((V_VT(left) == VT_UI1)  && (V_VT(right) == VT_BOOL)) resT = VT_I2;
    else if((V_VT(left) == VT_UI1)  && (V_VT(right) == VT_UI1))  resT = VT_UI1;
    else if((V_VT(left) == VT_UI1)  && (V_VT(right) == VT_I2))   resT = VT_I2;
    else if((V_VT(left) == VT_I2)   && (V_VT(right) == VT_BOOL)) resT = VT_I2;
    else if((V_VT(left) == VT_I2)   && (V_VT(right) == VT_UI1))  resT = VT_I2;
    else if((V_VT(left) == VT_I2)   && (V_VT(right) == VT_I2))   resT = VT_I2;
    else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_BOOL)) resT = VT_I2;
    else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_UI1))  resT = VT_I2;
    else if((V_VT(left) == VT_BOOL) && (V_VT(right) == VT_I2))   resT = VT_I2;
    else resT = VT_I4; /* most outputs are I4 */

    /* convert to I8 for the modulo */
    rc = VariantChangeType(&lv, left, 0, VT_I8);
    if(FAILED(rc))
    {
5163
      FIXME("Could not convert left type %d to %d? rc == 0x%X\n", V_VT(left), VT_I8, rc);
Chris Morgan's avatar
Chris Morgan committed
5164 5165 5166 5167 5168 5169
      return rc;
    }

    rc = VariantChangeType(&rv, right, 0, VT_I8);
    if(FAILED(rc))
    {
5170
      FIXME("Could not convert right type %d to %d? rc == 0x%X\n", V_VT(right), VT_I8, rc);
Chris Morgan's avatar
Chris Morgan committed
5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195
      return rc;
    }

    /* if right is zero set VT_EMPTY and return divide by zero */
    if(V_I8(&rv) == 0)
    {
      V_VT(result) = VT_EMPTY;
      return DISP_E_DIVBYZERO;
    }

    /* perform the modulo operation */
    V_VT(result) = VT_I8;
    V_I8(result) = V_I8(&lv) % V_I8(&rv);

    TRACE("V_I8(left) == %ld, V_I8(right) == %ld, V_I8(result) == %ld\n", (long)V_I8(&lv), (long)V_I8(&rv), (long)V_I8(result));

    /* convert left and right to the destination type */
    rc = VariantChangeType(result, result, 0, resT);
    if(FAILED(rc))
    {
      FIXME("Could not convert 0x%x to %d?\n", V_VT(result), resT);
      return rc;
    }

    return S_OK;
5196
}
5197 5198 5199 5200

/**********************************************************************
 *              VarPow [OLEAUT32.158]
 *
5201 5202 5203 5204 5205 5206 5207 5208 5209 5210
 * Computes the power of one variant to another variant.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
5211 5212 5213 5214 5215
 */
HRESULT WINAPI VarPow(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
    HRESULT hr;
    VARIANT dl,dr;
5216 5217 5218
    VARTYPE resvt = VT_EMPTY;
    VARTYPE leftvt,rightvt;
    VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
5219

5220 5221
    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left), debugstr_VF(left),
          right, debugstr_VT(right), debugstr_VF(right), result);
5222

5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260
    VariantInit(&dl);
    VariantInit(&dr);

    leftvt = V_VT(left)&VT_TYPEMASK;
    rightvt = V_VT(right)&VT_TYPEMASK;
    leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
    rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);

    if (leftExtraFlags != rightExtraFlags)
        return DISP_E_BADVARTYPE;
    ExtraFlags = leftExtraFlags;

    /* Native VarPow always returns a error when using any extra flags */
    if (ExtraFlags != 0)
        return DISP_E_BADVARTYPE;

    /* Determine return type */
    else if (leftvt == VT_NULL || rightvt == VT_NULL) {
        V_VT(result) = VT_NULL;
        return S_OK;
    }
    else if ((leftvt == VT_EMPTY || leftvt == VT_I2 ||
        leftvt == VT_I4 || leftvt == VT_R4 ||
        leftvt == VT_R8 || leftvt == VT_CY ||
        leftvt == VT_DATE || leftvt == VT_BSTR ||
        leftvt == VT_BOOL || leftvt == VT_DECIMAL ||
        (leftvt >= VT_I1 && leftvt <= VT_UINT)) &&
        (rightvt == VT_EMPTY || rightvt == VT_I2 ||
        rightvt == VT_I4 || rightvt == VT_R4 ||
        rightvt == VT_R8 || rightvt == VT_CY ||
        rightvt == VT_DATE || rightvt == VT_BSTR ||
        rightvt == VT_BOOL || rightvt == VT_DECIMAL ||
        (rightvt >= VT_I1 && rightvt <= VT_UINT)))
        resvt = VT_R8;
    else
        return DISP_E_BADVARTYPE;

    hr = VariantChangeType(&dl,left,0,resvt);
5261
    if (!SUCCEEDED(hr)) {
5262
        ERR("Could not change passed left argument to VT_R8, handle it differently.\n");
5263
        VariantClear(&dl);
5264
        return E_FAIL;
5265
    }
5266 5267

    hr = VariantChangeType(&dr,right,0,resvt);
5268
    if (!SUCCEEDED(hr)) {
5269
        ERR("Could not change passed right argument to VT_R8, handle it differently.\n");
5270 5271
        VariantClear(&dl);
        VariantClear(&dr);
5272
        return E_FAIL;
5273
    }
5274

5275 5276
    V_VT(result) = VT_R8;
    V_R8(result) = pow(V_R8(&dl),V_R8(&dr));
5277 5278 5279 5280

    VariantClear(&dl);
    VariantClear(&dr);

5281 5282
    return S_OK;
}
5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509

/**********************************************************************
 *              VarImp [OLEAUT32.154]
 *
 * Bitwise implication of two variants.
 *
 * PARAMS
 *  left    [I] First variant
 *  right   [I] Second variant
 *  result  [O] Result variant
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: An HRESULT error code indicating the error.
 */
HRESULT WINAPI VarImp(LPVARIANT left, LPVARIANT right, LPVARIANT result)
{
    HRESULT hres = S_OK;
    VARTYPE resvt = VT_EMPTY;
    VARTYPE leftvt,rightvt;
    VARTYPE rightExtraFlags,leftExtraFlags,ExtraFlags;
    VARIANT lv,rv;
    double d;

    leftvt = V_VT(left)&VT_TYPEMASK;
    rightvt = V_VT(right)&VT_TYPEMASK;
    leftExtraFlags = V_VT(left)&(~VT_TYPEMASK);
    rightExtraFlags = V_VT(right)&(~VT_TYPEMASK);

    if (leftExtraFlags != rightExtraFlags)
        return DISP_E_BADVARTYPE;
    ExtraFlags = leftExtraFlags;

    TRACE("(%p->(%s%s),%p->(%s%s),%p)\n", left, debugstr_VT(left),
          debugstr_VF(left), right, debugstr_VT(right), debugstr_VF(right), result);

    /* Native VarImp always returns a error when using any extra
     * flags or if the variants are I8 and INT.
     */
    if ((leftvt == VT_I8 && rightvt == VT_INT) ||
        ExtraFlags != 0)
        return DISP_E_BADVARTYPE;

    /* Determine result type */
    else if ((leftvt == VT_NULL && rightvt == VT_NULL) ||
        (leftvt == VT_NULL && rightvt == VT_EMPTY))
    {
        V_VT(result) = VT_NULL;
        return S_OK;
    }
    else if (leftvt == VT_I8 || rightvt == VT_I8)
        resvt = VT_I8;
    else if (leftvt == VT_I4 || rightvt == VT_I4 ||
        leftvt == VT_INT || rightvt == VT_INT ||
        leftvt == VT_UINT || rightvt == VT_UINT ||
        leftvt == VT_UI4 || rightvt == VT_UI4 ||
        leftvt == VT_UI8 || rightvt == VT_UI8 ||
        leftvt == VT_UI2 || rightvt == VT_UI2 ||
        leftvt == VT_DECIMAL || rightvt == VT_DECIMAL ||
        leftvt == VT_DATE || rightvt == VT_DATE ||
        leftvt == VT_CY || rightvt == VT_CY ||
        leftvt == VT_R8 || rightvt == VT_R8 ||
        leftvt == VT_R4 || rightvt == VT_R4 ||
        leftvt == VT_I1 || rightvt == VT_I1)
        resvt = VT_I4;
    else if ((leftvt == VT_UI1 && rightvt == VT_UI1) ||
        (leftvt == VT_UI1 && rightvt == VT_NULL) ||
        (leftvt == VT_NULL && rightvt == VT_UI1))
        resvt = VT_UI1;
    else if (leftvt == VT_EMPTY || rightvt == VT_EMPTY ||
        leftvt == VT_I2 || rightvt == VT_I2 ||
        leftvt == VT_UI1 || rightvt == VT_UI1)
        resvt = VT_I2;
    else if (leftvt == VT_BOOL || rightvt == VT_BOOL ||
        leftvt == VT_BSTR || rightvt == VT_BSTR)
        resvt = VT_BOOL;

    VariantInit(&lv);
    VariantInit(&rv);

    /* VT_NULL requires special handling for when the opposite
     * variant is equal to something other than -1.
     * (NULL Imp 0 = NULL, NULL Imp n = n)
     */
    if (leftvt == VT_NULL)
    {
        VARIANT_BOOL b;
        switch(rightvt)
        {
        case VT_I1:   if (!V_I1(right)) resvt = VT_NULL; break;
        case VT_UI1:  if (!V_UI1(right)) resvt = VT_NULL; break;
        case VT_I2:   if (!V_I2(right)) resvt = VT_NULL; break;
        case VT_UI2:  if (!V_UI2(right)) resvt = VT_NULL; break;
        case VT_I4:   if (!V_I4(right)) resvt = VT_NULL; break;
        case VT_UI4:  if (!V_UI4(right)) resvt = VT_NULL; break;
        case VT_I8:   if (!V_I8(right)) resvt = VT_NULL; break;
        case VT_UI8:  if (!V_UI8(right)) resvt = VT_NULL; break;
        case VT_INT:  if (!V_INT(right)) resvt = VT_NULL; break;
        case VT_UINT: if (!V_UINT(right)) resvt = VT_NULL; break;
        case VT_BOOL: if (!V_BOOL(right)) resvt = VT_NULL; break;
        case VT_R4:   if (!V_R4(right)) resvt = VT_NULL; break;
        case VT_R8:   if (!V_R8(right)) resvt = VT_NULL; break;
        case VT_DATE: if (!V_DATE(right)) resvt = VT_NULL; break;
        case VT_CY:   if (!V_CY(right).int64) resvt = VT_NULL; break;
        case VT_DECIMAL:
            if (!(DEC_HI32(&V_DECIMAL(right)) || DEC_LO64(&V_DECIMAL(right))))
                resvt = VT_NULL;
            break;
        case VT_BSTR:
            hres = VarBoolFromStr(V_BSTR(right),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
            if (FAILED(hres)) goto VarImp_Exit;
            else if (!b)
                V_VT(result) = VT_NULL;
            else
            {
                V_VT(result) = VT_BOOL;
                V_BOOL(result) = b;
            }
            goto VarImp_Exit;
        }
        if (resvt == VT_NULL)
        {
            V_VT(result) = resvt;
            goto VarImp_Exit;
        }
        else
        {
            hres = VariantChangeType(result,right,0,resvt);
            goto VarImp_Exit;
        }
    }

    /* Special handling is required when NULL is the right variant.
     * (-1 Imp NULL = NULL, n Imp NULL = n Imp 0)
     */
    else if (rightvt == VT_NULL)
    {
        VARIANT_BOOL b;
        switch(leftvt)
        {
        case VT_I1:     if (V_I1(left) == -1) resvt = VT_NULL; break;
        case VT_UI1:    if (V_UI1(left) == 0xff) resvt = VT_NULL; break;
        case VT_I2:     if (V_I2(left) == -1) resvt = VT_NULL; break;
        case VT_UI2:    if (V_UI2(left) == 0xffff) resvt = VT_NULL; break;
        case VT_INT:    if (V_INT(left) == -1) resvt = VT_NULL; break;
        case VT_UINT:   if (V_UINT(left) == ~0u) resvt = VT_NULL; break;
        case VT_I4:     if (V_I4(left) == -1) resvt = VT_NULL; break;
        case VT_UI4:    if (V_UI4(left) == ~0u) resvt = VT_NULL; break;
        case VT_I8:     if (V_I8(left) == -1) resvt = VT_NULL; break;
        case VT_UI8:    if (V_UI8(left) == ~(ULONGLONG)0) resvt = VT_NULL; break;
        case VT_BOOL:   if (V_BOOL(left) == VARIANT_TRUE) resvt = VT_NULL; break;
        case VT_R4:     if (V_R4(left) == -1.0) resvt = VT_NULL; break;
        case VT_R8:     if (V_R8(left) == -1.0) resvt = VT_NULL; break;
        case VT_CY:     if (V_CY(left).int64 == -1) resvt = VT_NULL; break;
        case VT_DECIMAL:
            if (DEC_HI32(&V_DECIMAL(left)) == 0xffffffff)
                resvt = VT_NULL;
            break;
        case VT_BSTR:
            hres = VarBoolFromStr(V_BSTR(left),LOCALE_USER_DEFAULT, VAR_LOCALBOOL, &b);
            if (FAILED(hres)) goto VarImp_Exit;
            else if (b == VARIANT_TRUE)
                resvt = VT_NULL;
        }
        if (resvt == VT_NULL)
        {
            V_VT(result) = resvt;
            goto VarImp_Exit;
        }
    }

    hres = VariantCopy(&lv, left);
    if (FAILED(hres)) goto VarImp_Exit;

    if (rightvt == VT_NULL)
        V_VT(&rv) = resvt;
    else
    {
        hres = VariantCopy(&rv, right);
        if (FAILED(hres)) goto VarImp_Exit;
    }

    if (V_VT(&lv) == VT_BSTR &&
        FAILED(VarR8FromStr(V_BSTR(&lv),LOCALE_USER_DEFAULT, 0, &d)))
        hres = VariantChangeType(&lv,&lv,VARIANT_LOCALBOOL, VT_BOOL);
    if (SUCCEEDED(hres) && V_VT(&lv) != resvt)
        hres = VariantChangeType(&lv,&lv,0,resvt);
    if (FAILED(hres)) goto VarImp_Exit;

    if (V_VT(&rv) == VT_BSTR &&
        FAILED(VarR8FromStr(V_BSTR(&rv),LOCALE_USER_DEFAULT, 0, &d)))
        hres = VariantChangeType(&rv, &rv,VARIANT_LOCALBOOL, VT_BOOL);
    if (SUCCEEDED(hres) && V_VT(&rv) != resvt)
        hres = VariantChangeType(&rv, &rv, 0, resvt);
    if (FAILED(hres)) goto VarImp_Exit;

    /* do the math */
    V_VT(result) = resvt;
    switch (resvt)
    {
    case VT_I8:
    V_I8(result) = (~V_I8(&lv)) | V_I8(&rv);
    break;
    case VT_I4:
    V_I4(result) = (~V_I4(&lv)) | V_I4(&rv);
    break;
    case VT_I2:
    V_I2(result) = (~V_I2(&lv)) | V_I2(&rv);
    break;
    case VT_UI1:
    V_UI1(result) = (~V_UI1(&lv)) | V_UI1(&rv);
    break;
    case VT_BOOL:
    V_BOOL(result) = (~V_BOOL(&lv)) | V_BOOL(&rv);
    break;
    default:
    FIXME("Couldn't perform bitwise implication on variant types %d,%d\n",
        leftvt,rightvt);
    }

VarImp_Exit:

    VariantClear(&lv);
    VariantClear(&rv);

    return hres;
}