xref: /titanic_50/usr/src/lib/iconv_modules/euro/utils/alt%koi8.c (revision 880d797826457b77414b37d531cc3e1aa166ecbe)
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 #include "table.alt.koi.c"
32 
33 /*
34  * struct _cv_state; to keep status
35  */
36 struct _icv_state {
37 	int	_st_cset;
38 	int	_st_stat;
39 };
40 
41 extern int errno;
42 
43 /*
44  * Open; called from iconv_open(); as taken unchanged from @(#)ISO-2022-JP%SJIS.
45  */
46 void *
_icv_open()47 _icv_open()
48 {
49 	struct _icv_state *st;
50 
51 	if ((st = (struct _icv_state *)malloc(sizeof(struct _icv_state)))
52 									== NULL)
53 		return ((void *)ERR_RETURN);
54 
55 	st->_st_cset = CS_0;
56 	st->_st_stat = ST_INIT;
57 
58 	return (st);
59 }
60 
61 
62 /*
63  * Close; called from iconv_close();  as taken unchanged from @(#)ISO-2022-JP%SJIS.
64  */
65 void
_icv_close(struct _icv_state * st)66 _icv_close(struct _icv_state *st)
67 {
68 	free(st);
69 }
70 
71 
72 
73 /*
74  * Actual conversion; called from iconv()
75  */
76 size_t
_icv_iconv(struct _icv_state * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)77 _icv_iconv(struct _icv_state *st, char **inbuf, size_t *inbytesleft,
78 				char **outbuf, size_t *outbytesleft)
79 {
80 	int				cset, stat;
81 	unsigned char	*op, ic;
82 	char			*ip;
83 	size_t			ileft, oleft;
84 	size_t			retval;
85 
86 	cset = st->_st_cset;
87 	stat = st->_st_stat;
88 
89 	if ((inbuf == 0) || (*inbuf == 0)) {
90 		cset = CS_0;
91 		stat = ST_INIT;
92 		op = (unsigned char *)*outbuf;
93 		oleft = *outbytesleft;
94 		retval = 0;
95 		goto ret2;
96 	}
97 
98 	ip = *inbuf;
99 	op = (unsigned char *)*outbuf;
100 	ileft = *inbytesleft;
101 	oleft = *outbytesleft;
102 
103 	/* Everything down to here was taken unchanged from  @(#)ISO-2022-JP%SJIS.
104 	   =======================================================================
105 
106 	 *
107 	 * Main loop; basically 1 loop per 1 input byte
108 	 */
109 
110 	while (ileft > 0)
111 	{
112 		GET(ic);
113 	/*
114 		If the char is one of the following [ / ] { | } then convert
115 		it to its corresponding value. In all other cases if the char
116 		is greater than octal \178 ( ie a high bit char) convert it
117 		to an underscore (_), as it has no mapping to 7 bit ASCII.
118 		Otrherwise the char is the same in both cose sets.
119 	*/
120 		ic=__alt_to_koi[ic];
121 
122 
123 		PUT(ic);
124 	/*
125 		Put the converted character into the output buffer, and decrement
126 		the count of chars left in both the in and out buffers.
127 		If we have no space left in the out buffer, but we have no reached
128 		the end of the input buffer. We return what we have, and set the
129 		errno (Error) to E2BIG.
130 	*/
131 		if ((oleft < 1)	 && (ileft > 0))
132 		{
133 			errno = E2BIG;
134 			retval = ERR_RETURN;
135 			goto ret;
136 		}
137 
138 
139 	}
140 /*
141 We only get here if the end of the in buffer has been reached, we therefore return the
142 value 0 to denote that we have sucesfully converted the inbuffer.
143 */
144 	retval = ileft;
145 
146 /*  Taken unchanged from   @(#)ISO-2022-JP%SJIS.  */
147 
148 ret:
149 	st->_st_cset = cset;
150 	st->_st_stat = stat;
151 	*inbuf = ip;
152 	*inbytesleft = ileft;
153 ret2:
154 	*outbuf = (char *)op;
155 	*outbytesleft = oleft;
156 
157 	return (retval);
158 }
159