1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <strings.h>
31*7c478bd9Sstevel@tonic-gate #include <pool.h>
32*7c478bd9Sstevel@tonic-gate #include "pool_internal.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * libpool Value Manipulation Routines
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * pool_value.c implements the value (pool_value_t) functionality for
38*7c478bd9Sstevel@tonic-gate  * libpool. The datatypes supported are: uint64_t, int64_t, double,
39*7c478bd9Sstevel@tonic-gate  * uchar_t (boolean), const char * (string). Values are used to
40*7c478bd9Sstevel@tonic-gate  * represent data stored to and retrieved from the datastore in a
41*7c478bd9Sstevel@tonic-gate  * simple discriminated union.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * Values are dynamically allocated using pool_value_alloc() and
44*7c478bd9Sstevel@tonic-gate  * destroyed using pool_value_free().
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * Values may be allocated statically for internal use in
47*7c478bd9Sstevel@tonic-gate  * libpool. Statically allocated pool_value_t variables must be
48*7c478bd9Sstevel@tonic-gate  * initialised with the POOL_VALUE_INITIALIZER macro, otherwise the
49*7c478bd9Sstevel@tonic-gate  * results are unpredictable.
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  * A pool_value_t variable can be used to store values in any of the
52*7c478bd9Sstevel@tonic-gate  * supported datatypes.
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  * A pool_value_t's name and string value are limited in size to
55*7c478bd9Sstevel@tonic-gate  * PV_NAME_MAX_LEN and PV_VALUE_MAX_LEN respectively. Attempting to
56*7c478bd9Sstevel@tonic-gate  * store values which are greater than this in length will fail with a
57*7c478bd9Sstevel@tonic-gate  * POE_BADPARAM error.
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate  * Get the uint64_t data held by the value. If the data type isn't
62*7c478bd9Sstevel@tonic-gate  * uint64_t return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate int
pool_value_get_uint64(const pool_value_t * pv,uint64_t * result)65*7c478bd9Sstevel@tonic-gate pool_value_get_uint64(const pool_value_t *pv, uint64_t *result)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class != POC_UINT) {
68*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BAD_PROP_TYPE);
69*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
70*7c478bd9Sstevel@tonic-gate 	}
71*7c478bd9Sstevel@tonic-gate 	*result = pv->pv_u.u;
72*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * Get the int64_t data held by the value. If the data type isn't
77*7c478bd9Sstevel@tonic-gate  * int64_t return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate int
pool_value_get_int64(const pool_value_t * pv,int64_t * result)80*7c478bd9Sstevel@tonic-gate pool_value_get_int64(const pool_value_t *pv, int64_t *result)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class != POC_INT) {
83*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BAD_PROP_TYPE);
84*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 	*result = pv->pv_u.i;
87*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate  * Get the double data held by the value. If the data type isn't
92*7c478bd9Sstevel@tonic-gate  * double return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate int
pool_value_get_double(const pool_value_t * pv,double * result)96*7c478bd9Sstevel@tonic-gate pool_value_get_double(const pool_value_t *pv, double *result)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class != POC_DOUBLE) {
99*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BAD_PROP_TYPE);
100*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 	*result = pv->pv_u.d;
103*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * Get the boolean data held by the value. If the data type isn't
108*7c478bd9Sstevel@tonic-gate  * boolean return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate int
pool_value_get_bool(const pool_value_t * pv,uchar_t * result)111*7c478bd9Sstevel@tonic-gate pool_value_get_bool(const pool_value_t *pv, uchar_t *result)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class != POC_BOOL) {
114*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BAD_PROP_TYPE);
115*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	*result = pv->pv_u.b;
118*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * Get the string data held by the value. If the data type isn't
123*7c478bd9Sstevel@tonic-gate  * string return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE.
124*7c478bd9Sstevel@tonic-gate  */
125*7c478bd9Sstevel@tonic-gate int
pool_value_get_string(const pool_value_t * pv,const char ** result)126*7c478bd9Sstevel@tonic-gate pool_value_get_string(const pool_value_t *pv, const char **result)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class != POC_STRING) {
129*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BAD_PROP_TYPE);
130*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 	*result = pv->pv_u.s;
133*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate /*
137*7c478bd9Sstevel@tonic-gate  * Get the type of the data held by the value. If the value has never
138*7c478bd9Sstevel@tonic-gate  * been used to store data, then the type is POC_INVAL.
139*7c478bd9Sstevel@tonic-gate  */
140*7c478bd9Sstevel@tonic-gate pool_value_class_t
pool_value_get_type(const pool_value_t * pv)141*7c478bd9Sstevel@tonic-gate pool_value_get_type(const pool_value_t *pv)
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	return (pv->pv_class);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate /*
147*7c478bd9Sstevel@tonic-gate  * Set the value's data to the supplied uint64_t data. Update the type
148*7c478bd9Sstevel@tonic-gate  * of the value data to POC_UINT.
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate void
pool_value_set_uint64(pool_value_t * pv,uint64_t val)151*7c478bd9Sstevel@tonic-gate pool_value_set_uint64(pool_value_t *pv, uint64_t val)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class == POC_STRING)
154*7c478bd9Sstevel@tonic-gate 		atom_free(pv->pv_u.s);
155*7c478bd9Sstevel@tonic-gate 	pv->pv_class = POC_UINT;
156*7c478bd9Sstevel@tonic-gate 	pv->pv_u.u = val;
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate  * Set the value's data to the supplied int64_t data. Update the type
161*7c478bd9Sstevel@tonic-gate  * of the value data to POC_INT.
162*7c478bd9Sstevel@tonic-gate  */
163*7c478bd9Sstevel@tonic-gate void
pool_value_set_int64(pool_value_t * pv,int64_t val)164*7c478bd9Sstevel@tonic-gate pool_value_set_int64(pool_value_t *pv, int64_t val)
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class == POC_STRING)
167*7c478bd9Sstevel@tonic-gate 		atom_free(pv->pv_u.s);
168*7c478bd9Sstevel@tonic-gate 	pv->pv_class = POC_INT;
169*7c478bd9Sstevel@tonic-gate 	pv->pv_u.i = val;
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate /*
173*7c478bd9Sstevel@tonic-gate  * Set the value's data to the supplied double data. Update the type
174*7c478bd9Sstevel@tonic-gate  * of the value data to POC_DOUBLE.
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate void
pool_value_set_double(pool_value_t * pv,double val)178*7c478bd9Sstevel@tonic-gate pool_value_set_double(pool_value_t *pv, double val)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class == POC_STRING)
181*7c478bd9Sstevel@tonic-gate 		atom_free(pv->pv_u.s);
182*7c478bd9Sstevel@tonic-gate 	pv->pv_class = POC_DOUBLE;
183*7c478bd9Sstevel@tonic-gate 	pv->pv_u.d = val;
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate /*
187*7c478bd9Sstevel@tonic-gate  * Set the value's data to the supplied uchar_t data. Update the type
188*7c478bd9Sstevel@tonic-gate  * of the value data to POC_BOOL.
189*7c478bd9Sstevel@tonic-gate  */
190*7c478bd9Sstevel@tonic-gate void
pool_value_set_bool(pool_value_t * pv,uchar_t val)191*7c478bd9Sstevel@tonic-gate pool_value_set_bool(pool_value_t *pv, uchar_t val)
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class == POC_STRING)
194*7c478bd9Sstevel@tonic-gate 		atom_free(pv->pv_u.s);
195*7c478bd9Sstevel@tonic-gate 	pv->pv_class = POC_BOOL;
196*7c478bd9Sstevel@tonic-gate 	pv->pv_u.b = !!val;	/* Lock value at 0 or 1 */
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate /*
200*7c478bd9Sstevel@tonic-gate  * Try to make an internal copy of the val, returning PO_SUCCESS or
201*7c478bd9Sstevel@tonic-gate  * PO_FAIL if the copy works or fails.
202*7c478bd9Sstevel@tonic-gate  */
203*7c478bd9Sstevel@tonic-gate int
pool_value_set_string(pool_value_t * pv,const char * val)204*7c478bd9Sstevel@tonic-gate pool_value_set_string(pool_value_t *pv, const char *val)
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class == POC_STRING)
207*7c478bd9Sstevel@tonic-gate 		atom_free(pv->pv_u.s);
208*7c478bd9Sstevel@tonic-gate 	pv->pv_class = POC_STRING;
209*7c478bd9Sstevel@tonic-gate 	if (val == NULL || strlen(val) >= PV_VALUE_MAX_LEN) {
210*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
211*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
212*7c478bd9Sstevel@tonic-gate 	} else {
213*7c478bd9Sstevel@tonic-gate 		if ((pv->pv_u.s = atom_string(val)) == NULL)
214*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate /*
220*7c478bd9Sstevel@tonic-gate  * Allocate a pool_value_t structure and initialise it to 0. Set the
221*7c478bd9Sstevel@tonic-gate  * type to POC_INVAL and return a pointer to the new pool_value_t. If
222*7c478bd9Sstevel@tonic-gate  * memory allocation fails, set POE_SYSTEM and return NULL.
223*7c478bd9Sstevel@tonic-gate  */
224*7c478bd9Sstevel@tonic-gate pool_value_t *
pool_value_alloc(void)225*7c478bd9Sstevel@tonic-gate pool_value_alloc(void)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate 	pool_value_t *val;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	if ((val = malloc(sizeof (pool_value_t))) == NULL) {
230*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
231*7c478bd9Sstevel@tonic-gate 		return (NULL);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 	(void) memset(val, 0, sizeof (pool_value_t));
234*7c478bd9Sstevel@tonic-gate 	val->pv_class = POC_INVAL;
235*7c478bd9Sstevel@tonic-gate 	return (val);
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate /*
239*7c478bd9Sstevel@tonic-gate  * Free any atoms associated with the value and then free the value
240*7c478bd9Sstevel@tonic-gate  * itself.
241*7c478bd9Sstevel@tonic-gate  */
242*7c478bd9Sstevel@tonic-gate void
pool_value_free(pool_value_t * pv)243*7c478bd9Sstevel@tonic-gate pool_value_free(pool_value_t *pv)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	if (pv->pv_name)
246*7c478bd9Sstevel@tonic-gate 		atom_free(pv->pv_name);
247*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class == POC_STRING)
248*7c478bd9Sstevel@tonic-gate 		atom_free(pv->pv_u.s);
249*7c478bd9Sstevel@tonic-gate 	free(pv);
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate /*
253*7c478bd9Sstevel@tonic-gate  * Return a pointer to the name of the value. This may be NULL if the
254*7c478bd9Sstevel@tonic-gate  * name has never been set.
255*7c478bd9Sstevel@tonic-gate  */
256*7c478bd9Sstevel@tonic-gate const char *
pool_value_get_name(const pool_value_t * pv)257*7c478bd9Sstevel@tonic-gate pool_value_get_name(const pool_value_t *pv)
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate 	return (pv->pv_name);
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate /*
263*7c478bd9Sstevel@tonic-gate  * Set the name of the value to the supplied name.
264*7c478bd9Sstevel@tonic-gate  */
265*7c478bd9Sstevel@tonic-gate int
pool_value_set_name(pool_value_t * pv,const char * name)266*7c478bd9Sstevel@tonic-gate pool_value_set_name(pool_value_t *pv, const char *name)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	if (name == NULL || strlen(name) >= PV_NAME_MAX_LEN) {
269*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
270*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
271*7c478bd9Sstevel@tonic-gate 	} else {
272*7c478bd9Sstevel@tonic-gate 		if (pv->pv_name)
273*7c478bd9Sstevel@tonic-gate 			atom_free(pv->pv_name);
274*7c478bd9Sstevel@tonic-gate 		if ((pv->pv_name = atom_string(name)) == NULL)
275*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate /*
281*7c478bd9Sstevel@tonic-gate  * Use the supplied nvpair_t to set the name, type and value of the
282*7c478bd9Sstevel@tonic-gate  * supplied pool_value_t.
283*7c478bd9Sstevel@tonic-gate  *
284*7c478bd9Sstevel@tonic-gate  * Return: PO_SUCCESS/PO_FAIL
285*7c478bd9Sstevel@tonic-gate  */
286*7c478bd9Sstevel@tonic-gate int
pool_value_from_nvpair(pool_value_t * pv,nvpair_t * pn)287*7c478bd9Sstevel@tonic-gate pool_value_from_nvpair(pool_value_t *pv, nvpair_t *pn)
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate 	uchar_t bval;
290*7c478bd9Sstevel@tonic-gate 	uint64_t uval;
291*7c478bd9Sstevel@tonic-gate 	int64_t ival;
292*7c478bd9Sstevel@tonic-gate 	double dval;
293*7c478bd9Sstevel@tonic-gate 	uint_t nelem;
294*7c478bd9Sstevel@tonic-gate 	uchar_t *dval_b;
295*7c478bd9Sstevel@tonic-gate 	char *sval;
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	if (pool_value_set_name(pv, nvpair_name(pn)) != PO_SUCCESS)
298*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
299*7c478bd9Sstevel@tonic-gate 	switch (nvpair_type(pn)) {
300*7c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE:
301*7c478bd9Sstevel@tonic-gate 		if (nvpair_value_byte(pn, &bval) != 0) {
302*7c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
303*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
304*7c478bd9Sstevel@tonic-gate 		}
305*7c478bd9Sstevel@tonic-gate 		pool_value_set_bool(pv, bval);
306*7c478bd9Sstevel@tonic-gate 		break;
307*7c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
308*7c478bd9Sstevel@tonic-gate 		if (nvpair_value_byte_array(pn, &dval_b, &nelem) != 0) {
309*7c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
310*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
311*7c478bd9Sstevel@tonic-gate 		}
312*7c478bd9Sstevel@tonic-gate 		(void) memcpy(&dval, dval_b, sizeof (double));
313*7c478bd9Sstevel@tonic-gate 		pool_value_set_double(pv, dval);
314*7c478bd9Sstevel@tonic-gate 		break;
315*7c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64:
316*7c478bd9Sstevel@tonic-gate 		if (nvpair_value_int64(pn, &ival) != 0) {
317*7c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
318*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
319*7c478bd9Sstevel@tonic-gate 		}
320*7c478bd9Sstevel@tonic-gate 		pool_value_set_int64(pv, ival);
321*7c478bd9Sstevel@tonic-gate 		break;
322*7c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64:
323*7c478bd9Sstevel@tonic-gate 		if (nvpair_value_uint64(pn, &uval) != 0) {
324*7c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
325*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 		pool_value_set_uint64(pv, uval);
328*7c478bd9Sstevel@tonic-gate 		break;
329*7c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING:
330*7c478bd9Sstevel@tonic-gate 		if (nvpair_value_string(pn, &sval) != 0) {
331*7c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
332*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
333*7c478bd9Sstevel@tonic-gate 		}
334*7c478bd9Sstevel@tonic-gate 		if (pool_value_set_string(pv, sval) != PO_SUCCESS)
335*7c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
336*7c478bd9Sstevel@tonic-gate 		break;
337*7c478bd9Sstevel@tonic-gate 	default:
338*7c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
339*7c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
340*7c478bd9Sstevel@tonic-gate 	}
341*7c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate /*
345*7c478bd9Sstevel@tonic-gate  * Check to see if the values held by two supplied values are
346*7c478bd9Sstevel@tonic-gate  * equal. First compare the pointers to see if we are comparing to
347*7c478bd9Sstevel@tonic-gate  * ourselves, if we are return PO_TRUE. If not, get the types and
348*7c478bd9Sstevel@tonic-gate  * ensure they match, if they don't return PO_FALSE. Then do a type
349*7c478bd9Sstevel@tonic-gate  * specific comparison returning PO_TRUE or PO_FALSE accordingly.
350*7c478bd9Sstevel@tonic-gate  */
351*7c478bd9Sstevel@tonic-gate int
pool_value_equal(pool_value_t * pv1,pool_value_t * pv2)352*7c478bd9Sstevel@tonic-gate pool_value_equal(pool_value_t *pv1, pool_value_t *pv2)
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	uint64_t uval1, uval2;
355*7c478bd9Sstevel@tonic-gate 	int64_t ival1, ival2;
356*7c478bd9Sstevel@tonic-gate 	double dval1, dval2;
357*7c478bd9Sstevel@tonic-gate 	uchar_t bval1, bval2;
358*7c478bd9Sstevel@tonic-gate 	const char *sval1, *sval2;
359*7c478bd9Sstevel@tonic-gate 	pool_value_class_t type;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	if (pv1 == pv2) /* optimisation */
362*7c478bd9Sstevel@tonic-gate 		return (PO_TRUE);
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	type = pool_value_get_type(pv1);
365*7c478bd9Sstevel@tonic-gate 	if (type != pool_value_get_type(pv2))
366*7c478bd9Sstevel@tonic-gate 	    return (PO_FALSE);
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	switch (type) {
369*7c478bd9Sstevel@tonic-gate 		case POC_UINT:
370*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_uint64(pv1, &uval1);
371*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_uint64(pv2, &uval2);
372*7c478bd9Sstevel@tonic-gate 			if (uval1 == uval2)
373*7c478bd9Sstevel@tonic-gate 				return (PO_TRUE);
374*7c478bd9Sstevel@tonic-gate 			break;
375*7c478bd9Sstevel@tonic-gate 		case POC_INT:
376*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_int64(pv1, &ival1);
377*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_int64(pv2, &ival2);
378*7c478bd9Sstevel@tonic-gate 			if (ival1 == ival2)
379*7c478bd9Sstevel@tonic-gate 				return (PO_TRUE);
380*7c478bd9Sstevel@tonic-gate 			break;
381*7c478bd9Sstevel@tonic-gate 		case POC_DOUBLE:
382*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_double(pv1, &dval1);
383*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_double(pv2, &dval2);
384*7c478bd9Sstevel@tonic-gate 			if (dval1 == dval2)
385*7c478bd9Sstevel@tonic-gate 				return (PO_TRUE);
386*7c478bd9Sstevel@tonic-gate 			break;
387*7c478bd9Sstevel@tonic-gate 		case POC_BOOL:
388*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_bool(pv1, &bval1);
389*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_bool(pv2, &bval2);
390*7c478bd9Sstevel@tonic-gate 			if (bval1 == bval2)
391*7c478bd9Sstevel@tonic-gate 				return (PO_TRUE);
392*7c478bd9Sstevel@tonic-gate 			break;
393*7c478bd9Sstevel@tonic-gate 		case POC_STRING:
394*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_string(pv1, &sval1);
395*7c478bd9Sstevel@tonic-gate 			(void) pool_value_get_string(pv2, &sval2);
396*7c478bd9Sstevel@tonic-gate 			if (strcmp(sval1, sval2) == 0)
397*7c478bd9Sstevel@tonic-gate 				return (PO_TRUE);
398*7c478bd9Sstevel@tonic-gate 			break;
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate 	return (PO_FALSE);
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
404*7c478bd9Sstevel@tonic-gate /*
405*7c478bd9Sstevel@tonic-gate  * Trace pool_value_t details using dprintf
406*7c478bd9Sstevel@tonic-gate  */
407*7c478bd9Sstevel@tonic-gate void
pool_value_dprintf(const pool_value_t * pv)408*7c478bd9Sstevel@tonic-gate pool_value_dprintf(const pool_value_t *pv)
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	const char *class_name[] = {
411*7c478bd9Sstevel@tonic-gate 		"POC_UINT",
412*7c478bd9Sstevel@tonic-gate 		"POC_INT",
413*7c478bd9Sstevel@tonic-gate 		"POC_DOUBLE",
414*7c478bd9Sstevel@tonic-gate 		"POC_BOOL",
415*7c478bd9Sstevel@tonic-gate 		"POC_STRING"
416*7c478bd9Sstevel@tonic-gate 	};
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	dprintf("name: %s\n", pv->pv_name ? pv->pv_name : "NULL");
419*7c478bd9Sstevel@tonic-gate 	if (pv->pv_class >= POC_UINT && pv->pv_class <= POC_STRING)
420*7c478bd9Sstevel@tonic-gate 		dprintf("type: %s\n", class_name[pv->pv_class]);
421*7c478bd9Sstevel@tonic-gate 	else
422*7c478bd9Sstevel@tonic-gate 		dprintf("type: POC_INVAL\n");
423*7c478bd9Sstevel@tonic-gate 	switch (pv->pv_class) {
424*7c478bd9Sstevel@tonic-gate 	case POC_UINT:
425*7c478bd9Sstevel@tonic-gate 		dprintf("value: %llu\n", pv->pv_u.u);
426*7c478bd9Sstevel@tonic-gate 		break;
427*7c478bd9Sstevel@tonic-gate 	case POC_INT:
428*7c478bd9Sstevel@tonic-gate 		dprintf("value: %lld\n", pv->pv_u.i);
429*7c478bd9Sstevel@tonic-gate 		break;
430*7c478bd9Sstevel@tonic-gate 	case POC_DOUBLE:
431*7c478bd9Sstevel@tonic-gate 		dprintf("value: %f\n", pv->pv_u.d);
432*7c478bd9Sstevel@tonic-gate 		break;
433*7c478bd9Sstevel@tonic-gate 	case POC_BOOL:
434*7c478bd9Sstevel@tonic-gate 		dprintf("value: %s\n", pv->pv_u.b ? "true" : "false");
435*7c478bd9Sstevel@tonic-gate 		break;
436*7c478bd9Sstevel@tonic-gate 	case POC_STRING:
437*7c478bd9Sstevel@tonic-gate 		dprintf("value: %s\n", pv->pv_u.s);
438*7c478bd9Sstevel@tonic-gate 		break;
439*7c478bd9Sstevel@tonic-gate 	default:
440*7c478bd9Sstevel@tonic-gate 		dprintf("value: invalid\n");
441*7c478bd9Sstevel@tonic-gate 		break;
442*7c478bd9Sstevel@tonic-gate 	}
443*7c478bd9Sstevel@tonic-gate }
444*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
445