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