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)
61 return sizeof(WCHAR *);
63 ERR("unkown column type %u\n", table->columns[column].type & COL_TYPE_MASK);
69 static UINT get_column_offset( const struct table *table, UINT column )
72 for (i = 0; i < column; i++) offset += get_column_size( table, i );
76 static UINT get_row_size( const struct table *table )
78 return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
81 static HRESULT get_value( const struct table *table, UINT row, UINT column, INT_PTR *val )
83 UINT col_offset, row_size;
86 col_offset = get_column_offset( table, column );
87 row_size = get_row_size( table );
88 ptr = table->data + row * row_size + col_offset;
90 if (table->columns[column].type & CIM_FLAG_ARRAY)
92 *val = (INT_PTR)*(const void **)ptr;
95 switch (table->columns[column].type & COL_TYPE_MASK)
99 *val = (INT_PTR)*(const WCHAR **)ptr;
102 *val = *(const INT16 *)ptr;
105 *val = *(const UINT16 *)ptr;
108 *val = *(const INT32 *)ptr;
111 *val = *(const UINT32 *)ptr;
114 ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
121 static BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
123 static const WCHAR fmt_signedW[] = {'%','d',0};
124 static const WCHAR fmt_unsignedW[] = {'%','u',0};
125 static const WCHAR fmt_strW[] = {'\"','%','s','\"',0};
131 if (table->columns[column].type & CIM_FLAG_ARRAY)
133 FIXME("array to string conversion not handled\n");
136 if (get_value( table, row, column, &val ) != S_OK) return NULL;
138 switch (table->columns[column].type & COL_TYPE_MASK)
142 len = strlenW( (const WCHAR *)val ) + 2;
143 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
144 sprintfW( ret, fmt_strW, (const WCHAR *)val );
149 sprintfW( number, fmt_signedW, val );
150 return SysAllocString( number );
154 sprintfW( number, fmt_unsignedW, val );
155 return SysAllocString( number );
158 FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
164 HRESULT create_view( const struct property *proplist, const WCHAR *class,
165 const struct expr *cond, struct view **ret )
167 struct view *view = heap_alloc( sizeof(struct view) );
169 if (!view) return E_OUTOFMEMORY;
170 view->proplist = proplist;
171 view->table = get_table( class );
180 static void clear_table( struct table *table )
184 if (!table->fill || !table->data) return;
186 for (i = 0; i < table->num_rows; i++)
188 for (j = 0; j < table->num_cols; j++)
190 if (!(table->columns[j].type & COL_FLAG_DYNAMIC)) continue;
192 type = table->columns[j].type & COL_TYPE_MASK;
193 if (type == CIM_STRING || type == CIM_DATETIME || (type & CIM_FLAG_ARRAY))
196 if (get_value( table, i, j, (INT_PTR *)&ptr ) == S_OK) heap_free( ptr );
200 heap_free( table->data );
204 void destroy_view( struct view *view )
206 if (view->table) clear_table( view->table );
207 heap_free( view->result );
211 static BOOL eval_like( INT_PTR lval, INT_PTR rval )
213 const WCHAR *p = (const WCHAR *)lval, *q = (const WCHAR *)rval;
219 while (*q == '%') q++;
220 if (!*q) return TRUE;
221 while (*p && toupperW( p[1] ) != toupperW( q[1] )) p++;
222 if (!*p) return TRUE;
224 if (toupperW( *p++ ) != toupperW( *q++ )) return FALSE;
229 static HRESULT eval_cond( const struct table *, UINT, const struct expr *, INT_PTR * );
231 static BOOL eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
237 lret = eval_cond( table, row, expr->left, &lval );
238 rret = eval_cond( table, row, expr->right, &rval );
239 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
244 *val = (lval == rval);
247 *val = (lval && rval);
250 *val = (lval || rval);
253 *val = (lval > rval);
256 *val = (lval < rval);
259 *val = (lval <= rval);
262 *val = (lval >= rval);
265 *val = (lval != rval);
268 *val = eval_like( lval, rval );
271 ERR("unknown operator %u\n", expr->op);
272 return WBEM_E_INVALID_QUERY;
277 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
285 hr = get_column_index( table, expr->left->u.propval->name, &column );
289 hr = get_value( table, row, column, &lval );
302 ERR("unknown operator %u\n", expr->op);
303 return WBEM_E_INVALID_QUERY;
308 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
315 hr = get_column_index( table, propval->name, &column );
319 return get_value( table, row, column, val );
322 static HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond,
333 return eval_binary( table, row, &cond->u.expr, val );
335 return eval_unary( table, row, &cond->u.expr, val );
337 return eval_propval( table, row, cond->u.propval, val );
339 *val = (INT_PTR)cond->u.sval;
346 ERR("invalid expression type\n");
349 return WBEM_E_INVALID_QUERY;
352 static HRESULT execute_view( struct view *view )
356 if (!view->table || !view->table->num_rows) return S_OK;
358 len = min( view->table->num_rows, 16 );
359 if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
361 for (i = 0; i < view->table->num_rows; i++)
370 if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
373 if ((hr = eval_cond( view->table, i, view->cond, &val )) != S_OK) return hr;
374 if (val) view->result[j++] = i;
380 static struct query *alloc_query(void)
384 if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
385 list_init( &query->mem );
389 void free_query( struct query *query )
391 struct list *mem, *next;
393 destroy_view( query->view );
394 LIST_FOR_EACH_SAFE( mem, next, &query->mem )
401 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
407 if (!(query = alloc_query())) return E_OUTOFMEMORY;
408 hr = parse_query( str, &query->view, &query->mem );
409 if (hr != S_OK) goto done;
410 hr = execute_view( query->view );
411 if (hr != S_OK) goto done;
412 hr = EnumWbemClassObject_create( NULL, query, (void **)result );
415 if (hr != S_OK) free_query( query );
419 static BOOL is_selected_prop( const struct view *view, const WCHAR *name )
421 const struct property *prop = view->proplist;
423 if (!prop) return TRUE;
426 if (!strcmpiW( prop->name, name )) return TRUE;
432 static BOOL is_system_prop( const WCHAR *name )
434 return (name[0] == '_' && name[1] == '_');
437 static BSTR build_servername( const struct view *view )
439 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
440 DWORD len = sizeof(server)/sizeof(server[0]);
442 if (view->proplist) return NULL;
444 if (!(GetComputerNameW( server, &len ))) return NULL;
445 for (p = server; *p; p++) *p = toupperW( *p );
446 return SysAllocString( server );
449 static BSTR build_classname( const struct view *view )
451 return SysAllocString( view->table->name );
454 static BSTR build_namespace( const struct view *view )
456 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
458 if (view->proplist) return NULL;
459 return SysAllocString( cimv2W );
462 static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
464 static const WCHAR fmtW[] = {'%','s','=','%','s',0};
465 UINT i, j, offset, row = view->result[index];
466 BSTR *values, ret = NULL;
468 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
471 for (i = 0; i < view->table->num_cols; i++)
473 if (view->table->columns[i].type & COL_FLAG_KEY)
475 const WCHAR *name = view->table->columns[i].name;
477 values[j] = get_value_bstr( view->table, row, i );
478 *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
482 if ((ret = SysAllocStringLen( NULL, *len )))
485 for (i = 0; i < view->table->num_cols; i++)
487 if (view->table->columns[i].type & COL_FLAG_KEY)
489 const WCHAR *name = view->table->columns[i].name;
491 offset += sprintfW( ret + offset, fmtW, name, values[j] );
492 if (j < count - 1) ret[offset++] = ',';
497 for (i = 0; i < count; i++) SysFreeString( values[i] );
502 static UINT count_key_columns( const struct view *view )
504 UINT i, num_keys = 0;
506 for (i = 0; i < view->table->num_cols; i++)
508 if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
513 static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
515 static const WCHAR fmtW[] = {'%','s','.','%','s',0};
516 BSTR class, proplist, ret = NULL;
519 if (view->proplist) return NULL;
521 if (!(class = build_classname( view ))) return NULL;
522 if (!(num_keys = count_key_columns( view ))) return class;
523 if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;
525 len += strlenW( fmtW ) + SysStringLen( class );
526 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
527 sprintfW( ret, fmtW, class, proplist );
530 SysFreeString( class );
531 SysFreeString( proplist );
535 static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
537 static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
538 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
541 if (view->proplist) return NULL;
543 if (!(server = build_servername( view ))) return NULL;
544 if (!(namespace = build_namespace( view ))) goto done;
545 if (!(relpath = build_relpath( view, index, name ))) goto done;
547 len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
548 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
549 sprintfW( ret, fmtW, server, namespace, relpath );
552 SysFreeString( server );
553 SysFreeString( namespace );
554 SysFreeString( relpath );
558 static UINT count_selected_props( const struct view *view )
560 const struct property *prop = view->proplist;
563 if (!prop) return view->table->num_cols;
566 while ((prop = prop->next)) count++;
570 static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
571 VARIANT *ret, CIMTYPE *type )
573 static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
574 static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
575 static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
576 static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
577 static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
578 static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
579 static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};
581 if (!strcmpiW( name, classW ))
583 V_VT( ret ) = VT_BSTR;
584 V_BSTR( ret ) = build_classname( view );
585 if (type) *type = CIM_STRING;
588 if (!strcmpiW( name, genusW ))
590 V_VT( ret ) = VT_INT;
591 V_INT( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
592 if (type) *type = CIM_SINT32;
595 else if (!strcmpiW( name, namespaceW ))
597 V_VT( ret ) = VT_BSTR;
598 V_BSTR( ret ) = build_namespace( view );
599 if (type) *type = CIM_STRING;
602 else if (!strcmpiW( name, pathW ))
604 V_VT( ret ) = VT_BSTR;
605 V_BSTR( ret ) = build_path( view, index, name );
606 if (type) *type = CIM_STRING;
609 if (!strcmpiW( name, propcountW ))
611 V_VT( ret ) = VT_INT;
612 V_INT( ret ) = count_selected_props( view );
613 if (type) *type = CIM_SINT32;
616 else if (!strcmpiW( name, relpathW ))
618 V_VT( ret ) = VT_BSTR;
619 V_BSTR( ret ) = build_relpath( view, index, name );
620 if (type) *type = CIM_STRING;
623 else if (!strcmpiW( name, serverW ))
625 V_VT( ret ) = VT_BSTR;
626 V_BSTR( ret ) = build_servername( view );
627 if (type) *type = CIM_STRING;
630 FIXME("system property %s not implemented\n", debugstr_w(name));
631 return WBEM_E_NOT_FOUND;
634 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret, CIMTYPE *type )
637 UINT column, row = view->result[index];
640 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type );
641 if (!is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
643 hr = get_column_index( view->table, name, &column );
644 if (hr != S_OK) return WBEM_E_NOT_FOUND;
646 hr = get_value( view->table, row, column, &val );
647 if (hr != S_OK) return hr;
649 switch (view->table->columns[column].type & COL_TYPE_MASK)
653 V_VT( ret ) = VT_BSTR;
654 V_BSTR( ret ) = SysAllocString( (const WCHAR *)val );
661 V_VT( ret ) = VT_UI2;
669 V_VT( ret ) = VT_UI4;
673 ERR("unhandled column type %u\n", view->table->columns[column].type);
674 return WBEM_E_FAILED;
676 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
680 HRESULT get_properties( const struct view *view, SAFEARRAY **props )
686 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, view->table->num_cols ))) return E_OUTOFMEMORY;
688 for (i = 0; i < view->table->num_cols; i++)
690 str = SysAllocString( view->table->columns[i].name );
691 if (!str || SafeArrayPutElement( sa, &i, str ) != S_OK)
693 SysFreeString( str );
694 SafeArrayDestroy( sa );
695 return E_OUTOFMEMORY;