Home | History | Annotate | Line # | Download | only in libprop
prop_data.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: prop_data.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_data.h>
     40  1.1  thorpej #include "prop_object_impl.h"
     41  1.1  thorpej 
     42  1.1  thorpej #if defined(_KERNEL)
     43  1.1  thorpej #include <sys/systm.h>
     44  1.1  thorpej #elif defined(_STANDALONE)
     45  1.1  thorpej #include <sys/param.h>
     46  1.1  thorpej #include <lib/libkern/libkern.h>
     47  1.1  thorpej #else
     48  1.1  thorpej #include <errno.h>
     49  1.1  thorpej #include <limits.h>
     50  1.1  thorpej #include <stdlib.h>
     51  1.1  thorpej #endif
     52  1.1  thorpej 
     53  1.1  thorpej struct _prop_data {
     54  1.1  thorpej 	struct _prop_object	pd_obj;
     55  1.1  thorpej 	union {
     56  1.1  thorpej 		void *		pdu_mutable;
     57  1.1  thorpej 		const void *	pdu_immutable;
     58  1.1  thorpej 	} pd_un;
     59  1.1  thorpej #define	pd_mutable		pd_un.pdu_mutable
     60  1.1  thorpej #define	pd_immutable		pd_un.pdu_immutable
     61  1.1  thorpej 	size_t			pd_size;
     62  1.1  thorpej 	int			pd_flags;
     63  1.1  thorpej };
     64  1.1  thorpej 
     65  1.1  thorpej #define	PD_F_NOCOPY		0x01
     66  1.1  thorpej 
     67  1.1  thorpej _PROP_POOL_INIT(_prop_data_pool, sizeof(struct _prop_data), "propdata")
     68  1.1  thorpej 
     69  1.1  thorpej _PROP_MALLOC_DEFINE(M_PROP_DATA, "prop data",
     70  1.1  thorpej 		    "property data container object")
     71  1.1  thorpej 
     72  1.1  thorpej #define	prop_object_is_data(x) ((x)->pd_obj.po_type == PROP_TYPE_DATA)
     73  1.1  thorpej 
     74  1.1  thorpej static void
     75  1.1  thorpej _prop_data_free(void *v)
     76  1.1  thorpej {
     77  1.1  thorpej 	prop_data_t pd = v;
     78  1.1  thorpej 
     79  1.1  thorpej 	if ((pd->pd_flags & PD_F_NOCOPY) == 0 && pd->pd_mutable != NULL)
     80  1.1  thorpej 	    	_PROP_FREE(pd->pd_mutable, M_PROP_DATA);
     81  1.1  thorpej 	_PROP_POOL_PUT(_prop_data_pool, v);
     82  1.1  thorpej }
     83  1.1  thorpej 
     84  1.1  thorpej static const char _prop_data_base64[] =
     85  1.1  thorpej     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     86  1.1  thorpej static const char _prop_data_pad64 = '=';
     87  1.1  thorpej 
     88  1.1  thorpej static boolean_t
     89  1.1  thorpej _prop_data_externalize(struct _prop_object_externalize_context *ctx, void *v)
     90  1.1  thorpej {
     91  1.1  thorpej 	prop_data_t pd = v;
     92  1.1  thorpej 	size_t i, srclen;
     93  1.1  thorpej 	const uint8_t *src;
     94  1.1  thorpej 	uint8_t output[4];
     95  1.1  thorpej 	uint8_t input[3];
     96  1.1  thorpej 
     97  1.1  thorpej 	if (pd->pd_size == 0)
     98  1.1  thorpej 		return (_prop_object_externalize_empty_tag(ctx, "data"));
     99  1.1  thorpej 
    100  1.1  thorpej 	if (_prop_object_externalize_start_tag(ctx, "data") == FALSE)
    101  1.1  thorpej 		return (FALSE);
    102  1.1  thorpej 
    103  1.1  thorpej 	for (src = pd->pd_immutable, srclen = pd->pd_size;
    104  1.1  thorpej 	     srclen > 2; srclen -= 3) {
    105  1.1  thorpej 		input[0] = *src++;
    106  1.1  thorpej 		input[1] = *src++;
    107  1.1  thorpej 		input[2] = *src++;
    108  1.1  thorpej 
    109  1.1  thorpej 		output[0] = (uint32_t)input[0] >> 2;
    110  1.1  thorpej 		output[1] = ((uint32_t)(input[0] & 0x03) << 4) +
    111  1.1  thorpej 		    ((uint32_t)input[1] >> 4);
    112  1.1  thorpej 		output[2] = ((u_int32_t)(input[1] & 0x0f) << 2) +
    113  1.1  thorpej 		    ((uint32_t)input[2] >> 6);
    114  1.1  thorpej 		output[3] = input[2] & 0x3f;
    115  1.1  thorpej 		_PROP_ASSERT(output[0] < 64);
    116  1.1  thorpej 		_PROP_ASSERT(output[1] < 64);
    117  1.1  thorpej 		_PROP_ASSERT(output[2] < 64);
    118  1.1  thorpej 		_PROP_ASSERT(output[3] < 64);
    119  1.1  thorpej 
    120  1.1  thorpej 		if (_prop_object_externalize_append_char(ctx,
    121  1.1  thorpej 				_prop_data_base64[output[0]]) == FALSE ||
    122  1.1  thorpej 		    _prop_object_externalize_append_char(ctx,
    123  1.1  thorpej 		    		_prop_data_base64[output[1]]) == FALSE ||
    124  1.1  thorpej 		    _prop_object_externalize_append_char(ctx,
    125  1.1  thorpej 		    		_prop_data_base64[output[2]]) == FALSE ||
    126  1.1  thorpej 		    _prop_object_externalize_append_char(ctx,
    127  1.1  thorpej 		    		_prop_data_base64[output[3]]) == FALSE)
    128  1.1  thorpej 			return (FALSE);
    129  1.1  thorpej 	}
    130  1.1  thorpej 
    131  1.1  thorpej 	if (srclen != 0) {
    132  1.1  thorpej 		input[0] = input[1] = input[2] = '\0';
    133  1.1  thorpej 		for (i = 0; i < srclen; i++)
    134  1.1  thorpej 			input[i] = *src++;
    135  1.1  thorpej 
    136  1.1  thorpej 		output[0] = (uint32_t)input[0] >> 2;
    137  1.1  thorpej 		output[1] = ((uint32_t)(input[0] & 0x03) << 4) +
    138  1.1  thorpej 		    ((uint32_t)input[1] >> 4);
    139  1.1  thorpej 		output[2] = ((u_int32_t)(input[1] & 0x0f) << 2) +
    140  1.1  thorpej 		    ((uint32_t)input[2] >> 6);
    141  1.1  thorpej 		_PROP_ASSERT(output[0] < 64);
    142  1.1  thorpej 		_PROP_ASSERT(output[1] < 64);
    143  1.1  thorpej 		_PROP_ASSERT(output[2] < 64);
    144  1.1  thorpej 
    145  1.1  thorpej 		if (_prop_object_externalize_append_char(ctx,
    146  1.1  thorpej 				_prop_data_base64[output[0]]) == FALSE ||
    147  1.1  thorpej 		    _prop_object_externalize_append_char(ctx,
    148  1.1  thorpej 		    		_prop_data_base64[output[1]]) == FALSE ||
    149  1.1  thorpej 		    _prop_object_externalize_append_char(ctx,
    150  1.1  thorpej 		    		srclen == 1 ? _prop_data_pad64
    151  1.1  thorpej 				: _prop_data_base64[output[2]]) == FALSE ||
    152  1.1  thorpej 		    _prop_object_externalize_append_char(ctx,
    153  1.1  thorpej 		    		_prop_data_pad64) == FALSE)
    154  1.1  thorpej 			return (FALSE);
    155  1.1  thorpej 	}
    156  1.1  thorpej 
    157  1.1  thorpej 	if (_prop_object_externalize_end_tag(ctx, "data") == FALSE)
    158  1.1  thorpej 		return (FALSE);
    159  1.1  thorpej 
    160  1.1  thorpej 	return (TRUE);
    161  1.1  thorpej }
    162  1.1  thorpej 
    163  1.1  thorpej static prop_data_t
    164  1.1  thorpej _prop_data_alloc(void)
    165  1.1  thorpej {
    166  1.1  thorpej 	prop_data_t pd;
    167  1.1  thorpej 
    168  1.1  thorpej 	pd = _PROP_POOL_GET(_prop_data_pool);
    169  1.1  thorpej 	if (pd != NULL) {
    170  1.1  thorpej 		_prop_object_init(&pd->pd_obj);
    171  1.1  thorpej 		pd->pd_obj.po_type = PROP_TYPE_DATA;
    172  1.1  thorpej 		pd->pd_obj.po_free = _prop_data_free;
    173  1.1  thorpej 		pd->pd_obj.po_extern = _prop_data_externalize;
    174  1.1  thorpej 
    175  1.1  thorpej 		pd->pd_mutable = NULL;
    176  1.1  thorpej 		pd->pd_size = 0;
    177  1.1  thorpej 		pd->pd_flags = 0;
    178  1.1  thorpej 	}
    179  1.1  thorpej 
    180  1.1  thorpej 	return (pd);
    181  1.1  thorpej }
    182  1.1  thorpej 
    183  1.1  thorpej /*
    184  1.1  thorpej  * prop_data_create_data --
    185  1.1  thorpej  *	Create a data container that contains a copy of the data.
    186  1.1  thorpej  */
    187  1.1  thorpej prop_data_t
    188  1.1  thorpej prop_data_create_data(const void *v, size_t size)
    189  1.1  thorpej {
    190  1.1  thorpej 	prop_data_t pd;
    191  1.1  thorpej 	void *nv;
    192  1.1  thorpej 
    193  1.1  thorpej 	pd = _prop_data_alloc();
    194  1.1  thorpej 	if (pd != NULL) {
    195  1.1  thorpej 		nv = _PROP_MALLOC(size, M_PROP_DATA);
    196  1.1  thorpej 		if (nv == NULL) {
    197  1.1  thorpej 			_prop_data_free(pd);
    198  1.1  thorpej 			return (NULL);
    199  1.1  thorpej 		}
    200  1.1  thorpej 		memcpy(nv, v, size);
    201  1.1  thorpej 		pd->pd_mutable = nv;
    202  1.1  thorpej 		pd->pd_size = size;
    203  1.1  thorpej 	}
    204  1.1  thorpej 	return (pd);
    205  1.1  thorpej }
    206  1.1  thorpej 
    207  1.1  thorpej /*
    208  1.1  thorpej  * prop_data_create_data_nocopy --
    209  1.1  thorpej  *	Create an immutable data container that contains a refrence to the
    210  1.1  thorpej  *	provided external data.
    211  1.1  thorpej  */
    212  1.1  thorpej prop_data_t
    213  1.1  thorpej prop_data_create_data_nocopy(const void *v, size_t size)
    214  1.1  thorpej {
    215  1.1  thorpej 	prop_data_t pd;
    216  1.1  thorpej 
    217  1.1  thorpej 	pd = _prop_data_alloc();
    218  1.1  thorpej 	if (pd != NULL) {
    219  1.1  thorpej 		pd->pd_immutable = v;
    220  1.1  thorpej 		pd->pd_size = size;
    221  1.1  thorpej 		pd->pd_flags |= PD_F_NOCOPY;
    222  1.1  thorpej 	}
    223  1.1  thorpej 	return (pd);
    224  1.1  thorpej }
    225  1.1  thorpej 
    226  1.1  thorpej /*
    227  1.1  thorpej  * prop_data_copy --
    228  1.1  thorpej  *	Copy a data container.  If the original data is external, then
    229  1.1  thorpej  *	the copy is also references the same external data.
    230  1.1  thorpej  */
    231  1.1  thorpej prop_data_t
    232  1.1  thorpej prop_data_copy(prop_data_t opd)
    233  1.1  thorpej {
    234  1.1  thorpej 	prop_data_t pd;
    235  1.1  thorpej 
    236  1.1  thorpej 	_PROP_ASSERT(prop_object_is_data(opd));
    237  1.1  thorpej 
    238  1.1  thorpej 	pd = _prop_data_alloc();
    239  1.1  thorpej 	if (pd != NULL) {
    240  1.1  thorpej 		pd->pd_size = opd->pd_size;
    241  1.1  thorpej 		pd->pd_flags = opd->pd_flags;
    242  1.1  thorpej 		if (opd->pd_flags & PD_F_NOCOPY)
    243  1.1  thorpej 			pd->pd_immutable = opd->pd_immutable;
    244  1.1  thorpej 		else if (opd->pd_size != 0) {
    245  1.1  thorpej 			void *nv = _PROP_MALLOC(pd->pd_size, M_PROP_DATA);
    246  1.1  thorpej 			if (nv == NULL) {
    247  1.1  thorpej 				_prop_data_free(pd);
    248  1.1  thorpej 				return (NULL);
    249  1.1  thorpej 			}
    250  1.1  thorpej 			memcpy(nv, opd->pd_immutable, opd->pd_size);
    251  1.1  thorpej 			pd->pd_mutable = nv;
    252  1.1  thorpej 		}
    253  1.1  thorpej 	}
    254  1.1  thorpej 	return (pd);
    255  1.1  thorpej }
    256  1.1  thorpej 
    257  1.1  thorpej /*
    258  1.1  thorpej  * prop_data_size --
    259  1.1  thorpej  *	Return the size of the data.
    260  1.1  thorpej  */
    261  1.1  thorpej size_t
    262  1.1  thorpej prop_data_size(prop_data_t pd)
    263  1.1  thorpej {
    264  1.1  thorpej 
    265  1.1  thorpej 	_PROP_ASSERT(prop_object_is_data(pd));
    266  1.1  thorpej 	return (pd->pd_size);
    267  1.1  thorpej }
    268  1.1  thorpej 
    269  1.1  thorpej /*
    270  1.1  thorpej  * prop_data_data --
    271  1.1  thorpej  *	Return a copy of the contents of the data container.
    272  1.1  thorpej  *	The data is allocated with the M_TEMP malloc type.
    273  1.1  thorpej  *	If the data container is empty, NULL is returned.
    274  1.1  thorpej  */
    275  1.1  thorpej void *
    276  1.1  thorpej prop_data_data(prop_data_t pd)
    277  1.1  thorpej {
    278  1.1  thorpej 	void *v;
    279  1.1  thorpej 
    280  1.1  thorpej 	_PROP_ASSERT(prop_object_is_data(pd));
    281  1.1  thorpej 
    282  1.1  thorpej 	if (pd->pd_size == 0) {
    283  1.1  thorpej 		_PROP_ASSERT(pd->pd_immutable == NULL);
    284  1.1  thorpej 		return (NULL);
    285  1.1  thorpej 	}
    286  1.1  thorpej 
    287  1.1  thorpej 	_PROP_ASSERT(pd->pd_immutable != NULL);
    288  1.1  thorpej 
    289  1.1  thorpej 	v = _PROP_MALLOC(pd->pd_size, M_TEMP);
    290  1.1  thorpej 	if (v != NULL)
    291  1.1  thorpej 		memcpy(v, pd->pd_immutable, pd->pd_size);
    292  1.1  thorpej 
    293  1.1  thorpej 	return (v);
    294  1.1  thorpej }
    295  1.1  thorpej 
    296  1.1  thorpej /*
    297  1.1  thorpej  * prop_data_data_nocopy --
    298  1.1  thorpej  *	Return an immutable reference to the contents of the data
    299  1.1  thorpej  *	container.
    300  1.1  thorpej  */
    301  1.1  thorpej const void *
    302  1.1  thorpej prop_data_data_nocopy(prop_data_t pd)
    303  1.1  thorpej {
    304  1.1  thorpej 
    305  1.1  thorpej 	_PROP_ASSERT(prop_object_is_data(pd));
    306  1.1  thorpej 	_PROP_ASSERT((pd->pd_size == 0 && pd->pd_immutable == NULL) ||
    307  1.1  thorpej 		     (pd->pd_size != 0 && pd->pd_immutable != NULL));
    308  1.1  thorpej 
    309  1.1  thorpej 	return (pd->pd_immutable);
    310  1.1  thorpej }
    311  1.1  thorpej 
    312  1.1  thorpej /*
    313  1.1  thorpej  * prop_data_equals --
    314  1.1  thorpej  *	Return TRUE if two strings are equivalent.
    315  1.1  thorpej  */
    316  1.1  thorpej boolean_t
    317  1.1  thorpej prop_data_equals(prop_data_t pd1, prop_data_t pd2)
    318  1.1  thorpej {
    319  1.1  thorpej 
    320  1.1  thorpej 	_PROP_ASSERT(prop_object_is_data(pd1));
    321  1.1  thorpej 	_PROP_ASSERT(prop_object_is_data(pd2));
    322  1.1  thorpej 	if (pd1 == pd2)
    323  1.1  thorpej 		return (TRUE);
    324  1.1  thorpej 	if (pd1->pd_size != pd2->pd_size)
    325  1.1  thorpej 		return (FALSE);
    326  1.1  thorpej 	if (pd1->pd_size == 0) {
    327  1.1  thorpej 		_PROP_ASSERT(pd1->pd_immutable == NULL);
    328  1.1  thorpej 		_PROP_ASSERT(pd2->pd_immutable == NULL);
    329  1.1  thorpej 		return (TRUE);
    330  1.1  thorpej 	}
    331  1.1  thorpej 	return (memcmp(pd1->pd_immutable, pd2->pd_immutable,
    332  1.1  thorpej 		       pd1->pd_size) == 0);
    333  1.1  thorpej }
    334  1.1  thorpej 
    335  1.1  thorpej /*
    336  1.1  thorpej  * prop_data_equals_data --
    337  1.1  thorpej  *	Return TRUE if the contained data is equivalent to the specified
    338  1.1  thorpej  *	external data.
    339  1.1  thorpej  */
    340  1.1  thorpej boolean_t
    341  1.1  thorpej prop_data_equals_data(prop_data_t pd, const void *v, size_t size)
    342  1.1  thorpej {
    343  1.1  thorpej 
    344  1.1  thorpej 	_PROP_ASSERT(prop_object_is_data(pd));
    345  1.1  thorpej 	if (pd->pd_size != size)
    346  1.1  thorpej 		return (FALSE);
    347  1.1  thorpej 	return (memcmp(pd->pd_immutable, v, size) == 0);
    348  1.1  thorpej }
    349  1.1  thorpej 
    350  1.1  thorpej static boolean_t
    351  1.1  thorpej _prop_data_internalize_decode(struct _prop_object_internalize_context *ctx,
    352  1.1  thorpej 			     uint8_t *target, size_t targsize, size_t *sizep,
    353  1.1  thorpej 			     const char **cpp)
    354  1.1  thorpej {
    355  1.1  thorpej 	const char *src;
    356  1.1  thorpej 	size_t tarindex;
    357  1.1  thorpej 	int state, ch;
    358  1.1  thorpej 	const char *pos;
    359  1.1  thorpej 
    360  1.1  thorpej 	state = 0;
    361  1.1  thorpej 	tarindex = 0;
    362  1.1  thorpej 	src = ctx->poic_cp;
    363  1.1  thorpej 
    364  1.1  thorpej 	for (;;) {
    365  1.1  thorpej 		ch = (unsigned char) *src++;
    366  1.1  thorpej 		if (_PROP_EOF(ch))
    367  1.1  thorpej 			return (FALSE);
    368  1.1  thorpej 		if (_PROP_ISSPACE(ch))
    369  1.1  thorpej 			continue;
    370  1.1  thorpej 		if (ch == '<') {
    371  1.1  thorpej 			src--;
    372  1.1  thorpej 			break;
    373  1.1  thorpej 		}
    374  1.1  thorpej 		if (ch == _prop_data_pad64)
    375  1.1  thorpej 			break;
    376  1.1  thorpej 
    377  1.1  thorpej 		pos = strchr(_prop_data_base64, ch);
    378  1.1  thorpej 		if (pos == NULL)
    379  1.1  thorpej 			return (FALSE);
    380  1.1  thorpej 
    381  1.1  thorpej 		switch (state) {
    382  1.1  thorpej 		case 0:
    383  1.1  thorpej 			if (target) {
    384  1.1  thorpej 				if (tarindex >= targsize)
    385  1.1  thorpej 					return (FALSE);
    386  1.1  thorpej 				target[tarindex] =
    387  1.1  thorpej 				    (pos - _prop_data_base64) << 2;
    388  1.1  thorpej 			}
    389  1.1  thorpej 			state = 1;
    390  1.1  thorpej 			break;
    391  1.1  thorpej 
    392  1.1  thorpej 		case 1:
    393  1.1  thorpej 			if (target) {
    394  1.1  thorpej 				if (tarindex + 1 >= targsize)
    395  1.1  thorpej 					return (FALSE);
    396  1.1  thorpej 				target[tarindex] |=
    397  1.1  thorpej 				    (uint32_t)(pos - _prop_data_base64) >> 4;
    398  1.1  thorpej 				target[tarindex + 1] =
    399  1.1  thorpej 				    ((pos - _prop_data_base64) & 0xf) << 4;
    400  1.1  thorpej 			}
    401  1.1  thorpej 			tarindex++;
    402  1.1  thorpej 			state = 2;
    403  1.1  thorpej 			break;
    404  1.1  thorpej 
    405  1.1  thorpej 		case 2:
    406  1.1  thorpej 			if (target) {
    407  1.1  thorpej 				if (tarindex + 1 >= targsize)
    408  1.1  thorpej 					return (FALSE);
    409  1.1  thorpej 				target[tarindex] |=
    410  1.1  thorpej 				    (uint32_t)(pos - _prop_data_base64) >> 2;
    411  1.1  thorpej 				target[tarindex + 1] =
    412  1.1  thorpej 				    ((pos - _prop_data_base64) & 0x3) << 6;
    413  1.1  thorpej 			}
    414  1.1  thorpej 			tarindex++;
    415  1.1  thorpej 			state = 3;
    416  1.1  thorpej 			break;
    417  1.1  thorpej 
    418  1.1  thorpej 		case 3:
    419  1.1  thorpej 			if (target) {
    420  1.1  thorpej 				if (tarindex >= targsize)
    421  1.1  thorpej 					return (FALSE);
    422  1.1  thorpej 				target[tarindex] |= (pos - _prop_data_base64);
    423  1.1  thorpej 			}
    424  1.1  thorpej 			tarindex++;
    425  1.1  thorpej 			state = 0;
    426  1.1  thorpej 			break;
    427  1.1  thorpej 
    428  1.1  thorpej 		default:
    429  1.1  thorpej 			_PROP_ASSERT(/*CONSTCOND*/0);
    430  1.1  thorpej 		}
    431  1.1  thorpej 	}
    432  1.1  thorpej 
    433  1.1  thorpej 	/*
    434  1.1  thorpej 	 * We are done decoding the Base64 characters.  Let's see if we
    435  1.1  thorpej 	 * ended up on a byte boundary and/or with unrecognized trailing
    436  1.1  thorpej 	 * characters.
    437  1.1  thorpej 	 */
    438  1.1  thorpej 	if (ch == _prop_data_pad64) {
    439  1.1  thorpej 		ch = (unsigned char) *src;	/* src already advanced */
    440  1.1  thorpej 		if (_PROP_EOF(ch))
    441  1.1  thorpej 			return (FALSE);
    442  1.1  thorpej 		switch (state) {
    443  1.1  thorpej 		case 0:		/* Invalid = in first position */
    444  1.1  thorpej 		case 1:		/* Invalid = in second position */
    445  1.1  thorpej 			return (FALSE);
    446  1.1  thorpej 
    447  1.1  thorpej 		case 2:		/* Valid, one byte of info */
    448  1.1  thorpej 			/* Skip whitespace */
    449  1.1  thorpej 			for (ch = (unsigned char) *src++;
    450  1.1  thorpej 			     ch != '<'; ch = (unsigned char) *src++) {
    451  1.1  thorpej 				if (_PROP_EOF(ch))
    452  1.1  thorpej 					return (FALSE);
    453  1.1  thorpej 				if (!_PROP_ISSPACE(ch))
    454  1.1  thorpej 					break;
    455  1.1  thorpej 			}
    456  1.1  thorpej 			/* Make sure there is another trailing = */
    457  1.1  thorpej 			if (ch != _prop_data_pad64)
    458  1.1  thorpej 				return (FALSE);
    459  1.1  thorpej 			ch = (unsigned char) *src;
    460  1.1  thorpej 			/* FALLTHROUGH */
    461  1.1  thorpej 
    462  1.1  thorpej 		case 3:		/* Valid, two bytes of info */
    463  1.1  thorpej 			/*
    464  1.1  thorpej 			 * We know this char is a =.  Is there anything but
    465  1.1  thorpej 			 * whitespace after it?
    466  1.1  thorpej 			 */
    467  1.1  thorpej 			for (; ch != '<'; ch = (unsigned char) *src++) {
    468  1.1  thorpej 				if (_PROP_EOF(ch))
    469  1.1  thorpej 					return (FALSE);
    470  1.1  thorpej 				if (!_PROP_ISSPACE(ch))
    471  1.1  thorpej 					return (FALSE);
    472  1.1  thorpej 			}
    473  1.1  thorpej 		}
    474  1.1  thorpej 	} else {
    475  1.1  thorpej 		/*
    476  1.1  thorpej 		 * We ended by seeing the end of the Base64 string.  Make
    477  1.1  thorpej 		 * sure there are no partial bytes lying around.
    478  1.1  thorpej 		 */
    479  1.1  thorpej 		if (state != 0)
    480  1.1  thorpej 			return (FALSE);
    481  1.1  thorpej 	}
    482  1.1  thorpej 
    483  1.1  thorpej 	_PROP_ASSERT(*src == '<');
    484  1.1  thorpej 	if (sizep != NULL)
    485  1.1  thorpej 		*sizep = tarindex;
    486  1.1  thorpej 	if (cpp != NULL)
    487  1.1  thorpej 		*cpp = src;
    488  1.1  thorpej 
    489  1.1  thorpej 	return (TRUE);
    490  1.1  thorpej }
    491  1.1  thorpej 
    492  1.1  thorpej /*
    493  1.1  thorpej  * _prop_data_internalize --
    494  1.1  thorpej  *	Parse a <data>...</data> and return the object created from the
    495  1.1  thorpej  *	external representation.
    496  1.1  thorpej  */
    497  1.1  thorpej prop_object_t
    498  1.1  thorpej _prop_data_internalize(struct _prop_object_internalize_context *ctx)
    499  1.1  thorpej {
    500  1.1  thorpej 	prop_data_t data;
    501  1.1  thorpej 	uint8_t *buf;
    502  1.1  thorpej 	size_t len, alen;
    503  1.1  thorpej 
    504  1.1  thorpej 	/* We don't accept empty elements. */
    505  1.1  thorpej 	if (ctx->poic_is_empty_element)
    506  1.1  thorpej 		return (NULL);
    507  1.1  thorpej 
    508  1.1  thorpej 	/*
    509  1.1  thorpej 	 * If we got a "size" attribute, get the size of the data blob
    510  1.1  thorpej 	 * from that.  Otherwise, we have to figure it out from the base64.
    511  1.1  thorpej 	 */
    512  1.1  thorpej 	if (ctx->poic_tagattr != NULL) {
    513  1.1  thorpej 		char *cp;
    514  1.1  thorpej 
    515  1.1  thorpej 		if (!_PROP_TAGATTR_MATCH(ctx, "size") ||
    516  1.1  thorpej 		    ctx->poic_tagattrval_len == 0)
    517  1.1  thorpej 			return (NULL);
    518  1.1  thorpej 
    519  1.1  thorpej #ifndef _KERNEL
    520  1.1  thorpej 		errno = 0;
    521  1.1  thorpej #endif
    522  1.1  thorpej 		/* XXX Assumes size_t and unsigned long are the same size. */
    523  1.1  thorpej 		len = strtoul(ctx->poic_tagattrval, &cp, 0);
    524  1.1  thorpej #ifndef _KERNEL		/* XXX can't check for ERANGE in the kernel */
    525  1.1  thorpej 		if (len == ULONG_MAX && errno == ERANGE)
    526  1.1  thorpej 			return (NULL);
    527  1.1  thorpej #endif
    528  1.1  thorpej 		if (cp != ctx->poic_tagattrval + ctx->poic_tagattrval_len)
    529  1.1  thorpej 			return (NULL);
    530  1.1  thorpej 		_PROP_ASSERT(*cp == '\"');
    531  1.1  thorpej 	} else if (_prop_data_internalize_decode(ctx, NULL, 0, &len,
    532  1.1  thorpej 						NULL) == FALSE)
    533  1.1  thorpej 		return (NULL);
    534  1.1  thorpej 
    535  1.1  thorpej 	/*
    536  1.1  thorpej 	 * Always allocate one extra in case we don't land on an even byte
    537  1.1  thorpej 	 * boundary during the decode.
    538  1.1  thorpej 	 */
    539  1.1  thorpej 	buf = _PROP_MALLOC(len + 1, M_PROP_DATA);
    540  1.1  thorpej 	if (buf == NULL)
    541  1.1  thorpej 		return (NULL);
    542  1.1  thorpej 
    543  1.1  thorpej 	if (_prop_data_internalize_decode(ctx, buf, len + 1, &alen,
    544  1.1  thorpej 					  &ctx->poic_cp) == FALSE) {
    545  1.1  thorpej 		_PROP_FREE(buf, M_PROP_DATA);
    546  1.1  thorpej 		return (NULL);
    547  1.1  thorpej 	}
    548  1.1  thorpej 	if (alen != len) {
    549  1.1  thorpej 		_PROP_FREE(buf, M_PROP_DATA);
    550  1.1  thorpej 		return (NULL);
    551  1.1  thorpej 	}
    552  1.1  thorpej 
    553  1.1  thorpej 	if (_prop_object_internalize_find_tag(ctx, "data",
    554  1.1  thorpej 					      _PROP_TAG_TYPE_END) == FALSE) {
    555  1.1  thorpej 		_PROP_FREE(buf, M_PROP_DATA);
    556  1.1  thorpej 		return (NULL);
    557  1.1  thorpej 	}
    558  1.1  thorpej 
    559  1.1  thorpej 	data = _prop_data_alloc();
    560  1.1  thorpej 	if (data == NULL) {
    561  1.1  thorpej 		_PROP_FREE(buf, M_PROP_DATA);
    562  1.1  thorpej 		return (NULL);
    563  1.1  thorpej 	}
    564  1.1  thorpej 
    565  1.1  thorpej 	data->pd_mutable = buf;
    566  1.1  thorpej 	data->pd_size = len;
    567  1.1  thorpej 
    568  1.1  thorpej 	return (data);
    569  1.1  thorpej }
    570