xref: /illumos-gate/usr/src/lib/iconv_modules/ko/common/wansung_to_utf.c (revision 4c87aefe8930bd07275b8dd2e96ea5f24d93a52e)
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  * Copyright (c) 1994 by Sun Microsystems, Inc.
23  */
24 
25 
26 #include <errno.h>
27 #include "ktable.h"
28 #include "hangulcode.h"
29 
30 
31 extern kcode_table	euc2utf8_tbl[];
32 
33 
34 /****  _ W A N S U N G _ T O _ U T F 8  ****/
35 
36 char _wansung_to_utf8(unsigned long* chosung, unsigned long* joongsung,
37 				unsigned long* jongsung, unsigned short wcode)
38 {
39 	register short	h, i, j, l;
40 	short		ci, v, cf;
41 	short		disp;
42 	long		cfbit;
43 
44 	if (wcode < 0xb0a1 || wcode > 0xc8fe)
45 	{  /* Hanja or special symbol */
46 		for (l = 0, h = MAX_E2U_NUM; l < h; )
47 		{
48 			i = (l + h) / 2;
49 			if (euc2utf8_tbl[i].code == wcode)
50 				break;
51 			else if (euc2utf8_tbl[l].code == wcode)
52 			{
53 				i = l;
54 				break;
55 			}
56 			else if (euc2utf8_tbl[h].code == wcode)
57 			{
58 				i = h;
59 				break;
60 			}
61 			else if (euc2utf8_tbl[i].code < wcode)
62 				l = i + 1;
63 			else
64 				h = i - 1;
65 		}
66 
67 		if (euc2utf8_tbl[i].code != wcode)
68 			return(FAILED);
69 
70 		*chosung = euc2utf8_tbl[i].utf8;
71 		return(HANJA_OR_SYMBOL);
72 	}
73 
74 	if ((short)(wcode & 0xFF) < 0xA1)
75 		return(FAILED);
76 
77 	/* Hangul processing. */
78 	for (h = CI_CNT, l = 0; ; )
79 	{
80 		ci = (l + h) / 2;
81 		if (l >= h)
82 			break;
83 		if (wcode < cmp_srchtbl[ci][0])
84 			h = ci - 1;
85 		else if (wcode < cmp_srchtbl[ci + 1][0])
86 			break;
87 		else
88 			l = ci + 1;
89 	}
90 
91 	for (v = 1; ; )
92 	{
93 		if (wcode < cmp_srchtbl[ci][v])
94 		{
95 			while (!cmp_srchtbl[ci][--v])
96 				;
97 			break;
98 		}
99 		else if (v == V_CNT)
100 			break;
101 		v++;
102 	}
103 
104 	disp = wcode - cmp_srchtbl[ci][v];
105 	if (((short)(cmp_srchtbl[ci][v] & BYTE_MASK) + disp) > 0xfe)
106 		disp -= SKIP;
107 
108 	for (cfbit = cmp_bitmap[ci][v], i = -1, cf = -1; i < disp; cf++)
109 	{
110 		if (cfbit & BIT_MASK)
111 			i++;
112 		cfbit >>= 1;
113 	}
114 
115 	if (cf == -1)
116 		return(FAILED);
117 
118 	*chosung = 0xE18480 + ci;
119 	*joongsung = 0xE185A1 + v;
120 	/**** Original meaning is like below.
121 	*jongsung = (cf < 2) ? 0 <-- FILL character
122 			     : (cf > 25) ? 0xE18780 + cf - 26
123 					 : 0xE186A8 + cf - 2;
124 	****/
125 	*jongsung = (cf < 2) ? 0 : ((cf > 25) ? 0xE18766 + cf : 0xE186A6 + cf);
126 	return(HANGUL);
127 }  /* end of char _wansung_to_utf8(unsigned long*, unsigned long*,
128 				unsigned long*, unsigned short). */
129