xref: /titanic_51/usr/src/lib/iconv_modules/euro/utils/8859-1%IBM-500.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  *  ICU License - ICU 1.8.1 and later
23*91e1e26aSAlexander Pyhalov  *
24*91e1e26aSAlexander Pyhalov  *  COPYRIGHT AND PERMISSION NOTICE
25*91e1e26aSAlexander Pyhalov  *
26*91e1e26aSAlexander Pyhalov  * Copyright (c) 1995-2005 International Business Machines Corporation and others
27*91e1e26aSAlexander Pyhalov  * All rights reserved.
28*91e1e26aSAlexander Pyhalov  *
29*91e1e26aSAlexander Pyhalov  * Permission is hereby granted, free of charge, to any person obtaining a
30*91e1e26aSAlexander Pyhalov  * copy of this software and associated documentation files (the
31*91e1e26aSAlexander Pyhalov  * "Software"), to deal in the Software without restriction, including
32*91e1e26aSAlexander Pyhalov  * without limitation the rights to use, copy, modify, merge, publish,
33*91e1e26aSAlexander Pyhalov  * distribute, and/or sell copies of the Software, and to permit persons
34*91e1e26aSAlexander Pyhalov  * to whom the Software is furnished to do so, provided that the above
35*91e1e26aSAlexander Pyhalov  * copyright notice(s) and this permission notice appear in all copies of
36*91e1e26aSAlexander Pyhalov  * the Software and that both the above copyright notice(s) and this
37*91e1e26aSAlexander Pyhalov  * permission notice appear in supporting documentation.
38*91e1e26aSAlexander Pyhalov  *
39*91e1e26aSAlexander Pyhalov  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40*91e1e26aSAlexander Pyhalov  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41*91e1e26aSAlexander Pyhalov  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
42*91e1e26aSAlexander Pyhalov  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
43*91e1e26aSAlexander Pyhalov  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
44*91e1e26aSAlexander Pyhalov  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
45*91e1e26aSAlexander Pyhalov  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
46*91e1e26aSAlexander Pyhalov  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
47*91e1e26aSAlexander Pyhalov  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48*91e1e26aSAlexander Pyhalov  *
49*91e1e26aSAlexander Pyhalov  * Except as contained in this notice, the name of a copyright holder
50*91e1e26aSAlexander Pyhalov  * shall not be used in advertising or otherwise to promote the sale, use
51*91e1e26aSAlexander Pyhalov  * or other dealings in this Software without prior written authorization
52*91e1e26aSAlexander Pyhalov  * of the copyright holder.
53*91e1e26aSAlexander Pyhalov  *
54*91e1e26aSAlexander Pyhalov  * --------------------------------------------------------------------------
55*91e1e26aSAlexander Pyhalov  * All trademarks and registered trademarks mentioned herein are the property
56*91e1e26aSAlexander Pyhalov  * of their respective owners.
57*91e1e26aSAlexander Pyhalov  */
58*91e1e26aSAlexander Pyhalov 
59*91e1e26aSAlexander Pyhalov /*
60*91e1e26aSAlexander Pyhalov  * Copyright (c) 1994, Sun Microsystems, Inc.
61*91e1e26aSAlexander Pyhalov  * Copyright (c) 1994, Nihon Sun Microsystems K.K.
62*91e1e26aSAlexander Pyhalov  */
63*91e1e26aSAlexander Pyhalov 
64*91e1e26aSAlexander Pyhalov #include <stdio.h>
65*91e1e26aSAlexander Pyhalov #include <stdlib.h>
66*91e1e26aSAlexander Pyhalov #include <errno.h>
67*91e1e26aSAlexander Pyhalov #include <euc.h>
68*91e1e26aSAlexander Pyhalov #include "japanese.h"
69*91e1e26aSAlexander Pyhalov #include "table.8859-1.IBM-500.c"
70*91e1e26aSAlexander Pyhalov 
71*91e1e26aSAlexander Pyhalov /*
72*91e1e26aSAlexander Pyhalov  * struct _cv_state; to keep status
73*91e1e26aSAlexander Pyhalov  */
74*91e1e26aSAlexander Pyhalov struct _icv_state {
75*91e1e26aSAlexander Pyhalov 	int	_st_cset;
76*91e1e26aSAlexander Pyhalov 	int	_st_stat;
77*91e1e26aSAlexander Pyhalov };
78*91e1e26aSAlexander Pyhalov 
79*91e1e26aSAlexander Pyhalov extern int errno;
80*91e1e26aSAlexander Pyhalov 
81*91e1e26aSAlexander Pyhalov /*
82*91e1e26aSAlexander Pyhalov  * Open; called from iconv_open(); as taken unchanged from @(#)ISO-2022-JP%SJIS.
83*91e1e26aSAlexander Pyhalov  */
84*91e1e26aSAlexander Pyhalov void *
85*91e1e26aSAlexander Pyhalov _icv_open()
86*91e1e26aSAlexander Pyhalov {
87*91e1e26aSAlexander Pyhalov 	struct _icv_state *st;
88*91e1e26aSAlexander Pyhalov 
89*91e1e26aSAlexander Pyhalov 	if ((st = (struct _icv_state *)malloc(sizeof(struct _icv_state)))
90*91e1e26aSAlexander Pyhalov 									== NULL)
91*91e1e26aSAlexander Pyhalov 		return ((void *)ERR_RETURN);
92*91e1e26aSAlexander Pyhalov 
93*91e1e26aSAlexander Pyhalov 	st->_st_cset = CS_0;
94*91e1e26aSAlexander Pyhalov 	st->_st_stat = ST_INIT;
95*91e1e26aSAlexander Pyhalov 
96*91e1e26aSAlexander Pyhalov 	return (st);
97*91e1e26aSAlexander Pyhalov }
98*91e1e26aSAlexander Pyhalov 
99*91e1e26aSAlexander Pyhalov 
100*91e1e26aSAlexander Pyhalov /*
101*91e1e26aSAlexander Pyhalov  * Close; called from iconv_close();  as taken unchanged from @(#)ISO-2022-JP%SJIS.
102*91e1e26aSAlexander Pyhalov  */
103*91e1e26aSAlexander Pyhalov void
104*91e1e26aSAlexander Pyhalov _icv_close(struct _icv_state *st)
105*91e1e26aSAlexander Pyhalov {
106*91e1e26aSAlexander Pyhalov 	free(st);
107*91e1e26aSAlexander Pyhalov }
108*91e1e26aSAlexander Pyhalov 
109*91e1e26aSAlexander Pyhalov 
110*91e1e26aSAlexander Pyhalov 
111*91e1e26aSAlexander Pyhalov /*
112*91e1e26aSAlexander Pyhalov  * Actual conversion; called from iconv()
113*91e1e26aSAlexander Pyhalov  */
114*91e1e26aSAlexander Pyhalov size_t
115*91e1e26aSAlexander Pyhalov _icv_iconv(struct _icv_state *st, char **inbuf, size_t *inbytesleft,
116*91e1e26aSAlexander Pyhalov 				char **outbuf, size_t *outbytesleft)
117*91e1e26aSAlexander Pyhalov {
118*91e1e26aSAlexander Pyhalov 	int				cset, stat;
119*91e1e26aSAlexander Pyhalov 	unsigned char	*op, ic;
120*91e1e26aSAlexander Pyhalov 	char			*ip;
121*91e1e26aSAlexander Pyhalov 	size_t			ileft, oleft;
122*91e1e26aSAlexander Pyhalov 	size_t			retval;
123*91e1e26aSAlexander Pyhalov 
124*91e1e26aSAlexander Pyhalov 	cset = st->_st_cset;
125*91e1e26aSAlexander Pyhalov 	stat = st->_st_stat;
126*91e1e26aSAlexander Pyhalov 
127*91e1e26aSAlexander Pyhalov 	if ((inbuf == 0) || (*inbuf == 0)) {
128*91e1e26aSAlexander Pyhalov 		cset = CS_0;
129*91e1e26aSAlexander Pyhalov 		stat = ST_INIT;
130*91e1e26aSAlexander Pyhalov 		op = (unsigned char *)*outbuf;
131*91e1e26aSAlexander Pyhalov 		oleft = *outbytesleft;
132*91e1e26aSAlexander Pyhalov 		retval = 0;
133*91e1e26aSAlexander Pyhalov 		goto ret2;
134*91e1e26aSAlexander Pyhalov 	}
135*91e1e26aSAlexander Pyhalov 
136*91e1e26aSAlexander Pyhalov 	ip = *inbuf;
137*91e1e26aSAlexander Pyhalov 	op = (unsigned char *)*outbuf;
138*91e1e26aSAlexander Pyhalov 	ileft = *inbytesleft;
139*91e1e26aSAlexander Pyhalov 	oleft = *outbytesleft;
140*91e1e26aSAlexander Pyhalov 
141*91e1e26aSAlexander Pyhalov 	/* Everything down to here was taken unchanged from  @(#)ISO-2022-JP%SJIS.
142*91e1e26aSAlexander Pyhalov 	   =======================================================================
143*91e1e26aSAlexander Pyhalov 
144*91e1e26aSAlexander Pyhalov 	 *
145*91e1e26aSAlexander Pyhalov 	 * Main loop; basically 1 loop per 1 input byte
146*91e1e26aSAlexander Pyhalov 	 */
147*91e1e26aSAlexander Pyhalov 
148*91e1e26aSAlexander Pyhalov 	while (ileft > 0)
149*91e1e26aSAlexander Pyhalov 	{
150*91e1e26aSAlexander Pyhalov 		GET(ic);
151*91e1e26aSAlexander Pyhalov 	/*
152*91e1e26aSAlexander Pyhalov 		If the char is one of the following [ / ] { | } then convert
153*91e1e26aSAlexander Pyhalov 		it to its corresponding value. In all other cases if the char
154*91e1e26aSAlexander Pyhalov 		is greater than octal \178 ( ie a high bit char) convert it
155*91e1e26aSAlexander Pyhalov 		to an underscore (_), as it has no mapping to 7 bit ASCII.
156*91e1e26aSAlexander Pyhalov 		Otrherwise the char is the same in both cose sets.
157*91e1e26aSAlexander Pyhalov 	*/
158*91e1e26aSAlexander Pyhalov 		ic=__iso_to_cp500[ic];
159*91e1e26aSAlexander Pyhalov 
160*91e1e26aSAlexander Pyhalov 
161*91e1e26aSAlexander Pyhalov 		PUT(ic);
162*91e1e26aSAlexander Pyhalov 	/*
163*91e1e26aSAlexander Pyhalov 		Put the converted character into the output buffer, and decrement
164*91e1e26aSAlexander Pyhalov 		the count of chars left in both the in and out buffers.
165*91e1e26aSAlexander Pyhalov 		If we have no space left in the out buffer, but we have no reached
166*91e1e26aSAlexander Pyhalov 		the end of the input buffer. We return what we have, and set the
167*91e1e26aSAlexander Pyhalov 		errno (Error) to E2BIG.
168*91e1e26aSAlexander Pyhalov 	*/
169*91e1e26aSAlexander Pyhalov 		if ((oleft < 1)	 && (ileft > 0))
170*91e1e26aSAlexander Pyhalov 		{
171*91e1e26aSAlexander Pyhalov 			errno = E2BIG;
172*91e1e26aSAlexander Pyhalov 			retval = ERR_RETURN;
173*91e1e26aSAlexander Pyhalov 			goto ret;
174*91e1e26aSAlexander Pyhalov 		}
175*91e1e26aSAlexander Pyhalov 
176*91e1e26aSAlexander Pyhalov 
177*91e1e26aSAlexander Pyhalov 	}
178*91e1e26aSAlexander Pyhalov /*
179*91e1e26aSAlexander Pyhalov We only get here if the end of the in buffer has been reached, we therefore return the
180*91e1e26aSAlexander Pyhalov value 0 to denote that we have sucesfully converted the inbuffer.
181*91e1e26aSAlexander Pyhalov */
182*91e1e26aSAlexander Pyhalov 	retval = ileft;
183*91e1e26aSAlexander Pyhalov 
184*91e1e26aSAlexander Pyhalov /*  Taken unchanged from   @(#)ISO-2022-JP%SJIS.  */
185*91e1e26aSAlexander Pyhalov 
186*91e1e26aSAlexander Pyhalov ret:
187*91e1e26aSAlexander Pyhalov 	st->_st_cset = cset;
188*91e1e26aSAlexander Pyhalov 	st->_st_stat = stat;
189*91e1e26aSAlexander Pyhalov 	*inbuf = ip;
190*91e1e26aSAlexander Pyhalov 	*inbytesleft = ileft;
191*91e1e26aSAlexander Pyhalov ret2:
192*91e1e26aSAlexander Pyhalov 	*outbuf = (char *)op;
193*91e1e26aSAlexander Pyhalov 	*outbytesleft = oleft;
194*91e1e26aSAlexander Pyhalov 
195*91e1e26aSAlexander Pyhalov 	return (retval);
196*91e1e26aSAlexander Pyhalov }
197