2 * encoding.c: Convert "multibyte" strings (the locale-default encoding---
3 * generally, UTF-8 or something like ISO-8859-1) to UTF-16LE strings, and vice
4 * versa. Also, convert UTF-8 strings to multibyte strings.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
26 #include "wimlib_internal.h"
33 bool wimlib_mbs_is_utf8 = true;
35 /* List of iconv_t conversion descriptors for a specific character conversion.
36 * The idea is that it is not thread-safe to have just one conversion
37 * descriptor, but it also is inefficient to open a new conversion descriptor to
38 * convert every string. Both these problems can be solved by maintaining a
39 * list of conversion descriptors; then, a thread can use an existing conversion
40 * descriptor if available. */
41 struct iconv_list_head {
42 const char *from_encoding;
43 const char *to_encoding;
44 struct list_head list;
45 pthread_mutex_t mutex;
50 struct list_head list;
51 struct iconv_list_head *head;
54 #define ICONV_LIST(name, from, to) \
55 struct iconv_list_head name = { \
56 .from_encoding = from, \
58 .list = LIST_HEAD_INIT(name.list), \
59 .mutex = PTHREAD_MUTEX_INITIALIZER, \
63 get_iconv(struct iconv_list_head *head)
69 pthread_mutex_lock(&head->mutex);
70 if (list_empty(&head->list)) {
71 cd = iconv_open(head->to_encoding, head->from_encoding);
72 if (cd == (iconv_t)-1) {
73 ERROR_WITH_ERRNO("Failed to open iconv from %s to %s",
74 head->from_encoding, head->to_encoding);
77 i = MALLOC(sizeof(struct iconv_node));
88 i = container_of(head->list.next, struct iconv_node, list);
89 list_del(head->list.next);
92 pthread_mutex_unlock(&head->mutex);
97 put_iconv(iconv_t *cd)
99 int errno_save = errno;
100 struct iconv_node *i = container_of(cd, struct iconv_node, cd);
101 struct iconv_list_head *head = i->head;
103 pthread_mutex_lock(&head->mutex);
104 list_add(&i->list, &head->list);
105 pthread_mutex_unlock(&head->mutex);
109 /* Prevent printing an error message if a character conversion error occurs
110 * while printing an error message. (This variable is not per-thread but it
111 * doesn't matter too much since it's just the error messages.) */
112 static bool error_message_being_printed = false;
114 #define DEFINE_CHAR_CONVERSION_FUNCTIONS(varname1, longname1, chartype1,\
115 varname2, longname2, chartype2,\
116 worst_case_len_expr, \
119 static ICONV_LIST(iconv_##varname1##_to_##varname2, \
120 longname1, longname2); \
123 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
124 size_t *out_nbytes_ret) \
126 iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2); \
128 return WIMLIB_ERR_ICONV_NOT_AVAILABLE; \
130 /* Worst case length */ \
131 chartype2 buf[worst_case_len_expr]; \
132 char *inbuf = (char*)in; \
133 size_t inbytesleft = in_nbytes; \
134 char *outbuf = (char*)buf; \
135 size_t outbytesleft = sizeof(buf); \
139 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
140 if (len == (size_t)-1) { \
141 if (!error_message_being_printed) { \
142 error_message_being_printed = true; \
144 error_message_being_printed = false; \
148 *out_nbytes_ret = sizeof(buf) - outbytesleft; \
156 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes, \
159 iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2); \
161 return WIMLIB_ERR_ICONV_NOT_AVAILABLE; \
163 char *inbuf = (char*)in; \
164 size_t inbytesleft = in_nbytes; \
165 char *outbuf = (char*)out; \
166 const size_t LARGE_NUMBER = 1000000000; \
167 size_t outbytesleft = LARGE_NUMBER; \
171 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
172 if (len == (size_t)-1) { \
173 if (!error_message_being_printed) { \
174 error_message_being_printed = true; \
176 error_message_being_printed = false; \
180 out[(LARGE_NUMBER-outbytesleft)/sizeof(chartype2)] = 0; \
188 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes, \
189 chartype2 **out_ret, \
190 size_t *out_nbytes_ret) \
196 ret = varname1##_to_##varname2##_nbytes(in, in_nbytes, \
201 out = MALLOC(out_nbytes + sizeof(chartype2)); \
203 return WIMLIB_ERR_NOMEM; \
205 ret = varname1##_to_##varname2##_buf(in, in_nbytes, out); \
207 int errno_save = errno; \
209 errno = errno_save; \
212 *out_nbytes_ret = out_nbytes; \
217 DEFINE_CHAR_CONVERSION_FUNCTIONS(utf16le, "UTF-16LE", utf16lechar,
219 in_nbytes / 2 * MB_CUR_MAX,
220 WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE,
221 ERROR("Failed to convert UTF-16LE string "
222 "to multibyte string!");
223 ERROR("This may be because the UTF-16LE data "
224 "could not be represented in your "
225 "locale's character encoding."))
227 DEFINE_CHAR_CONVERSION_FUNCTIONS(mbs, "", mbchar,
228 utf16le, "UTF-16LE", utf16lechar,
230 WIMLIB_ERR_INVALID_MULTIBYTE_STRING,
231 ERROR_WITH_ERRNO("Failed to convert multibyte "
232 "string \"%s\" to UTF-16LE string!", in);
233 ERROR("If the data you provided was UTF-8, please make sure "
234 "the character encoding of your current locale is UTF-8."))
236 DEFINE_CHAR_CONVERSION_FUNCTIONS(utf8, "UTF-8", utf8char,
239 WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE,
240 ERROR("Failed to convert UTF-8 string to multibyte string!");
241 ERROR("This may be because the UTF-8 data could not be represented "
242 "in your locale's character encoding."))
245 iconv_cleanup(struct iconv_list_head *head)
247 pthread_mutex_destroy(&head->mutex);
248 while (!list_empty(&head->list)) {
249 struct iconv_node *i;
251 i = container_of(head->list.next, struct iconv_node, list);
259 iconv_global_cleanup()
261 iconv_cleanup(&iconv_utf16le_to_mbs);
262 iconv_cleanup(&iconv_mbs_to_utf16le);
263 iconv_cleanup(&iconv_utf8_to_mbs);
267 utf8_str_contains_nonascii_chars(const utf8char *utf8_str)
270 if ((unsigned char)*utf8_str > 127)
272 } while (*++utf8_str);