aboutsummaryrefslogtreecommitdiffstats
path: root/libwc/hkscs.c
blob: 9badf76032625fb710f57b2010ca56d4801b7030 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "wc.h"
#include "big5.h"
#include "hkscs.h"
#include "search.h"
#include "wtf.h"
#ifdef USE_UNICODE
#include "ucs.h"
#endif

#define C0 WC_HKSCS_MAP_C0
#define GL WC_HKSCS_MAP_GL
#define C1 WC_HKSCS_MAP_C1
#define LB WC_HKSCS_MAP_LB
#define UB WC_HKSCS_MAP_UB
#define UH WC_HKSCS_MAP_UH

wc_uint8 WC_HKSCS_MAP[ 0x100 ] = {
    C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0,
    C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0,
    GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
    GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
    LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB,
    LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB,
    LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB,
    LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, LB, C0,

    C1, C1, C1, C1, C1, C1, C1, C1, UH, UH, UH, UH, UH, UH, UH, UH,
    UH, UH, UH, UH, UH, UH, UH, UH, UH, UH, UH, UH, UH, UH, UH, UH,
    UH, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB,
    UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB,
    UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB,
    UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB,
    UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB,
    UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, UB, C1,
};

wc_wchar_t
wc_hkscs_to_cs128w(wc_wchar_t cc)
{
    cc.code = WC_HKSCS_N(cc.code);
    if (cc.code < 0x4000)
	cc.ccs = WC_CCS_HKSCS_1;
    else {
	cc.ccs = WC_CCS_HKSCS_2;
	cc.code -= 0x4000;
    }
    cc.code = WC_N_CS128W(cc.code);
    return cc;
}

wc_wchar_t
wc_cs128w_to_hkscs(wc_wchar_t cc)
{
    cc.code = WC_CS128W_N(cc.code);
    if (cc.ccs == WC_CCS_HKSCS_2)
	cc.code += 0x4000;
    cc.ccs = WC_CCS_HKSCS;
    cc.code = WC_N_HKSCS(cc.code);
    return cc;
}

wc_uint32
wc_hkscs_to_N(wc_uint32 c)
{
    if (c < 0xA140)	/* 0x8840 - 0xA0FE */
	return WC_HKSCS_N(c);
    			/* 0xFA40 - 0xFEFE */
    return WC_HKSCS_N(c) - 0x59 * 0x9D;
}

Str
wc_conv_from_hkscs(Str is, wc_ces ces)
{
    Str os;
    wc_uchar *sp = (wc_uchar *)is->ptr;
    wc_uchar *ep = sp + is->length;
    wc_uchar *p;
    int state = WC_HKSCS_NOSTATE;
    wc_uint32 hkscs;

    for (p = sp; p < ep && *p < 0x80; p++) 
	;
    if (p == ep)
	return is;
    os = Strnew_size(is->length);
    if (p > sp)
	Strcat_charp_n(os, (char *)is->ptr, (int)(p - sp));

    for (; p < ep; p++) {
	switch (state) {
	case WC_HKSCS_NOSTATE:
	    switch (WC_HKSCS_MAP[*p]) {
	    case UB:
	    case UH:
		state = WC_HKSCS_MBYTE1;
		break;
	    case C1:
		wtf_push_unknown(os, p, 1);
		break;
	    default:
		Strcat_char(os, (char)*p);
		break;
	    }
	    break;
	case WC_HKSCS_MBYTE1:
	    if (WC_HKSCS_MAP[*p] & LB) {
		hkscs = ((wc_uint32)*(p-1) << 8) | *p;
		if (*(p-1) >= 0xA1 && *(p-1) <= 0xF9)
		    wtf_push(os, WC_CCS_BIG5, hkscs);
		else
		    wtf_push(os, WC_CCS_HKSCS, hkscs);
	    } else
		wtf_push_unknown(os, p-1, 2);
	    state = WC_HKSCS_NOSTATE;
	    break;
	}
    }
    switch (state) {
    case WC_HKSCS_MBYTE1:
	wtf_push_unknown(os, p-1, 1);
	break;
    }
    return os;
}

void
wc_push_to_hkscs(Str os, wc_wchar_t cc, wc_status *st)
{
  while (1) {
    switch (cc.ccs) {
    case WC_CCS_US_ASCII:
	Strcat_char(os, (char)cc.code);
	return;
    case WC_CCS_BIG5_1:
    case WC_CCS_BIG5_2:
	cc = wc_cs94w_to_big5(cc);
    case WC_CCS_BIG5:
	Strcat_char(os, (char)(cc.code >> 8));
	Strcat_char(os, (char)(cc.code & 0xff));
	return;
    case WC_CCS_HKSCS_1:
    case WC_CCS_HKSCS_2:
	cc = wc_cs128w_to_hkscs(cc);
    case WC_CCS_HKSCS:
	Strcat_char(os, (char)(cc.code >> 8));
	Strcat_char(os, (char)(cc.code & 0xff));
	return;
    case WC_CCS_UNKNOWN_W:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE_W);
	return;
    case WC_CCS_UNKNOWN:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE);
	return;
    default:
#ifdef USE_UNICODE
	if (WcOption.ucs_conv)
	    cc = wc_any_to_any_ces(cc, st);
	else
#endif
	    cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
	continue;
    }
  }
}

Str
wc_char_conv_from_hkscs(wc_uchar c, wc_status *st)
{
    static Str os;
    static wc_uchar hkscsu;
    wc_uint32 hkscs;

    if (st->state == -1) {
	st->state = WC_HKSCS_NOSTATE;
	os = Strnew_size(8);
    }

    switch (st->state) {
    case WC_HKSCS_NOSTATE:
	switch (WC_HKSCS_MAP[c]) {
	case UB:
	case UH:
	    hkscsu = c;
	    st->state = WC_HKSCS_MBYTE1;
	    return NULL;
	case C1:
	    break;
	default:
	    Strcat_char(os, (char)c);
	    break;
	}
	break;
    case WC_HKSCS_MBYTE1:
	if (WC_HKSCS_MAP[c] & LB) {
	    hkscs = ((wc_uint32)hkscsu << 8) | c;
	    if (hkscsu >= 0xA1 && hkscsu <= 0xF9 && c >= 0xA1)
		wtf_push(os, WC_CCS_BIG5, hkscs);
	    else
		wtf_push(os, WC_CCS_HKSCS, hkscs);
	}
	break;
    }
    st->state = -1;
    return os;
}