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