1*3f7675e0Sriastradh /* $NetBSD: prop_array_util.c,v 1.9 2022/08/03 21:13:46 riastradh Exp $ */
29b09c481Shaad
39b09c481Shaad /*-
4a792b843Sthorpej * Copyright (c) 2006, 2020 The NetBSD Foundation, Inc.
59b09c481Shaad * All rights reserved.
69b09c481Shaad *
79b09c481Shaad * This code is derived from software contributed to The NetBSD Foundation
89b09c481Shaad * by Jason R. Thorpe.
99b09c481Shaad *
109b09c481Shaad * Redistribution and use in source and binary forms, with or without
119b09c481Shaad * modification, are permitted provided that the following conditions
129b09c481Shaad * are met:
139b09c481Shaad * 1. Redistributions of source code must retain the above copyright
149b09c481Shaad * notice, this list of conditions and the following disclaimer.
159b09c481Shaad * 2. Redistributions in binary form must reproduce the above copyright
169b09c481Shaad * notice, this list of conditions and the following disclaimer in the
179b09c481Shaad * documentation and/or other materials provided with the distribution.
189b09c481Shaad *
199b09c481Shaad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209b09c481Shaad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219b09c481Shaad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229b09c481Shaad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239b09c481Shaad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249b09c481Shaad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259b09c481Shaad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269b09c481Shaad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279b09c481Shaad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289b09c481Shaad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299b09c481Shaad * POSSIBILITY OF SUCH DAMAGE.
309b09c481Shaad */
319b09c481Shaad
329b09c481Shaad /*
339b09c481Shaad * Utility routines to make it more convenient to work with values
349b09c481Shaad * stored in array.
359b09c481Shaad *
369b09c481Shaad * Note: There is no special magic going on here. We use the standard
379b09c481Shaad * proplib(3) APIs to do all of this work. Any application could do
389b09c481Shaad * exactly what we're doing here.
399b09c481Shaad */
409b09c481Shaad
419b09c481Shaad #include "prop_object_impl.h" /* hide kernel vs. not-kernel vs. standalone */
42c303bcbeSpooka #include <prop/proplib.h>
439b09c481Shaad
449b09c481Shaad bool
prop_array_get_bool(prop_array_t array,unsigned int indx,bool * valp)45a792b843Sthorpej prop_array_get_bool(prop_array_t array, unsigned int indx, bool *valp)
469b09c481Shaad {
479b09c481Shaad prop_bool_t b;
489b09c481Shaad
499b09c481Shaad b = prop_array_get(array, indx);
509b09c481Shaad if (prop_object_type(b) != PROP_TYPE_BOOL)
519b09c481Shaad return (false);
529b09c481Shaad
539b09c481Shaad *valp = prop_bool_true(b);
549b09c481Shaad
559b09c481Shaad return (true);
569b09c481Shaad }
579b09c481Shaad
589b09c481Shaad bool
prop_array_set_bool(prop_array_t array,unsigned int indx,bool val)59a792b843Sthorpej prop_array_set_bool(prop_array_t array, unsigned int indx, bool val)
609b09c481Shaad {
619b09c481Shaad
62a792b843Sthorpej return prop_array_set_and_rel(array, indx, prop_bool_create(val));
63a792b843Sthorpej }
64a792b843Sthorpej
65a792b843Sthorpej bool
prop_array_add_bool(prop_array_t array,bool val)66a792b843Sthorpej prop_array_add_bool(prop_array_t array, bool val)
67a792b843Sthorpej {
68a792b843Sthorpej
69a792b843Sthorpej return prop_array_add_and_rel(array, prop_bool_create(val));
70a792b843Sthorpej }
71a792b843Sthorpej
72a792b843Sthorpej #define TEMPLATE(name, typ) \
73a792b843Sthorpej bool \
74a792b843Sthorpej prop_array_get_ ## name (prop_array_t array, \
75a792b843Sthorpej unsigned int indx, \
76a792b843Sthorpej typ *valp) \
77a792b843Sthorpej { \
78a792b843Sthorpej return prop_number_ ## name ## _value( \
79a792b843Sthorpej prop_array_get(array, indx), valp); \
80a792b843Sthorpej }
TEMPLATE(schar,signed char)81a792b843Sthorpej TEMPLATE(schar, signed char)
82a792b843Sthorpej TEMPLATE(short, short)
83a792b843Sthorpej TEMPLATE(int, int)
84a792b843Sthorpej TEMPLATE(long, long)
85a792b843Sthorpej TEMPLATE(longlong, long long)
86a792b843Sthorpej TEMPLATE(intptr, intptr_t)
87a792b843Sthorpej TEMPLATE(int8, int8_t)
88a792b843Sthorpej TEMPLATE(int16, int16_t)
89a792b843Sthorpej TEMPLATE(int32, int32_t)
90a792b843Sthorpej TEMPLATE(int64, int64_t)
91a792b843Sthorpej
92a792b843Sthorpej TEMPLATE(uchar, unsigned char)
93a792b843Sthorpej TEMPLATE(ushort, unsigned short)
94a792b843Sthorpej TEMPLATE(uint, unsigned int)
95a792b843Sthorpej TEMPLATE(ulong, unsigned long)
96a792b843Sthorpej TEMPLATE(ulonglong, unsigned long long)
97a792b843Sthorpej TEMPLATE(uintptr, uintptr_t)
98a792b843Sthorpej TEMPLATE(uint8, uint8_t)
99a792b843Sthorpej TEMPLATE(uint16, uint16_t)
100a792b843Sthorpej TEMPLATE(uint32, uint32_t)
101a792b843Sthorpej TEMPLATE(uint64, uint64_t)
102a792b843Sthorpej
103a792b843Sthorpej #undef TEMPLATE
104a792b843Sthorpej
105a792b843Sthorpej static bool
106a792b843Sthorpej prop_array_set_signed_number(prop_array_t array, unsigned int indx,
107a792b843Sthorpej intmax_t val)
108a792b843Sthorpej {
109a792b843Sthorpej return prop_array_set_and_rel(array, indx,
110a792b843Sthorpej prop_number_create_signed(val));
111a792b843Sthorpej }
112a792b843Sthorpej
113a792b843Sthorpej static bool
prop_array_add_signed_number(prop_array_t array,intmax_t val)114a792b843Sthorpej prop_array_add_signed_number(prop_array_t array, intmax_t val)
115a792b843Sthorpej {
116a792b843Sthorpej return prop_array_add_and_rel(array, prop_number_create_signed(val));
117a792b843Sthorpej }
118a792b843Sthorpej
119a792b843Sthorpej static bool
prop_array_set_unsigned_number(prop_array_t array,unsigned int indx,uintmax_t val)120a792b843Sthorpej prop_array_set_unsigned_number(prop_array_t array, unsigned int indx,
121a792b843Sthorpej uintmax_t val)
122a792b843Sthorpej {
123a792b843Sthorpej return prop_array_set_and_rel(array, indx,
124a792b843Sthorpej prop_number_create_unsigned(val));
125a792b843Sthorpej }
126a792b843Sthorpej
127a792b843Sthorpej static bool
prop_array_add_unsigned_number(prop_array_t array,uintmax_t val)128e0cecd82Schristos prop_array_add_unsigned_number(prop_array_t array, uintmax_t val)
129a792b843Sthorpej {
130a792b843Sthorpej return prop_array_add_and_rel(array, prop_number_create_unsigned(val));
131a792b843Sthorpej }
132a792b843Sthorpej
133a792b843Sthorpej #define TEMPLATE(name, which, typ) \
134a792b843Sthorpej bool \
135a792b843Sthorpej prop_array_set_ ## name (prop_array_t array, \
136a792b843Sthorpej unsigned int indx, \
137a792b843Sthorpej typ val) \
138a792b843Sthorpej { \
139ad3c244aSchristos /*LINTED: for conversion from 'long long' to 'long'*/ \
140a792b843Sthorpej return prop_array_set_ ## which ## _number(array, indx, val); \
141a792b843Sthorpej } \
142a792b843Sthorpej \
143a792b843Sthorpej bool \
144a792b843Sthorpej prop_array_add_ ## name (prop_array_t array, \
145a792b843Sthorpej typ val) \
146a792b843Sthorpej { \
147ad3c244aSchristos /*LINTED: for conversion from 'long long' to 'long'*/ \
148a792b843Sthorpej return prop_array_add_ ## which ## _number(array, val); \
149a792b843Sthorpej }
150a792b843Sthorpej
151a792b843Sthorpej #define STEMPLATE(name, typ) TEMPLATE(name, signed, typ)
152a792b843Sthorpej #define UTEMPLATE(name, typ) TEMPLATE(name, unsigned, typ)
153a792b843Sthorpej
STEMPLATE(schar,signed char)154a792b843Sthorpej STEMPLATE(schar, signed char)
155a792b843Sthorpej STEMPLATE(short, short)
156a792b843Sthorpej STEMPLATE(int, int)
157a792b843Sthorpej STEMPLATE(long, long)
158a792b843Sthorpej STEMPLATE(longlong, long long)
159a792b843Sthorpej STEMPLATE(intptr, intptr_t)
160a792b843Sthorpej STEMPLATE(int8, int8_t)
161a792b843Sthorpej STEMPLATE(int16, int16_t)
162a792b843Sthorpej STEMPLATE(int32, int32_t)
163a792b843Sthorpej STEMPLATE(int64, int64_t)
164a792b843Sthorpej
165a792b843Sthorpej UTEMPLATE(uchar, unsigned char)
166a792b843Sthorpej UTEMPLATE(ushort, unsigned short)
167a792b843Sthorpej UTEMPLATE(uint, unsigned int)
168a792b843Sthorpej UTEMPLATE(ulong, unsigned long)
169a792b843Sthorpej UTEMPLATE(ulonglong, unsigned long long)
170a792b843Sthorpej UTEMPLATE(uintptr, uintptr_t)
171a792b843Sthorpej UTEMPLATE(uint8, uint8_t)
172a792b843Sthorpej UTEMPLATE(uint16, uint16_t)
173a792b843Sthorpej UTEMPLATE(uint32, uint32_t)
174a792b843Sthorpej UTEMPLATE(uint64, uint64_t)
175a792b843Sthorpej
176a792b843Sthorpej #undef STEMPLATE
177a792b843Sthorpej #undef UTEMPLATE
178a792b843Sthorpej #undef TEMPLATE
179a792b843Sthorpej
180a792b843Sthorpej bool
181a792b843Sthorpej prop_array_get_string(prop_array_t array, unsigned int indx, const char **cpp)
182a792b843Sthorpej {
183a792b843Sthorpej prop_string_t str;
184a792b843Sthorpej const char *cp;
185a792b843Sthorpej
186a792b843Sthorpej str = prop_array_get(array, indx);
187a792b843Sthorpej if (prop_object_type(str) != PROP_TYPE_STRING)
1889b09c481Shaad return (false);
189a792b843Sthorpej
190a792b843Sthorpej cp = prop_string_value(str);
191a792b843Sthorpej if (cp == NULL)
192a792b843Sthorpej return (false);
193a792b843Sthorpej
194a792b843Sthorpej *cpp = cp;
195a792b843Sthorpej return (true);
196a792b843Sthorpej }
197a792b843Sthorpej
198a792b843Sthorpej bool
prop_array_set_string(prop_array_t array,unsigned int indx,const char * cp)199a792b843Sthorpej prop_array_set_string(prop_array_t array, unsigned int indx, const char *cp)
200a792b843Sthorpej {
201a792b843Sthorpej return prop_array_set_and_rel(array, indx,
202a792b843Sthorpej prop_string_create_copy(cp));
203a792b843Sthorpej }
204a792b843Sthorpej
205a792b843Sthorpej bool
prop_array_add_string(prop_array_t array,const char * cp)206a792b843Sthorpej prop_array_add_string(prop_array_t array, const char *cp)
207a792b843Sthorpej {
208a792b843Sthorpej return prop_array_add_and_rel(array, prop_string_create_copy(cp));
209a792b843Sthorpej }
210a792b843Sthorpej
211a792b843Sthorpej bool
prop_array_set_string_nocopy(prop_array_t array,unsigned int indx,const char * cp)212a792b843Sthorpej prop_array_set_string_nocopy(prop_array_t array, unsigned int indx,
213a792b843Sthorpej const char *cp)
214a792b843Sthorpej {
215a792b843Sthorpej return prop_array_set_and_rel(array, indx,
216a792b843Sthorpej prop_string_create_nocopy(cp));
217a792b843Sthorpej }
218a792b843Sthorpej
219a792b843Sthorpej bool
prop_array_add_string_nocopy(prop_array_t array,const char * cp)220a792b843Sthorpej prop_array_add_string_nocopy(prop_array_t array, const char *cp)
221a792b843Sthorpej {
222a792b843Sthorpej return prop_array_add_and_rel(array, prop_string_create_nocopy(cp));
223a792b843Sthorpej }
224a792b843Sthorpej
225a792b843Sthorpej bool
prop_array_get_data(prop_array_t array,unsigned int indx,const void ** vp,size_t * sizep)226a792b843Sthorpej prop_array_get_data(prop_array_t array, unsigned int indx, const void **vp,
227a792b843Sthorpej size_t *sizep)
228a792b843Sthorpej {
229a792b843Sthorpej prop_data_t data;
230a792b843Sthorpej const void *v;
231a792b843Sthorpej
232a792b843Sthorpej data = prop_array_get(array, indx);
233a792b843Sthorpej if (prop_object_type(data) != PROP_TYPE_DATA)
234a792b843Sthorpej return (false);
235a792b843Sthorpej
236a792b843Sthorpej v = prop_data_value(data);
237a792b843Sthorpej if (v == NULL)
238a792b843Sthorpej return (false);
239a792b843Sthorpej
240a792b843Sthorpej *vp = v;
241a792b843Sthorpej if (sizep != NULL)
242a792b843Sthorpej *sizep = prop_data_size(data);
243a792b843Sthorpej return (true);
244a792b843Sthorpej }
245a792b843Sthorpej
246a792b843Sthorpej bool
prop_array_set_data(prop_array_t array,unsigned int indx,const void * v,size_t size)247a792b843Sthorpej prop_array_set_data(prop_array_t array, unsigned int indx, const void *v,
248a792b843Sthorpej size_t size)
249a792b843Sthorpej {
250a792b843Sthorpej return prop_array_set_and_rel(array, indx,
251a792b843Sthorpej prop_data_create_copy(v, size));
252a792b843Sthorpej }
253a792b843Sthorpej
254a792b843Sthorpej bool
prop_array_set_data_nocopy(prop_array_t array,unsigned int indx,const void * v,size_t size)255a792b843Sthorpej prop_array_set_data_nocopy(prop_array_t array, unsigned int indx, const void *v,
256a792b843Sthorpej size_t size)
257a792b843Sthorpej {
258a792b843Sthorpej return prop_array_set_and_rel(array, indx,
259a792b843Sthorpej prop_data_create_nocopy(v, size));
260a792b843Sthorpej }
261a792b843Sthorpej
262a792b843Sthorpej bool
prop_array_add_data(prop_array_t array,const void * v,size_t size)263a792b843Sthorpej prop_array_add_data(prop_array_t array, const void *v, size_t size)
264a792b843Sthorpej {
265a792b843Sthorpej return prop_array_add_and_rel(array,
266a792b843Sthorpej prop_data_create_copy(v, size));
267a792b843Sthorpej }
268a792b843Sthorpej
269a792b843Sthorpej bool
prop_array_add_data_nocopy(prop_array_t array,const void * v,size_t size)270a792b843Sthorpej prop_array_add_data_nocopy(prop_array_t array, const void *v, size_t size)
271a792b843Sthorpej {
272a792b843Sthorpej return prop_array_add_and_rel(array,
273a792b843Sthorpej prop_data_create_nocopy(v, size));
274a792b843Sthorpej }
275a792b843Sthorpej
276a792b843Sthorpej _PROP_DEPRECATED(prop_array_get_cstring,
277a792b843Sthorpej "this program uses prop_array_get_cstring(), "
278a792b843Sthorpej "which is deprecated; use prop_array_get_string() and copy instead.")
279a792b843Sthorpej bool
prop_array_get_cstring(prop_array_t array,unsigned int indx,char ** cpp)280a792b843Sthorpej prop_array_get_cstring(prop_array_t array, unsigned int indx, char **cpp)
281a792b843Sthorpej {
282a792b843Sthorpej prop_string_t str;
283a792b843Sthorpej char *cp;
284a792b843Sthorpej size_t len;
285a792b843Sthorpej bool rv;
286a792b843Sthorpej
287a792b843Sthorpej str = prop_array_get(array, indx);
288a792b843Sthorpej if (prop_object_type(str) != PROP_TYPE_STRING)
289a792b843Sthorpej return (false);
290a792b843Sthorpej
291a792b843Sthorpej len = prop_string_size(str);
292a792b843Sthorpej cp = _PROP_MALLOC(len + 1, M_TEMP);
293a792b843Sthorpej if (cp == NULL)
294a792b843Sthorpej return (false);
295a792b843Sthorpej
296a792b843Sthorpej rv = prop_string_copy_value(str, cp, len + 1);
297a792b843Sthorpej if (rv)
298a792b843Sthorpej *cpp = cp;
299a792b843Sthorpej else
300a792b843Sthorpej _PROP_FREE(cp, M_TEMP);
3019b09c481Shaad
3029b09c481Shaad return (rv);
3039b09c481Shaad }
3049b09c481Shaad
305a792b843Sthorpej _PROP_DEPRECATED(prop_array_get_cstring_nocopy,
306a792b843Sthorpej "this program uses prop_array_get_cstring_nocopy(), "
307a792b843Sthorpej "which is deprecated; use prop_array_get_string() instead.")
308a792b843Sthorpej bool
prop_array_get_cstring_nocopy(prop_array_t array,unsigned int indx,const char ** cpp)309a792b843Sthorpej prop_array_get_cstring_nocopy(prop_array_t array, unsigned int indx,
310a792b843Sthorpej const char **cpp)
311a792b843Sthorpej {
312a792b843Sthorpej return prop_array_get_string(array, indx, cpp);
3139b09c481Shaad }
3149b09c481Shaad
315a792b843Sthorpej _PROP_DEPRECATED(prop_array_set_cstring,
316a792b843Sthorpej "this program uses prop_array_set_cstring(), "
317a792b843Sthorpej "which is deprecated; use prop_array_set_string() instead.")
318a792b843Sthorpej bool
prop_array_set_cstring(prop_array_t array,unsigned int indx,const char * cp)319a792b843Sthorpej prop_array_set_cstring(prop_array_t array, unsigned int indx, const char *cp)
320a792b843Sthorpej {
321a792b843Sthorpej return prop_array_set_string(array, indx, cp);
3229b09c481Shaad }
3239b09c481Shaad
324a792b843Sthorpej _PROP_DEPRECATED(prop_array_add_cstring,
325a792b843Sthorpej "this program uses prop_array_add_cstring(), "
326a792b843Sthorpej "which is deprecated; use prop_array_add_string() instead.")
327a792b843Sthorpej bool
prop_array_add_cstring(prop_array_t array,const char * cp)328a792b843Sthorpej prop_array_add_cstring(prop_array_t array, const char *cp)
329a792b843Sthorpej {
330a792b843Sthorpej return prop_array_add_string(array, cp);
331a792b843Sthorpej }
3329b09c481Shaad
333a792b843Sthorpej _PROP_DEPRECATED(prop_array_set_cstring_nocopy,
334a792b843Sthorpej "this program uses prop_array_set_cstring_nocopy(), "
335a792b843Sthorpej "which is deprecated; use prop_array_set_string_nocopy() instead.")
336a792b843Sthorpej bool
prop_array_set_cstring_nocopy(prop_array_t array,unsigned int indx,const char * cp)337a792b843Sthorpej prop_array_set_cstring_nocopy(prop_array_t array, unsigned int indx,
338a792b843Sthorpej const char *cp)
339a792b843Sthorpej {
340a792b843Sthorpej return prop_array_set_string_nocopy(array, indx, cp);
341a792b843Sthorpej }
342a792b843Sthorpej
343a792b843Sthorpej _PROP_DEPRECATED(prop_array_add_cstring_nocopy,
344a792b843Sthorpej "this program uses prop_array_add_cstring_nocopy(), "
345a792b843Sthorpej "which is deprecated; use prop_array_add_string_nocopy() instead.")
346a792b843Sthorpej bool
prop_array_add_cstring_nocopy(prop_array_t array,const char * cp)347a792b843Sthorpej prop_array_add_cstring_nocopy(prop_array_t array, const char *cp)
348a792b843Sthorpej {
349a792b843Sthorpej return prop_array_add_string_nocopy(array, cp);
350a792b843Sthorpej }
351d9210c24Sbouyer
352d9210c24Sbouyer bool
prop_array_add_and_rel(prop_array_t array,prop_object_t po)353d9210c24Sbouyer prop_array_add_and_rel(prop_array_t array, prop_object_t po)
354d9210c24Sbouyer {
355a792b843Sthorpej bool rv;
356a792b843Sthorpej
357d9210c24Sbouyer if (po == NULL)
358d9210c24Sbouyer return false;
359a792b843Sthorpej rv = prop_array_add(array, po);
360d9210c24Sbouyer prop_object_release(po);
361a792b843Sthorpej return rv;
362a792b843Sthorpej }
363a792b843Sthorpej
364a792b843Sthorpej bool
prop_array_set_and_rel(prop_array_t array,unsigned int indx,prop_object_t po)365a792b843Sthorpej prop_array_set_and_rel(prop_array_t array, unsigned int indx,
366a792b843Sthorpej prop_object_t po)
367a792b843Sthorpej {
368a792b843Sthorpej bool rv;
369a792b843Sthorpej
370a792b843Sthorpej if (po == NULL)
371a792b843Sthorpej return false;
372a792b843Sthorpej rv = prop_array_set(array, indx, po);
373a792b843Sthorpej prop_object_release(po);
374a792b843Sthorpej return rv;
375d9210c24Sbouyer }
376