xref: /illumos-gate/usr/src/lib/iconv_modules/th_TH/common/euc_to_utf_sub.c (revision 16d8656330ae5622ec32e5007f62145ebafdc50f)
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 <stdio.h>
28 #include <stdlib.h>
29 #include "common_thai.h"
30 #include "common_utf.h"
31 #include "common_euc.h"
32 #include "euc_utf_table.h"
33 
node_compare(const void * node1,const void * node2)34 static int node_compare(const void *node1, const void *node2)
35 {
36 	return(((int)(((const hcode_table *)node1)->code) -
37 	        (int)(((const hcode_table *)node2)->code)));
38 }
39 
40 /****  _ W A N S U N G _ T O _ U T F 8  ****/
41 
_eucTH_to_utf8(hcode_type euc_code)42 hcode_type _eucTH_to_utf8(hcode_type euc_code)
43 {
44         hcode_table *node_ptr, node;
45 	hcode_type utf_code;
46 	int	udc_index;
47 
48 	/* User Definable Area Check */
49 	if ((EUC_UDC_SEG1 == euc_code.byte.byte3) ||
50 	    (EUC_UDC_SEG2 == euc_code.byte.byte3)) {
51 		if ((euc_code.byte.byte4 < EUC_UDC_OFFSET_START) ||
52 		    (EUC_UDC_OFFSET_END < euc_code.byte.byte4)) {
53 			/* beyond the UDC area */
54 			utf_code.code = 0;
55 
56 			return(utf_code);
57 		}
58 
59 		udc_index = (euc_code.byte.byte3 == EUC_UDC_SEG1) ?
60 				0 : EUC_UDC_SEG_GAP;
61 		udc_index += (int)(euc_code.byte.byte4 - EUC_UDC_OFFSET_START);
62 
63 		utf_code = _udcidx_to_utf(udc_index);
64 
65 		if (utf_code.code == UTF_UDC_ERROR)
66 			utf_code.code = 0;	/* Failed */
67 
68 		return(utf_code);
69 	}
70 
71         node.code = euc_code.word.low;
72 
73         node_ptr = bsearch( &node,
74                 euc2utf_tbl, sizeof(euc2utf_tbl)/sizeof(hcode_table),
75                 sizeof(hcode_table), node_compare);
76 
77         if (node_ptr != NULL) /* Success */
78 		return(node_ptr->utf8);
79 	else { 			/* Failed */
80 		utf_code.code = 0;
81 		return(utf_code);
82 	}
83 
84 }  /* end of hcode_type _eucTH_to_utf8(hcode_type euc_code) */
85