xref: /haiku/src/libs/iconv/cp1255.h (revision aef5731f38da6f7b913e0f64acd8a40555491ce5)
1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe  * Copyright (C) 1999-2001, 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  * CP1255
23*aef5731fSOliver Tappe  */
24*aef5731fSOliver Tappe 
25*aef5731fSOliver Tappe #include "flushwc.h"
26*aef5731fSOliver Tappe 
27*aef5731fSOliver Tappe /* Combining characters used in Hebrew encoding CP1255. */
28*aef5731fSOliver Tappe 
29*aef5731fSOliver Tappe /* Relevant combining characters:
30*aef5731fSOliver Tappe    0x05b4, 0x05b7, 0x05b8, 0x05b9, 0x05bc, 0x05bf, 0x05c1, 0x05c2. */
31*aef5731fSOliver Tappe 
32*aef5731fSOliver Tappe /* Composition tables for each of the relevant combining characters. */
33*aef5731fSOliver Tappe static const struct { unsigned short base; unsigned short composed; } cp1255_comp_table_data[] = {
34*aef5731fSOliver Tappe #define cp1255_comp_table05b4_idx 0
35*aef5731fSOliver Tappe #define cp1255_comp_table05b4_len 1
36*aef5731fSOliver Tappe   { 0x05D9, 0xFB1D },
37*aef5731fSOliver Tappe #define cp1255_comp_table05b7_idx (cp1255_comp_table05b4_idx+cp1255_comp_table05b4_len)
38*aef5731fSOliver Tappe #define cp1255_comp_table05b7_len 2
39*aef5731fSOliver Tappe   { 0x05D0, 0xFB2E },
40*aef5731fSOliver Tappe   { 0x05F2, 0xFB1F },
41*aef5731fSOliver Tappe #define cp1255_comp_table05b8_idx (cp1255_comp_table05b7_idx+cp1255_comp_table05b7_len)
42*aef5731fSOliver Tappe #define cp1255_comp_table05b8_len 1
43*aef5731fSOliver Tappe   { 0x05D0, 0xFB2F },
44*aef5731fSOliver Tappe #define cp1255_comp_table05b9_idx (cp1255_comp_table05b8_idx+cp1255_comp_table05b8_len)
45*aef5731fSOliver Tappe #define cp1255_comp_table05b9_len 1
46*aef5731fSOliver Tappe   { 0x05D5, 0xFB4B },
47*aef5731fSOliver Tappe #define cp1255_comp_table05bc_idx (cp1255_comp_table05b9_idx+cp1255_comp_table05b9_len)
48*aef5731fSOliver Tappe #define cp1255_comp_table05bc_len 24
49*aef5731fSOliver Tappe   { 0x05D0, 0xFB30 },
50*aef5731fSOliver Tappe   { 0x05D1, 0xFB31 },
51*aef5731fSOliver Tappe   { 0x05D2, 0xFB32 },
52*aef5731fSOliver Tappe   { 0x05D3, 0xFB33 },
53*aef5731fSOliver Tappe   { 0x05D4, 0xFB34 },
54*aef5731fSOliver Tappe   { 0x05D5, 0xFB35 },
55*aef5731fSOliver Tappe   { 0x05D6, 0xFB36 },
56*aef5731fSOliver Tappe   { 0x05D8, 0xFB38 },
57*aef5731fSOliver Tappe   { 0x05D9, 0xFB39 },
58*aef5731fSOliver Tappe   { 0x05DA, 0xFB3A },
59*aef5731fSOliver Tappe   { 0x05DB, 0xFB3B },
60*aef5731fSOliver Tappe   { 0x05DC, 0xFB3C },
61*aef5731fSOliver Tappe   { 0x05DE, 0xFB3E },
62*aef5731fSOliver Tappe   { 0x05E0, 0xFB40 },
63*aef5731fSOliver Tappe   { 0x05E1, 0xFB41 },
64*aef5731fSOliver Tappe   { 0x05E3, 0xFB43 },
65*aef5731fSOliver Tappe   { 0x05E4, 0xFB44 },
66*aef5731fSOliver Tappe   { 0x05E6, 0xFB46 },
67*aef5731fSOliver Tappe   { 0x05E7, 0xFB47 },
68*aef5731fSOliver Tappe   { 0x05E8, 0xFB48 },
69*aef5731fSOliver Tappe   { 0x05E9, 0xFB49 },
70*aef5731fSOliver Tappe   { 0x05EA, 0xFB4A },
71*aef5731fSOliver Tappe   { 0xFB2A, 0xFB2C },
72*aef5731fSOliver Tappe   { 0xFB2B, 0xFB2D },
73*aef5731fSOliver Tappe #define cp1255_comp_table05bf_idx (cp1255_comp_table05bc_idx+cp1255_comp_table05bc_len)
74*aef5731fSOliver Tappe #define cp1255_comp_table05bf_len 3
75*aef5731fSOliver Tappe   { 0x05D1, 0xFB4C },
76*aef5731fSOliver Tappe   { 0x05DB, 0xFB4D },
77*aef5731fSOliver Tappe   { 0x05E4, 0xFB4E },
78*aef5731fSOliver Tappe #define cp1255_comp_table05c1_idx (cp1255_comp_table05bf_idx+cp1255_comp_table05bf_len)
79*aef5731fSOliver Tappe #define cp1255_comp_table05c1_len 2
80*aef5731fSOliver Tappe   { 0x05E9, 0xFB2A },
81*aef5731fSOliver Tappe   { 0xFB49, 0xFB2C },
82*aef5731fSOliver Tappe #define cp1255_comp_table05c2_idx (cp1255_comp_table05c1_idx+cp1255_comp_table05c1_len)
83*aef5731fSOliver Tappe #define cp1255_comp_table05c2_len 2
84*aef5731fSOliver Tappe   { 0x05E9, 0xFB2B },
85*aef5731fSOliver Tappe   { 0xFB49, 0xFB2D },
86*aef5731fSOliver Tappe };
87*aef5731fSOliver Tappe static const struct { unsigned int len; unsigned int idx; } cp1255_comp_table[] = {
88*aef5731fSOliver Tappe   { cp1255_comp_table05b4_len, cp1255_comp_table05b4_idx },
89*aef5731fSOliver Tappe   { cp1255_comp_table05b7_len, cp1255_comp_table05b7_idx },
90*aef5731fSOliver Tappe   { cp1255_comp_table05b8_len, cp1255_comp_table05b8_idx },
91*aef5731fSOliver Tappe   { cp1255_comp_table05b9_len, cp1255_comp_table05b9_idx },
92*aef5731fSOliver Tappe   { cp1255_comp_table05bc_len, cp1255_comp_table05bc_idx },
93*aef5731fSOliver Tappe   { cp1255_comp_table05bf_len, cp1255_comp_table05bf_idx },
94*aef5731fSOliver Tappe   { cp1255_comp_table05c1_len, cp1255_comp_table05c1_idx },
95*aef5731fSOliver Tappe   { cp1255_comp_table05c2_len, cp1255_comp_table05c2_idx },
96*aef5731fSOliver Tappe };
97*aef5731fSOliver Tappe 
98*aef5731fSOliver Tappe /* Decomposition table for the relevant Unicode characters. */
99*aef5731fSOliver Tappe struct cp1255_decomp { unsigned short composed; unsigned short base; int comb1 : 8; signed int comb2 : 8; };
100*aef5731fSOliver Tappe static const struct cp1255_decomp cp1255_decomp_table[] = {
101*aef5731fSOliver Tappe   { 0xFB1D, 0x05D9, 0, -1 },
102*aef5731fSOliver Tappe   { 0xFB1F, 0x05F2, 1, -1 },
103*aef5731fSOliver Tappe   { 0xFB2A, 0x05E9, 6, -1 },
104*aef5731fSOliver Tappe   { 0xFB2B, 0x05E9, 7, -1 },
105*aef5731fSOliver Tappe   { 0xFB2C, 0x05E9, 4, 6 },
106*aef5731fSOliver Tappe   { 0xFB2D, 0x05E9, 4, 7 },
107*aef5731fSOliver Tappe   { 0xFB2E, 0x05D0, 1, -1 },
108*aef5731fSOliver Tappe   { 0xFB2F, 0x05D0, 2, -1 },
109*aef5731fSOliver Tappe   { 0xFB30, 0x05D0, 4, -1 },
110*aef5731fSOliver Tappe   { 0xFB31, 0x05D1, 4, -1 },
111*aef5731fSOliver Tappe   { 0xFB32, 0x05D2, 4, -1 },
112*aef5731fSOliver Tappe   { 0xFB33, 0x05D3, 4, -1 },
113*aef5731fSOliver Tappe   { 0xFB34, 0x05D4, 4, -1 },
114*aef5731fSOliver Tappe   { 0xFB35, 0x05D5, 4, -1 },
115*aef5731fSOliver Tappe   { 0xFB36, 0x05D6, 4, -1 },
116*aef5731fSOliver Tappe   { 0xFB38, 0x05D8, 4, -1 },
117*aef5731fSOliver Tappe   { 0xFB39, 0x05D9, 4, -1 },
118*aef5731fSOliver Tappe   { 0xFB3A, 0x05DA, 4, -1 },
119*aef5731fSOliver Tappe   { 0xFB3B, 0x05DB, 4, -1 },
120*aef5731fSOliver Tappe   { 0xFB3C, 0x05DC, 4, -1 },
121*aef5731fSOliver Tappe   { 0xFB3E, 0x05DE, 4, -1 },
122*aef5731fSOliver Tappe   { 0xFB40, 0x05E0, 4, -1 },
123*aef5731fSOliver Tappe   { 0xFB41, 0x05E1, 4, -1 },
124*aef5731fSOliver Tappe   { 0xFB43, 0x05E3, 4, -1 },
125*aef5731fSOliver Tappe   { 0xFB44, 0x05E4, 4, -1 },
126*aef5731fSOliver Tappe   { 0xFB46, 0x05E6, 4, -1 },
127*aef5731fSOliver Tappe   { 0xFB47, 0x05E7, 4, -1 },
128*aef5731fSOliver Tappe   { 0xFB48, 0x05E8, 4, -1 },
129*aef5731fSOliver Tappe   { 0xFB49, 0x05E9, 4, -1 },
130*aef5731fSOliver Tappe   { 0xFB4A, 0x05EA, 4, -1 },
131*aef5731fSOliver Tappe   { 0xFB4B, 0x05D5, 3, -1 },
132*aef5731fSOliver Tappe   { 0xFB4C, 0x05D1, 5, -1 },
133*aef5731fSOliver Tappe   { 0xFB4D, 0x05DB, 5, -1 },
134*aef5731fSOliver Tappe   { 0xFB4E, 0x05E4, 5, -1 },
135*aef5731fSOliver Tappe };
136*aef5731fSOliver Tappe 
137*aef5731fSOliver Tappe static const unsigned char cp1255_comb_table[] = {
138*aef5731fSOliver Tappe   0xc4, 0xc7, 0xc8, 0xc9, 0xcc, 0xcf, 0xd1, 0xd2,
139*aef5731fSOliver Tappe };
140*aef5731fSOliver Tappe 
141*aef5731fSOliver Tappe static const unsigned short cp1255_2uni[128] = {
142*aef5731fSOliver Tappe   /* 0x80 */
143*aef5731fSOliver Tappe   0x20ac, 0xfffd, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
144*aef5731fSOliver Tappe   0x02c6, 0x2030, 0xfffd, 0x2039, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
145*aef5731fSOliver Tappe   /* 0x90 */
146*aef5731fSOliver Tappe   0xfffd, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
147*aef5731fSOliver Tappe   0x02dc, 0x2122, 0xfffd, 0x203a, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
148*aef5731fSOliver Tappe   /* 0xa0 */
149*aef5731fSOliver Tappe   0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x20aa, 0x00a5, 0x00a6, 0x00a7,
150*aef5731fSOliver Tappe   0x00a8, 0x00a9, 0x00d7, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
151*aef5731fSOliver Tappe   /* 0xb0 */
152*aef5731fSOliver Tappe   0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
153*aef5731fSOliver Tappe   0x00b8, 0x00b9, 0x00f7, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
154*aef5731fSOliver Tappe   /* 0xc0 */
155*aef5731fSOliver Tappe   0x05b0, 0x05b1, 0x05b2, 0x05b3, 0x05b4, 0x05b5, 0x05b6, 0x05b7,
156*aef5731fSOliver Tappe   0x05b8, 0x05b9, 0xfffd, 0x05bb, 0x05bc, 0x05bd, 0x05be, 0x05bf,
157*aef5731fSOliver Tappe   /* 0xd0 */
158*aef5731fSOliver Tappe   0x05c0, 0x05c1, 0x05c2, 0x05c3, 0x05f0, 0x05f1, 0x05f2, 0x05f3,
159*aef5731fSOliver Tappe   0x05f4, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd, 0xfffd,
160*aef5731fSOliver Tappe   /* 0xe0 */
161*aef5731fSOliver Tappe   0x05d0, 0x05d1, 0x05d2, 0x05d3, 0x05d4, 0x05d5, 0x05d6, 0x05d7,
162*aef5731fSOliver Tappe   0x05d8, 0x05d9, 0x05da, 0x05db, 0x05dc, 0x05dd, 0x05de, 0x05df,
163*aef5731fSOliver Tappe   /* 0xf0 */
164*aef5731fSOliver Tappe   0x05e0, 0x05e1, 0x05e2, 0x05e3, 0x05e4, 0x05e5, 0x05e6, 0x05e7,
165*aef5731fSOliver Tappe   0x05e8, 0x05e9, 0x05ea, 0xfffd, 0xfffd, 0x200e, 0x200f, 0xfffd,
166*aef5731fSOliver Tappe };
167*aef5731fSOliver Tappe 
168*aef5731fSOliver Tappe /* In the CP1255 to Unicode direction, the state contains a buffered
169*aef5731fSOliver Tappe    character, or 0 if none. */
170*aef5731fSOliver Tappe 
171*aef5731fSOliver Tappe static int
cp1255_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)172*aef5731fSOliver Tappe cp1255_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
173*aef5731fSOliver Tappe {
174*aef5731fSOliver Tappe   unsigned char c = *s;
175*aef5731fSOliver Tappe   unsigned short wc;
176*aef5731fSOliver Tappe   unsigned short last_wc;
177*aef5731fSOliver Tappe   if (c < 0x80) {
178*aef5731fSOliver Tappe     wc = c;
179*aef5731fSOliver Tappe   } else {
180*aef5731fSOliver Tappe     wc = cp1255_2uni[c-0x80];
181*aef5731fSOliver Tappe     if (wc == 0xfffd)
182*aef5731fSOliver Tappe       return RET_ILSEQ;
183*aef5731fSOliver Tappe   }
184*aef5731fSOliver Tappe   last_wc = conv->istate;
185*aef5731fSOliver Tappe   if (last_wc) {
186*aef5731fSOliver Tappe     if (wc >= 0x05b0 && wc < 0x05c5) {
187*aef5731fSOliver Tappe       /* See whether last_wc and wc can be combined. */
188*aef5731fSOliver Tappe       unsigned int k;
189*aef5731fSOliver Tappe       unsigned int i1, i2;
190*aef5731fSOliver Tappe       switch (wc) {
191*aef5731fSOliver Tappe         case 0x05b4: k = 0; break;
192*aef5731fSOliver Tappe         case 0x05b7: k = 1; break;
193*aef5731fSOliver Tappe         case 0x05b8: k = 2; break;
194*aef5731fSOliver Tappe         case 0x05b9: k = 3; break;
195*aef5731fSOliver Tappe         case 0x05bc: k = 4; break;
196*aef5731fSOliver Tappe         case 0x05bf: k = 5; break;
197*aef5731fSOliver Tappe         case 0x05c1: k = 6; break;
198*aef5731fSOliver Tappe         case 0x05c2: k = 7; break;
199*aef5731fSOliver Tappe         default: goto not_combining;
200*aef5731fSOliver Tappe       }
201*aef5731fSOliver Tappe       i1 = cp1255_comp_table[k].idx;
202*aef5731fSOliver Tappe       i2 = i1 + cp1255_comp_table[k].len-1;
203*aef5731fSOliver Tappe       if (last_wc >= cp1255_comp_table_data[i1].base
204*aef5731fSOliver Tappe           && last_wc <= cp1255_comp_table_data[i2].base) {
205*aef5731fSOliver Tappe         unsigned int i;
206*aef5731fSOliver Tappe         for (;;) {
207*aef5731fSOliver Tappe           i = (i1+i2)>>1;
208*aef5731fSOliver Tappe           if (last_wc == cp1255_comp_table_data[i].base)
209*aef5731fSOliver Tappe             break;
210*aef5731fSOliver Tappe           if (last_wc < cp1255_comp_table_data[i].base) {
211*aef5731fSOliver Tappe             if (i1 == i)
212*aef5731fSOliver Tappe               goto not_combining;
213*aef5731fSOliver Tappe             i2 = i;
214*aef5731fSOliver Tappe           } else {
215*aef5731fSOliver Tappe             if (i1 != i)
216*aef5731fSOliver Tappe               i1 = i;
217*aef5731fSOliver Tappe             else {
218*aef5731fSOliver Tappe               i = i2;
219*aef5731fSOliver Tappe               if (last_wc == cp1255_comp_table_data[i].base)
220*aef5731fSOliver Tappe                 break;
221*aef5731fSOliver Tappe               goto not_combining;
222*aef5731fSOliver Tappe             }
223*aef5731fSOliver Tappe           }
224*aef5731fSOliver Tappe         }
225*aef5731fSOliver Tappe         last_wc = cp1255_comp_table_data[i].composed;
226*aef5731fSOliver Tappe         if (last_wc == 0xfb2a || last_wc == 0xfb2b || last_wc == 0xfb49) {
227*aef5731fSOliver Tappe           /* Buffer the combined character. */
228*aef5731fSOliver Tappe           conv->istate = last_wc;
229*aef5731fSOliver Tappe           return RET_TOOFEW(1);
230*aef5731fSOliver Tappe         } else {
231*aef5731fSOliver Tappe           /* Output the combined character. */
232*aef5731fSOliver Tappe           conv->istate = 0;
233*aef5731fSOliver Tappe           *pwc = (ucs4_t) last_wc;
234*aef5731fSOliver Tappe           return 1;
235*aef5731fSOliver Tappe         }
236*aef5731fSOliver Tappe       }
237*aef5731fSOliver Tappe     }
238*aef5731fSOliver Tappe   not_combining:
239*aef5731fSOliver Tappe     /* Output the buffered character. */
240*aef5731fSOliver Tappe     conv->istate = 0;
241*aef5731fSOliver Tappe     *pwc = (ucs4_t) last_wc;
242*aef5731fSOliver Tappe     return 0; /* Don't advance the input pointer. */
243*aef5731fSOliver Tappe   }
244*aef5731fSOliver Tappe   if ((wc >= 0x05d0 && wc <= 0x05ea && ((0x07db5f7f >> (wc - 0x05d0)) & 1))
245*aef5731fSOliver Tappe       || wc == 0x05f2) {
246*aef5731fSOliver Tappe     /* wc is a possible match in cp1255_comp_table_data. Buffer it. */
247*aef5731fSOliver Tappe     conv->istate = wc;
248*aef5731fSOliver Tappe     return RET_TOOFEW(1);
249*aef5731fSOliver Tappe   } else {
250*aef5731fSOliver Tappe     /* Output wc immediately. */
251*aef5731fSOliver Tappe     *pwc = (ucs4_t) wc;
252*aef5731fSOliver Tappe     return 1;
253*aef5731fSOliver Tappe   }
254*aef5731fSOliver Tappe }
255*aef5731fSOliver Tappe 
256*aef5731fSOliver Tappe #define cp1255_flushwc normal_flushwc
257*aef5731fSOliver Tappe 
258*aef5731fSOliver Tappe static const unsigned char cp1255_page00[88] = {
259*aef5731fSOliver Tappe   0xa0, 0xa1, 0xa2, 0xa3, 0x00, 0xa5, 0xa6, 0xa7, /* 0xa0-0xa7 */
260*aef5731fSOliver Tappe   0xa8, 0xa9, 0x00, 0xab, 0xac, 0xad, 0xae, 0xaf, /* 0xa8-0xaf */
261*aef5731fSOliver Tappe   0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* 0xb0-0xb7 */
262*aef5731fSOliver Tappe   0xb8, 0xb9, 0x00, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* 0xb8-0xbf */
263*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0-0xc7 */
264*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc8-0xcf */
265*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, /* 0xd0-0xd7 */
266*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd8-0xdf */
267*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0-0xe7 */
268*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe8-0xef */
269*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, /* 0xf0-0xf7 */
270*aef5731fSOliver Tappe };
271*aef5731fSOliver Tappe static const unsigned char cp1255_page02[32] = {
272*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, /* 0xc0-0xc7 */
273*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc8-0xcf */
274*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0-0xd7 */
275*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, /* 0xd8-0xdf */
276*aef5731fSOliver Tappe };
277*aef5731fSOliver Tappe static const unsigned char cp1255_page05[72] = {
278*aef5731fSOliver Tappe   0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 0xb0-0xb7 */
279*aef5731fSOliver Tappe   0xc8, 0xc9, 0x00, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* 0xb8-0xbf */
280*aef5731fSOliver Tappe   0xd0, 0xd1, 0xd2, 0xd3, 0x00, 0x00, 0x00, 0x00, /* 0xc0-0xc7 */
281*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc8-0xcf */
282*aef5731fSOliver Tappe   0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* 0xd0-0xd7 */
283*aef5731fSOliver Tappe   0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* 0xd8-0xdf */
284*aef5731fSOliver Tappe   0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 0xe0-0xe7 */
285*aef5731fSOliver Tappe   0xf8, 0xf9, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe8-0xef */
286*aef5731fSOliver Tappe   0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0x00, 0x00, 0x00, /* 0xf0-0xf7 */
287*aef5731fSOliver Tappe };
288*aef5731fSOliver Tappe static const unsigned char cp1255_page20[56] = {
289*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0xfe, /* 0x08-0x0f */
290*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x96, 0x97, 0x00, 0x00, 0x00, /* 0x10-0x17 */
291*aef5731fSOliver Tappe   0x91, 0x92, 0x82, 0x00, 0x93, 0x94, 0x84, 0x00, /* 0x18-0x1f */
292*aef5731fSOliver Tappe   0x86, 0x87, 0x95, 0x00, 0x00, 0x00, 0x85, 0x00, /* 0x20-0x27 */
293*aef5731fSOliver Tappe   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x28-0x2f */
294*aef5731fSOliver Tappe   0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x30-0x37 */
295*aef5731fSOliver Tappe   0x00, 0x8b, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x38-0x3f */
296*aef5731fSOliver Tappe };
297*aef5731fSOliver Tappe 
298*aef5731fSOliver Tappe static int
cp1255_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)299*aef5731fSOliver Tappe cp1255_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
300*aef5731fSOliver Tappe {
301*aef5731fSOliver Tappe   unsigned char c = 0;
302*aef5731fSOliver Tappe   if (wc < 0x0080) {
303*aef5731fSOliver Tappe     *r = wc;
304*aef5731fSOliver Tappe     return 1;
305*aef5731fSOliver Tappe   }
306*aef5731fSOliver Tappe   else if (wc >= 0x00a0 && wc < 0x00f8)
307*aef5731fSOliver Tappe     c = cp1255_page00[wc-0x00a0];
308*aef5731fSOliver Tappe   else if (wc == 0x0192)
309*aef5731fSOliver Tappe     c = 0x83;
310*aef5731fSOliver Tappe   else if (wc >= 0x02c0 && wc < 0x02e0)
311*aef5731fSOliver Tappe     c = cp1255_page02[wc-0x02c0];
312*aef5731fSOliver Tappe   else if (wc >= 0x05b0 && wc < 0x05f8)
313*aef5731fSOliver Tappe     c = cp1255_page05[wc-0x05b0];
314*aef5731fSOliver Tappe   else if (wc >= 0x2008 && wc < 0x2040)
315*aef5731fSOliver Tappe     c = cp1255_page20[wc-0x2008];
316*aef5731fSOliver Tappe   else if (wc == 0x20aa)
317*aef5731fSOliver Tappe     c = 0xa4;
318*aef5731fSOliver Tappe   else if (wc == 0x20ac)
319*aef5731fSOliver Tappe     c = 0x80;
320*aef5731fSOliver Tappe   else if (wc == 0x2122)
321*aef5731fSOliver Tappe     c = 0x99;
322*aef5731fSOliver Tappe   if (c != 0) {
323*aef5731fSOliver Tappe     *r = c;
324*aef5731fSOliver Tappe     return 1;
325*aef5731fSOliver Tappe   }
326*aef5731fSOliver Tappe   /* Try canonical decomposition. */
327*aef5731fSOliver Tappe   {
328*aef5731fSOliver Tappe     /* Binary search through cp1255_decomp_table. */
329*aef5731fSOliver Tappe     unsigned int i1 = 0;
330*aef5731fSOliver Tappe     unsigned int i2 = sizeof(cp1255_decomp_table)/sizeof(cp1255_decomp_table[0])-1;
331*aef5731fSOliver Tappe     if (wc >= cp1255_decomp_table[i1].composed
332*aef5731fSOliver Tappe         && wc <= cp1255_decomp_table[i2].composed) {
333*aef5731fSOliver Tappe       unsigned int i;
334*aef5731fSOliver Tappe       for (;;) {
335*aef5731fSOliver Tappe         /* Here i2 - i1 > 0. */
336*aef5731fSOliver Tappe         i = (i1+i2)>>1;
337*aef5731fSOliver Tappe         if (wc == cp1255_decomp_table[i].composed)
338*aef5731fSOliver Tappe           break;
339*aef5731fSOliver Tappe         if (wc < cp1255_decomp_table[i].composed) {
340*aef5731fSOliver Tappe           if (i1 == i)
341*aef5731fSOliver Tappe             return RET_ILUNI;
342*aef5731fSOliver Tappe           /* Here i1 < i < i2. */
343*aef5731fSOliver Tappe           i2 = i;
344*aef5731fSOliver Tappe         } else {
345*aef5731fSOliver Tappe           /* Here i1 <= i < i2. */
346*aef5731fSOliver Tappe           if (i1 != i)
347*aef5731fSOliver Tappe             i1 = i;
348*aef5731fSOliver Tappe           else {
349*aef5731fSOliver Tappe             /* Here i2 - i1 = 1. */
350*aef5731fSOliver Tappe             i = i2;
351*aef5731fSOliver Tappe             if (wc == cp1255_decomp_table[i].composed)
352*aef5731fSOliver Tappe               break;
353*aef5731fSOliver Tappe             else
354*aef5731fSOliver Tappe               return RET_ILUNI;
355*aef5731fSOliver Tappe           }
356*aef5731fSOliver Tappe         }
357*aef5731fSOliver Tappe       }
358*aef5731fSOliver Tappe       /* Found a canonical decomposition. */
359*aef5731fSOliver Tappe       wc = cp1255_decomp_table[i].base;
360*aef5731fSOliver Tappe       /* wc is one of 0x05d0..0x05d6, 0x05d8..0x05dc, 0x05de, 0x05e0..0x05e1,
361*aef5731fSOliver Tappe          0x05e3..0x05e4, 0x05e6..0x05ea, 0x05f2. */
362*aef5731fSOliver Tappe       c = cp1255_page05[wc-0x05b0];
363*aef5731fSOliver Tappe       if (cp1255_decomp_table[i].comb2 < 0) {
364*aef5731fSOliver Tappe         if (n < 2)
365*aef5731fSOliver Tappe           return RET_TOOSMALL;
366*aef5731fSOliver Tappe         r[0] = c;
367*aef5731fSOliver Tappe         r[1] = cp1255_comb_table[cp1255_decomp_table[i].comb1];
368*aef5731fSOliver Tappe         return 2;
369*aef5731fSOliver Tappe       } else {
370*aef5731fSOliver Tappe         if (n < 3)
371*aef5731fSOliver Tappe           return RET_TOOSMALL;
372*aef5731fSOliver Tappe         r[0] = c;
373*aef5731fSOliver Tappe         r[1] = cp1255_comb_table[cp1255_decomp_table[i].comb1];
374*aef5731fSOliver Tappe         r[2] = cp1255_comb_table[cp1255_decomp_table[i].comb2];
375*aef5731fSOliver Tappe         return 3;
376*aef5731fSOliver Tappe       }
377*aef5731fSOliver Tappe     }
378*aef5731fSOliver Tappe   }
379*aef5731fSOliver Tappe   return RET_ILUNI;
380*aef5731fSOliver Tappe }
381