]>
Commit | Line | Data |
---|---|---|
40b4c81d UD |
1 | /* Access functions for GB2312 conversion. |
2 | Copyright (C) 1998 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. | |
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 | #ifndef _GB2312_H | |
22 | #define _GB2312_H 1 | |
23 | ||
24 | #include <gconv.h> | |
25 | #include <stdint.h> | |
26 | ||
27 | /* Conversion table. */ | |
8619129f | 28 | extern const uint16_t __gb2312_to_ucs[]; |
40b4c81d UD |
29 | |
30 | ||
9b26f5c4 | 31 | static inline uint32_t |
40b4c81d UD |
32 | gb2312_to_ucs4 (const char **s, size_t avail, unsigned char offset) |
33 | { | |
34 | unsigned char ch = *(*s); | |
35 | unsigned char ch2; | |
36 | int idx; | |
37 | ||
38 | if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x77) | |
39 | return UNKNOWN_10646_CHAR; | |
40 | ||
41 | if (avail < 2) | |
42 | return 0; | |
43 | ||
44 | ch2 = (*s)[1]; | |
45 | if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) | |
46 | return UNKNOWN_10646_CHAR; | |
47 | ||
48 | idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset); | |
2b474353 | 49 | if (idx > 0x1ff1) |
40b4c81d UD |
50 | return UNKNOWN_10646_CHAR; |
51 | ||
52 | (*s) += 2; | |
53 | ||
8619129f | 54 | return __gb2312_to_ucs[idx] ?: ((*s) -= 2, UNKNOWN_10646_CHAR); |
40b4c81d UD |
55 | } |
56 | ||
57 | ||
8619129f UD |
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]; | |
40b4c81d UD |
67 | |
68 | static inline size_t | |
9b26f5c4 | 69 | ucs4_to_gb2312 (uint32_t wch, char *s, size_t avail) |
40b4c81d UD |
70 | { |
71 | unsigned int ch = (unsigned int) wch; | |
72 | char buf[2]; | |
8619129f | 73 | const char *cp = buf; |
40b4c81d | 74 | |
8619129f | 75 | switch (ch) |
40b4c81d | 76 | { |
8619129f UD |
77 | case 0xa4 ... 0x100: |
78 | cp = __gb2312_from_ucs4_tab1[ch - 0xa4]; | |
79 | break; | |
80 | case 0x113: | |
81 | cp = "\x28\x25"; | |
82 | break; | |
83 | case 0x11b: | |
84 | cp = "\x28\x27"; | |
85 | break; | |
86 | case 0x12b: | |
87 | cp = "\x28\x29"; | |
88 | break; | |
89 | case 0x14d: | |
90 | cp = "\x28\x2d"; | |
91 | break; | |
92 | case 0x16b: | |
93 | cp = "\x28\x31"; | |
94 | break; | |
95 | case 0x1ce: | |
96 | cp = "\x28\x23"; | |
97 | break; | |
98 | case 0x1d0: | |
99 | cp = "\x28\x2b"; | |
100 | break; | |
101 | case 0x1d2: | |
102 | cp = "\x28\x2f"; | |
103 | break; | |
104 | case 0x1d4: | |
105 | cp = "\x28\x33"; | |
106 | break; | |
107 | case 0x1d6: | |
108 | cp = "\x28\x35"; | |
109 | break; | |
110 | case 0x1d8: | |
111 | cp = "\x28\x36"; | |
112 | break; | |
113 | case 0x1da: | |
114 | cp = "\x28\x37"; | |
115 | break; | |
116 | case 0x1dc: | |
117 | cp = "\x28\x38"; | |
118 | break; | |
119 | case 0x2c7: | |
120 | cp = "\x21\x26"; | |
121 | break; | |
122 | case 0x2c9: | |
123 | cp = "\x21\x25"; | |
124 | break; | |
125 | case 0x391 ... 0x3c9: | |
126 | cp = __gb2312_from_ucs4_tab2[ch - 0x391]; | |
127 | break; | |
128 | case 0x401 ... 0x451: | |
129 | cp = __gb2312_from_ucs4_tab3[ch - 0x401]; | |
130 | break; | |
131 | case 0x2015 ... 0x203b: | |
132 | cp = __gb2312_from_ucs4_tab4[ch - 0x2015]; | |
133 | break; | |
134 | case 0x2103 ... 0x22a5: | |
135 | cp = __gb2312_from_ucs4_tab5[ch - 0x2103]; | |
136 | break; | |
137 | case 0x2313: | |
138 | cp = "\x21\x50"; | |
139 | break; | |
140 | case 0x2460 ... 0x249b: | |
141 | cp = __gb2312_from_ucs4_tab6[ch - 0x2460]; | |
142 | break; | |
143 | case 0x2500 ... 0x254b: | |
40b4c81d UD |
144 | buf[0] = '\x29'; |
145 | buf[1] = '\x24' + (ch & 256); | |
8619129f UD |
146 | break; |
147 | case 0x25a0: | |
148 | cp = "\x21\x76"; | |
149 | break; | |
150 | case 0x25a1: | |
151 | cp = "\x21\x75"; | |
152 | break; | |
153 | case 0x25b2: | |
154 | cp = "\x21\x78"; | |
155 | break; | |
156 | case 0x25b3: | |
157 | cp = "\x21\x77"; | |
158 | break; | |
159 | case 0x25c6: | |
160 | cp = "\x21\x74"; | |
161 | break; | |
162 | case 0x25c7: | |
163 | cp = "\x21\x73"; | |
164 | break; | |
165 | case 0x25cb: | |
166 | cp = "\x21\x70"; | |
167 | break; | |
168 | case 0x25ce: | |
169 | cp = "\x21\x72"; | |
170 | break; | |
171 | case 0x25cf: | |
172 | cp = "\x21\x71"; | |
173 | break; | |
174 | case 0x2605: | |
175 | cp = "\x21\x6f"; | |
176 | break; | |
177 | case 0x2606: | |
178 | cp = "\x21\x6e"; | |
179 | break; | |
180 | case 0x2640: | |
181 | cp = "\x21\x62"; | |
182 | break; | |
183 | case 0x2642: | |
184 | cp = "\x21\x61"; | |
185 | break; | |
186 | case 0x3000 ... 0x3129: | |
187 | cp = __gb2312_from_ucs4_tab7[ch - 0x3000]; | |
188 | break; | |
189 | case 0x3220 ... 0x3229: | |
40b4c81d UD |
190 | buf[0] = '\x22'; |
191 | buf[1] = '\x65' + (ch - 0x3220); | |
8619129f UD |
192 | break; |
193 | case 0x4e00 ... 0x9fa0: | |
194 | cp = __gb2312_from_ucs4_tab8[ch - 0x4e00]; | |
195 | break; | |
196 | case 0xff01 ... 0xff5e: | |
197 | cp = __gb2312_from_ucs4_tab9[ch - 0xff01]; | |
198 | break; | |
199 | case 0xffe0: | |
200 | cp = "\x21\x69"; | |
201 | break; | |
202 | case 0xffe1: | |
203 | cp = "\x21\x6a"; | |
204 | break; | |
205 | case 0xffe3: | |
206 | cp = "\x23\x7e"; | |
207 | break; | |
208 | case 0xffe5: | |
209 | cp = "\x23\x24"; | |
210 | break; | |
211 | default: | |
212 | return UNKNOWN_10646_CHAR; | |
40b4c81d | 213 | } |
40b4c81d UD |
214 | |
215 | if (cp[1] != '\0' && avail < 2) | |
216 | return 0; | |
217 | ||
218 | s[0] = cp[0]; | |
219 | s[1] = cp[1]; | |
220 | ||
221 | return 2; | |
222 | } | |
223 | ||
224 | #endif /* gb2312.h */ |