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 #pragma ident "%Z%%M% %I% %E% SMI" 27*45916cd2Sjpk 28*45916cd2Sjpk /* 29*45916cd2Sjpk * btohex.c - Binary to Hexadecimal string conversion. 30*45916cd2Sjpk * 31*45916cd2Sjpk * These routines convert binary labels into canonical 32*45916cd2Sjpk * hexadecimal representations of the binary form. 33*45916cd2Sjpk */ 34*45916cd2Sjpk 35*45916cd2Sjpk #include <stdlib.h> 36*45916cd2Sjpk #include <strings.h> 37*45916cd2Sjpk #include <tsol/label.h> 38*45916cd2Sjpk #include <sys/tsol/label_macro.h> 39*45916cd2Sjpk 40*45916cd2Sjpk /* 0x + Classification + '-' + ll + '-' + Compartments + end of string */ 41*45916cd2Sjpk #define _HEX_SIZE 2+(sizeof (Classification_t)*2)+4+\ 42*45916cd2Sjpk (sizeof (Compartments_t)*2)+1 43*45916cd2Sjpk 44*45916cd2Sjpk static char hex_buf[_HEX_SIZE]; 45*45916cd2Sjpk 46*45916cd2Sjpk /* 47*45916cd2Sjpk * h_alloc - Allocate data storage for a Hexadecimal label string. 48*45916cd2Sjpk * 49*45916cd2Sjpk * Entry id = Type of label to allocate storage for. 50*45916cd2Sjpk * SUN_SL_ID - Sensitivity Label. 51*45916cd2Sjpk * SUN_CLR_ID - Clearance. 52*45916cd2Sjpk * 53*45916cd2Sjpk * Returns NULL, If unable to allocate storage. 54*45916cd2Sjpk * Address of buffer. 55*45916cd2Sjpk * 56*45916cd2Sjpk * Calls malloc; 57*45916cd2Sjpk */ 58*45916cd2Sjpk 59*45916cd2Sjpk char * 60*45916cd2Sjpk h_alloc(unsigned char id) 61*45916cd2Sjpk { 62*45916cd2Sjpk size_t size; 63*45916cd2Sjpk 64*45916cd2Sjpk switch (id) { 65*45916cd2Sjpk 66*45916cd2Sjpk case SUN_SL_ID: 67*45916cd2Sjpk size = _HEX_SIZE; 68*45916cd2Sjpk break; 69*45916cd2Sjpk 70*45916cd2Sjpk case SUN_CLR_ID: 71*45916cd2Sjpk size = _HEX_SIZE; 72*45916cd2Sjpk break; 73*45916cd2Sjpk 74*45916cd2Sjpk default: 75*45916cd2Sjpk return (NULL); 76*45916cd2Sjpk } 77*45916cd2Sjpk 78*45916cd2Sjpk return ((char *)malloc(size)); 79*45916cd2Sjpk } 80*45916cd2Sjpk 81*45916cd2Sjpk 82*45916cd2Sjpk /* 83*45916cd2Sjpk * h_free - Free a Hexadecimal label string. 84*45916cd2Sjpk * 85*45916cd2Sjpk * Entry hex = Hexadecimal label string. 86*45916cd2Sjpk * 87*45916cd2Sjpk * Returns none. 88*45916cd2Sjpk * 89*45916cd2Sjpk * Calls free. 90*45916cd2Sjpk */ 91*45916cd2Sjpk 92*45916cd2Sjpk void 93*45916cd2Sjpk h_free(char *hex) 94*45916cd2Sjpk { 95*45916cd2Sjpk 96*45916cd2Sjpk if (hex == NULL) 97*45916cd2Sjpk return; 98*45916cd2Sjpk 99*45916cd2Sjpk free(hex); 100*45916cd2Sjpk } 101*45916cd2Sjpk 102*45916cd2Sjpk 103*45916cd2Sjpk /* 104*45916cd2Sjpk * bsltoh_r - Convert a Sensitivity Label into a Hexadecimal label string. 105*45916cd2Sjpk * 106*45916cd2Sjpk * Entry label = Sensitivity Label to be translated. 107*45916cd2Sjpk * hex = Buffer to place converted label. 108*45916cd2Sjpk * len = Length of buffer. 109*45916cd2Sjpk * 110*45916cd2Sjpk * Returns NULL, If invalid label type. 111*45916cd2Sjpk * Address of buffer. 112*45916cd2Sjpk * 113*45916cd2Sjpk * Calls label_to_str, strncpy. 114*45916cd2Sjpk */ 115*45916cd2Sjpk 116*45916cd2Sjpk char * 117*45916cd2Sjpk bsltoh_r(const m_label_t *label, char *hex) 118*45916cd2Sjpk { 119*45916cd2Sjpk char *h; 120*45916cd2Sjpk 121*45916cd2Sjpk if (label_to_str(label, &h, M_INTERNAL, DEF_NAMES) != 0) { 122*45916cd2Sjpk free(h); 123*45916cd2Sjpk return (NULL); 124*45916cd2Sjpk } 125*45916cd2Sjpk 126*45916cd2Sjpk (void) strncpy(hex, (const char *)h, _HEX_SIZE); 127*45916cd2Sjpk free(h); 128*45916cd2Sjpk return (hex); 129*45916cd2Sjpk } 130*45916cd2Sjpk 131*45916cd2Sjpk 132*45916cd2Sjpk /* 133*45916cd2Sjpk * bsltoh - Convert a Sensitivity Label into a Hexadecimal label string. 134*45916cd2Sjpk * 135*45916cd2Sjpk * Entry label = Sensitivity Label to be translated. 136*45916cd2Sjpk * 137*45916cd2Sjpk * Returns NULL, If invalid label type. 138*45916cd2Sjpk * Address of statically allocated hex label string. 139*45916cd2Sjpk * 140*45916cd2Sjpk * Calls bsltoh_r. 141*45916cd2Sjpk * 142*45916cd2Sjpk * Uses hex_buf. 143*45916cd2Sjpk */ 144*45916cd2Sjpk 145*45916cd2Sjpk char * 146*45916cd2Sjpk bsltoh(const m_label_t *label) 147*45916cd2Sjpk { 148*45916cd2Sjpk 149*45916cd2Sjpk return (bsltoh_r(label, hex_buf)); 150*45916cd2Sjpk } 151*45916cd2Sjpk 152*45916cd2Sjpk 153*45916cd2Sjpk /* 154*45916cd2Sjpk * bcleartoh_r - Convert a Clearance into a Hexadecimal label string. 155*45916cd2Sjpk * 156*45916cd2Sjpk * Entry clearance = Clearance to be translated. 157*45916cd2Sjpk * hex = Buffer to place converted label. 158*45916cd2Sjpk * len = Length of buffer. 159*45916cd2Sjpk * 160*45916cd2Sjpk * Returns NULL, If invalid label type. 161*45916cd2Sjpk * Address of buffer. 162*45916cd2Sjpk * 163*45916cd2Sjpk * Calls label_to_str, strncpy. 164*45916cd2Sjpk */ 165*45916cd2Sjpk 166*45916cd2Sjpk char * 167*45916cd2Sjpk bcleartoh_r(const m_label_t *clearance, char *hex) 168*45916cd2Sjpk { 169*45916cd2Sjpk char *h; 170*45916cd2Sjpk 171*45916cd2Sjpk if (label_to_str(clearance, &h, M_INTERNAL, DEF_NAMES) != 0) { 172*45916cd2Sjpk free(h); 173*45916cd2Sjpk return (NULL); 174*45916cd2Sjpk } 175*45916cd2Sjpk 176*45916cd2Sjpk (void) strncpy(hex, (const char *)h, _HEX_SIZE); 177*45916cd2Sjpk free(h); 178*45916cd2Sjpk return (hex); 179*45916cd2Sjpk } 180*45916cd2Sjpk 181*45916cd2Sjpk 182*45916cd2Sjpk /* 183*45916cd2Sjpk * bcleartoh - Convert a Clearance into a Hexadecimal label string. 184*45916cd2Sjpk * 185*45916cd2Sjpk * Entry clearance = Clearance to be translated. 186*45916cd2Sjpk * 187*45916cd2Sjpk * Returns NULL, If invalid label type. 188*45916cd2Sjpk * Address of statically allocated hex label string. 189*45916cd2Sjpk * 190*45916cd2Sjpk * Calls bcleartoh_r. 191*45916cd2Sjpk * 192*45916cd2Sjpk * Uses hex_buf. 193*45916cd2Sjpk */ 194*45916cd2Sjpk 195*45916cd2Sjpk char * 196*45916cd2Sjpk bcleartoh(const m_label_t *clearance) 197*45916cd2Sjpk { 198*45916cd2Sjpk 199*45916cd2Sjpk return (bcleartoh_r(clearance, hex_buf)); 200*45916cd2Sjpk } 201