1*86af3c87Sriastradh /* $NetBSD: prop_object.c,v 1.35 2022/08/07 23:49:46 riastradh Exp $ */
2774eb1a3Sthorpej
3774eb1a3Sthorpej /*-
4e835604cSjoerg * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5774eb1a3Sthorpej * All rights reserved.
6774eb1a3Sthorpej *
7774eb1a3Sthorpej * This code is derived from software contributed to The NetBSD Foundation
8774eb1a3Sthorpej * by Jason R. Thorpe.
9774eb1a3Sthorpej *
10774eb1a3Sthorpej * Redistribution and use in source and binary forms, with or without
11774eb1a3Sthorpej * modification, are permitted provided that the following conditions
12774eb1a3Sthorpej * are met:
13774eb1a3Sthorpej * 1. Redistributions of source code must retain the above copyright
14774eb1a3Sthorpej * notice, this list of conditions and the following disclaimer.
15774eb1a3Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
16774eb1a3Sthorpej * notice, this list of conditions and the following disclaimer in the
17774eb1a3Sthorpej * documentation and/or other materials provided with the distribution.
18774eb1a3Sthorpej *
19774eb1a3Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20774eb1a3Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21774eb1a3Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22774eb1a3Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23774eb1a3Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24774eb1a3Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25774eb1a3Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26774eb1a3Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27774eb1a3Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28774eb1a3Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29774eb1a3Sthorpej * POSSIBILITY OF SUCH DAMAGE.
30774eb1a3Sthorpej */
31774eb1a3Sthorpej
32774eb1a3Sthorpej #include "prop_object_impl.h"
33c303bcbeSpooka #include <prop/prop_object.h>
34c303bcbeSpooka
35c303bcbeSpooka #ifdef _PROP_NEED_REFCNT_MTX
36c303bcbeSpooka static pthread_mutex_t _prop_refcnt_mtx = PTHREAD_MUTEX_INITIALIZER;
37c303bcbeSpooka #endif /* _PROP_NEED_REFCNT_MTX */
38774eb1a3Sthorpej
39d21620b2Sthorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
40d21620b2Sthorpej #include <sys/mman.h>
41d21620b2Sthorpej #include <sys/stat.h>
42d21620b2Sthorpej #include <errno.h>
43d21620b2Sthorpej #include <fcntl.h>
44d21620b2Sthorpej #include <limits.h>
45d21620b2Sthorpej #include <unistd.h>
46d21620b2Sthorpej #endif
47d21620b2Sthorpej
48774eb1a3Sthorpej #ifdef _STANDALONE
49774eb1a3Sthorpej void *
_prop_standalone_calloc(size_t size)50774eb1a3Sthorpej _prop_standalone_calloc(size_t size)
51774eb1a3Sthorpej {
52774eb1a3Sthorpej void *rv;
53774eb1a3Sthorpej
54774eb1a3Sthorpej rv = alloc(size);
55774eb1a3Sthorpej if (rv != NULL)
56774eb1a3Sthorpej memset(rv, 0, size);
57774eb1a3Sthorpej
58774eb1a3Sthorpej return (rv);
59774eb1a3Sthorpej }
603e69f1b2Sthorpej
613e69f1b2Sthorpej void *
_prop_standalone_realloc(void * v,size_t size)623e69f1b2Sthorpej _prop_standalone_realloc(void *v, size_t size)
633e69f1b2Sthorpej {
643e69f1b2Sthorpej void *rv;
653e69f1b2Sthorpej
663e69f1b2Sthorpej rv = alloc(size);
673e69f1b2Sthorpej if (rv != NULL) {
683e69f1b2Sthorpej memcpy(rv, v, size); /* XXX */
693e69f1b2Sthorpej dealloc(v, 0); /* XXX */
703e69f1b2Sthorpej }
713e69f1b2Sthorpej
723e69f1b2Sthorpej return (rv);
733e69f1b2Sthorpej }
74774eb1a3Sthorpej #endif /* _STANDALONE */
75774eb1a3Sthorpej
76774eb1a3Sthorpej /*
77774eb1a3Sthorpej * _prop_object_init --
78774eb1a3Sthorpej * Initialize an object. Called when sub-classes create
79774eb1a3Sthorpej * an instance.
80774eb1a3Sthorpej */
81774eb1a3Sthorpej void
_prop_object_init(struct _prop_object * po,const struct _prop_object_type * pot)823e69f1b2Sthorpej _prop_object_init(struct _prop_object *po, const struct _prop_object_type *pot)
83774eb1a3Sthorpej {
84774eb1a3Sthorpej
853e69f1b2Sthorpej po->po_type = pot;
86774eb1a3Sthorpej po->po_refcnt = 1;
87774eb1a3Sthorpej }
88774eb1a3Sthorpej
89774eb1a3Sthorpej /*
90774eb1a3Sthorpej * _prop_object_fini --
91774eb1a3Sthorpej * Finalize an object. Called when sub-classes destroy
92774eb1a3Sthorpej * an instance.
93774eb1a3Sthorpej */
94d21620b2Sthorpej /*ARGSUSED*/
95774eb1a3Sthorpej void
_prop_object_fini(struct _prop_object * po _PROP_ARG_UNUSED)96d9fd2cbcSthorpej _prop_object_fini(struct _prop_object *po _PROP_ARG_UNUSED)
97774eb1a3Sthorpej {
98774eb1a3Sthorpej /* Nothing to do, currently. */
99774eb1a3Sthorpej }
100774eb1a3Sthorpej
101774eb1a3Sthorpej /*
102774eb1a3Sthorpej * _prop_object_externalize_start_tag --
103774eb1a3Sthorpej * Append an XML-style start tag to the externalize buffer.
104774eb1a3Sthorpej */
10504377267Sthorpej bool
_prop_object_externalize_start_tag(struct _prop_object_externalize_context * ctx,const char * tag)106774eb1a3Sthorpej _prop_object_externalize_start_tag(
107774eb1a3Sthorpej struct _prop_object_externalize_context *ctx, const char *tag)
108774eb1a3Sthorpej {
109774eb1a3Sthorpej unsigned int i;
110774eb1a3Sthorpej
111774eb1a3Sthorpej for (i = 0; i < ctx->poec_depth; i++) {
11204377267Sthorpej if (_prop_object_externalize_append_char(ctx, '\t') == false)
11304377267Sthorpej return (false);
114774eb1a3Sthorpej }
11504377267Sthorpej if (_prop_object_externalize_append_char(ctx, '<') == false ||
11604377267Sthorpej _prop_object_externalize_append_cstring(ctx, tag) == false ||
11704377267Sthorpej _prop_object_externalize_append_char(ctx, '>') == false)
11804377267Sthorpej return (false);
119774eb1a3Sthorpej
12004377267Sthorpej return (true);
121774eb1a3Sthorpej }
122774eb1a3Sthorpej
123774eb1a3Sthorpej /*
124774eb1a3Sthorpej * _prop_object_externalize_end_tag --
125774eb1a3Sthorpej * Append an XML-style end tag to the externalize buffer.
126774eb1a3Sthorpej */
12704377267Sthorpej bool
_prop_object_externalize_end_tag(struct _prop_object_externalize_context * ctx,const char * tag)128774eb1a3Sthorpej _prop_object_externalize_end_tag(
129774eb1a3Sthorpej struct _prop_object_externalize_context *ctx, const char *tag)
130774eb1a3Sthorpej {
131774eb1a3Sthorpej
13204377267Sthorpej if (_prop_object_externalize_append_char(ctx, '<') == false ||
13304377267Sthorpej _prop_object_externalize_append_char(ctx, '/') == false ||
13404377267Sthorpej _prop_object_externalize_append_cstring(ctx, tag) == false ||
13504377267Sthorpej _prop_object_externalize_append_char(ctx, '>') == false ||
13604377267Sthorpej _prop_object_externalize_append_char(ctx, '\n') == false)
13704377267Sthorpej return (false);
138774eb1a3Sthorpej
13904377267Sthorpej return (true);
140774eb1a3Sthorpej }
141774eb1a3Sthorpej
142774eb1a3Sthorpej /*
143774eb1a3Sthorpej * _prop_object_externalize_empty_tag --
144774eb1a3Sthorpej * Append an XML-style empty tag to the externalize buffer.
145774eb1a3Sthorpej */
14604377267Sthorpej bool
_prop_object_externalize_empty_tag(struct _prop_object_externalize_context * ctx,const char * tag)147774eb1a3Sthorpej _prop_object_externalize_empty_tag(
148774eb1a3Sthorpej struct _prop_object_externalize_context *ctx, const char *tag)
149774eb1a3Sthorpej {
150774eb1a3Sthorpej unsigned int i;
151774eb1a3Sthorpej
152774eb1a3Sthorpej for (i = 0; i < ctx->poec_depth; i++) {
15304377267Sthorpej if (_prop_object_externalize_append_char(ctx, '\t') == false)
15404377267Sthorpej return (false);
155774eb1a3Sthorpej }
156774eb1a3Sthorpej
15704377267Sthorpej if (_prop_object_externalize_append_char(ctx, '<') == false ||
15804377267Sthorpej _prop_object_externalize_append_cstring(ctx, tag) == false ||
15904377267Sthorpej _prop_object_externalize_append_char(ctx, '/') == false ||
16004377267Sthorpej _prop_object_externalize_append_char(ctx, '>') == false ||
16104377267Sthorpej _prop_object_externalize_append_char(ctx, '\n') == false)
16204377267Sthorpej return (false);
163774eb1a3Sthorpej
16404377267Sthorpej return (true);
165774eb1a3Sthorpej }
166774eb1a3Sthorpej
167774eb1a3Sthorpej /*
168774eb1a3Sthorpej * _prop_object_externalize_append_cstring --
169774eb1a3Sthorpej * Append a C string to the externalize buffer.
170774eb1a3Sthorpej */
17104377267Sthorpej bool
_prop_object_externalize_append_cstring(struct _prop_object_externalize_context * ctx,const char * cp)172774eb1a3Sthorpej _prop_object_externalize_append_cstring(
173774eb1a3Sthorpej struct _prop_object_externalize_context *ctx, const char *cp)
174774eb1a3Sthorpej {
175774eb1a3Sthorpej
176774eb1a3Sthorpej while (*cp != '\0') {
177774eb1a3Sthorpej if (_prop_object_externalize_append_char(ctx,
17804377267Sthorpej (unsigned char) *cp) == false)
17904377267Sthorpej return (false);
180774eb1a3Sthorpej cp++;
181774eb1a3Sthorpej }
182774eb1a3Sthorpej
18304377267Sthorpej return (true);
184774eb1a3Sthorpej }
185774eb1a3Sthorpej
186774eb1a3Sthorpej /*
187774eb1a3Sthorpej * _prop_object_externalize_append_encoded_cstring --
188774eb1a3Sthorpej * Append an encoded C string to the externalize buffer.
189774eb1a3Sthorpej */
19004377267Sthorpej bool
_prop_object_externalize_append_encoded_cstring(struct _prop_object_externalize_context * ctx,const char * cp)191774eb1a3Sthorpej _prop_object_externalize_append_encoded_cstring(
192774eb1a3Sthorpej struct _prop_object_externalize_context *ctx, const char *cp)
193774eb1a3Sthorpej {
194774eb1a3Sthorpej
195774eb1a3Sthorpej while (*cp != '\0') {
196774eb1a3Sthorpej switch (*cp) {
197774eb1a3Sthorpej case '<':
198774eb1a3Sthorpej if (_prop_object_externalize_append_cstring(ctx,
19904377267Sthorpej "<") == false)
20004377267Sthorpej return (false);
201774eb1a3Sthorpej break;
202774eb1a3Sthorpej case '>':
203774eb1a3Sthorpej if (_prop_object_externalize_append_cstring(ctx,
20404377267Sthorpej ">") == false)
20504377267Sthorpej return (false);
206774eb1a3Sthorpej break;
207774eb1a3Sthorpej case '&':
208774eb1a3Sthorpej if (_prop_object_externalize_append_cstring(ctx,
20904377267Sthorpej "&") == false)
21004377267Sthorpej return (false);
211774eb1a3Sthorpej break;
212774eb1a3Sthorpej default:
213774eb1a3Sthorpej if (_prop_object_externalize_append_char(ctx,
21404377267Sthorpej (unsigned char) *cp) == false)
21504377267Sthorpej return (false);
216774eb1a3Sthorpej break;
217774eb1a3Sthorpej }
218774eb1a3Sthorpej cp++;
219774eb1a3Sthorpej }
220774eb1a3Sthorpej
22104377267Sthorpej return (true);
222774eb1a3Sthorpej }
223774eb1a3Sthorpej
224ab821170Smartin #define BUF_EXPAND 256
225774eb1a3Sthorpej
226774eb1a3Sthorpej /*
227774eb1a3Sthorpej * _prop_object_externalize_append_char --
228774eb1a3Sthorpej * Append a single character to the externalize buffer.
229774eb1a3Sthorpej */
23004377267Sthorpej bool
_prop_object_externalize_append_char(struct _prop_object_externalize_context * ctx,unsigned char c)231774eb1a3Sthorpej _prop_object_externalize_append_char(
232774eb1a3Sthorpej struct _prop_object_externalize_context *ctx, unsigned char c)
233774eb1a3Sthorpej {
234774eb1a3Sthorpej
235774eb1a3Sthorpej _PROP_ASSERT(ctx->poec_capacity != 0);
236774eb1a3Sthorpej _PROP_ASSERT(ctx->poec_buf != NULL);
237774eb1a3Sthorpej _PROP_ASSERT(ctx->poec_len <= ctx->poec_capacity);
238774eb1a3Sthorpej
239774eb1a3Sthorpej if (ctx->poec_len == ctx->poec_capacity) {
2403e69f1b2Sthorpej char *cp = _PROP_REALLOC(ctx->poec_buf,
2413e69f1b2Sthorpej ctx->poec_capacity + BUF_EXPAND,
242774eb1a3Sthorpej M_TEMP);
243774eb1a3Sthorpej if (cp == NULL)
24404377267Sthorpej return (false);
245774eb1a3Sthorpej ctx->poec_capacity = ctx->poec_capacity + BUF_EXPAND;
246774eb1a3Sthorpej ctx->poec_buf = cp;
247774eb1a3Sthorpej }
248774eb1a3Sthorpej
249774eb1a3Sthorpej ctx->poec_buf[ctx->poec_len++] = c;
250774eb1a3Sthorpej
25104377267Sthorpej return (true);
252774eb1a3Sthorpej }
253774eb1a3Sthorpej
254774eb1a3Sthorpej /*
255d21620b2Sthorpej * _prop_object_externalize_header --
256d21620b2Sthorpej * Append the standard XML header to the externalize buffer.
257d21620b2Sthorpej */
25804377267Sthorpej bool
_prop_object_externalize_header(struct _prop_object_externalize_context * ctx)259d21620b2Sthorpej _prop_object_externalize_header(struct _prop_object_externalize_context *ctx)
260d21620b2Sthorpej {
261d21620b2Sthorpej static const char _plist_xml_header[] =
262d21620b2Sthorpej "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
263d21620b2Sthorpej "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
264d21620b2Sthorpej
265d21620b2Sthorpej if (_prop_object_externalize_append_cstring(ctx,
26604377267Sthorpej _plist_xml_header) == false ||
267d21620b2Sthorpej _prop_object_externalize_start_tag(ctx,
26804377267Sthorpej "plist version=\"1.0\"") == false ||
26904377267Sthorpej _prop_object_externalize_append_char(ctx, '\n') == false)
27004377267Sthorpej return (false);
271d21620b2Sthorpej
27204377267Sthorpej return (true);
273d21620b2Sthorpej }
274d21620b2Sthorpej
275d21620b2Sthorpej /*
276d21620b2Sthorpej * _prop_object_externalize_footer --
277d21620b2Sthorpej * Append the standard XML footer to the externalize buffer. This
278d21620b2Sthorpej * also NUL-terminates the buffer.
279d21620b2Sthorpej */
28004377267Sthorpej bool
_prop_object_externalize_footer(struct _prop_object_externalize_context * ctx)281d21620b2Sthorpej _prop_object_externalize_footer(struct _prop_object_externalize_context *ctx)
282d21620b2Sthorpej {
283d21620b2Sthorpej
28404377267Sthorpej if (_prop_object_externalize_end_tag(ctx, "plist") == false ||
28504377267Sthorpej _prop_object_externalize_append_char(ctx, '\0') == false)
28604377267Sthorpej return (false);
287d21620b2Sthorpej
28804377267Sthorpej return (true);
289d21620b2Sthorpej }
290d21620b2Sthorpej
291d21620b2Sthorpej /*
292774eb1a3Sthorpej * _prop_object_externalize_context_alloc --
293774eb1a3Sthorpej * Allocate an externalize context.
294774eb1a3Sthorpej */
295774eb1a3Sthorpej struct _prop_object_externalize_context *
_prop_object_externalize_context_alloc(void)296774eb1a3Sthorpej _prop_object_externalize_context_alloc(void)
297774eb1a3Sthorpej {
298774eb1a3Sthorpej struct _prop_object_externalize_context *ctx;
299774eb1a3Sthorpej
300774eb1a3Sthorpej ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
301774eb1a3Sthorpej if (ctx != NULL) {
302774eb1a3Sthorpej ctx->poec_buf = _PROP_MALLOC(BUF_EXPAND, M_TEMP);
303774eb1a3Sthorpej if (ctx->poec_buf == NULL) {
304774eb1a3Sthorpej _PROP_FREE(ctx, M_TEMP);
305774eb1a3Sthorpej return (NULL);
306774eb1a3Sthorpej }
307774eb1a3Sthorpej ctx->poec_len = 0;
308774eb1a3Sthorpej ctx->poec_capacity = BUF_EXPAND;
309774eb1a3Sthorpej ctx->poec_depth = 0;
310774eb1a3Sthorpej }
311774eb1a3Sthorpej return (ctx);
312774eb1a3Sthorpej }
313774eb1a3Sthorpej
314774eb1a3Sthorpej /*
315774eb1a3Sthorpej * _prop_object_externalize_context_free --
316774eb1a3Sthorpej * Free an externalize context.
317774eb1a3Sthorpej */
318774eb1a3Sthorpej void
_prop_object_externalize_context_free(struct _prop_object_externalize_context * ctx)319774eb1a3Sthorpej _prop_object_externalize_context_free(
320774eb1a3Sthorpej struct _prop_object_externalize_context *ctx)
321774eb1a3Sthorpej {
322774eb1a3Sthorpej
323774eb1a3Sthorpej /* Buffer is always freed by the caller. */
324774eb1a3Sthorpej _PROP_FREE(ctx, M_TEMP);
325774eb1a3Sthorpej }
326774eb1a3Sthorpej
327774eb1a3Sthorpej /*
328774eb1a3Sthorpej * _prop_object_internalize_skip_comment --
329774eb1a3Sthorpej * Skip the body and end tag of a comment.
330774eb1a3Sthorpej */
33104377267Sthorpej static bool
_prop_object_internalize_skip_comment(struct _prop_object_internalize_context * ctx)332774eb1a3Sthorpej _prop_object_internalize_skip_comment(
333774eb1a3Sthorpej struct _prop_object_internalize_context *ctx)
334774eb1a3Sthorpej {
335774eb1a3Sthorpej const char *cp = ctx->poic_cp;
336774eb1a3Sthorpej
337774eb1a3Sthorpej while (!_PROP_EOF(*cp)) {
338774eb1a3Sthorpej if (cp[0] == '-' &&
339774eb1a3Sthorpej cp[1] == '-' &&
340774eb1a3Sthorpej cp[2] == '>') {
341774eb1a3Sthorpej ctx->poic_cp = cp + 3;
34204377267Sthorpej return (true);
343774eb1a3Sthorpej }
344774eb1a3Sthorpej cp++;
345774eb1a3Sthorpej }
346774eb1a3Sthorpej
34704377267Sthorpej return (false); /* ran out of buffer */
348774eb1a3Sthorpej }
349774eb1a3Sthorpej
350774eb1a3Sthorpej /*
351774eb1a3Sthorpej * _prop_object_internalize_find_tag --
352774eb1a3Sthorpej * Find the next tag in an XML stream. Optionally compare the found
353774eb1a3Sthorpej * tag to an expected tag name. State of the context is undefined
35404377267Sthorpej * if this routine returns false. Upon success, the context points
355774eb1a3Sthorpej * to the first octet after the tag.
356774eb1a3Sthorpej */
35704377267Sthorpej bool
_prop_object_internalize_find_tag(struct _prop_object_internalize_context * ctx,const char * tag,_prop_tag_type_t type)358774eb1a3Sthorpej _prop_object_internalize_find_tag(struct _prop_object_internalize_context *ctx,
359774eb1a3Sthorpej const char *tag, _prop_tag_type_t type)
360774eb1a3Sthorpej {
361774eb1a3Sthorpej const char *cp;
362774eb1a3Sthorpej size_t taglen;
363774eb1a3Sthorpej
364774eb1a3Sthorpej if (tag != NULL)
365774eb1a3Sthorpej taglen = strlen(tag);
366774eb1a3Sthorpej else
367774eb1a3Sthorpej taglen = 0;
368774eb1a3Sthorpej
369774eb1a3Sthorpej start_over:
370774eb1a3Sthorpej cp = ctx->poic_cp;
371774eb1a3Sthorpej
372774eb1a3Sthorpej /*
373774eb1a3Sthorpej * Find the start of the tag.
374774eb1a3Sthorpej */
375774eb1a3Sthorpej while (_PROP_ISSPACE(*cp))
376774eb1a3Sthorpej cp++;
377774eb1a3Sthorpej if (_PROP_EOF(*cp))
37804377267Sthorpej return (false);
379774eb1a3Sthorpej
380774eb1a3Sthorpej if (*cp != '<')
38104377267Sthorpej return (false);
382774eb1a3Sthorpej
383774eb1a3Sthorpej ctx->poic_tag_start = cp++;
384774eb1a3Sthorpej if (_PROP_EOF(*cp))
38504377267Sthorpej return (false);
386774eb1a3Sthorpej
387774eb1a3Sthorpej if (*cp == '!') {
388774eb1a3Sthorpej if (cp[1] != '-' || cp[2] != '-')
38904377267Sthorpej return (false);
390774eb1a3Sthorpej /*
391774eb1a3Sthorpej * Comment block -- only allowed if we are allowed to
392774eb1a3Sthorpej * return a start tag.
393774eb1a3Sthorpej */
394774eb1a3Sthorpej if (type == _PROP_TAG_TYPE_END)
39504377267Sthorpej return (false);
396774eb1a3Sthorpej ctx->poic_cp = cp + 3;
39704377267Sthorpej if (_prop_object_internalize_skip_comment(ctx) == false)
39804377267Sthorpej return (false);
399774eb1a3Sthorpej goto start_over;
400774eb1a3Sthorpej }
401774eb1a3Sthorpej
402774eb1a3Sthorpej if (*cp == '/') {
403774eb1a3Sthorpej if (type != _PROP_TAG_TYPE_END &&
404774eb1a3Sthorpej type != _PROP_TAG_TYPE_EITHER)
40504377267Sthorpej return (false);
406774eb1a3Sthorpej cp++;
407774eb1a3Sthorpej if (_PROP_EOF(*cp))
40804377267Sthorpej return (false);
409774eb1a3Sthorpej ctx->poic_tag_type = _PROP_TAG_TYPE_END;
410774eb1a3Sthorpej } else {
411774eb1a3Sthorpej if (type != _PROP_TAG_TYPE_START &&
412774eb1a3Sthorpej type != _PROP_TAG_TYPE_EITHER)
41304377267Sthorpej return (false);
414774eb1a3Sthorpej ctx->poic_tag_type = _PROP_TAG_TYPE_START;
415774eb1a3Sthorpej }
416774eb1a3Sthorpej
417774eb1a3Sthorpej ctx->poic_tagname = cp;
418774eb1a3Sthorpej
41972b528eeSchristos while (!_PROP_ISSPACE(*cp) && *cp != '/' && *cp != '>') {
420774eb1a3Sthorpej if (_PROP_EOF(*cp))
42104377267Sthorpej return (false);
42272b528eeSchristos cp++;
42372b528eeSchristos }
424774eb1a3Sthorpej
425774eb1a3Sthorpej ctx->poic_tagname_len = cp - ctx->poic_tagname;
426774eb1a3Sthorpej
427774eb1a3Sthorpej /* Make sure this is the tag we're looking for. */
428774eb1a3Sthorpej if (tag != NULL &&
429774eb1a3Sthorpej (taglen != ctx->poic_tagname_len ||
430774eb1a3Sthorpej memcmp(tag, ctx->poic_tagname, taglen) != 0))
43104377267Sthorpej return (false);
432774eb1a3Sthorpej
433774eb1a3Sthorpej /* Check for empty tag. */
434774eb1a3Sthorpej if (*cp == '/') {
435774eb1a3Sthorpej if (ctx->poic_tag_type != _PROP_TAG_TYPE_START)
43604377267Sthorpej return(false); /* only valid on start tags */
43704377267Sthorpej ctx->poic_is_empty_element = true;
438774eb1a3Sthorpej cp++;
439774eb1a3Sthorpej if (_PROP_EOF(*cp) || *cp != '>')
44004377267Sthorpej return (false);
441774eb1a3Sthorpej } else
44204377267Sthorpej ctx->poic_is_empty_element = false;
443774eb1a3Sthorpej
444774eb1a3Sthorpej /* Easy case of no arguments. */
445774eb1a3Sthorpej if (*cp == '>') {
446774eb1a3Sthorpej ctx->poic_tagattr = NULL;
447774eb1a3Sthorpej ctx->poic_tagattr_len = 0;
448774eb1a3Sthorpej ctx->poic_tagattrval = NULL;
449774eb1a3Sthorpej ctx->poic_tagattrval_len = 0;
450774eb1a3Sthorpej ctx->poic_cp = cp + 1;
45104377267Sthorpej return (true);
452774eb1a3Sthorpej }
453774eb1a3Sthorpej
454774eb1a3Sthorpej _PROP_ASSERT(!_PROP_EOF(*cp));
455774eb1a3Sthorpej cp++;
456774eb1a3Sthorpej if (_PROP_EOF(*cp))
45704377267Sthorpej return (false);
458774eb1a3Sthorpej
459774eb1a3Sthorpej while (_PROP_ISSPACE(*cp))
460774eb1a3Sthorpej cp++;
461774eb1a3Sthorpej if (_PROP_EOF(*cp))
46204377267Sthorpej return (false);
463774eb1a3Sthorpej
464774eb1a3Sthorpej ctx->poic_tagattr = cp;
465774eb1a3Sthorpej
46672b528eeSchristos while (!_PROP_ISSPACE(*cp) && *cp != '=') {
467774eb1a3Sthorpej if (_PROP_EOF(*cp))
46804377267Sthorpej return (false);
46972b528eeSchristos cp++;
47072b528eeSchristos }
471774eb1a3Sthorpej
472774eb1a3Sthorpej ctx->poic_tagattr_len = cp - ctx->poic_tagattr;
473774eb1a3Sthorpej
474774eb1a3Sthorpej cp++;
475774eb1a3Sthorpej if (*cp != '\"')
47604377267Sthorpej return (false);
477774eb1a3Sthorpej cp++;
478774eb1a3Sthorpej if (_PROP_EOF(*cp))
47904377267Sthorpej return (false);
480774eb1a3Sthorpej
481774eb1a3Sthorpej ctx->poic_tagattrval = cp;
48272b528eeSchristos while (*cp != '\"') {
483774eb1a3Sthorpej if (_PROP_EOF(*cp))
48404377267Sthorpej return (false);
48572b528eeSchristos cp++;
48672b528eeSchristos }
487774eb1a3Sthorpej ctx->poic_tagattrval_len = cp - ctx->poic_tagattrval;
488774eb1a3Sthorpej
489774eb1a3Sthorpej cp++;
490774eb1a3Sthorpej if (*cp != '>')
49104377267Sthorpej return (false);
492774eb1a3Sthorpej
493774eb1a3Sthorpej ctx->poic_cp = cp + 1;
49404377267Sthorpej return (true);
495774eb1a3Sthorpej }
496774eb1a3Sthorpej
497774eb1a3Sthorpej /*
498774eb1a3Sthorpej * _prop_object_internalize_decode_string --
499774eb1a3Sthorpej * Decode an encoded string.
500774eb1a3Sthorpej */
50104377267Sthorpej bool
_prop_object_internalize_decode_string(struct _prop_object_internalize_context * ctx,char * target,size_t targsize,size_t * sizep,const char ** cpp)502774eb1a3Sthorpej _prop_object_internalize_decode_string(
503774eb1a3Sthorpej struct _prop_object_internalize_context *ctx,
504774eb1a3Sthorpej char *target, size_t targsize, size_t *sizep,
505774eb1a3Sthorpej const char **cpp)
506774eb1a3Sthorpej {
507774eb1a3Sthorpej const char *src;
508774eb1a3Sthorpej size_t tarindex;
509774eb1a3Sthorpej char c;
510774eb1a3Sthorpej
511774eb1a3Sthorpej tarindex = 0;
512774eb1a3Sthorpej src = ctx->poic_cp;
513774eb1a3Sthorpej
514774eb1a3Sthorpej for (;;) {
515774eb1a3Sthorpej if (_PROP_EOF(*src))
51604377267Sthorpej return (false);
517774eb1a3Sthorpej if (*src == '<') {
518774eb1a3Sthorpej break;
519774eb1a3Sthorpej }
520774eb1a3Sthorpej
521774eb1a3Sthorpej if ((c = *src) == '&') {
522774eb1a3Sthorpej if (src[1] == 'a' &&
523774eb1a3Sthorpej src[2] == 'm' &&
524774eb1a3Sthorpej src[3] == 'p' &&
525774eb1a3Sthorpej src[4] == ';') {
526774eb1a3Sthorpej c = '&';
527774eb1a3Sthorpej src += 5;
528774eb1a3Sthorpej } else if (src[1] == 'l' &&
529774eb1a3Sthorpej src[2] == 't' &&
530774eb1a3Sthorpej src[3] == ';') {
531774eb1a3Sthorpej c = '<';
532774eb1a3Sthorpej src += 4;
533774eb1a3Sthorpej } else if (src[1] == 'g' &&
534774eb1a3Sthorpej src[2] == 't' &&
535774eb1a3Sthorpej src[3] == ';') {
536774eb1a3Sthorpej c = '>';
537774eb1a3Sthorpej src += 4;
538774eb1a3Sthorpej } else if (src[1] == 'a' &&
539774eb1a3Sthorpej src[2] == 'p' &&
540774eb1a3Sthorpej src[3] == 'o' &&
541774eb1a3Sthorpej src[4] == 's' &&
542774eb1a3Sthorpej src[5] == ';') {
543774eb1a3Sthorpej c = '\'';
544774eb1a3Sthorpej src += 6;
545774eb1a3Sthorpej } else if (src[1] == 'q' &&
546774eb1a3Sthorpej src[2] == 'u' &&
547774eb1a3Sthorpej src[3] == 'o' &&
548774eb1a3Sthorpej src[4] == 't' &&
549774eb1a3Sthorpej src[5] == ';') {
550774eb1a3Sthorpej c = '\"';
551774eb1a3Sthorpej src += 6;
552774eb1a3Sthorpej } else
55304377267Sthorpej return (false);
554774eb1a3Sthorpej } else
555774eb1a3Sthorpej src++;
556774eb1a3Sthorpej if (target) {
557774eb1a3Sthorpej if (tarindex >= targsize)
55804377267Sthorpej return (false);
559774eb1a3Sthorpej target[tarindex] = c;
560774eb1a3Sthorpej }
561774eb1a3Sthorpej tarindex++;
562774eb1a3Sthorpej }
563774eb1a3Sthorpej
564774eb1a3Sthorpej _PROP_ASSERT(*src == '<');
565774eb1a3Sthorpej if (sizep != NULL)
566774eb1a3Sthorpej *sizep = tarindex;
567774eb1a3Sthorpej if (cpp != NULL)
568774eb1a3Sthorpej *cpp = src;
569774eb1a3Sthorpej
57004377267Sthorpej return (true);
571774eb1a3Sthorpej }
572774eb1a3Sthorpej
573774eb1a3Sthorpej /*
574774eb1a3Sthorpej * _prop_object_internalize_match --
575774eb1a3Sthorpej * Returns true if the two character streams match.
576774eb1a3Sthorpej */
57704377267Sthorpej bool
_prop_object_internalize_match(const char * str1,size_t len1,const char * str2,size_t len2)578774eb1a3Sthorpej _prop_object_internalize_match(const char *str1, size_t len1,
579774eb1a3Sthorpej const char *str2, size_t len2)
580774eb1a3Sthorpej {
581774eb1a3Sthorpej
582774eb1a3Sthorpej return (len1 == len2 && memcmp(str1, str2, len1) == 0);
583774eb1a3Sthorpej }
584774eb1a3Sthorpej
585774eb1a3Sthorpej #define INTERNALIZER(t, f) \
586774eb1a3Sthorpej { t, sizeof(t) - 1, f }
587774eb1a3Sthorpej
588774eb1a3Sthorpej static const struct _prop_object_internalizer {
589774eb1a3Sthorpej const char *poi_tag;
590774eb1a3Sthorpej size_t poi_taglen;
591e835604cSjoerg prop_object_internalizer_t poi_intern;
592774eb1a3Sthorpej } _prop_object_internalizer_table[] = {
593774eb1a3Sthorpej INTERNALIZER("array", _prop_array_internalize),
594774eb1a3Sthorpej
595774eb1a3Sthorpej INTERNALIZER("true", _prop_bool_internalize),
596774eb1a3Sthorpej INTERNALIZER("false", _prop_bool_internalize),
597774eb1a3Sthorpej
598774eb1a3Sthorpej INTERNALIZER("data", _prop_data_internalize),
599774eb1a3Sthorpej
600774eb1a3Sthorpej INTERNALIZER("dict", _prop_dictionary_internalize),
601774eb1a3Sthorpej
602774eb1a3Sthorpej INTERNALIZER("integer", _prop_number_internalize),
603774eb1a3Sthorpej
604774eb1a3Sthorpej INTERNALIZER("string", _prop_string_internalize),
605774eb1a3Sthorpej
6060616c072Schristos { 0, 0, NULL }
607774eb1a3Sthorpej };
608774eb1a3Sthorpej
609774eb1a3Sthorpej #undef INTERNALIZER
610774eb1a3Sthorpej
611774eb1a3Sthorpej /*
612774eb1a3Sthorpej * _prop_object_internalize_by_tag --
613774eb1a3Sthorpej * Determine the object type from the tag in the context and
614774eb1a3Sthorpej * internalize it.
615774eb1a3Sthorpej */
616774eb1a3Sthorpej prop_object_t
_prop_object_internalize_by_tag(struct _prop_object_internalize_context * ctx)617774eb1a3Sthorpej _prop_object_internalize_by_tag(struct _prop_object_internalize_context *ctx)
618774eb1a3Sthorpej {
619774eb1a3Sthorpej const struct _prop_object_internalizer *poi;
620e835604cSjoerg prop_object_t obj, parent_obj;
621e835604cSjoerg void *data, *iter;
622e835604cSjoerg prop_object_internalizer_continue_t iter_func;
623e835604cSjoerg struct _prop_stack stack;
624774eb1a3Sthorpej
625e835604cSjoerg _prop_stack_init(&stack);
626e835604cSjoerg
627e835604cSjoerg match_start:
628774eb1a3Sthorpej for (poi = _prop_object_internalizer_table;
629774eb1a3Sthorpej poi->poi_tag != NULL; poi++) {
630774eb1a3Sthorpej if (_prop_object_internalize_match(ctx->poic_tagname,
631774eb1a3Sthorpej ctx->poic_tagname_len,
632774eb1a3Sthorpej poi->poi_tag,
633774eb1a3Sthorpej poi->poi_taglen))
634e835604cSjoerg break;
635e835604cSjoerg }
6360c9b7348Shaad if ((poi == NULL) || (poi->poi_tag == NULL)) {
6374deb5931Sjoerg while (_prop_stack_pop(&stack, &obj, &iter, &data, NULL)) {
638e835604cSjoerg iter_func = (prop_object_internalizer_continue_t)iter;
639e835604cSjoerg (*iter_func)(&stack, &obj, ctx, data, NULL);
640774eb1a3Sthorpej }
641774eb1a3Sthorpej
642774eb1a3Sthorpej return (NULL);
643774eb1a3Sthorpej }
644774eb1a3Sthorpej
645e835604cSjoerg obj = NULL;
646e835604cSjoerg if (!(*poi->poi_intern)(&stack, &obj, ctx))
647e835604cSjoerg goto match_start;
648e835604cSjoerg
649e835604cSjoerg parent_obj = obj;
6504deb5931Sjoerg while (_prop_stack_pop(&stack, &parent_obj, &iter, &data, NULL)) {
651e835604cSjoerg iter_func = (prop_object_internalizer_continue_t)iter;
652e835604cSjoerg if (!(*iter_func)(&stack, &parent_obj, ctx, data, obj))
653e835604cSjoerg goto match_start;
654e835604cSjoerg obj = parent_obj;
655e835604cSjoerg }
656e835604cSjoerg
657e835604cSjoerg return (parent_obj);
658e835604cSjoerg }
659e835604cSjoerg
66039dccbf2Sjoerg prop_object_t
_prop_generic_internalize(const char * xml,const char * master_tag)66139dccbf2Sjoerg _prop_generic_internalize(const char *xml, const char *master_tag)
66239dccbf2Sjoerg {
66339dccbf2Sjoerg prop_object_t obj = NULL;
66439dccbf2Sjoerg struct _prop_object_internalize_context *ctx;
66539dccbf2Sjoerg
66639dccbf2Sjoerg ctx = _prop_object_internalize_context_alloc(xml);
66739dccbf2Sjoerg if (ctx == NULL)
66839dccbf2Sjoerg return (NULL);
66939dccbf2Sjoerg
67039dccbf2Sjoerg /* We start with a <plist> tag. */
67139dccbf2Sjoerg if (_prop_object_internalize_find_tag(ctx, "plist",
672e835604cSjoerg _PROP_TAG_TYPE_START) == false)
67339dccbf2Sjoerg goto out;
67439dccbf2Sjoerg
67539dccbf2Sjoerg /* Plist elements cannot be empty. */
67639dccbf2Sjoerg if (ctx->poic_is_empty_element)
67739dccbf2Sjoerg goto out;
67839dccbf2Sjoerg
67939dccbf2Sjoerg /*
68039dccbf2Sjoerg * We don't understand any plist attributes, but Apple XML
68139dccbf2Sjoerg * property lists often have a "version" attribute. If we
68239dccbf2Sjoerg * see that one, we simply ignore it.
68339dccbf2Sjoerg */
68439dccbf2Sjoerg if (ctx->poic_tagattr != NULL &&
68539dccbf2Sjoerg !_PROP_TAGATTR_MATCH(ctx, "version"))
68639dccbf2Sjoerg goto out;
68739dccbf2Sjoerg
68839dccbf2Sjoerg /* Next we expect to see opening master_tag. */
68939dccbf2Sjoerg if (_prop_object_internalize_find_tag(ctx, master_tag,
690e835604cSjoerg _PROP_TAG_TYPE_START) == false)
69139dccbf2Sjoerg goto out;
69239dccbf2Sjoerg
69339dccbf2Sjoerg obj = _prop_object_internalize_by_tag(ctx);
69439dccbf2Sjoerg if (obj == NULL)
69539dccbf2Sjoerg goto out;
69639dccbf2Sjoerg
69739dccbf2Sjoerg /*
69839dccbf2Sjoerg * We've advanced past the closing master_tag.
69939dccbf2Sjoerg * Now we want </plist>.
70039dccbf2Sjoerg */
70139dccbf2Sjoerg if (_prop_object_internalize_find_tag(ctx, "plist",
702e835604cSjoerg _PROP_TAG_TYPE_END) == false) {
70339dccbf2Sjoerg prop_object_release(obj);
70439dccbf2Sjoerg obj = NULL;
70539dccbf2Sjoerg }
70639dccbf2Sjoerg
70739dccbf2Sjoerg out:
70839dccbf2Sjoerg _prop_object_internalize_context_free(ctx);
70939dccbf2Sjoerg return (obj);
71039dccbf2Sjoerg }
71139dccbf2Sjoerg
712774eb1a3Sthorpej /*
713774eb1a3Sthorpej * _prop_object_internalize_context_alloc --
714774eb1a3Sthorpej * Allocate an internalize context.
715774eb1a3Sthorpej */
716774eb1a3Sthorpej struct _prop_object_internalize_context *
_prop_object_internalize_context_alloc(const char * xml)717774eb1a3Sthorpej _prop_object_internalize_context_alloc(const char *xml)
718774eb1a3Sthorpej {
719774eb1a3Sthorpej struct _prop_object_internalize_context *ctx;
720774eb1a3Sthorpej
721*86af3c87Sriastradh ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
722774eb1a3Sthorpej if (ctx == NULL)
723774eb1a3Sthorpej return (NULL);
724774eb1a3Sthorpej
725774eb1a3Sthorpej ctx->poic_xml = ctx->poic_cp = xml;
726774eb1a3Sthorpej
727774eb1a3Sthorpej /*
728774eb1a3Sthorpej * Skip any whitespace and XML preamble stuff that we don't
729774eb1a3Sthorpej * know about / care about.
730774eb1a3Sthorpej */
731774eb1a3Sthorpej for (;;) {
732774eb1a3Sthorpej while (_PROP_ISSPACE(*xml))
733774eb1a3Sthorpej xml++;
734774eb1a3Sthorpej if (_PROP_EOF(*xml) || *xml != '<')
735774eb1a3Sthorpej goto bad;
736774eb1a3Sthorpej
737e603605eSriastradh #define MATCH(str) (strncmp(&xml[1], str, strlen(str)) == 0)
738774eb1a3Sthorpej
739774eb1a3Sthorpej /*
740774eb1a3Sthorpej * Skip over the XML preamble that Apple XML property
741774eb1a3Sthorpej * lists usually include at the top of the file.
742774eb1a3Sthorpej */
743774eb1a3Sthorpej if (MATCH("?xml ") ||
744774eb1a3Sthorpej MATCH("!DOCTYPE plist")) {
745774eb1a3Sthorpej while (*xml != '>' && !_PROP_EOF(*xml))
746774eb1a3Sthorpej xml++;
747774eb1a3Sthorpej if (_PROP_EOF(*xml))
748774eb1a3Sthorpej goto bad;
749774eb1a3Sthorpej xml++; /* advance past the '>' */
750774eb1a3Sthorpej continue;
751774eb1a3Sthorpej }
752774eb1a3Sthorpej
753774eb1a3Sthorpej if (MATCH("<!--")) {
754774eb1a3Sthorpej ctx->poic_cp = xml + 4;
75504377267Sthorpej if (_prop_object_internalize_skip_comment(ctx) == false)
756774eb1a3Sthorpej goto bad;
757774eb1a3Sthorpej xml = ctx->poic_cp;
758774eb1a3Sthorpej continue;
759774eb1a3Sthorpej }
760774eb1a3Sthorpej
761774eb1a3Sthorpej #undef MATCH
762774eb1a3Sthorpej
763774eb1a3Sthorpej /*
764774eb1a3Sthorpej * We don't think we should skip it, so let's hope we can
765774eb1a3Sthorpej * parse it.
766774eb1a3Sthorpej */
767774eb1a3Sthorpej break;
768774eb1a3Sthorpej }
769774eb1a3Sthorpej
770774eb1a3Sthorpej ctx->poic_cp = xml;
771774eb1a3Sthorpej return (ctx);
772774eb1a3Sthorpej bad:
773774eb1a3Sthorpej _PROP_FREE(ctx, M_TEMP);
774774eb1a3Sthorpej return (NULL);
775774eb1a3Sthorpej }
776774eb1a3Sthorpej
777774eb1a3Sthorpej /*
778774eb1a3Sthorpej * _prop_object_internalize_context_free --
779774eb1a3Sthorpej * Free an internalize context.
780774eb1a3Sthorpej */
781774eb1a3Sthorpej void
_prop_object_internalize_context_free(struct _prop_object_internalize_context * ctx)782774eb1a3Sthorpej _prop_object_internalize_context_free(
783774eb1a3Sthorpej struct _prop_object_internalize_context *ctx)
784774eb1a3Sthorpej {
785774eb1a3Sthorpej
786774eb1a3Sthorpej _PROP_FREE(ctx, M_TEMP);
787774eb1a3Sthorpej }
788774eb1a3Sthorpej
789d21620b2Sthorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
790d21620b2Sthorpej /*
791d21620b2Sthorpej * _prop_object_externalize_file_dirname --
792d21620b2Sthorpej * dirname(3), basically. We have to roll our own because the
793d21620b2Sthorpej * system dirname(3) isn't reentrant.
794d21620b2Sthorpej */
795d21620b2Sthorpej static void
_prop_object_externalize_file_dirname(const char * path,char * result)796d21620b2Sthorpej _prop_object_externalize_file_dirname(const char *path, char *result)
797d21620b2Sthorpej {
798d21620b2Sthorpej const char *lastp;
799d21620b2Sthorpej size_t len;
800d21620b2Sthorpej
801d21620b2Sthorpej /*
802d21620b2Sthorpej * If `path' is a NULL pointer or points to an empty string,
803d21620b2Sthorpej * return ".".
804d21620b2Sthorpej */
805d21620b2Sthorpej if (path == NULL || *path == '\0')
806d21620b2Sthorpej goto singledot;
807d21620b2Sthorpej
808d21620b2Sthorpej /* String trailing slashes, if any. */
809d21620b2Sthorpej lastp = path + strlen(path) - 1;
810d21620b2Sthorpej while (lastp != path && *lastp == '/')
811d21620b2Sthorpej lastp--;
812d21620b2Sthorpej
813d21620b2Sthorpej /* Terminate path at the last occurrence of '/'. */
814d21620b2Sthorpej do {
815d21620b2Sthorpej if (*lastp == '/') {
816d21620b2Sthorpej /* Strip trailing slashes, if any. */
817d21620b2Sthorpej while (lastp != path && *lastp == '/')
818d21620b2Sthorpej lastp--;
819d21620b2Sthorpej
820d21620b2Sthorpej /* ...and copy the result into the result buffer. */
821d21620b2Sthorpej len = (lastp - path) + 1 /* last char */;
822d21620b2Sthorpej if (len > (PATH_MAX - 1))
823d21620b2Sthorpej len = PATH_MAX - 1;
824d21620b2Sthorpej
825d21620b2Sthorpej memcpy(result, path, len);
826d21620b2Sthorpej result[len] = '\0';
827d21620b2Sthorpej return;
828d21620b2Sthorpej }
829d21620b2Sthorpej } while (--lastp >= path);
830d21620b2Sthorpej
831d21620b2Sthorpej /* No /'s found, return ".". */
832d21620b2Sthorpej singledot:
833d21620b2Sthorpej strcpy(result, ".");
834d21620b2Sthorpej }
835d21620b2Sthorpej
836d21620b2Sthorpej /*
837d21620b2Sthorpej * _prop_object_externalize_write_file --
838d21620b2Sthorpej * Write an externalized dictionary to the specified file.
839d21620b2Sthorpej * The file is written atomically from the caller's perspective,
840d21620b2Sthorpej * and the mode set to 0666 modified by the caller's umask.
841d21620b2Sthorpej */
84204377267Sthorpej bool
_prop_object_externalize_write_file(const char * fname,const char * xml,size_t len)843d21620b2Sthorpej _prop_object_externalize_write_file(const char *fname, const char *xml,
844d21620b2Sthorpej size_t len)
845d21620b2Sthorpej {
846d21620b2Sthorpej char tname[PATH_MAX];
847d21620b2Sthorpej int fd;
848d21620b2Sthorpej int save_errno;
8498346a636Slukem mode_t myumask;
850d21620b2Sthorpej
851d21620b2Sthorpej if (len > SSIZE_MAX) {
852d21620b2Sthorpej errno = EFBIG;
85304377267Sthorpej return (false);
854d21620b2Sthorpej }
855d21620b2Sthorpej
856d21620b2Sthorpej /*
857d21620b2Sthorpej * Get the directory name where the file is to be written
858d21620b2Sthorpej * and create the temporary file.
859d21620b2Sthorpej */
860d21620b2Sthorpej _prop_object_externalize_file_dirname(fname, tname);
861c303bcbeSpooka #define PLISTTMP "/.plistXXXXXX"
862c303bcbeSpooka if (strlen(tname) + strlen(PLISTTMP) >= sizeof(tname)) {
863d21620b2Sthorpej errno = ENAMETOOLONG;
86404377267Sthorpej return (false);
865d21620b2Sthorpej }
866c303bcbeSpooka strcat(tname, PLISTTMP);
867c303bcbeSpooka #undef PLISTTMP
868c303bcbeSpooka
8698346a636Slukem if ((fd = mkstemp(tname)) == -1)
87004377267Sthorpej return (false);
871d21620b2Sthorpej
872d21620b2Sthorpej if (write(fd, xml, len) != (ssize_t)len)
873d21620b2Sthorpej goto bad;
874d21620b2Sthorpej
875d21620b2Sthorpej if (fsync(fd) == -1)
876d21620b2Sthorpej goto bad;
877d21620b2Sthorpej
8788346a636Slukem myumask = umask(0);
8798346a636Slukem (void)umask(myumask);
8808346a636Slukem if (fchmod(fd, 0666 & ~myumask) == -1)
8818346a636Slukem goto bad;
8828346a636Slukem
883d21620b2Sthorpej (void) close(fd);
884d21620b2Sthorpej fd = -1;
885d21620b2Sthorpej
886d21620b2Sthorpej if (rename(tname, fname) == -1)
887d21620b2Sthorpej goto bad;
888d21620b2Sthorpej
88904377267Sthorpej return (true);
890d21620b2Sthorpej
891d21620b2Sthorpej bad:
892d21620b2Sthorpej save_errno = errno;
893d21620b2Sthorpej if (fd != -1)
894d21620b2Sthorpej (void) close(fd);
895d21620b2Sthorpej (void) unlink(tname);
896d21620b2Sthorpej errno = save_errno;
89704377267Sthorpej return (false);
898d21620b2Sthorpej }
899d21620b2Sthorpej
900d21620b2Sthorpej /*
901d21620b2Sthorpej * _prop_object_internalize_map_file --
902d21620b2Sthorpej * Map a file for the purpose of internalizing it.
903d21620b2Sthorpej */
904d21620b2Sthorpej struct _prop_object_internalize_mapped_file *
_prop_object_internalize_map_file(const char * fname)905d21620b2Sthorpej _prop_object_internalize_map_file(const char *fname)
906d21620b2Sthorpej {
907d21620b2Sthorpej struct stat sb;
908d21620b2Sthorpej struct _prop_object_internalize_mapped_file *mf;
9094df50368She size_t pgsize = (size_t)sysconf(_SC_PAGESIZE);
910d21620b2Sthorpej size_t pgmask = pgsize - 1;
91104377267Sthorpej bool need_guard = false;
912d21620b2Sthorpej int fd;
913d21620b2Sthorpej
914d21620b2Sthorpej mf = _PROP_MALLOC(sizeof(*mf), M_TEMP);
915d21620b2Sthorpej if (mf == NULL)
916d21620b2Sthorpej return (NULL);
917d21620b2Sthorpej
918d21620b2Sthorpej fd = open(fname, O_RDONLY, 0400);
919d21620b2Sthorpej if (fd == -1) {
920d21620b2Sthorpej _PROP_FREE(mf, M_TEMP);
921d21620b2Sthorpej return (NULL);
922d21620b2Sthorpej }
923d21620b2Sthorpej
924d21620b2Sthorpej if (fstat(fd, &sb) == -1) {
925d21620b2Sthorpej (void) close(fd);
926d21620b2Sthorpej _PROP_FREE(mf, M_TEMP);
927d21620b2Sthorpej return (NULL);
928d21620b2Sthorpej }
929d21620b2Sthorpej mf->poimf_mapsize = ((size_t)sb.st_size + pgmask) & ~pgmask;
930114b3fb3Slukem if (mf->poimf_mapsize < (size_t)sb.st_size) {
931d21620b2Sthorpej (void) close(fd);
932d21620b2Sthorpej _PROP_FREE(mf, M_TEMP);
933d21620b2Sthorpej return (NULL);
934d21620b2Sthorpej }
935d21620b2Sthorpej
936d21620b2Sthorpej /*
937d21620b2Sthorpej * If the file length is an integral number of pages, then we
938d21620b2Sthorpej * need to map a guard page at the end in order to provide the
939d21620b2Sthorpej * necessary NUL-termination of the buffer.
940d21620b2Sthorpej */
941d21620b2Sthorpej if ((sb.st_size & pgmask) == 0)
94204377267Sthorpej need_guard = true;
943d21620b2Sthorpej
944d21620b2Sthorpej mf->poimf_xml = mmap(NULL, need_guard ? mf->poimf_mapsize + pgsize
945d21620b2Sthorpej : mf->poimf_mapsize,
946d21620b2Sthorpej PROT_READ, MAP_FILE|MAP_SHARED, fd, (off_t)0);
947d21620b2Sthorpej (void) close(fd);
948d21620b2Sthorpej if (mf->poimf_xml == MAP_FAILED) {
949d21620b2Sthorpej _PROP_FREE(mf, M_TEMP);
950d21620b2Sthorpej return (NULL);
951d21620b2Sthorpej }
95288c31669Sthorpej #ifdef POSIX_MADV_SEQUENTIAL
95388c31669Sthorpej (void) posix_madvise(mf->poimf_xml, mf->poimf_mapsize,
95488c31669Sthorpej POSIX_MADV_SEQUENTIAL);
95588c31669Sthorpej #endif
956d21620b2Sthorpej
957d21620b2Sthorpej if (need_guard) {
958d21620b2Sthorpej if (mmap(mf->poimf_xml + mf->poimf_mapsize,
959d21620b2Sthorpej pgsize, PROT_READ,
960d21620b2Sthorpej MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1,
961d21620b2Sthorpej (off_t)0) == MAP_FAILED) {
962d21620b2Sthorpej (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
963d21620b2Sthorpej _PROP_FREE(mf, M_TEMP);
964d21620b2Sthorpej return (NULL);
965d21620b2Sthorpej }
966d21620b2Sthorpej mf->poimf_mapsize += pgsize;
967d21620b2Sthorpej }
968d21620b2Sthorpej
969d21620b2Sthorpej return (mf);
970d21620b2Sthorpej }
971d21620b2Sthorpej
972d21620b2Sthorpej /*
973d21620b2Sthorpej * _prop_object_internalize_unmap_file --
974d21620b2Sthorpej * Unmap a file previously mapped for internalizing.
975d21620b2Sthorpej */
976d21620b2Sthorpej void
_prop_object_internalize_unmap_file(struct _prop_object_internalize_mapped_file * mf)977d21620b2Sthorpej _prop_object_internalize_unmap_file(
978d21620b2Sthorpej struct _prop_object_internalize_mapped_file *mf)
979d21620b2Sthorpej {
980d21620b2Sthorpej
98188c31669Sthorpej #ifdef POSIX_MADV_DONTNEED
98288c31669Sthorpej (void) posix_madvise(mf->poimf_xml, mf->poimf_mapsize,
98388c31669Sthorpej POSIX_MADV_DONTNEED);
98488c31669Sthorpej #endif
985d21620b2Sthorpej (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
986d21620b2Sthorpej _PROP_FREE(mf, M_TEMP);
987d21620b2Sthorpej }
988d21620b2Sthorpej #endif /* !_KERNEL && !_STANDALONE */
989d21620b2Sthorpej
990774eb1a3Sthorpej /*
991774eb1a3Sthorpej * prop_object_retain --
992774eb1a3Sthorpej * Increment the reference count on an object.
993774eb1a3Sthorpej */
994774eb1a3Sthorpej void
prop_object_retain(prop_object_t obj)995774eb1a3Sthorpej prop_object_retain(prop_object_t obj)
996774eb1a3Sthorpej {
997774eb1a3Sthorpej struct _prop_object *po = obj;
9985fb50cc6Smartin uint32_t ncnt __unused;
999774eb1a3Sthorpej
1000c303bcbeSpooka _PROP_ATOMIC_INC32_NV(&po->po_refcnt, ncnt);
1001b6e6025dSpooka _PROP_ASSERT(ncnt != 0);
1002774eb1a3Sthorpej }
1003774eb1a3Sthorpej
1004774eb1a3Sthorpej /*
1005e835604cSjoerg * prop_object_release_emergency
1006e835604cSjoerg * A direct free with prop_object_release failed.
1007e835604cSjoerg * Walk down the tree until a leaf is found and
1008e835604cSjoerg * free that. Do not recurse to avoid stack overflows.
1009e835604cSjoerg *
1010e835604cSjoerg * This is a slow edge condition, but necessary to
1011ed504658Sxtraeme * guarantee that an object can always be freed.
1012e835604cSjoerg */
1013e835604cSjoerg static void
prop_object_release_emergency(prop_object_t obj)1014e835604cSjoerg prop_object_release_emergency(prop_object_t obj)
1015e835604cSjoerg {
1016e835604cSjoerg struct _prop_object *po;
1017e51aea32Shaad void (*unlock)(void);
1018e835604cSjoerg prop_object_t parent = NULL;
1019e835604cSjoerg uint32_t ocnt;
1020e835604cSjoerg
1021e835604cSjoerg for (;;) {
1022e835604cSjoerg po = obj;
1023e835604cSjoerg _PROP_ASSERT(obj);
1024e835604cSjoerg
1025e51aea32Shaad if (po->po_type->pot_lock != NULL)
1026e51aea32Shaad po->po_type->pot_lock();
1027e51aea32Shaad
1028e51aea32Shaad /* Save pointerto unlock function */
1029e51aea32Shaad unlock = po->po_type->pot_unlock;
1030e51aea32Shaad
1031b6e6025dSpooka /* Dance a bit to make sure we always get the non-racy ocnt */
1032c303bcbeSpooka _PROP_ATOMIC_DEC32_NV(&po->po_refcnt, ocnt);
1033b6e6025dSpooka ocnt++;
1034e835604cSjoerg _PROP_ASSERT(ocnt != 0);
1035b6e6025dSpooka
1036e51aea32Shaad if (ocnt != 1) {
1037e51aea32Shaad if (unlock != NULL)
1038e51aea32Shaad unlock();
1039e835604cSjoerg break;
1040e51aea32Shaad }
1041e835604cSjoerg
1042e835604cSjoerg _PROP_ASSERT(po->po_type);
10434ce0dc3aSthorpej if ((po->po_type->pot_free)(NULL, &obj) ==
1044e51aea32Shaad _PROP_OBJECT_FREE_DONE) {
1045e51aea32Shaad if (unlock != NULL)
1046e51aea32Shaad unlock();
1047e835604cSjoerg break;
1048e51aea32Shaad }
1049e51aea32Shaad
1050e51aea32Shaad if (unlock != NULL)
1051e51aea32Shaad unlock();
1052e835604cSjoerg
1053e835604cSjoerg parent = po;
1054c303bcbeSpooka _PROP_ATOMIC_INC32(&po->po_refcnt);
1055e835604cSjoerg }
1056e835604cSjoerg _PROP_ASSERT(parent);
1057e835604cSjoerg /* One object was just freed. */
1058e835604cSjoerg po = parent;
1059e835604cSjoerg (*po->po_type->pot_emergency_free)(parent);
1060e835604cSjoerg }
1061e835604cSjoerg
1062e835604cSjoerg /*
1063774eb1a3Sthorpej * prop_object_release --
1064774eb1a3Sthorpej * Decrement the reference count on an object.
1065774eb1a3Sthorpej *
1066774eb1a3Sthorpej * Free the object if we are releasing the final
1067774eb1a3Sthorpej * reference.
1068774eb1a3Sthorpej */
1069774eb1a3Sthorpej void
prop_object_release(prop_object_t obj)1070774eb1a3Sthorpej prop_object_release(prop_object_t obj)
1071774eb1a3Sthorpej {
1072e835604cSjoerg struct _prop_object *po;
1073e835604cSjoerg struct _prop_stack stack;
1074e51aea32Shaad void (*unlock)(void);
1075e835604cSjoerg int ret;
1076774eb1a3Sthorpej uint32_t ocnt;
1077774eb1a3Sthorpej
1078e835604cSjoerg _prop_stack_init(&stack);
1079e835604cSjoerg
1080e835604cSjoerg do {
1081e835604cSjoerg do {
1082e835604cSjoerg po = obj;
1083e835604cSjoerg _PROP_ASSERT(obj);
1084e835604cSjoerg
1085e51aea32Shaad if (po->po_type->pot_lock != NULL)
1086e51aea32Shaad po->po_type->pot_lock();
1087e51aea32Shaad
1088e51aea32Shaad /* Save pointer to object unlock function */
1089e51aea32Shaad unlock = po->po_type->pot_unlock;
1090e51aea32Shaad
1091c303bcbeSpooka _PROP_ATOMIC_DEC32_NV(&po->po_refcnt, ocnt);
1092b6e6025dSpooka ocnt++;
1093774eb1a3Sthorpej _PROP_ASSERT(ocnt != 0);
1094b6e6025dSpooka
1095e835604cSjoerg if (ocnt != 1) {
1096e835604cSjoerg ret = 0;
1097e51aea32Shaad if (unlock != NULL)
1098e51aea32Shaad unlock();
1099e835604cSjoerg break;
1100e835604cSjoerg }
1101e835604cSjoerg
1102e835604cSjoerg ret = (po->po_type->pot_free)(&stack, &obj);
1103e835604cSjoerg
1104e51aea32Shaad if (unlock != NULL)
1105e51aea32Shaad unlock();
1106e51aea32Shaad
1107e835604cSjoerg if (ret == _PROP_OBJECT_FREE_DONE)
1108e835604cSjoerg break;
1109e835604cSjoerg
1110c303bcbeSpooka _PROP_ATOMIC_INC32(&po->po_refcnt);
1111e835604cSjoerg } while (ret == _PROP_OBJECT_FREE_RECURSE);
1112e835604cSjoerg if (ret == _PROP_OBJECT_FREE_FAILED)
1113e835604cSjoerg prop_object_release_emergency(obj);
11144deb5931Sjoerg } while (_prop_stack_pop(&stack, &obj, NULL, NULL, NULL));
1115774eb1a3Sthorpej }
1116774eb1a3Sthorpej
1117774eb1a3Sthorpej /*
1118774eb1a3Sthorpej * prop_object_type --
1119774eb1a3Sthorpej * Return the type of an object.
1120774eb1a3Sthorpej */
1121774eb1a3Sthorpej prop_type_t
prop_object_type(prop_object_t obj)1122774eb1a3Sthorpej prop_object_type(prop_object_t obj)
1123774eb1a3Sthorpej {
1124774eb1a3Sthorpej struct _prop_object *po = obj;
1125774eb1a3Sthorpej
1126d21620b2Sthorpej if (obj == NULL)
1127d21620b2Sthorpej return (PROP_TYPE_UNKNOWN);
1128d21620b2Sthorpej
11293e69f1b2Sthorpej return (po->po_type->pot_type);
11303e69f1b2Sthorpej }
11313e69f1b2Sthorpej
11323e69f1b2Sthorpej /*
11333e69f1b2Sthorpej * prop_object_equals --
113404377267Sthorpej * Returns true if thw two objects are equivalent.
11353e69f1b2Sthorpej */
113604377267Sthorpej bool
prop_object_equals(prop_object_t obj1,prop_object_t obj2)11373e69f1b2Sthorpej prop_object_equals(prop_object_t obj1, prop_object_t obj2)
11383e69f1b2Sthorpej {
11394deb5931Sjoerg return (prop_object_equals_with_error(obj1, obj2, NULL));
11404deb5931Sjoerg }
11414deb5931Sjoerg
11424deb5931Sjoerg bool
prop_object_equals_with_error(prop_object_t obj1,prop_object_t obj2,bool * error_flag)11434deb5931Sjoerg prop_object_equals_with_error(prop_object_t obj1, prop_object_t obj2,
11444deb5931Sjoerg bool *error_flag)
11454deb5931Sjoerg {
11464deb5931Sjoerg struct _prop_object *po1;
11474deb5931Sjoerg struct _prop_object *po2;
11484deb5931Sjoerg void *stored_pointer1, *stored_pointer2;
11494deb5931Sjoerg prop_object_t next_obj1, next_obj2;
11504deb5931Sjoerg struct _prop_stack stack;
11514ce0dc3aSthorpej _prop_object_equals_rv_t ret;
11524deb5931Sjoerg
11534deb5931Sjoerg _prop_stack_init(&stack);
11544deb5931Sjoerg if (error_flag)
11554deb5931Sjoerg *error_flag = false;
11564deb5931Sjoerg
11574deb5931Sjoerg start_subtree:
11584deb5931Sjoerg stored_pointer1 = NULL;
11594deb5931Sjoerg stored_pointer2 = NULL;
11604deb5931Sjoerg po1 = obj1;
11614deb5931Sjoerg po2 = obj2;
11623e69f1b2Sthorpej
11633e69f1b2Sthorpej if (po1->po_type != po2->po_type)
116404377267Sthorpej return (false);
11653e69f1b2Sthorpej
11664deb5931Sjoerg continue_subtree:
11674ce0dc3aSthorpej ret = (*po1->po_type->pot_equals)(obj1, obj2,
11684ce0dc3aSthorpej &stored_pointer1, &stored_pointer2,
11694deb5931Sjoerg &next_obj1, &next_obj2);
11704deb5931Sjoerg if (ret == _PROP_OBJECT_EQUALS_FALSE)
11714deb5931Sjoerg goto finish;
11724deb5931Sjoerg if (ret == _PROP_OBJECT_EQUALS_TRUE) {
11734deb5931Sjoerg if (!_prop_stack_pop(&stack, &obj1, &obj2,
11744deb5931Sjoerg &stored_pointer1, &stored_pointer2))
11754deb5931Sjoerg return true;
1176235708f8Smartin po1 = obj1;
1177235708f8Smartin po2 = obj2;
11784deb5931Sjoerg goto continue_subtree;
11794deb5931Sjoerg }
11804deb5931Sjoerg _PROP_ASSERT(ret == _PROP_OBJECT_EQUALS_RECURSE);
11814deb5931Sjoerg
11824deb5931Sjoerg if (!_prop_stack_push(&stack, obj1, obj2,
11834deb5931Sjoerg stored_pointer1, stored_pointer2)) {
11844deb5931Sjoerg if (error_flag)
11854deb5931Sjoerg *error_flag = true;
11864deb5931Sjoerg goto finish;
11874deb5931Sjoerg }
11884deb5931Sjoerg obj1 = next_obj1;
11894deb5931Sjoerg obj2 = next_obj2;
11904deb5931Sjoerg goto start_subtree;
11914deb5931Sjoerg
11924deb5931Sjoerg finish:
11934deb5931Sjoerg while (_prop_stack_pop(&stack, &obj1, &obj2, NULL, NULL)) {
11944deb5931Sjoerg po1 = obj1;
11954deb5931Sjoerg (*po1->po_type->pot_equals_finish)(obj1, obj2);
11964deb5931Sjoerg }
11974deb5931Sjoerg return (false);
1198774eb1a3Sthorpej }
1199774eb1a3Sthorpej
1200774eb1a3Sthorpej /*
1201774eb1a3Sthorpej * prop_object_iterator_next --
1202774eb1a3Sthorpej * Return the next item during an iteration.
1203774eb1a3Sthorpej */
1204774eb1a3Sthorpej prop_object_t
prop_object_iterator_next(prop_object_iterator_t pi)1205774eb1a3Sthorpej prop_object_iterator_next(prop_object_iterator_t pi)
1206774eb1a3Sthorpej {
1207774eb1a3Sthorpej
1208774eb1a3Sthorpej return ((*pi->pi_next_object)(pi));
1209774eb1a3Sthorpej }
1210774eb1a3Sthorpej
1211774eb1a3Sthorpej /*
1212774eb1a3Sthorpej * prop_object_iterator_reset --
1213774eb1a3Sthorpej * Reset the iterator to the first object so as to restart
1214774eb1a3Sthorpej * iteration.
1215774eb1a3Sthorpej */
1216774eb1a3Sthorpej void
prop_object_iterator_reset(prop_object_iterator_t pi)1217774eb1a3Sthorpej prop_object_iterator_reset(prop_object_iterator_t pi)
1218774eb1a3Sthorpej {
1219774eb1a3Sthorpej
1220774eb1a3Sthorpej (*pi->pi_reset)(pi);
1221774eb1a3Sthorpej }
1222774eb1a3Sthorpej
1223774eb1a3Sthorpej /*
1224774eb1a3Sthorpej * prop_object_iterator_release --
1225774eb1a3Sthorpej * Release the object iterator.
1226774eb1a3Sthorpej */
1227774eb1a3Sthorpej void
prop_object_iterator_release(prop_object_iterator_t pi)1228774eb1a3Sthorpej prop_object_iterator_release(prop_object_iterator_t pi)
1229774eb1a3Sthorpej {
1230774eb1a3Sthorpej
1231774eb1a3Sthorpej prop_object_release(pi->pi_obj);
1232774eb1a3Sthorpej _PROP_FREE(pi, M_TEMP);
1233774eb1a3Sthorpej }
1234