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