Home | History | Annotate | Line # | Download | only in libprop
prop_object.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: prop_object.c,v 1.1 2006/04/27 20:11:27 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.1  thorpej  *      This product includes software developed by the NetBSD
     21  1.1  thorpej  *      Foundation, Inc. and its contributors.
     22  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej #include <prop/prop_object.h>
     40  1.1  thorpej #include "prop_object_impl.h"
     41  1.1  thorpej 
     42  1.1  thorpej #ifdef _STANDALONE
     43  1.1  thorpej void *
     44  1.1  thorpej _prop_standalone_calloc(size_t size)
     45  1.1  thorpej {
     46  1.1  thorpej 	void *rv;
     47  1.1  thorpej 
     48  1.1  thorpej 	rv = alloc(size);
     49  1.1  thorpej 	if (rv != NULL)
     50  1.1  thorpej 		memset(rv, 0, size);
     51  1.1  thorpej 
     52  1.1  thorpej 	return (rv);
     53  1.1  thorpej }
     54  1.1  thorpej #endif /* _STANDALONE */
     55  1.1  thorpej 
     56  1.1  thorpej /*
     57  1.1  thorpej  * _prop_object_init --
     58  1.1  thorpej  *	Initialize an object.  Called when sub-classes create
     59  1.1  thorpej  *	an instance.
     60  1.1  thorpej  */
     61  1.1  thorpej void
     62  1.1  thorpej _prop_object_init(struct _prop_object *po)
     63  1.1  thorpej {
     64  1.1  thorpej 
     65  1.1  thorpej 	po->po_refcnt = 1;
     66  1.1  thorpej }
     67  1.1  thorpej 
     68  1.1  thorpej /*
     69  1.1  thorpej  * _prop_object_fini --
     70  1.1  thorpej  *	Finalize an object.  Called when sub-classes destroy
     71  1.1  thorpej  *	an instance.
     72  1.1  thorpej  */
     73  1.1  thorpej void
     74  1.1  thorpej _prop_object_fini(struct _prop_object *po)
     75  1.1  thorpej {
     76  1.1  thorpej 	/* Nothing to do, currently. */
     77  1.1  thorpej }
     78  1.1  thorpej 
     79  1.1  thorpej /*
     80  1.1  thorpej  * _prop_object_externalize_start_tag --
     81  1.1  thorpej  *	Append an XML-style start tag to the externalize buffer.
     82  1.1  thorpej  */
     83  1.1  thorpej boolean_t
     84  1.1  thorpej _prop_object_externalize_start_tag(
     85  1.1  thorpej     struct _prop_object_externalize_context *ctx, const char *tag)
     86  1.1  thorpej {
     87  1.1  thorpej 	unsigned int i;
     88  1.1  thorpej 
     89  1.1  thorpej 	for (i = 0; i < ctx->poec_depth; i++) {
     90  1.1  thorpej 		if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
     91  1.1  thorpej 			return (FALSE);
     92  1.1  thorpej 	}
     93  1.1  thorpej 	if (_prop_object_externalize_append_char(ctx, '<') == FALSE ||
     94  1.1  thorpej 	    _prop_object_externalize_append_cstring(ctx, tag) == FALSE ||
     95  1.1  thorpej 	    _prop_object_externalize_append_char(ctx, '>') == FALSE)
     96  1.1  thorpej 		return (FALSE);
     97  1.1  thorpej 
     98  1.1  thorpej 	return (TRUE);
     99  1.1  thorpej }
    100  1.1  thorpej 
    101  1.1  thorpej /*
    102  1.1  thorpej  * _prop_object_externalize_end_tag --
    103  1.1  thorpej  *	Append an XML-style end tag to the externalize buffer.
    104  1.1  thorpej  */
    105  1.1  thorpej boolean_t
    106  1.1  thorpej _prop_object_externalize_end_tag(
    107  1.1  thorpej     struct _prop_object_externalize_context *ctx, const char *tag)
    108  1.1  thorpej {
    109  1.1  thorpej 
    110  1.1  thorpej 	if (_prop_object_externalize_append_char(ctx, '<') == FALSE ||
    111  1.1  thorpej 	    _prop_object_externalize_append_char(ctx, '/') == FALSE ||
    112  1.1  thorpej 	    _prop_object_externalize_append_cstring(ctx, tag) == FALSE ||
    113  1.1  thorpej 	    _prop_object_externalize_append_char(ctx, '>') == FALSE ||
    114  1.1  thorpej 	    _prop_object_externalize_append_char(ctx, '\n') == FALSE)
    115  1.1  thorpej 		return (FALSE);
    116  1.1  thorpej 
    117  1.1  thorpej 	return (TRUE);
    118  1.1  thorpej }
    119  1.1  thorpej 
    120  1.1  thorpej /*
    121  1.1  thorpej  * _prop_object_externalize_empty_tag --
    122  1.1  thorpej  *	Append an XML-style empty tag to the externalize buffer.
    123  1.1  thorpej  */
    124  1.1  thorpej boolean_t
    125  1.1  thorpej _prop_object_externalize_empty_tag(
    126  1.1  thorpej     struct _prop_object_externalize_context *ctx, const char *tag)
    127  1.1  thorpej {
    128  1.1  thorpej 	unsigned int i;
    129  1.1  thorpej 
    130  1.1  thorpej 	for (i = 0; i < ctx->poec_depth; i++) {
    131  1.1  thorpej 		if (_prop_object_externalize_append_char(ctx, '\t') == FALSE)
    132  1.1  thorpej 			return (FALSE);
    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_cstring(ctx, tag) == FALSE ||
    137  1.1  thorpej 	    _prop_object_externalize_append_char(ctx, '/') == 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_append_cstring --
    147  1.1  thorpej  *	Append a C string to the externalize buffer.
    148  1.1  thorpej  */
    149  1.1  thorpej boolean_t
    150  1.1  thorpej _prop_object_externalize_append_cstring(
    151  1.1  thorpej     struct _prop_object_externalize_context *ctx, const char *cp)
    152  1.1  thorpej {
    153  1.1  thorpej 
    154  1.1  thorpej 	while (*cp != '\0') {
    155  1.1  thorpej 		if (_prop_object_externalize_append_char(ctx,
    156  1.1  thorpej 						(unsigned char) *cp) == FALSE)
    157  1.1  thorpej 			return (FALSE);
    158  1.1  thorpej 		cp++;
    159  1.1  thorpej 	}
    160  1.1  thorpej 
    161  1.1  thorpej 	return (TRUE);
    162  1.1  thorpej }
    163  1.1  thorpej 
    164  1.1  thorpej /*
    165  1.1  thorpej  * _prop_object_externalize_append_encoded_cstring --
    166  1.1  thorpej  *	Append an encoded C string to the externalize buffer.
    167  1.1  thorpej  */
    168  1.1  thorpej boolean_t
    169  1.1  thorpej _prop_object_externalize_append_encoded_cstring(
    170  1.1  thorpej     struct _prop_object_externalize_context *ctx, const char *cp)
    171  1.1  thorpej {
    172  1.1  thorpej 
    173  1.1  thorpej 	while (*cp != '\0') {
    174  1.1  thorpej 		switch (*cp) {
    175  1.1  thorpej 		case '<':
    176  1.1  thorpej 			if (_prop_object_externalize_append_cstring(ctx,
    177  1.1  thorpej 					"&lt;") == FALSE)
    178  1.1  thorpej 				return (FALSE);
    179  1.1  thorpej 			break;
    180  1.1  thorpej 		case '>':
    181  1.1  thorpej 			if (_prop_object_externalize_append_cstring(ctx,
    182  1.1  thorpej 					"&gt;") == FALSE)
    183  1.1  thorpej 				return (FALSE);
    184  1.1  thorpej 			break;
    185  1.1  thorpej 		case '&':
    186  1.1  thorpej 			if (_prop_object_externalize_append_cstring(ctx,
    187  1.1  thorpej 					"&amp;") == FALSE)
    188  1.1  thorpej 				return (FALSE);
    189  1.1  thorpej 			break;
    190  1.1  thorpej 		default:
    191  1.1  thorpej 			if (_prop_object_externalize_append_char(ctx,
    192  1.1  thorpej 					(unsigned char) *cp) == FALSE)
    193  1.1  thorpej 				return (FALSE);
    194  1.1  thorpej 			break;
    195  1.1  thorpej 		}
    196  1.1  thorpej 		cp++;
    197  1.1  thorpej 	}
    198  1.1  thorpej 
    199  1.1  thorpej 	return (TRUE);
    200  1.1  thorpej }
    201  1.1  thorpej 
    202  1.1  thorpej #define	BUF_EXPAND		256
    203  1.1  thorpej 
    204  1.1  thorpej /*
    205  1.1  thorpej  * _prop_object_externalize_append_char --
    206  1.1  thorpej  *	Append a single character to the externalize buffer.
    207  1.1  thorpej  */
    208  1.1  thorpej boolean_t
    209  1.1  thorpej _prop_object_externalize_append_char(
    210  1.1  thorpej     struct _prop_object_externalize_context *ctx, unsigned char c)
    211  1.1  thorpej {
    212  1.1  thorpej 
    213  1.1  thorpej 	_PROP_ASSERT(ctx->poec_capacity != 0);
    214  1.1  thorpej 	_PROP_ASSERT(ctx->poec_buf != NULL);
    215  1.1  thorpej 	_PROP_ASSERT(ctx->poec_len <= ctx->poec_capacity);
    216  1.1  thorpej 
    217  1.1  thorpej 	if (ctx->poec_len == ctx->poec_capacity) {
    218  1.1  thorpej 		char *ocp = ctx->poec_buf;
    219  1.1  thorpej 		char *cp = _PROP_MALLOC(ctx->poec_capacity + BUF_EXPAND,
    220  1.1  thorpej 					M_TEMP);
    221  1.1  thorpej 		if (cp == NULL)
    222  1.1  thorpej 			return (FALSE);
    223  1.1  thorpej 		memcpy(cp, ocp, ctx->poec_capacity);
    224  1.1  thorpej 		ctx->poec_capacity = ctx->poec_capacity + BUF_EXPAND;
    225  1.1  thorpej 		_PROP_FREE(ocp, M_TEMP);
    226  1.1  thorpej 		ctx->poec_buf = cp;
    227  1.1  thorpej 	}
    228  1.1  thorpej 
    229  1.1  thorpej 	ctx->poec_buf[ctx->poec_len++] = c;
    230  1.1  thorpej 
    231  1.1  thorpej 	return (TRUE);
    232  1.1  thorpej }
    233  1.1  thorpej 
    234  1.1  thorpej /*
    235  1.1  thorpej  * _prop_object_externalize_context_alloc --
    236  1.1  thorpej  *	Allocate an externalize context.
    237  1.1  thorpej  */
    238  1.1  thorpej struct _prop_object_externalize_context *
    239  1.1  thorpej _prop_object_externalize_context_alloc(void)
    240  1.1  thorpej {
    241  1.1  thorpej 	struct _prop_object_externalize_context *ctx;
    242  1.1  thorpej 
    243  1.1  thorpej 	ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
    244  1.1  thorpej 	if (ctx != NULL) {
    245  1.1  thorpej 		ctx->poec_buf = _PROP_MALLOC(BUF_EXPAND, M_TEMP);
    246  1.1  thorpej 		if (ctx->poec_buf == NULL) {
    247  1.1  thorpej 			_PROP_FREE(ctx, M_TEMP);
    248  1.1  thorpej 			return (NULL);
    249  1.1  thorpej 		}
    250  1.1  thorpej 		ctx->poec_len = 0;
    251  1.1  thorpej 		ctx->poec_capacity = BUF_EXPAND;
    252  1.1  thorpej 		ctx->poec_depth = 0;
    253  1.1  thorpej 	}
    254  1.1  thorpej 	return (ctx);
    255  1.1  thorpej }
    256  1.1  thorpej 
    257  1.1  thorpej /*
    258  1.1  thorpej  * _prop_object_externalize_context_free --
    259  1.1  thorpej  *	Free an externalize context.
    260  1.1  thorpej  */
    261  1.1  thorpej void
    262  1.1  thorpej _prop_object_externalize_context_free(
    263  1.1  thorpej 		struct _prop_object_externalize_context *ctx)
    264  1.1  thorpej {
    265  1.1  thorpej 
    266  1.1  thorpej 	/* Buffer is always freed by the caller. */
    267  1.1  thorpej 	_PROP_FREE(ctx, M_TEMP);
    268  1.1  thorpej }
    269  1.1  thorpej 
    270  1.1  thorpej /*
    271  1.1  thorpej  * _prop_object_internalize_skip_comment --
    272  1.1  thorpej  *	Skip the body and end tag of a comment.
    273  1.1  thorpej  */
    274  1.1  thorpej static boolean_t
    275  1.1  thorpej _prop_object_internalize_skip_comment(
    276  1.1  thorpej 				struct _prop_object_internalize_context *ctx)
    277  1.1  thorpej {
    278  1.1  thorpej 	const char *cp = ctx->poic_cp;
    279  1.1  thorpej 
    280  1.1  thorpej 	while (!_PROP_EOF(*cp)) {
    281  1.1  thorpej 		if (cp[0] == '-' &&
    282  1.1  thorpej 		    cp[1] == '-' &&
    283  1.1  thorpej 		    cp[2] == '>') {
    284  1.1  thorpej 			ctx->poic_cp = cp + 3;
    285  1.1  thorpej 			return (TRUE);
    286  1.1  thorpej 		}
    287  1.1  thorpej 		cp++;
    288  1.1  thorpej 	}
    289  1.1  thorpej 
    290  1.1  thorpej 	return (FALSE);		/* ran out of buffer */
    291  1.1  thorpej }
    292  1.1  thorpej 
    293  1.1  thorpej /*
    294  1.1  thorpej  * _prop_object_internalize_find_tag --
    295  1.1  thorpej  *	Find the next tag in an XML stream.  Optionally compare the found
    296  1.1  thorpej  *	tag to an expected tag name.  State of the context is undefined
    297  1.1  thorpej  *	if this routine returns FALSE.  Upon success, the context points
    298  1.1  thorpej  *	to the first octet after the tag.
    299  1.1  thorpej  */
    300  1.1  thorpej boolean_t
    301  1.1  thorpej _prop_object_internalize_find_tag(struct _prop_object_internalize_context *ctx,
    302  1.1  thorpej 		      const char *tag, _prop_tag_type_t type)
    303  1.1  thorpej {
    304  1.1  thorpej 	const char *cp;
    305  1.1  thorpej 	size_t taglen;
    306  1.1  thorpej 
    307  1.1  thorpej 	if (tag != NULL)
    308  1.1  thorpej 		taglen = strlen(tag);
    309  1.1  thorpej 	else
    310  1.1  thorpej 		taglen = 0;
    311  1.1  thorpej 
    312  1.1  thorpej  start_over:
    313  1.1  thorpej 	cp = ctx->poic_cp;
    314  1.1  thorpej 
    315  1.1  thorpej 	/*
    316  1.1  thorpej 	 * Find the start of the tag.
    317  1.1  thorpej 	 */
    318  1.1  thorpej 	while (_PROP_ISSPACE(*cp))
    319  1.1  thorpej 		cp++;
    320  1.1  thorpej 	if (_PROP_EOF(*cp))
    321  1.1  thorpej 		return (FALSE);
    322  1.1  thorpej 
    323  1.1  thorpej 	if (*cp != '<')
    324  1.1  thorpej 		return (FALSE);
    325  1.1  thorpej 
    326  1.1  thorpej 	ctx->poic_tag_start = cp++;
    327  1.1  thorpej 	if (_PROP_EOF(*cp))
    328  1.1  thorpej 		return (FALSE);
    329  1.1  thorpej 
    330  1.1  thorpej 	if (*cp == '!') {
    331  1.1  thorpej 		if (cp[1] != '-' || cp[2] != '-')
    332  1.1  thorpej 			return (FALSE);
    333  1.1  thorpej 		/*
    334  1.1  thorpej 		 * Comment block -- only allowed if we are allowed to
    335  1.1  thorpej 		 * return a start tag.
    336  1.1  thorpej 		 */
    337  1.1  thorpej 		if (type == _PROP_TAG_TYPE_END)
    338  1.1  thorpej 			return (FALSE);
    339  1.1  thorpej 		ctx->poic_cp = cp + 3;
    340  1.1  thorpej 		if (_prop_object_internalize_skip_comment(ctx) == FALSE)
    341  1.1  thorpej 			return (FALSE);
    342  1.1  thorpej 		goto start_over;
    343  1.1  thorpej 	}
    344  1.1  thorpej 
    345  1.1  thorpej 	if (*cp == '/') {
    346  1.1  thorpej 		if (type != _PROP_TAG_TYPE_END &&
    347  1.1  thorpej 		    type != _PROP_TAG_TYPE_EITHER)
    348  1.1  thorpej 			return (FALSE);
    349  1.1  thorpej 		cp++;
    350  1.1  thorpej 		if (_PROP_EOF(*cp))
    351  1.1  thorpej 			return (FALSE);
    352  1.1  thorpej 		ctx->poic_tag_type = _PROP_TAG_TYPE_END;
    353  1.1  thorpej 	} else {
    354  1.1  thorpej 		if (type != _PROP_TAG_TYPE_START &&
    355  1.1  thorpej 		    type != _PROP_TAG_TYPE_EITHER)
    356  1.1  thorpej 			return (FALSE);
    357  1.1  thorpej 		ctx->poic_tag_type = _PROP_TAG_TYPE_START;
    358  1.1  thorpej 	}
    359  1.1  thorpej 
    360  1.1  thorpej 	ctx->poic_tagname = cp;
    361  1.1  thorpej 
    362  1.1  thorpej 	while (!_PROP_ISSPACE(*cp) && *cp != '/' && *cp != '>')
    363  1.1  thorpej 		cp++;
    364  1.1  thorpej 	if (_PROP_EOF(*cp))
    365  1.1  thorpej 		return (FALSE);
    366  1.1  thorpej 
    367  1.1  thorpej 	ctx->poic_tagname_len = cp - ctx->poic_tagname;
    368  1.1  thorpej 
    369  1.1  thorpej 	/* Make sure this is the tag we're looking for. */
    370  1.1  thorpej 	if (tag != NULL &&
    371  1.1  thorpej 	    (taglen != ctx->poic_tagname_len ||
    372  1.1  thorpej 	     memcmp(tag, ctx->poic_tagname, taglen) != 0))
    373  1.1  thorpej 		return (FALSE);
    374  1.1  thorpej 
    375  1.1  thorpej 	/* Check for empty tag. */
    376  1.1  thorpej 	if (*cp == '/') {
    377  1.1  thorpej 		if (ctx->poic_tag_type != _PROP_TAG_TYPE_START)
    378  1.1  thorpej 			return(FALSE);		/* only valid on start tags */
    379  1.1  thorpej 		ctx->poic_is_empty_element = TRUE;
    380  1.1  thorpej 		cp++;
    381  1.1  thorpej 		if (_PROP_EOF(*cp) || *cp != '>')
    382  1.1  thorpej 			return (FALSE);
    383  1.1  thorpej 	} else
    384  1.1  thorpej 		ctx->poic_is_empty_element = FALSE;
    385  1.1  thorpej 
    386  1.1  thorpej 	/* Easy case of no arguments. */
    387  1.1  thorpej 	if (*cp == '>') {
    388  1.1  thorpej 		ctx->poic_tagattr = NULL;
    389  1.1  thorpej 		ctx->poic_tagattr_len = 0;
    390  1.1  thorpej 		ctx->poic_tagattrval = NULL;
    391  1.1  thorpej 		ctx->poic_tagattrval_len = 0;
    392  1.1  thorpej 		ctx->poic_cp = cp + 1;
    393  1.1  thorpej 		return (TRUE);
    394  1.1  thorpej 	}
    395  1.1  thorpej 
    396  1.1  thorpej 	_PROP_ASSERT(!_PROP_EOF(*cp));
    397  1.1  thorpej 	cp++;
    398  1.1  thorpej 	if (_PROP_EOF(*cp))
    399  1.1  thorpej 		return (FALSE);
    400  1.1  thorpej 
    401  1.1  thorpej 	while (_PROP_ISSPACE(*cp))
    402  1.1  thorpej 		cp++;
    403  1.1  thorpej 	if (_PROP_EOF(*cp))
    404  1.1  thorpej 		return (FALSE);
    405  1.1  thorpej 
    406  1.1  thorpej 	ctx->poic_tagattr = cp;
    407  1.1  thorpej 
    408  1.1  thorpej 	while (!_PROP_ISSPACE(*cp) && *cp != '=')
    409  1.1  thorpej 		cp++;
    410  1.1  thorpej 	if (_PROP_EOF(*cp))
    411  1.1  thorpej 		return (FALSE);
    412  1.1  thorpej 
    413  1.1  thorpej 	ctx->poic_tagattr_len = cp - ctx->poic_tagattr;
    414  1.1  thorpej 
    415  1.1  thorpej 	cp++;
    416  1.1  thorpej 	if (*cp != '\"')
    417  1.1  thorpej 		return (FALSE);
    418  1.1  thorpej 	cp++;
    419  1.1  thorpej 	if (_PROP_EOF(*cp))
    420  1.1  thorpej 		return (FALSE);
    421  1.1  thorpej 
    422  1.1  thorpej 	ctx->poic_tagattrval = cp;
    423  1.1  thorpej 	while (*cp != '\"')
    424  1.1  thorpej 		cp++;
    425  1.1  thorpej 	if (_PROP_EOF(*cp))
    426  1.1  thorpej 		return (FALSE);
    427  1.1  thorpej 	ctx->poic_tagattrval_len = cp - ctx->poic_tagattrval;
    428  1.1  thorpej 
    429  1.1  thorpej 	cp++;
    430  1.1  thorpej 	if (*cp != '>')
    431  1.1  thorpej 		return (FALSE);
    432  1.1  thorpej 
    433  1.1  thorpej 	ctx->poic_cp = cp + 1;
    434  1.1  thorpej 	return (TRUE);
    435  1.1  thorpej }
    436  1.1  thorpej 
    437  1.1  thorpej /*
    438  1.1  thorpej  * _prop_object_internalize_decode_string --
    439  1.1  thorpej  *	Decode an encoded string.
    440  1.1  thorpej  */
    441  1.1  thorpej boolean_t
    442  1.1  thorpej _prop_object_internalize_decode_string(
    443  1.1  thorpej 				struct _prop_object_internalize_context *ctx,
    444  1.1  thorpej 				char *target, size_t targsize, size_t *sizep,
    445  1.1  thorpej 				const char **cpp)
    446  1.1  thorpej {
    447  1.1  thorpej 	const char *src;
    448  1.1  thorpej 	size_t tarindex;
    449  1.1  thorpej 	char c;
    450  1.1  thorpej 
    451  1.1  thorpej 	tarindex = 0;
    452  1.1  thorpej 	src = ctx->poic_cp;
    453  1.1  thorpej 
    454  1.1  thorpej 	for (;;) {
    455  1.1  thorpej 		if (_PROP_EOF(*src))
    456  1.1  thorpej 			return (FALSE);
    457  1.1  thorpej 		if (*src == '<') {
    458  1.1  thorpej 			break;
    459  1.1  thorpej 		}
    460  1.1  thorpej 
    461  1.1  thorpej 		if ((c = *src) == '&') {
    462  1.1  thorpej 			if (src[1] == 'a' &&
    463  1.1  thorpej 			    src[2] == 'm' &&
    464  1.1  thorpej 			    src[3] == 'p' &&
    465  1.1  thorpej 			    src[4] == ';') {
    466  1.1  thorpej 			    	c = '&';
    467  1.1  thorpej 				src += 5;
    468  1.1  thorpej 			} else if (src[1] == 'l' &&
    469  1.1  thorpej 				   src[2] == 't' &&
    470  1.1  thorpej 				   src[3] == ';') {
    471  1.1  thorpej 				c = '<';
    472  1.1  thorpej 				src += 4;
    473  1.1  thorpej 			} else if (src[1] == 'g' &&
    474  1.1  thorpej 				   src[2] == 't' &&
    475  1.1  thorpej 				   src[3] == ';') {
    476  1.1  thorpej 				c = '>';
    477  1.1  thorpej 				src += 4;
    478  1.1  thorpej 			} else if (src[1] == 'a' &&
    479  1.1  thorpej 				   src[2] == 'p' &&
    480  1.1  thorpej 				   src[3] == 'o' &&
    481  1.1  thorpej 				   src[4] == 's' &&
    482  1.1  thorpej 				   src[5] == ';') {
    483  1.1  thorpej 				c = '\'';
    484  1.1  thorpej 				src += 6;
    485  1.1  thorpej 			} else if (src[1] == 'q' &&
    486  1.1  thorpej 				   src[2] == 'u' &&
    487  1.1  thorpej 				   src[3] == 'o' &&
    488  1.1  thorpej 				   src[4] == 't' &&
    489  1.1  thorpej 				   src[5] == ';') {
    490  1.1  thorpej 				c = '\"';
    491  1.1  thorpej 				src += 6;
    492  1.1  thorpej 			} else
    493  1.1  thorpej 				return (FALSE);
    494  1.1  thorpej 		} else
    495  1.1  thorpej 			src++;
    496  1.1  thorpej 		if (target) {
    497  1.1  thorpej 			if (tarindex >= targsize)
    498  1.1  thorpej 				return (FALSE);
    499  1.1  thorpej 			target[tarindex] = c;
    500  1.1  thorpej 		}
    501  1.1  thorpej 		tarindex++;
    502  1.1  thorpej 	}
    503  1.1  thorpej 
    504  1.1  thorpej 	_PROP_ASSERT(*src == '<');
    505  1.1  thorpej 	if (sizep != NULL)
    506  1.1  thorpej 		*sizep = tarindex;
    507  1.1  thorpej 	if (cpp != NULL)
    508  1.1  thorpej 		*cpp = src;
    509  1.1  thorpej 
    510  1.1  thorpej 	return (TRUE);
    511  1.1  thorpej }
    512  1.1  thorpej 
    513  1.1  thorpej /*
    514  1.1  thorpej  * _prop_object_internalize_match --
    515  1.1  thorpej  *	Returns true if the two character streams match.
    516  1.1  thorpej  */
    517  1.1  thorpej boolean_t
    518  1.1  thorpej _prop_object_internalize_match(const char *str1, size_t len1,
    519  1.1  thorpej 			       const char *str2, size_t len2)
    520  1.1  thorpej {
    521  1.1  thorpej 
    522  1.1  thorpej 	return (len1 == len2 && memcmp(str1, str2, len1) == 0);
    523  1.1  thorpej }
    524  1.1  thorpej 
    525  1.1  thorpej #define	INTERNALIZER(t, f)			\
    526  1.1  thorpej {	t,	sizeof(t) - 1,		f	}
    527  1.1  thorpej 
    528  1.1  thorpej static const struct _prop_object_internalizer {
    529  1.1  thorpej 	const char	*poi_tag;
    530  1.1  thorpej 	size_t		poi_taglen;
    531  1.1  thorpej 	prop_object_t	(*poi_intern)(
    532  1.1  thorpej 				struct _prop_object_internalize_context *);
    533  1.1  thorpej } _prop_object_internalizer_table[] = {
    534  1.1  thorpej 	INTERNALIZER("array", _prop_array_internalize),
    535  1.1  thorpej 
    536  1.1  thorpej 	INTERNALIZER("true", _prop_bool_internalize),
    537  1.1  thorpej 	INTERNALIZER("false", _prop_bool_internalize),
    538  1.1  thorpej 
    539  1.1  thorpej 	INTERNALIZER("data", _prop_data_internalize),
    540  1.1  thorpej 
    541  1.1  thorpej 	INTERNALIZER("dict", _prop_dictionary_internalize),
    542  1.1  thorpej 
    543  1.1  thorpej 	INTERNALIZER("integer", _prop_number_internalize),
    544  1.1  thorpej 
    545  1.1  thorpej 	INTERNALIZER("string", _prop_string_internalize),
    546  1.1  thorpej 
    547  1.1  thorpej 	{ 0 }
    548  1.1  thorpej };
    549  1.1  thorpej 
    550  1.1  thorpej #undef INTERNALIZER
    551  1.1  thorpej 
    552  1.1  thorpej /*
    553  1.1  thorpej  * _prop_object_internalize_by_tag --
    554  1.1  thorpej  *	Determine the object type from the tag in the context and
    555  1.1  thorpej  *	internalize it.
    556  1.1  thorpej  */
    557  1.1  thorpej prop_object_t
    558  1.1  thorpej _prop_object_internalize_by_tag(struct _prop_object_internalize_context *ctx)
    559  1.1  thorpej {
    560  1.1  thorpej 	const struct _prop_object_internalizer *poi;
    561  1.1  thorpej 
    562  1.1  thorpej 	for (poi = _prop_object_internalizer_table;
    563  1.1  thorpej 	     poi->poi_tag != NULL; poi++) {
    564  1.1  thorpej 		if (_prop_object_internalize_match(ctx->poic_tagname,
    565  1.1  thorpej 						   ctx->poic_tagname_len,
    566  1.1  thorpej 						   poi->poi_tag,
    567  1.1  thorpej 						   poi->poi_taglen))
    568  1.1  thorpej 			return ((*poi->poi_intern)(ctx));
    569  1.1  thorpej 	}
    570  1.1  thorpej 
    571  1.1  thorpej 	return (NULL);
    572  1.1  thorpej }
    573  1.1  thorpej 
    574  1.1  thorpej /*
    575  1.1  thorpej  * _prop_object_internalize_context_alloc --
    576  1.1  thorpej  *	Allocate an internalize context.
    577  1.1  thorpej  */
    578  1.1  thorpej struct _prop_object_internalize_context *
    579  1.1  thorpej _prop_object_internalize_context_alloc(const char *xml)
    580  1.1  thorpej {
    581  1.1  thorpej 	struct _prop_object_internalize_context *ctx;
    582  1.1  thorpej 
    583  1.1  thorpej 	ctx = _PROP_MALLOC(sizeof(struct _prop_object_internalize_context),
    584  1.1  thorpej 			   M_TEMP);
    585  1.1  thorpej 	if (ctx == NULL)
    586  1.1  thorpej 		return (NULL);
    587  1.1  thorpej 
    588  1.1  thorpej 	ctx->poic_xml = ctx->poic_cp = xml;
    589  1.1  thorpej 
    590  1.1  thorpej 	/*
    591  1.1  thorpej 	 * Skip any whitespace and XML preamble stuff that we don't
    592  1.1  thorpej 	 * know about / care about.
    593  1.1  thorpej 	 */
    594  1.1  thorpej 	for (;;) {
    595  1.1  thorpej 		while (_PROP_ISSPACE(*xml))
    596  1.1  thorpej 			xml++;
    597  1.1  thorpej 		if (_PROP_EOF(*xml) || *xml != '<')
    598  1.1  thorpej 			goto bad;
    599  1.1  thorpej 
    600  1.1  thorpej #define	MATCH(str)	(memcmp(&xml[1], str, sizeof(str) - 1) == 0)
    601  1.1  thorpej 
    602  1.1  thorpej 		/*
    603  1.1  thorpej 		 * Skip over the XML preamble that Apple XML property
    604  1.1  thorpej 		 * lists usually include at the top of the file.
    605  1.1  thorpej 		 */
    606  1.1  thorpej 		if (MATCH("?xml ") ||
    607  1.1  thorpej 		    MATCH("!DOCTYPE plist")) {
    608  1.1  thorpej 			while (*xml != '>' && !_PROP_EOF(*xml))
    609  1.1  thorpej 				xml++;
    610  1.1  thorpej 			if (_PROP_EOF(*xml))
    611  1.1  thorpej 				goto bad;
    612  1.1  thorpej 			xml++;	/* advance past the '>' */
    613  1.1  thorpej 			continue;
    614  1.1  thorpej 		}
    615  1.1  thorpej 
    616  1.1  thorpej 		if (MATCH("<!--")) {
    617  1.1  thorpej 			ctx->poic_cp = xml + 4;
    618  1.1  thorpej 			if (_prop_object_internalize_skip_comment(ctx) == FALSE)
    619  1.1  thorpej 				goto bad;
    620  1.1  thorpej 			xml = ctx->poic_cp;
    621  1.1  thorpej 			continue;
    622  1.1  thorpej 		}
    623  1.1  thorpej 
    624  1.1  thorpej #undef MATCH
    625  1.1  thorpej 
    626  1.1  thorpej 		/*
    627  1.1  thorpej 		 * We don't think we should skip it, so let's hope we can
    628  1.1  thorpej 		 * parse it.
    629  1.1  thorpej 		 */
    630  1.1  thorpej 		break;
    631  1.1  thorpej 	}
    632  1.1  thorpej 
    633  1.1  thorpej 	ctx->poic_cp = xml;
    634  1.1  thorpej 	return (ctx);
    635  1.1  thorpej  bad:
    636  1.1  thorpej 	_PROP_FREE(ctx, M_TEMP);
    637  1.1  thorpej 	return (NULL);
    638  1.1  thorpej }
    639  1.1  thorpej 
    640  1.1  thorpej /*
    641  1.1  thorpej  * _prop_object_internalize_context_free --
    642  1.1  thorpej  *	Free an internalize context.
    643  1.1  thorpej  */
    644  1.1  thorpej void
    645  1.1  thorpej _prop_object_internalize_context_free(
    646  1.1  thorpej 		struct _prop_object_internalize_context *ctx)
    647  1.1  thorpej {
    648  1.1  thorpej 
    649  1.1  thorpej 	_PROP_FREE(ctx, M_TEMP);
    650  1.1  thorpej }
    651  1.1  thorpej 
    652  1.1  thorpej /*
    653  1.1  thorpej  * Retain / release serialization --
    654  1.1  thorpej  *
    655  1.1  thorpej  * Eventually we would like to use atomic operations.  But until we have
    656  1.1  thorpej  * an MI API for them that is common to userland and the kernel, we will
    657  1.1  thorpej  * use a lock instead.
    658  1.1  thorpej  *
    659  1.1  thorpej  * We use a single global mutex for all serialization.  In the kernel, because
    660  1.1  thorpej  * we are still under a biglock, this will basically never contend (properties
    661  1.1  thorpej  * cannot be manipulated at interrupt level).  In userland, this will cost
    662  1.1  thorpej  * nothing for single-threaded programs.  For multi-threaded programs, there
    663  1.1  thorpej  * could be contention, but it probably won't cost that much unless the program
    664  1.1  thorpej  * makes heavy use of property lists.
    665  1.1  thorpej  */
    666  1.1  thorpej #if defined(_KERNEL)
    667  1.1  thorpej #include <sys/lock.h>
    668  1.1  thorpej static struct simplelock _prop_refcnt_mutex = SIMPLELOCK_INITIALIZER;
    669  1.1  thorpej 
    670  1.1  thorpej #define	_PROP_REFCNT_LOCK()	simple_lock(&_prop_refcnt_mutex)
    671  1.1  thorpej #define	_PROP_REFCNT_UNLOCK()	simple_unlock(&_prop_refcnt_mutex)
    672  1.1  thorpej #elif defined(_STANDALONE)
    673  1.1  thorpej /*
    674  1.1  thorpej  * No locking necessary for standalone environments.
    675  1.1  thorpej  */
    676  1.1  thorpej #define	_PROP_REFCNT_LOCK()	/* nothing */
    677  1.1  thorpej #define	_PROP_REFCNT_UNLOCK()	/* nothing */
    678  1.1  thorpej #elif defined(__NetBSD__) && defined(_LIBPROP)
    679  1.1  thorpej /*
    680  1.1  thorpej  * Use the same mechanism as libc; we get pthread mutexes for threaded
    681  1.1  thorpej  * programs and do-nothing stubs for non-threaded programs.
    682  1.1  thorpej  */
    683  1.1  thorpej #include "reentrant.h"
    684  1.1  thorpej static mutex_t _prop_refcnt_mutex = MUTEX_INITIALIZER;
    685  1.1  thorpej 
    686  1.1  thorpej #define	_PROP_REFCNT_LOCK()	mutex_lock(&_prop_refcnt_mutex)
    687  1.1  thorpej #define	_PROP_REFCNT_UNLOCK()	mutex_unlock(&_prop_refcnt_mutex)
    688  1.1  thorpej #elif defined(HAVE_NBTOOL_CONFIG_H)
    689  1.1  thorpej /* None of NetBSD's build tools are multi-threaded. */
    690  1.1  thorpej #define	_PROP_REFCNT_LOCK()	/* nothing */
    691  1.1  thorpej #define	_PROP_REFCNT_UNLOCK()	/* nothing */
    692  1.1  thorpej #else
    693  1.1  thorpej /* Use pthread mutexes everywhere else. */
    694  1.1  thorpej #include <pthread.h>
    695  1.1  thorpej static pthread_mutex_t _prop_refcnt_mutex = PTHREAD_MUTEX_INITIALIZER;
    696  1.1  thorpej 
    697  1.1  thorpej #define	_PROP_REFCNT_LOCK()	pthread_mutex_lock(&_prop_refcnt_mutex)
    698  1.1  thorpej #define	_PROP_REFCNT_UNLOCK()	pthread_mutex_unlock(&_prop_refcnt_mutex)
    699  1.1  thorpej #endif
    700  1.1  thorpej 
    701  1.1  thorpej /*
    702  1.1  thorpej  * prop_object_retain --
    703  1.1  thorpej  *	Increment the reference count on an object.
    704  1.1  thorpej  */
    705  1.1  thorpej void
    706  1.1  thorpej prop_object_retain(prop_object_t obj)
    707  1.1  thorpej {
    708  1.1  thorpej 	struct _prop_object *po = obj;
    709  1.1  thorpej 	uint32_t ocnt;
    710  1.1  thorpej 
    711  1.1  thorpej 	_PROP_REFCNT_LOCK();
    712  1.1  thorpej 	ocnt = po->po_refcnt++;
    713  1.1  thorpej 	_PROP_REFCNT_UNLOCK();
    714  1.1  thorpej 
    715  1.1  thorpej 	_PROP_ASSERT(ocnt != 0xffffffffU);
    716  1.1  thorpej }
    717  1.1  thorpej 
    718  1.1  thorpej /*
    719  1.1  thorpej  * prop_object_release --
    720  1.1  thorpej  *	Decrement the reference count on an object.
    721  1.1  thorpej  *
    722  1.1  thorpej  *	Free the object if we are releasing the final
    723  1.1  thorpej  *	reference.
    724  1.1  thorpej  */
    725  1.1  thorpej void
    726  1.1  thorpej prop_object_release(prop_object_t obj)
    727  1.1  thorpej {
    728  1.1  thorpej 	struct _prop_object *po = obj;
    729  1.1  thorpej 	uint32_t ocnt;
    730  1.1  thorpej 
    731  1.1  thorpej 	_PROP_REFCNT_LOCK();
    732  1.1  thorpej 	ocnt = po->po_refcnt--;
    733  1.1  thorpej 	_PROP_REFCNT_UNLOCK();
    734  1.1  thorpej 
    735  1.1  thorpej 	_PROP_ASSERT(ocnt != 0);
    736  1.1  thorpej 	if (ocnt == 1)
    737  1.1  thorpej 		(*po->po_free)(po);
    738  1.1  thorpej }
    739  1.1  thorpej 
    740  1.1  thorpej /*
    741  1.1  thorpej  * prop_object_type --
    742  1.1  thorpej  *	Return the type of an object.
    743  1.1  thorpej  */
    744  1.1  thorpej prop_type_t
    745  1.1  thorpej prop_object_type(prop_object_t obj)
    746  1.1  thorpej {
    747  1.1  thorpej 	struct _prop_object *po = obj;
    748  1.1  thorpej 
    749  1.1  thorpej 	return (po->po_type);
    750  1.1  thorpej }
    751  1.1  thorpej 
    752  1.1  thorpej /*
    753  1.1  thorpej  * prop_object_iterator_next --
    754  1.1  thorpej  *	Return the next item during an iteration.
    755  1.1  thorpej  */
    756  1.1  thorpej prop_object_t
    757  1.1  thorpej prop_object_iterator_next(prop_object_iterator_t pi)
    758  1.1  thorpej {
    759  1.1  thorpej 
    760  1.1  thorpej 	return ((*pi->pi_next_object)(pi));
    761  1.1  thorpej }
    762  1.1  thorpej 
    763  1.1  thorpej /*
    764  1.1  thorpej  * prop_object_iterator_reset --
    765  1.1  thorpej  *	Reset the iterator to the first object so as to restart
    766  1.1  thorpej  *	iteration.
    767  1.1  thorpej  */
    768  1.1  thorpej void
    769  1.1  thorpej prop_object_iterator_reset(prop_object_iterator_t pi)
    770  1.1  thorpej {
    771  1.1  thorpej 
    772  1.1  thorpej 	(*pi->pi_reset)(pi);
    773  1.1  thorpej }
    774  1.1  thorpej 
    775  1.1  thorpej /*
    776  1.1  thorpej  * prop_object_iterator_release --
    777  1.1  thorpej  *	Release the object iterator.
    778  1.1  thorpej  */
    779  1.1  thorpej void
    780  1.1  thorpej prop_object_iterator_release(prop_object_iterator_t pi)
    781  1.1  thorpej {
    782  1.1  thorpej 
    783  1.1  thorpej 	prop_object_release(pi->pi_obj);
    784  1.1  thorpej 	_PROP_FREE(pi, M_TEMP);
    785  1.1  thorpej }
    786