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 size_t 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. */
189 outleft = *outbytesleft;
190 outleftptr = &outleft;
195 inleft = inbytesleft ? *inbytesleft : 0;
197 return iconv (cd->cd, inbytes, &inleft, outbytes, outleftptr);
201 if (outbytes == NULL || outbytesleft == NULL) {
202 /* reset converter */
207 inleft = inbytesleft ? *inbytesleft : 0;
208 inptr = inbytes ? *inbytes : NULL;
209 outleft = *outbytesleft;
212 if ((c = cd->c) != (gunichar) -1)
216 if ((rc = cd->decode (inptr, inleft, &c)) < 0)
223 if ((rc = cd->encode (c, outptr, outleft)) < 0)
232 *inbytesleft = inleft;
237 *outbytesleft = outleft;
241 return rc < 0 ? -1 : 0;
245 * Unicode encoders and decoders
249 decode_utf32be (char *inbuf, size_t inleft, gunichar *outchar)
251 unsigned char *inptr = (unsigned char *) inbuf;
259 c = (inptr[0] << 24) | (inptr[1] << 16) | (inptr[2] << 8) | inptr[3];
261 if (c >= 0xd800 && c < 0xe000) {
264 } else if (c >= 0x110000) {
275 decode_utf32le (char *inbuf, size_t inleft, gunichar *outchar)
277 unsigned char *inptr = (unsigned char *) inbuf;
285 c = (inptr[3] << 24) | (inptr[2] << 16) | (inptr[1] << 8) | inptr[0];
287 if (c >= 0xd800 && c < 0xe000) {
290 } else if (c >= 0x110000) {
301 encode_utf32be (gunichar c, char *outbuf, size_t outleft)
303 unsigned char *outptr = (unsigned char *) outbuf;
310 outptr[0] = (c >> 24) & 0xff;
311 outptr[1] = (c >> 16) & 0xff;
312 outptr[2] = (c >> 8) & 0xff;
313 outptr[3] = c & 0xff;
319 encode_utf32le (gunichar c, char *outbuf, size_t outleft)
321 unsigned char *outptr = (unsigned char *) outbuf;
328 outptr[0] = c & 0xff;
329 outptr[1] = (c >> 8) & 0xff;
330 outptr[2] = (c >> 16) & 0xff;
331 outptr[3] = (c >> 24) & 0xff;
337 decode_utf16be (char *inbuf, size_t inleft, gunichar *outchar)
339 unsigned char *inptr = (unsigned char *) inbuf;
348 u = (inptr[0] << 8) | inptr[1];
351 /* 0x0000 -> 0xd7ff */
354 } else if (u < 0xdc00) {
355 /* 0xd800 -> 0xdbff */
361 c = (inptr[2] << 8) | inptr[3];
363 if (c < 0xdc00 || c > 0xdfff) {
368 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
372 } else if (u < 0xe000) {
373 /* 0xdc00 -> 0xdfff */
377 /* 0xe000 -> 0xffff */
384 decode_utf16le (char *inbuf, size_t inleft, gunichar *outchar)
386 unsigned char *inptr = (unsigned char *) inbuf;
395 u = (inptr[1] << 8) | inptr[0];
398 /* 0x0000 -> 0xd7ff */
401 } else if (u < 0xdc00) {
402 /* 0xd800 -> 0xdbff */
408 c = (inptr[3] << 8) | inptr[2];
410 if (c < 0xdc00 || c > 0xdfff) {
415 u = ((u - 0xd800) << 10) + (c - 0xdc00) + 0x0010000UL;
419 } else if (u < 0xe000) {
420 /* 0xdc00 -> 0xdfff */
424 /* 0xe000 -> 0xffff */
431 encode_utf16be (gunichar c, char *outbuf, size_t outleft)
433 unsigned char *outptr = (unsigned char *) outbuf;
443 outptr[0] = (c >> 8) & 0xff;
444 outptr[1] = c & 0xff;
455 ch = (gunichar2) ((c2 >> 10) + 0xd800);
456 outptr[0] = (ch >> 8) & 0xff;
457 outptr[1] = ch & 0xff;
459 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
460 outptr[2] = (ch >> 8) & 0xff;
461 outptr[3] = ch & 0xff;
468 encode_utf16le (gunichar c, char *outbuf, size_t outleft)
470 unsigned char *outptr = (unsigned char *) outbuf;
480 outptr[0] = c & 0xff;
481 outptr[1] = (c >> 8) & 0xff;
492 ch = (gunichar2) ((c2 >> 10) + 0xd800);
493 outptr[0] = ch & 0xff;
494 outptr[1] = (ch >> 8) & 0xff;
496 ch = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
497 outptr[2] = ch & 0xff;
498 outptr[3] = (ch >> 8) & 0xff;
504 static FORCE_INLINE (int)
505 decode_utf8 (char *inbuf, size_t inleft, gunichar *outchar)
507 unsigned char *inptr = (unsigned char *) inbuf;
514 /* simple ascii case */
517 } else if (u < 0xc2) {
520 } else if (u < 0xe0) {
523 } else if (u < 0xf0) {
526 } else if (u < 0xf8) {
529 } else if (u < 0xfc) {
532 } else if (u < 0xfe) {
545 #if UNROLL_DECODE_UTF8
547 case 6: u = (u << 6) | (*++inptr ^ 0x80);
548 case 5: u = (u << 6) | (*++inptr ^ 0x80);
549 case 4: u = (u << 6) | (*++inptr ^ 0x80);
550 case 3: u = (u << 6) | (*++inptr ^ 0x80);
551 case 2: u = (u << 6) | (*++inptr ^ 0x80);
554 for (i = 1; i < n; i++)
555 u = (u << 6) | (*++inptr ^ 0x80);
564 encode_utf8 (gunichar c, char *outbuf, size_t outleft)
566 unsigned char *outptr = (unsigned char *) outbuf;
572 } else if (c < 0x800) {
575 } else if (c < 0x10000) {
578 } else if (c < 0x200000) {
581 } else if (c < 0x4000000) {
594 #if UNROLL_ENCODE_UTF8
596 case 6: outptr[5] = (c & 0x3f) | 0x80; c >>= 6;
597 case 5: outptr[4] = (c & 0x3f) | 0x80; c >>= 6;
598 case 4: outptr[3] = (c & 0x3f) | 0x80; c >>= 6;
599 case 3: outptr[2] = (c & 0x3f) | 0x80; c >>= 6;
600 case 2: outptr[1] = (c & 0x3f) | 0x80; c >>= 6;
601 case 1: outptr[0] = c | base;
604 for (i = n - 1; i > 0; i--) {
605 outptr[i] = (c & 0x3f) | 0x80;
609 outptr[0] = c | base;
616 decode_latin1 (char *inbuf, size_t inleft, gunichar *outchar)
618 *outchar = (unsigned char) *inbuf;
623 encode_latin1 (gunichar c, char *outbuf, size_t outleft)
642 * Simple conversion API
645 static gpointer error_quark = "ConvertError";
648 g_convert_error_quark (void)
654 g_convert (const gchar *str, gssize len, const gchar *to_charset, const gchar *from_charset,
655 gsize *bytes_read, gsize *bytes_written, GError **err)
657 size_t outsize, outused, outleft, inleft, grow, rc;
658 char *result, *outbuf, *inbuf;
659 gboolean flush = FALSE;
660 gboolean done = FALSE;
663 g_return_val_if_fail (str != NULL, NULL);
664 g_return_val_if_fail (to_charset != NULL, NULL);
665 g_return_val_if_fail (from_charset != NULL, NULL);
667 if ((cd = g_iconv_open (to_charset, from_charset)) == (GIConv) -1) {
668 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
669 "Conversion from %s to %s not supported.",
670 from_charset, to_charset);
681 inleft = len < 0 ? strlen (str) : len;
682 inbuf = (char *) str;
684 outleft = outsize = MAX (inleft, 8);
685 outbuf = result = g_malloc (outsize + 4);
689 rc = g_iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
691 rc = g_iconv (cd, NULL, NULL, &outbuf, &outleft);
693 if (rc == (size_t) -1) {
696 /* grow our result buffer */
697 grow = MAX (inleft, 8) << 1;
698 outused = outbuf - result;
701 result = g_realloc (result, outsize + 4);
702 outbuf = result + outused;
705 /* incomplete input, stop converting and terminate here */
712 /* illegal sequence in the input */
713 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "%s", g_strerror (errno));
716 /* save offset of the illegal input sequence */
717 *bytes_read = (inbuf - str);
728 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, "%s", g_strerror (errno));
741 /* input has been converted and output has been flushed */
744 /* input has been converted, need to flush the output */
751 /* Note: not all charsets can be null-terminated with a single
752 null byte. UCS2, for example, needs 2 null bytes and UCS4
753 needs 4. I hope that 4 null bytes is enough to terminate all
754 multibyte charsets? */
756 /* null-terminate the result */
757 memset (outbuf, 0, 4);
760 *bytes_written = outbuf - result;
763 *bytes_read = inbuf - str;
774 * from http://home.tiscali.nl/t876506/utf8tbl.html
776 * From Unicode UCS-4 to UTF-8:
777 * Start with the Unicode number expressed as a decimal number and call this ud.
779 * If ud <128 (7F hex) then UTF-8 is 1 byte long, the value of ud.
781 * If ud >=128 and <=2047 (7FF hex) then UTF-8 is 2 bytes long.
782 * byte 1 = 192 + (ud div 64)
783 * byte 2 = 128 + (ud mod 64)
785 * If ud >=2048 and <=65535 (FFFF hex) then UTF-8 is 3 bytes long.
786 * byte 1 = 224 + (ud div 4096)
787 * byte 2 = 128 + ((ud div 64) mod 64)
788 * byte 3 = 128 + (ud mod 64)
790 * If ud >=65536 and <=2097151 (1FFFFF hex) then UTF-8 is 4 bytes long.
791 * byte 1 = 240 + (ud div 262144)
792 * byte 2 = 128 + ((ud div 4096) mod 64)
793 * byte 3 = 128 + ((ud div 64) mod 64)
794 * byte 4 = 128 + (ud mod 64)
796 * If ud >=2097152 and <=67108863 (3FFFFFF hex) then UTF-8 is 5 bytes long.
797 * byte 1 = 248 + (ud div 16777216)
798 * byte 2 = 128 + ((ud div 262144) mod 64)
799 * byte 3 = 128 + ((ud div 4096) mod 64)
800 * byte 4 = 128 + ((ud div 64) mod 64)
801 * byte 5 = 128 + (ud mod 64)
803 * If ud >=67108864 and <=2147483647 (7FFFFFFF hex) then UTF-8 is 6 bytes long.
804 * byte 1 = 252 + (ud div 1073741824)
805 * byte 2 = 128 + ((ud div 16777216) mod 64)
806 * byte 3 = 128 + ((ud div 262144) mod 64)
807 * byte 4 = 128 + ((ud div 4096) mod 64)
808 * byte 5 = 128 + ((ud div 64) mod 64)
809 * byte 6 = 128 + (ud mod 64)
812 g_unichar_to_utf8 (gunichar c, gchar *outbuf)
819 } else if (c < 0x800) {
822 } else if (c < 0x10000) {
825 } else if (c < 0x200000) {
828 } else if (c < 0x4000000) {
831 } else if (c < 0x80000000) {
838 if (outbuf != NULL) {
839 for (i = n - 1; i > 0; i--) {
840 /* mask off 6 bits worth and add 128 */
841 outbuf[i] = (c & 0x3f) | 0x80;
845 /* first character has a different base */
846 outbuf[0] = c | base;
852 static FORCE_INLINE (int)
853 g_unichar_to_utf16 (gunichar c, gunichar2 *outbuf)
859 *outbuf = (gunichar2) c;
862 } else if (c < 0xe000) {
864 } else if (c < 0x10000) {
866 *outbuf = (gunichar2) c;
869 } else if (c < 0x110000) {
873 outbuf[0] = (gunichar2) ((c2 >> 10) + 0xd800);
874 outbuf[1] = (gunichar2) ((c2 & 0x3ff) + 0xdc00);
884 g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
886 gunichar *outbuf, *outptr;
890 g_return_val_if_fail (str != NULL, NULL);
892 n = g_utf8_strlen (str, len);
897 outptr = outbuf = g_malloc ((n + 1) * sizeof (gunichar));
898 inptr = (char *) str;
900 for (i = 0; i < n; i++) {
901 *outptr++ = g_utf8_get_char (inptr);
902 inptr = g_utf8_next_char (inptr);
911 eg_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong *items_written, gboolean include_nuls, GError **err)
913 gunichar2 *outbuf, *outptr;
920 g_return_val_if_fail (str != NULL, NULL);
924 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, "Conversions with embedded nulls must pass the string length");
930 inptr = (char *) str;
934 if ((n = decode_utf8 (inptr, inleft, &c)) < 0) {
935 if (errno == EILSEQ) {
936 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
937 "Illegal byte sequence encounted in the input.");
938 } else if (items_read) {
939 /* partial input is ok if we can let our caller know... */
942 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
943 "Partial byte sequence encountered in the input.");
947 *items_read = inptr - str;
953 } else if (c == 0 && !include_nuls)
956 outlen += g_unichar_to_utf16 (c, NULL);
962 *items_read = inptr - str;
965 *items_written = outlen;
967 outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
968 inptr = (char *) str;
972 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
974 else if (c == 0 && !include_nuls)
977 outptr += g_unichar_to_utf16 (c, outptr);
988 g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
990 return eg_utf8_to_utf16_general (str, len, items_read, items_written, FALSE, err);
994 eg_utf8_to_utf16_with_nuls (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
996 return eg_utf8_to_utf16_general (str, len, items_read, items_written, TRUE, err);
1000 g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
1002 gunichar *outbuf, *outptr;
1009 g_return_val_if_fail (str != NULL, NULL);
1014 inptr = (char *) str;
1017 while (inleft > 0) {
1018 if ((n = decode_utf8 (inptr, inleft, &c)) < 0) {
1019 if (errno == EILSEQ) {
1020 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1021 "Illegal byte sequence encounted in the input.");
1022 } else if (items_read) {
1023 /* partial input is ok if we can let our caller know... */
1026 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1027 "Partial byte sequence encountered in the input.");
1031 *items_read = inptr - str;
1046 *items_written = outlen / 4;
1049 *items_read = inptr - str;
1051 outptr = outbuf = g_malloc (outlen + 4);
1052 inptr = (char *) str;
1055 while (inleft > 0) {
1056 if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
1072 g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1074 char *inptr, *outbuf, *outptr;
1080 g_return_val_if_fail (str != NULL, NULL);
1088 inptr = (char *) str;
1091 while (inleft > 0) {
1092 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1093 if (n == -2 && inleft > 2) {
1094 /* This means that the first UTF-16 char was read, but second failed */
1099 if (errno == EILSEQ) {
1100 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1101 "Illegal byte sequence encounted in the input.");
1102 } else if (items_read) {
1103 /* partial input is ok if we can let our caller know... */
1106 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1107 "Partial byte sequence encountered in the input.");
1111 *items_read = (inptr - (char *) str) / 2;
1120 outlen += g_unichar_to_utf8 (c, NULL);
1126 *items_read = (inptr - (char *) str) / 2;
1129 *items_written = outlen;
1131 outptr = outbuf = g_malloc (outlen + 1);
1132 inptr = (char *) str;
1135 while (inleft > 0) {
1136 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1141 outptr += g_unichar_to_utf8 (c, outptr);
1152 g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err)
1154 gunichar *outbuf, *outptr;
1161 g_return_val_if_fail (str != NULL, NULL);
1169 inptr = (char *) str;
1172 while (inleft > 0) {
1173 if ((n = decode_utf16 (inptr, inleft, &c)) < 0) {
1174 if (n == -2 && inleft > 2) {
1175 /* This means that the first UTF-16 char was read, but second failed */
1180 if (errno == EILSEQ) {
1181 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1182 "Illegal byte sequence encounted in the input.");
1183 } else if (items_read) {
1184 /* partial input is ok if we can let our caller know... */
1187 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1188 "Partial byte sequence encountered in the input.");
1192 *items_read = (inptr - (char *) str) / 2;
1207 *items_read = (inptr - (char *) str) / 2;
1210 *items_written = outlen / 4;
1212 outptr = outbuf = g_malloc (outlen + 4);
1213 inptr = (char *) str;
1216 while (inleft > 0) {
1217 if ((n = decode_utf16 (inptr, inleft, &c)) < 0)
1233 g_ucs4_to_utf8 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1235 char *outbuf, *outptr;
1240 g_return_val_if_fail (str != NULL, NULL);
1243 for (i = 0; str[i] != 0; i++) {
1244 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1245 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1246 "Illegal byte sequence encounted in the input.");
1260 for (i = 0; i < len && str[i] != 0; i++) {
1261 if ((n = g_unichar_to_utf8 (str[i], NULL)) < 0) {
1262 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1263 "Illegal byte sequence encounted in the input.");
1280 outptr = outbuf = g_malloc (outlen + 1);
1281 for (i = 0; i < len; i++)
1282 outptr += g_unichar_to_utf8 (str[i], outptr);
1286 *items_written = outlen;
1295 g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err)
1297 gunichar2 *outbuf, *outptr;
1302 g_return_val_if_fail (str != NULL, NULL);
1305 for (i = 0; str[i] != 0; i++) {
1306 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1307 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1308 "Illegal byte sequence encounted in the input.");
1322 for (i = 0; i < len && str[i] != 0; i++) {
1323 if ((n = g_unichar_to_utf16 (str[i], NULL)) < 0) {
1324 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1325 "Illegal byte sequence encounted in the input.");
1342 outptr = outbuf = g_malloc ((outlen + 1) * sizeof (gunichar2));
1343 for (i = 0; i < len; i++)
1344 outptr += g_unichar_to_utf16 (str[i], outptr);
1348 *items_written = outlen;