]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/DLLX/StringServices.cpp
a9c1d8a01bf47720083659f1f4fd7224e4657236
[apple/mdnsresponder.git] / mDNSWindows / DLLX / StringServices.cpp
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16
17 Change History (most recent first):
18
19 $Log: StringServices.cpp,v $
20 Revision 1.2 2009/06/02 18:43:57 herscher
21 <rdar://problem/3948252> Allow component consumers to pass in null strings
22
23 Revision 1.1 2009/05/26 04:43:54 herscher
24 <rdar://problem/3948252> COM component that can be used with any .NET language and VB.
25
26
27 */
28
29 #include "StringServices.h"
30 #include <DebugServices.h>
31
32
33 extern BOOL
34 BSTRToUTF8
35 (
36 BSTR inString,
37 std::string & outString
38 )
39 {
40 USES_CONVERSION;
41
42 char * utf8String = NULL;
43 OSStatus err = kNoErr;
44
45 outString = "";
46
47 if ( inString )
48 {
49 TCHAR * utf16String = NULL;
50 size_t size = 0;
51
52 utf16String = OLE2T( inString );
53 require_action( utf16String != NULL, exit, err = kUnknownErr );
54
55 if ( wcslen( utf16String ) > 0 )
56 {
57 size = (size_t) WideCharToMultiByte( CP_UTF8, 0, utf16String, ( int ) wcslen( utf16String ), NULL, 0, NULL, NULL );
58 err = translate_errno( size != 0, GetLastError(), kUnknownErr );
59 require_noerr( err, exit );
60
61 try
62 {
63 utf8String = new char[ size + 1 ];
64 }
65 catch ( ... )
66 {
67 utf8String = NULL;
68 }
69
70 require_action( utf8String != NULL, exit, err = kNoMemoryErr );
71 size = (size_t) WideCharToMultiByte( CP_UTF8, 0, utf16String, ( int ) wcslen( utf16String ), utf8String, (int) size, NULL, NULL);
72 err = translate_errno( size != 0, GetLastError(), kUnknownErr );
73 require_noerr( err, exit );
74
75 // have to add the trailing 0 because WideCharToMultiByte doesn't do it,
76 // although it does return the correct size
77
78 utf8String[size] = '\0';
79 outString = utf8String;
80 }
81 }
82
83 exit:
84
85 if ( utf8String != NULL )
86 {
87 delete [] utf8String;
88 }
89
90 return ( !err ) ? TRUE : FALSE;
91 }
92
93
94 extern BOOL
95 UTF8ToBSTR
96 (
97 const char * inString,
98 CComBSTR & outString
99 )
100 {
101 wchar_t * unicode = NULL;
102 OSStatus err = 0;
103
104 if ( inString )
105 {
106 int n;
107
108 n = MultiByteToWideChar( CP_UTF8, 0, inString, -1, NULL, 0 );
109
110 if ( n > 0 )
111 {
112 try
113 {
114 unicode = new wchar_t[ n ];
115 }
116 catch ( ... )
117 {
118 unicode = NULL;
119 }
120
121 require_action( unicode, exit, err = ERROR_INSUFFICIENT_BUFFER );
122
123 n = MultiByteToWideChar( CP_UTF8, 0, inString, -1, unicode, n );
124 }
125
126 outString = unicode;
127 }
128
129 exit:
130
131 if ( unicode != NULL )
132 {
133 delete [] unicode;
134 }
135
136 return ( !err ) ? TRUE : FALSE;
137 }
138
139
140 BOOL
141 ByteArrayToVariant
142 (
143 const void * inArray,
144 size_t inArrayLen,
145 VARIANT * outVariant
146 )
147 {
148 LPBYTE buf = NULL;
149 HRESULT hr = 0;
150 BOOL ok = TRUE;
151
152 VariantClear( outVariant );
153 outVariant->vt = VT_ARRAY|VT_UI1;
154 outVariant->parray = SafeArrayCreateVector( VT_UI1, 0, ( ULONG ) inArrayLen );
155 require_action( outVariant->parray, exit, ok = FALSE );
156 hr = SafeArrayAccessData( outVariant->parray, (LPVOID *)&buf );
157 require_action( hr == S_OK, exit, ok = FALSE );
158 memcpy( buf, inArray, inArrayLen );
159 hr = SafeArrayUnaccessData( outVariant->parray );
160 require_action( hr == S_OK, exit, ok = FALSE );
161
162 exit:
163
164 return ok;
165 }
166
167
168 extern BOOL
169 VariantToByteArray
170 (
171 VARIANT * inVariant,
172 std::vector< BYTE > & outArray
173 )
174 {
175 SAFEARRAY * psa = NULL;
176 BYTE * pData = NULL;
177 ULONG cElements = 0;
178 HRESULT hr;
179 BOOL ok = TRUE;
180
181 require_action( V_VT( inVariant ) == ( VT_ARRAY|VT_UI1 ), exit, ok = FALSE );
182 psa = V_ARRAY( inVariant );
183 require_action( psa, exit, ok = FALSE );
184 require_action( SafeArrayGetDim( psa ) == 1, exit, ok = FALSE );
185 hr = SafeArrayAccessData( psa, ( LPVOID* )&pData );
186 require_action( hr == S_OK, exit, ok = FALSE );
187 cElements = psa->rgsabound[0].cElements;
188 outArray.reserve( cElements );
189 outArray.assign( cElements, 0 );
190 memcpy( &outArray[ 0 ], pData, cElements );
191 SafeArrayUnaccessData( psa );
192
193 exit:
194
195 return ok;
196 }