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 <strings.h>
28*16d86563SAlexander Pyhalov #include <stdlib.h>
29*16d86563SAlexander Pyhalov #include "pc-iscii.h"
30*16d86563SAlexander Pyhalov 
31*16d86563SAlexander Pyhalov #define MSB          0x80
32*16d86563SAlexander Pyhalov #define REPLACE_CHAR '?'
33*16d86563SAlexander Pyhalov 
34*16d86563SAlexander Pyhalov typedef struct _icv_state {
35*16d86563SAlexander Pyhalov     int dummy;
36*16d86563SAlexander Pyhalov } _iconv_st;
37*16d86563SAlexander Pyhalov 
38*16d86563SAlexander Pyhalov static uchar
traverse_table(Entry * entry,int num,uchar iscii)39*16d86563SAlexander Pyhalov traverse_table(Entry *entry , int num, uchar iscii)
40*16d86563SAlexander Pyhalov {
41*16d86563SAlexander Pyhalov     int   i = 0;
42*16d86563SAlexander Pyhalov     uchar pc_iscii=0;
43*16d86563SAlexander Pyhalov 
44*16d86563SAlexander Pyhalov     for ( ; i < num; ++i) {
45*16d86563SAlexander Pyhalov         Entry en = entry[i];
46*16d86563SAlexander Pyhalov 
47*16d86563SAlexander Pyhalov         if ( iscii < en.iscii ) break;
48*16d86563SAlexander Pyhalov         if ( iscii >= en.iscii && iscii < en.iscii + en.count ) {
49*16d86563SAlexander Pyhalov             pc_iscii = (iscii - en.iscii) + en.pc_iscii;
50*16d86563SAlexander Pyhalov             break;
51*16d86563SAlexander Pyhalov         }
52*16d86563SAlexander Pyhalov     }
53*16d86563SAlexander Pyhalov 
54*16d86563SAlexander Pyhalov     return pc_iscii;
55*16d86563SAlexander Pyhalov }
56*16d86563SAlexander Pyhalov 
57*16d86563SAlexander Pyhalov void *
_icv_open()58*16d86563SAlexander Pyhalov _icv_open()
59*16d86563SAlexander Pyhalov {
60*16d86563SAlexander Pyhalov     _iconv_st *st;
61*16d86563SAlexander Pyhalov 
62*16d86563SAlexander Pyhalov     if ((st = (_iconv_st*)malloc(sizeof(_iconv_st))) == NULL) {
63*16d86563SAlexander Pyhalov         errno = ENOMEM;
64*16d86563SAlexander Pyhalov         return ((void*)-1);
65*16d86563SAlexander Pyhalov     }
66*16d86563SAlexander Pyhalov 
67*16d86563SAlexander Pyhalov     bzero(st, sizeof(_iconv_st));
68*16d86563SAlexander Pyhalov 
69*16d86563SAlexander Pyhalov     return ((void*)st);
70*16d86563SAlexander Pyhalov }
71*16d86563SAlexander Pyhalov 
72*16d86563SAlexander Pyhalov /*
73*16d86563SAlexander Pyhalov  * Close; called from iconv_close()
74*16d86563SAlexander Pyhalov  */
75*16d86563SAlexander Pyhalov void
_icv_close(_iconv_st * st)76*16d86563SAlexander Pyhalov _icv_close(_iconv_st *st)
77*16d86563SAlexander Pyhalov {
78*16d86563SAlexander Pyhalov     if (!st)
79*16d86563SAlexander Pyhalov         errno = EBADF;
80*16d86563SAlexander Pyhalov     else
81*16d86563SAlexander Pyhalov         free(st);
82*16d86563SAlexander Pyhalov }
83*16d86563SAlexander Pyhalov 
84*16d86563SAlexander Pyhalov size_t
_icv_iconv(_iconv_st * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)85*16d86563SAlexander Pyhalov _icv_iconv(_iconv_st *st, char **inbuf, size_t *inbytesleft,
86*16d86563SAlexander Pyhalov        char **outbuf, size_t *outbytesleft)
87*16d86563SAlexander Pyhalov {
88*16d86563SAlexander Pyhalov     if (st == NULL) {
89*16d86563SAlexander Pyhalov         errno = EBADF;
90*16d86563SAlexander Pyhalov         return ((size_t) -1);
91*16d86563SAlexander Pyhalov     }
92*16d86563SAlexander Pyhalov 
93*16d86563SAlexander Pyhalov     if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */
94*16d86563SAlexander Pyhalov         return ((size_t)0);
95*16d86563SAlexander Pyhalov     }
96*16d86563SAlexander Pyhalov 
97*16d86563SAlexander Pyhalov     /* a state machine for interpreting ISCII code */
98*16d86563SAlexander Pyhalov     while (*inbytesleft > 0 && *outbytesleft > 0) {
99*16d86563SAlexander Pyhalov         uchar c = (uchar)**inbuf;
100*16d86563SAlexander Pyhalov 
101*16d86563SAlexander Pyhalov 	if ( c & MSB ) {
102*16d86563SAlexander Pyhalov             uchar pc_iscii = traverse_table(isc_pciscii_tbl,
103*16d86563SAlexander Pyhalov                     sizeof(isc_pciscii_tbl)/sizeof(Entry), c);
104*16d86563SAlexander Pyhalov             if ( pc_iscii ) **outbuf = pc_iscii;
105*16d86563SAlexander Pyhalov             else **outbuf = REPLACE_CHAR;
106*16d86563SAlexander Pyhalov         } else { /* ASCII */
107*16d86563SAlexander Pyhalov             **outbuf = c;
108*16d86563SAlexander Pyhalov         }
109*16d86563SAlexander Pyhalov 
110*16d86563SAlexander Pyhalov         (*inbuf)++;
111*16d86563SAlexander Pyhalov         (*inbytesleft)--;
112*16d86563SAlexander Pyhalov         (*outbuf)++;
113*16d86563SAlexander Pyhalov         (*outbytesleft)--;
114*16d86563SAlexander Pyhalov     }
115*16d86563SAlexander Pyhalov 
116*16d86563SAlexander Pyhalov     if ( *inbytesleft > 0 && *outbytesleft == 0 ) {
117*16d86563SAlexander Pyhalov          errno = E2BIG;
118*16d86563SAlexander Pyhalov          return ((size_t)-1);
119*16d86563SAlexander Pyhalov     }
120*16d86563SAlexander Pyhalov 
121*16d86563SAlexander Pyhalov     return ((size_t)(*inbytesleft));
122*16d86563SAlexander Pyhalov }