query.c 26.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright 2012 Hans Leidekker for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS

#include "config.h"
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "wbemcli.h"

#include "wine/debug.h"
#include "wbemprox_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);

33 34 35 36 37 38 39
HRESULT create_view( const struct property *proplist, const WCHAR *class,
                     const struct expr *cond, struct view **ret )
{
    struct view *view = heap_alloc( sizeof(struct view) );

    if (!view) return E_OUTOFMEMORY;
    view->proplist = proplist;
40
    view->table    = grab_table( class );
41 42 43 44 45 46 47 48 49
    view->cond     = cond;
    view->result   = NULL;
    view->count    = 0;
    *ret = view;
    return S_OK;
}

void destroy_view( struct view *view )
{
50
    if (!view) return;
51
    if (view->table) release_table( view->table );
52 53 54 55
    heap_free( view->result );
    heap_free( view );
}

56
static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
57
{
58
    const WCHAR *p = lstr, *q = rstr;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    while (*p && *q)
    {
        if (*q == '%')
        {
            while (*q == '%') q++;
            if (!*q) return TRUE;
            while (*p && toupperW( p[1] ) != toupperW( q[1] )) p++;
            if (!*p) return TRUE;
        }
        if (toupperW( *p++ ) != toupperW( *q++ )) return FALSE;
    }
    return TRUE;
}

74 75
static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
{
76 77 78 79 80
    if (!lstr || !rstr)
    {
        *val = 0;
        return S_OK;
    }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    switch (op)
    {
    case OP_EQ:
        *val = !strcmpW( lstr, rstr );
        break;
    case OP_GT:
        *val = strcmpW( lstr, rstr ) > 0;
        break;
    case OP_LT:
        *val = strcmpW( lstr, rstr ) < 0;
        break;
    case OP_LE:
        *val = strcmpW( lstr, rstr ) <= 0;
        break;
    case OP_GE:
        *val = strcmpW( lstr, rstr ) >= 0;
        break;
    case OP_NE:
        *val = strcmpW( lstr, rstr );
        break;
    case OP_LIKE:
        *val = eval_like( lstr, rstr );
        break;
    default:
        ERR("unhandled operator %u\n", op);
        return WBEM_E_INVALID_QUERY;
    }
    return S_OK;
}

static inline BOOL is_strcmp( const struct complex_expr *expr )
{
    return ((expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) ||
            (expr->left->type == EXPR_SVAL && expr->right->type == EXPR_PROPVAL));
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
static inline BOOL is_boolcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
{
    if (ltype == CIM_BOOLEAN && expr->left->type == EXPR_PROPVAL &&
        (expr->right->type == EXPR_SVAL || expr->right->type == EXPR_BVAL)) return TRUE;
    else if (rtype == CIM_BOOLEAN && expr->right->type == EXPR_PROPVAL &&
             (expr->left->type == EXPR_SVAL || expr->left->type == EXPR_BVAL)) return TRUE;
    return FALSE;
}

static HRESULT eval_boolcmp( UINT op, LONGLONG lval, LONGLONG rval, UINT ltype, UINT rtype, LONGLONG *val )
{
    static const WCHAR trueW[] = {'T','r','u','e',0};

    if (ltype == CIM_STRING) lval = !strcmpiW( (const WCHAR *)(INT_PTR)lval, trueW ) ? -1 : 0;
    else if (rtype == CIM_STRING) rval = !strcmpiW( (const WCHAR *)(INT_PTR)rval, trueW ) ? -1 : 0;

    switch (op)
    {
    case OP_EQ:
        *val = (lval == rval);
        break;
    case OP_NE:
        *val = (lval != rval);
        break;
    default:
        ERR("unhandled operator %u\n", op);
        return WBEM_E_INVALID_QUERY;
    }
    return S_OK;
}

static UINT resolve_type( UINT left, UINT right )
{
    switch (left)
    {
    case CIM_SINT8:
    case CIM_SINT16:
    case CIM_SINT32:
    case CIM_SINT64:
    case CIM_UINT8:
    case CIM_UINT16:
    case CIM_UINT32:
    case CIM_UINT64:
        switch (right)
        {
            case CIM_SINT8:
            case CIM_SINT16:
            case CIM_SINT32:
            case CIM_SINT64:
            case CIM_UINT8:
            case CIM_UINT16:
            case CIM_UINT32:
            case CIM_UINT64:
                return CIM_UINT64;
            default: break;
        }
173
        break;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    case CIM_STRING:
        if (right == CIM_STRING) return CIM_STRING;
        break;

    case CIM_BOOLEAN:
        if (right == CIM_BOOLEAN) return CIM_BOOLEAN;
        break;

    default:
        break;
    }
    return CIM_ILLEGAL;
}

189
static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
190
                            LONGLONG *val, UINT *type )
