Home | History | Annotate | Line # | Download | only in dist
nvpair.c revision 1.6
      1  1.6     rmind /*	$NetBSD: nvpair.c,v 1.6 2019/02/15 22:49:24 rmind Exp $	*/
      2  1.2  christos 
      3  1.1  christos /*-
      4  1.1  christos  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  1.1  christos  *
      6  1.1  christos  * Copyright (c) 2009-2013 The FreeBSD Foundation
      7  1.1  christos  * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo (at) FreeBSD.org>
      8  1.1  christos  * All rights reserved.
      9  1.1  christos  *
     10  1.1  christos  * This software was developed by Pawel Jakub Dawidek under sponsorship from
     11  1.1  christos  * the FreeBSD Foundation.
     12  1.1  christos  *
     13  1.1  christos  * Redistribution and use in source and binary forms, with or without
     14  1.1  christos  * modification, are permitted provided that the following conditions
     15  1.1  christos  * are met:
     16  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     17  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     18  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     20  1.1  christos  *    documentation and/or other materials provided with the distribution.
     21  1.1  christos  *
     22  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
     26  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  christos  * SUCH DAMAGE.
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include <sys/cdefs.h>
     36  1.2  christos #ifdef __FreeBSD__
     37  1.1  christos __FBSDID("$FreeBSD: head/sys/contrib/libnv/nvpair.c 335382 2018-06-19 18:43:02Z lwhsu $");
     38  1.2  christos #else
     39  1.6     rmind __RCSID("$NetBSD: nvpair.c,v 1.6 2019/02/15 22:49:24 rmind Exp $");
     40  1.2  christos #endif
     41  1.1  christos 
     42  1.1  christos #include <sys/param.h>
     43  1.1  christos #include <sys/endian.h>
     44  1.1  christos #include <sys/queue.h>
     45  1.1  christos 
     46  1.2  christos #if defined(_KERNEL) || defined(_STANDALONE)
     47  1.1  christos 
     48  1.1  christos #include <sys/errno.h>
     49  1.1  christos #include <sys/lock.h>
     50  1.1  christos #include <sys/malloc.h>
     51  1.1  christos #include <sys/systm.h>
     52  1.1  christos 
     53  1.2  christos #ifdef __FreeBSD__
     54  1.1  christos #include <machine/stdarg.h>
     55  1.2  christos #endif
     56  1.1  christos 
     57  1.1  christos #else
     58  1.1  christos #include <errno.h>
     59  1.1  christos #include <fcntl.h>
     60  1.1  christos #include <stdarg.h>
     61  1.1  christos #include <stdbool.h>
     62  1.1  christos #include <stdint.h>
     63  1.1  christos #include <stdlib.h>
     64  1.1  christos #include <string.h>
     65  1.1  christos #include <unistd.h>
     66  1.1  christos 
     67  1.1  christos #include "common_impl.h"
     68  1.1  christos #endif
     69  1.1  christos 
     70  1.1  christos #ifdef HAVE_PJDLOG
     71  1.1  christos #include <pjdlog.h>
     72  1.1  christos #endif
     73  1.1  christos 
     74  1.2  christos #ifdef __FreeBSD__
     75  1.1  christos #include <sys/nv.h>
     76  1.2  christos #else
     77  1.2  christos #include "nv.h"
     78  1.2  christos #endif
     79  1.1  christos 
     80  1.1  christos #include "nv_impl.h"
     81  1.1  christos #include "nvlist_impl.h"
     82  1.1  christos #include "nvpair_impl.h"
     83  1.1  christos 
     84  1.1  christos #ifndef	HAVE_PJDLOG
     85  1.2  christos #if defined(_KERNEL) || defined(_STANDALONE)
     86  1.2  christos #ifdef __FreeBSD__
     87  1.1  christos #define	PJDLOG_ASSERT(...)		MPASS(__VA_ARGS__)
     88  1.2  christos #else
     89  1.2  christos #define	PJDLOG_ASSERT(...)		KASSERT(__VA_ARGS__)
     90  1.2  christos #endif
     91  1.1  christos #define	PJDLOG_RASSERT(expr, ...)	KASSERT(expr, (__VA_ARGS__))
     92  1.1  christos #define	PJDLOG_ABORT(...)		panic(__VA_ARGS__)
     93  1.1  christos #else
     94  1.2  christos #ifndef __lint__
     95  1.1  christos #include <assert.h>
     96  1.1  christos #define	PJDLOG_ASSERT(...)		assert(__VA_ARGS__)
     97  1.1  christos #define	PJDLOG_RASSERT(expr, ...)	assert(expr)
     98  1.1  christos #define	PJDLOG_ABORT(...)		abort()
     99  1.2  christos #else
    100  1.2  christos #define	PJDLOG_ASSERT(...)
    101  1.2  christos #define	PJDLOG_RASSERT(expr, ...)
    102  1.2  christos #define	PJDLOG_ABORT(...)
    103  1.2  christos #endif
    104  1.1  christos #endif
    105  1.1  christos #endif
    106  1.1  christos 
    107  1.1  christos #define	NVPAIR_MAGIC	0x6e7670	/* "nvp" */
    108  1.1  christos struct nvpair {
    109  1.1  christos 	int		 nvp_magic;
    110  1.1  christos 	char		*nvp_name;
    111  1.1  christos 	int		 nvp_type;
    112  1.1  christos 	uint64_t	 nvp_data;
    113  1.1  christos 	size_t		 nvp_datasize;
    114  1.1  christos 	size_t		 nvp_nitems;	/* Used only for array types. */
    115  1.1  christos 	nvlist_t	*nvp_list;
    116  1.1  christos 	TAILQ_ENTRY(nvpair) nvp_next;
    117  1.1  christos };
    118  1.1  christos 
    119  1.1  christos #define	NVPAIR_ASSERT(nvp)	do {					\
    120  1.1  christos 	PJDLOG_ASSERT((nvp) != NULL);					\
    121  1.1  christos 	PJDLOG_ASSERT((nvp)->nvp_magic == NVPAIR_MAGIC);		\
    122  1.2  christos } while (/*CONSTCOND*/0)
    123  1.1  christos 
    124  1.1  christos struct nvpair_header {
    125  1.1  christos 	uint8_t		nvph_type;
    126  1.1  christos 	uint16_t	nvph_namesize;
    127  1.1  christos 	uint64_t	nvph_datasize;
    128  1.1  christos 	uint64_t	nvph_nitems;
    129  1.1  christos } __packed;
    130  1.1  christos 
    131  1.1  christos 
    132  1.1  christos void
    133  1.1  christos nvpair_assert(const nvpair_t *nvp)
    134  1.1  christos {
    135  1.1  christos 
    136  1.1  christos 	NVPAIR_ASSERT(nvp);
    137  1.1  christos }
    138  1.1  christos 
    139  1.1  christos static nvpair_t *
    140  1.1  christos nvpair_allocv(const char *name, int type, uint64_t data, size_t datasize,
    141  1.1  christos     size_t nitems)
    142  1.1  christos {
    143  1.1  christos 	nvpair_t *nvp;
    144  1.1  christos 	size_t namelen;
    145  1.1  christos 
    146  1.1  christos 	PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST);
    147  1.1  christos 
    148  1.1  christos 	namelen = strlen(name);
    149  1.1  christos 	if (namelen >= NV_NAME_MAX) {
    150  1.1  christos 		ERRNO_SET(ENAMETOOLONG);
    151  1.1  christos 		return (NULL);
    152  1.1  christos 	}
    153  1.1  christos 
    154  1.1  christos 	nvp = nv_calloc(1, sizeof(*nvp) + namelen + 1);
    155  1.1  christos 	if (nvp != NULL) {
    156  1.1  christos 		nvp->nvp_name = (char *)(nvp + 1);
    157  1.1  christos 		memcpy(nvp->nvp_name, name, namelen);
    158  1.1  christos 		nvp->nvp_name[namelen] = '\0';
    159  1.1  christos 		nvp->nvp_type = type;
    160  1.1  christos 		nvp->nvp_data = data;
    161  1.1  christos 		nvp->nvp_datasize = datasize;
    162  1.1  christos 		nvp->nvp_nitems = nitems;
    163  1.1  christos 		nvp->nvp_magic = NVPAIR_MAGIC;
    164  1.1  christos 	}
    165  1.1  christos 
    166  1.1  christos 	return (nvp);
    167  1.1  christos }
    168  1.1  christos 
    169  1.1  christos static int
    170  1.1  christos nvpair_append(nvpair_t *nvp, const void *value, size_t valsize, size_t datasize)
    171  1.1  christos {
    172  1.1  christos 	void *olddata, *data, *valp;
    173  1.1  christos 	size_t oldlen;
    174  1.1  christos 
    175  1.1  christos 	oldlen = nvp->nvp_nitems * valsize;
    176  1.1  christos 	olddata = (void *)(uintptr_t)nvp->nvp_data;
    177  1.1  christos 	data = nv_realloc(olddata, oldlen + valsize);
    178  1.1  christos 	if (data == NULL) {
    179  1.1  christos 		ERRNO_SET(ENOMEM);
    180  1.1  christos 		return (-1);
    181  1.1  christos 	}
    182  1.1  christos 	valp = (unsigned char *)data + oldlen;
    183  1.1  christos 	memcpy(valp, value, valsize);
    184  1.1  christos 
    185  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)data;
    186  1.1  christos 	nvp->nvp_datasize += datasize;
    187  1.1  christos 	nvp->nvp_nitems++;
    188  1.1  christos 	return (0);
    189  1.1  christos }
    190  1.1  christos 
    191  1.1  christos nvlist_t *
    192  1.1  christos nvpair_nvlist(const nvpair_t *nvp)
    193  1.1  christos {
    194  1.1  christos 
    195  1.1  christos 	NVPAIR_ASSERT(nvp);
    196  1.1  christos 
    197  1.1  christos 	return (nvp->nvp_list);
    198  1.1  christos }
    199  1.1  christos 
    200  1.1  christos nvpair_t *
    201  1.1  christos nvpair_next(const nvpair_t *nvp)
    202  1.1  christos {
    203  1.1  christos 
    204  1.1  christos 	NVPAIR_ASSERT(nvp);
    205  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_list != NULL);
    206  1.1  christos 
    207  1.1  christos 	return (TAILQ_NEXT(nvp, nvp_next));
    208  1.1  christos }
    209  1.1  christos 
    210  1.1  christos nvpair_t *
    211  1.1  christos nvpair_prev(const nvpair_t *nvp)
    212  1.1  christos {
    213  1.1  christos 
    214  1.1  christos 	NVPAIR_ASSERT(nvp);
    215  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_list != NULL);
    216  1.1  christos 
    217  1.1  christos 	return (TAILQ_PREV(nvp, nvl_head, nvp_next));
    218  1.1  christos }
    219  1.1  christos 
    220  1.1  christos void
    221  1.1  christos nvpair_insert(struct nvl_head *head, nvpair_t *nvp, nvlist_t *nvl)
    222  1.1  christos {
    223  1.1  christos 
    224  1.1  christos 	NVPAIR_ASSERT(nvp);
    225  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_list == NULL);
    226  1.1  christos 	PJDLOG_ASSERT((nvlist_flags(nvl) & NV_FLAG_NO_UNIQUE) != 0 ||
    227  1.1  christos 	    !nvlist_exists(nvl, nvpair_name(nvp)));
    228  1.1  christos 
    229  1.1  christos 	TAILQ_INSERT_TAIL(head, nvp, nvp_next);
    230  1.1  christos 	nvp->nvp_list = nvl;
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos static void
    234  1.1  christos nvpair_remove_nvlist(nvpair_t *nvp)
    235  1.1  christos {
    236  1.1  christos 	nvlist_t *nvl;
    237  1.1  christos 
    238  1.1  christos 	/* XXX: DECONST is bad, mkay? */
    239  1.1  christos 	nvl = __DECONST(nvlist_t *, nvpair_get_nvlist(nvp));
    240  1.1  christos 	PJDLOG_ASSERT(nvl != NULL);
    241  1.1  christos 	nvlist_set_parent(nvl, NULL);
    242  1.1  christos }
    243  1.1  christos 
    244  1.1  christos static void
    245  1.1  christos nvpair_remove_nvlist_array(nvpair_t *nvp)
    246  1.1  christos {
    247  1.1  christos 	nvlist_t **nvlarray;
    248  1.1  christos 	size_t count, i;
    249  1.1  christos 
    250  1.1  christos 	/* XXX: DECONST is bad, mkay? */
    251  1.1  christos 	nvlarray = __DECONST(nvlist_t **,
    252  1.1  christos 	    nvpair_get_nvlist_array(nvp, &count));
    253  1.1  christos 	for (i = 0; i < count; i++) {
    254  1.6     rmind 		nvlist_set_array_next(nvlarray[i], NULL);
    255  1.6     rmind 		nvlist_set_parent(nvlarray[i], NULL);
    256  1.1  christos 	}
    257  1.1  christos }
    258  1.1  christos 
    259  1.1  christos void
    260  1.1  christos nvpair_remove(struct nvl_head *head, nvpair_t *nvp, const nvlist_t *nvl)
    261  1.1  christos {
    262  1.1  christos 
    263  1.1  christos 	NVPAIR_ASSERT(nvp);
    264  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_list == nvl);
    265  1.1  christos 
    266  1.1  christos 	if (nvpair_type(nvp) == NV_TYPE_NVLIST)
    267  1.1  christos 		nvpair_remove_nvlist(nvp);
    268  1.1  christos 	else if (nvpair_type(nvp) == NV_TYPE_NVLIST_ARRAY)
    269  1.1  christos 		nvpair_remove_nvlist_array(nvp);
    270  1.1  christos 
    271  1.1  christos 	TAILQ_REMOVE(head, nvp, nvp_next);
    272  1.1  christos 	nvp->nvp_list = NULL;
    273  1.1  christos }
    274  1.1  christos 
    275  1.1  christos nvpair_t *
    276  1.1  christos nvpair_clone(const nvpair_t *nvp)
    277  1.1  christos {
    278  1.1  christos 	nvpair_t *newnvp;
    279  1.1  christos 	const char *name;
    280  1.1  christos 	const void *data;
    281  1.1  christos 	size_t datasize;
    282  1.1  christos 
    283  1.1  christos 	NVPAIR_ASSERT(nvp);
    284  1.1  christos 
    285  1.1  christos 	name = nvpair_name(nvp);
    286  1.1  christos 
    287  1.1  christos 	switch (nvpair_type(nvp)) {
    288  1.1  christos 	case NV_TYPE_NULL:
    289  1.1  christos 		newnvp = nvpair_create_null(name);
    290  1.1  christos 		break;
    291  1.1  christos 	case NV_TYPE_BOOL:
    292  1.1  christos 		newnvp = nvpair_create_bool(name, nvpair_get_bool(nvp));
    293  1.1  christos 		break;
    294  1.1  christos 	case NV_TYPE_NUMBER:
    295  1.1  christos 		newnvp = nvpair_create_number(name, nvpair_get_number(nvp));
    296  1.1  christos 		break;
    297  1.1  christos 	case NV_TYPE_STRING:
    298  1.1  christos 		newnvp = nvpair_create_string(name, nvpair_get_string(nvp));
    299  1.1  christos 		break;
    300  1.1  christos 	case NV_TYPE_NVLIST:
    301  1.1  christos 		newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp));
    302  1.1  christos 		break;
    303  1.1  christos 	case NV_TYPE_BINARY:
    304  1.1  christos 		data = nvpair_get_binary(nvp, &datasize);
    305  1.1  christos 		newnvp = nvpair_create_binary(name, data, datasize);
    306  1.1  christos 		break;
    307  1.1  christos 	case NV_TYPE_BOOL_ARRAY:
    308  1.1  christos 		data = nvpair_get_bool_array(nvp, &datasize);
    309  1.1  christos 		newnvp = nvpair_create_bool_array(name, data, datasize);
    310  1.1  christos 		break;
    311  1.1  christos 	case NV_TYPE_NUMBER_ARRAY:
    312  1.1  christos 		data = nvpair_get_number_array(nvp, &datasize);
    313  1.1  christos 		newnvp = nvpair_create_number_array(name, data, datasize);
    314  1.1  christos 		break;
    315  1.1  christos 	case NV_TYPE_STRING_ARRAY:
    316  1.1  christos 		data = nvpair_get_string_array(nvp, &datasize);
    317  1.1  christos 		newnvp = nvpair_create_string_array(name, data, datasize);
    318  1.1  christos 		break;
    319  1.1  christos 	case NV_TYPE_NVLIST_ARRAY:
    320  1.1  christos 		data = nvpair_get_nvlist_array(nvp, &datasize);
    321  1.1  christos 		newnvp = nvpair_create_nvlist_array(name, data, datasize);
    322  1.1  christos 		break;
    323  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
    324  1.1  christos 	case NV_TYPE_DESCRIPTOR:
    325  1.1  christos 		newnvp = nvpair_create_descriptor(name,
    326  1.1  christos 		    nvpair_get_descriptor(nvp));
    327  1.1  christos 		break;
    328  1.1  christos 	case NV_TYPE_DESCRIPTOR_ARRAY:
    329  1.1  christos 		data = nvpair_get_descriptor_array(nvp, &datasize);
    330  1.1  christos 		newnvp = nvpair_create_descriptor_array(name, data, datasize);
    331  1.1  christos 		break;
    332  1.1  christos #endif
    333  1.1  christos 	default:
    334  1.1  christos 		PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
    335  1.1  christos 	}
    336  1.1  christos 
    337  1.1  christos 	return (newnvp);
    338  1.1  christos }
    339  1.1  christos 
    340  1.1  christos size_t
    341  1.1  christos nvpair_header_size(void)
    342  1.1  christos {
    343  1.1  christos 
    344  1.1  christos 	return (sizeof(struct nvpair_header));
    345  1.1  christos }
    346  1.1  christos 
    347  1.1  christos size_t
    348  1.1  christos nvpair_size(const nvpair_t *nvp)
    349  1.1  christos {
    350  1.1  christos 
    351  1.1  christos 	NVPAIR_ASSERT(nvp);
    352  1.1  christos 
    353  1.1  christos 	return (nvp->nvp_datasize);
    354  1.1  christos }
    355  1.1  christos 
    356  1.1  christos unsigned char *
    357  1.1  christos nvpair_pack_header(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    358  1.1  christos {
    359  1.1  christos 	struct nvpair_header nvphdr;
    360  1.1  christos 	size_t namesize;
    361  1.1  christos 
    362  1.1  christos 	NVPAIR_ASSERT(nvp);
    363  1.1  christos 
    364  1.1  christos 	nvphdr.nvph_type = nvp->nvp_type;
    365  1.1  christos 	namesize = strlen(nvp->nvp_name) + 1;
    366  1.1  christos 	PJDLOG_ASSERT(namesize > 0 && namesize <= UINT16_MAX);
    367  1.1  christos 	nvphdr.nvph_namesize = namesize;
    368  1.1  christos 	nvphdr.nvph_datasize = nvp->nvp_datasize;
    369  1.1  christos 	nvphdr.nvph_nitems = nvp->nvp_nitems;
    370  1.1  christos 	PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
    371  1.1  christos 	memcpy(ptr, &nvphdr, sizeof(nvphdr));
    372  1.1  christos 	ptr += sizeof(nvphdr);
    373  1.1  christos 	*leftp -= sizeof(nvphdr);
    374  1.1  christos 
    375  1.1  christos 	PJDLOG_ASSERT(*leftp >= namesize);
    376  1.1  christos 	memcpy(ptr, nvp->nvp_name, namesize);
    377  1.1  christos 	ptr += namesize;
    378  1.1  christos 	*leftp -= namesize;
    379  1.1  christos 
    380  1.1  christos 	return (ptr);
    381  1.1  christos }
    382  1.1  christos 
    383  1.1  christos unsigned char *
    384  1.1  christos nvpair_pack_null(const nvpair_t *nvp, unsigned char *ptr,
    385  1.1  christos     size_t *leftp __unused)
    386  1.1  christos {
    387  1.1  christos 
    388  1.1  christos 	NVPAIR_ASSERT(nvp);
    389  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL);
    390  1.1  christos 
    391  1.1  christos 	return (ptr);
    392  1.1  christos }
    393  1.1  christos 
    394  1.1  christos unsigned char *
    395  1.1  christos nvpair_pack_bool(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    396  1.1  christos {
    397  1.1  christos 	uint8_t value;
    398  1.1  christos 
    399  1.1  christos 	NVPAIR_ASSERT(nvp);
    400  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL);
    401  1.1  christos 
    402  1.1  christos 	value = (uint8_t)nvp->nvp_data;
    403  1.1  christos 
    404  1.1  christos 	PJDLOG_ASSERT(*leftp >= sizeof(value));
    405  1.1  christos 	memcpy(ptr, &value, sizeof(value));
    406  1.1  christos 	ptr += sizeof(value);
    407  1.1  christos 	*leftp -= sizeof(value);
    408  1.1  christos 
    409  1.1  christos 	return (ptr);
    410  1.1  christos }
    411  1.1  christos 
    412  1.1  christos unsigned char *
    413  1.1  christos nvpair_pack_number(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    414  1.1  christos {
    415  1.1  christos 	uint64_t value;
    416  1.1  christos 
    417  1.1  christos 	NVPAIR_ASSERT(nvp);
    418  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER);
    419  1.1  christos 
    420  1.1  christos 	value = (uint64_t)nvp->nvp_data;
    421  1.1  christos 
    422  1.1  christos 	PJDLOG_ASSERT(*leftp >= sizeof(value));
    423  1.1  christos 	memcpy(ptr, &value, sizeof(value));
    424  1.1  christos 	ptr += sizeof(value);
    425  1.1  christos 	*leftp -= sizeof(value);
    426  1.1  christos 
    427  1.1  christos 	return (ptr);
    428  1.1  christos }
    429  1.1  christos 
    430  1.1  christos unsigned char *
    431  1.1  christos nvpair_pack_string(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    432  1.1  christos {
    433  1.1  christos 
    434  1.1  christos 	NVPAIR_ASSERT(nvp);
    435  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
    436  1.1  christos 
    437  1.1  christos 	PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
    438  1.1  christos 	memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
    439  1.1  christos 	ptr += nvp->nvp_datasize;
    440  1.1  christos 	*leftp -= nvp->nvp_datasize;
    441  1.1  christos 
    442  1.1  christos 	return (ptr);
    443  1.1  christos }
    444  1.1  christos 
    445  1.1  christos unsigned char *
    446  1.1  christos nvpair_pack_nvlist_up(unsigned char *ptr, size_t *leftp)
    447  1.1  christos {
    448  1.1  christos 	struct nvpair_header nvphdr;
    449  1.1  christos 	size_t namesize;
    450  1.1  christos 	const char *name = "";
    451  1.1  christos 
    452  1.1  christos 	namesize = 1;
    453  1.1  christos 	nvphdr.nvph_type = NV_TYPE_NVLIST_UP;
    454  1.1  christos 	nvphdr.nvph_namesize = namesize;
    455  1.1  christos 	nvphdr.nvph_datasize = 0;
    456  1.1  christos 	nvphdr.nvph_nitems = 0;
    457  1.1  christos 	PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
    458  1.1  christos 	memcpy(ptr, &nvphdr, sizeof(nvphdr));
    459  1.1  christos 	ptr += sizeof(nvphdr);
    460  1.1  christos 	*leftp -= sizeof(nvphdr);
    461  1.1  christos 
    462  1.1  christos 	PJDLOG_ASSERT(*leftp >= namesize);
    463  1.1  christos 	memcpy(ptr, name, namesize);
    464  1.1  christos 	ptr += namesize;
    465  1.1  christos 	*leftp -= namesize;
    466  1.1  christos 
    467  1.1  christos 	return (ptr);
    468  1.1  christos }
    469  1.1  christos 
    470  1.1  christos unsigned char *
    471  1.1  christos nvpair_pack_nvlist_array_next(unsigned char *ptr, size_t *leftp)
    472  1.1  christos {
    473  1.1  christos 	struct nvpair_header nvphdr;
    474  1.1  christos 	size_t namesize;
    475  1.1  christos 	const char *name = "";
    476  1.1  christos 
    477  1.1  christos 	namesize = 1;
    478  1.1  christos 	nvphdr.nvph_type = NV_TYPE_NVLIST_ARRAY_NEXT;
    479  1.1  christos 	nvphdr.nvph_namesize = namesize;
    480  1.1  christos 	nvphdr.nvph_datasize = 0;
    481  1.1  christos 	nvphdr.nvph_nitems = 0;
    482  1.1  christos 	PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
    483  1.1  christos 	memcpy(ptr, &nvphdr, sizeof(nvphdr));
    484  1.1  christos 	ptr += sizeof(nvphdr);
    485  1.1  christos 	*leftp -= sizeof(nvphdr);
    486  1.1  christos 
    487  1.1  christos 	PJDLOG_ASSERT(*leftp >= namesize);
    488  1.1  christos 	memcpy(ptr, name, namesize);
    489  1.1  christos 	ptr += namesize;
    490  1.1  christos 	*leftp -= namesize;
    491  1.1  christos 
    492  1.1  christos 	return (ptr);
    493  1.1  christos }
    494  1.1  christos 
    495  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
    496  1.1  christos unsigned char *
    497  1.1  christos nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp,
    498  1.1  christos     size_t *leftp)
    499  1.1  christos {
    500  1.1  christos 	int64_t value;
    501  1.1  christos 
    502  1.1  christos 	NVPAIR_ASSERT(nvp);
    503  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
    504  1.1  christos 
    505  1.1  christos 	value = (int64_t)nvp->nvp_data;
    506  1.1  christos 	if (value != -1) {
    507  1.1  christos 		/*
    508  1.1  christos 		 * If there is a real descriptor here, we change its number
    509  1.1  christos 		 * to position in the array of descriptors send via control
    510  1.1  christos 		 * message.
    511  1.1  christos 		 */
    512  1.1  christos 		PJDLOG_ASSERT(fdidxp != NULL);
    513  1.1  christos 
    514  1.1  christos 		value = *fdidxp;
    515  1.1  christos 		(*fdidxp)++;
    516  1.1  christos 	}
    517  1.1  christos 
    518  1.1  christos 	PJDLOG_ASSERT(*leftp >= sizeof(value));
    519  1.1  christos 	memcpy(ptr, &value, sizeof(value));
    520  1.1  christos 	ptr += sizeof(value);
    521  1.1  christos 	*leftp -= sizeof(value);
    522  1.1  christos 
    523  1.1  christos 	return (ptr);
    524  1.1  christos }
    525  1.1  christos #endif
    526  1.1  christos 
    527  1.1  christos unsigned char *
    528  1.1  christos nvpair_pack_binary(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    529  1.1  christos {
    530  1.1  christos 
    531  1.1  christos 	NVPAIR_ASSERT(nvp);
    532  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
    533  1.1  christos 
    534  1.1  christos 	PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
    535  1.1  christos 	memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
    536  1.1  christos 	ptr += nvp->nvp_datasize;
    537  1.1  christos 	*leftp -= nvp->nvp_datasize;
    538  1.1  christos 
    539  1.1  christos 	return (ptr);
    540  1.1  christos }
    541  1.1  christos 
    542  1.1  christos unsigned char *
    543  1.1  christos nvpair_pack_bool_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    544  1.1  christos {
    545  1.1  christos 
    546  1.1  christos 	NVPAIR_ASSERT(nvp);
    547  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
    548  1.1  christos 	PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
    549  1.1  christos 
    550  1.1  christos 	memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
    551  1.1  christos 	ptr += nvp->nvp_datasize;
    552  1.1  christos 	*leftp -= nvp->nvp_datasize;
    553  1.1  christos 
    554  1.1  christos 	return (ptr);
    555  1.1  christos }
    556  1.1  christos 
    557  1.1  christos unsigned char *
    558  1.1  christos nvpair_pack_number_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    559  1.1  christos {
    560  1.1  christos 
    561  1.1  christos 	NVPAIR_ASSERT(nvp);
    562  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
    563  1.1  christos 	PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
    564  1.1  christos 
    565  1.1  christos 	memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
    566  1.1  christos 	ptr += nvp->nvp_datasize;
    567  1.1  christos 	*leftp -= nvp->nvp_datasize;
    568  1.1  christos 
    569  1.1  christos 	return (ptr);
    570  1.1  christos }
    571  1.1  christos 
    572  1.1  christos unsigned char *
    573  1.1  christos nvpair_pack_string_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
    574  1.1  christos {
    575  1.1  christos 	unsigned int ii;
    576  1.1  christos 	size_t size, len;
    577  1.1  christos 	const char * const *array;
    578  1.1  christos 
    579  1.1  christos 	NVPAIR_ASSERT(nvp);
    580  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
    581  1.1  christos 	PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
    582  1.1  christos 
    583  1.1  christos 	size = 0;
    584  1.1  christos 	array = nvpair_get_string_array(nvp, NULL);
    585  1.1  christos 	PJDLOG_ASSERT(array != NULL);
    586  1.1  christos 
    587  1.1  christos 	for (ii = 0; ii < nvp->nvp_nitems; ii++) {
    588  1.1  christos 		len = strlen(array[ii]) + 1;
    589  1.1  christos 		PJDLOG_ASSERT(*leftp >= len);
    590  1.1  christos 
    591  1.1  christos 		memcpy(ptr, (const void *)array[ii], len);
    592  1.1  christos 		size += len;
    593  1.1  christos 		ptr += len;
    594  1.1  christos 		*leftp -= len;
    595  1.1  christos 	}
    596  1.1  christos 
    597  1.1  christos 	PJDLOG_ASSERT(size == nvp->nvp_datasize);
    598  1.1  christos 
    599  1.1  christos 	return (ptr);
    600  1.1  christos }
    601  1.1  christos 
    602  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
    603  1.1  christos unsigned char *
    604  1.1  christos nvpair_pack_descriptor_array(const nvpair_t *nvp, unsigned char *ptr,
    605  1.1  christos     int64_t *fdidxp, size_t *leftp)
    606  1.1  christos {
    607  1.1  christos 	int64_t value;
    608  1.1  christos 	const int *array;
    609  1.1  christos 	unsigned int ii;
    610  1.1  christos 
    611  1.1  christos 	NVPAIR_ASSERT(nvp);
    612  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
    613  1.1  christos 	PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
    614  1.1  christos 
    615  1.1  christos 	array = nvpair_get_descriptor_array(nvp, NULL);
    616  1.1  christos 	PJDLOG_ASSERT(array != NULL);
    617  1.1  christos 
    618  1.1  christos 	for (ii = 0; ii < nvp->nvp_nitems; ii++) {
    619  1.1  christos 		PJDLOG_ASSERT(*leftp >= sizeof(value));
    620  1.1  christos 
    621  1.1  christos 		value = array[ii];
    622  1.1  christos 		if (value != -1) {
    623  1.1  christos 			/*
    624  1.1  christos 			 * If there is a real descriptor here, we change its
    625  1.1  christos 			 * number to position in the array of descriptors send
    626  1.1  christos 			 * via control message.
    627  1.1  christos 			 */
    628  1.1  christos 			PJDLOG_ASSERT(fdidxp != NULL);
    629  1.1  christos 
    630  1.1  christos 			value = *fdidxp;
    631  1.1  christos 			(*fdidxp)++;
    632  1.1  christos 		}
    633  1.1  christos 		memcpy(ptr, &value, sizeof(value));
    634  1.1  christos 		ptr += sizeof(value);
    635  1.1  christos 		*leftp -= sizeof(value);
    636  1.1  christos 	}
    637  1.1  christos 
    638  1.1  christos 	return (ptr);
    639  1.1  christos }
    640  1.1  christos #endif
    641  1.1  christos 
    642  1.1  christos void
    643  1.1  christos nvpair_init_datasize(nvpair_t *nvp)
    644  1.1  christos {
    645  1.1  christos 
    646  1.1  christos 	NVPAIR_ASSERT(nvp);
    647  1.1  christos 
    648  1.1  christos 	if (nvp->nvp_type == NV_TYPE_NVLIST) {
    649  1.1  christos 		if (nvp->nvp_data == 0) {
    650  1.1  christos 			nvp->nvp_datasize = 0;
    651  1.1  christos 		} else {
    652  1.1  christos 			nvp->nvp_datasize =
    653  1.1  christos 			    nvlist_size((const nvlist_t *)(intptr_t)nvp->nvp_data);
    654  1.1  christos 		}
    655  1.1  christos 	}
    656  1.1  christos }
    657  1.1  christos 
    658  1.1  christos const unsigned char *
    659  1.1  christos nvpair_unpack_header(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
    660  1.1  christos     size_t *leftp)
    661  1.1  christos {
    662  1.1  christos 	struct nvpair_header nvphdr;
    663  1.1  christos 
    664  1.1  christos 	if (*leftp < sizeof(nvphdr))
    665  1.1  christos 		goto fail;
    666  1.1  christos 
    667  1.1  christos 	memcpy(&nvphdr, ptr, sizeof(nvphdr));
    668  1.1  christos 	ptr += sizeof(nvphdr);
    669  1.1  christos 	*leftp -= sizeof(nvphdr);
    670  1.1  christos 
    671  1.1  christos #if NV_TYPE_FIRST > 0
    672  1.1  christos 	if (nvphdr.nvph_type < NV_TYPE_FIRST)
    673  1.1  christos 		goto fail;
    674  1.1  christos #endif
    675  1.1  christos 	if (nvphdr.nvph_type > NV_TYPE_LAST &&
    676  1.1  christos 	    nvphdr.nvph_type != NV_TYPE_NVLIST_UP &&
    677  1.1  christos 	    nvphdr.nvph_type != NV_TYPE_NVLIST_ARRAY_NEXT) {
    678  1.1  christos 		goto fail;
    679  1.1  christos 	}
    680  1.1  christos 
    681  1.1  christos #if BYTE_ORDER == BIG_ENDIAN
    682  1.1  christos 	if (!isbe) {
    683  1.1  christos 		nvphdr.nvph_namesize = le16toh(nvphdr.nvph_namesize);
    684  1.1  christos 		nvphdr.nvph_datasize = le64toh(nvphdr.nvph_datasize);
    685  1.1  christos 	}
    686  1.1  christos #else
    687  1.1  christos 	if (isbe) {
    688  1.1  christos 		nvphdr.nvph_namesize = be16toh(nvphdr.nvph_namesize);
    689  1.1  christos 		nvphdr.nvph_datasize = be64toh(nvphdr.nvph_datasize);
    690  1.1  christos 	}
    691  1.1  christos #endif
    692  1.1  christos 
    693  1.1  christos 	if (nvphdr.nvph_namesize > NV_NAME_MAX)
    694  1.1  christos 		goto fail;
    695  1.1  christos 	if (*leftp < nvphdr.nvph_namesize)
    696  1.1  christos 		goto fail;
    697  1.1  christos 	if (nvphdr.nvph_namesize < 1)
    698  1.1  christos 		goto fail;
    699  1.1  christos 	if (strnlen((const char *)ptr, nvphdr.nvph_namesize) !=
    700  1.1  christos 	    (size_t)(nvphdr.nvph_namesize - 1)) {
    701  1.1  christos 		goto fail;
    702  1.1  christos 	}
    703  1.1  christos 
    704  1.1  christos 	memcpy(nvp->nvp_name, ptr, nvphdr.nvph_namesize);
    705  1.1  christos 	ptr += nvphdr.nvph_namesize;
    706  1.1  christos 	*leftp -= nvphdr.nvph_namesize;
    707  1.1  christos 
    708  1.1  christos 	if (*leftp < nvphdr.nvph_datasize)
    709  1.1  christos 		goto fail;
    710  1.1  christos 
    711  1.1  christos 	nvp->nvp_type = nvphdr.nvph_type;
    712  1.1  christos 	nvp->nvp_data = 0;
    713  1.1  christos 	nvp->nvp_datasize = nvphdr.nvph_datasize;
    714  1.1  christos 	nvp->nvp_nitems = nvphdr.nvph_nitems;
    715  1.1  christos 
    716  1.1  christos 	return (ptr);
    717  1.1  christos fail:
    718  1.1  christos 	ERRNO_SET(EINVAL);
    719  1.1  christos 	return (NULL);
    720  1.1  christos }
    721  1.1  christos 
    722  1.1  christos const unsigned char *
    723  1.1  christos nvpair_unpack_null(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
    724  1.1  christos     size_t *leftp __unused)
    725  1.1  christos {
    726  1.1  christos 
    727  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL);
    728  1.1  christos 
    729  1.1  christos 	if (nvp->nvp_datasize != 0) {
    730  1.1  christos 		ERRNO_SET(EINVAL);
    731  1.1  christos 		return (NULL);
    732  1.1  christos 	}
    733  1.1  christos 
    734  1.1  christos 	return (ptr);
    735  1.1  christos }
    736  1.1  christos 
    737  1.1  christos const unsigned char *
    738  1.1  christos nvpair_unpack_bool(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
    739  1.1  christos     size_t *leftp)
    740  1.1  christos {
    741  1.1  christos 	uint8_t value;
    742  1.1  christos 
    743  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL);
    744  1.1  christos 
    745  1.1  christos 	if (nvp->nvp_datasize != sizeof(value)) {
    746  1.1  christos 		ERRNO_SET(EINVAL);
    747  1.1  christos 		return (NULL);
    748  1.1  christos 	}
    749  1.1  christos 	if (*leftp < sizeof(value)) {
    750  1.1  christos 		ERRNO_SET(EINVAL);
    751  1.1  christos 		return (NULL);
    752  1.1  christos 	}
    753  1.1  christos 
    754  1.1  christos 	memcpy(&value, ptr, sizeof(value));
    755  1.1  christos 	ptr += sizeof(value);
    756  1.1  christos 	*leftp -= sizeof(value);
    757  1.1  christos 
    758  1.1  christos 	if (value != 0 && value != 1) {
    759  1.1  christos 		ERRNO_SET(EINVAL);
    760  1.1  christos 		return (NULL);
    761  1.1  christos 	}
    762  1.1  christos 
    763  1.1  christos 	nvp->nvp_data = (uint64_t)value;
    764  1.1  christos 
    765  1.1  christos 	return (ptr);
    766  1.1  christos }
    767  1.1  christos 
    768  1.1  christos const unsigned char *
    769  1.1  christos nvpair_unpack_number(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
    770  1.1  christos      size_t *leftp)
    771  1.1  christos {
    772  1.1  christos 
    773  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER);
    774  1.1  christos 
    775  1.1  christos 	if (nvp->nvp_datasize != sizeof(uint64_t)) {
    776  1.1  christos 		ERRNO_SET(EINVAL);
    777  1.1  christos 		return (NULL);
    778  1.1  christos 	}
    779  1.1  christos 	if (*leftp < sizeof(uint64_t)) {
    780  1.1  christos 		ERRNO_SET(EINVAL);
    781  1.1  christos 		return (NULL);
    782  1.1  christos 	}
    783  1.1  christos 
    784  1.1  christos 	if (isbe)
    785  1.1  christos 		nvp->nvp_data = be64dec(ptr);
    786  1.1  christos 	else
    787  1.1  christos 		nvp->nvp_data = le64dec(ptr);
    788  1.1  christos 
    789  1.1  christos 	ptr += sizeof(uint64_t);
    790  1.1  christos 	*leftp -= sizeof(uint64_t);
    791  1.1  christos 
    792  1.1  christos 	return (ptr);
    793  1.1  christos }
    794  1.1  christos 
    795  1.1  christos const unsigned char *
    796  1.1  christos nvpair_unpack_string(bool isbe __unused, nvpair_t *nvp,
    797  1.1  christos     const unsigned char *ptr, size_t *leftp)
    798  1.1  christos {
    799  1.1  christos 
    800  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
    801  1.1  christos 
    802  1.1  christos 	if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
    803  1.1  christos 		ERRNO_SET(EINVAL);
    804  1.1  christos 		return (NULL);
    805  1.1  christos 	}
    806  1.1  christos 
    807  1.1  christos 	if (strnlen((const char *)ptr, nvp->nvp_datasize) !=
    808  1.1  christos 	    nvp->nvp_datasize - 1) {
    809  1.1  christos 		ERRNO_SET(EINVAL);
    810  1.1  christos 		return (NULL);
    811  1.1  christos 	}
    812  1.1  christos 
    813  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)nv_strdup((const char *)ptr);
    814  1.1  christos 	if (nvp->nvp_data == 0)
    815  1.1  christos 		return (NULL);
    816  1.1  christos 
    817  1.1  christos 	ptr += nvp->nvp_datasize;
    818  1.1  christos 	*leftp -= nvp->nvp_datasize;
    819  1.1  christos 
    820  1.1  christos 	return (ptr);
    821  1.1  christos }
    822  1.1  christos 
    823  1.1  christos const unsigned char *
    824  1.1  christos nvpair_unpack_nvlist(bool isbe __unused, nvpair_t *nvp,
    825  1.1  christos     const unsigned char *ptr, size_t *leftp, size_t nfds, nvlist_t **child)
    826  1.1  christos {
    827  1.1  christos 	nvlist_t *value;
    828  1.1  christos 
    829  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
    830  1.1  christos 
    831  1.1  christos 	if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
    832  1.1  christos 		ERRNO_SET(EINVAL);
    833  1.1  christos 		return (NULL);
    834  1.1  christos 	}
    835  1.1  christos 
    836  1.1  christos 	value = nvlist_create(0);
    837  1.1  christos 	if (value == NULL)
    838  1.1  christos 		return (NULL);
    839  1.1  christos 
    840  1.1  christos 	ptr = nvlist_unpack_header(value, ptr, nfds, NULL, leftp);
    841  1.1  christos 	if (ptr == NULL)
    842  1.1  christos 		return (NULL);
    843  1.1  christos 
    844  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)value;
    845  1.1  christos 	*child = value;
    846  1.1  christos 
    847  1.1  christos 	return (ptr);
    848  1.1  christos }
    849  1.1  christos 
    850  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
    851  1.1  christos const unsigned char *
    852  1.1  christos nvpair_unpack_descriptor(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
    853  1.1  christos     size_t *leftp, const int *fds, size_t nfds)
    854  1.1  christos {
    855  1.1  christos 	int64_t idx;
    856  1.1  christos 
    857  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
    858  1.1  christos 
    859  1.1  christos 	if (nvp->nvp_datasize != sizeof(idx)) {
    860  1.1  christos 		ERRNO_SET(EINVAL);
    861  1.1  christos 		return (NULL);
    862  1.1  christos 	}
    863  1.1  christos 	if (*leftp < sizeof(idx)) {
    864  1.1  christos 		ERRNO_SET(EINVAL);
    865  1.1  christos 		return (NULL);
    866  1.1  christos 	}
    867  1.1  christos 
    868  1.1  christos 	if (isbe)
    869  1.1  christos 		idx = be64dec(ptr);
    870  1.1  christos 	else
    871  1.1  christos 		idx = le64dec(ptr);
    872  1.1  christos 
    873  1.1  christos 	if (idx < 0) {
    874  1.1  christos 		ERRNO_SET(EINVAL);
    875  1.1  christos 		return (NULL);
    876  1.1  christos 	}
    877  1.1  christos 
    878  1.1  christos 	if ((size_t)idx >= nfds) {
    879  1.1  christos 		ERRNO_SET(EINVAL);
    880  1.1  christos 		return (NULL);
    881  1.1  christos 	}
    882  1.1  christos 
    883  1.1  christos 	nvp->nvp_data = (uint64_t)fds[idx];
    884  1.1  christos 
    885  1.1  christos 	ptr += sizeof(idx);
    886  1.1  christos 	*leftp -= sizeof(idx);
    887  1.1  christos 
    888  1.1  christos 	return (ptr);
    889  1.1  christos }
    890  1.1  christos #endif
    891  1.1  christos 
    892  1.1  christos const unsigned char *
    893  1.1  christos nvpair_unpack_binary(bool isbe __unused, nvpair_t *nvp,
    894  1.1  christos     const unsigned char *ptr, size_t *leftp)
    895  1.1  christos {
    896  1.1  christos 	void *value;
    897  1.1  christos 
    898  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
    899  1.1  christos 
    900  1.1  christos 	if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
    901  1.1  christos 		ERRNO_SET(EINVAL);
    902  1.1  christos 		return (NULL);
    903  1.1  christos 	}
    904  1.1  christos 
    905  1.1  christos 	value = nv_malloc(nvp->nvp_datasize);
    906  1.1  christos 	if (value == NULL)
    907  1.1  christos 		return (NULL);
    908  1.1  christos 
    909  1.1  christos 	memcpy(value, ptr, nvp->nvp_datasize);
    910  1.1  christos 	ptr += nvp->nvp_datasize;
    911  1.1  christos 	*leftp -= nvp->nvp_datasize;
    912  1.1  christos 
    913  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)value;
    914  1.1  christos 
    915  1.1  christos 	return (ptr);
    916  1.1  christos }
    917  1.1  christos 
    918  1.1  christos const unsigned char *
    919  1.1  christos nvpair_unpack_bool_array(bool isbe __unused, nvpair_t *nvp,
    920  1.1  christos     const unsigned char *ptr, size_t *leftp)
    921  1.1  christos {
    922  1.1  christos 	uint8_t *value;
    923  1.1  christos 	size_t size;
    924  1.1  christos 	unsigned int i;
    925  1.1  christos 
    926  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
    927  1.1  christos 
    928  1.1  christos 	size = sizeof(*value) * nvp->nvp_nitems;
    929  1.1  christos 	if (nvp->nvp_datasize != size || *leftp < size ||
    930  1.1  christos 	    nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
    931  1.1  christos 		ERRNO_SET(EINVAL);
    932  1.1  christos 		return (NULL);
    933  1.1  christos 	}
    934  1.1  christos 
    935  1.1  christos 	value = nv_malloc(size);
    936  1.1  christos 	if (value == NULL)
    937  1.1  christos 		return (NULL);
    938  1.1  christos 
    939  1.1  christos 	for (i = 0; i < nvp->nvp_nitems; i++) {
    940  1.1  christos 		value[i] = *(const uint8_t *)ptr;
    941  1.1  christos 
    942  1.1  christos 		ptr += sizeof(*value);
    943  1.1  christos 		*leftp -= sizeof(*value);
    944  1.1  christos 	}
    945  1.1  christos 
    946  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)value;
    947  1.1  christos 
    948  1.1  christos 	return (ptr);
    949  1.1  christos }
    950  1.1  christos 
    951  1.1  christos const unsigned char *
    952  1.1  christos nvpair_unpack_number_array(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
    953  1.1  christos      size_t *leftp)
    954  1.1  christos {
    955  1.1  christos 	uint64_t *value;
    956  1.1  christos 	size_t size;
    957  1.1  christos 	unsigned int i;
    958  1.1  christos 
    959  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
    960  1.1  christos 
    961  1.1  christos 	size = sizeof(*value) * nvp->nvp_nitems;
    962  1.1  christos 	if (nvp->nvp_datasize != size || *leftp < size ||
    963  1.1  christos 	    nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
    964  1.1  christos 		ERRNO_SET(EINVAL);
    965  1.1  christos 		return (NULL);
    966  1.1  christos 	}
    967  1.1  christos 
    968  1.1  christos 	value = nv_malloc(size);
    969  1.1  christos 	if (value == NULL)
    970  1.1  christos 		return (NULL);
    971  1.1  christos 
    972  1.1  christos 	for (i = 0; i < nvp->nvp_nitems; i++) {
    973  1.1  christos 		if (isbe)
    974  1.1  christos 			value[i] = be64dec(ptr);
    975  1.1  christos 		else
    976  1.1  christos 			value[i] = le64dec(ptr);
    977  1.1  christos 
    978  1.1  christos 		ptr += sizeof(*value);
    979  1.1  christos 		*leftp -= sizeof(*value);
    980  1.1  christos 	}
    981  1.1  christos 
    982  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)value;
    983  1.1  christos 
    984  1.1  christos 	return (ptr);
    985  1.1  christos }
    986  1.1  christos 
    987  1.1  christos const unsigned char *
    988  1.1  christos nvpair_unpack_string_array(bool isbe __unused, nvpair_t *nvp,
    989  1.1  christos     const unsigned char *ptr, size_t *leftp)
    990  1.1  christos {
    991  1.1  christos 	ssize_t size;
    992  1.1  christos 	size_t len;
    993  1.1  christos 	const char *tmp;
    994  1.1  christos 	char **value;
    995  1.1  christos 	unsigned int ii, j;
    996  1.1  christos 
    997  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
    998  1.1  christos 
    999  1.1  christos 	if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0 ||
   1000  1.1  christos 	    nvp->nvp_nitems == 0) {
   1001  1.1  christos 		ERRNO_SET(EINVAL);
   1002  1.1  christos 		return (NULL);
   1003  1.1  christos 	}
   1004  1.1  christos 
   1005  1.1  christos 	size = nvp->nvp_datasize;
   1006  1.1  christos 	tmp = (const char *)ptr;
   1007  1.1  christos 	for (ii = 0; ii < nvp->nvp_nitems; ii++) {
   1008  1.1  christos 		len = strnlen(tmp, size - 1) + 1;
   1009  1.1  christos 		size -= len;
   1010  1.1  christos 		if (size < 0) {
   1011  1.1  christos 			ERRNO_SET(EINVAL);
   1012  1.1  christos 			return (NULL);
   1013  1.1  christos 		}
   1014  1.1  christos 		tmp += len;
   1015  1.1  christos 	}
   1016  1.1  christos 	if (size != 0) {
   1017  1.1  christos 		ERRNO_SET(EINVAL);
   1018  1.1  christos 		return (NULL);
   1019  1.1  christos 	}
   1020  1.1  christos 
   1021  1.1  christos 	value = nv_malloc(sizeof(*value) * nvp->nvp_nitems);
   1022  1.1  christos 	if (value == NULL)
   1023  1.1  christos 		return (NULL);
   1024  1.1  christos 
   1025  1.1  christos 	for (ii = 0; ii < nvp->nvp_nitems; ii++) {
   1026  1.1  christos 		value[ii] = nv_strdup((const char *)ptr);
   1027  1.1  christos 		if (value[ii] == NULL)
   1028  1.1  christos 			goto out;
   1029  1.1  christos 		len = strlen(value[ii]) + 1;
   1030  1.1  christos 		ptr += len;
   1031  1.1  christos 		*leftp -= len;
   1032  1.1  christos 	}
   1033  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)value;
   1034  1.1  christos 
   1035  1.1  christos 	return (ptr);
   1036  1.1  christos out:
   1037  1.1  christos 	for (j = 0; j < ii; j++)
   1038  1.1  christos 		nv_free(value[j]);
   1039  1.1  christos 	nv_free(value);
   1040  1.1  christos 	return (NULL);
   1041  1.1  christos }
   1042  1.1  christos 
   1043  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE) && !defined(__NetBSD__)
   1044  1.1  christos const unsigned char *
   1045  1.1  christos nvpair_unpack_descriptor_array(bool isbe, nvpair_t *nvp,
   1046  1.1  christos     const unsigned char *ptr, size_t *leftp, const int *fds, size_t nfds)
   1047  1.1  christos {
   1048  1.1  christos 	int64_t idx;
   1049  1.1  christos 	size_t size;
   1050  1.1  christos 	unsigned int ii;
   1051  1.1  christos 	int *array;
   1052  1.1  christos 
   1053  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
   1054  1.1  christos 
   1055  1.1  christos 	size = sizeof(idx) * nvp->nvp_nitems;
   1056  1.1  christos 	if (nvp->nvp_datasize != size || *leftp < size ||
   1057  1.1  christos 	    nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
   1058  1.1  christos 		ERRNO_SET(EINVAL);
   1059  1.1  christos 		return (NULL);
   1060  1.1  christos 	}
   1061  1.1  christos 
   1062  1.1  christos 	array = (int *)nv_malloc(size);
   1063  1.1  christos 	if (array == NULL)
   1064  1.1  christos 		return (NULL);
   1065  1.1  christos 
   1066  1.1  christos 	for (ii = 0; ii < nvp->nvp_nitems; ii++) {
   1067  1.1  christos 		if (isbe)
   1068  1.1  christos 			idx = be64dec(ptr);
   1069  1.1  christos 		else
   1070  1.1  christos 			idx = le64dec(ptr);
   1071  1.1  christos 
   1072  1.1  christos 		if (idx < 0) {
   1073  1.1  christos 			ERRNO_SET(EINVAL);
   1074  1.1  christos 			nv_free(array);
   1075  1.1  christos 			return (NULL);
   1076  1.1  christos 		}
   1077  1.1  christos 
   1078  1.1  christos 		if ((size_t)idx >= nfds) {
   1079  1.1  christos 			ERRNO_SET(EINVAL);
   1080  1.1  christos 			nv_free(array);
   1081  1.1  christos 			return (NULL);
   1082  1.1  christos 		}
   1083  1.1  christos 
   1084  1.1  christos 		array[ii] = (uint64_t)fds[idx];
   1085  1.1  christos 
   1086  1.1  christos 		ptr += sizeof(idx);
   1087  1.1  christos 		*leftp -= sizeof(idx);
   1088  1.1  christos 	}
   1089  1.1  christos 
   1090  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)array;
   1091  1.1  christos 
   1092  1.1  christos 	return (ptr);
   1093  1.1  christos }
   1094  1.1  christos #endif
   1095  1.1  christos 
   1096  1.1  christos const unsigned char *
   1097  1.1  christos nvpair_unpack_nvlist_array(bool isbe __unused, nvpair_t *nvp,
   1098  1.1  christos     const unsigned char *ptr, size_t *leftp, nvlist_t **firstel)
   1099  1.1  christos {
   1100  1.1  christos 	nvlist_t **value;
   1101  1.1  christos 	nvpair_t *tmpnvp;
   1102  1.1  christos 	unsigned int ii, j;
   1103  1.1  christos 	size_t sizeup;
   1104  1.1  christos 
   1105  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
   1106  1.1  christos 
   1107  1.1  christos 	sizeup = sizeof(struct nvpair_header) * nvp->nvp_nitems;
   1108  1.1  christos 	if (nvp->nvp_nitems == 0 || sizeup < nvp->nvp_nitems ||
   1109  1.1  christos 	    sizeup > *leftp) {
   1110  1.1  christos 		ERRNO_SET(EINVAL);
   1111  1.1  christos 		return (NULL);
   1112  1.1  christos 	}
   1113  1.1  christos 
   1114  1.1  christos 	value = nv_malloc(nvp->nvp_nitems * sizeof(*value));
   1115  1.1  christos 	if (value == NULL)
   1116  1.1  christos 		return (NULL);
   1117  1.1  christos 
   1118  1.1  christos 	for (ii = 0; ii < nvp->nvp_nitems; ii++) {
   1119  1.1  christos 		value[ii] = nvlist_create(0);
   1120  1.1  christos 		if (value[ii] == NULL)
   1121  1.1  christos 			goto fail;
   1122  1.1  christos 		if (ii > 0) {
   1123  1.1  christos 			tmpnvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
   1124  1.1  christos 			    (uint64_t)(uintptr_t)value[ii], 0, 0);
   1125  1.1  christos 			if (tmpnvp == NULL)
   1126  1.1  christos 				goto fail;
   1127  1.1  christos 			nvlist_set_array_next(value[ii - 1], tmpnvp);
   1128  1.1  christos 		}
   1129  1.1  christos 	}
   1130  1.1  christos 	nvlist_set_flags(value[nvp->nvp_nitems - 1], NV_FLAG_IN_ARRAY);
   1131  1.1  christos 
   1132  1.1  christos 	nvp->nvp_data = (uint64_t)(uintptr_t)value;
   1133  1.1  christos 	*firstel = value[0];
   1134  1.1  christos 
   1135  1.1  christos 	return (ptr);
   1136  1.1  christos fail:
   1137  1.1  christos 	ERRNO_SAVE();
   1138  1.1  christos 	for (j = 0; j <= ii; j++)
   1139  1.1  christos 		nvlist_destroy(value[j]);
   1140  1.1  christos 	nv_free(value);
   1141  1.1  christos 	ERRNO_RESTORE();
   1142  1.1  christos 
   1143  1.1  christos 	return (NULL);
   1144  1.1  christos }
   1145  1.1  christos 
   1146  1.1  christos const unsigned char *
   1147  1.1  christos nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
   1148  1.1  christos     nvpair_t **nvpp)
   1149  1.1  christos {
   1150  1.1  christos 	nvpair_t *nvp, *tmp;
   1151  1.1  christos 
   1152  1.1  christos 	nvp = nv_calloc(1, sizeof(*nvp) + NV_NAME_MAX);
   1153  1.1  christos 	if (nvp == NULL)
   1154  1.1  christos 		return (NULL);
   1155  1.1  christos 	nvp->nvp_name = (char *)(nvp + 1);
   1156  1.1  christos 
   1157  1.1  christos 	ptr = nvpair_unpack_header(isbe, nvp, ptr, leftp);
   1158  1.1  christos 	if (ptr == NULL)
   1159  1.1  christos 		goto fail;
   1160  1.1  christos 	tmp = nv_realloc(nvp, sizeof(*nvp) + strlen(nvp->nvp_name) + 1);
   1161  1.1  christos 	if (tmp == NULL)
   1162  1.1  christos 		goto fail;
   1163  1.1  christos 	nvp = tmp;
   1164  1.1  christos 
   1165  1.1  christos 	/* Update nvp_name after realloc(). */
   1166  1.1  christos 	nvp->nvp_name = (char *)(nvp + 1);
   1167  1.1  christos 	nvp->nvp_data = 0x00;
   1168  1.1  christos 	nvp->nvp_magic = NVPAIR_MAGIC;
   1169  1.1  christos 	*nvpp = nvp;
   1170  1.1  christos 	return (ptr);
   1171  1.1  christos fail:
   1172  1.1  christos 	nv_free(nvp);
   1173  1.1  christos 	return (NULL);
   1174  1.1  christos }
   1175  1.1  christos 
   1176  1.1  christos int
   1177  1.1  christos nvpair_type(const nvpair_t *nvp)
   1178  1.1  christos {
   1179  1.1  christos 
   1180  1.1  christos 	NVPAIR_ASSERT(nvp);
   1181  1.1  christos 
   1182  1.1  christos 	return (nvp->nvp_type);
   1183  1.1  christos }
   1184  1.1  christos 
   1185  1.1  christos const char *
   1186  1.1  christos nvpair_name(const nvpair_t *nvp)
   1187  1.1  christos {
   1188  1.1  christos 
   1189  1.1  christos 	NVPAIR_ASSERT(nvp);
   1190  1.1  christos 
   1191  1.1  christos 	return (nvp->nvp_name);
   1192  1.1  christos }
   1193  1.1  christos 
   1194  1.3  christos #if !defined(_KERNEL) && !defined(_STANDALONE) && !defined(__NetBSD__)
   1195  1.1  christos nvpair_t *
   1196  1.1  christos nvpair_create_stringf(const char *name, const char *valuefmt, ...)
   1197  1.1  christos {
   1198  1.1  christos 	va_list valueap;
   1199  1.1  christos 	nvpair_t *nvp;
   1200  1.1  christos 
   1201  1.1  christos 	va_start(valueap, valuefmt);
   1202  1.1  christos 	nvp = nvpair_create_stringv(name, valuefmt, valueap);
   1203  1.1  christos 	va_end(valueap);
   1204  1.1  christos 
   1205  1.1  christos 	return (nvp);
   1206  1.1  christos }
   1207  1.1  christos 
   1208  1.1  christos nvpair_t *
   1209  1.1  christos nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap)
   1210  1.1  christos {
   1211  1.1  christos 	nvpair_t *nvp;
   1212  1.1  christos 	char *str;
   1213  1.1  christos 	int len;
   1214  1.1  christos 
   1215  1.1  christos 	len = nv_vasprintf(&str, valuefmt, valueap);
   1216  1.1  christos 	if (len < 0)
   1217  1.1  christos 		return (NULL);
   1218  1.1  christos 	nvp = nvpair_create_string(name, str);
   1219  1.4     rmind 	nv_free(str);
   1220  1.1  christos 	return (nvp);
   1221  1.1  christos }
   1222  1.3  christos #endif
   1223  1.1  christos 
   1224  1.1  christos nvpair_t *
   1225  1.1  christos nvpair_create_null(const char *name)
   1226  1.1  christos {
   1227  1.1  christos 
   1228  1.1  christos 	return (nvpair_allocv(name, NV_TYPE_NULL, 0, 0, 0));
   1229  1.1  christos }
   1230  1.1  christos 
   1231  1.1  christos nvpair_t *
   1232  1.1  christos nvpair_create_bool(const char *name, bool value)
   1233  1.1  christos {
   1234  1.1  christos 
   1235  1.1  christos 	return (nvpair_allocv(name, NV_TYPE_BOOL, value ? 1 : 0,
   1236  1.1  christos 	    sizeof(uint8_t), 0));
   1237  1.1  christos }
   1238  1.1  christos 
   1239  1.1  christos nvpair_t *
   1240  1.1  christos nvpair_create_number(const char *name, uint64_t value)
   1241  1.1  christos {
   1242  1.1  christos 
   1243  1.1  christos 	return (nvpair_allocv(name, NV_TYPE_NUMBER, value, sizeof(value), 0));
   1244  1.1  christos }
   1245  1.1  christos 
   1246  1.1  christos nvpair_t *
   1247  1.1  christos nvpair_create_string(const char *name, const char *value)
   1248  1.1  christos {
   1249  1.1  christos 	nvpair_t *nvp;
   1250  1.1  christos 	size_t size;
   1251  1.1  christos 	char *data;
   1252  1.1  christos 
   1253  1.1  christos 	if (value == NULL) {
   1254  1.1  christos 		ERRNO_SET(EINVAL);
   1255  1.1  christos 		return (NULL);
   1256  1.1  christos 	}
   1257  1.1  christos 
   1258  1.1  christos 	data = nv_strdup(value);
   1259  1.1  christos 	if (data == NULL)
   1260  1.1  christos 		return (NULL);
   1261  1.1  christos 	size = strlen(value) + 1;
   1262  1.1  christos 
   1263  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)data,
   1264  1.1  christos 	    size, 0);
   1265  1.1  christos 	if (nvp == NULL)
   1266  1.1  christos 		nv_free(data);
   1267  1.1  christos 
   1268  1.1  christos 	return (nvp);
   1269  1.1  christos }
   1270  1.1  christos 
   1271  1.1  christos nvpair_t *
   1272  1.1  christos nvpair_create_nvlist(const char *name, const nvlist_t *value)
   1273  1.1  christos {
   1274  1.1  christos 	nvlist_t *nvl;
   1275  1.1  christos 	nvpair_t *nvp;
   1276  1.1  christos 
   1277  1.1  christos 	if (value == NULL) {
   1278  1.1  christos 		ERRNO_SET(EINVAL);
   1279  1.1  christos 		return (NULL);
   1280  1.1  christos 	}
   1281  1.1  christos 
   1282  1.1  christos 	nvl = nvlist_clone(value);
   1283  1.1  christos 	if (nvl == NULL)
   1284  1.1  christos 		return (NULL);
   1285  1.1  christos 
   1286  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)nvl, 0,
   1287  1.1  christos 	    0);
   1288  1.1  christos 	if (nvp == NULL)
   1289  1.1  christos 		nvlist_destroy(nvl);
   1290  1.1  christos 	else
   1291  1.1  christos 		nvlist_set_parent(nvl, nvp);
   1292  1.1  christos 
   1293  1.1  christos 	return (nvp);
   1294  1.1  christos }
   1295  1.1  christos 
   1296  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   1297  1.1  christos nvpair_t *
   1298  1.1  christos nvpair_create_descriptor(const char *name, int value)
   1299  1.1  christos {
   1300  1.1  christos 	nvpair_t *nvp;
   1301  1.1  christos 
   1302  1.1  christos 	if (value < 0 || !fd_is_valid(value)) {
   1303  1.1  christos 		ERRNO_SET(EBADF);
   1304  1.1  christos 		return (NULL);
   1305  1.1  christos 	}
   1306  1.1  christos 
   1307  1.1  christos 	value = fcntl(value, F_DUPFD_CLOEXEC, 0);
   1308  1.1  christos 	if (value < 0)
   1309  1.1  christos 		return (NULL);
   1310  1.1  christos 
   1311  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
   1312  1.1  christos 	    sizeof(int64_t), 0);
   1313  1.1  christos 	if (nvp == NULL) {
   1314  1.1  christos 		ERRNO_SAVE();
   1315  1.1  christos 		close(value);
   1316  1.1  christos 		ERRNO_RESTORE();
   1317  1.1  christos 	}
   1318  1.1  christos 
   1319  1.1  christos 	return (nvp);
   1320  1.1  christos }
   1321  1.1  christos #endif
   1322  1.1  christos 
   1323  1.1  christos nvpair_t *
   1324  1.1  christos nvpair_create_binary(const char *name, const void *value, size_t size)
   1325  1.1  christos {
   1326  1.1  christos 	nvpair_t *nvp;
   1327  1.1  christos 	void *data;
   1328  1.1  christos 
   1329  1.1  christos 	if (value == NULL || size == 0) {
   1330  1.1  christos 		ERRNO_SET(EINVAL);
   1331  1.1  christos 		return (NULL);
   1332  1.1  christos 	}
   1333  1.1  christos 
   1334  1.1  christos 	data = nv_malloc(size);
   1335  1.1  christos 	if (data == NULL)
   1336  1.1  christos 		return (NULL);
   1337  1.1  christos 	memcpy(data, value, size);
   1338  1.1  christos 
   1339  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)data,
   1340  1.1  christos 	    size, 0);
   1341  1.1  christos 	if (nvp == NULL)
   1342  1.1  christos 		nv_free(data);
   1343  1.1  christos 
   1344  1.1  christos 	return (nvp);
   1345  1.1  christos }
   1346  1.1  christos 
   1347  1.1  christos nvpair_t *
   1348  1.1  christos nvpair_create_bool_array(const char *name, const bool *value, size_t nitems)
   1349  1.1  christos {
   1350  1.1  christos 	nvpair_t *nvp;
   1351  1.1  christos 	size_t size;
   1352  1.1  christos 	void *data;
   1353  1.1  christos 
   1354  1.1  christos 	if (value == NULL || nitems == 0) {
   1355  1.1  christos 		ERRNO_SET(EINVAL);
   1356  1.1  christos 		return (NULL);
   1357  1.1  christos 	}
   1358  1.1  christos 
   1359  1.1  christos 	size = sizeof(value[0]) * nitems;
   1360  1.1  christos 	data = nv_malloc(size);
   1361  1.1  christos 	if (data == NULL)
   1362  1.1  christos 		return (NULL);
   1363  1.1  christos 
   1364  1.1  christos 	memcpy(data, value, size);
   1365  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY, (uint64_t)(uintptr_t)data,
   1366  1.1  christos 	    size, nitems);
   1367  1.1  christos 	if (nvp == NULL) {
   1368  1.1  christos 		ERRNO_SAVE();
   1369  1.1  christos 		nv_free(data);
   1370  1.1  christos 		ERRNO_RESTORE();
   1371  1.1  christos 	}
   1372  1.1  christos 
   1373  1.1  christos 	return (nvp);
   1374  1.1  christos }
   1375  1.1  christos 
   1376  1.1  christos nvpair_t *
   1377  1.1  christos nvpair_create_number_array(const char *name, const uint64_t *value,
   1378  1.1  christos     size_t nitems)
   1379  1.1  christos {
   1380  1.1  christos 	nvpair_t *nvp;
   1381  1.1  christos 	size_t size;
   1382  1.1  christos 	void *data;
   1383  1.1  christos 
   1384  1.1  christos 	if (value == NULL || nitems == 0) {
   1385  1.1  christos 		ERRNO_SET(EINVAL);
   1386  1.1  christos 		return (NULL);
   1387  1.1  christos 	}
   1388  1.1  christos 
   1389  1.1  christos 	size = sizeof(value[0]) * nitems;
   1390  1.1  christos 	data = nv_malloc(size);
   1391  1.1  christos 	if (data == NULL)
   1392  1.1  christos 		return (NULL);
   1393  1.1  christos 
   1394  1.1  christos 	memcpy(data, value, size);
   1395  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
   1396  1.1  christos 	    (uint64_t)(uintptr_t)data, size, nitems);
   1397  1.1  christos 	if (nvp == NULL) {
   1398  1.1  christos 		ERRNO_SAVE();
   1399  1.1  christos 		nv_free(data);
   1400  1.1  christos 		ERRNO_RESTORE();
   1401  1.1  christos 	}
   1402  1.1  christos 
   1403  1.1  christos 	return (nvp);
   1404  1.1  christos }
   1405  1.1  christos 
   1406  1.1  christos nvpair_t *
   1407  1.1  christos nvpair_create_string_array(const char *name, const char * const *value,
   1408  1.1  christos     size_t nitems)
   1409  1.1  christos {
   1410  1.1  christos 	nvpair_t *nvp;
   1411  1.1  christos 	unsigned int ii;
   1412  1.1  christos 	size_t datasize, size;
   1413  1.1  christos 	char **data;
   1414  1.1  christos 
   1415  1.1  christos 	if (value == NULL || nitems == 0) {
   1416  1.1  christos 		ERRNO_SET(EINVAL);
   1417  1.1  christos 		return (NULL);
   1418  1.1  christos 	}
   1419  1.1  christos 
   1420  1.1  christos 	nvp = NULL;
   1421  1.1  christos 	datasize = 0;
   1422  1.1  christos 	data = nv_malloc(sizeof(value[0]) * nitems);
   1423  1.1  christos 	if (data == NULL)
   1424  1.1  christos 		return (NULL);
   1425  1.1  christos 
   1426  1.1  christos 	for (ii = 0; ii < nitems; ii++) {
   1427  1.1  christos 		if (value[ii] == NULL) {
   1428  1.1  christos 			ERRNO_SET(EINVAL);
   1429  1.1  christos 			goto fail;
   1430  1.1  christos 		}
   1431  1.1  christos 
   1432  1.1  christos 		size = strlen(value[ii]) + 1;
   1433  1.1  christos 		datasize += size;
   1434  1.1  christos 		data[ii] = nv_strdup(value[ii]);
   1435  1.1  christos 		if (data[ii] == NULL)
   1436  1.1  christos 			goto fail;
   1437  1.1  christos 	}
   1438  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
   1439  1.1  christos 	    (uint64_t)(uintptr_t)data, datasize, nitems);
   1440  1.1  christos 
   1441  1.1  christos fail:
   1442  1.1  christos 	if (nvp == NULL) {
   1443  1.1  christos 		ERRNO_SAVE();
   1444  1.1  christos 		for (; ii > 0; ii--)
   1445  1.1  christos 			nv_free(data[ii - 1]);
   1446  1.1  christos 		nv_free(data);
   1447  1.1  christos 		ERRNO_RESTORE();
   1448  1.1  christos 	}
   1449  1.1  christos 
   1450  1.1  christos 	return (nvp);
   1451  1.1  christos }
   1452  1.1  christos 
   1453  1.1  christos nvpair_t *
   1454  1.1  christos nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value,
   1455  1.1  christos     size_t nitems)
   1456  1.1  christos {
   1457  1.1  christos 	unsigned int ii;
   1458  1.1  christos 	nvlist_t **nvls;
   1459  1.1  christos 	nvpair_t *parent;
   1460  1.1  christos 	int flags;
   1461  1.1  christos 
   1462  1.1  christos 	nvls = NULL;
   1463  1.1  christos 
   1464  1.1  christos 	if (value == NULL || nitems == 0) {
   1465  1.1  christos 		ERRNO_SET(EINVAL);
   1466  1.1  christos 		return (NULL);
   1467  1.1  christos 	}
   1468  1.1  christos 
   1469  1.1  christos 	nvls = nv_malloc(sizeof(value[0]) * nitems);
   1470  1.1  christos 	if (nvls == NULL)
   1471  1.1  christos 		return (NULL);
   1472  1.1  christos 
   1473  1.1  christos 	for (ii = 0; ii < nitems; ii++) {
   1474  1.1  christos 		if (value[ii] == NULL) {
   1475  1.1  christos 			ERRNO_SET(EINVAL);
   1476  1.1  christos 			goto fail;
   1477  1.1  christos 		}
   1478  1.1  christos 
   1479  1.1  christos 		nvls[ii] = nvlist_clone(value[ii]);
   1480  1.1  christos 		if (nvls[ii] == NULL)
   1481  1.1  christos 			goto fail;
   1482  1.1  christos 
   1483  1.1  christos 		if (ii > 0) {
   1484  1.1  christos 			nvpair_t *nvp;
   1485  1.1  christos 
   1486  1.1  christos 			nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
   1487  1.1  christos 			    (uint64_t)(uintptr_t)nvls[ii], 0, 0);
   1488  1.1  christos 			if (nvp == NULL) {
   1489  1.1  christos 				ERRNO_SAVE();
   1490  1.1  christos 				nvlist_destroy(nvls[ii]);
   1491  1.1  christos 				ERRNO_RESTORE();
   1492  1.1  christos 				goto fail;
   1493  1.1  christos 			}
   1494  1.1  christos 			nvlist_set_array_next(nvls[ii - 1], nvp);
   1495  1.1  christos 		}
   1496  1.1  christos 	}
   1497  1.1  christos 	flags = nvlist_flags(nvls[nitems - 1]) | NV_FLAG_IN_ARRAY;
   1498  1.1  christos 	nvlist_set_flags(nvls[nitems - 1], flags);
   1499  1.1  christos 
   1500  1.1  christos 	parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
   1501  1.1  christos 	    (uint64_t)(uintptr_t)nvls, 0, nitems);
   1502  1.1  christos 	if (parent == NULL)
   1503  1.1  christos 		goto fail;
   1504  1.1  christos 
   1505  1.1  christos 	for (ii = 0; ii < nitems; ii++)
   1506  1.1  christos 		nvlist_set_parent(nvls[ii], parent);
   1507  1.1  christos 
   1508  1.1  christos 	return (parent);
   1509  1.1  christos 
   1510  1.1  christos fail:
   1511  1.1  christos 	ERRNO_SAVE();
   1512  1.1  christos 	for (; ii > 0; ii--)
   1513  1.1  christos 		nvlist_destroy(nvls[ii - 1]);
   1514  1.1  christos 	nv_free(nvls);
   1515  1.1  christos 	ERRNO_RESTORE();
   1516  1.1  christos 
   1517  1.1  christos 	return (NULL);
   1518  1.1  christos }
   1519  1.1  christos 
   1520  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   1521  1.1  christos nvpair_t *
   1522  1.1  christos nvpair_create_descriptor_array(const char *name, const int *value,
   1523  1.1  christos     size_t nitems)
   1524  1.1  christos {
   1525  1.1  christos 	unsigned int ii;
   1526  1.1  christos 	nvpair_t *nvp;
   1527  1.1  christos 	int *fds;
   1528  1.1  christos 
   1529  1.1  christos 	if (value == NULL) {
   1530  1.1  christos 		ERRNO_SET(EINVAL);
   1531  1.1  christos 		return (NULL);
   1532  1.1  christos 	}
   1533  1.1  christos 
   1534  1.1  christos 	nvp = NULL;
   1535  1.1  christos 
   1536  1.1  christos 	fds = nv_malloc(sizeof(value[0]) * nitems);
   1537  1.1  christos 	if (fds == NULL)
   1538  1.1  christos 		return (NULL);
   1539  1.1  christos 	for (ii = 0; ii < nitems; ii++) {
   1540  1.1  christos 		if (value[ii] == -1) {
   1541  1.1  christos 			fds[ii] = -1;
   1542  1.1  christos 		} else {
   1543  1.1  christos 			if (!fd_is_valid(value[ii])) {
   1544  1.1  christos 				ERRNO_SET(EBADF);
   1545  1.1  christos 				goto fail;
   1546  1.1  christos 			}
   1547  1.1  christos 
   1548  1.1  christos 			fds[ii] = fcntl(value[ii], F_DUPFD_CLOEXEC, 0);
   1549  1.1  christos 			if (fds[ii] == -1)
   1550  1.1  christos 				goto fail;
   1551  1.1  christos 		}
   1552  1.1  christos 	}
   1553  1.1  christos 
   1554  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
   1555  1.1  christos 	    (uint64_t)(uintptr_t)fds, sizeof(int64_t) * nitems, nitems);
   1556  1.1  christos 
   1557  1.1  christos fail:
   1558  1.1  christos 	if (nvp == NULL) {
   1559  1.1  christos 		ERRNO_SAVE();
   1560  1.1  christos 		for (; ii > 0; ii--) {
   1561  1.1  christos 			if (fds[ii - 1] != -1)
   1562  1.1  christos 				close(fds[ii - 1]);
   1563  1.1  christos 		}
   1564  1.1  christos 		nv_free(fds);
   1565  1.1  christos 		ERRNO_RESTORE();
   1566  1.1  christos 	}
   1567  1.1  christos 
   1568  1.1  christos 	return (nvp);
   1569  1.1  christos }
   1570  1.1  christos #endif
   1571  1.1  christos 
   1572  1.1  christos nvpair_t *
   1573  1.1  christos nvpair_move_string(const char *name, char *value)
   1574  1.1  christos {
   1575  1.1  christos 	nvpair_t *nvp;
   1576  1.1  christos 
   1577  1.1  christos 	if (value == NULL) {
   1578  1.1  christos 		ERRNO_SET(EINVAL);
   1579  1.1  christos 		return (NULL);
   1580  1.1  christos 	}
   1581  1.1  christos 
   1582  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)value,
   1583  1.1  christos 	    strlen(value) + 1, 0);
   1584  1.1  christos 	if (nvp == NULL) {
   1585  1.1  christos 		ERRNO_SAVE();
   1586  1.1  christos 		nv_free(value);
   1587  1.1  christos 		ERRNO_RESTORE();
   1588  1.1  christos 	}
   1589  1.1  christos 
   1590  1.1  christos 	return (nvp);
   1591  1.1  christos }
   1592  1.1  christos 
   1593  1.1  christos nvpair_t *
   1594  1.1  christos nvpair_move_nvlist(const char *name, nvlist_t *value)
   1595  1.1  christos {
   1596  1.1  christos 	nvpair_t *nvp;
   1597  1.1  christos 
   1598  1.1  christos 	if (value == NULL || nvlist_get_nvpair_parent(value) != NULL) {
   1599  1.1  christos 		ERRNO_SET(EINVAL);
   1600  1.1  christos 		return (NULL);
   1601  1.1  christos 	}
   1602  1.1  christos 
   1603  1.1  christos 	if (nvlist_error(value) != 0) {
   1604  1.1  christos 		ERRNO_SET(nvlist_error(value));
   1605  1.1  christos 		nvlist_destroy(value);
   1606  1.1  christos 		return (NULL);
   1607  1.1  christos 	}
   1608  1.1  christos 
   1609  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)value,
   1610  1.1  christos 	    0, 0);
   1611  1.1  christos 	if (nvp == NULL)
   1612  1.1  christos 		nvlist_destroy(value);
   1613  1.1  christos 	else
   1614  1.1  christos 		nvlist_set_parent(value, nvp);
   1615  1.1  christos 
   1616  1.1  christos 	return (nvp);
   1617  1.1  christos }
   1618  1.1  christos 
   1619  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   1620  1.1  christos nvpair_t *
   1621  1.1  christos nvpair_move_descriptor(const char *name, int value)
   1622  1.1  christos {
   1623  1.1  christos 	nvpair_t *nvp;
   1624  1.1  christos 
   1625  1.1  christos 	if (value < 0 || !fd_is_valid(value)) {
   1626  1.1  christos 		ERRNO_SET(EBADF);
   1627  1.1  christos 		return (NULL);
   1628  1.1  christos 	}
   1629  1.1  christos 
   1630  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
   1631  1.1  christos 	    sizeof(int64_t), 0);
   1632  1.1  christos 	if (nvp == NULL) {
   1633  1.1  christos 		ERRNO_SAVE();
   1634  1.1  christos 		close(value);
   1635  1.1  christos 		ERRNO_RESTORE();
   1636  1.1  christos 	}
   1637  1.1  christos 
   1638  1.1  christos 	return (nvp);
   1639  1.1  christos }
   1640  1.1  christos #endif
   1641  1.1  christos 
   1642  1.1  christos nvpair_t *
   1643  1.1  christos nvpair_move_binary(const char *name, void *value, size_t size)
   1644  1.1  christos {
   1645  1.1  christos 	nvpair_t *nvp;
   1646  1.1  christos 
   1647  1.1  christos 	if (value == NULL || size == 0) {
   1648  1.1  christos 		ERRNO_SET(EINVAL);
   1649  1.1  christos 		return (NULL);
   1650  1.1  christos 	}
   1651  1.1  christos 
   1652  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)value,
   1653  1.1  christos 	    size, 0);
   1654  1.1  christos 	if (nvp == NULL) {
   1655  1.1  christos 		ERRNO_SAVE();
   1656  1.1  christos 		nv_free(value);
   1657  1.1  christos 		ERRNO_RESTORE();
   1658  1.1  christos 	}
   1659  1.1  christos 
   1660  1.1  christos 	return (nvp);
   1661  1.1  christos }
   1662  1.1  christos 
   1663  1.1  christos nvpair_t *
   1664  1.1  christos nvpair_move_bool_array(const char *name, bool *value, size_t nitems)
   1665  1.1  christos {
   1666  1.1  christos 	nvpair_t *nvp;
   1667  1.1  christos 
   1668  1.1  christos 	if (value == NULL || nitems == 0) {
   1669  1.1  christos 		ERRNO_SET(EINVAL);
   1670  1.1  christos 		return (NULL);
   1671  1.1  christos 	}
   1672  1.1  christos 
   1673  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY,
   1674  1.1  christos 	    (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
   1675  1.1  christos 	if (nvp == NULL) {
   1676  1.1  christos 		ERRNO_SAVE();
   1677  1.1  christos 		nv_free(value);
   1678  1.1  christos 		ERRNO_RESTORE();
   1679  1.1  christos 	}
   1680  1.1  christos 
   1681  1.1  christos 	return (nvp);
   1682  1.1  christos }
   1683  1.1  christos 
   1684  1.1  christos nvpair_t *
   1685  1.1  christos nvpair_move_string_array(const char *name, char **value, size_t nitems)
   1686  1.1  christos {
   1687  1.1  christos 	nvpair_t *nvp;
   1688  1.1  christos 	size_t i, size;
   1689  1.1  christos 
   1690  1.1  christos 	if (value == NULL || nitems == 0) {
   1691  1.1  christos 		ERRNO_SET(EINVAL);
   1692  1.1  christos 		return (NULL);
   1693  1.1  christos 	}
   1694  1.1  christos 
   1695  1.1  christos 	size = 0;
   1696  1.1  christos 	for (i = 0; i < nitems; i++) {
   1697  1.1  christos 		if (value[i] == NULL) {
   1698  1.1  christos 			ERRNO_SET(EINVAL);
   1699  1.1  christos 			return (NULL);
   1700  1.1  christos 		}
   1701  1.1  christos 
   1702  1.1  christos 		size += strlen(value[i]) + 1;
   1703  1.1  christos 	}
   1704  1.1  christos 
   1705  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
   1706  1.1  christos 	    (uint64_t)(uintptr_t)value, size, nitems);
   1707  1.1  christos 	if (nvp == NULL) {
   1708  1.1  christos 		ERRNO_SAVE();
   1709  1.1  christos 		for (i = 0; i < nitems; i++)
   1710  1.1  christos 			nv_free(value[i]);
   1711  1.1  christos 		nv_free(value);
   1712  1.1  christos 		ERRNO_RESTORE();
   1713  1.1  christos 	}
   1714  1.1  christos 
   1715  1.1  christos 	return (nvp);
   1716  1.1  christos }
   1717  1.1  christos 
   1718  1.1  christos nvpair_t *
   1719  1.1  christos nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems)
   1720  1.1  christos {
   1721  1.1  christos 	nvpair_t *nvp;
   1722  1.1  christos 
   1723  1.1  christos 	if (value == NULL || nitems == 0) {
   1724  1.1  christos 		ERRNO_SET(EINVAL);
   1725  1.1  christos 		return (NULL);
   1726  1.1  christos 	}
   1727  1.1  christos 
   1728  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
   1729  1.1  christos 	    (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
   1730  1.1  christos 	if (nvp == NULL) {
   1731  1.1  christos 		ERRNO_SAVE();
   1732  1.1  christos 		nv_free(value);
   1733  1.1  christos 		ERRNO_RESTORE();
   1734  1.1  christos 	}
   1735  1.1  christos 
   1736  1.1  christos 	return (nvp);
   1737  1.1  christos }
   1738  1.1  christos 
   1739  1.1  christos nvpair_t *
   1740  1.1  christos nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems)
   1741  1.1  christos {
   1742  1.1  christos 	nvpair_t *parent;
   1743  1.1  christos 	unsigned int ii;
   1744  1.1  christos 	int flags;
   1745  1.1  christos 
   1746  1.1  christos 	if (value == NULL || nitems == 0) {
   1747  1.1  christos 		ERRNO_SET(EINVAL);
   1748  1.1  christos 		return (NULL);
   1749  1.1  christos 	}
   1750  1.1  christos 
   1751  1.1  christos 	for (ii = 0; ii < nitems; ii++) {
   1752  1.1  christos 		if (value == NULL || nvlist_error(value[ii]) != 0 ||
   1753  1.1  christos 		    nvlist_get_pararr(value[ii], NULL) != NULL) {
   1754  1.1  christos 			ERRNO_SET(EINVAL);
   1755  1.1  christos 			goto fail;
   1756  1.1  christos 		}
   1757  1.1  christos 		if (ii > 0) {
   1758  1.1  christos 			nvpair_t *nvp;
   1759  1.1  christos 
   1760  1.1  christos 			nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
   1761  1.1  christos 			    (uint64_t)(uintptr_t)value[ii], 0, 0);
   1762  1.1  christos 			if (nvp == NULL)
   1763  1.1  christos 				goto fail;
   1764  1.1  christos 			nvlist_set_array_next(value[ii - 1], nvp);
   1765  1.1  christos 		}
   1766  1.1  christos 	}
   1767  1.1  christos 	flags = nvlist_flags(value[nitems - 1]) | NV_FLAG_IN_ARRAY;
   1768  1.1  christos 	nvlist_set_flags(value[nitems - 1], flags);
   1769  1.1  christos 
   1770  1.1  christos 	parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
   1771  1.1  christos 	    (uint64_t)(uintptr_t)value, 0, nitems);
   1772  1.1  christos 	if (parent == NULL)
   1773  1.1  christos 		goto fail;
   1774  1.1  christos 
   1775  1.1  christos 	for (ii = 0; ii < nitems; ii++)
   1776  1.1  christos 		nvlist_set_parent(value[ii], parent);
   1777  1.1  christos 
   1778  1.1  christos 	return (parent);
   1779  1.1  christos fail:
   1780  1.1  christos 	ERRNO_SAVE();
   1781  1.1  christos 	for (ii = 0; ii < nitems; ii++) {
   1782  1.1  christos 		if (value[ii] != NULL &&
   1783  1.1  christos 		    nvlist_get_pararr(value[ii], NULL) != NULL) {
   1784  1.1  christos 			nvlist_destroy(value[ii]);
   1785  1.1  christos 		}
   1786  1.1  christos 	}
   1787  1.1  christos 	nv_free(value);
   1788  1.1  christos 	ERRNO_RESTORE();
   1789  1.1  christos 
   1790  1.1  christos 	return (NULL);
   1791  1.1  christos }
   1792  1.1  christos 
   1793  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   1794  1.1  christos nvpair_t *
   1795  1.1  christos nvpair_move_descriptor_array(const char *name, int *value, size_t nitems)
   1796  1.1  christos {
   1797  1.1  christos 	nvpair_t *nvp;
   1798  1.1  christos 	size_t i;
   1799  1.1  christos 
   1800  1.1  christos 	if (value == NULL || nitems == 0) {
   1801  1.1  christos 		ERRNO_SET(EINVAL);
   1802  1.1  christos 		return (NULL);
   1803  1.1  christos 	}
   1804  1.1  christos 
   1805  1.1  christos 	for (i = 0; i < nitems; i++) {
   1806  1.1  christos 		if (value[i] != -1 && !fd_is_valid(value[i])) {
   1807  1.1  christos 			ERRNO_SET(EBADF);
   1808  1.1  christos 			goto fail;
   1809  1.1  christos 		}
   1810  1.1  christos 	}
   1811  1.1  christos 
   1812  1.1  christos 	nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
   1813  1.1  christos 	    (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
   1814  1.1  christos 	if (nvp == NULL)
   1815  1.1  christos 		goto fail;
   1816  1.1  christos 
   1817  1.1  christos 	return (nvp);
   1818  1.1  christos fail:
   1819  1.1  christos 	ERRNO_SAVE();
   1820  1.1  christos 	for (i = 0; i < nitems; i++) {
   1821  1.1  christos 		if (fd_is_valid(value[i]))
   1822  1.1  christos 			close(value[i]);
   1823  1.1  christos 	}
   1824  1.1  christos 	nv_free(value);
   1825  1.1  christos 	ERRNO_RESTORE();
   1826  1.1  christos 
   1827  1.1  christos 	return (NULL);
   1828  1.1  christos }
   1829  1.1  christos #endif
   1830  1.1  christos 
   1831  1.1  christos bool
   1832  1.1  christos nvpair_get_bool(const nvpair_t *nvp)
   1833  1.1  christos {
   1834  1.1  christos 
   1835  1.1  christos 	NVPAIR_ASSERT(nvp);
   1836  1.1  christos 
   1837  1.1  christos 	return (nvp->nvp_data == 1);
   1838  1.1  christos }
   1839  1.1  christos 
   1840  1.1  christos uint64_t
   1841  1.1  christos nvpair_get_number(const nvpair_t *nvp)
   1842  1.1  christos {
   1843  1.1  christos 
   1844  1.1  christos 	NVPAIR_ASSERT(nvp);
   1845  1.1  christos 
   1846  1.1  christos 	return (nvp->nvp_data);
   1847  1.1  christos }
   1848  1.1  christos 
   1849  1.1  christos const char *
   1850  1.1  christos nvpair_get_string(const nvpair_t *nvp)
   1851  1.1  christos {
   1852  1.1  christos 
   1853  1.1  christos 	NVPAIR_ASSERT(nvp);
   1854  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
   1855  1.1  christos 
   1856  1.1  christos 	return ((const char *)(intptr_t)nvp->nvp_data);
   1857  1.1  christos }
   1858  1.1  christos 
   1859  1.1  christos const nvlist_t *
   1860  1.1  christos nvpair_get_nvlist(const nvpair_t *nvp)
   1861  1.1  christos {
   1862  1.1  christos 
   1863  1.1  christos 	NVPAIR_ASSERT(nvp);
   1864  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
   1865  1.1  christos 
   1866  1.1  christos 	return ((const nvlist_t *)(intptr_t)nvp->nvp_data);
   1867  1.1  christos }
   1868  1.1  christos 
   1869  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   1870  1.1  christos int
   1871  1.1  christos nvpair_get_descriptor(const nvpair_t *nvp)
   1872  1.1  christos {
   1873  1.1  christos 
   1874  1.1  christos 	NVPAIR_ASSERT(nvp);
   1875  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
   1876  1.1  christos 
   1877  1.1  christos 	return ((int)nvp->nvp_data);
   1878  1.1  christos }
   1879  1.1  christos #endif
   1880  1.1  christos 
   1881  1.1  christos const void *
   1882  1.1  christos nvpair_get_binary(const nvpair_t *nvp, size_t *sizep)
   1883  1.1  christos {
   1884  1.1  christos 
   1885  1.1  christos 	NVPAIR_ASSERT(nvp);
   1886  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
   1887  1.1  christos 
   1888  1.1  christos 	if (sizep != NULL)
   1889  1.1  christos 		*sizep = nvp->nvp_datasize;
   1890  1.1  christos 
   1891  1.1  christos 	return ((const void *)(intptr_t)nvp->nvp_data);
   1892  1.1  christos }
   1893  1.1  christos 
   1894  1.1  christos const bool *
   1895  1.1  christos nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitems)
   1896  1.1  christos {
   1897  1.1  christos 
   1898  1.1  christos 	NVPAIR_ASSERT(nvp);
   1899  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
   1900  1.1  christos 
   1901  1.1  christos 	if (nitems != NULL)
   1902  1.1  christos 		*nitems = nvp->nvp_nitems;
   1903  1.1  christos 
   1904  1.1  christos 	return ((const bool *)(intptr_t)nvp->nvp_data);
   1905  1.1  christos }
   1906  1.1  christos 
   1907  1.1  christos const uint64_t *
   1908  1.1  christos nvpair_get_number_array(const nvpair_t *nvp, size_t *nitems)
   1909  1.1  christos {
   1910  1.1  christos 
   1911  1.1  christos 	NVPAIR_ASSERT(nvp);
   1912  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
   1913  1.1  christos 
   1914  1.1  christos 	if (nitems != NULL)
   1915  1.1  christos 		*nitems = nvp->nvp_nitems;
   1916  1.1  christos 
   1917  1.1  christos 	return ((const uint64_t *)(intptr_t)nvp->nvp_data);
   1918  1.1  christos }
   1919  1.1  christos 
   1920  1.1  christos const char * const *
   1921  1.1  christos nvpair_get_string_array(const nvpair_t *nvp, size_t *nitems)
   1922  1.1  christos {
   1923  1.1  christos 
   1924  1.1  christos 	NVPAIR_ASSERT(nvp);
   1925  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
   1926  1.1  christos 
   1927  1.1  christos 	if (nitems != NULL)
   1928  1.1  christos 		*nitems = nvp->nvp_nitems;
   1929  1.1  christos 
   1930  1.1  christos 	return ((const char * const *)(intptr_t)nvp->nvp_data);
   1931  1.1  christos }
   1932  1.1  christos 
   1933  1.1  christos const nvlist_t * const *
   1934  1.1  christos nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitems)
   1935  1.1  christos {
   1936  1.1  christos 
   1937  1.1  christos 	NVPAIR_ASSERT(nvp);
   1938  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
   1939  1.1  christos 
   1940  1.1  christos 	if (nitems != NULL)
   1941  1.1  christos 		*nitems = nvp->nvp_nitems;
   1942  1.1  christos 
   1943  1.1  christos 	return ((const nvlist_t * const *)((intptr_t)nvp->nvp_data));
   1944  1.1  christos }
   1945  1.1  christos 
   1946  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   1947  1.1  christos const int *
   1948  1.1  christos nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitems)
   1949  1.1  christos {
   1950  1.1  christos 
   1951  1.1  christos 	NVPAIR_ASSERT(nvp);
   1952  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
   1953  1.1  christos 
   1954  1.1  christos 	if (nitems != NULL)
   1955  1.1  christos 		*nitems = nvp->nvp_nitems;
   1956  1.1  christos 
   1957  1.1  christos 	return ((const int *)(intptr_t)nvp->nvp_data);
   1958  1.1  christos }
   1959  1.1  christos #endif
   1960  1.1  christos 
   1961  1.1  christos int
   1962  1.1  christos nvpair_append_bool_array(nvpair_t *nvp, const bool value)
   1963  1.1  christos {
   1964  1.1  christos 
   1965  1.1  christos 	NVPAIR_ASSERT(nvp);
   1966  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
   1967  1.1  christos 	return (nvpair_append(nvp, &value, sizeof(value), sizeof(value)));
   1968  1.1  christos }
   1969  1.1  christos 
   1970  1.1  christos int
   1971  1.1  christos nvpair_append_number_array(nvpair_t *nvp, const uint64_t value)
   1972  1.1  christos {
   1973  1.1  christos 
   1974  1.1  christos 	NVPAIR_ASSERT(nvp);
   1975  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
   1976  1.1  christos 	return (nvpair_append(nvp, &value, sizeof(value), sizeof(value)));
   1977  1.1  christos }
   1978  1.1  christos 
   1979  1.1  christos int
   1980  1.1  christos nvpair_append_string_array(nvpair_t *nvp, const char *value)
   1981  1.1  christos {
   1982  1.1  christos 	char *str;
   1983  1.1  christos 
   1984  1.1  christos 	NVPAIR_ASSERT(nvp);
   1985  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
   1986  1.1  christos 	if (value == NULL) {
   1987  1.1  christos 		ERRNO_SET(EINVAL);
   1988  1.1  christos 		return (-1);
   1989  1.1  christos 	}
   1990  1.1  christos 	str = nv_strdup(value);
   1991  1.1  christos 	if (str == NULL) {
   1992  1.1  christos 		return (-1);
   1993  1.1  christos 	}
   1994  1.1  christos 	if (nvpair_append(nvp, &str, sizeof(str), strlen(str) + 1) == -1) {
   1995  1.1  christos 		nv_free(str);
   1996  1.1  christos 		return (-1);
   1997  1.1  christos 	}
   1998  1.1  christos 	return (0);
   1999  1.1  christos }
   2000  1.1  christos 
   2001  1.1  christos int
   2002  1.1  christos nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value)
   2003  1.1  christos {
   2004  1.1  christos 	nvpair_t *tmpnvp;
   2005  1.1  christos 	nvlist_t *nvl, *prev;
   2006  1.1  christos 	int flags;
   2007  1.1  christos 
   2008  1.1  christos 	NVPAIR_ASSERT(nvp);
   2009  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
   2010  1.1  christos 	if (value == NULL || nvlist_error(value) != 0 ||
   2011  1.1  christos 	    nvlist_get_pararr(value, NULL) != NULL) {
   2012  1.1  christos 		ERRNO_SET(EINVAL);
   2013  1.1  christos 		return (-1);
   2014  1.1  christos 	}
   2015  1.1  christos 	nvl = nvlist_clone(value);
   2016  1.1  christos 	if (nvl == NULL) {
   2017  1.1  christos 		return (-1);
   2018  1.1  christos 	}
   2019  1.1  christos 	flags = nvlist_flags(nvl) | NV_FLAG_IN_ARRAY;
   2020  1.1  christos 	nvlist_set_flags(nvl, flags);
   2021  1.1  christos 
   2022  1.1  christos 	tmpnvp = NULL;
   2023  1.1  christos 	prev = NULL;
   2024  1.1  christos 	if (nvp->nvp_nitems > 0) {
   2025  1.1  christos 		nvlist_t **nvls = (void *)(uintptr_t)nvp->nvp_data;
   2026  1.1  christos 
   2027  1.1  christos 		prev = nvls[nvp->nvp_nitems - 1];
   2028  1.1  christos 		PJDLOG_ASSERT(prev != NULL);
   2029  1.1  christos 
   2030  1.1  christos 		tmpnvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
   2031  1.1  christos 		    (uint64_t)(uintptr_t)nvl, 0, 0);
   2032  1.1  christos 		if (tmpnvp == NULL) {
   2033  1.1  christos 			goto fail;
   2034  1.1  christos 		}
   2035  1.1  christos 	}
   2036  1.1  christos 	if (nvpair_append(nvp, &nvl, sizeof(nvl), 0) == -1) {
   2037  1.1  christos 		goto fail;
   2038  1.1  christos 	}
   2039  1.1  christos 	if (tmpnvp) {
   2040  1.1  christos 		NVPAIR_ASSERT(tmpnvp);
   2041  1.1  christos 		nvlist_set_array_next(prev, tmpnvp);
   2042  1.1  christos 	}
   2043  1.1  christos 	nvlist_set_parent(nvl, nvp);
   2044  1.1  christos 	return (0);
   2045  1.1  christos fail:
   2046  1.1  christos 	if (tmpnvp) {
   2047  1.1  christos 		nvpair_free(tmpnvp);
   2048  1.1  christos 	}
   2049  1.1  christos 	nvlist_destroy(nvl);
   2050  1.1  christos 	return (-1);
   2051  1.1  christos }
   2052  1.1  christos 
   2053  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   2054  1.1  christos int
   2055  1.1  christos nvpair_append_descriptor_array(nvpair_t *nvp, const int value)
   2056  1.1  christos {
   2057  1.1  christos 	int fd;
   2058  1.1  christos 
   2059  1.1  christos 	NVPAIR_ASSERT(nvp);
   2060  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
   2061  1.1  christos 	if (value < 0 || !fd_is_valid(value)) {
   2062  1.1  christos 		ERRNO_SET(EBADF);
   2063  1.1  christos 		return -1;
   2064  1.1  christos 	}
   2065  1.1  christos 	fd = fcntl(value, F_DUPFD_CLOEXEC, 0);
   2066  1.1  christos 	if (fd == -1) {
   2067  1.1  christos 		return (-1);
   2068  1.1  christos 	}
   2069  1.1  christos 	if (nvpair_append(nvp, &fd, sizeof(fd), sizeof(fd)) == -1) {
   2070  1.1  christos 		close(fd);
   2071  1.1  christos 		return (-1);
   2072  1.1  christos 	}
   2073  1.1  christos 	return (0);
   2074  1.1  christos }
   2075  1.1  christos #endif
   2076  1.1  christos 
   2077  1.1  christos void
   2078  1.1  christos nvpair_free(nvpair_t *nvp)
   2079  1.1  christos {
   2080  1.1  christos 	size_t i;
   2081  1.1  christos 
   2082  1.1  christos 	NVPAIR_ASSERT(nvp);
   2083  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_list == NULL);
   2084  1.1  christos 
   2085  1.1  christos 	nvp->nvp_magic = 0;
   2086  1.1  christos 	switch (nvp->nvp_type) {
   2087  1.2  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
   2088  1.1  christos 	case NV_TYPE_DESCRIPTOR:
   2089  1.1  christos 		close((int)nvp->nvp_data);
   2090  1.1  christos 		break;
   2091  1.1  christos 	case NV_TYPE_DESCRIPTOR_ARRAY:
   2092  1.1  christos 		for (i = 0; i < nvp->nvp_nitems; i++)
   2093  1.1  christos 			close(((int *)(intptr_t)nvp->nvp_data)[i]);
   2094  1.5     rmind 		nv_free((int *)(intptr_t)nvp->nvp_data);
   2095  1.1  christos 		break;
   2096  1.1  christos #endif
   2097  1.1  christos 	case NV_TYPE_NVLIST:
   2098  1.1  christos 		nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data);
   2099  1.1  christos 		break;
   2100  1.1  christos 	case NV_TYPE_STRING:
   2101  1.1  christos 		nv_free((char *)(intptr_t)nvp->nvp_data);
   2102  1.1  christos 		break;
   2103  1.1  christos 	case NV_TYPE_BINARY:
   2104  1.1  christos 		nv_free((void *)(intptr_t)nvp->nvp_data);
   2105  1.1  christos 		break;
   2106  1.1  christos 	case NV_TYPE_NVLIST_ARRAY:
   2107  1.1  christos 		for (i = 0; i < nvp->nvp_nitems; i++) {
   2108  1.1  christos 			nvlist_destroy(
   2109  1.1  christos 			    ((nvlist_t **)(intptr_t)nvp->nvp_data)[i]);
   2110  1.1  christos 		}
   2111  1.1  christos 		nv_free(((nvlist_t **)(intptr_t)nvp->nvp_data));
   2112  1.1  christos 		break;
   2113  1.1  christos 	case NV_TYPE_NUMBER_ARRAY:
   2114  1.1  christos 		nv_free((uint64_t *)(intptr_t)nvp->nvp_data);
   2115  1.1  christos 		break;
   2116  1.1  christos 	case NV_TYPE_BOOL_ARRAY:
   2117  1.1  christos 		nv_free((bool *)(intptr_t)nvp->nvp_data);
   2118  1.1  christos 		break;
   2119  1.1  christos 	case NV_TYPE_STRING_ARRAY:
   2120  1.1  christos 		for (i = 0; i < nvp->nvp_nitems; i++)
   2121  1.1  christos 			nv_free(((char **)(intptr_t)nvp->nvp_data)[i]);
   2122  1.1  christos 		nv_free((char **)(intptr_t)nvp->nvp_data);
   2123  1.1  christos 		break;
   2124  1.1  christos 	}
   2125  1.1  christos 	nv_free(nvp);
   2126  1.1  christos }
   2127  1.1  christos 
   2128  1.1  christos void
   2129  1.1  christos nvpair_free_structure(nvpair_t *nvp)
   2130  1.1  christos {
   2131  1.1  christos 
   2132  1.1  christos 	NVPAIR_ASSERT(nvp);
   2133  1.1  christos 	PJDLOG_ASSERT(nvp->nvp_list == NULL);
   2134  1.1  christos 
   2135  1.1  christos 	nvp->nvp_magic = 0;
   2136  1.1  christos 	nv_free(nvp);
   2137  1.1  christos }
   2138  1.1  christos 
   2139  1.1  christos const char *
   2140  1.1  christos nvpair_type_string(int type)
   2141  1.1  christos {
   2142  1.1  christos 
   2143  1.1  christos 	switch (type) {
   2144  1.1  christos 	case NV_TYPE_NULL:
   2145  1.1  christos 		return ("NULL");
   2146  1.1  christos 	case NV_TYPE_BOOL:
   2147  1.1  christos 		return ("BOOL");
   2148  1.1  christos 	case NV_TYPE_NUMBER:
   2149  1.1  christos 		return ("NUMBER");
   2150  1.1  christos 	case NV_TYPE_STRING:
   2151  1.1  christos 		return ("STRING");
   2152  1.1  christos 	case NV_TYPE_NVLIST:
   2153  1.1  christos 		return ("NVLIST");
   2154  1.1  christos 	case NV_TYPE_DESCRIPTOR:
   2155  1.1  christos 		return ("DESCRIPTOR");
   2156  1.1  christos 	case NV_TYPE_BINARY:
   2157  1.1  christos 		return ("BINARY");
   2158  1.1  christos 	case NV_TYPE_BOOL_ARRAY:
   2159  1.1  christos 		return ("BOOL ARRAY");
   2160  1.1  christos 	case NV_TYPE_NUMBER_ARRAY:
   2161  1.1  christos 		return ("NUMBER ARRAY");
   2162  1.1  christos 	case NV_TYPE_STRING_ARRAY:
   2163  1.1  christos 		return ("STRING ARRAY");
   2164  1.1  christos 	case NV_TYPE_NVLIST_ARRAY:
   2165  1.1  christos 		return ("NVLIST ARRAY");
   2166  1.1  christos 	case NV_TYPE_DESCRIPTOR_ARRAY:
   2167  1.1  christos 		return ("DESCRIPTOR ARRAY");
   2168  1.1  christos 	default:
   2169  1.1  christos 		return ("<UNKNOWN>");
   2170  1.1  christos 	}
   2171  1.1  christos }
   2172  1.1  christos 
   2173