1*880d7978SAlexander Pyhalov /*
2*880d7978SAlexander Pyhalov * CDDL HEADER START
3*880d7978SAlexander Pyhalov *
4*880d7978SAlexander Pyhalov * The contents of this file are subject to the terms of the
5*880d7978SAlexander Pyhalov * Common Development and Distribution License (the "License").
6*880d7978SAlexander Pyhalov * You may not use this file except in compliance with the License.
7*880d7978SAlexander Pyhalov *
8*880d7978SAlexander Pyhalov * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9*880d7978SAlexander Pyhalov * or http://www.opensolaris.org/os/licensing.
10*880d7978SAlexander Pyhalov * See the License for the specific language governing permissions
11*880d7978SAlexander Pyhalov * and limitations under the License.
12*880d7978SAlexander Pyhalov *
13*880d7978SAlexander Pyhalov * When distributing Covered Code, include this CDDL HEADER in each
14*880d7978SAlexander Pyhalov * file and include the License file at src/OPENSOLARIS.LICENSE.
15*880d7978SAlexander Pyhalov * If applicable, add the following below this CDDL HEADER, with the
16*880d7978SAlexander Pyhalov * fields enclosed by brackets "[]" replaced with your own identifying
17*880d7978SAlexander Pyhalov * information: Portions Copyright [yyyy] [name of copyright owner]
18*880d7978SAlexander Pyhalov *
19*880d7978SAlexander Pyhalov * CDDL HEADER END
20*880d7978SAlexander Pyhalov */
21*880d7978SAlexander Pyhalov /*
22*880d7978SAlexander Pyhalov * Copyright(c) 2001 Sun Microsystems, Inc.
23*880d7978SAlexander Pyhalov * All rights reserved.
24*880d7978SAlexander Pyhalov */
25*880d7978SAlexander Pyhalov #include <stdio.h>
26*880d7978SAlexander Pyhalov #include <errno.h>
27*880d7978SAlexander Pyhalov #include <strings.h>
28*880d7978SAlexander Pyhalov #include <stdlib.h>
29*880d7978SAlexander Pyhalov #include "pc-iscii.h"
30*880d7978SAlexander Pyhalov
31*880d7978SAlexander Pyhalov #define MSB 0x80
32*880d7978SAlexander Pyhalov #define REPLACE_CHAR '?'
33*880d7978SAlexander Pyhalov
34*880d7978SAlexander Pyhalov typedef struct _icv_state {
35*880d7978SAlexander Pyhalov int dummy;
36*880d7978SAlexander Pyhalov } _iconv_st;
37*880d7978SAlexander Pyhalov
38*880d7978SAlexander Pyhalov static uchar
traverse_table(Entry * entry,int num,uchar pc_iscii)39*880d7978SAlexander Pyhalov traverse_table(Entry *entry , int num, uchar pc_iscii)
40*880d7978SAlexander Pyhalov {
41*880d7978SAlexander Pyhalov int i=0;
42*880d7978SAlexander Pyhalov uchar iscii=0;
43*880d7978SAlexander Pyhalov
44*880d7978SAlexander Pyhalov for ( ; i < num; ++i) {
45*880d7978SAlexander Pyhalov Entry en = entry[i];
46*880d7978SAlexander Pyhalov
47*880d7978SAlexander Pyhalov if ( pc_iscii < en.pc_iscii ) break;
48*880d7978SAlexander Pyhalov if ( pc_iscii >= en.pc_iscii && pc_iscii < en.pc_iscii + en.count ) {
49*880d7978SAlexander Pyhalov iscii = (pc_iscii - en.pc_iscii) + en.iscii;
50*880d7978SAlexander Pyhalov break;
51*880d7978SAlexander Pyhalov }
52*880d7978SAlexander Pyhalov }
53*880d7978SAlexander Pyhalov
54*880d7978SAlexander Pyhalov return iscii;
55*880d7978SAlexander Pyhalov }
56*880d7978SAlexander Pyhalov
57*880d7978SAlexander Pyhalov void *
_icv_open()58*880d7978SAlexander Pyhalov _icv_open()
59*880d7978SAlexander Pyhalov {
60*880d7978SAlexander Pyhalov _iconv_st *st;
61*880d7978SAlexander Pyhalov
62*880d7978SAlexander Pyhalov if ((st = (_iconv_st*)malloc(sizeof(_iconv_st))) == NULL) {
63*880d7978SAlexander Pyhalov errno = ENOMEM;
64*880d7978SAlexander Pyhalov return ((void*)-1);
65*880d7978SAlexander Pyhalov }
66*880d7978SAlexander Pyhalov
67*880d7978SAlexander Pyhalov bzero(st, sizeof(_iconv_st));
68*880d7978SAlexander Pyhalov
69*880d7978SAlexander Pyhalov return ((void*)st);
70*880d7978SAlexander Pyhalov }
71*880d7978SAlexander Pyhalov
72*880d7978SAlexander Pyhalov /*
73*880d7978SAlexander Pyhalov * Close; called from iconv_close()
74*880d7978SAlexander Pyhalov */
75*880d7978SAlexander Pyhalov void
_icv_close(_iconv_st * st)76*880d7978SAlexander Pyhalov _icv_close(_iconv_st *st)
77*880d7978SAlexander Pyhalov {
78*880d7978SAlexander Pyhalov if (!st)
79*880d7978SAlexander Pyhalov errno = EBADF;
80*880d7978SAlexander Pyhalov else
81*880d7978SAlexander Pyhalov free(st);
82*880d7978SAlexander Pyhalov }
83*880d7978SAlexander Pyhalov
84*880d7978SAlexander Pyhalov size_t
_icv_iconv(_iconv_st * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)85*880d7978SAlexander Pyhalov _icv_iconv(_iconv_st *st, char **inbuf, size_t *inbytesleft,
86*880d7978SAlexander Pyhalov char **outbuf, size_t *outbytesleft)
87*880d7978SAlexander Pyhalov {
88*880d7978SAlexander Pyhalov if (st == NULL) {
89*880d7978SAlexander Pyhalov errno = EBADF;
90*880d7978SAlexander Pyhalov return ((size_t) -1);
91*880d7978SAlexander Pyhalov }
92*880d7978SAlexander Pyhalov
93*880d7978SAlexander Pyhalov if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */
94*880d7978SAlexander Pyhalov return ((size_t)0);
95*880d7978SAlexander Pyhalov }
96*880d7978SAlexander Pyhalov
97*880d7978SAlexander Pyhalov /* a state machine for interpreting ISCII code */
98*880d7978SAlexander Pyhalov while (*inbytesleft > 0 && *outbytesleft > 0) {
99*880d7978SAlexander Pyhalov uchar c = (uchar)**inbuf;
100*880d7978SAlexander Pyhalov
101*880d7978SAlexander Pyhalov if ( c & MSB ) {
102*880d7978SAlexander Pyhalov uchar iscii = traverse_table(pciscii_isc_tbl,
103*880d7978SAlexander Pyhalov sizeof(pciscii_isc_tbl)/sizeof(Entry), c);
104*880d7978SAlexander Pyhalov if ( iscii ) **outbuf = iscii;
105*880d7978SAlexander Pyhalov else **outbuf = REPLACE_CHAR;
106*880d7978SAlexander Pyhalov } else { /* ASCII */
107*880d7978SAlexander Pyhalov **outbuf = c;
108*880d7978SAlexander Pyhalov }
109*880d7978SAlexander Pyhalov
110*880d7978SAlexander Pyhalov (*inbuf)++;
111*880d7978SAlexander Pyhalov (*inbytesleft)--;
112*880d7978SAlexander Pyhalov (*outbuf)++;
113*880d7978SAlexander Pyhalov (*outbytesleft)--;
114*880d7978SAlexander Pyhalov }
115*880d7978SAlexander Pyhalov
116*880d7978SAlexander Pyhalov if ( *inbytesleft > 0 && *outbytesleft == 0 ) {
117*880d7978SAlexander Pyhalov errno = E2BIG;
118*880d7978SAlexander Pyhalov return ((size_t)-1);
119*880d7978SAlexander Pyhalov }
120*880d7978SAlexander Pyhalov
121*880d7978SAlexander Pyhalov return ((size_t)(*inbytesleft));
122*880d7978SAlexander Pyhalov }
123