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) 1994, Sun Microsystems, Inc.
23 * Copyright (c) 1994, Nihon Sun Microsystems K.K.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <euc.h>
30 #include "japanese.h"
31
32 /*
33 * struct _cv_state; to keep status
34 */
35 struct _icv_state {
36 int _st_cset;
37 int _st_stat;
38 };
39
40 extern int errno;
41
42 /*
43 * Open; called from iconv_open(); as taken unchanged from @(#)ISO-2022-JP%SJIS.
44 */
45 void *
_icv_open()46 _icv_open()
47 {
48 struct _icv_state *st;
49
50 if ((st = (struct _icv_state *)malloc(sizeof(struct _icv_state)))
51 == NULL)
52 return ((void *)ERR_RETURN);
53
54 st->_st_cset = CS_0;
55 st->_st_stat = ST_INIT;
56
57 return (st);
58 }
59
60
61 /*
62 * Close; called from iconv_close(); as taken unchanged from @(#)ISO-2022-JP%SJIS.
63 */
64 void
_icv_close(struct _icv_state * st)65 _icv_close(struct _icv_state *st)
66 {
67 free(st);
68 }
69
70
71
72 /*
73 * Actual conversion; called from iconv()
74 */
75 size_t
_icv_iconv(struct _icv_state * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)76 _icv_iconv(struct _icv_state *st, char **inbuf, size_t *inbytesleft,
77 char **outbuf, size_t *outbytesleft)
78 {
79 int cset, stat;
80 unsigned char *op, ic;
81 char *ip;
82 size_t ileft, oleft;
83 size_t retval;
84
85 cset = st->_st_cset;
86 stat = st->_st_stat;
87
88 /*
89 * If (inbuf == 0 || *inbuf == 0) then this conversion is
90 * placed into initial state.
91 */
92 if ((inbuf == 0) || (*inbuf == 0)) {
93 cset = CS_0;
94 stat = ST_INIT;
95 op = (unsigned char *)*outbuf;
96 oleft = *outbytesleft;
97 retval = 0;
98 goto ret2;
99 }
100
101 ip = *inbuf;
102 op = (unsigned char *)*outbuf;
103 ileft = *inbytesleft;
104 oleft = *outbytesleft;
105 /* Everything down to here was taken unchanged from @(#)ISO-2022-JP%SJIS.
106 =======================================================================
107
108 *
109 * Main loop; basically 1 loop per 1 input byte
110 */
111
112 while (ileft > 0)
113 {
114 GET(ic);
115 /*
116 If the char is one of the following [ / ] { | } then convert
117 it to its corresponding value. In all other cases if the char
118 is greater than octal \178 ( ie a high bit char) convert it
119 to an underscore (_), as it has no mapping to 7 bit ASCII.
120 Otrherwise the char is the same in both cose sets.
121 */
122
123 /* The following was changed from the switch statement for
124 64-bit building
125 */
126
127 if (ic == '\100')
128 ic = '\247';
129 else if (ic == '[')
130 ic = '\304';
131 else if (ic == ']')
132 ic = '\334';
133 else if (ic == '{')
134 ic = '\344';
135 else if (ic == '|')
136 ic = '\366';
137 else if (ic == '}')
138 ic = '\374';
139 else if (ic == '\134')
140 ic = '\326';
141 else if (ic == '~')
142 ic = '\337';
143 else if (ic > '\177')
144 ic = '_';
145
146
147
148
149
150 /* switch ( ic )
151 {
152 case '\100' :
153 ic = '\247';
154 break;
155 case '[' :
156 ic = '\304';
157 break;
158 case ']' :
159 ic = '\334';
160 break;
161 case '{' :
162 ic = '\344';
163 break;
164 case '|' :
165 ic = '\366';
166 break;
167 case '}' :
168 ic = '\374';
169 break;
170 case '\134' :
171 ic = '\326';
172 break;
173 case '~' :
174 ic = '\337';
175 break;
176 default :
177 if (ic > '\177')
178 ic = '_';
179 break;
180 } */
181
182 PUT(ic);
183 /*
184 Put the converted character into the output buffer, and decrement
185 the count of chars left in both the in and out buffers.
186 If we have no space left in the out buffer, but we have no reached
187 the end of the input buffer. We return what we have, and set the
188 errno (Error) to E2BIG.
189 */
190 if ((oleft < 1) && (ileft > 0))
191 {
192 errno = E2BIG;
193 retval = ERR_RETURN;
194 goto ret;
195 }
196
197
198 }
199 /*
200 We only get here if the end of the in buffer has been reached, we therefore return the
201 value 0 to denote that we have sucesfully converted the inbuffer.
202 */
203 retval = ileft;
204
205 /* Taken unchanged from @(#)ISO-2022-JP%SJIS. */
206
207 ret:
208 st->_st_cset = cset;
209 st->_st_stat = stat;
210 *inbuf = ip;
211 *inbytesleft = ileft;
212 ret2:
213 *outbuf = (char *)op;
214 *outbytesleft = oleft;
215
216 return (retval);
217 }
218