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