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]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
36 #include <sys/timeb.h>
39 #include "KMSAgentStringUtilities.h"
44 #define gmtime_r(clock,result) ( *(result) = *gmtime(clock), result )
47 // Find header in KMSAgentStringUtilities.h
48 int64
UTF8ToInt64( const char* i_sNumber
)
50 FATAL_ASSERT( i_sNumber
);
53 return _atoi64( i_sNumber
);
55 return atoll( i_sNumber
);
59 void Int64ToUTF8(char* const o_psUTF8
,
67 if ( i_bPad
&& i_bHex
)
70 strcpy(sFormat
,"%016I64X");
72 strcpy(sFormat
,"%016llX");
75 else if ( i_bPad
&& !i_bHex
)
78 strcpy(sFormat
, "%019I64d");
80 strcpy(sFormat
, "%019lld");
83 else if ( !i_bPad
&& i_bHex
)
86 strcpy(sFormat
, "%I64X");
88 strcpy(sFormat
, "%llX");
91 else //( !i_bPad && !i_bHex )
94 strcpy(sFormat
, "%I64d");
96 strcpy(sFormat
, "%lld");
101 int iReturn
= sprintf( o_psUTF8
, sFormat
, i_iNumber
);
103 //int iReturn = K_snprintf(o_psUTF8, iBufferSize, sFormat, i_iNumber);
105 int iReturn
= sprintf( o_psUTF8
, sFormat
, i_iNumber
);
109 // Our buffer wasn't big enough. Shouldn't happen.
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 );
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;
158 o_pBinaryBuffer
[i
/2] = (char)(iDigitValue
<< 4);
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
, "");
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';
204 // Find header in StringUtilities.h
205 void GetCurrentDateTimeISO8601UTC(char* const o_psDateTimeISO8601UTC
,
213 FATAL_ASSERT( o_psDateTimeISO8601UTC
);
215 struct tm
* pstTime
= gmtime( &(stTime
.time
) );
218 o_psDateTimeISO8601UTC
,
220 "%04d-%02d-%02d %02d:%02d:%02d.%03dZ",
221 pstTime
->tm_year
+1900,
230 // no time functions for the metaware environment
231 strcpy( o_psDateTimeISO8601UTC
, "" );