2 * _codecs_hk.c: Codecs collection for encodings from Hong Kong
4 * Written by Hye-Shik Chang <perky@FreeBSD.org>
7 #define USING_IMPORTED_MAPS
10 #include "mappings_hk.h"
16 static const encode_map
*big5_encmap
= NULL
;
17 static const decode_map
*big5_decmap
= NULL
;
21 static int initialized
= 0;
23 if (!initialized
&& IMPORT_MAP(tw
, big5
, &big5_encmap
, &big5_decmap
))
30 * There are four possible pair unicode -> big5hkscs maps as in HKSCS 2004:
31 * U+00CA U+0304 -> 8862 (U+00CA alone is mapped to 8866)
32 * U+00CA U+030C -> 8864
33 * U+00EA U+0304 -> 88a3 (U+00EA alone is mapped to 88a7)
34 * U+00EA U+030C -> 88a5
35 * These are handled by not mapping tables but a hand-written code.
37 static const DBCHAR big5hkscs_pairenc_table
[4] = {0x8862, 0x8864, 0x88a3, 0x88a5};
48 **outbuf
= (unsigned char)c
;
54 insize
= GET_INSIZE(c
);
59 TRYMAP_ENC(big5hkscs_bmp
, code
, c
) {
62 ((c
& 0xffdf) == 0x00ca) &&
63 (((*inbuf
)[1] & 0xfff7) == 0x0304)) {
64 code
= big5hkscs_pairenc_table
[
66 ((*inbuf
)[1] >> 3)) & 3];
69 else if (inleft
< 2 &&
70 !(flags
& MBENC_FLUSH
))
80 else TRYMAP_ENC(big5
, code
, c
);
85 else if (c
< 0x30000) {
86 TRYMAP_ENC(big5hkscs_nonbmp
, code
, c
& 0xffff);
100 #define BH2S(c1, c2) (((c1) - 0x87) * (0xfe - 0x40 + 1) + ((c2) - 0x40))
105 unsigned char c
= IN1
;
118 if (0xc6 <= c
&& c
<= 0xc8 && (c
>= 0xc7 || IN2
>= 0xa1))
121 TRYMAP_DEC(big5
, **outbuf
, c
, IN2
) {
125 hkscsdec
: TRYMAP_DEC(big5hkscs
, decoded
, c
, IN2
) {
126 int s
= BH2S(c
, IN2
);
127 const unsigned char *hintbase
;
129 assert(0x87 <= c
&& c
<= 0xfe);
130 assert(0x40 <= IN2
&& IN2
<= 0xfe);
132 if (BH2S(0x87, 0x40) <= s
&& s
<= BH2S(0xa0, 0xfe)) {
133 hintbase
= big5hkscs_phint_0
;
134 s
-= BH2S(0x87, 0x40);
136 else if (BH2S(0xc6,0xa1) <= s
&& s
<= BH2S(0xc8,0xfe)){
137 hintbase
= big5hkscs_phint_12130
;
138 s
-= BH2S(0xc6, 0xa1);
140 else if (BH2S(0xf9,0xd6) <= s
&& s
<= BH2S(0xfe,0xfe)){
141 hintbase
= big5hkscs_phint_21924
;
142 s
-= BH2S(0xf9, 0xd6);
145 return MBERR_INTERNAL
;
147 if (hintbase
[s
>> 3] & (1 << (s
& 7))) {
148 WRITEUCS4(decoded
| 0x20000)
157 switch ((c
<< 8) | IN2
) {
158 case 0x8862: WRITE2(0x00ca, 0x0304); break;
159 case 0x8864: WRITE2(0x00ca, 0x030c); break;
160 case 0x88a3: WRITE2(0x00ea, 0x0304); break;
161 case 0x88a5: WRITE2(0x00ea, 0x030c); break;
165 NEXT(2, 2) /* all decoded codepoints are pairs, above. */
174 MAPPING_DECONLY(big5hkscs
)
175 MAPPING_ENCONLY(big5hkscs_bmp
)
176 MAPPING_ENCONLY(big5hkscs_nonbmp
)
180 CODEC_STATELESS_WINIT(big5hkscs
)
183 I_AM_A_MODULE_FOR(hk
)