1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 *
25 * This particular file is to cover conversions from UCS-2, UCS-2BE, UCS-2LE,
26 * UCS-4, UCS-4BE, UCS-4LE, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE,
27 * and UTF-32LE to various single byte codesets.
28 */
29
30
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/isa_defs.h>
35 #include "ucs_to_sb.h"
36
37
38 void *
_icv_open()39 _icv_open()
40 {
41 ucs_state_t *cd = (ucs_state_t *)calloc(1, sizeof(ucs_state_t));
42
43 if (cd == (ucs_state_t *)NULL) {
44 errno = ENOMEM;
45 return((void *)-1);
46 }
47
48 #if defined(UTF_16BE) || defined(UCS_2BE) || defined(UCS_4BE) || \
49 defined(UTF_32BE)
50 cd->little_endian = false;
51 cd->bom_written = true;
52 #elif defined(UTF_16LE) || defined(UCS_2LE) || defined(UCS_4LE) || \
53 defined(UTF_32LE)
54 cd->little_endian = true;
55 cd->bom_written = true;
56 #elif defined(_LITTLE_ENDIAN)
57 cd->little_endian = true;
58 #endif
59
60 return((void *)cd);
61 }
62
63
64 void
_icv_close(ucs_state_t * cd)65 _icv_close(ucs_state_t *cd)
66 {
67 if (! cd)
68 errno = EBADF;
69 else
70 free((void *)cd);
71 }
72
73
74 size_t
_icv_iconv(ucs_state_t * cd,char ** inbuf,size_t * inbufleft,char ** outbuf,size_t * outbufleft)75 _icv_iconv(ucs_state_t *cd, char **inbuf, size_t *inbufleft, char **outbuf,
76 size_t *outbufleft)
77 {
78 size_t ret_val = 0;
79 unsigned char *ib;
80 unsigned char *ob;
81 unsigned char *ibtail;
82 unsigned char *obtail;
83 uint_t u4;
84 uint_t u4_2;
85 register int i;
86 register int l, h;
87
88 if (! cd) {
89 errno = EBADF;
90 return((size_t)-1);
91 }
92
93 if (!inbuf || !(*inbuf)) {
94 #if defined(UCS_2) || defined(UCS_4) || defined(UTF_16) || defined(UTF_32)
95 cd->bom_written = false;
96 #endif
97 return((size_t)0);
98 }
99
100 ib = (unsigned char *)*inbuf;
101 ob = (unsigned char *)*outbuf;
102 ibtail = ib + *inbufleft;
103 obtail = ob + *outbufleft;
104
105 #if defined(UCS_2) || defined(UCS_4) || defined(UTF_16) || defined(UTF_32)
106 if (! cd->bom_written) {
107 if ((ibtail - ib) < ICV_FETCH_UCS_SIZE) {
108 errno = EINVAL;
109 ret_val = (size_t)-1;
110 goto need_more_input_err;
111 }
112
113 for (u4 = 0, i = 0; i < ICV_FETCH_UCS_SIZE; i++)
114 u4 = (u4 << 8) | ((uint_t)(*(ib + i)));
115
116 if (u4 == ICV_BOM_IN_BIG_ENDIAN) {
117 ib += ICV_FETCH_UCS_SIZE;
118 cd->little_endian = false;
119 } else if (u4 == ICV_BOM_IN_LITTLE_ENDIAN) {
120 ib += ICV_FETCH_UCS_SIZE;
121 cd->little_endian = true;
122 }
123 }
124 cd->bom_written = true;
125 #endif
126
127 while (ib < ibtail) {
128 if ((ibtail - ib) < ICV_FETCH_UCS_SIZE) {
129 errno = EINVAL;
130 ret_val = (size_t)-1;
131 break;
132 }
133
134 u4 = u4_2 = 0;
135 if (cd->little_endian) {
136 for (i = ICV_FETCH_UCS_SIZE - 1; i >= 0; i--)
137 u4 = (u4 << 8) | ((uint_t)(*(ib + i)));
138 } else {
139 for (i = 0; i < ICV_FETCH_UCS_SIZE; i++)
140 u4 = (u4 << 8) | ((uint_t)(*(ib + i)));
141 }
142
143 #if defined(UTF_16) || defined(UTF_16BE) || defined(UTF_16LE)
144 if ((u4 >= 0x00dc00 && u4 <= 0x00dfff) || u4 >= 0x00fffe) {
145 errno = EILSEQ;
146 ret_val = (size_t)-1;
147 break;
148 }
149
150 if (u4 >= 0x00d800 && u4 <= 0x00dbff) {
151 if ((ibtail - ib) < ICV_FETCH_UCS_SIZE_TWO) {
152 errno = EINVAL;
153 ret_val = (size_t)-1;
154 break;
155 }
156
157 if (cd->little_endian) {
158 for (i = ICV_FETCH_UCS_SIZE_TWO - 1;
159 i >= ICV_FETCH_UCS_SIZE;
160 i--)
161 u4_2 = (u4_2<<8)|((uint_t)(*(ib + i)));
162 } else {
163 for (i = ICV_FETCH_UCS_SIZE;
164 i < ICV_FETCH_UCS_SIZE_TWO;
165 i++)
166 u4_2 = (u4_2<<8)|((uint_t)(*(ib + i)));
167 }
168
169 if (u4_2 < 0x00dc00 || u4_2 > 0x00dfff) {
170 errno = EILSEQ;
171 ret_val = (size_t)-1;
172 break;
173 }
174
175 u4 = ((((u4 - 0x00d800) * 0x400) +
176 (u4_2 - 0x00dc00)) & 0x0fffff) + 0x010000;
177 }
178 #elif defined(UCS_2) || defined(UCS_2BE) || defined(UCS_2LE)
179 if (u4 >= 0x00fffe || (u4 >= 0x00d800 && u4 <= 0x00dfff)) {
180 errno = EILSEQ;
181 ret_val = (size_t)-1;
182 break;
183 }
184 #elif defined(UTF_32) || defined(UTF_32BE) || defined(UTF_32LE)
185 if (u4 == 0x00fffe || u4 == 0x00ffff || u4 > 0x10ffff ||
186 (u4 >= 0x00d800 && u4 <= 0x00dfff)) {
187 errno = EILSEQ;
188 ret_val = (size_t)-1;
189 break;
190 }
191 #elif defined(UCS_4) || defined(UCS_4BE) || defined(UCS_4LE)
192 if (u4 == 0x00fffe || u4 == 0x00ffff || u4 > 0x7fffffff ||
193 (u4 >= 0x00d800 && u4 <= 0x00dfff)) {
194 errno = EILSEQ;
195 ret_val = (size_t)-1;
196 break;
197 }
198 #else
199 #error "Fatal: one of the UCS macros need to be defined."
200 #endif
201
202 if (ob >= obtail) {
203 errno = E2BIG;
204 ret_val = (size_t)-1;
205 break;
206 }
207
208 if (u4 > 0x7f) {
209 i = l = 0;
210 h = (sizeof(u4_sb_tbl) /
211 sizeof(to_sb_table_component_t)) - 1;
212 while (l <= h) {
213 i = (l + h) / 2;
214 if (u4_sb_tbl[i].u8 == u4)
215 break;
216 else if (u4_sb_tbl[i].u8 < u4)
217 l = i + 1;
218 else
219 h = i - 1;
220 }
221
222 /*
223 * We just assume that either we found it or it is
224 * a non-identical character that we need to
225 * provide a replacement character.
226 */
227 if (u4_sb_tbl[i].u8 == u4) {
228 u4 = u4_sb_tbl[i].sb;
229 } else {
230 u4 = ICV_CHAR_ASCII_REPLACEMENT;
231 ret_val++;
232 }
233 }
234
235 *ob++ = (unsigned char)u4;
236 ib += (u4_2) ? ICV_FETCH_UCS_SIZE_TWO : ICV_FETCH_UCS_SIZE;
237 }
238 #if defined(UCS_2) || defined(UCS_4) || defined(UTF_16) || defined(UTF_32)
239 need_more_input_err:
240 #endif
241 *inbuf = (char *)ib;
242 *inbufleft = ibtail - ib;
243 *outbuf = (char *)ob;
244 *outbufleft = obtail - ob;
245
246 return(ret_val);
247 }
248