xref: /onnv-gate/usr/src/lib/libtnf/struct.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  *
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate static struct slotinfo *get_slotinfo(tnf_datum_t);
35*0Sstevel@tonic-gate static struct slot *	get_slot_named(struct slotinfo *, char *);
36*0Sstevel@tonic-gate static struct slot *	get_slot_indexed(struct slotinfo *, unsigned);
37*0Sstevel@tonic-gate static tnf_datum_t	get_slot(tnf_datum_t, struct slot *);
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate void
_tnf_check_slots(tnf_datum_t datum)44*0Sstevel@tonic-gate _tnf_check_slots(tnf_datum_t datum)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	struct taginfo	*info;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	CHECK_DATUM(datum);
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	info = DATUM_INFO(datum);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	/* Must be an aggregate */
53*0Sstevel@tonic-gate 	if (!(INFO_STRUCT(info) || INFO_ARRAY(info)))
54*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_TYPEMISMATCH);
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * Helpers
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static struct slotinfo *
get_slotinfo(tnf_datum_t datum)62*0Sstevel@tonic-gate get_slotinfo(tnf_datum_t datum)
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	struct taginfo	*info, *base_info;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	info 		= DATUM_INFO(datum);
67*0Sstevel@tonic-gate 	base_info	= INFO_DERIVED(info)? info->base: info;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	/* XXX base must not be a scalar tag */
70*0Sstevel@tonic-gate 	if (INFO_SCALAR(base_info))
71*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_BADTNF);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	return (base_info->slotinfo);
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate static struct slot *
get_slot_indexed(struct slotinfo * slotinfo,unsigned index)77*0Sstevel@tonic-gate get_slot_indexed(struct slotinfo *slotinfo, unsigned index)
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	unsigned 	count;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	count 		= slotinfo->slot_count;
82*0Sstevel@tonic-gate 	if (index >= count)
83*0Sstevel@tonic-gate 		return (NULL);
84*0Sstevel@tonic-gate 	else
85*0Sstevel@tonic-gate 		return (&slotinfo->slots[index]);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static struct slot *
get_slot_named(struct slotinfo * slotinfo,char * name)89*0Sstevel@tonic-gate get_slot_named(struct slotinfo *slotinfo, char *name)
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	unsigned 	count, i;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	count 		= slotinfo->slot_count;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	for (i = 0; i < count; i++)
96*0Sstevel@tonic-gate 		if (strcmp(name, slotinfo->slots[i].slot_name) == 0)
97*0Sstevel@tonic-gate 			return (&slotinfo->slots[i]);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	return (NULL);
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate static tnf_datum_t
get_slot(tnf_datum_t datum,struct slot * slot)103*0Sstevel@tonic-gate get_slot(tnf_datum_t datum, struct slot *slot)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	if (slot == NULL) {
106*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_BADSLOT); /* XXX */
107*0Sstevel@tonic-gate 		return (TNF_DATUM_NULL);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	} else if (INFO_TAGGED(slot->slot_type)) {
110*0Sstevel@tonic-gate 		TNF		*tnf;
111*0Sstevel@tonic-gate 		tnf_ref32_t	*rec;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 		tnf = DATUM_TNF(datum);
114*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
115*0Sstevel@tonic-gate 		rec = _GET_REF32(tnf, (tnf_ref32_t *)
116*0Sstevel@tonic-gate 			(DATUM_VAL(datum) + slot->slot_offset));
117*0Sstevel@tonic-gate 		/* NULL slots are allowed */
118*0Sstevel@tonic-gate 		return ((rec == TNF_NULL)? TNF_DATUM_NULL :
119*0Sstevel@tonic-gate 			RECORD_DATUM(tnf, rec));
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	} else			/* inline */
122*0Sstevel@tonic-gate 		return DATUM(slot->slot_type,
123*0Sstevel@tonic-gate 			DATUM_VAL(datum) + slot->slot_offset);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate /*
127*0Sstevel@tonic-gate  *
128*0Sstevel@tonic-gate  */
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate unsigned
tnf_get_slot_count(tnf_datum_t datum)131*0Sstevel@tonic-gate tnf_get_slot_count(tnf_datum_t datum)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	struct slotinfo	*slotinfo;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	CHECK_SLOTS(datum);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	slotinfo = get_slotinfo(datum);
138*0Sstevel@tonic-gate 	return (slotinfo->slot_count);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  *
143*0Sstevel@tonic-gate  */
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate unsigned
tnf_get_slot_index(tnf_datum_t datum,char * name)146*0Sstevel@tonic-gate tnf_get_slot_index(tnf_datum_t datum, char *name)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	struct slotinfo	*slotinfo;
149*0Sstevel@tonic-gate 	struct slot	*slot;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	CHECK_SLOTS(datum);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	slotinfo = get_slotinfo(datum);
154*0Sstevel@tonic-gate 	slot	 = get_slot_named(slotinfo, name);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if (slot == NULL) {
157*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_BADSLOT); /* XXX */
158*0Sstevel@tonic-gate 		return (((unsigned)-1));
159*0Sstevel@tonic-gate 	} else
160*0Sstevel@tonic-gate 		return (((char *)slot - (char *)&slotinfo->slots[0])
161*0Sstevel@tonic-gate 			/ sizeof (struct slot));
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate  *
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate char *
tnf_get_slot_name(tnf_datum_t datum,unsigned index)169*0Sstevel@tonic-gate tnf_get_slot_name(tnf_datum_t datum, unsigned index)
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate 	struct slotinfo	*slotinfo;
172*0Sstevel@tonic-gate 	struct slot	*slot;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	CHECK_SLOTS(datum);
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	slotinfo 	= get_slotinfo(datum);
177*0Sstevel@tonic-gate 	slot		= get_slot_indexed(slotinfo, index);
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	if (slot == NULL) {
180*0Sstevel@tonic-gate 		_tnf_error(DATUM_TNF(datum), TNF_ERR_BADSLOT); /* XXX */
181*0Sstevel@tonic-gate 		return ((char *)NULL);
182*0Sstevel@tonic-gate 	} else
183*0Sstevel@tonic-gate 		return (slot->slot_name);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate /*
187*0Sstevel@tonic-gate  *
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate tnf_datum_t
tnf_get_slot_named(tnf_datum_t datum,char * name)191*0Sstevel@tonic-gate tnf_get_slot_named(tnf_datum_t datum, char *name)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	struct slotinfo	*slotinfo;
194*0Sstevel@tonic-gate 	struct slot	*slot;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	CHECK_SLOTS(datum);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	slotinfo 	= get_slotinfo(datum);
199*0Sstevel@tonic-gate 	slot		= get_slot_named(slotinfo, name);
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	return (get_slot(datum, slot));
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate  *
206*0Sstevel@tonic-gate  */
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate tnf_datum_t
tnf_get_slot_indexed(tnf_datum_t datum,unsigned index)209*0Sstevel@tonic-gate tnf_get_slot_indexed(tnf_datum_t datum, unsigned index)
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate 	struct slotinfo	*slotinfo;
212*0Sstevel@tonic-gate 	struct slot	*slot;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	CHECK_SLOTS(datum);
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	slotinfo 	= get_slotinfo(datum);
217*0Sstevel@tonic-gate 	slot		= get_slot_indexed(slotinfo, index);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	return (get_slot(datum, slot));
220*0Sstevel@tonic-gate }
221