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, \
57 static ICONV_LIST(iconv_mbs_to_utf16le, "", "UTF-16LE");
58 static ICONV_LIST(iconv_utf16le_to_mbs, "UTF-16LE", "");
61 get_iconv(struct iconv_list_head *head)
66 pthread_mutex_lock(&head->mutex);
67 if (list_empty(&head->list)) {
68 cd = iconv_open(head->to_encoding, head->from_encoding);
69 if (cd == (iconv_t)-1) {
72 i = MALLOC(sizeof(struct iconv_node));
81 i = container_of(head->list.next, struct iconv_node, list);
82 list_del(head->list.next);
86 pthread_mutex_unlock(&head->mutex);
91 put_iconv(iconv_t *cd)
93 struct iconv_node *i = container_of(cd, struct iconv_node, cd);
94 struct iconv_list_head *head = i->head;
96 pthread_mutex_lock(&head->mutex);
97 list_add(&i->list, &head->list);
98 pthread_mutex_unlock(&head->mutex);
102 mbs_to_utf16le_nbytes(const mbchar *mbs, size_t mbs_nbytes,
103 size_t *utf16le_nbytes_ret)
105 iconv_t *cd = get_iconv(&iconv_mbs_to_utf16le);
106 if (*cd == (iconv_t)-1)
107 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
109 /* Worst case length */
110 utf16lechar buf[mbs_nbytes * 2];
111 char *inbuf = (char*)mbs;
112 char *outbuf = (char*)buf;
113 size_t outbytesleft = sizeof(buf);
114 size_t inbytesleft = mbs_nbytes;
118 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
119 if (len == (size_t)-1) {
120 ret = WIMLIB_ERR_INVALID_MULTIBYTE_STRING;
122 *utf16le_nbytes_ret = sizeof(buf) - outbytesleft;
131 utf16le_to_mbs_nbytes(const utf16lechar *utf16le_str, size_t utf16le_nbytes,
132 size_t *mbs_nbytes_ret)
134 iconv_t *cd = get_iconv(&iconv_utf16le_to_mbs);
135 if (*cd == (iconv_t)-1)
136 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
138 /* Worst case length */
139 mbchar buf[utf16le_nbytes / 2 * MB_CUR_MAX];
140 char *inbuf = (char*)utf16le_str;
141 char *outbuf = (char*)buf;
142 size_t outbytesleft = sizeof(buf);
143 size_t inbytesleft = utf16le_nbytes;
147 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
148 if (len == (size_t)-1) {
149 ERROR("Could not convert \"%W\" to encoding of current locale",
151 /* EILSEQ is supposed to mean that the *input* is invalid, but
152 * it's also returned if any input characters are not
153 * representable in the output encoding. (The actual behavior
154 * in this case is undefined for some reason...). Assume it's
155 * the latter error case. */
156 ret = WIMLIB_ERR_UNICODE_STRING_NOT_REPRESENTABLE;
158 *mbs_nbytes_ret = sizeof(buf) - outbytesleft;
166 mbs_to_utf16le_buf(const mbchar *mbs, size_t mbs_nbytes,
167 utf16lechar *utf16le_str)
169 iconv_t *cd = get_iconv(&iconv_mbs_to_utf16le);
170 if (*cd == (iconv_t)-1)
171 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
173 char *inbuf = (char*)mbs;
174 size_t inbytesleft = mbs_nbytes;
175 char *outbuf = (char*)utf16le_str;
176 size_t outbytesleft = SIZE_MAX;
180 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
181 if (len == (size_t)-1) {
182 ret = WIMLIB_ERR_INVALID_MULTIBYTE_STRING;
191 utf16le_to_mbs_buf(const utf16lechar *utf16le_str, size_t utf16le_nbytes,
195 iconv_t *cd = get_iconv(&iconv_utf16le_to_mbs);
196 if (*cd == (iconv_t)-1)
197 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
199 char *inbuf = (char*)utf16le_str;
201 char *outbuf = (char*)mbs;
202 size_t outbytesleft = SIZE_MAX;
205 len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
206 if (len == (size_t)-1) {
207 ret = WIMLIB_ERR_INVALID_UTF16_STRING;
211 mbs[SIZE_MAX - inbytesleft] = '\0';
217 mbs_to_utf16le(const mbchar *mbs, size_t mbs_nbytes,
218 utf16lechar **utf16le_ret, size_t *utf16le_nbytes_ret)
221 utf16lechar *utf16le_str;
222 size_t utf16le_nbytes;
224 ret = mbs_to_utf16le_nbytes(mbs, mbs_nbytes,
229 utf16le_str = MALLOC(utf16le_nbytes + 1);
231 return WIMLIB_ERR_NOMEM;
233 ret = mbs_to_utf16le_buf(mbs, mbs_nbytes, utf16le_str);
237 *utf16le_ret = utf16le_str;
238 *utf16le_nbytes_ret = utf16le_nbytes;
245 utf16le_to_mbs(const utf16lechar *utf16le_str, size_t utf16le_nbytes,
246 mbchar **mbs_ret, size_t *mbs_nbytes_ret)
252 ret = utf16le_to_mbs_nbytes(utf16le_str, utf16le_nbytes,
257 mbs = MALLOC(mbs_nbytes + 1);
259 return WIMLIB_ERR_NOMEM;
261 ret = utf16le_to_mbs_buf(utf16le_str, utf16le_nbytes, mbs);
266 *mbs_nbytes_ret = mbs_nbytes;
272 utf8_str_contains_nonascii_chars(const utf8char *utf8_str)
275 if ((unsigned char)*utf8_str > 127)
277 } while (*++utf8_str);