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) 1999 by Sun Microsystems, Inc.
23 */
24
25
26 #include <errno.h>
27 #include <widec.h>
28 #include "common_def.h"
29 #include "common_han.h"
30 #include "uhang_utf_api.h"
31 #include "euc_utf_api.h"
32
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) /* Korean EUC doesn't have CS2 or CS3. */
78 {
79 hcode_type euc_code, utf_code;
80 int flag;
81
82 flag = 0;
83
84 if ((ibtail - ib) < 2)
85 {
86 errno = EINVAL;
87 ret_val = (size_t)-1;
88 break;
89 }
90
91
92 if(*ib<0xA1)
93 {
94 if((*(ib+1)>0x40 && *(ib+1)<0x5B) || (*(ib+1)>0x60 && *(ib+1)<0x7B) || (*(ib+1)>0x80 && *(ib+1)<0xFF))
95 flag = 0;
96 else
97 flag = 1;
98
99 }
100 else
101 {
102 if(*ib<0xC7)
103 {
104 if((*(ib+1)>0x40 && *(ib+1)<0x5B) || (*(ib+1)>0x60 && *(ib+1)<0x7B) || (*(ib+1)>0x80 && *(ib+1)<0xFF))
105 flag = 0;
106 else
107 flag = 1;
108 }
109 else
110 {
111 if(*(ib+1)>0xA0 && *(ib+1)<0xFF)
112 flag = 0;
113 else
114 flag = 1;
115 }
116
117 }
118
119 if(flag)
120 {
121 errno = EILSEQ;
122 ret_val = (size_t)-1;
123 break;
124 }
125
126 euc_code.code = 0;
127 euc_code.byte.byte3 = *ib;
128 euc_code.byte.byte4 = *(ib + 1);
129 utf_code = _unified_hangul_to_utf8(euc_code);
130
131 if (utf_code.code != 0)
132 {
133 if ((obtail - ob) < 3)
134 {
135 errno = E2BIG;
136 ret_val = (size_t)-1;
137 break;
138 }
139 /* UTF8 code from 2 bytes is always 3 bytes */
140 if(utf_code.byte.byte2)
141 *ob++ = (char)utf_code.byte.byte2;
142 *ob++ = (char)utf_code.byte.byte3;
143 *ob++ = (char)utf_code.byte.byte4;
144 }
145 else /* FAILED - this means input char doesn't belong to
146 * input codeset. */
147 {
148 errno = EILSEQ;
149 ret_val = (size_t)-1;
150 break;
151 }
152 ib += 2;
153
154 }
155 else /* CS0 */
156 {
157 if (ob >= obtail)
158 {
159 errno = E2BIG;
160 ret_val = (size_t)-1;
161 break;
162 }
163 *ob++ = *ib++;
164 }
165 }
166
167 *inbuf = (char*)ib;
168 *inbufleft = ibtail - ib;
169 *outbuf = (char*)ob;
170 *outbufleft = obtail - ob;
171
172 return(ret_val);
173 } /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*). */
174