xref: /illumos-gate/usr/src/lib/iconv_modules/ko/common/njh_to_utf_sub.c (revision f52943a93040563107b95bccb9db87d9971ef47d)
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) 1996 by Sun Microsystems, Inc.
23  */
24 
25 
26 #include <errno.h>
27 #include <stdlib.h>
28 #include "common_han.h"
29 #include "common_utf.h"
30 #include "common_njh.h"
31 #include "njh_utf_table.h"
32 
33 static int node_compare(const void *node1, const void *node2)
34 {
35 	return(((int)(((const hcode_table *)node1)->code) -
36 	        (int)(((const hcode_table *)node2)->code)));
37 }
38 
39 /****  _ J O H A P 9 2 _ T O _ U T F 8  ****/
40 
41 hcode_type _johap92_to_utf8(hcode_type njh_code)
42 {
43         hcode_table *node_ptr, node;
44 	hcode_type utf_code;
45 	int	udc_index;
46 
47 	/* User Definable Area Check */
48 	if (njh_code.byte.byte3 == NJH_UDC_SEG) {
49 		if ((njh_code.byte.byte4 < NJH_UDC_OFFSET1_START) ||
50 		    (NJH_UDC_OFFSET2_END < njh_code.byte.byte4) ||
51 		    ((NJH_UDC_OFFSET1_END < njh_code.byte.byte4) &&
52 		     (njh_code.byte.byte4 < NJH_UDC_OFFSET2_START))) {
53 			/* beyond the UDC area */
54 			utf_code.code = 0;
55 
56 			return(utf_code);
57 		}
58 
59 		if (njh_code.byte.byte4 >= NJH_UDC_OFFSET2_START)
60 			udc_index = NJH_UDC_OFFSET_GAP +
61 				(int)(njh_code.byte.byte4 - NJH_UDC_OFFSET2_START);
62 		else
63 			udc_index =
64 				(int)(njh_code.byte.byte4 - NJH_UDC_OFFSET1_START);
65 
66 		utf_code = _udcidx_to_utf(udc_index);
67 
68 		if (utf_code.code == UTF_UDC_ERROR)
69 			utf_code.code = UTF8_NON_ID_CHAR;	/* Failed */
70 
71 		return(utf_code);
72 
73 	} else if (njh_code.code > NJH_HANGUL_END) {
74 		/* Hanja or special symbol */
75 		/* Notes: if Hangul Jamo needed, add here to table coversion */
76 
77 		node.code = njh_code.word.low;
78 
79 		node_ptr = bsearch( &node,
80 			njh2utf_tbl, sizeof(njh2utf_tbl)/sizeof(hcode_table),
81 			sizeof(hcode_table), node_compare);
82 
83 		if (node_ptr != NULL) /* Success */
84 			return(node_ptr->utf8);
85 		else { 			/* Failed */
86 			utf_code.code = UTF8_NON_ID_CHAR;
87 			return(utf_code);
88 		}
89 
90 	} else {
91 		/* Hangul code */
92 		hcode_type unicode;
93 		register unsigned int x, y, z;
94 
95 		x = njh_code.johap.chosung - 2;  /* 2 = 'Kyoug' */
96 		y = njh_code.johap.joongsung;
97 		y = y < 0x08 ? y - 3 :
98 			y < 0x10 ? y - 5 :
99 			y < 0x18 ? y - 7 : y - 9;
100 		z = njh_code.johap.jongsung;
101 		z = z < 0x12 ? z - 1 : z - 2;
102 		unicode.code = (unsigned int)(x*588 + y*28 + z)
103 				+ 0xAC00;
104 			/* 588 = 21(Joongsung Number) * 28(Jongsung Number) */
105 
106 		utf_code = _uni_to_utf8(unicode);
107 
108 		return(utf_code);
109 
110 	}
111 
112 }  /* end of hcode_type johap92_to_utf8(hcode_type njh_code) */
113