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 unsigned char *inptr = (unsigned char *) inbuf;
238 c = (inptr[0] << 24) | (inptr[1] << 16) | (inptr[2] << 8) | inptr[3];
240 if (c >= 0xd800 && c < 0xe000) {
243 } else if (c >= 0x110000) {
254 decode_utf32le (char *inbuf, size_t inleft, gunichar *outchar)
256 unsigned char *inptr = (unsigned char *) inbuf;
264 c = (inptr[3] << 24) | (inptr[2] << 16) | (inptr[1] << 8) | inptr[0];
266 if (c >= 0xd800 && c < 0xe000) {
269 } else if (c >= 0x110000) {
280 encode_utf32be (gunichar c, char *outbuf, size_t outleft)
282 unsigned char *outptr = (unsigned char *) outbuf;
289 outptr[0] = (c >> 24) & 0xff;
290 outptr[1] = (c >> 16) & 0xff;
291 outptr[2] = (c >> 8) & 0xff;
292 outptr[3] = c & 0xff;
298 encode_utf32le (gunichar c, char *outbuf, size_t outleft)
300 unsigned char *outptr = (unsigned char *) outbuf;
307 outptr[0] = c & 0xff;
308 outptr[1] = (c >> 8) & 0xff;
309 outptr[2] = (c >> 16) & 0xff;
310 outptr[3] = (c >> 24) & 0xff;
316 decode_utf16be (char *inbuf, size_t inleft, gunichar *outchar)
318 unsigned char *inptr = (unsigned char *) inbuf;
327 u = (inptr[0] << 8) | inptr[1];
330 /* 0x0000 -> 0xd7ff */
333 } else if (u < 0xdc00) {
334 /* 0xd800 -> 0xdbff */
340 c = (inptr[2] << 8) | inptr[3];
342 if (c < 0xdc00 || c > 0xdfff) {
347 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
351 } else if (u < 0xe000) {
352 /* 0xdc00 -> 0xdfff */
356 /* 0xe000 -> 0xffff */
363 decode_utf16le (char *inbuf, size_t inleft, gunichar *outchar)
365 unsigned char *inptr = (unsigned char *) inbuf;
374 u = (inptr[1] << 8) | inptr[0];
377 /* 0x0000 -> 0xd7ff */
380 } else if (u < 0xdc00) {
381 /* 0xd800 -> 0xdbff */
387 c = (inptr[3] << 8) | inptr[2];
389 if (c < 0xdc00 || c > 0xdfff) {
394 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
398 } else if (u < 0xe000) {
399 /* 0xdc00 -> 0xdfff */
403 /* 0xe000 -> 0xffff */
410 encode_utf16be (gunichar c, char *outbuf, size_t outleft)
412 unsigned char *outptr = (unsigned char *) outbuf;
422 outptr[0] = (c >> 8) & 0xff;
423 outptr[1] = c & 0xff;
434 ch = (gunichar2) ((c2 >> 10) + 0xd800);
435 outptr[0] = (ch >> 8) & 0xff;
436 outptr[1] = ch & 0xff;
438 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
439 outptr[2] = (ch >> 8) & 0xff;
440 outptr[3] = ch & 0xff;
447 encode_utf16le (gunichar c, char *outbuf, size_t outleft)
449 unsigned char *outptr = (unsigned char *) outbuf;
459 outptr[0] = c & 0xff;
460 outptr[1] = (c >> 8) & 0xff;
471 ch = (gunichar2) ((c2 >> 10) + 0xd800);
472 outptr[0] = ch & 0xff;
473 outptr[1] = (ch >> 8) & 0xff;
475 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
476 outptr[2] = ch & 0xff;
477 outptr[3] = (ch >> 8) & 0xff;
484 decode_utf8 (char *inbuf, size_t inleft, gunichar *outchar)
486 unsigned char *inptr = (unsigned char *) inbuf;
493 /* simple ascii case */
496 } else if (u < 0xc2) {
499 } else if (u < 0xe0) {
502 } else if (u < 0xf0) {
505 } else if (u < 0xf8) {
508 } else if (u < 0xfc) {
511 } else if (u < 0xfe) {
524 #if UNROLL_DECODE_UTF8
526 case 6: u = (u << 6) | (*++inptr ^ 0x80);
527 case 5: u = (u << 6) | (*++inptr ^ 0x80);
528 case 4: u = (u << 6) | (*++inptr ^ 0x80);
529 case 3: u = (u << 6) | (*++inptr ^ 0x80);
530 case 2: u = (u << 6) | (*++inptr ^ 0x80);
533 for (i = 1; i < n; i++)
534 u = (u << 6) | (*++inptr ^ 0x80);
543 encode_utf8 (gunichar c, char *outbuf, size_t outleft)
545 unsigned char *outptr = (unsigned char *) outbuf;
551 } else if (c < 0x800) {
554 } else if (c < 0x10000) {
557 } else if (c < 0x200000) {
560 } else if (c < 0x4000000) {
573 #if UNROLL_ENCODE_UTF8
575 case 6: outptr[5] = (c & 0x3f) | 0x80; c >>= 6;
576 case 5: outptr[4] = (c & 0x3f) | 0x80; c >>= 6;
577 case 4: outptr[3] = (c & 0x3f) | 0x80; c >>= 6;
578 case 3: outptr[2] = (c & 0x3f) | 0x80; c >>= 6;
579 case 2: outptr[1] = (c & 0x3f) | 0x80; c >>= 6;
580 case 1: outptr[0] = c | base;
583 for (i = n - 1; i > 0; i--) {
584 outptr[i] = (c & 0x3f) | 0x80;
588 outptr[0] = c | base;
595 decode_latin1 (char *inbuf, size_t inleft, gunichar *outchar)
597 *outchar = (unsigned char) *inbuf;
602 encode_latin1 (gunichar c, char *outbuf, size_t outleft)
621 * Simple conversion API
624 static gpointer error_quark = "ConvertError";
627 g_convert_error_quark (void)
633 g_convert (const gchar *str, gssize len, const gchar *to_charset, const gchar *from_charset,
634 gsize *bytes_read, gsize *bytes_written, GError **err)
636 size_t outsize, outused, outleft, inleft, grow, rc;
637 char *result, *outbuf, *inbuf;
638 gboolean flush = FALSE;
639 gboolean done = FALSE;
642 g_return_val_if_fail (str != NULL, NULL);
643 g_return_val_if_fail (to_charset != NULL, NULL);
644 g_return_val_if_fail (from_charset != NULL, NULL);
646 if ((cd = g_iconv_open (to_charset, from_charset)) == (GIConv) -1) {
647 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
648 "Conversion from %s to %s not supported.",
649 from_charset, to_charset);
660 inleft = len < 0 ? strlen (str) : len;
661 inbuf = (char *) str;
663 outleft = outsize = MAX (inleft, 8);
664 outbuf = result = g_malloc (outsize + 4);
668 rc = g_iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
670 rc = g_iconv (cd, NULL, NULL, &outbuf, &outleft);
672 if (rc == (size_t) -1) {
675 /* grow our result buffer */
676 grow = MAX (inleft, 8) << 1;
677 outused = outbuf - result;
680 result = g_realloc (result, outsize + 4);
681 outbuf = result + outused;
684 /* incomplete input, stop converting and terminate here */
691 /* illegal sequence in the input */
692 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "%s", g_strerror (errno));
695 /* save offset of the illegal input sequence */
696 *bytes_read = (inbuf - str);
707 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, "%s", g_strerror (errno));
720 /* input has been converted and output has been flushed */
723 /* input has been converted, need to flush the output */
730 /* Note: not all charsets can be null-terminated with a single
731 null byte. UCS2, for example, needs 2 null bytes and UCS4
732 needs 4. I hope that 4 null bytes is enough to terminate all
733 multibyte charsets? */
735 /* null-terminate the result */
736 memset (outbuf, 0, 4);
739 *bytes_written = outbuf - result;
742 *bytes_read = inbuf - str;
753 * from http://home.tiscali.nl/t876506/utf8tbl.html
755 * From Unicode UCS-4 to UTF-8:
756 * Start with the Unicode number expressed as a decimal number and call this ud.
758 * If ud <128 (7F hex) then UTF-8 is 1 byte long, the value of ud.
760 * If ud >=128 and <=2047 (7FF hex) then UTF-8 is 2 bytes long.
761 * byte 1 = 192 + (ud div 64)
762 * byte 2 = 128 + (ud mod 64)
764 * If ud >=2048 and <=65535 (FFFF hex) then UTF-8 is 3 bytes long.
765 * byte 1 = 224 + (ud div 4096)
766 * byte 2 = 128 + ((ud div 64) mod 64)
767 * byte 3 = 128 + (ud mod 64)
769 * If ud >=65536 and <=2097151 (1FFFFF hex) then UTF-8 is 4 bytes long.
770 * byte 1 = 240 + (ud div 262144)
771 * byte 2 = 128 + ((ud div 4096) mod 64)
772 * byte 3 = 128 + ((ud div 64) mod 64)
773 * byte 4 = 128 + (ud mod 64)
775 * If ud >=2097152 and <=67108863 (3FFFFFF hex) then UTF-8 is 5 bytes long.
776 * byte 1 = 248 + (ud div 16777216)
777 * byte 2 = 128 + ((ud div 262144) mod 64)
778 * byte 3 = 128 + ((ud div 4096) mod 64)
779 * byte 4 = 128 + ((ud div 64) mod 64)
780 * byte 5 = 128 + (ud mod 64)
782 * If ud >=67108864 and <=2147483647 (7FFFFFFF hex) then UTF-8 is 6 bytes long.
783 * byte 1 = 252 + (ud div 1073741824)
784 * byte 2 = 128 + ((ud div 16777216) mod 64)
785 * byte 3 = 128 + ((ud div 262144) mod 64)
786 * byte 4 = 128 + ((ud div 4096) mod 64)
787 * byte 5 = 128 + ((ud div 64) mod 64)
788 * byte 6 = 128 + (ud mod 64)
791 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
798 } else if (c < 0x800) {
801 } else if (c < 0x10000) {
804 } else if (c < 0x200000) {
807 } else if (c < 0x4000000) {
810 } else if (c < 0x80000000) {
817 if (outbuf != NULL) {
818 for (i = n - 1; i > 0; i--) {
819 /* mask off 6 bits worth and add 128 */
820 outbuf[i] = (c & 0x3f) | 0x80;
824 /* first character has a different base */
825 outbuf[0] = c | base;
832 g_unichar_to_utf16 (gunichar c, gunichar2 *outbuf)
838 *outbuf = (gunichar2) c;
841 } else if (c < 0xe000) {
843 } else if (c < 0x10000) {
845 *outbuf = (gunichar2) c;
848 } else if (c < 0x110000) {
852 outbuf[0] = (gunichar2) ((c2 >> 10) + 0xd800);
853 outbuf[1] = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
863 g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
865 gunichar *outbuf, *outptr;
869 g_return_val_if_fail (str != NULL, NULL);
871 n = g_utf8_strlen (str, len);
876 outptr = outbuf = g_malloc ((n + 1) * sizeof (gunichar));
877 inptr = (char *) str;
879 for (i = 0; i < n; i++) {
880 *outptr++ = g_utf8_get_char (inptr);
881 inptr = g_utf8_next_char (inptr);
890 g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
892 gunichar2 *outbuf, *outptr;
899 g_return_val_if_fail (str != NULL, NULL);
904 inptr = (char *) str;
908 if ((n = decode_utf8 (inptr, inleft, &c)) < 0) {
909 if (errno == EILSEQ) {
910 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
911 "Illegal byte sequence encounted in the input.");
912 } else if (items_read) {
913 /* partial input is ok if we can let our caller know... */
916 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
917 "Partial byte sequence encountered in the input.");
921 *items_read = inptr - str;
930 outlen += g_unichar_to_utf16 (c, NULL);
936 *items_read = inptr - str;
939 *items_written = outlen;
941 outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
942 inptr = (char *) str;
946 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
951 outptr += g_unichar_to_utf16 (c, outptr);
962 g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
964 gunichar *outbuf, *outptr;
971 g_return_val_if_fail (str != NULL, NULL);
976 inptr = (char *) str;
980 if ((n = decode_utf8 (inptr, inleft, &c)) < 0) {
981 if (errno == EILSEQ) {
982 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
983 "Illegal byte sequence encounted in the input.");
984 } else if (items_read) {
985 /* partial input is ok if we can let our caller know... */
988 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
989 "Partial byte sequence encountered in the input.");
993 *items_read = inptr - str;
1008 *items_written = outlen / 4;
1011 *items_read = inptr - str;
1013 outptr = outbuf = g_malloc (outlen + 4);
1014 inptr = (char *) str;
1017 while (inleft > 0) {
1018 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
1034 g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1036 char *inptr, *outbuf, *outptr;
1042 g_return_val_if_fail (str != NULL, NULL);
1050 inptr = (char *) str;
1053 while (inleft > 0) {
1054 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1055 if (n == -2 && inleft > 2) {
1056 /* This means that the first UTF-16 char was read, but second failed */
1061 if (errno == EILSEQ) {
1062 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1063 "Illegal byte sequence encounted in the input.");
1064 } else if (items_read) {
1065 /* partial input is ok if we can let our caller know... */
1068 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1069 "Partial byte sequence encountered in the input.");
1073 *items_read = (inptr - (char *) str) / 2;
1082 outlen += g_unichar_to_utf8 (c, NULL);
1088 *items_read = (inptr - (char *) str) / 2;
1091 *items_written = outlen;
1093 outptr = outbuf = g_malloc (outlen + 1);
1094 inptr = (char *) str;
1097 while (inleft > 0) {
1098 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1103 outptr += g_unichar_to_utf8 (c, outptr);
1114 g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1116 gunichar *outbuf, *outptr;
1123 g_return_val_if_fail (str != NULL, NULL);
1131 inptr = (char *) str;
1134 while (inleft > 0) {
1135 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1136 if (n == -2 && inleft > 2) {
1137 /* This means that the first UTF-16 char was read, but second failed */
1142 if (errno == EILSEQ) {
1143 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1144 "Illegal byte sequence encounted in the input.");
1145 } else if (items_read) {
1146 /* partial input is ok if we can let our caller know... */
1149 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1150 "Partial byte sequence encountered in the input.");
1154 *items_read = (inptr - (char *) str) / 2;
1169 *items_read = (inptr - (char *) str) / 2;
1172 *items_written = outlen / 4;
1174 outptr = outbuf = g_malloc (outlen + 4);
1175 inptr = (char *) str;
1178 while (inleft > 0) {
1179 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1195 g_ucs4_to_utf8 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1197 char *outbuf, *outptr;
1202 g_return_val_if_fail (str != NULL, NULL);
1205 for (i = 0; str[i] != 0; i++) {
1206 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1207 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1208 "Illegal byte sequence encounted in the input.");
1222 for (i = 0; i < len && str[i] != 0; i++) {
1223 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1224 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1225 "Illegal byte sequence encounted in the input.");
1242 outptr = outbuf = g_malloc (outlen + 1);
1243 for (i = 0; i < len; i++)
1244 outptr += g_unichar_to_utf8 (str[i], outptr);
1248 *items_written = outlen;
1257 g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1259 gunichar2 *outbuf, *outptr;
1264 g_return_val_if_fail (str != NULL, NULL);
1267 for (i = 0; str[i] != 0; i++) {
1268 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1269 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1270 "Illegal byte sequence encounted in the input.");
1284 for (i = 0; i < len && str[i] != 0; i++) {
1285 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1286 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1287 "Illegal byte sequence encounted in the input.");
1304 outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
1305 for (i = 0; i < len; i++)
1306 outptr += g_unichar_to_utf16 (str[i], outptr);
1310 *items_written = outlen;