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