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