1*16d86563SAlexander Pyhalov /*
2*16d86563SAlexander Pyhalov  * CDDL HEADER START
3*16d86563SAlexander Pyhalov  *
4*16d86563SAlexander Pyhalov  * The contents of this file are subject to the terms of the
5*16d86563SAlexander Pyhalov  * Common Development and Distribution License (the "License").
6*16d86563SAlexander Pyhalov  * You may not use this file except in compliance with the License.
7*16d86563SAlexander Pyhalov  *
8*16d86563SAlexander Pyhalov  * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9*16d86563SAlexander Pyhalov  * or http://www.opensolaris.org/os/licensing.
10*16d86563SAlexander Pyhalov  * See the License for the specific language governing permissions
11*16d86563SAlexander Pyhalov  * and limitations under the License.
12*16d86563SAlexander Pyhalov  *
13*16d86563SAlexander Pyhalov  * When distributing Covered Code, include this CDDL HEADER in each
14*16d86563SAlexander Pyhalov  * file and include the License file at src/OPENSOLARIS.LICENSE.
15*16d86563SAlexander Pyhalov  * If applicable, add the following below this CDDL HEADER, with the
16*16d86563SAlexander Pyhalov  * fields enclosed by brackets "[]" replaced with your own identifying
17*16d86563SAlexander Pyhalov  * information: Portions Copyright [yyyy] [name of copyright owner]
18*16d86563SAlexander Pyhalov  *
19*16d86563SAlexander Pyhalov  * CDDL HEADER END
20*16d86563SAlexander Pyhalov  */
21*16d86563SAlexander Pyhalov /*
22*16d86563SAlexander Pyhalov  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
23*16d86563SAlexander Pyhalov  * Use is subject to license terms.
24*16d86563SAlexander Pyhalov  *
25*16d86563SAlexander Pyhalov  * This is for UTF-8 to UTF-8 code conversion; it simply passes through
26*16d86563SAlexander Pyhalov  * all things with UTF-8 byte sequence checking to screen out any illegal
27*16d86563SAlexander Pyhalov  * and thus potentially harmful bytes.
28*16d86563SAlexander Pyhalov  */
29*16d86563SAlexander Pyhalov 
30*16d86563SAlexander Pyhalov 
31*16d86563SAlexander Pyhalov #include <stdlib.h>
32*16d86563SAlexander Pyhalov #include <errno.h>
33*16d86563SAlexander Pyhalov #include <sys/types.h>
34*16d86563SAlexander Pyhalov #include <sys/isa_defs.h>
35*16d86563SAlexander Pyhalov #include "common_defs.h"
36*16d86563SAlexander Pyhalov 
37*16d86563SAlexander Pyhalov 
38*16d86563SAlexander Pyhalov void *
_icv_open()39*16d86563SAlexander Pyhalov _icv_open()
40*16d86563SAlexander Pyhalov {
41*16d86563SAlexander Pyhalov 	return((void *)MAGIC_NUMBER);
42*16d86563SAlexander Pyhalov }
43*16d86563SAlexander Pyhalov 
44*16d86563SAlexander Pyhalov 
45*16d86563SAlexander Pyhalov void
_icv_close(int * cd)46*16d86563SAlexander Pyhalov _icv_close(int *cd)
47*16d86563SAlexander Pyhalov {
48*16d86563SAlexander Pyhalov 	if (! cd || cd != (int *)MAGIC_NUMBER)
49*16d86563SAlexander Pyhalov 		errno = EBADF;
50*16d86563SAlexander Pyhalov }
51*16d86563SAlexander Pyhalov 
52*16d86563SAlexander Pyhalov 
53*16d86563SAlexander Pyhalov size_t
_icv_iconv(int * cd,char ** inbuf,size_t * inbufleft,char ** outbuf,size_t * outbufleft)54*16d86563SAlexander Pyhalov _icv_iconv(int *cd, char **inbuf, size_t *inbufleft, char **outbuf,
55*16d86563SAlexander Pyhalov                 size_t *outbufleft)
56*16d86563SAlexander Pyhalov {
57*16d86563SAlexander Pyhalov 	size_t ret_val = 0;
58*16d86563SAlexander Pyhalov 	uchar_t *ib;
59*16d86563SAlexander Pyhalov 	uchar_t *ob;
60*16d86563SAlexander Pyhalov 	uchar_t *ibtail;
61*16d86563SAlexander Pyhalov 	uchar_t *obtail;
62*16d86563SAlexander Pyhalov 	uchar_t *ib_copy;
63*16d86563SAlexander Pyhalov 	uint_t u4;
64*16d86563SAlexander Pyhalov 	uint_t first_byte;
65*16d86563SAlexander Pyhalov 	signed char sz;
66*16d86563SAlexander Pyhalov 	signed char obsz;
67*16d86563SAlexander Pyhalov 
68*16d86563SAlexander Pyhalov 	if (! cd || cd != (int *)MAGIC_NUMBER) {
69*16d86563SAlexander Pyhalov 		errno = EBADF;
70*16d86563SAlexander Pyhalov 		return((size_t)-1);
71*16d86563SAlexander Pyhalov 	}
72*16d86563SAlexander Pyhalov 
73*16d86563SAlexander Pyhalov 	if (!inbuf || !(*inbuf))
74*16d86563SAlexander Pyhalov 		return((size_t)0);
75*16d86563SAlexander Pyhalov 
76*16d86563SAlexander Pyhalov 	ib = (uchar_t *)*inbuf;
77*16d86563SAlexander Pyhalov 	ob = (uchar_t *)*outbuf;
78*16d86563SAlexander Pyhalov 	ibtail = ib + *inbufleft;
79*16d86563SAlexander Pyhalov 	obtail = ob + *outbufleft;
80*16d86563SAlexander Pyhalov 
81*16d86563SAlexander Pyhalov 	while (ib < ibtail) {
82*16d86563SAlexander Pyhalov 		sz = number_of_bytes_in_utf8_char[*ib];
83*16d86563SAlexander Pyhalov 		if (sz == ICV_TYPE_ILLEGAL_CHAR) {
84*16d86563SAlexander Pyhalov 			errno = EILSEQ;
85*16d86563SAlexander Pyhalov 			ret_val = (size_t)-1;
86*16d86563SAlexander Pyhalov 			break;
87*16d86563SAlexander Pyhalov 		}
88*16d86563SAlexander Pyhalov 		obsz = sz;
89*16d86563SAlexander Pyhalov 
90*16d86563SAlexander Pyhalov 		if ((ibtail - ib) < sz) {
91*16d86563SAlexander Pyhalov 			errno = EINVAL;
92*16d86563SAlexander Pyhalov 			ret_val = (size_t)-1;
93*16d86563SAlexander Pyhalov 			break;
94*16d86563SAlexander Pyhalov 		}
95*16d86563SAlexander Pyhalov 
96*16d86563SAlexander Pyhalov 		ib_copy = ib;
97*16d86563SAlexander Pyhalov 		first_byte = *ib_copy++;
98*16d86563SAlexander Pyhalov 		u4 = first_byte & (uint_t)masks_tbl[sz];
99*16d86563SAlexander Pyhalov 		for (; sz > 1; sz--) {
100*16d86563SAlexander Pyhalov 			if (first_byte) {
101*16d86563SAlexander Pyhalov 				if (((uchar_t)*ib_copy) <
102*16d86563SAlexander Pyhalov 					valid_min_2nd_byte[first_byte] ||
103*16d86563SAlexander Pyhalov 				    ((uchar_t)*ib_copy) >
104*16d86563SAlexander Pyhalov 					valid_max_2nd_byte[first_byte]) {
105*16d86563SAlexander Pyhalov 					errno = EILSEQ;
106*16d86563SAlexander Pyhalov 					ret_val = (size_t)-1;
107*16d86563SAlexander Pyhalov 					goto ILLEGAL_CHAR_ERR;
108*16d86563SAlexander Pyhalov 				}
109*16d86563SAlexander Pyhalov 				first_byte = 0;
110*16d86563SAlexander Pyhalov 			} else if (((uint_t)*ib_copy) < 0x80 ||
111*16d86563SAlexander Pyhalov 				   ((uint_t)*ib_copy) > 0xbf) {
112*16d86563SAlexander Pyhalov 				errno = EILSEQ;
113*16d86563SAlexander Pyhalov 				ret_val = (size_t)-1;
114*16d86563SAlexander Pyhalov 				goto ILLEGAL_CHAR_ERR;
115*16d86563SAlexander Pyhalov 			}
116*16d86563SAlexander Pyhalov 			u4 = (u4 << ICV_UTF8_BIT_SHIFT) |
117*16d86563SAlexander Pyhalov 				(((uint_t)*ib_copy) & ICV_UTF8_BIT_MASK);
118*16d86563SAlexander Pyhalov 			ib_copy++;
119*16d86563SAlexander Pyhalov 		}
120*16d86563SAlexander Pyhalov 
121*16d86563SAlexander Pyhalov 		/*
122*16d86563SAlexander Pyhalov 		 * Check some more illegal characters and noncharacters from
123*16d86563SAlexander Pyhalov 		 * the input buffer. Surrogate pairs (U+D800 - U+DFFF) are
124*16d86563SAlexander Pyhalov 		 * checked at the above for loop.
125*16d86563SAlexander Pyhalov 		 */
126*16d86563SAlexander Pyhalov 		if ((u4 & 0xffff) == 0x00fffe || (u4 & 0xffff) == 0x00ffff ||
127*16d86563SAlexander Pyhalov 		    (u4 >= 0x00fdd0 && u4 <= 0x00fdef) || u4 > 0x10fffd) {
128*16d86563SAlexander Pyhalov 			errno = EILSEQ;
129*16d86563SAlexander Pyhalov 			ret_val = (size_t)-1;
130*16d86563SAlexander Pyhalov 			goto ILLEGAL_CHAR_ERR;
131*16d86563SAlexander Pyhalov 		}
132*16d86563SAlexander Pyhalov 
133*16d86563SAlexander Pyhalov 		if ((obtail - ob) < obsz) {
134*16d86563SAlexander Pyhalov 			errno = E2BIG;
135*16d86563SAlexander Pyhalov 			ret_val = (size_t)-1;
136*16d86563SAlexander Pyhalov 			break;
137*16d86563SAlexander Pyhalov 		}
138*16d86563SAlexander Pyhalov 
139*16d86563SAlexander Pyhalov 		for (; obsz >= 1; obsz--)
140*16d86563SAlexander Pyhalov 			*ob++ = *ib++;
141*16d86563SAlexander Pyhalov 	}
142*16d86563SAlexander Pyhalov 
143*16d86563SAlexander Pyhalov ILLEGAL_CHAR_ERR:
144*16d86563SAlexander Pyhalov 	*inbuf = (char *)ib;
145*16d86563SAlexander Pyhalov 	*inbufleft = ibtail - ib;
146*16d86563SAlexander Pyhalov 	*outbuf = (char *)ob;
147*16d86563SAlexander Pyhalov 	*outbufleft = obtail - ob;
148*16d86563SAlexander Pyhalov 
149*16d86563SAlexander Pyhalov 	return(ret_val);
150*16d86563SAlexander Pyhalov }
151