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