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.
38 #define FORCE_INLINE(RET_TYPE) __forceinline RET_TYPE
40 #define FORCE_INLINE(RET_TYPE) inline RET_TYPE __attribute__((always_inline))
44 #define UNROLL_DECODE_UTF8 0
45 #define UNROLL_ENCODE_UTF8 0
47 typedef int (* Decoder) (char *inbuf, size_t inleft, gunichar *outchar);
48 typedef int (* Encoder) (gunichar c, char *outbuf, size_t outleft);
59 static int decode_utf32be (char *inbuf, size_t inleft, gunichar *outchar);
60 static int encode_utf32be (gunichar c, char *outbuf, size_t outleft);
62 static int decode_utf32le (char *inbuf, size_t inleft, gunichar *outchar);
63 static int encode_utf32le (gunichar c, char *outbuf, size_t outleft);
65 static int decode_utf16be (char *inbuf, size_t inleft, gunichar *outchar);
66 static int encode_utf16be (gunichar c, char *outbuf, size_t outleft);
68 static int decode_utf16le (char *inbuf, size_t inleft, gunichar *outchar);
69 static int encode_utf16le (gunichar c, char *outbuf, size_t outleft);
71 static FORCE_INLINE (int) decode_utf8 (char *inbuf, size_t inleft, gunichar *outchar);
72 static int encode_utf8 (gunichar c, char *outbuf, size_t outleft);
74 static int decode_latin1 (char *inbuf, size_t inleft, gunichar *outchar);
75 static int encode_latin1 (gunichar c, char *outbuf, size_t outleft);
77 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
78 #define decode_utf32 decode_utf32le
79 #define encode_utf32 encode_utf32le
80 #define decode_utf16 decode_utf16le
81 #define encode_utf16 encode_utf16le
83 #define decode_utf32 decode_utf32be
84 #define encode_utf32 encode_utf32be
85 #define decode_utf16 decode_utf16be
86 #define encode_utf16 encode_utf16be
94 { "ISO-8859-1", decode_latin1, encode_latin1 },
95 { "ISO8859-1", decode_latin1, encode_latin1 },
96 { "UTF-32BE", decode_utf32be, encode_utf32be },
97 { "UTF-32LE", decode_utf32le, encode_utf32le },
98 { "UTF-16BE", decode_utf16be, encode_utf16be },
99 { "UTF-16LE", decode_utf16le, encode_utf16le },
100 { "UTF-32", decode_utf32, encode_utf32 },
101 { "UTF-16", decode_utf16, encode_utf16 },
102 { "UTF-8", decode_utf8, encode_utf8 },
103 { "US-ASCII", decode_latin1, encode_latin1 },
104 { "Latin1", decode_latin1, encode_latin1 },
105 { "ASCII", decode_latin1, encode_latin1 },
106 { "UTF32", decode_utf32, encode_utf32 },
107 { "UTF16", decode_utf16, encode_utf16 },
108 { "UTF8", decode_utf8, encode_utf8 },
113 g_iconv_open (const char *to_charset, const char *from_charset)
116 iconv_t icd = (iconv_t) -1;
118 Decoder decoder = NULL;
119 Encoder encoder = NULL;
123 if (!to_charset || !from_charset || !to_charset[0] || !from_charset[0]) {
129 for (i = 0; i < G_N_ELEMENTS (charsets); i++) {
130 if (!g_ascii_strcasecmp (charsets[i].name, from_charset))
131 decoder = charsets[i].decoder;
133 if (!g_ascii_strcasecmp (charsets[i].name, to_charset))
134 encoder = charsets[i].encoder;
137 if (!encoder || !decoder) {
139 if ((icd = iconv_open (to_charset, from_charset)) == (iconv_t) -1)
148 cd = (GIConv) g_malloc (sizeof (struct _GIConv));
149 cd->decode = decoder;
150 cd->encode = encoder;
161 g_iconv_close (GIConv cd)
164 if (cd->cd != (iconv_t) -1)
165 iconv_close (cd->cd);
174 g_iconv (GIConv cd, gchar **inbytes, gsize *inbytesleft,
175 gchar **outbytes, gsize *outbytesleft)
177 gsize inleft, outleft;
178 char *inptr, *outptr;
183 if (cd->cd != (iconv_t) -1) {
184 /* Note: gsize may have a different size than size_t, so we need to
185 remap inbytesleft and outbytesleft to size_t's. */
186 size_t *outleftptr, *inleftptr;
187 size_t n_outleft, n_inleft;
190 n_inleft = *inbytesleft;
191 inleftptr = &n_inleft;
197 n_outleft = *outbytesleft;
198 outleftptr = &n_outleft;
202 #if defined(__NetBSD__)
203 return iconv (cd->cd, (const gchar **)inbytes, inleftptr, outbytes, outleftptr);
205 return iconv (cd->cd, inbytes, inleftptr, outbytes, outleftptr);
210 if (outbytes == NULL || outbytesleft == NULL) {
211 /* reset converter */
216 inleft = inbytesleft ? *inbytesleft : 0;
217 inptr = inbytes ? *inbytes : NULL;
218 outleft = *outbytesleft;
221 if ((c = cd->c) != (gunichar) -1)
225 if ((rc = cd->decode (inptr, inleft, &c)) < 0)
232 if ((rc = cd->encode (c, outptr, outleft)) < 0)
241 *inbytesleft = inleft;
246 *outbytesleft = outleft;
250 return rc < 0 ? -1 : 0;
254 * Unicode encoders and decoders
258 decode_utf32be (char *inbuf, size_t inleft, gunichar *outchar)
260 unsigned char *inptr = (unsigned char *) inbuf;
268 c = (inptr[0] << 24) | (inptr[1] << 16) | (inptr[2] << 8) | inptr[3];
270 if (c >= 0xd800 && c < 0xe000) {
273 } else if (c >= 0x110000) {
284 decode_utf32le (char *inbuf, size_t inleft, gunichar *outchar)
286 unsigned char *inptr = (unsigned char *) inbuf;
294 c = (inptr[3] << 24) | (inptr[2] << 16) | (inptr[1] << 8) | inptr[0];
296 if (c >= 0xd800 && c < 0xe000) {
299 } else if (c >= 0x110000) {
310 encode_utf32be (gunichar c, char *outbuf, size_t outleft)
312 unsigned char *outptr = (unsigned char *) outbuf;
319 outptr[0] = (c >> 24) & 0xff;
320 outptr[1] = (c >> 16) & 0xff;
321 outptr[2] = (c >> 8) & 0xff;
322 outptr[3] = c & 0xff;
328 encode_utf32le (gunichar c, char *outbuf, size_t outleft)
330 unsigned char *outptr = (unsigned char *) outbuf;
337 outptr[0] = c & 0xff;
338 outptr[1] = (c >> 8) & 0xff;
339 outptr[2] = (c >> 16) & 0xff;
340 outptr[3] = (c >> 24) & 0xff;
346 decode_utf16be (char *inbuf, size_t inleft, gunichar *outchar)
348 unsigned char *inptr = (unsigned char *) inbuf;
357 u = (inptr[0] << 8) | inptr[1];
360 /* 0x0000 -> 0xd7ff */
363 } else if (u < 0xdc00) {
364 /* 0xd800 -> 0xdbff */
370 c = (inptr[2] << 8) | inptr[3];
372 if (c < 0xdc00 || c > 0xdfff) {
377 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
381 } else if (u < 0xe000) {
382 /* 0xdc00 -> 0xdfff */
386 /* 0xe000 -> 0xffff */
393 decode_utf16le (char *inbuf, size_t inleft, gunichar *outchar)
395 unsigned char *inptr = (unsigned char *) inbuf;
404 u = (inptr[1] << 8) | inptr[0];
407 /* 0x0000 -> 0xd7ff */
410 } else if (u < 0xdc00) {
411 /* 0xd800 -> 0xdbff */
417 c = (inptr[3] << 8) | inptr[2];
419 if (c < 0xdc00 || c > 0xdfff) {
424 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
428 } else if (u < 0xe000) {
429 /* 0xdc00 -> 0xdfff */
433 /* 0xe000 -> 0xffff */
440 encode_utf16be (gunichar c, char *outbuf, size_t outleft)
442 unsigned char *outptr = (unsigned char *) outbuf;
452 outptr[0] = (c >> 8) & 0xff;
453 outptr[1] = c & 0xff;
464 ch = (gunichar2) ((c2 >> 10) + 0xd800);
465 outptr[0] = (ch >> 8) & 0xff;
466 outptr[1] = ch & 0xff;
468 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
469 outptr[2] = (ch >> 8) & 0xff;
470 outptr[3] = ch & 0xff;
477 encode_utf16le (gunichar c, char *outbuf, size_t outleft)
479 unsigned char *outptr = (unsigned char *) outbuf;
489 outptr[0] = c & 0xff;
490 outptr[1] = (c >> 8) & 0xff;
501 ch = (gunichar2) ((c2 >> 10) + 0xd800);
502 outptr[0] = ch & 0xff;
503 outptr[1] = (ch >> 8) & 0xff;
505 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
506 outptr[2] = ch & 0xff;
507 outptr[3] = (ch >> 8) & 0xff;
513 static FORCE_INLINE (int)
514 decode_utf8 (char *inbuf, size_t inleft, gunichar *outchar)
516 unsigned char *inptr = (unsigned char *) inbuf;
523 /* simple ascii case */
526 } else if (u < 0xc2) {
529 } else if (u < 0xe0) {
532 } else if (u < 0xf0) {
535 } else if (u < 0xf8) {
538 } else if (u < 0xfc) {
541 } else if (u < 0xfe) {
554 #if UNROLL_DECODE_UTF8
556 case 6: u = (u << 6) | (*++inptr ^ 0x80);
557 case 5: u = (u << 6) | (*++inptr ^ 0x80);
558 case 4: u = (u << 6) | (*++inptr ^ 0x80);
559 case 3: u = (u << 6) | (*++inptr ^ 0x80);
560 case 2: u = (u << 6) | (*++inptr ^ 0x80);
563 for (i = 1; i < n; i++)
564 u = (u << 6) | (*++inptr ^ 0x80);
573 encode_utf8 (gunichar c, char *outbuf, size_t outleft)
575 unsigned char *outptr = (unsigned char *) outbuf;
581 } else if (c < 0x800) {
584 } else if (c < 0x10000) {
587 } else if (c < 0x200000) {
590 } else if (c < 0x4000000) {
603 #if UNROLL_ENCODE_UTF8
605 case 6: outptr[5] = (c & 0x3f) | 0x80; c >>= 6;
606 case 5: outptr[4] = (c & 0x3f) | 0x80; c >>= 6;
607 case 4: outptr[3] = (c & 0x3f) | 0x80; c >>= 6;
608 case 3: outptr[2] = (c & 0x3f) | 0x80; c >>= 6;
609 case 2: outptr[1] = (c & 0x3f) | 0x80; c >>= 6;
610 case 1: outptr[0] = c | base;
613 for (i = n - 1; i > 0; i--) {
614 outptr[i] = (c & 0x3f) | 0x80;
618 outptr[0] = c | base;
625 decode_latin1 (char *inbuf, size_t inleft, gunichar *outchar)
627 *outchar = (unsigned char) *inbuf;
632 encode_latin1 (gunichar c, char *outbuf, size_t outleft)
651 * Simple conversion API
654 static gpointer error_quark = "ConvertError";
657 g_convert_error_quark (void)
663 g_convert (const gchar *str, gssize len, const gchar *to_charset, const gchar *from_charset,
664 gsize *bytes_read, gsize *bytes_written, GError **err)
666 gsize outsize, outused, outleft, inleft, grow, rc;
667 char *result, *outbuf, *inbuf;
668 gboolean flush = FALSE;
669 gboolean done = FALSE;
672 g_return_val_if_fail (str != NULL, NULL);
673 g_return_val_if_fail (to_charset != NULL, NULL);
674 g_return_val_if_fail (from_charset != NULL, NULL);
676 if ((cd = g_iconv_open (to_charset, from_charset)) == (GIConv) -1) {
677 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
678 "Conversion from %s to %s not supported.",
679 from_charset, to_charset);
690 inleft = len < 0 ? strlen (str) : len;
691 inbuf = (char *) str;
693 outleft = outsize = MAX (inleft, 8);
694 outbuf = result = g_malloc (outsize + 4);
698 rc = g_iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
700 rc = g_iconv (cd, NULL, NULL, &outbuf, &outleft);
702 if (rc == (gsize) -1) {
705 /* grow our result buffer */
706 grow = MAX (inleft, 8) << 1;
707 outused = outbuf - result;
710 result = g_realloc (result, outsize + 4);
711 outbuf = result + outused;
714 /* incomplete input, stop converting and terminate here */
721 /* illegal sequence in the input */
722 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "%s", g_strerror (errno));
725 /* save offset of the illegal input sequence */
726 *bytes_read = (inbuf - str);
737 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, "%s", g_strerror (errno));
750 /* input has been converted and output has been flushed */
753 /* input has been converted, need to flush the output */
760 /* Note: not all charsets can be null-terminated with a single
761 null byte. UCS2, for example, needs 2 null bytes and UCS4
762 needs 4. I hope that 4 null bytes is enough to terminate all
763 multibyte charsets? */
765 /* null-terminate the result */
766 memset (outbuf, 0, 4);
769 *bytes_written = outbuf - result;
772 *bytes_read = inbuf - str;
783 * An explanation of the conversion can be found at:
784 * http://home.tiscali.nl/t876506/utf8tbl.html
788 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
795 } else if (c < 0x800) {
798 } else if (c < 0x10000) {
801 } else if (c < 0x200000) {
804 } else if (c < 0x4000000) {
807 } else if (c < 0x80000000) {
814 if (outbuf != NULL) {
815 for (i = n - 1; i > 0; i--) {
816 /* mask off 6 bits worth and add 128 */
817 outbuf[i] = (c & 0x3f) | 0x80;
821 /* first character has a different base */
822 outbuf[0] = c | base;
828 static FORCE_INLINE (int)
829 g_unichar_to_utf16 (gunichar c, gunichar2 *outbuf)
835 *outbuf = (gunichar2) c;
838 } else if (c < 0xe000) {
840 } else if (c < 0x10000) {
842 *outbuf = (gunichar2) c;
845 } else if (c < 0x110000) {
849 outbuf[0] = (gunichar2) ((c2 >> 10) + 0xd800);
850 outbuf[1] = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
860 g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
862 gunichar *outbuf, *outptr;
866 g_return_val_if_fail (str != NULL, NULL);
868 n = g_utf8_strlen (str, len);
873 outptr = outbuf = g_malloc ((n + 1) * sizeof (gunichar));
874 inptr = (char *) str;
876 for (i = 0; i < n; i++) {
877 *outptr++ = g_utf8_get_char (inptr);
878 inptr = g_utf8_next_char (inptr);
887 eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong *items_written, gboolean include_nuls, GError **err)
889 gunichar2 *outbuf, *outptr;
896 g_return_val_if_fail (str != NULL, NULL);
900 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, "Conversions with embedded nulls must pass the string length");
907 inptr = (char *) str;
911 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
914 if (c == 0 && !include_nuls)
917 if ((u = g_unichar_to_utf16 (c, NULL)) < 0) {
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)
941 if (c == 0 && !include_nuls)
944 outptr += g_unichar_to_utf16 (c, outptr);
954 if (errno == EILSEQ) {
955 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
956 "Illegal byte sequence encounted in the input.");
957 } else if (items_read) {
958 /* partial input is ok if we can let our caller know... */
960 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
961 "Partial byte sequence encountered in the input.");
965 *items_read = inptr - str;
974 g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
976 return eg_utf8_to_utf16_general (str, len, items_read, items_written, FALSE, err);
980 eg_utf8_to_utf16_with_nuls (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
982 return eg_utf8_to_utf16_general (str, len, items_read, items_written, TRUE, err);
986 g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
988 gunichar *outbuf, *outptr;
995 g_return_val_if_fail (str != NULL, NULL);
1000 inptr = (char *) str;
1003 while (inleft > 0) {
1004 if ((n = decode_utf8 (inptr, inleft, &c)) < 0) {
1005 if (errno == EILSEQ) {
1006 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1007 "Illegal byte sequence encounted in the input.");
1008 } else if (items_read) {
1009 /* partial input is ok if we can let our caller know... */
1012 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1013 "Partial byte sequence encountered in the input.");
1017 *items_read = inptr - str;
1032 *items_written = outlen / 4;
1035 *items_read = inptr - str;
1037 outptr = outbuf = g_malloc (outlen + 4);
1038 inptr = (char *) str;
1041 while (inleft > 0) {
1042 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
1058 g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1060 char *inptr, *outbuf, *outptr;
1066 g_return_val_if_fail (str != NULL, NULL);
1074 inptr = (char *) str;
1077 while (inleft > 0) {
1078 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1079 if (n == -2 && inleft > 2) {
1080 /* This means that the first UTF-16 char was read, but second failed */
1085 if (errno == EILSEQ) {
1086 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1087 "Illegal byte sequence encounted in the input.");
1088 } else if (items_read) {
1089 /* partial input is ok if we can let our caller know... */
1092 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1093 "Partial byte sequence encountered in the input.");
1097 *items_read = (inptr - (char *) str) / 2;
1106 outlen += g_unichar_to_utf8 (c, NULL);
1112 *items_read = (inptr - (char *) str) / 2;
1115 *items_written = outlen;
1117 outptr = outbuf = g_malloc (outlen + 1);
1118 inptr = (char *) str;
1121 while (inleft > 0) {
1122 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1127 outptr += g_unichar_to_utf8 (c, outptr);
1138 g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1140 gunichar *outbuf, *outptr;
1147 g_return_val_if_fail (str != NULL, NULL);
1155 inptr = (char *) str;
1158 while (inleft > 0) {
1159 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1160 if (n == -2 && inleft > 2) {
1161 /* This means that the first UTF-16 char was read, but second failed */
1166 if (errno == EILSEQ) {
1167 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1168 "Illegal byte sequence encounted in the input.");
1169 } else if (items_read) {
1170 /* partial input is ok if we can let our caller know... */
1173 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1174 "Partial byte sequence encountered in the input.");
1178 *items_read = (inptr - (char *) str) / 2;
1193 *items_read = (inptr - (char *) str) / 2;
1196 *items_written = outlen / 4;
1198 outptr = outbuf = g_malloc (outlen + 4);
1199 inptr = (char *) str;
1202 while (inleft > 0) {
1203 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1219 g_ucs4_to_utf8 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1221 char *outbuf, *outptr;
1226 g_return_val_if_fail (str != NULL, NULL);
1229 for (i = 0; str[i] != 0; i++) {
1230 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1231 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1232 "Illegal byte sequence encounted in the input.");
1246 for (i = 0; i < len && str[i] != 0; i++) {
1247 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1248 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1249 "Illegal byte sequence encounted in the input.");
1266 outptr = outbuf = g_malloc (outlen + 1);
1267 for (i = 0; i < len; i++)
1268 outptr += g_unichar_to_utf8 (str[i], outptr);
1272 *items_written = outlen;
1281 g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1283 gunichar2 *outbuf, *outptr;
1288 g_return_val_if_fail (str != NULL, NULL);
1291 for (i = 0; str[i] != 0; i++) {
1292 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1293 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1294 "Illegal byte sequence encounted in the input.");
1308 for (i = 0; i < len && str[i] != 0; i++) {
1309 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1310 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1311 "Illegal byte sequence encounted in the input.");
1328 outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
1329 for (i = 0; i < len; i++)
1330 outptr += g_unichar_to_utf16 (str[i], outptr);
1334 *items_written = outlen;