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