include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / wbemprox / query.c
bloba6599334f586adea496fe9516acddd61c3b12d40
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 = find_table( ns, class );
58 HRESULT hr;
60 if (table && (hr = append_table( view, table )) != S_OK)
62 release_table( table );
63 free( view );
64 return hr;
66 else if (!table && ns == WBEMPROX_NAMESPACE_LAST)
68 free( view );
69 return WBEM_E_INVALID_CLASS;
71 view->proplist = proplist;
72 view->cond = cond;
73 break;
75 default:
76 ERR( "unhandled type %u\n", type );
77 free( view );
78 return E_INVALIDARG;
81 view->type = type;
82 view->ns = ns;
83 *ret = view;
84 return S_OK;
87 void destroy_view( struct view *view )
89 ULONG i;
90 if (!view) return;
91 for (i = 0; i < view->table_count; i++) release_table( view->table[i] );
92 free( view->table );
93 free( view->result );
94 free( view );
97 static BOOL eval_like( const WCHAR *text, const WCHAR *pattern )
99 if (wcsstr( pattern, L"[" )) FIXME( "character ranges (i.e. [abc], [^a-z]) are not supported\n" );
101 while (*text)
103 if (pattern[0] == '\\' && pattern[1] == '\\') pattern++;
105 if (*pattern == '%')
107 while (*pattern == '%') pattern++;
108 if (!*pattern) return TRUE;
109 while (!eval_like( text, pattern ) && *text) text++;
111 return !!*text;
114 if (*pattern != '_' && towupper( *text ) != towupper( *pattern )) return FALSE;
115 text++; pattern++;
118 while (*pattern == '%') pattern++;
120 return *pattern == '\0';
123 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
125 if (!lstr || !rstr)
127 *val = 0;
128 return S_OK;
130 switch (op)
132 case OP_EQ:
133 *val = !wcsicmp( lstr, rstr );
134 break;
135 case OP_GT:
136 *val = wcsicmp( lstr, rstr ) > 0;
137 break;
138 case OP_LT:
139 *val = wcsicmp( lstr, rstr ) < 0;
140 break;
141 case OP_LE:
142 *val = wcsicmp( lstr, rstr ) <= 0;
143 break;
144 case OP_GE:
145 *val = wcsicmp( lstr, rstr ) >= 0;
146 break;
147 case OP_NE:
148 *val = wcsicmp( lstr, rstr );
149 break;
150 case OP_LIKE:
151 *val = eval_like( lstr, rstr );
152 break;
153 default:
154 ERR("unhandled operator %u\n", op);
155 return WBEM_E_INVALID_QUERY;
157 return S_OK;
160 static BOOL is_int( CIMTYPE type )
162 switch (type)
164 case CIM_SINT8:
165 case CIM_SINT16:
166 case CIM_SINT32:
167 case CIM_SINT64:
168 case CIM_UINT8:
169 case CIM_UINT16:
170 case CIM_UINT32:
171 case CIM_UINT64:
172 return TRUE;
173 default:
174 return FALSE;
178 static inline BOOL is_strcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
180 if ((ltype == CIM_STRING || is_int( ltype )) && expr->left->type == EXPR_PROPVAL &&
181 expr->right->type == EXPR_SVAL) return TRUE;
182 else if ((rtype == CIM_STRING || is_int( rtype )) && expr->right->type == EXPR_PROPVAL &&
183 expr->left->type == EXPR_SVAL) return TRUE;
184 return FALSE;
187 static inline BOOL is_boolcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
189 if (ltype == CIM_BOOLEAN && expr->left->type == EXPR_PROPVAL &&
190 (expr->right->type == EXPR_SVAL || expr->right->type == EXPR_BVAL)) return TRUE;
191 else if (rtype == CIM_BOOLEAN && expr->right->type == EXPR_PROPVAL &&
192 (expr->left->type == EXPR_SVAL || expr->left->type == EXPR_BVAL)) return TRUE;
193 return FALSE;
196 static HRESULT eval_boolcmp( UINT op, LONGLONG lval, LONGLONG rval, UINT ltype, UINT rtype, LONGLONG *val )
198 if (ltype == CIM_STRING) lval = !wcsicmp( (const WCHAR *)(INT_PTR)lval, L"True" ) ? -1 : 0;
199 else if (rtype == CIM_STRING) rval = !wcsicmp( (const WCHAR *)(INT_PTR)rval, L"True" ) ? -1 : 0;
201 switch (op)
203 case OP_EQ:
204 *val = (lval == rval);
205 break;
206 case OP_NE:
207 *val = (lval != rval);
208 break;
209 default:
210 ERR("unhandled operator %u\n", op);
211 return WBEM_E_INVALID_QUERY;
213 return S_OK;
216 static inline BOOL is_refcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
218 if (ltype == CIM_REFERENCE && expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) return TRUE;
219 else if (rtype == CIM_REFERENCE && expr->right->type == EXPR_PROPVAL && expr->left->type == EXPR_SVAL) return TRUE;
220 return FALSE;
223 static HRESULT eval_refcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
225 if (!lstr || !rstr)
227 *val = 0;
228 return S_OK;
230 switch (op)
232 case OP_EQ:
233 *val = !wcsicmp( lstr, rstr );
234 break;
235 case OP_NE:
236 *val = wcsicmp( lstr, rstr );
237 break;
238 default:
239 ERR("unhandled operator %u\n", op);
240 return WBEM_E_INVALID_QUERY;
242 return S_OK;
245 static UINT resolve_type( UINT left, UINT right )
247 switch (left)
249 case CIM_SINT8:
250 case CIM_SINT16:
251 case CIM_SINT32:
252 case CIM_SINT64:
253 case CIM_UINT8:
254 case CIM_UINT16:
255 case CIM_UINT32:
256 case CIM_UINT64:
257 switch (right)
259 case CIM_SINT8:
260 case CIM_SINT16:
261 case CIM_SINT32:
262 case CIM_SINT64:
263 case CIM_UINT8:
264 case CIM_UINT16:
265 case CIM_UINT32:
266 case CIM_UINT64:
267 return CIM_UINT64;
268 default: break;
270 break;
272 case CIM_STRING:
273 if (right == CIM_STRING) return CIM_STRING;
274 break;
276 case CIM_BOOLEAN:
277 if (right == CIM_BOOLEAN) return CIM_BOOLEAN;
278 break;
280 case CIM_REFERENCE:
281 if (right == CIM_REFERENCE) return CIM_REFERENCE;
282 break;
284 default:
285 break;
287 return CIM_ILLEGAL;
290 static const WCHAR *format_int( WCHAR *buf, UINT len, CIMTYPE type, LONGLONG val )
292 switch (type)
294 case CIM_SINT8:
295 case CIM_SINT16:
296 case CIM_SINT32:
297 swprintf( buf, len, L"%d", val );
298 return buf;
300 case CIM_UINT8:
301 case CIM_UINT16:
302 case CIM_UINT32:
303 swprintf( buf, len, L"%u", val );
304 return buf;
306 case CIM_SINT64:
307 wsprintfW( buf, L"%I64d", val );
308 return buf;
310 case CIM_UINT64:
311 wsprintfW( buf, L"%I64u", val );
312 return buf;
314 default:
315 ERR( "unhandled type %lu\n", type );
316 return NULL;
320 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
321 LONGLONG *val, UINT *type )
323 HRESULT lret, rret;
324 LONGLONG lval, rval;
325 UINT ltype, rtype;
327 lret = eval_cond( table, row, expr->left, &lval, &ltype );
328 rret = eval_cond( table, row, expr->right, &rval, &rtype );
329 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
331 *type = resolve_type( ltype, rtype );
333 if (is_strcmp( expr, ltype, rtype ))
335 const WCHAR *lstr, *rstr;
336 WCHAR lbuf[21], rbuf[21];
338 if (is_int( ltype )) lstr = format_int( lbuf, ARRAY_SIZE( lbuf ), ltype, lval );
339 else lstr = (const WCHAR *)(INT_PTR)lval;
341 if (is_int( rtype )) rstr = format_int( rbuf, ARRAY_SIZE( rbuf ), rtype, rval );
342 else rstr = (const WCHAR *)(INT_PTR)rval;
344 return eval_strcmp( expr->op, lstr, rstr, val );
346 if (is_boolcmp( expr, ltype, rtype ))
348 return eval_boolcmp( expr->op, lval, rval, ltype, rtype, val );
350 if (is_refcmp( expr, ltype, rtype ))
352 return eval_refcmp( expr->op, (const WCHAR *)(INT_PTR)lval, (const WCHAR *)(INT_PTR)rval, val );
355 switch (expr->op)
357 case OP_EQ:
358 *val = (lval == rval);
359 break;
360 case OP_AND:
361 *val = (lval && rval);
362 break;
363 case OP_OR:
364 *val = (lval || rval);
365 break;
366 case OP_GT:
367 *val = (lval > rval);
368 break;
369 case OP_LT:
370 *val = (lval < rval);
371 break;
372 case OP_LE:
373 *val = (lval <= rval);
374 break;
375 case OP_GE:
376 *val = (lval >= rval);
377 break;
378 case OP_NE:
379 *val = (lval != rval);
380 break;
381 default:
382 ERR("unhandled operator %u\n", expr->op);
383 return WBEM_E_INVALID_QUERY;
385 return S_OK;
388 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
389 LONGLONG *val, UINT *type )
392 HRESULT hr;
393 UINT column;
394 LONGLONG lval;
396 if (expr->op == OP_NOT)
398 hr = eval_cond( table, row, expr->left, &lval, type );
399 if (hr != S_OK)
400 return hr;
401 *val = !lval;
402 return S_OK;
405 hr = get_column_index( table, expr->left->u.propval->name, &column );
406 if (hr != S_OK)
407 return hr;
409 hr = get_value( table, row, column, &lval );
410 if (hr != S_OK)
411 return hr;
413 switch (expr->op)
415 case OP_ISNULL:
416 *val = !lval;
417 break;
418 case OP_NOTNULL:
419 *val = lval;
420 break;
421 default:
422 ERR("unknown operator %u\n", expr->op);
423 return WBEM_E_INVALID_QUERY;
426 *type = table->columns[column].type & CIM_TYPE_MASK;
427 return S_OK;
430 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
431 LONGLONG *val, UINT *type )
434 HRESULT hr;
435 UINT column;
437 hr = get_column_index( table, propval->name, &column );
438 if (hr != S_OK)
439 return hr;
441 *type = table->columns[column].type & CIM_TYPE_MASK;
442 return get_value( table, row, column, val );
445 HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond, LONGLONG *val, UINT *type )
447 if (!cond)
449 *val = 1;
450 *type = CIM_UINT64;
451 return S_OK;
453 switch (cond->type)
455 case EXPR_COMPLEX:
456 return eval_binary( table, row, &cond->u.expr, val, type );
458 case EXPR_UNARY:
459 return eval_unary( table, row, &cond->u.expr, val, type );
461 case EXPR_PROPVAL:
462 return eval_propval( table, row, cond->u.propval, val, type );
464 case EXPR_SVAL:
465 *val = (INT_PTR)cond->u.sval;
466 *type = CIM_STRING;
467 return S_OK;
469 case EXPR_IVAL:
470 *val = cond->u.ival;
471 *type = CIM_UINT64;
472 return S_OK;
474 case EXPR_BVAL:
475 *val = cond->u.ival;
476 *type = CIM_BOOLEAN;
477 return S_OK;
479 default:
480 ERR("invalid expression type\n");
481 break;
483 return WBEM_E_INVALID_QUERY;
486 static WCHAR *build_assoc_query( const WCHAR *class, UINT class_len )
488 static const WCHAR fmtW[] = L"SELECT * FROM __ASSOCIATORS WHERE Class='%s'";
489 UINT len = class_len + ARRAY_SIZE(fmtW);
490 WCHAR *ret;
492 if (!(ret = malloc( len * sizeof(WCHAR) ))) return NULL;
493 swprintf( ret, len, fmtW, class );
494 return ret;
497 static HRESULT create_assoc_enum( enum wbm_namespace ns, const WCHAR *class, UINT class_len,
498 IEnumWbemClassObject **iter )
500 WCHAR *query;
501 HRESULT hr;
503 if (!(query = build_assoc_query( class, class_len ))) return E_OUTOFMEMORY;
504 hr = exec_query( ns, query, iter );
505 free( query );
506 return hr;
509 static WCHAR *build_antecedent_query( const WCHAR *assocclass, const WCHAR *dependent )
511 static const WCHAR fmtW[] = L"SELECT Antecedent FROM %s WHERE Dependent='%s'";
512 UINT len = lstrlenW(assocclass) + lstrlenW(dependent) + ARRAY_SIZE(fmtW);
513 WCHAR *ret;
515 if (!(ret = malloc( len * sizeof(WCHAR) ))) return NULL;
516 swprintf( ret, len, fmtW, assocclass, dependent );
517 return ret;
520 static BSTR build_servername(void)
522 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
523 DWORD len = ARRAY_SIZE( server );
525 if (!(GetComputerNameW( server, &len ))) return NULL;
526 for (p = server; *p; p++) *p = towupper( *p );
527 return SysAllocString( server );
530 static BSTR build_namespace(void)
532 return SysAllocString( L"ROOT\\CIMV2" );
535 static WCHAR *build_canonical_path( const WCHAR *relpath )
537 BSTR server, namespace;
538 WCHAR *ret;
539 UINT len, i;
541 if (!(server = build_servername())) return NULL;
542 if (!(namespace = build_namespace()))
544 SysFreeString( server );
545 return NULL;
548 len = ARRAY_SIZE( L"\\\\%s\\%s:" ) + SysStringLen( server ) + SysStringLen( namespace ) + lstrlenW( relpath );
549 if ((ret = malloc( len * sizeof(WCHAR ) )))
551 len = swprintf( ret, len, L"\\\\%s\\%s:", server, namespace );
552 for (i = 0; i < lstrlenW( relpath ); i ++)
554 if (relpath[i] == '\'') ret[len++] = '"';
555 else ret[len++] = relpath[i];
557 ret[len] = 0;
560 SysFreeString( server );
561 SysFreeString( namespace );
562 return ret;
565 static HRESULT get_antecedent( enum wbm_namespace ns, const WCHAR *assocclass, const WCHAR *dependent, BSTR *ret )
567 WCHAR *fullpath, *str;
568 IEnumWbemClassObject *iter = NULL;
569 IWbemClassObject *obj;
570 HRESULT hr = E_OUTOFMEMORY;
571 ULONG count;
572 VARIANT var;
574 if (!(fullpath = build_canonical_path( dependent ))) return E_OUTOFMEMORY;
575 if (!(str = build_antecedent_query( assocclass, fullpath ))) goto done;
576 if ((hr = exec_query( ns, str, &iter )) != S_OK) goto done;
578 IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
579 if (!count)
581 *ret = NULL;
582 goto done;
585 hr = IWbemClassObject_Get( obj, L"Antecedent", 0, &var, NULL, NULL );
586 IWbemClassObject_Release( obj );
587 if (hr != S_OK) goto done;
588 *ret = V_BSTR( &var );
590 done:
591 if (iter) IEnumWbemClassObject_Release( iter );
592 free( str );
593 free( fullpath );
594 return hr;
597 static HRESULT do_query( enum wbm_namespace ns, const WCHAR *str, struct query **ret_query )
599 struct query *query;
600 HRESULT hr;
602 if (!(query = create_query( ns ))) return E_OUTOFMEMORY;
603 if ((hr = parse_query( ns, str, &query->view, &query->mem )) != S_OK || (hr = execute_view( query->view )) != S_OK)
605 release_query( query );
606 return hr;
608 *ret_query = query;
609 return S_OK;
612 static HRESULT get_antecedent_table( enum wbm_namespace ns, const WCHAR *assocclass, const WCHAR *dependent,
613 struct table **table )
615 BSTR antecedent = NULL;
616 struct path *path = NULL;
617 WCHAR *str = NULL;
618 struct query *query = NULL;
619 HRESULT hr;
621 if ((hr = get_antecedent( ns, assocclass, dependent, &antecedent )) != S_OK) return hr;
622 if (!antecedent)
624 *table = NULL;
625 return S_OK;
627 if ((hr = parse_path( antecedent, &path )) != S_OK) goto done;
628 if (!(str = query_from_path( path )))
630 hr = E_OUTOFMEMORY;
631 goto done;
634 if ((hr = do_query( ns, str, &query )) != S_OK) goto done;
635 if (query->view->table_count) *table = grab_table( query->view->table[0] );
636 else *table = NULL;
638 done:
639 if (query) release_query( query );
640 free_path( path );
641 SysFreeString( antecedent );
642 free( str );
643 return hr;
646 static HRESULT exec_assoc_view( struct view *view )
648 IEnumWbemClassObject *iter = NULL;
649 struct path *path;
650 HRESULT hr;
652 if (view->keywordlist) FIXME( "ignoring keywords\n" );
653 if ((hr = parse_path( view->path, &path )) != S_OK) return hr;
655 if ((hr = create_assoc_enum( view->ns, path->class, path->class_len, &iter )) != S_OK) goto done;
656 for (;;)
658 ULONG count;
659 IWbemClassObject *obj;
660 struct table *table;
661 VARIANT var;
663 IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &obj, &count );
664 if (!count) break;
666 if ((hr = IWbemClassObject_Get( obj, L"AssocClass", 0, &var, NULL, NULL )) != S_OK)
668 IWbemClassObject_Release( obj );
669 goto done;
671 IWbemClassObject_Release( obj );
673 hr = get_antecedent_table( view->ns, V_BSTR(&var), view->path, &table );
674 VariantClear( &var );
675 if (hr != S_OK) goto done;
677 if (table && (hr = append_table( view, table )) != S_OK)
679 release_table( table );
680 goto done;
684 if (view->table_count)
686 if (!(view->result = calloc( view->table_count, sizeof(UINT) ))) hr = E_OUTOFMEMORY;
687 else view->result_count = view->table_count;
690 done:
691 if (iter) IEnumWbemClassObject_Release( iter );
692 free_path( path );
693 return hr;
696 static HRESULT exec_select_view( struct view *view )
698 UINT i, j = 0, len;
699 enum fill_status status = FILL_STATUS_UNFILTERED;
700 struct table *table;
702 if (!view->table_count) return S_OK;
704 table = view->table[0];
705 if (table->fill)
707 clear_table( table );
708 status = table->fill( table, view->cond );
710 if (status == FILL_STATUS_FAILED) return WBEM_E_FAILED;
711 if (!table->num_rows) return S_OK;
713 len = min( table->num_rows, 16 );
714 if (!(view->result = malloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
716 for (i = 0; i < table->num_rows; i++)
718 HRESULT hr;
719 LONGLONG val = 0;
720 UINT type;
722 if (j >= len)
724 UINT *tmp;
725 len *= 2;
726 if (!(tmp = realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
727 view->result = tmp;
729 if (status == FILL_STATUS_FILTERED) val = 1;
730 else if ((hr = eval_cond( table, i, view->cond, &val, &type )) != S_OK) return hr;
731 if (val) view->result[j++] = i;
734 view->result_count = j;
735 return S_OK;
738 HRESULT execute_view( struct view *view )
740 switch (view->type)
742 case VIEW_TYPE_ASSOCIATORS:
743 return exec_assoc_view( view );
745 case VIEW_TYPE_SELECT:
746 return exec_select_view( view );
748 default:
749 ERR( "unhandled type %u\n", view->type );
750 return E_INVALIDARG;
754 struct query *create_query( enum wbm_namespace ns )
756 struct query *query;
758 if (!(query = malloc( sizeof(*query) ))) return NULL;
759 list_init( &query->mem );
760 query->ns = ns;
761 query->refs = 1;
762 return query;
765 void free_query( struct query *query )
767 struct list *mem, *next;
769 if (!query) return;
770 destroy_view( query->view );
771 LIST_FOR_EACH_SAFE( mem, next, &query->mem ) { free( mem ); }
772 free( query );
775 struct query *addref_query( struct query *query )
777 InterlockedIncrement( &query->refs );
778 return query;
781 void release_query( struct query *query )
783 if (!InterlockedDecrement( &query->refs )) free_query( query );
786 HRESULT exec_query( enum wbm_namespace ns, const WCHAR *str, IEnumWbemClassObject **result )
788 HRESULT hr;
789 struct query *query;
791 *result = NULL;
792 if (!(query = create_query( ns ))) return E_OUTOFMEMORY;
793 hr = parse_query( ns, str, &query->view, &query->mem );
794 if (hr != S_OK) goto done;
795 hr = execute_view( query->view );
796 if (hr != S_OK) goto done;
797 hr = EnumWbemClassObject_create( query, (void **)result );
799 done:
800 release_query( query );
801 return hr;
804 BOOL is_result_prop( const struct view *view, const WCHAR *name )
806 const struct property *prop = view->proplist;
807 if (!prop) return TRUE;
808 while (prop)
810 if (!wcsicmp( prop->name, name )) return TRUE;
811 prop = prop->next;
813 return FALSE;
816 static BOOL is_system_prop( const WCHAR *name )
818 return (name[0] == '_' && name[1] == '_');
821 static BSTR build_proplist( const struct table *table, UINT row, UINT count, UINT *len )
823 UINT i, j, offset;
824 BSTR *values, ret = NULL;
826 if (!(values = malloc( count * sizeof(BSTR) ))) return NULL;
828 *len = j = 0;
829 for (i = 0; i < table->num_cols; i++)
831 if (table->columns[i].type & COL_FLAG_KEY)
833 const WCHAR *name = table->columns[i].name;
834 values[j] = get_value_bstr( table, row, i );
835 *len += lstrlenW( L"%s=%s" ) + lstrlenW( name ) + lstrlenW( values[j] );
836 j++;
839 if ((ret = SysAllocStringLen( NULL, *len )))
841 offset = j = 0;
842 for (i = 0; i < table->num_cols; i++)
844 if (table->columns[i].type & COL_FLAG_KEY)
846 const WCHAR *name = table->columns[i].name;
847 offset += swprintf( ret + offset, *len - offset, L"%s=%s", name, values[j] );
848 if (j < count - 1) ret[offset++] = ',';
849 j++;
853 for (i = 0; i < count; i++) SysFreeString( values[i] );
854 free( values );
855 return ret;
858 static UINT count_key_columns( const struct table *table )
860 UINT i, num_keys = 0;
862 for (i = 0; i < table->num_cols; i++)
864 if (table->columns[i].type & COL_FLAG_KEY) num_keys++;
866 return num_keys;
869 static BSTR build_relpath( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
871 BSTR class, proplist, ret = NULL;
872 struct table *table = view->table[table_index];
873 UINT row = view->result[result_index];
874 UINT num_keys, len;
876 if (view->proplist) return NULL;
878 if (!(class = SysAllocString( view->table[table_index]->name ))) return NULL;
879 if (!(num_keys = count_key_columns( table ))) return class;
880 if (!(proplist = build_proplist( table, row, num_keys, &len ))) goto done;
882 len += lstrlenW( L"%s.%s" ) + SysStringLen( class );
883 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
884 swprintf( ret, len, L"%s.%s", class, proplist );
886 done:
887 SysFreeString( class );
888 SysFreeString( proplist );
889 return ret;
892 static BSTR build_path( const struct view *view, UINT table_index, UINT result_index, const WCHAR *name )
894 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
895 UINT len;
897 if (view->proplist) return NULL;
899 if (!(server = build_servername())) return NULL;
900 if (!(namespace = build_namespace())) goto done;
901 if (!(relpath = build_relpath( view, table_index, result_index, name ))) goto done;
903 len = lstrlenW( L"\\\\%s\\%s:%s" ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
904 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
905 swprintf( ret, len, L"\\\\%s\\%s:%s", server, namespace, relpath );
907 done:
908 SysFreeString( server );
909 SysFreeString( namespace );
910 SysFreeString( relpath );
911 return ret;
914 BOOL is_method( const struct table *table, UINT column )
916 return table->columns[column].type & COL_FLAG_METHOD;
919 static UINT count_properties( const struct table *table )
921 UINT i, num_props = 0;
923 for (i = 0; i < table->num_cols; i++)
925 if (!is_method( table, i )) num_props++;
927 return num_props;
930 static UINT count_result_properties( const struct view *view, UINT table_index )
932 const struct property *prop = view->proplist;
933 UINT count;
935 if (!prop) return count_properties( view->table[table_index] );
937 count = 1;
938 while ((prop = prop->next)) count++;
939 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 )
945 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
947 if (!wcsicmp( name, L"__CLASS" ))
949 if (ret)
951 V_VT( ret ) = VT_BSTR;
952 V_BSTR( ret ) = SysAllocString( view->table[table_index]->name );
954 if (type) *type = CIM_STRING;
955 return S_OK;
957 if (!wcsicmp( name, L"__GENUS" ))
959 if (ret)
961 V_VT( ret ) = VT_I4;
962 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
964 if (type) *type = CIM_SINT32;
965 return S_OK;
967 if (!wcsicmp( name, L"__NAMESPACE" ))
969 if (ret)
971 V_VT( ret ) = VT_BSTR;
972 V_BSTR( ret ) = view->proplist ? NULL : build_namespace();
973 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
975 if (type) *type = CIM_STRING;
976 return S_OK;
978 if (!wcsicmp( name, L"__PATH" ))
980 if (ret)
982 V_VT( ret ) = VT_BSTR;
983 V_BSTR( ret ) = build_path( view, table_index, result_index, name );
984 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
986 if (type) *type = CIM_STRING;
987 return S_OK;
989 if (!wcsicmp( name, L"__PROPERTY_COUNT" ))
991 if (ret)
993 V_VT( ret ) = VT_I4;
994 V_I4( ret ) = count_result_properties( view, table_index );
996 if (type) *type = CIM_SINT32;
997 return S_OK;
999 if (!wcsicmp( name, L"__RELPATH" ))
1001 if (ret)
1003 V_VT( ret ) = VT_BSTR;
1004 V_BSTR( ret ) = build_relpath( view, table_index, result_index, name );
1005 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
1007 if (type) *type = CIM_STRING;
1008 return S_OK;
1010 if (!wcsicmp( name, L"__SERVER" ))
1012 if (ret)
1014 V_VT( ret ) = VT_BSTR;
1015 V_BSTR( ret ) = view->proplist ? NULL : build_servername();
1016 if (!V_BSTR( ret )) V_VT( ret ) = VT_NULL;
1018 if (type) *type = CIM_STRING;
1019 return S_OK;
1021 if (!wcsicmp( name, L"__DERIVATION" ))
1023 if (ret)
1025 SAFEARRAY *sa;
1026 FIXME( "returning empty array for __DERIVATION\n" );
1027 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, 0 ))) return E_OUTOFMEMORY;
1028 V_VT( ret ) = VT_BSTR | VT_ARRAY;
1029 V_ARRAY( ret ) = sa;
1031 if (type) *type = CIM_STRING | CIM_FLAG_ARRAY;
1032 return S_OK;
1034 FIXME("system property %s not implemented\n", debugstr_w(name));
1035 return WBEM_E_NOT_FOUND;
1038 VARTYPE to_vartype( CIMTYPE type )
1040 switch (type)
1042 case CIM_BOOLEAN: return VT_BOOL;
1044 case CIM_STRING:
1045 case CIM_REFERENCE:
1046 case CIM_DATETIME: return VT_BSTR;
1048 case CIM_SINT8: return VT_I1;
1049 case CIM_UINT8: return VT_UI1;
1050 case CIM_SINT16: return VT_I2;
1052 case CIM_UINT16:
1053 case CIM_SINT32:
1054 case CIM_UINT32: return VT_I4;
1056 case CIM_SINT64: return VT_I8;
1057 case CIM_UINT64: return VT_UI8;
1059 case CIM_REAL32: return VT_R4;
1061 default:
1062 ERR( "unhandled type %lu\n", type );
1063 break;
1065 return 0;
1068 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE basetype )
1070 SAFEARRAY *ret;
1071 VARTYPE vartype = to_vartype( basetype );
1072 LONG i;
1074 if (!array || !(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
1076 for (i = 0; i < array->count; i++)
1078 void *ptr = (char *)array->ptr + i * array->elem_size;
1079 if (vartype == VT_BSTR)
1081 BSTR str = SysAllocString( *(const WCHAR **)ptr );
1082 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
1084 SysFreeString( str );
1085 SafeArrayDestroy( ret );
1086 return NULL;
1088 SysFreeString( str );
1090 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
1092 SafeArrayDestroy( ret );
1093 return NULL;
1096 return ret;
1099 void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
1101 if (type & VT_ARRAY)
1103 V_VT( ret ) = type;
1104 V_ARRAY( ret ) = val_ptr;
1105 return;
1107 switch (type)
1109 case VT_BOOL:
1110 V_BOOL( ret ) = val;
1111 break;
1112 case VT_BSTR:
1113 V_BSTR( ret ) = val_ptr;
1114 break;
1115 case VT_I1:
1116 V_I1( ret ) = val;
1117 break;
1118 case VT_UI1:
1119 V_UI1( ret ) = val;
1120 break;
1121 case VT_I2:
1122 V_I2( ret ) = val;
1123 break;
1124 case VT_UI2:
1125 V_UI2( ret ) = val;
1126 break;
1127 case VT_I4:
1128 V_I4( ret ) = val;
1129 break;
1130 case VT_UI4:
1131 V_UI4( ret ) = val;
1132 break;
1133 case VT_NULL:
1134 break;
1135 case VT_R4:
1136 V_R4( ret ) = *(FLOAT *)&val;
1137 break;
1138 default:
1139 ERR("unhandled variant type %u\n", type);
1140 return;
1142 V_VT( ret ) = type;
1145 static HRESULT map_view_index( const struct view *view, UINT index, UINT *table_index, UINT *result_index )
1147 if (!view->table) return WBEM_E_NOT_FOUND;
1149 switch (view->type)
1151 case VIEW_TYPE_SELECT:
1152 *table_index = 0;
1153 *result_index = index;
1154 break;
1156 case VIEW_TYPE_ASSOCIATORS:
1157 *table_index = *result_index = index;
1158 break;
1160 default:
1161 ERR( "unhandled view type %u\n", view->type );
1162 return WBEM_E_FAILED;
1164 return S_OK;
1167 struct table *get_view_table( const struct view *view, UINT index )
1169 switch (view->type)
1171 case VIEW_TYPE_SELECT:
1172 return view->table[0];
1174 case VIEW_TYPE_ASSOCIATORS:
1175 return view->table[index];
1177 default:
1178 ERR( "unhandled view type %u\n", view->type );
1179 return NULL;
1183 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret, CIMTYPE *type,
1184 LONG *flavor )
1186 HRESULT hr;
1187 UINT column, row, table_index, result_index;
1188 struct table *table;
1189 VARTYPE vartype;
1190 void *val_ptr = NULL;
1191 LONGLONG val;
1193 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1195 if (is_system_prop( name )) return get_system_propval( view, table_index, result_index, name, ret, type, flavor );
1196 if (!view->result_count || !is_result_prop( view, name )) return WBEM_E_NOT_FOUND;
1198 table = view->table[table_index];
1199 hr = get_column_index( table, name, &column );
1200 if (hr != S_OK || is_method( table, column )) return WBEM_E_NOT_FOUND;
1202 row = view->result[result_index];
1203 hr = get_value( table, row, column, &val );
1204 if (hr != S_OK) return hr;
1206 if (type) *type = table->columns[column].type & COL_TYPE_MASK;
1207 if (flavor) *flavor = 0;
1209 if (!ret) return S_OK;
1211 vartype = to_vartype( table->columns[column].type & CIM_TYPE_MASK );
1212 if (table->columns[column].type & CIM_FLAG_ARRAY)
1214 CIMTYPE basetype = table->columns[column].type & CIM_TYPE_MASK;
1216 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
1217 if (!val_ptr) vartype = VT_NULL;
1218 else vartype |= VT_ARRAY;
1219 set_variant( vartype, val, val_ptr, ret );
1220 return S_OK;
1223 switch (table->columns[column].type & COL_TYPE_MASK)
1225 case CIM_STRING:
1226 case CIM_REFERENCE:
1227 case CIM_DATETIME:
1228 if (val)
1230 vartype = VT_BSTR;
1231 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
1233 else
1234 vartype = VT_NULL;
1235 break;
1236 case CIM_SINT64:
1237 vartype = VT_BSTR;
1238 val_ptr = get_value_bstr( table, row, column );
1239 break;
1240 case CIM_UINT64:
1241 vartype = VT_BSTR;
1242 val_ptr = get_value_bstr( table, row, column );
1243 break;
1244 case CIM_BOOLEAN:
1245 case CIM_SINT8:
1246 case CIM_UINT8:
1247 case CIM_SINT16:
1248 case CIM_UINT16:
1249 case CIM_SINT32:
1250 case CIM_UINT32:
1251 case CIM_REAL32:
1252 break;
1253 default:
1254 ERR("unhandled column type %u\n", table->columns[column].type);
1255 return WBEM_E_FAILED;
1258 set_variant( vartype, val, val_ptr, ret );
1259 return S_OK;
1262 static CIMTYPE to_cimtype( VARTYPE type )
1264 switch (type)
1266 case VT_BOOL: return CIM_BOOLEAN;
1267 case VT_BSTR: return CIM_STRING;
1268 case VT_I1: return CIM_SINT8;
1269 case VT_UI1: return CIM_UINT8;
1270 case VT_I2: return CIM_SINT16;
1271 case VT_UI2: return CIM_UINT16;
1272 case VT_I4: return CIM_SINT32;
1273 case VT_UI4: return CIM_UINT32;
1274 case VT_I8: return CIM_SINT64;
1275 case VT_UI8: return CIM_UINT64;
1276 default:
1277 ERR("unhandled type %u\n", type);
1278 break;
1280 return 0;
1283 static struct array *to_array( VARIANT *var, CIMTYPE *type )
1285 struct array *ret;
1286 LONG bound, i;
1287 VARTYPE vartype;
1288 CIMTYPE basetype;
1290 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
1291 if (!(basetype = to_cimtype( vartype ))) return NULL;
1292 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
1293 if (!(ret = malloc( sizeof(struct array) ))) return NULL;
1295 ret->count = bound + 1;
1296 ret->elem_size = get_type_size( basetype );
1297 if (!(ret->ptr = calloc( ret->count, ret->elem_size )))
1299 free( ret );
1300 return NULL;
1302 for (i = 0; i < ret->count; i++)
1304 void *ptr = (char *)ret->ptr + i * ret->elem_size;
1305 if (vartype == VT_BSTR)
1307 BSTR str;
1308 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
1310 destroy_array( ret, basetype );
1311 return NULL;
1313 *(WCHAR **)ptr = wcsdup( str );
1314 SysFreeString( str );
1315 if (!*(WCHAR **)ptr)
1317 destroy_array( ret, basetype );
1318 return NULL;
1321 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
1323 destroy_array( ret, basetype );
1324 return NULL;
1327 *type = basetype | CIM_FLAG_ARRAY;
1328 return ret;
1331 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
1333 if (!var)
1335 *val = 0;
1336 return S_OK;
1338 if (V_VT( var ) & VT_ARRAY)
1340 *val = (INT_PTR)to_array( var, type );
1341 if (!*val) return E_OUTOFMEMORY;
1342 return S_OK;
1344 switch (V_VT( var ))
1346 case VT_BOOL:
1347 *val = V_BOOL( var );
1348 *type = CIM_BOOLEAN;
1349 break;
1350 case VT_BSTR:
1351 *val = (INT_PTR)wcsdup( V_BSTR( var ) );
1352 if (!*val) return E_OUTOFMEMORY;
1353 *type = CIM_STRING;
1354 break;
1355 case VT_I2:
1356 *val = V_I2( var );
1357 *type = CIM_SINT16;
1358 break;
1359 case VT_UI2:
1360 *val = V_UI2( var );
1361 *type = CIM_UINT16;
1362 break;
1363 case VT_I4:
1364 *val = V_I4( var );
1365 *type = CIM_SINT32;
1366 break;
1367 case VT_UI4:
1368 *val = V_UI4( var );
1369 *type = CIM_UINT32;
1370 break;
1371 case VT_NULL:
1372 *val = 0;
1373 break;
1374 default:
1375 ERR("unhandled type %u\n", V_VT( var ));
1376 return WBEM_E_FAILED;
1378 return S_OK;
1381 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
1383 HRESULT hr;
1384 UINT row, column, table_index, result_index;
1385 struct table *table;
1386 LONGLONG val;
1388 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1390 table = view->table[table_index];
1391 hr = get_column_index( table, name, &column );
1392 if (hr != S_OK)
1394 FIXME("no support for creating new properties\n");
1395 return WBEM_E_FAILED;
1397 if (is_method( table, column ) || !(table->columns[column].type & COL_FLAG_DYNAMIC))
1398 return WBEM_E_FAILED;
1400 hr = to_longlong( var, &val, &type );
1401 if (hr != S_OK) return hr;
1403 row = view->result[result_index];
1404 return set_value( table, row, column, val, type );
1407 HRESULT get_properties( const struct view *view, UINT index, LONG flags, SAFEARRAY **props )
1409 static const WCHAR * const system_props[] =
1410 { L"__GENUS", L"__CLASS", L"__RELPATH", L"__PROPERTY_COUNT", L"__DERIVATION", L"__SERVER", L"__NAMESPACE",
1411 L"__PATH" };
1412 SAFEARRAY *sa;
1413 BSTR str;
1414 UINT i, table_index, result_index, count = 0;
1415 struct table *table;
1416 HRESULT hr;
1417 LONG j = 0;
1419 if ((hr = map_view_index( view, index, &table_index, &result_index )) != S_OK) return hr;
1420 table = view->table[table_index];
1422 if (!(flags & WBEM_FLAG_NONSYSTEM_ONLY)) count += ARRAY_SIZE(system_props);
1423 if (!(flags & WBEM_FLAG_SYSTEM_ONLY))
1425 for (i = 0; i < table->num_cols; i++)
1427 if (!is_method( table, i ) && is_result_prop( view, table->columns[i].name )) count++;
1431 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, count ))) return E_OUTOFMEMORY;
1433 if (!(flags & WBEM_FLAG_NONSYSTEM_ONLY))
1435 for (j = 0; j < ARRAY_SIZE(system_props); j++)
1437 str = SysAllocString( system_props[j] );
1438 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1440 SysFreeString( str );
1441 SafeArrayDestroy( sa );
1442 return E_OUTOFMEMORY;
1444 SysFreeString( str );
1447 if (!(flags & WBEM_FLAG_SYSTEM_ONLY))
1449 for (i = 0; i < table->num_cols; i++)
1451 if (is_method( table, i ) || !is_result_prop( view, table->columns[i].name )) continue;
1453 str = SysAllocString( table->columns[i].name );
1454 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1456 SysFreeString( str );
1457 SafeArrayDestroy( sa );
1458 return E_OUTOFMEMORY;
1460 SysFreeString( str );
1461 j++;
1465 *props = sa;
1466 return S_OK;