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 )
51 if (view->table) release_table( view->table );
52 heap_free( view->result );
56 static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
58 const WCHAR *p = lstr, *q = rstr;
64 while (*q == '%') q++;
66 while (*p && toupperW( p[1] ) != toupperW( q[1] )) p++;
69 if (toupperW( *p++ ) != toupperW( *q++ )) return FALSE;
74 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
84 *val = !strcmpW( lstr, rstr );
87 *val = strcmpW( lstr, rstr ) > 0;
90 *val = strcmpW( lstr, rstr ) < 0;
93 *val = strcmpW( lstr, rstr ) <= 0;
96 *val = strcmpW( lstr, rstr ) >= 0;
99 *val = strcmpW( lstr, rstr );
102 *val = eval_like( lstr, rstr );
105 ERR("unhandled operator %u\n", op);
106 return WBEM_E_INVALID_QUERY;
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 HRESULT eval_cond( const struct table *, UINT, const struct expr *, LONGLONG * );
119 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
125 lret = eval_cond( table, row, expr->left, &lval );
126 rret = eval_cond( table, row, expr->right, &rval );
127 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
129 if (is_strcmp( expr ))
131 const WCHAR *lstr = (const WCHAR *)(INT_PTR)lval;
132 const WCHAR *rstr = (const WCHAR *)(INT_PTR)rval;
134 return eval_strcmp( expr->op, lstr, rstr, val );
139 *val = (lval == rval);
142 *val = (lval && rval);
145 *val = (lval || rval);
148 *val = (lval > rval);
151 *val = (lval < rval);
154 *val = (lval <= rval);
157 *val = (lval >= rval);
160 *val = (lval != rval);
163 ERR("unhandled operator %u\n", expr->op);
164 return WBEM_E_INVALID_QUERY;
169 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
177 hr = get_column_index( table, expr->left->u.propval->name, &column );
181 hr = get_value( table, row, column, &lval );
194 ERR("unknown operator %u\n", expr->op);
195 return WBEM_E_INVALID_QUERY;
200 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
207 hr = get_column_index( table, propval->name, &column );
211 return get_value( table, row, column, val );
214 static HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond,
225 return eval_binary( table, row, &cond->u.expr, val );
227 return eval_unary( table, row, &cond->u.expr, val );
229 return eval_propval( table, row, cond->u.propval, val );
231 *val = (INT_PTR)cond->u.sval;
238 ERR("invalid expression type\n");
241 return WBEM_E_INVALID_QUERY;
244 static HRESULT execute_view( struct view *view )
248 if (!view->table || !view->table->num_rows) return S_OK;
250 len = min( view->table->num_rows, 16 );
251 if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
253 for (i = 0; i < view->table->num_rows; i++)
262 if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
265 if ((hr = eval_cond( view->table, i, view->cond, &val )) != S_OK) return hr;
266 if (val) view->result[j++] = i;
272 static struct query *create_query(void)
276 if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
277 list_init( &query->mem );
282 static void free_query( struct query *query )
284 struct list *mem, *next;
286 destroy_view( query->view );
287 LIST_FOR_EACH_SAFE( mem, next, &query->mem )
294 struct query *addref_query( struct query *query )
296 InterlockedIncrement( &query->refs );
300 void release_query( struct query *query )
302 if (!InterlockedDecrement( &query->refs )) free_query( query );
305 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
311 if (!(query = create_query())) return E_OUTOFMEMORY;
312 hr = parse_query( str, &query->view, &query->mem );
313 if (hr != S_OK) goto done;
314 hr = execute_view( query->view );
315 if (hr != S_OK) goto done;
316 hr = EnumWbemClassObject_create( NULL, query, (void **)result );
319 release_query( query );
323 static BOOL is_selected_prop( const struct view *view, const WCHAR *name )
325 const struct property *prop = view->proplist;
327 if (!prop) return TRUE;
330 if (!strcmpiW( prop->name, name )) return TRUE;
336 static BOOL is_system_prop( const WCHAR *name )
338 return (name[0] == '_' && name[1] == '_');
341 static BSTR build_servername( const struct view *view )
343 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
344 DWORD len = sizeof(server)/sizeof(server[0]);
346 if (view->proplist) return NULL;
348 if (!(GetComputerNameW( server, &len ))) return NULL;
349 for (p = server; *p; p++) *p = toupperW( *p );
350 return SysAllocString( server );
353 static BSTR build_classname( const struct view *view )
355 return SysAllocString( view->table->name );
358 static BSTR build_namespace( const struct view *view )
360 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
362 if (view->proplist) return NULL;
363 return SysAllocString( cimv2W );
366 static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
368 static const WCHAR fmtW[] = {'%','s','=','%','s',0};
369 UINT i, j, offset, row = view->result[index];
370 BSTR *values, ret = NULL;
372 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
375 for (i = 0; i < view->table->num_cols; i++)
377 if (view->table->columns[i].type & COL_FLAG_KEY)
379 const WCHAR *name = view->table->columns[i].name;
381 values[j] = get_value_bstr( view->table, row, i );
382 *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
386 if ((ret = SysAllocStringLen( NULL, *len )))
389 for (i = 0; i < view->table->num_cols; i++)
391 if (view->table->columns[i].type & COL_FLAG_KEY)
393 const WCHAR *name = view->table->columns[i].name;
395 offset += sprintfW( ret + offset, fmtW, name, values[j] );
396 if (j < count - 1) ret[offset++] = ',';
401 for (i = 0; i < count; i++) SysFreeString( values[i] );
406 static UINT count_key_columns( const struct view *view )
408 UINT i, num_keys = 0;
410 for (i = 0; i < view->table->num_cols; i++)
412 if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
417 static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
419 static const WCHAR fmtW[] = {'%','s','.','%','s',0};
420 BSTR class, proplist, ret = NULL;
423 if (view->proplist) return NULL;
425 if (!(class = build_classname( view ))) return NULL;
426 if (!(num_keys = count_key_columns( view ))) return class;
427 if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;
429 len += strlenW( fmtW ) + SysStringLen( class );
430 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
431 sprintfW( ret, fmtW, class, proplist );
434 SysFreeString( class );
435 SysFreeString( proplist );
439 static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
441 static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
442 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
445 if (view->proplist) return NULL;
447 if (!(server = build_servername( view ))) return NULL;
448 if (!(namespace = build_namespace( view ))) goto done;
449 if (!(relpath = build_relpath( view, index, name ))) goto done;
451 len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
452 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
453 sprintfW( ret, fmtW, server, namespace, relpath );
456 SysFreeString( server );
457 SysFreeString( namespace );
458 SysFreeString( relpath );
462 static inline BOOL is_method( const struct table *table, UINT column )
464 return table->columns[column].type & COL_FLAG_METHOD;
467 static UINT count_properties( const struct view *view )
469 UINT i, num_props = 0;
471 for (i = 0; i < view->table->num_cols; i++)
473 if (!is_method( view->table, i)) num_props++;
478 static UINT count_selected_properties( const struct view *view )
480 const struct property *prop = view->proplist;
483 if (!prop) return count_properties( view );
486 while ((prop = prop->next)) count++;
490 static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
491 VARIANT *ret, CIMTYPE *type, LONG *flavor )
493 static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
494 static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
495 static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
496 static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
497 static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
498 static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
499 static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};
501 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
503 if (!strcmpiW( name, classW ))
505 V_VT( ret ) = VT_BSTR;
506 V_BSTR( ret ) = build_classname( view );
507 if (type) *type = CIM_STRING;
510 if (!strcmpiW( name, genusW ))
513 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
514 if (type) *type = CIM_SINT32;
517 else if (!strcmpiW( name, namespaceW ))
519 V_VT( ret ) = VT_BSTR;
520 V_BSTR( ret ) = build_namespace( view );
521 if (type) *type = CIM_STRING;
524 else if (!strcmpiW( name, pathW ))
526 V_VT( ret ) = VT_BSTR;
527 V_BSTR( ret ) = build_path( view, index, name );
528 if (type) *type = CIM_STRING;
531 if (!strcmpiW( name, propcountW ))
534 V_I4( ret ) = count_selected_properties( view );
535 if (type) *type = CIM_SINT32;
538 else if (!strcmpiW( name, relpathW ))
540 V_VT( ret ) = VT_BSTR;
541 V_BSTR( ret ) = build_relpath( view, index, name );
542 if (type) *type = CIM_STRING;
545 else if (!strcmpiW( name, serverW ))
547 V_VT( ret ) = VT_BSTR;
548 V_BSTR( ret ) = build_servername( view );
549 if (type) *type = CIM_STRING;
552 FIXME("system property %s not implemented\n", debugstr_w(name));
553 return WBEM_E_NOT_FOUND;
556 VARTYPE to_vartype( CIMTYPE type )
560 case CIM_BOOLEAN: return VT_BOOL;
562 case CIM_DATETIME: return VT_BSTR;
563 case CIM_SINT16: return VT_I2;
564 case CIM_UINT16: return VT_UI2;
565 case CIM_SINT32: return VT_I4;
566 case CIM_UINT32: return VT_UI4;
567 case CIM_SINT64: return VT_I8;
568 case CIM_UINT64: return VT_UI8;
570 ERR("unhandled type %u\n", type);
576 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE type )
579 UINT size = get_type_size( type );
580 VARTYPE vartype = to_vartype( type );
583 if (!(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
585 for (i = 0; i < array->count; i++)
587 void *ptr = (char *)array->ptr + i * size;
588 if (vartype == VT_BSTR)
590 BSTR str = SysAllocString( *(const WCHAR **)ptr );
591 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
593 SysFreeString( str );
594 SafeArrayDestroy( ret );
598 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
600 SafeArrayDestroy( ret );
607 void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
612 V_ARRAY( ret ) = val_ptr;
621 V_BSTR( ret ) = val_ptr;
638 ERR("unhandled variant type %u\n", type);
644 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
645 CIMTYPE *type, LONG *flavor )
650 void *val_ptr = NULL;
653 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
654 if (!view->count || !is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
656 hr = get_column_index( view->table, name, &column );
657 if (hr != S_OK || is_method( view->table, column )) return WBEM_E_NOT_FOUND;
659 row = view->result[index];
660 hr = get_value( view->table, row, column, &val );
661 if (hr != S_OK) return hr;
663 vartype = view->table->columns[column].vartype;
664 if (view->table->columns[column].type & CIM_FLAG_ARRAY)
666 CIMTYPE basetype = view->table->columns[column].type & CIM_TYPE_MASK;
668 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
669 if (!vartype) vartype = to_vartype( basetype ) | VT_ARRAY;
672 switch (view->table->columns[column].type & COL_TYPE_MASK)
675 if (!vartype) vartype = VT_BOOL;
682 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
688 if (!vartype) vartype = VT_I2;
691 if (!vartype) vartype = VT_UI2;
694 if (!vartype) vartype = VT_I4;
697 if (!vartype) vartype = VT_UI4;
701 val_ptr = get_value_bstr( view->table, row, column );
705 val_ptr = get_value_bstr( view->table, row, column );
708 ERR("unhandled column type %u\n", view->table->columns[column].type);
709 return WBEM_E_FAILED;
713 set_variant( vartype, val, val_ptr, ret );
714 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
715 if (flavor) *flavor = 0;
719 static CIMTYPE to_cimtype( VARTYPE type )
723 case VT_BOOL: return CIM_BOOLEAN;
724 case VT_BSTR: return CIM_STRING;
725 case VT_I2: return CIM_SINT16;
726 case VT_UI2: return CIM_UINT16;
727 case VT_I4: return CIM_SINT32;
728 case VT_UI4: return CIM_UINT32;
729 case VT_I8: return CIM_SINT64;
730 case VT_UI8: return CIM_UINT64;
732 ERR("unhandled type %u\n", type);
738 static struct array *to_array( VARIANT *var, CIMTYPE *type )
746 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
747 if (!(basetype = to_cimtype( vartype ))) return NULL;
748 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
749 if (!(ret = heap_alloc( sizeof(struct array) ))) return NULL;
751 ret->count = bound + 1;
752 size = get_type_size( basetype );
753 if (!(ret->ptr = heap_alloc_zero( ret->count * size )))
758 for (i = 0; i < ret->count; i++)
760 void *ptr = (char *)ret->ptr + i * size;
761 if (vartype == VT_BSTR)
764 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
766 destroy_array( ret, basetype );
769 *(WCHAR **)ptr = heap_strdupW( str );
770 SysFreeString( str );
773 destroy_array( ret, basetype );
777 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
779 destroy_array( ret, basetype );
783 *type = basetype | CIM_FLAG_ARRAY;
787 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
794 if (V_VT( var ) & VT_ARRAY)
796 *val = (INT_PTR)to_array( var, type );
797 if (!*val) return E_OUTOFMEMORY;
803 *val = V_BOOL( var );
807 *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
808 if (!*val) return E_OUTOFMEMORY;
831 ERR("unhandled type %u\n", V_VT( var ));
832 return WBEM_E_FAILED;
837 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
840 UINT column, row = view->result[index];
843 hr = get_column_index( view->table, name, &column );
846 FIXME("no support for creating new properties\n");
847 return WBEM_E_FAILED;
849 if (is_method( view->table, column ) || !(view->table->columns[column].type & COL_FLAG_DYNAMIC))
850 return WBEM_E_FAILED;
852 hr = to_longlong( var, &val, &type );
853 if (hr != S_OK) return hr;
855 return set_value( view->table, row, column, val, type );
858 HRESULT get_properties( const struct view *view, SAFEARRAY **props )
863 UINT num_props = count_properties( view );
865 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
867 for (i = 0; i < view->table->num_cols; i++)
869 if (is_method( view->table, i )) continue;
871 str = SysAllocString( view->table->columns[i].name );
872 if (!str || SafeArrayPutElement( sa, &i, str ) != S_OK)
874 SysFreeString( str );
875 SafeArrayDestroy( sa );
876 return E_OUTOFMEMORY;