/*-
* Copyright (c) 2006, 2020, 2025 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
_PROP_DEPRECATED(prop_data_create_data_nocopy,
"this program uses prop_data_create_data_nocopy(), "
"which is deprecated; use prop_data_create_nocopy() instead.")
_PROP_EXPORT prop_data_t
prop_data_create_data_nocopy(const void *v, size_t size)
{
return prop_data_create_nocopy(v, size);
}
/*
* prop_data_create_copy --
* Create a data object with a copy of the provided data.
*/
_PROP_EXPORT prop_data_t
prop_data_create_copy(const void *v, size_t size)
{
prop_data_t pd;
void *nv;
/* Tolerate the creation of empty data objects. */
if (v != NULL && size != 0) {
nv = _PROP_MALLOC(size, M_PROP_DATA);
if (nv == NULL)
return (NULL);
/*
* prop_data_create_nocopy --
* Create a data object using the provided external data reference.
*/
_PROP_EXPORT prop_data_t
prop_data_create_nocopy(const void *v, size_t size)
{
/* Tolerate the creation of empty data objects. */
if (v == NULL || size == 0) {
v = NULL;
size = 0;
}
/*
* prop_data_copy --
* Copy a data container. If the original data is external, then
* the copy is also references the same external data.
*/
_PROP_EXPORT prop_data_t
prop_data_copy(prop_data_t opd)
{
prop_data_t pd;
if (! prop_object_is_data(opd))
return (NULL);
if ((opd->pd_flags & PD_F_NOCOPY) != 0 ||
(opd->pd_flags & PD_F_MUTABLE) == 0) {
/* Just retain and return the original. */
prop_object_retain(opd);
return (opd);
}
/*
* prop_data_size --
* Return the size of the data.
*/
_PROP_EXPORT size_t
prop_data_size(prop_data_t pd)
{
if (! prop_object_is_data(pd))
return (0);
return (pd->pd_size);
}
/*
* prop_data_value --
* Returns a pointer to the data object's value. This pointer
* remains valid only as long as the data object.
*/
_PROP_EXPORT const void *
prop_data_value(prop_data_t pd)
{
if (! prop_object_is_data(pd))
return (0);
return (pd->pd_immutable);
}
/*
* prop_data_copy_value --
* Copy the data object's value into the supplied buffer.
*/
_PROP_EXPORT bool
prop_data_copy_value(prop_data_t pd, void *buf, size_t buflen)
{
if (! prop_object_is_data(pd))
return (false);
if (buf == NULL || buflen < pd->pd_size)
return (false);
/* Tolerate empty data objects. */
if (pd->pd_immutable == NULL || pd->pd_size == 0)
return (false);
memcpy(buf, pd->pd_immutable, pd->pd_size);
return (true);
}
_PROP_DEPRECATED(prop_data_data,
"this program uses prop_data_data(), "
"which is deprecated; use prop_data_copy_value() instead.")
_PROP_EXPORT void *
prop_data_data(prop_data_t pd)
{
void *v;
v = _PROP_MALLOC(pd->pd_size, M_TEMP);
if (v != NULL)
memcpy(v, pd->pd_immutable, pd->pd_size);
return (v);
}
_PROP_DEPRECATED(prop_data_data_nocopy,
"this program uses prop_data_data_nocopy(), "
"which is deprecated; use prop_data_value() instead.")
_PROP_EXPORT const void *
prop_data_data_nocopy(prop_data_t pd)
{
return prop_data_value(pd);
}
/*
* prop_data_equals --
* Return true if two data objects are equivalent.
*/
_PROP_EXPORT bool
prop_data_equals(prop_data_t pd1, prop_data_t pd2)
{
if (!prop_object_is_data(pd1) || !prop_object_is_data(pd2))
return (false);
return (prop_object_equals(pd1, pd2));
}
/*
* prop_data_equals_data --
* Return true if the contained data is equivalent to the specified
* external data.
*/
_PROP_EXPORT bool
prop_data_equals_data(prop_data_t pd, const void *v, size_t size)
{
if (! prop_object_is_data(pd))
return (false);
if (pd->pd_size != size || v == NULL)
return (false);
for (;;) {
ch = (unsigned char) *src++;
if (_PROP_EOF(ch))
return (false);
if (_PROP_ISSPACE(ch))
continue;
if (ch == '<') {
src--;
break;
}
if (ch == _prop_data_pad64)
break;
pos = strchr(_prop_data_base64, ch);
if (pos == NULL)
return (false);
switch (state) {
case 0:
if (target) {
if (tarindex >= targsize)
return (false);
target[tarindex] =
(uint8_t)((pos - _prop_data_base64) << 2);
}
state = 1;
break;
case 1:
if (target) {
if (tarindex + 1 >= targsize)
return (false);
target[tarindex] |=
(uint32_t)(pos - _prop_data_base64) >> 4;
target[tarindex + 1] =
(uint8_t)(((pos - _prop_data_base64) & 0xf)
<< 4);
}
tarindex++;
state = 2;
break;
case 2:
if (target) {
if (tarindex + 1 >= targsize)
return (false);
target[tarindex] |=
(uint32_t)(pos - _prop_data_base64) >> 2;
target[tarindex + 1] =
(uint8_t)(((pos - _prop_data_base64)
& 0x3) << 6);
}
tarindex++;
state = 3;
break;
case 3:
if (target) {
if (tarindex >= targsize)
return (false);
target[tarindex] |= (uint8_t)
(pos - _prop_data_base64);
}
tarindex++;
state = 0;
break;
default:
_PROP_ASSERT(/*CONSTCOND*/0);
}
}
/*
* We are done decoding the Base64 characters. Let's see if we
* ended up on a byte boundary and/or with unrecognized trailing
* characters.
*/
if (ch == _prop_data_pad64) {
ch = (unsigned char) *src; /* src already advanced */
if (_PROP_EOF(ch))
return (false);
switch (state) {
case 0: /* Invalid = in first position */
case 1: /* Invalid = in second position */
return (false);
case 2: /* Valid, one byte of info */
/* Skip whitespace */
for (ch = (unsigned char) *src++;
ch != '<'; ch = (unsigned char) *src++) {
if (_PROP_EOF(ch))
return (false);
if (!_PROP_ISSPACE(ch))
break;
}
/* Make sure there is another trailing = */
if (ch != _prop_data_pad64)
return (false);
ch = (unsigned char) *src;
/* FALLTHROUGH */
case 3: /* Valid, two bytes of info */
/*
* We know this char is a =. Is there anything but
* whitespace after it?
*/
for (ch = (unsigned char) *src++;
ch != '<'; ch = (unsigned char) *src++) {
if (_PROP_EOF(ch))
return (false);
if (!_PROP_ISSPACE(ch))
return (false);
}
/* back up to '<' */
src--;
}
} else {
/*
* We ended by seeing the end of the Base64 string. Make
* sure there are no partial bytes lying around.
*/
if (state != 0)
return (false);
}
_PROP_ASSERT(*src == '<');
if (sizep != NULL)
*sizep = tarindex;
if (cpp != NULL)
*cpp = src;
return (true);
}
/*
* _prop_data_internalize --
* Parse a <data>...</data> and return the object created from the
* external representation.
*/
/* strtoul is used for parsing, enforce. */
typedef int PROP_DATA_ASSERT[/* CONSTCOND */sizeof(size_t) == sizeof(unsigned long) ? 1 : -1];
/* No JSON binary data object representation. */
if (ctx->poic_format == PROP_FORMAT_JSON) {
return true;
}
/*
* We don't accept empty elements.
* This actually only checks for the node to be <data/>
* (Which actually causes another error if found.)
*/
if (ctx->poic_is_empty_element)
return (true);
/*
* If we got a "size" attribute, get the size of the data blob
* from that. Otherwise, we have to figure it out from the base64.
*/
if (ctx->poic_tagattr != NULL) {
char *cp;
if (!_PROP_TAGATTR_MATCH(ctx, "size") ||
ctx->poic_tagattrval_len == 0)
return (true);
#ifndef _KERNEL
errno = 0;
#endif
len = strtoul(ctx->poic_tagattrval, &cp, 0);
#ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
if (len == ULONG_MAX && errno == ERANGE)
return (true);
#endif
if (cp != ctx->poic_tagattrval + ctx->poic_tagattrval_len)
return (true);
_PROP_ASSERT(*cp == '\"');
} else if (_prop_data_internalize_decode(ctx, NULL, 0, &len,
NULL) == false)
return (true);
/*
* Always allocate one extra in case we don't land on an even byte
* boundary during the decode.
*/
buf = _PROP_MALLOC(len + 1, M_PROP_DATA);
if (buf == NULL)
return (true);
if (_prop_data_internalize_decode(ctx, buf, len + 1, &alen,
&ctx->poic_cp) == false) {
_PROP_FREE(buf, M_PROP_DATA);
return (true);
}
if (alen != len) {
_PROP_FREE(buf, M_PROP_DATA);
return (true);
}
/*
* Handle alternate type of empty node.
* XML document could contain open/close tags, yet still be empty.
*/
if (alen == 0) {
_PROP_FREE(buf, M_PROP_DATA);
buf = NULL;
}
data = _prop_data_instantiate(0, buf, len);
if (data == NULL && buf != NULL)
_PROP_FREE(buf, M_PROP_DATA);