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