xref: /titanic_50/usr/src/lib/iconv_modules/euro/utils/8859%646es.c (revision 880d797826457b77414b37d531cc3e1aa166ecbe)
1*880d7978SAlexander Pyhalov /*
2*880d7978SAlexander Pyhalov  * CDDL HEADER START
3*880d7978SAlexander Pyhalov  *
4*880d7978SAlexander Pyhalov  * The contents of this file are subject to the terms of the
5*880d7978SAlexander Pyhalov  * Common Development and Distribution License (the "License").
6*880d7978SAlexander Pyhalov  * You may not use this file except in compliance with the License.
7*880d7978SAlexander Pyhalov  *
8*880d7978SAlexander Pyhalov  * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9*880d7978SAlexander Pyhalov  * or http://www.opensolaris.org/os/licensing.
10*880d7978SAlexander Pyhalov  * See the License for the specific language governing permissions
11*880d7978SAlexander Pyhalov  * and limitations under the License.
12*880d7978SAlexander Pyhalov  *
13*880d7978SAlexander Pyhalov  * When distributing Covered Code, include this CDDL HEADER in each
14*880d7978SAlexander Pyhalov  * file and include the License file at src/OPENSOLARIS.LICENSE.
15*880d7978SAlexander Pyhalov  * If applicable, add the following below this CDDL HEADER, with the
16*880d7978SAlexander Pyhalov  * fields enclosed by brackets "[]" replaced with your own identifying
17*880d7978SAlexander Pyhalov  * information: Portions Copyright [yyyy] [name of copyright owner]
18*880d7978SAlexander Pyhalov  *
19*880d7978SAlexander Pyhalov  * CDDL HEADER END
20*880d7978SAlexander Pyhalov  */
21*880d7978SAlexander Pyhalov /*
22*880d7978SAlexander Pyhalov  * Copyright (c) 1994, Sun Microsystems, Inc.
23*880d7978SAlexander Pyhalov  * Copyright (c) 1994, Nihon Sun Microsystems K.K.
24*880d7978SAlexander Pyhalov  */
25*880d7978SAlexander Pyhalov 
26*880d7978SAlexander Pyhalov #include <stdio.h>
27*880d7978SAlexander Pyhalov #include <stdlib.h>
28*880d7978SAlexander Pyhalov #include <errno.h>
29*880d7978SAlexander Pyhalov #include <euc.h>
30*880d7978SAlexander Pyhalov #include "japanese.h"
31*880d7978SAlexander Pyhalov 
32*880d7978SAlexander Pyhalov /*
33*880d7978SAlexander Pyhalov  * struct _cv_state; to keep status
34*880d7978SAlexander Pyhalov  */
35*880d7978SAlexander Pyhalov struct _icv_state {
36*880d7978SAlexander Pyhalov 	int	_st_cset;
37*880d7978SAlexander Pyhalov 	int	_st_stat;
38*880d7978SAlexander Pyhalov };
39*880d7978SAlexander Pyhalov 
40*880d7978SAlexander Pyhalov extern int errno;
41*880d7978SAlexander Pyhalov 
42*880d7978SAlexander Pyhalov /*
43*880d7978SAlexander Pyhalov  * Open; called from iconv_open(); as taken unchanged from @(#)ISO-2022-JP%SJIS.
44*880d7978SAlexander Pyhalov  */
45*880d7978SAlexander Pyhalov void *
_icv_open()46*880d7978SAlexander Pyhalov _icv_open()
47*880d7978SAlexander Pyhalov {
48*880d7978SAlexander Pyhalov 	struct _icv_state *st;
49*880d7978SAlexander Pyhalov 
50*880d7978SAlexander Pyhalov 	if ((st = (struct _icv_state *)malloc(sizeof(struct _icv_state)))
51*880d7978SAlexander Pyhalov 									== NULL)
52*880d7978SAlexander Pyhalov 		return ((void *)ERR_RETURN);
53*880d7978SAlexander Pyhalov 
54*880d7978SAlexander Pyhalov 	st->_st_cset = CS_0;
55*880d7978SAlexander Pyhalov 	st->_st_stat = ST_INIT;
56*880d7978SAlexander Pyhalov 
57*880d7978SAlexander Pyhalov 	return (st);
58*880d7978SAlexander Pyhalov }
59*880d7978SAlexander Pyhalov 
60*880d7978SAlexander Pyhalov 
61*880d7978SAlexander Pyhalov /*
62*880d7978SAlexander Pyhalov  * Close; called from iconv_close();  as taken unchanged from @(#)ISO-2022-JP%SJIS.
63*880d7978SAlexander Pyhalov  */
64*880d7978SAlexander Pyhalov void
_icv_close(struct _icv_state * st)65*880d7978SAlexander Pyhalov _icv_close(struct _icv_state *st)
66*880d7978SAlexander Pyhalov {
67*880d7978SAlexander Pyhalov 	free(st);
68*880d7978SAlexander Pyhalov }
69*880d7978SAlexander Pyhalov 
70*880d7978SAlexander Pyhalov 
71*880d7978SAlexander Pyhalov 
72*880d7978SAlexander Pyhalov /*
73*880d7978SAlexander Pyhalov  * Actual conversion; called from iconv()
74*880d7978SAlexander Pyhalov  */
75*880d7978SAlexander Pyhalov size_t
_icv_iconv(struct _icv_state * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)76*880d7978SAlexander Pyhalov _icv_iconv(struct _icv_state *st, char **inbuf, size_t *inbytesleft,
77*880d7978SAlexander Pyhalov 				char **outbuf, size_t *outbytesleft)
78*880d7978SAlexander Pyhalov {
79*880d7978SAlexander Pyhalov 	int				cset, stat;
80*880d7978SAlexander Pyhalov 	unsigned char	*op, ic;
81*880d7978SAlexander Pyhalov 	char			*ip;
82*880d7978SAlexander Pyhalov 	size_t			ileft, oleft;
83*880d7978SAlexander Pyhalov 	size_t			retval;
84*880d7978SAlexander Pyhalov 
85*880d7978SAlexander Pyhalov 	cset = st->_st_cset;
86*880d7978SAlexander Pyhalov 	stat = st->_st_stat;
87*880d7978SAlexander Pyhalov 
88*880d7978SAlexander Pyhalov 	/*
89*880d7978SAlexander Pyhalov 	 * If (inbuf == 0 || *inbuf == 0) then this conversion is
90*880d7978SAlexander Pyhalov 	 * placed into initial state.
91*880d7978SAlexander Pyhalov 	 */
92*880d7978SAlexander Pyhalov 	if ((inbuf == 0) || (*inbuf == 0)) {
93*880d7978SAlexander Pyhalov 		cset = CS_0;
94*880d7978SAlexander Pyhalov 		stat = ST_INIT;
95*880d7978SAlexander Pyhalov 		op = (unsigned char *)*outbuf;
96*880d7978SAlexander Pyhalov 		oleft = *outbytesleft;
97*880d7978SAlexander Pyhalov 		retval = 0;
98*880d7978SAlexander Pyhalov 		goto ret2;
99*880d7978SAlexander Pyhalov 	}
100*880d7978SAlexander Pyhalov 
101*880d7978SAlexander Pyhalov 	ip = *inbuf;
102*880d7978SAlexander Pyhalov 	op = (unsigned char *)*outbuf;
103*880d7978SAlexander Pyhalov 	ileft = *inbytesleft;
104*880d7978SAlexander Pyhalov 	oleft = *outbytesleft;
105*880d7978SAlexander Pyhalov 	/* Everything down to here was taken unchanged from  @(#)ISO-2022-JP%SJIS.
106*880d7978SAlexander Pyhalov 	   =======================================================================
107*880d7978SAlexander Pyhalov 
108*880d7978SAlexander Pyhalov 	 *
109*880d7978SAlexander Pyhalov 	 * Main loop; basically 1 loop per 1 input byte
110*880d7978SAlexander Pyhalov 	 */
111*880d7978SAlexander Pyhalov 
112*880d7978SAlexander Pyhalov 	while (ileft > 0)
113*880d7978SAlexander Pyhalov 	{
114*880d7978SAlexander Pyhalov 		GET(ic);
115*880d7978SAlexander Pyhalov 	/*
116*880d7978SAlexander Pyhalov 		If the char is one of the following [ / ] { | } then convert
117*880d7978SAlexander Pyhalov 		it to its corresponding value. In all other cases if the char
118*880d7978SAlexander Pyhalov 		is greater than octal \178 ( ie a high bit char) convert it
119*880d7978SAlexander Pyhalov 		to an underscore (_), as it has no mapping to 7 bit ASCII.
120*880d7978SAlexander Pyhalov 		Otrherwise the char is the same in both cose sets.
121*880d7978SAlexander Pyhalov 	*/
122*880d7978SAlexander Pyhalov 		if ( ic == '@' )
123*880d7978SAlexander Pyhalov 				ic = '_';
124*880d7978SAlexander Pyhalov 		else if ( ic == '[' )
125*880d7978SAlexander Pyhalov 				ic = '_';
126*880d7978SAlexander Pyhalov 		else if ( ic == '\\' )
127*880d7978SAlexander Pyhalov 				ic = '_';
128*880d7978SAlexander Pyhalov 		else if ( ic == ']' )
129*880d7978SAlexander Pyhalov 				ic = '_';
130*880d7978SAlexander Pyhalov 		else if ( ic == '{' )
131*880d7978SAlexander Pyhalov 				ic = '_';
132*880d7978SAlexander Pyhalov 		else if ( ic == '|' )
133*880d7978SAlexander Pyhalov 				ic = '_';
134*880d7978SAlexander Pyhalov 		else if ( ic == '}' )
135*880d7978SAlexander Pyhalov 				ic = '_';
136*880d7978SAlexander Pyhalov 		else if ( ic == 241 )
137*880d7978SAlexander Pyhalov 				ic = '|';
138*880d7978SAlexander Pyhalov 		else if ( ic == 161 )
139*880d7978SAlexander Pyhalov 				ic = '[';
140*880d7978SAlexander Pyhalov 		else if ( ic == 167 )
141*880d7978SAlexander Pyhalov 				ic = '\100';
142*880d7978SAlexander Pyhalov 		else if ( ic == 176 )
143*880d7978SAlexander Pyhalov 				ic = '{';
144*880d7978SAlexander Pyhalov 		else if ( ic == 191 )
145*880d7978SAlexander Pyhalov 				ic = ']';
146*880d7978SAlexander Pyhalov 		else if ( ic == 209 )
147*880d7978SAlexander Pyhalov 				ic = '\\';
148*880d7978SAlexander Pyhalov 		else if ( ic == 231 )
149*880d7978SAlexander Pyhalov 				ic = '}';
150*880d7978SAlexander Pyhalov 		else if (ic > '\177')
151*880d7978SAlexander Pyhalov 					ic = '_';
152*880d7978SAlexander Pyhalov 
153*880d7978SAlexander Pyhalov 
154*880d7978SAlexander Pyhalov 
155*880d7978SAlexander Pyhalov 
156*880d7978SAlexander Pyhalov 
157*880d7978SAlexander Pyhalov /*		switch ( ic )
158*880d7978SAlexander Pyhalov 		{
159*880d7978SAlexander Pyhalov 			case '@' :
160*880d7978SAlexander Pyhalov 				ic = '_';
161*880d7978SAlexander Pyhalov 				break;
162*880d7978SAlexander Pyhalov 			case '[' :
163*880d7978SAlexander Pyhalov 				ic = '_';
164*880d7978SAlexander Pyhalov 				break;
165*880d7978SAlexander Pyhalov 			case '\\' :
166*880d7978SAlexander Pyhalov 				ic = '_';
167*880d7978SAlexander Pyhalov 				break;
168*880d7978SAlexander Pyhalov 			case ']' :
169*880d7978SAlexander Pyhalov 				ic = '_';
170*880d7978SAlexander Pyhalov 				break;
171*880d7978SAlexander Pyhalov 			case '{' :
172*880d7978SAlexander Pyhalov 				ic = '_';
173*880d7978SAlexander Pyhalov 				break;
174*880d7978SAlexander Pyhalov 			case '|' :
175*880d7978SAlexander Pyhalov 				ic = '_';
176*880d7978SAlexander Pyhalov 				break;
177*880d7978SAlexander Pyhalov 			case '}' :
178*880d7978SAlexander Pyhalov 				ic = '_';
179*880d7978SAlexander Pyhalov 				break;
180*880d7978SAlexander Pyhalov 			case 241 :
181*880d7978SAlexander Pyhalov 				ic = '|';
182*880d7978SAlexander Pyhalov 				break;
183*880d7978SAlexander Pyhalov 			case 161 :
184*880d7978SAlexander Pyhalov 				ic = ']';
185*880d7978SAlexander Pyhalov 				break;
186*880d7978SAlexander Pyhalov 			case 167 :
187*880d7978SAlexander Pyhalov 				ic = '\100';
188*880d7978SAlexander Pyhalov 				break;
189*880d7978SAlexander Pyhalov 			case 176 :
190*880d7978SAlexander Pyhalov 				ic = '{';
191*880d7978SAlexander Pyhalov 				break;
192*880d7978SAlexander Pyhalov 			case 191 :
193*880d7978SAlexander Pyhalov 				ic = ']';
194*880d7978SAlexander Pyhalov 				break;
195*880d7978SAlexander Pyhalov 			case 209 :
196*880d7978SAlexander Pyhalov 				ic = '\\';
197*880d7978SAlexander Pyhalov 				break;
198*880d7978SAlexander Pyhalov 			case 231 :
199*880d7978SAlexander Pyhalov 				ic = '}';
200*880d7978SAlexander Pyhalov 				break;
201*880d7978SAlexander Pyhalov 			default :
202*880d7978SAlexander Pyhalov 				if (ic > '\177')
203*880d7978SAlexander Pyhalov 					ic = '_';
204*880d7978SAlexander Pyhalov 				break;
205*880d7978SAlexander Pyhalov 		} */
206*880d7978SAlexander Pyhalov 
207*880d7978SAlexander Pyhalov 		PUT(ic);
208*880d7978SAlexander Pyhalov 	/*
209*880d7978SAlexander Pyhalov 		Put the converted character into the output buffer, and decrement
210*880d7978SAlexander Pyhalov 		the count of chars left in both the in and out buffers.
211*880d7978SAlexander Pyhalov 		If we have no space left in the out buffer, but we have no reached
212*880d7978SAlexander Pyhalov 		the end of the input buffer. We return what we have, and set the
213*880d7978SAlexander Pyhalov 		errno (Error) to E2BIG.
214*880d7978SAlexander Pyhalov 	*/
215*880d7978SAlexander Pyhalov 		if ((oleft < 1)	 && (ileft > 0))
216*880d7978SAlexander Pyhalov 		{
217*880d7978SAlexander Pyhalov 			errno = E2BIG;
218*880d7978SAlexander Pyhalov 			retval = ERR_RETURN;
219*880d7978SAlexander Pyhalov 			goto ret;
220*880d7978SAlexander Pyhalov 		}
221*880d7978SAlexander Pyhalov 
222*880d7978SAlexander Pyhalov 
223*880d7978SAlexander Pyhalov 	}
224*880d7978SAlexander Pyhalov /*
225*880d7978SAlexander Pyhalov We only get here if the end of the in buffer has been reached, we therefore return the
226*880d7978SAlexander Pyhalov value 0 to denote that we have sucesfully converted the inbuffer.
227*880d7978SAlexander Pyhalov */
228*880d7978SAlexander Pyhalov 	retval = ileft;
229*880d7978SAlexander Pyhalov 
230*880d7978SAlexander Pyhalov /*  Taken unchanged from   @(#)ISO-2022-JP%SJIS.  */
231*880d7978SAlexander Pyhalov 
232*880d7978SAlexander Pyhalov ret:
233*880d7978SAlexander Pyhalov 	st->_st_cset = cset;
234*880d7978SAlexander Pyhalov 	st->_st_stat = stat;
235*880d7978SAlexander Pyhalov 	*inbuf = ip;
236*880d7978SAlexander Pyhalov 	*inbytesleft = ileft;
237*880d7978SAlexander Pyhalov ret2:
238*880d7978SAlexander Pyhalov 	*outbuf = (char *)op;
239*880d7978SAlexander Pyhalov 	*outbytesleft = oleft;
240*880d7978SAlexander Pyhalov 
241*880d7978SAlexander Pyhalov 	return (retval);
242*880d7978SAlexander Pyhalov }
243