1 /* Character set conversion.
2 Copyright (C) 1999-2001, 2007, 2009-2020 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, see <https://www.gnu.org/licenses/>. */
33 /* UTF-{16,32}{BE,LE} converters taken from GNU libiconv 1.11. */
35 /* Return code if invalid. (xxx_mbtowc) */
37 /* Return code if no bytes were read. (xxx_mbtowc) */
38 # define RET_TOOFEW -2
40 /* Return code if invalid. (xxx_wctomb) */
42 /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
43 # define RET_TOOSMALL -2
49 /* Specification: RFC 2781 */
52 utf16be_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
56 ucs4_t wc
= (s
[0] << 8) + s
[1];
57 if (wc
>= 0xd800 && wc
< 0xdc00)
61 ucs4_t wc2
= (s
[2] << 8) + s
[3];
62 if (!(wc2
>= 0xdc00 && wc2
< 0xe000))
64 *pwc
= 0x10000 + ((wc
- 0xd800) << 10) + (wc2
- 0xdc00);
68 else if (wc
>= 0xdc00 && wc
< 0xe000)
82 utf16be_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
84 if (!(wc
>= 0xd800 && wc
< 0xe000))
90 r
[0] = (unsigned char) (wc
>> 8);
91 r
[1] = (unsigned char) wc
;
97 else if (wc
< 0x110000)
101 ucs4_t wc1
= 0xd800 + ((wc
- 0x10000) >> 10);
102 ucs4_t wc2
= 0xdc00 + ((wc
- 0x10000) & 0x3ff);
103 r
[0] = (unsigned char) (wc1
>> 8);
104 r
[1] = (unsigned char) wc1
;
105 r
[2] = (unsigned char) (wc2
>> 8);
106 r
[3] = (unsigned char) wc2
;
120 /* Specification: RFC 2781 */
123 utf16le_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
127 ucs4_t wc
= s
[0] + (s
[1] << 8);
128 if (wc
>= 0xd800 && wc
< 0xdc00)
132 ucs4_t wc2
= s
[2] + (s
[3] << 8);
133 if (!(wc2
>= 0xdc00 && wc2
< 0xe000))
135 *pwc
= 0x10000 + ((wc
- 0xd800) << 10) + (wc2
- 0xdc00);
139 else if (wc
>= 0xdc00 && wc
< 0xe000)
153 utf16le_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
155 if (!(wc
>= 0xd800 && wc
< 0xe000))
161 r
[0] = (unsigned char) wc
;
162 r
[1] = (unsigned char) (wc
>> 8);
168 else if (wc
< 0x110000)
172 ucs4_t wc1
= 0xd800 + ((wc
- 0x10000) >> 10);
173 ucs4_t wc2
= 0xdc00 + ((wc
- 0x10000) & 0x3ff);
174 r
[0] = (unsigned char) wc1
;
175 r
[1] = (unsigned char) (wc1
>> 8);
176 r
[2] = (unsigned char) wc2
;
177 r
[3] = (unsigned char) (wc2
>> 8);
191 /* Specification: Unicode 3.1 Standard Annex #19 */
194 utf32be_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
198 ucs4_t wc
= (s
[0] << 24) + (s
[1] << 16) + (s
[2] << 8) + s
[3];
199 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
211 utf32be_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
213 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
218 r
[1] = (unsigned char) (wc
>> 16);
219 r
[2] = (unsigned char) (wc
>> 8);
220 r
[3] = (unsigned char) wc
;
233 /* Specification: Unicode 3.1 Standard Annex #19 */
236 utf32le_mbtowc (ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
240 ucs4_t wc
= s
[0] + (s
[1] << 8) + (s
[2] << 16) + (s
[3] << 24);
241 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
253 utf32le_wctomb (unsigned char *r
, ucs4_t wc
, size_t n
)
255 if (wc
< 0x110000 && !(wc
>= 0xd800 && wc
< 0xe000))
259 r
[0] = (unsigned char) wc
;
260 r
[1] = (unsigned char) (wc
>> 8);
261 r
[2] = (unsigned char) (wc
>> 16);
274 rpl_iconv (iconv_t cd
,
275 ICONV_CONST
char **inbuf
, size_t *inbytesleft
,
276 char **outbuf
, size_t *outbytesleft
)
279 #if REPLACE_ICONV_UTF
280 switch ((uintptr_t) cd
)
283 int (*xxx_wctomb
) (unsigned char *, ucs4_t
, size_t);
285 case (uintptr_t) _ICONV_UTF8_UTF16BE
:
286 xxx_wctomb
= utf16be_wctomb
;
288 case (uintptr_t) _ICONV_UTF8_UTF16LE
:
289 xxx_wctomb
= utf16le_wctomb
;
291 case (uintptr_t) _ICONV_UTF8_UTF32BE
:
292 xxx_wctomb
= utf32be_wctomb
;
294 case (uintptr_t) _ICONV_UTF8_UTF32LE
:
295 xxx_wctomb
= utf32le_wctomb
;
299 if (inbuf
== NULL
|| *inbuf
== NULL
)
302 ICONV_CONST
char *inptr
= *inbuf
;
303 size_t inleft
= *inbytesleft
;
304 char *outptr
= *outbuf
;
305 size_t outleft
= *outbytesleft
;
310 int m
= u8_mbtoucr (&uc
, (const uint8_t *) inptr
, inleft
);
329 int n
= xxx_wctomb ((uint8_t *) outptr
, uc
, outleft
);
338 if (n
== RET_TOOSMALL
)
356 *inbytesleft
= inleft
;
358 *outbytesleft
= outleft
;
364 int (*xxx_mbtowc
) (ucs4_t
*, const unsigned char *, size_t);
366 case (uintptr_t) _ICONV_UTF16BE_UTF8
:
367 xxx_mbtowc
= utf16be_mbtowc
;
369 case (uintptr_t) _ICONV_UTF16LE_UTF8
:
370 xxx_mbtowc
= utf16le_mbtowc
;
372 case (uintptr_t) _ICONV_UTF32BE_UTF8
:
373 xxx_mbtowc
= utf32be_mbtowc
;
375 case (uintptr_t) _ICONV_UTF32LE_UTF8
:
376 xxx_mbtowc
= utf32le_mbtowc
;
380 if (inbuf
== NULL
|| *inbuf
== NULL
)
383 ICONV_CONST
char *inptr
= *inbuf
;
384 size_t inleft
= *inbytesleft
;
385 char *outptr
= *outbuf
;
386 size_t outleft
= *outbytesleft
;
391 int m
= xxx_mbtowc (&uc
, (const uint8_t *) inptr
, inleft
);
410 int n
= u8_uctomb ((uint8_t *) outptr
, uc
, outleft
);
437 *inbytesleft
= inleft
;
439 *outbytesleft
= outleft
;
445 return iconv (cd
, inbuf
, inbytesleft
, outbuf
, outbytesleft
);