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.
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.
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.
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. */
26 /* Definitions used in the body of the `gconv' function. */
27 #define CHARSET_NAME "GBK//"
28 #define FROM_LOOP from_gbk_to_gb
29 #define TO_LOOP from_gb_to_gbk
32 #define MIN_NEEDED_FROM 1
33 #define MAX_NEEDED_FROM 2
34 #define MIN_NEEDED_TO 1
35 #define MAX_NEEDED_TO 2
38 /* First define the conversion function from GBK to GB2312. */
39 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
40 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
41 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
42 #define MAX_NEEDED_OUTPUT MAX_NEEDED_TO
43 #define LOOPFCT FROM_LOOP
46 uint32_t ch = *inptr; \
49 *outptr++ = *inptr++; \
52 /* It's a two-byte sequence. We have to mask out all the sequences \
53 which are not in GB2312. Besides all of them in the range \
54 0x8140 to 0xA0FE this also includes in the remaining range the \
55 sequences which the second byte being in the range from 0x40 to \
56 0xA0 and the following exceptions: \
65 All these characters are not defined in GB2312. Besides this \
66 there is an incomatibility in the mapping. The Unicode tables \
67 say that 0xA1A4 maps in GB2312 to U30FB while in GBK it maps to \
68 U00B7. Similarly, 0xA1AA maps in GB2312 to U2015 while in GBK \
69 it maps to U2014. Since we are free to do whatever we want if \
70 a mapping is not available we will not flag this as an error \
71 but instead map the two positions. But this means that the \
74 UCS4 -> GB2312 -> GBK -> UCS4 \
76 might not produce identical text. */ \
77 if (__builtin_expect (inptr + 1 >= inend, 0)) \
79 /* The second character is not available. Store \
80 the intermediate result. */ \
81 result = __GCONV_INCOMPLETE_INPUT; \
85 if (__builtin_expect (outend - outptr < 2, 0)) \
87 /* We ran out of space. */ \
88 result = __GCONV_FULL_OUTPUT; \
92 ch = (ch << 8) | inptr[1]; \
94 /* Map 0xA844 (U2015 in GBK) to 0xA1AA (U2015 in GB2312). */ \
95 if (__builtin_expect (ch == 0xa844, 0)) \
98 /* Now determine whether the character is valid. */ \
99 if (__builtin_expect (ch, 0xa1a1) < 0xa1a1 \
100 || __builtin_expect (ch, 0xa1a1) > 0xf7fe \
101 || __builtin_expect (inptr[1], 0xa1) < 0xa1 \
102 /* Now test the exceptions. */ \
103 || (__builtin_expect (ch, 0xa1a1) >= 0xa2a1 \
104 && __builtin_expect (ch, 0xa1a1) <= 0xa2aa) \
105 || (__builtin_expect (ch, 0xa1a1) >= 0xa6e0 \
106 && __builtin_expect (ch, 0xa1a1) <= 0xa6f5) \
107 || (__builtin_expect (ch, 0xa1a1) >= 0xa8bb \
108 && __builtin_expect (ch, 0xa1a1) <= 0xa8c0)) \
110 /* One of the characters we cannot map. */ \
111 STANDARD_ERR_HANDLER (2); \
114 /* Copy the two bytes. */ \
115 *outptr++ = *inptr++; \
116 *outptr++ = *inptr++; \
119 #define LOOP_NEED_FLAGS
120 #include <iconv/loop.c>
123 /* Next, define the other direction. */
124 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
125 #define MAX_NEEDED_INPUT MAX_NEEDED_TO
126 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
127 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
128 #define LOOPFCT TO_LOOP
131 /* We don't have to care about characters we cannot map. The only \
132 problem are the mapping of 0xA1A4 and 0xA1AA but as explained above \
133 we do not do anything special here. */ \
134 unsigned char ch = *inptr++; \
138 if (__builtin_expect (inptr + 1 >= inend, 0)) \
140 /* The second character is not available. Store \
141 the intermediate result. */ \
142 result = __GCONV_INCOMPLETE_INPUT; \
146 if (__builtin_expect (outend - outptr < 2, 0)) \
148 /* We ran out of space. */ \
149 result = __GCONV_FULL_OUTPUT; \
158 #include <iconv/loop.c>
161 /* Now define the toplevel functions. */
162 #include <iconv/skeleton.c>