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 /*
23 * Copyright (c) 1997, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sys/types.h>
31
32 #include "tab_lookup.h" /* table lookup data types */
33
34 #define MSB 0x80 /* most significant bit */
35 #define MBYTE 0x8e /* multi-byte (4 byte character) */
36 #define PMASK 0xa0 /* plane number mask */
37 #define ONEBYTE 0xff /* right most byte */
38
39 /* non-identified character */
40 #define UTF8_NON_ID_CHAR1 0xEF
41 #define UTF8_NON_ID_CHAR2 0xBF
42 #define UTF8_NON_ID_CHAR3 0xBD
43
44 enum _USTATE { C0, C1 };
45
46
47
48
49 /*
50 * Actual conversion; called from iconv()
51 * Input is UTF-8 data.
52 * first convert to UCS2
53 */
54 size_t
_icv_iconv(_icv_state * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)55 _icv_iconv(_icv_state *st, char **inbuf, size_t *inbytesleft,
56 char **outbuf, size_t *outbytesleft)
57 {
58 /*
59 * Actual conversion; called from iconv()
60 */
61
62 char c1, c2;
63 int n, unidx;
64 unsigned long ibm_code;
65
66 #ifdef DEBUG
67 fprintf(stderr, "========== iconv(): IBM --> UTF8 ==========\n");
68 #endif
69
70 if (st == NULL) {
71 errno = EBADF;
72 return ((size_t) -1);
73 }
74
75 if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */
76 st->ustate = C0;
77 st->_errno = 0;
78 return ((size_t) 0);
79 }
80
81 st->_errno = 0; /* reset internal errno */
82 errno = 0; /* reset external errno */
83
84 /* a state machine for interpreting UTF8 code */
85 while (*inbytesleft > 0 && *outbytesleft > 0) {
86 switch (st->ustate) {
87 case C0 :
88 st->keepc[0] = (**inbuf);
89 st->ustate = C1;
90 break;
91
92 case C1 :
93 st->keepc[1] = (**inbuf);
94 n = ibm_to_utf8(st, *outbuf, *outbytesleft);
95 if (n > 0) {
96 (*outbuf) += n;
97 (*outbytesleft) -= n;
98 } else {
99 st->_errno = errno;
100 return((size_t)-1);
101 }
102 st->ustate = C0;
103 st->_errno = 0;
104 break;
105
106 default: /* should never come here */
107 st->_errno = errno = EILSEQ;
108 st->ustate = C0; /* reset state */
109 break;
110 }
111
112
113 (*inbuf)++;
114 (*inbytesleft)--;
115
116 if (st->_errno) {
117 #ifdef DEBUG
118 fprintf(stderr, "!!!!!\tst->_errno = %d\tst->ustate = %d\n",
119 st->_errno, st->ustate);
120 #endif
121 break;
122 }
123
124 if (errno)
125 return((size_t)-1);
126 }
127
128 if (*outbytesleft == 0) {
129 errno = E2BIG;
130 return((size_t)-1);
131 }
132 return (*inbytesleft);
133 }
134
135 /*
136 * IBM code --> (Unicode)
137 * Unicode --> UTF8 (FSS-UTF)
138 * (File System Safe Universal Character Set Transformation Format)
139 * Return: > 0 - converted with enough space in output buffer
140 * = 0 - no space in outbuf
141 */
ibm_to_utf8(st,buf,buflen)142 int ibm_to_utf8(st, buf, buflen)
143 _icv_state *st;
144 char *buf;
145 size_t buflen;
146 {
147 unsigned long ibm_val; /* Big-5 value */
148 int unidx; /* Unicode index */
149 unsigned long uni_val; /* Unicode */
150
151 ibm_val = ((st->keepc[0]&ONEBYTE) << 8) + (st->keepc[1]&ONEBYTE);
152 #ifdef DEBUG
153 fprintf(stderr, "%x\t", ibm_val);
154 #endif
155
156
157 unidx = bisearch(ibm_val, st, st->table_size);
158
159 if (unidx >= 0)
160 {
161 if ( st->left_to_right )
162 uni_val = st->table[unidx].right_code;
163 else
164 uni_val = st->table[unidx].left_code;
165 }
166
167 #ifdef DEBUG
168 fprintf(stderr, "unidx = %d, unicode = %x\t", unidx, uni_val);
169 #endif
170
171 if (unidx >= 0) { /* do Unicode to UTF8 conversion */
172 if (uni_val > 0x0000 && uni_val <= 0x07ff) {
173 if (buflen < 2) {
174 #ifdef DEBUG
175 fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n");
176 #endif
177 errno = E2BIG;
178 return(0);
179 }
180 *buf = (char)((uni_val >> 6) & 0x1f) | 0xc0;
181 *(buf+1) = (char)(uni_val & 0x3f) | 0x80;
182 #ifdef DEBUG
183 fprintf(stderr, "%x %x\n", *buf&ONEBYTE, *(buf+1)&ONEBYTE);
184 #endif
185 return(2);
186 }
187 if (uni_val > 0x0800 && uni_val <= 0xffff) {
188 if (buflen < 3) {
189 #ifdef DEBUG
190 fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n");
191 #endif
192 errno = E2BIG;
193 return(0);
194 }
195 *buf = (char)((uni_val >> 12) & 0xf) | 0xe0;
196 *(buf+1) = (char)((uni_val >>6) & 0x3f) | 0x80;
197 *(buf+2) = (char)(uni_val & 0x3f) | 0x80;
198 #ifdef DEBUG
199 fprintf(stderr, "%x %x %x\n", *buf&ONEBYTE, *(buf+1)&ONEBYTE, *(buf+2)&ONEBYTE);
200 #endif
201 return(3);
202 }
203 }
204
205 /* can't find a match in IBM --> UTF8 table or illegal UTF8 code */
206 if (buflen < 3) {
207 #ifdef DEBUG
208 fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n");
209 #endif
210 errno = E2BIG;
211 return(0);
212 }
213
214 *buf = (char)UTF8_NON_ID_CHAR1;
215 *(buf+1) = (char)UTF8_NON_ID_CHAR2;
216 *(buf+2) = (char)UTF8_NON_ID_CHAR3;
217
218 #ifdef DEBUG
219 fprintf(stderr, "%c %c %c\n", *buf, *(buf+1), *(buf+2));
220 #endif
221 return(3);
222 }
223