1 /* Character set conversion.
2 Copyright (C) 1999-2001, 2007, 2009-2010 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
31 # define uintptr_t unsigned long
37 /* UTF-{16,32}{BE,LE} converters taken from GNU libiconv 1.11. */
39 /* Return code if invalid. (xxx_mbtowc) */
41 /* Return code if no bytes were read. (xxx_mbtowc) */
42 # define RET_TOOFEW -2
44 /* Return code if invalid. (xxx_wctomb) */
46 /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
47 # define RET_TOOSMALL -2
53 /* Specification: RFC 2781 */
56 utf16be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
60 ucs4_t wc = (s[0] << 8) + s[1];
61 if (wc >= 0xd800 && wc < 0xdc00)
65 ucs4_t wc2 = (s[2] << 8) + s[3];
66 if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
68 *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
72 else if (wc >= 0xdc00 && wc < 0xe000)
86 utf16be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
88 if (!(wc >= 0xd800 && wc < 0xe000))
94 r[0] = (unsigned char) (wc >> 8);
95 r[1] = (unsigned char) wc;
101 else if (wc < 0x110000)
105 ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
106 ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
107 r[0] = (unsigned char) (wc1 >> 8);
108 r[1] = (unsigned char) wc1;
109 r[2] = (unsigned char) (wc2 >> 8);
110 r[3] = (unsigned char) wc2;
124 /* Specification: RFC 2781 */
127 utf16le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
131 ucs4_t wc = s[0] + (s[1] << 8);
132 if (wc >= 0xd800 && wc < 0xdc00)
136 ucs4_t wc2 = s[2] + (s[3] << 8);
137 if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
139 *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
143 else if (wc >= 0xdc00 && wc < 0xe000)
157 utf16le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
159 if (!(wc >= 0xd800 && wc < 0xe000))
165 r[0] = (unsigned char) wc;
166 r[1] = (unsigned char) (wc >> 8);
172 else if (wc < 0x110000)
176 ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
177 ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
178 r[0] = (unsigned char) wc1;
179 r[1] = (unsigned char) (wc1 >> 8);
180 r[2] = (unsigned char) wc2;
181 r[3] = (unsigned char) (wc2 >> 8);
195 /* Specification: Unicode 3.1 Standard Annex #19 */
198 utf32be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
202 ucs4_t wc = (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3];
203 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
215 utf32be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
217 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
222 r[1] = (unsigned char) (wc >> 16);
223 r[2] = (unsigned char) (wc >> 8);
224 r[3] = (unsigned char) wc;
237 /* Specification: Unicode 3.1 Standard Annex #19 */
240 utf32le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
244 ucs4_t wc = s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24);
245 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
257 utf32le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
259 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
263 r[0] = (unsigned char) wc;
264 r[1] = (unsigned char) (wc >> 8);
265 r[2] = (unsigned char) (wc >> 16);
278 rpl_iconv (iconv_t cd,
279 ICONV_CONST char **inbuf, size_t *inbytesleft,
280 char **outbuf, size_t *outbytesleft)
283 #if REPLACE_ICONV_UTF
284 switch ((uintptr_t) cd)
287 int (*xxx_wctomb) (unsigned char *, ucs4_t, size_t);
289 case (uintptr_t) _ICONV_UTF8_UTF16BE:
290 xxx_wctomb = utf16be_wctomb;
292 case (uintptr_t) _ICONV_UTF8_UTF16LE:
293 xxx_wctomb = utf16le_wctomb;
295 case (uintptr_t) _ICONV_UTF8_UTF32BE:
296 xxx_wctomb = utf32be_wctomb;
298 case (uintptr_t) _ICONV_UTF8_UTF32LE:
299 xxx_wctomb = utf32le_wctomb;
303 if (inbuf == NULL || *inbuf == NULL)
306 ICONV_CONST char *inptr = *inbuf;
307 size_t inleft = *inbytesleft;
308 char *outptr = *outbuf;
309 size_t outleft = *outbytesleft;
314 int m = u8_mbtoucr (&uc, (const uint8_t *) inptr, inleft);
333 int n = xxx_wctomb ((uint8_t *) outptr, uc, outleft);
342 if (n == RET_TOOSMALL)
360 *inbytesleft = inleft;
362 *outbytesleft = outleft;
368 int (*xxx_mbtowc) (ucs4_t *, const unsigned char *, size_t);
370 case (uintptr_t) _ICONV_UTF16BE_UTF8:
371 xxx_mbtowc = utf16be_mbtowc;
373 case (uintptr_t) _ICONV_UTF16LE_UTF8:
374 xxx_mbtowc = utf16le_mbtowc;
376 case (uintptr_t) _ICONV_UTF32BE_UTF8:
377 xxx_mbtowc = utf32be_mbtowc;
379 case (uintptr_t) _ICONV_UTF32LE_UTF8:
380 xxx_mbtowc = utf32le_mbtowc;
384 if (inbuf == NULL || *inbuf == NULL)
387 ICONV_CONST char *inptr = *inbuf;
388 size_t inleft = *inbytesleft;
389 char *outptr = *outbuf;
390 size_t outleft = *outbytesleft;
395 int m = xxx_mbtowc (&uc, (const uint8_t *) inptr, inleft);
414 int n = u8_uctomb ((uint8_t *) outptr, uc, outleft);
441 *inbytesleft = inleft;
443 *outbytesleft = outleft;
449 return iconv (cd, inbuf, inbytesleft, outbuf, outbytesleft);