s3:registry: remove definition of regsubkey_ctr from the surface.
[Samba.git] / source3 / registry / reg_objects.c
blobc3d67a01ed180c4dd101994932799602265b2727
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 3 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, see <http://www.gnu.org/licenses/>.
20 /* Implementation of registry frontend view functions. */
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
27 struct regsubkey_ctr {
28 uint32 num_subkeys;
29 char **subkeys;
30 int seqnum;
33 /**********************************************************************
35 Note that the struct regsubkey_ctr and REGVAL_CTR objects *must* be
36 talloc()'d since the methods use the object pointer as the talloc
37 context for internal private data.
39 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
40 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
41 object.
43 **********************************************************************/
45 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
47 if (ctr == NULL) {
48 return WERR_INVALID_PARAM;
51 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
52 if (*ctr == NULL) {
53 return WERR_NOMEM;
56 return WERR_OK;
59 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
61 if (ctr == NULL) {
62 return WERR_INVALID_PARAM;
65 ctr->seqnum = seqnum;
67 return WERR_OK;
70 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
72 if (ctr == NULL) {
73 return -1;
76 return ctr->seqnum;
79 /***********************************************************************
80 Add a new key to the array
81 **********************************************************************/
83 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
85 char **newkeys;
87 if ( !keyname ) {
88 return WERR_OK;
91 /* make sure the keyname is not already there */
93 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
94 return WERR_OK;
97 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
98 ctr->num_subkeys+1))) {
99 return WERR_NOMEM;
102 ctr->subkeys = newkeys;
104 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
105 keyname ))) {
107 * Don't shrink the new array again, this wastes a pointer
109 return WERR_NOMEM;
111 ctr->num_subkeys++;
113 return WERR_OK;
116 /***********************************************************************
117 Delete a key from the array
118 **********************************************************************/
120 int regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
122 int i;
124 if ( !keyname )
125 return ctr->num_subkeys;
127 /* make sure the keyname is actually already there */
129 for ( i=0; i<ctr->num_subkeys; i++ ) {
130 if ( strequal( ctr->subkeys[i], keyname ) )
131 break;
134 if ( i == ctr->num_subkeys )
135 return ctr->num_subkeys;
137 /* update if we have any keys left */
138 ctr->num_subkeys--;
139 if ( i < ctr->num_subkeys )
140 memmove(&ctr->subkeys[i], &ctr->subkeys[i+1],
141 sizeof(char*) * (ctr->num_subkeys-i));
143 return ctr->num_subkeys;
146 /***********************************************************************
147 Check for the existance of a key
148 **********************************************************************/
150 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
152 int i;
154 if (!ctr->subkeys) {
155 return False;
158 for ( i=0; i<ctr->num_subkeys; i++ ) {
159 if ( strequal( ctr->subkeys[i],keyname ) )
160 return True;
163 return False;
166 /***********************************************************************
167 How many keys does the container hold ?
168 **********************************************************************/
170 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
172 return ctr->num_subkeys;
175 /***********************************************************************
176 Retreive a specific key string
177 **********************************************************************/
179 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32 key_index )
181 if ( ! (key_index < ctr->num_subkeys) )
182 return NULL;
184 return ctr->subkeys[key_index];
188 * Utility functions for REGVAL_CTR
191 /***********************************************************************
192 How many keys does the container hold ?
193 **********************************************************************/
195 int regval_ctr_numvals( REGVAL_CTR *ctr )
197 return ctr->num_values;
200 /***********************************************************************
201 allocate memory for and duplicate a REGISTRY_VALUE.
202 This is malloc'd memory so the caller should free it when done
203 **********************************************************************/
205 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
207 REGISTRY_VALUE *copy = NULL;
209 if ( !val )
210 return NULL;
212 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
213 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
214 return NULL;
217 /* copy all the non-pointer initial data */
219 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
221 copy->size = 0;
222 copy->data_p = NULL;
224 if ( val->data_p && val->size )
226 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
227 val->size )) ) {
228 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
229 "bytes!\n", val->size));
230 SAFE_FREE( copy );
231 return NULL;
233 copy->size = val->size;
236 return copy;
239 /**********************************************************************
240 free the memory allocated to a REGISTRY_VALUE
241 *********************************************************************/
243 void free_registry_value( REGISTRY_VALUE *val )
245 if ( !val )
246 return;
248 SAFE_FREE( val->data_p );
249 SAFE_FREE( val );
251 return;
254 /**********************************************************************
255 *********************************************************************/
257 uint8* regval_data_p( REGISTRY_VALUE *val )
259 return val->data_p;
262 /**********************************************************************
263 *********************************************************************/
265 uint32 regval_size( REGISTRY_VALUE *val )
267 return val->size;
270 /**********************************************************************
271 *********************************************************************/
273 char* regval_name( REGISTRY_VALUE *val )
275 return val->valuename;
278 /**********************************************************************
279 *********************************************************************/
281 uint32 regval_type( REGISTRY_VALUE *val )
283 return val->type;
286 /***********************************************************************
287 Retreive a pointer to a specific value. Caller shoud dup the structure
288 since this memory will go away when the ctr is free()'d
289 **********************************************************************/
291 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
293 if ( !(idx < ctr->num_values) )
294 return NULL;
296 return ctr->values[idx];
299 /***********************************************************************
300 Check for the existance of a value
301 **********************************************************************/
303 bool regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
305 int i;
307 for ( i=0; i<ctr->num_values; i++ ) {
308 if ( strequal( ctr->values[i]->valuename, value) )
309 return True;
312 return False;
315 /***********************************************************************
316 * compose a REGISTRY_VALUE from input data
317 **********************************************************************/
319 REGISTRY_VALUE *regval_compose(TALLOC_CTX *ctx, const char *name, uint16 type,
320 const char *data_p, size_t size)
322 REGISTRY_VALUE *regval = TALLOC_P(ctx, REGISTRY_VALUE);
324 if (regval == NULL) {
325 return NULL;
328 fstrcpy(regval->valuename, name);
329 regval->type = type;
330 if (size) {
331 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
332 if (!regval->data_p) {
333 TALLOC_FREE(regval);
334 return NULL;
336 } else {
337 regval->data_p = NULL;
339 regval->size = size;
341 return regval;
344 /***********************************************************************
345 Add a new registry value to the array
346 **********************************************************************/
348 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
349 const char *data_p, size_t size )
351 if ( !name )
352 return ctr->num_values;
354 /* Delete the current value (if it exists) and add the new one */
356 regval_ctr_delvalue( ctr, name );
358 /* allocate a slot in the array of pointers */
360 if ( ctr->num_values == 0 ) {
361 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
362 } else {
363 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
364 REGISTRY_VALUE *,
365 ctr->num_values+1);
368 if (!ctr->values) {
369 ctr->num_values = 0;
370 return 0;
373 /* allocate a new value and store the pointer in the arrya */
375 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
376 size);
377 if (ctr->values[ctr->num_values] == NULL) {
378 ctr->num_values = 0;
379 return 0;
381 ctr->num_values++;
383 return ctr->num_values;
386 /***********************************************************************
387 Add a new registry value to the array
388 **********************************************************************/
390 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
392 if ( val ) {
393 regval_ctr_addvalue(ctr, val->valuename, val->type,
394 (char *)val->data_p, val->size);
397 return ctr->num_values;
400 /***********************************************************************
401 Delete a single value from the registry container.
402 No need to free memory since it is talloc'd.
403 **********************************************************************/
405 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
407 int i;
409 for ( i=0; i<ctr->num_values; i++ ) {
410 if ( strequal( ctr->values[i]->valuename, name ) )
411 break;
414 /* just return if we don't find it */
416 if ( i == ctr->num_values )
417 return ctr->num_values;
419 /* If 'i' was not the last element, just shift everything down one */
420 ctr->num_values--;
421 if ( i < ctr->num_values )
422 memmove(&ctr->values[i], &ctr->values[i+1],
423 sizeof(REGISTRY_VALUE*)*(ctr->num_values-i));
425 return ctr->num_values;
428 /***********************************************************************
429 Retrieve single value from the registry container.
430 No need to free memory since it is talloc'd.
431 **********************************************************************/
433 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
435 int i;
437 /* search for the value */
439 for ( i=0; i<ctr->num_values; i++ ) {
440 if ( strequal( ctr->values[i]->valuename, name ) )
441 return ctr->values[i];
444 return NULL;
447 /***********************************************************************
448 return the data_p as a uint32
449 **********************************************************************/
451 uint32 regval_dword( REGISTRY_VALUE *val )
453 uint32 data;
455 data = IVAL( regval_data_p(val), 0 );
457 return data;
460 /***********************************************************************
461 return the data_p as a character string
462 **********************************************************************/
464 char *regval_sz(REGISTRY_VALUE *val)
466 char *data = NULL;
468 rpcstr_pull_talloc(talloc_tos(), &data,
469 regval_data_p(val), regval_size(val),0);
470 return data;