msvcp90: Fixed EOF detection in basic_streambuf_char__Xsgetn_s function.
[wine/multimedia.git] / dlls / wbemprox / query.c
blob74b48e4dfdb3754d5b89fe1842956da0291f5f05
1 /*
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
19 #define COBJMACROS
21 #include "config.h"
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wbemcli.h"
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 = get_table( class );
41 view->cond = cond;
42 view->result = NULL;
43 view->count = 0;
44 *ret = view;
45 return S_OK;
48 void destroy_view( struct view *view )
50 free_table( view->table );
51 heap_free( view->result );
52 heap_free( view );
55 static BOOL eval_like( const WCHAR *lstr, const WCHAR *rstr )
57 const WCHAR *p = lstr, *q = rstr;
59 while (*p && *q)
61 if (*q == '%')
63 while (*q == '%') q++;
64 if (!*q) return TRUE;
65 while (*p && toupperW( p[1] ) != toupperW( q[1] )) p++;
66 if (!*p) return TRUE;
68 if (toupperW( *p++ ) != toupperW( *q++ )) return FALSE;
70 return TRUE;
73 static HRESULT eval_strcmp( UINT op, const WCHAR *lstr, const WCHAR *rstr, LONGLONG *val )
75 switch (op)
77 case OP_EQ:
78 *val = !strcmpW( lstr, rstr );
79 break;
80 case OP_GT:
81 *val = strcmpW( lstr, rstr ) > 0;
82 break;
83 case OP_LT:
84 *val = strcmpW( lstr, rstr ) < 0;
85 break;
86 case OP_LE:
87 *val = strcmpW( lstr, rstr ) <= 0;
88 break;
89 case OP_GE:
90 *val = strcmpW( lstr, rstr ) >= 0;
91 break;
92 case OP_NE:
93 *val = strcmpW( lstr, rstr );
94 break;
95 case OP_LIKE:
96 *val = eval_like( lstr, rstr );
97 break;
98 default:
99 ERR("unhandled operator %u\n", op);
100 return WBEM_E_INVALID_QUERY;
102 return S_OK;
105 static inline BOOL is_strcmp( const struct complex_expr *expr )
107 return ((expr->left->type == EXPR_PROPVAL && expr->right->type == EXPR_SVAL) ||
108 (expr->left->type == EXPR_SVAL && expr->right->type == EXPR_PROPVAL));
111 static HRESULT eval_cond( const struct table *, UINT, const struct expr *, LONGLONG * );
113 static HRESULT eval_binary( const struct table *table, UINT row, const struct complex_expr *expr,
114 LONGLONG *val )
116 HRESULT lret, rret;
117 LONGLONG lval, rval;
119 lret = eval_cond( table, row, expr->left, &lval );
120 rret = eval_cond( table, row, expr->right, &rval );
121 if (lret != S_OK || rret != S_OK) return WBEM_E_INVALID_QUERY;
123 if (is_strcmp( expr ))
125 const WCHAR *lstr = (const WCHAR *)(INT_PTR)lval;
126 const WCHAR *rstr = (const WCHAR *)(INT_PTR)rval;
128 return eval_strcmp( expr->op, lstr, rstr, val );
130 switch (expr->op)
132 case OP_EQ:
133 *val = (lval == rval);
134 break;
135 case OP_AND:
136 *val = (lval && rval);
137 break;
138 case OP_OR:
139 *val = (lval || rval);
140 break;
141 case OP_GT:
142 *val = (lval > rval);
143 break;
144 case OP_LT:
145 *val = (lval < rval);
146 break;
147 case OP_LE:
148 *val = (lval <= rval);
149 break;
150 case OP_GE:
151 *val = (lval >= rval);
152 break;
153 case OP_NE:
154 *val = (lval != rval);
155 break;
156 default:
157 ERR("unhandled operator %u\n", expr->op);
158 return WBEM_E_INVALID_QUERY;
160 return S_OK;
163 static HRESULT eval_unary( const struct table *table, UINT row, const struct complex_expr *expr,
164 LONGLONG *val )
167 HRESULT hr;
168 UINT column;
169 LONGLONG lval;
171 hr = get_column_index( table, expr->left->u.propval->name, &column );
172 if (hr != S_OK)
173 return hr;
175 hr = get_value( table, row, column, &lval );
176 if (hr != S_OK)
177 return hr;
179 switch (expr->op)
181 case OP_ISNULL:
182 *val = !lval;
183 break;
184 case OP_NOTNULL:
185 *val = lval;
186 break;
187 default:
188 ERR("unknown operator %u\n", expr->op);
189 return WBEM_E_INVALID_QUERY;
191 return S_OK;
194 static HRESULT eval_propval( const struct table *table, UINT row, const struct property *propval,
195 LONGLONG *val )
198 HRESULT hr;
199 UINT column;
201 hr = get_column_index( table, propval->name, &column );
202 if (hr != S_OK)
203 return hr;
205 return get_value( table, row, column, val );
208 static HRESULT eval_cond( const struct table *table, UINT row, const struct expr *cond,
209 LONGLONG *val )
211 if (!cond)
213 *val = 1;
214 return S_OK;
216 switch (cond->type)
218 case EXPR_COMPLEX:
219 return eval_binary( table, row, &cond->u.expr, val );
220 case EXPR_UNARY:
221 return eval_unary( table, row, &cond->u.expr, val );
222 case EXPR_PROPVAL:
223 return eval_propval( table, row, cond->u.propval, val );
224 case EXPR_SVAL:
225 *val = (LONGLONG)(INT_PTR)cond->u.sval;
226 return S_OK;
227 case EXPR_IVAL:
228 case EXPR_BVAL:
229 *val = cond->u.ival;
230 return S_OK;
231 default:
232 ERR("invalid expression type\n");
233 break;
235 return WBEM_E_INVALID_QUERY;
238 static HRESULT execute_view( struct view *view )
240 UINT i, j = 0, len;
242 if (!view->table || !view->table->num_rows) return S_OK;
244 len = min( view->table->num_rows, 16 );
245 if (!(view->result = heap_alloc( len * sizeof(UINT) ))) return E_OUTOFMEMORY;
247 for (i = 0; i < view->table->num_rows; i++)
249 HRESULT hr;
250 LONGLONG val = 0;
252 if (j >= len)
254 UINT *tmp;
255 len *= 2;
256 if (!(tmp = heap_realloc( view->result, len * sizeof(UINT) ))) return E_OUTOFMEMORY;
257 view->result = tmp;
259 if ((hr = eval_cond( view->table, i, view->cond, &val )) != S_OK) return hr;
260 if (val) view->result[j++] = i;
262 view->count = j;
263 return S_OK;
266 static struct query *create_query(void)
268 struct query *query;
270 if (!(query = heap_alloc( sizeof(*query) ))) return NULL;
271 list_init( &query->mem );
272 query->refs = 1;
273 return query;
276 static void free_query( struct query *query )
278 struct list *mem, *next;
280 destroy_view( query->view );
281 LIST_FOR_EACH_SAFE( mem, next, &query->mem )
283 heap_free( mem );
285 heap_free( query );
288 void addref_query( struct query *query )
290 InterlockedIncrement( &query->refs );
293 void release_query( struct query *query )
295 if (!InterlockedDecrement( &query->refs )) free_query( query );
298 HRESULT exec_query( const WCHAR *str, IEnumWbemClassObject **result )
300 HRESULT hr;
301 struct query *query;
303 *result = NULL;
304 if (!(query = create_query())) return E_OUTOFMEMORY;
305 hr = parse_query( str, &query->view, &query->mem );
306 if (hr != S_OK) goto done;
307 hr = execute_view( query->view );
308 if (hr != S_OK) goto done;
309 hr = EnumWbemClassObject_create( NULL, query, (void **)result );
311 done:
312 release_query( query );
313 return hr;
316 static BOOL is_selected_prop( const struct view *view, const WCHAR *name )
318 const struct property *prop = view->proplist;
320 if (!prop) return TRUE;
321 while (prop)
323 if (!strcmpiW( prop->name, name )) return TRUE;
324 prop = prop->next;
326 return FALSE;
329 static BOOL is_system_prop( const WCHAR *name )
331 return (name[0] == '_' && name[1] == '_');
334 static BSTR build_servername( const struct view *view )
336 WCHAR server[MAX_COMPUTERNAME_LENGTH + 1], *p;
337 DWORD len = sizeof(server)/sizeof(server[0]);
339 if (view->proplist) return NULL;
341 if (!(GetComputerNameW( server, &len ))) return NULL;
342 for (p = server; *p; p++) *p = toupperW( *p );
343 return SysAllocString( server );
346 static BSTR build_classname( const struct view *view )
348 return SysAllocString( view->table->name );
351 static BSTR build_namespace( const struct view *view )
353 static const WCHAR cimv2W[] = {'R','O','O','T','\\','C','I','M','V','2',0};
355 if (view->proplist) return NULL;
356 return SysAllocString( cimv2W );
359 static BSTR build_proplist( const struct view *view, UINT index, UINT count, UINT *len )
361 static const WCHAR fmtW[] = {'%','s','=','%','s',0};
362 UINT i, j, offset, row = view->result[index];
363 BSTR *values, ret = NULL;
365 if (!(values = heap_alloc( count * sizeof(BSTR) ))) return NULL;
367 *len = j = 0;
368 for (i = 0; i < view->table->num_cols; i++)
370 if (view->table->columns[i].type & COL_FLAG_KEY)
372 const WCHAR *name = view->table->columns[i].name;
374 values[j] = get_value_bstr( view->table, row, i );
375 *len += strlenW( fmtW ) + strlenW( name ) + strlenW( values[j] );
376 j++;
379 if ((ret = SysAllocStringLen( NULL, *len )))
381 offset = j = 0;
382 for (i = 0; i < view->table->num_cols; i++)
384 if (view->table->columns[i].type & COL_FLAG_KEY)
386 const WCHAR *name = view->table->columns[i].name;
388 offset += sprintfW( ret + offset, fmtW, name, values[j] );
389 if (j < count - 1) ret[offset++] = ',';
390 j++;
394 for (i = 0; i < count; i++) SysFreeString( values[i] );
395 heap_free( values );
396 return ret;
399 static UINT count_key_columns( const struct view *view )
401 UINT i, num_keys = 0;
403 for (i = 0; i < view->table->num_cols; i++)
405 if (view->table->columns[i].type & COL_FLAG_KEY) num_keys++;
407 return num_keys;
410 static BSTR build_relpath( const struct view *view, UINT index, const WCHAR *name )
412 static const WCHAR fmtW[] = {'%','s','.','%','s',0};
413 BSTR class, proplist, ret = NULL;
414 UINT num_keys, len;
416 if (view->proplist) return NULL;
418 if (!(class = build_classname( view ))) return NULL;
419 if (!(num_keys = count_key_columns( view ))) return class;
420 if (!(proplist = build_proplist( view, index, num_keys, &len ))) goto done;
422 len += strlenW( fmtW ) + SysStringLen( class );
423 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
424 sprintfW( ret, fmtW, class, proplist );
426 done:
427 SysFreeString( class );
428 SysFreeString( proplist );
429 return ret;
432 static BSTR build_path( const struct view *view, UINT index, const WCHAR *name )
434 static const WCHAR fmtW[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
435 BSTR server, namespace = NULL, relpath = NULL, ret = NULL;
436 UINT len;
438 if (view->proplist) return NULL;
440 if (!(server = build_servername( view ))) return NULL;
441 if (!(namespace = build_namespace( view ))) goto done;
442 if (!(relpath = build_relpath( view, index, name ))) goto done;
444 len = strlenW( fmtW ) + SysStringLen( server ) + SysStringLen( namespace ) + SysStringLen( relpath );
445 if (!(ret = SysAllocStringLen( NULL, len ))) goto done;
446 sprintfW( ret, fmtW, server, namespace, relpath );
448 done:
449 SysFreeString( server );
450 SysFreeString( namespace );
451 SysFreeString( relpath );
452 return ret;
455 static inline BOOL is_method( const struct table *table, UINT column )
457 return table->columns[column].type & COL_FLAG_METHOD;
460 static UINT count_properties( const struct view *view )
462 UINT i, num_props = 0;
464 for (i = 0; i < view->table->num_cols; i++)
466 if (!is_method( view->table, i)) num_props++;
468 return num_props;
471 static UINT count_selected_properties( const struct view *view )
473 const struct property *prop = view->proplist;
474 UINT count;
476 if (!prop) return count_properties( view );
478 count = 1;
479 while ((prop = prop->next)) count++;
480 return count;
483 static HRESULT get_system_propval( const struct view *view, UINT index, const WCHAR *name,
484 VARIANT *ret, CIMTYPE *type, LONG *flavor )
486 static const WCHAR classW[] = {'_','_','C','L','A','S','S',0};
487 static const WCHAR genusW[] = {'_','_','G','E','N','U','S',0};
488 static const WCHAR pathW[] = {'_','_','P','A','T','H',0};
489 static const WCHAR namespaceW[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
490 static const WCHAR propcountW[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
491 static const WCHAR relpathW[] = {'_','_','R','E','L','P','A','T','H',0};
492 static const WCHAR serverW[] = {'_','_','S','E','R','V','E','R',0};
494 if (flavor) *flavor = WBEM_FLAVOR_ORIGIN_SYSTEM;
496 if (!strcmpiW( name, classW ))
498 V_VT( ret ) = VT_BSTR;
499 V_BSTR( ret ) = build_classname( view );
500 if (type) *type = CIM_STRING;
501 return S_OK;
503 if (!strcmpiW( name, genusW ))
505 V_VT( ret ) = VT_I4;
506 V_I4( ret ) = WBEM_GENUS_INSTANCE; /* FIXME */
507 if (type) *type = CIM_SINT32;
508 return S_OK;
510 else if (!strcmpiW( name, namespaceW ))
512 V_VT( ret ) = VT_BSTR;
513 V_BSTR( ret ) = build_namespace( view );
514 if (type) *type = CIM_STRING;
515 return S_OK;
517 else if (!strcmpiW( name, pathW ))
519 V_VT( ret ) = VT_BSTR;
520 V_BSTR( ret ) = build_path( view, index, name );
521 if (type) *type = CIM_STRING;
522 return S_OK;
524 if (!strcmpiW( name, propcountW ))
526 V_VT( ret ) = VT_I4;
527 V_I4( ret ) = count_selected_properties( view );
528 if (type) *type = CIM_SINT32;
529 return S_OK;
531 else if (!strcmpiW( name, relpathW ))
533 V_VT( ret ) = VT_BSTR;
534 V_BSTR( ret ) = build_relpath( view, index, name );
535 if (type) *type = CIM_STRING;
536 return S_OK;
538 else if (!strcmpiW( name, serverW ))
540 V_VT( ret ) = VT_BSTR;
541 V_BSTR( ret ) = build_servername( view );
542 if (type) *type = CIM_STRING;
543 return S_OK;
545 FIXME("system property %s not implemented\n", debugstr_w(name));
546 return WBEM_E_NOT_FOUND;
549 static void set_variant( VARTYPE vartype, LONGLONG val, BSTR val_bstr, VARIANT *ret )
551 switch (vartype)
553 case VT_BSTR:
554 V_VT( ret ) = VT_BSTR;
555 V_BSTR( ret ) = val_bstr;
556 return;
557 case VT_I2:
558 V_VT( ret ) = VT_I2;
559 V_I2( ret ) = val;
560 return;
561 case VT_UI2:
562 V_VT( ret ) = VT_UI2;
563 V_UI2( ret ) = val;
564 return;
565 case VT_I4:
566 V_VT( ret ) = VT_I4;
567 V_I4( ret ) = val;
568 return;
569 case VT_UI4:
570 V_VT( ret ) = VT_UI4;
571 V_UI4( ret ) = val;
572 return;
573 case VT_NULL:
574 V_VT( ret ) = VT_NULL;
575 return;
576 default:
577 ERR("unhandled variant type %u\n", vartype);
578 return;
582 HRESULT get_propval( const struct view *view, UINT index, const WCHAR *name, VARIANT *ret,
583 CIMTYPE *type, LONG *flavor )
585 HRESULT hr;
586 UINT column, row = view->result[index];
587 VARTYPE vartype;
588 BSTR val_bstr = NULL;
589 LONGLONG val;
591 if (is_system_prop( name )) return get_system_propval( view, index, name, ret, type, flavor );
592 if (!is_selected_prop( view, name )) return WBEM_E_NOT_FOUND;
594 hr = get_column_index( view->table, name, &column );
595 if (hr != S_OK || is_method( view->table, column )) return WBEM_E_NOT_FOUND;
597 vartype = view->table->columns[column].vartype;
599 hr = get_value( view->table, row, column, &val );
600 if (hr != S_OK) return hr;
602 switch (view->table->columns[column].type & COL_TYPE_MASK)
604 case CIM_STRING:
605 case CIM_DATETIME:
606 if (val)
608 vartype = VT_BSTR;
609 val_bstr = SysAllocString( (const WCHAR *)(INT_PTR)val );
611 else
612 vartype = VT_NULL;
613 break;
614 case CIM_SINT16:
615 if (!vartype) vartype = VT_I2;
616 break;
617 case CIM_UINT16:
618 if (!vartype) vartype = VT_UI2;
619 break;
620 case CIM_SINT32:
621 if (!vartype) vartype = VT_I4;
622 break;
623 case CIM_UINT32:
624 if (!vartype) vartype = VT_UI4;
625 break;
626 case CIM_SINT64:
627 vartype = VT_BSTR;
628 val_bstr = get_value_bstr( view->table, row, column );
629 break;
630 case CIM_UINT64:
631 vartype = VT_BSTR;
632 val_bstr = get_value_bstr( view->table, row, column );
633 break;
634 default:
635 ERR("unhandled column type %u\n", view->table->columns[column].type);
636 return WBEM_E_FAILED;
638 set_variant( vartype, val, val_bstr, ret );
639 if (type) *type = view->table->columns[column].type & COL_TYPE_MASK;
640 if (flavor) *flavor = 0;
641 return S_OK;
644 HRESULT get_properties( const struct view *view, SAFEARRAY **props )
646 SAFEARRAY *sa;
647 BSTR str;
648 LONG i;
649 UINT num_props = count_properties( view );
651 if (!(sa = SafeArrayCreateVector( VT_BSTR, 0, num_props ))) return E_OUTOFMEMORY;
653 for (i = 0; i < view->table->num_cols; i++)
655 if (is_method( view->table, i )) continue;
657 str = SysAllocString( view->table->columns[i].name );
658 if (!str || SafeArrayPutElement( sa, &i, str ) != S_OK)
660 SysFreeString( str );
661 SafeArrayDestroy( sa );
662 return E_OUTOFMEMORY;
665 *props = sa;
666 return S_OK;