xref: /titanic_52/usr/src/lib/iconv_modules/ko/common/pack_to_comp.c (revision 91e1e26ac6a73ce959289cf7d3d96c4baedbe0b8)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * $Id: pack_to_comp.c,v 1.15 1997/10/31 16:17:04 binz Exp $ SMI
29  */
30 
31 /* Based on Korean Standard Code    87-3 */
32 
33 /*
34  * convert 2 byte combination code into
35  * 	   2 byte completion code
36  */
37 
38 #include 	<stdio.h>
39 #include	"kdefs.h"
40 #include	"ktable.h"
41 
42 #define		SKIP	0xa1 + 0xff - 0xfe
43 
44 #ifdef	__STDC__
45 	KCHAR packtocomp(KCHAR comb2)
46 #else
47 	KCHAR packtocomp(comb2)
48 	KCHAR comb2;
49 #endif
50 {
51 	KCHAR	comp2 ;
52 	short 	Ci_val;    /* initial sound */
53 	short	V_val ;    /* middle  sound */
54 	short	Cf_val;    /* final   sound */
55 	short	mask ;
56 
57 	int	disp, k;
58 
59 	long	Cfbit ;
60 
61 #if defined(i386) || defined(__ppc)
62 	comb2 = ((comb2 & 0xff00) >> 8) | ((comb2 & 0x00ff) << 8);
63 #endif
64 
65 	/* Find index value of	initial sound	*/
66 	/*			middle  sound	*/
67 	/*			final   sound	*/
68 	/*	from combination code for table	*/
69 
70 	Ci_val = INITIAL_SOUND((unsigned int)comb2) - 0x0a;
71 	V_val  = MIDDLE_SOUND((unsigned int)comb2) - (MIDDLE_SOUND((unsigned int)comb2)/4 + 2);
72 	Cf_val = FINAL_SOUND(comb2);
73 
74 	/*
75 	 * Special case code check
76 	 */
77 	if ( V_val < 0 )	/* just initial sound */
78 #if defined(i386) || defined(__ppc)
79 	{
80 		comp2 = 0xa4a0 + Y19_32[INITIAL_SOUND((unsigned int)comb2)
81 			- 0x09];
82 		return(((comp2 & 0x00ff) << 8)|((comp2 & 0xff00) >> 8));
83 	}
84 #else
85 		return(0xa4a0 + Y19_32[INITIAL_SOUND((unsigned int)comb2)
86 			- 0x09]);
87 #endif
88 
89 	if (Ci_val < 0 )	/* just middle  sound */
90         {
91 		if (Cf_val <= 1)
92 #if defined(i386) || defined(__ppc)
93 		{
94 			comp2 = 0xa4bf + MIDDLE_SOUND((unsigned int)comb2)
95 				- MIDDLE_SOUND((unsigned int)comb2)/4 - 2;
96 			return(((comp2 & 0x00ff) << 8)|((comp2 & 0xff00) >> 8));
97 		}
98 #else
99 			return(0xa4bf + MIDDLE_SOUND((unsigned int)comb2)
100 				- MIDDLE_SOUND((unsigned int)comb2)/4 - 2);
101 #endif
102 		return(K_ILLEGAL);
103 	}
104 
105 	/*
106 	 * Existence check
107 	 */
108 
109 	Cfbit = cmp_bitmap[Ci_val][V_val] ;
110 	for (disp = 0, k = 0; k < Cf_val; k++)
111 		{
112 			if (Cfbit & BIT_MASK)
113 				disp++ ;
114 			Cfbit >>= 1    ;
115 		}
116 
117 	if (!(Cfbit & BIT_MASK))	/* check Ci-val'th bit */
118 		return(K_ILLEGAL) ;		/* non-existence       */
119 
120 	/* Find 2 byte completion code	*/
121 
122 	comp2 = cmp_srchtbl[Ci_val][V_val] + disp ;
123 	mask  = cmp_srchtbl[Ci_val][V_val] & 0xff ;
124 
125 	if ((mask + disp) > 0xfe)
126 		comp2 += SKIP ;
127 #if defined(i386) || defined(__ppc)
128 	return(((comp2 & 0x00ff) << 8)|((comp2 & 0xff00) >> 8));
129 #else
130 	return(comp2);
131 #endif
132 }
133 
134 #ifdef TESTP
135 main()	/* This main portion is just for test */
136 {
137 	unsigned int comb2, comp2;
138 	for(;;) {
139 	scanf("%x",&comb2);
140 	comp2 = packtocomp(comb2);
141 	printf("/n completion code = 0x%x\n", comp2);
142 	}
143 }
144 #endif
145