/*-
* Copyright (c) 2006, 2007, 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.
*/
/* The easy case is an empty array, just free and return. */
if (pa->pa_count == 0) {
if (pa->pa_array != NULL)
_PROP_FREE(pa->pa_array, M_PROP_ARRAY);
_PROP_RWLOCK_DESTROY(pa->pa_rwlock);
_PROP_POOL_PUT(_prop_array_pool, pa);
return (_PROP_OBJECT_FREE_DONE);
}
po = pa->pa_array[pa->pa_count - 1];
_PROP_ASSERT(po != NULL);
if (stack == NULL) {
/*
* If we are in emergency release mode,
* just let caller recurse down.
*/
*obj = po;
return (_PROP_OBJECT_FREE_FAILED);
}
/* Otherwise, try to push the current object on the stack. */
if (!_prop_stack_push(stack, pa, NULL, NULL, NULL)) {
/* Push failed, entering emergency release mode. */
return (_PROP_OBJECT_FREE_FAILED);
}
/* Object pushed on stack, caller will release it. */
--pa->pa_count;
*obj = po;
return (_PROP_OBJECT_FREE_RECURSE);
}
static void
_prop_array_emergency_free(prop_object_t obj)
{
prop_array_t pa = obj;
/* For the first iteration, lock the objects. */
if (idx == 0) {
if ((uintptr_t)array1 < (uintptr_t)array2) {
_PROP_RWLOCK_RDLOCK(array1->pa_rwlock);
_PROP_RWLOCK_RDLOCK(array2->pa_rwlock);
} else {
_PROP_RWLOCK_RDLOCK(array2->pa_rwlock);
_PROP_RWLOCK_RDLOCK(array1->pa_rwlock);
}
}
if (array1->pa_count != array2->pa_count)
goto out;
if (idx == array1->pa_count) {
rv = _PROP_OBJECT_EQUALS_TRUE;
goto out;
}
_PROP_ASSERT(idx < array1->pa_count);
/*
* prop_array_create_with_capacity --
* Create an array with the capacity to store N objects.
*/
_PROP_EXPORT prop_array_t
prop_array_create_with_capacity(unsigned int capacity)
{
return (_prop_array_alloc(capacity));
}
/*
* prop_array_copy --
* Copy an array. The new array has an initial capacity equal to
* the number of objects stored in the original array. The new
* array contains references to the original array's objects, not
* copies of those objects (i.e. a shallow copy).
*/
_PROP_EXPORT prop_array_t
prop_array_copy(prop_array_t opa)
{
prop_array_t pa;
prop_object_t po;
unsigned int idx;
if (! prop_object_is_array(opa))
return (NULL);
_PROP_RWLOCK_RDLOCK(opa->pa_rwlock);
pa = _prop_array_alloc(opa->pa_count);
if (pa != NULL) {
for (idx = 0; idx < opa->pa_count; idx++) {
po = opa->pa_array[idx];
prop_object_retain(po);
pa->pa_array[idx] = po;
}
pa->pa_count = opa->pa_count;
pa->pa_flags = opa->pa_flags;
}
_PROP_RWLOCK_UNLOCK(opa->pa_rwlock);
return (pa);
}
/*
* prop_array_copy_mutable --
* Like prop_array_copy(), but the resulting array is mutable.
*/
_PROP_EXPORT prop_array_t
prop_array_copy_mutable(prop_array_t opa)
{
prop_array_t pa;
pa = prop_array_copy(opa);
if (pa != NULL)
pa->pa_flags &= ~PA_F_IMMUTABLE;
return (pa);
}
/*
* prop_array_capacity --
* Return the capacity of the array.
*/
_PROP_EXPORT unsigned int
prop_array_capacity(prop_array_t pa)
{
unsigned int rv;
/*
* prop_array_count --
* Return the number of objects stored in the array.
*/
_PROP_EXPORT unsigned int
prop_array_count(prop_array_t pa)
{
unsigned int rv;
/*
* prop_array_ensure_capacity --
* Ensure that the array has the capacity to store the specified
* total number of objects (including the objects already stored
* in the array).
*/
_PROP_EXPORT bool
prop_array_ensure_capacity(prop_array_t pa, unsigned int capacity)
{
bool rv;
/*
* prop_array_iterator --
* Return an iterator for the array. The array is retained by
* the iterator.
*/
_PROP_EXPORT prop_object_iterator_t
prop_array_iterator(prop_array_t pa)
{
struct _prop_array_iterator *pai;
_PROP_RWLOCK_RDLOCK(pa->pa_rwlock);
pai = _prop_array_iterator_locked(pa);
_PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
return &pai->pai_base;
}
/*
* prop_array_make_immutable --
* Make the array immutable.
*/
_PROP_EXPORT void
prop_array_make_immutable(prop_array_t pa)
{
_PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
if (prop_array_is_immutable(pa) == false)
pa->pa_flags |= PA_F_IMMUTABLE;
_PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
}
/*
* prop_array_mutable --
* Returns true if the array is mutable.
*/
_PROP_EXPORT bool
prop_array_mutable(prop_array_t pa)
{
bool rv;
/*
* prop_array_set --
* Store a reference to an object at the specified array index.
* This method is not allowed to create holes in the array; the
* caller must either be setting the object just beyond the existing
* count or replacing an already existing object reference.
*/
_PROP_EXPORT bool
prop_array_set(prop_array_t pa, unsigned int idx, prop_object_t po)
{
prop_object_t opo;
bool rv = false;
/*
* prop_array_add --
* Add a reference to an object to the specified array, appending
* to the end and growing the array's capacity, if necessary.
*/
_PROP_EXPORT bool
prop_array_add(prop_array_t pa, prop_object_t po)
{
bool rv;
/*
* prop_array_remove --
* Remove the reference to an object from an array at the specified
* index. The array will be compacted following the removal.
*/
_PROP_EXPORT void
prop_array_remove(prop_array_t pa, unsigned int idx)
{
prop_object_t po;
if (! prop_object_is_array(pa))
return;
_PROP_RWLOCK_WRLOCK(pa->pa_rwlock);
_PROP_ASSERT(idx < pa->pa_count);
/* XXX Should this be a _PROP_ASSERT()? */
if (prop_array_is_immutable(pa)) {
_PROP_RWLOCK_UNLOCK(pa->pa_rwlock);
return;
}
/*
* prop_array_equals --
* Return true if the two arrays are equivalent. Note we do a
* by-value comparison of the objects in the array.
*/
_PROP_EXPORT bool
prop_array_equals(prop_array_t array1, prop_array_t array2)
{
if (!prop_object_is_array(array1) || !prop_object_is_array(array2))
return (false);
return (prop_object_equals(array1, array2));
}
/*
* prop_array_externalize --
* Externalize an array in XML format.
*/
_PROP_EXPORT char *
prop_array_externalize(prop_array_t pa)
{
return _prop_object_externalize(&pa->pa_obj, PROP_FORMAT_XML);
}
/*
* _prop_array_internalize --
* Parse an <array>...</array> and return the object created from the
* external representation.
*/
static bool _prop_array_internalize_body(prop_stack_t, prop_object_t *,
struct _prop_object_internalize_context *);
bool
_prop_array_internalize(prop_stack_t stack, prop_object_t *obj,
struct _prop_object_internalize_context *ctx)
{
/* We don't currently understand any attributes. */
if (ctx->poic_tagattr != NULL)
return (true);
*obj = prop_array_create();
/*
* We are done if the create failed or no child elements exist.
*/
if (*obj == NULL || ctx->poic_is_empty_element)
return (true);
/*
* Opening tag is found, now continue to the first element.
*/
return (_prop_array_internalize_body(stack, obj, ctx));
}
/*
* Current element is processed and added, look for next.
* For JSON, we'll skip the comma separator, if present.
*
* By doing this here, we correctly error out if a separator
* is found other than after an element, but this does mean
* that we do allow a trailing comma after the final element
* which isn't allowed in the JSON spec, but seems pretty
* harmless (and there are other JSON parsers that also allow
* it).
*
* Conversely, we don't want to *require* the separator if the
* spec doesn't require it, and we don't know what's next in
* the buffer, so we basically treat the separator as completely
* optional. Since there does not appear to be any ambiguity,
* this also seems pretty harmless.
*
* (FWIW, RFC 8259 section 9 seems to specifically allow this.)
*/
if (ctx->poic_format == PROP_FORMAT_JSON) {
ctx->poic_cp = _prop_intern_skip_whitespace(ctx->poic_cp);
if (*ctx->poic_cp == ',') {
ctx->poic_cp++;
}
}
return (_prop_array_internalize_body(stack, obj, ctx));
if (ctx->poic_format == PROP_FORMAT_JSON) {
ctx->poic_cp = _prop_intern_skip_whitespace(ctx->poic_cp);
/* Check to see if this is the end of the array. */
if (*ctx->poic_cp == ']') {
/* It is, so don't iterate any further. */
ctx->poic_cp++;
return true;
}
} else {
/* Fetch the next tag. */
if (_prop_xml_intern_find_tag(ctx, NULL,
_PROP_TAG_TYPE_EITHER) == false)
goto bad;
/* Check to see if this is the end of the array. */
if (_PROP_TAG_MATCH(ctx, "array") &&
ctx->poic_tag_type == _PROP_TAG_TYPE_END) {
/* It is, so don't iterate any further. */
return (true);
}
}
if (_prop_stack_push(stack, array,
_prop_array_internalize_continue, NULL, NULL))
return (false);