191 192
{
    HRESULT lret, rret;
193
    LONGLONG lval, rval;
194
    UINT ltype, rtype;
195

196 197
    lret = eval_cond( table, row, expr->left, &lval, &ltype );
    rret = eval_cond( table, row, expr->right, &rval, &rtype );
198 199
    if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;

200 201 202 203 204
    *type = resolve_type( ltype, rtype );

    if (is_boolcmp( expr, ltype, rtype ))
        return eval_boolcmp( expr->op, lval, rval, ltype, rtype, val );

205 206 207 208 209 210 211
    if (is_strcmp( expr ))
    {
        const WCHAR *lstr = (const WCHAR *)(INT_PTR)lval;
        const WCHAR *rstr = (const WCHAR *)(INT_PTR)rval;

        return eval_strcmp( expr->op, lstr, rstr, val );
    }
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    switch (expr->op)
    {
    case OP_EQ:
        *val = (lval == rval);
        break;
    case OP_AND:
        *val = (lval && rval);
        break;
    case OP_OR:
        *val = (lval || rval);
        break;
    case OP_GT:
        *val = (lval > rval);
        break;
    case OP_LT:
        *val = (lval < rval);
        break;
    case OP_LE:
        *val = (lval <= rval);
        break;
    case OP_GE:
        *val = (lval >= rval);
        break;
    case OP_NE:
        *val = (lval != rval);
        break;
    default:
239
        ERR("unhandled operator %u\n", expr->op);
240 241 242 243 244 245
        return WBEM_E_INVALID_QUERY;
    }
    return S_OK;
}

static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
246
                           LONGLONG *val, UINT *type )
247 248 249 250

{
    HRESULT hr;
    UINT column;
251
    LONGLONG lval;
252

253 254 255 256 257 258 259 260 261
    if (expr->op == OP_NOT)
    {
        hr = eval_cond( table, row, expr->left, &lval, type );
        if (hr != S_OK)
            return hr;
        *val = !lval;
        return S_OK;
    }

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    hr = get_column_index( table, expr->left->u.propval->name, &column );
    if (hr != S_OK)
        return hr;

    hr = get_value( table, row, column, &lval );
    if (hr != S_OK)
        return hr;

    switch (expr->op)
    {
    case OP_ISNULL:
        *val = !lval;
        break;
    case OP_NOTNULL:
        *val = lval;
        break;
    default:
        ERR("unknown operator %u\n", expr->op);
        return WBEM_E_INVALID_QUERY;
    }
282 283

    *type = table->columns[column].type & CIM_TYPE_MASK;
284 285 286 287
    return S_OK;
}

static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
288
                             LONGLONG *val, UINT *type )
289 290 291 292 293 294 295 296 297

{
    HRESULT hr;
    UINT column;

    hr = get_column_index( table, propval->name, &column );
    if (hr != S_OK)
        return hr;

298
    *type = table->columns[column].type & CIM_TYPE_MASK;
299 300 301
    return get_value( table, row, column, val );
}

