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
28 #include "../lib/deps.h"
30 #ifdef _ICONV_CONVERTER_ISO_10646_UCS_2
31 #include "../lib/local.h"
33 #define out_char(ptr, ch) \
35 *(*(ptr))++ = ((ch) >> 8) & 0xFF; \
36 *(*(ptr))++ = (ch) & 0xFF; \
40 _DEFUN(convert_from_ucs, (ces, in, outbuf, outbytesleft),
41 struct iconv_ces *ces _AND
43 unsigned char **outbuf _AND
49 if (in == UCS_CHAR_NONE)
50 return 1; /* No state reinitialization for table charsets */
51 if (iconv_char32bit(in))
52 return -1; /* No such character in UCS-2 */
53 bytes = *(state = (int *)(ces->data)) ? 2 : 4;
54 if (*outbytesleft < bytes)
55 return 0; /* No space in the output buffer */
57 out_char(outbuf, UCS_CHAR_ZERO_WIDTH_NBSP);
61 *outbytesleft -= bytes;
66 _DEFUN(msb, (buf), _CONST unsigned char *buf)
68 return (buf[0] << 8) | buf[1];
72 _DEFUN(convert_to_ucs, (ces, inbuf, inbytesleft),
73 struct iconv_ces *ces _AND
74 _CONST unsigned char **inbuf _AND
81 return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */
82 state = (int *)(ces->data);
83 res = msb((_CONST unsigned char*)*inbuf);
85 case UCS_CHAR_ZERO_WIDTH_NBSP:
89 case UCS_CHAR_INVALID:
98 return UCS_CHAR_NONE; /* Not enough bytes in the input buffer */
100 res = msb(*inbuf += 2);
103 res = (unsigned char)(*(*inbuf) ++);
104 res |= (unsigned char)(*(*inbuf) ++) << 8;
111 ICONV_CES_STATEFUL_MODULE_DECL(iso_10646_ucs_2);
113 #endif /* #ifdef _ICONV_CONVERTER_ISO_10646_UCS_2 */