xref: /netbsd-src/sys/external/bsd/libnv/dist/cnvlist.c (revision 32302d25abec5456e298ff05ca4fde499624bc77)
1*32302d25Schristos /*	$NetBSD: cnvlist.c,v 1.2 2018/09/08 14:02:15 christos Exp $	*/
2*32302d25Schristos 
384193a29Schristos /*-
484193a29Schristos  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
584193a29Schristos  *
684193a29Schristos  * Copyright (c) 2016 Adam Starak <starak.adam@gmail.com>
784193a29Schristos  * All rights reserved.
884193a29Schristos  *
984193a29Schristos  * Redistribution and use in source and binary forms, with or without
1084193a29Schristos  * modification, are permitted provided that the following conditions
1184193a29Schristos  * are met:
1284193a29Schristos  * 1. Redistributions of source code must retain the above copyright
1384193a29Schristos  *    notice, this list of conditions and the following disclaimer.
1484193a29Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1584193a29Schristos  *    notice, this list of conditions and the following disclaimer in the
1684193a29Schristos  *    documentation and/or other materials provided with the distribution.
1784193a29Schristos  *
1884193a29Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1984193a29Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2084193a29Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2184193a29Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2284193a29Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2384193a29Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2484193a29Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2584193a29Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2684193a29Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2784193a29Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2884193a29Schristos  * SUCH DAMAGE.
2984193a29Schristos  *
3084193a29Schristos  * $FreeBSD: head/sys/contrib/libnv/cnvlist.c 335343 2018-06-18 21:26:58Z oshogbo $
3184193a29Schristos  */
3284193a29Schristos 
3384193a29Schristos #include <sys/cdefs.h>
34*32302d25Schristos #ifdef __FreeBSD__
3584193a29Schristos __FBSDID("$FreeBSD: head/sys/contrib/libnv/cnvlist.c 335343 2018-06-18 21:26:58Z oshogbo $");
36*32302d25Schristos #else
37*32302d25Schristos __RCSID("$NetBSD: cnvlist.c,v 1.2 2018/09/08 14:02:15 christos Exp $");
38*32302d25Schristos #endif
3984193a29Schristos 
40*32302d25Schristos #if defined(_KERNEL) || defined(_STANDALONE)
4184193a29Schristos 
4284193a29Schristos #include <sys/types.h>
4384193a29Schristos #include <sys/param.h>
4484193a29Schristos #include <sys/kernel.h>
4584193a29Schristos #include <sys/systm.h>
4684193a29Schristos #include <sys/malloc.h>
4784193a29Schristos 
48*32302d25Schristos #ifdef __FreeBSD__
4984193a29Schristos #include <machine/stdarg.h>
50*32302d25Schristos #endif
5184193a29Schristos 
5284193a29Schristos #else
5384193a29Schristos #include <stdarg.h>
5484193a29Schristos #include <stdbool.h>
5584193a29Schristos #include <stdint.h>
5684193a29Schristos #include <stdlib.h>
5784193a29Schristos #endif
5884193a29Schristos 
59*32302d25Schristos #ifdef __FreeBSD__
6084193a29Schristos #include <sys/nv.h>
61*32302d25Schristos #include <sys/cnv.h>
62*32302d25Schristos #else
63*32302d25Schristos #include "nv.h"
64*32302d25Schristos #include "cnv.h"
65*32302d25Schristos #endif
6684193a29Schristos 
6784193a29Schristos #include "nv_impl.h"
6884193a29Schristos #include "nvlist_impl.h"
6984193a29Schristos #include "nvpair_impl.h"
7084193a29Schristos 
7184193a29Schristos const char *
cnvlist_name(const void * cookie)7284193a29Schristos cnvlist_name(const void *cookie)
7384193a29Schristos {
7484193a29Schristos 
7584193a29Schristos 	return (nvpair_name(cookie));
7684193a29Schristos }
7784193a29Schristos 
7884193a29Schristos int
cnvlist_type(const void * cookie)7984193a29Schristos cnvlist_type(const void *cookie)
8084193a29Schristos {
8184193a29Schristos 
8284193a29Schristos 	return (nvpair_type(cookie));
8384193a29Schristos }
8484193a29Schristos 
8584193a29Schristos #define	CNVLIST_GET(ftype, type, NVTYPE)				\
8684193a29Schristos ftype									\
8784193a29Schristos cnvlist_get_##type(const void *cookie)					\
8884193a29Schristos {									\
8984193a29Schristos 									\
9084193a29Schristos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
9184193a29Schristos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
9284193a29Schristos 		    nvpair_name(cookie));				\
9384193a29Schristos 	}								\
9484193a29Schristos         return (nvpair_get_##type(cookie));				\
9584193a29Schristos }
9684193a29Schristos 
CNVLIST_GET(bool,bool,BOOL)9784193a29Schristos CNVLIST_GET(bool, bool, BOOL)
9884193a29Schristos CNVLIST_GET(uint64_t, number, NUMBER)
9984193a29Schristos CNVLIST_GET(const char *, string, STRING)
10084193a29Schristos CNVLIST_GET(const nvlist_t *, nvlist, NVLIST)
101*32302d25Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
10284193a29Schristos CNVLIST_GET(int, descriptor, DESCRIPTOR)
10384193a29Schristos #endif
10484193a29Schristos 
10584193a29Schristos #undef	CNVLIST_GET
10684193a29Schristos 
10784193a29Schristos #define	CNVLIST_GET_ARRAY(ftype, type, NVTYPE)				\
10884193a29Schristos ftype									\
10984193a29Schristos cnvlist_get_##type(const void *cookie, size_t *nitemsp)			\
11084193a29Schristos {									\
11184193a29Schristos 									\
11284193a29Schristos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
11384193a29Schristos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
11484193a29Schristos 		    nvpair_name(cookie));				\
11584193a29Schristos 	}								\
11684193a29Schristos 	return (nvpair_get_##type(cookie, nitemsp));			\
11784193a29Schristos }
11884193a29Schristos 
11984193a29Schristos CNVLIST_GET_ARRAY(const bool *, bool_array, BOOL_ARRAY)
12084193a29Schristos CNVLIST_GET_ARRAY(const uint64_t *, number_array, NUMBER_ARRAY)
12184193a29Schristos CNVLIST_GET_ARRAY(const char * const *, string_array, STRING_ARRAY)
12284193a29Schristos CNVLIST_GET_ARRAY(const nvlist_t * const *, nvlist_array, NVLIST_ARRAY)
123*32302d25Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
12484193a29Schristos CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY)
12584193a29Schristos #endif
12684193a29Schristos 
12784193a29Schristos #undef	CNVLIST_GET_ARRAY
12884193a29Schristos 
12984193a29Schristos const void *
13084193a29Schristos cnvlist_get_binary(const void *cookie, size_t *sizep)
13184193a29Schristos {
13284193a29Schristos 
13384193a29Schristos 	if (nvpair_type(cookie) != NV_TYPE_BINARY)
13484193a29Schristos 		nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
13584193a29Schristos 	return (nvpair_get_binary(cookie, sizep));
13684193a29Schristos }
13784193a29Schristos 
13884193a29Schristos #define CNVLIST_TAKE(ftype, type, NVTYPE)				\
13984193a29Schristos ftype									\
14084193a29Schristos cnvlist_take_##type(void *cookie)					\
14184193a29Schristos {									\
14284193a29Schristos 	ftype value;							\
14384193a29Schristos 	nvlist_t *nvl;							\
14484193a29Schristos 									\
14584193a29Schristos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
14684193a29Schristos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
14784193a29Schristos 		    nvpair_name(cookie));				\
14884193a29Schristos 	}								\
14984193a29Schristos 	nvl = nvpair_nvlist(cookie);					\
15084193a29Schristos 	value = (ftype)(intptr_t)nvpair_get_##type(cookie);		\
15184193a29Schristos 	nvlist_remove_nvpair(nvl, cookie);				\
15284193a29Schristos 	nvpair_free_structure(cookie);					\
15384193a29Schristos 	return (value);							\
15484193a29Schristos }
15584193a29Schristos 
15684193a29Schristos CNVLIST_TAKE(bool, bool, BOOL)
15784193a29Schristos CNVLIST_TAKE(uint64_t, number, NUMBER)
15884193a29Schristos CNVLIST_TAKE(char *, string, STRING)
15984193a29Schristos CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
160*32302d25Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
16184193a29Schristos CNVLIST_TAKE(int, descriptor, DESCRIPTOR)
16284193a29Schristos #endif
16384193a29Schristos 
16484193a29Schristos #undef	CNVLIST_TAKE
16584193a29Schristos 
16684193a29Schristos #define	CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE)				\
16784193a29Schristos ftype									\
16884193a29Schristos cnvlist_take_##type(void *cookie, size_t *nitemsp)			\
16984193a29Schristos {									\
17084193a29Schristos 	ftype value;							\
17184193a29Schristos 	nvlist_t *nvl;							\
17284193a29Schristos 									\
17384193a29Schristos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
17484193a29Schristos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
17584193a29Schristos 		    nvpair_name(cookie));				\
17684193a29Schristos 	}								\
17784193a29Schristos 	nvl = nvpair_nvlist(cookie);					\
17884193a29Schristos 	value = (ftype)(intptr_t)nvpair_get_##type(cookie, nitemsp);	\
17984193a29Schristos 	nvlist_remove_nvpair(nvl, cookie);				\
18084193a29Schristos 	nvpair_free_structure(cookie);					\
18184193a29Schristos 	return (value);							\
18284193a29Schristos }
18384193a29Schristos 
18484193a29Schristos CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY)
18584193a29Schristos CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY)
18684193a29Schristos CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY)
18784193a29Schristos CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY)
188*32302d25Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
18984193a29Schristos CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY);
19084193a29Schristos #endif
19184193a29Schristos 
19284193a29Schristos #undef	CNVLIST_TAKE_ARRAY
19384193a29Schristos 
19484193a29Schristos void *
cnvlist_take_binary(void * cookie,size_t * sizep)19584193a29Schristos cnvlist_take_binary(void *cookie, size_t *sizep)
19684193a29Schristos {
19784193a29Schristos 	void *value;
19884193a29Schristos 	nvlist_t *nvl;
19984193a29Schristos 
20084193a29Schristos 	if (nvpair_type(cookie) != NV_TYPE_BINARY)
20184193a29Schristos 		nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
20284193a29Schristos 	nvl = nvpair_nvlist(cookie);
20384193a29Schristos 	value = (void *)(intptr_t)nvpair_get_binary(cookie, sizep);
20484193a29Schristos 	nvlist_remove_nvpair(nvl, cookie);
20584193a29Schristos 	nvpair_free_structure(cookie);
20684193a29Schristos 	return (value);
20784193a29Schristos }
20884193a29Schristos 
20984193a29Schristos 
21084193a29Schristos #define	CNVLIST_FREE(type)						\
21184193a29Schristos void									\
21284193a29Schristos cnvlist_free_##type(void *cookie)					\
21384193a29Schristos {									\
21484193a29Schristos 									\
21584193a29Schristos 	nvlist_free_nvpair(nvpair_nvlist(cookie), cookie);		\
21684193a29Schristos }
21784193a29Schristos 
21884193a29Schristos CNVLIST_FREE(bool)
21984193a29Schristos CNVLIST_FREE(number)
22084193a29Schristos CNVLIST_FREE(string)
22184193a29Schristos CNVLIST_FREE(nvlist)
22284193a29Schristos CNVLIST_FREE(binary);
22384193a29Schristos CNVLIST_FREE(bool_array)
22484193a29Schristos CNVLIST_FREE(number_array)
22584193a29Schristos CNVLIST_FREE(string_array)
22684193a29Schristos CNVLIST_FREE(nvlist_array)
227*32302d25Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
22884193a29Schristos CNVLIST_FREE(descriptor)
22984193a29Schristos CNVLIST_FREE(descriptor_array)
23084193a29Schristos #endif
23184193a29Schristos 
23284193a29Schristos #undef	CNVLIST_FREE
233