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 struct query *addref_query( struct query *query )
295 InterlockedIncrement( &query->refs );
299 void release_query( struct query *query )
301 if (!InterlockedDecrement( &query->refs )) free_query( query );
304 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
310 if (!(query = create_query())) return E_OUTOFMEMORY;
311 hr = parse_query( str, &query->view, &query->mem );
312 if (hr != S_OK) goto done;
313 hr = execute_view( query->view );
314 if (hr != S_OK) goto done;
315 hr = EnumWbemClassObject_create( NULL, query, (void **)result );
318 release_query( query );
322 static BOOL is_selected_prop( const struct view *view, const WCHAR *name )
324 const struct property *prop = view->proplist;
326 if (!prop) return TRUE;
329 if (!strcmpiW( prop->name, name )) return TRUE;
335 static BOOL is_system_prop( const WCHAR *name )
337 return (name[0] == '_' && name[1] == '_');
340 static BSTR build_servername( const struct view *view )
342 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
343 DWORD len = sizeof(server)/sizeof(server[0]);
345 if (view->proplist) return NULL;
347 if (!(GetComputerNameW( server, &len ))) return NULL;
348 for (p = server; *p; p++) *p = toupperW( *p );
349 return SysAllocString( server );
352 static BSTR build_classname( const struct view *view )
354 return SysAllocString( view->table->name );
357 static BSTR build_namespace( const struct view *view )
359 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
361 if (view->proplist) return NULL;
362 return SysAllocString( cimv2W );
365 static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
367 static const WCHAR fmtW[] = {'%','s','=','%','s',0};
368 UINT i, j, offset, row = view->result[index];
369 BSTR *values, ret = NULL;
371 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
374 for (i = 0; i < view->table->num_cols; i++)
376 if (view->table->columns[i].type & COL_FLAG_KEY)
378 const WCHAR *name = view->table->columns[i].name;
380 values[j] = get_value_bstr( view->table, row, i );
381 *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
385 if ((ret = SysAllocStringLen( NULL, *len )))
388 for (i = 0; i < view->table->num_cols; i++)
390 if (view->table->columns[i].type & COL_FLAG_KEY)
392 const WCHAR *name = view->table->columns[i].name;
394 offset += sprintfW( ret + offset, fmtW, name, values[j] );
395 if (j < count - 1) ret[offset++] = ',';
400 for (i = 0; i < count; i++) SysFreeString( values[i] );
405 static UINT count_key_columns( const struct view *view )
407 UINT i, num_keys = 0;
409 for (i = 0; i < view->table->num_cols; i++)
411 if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
416 static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
418 static const WCHAR fmtW[] = {'%','s','.','%','s',0};
419 BSTR class, proplist, ret = NULL;
422 if (view->proplist) return NULL;
424 if (!(class = build_classname( view ))) return NULL;
425 if (!(num_keys = count_key_columns( view ))) return class;
426 if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;
428 len += strlenW( fmtW ) + SysStringLen( class );
429 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
430 sprintfW( ret, fmtW, class, proplist );
433 SysFreeString( class );
434 SysFreeString( proplist );
438 static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
440 static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
441 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
444 if (view->proplist) return NULL;
446 if (!(server = build_servername( view ))) return NULL;
447 if (!(namespace = build_namespace( view ))) goto done;
448 if (!(relpath = build_relpath( view, index, name ))) goto done;
450 len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
451 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
452 sprintfW( ret, fmtW, server, namespace, relpath );
455 SysFreeString( server );
456 SysFreeString( namespace );
457 SysFreeString( relpath );
461 static inline BOOL is_method( const struct table *table, UINT column )
463 return table->columns[column].type & COL_FLAG_METHOD;
466 static UINT count_properties( const struct view *view )
468 UINT i, num_props = 0;
470 for (i = 0; i < view->table->num_cols; i++)
472 if (!is_method( view->table, i)) num_props++;
477 static UINT count_selected_properties( const struct view *view )
479 const struct property *prop = view->proplist;
482 if (!prop) return count_properties( view );
485 while ((prop = prop->next)) count++;
489 static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
490 VARIANT *ret, CIMTYPE *type, LONG *flavor )
492 static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
493 static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
494 static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
495 static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
496 static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
497 static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
498 static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};
500 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
502 if (!strcmpiW( name, classW ))
504 V_VT( ret ) = VT_BSTR;
505 V_BSTR( ret ) = build_classname( view );
506 if (type) *type = CIM_STRING;
509 if (!strcmpiW( name, genusW ))
512 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
513 if (type) *type = CIM_SINT32;
516 else if (!strcmpiW( name, namespaceW ))
518 V_VT( ret ) = VT_BSTR;
519 V_BSTR( ret ) = build_namespace( view );
520 if (type) *type = CIM_STRING;
523 else if (!strcmpiW( name, pathW ))
525 V_VT( ret ) = VT_BSTR;
526 V_BSTR( ret ) = build_path( view, index, name );
527 if (type) *type = CIM_STRING;
530 if (!strcmpiW( name, propcountW ))
533 V_I4( ret ) = count_selected_properties( view );
534 if (type) *type = CIM_SINT32;
537 else if (!strcmpiW( name, relpathW ))
539 V_VT( ret ) = VT_BSTR;
540 V_BSTR( ret ) = build_relpath( view, index, name );
541 if (type) *type = CIM_STRING;
544 else if (!strcmpiW( name, serverW ))
546 V_VT( ret ) = VT_BSTR;
547 V_BSTR( ret ) = build_servername( view );
548 if (type) *type = CIM_STRING;
551 FIXME("system property %s not implemented\n", debugstr_w(name));
552 return WBEM_E_NOT_FOUND;
555 VARTYPE to_vartype( CIMTYPE type )
559 case CIM_BOOLEAN: return VT_BOOL;
561 case CIM_DATETIME: return VT_BSTR;
562 case CIM_SINT16: return VT_I2;
563 case CIM_UINT16: return VT_UI2;
564 case CIM_SINT32: return VT_I4;
565 case CIM_UINT32: return VT_UI4;
566 case CIM_SINT64: return VT_I8;
567 case CIM_UINT64: return VT_UI8;
569 ERR("unhandled type %u\n", type);
575 SAFEARRAY *to_safearray( const struct array *array, CIMTYPE type )
578 UINT size = get_type_size( type );
579 VARTYPE vartype = to_vartype( type );
582 if (!(ret = SafeArrayCreateVector( vartype, 0, array->count ))) return NULL;
584 for (i = 0; i < array->count; i++)
586 void *ptr = (char *)array->ptr + i * size;
587 if (vartype == VT_BSTR)
589 BSTR str = SysAllocString( *(const WCHAR **)ptr );
590 if (!str || SafeArrayPutElement( ret, &i, str ) != S_OK)
592 SysFreeString( str );
593 SafeArrayDestroy( ret );
597 else if (SafeArrayPutElement( ret, &i, ptr ) != S_OK)
599 SafeArrayDestroy( ret );
606 void set_variant( VARTYPE type, LONGLONG val, void *val_ptr, VARIANT *ret )
611 V_ARRAY( ret ) = val_ptr;
620 V_BSTR( ret ) = val_ptr;
637 ERR("unhandled variant type %u\n", type);
643 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
644 CIMTYPE *type, LONG *flavor )
647 UINT column, row = view->result[index];
649 void *val_ptr = NULL;
652 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
653 if (!is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
655 hr = get_column_index( view->table, name, &column );
656 if (hr != S_OK || is_method( view->table, column )) return WBEM_E_NOT_FOUND;
658 vartype = view->table->columns[column].vartype;
660 hr = get_value( view->table, row, column, &val );
661 if (hr != S_OK) return hr;
663 if (view->table->columns[column].type & CIM_FLAG_ARRAY)
665 CIMTYPE basetype = view->table->columns[column].type & CIM_TYPE_MASK;
667 val_ptr = to_safearray( (const struct array *)(INT_PTR)val, basetype );
668 if (!vartype) vartype = to_vartype( basetype ) | VT_ARRAY;
671 switch (view->table->columns[column].type & COL_TYPE_MASK)
674 if (!vartype) vartype = VT_BOOL;
681 val_ptr = SysAllocString( (const WCHAR *)(INT_PTR)val );
687 if (!vartype) vartype = VT_I2;
690 if (!vartype) vartype = VT_UI2;
693 if (!vartype) vartype = VT_I4;
696 if (!vartype) vartype = VT_UI4;
700 val_ptr = get_value_bstr( view->table, row, column );
704 val_ptr = get_value_bstr( view->table, row, column );
707 ERR("unhandled column type %u\n", view->table->columns[column].type);
708 return WBEM_E_FAILED;
712 set_variant( vartype, val, val_ptr, ret );
713 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
714 if (flavor) *flavor = 0;
718 static CIMTYPE to_cimtype( VARTYPE type )
722 case VT_BOOL: return CIM_BOOLEAN;
723 case VT_BSTR: return CIM_STRING;
724 case VT_I2: return CIM_SINT16;
725 case VT_UI2: return CIM_UINT16;
726 case VT_I4: return CIM_SINT32;
727 case VT_UI4: return CIM_UINT32;
728 case VT_I8: return CIM_SINT64;
729 case VT_UI8: return CIM_UINT64;
731 ERR("unhandled type %u\n", type);
737 static struct array *to_array( VARIANT *var, CIMTYPE *type )
745 if (SafeArrayGetVartype( V_ARRAY( var ), &vartype ) != S_OK) return NULL;
746 if (!(basetype = to_cimtype( vartype ))) return NULL;
747 if (SafeArrayGetUBound( V_ARRAY( var ), 1, &bound ) != S_OK) return NULL;
748 if (!(ret = heap_alloc( sizeof(struct array) ))) return NULL;
750 ret->count = bound + 1;
751 size = get_type_size( basetype );
752 if (!(ret->ptr = heap_alloc_zero( ret->count * size )))
757 for (i = 0; i < ret->count; i++)
759 void *ptr = (char *)ret->ptr + i * size;
760 if (vartype == VT_BSTR)
763 if (SafeArrayGetElement( V_ARRAY( var ), &i, &str ) != S_OK)
765 destroy_array( ret, basetype );
768 *(WCHAR **)ptr = heap_strdupW( str );
769 SysFreeString( str );
772 destroy_array( ret, basetype );
776 else if (SafeArrayGetElement( V_ARRAY( var ), &i, ptr ) != S_OK)
778 destroy_array( ret, basetype );
782 *type = basetype | CIM_FLAG_ARRAY;
786 HRESULT to_longlong( VARIANT *var, LONGLONG *val, CIMTYPE *type )
793 if (V_VT( var ) & VT_ARRAY)
795 *val = (INT_PTR)to_array( var, type );
796 if (!*val) return E_OUTOFMEMORY;
802 *val = V_BOOL( var );
806 *val = (INT_PTR)heap_strdupW( V_BSTR( var ) );
807 if (!*val) return E_OUTOFMEMORY;
830 ERR("unhandled type %u\n", V_VT( var ));
831 return WBEM_E_FAILED;
836 HRESULT put_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *var, CIMTYPE type )
839 UINT column, row = view->result[index];
842 hr = get_column_index( view->table, name, &column );
845 FIXME("no support for creating new properties\n");
846 return WBEM_E_FAILED;
848 if (is_method( view->table, column ) || !(view->table->columns[column].type & COL_FLAG_DYNAMIC))
849 return WBEM_E_FAILED;
851 hr = to_longlong( var, &val, &type );
852 if (hr != S_OK) return hr;
854 return set_value( view->table, row, column, val, type );
857 HRESULT get_properties( const struct view *view, SAFEARRAY **props )
862 UINT num_props = count_properties( view );
864 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
866 for (i = 0; i < view->table->num_cols; i++)
868 if (is_method( view->table, i )) continue;
870 str = SysAllocString( view->table->columns[i].name );
871 if (!str || SafeArrayPutElement( sa, &i, str ) != S_OK)
873 SysFreeString( str );
874 SafeArrayDestroy( sa );
875 return E_OUTOFMEMORY;