1 /* Access functions for GB2312 conversion.
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
26 /* Conversion table. */
27 extern const uint16_t __gb2312_to_ucs
[];
30 static inline uint32_t
31 __attribute ((always_inline
))
32 gb2312_to_ucs4 (const unsigned char **s
, size_t avail
, unsigned char offset
)
34 unsigned char ch
= *(*s
);
38 if (ch
< offset
|| (ch
- offset
) <= 0x20 || (ch
- offset
) > 0x77)
39 return __UNKNOWN_10646_CHAR
;
45 if ((ch2
- offset
) <= 0x20 || (ch2
- offset
) >= 0x7f)
46 return __UNKNOWN_10646_CHAR
;
48 idx
= (ch
- 0x21 - offset
) * 94 + (ch2
- 0x21 - offset
);
50 return __UNKNOWN_10646_CHAR
;
54 return __gb2312_to_ucs
[idx
] ?: ((*s
) -= 2, __UNKNOWN_10646_CHAR
);
58 extern const char __gb2312_from_ucs4_tab1
[][2];
59 extern const char __gb2312_from_ucs4_tab2
[][2];
60 extern const char __gb2312_from_ucs4_tab3
[][2];
61 extern const char __gb2312_from_ucs4_tab4
[][2];
62 extern const char __gb2312_from_ucs4_tab5
[][2];
63 extern const char __gb2312_from_ucs4_tab6
[][2];
64 extern const char __gb2312_from_ucs4_tab7
[][2];
65 extern const char __gb2312_from_ucs4_tab8
[][2];
66 extern const char __gb2312_from_ucs4_tab9
[][2];
69 __attribute ((always_inline
))
70 ucs4_to_gb2312 (uint32_t wch
, unsigned char *s
, size_t avail
)
72 unsigned int ch
= (unsigned int) wch
;
79 cp
= __gb2312_from_ucs4_tab1
[ch
- 0xa4];
126 case 0x391 ... 0x3c9:
127 cp
= __gb2312_from_ucs4_tab2
[ch
- 0x391];
129 case 0x401 ... 0x451:
130 cp
= __gb2312_from_ucs4_tab3
[ch
- 0x401];
132 case 0x2015 ... 0x203b:
133 cp
= __gb2312_from_ucs4_tab4
[ch
- 0x2015];
135 case 0x2103 ... 0x22a5:
136 cp
= __gb2312_from_ucs4_tab5
[ch
- 0x2103];
141 case 0x2460 ... 0x249b:
142 cp
= __gb2312_from_ucs4_tab6
[ch
- 0x2460];
144 case 0x2500 ... 0x254b:
146 buf
[1] = '\x24' + (ch
% 256);
187 case 0x3000 ... 0x3129:
188 cp
= __gb2312_from_ucs4_tab7
[ch
- 0x3000];
190 case 0x3220 ... 0x3229:
192 buf
[1] = '\x65' + (ch
- 0x3220);
194 case 0x4e00 ... 0x9fa0:
195 cp
= __gb2312_from_ucs4_tab8
[ch
- 0x4e00];
197 case 0xff01 ... 0xff5e:
198 cp
= __gb2312_from_ucs4_tab9
[ch
- 0xff01];
213 return __UNKNOWN_10646_CHAR
;
217 return __UNKNOWN_10646_CHAR
;
219 assert (cp
[1] != '\0');
230 #endif /* gb2312.h */