Back to index

glibc  2.9
ibm943.c
Go to the documentation of this file.
00001 /* Conversion from and to IBM943.
00002    Copyright (C) 2000-2002, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Masahide Washizawa <washi@jp.ibm.com>, 2000.
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 <dlfcn.h>
00022 #include <stdint.h>
00023 #include <stdbool.h>
00024 #include "ibm943.h"
00025 
00026 #define FROM  0
00027 #define TO    1
00028 
00029 /* Definitions used in the body of the `gconv' function.  */
00030 #define CHARSET_NAME "IBM943//"
00031 #define FROM_LOOP    from_ibm943
00032 #define TO_LOOP             to_ibm943
00033 
00034 /* Definitions of initialization and destructor function.  */
00035 #define DEFINE_INIT  1
00036 #define DEFINE_FINI  1
00037 
00038 #define MIN_NEEDED_FROM     1
00039 #define MAX_NEEDED_FROM     2
00040 #define MIN_NEEDED_TO       4
00041 
00042 /* First, define the conversion function from IBM-943 to UCS4.  */
00043 #define MIN_NEEDED_INPUT    MIN_NEEDED_FROM
00044 #define MAX_NEEDED_INPUT    MAX_NEEDED_FROM
00045 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_TO
00046 #define LOOPFCT                    FROM_LOOP
00047 #define BODY \
00048   {                                                                  \
00049     const struct gap *rp2 = __ibm943db_to_ucs4_idx;                         \
00050     uint32_t ch = *inptr;                                            \
00051     uint32_t res;                                                    \
00052                                                                      \
00053     if (__builtin_expect (ch == 0x80, 0)                             \
00054        || __builtin_expect (ch == 0xa0, 0)                                  \
00055        || __builtin_expect (ch == 0xfd, 0)                                  \
00056        || __builtin_expect (ch == 0xfe, 0)                                  \
00057        || __builtin_expect (ch == 0xff, 0))                                 \
00058       {                                                                     \
00059        /* This is an illegal character.  */                                 \
00060        STANDARD_FROM_LOOP_ERR_HANDLER (1);                                  \
00061       }                                                                     \
00062                                                                      \
00063     /* Use the IBM943 table for single byte.  */                     \
00064     if (__builtin_expect (ch > 0xdf, 0)                                     \
00065        || (res = __ibm943sb_to_ucs4[ch],                             \
00066            __builtin_expect (res == 0, 0) && ch != 0))                      \
00067       {                                                                     \
00068        /* Use the IBM943 table for double byte.  */                         \
00069        if (__builtin_expect (inptr + 1 >= inend, 0))                        \
00070          {                                                           \
00071            /* The second character is not available.                        \
00072               Store the intermediate result.  */                     \
00073            result = __GCONV_INCOMPLETE_INPUT;                               \
00074            break;                                                    \
00075          }                                                           \
00076                                                                      \
00077        ch = (ch * 0x100) + inptr[1];                                        \
00078        while (ch > rp2->end)                                                \
00079          ++rp2;                                                      \
00080                                                                      \
00081        if (__builtin_expect (rp2 == NULL, 0)                                \
00082            || __builtin_expect (ch < rp2->start, 0)                         \
00083            || (res = __ibm943db_to_ucs4[ch + rp2->idx],              \
00084            __builtin_expect (res, '\1') == 0 && ch !=0))                    \
00085          {                                                           \
00086            /* This is an illegal character.  */                      \
00087            STANDARD_FROM_LOOP_ERR_HANDLER (2);                              \
00088          }                                                           \
00089        else                                                          \
00090          {                                                           \
00091            put32 (outptr, res);                                      \
00092            outptr += 4;                                              \
00093            inptr += 2;                                                      \
00094          }                                                           \
00095       }                                                                     \
00096     else                                                             \
00097       {                                                                     \
00098        if (res == 0xa5)                                              \
00099          res = 0x5c;                                                 \
00100        else if (res == 0x203e)                                              \
00101          res = 0x7e;                                                 \
00102        put32 (outptr, res);                                          \
00103        outptr += 4;                                                  \
00104        inptr++;                                                      \
00105       }                                                                     \
00106   }
00107 #define LOOP_NEED_FLAGS
00108 #define ONEBYTE_BODY \
00109   {                                                                  \
00110     if (c == 0x80 || c == 0xa0 || c >= 0xe0)                                \
00111       return WEOF;                                                   \
00112     uint32_t res = __ibm943sb_to_ucs4[c];                            \
00113     if (res == 0 && c != 0)                                          \
00114       return WEOF;                                                   \
00115     if (res == 0xa5)                                                  \
00116       res = 0x5c;                                                    \
00117     else if (res == 0x203e)                                          \
00118       res = 0x7e;                                                    \
00119     return res;                                                             \
00120   }
00121 #include <iconv/loop.c>
00122 
00123 /* Next, define the other direction.  */
00124 #define MIN_NEEDED_INPUT    MIN_NEEDED_TO
00125 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_FROM
00126 #define MAX_NEEDED_OUTPUT   MAX_NEEDED_FROM
00127 #define LOOPFCT                    TO_LOOP
00128 #define BODY \
00129   {                                                                  \
00130     const struct gap *rp = __ucs4_to_ibm943sb_idx;                          \
00131     unsigned char sc;                                                       \
00132     uint32_t ch = get32(inptr);                                             \
00133     bool found = true;                                                      \
00134     uint32_t i;                                                             \
00135     uint32_t low;                                                    \
00136     uint32_t high;                                                   \
00137     uint16_t pccode;                                                 \
00138                                                                      \
00139     if (__builtin_expect (ch >= 0xffff, 0))                                 \
00140       {                                                                     \
00141        UNICODE_TAG_HANDLER (ch, 4);                                         \
00142        rp = NULL;                                                    \
00143       }                                                                     \
00144     else                                                             \
00145       while (ch > rp->end)                                           \
00146        ++rp;                                                         \
00147                                                                      \
00148     /* Use the UCS4 table for single byte.  */                              \
00149     if (__builtin_expect (rp == NULL, 0)                             \
00150        || __builtin_expect (ch < rp->start, 0)                              \
00151        || (sc = __ucs4_to_ibm943sb[ch + rp->idx],                           \
00152        __builtin_expect (sc, '\1') == '\0' && ch != L'\0'))                 \
00153       {                                                                     \
00154                                                                      \
00155        /* Use the UCS4 table for double byte.  */                           \
00156        found = false;                                                       \
00157        low = 0;                                                      \
00158        high = (sizeof (__ucs4_to_ibm943db) >> 1)                     \
00159               / sizeof (__ucs4_to_ibm943db[0][FROM]);                       \
00160        pccode = ch;                                                  \
00161        if (__builtin_expect (rp != NULL, 1))                                \
00162          while (low < high)                                          \
00163            {                                                         \
00164              i = (low + high) >> 1;                                         \
00165              if (pccode < __ucs4_to_ibm943db[i][FROM])                      \
00166               high = i;                                              \
00167              else if (pccode > __ucs4_to_ibm943db[i][FROM])                 \
00168               low = i + 1;                                           \
00169              else                                                    \
00170               {                                                      \
00171                 pccode = __ucs4_to_ibm943db[i][TO];                         \
00172                 found = true;                                               \
00173                 break;                                               \
00174               }                                                      \
00175            }                                                         \
00176        if (found)                                                    \
00177          {                                                           \
00178            if (__builtin_expect (outptr + 2 > outend, 0))                   \
00179              {                                                              \
00180               result = __GCONV_FULL_OUTPUT;                                 \
00181               break;                                                 \
00182              }                                                              \
00183            *outptr++ = pccode >> 8 & 0xff;                                  \
00184            *outptr++ = pccode & 0xff;                                       \
00185          }                                                           \
00186        else                                                          \
00187          {                                                           \
00188            /* This is an illegal character.  */                      \
00189            STANDARD_TO_LOOP_ERR_HANDLER (4);                                \
00190          }                                                           \
00191       }                                                                     \
00192     else                                                             \
00193       {                                                                     \
00194        if (__builtin_expect (outptr + 1 > outend, 0))                       \
00195          {                                                           \
00196            result = __GCONV_FULL_OUTPUT;                             \
00197            break;                                                    \
00198          }                                                           \
00199        if (ch == 0x5c)                                                      \
00200          *outptr++ = 0x5c;                                           \
00201        else if (ch == 0x7e)                                          \
00202          *outptr++ = 0x7e;                                           \
00203        else                                                          \
00204          *outptr++ = sc;                                             \
00205       }                                                                     \
00206                                                                      \
00207     /* Now that we wrote the output increment the input pointer.  */        \
00208     inptr += 4;                                                             \
00209   }
00210 #define LOOP_NEED_FLAGS
00211 #include <iconv/loop.c>
00212 
00213 /* Now define the toplevel functions.  */
00214 #include <iconv/skeleton.c>