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 AND PERMISSION NOTICE
23  *
24  * Copyright (c) 1991-2005 Unicode, Inc. All rights reserved. Distributed
25  * under the Terms of Use in http://www.unicode.org/copyright.html.
26  *
27  * This file has been modified by Sun Microsystems, Inc.
28  */
29 /*
30  * Copyright 1999-2004 Sun Microsystems, Inc.  All rights reserved.
31  * Use is subject to license terms.
32  */
33 
34 
35 #if	defined(DEBUG)
36 #include <stdio.h>
37 #endif	/* DEBUG */
38 #include <stdlib.h>
39 #include <errno.h>
40 
41 #define	JFP_ICONV_STATELESS
42 #include "japanese.h"
43 #include "jfp_iconv_unicode.h"
44 
45 void *
_icv_open(void)46 _icv_open(void)
47 {
48 	return (_icv_open_stateless());
49 }
50 
51 void
_icv_close(void * cd)52 _icv_close(void *cd)
53 {
54 	_icv_close_stateless(cd);
55 	return;
56 }
57 
58 size_t
_icv_iconv(void * cd,char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)59 _icv_iconv(void *cd, char **inbuf, size_t *inbytesleft,
60 				char **outbuf, size_t *outbytesleft)
61 {
62 	size_t		rv = (size_t)0;
63 	unsigned int	ucs4;
64 
65 	unsigned char	*ip;
66         size_t		ileft;
67 	char		*op;
68         size_t		oleft;
69 
70 	/*
71 	 * If inbuf and/or *inbuf are NULL, reset conversion descriptor
72 	 * and put escape sequence if needed.
73 	 */
74 	if ((inbuf == NULL) || (*inbuf == NULL)) {
75 		/* nothing to do here for this module */
76 		return ((size_t)0);
77 	}
78 
79 	ip = (unsigned char *)*inbuf;
80 	ileft = *inbytesleft;
81 	op = *outbuf;
82 	oleft = *outbytesleft;
83 
84 	while (ileft != 0) {
85 		if (utf8_ucs(&ucs4, &ip, &ileft) == (size_t)-1) {
86 			/* errno has been set in utf8_ucs() */
87 			rv = (size_t)-1;
88 			goto ret;
89 		}
90 
91 		if (ucs4 == 0x301c)		/* WAVE DASH */
92 			ucs4 = 0xff5e;		/* FULLWIDTH TILDE */
93 		else if (ucs4 == 0x2016)	/* DOUBLE VERTICAL BAR/LINE */
94 			ucs4 = 0x2225;		/* PARALLEL TO */
95 		else if (ucs4 == 0x2212)	/* MINUS SIGN */
96 			ucs4 = 0xff0d;		/* FULLWIDTH MINUS SIGN */
97 		else if (ucs4 == 0x00a2)	/* CENT SIGN */
98 			ucs4 = 0xffe0;		/* FULLWIDTH CENT SIGN */
99 		else if (ucs4 == 0x00a3)	/* POUND SIGN */
100 			ucs4 = 0xffe1;		/* FULLWIDTH POUND SIGN */
101 		else if (ucs4 == 0x00ac)	/* NOT SIGN */
102 			ucs4 = 0xffe2;		/* FULLWIDTH NOT SIGN */
103 
104 		PUTUCS2((unsigned short)ucs4, "E2BIG");
105 
106 		/*
107 		 * One character successfully converted so update
108 		 * values outside of this function's stack.
109 		 */
110 		*inbuf = (char *)ip;
111 		*inbytesleft = ileft;
112 		*outbuf = op;
113 		*outbytesleft = oleft;
114 	}
115 
116 ret:
117 
118 #if	defined(DEBUG)
119 	if (rv == (size_t)-1) {
120 		fprintf(stderr, "DEBUG: errno=%d: %s\n", errno, debugmsg);
121 	}
122 #endif	/* DEBUG */
123 
124 	/*
125 	 * Return value for successful return is not defined by XPG
126 	 * so return same as *inbytesleft as existing codes do.
127 	 */
128 	return ((rv == (size_t)-1) ? rv : *inbytesleft);
129 }
130