mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / wbemprox / query.c
blobec2a43c3f5f4755a1ef21a4bc35a46e5fc596877
1 /*
2 * Copyright 2012 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define COBJMACROS
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wbemcli.h"
27 #include "wine/debug.h"
28 #include "wbemprox_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
32 static HRESULT append_table( struct view *view, struct table *table )
34 struct table **tmp;
35 if (!(tmp = heap_realloc( view->table, (view->table_count + 1) * sizeof(*tmp) ))) return E_OUTOFMEMORY;
36 view->table = tmp;
37 view->table[view->table_count++] = table;
38 return S_OK;
41 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 )
44 struct view *view = heap_alloc_zero( sizeof(*view) );
46 if (!view) return E_OUTOFMEMORY;
48 switch (type)
50 case VIEW_TYPE_ASSOCIATORS:
51 view->path = path;
52 view->keywordlist = keywordlist;
53 break;
55 case VIEW_TYPE_SELECT:
57 struct table *table = grab_table( class );
58 HRESULT hr;
60 if (table && (hr = append_table( view, table )) != S_OK)
62 heap_free( view );
63 return hr;
65 view->proplist = proplist;
66 view->cond = cond;
67 break;
69 default:
70 ERR( "unhandled type %u\n", type );
71 heap_free( view );
72 return E_INVALIDARG;
75 view->type = type;
76 *ret = view;
77 return S_OK;
80 void destroy_view( struct view *view )
82 ULONG i;
83 if (!view) return;
84 for (i = 0; i < view->table_count; i++) release_table( view->table[i] );
85 heap_free( view->table );
86 heap_free( view->result );
87 heap_free( view );
90 static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
92 const WCHAR *p = lstr, *q = rstr;
94 while (*p && *q)
96 if (q[0] == '\\' && q[1] == '\\') q++;
97 if (*q == '%')
99 while (*q == '%') q++;
100 if (!*q) return TRUE;
101 while (*p && *q && towupper( *p ) == towupper( *q )) { p++; q++; };
102 if (!*p && !*q) return TRUE;
104 if (*q != '%' && towupper( *p++ ) != towupper( *q++ )) return FALSE;
106 return TRUE;
109 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
111 if (!lstr || !rstr)
113 *val = 0;
114 return S_OK;
116 switch (op)
118 case OP_EQ:
119 *val = !wcscmp( lstr, rstr );
120 break;
121 case OP_GT:
122 *val = wcscmp( lstr, rstr ) > 0;
123 break;
124 case OP_LT:
125 *val = wcscmp( lstr, rstr ) < 0;
126 break;
127 case OP_LE:
128 *val = wcscmp( lstr, rstr ) <= 0;
129 break;
130 case OP_GE:
131 *val = wcscmp( lstr, rstr ) >= 0;
132 break;
133 case OP_NE:
134 *val = wcscmp( lstr, rstr );
135 break;
136 case OP_LIKE:
137 *val = eval_like( lstr, rstr );
138 break;
139 default:
140 ERR("unhandled operator %u\n", op);
141 return WBEM_E_INVALID_QUERY;
143 return S_OK;
146 static BOOL is_int( CIMTYPE type )
148 switch (type)
150 case CIM_SINT8:
151 case CIM_SINT16:
152 case CIM_SINT32:
153 case CIM_SINT64:
154 case CIM_UINT8:
155 case CIM_UINT16:
156 case CIM_UINT32:
157 case CIM_UINT64:
158 return TRUE;
159 default:
160 return FALSE;
164 static inline BOOL is_strcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
166 if ((ltype == CIM_STRING || is_int( ltype )) && expr->left->type == EXPR_PROPVAL &&
167 expr->right->type == EXPR_SVAL) return TRUE;
168 else if ((rtype == CIM_STRING || is_int( rtype )) && expr->right->type == EXPR_PROPVAL &&
169 expr->left->type == EXPR_SVAL) return TRUE;
170 return FALSE;
173 static inline BOOL is_boolcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
175 if (ltype == CIM_BOOLEAN && expr->left->type == EXPR_PROPVAL &&
176 (expr->right->type == EXPR_SVAL || expr->right->type == EXPR_BVAL)) return TRUE;
177 else if (rtype == CIM_BOOLEAN && expr->right->type == EXPR_PROPVAL &&
178 (expr->left->type == EXPR_SVAL || expr->left->type == EXPR_BVAL)) return TRUE;
179 return FALSE;
182 static HRESULT eval_boolcmp( UINT op, LONGLONG lval, LONGLONG rval, UINT ltype, UINT rtype, LONGLONG *val )
184 if (ltype == CIM_STRING) lval = !wcsicmp( (const WCHAR *)(INT_PTR)lval, L"True" ) ? -1 : 0;
185 else if (rtype == CIM_STRING) rval = !wcsicmp( (const WCHAR *)(INT_PTR)rval, L"True" ) ? -1 : 0;
187 switch (op)
189 case OP_EQ:
190 *val = (lval == rval);
191 break;
192 case OP_NE:
193 *val = (lval != rval);
194 break;
195 default:
196 ERR("unhandled operator %u\n", op);
197 return WBEM_E_INVALID_QUERY;
199 return S_OK;
202 static inline BOOL is_refcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
204 if (ltype == CIM_REFERENCE && expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) return TRUE;
205 else if (rtype == CIM_REFERENCE && expr->right->type == EXPR_PROPVAL && expr->left->type == EXPR_SVAL) return TRUE;
206 return FALSE;
209 static HRESULT eval_refcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
211 if (!lstr || !rstr)
213 *val = 0;
214 return S_OK;
216 switch (op)
218 case OP_EQ:
219 *val = !wcsicmp( lstr, rstr );
220 break;
221 case OP_NE:
222 *val = wcsicmp( lstr, rstr );
223 break;
224 default:
225 ERR("unhandled operator %u\n", op);
226 return WBEM_E_INVALID_QUERY;
228 return S_OK;
231 static UINT resolve_type( UINT left, UINT right )
233 switch (left)
235 case CIM_SINT8:
236 case CIM_SINT16:
237 case CIM_SINT32:
238 case CIM_SINT64:
239 case CIM_UINT8:
240 case CIM_UINT16:
241 case CIM_UINT32:
242 case CIM_UINT64:
243 switch (right)
245 case CIM_SINT8:
246 case CIM_SINT16:
247 case CIM_SINT32:
248 case CIM_SINT64:
249 case CIM_UINT8:
250 case CIM_UINT16:
251 case CIM_UINT32:
252 case CIM_UINT64:
253 return CIM_UINT64;
254 default: break;
256 break;
258 case CIM_STRING:
259 if (right == CIM_STRING) return CIM_STRING;
260 break;
262 case CIM_BOOLEAN:
263 if (right == CIM_BOOLEAN) return CIM_BOOLEAN;
264 break;
266 case CIM_REFERENCE:
267 if (right == CIM_REFERENCE) return CIM_REFERENCE;
268 break;
270 default:
271 break;
273 return CIM_ILLEGAL;
276 static const WCHAR *format_int( WCHAR *buf, UINT len, CIMTYPE type, LONGLONG val )
278 switch (type)
280 case CIM_SINT8:
281 case CIM_SINT16:
282 case CIM_SINT32:
283 swprintf( buf, len, L"%d", val );
284 return buf;
286 case CIM_UINT8:
287 case CIM_UINT16:
288 case CIM_UINT32:
289 swprintf( buf, len, L"%u", val );
290 return buf;
292 case CIM_SINT64:
293 wsprintfW( buf, L"%I64d", val );
294 return buf;
296 case CIM_UINT64:
297 wsprintfW( buf, L"%I64u", val );
298 return buf;
300 default:
301 ERR( "unhandled type %u\n", type );
302 return NULL;
306 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
307 LONGLONG *val, UINT *type )
309 HRESULT lret, rret;
310 LONGLONG lval, rval;
311 UINT ltype, rtype;
313 lret = eval_cond( table, row, expr->left, &lval, &ltype );
314 rret = eval_cond( table, row, expr->right, &rval, &rtype );
315 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
317 *type = resolve_type( ltype, rtype );
319 if (is_strcmp( expr, ltype, rtype ))
321 const WCHAR *lstr, *rstr;
322 WCHAR lbuf[21], rbuf[21];
324 if (is_int( ltype )) lstr = format_int( lbuf, ARRAY_SIZE( lbuf ), ltype, lval );
325 else lstr = (const WCHAR *)(INT_PTR)lval;
327 if (is_int( rtype )) rstr = format_int( rbuf, ARRAY_SIZE( rbuf ), rtype, rval );
328 else rstr = (const WCHAR *)(INT_PTR)rval;
330 return eval_strcmp( expr->op, lstr, rstr, val );
332 if (is_boolcmp( expr, ltype, rtype ))
334 return eval_boolcmp( expr->op, lval, rval, ltype, rtype, val );
336 if (is_refcmp( expr, ltype, rtype ))
338 return eval_refcmp( expr->op, (const WCHAR *)(INT_PTR)lval, (const WCHAR *)(INT_PTR)rval, val );
341 switch (expr->op)
343 case OP_EQ:
344 *val = (lval == rval);
345 break;
346 case OP_AND:
347 *val = (lval && rval);
348 break;
349 case OP_OR:
350 *val = (lval || rval);
351 break;
352 case OP_GT:
353 *val = (lval > rval);
354 break;
355 case OP_LT:
356 *val = (lval < rval);
357 break;
358 case OP_LE:
359 *val = (lval <= rval);
360 break;
361 case OP_GE:
362 *val = (lval >= rval);
363 break;
364 case OP_NE:
365 *val = (lval != rval);
366 break;
367 default:
368 ERR("unhandled operator %u\n", expr->op);
369 return WBEM_E_INVALID_QUERY;
371 return S_OK;
374 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
375 LONGLONG *val, UINT *type )
378 HRESULT hr;
379 UINT column;
380 LONGLONG lval;
382 if (expr->op == OP_NOT)
384 hr = eval_cond( table, row, expr->left, &lval, type );
385 if (hr != S_OK)
386 return hr;
387 *val = !lval;
388 return S_OK;
391 hr = get_column_index( table, expr->left->u.propval->name, &column );
392 if (hr != S_OK)
393 return hr;
395 hr = get_value( table, row, column, &lval );
396 if (hr != S_OK)
397 return hr;
399 switch (expr->op)
401 case OP_ISNULL:
402 *val = !lval;
403 break;
404 case OP_NOTNULL:
405 *val = lval;
406 break;
407 default:
408 ERR("unknown operator %u\n", expr->op);
409 return WBEM_E_INVALID_QUERY;
412 *type = table->columns[column].type & CIM_TYPE_MASK;
413 return S_OK;
416 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
417 LONGLONG *val, UINT *type )
420 HRESULT hr;
421 UINT column;
423 hr = get_column_index( table, propval->name, &column );
424 if (hr != S_OK)
425 return hr;
427 *type = table->columns[column].type & CIM_TYPE_MASK;
428 return get_value( table, row, column, val );
431 HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond, LONGLONG *val, UINT *type )
433 if (!cond)
435 *val = 1;
436 *type = CIM_UINT64;
437 return S_OK;
439 switch (cond->type)
441 case EXPR_COMPLEX:
442 return eval_binary( table, row, &cond->u.expr, val, type );
444 case EXPR_UNARY:
445 return eval_unary( table, row, &cond->u.expr, val, type );
447 case EXPR_PROPVAL:
448 return eval_propval( table, row, cond->u.propval, val, type );
450 case EXPR_SVAL:
451 *val = (INT_PTR)cond->u.sval;
452 *type = CIM_STRING;
453 return S_OK;
455 case EXPR_IVAL:
456 *val = cond->u.ival;
457 *type = CIM_UINT64;
458 return S_OK;
460 case EXPR_BVAL:
461 *val = cond->u.ival;
462 *type = CIM_BOOLEAN;
463 return S_OK;
465 default:
466 ERR("invalid expression type\n");
467 break;
469 return WBEM_E_INVALID_QUERY;
472 static WCHAR *build_assoc_query( const WCHAR *class, UINT class_len )
474 static const WCHAR fmtW[] = L"SELECT * FROM __ASSOCIATORS WHERE Class='%s'";
475 UINT len = class_len + ARRAY_SIZE(fmtW);
476 WCHAR *ret;
478 if (!(ret = heap_alloc( len * sizeof(WCHAR) ))) return NULL;
479 swprintf( ret, len, fmtW, class );
480 return ret;
483 static HRESULT create_assoc_enum( const WCHAR *class, UINT class_len, IEnumWbemClassObject **iter )
485 WCHAR *query;
486 HRESULT hr;
488 if (!(query = build_assoc_query( class, class_len ))) return E_OUTOFMEMORY;
489 hr = exec_query( query, iter );
490 heap_free( query );
491 return hr;
494 static WCHAR *build_antecedent_query( const WCHAR *assocclass, const WCHAR *dependent )
496 static const WCHAR fmtW[] = L"SELECT Antecedent FROM %s WHERE Dependent='%s'";
497 UINT len = lstrlenW(assocclass) + lstrlenW(dependent) + ARRAY_SIZE(fmtW);
498 WCHAR *ret;
500 if (!(ret = heap_alloc( len * sizeof(WCHAR) ))) return NULL;
501 swprintf( ret, len, fmtW, assocclass, dependent );
502 return ret;
505 static BSTR build_servername(void)
507 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
508 DWORD len = ARRAY_SIZE( server );
510 if (!(GetComputerNameW( server, &len ))) return NULL;
511 for (p = server; *p; p++) *p = towupper( *p );
512 return SysAllocString( server );
515 static BSTR build_namespace(void)
517 return SysAllocString( L"ROOT\\CIMV2" );
520 static WCHAR *build_canonical_path( const WCHAR *relpath )
522 BSTR server, namespace;
523 WCHAR *ret;
524 UINT len, i;
526 if (!(server = build_servername())) return NULL;
527 if (!(namespace = build_namespace()))
529 SysFreeString( server );
530 return NULL;
533 len = ARRAY_SIZE( L"\\\\%s\\%s:" ) + SysStringLen( server ) + SysStringLen( namespace ) + lstrlenW( relpath );
534 if ((ret = heap_alloc( len * sizeof(WCHAR ) )))
536 len = swprintf( ret, len, L"\\\\%s\\%s:", server, namespace );
537 for (i = 0; i < lstrlenW( relpath ); i ++)
539 if (relpath[i] == '\'') ret[len++] = '"';
540 else ret[len++] = relpath[i];
542 ret[len] = 0;
545 SysFreeString( server );
546 SysFreeString( namespace );
547 return ret;
550 static HRESULT get_antecedent( const WCHAR *assocclass, const WCHAR *dependent, BSTR *ret )
552 WCHAR *fullpath, *str;
553 IEnumWbemClassObject *iter = NULL;
554 IWbemClassObject *obj;
555 HRESULT hr = E_OUTOFMEMORY;
556 ULONG count;
557 VARIANT var;
559 if (!(fullpath = build_canonical_path( dependent ))) return E_OUTOFMEMORY;
560 if (!(str = build_antecedent_query( assocclass, fullpath ))) goto done;
561 if ((hr = exec_query( str, &iter )) != S_OK) goto done;
563 IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
564 if (!count)
566 *ret = NULL;
567 goto done;
570 hr = IWbemClassObject_Get( obj, L"Antecedent", 0, &var, NULL, NULL );
571 IWbemClassObject_Release( obj );
572 if (hr != S_OK) goto done;
573 *ret = V_BSTR( &var );
575 done:
576 if (iter) IEnumWbemClassObject_Release( iter );
577 heap_free( str );
578 heap_free( fullpath );
579 return hr;
582 static HRESULT do_query( const WCHAR *str, struct query **ret_query )
584 struct query *query;
585 HRESULT hr;
587 if (!(query = create_query())) return E_OUTOFMEMORY;
588 if ((hr = parse_query( str, &query->view, &query->mem )) != S_OK || (hr = execute_view( query->view )) != S_OK)
590 release_query( query );
591 return hr;
593 *ret_query = query;
594 return S_OK;
597 static HRESULT get_antecedent_table( const WCHAR *assocclass, const WCHAR *dependent, struct table **table )
599 BSTR antecedent = NULL;
600 struct path *path = NULL;
601 WCHAR *str = NULL;
602 struct query *query = NULL;
603 HRESULT hr;
605 if ((hr = get_antecedent( assocclass, dependent, &antecedent )) != S_OK) return hr;
606 if (!antecedent)
608 *table = NULL;
609 return S_OK;
611 if ((hr = parse_path( antecedent, &path )) != S_OK) goto done;
612 if (!(str = query_from_path( path )))
614 hr = E_OUTOFMEMORY;
615 goto done;
618 if ((hr = do_query( str, &query )) != S_OK) goto done;
619 if (query->view->table_count) *table = addref_table( query->view->table[0] );
620 else *table = NULL;
622 done:
623 if (query) release_query( query );
624 free_path( path );
625 SysFreeString( antecedent );
626 heap_free( str );
627 return hr;
630 static HRESULT exec_assoc_view( struct view *view )
632 IEnumWbemClassObject *iter = NULL;
633 struct path *path;
634 HRESULT hr;
636 if (view->keywordlist) FIXME( "ignoring keywords\n" );
637 if ((hr = parse_path( view->path, &path )) != S_OK) return hr;
639 if ((hr = create_assoc_enum( path->class, path->class_len, &iter )) != S_OK) goto done;
640 for (;;)
642 ULONG count;
643 IWbemClassObject *obj;
644 struct table *table;
645 VARIANT var;
647 IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
648 if (!count) break;
650 if ((hr = IWbemClassObject_Get( obj, L"AssocClass", 0, &var, NULL, NULL )) != S_OK)
652 IWbemClassObject_Release( obj );
653 goto done;
655 IWbemClassObject_Release( obj );
657 hr = get_antecedent_table( V_BSTR(&var), view->path, &table );
658 VariantClear( &var );
659 if (hr != S_OK) goto done;
661 if (table && (hr = append_table( view, table )) != S_OK)
663 release_table( table );
664 goto done;
668 if (view->table_count)
670 if (!(view->result = heap_alloc_zero( view->table_count * sizeof(UINT) ))) hr = E_OUTOFMEMORY;
671 else view->result_count = view->table_count;
674 done:
675 if (iter) IEnumWbemClassObject_Release( iter );
676 free_path( path );
677 return hr;
680 static HRESULT exec_select_view( struct view *view )
682 UINT i, j = 0, len;
683 enum fill_status status = FILL_STATUS_UNFILTERED;
684 struct table *table;
686 if (!view->table_count) return S_OK;
688 table = view->table[0];
689 if (table->fill)
691 clear_table( table );
692 status = table->fill( table, view->cond );
694 if (status == FILL_STATUS_FAILED) return WBEM_E_FAILED;
695 if (!table->num_rows) return S_OK;
697 len = min( table->num_rows, 16 );
698 if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
700 for (i = 0; i < table->num_rows; i++)
702 HRESULT hr;
703 LONGLONG val = 0;
704 UINT type;
706 if (j >= len)
708 UINT *tmp;
709 len *= 2;
710 if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
711 view->result = tmp;
713 if (status == FILL_STATUS_FILTERED) val = 1;
714 else if ((hr = eval_cond( table, i, view->cond, &val, &type )) != S_OK) return hr;
715 if (val) view->result[j++] = i;
718 view->result_count = j;
719 return S_OK;
722 HRESULT execute_view( struct view *view )
724 switch (view->type)
726 case VIEW_TYPE_ASSOCIATORS:
727 return exec_assoc_view( view );
729 case VIEW_TYPE_SELECT:
730 return exec_select_view( view );
732 default:
733 ERR( "unhandled type %u\n", view->type );
734 return E_INVALIDARG;
738 struct query *create_query(void)
740 struct query *query;
742 if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
743 list_init( &query->mem );
744 query->refs = 1;
745 return query;
748 void free_query( struct query *query )
750 struct list *mem, *next;
752 if (!query) return;
753 destroy_view( query->view );
754 LIST_FOR_EACH_SAFE( mem, next, &query->mem ) { heap_free( mem ); }
755 heap_free( query );
758 struct query *addref_query( struct query *query )
760 InterlockedIncrement( &query->refs );
761 return query;
764 void release_query( struct query *query )
766 if (!InterlockedDecrement( &query->refs )) free_query( query );
769 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
771 HRESULT hr;
772 struct query *query;
774 *result = NULL;
775 if (!(query = create_query())) return E_OUTOFMEMORY;
776 hr = parse_query( str, &query->view, &query->mem );
777 if (hr != S_OK) goto done;
778 hr = execute_view( query->view );
779 if (hr != S_OK) goto done;
780 hr = EnumWbemClassObject_create( query, (void **)result );
782 done:
783 release_query( query );
784 return hr;
787 BOOL is_result_prop( const struct view *view, const WCHAR *name )
789 const struct property *prop = view->proplist;
790 if (!prop) return TRUE;
791 while (prop)
793 if (!wcsicmp( prop->name, name )) return TRUE;
794 prop = prop->next;
796 return FALSE;
799 static BOOL is_system_prop( const WCHAR *name )
801 return (name[0] == '_' && name[1] == '_');
804 static BSTR build_proplist( const struct table *table, UINT row, UINT count, UINT *len )
806 UINT i, j, offset;
807 BSTR *values, ret = NULL;
809 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
811 *len = j = 0;
812 for (i = 0; i < table->num_cols; i++)
814 if (table->columns[i].type & COL_FLAG_KEY)
816 const WCHAR *name = table->columns[i].name;
817 values[j] = get_value_bstr( table, row, i );
818 *len += lstrlenW( L"%s=%s" ) + lstrlenW( name ) + lstrlenW( values[j] );
819 j++;
822 if ((ret = SysAllocStringLen( NULL, *len )))
824 offset = j = 0;
825 for (i = 0; i < table->num_cols; i++)
827 if (table->columns[i].type & COL_FLAG_KEY)
829 const WCHAR *name = table->columns[i].name;
830 offset += swprintf( ret + offset, *len - offset, L"%s=%s", name, values[j] );
831 if (j < count - 1) ret[offset++] = ',';
832 j++;
836 for (i = 0; i < count; i++) SysFreeString( values[i] );
837 heap_free( values );
838 return ret;
841 static UINT count_key_columns( const struct table *table )
843 UINT i, num_keys = 0;
845 for (i = 0; i < table->num_cols; i++)
847 if (table->columns[i].type & COL_FLAG_KEY) num_keys++;
849 return num_keys;
852 static BSTR build_relpath( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
854 BSTR class, proplist, ret = NULL;
855 struct table *table = view->table[table_index];
856 UINT row = view->result[result_index];
857 UINT num_keys, len;
859 if (view->proplist) return NULL;
861 if (!(class = SysAllocString( view->table[table_index]->name ))) return NULL;
862 if (!(num_keys = count_key_columns( table ))) return class;
863 if (!(proplist = build_proplist( table, row, num_keys, &len ))) goto done;
865 len += lstrlenW( L"%s.%s" ) + SysStringLen( class );
866 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
867 swprintf( ret, len, L"%s.%s", class, proplist );
869 done:
870 SysFreeString( class );
871 SysFreeString( proplist );
872 return ret;
875 static BSTR build_path( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
877 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
878 UINT len;
880 if (view->proplist) return NULL;
882 if (!(server = build_servername())) return NULL;
883 if (!(namespace = build_namespace())) goto done;
884 if (!(relpath = build_relpath( view, table_index, result_index, name ))) goto done;
886 len = lstrlenW( L"\\\\%s\\%s:%s" ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
887 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
888 swprintf( ret, len, L"\\\\%s\\%s:%s", server, namespace, relpath );
890 done:
891 SysFreeString( server );
892 SysFreeString( namespace );
893 SysFreeString( relpath );
894 return ret;
897 BOOL is_method( const struct table *table, UINT column )
899 return table->columns[column].type & COL_FLAG_METHOD;
902 static UINT count_properties( const struct table *table )
904 UINT i, num_props = 0;
906 for (i = 0; i < table->num_cols; i++)
908 if (!is_method( table, i )) num_props++;
910 return num_props;
913 static UINT count_result_properties( const struct view *view, UINT table_index )
915 const struct property *prop = view->proplist;
916 UINT count;
918 if (!prop) return count_properties( view->table[table_index] );
920 count = 1;
921 while ((prop = prop->next)) count++;
922 return count;
925 static HRESULT get_system_propval( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name,
926 VARIANT *ret, CIMTYPE *type, LONG *flavor )
928 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
930 if (!wcsicmp( name, L"__CLASS" ))
932 if (ret)
934 V_VT( ret ) = VT_BSTR;
935 V_BSTR( ret ) = SysAllocString( view->table[table_index]->name );
937 if (type) *type = CIM_STRING;
938 return S_OK;
940 if (!wcsicmp( name, L"__GENUS" ))
942 if (ret)
944 V_VT( ret ) = VT_I4;
945 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
947 if (type) *type = CIM_SINT32;
948 return S_OK;
950 if (!wcsicmp( name, L"__NAMESPACE" ))
952 if (ret)
954 V_VT( ret ) = VT_BSTR;
955 V_BSTR( ret ) = view->proplist ? NULL : build_namespace();
956 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
958 if (type) *type = CIM_STRING;
959 return S_OK;
961 if (!wcsicmp( name, L"__PATH" ))
963 if (ret)
965 V_VT( ret ) = VT_BSTR;
966 V_BSTR( ret ) = build_path( view, table_index, result_index, name );
967 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
969 if (type) *type = CIM_STRING;
970 return S_OK;
972 if (!wcsicmp( name, L"__PROPERTY_COUNT" ))
974 if (ret)
976 V_VT( ret ) = VT_I4;
977 V_I4( ret ) = count_result_properties( view, table_index );
979 if (type) *type = CIM_SINT32;
980 return S_OK;
982 if (!wcsicmp( name, L"__RELPATH" ))
984 if (ret)
986 V_VT( ret ) = VT_BSTR;
987 V_BSTR( ret ) = build_relpath( view, table_index, result_index, name );
988 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
990 if (type) *type = CIM_STRING;
991 return S_OK;
993 if (!wcsicmp( name, L"__SERVER" ))
995 if (ret)
997 V_VT( ret ) = VT_BSTR;
998 V_BSTR( ret ) = view->proplist ? NULL : build_servername();
999 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
1001 if (type) *type = CIM_STRING;
1002 return S_OK;
1004 FIXME("system property %s not implemented\n", debugstr_w(name));
1005 return WBEM_E_NOT_FOUND;
1008 VARTYPE to_vartype( CIMTYPE type )
1010 switch (type)
1012 case CIM_BOOLEAN: return VT_BOOL;
1014 case CIM_STRING:
1015 case CIM_REFERENCE:
1016 case CIM_DATETIME: return VT_BSTR;
1018 case CIM_SINT8: return VT_I1;
1019 case CIM_UINT8: return VT_UI1;
1020 case CIM_SINT16: return VT_I2;
1022 case CIM_UINT16:
1023 case CIM_SINT32:
1024 case CIM_UINT32: return VT_I4;
1026 case CIM_SINT64: return VT_I8;
1027 case CIM_UINT64: return VT_UI8;
1029 case CIM_REAL32: return VT_R4;
1031 default:
1032 ERR("unhandled type %u\n", type);
1033 break;
1035 return 0;
1038 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE basetype )
1040 SAFEARRAY *ret;
1041 VARTYPE vartype = to_vartype( basetype );
1042 LONG i;
1044 if (!array || !(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
1046 for (i = 0; i < array->count; i++)
1048 void *ptr = (char *)array->ptr + i * array->elem_size;
1049 if (vartype == VT_BSTR)
1051 BSTR str = SysAllocString( *(const WCHAR **)ptr );
1052 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
1054 SysFreeString( str );
1055 SafeArrayDestroy( ret );
1056 return NULL;
1058 SysFreeString( str );
1060 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
1062 SafeArrayDestroy( ret );
1063 return NULL;
1066 return ret;
1069 void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
1071 if (type & VT_ARRAY)
1073 V_VT( ret ) = type;
1074 V_ARRAY( ret ) = val_ptr;
1075 return;
1077 switch (type)
1079 case VT_BOOL:
1080 V_BOOL( ret ) = val;
1081 break;
1082 case VT_BSTR:
1083 V_BSTR( ret ) = val_ptr;
1084 break;
1085 case VT_I1:
1086 V_I1( ret ) = val;
1087 break;
1088 case VT_UI1:
1089 V_UI1( ret ) = val;
1090 break;
1091 case VT_I2:
1092 V_I2( ret ) = val;
1093 break;
1094 case VT_UI2:
1095 V_UI2( ret ) = val;
1096 break;
1097 case VT_I4:
1098 V_I4( ret ) = val;
1099 break;
1100 case VT_UI4:
1101 V_UI4( ret ) = val;
1102 break;
1103 case VT_NULL:
1104 break;
1105 case VT_R4:
1106 V_R4( ret ) = *(FLOAT *)&val;
1107 break;
1108 default:
1109 ERR("unhandled variant type %u\n", type);
1110 return;
1112 V_VT( ret ) = type;
1115 static HRESULT map_view_index( const struct view *view, UINT index, UINT *table_index, UINT *result_index )
1117 if (!view->table) return WBEM_E_NOT_FOUND;
1119 switch (view->type)
1121 case VIEW_TYPE_SELECT:
1122 *table_index = 0;
1123 *result_index = index;
1124 break;
1126 case VIEW_TYPE_ASSOCIATORS:
1127 *table_index = *result_index = index;
1128 break;
1130 default:
1131 ERR( "unhandled view type %u\n", view->type );
1132 return WBEM_E_FAILED;
1134 return S_OK;
1137 struct table *get_view_table( const struct view *view, UINT index )
1139 switch (view->type)
1141 case VIEW_TYPE_SELECT:
1142 return view->table[0];
1144 case VIEW_TYPE_ASSOCIATORS:
1145 return view->table[index];
1147 default:
1148 ERR( "unhandled view type %u\n", view->type );
1149 return NULL;
1153 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret, CIMTYPE *type,
1154 LONG *flavor )
1156 HRESULT hr;
1157 UINT column, row, table_index, result_index;
1158 struct table *table;
1159 VARTYPE vartype;
1160 void *val_ptr = NULL;
1161 LONGLONG val;
1163 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1165 if (is_system_prop( name )) return get_system_propval( view, table_index, result_index, name, ret, type, flavor );
1166 if (!view->result_count || !is_result_prop( view, name )) return WBEM_E_NOT_FOUND;
1168 table = view->table[table_index];
1169 hr = get_column_index( table, name, &column );
1170 if (hr != S_OK || is_method( table, column )) return WBEM_E_NOT_FOUND;
1172 row = view->result[result_index];
1173 hr = get_value( table, row, column, &val );
1174 if (hr != S_OK) return hr;
1176 if (type) *type = table->columns[column].type & COL_TYPE_MASK;
1177 if (flavor) *flavor = 0;
1179 if (!ret) return S_OK;
1181 vartype = to_vartype( table->columns[column].type & CIM_TYPE_MASK );
1182 if (table->columns[column].type & CIM_FLAG_ARRAY)
1184 CIMTYPE basetype = table->columns[column].type & CIM_TYPE_MASK;
1186 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
1187 if (!val_ptr) vartype = VT_NULL;
1188 else vartype |= VT_ARRAY;
1189 set_variant( vartype, val, val_ptr, ret );
1190 return S_OK;
1193 switch (table->columns[column].type & COL_TYPE_MASK)
1195 case CIM_STRING:
1196 case CIM_REFERENCE:
1197 case CIM_DATETIME:
1198 if (val)
1200 vartype = VT_BSTR;
1201 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
1203 else
1204 vartype = VT_NULL;
1205 break;
1206 case CIM_SINT64:
1207 vartype = VT_BSTR;
1208 val_ptr = get_value_bstr( table, row, column );
1209 break;
1210 case CIM_UINT64:
1211 vartype = VT_BSTR;
1212 val_ptr = get_value_bstr( table, row, column );
1213 break;
1214 case CIM_BOOLEAN:
1215 case CIM_SINT8:
1216 case CIM_UINT8:
1217 case CIM_SINT16:
1218 case CIM_UINT16:
1219 case CIM_SINT32:
1220 case CIM_UINT32:
1221 case CIM_REAL32:
1222 break;
1223 default:
1224 ERR("unhandled column type %u\n", table->columns[column].type);
1225 return WBEM_E_FAILED;
1228 set_variant( vartype, val, val_ptr, ret );
1229 return S_OK;
1232 static CIMTYPE to_cimtype( VARTYPE type )
1234 switch (type)
1236 case VT_BOOL: return CIM_BOOLEAN;
1237 case VT_BSTR: return CIM_STRING;
1238 case VT_I1: return CIM_SINT8;
1239 case VT_UI1: return CIM_UINT8;
1240 case VT_I2: return CIM_SINT16;
1241 case VT_UI2: return CIM_UINT16;
1242 case VT_I4: return CIM_SINT32;
1243 case VT_UI4: return CIM_UINT32;
1244 case VT_I8: return CIM_SINT64;
1245 case VT_UI8: return CIM_UINT64;
1246 default:
1247 ERR("unhandled type %u\n", type);
1248 break;
1250 return 0;
1253 static struct array *to_array( VARIANT *var, CIMTYPE *type )
1255 struct array *ret;
1256 LONG bound, i;
1257 VARTYPE vartype;
1258 CIMTYPE basetype;
1260 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
1261 if (!(basetype = to_cimtype( vartype ))) return NULL;
1262 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
1263 if (!(ret = heap_alloc( sizeof(struct array) ))) return NULL;
1265 ret->count = bound + 1;
1266 ret->elem_size = get_type_size( basetype );
1267 if (!(ret->ptr = heap_alloc_zero( ret->count * ret->elem_size )))
1269 heap_free( ret );
1270 return NULL;
1272 for (i = 0; i < ret->count; i++)
1274 void *ptr = (char *)ret->ptr + i * ret->elem_size;
1275 if (vartype == VT_BSTR)
1277 BSTR str;
1278 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
1280 destroy_array( ret, basetype );
1281 return NULL;
1283 *(WCHAR **)ptr = heap_strdupW( str );
1284 SysFreeString( str );
1285 if (!*(WCHAR **)ptr)
1287 destroy_array( ret, basetype );
1288 return NULL;
1291 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
1293 destroy_array( ret, basetype );
1294 return NULL;
1297 *type = basetype | CIM_FLAG_ARRAY;
1298 return ret;
1301 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
1303 if (!var)
1305 *val = 0;
1306 return S_OK;
1308 if (V_VT( var ) & VT_ARRAY)
1310 *val = (INT_PTR)to_array( var, type );
1311 if (!*val) return E_OUTOFMEMORY;
1312 return S_OK;
1314 switch (V_VT( var ))
1316 case VT_BOOL:
1317 *val = V_BOOL( var );
1318 *type = CIM_BOOLEAN;
1319 break;
1320 case VT_BSTR:
1321 *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
1322 if (!*val) return E_OUTOFMEMORY;
1323 *type = CIM_STRING;
1324 break;
1325 case VT_I2:
1326 *val = V_I2( var );
1327 *type = CIM_SINT16;
1328 break;
1329 case VT_UI2:
1330 *val = V_UI2( var );
1331 *type = CIM_UINT16;
1332 break;
1333 case VT_I4:
1334 *val = V_I4( var );
1335 *type = CIM_SINT32;
1336 break;
1337 case VT_UI4:
1338 *val = V_UI4( var );
1339 *type = CIM_UINT32;
1340 break;
1341 case VT_NULL:
1342 *val = 0;
1343 break;
1344 default:
1345 ERR("unhandled type %u\n", V_VT( var ));
1346 return WBEM_E_FAILED;
1348 return S_OK;
1351 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
1353 HRESULT hr;
1354 UINT row, column, table_index, result_index;
1355 struct table *table;
1356 LONGLONG val;
1358 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1360 table = view->table[table_index];
1361 hr = get_column_index( table, name, &column );
1362 if (hr != S_OK)
1364 FIXME("no support for creating new properties\n");
1365 return WBEM_E_FAILED;
1367 if (is_method( table, column ) || !(table->columns[column].type & COL_FLAG_DYNAMIC))
1368 return WBEM_E_FAILED;
1370 hr = to_longlong( var, &val, &type );
1371 if (hr != S_OK) return hr;
1373 row = view->result[result_index];
1374 return set_value( table, row, column, val, type );
1377 HRESULT get_properties( const struct view *view, UINT index, LONG flags, SAFEARRAY **props )
1379 static const WCHAR * const system_props[] =
1380 { L"__GENUS", L"__CLASS", L"__RELPATH", L"__PROPERTY_COUNT", L"__SERVER", L"__NAMESPACE", L"__PATH" };
1381 SAFEARRAY *sa;
1382 BSTR str;
1383 UINT i, table_index, result_index, count = 0;
1384 struct table *table;
1385 HRESULT hr;
1386 LONG j = 0;
1388 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1389 table = view->table[table_index];
1391 if (!(flags & WBEM_FLAG_NONSYSTEM_ONLY)) count += ARRAY_SIZE(system_props);
1392 if (!(flags & WBEM_FLAG_SYSTEM_ONLY))
1394 for (i = 0; i < table->num_cols; i++)
1396 if (!is_method( table, i ) && is_result_prop( view, table->columns[i].name )) count++;
1400 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, count ))) return E_OUTOFMEMORY;
1402 if (!(flags & WBEM_FLAG_NONSYSTEM_ONLY))
1404 for (j = 0; j < ARRAY_SIZE(system_props); j++)
1406 str = SysAllocString( system_props[j] );
1407 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1409 SysFreeString( str );
1410 SafeArrayDestroy( sa );
1411 return E_OUTOFMEMORY;
1413 SysFreeString( str );
1416 if (!(flags & WBEM_FLAG_SYSTEM_ONLY))
1418 for (i = 0; i < table->num_cols; i++)
1420 if (is_method( table, i ) || !is_result_prop( view, table->columns[i].name )) continue;
1422 str = SysAllocString( table->columns[i].name );
1423 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1425 SysFreeString( str );
1426 SafeArrayDestroy( sa );
1427 return E_OUTOFMEMORY;
1429 SysFreeString( str );
1430 j++;
1434 *props = sa;
1435 return S_OK;