prop_object.c revision 1.7 1 1.7 thorpej /* $NetBSD: prop_object.c,v 1.7 2006/10/12 04:41:51 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_object.h>
40 1.1 thorpej #include "prop_object_impl.h"
41 1.1 thorpej
42 1.4 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
43 1.4 thorpej #include <sys/mman.h>
44 1.4 thorpej #include <sys/stat.h>
45 1.4 thorpej #include <errno.h>
46 1.4 thorpej #include <fcntl.h>
47 1.4 thorpej #include <limits.h>
48 1.4 thorpej #include <unistd.h>
49 1.4 thorpej #endif
50 1.4 thorpej
51 1.1 thorpej #ifdef _STANDALONE
52 1.1 thorpej void *
53 1.1 thorpej _prop_standalone_calloc(size_t size)
54 1.1 thorpej {
55 1.1 thorpej void *rv;
56 1.1 thorpej
57 1.1 thorpej rv = alloc(size);
58 1.1 thorpej if (rv != NULL)
59 1.1 thorpej memset(rv, 0, size);
60 1.1 thorpej
61 1.1 thorpej return (rv);
62 1.1 thorpej }
63 1.2 thorpej
64 1.2 thorpej void *
65 1.2 thorpej _prop_standalone_realloc(void *v, size_t size)
66 1.2 thorpej {
67 1.2 thorpej void *rv;
68 1.2 thorpej
69 1.2 thorpej rv = alloc(size);
70 1.2 thorpej if (rv != NULL) {
71 1.2 thorpej memcpy(rv, v, size); /* XXX */
72 1.2 thorpej dealloc(v, 0); /* XXX */
73 1.2 thorpej }
74 1.2 thorpej
75 1.2 thorpej return (rv);
76 1.2 thorpej }
77 1.1 thorpej #endif /* _STANDALONE */
78 1.1 thorpej
79 1.1 thorpej /*
80 1.1 thorpej * _prop_object_init --
81 1.1 thorpej * Initialize an object. Called when sub-classes create
82 1.1 thorpej * an instance.
83 1.1 thorpej */
84 1.1 thorpej void
85 1.2 thorpej _prop_object_init(struct _prop_object *po, const struct _prop_object_type *pot)
86 1.1 thorpej {
87 1.1 thorpej
88 1.2 thorpej po->po_type = pot;
89 1.1 thorpej po->po_refcnt = 1;
90 1.1 thorpej }
91 1.1 thorpej
92 1.1 thorpej /*
93 1.1 thorpej * _prop_object_fini --
94 1.1 thorpej * Finalize an object. Called when sub-classes destroy
95 1.1 thorpej * an instance.
96 1.1 thorpej */
97 1.4 thorpej /*ARGSUSED*/
98 1.1 thorpej void
99 1.1 thorpej _prop_object_fini(struct _prop_object *po)
100 1.1 thorpej {
101 1.1 thorpej /* Nothing to do, currently. */
102 1.7 thorpej (void) po;
103 1.1 thorpej }
104 1.1 thorpej
105 1.1 thorpej /*
106 1.1 thorpej * _prop_object_externalize_start_tag --
107 1.1 thorpej * Append an XML-style start tag to the externalize buffer.
108 1.1 thorpej */
109 1.1 thorpej boolean_t
110 1.1 thorpej _prop_object_externalize_start_tag(
111 1.1 thorpej struct _prop_object_externalize_context *ctx, const char *tag)
112 1.1 thorpej {
113 1.1 thorpej unsigned int i;
114 1.1 thorpej
115 1.1 thorpej for (i = 0; i < ctx->poec_depth; i++) {
116 1.1 thorpej if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
117 1.1 thorpej return (FALSE);
118 1.1 thorpej }
119 1.1 thorpej if (_prop_object_externalize_append_char(ctx, '<') == FALSE ||
120 1.1 thorpej _prop_object_externalize_append_cstring(ctx, tag) == FALSE ||
121 1.1 thorpej _prop_object_externalize_append_char(ctx, '>') == FALSE)
122 1.1 thorpej return (FALSE);
123 1.1 thorpej
124 1.1 thorpej return (TRUE);
125 1.1 thorpej }
126 1.1 thorpej
127 1.1 thorpej /*
128 1.1 thorpej * _prop_object_externalize_end_tag --
129 1.1 thorpej * Append an XML-style end tag to the externalize buffer.
130 1.1 thorpej */
131 1.1 thorpej boolean_t
132 1.1 thorpej _prop_object_externalize_end_tag(
133 1.1 thorpej struct _prop_object_externalize_context *ctx, const char *tag)
134 1.1 thorpej {
135 1.1 thorpej
136 1.1 thorpej if (_prop_object_externalize_append_char(ctx, '<') == FALSE ||
137 1.1 thorpej _prop_object_externalize_append_char(ctx, '/') == FALSE ||
138 1.1 thorpej _prop_object_externalize_append_cstring(ctx, tag) == FALSE ||
139 1.1 thorpej _prop_object_externalize_append_char(ctx, '>') == FALSE ||
140 1.1 thorpej _prop_object_externalize_append_char(ctx, '\n') == FALSE)
141 1.1 thorpej return (FALSE);
142 1.1 thorpej
143 1.1 thorpej return (TRUE);
144 1.1 thorpej }
145 1.1 thorpej
146 1.1 thorpej /*
147 1.1 thorpej * _prop_object_externalize_empty_tag --
148 1.1 thorpej * Append an XML-style empty tag to the externalize buffer.
149 1.1 thorpej */
150 1.1 thorpej boolean_t
151 1.1 thorpej _prop_object_externalize_empty_tag(
152 1.1 thorpej struct _prop_object_externalize_context *ctx, const char *tag)
153 1.1 thorpej {
154 1.1 thorpej unsigned int i;
155 1.1 thorpej
156 1.1 thorpej for (i = 0; i < ctx->poec_depth; i++) {
157 1.1 thorpej if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
158 1.1 thorpej return (FALSE);
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej if (_prop_object_externalize_append_char(ctx, '<') == FALSE ||
162 1.1 thorpej _prop_object_externalize_append_cstring(ctx, tag) == FALSE ||
163 1.1 thorpej _prop_object_externalize_append_char(ctx, '/') == FALSE ||
164 1.1 thorpej _prop_object_externalize_append_char(ctx, '>') == FALSE ||
165 1.1 thorpej _prop_object_externalize_append_char(ctx, '\n') == FALSE)
166 1.1 thorpej return (FALSE);
167 1.1 thorpej
168 1.1 thorpej return (TRUE);
169 1.1 thorpej }
170 1.1 thorpej
171 1.1 thorpej /*
172 1.1 thorpej * _prop_object_externalize_append_cstring --
173 1.1 thorpej * Append a C string to the externalize buffer.
174 1.1 thorpej */
175 1.1 thorpej boolean_t
176 1.1 thorpej _prop_object_externalize_append_cstring(
177 1.1 thorpej struct _prop_object_externalize_context *ctx, const char *cp)
178 1.1 thorpej {
179 1.1 thorpej
180 1.1 thorpej while (*cp != '\0') {
181 1.1 thorpej if (_prop_object_externalize_append_char(ctx,
182 1.1 thorpej (unsigned char) *cp) == FALSE)
183 1.1 thorpej return (FALSE);
184 1.1 thorpej cp++;
185 1.1 thorpej }
186 1.1 thorpej
187 1.1 thorpej return (TRUE);
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej /*
191 1.1 thorpej * _prop_object_externalize_append_encoded_cstring --
192 1.1 thorpej * Append an encoded C string to the externalize buffer.
193 1.1 thorpej */
194 1.1 thorpej boolean_t
195 1.1 thorpej _prop_object_externalize_append_encoded_cstring(
196 1.1 thorpej struct _prop_object_externalize_context *ctx, const char *cp)
197 1.1 thorpej {
198 1.1 thorpej
199 1.1 thorpej while (*cp != '\0') {
200 1.1 thorpej switch (*cp) {
201 1.1 thorpej case '<':
202 1.1 thorpej if (_prop_object_externalize_append_cstring(ctx,
203 1.1 thorpej "<") == FALSE)
204 1.1 thorpej return (FALSE);
205 1.1 thorpej break;
206 1.1 thorpej case '>':
207 1.1 thorpej if (_prop_object_externalize_append_cstring(ctx,
208 1.1 thorpej ">") == FALSE)
209 1.1 thorpej return (FALSE);
210 1.1 thorpej break;
211 1.1 thorpej case '&':
212 1.1 thorpej if (_prop_object_externalize_append_cstring(ctx,
213 1.1 thorpej "&") == FALSE)
214 1.1 thorpej return (FALSE);
215 1.1 thorpej break;
216 1.1 thorpej default:
217 1.1 thorpej if (_prop_object_externalize_append_char(ctx,
218 1.1 thorpej (unsigned char) *cp) == FALSE)
219 1.1 thorpej return (FALSE);
220 1.1 thorpej break;
221 1.1 thorpej }
222 1.1 thorpej cp++;
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej return (TRUE);
226 1.1 thorpej }
227 1.1 thorpej
228 1.1 thorpej #define BUF_EXPAND 256
229 1.1 thorpej
230 1.1 thorpej /*
231 1.1 thorpej * _prop_object_externalize_append_char --
232 1.1 thorpej * Append a single character to the externalize buffer.
233 1.1 thorpej */
234 1.1 thorpej boolean_t
235 1.1 thorpej _prop_object_externalize_append_char(
236 1.1 thorpej struct _prop_object_externalize_context *ctx, unsigned char c)
237 1.1 thorpej {
238 1.1 thorpej
239 1.1 thorpej _PROP_ASSERT(ctx->poec_capacity != 0);
240 1.1 thorpej _PROP_ASSERT(ctx->poec_buf != NULL);
241 1.1 thorpej _PROP_ASSERT(ctx->poec_len <= ctx->poec_capacity);
242 1.1 thorpej
243 1.1 thorpej if (ctx->poec_len == ctx->poec_capacity) {
244 1.2 thorpej char *cp = _PROP_REALLOC(ctx->poec_buf,
245 1.2 thorpej ctx->poec_capacity + BUF_EXPAND,
246 1.2 thorpej M_TEMP);
247 1.1 thorpej if (cp == NULL)
248 1.1 thorpej return (FALSE);
249 1.1 thorpej ctx->poec_capacity = ctx->poec_capacity + BUF_EXPAND;
250 1.1 thorpej ctx->poec_buf = cp;
251 1.1 thorpej }
252 1.1 thorpej
253 1.1 thorpej ctx->poec_buf[ctx->poec_len++] = c;
254 1.1 thorpej
255 1.1 thorpej return (TRUE);
256 1.1 thorpej }
257 1.1 thorpej
258 1.1 thorpej /*
259 1.4 thorpej * _prop_object_externalize_header --
260 1.4 thorpej * Append the standard XML header to the externalize buffer.
261 1.4 thorpej */
262 1.4 thorpej boolean_t
263 1.4 thorpej _prop_object_externalize_header(struct _prop_object_externalize_context *ctx)
264 1.4 thorpej {
265 1.4 thorpej static const char _plist_xml_header[] =
266 1.4 thorpej "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
267 1.4 thorpej "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
268 1.4 thorpej
269 1.4 thorpej if (_prop_object_externalize_append_cstring(ctx,
270 1.4 thorpej _plist_xml_header) == FALSE ||
271 1.4 thorpej _prop_object_externalize_start_tag(ctx,
272 1.4 thorpej "plist version=\"1.0\"") == FALSE ||
273 1.4 thorpej _prop_object_externalize_append_char(ctx, '\n') == FALSE)
274 1.4 thorpej return (FALSE);
275 1.4 thorpej
276 1.4 thorpej return (TRUE);
277 1.4 thorpej }
278 1.4 thorpej
279 1.4 thorpej /*
280 1.4 thorpej * _prop_object_externalize_footer --
281 1.4 thorpej * Append the standard XML footer to the externalize buffer. This
282 1.4 thorpej * also NUL-terminates the buffer.
283 1.4 thorpej */
284 1.4 thorpej boolean_t
285 1.4 thorpej _prop_object_externalize_footer(struct _prop_object_externalize_context *ctx)
286 1.4 thorpej {
287 1.4 thorpej
288 1.4 thorpej if (_prop_object_externalize_end_tag(ctx, "plist") == FALSE ||
289 1.4 thorpej _prop_object_externalize_append_char(ctx, '\0') == FALSE)
290 1.4 thorpej return (FALSE);
291 1.4 thorpej
292 1.4 thorpej return (TRUE);
293 1.4 thorpej }
294 1.4 thorpej
295 1.4 thorpej /*
296 1.1 thorpej * _prop_object_externalize_context_alloc --
297 1.1 thorpej * Allocate an externalize context.
298 1.1 thorpej */
299 1.1 thorpej struct _prop_object_externalize_context *
300 1.1 thorpej _prop_object_externalize_context_alloc(void)
301 1.1 thorpej {
302 1.1 thorpej struct _prop_object_externalize_context *ctx;
303 1.1 thorpej
304 1.1 thorpej ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
305 1.1 thorpej if (ctx != NULL) {
306 1.1 thorpej ctx->poec_buf = _PROP_MALLOC(BUF_EXPAND, M_TEMP);
307 1.1 thorpej if (ctx->poec_buf == NULL) {
308 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
309 1.1 thorpej return (NULL);
310 1.1 thorpej }
311 1.1 thorpej ctx->poec_len = 0;
312 1.1 thorpej ctx->poec_capacity = BUF_EXPAND;
313 1.1 thorpej ctx->poec_depth = 0;
314 1.1 thorpej }
315 1.1 thorpej return (ctx);
316 1.1 thorpej }
317 1.1 thorpej
318 1.1 thorpej /*
319 1.1 thorpej * _prop_object_externalize_context_free --
320 1.1 thorpej * Free an externalize context.
321 1.1 thorpej */
322 1.1 thorpej void
323 1.1 thorpej _prop_object_externalize_context_free(
324 1.1 thorpej struct _prop_object_externalize_context *ctx)
325 1.1 thorpej {
326 1.1 thorpej
327 1.1 thorpej /* Buffer is always freed by the caller. */
328 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
329 1.1 thorpej }
330 1.1 thorpej
331 1.1 thorpej /*
332 1.1 thorpej * _prop_object_internalize_skip_comment --
333 1.1 thorpej * Skip the body and end tag of a comment.
334 1.1 thorpej */
335 1.1 thorpej static boolean_t
336 1.1 thorpej _prop_object_internalize_skip_comment(
337 1.1 thorpej struct _prop_object_internalize_context *ctx)
338 1.1 thorpej {
339 1.1 thorpej const char *cp = ctx->poic_cp;
340 1.1 thorpej
341 1.1 thorpej while (!_PROP_EOF(*cp)) {
342 1.1 thorpej if (cp[0] == '-' &&
343 1.1 thorpej cp[1] == '-' &&
344 1.1 thorpej cp[2] == '>') {
345 1.1 thorpej ctx->poic_cp = cp + 3;
346 1.1 thorpej return (TRUE);
347 1.1 thorpej }
348 1.1 thorpej cp++;
349 1.1 thorpej }
350 1.1 thorpej
351 1.1 thorpej return (FALSE); /* ran out of buffer */
352 1.1 thorpej }
353 1.1 thorpej
354 1.1 thorpej /*
355 1.1 thorpej * _prop_object_internalize_find_tag --
356 1.1 thorpej * Find the next tag in an XML stream. Optionally compare the found
357 1.1 thorpej * tag to an expected tag name. State of the context is undefined
358 1.1 thorpej * if this routine returns FALSE. Upon success, the context points
359 1.1 thorpej * to the first octet after the tag.
360 1.1 thorpej */
361 1.1 thorpej boolean_t
362 1.1 thorpej _prop_object_internalize_find_tag(struct _prop_object_internalize_context *ctx,
363 1.1 thorpej const char *tag, _prop_tag_type_t type)
364 1.1 thorpej {
365 1.1 thorpej const char *cp;
366 1.1 thorpej size_t taglen;
367 1.1 thorpej
368 1.1 thorpej if (tag != NULL)
369 1.1 thorpej taglen = strlen(tag);
370 1.1 thorpej else
371 1.1 thorpej taglen = 0;
372 1.1 thorpej
373 1.1 thorpej start_over:
374 1.1 thorpej cp = ctx->poic_cp;
375 1.1 thorpej
376 1.1 thorpej /*
377 1.1 thorpej * Find the start of the tag.
378 1.1 thorpej */
379 1.1 thorpej while (_PROP_ISSPACE(*cp))
380 1.1 thorpej cp++;
381 1.1 thorpej if (_PROP_EOF(*cp))
382 1.1 thorpej return (FALSE);
383 1.1 thorpej
384 1.1 thorpej if (*cp != '<')
385 1.1 thorpej return (FALSE);
386 1.1 thorpej
387 1.1 thorpej ctx->poic_tag_start = cp++;
388 1.1 thorpej if (_PROP_EOF(*cp))
389 1.1 thorpej return (FALSE);
390 1.1 thorpej
391 1.1 thorpej if (*cp == '!') {
392 1.1 thorpej if (cp[1] != '-' || cp[2] != '-')
393 1.1 thorpej return (FALSE);
394 1.1 thorpej /*
395 1.1 thorpej * Comment block -- only allowed if we are allowed to
396 1.1 thorpej * return a start tag.
397 1.1 thorpej */
398 1.1 thorpej if (type == _PROP_TAG_TYPE_END)
399 1.1 thorpej return (FALSE);
400 1.1 thorpej ctx->poic_cp = cp + 3;
401 1.1 thorpej if (_prop_object_internalize_skip_comment(ctx) == FALSE)
402 1.1 thorpej return (FALSE);
403 1.1 thorpej goto start_over;
404 1.1 thorpej }
405 1.1 thorpej
406 1.1 thorpej if (*cp == '/') {
407 1.1 thorpej if (type != _PROP_TAG_TYPE_END &&
408 1.1 thorpej type != _PROP_TAG_TYPE_EITHER)
409 1.1 thorpej return (FALSE);
410 1.1 thorpej cp++;
411 1.1 thorpej if (_PROP_EOF(*cp))
412 1.1 thorpej return (FALSE);
413 1.1 thorpej ctx->poic_tag_type = _PROP_TAG_TYPE_END;
414 1.1 thorpej } else {
415 1.1 thorpej if (type != _PROP_TAG_TYPE_START &&
416 1.1 thorpej type != _PROP_TAG_TYPE_EITHER)
417 1.1 thorpej return (FALSE);
418 1.1 thorpej ctx->poic_tag_type = _PROP_TAG_TYPE_START;
419 1.1 thorpej }
420 1.1 thorpej
421 1.1 thorpej ctx->poic_tagname = cp;
422 1.1 thorpej
423 1.1 thorpej while (!_PROP_ISSPACE(*cp) && *cp != '/' && *cp != '>')
424 1.1 thorpej cp++;
425 1.1 thorpej if (_PROP_EOF(*cp))
426 1.1 thorpej return (FALSE);
427 1.1 thorpej
428 1.1 thorpej ctx->poic_tagname_len = cp - ctx->poic_tagname;
429 1.1 thorpej
430 1.1 thorpej /* Make sure this is the tag we're looking for. */
431 1.1 thorpej if (tag != NULL &&
432 1.1 thorpej (taglen != ctx->poic_tagname_len ||
433 1.1 thorpej memcmp(tag, ctx->poic_tagname, taglen) != 0))
434 1.1 thorpej return (FALSE);
435 1.1 thorpej
436 1.1 thorpej /* Check for empty tag. */
437 1.1 thorpej if (*cp == '/') {
438 1.1 thorpej if (ctx->poic_tag_type != _PROP_TAG_TYPE_START)
439 1.1 thorpej return(FALSE); /* only valid on start tags */
440 1.1 thorpej ctx->poic_is_empty_element = TRUE;
441 1.1 thorpej cp++;
442 1.1 thorpej if (_PROP_EOF(*cp) || *cp != '>')
443 1.1 thorpej return (FALSE);
444 1.1 thorpej } else
445 1.1 thorpej ctx->poic_is_empty_element = FALSE;
446 1.1 thorpej
447 1.1 thorpej /* Easy case of no arguments. */
448 1.1 thorpej if (*cp == '>') {
449 1.1 thorpej ctx->poic_tagattr = NULL;
450 1.1 thorpej ctx->poic_tagattr_len = 0;
451 1.1 thorpej ctx->poic_tagattrval = NULL;
452 1.1 thorpej ctx->poic_tagattrval_len = 0;
453 1.1 thorpej ctx->poic_cp = cp + 1;
454 1.1 thorpej return (TRUE);
455 1.1 thorpej }
456 1.1 thorpej
457 1.1 thorpej _PROP_ASSERT(!_PROP_EOF(*cp));
458 1.1 thorpej cp++;
459 1.1 thorpej if (_PROP_EOF(*cp))
460 1.1 thorpej return (FALSE);
461 1.1 thorpej
462 1.1 thorpej while (_PROP_ISSPACE(*cp))
463 1.1 thorpej cp++;
464 1.1 thorpej if (_PROP_EOF(*cp))
465 1.1 thorpej return (FALSE);
466 1.1 thorpej
467 1.1 thorpej ctx->poic_tagattr = cp;
468 1.1 thorpej
469 1.1 thorpej while (!_PROP_ISSPACE(*cp) && *cp != '=')
470 1.1 thorpej cp++;
471 1.1 thorpej if (_PROP_EOF(*cp))
472 1.1 thorpej return (FALSE);
473 1.1 thorpej
474 1.1 thorpej ctx->poic_tagattr_len = cp - ctx->poic_tagattr;
475 1.1 thorpej
476 1.1 thorpej cp++;
477 1.1 thorpej if (*cp != '\"')
478 1.1 thorpej return (FALSE);
479 1.1 thorpej cp++;
480 1.1 thorpej if (_PROP_EOF(*cp))
481 1.1 thorpej return (FALSE);
482 1.1 thorpej
483 1.1 thorpej ctx->poic_tagattrval = cp;
484 1.1 thorpej while (*cp != '\"')
485 1.1 thorpej cp++;
486 1.1 thorpej if (_PROP_EOF(*cp))
487 1.1 thorpej return (FALSE);
488 1.1 thorpej ctx->poic_tagattrval_len = cp - ctx->poic_tagattrval;
489 1.1 thorpej
490 1.1 thorpej cp++;
491 1.1 thorpej if (*cp != '>')
492 1.1 thorpej return (FALSE);
493 1.1 thorpej
494 1.1 thorpej ctx->poic_cp = cp + 1;
495 1.1 thorpej return (TRUE);
496 1.1 thorpej }
497 1.1 thorpej
498 1.1 thorpej /*
499 1.1 thorpej * _prop_object_internalize_decode_string --
500 1.1 thorpej * Decode an encoded string.
501 1.1 thorpej */
502 1.1 thorpej boolean_t
503 1.1 thorpej _prop_object_internalize_decode_string(
504 1.1 thorpej struct _prop_object_internalize_context *ctx,
505 1.1 thorpej char *target, size_t targsize, size_t *sizep,
506 1.1 thorpej const char **cpp)
507 1.1 thorpej {
508 1.1 thorpej const char *src;
509 1.1 thorpej size_t tarindex;
510 1.1 thorpej char c;
511 1.1 thorpej
512 1.1 thorpej tarindex = 0;
513 1.1 thorpej src = ctx->poic_cp;
514 1.1 thorpej
515 1.1 thorpej for (;;) {
516 1.1 thorpej if (_PROP_EOF(*src))
517 1.1 thorpej return (FALSE);
518 1.1 thorpej if (*src == '<') {
519 1.1 thorpej break;
520 1.1 thorpej }
521 1.1 thorpej
522 1.1 thorpej if ((c = *src) == '&') {
523 1.1 thorpej if (src[1] == 'a' &&
524 1.1 thorpej src[2] == 'm' &&
525 1.1 thorpej src[3] == 'p' &&
526 1.1 thorpej src[4] == ';') {
527 1.1 thorpej c = '&';
528 1.1 thorpej src += 5;
529 1.1 thorpej } else if (src[1] == 'l' &&
530 1.1 thorpej src[2] == 't' &&
531 1.1 thorpej src[3] == ';') {
532 1.1 thorpej c = '<';
533 1.1 thorpej src += 4;
534 1.1 thorpej } else if (src[1] == 'g' &&
535 1.1 thorpej src[2] == 't' &&
536 1.1 thorpej src[3] == ';') {
537 1.1 thorpej c = '>';
538 1.1 thorpej src += 4;
539 1.1 thorpej } else if (src[1] == 'a' &&
540 1.1 thorpej src[2] == 'p' &&
541 1.1 thorpej src[3] == 'o' &&
542 1.1 thorpej src[4] == 's' &&
543 1.1 thorpej src[5] == ';') {
544 1.1 thorpej c = '\'';
545 1.1 thorpej src += 6;
546 1.1 thorpej } else if (src[1] == 'q' &&
547 1.1 thorpej src[2] == 'u' &&
548 1.1 thorpej src[3] == 'o' &&
549 1.1 thorpej src[4] == 't' &&
550 1.1 thorpej src[5] == ';') {
551 1.1 thorpej c = '\"';
552 1.1 thorpej src += 6;
553 1.1 thorpej } else
554 1.1 thorpej return (FALSE);
555 1.1 thorpej } else
556 1.1 thorpej src++;
557 1.1 thorpej if (target) {
558 1.1 thorpej if (tarindex >= targsize)
559 1.1 thorpej return (FALSE);
560 1.1 thorpej target[tarindex] = c;
561 1.1 thorpej }
562 1.1 thorpej tarindex++;
563 1.1 thorpej }
564 1.1 thorpej
565 1.1 thorpej _PROP_ASSERT(*src == '<');
566 1.1 thorpej if (sizep != NULL)
567 1.1 thorpej *sizep = tarindex;
568 1.1 thorpej if (cpp != NULL)
569 1.1 thorpej *cpp = src;
570 1.1 thorpej
571 1.1 thorpej return (TRUE);
572 1.1 thorpej }
573 1.1 thorpej
574 1.1 thorpej /*
575 1.1 thorpej * _prop_object_internalize_match --
576 1.1 thorpej * Returns true if the two character streams match.
577 1.1 thorpej */
578 1.1 thorpej boolean_t
579 1.1 thorpej _prop_object_internalize_match(const char *str1, size_t len1,
580 1.1 thorpej const char *str2, size_t len2)
581 1.1 thorpej {
582 1.1 thorpej
583 1.1 thorpej return (len1 == len2 && memcmp(str1, str2, len1) == 0);
584 1.1 thorpej }
585 1.1 thorpej
586 1.1 thorpej #define INTERNALIZER(t, f) \
587 1.1 thorpej { t, sizeof(t) - 1, f }
588 1.1 thorpej
589 1.1 thorpej static const struct _prop_object_internalizer {
590 1.1 thorpej const char *poi_tag;
591 1.1 thorpej size_t poi_taglen;
592 1.1 thorpej prop_object_t (*poi_intern)(
593 1.1 thorpej struct _prop_object_internalize_context *);
594 1.1 thorpej } _prop_object_internalizer_table[] = {
595 1.1 thorpej INTERNALIZER("array", _prop_array_internalize),
596 1.1 thorpej
597 1.1 thorpej INTERNALIZER("true", _prop_bool_internalize),
598 1.1 thorpej INTERNALIZER("false", _prop_bool_internalize),
599 1.1 thorpej
600 1.1 thorpej INTERNALIZER("data", _prop_data_internalize),
601 1.1 thorpej
602 1.1 thorpej INTERNALIZER("dict", _prop_dictionary_internalize),
603 1.1 thorpej
604 1.1 thorpej INTERNALIZER("integer", _prop_number_internalize),
605 1.1 thorpej
606 1.1 thorpej INTERNALIZER("string", _prop_string_internalize),
607 1.1 thorpej
608 1.5 christos { 0, 0, NULL }
609 1.1 thorpej };
610 1.1 thorpej
611 1.1 thorpej #undef INTERNALIZER
612 1.1 thorpej
613 1.1 thorpej /*
614 1.1 thorpej * _prop_object_internalize_by_tag --
615 1.1 thorpej * Determine the object type from the tag in the context and
616 1.1 thorpej * internalize it.
617 1.1 thorpej */
618 1.1 thorpej prop_object_t
619 1.1 thorpej _prop_object_internalize_by_tag(struct _prop_object_internalize_context *ctx)
620 1.1 thorpej {
621 1.1 thorpej const struct _prop_object_internalizer *poi;
622 1.1 thorpej
623 1.1 thorpej for (poi = _prop_object_internalizer_table;
624 1.1 thorpej poi->poi_tag != NULL; poi++) {
625 1.1 thorpej if (_prop_object_internalize_match(ctx->poic_tagname,
626 1.1 thorpej ctx->poic_tagname_len,
627 1.1 thorpej poi->poi_tag,
628 1.1 thorpej poi->poi_taglen))
629 1.1 thorpej return ((*poi->poi_intern)(ctx));
630 1.1 thorpej }
631 1.1 thorpej
632 1.1 thorpej return (NULL);
633 1.1 thorpej }
634 1.1 thorpej
635 1.1 thorpej /*
636 1.1 thorpej * _prop_object_internalize_context_alloc --
637 1.1 thorpej * Allocate an internalize context.
638 1.1 thorpej */
639 1.1 thorpej struct _prop_object_internalize_context *
640 1.1 thorpej _prop_object_internalize_context_alloc(const char *xml)
641 1.1 thorpej {
642 1.1 thorpej struct _prop_object_internalize_context *ctx;
643 1.1 thorpej
644 1.1 thorpej ctx = _PROP_MALLOC(sizeof(struct _prop_object_internalize_context),
645 1.1 thorpej M_TEMP);
646 1.1 thorpej if (ctx == NULL)
647 1.1 thorpej return (NULL);
648 1.1 thorpej
649 1.1 thorpej ctx->poic_xml = ctx->poic_cp = xml;
650 1.1 thorpej
651 1.1 thorpej /*
652 1.1 thorpej * Skip any whitespace and XML preamble stuff that we don't
653 1.1 thorpej * know about / care about.
654 1.1 thorpej */
655 1.1 thorpej for (;;) {
656 1.1 thorpej while (_PROP_ISSPACE(*xml))
657 1.1 thorpej xml++;
658 1.1 thorpej if (_PROP_EOF(*xml) || *xml != '<')
659 1.1 thorpej goto bad;
660 1.1 thorpej
661 1.1 thorpej #define MATCH(str) (memcmp(&xml[1], str, sizeof(str) - 1) == 0)
662 1.1 thorpej
663 1.1 thorpej /*
664 1.1 thorpej * Skip over the XML preamble that Apple XML property
665 1.1 thorpej * lists usually include at the top of the file.
666 1.1 thorpej */
667 1.1 thorpej if (MATCH("?xml ") ||
668 1.1 thorpej MATCH("!DOCTYPE plist")) {
669 1.1 thorpej while (*xml != '>' && !_PROP_EOF(*xml))
670 1.1 thorpej xml++;
671 1.1 thorpej if (_PROP_EOF(*xml))
672 1.1 thorpej goto bad;
673 1.1 thorpej xml++; /* advance past the '>' */
674 1.1 thorpej continue;
675 1.1 thorpej }
676 1.1 thorpej
677 1.1 thorpej if (MATCH("<!--")) {
678 1.1 thorpej ctx->poic_cp = xml + 4;
679 1.1 thorpej if (_prop_object_internalize_skip_comment(ctx) == FALSE)
680 1.1 thorpej goto bad;
681 1.1 thorpej xml = ctx->poic_cp;
682 1.1 thorpej continue;
683 1.1 thorpej }
684 1.1 thorpej
685 1.1 thorpej #undef MATCH
686 1.1 thorpej
687 1.1 thorpej /*
688 1.1 thorpej * We don't think we should skip it, so let's hope we can
689 1.1 thorpej * parse it.
690 1.1 thorpej */
691 1.1 thorpej break;
692 1.1 thorpej }
693 1.1 thorpej
694 1.1 thorpej ctx->poic_cp = xml;
695 1.1 thorpej return (ctx);
696 1.1 thorpej bad:
697 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
698 1.1 thorpej return (NULL);
699 1.1 thorpej }
700 1.1 thorpej
701 1.1 thorpej /*
702 1.1 thorpej * _prop_object_internalize_context_free --
703 1.1 thorpej * Free an internalize context.
704 1.1 thorpej */
705 1.1 thorpej void
706 1.1 thorpej _prop_object_internalize_context_free(
707 1.1 thorpej struct _prop_object_internalize_context *ctx)
708 1.1 thorpej {
709 1.1 thorpej
710 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
711 1.1 thorpej }
712 1.1 thorpej
713 1.4 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
714 1.4 thorpej /*
715 1.4 thorpej * _prop_object_externalize_file_dirname --
716 1.4 thorpej * dirname(3), basically. We have to roll our own because the
717 1.4 thorpej * system dirname(3) isn't reentrant.
718 1.4 thorpej */
719 1.4 thorpej static void
720 1.4 thorpej _prop_object_externalize_file_dirname(const char *path, char *result)
721 1.4 thorpej {
722 1.4 thorpej const char *lastp;
723 1.4 thorpej size_t len;
724 1.4 thorpej
725 1.4 thorpej /*
726 1.4 thorpej * If `path' is a NULL pointer or points to an empty string,
727 1.4 thorpej * return ".".
728 1.4 thorpej */
729 1.4 thorpej if (path == NULL || *path == '\0')
730 1.4 thorpej goto singledot;
731 1.4 thorpej
732 1.4 thorpej /* String trailing slashes, if any. */
733 1.4 thorpej lastp = path + strlen(path) - 1;
734 1.4 thorpej while (lastp != path && *lastp == '/')
735 1.4 thorpej lastp--;
736 1.4 thorpej
737 1.4 thorpej /* Terminate path at the last occurrence of '/'. */
738 1.4 thorpej do {
739 1.4 thorpej if (*lastp == '/') {
740 1.4 thorpej /* Strip trailing slashes, if any. */
741 1.4 thorpej while (lastp != path && *lastp == '/')
742 1.4 thorpej lastp--;
743 1.4 thorpej
744 1.4 thorpej /* ...and copy the result into the result buffer. */
745 1.4 thorpej len = (lastp - path) + 1 /* last char */;
746 1.4 thorpej if (len > (PATH_MAX - 1))
747 1.4 thorpej len = PATH_MAX - 1;
748 1.4 thorpej
749 1.4 thorpej memcpy(result, path, len);
750 1.4 thorpej result[len] = '\0';
751 1.4 thorpej return;
752 1.4 thorpej }
753 1.4 thorpej } while (--lastp >= path);
754 1.4 thorpej
755 1.4 thorpej /* No /'s found, return ".". */
756 1.4 thorpej singledot:
757 1.4 thorpej strcpy(result, ".");
758 1.4 thorpej }
759 1.4 thorpej
760 1.4 thorpej /*
761 1.4 thorpej * _prop_object_externalize_write_file --
762 1.4 thorpej * Write an externalized dictionary to the specified file.
763 1.4 thorpej * The file is written atomically from the caller's perspective,
764 1.4 thorpej * and the mode set to 0666 modified by the caller's umask.
765 1.4 thorpej */
766 1.4 thorpej boolean_t
767 1.4 thorpej _prop_object_externalize_write_file(const char *fname, const char *xml,
768 1.4 thorpej size_t len)
769 1.4 thorpej {
770 1.4 thorpej char tname[PATH_MAX];
771 1.4 thorpej int fd;
772 1.4 thorpej int save_errno;
773 1.4 thorpej
774 1.4 thorpej if (len > SSIZE_MAX) {
775 1.4 thorpej errno = EFBIG;
776 1.4 thorpej return (FALSE);
777 1.4 thorpej }
778 1.4 thorpej
779 1.4 thorpej /*
780 1.4 thorpej * Get the directory name where the file is to be written
781 1.4 thorpej * and create the temporary file.
782 1.4 thorpej *
783 1.4 thorpej * We don't use mkstemp() because mkstemp() always creates the
784 1.4 thorpej * file with mode 0600. We do, however, use mktemp() safely.
785 1.4 thorpej */
786 1.4 thorpej again:
787 1.4 thorpej _prop_object_externalize_file_dirname(fname, tname);
788 1.4 thorpej if (strlcat(tname, "/.plistXXXXXX", sizeof(tname)) >= sizeof(tname)) {
789 1.4 thorpej errno = ENAMETOOLONG;
790 1.4 thorpej return (FALSE);
791 1.4 thorpej }
792 1.4 thorpej if (mktemp(tname) == NULL)
793 1.4 thorpej return (FALSE);
794 1.4 thorpej if ((fd = open(tname, O_CREAT|O_RDWR|O_EXCL, 0666)) == -1) {
795 1.4 thorpej if (errno == EEXIST)
796 1.4 thorpej goto again;
797 1.4 thorpej return (FALSE);
798 1.4 thorpej }
799 1.4 thorpej
800 1.4 thorpej if (write(fd, xml, len) != (ssize_t)len)
801 1.4 thorpej goto bad;
802 1.4 thorpej
803 1.4 thorpej if (fsync(fd) == -1)
804 1.4 thorpej goto bad;
805 1.4 thorpej
806 1.4 thorpej (void) close(fd);
807 1.4 thorpej fd = -1;
808 1.4 thorpej
809 1.4 thorpej if (rename(tname, fname) == -1)
810 1.4 thorpej goto bad;
811 1.4 thorpej
812 1.4 thorpej return (TRUE);
813 1.4 thorpej
814 1.4 thorpej bad:
815 1.4 thorpej save_errno = errno;
816 1.4 thorpej if (fd != -1)
817 1.4 thorpej (void) close(fd);
818 1.4 thorpej (void) unlink(tname);
819 1.4 thorpej errno = save_errno;
820 1.4 thorpej return (FALSE);
821 1.4 thorpej }
822 1.4 thorpej
823 1.4 thorpej /*
824 1.4 thorpej * _prop_object_internalize_map_file --
825 1.4 thorpej * Map a file for the purpose of internalizing it.
826 1.4 thorpej */
827 1.4 thorpej struct _prop_object_internalize_mapped_file *
828 1.4 thorpej _prop_object_internalize_map_file(const char *fname)
829 1.4 thorpej {
830 1.4 thorpej struct stat sb;
831 1.4 thorpej struct _prop_object_internalize_mapped_file *mf;
832 1.4 thorpej size_t pgsize = sysconf(_SC_PAGESIZE);
833 1.4 thorpej size_t pgmask = pgsize - 1;
834 1.4 thorpej boolean_t need_guard = FALSE;
835 1.4 thorpej int fd;
836 1.4 thorpej
837 1.4 thorpej mf = _PROP_MALLOC(sizeof(*mf), M_TEMP);
838 1.4 thorpej if (mf == NULL)
839 1.4 thorpej return (NULL);
840 1.4 thorpej
841 1.4 thorpej fd = open(fname, O_RDONLY, 0400);
842 1.4 thorpej if (fd == -1) {
843 1.4 thorpej _PROP_FREE(mf, M_TEMP);
844 1.4 thorpej return (NULL);
845 1.4 thorpej }
846 1.4 thorpej
847 1.4 thorpej if (fstat(fd, &sb) == -1) {
848 1.4 thorpej (void) close(fd);
849 1.4 thorpej _PROP_FREE(mf, M_TEMP);
850 1.4 thorpej return (NULL);
851 1.4 thorpej }
852 1.4 thorpej mf->poimf_mapsize = ((size_t)sb.st_size + pgmask) & ~pgmask;
853 1.4 thorpej if (mf->poimf_mapsize < sb.st_size) {
854 1.4 thorpej (void) close(fd);
855 1.4 thorpej _PROP_FREE(mf, M_TEMP);
856 1.4 thorpej return (NULL);
857 1.4 thorpej }
858 1.4 thorpej
859 1.4 thorpej /*
860 1.4 thorpej * If the file length is an integral number of pages, then we
861 1.4 thorpej * need to map a guard page at the end in order to provide the
862 1.4 thorpej * necessary NUL-termination of the buffer.
863 1.4 thorpej */
864 1.4 thorpej if ((sb.st_size & pgmask) == 0)
865 1.4 thorpej need_guard = TRUE;
866 1.4 thorpej
867 1.4 thorpej mf->poimf_xml = mmap(NULL, need_guard ? mf->poimf_mapsize + pgsize
868 1.4 thorpej : mf->poimf_mapsize,
869 1.4 thorpej PROT_READ, MAP_FILE|MAP_SHARED, fd, (off_t)0);
870 1.4 thorpej (void) close(fd);
871 1.4 thorpej if (mf->poimf_xml == MAP_FAILED) {
872 1.4 thorpej _PROP_FREE(mf, M_TEMP);
873 1.4 thorpej return (NULL);
874 1.4 thorpej }
875 1.4 thorpej (void) madvise(mf->poimf_xml, mf->poimf_mapsize, MADV_SEQUENTIAL);
876 1.4 thorpej
877 1.4 thorpej if (need_guard) {
878 1.4 thorpej if (mmap(mf->poimf_xml + mf->poimf_mapsize,
879 1.4 thorpej pgsize, PROT_READ,
880 1.4 thorpej MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1,
881 1.4 thorpej (off_t)0) == MAP_FAILED) {
882 1.4 thorpej (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
883 1.4 thorpej _PROP_FREE(mf, M_TEMP);
884 1.4 thorpej return (NULL);
885 1.4 thorpej }
886 1.4 thorpej mf->poimf_mapsize += pgsize;
887 1.4 thorpej }
888 1.4 thorpej
889 1.4 thorpej return (mf);
890 1.4 thorpej }
891 1.4 thorpej
892 1.4 thorpej /*
893 1.4 thorpej * _prop_object_internalize_unmap_file --
894 1.4 thorpej * Unmap a file previously mapped for internalizing.
895 1.4 thorpej */
896 1.4 thorpej void
897 1.4 thorpej _prop_object_internalize_unmap_file(
898 1.4 thorpej struct _prop_object_internalize_mapped_file *mf)
899 1.4 thorpej {
900 1.4 thorpej
901 1.4 thorpej (void) madvise(mf->poimf_xml, mf->poimf_mapsize, MADV_DONTNEED);
902 1.4 thorpej (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
903 1.4 thorpej _PROP_FREE(mf, M_TEMP);
904 1.4 thorpej }
905 1.4 thorpej #endif /* !_KERNEL && !_STANDALONE */
906 1.4 thorpej
907 1.1 thorpej /*
908 1.1 thorpej * Retain / release serialization --
909 1.1 thorpej *
910 1.1 thorpej * Eventually we would like to use atomic operations. But until we have
911 1.1 thorpej * an MI API for them that is common to userland and the kernel, we will
912 1.1 thorpej * use a lock instead.
913 1.1 thorpej *
914 1.1 thorpej * We use a single global mutex for all serialization. In the kernel, because
915 1.1 thorpej * we are still under a biglock, this will basically never contend (properties
916 1.1 thorpej * cannot be manipulated at interrupt level). In userland, this will cost
917 1.1 thorpej * nothing for single-threaded programs. For multi-threaded programs, there
918 1.1 thorpej * could be contention, but it probably won't cost that much unless the program
919 1.1 thorpej * makes heavy use of property lists.
920 1.1 thorpej */
921 1.6 thorpej _PROP_MUTEX_DECL_STATIC(_prop_refcnt_mutex)
922 1.3 thorpej #define _PROP_REFCNT_LOCK() _PROP_MUTEX_LOCK(_prop_refcnt_mutex)
923 1.3 thorpej #define _PROP_REFCNT_UNLOCK() _PROP_MUTEX_UNLOCK(_prop_refcnt_mutex)
924 1.1 thorpej
925 1.1 thorpej /*
926 1.1 thorpej * prop_object_retain --
927 1.1 thorpej * Increment the reference count on an object.
928 1.1 thorpej */
929 1.1 thorpej void
930 1.1 thorpej prop_object_retain(prop_object_t obj)
931 1.1 thorpej {
932 1.1 thorpej struct _prop_object *po = obj;
933 1.1 thorpej uint32_t ocnt;
934 1.1 thorpej
935 1.1 thorpej _PROP_REFCNT_LOCK();
936 1.1 thorpej ocnt = po->po_refcnt++;
937 1.1 thorpej _PROP_REFCNT_UNLOCK();
938 1.1 thorpej
939 1.1 thorpej _PROP_ASSERT(ocnt != 0xffffffffU);
940 1.1 thorpej }
941 1.1 thorpej
942 1.1 thorpej /*
943 1.1 thorpej * prop_object_release --
944 1.1 thorpej * Decrement the reference count on an object.
945 1.1 thorpej *
946 1.1 thorpej * Free the object if we are releasing the final
947 1.1 thorpej * reference.
948 1.1 thorpej */
949 1.1 thorpej void
950 1.1 thorpej prop_object_release(prop_object_t obj)
951 1.1 thorpej {
952 1.1 thorpej struct _prop_object *po = obj;
953 1.1 thorpej uint32_t ocnt;
954 1.1 thorpej
955 1.1 thorpej _PROP_REFCNT_LOCK();
956 1.1 thorpej ocnt = po->po_refcnt--;
957 1.1 thorpej _PROP_REFCNT_UNLOCK();
958 1.1 thorpej
959 1.1 thorpej _PROP_ASSERT(ocnt != 0);
960 1.1 thorpej if (ocnt == 1)
961 1.2 thorpej (*po->po_type->pot_free)(po);
962 1.1 thorpej }
963 1.1 thorpej
964 1.1 thorpej /*
965 1.1 thorpej * prop_object_type --
966 1.1 thorpej * Return the type of an object.
967 1.1 thorpej */
968 1.1 thorpej prop_type_t
969 1.1 thorpej prop_object_type(prop_object_t obj)
970 1.1 thorpej {
971 1.1 thorpej struct _prop_object *po = obj;
972 1.1 thorpej
973 1.4 thorpej if (obj == NULL)
974 1.4 thorpej return (PROP_TYPE_UNKNOWN);
975 1.4 thorpej
976 1.2 thorpej return (po->po_type->pot_type);
977 1.2 thorpej }
978 1.2 thorpej
979 1.2 thorpej /*
980 1.2 thorpej * prop_object_equals --
981 1.2 thorpej * Returns TRUE if thw two objects are equivalent.
982 1.2 thorpej */
983 1.2 thorpej boolean_t
984 1.2 thorpej prop_object_equals(prop_object_t obj1, prop_object_t obj2)
985 1.2 thorpej {
986 1.2 thorpej struct _prop_object *po1 = obj1;
987 1.2 thorpej struct _prop_object *po2 = obj2;
988 1.2 thorpej
989 1.2 thorpej if (po1->po_type != po2->po_type)
990 1.2 thorpej return (FALSE);
991 1.2 thorpej
992 1.2 thorpej return ((*po1->po_type->pot_equals)(obj1, obj2));
993 1.1 thorpej }
994 1.1 thorpej
995 1.1 thorpej /*
996 1.1 thorpej * prop_object_iterator_next --
997 1.1 thorpej * Return the next item during an iteration.
998 1.1 thorpej */
999 1.1 thorpej prop_object_t
1000 1.1 thorpej prop_object_iterator_next(prop_object_iterator_t pi)
1001 1.1 thorpej {
1002 1.1 thorpej
1003 1.1 thorpej return ((*pi->pi_next_object)(pi));
1004 1.1 thorpej }
1005 1.1 thorpej
1006 1.1 thorpej /*
1007 1.1 thorpej * prop_object_iterator_reset --
1008 1.1 thorpej * Reset the iterator to the first object so as to restart
1009 1.1 thorpej * iteration.
1010 1.1 thorpej */
1011 1.1 thorpej void
1012 1.1 thorpej prop_object_iterator_reset(prop_object_iterator_t pi)
1013 1.1 thorpej {
1014 1.1 thorpej
1015 1.1 thorpej (*pi->pi_reset)(pi);
1016 1.1 thorpej }
1017 1.1 thorpej
1018 1.1 thorpej /*
1019 1.1 thorpej * prop_object_iterator_release --
1020 1.1 thorpej * Release the object iterator.
1021 1.1 thorpej */
1022 1.1 thorpej void
1023 1.1 thorpej prop_object_iterator_release(prop_object_iterator_t pi)
1024 1.1 thorpej {
1025 1.1 thorpej
1026 1.1 thorpej prop_object_release(pi->pi_obj);
1027 1.1 thorpej _PROP_FREE(pi, M_TEMP);
1028 1.1 thorpej }
1029