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