998 obsolete DMA driver interfaces should be removed
[illumos-gate.git] / usr / src / lib / libkmsagent / common / KMSAgentStringUtilities.cpp
blobc47e6848e8021e10b158a7731788ad6341189d90
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
26 #ifndef WIN32
27 #include <ctype.h>
29 #ifndef METAWARE
30 #include <wctype.h>
31 #endif
33 #endif
35 #ifndef METAWARE
36 #include <sys/timeb.h>
37 #endif
39 #include "KMSAgentStringUtilities.h"
41 #ifdef WIN32
42 #include <stdlib.h>
43 #include <time.h>
44 #define gmtime_r(clock,result) ( *(result) = *gmtime(clock), result )
45 #endif
47 // Find header in KMSAgentStringUtilities.h
48 int64 UTF8ToInt64( const char* i_sNumber )
50 FATAL_ASSERT( i_sNumber );
52 #ifdef WIN32
53 return _atoi64( i_sNumber );
54 #else
55 return atoll( i_sNumber );
56 #endif
59 void Int64ToUTF8(char* const o_psUTF8,
60 int64 i_iNumber,
61 int i_bPad,
62 int i_bHex )
64 //string sFormat;
65 char sFormat[10];
67 if ( i_bPad && i_bHex )
69 #ifdef WIN32
70 strcpy(sFormat,"%016I64X");
71 #else
72 strcpy(sFormat,"%016llX");
73 #endif
75 else if ( i_bPad && !i_bHex )
77 #ifdef WIN32
78 strcpy(sFormat, "%019I64d");
79 #else
80 strcpy(sFormat, "%019lld");
81 #endif
83 else if ( !i_bPad && i_bHex )
85 #ifdef WIN32
86 strcpy(sFormat, "%I64X");
87 #else
88 strcpy(sFormat, "%llX");
89 #endif
91 else //( !i_bPad && !i_bHex )
93 #ifdef WIN32
94 strcpy(sFormat, "%I64d");
95 #else
96 strcpy(sFormat, "%lld");
97 #endif
100 #ifndef METAWARE
101 int iReturn = sprintf( o_psUTF8, sFormat, i_iNumber);
103 //int iReturn = K_snprintf(o_psUTF8, iBufferSize, sFormat, i_iNumber);
104 #else
105 int iReturn = sprintf( o_psUTF8, sFormat, i_iNumber);
106 #endif
107 if ( iReturn < 0 )
109 // Our buffer wasn't big enough. Shouldn't happen.
110 FATAL_ASSERT(0);
113 return;
117 // Find header in KMSAgentStringUtilities.h
118 int ConvertUTF8HexStringToBinary(
119 const char* i_sHexString,
120 unsigned char* o_pBinaryBuffer)
122 int iHexLen = i_sHexString ? strlen(i_sHexString) : 0;
123 FATAL_ASSERT( (iHexLen % 2) == 0 ); // to be valid, the hex string must have an even number of characters
125 if ( !o_pBinaryBuffer )
127 return ( iHexLen / 2 );
130 if ( iHexLen <= 0 )
132 return 0;
135 int iDigitValue = 0;
137 for ( int i = 0; i < iHexLen; i++)
139 if (i_sHexString[i] >= '0' && i_sHexString[i] <= '9')
141 iDigitValue = i_sHexString[i] - '0';
143 else if (i_sHexString[i] >= 'A' && i_sHexString[i] <= 'F')
145 iDigitValue = i_sHexString[i] - 'A' + 10;
147 else if (i_sHexString[i] >= 'a' && i_sHexString[i] <= 'f')
149 iDigitValue = i_sHexString[i] - 'a' + 10;
151 else
153 iDigitValue = 0;
156 if (i % 2 == 0)
158 o_pBinaryBuffer[i/2] = (char)(iDigitValue << 4);
160 else
162 o_pBinaryBuffer[i/2] |= (char)iDigitValue;
166 return ( iHexLen / 2 );
169 // Find header in KMSAgentStringUtilities.h
170 void ConvertBinaryToUTF8HexString(
171 char* const o_sHexString,
172 const unsigned char* const i_pBinaryBuffer,
173 int i_iBinaryBufferSize )
175 const char HEXCHARS[] = "0123456789ABCDEF";
177 FATAL_ASSERT( o_sHexString );
179 if ( (i_pBinaryBuffer == 0) || (i_iBinaryBufferSize == 0) )
181 strcpy(o_sHexString, "");
182 return;
185 FATAL_ASSERT( i_pBinaryBuffer );
187 for ( int i = 0; i < (2 * i_iBinaryBufferSize); i++ )
189 unsigned char ucFourBits = i_pBinaryBuffer[i / 2];
190 if ( (i % 2) == 0 ) // high four bits of the current byte
191 ucFourBits = (ucFourBits >> 4) & 0xF; // shift down and blank out upper bits
192 else // low four bits of the current byte
193 ucFourBits = ucFourBits & 0xF; // blank out upper bits
195 o_sHexString[i] = HEXCHARS[ucFourBits];
198 o_sHexString[i_iBinaryBufferSize * 2] = '\0';
200 return;
204 // Find header in StringUtilities.h
205 void GetCurrentDateTimeISO8601UTC(char* const o_psDateTimeISO8601UTC,
206 int i_iLength)
209 #ifndef METAWARE
210 timeb stTime;
211 ftime(&stTime);
213 FATAL_ASSERT( o_psDateTimeISO8601UTC );
215 struct tm* pstTime = gmtime( &(stTime.time) );
217 K_snprintf(
218 o_psDateTimeISO8601UTC,
219 i_iLength,
220 "%04d-%02d-%02d %02d:%02d:%02d.%03dZ",
221 pstTime->tm_year+1900,
222 pstTime->tm_mon+1,
223 pstTime->tm_mday,
224 pstTime->tm_hour,
225 pstTime->tm_min,
226 pstTime->tm_sec,
227 stTime.millitm);
229 #else
230 // no time functions for the metaware environment
231 strcpy( o_psDateTimeISO8601UTC, "" );
232 #endif
233 return;