1 /*
2 * win32_xlate.c : Windows xlate stuff.
3 *
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 * ====================================================================
22 */
23
24 /* prevent "empty compilation unit" warning on e.g. UNIX */
25 typedef int win32_xlate__dummy;
26
27 #ifdef WIN32
28
29 /* Define _WIN32_DCOM for CoInitializeEx(). */
30 #define _WIN32_DCOM
31
32 /* We must include windows.h ourselves or apr.h includes it for us with
33 many ignore options set. Including Winsock is required to resolve IPv6
34 compilation errors. APR_HAVE_IPV6 is only defined after including
35 apr.h, so we can't detect this case here. */
36
37 /* winsock2.h includes windows.h */
38 #include <winsock2.h>
39 #include <Ws2tcpip.h>
40 #include <mlang.h>
41
42 #include <apr.h>
43 #include <apr_errno.h>
44 #include <apr_portable.h>
45
46 #include "svn_pools.h"
47 #include "svn_string.h"
48 #include "svn_utf.h"
49 #include "private/svn_atomic.h"
50 #include "private/svn_subr_private.h"
51
52 #include "win32_xlate.h"
53
54 #include "svn_private_config.h"
55
56 static svn_atomic_t com_initialized = 0;
57
58 /* Initializes COM and keeps COM available until process exit.
59 Implements svn_atomic__init_once init_func */
60 static svn_error_t *
initialize_com(void * baton,apr_pool_t * pool)61 initialize_com(void *baton, apr_pool_t* pool)
62 {
63 /* Try to initialize for apartment-threaded object concurrency. */
64 HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
65
66 if (hr == RPC_E_CHANGED_MODE)
67 {
68 /* COM already initalized for multi-threaded object concurrency. We are
69 neutral to object concurrency so try to initalize it in the same way
70 for us, to keep an handle open. */
71 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
72 }
73
74 if (FAILED(hr))
75 return svn_error_create(APR_EGENERAL, NULL, NULL);
76
77 return SVN_NO_ERROR;
78 }
79
80 struct svn_subr__win32_xlate_t
81 {
82 UINT from_page_id;
83 UINT to_page_id;
84 };
85
86 static apr_status_t
get_page_id_from_name(UINT * page_id_p,const char * page_name,apr_pool_t * pool)87 get_page_id_from_name(UINT *page_id_p, const char *page_name, apr_pool_t *pool)
88 {
89 IMultiLanguage * mlang = NULL;
90 HRESULT hr;
91 MIMECSETINFO page_info;
92 WCHAR ucs2_page_name[128];
93 svn_error_t *err;
94
95 if (page_name == SVN_APR_DEFAULT_CHARSET)
96 {
97 *page_id_p = CP_ACP;
98 return APR_SUCCESS;
99 }
100 else if (page_name == SVN_APR_LOCALE_CHARSET)
101 {
102 *page_id_p = CP_THREAD_ACP; /* Valid on Windows 2000+ */
103 return APR_SUCCESS;
104 }
105 else if (!strcmp(page_name, "UTF-8"))
106 {
107 *page_id_p = CP_UTF8;
108 return APR_SUCCESS;
109 }
110
111 /* Use codepage identifier nnn if the codepage name is in the form
112 of "CPnnn".
113 We need this code since apr_os_locale_encoding() and svn_cmdline_init()
114 generates such codepage names even if they are not valid IANA charset
115 name. */
116 if ((page_name[0] == 'c' || page_name[0] == 'C')
117 && (page_name[1] == 'p' || page_name[1] == 'P'))
118 {
119 int page_id;
120
121 err = svn_cstring_atoi(&page_id, page_name + 2);
122 if (err)
123 {
124 apr_status_t saved = err->apr_err;
125 svn_error_clear(err);
126 return saved;
127 }
128
129 *page_id_p = page_id;
130 return APR_SUCCESS;
131 }
132
133 err = svn_atomic__init_once(&com_initialized, initialize_com, NULL, pool);
134 if (err)
135 {
136 apr_status_t saved = err->apr_err;
137 svn_error_clear(err);
138 return saved; /* probably SVN_ERR_ATOMIC_INIT_FAILURE */
139 }
140
141 hr = CoCreateInstance(&CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER,
142 &IID_IMultiLanguage, (void **) &mlang);
143
144 if (FAILED(hr))
145 return APR_EGENERAL;
146
147 /* Convert page name to wide string. */
148 MultiByteToWideChar(CP_UTF8, 0, page_name, -1, ucs2_page_name,
149 sizeof(ucs2_page_name) / sizeof(ucs2_page_name[0]));
150 memset(&page_info, 0, sizeof(page_info));
151 hr = mlang->lpVtbl->GetCharsetInfo(mlang, ucs2_page_name, &page_info);
152 if (FAILED(hr))
153 {
154 mlang->lpVtbl->Release(mlang);
155 return APR_EINVAL;
156 }
157
158 if (page_info.uiInternetEncoding)
159 *page_id_p = page_info.uiInternetEncoding;
160 else
161 *page_id_p = page_info.uiCodePage;
162
163 mlang->lpVtbl->Release(mlang);
164
165 return APR_SUCCESS;
166 }
167
168 apr_status_t
svn_subr__win32_xlate_open(svn_subr__win32_xlate_t ** xlate_p,const char * topage,const char * frompage,apr_pool_t * pool)169 svn_subr__win32_xlate_open(svn_subr__win32_xlate_t **xlate_p, const char *topage,
170 const char *frompage, apr_pool_t *pool)
171 {
172 UINT from_page_id, to_page_id;
173 apr_status_t apr_err = APR_SUCCESS;
174 svn_subr__win32_xlate_t *xlate;
175
176 apr_err = get_page_id_from_name(&to_page_id, topage, pool);
177 if (apr_err == APR_SUCCESS)
178 apr_err = get_page_id_from_name(&from_page_id, frompage, pool);
179
180 if (apr_err == APR_SUCCESS)
181 {
182 xlate = apr_palloc(pool, sizeof(*xlate));
183 xlate->from_page_id = from_page_id;
184 xlate->to_page_id = to_page_id;
185
186 *xlate_p = xlate;
187 }
188
189 return apr_err;
190 }
191
192 apr_status_t
svn_subr__win32_xlate_to_stringbuf(svn_subr__win32_xlate_t * handle,const char * src_data,apr_size_t src_length,svn_stringbuf_t ** dest,apr_pool_t * pool)193 svn_subr__win32_xlate_to_stringbuf(svn_subr__win32_xlate_t *handle,
194 const char *src_data,
195 apr_size_t src_length,
196 svn_stringbuf_t **dest,
197 apr_pool_t *pool)
198 {
199 WCHAR * wide_str;
200 int retval, wide_size;
201
202 if (src_length == 0)
203 {
204 *dest = svn_stringbuf_create_empty(pool);
205 return APR_SUCCESS;
206 }
207
208 retval = MultiByteToWideChar(handle->from_page_id, 0, src_data, src_length,
209 NULL, 0);
210 if (retval == 0)
211 return apr_get_os_error();
212
213 wide_size = retval;
214
215 /* Allocate temporary buffer for small strings on stack instead of heap. */
216 if (wide_size <= MAX_PATH)
217 {
218 wide_str = alloca(wide_size * sizeof(WCHAR));
219 }
220 else
221 {
222 wide_str = apr_palloc(pool, wide_size * sizeof(WCHAR));
223 }
224
225 retval = MultiByteToWideChar(handle->from_page_id, 0, src_data, src_length,
226 wide_str, wide_size);
227
228 if (retval == 0)
229 return apr_get_os_error();
230
231 retval = WideCharToMultiByte(handle->to_page_id, 0, wide_str, wide_size,
232 NULL, 0, NULL, NULL);
233
234 if (retval == 0)
235 return apr_get_os_error();
236
237 /* Ensure that buffer is enough to hold result string and termination
238 character. */
239 *dest = svn_stringbuf_create_ensure(retval + 1, pool);
240 (*dest)->len = retval;
241
242 retval = WideCharToMultiByte(handle->to_page_id, 0, wide_str, wide_size,
243 (*dest)->data, (*dest)->len, NULL, NULL);
244 if (retval == 0)
245 return apr_get_os_error();
246
247 (*dest)->len = retval;
248 return APR_SUCCESS;
249 }
250
251 #else /* !WIN32 */
252
253 /* Silence OSX ranlib warnings about object files with no symbols. */
254 #include <apr.h>
255 extern const apr_uint32_t svn__fake__win32_xlate;
256 const apr_uint32_t svn__fake__win32_xlate = 0xdeadbeef;
257
258 #endif /* WIN32 */
259