302
HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond, LONGLONG *val, UINT *type )
303 304 305 306
{
    if (!cond)
    {
        *val = 1;
307
        *type = CIM_UINT64;
308 309 310 311 312
        return S_OK;
    }
    switch (cond->type)
    {
    case EXPR_COMPLEX:
313 314
        return eval_binary( table, row, &cond->u.expr, val, type );

315
    case EXPR_UNARY:
316 317
        return eval_unary( table, row, &cond->u.expr, val, type );

318
    case EXPR_PROPVAL:
319 320
        return eval_propval( table, row, cond->u.propval, val, type );

321
    case EXPR_SVAL:
322
        *val = (INT_PTR)cond->u.sval;
323
        *type = CIM_STRING;
324
        return S_OK;
325

326
    case EXPR_IVAL:
327 328 329 330
        *val = cond->u.ival;
        *type = CIM_UINT64;
        return S_OK;

331 332
    case EXPR_BVAL:
        *val = cond->u.ival;
333
        *type = CIM_BOOLEAN;
334
        return S_OK;
335

336 337 338 339 340 341 342
    default:
        ERR("invalid expression type\n");
        break;
    }
    return WBEM_E_INVALID_QUERY;
}

343
HRESULT execute_view( struct view *view )
344 345 346
{
    UINT i, j = 0, len;

347
    if (!view->table) return S_OK;
348 349 350
    if (view->table->fill)
    {
        clear_table( view->table );
351
        view->table->fill( view->table, view->cond );
352
    }
353
    if (!view->table->num_rows) return S_OK;
354 355 356 357 358 359 360

    len = min( view->table->num_rows, 16 );
    if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;

    for (i = 0; i < view->table->num_rows; i++)
    {
        HRESULT hr;
361
        LONGLONG val = 0;
362
        UINT type;
363 364 365 366 367 368 369 370

        if (j >= len)
        {
            UINT *tmp;
            len *= 2;
            if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
            view->result = tmp;
        }
371
        if ((hr = eval_cond( view->table, i, view->cond, &val, &type )) != S_OK) return hr;
372 373 374 375 376 377
        if (val) view->result[j++] = i;
    }
    view->count = j;
    return S_OK;
}

378
struct query *create_query(void)
379 380 381 382 383
{
    struct query *query;

    if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
    list_init( &query->mem );
384
    query->refs = 1;
385 386 387
    return query;
}

388
void free_query( struct query *query )
389 390 391
{
    struct list *mem, *next;

392
    if (!query) return;
393
    destroy_view( query->view );
394
    LIST_FOR_EACH_SAFE( mem, next, &query->mem ) { heap_free( mem ); }
395 396 397
    heap_free( query );
}

398
struct query *addref_query( struct query *query )
399 400
{
    InterlockedIncrement( &query->refs );
401
    return query;
402 403 404 405 406 407 408
}

void release_query( struct query *query )
{
    if (!InterlockedDecrement( &query->refs )) free_query( query );
}

409 410 411 412 413 414
HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
{
    HRESULT hr;
    struct query *query;

    *result = NULL;
415
    if (!(query = create_query())) return E_OUTOFMEMORY;
416
    hr = parse_query( str, &query->view, &query->mem );
417 418 419
    if (hr != S_OK) goto done;
    hr = execute_view( query->view );
    if (hr != S_OK) goto done;
420
    hr = EnumWbemClassObject_create( query, (void **)result );
421 422

done:
423
    release_query( query );
424 425
    return hr;
}
426

427
BOOL is_selected_prop( const struct view *view, const WCHAR *name )
428 429 430 431 432 433 434 435 436 437 438 439
{
    const struct property *prop = view->proplist;

    if (!prop) return TRUE;
    while (prop)
    {
        if (!strcmpiW( prop->name, name )) return TRUE;
        prop = prop->next;
    }
    return FALSE;
}

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
static BOOL is_system_prop( const WCHAR *name )
{
    return (name[0] == '_' && name[1] == '_');
}

static BSTR build_servername( const struct view *view )
{
    WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
    DWORD len = sizeof(server)/sizeof(server[0]);

    if (view->proplist) return NULL;

    if (!(GetComputerNameW( server, &len ))) return NULL;
    for (p = server; *p; p++) *p = toupperW( *p );
    return SysAllocString( server );
}

