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 (c) 1996 by Sun Microsystems, Inc.
23 */
24
25
26 #include <errno.h>
27 #include <widec.h>
28 #include "common_def.h"
29 #include "common_thai.h"
30 #include "utf_euc_api.h"
31
32 #include "common_defs.h"
33 /**** _ I C V _ O P E N ****/
34
_icv_open()35 void* _icv_open()
36 {
37 return((void*)MAGIC_NUMBER);
38 } /* end of int _icv_open(). */
39
40
41 /**** _ I C V _ C L O S E ****/
42
_icv_close(int * cd)43 void _icv_close(int* cd)
44 {
45 if (!cd || cd != (int*)MAGIC_NUMBER)
46 errno = EBADF;
47 } /* end of void _icv_close(int*). */
48
49
50 /**** _ I C V _ I C O N V ****/
51
_icv_iconv(int * cd,char ** inbuf,size_t * inbufleft,char ** outbuf,size_t * outbufleft)52 size_t _icv_iconv(int* cd, char** inbuf, size_t* inbufleft,
53 char** outbuf, size_t* outbufleft)
54 {
55 size_t ret_val = 0;
56 unsigned char* ib;
57 unsigned char* ob;
58 unsigned char* ibtail;
59 unsigned char* obtail;
60
61 if (!cd || cd != (int*)MAGIC_NUMBER)
62 {
63 errno = EBADF;
64 return((size_t)-1);
65 }
66
67 if (!inbuf || !(*inbuf))
68 return((size_t)0);
69
70 ib = (unsigned char*)*inbuf;
71 ob = (unsigned char*)*outbuf;
72 ibtail = ib + *inbufleft;
73 obtail = ob + *outbufleft;
74
75 while (ib < ibtail)
76 {
77 if (!(*ib & 0x80)) /* 7 bits */
78 {
79 if (ob >= obtail)
80 {
81 errno = E2BIG;
82 ret_val = (size_t)-1;
83 break;
84 }
85 *ob++ = *ib++;
86 }
87 else if ((*ib & 0xF0) == 0xE0) /* 16 bits */
88 {
89 hcode_type utf8_code, euc_code;
90
91 if ((ibtail - ib) < 3)
92 {
93 errno = EINVAL;
94 ret_val = (size_t)-1;
95 break;
96 }
97
98 /* UNICODE 3.2 :eliminate non-secure UTF-8 sequence */
99 if (*(ib+1)<valid_min_2nd_byte[*ib] || *(ib+1)>valid_max_2nd_byte[*ib])
100 {
101 errno = EILSEQ;
102 ret_val = (size_t)-1;
103 break;
104 }
105 else if ((*(ib+2)&0xC0)^0x80)
106 {
107 errno = EILSEQ;
108 ret_val = (size_t)-1;
109 break;
110 }
111 else if (*ib==0xEF && *(ib+1)==0xBF && (*(ib+2)==0xBF || *(ib+2)==0xBE))
112 { /*U+FFFE , U+FFFF*/
113 errno = EILSEQ;
114 ret_val = (size_t)-1;
115 break;
116 }
117
118 utf8_code.byte.byte1 = 0;
119 utf8_code.byte.byte2 = *ib;
120 utf8_code.byte.byte3 = *(ib + 1);
121 utf8_code.byte.byte4 = *(ib + 2);
122
123 euc_code = _utf8_to_eucTH(utf8_code);
124
125 if (euc_code.code != 0) {
126 /* If find something -> EUC code */
127 *ob++ = euc_code.byte.byte4;
128 }
129 else
130 {
131 /* Let's assume the code is non-identical. */
132 if ((obtail - ob) < 1)
133 {
134 errno = E2BIG;
135 ret_val = (size_t)-1;
136 break;
137 }
138 *ob++ = NON_IDENTICAL;
139 ret_val += 1;
140 }
141 ib += 3;
142 }
143 else /* 11, 21, 26 & 31 bits codes won't be able to convert. */
144 {
145 short int offset;
146
147 errno = 0;
148 offset = number_of_bytes_in_utf8_char[*ib];
149 if (offset == ICV_TYPE_ILLEGAL_CHAR )
150 {
151 errno = EILSEQ;
152 }
153 else if (ibtail-ib < offset)
154 {
155 errno = EINVAL;
156 }
157 else if (*(ib+1)<valid_min_2nd_byte[*ib] || *(ib+1)>valid_max_2nd_byte[*ib])
158 {
159 errno = EILSEQ;
160 }
161 else /* check the remaining byte [0x80, 0xBF] */
162 {
163 int i;
164 for(i=2;i<offset;i++)
165 {
166 if ((*(ib+i) & 0xC0) ^ 0x80)
167 {
168 errno = EILSEQ;
169 break;
170 }
171 }
172 }
173 if(errno)
174 {
175 ret_val = (size_t)-1;
176 break;
177 }
178
179 ib += offset;
180
181 /* Let's assume the code is non-identical. */
182 offset = (offset > 2) ? 2 : 1;
183 if ((obtail - ob) < offset)
184 {
185 errno = E2BIG;
186 ret_val = (size_t)-1;
187 break;
188 }
189 *ob++ = NON_IDENTICAL;
190 if (offset > 1)
191 *ob++ = NON_IDENTICAL;
192 ret_val += offset;
193 }
194 }
195
196 *inbuf = (char*)ib;
197 *inbufleft = ibtail - ib;
198 *outbuf = (char*)ob;
199 *outbufleft = obtail - ob;
200
201 return(ret_val);
202 } /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*).*/
203