qedit: Remove an unused and wrong assignment (PVS-Studio).
[wine/multimedia.git] / dlls / wbemprox / query.c
blob7ae9a0f1884bd342c1ef6276d8bc3296d3341bff
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 "config.h"
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wbemcli.h"
28 #include "wine/debug.h"
29 #include "wbemprox_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
33 HRESULT create_view( const struct property *proplist, const WCHAR *class,
34 const struct expr *cond, struct view **ret )
36 struct view *view = heap_alloc( sizeof(struct view) );
38 if (!view) return E_OUTOFMEMORY;
39 view->proplist = proplist;
40 view->table = grab_table( class );
41 view->cond = cond;
42 view->result = NULL;
43 view->count = 0;
44 *ret = view;
45 return S_OK;
48 void destroy_view( struct view *view )
50 if (!view) return;
51 if (view->table) release_table( view->table );
52 heap_free( view->result );
53 heap_free( view );
56 static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
58 const WCHAR *p = lstr, *q = rstr;
60 while (*p && *q)
62 if (*q == '%')
64 while (*q == '%') q++;
65 if (!*q) return TRUE;
66 while (*p && toupperW( p[1] ) != toupperW( q[1] )) p++;
67 if (!*p) return TRUE;
69 if (toupperW( *p++ ) != toupperW( *q++ )) return FALSE;
71 return TRUE;
74 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
76 if (!lstr || !rstr)
78 *val = 0;
79 return S_OK;
81 switch (op)
83 case OP_EQ:
84 *val = !strcmpW( lstr, rstr );
85 break;
86 case OP_GT:
87 *val = strcmpW( lstr, rstr ) > 0;
88 break;
89 case OP_LT:
90 *val = strcmpW( lstr, rstr ) < 0;
91 break;
92 case OP_LE:
93 *val = strcmpW( lstr, rstr ) <= 0;
94 break;
95 case OP_GE:
96 *val = strcmpW( lstr, rstr ) >= 0;
97 break;
98 case OP_NE:
99 *val = strcmpW( lstr, rstr );
100 break;
101 case OP_LIKE:
102 *val = eval_like( lstr, rstr );
103 break;
104 default:
105 ERR("unhandled operator %u\n", op);
106 return WBEM_E_INVALID_QUERY;
108 return S_OK;
111 static inline BOOL is_strcmp( const struct complex_expr *expr )
113 return ((expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) ||
114 (expr->left->type == EXPR_SVAL && expr->right->type == EXPR_PROPVAL));
117 static inline BOOL is_boolcmp( const struct complex_expr *expr, UINT ltype, UINT rtype )
119 if (ltype == CIM_BOOLEAN && expr->left->type == EXPR_PROPVAL &&
120 (expr->right->type == EXPR_SVAL || expr->right->type == EXPR_BVAL)) return TRUE;
121 else if (rtype == CIM_BOOLEAN && expr->right->type == EXPR_PROPVAL &&
122 (expr->left->type == EXPR_SVAL || expr->left->type == EXPR_BVAL)) return TRUE;
123 return FALSE;
126 static HRESULT eval_boolcmp( UINT op, LONGLONG lval, LONGLONG rval, UINT ltype, UINT rtype, LONGLONG *val )
128 static const WCHAR trueW[] = {'T','r','u','e',0};
130 if (ltype == CIM_STRING) lval = !strcmpiW( (const WCHAR *)(INT_PTR)lval, trueW ) ? -1 : 0;
131 else if (rtype == CIM_STRING) rval = !strcmpiW( (const WCHAR *)(INT_PTR)rval, trueW ) ? -1 : 0;
133 switch (op)
135 case OP_EQ:
136 *val = (lval == rval);
137 break;
138 case OP_NE:
139 *val = (lval != rval);
140 break;
141 default:
142 ERR("unhandled operator %u\n", op);
143 return WBEM_E_INVALID_QUERY;
145 return S_OK;
148 static UINT resolve_type( UINT left, UINT right )
150 switch (left)
152 case CIM_SINT8:
153 case CIM_SINT16:
154 case CIM_SINT32:
155 case CIM_SINT64:
156 case CIM_UINT8:
157 case CIM_UINT16:
158 case CIM_UINT32:
159 case CIM_UINT64:
160 switch (right)
162 case CIM_SINT8:
163 case CIM_SINT16:
164 case CIM_SINT32:
165 case CIM_SINT64:
166 case CIM_UINT8:
167 case CIM_UINT16:
168 case CIM_UINT32:
169 case CIM_UINT64:
170 return CIM_UINT64;
171 default: break;
173 break;
175 case CIM_STRING:
176 if (right == CIM_STRING) return CIM_STRING;
177 break;
179 case CIM_BOOLEAN:
180 if (right == CIM_BOOLEAN) return CIM_BOOLEAN;
181 break;
183 default:
184 break;
186 return CIM_ILLEGAL;
189 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
190 LONGLONG *val, UINT *type )
192 HRESULT lret, rret;
193 LONGLONG lval, rval;
194 UINT ltype, rtype;
196 lret = eval_cond( table, row, expr->left, &lval, &ltype );
197 rret = eval_cond( table, row, expr->right, &rval, &rtype );
198 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
200 *type = resolve_type( ltype, rtype );
202 if (is_boolcmp( expr, ltype, rtype ))
203 return eval_boolcmp( expr->op, lval, rval, ltype, rtype, val );
205 if (is_strcmp( expr ))
207 const WCHAR *lstr = (const WCHAR *)(INT_PTR)lval;
208 const WCHAR *rstr = (const WCHAR *)(INT_PTR)rval;
210 return eval_strcmp( expr->op, lstr, rstr, val );
212 switch (expr->op)
214 case OP_EQ:
215 *val = (lval == rval);
216 break;
217 case OP_AND:
218 *val = (lval && rval);
219 break;
220 case OP_OR:
221 *val = (lval || rval);
222 break;
223 case OP_GT:
224 *val = (lval > rval);
225 break;
226 case OP_LT:
227 *val = (lval < rval);
228 break;
229 case OP_LE:
230 *val = (lval <= rval);
231 break;
232 case OP_GE:
233 *val = (lval >= rval);
234 break;
235 case OP_NE:
236 *val = (lval != rval);
237 break;
238 default:
239 ERR("unhandled operator %u\n", expr->op);
240 return WBEM_E_INVALID_QUERY;
242 return S_OK;
245 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
246 LONGLONG *val, UINT *type )
249 HRESULT hr;
250 UINT column;
251 LONGLONG lval;
253 if (expr->op == OP_NOT)
255 hr = eval_cond( table, row, expr->left, &lval, type );
256 if (hr != S_OK)
257 return hr;
258 *val = !lval;
259 return S_OK;
262 hr = get_column_index( table, expr->left->u.propval->name, &column );
263 if (hr != S_OK)
264 return hr;
266 hr = get_value( table, row, column, &lval );
267 if (hr != S_OK)
268 return hr;
270 switch (expr->op)
272 case OP_ISNULL:
273 *val = !lval;
274 break;
275 case OP_NOTNULL:
276 *val = lval;
277 break;
278 default:
279 ERR("unknown operator %u\n", expr->op);
280 return WBEM_E_INVALID_QUERY;
283 *type = table->columns[column].type & CIM_TYPE_MASK;
284 return S_OK;
287 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
288 LONGLONG *val, UINT *type )
291 HRESULT hr;
292 UINT column;
294 hr = get_column_index( table, propval->name, &column );
295 if (hr != S_OK)
296 return hr;
298 *type = table->columns[column].type & CIM_TYPE_MASK;
299 return get_value( table, row, column, val );
302 HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond, LONGLONG *val, UINT *type )
304 if (!cond)
306 *val = 1;
307 *type = CIM_UINT64;
308 return S_OK;
310 switch (cond->type)
312 case EXPR_COMPLEX:
313 return eval_binary( table, row, &cond->u.expr, val, type );
315 case EXPR_UNARY:
316 return eval_unary( table, row, &cond->u.expr, val, type );
318 case EXPR_PROPVAL:
319 return eval_propval( table, row, cond->u.propval, val, type );
321 case EXPR_SVAL:
322 *val = (INT_PTR)cond->u.sval;
323 *type = CIM_STRING;
324 return S_OK;
326 case EXPR_IVAL:
327 *val = cond->u.ival;
328 *type = CIM_UINT64;
329 return S_OK;
331 case EXPR_BVAL:
332 *val = cond->u.ival;
333 *type = CIM_BOOLEAN;
334 return S_OK;
336 default:
337 ERR("invalid expression type\n");
338 break;
340 return WBEM_E_INVALID_QUERY;
343 HRESULT execute_view( struct view *view )
345 UINT i, j = 0, len;
347 if (!view->table) return S_OK;
348 if (view->table->fill)
350 clear_table( view->table );
351 view->table->fill( view->table, view->cond );
353 if (!view->table->num_rows) return S_OK;
355 len = min( view->table->num_rows, 16 );
356 if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
358 for (i = 0; i < view->table->num_rows; i++)
360 HRESULT hr;
361 LONGLONG val = 0;
362 UINT type;
364 if (j >= len)
366 UINT *tmp;
367 len *= 2;
368 if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
369 view->result = tmp;
371 if ((hr = eval_cond( view->table, i, view->cond, &val, &type )) != S_OK) return hr;
372 if (val) view->result[j++] = i;
374 view->count = j;
375 return S_OK;
378 struct query *create_query(void)
380 struct query *query;
382 if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
383 list_init( &query->mem );
384 query->refs = 1;
385 return query;
388 void free_query( struct query *query )
390 struct list *mem, *next;
392 if (!query) return;
393 destroy_view( query->view );
394 LIST_FOR_EACH_SAFE( mem, next, &query->mem ) { heap_free( mem ); }
395 heap_free( query );
398 struct query *addref_query( struct query *query )
400 InterlockedIncrement( &query->refs );
401 return query;
404 void release_query( struct query *query )
406 if (!InterlockedDecrement( &query->refs )) free_query( query );
409 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
411 HRESULT hr;
412 struct query *query;
414 *result = NULL;
415 if (!(query = create_query())) return E_OUTOFMEMORY;
416 hr = parse_query( str, &query->view, &query->mem );
417 if (hr != S_OK) goto done;
418 hr = execute_view( query->view );
419 if (hr != S_OK) goto done;
420 hr = EnumWbemClassObject_create( query, (void **)result );
422 done:
423 release_query( query );
424 return hr;
427 BOOL is_selected_prop( const struct view *view, const WCHAR *name )
429 const struct property *prop = view->proplist;
431 if (!prop) return TRUE;
432 while (prop)
434 if (!strcmpiW( prop->name, name )) return TRUE;
435 prop = prop->next;
437 return FALSE;
440 static BOOL is_system_prop( const WCHAR *name )
442 return (name[0] == '_' && name[1] == '_');
445 static BSTR build_servername( const struct view *view )
447 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
448 DWORD len = sizeof(server)/sizeof(server[0]);
450 if (view->proplist) return NULL;
452 if (!(GetComputerNameW( server, &len ))) return NULL;
453 for (p = server; *p; p++) *p = toupperW( *p );
454 return SysAllocString( server );
457 static BSTR build_classname( const struct view *view )
459 return SysAllocString( view->table->name );
462 static BSTR build_namespace( const struct view *view )
464 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
466 if (view->proplist) return NULL;
467 return SysAllocString( cimv2W );
470 static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
472 static const WCHAR fmtW[] = {'%','s','=','%','s',0};
473 UINT i, j, offset, row = view->result[index];
474 BSTR *values, ret = NULL;
476 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
478 *len = j = 0;
479 for (i = 0; i < view->table->num_cols; i++)
481 if (view->table->columns[i].type & COL_FLAG_KEY)
483 const WCHAR *name = view->table->columns[i].name;
485 values[j] = get_value_bstr( view->table, row, i );
486 *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
487 j++;
490 if ((ret = SysAllocStringLen( NULL, *len )))
492 offset = j = 0;
493 for (i = 0; i < view->table->num_cols; i++)
495 if (view->table->columns[i].type & COL_FLAG_KEY)
497 const WCHAR *name = view->table->columns[i].name;
499 offset += sprintfW( ret + offset, fmtW, name, values[j] );
500 if (j < count - 1) ret[offset++] = ',';
501 j++;
505 for (i = 0; i < count; i++) SysFreeString( values[i] );
506 heap_free( values );
507 return ret;
510 static UINT count_key_columns( const struct view *view )
512 UINT i, num_keys = 0;
514 for (i = 0; i < view->table->num_cols; i++)
516 if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
518 return num_keys;
521 static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
523 static const WCHAR fmtW[] = {'%','s','.','%','s',0};
524 BSTR class, proplist, ret = NULL;
525 UINT num_keys, len;
527 if (view->proplist) return NULL;
529 if (!(class = build_classname( view ))) return NULL;
530 if (!(num_keys = count_key_columns( view ))) return class;
531 if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;
533 len += strlenW( fmtW ) + SysStringLen( class );
534 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
535 sprintfW( ret, fmtW, class, proplist );
537 done:
538 SysFreeString( class );
539 SysFreeString( proplist );
540 return ret;
543 static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
545 static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
546 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
547 UINT len;
549 if (view->proplist) return NULL;
551 if (!(server = build_servername( view ))) return NULL;
552 if (!(namespace = build_namespace( view ))) goto done;
553 if (!(relpath = build_relpath( view, index, name ))) goto done;
555 len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
556 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
557 sprintfW( ret, fmtW, server, namespace, relpath );
559 done:
560 SysFreeString( server );
561 SysFreeString( namespace );
562 SysFreeString( relpath );
563 return ret;
566 BOOL is_method( const struct table *table, UINT column )
568 return table->columns[column].type & COL_FLAG_METHOD;
571 static UINT count_properties( const struct view *view )
573 UINT i, num_props = 0;
575 for (i = 0; i < view->table->num_cols; i++)
577 if (!is_method( view->table, i)) num_props++;
579 return num_props;
582 static UINT count_selected_properties( const struct view *view )
584 const struct property *prop = view->proplist;
585 UINT count;
587 if (!prop) return count_properties( view );
589 count = 1;
590 while ((prop = prop->next)) count++;
591 return count;
594 static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
595 VARIANT *ret, CIMTYPE *type, LONG *flavor )
597 static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
598 static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
599 static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
600 static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
601 static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
602 static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
603 static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};
605 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
607 if (!strcmpiW( name, classW ))
609 V_VT( ret ) = VT_BSTR;
610 V_BSTR( ret ) = build_classname( view );
611 if (type) *type = CIM_STRING;
612 return S_OK;
614 if (!strcmpiW( name, genusW ))
616 V_VT( ret ) = VT_I4;
617 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
618 if (type) *type = CIM_SINT32;
619 return S_OK;
621 else if (!strcmpiW( name, namespaceW ))
623 V_VT( ret ) = VT_BSTR;
624 V_BSTR( ret ) = build_namespace( view );
625 if (type) *type = CIM_STRING;
626 return S_OK;
628 else if (!strcmpiW( name, pathW ))
630 V_VT( ret ) = VT_BSTR;
631 V_BSTR( ret ) = build_path( view, index, name );
632 if (type) *type = CIM_STRING;
633 return S_OK;
635 if (!strcmpiW( name, propcountW ))
637 V_VT( ret ) = VT_I4;
638 V_I4( ret ) = count_selected_properties( view );
639 if (type) *type = CIM_SINT32;
640 return S_OK;
642 else if (!strcmpiW( name, relpathW ))
644 V_VT( ret ) = VT_BSTR;
645 V_BSTR( ret ) = build_relpath( view, index, name );
646 if (type) *type = CIM_STRING;
647 return S_OK;
649 else if (!strcmpiW( name, serverW ))
651 V_VT( ret ) = VT_BSTR;
652 V_BSTR( ret ) = build_servername( view );
653 if (type) *type = CIM_STRING;
654 return S_OK;
656 FIXME("system property %s not implemented\n", debugstr_w(name));
657 return WBEM_E_NOT_FOUND;
660 VARTYPE to_vartype( CIMTYPE type )
662 switch (type)
664 case CIM_BOOLEAN: return VT_BOOL;
665 case CIM_STRING:
666 case CIM_DATETIME: return VT_BSTR;
667 case CIM_SINT8: return VT_I1;
668 case CIM_UINT8: return VT_UI1;
669 case CIM_SINT16: return VT_I2;
670 case CIM_UINT16: return VT_UI2;
671 case CIM_SINT32: return VT_I4;
672 case CIM_UINT32: return VT_UI4;
673 case CIM_SINT64: return VT_I8;
674 case CIM_UINT64: return VT_UI8;
675 default:
676 ERR("unhandled type %u\n", type);
677 break;
679 return 0;
682 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE type )
684 SAFEARRAY *ret;
685 UINT size = get_type_size( type );
686 VARTYPE vartype = to_vartype( type );
687 LONG i;
689 if (!array || !(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
691 for (i = 0; i < array->count; i++)
693 void *ptr = (char *)array->ptr + i * size;
694 if (vartype == VT_BSTR)
696 BSTR str = SysAllocString( *(const WCHAR **)ptr );
697 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
699 SysFreeString( str );
700 SafeArrayDestroy( ret );
701 return NULL;
704 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
706 SafeArrayDestroy( ret );
707 return NULL;
710 return ret;
713 void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
715 if (type & VT_ARRAY)
717 V_VT( ret ) = type;
718 V_ARRAY( ret ) = val_ptr;
719 return;
721 switch (type)
723 case VT_BOOL:
724 V_BOOL( ret ) = val;
725 break;
726 case VT_BSTR:
727 V_BSTR( ret ) = val_ptr;
728 break;
729 case VT_I1:
730 V_I1( ret ) = val;
731 break;
732 case VT_UI1:
733 V_UI1( ret ) = val;
734 break;
735 case VT_I2:
736 V_I2( ret ) = val;
737 break;
738 case VT_UI2:
739 V_UI2( ret ) = val;
740 break;
741 case VT_I4:
742 V_I4( ret ) = val;
743 break;
744 case VT_UI4:
745 V_UI4( ret ) = val;
746 break;
747 case VT_NULL:
748 break;
749 default:
750 ERR("unhandled variant type %u\n", type);
751 return;
753 V_VT( ret ) = type;
756 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
757 CIMTYPE *type, LONG *flavor )
759 HRESULT hr;
760 UINT column, row;
761 VARTYPE vartype;
762 void *val_ptr = NULL;
763 LONGLONG val;
765 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
766 if (!view->count || !is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
768 hr = get_column_index( view->table, name, &column );
769 if (hr != S_OK || is_method( view->table, column )) return WBEM_E_NOT_FOUND;
771 row = view->result[index];
772 hr = get_value( view->table, row, column, &val );
773 if (hr != S_OK) return hr;
775 vartype = view->table->columns[column].vartype;
776 if (view->table->columns[column].type & CIM_FLAG_ARRAY)
778 CIMTYPE basetype = view->table->columns[column].type & CIM_TYPE_MASK;
780 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
781 if (!vartype) vartype = to_vartype( basetype ) | VT_ARRAY;
782 goto done;
784 switch (view->table->columns[column].type & COL_TYPE_MASK)
786 case CIM_BOOLEAN:
787 if (!vartype) vartype = VT_BOOL;
788 break;
789 case CIM_STRING:
790 case CIM_DATETIME:
791 if (val)
793 vartype = VT_BSTR;
794 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
796 else
797 vartype = VT_NULL;
798 break;
799 case CIM_SINT8:
800 if (!vartype) vartype = VT_I1;
801 break;
802 case CIM_UINT8:
803 if (!vartype) vartype = VT_UI1;
804 break;
805 case CIM_SINT16:
806 if (!vartype) vartype = VT_I2;
807 break;
808 case CIM_UINT16:
809 if (!vartype) vartype = VT_UI2;
810 break;
811 case CIM_SINT32:
812 if (!vartype) vartype = VT_I4;
813 break;
814 case CIM_UINT32:
815 if (!vartype) vartype = VT_UI4;
816 break;
817 case CIM_SINT64:
818 vartype = VT_BSTR;
819 val_ptr = get_value_bstr( view->table, row, column );
820 break;
821 case CIM_UINT64:
822 vartype = VT_BSTR;
823 val_ptr = get_value_bstr( view->table, row, column );
824 break;
825 default:
826 ERR("unhandled column type %u\n", view->table->columns[column].type);
827 return WBEM_E_FAILED;
830 done:
831 set_variant( vartype, val, val_ptr, ret );
832 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
833 if (flavor) *flavor = 0;
834 return S_OK;
837 static CIMTYPE to_cimtype( VARTYPE type )
839 switch (type)
841 case VT_BOOL: return CIM_BOOLEAN;
842 case VT_BSTR: return CIM_STRING;
843 case VT_I1: return CIM_SINT8;
844 case VT_UI1: return CIM_UINT8;
845 case VT_I2: return CIM_SINT16;
846 case VT_UI2: return CIM_UINT16;
847 case VT_I4: return CIM_SINT32;
848 case VT_UI4: return CIM_UINT32;
849 case VT_I8: return CIM_SINT64;
850 case VT_UI8: return CIM_UINT64;
851 default:
852 ERR("unhandled type %u\n", type);
853 break;
855 return 0;
858 static struct array *to_array( VARIANT *var, CIMTYPE *type )
860 struct array *ret;
861 LONG bound, i;
862 VARTYPE vartype;
863 CIMTYPE basetype;
864 UINT size;
866 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
867 if (!(basetype = to_cimtype( vartype ))) return NULL;
868 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
869 if (!(ret = heap_alloc( sizeof(struct array) ))) return NULL;
871 ret->count = bound + 1;
872 size = get_type_size( basetype );
873 if (!(ret->ptr = heap_alloc_zero( ret->count * size )))
875 heap_free( ret );
876 return NULL;
878 for (i = 0; i < ret->count; i++)
880 void *ptr = (char *)ret->ptr + i * size;
881 if (vartype == VT_BSTR)
883 BSTR str;
884 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
886 destroy_array( ret, basetype );
887 return NULL;
889 *(WCHAR **)ptr = heap_strdupW( str );
890 SysFreeString( str );
891 if (!*(WCHAR **)ptr)
893 destroy_array( ret, basetype );
894 return NULL;
897 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
899 destroy_array( ret, basetype );
900 return NULL;
903 *type = basetype | CIM_FLAG_ARRAY;
904 return ret;
907 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
909 if (!var)
911 *val = 0;
912 return S_OK;
914 if (V_VT( var ) & VT_ARRAY)
916 *val = (INT_PTR)to_array( var, type );
917 if (!*val) return E_OUTOFMEMORY;
918 return S_OK;
920 switch (V_VT( var ))
922 case VT_BOOL:
923 *val = V_BOOL( var );
924 *type = CIM_BOOLEAN;
925 break;
926 case VT_BSTR:
927 *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
928 if (!*val) return E_OUTOFMEMORY;
929 *type = CIM_STRING;
930 break;
931 case VT_I2:
932 *val = V_I2( var );
933 *type = CIM_SINT16;
934 break;
935 case VT_UI2:
936 *val = V_UI2( var );
937 *type = CIM_UINT16;
938 break;
939 case VT_I4:
940 *val = V_I4( var );
941 *type = CIM_SINT32;
942 break;
943 case VT_UI4:
944 *val = V_UI4( var );
945 *type = CIM_UINT32;
946 break;
947 case VT_NULL:
948 *val = 0;
949 break;
950 default:
951 ERR("unhandled type %u\n", V_VT( var ));
952 return WBEM_E_FAILED;
954 return S_OK;
957 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
959 HRESULT hr;
960 UINT column, row = view->result[index];
961 LONGLONG val;
963 hr = get_column_index( view->table, name, &column );
964 if (hr != S_OK)
966 FIXME("no support for creating new properties\n");
967 return WBEM_E_FAILED;
969 if (is_method( view->table, column ) || !(view->table->columns[column].type & COL_FLAG_DYNAMIC))
970 return WBEM_E_FAILED;
972 hr = to_longlong( var, &val, &type );
973 if (hr != S_OK) return hr;
975 return set_value( view->table, row, column, val, type );
978 HRESULT get_properties( const struct view *view, LONG flags, SAFEARRAY **props )
980 SAFEARRAY *sa;
981 BSTR str;
982 UINT i, num_props = count_selected_properties( view );
983 LONG j;
985 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
987 for (i = 0, j = 0; i < view->table->num_cols; i++)
989 BOOL is_system;
991 if (is_method( view->table, i )) continue;
992 if (!is_selected_prop( view, view->table->columns[i].name )) continue;
994 is_system = is_system_prop( view->table->columns[i].name );
995 if ((flags & WBEM_FLAG_NONSYSTEM_ONLY) && is_system) continue;
996 else if ((flags & WBEM_FLAG_SYSTEM_ONLY) && !is_system) continue;
998 str = SysAllocString( view->table->columns[i].name );
999 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
1001 SysFreeString( str );
1002 SafeArrayDestroy( sa );
1003 return E_OUTOFMEMORY;
1005 j++;
1007 *props = sa;
1008 return S_OK;