1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /**
27  * @file KMSAgentStringUtilities.h
28  */
29 
30 #ifndef KMSAgentStringUtilities_h
31 #define KMSAgentStringUtilities_h
32 
33 #ifdef WIN32
34 #pragma warning(disable: 4786)
35 #endif
36 
37 #include <stdio.h>
38 
39 #include "SYSCommon.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 //
46 // Functions for conversions between integers and strings.
47 //
48 
49 /*---------------------------------------------------------------------------*/
50 /**
51  *  Converts a UTF8 string to an int64.
52  *
53  *  @param   i_sNumber: A string representation of the number to convert.
54  *  @return  The integer the input string represented.
55  */
56 /*---------------------------------------------------------------------------*/
57  int64 UTF8ToInt64( const char* i_sNumber );
58 
59 /**
60  * Formats an int64 into a UTF8 string.
61  *
62  * A note on padding: If i_bPad is true, the string will be padded to the
63  * maximum size necessary to hold a an int64 representation. For decimal this
64  * is 19, for hex it is 16.
65  *
66  *  @param i_iNumber The number to format.
67  *  @param i_bPad If true, the string will be padded with zeroes. (See note above.)
68  *  @param i_bHex Indicates whether the string format should be a hexadecimal
69  *    representation of the integer (true) or a decimal representation (false).
70  *  @param o_psUTF8 the string representation of the integer
71  *
72  *  @return  void
73  */
74 void Int64ToUTF8(char* const o_psUTF8,
75                  int64 i_iNumber,
76                  int i_bPad,
77                  int i_bHex );
78 
79 //
80 // Functions for converting between binary buffer and hex string
81 //
82 
83 /*--------------------------------------------------------------------------*/
84 /**
85  *  Converts a UTF8 hex string to its binary representation.
86  *
87  *  If o_pBinaryBuffer is null, the function will return the required size.
88  *  (The required size is always strlen(i_sHexString)/2.)
89  *
90  *  @param   i_sHexString:     The hex string to convert.
91  *  @param   o_pBinaryBuffer:  The buffer in which to put the binary
92  *     representation of the hex string. If this is null, the function
93  *     returns the required size.
94  *     If this is not null, it must be large enough to hold binary conversion.
95  *
96  *  @return The number of bytes put into o_pBinaryBuffer (or the number of bytes
97  *     required, if o_pBinaryBuffer was null).
98  */
99 /*---------------------------------------------------------------------------*/
100 int ConvertUTF8HexStringToBinary(
101    const char* i_sHexString,
102    unsigned char* o_pBinaryBuffer);
103 
104 /**
105  * Converts a binary buffer to its UTF8 hex string representation.
106  *
107  *  @param i_pBinaryBuffer: The binary buffer to convert.
108  *  @param i_iBinaryBufferSize: The size of i_pBinaryBuffer;
109  *  @param o_sHexString The hex string representation of the
110  *                      binary buffer which should be at least
111  *                      (i_iBinaryBufferSize * 2) + 1 characters long
112  */
113 void ConvertBinaryToUTF8HexString(
114                              char* const                o_sHexString,
115                              const unsigned char* const i_pBinaryBuffer,
116                              int                        i_iBinaryBufferSize );
117 
118 //
119 // Functions for date strings
120 //
121 
122 /**
123  *  populates o_psDateTimeISO8601UTC with a null terminated ISO 8601
124  *  formatted timestamp string from the current UTC time of the
125  *  system.  The timestamp length will be restricted to i_iLength-1
126  *  characters.
127  */
128 void GetCurrentDateTimeISO8601UTC(char* const o_psDateTimeISO8601UTC,
129                                   int i_iLength);
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif //KMSAgentStringUtilities_h
136