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 WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
34 #include "wine/debug.h"
35 #include "wbemprox_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox
);
39 HRESULT
create_view( const struct property
*proplist
, const WCHAR
*class,
40 const struct expr
*cond
, struct view
**ret
)
42 struct view
*view
= heap_alloc( sizeof(struct view
) );
44 if (!view
) return E_OUTOFMEMORY
;
45 view
->proplist
= proplist
;
46 view
->table
= grab_table( class );
54 void destroy_view( struct view
*view
)
57 if (view
->table
) release_table( view
->table
);
58 heap_free( view
->result
);
62 static BOOL
eval_like( const WCHAR
*lstr
, const WCHAR
*rstr
)
64 const WCHAR
*p
= lstr
, *q
= rstr
;
70 while (*q
== '%') q
++;
72 while (*p
&& toupperW( p
[1] ) != toupperW( q
[1] )) p
++;
75 if (toupperW( *p
++ ) != toupperW( *q
++ )) return FALSE
;
80 static HRESULT
eval_strcmp( UINT op
, const WCHAR
*lstr
, const WCHAR
*rstr
, LONGLONG
*val
)
90 *val
= !strcmpW( lstr
, rstr
);
93 *val
= strcmpW( lstr
, rstr
) > 0;
96 *val
= strcmpW( lstr
, rstr
) < 0;
99 *val
= strcmpW( lstr
, rstr
) <= 0;
102 *val
= strcmpW( lstr
, rstr
) >= 0;
105 *val
= strcmpW( lstr
, rstr
);
108 *val
= eval_like( lstr
, rstr
);
111 ERR("unhandled operator %u\n", op
);
112 return WBEM_E_INVALID_QUERY
;
117 static inline BOOL
is_strcmp( const struct complex_expr
*expr
)
119 return ((expr
->left
->type
== EXPR_PROPVAL
&& expr
->right
->type
== EXPR_SVAL
) ||
120 (expr
->left
->type
== EXPR_SVAL
&& expr
->right
->type
== EXPR_PROPVAL
));
123 static HRESULT
eval_cond( const struct table
*, UINT
, const struct expr
*, LONGLONG
* );
125 static HRESULT
eval_binary( const struct table
*table
, UINT row
, const struct complex_expr
*expr
,
131 lret
= eval_cond( table
, row
, expr
->left
, &lval
);
132 rret
= eval_cond( table
, row
, expr
->right
, &rval
);
133 if (lret
!= S_OK
|| rret
!= S_OK
) return WBEM_E_INVALID_QUERY
;
135 if (is_strcmp( expr
))
137 const WCHAR
*lstr
= (const WCHAR
*)(INT_PTR
)lval
;
138 const WCHAR
*rstr
= (const WCHAR
*)(INT_PTR
)rval
;
140 return eval_strcmp( expr
->op
, lstr
, rstr
, val
);
145 *val
= (lval
== rval
);
148 *val
= (lval
&& rval
);
151 *val
= (lval
|| rval
);
154 *val
= (lval
> rval
);
157 *val
= (lval
< rval
);
160 *val
= (lval
<= rval
);
163 *val
= (lval
>= rval
);
166 *val
= (lval
!= rval
);
169 ERR("unhandled operator %u\n", expr
->op
);
170 return WBEM_E_INVALID_QUERY
;
175 static HRESULT
eval_unary( const struct table
*table
, UINT row
, const struct complex_expr
*expr
,
183 hr
= get_column_index( table
, expr
->left
->u
.propval
->name
, &column
);
187 hr
= get_value( table
, row
, column
, &lval
);
200 ERR("unknown operator %u\n", expr
->op
);
201 return WBEM_E_INVALID_QUERY
;
206 static HRESULT
eval_propval( const struct table
*table
, UINT row
, const struct property
*propval
,
213 hr
= get_column_index( table
, propval
->name
, &column
);
217 return get_value( table
, row
, column
, val
);
220 static HRESULT
eval_cond( const struct table
*table
, UINT row
, const struct expr
*cond
,
231 return eval_binary( table
, row
, &cond
->u
.expr
, val
);
233 return eval_unary( table
, row
, &cond
->u
.expr
, val
);
235 return eval_propval( table
, row
, cond
->u
.propval
, val
);
237 *val
= (INT_PTR
)cond
->u
.sval
;
244 ERR("invalid expression type\n");
247 return WBEM_E_INVALID_QUERY
;
250 static HRESULT
execute_view( struct view
*view
)
254 if (!view
->table
|| !view
->table
->num_rows
) return S_OK
;
256 len
= min( view
->table
->num_rows
, 16 );
257 if (!(view
->result
= heap_alloc( len
* sizeof(UINT
) ))) return E_OUTOFMEMORY
;
259 for (i
= 0; i
< view
->table
->num_rows
; i
++)
268 if (!(tmp
= heap_realloc( view
->result
, len
* sizeof(UINT
) ))) return E_OUTOFMEMORY
;
271 if ((hr
= eval_cond( view
->table
, i
, view
->cond
, &val
)) != S_OK
) return hr
;
272 if (val
) view
->result
[j
++] = i
;
278 static struct query
*create_query(void)
282 if (!(query
= heap_alloc( sizeof(*query
) ))) return NULL
;
283 list_init( &query
->mem
);
288 static void free_query( struct query
*query
)
290 struct list
*mem
, *next
;
292 destroy_view( query
->view
);
293 LIST_FOR_EACH_SAFE( mem
, next
, &query
->mem
)
300 struct query
*addref_query( struct query
*query
)
302 InterlockedIncrement( &query
->refs
);
306 void release_query( struct query
*query
)
308 if (!InterlockedDecrement( &query
->refs
)) free_query( query
);
311 HRESULT
exec_query( const WCHAR
*str
, IEnumWbemClassObject
**result
)
317 if (!(query
= create_query())) return E_OUTOFMEMORY
;
318 hr
= parse_query( str
, &query
->view
, &query
->mem
);
319 if (hr
!= S_OK
) goto done
;
320 hr
= execute_view( query
->view
);
321 if (hr
!= S_OK
) goto done
;
322 hr
= EnumWbemClassObject_create( NULL
, query
, (void **)result
);
325 release_query( query
);
329 static BOOL
is_selected_prop( const struct view
*view
, const WCHAR
*name
)
331 const struct property
*prop
= view
->proplist
;
333 if (!prop
) return TRUE
;
336 if (!strcmpiW( prop
->name
, name
)) return TRUE
;
342 static BOOL
is_system_prop( const WCHAR
*name
)
344 return (name
[0] == '_' && name
[1] == '_');
347 static BSTR
build_servername( const struct view
*view
)
349 WCHAR server
[MAX_COMPUTERNAME_LENGTH
+ 1], *p
;
350 DWORD len
= sizeof(server
)/sizeof(server
[0]);
352 if (view
->proplist
) return NULL
;
354 if (!(GetComputerNameW( server
, &len
))) return NULL
;
355 for (p
= server
; *p
; p
++) *p
= toupperW( *p
);
356 return SysAllocString( server
);
359 static BSTR
build_classname( const struct view
*view
)
361 return SysAllocString( view
->table
->name
);
364 static BSTR
build_namespace( const struct view
*view
)
366 static const WCHAR cimv2W
[] = {'R','O','O','T','\\','C','I','M','V','2',0};
368 if (view
->proplist
) return NULL
;
369 return SysAllocString( cimv2W
);
372 static BSTR
build_proplist( const struct view
*view
, UINT index
, UINT count
, UINT
*len
)
374 static const WCHAR fmtW
[] = {'%','s','=','%','s',0};
375 UINT i
, j
, offset
, row
= view
->result
[index
];
376 BSTR
*values
, ret
= NULL
;
378 if (!(values
= heap_alloc( count
* sizeof(BSTR
) ))) return NULL
;
381 for (i
= 0; i
< view
->table
->num_cols
; i
++)
383 if (view
->table
->columns
[i
].type
& COL_FLAG_KEY
)
385 const WCHAR
*name
= view
->table
->columns
[i
].name
;
387 values
[j
] = get_value_bstr( view
->table
, row
, i
);
388 *len
+= strlenW( fmtW
) + strlenW( name
) + strlenW( values
[j
] );
392 if ((ret
= SysAllocStringLen( NULL
, *len
)))
395 for (i
= 0; i
< view
->table
->num_cols
; i
++)
397 if (view
->table
->columns
[i
].type
& COL_FLAG_KEY
)
399 const WCHAR
*name
= view
->table
->columns
[i
].name
;
401 offset
+= sprintfW( ret
+ offset
, fmtW
, name
, values
[j
] );
402 if (j
< count
- 1) ret
[offset
++] = ',';
407 for (i
= 0; i
< count
; i
++) SysFreeString( values
[i
] );
412 static UINT
count_key_columns( const struct view
*view
)
414 UINT i
, num_keys
= 0;
416 for (i
= 0; i
< view
->table
->num_cols
; i
++)
418 if (view
->table
->columns
[i
].type
& COL_FLAG_KEY
) num_keys
++;
423 static BSTR
build_relpath( const struct view
*view
, UINT index
, const WCHAR
*name
)
425 static const WCHAR fmtW
[] = {'%','s','.','%','s',0};
426 BSTR
class, proplist
, ret
= NULL
;
429 if (view
->proplist
) return NULL
;
431 if (!(class = build_classname( view
))) return NULL
;
432 if (!(num_keys
= count_key_columns( view
))) return class;
433 if (!(proplist
= build_proplist( view
, index
, num_keys
, &len
))) goto done
;
435 len
+= strlenW( fmtW
) + SysStringLen( class );
436 if (!(ret
= SysAllocStringLen( NULL
, len
))) goto done
;
437 sprintfW( ret
, fmtW
, class, proplist
);
440 SysFreeString( class );
441 SysFreeString( proplist
);
445 static BSTR
build_path( const struct view
*view
, UINT index
, const WCHAR
*name
)
447 static const WCHAR fmtW
[] = {'\\','\\','%','s','\\','%','s',':','%','s',0};
448 BSTR server
, namespace = NULL
, relpath
= NULL
, ret
= NULL
;
451 if (view
->proplist
) return NULL
;
453 if (!(server
= build_servername( view
))) return NULL
;
454 if (!(namespace = build_namespace( view
))) goto done
;
455 if (!(relpath
= build_relpath( view
, index
, name
))) goto done
;
457 len
= strlenW( fmtW
) + SysStringLen( server
) + SysStringLen( namespace ) + SysStringLen( relpath
);
458 if (!(ret
= SysAllocStringLen( NULL
, len
))) goto done
;
459 sprintfW( ret
, fmtW
, server
, namespace, relpath
);
462 SysFreeString( server
);
463 SysFreeString( namespace );
464 SysFreeString( relpath
);
468 static inline BOOL
is_method( const struct table
*table
, UINT column
)
470 return table
->columns
[column
].type
& COL_FLAG_METHOD
;
473 static UINT
count_properties( const struct view
*view
)
475 UINT i
, num_props
= 0;
477 for (i
= 0; i
< view
->table
->num_cols
; i
++)
479 if (!is_method( view
->table
, i
)) num_props
++;
484 static UINT
count_selected_properties( const struct view
*view
)
486 const struct property
*prop
= view
->proplist
;
489 if (!prop
) return count_properties( view
);
492 while ((prop
= prop
->next
)) count
++;
496 static HRESULT
get_system_propval( const struct view
*view
, UINT index
, const WCHAR
*name
,
497 VARIANT
*ret
, CIMTYPE
*type
, LONG
*flavor
)
499 static const WCHAR classW
[] = {'_','_','C','L','A','S','S',0};
500 static const WCHAR genusW
[] = {'_','_','G','E','N','U','S',0};
501 static const WCHAR pathW
[] = {'_','_','P','A','T','H',0};
502 static const WCHAR namespaceW
[] = {'_','_','N','A','M','E','S','P','A','C','E',0};
503 static const WCHAR propcountW
[] = {'_','_','P','R','O','P','E','R','T','Y','_','C','O','U','N','T',0};
504 static const WCHAR relpathW
[] = {'_','_','R','E','L','P','A','T','H',0};
505 static const WCHAR serverW
[] = {'_','_','S','E','R','V','E','R',0};
507 if (flavor
) *flavor
= WBEM_FLAVOR_ORIGIN_SYSTEM
;
509 if (!strcmpiW( name
, classW
))
511 V_VT( ret
) = VT_BSTR
;
512 V_BSTR( ret
) = build_classname( view
);
513 if (type
) *type
= CIM_STRING
;
516 if (!strcmpiW( name
, genusW
))
519 V_I4( ret
) = WBEM_GENUS_INSTANCE
; /* FIXME */
520 if (type
) *type
= CIM_SINT32
;
523 else if (!strcmpiW( name
, namespaceW
))
525 V_VT( ret
) = VT_BSTR
;
526 V_BSTR( ret
) = build_namespace( view
);
527 if (type
) *type
= CIM_STRING
;
530 else if (!strcmpiW( name
, pathW
))
532 V_VT( ret
) = VT_BSTR
;
533 V_BSTR( ret
) = build_path( view
, index
, name
);
534 if (type
) *type
= CIM_STRING
;
537 if (!strcmpiW( name
, propcountW
))
540 V_I4( ret
) = count_selected_properties( view
);
541 if (type
) *type
= CIM_SINT32
;
544 else if (!strcmpiW( name
, relpathW
))
546 V_VT( ret
) = VT_BSTR
;
547 V_BSTR( ret
) = build_relpath( view
, index
, name
);
548 if (type
) *type
= CIM_STRING
;
551 else if (!strcmpiW( name
, serverW
))
553 V_VT( ret
) = VT_BSTR
;
554 V_BSTR( ret
) = build_servername( view
);
555 if (type
) *type
= CIM_STRING
;
558 FIXME("system property %s not implemented\n", debugstr_w(name
));
559 return WBEM_E_NOT_FOUND
;
562 VARTYPE
to_vartype( CIMTYPE type
)
566 case CIM_BOOLEAN
: return VT_BOOL
;
568 case CIM_DATETIME
: return VT_BSTR
;
569 case CIM_SINT16
: return VT_I2
;
570 case CIM_UINT16
: return VT_UI2
;
571 case CIM_SINT32
: return VT_I4
;
572 case CIM_UINT32
: return VT_UI4
;
573 case CIM_SINT64
: return VT_I8
;
574 case CIM_UINT64
: return VT_UI8
;
576 ERR("unhandled type %u\n", type
);
582 SAFEARRAY
*to_safearray( const struct array
*array
, CIMTYPE type
)
585 UINT size
= get_type_size( type
);
586 VARTYPE vartype
= to_vartype( type
);
589 if (!(ret
= SafeArrayCreateVector( vartype
, 0, array
->count
))) return NULL
;
591 for (i
= 0; i
< array
->count
; i
++)
593 void *ptr
= (char *)array
->ptr
+ i
* size
;
594 if (vartype
== VT_BSTR
)
596 BSTR str
= SysAllocString( *(const WCHAR
**)ptr
);
597 if (!str
|| SafeArrayPutElement( ret
, &i
, str
) != S_OK
)
599 SysFreeString( str
);
600 SafeArrayDestroy( ret
);
604 else if (SafeArrayPutElement( ret
, &i
, ptr
) != S_OK
)
606 SafeArrayDestroy( ret
);
613 void set_variant( VARTYPE type
, LONGLONG val
, void *val_ptr
, VARIANT
*ret
)
618 V_ARRAY( ret
) = val_ptr
;
627 V_BSTR( ret
) = val_ptr
;
644 ERR("unhandled variant type %u\n", type
);
650 HRESULT
get_propval( const struct view
*view
, UINT index
, const WCHAR
*name
, VARIANT
*ret
,
651 CIMTYPE
*type
, LONG
*flavor
)
656 void *val_ptr
= NULL
;
659 if (is_system_prop( name
)) return get_system_propval( view
, index
, name
, ret
, type
, flavor
);
660 if (!view
->count
|| !is_selected_prop( view
, name
)) return WBEM_E_NOT_FOUND
;
662 hr
= get_column_index( view
->table
, name
, &column
);
663 if (hr
!= S_OK
|| is_method( view
->table
, column
)) return WBEM_E_NOT_FOUND
;
665 row
= view
->result
[index
];
666 hr
= get_value( view
->table
, row
, column
, &val
);
667 if (hr
!= S_OK
) return hr
;
669 vartype
= view
->table
->columns
[column
].vartype
;
670 if (view
->table
->columns
[column
].type
& CIM_FLAG_ARRAY
)
672 CIMTYPE basetype
= view
->table
->columns
[column
].type
& CIM_TYPE_MASK
;
674 val_ptr
= to_safearray( (const struct array
*)(INT_PTR
)val
, basetype
);
675 if (!vartype
) vartype
= to_vartype( basetype
) | VT_ARRAY
;
678 switch (view
->table
->columns
[column
].type
& COL_TYPE_MASK
)
681 if (!vartype
) vartype
= VT_BOOL
;
688 val_ptr
= SysAllocString( (const WCHAR
*)(INT_PTR
)val
);
694 if (!vartype
) vartype
= VT_I2
;
697 if (!vartype
) vartype
= VT_UI2
;
700 if (!vartype
) vartype
= VT_I4
;
703 if (!vartype
) vartype
= VT_UI4
;
707 val_ptr
= get_value_bstr( view
->table
, row
, column
);
711 val_ptr
= get_value_bstr( view
->table
, row
, column
);
714 ERR("unhandled column type %u\n", view
->table
->columns
[column
].type
);
715 return WBEM_E_FAILED
;
719 set_variant( vartype
, val
, val_ptr
, ret
);
720 if (type
) *type
= view
->table
->columns
[column
].type
& COL_TYPE_MASK
;
721 if (flavor
) *flavor
= 0;
725 static CIMTYPE
to_cimtype( VARTYPE type
)
729 case VT_BOOL
: return CIM_BOOLEAN
;
730 case VT_BSTR
: return CIM_STRING
;
731 case VT_I2
: return CIM_SINT16
;
732 case VT_UI2
: return CIM_UINT16
;
733 case VT_I4
: return CIM_SINT32
;
734 case VT_UI4
: return CIM_UINT32
;
735 case VT_I8
: return CIM_SINT64
;
736 case VT_UI8
: return CIM_UINT64
;
738 ERR("unhandled type %u\n", type
);
744 static struct array
*to_array( VARIANT
*var
, CIMTYPE
*type
)
752 if (SafeArrayGetVartype( V_ARRAY( var
), &vartype
) != S_OK
) return NULL
;
753 if (!(basetype
= to_cimtype( vartype
))) return NULL
;
754 if (SafeArrayGetUBound( V_ARRAY( var
), 1, &bound
) != S_OK
) return NULL
;
755 if (!(ret
= heap_alloc( sizeof(struct array
) ))) return NULL
;
757 ret
->count
= bound
+ 1;
758 size
= get_type_size( basetype
);
759 if (!(ret
->ptr
= heap_alloc_zero( ret
->count
* size
)))
764 for (i
= 0; i
< ret
->count
; i
++)
766 void *ptr
= (char *)ret
->ptr
+ i
* size
;
767 if (vartype
== VT_BSTR
)
770 if (SafeArrayGetElement( V_ARRAY( var
), &i
, &str
) != S_OK
)
772 destroy_array( ret
, basetype
);
775 *(WCHAR
**)ptr
= heap_strdupW( str
);
776 SysFreeString( str
);
779 destroy_array( ret
, basetype
);
783 else if (SafeArrayGetElement( V_ARRAY( var
), &i
, ptr
) != S_OK
)
785 destroy_array( ret
, basetype
);
789 *type
= basetype
| CIM_FLAG_ARRAY
;
793 HRESULT
to_longlong( VARIANT
*var
, LONGLONG
*val
, CIMTYPE
*type
)
800 if (V_VT( var
) & VT_ARRAY
)
802 *val
= (INT_PTR
)to_array( var
, type
);
803 if (!*val
) return E_OUTOFMEMORY
;
809 *val
= V_BOOL( var
);
813 *val
= (INT_PTR
)heap_strdupW( V_BSTR( var
) );
814 if (!*val
) return E_OUTOFMEMORY
;
837 ERR("unhandled type %u\n", V_VT( var
));
838 return WBEM_E_FAILED
;
843 HRESULT
put_propval( const struct view
*view
, UINT index
, const WCHAR
*name
, VARIANT
*var
, CIMTYPE type
)
846 UINT column
, row
= view
->result
[index
];
849 hr
= get_column_index( view
->table
, name
, &column
);
852 FIXME("no support for creating new properties\n");
853 return WBEM_E_FAILED
;
855 if (is_method( view
->table
, column
) || !(view
->table
->columns
[column
].type
& COL_FLAG_DYNAMIC
))
856 return WBEM_E_FAILED
;
858 hr
= to_longlong( var
, &val
, &type
);
859 if (hr
!= S_OK
) return hr
;
861 return set_value( view
->table
, row
, column
, val
, type
);
864 HRESULT
get_properties( const struct view
*view
, SAFEARRAY
**props
)
869 UINT num_props
= count_properties( view
);
871 if (!(sa
= SafeArrayCreateVector( VT_BSTR
, 0, num_props
))) return E_OUTOFMEMORY
;
873 for (i
= 0; i
< view
->table
->num_cols
; i
++)
875 if (is_method( view
->table
, i
)) continue;
877 str
= SysAllocString( view
->table
->columns
[i
].name
);
878 if (!str
|| SafeArrayPutElement( sa
, &i
, str
) != S_OK
)
880 SysFreeString( str
);
881 SafeArrayDestroy( sa
);
882 return E_OUTOFMEMORY
;