xref: /illumos-gate/usr/src/lib/libtsol/common/btohex.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *      btohex.c - Binary to Hexadecimal string conversion.
28  *
29  *		These routines convert binary labels into canonical
30  *	hexadecimal representations of the binary form.
31  */
32 
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <tsol/label.h>
36 #include <sys/tsol/label_macro.h>
37 
38 /* 0x + Classification + '-' + ll + '-' + Compartments + end of string */
39 #define	_HEX_SIZE 2+(sizeof (Classification_t)*2)+4+\
40 	(sizeof (Compartments_t)*2)+1
41 
42 static char hex_buf[_HEX_SIZE];
43 
44 /*
45  *	h_alloc - Allocate data storage for a Hexadecimal label string.
46  *
47  *	Entry	id = Type of label to allocate storage for.
48  *		     SUN_SL_ID  - Sensitivity Label.
49  *		     SUN_CLR_ID - Clearance.
50  *
51  *	Returns	NULL, If unable to allocate storage.
52  *		Address of buffer.
53  *
54  *	Calls	malloc;
55  */
56 
57 char *
58 h_alloc(unsigned char id)
59 {
60 	size_t size;
61 
62 	switch (id) {
63 
64 	case SUN_SL_ID:
65 		size = _HEX_SIZE;
66 		break;
67 
68 	case SUN_CLR_ID:
69 		size = _HEX_SIZE;
70 		break;
71 
72 	default:
73 		return (NULL);
74 	}
75 
76 	return ((char *)malloc(size));
77 }
78 
79 
80 /*
81  *	h_free - Free a Hexadecimal label string.
82  *
83  *	Entry	hex = Hexadecimal label string.
84  *
85  *	Returns	none.
86  *
87  *	Calls	free.
88  */
89 
90 void
91 h_free(char *hex)
92 {
93 
94 	if (hex == NULL)
95 		return;
96 
97 	free(hex);
98 }
99 
100 
101 /*
102  *	bsltoh_r - Convert a Sensitivity Label into a Hexadecimal label string.
103  *
104  *	Entry	label = Sensitivity Label to be translated.
105  *		hex = Buffer to place converted label.
106  *		len = Length of buffer.
107  *
108  *	Returns	NULL, If invalid label type.
109  *		Address of buffer.
110  *
111  *	Calls	label_to_str, strncpy.
112  */
113 
114 char *
115 bsltoh_r(const m_label_t *label, char *hex)
116 {
117 	char *h;
118 
119 	if (label_to_str(label, &h, M_INTERNAL, DEF_NAMES) != 0) {
120 		free(h);
121 		return (NULL);
122 	}
123 
124 	(void) strncpy(hex, (const char *)h, _HEX_SIZE);
125 	free(h);
126 	return (hex);
127 }
128 
129 
130 /*
131  *	bsltoh - Convert a Sensitivity Label into a Hexadecimal label string.
132  *
133  *	Entry	label = Sensitivity Label to be translated.
134  *
135  *	Returns	NULL, If invalid label type.
136  *		Address of statically allocated hex label string.
137  *
138  *	Calls	bsltoh_r.
139  *
140  *	Uses	hex_buf.
141  */
142 
143 char *
144 bsltoh(const m_label_t *label)
145 {
146 
147 	return (bsltoh_r(label, hex_buf));
148 }
149 
150 
151 /*
152  *	bcleartoh_r - Convert a Clearance into a Hexadecimal label string.
153  *
154  *	Entry	clearance = Clearance to be translated.
155  *		hex = Buffer to place converted label.
156  *		len = Length of buffer.
157  *
158  *	Returns	NULL, If invalid label type.
159  *		Address of buffer.
160  *
161  *	Calls	label_to_str, strncpy.
162  */
163 
164 char *
165 bcleartoh_r(const m_label_t *clearance, char *hex)
166 {
167 	char *h;
168 
169 	if (label_to_str(clearance, &h, M_INTERNAL, DEF_NAMES) != 0) {
170 		free(h);
171 		return (NULL);
172 	}
173 
174 	(void) strncpy(hex, (const char *)h, _HEX_SIZE);
175 	free(h);
176 	return (hex);
177 }
178 
179 
180 /*
181  *	bcleartoh - Convert a Clearance into a Hexadecimal label string.
182  *
183  *	Entry	clearance = Clearance to be translated.
184  *
185  *	Returns	NULL, If invalid label type.
186  *		Address of statically allocated hex label string.
187  *
188  *	Calls	bcleartoh_r.
189  *
190  *	Uses	hex_buf.
191  */
192 
193 char *
194 bcleartoh(const m_label_t *clearance)
195 {
196 
197 	return (bcleartoh_r(clearance, hex_buf));
198 }
199