]> wimlib.net Git - wimlib/blob - src/encoding.c
980e4cfcac00f5b2d3855346eb01d14370d3a147
[wimlib] / src / encoding.c
1 /*
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.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #include "config.h"
27 #include "wimlib_internal.h"
28 #include <pthread.h>
29 #include "list.h"
30
31 #include <iconv.h>
32 #include <stdlib.h>
33
34 bool wimlib_mbs_is_utf8 = false;
35
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;
41 };
42
43 struct iconv_node {
44         iconv_t cd;
45         struct list_head list;
46         struct iconv_list_head *head;
47 };
48
49 #define ICONV_LIST(name, from, to)                      \
50 struct iconv_list_head name = {                         \
51         .from_encoding = from,                          \
52         .to_encoding = to,                              \
53         .list = LIST_HEAD_INIT(name.list),              \
54         .mutex = PTHREAD_MUTEX_INITIALIZER,             \
55 }
56
57 static iconv_t *
58 get_iconv(struct iconv_list_head *head)
59 {
60         iconv_t cd;
61         iconv_t *cd_p;
62         struct iconv_node *i;
63
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);
70                         cd_p = NULL;
71                 } else {
72                         i = MALLOC(sizeof(struct iconv_node));
73                         if (i) {
74                                 i->head = head;
75                                 i->cd = cd;
76                                 cd_p = &i->cd;
77                         } else {
78                                 iconv_close(cd);
79                                 cd_p = NULL;
80                         }
81                 }
82         } else {
83                 i = container_of(head->list.next, struct iconv_node, list);
84                 list_del(head->list.next);
85                 cd_p = &i->cd;
86         }
87         pthread_mutex_unlock(&head->mutex);
88         return cd_p;
89 }
90
91 static void
92 put_iconv(iconv_t *cd)
93 {
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;
97         
98         pthread_mutex_lock(&head->mutex);
99         list_add(&i->list, &head->list);
100         pthread_mutex_unlock(&head->mutex);
101         errno = errno_save;
102 }
103
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;
108
109 #define DEFINE_CHAR_CONVERSION_FUNCTIONS(varname1, longname1, chartype1,\
110                                          varname2, longname2, chartype2,\
111                                          worst_case_len_expr,           \
112                                          err_return,                    \
113                                          err_msg)                       \
114 static ICONV_LIST(iconv_##varname1##_to_##varname2,                     \
115                   longname1, longname2);                                \
116                                                                         \
117 int                                                                     \
118 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
119                                   size_t *out_nbytes_ret)               \
120 {                                                                       \
121         iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2);     \
122         if (cd == NULL)                                                 \
123                 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;                  \
124                                                                         \
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);                              \
131         size_t len;                                                     \
132         int ret;                                                        \
133                                                                         \
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;             \
138                         err_msg;                                        \
139                         error_message_being_printed = false;            \
140                 }                                                       \
141                 ret = err_return;                                       \
142         } else {                                                        \
143                 *out_nbytes_ret = sizeof(buf) - outbytesleft;           \
144                 ret = 0;                                                \
145         }                                                               \
146         put_iconv(cd);                                                  \
147         return ret;                                                     \
148 }                                                                       \
149                                                                         \
150 int                                                                     \
151 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes,   \
152                                chartype2 *out)                          \
153 {                                                                       \
154         iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2);     \
155         if (cd == NULL)                                                 \
156                 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;                  \
157                                                                         \
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;                             \
163         size_t len;                                                     \
164         int ret;                                                        \
165                                                                         \
166         len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
167         if (len == (size_t)-1) {                                        \
168                 err_msg;                                                \
169                 ret = err_return;                                       \
170         } else {                                                        \
171                 out[(LARGE_NUMBER-outbytesleft)/sizeof(chartype2)] = 0; \
172                 ret = 0;                                                \
173         }                                                               \
174         put_iconv(cd);                                                  \
175         return ret;                                                     \
176 }                                                                       \
177                                                                         \
178 int                                                                     \
179 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes,         \
180                          chartype2 **out_ret,                           \
181                          size_t *out_nbytes_ret)                        \
182 {                                                                       \
183         int ret;                                                        \
184         chartype2 *out;                                                 \
185         size_t out_nbytes;                                              \
186                                                                         \
187         ret = varname1##_to_##varname2##_nbytes(in, in_nbytes,          \
188                                                 &out_nbytes);           \
189         if (ret)                                                        \
190                 return ret;                                             \
191                                                                         \
192         out = MALLOC(out_nbytes + sizeof(chartype2));                   \
193         if (!out)                                                       \
194                 return WIMLIB_ERR_NOMEM;                                \
195                                                                         \
196         ret = varname1##_to_##varname2##_buf(in, in_nbytes, out);       \
197         if (ret) {                                                      \
198                 int errno_save = errno;                                 \
199                 FREE(out);                                              \
200                 errno = errno_save;                                     \
201         } else {                                                        \
202                 *out_ret = out;                                         \
203                 *out_nbytes_ret = out_nbytes;                           \
204         }                                                               \
205         return ret;                                                     \
206 }
207
208 DEFINE_CHAR_CONVERSION_FUNCTIONS(utf16le, "UTF-16LE", utf16lechar,
209                                  mbs, "", mbchar,
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))
214
215 DEFINE_CHAR_CONVERSION_FUNCTIONS(mbs, "", mbchar,
216                                  utf16le, "UTF-16LE", utf16lechar,
217                                  in_nbytes * 2,
218                                  WIMLIB_ERR_INVALID_MULTIBYTE_STRING,
219                                  ERROR_WITH_ERRNO("Failed to convert multibyte "
220                                                   "string %s to UTF-16LE string", in))
221
222 DEFINE_CHAR_CONVERSION_FUNCTIONS(utf8, "UTF-8", utf8char,
223                                  mbs, "", mbchar,
224                                  in_nbytes,
225                                  WIMLIB_ERR_INVALID_UTF8_STRING,
226                                  ERROR_WITH_ERRNO("Failed to convert UTF-8 "
227                                                   "string %U to multibyte string", in))
228
229
230 static void
231 iconv_cleanup(struct iconv_list_head *head)
232 {
233         pthread_mutex_destroy(&head->mutex);
234         while (!list_empty(&head->list)) {
235                 struct iconv_node *i;
236                 
237                 i = container_of(head->list.next, struct iconv_node, list);
238                 list_del(&i->list);
239                 iconv_close(i->cd);
240                 FREE(i);
241         }
242 }
243
244 void
245 iconv_global_cleanup()
246 {
247         iconv_cleanup(&iconv_utf16le_to_mbs);
248         iconv_cleanup(&iconv_mbs_to_utf16le);
249         iconv_cleanup(&iconv_utf8_to_mbs);
250 }
251
252
253
254 bool
255 utf8_str_contains_nonascii_chars(const utf8char *utf8_str)
256 {
257         do {
258                 if ((unsigned char)*utf8_str > 127)
259                         return false;
260         } while (*++utf8_str);
261         return true;
262 }