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