xref: /netbsd-src/sys/external/bsd/libnv/dist/dnvlist.c (revision 9e195d8e86e639266f7f86875710ad458c368a64)
1*9e195d8eSchristos /*	$NetBSD: dnvlist.c,v 1.4 2018/09/08 14:32:25 christos Exp $	*/
232302d25Schristos 
384193a29Schristos /*-
484193a29Schristos  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
584193a29Schristos  *
684193a29Schristos  * Copyright (c) 2013 The FreeBSD Foundation
784193a29Schristos  * All rights reserved.
884193a29Schristos  *
984193a29Schristos  * This software was developed by Pawel Jakub Dawidek under sponsorship from
1084193a29Schristos  * the FreeBSD Foundation.
1184193a29Schristos  *
1284193a29Schristos  * Redistribution and use in source and binary forms, with or without
1384193a29Schristos  * modification, are permitted provided that the following conditions
1484193a29Schristos  * are met:
1584193a29Schristos  * 1. Redistributions of source code must retain the above copyright
1684193a29Schristos  *    notice, this list of conditions and the following disclaimer.
1784193a29Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1884193a29Schristos  *    notice, this list of conditions and the following disclaimer in the
1984193a29Schristos  *    documentation and/or other materials provided with the distribution.
2084193a29Schristos  *
2184193a29Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
2284193a29Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2384193a29Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2484193a29Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2584193a29Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2684193a29Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2784193a29Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2884193a29Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2984193a29Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3084193a29Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3184193a29Schristos  * SUCH DAMAGE.
3284193a29Schristos  */
3384193a29Schristos 
3484193a29Schristos #include <sys/cdefs.h>
3532302d25Schristos #ifdef __FreeBSD__
3684193a29Schristos __FBSDID("$FreeBSD: head/sys/contrib/libnv/dnvlist.c 328474 2018-01-27 12:58:21Z oshogbo $");
3732302d25Schristos #else
38*9e195d8eSchristos __RCSID("$NetBSD: dnvlist.c,v 1.4 2018/09/08 14:32:25 christos Exp $");
3932302d25Schristos #endif
4084193a29Schristos 
4132302d25Schristos #if defined(_KERNEL) || defined(_STANDALONE)
4284193a29Schristos 
4384193a29Schristos #include <sys/types.h>
4484193a29Schristos #include <sys/param.h>
4584193a29Schristos #include <sys/kernel.h>
4684193a29Schristos #include <sys/systm.h>
4784193a29Schristos #include <sys/malloc.h>
4884193a29Schristos 
49*9e195d8eSchristos #ifdef __FreeBSD__
5084193a29Schristos #include <machine/stdarg.h>
5132302d25Schristos #endif
5284193a29Schristos 
5384193a29Schristos #else
5484193a29Schristos #include <stdarg.h>
5584193a29Schristos #include <stdbool.h>
5684193a29Schristos #include <stdint.h>
5784193a29Schristos #include <stdlib.h>
5884193a29Schristos #endif
5984193a29Schristos 
6043feefbfSchristos #ifdef __FreeBSD__
6184193a29Schristos #include <sys/dnv.h>
6284193a29Schristos #include <sys/nv.h>
6332302d25Schristos #else
6432302d25Schristos #include "dnv.h"
6532302d25Schristos #include "nv.h"
6632302d25Schristos #endif
6784193a29Schristos 
6884193a29Schristos #include "nv_impl.h"
6984193a29Schristos 
7084193a29Schristos #define	DNVLIST_GET(ftype, type)					\
7184193a29Schristos ftype									\
7284193a29Schristos dnvlist_get_##type(const nvlist_t *nvl, const char *name, ftype defval)	\
7384193a29Schristos {									\
7484193a29Schristos 									\
7584193a29Schristos 	if (nvlist_exists_##type(nvl, name))				\
7684193a29Schristos 		return (nvlist_get_##type(nvl, name));			\
7784193a29Schristos 	else								\
7884193a29Schristos 		return (defval);					\
7984193a29Schristos }
8084193a29Schristos 
DNVLIST_GET(bool,bool)8184193a29Schristos DNVLIST_GET(bool, bool)
8284193a29Schristos DNVLIST_GET(uint64_t, number)
8384193a29Schristos DNVLIST_GET(const char *, string)
8484193a29Schristos DNVLIST_GET(const nvlist_t *, nvlist)
8532302d25Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
8684193a29Schristos DNVLIST_GET(int, descriptor)
8784193a29Schristos #endif
8884193a29Schristos 
8984193a29Schristos #undef	DNVLIST_GET
9084193a29Schristos 
9184193a29Schristos const void *
9284193a29Schristos dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep,
9384193a29Schristos     const void *defval, size_t defsize)
9484193a29Schristos {
9584193a29Schristos 	const void *value;
9684193a29Schristos 
9784193a29Schristos 	if (nvlist_exists_binary(nvl, name))
9884193a29Schristos 		value = nvlist_get_binary(nvl, name, sizep);
9984193a29Schristos 	else {
10084193a29Schristos 		if (sizep != NULL)
10184193a29Schristos 			*sizep = defsize;
10284193a29Schristos 		value = defval;
10384193a29Schristos 	}
10484193a29Schristos 	return (value);
10584193a29Schristos }
10684193a29Schristos 
10784193a29Schristos #define	DNVLIST_TAKE(ftype, type)					\
10884193a29Schristos ftype									\
10984193a29Schristos dnvlist_take_##type(nvlist_t *nvl, const char *name, ftype defval)	\
11084193a29Schristos {									\
11184193a29Schristos 									\
11284193a29Schristos 	if (nvlist_exists_##type(nvl, name))				\
11384193a29Schristos 		return (nvlist_take_##type(nvl, name));			\
11484193a29Schristos 	else								\
11584193a29Schristos 		return (defval);					\
11684193a29Schristos }
11784193a29Schristos 
DNVLIST_TAKE(bool,bool)11884193a29Schristos DNVLIST_TAKE(bool, bool)
11984193a29Schristos DNVLIST_TAKE(uint64_t, number)
12084193a29Schristos DNVLIST_TAKE(char *, string)
12184193a29Schristos DNVLIST_TAKE(nvlist_t *, nvlist)
12232302d25Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
12384193a29Schristos DNVLIST_TAKE(int, descriptor)
12484193a29Schristos #endif
12584193a29Schristos 
12684193a29Schristos #undef	DNVLIST_TAKE
12784193a29Schristos 
12884193a29Schristos void *
12984193a29Schristos dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep,
13084193a29Schristos     void *defval, size_t defsize)
13184193a29Schristos {
13284193a29Schristos 	void *value;
13384193a29Schristos 
13484193a29Schristos 	if (nvlist_exists_binary(nvl, name))
13584193a29Schristos 		value = nvlist_take_binary(nvl, name, sizep);
13684193a29Schristos 	else {
13784193a29Schristos 		if (sizep != NULL)
13884193a29Schristos 			*sizep = defsize;
13984193a29Schristos 		value = defval;
14084193a29Schristos 	}
14184193a29Schristos 	return (value);
14284193a29Schristos }
14384193a29Schristos 
144