blob: 544528acd088379460492717cc3af23796412bf2 [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 Vlasenkofda8f572009-07-11 22:26:48 +020010# include "unicode.h"
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020011
Denys Vlasenkofda8f572009-07-11 22:26:48 +020012size_t FAST_FUNC bb_mbstrlen(const char *string)
13{
14 size_t width = mbstowcs(NULL, string, INT_MAX);
15 if (width == (size_t)-1L)
16 return strlen(string);
17 return width;
18}
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020019
20#if !ENABLE_LOCALE_SUPPORT
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020021
Denys Vlasenkofda8f572009-07-11 22:26:48 +020022/* Crude "locale support" which knows only C and Unicode locales */
23
24/* unicode_is_enabled:
25 * 0: not known yet,
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020026 * 1: not unicode (IOW: assuming one char == one byte)
27 * 2: unicode
28 */
29# if !ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
30# define unicode_is_enabled 2
31# else
32static smallint unicode_is_enabled;
33void FAST_FUNC check_unicode_in_env(void)
34{
35 char *lang;
36
37 if (unicode_is_enabled)
38 return;
39 unicode_is_enabled = 1;
40
41 lang = getenv("LANG");
Denys Vlasenkofff73642009-07-16 16:09:25 +020042 if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF")))
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020043 return;
44
45 unicode_is_enabled = 2;
46}
47# endif
48
49static size_t wcrtomb_internal(char *s, wchar_t wc)
50{
Denys Vlasenko01ba1672009-07-16 03:06:22 +020051 int n, i;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020052 uint32_t v = wc;
53
54 if (v <= 0x7f) {
55 *s = v;
56 return 1;
57 }
58
Denys Vlasenkofda8f572009-07-11 22:26:48 +020059 /* RFC 3629 says that Unicode ends at 10FFFF,
60 * but we cover entire 32 bits */
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020061
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020062 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020063 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020064 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020065 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
Denys Vlasenko01ba1672009-07-16 03:06:22 +020066 /* 80-7FF -> 110yyyxx 10xxxxxx */
67
68 /* How many bytes do we need? */
69 n = 2;
70 /* (0x80000000+ would result in n = 7, limiting n to 6) */
71 while (v >= 0x800 && n < 6) {
72 v >>= 5;
Denys Vlasenkofda8f572009-07-11 22:26:48 +020073 n++;
74 }
Denys Vlasenko01ba1672009-07-16 03:06:22 +020075 /* Fill bytes n-1..1 */
76 i = n;
77 while (--i) {
78 s[i] = (wc & 0x3f) | 0x80;
79 wc >>= 6;
80 }
81 /* Fill byte 0 */
Denys Vlasenkofda8f572009-07-11 22:26:48 +020082 s[0] = wc | (uint8_t)(0x3f00 >> n);
83 return n;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +020084}
85
86size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM)
87{
88 if (unicode_is_enabled != 2) {
89 *s = wc;
90 return 1;
91 }
92
93 return wcrtomb_internal(s, wc);
94}
95
96size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n)
97{
98 size_t org_n = n;
99
100 if (unicode_is_enabled != 2) {
101 while (n) {
102 wchar_t c = *src++;
103 *dest++ = c;
104 if (c == 0)
105 break;
106 n--;
107 }
108 return org_n - n;
109 }
110
111 while (n >= MB_CUR_MAX) {
112 wchar_t wc = *src++;
113 size_t len = wcrtomb_internal(dest, wc);
114
115 if (wc == L'\0')
116 return org_n - n;
117 dest += len;
118 n -= len;
119 }
120 while (n) {
121 char tbuf[MB_CUR_MAX];
122 wchar_t wc = *src++;
123 size_t len = wcrtomb_internal(tbuf, wc);
124
125 if (len > n)
126 len = n;
127 memcpy(dest, tbuf, len);
128 if (wc == L'\0')
129 return org_n - n;
130 dest += len;
131 n -= len;
132 }
133 return org_n - n;
134}
135
136size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n)
137{
138 size_t org_n = n;
139
140 if (unicode_is_enabled != 2) {
141 while (n) {
142 unsigned char c = *src++;
Denys Vlasenkofda8f572009-07-11 22:26:48 +0200143
144 if (dest)
145 *dest++ = c;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200146 if (c == 0)
147 break;
148 n--;
149 }
150 return org_n - n;
151 }
152
153 while (n) {
154 int bytes;
155 unsigned c = (unsigned char) *src++;
156
157 if (c <= 0x7f) {
Denys Vlasenkofda8f572009-07-11 22:26:48 +0200158 if (dest)
159 *dest++ = c;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200160 if (c == '\0')
161 break;
162 n--;
163 continue;
164 }
165
166 /* 80-7FF -> 110yyyxx 10xxxxxx */
167 /* 800-FFFF -> 1110yyyy 10yyyyxx 10xxxxxx */
168 /* 10000-1FFFFF -> 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx */
169 /* 200000-3FFFFFF -> 111110tt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
170 /* 4000000-FFFFFFFF -> 111111tt 10tttttt 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx */
171 bytes = 0;
172 do {
173 c <<= 1;
174 bytes++;
175 } while ((c & 0x80) && bytes < 6);
176 if (bytes == 1)
177 return (size_t) -1L;
178 c = (uint8_t)(c) >> bytes;
179
180 while (--bytes) {
181 unsigned ch = (unsigned char) *src++;
182 if ((ch & 0xc0) != 0x80) {
183 return (size_t) -1L;
184 }
185 c = (c << 6) + (ch & 0x3f);
186 }
187
188 /* TODO */
189 /* Need to check that c isn't produced by overlong encoding */
190 /* Example: 11000000 10000000 converts to NUL */
191 /* 11110000 10000000 10000100 10000000 converts to 0x100 */
192 /* correct encoding: 11000100 10000000 */
193 if (c <= 0x7f) { /* crude check */
194 return (size_t) -1L;
195 //or maybe: c = 0xfffd; /* replacement character */
196 }
197
Denys Vlasenkofda8f572009-07-11 22:26:48 +0200198 if (dest)
199 *dest++ = c;
Denys Vlasenko42a8fd02009-07-11 21:36:13 +0200200 n--;
201 }
202
203 return org_n - n;
204}
205
206int FAST_FUNC iswspace(wint_t wc)
207{
208 return (unsigned)wc <= 0x7f && isspace(wc);
209}
210
211int FAST_FUNC iswalnum(wint_t wc)
212{
213 return (unsigned)wc <= 0x7f && isalnum(wc);
214}
215
216int FAST_FUNC iswpunct(wint_t wc)
217{
218 return (unsigned)wc <= 0x7f && ispunct(wc);
219}
220
221#endif