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