Back to index

glibc  2.9
euc-tw.c
Go to the documentation of this file.
00001 /* Mapping tables for EUC-TW handling.
00002    Copyright (C) 1998, 1999, 2000-2002, 2003, 2007
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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 #include <dlfcn.h>
00023 #include <stdint.h>
00024 #include <cns11643l1.h>
00025 #include <cns11643.h>
00026 
00027 /* Definitions used in the body of the `gconv' function.  */
00028 #define CHARSET_NAME        "EUC-TW//"
00029 #define FROM_LOOP           from_euc_tw
00030 #define TO_LOOP                    to_euc_tw
00031 #define DEFINE_INIT         1
00032 #define DEFINE_FINI         1
00033 #define MIN_NEEDED_FROM            1
00034 #define MAX_NEEDED_FROM            4
00035 #define MIN_NEEDED_TO              4
00036 
00037 
00038 /* First define the conversion function from EUC-TW to UCS4.  */
00039 #define MIN_NEEDED_INPUT    MIN_NEEDED_FROM
00040 #define MAX_NEEDED_INPUT    MAX_NEEDED_FROM
00041 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_TO
00042 #define LOOPFCT                    FROM_LOOP
00043 #define BODY \
00044   {                                                                  \
00045     uint32_t ch = *inptr;                                            \
00046                                                                      \
00047     if (ch <= 0x7f)                                                  \
00048       /* Plain ASCII.  */                                            \
00049       ++inptr;                                                              \
00050     else if ((ch <= 0xa0 || ch > 0xfe) && ch != 0x8e)                       \
00051       {                                                                     \
00052        /* This is illegal.  */                                              \
00053        STANDARD_FROM_LOOP_ERR_HANDLER (1);                                  \
00054       }                                                                     \
00055     else                                                             \
00056       {                                                                     \
00057        /* Two or more byte character.  First test whether the next byte      \
00058           is also available.  */                                     \
00059        uint32_t ch2;                                                 \
00060                                                                      \
00061        if (inptr + 1 >= inend)                                              \
00062          {                                                           \
00063            /* The second byte is not available.  Store the intermediate      \
00064               result.  */                                            \
00065            result = __GCONV_INCOMPLETE_INPUT;                               \
00066            break;                                                    \
00067          }                                                           \
00068                                                                      \
00069        ch2 = *(inptr + 1);                                           \
00070                                                                      \
00071        /* All second bytes of a multibyte character must be >= 0xa1. */      \
00072        if (ch2 < 0xa1 || ch2 == 0xff)                                       \
00073          STANDARD_FROM_LOOP_ERR_HANDLER (1);                                \
00074                                                                      \
00075        if (ch == 0x8e)                                                      \
00076          {                                                           \
00077            /* This is code set 2: CNS 11643, planes 1 to 16.  */            \
00078            const unsigned char *endp = inptr + 1;                           \
00079                                                                      \
00080            ch = cns11643_to_ucs4 (&endp, inend - inptr - 1, 0x80);          \
00081                                                                      \
00082            if (ch == 0)                                              \
00083              {                                                              \
00084               /* The third or fourth byte is not available.  Store          \
00085                  the intermediate result.  */                               \
00086               result = __GCONV_INCOMPLETE_INPUT;                     \
00087               break;                                                 \
00088              }                                                              \
00089                                                                      \
00090            if (ch == __UNKNOWN_10646_CHAR)                                  \
00091              /* Illegal input.  */                                   \
00092              STANDARD_FROM_LOOP_ERR_HANDLER (1);                     \
00093                                                                      \
00094            inptr += 4;                                                      \
00095          }                                                           \
00096        else                                                          \
00097          {                                                           \
00098            /* This is code set 1: CNS 11643, plane 1.  */                   \
00099            const unsigned char *endp = inptr;                               \
00100                                                                      \
00101            ch = cns11643l1_to_ucs4 (&endp, inend - inptr, 0x80);            \
00102            /* Please note that we need not test for the missing input       \
00103               characters here anymore.  */                                  \
00104            if (ch == __UNKNOWN_10646_CHAR)                                  \
00105              /* Illegal input.  */                                   \
00106              STANDARD_FROM_LOOP_ERR_HANDLER (2);                     \
00107                                                                      \
00108            inptr += 2;                                                      \
00109          }                                                           \
00110       }                                                                     \
00111                                                                      \
00112     put32 (outptr, ch);                                                     \
00113     outptr += 4;                                                     \
00114   }
00115 #define LOOP_NEED_FLAGS
00116 #define ONEBYTE_BODY \
00117   {                                                                  \
00118     if (c < 0x80)                                                    \
00119       return c;                                                             \
00120     else                                                             \
00121       return WEOF;                                                   \
00122   }
00123 #include <iconv/loop.c>
00124 
00125 
00126 /* Next, define the other direction.  */
00127 #define MIN_NEEDED_INPUT    MIN_NEEDED_TO
00128 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_FROM
00129 #define MAX_NEEDED_OUTPUT   MAX_NEEDED_FROM
00130 #define LOOPFCT                    TO_LOOP
00131 #define BODY \
00132   {                                                                  \
00133     uint32_t ch = get32 (inptr);                                     \
00134                                                                      \
00135     if (ch <= 0x7f)                                                  \
00136       /* It's plain ASCII.  */                                              \
00137       *outptr++ = ch;                                                       \
00138     else                                                             \
00139       {                                                                     \
00140        /* Try the CNS 11643 planes.  */                              \
00141        size_t found;                                                 \
00142                                                                      \
00143        found = ucs4_to_cns11643l1 (ch, outptr, outend - outptr);            \
00144        if (__builtin_expect (found, 1) == 0)                                \
00145          {                                                           \
00146            /* We ran out of space.  */                                      \
00147            result = __GCONV_FULL_OUTPUT;                             \
00148            break;                                                    \
00149          }                                                           \
00150        if (__builtin_expect (found, 1) != __UNKNOWN_10646_CHAR)             \
00151          {                                                           \
00152            /* It's a CNS 11643, plane 1 character, adjust it for EUC-TW.  */ \
00153            *outptr++ += 0x80;                                               \
00154            *outptr++ += 0x80;                                               \
00155          }                                                           \
00156        else                                                          \
00157          {                                                           \
00158            /* No CNS 11643, plane 1 character.  */                          \
00159                                                                      \
00160            found = ucs4_to_cns11643 (ch, outptr + 1, outend - outptr - 1);   \
00161            if (__builtin_expect (found, 1) == 0)                     \
00162              {                                                              \
00163               /* We ran out of space.  */                            \
00164               result = __GCONV_FULL_OUTPUT;                                 \
00165               break;                                                 \
00166              }                                                              \
00167            if (__builtin_expect (found, 0) == __UNKNOWN_10646_CHAR)         \
00168              {                                                              \
00169               UNICODE_TAG_HANDLER (ch, 4);                                  \
00170                                                                      \
00171               /* Illegal character.  */                              \
00172               STANDARD_TO_LOOP_ERR_HANDLER (4);                      \
00173              }                                                              \
00174                                                                      \
00175            /* It's a CNS 11643 character, adjust it for EUC-TW.  */         \
00176            *outptr++ = '\x8e';                                              \
00177            *outptr++ += 0xa0;                                               \
00178            *outptr++ += 0x80;                                               \
00179            *outptr++ += 0x80;                                               \
00180          }                                                           \
00181       }                                                                     \
00182                                                                      \
00183     inptr += 4;                                                             \
00184   }
00185 #define LOOP_NEED_FLAGS
00186 #include <iconv/loop.c>
00187 
00188 
00189 /* Now define the toplevel functions.  */
00190 #include <iconv/skeleton.c>