blob: 878af84bc0d9b12e135b771d76246362aa0c8a6e [file] [log] [blame]
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Unicode support routines.
4 *
Denys Vlasenkof6106e62009-07-16 02:27:04 +02005 * Copyright (C) 2009 Denys Vlasenko
Denys Vlasenko42a8fd02009-07-11 21:36:13 +02006 *
7 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
8 */
9#include "libbb.h"
Denys Vlasenko28055022010-01-04 20:49:58 +010010#include "unicode.h"
11
Denys Vlasenko9f93d622010-01-24 07:44:03 +010012/* If it's not #defined as a constant in unicode.h... */
Denys Vlasenko94ca6942010-01-20 02:51:09 +010013#ifndef unicode_status
Denys Vlasenko28055022010-01-04 20:49:58 +010014uint8_t unicode_status;
Denys Vlasenko94ca6942010-01-20 02:51:09 +010015#endif
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020016
Denys Vlasenko9f93d622010-01-24 07:44:03 +010017/* This file is compiled only if FEATURE_ASSUME_UNICODE is on.
18 * We check other options and decide whether to use libc support
19 * via locale, or use our own logic:
20 */
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020021
Denys Vlasenko28055022010-01-04 20:49:58 +010022#if ENABLE_LOCALE_SUPPORT
23
Denys Vlasenko9f93d622010-01-24 07:44:03 +010024/* Unicode support using libc locale support. */
Denys Vlasenko28055022010-01-04 20:49:58 +010025
26void FAST_FUNC init_unicode(void)
27{
28 /* In unicode, this is a one character string */
29 static const char unicode_0x394[] = { 0xce, 0x94, 0 };
30
31 if (unicode_status != UNICODE_UNKNOWN)
32 return;
33
Denys Vlasenko9f93d622010-01-24 07:44:03 +010034 unicode_status = unicode_strlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF;
Denys Vlasenko28055022010-01-04 20:49:58 +010035}
36
37#else
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020038
Denys Vlasenko9f93d622010-01-24 07:44:03 +010039/* Homegrown Unicode support. It knows only C and Unicode locales. */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020040
Denys Vlasenko28055022010-01-04 20:49:58 +010041# if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
42void FAST_FUNC init_unicode(void)
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020043{
44 char *lang;
45
Denys Vlasenko28055022010-01-04 20:49:58 +010046 if (unicode_status != UNICODE_UNKNOWN)
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020047 return;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020048
Denys Vlasenko28055022010-01-04 20:49:58 +010049 unicode_status = UNICODE_OFF;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020050 lang = getenv("LANG");
Denys Vlasenkofff73642009-07-16 16:09:25 +020051 if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF")))
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020052 return;
Denys Vlasenko28055022010-01-04 20:49:58 +010053 unicode_status = UNICODE_ON;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020054}
55# endif
56
57static size_t wcrtomb_internal(char *s, wchar_t wc)
58{
Denys Vlasenko01ba1672009-07-16 03:06:22 +020059 int n, i;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020060 uint32_t v = wc;
61
62 if (v <= 0x7f) {
63 *s = v;
64 return 1;
65 }
66
Denys Vlasenkofda8f572009-07-11 22:26:48 +020067 /* RFC 3629 says that Unicode ends at 10FFFF,
68 * but we cover entire 32 bits */
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020069
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020070 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020071 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020072 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020073 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
Denys Vlasenko01ba1672009-07-16 03:06:22 +020074 /* 80-7FF -> 110yyyxx 10xxxxxx */
75
76 /* How many bytes do we need? */
77 n = 2;
78 /* (0x80000000+ would result in n = 7, limiting n to 6) */
79 while (v >= 0x800 && n < 6) {
80 v >>= 5;
Denys Vlasenkofda8f572009-07-11 22:26:48 +020081 n++;
82 }
Denys Vlasenko01ba1672009-07-16 03:06:22 +020083 /* Fill bytes n-1..1 */
84 i = n;
85 while (--i) {
86 s[i] = (wc & 0x3f) | 0x80;
87 wc >>= 6;
88 }
89 /* Fill byte 0 */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020090 s[0] = wc | (uint8_t)(0x3f00 >> n);
91 return n;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020092}
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020093size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
94{
Denys Vlasenko28055022010-01-04 20:49:58 +010095 if (unicode_status != UNICODE_ON) {
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020096 *s = wc;
97 return 1;
98 }
99
100 return wcrtomb_internal(s, wc);
101}
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200102size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
103{
104 size_t org_n = n;
105
Denys Vlasenko28055022010-01-04 20:49:58 +0100106 if (unicode_status != UNICODE_ON) {
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200107 while (n) {
108 wchar_t c = *src++;
109 *dest++ = c;
110 if (c == 0)
111 break;
112 n--;
113 }
114 return org_n - n;
115 }
116
117 while (n >= MB_CUR_MAX) {
118 wchar_t wc = *src++;
119 size_t len = wcrtomb_internal(dest, wc);
120
121 if (wc == L'\0')
122 return org_n - n;
123 dest += len;
124 n -= len;
125 }
126 while (n) {
127 char tbuf[MB_CUR_MAX];
128 wchar_t wc = *src++;
129 size_t len = wcrtomb_internal(tbuf, wc);
130
131 if (len > n)
132 len = n;
133 memcpy(dest, tbuf, len);
134 if (wc == L'\0')
135 return org_n - n;
136 dest += len;
137 n -= len;
138 }
139 return org_n - n;
140}
141
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100142static const char *mbstowc_internal(wchar_t *res, const char *src)
143{
144 int bytes;
145 unsigned c = (unsigned char) *src++;
146
147 if (c <= 0x7f) {
148 *res = c;
149 return src;
150 }
151
152 /* 80-7FF -> 110yyyxx 10xxxxxx */
153 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
154 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
155 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
156 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
157 bytes = 0;
158 do {
159 c <<= 1;
160 bytes++;
161 } while ((c & 0x80) && bytes < 6);
162 if (bytes == 1)
163 return NULL;
164 c = (uint8_t)(c) >> bytes;
165
166 while (--bytes) {
167 unsigned ch = (unsigned char) *src++;
168 if ((ch & 0xc0) != 0x80) {
169 return NULL;
170 }
171 c = (c << 6) + (ch & 0x3f);
172 }
173
174 /* TODO */
175 /* Need to check that c isn't produced by overlong encoding */
176 /* Example: 11000000 10000000 converts to NUL */
177 /* 11110000 10000000 10000100 10000000 converts to 0x100 */
178 /* correct encoding: 11000100 10000000 */
179 if (c <= 0x7f) { /* crude check */
180 return NULL;
181 //or maybe 0xfffd; /* replacement character */
182 }
183
184 *res = c;
185 return src;
186}
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200187size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
188{
189 size_t org_n = n;
190
Denys Vlasenko28055022010-01-04 20:49:58 +0100191 if (unicode_status != UNICODE_ON) {
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200192 while (n) {
193 unsigned char c = *src++;
Denys Vlasenkofda8f572009-07-11 22:26:48 +0200194
195 if (dest)
196 *dest++ = c;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200197 if (c == 0)
198 break;
199 n--;
200 }
201 return org_n - n;
202 }
203
204 while (n) {
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100205 wchar_t wc;
Denys Vlasenko307b24c2010-01-25 02:00:16 +0100206 src = mbstowc_internal(&wc, src);
207 if (src == NULL) /* error */
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200208 return (size_t) -1L;
Denys Vlasenkofda8f572009-07-11 22:26:48 +0200209 if (dest)
Denys Vlasenko307b24c2010-01-25 02:00:16 +0100210 *dest++ = wc;
211 if (wc == 0) /* end-of-string */
212 break;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200213 n--;
214 }
215
216 return org_n - n;
217}
218
219int FAST_FUNC iswspace(wint_t wc)
220{
221 return (unsigned)wc <= 0x7f && isspace(wc);
222}
223
224int FAST_FUNC iswalnum(wint_t wc)
225{
226 return (unsigned)wc <= 0x7f && isalnum(wc);
227}
228
229int FAST_FUNC iswpunct(wint_t wc)
230{
231 return (unsigned)wc <= 0x7f && ispunct(wc);
232}
233
Denys Vlasenko2edba212010-01-29 09:11:47 +0100234#include "unicode_wcwidth.c"
235
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100236#endif /* Homegrown Unicode support */
237
238
239/* The rest is mostly same for libc and for "homegrown" support */
240
241size_t FAST_FUNC unicode_strlen(const char *string)
242{
243 size_t width = mbstowcs(NULL, string, INT_MAX);
244 if (width == (size_t)-1L)
245 return strlen(string);
246 return width;
247}
248
249char* FAST_FUNC unicode_cut_nchars(unsigned width, const char *src)
250{
251 char *dst;
252 unsigned dst_len;
253
Denys Vlasenko2edba212010-01-29 09:11:47 +0100254 if (unicode_status != UNICODE_ON) {
255 char *d = dst = xmalloc(width + 1);
256 while ((int)--width >= 0) {
257 unsigned char c = *src;
258 if (c == '\0') {
259 do
260 *d++ = ' ';
261 while ((int)--width >= 0);
262 break;
263 }
264 *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
265 src++;
266 }
267 *d = '\0';
268 return dst;
269 }
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100270
271 dst = NULL;
272 dst_len = 0;
273 while (1) {
274 int w;
275 wchar_t wc;
276
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100277#if ENABLE_LOCALE_SUPPORT
278 {
279 mbstate_t mbst = { 0 };
280 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
Denys Vlasenko2edba212010-01-29 09:11:47 +0100281 /* If invalid sequence is seen: -1 is returned,
282 * src points to the invalid sequence, errno = EILSEQ.
283 * Else number of wchars (excluding terminating L'\0')
284 * written to dest is returned.
285 * If len (here: 1) non-L'\0' wchars stored at dest,
286 * src points to the next char to be converted.
287 * If string is completely converted: src = NULL.
288 */
289 if (rc == 0) /* end-of-string */
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100290 break;
Denys Vlasenko2edba212010-01-29 09:11:47 +0100291 if (rc < 0) { /* error */
292 src++;
293 goto subst;
294 }
295 if (!iswprint(wc))
296 goto subst;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100297 }
298#else
Denys Vlasenko2edba212010-01-29 09:11:47 +0100299 {
300 const char *src1 = mbstowc_internal(&wc, src);
301 /* src = NULL: invalid sequence is seen,
302 * else: wc is set, src is advanced to next mb char
303 */
304 if (src1) {/* no error */
305 if (wc == 0) /* end-of-string */
306 break;
307 src = src1;
308 } else { /* error */
309 src++;
310 goto subst;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100311 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100312 }
313#endif
314 if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
315 goto subst;
316 w = wcwidth(wc);
317 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
318 || (!ENABLE_UNICODE_COMBINING_WCHARS && wc <= 0)
319 || (!ENABLE_UNICODE_WIDE_WCHARS && wc > 1)
320 ) {
321 subst:
322 wc = CONFIG_SUBST_WCHAR;
323 w = 1;
324 }
325 width -= w;
326 /* Note: if width == 0, we still may add more chars,
327 * they may be zero-width or combining ones */
328 if ((int)width < 0) {
329 /* can't add this wc, string would become longer than width */
330 width += w;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100331 break;
332 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100333
334 dst = xrealloc(dst, dst_len + MB_CUR_MAX);
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100335#if ENABLE_LOCALE_SUPPORT
336 {
337 mbstate_t mbst = { 0 };
338 dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
339 }
340#else
341 dst_len += wcrtomb_internal(&dst[dst_len], wc);
342#endif
343 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100344
345 /* Pad to remaining width */
346 dst = xrealloc(dst, dst_len + width + 1);
347 while ((int)--width >= 0) {
348 dst[dst_len++] = ' ';
349 }
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100350 dst[dst_len] = '\0';
Denys Vlasenko2edba212010-01-29 09:11:47 +0100351
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100352 return dst;
353}
354
355unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
356{
357 if (unicode_status != UNICODE_ON) {
358 return width - strnlen(src, width);
359 }
360
361 while (1) {
362 int w;
363 wchar_t wc;
364
365#if ENABLE_LOCALE_SUPPORT
366 {
367 mbstate_t mbst = { 0 };
368 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
369 if (rc <= 0) /* error, or end-of-string */
370 return width;
371 }
372#else
373 src = mbstowc_internal(&wc, src);
374 if (!src || wc == 0) /* error, or end-of-string */
375 return width;
376#endif
377 w = wcwidth(wc);
378 if (w < 0) /* non-printable wchar */
379 return width;
380 width -= w;
381 if ((int)width <= 0) /* string is longer than width */
382 return 0;
383 }
384}