dsdmo: Add Flanger effect stub.
[wine.git] / dlls / wbemprox / query.c
blob1087e27183780a9d89fdd5987afd2333bc1c7db9
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 = 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, enum wbm_namespace ns, const WCHAR *path, const struct keyword *keywordlist,
42 const WCHAR *class, const struct property *proplist, const struct expr *cond, struct view **ret )
44 struct view *view = calloc( 1, 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( ns, class );
58 HRESULT hr;
60 if (table && (hr = append_table( view, table )) != S_OK)
62 free( view );
63 return hr;
65 else if (!table && ns == WBEMPROX_NAMESPACE_LAST)
67 free( view );
68 return WBEM_E_INVALID_CLASS;
70 view->proplist = proplist;
71 view->cond = cond;
72 break;
74 default:
75 ERR( "unhandled type %u\n", type );
76 free( view );
77 return E_INVALIDARG;
80 view->type = type;
81 view->ns = ns;
82 *ret = view;
83 return S_OK;
86 void destroy_view( struct view *view )
88 ULONG i;
89 if (!view) return;
90 for (i = 0; i < view->table_count; i++) release_table( view->table[i] );
91 free( view->table );
92 free( view->result );
93 free( view );
96 static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
98 const WCHAR *p = lstr, *q = rstr;
100 while (*p && *q)
102 if (q[0] == '\\' && q[1] == '\\') q++;
103 if (*q == '%')
105 while (*q == '%') q++;
106 if (!*q) return TRUE;
107 while (*p && *q && towupper( *p ) == towupper( *q )) { p++; q++; };
108 if (!*p && !*q) return TRUE;
110 if (*q != '%' && towupper( *p++ ) != towupper( *q++ )) return FALSE;
112 return TRUE;
115 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
117 if (!lstr || !rstr)
119 *val = 0;
120 return S_OK;
122 switch (op)
124 case OP_EQ:
125 *val = !wcsicmp( lstr, rstr );
126 break;
127 case OP_GT:
128 *val = wcsicmp( lstr, rstr ) > 0;
129 break;
130 case OP_LT:
131 *val = wcsicmp( lstr, rstr ) < 0;
132 break;
133 case OP_LE:
134 *val = wcsicmp( lstr, rstr ) <= 0;
135 break;
136 case OP_GE:
137 *val = wcsicmp( lstr, rstr ) >= 0;
138 break;
139 case OP_NE:
140 *val = wcsicmp( lstr, rstr );
141 break;
142 case OP_LIKE:
143 *val = eval_like( lstr, rstr );
144 break;
145 default:
146 ERR("unhandled operator %u\n", op);
147 return WBEM_E_INVALID_QUERY;
149 return S_OK;
152 static BOOL is_int( CIMTYPE type )
154 switch (type)
156 case CIM_SINT8:
157 case CIM_SINT16:
158 case CIM_SINT32:
159 case CIM_SINT64:
160 case CIM_UINT8:
161 case CIM_UINT16:
162 case CIM_UINT32:
163 case CIM_UINT64:
164 return TRUE;
165 default:
166 return FALSE;
170 static inline BOOL is_strcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
172 if ((ltype == CIM_STRING || is_int( ltype )) && expr->left->type == EXPR_PROPVAL &&
173 expr->right->type == EXPR_SVAL) return TRUE;
174 else if ((rtype == CIM_STRING || is_int( rtype )) && expr->right->type == EXPR_PROPVAL &&
175 expr->left->type == EXPR_SVAL) return TRUE;
176 return FALSE;
179 static inline BOOL is_boolcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
181 if (ltype == CIM_BOOLEAN && expr->left->type == EXPR_PROPVAL &&
182 (expr->right->type == EXPR_SVAL || expr->right->type == EXPR_BVAL)) return TRUE;
183 else if (rtype == CIM_BOOLEAN && expr->right->type == EXPR_PROPVAL &&
184 (expr->left->type == EXPR_SVAL || expr->left->type == EXPR_BVAL)) return TRUE;
185 return FALSE;
188 static HRESULT eval_boolcmp( UINT op, LONGLONG lval, LONGLONG rval, UINT ltype, UINT rtype, LONGLONG *val )
190 if (ltype == CIM_STRING) lval = !wcsicmp( (const WCHAR *)(INT_PTR)lval, L"True" ) ? -1 : 0;
191 else if (rtype == CIM_STRING) rval = !wcsicmp( (const WCHAR *)(INT_PTR)rval, L"True" ) ? -1 : 0;
193 switch (op)
195 case OP_EQ:
196 *val = (lval == rval);
197 break;
198 case OP_NE:
199 *val = (lval != rval);
200 break;
201 default:
202 ERR("unhandled operator %u\n", op);
203 return WBEM_E_INVALID_QUERY;
205 return S_OK;
208 static inline BOOL is_refcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
210 if (ltype == CIM_REFERENCE && expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) return TRUE;
211 else if (rtype == CIM_REFERENCE && expr->right->type == EXPR_PROPVAL && expr->left->type == EXPR_SVAL) return TRUE;
212 return FALSE;
215 static HRESULT eval_refcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
217 if (!lstr || !rstr)
219 *val = 0;
220 return S_OK;
222 switch (op)
224 case OP_EQ:
225 *val = !wcsicmp( lstr, rstr );
226 break;
227 case OP_NE:
228 *val = wcsicmp( lstr, rstr );
229 break;
230 default:
231 ERR("unhandled operator %u\n", op);
232 return WBEM_E_INVALID_QUERY;
234 return S_OK;
237 static UINT resolve_type( UINT left, UINT right )
239 switch (left)
241 case CIM_SINT8:
242 case CIM_SINT16:
243 case CIM_SINT32:
244 case CIM_SINT64:
245 case CIM_UINT8:
246 case CIM_UINT16:
247 case CIM_UINT32:
248 case CIM_UINT64:
249 switch (right)
251 case CIM_SINT8:
252 case CIM_SINT16:
253 case CIM_SINT32:
254 case CIM_SINT64:
255 case CIM_UINT8:
256 case CIM_UINT16:
257 case CIM_UINT32:
258 case CIM_UINT64:
259 return CIM_UINT64;
260 default: break;
262 break;
264 case CIM_STRING:
265 if (right == CIM_STRING) return CIM_STRING;
266 break;
268 case CIM_BOOLEAN:
269 if (right == CIM_BOOLEAN) return CIM_BOOLEAN;
270 break;
272 case CIM_REFERENCE:
273 if (right == CIM_REFERENCE) return CIM_REFERENCE;
274 break;
276 default:
277 break;
279 return CIM_ILLEGAL;
282 static const WCHAR *format_int( WCHAR *buf, UINT len, CIMTYPE type, LONGLONG val )
284 switch (type)
286 case CIM_SINT8:
287 case CIM_SINT16:
288 case CIM_SINT32:
289 swprintf( buf, len, L"%d", val );
290 return buf;
292 case CIM_UINT8:
293 case CIM_UINT16:
294 case CIM_UINT32:
295 swprintf( buf, len, L"%u", val );
296 return buf;
298 case CIM_SINT64:
299 wsprintfW( buf, L"%I64d", val );
300 return buf;
302 case CIM_UINT64:
303 wsprintfW( buf, L"%I64u", val );
304 return buf;
306 default:
307 ERR( "unhandled type %lu\n", type );
308 return NULL;
312 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
313 LONGLONG *val, UINT *type )
315 HRESULT lret, rret;
316 LONGLONG lval, rval;
317 UINT ltype, rtype;
319 lret = eval_cond( table, row, expr->left, &lval, &ltype );
320 rret = eval_cond( table, row, expr->right, &rval, &rtype );
321 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
323 *type = resolve_type( ltype, rtype );
325 if (is_strcmp( expr, ltype, rtype ))
327 const WCHAR *lstr, *rstr;
328 WCHAR lbuf[21], rbuf[21];
330 if (is_int( ltype )) lstr = format_int( lbuf, ARRAY_SIZE( lbuf ), ltype, lval );
331 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;
336 return eval_strcmp( expr->op, lstr, rstr, val );
338 if (is_boolcmp( expr, ltype, rtype ))
340 return eval_boolcmp( expr->op, lval, rval, ltype, rtype, val );
342 if (is_refcmp( expr, ltype, rtype ))
344 return eval_refcmp( expr->op, (const WCHAR *)(INT_PTR)lval, (const WCHAR *)(INT_PTR)rval, val );
347 switch (expr->op)
349 case OP_EQ:
350 *val = (lval == rval);
351 break;
352 case OP_AND:
353 *val = (lval && rval);
354 break;
355 case OP_OR:
356 *val = (lval || rval);
357 break;
358 case OP_GT:
359 *val = (lval > rval);
360 break;
361 case OP_LT:
362 *val = (lval < rval);
363 break;
364 case OP_LE:
365 *val = (lval <= rval);
366 break;
367 case OP_GE:
368 *val = (lval >= rval);
369 break;
370 case OP_NE:
371 *val = (lval != rval);
372 break;
373 default:
374 ERR("unhandled operator %u\n", expr->op);
375 return WBEM_E_INVALID_QUERY;
377 return S_OK;
380 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
381 LONGLONG *val, UINT *type )
384 HRESULT hr;
385 UINT column;
386 LONGLONG lval;
388 if (expr->op == OP_NOT)
390 hr = eval_cond( table, row, expr->left, &lval, type );
391 if (hr != S_OK)
392 return hr;
393 *val = !lval;
394 return S_OK;
397 hr = get_column_index( table, expr->left->u.propval->name, &column );
398 if (hr != S_OK)
399 return hr;
401 hr = get_value( table, row, column, &lval );
402 if (hr != S_OK)
403 return hr;
405 switch (expr->op)
407 case OP_ISNULL:
408 *val = !lval;
409 break;
410 case OP_NOTNULL:
411 *val = lval;
412 break;
413 default:
414 ERR("unknown operator %u\n", expr->op);
415 return WBEM_E_INVALID_QUERY;
418 *type = table->columns[column].type & CIM_TYPE_MASK;
419 return S_OK;
422 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
423 LONGLONG *val, UINT *type )
426 HRESULT hr;
427 UINT column;
429 hr = get_column_index( table, propval->name, &column );
430 if (hr != S_OK)
431 return hr;
433 *type = table->columns[column].type & CIM_TYPE_MASK;
434 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 )
439 if (!cond)
441 *val = 1;
442 *type = CIM_UINT64;
443 return S_OK;
445 switch (cond->type)
447 case EXPR_COMPLEX:
448 return eval_binary( table, row, &cond->u.expr, val, type );
450 case EXPR_UNARY:
451 return eval_unary( table, row, &cond->u.expr, val, type );
453 case EXPR_PROPVAL:
454 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;
461 case EXPR_IVAL:
462 *val = cond->u.ival;
463 *type = CIM_UINT64;
464 return S_OK;
466 case EXPR_BVAL:
467 *val = cond->u.ival;
468 *type = CIM_BOOLEAN;
469 return S_OK;
471 default:
472 ERR("invalid expression type\n");
473 break;
475 return WBEM_E_INVALID_QUERY;
478 static WCHAR *build_assoc_query( const WCHAR *class, UINT class_len )
480 static const WCHAR fmtW[] = L"SELECT * FROM __ASSOCIATORS WHERE Class='%s'";
481 UINT len = class_len + ARRAY_SIZE(fmtW);
482 WCHAR *ret;
484 if (!(ret = malloc( len * sizeof(WCHAR) ))) return NULL;
485 swprintf( ret, len, fmtW, class );
486 return ret;
489 static HRESULT create_assoc_enum( enum wbm_namespace ns, const WCHAR *class, UINT class_len,
490 IEnumWbemClassObject **iter )
492 WCHAR *query;
493 HRESULT hr;
495 if (!(query = build_assoc_query( class, class_len ))) return E_OUTOFMEMORY;
496 hr = exec_query( ns, query, iter );
497 free( query );
498 return hr;
501 static WCHAR *build_antecedent_query( const WCHAR *assocclass, const WCHAR *dependent )
503 static const WCHAR fmtW[] = L"SELECT Antecedent FROM %s WHERE Dependent='%s'";
504 UINT len = lstrlenW(assocclass) + lstrlenW(dependent) + ARRAY_SIZE(fmtW);
505 WCHAR *ret;
507 if (!(ret = malloc( len * sizeof(WCHAR) ))) return NULL;
508 swprintf( ret, len, fmtW, assocclass, dependent );
509 return ret;
512 static BSTR build_servername(void)
514 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
515 DWORD len = ARRAY_SIZE( server );
517 if (!(GetComputerNameW( server, &len ))) return NULL;
518 for (p = server; *p; p++) *p = towupper( *p );
519 return SysAllocString( server );
522 static BSTR build_namespace(void)
524 return SysAllocString( L"ROOT\\CIMV2" );
527 static WCHAR *build_canonical_path( const WCHAR *relpath )
529 BSTR server, namespace;
530 WCHAR *ret;
531 UINT len, i;
533 if (!(server = build_servername())) return NULL;
534 if (!(namespace = build_namespace()))
536 SysFreeString( server );
537 return NULL;
540 len = ARRAY_SIZE( L"\\\\%s\\%s:" ) + SysStringLen( server ) + SysStringLen( namespace ) + lstrlenW( relpath );
541 if ((ret = malloc( len * sizeof(WCHAR ) )))
543 len = swprintf( ret, len, L"\\\\%s\\%s:", server, namespace );
544 for (i = 0; i < lstrlenW( relpath ); i ++)
546 if (relpath[i] == '\'') ret[len++] = '"';
547 else ret[len++] = relpath[i];
549 ret[len] = 0;
552 SysFreeString( server );
553 SysFreeString( namespace );
554 return ret;
557 static HRESULT get_antecedent( enum wbm_namespace ns, const WCHAR *assocclass, const WCHAR *dependent, BSTR *ret )
559 WCHAR *fullpath, *str;
560 IEnumWbemClassObject *iter = NULL;
561 IWbemClassObject *obj;
562 HRESULT hr = E_OUTOFMEMORY;
563 ULONG count;
564 VARIANT var;
566 if (!(fullpath = build_canonical_path( dependent ))) return E_OUTOFMEMORY;
567 if (!(str = build_antecedent_query( assocclass, fullpath ))) goto done;
568 if ((hr = exec_query( ns, str, &iter )) != S_OK) goto done;
570 IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
571 if (!count)
573 *ret = NULL;
574 goto done;
577 hr = IWbemClassObject_Get( obj, L"Antecedent", 0, &var, NULL, NULL );
578 IWbemClassObject_Release( obj );
579 if (hr != S_OK) goto done;
580 *ret = V_BSTR( &var );
582 done:
583 if (iter) IEnumWbemClassObject_Release( iter );
584 free( str );
585 free( fullpath );
586 return hr;
589 static HRESULT do_query( enum wbm_namespace ns, const WCHAR *str, struct query **ret_query )
591 struct query *query;
592 HRESULT hr;
594 if (!(query = create_query( ns ))) return E_OUTOFMEMORY;
595 if ((hr = parse_query( ns, str, &query->view, &query->mem )) != S_OK || (hr = execute_view( query->view )) != S_OK)
597 release_query( query );
598 return hr;
600 *ret_query = query;
601 return S_OK;
604 static HRESULT get_antecedent_table( enum wbm_namespace ns, const WCHAR *assocclass, const WCHAR *dependent,
605 struct table **table )
607 BSTR antecedent = NULL;
608 struct path *path = NULL;
609 WCHAR *str = NULL;
610 struct query *query = NULL;
611 HRESULT hr;
613 if ((hr = get_antecedent( ns, assocclass, dependent, &antecedent )) != S_OK) return hr;
614 if (!antecedent)
616 *table = NULL;
617 return S_OK;
619 if ((hr = parse_path( antecedent, &path )) != S_OK) goto done;
620 if (!(str = query_from_path( path )))
622 hr = E_OUTOFMEMORY;
623 goto done;
626 if ((hr = do_query( ns, str, &query )) != S_OK) goto done;
627 if (query->view->table_count) *table = addref_table( query->view->table[0] );
628 else *table = NULL;
630 done:
631 if (query) release_query( query );
632 free_path( path );
633 SysFreeString( antecedent );
634 free( str );
635 return hr;
638 static HRESULT exec_assoc_view( struct view *view )
640 IEnumWbemClassObject *iter = NULL;
641 struct path *path;
642 HRESULT hr;
644 if (view->keywordlist) FIXME( "ignoring keywords\n" );
645 if ((hr = parse_path( view->path, &path )) != S_OK) return hr;
647 if ((hr = create_assoc_enum( view->ns, path->class, path->class_len, &iter )) != S_OK) goto done;
648 for (;;)
650 ULONG count;
651 IWbemClassObject *obj;
652 struct table *table;
653 VARIANT var;
655 IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
656 if (!count) break;
658 if ((hr = IWbemClassObject_Get( obj, L"AssocClass", 0, &var, NULL, NULL )) != S_OK)
660 IWbemClassObject_Release( obj );
661 goto done;
663 IWbemClassObject_Release( obj );
665 hr = get_antecedent_table( view->ns, V_BSTR(&var), view->path, &table );
666 VariantClear( &var );
667 if (hr != S_OK) goto done;
669 if (table && (hr = append_table( view, table )) != S_OK)
671 release_table( table );
672 goto done;
676 if (view->table_count)
678 if (!(view->result = calloc( view->table_count, sizeof(UINT) ))) hr = E_OUTOFMEMORY;
679 else view->result_count = view->table_count;
682 done:
683 if (iter) IEnumWbemClassObject_Release( iter );
684 free_path( path );
685 return hr;
688 static HRESULT exec_select_view( struct view *view )
690 UINT i, j = 0, len;
691 enum fill_status status = FILL_STATUS_UNFILTERED;
692 struct table *table;
694 if (!view->table_count) return S_OK;
696 table = view->table[0];
697 if (table->fill)
699 clear_table( table );
700 status = table->fill( table, view->cond );
702 if (status == FILL_STATUS_FAILED) return WBEM_E_FAILED;
703 if (!table->num_rows) return S_OK;
705 len = min( table->num_rows, 16 );
706 if (!(view->result = malloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
708 for (i = 0; i < table->num_rows; i++)
710 HRESULT hr;
711 LONGLONG val = 0;
712 UINT type;
714 if (j >= len)
716 UINT *tmp;
717 len *= 2;
718 if (!(tmp = realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
719 view->result = tmp;
721 if (status == FILL_STATUS_FILTERED) val = 1;
722 else if ((hr = eval_cond( table, i, view->cond, &val, &type )) != S_OK) return hr;
723 if (val) view->result[j++] = i;
726 view->result_count = j;
727 return S_OK;
730 HRESULT execute_view( struct view *view )
732 switch (view->type)
734 case VIEW_TYPE_ASSOCIATORS:
735 return exec_assoc_view( view );
737 case VIEW_TYPE_SELECT:
738 return exec_select_view( view );
740 default:
741 ERR( "unhandled type %u\n", view->type );
742 return E_INVALIDARG;
746 struct query *create_query( enum wbm_namespace ns )
748 struct query *query;
750 if (!(query = malloc( sizeof(*query) ))) return NULL;
751 list_init( &query->mem );
752 query->ns = ns;
753 query->refs = 1;
754 return query;
757 void free_query( struct query *query )
759 struct list *mem, *next;
761 if (!query) return;
762 destroy_view( query->view );
763 LIST_FOR_EACH_SAFE( mem, next, &query->mem ) { free( mem ); }
764 free( query );
767 struct query *addref_query( struct query *query )
769 InterlockedIncrement( &query->refs );
770 return query;
773 void release_query( struct query *query )
775 if (!InterlockedDecrement( &query->refs )) free_query( query );
778 HRESULT exec_query( enum wbm_namespace ns, const WCHAR *str, IEnumWbemClassObject **result )
780 HRESULT hr;
781 struct query *query;
783 *result = NULL;
784 if (!(query = create_query( ns ))) return E_OUTOFMEMORY;
785 hr = parse_query( ns, str, &query->view, &query->mem );
786 if (hr != S_OK) goto done;
787 hr = execute_view( query->view );
788 if (hr != S_OK) goto done;
789 hr = EnumWbemClassObject_create( query, (void **)result );
791 done:
792 release_query( query );
793 return hr;
796 BOOL is_result_prop( const struct view *view, const WCHAR *name )
798 const struct property *prop = view->proplist;
799 if (!prop) return TRUE;
800 while (prop)
802 if (!wcsicmp( prop->name, name )) return TRUE;
803 prop = prop->next;
805 return FALSE;
808 static BOOL is_system_prop( const WCHAR *name )
810 return (name[0] == '_' && name[1] == '_');
813 static BSTR build_proplist( const struct table *table, UINT row, UINT count, UINT *len )
815 UINT i, j, offset;
816 BSTR *values, ret = NULL;
818 if (!(values = malloc( count * sizeof(BSTR) ))) return NULL;
820 *len = j = 0;
821 for (i = 0; i < table->num_cols; i++)
823 if (table->columns[i].type & COL_FLAG_KEY)
825 const WCHAR *name = table->columns[i].name;
826 values[j] = get_value_bstr( table, row, i );
827 *len += lstrlenW( L"%s=%s" ) + lstrlenW( name ) + lstrlenW( values[j] );
828 j++;
831 if ((ret = SysAllocStringLen( NULL, *len )))
833 offset = j = 0;
834 for (i = 0; i < table->num_cols; i++)
836 if (table->columns[i].type & COL_FLAG_KEY)
838 const WCHAR *name = table->columns[i].name;
839 offset += swprintf( ret + offset, *len - offset, L"%s=%s", name, values[j] );
840 if (j < count - 1) ret[offset++] = ',';
841 j++;
845 for (i = 0; i < count; i++) SysFreeString( values[i] );
846 free( values );
847 return ret;
850 static UINT count_key_columns( const struct table *table )
852 UINT i, num_keys = 0;
854 for (i = 0; i < table->num_cols; i++)
856 if (table->columns[i].type & COL_FLAG_KEY) num_keys++;
858 return num_keys;
861 static BSTR build_relpath( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
863 BSTR class, proplist, ret = NULL;
864 struct table *table = view->table[table_index];
865 UINT row = view->result[result_index];
866 UINT num_keys, len;
868 if (view->proplist) return NULL;
870 if (!(class = SysAllocString( view->table[table_index]->name ))) return NULL;
871 if (!(num_keys = count_key_columns( table ))) return class;
872 if (!(proplist = build_proplist( table, row, num_keys, &len ))) goto done;
874 len += lstrlenW( L"%s.%s" ) + SysStringLen( class );
875 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
876 swprintf( ret, len, L"%s.%s", class, proplist );
878 done:
879 SysFreeString( class );
880 SysFreeString( proplist );
881 return ret;
884 static BSTR build_path( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
886 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
887 UINT len;
889 if (view->proplist) return NULL;
891 if (!(server = build_servername())) return NULL;
892 if (!(namespace = build_namespace())) goto done;
893 if (!(relpath = build_relpath( view, table_index, result_index, name ))) goto done;
895 len = lstrlenW( L"\\\\%s\\%s:%s" ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
896 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
897 swprintf( ret, len, L"\\\\%s\\%s:%s", server, namespace, relpath );
899 done:
900 SysFreeString( server );
901 SysFreeString( namespace );
902 SysFreeString( relpath );
903 return ret;
906 BOOL is_method( const struct table *table, UINT column )
908 return table->columns[column].type & COL_FLAG_METHOD;
911 static UINT count_properties( const struct table *table )
913 UINT i, num_props = 0;
915 for (i = 0; i < table->num_cols; i++)
917 if (!is_method( table, i )) num_props++;
919 return num_props;
922 static UINT count_result_properties( const struct view *view, UINT table_index )
924 const struct property *prop = view->proplist;
925 UINT count;
927 if (!prop) return count_properties( view->table[table_index] );
929 count = 1;
930 while ((prop = prop->next)) count++;
931 return count;
934 static HRESULT get_system_propval( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name,
935 VARIANT *ret, CIMTYPE *type, LONG *flavor )
937 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
939 if (!wcsicmp( name, L"__CLASS" ))
941 if (ret)
943 V_VT( ret ) = VT_BSTR;
944 V_BSTR( ret ) = SysAllocString( view->table[table_index]->name );
946 if (type) *type = CIM_STRING;
947 return S_OK;
949 if (!wcsicmp( name, L"__GENUS" ))
951 if (ret)
953 V_VT( ret ) = VT_I4;
954 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
956 if (type) *type = CIM_SINT32;
957 return S_OK;
959 if (!wcsicmp( name, L"__NAMESPACE" ))
961 if (ret)
963 V_VT( ret ) = VT_BSTR;
964 V_BSTR( ret ) = view->proplist ? NULL : build_namespace();
965 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
967 if (type) *type = CIM_STRING;
968 return S_OK;
970 if (!wcsicmp( name, L"__PATH" ))
972 if (ret)
974 V_VT( ret ) = VT_BSTR;
975 V_BSTR( ret ) = build_path( view, table_index, result_index, name );
976 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
978 if (type) *type = CIM_STRING;
979 return S_OK;
981 if (!wcsicmp( name, L"__PROPERTY_COUNT" ))
983 if (ret)
985 V_VT( ret ) = VT_I4;
986 V_I4( ret ) = count_result_properties( view, table_index );
988 if (type) *type = CIM_SINT32;
989 return S_OK;
991 if (!wcsicmp( name, L"__RELPATH" ))
993 if (ret)
995 V_VT( ret ) = VT_BSTR;
996 V_BSTR( ret ) = build_relpath( view, table_index, result_index, name );
997 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
999 if (type) *type = CIM_STRING;
1000 return S_OK;
1002 if (!wcsicmp( name, L"__SERVER" ))
1004 if (ret)
1006 V_VT( ret ) = VT_BSTR;
1007 V_BSTR( ret ) = view->proplist ? NULL : build_servername();
1008 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
1010 if (type) *type = CIM_STRING;
1011 return S_OK;
1013 if (!wcsicmp( name, L"__DERIVATION" ))
1015 if (ret)
1017 SAFEARRAY *sa;
1018 FIXME( "returning empty array for __DERIVATION\n" );
1019 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, 0 ))) return E_OUTOFMEMORY;
1020 V_VT( ret ) = VT_BSTR | VT_ARRAY;
1021 V_ARRAY( ret ) = sa;
1023 if (type) *type = CIM_STRING | CIM_FLAG_ARRAY;
1024 return S_OK;
1026 FIXME("system property %s not implemented\n", debugstr_w(name));
1027 return WBEM_E_NOT_FOUND;
1030 VARTYPE to_vartype( CIMTYPE type )
1032 switch (type)
1034 case CIM_BOOLEAN: return VT_BOOL;
1036 case CIM_STRING:
1037 case CIM_REFERENCE:
1038 case CIM_DATETIME: return VT_BSTR;
1040 case CIM_SINT8: return VT_I1;
1041 case CIM_UINT8: return VT_UI1;
1042 case CIM_SINT16: return VT_I2;
1044 case CIM_UINT16:
1045 case CIM_SINT32:
1046 case CIM_UINT32: return VT_I4;
1048 case CIM_SINT64: return VT_I8;
1049 case CIM_UINT64: return VT_UI8;
1051 case CIM_REAL32: return VT_R4;
1053 default:
1054 ERR( "unhandled type %lu\n", type );
1055 break;
1057 return 0;
1060 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE basetype )
1062 SAFEARRAY *ret;
1063 VARTYPE vartype = to_vartype( basetype );
1064 LONG i;
1066 if (!array || !(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
1068 for (i = 0; i < array->count; i++)
1070 void *ptr = (char *)array->ptr + i * array->elem_size;
1071 if (vartype == VT_BSTR)
1073 BSTR str = SysAllocString( *(const WCHAR **)ptr );
1074 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
1076 SysFreeString( str );
1077 SafeArrayDestroy( ret );
1078 return NULL;
1080 SysFreeString( str );
1082 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
1084 SafeArrayDestroy( ret );
1085 return NULL;
1088 return ret;
1091 void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
1093 if (type & VT_ARRAY)
1095 V_VT( ret ) = type;
1096 V_ARRAY( ret ) = val_ptr;
1097 return;
1099 switch (type)
1101 case VT_BOOL:
1102 V_BOOL( ret ) = val;
1103 break;
1104 case VT_BSTR:
1105 V_BSTR( ret ) = val_ptr;
1106 break;
1107 case VT_I1:
1108 V_I1( ret ) = val;
1109 break;
1110 case VT_UI1:
1111 V_UI1( ret ) = val;
1112 break;
1113 case VT_I2:
1114 V_I2( ret ) = val;
1115 break;
1116 case VT_UI2:
1117 V_UI2( ret ) = val;
1118 break;
1119 case VT_I4:
1120 V_I4( ret ) = val;
1121 break;
1122 case VT_UI4:
1123 V_UI4( ret ) = val;
1124 break;
1125 case VT_NULL:
1126 break;
1127 case VT_R4:
1128 V_R4( ret ) = *(FLOAT *)&val;
1129 break;
1130 default:
1131 ERR("unhandled variant type %u\n", type);
1132 return;
1134 V_VT( ret ) = type;
1137 static HRESULT map_view_index( const struct view *view, UINT index, UINT *table_index, UINT *result_index )
1139 if (!view->table) return WBEM_E_NOT_FOUND;
1141 switch (view->type)
1143 case VIEW_TYPE_SELECT:
1144 *table_index = 0;
1145 *result_index = index;
1146 break;
1148 case VIEW_TYPE_ASSOCIATORS:
1149 *table_index = *result_index = index;
1150 break;
1152 default:
1153 ERR( "unhandled view type %u\n", view->type );
1154 return WBEM_E_FAILED;
1156 return S_OK;
1159 struct table *get_view_table( const struct view *view, UINT index )
1161 switch (view->type)
1163 case VIEW_TYPE_SELECT:
1164 return view->table[0];
1166 case VIEW_TYPE_ASSOCIATORS:
1167 return view->table[index];
1169 default:
1170 ERR( "unhandled view type %u\n", view->type );
1171 return NULL;
1175 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret, CIMTYPE *type,
1176 LONG *flavor )
1178 HRESULT hr;
1179 UINT column, row, table_index, result_index;
1180 struct table *table;
1181 VARTYPE vartype;
1182 void *val_ptr = NULL;
1183 LONGLONG val;
1185 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1187 if (is_system_prop( name )) return get_system_propval( view, table_index, result_index, name, ret, type, flavor );
1188 if (!view->result_count || !is_result_prop( view, name )) return WBEM_E_NOT_FOUND;
1190 table = view->table[table_index];
1191 hr = get_column_index( table, name, &column );
1192 if (hr != S_OK || is_method( table, column )) return WBEM_E_NOT_FOUND;
1194 row = view->result[result_index];
1195 hr = get_value( table, row, column, &val );
1196 if (hr != S_OK) return hr;
1198 if (type) *type = table->columns[column].type & COL_TYPE_MASK;
1199 if (flavor) *flavor = 0;
1201 if (!ret) return S_OK;
1203 vartype = to_vartype( table->columns[column].type & CIM_TYPE_MASK );
1204 if (table->columns[column].type & CIM_FLAG_ARRAY)
1206 CIMTYPE basetype = table->columns[column].type & CIM_TYPE_MASK;
1208 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
1209 if (!val_ptr) vartype = VT_NULL;
1210 else vartype |= VT_ARRAY;
1211 set_variant( vartype, val, val_ptr, ret );
1212 return S_OK;
1215 switch (table->columns[column].type & COL_TYPE_MASK)
1217 case CIM_STRING:
1218 case CIM_REFERENCE:
1219 case CIM_DATETIME:
1220 if (val)
1222 vartype = VT_BSTR;
1223 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
1225 else
1226 vartype = VT_NULL;
1227 break;
1228 case CIM_SINT64:
1229 vartype = VT_BSTR;
1230 val_ptr = get_value_bstr( table, row, column );
1231 break;
1232 case CIM_UINT64:
1233 vartype = VT_BSTR;
1234 val_ptr = get_value_bstr( table, row, column );
1235 break;
1236 case CIM_BOOLEAN:
1237 case CIM_SINT8:
1238 case CIM_UINT8:
1239 case CIM_SINT16:
1240 case CIM_UINT16:
1241 case CIM_SINT32:
1242 case CIM_UINT32:
1243 case CIM_REAL32:
1244 break;
1245 default:
1246 ERR("unhandled column type %u\n", table->columns[column].type);
1247 return WBEM_E_FAILED;
1250 set_variant( vartype, val, val_ptr, ret );
1251 return S_OK;
1254 static CIMTYPE to_cimtype( VARTYPE type )
1256 switch (type)
1258 case VT_BOOL: return CIM_BOOLEAN;
1259 case VT_BSTR: return CIM_STRING;
1260 case VT_I1: return CIM_SINT8;
1261 case VT_UI1: return CIM_UINT8;
1262 case VT_I2: return CIM_SINT16;
1263 case VT_UI2: return CIM_UINT16;
1264 case VT_I4: return CIM_SINT32;
1265 case VT_UI4: return CIM_UINT32;
1266 case VT_I8: return CIM_SINT64;
1267 case VT_UI8: return CIM_UINT64;
1268 default:
1269 ERR("unhandled type %u\n", type);
1270 break;
1272 return 0;
1275 static struct array *to_array( VARIANT *var, CIMTYPE *type )
1277 struct array *ret;
1278 LONG bound, i;
1279 VARTYPE vartype;
1280 CIMTYPE basetype;
1282 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
1283 if (!(basetype = to_cimtype( vartype ))) return NULL;
1284 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
1285 if (!(ret = malloc( sizeof(struct array) ))) return NULL;
1287 ret->count = bound + 1;
1288 ret->elem_size = get_type_size( basetype );
1289 if (!(ret->ptr = calloc( ret->count, ret->elem_size )))
1291 free( ret );
1292 return NULL;
1294 for (i = 0; i < ret->count; i++)
1296 void *ptr = (char *)ret->ptr + i * ret->elem_size;
1297 if (vartype == VT_BSTR)
1299 BSTR str;
1300 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
1302 destroy_array( ret, basetype );
1303 return NULL;
1305 *(WCHAR **)ptr = wcsdup( str );
1306 SysFreeString( str );
1307 if (!*(WCHAR **)ptr)
1309 destroy_array( ret, basetype );
1310 return NULL;
1313 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
1315 destroy_array( ret, basetype );
1316 return NULL;
1319 *type = basetype | CIM_FLAG_ARRAY;
1320 return ret;
1323 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
1325 if (!var)
1327 *val = 0;
1328 return S_OK;
1330 if (V_VT( var ) & VT_ARRAY)
1332 *val = (INT_PTR)to_array( var, type );
1333 if (!*val) return E_OUTOFMEMORY;
1334 return S_OK;
1336 switch (V_VT( var ))
1338 case VT_BOOL:
1339 *val = V_BOOL( var );
1340 *type = CIM_BOOLEAN;
1341 break;
1342 case VT_BSTR:
1343 *val = (INT_PTR)wcsdup( V_BSTR( var ) );
1344 if (!*val) return E_OUTOFMEMORY;
1345 *type = CIM_STRING;
1346 break;
1347 case VT_I2:
1348 *val = V_I2( var );
1349 *type = CIM_SINT16;
1350 break;
1351 case VT_UI2:
1352 *val = V_UI2( var );
1353 *type = CIM_UINT16;
1354 break;
1355 case VT_I4:
1356 *val = V_I4( var );
1357 *type = CIM_SINT32;
1358 break;
1359 case VT_UI4:
1360 *val = V_UI4( var );
1361 *type = CIM_UINT32;
1362 break;
1363 case VT_NULL:
1364 *val = 0;
1365 break;
1366 default:
1367 ERR("unhandled type %u\n", V_VT( var ));
1368 return WBEM_E_FAILED;
1370 return S_OK;
1373 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
1375 HRESULT hr;
1376 UINT row, column, table_index, result_index;
1377 struct table *table;
1378 LONGLONG val;
1380 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1382 table = view->table[table_index];
1383 hr = get_column_index( table, name, &column );
1384 if (hr != S_OK)
1386 FIXME("no support for creating new properties\n");
1387 return WBEM_E_FAILED;
1389 if (is_method( table, column ) || !(table->columns[column].type & COL_FLAG_DYNAMIC))
1390 return WBEM_E_FAILED;
1392 hr = to_longlong( var, &val, &type );
1393 if (hr != S_OK) return hr;
1395 row = view->result[result_index];
1396 return set_value( table, row, column, val, type );
1399 HRESULT get_properties( const struct view *view, UINT index, LONG flags, SAFEARRAY **props )
1401 static const WCHAR * const system_props[] =
1402 { L"__GENUS", L"__CLASS", L"__RELPATH", L"__PROPERTY_COUNT", L"__DERIVATION", L"__SERVER", L"__NAMESPACE",
1403 L"__PATH" };
1404 SAFEARRAY *sa;
1405 BSTR str;
1406 UINT i, table_index, result_index, count = 0;
1407 struct table *table;
1408 HRESULT hr;
1409 LONG j = 0;
1411 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1412 table = view->table[table_index];
1414 if (!(flags & WBEM_FLAG_NONSYSTEM_ONLY)) count += ARRAY_SIZE(system_props);
1415 if (!(flags & WBEM_FLAG_SYSTEM_ONLY))
1417 for (i = 0; i < table->num_cols; i++)
1419 if (!is_method( table, i ) && is_result_prop( view, table->columns[i].name )) count++;
1423 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, count ))) return E_OUTOFMEMORY;
1425 if (!(flags & WBEM_FLAG_NONSYSTEM_ONLY))
1427 for (j = 0; j < ARRAY_SIZE(system_props); j++)
1429 str = SysAllocString( system_props[j] );
1430 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1432 SysFreeString( str );
1433 SafeArrayDestroy( sa );
1434 return E_OUTOFMEMORY;
1436 SysFreeString( str );
1439 if (!(flags & WBEM_FLAG_SYSTEM_ONLY))
1441 for (i = 0; i < table->num_cols; i++)
1443 if (is_method( table, i ) || !is_result_prop( view, table->columns[i].name )) continue;
1445 str = SysAllocString( table->columns[i].name );
1446 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1448 SysFreeString( str );
1449 SafeArrayDestroy( sa );
1450 return E_OUTOFMEMORY;
1452 SysFreeString( str );
1453 j++;
1457 *props = sa;
1458 return S_OK;