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 static HRESULT get_column_index( const struct table *table, const WCHAR *name, UINT *column )
36 for (i = 0; i < table->num_cols; i++)
38 if (!strcmpiW( table->columns[i].name, name ))
44 return WBEM_E_INVALID_QUERY;
47 static UINT get_column_size( const struct table *table, UINT column )
49 if (table->columns[column].type & CIM_FLAG_ARRAY) return sizeof(void *);
51 switch (table->columns[column].type & COL_TYPE_MASK)
64 return sizeof(WCHAR *);
66 ERR("unknown column type %u\n", table->columns[column].type & COL_TYPE_MASK);
72 static UINT get_column_offset( const struct table *table, UINT column )
75 for (i = 0; i < column; i++) offset += get_column_size( table, i );
79 static UINT get_row_size( const struct table *table )
81 return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
84 static HRESULT get_value( const struct table *table, UINT row, UINT column, LONGLONG *val )
86 UINT col_offset, row_size;
89 col_offset = get_column_offset( table, column );
90 row_size = get_row_size( table );
91 ptr = table->data + row * row_size + col_offset;
93 if (table->columns[column].type & CIM_FLAG_ARRAY)
95 *val = (LONGLONG)(INT_PTR)*(const void **)ptr;
98 switch (table->columns[column].type & COL_TYPE_MASK)
102 *val = (LONGLONG)(INT_PTR)*(const WCHAR **)ptr;
105 *val = *(const INT16 *)ptr;
108 *val = *(const UINT16 *)ptr;
111 *val = *(const INT32 *)ptr;
114 *val = *(const UINT32 *)ptr;
117 *val = *(const INT64 *)ptr;
120 *val = *(const UINT64 *)ptr;
123 ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
130 static BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
132 static const WCHAR fmt_signedW[] = {'%','d',0};
133 static const WCHAR fmt_unsignedW[] = {'%','u',0};
134 static const WCHAR fmt_signed64W[] = {'%','I','6','4','d',0};
135 static const WCHAR fmt_unsigned64W[] = {'%','I','6','4','u',0};
136 static const WCHAR fmt_strW[] = {'\"','%','s','\"',0};
142 if (table->columns[column].type & CIM_FLAG_ARRAY)
144 FIXME("array to string conversion not handled\n");
147 if (get_value( table, row, column, &val ) != S_OK) return NULL;
149 switch (table->columns[column].type & COL_TYPE_MASK)
153 len = strlenW( (const WCHAR *)(INT_PTR)val ) + 2;
154 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
155 sprintfW( ret, fmt_strW, (const WCHAR *)(INT_PTR)val );
160 sprintfW( number, fmt_signedW, val );
161 return SysAllocString( number );
165 sprintfW( number, fmt_unsignedW, val );
166 return SysAllocString( number );
169 wsprintfW( number, fmt_signed64W, val );
170 return SysAllocString( number );
173 wsprintfW( number, fmt_unsigned64W, val );
174 return SysAllocString( number );
177 FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
183 HRESULT create_view( const struct property *proplist, const WCHAR *class,
184 const struct expr *cond, struct view **ret )
186 struct view *view = heap_alloc( sizeof(struct view) );
188 if (!view) return E_OUTOFMEMORY;
189 view->proplist = proplist;
190 view->table = get_table( class );
198 static void clear_table( struct table *table )
202 if (!table->fill || !table->data) return;
204 for (i = 0; i < table->num_rows; i++)
206 for (j = 0; j < table->num_cols; j++)
208 if (!(table->columns[j].type & COL_FLAG_DYNAMIC)) continue;
210 type = table->columns[j].type & COL_TYPE_MASK;
211 if (type == CIM_STRING || type == CIM_DATETIME || (type & CIM_FLAG_ARRAY))
214 if (get_value( table, i, j, (LONGLONG *)&ptr ) == S_OK) heap_free( ptr );
218 heap_free( table->data );
222 void destroy_view( struct view *view )
224 if (view->table) clear_table( view->table );
225 heap_free( view->result );
229 static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
231 const WCHAR *p = lstr, *q = rstr;
237 while (*q == '%') q++;
238 if (!*q) return TRUE;
239 while (*p && toupperW( p[1] ) != toupperW( q[1] )) p++;
240 if (!*p) return TRUE;
242 if (toupperW( *p++ ) != toupperW( *q++ )) return FALSE;
247 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
252 *val = !strcmpW( lstr, rstr );
255 *val = strcmpW( lstr, rstr ) > 0;
258 *val = strcmpW( lstr, rstr ) < 0;
261 *val = strcmpW( lstr, rstr ) <= 0;
264 *val = strcmpW( lstr, rstr ) >= 0;
267 *val = strcmpW( lstr, rstr );
270 *val = eval_like( lstr, rstr );
273 ERR("unhandled operator %u\n", op);
274 return WBEM_E_INVALID_QUERY;
279 static inline BOOL is_strcmp( const struct complex_expr *expr )
281 return ((expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) ||
282 (expr->left->type == EXPR_SVAL && expr->right->type == EXPR_PROPVAL));
285 static HRESULT eval_cond( const struct table *, UINT, const struct expr *, LONGLONG * );
287 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
293 lret = eval_cond( table, row, expr->left, &lval );
294 rret = eval_cond( table, row, expr->right, &rval );
295 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
297 if (is_strcmp( expr ))
299 const WCHAR *lstr = (const WCHAR *)(INT_PTR)lval;
300 const WCHAR *rstr = (const WCHAR *)(INT_PTR)rval;
302 return eval_strcmp( expr->op, lstr, rstr, val );
307 *val = (lval == rval);
310 *val = (lval && rval);
313 *val = (lval || rval);
316 *val = (lval > rval);
319 *val = (lval < rval);
322 *val = (lval <= rval);
325 *val = (lval >= rval);
328 *val = (lval != rval);
331 ERR("unhandled operator %u\n", expr->op);
332 return WBEM_E_INVALID_QUERY;
337 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
345 hr = get_column_index( table, expr->left->u.propval->name, &column );
349 hr = get_value( table, row, column, &lval );
362 ERR("unknown operator %u\n", expr->op);
363 return WBEM_E_INVALID_QUERY;
368 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
375 hr = get_column_index( table, propval->name, &column );
379 return get_value( table, row, column, val );
382 static HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond,
393 return eval_binary( table, row, &cond->u.expr, val );
395 return eval_unary( table, row, &cond->u.expr, val );
397 return eval_propval( table, row, cond->u.propval, val );
399 *val = (LONGLONG)(INT_PTR)cond->u.sval;
406 ERR("invalid expression type\n");
409 return WBEM_E_INVALID_QUERY;
412 static HRESULT execute_view( struct view *view )
416 if (!view->table || !view->table->num_rows) return S_OK;
418 len = min( view->table->num_rows, 16 );
419 if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
421 for (i = 0; i < view->table->num_rows; i++)
430 if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
433 if ((hr = eval_cond( view->table, i, view->cond, &val )) != S_OK) return hr;
434 if (val) view->result[j++] = i;
440 static struct query *create_query(void)
444 if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
445 list_init( &query->mem );
450 static void free_query( struct query *query )
452 struct list *mem, *next;
454 destroy_view( query->view );
455 LIST_FOR_EACH_SAFE( mem, next, &query->mem )
462 void addref_query( struct query *query )
464 InterlockedIncrement( &query->refs );
467 void release_query( struct query *query )
469 if (!InterlockedDecrement( &query->refs )) free_query( query );
472 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
478 if (!(query = create_query())) return E_OUTOFMEMORY;
479 hr = parse_query( str, &query->view, &query->mem );
480 if (hr != S_OK) goto done;
481 hr = execute_view( query->view );
482 if (hr != S_OK) goto done;
483 hr = EnumWbemClassObject_create( NULL, query, (void **)result );
486 release_query( query );
490 static BOOL is_selected_prop( const struct view *view, const WCHAR *name )
492 const struct property *prop = view->proplist;
494 if (!prop) return TRUE;
497 if (!strcmpiW( prop->name, name )) return TRUE;
503 static BOOL is_system_prop( const WCHAR *name )
505 return (name[0] == '_' && name[1] == '_');
508 static BSTR build_servername( const struct view *view )
510 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
511 DWORD len = sizeof(server)/sizeof(server[0]);
513 if (view->proplist) return NULL;
515 if (!(GetComputerNameW( server, &len ))) return NULL;
516 for (p = server; *p; p++) *p = toupperW( *p );
517 return SysAllocString( server );
520 static BSTR build_classname( const struct view *view )
522 return SysAllocString( view->table->name );
525 static BSTR build_namespace( const struct view *view )
527 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
529 if (view->proplist) return NULL;
530 return SysAllocString( cimv2W );
533 static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
535 static const WCHAR fmtW[] = {'%','s','=','%','s',0};
536 UINT i, j, offset, row = view->result[index];
537 BSTR *values, ret = NULL;
539 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
542 for (i = 0; i < view->table->num_cols; i++)
544 if (view->table->columns[i].type & COL_FLAG_KEY)
546 const WCHAR *name = view->table->columns[i].name;
548 values[j] = get_value_bstr( view->table, row, i );
549 *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
553 if ((ret = SysAllocStringLen( NULL, *len )))
556 for (i = 0; i < view->table->num_cols; i++)
558 if (view->table->columns[i].type & COL_FLAG_KEY)
560 const WCHAR *name = view->table->columns[i].name;
562 offset += sprintfW( ret + offset, fmtW, name, values[j] );
563 if (j < count - 1) ret[offset++] = ',';
568 for (i = 0; i < count; i++) SysFreeString( values[i] );
573 static UINT count_key_columns( const struct view *view )
575 UINT i, num_keys = 0;
577 for (i = 0; i < view->table->num_cols; i++)
579 if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
584 static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
586 static const WCHAR fmtW[] = {'%','s','.','%','s',0};
587 BSTR class, proplist, ret = NULL;
590 if (view->proplist) return NULL;
592 if (!(class = build_classname( view ))) return NULL;
593 if (!(num_keys = count_key_columns( view ))) return class;
594 if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;
596 len += strlenW( fmtW ) + SysStringLen( class );
597 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
598 sprintfW( ret, fmtW, class, proplist );
601 SysFreeString( class );
602 SysFreeString( proplist );
606 static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
608 static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
609 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
612 if (view->proplist) return NULL;
614 if (!(server = build_servername( view ))) return NULL;
615 if (!(namespace = build_namespace( view ))) goto done;
616 if (!(relpath = build_relpath( view, index, name ))) goto done;
618 len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
619 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
620 sprintfW( ret, fmtW, server, namespace, relpath );
623 SysFreeString( server );
624 SysFreeString( namespace );
625 SysFreeString( relpath );
629 static UINT count_selected_props( const struct view *view )
631 const struct property *prop = view->proplist;
634 if (!prop) return view->table->num_cols;
637 while ((prop = prop->next)) count++;
641 static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
642 VARIANT *ret, CIMTYPE *type, LONG *flavor )
644 static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
645 static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
646 static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
647 static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
648 static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
649 static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
650 static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};
652 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
654 if (!strcmpiW( name, classW ))
656 V_VT( ret ) = VT_BSTR;
657 V_BSTR( ret ) = build_classname( view );
658 if (type) *type = CIM_STRING;
661 if (!strcmpiW( name, genusW ))
664 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
665 if (type) *type = CIM_SINT32;
668 else if (!strcmpiW( name, namespaceW ))
670 V_VT( ret ) = VT_BSTR;
671 V_BSTR( ret ) = build_namespace( view );
672 if (type) *type = CIM_STRING;
675 else if (!strcmpiW( name, pathW ))
677 V_VT( ret ) = VT_BSTR;
678 V_BSTR( ret ) = build_path( view, index, name );
679 if (type) *type = CIM_STRING;
682 if (!strcmpiW( name, propcountW ))
685 V_I4( ret ) = count_selected_props( view );
686 if (type) *type = CIM_SINT32;
689 else if (!strcmpiW( name, relpathW ))
691 V_VT( ret ) = VT_BSTR;
692 V_BSTR( ret ) = build_relpath( view, index, name );
693 if (type) *type = CIM_STRING;
696 else if (!strcmpiW( name, serverW ))
698 V_VT( ret ) = VT_BSTR;
699 V_BSTR( ret ) = build_servername( view );
700 if (type) *type = CIM_STRING;
703 FIXME("system property %s not implemented\n", debugstr_w(name));
704 return WBEM_E_NOT_FOUND;
707 static void set_variant( VARTYPE vartype, LONGLONG val, BSTR val_bstr, VARIANT *ret )
712 V_VT( ret ) = VT_BSTR;
713 V_BSTR( ret ) = val_bstr;
720 V_VT( ret ) = VT_UI2;
728 V_VT( ret ) = VT_UI4;
732 V_VT( ret ) = VT_NULL;
735 ERR("unhandled variant type %u\n", vartype);
740 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
741 CIMTYPE *type, LONG *flavor )
744 UINT column, row = view->result[index];
746 BSTR val_bstr = NULL;
749 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
750 if (!is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
752 hr = get_column_index( view->table, name, &column );
753 if (hr != S_OK) return WBEM_E_NOT_FOUND;
755 vartype = view->table->columns[column].vartype;
757 hr = get_value( view->table, row, column, &val );
758 if (hr != S_OK) return hr;
760 switch (view->table->columns[column].type & COL_TYPE_MASK)
767 val_bstr = SysAllocString( (const WCHAR *)(INT_PTR)val );
773 if (!vartype) vartype = VT_I2;
776 if (!vartype) vartype = VT_UI2;
779 if (!vartype) vartype = VT_I4;
782 if (!vartype) vartype = VT_UI4;
786 val_bstr = get_value_bstr( view->table, row, column );
790 val_bstr = get_value_bstr( view->table, row, column );
793 ERR("unhandled column type %u\n", view->table->columns[column].type);
794 return WBEM_E_FAILED;
796 set_variant( vartype, val, val_bstr, ret );
797 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
798 if (flavor) *flavor = 0;
802 HRESULT get_properties( const struct view *view, SAFEARRAY **props )
808 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, view->table->num_cols ))) return E_OUTOFMEMORY;
810 for (i = 0; i < view->table->num_cols; i++)
812 str = SysAllocString( view->table->columns[i].name );
813 if (!str || SafeArrayPutElement( sa, &i, str ) != S_OK)
815 SysFreeString( str );
816 SafeArrayDestroy( sa );
817 return E_OUTOFMEMORY;