Back to index

glibc  2.9
cns11643l2.h
Go to the documentation of this file.
00001 /* Access functions for CNS 11643, plane 2 handling.
00002    Copyright (C) 1998, 1999, 2001, 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 2 to UCS4 conversion.  */
00025 extern const uint16_t __cns11643l2_to_ucs4_tab[];
00026 
00027 
00028 static inline uint32_t
00029 __attribute ((always_inline))
00030 cns11643l2_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 > 0x1de1)
00049     return __UNKNOWN_10646_CHAR;
00050 
00051   (*s) += 2;
00052 
00053   return __cns11643l2_to_ucs4_tab[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
00054 }
00055 
00056 
00057 /* The table which contains the CNS 11643 level 2 mappings.  */
00058 extern const char __cns11643_from_ucs4p0_tab[][3];
00059 
00060 
00061 static inline size_t
00062 __attribute ((always_inline))
00063 ucs4_to_cns11643l2 (uint32_t wch, unsigned char *s, size_t avail)
00064 {
00065   unsigned int ch = (unsigned int) wch;
00066   const char *cp = NULL;
00067 
00068   if (ch >= 0x4e07 && ch <= 0x9fa4)
00069     {
00070       cp = __cns11643_from_ucs4p0_tab[ch - 0x3400];
00071       if (cp[0] == '\2')
00072        ++cp;
00073       else
00074        cp = NULL;
00075     }
00076 
00077   if (cp == NULL)
00078     return __UNKNOWN_10646_CHAR;
00079 
00080   if (avail < 2)
00081     return 0;
00082 
00083   s[0] = cp[0];
00084   s[1] = cp[1];
00085 
00086   return 2;
00087 }