2012-07-13 11:35:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Hans Leidekker for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define COBJMACROS
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wbemcli.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wbemprox_private.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
|
|
|
|
|
|
|
|
HRESULT get_column_index( const struct table *table, const WCHAR *name, UINT *column )
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
for (i = 0; i < table->num_cols; i++)
|
|
|
|
{
|
|
|
|
if (!strcmpiW( table->columns[i].name, name ))
|
|
|
|
{
|
|
|
|
*column = i;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WBEM_E_INVALID_QUERY;
|
|
|
|
}
|
|
|
|
|
2012-07-25 13:13:17 +02:00
|
|
|
UINT get_type_size( CIMTYPE type )
|
2012-07-13 11:35:39 +02:00
|
|
|
{
|
2012-07-25 13:13:17 +02:00
|
|
|
if (type & CIM_FLAG_ARRAY) return sizeof(void *);
|
2012-07-13 11:35:39 +02:00
|
|
|
|
2012-07-25 13:13:17 +02:00
|
|
|
switch (type)
|
2012-07-13 11:35:39 +02:00
|
|
|
{
|
2012-07-30 15:04:03 +02:00
|
|
|
case CIM_BOOLEAN:
|
|
|
|
return sizeof(int);
|
2014-08-04 23:29:16 +02:00
|
|
|
case CIM_SINT8:
|
|
|
|
case CIM_UINT8:
|
|
|
|
return sizeof(INT8);
|
2012-07-13 11:35:39 +02:00
|
|
|
case CIM_SINT16:
|
|
|
|
case CIM_UINT16:
|
|
|
|
return sizeof(INT16);
|
|
|
|
case CIM_SINT32:
|
|
|
|
case CIM_UINT32:
|
|
|
|
return sizeof(INT32);
|
|
|
|
case CIM_SINT64:
|
|
|
|
case CIM_UINT64:
|
|
|
|
return sizeof(INT64);
|
|
|
|
case CIM_DATETIME:
|
|
|
|
case CIM_STRING:
|
|
|
|
return sizeof(WCHAR *);
|
|
|
|
default:
|
2012-07-25 13:13:17 +02:00
|
|
|
ERR("unhandled type %u\n", type);
|
2012-07-13 11:35:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-07-25 13:13:17 +02:00
|
|
|
return sizeof(LONGLONG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT get_column_size( const struct table *table, UINT column )
|
|
|
|
{
|
|
|
|
return get_type_size( table->columns[column].type & COL_TYPE_MASK );
|
2012-07-13 11:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static UINT get_column_offset( const struct table *table, UINT column )
|
|
|
|
{
|
|
|
|
UINT i, offset = 0;
|
|
|
|
for (i = 0; i < column; i++) offset += get_column_size( table, i );
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT get_row_size( const struct table *table )
|
|
|
|
{
|
|
|
|
return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT get_value( const struct table *table, UINT row, UINT column, LONGLONG *val )
|
|
|
|
{
|
|
|
|
UINT col_offset, row_size;
|
|
|
|
const BYTE *ptr;
|
|
|
|
|
|
|
|
col_offset = get_column_offset( table, column );
|
|
|
|
row_size = get_row_size( table );
|
|
|
|
ptr = table->data + row * row_size + col_offset;
|
|
|
|
|
|
|
|
if (table->columns[column].type & CIM_FLAG_ARRAY)
|
|
|
|
{
|
2012-10-10 12:03:10 +02:00
|
|
|
*val = (INT_PTR)*(const void **)ptr;
|
2012-07-13 11:35:39 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
switch (table->columns[column].type & COL_TYPE_MASK)
|
|
|
|
{
|
2012-07-30 15:04:03 +02:00
|
|
|
case CIM_BOOLEAN:
|
|
|
|
*val = *(const int *)ptr;
|
|
|
|
break;
|
2012-07-13 11:35:39 +02:00
|
|
|
case CIM_DATETIME:
|
|
|
|
case CIM_STRING:
|
2012-10-10 12:03:10 +02:00
|
|
|
*val = (INT_PTR)*(const WCHAR **)ptr;
|
2012-07-13 11:35:39 +02:00
|
|
|
break;
|
2014-08-04 23:29:16 +02:00
|
|
|
case CIM_SINT8:
|
|
|
|
*val = *(const INT8 *)ptr;
|
|
|
|
break;
|
|
|
|
case CIM_UINT8:
|
|
|
|
*val = *(const UINT8 *)ptr;
|
|
|
|
break;
|
2012-07-13 11:35:39 +02:00
|
|
|
case CIM_SINT16:
|
|
|
|
*val = *(const INT16 *)ptr;
|
|
|
|
break;
|
|
|
|
case CIM_UINT16:
|
|
|
|
*val = *(const UINT16 *)ptr;
|
|
|
|
break;
|
|
|
|
case CIM_SINT32:
|
|
|
|
*val = *(const INT32 *)ptr;
|
|
|
|
break;
|
|
|
|
case CIM_UINT32:
|
|
|
|
*val = *(const UINT32 *)ptr;
|
|
|
|
break;
|
|
|
|
case CIM_SINT64:
|
|
|
|
*val = *(const INT64 *)ptr;
|
|
|
|
break;
|
|
|
|
case CIM_UINT64:
|
|
|
|
*val = *(const UINT64 *)ptr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
|
|
|
|
*val = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
|
|
|
|
{
|
|
|
|
static const WCHAR fmt_signedW[] = {'%','d',0};
|
|
|
|
static const WCHAR fmt_unsignedW[] = {'%','u',0};
|
|
|
|
static const WCHAR fmt_signed64W[] = {'%','I','6','4','d',0};
|
|
|
|
static const WCHAR fmt_unsigned64W[] = {'%','I','6','4','u',0};
|
|
|
|
static const WCHAR fmt_strW[] = {'\"','%','s','\"',0};
|
2012-09-04 13:49:24 +02:00
|
|
|
static const WCHAR trueW[] = {'T','R','U','E',0};
|
|
|
|
static const WCHAR falseW[] = {'F','A','L','S','E',0};
|
2012-07-13 11:35:39 +02:00
|
|
|
LONGLONG val;
|
|
|
|
BSTR ret;
|
|
|
|
WCHAR number[22];
|
|
|
|
UINT len;
|
|
|
|
|
|
|
|
if (table->columns[column].type & CIM_FLAG_ARRAY)
|
|
|
|
{
|
|
|
|
FIXME("array to string conversion not handled\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (get_value( table, row, column, &val ) != S_OK) return NULL;
|
|
|
|
|
|
|
|
switch (table->columns[column].type & COL_TYPE_MASK)
|
|
|
|
{
|
2012-09-04 13:49:24 +02:00
|
|
|
case CIM_BOOLEAN:
|
|
|
|
if (val) return SysAllocString( trueW );
|
|
|
|
else return SysAllocString( falseW );
|
|
|
|
|
2012-07-13 11:35:39 +02:00
|
|
|
case CIM_DATETIME:
|
|
|
|
case CIM_STRING:
|
2012-11-02 15:34:27 +01:00
|
|
|
if (!val) return NULL;
|
2012-07-13 11:35:39 +02:00
|
|
|
len = strlenW( (const WCHAR *)(INT_PTR)val ) + 2;
|
|
|
|
if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
|
|
|
|
sprintfW( ret, fmt_strW, (const WCHAR *)(INT_PTR)val );
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
case CIM_SINT16:
|
|
|
|
case CIM_SINT32:
|
|
|
|
sprintfW( number, fmt_signedW, val );
|
|
|
|
return SysAllocString( number );
|
|
|
|
|
|
|
|
case CIM_UINT16:
|
|
|
|
case CIM_UINT32:
|
|
|
|
sprintfW( number, fmt_unsignedW, val );
|
|
|
|
return SysAllocString( number );
|
|
|
|
|
|
|
|
case CIM_SINT64:
|
|
|
|
wsprintfW( number, fmt_signed64W, val );
|
|
|
|
return SysAllocString( number );
|
|
|
|
|
|
|
|
case CIM_UINT64:
|
|
|
|
wsprintfW( number, fmt_unsigned64W, val );
|
|
|
|
return SysAllocString( number );
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-25 13:12:10 +02:00
|
|
|
HRESULT set_value( const struct table *table, UINT row, UINT column, LONGLONG val,
|
|
|
|
CIMTYPE type )
|
|
|
|
{
|
|
|
|
UINT col_offset, row_size;
|
|
|
|
BYTE *ptr;
|
|
|
|
|
|
|
|
if ((table->columns[column].type & COL_TYPE_MASK) != type) return WBEM_E_TYPE_MISMATCH;
|
|
|
|
|
|
|
|
col_offset = get_column_offset( table, column );
|
|
|
|
row_size = get_row_size( table );
|
|
|
|
ptr = table->data + row * row_size + col_offset;
|
|
|
|
|
|
|
|
switch (table->columns[column].type & COL_TYPE_MASK)
|
|
|
|
{
|
|
|
|
case CIM_DATETIME:
|
|
|
|
case CIM_STRING:
|
|
|
|
*(WCHAR **)ptr = (WCHAR *)(INT_PTR)val;
|
|
|
|
break;
|
2014-08-04 23:29:16 +02:00
|
|
|
case CIM_SINT8:
|
|
|
|
*(INT8 *)ptr = val;
|
|
|
|
break;
|
|
|
|
case CIM_UINT8:
|
|
|
|
*(UINT8 *)ptr = val;
|
|
|
|
break;
|
2012-07-25 13:12:10 +02:00
|
|
|
case CIM_SINT16:
|
|
|
|
*(INT16 *)ptr = val;
|
|
|
|
break;
|
|
|
|
case CIM_UINT16:
|
|
|
|
*(UINT16 *)ptr = val;
|
|
|
|
break;
|
|
|
|
case CIM_SINT32:
|
|
|
|
*(INT32 *)ptr = val;
|
|
|
|
break;
|
|
|
|
case CIM_UINT32:
|
|
|
|
*(UINT32 *)ptr = val;
|
|
|
|
break;
|
|
|
|
case CIM_SINT64:
|
|
|
|
*(INT64 *)ptr = val;
|
|
|
|
break;
|
|
|
|
case CIM_UINT64:
|
|
|
|
*(UINT64 *)ptr = val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FIXME("unhandled column type %u\n", type);
|
|
|
|
return WBEM_E_FAILED;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-28 13:37:21 +02:00
|
|
|
HRESULT get_method( const struct table *table, const WCHAR *name, class_method **func )
|
|
|
|
{
|
|
|
|
UINT i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < table->num_rows; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < table->num_cols; j++)
|
|
|
|
{
|
|
|
|
if (table->columns[j].type & COL_FLAG_METHOD && !strcmpW( table->columns[j].name, name ))
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
LONGLONG val;
|
|
|
|
|
|
|
|
if ((hr = get_value( table, i, j, &val )) != S_OK) return hr;
|
|
|
|
*func = (class_method *)(INT_PTR)val;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WBEM_E_INVALID_METHOD;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-22 10:10:16 +02:00
|
|
|
void free_row_values( const struct table *table, UINT row )
|
2012-07-13 11:35:39 +02:00
|
|
|
{
|
2013-05-22 10:10:16 +02:00
|
|
|
UINT i, type;
|
2012-07-13 11:35:39 +02:00
|
|
|
LONGLONG val;
|
|
|
|
|
2013-05-22 10:10:16 +02:00
|
|
|
for (i = 0; i < table->num_cols; i++)
|
2012-07-13 11:35:39 +02:00
|
|
|
{
|
2013-05-22 10:10:16 +02:00
|
|
|
if (!(table->columns[i].type & COL_FLAG_DYNAMIC)) continue;
|
2012-07-13 11:35:39 +02:00
|
|
|
|
2013-05-22 10:10:16 +02:00
|
|
|
type = table->columns[i].type & COL_TYPE_MASK;
|
2015-04-17 09:38:15 +02:00
|
|
|
if (type == CIM_STRING || type == CIM_DATETIME)
|
2013-05-22 10:10:16 +02:00
|
|
|
{
|
|
|
|
if (get_value( table, row, i, &val ) == S_OK) heap_free( (void *)(INT_PTR)val );
|
2012-07-13 11:35:39 +02:00
|
|
|
}
|
2015-04-17 09:38:15 +02:00
|
|
|
else if (type & CIM_FLAG_ARRAY)
|
|
|
|
{
|
|
|
|
if (get_value( table, row, i, &val ) == S_OK)
|
|
|
|
destroy_array( (void *)(INT_PTR)val, type & CIM_TYPE_MASK );
|
|
|
|
}
|
2012-07-13 11:35:39 +02:00
|
|
|
}
|
2013-05-22 10:10:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear_table( struct table *table )
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
|
|
|
|
if (!table->data) return;
|
|
|
|
|
|
|
|
for (i = 0; i < table->num_rows; i++) free_row_values( table, i );
|
2012-07-13 11:35:39 +02:00
|
|
|
if (table->fill)
|
|
|
|
{
|
2012-07-25 13:12:57 +02:00
|
|
|
table->num_rows = 0;
|
2013-06-04 13:28:21 +02:00
|
|
|
table->num_rows_allocated = 0;
|
2012-07-13 11:35:39 +02:00
|
|
|
heap_free( table->data );
|
|
|
|
table->data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_columns( struct column *columns, UINT num_cols )
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
|
2013-05-22 10:10:16 +02:00
|
|
|
for (i = 0; i < num_cols; i++) { heap_free( (WCHAR *)columns[i].name ); }
|
2012-07-13 11:35:39 +02:00
|
|
|
heap_free( columns );
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_table( struct table *table )
|
|
|
|
{
|
|
|
|
if (!table) return;
|
|
|
|
|
|
|
|
clear_table( table );
|
|
|
|
if (table->flags & TABLE_FLAG_DYNAMIC)
|
|
|
|
{
|
2012-10-04 15:36:33 +02:00
|
|
|
TRACE("destroying %p\n", table);
|
2012-07-13 11:35:39 +02:00
|
|
|
heap_free( (WCHAR *)table->name );
|
|
|
|
free_columns( (struct column *)table->columns, table->num_cols );
|
2017-03-24 15:49:20 +01:00
|
|
|
heap_free( table->data );
|
2012-10-04 15:36:33 +02:00
|
|
|
list_remove( &table->entry );
|
2012-07-13 11:35:39 +02:00
|
|
|
heap_free( table );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-10 12:01:44 +02:00
|
|
|
void release_table( struct table *table )
|
|
|
|
{
|
|
|
|
if (!InterlockedDecrement( &table->refs )) free_table( table );
|
|
|
|
}
|
|
|
|
|
2012-10-17 11:06:41 +02:00
|
|
|
struct table *addref_table( struct table *table )
|
|
|
|
{
|
|
|
|
InterlockedIncrement( &table->refs );
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2012-10-10 12:01:44 +02:00
|
|
|
struct table *grab_table( const WCHAR *name )
|
2012-07-13 11:35:39 +02:00
|
|
|
{
|
|
|
|
struct table *table;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( table, table_list, struct table, entry )
|
|
|
|
{
|
|
|
|
if (!strcmpiW( table->name, name ))
|
|
|
|
{
|
2012-10-10 12:01:44 +02:00
|
|
|
TRACE("returning %p\n", table);
|
2012-10-17 11:06:41 +02:00
|
|
|
return addref_table( table );
|
2012-07-13 11:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct table *create_table( const WCHAR *name, UINT num_cols, const struct column *columns,
|
2013-06-04 13:28:21 +02:00
|
|
|
UINT num_rows, UINT num_allocated, BYTE *data,
|
2013-05-22 10:09:26 +02:00
|
|
|
enum fill_status (*fill)(struct table *, const struct expr *cond) )
|
2012-07-13 11:35:39 +02:00
|
|
|
{
|
|
|
|
struct table *table;
|
|
|
|
|
|
|
|
if (!(table = heap_alloc( sizeof(*table) ))) return NULL;
|
2013-06-04 13:28:21 +02:00
|
|
|
table->name = heap_strdupW( name );
|
|
|
|
table->num_cols = num_cols;
|
|
|
|
table->columns = columns;
|
|
|
|
table->num_rows = num_rows;
|
|
|
|
table->num_rows_allocated = num_allocated;
|
|
|
|
table->data = data;
|
|
|
|
table->fill = fill;
|
|
|
|
table->flags = TABLE_FLAG_DYNAMIC;
|
|
|
|
table->refs = 0;
|
2012-10-04 15:36:33 +02:00
|
|
|
list_init( &table->entry );
|
2012-07-13 11:35:39 +02:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL add_table( struct table *table )
|
|
|
|
{
|
|
|
|
struct table *iter;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( iter, table_list, struct table, entry )
|
|
|
|
{
|
|
|
|
if (!strcmpiW( iter->name, table->name ))
|
|
|
|
{
|
|
|
|
TRACE("table %s already exists\n", debugstr_w(table->name));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_add_tail( table_list, &table->entry );
|
2012-10-10 12:01:44 +02:00
|
|
|
TRACE("added %p\n", table);
|
2012-07-13 11:35:39 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-30 15:04:49 +02:00
|
|
|
|
2012-10-10 12:01:44 +02:00
|
|
|
BSTR get_method_name( const WCHAR *class, UINT index )
|
2012-07-30 15:04:49 +02:00
|
|
|
{
|
|
|
|
struct table *table;
|
|
|
|
UINT i, count = 0;
|
2012-10-10 12:01:44 +02:00
|
|
|
BSTR ret;
|
2012-07-30 15:04:49 +02:00
|
|
|
|
2012-10-10 12:01:44 +02:00
|
|
|
if (!(table = grab_table( class ))) return NULL;
|
2012-07-30 15:04:49 +02:00
|
|
|
|
|
|
|
for (i = 0; i < table->num_cols; i++)
|
|
|
|
{
|
|
|
|
if (table->columns[i].type & COL_FLAG_METHOD)
|
|
|
|
{
|
2012-10-10 12:01:44 +02:00
|
|
|
if (index == count)
|
|
|
|
{
|
|
|
|
ret = SysAllocString( table->columns[i].name );
|
|
|
|
release_table( table );
|
|
|
|
return ret;
|
|
|
|
}
|
2012-07-30 15:04:49 +02:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2012-10-10 12:01:44 +02:00
|
|
|
release_table( table );
|
2012-07-30 15:04:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|