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