2 * win32_xlate.c : Windows xlate stuff.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * ====================================================================
24 /* prevent "empty compilation unit" warning on e.g. UNIX */
25 typedef int win32_xlate__dummy;
29 /* Define _WIN32_DCOM for CoInitializeEx(). */
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. */
37 /* winsock2.h includes windows.h */
43 #include <apr_errno.h>
44 #include <apr_portable.h>
46 #include "svn_pools.h"
47 #include "svn_string.h"
49 #include "private/svn_atomic.h"
51 #include "win32_xlate.h"
53 static svn_atomic_t com_initialized = 0;
55 /* Initializes COM and keeps COM available until process exit.
56 Implements svn_atomic__init_once init_func */
58 initialize_com(void *baton, apr_pool_t* pool)
60 /* Try to initialize for apartment-threaded object concurrency. */
61 HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
63 if (hr == RPC_E_CHANGED_MODE)
65 /* COM already initalized for multi-threaded object concurrency. We are
66 neutral to object concurrency so try to initalize it in the same way
67 for us, to keep an handle open. */
68 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
72 return svn_error_create(APR_EGENERAL, NULL, NULL);
77 typedef struct win32_xlate_t
84 get_page_id_from_name(UINT *page_id_p, const char *page_name, apr_pool_t *pool)
86 IMultiLanguage * mlang = NULL;
88 MIMECSETINFO page_info;
89 WCHAR ucs2_page_name[128];
92 if (page_name == SVN_APR_DEFAULT_CHARSET)
97 else if (page_name == SVN_APR_LOCALE_CHARSET)
99 *page_id_p = CP_THREAD_ACP; /* Valid on Windows 2000+ */
102 else if (!strcmp(page_name, "UTF-8"))
104 *page_id_p = CP_UTF8;
108 /* Use codepage identifier nnn if the codepage name is in the form
110 We need this code since apr_os_locale_encoding() and svn_cmdline_init()
111 generates such codepage names even if they are not valid IANA charset
113 if ((page_name[0] == 'c' || page_name[0] == 'C')
114 && (page_name[1] == 'p' || page_name[1] == 'P'))
116 *page_id_p = atoi(page_name + 2);
120 err = svn_atomic__init_once(&com_initialized, initialize_com, NULL, pool);
124 svn_error_clear(err);
128 hr = CoCreateInstance(&CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER,
129 &IID_IMultiLanguage, (void **) &mlang);
134 /* Convert page name to wide string. */
135 MultiByteToWideChar(CP_UTF8, 0, page_name, -1, ucs2_page_name,
136 sizeof(ucs2_page_name) / sizeof(ucs2_page_name[0]));
137 memset(&page_info, 0, sizeof(page_info));
138 hr = mlang->lpVtbl->GetCharsetInfo(mlang, ucs2_page_name, &page_info);
141 mlang->lpVtbl->Release(mlang);
145 if (page_info.uiInternetEncoding)
146 *page_id_p = page_info.uiInternetEncoding;
148 *page_id_p = page_info.uiCodePage;
150 mlang->lpVtbl->Release(mlang);
156 svn_subr__win32_xlate_open(win32_xlate_t **xlate_p, const char *topage,
157 const char *frompage, apr_pool_t *pool)
159 UINT from_page_id, to_page_id;
160 apr_status_t apr_err = APR_SUCCESS;
161 win32_xlate_t *xlate;
163 apr_err = get_page_id_from_name(&to_page_id, topage, pool);
164 if (apr_err == APR_SUCCESS)
165 apr_err = get_page_id_from_name(&from_page_id, frompage, pool);
167 if (apr_err == APR_SUCCESS)
169 xlate = apr_palloc(pool, sizeof(*xlate));
170 xlate->from_page_id = from_page_id;
171 xlate->to_page_id = to_page_id;
180 svn_subr__win32_xlate_to_stringbuf(win32_xlate_t *handle,
181 const char *src_data,
182 apr_size_t src_length,
183 svn_stringbuf_t **dest,
187 int retval, wide_size;
191 *dest = svn_stringbuf_create_empty(pool);
195 retval = MultiByteToWideChar(handle->from_page_id, 0, src_data, src_length,
198 return apr_get_os_error();
202 /* Allocate temporary buffer for small strings on stack instead of heap. */
203 if (wide_size <= MAX_PATH)
205 wide_str = alloca(wide_size * sizeof(WCHAR));
209 wide_str = apr_palloc(pool, wide_size * sizeof(WCHAR));
212 retval = MultiByteToWideChar(handle->from_page_id, 0, src_data, src_length,
213 wide_str, wide_size);
216 return apr_get_os_error();
218 retval = WideCharToMultiByte(handle->to_page_id, 0, wide_str, wide_size,
219 NULL, 0, NULL, NULL);
222 return apr_get_os_error();
224 /* Ensure that buffer is enough to hold result string and termination
226 *dest = svn_stringbuf_create_ensure(retval + 1, pool);
227 (*dest)->len = retval;
229 retval = WideCharToMultiByte(handle->to_page_id, 0, wide_str, wide_size,
230 (*dest)->data, (*dest)->len, NULL, NULL);
232 return apr_get_os_error();
234 (*dest)->len = retval;