Back to index

glibc  2.9
cns11643l1.h
Go to the documentation of this file.
00001 /* Access functions for CNS 11643, plane 1 handling.
00002    Copyright (C) 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdint.h>
00022 #include <gconv.h>
00023 
00024 /* Table for CNS 11643, plane 1 to UCS4 conversion.  */
00025 extern const uint16_t __cns11643l1_to_ucs4_tab[];
00026 
00027 
00028 static inline uint32_t
00029 __attribute ((always_inline))
00030 cns11643l1_to_ucs4 (const unsigned char **s, size_t avail,
00031                   unsigned char offset)
00032 {
00033   unsigned char ch = *(*s);
00034   unsigned char ch2;
00035   int idx;
00036 
00037   if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x7d)
00038     return __UNKNOWN_10646_CHAR;
00039 
00040   if (avail < 2)
00041     return 0;
00042 
00043   ch2 = (*s)[1];
00044   if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
00045     return __UNKNOWN_10646_CHAR;
00046 
00047   idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
00048   if (idx > 0x21f2)
00049     return __UNKNOWN_10646_CHAR;
00050 
00051   (*s) += 2;
00052 
00053   return __cns11643l1_to_ucs4_tab[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
00054 }
00055 
00056 
00057 /* Tables for the UCS4 -> CNS conversion.  */
00058 extern const char __cns11643l1_from_ucs4_tab1[][2];
00059 extern const char __cns11643l1_from_ucs4_tab2[][2];
00060 extern const char __cns11643l1_from_ucs4_tab3[][2];
00061 extern const char __cns11643l1_from_ucs4_tab4[][2];
00062 extern const char __cns11643l1_from_ucs4_tab5[][2];
00063 extern const char __cns11643l1_from_ucs4_tab6[][2];
00064 extern const char __cns11643l1_from_ucs4_tab7[][2];
00065 extern const char __cns11643l1_from_ucs4_tab8[][2];
00066 extern const char __cns11643l1_from_ucs4_tab9[][2];
00067 extern const char __cns11643l1_from_ucs4_tab10[][2];
00068 extern const char __cns11643l1_from_ucs4_tab11[][2];
00069 extern const char __cns11643l1_from_ucs4_tab12[][2];
00070 extern const char __cns11643l1_from_ucs4_tab13[][2];
00071 extern const char __cns11643l1_from_ucs4_tab14[][2];
00072 
00073 
00074 static inline size_t
00075 __attribute ((always_inline))
00076 ucs4_to_cns11643l1 (uint32_t wch, unsigned char *s, size_t avail)
00077 {
00078   unsigned int ch = (unsigned int) wch;
00079   char buf[2];
00080   const char *cp = buf;
00081 
00082   switch (ch)
00083     {
00084     case 0xa7 ... 0xf7:
00085       cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7];
00086       break;
00087     case 0x2c7 ... 0x2d9:
00088       cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7];
00089       break;
00090     case 0x391 ... 0x3c9:
00091       cp = __cns11643l1_from_ucs4_tab3[ch - 0x391];
00092       break;
00093     case 0x2013 ... 0x203e:
00094       cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013];
00095       break;
00096     case 0x2103:
00097       cp = "\x22\x6a";
00098       break;
00099     case 0x2105:
00100       cp = "\x22\x22";
00101       break;
00102     case 0x2109:
00103       cp = "\x22\x6b";
00104       break;
00105     case 0x2160 ... 0x2169:
00106       buf[0] = '\x24';
00107       buf[1] = '\x2b' + (ch - 0x2160);
00108       break;
00109     case 0x2170 ... 0x2179:
00110       buf[0] = '\x26';
00111       buf[1] = '\x35' + (ch - 0x2170);
00112       break;
00113     case 0x2190 ... 0x2199:
00114       cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190];
00115       break;
00116     case 0x2215 ... 0x2267:
00117       cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215];
00118       break;
00119     case 0x22a5:
00120       cp = "\x22\x47";
00121       break;
00122     case 0x22bf:
00123       cp = "\x22\x4a";
00124       break;
00125     case 0x2400 ... 0x2421:
00126       cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400];
00127       break;
00128     case 0x2460 ... 0x247d:
00129       cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460];
00130       break;
00131     case 0x2500 ... 0x2642:
00132       cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500];
00133       break;
00134     case 0x3000 ... 0x3029:
00135       cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000];
00136       break;
00137     case 0x30fb:
00138       cp = "\x21\x26";
00139       break;
00140     case 0x3105 ... 0x3129:
00141       buf[0] = '\x25';
00142       buf[1] = '\x47' + (ch - 0x3105);
00143       break;
00144     case 0x32a3:
00145       cp = "\x22\x21";
00146       break;
00147     case 0x338e ... 0x33d5:
00148       cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e];
00149       break;
00150     case 0x4e00 ... 0x9f9c:
00151       cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00];
00152       break;
00153     case 0xfe30 ... 0xfe6b:
00154       cp = __cns11643l1_from_ucs4_tab13[ch - 0xfe30];
00155       break;
00156     case 0xff01 ... 0xff5d:
00157       cp = __cns11643l1_from_ucs4_tab14[ch - 0xff01];
00158       break;
00159     case 0xffe0:
00160       cp = "\x22\x66";
00161       break;
00162     case 0xffe1:
00163       cp = "\x22\x67";
00164       break;
00165     case 0xffe5:
00166       cp = "\x22\x64";
00167       break;
00168     default:
00169       return __UNKNOWN_10646_CHAR;
00170     }
00171 
00172   if (cp[0] == '\0')
00173     return __UNKNOWN_10646_CHAR;
00174 
00175   if (avail < 2)
00176     return 0;
00177 
00178   s[0] = cp[0];
00179   s[1] = cp[1];
00180 
00181   return 2;
00182 }