1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2016 Adam Starak <starak.adam@gmail.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #ifdef _KERNEL
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/stdarg.h>
42
43 #else
44 #include <stdarg.h>
45 #include <stdbool.h>
46 #include <stdint.h>
47 #include <stdlib.h>
48 #endif
49
50 #include <sys/cnv.h>
51 #include <sys/nv.h>
52
53 #include "nv_impl.h"
54 #include "nvlist_impl.h"
55 #include "nvpair_impl.h"
56
57 const char *
cnvlist_name(const void * cookie)58 cnvlist_name(const void *cookie)
59 {
60
61 return (nvpair_name(cookie));
62 }
63
64 int
cnvlist_type(const void * cookie)65 cnvlist_type(const void *cookie)
66 {
67
68 return (nvpair_type(cookie));
69 }
70
71 #define CNVLIST_GET(ftype, type, NVTYPE) \
72 ftype \
73 cnvlist_get_##type(const void *cookie) \
74 { \
75 \
76 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
77 nvlist_report_missing(NV_TYPE_##NVTYPE, \
78 nvpair_name(cookie)); \
79 } \
80 return (nvpair_get_##type(cookie)); \
81 }
82
CNVLIST_GET(bool,bool,BOOL)83 CNVLIST_GET(bool, bool, BOOL)
84 CNVLIST_GET(uint64_t, number, NUMBER)
85 CNVLIST_GET(const char *, string, STRING)
86 CNVLIST_GET(const nvlist_t *, nvlist, NVLIST)
87 #ifndef _KERNEL
88 CNVLIST_GET(int, descriptor, DESCRIPTOR)
89 #endif
90
91 #undef CNVLIST_GET
92
93 #define CNVLIST_GET_ARRAY(ftype, type, NVTYPE) \
94 ftype \
95 cnvlist_get_##type(const void *cookie, size_t *nitemsp) \
96 { \
97 \
98 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
99 nvlist_report_missing(NV_TYPE_##NVTYPE, \
100 nvpair_name(cookie)); \
101 } \
102 return (nvpair_get_##type(cookie, nitemsp)); \
103 }
104
105 CNVLIST_GET_ARRAY(const bool *, bool_array, BOOL_ARRAY)
106 CNVLIST_GET_ARRAY(const uint64_t *, number_array, NUMBER_ARRAY)
107 CNVLIST_GET_ARRAY(const char * const *, string_array, STRING_ARRAY)
108 CNVLIST_GET_ARRAY(const nvlist_t * const *, nvlist_array, NVLIST_ARRAY)
109 #ifndef _KERNEL
110 CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY)
111 #endif
112
113 #undef CNVLIST_GET_ARRAY
114
115 const void *
116 cnvlist_get_binary(const void *cookie, size_t *sizep)
117 {
118
119 if (nvpair_type(cookie) != NV_TYPE_BINARY)
120 nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
121 return (nvpair_get_binary(cookie, sizep));
122 }
123
124 #define CNVLIST_TAKE(ftype, type, NVTYPE) \
125 ftype \
126 cnvlist_take_##type(void *cookie) \
127 { \
128 ftype value; \
129 nvlist_t *nvl; \
130 \
131 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
132 nvlist_report_missing(NV_TYPE_##NVTYPE, \
133 nvpair_name(cookie)); \
134 } \
135 nvl = nvpair_nvlist(cookie); \
136 value = (ftype)(intptr_t)nvpair_get_##type(cookie); \
137 nvlist_remove_nvpair(nvl, cookie); \
138 nvpair_free_structure(cookie); \
139 return (value); \
140 }
141
142 CNVLIST_TAKE(bool, bool, BOOL)
143 CNVLIST_TAKE(uint64_t, number, NUMBER)
144 CNVLIST_TAKE(char *, string, STRING)
145 CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
146 #ifndef _KERNEL
147 CNVLIST_TAKE(int, descriptor, DESCRIPTOR)
148 #endif
149
150 #undef CNVLIST_TAKE
151
152 #define CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE) \
153 ftype \
154 cnvlist_take_##type(void *cookie, size_t *nitemsp) \
155 { \
156 ftype value; \
157 nvlist_t *nvl; \
158 \
159 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
160 nvlist_report_missing(NV_TYPE_##NVTYPE, \
161 nvpair_name(cookie)); \
162 } \
163 nvl = nvpair_nvlist(cookie); \
164 value = (ftype)(intptr_t)nvpair_get_##type(cookie, nitemsp); \
165 nvlist_remove_nvpair(nvl, cookie); \
166 nvpair_free_structure(cookie); \
167 return (value); \
168 }
169
170 CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY)
171 CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY)
172 CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY)
173 CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY)
174 #ifndef _KERNEL
175 CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY);
176 #endif
177
178 #undef CNVLIST_TAKE_ARRAY
179
180 void *
cnvlist_take_binary(void * cookie,size_t * sizep)181 cnvlist_take_binary(void *cookie, size_t *sizep)
182 {
183 void *value;
184 nvlist_t *nvl;
185
186 if (nvpair_type(cookie) != NV_TYPE_BINARY)
187 nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
188 nvl = nvpair_nvlist(cookie);
189 value = (void *)(intptr_t)nvpair_get_binary(cookie, sizep);
190 nvlist_remove_nvpair(nvl, cookie);
191 nvpair_free_structure(cookie);
192 return (value);
193 }
194
195
196 #define CNVLIST_FREE(type) \
197 void \
198 cnvlist_free_##type(void *cookie) \
199 { \
200 \
201 nvlist_free_nvpair(nvpair_nvlist(cookie), cookie); \
202 }
203
204 CNVLIST_FREE(bool)
205 CNVLIST_FREE(number)
206 CNVLIST_FREE(string)
207 CNVLIST_FREE(nvlist)
208 CNVLIST_FREE(binary);
209 CNVLIST_FREE(bool_array)
210 CNVLIST_FREE(number_array)
211 CNVLIST_FREE(string_array)
212 CNVLIST_FREE(nvlist_array)
213 #ifndef _KERNEL
214 CNVLIST_FREE(descriptor)
215 CNVLIST_FREE(descriptor_array)
216 #endif
217
218 #undef CNVLIST_FREE
219