dxgi: Implement dxgi_surface_GetDesc().
[wine.git] / dlls / wbemprox / query.c
blobb3a3eb9e64d4f3d46ef1ab2f2b60e26750d15f49
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_SINT16: return VT_I2;
668 case CIM_UINT16: return VT_UI2;
669 case CIM_SINT32: return VT_I4;
670 case CIM_UINT32: return VT_UI4;
671 case CIM_SINT64: return VT_I8;
672 case CIM_UINT64: return VT_UI8;
673 default:
674 ERR("unhandled type %u\n", type);
675 break;
677 return 0;
680 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE type )
682 SAFEARRAY *ret;
683 UINT size = get_type_size( type );
684 VARTYPE vartype = to_vartype( type );
685 LONG i;
687 if (!array || !(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
689 for (i = 0; i < array->count; i++)
691 void *ptr = (char *)array->ptr + i * size;
692 if (vartype == VT_BSTR)
694 BSTR str = SysAllocString( *(const WCHAR **)ptr );
695 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
697 SysFreeString( str );
698 SafeArrayDestroy( ret );
699 return NULL;
702 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
704 SafeArrayDestroy( ret );
705 return NULL;
708 return ret;
711 void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
713 if (type & VT_ARRAY)
715 V_VT( ret ) = type;
716 V_ARRAY( ret ) = val_ptr;
717 return;
719 switch (type)
721 case VT_BOOL:
722 V_BOOL( ret ) = val;
723 break;
724 case VT_BSTR:
725 V_BSTR( ret ) = val_ptr;
726 break;
727 case VT_I2:
728 V_I2( ret ) = val;
729 break;
730 case VT_UI2:
731 V_UI2( ret ) = val;
732 break;
733 case VT_I4:
734 V_I4( ret ) = val;
735 break;
736 case VT_UI4:
737 V_UI4( ret ) = val;
738 break;
739 case VT_NULL:
740 break;
741 default:
742 ERR("unhandled variant type %u\n", type);
743 return;
745 V_VT( ret ) = type;
748 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
749 CIMTYPE *type, LONG *flavor )
751 HRESULT hr;
752 UINT column, row;
753 VARTYPE vartype;
754 void *val_ptr = NULL;
755 LONGLONG val;
757 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
758 if (!view->count || !is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
760 hr = get_column_index( view->table, name, &column );
761 if (hr != S_OK || is_method( view->table, column )) return WBEM_E_NOT_FOUND;
763 row = view->result[index];
764 hr = get_value( view->table, row, column, &val );
765 if (hr != S_OK) return hr;
767 vartype = view->table->columns[column].vartype;
768 if (view->table->columns[column].type & CIM_FLAG_ARRAY)
770 CIMTYPE basetype = view->table->columns[column].type & CIM_TYPE_MASK;
772 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
773 if (!vartype) vartype = to_vartype( basetype ) | VT_ARRAY;
774 goto done;
776 switch (view->table->columns[column].type & COL_TYPE_MASK)
778 case CIM_BOOLEAN:
779 if (!vartype) vartype = VT_BOOL;
780 break;
781 case CIM_STRING:
782 case CIM_DATETIME:
783 if (val)
785 vartype = VT_BSTR;
786 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
788 else
789 vartype = VT_NULL;
790 break;
791 case CIM_SINT16:
792 if (!vartype) vartype = VT_I2;
793 break;
794 case CIM_UINT16:
795 if (!vartype) vartype = VT_UI2;
796 break;
797 case CIM_SINT32:
798 if (!vartype) vartype = VT_I4;
799 break;
800 case CIM_UINT32:
801 if (!vartype) vartype = VT_UI4;
802 break;
803 case CIM_SINT64:
804 vartype = VT_BSTR;
805 val_ptr = get_value_bstr( view->table, row, column );
806 break;
807 case CIM_UINT64:
808 vartype = VT_BSTR;
809 val_ptr = get_value_bstr( view->table, row, column );
810 break;
811 default:
812 ERR("unhandled column type %u\n", view->table->columns[column].type);
813 return WBEM_E_FAILED;
816 done:
817 set_variant( vartype, val, val_ptr, ret );
818 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
819 if (flavor) *flavor = 0;
820 return S_OK;
823 static CIMTYPE to_cimtype( VARTYPE type )
825 switch (type)
827 case VT_BOOL: return CIM_BOOLEAN;
828 case VT_BSTR: return CIM_STRING;
829 case VT_I2: return CIM_SINT16;
830 case VT_UI2: return CIM_UINT16;
831 case VT_I4: return CIM_SINT32;
832 case VT_UI4: return CIM_UINT32;
833 case VT_I8: return CIM_SINT64;
834 case VT_UI8: return CIM_UINT64;
835 default:
836 ERR("unhandled type %u\n", type);
837 break;
839 return 0;
842 static struct array *to_array( VARIANT *var, CIMTYPE *type )
844 struct array *ret;
845 LONG bound, i;
846 VARTYPE vartype;
847 CIMTYPE basetype;
848 UINT size;
850 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
851 if (!(basetype = to_cimtype( vartype ))) return NULL;
852 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
853 if (!(ret = heap_alloc( sizeof(struct array) ))) return NULL;
855 ret->count = bound + 1;
856 size = get_type_size( basetype );
857 if (!(ret->ptr = heap_alloc_zero( ret->count * size )))
859 heap_free( ret );
860 return NULL;
862 for (i = 0; i < ret->count; i++)
864 void *ptr = (char *)ret->ptr + i * size;
865 if (vartype == VT_BSTR)
867 BSTR str;
868 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
870 destroy_array( ret, basetype );
871 return NULL;
873 *(WCHAR **)ptr = heap_strdupW( str );
874 SysFreeString( str );
875 if (!*(WCHAR **)ptr)
877 destroy_array( ret, basetype );
878 return NULL;
881 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
883 destroy_array( ret, basetype );
884 return NULL;
887 *type = basetype | CIM_FLAG_ARRAY;
888 return ret;
891 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
893 if (!var)
895 *val = 0;
896 return S_OK;
898 if (V_VT( var ) & VT_ARRAY)
900 *val = (INT_PTR)to_array( var, type );
901 if (!*val) return E_OUTOFMEMORY;
902 return S_OK;
904 switch (V_VT( var ))
906 case VT_BOOL:
907 *val = V_BOOL( var );
908 *type = CIM_BOOLEAN;
909 break;
910 case VT_BSTR:
911 *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
912 if (!*val) return E_OUTOFMEMORY;
913 *type = CIM_STRING;
914 break;
915 case VT_I2:
916 *val = V_I2( var );
917 *type = CIM_SINT16;
918 break;
919 case VT_UI2:
920 *val = V_UI2( var );
921 *type = CIM_UINT16;
922 break;
923 case VT_I4:
924 *val = V_I4( var );
925 *type = CIM_SINT32;
926 break;
927 case VT_UI4:
928 *val = V_UI4( var );
929 *type = CIM_UINT32;
930 break;
931 case VT_NULL:
932 *val = 0;
933 break;
934 default:
935 ERR("unhandled type %u\n", V_VT( var ));
936 return WBEM_E_FAILED;
938 return S_OK;
941 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
943 HRESULT hr;
944 UINT column, row = view->result[index];
945 LONGLONG val;
947 hr = get_column_index( view->table, name, &column );
948 if (hr != S_OK)
950 FIXME("no support for creating new properties\n");
951 return WBEM_E_FAILED;
953 if (is_method( view->table, column ) || !(view->table->columns[column].type & COL_FLAG_DYNAMIC))
954 return WBEM_E_FAILED;
956 hr = to_longlong( var, &val, &type );
957 if (hr != S_OK) return hr;
959 return set_value( view->table, row, column, val, type );
962 HRESULT get_properties( const struct view *view, LONG flags, SAFEARRAY **props )
964 SAFEARRAY *sa;
965 BSTR str;
966 UINT i, num_props = count_selected_properties( view );
967 LONG j;
969 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
971 for (i = 0, j = 0; i < view->table->num_cols; i++)
973 BOOL is_system;
975 if (is_method( view->table, i )) continue;
976 if (!is_selected_prop( view, view->table->columns[i].name )) continue;
978 is_system = is_system_prop( view->table->columns[i].name );
979 if ((flags & WBEM_FLAG_NONSYSTEM_ONLY) && is_system) continue;
980 else if ((flags & WBEM_FLAG_SYSTEM_ONLY) && !is_system) continue;
982 str = SysAllocString( view->table->columns[i].name );
983 if (!str || SafeArrayPutElement( sa, &j, str ) != S_OK)
985 SysFreeString( str );
986 SafeArrayDestroy( sa );
987 return E_OUTOFMEMORY;
989 j++;
991 *props = sa;
992 return S_OK;