xref: /illumos-gate/usr/src/lib/iconv_modules/ko/common/utf_to_njh_sub.c (revision eb00b1c8a31c2253a353644606388dff5b0e0275)
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 "utf_njh_table.h"
32 
33 static int node_compare(const void *node1, const void *node2)
34 {
35 	return((int)(((const hcode_table *)node1)->utf8.code) -
36 	       (int)(((const hcode_table *)node2)->utf8.code));
37 }
38 
39 
40 /****  _ U T F 8 _ T O _ J O H A P 9 2 ****/
41 
42 hcode_type _utf8_to_johap92(hcode_type utfcode)
43 {
44         hcode_table *node_ptr, node;
45 	hcode_type johap, unicode;
46 	int udc_index;
47 
48 	/* User Definable Area Check */
49 	if ((udc_index = _utf_to_udcidx(utfcode)) != IDX_UDC_ERROR) {
50 
51 		johap.byte.byte3 = NJH_UDC_SEG;
52 
53 		if (udc_index < NJH_UDC_OFFSET_GAP)
54 			johap.byte.byte4 = (unsigned int)(udc_index +
55 						NJH_UDC_OFFSET1_START);
56 		else
57 			johap.byte.byte4 = NJH_UDC_OFFSET2_START +
58 				(unsigned int)(udc_index - NJH_UDC_OFFSET_GAP);
59 
60 		return(johap);
61 	}
62 
63 	unicode = _utf8_to_uni(utfcode);
64 
65 	if ((UNICODE_HANGUL_START <= unicode.code) &&
66 	    (unicode.code <= UNICODE_HANGUL_END)) {
67 		/* Hangul Area */
68 		unsigned int uni, x, y, z;
69 
70 		uni  = unicode.code - 0xAC00;
71 		x = uni / 588;
72 			/* 588 = 21(Joongsung Number) * 28(Jongsung Number) */
73 		y = (uni % 588) / 28;
74 		z = (uni % 588) % 28;
75 		z = z > 0x10 ? z + 2 : z + 1;
76 
77 		johap.code = 0;
78 		johap.johap.msb = 1;
79 		johap.johap.jongsung = z;
80 		johap.johap.joongsung = y > 0x10 ? y + 9 :
81 			y > 0x0A ? y + 7 :
82 			y > 0x04 ? y + 5 : y + 3;
83 		johap.johap.chosung = x + 2;
84 
85 		return(johap);
86 
87 	} else {
88 		/* Notes: if need hangul jamo later, add here */
89 
90 	        node.utf8 = utfcode;
91 
92 		node_ptr = bsearch( &node,
93 			utf2njh_tbl, sizeof(utf2njh_tbl)/sizeof(hcode_table),
94 			sizeof(hcode_table), node_compare);
95 
96 		johap.code = NON_ID_CHAR; /* initial & default set to fail value */
97 
98 		if (node_ptr != NULL)
99 			johap.word.low = node_ptr->code; /* Success */
100 
101 		return(johap);
102 	}
103 
104 
105 }  /* end of hcode_type _utf8_to_johap92(hcode_type utfcode) */
106