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