xref: /titanic_44/usr/src/lib/libtnf/datum.c (revision 24db46411fd54f70c35b94bb952eb7ba040e43b4)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1994, by Sun Microsytems, Inc.
24  */
25 
26 #pragma	ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "libtnf.h"
29 
30 /*
31  * Defines
32  */
33 
34 #define	DATUM_KIND(d)	(DATUM_INFO(d)->kind)
35 
36 /*
37  * Declarations
38  */
39 
40 static int 	has_prop(tnf_datum_t, tag_props_t);
41 
42 /*
43  * Datum operations: for more debuggability
44  */
45 
46 #ifndef _DATUM_MACROS
47 
48 tnf_datum_t
49 _tnf_datum(struct taginfo *info, caddr_t val)
50 {
51 	return (_DATUM(info, val));
52 }
53 
54 struct taginfo *
55 _tnf_datum_info(tnf_datum_t datum)
56 {
57 	return ((struct taginfo *)_DATUM_HI(datum));
58 }
59 
60 caddr_t
61 _tnf_datum_val(tnf_datum_t datum)
62 {
63 	return ((caddr_t)_DATUM_LO(datum));
64 }
65 
66 #endif
67 
68 /*
69  * Check for valid datum
70  */
71 
72 void
73 _tnf_check_datum(tnf_datum_t datum)
74 {
75 	caddr_t		val;
76 	TNF		*tnf;
77 
78 	if (datum == TNF_DATUM_NULL)
79 		_tnf_error(NULL, TNF_ERR_BADTNF);
80 
81 	val 	= DATUM_VAL(datum);
82 	tnf	= DATUM_TNF(datum);
83 
84 	if ((val <= tnf->file_start) || (val >= tnf->file_end))
85 		_tnf_error(tnf, TNF_ERR_BADDATUM);
86 }
87 
88 /*
89  * Retrieve datum kind from cached information
90  */
91 
92 tnf_kind_t
93 tnf_get_kind(tnf_datum_t datum)
94 {
95 	CHECK_DATUM(datum);
96 	/* The kind field is always completely initialized */
97 	return (DATUM_KIND(datum));
98 }
99 
100 /*
101  * Classification predicates: check the cached tag props
102  */
103 
104 static int
105 has_prop(tnf_datum_t datum, tag_props_t prop)
106 {
107 	CHECK_DATUM(datum);
108 
109 	/* Note: No need to get base info because props inherited */
110 	return (INFO_PROP(DATUM_INFO(datum), prop));
111 }
112 
113 int
114 tnf_is_inline(tnf_datum_t datum)
115 {
116 	return (has_prop(datum, TAG_PROP_INLINE));
117 }
118 
119 int
120 tnf_is_scalar(tnf_datum_t datum)
121 {
122 	return (has_prop(datum, TAG_PROP_SCALAR));
123 }
124 
125 int
126 tnf_is_record(tnf_datum_t datum) /* XXX was: tnf_is_tagged */
127 {
128 	return (has_prop(datum, TAG_PROP_TAGGED));
129 }
130 
131 int
132 tnf_is_array(tnf_datum_t datum)
133 {
134 	return (has_prop(datum, TAG_PROP_ARRAY));
135 }
136 
137 int
138 tnf_is_string(tnf_datum_t datum)
139 {
140 	return (has_prop(datum, TAG_PROP_STRING));
141 }
142 
143 int
144 tnf_is_struct(tnf_datum_t datum)
145 {
146 	return (has_prop(datum, TAG_PROP_STRUCT));
147 }
148 
149 int
150 tnf_is_type(tnf_datum_t datum)
151 {
152 	return (has_prop(datum, TAG_PROP_TYPE));
153 }
154 
155 /*
156  * Get the type datum for any datum
157  */
158 
159 tnf_datum_t
160 tnf_get_type(tnf_datum_t datum)
161 {
162 	struct taginfo	*info;
163 
164 	CHECK_DATUM(datum);
165 
166 	info = DATUM_INFO(datum);
167 	return (DATUM(info->meta, (caddr_t)info->tag));
168 }
169 
170 /*
171  * Get the type name for any datum
172  * XXX Beware: this is a pointer into the file
173  */
174 
175 char *
176 tnf_get_type_name(tnf_datum_t datum)
177 {
178 	CHECK_DATUM(datum);
179 	return (DATUM_INFO(datum)->name);	/* cached */
180 }
181 
182 /*
183  * Get the size of any datum
184  */
185 
186 size_t
187 tnf_get_size(tnf_datum_t datum)
188 {
189 	struct taginfo	*info;
190 	size_t		size;
191 
192 	CHECK_DATUM(datum);
193 
194 	info = DATUM_INFO(datum);
195 	size = info->size;
196 
197 	if (size == (size_t)-1)	/* self sized */
198 		/* XXX tnf_get_slot_named(datum, TNF_N_SELF_SIZE) */
199 		/* LINTED pointer cast may result in improper alignment */
200 		return (_tnf_get_self_size(info->tnf, DATUM_RECORD(datum)));
201 	else
202 		return (size);
203 }
204 
205 /*
206  * Get raw pointer to any datum
207  */
208 
209 caddr_t
210 tnf_get_raw(tnf_datum_t datum)
211 {
212 	CHECK_DATUM(datum);
213 	return (DATUM_VAL(datum));
214 }
215