2 * Copyright (c) 2003-2004, Artem B. Bityuckiy
3 * Copyright (c) 1999,2000, Konstantin Chuguev. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #if defined (ICONV_TO_UCS_CES_UTF_16) \
29 || defined (ICONV_FROM_UCS_CES_UTF_16)
33 #include <sys/types.h>
37 #include "../lib/local.h"
38 #include "../lib/ucsconv.h"
39 #include "../lib/endian.h"
42 * On input UTF-16 converter interpret BOM and uses Big Endian byte order if BOM
43 * is absent. UTF-16 converter outputs in System Endian and adds correspondent
44 * BOM as first code. UTF-16LE and UTF-16BE converters ignore BOM on input and
48 #define UTF16_UNDEFINED 0x00
49 #define UTF16_BIG_ENDIAN 0x01
50 #define UTF16_LITTLE_ENDIAN 0x02
51 #define UTF16_SYSTEM_ENDIAN 0x04
52 #define UTF16_BOM_WRITTEN 0x08
54 #define UTF16_BOM 0xFEFF
56 #define UTF_16 "utf_16"
57 #define UTF_16BE "utf_16be"
58 #define UTF_16LE "utf_16le"
61 _DEFUN(utf_16_close, (rptr, data),
62 struct _reent *rptr _AND
69 #if defined (ICONV_FROM_UCS_CES_UTF_16)
71 _DEFUN(utf_16_init_from_ucs, (rptr, encoding),
72 struct _reent *rptr _AND
73 _CONST char *encoding)
77 if ((data = (int *)_malloc_r (rptr, sizeof (int))) == NULL)
78 return (_VOID_PTR)NULL;
80 if (strcmp (encoding, UTF_16LE) == 0)
81 *data = UTF16_LITTLE_ENDIAN;
82 else if (strcmp (encoding, UTF_16BE) == 0)
83 *data = UTF16_BIG_ENDIAN;
85 *data = UTF16_SYSTEM_ENDIAN;
87 return (_VOID_PTR)data;
91 _DEFUN(utf_16_convert_from_ucs, (data, in, outbuf, outbytesleft),
93 register ucs4_t in _AND
94 unsigned char **outbuf _AND
98 register size_t bytes;
101 if (in > 0x0010FFFF || (in >= 0x0000D800 && in <= 0x0000DFFF)
102 || in == 0x0000FFFF || in == 0x0000FFFE)
103 return (size_t)ICONV_CES_INVALID_CHARACTER;
106 bytes = (*state == UTF16_SYSTEM_ENDIAN) ? sizeof (ucs2_t) * 2
110 bytes += sizeof (ucs2_t);
112 if (*outbytesleft < bytes)
113 return (size_t)ICONV_CES_NOSPACE;
115 cp = (ucs2_t *)*outbuf;
117 if (*state == UTF16_SYSTEM_ENDIAN)
120 *state |= UTF16_BOM_WRITTEN;
127 case UTF16_LITTLE_ENDIAN:
128 *cp = ICONV_HTOLES ((ucs2_t)in);
130 case UTF16_BIG_ENDIAN:
131 *cp = ICONV_HTOBES ((ucs2_t)in);
133 case (UTF16_SYSTEM_ENDIAN | UTF16_BOM_WRITTEN):
142 /* Process surrogate pair */
144 w1 = ((ucs2_t)((in >> 10)) & 0x03FF) | 0xD800;
145 w2 = (ucs2_t)(in & 0x000003FF) | 0xDC00;
149 case UTF16_LITTLE_ENDIAN:
150 *cp++ = ICONV_HTOLES (w1);
151 *cp = ICONV_HTOLES (w2);
153 case UTF16_BIG_ENDIAN:
154 *cp++ = ICONV_HTOBES (w1);
155 *cp = ICONV_HTOBES (w2);
157 case (UTF16_SYSTEM_ENDIAN | UTF16_BOM_WRITTEN):
165 *outbytesleft -= bytes;
169 #endif /* ICONV_FROM_UCS_CES_UTF_16 */
171 #if defined (ICONV_TO_UCS_CES_UTF_16)
173 _DEFUN(utf_16_init_to_ucs, (rptr, encoding),
174 struct _reent *rptr _AND
175 _CONST char *encoding)
179 if ((data = (int *)_malloc_r (rptr, sizeof (int))) == NULL)
180 return (_VOID_PTR)NULL;
182 if (strcmp (encoding, UTF_16BE) == 0)
183 *data = UTF16_BIG_ENDIAN;
184 else if (strcmp (encoding, UTF_16LE) == 0)
185 *data = UTF16_LITTLE_ENDIAN;
187 *data = UTF16_UNDEFINED;
189 return (_VOID_PTR)data;
193 _DEFUN(utf_16_convert_to_ucs, (data, inbuf, inbytesleft),
195 _CONST unsigned char **inbuf _AND
203 int bytes = sizeof (ucs2_t);
205 if (*inbytesleft < bytes)
206 return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
209 cp = ((ucs2_t *)*inbuf);
211 if (*state == UTF16_UNDEFINED)
213 if (*cp == ICONV_HTOLES(UTF16_BOM))
214 *state = UTF16_LITTLE_ENDIAN;
216 *state = UTF16_BIG_ENDIAN;
218 if ( *cp == ICONV_HTOBES (UTF16_BOM)
219 || *cp == ICONV_HTOLES (UTF16_BOM))
221 if (*inbytesleft < (bytes += sizeof (ucs2_t)))
222 return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
227 if (*state == UTF16_LITTLE_ENDIAN)
228 w1 = ICONV_LETOHS (*cp);
230 w1 = ICONV_BETOHS (*cp);
232 if (w1 < 0xD800 || w1 > 0xDFFF)
234 if (w1 == 0xFFFF || w1 == 0xFFFE)
235 return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
240 /* Process surrogate pair */
241 if (*inbytesleft < (bytes += 2))
242 return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
245 /* Broken surrogate character */
246 return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
250 if (*state == UTF16_LITTLE_ENDIAN)
251 w2 = ICONV_LETOHS (*cp);
253 w2 = ICONV_BETOHS (*cp);
255 if (w2 < 0xDC00 || w2 > 0xDFFF)
256 /* Broken surrogate character */
257 return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
259 res = (ucs4_t)(w2 & 0x03FF) | ((ucs4_t)(w1 & 0x03FF) << 10);
264 *inbytesleft -= bytes;
268 #endif /* ICONV_TO_UCS_CES_UTF_16 */
271 _DEFUN(utf_16_get_mb_cur_max, (data),
277 #if defined (ICONV_TO_UCS_CES_UTF_16)
278 _CONST iconv_to_ucs_ces_handlers_t
279 _iconv_to_ucs_ces_handlers_utf_16 =
283 utf_16_get_mb_cur_max,
287 utf_16_convert_to_ucs
291 #if defined (ICONV_FROM_UCS_CES_UTF_16)
292 _CONST iconv_from_ucs_ces_handlers_t
293 _iconv_from_ucs_ces_handlers_utf_16 =
295 utf_16_init_from_ucs,
297 utf_16_get_mb_cur_max,
301 utf_16_convert_from_ucs
305 #endif /* ICONV_TO_UCS_CES_UTF_16 || ICONV_FROM_UCS_CES_UTF_16 */