1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM  *
4*12720SWyllys.Ingersoll@Sun.COM  * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM  * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM  * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM  *
8*12720SWyllys.Ingersoll@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM  * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM  * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM  *
13*12720SWyllys.Ingersoll@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM  *
19*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM  */
21*12720SWyllys.Ingersoll@Sun.COM 
22*12720SWyllys.Ingersoll@Sun.COM /*
23*12720SWyllys.Ingersoll@Sun.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*12720SWyllys.Ingersoll@Sun.COM  */
25*12720SWyllys.Ingersoll@Sun.COM 
26*12720SWyllys.Ingersoll@Sun.COM #ifndef WIN32
27*12720SWyllys.Ingersoll@Sun.COM #include <ctype.h>
28*12720SWyllys.Ingersoll@Sun.COM 
29*12720SWyllys.Ingersoll@Sun.COM   #ifndef METAWARE
30*12720SWyllys.Ingersoll@Sun.COM     #include <wctype.h>
31*12720SWyllys.Ingersoll@Sun.COM   #endif
32*12720SWyllys.Ingersoll@Sun.COM 
33*12720SWyllys.Ingersoll@Sun.COM #endif
34*12720SWyllys.Ingersoll@Sun.COM 
35*12720SWyllys.Ingersoll@Sun.COM #ifndef METAWARE
36*12720SWyllys.Ingersoll@Sun.COM   #include <sys/timeb.h>
37*12720SWyllys.Ingersoll@Sun.COM #endif
38*12720SWyllys.Ingersoll@Sun.COM 
39*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentStringUtilities.h"
40*12720SWyllys.Ingersoll@Sun.COM 
41*12720SWyllys.Ingersoll@Sun.COM #ifdef WIN32
42*12720SWyllys.Ingersoll@Sun.COM #include <stdlib.h>
43*12720SWyllys.Ingersoll@Sun.COM #include <time.h>
44*12720SWyllys.Ingersoll@Sun.COM #define gmtime_r(clock,result) ( *(result) = *gmtime(clock), result )
45*12720SWyllys.Ingersoll@Sun.COM #endif
46*12720SWyllys.Ingersoll@Sun.COM 
47*12720SWyllys.Ingersoll@Sun.COM // Find header in KMSAgentStringUtilities.h
UTF8ToInt64(const char * i_sNumber)48*12720SWyllys.Ingersoll@Sun.COM int64 UTF8ToInt64( const char* i_sNumber )
49*12720SWyllys.Ingersoll@Sun.COM {
50*12720SWyllys.Ingersoll@Sun.COM     FATAL_ASSERT( i_sNumber );
51*12720SWyllys.Ingersoll@Sun.COM 
52*12720SWyllys.Ingersoll@Sun.COM #ifdef WIN32
53*12720SWyllys.Ingersoll@Sun.COM     return _atoi64( i_sNumber );
54*12720SWyllys.Ingersoll@Sun.COM #else
55*12720SWyllys.Ingersoll@Sun.COM     return atoll( i_sNumber );
56*12720SWyllys.Ingersoll@Sun.COM #endif
57*12720SWyllys.Ingersoll@Sun.COM }
58*12720SWyllys.Ingersoll@Sun.COM 
Int64ToUTF8(char * const o_psUTF8,int64 i_iNumber,int i_bPad,int i_bHex)59*12720SWyllys.Ingersoll@Sun.COM void Int64ToUTF8(char* const o_psUTF8,
60*12720SWyllys.Ingersoll@Sun.COM                  int64 i_iNumber,
61*12720SWyllys.Ingersoll@Sun.COM                  int i_bPad,
62*12720SWyllys.Ingersoll@Sun.COM                  int i_bHex )
63*12720SWyllys.Ingersoll@Sun.COM {
64*12720SWyllys.Ingersoll@Sun.COM     //string sFormat;
65*12720SWyllys.Ingersoll@Sun.COM     char sFormat[10];
66*12720SWyllys.Ingersoll@Sun.COM 
67*12720SWyllys.Ingersoll@Sun.COM     if ( i_bPad && i_bHex )
68*12720SWyllys.Ingersoll@Sun.COM     {
69*12720SWyllys.Ingersoll@Sun.COM #ifdef WIN32
70*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat,"%016I64X");
71*12720SWyllys.Ingersoll@Sun.COM #else
72*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat,"%016llX");
73*12720SWyllys.Ingersoll@Sun.COM #endif
74*12720SWyllys.Ingersoll@Sun.COM     }
75*12720SWyllys.Ingersoll@Sun.COM     else if ( i_bPad && !i_bHex )
76*12720SWyllys.Ingersoll@Sun.COM     {
77*12720SWyllys.Ingersoll@Sun.COM #ifdef WIN32
78*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat, "%019I64d");
79*12720SWyllys.Ingersoll@Sun.COM #else
80*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat, "%019lld");
81*12720SWyllys.Ingersoll@Sun.COM #endif
82*12720SWyllys.Ingersoll@Sun.COM     }
83*12720SWyllys.Ingersoll@Sun.COM     else if ( !i_bPad && i_bHex )
84*12720SWyllys.Ingersoll@Sun.COM     {
85*12720SWyllys.Ingersoll@Sun.COM #ifdef WIN32
86*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat, "%I64X");
87*12720SWyllys.Ingersoll@Sun.COM #else
88*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat, "%llX");
89*12720SWyllys.Ingersoll@Sun.COM #endif
90*12720SWyllys.Ingersoll@Sun.COM     }
91*12720SWyllys.Ingersoll@Sun.COM     else //( !i_bPad && !i_bHex )
92*12720SWyllys.Ingersoll@Sun.COM     {
93*12720SWyllys.Ingersoll@Sun.COM #ifdef WIN32
94*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat, "%I64d");
95*12720SWyllys.Ingersoll@Sun.COM #else
96*12720SWyllys.Ingersoll@Sun.COM         strcpy(sFormat, "%lld");
97*12720SWyllys.Ingersoll@Sun.COM #endif
98*12720SWyllys.Ingersoll@Sun.COM     }
99*12720SWyllys.Ingersoll@Sun.COM 
100*12720SWyllys.Ingersoll@Sun.COM #ifndef METAWARE
101*12720SWyllys.Ingersoll@Sun.COM     int iReturn = sprintf( o_psUTF8, sFormat, i_iNumber);
102*12720SWyllys.Ingersoll@Sun.COM 
103*12720SWyllys.Ingersoll@Sun.COM     //int iReturn = K_snprintf(o_psUTF8, iBufferSize, sFormat, i_iNumber);
104*12720SWyllys.Ingersoll@Sun.COM #else
105*12720SWyllys.Ingersoll@Sun.COM     int iReturn = sprintf( o_psUTF8, sFormat, i_iNumber);
106*12720SWyllys.Ingersoll@Sun.COM #endif
107*12720SWyllys.Ingersoll@Sun.COM     if ( iReturn < 0 )
108*12720SWyllys.Ingersoll@Sun.COM     {
109*12720SWyllys.Ingersoll@Sun.COM         // Our buffer wasn't big enough. Shouldn't happen.
110*12720SWyllys.Ingersoll@Sun.COM         FATAL_ASSERT(0);
111*12720SWyllys.Ingersoll@Sun.COM     }
112*12720SWyllys.Ingersoll@Sun.COM 
113*12720SWyllys.Ingersoll@Sun.COM     return;
114*12720SWyllys.Ingersoll@Sun.COM 
115*12720SWyllys.Ingersoll@Sun.COM }
116*12720SWyllys.Ingersoll@Sun.COM 
117*12720SWyllys.Ingersoll@Sun.COM // Find header in KMSAgentStringUtilities.h
ConvertUTF8HexStringToBinary(const char * i_sHexString,unsigned char * o_pBinaryBuffer)118*12720SWyllys.Ingersoll@Sun.COM int ConvertUTF8HexStringToBinary(
119*12720SWyllys.Ingersoll@Sun.COM             const char* i_sHexString,
120*12720SWyllys.Ingersoll@Sun.COM             unsigned char* o_pBinaryBuffer)
121*12720SWyllys.Ingersoll@Sun.COM {
122*12720SWyllys.Ingersoll@Sun.COM     int iHexLen = i_sHexString ? strlen(i_sHexString) : 0;
123*12720SWyllys.Ingersoll@Sun.COM     FATAL_ASSERT( (iHexLen % 2) == 0 ); // to be valid, the hex string must have an even number of characters
124*12720SWyllys.Ingersoll@Sun.COM 
125*12720SWyllys.Ingersoll@Sun.COM     if ( !o_pBinaryBuffer )
126*12720SWyllys.Ingersoll@Sun.COM     {
127*12720SWyllys.Ingersoll@Sun.COM        return ( iHexLen / 2 );
128*12720SWyllys.Ingersoll@Sun.COM     }
129*12720SWyllys.Ingersoll@Sun.COM 
130*12720SWyllys.Ingersoll@Sun.COM     if ( iHexLen <= 0 )
131*12720SWyllys.Ingersoll@Sun.COM     {
132*12720SWyllys.Ingersoll@Sun.COM         return 0;
133*12720SWyllys.Ingersoll@Sun.COM     }
134*12720SWyllys.Ingersoll@Sun.COM 
135*12720SWyllys.Ingersoll@Sun.COM     int iDigitValue = 0;
136*12720SWyllys.Ingersoll@Sun.COM 
137*12720SWyllys.Ingersoll@Sun.COM     for ( int i = 0; i < iHexLen; i++)
138*12720SWyllys.Ingersoll@Sun.COM     {
139*12720SWyllys.Ingersoll@Sun.COM         if (i_sHexString[i] >= '0' && i_sHexString[i] <= '9')
140*12720SWyllys.Ingersoll@Sun.COM         {
141*12720SWyllys.Ingersoll@Sun.COM             iDigitValue = i_sHexString[i] - '0';
142*12720SWyllys.Ingersoll@Sun.COM         }
143*12720SWyllys.Ingersoll@Sun.COM         else if (i_sHexString[i] >= 'A' && i_sHexString[i] <= 'F')
144*12720SWyllys.Ingersoll@Sun.COM         {
145*12720SWyllys.Ingersoll@Sun.COM             iDigitValue = i_sHexString[i] - 'A' + 10;
146*12720SWyllys.Ingersoll@Sun.COM         }
147*12720SWyllys.Ingersoll@Sun.COM         else if (i_sHexString[i] >= 'a' && i_sHexString[i] <= 'f')
148*12720SWyllys.Ingersoll@Sun.COM         {
149*12720SWyllys.Ingersoll@Sun.COM             iDigitValue = i_sHexString[i] - 'a' + 10;
150*12720SWyllys.Ingersoll@Sun.COM         }
151*12720SWyllys.Ingersoll@Sun.COM         else
152*12720SWyllys.Ingersoll@Sun.COM         {
153*12720SWyllys.Ingersoll@Sun.COM             iDigitValue = 0;
154*12720SWyllys.Ingersoll@Sun.COM         }
155*12720SWyllys.Ingersoll@Sun.COM 
156*12720SWyllys.Ingersoll@Sun.COM         if (i % 2 == 0)
157*12720SWyllys.Ingersoll@Sun.COM         {
158*12720SWyllys.Ingersoll@Sun.COM             o_pBinaryBuffer[i/2] = (char)(iDigitValue << 4);
159*12720SWyllys.Ingersoll@Sun.COM         }
160*12720SWyllys.Ingersoll@Sun.COM         else
161*12720SWyllys.Ingersoll@Sun.COM         {
162*12720SWyllys.Ingersoll@Sun.COM             o_pBinaryBuffer[i/2] |= (char)iDigitValue;
163*12720SWyllys.Ingersoll@Sun.COM         }
164*12720SWyllys.Ingersoll@Sun.COM     }
165*12720SWyllys.Ingersoll@Sun.COM 
166*12720SWyllys.Ingersoll@Sun.COM     return ( iHexLen / 2 );
167*12720SWyllys.Ingersoll@Sun.COM }
168*12720SWyllys.Ingersoll@Sun.COM 
169*12720SWyllys.Ingersoll@Sun.COM // Find header in KMSAgentStringUtilities.h
ConvertBinaryToUTF8HexString(char * const o_sHexString,const unsigned char * const i_pBinaryBuffer,int i_iBinaryBufferSize)170*12720SWyllys.Ingersoll@Sun.COM void ConvertBinaryToUTF8HexString(
171*12720SWyllys.Ingersoll@Sun.COM                              char* const                o_sHexString,
172*12720SWyllys.Ingersoll@Sun.COM                              const unsigned char* const i_pBinaryBuffer,
173*12720SWyllys.Ingersoll@Sun.COM                              int                        i_iBinaryBufferSize )
174*12720SWyllys.Ingersoll@Sun.COM {
175*12720SWyllys.Ingersoll@Sun.COM     const char HEXCHARS[] = "0123456789ABCDEF";
176*12720SWyllys.Ingersoll@Sun.COM 
177*12720SWyllys.Ingersoll@Sun.COM     FATAL_ASSERT( o_sHexString );
178*12720SWyllys.Ingersoll@Sun.COM 
179*12720SWyllys.Ingersoll@Sun.COM     if ( (i_pBinaryBuffer == 0) || (i_iBinaryBufferSize == 0) )
180*12720SWyllys.Ingersoll@Sun.COM     {
181*12720SWyllys.Ingersoll@Sun.COM         strcpy(o_sHexString, "");
182*12720SWyllys.Ingersoll@Sun.COM         return;
183*12720SWyllys.Ingersoll@Sun.COM     }
184*12720SWyllys.Ingersoll@Sun.COM 
185*12720SWyllys.Ingersoll@Sun.COM     FATAL_ASSERT( i_pBinaryBuffer );
186*12720SWyllys.Ingersoll@Sun.COM 
187*12720SWyllys.Ingersoll@Sun.COM     for ( int i = 0; i < (2 * i_iBinaryBufferSize); i++ )
188*12720SWyllys.Ingersoll@Sun.COM     {
189*12720SWyllys.Ingersoll@Sun.COM         unsigned char ucFourBits = i_pBinaryBuffer[i / 2];
190*12720SWyllys.Ingersoll@Sun.COM         if ( (i % 2) == 0 ) // high four bits of the current byte
191*12720SWyllys.Ingersoll@Sun.COM             ucFourBits = (ucFourBits >> 4) & 0xF; // shift down and blank out upper bits
192*12720SWyllys.Ingersoll@Sun.COM         else                // low four bits of the current byte
193*12720SWyllys.Ingersoll@Sun.COM             ucFourBits = ucFourBits & 0xF; // blank out upper bits
194*12720SWyllys.Ingersoll@Sun.COM 
195*12720SWyllys.Ingersoll@Sun.COM         o_sHexString[i] = HEXCHARS[ucFourBits];
196*12720SWyllys.Ingersoll@Sun.COM     }
197*12720SWyllys.Ingersoll@Sun.COM 
198*12720SWyllys.Ingersoll@Sun.COM     o_sHexString[i_iBinaryBufferSize * 2] = '\0';
199*12720SWyllys.Ingersoll@Sun.COM 
200*12720SWyllys.Ingersoll@Sun.COM     return;
201*12720SWyllys.Ingersoll@Sun.COM }
202*12720SWyllys.Ingersoll@Sun.COM 
203*12720SWyllys.Ingersoll@Sun.COM 
204*12720SWyllys.Ingersoll@Sun.COM // Find header in StringUtilities.h
GetCurrentDateTimeISO8601UTC(char * const o_psDateTimeISO8601UTC,int i_iLength)205*12720SWyllys.Ingersoll@Sun.COM void GetCurrentDateTimeISO8601UTC(char* const o_psDateTimeISO8601UTC,
206*12720SWyllys.Ingersoll@Sun.COM                                   int i_iLength)
207*12720SWyllys.Ingersoll@Sun.COM {
208*12720SWyllys.Ingersoll@Sun.COM 
209*12720SWyllys.Ingersoll@Sun.COM #ifndef METAWARE
210*12720SWyllys.Ingersoll@Sun.COM     timeb stTime;
211*12720SWyllys.Ingersoll@Sun.COM     ftime(&stTime);
212*12720SWyllys.Ingersoll@Sun.COM 
213*12720SWyllys.Ingersoll@Sun.COM     FATAL_ASSERT( o_psDateTimeISO8601UTC );
214*12720SWyllys.Ingersoll@Sun.COM 
215*12720SWyllys.Ingersoll@Sun.COM     struct tm* pstTime = gmtime( &(stTime.time) );
216*12720SWyllys.Ingersoll@Sun.COM 
217*12720SWyllys.Ingersoll@Sun.COM     K_snprintf(
218*12720SWyllys.Ingersoll@Sun.COM         o_psDateTimeISO8601UTC,
219*12720SWyllys.Ingersoll@Sun.COM         i_iLength,
220*12720SWyllys.Ingersoll@Sun.COM         "%04d-%02d-%02d %02d:%02d:%02d.%03dZ",
221*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_year+1900,
222*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_mon+1,
223*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_mday,
224*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_hour,
225*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_min,
226*12720SWyllys.Ingersoll@Sun.COM         pstTime->tm_sec,
227*12720SWyllys.Ingersoll@Sun.COM         stTime.millitm);
228*12720SWyllys.Ingersoll@Sun.COM 
229*12720SWyllys.Ingersoll@Sun.COM #else
230*12720SWyllys.Ingersoll@Sun.COM     // no time functions for the metaware environment
231*12720SWyllys.Ingersoll@Sun.COM     strcpy( o_psDateTimeISO8601UTC, "" );
232*12720SWyllys.Ingersoll@Sun.COM #endif
233*12720SWyllys.Ingersoll@Sun.COM     return;
234*12720SWyllys.Ingersoll@Sun.COM }
235*12720SWyllys.Ingersoll@Sun.COM 
236