static BSTR build_classname( const struct view *view )
{
    return SysAllocString( view->table->name );
}

static BSTR build_namespace( const struct view *view )
{
    static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};

    if (view->proplist) return NULL;
    return SysAllocString( cimv2W );
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
{
    static const WCHAR fmtW[] = {'%','s','=','%','s',0};
    UINT i, j, offset, row = view->result[index];
    BSTR *values, ret = NULL;

    if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;

    *len = j = 0;
    for (i = 0; i < view->table->num_cols; i++)
    {
        if (view->table->columns[i].type & COL_FLAG_KEY)
        {
            const WCHAR *name = view->table->columns[i].name;

            values[j] = get_value_bstr( view->table, row, i );
            *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
            j++;
        }
    }
    if ((ret = SysAllocStringLen( NULL, *len )))
    {
        offset = j = 0;
        for (i = 0; i < view->table->num_cols; i++)
        {
            if (view->table->columns[i].type & COL_FLAG_KEY)
            {
                const WCHAR *name = view->table->columns[i].name;

                offset += sprintfW( ret + offset, fmtW, name, values[j] );
                if (j < count - 1) ret[offset++] = ',';
                j++;
            }
        }
    }
    for (i = 0; i < count; i++) SysFreeString( values[i] );
    heap_free( values );
    return ret;
}

static UINT count_key_columns( const struct view *view )
{
    UINT i, num_keys = 0;

    for (i = 0; i < view->table->num_cols; i++)
    {
        if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
    }
    return num_keys;
}

521 522
static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
{
523 524 525 526
    static const WCHAR fmtW[] = {'%','s','.','%','s',0};
    BSTR class, proplist, ret = NULL;
    UINT num_keys, len;

527
    if (view->proplist) return NULL;
528 529 530 531 532 533 534 535 536 537 538 539 540

    if (!(class = build_classname( view ))) return NULL;
    if (!(num_keys = count_key_columns( view ))) return class;
    if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;

    len += strlenW( fmtW ) + SysStringLen( class );
    if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
    sprintfW( ret, fmtW, class, proplist );

done:
    SysFreeString( class );
    SysFreeString( proplist );
    return ret;
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
}

static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
{
    static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
    BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
    UINT len;

    if (view->proplist) return NULL;

    if (!(server = build_servername( view ))) return NULL;
    if (!(namespace = build_namespace( view ))) goto done;
    if (!(relpath = build_relpath( view, index, name ))) goto done;

    len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
    if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
    sprintfW( ret, fmtW, server, namespace, relpath );

done:
    SysFreeString( server );
    SysFreeString( namespace );
    SysFreeString( relpath );
    return ret;
}

566
BOOL is_method( const struct table *table, UINT column )
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
{
    return table->columns[column].type & COL_FLAG_METHOD;
}

static UINT count_properties( const struct view *view )
{
    UINT i, num_props = 0;

    for (i = 0; i < view->table->num_cols; i++)
    {
        if (!is_method( view->table, i)) num_props++;
    }
    return num_props;
}

static UINT count_selected_properties( const struct view *view )
583 584 585 586
{
    const struct property *prop = view->proplist;
    UINT count;

587
    if (!prop) return count_properties( view );
588 589 590 591 592 593 594

    count = 1;
    while ((prop = prop->next)) count++;
    return count;
}

static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
595
                                   VARIANT *ret, CIMTYPE *type, LONG *flavor )
596 597 598 599 600 601 602 603 604
{
    static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
    static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
    static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
    static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
    static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
    static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
    static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};

605 606
    if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;

