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