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"
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 */
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
);
75 return svn_error_create(APR_EGENERAL
, NULL
, NULL
);
80 struct svn_subr__win32_xlate_t
87 get_page_id_from_name(UINT
*page_id_p
, const char *page_name
, apr_pool_t
*pool
)
89 IMultiLanguage
* mlang
= NULL
;
91 MIMECSETINFO page_info
;
92 WCHAR ucs2_page_name
[128];
95 if (page_name
== SVN_APR_DEFAULT_CHARSET
)
100 else if (page_name
== SVN_APR_LOCALE_CHARSET
)
102 *page_id_p
= CP_THREAD_ACP
; /* Valid on Windows 2000+ */
105 else if (!strcmp(page_name
, "UTF-8"))
107 *page_id_p
= CP_UTF8
;
111 /* Use codepage identifier nnn if the codepage name is in the form
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
116 if ((page_name
[0] == 'c' || page_name
[0] == 'C')
117 && (page_name
[1] == 'p' || page_name
[1] == 'P'))
121 err
= svn_cstring_atoi(&page_id
, page_name
+ 2);
124 apr_status_t saved
= err
->apr_err
;
125 svn_error_clear(err
);
129 *page_id_p
= page_id
;
133 err
= svn_atomic__init_once(&com_initialized
, initialize_com
, NULL
, pool
);
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
);
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
);
154 mlang
->lpVtbl
->Release(mlang
);
158 if (page_info
.uiInternetEncoding
)
159 *page_id_p
= page_info
.uiInternetEncoding
;
161 *page_id_p
= page_info
.uiCodePage
;
163 mlang
->lpVtbl
->Release(mlang
);
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
;
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
,
200 int retval
, wide_size
;
204 *dest
= svn_stringbuf_create_empty(pool
);
208 retval
= MultiByteToWideChar(handle
->from_page_id
, 0, src_data
, src_length
,
211 return apr_get_os_error();
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
));
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
);
229 return apr_get_os_error();
231 retval
= WideCharToMultiByte(handle
->to_page_id
, 0, wide_str
, wide_size
,
232 NULL
, 0, NULL
, NULL
);
235 return apr_get_os_error();
237 /* Ensure that buffer is enough to hold result string and termination
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
);
245 return apr_get_os_error();
247 (*dest
)->len
= retval
;
253 /* Silence OSX ranlib warnings about object files with no symbols. */
255 extern const apr_uint32_t svn__fake__win32_xlate
;
256 const apr_uint32_t svn__fake__win32_xlate
= 0xdeadbeef;