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 (c) 1996 by Sun Microsystems, Inc.
23*16d86563SAlexander Pyhalov  */
24*16d86563SAlexander Pyhalov 
25*16d86563SAlexander Pyhalov #include "common_han.h"
26*16d86563SAlexander Pyhalov #include "common_utf.h"
27*16d86563SAlexander Pyhalov 
28*16d86563SAlexander Pyhalov #define UNICODE_UDC_START	0xF700
29*16d86563SAlexander Pyhalov #define UNICODE_UDC_END		0xF8FF
30*16d86563SAlexander Pyhalov #define UNICODE_UDC_MAX		(UNICODE_UDC_END - UNICODE_UDC_START)
31*16d86563SAlexander Pyhalov 
32*16d86563SAlexander Pyhalov /****  _ U N I _ T O _ U T F 8  ****/
33*16d86563SAlexander Pyhalov 
_uni_to_utf8(hcode_type unicode)34*16d86563SAlexander Pyhalov hcode_type _uni_to_utf8(hcode_type unicode)
35*16d86563SAlexander Pyhalov {
36*16d86563SAlexander Pyhalov         hcode_type utf8;
37*16d86563SAlexander Pyhalov 	utf8.code = 0;
38*16d86563SAlexander Pyhalov 
39*16d86563SAlexander Pyhalov 	/* new code */
40*16d86563SAlexander Pyhalov 	if(unicode.code <= 0x007F){
41*16d86563SAlexander Pyhalov 		utf8.code = unicode.code;
42*16d86563SAlexander Pyhalov 	}
43*16d86563SAlexander Pyhalov 	else if(unicode.code >= 0x0080 && unicode.code <= 0x07FF){
44*16d86563SAlexander Pyhalov 		utf8.utf8.high8bits = 0x00;
45*16d86563SAlexander Pyhalov 		utf8.utf8.sign1 = 0x00;
46*16d86563SAlexander Pyhalov 		utf8.utf8.data1 = 0x00;
47*16d86563SAlexander Pyhalov 		utf8.utf8.sign2 = 0x03;
48*16d86563SAlexander Pyhalov 		utf8.utf8.data2 = unicode.unicode.data2;
49*16d86563SAlexander Pyhalov 		utf8.utf8.sign3 = 0x02;
50*16d86563SAlexander Pyhalov 		utf8.utf8.data3 = unicode.unicode.data3;
51*16d86563SAlexander Pyhalov 	}
52*16d86563SAlexander Pyhalov 	else if(unicode.code >= 0x0800 && unicode.code < 0xFFFF){
53*16d86563SAlexander Pyhalov 		utf8.utf8.high8bits = 0x00;
54*16d86563SAlexander Pyhalov 		utf8.utf8.sign1 = 0x0E;
55*16d86563SAlexander Pyhalov 		utf8.utf8.data1 = unicode.unicode.data1;
56*16d86563SAlexander Pyhalov 		utf8.utf8.sign2 = 0x02;                  /* 10xxxxxx */
57*16d86563SAlexander Pyhalov 		utf8.utf8.data2 = unicode.unicode.data2;
58*16d86563SAlexander Pyhalov 		utf8.utf8.sign3 = 0x02;                  /* 10xxxxxx */
59*16d86563SAlexander Pyhalov 		utf8.utf8.data3 = unicode.unicode.data3;
60*16d86563SAlexander Pyhalov 	}
61*16d86563SAlexander Pyhalov 
62*16d86563SAlexander Pyhalov         return(utf8);
63*16d86563SAlexander Pyhalov 
64*16d86563SAlexander Pyhalov }  /* end of hcode_type _uni_to_utf8(hcode_type uni_code) */
65*16d86563SAlexander Pyhalov 
66*16d86563SAlexander Pyhalov /****  _ U T F 8 _ T O _ U N I  ****/
67*16d86563SAlexander Pyhalov 
_utf8_to_uni(hcode_type utf8)68*16d86563SAlexander Pyhalov hcode_type _utf8_to_uni(hcode_type utf8)
69*16d86563SAlexander Pyhalov {
70*16d86563SAlexander Pyhalov 	hcode_type unicode;
71*16d86563SAlexander Pyhalov 	unicode.code = 0;
72*16d86563SAlexander Pyhalov 
73*16d86563SAlexander Pyhalov         if(utf8.byte.byte3 == 0 && utf8.byte.byte2 ==0)
74*16d86563SAlexander Pyhalov 	{/* ASCII */
75*16d86563SAlexander Pyhalov 		unicode.byte.byte1 = 0;
76*16d86563SAlexander Pyhalov 		unicode.byte.byte2 = 0;
77*16d86563SAlexander Pyhalov                 unicode.byte.byte3 = 0;
78*16d86563SAlexander Pyhalov 		unicode.byte.byte4 = utf8.byte.byte4;
79*16d86563SAlexander Pyhalov 		return(unicode);
80*16d86563SAlexander Pyhalov 	}
81*16d86563SAlexander Pyhalov 
82*16d86563SAlexander Pyhalov 	if(utf8.byte.byte2 == 0){
83*16d86563SAlexander Pyhalov 	/* 2-byte UTF-8 */
84*16d86563SAlexander Pyhalov 		unicode.byte.byte1 = 0;
85*16d86563SAlexander Pyhalov 		unicode.byte.byte2 = 0;
86*16d86563SAlexander Pyhalov 		unicode.byte.byte3 = (utf8.byte.byte3 & 0x3F) >> 2;
87*16d86563SAlexander Pyhalov 		unicode.byte.byte4 = (utf8.byte.byte3 << 6) | (0x3F & utf8.byte.byte4);
88*16d86563SAlexander Pyhalov 	}
89*16d86563SAlexander Pyhalov 	else {
90*16d86563SAlexander Pyhalov 	/* 3-byte UTF-8 */
91*16d86563SAlexander Pyhalov 		unicode.unicode.data1 = utf8.utf8.data1;
92*16d86563SAlexander Pyhalov 		unicode.unicode.data2 = utf8.utf8.data2;
93*16d86563SAlexander Pyhalov 		unicode.unicode.data3 = utf8.utf8.data3;
94*16d86563SAlexander Pyhalov 	}
95*16d86563SAlexander Pyhalov 	return (unicode);
96*16d86563SAlexander Pyhalov 
97*16d86563SAlexander Pyhalov }  /* end of hcode_type _utf8_to_uni(hcode_type utf8) */
98*16d86563SAlexander Pyhalov 
99*16d86563SAlexander Pyhalov /*  Return UTF-8 code from given User Defined Character Index(Serial Number) */
_udcidx_to_utf(int udcidx)100*16d86563SAlexander Pyhalov hcode_type _udcidx_to_utf(int udcidx)
101*16d86563SAlexander Pyhalov {
102*16d86563SAlexander Pyhalov 	hcode_type unicode, utf8;
103*16d86563SAlexander Pyhalov 
104*16d86563SAlexander Pyhalov 	if (udcidx < 0 || UNICODE_UDC_MAX < udcidx)
105*16d86563SAlexander Pyhalov 		utf8.code = UTF_UDC_ERROR;	/* over the UDC bound */
106*16d86563SAlexander Pyhalov 	else {
107*16d86563SAlexander Pyhalov 		unicode.code = UNICODE_UDC_START + udcidx;
108*16d86563SAlexander Pyhalov 		utf8 = _uni_to_utf8(unicode);
109*16d86563SAlexander Pyhalov 	}
110*16d86563SAlexander Pyhalov 
111*16d86563SAlexander Pyhalov 	return(utf8);
112*16d86563SAlexander Pyhalov }
113*16d86563SAlexander Pyhalov 
114*16d86563SAlexander Pyhalov /*  Return User Defined Character Index(Serial Number) from given UTF-8 code */
_utf_to_udcidx(hcode_type utf_code)115*16d86563SAlexander Pyhalov int _utf_to_udcidx(hcode_type utf_code)
116*16d86563SAlexander Pyhalov {
117*16d86563SAlexander Pyhalov 	hcode_type unicode;
118*16d86563SAlexander Pyhalov 
119*16d86563SAlexander Pyhalov 	unicode = _utf8_to_uni(utf_code);
120*16d86563SAlexander Pyhalov 
121*16d86563SAlexander Pyhalov 	if (unicode.code < UNICODE_UDC_START || UNICODE_UDC_END < unicode.code)
122*16d86563SAlexander Pyhalov 		return(IDX_UDC_ERROR);
123*16d86563SAlexander Pyhalov 	else
124*16d86563SAlexander Pyhalov 		return((int)(unicode.code - UNICODE_UDC_START));
125*16d86563SAlexander Pyhalov }
126