0fbbfc81e4251dfe3a34e36ea13cff5453ca681d
[platform/upstream/glibc.git] / iconvdata / gbgbk.c
1 /* Mapping tables from GBK to GB2312 and vice versa.
2    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <gconv.h>
22 #include <stdint.h>
23
24
25 /* Definitions used in the body of the `gconv' function.  */
26 #define CHARSET_NAME            "GBK//"
27 #define FROM_LOOP               from_gbk_to_gb
28 #define TO_LOOP                 from_gb_to_gbk
29 #define DEFINE_INIT             1
30 #define DEFINE_FINI             1
31 #define MIN_NEEDED_FROM         1
32 #define MAX_NEEDED_FROM         2
33 #define MIN_NEEDED_TO           1
34 #define MAX_NEEDED_TO           2
35
36
37 /* First define the conversion function from GBK to GB2312.  */
38 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
39 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
40 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
41 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_TO
42 #define LOOPFCT                 FROM_LOOP
43 #define BODY \
44   {                                                                           \
45     uint32_t ch = *inptr;                                                     \
46                                                                               \
47     if (ch <= 0x7f)                                                           \
48       *outptr++ = *inptr++;                                                   \
49     else                                                                      \
50       {                                                                       \
51         /* It's a two-byte sequence.  We have to mask out all the sequences   \
52            which are not in GB2312.  Besides all of them in the range         \
53            0x8140 to 0xA0FE this also includes in the remaining range the     \
54            sequences which the second byte being in the range from 0x40 to    \
55            0xA0 and the following exceptions:                                 \
56                                                                               \
57              0xA2A1 to 0xA2A9,                                                \
58              0xA2AA,                                                          \
59              0xA6E0 to 0xA6EB,                                                \
60              0xA6EE to 0xA6F2,                                                \
61              0xA6F4, 0xA6F5,                                                  \
62              0xA8BB to 0xA8C0                                                 \
63                                                                               \
64            All these characters are not defined in GB2312.  Besides this      \
65            there is an incomatibility in the mapping.  The Unicode tables     \
66            say that 0xA1A4 maps in GB2312 to U30FB while in GBK it maps to    \
67            U00B7.  Since we are free to do whatever we want if a mapping      \
68            is not available we will not flag this as an error but instead     \
69            map the two positions.  But this means that the mapping            \
70                                                                               \
71                 UCS4 -> GB2312 -> GBK -> UCS4                                 \
72                                                                               \
73            might not produce identical text.  */                              \
74         if (NEED_LENGTH_TEST && __builtin_expect (inptr + 1 >= inend, 0))     \
75           {                                                                   \
76             /* The second character is not available.  Store                  \
77                the intermediate result.  */                                   \
78             result = __GCONV_INCOMPLETE_INPUT;                                \
79             break;                                                            \
80           }                                                                   \
81                                                                               \
82         if (NEED_LENGTH_TEST && __builtin_expect (outend - outptr < 2, 0))    \
83           {                                                                   \
84             /* We ran out of space.  */                                       \
85             result = __GCONV_FULL_OUTPUT;                                     \
86             break;                                                            \
87           }                                                                   \
88                                                                               \
89         ch = (ch << 8) | inptr[1];                                            \
90                                                                               \
91         /* Now determine whether the character is valid.  */                  \
92         if (__builtin_expect (ch, 0xa1a1) < 0xa1a1                            \
93             || __builtin_expect (ch, 0xa1a1) > 0xf7fe                         \
94             || __builtin_expect (inptr[1], 0xa1) < 0xa1                       \
95             /* Now test the exceptions.  */                                   \
96             || (__builtin_expect (ch, 0xa1a1) >= 0xa2a1                       \
97                 && __builtin_expect (ch, 0xa1a1) <= 0xa2aa)                   \
98             || (__builtin_expect (ch, 0xa1a1) >= 0xa6e0                       \
99                 && __builtin_expect (ch, 0xa1a1) <= 0xa6f5)                   \
100             || (__builtin_expect (ch, 0xa1a1) >= 0xa8bb                       \
101                 && __builtin_expect (ch, 0xa1a1) <= 0xa8c0))                  \
102           {                                                                   \
103             /* One of the characters we cannot map.  */                       \
104             if (! ignore_errors_p ())                                         \
105               {                                                               \
106                 result = __GCONV_ILLEGAL_INPUT;                               \
107                 break;                                                        \
108               }                                                               \
109                                                                               \
110             inptr += 2;                                                       \
111             ++*irreversible;                                                  \
112           }                                                                   \
113         else                                                                  \
114           {                                                                   \
115             /* Copy the two bytes.  */                                        \
116             *outptr++ = *inptr++;                                             \
117             *outptr++ = *inptr++;                                             \
118           }                                                                   \
119       }                                                                       \
120   }
121 #include <iconv/loop.c>
122
123
124 /* Next, define the other direction.  */
125 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
126 #define MAX_NEEDED_INPUT        MAX_NEEDED_TO
127 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
128 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
129 #define LOOPFCT                 TO_LOOP
130 #define BODY \
131   {                                                                           \
132     /* We don't have to care about characters we cannot map.  The only        \
133        problem is the mapping of 0xA1A4 but as explained above we do not      \
134        do anything special here.  */                                          \
135     unsigned char ch = *inptr++;                                              \
136                                                                               \
137     if (ch > 0x7f)                                                            \
138       {                                                                       \
139         if (NEED_LENGTH_TEST && __builtin_expect (inptr + 1 >= inend, 0))     \
140           {                                                                   \
141             /* The second character is not available.  Store                  \
142                  the intermediate result.  */                                 \
143             result = __GCONV_INCOMPLETE_INPUT;                                \
144             break;                                                            \
145           }                                                                   \
146                                                                               \
147         if (NEED_LENGTH_TEST && __builtin_expect (outend - outptr < 2, 0))    \
148           {                                                                   \
149             /* We ran out of space.  */                                       \
150             result = __GCONV_FULL_OUTPUT;                                     \
151             break;                                                            \
152           }                                                                   \
153                                                                               \
154         *outptr++ = ch;                                                       \
155         ch = *inptr++;                                                        \
156       }                                                                       \
157     *outptr++ = ch;                                                           \
158   }
159 #include <iconv/loop.c>
160
161
162 /* Now define the toplevel functions.  */
163 #include <iconv/skeleton.c>