9162 libpp: this statement may fall through
[unleashed.git] / usr / src / lib / libtnf / struct.c
blobc7aa1e6a9581415c43d74022974b10f3832db476
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include "libtnf.h"
34 static struct slotinfo *get_slotinfo(tnf_datum_t);
35 static struct slot * get_slot_named(struct slotinfo *, char *);
36 static struct slot * get_slot_indexed(struct slotinfo *, unsigned);
37 static tnf_datum_t get_slot(tnf_datum_t, struct slot *);
43 void
44 _tnf_check_slots(tnf_datum_t datum)
46 struct taginfo *info;
48 CHECK_DATUM(datum);
50 info = DATUM_INFO(datum);
52 /* Must be an aggregate */
53 if (!(INFO_STRUCT(info) || INFO_ARRAY(info)))
54 _tnf_error(DATUM_TNF(datum), TNF_ERR_TYPEMISMATCH);
58 * Helpers
61 static struct slotinfo *
62 get_slotinfo(tnf_datum_t datum)
64 struct taginfo *info, *base_info;
66 info = DATUM_INFO(datum);
67 base_info = INFO_DERIVED(info)? info->base: info;
69 /* XXX base must not be a scalar tag */
70 if (INFO_SCALAR(base_info))
71 _tnf_error(DATUM_TNF(datum), TNF_ERR_BADTNF);
73 return (base_info->slotinfo);
76 static struct slot *
77 get_slot_indexed(struct slotinfo *slotinfo, unsigned index)
79 unsigned count;
81 count = slotinfo->slot_count;
82 if (index >= count)
83 return (NULL);
84 else
85 return (&slotinfo->slots[index]);
88 static struct slot *
89 get_slot_named(struct slotinfo *slotinfo, char *name)
91 unsigned count, i;
93 count = slotinfo->slot_count;
95 for (i = 0; i < count; i++)
96 if (strcmp(name, slotinfo->slots[i].slot_name) == 0)
97 return (&slotinfo->slots[i]);
99 return (NULL);
102 static tnf_datum_t
103 get_slot(tnf_datum_t datum, struct slot *slot)
105 if (slot == NULL) {
106 _tnf_error(DATUM_TNF(datum), TNF_ERR_BADSLOT); /* XXX */
107 return (TNF_DATUM_NULL);
109 } else if (INFO_TAGGED(slot->slot_type)) {
110 TNF *tnf;
111 tnf_ref32_t *rec;
113 tnf = DATUM_TNF(datum);
114 /* LINTED pointer cast may result in improper alignment */
115 rec = _GET_REF32(tnf, (tnf_ref32_t *)
116 (DATUM_VAL(datum) + slot->slot_offset));
117 /* NULL slots are allowed */
118 return ((rec == TNF_NULL)? TNF_DATUM_NULL :
119 RECORD_DATUM(tnf, rec));
121 } else /* inline */
122 return DATUM(slot->slot_type,
123 DATUM_VAL(datum) + slot->slot_offset);
130 unsigned
131 tnf_get_slot_count(tnf_datum_t datum)
133 struct slotinfo *slotinfo;
135 CHECK_SLOTS(datum);
137 slotinfo = get_slotinfo(datum);
138 return (slotinfo->slot_count);
145 unsigned
146 tnf_get_slot_index(tnf_datum_t datum, char *name)
148 struct slotinfo *slotinfo;
149 struct slot *slot;
151 CHECK_SLOTS(datum);
153 slotinfo = get_slotinfo(datum);
154 slot = get_slot_named(slotinfo, name);
156 if (slot == NULL) {
157 _tnf_error(DATUM_TNF(datum), TNF_ERR_BADSLOT); /* XXX */
158 return (((unsigned)-1));
159 } else
160 return (((char *)slot - (char *)&slotinfo->slots[0])
161 / sizeof (struct slot));
168 char *
169 tnf_get_slot_name(tnf_datum_t datum, unsigned index)
171 struct slotinfo *slotinfo;
172 struct slot *slot;
174 CHECK_SLOTS(datum);
176 slotinfo = get_slotinfo(datum);
177 slot = get_slot_indexed(slotinfo, index);
179 if (slot == NULL) {
180 _tnf_error(DATUM_TNF(datum), TNF_ERR_BADSLOT); /* XXX */
181 return ((char *)NULL);
182 } else
183 return (slot->slot_name);
190 tnf_datum_t
191 tnf_get_slot_named(tnf_datum_t datum, char *name)
193 struct slotinfo *slotinfo;
194 struct slot *slot;
196 CHECK_SLOTS(datum);
198 slotinfo = get_slotinfo(datum);
199 slot = get_slot_named(slotinfo, name);
201 return (get_slot(datum, slot));
208 tnf_datum_t
209 tnf_get_slot_indexed(tnf_datum_t datum, unsigned index)
211 struct slotinfo *slotinfo;
212 struct slot *slot;
214 CHECK_SLOTS(datum);
216 slotinfo = get_slotinfo(datum);
217 slot = get_slot_indexed(slotinfo, index);
219 return (get_slot(datum, slot));