chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / iconvdata / cns11643.h
1 /* Access functions for CNS 11643 handling.
2    Copyright (C) 1998,1999,2000,2001,2002,2003,2007
3    Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6
7    The GNU C Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 2.1 of the License, or (at your option) any later version.
11
12    The GNU C Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with the GNU C Library; if not, write to the Free
19    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20    02111-1307 USA.  */
21
22 #include <stdint.h>
23
24
25 /* Table for CNS 11643, plane 1 to UCS4 conversion.  */
26 extern const uint16_t __cns11643l1_to_ucs4_tab[];
27 /* Table for CNS 11643, plane 2 to UCS4 conversion.  */
28 extern const uint16_t __cns11643l2_to_ucs4_tab[];
29 /* Table for CNS 11643, plane 3 to UCS4 conversion.  */
30 extern const uint32_t __cns11643l3_to_ucs4_tab[];
31 /* Table for CNS 11643, plane 4 to UCS4 conversion.  */
32 extern const uint32_t __cns11643l4_to_ucs4_tab[];
33 /* Table for CNS 11643, plane 5 to UCS4 conversion.  */
34 extern const uint32_t __cns11643l5_to_ucs4_tab[];
35 /* Table for CNS 11643, plane 6 to UCS4 conversion.  */
36 extern const uint32_t __cns11643l6_to_ucs4_tab[];
37 /* Table for CNS 11643, plane 7 to UCS4 conversion.  */
38 extern const uint32_t __cns11643l7_to_ucs4_tab[];
39 /* Table for CNS 11643, plane 15 (old) to UCS4 conversion.  */
40 extern const uint32_t __cns11643l15_to_ucs4_tab[];
41
42
43 static inline uint32_t
44 __attribute ((always_inline))
45 cns11643_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
46 {
47   unsigned char ch = *(*s);
48   unsigned char ch2;
49   unsigned char ch3;
50   uint32_t result;
51   int idx;
52
53   if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x30)
54     return __UNKNOWN_10646_CHAR;
55
56   if (avail < 3)
57     return 0;
58
59   ch2 = (*s)[1];
60   if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
61     return __UNKNOWN_10646_CHAR;
62
63   ch3 = (*s)[2];
64   if ((ch3 - offset) <= 0x20 || (ch3 - offset) >= 0x7f)
65     return __UNKNOWN_10646_CHAR;
66
67   idx = (ch2 - 0x21 - offset) * 94 + (ch3 - 0x21 - offset);
68
69   switch (ch - 0x20 - offset)
70     {
71     case 1:
72       if (idx > 0x21f2)
73         return __UNKNOWN_10646_CHAR;
74       result = __cns11643l1_to_ucs4_tab[idx];
75       break;
76     case 2:
77       if (idx > 0x1de1)
78         return __UNKNOWN_10646_CHAR;
79       result = __cns11643l2_to_ucs4_tab[idx];
80       break;
81     case 3:
82       if (idx > 0x19bd)
83         return __UNKNOWN_10646_CHAR;
84       result = __cns11643l3_to_ucs4_tab[idx];
85       break;
86     case 4:
87       if (idx > 0x1c81)
88         return __UNKNOWN_10646_CHAR;
89       result = __cns11643l4_to_ucs4_tab[idx];
90       break;
91     case 5:
92       if (idx > 0x219a)
93         return __UNKNOWN_10646_CHAR;
94       result = __cns11643l5_to_ucs4_tab[idx];
95       break;
96     case 6:
97       if (idx > 0x18f3)
98         return __UNKNOWN_10646_CHAR;
99       result = __cns11643l6_to_ucs4_tab[idx];
100       break;
101     case 7:
102       if (idx > 0x198a)
103         return __UNKNOWN_10646_CHAR;
104       result = __cns11643l7_to_ucs4_tab[idx];
105       break;
106     case 15:
107       if (idx > 0x1c00)
108         return __UNKNOWN_10646_CHAR;
109       result = __cns11643l15_to_ucs4_tab[idx];
110       break;
111     default:
112       return __UNKNOWN_10646_CHAR;
113     }
114
115   if (result != L'\0')
116     (*s) += 3;
117   else
118     result = __UNKNOWN_10646_CHAR;
119
120   return result;
121 }
122
123
124 /* Tables for the UCS4 -> CNS conversion.  */
125 extern const char __cns11643l1_from_ucs4_tab1[][2];
126 extern const char __cns11643l1_from_ucs4_tab2[][2];
127 extern const char __cns11643l1_from_ucs4_tab3[][2];
128 extern const char __cns11643l1_from_ucs4_tab4[][2];
129 extern const char __cns11643l1_from_ucs4_tab5[][2];
130 extern const char __cns11643l1_from_ucs4_tab6[][2];
131 extern const char __cns11643l1_from_ucs4_tab7[][2];
132 extern const char __cns11643l1_from_ucs4_tab8[][2];
133 extern const char __cns11643l1_from_ucs4_tab9[][2];
134 extern const char __cns11643l1_from_ucs4_tab10[][2];
135 extern const char __cns11643l1_from_ucs4_tab11[][2];
136 extern const char __cns11643l1_from_ucs4_tab12[][2];
137 extern const char __cns11643l1_from_ucs4_tab13[][2];
138 extern const char __cns11643l1_from_ucs4_tab14[][2];
139 extern const char __cns11643_from_ucs4p0_tab[][3];
140 extern const char __cns11643_from_ucs4p2_tab[][3];
141 extern const char __cns11643_from_ucs4p2c_tab[][3];
142
143
144 static inline size_t
145 __attribute ((always_inline))
146 ucs4_to_cns11643 (uint32_t wch, unsigned char *s, size_t avail)
147 {
148   unsigned int ch = (unsigned int) wch;
149   char buf[2];
150   const char *cp = buf;
151   size_t needed = 2;
152
153   switch (ch)
154     {
155     case 0xa7 ... 0xf7:
156       cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7];
157       break;
158     case 0x2c7 ... 0x2d9:
159       cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7];
160       break;
161     case 0x391 ... 0x3c9:
162       cp = __cns11643l1_from_ucs4_tab3[ch - 0x391];
163       break;
164     case 0x2013 ... 0x203e:
165       cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013];
166       break;
167     case 0x2103:
168       cp = "\x22\x6a";
169       break;
170     case 0x2105:
171       cp = "\x22\x22";
172       break;
173     case 0x2109:
174       cp = "\x22\x6b";
175       break;
176     case 0x2160 ... 0x2169:
177       buf[0] = '\x24';
178       buf[1] = '\x2b' + (ch - 0x2160);
179       break;
180     case 0x2170 ... 0x2179:
181       buf[0] = '\x26';
182       buf[1] = '\x35' + (ch - 0x2170);
183       break;
184     case 0x2190 ... 0x2199:
185       cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190];
186       break;
187     case 0x2215 ... 0x2267:
188       cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215];
189       break;
190     case 0x22a5:
191       cp = "\x22\x47";
192       break;
193     case 0x22bf:
194       cp = "\x22\x4a";
195       break;
196     case 0x2400 ... 0x2421:
197       cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400];
198       break;
199     case 0x2460 ... 0x247d:
200       cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460];
201       break;
202     case 0x2500 ... 0x2642:
203       cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500];
204       break;
205     case 0x3000 ... 0x3029:
206       cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000];
207       break;
208     case 0x30fb:
209       cp = "\x21\x26";
210       break;
211     case 0x3105 ... 0x3129:
212       buf[0] = '\x25';
213       buf[1] = '\x47' + (ch - 0x3105);
214       break;
215     case 0x32a3:
216       cp = "\x22\x21";
217       break;
218     case 0x338e ... 0x33d5:
219       cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e];
220       break;
221     case 0x4e00 ... 0x9f9c:
222       cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00];
223       if (cp[0] != '\0')
224         break;
225       /* FALLTHROUGH.  Let's try the other planes.  */
226     case 0x3400 ... 0x4dff:
227     case 0x9f9d ... 0x9fa5:
228       /* Let's try the other planes.  */
229       needed = 3;
230       cp = __cns11643_from_ucs4p0_tab[ch - 0x3400];
231       break;
232     case 0xfa28:
233       needed = 3;
234       cp = "\x0f\x58\x4c";
235       break;
236     case 0xfe30 ... 0xfe6b:
237       cp = __cns11643l1_from_ucs4_tab13[ch - 0xfe30];
238       break;
239     case 0xff01 ... 0xff5d:
240       cp = __cns11643l1_from_ucs4_tab14[ch - 0xff01];
241       break;
242     case 0xffe0:
243       cp = "\x22\x66";
244       break;
245     case 0xffe1:
246       cp = "\x22\x67";
247       break;
248     case 0xffe5:
249       cp = "\x22\x64";
250       break;
251     case 0x20000 ... 0x2a6d6:
252       needed = 3;
253       cp = __cns11643_from_ucs4p2_tab[ch - 0x20000];
254       break;
255     case 0x2f800 ... 0x2fa1d:
256       needed = 3;
257       cp = __cns11643_from_ucs4p2c_tab[ch - 0x2f800];
258       break;
259     default:
260       return __UNKNOWN_10646_CHAR;
261     }
262
263   if (cp[0] == '\0')
264     return __UNKNOWN_10646_CHAR;
265
266   if (avail < needed)
267     return 0;
268
269   s[0] = cp[0];
270   s[1] = cp[1];
271   if (needed == 3)
272     s[2] = cp[2];
273
274   return needed;
275 }