1 /* Character set conversion.
2 Copyright (C) 1999-2001, 2007, 2009-2012 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 3, 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, see <http://www.gnu.org/licenses/>. */
30 # define uintptr_t unsigned long
36 /* UTF-{16,32}{BE,LE} converters taken from GNU libiconv 1.11. */
38 /* Return code if invalid. (xxx_mbtowc) */
40 /* Return code if no bytes were read. (xxx_mbtowc) */
41 # define RET_TOOFEW -2
43 /* Return code if invalid. (xxx_wctomb) */
45 /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
46 # define RET_TOOSMALL -2
52 /* Specification: RFC 2781 */
55 utf16be_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
59 ucs4_t wc
= (s
[0] << 8) + s
[1];
60 if (wc
>= 0xd800 && wc
< 0xdc00)
64 ucs4_t wc2
= (s
[2] << 8) + s
[3];
65 if (!(wc2
>= 0xdc00 && wc2
< 0xe000))
67 *pwc
= 0x10000 + ((wc
- 0xd800) << 10) + (wc2
- 0xdc00);
71 else if (wc
>= 0xdc00 && wc
< 0xe000)
85 utf16be_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
87 if (!(wc
>= 0xd800 && wc
< 0xe000))
93 r
[0] = (unsigned char) (wc
>> 8);
94 r
[1] = (unsigned char) wc
;
100 else if (wc
< 0x110000)
104 ucs4_t wc1
= 0xd800 + ((wc
- 0x10000) >> 10);
105 ucs4_t wc2
= 0xdc00 + ((wc
- 0x10000) & 0x3ff);
106 r
[0] = (unsigned char) (wc1
>> 8);
107 r
[1] = (unsigned char) wc1
;
108 r
[2] = (unsigned char) (wc2
>> 8);
109 r
[3] = (unsigned char) wc2
;
123 /* Specification: RFC 2781 */
126 utf16le_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
130 ucs4_t wc
= s
[0] + (s
[1] << 8);
131 if (wc
>= 0xd800 && wc
< 0xdc00)
135 ucs4_t wc2
= s
[2] + (s
[3] << 8);
136 if (!(wc2
>= 0xdc00 && wc2
< 0xe000))
138 *pwc
= 0x10000 + ((wc
- 0xd800) << 10) + (wc2
- 0xdc00);
142 else if (wc
>= 0xdc00 && wc
< 0xe000)
156 utf16le_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
158 if (!(wc
>= 0xd800 && wc
< 0xe000))
164 r
[0] = (unsigned char) wc
;
165 r
[1] = (unsigned char) (wc
>> 8);
171 else if (wc
< 0x110000)
175 ucs4_t wc1
= 0xd800 + ((wc
- 0x10000) >> 10);
176 ucs4_t wc2
= 0xdc00 + ((wc
- 0x10000) & 0x3ff);
177 r
[0] = (unsigned char) wc1
;
178 r
[1] = (unsigned char) (wc1
>> 8);
179 r
[2] = (unsigned char) wc2
;
180 r
[3] = (unsigned char) (wc2
>> 8);
194 /* Specification: Unicode 3.1 Standard Annex #19 */
197 utf32be_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
201 ucs4_t wc
= (s
[0] << 24) + (s
[1] << 16) + (s
[2] << 8) + s
[3];
202 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
214 utf32be_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
216 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
221 r
[1] = (unsigned char) (wc
>> 16);
222 r
[2] = (unsigned char) (wc
>> 8);
223 r
[3] = (unsigned char) wc
;
236 /* Specification: Unicode 3.1 Standard Annex #19 */
239 utf32le_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
243 ucs4_t wc
= s
[0] + (s
[1] << 8) + (s
[2] << 16) + (s
[3] << 24);
244 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
256 utf32le_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
258 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
262 r
[0] = (unsigned char) wc
;
263 r
[1] = (unsigned char) (wc
>> 8);
264 r
[2] = (unsigned char) (wc
>> 16);
277 rpl_iconv (iconv_t cd
,
278 ICONV_CONST
char **inbuf
, size_t *inbytesleft
,
279 char **outbuf
, size_t *outbytesleft
)
282 #if REPLACE_ICONV_UTF
283 switch ((uintptr_t) cd
)
286 int (*xxx_wctomb
) (unsigned char *, ucs4_t
, size_t);
288 case (uintptr_t) _ICONV_UTF8_UTF16BE
:
289 xxx_wctomb
= utf16be_wctomb
;
291 case (uintptr_t) _ICONV_UTF8_UTF16LE
:
292 xxx_wctomb
= utf16le_wctomb
;
294 case (uintptr_t) _ICONV_UTF8_UTF32BE
:
295 xxx_wctomb
= utf32be_wctomb
;
297 case (uintptr_t) _ICONV_UTF8_UTF32LE
:
298 xxx_wctomb
= utf32le_wctomb
;
302 if (inbuf
== NULL
|| *inbuf
== NULL
)
305 ICONV_CONST
char *inptr
= *inbuf
;
306 size_t inleft
= *inbytesleft
;
307 char *outptr
= *outbuf
;
308 size_t outleft
= *outbytesleft
;
313 int m
= u8_mbtoucr (&uc
, (const uint8_t *) inptr
, inleft
);
332 int n
= xxx_wctomb ((uint8_t *) outptr
, uc
, outleft
);
341 if (n
== RET_TOOSMALL
)
359 *inbytesleft
= inleft
;
361 *outbytesleft
= outleft
;
367 int (*xxx_mbtowc
) (ucs4_t
*, const unsigned char *, size_t);
369 case (uintptr_t) _ICONV_UTF16BE_UTF8
:
370 xxx_mbtowc
= utf16be_mbtowc
;
372 case (uintptr_t) _ICONV_UTF16LE_UTF8
:
373 xxx_mbtowc
= utf16le_mbtowc
;
375 case (uintptr_t) _ICONV_UTF32BE_UTF8
:
376 xxx_mbtowc
= utf32be_mbtowc
;
378 case (uintptr_t) _ICONV_UTF32LE_UTF8
:
379 xxx_mbtowc
= utf32le_mbtowc
;
383 if (inbuf
== NULL
|| *inbuf
== NULL
)
386 ICONV_CONST
char *inptr
= *inbuf
;
387 size_t inleft
= *inbytesleft
;
388 char *outptr
= *outbuf
;
389 size_t outleft
= *outbytesleft
;
394 int m
= xxx_mbtowc (&uc
, (const uint8_t *) inptr
, inleft
);
413 int n
= u8_uctomb ((uint8_t *) outptr
, uc
, outleft
);
440 *inbytesleft
= inleft
;
442 *outbytesleft
= outleft
;
448 return iconv (cd
, inbuf
, inbytesleft
, outbuf
, outbytesleft
);