Back to index

glibc  2.9
gbgbk.c
Go to the documentation of this file.
00001 /* Mapping tables from GBK to GB2312 and vice versa.
00002    Copyright (C) 1999, 2000-2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
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 <gconv.h>
00023 #include <stdint.h>
00024 
00025 
00026 /* Definitions used in the body of the `gconv' function.  */
00027 #define CHARSET_NAME        "GBK//"
00028 #define FROM_LOOP           from_gbk_to_gb
00029 #define TO_LOOP                    from_gb_to_gbk
00030 #define DEFINE_INIT         1
00031 #define DEFINE_FINI         1
00032 #define MIN_NEEDED_FROM            1
00033 #define MAX_NEEDED_FROM            2
00034 #define MIN_NEEDED_TO              1
00035 #define MAX_NEEDED_TO              2
00036 
00037 
00038 /* First define the conversion function from GBK to GB2312.  */
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 MAX_NEEDED_OUTPUT   MAX_NEEDED_TO
00043 #define LOOPFCT                    FROM_LOOP
00044 #define BODY \
00045   {                                                                  \
00046     uint32_t ch = *inptr;                                            \
00047                                                                      \
00048     if (ch <= 0x7f)                                                  \
00049       *outptr++ = *inptr++;                                          \
00050     else                                                             \
00051       {                                                                     \
00052        /* It's a two-byte sequence.  We have to mask out all the sequences   \
00053           which are not in GB2312.  Besides all of them in the range        \
00054           0x8140 to 0xA0FE this also includes in the remaining range the     \
00055           sequences which the second byte being in the range from 0x40 to    \
00056           0xA0 and the following exceptions:                                \
00057                                                                      \
00058             0xA2A1 to 0xA2A9,                                               \
00059             0xA2AA,                                                  \
00060             0xA6E0 to 0xA6EB,                                               \
00061             0xA6EE to 0xA6F2,                                               \
00062             0xA6F4, 0xA6F5,                                          \
00063             0xA8BB to 0xA8C0                                                \
00064                                                                      \
00065           All these characters are not defined in GB2312.  Besides this      \
00066           there is an incomatibility in the mapping.  The Unicode tables     \
00067           say that 0xA1A4 maps in GB2312 to U30FB while in GBK it maps to    \
00068           U00B7.  Similarly, 0xA1AA maps in GB2312 to U2015 while in GBK     \
00069           it maps to U2014.  Since we are free to do whatever we want if     \
00070           a mapping is not available we will not flag this as an error             \
00071           but instead map the two positions.  But this means that the       \
00072           mapping                                                    \
00073                                                                      \
00074               UCS4 -> GB2312 -> GBK -> UCS4                                 \
00075                                                                      \
00076           might not produce identical text.  */                      \
00077        if (__builtin_expect (inptr + 1 >= inend, 0))                        \
00078          {                                                           \
00079            /* The second character is not available.  Store                 \
00080               the intermediate result.  */                                  \
00081            result = __GCONV_INCOMPLETE_INPUT;                               \
00082            break;                                                    \
00083          }                                                           \
00084                                                                      \
00085        if (__builtin_expect (outend - outptr < 2, 0))                       \
00086          {                                                           \
00087            /* We ran out of space.  */                                      \
00088            result = __GCONV_FULL_OUTPUT;                             \
00089            break;                                                    \
00090          }                                                           \
00091                                                                      \
00092        ch = (ch << 8) | inptr[1];                                    \
00093                                                                      \
00094        /* Map 0xA844 (U2015 in GBK) to 0xA1AA (U2015 in GB2312).  */        \
00095        if (__builtin_expect (ch == 0xa844, 0))                              \
00096          ch = 0xa1aa;                                                       \
00097                                                                      \
00098        /* Now determine whether the character is valid.  */                 \
00099        if (__builtin_expect (ch < 0xa1a1, 0)                                \
00100            || __builtin_expect (ch > 0xf7fe, 0)                      \
00101            || __builtin_expect (inptr[1] < 0xa1, 0)                         \
00102            /* Now test the exceptions.  */                                  \
00103            || (__builtin_expect (ch >= 0xa2a1, 0)                           \
00104               && __builtin_expect (ch <= 0xa2aa, 0))                        \
00105            || (__builtin_expect (ch >= 0xa6e0, 0)                           \
00106               && __builtin_expect (ch <= 0xa6f5, 0))                        \
00107            || (__builtin_expect (ch >= 0xa8bb, 0)                           \
00108               && __builtin_expect (ch <= 0xa8c0, 0)))                       \
00109          {                                                           \
00110            /* One of the characters we cannot map.  */                      \
00111            STANDARD_TO_LOOP_ERR_HANDLER (2);                                \
00112          }                                                           \
00113                                                                      \
00114        /* Copy the two bytes.  */                                    \
00115        *outptr++ = *inptr++;                                                \
00116        *outptr++ = *inptr++;                                                \
00117       }                                                                     \
00118   }
00119 #define LOOP_NEED_FLAGS
00120 #include <iconv/loop.c>
00121 
00122 
00123 /* Next, define the other direction.  */
00124 #define MIN_NEEDED_INPUT    MIN_NEEDED_TO
00125 #define MAX_NEEDED_INPUT    MAX_NEEDED_TO
00126 #define MIN_NEEDED_OUTPUT   MIN_NEEDED_FROM
00127 #define MAX_NEEDED_OUTPUT   MAX_NEEDED_FROM
00128 #define LOOPFCT                    TO_LOOP
00129 #define BODY \
00130   {                                                                  \
00131     /* We don't have to care about characters we cannot map.  The only             \
00132        problem are the mapping of 0xA1A4 and 0xA1AA but as explained above    \
00133        we do not do anything special here.  */                              \
00134     unsigned char ch = *inptr++;                                     \
00135                                                                      \
00136     if (ch > 0x7f)                                                   \
00137       {                                                                     \
00138        if (__builtin_expect (inptr + 1 >= inend, 0))                        \
00139          {                                                           \
00140            /* The second character is not available.  Store                 \
00141                the intermediate result.  */                                 \
00142            result = __GCONV_INCOMPLETE_INPUT;                               \
00143            break;                                                    \
00144          }                                                           \
00145                                                                      \
00146        if (__builtin_expect (outend - outptr < 2, 0))                       \
00147          {                                                           \
00148            /* We ran out of space.  */                                      \
00149            result = __GCONV_FULL_OUTPUT;                             \
00150            break;                                                    \
00151          }                                                           \
00152                                                                      \
00153        *outptr++ = ch;                                                      \
00154        ch = *inptr++;                                                       \
00155       }                                                                     \
00156     *outptr++ = ch;                                                  \
00157   }
00158 #include <iconv/loop.c>
00159 
00160 
00161 /* Now define the toplevel functions.  */
00162 #include <iconv/skeleton.c>