xref: /minix/common/lib/libprop/prop_dictionary.c (revision 0a6a1f1d)
1 /*	$NetBSD: prop_dictionary.c,v 1.39 2013/10/18 18:26:20 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006, 2007 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "prop_object_impl.h"
33 #include <prop/prop_array.h>
34 #include <prop/prop_dictionary.h>
35 #include <prop/prop_string.h>
36 #include "prop_rb_impl.h"
37 
38 #if !defined(_KERNEL) && !defined(_STANDALONE)
39 #include <errno.h>
40 #endif
41 
42 /*
43  * We implement these like arrays, but we keep them sorted by key.
44  * This allows us to binary-search as well as keep externalized output
45  * sane-looking for human eyes.
46  */
47 
48 #define	EXPAND_STEP		16
49 
50 /*
51  * prop_dictionary_keysym_t is allocated with space at the end to hold the
52  * key.  This must be a regular object so that we can maintain sane iterator
53  * semantics -- we don't want to require that the caller release the result
54  * of prop_object_iterator_next().
55  *
56  * We'd like to have some small'ish keysym objects for up-to-16 characters
57  * in a key, some for up-to-32 characters in a key, and then a final bucket
58  * for up-to-128 characters in a key (not including NUL).  Keys longer than
59  * 128 characters are not allowed.
60  */
61 struct _prop_dictionary_keysym {
62 	struct _prop_object		pdk_obj;
63 	size_t				pdk_size;
64 	struct rb_node			pdk_link;
65 	char 				pdk_key[1];
66 	/* actually variable length */
67 };
68 
69 	/* pdk_key[1] takes care of the NUL */
70 #define	PDK_SIZE_16		(sizeof(struct _prop_dictionary_keysym) + 16)
71 #define	PDK_SIZE_32		(sizeof(struct _prop_dictionary_keysym) + 32)
72 #define	PDK_SIZE_128		(sizeof(struct _prop_dictionary_keysym) + 128)
73 
74 #define	PDK_MAXKEY		128
75 
76 _PROP_POOL_INIT(_prop_dictionary_keysym16_pool, PDK_SIZE_16, "pdict16")
77 _PROP_POOL_INIT(_prop_dictionary_keysym32_pool, PDK_SIZE_32, "pdict32")
78 _PROP_POOL_INIT(_prop_dictionary_keysym128_pool, PDK_SIZE_128, "pdict128")
79 
80 struct _prop_dict_entry {
81 	prop_dictionary_keysym_t	pde_key;
82 	prop_object_t			pde_objref;
83 };
84 
85 struct _prop_dictionary {
86 	struct _prop_object	pd_obj;
87 	_PROP_RWLOCK_DECL(pd_rwlock)
88 	struct _prop_dict_entry	*pd_array;
89 	unsigned int		pd_capacity;
90 	unsigned int		pd_count;
91 	int			pd_flags;
92 
93 	uint32_t		pd_version;
94 };
95 
96 #define	PD_F_IMMUTABLE		0x01	/* dictionary is immutable */
97 
98 _PROP_POOL_INIT(_prop_dictionary_pool, sizeof(struct _prop_dictionary),
99 		"propdict")
100 _PROP_MALLOC_DEFINE(M_PROP_DICT, "prop dictionary",
101 		    "property dictionary container object")
102 
103 static _prop_object_free_rv_t
104 		_prop_dictionary_free(prop_stack_t, prop_object_t *);
105 static void	_prop_dictionary_emergency_free(prop_object_t);
106 static bool	_prop_dictionary_externalize(
107 				struct _prop_object_externalize_context *,
108 				void *);
109 static _prop_object_equals_rv_t
110 		_prop_dictionary_equals(prop_object_t, prop_object_t,
111 				        void **, void **,
112 					prop_object_t *, prop_object_t *);
113 static void	_prop_dictionary_equals_finish(prop_object_t, prop_object_t);
114 static prop_object_iterator_t
115 		_prop_dictionary_iterator_locked(prop_dictionary_t);
116 static prop_object_t
117 		_prop_dictionary_iterator_next_object_locked(void *);
118 static prop_object_t
119 		_prop_dictionary_get_keysym(prop_dictionary_t,
120 					    prop_dictionary_keysym_t, bool);
121 static prop_object_t
122 		_prop_dictionary_get(prop_dictionary_t, const char *, bool);
123 
124 static void _prop_dictionary_lock(void);
125 static void _prop_dictionary_unlock(void);
126 
127 static const struct _prop_object_type _prop_object_type_dictionary = {
128 	.pot_type		=	PROP_TYPE_DICTIONARY,
129 	.pot_free		=	_prop_dictionary_free,
130 	.pot_emergency_free	=	_prop_dictionary_emergency_free,
131 	.pot_extern		=	_prop_dictionary_externalize,
132 	.pot_equals		=	_prop_dictionary_equals,
133 	.pot_equals_finish	=	_prop_dictionary_equals_finish,
134 	.pot_lock 	        =       _prop_dictionary_lock,
135 	.pot_unlock 	        =       _prop_dictionary_unlock,
136 };
137 
138 static _prop_object_free_rv_t
139 		_prop_dict_keysym_free(prop_stack_t, prop_object_t *);
140 static bool	_prop_dict_keysym_externalize(
141 				struct _prop_object_externalize_context *,
142 				void *);
143 static _prop_object_equals_rv_t
144 		_prop_dict_keysym_equals(prop_object_t, prop_object_t,
145 					 void **, void **,
146 					 prop_object_t *, prop_object_t *);
147 
148 static const struct _prop_object_type _prop_object_type_dict_keysym = {
149 	.pot_type	=	PROP_TYPE_DICT_KEYSYM,
150 	.pot_free	=	_prop_dict_keysym_free,
151 	.pot_extern	=	_prop_dict_keysym_externalize,
152 	.pot_equals	=	_prop_dict_keysym_equals,
153 };
154 
155 #define	prop_object_is_dictionary(x)		\
156 	((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_dictionary)
157 #define	prop_object_is_dictionary_keysym(x)	\
158 	((x) != NULL && (x)->pdk_obj.po_type == &_prop_object_type_dict_keysym)
159 
160 #define	prop_dictionary_is_immutable(x)		\
161 				(((x)->pd_flags & PD_F_IMMUTABLE) != 0)
162 
163 struct _prop_dictionary_iterator {
164 	struct _prop_object_iterator pdi_base;
165 	unsigned int		pdi_index;
166 };
167 
168 /*
169  * Dictionary key symbols are immutable, and we are likely to have many
170  * duplicated key symbols.  So, to save memory, we unique'ify key symbols
171  * so we only have to have one copy of each string.
172  */
173 
174 static int
175 /*ARGSUSED*/
_prop_dict_keysym_rb_compare_nodes(void * ctx _PROP_ARG_UNUSED,const void * n1,const void * n2)176 _prop_dict_keysym_rb_compare_nodes(void *ctx _PROP_ARG_UNUSED,
177 				   const void *n1, const void *n2)
178 {
179 	const struct _prop_dictionary_keysym *pdk1 = n1;
180 	const struct _prop_dictionary_keysym *pdk2 = n2;
181 
182 	return strcmp(pdk1->pdk_key, pdk2->pdk_key);
183 }
184 
185 static int
186 /*ARGSUSED*/
_prop_dict_keysym_rb_compare_key(void * ctx _PROP_ARG_UNUSED,const void * n,const void * v)187 _prop_dict_keysym_rb_compare_key(void *ctx _PROP_ARG_UNUSED,
188 				 const void *n, const void *v)
189 {
190 	const struct _prop_dictionary_keysym *pdk = n;
191 	const char *cp = v;
192 
193 	return strcmp(pdk->pdk_key, cp);
194 }
195 
196 static const rb_tree_ops_t _prop_dict_keysym_rb_tree_ops = {
197 	.rbto_compare_nodes = _prop_dict_keysym_rb_compare_nodes,
198 	.rbto_compare_key = _prop_dict_keysym_rb_compare_key,
199 	.rbto_node_offset = offsetof(struct _prop_dictionary_keysym, pdk_link),
200 	.rbto_context = NULL
201 };
202 
203 static struct rb_tree _prop_dict_keysym_tree;
204 
205 _PROP_ONCE_DECL(_prop_dict_init_once)
_PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex)206 _PROP_MUTEX_DECL_STATIC(_prop_dict_keysym_tree_mutex)
207 
208 static int
209 _prop_dict_init(void)
210 {
211 
212 	_PROP_MUTEX_INIT(_prop_dict_keysym_tree_mutex);
213 	_prop_rb_tree_init(&_prop_dict_keysym_tree,
214 			   &_prop_dict_keysym_rb_tree_ops);
215 	return 0;
216 }
217 
218 static void
_prop_dict_keysym_put(prop_dictionary_keysym_t pdk)219 _prop_dict_keysym_put(prop_dictionary_keysym_t pdk)
220 {
221 
222 	if (pdk->pdk_size <= PDK_SIZE_16)
223 		_PROP_POOL_PUT(_prop_dictionary_keysym16_pool, pdk);
224 	else if (pdk->pdk_size <= PDK_SIZE_32)
225 		_PROP_POOL_PUT(_prop_dictionary_keysym32_pool, pdk);
226 	else {
227 		_PROP_ASSERT(pdk->pdk_size <= PDK_SIZE_128);
228 		_PROP_POOL_PUT(_prop_dictionary_keysym128_pool, pdk);
229 	}
230 }
231 
232 /* ARGSUSED */
233 static _prop_object_free_rv_t
_prop_dict_keysym_free(prop_stack_t stack,prop_object_t * obj)234 _prop_dict_keysym_free(prop_stack_t stack, prop_object_t *obj)
235 {
236 	prop_dictionary_keysym_t pdk = *obj;
237 
238 	_prop_rb_tree_remove_node(&_prop_dict_keysym_tree, pdk);
239 	_prop_dict_keysym_put(pdk);
240 
241 	return _PROP_OBJECT_FREE_DONE;
242 }
243 
244 static bool
_prop_dict_keysym_externalize(struct _prop_object_externalize_context * ctx,void * v)245 _prop_dict_keysym_externalize(struct _prop_object_externalize_context *ctx,
246 			     void *v)
247 {
248 	prop_dictionary_keysym_t pdk = v;
249 
250 	/* We externalize these as strings, and they're never empty. */
251 
252 	_PROP_ASSERT(pdk->pdk_key[0] != '\0');
253 
254 	if (_prop_object_externalize_start_tag(ctx, "string") == false ||
255 	    _prop_object_externalize_append_encoded_cstring(ctx,
256 						pdk->pdk_key) == false ||
257 	    _prop_object_externalize_end_tag(ctx, "string") == false)
258 		return (false);
259 
260 	return (true);
261 }
262 
263 /* ARGSUSED */
264 static _prop_object_equals_rv_t
_prop_dict_keysym_equals(prop_object_t v1,prop_object_t v2,void ** stored_pointer1,void ** stored_pointer2,prop_object_t * next_obj1,prop_object_t * next_obj2)265 _prop_dict_keysym_equals(prop_object_t v1, prop_object_t v2,
266     void **stored_pointer1, void **stored_pointer2,
267     prop_object_t *next_obj1, prop_object_t *next_obj2)
268 {
269 	prop_dictionary_keysym_t pdk1 = v1;
270 	prop_dictionary_keysym_t pdk2 = v2;
271 
272 	/*
273 	 * There is only ever one copy of a keysym at any given time,
274 	 * so we can reduce this to a simple pointer equality check.
275 	 */
276 	if (pdk1 == pdk2)
277 		return _PROP_OBJECT_EQUALS_TRUE;
278 	else
279 		return _PROP_OBJECT_EQUALS_FALSE;
280 }
281 
282 static prop_dictionary_keysym_t
_prop_dict_keysym_alloc(const char * key)283 _prop_dict_keysym_alloc(const char *key)
284 {
285 	prop_dictionary_keysym_t opdk, pdk, rpdk;
286 	size_t size;
287 
288 	_PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init);
289 
290 	/*
291 	 * Check to see if this already exists in the tree.  If it does,
292 	 * we just retain it and return it.
293 	 */
294 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
295 	opdk = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
296 	if (opdk != NULL) {
297 		prop_object_retain(opdk);
298 		_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
299 		return (opdk);
300 	}
301 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
302 
303 	/*
304 	 * Not in the tree.  Create it now.
305 	 */
306 
307 	size = sizeof(*pdk) + strlen(key) /* pdk_key[1] covers the NUL */;
308 
309 	if (size <= PDK_SIZE_16)
310 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym16_pool);
311 	else if (size <= PDK_SIZE_32)
312 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym32_pool);
313 	else if (size <= PDK_SIZE_128)
314 		pdk = _PROP_POOL_GET(_prop_dictionary_keysym128_pool);
315 	else
316 		pdk = NULL;	/* key too long */
317 
318 	if (pdk == NULL)
319 		return (NULL);
320 
321 	_prop_object_init(&pdk->pdk_obj, &_prop_object_type_dict_keysym);
322 
323 	strcpy(pdk->pdk_key, key);
324 	pdk->pdk_size = size;
325 
326 	/*
327 	 * We dropped the mutex when we allocated the new object, so
328 	 * we have to check again if it is in the tree.
329 	 */
330 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
331 	opdk = _prop_rb_tree_find(&_prop_dict_keysym_tree, key);
332 	if (opdk != NULL) {
333 		prop_object_retain(opdk);
334 		_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
335 		_prop_dict_keysym_put(pdk);
336 		return (opdk);
337 	}
338 	rpdk = _prop_rb_tree_insert_node(&_prop_dict_keysym_tree, pdk);
339 	_PROP_ASSERT(rpdk == pdk);
340 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
341 	return (rpdk);
342 }
343 
344 static _prop_object_free_rv_t
_prop_dictionary_free(prop_stack_t stack,prop_object_t * obj)345 _prop_dictionary_free(prop_stack_t stack, prop_object_t *obj)
346 {
347 	prop_dictionary_t pd = *obj;
348 	prop_dictionary_keysym_t pdk;
349 	prop_object_t po;
350 
351 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
352 	_PROP_ASSERT((pd->pd_capacity == 0 && pd->pd_array == NULL) ||
353 		     (pd->pd_capacity != 0 && pd->pd_array != NULL));
354 
355 	/* The empty dictorinary is easy, handle that first. */
356 	if (pd->pd_count == 0) {
357 		if (pd->pd_array != NULL)
358 			_PROP_FREE(pd->pd_array, M_PROP_DICT);
359 
360 		_PROP_RWLOCK_DESTROY(pd->pd_rwlock);
361 
362 		_PROP_POOL_PUT(_prop_dictionary_pool, pd);
363 
364 		return (_PROP_OBJECT_FREE_DONE);
365 	}
366 
367 	po = pd->pd_array[pd->pd_count - 1].pde_objref;
368 	_PROP_ASSERT(po != NULL);
369 
370 	if (stack == NULL) {
371 		/*
372 		 * If we are in emergency release mode,
373 		 * just let caller recurse down.
374 		 */
375 		*obj = po;
376 		return (_PROP_OBJECT_FREE_FAILED);
377 	}
378 
379 	/* Otherwise, try to push the current object on the stack. */
380 	if (!_prop_stack_push(stack, pd, NULL, NULL, NULL)) {
381 		/* Push failed, entering emergency release mode. */
382 		return (_PROP_OBJECT_FREE_FAILED);
383 	}
384 	/* Object pushed on stack, caller will release it. */
385 	--pd->pd_count;
386 	pdk = pd->pd_array[pd->pd_count].pde_key;
387 	_PROP_ASSERT(pdk != NULL);
388 
389 	prop_object_release(pdk);
390 
391 	*obj = po;
392 	return (_PROP_OBJECT_FREE_RECURSE);
393 }
394 
395 
396 static void
_prop_dictionary_lock(void)397 _prop_dictionary_lock(void)
398 {
399 
400 	/* XXX: once necessary or paranoia? */
401 	_PROP_ONCE_RUN(_prop_dict_init_once, _prop_dict_init);
402 	_PROP_MUTEX_LOCK(_prop_dict_keysym_tree_mutex);
403 }
404 
405 static void
_prop_dictionary_unlock(void)406 _prop_dictionary_unlock(void)
407 {
408 	_PROP_MUTEX_UNLOCK(_prop_dict_keysym_tree_mutex);
409 }
410 
411 static void
_prop_dictionary_emergency_free(prop_object_t obj)412 _prop_dictionary_emergency_free(prop_object_t obj)
413 {
414 	prop_dictionary_t pd = obj;
415 	prop_dictionary_keysym_t pdk;
416 
417 	_PROP_ASSERT(pd->pd_count != 0);
418 	--pd->pd_count;
419 
420 	pdk = pd->pd_array[pd->pd_count].pde_key;
421 	_PROP_ASSERT(pdk != NULL);
422 	prop_object_release(pdk);
423 }
424 
425 static bool
_prop_dictionary_externalize(struct _prop_object_externalize_context * ctx,void * v)426 _prop_dictionary_externalize(struct _prop_object_externalize_context *ctx,
427 			     void *v)
428 {
429 	prop_dictionary_t pd = v;
430 	prop_dictionary_keysym_t pdk;
431 	struct _prop_object *po;
432 	prop_object_iterator_t pi;
433 	unsigned int i;
434 	bool rv = false;
435 
436 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
437 
438 	if (pd->pd_count == 0) {
439 		_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
440 		return (_prop_object_externalize_empty_tag(ctx, "dict"));
441 	}
442 
443 	if (_prop_object_externalize_start_tag(ctx, "dict") == false ||
444 	    _prop_object_externalize_append_char(ctx, '\n') == false)
445 		goto out;
446 
447 	pi = _prop_dictionary_iterator_locked(pd);
448 	if (pi == NULL)
449 		goto out;
450 
451 	ctx->poec_depth++;
452 	_PROP_ASSERT(ctx->poec_depth != 0);
453 
454 	while ((pdk = _prop_dictionary_iterator_next_object_locked(pi))
455 	    != NULL) {
456 		po = _prop_dictionary_get_keysym(pd, pdk, true);
457 		if (po == NULL ||
458 		    _prop_object_externalize_start_tag(ctx, "key") == false ||
459 		    _prop_object_externalize_append_encoded_cstring(ctx,
460 						   pdk->pdk_key) == false ||
461 		    _prop_object_externalize_end_tag(ctx, "key") == false ||
462 		    (*po->po_type->pot_extern)(ctx, po) == false) {
463 			prop_object_iterator_release(pi);
464 			goto out;
465 		}
466 	}
467 
468 	prop_object_iterator_release(pi);
469 
470 	ctx->poec_depth--;
471 	for (i = 0; i < ctx->poec_depth; i++) {
472 		if (_prop_object_externalize_append_char(ctx, '\t') == false)
473 			goto out;
474 	}
475 	if (_prop_object_externalize_end_tag(ctx, "dict") == false)
476 		goto out;
477 
478 	rv = true;
479 
480  out:
481 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
482 	return (rv);
483 }
484 
485 /* ARGSUSED */
486 static _prop_object_equals_rv_t
_prop_dictionary_equals(prop_object_t v1,prop_object_t v2,void ** stored_pointer1,void ** stored_pointer2,prop_object_t * next_obj1,prop_object_t * next_obj2)487 _prop_dictionary_equals(prop_object_t v1, prop_object_t v2,
488     void **stored_pointer1, void **stored_pointer2,
489     prop_object_t *next_obj1, prop_object_t *next_obj2)
490 {
491 	prop_dictionary_t dict1 = v1;
492 	prop_dictionary_t dict2 = v2;
493 	uintptr_t idx;
494 	_prop_object_equals_rv_t rv = _PROP_OBJECT_EQUALS_FALSE;
495 
496 	if (dict1 == dict2)
497 		return (_PROP_OBJECT_EQUALS_TRUE);
498 
499 	_PROP_ASSERT(*stored_pointer1 == *stored_pointer2);
500 
501 	idx = (uintptr_t)*stored_pointer1;
502 
503 	if (idx == 0) {
504 		if ((uintptr_t)dict1 < (uintptr_t)dict2) {
505 			_PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
506 			_PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
507 		} else {
508 			_PROP_RWLOCK_RDLOCK(dict2->pd_rwlock);
509 			_PROP_RWLOCK_RDLOCK(dict1->pd_rwlock);
510 		}
511 	}
512 
513 	if (dict1->pd_count != dict2->pd_count)
514 		goto out;
515 
516 	if (idx == dict1->pd_count) {
517 		rv = _PROP_OBJECT_EQUALS_TRUE;
518 		goto out;
519 	}
520 
521 	_PROP_ASSERT(idx < dict1->pd_count);
522 
523 	*stored_pointer1 = (void *)(idx + 1);
524 	*stored_pointer2 = (void *)(idx + 1);
525 
526 	*next_obj1 = dict1->pd_array[idx].pde_objref;
527 	*next_obj2 = dict2->pd_array[idx].pde_objref;
528 
529 	if (!prop_dictionary_keysym_equals(dict1->pd_array[idx].pde_key,
530 					   dict2->pd_array[idx].pde_key))
531 		goto out;
532 
533 	return (_PROP_OBJECT_EQUALS_RECURSE);
534 
535  out:
536  	_PROP_RWLOCK_UNLOCK(dict1->pd_rwlock);
537 	_PROP_RWLOCK_UNLOCK(dict2->pd_rwlock);
538 	return (rv);
539 }
540 
541 static void
_prop_dictionary_equals_finish(prop_object_t v1,prop_object_t v2)542 _prop_dictionary_equals_finish(prop_object_t v1, prop_object_t v2)
543 {
544  	_PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v1)->pd_rwlock);
545  	_PROP_RWLOCK_UNLOCK(((prop_dictionary_t)v2)->pd_rwlock);
546 }
547 
548 static prop_dictionary_t
_prop_dictionary_alloc(unsigned int capacity)549 _prop_dictionary_alloc(unsigned int capacity)
550 {
551 	prop_dictionary_t pd;
552 	struct _prop_dict_entry *array;
553 
554 	if (capacity != 0) {
555 		array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
556 		if (array == NULL)
557 			return (NULL);
558 	} else
559 		array = NULL;
560 
561 	pd = _PROP_POOL_GET(_prop_dictionary_pool);
562 	if (pd != NULL) {
563 		_prop_object_init(&pd->pd_obj, &_prop_object_type_dictionary);
564 
565 		_PROP_RWLOCK_INIT(pd->pd_rwlock);
566 		pd->pd_array = array;
567 		pd->pd_capacity = capacity;
568 		pd->pd_count = 0;
569 		pd->pd_flags = 0;
570 
571 		pd->pd_version = 0;
572 	} else if (array != NULL)
573 		_PROP_FREE(array, M_PROP_DICT);
574 
575 	return (pd);
576 }
577 
578 static bool
_prop_dictionary_expand(prop_dictionary_t pd,unsigned int capacity)579 _prop_dictionary_expand(prop_dictionary_t pd, unsigned int capacity)
580 {
581 	struct _prop_dict_entry *array, *oarray;
582 
583 	/*
584 	 * Dictionary must be WRITE-LOCKED.
585 	 */
586 
587 	oarray = pd->pd_array;
588 
589 	array = _PROP_CALLOC(capacity * sizeof(*array), M_PROP_DICT);
590 	if (array == NULL)
591 		return (false);
592 	if (oarray != NULL)
593 		memcpy(array, oarray, pd->pd_capacity * sizeof(*array));
594 	pd->pd_array = array;
595 	pd->pd_capacity = capacity;
596 
597 	if (oarray != NULL)
598 		_PROP_FREE(oarray, M_PROP_DICT);
599 
600 	return (true);
601 }
602 
603 static prop_object_t
_prop_dictionary_iterator_next_object_locked(void * v)604 _prop_dictionary_iterator_next_object_locked(void *v)
605 {
606 	struct _prop_dictionary_iterator *pdi = v;
607 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
608 	prop_dictionary_keysym_t pdk = NULL;
609 
610 	_PROP_ASSERT(prop_object_is_dictionary(pd));
611 
612 	if (pd->pd_version != pdi->pdi_base.pi_version)
613 		goto out;	/* dictionary changed during iteration */
614 
615 	_PROP_ASSERT(pdi->pdi_index <= pd->pd_count);
616 
617 	if (pdi->pdi_index == pd->pd_count)
618 		goto out;	/* we've iterated all objects */
619 
620 	pdk = pd->pd_array[pdi->pdi_index].pde_key;
621 	pdi->pdi_index++;
622 
623  out:
624 	return (pdk);
625 }
626 
627 static prop_object_t
_prop_dictionary_iterator_next_object(void * v)628 _prop_dictionary_iterator_next_object(void *v)
629 {
630 	struct _prop_dictionary_iterator *pdi = v;
631 	prop_dictionary_t pd _PROP_ARG_UNUSED = pdi->pdi_base.pi_obj;
632 	prop_dictionary_keysym_t pdk;
633 
634 	_PROP_ASSERT(prop_object_is_dictionary(pd));
635 
636 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
637 	pdk = _prop_dictionary_iterator_next_object_locked(pdi);
638 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
639 	return (pdk);
640 }
641 
642 static void
_prop_dictionary_iterator_reset_locked(void * v)643 _prop_dictionary_iterator_reset_locked(void *v)
644 {
645 	struct _prop_dictionary_iterator *pdi = v;
646 	prop_dictionary_t pd = pdi->pdi_base.pi_obj;
647 
648 	_PROP_ASSERT(prop_object_is_dictionary(pd));
649 
650 	pdi->pdi_index = 0;
651 	pdi->pdi_base.pi_version = pd->pd_version;
652 }
653 
654 static void
_prop_dictionary_iterator_reset(void * v)655 _prop_dictionary_iterator_reset(void *v)
656 {
657 	struct _prop_dictionary_iterator *pdi = v;
658 #if defined(__minix) && defined(_REENTRANT)
659 	prop_dictionary_t pd _PROP_ARG_UNUSED = pdi->pdi_base.pi_obj;
660 #endif /* defined(__minix) && defined(_REENTRANT) */
661 
662 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
663 	_prop_dictionary_iterator_reset_locked(pdi);
664 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
665 }
666 
667 /*
668  * prop_dictionary_create --
669  *	Create a dictionary.
670  */
671 prop_dictionary_t
prop_dictionary_create(void)672 prop_dictionary_create(void)
673 {
674 
675 	return (_prop_dictionary_alloc(0));
676 }
677 
678 /*
679  * prop_dictionary_create_with_capacity --
680  *	Create a dictionary with the capacity to store N objects.
681  */
682 prop_dictionary_t
prop_dictionary_create_with_capacity(unsigned int capacity)683 prop_dictionary_create_with_capacity(unsigned int capacity)
684 {
685 
686 	return (_prop_dictionary_alloc(capacity));
687 }
688 
689 /*
690  * prop_dictionary_copy --
691  *	Copy a dictionary.  The new dictionary has an initial capacity equal
692  *	to the number of objects stored int the original dictionary.  The new
693  *	dictionary contains refrences to the original dictionary's objects,
694  *	not copies of those objects (i.e. a shallow copy).
695  */
696 prop_dictionary_t
prop_dictionary_copy(prop_dictionary_t opd)697 prop_dictionary_copy(prop_dictionary_t opd)
698 {
699 	prop_dictionary_t pd;
700 	prop_dictionary_keysym_t pdk;
701 	prop_object_t po;
702 	unsigned int idx;
703 
704 	if (! prop_object_is_dictionary(opd))
705 		return (NULL);
706 
707 	_PROP_RWLOCK_RDLOCK(opd->pd_rwlock);
708 
709 	pd = _prop_dictionary_alloc(opd->pd_count);
710 	if (pd != NULL) {
711 		for (idx = 0; idx < opd->pd_count; idx++) {
712 			pdk = opd->pd_array[idx].pde_key;
713 			po = opd->pd_array[idx].pde_objref;
714 
715 			prop_object_retain(pdk);
716 			prop_object_retain(po);
717 
718 			pd->pd_array[idx].pde_key = pdk;
719 			pd->pd_array[idx].pde_objref = po;
720 		}
721 		pd->pd_count = opd->pd_count;
722 		pd->pd_flags = opd->pd_flags;
723 	}
724 	_PROP_RWLOCK_UNLOCK(opd->pd_rwlock);
725 	return (pd);
726 }
727 
728 /*
729  * prop_dictionary_copy_mutable --
730  *	Like prop_dictionary_copy(), but the resulting dictionary is
731  *	mutable.
732  */
733 prop_dictionary_t
prop_dictionary_copy_mutable(prop_dictionary_t opd)734 prop_dictionary_copy_mutable(prop_dictionary_t opd)
735 {
736 	prop_dictionary_t pd;
737 
738 	if (! prop_object_is_dictionary(opd))
739 		return (NULL);
740 
741 	pd = prop_dictionary_copy(opd);
742 	if (pd != NULL)
743 		pd->pd_flags &= ~PD_F_IMMUTABLE;
744 
745 	return (pd);
746 }
747 
748 /*
749  * prop_dictionary_make_immutable --
750  *	Set the immutable flag on that dictionary.
751  */
752 void
prop_dictionary_make_immutable(prop_dictionary_t pd)753 prop_dictionary_make_immutable(prop_dictionary_t pd)
754 {
755 
756 	_PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
757 	if (prop_dictionary_is_immutable(pd) == false)
758 		pd->pd_flags |= PD_F_IMMUTABLE;
759 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
760 }
761 
762 /*
763  * prop_dictionary_count --
764  *	Return the number of objects stored in the dictionary.
765  */
766 unsigned int
prop_dictionary_count(prop_dictionary_t pd)767 prop_dictionary_count(prop_dictionary_t pd)
768 {
769 	unsigned int rv;
770 
771 	if (! prop_object_is_dictionary(pd))
772 		return (0);
773 
774 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
775 	rv = pd->pd_count;
776 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
777 
778 	return (rv);
779 }
780 
781 /*
782  * prop_dictionary_ensure_capacity --
783  *	Ensure that the dictionary has the capacity to store the specified
784  *	total number of objects (including the objects already stored in
785  *	the dictionary).
786  */
787 bool
prop_dictionary_ensure_capacity(prop_dictionary_t pd,unsigned int capacity)788 prop_dictionary_ensure_capacity(prop_dictionary_t pd, unsigned int capacity)
789 {
790 	bool rv;
791 
792 	if (! prop_object_is_dictionary(pd))
793 		return (false);
794 
795 	_PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
796 	if (capacity > pd->pd_capacity)
797 		rv = _prop_dictionary_expand(pd, capacity);
798 	else
799 		rv = true;
800 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
801 	return (rv);
802 }
803 
804 static prop_object_iterator_t
_prop_dictionary_iterator_locked(prop_dictionary_t pd)805 _prop_dictionary_iterator_locked(prop_dictionary_t pd)
806 {
807 	struct _prop_dictionary_iterator *pdi;
808 
809 	if (! prop_object_is_dictionary(pd))
810 		return (NULL);
811 
812 	pdi = _PROP_CALLOC(sizeof(*pdi), M_TEMP);
813 	if (pdi == NULL)
814 		return (NULL);
815 	pdi->pdi_base.pi_next_object = _prop_dictionary_iterator_next_object;
816 	pdi->pdi_base.pi_reset = _prop_dictionary_iterator_reset;
817 	prop_object_retain(pd);
818 	pdi->pdi_base.pi_obj = pd;
819 	_prop_dictionary_iterator_reset_locked(pdi);
820 
821 	return (&pdi->pdi_base);
822 }
823 
824 /*
825  * prop_dictionary_iterator --
826  *	Return an iterator for the dictionary.  The dictionary is retained by
827  *	the iterator.
828  */
829 prop_object_iterator_t
prop_dictionary_iterator(prop_dictionary_t pd)830 prop_dictionary_iterator(prop_dictionary_t pd)
831 {
832 	prop_object_iterator_t pi;
833 
834 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
835 	pi = _prop_dictionary_iterator_locked(pd);
836 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
837 	return (pi);
838 }
839 
840 /*
841  * prop_dictionary_all_keys --
842  *	Return an array containing a snapshot of all of the keys
843  *	in the dictionary.
844  */
845 prop_array_t
prop_dictionary_all_keys(prop_dictionary_t pd)846 prop_dictionary_all_keys(prop_dictionary_t pd)
847 {
848 	prop_array_t array;
849 	unsigned int idx;
850 	bool rv = true;
851 
852 	if (! prop_object_is_dictionary(pd))
853 		return (NULL);
854 
855 	/* There is no pressing need to lock the dictionary for this. */
856 	array = prop_array_create_with_capacity(pd->pd_count);
857 
858 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
859 
860 	for (idx = 0; idx < pd->pd_count; idx++) {
861 		rv = prop_array_add(array, pd->pd_array[idx].pde_key);
862 		if (rv == false)
863 			break;
864 	}
865 
866 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
867 
868 	if (rv == false) {
869 		prop_object_release(array);
870 		array = NULL;
871 	}
872 	return (array);
873 }
874 
875 static struct _prop_dict_entry *
_prop_dict_lookup(prop_dictionary_t pd,const char * key,unsigned int * idxp)876 _prop_dict_lookup(prop_dictionary_t pd, const char *key,
877 		  unsigned int *idxp)
878 {
879 	struct _prop_dict_entry *pde;
880 	unsigned int base, idx, distance;
881 	int res;
882 
883 	/*
884 	 * Dictionary must be READ-LOCKED or WRITE-LOCKED.
885 	 */
886 
887 	for (idx = 0, base = 0, distance = pd->pd_count; distance != 0;
888 	     distance >>= 1) {
889 		idx = base + (distance >> 1);
890 		pde = &pd->pd_array[idx];
891 		_PROP_ASSERT(pde->pde_key != NULL);
892 		res = strcmp(key, pde->pde_key->pdk_key);
893 		if (res == 0) {
894 			if (idxp != NULL)
895 				*idxp = idx;
896 			return (pde);
897 		}
898 		if (res > 0) {	/* key > pdk_key: move right */
899 			base = idx + 1;
900 			distance--;
901 		}		/* else move left */
902 	}
903 
904 	/* idx points to the slot we looked at last. */
905 	if (idxp != NULL)
906 		*idxp = idx;
907 	return (NULL);
908 }
909 
910 static prop_object_t
_prop_dictionary_get(prop_dictionary_t pd,const char * key,bool locked)911 _prop_dictionary_get(prop_dictionary_t pd, const char *key, bool locked)
912 {
913 	const struct _prop_dict_entry *pde;
914 	prop_object_t po = NULL;
915 
916 	if (! prop_object_is_dictionary(pd))
917 		return (NULL);
918 
919 #if defined(__minix) && defined(_REENTRANT)
920 	if (!locked)
921 		_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
922 #endif /* defined(__minix) && defined(_REENTRANT) */
923 	pde = _prop_dict_lookup(pd, key, NULL);
924 	if (pde != NULL) {
925 		_PROP_ASSERT(pde->pde_objref != NULL);
926 		po = pde->pde_objref;
927 	}
928 #if defined(__minix) && defined(_REENTRANT)
929 	if (!locked)
930 		_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
931 #endif /* defined(__minix) && defined(_REENTRANT) */
932 	return (po);
933 }
934 /*
935  * prop_dictionary_get --
936  *	Return the object stored with specified key.
937  */
938 prop_object_t
prop_dictionary_get(prop_dictionary_t pd,const char * key)939 prop_dictionary_get(prop_dictionary_t pd, const char *key)
940 {
941 	prop_object_t po = NULL;
942 
943 	if (! prop_object_is_dictionary(pd))
944 		return (NULL);
945 
946 	_PROP_RWLOCK_RDLOCK(pd->pd_rwlock);
947 	po = _prop_dictionary_get(pd, key, true);
948 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
949 	return (po);
950 }
951 
952 static prop_object_t
_prop_dictionary_get_keysym(prop_dictionary_t pd,prop_dictionary_keysym_t pdk,bool locked)953 _prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
954     bool locked)
955 {
956 
957 	if (! (prop_object_is_dictionary(pd) &&
958 	       prop_object_is_dictionary_keysym(pdk)))
959 		return (NULL);
960 
961 	return (_prop_dictionary_get(pd, pdk->pdk_key, locked));
962 }
963 
964 /*
965  * prop_dictionary_get_keysym --
966  *	Return the object stored at the location encoded by the keysym.
967  */
968 prop_object_t
prop_dictionary_get_keysym(prop_dictionary_t pd,prop_dictionary_keysym_t pdk)969 prop_dictionary_get_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk)
970 {
971 
972 	return (_prop_dictionary_get_keysym(pd, pdk, false));
973 }
974 
975 /*
976  * prop_dictionary_set --
977  *	Store a reference to an object at with the specified key.
978  *	If the key already exisit, the original object is released.
979  */
980 bool
prop_dictionary_set(prop_dictionary_t pd,const char * key,prop_object_t po)981 prop_dictionary_set(prop_dictionary_t pd, const char *key, prop_object_t po)
982 {
983 	struct _prop_dict_entry *pde;
984 	prop_dictionary_keysym_t pdk;
985 	unsigned int idx;
986 	bool rv = false;
987 
988 	if (! prop_object_is_dictionary(pd))
989 		return (false);
990 
991 	_PROP_ASSERT(pd->pd_count <= pd->pd_capacity);
992 
993 	if (prop_dictionary_is_immutable(pd))
994 		return (false);
995 
996 	_PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
997 
998 	pde = _prop_dict_lookup(pd, key, &idx);
999 	if (pde != NULL) {
1000 		prop_object_t opo = pde->pde_objref;
1001 		prop_object_retain(po);
1002 		pde->pde_objref = po;
1003 		prop_object_release(opo);
1004 		rv = true;
1005 		goto out;
1006 	}
1007 
1008 	pdk = _prop_dict_keysym_alloc(key);
1009 	if (pdk == NULL)
1010 		goto out;
1011 
1012 	if (pd->pd_count == pd->pd_capacity &&
1013 	    _prop_dictionary_expand(pd,
1014 	    			    pd->pd_capacity + EXPAND_STEP) == false) {
1015 		prop_object_release(pdk);
1016 	    	goto out;
1017 	}
1018 
1019 	/* At this point, the store will succeed. */
1020 	prop_object_retain(po);
1021 
1022 	if (pd->pd_count == 0) {
1023 		pd->pd_array[0].pde_key = pdk;
1024 		pd->pd_array[0].pde_objref = po;
1025 		pd->pd_count++;
1026 		pd->pd_version++;
1027 		rv = true;
1028 		goto out;
1029 	}
1030 
1031 	pde = &pd->pd_array[idx];
1032 	_PROP_ASSERT(pde->pde_key != NULL);
1033 
1034 	if (strcmp(key, pde->pde_key->pdk_key) < 0) {
1035 		/*
1036 		 * key < pdk_key: insert to the left.  This is the same as
1037 		 * inserting to the right, except we decrement the current
1038 		 * index first.
1039 		 *
1040 		 * Because we're unsigned, we have to special case 0
1041 		 * (grumble).
1042 		 */
1043 		if (idx == 0) {
1044 			memmove(&pd->pd_array[1], &pd->pd_array[0],
1045 				pd->pd_count * sizeof(*pde));
1046 			pd->pd_array[0].pde_key = pdk;
1047 			pd->pd_array[0].pde_objref = po;
1048 			pd->pd_count++;
1049 			pd->pd_version++;
1050 			rv = true;
1051 			goto out;
1052 		}
1053 		idx--;
1054 	}
1055 
1056 	memmove(&pd->pd_array[idx + 2], &pd->pd_array[idx + 1],
1057 		(pd->pd_count - (idx + 1)) * sizeof(*pde));
1058 	pd->pd_array[idx + 1].pde_key = pdk;
1059 	pd->pd_array[idx + 1].pde_objref = po;
1060 	pd->pd_count++;
1061 
1062 	pd->pd_version++;
1063 
1064 	rv = true;
1065 
1066  out:
1067 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1068 	return (rv);
1069 }
1070 
1071 /*
1072  * prop_dictionary_set_keysym --
1073  *	Replace the object in the dictionary at the location encoded by
1074  *	the keysym.
1075  */
1076 bool
prop_dictionary_set_keysym(prop_dictionary_t pd,prop_dictionary_keysym_t pdk,prop_object_t po)1077 prop_dictionary_set_keysym(prop_dictionary_t pd, prop_dictionary_keysym_t pdk,
1078 			   prop_object_t po)
1079 {
1080 
1081 	if (! (prop_object_is_dictionary(pd) &&
1082 	       prop_object_is_dictionary_keysym(pdk)))
1083 		return (false);
1084 
1085 	return (prop_dictionary_set(pd, pdk->pdk_key, po));
1086 }
1087 
1088 static void
_prop_dictionary_remove(prop_dictionary_t pd,struct _prop_dict_entry * pde,unsigned int idx)1089 _prop_dictionary_remove(prop_dictionary_t pd, struct _prop_dict_entry *pde,
1090     unsigned int idx)
1091 {
1092 	prop_dictionary_keysym_t pdk = pde->pde_key;
1093 	prop_object_t po = pde->pde_objref;
1094 
1095 	/*
1096 	 * Dictionary must be WRITE-LOCKED.
1097 	 */
1098 
1099 	_PROP_ASSERT(pd->pd_count != 0);
1100 	_PROP_ASSERT(idx < pd->pd_count);
1101 	_PROP_ASSERT(pde == &pd->pd_array[idx]);
1102 
1103 	idx++;
1104 	memmove(&pd->pd_array[idx - 1], &pd->pd_array[idx],
1105 		(pd->pd_count - idx) * sizeof(*pde));
1106 	pd->pd_count--;
1107 	pd->pd_version++;
1108 
1109 
1110 	prop_object_release(pdk);
1111 
1112 	prop_object_release(po);
1113 }
1114 
1115 /*
1116  * prop_dictionary_remove --
1117  *	Remove the reference to an object with the specified key from
1118  *	the dictionary.
1119  */
1120 void
prop_dictionary_remove(prop_dictionary_t pd,const char * key)1121 prop_dictionary_remove(prop_dictionary_t pd, const char *key)
1122 {
1123 	struct _prop_dict_entry *pde;
1124 	unsigned int idx;
1125 
1126 	if (! prop_object_is_dictionary(pd))
1127 		return;
1128 
1129 	_PROP_RWLOCK_WRLOCK(pd->pd_rwlock);
1130 
1131 	/* XXX Should this be a _PROP_ASSERT()? */
1132 	if (prop_dictionary_is_immutable(pd))
1133 		goto out;
1134 
1135 	pde = _prop_dict_lookup(pd, key, &idx);
1136 	/* XXX Should this be a _PROP_ASSERT()? */
1137 	if (pde == NULL)
1138 		goto out;
1139 
1140 	_prop_dictionary_remove(pd, pde, idx);
1141  out:
1142 	_PROP_RWLOCK_UNLOCK(pd->pd_rwlock);
1143 }
1144 
1145 /*
1146  * prop_dictionary_remove_keysym --
1147  *	Remove a reference to an object stored in the dictionary at the
1148  *	location encoded by the keysym.
1149  */
1150 void
prop_dictionary_remove_keysym(prop_dictionary_t pd,prop_dictionary_keysym_t pdk)1151 prop_dictionary_remove_keysym(prop_dictionary_t pd,
1152 			      prop_dictionary_keysym_t pdk)
1153 {
1154 
1155 	if (! (prop_object_is_dictionary(pd) &&
1156 	       prop_object_is_dictionary_keysym(pdk)))
1157 		return;
1158 
1159 	prop_dictionary_remove(pd, pdk->pdk_key);
1160 }
1161 
1162 /*
1163  * prop_dictionary_equals --
1164  *	Return true if the two dictionaries are equivalent.  Note we do a
1165  *	by-value comparison of the objects in the dictionary.
1166  */
1167 bool
prop_dictionary_equals(prop_dictionary_t dict1,prop_dictionary_t dict2)1168 prop_dictionary_equals(prop_dictionary_t dict1, prop_dictionary_t dict2)
1169 {
1170 	if (!prop_object_is_dictionary(dict1) ||
1171 	    !prop_object_is_dictionary(dict2))
1172 		return (false);
1173 
1174 	return (prop_object_equals(dict1, dict2));
1175 }
1176 
1177 /*
1178  * prop_dictionary_keysym_cstring_nocopy --
1179  *	Return an immutable reference to the keysym's value.
1180  */
1181 const char *
prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)1182 prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t pdk)
1183 {
1184 
1185 	if (! prop_object_is_dictionary_keysym(pdk))
1186 		return (NULL);
1187 
1188 	return (pdk->pdk_key);
1189 }
1190 
1191 /*
1192  * prop_dictionary_keysym_equals --
1193  *	Return true if the two dictionary key symbols are equivalent.
1194  *	Note: We do not compare the object references.
1195  */
1196 bool
prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,prop_dictionary_keysym_t pdk2)1197 prop_dictionary_keysym_equals(prop_dictionary_keysym_t pdk1,
1198 			      prop_dictionary_keysym_t pdk2)
1199 {
1200 	if (!prop_object_is_dictionary_keysym(pdk1) ||
1201 	    !prop_object_is_dictionary_keysym(pdk2))
1202 		return (false);
1203 
1204 	return (prop_object_equals(pdk1, pdk2));
1205 }
1206 
1207 /*
1208  * prop_dictionary_externalize --
1209  *	Externalize a dictionary, returning a NUL-terminated buffer
1210  *	containing the XML-style representation.  The buffer is allocated
1211  *	with the M_TEMP memory type.
1212  */
1213 char *
prop_dictionary_externalize(prop_dictionary_t pd)1214 prop_dictionary_externalize(prop_dictionary_t pd)
1215 {
1216 	struct _prop_object_externalize_context *ctx;
1217 	char *cp;
1218 
1219 	ctx = _prop_object_externalize_context_alloc();
1220 	if (ctx == NULL)
1221 		return (NULL);
1222 
1223 	if (_prop_object_externalize_header(ctx) == false ||
1224 	    (*pd->pd_obj.po_type->pot_extern)(ctx, pd) == false ||
1225 	    _prop_object_externalize_footer(ctx) == false) {
1226 		/* We are responsible for releasing the buffer. */
1227 		_PROP_FREE(ctx->poec_buf, M_TEMP);
1228 		_prop_object_externalize_context_free(ctx);
1229 		return (NULL);
1230 	}
1231 
1232 	cp = ctx->poec_buf;
1233 	_prop_object_externalize_context_free(ctx);
1234 
1235 	return (cp);
1236 }
1237 
1238 /*
1239  * _prop_dictionary_internalize --
1240  *	Parse a <dict>...</dict> and return the object created from the
1241  *	external representation.
1242  *
1243  * Internal state in via rec_data is the storage area for the last processed
1244  * key.
1245  * _prop_dictionary_internalize_body is the upper half of the parse loop.
1246  * It is responsible for parsing the key directly and storing it in the area
1247  * referenced by rec_data.
1248  * _prop_dictionary_internalize_cont is the lower half and called with the value
1249  * associated with the key.
1250  */
1251 static bool _prop_dictionary_internalize_body(prop_stack_t,
1252     prop_object_t *, struct _prop_object_internalize_context *, char *);
1253 
1254 bool
_prop_dictionary_internalize(prop_stack_t stack,prop_object_t * obj,struct _prop_object_internalize_context * ctx)1255 _prop_dictionary_internalize(prop_stack_t stack, prop_object_t *obj,
1256     struct _prop_object_internalize_context *ctx)
1257 {
1258 	prop_dictionary_t dict;
1259 	char *tmpkey;
1260 
1261 	/* We don't currently understand any attributes. */
1262 	if (ctx->poic_tagattr != NULL)
1263 		return (true);
1264 
1265 	dict = prop_dictionary_create();
1266 	if (dict == NULL)
1267 		return (true);
1268 
1269 	if (ctx->poic_is_empty_element) {
1270 		*obj = dict;
1271 		return (true);
1272 	}
1273 
1274 	tmpkey = _PROP_MALLOC(PDK_MAXKEY + 1, M_TEMP);
1275 	if (tmpkey == NULL) {
1276 		prop_object_release(dict);
1277 		return (true);
1278 	}
1279 
1280 	*obj = dict;
1281 	/*
1282 	 * Opening tag is found, storage for key allocated and
1283 	 * now continue to the first element.
1284 	 */
1285 	return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1286 }
1287 
1288 static bool
_prop_dictionary_internalize_continue(prop_stack_t stack,prop_object_t * obj,struct _prop_object_internalize_context * ctx,void * data,prop_object_t child)1289 _prop_dictionary_internalize_continue(prop_stack_t stack, prop_object_t *obj,
1290     struct _prop_object_internalize_context *ctx, void *data, prop_object_t child)
1291 {
1292 	prop_dictionary_t dict = *obj;
1293 	char *tmpkey = data;
1294 
1295 	_PROP_ASSERT(tmpkey != NULL);
1296 
1297 	if (child == NULL ||
1298 	    prop_dictionary_set(dict, tmpkey, child) == false) {
1299 		_PROP_FREE(tmpkey, M_TEMP);
1300 		if (child != NULL)
1301 			prop_object_release(child);
1302 		prop_object_release(dict);
1303 		*obj = NULL;
1304 		return (true);
1305 	}
1306 
1307 	prop_object_release(child);
1308 
1309 	/*
1310 	 * key, value was added, now continue looking for the next key
1311 	 * or the closing tag.
1312 	 */
1313 	return _prop_dictionary_internalize_body(stack, obj, ctx, tmpkey);
1314 }
1315 
1316 static bool
_prop_dictionary_internalize_body(prop_stack_t stack,prop_object_t * obj,struct _prop_object_internalize_context * ctx,char * tmpkey)1317 _prop_dictionary_internalize_body(prop_stack_t stack, prop_object_t *obj,
1318     struct _prop_object_internalize_context *ctx, char *tmpkey)
1319 {
1320 	prop_dictionary_t dict = *obj;
1321 	size_t keylen;
1322 
1323 	/* Fetch the next tag. */
1324 	if (_prop_object_internalize_find_tag(ctx, NULL, _PROP_TAG_TYPE_EITHER) == false)
1325 		goto bad;
1326 
1327 	/* Check to see if this is the end of the dictionary. */
1328 	if (_PROP_TAG_MATCH(ctx, "dict") &&
1329 	    ctx->poic_tag_type == _PROP_TAG_TYPE_END) {
1330 		_PROP_FREE(tmpkey, M_TEMP);
1331 		return (true);
1332 	}
1333 
1334 	/* Ok, it must be a non-empty key start tag. */
1335 	if (!_PROP_TAG_MATCH(ctx, "key") ||
1336 	    ctx->poic_tag_type != _PROP_TAG_TYPE_START ||
1337 	    ctx->poic_is_empty_element)
1338 	    	goto bad;
1339 
1340 	if (_prop_object_internalize_decode_string(ctx,
1341 					tmpkey, PDK_MAXKEY, &keylen,
1342 					&ctx->poic_cp) == false)
1343 		goto bad;
1344 
1345 	_PROP_ASSERT(keylen <= PDK_MAXKEY);
1346 	tmpkey[keylen] = '\0';
1347 
1348 	if (_prop_object_internalize_find_tag(ctx, "key",
1349 				_PROP_TAG_TYPE_END) == false)
1350 		goto bad;
1351 
1352 	/* ..and now the beginning of the value. */
1353 	if (_prop_object_internalize_find_tag(ctx, NULL,
1354 				_PROP_TAG_TYPE_START) == false)
1355 		goto bad;
1356 
1357 	/*
1358 	 * Key is found, now wait for value to be parsed.
1359 	 */
1360 	if (_prop_stack_push(stack, *obj,
1361 			     _prop_dictionary_internalize_continue,
1362 			     tmpkey, NULL))
1363 		return (false);
1364 
1365  bad:
1366 	_PROP_FREE(tmpkey, M_TEMP);
1367 	prop_object_release(dict);
1368 	*obj = NULL;
1369 	return (true);
1370 }
1371 
1372 /*
1373  * prop_dictionary_internalize --
1374  *	Create a dictionary by parsing the NUL-terminated XML-style
1375  *	representation.
1376  */
1377 prop_dictionary_t
prop_dictionary_internalize(const char * xml)1378 prop_dictionary_internalize(const char *xml)
1379 {
1380 	return _prop_generic_internalize(xml, "dict");
1381 }
1382 
1383 #if !defined(_KERNEL) && !defined(_STANDALONE)
1384 /*
1385  * prop_dictionary_externalize_to_file --
1386  *	Externalize a dictionary to the specified file.
1387  */
1388 bool
prop_dictionary_externalize_to_file(prop_dictionary_t dict,const char * fname)1389 prop_dictionary_externalize_to_file(prop_dictionary_t dict, const char *fname)
1390 {
1391 	char *xml;
1392 	bool rv;
1393 	int save_errno = 0;	/* XXXGCC -Wuninitialized [mips, ...] */
1394 
1395 	xml = prop_dictionary_externalize(dict);
1396 	if (xml == NULL)
1397 		return (false);
1398 	rv = _prop_object_externalize_write_file(fname, xml, strlen(xml));
1399 	if (rv == false)
1400 		save_errno = errno;
1401 	_PROP_FREE(xml, M_TEMP);
1402 	if (rv == false)
1403 		errno = save_errno;
1404 
1405 	return (rv);
1406 }
1407 
1408 /*
1409  * prop_dictionary_internalize_from_file --
1410  *	Internalize a dictionary from a file.
1411  */
1412 prop_dictionary_t
prop_dictionary_internalize_from_file(const char * fname)1413 prop_dictionary_internalize_from_file(const char *fname)
1414 {
1415 	struct _prop_object_internalize_mapped_file *mf;
1416 	prop_dictionary_t dict;
1417 
1418 	mf = _prop_object_internalize_map_file(fname);
1419 	if (mf == NULL)
1420 		return (NULL);
1421 	dict = prop_dictionary_internalize(mf->poimf_xml);
1422 	_prop_object_internalize_unmap_file(mf);
1423 
1424 	return (dict);
1425 }
1426 #endif /* !_KERNEL && !_STANDALONE */
1427