Home | History | Annotate | Line # | Download | only in libprop
prop_dictionary.c revision 1.2
      1 /*	$NetBSD: prop_dictionary.c,v 1.2 2006/05/07 06:25:49 simonb 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 /*
     44  * We implement these like arrays, but we keep them sorted by key.
     45  * This allows us to binary-search as well as keep externalized output
     46  * sane-looking for human eyes.
     47  */
     48 
     49 #define	EXPAND_STEP		16
     50 
     51 /*
     52  * prop_dictionary_keysym_t is allocated with space at the end to hold the
     53  * key.  This must be a regular object so that we can maintain sane iterator
     54  * semantics -- we don't want to require that the caller release the result
     55  * of prop_object_iterator_next().
     56  *
     57  * We'd like to have some small'ish keysym objects for up-to-16 characters
     58  * in a key, some for up-to-32 characters in a key, and then a final bucket
     59  * for up-to-128 characters in a key (not including NUL).  Keys longer than
     60  * 128 characters are not allowed.
     61  */
     62 struct _prop_dictionary_keysym {
     63 	struct _prop_object		pde_obj;
     64 	prop_object_t			pde_objref;
     65 	size_t				pde_size;
     66 	char 				pde_key[1];
     67 	/* actually variable length */
     68 };
     69 
     70 	/* pde_key[1] takes care of the NUL */
     71 #define	PDE_SIZE_16		(sizeof(struct _prop_dictionary_keysym) + 16)
     72 #define	PDE_SIZE_32		(sizeof(struct _prop_dictionary_keysym) + 32)
     73 #define	PDE_SIZE_128		(sizeof(struct _prop_dictionary_keysym) + 128)
     74 
     75 #define	PDE_MAXKEY		128
     76 
     77 _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDE_SIZE_16, "pdict16")
     78 _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDE_SIZE_32, "pdict32")
     79 _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDE_SIZE_128, "pdict128")
     80 
     81 struct _prop_dictionary {
     82 	struct _prop_object	pd_obj;
     83 	prop_dictionary_keysym_t *pd_array;
     84 	unsigned int		pd_capacity;
     85 	unsigned int		pd_count;
     86 	int			pd_flags;
     87 
     88 	uint32_t		pd_version;
     89 };
     90 
     91 #define	PD_F_IMMUTABLE		0x01	/* dictionary is immutable */
     92 
     93 _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
     94 		"propdict")
     95 _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
     96 		    "property dictionary container object")
     97 
     98 #define	prop_object_is_dictionary(x)		\
     99 				((x)->pd_obj.po_type == PROP_TYPE_DICTIONARY)
    100 #define	prop_object_is_dictionary_keysym(x)	\
    101 				((x)->pde_obj.po_type == PROP_TYPE_DICT_KEYSYM)
    102 
    103 #define	prop_dictionary_is_immutable(x)		\
    104 				(((x)->pd_flags & PD_F_IMMUTABLE) != 0)
    105 
    106 struct _prop_dictionary_iterator {
    107 	struct _prop_object_iterator pdi_base;
    108 	unsigned int		pdi_index;
    109 };
    110 
    111 static void
    112 _prop_dict_entry_free(void *v)
    113 {
    114 	prop_dictionary_keysym_t pde = v;
    115 	prop_object_t po;
    116 
    117 	_PROP_ASSERT(pde->pde_objref != NULL);
    118 	po = pde->pde_objref;
    119 
    120 	if (pde->pde_size <= PDE_SIZE_16)
    121 		_PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pde);
    122 	else if (pde->pde_size <= PDE_SIZE_32)
    123 		_PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pde);
    124 	else {
    125 		_PROP_ASSERT(pde->pde_size <= PDE_SIZE_128);
    126 		_PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pde);
    127 	}
    128 
    129 	prop_object_release(po);
    130 }
    131 
    132 static boolean_t
    133 _prop_dict_entry_externalize(struct _prop_object_externalize_context *ctx,
    134 			     void *v)
    135 {
    136 	prop_dictionary_keysym_t pde = v;
    137 
    138 	/* We externalize these as strings, and they're never empty. */
    139 
    140 	_PROP_ASSERT(pde->pde_key[0] != '\0');
    141 
    142 	if (_prop_object_externalize_start_tag(ctx, "string") == FALSE ||
    143 	    _prop_object_externalize_append_encoded_cstring(ctx,
    144 						pde->pde_key) == FALSE ||
    145 	    _prop_object_externalize_end_tag(ctx, "string") == FALSE)
    146 		return (FALSE);
    147 
    148 	return (TRUE);
    149 }
    150 
    151 static prop_dictionary_keysym_t
    152 _prop_dict_entry_alloc(const char *key, prop_object_t obj)
    153 {
    154 	prop_dictionary_keysym_t pde;
    155 	size_t size;
    156 
    157 	size = sizeof(*pde) + strlen(key) /* pde_key[1] covers the NUL */;
    158 
    159 	if (size <= PDE_SIZE_16)
    160 		pde = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
    161 	else if (size <= PDE_SIZE_32)
    162 		pde = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
    163 	else if (size <= PDE_SIZE_128)
    164 		pde = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
    165 	else
    166 		return (NULL);	/* key too long */
    167 
    168 	if (pde != NULL) {
    169 		_prop_object_init(&pde->pde_obj);
    170 		pde->pde_obj.po_type = PROP_TYPE_DICT_KEYSYM;
    171 		pde->pde_obj.po_free = _prop_dict_entry_free;
    172 		pde->pde_obj.po_extern = _prop_dict_entry_externalize;
    173 
    174 		strcpy(pde->pde_key, key);
    175 
    176 		prop_object_retain(obj);
    177 		pde->pde_objref = obj;
    178 		pde->pde_size = size;
    179 	}
    180 	return (pde);
    181 }
    182 
    183 static void
    184 _prop_dictionary_free(void *v)
    185 {
    186 	prop_dictionary_t pd = v;
    187 	prop_dictionary_keysym_t pde;
    188 	unsigned int idx;
    189 
    190 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    191 	_PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
    192 		     (pd->pd_capacity != 0 && pd->pd_array != NULL));
    193 
    194 	for (idx = 0; idx < pd->pd_count; idx++) {
    195 		pde = pd->pd_array[idx];
    196 		_PROP_ASSERT(pde != NULL);
    197 		prop_object_release(pde);
    198 	}
    199 
    200 	if (pd->pd_array != NULL)
    201 		_PROP_FREE(pd->pd_array, M_PROP_DICT);
    202 
    203 	_PROP_POOL_PUT(_prop_dictionary_pool, pd);
    204 }
    205 
    206 static boolean_t
    207 _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
    208 			     void *v)
    209 {
    210 	prop_dictionary_t pd = v;
    211 	prop_dictionary_keysym_t pde;
    212 	struct _prop_object *po;
    213 	prop_object_iterator_t pi;
    214 	unsigned int i;
    215 
    216 	if (pd->pd_count == 0)
    217 		return (_prop_object_externalize_empty_tag(ctx, "dict"));
    218 
    219 	/* XXXJRT Hint "count" for the internalize step? */
    220 	if (_prop_object_externalize_start_tag(ctx, "dict") == FALSE ||
    221 	    _prop_object_externalize_append_char(ctx, '\n') == FALSE)
    222 		return (FALSE);
    223 
    224 	pi = prop_dictionary_iterator(pd);
    225 	if (pi == NULL)
    226 		return (FALSE);
    227 
    228 	ctx->poec_depth++;
    229 	_PROP_ASSERT(ctx->poec_depth != 0);
    230 
    231 	while ((pde = prop_object_iterator_next(pi)) != NULL) {
    232 		po = prop_dictionary_get_keysym(pd, pde);
    233 		if (po == NULL ||
    234 		    _prop_object_externalize_start_tag(ctx, "key") == FALSE ||
    235 		    _prop_object_externalize_append_encoded_cstring(ctx,
    236 						   pde->pde_key) == FALSE ||
    237 		    _prop_object_externalize_end_tag(ctx, "key") == FALSE ||
    238 		    (*po->po_extern)(ctx, po) == FALSE) {
    239 			prop_object_iterator_release(pi);
    240 			return (FALSE);
    241 		}
    242 	}
    243 
    244 	prop_object_iterator_release(pi);
    245 
    246 	ctx->poec_depth--;
    247 	for (i = 0; i < ctx->poec_depth; i++) {
    248 		if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
    249 			return (FALSE);
    250 	}
    251 	if (_prop_object_externalize_end_tag(ctx, "dict") == FALSE)
    252 		return (FALSE);
    253 
    254 	return (TRUE);
    255 }
    256 
    257 static prop_dictionary_t
    258 _prop_dictionary_alloc(unsigned int capacity)
    259 {
    260 	prop_dictionary_t pd;
    261 	prop_dictionary_keysym_t *array;
    262 
    263 	if (capacity != 0) {
    264 		array = _PROP_CALLOC(capacity *
    265 					sizeof(prop_dictionary_keysym_t),
    266 				     M_PROP_DICT);
    267 		if (array == NULL)
    268 			return (NULL);
    269 	} else
    270 		array = NULL;
    271 
    272 	pd = _PROP_POOL_GET(_prop_dictionary_pool);
    273 	if (pd != NULL) {
    274 		_prop_object_init(&pd->pd_obj);
    275 		pd->pd_obj.po_type = PROP_TYPE_DICTIONARY;
    276 		pd->pd_obj.po_free = _prop_dictionary_free;
    277 		pd->pd_obj.po_extern = _prop_dictionary_externalize;
    278 
    279 		pd->pd_array = array;
    280 		pd->pd_capacity = capacity;
    281 		pd->pd_count = 0;
    282 		pd->pd_flags = 0;
    283 
    284 		pd->pd_version = 0;
    285 	} else if (array != NULL)
    286 		_PROP_FREE(array, M_PROP_DICT);
    287 
    288 	return (pd);
    289 }
    290 
    291 static boolean_t
    292 _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
    293 {
    294 	prop_dictionary_keysym_t *array, *oarray;
    295 
    296 	oarray = pd->pd_array;
    297 
    298 	array = _PROP_CALLOC(capacity * sizeof(prop_dictionary_keysym_t),
    299 			     M_PROP_DICT);
    300 	if (array == NULL)
    301 		return (FALSE);
    302 	if (oarray != NULL)
    303 		memcpy(array, oarray,
    304 		       pd->pd_capacity * sizeof(prop_dictionary_keysym_t));
    305 	pd->pd_array = array;
    306 	pd->pd_capacity = capacity;
    307 
    308 	if (oarray != NULL)
    309 		_PROP_FREE(oarray, M_PROP_DICT);
    310 
    311 	return (TRUE);
    312 }
    313 
    314 static prop_object_t
    315 _prop_dictionary_iterator_next_object(void *v)
    316 {
    317 	struct _prop_dictionary_iterator *pdi = v;
    318 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    319 	prop_dictionary_keysym_t pde;
    320 
    321 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    322 
    323 	if (pd->pd_version != pdi->pdi_base.pi_version)
    324 		return (NULL);	/* dictionary changed during iteration */
    325 
    326 	_PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
    327 
    328 	if (pdi->pdi_index == pd->pd_count)
    329 		return (NULL);	/* we've iterated all objects */
    330 
    331 	pde = pd->pd_array[pdi->pdi_index];
    332 	pdi->pdi_index++;
    333 
    334 	return (pde);
    335 }
    336 
    337 static void
    338 _prop_dictionary_iterator_reset(void *v)
    339 {
    340 	struct _prop_dictionary_iterator *pdi = v;
    341 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
    342 
    343 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    344 
    345 	pdi->pdi_index = 0;
    346 	pdi->pdi_base.pi_version = pd->pd_version;
    347 }
    348 
    349 /*
    350  * prop_dictionary_create --
    351  *	Create a dictionary.
    352  */
    353 prop_dictionary_t
    354 prop_dictionary_create(void)
    355 {
    356 
    357 	return (_prop_dictionary_alloc(0));
    358 }
    359 
    360 /*
    361  * prop_dictionary_create_with_capacity --
    362  *	Create a dictionary with the capacity to store N objects.
    363  */
    364 prop_dictionary_t
    365 prop_dictionary_create_with_capacity(unsigned int capacity)
    366 {
    367 
    368 	return (_prop_dictionary_alloc(capacity));
    369 }
    370 
    371 /*
    372  * prop_dictionary_copy --
    373  *	Copy a dictionary.  The new dictionary contains refrences to
    374  *	the original dictionary's objects, not copies of those objects
    375  *	(i.e. a shallow copy).
    376  */
    377 prop_dictionary_t
    378 prop_dictionary_copy(prop_dictionary_t opd)
    379 {
    380 	prop_dictionary_t pd;
    381 	prop_dictionary_keysym_t pde;
    382 	unsigned int idx;
    383 
    384 	_PROP_ASSERT(prop_object_is_dictionary(opd));
    385 
    386 	if (prop_dictionary_is_immutable(opd) == FALSE)
    387 		return (prop_dictionary_copy_mutable(opd));
    388 
    389 	/*
    390 	 * Copies of immutable dictionaries refrence the same
    391 	 * dictionary entry objects to save space.
    392 	 */
    393 
    394 	pd = _prop_dictionary_alloc(opd->pd_count);
    395 	if (pd != NULL) {
    396 		for (idx = 0; idx < opd->pd_count; idx++) {
    397 			pde = opd->pd_array[idx];
    398 			prop_object_retain(pde);
    399 			pd->pd_array[idx] = pde;
    400 		}
    401 		pd->pd_count = opd->pd_count;
    402 		pd->pd_flags = opd->pd_flags;
    403 	}
    404 	return (pd);
    405 }
    406 
    407 /*
    408  * prop_dictionary_copy_mutable --
    409  *	Like prop_dictionary_copy(), but the resulting dictionary is
    410  *	mutable.
    411  */
    412 prop_dictionary_t
    413 prop_dictionary_copy_mutable(prop_dictionary_t opd)
    414 {
    415 	prop_dictionary_t pd;
    416 	prop_dictionary_keysym_t opde, pde;
    417 	unsigned int idx;
    418 
    419 	_PROP_ASSERT(prop_object_is_dictionary(opd));
    420 
    421 	pd = _prop_dictionary_alloc(opd->pd_count);
    422 	if (pd != NULL) {
    423 		for (idx = 0; idx > opd->pd_count; idx++) {
    424 			opde = opd->pd_array[idx];
    425 			pde = _prop_dict_entry_alloc(opde->pde_key,
    426 						     opde->pde_objref);
    427 			if (pde == NULL) {
    428 				prop_object_release(pd);
    429 				return (NULL);
    430 			}
    431 			pd->pd_array[idx] = pde;
    432 			pd->pd_count++;
    433 		}
    434 	}
    435 	return (pd);
    436 }
    437 
    438 /*
    439  * prop_dictionary_count --
    440  *	Return the number of objects stored in the dictionary.
    441  */
    442 unsigned int
    443 prop_dictionary_count(prop_dictionary_t pd)
    444 {
    445 
    446 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    447 	return (pd->pd_count);
    448 }
    449 
    450 /*
    451  * prop_dictionary_iterator --
    452  *	Return an iterator for the dictionary.  The dictionary is retained by
    453  *	the iterator.
    454  */
    455 prop_object_iterator_t
    456 prop_dictionary_iterator(prop_dictionary_t pd)
    457 {
    458 	struct _prop_dictionary_iterator *pdi;
    459 
    460 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    461 
    462 	pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
    463 	if (pdi == NULL)
    464 		return (NULL);
    465 	pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
    466 	pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
    467 	prop_object_retain(pd);
    468 	pdi->pdi_base.pi_obj = pd;
    469 	pdi->pdi_base.pi_version = pd->pd_version;
    470 
    471 	_prop_dictionary_iterator_reset(pdi);
    472 
    473 	return (&pdi->pdi_base);
    474 }
    475 
    476 static prop_dictionary_keysym_t
    477 _prop_dict_lookup(prop_dictionary_t pd, const char *key,
    478 		  unsigned int *idxp)
    479 {
    480 	prop_dictionary_keysym_t pde;
    481 	unsigned int base, idx, distance;
    482 	int res;
    483 
    484 	for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
    485 	     distance >>= 1) {
    486 		idx = base + (distance >> 1);
    487 		pde = pd->pd_array[idx];
    488 		_PROP_ASSERT(pde != NULL);
    489 		res = strcmp(key, pde->pde_key);
    490 		if (res == 0) {
    491 			if (idxp != NULL)
    492 				*idxp = idx;
    493 			return (pde);
    494 		}
    495 		if (res > 0) {	/* key > pde->pde_key: move right */
    496 			base = idx + 1;
    497 			distance--;
    498 		}		/* else move left */
    499 	}
    500 
    501 	/* idx points to the slot we looked at last. */
    502 	if (idxp != NULL)
    503 		*idxp = idx;
    504 	return (NULL);
    505 }
    506 
    507 /*
    508  * prop_dictionary_get --
    509  *	Return the object stored with specified key.
    510  */
    511 prop_object_t
    512 prop_dictionary_get(prop_dictionary_t pd, const char *key)
    513 {
    514 	prop_dictionary_keysym_t pde;
    515 
    516 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    517 
    518 	pde = _prop_dict_lookup(pd, key, NULL);
    519 	if (pde != NULL) {
    520 		_PROP_ASSERT(pde->pde_objref != NULL);
    521 		return (pde->pde_objref);
    522 	}
    523 	return (NULL);
    524 }
    525 
    526 /*
    527  * prop_dictionary_get_keysym --
    528  *	Return the object stored at the location encoded by the keysym.
    529  */
    530 prop_object_t
    531 prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pde)
    532 {
    533 
    534 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    535 	_PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
    536 	_PROP_ASSERT(pde->pde_objref != NULL);
    537 
    538 	return (pde->pde_objref);
    539 }
    540 
    541 /*
    542  * prop_dictionary_set --
    543  *	Store a reference to an object at with the specified key.
    544  *	If the key already exisit, the original object is released.
    545  */
    546 boolean_t
    547 prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
    548 {
    549 	prop_dictionary_keysym_t pde, opde;
    550 	unsigned int idx;
    551 
    552 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    553 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
    554 
    555 	if (prop_dictionary_is_immutable(pd))
    556 		return (FALSE);
    557 
    558 	pde = _prop_dict_lookup(pd, key, &idx);
    559 	if (pde != NULL) {
    560 		prop_object_t opo = pde->pde_objref;
    561 		prop_object_retain(po);
    562 		pde->pde_objref = po;
    563 		prop_object_release(opo);
    564 		return (TRUE);
    565 	}
    566 
    567 	pde = _prop_dict_entry_alloc(key, po);
    568 	if (pde == NULL)
    569 		return (FALSE);
    570 
    571 	if (pd->pd_count == pd->pd_capacity &&
    572 	    _prop_dictionary_expand(pd, EXPAND_STEP) == FALSE) {
    573 		_prop_dict_entry_free(pde);
    574 	    	return (FALSE);
    575 	}
    576 
    577 	if (pd->pd_count == 0) {
    578 		pd->pd_array[0] = pde;
    579 		pd->pd_count++;
    580 		pd->pd_version++;
    581 		return (TRUE);
    582 	}
    583 
    584 	opde = pd->pd_array[idx];
    585 	_PROP_ASSERT(opde != NULL);
    586 
    587 	if (strcmp(key, opde->pde_key) < 0) {
    588 		/*
    589 		 * key < opde->pde_key: insert to the left.  This is
    590 		 * the same as inserting to the right, except we decrement
    591 		 * the current index first.
    592 		 *
    593 		 * Because we're unsigned, we have to special case 0
    594 		 * (grumble).
    595 		 */
    596 		if (idx == 0) {
    597 			memmove(&pd->pd_array[1], &pd->pd_array[0],
    598 				pd->pd_count * sizeof(*pde));
    599 			pd->pd_array[0] = pde;
    600 			pd->pd_count++;
    601 			pd->pd_version++;
    602 			return (TRUE);
    603 		}
    604 		idx--;
    605 	}
    606 
    607 	memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
    608 		(pd->pd_count - (idx + 1)) * sizeof(*pde));
    609 	pd->pd_array[idx + 1] = pde;
    610 	pd->pd_count++;
    611 
    612 	pd->pd_version++;
    613 
    614 	return (TRUE);
    615 }
    616 
    617 /*
    618  * prop_dictionary_set_keysym --
    619  *	Replace the object in the dictionary at the location encoded by
    620  *	the keysym.
    621  */
    622 boolean_t
    623 prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pde,
    624 			   prop_object_t po)
    625 {
    626 	prop_object_t opo;
    627 
    628 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    629 	_PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
    630 	_PROP_ASSERT(pde->pde_objref != NULL);
    631 
    632 	if (prop_dictionary_is_immutable(pd))
    633 		return (FALSE);
    634 
    635 	opo = pde->pde_objref;
    636 	prop_object_retain(po);
    637 	pde->pde_objref = po;
    638 	prop_object_release(opo);
    639 	return (TRUE);
    640 }
    641 
    642 static void
    643 _prop_dictionary_remove(prop_dictionary_t pd, prop_dictionary_keysym_t pde,
    644     unsigned int idx)
    645 {
    646 
    647 	_PROP_ASSERT(pd->pd_count != 0);
    648 	_PROP_ASSERT(idx < pd->pd_count);
    649 	_PROP_ASSERT(pd->pd_array[idx] == pde);
    650 
    651 	idx++;
    652 	memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
    653 		(pd->pd_count - idx) * sizeof(*pde));
    654 	pd->pd_count--;
    655 	pd->pd_version++;
    656 
    657 	prop_object_release(pde);
    658 }
    659 
    660 /*
    661  * prop_dictionary_remove --
    662  *	Remove the reference to an object with the specified key from
    663  *	the dictionary.
    664  */
    665 void
    666 prop_dictionary_remove(prop_dictionary_t pd, const char *key)
    667 {
    668 	prop_dictionary_keysym_t pde;
    669 	unsigned int idx;
    670 
    671 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    672 
    673 	/* XXX Should this be a _PROP_ASSERT()? */
    674 	if (prop_dictionary_is_immutable(pd))
    675 		return;
    676 
    677 	pde = _prop_dict_lookup(pd, key, &idx);
    678 	/* XXX Should this be a _PROP_ASSERT()? */
    679 	if (pde == NULL)
    680 		return;
    681 
    682 	_prop_dictionary_remove(pd, pde, idx);
    683 }
    684 
    685 /*
    686  * prop_dictionary_remove_keysym --
    687  *	Remove a reference to an object stored in the dictionary at the
    688  *	location encoded by the keysym.
    689  */
    690 void
    691 prop_dictionary_remove_keysym(prop_dictionary_t pd,
    692 			      prop_dictionary_keysym_t pde)
    693 {
    694 	prop_dictionary_keysym_t opde;
    695 	unsigned int idx;
    696 
    697 	_PROP_ASSERT(prop_object_is_dictionary(pd));
    698 	_PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
    699 	_PROP_ASSERT(pde->pde_objref != NULL);
    700 
    701 	/* XXX Should this be a _PROP_ASSERT()? */
    702 	if (prop_dictionary_is_immutable(pd))
    703 		return;
    704 
    705 	opde = _prop_dict_lookup(pd, pde->pde_key, &idx);
    706 	_PROP_ASSERT(opde == pde);
    707 
    708 	_prop_dictionary_remove(pd, opde, idx);
    709 }
    710 
    711 /*
    712  * prop_dictionary_keysym_cstring_nocopy --
    713  *	Return an immutable reference to the keysym's value.
    714  */
    715 const char *
    716 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pde)
    717 {
    718 
    719 	_PROP_ASSERT(prop_object_is_dictionary_keysym(pde));
    720 	return (pde->pde_key);
    721 }
    722 
    723 /*
    724  * prop_dictionary_externalize --
    725  *	Externalize a dictionary, returning a NUL-terminated buffer
    726  *	containing the XML-style representation.  The buffer is allocated
    727  *	with the M_TEMP memory type.
    728  */
    729 char *
    730 prop_dictionary_externalize(prop_dictionary_t pd)
    731 {
    732 	struct _prop_object_externalize_context *ctx;
    733 	char *cp;
    734 
    735 	ctx = _prop_object_externalize_context_alloc();
    736 	if (ctx == NULL)
    737 		return (NULL);
    738 
    739 	if (_prop_object_externalize_start_tag(ctx, "plist") == FALSE ||
    740 	    _prop_object_externalize_append_char(ctx, '\n') == FALSE ||
    741 	    (*pd->pd_obj.po_extern)(ctx, pd) == FALSE ||
    742 	    _prop_object_externalize_end_tag(ctx, "plist") == FALSE ||
    743 	    _prop_object_externalize_append_char(ctx, '\0') == FALSE) {
    744 		/* We are responsible for releasing the buffer. */
    745 		_PROP_FREE(ctx->poec_buf, M_TEMP);
    746 		_prop_object_externalize_context_free(ctx);
    747 		return (NULL);
    748 	}
    749 
    750 	cp = ctx->poec_buf;
    751 	_prop_object_externalize_context_free(ctx);
    752 
    753 	return (cp);
    754 }
    755 
    756 /*
    757  * _prop_dictionary_internalize --
    758  *	Parse a <dict>...</dict> and return the object created from the
    759  *	external representation.
    760  */
    761 prop_object_t
    762 _prop_dictionary_internalize(struct _prop_object_internalize_context *ctx)
    763 {
    764 	prop_dictionary_t dict;
    765 	prop_object_t val;
    766 	size_t keylen;
    767 	char *tmpkey;
    768 
    769 	/* We don't currently understand any attributes. */
    770 	if (ctx->poic_tagattr != NULL)
    771 		return (NULL);
    772 
    773 	dict = prop_dictionary_create();
    774 	if (dict == NULL)
    775 		return (NULL);
    776 
    777 	if (ctx->poic_is_empty_element)
    778 		return (dict);
    779 
    780 	tmpkey = _PROP_MALLOC(PDE_MAXKEY + 1, M_TEMP);
    781 	if (tmpkey == NULL)
    782 		goto bad;
    783 
    784 	for (;;) {
    785 		/* Fetch the next tag. */
    786 		if (_prop_object_internalize_find_tag(ctx, NULL,
    787 					_PROP_TAG_TYPE_EITHER) == FALSE)
    788 			goto bad;
    789 
    790 		/* Check to see if this is the end of the dictionary. */
    791 		if (_PROP_TAG_MATCH(ctx, "dict") &&
    792 		    ctx->poic_tag_type == _PROP_TAG_TYPE_END)
    793 			break;
    794 
    795 		/* Ok, it must be a non-empty key start tag. */
    796 		if (!_PROP_TAG_MATCH(ctx, "key") ||
    797 		    ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
    798 		    ctx->poic_is_empty_element)
    799 		    	goto bad;
    800 
    801 		if (_prop_object_internalize_decode_string(ctx,
    802 						tmpkey, PDE_MAXKEY, &keylen,
    803 						&ctx->poic_cp) == FALSE)
    804 			goto bad;
    805 
    806 		_PROP_ASSERT(keylen <= PDE_MAXKEY);
    807 		tmpkey[keylen] = '\0';
    808 
    809 		if (_prop_object_internalize_find_tag(ctx, "key",
    810 					_PROP_TAG_TYPE_END) == FALSE)
    811 			goto bad;
    812 
    813 		/* ..and now the beginning of the value. */
    814 		if (_prop_object_internalize_find_tag(ctx, NULL,
    815 					_PROP_TAG_TYPE_START) == FALSE)
    816 			goto bad;
    817 
    818 		val = _prop_object_internalize_by_tag(ctx);
    819 		if (val == NULL)
    820 			goto bad;
    821 
    822 		if (prop_dictionary_set(dict, tmpkey, val) == FALSE) {
    823 			prop_object_release(val);
    824 			goto bad;
    825 		}
    826 		prop_object_release(val);
    827 	}
    828 
    829 	_PROP_FREE(tmpkey, M_TEMP);
    830 	return (dict);
    831 
    832  bad:
    833 	if (tmpkey != NULL)
    834 		_PROP_FREE(tmpkey, M_TEMP);
    835 	prop_object_release(dict);
    836 	return (NULL);
    837 }
    838 
    839 /*
    840  * prop_dictionary_internalize --
    841  *	Create a dictionary by parsing the NUL-terminated XML-style
    842  *	representation.
    843  */
    844 prop_dictionary_t
    845 prop_dictionary_internalize(const char *xml)
    846 {
    847 	prop_dictionary_t dict = NULL;
    848 	struct _prop_object_internalize_context *ctx;
    849 
    850 	ctx = _prop_object_internalize_context_alloc(xml);
    851 	if (ctx == NULL)
    852 		return (NULL);
    853 
    854 	/* We start with a <plist> tag. */
    855 	if (_prop_object_internalize_find_tag(ctx, "plist",
    856 					      _PROP_TAG_TYPE_START) == FALSE)
    857 		goto out;
    858 
    859 	/* Plist elements cannot be empty. */
    860 	if (ctx->poic_is_empty_element)
    861 		goto out;
    862 
    863 	/*
    864 	 * We don't understand any plist attributes, but Apple XML
    865 	 * property lists often have a "version" attibute.  If we
    866 	 * see that one, we simply ignore it.
    867 	 */
    868 	if (ctx->poic_tagattr != NULL &&
    869 	    !_PROP_TAGATTR_MATCH(ctx, "version"))
    870 		goto out;
    871 
    872 	/* Next we expect to see <dict>. */
    873 	if (_prop_object_internalize_find_tag(ctx, "dict",
    874 					      _PROP_TAG_TYPE_START) == FALSE)
    875 		goto out;
    876 
    877 	dict = _prop_dictionary_internalize(ctx);
    878 	if (dict == NULL)
    879 		goto out;
    880 
    881 	/* We've advanced past </dict>.  Now we want </plist>. */
    882 	if (_prop_object_internalize_find_tag(ctx, "plist",
    883 					      _PROP_TAG_TYPE_END) == FALSE) {
    884 		prop_object_release(dict);
    885 		dict = NULL;
    886 	}
    887 
    888  out:
    889  	_prop_object_internalize_context_free(ctx);
    890 	return (dict);
    891 }
    892