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