xref: /illumos-gate/usr/src/lib/iconv_modules/ko/common/c2p.c (revision e616c49c963c6536f26b6d8888bc4bbb54716901)
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 /* $Id: c2p.c,v 1.12 1997/10/31 16:16:56 binz Exp $ SMI: ALE */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  *  Convert 2 byte completion  code to
30  *	    2 byte combination code
31  *  1) find the sequential No. of initial and middle sounds.
32  *  2) decide the displacement of final sound from the starting point.
33  *  3) combine each sounds into combination code.
34  */
35 
36 #include 	<stdio.h>
37 #include	"kdefs.h"
38 #include	"ktable.h"
39 
40 #define		SKIP	0xa1 + 0xff - 0xfe
41 
42 #define 	CI_CNT  19 - 1
43 #define		V_CNT	21 - 1
44 
45 /* KS C 5601-1986 */
46 KCHAR c2p(comp2)
47 KCHAR comp2;
48 {
49 
50 	KCHAR	comb2;
51 	short 	Ci_val;		/* initial sound */
52 	short	V_val ;		/* middle  sound */
53 	short	Cf_val;		/* final   sound */
54 	short	mask  ;
55 	short 	high = CI_CNT ;
56 	short	low  = 0      ;
57 
58 	int	disp,cnt;
59 
60 	long	Cfbit ;
61 
62 /*
63  * Find initial sound (Ci_val) and
64  *	middle  sound (V_val )
65  * which make the starting point for 'comp2'.
66  */
67 
68 	for (;;) {
69 		Ci_val = (low + high) / 2 ;
70 		if (low >= high)
71 			break ;
72 		if (comp2 < cmp_srchtbl[Ci_val][0])
73 			high = Ci_val - 1 ;
74 		else if (comp2 < cmp_srchtbl[Ci_val+1][0])
75 			break ;
76 		     else low = Ci_val + 1 ;
77 	}
78 
79 	V_val = 1;
80 	while(1) {
81 		if (comp2 < cmp_srchtbl[Ci_val][V_val]) {
82 			while(cmp_srchtbl[Ci_val][--V_val] == 0)
83 				;
84 			break;
85 		}else if (V_val == V_CNT)
86 			break ;
87 		V_val++;
88 	}
89 
90 	/* Find displacement (temporary final sound value) */
91 
92 	disp  = comp2 - cmp_srchtbl[Ci_val][V_val] ;
93 	mask  = cmp_srchtbl[Ci_val][V_val] & BYTE_MASK  ;
94 
95 	if ((mask + disp) > 0xfe)
96 		disp -= SKIP ;
97 
98 	/* Find the value of final sound */
99 
100 	Cfbit = cmp_bitmap[Ci_val][V_val] ;
101 	for (cnt = -1 , Cf_val = -1; cnt < disp; Cf_val++)
102 		{
103 			if (Cfbit & BIT_MASK)
104 				cnt++ ;
105 			Cfbit >>= 1   ;
106 		}
107 
108 	/* make 2 byte combination code	*/
109 
110 	comb2  = (unsigned int) (Ci_val + 0x0a) ;
111 	comb2  = (comb2 << 5) | (V_val + (V_val + 1)/3 + 2) ;
112 	comb2  = (comb2 << 5) | Cf_val ;
113 
114 	return(comb2 | 0x8000) ;
115 }
116 
117 /* KS C 5601-1992 */
118 KCHAR c2j(comp2)
119 KCHAR comp2;
120 {
121 
122 	KCHAR	comb2;
123 	short 	Ci_val;		/* initial sound */
124 	short	V_val ;		/* middle  sound */
125 	short	Cf_val;		/* final   sound */
126 	short	mask  ;
127 	short 	high = CI_CNT ;
128 	short	low  = 0      ;
129 
130 	int	disp,cnt;
131 
132 	long	Cfbit ;
133 
134 /*
135  * Find initial sound (Ci_val) and
136  *	middle  sound (V_val )
137  * which make the starting point for 'comp2'.
138  */
139 
140 	for (;;) {
141 		Ci_val = (low + high) / 2 ;
142 		if (low >= high)
143 			break ;
144 		if (comp2 < cmp_srchtbl[Ci_val][0])
145 			high = Ci_val - 1 ;
146 		else if (comp2 < cmp_srchtbl[Ci_val+1][0])
147 			break ;
148 		     else low = Ci_val + 1 ;
149 	}
150 
151 	V_val = 1;
152 	while(1) {
153 		if (comp2 < cmp_srchtbl[Ci_val][V_val]) {
154 			while(cmp_srchtbl[Ci_val][--V_val] == 0)
155 				;
156 			break;
157 		}else if (V_val == V_CNT)
158 			break ;
159 		V_val++;
160 	}
161 
162 	/* Find displacement (temporary final sound value) */
163 
164 	disp  = comp2 - cmp_srchtbl[Ci_val][V_val] ;
165 	mask  = cmp_srchtbl[Ci_val][V_val] & BYTE_MASK  ;
166 
167 	if ((mask + disp) > 0xfe)
168 		disp -= SKIP ;
169 
170 	/* Find the value of final sound */
171 
172 	Cfbit = cmp_bitmap[Ci_val][V_val] ;
173 	for (cnt = -1 , Cf_val = -1; cnt < disp; Cf_val++)
174 		{
175 			if (Cfbit & BIT_MASK)
176 				cnt++ ;
177 			Cfbit >>= 1   ;
178 		}
179 
180 	/* make 2 byte combination code	*/
181 
182 	comb2 = (unsigned int) (Ci_val + 2);
183 	comb2 = (comb2 << 5) | (V_val + (V_val + 1) / 6 * 2 + 3);
184 	comb2 = (comb2 << 5) | (Cf_val + (Cf_val) / 18);
185 
186 
187 	return(comb2 | 0x8000) ;
188 }
189 
190 #ifdef TESTPRINT
191 main()
192 {
193 	unsigned short comp2, comb2;
194 	int i,j;
195 
196 	printf("\ncompletion code       combination code\n");
197 	for (i=0xb0;i<=0xc8;i++) {
198 		for (j=0xa1;j<=0xfe;j++) {
199 			comp2 = i<<8|j;
200 			comb2 = comptopack(comp2);
201 			printf("    %4x                        %4x\n", comp2,comb2);
202 		}
203 	}
204 }
205 #endif
206