xref: /dragonfly/sys/libprop/prop_data.c (revision f3f3eadb)
19f95f3e0SAlex Hornung /*	$NetBSD: prop_data.c,v 1.14 2009/01/25 06:59:35 cyber Exp $	*/
29f95f3e0SAlex Hornung 
39f95f3e0SAlex Hornung /*-
49f95f3e0SAlex Hornung  * Copyright (c) 2006 The NetBSD Foundation, Inc.
59f95f3e0SAlex Hornung  * All rights reserved.
69f95f3e0SAlex Hornung  *
79f95f3e0SAlex Hornung  * This code is derived from software contributed to The NetBSD Foundation
89f95f3e0SAlex Hornung  * by Jason R. Thorpe.
99f95f3e0SAlex Hornung  *
109f95f3e0SAlex Hornung  * Redistribution and use in source and binary forms, with or without
119f95f3e0SAlex Hornung  * modification, are permitted provided that the following conditions
129f95f3e0SAlex Hornung  * are met:
139f95f3e0SAlex Hornung  * 1. Redistributions of source code must retain the above copyright
149f95f3e0SAlex Hornung  *    notice, this list of conditions and the following disclaimer.
159f95f3e0SAlex Hornung  * 2. Redistributions in binary form must reproduce the above copyright
169f95f3e0SAlex Hornung  *    notice, this list of conditions and the following disclaimer in the
179f95f3e0SAlex Hornung  *    documentation and/or other materials provided with the distribution.
189f95f3e0SAlex Hornung  *
199f95f3e0SAlex Hornung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209f95f3e0SAlex Hornung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219f95f3e0SAlex Hornung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229f95f3e0SAlex Hornung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239f95f3e0SAlex Hornung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249f95f3e0SAlex Hornung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259f95f3e0SAlex Hornung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269f95f3e0SAlex Hornung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279f95f3e0SAlex Hornung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289f95f3e0SAlex Hornung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299f95f3e0SAlex Hornung  * POSSIBILITY OF SUCH DAMAGE.
309f95f3e0SAlex Hornung  */
319f95f3e0SAlex Hornung 
329f95f3e0SAlex Hornung #include <libprop/prop_data.h>
339f95f3e0SAlex Hornung #include "prop_object_impl.h"
349f95f3e0SAlex Hornung 
359f95f3e0SAlex Hornung #if defined(_KERNEL)
369f95f3e0SAlex Hornung #include <sys/systm.h>
379f95f3e0SAlex Hornung #include <sys/libkern.h>
389f95f3e0SAlex Hornung #elif defined(_STANDALONE)
399f95f3e0SAlex Hornung #include <sys/param.h>
409f95f3e0SAlex Hornung #include <lib/libkern/libkern.h>
419f95f3e0SAlex Hornung #else
429f95f3e0SAlex Hornung #include <errno.h>
439f95f3e0SAlex Hornung #include <limits.h>
449f95f3e0SAlex Hornung #include <stdlib.h>
459f95f3e0SAlex Hornung #endif
469f95f3e0SAlex Hornung 
479f95f3e0SAlex Hornung struct _prop_data {
489f95f3e0SAlex Hornung 	struct _prop_object	pd_obj;
499f95f3e0SAlex Hornung 	union {
509f95f3e0SAlex Hornung 		void *		pdu_mutable;
519f95f3e0SAlex Hornung 		const void *	pdu_immutable;
529f95f3e0SAlex Hornung 	} pd_un;
539f95f3e0SAlex Hornung #define	pd_mutable		pd_un.pdu_mutable
549f95f3e0SAlex Hornung #define	pd_immutable		pd_un.pdu_immutable
559f95f3e0SAlex Hornung 	size_t			pd_size;
569f95f3e0SAlex Hornung 	int			pd_flags;
579f95f3e0SAlex Hornung };
589f95f3e0SAlex Hornung 
599f95f3e0SAlex Hornung #define	PD_F_NOCOPY		0x01
609f95f3e0SAlex Hornung 
61*f3f3eadbSSascha Wildner _PROP_POOL_INIT(_prop_data_pool, sizeof(struct _prop_data), "propdata");
629f95f3e0SAlex Hornung 
639f95f3e0SAlex Hornung _PROP_MALLOC_DEFINE(M_PROP_DATA, "prop data",
649f95f3e0SAlex Hornung 		    "property data container object")
659f95f3e0SAlex Hornung 
669f95f3e0SAlex Hornung static _prop_object_free_rv_t
679f95f3e0SAlex Hornung 		_prop_data_free(prop_stack_t, prop_object_t *);
689f95f3e0SAlex Hornung static bool	_prop_data_externalize(
699f95f3e0SAlex Hornung 				struct _prop_object_externalize_context *,
709f95f3e0SAlex Hornung 				void *);
719f95f3e0SAlex Hornung static _prop_object_equals_rv_t
729f95f3e0SAlex Hornung 		_prop_data_equals(prop_object_t, prop_object_t,
739f95f3e0SAlex Hornung 				  void **, void **,
749f95f3e0SAlex Hornung 				  prop_object_t *, prop_object_t *);
759f95f3e0SAlex Hornung 
769f95f3e0SAlex Hornung static const struct _prop_object_type _prop_object_type_data = {
779f95f3e0SAlex Hornung 	.pot_type	=	PROP_TYPE_DATA,
789f95f3e0SAlex Hornung 	.pot_free	=	_prop_data_free,
799f95f3e0SAlex Hornung 	.pot_extern	=	_prop_data_externalize,
809f95f3e0SAlex Hornung 	.pot_equals	=	_prop_data_equals,
819f95f3e0SAlex Hornung };
829f95f3e0SAlex Hornung 
839f95f3e0SAlex Hornung #define	prop_object_is_data(x)		\
849f95f3e0SAlex Hornung 	((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_data)
859f95f3e0SAlex Hornung 
869f95f3e0SAlex Hornung /* ARGSUSED */
879f95f3e0SAlex Hornung static _prop_object_free_rv_t
_prop_data_free(prop_stack_t stack,prop_object_t * obj)889f95f3e0SAlex Hornung _prop_data_free(prop_stack_t stack, prop_object_t *obj)
899f95f3e0SAlex Hornung {
909f95f3e0SAlex Hornung 	prop_data_t pd = *obj;
919f95f3e0SAlex Hornung 
929f95f3e0SAlex Hornung 	if ((pd->pd_flags & PD_F_NOCOPY) == 0 && pd->pd_mutable != NULL)
939f95f3e0SAlex Hornung 		_PROP_FREE(pd->pd_mutable, M_PROP_DATA);
949f95f3e0SAlex Hornung 	_PROP_POOL_PUT(_prop_data_pool, pd);
959f95f3e0SAlex Hornung 
969f95f3e0SAlex Hornung 	return (_PROP_OBJECT_FREE_DONE);
979f95f3e0SAlex Hornung }
989f95f3e0SAlex Hornung 
999f95f3e0SAlex Hornung static const char _prop_data_base64[] =
1009f95f3e0SAlex Hornung     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1019f95f3e0SAlex Hornung static const char _prop_data_pad64 = '=';
1029f95f3e0SAlex Hornung 
1039f95f3e0SAlex Hornung static bool
_prop_data_externalize(struct _prop_object_externalize_context * ctx,void * v)1049f95f3e0SAlex Hornung _prop_data_externalize(struct _prop_object_externalize_context *ctx, void *v)
1059f95f3e0SAlex Hornung {
1069f95f3e0SAlex Hornung 	prop_data_t pd = v;
1079f95f3e0SAlex Hornung 	size_t i, srclen;
1089f95f3e0SAlex Hornung 	const uint8_t *src;
1099f95f3e0SAlex Hornung 	uint8_t output[4];
1109f95f3e0SAlex Hornung 	uint8_t input[3];
1119f95f3e0SAlex Hornung 
1129f95f3e0SAlex Hornung 	if (pd->pd_size == 0)
1139f95f3e0SAlex Hornung 		return (_prop_object_externalize_empty_tag(ctx, "data"));
1149f95f3e0SAlex Hornung 
1159f95f3e0SAlex Hornung 	if (_prop_object_externalize_start_tag(ctx, "data") == false)
1169f95f3e0SAlex Hornung 		return (false);
1179f95f3e0SAlex Hornung 
1189f95f3e0SAlex Hornung 	for (src = pd->pd_immutable, srclen = pd->pd_size;
1199f95f3e0SAlex Hornung 	     srclen > 2; srclen -= 3) {
1209f95f3e0SAlex Hornung 		input[0] = *src++;
1219f95f3e0SAlex Hornung 		input[1] = *src++;
1229f95f3e0SAlex Hornung 		input[2] = *src++;
1239f95f3e0SAlex Hornung 
1249f95f3e0SAlex Hornung 		output[0] = (uint32_t)input[0] >> 2;
1259f95f3e0SAlex Hornung 		output[1] = ((uint32_t)(input[0] & 0x03) << 4) +
1269f95f3e0SAlex Hornung 		    ((uint32_t)input[1] >> 4);
1279f95f3e0SAlex Hornung 		output[2] = ((uint32_t)(input[1] & 0x0f) << 2) +
1289f95f3e0SAlex Hornung 		    ((uint32_t)input[2] >> 6);
1299f95f3e0SAlex Hornung 		output[3] = input[2] & 0x3f;
1309f95f3e0SAlex Hornung 		_PROP_ASSERT(output[0] < 64);
1319f95f3e0SAlex Hornung 		_PROP_ASSERT(output[1] < 64);
1329f95f3e0SAlex Hornung 		_PROP_ASSERT(output[2] < 64);
1339f95f3e0SAlex Hornung 		_PROP_ASSERT(output[3] < 64);
1349f95f3e0SAlex Hornung 
1359f95f3e0SAlex Hornung 		if (_prop_object_externalize_append_char(ctx,
1369f95f3e0SAlex Hornung 				_prop_data_base64[output[0]]) == false ||
1379f95f3e0SAlex Hornung 		    _prop_object_externalize_append_char(ctx,
1389f95f3e0SAlex Hornung 				_prop_data_base64[output[1]]) == false ||
1399f95f3e0SAlex Hornung 		    _prop_object_externalize_append_char(ctx,
1409f95f3e0SAlex Hornung 				_prop_data_base64[output[2]]) == false ||
1419f95f3e0SAlex Hornung 		    _prop_object_externalize_append_char(ctx,
1429f95f3e0SAlex Hornung 				_prop_data_base64[output[3]]) == false)
1439f95f3e0SAlex Hornung 			return (false);
1449f95f3e0SAlex Hornung 	}
1459f95f3e0SAlex Hornung 
1469f95f3e0SAlex Hornung 	if (srclen != 0) {
1479f95f3e0SAlex Hornung 		input[0] = input[1] = input[2] = '\0';
1489f95f3e0SAlex Hornung 		for (i = 0; i < srclen; i++)
1499f95f3e0SAlex Hornung 			input[i] = *src++;
1509f95f3e0SAlex Hornung 
1519f95f3e0SAlex Hornung 		output[0] = (uint32_t)input[0] >> 2;
1529f95f3e0SAlex Hornung 		output[1] = ((uint32_t)(input[0] & 0x03) << 4) +
1539f95f3e0SAlex Hornung 		    ((uint32_t)input[1] >> 4);
1549f95f3e0SAlex Hornung 		output[2] = ((uint32_t)(input[1] & 0x0f) << 2) +
1559f95f3e0SAlex Hornung 		    ((uint32_t)input[2] >> 6);
1569f95f3e0SAlex Hornung 		_PROP_ASSERT(output[0] < 64);
1579f95f3e0SAlex Hornung 		_PROP_ASSERT(output[1] < 64);
1589f95f3e0SAlex Hornung 		_PROP_ASSERT(output[2] < 64);
1599f95f3e0SAlex Hornung 
1609f95f3e0SAlex Hornung 		if (_prop_object_externalize_append_char(ctx,
1619f95f3e0SAlex Hornung 				_prop_data_base64[output[0]]) == false ||
1629f95f3e0SAlex Hornung 		    _prop_object_externalize_append_char(ctx,
1639f95f3e0SAlex Hornung 				_prop_data_base64[output[1]]) == false ||
1649f95f3e0SAlex Hornung 		    _prop_object_externalize_append_char(ctx,
1659f95f3e0SAlex Hornung 				srclen == 1 ? _prop_data_pad64
1669f95f3e0SAlex Hornung 				: _prop_data_base64[output[2]]) == false ||
1679f95f3e0SAlex Hornung 		    _prop_object_externalize_append_char(ctx,
1689f95f3e0SAlex Hornung 				_prop_data_pad64) == false)
1699f95f3e0SAlex Hornung 			return (false);
1709f95f3e0SAlex Hornung 	}
1719f95f3e0SAlex Hornung 
1729f95f3e0SAlex Hornung 	if (_prop_object_externalize_end_tag(ctx, "data") == false)
1739f95f3e0SAlex Hornung 		return (false);
1749f95f3e0SAlex Hornung 
1759f95f3e0SAlex Hornung 	return (true);
1769f95f3e0SAlex Hornung }
1779f95f3e0SAlex Hornung 
1789f95f3e0SAlex Hornung /* ARGSUSED */
1799f95f3e0SAlex Hornung static _prop_object_equals_rv_t
_prop_data_equals(prop_object_t v1,prop_object_t v2,void ** stored_pointer1,void ** stored_pointer2,prop_object_t * next_obj1,prop_object_t * next_obj2)1809f95f3e0SAlex Hornung _prop_data_equals(prop_object_t v1, prop_object_t v2,
1819f95f3e0SAlex Hornung     void **stored_pointer1, void **stored_pointer2,
1829f95f3e0SAlex Hornung     prop_object_t *next_obj1, prop_object_t *next_obj2)
1839f95f3e0SAlex Hornung {
1849f95f3e0SAlex Hornung 	prop_data_t pd1 = v1;
1859f95f3e0SAlex Hornung 	prop_data_t pd2 = v2;
1869f95f3e0SAlex Hornung 
1879f95f3e0SAlex Hornung 	if (pd1 == pd2)
1889f95f3e0SAlex Hornung 		return (_PROP_OBJECT_EQUALS_TRUE);
1899f95f3e0SAlex Hornung 	if (pd1->pd_size != pd2->pd_size)
1909f95f3e0SAlex Hornung 		return (_PROP_OBJECT_EQUALS_FALSE);
1919f95f3e0SAlex Hornung 	if (pd1->pd_size == 0) {
1929f95f3e0SAlex Hornung 		_PROP_ASSERT(pd1->pd_immutable == NULL);
1939f95f3e0SAlex Hornung 		_PROP_ASSERT(pd2->pd_immutable == NULL);
1949f95f3e0SAlex Hornung 		return (_PROP_OBJECT_EQUALS_TRUE);
1959f95f3e0SAlex Hornung 	}
1969f95f3e0SAlex Hornung 	if (memcmp(pd1->pd_immutable, pd2->pd_immutable, pd1->pd_size) == 0)
1979f95f3e0SAlex Hornung 		return _PROP_OBJECT_EQUALS_TRUE;
1989f95f3e0SAlex Hornung 	else
1999f95f3e0SAlex Hornung 		return _PROP_OBJECT_EQUALS_FALSE;
2009f95f3e0SAlex Hornung }
2019f95f3e0SAlex Hornung 
2029f95f3e0SAlex Hornung static prop_data_t
_prop_data_alloc(void)2039f95f3e0SAlex Hornung _prop_data_alloc(void)
2049f95f3e0SAlex Hornung {
2059f95f3e0SAlex Hornung 	prop_data_t pd;
2069f95f3e0SAlex Hornung 
2079f95f3e0SAlex Hornung 	pd = _PROP_POOL_GET(_prop_data_pool);
2089f95f3e0SAlex Hornung 	if (pd != NULL) {
2099f95f3e0SAlex Hornung 		_prop_object_init(&pd->pd_obj, &_prop_object_type_data);
2109f95f3e0SAlex Hornung 
2119f95f3e0SAlex Hornung 		pd->pd_mutable = NULL;
2129f95f3e0SAlex Hornung 		pd->pd_size = 0;
2139f95f3e0SAlex Hornung 		pd->pd_flags = 0;
2149f95f3e0SAlex Hornung 	}
2159f95f3e0SAlex Hornung 
2169f95f3e0SAlex Hornung 	return (pd);
2179f95f3e0SAlex Hornung }
2189f95f3e0SAlex Hornung 
2199f95f3e0SAlex Hornung /*
2209f95f3e0SAlex Hornung  * prop_data_create_data --
2219f95f3e0SAlex Hornung  *	Create a data container that contains a copy of the data.
2229f95f3e0SAlex Hornung  */
2239f95f3e0SAlex Hornung prop_data_t
prop_data_create_data(const void * v,size_t size)2249f95f3e0SAlex Hornung prop_data_create_data(const void *v, size_t size)
2259f95f3e0SAlex Hornung {
2269f95f3e0SAlex Hornung 	prop_data_t pd;
2279f95f3e0SAlex Hornung 	void *nv;
2289f95f3e0SAlex Hornung 
2299f95f3e0SAlex Hornung 	pd = _prop_data_alloc();
2309f95f3e0SAlex Hornung 	if (pd != NULL && size != 0) {
2319f95f3e0SAlex Hornung 		nv = _PROP_MALLOC(size, M_PROP_DATA);
2329f95f3e0SAlex Hornung 		if (nv == NULL) {
2339f95f3e0SAlex Hornung 			prop_object_release(pd);
2349f95f3e0SAlex Hornung 			return (NULL);
2359f95f3e0SAlex Hornung 		}
2369f95f3e0SAlex Hornung 		memcpy(nv, v, size);
2379f95f3e0SAlex Hornung 		pd->pd_mutable = nv;
2389f95f3e0SAlex Hornung 		pd->pd_size = size;
2399f95f3e0SAlex Hornung 	}
2409f95f3e0SAlex Hornung 	return (pd);
2419f95f3e0SAlex Hornung }
2429f95f3e0SAlex Hornung 
2439f95f3e0SAlex Hornung /*
2449f95f3e0SAlex Hornung  * prop_data_create_data_nocopy --
2459f95f3e0SAlex Hornung  *	Create an immutable data container that contains a refrence to the
2469f95f3e0SAlex Hornung  *	provided external data.
2479f95f3e0SAlex Hornung  */
2489f95f3e0SAlex Hornung prop_data_t
prop_data_create_data_nocopy(const void * v,size_t size)2499f95f3e0SAlex Hornung prop_data_create_data_nocopy(const void *v, size_t size)
2509f95f3e0SAlex Hornung {
2519f95f3e0SAlex Hornung 	prop_data_t pd;
2529f95f3e0SAlex Hornung 
2539f95f3e0SAlex Hornung 	pd = _prop_data_alloc();
2549f95f3e0SAlex Hornung 	if (pd != NULL) {
2559f95f3e0SAlex Hornung 		pd->pd_immutable = v;
2569f95f3e0SAlex Hornung 		pd->pd_size = size;
2579f95f3e0SAlex Hornung 		pd->pd_flags |= PD_F_NOCOPY;
2589f95f3e0SAlex Hornung 	}
2599f95f3e0SAlex Hornung 	return (pd);
2609f95f3e0SAlex Hornung }
2619f95f3e0SAlex Hornung 
2629f95f3e0SAlex Hornung /*
2639f95f3e0SAlex Hornung  * prop_data_copy --
2649f95f3e0SAlex Hornung  *	Copy a data container.  If the original data is external, then
2659f95f3e0SAlex Hornung  *	the copy is also references the same external data.
2669f95f3e0SAlex Hornung  */
2679f95f3e0SAlex Hornung prop_data_t
prop_data_copy(prop_data_t opd)2689f95f3e0SAlex Hornung prop_data_copy(prop_data_t opd)
2699f95f3e0SAlex Hornung {
2709f95f3e0SAlex Hornung 	prop_data_t pd;
2719f95f3e0SAlex Hornung 
2729f95f3e0SAlex Hornung 	if (! prop_object_is_data(opd))
2739f95f3e0SAlex Hornung 		return (NULL);
2749f95f3e0SAlex Hornung 
2759f95f3e0SAlex Hornung 	pd = _prop_data_alloc();
2769f95f3e0SAlex Hornung 	if (pd != NULL) {
2779f95f3e0SAlex Hornung 		pd->pd_size = opd->pd_size;
2789f95f3e0SAlex Hornung 		pd->pd_flags = opd->pd_flags;
2799f95f3e0SAlex Hornung 		if (opd->pd_flags & PD_F_NOCOPY)
2809f95f3e0SAlex Hornung 			pd->pd_immutable = opd->pd_immutable;
2819f95f3e0SAlex Hornung 		else if (opd->pd_size != 0) {
2829f95f3e0SAlex Hornung 			void *nv = _PROP_MALLOC(pd->pd_size, M_PROP_DATA);
2839f95f3e0SAlex Hornung 			if (nv == NULL) {
2849f95f3e0SAlex Hornung 				prop_object_release(pd);
2859f95f3e0SAlex Hornung 				return (NULL);
2869f95f3e0SAlex Hornung 			}
2879f95f3e0SAlex Hornung 			memcpy(nv, opd->pd_immutable, opd->pd_size);
2889f95f3e0SAlex Hornung 			pd->pd_mutable = nv;
2899f95f3e0SAlex Hornung 		}
2909f95f3e0SAlex Hornung 	}
2919f95f3e0SAlex Hornung 	return (pd);
2929f95f3e0SAlex Hornung }
2939f95f3e0SAlex Hornung 
2949f95f3e0SAlex Hornung /*
2959f95f3e0SAlex Hornung  * prop_data_size --
2969f95f3e0SAlex Hornung  *	Return the size of the data.
2979f95f3e0SAlex Hornung  */
2989f95f3e0SAlex Hornung size_t
prop_data_size(prop_data_t pd)2999f95f3e0SAlex Hornung prop_data_size(prop_data_t pd)
3009f95f3e0SAlex Hornung {
3019f95f3e0SAlex Hornung 
3029f95f3e0SAlex Hornung 	if (! prop_object_is_data(pd))
3039f95f3e0SAlex Hornung 		return (0);
3049f95f3e0SAlex Hornung 
3059f95f3e0SAlex Hornung 	return (pd->pd_size);
3069f95f3e0SAlex Hornung }
3079f95f3e0SAlex Hornung 
3089f95f3e0SAlex Hornung /*
3099f95f3e0SAlex Hornung  * prop_data_data --
3109f95f3e0SAlex Hornung  *	Return a copy of the contents of the data container.
3119f95f3e0SAlex Hornung  *	The data is allocated with the M_TEMP malloc type.
3129f95f3e0SAlex Hornung  *	If the data container is empty, NULL is returned.
3139f95f3e0SAlex Hornung  */
3149f95f3e0SAlex Hornung void *
prop_data_data(prop_data_t pd)3159f95f3e0SAlex Hornung prop_data_data(prop_data_t pd)
3169f95f3e0SAlex Hornung {
3179f95f3e0SAlex Hornung 	void *v;
3189f95f3e0SAlex Hornung 
3199f95f3e0SAlex Hornung 	if (! prop_object_is_data(pd))
3209f95f3e0SAlex Hornung 		return (NULL);
3219f95f3e0SAlex Hornung 
3229f95f3e0SAlex Hornung 	if (pd->pd_size == 0) {
3239f95f3e0SAlex Hornung 		_PROP_ASSERT(pd->pd_immutable == NULL);
3249f95f3e0SAlex Hornung 		return (NULL);
3259f95f3e0SAlex Hornung 	}
3269f95f3e0SAlex Hornung 
3279f95f3e0SAlex Hornung 	_PROP_ASSERT(pd->pd_immutable != NULL);
3289f95f3e0SAlex Hornung 
3299f95f3e0SAlex Hornung 	v = _PROP_MALLOC(pd->pd_size, M_TEMP);
3309f95f3e0SAlex Hornung 	if (v != NULL)
3319f95f3e0SAlex Hornung 		memcpy(v, pd->pd_immutable, pd->pd_size);
3329f95f3e0SAlex Hornung 
3339f95f3e0SAlex Hornung 	return (v);
3349f95f3e0SAlex Hornung }
3359f95f3e0SAlex Hornung 
3369f95f3e0SAlex Hornung /*
3379f95f3e0SAlex Hornung  * prop_data_data_nocopy --
3389f95f3e0SAlex Hornung  *	Return an immutable reference to the contents of the data
3399f95f3e0SAlex Hornung  *	container.
3409f95f3e0SAlex Hornung  */
3419f95f3e0SAlex Hornung const void *
prop_data_data_nocopy(prop_data_t pd)3429f95f3e0SAlex Hornung prop_data_data_nocopy(prop_data_t pd)
3439f95f3e0SAlex Hornung {
3449f95f3e0SAlex Hornung 
3459f95f3e0SAlex Hornung 	if (! prop_object_is_data(pd))
3469f95f3e0SAlex Hornung 		return (NULL);
3479f95f3e0SAlex Hornung 
3489f95f3e0SAlex Hornung 	_PROP_ASSERT((pd->pd_size == 0 && pd->pd_immutable == NULL) ||
3499f95f3e0SAlex Hornung 		     (pd->pd_size != 0 && pd->pd_immutable != NULL));
3509f95f3e0SAlex Hornung 
3519f95f3e0SAlex Hornung 	return (pd->pd_immutable);
3529f95f3e0SAlex Hornung }
3539f95f3e0SAlex Hornung 
3549f95f3e0SAlex Hornung /*
3559f95f3e0SAlex Hornung  * prop_data_equals --
3569f95f3e0SAlex Hornung  *	Return true if two strings are equivalent.
3579f95f3e0SAlex Hornung  */
3589f95f3e0SAlex Hornung bool
prop_data_equals(prop_data_t pd1,prop_data_t pd2)3599f95f3e0SAlex Hornung prop_data_equals(prop_data_t pd1, prop_data_t pd2)
3609f95f3e0SAlex Hornung {
3619f95f3e0SAlex Hornung 	if (!prop_object_is_data(pd1) || !prop_object_is_data(pd2))
3629f95f3e0SAlex Hornung 		return (false);
3639f95f3e0SAlex Hornung 
3649f95f3e0SAlex Hornung 	return (prop_object_equals(pd1, pd2));
3659f95f3e0SAlex Hornung }
3669f95f3e0SAlex Hornung 
3679f95f3e0SAlex Hornung /*
3689f95f3e0SAlex Hornung  * prop_data_equals_data --
3699f95f3e0SAlex Hornung  *	Return true if the contained data is equivalent to the specified
3709f95f3e0SAlex Hornung  *	external data.
3719f95f3e0SAlex Hornung  */
3729f95f3e0SAlex Hornung bool
prop_data_equals_data(prop_data_t pd,const void * v,size_t size)3739f95f3e0SAlex Hornung prop_data_equals_data(prop_data_t pd, const void *v, size_t size)
3749f95f3e0SAlex Hornung {
3759f95f3e0SAlex Hornung 
3769f95f3e0SAlex Hornung 	if (! prop_object_is_data(pd))
3779f95f3e0SAlex Hornung 		return (false);
3789f95f3e0SAlex Hornung 
3799f95f3e0SAlex Hornung 	if (pd->pd_size != size)
3809f95f3e0SAlex Hornung 		return (false);
3819f95f3e0SAlex Hornung 	return (memcmp(pd->pd_immutable, v, size) == 0);
3829f95f3e0SAlex Hornung }
3839f95f3e0SAlex Hornung 
3849f95f3e0SAlex Hornung static bool
_prop_data_internalize_decode(struct _prop_object_internalize_context * ctx,uint8_t * target,size_t targsize,size_t * sizep,const char ** cpp)3859f95f3e0SAlex Hornung _prop_data_internalize_decode(struct _prop_object_internalize_context *ctx,
3869f95f3e0SAlex Hornung 			     uint8_t *target, size_t targsize, size_t *sizep,
3879f95f3e0SAlex Hornung 			     const char **cpp)
3889f95f3e0SAlex Hornung {
3899f95f3e0SAlex Hornung 	const char *src;
3909f95f3e0SAlex Hornung 	size_t tarindex;
3919f95f3e0SAlex Hornung 	int state, ch;
3929f95f3e0SAlex Hornung 	const char *pos;
3939f95f3e0SAlex Hornung 
3949f95f3e0SAlex Hornung 	state = 0;
3959f95f3e0SAlex Hornung 	tarindex = 0;
3969f95f3e0SAlex Hornung 	src = ctx->poic_cp;
3979f95f3e0SAlex Hornung 
3989f95f3e0SAlex Hornung 	for (;;) {
3999f95f3e0SAlex Hornung 		ch = (unsigned char) *src++;
4009f95f3e0SAlex Hornung 		if (_PROP_EOF(ch))
4019f95f3e0SAlex Hornung 			return (false);
4029f95f3e0SAlex Hornung 		if (_PROP_ISSPACE(ch))
4039f95f3e0SAlex Hornung 			continue;
4049f95f3e0SAlex Hornung 		if (ch == '<') {
4059f95f3e0SAlex Hornung 			src--;
4069f95f3e0SAlex Hornung 			break;
4079f95f3e0SAlex Hornung 		}
4089f95f3e0SAlex Hornung 		if (ch == _prop_data_pad64)
4099f95f3e0SAlex Hornung 			break;
4109f95f3e0SAlex Hornung 
4119f95f3e0SAlex Hornung 		pos = strchr(_prop_data_base64, ch);
4129f95f3e0SAlex Hornung 		if (pos == NULL)
4139f95f3e0SAlex Hornung 			return (false);
4149f95f3e0SAlex Hornung 
4159f95f3e0SAlex Hornung 		switch (state) {
4169f95f3e0SAlex Hornung 		case 0:
4179f95f3e0SAlex Hornung 			if (target) {
4189f95f3e0SAlex Hornung 				if (tarindex >= targsize)
4199f95f3e0SAlex Hornung 					return (false);
4209f95f3e0SAlex Hornung 				target[tarindex] =
4219f95f3e0SAlex Hornung 				    (uint8_t)((pos - _prop_data_base64) << 2);
4229f95f3e0SAlex Hornung 			}
4239f95f3e0SAlex Hornung 			state = 1;
4249f95f3e0SAlex Hornung 			break;
4259f95f3e0SAlex Hornung 
4269f95f3e0SAlex Hornung 		case 1:
4279f95f3e0SAlex Hornung 			if (target) {
4289f95f3e0SAlex Hornung 				if (tarindex + 1 >= targsize)
4299f95f3e0SAlex Hornung 					return (false);
4309f95f3e0SAlex Hornung 				target[tarindex] |=
4319f95f3e0SAlex Hornung 				    (uint32_t)(pos - _prop_data_base64) >> 4;
4329f95f3e0SAlex Hornung 				target[tarindex + 1] =
4339f95f3e0SAlex Hornung 				    (uint8_t)(((pos - _prop_data_base64) & 0xf)
4349f95f3e0SAlex Hornung 				        << 4);
4359f95f3e0SAlex Hornung 			}
4369f95f3e0SAlex Hornung 			tarindex++;
4379f95f3e0SAlex Hornung 			state = 2;
4389f95f3e0SAlex Hornung 			break;
4399f95f3e0SAlex Hornung 
4409f95f3e0SAlex Hornung 		case 2:
4419f95f3e0SAlex Hornung 			if (target) {
4429f95f3e0SAlex Hornung 				if (tarindex + 1 >= targsize)
4439f95f3e0SAlex Hornung 					return (false);
4449f95f3e0SAlex Hornung 				target[tarindex] |=
4459f95f3e0SAlex Hornung 				    (uint32_t)(pos - _prop_data_base64) >> 2;
4469f95f3e0SAlex Hornung 				target[tarindex + 1] =
4479f95f3e0SAlex Hornung 				    (uint8_t)(((pos - _prop_data_base64)
4489f95f3e0SAlex Hornung 				        & 0x3) << 6);
4499f95f3e0SAlex Hornung 			}
4509f95f3e0SAlex Hornung 			tarindex++;
4519f95f3e0SAlex Hornung 			state = 3;
4529f95f3e0SAlex Hornung 			break;
4539f95f3e0SAlex Hornung 
4549f95f3e0SAlex Hornung 		case 3:
4559f95f3e0SAlex Hornung 			if (target) {
4569f95f3e0SAlex Hornung 				if (tarindex >= targsize)
4579f95f3e0SAlex Hornung 					return (false);
4589f95f3e0SAlex Hornung 				target[tarindex] |= (uint8_t)
4599f95f3e0SAlex Hornung 				    (pos - _prop_data_base64);
4609f95f3e0SAlex Hornung 			}
4619f95f3e0SAlex Hornung 			tarindex++;
4629f95f3e0SAlex Hornung 			state = 0;
4639f95f3e0SAlex Hornung 			break;
4649f95f3e0SAlex Hornung 
4659f95f3e0SAlex Hornung 		default:
4669f95f3e0SAlex Hornung 			_PROP_ASSERT(/*CONSTCOND*/0);
4679f95f3e0SAlex Hornung 		}
4689f95f3e0SAlex Hornung 	}
4699f95f3e0SAlex Hornung 
4709f95f3e0SAlex Hornung 	/*
4719f95f3e0SAlex Hornung 	 * We are done decoding the Base64 characters.  Let's see if we
4729f95f3e0SAlex Hornung 	 * ended up on a byte boundary and/or with unrecognized trailing
4739f95f3e0SAlex Hornung 	 * characters.
4749f95f3e0SAlex Hornung 	 */
4759f95f3e0SAlex Hornung 	if (ch == _prop_data_pad64) {
4769f95f3e0SAlex Hornung 		ch = (unsigned char) *src;	/* src already advanced */
4779f95f3e0SAlex Hornung 		if (_PROP_EOF(ch))
4789f95f3e0SAlex Hornung 			return (false);
4799f95f3e0SAlex Hornung 		switch (state) {
4809f95f3e0SAlex Hornung 		case 0:		/* Invalid = in first position */
4819f95f3e0SAlex Hornung 		case 1:		/* Invalid = in second position */
4829f95f3e0SAlex Hornung 			return (false);
4839f95f3e0SAlex Hornung 
4849f95f3e0SAlex Hornung 		case 2:		/* Valid, one byte of info */
4859f95f3e0SAlex Hornung 			/* Skip whitespace */
4869f95f3e0SAlex Hornung 			for (ch = (unsigned char) *src++;
4879f95f3e0SAlex Hornung 			     ch != '<'; ch = (unsigned char) *src++) {
4889f95f3e0SAlex Hornung 				if (_PROP_EOF(ch))
4899f95f3e0SAlex Hornung 					return (false);
4909f95f3e0SAlex Hornung 				if (!_PROP_ISSPACE(ch))
4919f95f3e0SAlex Hornung 					break;
4929f95f3e0SAlex Hornung 			}
4939f95f3e0SAlex Hornung 			/* Make sure there is another trailing = */
4949f95f3e0SAlex Hornung 			if (ch != _prop_data_pad64)
4959f95f3e0SAlex Hornung 				return (false);
4969f95f3e0SAlex Hornung 			ch = (unsigned char) *src;
4979f95f3e0SAlex Hornung 			/* FALLTHROUGH */
4989f95f3e0SAlex Hornung 
4999f95f3e0SAlex Hornung 		case 3:		/* Valid, two bytes of info */
5009f95f3e0SAlex Hornung 			/*
5019f95f3e0SAlex Hornung 			 * We know this char is a =.  Is there anything but
5029f95f3e0SAlex Hornung 			 * whitespace after it?
5039f95f3e0SAlex Hornung 			 */
5049f95f3e0SAlex Hornung 			for (ch = (unsigned char) *src++;
5059f95f3e0SAlex Hornung 			     ch != '<'; ch = (unsigned char) *src++) {
5069f95f3e0SAlex Hornung 				if (_PROP_EOF(ch))
5079f95f3e0SAlex Hornung 					return (false);
5089f95f3e0SAlex Hornung 				if (!_PROP_ISSPACE(ch))
5099f95f3e0SAlex Hornung 					return (false);
5109f95f3e0SAlex Hornung 			}
5119f95f3e0SAlex Hornung 			/* back up to '<' */
5129f95f3e0SAlex Hornung 			src--;
5139f95f3e0SAlex Hornung 		}
5149f95f3e0SAlex Hornung 	} else {
5159f95f3e0SAlex Hornung 		/*
5169f95f3e0SAlex Hornung 		 * We ended by seeing the end of the Base64 string.  Make
5179f95f3e0SAlex Hornung 		 * sure there are no partial bytes lying around.
5189f95f3e0SAlex Hornung 		 */
5199f95f3e0SAlex Hornung 		if (state != 0)
5209f95f3e0SAlex Hornung 			return (false);
5219f95f3e0SAlex Hornung 	}
5229f95f3e0SAlex Hornung 
5239f95f3e0SAlex Hornung 	_PROP_ASSERT(*src == '<');
5249f95f3e0SAlex Hornung 	if (sizep != NULL)
5259f95f3e0SAlex Hornung 		*sizep = tarindex;
5269f95f3e0SAlex Hornung 	if (cpp != NULL)
5279f95f3e0SAlex Hornung 		*cpp = src;
5289f95f3e0SAlex Hornung 
5299f95f3e0SAlex Hornung 	return (true);
5309f95f3e0SAlex Hornung }
5319f95f3e0SAlex Hornung 
5329f95f3e0SAlex Hornung /*
5339f95f3e0SAlex Hornung  * _prop_data_internalize --
5349f95f3e0SAlex Hornung  *	Parse a <data>...</data> and return the object created from the
5359f95f3e0SAlex Hornung  *	external representation.
5369f95f3e0SAlex Hornung  */
5379f95f3e0SAlex Hornung 
5389f95f3e0SAlex Hornung /* strtoul is used for parsing, enforce. */
5399f95f3e0SAlex Hornung typedef int PROP_DATA_ASSERT[/* CONSTCOND */sizeof(size_t) == sizeof(unsigned long) ? 1 : -1];
5409f95f3e0SAlex Hornung 
5419f95f3e0SAlex Hornung /* ARGSUSED */
5429f95f3e0SAlex Hornung bool
_prop_data_internalize(prop_stack_t stack,prop_object_t * obj,struct _prop_object_internalize_context * ctx)5439f95f3e0SAlex Hornung _prop_data_internalize(prop_stack_t stack, prop_object_t *obj,
5449f95f3e0SAlex Hornung     struct _prop_object_internalize_context *ctx)
5459f95f3e0SAlex Hornung {
5469f95f3e0SAlex Hornung 	prop_data_t data;
5479f95f3e0SAlex Hornung 	uint8_t *buf;
5489f95f3e0SAlex Hornung 	size_t len, alen;
5499f95f3e0SAlex Hornung 
5509f95f3e0SAlex Hornung 	/*
5519f95f3e0SAlex Hornung 	 * We don't accept empty elements.
5529f95f3e0SAlex Hornung 	 * This actually only checks for the node to be <data/>
5539f95f3e0SAlex Hornung 	 * (Which actually causes another error if found.)
5549f95f3e0SAlex Hornung 	 */
5559f95f3e0SAlex Hornung 	if (ctx->poic_is_empty_element)
5569f95f3e0SAlex Hornung 		return (true);
5579f95f3e0SAlex Hornung 
5589f95f3e0SAlex Hornung 	/*
5599f95f3e0SAlex Hornung 	 * If we got a "size" attribute, get the size of the data blob
5609f95f3e0SAlex Hornung 	 * from that.  Otherwise, we have to figure it out from the base64.
5619f95f3e0SAlex Hornung 	 */
5629f95f3e0SAlex Hornung 	if (ctx->poic_tagattr != NULL) {
5639f95f3e0SAlex Hornung 		char *cp;
5649f95f3e0SAlex Hornung 
5659f95f3e0SAlex Hornung 		if (!_PROP_TAGATTR_MATCH(ctx, "size") ||
5669f95f3e0SAlex Hornung 		    ctx->poic_tagattrval_len == 0)
5679f95f3e0SAlex Hornung 			return (true);
5689f95f3e0SAlex Hornung 
5699f95f3e0SAlex Hornung #ifndef _KERNEL
5709f95f3e0SAlex Hornung 		errno = 0;
5719f95f3e0SAlex Hornung #endif
5729f95f3e0SAlex Hornung 		len = strtoul(ctx->poic_tagattrval, &cp, 0);
5739f95f3e0SAlex Hornung #ifndef _KERNEL		/* XXX can't check for ERANGE in the kernel */
5749f95f3e0SAlex Hornung 		if (len == ULONG_MAX && errno == ERANGE)
5759f95f3e0SAlex Hornung 			return (true);
5769f95f3e0SAlex Hornung #endif
5779f95f3e0SAlex Hornung 		if (cp != ctx->poic_tagattrval + ctx->poic_tagattrval_len)
5789f95f3e0SAlex Hornung 			return (true);
5799f95f3e0SAlex Hornung 		_PROP_ASSERT(*cp == '\"');
5809f95f3e0SAlex Hornung 	} else if (_prop_data_internalize_decode(ctx, NULL, 0, &len,
5819f95f3e0SAlex Hornung 						NULL) == false)
5829f95f3e0SAlex Hornung 		return (true);
5839f95f3e0SAlex Hornung 
5849f95f3e0SAlex Hornung 	/*
5859f95f3e0SAlex Hornung 	 * Always allocate one extra in case we don't land on an even byte
5869f95f3e0SAlex Hornung 	 * boundary during the decode.
5879f95f3e0SAlex Hornung 	 */
5889f95f3e0SAlex Hornung 	buf = _PROP_MALLOC(len + 1, M_PROP_DATA);
5899f95f3e0SAlex Hornung 	if (buf == NULL)
5909f95f3e0SAlex Hornung 		return (true);
5919f95f3e0SAlex Hornung 
5929f95f3e0SAlex Hornung 	if (_prop_data_internalize_decode(ctx, buf, len + 1, &alen,
5939f95f3e0SAlex Hornung 					  &ctx->poic_cp) == false) {
5949f95f3e0SAlex Hornung 		_PROP_FREE(buf, M_PROP_DATA);
5959f95f3e0SAlex Hornung 		return (true);
5969f95f3e0SAlex Hornung 	}
5979f95f3e0SAlex Hornung 	if (alen != len) {
5989f95f3e0SAlex Hornung 		_PROP_FREE(buf, M_PROP_DATA);
5999f95f3e0SAlex Hornung 		return (true);
6009f95f3e0SAlex Hornung 	}
6019f95f3e0SAlex Hornung 
6029f95f3e0SAlex Hornung 	if (_prop_object_internalize_find_tag(ctx, "data",
6039f95f3e0SAlex Hornung 					      _PROP_TAG_TYPE_END) == false) {
6049f95f3e0SAlex Hornung 		_PROP_FREE(buf, M_PROP_DATA);
6059f95f3e0SAlex Hornung 		return (true);
6069f95f3e0SAlex Hornung 	}
6079f95f3e0SAlex Hornung 
6089f95f3e0SAlex Hornung 	data = _prop_data_alloc();
6099f95f3e0SAlex Hornung 	if (data == NULL) {
6109f95f3e0SAlex Hornung 		_PROP_FREE(buf, M_PROP_DATA);
6119f95f3e0SAlex Hornung 		return (true);
6129f95f3e0SAlex Hornung 	}
6139f95f3e0SAlex Hornung 
6149f95f3e0SAlex Hornung 	/*
6159f95f3e0SAlex Hornung 	 * Handle alternate type of empty node.
6169f95f3e0SAlex Hornung 	 * XML document could contain open/close tags, yet still be empty.
6179f95f3e0SAlex Hornung 	 */
6189f95f3e0SAlex Hornung 	if (alen == 0) {
6199f95f3e0SAlex Hornung 		_PROP_FREE(buf, M_PROP_DATA);
6209f95f3e0SAlex Hornung 		data->pd_mutable = NULL;
6219f95f3e0SAlex Hornung 	} else {
6229f95f3e0SAlex Hornung 		data->pd_mutable = buf;
6239f95f3e0SAlex Hornung 	}
6249f95f3e0SAlex Hornung 	data->pd_size = len;
6259f95f3e0SAlex Hornung 
6269f95f3e0SAlex Hornung 	*obj = data;
6279f95f3e0SAlex Hornung 	return (true);
6289f95f3e0SAlex Hornung }
629