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