Back to index

glibc  2.9
iso-ir-165.h
Go to the documentation of this file.
00001 /* Tables for conversion to and from ISO-IR-165.
00002    converting from UCS using gaps.
00003    Copyright (C) 2000, 2003, 2007 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
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 #ifndef _ISO_IR_165_H
00023 #define _ISO_IR_165_H       1
00024 
00025 #include <gconv.h>
00026 #include <stdint.h>
00027 
00028 struct gap
00029 {
00030   uint16_t start;
00031   uint16_t end;
00032   int32_t idx;
00033 };
00034 
00035 /* Table for ISO-IR-165 (CCITT Chinese) to UCS4 conversion.  */
00036 #define ISOIR165_FROMSIZE   0x2284
00037 extern const uint16_t __isoir165_to_tab[ISOIR165_FROMSIZE];
00038 
00039 
00040 /* XXX If we at some point need an offset value to decode the byte
00041    sequences another parameter can be added.  */
00042 static inline uint32_t
00043 __attribute ((always_inline))
00044 isoir165_to_ucs4 (const unsigned char **s, size_t avail)
00045 {
00046   unsigned char ch = *(*s);
00047   unsigned char ch2;
00048   uint32_t res;
00049 
00050   if (ch <= 0x20 || ch >= 0x7f)
00051     return __UNKNOWN_10646_CHAR;
00052 
00053   if (avail < 2)
00054     return 0;
00055 
00056   ch2 = (*s)[1];
00057   if (ch2 <= 0x20 || ch2 >= 0x7f)
00058     return __UNKNOWN_10646_CHAR;
00059 
00060   res = __isoir165_to_tab[(ch - 0x21) * 94 + (ch2 - 0x21)];
00061   if (res == 0)
00062     return __UNKNOWN_10646_CHAR;
00063 
00064   *s += 2;
00065   return res;
00066 }
00067 
00068 
00069 /* Tables for ISO-IR-165 (CCITT Chinese) from UCS4 conversion.  */
00070 extern const struct gap __isoir165_from_idx[];
00071 extern const char __isoir165_from_tab[];
00072 
00073 static inline size_t
00074 __attribute ((always_inline))
00075 ucs4_to_isoir165 (uint32_t wch, unsigned char *s, size_t avail)
00076 {
00077   unsigned int ch = (unsigned int) wch;
00078   const char *cp;
00079   const struct gap *rp = __isoir165_from_idx;
00080 
00081   if (ch > 0xffe5)
00082     /* This is an illegal character.  */
00083     return __UNKNOWN_10646_CHAR;
00084 
00085   while (ch > rp->end)
00086     ++rp;
00087   if (ch < rp->start)
00088     /* This is an illegal character.  */
00089     return __UNKNOWN_10646_CHAR;
00090 
00091   /* The two bytes following the index given in this record give the
00092      encoding in ISO-IR-165.  Unless the bytes are zero.  */
00093   cp = &__isoir165_from_tab[(ch + rp->idx) * 2];
00094   if (*cp == '\0')
00095     /* This is an illegal character.  */
00096     return __UNKNOWN_10646_CHAR;
00097 
00098   if (avail < 2)
00099     return 0;
00100 
00101   s[0] = cp[0];
00102   s[1] = cp[1];
00103 
00104   return 2;
00105 }
00106 
00107 #endif /* iso-ir-165.h */