Back to index

glibc  2.9
cns11643.h
Go to the documentation of this file.
00001 /* Access functions for CNS 11643 handling.
00002    Copyright (C) 1998,1999,2000,2001,2002,2003,2007
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <stdint.h>
00023 
00024 
00025 /* Table for CNS 11643, plane 1 to UCS4 conversion.  */
00026 extern const uint16_t __cns11643l1_to_ucs4_tab[];
00027 /* Table for CNS 11643, plane 2 to UCS4 conversion.  */
00028 extern const uint16_t __cns11643l2_to_ucs4_tab[];
00029 /* Table for CNS 11643, plane 3 to UCS4 conversion.  */
00030 extern const uint32_t __cns11643l3_to_ucs4_tab[];
00031 /* Table for CNS 11643, plane 4 to UCS4 conversion.  */
00032 extern const uint32_t __cns11643l4_to_ucs4_tab[];
00033 /* Table for CNS 11643, plane 5 to UCS4 conversion.  */
00034 extern const uint32_t __cns11643l5_to_ucs4_tab[];
00035 /* Table for CNS 11643, plane 6 to UCS4 conversion.  */
00036 extern const uint32_t __cns11643l6_to_ucs4_tab[];
00037 /* Table for CNS 11643, plane 7 to UCS4 conversion.  */
00038 extern const uint32_t __cns11643l7_to_ucs4_tab[];
00039 /* Table for CNS 11643, plane 15 (old) to UCS4 conversion.  */
00040 extern const uint32_t __cns11643l15_to_ucs4_tab[];
00041 
00042 
00043 static inline uint32_t
00044 __attribute ((always_inline))
00045 cns11643_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
00046 {
00047   unsigned char ch = *(*s);
00048   unsigned char ch2;
00049   unsigned char ch3;
00050   uint32_t result;
00051   int idx;
00052 
00053   if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x30)
00054     return __UNKNOWN_10646_CHAR;
00055 
00056   if (avail < 3)
00057     return 0;
00058 
00059   ch2 = (*s)[1];
00060   if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
00061     return __UNKNOWN_10646_CHAR;
00062 
00063   ch3 = (*s)[2];
00064   if ((ch3 - offset) <= 0x20 || (ch3 - offset) >= 0x7f)
00065     return __UNKNOWN_10646_CHAR;
00066 
00067   idx = (ch2 - 0x21 - offset) * 94 + (ch3 - 0x21 - offset);
00068 
00069   switch (ch - 0x20 - offset)
00070     {
00071     case 1:
00072       if (idx > 0x21f2)
00073        return __UNKNOWN_10646_CHAR;
00074       result = __cns11643l1_to_ucs4_tab[idx];
00075       break;
00076     case 2:
00077       if (idx > 0x1de1)
00078        return __UNKNOWN_10646_CHAR;
00079       result = __cns11643l2_to_ucs4_tab[idx];
00080       break;
00081     case 3:
00082       if (idx > 0x19bd)
00083        return __UNKNOWN_10646_CHAR;
00084       result = __cns11643l3_to_ucs4_tab[idx];
00085       break;
00086     case 4:
00087       if (idx > 0x1c81)
00088        return __UNKNOWN_10646_CHAR;
00089       result = __cns11643l4_to_ucs4_tab[idx];
00090       break;
00091     case 5:
00092       if (idx > 0x219a)
00093        return __UNKNOWN_10646_CHAR;
00094       result = __cns11643l5_to_ucs4_tab[idx];
00095       break;
00096     case 6:
00097       if (idx > 0x18f3)
00098        return __UNKNOWN_10646_CHAR;
00099       result = __cns11643l6_to_ucs4_tab[idx];
00100       break;
00101     case 7:
00102       if (idx > 0x198a)
00103        return __UNKNOWN_10646_CHAR;
00104       result = __cns11643l7_to_ucs4_tab[idx];
00105       break;
00106     case 15:
00107       if (idx > 0x1c00)
00108        return __UNKNOWN_10646_CHAR;
00109       result = __cns11643l15_to_ucs4_tab[idx];
00110       break;
00111     default:
00112       return __UNKNOWN_10646_CHAR;
00113     }
00114 
00115   if (result != L'\0')
00116     (*s) += 3;
00117   else
00118     result = __UNKNOWN_10646_CHAR;
00119 
00120   return result;
00121 }
00122 
00123 
00124 /* Tables for the UCS4 -> CNS conversion.  */
00125 extern const char __cns11643l1_from_ucs4_tab1[][2];
00126 extern const char __cns11643l1_from_ucs4_tab2[][2];
00127 extern const char __cns11643l1_from_ucs4_tab3[][2];
00128 extern const char __cns11643l1_from_ucs4_tab4[][2];
00129 extern const char __cns11643l1_from_ucs4_tab5[][2];
00130 extern const char __cns11643l1_from_ucs4_tab6[][2];
00131 extern const char __cns11643l1_from_ucs4_tab7[][2];
00132 extern const char __cns11643l1_from_ucs4_tab8[][2];
00133 extern const char __cns11643l1_from_ucs4_tab9[][2];
00134 extern const char __cns11643l1_from_ucs4_tab10[][2];
00135 extern const char __cns11643l1_from_ucs4_tab11[][2];
00136 extern const char __cns11643l1_from_ucs4_tab12[][2];
00137 extern const char __cns11643l1_from_ucs4_tab13[][2];
00138 extern const char __cns11643l1_from_ucs4_tab14[][2];
00139 extern const char __cns11643_from_ucs4p0_tab[][3];
00140 extern const char __cns11643_from_ucs4p2_tab[][3];
00141 extern const char __cns11643_from_ucs4p2c_tab[][3];
00142 
00143 
00144 static inline size_t
00145 __attribute ((always_inline))
00146 ucs4_to_cns11643 (uint32_t wch, unsigned char *s, size_t avail)
00147 {
00148   unsigned int ch = (unsigned int) wch;
00149   char buf[2];
00150   const char *cp = buf;
00151   size_t needed = 2;
00152 
00153   switch (ch)
00154     {
00155     case 0xa7 ... 0xf7:
00156       cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7];
00157       break;
00158     case 0x2c7 ... 0x2d9:
00159       cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7];
00160       break;
00161     case 0x391 ... 0x3c9:
00162       cp = __cns11643l1_from_ucs4_tab3[ch - 0x391];
00163       break;
00164     case 0x2013 ... 0x203e:
00165       cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013];
00166       break;
00167     case 0x2103:
00168       cp = "\x22\x6a";
00169       break;
00170     case 0x2105:
00171       cp = "\x22\x22";
00172       break;
00173     case 0x2109:
00174       cp = "\x22\x6b";
00175       break;
00176     case 0x2160 ... 0x2169:
00177       buf[0] = '\x24';
00178       buf[1] = '\x2b' + (ch - 0x2160);
00179       break;
00180     case 0x2170 ... 0x2179:
00181       buf[0] = '\x26';
00182       buf[1] = '\x35' + (ch - 0x2170);
00183       break;
00184     case 0x2190 ... 0x2199:
00185       cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190];
00186       break;
00187     case 0x2215 ... 0x2267:
00188       cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215];
00189       break;
00190     case 0x22a5:
00191       cp = "\x22\x47";
00192       break;
00193     case 0x22bf:
00194       cp = "\x22\x4a";
00195       break;
00196     case 0x2400 ... 0x2421:
00197       cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400];
00198       break;
00199     case 0x2460 ... 0x247d:
00200       cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460];
00201       break;
00202     case 0x2500 ... 0x2642:
00203       cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500];
00204       break;
00205     case 0x3000 ... 0x3029:
00206       cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000];
00207       break;
00208     case 0x30fb:
00209       cp = "\x21\x26";
00210       break;
00211     case 0x3105 ... 0x3129:
00212       buf[0] = '\x25';
00213       buf[1] = '\x47' + (ch - 0x3105);
00214       break;
00215     case 0x32a3:
00216       cp = "\x22\x21";
00217       break;
00218     case 0x338e ... 0x33d5:
00219       cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e];
00220       break;
00221     case 0x4e00 ... 0x9f9c:
00222       cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00];
00223       if (cp[0] != '\0')
00224        break;
00225       /* FALLTHROUGH.  Let's try the other planes.  */
00226     case 0x3400 ... 0x4dff:
00227     case 0x9f9d ... 0x9fa5:
00228       /* Let's try the other planes.  */
00229       needed = 3;
00230       cp = __cns11643_from_ucs4p0_tab[ch - 0x3400];
00231       break;
00232     case 0xfa28:
00233       needed = 3;
00234       cp = "\x0f\x58\x4c";
00235       break;
00236     case 0xfe30 ... 0xfe6b:
00237       cp = __cns11643l1_from_ucs4_tab13[ch - 0xfe30];
00238       break;
00239     case 0xff01 ... 0xff5d:
00240       cp = __cns11643l1_from_ucs4_tab14[ch - 0xff01];
00241       break;
00242     case 0xffe0:
00243       cp = "\x22\x66";
00244       break;
00245     case 0xffe1:
00246       cp = "\x22\x67";
00247       break;
00248     case 0xffe5:
00249       cp = "\x22\x64";
00250       break;
00251     case 0x20000 ... 0x2a6d6:
00252       needed = 3;
00253       cp = __cns11643_from_ucs4p2_tab[ch - 0x20000];
00254       break;
00255     case 0x2f800 ... 0x2fa1d:
00256       needed = 3;
00257       cp = __cns11643_from_ucs4p2c_tab[ch - 0x2f800];
00258       break;
00259     default:
00260       return __UNKNOWN_10646_CHAR;
00261     }
00262 
00263   if (cp[0] == '\0')
00264     return __UNKNOWN_10646_CHAR;
00265 
00266   if (avail < needed)
00267     return 0;
00268 
00269   s[0] = cp[0];
00270   s[1] = cp[1];
00271   if (needed == 3)
00272     s[2] = cp[2];
00273 
00274   return needed;
00275 }