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
= get_table( class );
48 void destroy_view( struct view
*view
)
50 free_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
)
78 *val
= !strcmpW( lstr
, rstr
);
81 *val
= strcmpW( lstr
, rstr
) > 0;
84 *val
= strcmpW( lstr
, rstr
) < 0;
87 *val
= strcmpW( lstr
, rstr
) <= 0;
90 *val
= strcmpW( lstr
, rstr
) >= 0;
93 *val
= strcmpW( lstr
, rstr
);
96 *val
= eval_like( lstr
, rstr
);
99 ERR("unhandled operator %u\n", op
);
100 return WBEM_E_INVALID_QUERY
;
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
,
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
);
133 *val
= (lval
== rval
);
136 *val
= (lval
&& rval
);
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 ERR("unhandled operator %u\n", expr
->op
);
158 return WBEM_E_INVALID_QUERY
;
163 static HRESULT
eval_unary( const struct table
*table
, UINT row
, const struct complex_expr
*expr
,
171 hr
= get_column_index( table
, expr
->left
->u
.propval
->name
, &column
);
175 hr
= get_value( table
, row
, column
, &lval
);
188 ERR("unknown operator %u\n", expr
->op
);
189 return WBEM_E_INVALID_QUERY
;
194 static HRESULT
eval_propval( const struct table
*table
, UINT row
, const struct property
*propval
,
201 hr
= get_column_index( table
, propval
->name
, &column
);
205 return get_value( table
, row
, column
, val
);
208 static HRESULT
eval_cond( const struct table
*table
, UINT row
, const struct expr
*cond
,
219 return eval_binary( table
, row
, &cond
->u
.expr
, val
);
221 return eval_unary( table
, row
, &cond
->u
.expr
, val
);
223 return eval_propval( table
, row
, cond
->u
.propval
, val
);
225 *val
= (LONGLONG
)(INT_PTR
)cond
->u
.sval
;
232 ERR("invalid expression type\n");
235 return WBEM_E_INVALID_QUERY
;
238 static HRESULT
execute_view( struct view
*view
)
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
++)
256 if (!(tmp
= heap_realloc( view
->result
, len
* sizeof(UINT
) ))) return E_OUTOFMEMORY
;
259 if ((hr
= eval_cond( view
->table
, i
, view
->cond
, &val
)) != S_OK
) return hr
;
260 if (val
) view
->result
[j
++] = i
;
266 static struct query
*create_query(void)
270 if (!(query
= heap_alloc( sizeof(*query
) ))) return NULL
;
271 list_init( &query
->mem
);
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
)
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
)
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
);
312 release_query( query
);
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
;
323 if (!strcmpiW( prop
->name
, name
)) return TRUE
;
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
;
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
] );
379 if ((ret
= SysAllocStringLen( NULL
, *len
)))
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
++] = ',';
394 for (i
= 0; i
< count
; i
++) SysFreeString( values
[i
] );
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
++;
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
;
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
);
427 SysFreeString( class );
428 SysFreeString( proplist
);
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
;
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
);
449 SysFreeString( server
);
450 SysFreeString( namespace );
451 SysFreeString( relpath
);
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
++;
471 static UINT
count_selected_properties( const struct view
*view
)
473 const struct property
*prop
= view
->proplist
;
476 if (!prop
) return count_properties( view
);
479 while ((prop
= prop
->next
)) 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
;
503 if (!strcmpiW( name
, genusW
))
506 V_I4( ret
) = WBEM_GENUS_INSTANCE
; /* FIXME */
507 if (type
) *type
= CIM_SINT32
;
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
;
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
;
524 if (!strcmpiW( name
, propcountW
))
527 V_I4( ret
) = count_selected_properties( view
);
528 if (type
) *type
= CIM_SINT32
;
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
;
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
;
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
)
554 V_VT( ret
) = VT_BSTR
;
555 V_BSTR( ret
) = val_bstr
;
562 V_VT( ret
) = VT_UI2
;
570 V_VT( ret
) = VT_UI4
;
574 V_VT( ret
) = VT_NULL
;
577 ERR("unhandled variant type %u\n", vartype
);
582 HRESULT
get_propval( const struct view
*view
, UINT index
, const WCHAR
*name
, VARIANT
*ret
,
583 CIMTYPE
*type
, LONG
*flavor
)
586 UINT column
, row
= view
->result
[index
];
588 BSTR val_bstr
= NULL
;
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
)
609 val_bstr
= SysAllocString( (const WCHAR
*)(INT_PTR
)val
);
615 if (!vartype
) vartype
= VT_I2
;
618 if (!vartype
) vartype
= VT_UI2
;
621 if (!vartype
) vartype
= VT_I4
;
624 if (!vartype
) vartype
= VT_UI4
;
628 val_bstr
= get_value_bstr( view
->table
, row
, column
);
632 val_bstr
= get_value_bstr( view
->table
, row
, column
);
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;
644 HRESULT
get_properties( const struct view
*view
, SAFEARRAY
**props
)
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
;