1 /* Access functions for GB2312 conversion.
2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
27 /* Conversion table. */
28 extern const uint16_t __gb2312_to_ucs[];
31 static inline uint32_t
32 __attribute ((always_inline))
33 gb2312_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
35 unsigned char ch = *(*s);
39 if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x77)
40 return __UNKNOWN_10646_CHAR;
46 if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
47 return __UNKNOWN_10646_CHAR;
49 idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
51 return __UNKNOWN_10646_CHAR;
55 return __gb2312_to_ucs[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
59 extern const char __gb2312_from_ucs4_tab1[][2];
60 extern const char __gb2312_from_ucs4_tab2[][2];
61 extern const char __gb2312_from_ucs4_tab3[][2];
62 extern const char __gb2312_from_ucs4_tab4[][2];
63 extern const char __gb2312_from_ucs4_tab5[][2];
64 extern const char __gb2312_from_ucs4_tab6[][2];
65 extern const char __gb2312_from_ucs4_tab7[][2];
66 extern const char __gb2312_from_ucs4_tab8[][2];
67 extern const char __gb2312_from_ucs4_tab9[][2];
70 __attribute ((always_inline))
71 ucs4_to_gb2312 (uint32_t wch, unsigned char *s, size_t avail)
73 unsigned int ch = (unsigned int) wch;
80 cp = __gb2312_from_ucs4_tab1[ch - 0xa4];
127 case 0x391 ... 0x3c9:
128 cp = __gb2312_from_ucs4_tab2[ch - 0x391];
130 case 0x401 ... 0x451:
131 cp = __gb2312_from_ucs4_tab3[ch - 0x401];
133 case 0x2015 ... 0x203b:
134 cp = __gb2312_from_ucs4_tab4[ch - 0x2015];
136 case 0x2103 ... 0x22a5:
137 cp = __gb2312_from_ucs4_tab5[ch - 0x2103];
142 case 0x2460 ... 0x249b:
143 cp = __gb2312_from_ucs4_tab6[ch - 0x2460];
145 case 0x2500 ... 0x254b:
147 buf[1] = '\x24' + (ch % 256);
188 case 0x3000 ... 0x3129:
189 cp = __gb2312_from_ucs4_tab7[ch - 0x3000];
191 case 0x3220 ... 0x3229:
193 buf[1] = '\x65' + (ch - 0x3220);
195 case 0x4e00 ... 0x9fa0:
196 cp = __gb2312_from_ucs4_tab8[ch - 0x4e00];
198 case 0xff01 ... 0xff5e:
199 cp = __gb2312_from_ucs4_tab9[ch - 0xff01];
214 return __UNKNOWN_10646_CHAR;
218 return __UNKNOWN_10646_CHAR;
220 assert (cp[1] != '\0');
231 #endif /* gb2312.h */