r17333: Some C++ warnings
[Samba.git] / source / registry / reg_objects.c
blob2a898f017054bba45bf9baab134a7e51a9501c35
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* Implementation of registry frontend view functions. */
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
28 /**********************************************************************
30 Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d
31 since the methods use the object pointer as the talloc context for
32 internal private data.
34 There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy()
35 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
36 object.
38 **********************************************************************/
40 /***********************************************************************
41 Add a new key to the array
42 **********************************************************************/
44 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
46 if ( !keyname )
47 return ctr->num_subkeys;
49 /* make sure the keyname is not already there */
51 if ( regsubkey_ctr_key_exists( ctr, keyname ) )
52 return ctr->num_subkeys;
54 /* allocate a space for the char* in the array */
56 if (ctr->subkeys == NULL) {
57 ctr->subkeys = TALLOC_P(ctr, char *);
58 } else {
59 ctr->subkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *, ctr->num_subkeys+1);
62 if (!ctr->subkeys) {
63 ctr->num_subkeys = 0;
64 return 0;
67 /* allocate the string and save it in the array */
69 ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr, keyname );
70 ctr->num_subkeys++;
72 return ctr->num_subkeys;
75 /***********************************************************************
76 Add a new key to the array
77 **********************************************************************/
79 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
81 int i;
83 if ( !keyname )
84 return ctr->num_subkeys;
86 /* make sure the keyname is actually already there */
88 for ( i=0; i<ctr->num_subkeys; i++ ) {
89 if ( strequal( ctr->subkeys[i], keyname ) )
90 break;
93 if ( i == ctr->num_subkeys )
94 return ctr->num_subkeys;
96 /* update if we have any keys left */
97 ctr->num_subkeys--;
98 if ( i < ctr->num_subkeys )
99 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
101 return ctr->num_subkeys;
104 /***********************************************************************
105 Check for the existance of a key
106 **********************************************************************/
108 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
110 int i;
112 for ( i=0; i<ctr->num_subkeys; i++ ) {
113 if ( strequal( ctr->subkeys[i],keyname ) )
114 return True;
117 return False;
120 /***********************************************************************
121 How many keys does the container hold ?
122 **********************************************************************/
124 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
126 return ctr->num_subkeys;
129 /***********************************************************************
130 Retreive a specific key string
131 **********************************************************************/
133 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
135 if ( ! (key_index < ctr->num_subkeys) )
136 return NULL;
138 return ctr->subkeys[key_index];
142 * Utility functions for REGVAL_CTR
145 /***********************************************************************
146 How many keys does the container hold ?
147 **********************************************************************/
149 int regval_ctr_numvals( REGVAL_CTR *ctr )
151 return ctr->num_values;
154 /***********************************************************************
155 allocate memory for and duplicate a REGISTRY_VALUE.
156 This is malloc'd memory so the caller should free it when done
157 **********************************************************************/
159 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
161 REGISTRY_VALUE *copy = NULL;
163 if ( !val )
164 return NULL;
166 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
167 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
168 return NULL;
171 /* copy all the non-pointer initial data */
173 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
175 copy->size = 0;
176 copy->data_p = NULL;
178 if ( val->data_p && val->size )
180 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
181 val->size )) ) {
182 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
183 val->size));
184 SAFE_FREE( copy );
186 copy->size = val->size;
189 return copy;
192 /**********************************************************************
193 free the memory allocated to a REGISTRY_VALUE
194 *********************************************************************/
196 void free_registry_value( REGISTRY_VALUE *val )
198 if ( !val )
199 return;
201 SAFE_FREE( val->data_p );
202 SAFE_FREE( val );
204 return;
207 /**********************************************************************
208 *********************************************************************/
210 uint8* regval_data_p( REGISTRY_VALUE *val )
212 return val->data_p;
215 /**********************************************************************
216 *********************************************************************/
218 uint32 regval_size( REGISTRY_VALUE *val )
220 return val->size;
223 /**********************************************************************
224 *********************************************************************/
226 char* regval_name( REGISTRY_VALUE *val )
228 return val->valuename;
231 /**********************************************************************
232 *********************************************************************/
234 uint32 regval_type( REGISTRY_VALUE *val )
236 return val->type;
239 /***********************************************************************
240 Retreive a pointer to a specific value. Caller shoud dup the structure
241 since this memory will go away when the ctr is free()'d
242 **********************************************************************/
244 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
246 if ( !(idx < ctr->num_values) )
247 return NULL;
249 return ctr->values[idx];
252 /***********************************************************************
253 Check for the existance of a value
254 **********************************************************************/
256 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
258 int i;
260 for ( i=0; i<ctr->num_values; i++ ) {
261 if ( strequal( ctr->values[i]->valuename, value) )
262 return True;
265 return False;
267 /***********************************************************************
268 Add a new registry value to the array
269 **********************************************************************/
271 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
272 const char *data_p, size_t size )
274 if ( !name )
275 return ctr->num_values;
277 /* Delete the current value (if it exists) and add the new one */
279 regval_ctr_delvalue( ctr, name );
281 /* allocate a slot in the array of pointers */
283 if ( ctr->num_values == 0 ) {
284 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
285 } else {
286 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
289 if (!ctr->values) {
290 ctr->num_values = 0;
291 return 0;
294 /* allocate a new value and store the pointer in the arrya */
296 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
297 if (!ctr->values[ctr->num_values]) {
298 ctr->num_values = 0;
299 return 0;
302 /* init the value */
304 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
305 ctr->values[ctr->num_values]->type = type;
306 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
307 ctr, data_p, size );
308 ctr->values[ctr->num_values]->size = size;
309 ctr->num_values++;
311 return ctr->num_values;
314 /***********************************************************************
315 Add a new registry value to the array
316 **********************************************************************/
318 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
320 if ( val ) {
321 /* allocate a slot in the array of pointers */
323 if ( ctr->num_values == 0 ) {
324 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
325 } else {
326 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
329 if (!ctr->values) {
330 ctr->num_values = 0;
331 return 0;
334 /* allocate a new value and store the pointer in the arrya */
336 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
337 if (!ctr->values[ctr->num_values]) {
338 ctr->num_values = 0;
339 return 0;
342 /* init the value */
344 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
345 ctr->values[ctr->num_values]->type = val->type;
346 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
347 ctr, val->data_p, val->size );
348 ctr->values[ctr->num_values]->size = val->size;
349 ctr->num_values++;
352 return ctr->num_values;
355 /***********************************************************************
356 Delete a single value from the registry container.
357 No need to free memory since it is talloc'd.
358 **********************************************************************/
360 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
362 int i;
364 for ( i=0; i<ctr->num_values; i++ ) {
365 if ( strequal( ctr->values[i]->valuename, name ) )
366 break;
369 /* just return if we don't find it */
371 if ( i == ctr->num_values )
372 return ctr->num_values;
374 /* If 'i' was not the last element, just shift everything down one */
375 ctr->num_values--;
376 if ( i < ctr->num_values )
377 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
379 return ctr->num_values;
382 /***********************************************************************
383 Retrieve single value from the registry container.
384 No need to free memory since it is talloc'd.
385 **********************************************************************/
387 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
389 int i;
391 /* search for the value */
393 for ( i=0; i<ctr->num_values; i++ ) {
394 if ( strequal( ctr->values[i]->valuename, name ) )
395 return ctr->values[i];
398 return NULL;
401 /***********************************************************************
402 return the data_p as a uint32
403 **********************************************************************/
405 uint32 regval_dword( REGISTRY_VALUE *val )
407 uint32 data;
409 data = IVAL( regval_data_p(val), 0 );
411 return data;
414 /***********************************************************************
415 return the data_p as a character string
416 **********************************************************************/
418 char* regval_sz( REGISTRY_VALUE *val )
420 static pstring data;
422 rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
424 return data;