blob: bc97145622071d27263a9c11a407b7f2565a182a [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 Vlasenko3d5b6062010-01-31 05:55:55 +0100142#define ERROR_WCHAR (~(wchar_t)0)
143
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100144static const char *mbstowc_internal(wchar_t *res, const char *src)
145{
146 int bytes;
147 unsigned c = (unsigned char) *src++;
148
149 if (c <= 0x7f) {
150 *res = c;
151 return src;
152 }
153
154 /* 80-7FF -> 110yyyxx 10xxxxxx */
155 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
156 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
157 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
158 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
159 bytes = 0;
160 do {
161 c <<= 1;
162 bytes++;
163 } while ((c & 0x80) && bytes < 6);
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100164 if (bytes == 1) {
165 /* A bare "continuation" byte. Say, 80 */
166 *res = ERROR_WCHAR;
167 return src;
168 }
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100169 c = (uint8_t)(c) >> bytes;
170
171 while (--bytes) {
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100172 unsigned ch = (unsigned char) *src;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100173 if ((ch & 0xc0) != 0x80) {
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100174 /* Missing "continuation" byte. Example: e0 80 */
175 *res = ERROR_WCHAR;
176 return src;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100177 }
178 c = (c << 6) + (ch & 0x3f);
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100179 src++;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100180 }
181
182 /* TODO */
183 /* Need to check that c isn't produced by overlong encoding */
184 /* Example: 11000000 10000000 converts to NUL */
185 /* 11110000 10000000 10000100 10000000 converts to 0x100 */
186 /* correct encoding: 11000100 10000000 */
187 if (c <= 0x7f) { /* crude check */
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100188 *res = ERROR_WCHAR;
189 return src;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100190 }
191
192 *res = c;
193 return src;
194}
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200195size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
196{
197 size_t org_n = n;
198
Denys Vlasenko28055022010-01-04 20:49:58 +0100199 if (unicode_status != UNICODE_ON) {
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200200 while (n) {
201 unsigned char c = *src++;
Denys Vlasenkofda8f572009-07-11 22:26:48 +0200202
203 if (dest)
204 *dest++ = c;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200205 if (c == 0)
206 break;
207 n--;
208 }
209 return org_n - n;
210 }
211
212 while (n) {
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100213 wchar_t wc;
Denys Vlasenko307b24c2010-01-25 02:00:16 +0100214 src = mbstowc_internal(&wc, src);
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100215 if (wc == ERROR_WCHAR) /* error */
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200216 return (size_t) -1L;
Denys Vlasenkofda8f572009-07-11 22:26:48 +0200217 if (dest)
Denys Vlasenko307b24c2010-01-25 02:00:16 +0100218 *dest++ = wc;
219 if (wc == 0) /* end-of-string */
220 break;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200221 n--;
222 }
223
224 return org_n - n;
225}
226
227int FAST_FUNC iswspace(wint_t wc)
228{
229 return (unsigned)wc <= 0x7f && isspace(wc);
230}
231
232int FAST_FUNC iswalnum(wint_t wc)
233{
234 return (unsigned)wc <= 0x7f && isalnum(wc);
235}
236
237int FAST_FUNC iswpunct(wint_t wc)
238{
239 return (unsigned)wc <= 0x7f && ispunct(wc);
240}
241
Denys Vlasenko2edba212010-01-29 09:11:47 +0100242#include "unicode_wcwidth.c"
243
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100244# if ENABLE_UNICODE_BIDI_SUPPORT
Tomas Heinrichaa167552010-03-26 13:13:24 +0100245int FAST_FUNC unicode_bidi_isrtl(wint_t wc)
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100246{
247 /* ranges taken from
248 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
249 * Bidi_Class=Left_To_Right | Bidi_Class=Arabic_Letter
250 */
251 static const struct interval rtl_b[] = {
252# define BIG_(a,b) { a, b },
253# define PAIR(a,b)
Tomas Heinrichaa167552010-03-26 13:13:24 +0100254# define ARRAY \
255 PAIR(0x0590, 0x0590) \
256 PAIR(0x05BE, 0x05BE) \
257 PAIR(0x05C0, 0x05C0) \
258 PAIR(0x05C3, 0x05C3) \
259 PAIR(0x05C6, 0x05C6) \
260 BIG_(0x05C8, 0x05FF) \
261 PAIR(0x0604, 0x0605) \
262 PAIR(0x0608, 0x0608) \
263 PAIR(0x060B, 0x060B) \
264 PAIR(0x060D, 0x060D) \
265 BIG_(0x061B, 0x064A) \
266 PAIR(0x065F, 0x065F) \
267 PAIR(0x066D, 0x066F) \
268 BIG_(0x0671, 0x06D5) \
269 PAIR(0x06E5, 0x06E6) \
270 PAIR(0x06EE, 0x06EF) \
271 BIG_(0x06FA, 0x070E) \
272 PAIR(0x0710, 0x0710) \
273 BIG_(0x0712, 0x072F) \
274 BIG_(0x074B, 0x07A5) \
275 BIG_(0x07B1, 0x07EA) \
276 PAIR(0x07F4, 0x07F5) \
277 BIG_(0x07FA, 0x0815) \
278 PAIR(0x081A, 0x081A) \
279 PAIR(0x0824, 0x0824) \
280 PAIR(0x0828, 0x0828) \
281 BIG_(0x082E, 0x08FF) \
282 PAIR(0x200F, 0x200F) \
283 PAIR(0x202B, 0x202B) \
284 PAIR(0x202E, 0x202E) \
285 BIG_(0xFB1D, 0xFB1D) \
286 BIG_(0xFB1F, 0xFB28) \
287 BIG_(0xFB2A, 0xFD3D) \
288 BIG_(0xFD40, 0xFDCF) \
289 BIG_(0xFDC8, 0xFDCF) \
290 BIG_(0xFDF0, 0xFDFC) \
291 BIG_(0xFDFE, 0xFDFF) \
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100292 BIG_(0xFE70, 0xFEFE)
293 /* Probably not necessary
294 {0x10800, 0x1091E},
295 {0x10920, 0x10A00},
296 {0x10A04, 0x10A04},
297 {0x10A07, 0x10A0B},
298 {0x10A10, 0x10A37},
299 {0x10A3B, 0x10A3E},
300 {0x10A40, 0x10A7F},
301 {0x10B36, 0x10B38},
302 {0x10B40, 0x10E5F},
303 {0x10E7F, 0x10FFF},
304 {0x1E800, 0x1EFFF}
305 */
Tomas Heinrichaa167552010-03-26 13:13:24 +0100306 ARRAY
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100307# undef BIG_
308# undef PAIR
309 };
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100310# define BIG_(a,b)
311# define PAIR(a,b) (a << 2) | (b-a),
Tomas Heinrichaa167552010-03-26 13:13:24 +0100312 static const uint16_t rtl_p[] = { ARRAY };
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100313# undef BIG_
314# undef PAIR
Tomas Heinrichaa167552010-03-26 13:13:24 +0100315# define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
316# define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
317 struct CHECK { ARRAY };
318# undef BIG_
319# undef PAIR
320# undef ARRAY
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100321
322 if (in_interval_table(wc, rtl_b, ARRAY_SIZE(rtl_b) - 1))
323 return 1;
324 if (in_uint16_table(wc, rtl_p, ARRAY_SIZE(rtl_p) - 1))
325 return 1;
326 return 0;
327}
Tomas Heinrichaa167552010-03-26 13:13:24 +0100328
329# if ENABLE_UNICODE_NEUTRAL_TABLE
330int FAST_FUNC unicode_bidi_is_neutral_wchar(wint_t wc)
331{
332 /* ranges taken from
333 * http://www.unicode.org/Public/5.2.0/ucd/extracted/DerivedBidiClass.txt
334 * Bidi_Classes: Paragraph_Separator, Segment_Separator,
335 * White_Space, Other_Neutral, European_Number, European_Separator,
336 * European_Terminator, Arabic_Number, Common_Separator
337 */
338 static const struct interval neutral_b[] = {
339# define BIG_(a,b) { a, b },
340# define PAIR(a,b)
341# define ARRAY \
342 BIG_(0x0009, 0x000D) \
343 BIG_(0x001C, 0x0040) \
344 BIG_(0x005B, 0x0060) \
345 PAIR(0x007B, 0x007E) \
346 PAIR(0x0085, 0x0085) \
347 BIG_(0x00A0, 0x00A9) \
348 PAIR(0x00AB, 0x00AC) \
349 BIG_(0x00AE, 0x00B4) \
350 PAIR(0x00B6, 0x00B9) \
351 BIG_(0x00BB, 0x00BF) \
352 PAIR(0x00D7, 0x00D7) \
353 PAIR(0x00F7, 0x00F7) \
354 PAIR(0x02B9, 0x02BA) \
355 BIG_(0x02C2, 0x02CF) \
356 BIG_(0x02D2, 0x02DF) \
357 BIG_(0x02E5, 0x02FF) \
358 PAIR(0x0374, 0x0375) \
359 PAIR(0x037E, 0x037E) \
360 PAIR(0x0384, 0x0385) \
361 PAIR(0x0387, 0x0387) \
362 PAIR(0x03F6, 0x03F6) \
363 PAIR(0x058A, 0x058A) \
364 PAIR(0x0600, 0x0603) \
365 PAIR(0x0606, 0x0607) \
366 PAIR(0x0609, 0x060A) \
367 PAIR(0x060C, 0x060C) \
368 PAIR(0x060E, 0x060F) \
369 BIG_(0x0660, 0x066C) \
370 PAIR(0x06DD, 0x06DD) \
371 PAIR(0x06E9, 0x06E9) \
372 BIG_(0x06F0, 0x06F9) \
373 PAIR(0x07F6, 0x07F9) \
374 PAIR(0x09F2, 0x09F3) \
375 PAIR(0x09FB, 0x09FB) \
376 PAIR(0x0AF1, 0x0AF1) \
377 BIG_(0x0BF3, 0x0BFA) \
378 BIG_(0x0C78, 0x0C7E) \
379 PAIR(0x0CF1, 0x0CF2) \
380 PAIR(0x0E3F, 0x0E3F) \
381 PAIR(0x0F3A, 0x0F3D) \
382 BIG_(0x1390, 0x1400) \
383 PAIR(0x1680, 0x1680) \
384 PAIR(0x169B, 0x169C) \
385 PAIR(0x17DB, 0x17DB) \
386 BIG_(0x17F0, 0x17F9) \
387 BIG_(0x1800, 0x180A) \
388 PAIR(0x180E, 0x180E) \
389 PAIR(0x1940, 0x1940) \
390 PAIR(0x1944, 0x1945) \
391 BIG_(0x19DE, 0x19FF) \
392 PAIR(0x1FBD, 0x1FBD) \
393 PAIR(0x1FBF, 0x1FC1) \
394 PAIR(0x1FCD, 0x1FCF) \
395 PAIR(0x1FDD, 0x1FDF) \
396 PAIR(0x1FED, 0x1FEF) \
397 PAIR(0x1FFD, 0x1FFE) \
398 BIG_(0x2000, 0x200A) \
399 BIG_(0x2010, 0x2029) \
400 BIG_(0x202F, 0x205F) \
401 PAIR(0x2070, 0x2070) \
402 BIG_(0x2074, 0x207E) \
403 BIG_(0x2080, 0x208E) \
404 BIG_(0x20A0, 0x20B8) \
405 PAIR(0x2100, 0x2101) \
406 PAIR(0x2103, 0x2106) \
407 PAIR(0x2108, 0x2109) \
408 PAIR(0x2114, 0x2114) \
409 PAIR(0x2116, 0x2118) \
410 BIG_(0x211E, 0x2123) \
411 PAIR(0x2125, 0x2125) \
412 PAIR(0x2127, 0x2127) \
413 PAIR(0x2129, 0x2129) \
414 PAIR(0x212E, 0x212E) \
415 PAIR(0x213A, 0x213B) \
416 BIG_(0x2140, 0x2144) \
417 PAIR(0x214A, 0x214D) \
418 BIG_(0x2150, 0x215F) \
419 PAIR(0x2189, 0x2189) \
420 BIG_(0x2190, 0x2335) \
421 BIG_(0x237B, 0x2394) \
422 BIG_(0x2396, 0x23E8) \
423 BIG_(0x2400, 0x2426) \
424 BIG_(0x2440, 0x244A) \
425 BIG_(0x2460, 0x249B) \
426 BIG_(0x24EA, 0x26AB) \
427 BIG_(0x26AD, 0x26CD) \
428 BIG_(0x26CF, 0x26E1) \
429 PAIR(0x26E3, 0x26E3) \
430 BIG_(0x26E8, 0x26FF) \
431 PAIR(0x2701, 0x2704) \
432 PAIR(0x2706, 0x2709) \
433 BIG_(0x270C, 0x2727) \
434 BIG_(0x2729, 0x274B) \
435 PAIR(0x274D, 0x274D) \
436 PAIR(0x274F, 0x2752) \
437 BIG_(0x2756, 0x275E) \
438 BIG_(0x2761, 0x2794) \
439 BIG_(0x2798, 0x27AF) \
440 BIG_(0x27B1, 0x27BE) \
441 BIG_(0x27C0, 0x27CA) \
442 PAIR(0x27CC, 0x27CC) \
443 BIG_(0x27D0, 0x27FF) \
444 BIG_(0x2900, 0x2B4C) \
445 BIG_(0x2B50, 0x2B59) \
446 BIG_(0x2CE5, 0x2CEA) \
447 BIG_(0x2CF9, 0x2CFF) \
448 BIG_(0x2E00, 0x2E99) \
449 BIG_(0x2E9B, 0x2EF3) \
450 BIG_(0x2F00, 0x2FD5) \
451 BIG_(0x2FF0, 0x2FFB) \
452 BIG_(0x3000, 0x3004) \
453 BIG_(0x3008, 0x3020) \
454 PAIR(0x3030, 0x3030) \
455 PAIR(0x3036, 0x3037) \
456 PAIR(0x303D, 0x303D) \
457 PAIR(0x303E, 0x303F) \
458 PAIR(0x309B, 0x309C) \
459 PAIR(0x30A0, 0x30A0) \
460 PAIR(0x30FB, 0x30FB) \
461 BIG_(0x31C0, 0x31E3) \
462 PAIR(0x321D, 0x321E) \
463 BIG_(0x3250, 0x325F) \
464 PAIR(0x327C, 0x327E) \
465 BIG_(0x32B1, 0x32BF) \
466 PAIR(0x32CC, 0x32CF) \
467 PAIR(0x3377, 0x337A) \
468 PAIR(0x33DE, 0x33DF) \
469 PAIR(0x33FF, 0x33FF) \
470 BIG_(0x4DC0, 0x4DFF) \
471 BIG_(0xA490, 0xA4C6) \
472 BIG_(0xA60D, 0xA60F) \
473 BIG_(0xA673, 0xA673) \
474 BIG_(0xA67E, 0xA67F) \
475 BIG_(0xA700, 0xA721) \
476 BIG_(0xA788, 0xA788) \
477 BIG_(0xA828, 0xA82B) \
478 BIG_(0xA838, 0xA839) \
479 BIG_(0xA874, 0xA877) \
480 BIG_(0xFB29, 0xFB29) \
481 BIG_(0xFD3E, 0xFD3F) \
482 BIG_(0xFDFD, 0xFDFD) \
483 BIG_(0xFE10, 0xFE19) \
484 BIG_(0xFE30, 0xFE52) \
485 BIG_(0xFE54, 0xFE66) \
486 BIG_(0xFE68, 0xFE6B) \
487 BIG_(0xFF01, 0xFF20) \
488 BIG_(0xFF3B, 0xFF40) \
489 BIG_(0xFF5B, 0xFF65) \
490 BIG_(0xFFE0, 0xFFE6) \
491 BIG_(0xFFE8, 0xFFEE) \
492 BIG_(0xFFF9, 0xFFFD)
493 /*
494 {0x10101, 0x10101},
495 {0x10140, 0x1019B},
496 {0x1091F, 0x1091F},
497 {0x10B39, 0x10B3F},
498 {0x10E60, 0x10E7E},
499 {0x1D200, 0x1D241},
500 {0x1D245, 0x1D245},
501 {0x1D300, 0x1D356},
502 {0x1D6DB, 0x1D6DB},
503 {0x1D715, 0x1D715},
504 {0x1D74F, 0x1D74F},
505 {0x1D789, 0x1D789},
506 {0x1D7C3, 0x1D7C3},
507 {0x1D7CE, 0x1D7FF},
508 {0x1F000, 0x1F02B},
509 {0x1F030, 0x1F093},
510 {0x1F100, 0x1F10A}
511 */
512 ARRAY
513# undef BIG_
514# undef PAIR
515 };
516# define BIG_(a,b)
517# define PAIR(a,b) (a << 2) | (b-a),
518 static const uint16_t neutral_p[] = { ARRAY };
519# undef BIG_
520# undef PAIR
521# define BIG_(a,b) char big_##a[b < 0x4000 && b-a <= 3 ? -1 : 1];
522# define PAIR(a,b) char pair##a[b >= 0x4000 || b-a > 3 ? -1 : 1];
523 struct CHECK { ARRAY };
524# undef BIG_
525# undef PAIR
526# undef ARRAY
527
528 if (in_interval_table(wc, neutral_b, ARRAY_SIZE(neutral_b) - 1))
529 return 1;
530 if (in_uint16_table(wc, neutral_p, ARRAY_SIZE(neutral_p) - 1))
531 return 1;
532 return 0;
533}
534# endif
535
Tomas Heinrichc5c006c2010-03-18 18:35:37 +0100536# endif /* UNICODE_BIDI_SUPPORT */
537
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100538#endif /* Homegrown Unicode support */
539
540
541/* The rest is mostly same for libc and for "homegrown" support */
542
543size_t FAST_FUNC unicode_strlen(const char *string)
544{
545 size_t width = mbstowcs(NULL, string, INT_MAX);
546 if (width == (size_t)-1L)
547 return strlen(string);
548 return width;
549}
550
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100551static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags)
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100552{
553 char *dst;
554 unsigned dst_len;
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100555 unsigned uni_count;
556 unsigned uni_width;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100557
Denys Vlasenko2edba212010-01-29 09:11:47 +0100558 if (unicode_status != UNICODE_ON) {
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100559 char *d;
560 if (flags & UNI_FLAG_PAD) {
561 d = dst = xmalloc(width + 1);
562 while ((int)--width >= 0) {
563 unsigned char c = *src;
564 if (c == '\0') {
565 do
566 *d++ = ' ';
567 while ((int)--width >= 0);
568 break;
569 }
570 *d++ = (c >= ' ' && c < 0x7f) ? c : '?';
571 src++;
Denys Vlasenko2edba212010-01-29 09:11:47 +0100572 }
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100573 *d = '\0';
574 } else {
575 d = dst = xstrndup(src, width);
576 while (*d) {
577 unsigned char c = *d;
578 if (c < ' ' || c >= 0x7f)
579 *d = '?';
580 d++;
581 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100582 }
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100583 if (stats)
584 stats->byte_count = stats->unicode_count = (d - dst);
Denys Vlasenko2edba212010-01-29 09:11:47 +0100585 return dst;
586 }
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100587
588 dst = NULL;
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100589 uni_count = uni_width = 0;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100590 dst_len = 0;
591 while (1) {
592 int w;
593 wchar_t wc;
594
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100595#if ENABLE_LOCALE_SUPPORT
596 {
597 mbstate_t mbst = { 0 };
598 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
Denys Vlasenko2edba212010-01-29 09:11:47 +0100599 /* If invalid sequence is seen: -1 is returned,
600 * src points to the invalid sequence, errno = EILSEQ.
601 * Else number of wchars (excluding terminating L'\0')
602 * written to dest is returned.
603 * If len (here: 1) non-L'\0' wchars stored at dest,
604 * src points to the next char to be converted.
605 * If string is completely converted: src = NULL.
606 */
607 if (rc == 0) /* end-of-string */
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100608 break;
Denys Vlasenko2edba212010-01-29 09:11:47 +0100609 if (rc < 0) { /* error */
610 src++;
611 goto subst;
612 }
613 if (!iswprint(wc))
614 goto subst;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100615 }
616#else
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100617 src = mbstowc_internal(&wc, src);
618 /* src is advanced to next mb char
619 * wc == ERROR_WCHAR: invalid sequence is seen
620 * else: wc is set
621 */
622 if (wc == ERROR_WCHAR) /* error */
623 goto subst;
624 if (wc == 0) /* end-of-string */
625 break;
Denys Vlasenko2edba212010-01-29 09:11:47 +0100626#endif
627 if (CONFIG_LAST_SUPPORTED_WCHAR && wc > CONFIG_LAST_SUPPORTED_WCHAR)
628 goto subst;
629 w = wcwidth(wc);
630 if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0) /* non-printable wchar */
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100631 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
632 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
Denys Vlasenko2edba212010-01-29 09:11:47 +0100633 ) {
634 subst:
635 wc = CONFIG_SUBST_WCHAR;
636 w = 1;
637 }
638 width -= w;
639 /* Note: if width == 0, we still may add more chars,
640 * they may be zero-width or combining ones */
641 if ((int)width < 0) {
642 /* can't add this wc, string would become longer than width */
643 width += w;
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100644 break;
645 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100646
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100647 uni_count++;
648 uni_width += w;
Denys Vlasenko2edba212010-01-29 09:11:47 +0100649 dst = xrealloc(dst, dst_len + MB_CUR_MAX);
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100650#if ENABLE_LOCALE_SUPPORT
651 {
652 mbstate_t mbst = { 0 };
653 dst_len += wcrtomb(&dst[dst_len], wc, &mbst);
654 }
655#else
656 dst_len += wcrtomb_internal(&dst[dst_len], wc);
657#endif
658 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100659
660 /* Pad to remaining width */
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100661 if (flags & UNI_FLAG_PAD) {
662 dst = xrealloc(dst, dst_len + width + 1);
663 uni_count += width;
664 uni_width += width;
665 while ((int)--width >= 0) {
666 dst[dst_len++] = ' ';
667 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100668 }
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100669 dst[dst_len] = '\0';
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100670 if (stats) {
671 stats->byte_count = dst_len;
672 stats->unicode_count = uni_count;
673 stats->unicode_width = uni_width;
674 }
Denys Vlasenko2edba212010-01-29 09:11:47 +0100675
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100676 return dst;
677}
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100678char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src)
679{
680 return unicode_conv_to_printable2(stats, src, INT_MAX, 0);
681}
682char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth)
683{
684 return unicode_conv_to_printable2(stats, src, maxwidth, 0);
685}
686char* FAST_FUNC unicode_conv_to_printable_fixedwidth(uni_stat_t *stats, const char *src, unsigned width)
687{
688 return unicode_conv_to_printable2(stats, src, width, UNI_FLAG_PAD);
689}
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100690
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100691#ifdef UNUSED
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100692unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src)
693{
694 if (unicode_status != UNICODE_ON) {
695 return width - strnlen(src, width);
696 }
697
698 while (1) {
699 int w;
700 wchar_t wc;
701
702#if ENABLE_LOCALE_SUPPORT
703 {
704 mbstate_t mbst = { 0 };
705 ssize_t rc = mbsrtowcs(&wc, &src, 1, &mbst);
706 if (rc <= 0) /* error, or end-of-string */
707 return width;
708 }
709#else
710 src = mbstowc_internal(&wc, src);
Denys Vlasenko3d5b6062010-01-31 05:55:55 +0100711 if (wc == ERROR_WCHAR || wc == 0) /* error, or end-of-string */
Denys Vlasenko9f93d622010-01-24 07:44:03 +0100712 return width;
713#endif
714 w = wcwidth(wc);
715 if (w < 0) /* non-printable wchar */
716 return width;
717 width -= w;
718 if ((int)width <= 0) /* string is longer than width */
719 return 0;
720 }
721}
Denys Vlasenkoe17764c2010-01-30 23:16:21 +0100722#endif