2 * Copyright (c) 1999,2000
3 * 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
26 * iconv (Charset Conversion Library) v2.0
30 #include "../lib/deps.h"
32 #ifdef ICONV_CONVERTER_UTF_16
36 #include "../lib/local.h"
39 _DEFUN(convert_from_ucs, (ces, in, outbuf, outbytesleft),
40 struct iconv_ces *ces _AND
42 unsigned char **outbuf _AND
49 if (in == UCS_CHAR_NONE)
50 return 1; /* No state reinitialization for table charsets */
53 bytes = *(state = (int *)(ces->data)) ? 2 : 4;
56 if (*outbytesleft < bytes)
57 return 0; /* No space in the output buffer */
65 *cp++ = ((in -= 0x10000) >> 18) | 0xD8;
66 *cp++ = (in >> 10) & 0xFF;
67 *cp++ = ((in >> 8) & 3) | 0xDC;
69 *cp++ = (in >> 8) & 0xFF;
72 *outbytesleft -= bytes;
77 _DEFUN(msb, (buf), _CONST unsigned char *buf)
79 return (buf[0] << 8) | buf[1];
83 _DEFUN(convert_to_ucs, (ces, inbuf, inbytesleft),
84 struct iconv_ces *ces _AND
85 _CONST unsigned char **inbuf _AND
92 return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */
93 state = (int *)(ces->data);
96 case UCS_CHAR_ZERO_WIDTH_NBSP:
101 case UCS_CHAR_INVALID:
112 if (*inbytesleft < 4)
113 return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */
115 res = msb((*inbuf) += 2);
117 if (*state == 2) { /* LSB order */
118 res = (*(*inbuf) ++);
119 res |= (*(*inbuf) ++) << 8;
123 if ((res & 0xFC00) != 0xD800) /* Non-surrogate character */
125 if (*inbytesleft < 2)
126 return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */
129 res2 |= (*inbuf)[1] << 8;
132 if ((res2 & 0xFC00) != 0xDC00) /* Broken surrogate pair */
136 return (((res & 0x3FF) << 10) | (res2 & 0x3FF)) + 0x10000;
139 ICONV_CES_STATEFUL_MODULE_DECL(utf_16);
141 #endif /* #ifdef ICONV_CONVERTER_UTF_16 */
143 #endif /* #ifdef ENABLE_ICONV */