Home | History | Annotate | Line # | Download | only in libprop
prop_object_impl.h revision 1.1
      1 /*	$NetBSD: prop_object_impl.h,v 1.1 2006/04/27 20:11:27 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_
     40 #define	_PROPLIB_PROP_OBJECT_IMPL_H_
     41 
     42 struct _prop_object_externalize_context {
     43 	char *		poec_buf;		/* string buffer */
     44 	size_t		poec_capacity;		/* capacity of buffer */
     45 	size_t		poec_len;		/* current length of string */
     46 	unsigned int	poec_depth;		/* nesting depth */
     47 };
     48 
     49 boolean_t	_prop_object_externalize_start_tag(
     50 				struct _prop_object_externalize_context *,
     51 				const char *);
     52 boolean_t	_prop_object_externalize_end_tag(
     53 				struct _prop_object_externalize_context *,
     54 				const char *);
     55 boolean_t	_prop_object_externalize_empty_tag(
     56 				struct _prop_object_externalize_context *,
     57 				const char *);
     58 boolean_t	_prop_object_externalize_append_cstring(
     59 				struct _prop_object_externalize_context *,
     60 				const char *);
     61 boolean_t	_prop_object_externalize_append_encoded_cstring(
     62 				struct _prop_object_externalize_context *,
     63 				const char *);
     64 boolean_t	_prop_object_externalize_append_char(
     65 				struct _prop_object_externalize_context *,
     66 				unsigned char);
     67 
     68 struct _prop_object_externalize_context *
     69 		_prop_object_externalize_context_alloc(void);
     70 void		_prop_object_externalize_context_free(
     71 				struct _prop_object_externalize_context *);
     72 
     73 typedef enum {
     74 	_PROP_TAG_TYPE_START,			/* e.g. <dict> */
     75 	_PROP_TAG_TYPE_END,			/* e.g. </dict> */
     76 	_PROP_TAG_TYPE_EITHER
     77 } _prop_tag_type_t;
     78 
     79 struct _prop_object_internalize_context {
     80 	const char *poic_xml;
     81 	const char *poic_cp;
     82 
     83 	const char *poic_tag_start;
     84 
     85 	const char *poic_tagname;
     86 	size_t      poic_tagname_len;
     87 	const char *poic_tagattr;
     88 	size_t      poic_tagattr_len;
     89 	const char *poic_tagattrval;
     90 	size_t      poic_tagattrval_len;
     91 
     92 	boolean_t   poic_is_empty_element;
     93 	_prop_tag_type_t poic_tag_type;
     94 };
     95 
     96 #define	_PROP_EOF(c)		((c) == '\0')
     97 #define	_PROP_ISSPACE(c)	\
     98 	((c) == ' ' || (c) == '\t' || (c) == '\n' || _PROP_EOF(c))
     99 
    100 #define	_PROP_TAG_MATCH(ctx, t)					\
    101 	_prop_object_internalize_match((ctx)->poic_tagname,	\
    102 				       (ctx)->poic_tagname_len,	\
    103 				       (t), strlen(t))
    104 
    105 #define	_PROP_TAGATTR_MATCH(ctx, a)				\
    106 	_prop_object_internalize_match((ctx)->poic_tagattr,	\
    107 				       (ctx)->poic_tagattr_len,	\
    108 				       (a), strlen(a))
    109 
    110 #define	_PROP_TAGATTRVAL_MATCH(ctx, a)				  \
    111 	_prop_object_internalize_match((ctx)->poic_tagattrval,	  \
    112 				       (ctx)->poic_tagattrval_len,\
    113 				       (a), strlen(a))
    114 
    115 boolean_t	_prop_object_internalize_find_tag(
    116 				struct _prop_object_internalize_context *,
    117 				const char *, _prop_tag_type_t);
    118 boolean_t	_prop_object_internalize_match(const char *, size_t,
    119 					       const char *, size_t);
    120 prop_object_t	_prop_object_internalize_by_tag(
    121 				struct _prop_object_internalize_context *);
    122 boolean_t	_prop_object_internalize_decode_string(
    123 				struct _prop_object_internalize_context *,
    124 				char *, size_t, size_t *, const char **);
    125 
    126 struct _prop_object_internalize_context *
    127 		_prop_object_internalize_context_alloc(const char *);
    128 void		_prop_object_internalize_context_free(
    129 				struct _prop_object_internalize_context *);
    130 
    131 	/* These are here because they're required by shared code. */
    132 prop_object_t	_prop_array_internalize(
    133 				struct _prop_object_internalize_context *);
    134 prop_object_t	_prop_bool_internalize(
    135 				struct _prop_object_internalize_context *);
    136 prop_object_t	_prop_data_internalize(
    137 				struct _prop_object_internalize_context *);
    138 prop_object_t	_prop_dictionary_internalize(
    139 				struct _prop_object_internalize_context *);
    140 prop_object_t	_prop_number_internalize(
    141 				struct _prop_object_internalize_context *);
    142 prop_object_t	_prop_string_internalize(
    143 				struct _prop_object_internalize_context *);
    144 
    145 struct _prop_object {
    146 	uint32_t	po_refcnt;		/* reference count */
    147 	uint32_t	po_type;		/* type indicator */
    148 	void		(*po_free)(void *);	/* func to free object */
    149 	boolean_t	(*po_extern)		/* func to externalize object */
    150 			    (struct _prop_object_externalize_context *,
    151 			     void *);
    152 };
    153 
    154 void	_prop_object_init(struct _prop_object *);
    155 void	_prop_object_fini(struct _prop_object *);
    156 
    157 struct _prop_object_iterator {
    158 	prop_object_t	(*pi_next_object)(void *);
    159 	void		(*pi_reset)(void *);
    160 	prop_object_t	pi_obj;
    161 	uint32_t	pi_version;
    162 };
    163 
    164 #if defined(_KERNEL)
    165 
    166 /*
    167  * proplib in the kernel...
    168  */
    169 
    170 #include <sys/malloc.h>
    171 #include <sys/pool.h>
    172 #include <sys/systm.h>
    173 
    174 #define	_PROP_ASSERT(x)		KASSERT(x)
    175 
    176 #define	_PROP_MALLOC(s, t)	malloc((s), (t), M_WAITOK)
    177 #define	_PROP_CALLOC(s, t)	malloc((s), (t), M_WAITOK | M_ZERO)
    178 #define	_PROP_FREE(v, t)	free((v), (t))
    179 
    180 #define	_PROP_POOL_GET(p)	pool_get(&(p), PR_WAITOK)
    181 #define	_PROP_POOL_PUT(p, v)	pool_put(&(p), (v))
    182 
    183 #define	_PROP_POOL_INIT(p, s, d)					\
    184 		POOL_INIT(p, s, 0, 0, 0, d, &pool_allocator_nointr);
    185 
    186 #define	_PROP_MALLOC_DEFINE(t, s, l)					\
    187 		MALLOC_DEFINE(t, s, l);
    188 
    189 #elif defined(_STANDALONE)
    190 
    191 /*
    192  * proplib in a standalone environment...
    193  */
    194 
    195 #include <lib/libsa/stand.h>
    196 #include <lib/libkern/libkern.h>
    197 
    198 void *		_prop_standalone_calloc(size_t);
    199 
    200 #define	_PROP_ASSERT(x)		/* nothing */
    201 
    202 #define	_PROP_MALLOC(s, t)	alloc((s))
    203 #define	_PROP_CALLOC(s, t)	_prop_standalone_calloc((s))
    204 #define	_PROP_FREE(v, t)	dealloc((v), 0)		/* XXX */
    205 
    206 #define	_PROP_POOL_GET(p)	alloc((p))
    207 #define	_PROP_POOL_PUT(p, v)	dealloc((v), (p))
    208 
    209 #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    210 
    211 #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    212 
    213 #else
    214 
    215 /*
    216  * proplib in user space...
    217  */
    218 
    219 #include <assert.h>
    220 #include <string.h>
    221 #include <stdio.h>
    222 #include <stdlib.h>
    223 
    224 #define	_PROP_ASSERT(x)		assert(x)
    225 
    226 #define	_PROP_MALLOC(s, t)	malloc((s))
    227 #define	_PROP_CALLOC(s, t)	calloc(1, (s))
    228 #define	_PROP_FREE(v, t)	free((v))
    229 
    230 #define	_PROP_POOL_GET(p)	malloc((p))
    231 #define	_PROP_POOL_PUT(p, v)	free((v))
    232 
    233 #define	_PROP_POOL_INIT(p, s, d)	static const size_t p = s;
    234 
    235 #define	_PROP_MALLOC_DEFINE(t, s, l)	/* nothing */
    236 
    237 #endif /* _KERNEL */
    238 
    239 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */
    240