Synchronize with trunk.
[reactos.git] / dll / win32 / wbemprox / table.c
1 /*
2 * Copyright 2012 Hans Leidekker for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define COM_NO_WINDOWS_H
22
23 #define COBJMACROS
24
25 #include "config.h"
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "objbase.h"
32 #include "oleauto.h"
33 #include "wbemcli.h"
34
35 #include "wine/debug.h"
36 #include "wbemprox_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
39
40 HRESULT get_column_index( const struct table *table, const WCHAR *name, UINT *column )
41 {
42 UINT i;
43 for (i = 0; i < table->num_cols; i++)
44 {
45 if (!strcmpiW( table->columns[i].name, name ))
46 {
47 *column = i;
48 return S_OK;
49 }
50 }
51 return WBEM_E_INVALID_QUERY;
52 }
53
54 UINT get_type_size( CIMTYPE type )
55 {
56 if (type & CIM_FLAG_ARRAY) return sizeof(void *);
57
58 switch (type)
59 {
60 case CIM_BOOLEAN:
61 return sizeof(int);
62 case CIM_SINT16:
63 case CIM_UINT16:
64 return sizeof(INT16);
65 case CIM_SINT32:
66 case CIM_UINT32:
67 return sizeof(INT32);
68 case CIM_SINT64:
69 case CIM_UINT64:
70 return sizeof(INT64);
71 case CIM_DATETIME:
72 case CIM_STRING:
73 return sizeof(WCHAR *);
74 default:
75 ERR("unhandled type %u\n", type);
76 break;
77 }
78 return sizeof(LONGLONG);
79 }
80
81 static UINT get_column_size( const struct table *table, UINT column )
82 {
83 return get_type_size( table->columns[column].type & COL_TYPE_MASK );
84 }
85
86 static UINT get_column_offset( const struct table *table, UINT column )
87 {
88 UINT i, offset = 0;
89 for (i = 0; i < column; i++) offset += get_column_size( table, i );
90 return offset;
91 }
92
93 static UINT get_row_size( const struct table *table )
94 {
95 return get_column_offset( table, table->num_cols - 1 ) + get_column_size( table, table->num_cols - 1 );
96 }
97
98 HRESULT get_value( const struct table *table, UINT row, UINT column, LONGLONG *val )
99 {
100 UINT col_offset, row_size;
101 const BYTE *ptr;
102
103 col_offset = get_column_offset( table, column );
104 row_size = get_row_size( table );
105 ptr = table->data + row * row_size + col_offset;
106
107 if (table->columns[column].type & CIM_FLAG_ARRAY)
108 {
109 *val = (INT_PTR)*(const void **)ptr;
110 return S_OK;
111 }
112 switch (table->columns[column].type & COL_TYPE_MASK)
113 {
114 case CIM_BOOLEAN:
115 *val = *(const int *)ptr;
116 break;
117 case CIM_DATETIME:
118 case CIM_STRING:
119 *val = (INT_PTR)*(const WCHAR **)ptr;
120 break;
121 case CIM_SINT16:
122 *val = *(const INT16 *)ptr;
123 break;
124 case CIM_UINT16:
125 *val = *(const UINT16 *)ptr;
126 break;
127 case CIM_SINT32:
128 *val = *(const INT32 *)ptr;
129 break;
130 case CIM_UINT32:
131 *val = *(const UINT32 *)ptr;
132 break;
133 case CIM_SINT64:
134 *val = *(const INT64 *)ptr;
135 break;
136 case CIM_UINT64:
137 *val = *(const UINT64 *)ptr;
138 break;
139 default:
140 ERR("invalid column type %u\n", table->columns[column].type & COL_TYPE_MASK);
141 *val = 0;
142 break;
143 }
144 return S_OK;
145 }
146
147 BSTR get_value_bstr( const struct table *table, UINT row, UINT column )
148 {
149 static const WCHAR fmt_signedW[] = {'%','d',0};
150 static const WCHAR fmt_unsignedW[] = {'%','u',0};
151 static const WCHAR fmt_signed64W[] = {'%','I','6','4','d',0};
152 static const WCHAR fmt_unsigned64W[] = {'%','I','6','4','u',0};
153 static const WCHAR fmt_strW[] = {'\"','%','s','\"',0};
154 static const WCHAR trueW[] = {'T','R','U','E',0};
155 static const WCHAR falseW[] = {'F','A','L','S','E',0};
156 LONGLONG val;
157 BSTR ret;
158 WCHAR number[22];
159 UINT len;
160
161 if (table->columns[column].type & CIM_FLAG_ARRAY)
162 {
163 FIXME("array to string conversion not handled\n");
164 return NULL;
165 }
166 if (get_value( table, row, column, &val ) != S_OK) return NULL;
167
168 switch (table->columns[column].type & COL_TYPE_MASK)
169 {
170 case CIM_BOOLEAN:
171 if (val) return SysAllocString( trueW );
172 else return SysAllocString( falseW );
173
174 case CIM_DATETIME:
175 case CIM_STRING:
176 if (!val) return NULL;
177 len = strlenW( (const WCHAR *)(INT_PTR)val ) + 2;
178 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
179 sprintfW( ret, fmt_strW, (const WCHAR *)(INT_PTR)val );
180 return ret;
181
182 case CIM_SINT16:
183 case CIM_SINT32:
184 sprintfW( number, fmt_signedW, val );
185 return SysAllocString( number );
186
187 case CIM_UINT16:
188 case CIM_UINT32:
189 sprintfW( number, fmt_unsignedW, val );
190 return SysAllocString( number );
191
192 case CIM_SINT64:
193 wsprintfW( number, fmt_signed64W, val );
194 return SysAllocString( number );
195
196 case CIM_UINT64:
197 wsprintfW( number, fmt_unsigned64W, val );
198 return SysAllocString( number );
199
200 default:
201 FIXME("unhandled column type %u\n", table->columns[column].type & COL_TYPE_MASK);
202 break;
203 }
204 return NULL;
205 }
206
207 HRESULT set_value( const struct table *table, UINT row, UINT column, LONGLONG val,
208 CIMTYPE type )
209 {
210 UINT col_offset, row_size;
211 BYTE *ptr;
212
213 if ((table->columns[column].type & COL_TYPE_MASK) != type) return WBEM_E_TYPE_MISMATCH;
214
215 col_offset = get_column_offset( table, column );
216 row_size = get_row_size( table );
217 ptr = table->data + row * row_size + col_offset;
218
219 switch (table->columns[column].type & COL_TYPE_MASK)
220 {
221 case CIM_DATETIME:
222 case CIM_STRING:
223 *(WCHAR **)ptr = (WCHAR *)(INT_PTR)val;
224 break;
225 case CIM_SINT16:
226 *(INT16 *)ptr = val;
227 break;
228 case CIM_UINT16:
229 *(UINT16 *)ptr = val;
230 break;
231 case CIM_SINT32:
232 *(INT32 *)ptr = val;
233 break;
234 case CIM_UINT32:
235 *(UINT32 *)ptr = val;
236 break;
237 case CIM_SINT64:
238 *(INT64 *)ptr = val;
239 break;
240 case CIM_UINT64:
241 *(UINT64 *)ptr = val;
242 break;
243 default:
244 FIXME("unhandled column type %u\n", type);
245 return WBEM_E_FAILED;
246 }
247 return S_OK;
248 }
249
250 HRESULT get_method( const struct table *table, const WCHAR *name, class_method **func )
251 {
252 UINT i, j;
253
254 for (i = 0; i < table->num_rows; i++)
255 {
256 for (j = 0; j < table->num_cols; j++)
257 {
258 if (table->columns[j].type & COL_FLAG_METHOD && !strcmpW( table->columns[j].name, name ))
259 {
260 HRESULT hr;
261 LONGLONG val;
262
263 if ((hr = get_value( table, i, j, &val )) != S_OK) return hr;
264 *func = (class_method *)(INT_PTR)val;
265 return S_OK;
266 }
267 }
268 }
269 return WBEM_E_INVALID_METHOD;
270
271 }
272
273 static void clear_table( struct table *table )
274 {
275 UINT i, j, type;
276 LONGLONG val;
277
278 if (!table->data) return;
279
280 for (i = 0; i < table->num_rows; i++)
281 {
282 for (j = 0; j < table->num_cols; j++)
283 {
284 if (!(table->columns[j].type & COL_FLAG_DYNAMIC)) continue;
285
286 type = table->columns[j].type & COL_TYPE_MASK;
287 if (type == CIM_STRING || type == CIM_DATETIME || (type & CIM_FLAG_ARRAY))
288 {
289 if (get_value( table, i, j, &val ) == S_OK) heap_free( (void *)(INT_PTR)val );
290 }
291 }
292 }
293 if (table->fill)
294 {
295 table->num_rows = 0;
296 heap_free( table->data );
297 table->data = NULL;
298 }
299 }
300
301 void free_columns( struct column *columns, UINT num_cols )
302 {
303 UINT i;
304
305 for (i = 0; i < num_cols; i++)
306 {
307 heap_free( (WCHAR *)columns[i].name );
308 }
309 heap_free( columns );
310 }
311
312 void free_table( struct table *table )
313 {
314 if (!table) return;
315
316 clear_table( table );
317 if (table->flags & TABLE_FLAG_DYNAMIC)
318 {
319 TRACE("destroying %p\n", table);
320 heap_free( (WCHAR *)table->name );
321 free_columns( (struct column *)table->columns, table->num_cols );
322 list_remove( &table->entry );
323 heap_free( table );
324 }
325 }
326
327 void release_table( struct table *table )
328 {
329 if (!InterlockedDecrement( &table->refs )) free_table( table );
330 }
331
332 struct table *addref_table( struct table *table )
333 {
334 InterlockedIncrement( &table->refs );
335 return table;
336 }
337
338 struct table *grab_table( const WCHAR *name )
339 {
340 struct table *table;
341
342 LIST_FOR_EACH_ENTRY( table, table_list, struct table, entry )
343 {
344 if (!strcmpiW( table->name, name ))
345 {
346 if (table->fill && !table->data) table->fill( table );
347 TRACE("returning %p\n", table);
348 return addref_table( table );
349 }
350 }
351 return NULL;
352 }
353
354 struct table *create_table( const WCHAR *name, UINT num_cols, const struct column *columns,
355 UINT num_rows, BYTE *data, void (*fill)(struct table *) )
356 {
357 struct table *table;
358
359 if (!(table = heap_alloc( sizeof(*table) ))) return NULL;
360 table->name = heap_strdupW( name );
361 table->num_cols = num_cols;
362 table->columns = columns;
363 table->num_rows = num_rows;
364 table->data = data;
365 table->fill = fill;
366 table->flags = TABLE_FLAG_DYNAMIC;
367 table->refs = 0;
368 list_init( &table->entry );
369 return table;
370 }
371
372 BOOL add_table( struct table *table )
373 {
374 struct table *iter;
375
376 LIST_FOR_EACH_ENTRY( iter, table_list, struct table, entry )
377 {
378 if (!strcmpiW( iter->name, table->name ))
379 {
380 TRACE("table %s already exists\n", debugstr_w(table->name));
381 return FALSE;
382 }
383 }
384 list_add_tail( table_list, &table->entry );
385 TRACE("added %p\n", table);
386 return TRUE;
387 }
388
389 BSTR get_method_name( const WCHAR *class, UINT index )
390 {
391 struct table *table;
392 UINT i, count = 0;
393 BSTR ret;
394
395 if (!(table = grab_table( class ))) return NULL;
396
397 for (i = 0; i < table->num_cols; i++)
398 {
399 if (table->columns[i].type & COL_FLAG_METHOD)
400 {
401 if (index == count)
402 {
403 ret = SysAllocString( table->columns[i].name );
404 release_table( table );
405 return ret;
406 }
407 count++;
408 }
409 }
410 release_table( table );
411 return NULL;
412 }
413
414 BSTR get_property_name( const WCHAR *class, UINT index )
415 {
416 struct table *table;
417 UINT i, count = 0;
418 BSTR ret;
419
420 if (!(table = grab_table( class ))) return NULL;
421
422 for (i = 0; i < table->num_cols; i++)
423 {
424 if (!(table->columns[i].type & COL_FLAG_METHOD))
425 {
426 if (index == count)
427 {
428 ret = SysAllocString( table->columns[i].name );
429 release_table( table );
430 return ret;
431 }
432 count++;
433 }
434 }
435 release_table( table );
436 return NULL;
437 }