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 *
_icv_open()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
_icv_close(struct _icv_state * st)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
_icv_iconv(struct _icv_state * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)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
123*91e1e26aSAlexander Pyhalov
124*91e1e26aSAlexander Pyhalov if (ic == '[')
125*91e1e26aSAlexander Pyhalov ic = '\306';
126*91e1e26aSAlexander Pyhalov else if (ic == '\134')
127*91e1e26aSAlexander Pyhalov ic = '\330';
128*91e1e26aSAlexander Pyhalov else if (ic == ']')
129*91e1e26aSAlexander Pyhalov ic = '\305';
130*91e1e26aSAlexander Pyhalov else if (ic == '{')
131*91e1e26aSAlexander Pyhalov ic = '\346';
132*91e1e26aSAlexander Pyhalov else if (ic == '|')
133*91e1e26aSAlexander Pyhalov ic = '\370';
134*91e1e26aSAlexander Pyhalov else if (ic == '{')
135*91e1e26aSAlexander Pyhalov ic = '\345';
136*91e1e26aSAlexander Pyhalov else if (ic > '\177')
137*91e1e26aSAlexander Pyhalov ic = '_';
138*91e1e26aSAlexander Pyhalov
139*91e1e26aSAlexander Pyhalov
140*91e1e26aSAlexander Pyhalov
141*91e1e26aSAlexander Pyhalov /* switch ( ic )
142*91e1e26aSAlexander Pyhalov {
143*91e1e26aSAlexander Pyhalov case '[' :
144*91e1e26aSAlexander Pyhalov ic = '\306';
145*91e1e26aSAlexander Pyhalov break;
146*91e1e26aSAlexander Pyhalov case '\134' :
147*91e1e26aSAlexander Pyhalov ic = '\330';
148*91e1e26aSAlexander Pyhalov break;
149*91e1e26aSAlexander Pyhalov case ']' :
150*91e1e26aSAlexander Pyhalov ic = '\305';
151*91e1e26aSAlexander Pyhalov break;
152*91e1e26aSAlexander Pyhalov case '{' :
153*91e1e26aSAlexander Pyhalov ic = '\346';
154*91e1e26aSAlexander Pyhalov break;
155*91e1e26aSAlexander Pyhalov case '|' :
156*91e1e26aSAlexander Pyhalov ic = '\370';
157*91e1e26aSAlexander Pyhalov break;
158*91e1e26aSAlexander Pyhalov case '}' :
159*91e1e26aSAlexander Pyhalov ic = '\345';
160*91e1e26aSAlexander Pyhalov break;
161*91e1e26aSAlexander Pyhalov default :
162*91e1e26aSAlexander Pyhalov if (ic > '\177')
163*91e1e26aSAlexander Pyhalov ic = '_';
164*91e1e26aSAlexander Pyhalov break;
165*91e1e26aSAlexander Pyhalov } */
166*91e1e26aSAlexander Pyhalov
167*91e1e26aSAlexander Pyhalov PUT(ic);
168*91e1e26aSAlexander Pyhalov /*
169*91e1e26aSAlexander Pyhalov Put the converted character into the output buffer, and decrement
170*91e1e26aSAlexander Pyhalov the count of chars left in both the in and out buffers.
171*91e1e26aSAlexander Pyhalov If we have no space left in the out buffer, but we have no reached
172*91e1e26aSAlexander Pyhalov the end of the input buffer. We return what we have, and set the
173*91e1e26aSAlexander Pyhalov errno (Error) to E2BIG.
174*91e1e26aSAlexander Pyhalov */
175*91e1e26aSAlexander Pyhalov if ((oleft < 1) && (ileft > 0))
176*91e1e26aSAlexander Pyhalov {
177*91e1e26aSAlexander Pyhalov errno = E2BIG;
178*91e1e26aSAlexander Pyhalov retval = ERR_RETURN;
179*91e1e26aSAlexander Pyhalov goto ret;
180*91e1e26aSAlexander Pyhalov }
181*91e1e26aSAlexander Pyhalov
182*91e1e26aSAlexander Pyhalov
183*91e1e26aSAlexander Pyhalov }
184*91e1e26aSAlexander Pyhalov /*
185*91e1e26aSAlexander Pyhalov We only get here if the end of the in buffer has been reached, we therefore return the
186*91e1e26aSAlexander Pyhalov value 0 to denote that we have sucesfully converted the inbuffer.
187*91e1e26aSAlexander Pyhalov */
188*91e1e26aSAlexander Pyhalov retval = ileft;
189*91e1e26aSAlexander Pyhalov
190*91e1e26aSAlexander Pyhalov /* Taken unchanged from @(#)ISO-2022-JP%SJIS. */
191*91e1e26aSAlexander Pyhalov
192*91e1e26aSAlexander Pyhalov ret:
193*91e1e26aSAlexander Pyhalov st->_st_cset = cset;
194*91e1e26aSAlexander Pyhalov st->_st_stat = stat;
195*91e1e26aSAlexander Pyhalov *inbuf = ip;
196*91e1e26aSAlexander Pyhalov *inbytesleft = ileft;
197*91e1e26aSAlexander Pyhalov ret2:
198*91e1e26aSAlexander Pyhalov *outbuf = (char *)op;
199*91e1e26aSAlexander Pyhalov *outbytesleft = oleft;
200*91e1e26aSAlexander Pyhalov
201*91e1e26aSAlexander Pyhalov return (retval);
202*91e1e26aSAlexander Pyhalov }
203