1 | /* |
---|
2 | * Copyright (c) 2003-2004, Artem B. Bityuckiy |
---|
3 | * Copyright (c) 1999,2000, Konstantin Chuguev. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
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. |
---|
13 | * |
---|
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 |
---|
24 | * SUCH DAMAGE. |
---|
25 | */ |
---|
26 | #include "cesbi.h" |
---|
27 | |
---|
28 | #if defined (ICONV_TO_UCS_CES_UCS_2) \ |
---|
29 | || defined (ICONV_FROM_UCS_CES_UCS_2) |
---|
30 | |
---|
31 | #include <_ansi.h> |
---|
32 | #include <reent.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <string.h> |
---|
35 | #include <sys/types.h> |
---|
36 | #include "../lib/local.h" |
---|
37 | #include "../lib/ucsconv.h" |
---|
38 | #include "../lib/endian.h" |
---|
39 | |
---|
40 | /* |
---|
41 | * BOM isn't supported. UCS-2 is Big Endian. Bad codes are rejected. |
---|
42 | * Bad codes: 0xFFFF, 0xFFFE, 0xD800-0xDFFF. |
---|
43 | */ |
---|
44 | |
---|
45 | #define UCS_2_BIG_ENDIAN 0 |
---|
46 | #define UCS_2_LITTLE_ENDIAN 1 |
---|
47 | |
---|
48 | #define UCS_2 "ucs_2" |
---|
49 | #define UCS_2BE "ucs_2be" |
---|
50 | #define UCS_2LE "ucs_2le" |
---|
51 | |
---|
52 | static void * |
---|
53 | ucs_2_init (struct _reent *rptr, |
---|
54 | const char *encoding) |
---|
55 | { |
---|
56 | int *data; |
---|
57 | |
---|
58 | if ((data = (int *) _malloc_r(rptr, sizeof (int))) == NULL) |
---|
59 | return (void *)NULL; |
---|
60 | |
---|
61 | if (strcmp (encoding, UCS_2LE) == 0) |
---|
62 | *data = UCS_2_LITTLE_ENDIAN; |
---|
63 | else |
---|
64 | *data = UCS_2_BIG_ENDIAN; |
---|
65 | |
---|
66 | return (void *)data; |
---|
67 | } |
---|
68 | |
---|
69 | static size_t |
---|
70 | ucs_2_close (struct _reent *rptr, |
---|
71 | void *data) |
---|
72 | { |
---|
73 | _free_r (rptr, data); |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | #if defined (ICONV_FROM_UCS_CES_UCS_2) |
---|
78 | static size_t |
---|
79 | ucs_2_convert_from_ucs (void *data, |
---|
80 | ucs4_t in, |
---|
81 | unsigned char **outbuf, |
---|
82 | size_t *outbytesleft) |
---|
83 | { |
---|
84 | if ((in >= 0x0000D800 && in <= 0x0000DFFF) /* Surrogate character */ |
---|
85 | || in >= 0x0000FFFE) |
---|
86 | return (size_t)ICONV_CES_INVALID_CHARACTER; |
---|
87 | |
---|
88 | if (*outbytesleft < sizeof (ucs2_t)) |
---|
89 | return (size_t)ICONV_CES_NOSPACE; |
---|
90 | |
---|
91 | if (*((int *)data) == UCS_2_BIG_ENDIAN) |
---|
92 | *((ucs2_t *)(*outbuf)) = ICONV_HTOBES ((ucs2_t)in); |
---|
93 | else |
---|
94 | *((ucs2_t *)(*outbuf)) = ICONV_HTOLES ((ucs2_t)in); |
---|
95 | |
---|
96 | *outbuf += sizeof (ucs2_t); |
---|
97 | *outbytesleft -= sizeof (ucs2_t); |
---|
98 | |
---|
99 | return sizeof (ucs2_t); |
---|
100 | } |
---|
101 | #endif /* ICONV_FROM_UCS_CES_UCS_2 */ |
---|
102 | |
---|
103 | #if defined (ICONV_TO_UCS_CES_UCS_2) |
---|
104 | static ucs4_t |
---|
105 | ucs_2_convert_to_ucs (void *data, |
---|
106 | const unsigned char **inbuf, |
---|
107 | size_t *inbytesleft) |
---|
108 | { |
---|
109 | ucs4_t res; |
---|
110 | |
---|
111 | if (*inbytesleft < sizeof (ucs2_t)) |
---|
112 | return (ucs4_t)ICONV_CES_BAD_SEQUENCE; |
---|
113 | |
---|
114 | if (*((int *)data) == UCS_2_BIG_ENDIAN) |
---|
115 | res = (ucs4_t)ICONV_BETOHS (*((ucs2_t *)(*inbuf))); |
---|
116 | else |
---|
117 | res = (ucs4_t)ICONV_LETOHS (*((ucs2_t *)(*inbuf))); |
---|
118 | |
---|
119 | if ((res >= 0x0000D800 && res <= 0x0000DFFF) /* Surrogate character */ |
---|
120 | || res >= 0x0000FFFE) |
---|
121 | return (ucs4_t)ICONV_CES_INVALID_CHARACTER; |
---|
122 | |
---|
123 | *inbytesleft -= sizeof (ucs2_t); |
---|
124 | *inbuf += sizeof (ucs2_t); |
---|
125 | |
---|
126 | return res; |
---|
127 | } |
---|
128 | #endif /* ICONV_TO_UCS_CES_UCS_2 */ |
---|
129 | |
---|
130 | static int |
---|
131 | ucs_2_get_mb_cur_max (void *data) |
---|
132 | { |
---|
133 | return 2; |
---|
134 | } |
---|
135 | |
---|
136 | #if defined (ICONV_TO_UCS_CES_UCS_2) |
---|
137 | const iconv_to_ucs_ces_handlers_t |
---|
138 | _iconv_to_ucs_ces_handlers_ucs_2 = |
---|
139 | { |
---|
140 | ucs_2_init, |
---|
141 | ucs_2_close, |
---|
142 | ucs_2_get_mb_cur_max, |
---|
143 | NULL, |
---|
144 | NULL, |
---|
145 | NULL, |
---|
146 | ucs_2_convert_to_ucs |
---|
147 | }; |
---|
148 | #endif |
---|
149 | |
---|
150 | #if defined (ICONV_FROM_UCS_CES_UCS_2) |
---|
151 | const iconv_from_ucs_ces_handlers_t |
---|
152 | _iconv_from_ucs_ces_handlers_ucs_2 = |
---|
153 | { |
---|
154 | ucs_2_init, |
---|
155 | ucs_2_close, |
---|
156 | ucs_2_get_mb_cur_max, |
---|
157 | NULL, |
---|
158 | NULL, |
---|
159 | NULL, |
---|
160 | ucs_2_convert_from_ucs |
---|
161 | }; |
---|
162 | #endif |
---|
163 | |
---|
164 | #endif /* ICONV_TO_UCS_CES_UCS_2 || ICONV_FROM_UCS_CES_UCS_2 */ |
---|
165 | |
---|