Back to index

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