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 /*
23  * Copyright (c) 1996 by Sun Microsystems, Inc.
24  */
25 
26 
27 #include "common_thai.h"
28 #include "common_utf.h"
29 
30 #define UNICODE_UDC_START	0xF700
31 #define UNICODE_UDC_END		0xF8FF
32 #define UNICODE_UDC_MAX		(UNICODE_UDC_END - UNICODE_UDC_START)
33 
34 /****  _ U N I _ T O _ U T F 8  ****/
35 
_uni_to_utf8(hcode_type unicode)36 hcode_type _uni_to_utf8(hcode_type unicode)
37 {
38         hcode_type utf8;
39 
40         utf8.utf8.high8bits = 0;
41         utf8.utf8.sign1 = 0x0E;                  /* 1110xxxx */
42         utf8.utf8.data1 = unicode.unicode.data1;
43         utf8.utf8.sign2 = 0x02;                  /* 10xxxxxx */
44         utf8.utf8.data2 = unicode.unicode.data2;
45         utf8.utf8.sign3 = 0x02;                  /* 10xxxxxx */
46         utf8.utf8.data3 = unicode.unicode.data3;
47 
48         return(utf8);
49 
50 }  /* end of hcode_type _uni_to_utf8(hcode_type uni_code) */
51 
52 /****  _ U T F 8 _ T O _ U N I  ****/
53 
_utf8_to_uni(hcode_type utf8)54 hcode_type _utf8_to_uni(hcode_type utf8)
55 {
56         hcode_type unicode;
57 
58         unicode.code = 0;
59         unicode.unicode.data1 = utf8.utf8.data1;
60         unicode.unicode.data2 = utf8.utf8.data2;
61         unicode.unicode.data3 = utf8.utf8.data3;
62 
63         return(unicode);
64 
65 }  /* end of hcode_type _utf8_to_uni(hcode_type utf8) */
66 
67 /*  Return UTF-8 code from given User Defined Character Index(Serial Number) */
_udcidx_to_utf(int udcidx)68 hcode_type _udcidx_to_utf(int udcidx)
69 {
70 	hcode_type unicode, utf8;
71 
72 	if (udcidx < 0 || UNICODE_UDC_MAX < udcidx)
73 		utf8.code = UTF_UDC_ERROR;	/* over the UDC bound */
74 	else {
75 		unicode.code = UNICODE_UDC_START + udcidx;
76 		utf8 = _uni_to_utf8(unicode);
77 	}
78 
79 	return(utf8);
80 }
81 
82 /*  Return User Defined Character Index(Serial Number) from given UTF-8 code */
_utf_to_udcidx(hcode_type utf_code)83 int _utf_to_udcidx(hcode_type utf_code)
84 {
85 	hcode_type unicode;
86 
87 	unicode = _utf8_to_uni(utf_code);
88 
89 	if (unicode.code < UNICODE_UDC_START || UNICODE_UDC_END < unicode.code)
90 		return(IDX_UDC_ERROR);
91 	else
92 		return((int)(unicode.code - UNICODE_UDC_START));
93 }
94