Back to index

glibc  2.9
jis0208.h
Go to the documentation of this file.
00001 /* Access functions for JISX0208 conversion.
00002    Copyright (C) 1997,1998,1999,2000,2003,2005,2007
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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 _JIS0208_H
00023 #define _JIS0208_H   1
00024 
00025 #include <gconv.h>
00026 #include <stdint.h>
00027 
00028 /* Struct for table with indeces in UCS mapping table.  */
00029 struct jisx0208_ucs_idx
00030 {
00031   uint16_t start;
00032   uint16_t end;
00033   uint16_t idx;
00034 };
00035 
00036 
00037 /* Conversion table.  */
00038 extern const uint16_t __jis0208_to_ucs[];
00039 
00040 #define JIS0208_LAT1_MIN 0xa2
00041 #define JIS0208_LAT1_MAX 0xf7
00042 extern const char __jisx0208_from_ucs4_lat1[JIS0208_LAT1_MAX + 1
00043                                        - JIS0208_LAT1_MIN][2];
00044 extern const char __jisx0208_from_ucs4_greek[0xc1][2];
00045 extern const struct jisx0208_ucs_idx __jisx0208_from_ucs_idx[];
00046 extern const char __jisx0208_from_ucs_tab[][2];
00047 
00048 
00049 static inline uint32_t
00050 __attribute ((always_inline))
00051 jisx0208_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
00052 {
00053   unsigned char ch = *(*s);
00054   unsigned char ch2;
00055   int idx;
00056 
00057   if (ch < offset || (ch - offset) <= 0x20)
00058     return __UNKNOWN_10646_CHAR;
00059 
00060   if (avail < 2)
00061     return 0;
00062 
00063   ch2 = (*s)[1];
00064   if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
00065     return __UNKNOWN_10646_CHAR;
00066 
00067   idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
00068   if (idx >= 0x1e80)
00069     return __UNKNOWN_10646_CHAR;
00070 
00071   (*s) += 2;
00072 
00073   return __jis0208_to_ucs[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
00074 }
00075 
00076 
00077 static inline size_t
00078 __attribute ((always_inline))
00079 ucs4_to_jisx0208 (uint32_t wch, unsigned char *s, size_t avail)
00080 {
00081   unsigned int ch = (unsigned int) wch;
00082   const char *cp;
00083 
00084   if (avail < 2)
00085     return 0;
00086 
00087   if (ch >= JIS0208_LAT1_MIN && ch <= JIS0208_LAT1_MAX)
00088     cp = __jisx0208_from_ucs4_lat1[ch - JIS0208_LAT1_MIN];
00089   else if (ch >= 0x391 && ch <= 0x451)
00090     cp = __jisx0208_from_ucs4_greek[ch - 0x391];
00091   else
00092     {
00093       const struct jisx0208_ucs_idx *rp = __jisx0208_from_ucs_idx;
00094 
00095       if (ch >= 0xffff)
00096        return __UNKNOWN_10646_CHAR;
00097       while (ch > rp->end)
00098        ++rp;
00099       if (ch >= rp->start)
00100        cp = __jisx0208_from_ucs_tab[rp->idx + ch - rp->start];
00101       else
00102        return __UNKNOWN_10646_CHAR;
00103     }
00104 
00105   if (cp[0] == '\0')
00106     return __UNKNOWN_10646_CHAR;
00107 
00108   s[0] = cp[0];
00109   s[1] = cp[1];
00110 
00111   return 2;
00112 }
00113 
00114 #endif /* jis0208.h */