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