2 * utf.c: UTF-8 conversion routines
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 * ====================================================================
30 #include <apr_strings.h>
32 #include <apr_xlate.h>
33 #include <apr_atomic.h>
36 #include "svn_string.h"
37 #include "svn_error.h"
38 #include "svn_pools.h"
39 #include "svn_ctype.h"
41 #include "svn_private_config.h"
42 #include "win32_xlate.h"
44 #include "private/svn_utf_private.h"
45 #include "private/svn_dep_compat.h"
46 #include "private/svn_string_private.h"
47 #include "private/svn_mutex.h"
51 /* Use these static strings to maximize performance on standard conversions.
52 * Any strings on other locations are still valid, however.
54 static const char *SVN_UTF_NTOU_XLATE_HANDLE
= "svn-utf-ntou-xlate-handle";
55 static const char *SVN_UTF_UTON_XLATE_HANDLE
= "svn-utf-uton-xlate-handle";
57 static const char *SVN_APR_UTF8_CHARSET
= "UTF-8";
59 static svn_mutex__t
*xlate_handle_mutex
= NULL
;
60 static svn_boolean_t assume_native_charset_is_utf8
= FALSE
;
63 typedef svn_subr__win32_xlate_t xlate_handle_t
;
65 typedef apr_xlate_t xlate_handle_t
;
68 /* The xlate handle cache is a global hash table with linked lists of xlate
69 * handles. In multi-threaded environments, a thread "borrows" an xlate
70 * handle from the cache during a translation and puts it back afterwards.
71 * This avoids holding a global lock for all translations.
72 * If there is no handle for a particular key when needed, a new is
73 * handle is created and put in the cache after use.
74 * This means that there will be at most N handles open for a key, where N
75 * is the number of simultanous handles in use for that key. */
77 typedef struct xlate_handle_node_t
{
78 xlate_handle_t
*handle
;
79 /* FALSE if the handle is not valid, since its pool is being
82 /* The name of a char encoding or APR_LOCALE_CHARSET. */
83 const char *frompage
, *topage
;
84 struct xlate_handle_node_t
*next
;
85 } xlate_handle_node_t
;
87 /* This maps const char * userdata_key strings to xlate_handle_node_t **
88 handles to the first entry in the linked list of xlate handles. We don't
89 store the pointer to the list head directly in the hash table, since we
90 remove/insert entries at the head in the list in the code below, and
91 we can't use apr_hash_set() in each character translation because that
92 function allocates memory in each call where the value is non-NULL.
93 Since these allocations take place in a global pool, this would be a
95 static apr_hash_t
*xlate_handle_hash
= NULL
;
97 /* "1st level cache" to standard conversion maps. We may access these
98 * using atomic xchange ops, i.e. without further thread synchronization.
99 * If the respective item is NULL, fallback to hash lookup.
101 static void * volatile xlat_ntou_static_handle
= NULL
;
102 static void * volatile xlat_uton_static_handle
= NULL
;
104 /* Clean up the xlate handle cache. */
106 xlate_cleanup(void *arg
)
108 /* We set the cache variables to NULL so that translation works in other
109 cleanup functions, even if it isn't cached then. */
110 xlate_handle_hash
= NULL
;
112 /* ensure no stale objects get accessed */
113 xlat_ntou_static_handle
= NULL
;
114 xlat_uton_static_handle
= NULL
;
119 /* Set the handle of ARG to NULL. */
121 xlate_handle_node_cleanup(void *arg
)
123 xlate_handle_node_t
*node
= arg
;
130 svn_utf_initialize2(svn_boolean_t assume_native_utf8
,
133 if (!xlate_handle_hash
)
135 /* We create our own subpool, which we protect with the mutex.
136 We can't use the pool passed to us by the caller, since we will
137 use it for xlate handle allocations, possibly in multiple threads,
138 and pool allocation is not thread-safe. */
139 apr_pool_t
*subpool
= svn_pool_create(pool
);
141 svn_error_t
*err
= svn_mutex__init(&mutex
, TRUE
, subpool
);
144 svn_error_clear(err
);
148 xlate_handle_mutex
= mutex
;
149 xlate_handle_hash
= apr_hash_make(subpool
);
151 apr_pool_cleanup_register(subpool
, NULL
, xlate_cleanup
,
152 apr_pool_cleanup_null
);
155 if (!assume_native_charset_is_utf8
)
156 assume_native_charset_is_utf8
= assume_native_utf8
;
159 /* Return a unique string key based on TOPAGE and FROMPAGE. TOPAGE and
160 * FROMPAGE can be any valid arguments of the same name to
161 * apr_xlate_open(). Allocate the returned string in POOL. */
163 get_xlate_key(const char *topage
,
164 const char *frompage
,
167 /* In the cases of SVN_APR_LOCALE_CHARSET and SVN_APR_DEFAULT_CHARSET
168 * topage/frompage is really an int, not a valid string. So generate a
169 * unique key accordingly. */
170 if (frompage
== SVN_APR_LOCALE_CHARSET
)
171 frompage
= "APR_LOCALE_CHARSET";
172 else if (frompage
== SVN_APR_DEFAULT_CHARSET
)
173 frompage
= "APR_DEFAULT_CHARSET";
175 if (topage
== SVN_APR_LOCALE_CHARSET
)
176 topage
= "APR_LOCALE_CHARSET";
177 else if (topage
== SVN_APR_DEFAULT_CHARSET
)
178 topage
= "APR_DEFAULT_CHARSET";
180 return apr_pstrcat(pool
, "svn-utf-", frompage
, "to", topage
,
181 "-xlate-handle", SVN_VA_NULL
);
184 /* Atomically replace the content in *MEM with NEW_VALUE and return
185 * the previous content of *MEM. If atomicy cannot be guaranteed,
186 * *MEM will not be modified and NEW_VALUE is simply returned to
189 static APR_INLINE
void*
190 atomic_swap(void * volatile * mem
, void *new_value
)
193 return svn_atomic_xchgptr(mem
, new_value
);
195 /* no threads - no sync. necessary */
196 void *old_value
= (void*)*mem
;
202 /* Set *RET to a newly created handle node for converting from FROMPAGE
203 to TOPAGE, If apr_xlate_open() returns APR_EINVAL or APR_ENOTIMPL, set
204 (*RET)->handle to NULL. If fail for any other reason, return the error.
205 Allocate *RET and its xlate handle in POOL. */
207 xlate_alloc_handle(xlate_handle_node_t
**ret
,
208 const char *topage
, const char *frompage
,
211 apr_status_t apr_err
;
212 xlate_handle_t
*handle
;
215 /* The error handling doesn't support the following cases, since we don't
216 use them currently. Catch this here. */
217 SVN_ERR_ASSERT(frompage
!= SVN_APR_DEFAULT_CHARSET
218 && topage
!= SVN_APR_DEFAULT_CHARSET
219 && (frompage
!= SVN_APR_LOCALE_CHARSET
220 || topage
!= SVN_APR_LOCALE_CHARSET
));
222 /* Try to create a handle. */
224 apr_err
= svn_subr__win32_xlate_open(&handle
, topage
,
226 name
= "win32-xlate: ";
228 apr_err
= apr_xlate_open(&handle
, topage
, frompage
, pool
);
232 if (APR_STATUS_IS_EINVAL(apr_err
) || APR_STATUS_IS_ENOTIMPL(apr_err
))
234 else if (apr_err
!= APR_SUCCESS
)
237 char apr_strerr
[512];
239 /* Can't use svn_error_wrap_apr here because it calls functions in
240 this file, leading to infinite recursion. */
241 if (frompage
== SVN_APR_LOCALE_CHARSET
)
242 errstr
= apr_psprintf(pool
,
243 _("Can't create a character converter from "
244 "native encoding to '%s'"), topage
);
245 else if (topage
== SVN_APR_LOCALE_CHARSET
)
246 errstr
= apr_psprintf(pool
,
247 _("Can't create a character converter from "
248 "'%s' to native encoding"), frompage
);
250 errstr
= apr_psprintf(pool
,
251 _("Can't create a character converter from "
252 "'%s' to '%s'"), frompage
, topage
);
254 /* Just put the error on the stack, since svn_error_create duplicates it
255 later. APR_STRERR will be in the local encoding, not in UTF-8, though.
257 svn_strerror(apr_err
, apr_strerr
, sizeof(apr_strerr
));
258 return svn_error_createf(SVN_ERR_PLUGIN_LOAD_FAILURE
,
259 svn_error_create(apr_err
, NULL
, apr_strerr
),
260 "%s%s", name
, errstr
);
263 /* Allocate and initialize the node. */
264 *ret
= apr_palloc(pool
, sizeof(xlate_handle_node_t
));
265 (*ret
)->handle
= handle
;
266 (*ret
)->valid
= TRUE
;
267 (*ret
)->frompage
= ((frompage
!= SVN_APR_LOCALE_CHARSET
)
268 ? apr_pstrdup(pool
, frompage
) : frompage
);
269 (*ret
)->topage
= ((topage
!= SVN_APR_LOCALE_CHARSET
)
270 ? apr_pstrdup(pool
, topage
) : topage
);
273 /* If we are called from inside a pool cleanup handler, the just created
274 xlate handle will be closed when that handler returns by a newly
275 registered cleanup handler, however, the handle is still cached by us.
276 To prevent this, we register a cleanup handler that will reset the valid
277 flag of our node, so we don't use an invalid handle. */
279 apr_pool_cleanup_register(pool
, *ret
, xlate_handle_node_cleanup
,
280 apr_pool_cleanup_null
);
285 /* Extend xlate_alloc_handle by using USERDATA_KEY as a key in our
286 global hash map, if available.
288 Allocate *RET and its xlate handle in POOL if svn_utf_initialize()
289 hasn't been called or USERDATA_KEY is NULL. Else, allocate them
290 in the pool of xlate_handle_hash.
292 Note: this function is not thread-safe. Call get_xlate_handle_node
295 get_xlate_handle_node_internal(xlate_handle_node_t
**ret
,
296 const char *topage
, const char *frompage
,
297 const char *userdata_key
, apr_pool_t
*pool
)
299 /* If we already have a handle, just return it. */
300 if (userdata_key
&& xlate_handle_hash
)
302 xlate_handle_node_t
*old_node
= NULL
;
304 /* 2nd level: hash lookup */
305 xlate_handle_node_t
**old_node_p
= svn_hash_gets(xlate_handle_hash
,
308 old_node
= *old_node_p
;
311 /* Ensure that the handle is still valid. */
314 /* Remove from the list. */
315 *old_node_p
= old_node
->next
;
316 old_node
->next
= NULL
;
323 /* Note that we still have the mutex locked (if it is initialized), so we
324 can use the global pool for creating the new xlate handle. */
326 /* Use the correct pool for creating the handle. */
327 pool
= apr_hash_pool_get(xlate_handle_hash
);
329 return xlate_alloc_handle(ret
, topage
, frompage
, pool
);
332 /* Set *RET to a handle node for converting from FROMPAGE to TOPAGE,
333 creating the handle node if it doesn't exist in USERDATA_KEY.
334 If a node is not cached and apr_xlate_open() returns APR_EINVAL or
335 APR_ENOTIMPL, set (*RET)->handle to NULL. If fail for any other
336 reason, return the error.
338 Allocate *RET and its xlate handle in POOL if svn_utf_initialize()
339 hasn't been called or USERDATA_KEY is NULL. Else, allocate them
340 in the pool of xlate_handle_hash. */
342 get_xlate_handle_node(xlate_handle_node_t
**ret
,
343 const char *topage
, const char *frompage
,
344 const char *userdata_key
, apr_pool_t
*pool
)
346 xlate_handle_node_t
*old_node
= NULL
;
348 /* If we already have a handle, just return it. */
351 if (xlate_handle_hash
)
353 /* 1st level: global, static items */
354 if (userdata_key
== SVN_UTF_NTOU_XLATE_HANDLE
)
355 old_node
= atomic_swap(&xlat_ntou_static_handle
, NULL
);
356 else if (userdata_key
== SVN_UTF_UTON_XLATE_HANDLE
)
357 old_node
= atomic_swap(&xlat_uton_static_handle
, NULL
);
359 if (old_node
&& old_node
->valid
)
368 /* We fall back on a per-pool cache instead. */
369 apr_pool_userdata_get(&p
, userdata_key
, pool
);
371 /* Ensure that the handle is still valid. */
372 if (old_node
&& old_node
->valid
)
378 return xlate_alloc_handle(ret
, topage
, frompage
, pool
);
382 SVN_MUTEX__WITH_LOCK(xlate_handle_mutex
,
383 get_xlate_handle_node_internal(ret
,
392 /* Put back NODE into the xlate handle cache for use by other calls.
394 Note: this function is not thread-safe. Call put_xlate_handle_node
397 put_xlate_handle_node_internal(xlate_handle_node_t
*node
,
398 const char *userdata_key
)
400 xlate_handle_node_t
**node_p
= svn_hash_gets(xlate_handle_hash
, userdata_key
);
403 userdata_key
= apr_pstrdup(apr_hash_pool_get(xlate_handle_hash
),
405 node_p
= apr_palloc(apr_hash_pool_get(xlate_handle_hash
),
408 svn_hash_sets(xlate_handle_hash
, userdata_key
, node_p
);
410 node
->next
= *node_p
;
416 /* Put back NODE into the xlate handle cache for use by other calls.
417 If there is no global cache, store the handle in POOL.
418 Ignore errors related to locking/unlocking the mutex. */
420 put_xlate_handle_node(xlate_handle_node_t
*node
,
421 const char *userdata_key
,
424 assert(node
->next
== NULL
);
428 /* push previous global node to the hash */
429 if (xlate_handle_hash
)
431 /* 1st level: global, static items */
432 if (userdata_key
== SVN_UTF_NTOU_XLATE_HANDLE
)
433 node
= atomic_swap(&xlat_ntou_static_handle
, node
);
434 else if (userdata_key
== SVN_UTF_UTON_XLATE_HANDLE
)
435 node
= atomic_swap(&xlat_uton_static_handle
, node
);
439 SVN_MUTEX__WITH_LOCK(xlate_handle_mutex
,
440 put_xlate_handle_node_internal(node
,
445 /* Store it in the per-pool cache. */
446 apr_pool_userdata_set(node
, userdata_key
, apr_pool_cleanup_null
, pool
);
452 /* Return the apr_xlate handle for converting native characters to UTF-8. */
454 get_ntou_xlate_handle_node(xlate_handle_node_t
**ret
, apr_pool_t
*pool
)
456 return get_xlate_handle_node(ret
, SVN_APR_UTF8_CHARSET
,
457 assume_native_charset_is_utf8
458 ? SVN_APR_UTF8_CHARSET
459 : SVN_APR_LOCALE_CHARSET
,
460 SVN_UTF_NTOU_XLATE_HANDLE
, pool
);
464 /* Return the apr_xlate handle for converting UTF-8 to native characters.
465 Create one if it doesn't exist. If unable to find a handle, or
466 unable to create one because apr_xlate_open returned APR_EINVAL, then
467 set *RET to null and return SVN_NO_ERROR; if fail for some other
468 reason, return error. */
470 get_uton_xlate_handle_node(xlate_handle_node_t
**ret
, apr_pool_t
*pool
)
472 return get_xlate_handle_node(ret
,
473 assume_native_charset_is_utf8
474 ? SVN_APR_UTF8_CHARSET
475 : SVN_APR_LOCALE_CHARSET
,
476 SVN_APR_UTF8_CHARSET
,
477 SVN_UTF_UTON_XLATE_HANDLE
, pool
);
481 /* Convert SRC_LENGTH bytes of SRC_DATA in NODE->handle, store the result
482 in *DEST, which is allocated in POOL. */
484 convert_to_stringbuf(xlate_handle_node_t
*node
,
485 const char *src_data
,
486 apr_size_t src_length
,
487 svn_stringbuf_t
**dest
,
491 apr_status_t apr_err
;
493 apr_err
= svn_subr__win32_xlate_to_stringbuf(node
->handle
, src_data
,
494 src_length
, dest
, pool
);
496 apr_size_t buflen
= src_length
* 2;
497 apr_status_t apr_err
;
498 apr_size_t srclen
= src_length
;
499 apr_size_t destlen
= buflen
;
501 /* Initialize *DEST to an empty stringbuf.
502 A 1:2 ratio of input bytes to output bytes (as assigned above)
503 should be enough for most translations, and if it turns out not
504 to be enough, we'll grow the buffer again, sizing it based on a
505 1:3 ratio of the remainder of the string. */
506 *dest
= svn_stringbuf_create_ensure(buflen
+ 1, pool
);
508 /* Not only does it not make sense to convert an empty string, but
509 apr-iconv is quite unreasonable about not allowing that. */
515 /* Set up state variables for xlate. */
516 destlen
= buflen
- (*dest
)->len
;
518 /* Attempt the conversion. */
519 apr_err
= apr_xlate_conv_buffer(node
->handle
,
520 src_data
+ (src_length
- srclen
),
522 (*dest
)->data
+ (*dest
)->len
,
525 /* Now, update the *DEST->len to track the amount of output data
526 churned out so far from this loop. */
527 (*dest
)->len
+= ((buflen
- (*dest
)->len
) - destlen
);
528 buflen
+= srclen
* 3; /* 3 is middle ground, 2 wasn't enough
529 for all characters in the buffer, 4 is
530 maximum character size (currently) */
533 } while (apr_err
== APR_SUCCESS
&& srclen
!= 0);
536 /* If we exited the loop with an error, return the error. */
542 /* Can't use svn_error_wrap_apr here because it calls functions in
543 this file, leading to infinite recursion. */
544 if (node
->frompage
== SVN_APR_LOCALE_CHARSET
)
545 errstr
= apr_psprintf
546 (pool
, _("Can't convert string from native encoding to '%s':"),
548 else if (node
->topage
== SVN_APR_LOCALE_CHARSET
)
549 errstr
= apr_psprintf
550 (pool
, _("Can't convert string from '%s' to native encoding:"),
553 errstr
= apr_psprintf
554 (pool
, _("Can't convert string from '%s' to '%s':"),
555 node
->frompage
, node
->topage
);
557 err
= svn_error_create(
558 apr_err
, NULL
, svn_utf__fuzzy_escape(src_data
, src_length
, pool
));
559 return svn_error_create(apr_err
, err
, errstr
);
561 /* Else, exited due to success. Trim the result buffer down to the
563 (*dest
)->data
[(*dest
)->len
] = '\0';
569 /* Return APR_EINVAL if the first LEN bytes of DATA contain anything
570 other than seven-bit, non-control (except for whitespace) ASCII
571 characters, finding the error pool from POOL. Otherwise, return
574 check_non_ascii(const char *data
, apr_size_t len
, apr_pool_t
*pool
)
576 const char *data_start
= data
;
578 for (; len
> 0; --len
, data
++)
580 if ((! svn_ctype_isascii(*data
))
581 || ((! svn_ctype_isspace(*data
))
582 && svn_ctype_iscntrl(*data
)))
584 /* Show the printable part of the data, followed by the
585 decimal code of the questionable character. Because if a
586 user ever gets this error, she's going to have to spend
587 time tracking down the non-ASCII data, so we want to help
588 as much as possible. And yes, we just call the unsafe
589 data "non-ASCII", even though the actual constraint is
590 somewhat more complex than that. */
592 if (data
- data_start
)
594 const char *error_data
595 = apr_pstrndup(pool
, data_start
, (data
- data_start
));
597 return svn_error_createf
599 _("Safe data '%s' was followed by non-ASCII byte %d: "
600 "unable to convert to/from UTF-8"),
601 error_data
, *((const unsigned char *) data
));
605 return svn_error_createf
607 _("Non-ASCII character (code %d) detected, "
608 "and unable to convert to/from UTF-8"),
609 *((const unsigned char *) data
));
617 /* Construct an error with code APR_EINVAL and with a suitable message
618 * to describe the invalid UTF-8 sequence DATA of length LEN (which
619 * may have embedded NULLs). We can't simply print the data, almost
620 * by definition we don't really know how it is encoded.
623 invalid_utf8(const char *data
, apr_size_t len
, apr_pool_t
*pool
)
625 const char *last
= svn_utf__last_valid(data
, len
);
626 const char *valid_txt
= "", *invalid_txt
= "";
628 size_t valid
, invalid
;
630 /* We will display at most 24 valid octets (this may split a leading
631 multi-byte character) as that should fit on one 80 character line. */
635 for (i
= 0; i
< valid
; ++i
)
636 valid_txt
= apr_pstrcat(pool
, valid_txt
,
637 apr_psprintf(pool
, " %02x",
638 (unsigned char)last
[i
-valid
]),
641 /* 4 invalid octets will guarantee that the faulty octet is displayed */
642 invalid
= data
+ len
- last
;
645 for (i
= 0; i
< invalid
; ++i
)
646 invalid_txt
= apr_pstrcat(pool
, invalid_txt
,
647 apr_psprintf(pool
, " %02x",
648 (unsigned char)last
[i
]),
651 return svn_error_createf(APR_EINVAL
, NULL
,
652 _("Valid UTF-8 data\n(hex:%s)\n"
653 "followed by invalid UTF-8 sequence\n(hex:%s)"),
654 valid_txt
, invalid_txt
);
657 /* Verify that the sequence DATA of length LEN is valid UTF-8.
658 If it is not, return an error with code APR_EINVAL. */
660 check_utf8(const char *data
, apr_size_t len
, apr_pool_t
*pool
)
662 if (! svn_utf__is_valid(data
, len
))
663 return invalid_utf8(data
, len
, pool
);
667 /* Verify that the NULL terminated sequence DATA is valid UTF-8.
668 If it is not, return an error with code APR_EINVAL. */
670 check_cstring_utf8(const char *data
, apr_pool_t
*pool
)
673 if (! svn_utf__cstring_is_valid(data
))
674 return invalid_utf8(data
, strlen(data
), pool
);
680 svn_utf_stringbuf_to_utf8(svn_stringbuf_t
**dest
,
681 const svn_stringbuf_t
*src
,
684 xlate_handle_node_t
*node
;
687 SVN_ERR(get_ntou_xlate_handle_node(&node
, pool
));
691 err
= convert_to_stringbuf(node
, src
->data
, src
->len
, dest
, pool
);
693 err
= check_utf8((*dest
)->data
, (*dest
)->len
, pool
);
697 err
= check_non_ascii(src
->data
, src
->len
, pool
);
699 *dest
= svn_stringbuf_dup(src
, pool
);
702 return svn_error_compose_create(err
,
703 put_xlate_handle_node
705 SVN_UTF_NTOU_XLATE_HANDLE
,
711 svn_utf_string_to_utf8(const svn_string_t
**dest
,
712 const svn_string_t
*src
,
715 svn_stringbuf_t
*destbuf
;
716 xlate_handle_node_t
*node
;
719 SVN_ERR(get_ntou_xlate_handle_node(&node
, pool
));
723 err
= convert_to_stringbuf(node
, src
->data
, src
->len
, &destbuf
, pool
);
725 err
= check_utf8(destbuf
->data
, destbuf
->len
, pool
);
727 *dest
= svn_stringbuf__morph_into_string(destbuf
);
731 err
= check_non_ascii(src
->data
, src
->len
, pool
);
733 *dest
= svn_string_dup(src
, pool
);
736 return svn_error_compose_create(err
,
737 put_xlate_handle_node
739 SVN_UTF_NTOU_XLATE_HANDLE
,
744 /* Common implementation for svn_utf_cstring_to_utf8,
745 svn_utf_cstring_to_utf8_ex, svn_utf_cstring_from_utf8 and
746 svn_utf_cstring_from_utf8_ex. Convert SRC to DEST using NODE->handle as
747 the translator and allocating from POOL. */
749 convert_cstring(const char **dest
,
751 xlate_handle_node_t
*node
,
756 svn_stringbuf_t
*destbuf
;
757 SVN_ERR(convert_to_stringbuf(node
, src
, strlen(src
),
759 *dest
= destbuf
->data
;
763 apr_size_t len
= strlen(src
);
764 SVN_ERR(check_non_ascii(src
, len
, pool
));
765 *dest
= apr_pstrmemdup(pool
, src
, len
);
772 svn_utf_cstring_to_utf8(const char **dest
,
776 xlate_handle_node_t
*node
;
779 SVN_ERR(get_ntou_xlate_handle_node(&node
, pool
));
780 err
= convert_cstring(dest
, src
, node
, pool
);
781 SVN_ERR(svn_error_compose_create(err
,
782 put_xlate_handle_node
784 SVN_UTF_NTOU_XLATE_HANDLE
,
786 return check_cstring_utf8(*dest
, pool
);
791 svn_utf_cstring_to_utf8_ex2(const char **dest
,
793 const char *frompage
,
796 xlate_handle_node_t
*node
;
798 const char *convset_key
= get_xlate_key(SVN_APR_UTF8_CHARSET
, frompage
,
801 SVN_ERR(get_xlate_handle_node(&node
, SVN_APR_UTF8_CHARSET
, frompage
,
803 err
= convert_cstring(dest
, src
, node
, pool
);
804 SVN_ERR(svn_error_compose_create(err
,
805 put_xlate_handle_node
807 SVN_UTF_NTOU_XLATE_HANDLE
,
810 return check_cstring_utf8(*dest
, pool
);
815 svn_utf_cstring_to_utf8_ex(const char **dest
,
817 const char *frompage
,
818 const char *convset_key
,
821 return svn_utf_cstring_to_utf8_ex2(dest
, src
, frompage
, pool
);
826 svn_utf_stringbuf_from_utf8(svn_stringbuf_t
**dest
,
827 const svn_stringbuf_t
*src
,
830 xlate_handle_node_t
*node
;
833 SVN_ERR(get_uton_xlate_handle_node(&node
, pool
));
837 err
= check_utf8(src
->data
, src
->len
, pool
);
839 err
= convert_to_stringbuf(node
, src
->data
, src
->len
, dest
, pool
);
843 err
= check_non_ascii(src
->data
, src
->len
, pool
);
845 *dest
= svn_stringbuf_dup(src
, pool
);
848 err
= svn_error_compose_create(
850 put_xlate_handle_node(node
, SVN_UTF_UTON_XLATE_HANDLE
, pool
));
857 svn_utf_string_from_utf8(const svn_string_t
**dest
,
858 const svn_string_t
*src
,
861 xlate_handle_node_t
*node
;
864 SVN_ERR(get_uton_xlate_handle_node(&node
, pool
));
868 err
= check_utf8(src
->data
, src
->len
, pool
);
871 svn_stringbuf_t
*dbuf
;
873 err
= convert_to_stringbuf(node
, src
->data
, src
->len
,
877 *dest
= svn_stringbuf__morph_into_string(dbuf
);
882 err
= check_non_ascii(src
->data
, src
->len
, pool
);
884 *dest
= svn_string_dup(src
, pool
);
887 err
= svn_error_compose_create(
889 put_xlate_handle_node(node
, SVN_UTF_UTON_XLATE_HANDLE
, pool
));
896 svn_utf_cstring_from_utf8(const char **dest
,
900 xlate_handle_node_t
*node
;
903 SVN_ERR(check_cstring_utf8(src
, pool
));
905 SVN_ERR(get_uton_xlate_handle_node(&node
, pool
));
906 err
= convert_cstring(dest
, src
, node
, pool
);
907 err
= svn_error_compose_create(
909 put_xlate_handle_node(node
, SVN_UTF_UTON_XLATE_HANDLE
, pool
));
916 svn_utf_cstring_from_utf8_ex2(const char **dest
,
921 xlate_handle_node_t
*node
;
923 const char *convset_key
= get_xlate_key(topage
, SVN_APR_UTF8_CHARSET
,
926 SVN_ERR(check_cstring_utf8(src
, pool
));
928 SVN_ERR(get_xlate_handle_node(&node
, topage
, SVN_APR_UTF8_CHARSET
,
930 err
= convert_cstring(dest
, src
, node
, pool
);
931 err
= svn_error_compose_create(
933 put_xlate_handle_node(node
, convset_key
, pool
));
939 svn_utf__cstring_from_utf8_fuzzy(const char *src
,
941 svn_error_t
*(*convert_from_utf8
)
942 (const char **, const char *, apr_pool_t
*))
944 const char *escaped
, *converted
;
947 escaped
= svn_utf__fuzzy_escape(src
, strlen(src
), pool
);
949 /* Okay, now we have a *new* UTF-8 string, one that's guaranteed to
950 contain only 7-bit bytes :-). Recode to native... */
951 err
= convert_from_utf8(((const char **) &converted
), escaped
, pool
);
955 svn_error_clear(err
);
961 /* ### Check the client locale, maybe we can avoid that second
962 * conversion! See Ulrich Drepper's patch at
963 * https://issues.apache.org/jira/browse/SVN-807.
969 svn_utf_cstring_from_utf8_fuzzy(const char *src
,
972 return svn_utf__cstring_from_utf8_fuzzy(src
, pool
,
973 svn_utf_cstring_from_utf8
);
978 svn_utf_cstring_from_utf8_stringbuf(const char **dest
,
979 const svn_stringbuf_t
*src
,
982 svn_stringbuf_t
*destbuf
;
984 SVN_ERR(svn_utf_stringbuf_from_utf8(&destbuf
, src
, pool
));
985 *dest
= destbuf
->data
;
992 svn_utf_cstring_from_utf8_string(const char **dest
,
993 const svn_string_t
*src
,
996 xlate_handle_node_t
*node
;
999 SVN_ERR(get_uton_xlate_handle_node(&node
, pool
));
1003 err
= check_utf8(src
->data
, src
->len
, pool
);
1006 svn_stringbuf_t
*dbuf
;
1008 err
= convert_to_stringbuf(node
, src
->data
, src
->len
,
1016 err
= check_non_ascii(src
->data
, src
->len
, pool
);
1018 *dest
= apr_pstrmemdup(pool
, src
->data
, src
->len
);
1021 err
= svn_error_compose_create(
1023 put_xlate_handle_node(node
, SVN_UTF_UTON_XLATE_HANDLE
, pool
));
1029 /* Insert the given UCS-4 VALUE into BUF at the given OFFSET. */
1031 membuf_insert_ucs4(svn_membuf_t
*buf
, apr_size_t offset
, apr_int32_t value
)
1033 svn_membuf__resize(buf
, (offset
+ 1) * sizeof(value
));
1034 ((apr_int32_t
*)buf
->data
)[offset
] = value
;
1037 /* TODO: Use compiler intrinsics for byte swaps. */
1038 #define SWAP_SHORT(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))
1039 #define SWAP_LONG(x) ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) \
1040 | (((x) >> 8) & 0xff00) | (((x) >> 24) & 0xff))
1042 #define IS_UTF16_LEAD_SURROGATE(c) ((c) >= 0xd800 && (c) <= 0xdbff)
1043 #define IS_UTF16_TRAIL_SURROGATE(c) ((c) >= 0xdc00 && (c) <= 0xdfff)
1046 svn_utf__utf16_to_utf8(const svn_string_t
**result
,
1047 const apr_uint16_t
*utf16str
,
1048 apr_size_t utf16len
,
1049 svn_boolean_t big_endian
,
1050 apr_pool_t
*result_pool
,
1051 apr_pool_t
*scratch_pool
)
1053 static const apr_uint16_t endiancheck
= 0xa55a;
1054 const svn_boolean_t arch_big_endian
=
1055 (((const char*)&endiancheck
)[sizeof(endiancheck
) - 1] == '\x5a');
1056 const svn_boolean_t swap_order
= (!big_endian
!= !arch_big_endian
);
1058 apr_uint16_t lead_surrogate
;
1061 svn_membuf_t ucs4buf
;
1062 svn_membuf_t resultbuf
;
1065 if (utf16len
== SVN_UTF__UNKNOWN_LENGTH
)
1067 const apr_uint16_t
*endp
= utf16str
;
1070 utf16len
= (endp
- utf16str
);
1073 svn_membuf__create(&ucs4buf
, utf16len
* sizeof(apr_int32_t
), scratch_pool
);
1075 for (lead_surrogate
= 0, length
= 0, offset
= 0;
1076 offset
< utf16len
; ++offset
)
1078 const apr_uint16_t code
=
1079 (swap_order
? SWAP_SHORT(utf16str
[offset
]) : utf16str
[offset
]);
1083 if (IS_UTF16_TRAIL_SURROGATE(code
))
1085 /* Combine the lead and trail currogates into a 32-bit code. */
1086 membuf_insert_ucs4(&ucs4buf
, length
++,
1088 + (((lead_surrogate
& 0x03ff) << 10)
1089 | (code
& 0x03ff))));
1095 /* If we didn't find a surrogate pair, just dump the
1096 lead surrogate into the stream. */
1097 membuf_insert_ucs4(&ucs4buf
, length
++, lead_surrogate
);
1102 if ((offset
+ 1) < utf16len
&& IS_UTF16_LEAD_SURROGATE(code
))
1104 /* Store a lead surrogate that is followed by at least one
1105 code for the next iteration. */
1106 lead_surrogate
= code
;
1110 membuf_insert_ucs4(&ucs4buf
, length
++, code
);
1113 /* Convert the UCS-4 buffer to UTF-8, assuming an average of 2 bytes
1114 per code point for encoding. The buffer will grow as
1116 svn_membuf__create(&resultbuf
, length
* 2, result_pool
);
1117 SVN_ERR(svn_utf__encode_ucs4_string(
1118 &resultbuf
, ucs4buf
.data
, length
, &length
));
1120 res
= apr_palloc(result_pool
, sizeof(*res
));
1121 res
->data
= resultbuf
.data
;
1124 return SVN_NO_ERROR
;
1129 svn_utf__utf32_to_utf8(const svn_string_t
**result
,
1130 const apr_int32_t
*utf32str
,
1131 apr_size_t utf32len
,
1132 svn_boolean_t big_endian
,
1133 apr_pool_t
*result_pool
,
1134 apr_pool_t
*scratch_pool
)
1136 static const apr_int32_t endiancheck
= 0xa5cbbc5a;
1137 const svn_boolean_t arch_big_endian
=
1138 (((const char*)&endiancheck
)[sizeof(endiancheck
) - 1] == '\x5a');
1139 const svn_boolean_t swap_order
= (!big_endian
!= !arch_big_endian
);
1142 svn_membuf_t resultbuf
;
1145 if (utf32len
== SVN_UTF__UNKNOWN_LENGTH
)
1147 const apr_int32_t
*endp
= utf32str
;
1150 utf32len
= (endp
- utf32str
);
1156 svn_membuf_t ucs4buf
;
1158 svn_membuf__create(&ucs4buf
, utf32len
* sizeof(apr_int32_t
),
1161 for (offset
= 0; offset
< utf32len
; ++offset
)
1163 const apr_int32_t code
= SWAP_LONG(utf32str
[offset
]);
1164 membuf_insert_ucs4(&ucs4buf
, offset
, code
);
1166 utf32str
= ucs4buf
.data
;
1169 /* Convert the UCS-4 buffer to UTF-8, assuming an average of 2 bytes
1170 per code point for encoding. The buffer will grow as
1172 svn_membuf__create(&resultbuf
, utf32len
* 2, result_pool
);
1173 SVN_ERR(svn_utf__encode_ucs4_string(
1174 &resultbuf
, utf32str
, utf32len
, &length
));
1176 res
= apr_palloc(result_pool
, sizeof(*res
));
1177 res
->data
= resultbuf
.data
;
1180 return SVN_NO_ERROR
;
1188 svn_utf__win32_utf8_to_utf16(const WCHAR
**result
,
1190 const WCHAR
*prefix
,
1191 apr_pool_t
*result_pool
)
1193 const int utf8_count
= strlen(src
);
1194 const int prefix_len
= (prefix
? lstrlenW(prefix
) : 0);
1198 if (0 == prefix_len
+ utf8_count
)
1201 return SVN_NO_ERROR
;
1204 wide_count
= MultiByteToWideChar(CP_UTF8
, 0, src
, utf8_count
, NULL
, 0);
1205 if (wide_count
== 0)
1206 return svn_error_wrap_apr(apr_get_os_error(),
1207 _("Conversion to UTF-16 failed"));
1209 wide_str
= apr_palloc(result_pool
,
1210 (prefix_len
+ wide_count
+ 1) * sizeof(*wide_str
));
1212 memcpy(wide_str
, prefix
, prefix_len
* sizeof(*wide_str
));
1213 if (0 == MultiByteToWideChar(CP_UTF8
, 0, src
, utf8_count
,
1214 wide_str
+ prefix_len
, wide_count
))
1215 return svn_error_wrap_apr(apr_get_os_error(),
1216 _("Conversion to UTF-16 failed"));
1218 wide_str
[prefix_len
+ wide_count
] = 0;
1221 return SVN_NO_ERROR
;
1225 svn_utf__win32_utf16_to_utf8(const char **result
,
1228 apr_pool_t
*result_pool
)
1230 const int wide_count
= lstrlenW(src
);
1231 const int prefix_len
= (prefix
? strlen(prefix
) : 0);
1235 if (0 == prefix_len
+ wide_count
)
1238 return SVN_NO_ERROR
;
1241 utf8_count
= WideCharToMultiByte(CP_UTF8
, 0, src
, wide_count
,
1242 NULL
, 0, NULL
, FALSE
);
1243 if (utf8_count
== 0)
1244 return svn_error_wrap_apr(apr_get_os_error(),
1245 _("Conversion from UTF-16 failed"));
1247 utf8_str
= apr_palloc(result_pool
,
1248 (prefix_len
+ utf8_count
+ 1) * sizeof(*utf8_str
));
1250 memcpy(utf8_str
, prefix
, prefix_len
* sizeof(*utf8_str
));
1251 if (0 == WideCharToMultiByte(CP_UTF8
, 0, src
, wide_count
,
1252 utf8_str
+ prefix_len
, utf8_count
,
1254 return svn_error_wrap_apr(apr_get_os_error(),
1255 _("Conversion from UTF-16 failed"));
1257 utf8_str
[prefix_len
+ utf8_count
] = 0;
1260 return SVN_NO_ERROR
;