ยปCore Development>Code coverage>Modules/cjkcodecs/_codecs_hk.c

Python code coverage for Modules/cjkcodecs/_codecs_hk.c

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