607 608 609 610 611 612 613 614 615
    if (!strcmpiW( name, classW ))
    {
        V_VT( ret ) = VT_BSTR;
        V_BSTR( ret ) = build_classname( view );
        if (type) *type = CIM_STRING;
        return S_OK;
    }
    if (!strcmpiW( name, genusW ))
    {
616 617
        V_VT( ret ) = VT_I4;
        V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
        if (type) *type = CIM_SINT32;
        return S_OK;
    }
    else if (!strcmpiW( name, namespaceW ))
    {
        V_VT( ret ) = VT_BSTR;
        V_BSTR( ret ) = build_namespace( view );
        if (type) *type = CIM_STRING;
        return S_OK;
    }
    else if (!strcmpiW( name, pathW ))
    {
        V_VT( ret ) = VT_BSTR;
        V_BSTR( ret ) = build_path( view, index, name );
        if (type) *type = CIM_STRING;
        return S_OK;
    }
    if (!strcmpiW( name, propcountW ))
    {
637
        V_VT( ret ) = VT_I4;
638
        V_I4( ret ) = count_selected_properties( view );
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        if (type) *type = CIM_SINT32;
        return S_OK;
    }
    else if (!strcmpiW( name, relpathW ))
    {
        V_VT( ret ) = VT_BSTR;
        V_BSTR( ret ) = build_relpath( view, index, name );
        if (type) *type = CIM_STRING;
        return S_OK;
    }
    else if (!strcmpiW( name, serverW ))
    {
        V_VT( ret ) = VT_BSTR;
        V_BSTR( ret ) = build_servername( view );
        if (type) *type = CIM_STRING;
        return S_OK;
    }
    FIXME("system property %s not implemented\n", debugstr_w(name));
    return WBEM_E_NOT_FOUND;
}

660
VARTYPE to_vartype( CIMTYPE type )
661
{
662 663 664 665 666
    switch (type)
    {
    case CIM_BOOLEAN:  return VT_BOOL;
    case CIM_STRING:
    case CIM_DATETIME: return VT_BSTR;
667 668
    case CIM_SINT8:    return VT_I1;
    case CIM_UINT8:    return VT_UI1;
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
    case CIM_SINT16:   return VT_I2;
    case CIM_UINT16:   return VT_UI2;
    case CIM_SINT32:   return VT_I4;
    case CIM_UINT32:   return VT_UI4;
    case CIM_SINT64:   return VT_I8;
    case CIM_UINT64:   return VT_UI8;
    default:
        ERR("unhandled type %u\n", type);
        break;
    }
    return 0;
}

