query.c 37.8 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
/*
 * 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 <stdarg.h>

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

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

WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);

32 33 34 35 36 37 38 39 40 41
static HRESULT append_table( struct view *view, struct table *table )
{
    struct table **tmp;
    if (!(tmp = heap_realloc( view->table, (view->table_count + 1) * sizeof(*tmp) ))) return E_OUTOFMEMORY;
    view->table = tmp;
    view->table[view->table_count++] = table;
    return S_OK;
}

HRESULT create_view( enum view_type type, const WCHAR *path, const struct keyword *keywordlist, const WCHAR *class,
42
                     const struct property *proplist, const struct expr *cond, struct view **ret )
43
{
44
    struct view *view = heap_alloc_zero( sizeof(*view) );
45 46

    if (!view) return E_OUTOFMEMORY;
47 48 49 50 51 52 53 54 55 56 57 58 59

    switch (type)
    {
    case VIEW_TYPE_ASSOCIATORS:
        view->path        = path;
        view->keywordlist = keywordlist;
        break;

    case VIEW_TYPE_SELECT:
    {
        struct table *table = grab_table( class );
        HRESULT hr;

60 61 62 63 64
        if (table && (hr = append_table( view, table )) != S_OK)
        {
            heap_free( view );
            return hr;
        }
65 66 67 68 69 70 71 72 73 74 75
        view->proplist = proplist;
        view->cond     = cond;
        break;
    }
    default:
        ERR( "unhandled type %u\n", type );
        heap_free( view );
        return E_INVALIDARG;
    }

    view->type = type;
76 77 78 79 80 81
    *ret = view;
    return S_OK;
}

void destroy_view( struct view *view )
{
82
    ULONG i;
83
    if (!view) return;
84 85
    for (i = 0; i < view->table_count; i++) release_table( view->table[i] );
    heap_free( view->table );
86 87 88 89
    heap_free( view->result );
    heap_free( view );
}

90
static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
91
{
92
    const WCHAR *p = lstr, *q = rstr;
93 94 95 96 97 98 99

    while (*p && *q)
    {
        if (*q == '%')
        {
            while (*q == '%') q++;
            if (!*q) return TRUE;
100
            while (*p && *q && towupper( *p ) == towupper( *q )) { p++; q++; };
101
            if (!*p && !*q) return TRUE;
102
        }
103
        if (*q != '%' && towupper( *p++ ) != towupper( *q++ )) return FALSE;
104 105 106 107
    }
    return TRUE;
}

108 109
static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
{
110 111 112 113 114
    if (!lstr || !rstr)
    {
        *val = 0;
        return S_OK;
    }
115 116 117
    switch (op)
    {
    case OP_EQ:
118
        *val = !wcscmp( lstr, rstr );
119 120
        break;
    case OP_GT:
121
        *val = wcscmp( lstr, rstr ) > 0;
122 123
        break;
    case OP_LT:
124
        *val = wcscmp( lstr, rstr ) < 0;
125 126
        break;
    case OP_LE:
127
        *val = wcscmp( lstr, rstr ) <= 0;
128 129
        break;
    case OP_GE:
130
        *val = wcscmp( lstr, rstr ) >= 0;
131 132
        break;
    case OP_NE:
133
        *val = wcscmp( lstr, rstr );
134 135 136 137 138 139 140 141 142 143 144
        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;
}

145
static BOOL is_int( CIMTYPE type )
146
{
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    switch (type)
    {
    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 TRUE;
    default:
        return FALSE;
    }
}

static inline BOOL is_strcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
{
    if ((ltype == CIM_STRING || is_int( ltype )) && expr->left->type == EXPR_PROPVAL &&
        expr->right->type == EXPR_SVAL) return TRUE;
    else if ((rtype == CIM_STRING || is_int( rtype )) && expr->right->type == EXPR_PROPVAL &&
             expr->left->type == EXPR_SVAL) return TRUE;
    return FALSE;
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184
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};

185 186
    if (ltype == CIM_STRING) lval = !wcsicmp( (const WCHAR *)(INT_PTR)lval, trueW ) ? -1 : 0;
    else if (rtype == CIM_STRING) rval = !wcsicmp( (const WCHAR *)(INT_PTR)rval, trueW ) ? -1 : 0;
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    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;
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static inline BOOL is_refcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
{
    if (ltype == CIM_REFERENCE && expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) return TRUE;
    else if (rtype == CIM_REFERENCE && expr->right->type == EXPR_PROPVAL && expr->left->type == EXPR_SVAL) return TRUE;
    return FALSE;
}

static HRESULT eval_refcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
{
    if (!lstr || !rstr)
    {
        *val = 0;
        return S_OK;
    }
    switch (op)
    {
    case OP_EQ:
        *val = !wcsicmp( lstr, rstr );
        break;
    case OP_NE:
        *val = wcsicmp( lstr, rstr );
        break;
    default:
        ERR("unhandled operator %u\n", op);
        return WBEM_E_INVALID_QUERY;
    }
    return S_OK;
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
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;
        }
257
        break;
258 259 260 261 262 263 264 265 266

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

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

267 268 269 270
    case CIM_REFERENCE:
        if (right == CIM_REFERENCE) return CIM_REFERENCE;
        break;

271 272 273 274 275 276
    default:
        break;
    }
    return CIM_ILLEGAL;
}

277
static const WCHAR *format_int( WCHAR *buf, UINT len, CIMTYPE type, LONGLONG val )
278 279 280 281 282 283 284 285 286 287 288
{
    static const WCHAR fmt_signedW[] = {'%','d',0};
    static const WCHAR fmt_unsignedW[] = {'%','u',0};
    static const WCHAR fmt_signed64W[] = {'%','I','6','4','d',0};
    static const WCHAR fmt_unsigned64W[] = {'%','I','6','4','u',0};

    switch (type)
    {
    case CIM_SINT8:
    case CIM_SINT16:
    case CIM_SINT32:
289
        swprintf( buf, len, fmt_signedW, val );
290 291 292 293 294
        return buf;

    case CIM_UINT8:
    case CIM_UINT16:
    case CIM_UINT32:
295
        swprintf( buf, len, fmt_unsignedW, val );
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        return buf;

    case CIM_SINT64:
        wsprintfW( buf, fmt_signed64W, val );
        return buf;

    case CIM_UINT64:
        wsprintfW( buf, fmt_unsigned64W, val );
        return buf;

    default:
        ERR( "unhandled type %u\n", type );
        return NULL;
    }
}

312
static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
313
                            LONGLONG *val, UINT *type )
314 315
{
    HRESULT lret, rret;
316
    LONGLONG lval, rval;
317
    UINT ltype, rtype;
318

319 320
    lret = eval_cond( table, row, expr->left, &lval, &ltype );
    rret = eval_cond( table, row, expr->right, &rval, &rtype );
321 322
    if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;

323 324
    *type = resolve_type( ltype, rtype );

325
    if (is_strcmp( expr, ltype, rtype ))
326
    {
327 328 329
        const WCHAR *lstr, *rstr;
        WCHAR lbuf[21], rbuf[21];

330
        if (is_int( ltype )) lstr = format_int( lbuf, ARRAY_SIZE( lbuf ), ltype, lval );
331 332
        else lstr = (const WCHAR *)(INT_PTR)lval;

333
        if (is_int( rtype )) rstr = format_int( rbuf, ARRAY_SIZE( rbuf ), rtype, rval );
334
        else rstr = (const WCHAR *)(INT_PTR)rval;
335 336 337

        return eval_strcmp( expr->op, lstr, rstr, val );
    }
338 339 340 341 342 343 344 345 346
    if (is_boolcmp( expr, ltype, rtype ))
    {
        return eval_boolcmp( expr->op, lval, rval, ltype, rtype, val );
    }
    if (is_refcmp( expr, ltype, rtype ))
    {
        return eval_refcmp( expr->op, (const WCHAR *)(INT_PTR)lval, (const WCHAR *)(INT_PTR)rval, val );
    }

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
    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:
374
        ERR("unhandled operator %u\n", expr->op);
375 376 377 378 379 380
        return WBEM_E_INVALID_QUERY;
    }
    return S_OK;
}

static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
381
                           LONGLONG *val, UINT *type )
382 383 384 385

{
    HRESULT hr;
    UINT column;
386
    LONGLONG lval;
387

388 389 390 391 392 393 394 395 396
    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;
    }

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    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;
    }
417 418

    *type = table->columns[column].type & CIM_TYPE_MASK;
419 420 421 422
    return S_OK;
}

static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
423
                             LONGLONG *val, UINT *type )
424 425 426 427 428 429 430 431 432

{
    HRESULT hr;
    UINT column;

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

433
    *type = table->columns[column].type & CIM_TYPE_MASK;
434 435 436
    return get_value( table, row, column, val );
}

437
HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond, LONGLONG *val, UINT *type )
438 439 440 441
{
    if (!cond)
    {
        *val = 1;
442
        *type = CIM_UINT64;
443 444 445 446 447
        return S_OK;
    }
    switch (cond->type)
    {
    case EXPR_COMPLEX:
448 449
        return eval_binary( table, row, &cond->u.expr, val, type );

450
    case EXPR_UNARY:
451 452
        return eval_unary( table, row, &cond->u.expr, val, type );

453
    case EXPR_PROPVAL:
454 455
        return eval_propval( table, row, cond->u.propval, val, type );

456
    case EXPR_SVAL:
457
        *val = (INT_PTR)cond->u.sval;
458
        *type = CIM_STRING;
459
        return S_OK;
460

461
    case EXPR_IVAL:
462 463 464 465
        *val = cond->u.ival;
        *type = CIM_UINT64;
        return S_OK;

466 467
    case EXPR_BVAL:
        *val = cond->u.ival;
468
        *type = CIM_BOOLEAN;
469
        return S_OK;
470

471 472 473 474 475 476 477
    default:
        ERR("invalid expression type\n");
        break;
    }
    return WBEM_E_INVALID_QUERY;
}

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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
static WCHAR *build_assoc_query( const WCHAR *class, UINT class_len )
{
    static const WCHAR fmtW[] =
        {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','_','_','A','S','S','O','C','I','A','T','O','R','S',
         ' ','W','H','E','R','E',' ','C','l','a','s','s','=','\'','%','s','\'',0};
    UINT len = class_len + ARRAY_SIZE(fmtW);
    WCHAR *ret;

    if (!(ret = heap_alloc( len * sizeof(WCHAR) ))) return NULL;
    swprintf( ret, len, fmtW, class );
    return ret;
}

static HRESULT create_assoc_enum( const WCHAR *class, UINT class_len, IEnumWbemClassObject **iter )
{
    WCHAR *query;
    HRESULT hr;

    if (!(query = build_assoc_query( class, class_len ))) return E_OUTOFMEMORY;
    hr = exec_query( query, iter );
    heap_free( query );
    return hr;
}

static WCHAR *build_antecedent_query( const WCHAR *assocclass, const WCHAR *dependent )
{
    static const WCHAR fmtW[] =
        {'S','E','L','E','C','T',' ','A','n','t','e','c','e','d','e','n','t',' ','F','R','O','M',' ','%','s',' ',
         'W','H','E','R','E',' ','D','e','p','e','n','d','e','n','t','=','\'','%','s','\'',0};
    UINT len = lstrlenW(assocclass) + lstrlenW(dependent) + ARRAY_SIZE(fmtW);
    WCHAR *ret;

    if (!(ret = heap_alloc( len * sizeof(WCHAR) ))) return NULL;
    swprintf( ret, len, fmtW, assocclass, dependent );
    return ret;
}

static BSTR build_servername(void)
{
    WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
    DWORD len = ARRAY_SIZE( server );

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

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

static WCHAR *build_canonical_path( const WCHAR *relpath )
{
    static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':',0};
    BSTR server, namespace;
    WCHAR *ret;
    UINT len, i;

    if (!(server = build_servername())) return NULL;
    if (!(namespace = build_namespace()))
    {
        SysFreeString( server );
        return NULL;
    }

    len = ARRAY_SIZE( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + lstrlenW( relpath );
    if ((ret = heap_alloc( len * sizeof(WCHAR ) )))
    {
        len = swprintf( ret, len, fmtW, server, namespace );
        for (i = 0; i < lstrlenW( relpath ); i ++)
        {
            if (relpath[i] == '\'') ret[len++] = '"';
            else ret[len++] = relpath[i];
        }
        ret[len] = 0;
    }

    SysFreeString( server );
    SysFreeString( namespace );
    return ret;
}

static HRESULT get_antecedent( const WCHAR *assocclass, const WCHAR *dependent, BSTR *ret )
{
    static const WCHAR antecedentW[] = {'A','n','t','e','c','e','d','e','n','t',0};
    WCHAR *fullpath, *str;
    IEnumWbemClassObject *iter = NULL;
    IWbemClassObject *obj;
    HRESULT hr = E_OUTOFMEMORY;
    ULONG count;
    VARIANT var;

    if (!(fullpath = build_canonical_path( dependent ))) return E_OUTOFMEMORY;
    if (!(str = build_antecedent_query( assocclass, fullpath ))) goto done;
    if ((hr = exec_query( str, &iter )) != S_OK) goto done;

    IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
    if (!count)
    {
        *ret = NULL;
        goto done;
    }

    hr = IWbemClassObject_Get( obj, antecedentW, 0, &var, NULL, NULL );
    IWbemClassObject_Release( obj );
    if (hr != S_OK) goto done;
    *ret = V_BSTR( &var );

done:
    if (iter) IEnumWbemClassObject_Release( iter );
    heap_free( str );
    heap_free( fullpath );
    return hr;
}

static HRESULT do_query( const WCHAR *str, struct query **ret_query )
{
    struct query *query;
    HRESULT hr;

    if (!(query = create_query())) return E_OUTOFMEMORY;
    if ((hr = parse_query( str, &query->view, &query->mem )) != S_OK || (hr = execute_view( query->view )) != S_OK)
    {
        release_query( query );
        return hr;
    }
    *ret_query = query;
    return S_OK;
}

static HRESULT get_antecedent_table( const WCHAR *assocclass, const WCHAR *dependent, struct table **table )
{
    BSTR antecedent = NULL;
    struct path *path = NULL;
    WCHAR *str = NULL;
    struct query *query = NULL;
    HRESULT hr;

    if ((hr = get_antecedent( assocclass, dependent, &antecedent )) != S_OK) return hr;
    if (!antecedent)
    {
        *table = NULL;
        return S_OK;
    }
    if ((hr = parse_path( antecedent, &path )) != S_OK) goto done;
    if (!(str = query_from_path( path )))
    {
        hr = E_OUTOFMEMORY;
        goto done;
    }

    if ((hr = do_query( str, &query )) != S_OK) goto done;
    if (query->view->table_count) *table = addref_table( query->view->table[0] );
    else *table = NULL;

done:
    if (query) release_query( query );
    free_path( path );
    SysFreeString( antecedent );
639
    heap_free( str );
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 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
    return hr;
}

static HRESULT exec_assoc_view( struct view *view )
{
    static const WCHAR assocclassW[] = {'A','s','s','o','c','C','l','a','s','s',0};
    IEnumWbemClassObject *iter = NULL;
    struct path *path;
    HRESULT hr;

    if (view->keywordlist) FIXME( "ignoring keywords\n" );
    if ((hr = parse_path( view->path, &path )) != S_OK) return hr;

    if ((hr = create_assoc_enum( path->class, path->class_len, &iter )) != S_OK) goto done;
    for (;;)
    {
        ULONG count;
        IWbemClassObject *obj;
        struct table *table;
        VARIANT var;

        IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
        if (!count) break;

        if ((hr = IWbemClassObject_Get( obj, assocclassW, 0, &var, NULL, NULL )) != S_OK)
        {
            IWbemClassObject_Release( obj );
            goto done;
        }
        IWbemClassObject_Release( obj );

        hr = get_antecedent_table( V_BSTR(&var), view->path, &table );
        VariantClear( &var );
        if (hr != S_OK) goto done;

        if (table && (hr = append_table( view, table )) != S_OK)
        {
            release_table( table );
            goto done;
        }
    }

    if (view->table_count)
    {
        if (!(view->result = heap_alloc_zero( view->table_count * sizeof(UINT) ))) hr = E_OUTOFMEMORY;
        else view->result_count = view->table_count;
    }

done:
    if (iter) IEnumWbemClassObject_Release( iter );
    free_path( path );
    return hr;
}

static HRESULT exec_select_view( struct view *view )
695 696
{
    UINT i, j = 0, len;
697
    enum fill_status status = FILL_STATUS_UNFILTERED;
698
    struct table *table;
699

700 701 702 703
    if (!view->table_count) return S_OK;

    table = view->table[0];
    if (table->fill)
704
    {
705 706
        clear_table( table );
        status = table->fill( table, view->cond );
707
    }
708
    if (status == FILL_STATUS_FAILED) return WBEM_E_FAILED;
709
    if (!table->num_rows) return S_OK;
710

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

714
    for (i = 0; i < table->num_rows; i++)
715 716
    {
        HRESULT hr;
717
        LONGLONG val = 0;
718
        UINT type;
719 720 721 722 723 724 725 726

        if (j >= len)
        {
            UINT *tmp;
            len *= 2;
            if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
            view->result = tmp;
        }
727
        if (status == FILL_STATUS_FILTERED) val = 1;
728
        else if ((hr = eval_cond( table, i, view->cond, &val, &type )) != S_OK) return hr;
729 730
        if (val) view->result[j++] = i;
    }
731 732

    view->result_count = j;
733 734 735
    return S_OK;
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
HRESULT execute_view( struct view *view )
{
    switch (view->type)
    {
    case VIEW_TYPE_ASSOCIATORS:
        return exec_assoc_view( view );

    case VIEW_TYPE_SELECT:
        return exec_select_view( view );

    default:
        ERR( "unhandled type %u\n", view->type );
        return E_INVALIDARG;
    }
}

752
struct query *create_query(void)
753 754 755 756 757
{
    struct query *query;

    if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
    list_init( &query->mem );
758
    query->refs = 1;
759 760 761
    return query;
}

762
void free_query( struct query *query )
763 764 765
{
    struct list *mem, *next;

766
    if (!query) return;
767
    destroy_view( query->view );
768
    LIST_FOR_EACH_SAFE( mem, next, &query->mem ) { heap_free( mem ); }
769 770 771
    heap_free( query );
}

772
struct query *addref_query( struct query *query )
773 774
{
    InterlockedIncrement( &query->refs );
775
    return query;
776 777 778 779 780 781 782
}

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

783 784 785 786 787 788
HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
{
    HRESULT hr;
    struct query *query;

    *result = NULL;
789
    if (!(query = create_query())) return E_OUTOFMEMORY;
790
    hr = parse_query( str, &query->view, &query->mem );
791 792 793
    if (hr != S_OK) goto done;
    hr = execute_view( query->view );
    if (hr != S_OK) goto done;
794
    hr = EnumWbemClassObject_create( query, (void **)result );
795 796

done:
797
    release_query( query );
798 799
    return hr;
}
800

801
BOOL is_result_prop( const struct view *view, const WCHAR *name )
802 803 804 805 806
{
    const struct property *prop = view->proplist;
    if (!prop) return TRUE;
    while (prop)
    {
807
        if (!wcsicmp( prop->name, name )) return TRUE;
808 809 810 811 812
        prop = prop->next;
    }
    return FALSE;
}

813 814 815 816 817
static BOOL is_system_prop( const WCHAR *name )
{
    return (name[0] == '_' && name[1] == '_');
}

818
static BSTR build_proplist( const struct table *table, UINT row, UINT count, UINT *len )
819 820
{
    static const WCHAR fmtW[] = {'%','s','=','%','s',0};
821
    UINT i, j, offset;
822 823 824 825 826
    BSTR *values, ret = NULL;

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

    *len = j = 0;
827
    for (i = 0; i < table->num_cols; i++)
828
    {
829
        if (table->columns[i].type & COL_FLAG_KEY)
830
        {
831 832
            const WCHAR *name = table->columns[i].name;
            values[j] = get_value_bstr( table, row, i );
833
            *len += lstrlenW( fmtW ) + lstrlenW( name ) + lstrlenW( values[j] );
834 835 836 837 838 839
            j++;
        }
    }
    if ((ret = SysAllocStringLen( NULL, *len )))
    {
        offset = j = 0;
840
        for (i = 0; i < table->num_cols; i++)
841
        {
842
            if (table->columns[i].type & COL_FLAG_KEY)
843
            {
844
                const WCHAR *name = table->columns[i].name;
845
                offset += swprintf( ret + offset, *len - offset, fmtW, name, values[j] );
846 847 848 849 850 851 852 853 854 855
                if (j < count - 1) ret[offset++] = ',';
                j++;
            }
        }
    }
    for (i = 0; i < count; i++) SysFreeString( values[i] );
    heap_free( values );
    return ret;
}

856
static UINT count_key_columns( const struct table *table )
857 858 859
{
    UINT i, num_keys = 0;

860
    for (i = 0; i < table->num_cols; i++)
861
    {
862
        if (table->columns[i].type & COL_FLAG_KEY) num_keys++;
863 864 865 866
    }
    return num_keys;
}

867
static BSTR build_relpath( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
868
{
869 870
    static const WCHAR fmtW[] = {'%','s','.','%','s',0};
    BSTR class, proplist, ret = NULL;
871 872
    struct table *table = view->table[table_index];
    UINT row = view->result[result_index];
873 874
    UINT num_keys, len;

875
    if (view->proplist) return NULL;
876

877 878 879
    if (!(class = SysAllocString( view->table[table_index]->name ))) return NULL;
    if (!(num_keys = count_key_columns( table ))) return class;
    if (!(proplist = build_proplist( table, row, num_keys, &len ))) goto done;
880

881
    len += lstrlenW( fmtW ) + SysStringLen( class );
882
    if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
883
    swprintf( ret, len, fmtW, class, proplist );
884 885 886 887 888

done:
    SysFreeString( class );
    SysFreeString( proplist );
    return ret;
889 890
}

891
static BSTR build_path( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
892 893 894 895 896 897 898
{
    static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
    BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
    UINT len;

    if (view->proplist) return NULL;

899 900 901
    if (!(server = build_servername())) return NULL;
    if (!(namespace = build_namespace())) goto done;
    if (!(relpath = build_relpath( view, table_index, result_index, name ))) goto done;
902

903
    len = lstrlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
904
    if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
905
    swprintf( ret, len, fmtW, server, namespace, relpath );
906 907 908 909 910 911 912 913

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

914
BOOL is_method( const struct table *table, UINT column )
915 916 917 918
{
    return table->columns[column].type & COL_FLAG_METHOD;
}

919
static UINT count_properties( const struct table *table )
920 921 922
{
    UINT i, num_props = 0;

923
    for (i = 0; i < table->num_cols; i++)
924
    {
925
        if (!is_method( table, i )) num_props++;
926 927 928 929
    }
    return num_props;
}

930
static UINT count_result_properties( const struct view *view, UINT table_index )
931 932 933 934
{
    const struct property *prop = view->proplist;
    UINT count;

935
    if (!prop) return count_properties( view->table[table_index] );
936 937 938 939 940 941

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

942
static HRESULT get_system_propval( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name,
943
                                   VARIANT *ret, CIMTYPE *type, LONG *flavor )
944 945 946 947 948 949 950 951 952
{
    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};

953 954
    if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;

955
    if (!wcsicmp( name, classW ))
956
    {
957 958 959
        if (ret)
        {
            V_VT( ret ) = VT_BSTR;
960
            V_BSTR( ret ) = SysAllocString( view->table[table_index]->name );
961
        }
962 963 964
        if (type) *type = CIM_STRING;
        return S_OK;
    }
965
    if (!wcsicmp( name, genusW ))
966
    {
967 968 969 970 971
        if (ret)
        {
            V_VT( ret ) = VT_I4;
            V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
        }
972 973 974
        if (type) *type = CIM_SINT32;
        return S_OK;
    }
975
    else if (!wcsicmp( name, namespaceW ))
976
    {
977 978 979
        if (ret)
        {
            V_VT( ret ) = VT_BSTR;
980
            V_BSTR( ret ) = view->proplist ? NULL : build_namespace();
981
        }
982 983 984
        if (type) *type = CIM_STRING;
        return S_OK;
    }
985
    else if (!wcsicmp( name, pathW ))
986
    {
987 988 989
        if (ret)
        {
            V_VT( ret ) = VT_BSTR;
990
            V_BSTR( ret ) = build_path( view, table_index, result_index, name );
991
        }
992 993 994
        if (type) *type = CIM_STRING;
        return S_OK;
    }
995
    if (!wcsicmp( name, propcountW ))
996
    {
997 998 999
        if (ret)
        {
            V_VT( ret ) = VT_I4;
1000
            V_I4( ret ) = count_result_properties( view, table_index );
1001
        }
1002 1003 1004
        if (type) *type = CIM_SINT32;
        return S_OK;
    }
1005
    else if (!wcsicmp( name, relpathW ))
1006
    {
1007 1008 1009
        if (ret)
        {
            V_VT( ret ) = VT_BSTR;
1010
            V_BSTR( ret ) = build_relpath( view, table_index, result_index, name );
1011
        }
1012 1013 1014
        if (type) *type = CIM_STRING;
        return S_OK;
    }
1015
    else if (!wcsicmp( name, serverW ))
1016
    {
1017 1018 1019
        if (ret)
        {
            V_VT( ret ) = VT_BSTR;
1020
            V_BSTR( ret ) = view->proplist ? NULL : build_servername();
1021
        }
1022 1023 1024 1025 1026 1027 1028
        if (type) *type = CIM_STRING;
        return S_OK;
    }
    FIXME("system property %s not implemented\n", debugstr_w(name));
    return WBEM_E_NOT_FOUND;
}

1029
VARTYPE to_vartype( CIMTYPE type )
1030
{
1031 1032
    switch (type)
    {
1033 1034
    case CIM_BOOLEAN:   return VT_BOOL;

1035
    case CIM_STRING:
1036
    case CIM_REFERENCE:
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    case CIM_DATETIME:  return VT_BSTR;

    case CIM_SINT8:     return VT_I1;
    case CIM_UINT8:     return VT_UI1;
    case CIM_SINT16:    return VT_I2;

    case CIM_UINT16:
    case CIM_SINT32:
    case CIM_UINT32:    return VT_I4;

    case CIM_SINT64:    return VT_I8;
    case CIM_UINT64:    return VT_UI8;

    case CIM_REAL32:    return VT_R4;

1052 1053 1054 1055 1056 1057 1058
    default:
        ERR("unhandled type %u\n", type);
        break;
    }
    return 0;
}

1059
SAFEARRAY *to_safearray( const struct array *array, CIMTYPE basetype )
1060 1061
{
    SAFEARRAY *ret;
1062
    VARTYPE vartype = to_vartype( basetype );
1063 1064
    LONG i;

1065
    if (!array || !(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
1066 1067 1068

    for (i = 0; i < array->count; i++)
    {
1069
        void *ptr = (char *)array->ptr + i * array->elem_size;
1070 1071 1072 1073 1074 1075 1076 1077 1078
        if (vartype == VT_BSTR)
        {
            BSTR str = SysAllocString( *(const WCHAR **)ptr );
            if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
            {
                SysFreeString( str );
                SafeArrayDestroy( ret );
                return NULL;
            }
1079
            SysFreeString( str );
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
        }
        else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
        {
            SafeArrayDestroy( ret );
            return NULL;
        }
    }
    return ret;
}

1090
void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
1091 1092 1093 1094 1095 1096 1097 1098
{
    if (type & VT_ARRAY)
    {
        V_VT( ret ) = type;
        V_ARRAY( ret ) = val_ptr;
        return;
    }
    switch (type)
1099
    {
1100 1101
    case VT_BOOL:
        V_BOOL( ret ) = val;
1102
        break;
1103
    case VT_BSTR:
1104 1105
        V_BSTR( ret ) = val_ptr;
        break;
1106 1107 1108 1109 1110 1111
    case VT_I1:
        V_I1( ret ) = val;
        break;
    case VT_UI1:
        V_UI1( ret ) = val;
        break;
1112 1113
    case VT_I2:
        V_I2( ret ) = val;
1114
        break;
1115 1116
    case VT_UI2:
        V_UI2( ret ) = val;
1117
        break;
1118 1119
    case VT_I4:
        V_I4( ret ) = val;
1120
        break;
1121 1122
    case VT_UI4:
        V_UI4( ret ) = val;
1123
        break;
1124
    case VT_NULL:
1125
        break;
1126 1127 1128
    case VT_R4:
        V_R4( ret ) = *(FLOAT *)&val;
        break;
1129
    default:
1130
        ERR("unhandled variant type %u\n", type);
1131 1132
        return;
    }
1133
    V_VT( ret ) = type;
1134 1135
}

1136 1137
static HRESULT map_view_index( const struct view *view, UINT index, UINT *table_index, UINT *result_index )
{
1138 1139
    if (!view->table) return WBEM_E_NOT_FOUND;

1140 1141 1142 1143 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 1173 1174 1175
    switch (view->type)
    {
    case VIEW_TYPE_SELECT:
        *table_index = 0;
        *result_index = index;
        break;

    case VIEW_TYPE_ASSOCIATORS:
        *table_index = *result_index = index;
        break;

    default:
        ERR( "unhandled view type %u\n", view->type );
        return WBEM_E_FAILED;
    }
    return S_OK;
}

struct table *get_view_table( const struct view *view, UINT index )
{
    switch (view->type)
    {
    case VIEW_TYPE_SELECT:
        return view->table[0];

    case VIEW_TYPE_ASSOCIATORS:
        return view->table[index];

    default:
        ERR( "unhandled view type %u\n", view->type );
        return NULL;
    }
}

HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret, CIMTYPE *type,
                     LONG *flavor )
1176 1177
{
    HRESULT hr;
1178 1179
    UINT column, row, table_index, result_index;
    struct table *table;
1180
    VARTYPE vartype;
1181
    void *val_ptr = NULL;
1182
    LONGLONG val;
1183

1184
    if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1185

1186 1187
    if (is_system_prop( name )) return get_system_propval( view, table_index, result_index, name, ret, type, flavor );
    if (!view->result_count || !is_result_prop( view, name )) return WBEM_E_NOT_FOUND;
1188

1189 1190 1191 1192 1193 1194
    table = view->table[table_index];
    hr = get_column_index( table, name, &column );
    if (hr != S_OK || is_method( table, column )) return WBEM_E_NOT_FOUND;

    row = view->result[result_index];
    hr = get_value( table, row, column, &val );
1195 1196
    if (hr != S_OK) return hr;

1197
    if (type) *type = table->columns[column].type & COL_TYPE_MASK;
1198 1199 1200 1201
    if (flavor) *flavor = 0;

    if (!ret) return S_OK;

1202
    vartype = to_vartype( table->columns[column].type & CIM_TYPE_MASK );
1203
    if (table->columns[column].type & CIM_FLAG_ARRAY)
1204
    {
1205
        CIMTYPE basetype = table->columns[column].type & CIM_TYPE_MASK;
1206 1207

        val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
1208
        if (!val_ptr) vartype = VT_NULL;
1209
        else vartype |= VT_ARRAY;
1210 1211
        set_variant( vartype, val, val_ptr, ret );
        return S_OK;
1212
    }
1213

1214
    switch (table->columns[column].type & COL_TYPE_MASK)
1215 1216
    {
    case CIM_STRING:
1217
    case CIM_REFERENCE:
1218
    case CIM_DATETIME:
1219 1220 1221
        if (val)
        {
            vartype = VT_BSTR;
1222
            val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
1223 1224 1225
        }
        else
            vartype = VT_NULL;
1226
        break;
1227
    case CIM_SINT64:
1228
        vartype = VT_BSTR;
1229
        val_ptr = get_value_bstr( table, row, column );
1230 1231
        break;
    case CIM_UINT64:
1232
        vartype = VT_BSTR;
1233 1234
        val_ptr = get_value_bstr( table, row, column );
        break;
1235 1236 1237 1238 1239 1240 1241
    case CIM_BOOLEAN:
    case CIM_SINT8:
    case CIM_UINT8:
    case CIM_SINT16:
    case CIM_UINT16:
    case CIM_SINT32:
    case CIM_UINT32:
1242
    case CIM_REAL32:
1243
        break;
1244
    default:
1245
        ERR("unhandled column type %u\n", table->columns[column].type);
1246 1247
        return WBEM_E_FAILED;
    }
1248 1249

    set_variant( vartype, val, val_ptr, ret );
1250 1251
    return S_OK;
}
1252

1253 1254 1255 1256 1257 1258
static CIMTYPE to_cimtype( VARTYPE type )
{
    switch (type)
    {
    case VT_BOOL:  return CIM_BOOLEAN;
    case VT_BSTR:  return CIM_STRING;
1259 1260
    case VT_I1:    return CIM_SINT8;
    case VT_UI1:   return CIM_UINT8;
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
    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;

    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;

1286 1287 1288
    ret->count     = bound + 1;
    ret->elem_size = get_type_size( basetype );
    if (!(ret->ptr = heap_alloc_zero( ret->count * ret->elem_size )))
1289 1290 1291 1292 1293 1294
    {
        heap_free( ret );
        return NULL;
    }
    for (i = 0; i < ret->count; i++)
    {
1295
        void *ptr = (char *)ret->ptr + i * ret->elem_size;
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
        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)
1313
        {
1314
            destroy_array( ret, basetype );
1315 1316 1317 1318 1319 1320 1321 1322
            return NULL;
        }
    }
    *type = basetype | CIM_FLAG_ARRAY;
    return ret;
}

HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
1323 1324 1325 1326 1327 1328
{
    if (!var)
    {
        *val = 0;
        return S_OK;
    }
1329 1330 1331 1332 1333 1334
    if (V_VT( var ) & VT_ARRAY)
    {
        *val = (INT_PTR)to_array( var, type );
        if (!*val) return E_OUTOFMEMORY;
        return S_OK;
    }
1335 1336
    switch (V_VT( var ))
    {
1337 1338 1339 1340
    case VT_BOOL:
        *val = V_BOOL( var );
        *type = CIM_BOOLEAN;
        break;
1341
    case VT_BSTR:
1342
        *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
        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;
1375 1376
    UINT row, column, table_index, result_index;
    struct table *table;
1377 1378
    LONGLONG val;

1379 1380 1381 1382
    if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;

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

1391
    hr = to_longlong( var, &val, &type );
1392 1393
    if (hr != S_OK) return hr;

1394 1395
    row = view->result[result_index];
    return set_value( table, row, column, val, type );
1396 1397
}

1398
HRESULT get_properties( const struct view *view, UINT index, LONG flags, SAFEARRAY **props )
1399 1400 1401
{
    SAFEARRAY *sa;
    BSTR str;
1402 1403 1404
    UINT i, table_index, result_index, num_props;
    struct table *table;
    HRESULT hr;
1405
    LONG j;
1406

1407 1408 1409
    if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;

    num_props = count_result_properties( view, table_index );
1410
    if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
1411

1412 1413
    table = view->table[table_index];
    for (i = 0, j = 0; i < table->num_cols; i++)
1414
    {
1415 1416
        BOOL is_system;

1417 1418
        if (is_method( table, i )) continue;
        if (!is_result_prop( view, table->columns[i].name )) continue;
1419

1420
        is_system = is_system_prop( table->columns[i].name );
1421 1422 1423
        if ((flags & WBEM_FLAG_NONSYSTEM_ONLY) && is_system) continue;
        else if ((flags & WBEM_FLAG_SYSTEM_ONLY) && !is_system) continue;

1424
        str = SysAllocString( table->columns[i].name );
1425
        if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1426 1427 1428 1429 1430
        {
            SysFreeString( str );
            SafeArrayDestroy( sa );
            return E_OUTOFMEMORY;
        }
1431
        SysFreeString( str );
1432
        j++;
1433 1434 1435 1436
    }
    *props = sa;
    return S_OK;
}