1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <euc.h>
30 #include "japanese.h"
31 #include "jfp_iconv_unicode.h"
32
33 #ifdef JAVA_CONV_COMPAT
34 #define JFP_J2U_ICONV_JAVA
35 #elif JFP_ICONV_MS932
36 #define JFP_J2U_ICONV_MS932
37 #else
38 #define JFP_J2U_ICONV
39 #endif
40 #include "jfp_jis_to_ucs2.h"
41
42 void *
_icv_open(void)43 _icv_open(void)
44 {
45 return (_icv_open_unicode((size_t)0));
46 }
47
48 void
_icv_close(void * cd)49 _icv_close(void *cd)
50 {
51 _icv_close_unicode(cd);
52 return;
53 }
54
55 size_t
_icv_iconv(void * cd,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)56 _icv_iconv(void *cd, char **inbuf, size_t *inbytesleft,
57 char **outbuf, size_t *outbytesleft)
58 {
59 unsigned int u32; /* UTF-32 */
60 unsigned char ic1, ic2, ic3; /* 1st, 2nd, and 3rd bytes of a char */
61 size_t rv = (size_t)0; /* return value of this function */
62
63 unsigned char *ip;
64 size_t ileft;
65 char *op;
66 size_t oleft;
67
68 /*
69 * If inbuf and/or *inbuf are NULL, reset conversion descriptor
70 * and put escape sequence if needed.
71 */
72 if ((inbuf == NULL) || (*inbuf == NULL)) {
73 _icv_reset_unicode(cd);
74 return ((size_t)0);
75 }
76
77 ip = (unsigned char *)*inbuf;
78 ileft = *inbytesleft;
79 op = *outbuf;
80 oleft = *outbytesleft;
81
82 while (ileft != 0) {
83 NGET(ic1, "never fail here"); /* get 1st byte */
84
85 if (ISASC(ic1)) { /* ASCII; 1 byte */
86 u32 = _jfp_tbl_jisx0201roman_to_ucs2[ic1];
87 PUTU(u32, "ASCII");
88 } else if (ISCS1(ic1)) { /* JIS X 0208 or UDC; 2 bytes */
89 NGET(ic2, "CS1-2 not available");
90 if (ISCS1(ic2)) { /* 2nd byte check passed */
91 u32 = _jfp_tbl_jisx0208_to_ucs2[
92 (ic1 - 0xa1) * 94 + (ic2 - 0xa1)];
93 PUTU(u32, "CS1");
94 } else { /* 2nd byte check failed */
95 RETERROR(EILSEQ, "CS1-2")
96 }
97 } else if (ic1 == SS2) { /* JIS X 0201 Kana; 2 bytes */
98 NGET(ic2, "CS2-2");
99 if (ISCS2(ic2)) { /* 2nd byte check passed */
100 u32 = _jfp_tbl_jisx0201kana_to_ucs2[
101 (ic2 - 0xa1)];
102 PUTU(u32, "CS2");
103 } else { /* 2nd byte check failed */
104 RETERROR(EILSEQ, "CS2-2")
105 }
106 } else if (ic1 == SS3) { /* JIS X 0212 or UDC; 3 bytes */
107 NGET(ic2, "CS3-2");
108 if (ISCS3(ic2)) { /* 2nd byte check passed */
109 NGET(ic3, "CS3-3");
110 if (ISCS3(ic3)) { /* 3rd byte check passed */
111 u32 = _jfp_tbl_jisx0212_to_ucs2[
112 ((ic2 - 0xa1) * 94 +
113 (ic3 - 0xa1))];
114 PUTU(u32, "CS3");
115 } else { /* 3rd byte check failed */
116 RETERROR(EILSEQ, "CS3-3")
117 }
118 } else { /* 2nd byte check failed */
119 RETERROR(EILSEQ, "CS3-2")
120 }
121 } else if (ISC1CTRLEUC(ic1)) { /* C1 control; 1 byte */
122 u32 = ic1;
123 PUTU(u32, "C1CTRL");
124 } else { /* 1st byte check failed */
125 RETERROR(EILSEQ, "at 1st")
126 }
127
128 /*
129 * One character successfully converted so update
130 * values outside of this function's stack.
131 */
132 *inbuf = (char *)ip;
133 *inbytesleft = ileft;
134 *outbuf = op;
135 *outbytesleft = oleft;
136 }
137
138 ret:
139
140 DEBUGPRINTERROR
141
142 /*
143 * Return value for successful return is not defined by XPG
144 * so return same as *inbytesleft as existing codes do.
145 */
146 return ((rv == (size_t)-1) ? rv : *inbytesleft);
147 }
148