1 /* Conversion from and to CP1255.
2    Copyright (C) 1998-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <dlfcn.h>
20 #include <stdint.h>
21 #include <assert.h>
22 
23 #define NELEMS(arr) (sizeof (arr) / sizeof (arr[0]))
24 
25 /* Definitions used in the body of the `gconv' function.  */
26 #define CHARSET_NAME		"CP1255//"
27 #define FROM_LOOP		from_cp1255
28 #define TO_LOOP			to_cp1255
29 #define DEFINE_INIT		1
30 #define DEFINE_FINI		1
31 #define ONE_DIRECTION		0
32 #define FROM_LOOP_MIN_NEEDED_FROM	1
33 #define FROM_LOOP_MAX_NEEDED_FROM	1
34 #define FROM_LOOP_MIN_NEEDED_TO		4
35 #define FROM_LOOP_MAX_NEEDED_TO		4
36 #define TO_LOOP_MIN_NEEDED_FROM		4
37 #define TO_LOOP_MAX_NEEDED_FROM		4
38 #define TO_LOOP_MIN_NEEDED_TO		1
39 #define TO_LOOP_MAX_NEEDED_TO		3
40 #define PREPARE_LOOP \
41   int saved_state;							      \
42   int *statep = &data->__statep->__count;
43 #define EXTRA_LOOP_ARGS		, statep
44 
45 
46 /* Since we might have to reset input pointer we must be able to save
47    and restore the state.  */
48 #define SAVE_RESET_STATE(Save) \
49   if (Save)								      \
50     saved_state = *statep;						      \
51   else									      \
52     *statep = saved_state
53 
54 
55 /* During CP1255 to UCS4 conversion, the COUNT element of the state
56    contains the last UCS4 character, shifted by 3 bits.  */
57 
58 
59 /* Since this is a stateful encoding we have to provide code which resets
60    the output state to the initial state.  This has to be done during the
61    flushing.  */
62 #define EMIT_SHIFT_TO_INIT \
63   if (data->__statep->__count != 0)					      \
64     {									      \
65       if (FROM_DIRECTION)						      \
66 	{								      \
67 	  if (__glibc_likely (outbuf + 4 <= outend))			      \
68 	    {								      \
69 	      /* Write out the last character.  */			      \
70 	      *((uint32_t *) outbuf) = data->__statep->__count >> 3;	      \
71 	      outbuf += sizeof (uint32_t);				      \
72 	      data->__statep->__count = 0;				      \
73 	    }								      \
74 	  else								      \
75 	    /* We don't have enough room in the output buffer.  */	      \
76 	    status = __GCONV_FULL_OUTPUT;				      \
77 	}								      \
78       else								      \
79 	/* We don't use shift states in the TO_DIRECTION.  */		      \
80 	data->__statep->__count = 0;					      \
81     }
82 
83 
84 /* First define the conversion function from CP1255 to UCS4.  */
85 
86 static const uint16_t to_ucs4[128] = {
87   /* 0x80 */
88   0x20AC,      0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
89   0x02C6, 0x2030,      0, 0x2039,      0,      0,      0,      0,
90   /* 0x90 */
91        0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
92   0x02DC, 0x2122,      0, 0x203A,      0,      0,      0,      0,
93   /* 0xA0 */
94   0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AA, 0x00A5, 0x00A6, 0x00A7,
95   0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
96   /* 0xB0 */
97   0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
98   0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
99   /* 0xC0 */
100   0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
101   0x05B8, 0x05B9,      0, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
102   /* 0xD0 */
103   0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3,
104   0x05F4,      0,      0,      0,      0,      0,      0,      0,
105   /* 0xE0 */
106   0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
107   0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
108   /* 0xF0 */
109   0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
110   0x05E8, 0x05E9, 0x05EA,      0,      0, 0x200E, 0x200F,      0,
111 };
112 
113 /* CP1255 contains eight combining characters:
114    0x05b4, 0x05b7, 0x05b8, 0x05b9, 0x05bc, 0x05bf, 0x05c1, 0x05c2.  */
115 
116 /* Composition tables for each of the relevant combining characters.  */
117 static const struct {
118   uint16_t base;
119   uint16_t composed;
120 } comp_table_data[] = {
121 #define COMP_TABLE_IDX_05B4 0
122 #define COMP_TABLE_LEN_05B4 1
123   { 0x05D9, 0xFB1D },
124 #define COMP_TABLE_IDX_05B7 (COMP_TABLE_IDX_05B4 + COMP_TABLE_LEN_05B4)
125 #define COMP_TABLE_LEN_05B7 2
126   { 0x05D0, 0xFB2E },
127   { 0x05F2, 0xFB1F },
128 #define COMP_TABLE_IDX_05B8 (COMP_TABLE_IDX_05B7 + COMP_TABLE_LEN_05B7)
129 #define COMP_TABLE_LEN_05B8 1
130   { 0x05D0, 0xFB2F },
131 #define COMP_TABLE_IDX_05B9 (COMP_TABLE_IDX_05B8 + COMP_TABLE_LEN_05B8)
132 #define COMP_TABLE_LEN_05B9 1
133   { 0x05D5, 0xFB4B },
134 #define COMP_TABLE_IDX_05BC (COMP_TABLE_IDX_05B9 + COMP_TABLE_LEN_05B9)
135 #define COMP_TABLE_LEN_05BC 24
136   { 0x05D0, 0xFB30 },
137   { 0x05D1, 0xFB31 },
138   { 0x05D2, 0xFB32 },
139   { 0x05D3, 0xFB33 },
140   { 0x05D4, 0xFB34 },
141   { 0x05D5, 0xFB35 },
142   { 0x05D6, 0xFB36 },
143   { 0x05D8, 0xFB38 },
144   { 0x05D9, 0xFB39 },
145   { 0x05DA, 0xFB3A },
146   { 0x05DB, 0xFB3B },
147   { 0x05DC, 0xFB3C },
148   { 0x05DE, 0xFB3E },
149   { 0x05E0, 0xFB40 },
150   { 0x05E1, 0xFB41 },
151   { 0x05E3, 0xFB43 },
152   { 0x05E4, 0xFB44 },
153   { 0x05E6, 0xFB46 },
154   { 0x05E7, 0xFB47 },
155   { 0x05E8, 0xFB48 },
156   { 0x05E9, 0xFB49 },
157   { 0x05EA, 0xFB4A },
158   { 0xFB2A, 0xFB2C },
159   { 0xFB2B, 0xFB2D },
160 #define COMP_TABLE_IDX_05BF (COMP_TABLE_IDX_05BC + COMP_TABLE_LEN_05BC)
161 #define COMP_TABLE_LEN_05BF 3
162   { 0x05D1, 0xFB4C },
163   { 0x05DB, 0xFB4D },
164   { 0x05E4, 0xFB4E },
165 #define COMP_TABLE_IDX_05C1 (COMP_TABLE_IDX_05BF + COMP_TABLE_LEN_05BF)
166 #define COMP_TABLE_LEN_05C1 2
167   { 0x05E9, 0xFB2A },
168   { 0xFB49, 0xFB2C },
169 #define COMP_TABLE_IDX_05C2 (COMP_TABLE_IDX_05C1 + COMP_TABLE_LEN_05C1)
170 #define COMP_TABLE_LEN_05C2 2
171   { 0x05E9, 0xFB2B },
172   { 0xFB49, 0xFB2D },
173 #define COMP_TABLE_IDX_END (COMP_TABLE_IDX_05C2 + COMP_TABLE_LEN_05C2)
174 };
175 /* Compile-time verification of table size.  */
176 typedef int verify1[(NELEMS (comp_table_data) == COMP_TABLE_IDX_END) - 1];
177 
178 static const struct { unsigned int idx; unsigned int len; } comp_table[8] = {
179   { COMP_TABLE_IDX_05B4, COMP_TABLE_LEN_05B4 },
180   { COMP_TABLE_IDX_05B7, COMP_TABLE_LEN_05B7 },
181   { COMP_TABLE_IDX_05B8, COMP_TABLE_LEN_05B8 },
182   { COMP_TABLE_IDX_05B9, COMP_TABLE_LEN_05B9 },
183   { COMP_TABLE_IDX_05BC, COMP_TABLE_LEN_05BC },
184   { COMP_TABLE_IDX_05BF, COMP_TABLE_LEN_05BF },
185   { COMP_TABLE_IDX_05C1, COMP_TABLE_LEN_05C1 },
186   { COMP_TABLE_IDX_05C2, COMP_TABLE_LEN_05C2 },
187 };
188 
189 #define MIN_NEEDED_INPUT	FROM_LOOP_MIN_NEEDED_FROM
190 #define MAX_NEEDED_INPUT	FROM_LOOP_MAX_NEEDED_FROM
191 #define MIN_NEEDED_OUTPUT	FROM_LOOP_MIN_NEEDED_TO
192 #define MAX_NEEDED_OUTPUT	FROM_LOOP_MAX_NEEDED_TO
193 #define LOOPFCT			FROM_LOOP
194 #define BODY \
195   {									      \
196     uint32_t ch = *inptr;						      \
197     uint32_t last_ch;							      \
198     int must_buffer_ch;							      \
199 									      \
200     if (ch >= 0x80)							      \
201       {									      \
202 	ch = to_ucs4[ch - 0x80];					      \
203 	if (__glibc_unlikely (ch == L'\0'))				      \
204 	  {								      \
205 	    /* This is an illegal character.  */			      \
206 	    STANDARD_FROM_LOOP_ERR_HANDLER (1);				      \
207 	  }								      \
208       }									      \
209 									      \
210     /* Determine whether there is a buffered character pending.  */	      \
211     last_ch = *statep >> 3;						      \
212 									      \
213     /* We have to buffer ch if it is a possible match in comp_table_data.  */ \
214     must_buffer_ch = (ch >= 0x05d0 && ch <= 0x05f2);			      \
215 									      \
216     if (last_ch)							      \
217       {									      \
218 	if (ch >= 0x05b0 && ch < 0x05c5)				      \
219 	  {								      \
220 	    /* See whether last_ch and ch can be combined.  */		      \
221 	    unsigned int i, i1, i2;					      \
222 									      \
223 	    switch (ch)							      \
224 	      {								      \
225 	      case 0x05b4:						      \
226 		i = 0;							      \
227 		break;							      \
228 	      case 0x05b7:						      \
229 		i = 1;							      \
230 		break;							      \
231 	      case 0x05b8:						      \
232 		i = 2;							      \
233 		break;							      \
234 	      case 0x05b9:						      \
235 		i = 3;							      \
236 		break;							      \
237 	      case 0x05bc:						      \
238 		i = 4;							      \
239 		break;							      \
240 	      case 0x05bf:						      \
241 		i = 5;							      \
242 		break;							      \
243 	      case 0x05c1:						      \
244 		i = 6;							      \
245 		break;							      \
246 	      case 0x05c2:						      \
247 		i = 7;							      \
248 		break;							      \
249 	      default:							      \
250 		goto not_combining;					      \
251 	      }								      \
252 									      \
253 	    i1 = comp_table[i].idx;					      \
254 	    i2 = i1 + comp_table[i].len - 1;				      \
255 									      \
256 	    if (last_ch >= comp_table_data[i1].base			      \
257 		&& last_ch <= comp_table_data[i2].base)			      \
258 	      {								      \
259 		for (;;)						      \
260 		  {							      \
261 		    i = (i1 + i2) >> 1;					      \
262 		    if (last_ch == comp_table_data[i].base)		      \
263 		      break;						      \
264 		    if (last_ch < comp_table_data[i].base)		      \
265 		      {							      \
266 			if (i1 == i)					      \
267 			  goto not_combining;				      \
268 			i2 = i;						      \
269 		      }							      \
270 		    else						      \
271 		      {							      \
272 			if (i1 != i)					      \
273 			  i1 = i;					      \
274 			else						      \
275 			  {						      \
276 			    i = i2;					      \
277 			    if (last_ch == comp_table_data[i].base)	      \
278 			      break;					      \
279 			    goto not_combining;				      \
280 			  }						      \
281 		      }							      \
282 		  }							      \
283 		last_ch = comp_table_data[i].composed;			      \
284 		if (last_ch == 0xfb2a || last_ch == 0xfb2b		      \
285 		    || last_ch == 0xfb49)				      \
286 		  /* Buffer the combined character.  */			      \
287 		  *statep = last_ch << 3;				      \
288 		else							      \
289 		  {							      \
290 		    /* Output the combined character.  */		      \
291 		    put32 (outptr, last_ch);				      \
292 		    outptr += 4;					      \
293 		    *statep = 0;					      \
294 		  }							      \
295 		++inptr;						      \
296 		continue;						      \
297 	      }								      \
298 	  }								      \
299 									      \
300       not_combining:							      \
301 	/* Output the buffered character.  */				      \
302 	put32 (outptr, last_ch);					      \
303 	outptr += 4;							      \
304 	*statep = 0;							      \
305 									      \
306 	/* If we don't have enough room to output ch as well, then deal	      \
307 	   with it in another round.  */				      \
308 	if (!must_buffer_ch && __builtin_expect (outptr + 4 > outend, 0))     \
309 	  continue;							      \
310       }									      \
311 									      \
312     if (must_buffer_ch)							      \
313       *statep = ch << 3;						      \
314     else								      \
315       {									      \
316 	put32 (outptr, ch);						      \
317 	outptr += 4;							      \
318       }									      \
319     ++inptr;								      \
320   }
321 #define LOOP_NEED_FLAGS
322 #define EXTRA_LOOP_DECLS	, int *statep
323 #define ONEBYTE_BODY \
324   {									      \
325     if (c < 0x80)							      \
326       return c;								      \
327     uint32_t ch = to_ucs4[c - 0x80];					      \
328     if (ch == L'\0' || (ch >= 0x05d0 && ch <= 0x05f2))			      \
329       return WEOF;							      \
330     return ch;								      \
331   }
332 #include <iconv/loop.c>
333 
334 
335 /* Next, define the conversion function from UCS4 to CP1255.  */
336 
337 static const unsigned char from_ucs4[] = {
338 #define FROM_IDX_00 0
339   0xa0, 0xa1, 0xa2, 0xa3, 0x00, 0xa5, 0xa6, 0xa7, /* 0x00a0-0x00a7 */
340   0xa8, 0xa9, 0x00, 0xab, 0xac, 0xad, 0xae, 0xaf, /* 0x00a8-0x00af */
341   0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* 0x00b0-0x00b7 */
342   0xb8, 0xb9, 0x00, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* 0x00b8-0x00bf */
343   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x00c0-0x00c7 */
344   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x00c8-0x00cf */
345   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, /* 0x00d0-0x00d7 */
346   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x00d8-0x00df */
347   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x00e0-0x00e7 */
348   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x00e8-0x00ef */
349   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, /* 0x00f0-0x00f7 */
350 #define FROM_IDX_02 (FROM_IDX_00 + 88)
351                                       0x88, 0x00, /* 0x02c6-0x02c7 */
352   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x02c8-0x02cf */
353   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x02d0-0x02d7 */
354   0x00, 0x00, 0x00, 0x00, 0x98,                   /* 0x02d8-0x02dc */
355 #define FROM_IDX_05 (FROM_IDX_02 + 23)
356   0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 0x05b0-0x05b7 */
357   0xc8, 0xc9, 0x00, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* 0x05b8-0x05bf */
358   0xd0, 0xd1, 0xd2, 0xd3, 0x00, 0x00, 0x00, 0x00, /* 0x05c0-0x05c7 */
359   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x05c8-0x05cf */
360   0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* 0x05d0-0x05d7 */
361   0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* 0x05d8-0x05df */
362   0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 0x05e0-0x05e7 */
363   0xf8, 0xf9, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x05e8-0x05ef */
364   0xd4, 0xd5, 0xd6, 0xd7, 0xd8,                   /* 0x05f0-0x05f4 */
365 #define FROM_IDX_20 (FROM_IDX_05 + 69)
366                                       0xfd, 0xfe, /* 0x200e-0x200f */
367   0x00, 0x00, 0x00, 0x96, 0x97, 0x00, 0x00, 0x00, /* 0x2010-0x2017 */
368   0x91, 0x92, 0x82, 0x00, 0x93, 0x94, 0x84, 0x00, /* 0x2018-0x201f */
369   0x86, 0x87, 0x95, 0x00, 0x00, 0x00, 0x85, 0x00, /* 0x2020-0x2027 */
370   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x2028-0x202f */
371   0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x2030-0x2037 */
372   0x00, 0x8b, 0x9b,                               /* 0x2038-0x203a */
373 #define FROM_IDX_FF (FROM_IDX_20 + 45)
374 };
375 /* Compile-time verification of table size.  */
376 typedef int verify2[(NELEMS (from_ucs4) == FROM_IDX_FF) - 1];
377 
378 static const unsigned char comb_table[8] = {
379   0xc4, 0xc7, 0xc8, 0xc9, 0xcc, 0xcf, 0xd1, 0xd2,
380 };
381 
382 /* Decomposition table for the relevant Unicode characters. */
383 static const struct {
384   uint16_t composed;
385   uint16_t base;
386   uint32_t comb1 : 8;
387   int32_t comb2 : 8;
388 } decomp_table[] = {
389   { 0xFB1D, 0x05D9, 0, -1 },
390   { 0xFB1F, 0x05F2, 1, -1 },
391   { 0xFB2A, 0x05E9, 6, -1 },
392   { 0xFB2B, 0x05E9, 7, -1 },
393   { 0xFB2C, 0x05E9, 4, 6 },
394   { 0xFB2D, 0x05E9, 4, 7 },
395   { 0xFB2E, 0x05D0, 1, -1 },
396   { 0xFB2F, 0x05D0, 2, -1 },
397   { 0xFB30, 0x05D0, 4, -1 },
398   { 0xFB31, 0x05D1, 4, -1 },
399   { 0xFB32, 0x05D2, 4, -1 },
400   { 0xFB33, 0x05D3, 4, -1 },
401   { 0xFB34, 0x05D4, 4, -1 },
402   { 0xFB35, 0x05D5, 4, -1 },
403   { 0xFB36, 0x05D6, 4, -1 },
404   { 0xFB38, 0x05D8, 4, -1 },
405   { 0xFB39, 0x05D9, 4, -1 },
406   { 0xFB3A, 0x05DA, 4, -1 },
407   { 0xFB3B, 0x05DB, 4, -1 },
408   { 0xFB3C, 0x05DC, 4, -1 },
409   { 0xFB3E, 0x05DE, 4, -1 },
410   { 0xFB40, 0x05E0, 4, -1 },
411   { 0xFB41, 0x05E1, 4, -1 },
412   { 0xFB43, 0x05E3, 4, -1 },
413   { 0xFB44, 0x05E4, 4, -1 },
414   { 0xFB46, 0x05E6, 4, -1 },
415   { 0xFB47, 0x05E7, 4, -1 },
416   { 0xFB48, 0x05E8, 4, -1 },
417   { 0xFB49, 0x05E9, 4, -1 },
418   { 0xFB4A, 0x05EA, 4, -1 },
419   { 0xFB4B, 0x05D5, 3, -1 },
420   { 0xFB4C, 0x05D1, 5, -1 },
421   { 0xFB4D, 0x05DB, 5, -1 },
422   { 0xFB4E, 0x05E4, 5, -1 },
423 };
424 
425 #define MIN_NEEDED_INPUT	TO_LOOP_MIN_NEEDED_FROM
426 #define MAX_NEEDED_INPUT	TO_LOOP_MAX_NEEDED_FROM
427 #define MIN_NEEDED_OUTPUT	TO_LOOP_MIN_NEEDED_TO
428 #define MAX_NEEDED_OUTPUT	TO_LOOP_MAX_NEEDED_TO
429 #define LOOPFCT			TO_LOOP
430 #define BODY \
431   {									      \
432     uint32_t ch = get32 (inptr);					      \
433 									      \
434     if (ch < 0x0080)							      \
435       {									      \
436 	*outptr++ = ch;							      \
437 	inptr += 4;							      \
438       }									      \
439     else								      \
440       {									      \
441 	unsigned char res;						      \
442 									      \
443 	if (ch >= 0x00a0 && ch < 0x00f8)				      \
444 	  res = from_ucs4[ch - 0x00a0 + FROM_IDX_00];			      \
445 	else if (ch == 0x0192)						      \
446 	  res = 0x83;							      \
447 	else if (ch >= 0x02c6 && ch < 0x02dd)				      \
448 	  res = from_ucs4[ch - 0x02c6 + FROM_IDX_02];			      \
449 	else if (ch >= 0x05b0 && ch < 0x05f5)				      \
450 	  res = from_ucs4[ch - 0x05b0 + FROM_IDX_05];			      \
451 	else if (ch >= 0x200e && ch < 0x203b)				      \
452 	  res = from_ucs4[ch - 0x200e + FROM_IDX_20];			      \
453 	else if (ch == 0x20aa)						      \
454 	  res = 0xa4;							      \
455 	else if (ch == 0x20ac)						      \
456 	  res = 0x80;							      \
457 	else if (ch == 0x2122)						      \
458 	  res = 0x99;							      \
459 	else								      \
460 	  {								      \
461 	    UNICODE_TAG_HANDLER (ch, 4);				      \
462 	    res = 0;							      \
463 	  }								      \
464 									      \
465 	if (__glibc_likely (res != 0))					      \
466 	  {								      \
467 	    *outptr++ = res;						      \
468 	    inptr += 4;							      \
469 	  }								      \
470 	else								      \
471 	  {								      \
472 	    /* Try canonical decomposition.  */				      \
473 	    unsigned int i1, i2;					      \
474 									      \
475 	    i1 = 0;							      \
476 	    i2 = sizeof (decomp_table) / sizeof (decomp_table[0]) - 1;	      \
477 	    if (ch >= decomp_table[i1].composed				      \
478 		&& ch <= decomp_table[i2].composed)			      \
479 	      {								      \
480 		unsigned int i;						      \
481 									      \
482 		for (;;)						      \
483 		  {							      \
484 		    i = (i1 + i2) >> 1;					      \
485 		    if (ch == decomp_table[i].composed)			      \
486 		      break;						      \
487 		    if (ch < decomp_table[i].composed)			      \
488 		      {							      \
489 			if (i1 == i)					      \
490 			  goto failed;					      \
491 			i2 = i;						      \
492 		      }							      \
493 		    else						      \
494 		      {							      \
495 			if (i1 != i)					      \
496 			  i1 = i;					      \
497 			else						      \
498 			  {						      \
499 			    i = i2;					      \
500 			    if (ch == decomp_table[i].composed)		      \
501 			      break;					      \
502 			    goto failed;				      \
503 			  }						      \
504 		      }							      \
505 		  }							      \
506 									      \
507 		/* Found a canonical decomposition.  */			      \
508 		ch = decomp_table[i].base;				      \
509 		/* ch is one of 0x05d0..0x05d6, 0x05d8..0x05dc, 0x05de,	      \
510 		   0x05e0..0x05e1, 0x05e3..0x05e4, 0x05e6..0x05ea, 0x05f2. */ \
511 		ch = from_ucs4[ch - 0x05b0 + FROM_IDX_05];		      \
512 		assert (ch != 0);					      \
513 									      \
514 		if (decomp_table[i].comb2 < 0)				      \
515 		  {							      \
516 		    /* See whether we have room for two bytes.  */	      \
517 		    if (__glibc_unlikely (outptr + 1 >= outend))	      \
518 		      {							      \
519 			result = __GCONV_FULL_OUTPUT;			      \
520 			break;						      \
521 		      }							      \
522 									      \
523 		    *outptr++ = (unsigned char) ch;			      \
524 		    *outptr++ = comb_table[decomp_table[i].comb1];	      \
525 		  }							      \
526 		else							      \
527 		  {							      \
528 		    /* See whether we have room for three bytes.  */	      \
529 		    if (__glibc_unlikely (outptr + 2 >= outend))	      \
530 		      {							      \
531 			result = __GCONV_FULL_OUTPUT;			      \
532 			break;						      \
533 		      }							      \
534 									      \
535 		    *outptr++ = (unsigned char) ch;			      \
536 		    *outptr++ = comb_table[decomp_table[i].comb1];	      \
537 		    *outptr++ = comb_table[decomp_table[i].comb2];	      \
538 		  }							      \
539 									      \
540 		inptr += 4;						      \
541 		continue;						      \
542 	      }								      \
543 									      \
544 	  failed:							      \
545 	    /* This is an illegal character.  */			      \
546 	    STANDARD_TO_LOOP_ERR_HANDLER (4);				      \
547 	  }								      \
548       }									      \
549   }
550 #define LOOP_NEED_FLAGS
551 #define EXTRA_LOOP_DECLS	, int *statep
552 #include <iconv/loop.c>
553 
554 
555 /* Now define the toplevel functions.  */
556 #include <iconv/skeleton.c>
557