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