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) 2008, by 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 <stdlib.h>
28*880d7978SAlexander Pyhalov #include <sys/types.h>
29*880d7978SAlexander Pyhalov #define __NEED_UNI_2_TCVN__
30*880d7978SAlexander Pyhalov #include <unicode_tcvn.h> /* Unicode to TCVN mapping table */
31*880d7978SAlexander Pyhalov #include "common_defs.h"
32*880d7978SAlexander Pyhalov
33*880d7978SAlexander Pyhalov #define NON_ID_CHAR '?' /* non-identified character */
34*880d7978SAlexander Pyhalov
35*880d7978SAlexander Pyhalov typedef struct _icv_state {
36*880d7978SAlexander Pyhalov int _errno; /* internal errno */
37*880d7978SAlexander Pyhalov } _iconv_st;
38*880d7978SAlexander Pyhalov
39*880d7978SAlexander Pyhalov
40*880d7978SAlexander Pyhalov /*
41*880d7978SAlexander Pyhalov * Open; called from iconv_open()
42*880d7978SAlexander Pyhalov */
43*880d7978SAlexander Pyhalov void *
_icv_open()44*880d7978SAlexander Pyhalov _icv_open()
45*880d7978SAlexander Pyhalov {
46*880d7978SAlexander Pyhalov _iconv_st *st;
47*880d7978SAlexander Pyhalov
48*880d7978SAlexander Pyhalov if ((st = (_iconv_st *)malloc(sizeof(_iconv_st))) == NULL) {
49*880d7978SAlexander Pyhalov errno = ENOMEM;
50*880d7978SAlexander Pyhalov return ((void *) -1);
51*880d7978SAlexander Pyhalov }
52*880d7978SAlexander Pyhalov
53*880d7978SAlexander Pyhalov st->_errno = 0;
54*880d7978SAlexander Pyhalov return ((void *) st);
55*880d7978SAlexander Pyhalov }
56*880d7978SAlexander Pyhalov
57*880d7978SAlexander Pyhalov
58*880d7978SAlexander Pyhalov /*
59*880d7978SAlexander Pyhalov * Close; called from iconv_close()
60*880d7978SAlexander Pyhalov */
61*880d7978SAlexander Pyhalov void
_icv_close(_iconv_st * st)62*880d7978SAlexander Pyhalov _icv_close(_iconv_st *st)
63*880d7978SAlexander Pyhalov {
64*880d7978SAlexander Pyhalov if (!st)
65*880d7978SAlexander Pyhalov errno = EBADF;
66*880d7978SAlexander Pyhalov else
67*880d7978SAlexander Pyhalov free(st);
68*880d7978SAlexander Pyhalov }
69*880d7978SAlexander Pyhalov
70*880d7978SAlexander Pyhalov
71*880d7978SAlexander Pyhalov /*
72*880d7978SAlexander Pyhalov * Actual conversion; called from iconv()
73*880d7978SAlexander Pyhalov */
74*880d7978SAlexander Pyhalov size_t
_icv_iconv(_iconv_st * st,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)75*880d7978SAlexander Pyhalov _icv_iconv(_iconv_st *st, char **inbuf, size_t *inbytesleft,
76*880d7978SAlexander Pyhalov char **outbuf, size_t *outbytesleft)
77*880d7978SAlexander Pyhalov {
78*880d7978SAlexander Pyhalov int no_id_char_num = 0;
79*880d7978SAlexander Pyhalov #ifdef DEBUG
80*880d7978SAlexander Pyhalov fprintf(stderr, "========== iconv(): UCS-2 --> TCVN5712 ==========\n");
81*880d7978SAlexander Pyhalov #endif
82*880d7978SAlexander Pyhalov if (st == NULL) {
83*880d7978SAlexander Pyhalov errno = EBADF;
84*880d7978SAlexander Pyhalov return ((size_t) -1);
85*880d7978SAlexander Pyhalov }
86*880d7978SAlexander Pyhalov
87*880d7978SAlexander Pyhalov if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */
88*880d7978SAlexander Pyhalov st->_errno = 0;
89*880d7978SAlexander Pyhalov return ((size_t) 0);
90*880d7978SAlexander Pyhalov }
91*880d7978SAlexander Pyhalov
92*880d7978SAlexander Pyhalov st->_errno = 0; /* Rreset internal errno */
93*880d7978SAlexander Pyhalov errno = 0; /* Rreset external errno */
94*880d7978SAlexander Pyhalov
95*880d7978SAlexander Pyhalov /* Convert UCS-2 encoding to TCVN5712 */
96*880d7978SAlexander Pyhalov while (*inbytesleft > 0 && *outbytesleft > 0) {
97*880d7978SAlexander Pyhalov unsigned long uni = 0;
98*880d7978SAlexander Pyhalov unsigned char c1 = 0, c2 = 0;
99*880d7978SAlexander Pyhalov unsigned char ch = 0;
100*880d7978SAlexander Pyhalov
101*880d7978SAlexander Pyhalov c1 = **inbuf;
102*880d7978SAlexander Pyhalov if (*inbytesleft <= 1 ) {
103*880d7978SAlexander Pyhalov errno = EINVAL;
104*880d7978SAlexander Pyhalov return ((size_t)-1);
105*880d7978SAlexander Pyhalov }
106*880d7978SAlexander Pyhalov (*inbuf)++;
107*880d7978SAlexander Pyhalov (*inbytesleft) -= 1;
108*880d7978SAlexander Pyhalov c2 = **inbuf;
109*880d7978SAlexander Pyhalov (*inbuf)++;
110*880d7978SAlexander Pyhalov (*inbytesleft) -= 1;
111*880d7978SAlexander Pyhalov
112*880d7978SAlexander Pyhalov #if defined(UCS_2LE)
113*880d7978SAlexander Pyhalov uni |= (unsigned long)c1;
114*880d7978SAlexander Pyhalov uni |= (unsigned long)c2<< 8;
115*880d7978SAlexander Pyhalov #else
116*880d7978SAlexander Pyhalov uni |= (unsigned long)c1<< 8;
117*880d7978SAlexander Pyhalov uni |= (unsigned long)c2;
118*880d7978SAlexander Pyhalov #endif
119*880d7978SAlexander Pyhalov if ( *inbytesleft > 0 && *outbytesleft <= 0 ) {
120*880d7978SAlexander Pyhalov errno = E2BIG;
121*880d7978SAlexander Pyhalov (*inbuf) -= 2;
122*880d7978SAlexander Pyhalov (*inbytesleft) +=2;
123*880d7978SAlexander Pyhalov return ((size_t)-1);
124*880d7978SAlexander Pyhalov }
125*880d7978SAlexander Pyhalov
126*880d7978SAlexander Pyhalov if (uni_2_tcvn(uni, &ch) == 1) {
127*880d7978SAlexander Pyhalov **outbuf = ch;
128*880d7978SAlexander Pyhalov } else {
129*880d7978SAlexander Pyhalov **outbuf = NON_ID_CHAR;
130*880d7978SAlexander Pyhalov no_id_char_num += 1;
131*880d7978SAlexander Pyhalov }
132*880d7978SAlexander Pyhalov (*outbuf) += 1;
133*880d7978SAlexander Pyhalov (*outbytesleft) -= 1;
134*880d7978SAlexander Pyhalov
135*880d7978SAlexander Pyhalov }
136*880d7978SAlexander Pyhalov
137*880d7978SAlexander Pyhalov return ((size_t)no_id_char_num);
138*880d7978SAlexander Pyhalov }
139