Home | History | Annotate | Line # | Download | only in libprop
prop_dictionary.c revision 1.9
      1 /*	$NetBSD: prop_dictionary.c,v 1.9 2006/08/21 04:13:28 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <prop/prop_dictionary.h>
     40 #include <prop/prop_string.h>
     41 #include "prop_object_impl.h"
     42 
     43 #if defined(__NetBSD__)
     44 #include <sys/tree.h>
     45 #else
     46 #error Need to find a NetBSD sys/tree.h
     47 #endif
     48 
     49 #if !defined(_KERNEL) && !defined(_STANDALONE)
     50 #include <errno.h>
     51 #endif
     52 
     53 /*
     54  * We implement these like arrays, but we keep them sorted by key.
     55  * This allows us to binary-search as well as keep externalized output
     56  * sane-looking for human eyes.
     57  */
     58 
     59 #define	EXPAND_STEP		16
     60 
     61 /*
     62  * prop_dictionary_keysym_t is allocated with space at the end to hold the
     63  * key.  This must be a regular object so that we can maintain sane iterator
     64  * semantics -- we don't want to require that the caller release the result
     65  * of prop_object_iterator_next().
     66  *
     67  * We'd like to have some small'ish keysym objects for up-to-16 characters
     68  * in a key, some for up-to-32 characters in a key, and then a final bucket
     69  * for up-to-128 characters in a key (not including NUL).  Keys longer than
     70  * 128 characters are not allowed.
     71  */
     72 struct _prop_dictionary_keysym {
     73 	struct _prop_object		pdk_obj;
     74 	size_t				pdk_size;
     75 	RB_ENTRY(_prop_dictionary_keysym) pdk_link;
     76 	char 				pdk_key[1];
     77 	/* actually variable length */
     78 };
     79 
     80 	/* pdk_key[1] takes care of the NUL */
     81 #define	PDK_SIZE_16		(sizeof(struct _prop_dictionary_keysym) + 16)
     82 #define	PDK_SIZE_32		(sizeof(struct _prop_dictionary_keysym) + 32)
     83 #define	PDK_SIZE_128		(sizeof(struct _prop_dictionary_keysym) + 128)
     84 
     85 #define	PDK_MAXKEY		128
     86 
     87 _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDK_SIZE_16, "pdict16")
     88 _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDK_SIZE_32, "pdict32")
     89 _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDK_SIZE_128, "pdict128")
     90 
     91 struct _prop_dict_entry {
     92 	prop_dictionary_keysym_t	pde_key;
     93 	prop_object_t			pde_objref;
     94 };
     95 
     96 struct _prop_dictionary {
     97 	struct _prop_object	pd_obj;
     98 	struct _prop_dict_entry	*pd_array;
     99 	unsigned int		pd_capacity;
    100 	unsigned int		pd_count;
    101 	int			pd_flags;
    102 
    103 	uint32_t		pd_version;
    104 };
    105 
    106 #define	PD_F_IMMUTABLE		0x01	/* dictionary is immutable */
    107 
    108 _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
    109 		"propdict")
    110 _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
    111 		    "property dictionary container object")
    112 
    113 static void		_prop_dictionary_free(void *);
    114 static boolean_t	_prop_dictionary_externalize(
    115 				struct _prop_object_externalize_context *,
    116 				void *);
    117 static boolean_t	_prop_dictionary_equals(void *, void *);
    118 
    119 static const struct _prop_object_type _prop_object_type_dictionary = {
    120 	.pot_type	=	PROP_TYPE_DICTIONARY,
    121 	.pot_free	=	_prop_dictionary_free,
    122 	.pot_extern	=	_prop_dictionary_externalize,
    123 	.pot_equals	=	_prop_dictionary_equals,
    124 };
    125 
    126 static void		_prop_dict_keysym_free(void *);
    127 static boolean_t	_prop_dict_keysym_externalize(
    128 				struct _prop_object_externalize_context *,
    129 				void *);
    130 static boolean_t	_prop_dict_keysym_equals(void *, void *);
    131 
    132 static const struct _prop_object_type _prop_object_type_dict_keysym = {
    133 	.pot_type	=	PROP_TYPE_DICT_KEYSYM,
    134 	.pot_free	=	_prop_dict_keysym_free,
    135 	.pot_extern	=	_prop_dict_keysym_externalize,
    136 	.pot_equals	=	_prop_dict_keysym_equals,
    137 };
    138 
    139 #define	prop_object_is_dictionary(x)		\
    140 		((x)->pd_obj.po_type == &_prop_object_type_dictionary)
    141 #define	prop_object_is_dictionary_keysym(x)	\
    142 		((x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
    143 
    144 #define	prop_dictionary_is_immutable(x)		\
    145 				(((x)->pd_flags & PD_F_IMMUTABLE) != 0)
    146 
    147 struct _prop_dictionary_iterator {
    148 	struct _prop_object_iterator pdi_base;
    149 	unsigned int		pdi_index;
    150 };
    151 
    152 /*
    153  * Dictionary key symbols are immutable, and we are likely to have many
    154  * duplicated key symbols.  So, to save memory, we unique'ify key symbols
    155  * so we only have to have one copy of each string.
    156  */
    157 
    158 static int
    159 _prop_dict_keysym_tree_cmp(prop_dictionary_keysym_t pdk1,
    160 			   prop_dictionary_keysym_t pdk2)
    161 {
    162 
    163 	return (strcmp(pdk1->pdk_key, pdk2->pdk_key));
    164 }
    165 
    166 static RB_HEAD(_prop_dict_keysym_tree, _prop_dictionary_keysym)
    167     _prop_dict_keysym_tree = RB_INITIALIZER(&_prop_dict_keysym_tree);
    168 RB_PROTOTYPE(_prop_dict_keysym_tree, _prop_dictionary_keysym, pdk_link,
    169 	     _prop_dict_keysym_tree_cmp)
    170 RB_GENERATE(_prop_dict_keysym_tree, _prop_dictionary_keysym, pdk_link,
    171 	    _prop_dict_keysym_tree_cmp)
    172 
    173 _PROP_MUTEX_DECL(_prop_dict_keysym_tree_mutex)
    174 
    175 static void
    176 _prop_dict_keysym_put(prop_dictionary_keysym_t pdk)
    177 {
    178 
    179 	if (pdk->pdk_size <= PDK_SIZE_16)
    180 		_PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pdk);
    181 	else if (pdk->pdk_size <= PDK_SIZE_32)
    182 		_PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pdk);
    183 	else {
    184 		_PROP_ASSERT(pdk->pdk_size <= PDK_SIZE_128);
    185 		_PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pdk);
    186 	}
    187 }
    188 
    189 static void
    190 _prop_dict_keysym_free(void *v)
    191 {
    192 	prop_dictionary_keysym_t pdk = v;
    193 
    194 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
    195 	RB_REMOVE(_prop_dict_keysym_tree, &_prop_dict_keysym_tree, pdk);
    196 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    197 
    198 	_prop_dict_keysym_put(pdk);
    199 }
    200 
    201 static boolean_t
    202 _prop_dict_keysym_externalize(struct _prop_object_externalize_context *ctx,
    203 			     void *v)
    204 {
    205 	prop_dictionary_keysym_t pdk = v;
    206 
    207 	/* We externalize these as strings, and they're never empty. */
    208 
    209 	_PROP_ASSERT(pdk->pdk_key[0] != '\0');
    210 
    211 	if (_prop_object_externalize_start_tag(ctx, "string") == FALSE ||
    212 	    _prop_object_externalize_append_encoded_cstring(ctx,
    213 						pdk->pdk_key) == FALSE ||
    214 	    _prop_object_externalize_end_tag(ctx, "string") == FALSE)
    215 		return (FALSE);
    216 
    217 	return (TRUE);
    218 }
    219 
    220 static boolean_t
    221 _prop_dict_keysym_equals(void *v1, void *v2)
    222 {
    223 	prop_dictionary_keysym_t pdk1 = v1;
    224 	prop_dictionary_keysym_t pdk2 = v2;
    225 
    226 	if (! (prop_object_is_dictionary_keysym(pdk1) &&
    227 	       prop_object_is_dictionary_keysym(pdk2)))
    228 		return (FALSE);
    229 
    230 	/*
    231 	 * There is only ever one copy of a keysym at any given time,
    232 	 * so we can reduce this to a simple pointer equality check.
    233 	 */
    234 	return (pdk1 == pdk2);
    235 }
    236 
    237 static prop_dictionary_keysym_t
    238 _prop_dict_keysym_alloc(const char *key)
    239 {
    240 	prop_dictionary_keysym_t opdk, pdk;
    241 	size_t size;
    242 
    243 	/*
    244 	 * Because of the way our RB trees work, we need to create the
    245 	 * new keysym in order to check if it's already in the tree.
    246 	 * Oh well.
    247 	 */
    248 
    249 	size = sizeof(*pdk) + strlen(key) /* pdk_key[1] covers the NUL */;
    250 
    251 	if (size <= PDK_SIZE_16)
    252 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
    253 	else if (size <= PDK_SIZE_32)
    254 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
    255 	else if (size <= PDK_SIZE_128)
    256 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
    257 	else
    258 		pdk = NULL;	/* key too long */
    259 
    260 	if (pdk == NULL)
    261 		return (NULL);
    262 
    263 	_prop_object_init(&pdk->pdk_obj, &_prop_object_type_dict_keysym);
    264 
    265 	strcpy(pdk->pdk_key, key);
    266 	pdk->pdk_size = size;
    267 
    268 	/*
    269 	 * Now check to see if this already exists in the tree.  If it
    270 	 * does, we return a reference to the existing one and free the
    271 	 * new one we just created.
    272 	 */
    273 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
    274 	opdk = RB_INSERT(_prop_dict_keysym_tree, &_prop_dict_keysym_tree, pdk);
    275 	if (opdk != NULL) {
    276 		prop_object_retain(opdk);
    277 		_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    278 		_prop_dict_keysym_put(pdk);
    279 		return (opdk);
    280 	}
    281 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
    282 	return (pdk);
    283 }
    284 
    285 static void
    286 _prop_dictionary_free(void *v)
    287 {
    288 	prop_dictionary_t pd = v;
    289 	prop_dictionary_keysym_t pdk;
    290 	prop_object_t po;
    291 	unsigned int idx;
    292 
    293 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    294 	_PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
    295 		     (pd->pd_capacity != 0 && pd->pd_array != NULL));
    296 
    297 	for (idx = 0; idx < pd->pd_count; idx++) {
    298 		pdk = pd->pd_array[idx].pde_key;
    299 		_PROP_ASSERT(pdk != NULL);
    300 		prop_object_release(pdk);
    301 		po = pd->pd_array[idx].pde_objref;
    302 		_PROP_ASSERT(po != NULL);
    303 		prop_object_release(po);
    304 	}
    305 
    306 	if (pd->pd_array != NULL)
    307 		_PROP_FREE(pd->pd_array, M_PROP_DICT);
    308 
    309 	_PROP_POOL_PUT(_prop_dictionary_pool, pd);
    310 }
    311 
    312 static boolean_t
    313 _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
    314 			     void *v)
    315 {
    316 	prop_dictionary_t pd = v;
    317 	prop_dictionary_keysym_t pdk;
    318 	struct _prop_object *po;
    319 	prop_object_iterator_t pi;
    320 	unsigned int i;
    321 
    322 	if (pd->pd_count == 0)
    323 		return (_prop_object_externalize_empty_tag(ctx, "dict"));
    324 
    325 	if (_prop_object_externalize_start_tag(ctx, "dict") == FALSE ||
    326 	    _prop_object_externalize_append_char(ctx, '\n') == FALSE)
    327 		return (FALSE);
    328 
    329 	pi = prop_dictionary_iterator(pd);
    330 	if (pi == NULL)
    331 		return (FALSE);
    332 
    333 	ctx->poec_depth++;
    334 	_PROP_ASSERT(ctx->poec_depth != 0);
    335 
    336 	while ((pdk = prop_object_iterator_next(pi)) != NULL) {
    337 		po = prop_dictionary_get_keysym(pd, pdk);
    338 		if (po == NULL ||
    339 		    _prop_object_externalize_start_tag(ctx, "key") == FALSE ||
    340 		    _prop_object_externalize_append_encoded_cstring(ctx,
    341 						   pdk->pdk_key) == FALSE ||
    342 		    _prop_object_externalize_end_tag(ctx, "key") == FALSE ||
    343 		    (*po->po_type->pot_extern)(ctx, po) == FALSE) {
    344 			prop_object_iterator_release(pi);
    345 			return (FALSE);
    346 		}
    347 	}
    348 
    349 	prop_object_iterator_release(pi);
    350 
    351 	ctx->poec_depth--;
    352 	for (i = 0; i < ctx->poec_depth; i++) {
    353 		if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
    354 			return (FALSE);
    355 	}
    356 	if (_prop_object_externalize_end_tag(ctx, "dict") == FALSE)
    357 		return (FALSE);
    358 
    359 	return (TRUE);
    360 }
    361 
    362 static boolean_t
    363 _prop_dictionary_equals(void *v1, void *v2)
    364 {
    365 	prop_dictionary_t dict1 = v1;
    366 	prop_dictionary_t dict2 = v2;
    367 	const struct _prop_dict_entry *pde1, *pde2;
    368 	unsigned int idx;
    369 
    370 	if (! (prop_object_is_dictionary(dict1) &&
    371 	       prop_object_is_dictionary(dict2)))
    372 		return (FALSE);
    373 
    374 	if (dict1 == dict2)
    375 		return (TRUE);
    376 	if (dict1->pd_count != dict2->pd_count)
    377 		return (FALSE);
    378 
    379 	for (idx = 0; idx < dict1->pd_count; idx++) {
    380 		pde1 = &dict1->pd_array[idx];
    381 		pde2 = &dict2->pd_array[idx];
    382 
    383 		if (prop_dictionary_keysym_equals(pde1->pde_key,
    384 						  pde2->pde_key) == FALSE)
    385 			return (FALSE);
    386 		if (prop_object_equals(pde1->pde_objref,
    387 				       pde2->pde_objref) == FALSE)
    388 			return (FALSE);
    389 	}
    390 
    391 	return (TRUE);
    392 }
    393 
    394 static prop_dictionary_t
    395 _prop_dictionary_alloc(unsigned int capacity)
    396 {
    397 	prop_dictionary_t pd;
    398 	struct _prop_dict_entry *array;
    399 
    400 	if (capacity != 0) {
    401 		array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
    402 		if (array == NULL)
    403 			return (NULL);
    404 	} else
    405 		array = NULL;
    406 
    407 	pd = _PROP_POOL_GET(_prop_dictionary_pool);
    408 	if (pd != NULL) {
    409 		_prop_object_init(&pd->pd_obj, &_prop_object_type_dictionary);
    410 
    411 		pd->pd_array = array;
    412 		pd->pd_capacity = capacity;
    413 		pd->pd_count = 0;
    414 		pd->pd_flags = 0;
    415 
    416 		pd->pd_version = 0;
    417 	} else if (array != NULL)
    418 		_PROP_FREE(array, M_PROP_DICT);
    419 
    420 	return (pd);
    421 }
    422 
    423 static boolean_t
    424 _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
    425 {
    426 	struct _prop_dict_entry *array, *oarray;
    427 
    428 	oarray = pd->pd_array;
    429 
    430 	array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
    431 	if (array == NULL)
    432 		return (FALSE);
    433 	if (oarray != NULL)
    434 		memcpy(array, oarray, pd->pd_capacity * sizeof(*array));
    435 	pd->pd_array = array;
    436 	pd->pd_capacity = capacity;
    437 
    438 	if (oarray != NULL)
    439 		_PROP_FREE(oarray, M_PROP_DICT);
    440 
    441 	return (TRUE);
    442 }
    443 
    444 static prop_object_t
    445 _prop_dictionary_iterator_next_object(void *v)
    446 {
    447 	struct _prop_dictionary_iterator *pdi = v;
    448 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    449 	prop_dictionary_keysym_t pdk;
    450 
    451 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    452 
    453 	if (pd->pd_version != pdi->pdi_base.pi_version)
    454 		return (NULL);	/* dictionary changed during iteration */
    455 
    456 	_PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
    457 
    458 	if (pdi->pdi_index == pd->pd_count)
    459 		return (NULL);	/* we've iterated all objects */
    460 
    461 	pdk = pd->pd_array[pdi->pdi_index].pde_key;
    462 	pdi->pdi_index++;
    463 
    464 	return (pdk);
    465 }
    466 
    467 static void
    468 _prop_dictionary_iterator_reset(void *v)
    469 {
    470 	struct _prop_dictionary_iterator *pdi = v;
    471 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    472 
    473 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    474 
    475 	pdi->pdi_index = 0;
    476 	pdi->pdi_base.pi_version = pd->pd_version;
    477 }
    478 
    479 /*
    480  * prop_dictionary_create --
    481  *	Create a dictionary.
    482  */
    483 prop_dictionary_t
    484 prop_dictionary_create(void)
    485 {
    486 
    487 	return (_prop_dictionary_alloc(0));
    488 }
    489 
    490 /*
    491  * prop_dictionary_create_with_capacity --
    492  *	Create a dictionary with the capacity to store N objects.
    493  */
    494 prop_dictionary_t
    495 prop_dictionary_create_with_capacity(unsigned int capacity)
    496 {
    497 
    498 	return (_prop_dictionary_alloc(capacity));
    499 }
    500 
    501 /*
    502  * prop_dictionary_copy --
    503  *	Copy a dictionary.  The new dictionary has an initial capacity equal
    504  *	to the number of objects stored int the original dictionary.  The new
    505  *	dictionary contains refrences to the original dictionary's objects,
    506  *	not copies of those objects (i.e. a shallow copy).
    507  */
    508 prop_dictionary_t
    509 prop_dictionary_copy(prop_dictionary_t opd)
    510 {
    511 	prop_dictionary_t pd;
    512 	prop_dictionary_keysym_t pdk;
    513 	prop_object_t po;
    514 	unsigned int idx;
    515 
    516 	if (! prop_object_is_dictionary(opd))
    517 		return (NULL);
    518 
    519 	pd = _prop_dictionary_alloc(opd->pd_count);
    520 	if (pd != NULL) {
    521 		for (idx = 0; idx < opd->pd_count; idx++) {
    522 			pdk = opd->pd_array[idx].pde_key;
    523 			po = opd->pd_array[idx].pde_objref;
    524 
    525 			prop_object_retain(pdk);
    526 			prop_object_retain(po);
    527 
    528 			pd->pd_array[idx].pde_key = pdk;
    529 			pd->pd_array[idx].pde_objref = po;
    530 		}
    531 		pd->pd_count = opd->pd_count;
    532 		pd->pd_flags = opd->pd_flags;
    533 	}
    534 	return (pd);
    535 }
    536 
    537 /*
    538  * prop_dictionary_copy_mutable --
    539  *	Like prop_dictionary_copy(), but the resulting dictionary is
    540  *	mutable.
    541  */
    542 prop_dictionary_t
    543 prop_dictionary_copy_mutable(prop_dictionary_t opd)
    544 {
    545 	prop_dictionary_t pd;
    546 
    547 	if (! prop_object_is_dictionary(opd))
    548 		return (NULL);
    549 
    550 	pd = prop_dictionary_copy(opd);
    551 	if (pd != NULL)
    552 		pd->pd_flags &= ~PD_F_IMMUTABLE;
    553 
    554 	return (pd);
    555 }
    556 
    557 /*
    558  * prop_dictionary_count --
    559  *	Return the number of objects stored in the dictionary.
    560  */
    561 unsigned int
    562 prop_dictionary_count(prop_dictionary_t pd)
    563 {
    564 
    565 	if (! prop_object_is_dictionary(pd))
    566 		return (0);
    567 
    568 	return (pd->pd_count);
    569 }
    570 
    571 /*
    572  * prop_dictionary_ensure_capacity --
    573  *	Ensure that the dictionary has the capacity to store the specified
    574  *	total number of objects (including the objects already stored in
    575  *	the dictionary).
    576  */
    577 boolean_t
    578 prop_dictionary_ensure_capacity(prop_dictionary_t pd, unsigned int capacity)
    579 {
    580 
    581 	if (! prop_object_is_dictionary(pd))
    582 		return (FALSE);
    583 
    584 	if (capacity > pd->pd_capacity)
    585 		return (_prop_dictionary_expand(pd, capacity));
    586 	return (TRUE);
    587 }
    588 
    589 /*
    590  * prop_dictionary_iterator --
    591  *	Return an iterator for the dictionary.  The dictionary is retained by
    592  *	the iterator.
    593  */
    594 prop_object_iterator_t
    595 prop_dictionary_iterator(prop_dictionary_t pd)
    596 {
    597 	struct _prop_dictionary_iterator *pdi;
    598 
    599 	if (! prop_object_is_dictionary(pd))
    600 		return (NULL);
    601 
    602 	pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
    603 	if (pdi == NULL)
    604 		return (NULL);
    605 	pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
    606 	pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
    607 	prop_object_retain(pd);
    608 	pdi->pdi_base.pi_obj = pd;
    609 	pdi->pdi_base.pi_version = pd->pd_version;
    610 
    611 	_prop_dictionary_iterator_reset(pdi);
    612 
    613 	return (&pdi->pdi_base);
    614 }
    615 
    616 static struct _prop_dict_entry *
    617 _prop_dict_lookup(prop_dictionary_t pd, const char *key,
    618 		  unsigned int *idxp)
    619 {
    620 	struct _prop_dict_entry *pde;
    621 	unsigned int base, idx, distance;
    622 	int res;
    623 
    624 	for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
    625 	     distance >>= 1) {
    626 		idx = base + (distance >> 1);
    627 		pde = &pd->pd_array[idx];
    628 		_PROP_ASSERT(pde->pde_key != NULL);
    629 		res = strcmp(key, pde->pde_key->pdk_key);
    630 		if (res == 0) {
    631 			if (idxp != NULL)
    632 				*idxp = idx;
    633 			return (pde);
    634 		}
    635 		if (res > 0) {	/* key > pdk_key: move right */
    636 			base = idx + 1;
    637 			distance--;
    638 		}		/* else move left */
    639 	}
    640 
    641 	/* idx points to the slot we looked at last. */
    642 	if (idxp != NULL)
    643 		*idxp = idx;
    644 	return (NULL);
    645 }
    646 
    647 /*
    648  * prop_dictionary_get --
    649  *	Return the object stored with specified key.
    650  */
    651 prop_object_t
    652 prop_dictionary_get(prop_dictionary_t pd, const char *key)
    653 {
    654 	const struct _prop_dict_entry *pde;
    655 
    656 	if (! prop_object_is_dictionary(pd))
    657 		return (NULL);
    658 
    659 	pde = _prop_dict_lookup(pd, key, NULL);
    660 	if (pde != NULL) {
    661 		_PROP_ASSERT(pde->pde_objref != NULL);
    662 		return (pde->pde_objref);
    663 	}
    664 	return (NULL);
    665 }
    666 
    667 /*
    668  * prop_dictionary_get_keysym --
    669  *	Return the object stored at the location encoded by the keysym.
    670  */
    671 prop_object_t
    672 prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk)
    673 {
    674 
    675 	if (! (prop_object_is_dictionary(pd) &&
    676 	       prop_object_is_dictionary_keysym(pdk)))
    677 		return (NULL);
    678 
    679 	return (prop_dictionary_get(pd, pdk->pdk_key));
    680 }
    681 
    682 /*
    683  * prop_dictionary_set --
    684  *	Store a reference to an object at with the specified key.
    685  *	If the key already exisit, the original object is released.
    686  */
    687 boolean_t
    688 prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
    689 {
    690 	struct _prop_dict_entry *pde;
    691 	prop_dictionary_keysym_t pdk;
    692 	unsigned int idx;
    693 
    694 	if (! prop_object_is_dictionary(pd))
    695 		return (FALSE);
    696 
    697 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    698 
    699 	if (prop_dictionary_is_immutable(pd))
    700 		return (FALSE);
    701 
    702 	pde = _prop_dict_lookup(pd, key, &idx);
    703 	if (pde != NULL) {
    704 		prop_object_t opo = pde->pde_objref;
    705 		prop_object_retain(po);
    706 		pde->pde_objref = po;
    707 		prop_object_release(opo);
    708 		return (TRUE);
    709 	}
    710 
    711 	pdk = _prop_dict_keysym_alloc(key);
    712 	if (pdk == NULL)
    713 		return (FALSE);
    714 
    715 	if (pd->pd_count == pd->pd_capacity &&
    716 	    _prop_dictionary_expand(pd,
    717 	    			    pd->pd_capacity + EXPAND_STEP) == FALSE) {
    718 		prop_object_release(pdk);
    719 	    	return (FALSE);
    720 	}
    721 
    722 	/* At this point, the store will succeed. */
    723 	prop_object_retain(po);
    724 
    725 	if (pd->pd_count == 0) {
    726 		pd->pd_array[0].pde_key = pdk;
    727 		pd->pd_array[0].pde_objref = po;
    728 		pd->pd_count++;
    729 		pd->pd_version++;
    730 		return (TRUE);
    731 	}
    732 
    733 	pde = &pd->pd_array[idx];
    734 	_PROP_ASSERT(pde->pde_key != NULL);
    735 
    736 	if (strcmp(key, pde->pde_key->pdk_key) < 0) {
    737 		/*
    738 		 * key < pdk_key: insert to the left.  This is the same as
    739 		 * inserting to the right, except we decrement the current
    740 		 * index first.
    741 		 *
    742 		 * Because we're unsigned, we have to special case 0
    743 		 * (grumble).
    744 		 */
    745 		if (idx == 0) {
    746 			memmove(&pd->pd_array[1], &pd->pd_array[0],
    747 				pd->pd_count * sizeof(*pde));
    748 			pd->pd_array[0].pde_key = pdk;
    749 			pd->pd_array[0].pde_objref = po;
    750 			pd->pd_count++;
    751 			pd->pd_version++;
    752 			return (TRUE);
    753 		}
    754 		idx--;
    755 	}
    756 
    757 	memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
    758 		(pd->pd_count - (idx + 1)) * sizeof(*pde));
    759 	pd->pd_array[idx + 1].pde_key = pdk;
    760 	pd->pd_array[idx + 1].pde_objref = po;
    761 	pd->pd_count++;
    762 
    763 	pd->pd_version++;
    764 
    765 	return (TRUE);
    766 }
    767 
    768 /*
    769  * prop_dictionary_set_keysym --
    770  *	Replace the object in the dictionary at the location encoded by
    771  *	the keysym.
    772  */
    773 boolean_t
    774 prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
    775 			   prop_object_t po)
    776 {
    777 
    778 	if (! (prop_object_is_dictionary(pd) &&
    779 	       prop_object_is_dictionary_keysym(pdk)))
    780 		return (FALSE);
    781 
    782 	if (prop_dictionary_is_immutable(pd))
    783 		return (FALSE);
    784 
    785 	return (prop_dictionary_set(pd, pdk->pdk_key, po));
    786 }
    787 
    788 static void
    789 _prop_dictionary_remove(prop_dictionary_t pd, struct _prop_dict_entry *pde,
    790     unsigned int idx)
    791 {
    792 	prop_dictionary_keysym_t pdk = pde->pde_key;
    793 	prop_object_t po = pde->pde_objref;
    794 
    795 	_PROP_ASSERT(pd->pd_count != 0);
    796 	_PROP_ASSERT(idx < pd->pd_count);
    797 	_PROP_ASSERT(pde == &pd->pd_array[idx]);
    798 
    799 	idx++;
    800 	memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
    801 		(pd->pd_count - idx) * sizeof(*pde));
    802 	pd->pd_count--;
    803 	pd->pd_version++;
    804 
    805 	prop_object_release(pdk);
    806 	prop_object_release(po);
    807 }
    808 
    809 /*
    810  * prop_dictionary_remove --
    811  *	Remove the reference to an object with the specified key from
    812  *	the dictionary.
    813  */
    814 void
    815 prop_dictionary_remove(prop_dictionary_t pd, const char *key)
    816 {
    817 	struct _prop_dict_entry *pde;
    818 	unsigned int idx;
    819 
    820 	if (! prop_object_is_dictionary(pd))
    821 		return;
    822 
    823 	/* XXX Should this be a _PROP_ASSERT()? */
    824 	if (prop_dictionary_is_immutable(pd))
    825 		return;
    826 
    827 	pde = _prop_dict_lookup(pd, key, &idx);
    828 	/* XXX Should this be a _PROP_ASSERT()? */
    829 	if (pde == NULL)
    830 		return;
    831 
    832 	_prop_dictionary_remove(pd, pde, idx);
    833 }
    834 
    835 /*
    836  * prop_dictionary_remove_keysym --
    837  *	Remove a reference to an object stored in the dictionary at the
    838  *	location encoded by the keysym.
    839  */
    840 void
    841 prop_dictionary_remove_keysym(prop_dictionary_t pd,
    842 			      prop_dictionary_keysym_t pdk)
    843 {
    844 
    845 	if (! (prop_object_is_dictionary(pd) &&
    846 	       prop_object_is_dictionary_keysym(pdk)))
    847 		return;
    848 
    849 	/* XXX Should this be a _PROP_ASSERT()? */
    850 	if (prop_dictionary_is_immutable(pd))
    851 		return;
    852 
    853 	prop_dictionary_remove(pd, pdk->pdk_key);
    854 }
    855 
    856 /*
    857  * prop_dictionary_equals --
    858  *	Return TRUE if the two dictionaries are equivalent.  Note we do a
    859  *	by-value comparison of the objects in the dictionary.
    860  */
    861 boolean_t
    862 prop_dictionary_equals(prop_dictionary_t dict1, prop_dictionary_t dict2)
    863 {
    864 
    865 	return (_prop_dictionary_equals(dict1, dict2));
    866 }
    867 
    868 /*
    869  * prop_dictionary_keysym_cstring_nocopy --
    870  *	Return an immutable reference to the keysym's value.
    871  */
    872 const char *
    873 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)
    874 {
    875 
    876 	if (! prop_object_is_dictionary_keysym(pdk))
    877 		return (NULL);
    878 
    879 	return (pdk->pdk_key);
    880 }
    881 
    882 /*
    883  * prop_dictionary_keysym_equals --
    884  *	Return TRUE if the two dictionary key symbols are equivalent.
    885  *	Note: We do not compare the object references.
    886  */
    887 boolean_t
    888 prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,
    889 			      prop_dictionary_keysym_t pdk2)
    890 {
    891 
    892 	return (_prop_dict_keysym_equals(pdk1, pdk2));
    893 }
    894 
    895 /*
    896  * prop_dictionary_externalize --
    897  *	Externalize a dictionary, returning a NUL-terminated buffer
    898  *	containing the XML-style representation.  The buffer is allocated
    899  *	with the M_TEMP memory type.
    900  */
    901 char *
    902 prop_dictionary_externalize(prop_dictionary_t pd)
    903 {
    904 	struct _prop_object_externalize_context *ctx;
    905 	char *cp;
    906 
    907 	ctx = _prop_object_externalize_context_alloc();
    908 	if (ctx == NULL)
    909 		return (NULL);
    910 
    911 	if (_prop_object_externalize_header(ctx) == FALSE ||
    912 	    (*pd->pd_obj.po_type->pot_extern)(ctx, pd) == FALSE ||
    913 	    _prop_object_externalize_footer(ctx) == FALSE) {
    914 		/* We are responsible for releasing the buffer. */
    915 		_PROP_FREE(ctx->poec_buf, M_TEMP);
    916 		_prop_object_externalize_context_free(ctx);
    917 		return (NULL);
    918 	}
    919 
    920 	cp = ctx->poec_buf;
    921 	_prop_object_externalize_context_free(ctx);
    922 
    923 	return (cp);
    924 }
    925 
    926 /*
    927  * _prop_dictionary_internalize --
    928  *	Parse a <dict>...</dict> and return the object created from the
    929  *	external representation.
    930  */
    931 prop_object_t
    932 _prop_dictionary_internalize(struct _prop_object_internalize_context *ctx)
    933 {
    934 	prop_dictionary_t dict;
    935 	prop_object_t val;
    936 	size_t keylen;
    937 	char *tmpkey;
    938 
    939 	/* We don't currently understand any attributes. */
    940 	if (ctx->poic_tagattr != NULL)
    941 		return (NULL);
    942 
    943 	dict = prop_dictionary_create();
    944 	if (dict == NULL)
    945 		return (NULL);
    946 
    947 	if (ctx->poic_is_empty_element)
    948 		return (dict);
    949 
    950 	tmpkey = _PROP_MALLOC(PDK_MAXKEY + 1, M_TEMP);
    951 	if (tmpkey == NULL)
    952 		goto bad;
    953 
    954 	for (;;) {
    955 		/* Fetch the next tag. */
    956 		if (_prop_object_internalize_find_tag(ctx, NULL,
    957 					_PROP_TAG_TYPE_EITHER) == FALSE)
    958 			goto bad;
    959 
    960 		/* Check to see if this is the end of the dictionary. */
    961 		if (_PROP_TAG_MATCH(ctx, "dict") &&
    962 		    ctx->poic_tag_type == _PROP_TAG_TYPE_END)
    963 			break;
    964 
    965 		/* Ok, it must be a non-empty key start tag. */
    966 		if (!_PROP_TAG_MATCH(ctx, "key") ||
    967 		    ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
    968 		    ctx->poic_is_empty_element)
    969 		    	goto bad;
    970 
    971 		if (_prop_object_internalize_decode_string(ctx,
    972 						tmpkey, PDK_MAXKEY, &keylen,
    973 						&ctx->poic_cp) == FALSE)
    974 			goto bad;
    975 
    976 		_PROP_ASSERT(keylen <= PDK_MAXKEY);
    977 		tmpkey[keylen] = '\0';
    978 
    979 		if (_prop_object_internalize_find_tag(ctx, "key",
    980 					_PROP_TAG_TYPE_END) == FALSE)
    981 			goto bad;
    982 
    983 		/* ..and now the beginning of the value. */
    984 		if (_prop_object_internalize_find_tag(ctx, NULL,
    985 					_PROP_TAG_TYPE_START) == FALSE)
    986 			goto bad;
    987 
    988 		val = _prop_object_internalize_by_tag(ctx);
    989 		if (val == NULL)
    990 			goto bad;
    991 
    992 		if (prop_dictionary_set(dict, tmpkey, val) == FALSE) {
    993 			prop_object_release(val);
    994 			goto bad;
    995 		}
    996 		prop_object_release(val);
    997 	}
    998 
    999 	_PROP_FREE(tmpkey, M_TEMP);
   1000 	return (dict);
   1001 
   1002  bad:
   1003 	if (tmpkey != NULL)
   1004 		_PROP_FREE(tmpkey, M_TEMP);
   1005 	prop_object_release(dict);
   1006 	return (NULL);
   1007 }
   1008 
   1009 /*
   1010  * prop_dictionary_internalize --
   1011  *	Create a dictionary by parsing the NUL-terminated XML-style
   1012  *	representation.
   1013  */
   1014 prop_dictionary_t
   1015 prop_dictionary_internalize(const char *xml)
   1016 {
   1017 	prop_dictionary_t dict = NULL;
   1018 	struct _prop_object_internalize_context *ctx;
   1019 
   1020 	ctx = _prop_object_internalize_context_alloc(xml);
   1021 	if (ctx == NULL)
   1022 		return (NULL);
   1023 
   1024 	/* We start with a <plist> tag. */
   1025 	if (_prop_object_internalize_find_tag(ctx, "plist",
   1026 					      _PROP_TAG_TYPE_START) == FALSE)
   1027 		goto out;
   1028 
   1029 	/* Plist elements cannot be empty. */
   1030 	if (ctx->poic_is_empty_element)
   1031 		goto out;
   1032 
   1033 	/*
   1034 	 * We don't understand any plist attributes, but Apple XML
   1035 	 * property lists often have a "version" attribute.  If we
   1036 	 * see that one, we simply ignore it.
   1037 	 */
   1038 	if (ctx->poic_tagattr != NULL &&
   1039 	    !_PROP_TAGATTR_MATCH(ctx, "version"))
   1040 		goto out;
   1041 
   1042 	/* Next we expect to see <dict>. */
   1043 	if (_prop_object_internalize_find_tag(ctx, "dict",
   1044 					      _PROP_TAG_TYPE_START) == FALSE)
   1045 		goto out;
   1046 
   1047 	dict = _prop_dictionary_internalize(ctx);
   1048 	if (dict == NULL)
   1049 		goto out;
   1050 
   1051 	/* We've advanced past </dict>.  Now we want </plist>. */
   1052 	if (_prop_object_internalize_find_tag(ctx, "plist",
   1053 					      _PROP_TAG_TYPE_END) == FALSE) {
   1054 		prop_object_release(dict);
   1055 		dict = NULL;
   1056 	}
   1057 
   1058  out:
   1059  	_prop_object_internalize_context_free(ctx);
   1060 	return (dict);
   1061 }
   1062 
   1063 #if !defined(_KERNEL) && !defined(_STANDALONE)
   1064 /*
   1065  * prop_dictionary_externalize_to_file --
   1066  *	Externalize a dictionary to the specified file.
   1067  */
   1068 boolean_t
   1069 prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname)
   1070 {
   1071 	char *xml;
   1072 	boolean_t rv;
   1073 	int save_errno;
   1074 
   1075 	xml = prop_dictionary_externalize(dict);
   1076 	if (xml == NULL)
   1077 		return (FALSE);
   1078 	rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
   1079 	if (rv == FALSE)
   1080 		save_errno = errno;
   1081 	_PROP_FREE(xml, M_TEMP);
   1082 	if (rv == FALSE)
   1083 		errno = save_errno;
   1084 
   1085 	return (rv);
   1086 }
   1087 
   1088 /*
   1089  * prop_dictionary_internalize_from_file --
   1090  *	Internalize a dictionary from a file.
   1091  */
   1092 prop_dictionary_t
   1093 prop_dictionary_internalize_from_file(const char *fname)
   1094 {
   1095 	struct _prop_object_internalize_mapped_file *mf;
   1096 	prop_dictionary_t dict;
   1097 
   1098 	mf = _prop_object_internalize_map_file(fname);
   1099 	if (mf == NULL)
   1100 		return (NULL);
   1101 	dict = prop_dictionary_internalize(mf->poimf_xml);
   1102 	_prop_object_internalize_unmap_file(mf);
   1103 
   1104 	return (dict);
   1105 }
   1106 #endif /* !_KERNEL && !_STANDALONE */
   1107