Debian Codegraph @

pypy/module/_multibytecodec/src/cjkcodecs/_codecs_hk.c

raw symbols SCIP
1 /*
2 * _codecs_hk.c: Codecs collection for encodings from Hong Kong
3 *
4 * Written by Hye-Shik Chang <[email protected]>
5 */
6
7 #define USING_IMPORTED_MAPS
8
9 #include "src/cjkcodecs/cjkcodecs.h"
10 #include "src/cjkcodecs/mappings_hk.h"
11
12 /*
13 * BIG5HKSCS codec
14 */
15
16 USING_IMPORTED_MAP(big5);
17 static const encode_map *big5_encmap = NULL;
18 static const decode_map *big5_decmap = NULL;
19
20 CODEC_INIT(big5hkscs)
21 {
22 IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap);
23 return 0;
24 }
25
26 /*
27 * There are four possible pair unicode -> big5hkscs maps as in HKSCS 2004:
28 * U+00CA U+0304 -> 8862 (U+00CA alone is mapped to 8866)
29 * U+00CA U+030C -> 8864
30 * U+00EA U+0304 -> 88a3 (U+00EA alone is mapped to 88a7)
31 * U+00EA U+030C -> 88a5
32 * These are handled by not mapping tables but a hand-written code.
33 */
34 static const DBCHAR big5hkscs_pairenc_table[4] = {0x8862, 0x8864, 0x88a3, 0x88a5};
35
36 ENCODER(big5hkscs)
37 {
38 while (inleft > 0) {
39 ucs4_t c = **inbuf;
40 DBCHAR code;
41 Py_ssize_t insize;
42
43 if (c < 0x80) {
44 REQUIRE_OUTBUF(1)
45 **outbuf = (unsigned char)c;
46 NEXT(1, 1)
47 continue;
48 }
49
50 DECODE_SURROGATE(c)
51 insize = GET_INSIZE(c);
52
53 REQUIRE_OUTBUF(2)
54
55 if (c < 0x10000) {
56 TRYMAP_ENC(big5hkscs_bmp, code, c) {
57 if (code == MULTIC) {
58 if (inleft >= 2 &&
59 ((c & 0xffdf) == 0x00ca) &&
60 (((*inbuf)[1] & 0xfff7) == 0x0304)) {
61 code = big5hkscs_pairenc_table[
62 ((c >> 4) |
63 ((*inbuf)[1] >> 3)) & 3];
64 insize = 2;
65 }
66 else if (inleft < 2 &&
67 !(flags & MBENC_FLUSH))
68 return MBERR_TOOFEW;
69 else {
70 if (c == 0xca)
71 code = 0x8866;
72 else /* c == 0xea */
73 code = 0x88a7;
74 }
75 }
76 }
77 else TRYMAP_ENC(big5, code, c);
78 else return 1;
79 }
80 else if (c < 0x20000)
81 return insize;
82 else if (c < 0x30000) {
83 TRYMAP_ENC(big5hkscs_nonbmp, code, c & 0xffff);
84 else return insize;
85 }
86 else
87 return insize;
88
89 OUT1(code >> 8)
90 OUT2(code & 0xFF)
91 NEXT(insize, 2)
92 }
93
94 return 0;
95 }
96
97 #define BH2S(c1, c2) (((c1) - 0x87) * (0xfe - 0x40 + 1) + ((c2) - 0x40))
98
99 DECODER(big5hkscs)
100 {
101 while (inleft > 0) {
102 unsigned char c = IN1;
103 ucs4_t decoded;
104
105 REQUIRE_OUTBUF(1)
106
107 if (c < 0x80) {
108 OUT1(c)
109 NEXT(1, 1)
110 continue;
111 }
112
113 REQUIRE_INBUF(2)
114
115 if (0xc6 > c || c > 0xc8 || (c < 0xc7 && IN2 < 0xa1)) {
116 TRYMAP_DEC(big5, **outbuf, c, IN2) {
117 NEXT(2, 1)
118 continue;
119 }
120 }
121
122 TRYMAP_DEC(big5hkscs, decoded, c, IN2)
123 {
124 int s = BH2S(c, IN2);
125 const unsigned char *hintbase;
126
127 assert(0x87 <= c && c <= 0xfe);
128 assert(0x40 <= IN2 && IN2 <= 0xfe);
129
130 if (BH2S(0x87, 0x40) <= s && s <= BH2S(0xa0, 0xfe)) {
131 hintbase = big5hkscs_phint_0;
132 s -= BH2S(0x87, 0x40);
133 }
134 else if (BH2S(0xc6,0xa1) <= s && s <= BH2S(0xc8,0xfe)){
135 hintbase = big5hkscs_phint_12130;
136 s -= BH2S(0xc6, 0xa1);
137 }
138 else if (BH2S(0xf9,0xd6) <= s && s <= BH2S(0xfe,0xfe)){
139 hintbase = big5hkscs_phint_21924;
140 s -= BH2S(0xf9, 0xd6);
141 }
142 else
143 return MBERR_INTERNAL;
144
145 if (hintbase[s >> 3] & (1 << (s & 7))) {
146 WRITEUCS4(decoded | 0x20000)
147 NEXT_IN(2)
148 }
149 else {
150 OUT1(decoded)
151 NEXT(2, 1)
152 }
153 continue;
154 }
155
156 switch ((c << 8) | IN2) {
157 case 0x8862: WRITE2(0x00ca, 0x0304); break;
158 case 0x8864: WRITE2(0x00ca, 0x030c); break;
159 case 0x88a3: WRITE2(0x00ea, 0x0304); break;
160 case 0x88a5: WRITE2(0x00ea, 0x030c); break;
161 default: return 1;
162 }
163
164 NEXT(2, 2) /* all decoded codepoints are pairs, above. */
165 }
166
167 return 0;
168 }
169
170
171 BEGIN_MAPPINGS_LIST
172 MAPPING_DECONLY(big5hkscs)
173 MAPPING_ENCONLY(big5hkscs_bmp)
174 MAPPING_ENCONLY(big5hkscs_nonbmp)
175 END_MAPPINGS_LIST
176
177 BEGIN_CODECS_LIST
178 CODEC_STATELESS_WINIT(big5hkscs)
179 END_CODECS_LIST
180
181 I_AM_A_MODULE_FOR(hk)