Back to index

glibc  2.9
ibm932.c
Go to the documentation of this file.
00001 /* Conversion from and to IBM932.
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 "ibm932.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 "IBM932//"
00031 #define FROM_LOOP    from_ibm932
00032 #define TO_LOOP             to_ibm932
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-932 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 = __ibm932db_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 IBM932 table for single byte.  */                     \
00064     res = __ibm932sb_to_ucs4[ch];                                    \
00065     if (__builtin_expect (res == 0, 0) && ch != 0)                          \
00066       {                                                                     \
00067        /* Use the IBM932 table for double byte.  */                         \
00068        if (__builtin_expect (inptr + 1 >= inend, 0))                        \
00069          {                                                           \
00070            /* The second character is not available.                        \
00071               Store the intermediate result.  */                     \
00072            result = __GCONV_INCOMPLETE_INPUT;                               \
00073            break;                                                    \
00074          }                                                           \
00075                                                                      \
00076        ch = (ch * 0x100) + inptr[1];                                        \
00077        while (ch > rp2->end)                                                \
00078          ++rp2;                                                      \
00079                                                                      \
00080        if (__builtin_expect (rp2 == NULL, 0)                                \
00081            || __builtin_expect (ch < rp2->start, 0)                         \
00082            || (res = __ibm932db_to_ucs4[ch + rp2->idx],              \
00083            __builtin_expect (res, '\1') == 0 && ch !=0))                    \
00084          {                                                           \
00085            /* This is an illegal character.  */                      \
00086            STANDARD_FROM_LOOP_ERR_HANDLER (2);                              \
00087          }                                                           \
00088        else                                                          \
00089          {                                                           \
00090            put32 (outptr, res);                                      \
00091            outptr += 4;                                              \
00092            inptr += 2;                                                      \
00093          }                                                           \
00094       }                                                                     \
00095     else                                                             \
00096       {                                                                     \
00097        if (res == 0xa5)                                              \
00098          res = 0x5c;                                                 \
00099        else if (res == 0x203e)                                              \
00100          res = 0x7e;                                                 \
00101        put32 (outptr, res);                                          \
00102        outptr += 4;                                                  \
00103        inptr++;                                                      \
00104       }                                                                     \
00105   }
00106 #define LOOP_NEED_FLAGS
00107 #define ONEBYTE_BODY \
00108   {                                                                  \
00109     if (c == 0x80 || c == 0xa0 || c >= 0xfd)                                \
00110       return WEOF;                                                   \
00111     uint32_t res = __ibm932sb_to_ucs4[c];                            \
00112     if (res == 0 && c != 0)                                          \
00113       return WEOF;                                                   \
00114     if (res == 0xa5)                                                  \
00115       res = 0x5c;                                                    \
00116     else if (res == 0x203e)                                          \
00117       res = 0x7e;                                                    \
00118     return res;                                                             \
00119   }
00120 #include <iconv/loop.c>
00121 
00122 /* Next, define the other direction.  */
00123 #define MIN_NEEDED_INPUT    MIN_NEEDED_TO
00124 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_FROM
00125 #define MAX_NEEDED_OUTPUT   MAX_NEEDED_FROM
00126 #define LOOPFCT                    TO_LOOP
00127 #define BODY \
00128   {                                                                  \
00129     const struct gap *rp = __ucs4_to_ibm932sb_idx;                          \
00130     unsigned char sc;                                                       \
00131     uint32_t ch = get32 (inptr);                                     \
00132     bool found = true;                                                      \
00133     uint32_t i;                                                             \
00134     uint32_t low;                                                    \
00135     uint32_t high;                                                   \
00136     uint16_t pccode;                                                 \
00137                                                                      \
00138     if (__builtin_expect (ch >= 0xffff, 0))                                 \
00139       {                                                                     \
00140        UNICODE_TAG_HANDLER (ch, 4);                                         \
00141        rp = NULL;                                                    \
00142       }                                                                     \
00143     else                                                             \
00144       while (ch > rp->end)                                           \
00145        ++rp;                                                         \
00146                                                                      \
00147     /* Use the UCS4 table for single byte.  */                              \
00148     if (__builtin_expect (rp == NULL, 0)                             \
00149        || __builtin_expect (ch < rp->start, 0)                              \
00150        || (sc = __ucs4_to_ibm932sb[ch + rp->idx],                           \
00151        __builtin_expect (sc, '\1') == '\0' && ch != L'\0'))                 \
00152       {                                                                     \
00153                                                                      \
00154        /* Use the UCS4 table for double byte.  */                           \
00155        found = false;                                                       \
00156        low = 0;                                                      \
00157        high = (sizeof (__ucs4_to_ibm932db) >> 1)                     \
00158               / sizeof (__ucs4_to_ibm932db[0][FROM]);                       \
00159        pccode = ch;                                                  \
00160        if (__builtin_expect (rp != NULL, 1))                                \
00161          while (low < high)                                          \
00162            {                                                         \
00163              i = (low + high) >> 1;                                         \
00164              if (pccode < __ucs4_to_ibm932db[i][FROM])                      \
00165               high = i;                                              \
00166              else if (pccode > __ucs4_to_ibm932db[i][FROM])                 \
00167               low = i + 1;                                           \
00168              else                                                    \
00169               {                                                      \
00170                 pccode = __ucs4_to_ibm932db[i][TO];                         \
00171                 found = true;                                               \
00172                 break;                                               \
00173               }                                                      \
00174            }                                                         \
00175        if (found)                                                    \
00176          {                                                           \
00177            if (__builtin_expect (outptr + 2 > outend, 0))                   \
00178              {                                                              \
00179               result = __GCONV_FULL_OUTPUT;                                 \
00180               break;                                                 \
00181              }                                                              \
00182            *outptr++ = pccode >> 8 & 0xff;                                  \
00183            *outptr++ = pccode & 0xff;                                       \
00184          }                                                           \
00185        else                                                          \
00186          {                                                           \
00187            /* This is an illegal character.  */                      \
00188            STANDARD_TO_LOOP_ERR_HANDLER (4);                                \
00189          }                                                           \
00190       }                                                                     \
00191     else                                                             \
00192       {                                                                     \
00193        if (__builtin_expect (outptr + 1 > outend, 0))                       \
00194          {                                                           \
00195            result = __GCONV_FULL_OUTPUT;                             \
00196            break;                                                    \
00197          }                                                           \
00198        if (ch == 0x5c)                                                      \
00199          *outptr++ = 0x5c;                                           \
00200        else if (ch == 0x7e)                                          \
00201          *outptr++ = 0x7e;                                           \
00202        else                                                          \
00203          *outptr++ = sc;                                             \
00204       }                                                                     \
00205                                                                      \
00206     /* Now that we wrote the output increment the input pointer.  */        \
00207     inptr += 4;                                                             \
00208   }
00209 #define LOOP_NEED_FLAGS
00210 #include <iconv/loop.c>
00211 
00212 /* Now define the toplevel functions.  */
00213 #include <iconv/skeleton.c>