1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2011 Jeffrey Stedfast
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
37 #define UNROLL_DECODE_UTF8 0
38 #define UNROLL_ENCODE_UTF8 0
40 typedef int (* Decoder) (char *inbuf, size_t inleft, gunichar *outchar);
41 typedef int (* Encoder) (gunichar c, char *outbuf, size_t outleft);
52 static int decode_utf32be (char *inbuf, size_t inleft, gunichar *outchar);
53 static int encode_utf32be (gunichar c, char *outbuf, size_t outleft);
55 static int decode_utf32le (char *inbuf, size_t inleft, gunichar *outchar);
56 static int encode_utf32le (gunichar c, char *outbuf, size_t outleft);
58 static int decode_utf16be (char *inbuf, size_t inleft, gunichar *outchar);
59 static int encode_utf16be (gunichar c, char *outbuf, size_t outleft);
61 static int decode_utf16le (char *inbuf, size_t inleft, gunichar *outchar);
62 static int encode_utf16le (gunichar c, char *outbuf, size_t outleft);
64 static int decode_utf8 (char *inbuf, size_t inleft, gunichar *outchar);
65 static int encode_utf8 (gunichar c, char *outbuf, size_t outleft);
67 static int decode_latin1 (char *inbuf, size_t inleft, gunichar *outchar);
68 static int encode_latin1 (gunichar c, char *outbuf, size_t outleft);
70 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
71 #define decode_utf32 decode_utf32le
72 #define encode_utf32 encode_utf32le
73 #define decode_utf16 decode_utf16le
74 #define encode_utf16 encode_utf16le
76 #define decode_utf32 decode_utf32be
77 #define encode_utf32 encode_utf32be
78 #define decode_utf16 decode_utf16be
79 #define encode_utf16 encode_utf16be
87 { "ISO-8859-1", decode_latin1, encode_latin1 },
88 { "ISO8859-1", decode_latin1, encode_latin1 },
89 { "UTF-32BE", decode_utf32be, encode_utf32be },
90 { "UTF-32LE", decode_utf32le, encode_utf32le },
91 { "UTF-16BE", decode_utf16be, encode_utf16be },
92 { "UTF-16LE", decode_utf16le, encode_utf16le },
93 { "UTF-32", decode_utf32, encode_utf32 },
94 { "UTF-16", decode_utf16, encode_utf16 },
95 { "UTF-8", decode_utf8, encode_utf8 },
96 { "US-ASCII", decode_latin1, encode_latin1 },
97 { "Latin1", decode_latin1, encode_latin1 },
98 { "ASCII", decode_latin1, encode_latin1 },
99 { "UTF32", decode_utf32, encode_utf32 },
100 { "UTF16", decode_utf16, encode_utf16 },
101 { "UTF8", decode_utf8, encode_utf8 },
106 g_iconv_open (const char *to_charset, const char *from_charset)
109 iconv_t icd = (iconv_t) -1;
111 Decoder decoder = NULL;
112 Encoder encoder = NULL;
116 if (!to_charset || !from_charset || !to_charset[0] || !from_charset[0]) {
122 for (i = 0; i < G_N_ELEMENTS (charsets); i++) {
123 if (!g_ascii_strcasecmp (charsets[i].name, from_charset))
124 decoder = charsets[i].decoder;
126 if (!g_ascii_strcasecmp (charsets[i].name, to_charset))
127 encoder = charsets[i].encoder;
130 if (!encoder || !decoder) {
132 if ((icd = iconv_open (to_charset, from_charset)) == (iconv_t) -1)
141 cd = (GIConv) g_malloc (sizeof (struct _GIConv));
142 cd->decode = decoder;
143 cd->encode = encoder;
154 g_iconv_close (GIConv cd)
157 if (cd->cd != (iconv_t) -1)
158 iconv_close (cd->cd);
167 g_iconv (GIConv cd, gchar **inbytes, gsize *inbytesleft,
168 gchar **outbytes, gsize *outbytesleft)
170 size_t inleft, outleft;
171 char *inptr, *outptr;
176 if (cd->cd != (iconv_t) -1)
177 return iconv (cd->cd, inbytes, inbytesleft, outbytes, outbytesleft);
180 if (outbytes == NULL || outbytesleft == NULL) {
181 /* reset converter */
186 inleft = inbytesleft ? *inbytesleft : 0;
187 inptr = inbytes ? *inbytes : NULL;
188 outleft = *outbytesleft;
191 if ((c = cd->c) != (gunichar) -1)
195 if ((rc = cd->decode (inptr, inleft, &c)) < 0)
202 if ((rc = cd->encode (c, outptr, outleft)) < 0)
211 *inbytesleft = inleft;
216 *outbytesleft = outleft;
220 return rc < 0 ? -1 : 0;
224 * Unicode encoders and decoders
228 decode_utf32be (char *inbuf, size_t inleft, gunichar *outchar)
230 gunichar *inptr = (gunichar *) inbuf;
238 c = GUINT32_FROM_BE (*inptr);
240 if (c >= 0xd800 && c < 0xe000) {
243 } else if (c >= 0x110000) {
254 decode_utf32le (char *inbuf, size_t inleft, gunichar *outchar)
256 gunichar *inptr = (gunichar *) inbuf;
264 c = GUINT32_FROM_LE (*inptr);
266 if (c >= 0xd800 && c < 0xe000) {
269 } else if (c >= 0x110000) {
280 encode_utf32be (gunichar c, char *outbuf, size_t outleft)
282 gunichar *outptr = (gunichar *) outbuf;
289 *outptr = GUINT32_TO_BE (c);
295 encode_utf32le (gunichar c, char *outbuf, size_t outleft)
297 gunichar *outptr = (gunichar *) outbuf;
304 *outptr = GUINT32_TO_LE (c);
310 decode_utf16be (char *inbuf, size_t inleft, gunichar *outchar)
312 gunichar2 *inptr = (gunichar2 *) inbuf;
321 u = GUINT16_FROM_BE (*inptr);
324 /* 0x0000 -> 0xd7ff */
327 } else if (u < 0xdc00) {
328 /* 0xd800 -> 0xdbff */
334 c = GUINT16_FROM_BE (inptr[1]);
336 if (c < 0xdc00 || c > 0xdfff) {
341 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
345 } else if (u < 0xe000) {
346 /* 0xdc00 -> 0xdfff */
350 /* 0xe000 -> 0xffff */
357 decode_utf16le (char *inbuf, size_t inleft, gunichar *outchar)
359 gunichar2 *inptr = (gunichar2 *) inbuf;
368 u = GUINT16_FROM_LE (*inptr);
371 /* 0x0000 -> 0xd7ff */
374 } else if (u < 0xdc00) {
375 /* 0xd800 -> 0xdbff */
381 c = GUINT16_FROM_LE (inptr[1]);
383 if (c < 0xdc00 || c > 0xdfff) {
388 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
392 } else if (u < 0xe000) {
393 /* 0xdc00 -> 0xdfff */
397 /* 0xe000 -> 0xffff */
404 encode_utf16be (gunichar c, char *outbuf, size_t outleft)
406 gunichar2 *outptr = (gunichar2 *) outbuf;
418 *outptr = GUINT16_TO_BE (ch);
429 ch = (gunichar2) ((c2 >> 10) + 0xd800);
430 outptr[0] = GUINT16_TO_BE (ch);
432 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
433 outptr[1] = GUINT16_TO_BE (ch);
440 encode_utf16le (gunichar c, char *outbuf, size_t outleft)
442 gunichar2 *outptr = (gunichar2 *) outbuf;
454 *outptr = GUINT16_TO_LE (ch);
465 ch = (gunichar2) ((c2 >> 10) + 0xd800);
466 outptr[0] = GUINT16_TO_LE (ch);
468 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
469 outptr[1] = GUINT16_TO_LE (ch);
476 decode_utf8 (char *inbuf, size_t inleft, gunichar *outchar)
478 unsigned char *inptr = (unsigned char *) inbuf;
485 /* simple ascii case */
488 } else if (u < 0xc2) {
491 } else if (u < 0xe0) {
494 } else if (u < 0xf0) {
497 } else if (u < 0xf8) {
500 } else if (u < 0xfc) {
503 } else if (u < 0xfe) {
516 #if UNROLL_DECODE_UTF8
518 case 6: u = (u << 6) | (*++inptr ^ 0x80);
519 case 5: u = (u << 6) | (*++inptr ^ 0x80);
520 case 4: u = (u << 6) | (*++inptr ^ 0x80);
521 case 3: u = (u << 6) | (*++inptr ^ 0x80);
522 case 2: u = (u << 6) | (*++inptr ^ 0x80);
525 for (i = 1; i < n; i++)
526 u = (u << 6) | (*++inptr ^ 0x80);
535 encode_utf8 (gunichar c, char *outbuf, size_t outleft)
537 unsigned char *outptr = (unsigned char *) outbuf;
543 } else if (c < 0x800) {
546 } else if (c < 0x10000) {
549 } else if (c < 0x200000) {
552 } else if (c < 0x4000000) {
565 #if UNROLL_ENCODE_UTF8
567 case 6: outptr[5] = (c & 0x3f) | 0x80; c >>= 6;
568 case 5: outptr[4] = (c & 0x3f) | 0x80; c >>= 6;
569 case 4: outptr[3] = (c & 0x3f) | 0x80; c >>= 6;
570 case 3: outptr[2] = (c & 0x3f) | 0x80; c >>= 6;
571 case 2: outptr[1] = (c & 0x3f) | 0x80; c >>= 6;
572 case 1: outptr[0] = c | base;
575 for (i = n - 1; i > 0; i--) {
576 outptr[i] = (c & 0x3f) | 0x80;
580 outptr[0] = c | base;
587 decode_latin1 (char *inbuf, size_t inleft, gunichar *outchar)
589 *outchar = (unsigned char) *inbuf;
594 encode_latin1 (gunichar c, char *outbuf, size_t outleft)
613 * Simple conversion API
616 static gpointer error_quark = "ConvertError";
619 g_convert_error_quark (void)
625 g_convert (const gchar *str, gssize len, const gchar *to_charset, const gchar *from_charset,
626 gsize *bytes_read, gsize *bytes_written, GError **err)
628 size_t outsize, outused, outleft, inleft, grow, rc;
629 char *result, *outbuf, *inbuf;
630 gboolean flush = FALSE;
631 gboolean done = FALSE;
634 g_return_val_if_fail (str != NULL, NULL);
635 g_return_val_if_fail (to_charset != NULL, NULL);
636 g_return_val_if_fail (from_charset != NULL, NULL);
638 if ((cd = g_iconv_open (to_charset, from_charset)) == (GIConv) -1) {
639 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
640 "Conversion from %s to %s not supported.",
641 from_charset, to_charset);
652 inleft = len < 0 ? strlen (str) : len;
653 inbuf = (char *) str;
655 outleft = outsize = MAX (inleft, 8);
656 outbuf = result = g_malloc (outsize + 4);
660 rc = g_iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
662 rc = g_iconv (cd, NULL, NULL, &outbuf, &outleft);
664 if (rc == (size_t) -1) {
667 /* grow our result buffer */
668 grow = MAX (inleft, 8) << 1;
669 outused = outbuf - result;
672 result = g_realloc (result, outsize + 4);
673 outbuf = result + outused;
676 /* incomplete input, stop converting and terminate here */
683 /* illegal sequence in the input */
684 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "%s", g_strerror (errno));
687 /* save offset of the illegal input sequence */
688 *bytes_read = (inbuf - str);
699 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, "%s", g_strerror (errno));
712 /* input has been converted and output has been flushed */
715 /* input has been converted, need to flush the output */
722 /* Note: not all charsets can be null-terminated with a single
723 null byte. UCS2, for example, needs 2 null bytes and UCS4
724 needs 4. I hope that 4 null bytes is enough to terminate all
725 multibyte charsets? */
727 /* null-terminate the result */
728 memset (outbuf, 0, 4);
731 *bytes_written = outbuf - result;
734 *bytes_read = inbuf - str;
745 * from http://home.tiscali.nl/t876506/utf8tbl.html
747 * From Unicode UCS-4 to UTF-8:
748 * Start with the Unicode number expressed as a decimal number and call this ud.
750 * If ud <128 (7F hex) then UTF-8 is 1 byte long, the value of ud.
752 * If ud >=128 and <=2047 (7FF hex) then UTF-8 is 2 bytes long.
753 * byte 1 = 192 + (ud div 64)
754 * byte 2 = 128 + (ud mod 64)
756 * If ud >=2048 and <=65535 (FFFF hex) then UTF-8 is 3 bytes long.
757 * byte 1 = 224 + (ud div 4096)
758 * byte 2 = 128 + ((ud div 64) mod 64)
759 * byte 3 = 128 + (ud mod 64)
761 * If ud >=65536 and <=2097151 (1FFFFF hex) then UTF-8 is 4 bytes long.
762 * byte 1 = 240 + (ud div 262144)
763 * byte 2 = 128 + ((ud div 4096) mod 64)
764 * byte 3 = 128 + ((ud div 64) mod 64)
765 * byte 4 = 128 + (ud mod 64)
767 * If ud >=2097152 and <=67108863 (3FFFFFF hex) then UTF-8 is 5 bytes long.
768 * byte 1 = 248 + (ud div 16777216)
769 * byte 2 = 128 + ((ud div 262144) mod 64)
770 * byte 3 = 128 + ((ud div 4096) mod 64)
771 * byte 4 = 128 + ((ud div 64) mod 64)
772 * byte 5 = 128 + (ud mod 64)
774 * If ud >=67108864 and <=2147483647 (7FFFFFFF hex) then UTF-8 is 6 bytes long.
775 * byte 1 = 252 + (ud div 1073741824)
776 * byte 2 = 128 + ((ud div 16777216) mod 64)
777 * byte 3 = 128 + ((ud div 262144) mod 64)
778 * byte 4 = 128 + ((ud div 4096) mod 64)
779 * byte 5 = 128 + ((ud div 64) mod 64)
780 * byte 6 = 128 + (ud mod 64)
783 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
790 } else if (c < 0x800) {
793 } else if (c < 0x10000) {
796 } else if (c < 0x200000) {
799 } else if (c < 0x4000000) {
802 } else if (c < 0x80000000) {
809 if (outbuf != NULL) {
810 for (i = n - 1; i > 0; i--) {
811 /* mask off 6 bits worth and add 128 */
812 outbuf[i] = (c & 0x3f) | 0x80;
816 /* first character has a different base */
817 outbuf[0] = c | base;
824 g_unichar_to_utf16 (gunichar c, gunichar2 *outbuf)
830 *outbuf = (gunichar2) c;
833 } else if (c < 0xe000) {
835 } else if (c < 0x10000) {
837 *outbuf = (gunichar2) c;
840 } else if (c < 0x110000) {
844 outbuf[0] = (gunichar2) ((c2 >> 10) + 0xd800);
845 outbuf[1] = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
855 g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
857 gunichar *outbuf, *outptr;
861 g_return_val_if_fail (str != NULL, NULL);
863 n = g_utf8_strlen (str, len);
868 outptr = outbuf = g_malloc ((n + 1) * sizeof (gunichar));
869 inptr = (char *) str;
871 for (i = 0; i < n; i++) {
872 *outptr++ = g_utf8_get_char (inptr);
873 inptr = g_utf8_next_char (inptr);
882 g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
884 gunichar2 *outbuf, *outptr;
891 g_return_val_if_fail (str != NULL, NULL);
896 inptr = (char *) str;
900 if ((n = decode_utf8 (inptr, inleft, &c)) < 0) {
901 if (errno == EILSEQ) {
902 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
903 "Illegal byte sequence encounted in the input.");
904 } else if (items_read) {
905 /* partial input is ok if we can let our caller know... */
908 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
909 "Partial byte sequence encountered in the input.");
913 *items_read = inptr - str;
922 outlen += g_unichar_to_utf16 (c, NULL);
928 *items_read = inptr - str;
931 *items_written = outlen;
933 outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
934 inptr = (char *) str;
938 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
943 outptr += g_unichar_to_utf16 (c, outptr);
954 g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
956 gunichar *outbuf, *outptr;
963 g_return_val_if_fail (str != NULL, NULL);
968 inptr = (char *) str;
972 if ((n = decode_utf8 (inptr, inleft, &c)) < 0) {
973 if (errno == EILSEQ) {
974 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
975 "Illegal byte sequence encounted in the input.");
976 } else if (items_read) {
977 /* partial input is ok if we can let our caller know... */
980 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
981 "Partial byte sequence encountered in the input.");
985 *items_read = inptr - str;
1000 *items_written = outlen / 4;
1003 *items_read = inptr - str;
1005 outptr = outbuf = g_malloc (outlen + 4);
1006 inptr = (char *) str;
1009 while (inleft > 0) {
1010 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
1026 g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1028 char *inptr, *outbuf, *outptr;
1034 g_return_val_if_fail (str != NULL, NULL);
1042 inptr = (char *) str;
1045 while (inleft > 0) {
1046 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1047 if (n == -2 && inleft > 2) {
1048 /* This means that the first UTF-16 char was read, but second failed */
1053 if (errno == EILSEQ) {
1054 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1055 "Illegal byte sequence encounted in the input.");
1056 } else if (items_read) {
1057 /* partial input is ok if we can let our caller know... */
1060 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1061 "Partial byte sequence encountered in the input.");
1065 *items_read = (inptr - (char *) str) / 2;
1074 outlen += g_unichar_to_utf8 (c, NULL);
1080 *items_read = (inptr - (char *) str) / 2;
1083 *items_written = outlen;
1085 outptr = outbuf = g_malloc (outlen + 1);
1086 inptr = (char *) str;
1089 while (inleft > 0) {
1090 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1095 outptr += g_unichar_to_utf8 (c, outptr);
1106 g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1108 gunichar *outbuf, *outptr;
1115 g_return_val_if_fail (str != NULL, NULL);
1123 inptr = (char *) str;
1126 while (inleft > 0) {
1127 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1128 if (n == -2 && inleft > 2) {
1129 /* This means that the first UTF-16 char was read, but second failed */
1134 if (errno == EILSEQ) {
1135 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1136 "Illegal byte sequence encounted in the input.");
1137 } else if (items_read) {
1138 /* partial input is ok if we can let our caller know... */
1141 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1142 "Partial byte sequence encountered in the input.");
1146 *items_read = (inptr - (char *) str) / 2;
1161 *items_read = (inptr - (char *) str) / 2;
1164 *items_written = outlen / 4;
1166 outptr = outbuf = g_malloc (outlen + 4);
1167 inptr = (char *) str;
1170 while (inleft > 0) {
1171 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1187 g_ucs4_to_utf8 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1189 char *outbuf, *outptr;
1194 g_return_val_if_fail (str != NULL, NULL);
1197 for (i = 0; str[i] != 0; i++) {
1198 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1199 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1200 "Illegal byte sequence encounted in the input.");
1214 for (i = 0; i < len && str[i] != 0; i++) {
1215 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1216 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1217 "Illegal byte sequence encounted in the input.");
1234 outptr = outbuf = g_malloc (outlen + 1);
1235 for (i = 0; i < len; i++)
1236 outptr += g_unichar_to_utf8 (str[i], outptr);
1240 *items_written = outlen;
1249 g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1251 gunichar2 *outbuf, *outptr;
1256 g_return_val_if_fail (str != NULL, NULL);
1259 for (i = 0; str[i] != 0; i++) {
1260 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1261 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1262 "Illegal byte sequence encounted in the input.");
1276 for (i = 0; i < len && str[i] != 0; i++) {
1277 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1278 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1279 "Illegal byte sequence encounted in the input.");
1296 outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
1297 for (i = 0; i < len; i++)
1298 outptr += g_unichar_to_utf16 (str[i], outptr);
1302 *items_written = outlen;