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