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 <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <euc.h>
31
32 #include "japanese.h"
33 #include "jfp_iconv_unicode.h"
34
35 #ifdef JAVA_CONV_COMPAT
36 #define JFP_U2E_ICONV_JAVA
37 #elif JFP_ICONV_MS932
38 #define JFP_U2E_ICONV_MS932
39 #else
40 #define JFP_U2E_ICONV
41 #endif
42 #include "jfp_ucs2_to_euc16.h"
43
44 #define DEF_SINGLE '?'
45
46 void *
_icv_open(void)47 _icv_open(void)
48 {
49 return (_icv_open_unicode((size_t)0));
50 }
51
52 void
_icv_close(void * cd)53 _icv_close(void *cd)
54 {
55 _icv_close_unicode(cd);
56 return;
57 }
58
59 size_t
_icv_iconv(void * cd,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)60 _icv_iconv(void *cd, char **inbuf, size_t *inbytesleft,
61 char **outbuf, size_t *outbytesleft)
62 {
63 unsigned char ic;
64 size_t rv = (size_t)0;
65 unsigned int ucs4;
66 unsigned short euc16;
67
68 unsigned char *ip;
69 size_t ileft;
70 char *op;
71 size_t oleft;
72
73 /*
74 * If inbuf and/or *inbuf are NULL, reset conversion descriptor
75 * and put escape sequence if needed.
76 */
77 if ((inbuf == NULL) || (*inbuf == NULL)) {
78 _icv_reset_unicode(cd);
79 return ((size_t)0);
80 }
81
82 ip = (unsigned char *)*inbuf;
83 ileft = *inbytesleft;
84 op = *outbuf;
85 oleft = *outbytesleft;
86
87 while (ileft != 0) {
88 GETU(&ucs4);
89
90 if (ucs4 > 0xffff) {
91 /* non-BMP */
92 ic = (unsigned char)DEF_SINGLE;
93 NPUT(ic, "DEF for non-BMP");
94 } else {
95 euc16 = _jfp_ucs2_to_euc16((unsigned short)ucs4);
96
97 switch (euc16 & 0x8080) {
98 case 0x0000: /* CS0 */
99 ic = (unsigned char)euc16;
100 NPUT(ic, "CS0");
101 break;
102 case 0x8080: /* CS1 */
103 ic = (unsigned char)((euc16 >> 8) & 0xff);
104 NPUT(ic, "CS1-1");
105 ic = (unsigned char)(euc16 & 0xff);
106 NPUT(ic, "CS1-2");
107 break;
108 case 0x0080: /* CS2 */
109 NPUT(SS2, "CS2-1");
110 ic = (unsigned char)euc16;
111 NPUT(ic, "CS2-2");
112 break;
113 case 0x8000: /* CS3 */
114 NPUT(SS3, "E2BIG at CS3-1");
115 ic = (unsigned char)((euc16 >> 8) & 0xff);
116 NPUT(ic, "CS3-2");
117 ic = (unsigned char)(euc16 & CMASK);
118 NPUT(ic | CMSB, "CS3-3");
119 break;
120 }
121 }
122
123 next:
124 /*
125 * One character successfully converted so update
126 * values outside of this function's stack.
127 */
128 *inbuf = (char *)ip;
129 *inbytesleft = ileft;
130 *outbuf = op;
131 *outbytesleft = oleft;
132 }
133
134 ret:
135 DEBUGPRINTERROR
136
137 /*
138 * Return value for successful return is not defined by XPG
139 * so return same as *inbytesleft as existing codes do.
140 */
141 return ((rv == (size_t)-1) ? rv : *inbytesleft);
142 }
143