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) 2001 Sun Microsystems, Inc.
23*16d86563SAlexander Pyhalov  * All rights reserved.
24*16d86563SAlexander Pyhalov  */
25*16d86563SAlexander Pyhalov #include <stdio.h>
26*16d86563SAlexander Pyhalov #include <errno.h>
27*16d86563SAlexander Pyhalov #include <ctype.h>
28*16d86563SAlexander Pyhalov #include <strings.h>
29*16d86563SAlexander Pyhalov #include <stdlib.h>
30*16d86563SAlexander Pyhalov #include "ea-iscii.h"
31*16d86563SAlexander Pyhalov 
32*16d86563SAlexander Pyhalov #define MSB          0x80
33*16d86563SAlexander Pyhalov #define REPLACE_CHAR '?'
34*16d86563SAlexander Pyhalov 
35*16d86563SAlexander Pyhalov typedef enum { SPACE, ASCII, ISCII } CONTEXT;
36*16d86563SAlexander Pyhalov 
37*16d86563SAlexander Pyhalov typedef struct _icv_state {
38*16d86563SAlexander Pyhalov     CONTEXT context;
39*16d86563SAlexander Pyhalov } _iconv_st;
40*16d86563SAlexander Pyhalov 
41*16d86563SAlexander Pyhalov static uchar
traverse_table(Entry * entry,int num,uchar iscii,uchar * type)42*16d86563SAlexander Pyhalov traverse_table(Entry *entry , int num, uchar iscii, uchar *type)
43*16d86563SAlexander Pyhalov {
44*16d86563SAlexander Pyhalov     int   i = 0;
45*16d86563SAlexander Pyhalov     uchar ea_iscii=0;
46*16d86563SAlexander Pyhalov 
47*16d86563SAlexander Pyhalov     *type = 0;
48*16d86563SAlexander Pyhalov 
49*16d86563SAlexander Pyhalov     for ( ; i < num; ++i) {
50*16d86563SAlexander Pyhalov         Entry en = entry[i];
51*16d86563SAlexander Pyhalov 
52*16d86563SAlexander Pyhalov 	if ( iscii < en.iscii ) break;
53*16d86563SAlexander Pyhalov 
54*16d86563SAlexander Pyhalov         if ( en.count == NUKTA || en.count == MATRA || en.count ==
55*16d86563SAlexander Pyhalov              COMBINED_MATRA_NUKTA ) {
56*16d86563SAlexander Pyhalov 	     if ( iscii == en.iscii ) {
57*16d86563SAlexander Pyhalov                  *type = en.count;
58*16d86563SAlexander Pyhalov                  ea_iscii = en.ea_iscii;
59*16d86563SAlexander Pyhalov                  break;
60*16d86563SAlexander Pyhalov              }
61*16d86563SAlexander Pyhalov         } else {
62*16d86563SAlexander Pyhalov              if ( iscii >= en.iscii && iscii < en.iscii + en.count ) {
63*16d86563SAlexander Pyhalov                  ea_iscii = (iscii - en.iscii) + en.ea_iscii;
64*16d86563SAlexander Pyhalov                  break;
65*16d86563SAlexander Pyhalov              }
66*16d86563SAlexander Pyhalov         }
67*16d86563SAlexander Pyhalov     }
68*16d86563SAlexander Pyhalov 
69*16d86563SAlexander Pyhalov     return ea_iscii;
70*16d86563SAlexander Pyhalov }
71*16d86563SAlexander Pyhalov 
72*16d86563SAlexander Pyhalov void *
_icv_open()73*16d86563SAlexander Pyhalov _icv_open()
74*16d86563SAlexander Pyhalov {
75*16d86563SAlexander Pyhalov     _iconv_st *st;
76*16d86563SAlexander Pyhalov 
77*16d86563SAlexander Pyhalov     if ((st = (_iconv_st*)malloc(sizeof(_iconv_st))) == NULL) {
78*16d86563SAlexander Pyhalov         errno = ENOMEM;
79*16d86563SAlexander Pyhalov         return ((void*)-1);
80*16d86563SAlexander Pyhalov     }
81*16d86563SAlexander Pyhalov 
82*16d86563SAlexander Pyhalov     bzero(st, sizeof(_iconv_st));
83*16d86563SAlexander Pyhalov 
84*16d86563SAlexander Pyhalov     return ((void*)st);
85*16d86563SAlexander Pyhalov }
86*16d86563SAlexander Pyhalov 
87*16d86563SAlexander Pyhalov /*
88*16d86563SAlexander Pyhalov  * Close; called from iconv_close()
89*16d86563SAlexander Pyhalov  */
90*16d86563SAlexander Pyhalov void
_icv_close(_iconv_st * st)91*16d86563SAlexander Pyhalov _icv_close(_iconv_st *st)
92*16d86563SAlexander Pyhalov {
93*16d86563SAlexander Pyhalov     if (!st)
94*16d86563SAlexander Pyhalov         errno = EBADF;
95*16d86563SAlexander Pyhalov     else
96*16d86563SAlexander Pyhalov         free(st);
97*16d86563SAlexander Pyhalov }
98*16d86563SAlexander Pyhalov 
99*16d86563SAlexander Pyhalov size_t
_icv_iconv(_iconv_st * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)100*16d86563SAlexander Pyhalov _icv_iconv(_iconv_st *st, char **inbuf, size_t *inbytesleft,
101*16d86563SAlexander Pyhalov        char **outbuf, size_t *outbytesleft)
102*16d86563SAlexander Pyhalov {
103*16d86563SAlexander Pyhalov     if (st == NULL) {
104*16d86563SAlexander Pyhalov         errno = EBADF;
105*16d86563SAlexander Pyhalov         return ((size_t) -1);
106*16d86563SAlexander Pyhalov     }
107*16d86563SAlexander Pyhalov 
108*16d86563SAlexander Pyhalov     if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */
109*16d86563SAlexander Pyhalov         return ((size_t)0);
110*16d86563SAlexander Pyhalov     }
111*16d86563SAlexander Pyhalov 
112*16d86563SAlexander Pyhalov     /* a state machine for interpreting ISCII code */
113*16d86563SAlexander Pyhalov     while (*inbytesleft > 0 && *outbytesleft > 0) {
114*16d86563SAlexander Pyhalov         uchar c = (uchar)**inbuf;
115*16d86563SAlexander Pyhalov 
116*16d86563SAlexander Pyhalov         if ( c & MSB ) {
117*16d86563SAlexander Pyhalov 
118*16d86563SAlexander Pyhalov              uchar type, ea_iscii;
119*16d86563SAlexander Pyhalov 
120*16d86563SAlexander Pyhalov              if ( st->context != ISCII ) {
121*16d86563SAlexander Pyhalov 
122*16d86563SAlexander Pyhalov                  if ( st->context != SPACE ) {
123*16d86563SAlexander Pyhalov                     /* force to insert ' ' between ASCII and ISCII */
124*16d86563SAlexander Pyhalov                     **outbuf = 0x20;
125*16d86563SAlexander Pyhalov                     (*outbuf)++;
126*16d86563SAlexander Pyhalov                     (*outbytesleft)--;
127*16d86563SAlexander Pyhalov                     st->context = SPACE;
128*16d86563SAlexander Pyhalov                  }
129*16d86563SAlexander Pyhalov 
130*16d86563SAlexander Pyhalov                  if ( *outbytesleft < 1 ) {
131*16d86563SAlexander Pyhalov                      errno = E2BIG;
132*16d86563SAlexander Pyhalov                      /* don't advance */
133*16d86563SAlexander Pyhalov                      return (size_t)-1;
134*16d86563SAlexander Pyhalov                  }
135*16d86563SAlexander Pyhalov 
136*16d86563SAlexander Pyhalov                  st->context = ISCII;
137*16d86563SAlexander Pyhalov                  **outbuf = LEADING_BYTE;
138*16d86563SAlexander Pyhalov                  (*outbuf)++;
139*16d86563SAlexander Pyhalov                  (*outbytesleft)--;
140*16d86563SAlexander Pyhalov              }
141*16d86563SAlexander Pyhalov 
142*16d86563SAlexander Pyhalov              if ((ea_iscii = traverse_table(isc_eaiscii_tbl,
143*16d86563SAlexander Pyhalov 			sizeof(isc_eaiscii_tbl)/sizeof(Entry), c, &type ))) {
144*16d86563SAlexander Pyhalov                  switch ( type ) {
145*16d86563SAlexander Pyhalov                  case MATRA:
146*16d86563SAlexander Pyhalov                       if ( *outbytesleft < 2 ) {
147*16d86563SAlexander Pyhalov                          errno = E2BIG;
148*16d86563SAlexander Pyhalov                          return (size_t)-1;
149*16d86563SAlexander Pyhalov                       }
150*16d86563SAlexander Pyhalov 
151*16d86563SAlexander Pyhalov                       **outbuf = FIRST_VOWEL;
152*16d86563SAlexander Pyhalov                       *(*outbuf+1) = ea_iscii;
153*16d86563SAlexander Pyhalov                       (*outbuf) += 2;
154*16d86563SAlexander Pyhalov                       (*outbytesleft) -= 2;
155*16d86563SAlexander Pyhalov                       break;
156*16d86563SAlexander Pyhalov                  case NUKTA:
157*16d86563SAlexander Pyhalov                       if ( *outbytesleft < 2 ) {
158*16d86563SAlexander Pyhalov                          errno = E2BIG;
159*16d86563SAlexander Pyhalov                          return (size_t)-1;
160*16d86563SAlexander Pyhalov                       }
161*16d86563SAlexander Pyhalov 
162*16d86563SAlexander Pyhalov                       **outbuf = ea_iscii;
163*16d86563SAlexander Pyhalov                       *(*outbuf+1) = NUKTA_VALUE;
164*16d86563SAlexander Pyhalov                       (*outbuf) += 2;
165*16d86563SAlexander Pyhalov                       (*outbytesleft) -= 2;
166*16d86563SAlexander Pyhalov                       break;
167*16d86563SAlexander Pyhalov                  case COMBINED_MATRA_NUKTA:
168*16d86563SAlexander Pyhalov                       if ( *outbytesleft < 3 ) {
169*16d86563SAlexander Pyhalov                          errno = E2BIG;
170*16d86563SAlexander Pyhalov                          return (size_t)-1;
171*16d86563SAlexander Pyhalov                       }
172*16d86563SAlexander Pyhalov 
173*16d86563SAlexander Pyhalov                       **outbuf = FIRST_VOWEL;
174*16d86563SAlexander Pyhalov                       *(*outbuf+1) = ea_iscii;
175*16d86563SAlexander Pyhalov                       *(*outbuf+2) = NUKTA_VALUE;
176*16d86563SAlexander Pyhalov                       (*outbuf) += 3;
177*16d86563SAlexander Pyhalov                       (*outbytesleft) -= 3;
178*16d86563SAlexander Pyhalov                       break;
179*16d86563SAlexander Pyhalov                  case 0:
180*16d86563SAlexander Pyhalov                       if ( *outbytesleft < 1 ) {
181*16d86563SAlexander Pyhalov                          errno = E2BIG;
182*16d86563SAlexander Pyhalov                          return (size_t)-1;
183*16d86563SAlexander Pyhalov                       }
184*16d86563SAlexander Pyhalov 
185*16d86563SAlexander Pyhalov                       **outbuf = ea_iscii;
186*16d86563SAlexander Pyhalov                       (*outbuf)++;
187*16d86563SAlexander Pyhalov                       (*outbytesleft)--;
188*16d86563SAlexander Pyhalov                       break;
189*16d86563SAlexander Pyhalov                  }
190*16d86563SAlexander Pyhalov              } else { /* REPLACE_CHAR */
191*16d86563SAlexander Pyhalov                  if ( *outbytesleft < 1 ) {
192*16d86563SAlexander Pyhalov                     errno = E2BIG;
193*16d86563SAlexander Pyhalov                     return (size_t)-1;
194*16d86563SAlexander Pyhalov                  }
195*16d86563SAlexander Pyhalov 
196*16d86563SAlexander Pyhalov                  **outbuf = REPLACE_CHAR;
197*16d86563SAlexander Pyhalov                  (*outbuf)++;
198*16d86563SAlexander Pyhalov                  (*outbytesleft)--;
199*16d86563SAlexander Pyhalov              }
200*16d86563SAlexander Pyhalov         } else { /* ASCII */
201*16d86563SAlexander Pyhalov              if ( st->context == ISCII && !isspace(c) ) {
202*16d86563SAlexander Pyhalov                  /* force to insert ' ' between ASCII and ISCII */
203*16d86563SAlexander Pyhalov                  **outbuf = 0x20;
204*16d86563SAlexander Pyhalov                  (*outbuf)++;
205*16d86563SAlexander Pyhalov                  (*outbytesleft)--;
206*16d86563SAlexander Pyhalov                  st->context = SPACE;
207*16d86563SAlexander Pyhalov              }
208*16d86563SAlexander Pyhalov 
209*16d86563SAlexander Pyhalov              if ( *outbytesleft < 1 ) {
210*16d86563SAlexander Pyhalov                 errno = E2BIG;
211*16d86563SAlexander Pyhalov                 return (size_t)-1;
212*16d86563SAlexander Pyhalov              }
213*16d86563SAlexander Pyhalov 
214*16d86563SAlexander Pyhalov              **outbuf = c;
215*16d86563SAlexander Pyhalov              (*outbuf)++;
216*16d86563SAlexander Pyhalov              (*outbytesleft)--;
217*16d86563SAlexander Pyhalov 
218*16d86563SAlexander Pyhalov              st->context = ASCII;
219*16d86563SAlexander Pyhalov              if ( isspace(c) )
220*16d86563SAlexander Pyhalov                   st->context = SPACE;
221*16d86563SAlexander Pyhalov         }
222*16d86563SAlexander Pyhalov 
223*16d86563SAlexander Pyhalov         (*inbuf)++;
224*16d86563SAlexander Pyhalov         (*inbytesleft)--;
225*16d86563SAlexander Pyhalov     }
226*16d86563SAlexander Pyhalov 
227*16d86563SAlexander Pyhalov     if ( *inbytesleft > 0 && *outbytesleft == 0 ) {
228*16d86563SAlexander Pyhalov          errno = E2BIG;
229*16d86563SAlexander Pyhalov          return ((size_t)-1);
230*16d86563SAlexander Pyhalov     }
231*16d86563SAlexander Pyhalov 
232*16d86563SAlexander Pyhalov     return ((size_t)(*inbytesleft));
233*16d86563SAlexander Pyhalov }
234