5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2004 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 #include "silcincludes.h"
23 /* Encodes the string `bin' of which encoding is `bin_encoding' to the
24 UTF-8 encoding into the buffer `utf8' which is of size of `utf8_size'.
25 Returns the length of the UTF-8 encoded string, or zero (0) on error.
26 By default `bin_encoding' is ASCII, and the caller needs to know the
27 encoding of the input string if it is anything else. */
29 SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
30 SilcStringEncoding bin_encoding,
31 unsigned char *utf8, SilcUInt32 utf8_size)
33 SilcUInt32 enclen = 0, i, charval = 0;
38 if (bin_encoding == SILC_STRING_UTF8 ||
39 (silc_utf8_valid(bin, bin_len) && bin_len <= utf8_size)) {
40 memcpy(utf8, bin, bin_len);
44 if (bin_encoding == SILC_STRING_LOCALE) {
45 #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
46 char *fromconv, *icp, *ocp;
50 setlocale(LC_CTYPE, "");
51 fromconv = nl_langinfo(CODESET);
52 if (fromconv && strlen(fromconv)) {
53 icd = iconv_open("UTF-8", fromconv);
58 if (icp && ocp && icd != (iconv_t)-1) {
59 if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
65 if (icd != (iconv_t)-1)
70 /* Fallback to 8-bit ASCII */
71 bin_encoding = SILC_STRING_ASCII;
74 for (i = 0; i < bin_len; i++) {
75 switch (bin_encoding) {
76 case SILC_STRING_ASCII:
77 case SILC_STRING_TELETEX:
80 case SILC_STRING_ASCII_ESC:
81 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
87 SILC_GET16_MSB(charval, bin + i);
90 case SILC_STRING_BMP_LSB:
93 SILC_GET16_LSB(charval, bin + i);
96 case SILC_STRING_UNIVERSAL:
99 SILC_GET32_MSB(charval, bin + i);
102 case SILC_STRING_UNIVERSAL_LSB:
103 if (i + 3 >= bin_len)
105 SILC_GET32_LSB(charval, bin + i);
108 case SILC_STRING_PRINTABLE:
109 case SILC_STRING_VISIBLE:
110 if (!isprint(bin[i]))
114 case SILC_STRING_NUMERICAL:
115 if (bin[i] != 0x20 && !isdigit(bin[i]))
119 case SILC_STRING_LDAP_DN:
120 /* Remove any escaping */
121 if (bin[i] == '\\') {
123 if (i + 1 >= bin_len)
126 /* If escaped character is any of the following no processing is
127 needed, otherwise it is a hex value and we need to read it. */
129 if (cv != ',' && cv != '+' && cv != '"' && cv != '\\' && cv != '<' &&
130 cv != '>' && cv != ';' && cv != ' ' && cv != '#') {
132 if (i + 1 >= bin_len)
134 if (sscanf(&bin[++i], "%02X", &hexval) != 1)
136 cv = (unsigned char)hexval;
149 if (charval < 0x80) {
151 if (enclen > utf8_size)
154 utf8[enclen] = (unsigned char)charval;
157 } else if (charval < 0x800) {
159 if (enclen + 2 > utf8_size)
162 utf8[enclen ] = (unsigned char )(((charval >> 6) & 0x1f) | 0xc0);
163 utf8[enclen + 1] = (unsigned char )((charval & 0x3f) | 0x80);
166 } else if (charval < 0x10000) {
168 if (enclen + 3 > utf8_size)
171 utf8[enclen ] = (unsigned char )(((charval >> 12) & 0xf) | 0xe0);
172 utf8[enclen + 1] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
173 utf8[enclen + 2] = (unsigned char )((charval & 0x3f) | 0x80);
176 } else if (charval < 0x200000) {
178 if (enclen + 4 > utf8_size)
181 utf8[enclen ] = (unsigned char )(((charval >> 18) & 0x7) | 0xf0);
182 utf8[enclen + 1] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
183 utf8[enclen + 2] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
184 utf8[enclen + 3] = (unsigned char )((charval & 0x3f) | 0x80);
187 } else if (charval < 0x4000000) {
189 if (enclen + 5 > utf8_size)
192 utf8[enclen ] = (unsigned char )(((charval >> 24) & 0x3) | 0xf8);
193 utf8[enclen + 1] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
194 utf8[enclen + 2] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
195 utf8[enclen + 3] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
196 utf8[enclen + 4] = (unsigned char )((charval & 0x3f) | 0x80);
201 if (enclen + 6 > utf8_size)
204 utf8[enclen ] = (unsigned char )(((charval >> 30) & 0x1) | 0xfc);
205 utf8[enclen + 1] = (unsigned char )(((charval >> 24) & 0x3f) | 0x80);
206 utf8[enclen + 2] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
207 utf8[enclen + 3] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
208 utf8[enclen + 4] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
209 utf8[enclen + 5] = (unsigned char )((charval & 0x3f) | 0x80);
218 /* Decodes UTF-8 encoded string `utf8' to string of which encoding is
219 to be `bin_encoding', into the `bin' buffer of size of `bin_size'.
220 Returns the length of the decoded buffer, or zero (0) on error.
221 By default `bin_encoding' is ASCII, and the caller needs to know to
222 which encoding the output string is to be encoded if ASCII is not
225 SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
226 SilcStringEncoding bin_encoding,
227 unsigned char *bin, SilcUInt32 bin_size)
229 SilcUInt32 enclen = 0, i, charval;
231 if (!utf8 || !utf8_len)
234 if (bin_encoding == SILC_STRING_UTF8) {
235 if (!silc_utf8_valid(utf8, utf8_len) ||
238 memcpy(bin, utf8, utf8_len);
242 if (bin_encoding == SILC_STRING_LOCALE) {
243 #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
244 char *toconv, *icp, *ocp;
246 size_t inlen, outlen;
248 setlocale(LC_CTYPE, "");
249 toconv = nl_langinfo(CODESET);
250 if (toconv && strlen(toconv)) {
251 icd = iconv_open(toconv, "UTF-8");
256 if (icp && ocp && icd != (iconv_t)-1) {
257 if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
263 if (icd != (iconv_t)-1)
268 /* Fallback to 8-bit ASCII */
269 bin_encoding = SILC_STRING_ASCII;
272 for (i = 0; i < utf8_len; i++) {
273 if ((utf8[i] & 0x80) == 0x00) {
274 charval = utf8[i] & 0x7f;
275 } else if ((utf8[i] & 0xe0) == 0xc0) {
276 if (i + 1 >= utf8_len)
279 if ((utf8[i + 1] & 0xc0) != 0x80)
282 charval = (utf8[i++] & 0x1f) << 6;
283 charval |= utf8[i] & 0x3f;
286 } else if ((utf8[i] & 0xf0) == 0xe0) {
287 if (i + 2 >= utf8_len)
290 if (((utf8[i + 1] & 0xc0) != 0x80) ||
291 ((utf8[i + 2] & 0xc0) != 0x80))
294 /* Surrogates not allowed (D800-DFFF) */
295 if (utf8[i] == 0xed &&
296 utf8[i + 1] >= 0xa0 && utf8[i + 1] <= 0xbf &&
297 utf8[i + 2] >= 0x80 && utf8[i + 2] <= 0xbf)
300 charval = (utf8[i++] & 0xf) << 12;
301 charval |= (utf8[i++] & 0x3f) << 6;
302 charval |= utf8[i] & 0x3f;
305 } else if ((utf8[i] & 0xf8) == 0xf0) {
306 if (i + 3 >= utf8_len)
309 if (((utf8[i + 1] & 0xc0) != 0x80) ||
310 ((utf8[i + 2] & 0xc0) != 0x80) ||
311 ((utf8[i + 3] & 0xc0) != 0x80))
314 charval = ((SilcUInt32)(utf8[i++] & 0x7)) << 18;
315 charval |= (utf8[i++] & 0x3f) << 12;
316 charval |= (utf8[i++] & 0x3f) << 6;
317 charval |= utf8[i] & 0x3f;
318 if (charval < 0x10000)
320 } else if ((utf8[i] & 0xfc) == 0xf8) {
321 if (i + 4 >= utf8_len)
324 if (((utf8[i + 1] & 0xc0) != 0x80) ||
325 ((utf8[i + 2] & 0xc0) != 0x80) ||
326 ((utf8[i + 3] & 0xc0) != 0x80) ||
327 ((utf8[i + 4] & 0xc0) != 0x80))
330 charval = ((SilcUInt32)(utf8[i++] & 0x3)) << 24;
331 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
332 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
333 charval |= (utf8[i++] & 0x3f) << 6;
334 charval |= utf8[i] & 0x3f;
335 if (charval < 0x200000)
337 } else if ((utf8[i] & 0xfe) == 0xfc) {
338 if (i + 5 >= utf8_len)
341 if (((utf8[i + 1] & 0xc0) != 0x80) ||
342 ((utf8[i + 2] & 0xc0) != 0x80) ||
343 ((utf8[i + 3] & 0xc0) != 0x80) ||
344 ((utf8[i + 4] & 0xc0) != 0x80) ||
345 ((utf8[i + 5] & 0xc0) != 0x80))
348 charval = ((SilcUInt32)(utf8[i++] & 0x1)) << 30;
349 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 24;
350 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
351 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
352 charval |= (utf8[i++] & 0x3f) << 6;
353 charval |= utf8[i] & 0x3f;
354 if (charval < 0x4000000)
360 switch (bin_encoding) {
361 case SILC_STRING_ASCII:
362 case SILC_STRING_PRINTABLE:
363 case SILC_STRING_VISIBLE:
364 case SILC_STRING_TELETEX:
365 case SILC_STRING_NUMERICAL:
367 if (enclen + 1 > bin_size)
370 bin[enclen] = (unsigned char)charval;
374 case SILC_STRING_ASCII_ESC:
375 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
378 case SILC_STRING_BMP:
380 if (enclen + 2 > bin_size)
382 SILC_PUT16_MSB(charval, bin + enclen);
386 case SILC_STRING_BMP_LSB:
388 if (enclen + 2 > bin_size)
390 SILC_PUT16_LSB(charval, bin + enclen);
394 case SILC_STRING_UNIVERSAL:
396 if (enclen + 4 > bin_size)
398 SILC_PUT32_MSB(charval, bin + enclen);
402 case SILC_STRING_UNIVERSAL_LSB:
404 if (enclen + 4 > bin_size)
406 SILC_PUT32_LSB(charval, bin + enclen);
410 case SILC_STRING_LDAP_DN:
412 /* XXX multibyte handling */
413 unsigned char cv = (unsigned char)charval;
415 /* If string starts with space or # escape it */
416 if (!enclen && (cv == '#' || cv == ' ')) {
418 if (enclen + 2 > bin_size)
421 bin[enclen + 1] = cv;
427 /* If string ends with space escape it */
428 if (i == utf8_len - 1 && cv == ' ') {
430 if (enclen + 2 > bin_size)
433 bin[enclen + 1] = cv;
439 /* If character is any of following then escape */
440 if (cv == ',' || cv == '+' || cv == '"' || cv == '\\' || cv == '<' ||
441 cv == '>' || cv == ';') {
443 if (enclen + 2 > bin_size)
446 bin[enclen + 1] = cv;
452 /* If character is not printable escape it with hex character */
453 if (!isprint((int)cv)) {
455 if (enclen + 2 > bin_size)
458 snprintf(bin + enclen + 1, 3, "%02X", cv);
465 if (enclen + 1 > bin_size)
481 /* Returns the length of UTF-8 encoded string if the `bin' of
482 encoding of `bin_encoding' is encoded with silc_utf8_encode. */
484 SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, SilcUInt32 bin_len,
485 SilcStringEncoding bin_encoding)
487 return silc_utf8_encode(bin, bin_len, bin_encoding, NULL, 0);
490 /* Returns the length of decoded string if the `bin' of encoding of
491 `bin_encoding' is decoded with silc_utf8_decode. */
493 SilcUInt32 silc_utf8_decoded_len(const unsigned char *bin, SilcUInt32 bin_len,
494 SilcStringEncoding bin_encoding)
496 return silc_utf8_decode(bin, bin_len, bin_encoding, NULL, 0);
499 /* Returns TRUE if the `utf8' string of length of `utf8_len' is valid
500 UTF-8 encoded string, FALSE if it is not UTF-8 encoded string. */
502 bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len)
504 return silc_utf8_decode(utf8, utf8_len, 0, NULL, 0) != 0;