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
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/.
27 #include "wimlib_internal.h"
34 bool wimlib_mbs_is_utf8 = false;
36 struct iconv_list_head {
37 const char *from_encoding;
38 const char *to_encoding;
39 struct list_head list;
40 pthread_mutex_t mutex;
45 struct list_head list;
46 struct iconv_list_head *head;
49 #define ICONV_LIST(name, from, to) \
50 struct iconv_list_head name = { \
51 .from_encoding = from, \
53 .list = LIST_HEAD_INIT(name.list), \
54 .mutex = PTHREAD_MUTEX_INITIALIZER, \
58 get_iconv(struct iconv_list_head *head)
64 pthread_mutex_lock(&head->mutex);
65 if (list_empty(&head->list)) {
66 cd = iconv_open(head->to_encoding, head->from_encoding);
67 if (cd == (iconv_t)-1) {
68 ERROR_WITH_ERRNO("Failed to open iconv from %s to %s",
69 head->from_encoding, head->to_encoding);
72 i = MALLOC(sizeof(struct iconv_node));
83 i = container_of(head->list.next, struct iconv_node, list);
84 list_del(head->list.next);
87 pthread_mutex_unlock(&head->mutex);
92 put_iconv(iconv_t *cd)
94 int errno_save = errno;
95 struct iconv_node *i = container_of(cd, struct iconv_node, cd);
96 struct iconv_list_head *head = i->head;
98 pthread_mutex_lock(&head->mutex);
99 list_add(&i->list, &head->list);
100 pthread_mutex_unlock(&head->mutex);
104 /* Prevent printing an error message if a character conversion error occurs
105 * while printing an error message. (This variable is not per-thread but it
106 * doesn't matter too much since it's just the error messages.) */
107 static bool error_message_being_printed = false;
109 #define DEFINE_CHAR_CONVERSION_FUNCTIONS(varname1, longname1, chartype1,\
110 varname2, longname2, chartype2,\
111 worst_case_len_expr, \
114 static ICONV_LIST(iconv_##varname1##_to_##varname2, \
115 longname1, longname2); \
118 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
119 size_t *out_nbytes_ret) \
121 iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2); \
123 return WIMLIB_ERR_ICONV_NOT_AVAILABLE; \
125 /* Worst case length */ \
126 chartype2 buf[worst_case_len_expr]; \
127 char *inbuf = (char*)in; \
128 size_t inbytesleft = in_nbytes; \
129 char *outbuf = (char*)buf; \
130 size_t outbytesleft = sizeof(buf); \
134 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
135 if (len == (size_t)-1) { \
136 if (!error_message_being_printed) { \
137 error_message_being_printed = true; \
139 error_message_being_printed = false; \
143 *out_nbytes_ret = sizeof(buf) - outbytesleft; \
151 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes, \
154 iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2); \
156 return WIMLIB_ERR_ICONV_NOT_AVAILABLE; \
158 char *inbuf = (char*)in; \
159 size_t inbytesleft = in_nbytes; \
160 char *outbuf = (char*)out; \
161 const size_t LARGE_NUMBER = 1000000000; \
162 size_t outbytesleft = LARGE_NUMBER; \
166 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
167 if (len == (size_t)-1) { \
171 out[(LARGE_NUMBER-outbytesleft)/sizeof(chartype2)] = 0; \
179 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes, \
180 chartype2 **out_ret, \
181 size_t *out_nbytes_ret) \
187 ret = varname1##_to_##varname2##_nbytes(in, in_nbytes, \
192 out = MALLOC(out_nbytes + sizeof(chartype2)); \
194 return WIMLIB_ERR_NOMEM; \
196 ret = varname1##_to_##varname2##_buf(in, in_nbytes, out); \
198 int errno_save = errno; \
200 errno = errno_save; \
203 *out_nbytes_ret = out_nbytes; \
208 DEFINE_CHAR_CONVERSION_FUNCTIONS(utf16le, "UTF-16LE", utf16lechar,
210 in_nbytes / 2 * MB_CUR_MAX,
211 WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE,
212 ERROR_WITH_ERRNO("Failed to convert UTF-16LE "
213 "string %U to multibyte string", in))
215 DEFINE_CHAR_CONVERSION_FUNCTIONS(mbs, "", mbchar,
216 utf16le, "UTF-16LE", utf16lechar,
218 WIMLIB_ERR_INVALID_MULTIBYTE_STRING,
219 ERROR_WITH_ERRNO("Failed to convert multibyte "
220 "string %s to UTF-16LE string", in))
222 DEFINE_CHAR_CONVERSION_FUNCTIONS(utf8, "UTF-8", utf8char,
225 WIMLIB_ERR_INVALID_UTF8_STRING,
226 ERROR_WITH_ERRNO("Failed to convert UTF-8 "
227 "string %U to multibyte string", in))
231 iconv_cleanup(struct iconv_list_head *head)
233 pthread_mutex_destroy(&head->mutex);
234 while (!list_empty(&head->list)) {
235 struct iconv_node *i;
237 i = container_of(head->list.next, struct iconv_node, list);
245 iconv_global_cleanup()
247 iconv_cleanup(&iconv_utf16le_to_mbs);
248 iconv_cleanup(&iconv_mbs_to_utf16le);
249 iconv_cleanup(&iconv_utf8_to_mbs);
255 utf8_str_contains_nonascii_chars(const utf8char *utf8_str)
258 if ((unsigned char)*utf8_str > 127)
260 } while (*++utf8_str);