Win32: fix an incorrect error status being propagated to the caller in case
[svn/apache.git] / subversion / libsvn_subr / win32_xlate.c
blob2f508c930af6cc5ae71c4cea50b8eb0fdabab2da
1 /*
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
20 * under the License.
21 * ====================================================================
24 /* prevent "empty compilation unit" warning on e.g. UNIX */
25 typedef int win32_xlate__dummy;
27 #ifdef WIN32
29 /* Define _WIN32_DCOM for CoInitializeEx(). */
30 #define _WIN32_DCOM
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 */
38 #include <winsock2.h>
39 #include <Ws2tcpip.h>
40 #include <mlang.h>
42 #include <apr.h>
43 #include <apr_errno.h>
44 #include <apr_portable.h>
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"
52 #include "win32_xlate.h"
54 #include "svn_private_config.h"
56 static svn_atomic_t com_initialized = 0;
58 /* Initializes COM and keeps COM available until process exit.
59 Implements svn_atomic__init_once init_func */
60 static svn_error_t *
61 initialize_com(void *baton, apr_pool_t* pool)
63 /* Try to initialize for apartment-threaded object concurrency. */
64 HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
66 if (hr == RPC_E_CHANGED_MODE)
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);
74 if (FAILED(hr))
75 return svn_error_create(APR_EGENERAL, NULL, NULL);
77 return SVN_NO_ERROR;
80 struct svn_subr__win32_xlate_t
82 UINT from_page_id;
83 UINT to_page_id;
86 static apr_status_t
87 get_page_id_from_name(UINT *page_id_p, const char *page_name, apr_pool_t *pool)
89 IMultiLanguage * mlang = NULL;
90 HRESULT hr;
91 MIMECSETINFO page_info;
92 WCHAR ucs2_page_name[128];
93 svn_error_t *err;
95 if (page_name == SVN_APR_DEFAULT_CHARSET)
97 *page_id_p = CP_ACP;
98 return APR_SUCCESS;
100 else if (page_name == SVN_APR_LOCALE_CHARSET)
102 *page_id_p = CP_THREAD_ACP; /* Valid on Windows 2000+ */
103 return APR_SUCCESS;
105 else if (!strcmp(page_name, "UTF-8"))
107 *page_id_p = CP_UTF8;
108 return APR_SUCCESS;
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'))
119 int page_id;
121 err = svn_cstring_atoi(&page_id, page_name + 2);
122 if (err)
124 apr_status_t saved = err->apr_err;
125 svn_error_clear(err);
126 return saved;
129 *page_id_p = page_id;
130 return APR_SUCCESS;
133 err = svn_atomic__init_once(&com_initialized, initialize_com, NULL, pool);
134 if (err)
136 apr_status_t saved = err->apr_err;
137 svn_error_clear(err);
138 return saved; /* probably SVN_ERR_ATOMIC_INIT_FAILURE */
141 hr = CoCreateInstance(&CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER,
142 &IID_IMultiLanguage, (void **) &mlang);
144 if (FAILED(hr))
145 return APR_EGENERAL;
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))
154 mlang->lpVtbl->Release(mlang);
155 return APR_EINVAL;
158 if (page_info.uiInternetEncoding)
159 *page_id_p = page_info.uiInternetEncoding;
160 else
161 *page_id_p = page_info.uiCodePage;
163 mlang->lpVtbl->Release(mlang);
165 return APR_SUCCESS;
168 apr_status_t
169 svn_subr__win32_xlate_open(svn_subr__win32_xlate_t **xlate_p, const char *topage,
170 const char *frompage, apr_pool_t *pool)
172 UINT from_page_id, to_page_id;
173 apr_status_t apr_err = APR_SUCCESS;
174 svn_subr__win32_xlate_t *xlate;
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);
180 if (apr_err == APR_SUCCESS)
182 xlate = apr_palloc(pool, sizeof(*xlate));
183 xlate->from_page_id = from_page_id;
184 xlate->to_page_id = to_page_id;
186 *xlate_p = xlate;
189 return apr_err;
192 apr_status_t
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)
199 WCHAR * wide_str;
200 int retval, wide_size;
202 if (src_length == 0)
204 *dest = svn_stringbuf_create_empty(pool);
205 return APR_SUCCESS;
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();
213 wide_size = retval;
215 /* Allocate temporary buffer for small strings on stack instead of heap. */
216 if (wide_size <= MAX_PATH)
218 wide_str = alloca(wide_size * sizeof(WCHAR));
220 else
222 wide_str = apr_palloc(pool, wide_size * sizeof(WCHAR));
225 retval = MultiByteToWideChar(handle->from_page_id, 0, src_data, src_length,
226 wide_str, wide_size);
228 if (retval == 0)
229 return apr_get_os_error();
231 retval = WideCharToMultiByte(handle->to_page_id, 0, wide_str, wide_size,
232 NULL, 0, NULL, NULL);
234 if (retval == 0)
235 return apr_get_os_error();
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;
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();
247 (*dest)->len = retval;
248 return APR_SUCCESS;
251 #else /* !WIN32 */
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;
258 #endif /* WIN32 */