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