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