xref: /onnv-gate/usr/src/lib/libtnf/array.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	Copyright (c) 1994, by Sun Microsytems, Inc.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include "libtnf.h"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * XXX This module assumes that all arrays are self-sized records.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static struct taginfo *	get_array_info(
39*0Sstevel@tonic-gate 	tnf_datum_t,
40*0Sstevel@tonic-gate 	struct taginfo **base,
41*0Sstevel@tonic-gate 	struct taginfo **elt,
42*0Sstevel@tonic-gate 	struct taginfo **elt_base);
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * XXX Assumes arrays are (self-sized) records
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate void
_tnf_check_array(tnf_datum_t datum)49*0Sstevel@tonic-gate _tnf_check_array(tnf_datum_t datum)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	struct taginfo	*info;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	CHECK_RECORD(datum);		/* XXX */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	info = DATUM_INFO(datum);
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if (!INFO_ARRAY(info))
58*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_TYPEMISMATCH);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * Helper
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static struct taginfo *
get_array_info(tnf_datum_t datum,struct taginfo ** basep,struct taginfo ** eltp,struct taginfo ** elt_basep)67*0Sstevel@tonic-gate get_array_info(
68*0Sstevel@tonic-gate 	tnf_datum_t datum,
69*0Sstevel@tonic-gate 	struct taginfo **basep,
70*0Sstevel@tonic-gate 	struct taginfo **eltp,
71*0Sstevel@tonic-gate 	struct taginfo **elt_basep)
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate 	struct taginfo	*info, *base, *elt, *elt_base;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	info	= DATUM_INFO(datum);
76*0Sstevel@tonic-gate 	base	= INFO_DERIVED(info) ? info->base : info;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (INFO_DERIVED(base) || (!INFO_ARRAY(base)))
79*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_INTERNAL);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	elt 	= base->base;	/* XXX base slot is reused for elttype */
82*0Sstevel@tonic-gate 	elt_base = INFO_DERIVED(elt) ? elt->base : elt;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	*basep	= base;
85*0Sstevel@tonic-gate 	*eltp	= elt;
86*0Sstevel@tonic-gate 	*elt_basep = elt_base;
87*0Sstevel@tonic-gate 	return (info);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate  * Return number of elements in array
92*0Sstevel@tonic-gate  */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate unsigned
tnf_get_element_count(tnf_datum_t datum)95*0Sstevel@tonic-gate tnf_get_element_count(tnf_datum_t datum)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	size_t		hdr_size, elt_size, self_size;
98*0Sstevel@tonic-gate 	struct taginfo	*base, *elt, *elt_base;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	CHECK_ARRAY(datum);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	(void) get_array_info(datum, &base, &elt, &elt_base);
103*0Sstevel@tonic-gate 	hdr_size	= base->hdrsize;
104*0Sstevel@tonic-gate 	elt_size	= INFO_ELEMENT_SIZE(elt_base);
105*0Sstevel@tonic-gate 	self_size	= _tnf_get_self_size(DATUM_TNF(datum),
106*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
107*0Sstevel@tonic-gate 				DATUM_RECORD(datum));
108*0Sstevel@tonic-gate 	return (((self_size - hdr_size) / elt_size));
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate  * Fetch indexed element
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate tnf_datum_t
tnf_get_element(tnf_datum_t datum,unsigned index)116*0Sstevel@tonic-gate tnf_get_element(tnf_datum_t datum, unsigned index)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	size_t		hdr_size, elt_size, self_size;
119*0Sstevel@tonic-gate 	struct taginfo	*base, *elt, *elt_base;
120*0Sstevel@tonic-gate 	unsigned	count, offset;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	CHECK_ARRAY(datum);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	(void) get_array_info(datum, &base, &elt, &elt_base);
125*0Sstevel@tonic-gate 	hdr_size	= base->hdrsize;
126*0Sstevel@tonic-gate 	elt_size	= INFO_ELEMENT_SIZE(elt_base);
127*0Sstevel@tonic-gate 	self_size	= _tnf_get_self_size(DATUM_TNF(datum),
128*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
129*0Sstevel@tonic-gate 				DATUM_RECORD(datum));
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	count		= (self_size - hdr_size) / elt_size;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	if (index >= count)
134*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_BADINDEX);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	offset		= hdr_size + (index * elt_size);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/*
139*0Sstevel@tonic-gate 	 * If tagged, use the tag to construct datum
140*0Sstevel@tonic-gate 	 */
141*0Sstevel@tonic-gate 	if (INFO_TAGGED(elt)) {
142*0Sstevel@tonic-gate 		TNF		*tnf;
143*0Sstevel@tonic-gate 		tnf_ref32_t	*rec;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		tnf = DATUM_TNF(datum);
146*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
147*0Sstevel@tonic-gate 		rec = _GET_REF32(tnf, (tnf_ref32_t *)
148*0Sstevel@tonic-gate 			(DATUM_VAL(datum) + offset));
149*0Sstevel@tonic-gate 		/* NULL elements are allowed */
150*0Sstevel@tonic-gate 		return ((rec == TNF_NULL)? TNF_DATUM_NULL :
151*0Sstevel@tonic-gate 			RECORD_DATUM(tnf, rec));
152*0Sstevel@tonic-gate 	} else
153*0Sstevel@tonic-gate 		return (DATUM(elt, DATUM_VAL(datum) + offset));
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate /*
157*0Sstevel@tonic-gate  * Return element type of array
158*0Sstevel@tonic-gate  */
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate tnf_datum_t
tnf_get_element_type(tnf_datum_t datum)161*0Sstevel@tonic-gate tnf_get_element_type(tnf_datum_t datum)
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate 	struct taginfo	*base, *elt, *elt_base;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	CHECK_ARRAY(datum);
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	(void) get_array_info(datum, &base, &elt, &elt_base);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	return (RECORD_DATUM(DATUM_TNF(datum), elt->tag));
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate  * Return a char pointer for string record
174*0Sstevel@tonic-gate  */
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate char *
tnf_get_chars(tnf_datum_t datum)177*0Sstevel@tonic-gate tnf_get_chars(tnf_datum_t datum)
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	struct taginfo	*info, *base, *elt, *elt_base;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	CHECK_ARRAY(datum);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	info = get_array_info(datum, &base, &elt, &elt_base);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	if (!INFO_STRING(info))
186*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_TYPEMISMATCH);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	return (DATUM_VAL(datum) + base->hdrsize);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate  * Return the base pointer of array
193*0Sstevel@tonic-gate  */
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate caddr_t
tnf_get_elements(tnf_datum_t datum)196*0Sstevel@tonic-gate tnf_get_elements(tnf_datum_t datum)
197*0Sstevel@tonic-gate {
198*0Sstevel@tonic-gate 	struct taginfo	*base, *elt, *elt_base;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	CHECK_ARRAY(datum);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	(void) get_array_info(datum, &base, &elt, &elt_base);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	return ((caddr_t)(DATUM_VAL(datum) + base->hdrsize));
205*0Sstevel@tonic-gate }
206