]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Modules/cjkcodecs/_codecs_hk.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Modules / cjkcodecs / _codecs_hk.c
CommitLineData
4710c53d 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
16static const encode_map *big5_encmap = NULL;\r
17static const decode_map *big5_decmap = NULL;\r
18\r
19CODEC_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
37static const DBCHAR big5hkscs_pairenc_table[4] = {0x8862, 0x8864, 0x88a3, 0x88a5};\r
38\r
39ENCODER(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
102DECODER(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 goto hkscsdec;\r
120\r
121 TRYMAP_DEC(big5, **outbuf, c, IN2) {\r
122 NEXT(2, 1)\r
123 }\r
124 else\r
125hkscsdec: TRYMAP_DEC(big5hkscs, decoded, c, IN2) {\r
126 int s = BH2S(c, IN2);\r
127 const unsigned char *hintbase;\r
128\r
129 assert(0x87 <= c && c <= 0xfe);\r
130 assert(0x40 <= IN2 && IN2 <= 0xfe);\r
131\r
132 if (BH2S(0x87, 0x40) <= s && s <= BH2S(0xa0, 0xfe)) {\r
133 hintbase = big5hkscs_phint_0;\r
134 s -= BH2S(0x87, 0x40);\r
135 }\r
136 else if (BH2S(0xc6,0xa1) <= s && s <= BH2S(0xc8,0xfe)){\r
137 hintbase = big5hkscs_phint_12130;\r
138 s -= BH2S(0xc6, 0xa1);\r
139 }\r
140 else if (BH2S(0xf9,0xd6) <= s && s <= BH2S(0xfe,0xfe)){\r
141 hintbase = big5hkscs_phint_21924;\r
142 s -= BH2S(0xf9, 0xd6);\r
143 }\r
144 else\r
145 return MBERR_INTERNAL;\r
146\r
147 if (hintbase[s >> 3] & (1 << (s & 7))) {\r
148 WRITEUCS4(decoded | 0x20000)\r
149 NEXT_IN(2)\r
150 }\r
151 else {\r
152 OUT1(decoded)\r
153 NEXT(2, 1)\r
154 }\r
155 }\r
156 else {\r
157 switch ((c << 8) | IN2) {\r
158 case 0x8862: WRITE2(0x00ca, 0x0304); break;\r
159 case 0x8864: WRITE2(0x00ca, 0x030c); break;\r
160 case 0x88a3: WRITE2(0x00ea, 0x0304); break;\r
161 case 0x88a5: WRITE2(0x00ea, 0x030c); break;\r
162 default: return 2;\r
163 }\r
164\r
165 NEXT(2, 2) /* all decoded codepoints are pairs, above. */\r
166 }\r
167 }\r
168\r
169 return 0;\r
170}\r
171\r
172\r
173BEGIN_MAPPINGS_LIST\r
174 MAPPING_DECONLY(big5hkscs)\r
175 MAPPING_ENCONLY(big5hkscs_bmp)\r
176 MAPPING_ENCONLY(big5hkscs_nonbmp)\r
177END_MAPPINGS_LIST\r
178\r
179BEGIN_CODECS_LIST\r
180 CODEC_STATELESS_WINIT(big5hkscs)\r
181END_CODECS_LIST\r
182\r
183I_AM_A_MODULE_FOR(hk)\r