missed one on BUG 1195; make sure to set the private * to NULL
[Samba/gebeck_regimport.git] / source3 / registry / reg_objects.c
blob9cfeb7faa97baf29b61c973a2af613295262e246
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Gerald Carter 2002.
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
29 /***********************************************************************
30 Init the talloc context held by a REGSUBKEY_CTR structure
31 **********************************************************************/
33 void regsubkey_ctr_init( REGSUBKEY_CTR *ctr )
35 if ( !ctr->ctx )
36 ctr->ctx = talloc_init("regsubkey_ctr_init for ctr %p", ctr);
39 /***********************************************************************
40 Add a new key to the array
41 **********************************************************************/
43 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
45 uint32 len;
46 char **pp;
48 if ( keyname )
50 len = strlen( keyname );
52 /* allocate a space for the char* in the array */
54 if ( ctr->subkeys == 0 )
55 ctr->subkeys = talloc( ctr->ctx, sizeof(char*) );
56 else {
57 pp = talloc_realloc( ctr->ctx, ctr->subkeys, sizeof(char*)*(ctr->num_subkeys+1) );
58 if ( pp )
59 ctr->subkeys = pp;
62 /* allocate the string and save it in the array */
64 ctr->subkeys[ctr->num_subkeys] = talloc( ctr->ctx, len+1 );
65 strncpy( ctr->subkeys[ctr->num_subkeys], keyname, len+1 );
66 ctr->num_subkeys++;
69 return ctr->num_subkeys;
72 /***********************************************************************
73 How many keys does the container hold ?
74 **********************************************************************/
76 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
78 return ctr->num_subkeys;
81 /***********************************************************************
82 Retreive a specific key string
83 **********************************************************************/
85 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
87 if ( ! (key_index < ctr->num_subkeys) )
88 return NULL;
90 return ctr->subkeys[key_index];
93 /***********************************************************************
94 free memory held by a REGSUBKEY_CTR structure
95 **********************************************************************/
97 void regsubkey_ctr_destroy( REGSUBKEY_CTR *ctr )
99 if ( ctr ) {
100 talloc_destroy( ctr->ctx );
101 ZERO_STRUCTP( ctr );
107 * Utility functions for REGVAL_CTR
110 /***********************************************************************
111 Init the talloc context held by a REGSUBKEY_CTR structure
112 **********************************************************************/
114 void regval_ctr_init( REGVAL_CTR *ctr )
116 if ( !ctr->ctx )
117 ctr->ctx = talloc_init("regval_ctr_init for ctr %p", ctr);
120 /***********************************************************************
121 How many keys does the container hold ?
122 **********************************************************************/
124 int regval_ctr_numvals( REGVAL_CTR *ctr )
126 return ctr->num_values;
129 /***********************************************************************
130 allocate memory for and duplicate a REGISTRY_VALUE.
131 This is malloc'd memory so the caller should free it when done
132 **********************************************************************/
134 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
136 REGISTRY_VALUE *copy = NULL;
138 if ( !val )
139 return NULL;
141 if ( !(copy = malloc( sizeof(REGISTRY_VALUE) )) ) {
142 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
143 return NULL;
146 /* copy all the non-pointer initial data */
148 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
149 if ( val->data_p )
151 if ( !(copy->data_p = memdup( val->data_p, val->size )) ) {
152 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
153 val->size));
154 SAFE_FREE( copy );
158 return copy;
161 /**********************************************************************
162 free the memory allocated to a REGISTRY_VALUE
163 *********************************************************************/
165 void free_registry_value( REGISTRY_VALUE *val )
167 if ( !val )
168 return;
170 SAFE_FREE( val->data_p );
171 SAFE_FREE( val );
173 return;
176 /**********************************************************************
177 *********************************************************************/
179 uint8* regval_data_p( REGISTRY_VALUE *val )
181 return val->data_p;
184 /**********************************************************************
185 *********************************************************************/
187 int regval_size( REGISTRY_VALUE *val )
189 return val->size;
192 /**********************************************************************
193 *********************************************************************/
195 char* regval_name( REGISTRY_VALUE *val )
197 return val->valuename;
200 /**********************************************************************
201 *********************************************************************/
203 uint32 regval_type( REGISTRY_VALUE *val )
205 return val->type;
208 /***********************************************************************
209 Retreive a pointer to a specific value. Caller shoud dup the structure
210 since this memory may go away with a regval_ctr_destroy()
211 **********************************************************************/
213 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
215 if ( !(idx < ctr->num_values) )
216 return NULL;
218 return ctr->values[idx];
221 /***********************************************************************
222 Retrive the TALLOC_CTX associated with a REGISTRY_VALUE
223 **********************************************************************/
225 TALLOC_CTX* regval_ctr_getctx( REGVAL_CTR *val )
227 if ( !val )
228 return NULL;
230 return val->ctx;
233 /***********************************************************************
234 Add a new registry value to the array
235 **********************************************************************/
237 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
238 const char *data_p, size_t size )
240 REGISTRY_VALUE **ppreg;
242 if ( name )
244 /* allocate a slot in the array of pointers */
246 if ( ctr->num_values == 0 )
247 ctr->values = talloc( ctr->ctx, sizeof(REGISTRY_VALUE*) );
248 else {
249 ppreg = talloc_realloc( ctr->ctx, ctr->values, sizeof(REGISTRY_VALUE*)*(ctr->num_values+1) );
250 if ( ppreg )
251 ctr->values = ppreg;
254 /* allocate a new value and store the pointer in the arrya */
256 ctr->values[ctr->num_values] = talloc( ctr->ctx, sizeof(REGISTRY_VALUE) );
258 /* init the value */
260 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
261 ctr->values[ctr->num_values]->type = type;
262 ctr->values[ctr->num_values]->data_p = talloc_memdup( ctr->ctx, data_p, size );
263 ctr->values[ctr->num_values]->size = size;
264 ctr->num_values++;
267 return ctr->num_values;
270 /***********************************************************************
271 Add a new registry value to the array
272 **********************************************************************/
274 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
276 REGISTRY_VALUE **ppreg;
278 if ( val )
280 /* allocate a slot in the array of pointers */
282 if ( ctr->num_values == 0 )
283 ctr->values = talloc( ctr->ctx, sizeof(REGISTRY_VALUE*) );
284 else {
285 ppreg = talloc_realloc( ctr->ctx, ctr->values, sizeof(REGISTRY_VALUE*)*(ctr->num_values+1) );
286 if ( ppreg )
287 ctr->values = ppreg;
290 /* allocate a new value and store the pointer in the arrya */
292 ctr->values[ctr->num_values] = talloc( ctr->ctx, sizeof(REGISTRY_VALUE) );
294 /* init the value */
296 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
297 ctr->values[ctr->num_values]->type = val->type;
298 ctr->values[ctr->num_values]->data_p = talloc_memdup( ctr->ctx, val->data_p, val->size );
299 ctr->values[ctr->num_values]->size = val->size;
300 ctr->num_values++;
303 return ctr->num_values;
306 /***********************************************************************
307 Delete a single value from the registry container.
308 No need to free memory since it is talloc'd.
309 **********************************************************************/
311 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
313 int i;
315 /* search for the value */
316 if (!(ctr->num_values))
317 return 0;
319 for ( i=0; i<ctr->num_values; i++ ) {
320 if ( strcmp( ctr->values[i]->valuename, name ) == 0)
321 break;
324 /* just return if we don't find it */
326 if ( i == ctr->num_values )
327 return ctr->num_values;
329 /* just shift everything down one */
331 for ( /* use previous i */; i<(ctr->num_values-1); i++ )
332 memcpy( ctr->values[i], ctr->values[i+1], sizeof(REGISTRY_VALUE) );
334 /* paranoia */
336 ZERO_STRUCTP( ctr->values[i] );
338 ctr->num_values--;
340 return ctr->num_values;
343 /***********************************************************************
344 Delete a single value from the registry container.
345 No need to free memory since it is talloc'd.
346 **********************************************************************/
348 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
350 int i;
352 /* search for the value */
354 for ( i=0; i<ctr->num_values; i++ ) {
355 if ( strequal( ctr->values[i]->valuename, name ) )
356 return ctr->values[i];
359 return NULL;
362 /***********************************************************************
363 free memory held by a REGVAL_CTR structure
364 **********************************************************************/
366 void regval_ctr_destroy( REGVAL_CTR *ctr )
368 if ( ctr ) {
369 talloc_destroy( ctr->ctx );
370 ZERO_STRUCTP( ctr );