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	johap922utf8_tbl[];
32 
33 
34 /****  _ J O H A P 9 2 _ T O _ U T F 8  ****/
35 
_johap92_to_utf8(unsigned long * chosung,unsigned long * joongsung,unsigned long * jongsung,unsigned short wcode)36 char _johap92_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 	unsigned char	byte1, byte2;
42 
43 	if (wcode > 0xD3FE)
44 	{  /* Hanja or special symbol */
45 		for (l = 0, h = MAX_J922U_NUM; l < h; )
46 		{
47 			i = (l + h) / 2;
48 			if (johap922utf8_tbl[i].code == wcode)
49 				break;
50 			else if (johap922utf8_tbl[l].code == wcode)
51 			{
52 				i = l;
53 				break;
54 			}
55 			else if (johap922utf8_tbl[h].code == wcode)
56 			{
57 				i = h;
58 				break;
59 			}
60 			else if (johap922utf8_tbl[i].code < wcode)
61 				l = i + 1;
62 			else
63 				h = i - 1;
64 		}
65 
66 		if (johap922utf8_tbl[i].code != wcode)
67 			return(ILLEGAL_SEQ);
68 
69 		*chosung = johap922utf8_tbl[i].utf8;
70 		return(HANJA_OR_SYMBOL);
71 	}
72 
73 	/* Hangul processing. */
74 	byte1 = (char)((wcode >> 8) & 0xFF);
75 	byte2 = (char)(wcode & 0xFF);
76 	if (byte1 < 0x84 || byte1 > 0xD3 || byte2 < 0x41 || byte2 > 0xFE ||
77 	    (byte2 > 0x7E && byte2 < 0x81))
78 		return(ILLEGAL_SEQ);
79 
80 	ci = CHOSUNG(wcode) - 2;
81 	v = JOONGSUNG(wcode) -
82 		((unsigned short)((unsigned short)JOONGSUNG(wcode) - 2) /
83 			((unsigned short)8) * 2 + 3);
84 	cf = JONGSUNG(wcode) - (unsigned short)JONGSUNG(wcode) / 18;
85 	*chosung = (ci < 0) ? 0xE1859F : 0xE18480 + ci;
86 	*joongsung = (v < 0) ? 0xE185A0 : 0xE185A1 + v;
87 	*jongsung = (cf < 2) ? 0 : ((cf > 25) ? 0xE18766 + cf : 0xE186A6 + cf);
88 	return(HANGUL);
89 }  /* end of char _johap92_to_utf8(unsigned long*, unsigned long*,
90 				unsigned long*, unsigned short). */
91