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