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