xref: /haiku/src/libs/iconv/tcvn.h (revision aef5731f38da6f7b913e0f64acd8a40555491ce5)
1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe  * Copyright (C) 1999-2002, 2004 Free Software Foundation, Inc.
3*aef5731fSOliver Tappe  * This file is part of the GNU LIBICONV Library.
4*aef5731fSOliver Tappe  *
5*aef5731fSOliver Tappe  * The GNU LIBICONV Library is free software; you can redistribute it
6*aef5731fSOliver Tappe  * and/or modify it under the terms of the GNU Library General Public
7*aef5731fSOliver Tappe  * License as published by the Free Software Foundation; either version 2
8*aef5731fSOliver Tappe  * of the License, or (at your option) any later version.
9*aef5731fSOliver Tappe  *
10*aef5731fSOliver Tappe  * The GNU LIBICONV Library is distributed in the hope that it will be
11*aef5731fSOliver Tappe  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12*aef5731fSOliver Tappe  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*aef5731fSOliver Tappe  * Library General Public License for more details.
14*aef5731fSOliver Tappe  *
15*aef5731fSOliver Tappe  * You should have received a copy of the GNU Library General Public
16*aef5731fSOliver Tappe  * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17*aef5731fSOliver Tappe  * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
18*aef5731fSOliver Tappe  * Fifth Floor, Boston, MA 02110-1301, USA.
19*aef5731fSOliver Tappe  */
20*aef5731fSOliver Tappe 
21*aef5731fSOliver Tappe /*
22*aef5731fSOliver Tappe  * TCVN-5712
23*aef5731fSOliver Tappe  */
24*aef5731fSOliver Tappe 
25*aef5731fSOliver Tappe #include "flushwc.h"
26*aef5731fSOliver Tappe #include "vietcomb.h"
27*aef5731fSOliver Tappe 
28*aef5731fSOliver Tappe static const unsigned char tcvn_comb_table[] = {
29*aef5731fSOliver Tappe   0xb0, 0xb3, 0xb2, 0xb1, 0xb4,
30*aef5731fSOliver Tappe };
31*aef5731fSOliver Tappe 
32*aef5731fSOliver Tappe /* The possible bases in viet_comp_table_data:
33*aef5731fSOliver Tappe    0x0041..0x0045, 0x0047..0x0049, 0x004B..0x0050, 0x0052..0x0057,
34*aef5731fSOliver Tappe    0x0059..0x005A, 0x0061..0x0065, 0x0067..0x0069, 0x006B..0x0070,
35*aef5731fSOliver Tappe    0x0072..0x0077, 0x0079..0x007A, 0x00A5, 0x00C2, 0x00CA, 0x00D3..0x00D6,
36*aef5731fSOliver Tappe    0x00DA, 0x00E2, 0x00EA, 0x00F3..0x00F6, 0x00FA, 0x0102..0x0103,
37*aef5731fSOliver Tappe    0x0168..0x0169, 0x01A0..0x01A1, 0x01AF..0x01B0. */
38*aef5731fSOliver Tappe static const unsigned int tcvn_comp_bases[] = {
39*aef5731fSOliver Tappe   0x06fdfbbe, 0x06fdfbbe, 0x00000000, 0x00000020, 0x04780404, 0x04780404,
40*aef5731fSOliver Tappe   0x0000000c, 0x00000000, 0x00000000, 0x00000300, 0x00000000, 0x00018003
41*aef5731fSOliver Tappe };
42*aef5731fSOliver Tappe 
43*aef5731fSOliver Tappe static const unsigned short tcvn_2uni_1[24] = {
44*aef5731fSOliver Tappe   /* 0x00 */
45*aef5731fSOliver Tappe   0x0000, 0x00da, 0x1ee4, 0x0003, 0x1eea, 0x1eec, 0x1eee, 0x0007,
46*aef5731fSOliver Tappe   0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
47*aef5731fSOliver Tappe   /* 0x10 */
48*aef5731fSOliver Tappe   0x0010, 0x1ee8, 0x1ef0, 0x1ef2, 0x1ef6, 0x1ef8, 0x00dd, 0x1ef4,
49*aef5731fSOliver Tappe };
50*aef5731fSOliver Tappe static const unsigned short tcvn_2uni_2[128] = {
51*aef5731fSOliver Tappe   /* 0x80 */
52*aef5731fSOliver Tappe   0x00c0, 0x1ea2, 0x00c3, 0x00c1, 0x1ea0, 0x1eb6, 0x1eac, 0x00c8,
53*aef5731fSOliver Tappe   0x1eba, 0x1ebc, 0x00c9, 0x1eb8, 0x1ec6, 0x00cc, 0x1ec8, 0x0128,
54*aef5731fSOliver Tappe   /* 0x90 */
55*aef5731fSOliver Tappe   0x00cd, 0x1eca, 0x00d2, 0x1ece, 0x00d5, 0x00d3, 0x1ecc, 0x1ed8,
56*aef5731fSOliver Tappe   0x1edc, 0x1ede, 0x1ee0, 0x1eda, 0x1ee2, 0x00d9, 0x1ee6, 0x0168,
57*aef5731fSOliver Tappe   /* 0xa0 */
58*aef5731fSOliver Tappe   0x00a0, 0x0102, 0x00c2, 0x00ca, 0x00d4, 0x01a0, 0x01af, 0x0110,
59*aef5731fSOliver Tappe   0x0103, 0x00e2, 0x00ea, 0x00f4, 0x01a1, 0x01b0, 0x0111, 0x1eb0,
60*aef5731fSOliver Tappe   /* 0xb0 */
61*aef5731fSOliver Tappe   0x0300, 0x0309, 0x0303, 0x0301, 0x0323, 0x00e0, 0x1ea3, 0x00e3,
62*aef5731fSOliver Tappe   0x00e1, 0x1ea1, 0x1eb2, 0x1eb1, 0x1eb3, 0x1eb5, 0x1eaf, 0x1eb4,
63*aef5731fSOliver Tappe   /* 0xc0 */
64*aef5731fSOliver Tappe   0x1eae, 0x1ea6, 0x1ea8, 0x1eaa, 0x1ea4, 0x1ec0, 0x1eb7, 0x1ea7,
65*aef5731fSOliver Tappe   0x1ea9, 0x1eab, 0x1ea5, 0x1ead, 0x00e8, 0x1ec2, 0x1ebb, 0x1ebd,
66*aef5731fSOliver Tappe   /* 0xd0 */
67*aef5731fSOliver Tappe   0x00e9, 0x1eb9, 0x1ec1, 0x1ec3, 0x1ec5, 0x1ebf, 0x1ec7, 0x00ec,
68*aef5731fSOliver Tappe   0x1ec9, 0x1ec4, 0x1ebe, 0x1ed2, 0x0129, 0x00ed, 0x1ecb, 0x00f2,
69*aef5731fSOliver Tappe   /* 0xe0 */
70*aef5731fSOliver Tappe   0x1ed4, 0x1ecf, 0x00f5, 0x00f3, 0x1ecd, 0x1ed3, 0x1ed5, 0x1ed7,
71*aef5731fSOliver Tappe   0x1ed1, 0x1ed9, 0x1edd, 0x1edf, 0x1ee1, 0x1edb, 0x1ee3, 0x00f9,
72*aef5731fSOliver Tappe   /* 0xf0 */
73*aef5731fSOliver Tappe   0x1ed6, 0x1ee7, 0x0169, 0x00fa, 0x1ee5, 0x1eeb, 0x1eed, 0x1eef,
74*aef5731fSOliver Tappe   0x1ee9, 0x1ef1, 0x1ef3, 0x1ef7, 0x1ef9, 0x00fd, 0x1ef5, 0x1ed0,
75*aef5731fSOliver Tappe };
76*aef5731fSOliver Tappe 
77*aef5731fSOliver Tappe /* In the TCVN to Unicode direction, the state contains a buffered
78*aef5731fSOliver Tappe    character, or 0 if none. */
79*aef5731fSOliver Tappe 
80*aef5731fSOliver Tappe static int
tcvn_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)81*aef5731fSOliver Tappe tcvn_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
82*aef5731fSOliver Tappe {
83*aef5731fSOliver Tappe   unsigned char c = *s;
84*aef5731fSOliver Tappe   unsigned short wc;
85*aef5731fSOliver Tappe   unsigned short last_wc;
86*aef5731fSOliver Tappe   if (c < 0x18)
87*aef5731fSOliver Tappe     wc = tcvn_2uni_1[c];
88*aef5731fSOliver Tappe   else if (c < 0x80)
89*aef5731fSOliver Tappe     wc = c;
90*aef5731fSOliver Tappe   else
91*aef5731fSOliver Tappe     wc = tcvn_2uni_2[c-0x80];
92*aef5731fSOliver Tappe   last_wc = conv->istate;
93*aef5731fSOliver Tappe   if (last_wc) {
94*aef5731fSOliver Tappe     if (wc >= 0x0300 && wc < 0x0340) {
95*aef5731fSOliver Tappe       /* See whether last_wc and wc can be combined. */
96*aef5731fSOliver Tappe       unsigned int k;
97*aef5731fSOliver Tappe       unsigned int i1, i2;
98*aef5731fSOliver Tappe       switch (wc) {
99*aef5731fSOliver Tappe         case 0x0300: k = 0; break;
100*aef5731fSOliver Tappe         case 0x0301: k = 1; break;
101*aef5731fSOliver Tappe         case 0x0303: k = 2; break;
102*aef5731fSOliver Tappe         case 0x0309: k = 3; break;
103*aef5731fSOliver Tappe         case 0x0323: k = 4; break;
104*aef5731fSOliver Tappe         default: abort();
105*aef5731fSOliver Tappe       }
106*aef5731fSOliver Tappe       i1 = viet_comp_table[k].idx;
107*aef5731fSOliver Tappe       i2 = i1 + viet_comp_table[k].len-1;
108*aef5731fSOliver Tappe       if (last_wc >= viet_comp_table_data[i1].base
109*aef5731fSOliver Tappe           && last_wc <= viet_comp_table_data[i2].base) {
110*aef5731fSOliver Tappe         unsigned int i;
111*aef5731fSOliver Tappe         for (;;) {
112*aef5731fSOliver Tappe           i = (i1+i2)>>1;
113*aef5731fSOliver Tappe           if (last_wc == viet_comp_table_data[i].base)
114*aef5731fSOliver Tappe             break;
115*aef5731fSOliver Tappe           if (last_wc < viet_comp_table_data[i].base) {
116*aef5731fSOliver Tappe             if (i1 == i)
117*aef5731fSOliver Tappe               goto not_combining;
118*aef5731fSOliver Tappe             i2 = i;
119*aef5731fSOliver Tappe           } else {
120*aef5731fSOliver Tappe             if (i1 != i)
121*aef5731fSOliver Tappe               i1 = i;
122*aef5731fSOliver Tappe             else {
123*aef5731fSOliver Tappe               i = i2;
124*aef5731fSOliver Tappe               if (last_wc == viet_comp_table_data[i].base)
125*aef5731fSOliver Tappe                 break;
126*aef5731fSOliver Tappe               goto not_combining;
127*aef5731fSOliver Tappe             }
128*aef5731fSOliver Tappe           }
129*aef5731fSOliver Tappe         }
130*aef5731fSOliver Tappe         last_wc = viet_comp_table_data[i].composed;
131*aef5731fSOliver Tappe         /* Output the combined character. */
132*aef5731fSOliver Tappe         conv->istate = 0;
133*aef5731fSOliver Tappe         *pwc = (ucs4_t) last_wc;
134*aef5731fSOliver Tappe         return 1;
135*aef5731fSOliver Tappe       }
136*aef5731fSOliver Tappe     }
137*aef5731fSOliver Tappe   not_combining:
138*aef5731fSOliver Tappe     /* Output the buffered character. */
139*aef5731fSOliver Tappe     conv->istate = 0;
140*aef5731fSOliver Tappe     *pwc = (ucs4_t) last_wc;
141*aef5731fSOliver Tappe     return 0; /* Don't advance the input pointer. */
142*aef5731fSOliver Tappe   }
143*aef5731fSOliver Tappe   if (wc >= 0x0041 && wc <= 0x01b0
144*aef5731fSOliver Tappe       && ((tcvn_comp_bases[(wc - 0x0040) >> 5] >> (wc & 0x1f)) & 1)) {
145*aef5731fSOliver Tappe     /* wc is a possible match in viet_comp_table_data. Buffer it. */
146*aef5731fSOliver Tappe     conv->istate = wc;
147*aef5731fSOliver Tappe     return RET_TOOFEW(1);
148*aef5731fSOliver Tappe   } else {
149*aef5731fSOliver Tappe     /* Output wc immediately. */
150*aef5731fSOliver Tappe     *pwc = (ucs4_t) wc;
151*aef5731fSOliver Tappe     return 1;
152*aef5731fSOliver Tappe   }
153*aef5731fSOliver Tappe }
154*aef5731fSOliver Tappe 
155*aef5731fSOliver Tappe #define tcvn_flushwc normal_flushwc
156*aef5731fSOliver Tappe 
157*aef5731fSOliver Tappe static const unsigned char tcvn_page00[96+184] = {
158*aef5731fSOliver Tappe   0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0-0xa7 */
159*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa8-0xaf */
160*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0-0xb7 */
161*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb8-0xbf */
162*aef5731fSOliver Tappe   0x80, 0x83, 0xa2, 0x82, 0x00, 0x00, 0x00, 0x00, /* 0xc0-0xc7 */
163*aef5731fSOliver Tappe   0x87, 0x8a, 0xa3, 0x00, 0x8d, 0x90, 0x00, 0x00, /* 0xc8-0xcf */
164*aef5731fSOliver Tappe   0x00, 0x00, 0x92, 0x95, 0xa4, 0x94, 0x00, 0x00, /* 0xd0-0xd7 */
165*aef5731fSOliver Tappe   0x00, 0x9d, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00, /* 0xd8-0xdf */
166*aef5731fSOliver Tappe   0xb5, 0xb8, 0xa9, 0xb7, 0x00, 0x00, 0x00, 0x00, /* 0xe0-0xe7 */
167*aef5731fSOliver Tappe   0xcc, 0xd0, 0xaa, 0x00, 0xd7, 0xdd, 0x00, 0x00, /* 0xe8-0xef */
168*aef5731fSOliver Tappe   0x00, 0x00, 0xdf, 0xe3, 0xab, 0xe2, 0x00, 0x00, /* 0xf0-0xf7 */
169*aef5731fSOliver Tappe   0x00, 0xef, 0xf3, 0x00, 0x00, 0xfd, 0x00, 0x00, /* 0xf8-0xff */
170*aef5731fSOliver Tappe   /* 0x0100 */
171*aef5731fSOliver Tappe   0x00, 0x00, 0xa1, 0xa8, 0x00, 0x00, 0x00, 0x00, /* 0x00-0x07 */
172*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x08-0x0f */
173*aef5731fSOliver Tappe   0xa7, 0xae, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x10-0x17 */
174*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x18-0x1f */
175*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x20-0x27 */
176*aef5731fSOliver Tappe   0x8f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x28-0x2f */
177*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x30-0x37 */
178*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x38-0x3f */
179*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40-0x47 */
180*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x48-0x4f */
181*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x50-0x57 */
182*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x58-0x5f */
183*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x60-0x67 */
184*aef5731fSOliver Tappe   0x9f, 0xf2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x68-0x6f */
185*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70-0x77 */
186*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x78-0x7f */
187*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80-0x87 */
188*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x88-0x8f */
189*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90-0x97 */
190*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x98-0x9f */
191*aef5731fSOliver Tappe   0xa5, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0-0xa7 */
192*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, /* 0xa8-0xaf */
193*aef5731fSOliver Tappe   0xad, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0-0xb7 */
194*aef5731fSOliver Tappe };
195*aef5731fSOliver Tappe static const unsigned char tcvn_page03[40] = {
196*aef5731fSOliver Tappe   0xb0, 0xb3, 0x00, 0xb2, 0x00, 0x00, 0x00, 0x00, /* 0x00-0x07 */
197*aef5731fSOliver Tappe   0x00, 0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x08-0x0f */
198*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x10-0x17 */
199*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x18-0x1f */
200*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0xb4, 0x00, 0x00, 0x00, 0x00, /* 0x20-0x27 */
201*aef5731fSOliver Tappe };
202*aef5731fSOliver Tappe static const unsigned char tcvn_page1e[96] = {
203*aef5731fSOliver Tappe   0x84, 0xb9, 0x81, 0xb6, 0xc4, 0xca, 0xc1, 0xc7, /* 0xa0-0xa7 */
204*aef5731fSOliver Tappe   0xc2, 0xc8, 0xc3, 0xc9, 0x86, 0xcb, 0xc0, 0xbe, /* 0xa8-0xaf */
205*aef5731fSOliver Tappe   0xaf, 0xbb, 0xba, 0xbc, 0xbf, 0xbd, 0x85, 0xc6, /* 0xb0-0xb7 */
206*aef5731fSOliver Tappe   0x8b, 0xd1, 0x88, 0xce, 0x89, 0xcf, 0xda, 0xd5, /* 0xb8-0xbf */
207*aef5731fSOliver Tappe   0xc5, 0xd2, 0xcd, 0xd3, 0xd9, 0xd4, 0x8c, 0xd6, /* 0xc0-0xc7 */
208*aef5731fSOliver Tappe   0x8e, 0xd8, 0x91, 0xde, 0x96, 0xe4, 0x93, 0xe1, /* 0xc8-0xcf */
209*aef5731fSOliver Tappe   0xff, 0xe8, 0xdb, 0xe5, 0xe0, 0xe6, 0xf0, 0xe7, /* 0xd0-0xd7 */
210*aef5731fSOliver Tappe   0x97, 0xe9, 0x9b, 0xed, 0x98, 0xea, 0x99, 0xeb, /* 0xd8-0xdf */
211*aef5731fSOliver Tappe   0x9a, 0xec, 0x9c, 0xee, 0x02, 0xf4, 0x9e, 0xf1, /* 0xe0-0xe7 */
212*aef5731fSOliver Tappe   0x11, 0xf8, 0x04, 0xf5, 0x05, 0xf6, 0x06, 0xf7, /* 0xe8-0xef */
213*aef5731fSOliver Tappe   0x12, 0xf9, 0x13, 0xfa, 0x17, 0xfe, 0x14, 0xfb, /* 0xf0-0xf7 */
214*aef5731fSOliver Tappe   0x15, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xf8-0xff */
215*aef5731fSOliver Tappe };
216*aef5731fSOliver Tappe 
217*aef5731fSOliver Tappe static int
tcvn_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)218*aef5731fSOliver Tappe tcvn_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
219*aef5731fSOliver Tappe {
220*aef5731fSOliver Tappe   unsigned char c = 0;
221*aef5731fSOliver Tappe   if (wc < 0x0080 && (wc >= 0x0020 || (0x00fe0076 & (1 << wc)) == 0)) {
222*aef5731fSOliver Tappe     *r = wc;
223*aef5731fSOliver Tappe     return 1;
224*aef5731fSOliver Tappe   }
225*aef5731fSOliver Tappe   else if (wc >= 0x00a0 && wc < 0x01b8)
226*aef5731fSOliver Tappe     c = tcvn_page00[wc-0x00a0];
227*aef5731fSOliver Tappe   else if (wc >= 0x0300 && wc < 0x0328)
228*aef5731fSOliver Tappe     c = tcvn_page03[wc-0x0300];
229*aef5731fSOliver Tappe   else if (wc >= 0x0340 && wc < 0x0342) /* deprecated Vietnamese tone marks */
230*aef5731fSOliver Tappe     c = tcvn_page03[wc-0x0340];
231*aef5731fSOliver Tappe   else if (wc >= 0x1ea0 && wc < 0x1f00)
232*aef5731fSOliver Tappe     c = tcvn_page1e[wc-0x1ea0];
233*aef5731fSOliver Tappe   if (c != 0) {
234*aef5731fSOliver Tappe     *r = c;
235*aef5731fSOliver Tappe     return 1;
236*aef5731fSOliver Tappe   }
237*aef5731fSOliver Tappe   /* Try compatibility or canonical decomposition. */
238*aef5731fSOliver Tappe   {
239*aef5731fSOliver Tappe     /* Binary search through viet_decomp_table. */
240*aef5731fSOliver Tappe     unsigned int i1 = 0;
241*aef5731fSOliver Tappe     unsigned int i2 = sizeof(viet_decomp_table)/sizeof(viet_decomp_table[0])-1;
242*aef5731fSOliver Tappe     if (wc >= viet_decomp_table[i1].composed
243*aef5731fSOliver Tappe         && wc <= viet_decomp_table[i2].composed) {
244*aef5731fSOliver Tappe       unsigned int i;
245*aef5731fSOliver Tappe       for (;;) {
246*aef5731fSOliver Tappe         /* Here i2 - i1 > 0. */
247*aef5731fSOliver Tappe         i = (i1+i2)>>1;
248*aef5731fSOliver Tappe         if (wc == viet_decomp_table[i].composed)
249*aef5731fSOliver Tappe           break;
250*aef5731fSOliver Tappe         if (wc < viet_decomp_table[i].composed) {
251*aef5731fSOliver Tappe           if (i1 == i)
252*aef5731fSOliver Tappe             return RET_ILUNI;
253*aef5731fSOliver Tappe           /* Here i1 < i < i2. */
254*aef5731fSOliver Tappe           i2 = i;
255*aef5731fSOliver Tappe         } else {
256*aef5731fSOliver Tappe           /* Here i1 <= i < i2. */
257*aef5731fSOliver Tappe           if (i1 != i)
258*aef5731fSOliver Tappe             i1 = i;
259*aef5731fSOliver Tappe           else {
260*aef5731fSOliver Tappe             /* Here i2 - i1 = 1. */
261*aef5731fSOliver Tappe             i = i2;
262*aef5731fSOliver Tappe             if (wc == viet_decomp_table[i].composed)
263*aef5731fSOliver Tappe               break;
264*aef5731fSOliver Tappe             else
265*aef5731fSOliver Tappe               return RET_ILUNI;
266*aef5731fSOliver Tappe           }
267*aef5731fSOliver Tappe         }
268*aef5731fSOliver Tappe       }
269*aef5731fSOliver Tappe       /* Found a compatibility or canonical decomposition. */
270*aef5731fSOliver Tappe       wc = viet_decomp_table[i].base;
271*aef5731fSOliver Tappe       /* wc is one of 0x0020, 0x0041..0x005a, 0x0061..0x007a, 0x00a5, 0x00a8,
272*aef5731fSOliver Tappe          0x00c2, 0x00c5..0x00c7, 0x00ca, 0x00cf, 0x00d3, 0x00d4, 0x00d6,
273*aef5731fSOliver Tappe          0x00d8, 0x00da, 0x00dc, 0x00e2, 0x00e5..0x00e7, 0x00ea, 0x00ef,
274*aef5731fSOliver Tappe          0x00f3, 0x00f4, 0x00f6, 0x00f8, 0x00fc, 0x0102, 0x0103, 0x01a0,
275*aef5731fSOliver Tappe          0x01a1, 0x01af, 0x01b0. */
276*aef5731fSOliver Tappe       if (wc < 0x0080)
277*aef5731fSOliver Tappe         c = wc;
278*aef5731fSOliver Tappe       else {
279*aef5731fSOliver Tappe         c = tcvn_page00[wc-0x00a0];
280*aef5731fSOliver Tappe         if (c == 0)
281*aef5731fSOliver Tappe           return RET_ILUNI;
282*aef5731fSOliver Tappe       }
283*aef5731fSOliver Tappe       if (n < 2)
284*aef5731fSOliver Tappe         return RET_TOOSMALL;
285*aef5731fSOliver Tappe       r[0] = c;
286*aef5731fSOliver Tappe       r[1] = tcvn_comb_table[viet_decomp_table[i].comb1];
287*aef5731fSOliver Tappe       return 2;
288*aef5731fSOliver Tappe     }
289*aef5731fSOliver Tappe   }
290*aef5731fSOliver Tappe   return RET_ILUNI;
291*aef5731fSOliver Tappe }
292