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
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 );
48 void destroy_view( struct view *view )
50 if (view->table) release_table( view->table );
51 heap_free( view->result );
55 static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
57 const WCHAR *p = lstr, *q = rstr;
63 while (*q == '%') q++;
65 while (*p && toupperW( p[1] ) != toupperW( q[1] )) p++;
68 if (toupperW( *p++ ) != toupperW( *q++ )) return FALSE;
73 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
83 *val = !strcmpW( lstr, rstr );
86 *val = strcmpW( lstr, rstr ) > 0;
89 *val = strcmpW( lstr, rstr ) < 0;
92 *val = strcmpW( lstr, rstr ) <= 0;
95 *val = strcmpW( lstr, rstr ) >= 0;
98 *val = strcmpW( lstr, rstr );
101 *val = eval_like( lstr, rstr );
104 ERR("unhandled operator %u\n", op);
105 return WBEM_E_INVALID_QUERY;
110 static inline BOOL is_strcmp( const struct complex_expr *expr )
112 return ((expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) ||
113 (expr->left->type == EXPR_SVAL && expr->right->type == EXPR_PROPVAL));
116 static HRESULT eval_cond( const struct table *, UINT, const struct expr *, LONGLONG * );
118 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
124 lret = eval_cond( table, row, expr->left, &lval );
125 rret = eval_cond( table, row, expr->right, &rval );
126 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
128 if (is_strcmp( expr ))
130 const WCHAR *lstr = (const WCHAR *)(INT_PTR)lval;
131 const WCHAR *rstr = (const WCHAR *)(INT_PTR)rval;
133 return eval_strcmp( expr->op, lstr, rstr, val );
138 *val = (lval == rval);
141 *val = (lval && rval);
144 *val = (lval || rval);
147 *val = (lval > rval);
150 *val = (lval < rval);
153 *val = (lval <= rval);
156 *val = (lval >= rval);
159 *val = (lval != rval);
162 ERR("unhandled operator %u\n", expr->op);
163 return WBEM_E_INVALID_QUERY;
168 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
176 hr = get_column_index( table, expr->left->u.propval->name, &column );
180 hr = get_value( table, row, column, &lval );
193 ERR("unknown operator %u\n", expr->op);
194 return WBEM_E_INVALID_QUERY;
199 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
206 hr = get_column_index( table, propval->name, &column );
210 return get_value( table, row, column, val );
213 static HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond,
224 return eval_binary( table, row, &cond->u.expr, val );
226 return eval_unary( table, row, &cond->u.expr, val );
228 return eval_propval( table, row, cond->u.propval, val );
230 *val = (INT_PTR)cond->u.sval;
237 ERR("invalid expression type\n");
240 return WBEM_E_INVALID_QUERY;
243 static HRESULT execute_view( struct view *view )
247 if (!view->table || !view->table->num_rows) return S_OK;
249 len = min( view->table->num_rows, 16 );
250 if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
252 for (i = 0; i < view->table->num_rows; i++)
261 if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
264 if ((hr = eval_cond( view->table, i, view->cond, &val )) != S_OK) return hr;
265 if (val) view->result[j++] = i;
271 static struct query *create_query(void)
275 if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
276 list_init( &query->mem );
281 static void free_query( struct query *query )
283 struct list *mem, *next;
285 destroy_view( query->view );
286 LIST_FOR_EACH_SAFE( mem, next, &query->mem )
293 void addref_query( struct query *query )
295 InterlockedIncrement( &query->refs );
298 void release_query( struct query *query )
300 if (!InterlockedDecrement( &query->refs )) free_query( query );
303 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
309 if (!(query = create_query())) return E_OUTOFMEMORY;
310 hr = parse_query( str, &query->view, &query->mem );
311 if (hr != S_OK) goto done;
312 hr = execute_view( query->view );
313 if (hr != S_OK) goto done;
314 hr = EnumWbemClassObject_create( NULL, query, (void **)result );
317 release_query( query );
321 static BOOL is_selected_prop( const struct view *view, const WCHAR *name )
323 const struct property *prop = view->proplist;
325 if (!prop) return TRUE;
328 if (!strcmpiW( prop->name, name )) return TRUE;
334 static BOOL is_system_prop( const WCHAR *name )
336 return (name[0] == '_' && name[1] == '_');
339 static BSTR build_servername( const struct view *view )
341 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
342 DWORD len = sizeof(server)/sizeof(server[0]);
344 if (view->proplist) return NULL;
346 if (!(GetComputerNameW( server, &len ))) return NULL;
347 for (p = server; *p; p++) *p = toupperW( *p );
348 return SysAllocString( server );
351 static BSTR build_classname( const struct view *view )
353 return SysAllocString( view->table->name );
356 static BSTR build_namespace( const struct view *view )
358 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
360 if (view->proplist) return NULL;
361 return SysAllocString( cimv2W );
364 static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
366 static const WCHAR fmtW[] = {'%','s','=','%','s',0};
367 UINT i, j, offset, row = view->result[index];
368 BSTR *values, ret = NULL;
370 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
373 for (i = 0; i < view->table->num_cols; i++)
375 if (view->table->columns[i].type & COL_FLAG_KEY)
377 const WCHAR *name = view->table->columns[i].name;
379 values[j] = get_value_bstr( view->table, row, i );
380 *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
384 if ((ret = SysAllocStringLen( NULL, *len )))
387 for (i = 0; i < view->table->num_cols; i++)
389 if (view->table->columns[i].type & COL_FLAG_KEY)
391 const WCHAR *name = view->table->columns[i].name;
393 offset += sprintfW( ret + offset, fmtW, name, values[j] );
394 if (j < count - 1) ret[offset++] = ',';
399 for (i = 0; i < count; i++) SysFreeString( values[i] );
404 static UINT count_key_columns( const struct view *view )
406 UINT i, num_keys = 0;
408 for (i = 0; i < view->table->num_cols; i++)
410 if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
415 static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
417 static const WCHAR fmtW[] = {'%','s','.','%','s',0};
418 BSTR class, proplist, ret = NULL;
421 if (view->proplist) return NULL;
423 if (!(class = build_classname( view ))) return NULL;
424 if (!(num_keys = count_key_columns( view ))) return class;
425 if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;
427 len += strlenW( fmtW ) + SysStringLen( class );
428 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
429 sprintfW( ret, fmtW, class, proplist );
432 SysFreeString( class );
433 SysFreeString( proplist );
437 static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
439 static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
440 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
443 if (view->proplist) return NULL;
445 if (!(server = build_servername( view ))) return NULL;
446 if (!(namespace = build_namespace( view ))) goto done;
447 if (!(relpath = build_relpath( view, index, name ))) goto done;
449 len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
450 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
451 sprintfW( ret, fmtW, server, namespace, relpath );
454 SysFreeString( server );
455 SysFreeString( namespace );
456 SysFreeString( relpath );
460 static inline BOOL is_method( const struct table *table, UINT column )
462 return table->columns[column].type & COL_FLAG_METHOD;
465 static UINT count_properties( const struct view *view )
467 UINT i, num_props = 0;
469 for (i = 0; i < view->table->num_cols; i++)
471 if (!is_method( view->table, i)) num_props++;
476 static UINT count_selected_properties( const struct view *view )
478 const struct property *prop = view->proplist;
481 if (!prop) return count_properties( view );
484 while ((prop = prop->next)) count++;
488 static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
489 VARIANT *ret, CIMTYPE *type, LONG *flavor )
491 static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
492 static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
493 static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
494 static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
495 static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
496 static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
497 static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};
499 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
501 if (!strcmpiW( name, classW ))
503 V_VT( ret ) = VT_BSTR;
504 V_BSTR( ret ) = build_classname( view );
505 if (type) *type = CIM_STRING;
508 if (!strcmpiW( name, genusW ))
511 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
512 if (type) *type = CIM_SINT32;
515 else if (!strcmpiW( name, namespaceW ))
517 V_VT( ret ) = VT_BSTR;
518 V_BSTR( ret ) = build_namespace( view );
519 if (type) *type = CIM_STRING;
522 else if (!strcmpiW( name, pathW ))
524 V_VT( ret ) = VT_BSTR;
525 V_BSTR( ret ) = build_path( view, index, name );
526 if (type) *type = CIM_STRING;
529 if (!strcmpiW( name, propcountW ))
532 V_I4( ret ) = count_selected_properties( view );
533 if (type) *type = CIM_SINT32;
536 else if (!strcmpiW( name, relpathW ))
538 V_VT( ret ) = VT_BSTR;
539 V_BSTR( ret ) = build_relpath( view, index, name );
540 if (type) *type = CIM_STRING;
543 else if (!strcmpiW( name, serverW ))
545 V_VT( ret ) = VT_BSTR;
546 V_BSTR( ret ) = build_servername( view );
547 if (type) *type = CIM_STRING;
550 FIXME("system property %s not implemented\n", debugstr_w(name));
551 return WBEM_E_NOT_FOUND;
554 VARTYPE to_vartype( CIMTYPE type )
558 case CIM_BOOLEAN: return VT_BOOL;
560 case CIM_DATETIME: return VT_BSTR;
561 case CIM_SINT16: return VT_I2;
562 case CIM_UINT16: return VT_UI2;
563 case CIM_SINT32: return VT_I4;
564 case CIM_UINT32: return VT_UI4;
565 case CIM_SINT64: return VT_I8;
566 case CIM_UINT64: return VT_UI8;
568 ERR("unhandled type %u\n", type);
574 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE type )
577 UINT size = get_type_size( type );
578 VARTYPE vartype = to_vartype( type );
581 if (!(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
583 for (i = 0; i < array->count; i++)
585 void *ptr = (char *)array->ptr + i * size;
586 if (vartype == VT_BSTR)
588 BSTR str = SysAllocString( *(const WCHAR **)ptr );
589 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
591 SysFreeString( str );
592 SafeArrayDestroy( ret );
596 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
598 SafeArrayDestroy( ret );
605 static void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
610 V_ARRAY( ret ) = val_ptr;
619 V_BSTR( ret ) = val_ptr;
636 ERR("unhandled variant type %u\n", type);
642 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
643 CIMTYPE *type, LONG *flavor )
646 UINT column, row = view->result[index];
648 void *val_ptr = NULL;
651 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
652 if (!is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
654 hr = get_column_index( view->table, name, &column );
655 if (hr != S_OK || is_method( view->table, column )) return WBEM_E_NOT_FOUND;
657 vartype = view->table->columns[column].vartype;
659 hr = get_value( view->table, row, column, &val );
660 if (hr != S_OK) return hr;
662 if (view->table->columns[column].type & CIM_FLAG_ARRAY)
664 CIMTYPE basetype = view->table->columns[column].type & CIM_TYPE_MASK;
666 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
667 if (!vartype) vartype = to_vartype( basetype ) | VT_ARRAY;
670 switch (view->table->columns[column].type & COL_TYPE_MASK)
673 if (!vartype) vartype = VT_BOOL;
680 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
686 if (!vartype) vartype = VT_I2;
689 if (!vartype) vartype = VT_UI2;
692 if (!vartype) vartype = VT_I4;
695 if (!vartype) vartype = VT_UI4;
699 val_ptr = get_value_bstr( view->table, row, column );
703 val_ptr = get_value_bstr( view->table, row, column );
706 ERR("unhandled column type %u\n", view->table->columns[column].type);
707 return WBEM_E_FAILED;
711 set_variant( vartype, val, val_ptr, ret );
712 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
713 if (flavor) *flavor = 0;
717 static CIMTYPE to_cimtype( VARTYPE type )
721 case VT_BOOL: return CIM_BOOLEAN;
722 case VT_BSTR: return CIM_STRING;
723 case VT_I2: return CIM_SINT16;
724 case VT_UI2: return CIM_UINT16;
725 case VT_I4: return CIM_SINT32;
726 case VT_UI4: return CIM_UINT32;
727 case VT_I8: return CIM_SINT64;
728 case VT_UI8: return CIM_UINT64;
730 ERR("unhandled type %u\n", type);
736 static struct array *to_array( VARIANT *var, CIMTYPE *type )
744 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
745 if (!(basetype = to_cimtype( vartype ))) return NULL;
746 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
747 if (!(ret = heap_alloc( sizeof(struct array) ))) return NULL;
749 ret->count = bound + 1;
750 size = get_type_size( basetype );
751 if (!(ret->ptr = heap_alloc_zero( ret->count * size )))
756 for (i = 0; i < ret->count; i++)
758 void *ptr = (char *)ret->ptr + i * size;
759 if (vartype == VT_BSTR)
762 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
764 destroy_array( ret, basetype );
767 *(WCHAR **)ptr = heap_strdupW( str );
768 SysFreeString( str );
771 destroy_array( ret, basetype );
775 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
777 destroy_array( ret, basetype );
781 *type = basetype | CIM_FLAG_ARRAY;
785 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
792 if (V_VT( var ) & VT_ARRAY)
794 *val = (INT_PTR)to_array( var, type );
795 if (!*val) return E_OUTOFMEMORY;
801 *val = V_BOOL( var );
805 *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
806 if (!*val) return E_OUTOFMEMORY;
829 ERR("unhandled type %u\n", V_VT( var ));
830 return WBEM_E_FAILED;
835 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
838 UINT column, row = view->result[index];
841 hr = get_column_index( view->table, name, &column );
844 FIXME("no support for creating new properties\n");
845 return WBEM_E_FAILED;
847 if (is_method( view->table, column ) || !(view->table->columns[column].type & COL_FLAG_DYNAMIC))
848 return WBEM_E_FAILED;
850 hr = to_longlong( var, &val, &type );
851 if (hr != S_OK) return hr;
853 return set_value( view->table, row, column, val, type );
856 HRESULT get_properties( const struct view *view, SAFEARRAY **props )
861 UINT num_props = count_properties( view );
863 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
865 for (i = 0; i < view->table->num_cols; i++)
867 if (is_method( view->table, i )) continue;
869 str = SysAllocString( view->table->columns[i].name );
870 if (!str || SafeArrayPutElement( sa, &i, str ) != S_OK)
872 SysFreeString( str );
873 SafeArrayDestroy( sa );
874 return E_OUTOFMEMORY;