SAFEARRAY *to_safearray( const struct array *array, CIMTYPE type )
{
    SAFEARRAY *ret;
    UINT size = get_type_size( type );
    VARTYPE vartype = to_vartype( type );
    LONG i;

689
    if (!array || !(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

    for (i = 0; i < array->count; i++)
    {
        void *ptr = (char *)array->ptr + i * size;
        if (vartype == VT_BSTR)
        {
            BSTR str = SysAllocString( *(const WCHAR **)ptr );
            if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
            {
                SysFreeString( str );
                SafeArrayDestroy( ret );
                return NULL;
            }
        }
        else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
        {
            SafeArrayDestroy( ret );
            return NULL;
        }
    }
    return ret;
}

713
void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
714 715 716 717 718 719 720 721
{
    if (type & VT_ARRAY)
    {
        V_VT( ret ) = type;
        V_ARRAY( ret ) = val_ptr;
        return;
    }
    switch (type)
722
    {
723 724
    case VT_BOOL:
        V_BOOL( ret ) = val;
725
        break;
726
    case VT_BSTR:
727 728
        V_BSTR( ret ) = val_ptr;
        break;
729 730 731 732 733 734
    case VT_I1:
        V_I1( ret ) = val;
        break;
    case VT_UI1:
        V_UI1( ret ) = val;
        break;
735 736
    case VT_I2:
        V_I2( ret ) = val;
737
        break;
738 739
    case VT_UI2:
        V_UI2( ret ) = val;
740
        break;
741 742
    case VT_I4:
        V_I4( ret ) = val;
743
        break;
744 745
    case VT_UI4:
        V_UI4( ret ) = val;
746
        break;
747
    case VT_NULL:
748
        break;
749
    default:
750
        ERR("unhandled variant type %u\n", type);
751 752
        return;
    }
753
    V_VT( ret ) = type;
754 755
}

756 757
HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
                     CIMTYPE *type, LONG *flavor )
758 759
{
    HRESULT hr;
760
    UINT column, row;
761
    VARTYPE vartype;
762
    void *val_ptr = NULL;
763
    LONGLONG val;
764

765
    if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
766
    if (!view->count || !is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
767 768

    hr = get_column_index( view->table, name, &column );
769
    if (hr != S_OK || is_method( view->table, column )) return WBEM_E_NOT_FOUND;
770

771
    row = view->result[index];
772 773 774
    hr = get_value( view->table, row, column, &val );
    if (hr != S_OK) return hr;

775
    vartype = view->table->columns[column].vartype;
776 777 778 779 780 781 782 783
    if (view->table->columns[column].type & CIM_FLAG_ARRAY)
    {
        CIMTYPE basetype = view->table->columns[column].type & CIM_TYPE_MASK;

        val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
        if (!vartype) vartype = to_vartype( basetype ) | VT_ARRAY;
        goto done;
    }
784
    switch (view->table->columns[column].type & COL_TYPE_MASK)
785
    {
786 787 788
    case CIM_BOOLEAN:
        if (!vartype) vartype = VT_BOOL;
        break;
789 790
    case CIM_STRING:
    case CIM_DATETIME:
791 792 793
        if (val)
        {
            vartype = VT_BSTR;
794
            val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
795 796 797
        }
        else
            vartype = VT_NULL;
798
        break;
799 800 801 802 803 804
    case CIM_SINT8:
        if (!vartype) vartype = VT_I1;
        break;
    case CIM_UINT8:
        if (!vartype) vartype = VT_UI1;
        break;
805
    case CIM_SINT16:
806
        if (!vartype) vartype = VT_I2;
807 808
        break;
    case CIM_UINT16:
809
        if (!vartype) vartype = VT_UI2;
810 811
        break;
    case CIM_SINT32:
812
        if (!vartype) vartype = VT_I4;
813 814
        break;
    case CIM_UINT32:
815
        if (!vartype) vartype = VT_UI4;
816
        break;
817
    case CIM_SINT64:
818
        vartype = VT_BSTR;
819
        val_ptr = get_value_bstr( view->table, row, column );
820 821
        break;
    case CIM_UINT64:
822
        vartype = VT_BSTR;
823
        val_ptr = get_value_bstr( view->table, row, column );
824
        break;
825 826 827 828
    default:
        ERR("unhandled column type %u\n", view->table->columns[column].type);
        return WBEM_E_FAILED;
    }
829 830 831

done:
    set_variant( vartype, val, val_ptr, ret );
832
    if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
833
    if (flavor) *flavor = 0;
834 835
    return S_OK;
}
836

837 838 839 840 841 842
static CIMTYPE to_cimtype( VARTYPE type )
{
    switch (type)
    {
    case VT_BOOL:  return CIM_BOOLEAN;
    case VT_BSTR:  return CIM_STRING;
843 844
    case VT_I1:    return CIM_SINT8;
    case VT_UI1:   return CIM_UINT8;
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
    case VT_I2:    return CIM_SINT16;
    case VT_UI2:   return CIM_UINT16;
    case VT_I4:    return CIM_SINT32;
    case VT_UI4:   return CIM_UINT32;
    case VT_I8:    return CIM_SINT64;
    case VT_UI8:   return CIM_UINT64;
    default:
        ERR("unhandled type %u\n", type);
        break;
    }
    return 0;
}

static struct array *to_array( VARIANT *var, CIMTYPE *type )
{
    struct array *ret;
    LONG bound, i;
    VARTYPE vartype;
    CIMTYPE basetype;
    UINT size;

    if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
    if (!(basetype = to_cimtype( vartype ))) return NULL;
    if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
    if (!(ret = heap_alloc( sizeof(struct array) ))) return NULL;

    ret->count = bound + 1;
    size = get_type_size( basetype );
873
    if (!(ret->ptr = heap_alloc_zero( ret->count * size )))
874 875 876 877 878 879
    {
        heap_free( ret );
        return NULL;
    }
    for (i = 0; i < ret->count; i++)
    {
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
        void *ptr = (char *)ret->ptr + i * size;
        if (vartype == VT_BSTR)
        {
            BSTR str;
            if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
            {
                destroy_array( ret, basetype );
                return NULL;
            }
            *(WCHAR **)ptr = heap_strdupW( str );
            SysFreeString( str );
            if (!*(WCHAR **)ptr)
            {
                destroy_array( ret, basetype );
                return NULL;
            }
        }
        else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
898
        {
899
            destroy_array( ret, basetype );
900 901 902 903 904 905 906 907
            return NULL;
        }
    }
    *type = basetype | CIM_FLAG_ARRAY;
    return ret;
}

HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
908 909 910 911 912 913
{
    if (!var)
    {
        *val = 0;
        return S_OK;
    }
914 915 916 917 918 919
    if (V_VT( var ) & VT_ARRAY)
    {
        *val = (INT_PTR)to_array( var, type );
        if (!*val) return E_OUTOFMEMORY;
        return S_OK;
    }
920 921
    switch (V_VT( var ))
    {
922 923 924 925
    case VT_BOOL:
        *val = V_BOOL( var );
        *type = CIM_BOOLEAN;
        break;
926
    case VT_BSTR:
927
        *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
        if (!*val) return E_OUTOFMEMORY;
        *type = CIM_STRING;
        break;
    case VT_I2:
        *val = V_I2( var );
        *type = CIM_SINT16;
        break;
    case VT_UI2:
        *val = V_UI2( var );
        *type = CIM_UINT16;
        break;
    case VT_I4:
        *val = V_I4( var );
        *type = CIM_SINT32;
        break;
    case VT_UI4:
        *val = V_UI4( var );
        *type = CIM_UINT32;
        break;
    case VT_NULL:
        *val = 0;
        break;
    default:
        ERR("unhandled type %u\n", V_VT( var ));
        return WBEM_E_FAILED;
    }
    return S_OK;
}

HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
{
    HRESULT hr;
    UINT column, row = view->result[index];
    LONGLONG val;

    hr = get_column_index( view->table, name, &column );
    if (hr != S_OK)
    {
        FIXME("no support for creating new properties\n");
        return WBEM_E_FAILED;
    }
    if (is_method( view->table, column ) || !(view->table->columns[column].type & COL_FLAG_DYNAMIC))
        return WBEM_E_FAILED;

972
    hr = to_longlong( var, &val, &type );
973 974 975 976 977
    if (hr != S_OK) return hr;

    return set_value( view->table, row, column, val, type );
}

978
HRESULT get_properties( const struct view *view, LONG flags, SAFEARRAY **props )
979 980 981
{
    SAFEARRAY *sa;
    BSTR str;
982 983
    UINT i, num_props = count_selected_properties( view );
    LONG j;
984

985
    if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
986

987
    for (i = 0, j = 0; i < view->table->num_cols; i++)
988
    {
989 990
        BOOL is_system;

991
        if (is_method( view->table, i )) continue;
992
        if (!is_selected_prop( view, view->table->columns[i].name )) continue;
993

994 995 996 997
        is_system = is_system_prop( view->table->columns[i].name );
        if ((flags & WBEM_FLAG_NONSYSTEM_ONLY) && is_system) continue;
        else if ((flags & WBEM_FLAG_SYSTEM_ONLY) && !is_system) continue;

998
        str = SysAllocString( view->table->columns[i].name );
999
        if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1000 1001 1002 1003 1004
        {
            SysFreeString( str );
            SafeArrayDestroy( sa );
            return E_OUTOFMEMORY;
        }
1005
        j++;
1006 1007 1008 1009
    }
    *props = sa;
    return S_OK;
}