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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
#define NONE 0xfd
#define FILL 0xff
static const unsigned char jamo_initial[32] = {
NONE, FILL, 0x01, 0x02, 0x04, 0x07, 0x08, 0x09,
0x11, 0x12, 0x13, 0x15, 0x16, 0x17, 0x18, 0x19,
0x1a, 0x1b, 0x1c, 0x1d, 0x1e, NONE, NONE, NONE,
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
};
static const unsigned char jamo_medial[32] = {
NONE, NONE, FILL, 0x1f, 0x20, 0x21, 0x22, 0x23,
NONE, NONE, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
NONE, NONE, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
NONE, NONE, 0x30, 0x31, 0x32, 0x33, NONE, NONE,
};
static const unsigned char jamo_final[32] = {
NONE, FILL, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, NONE, 0x12, 0x14, 0x15, 0x16, 0x17,
0x18, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, NONE, NONE,
};
static const unsigned char jamo_final_notinitial[32] = {
NONE, NONE, NONE, NONE, 0x03, NONE, 0x05, 0x06,
NONE, NONE, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, NONE, NONE, NONE, 0x14, NONE, NONE, NONE,
NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
};
#define none -1
#define fill 0
static const signed char jamo_initial_index[32] = {
none, fill, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, none, none, none,
none, none, none, none, none, none, none, none,
};
static const signed char jamo_medial_index[32] = {
none, none, fill, 0x01, 0x02, 0x03, 0x04, 0x05,
none, none, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
none, none, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
none, none, 0x12, 0x13, 0x14, 0x15, none, none,
};
static const signed char jamo_final_index[32] = {
none, fill, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, none, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, none, none,
};
static int
johab_hangul_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
{
unsigned char c1 = s[0];
if ((c1 >= 0x84 && c1 <= 0xd3)) {
if (n >= 2) {
unsigned char c2 = s[1];
if ((c2 >= 0x41 && c2 < 0x7f) || (c2 >= 0x81 && c2 < 0xff)) {
unsigned int johab = (c1 << 8) | c2;
unsigned int bitspart1 = (johab >> 10) & 31;
unsigned int bitspart2 = (johab >> 5) & 31;
unsigned int bitspart3 = johab & 31;
int index1 = jamo_initial_index[bitspart1];
int index2 = jamo_medial_index[bitspart2];
int index3 = jamo_final_index[bitspart3];
if (index1 >= 0 && index2 >= 0 && index3 >= 0) {
if (index1 == fill) {
if (index2 == fill) {
unsigned char jamo3 = jamo_final_notinitial[bitspart3];
if (jamo3 != NONE) {
*pwc = (ucs4_t) 0x3130 + jamo3;
return 2;
}
} else if (index3 == fill) {
unsigned char jamo2 = jamo_medial[bitspart2];
if (jamo2 != NONE && jamo2 != FILL) {
*pwc = (ucs4_t) 0x3130 + jamo2;
return 2;
}
}
} else if (index2 == fill) {
if (index3 == fill) {
unsigned char jamo1 = jamo_initial[bitspart1];
if (jamo1 != NONE && jamo1 != FILL) {
*pwc = (ucs4_t) 0x3130 + jamo1;
return 2;
}
}
} else {
*pwc = 0xac00 + ((index1 - 1) * 21 + (index2 - 1)) * 28 + index3;
return 2;
}
}
}
return RET_ILSEQ;
}
return RET_TOOFEW(0);
}
return RET_ILSEQ;
}
static const unsigned short johab_hangul_page31[51] = {
0x8841, 0x8c41, 0x8444, 0x9041, 0x8446, 0x8447, 0x9441,
0x9841, 0x9c41, 0x844a, 0x844b, 0x844c, 0x844d, 0x844e, 0x844f,
0x8450, 0xa041, 0xa441, 0xa841, 0x8454, 0xac41, 0xb041, 0xb441,
0xb841, 0xbc41, 0xc041, 0xc441, 0xc841, 0xcc41, 0xd041, 0x8461,
0x8481, 0x84a1, 0x84c1, 0x84e1, 0x8541, 0x8561, 0x8581, 0x85a1,
0x85c1, 0x85e1, 0x8641, 0x8661, 0x8681, 0x86a1, 0x86c1, 0x86e1,
0x8741, 0x8761, 0x8781, 0x87a1,
};
static const char jamo_initial_index_inverse[19] = {
0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14,
};
static const char jamo_medial_index_inverse[21] = {
0x03, 0x04, 0x05, 0x06, 0x07,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x1a, 0x1b, 0x1c, 0x1d,
};
static const char jamo_final_index_inverse[28] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
};
static int
johab_hangul_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
{
if (n >= 2) {
if (wc >= 0x3131 && wc < 0x3164) {
unsigned short c = johab_hangul_page31[wc-0x3131];
r[0] = (c >> 8); r[1] = (c & 0xff);
return 2;
} else if (wc >= 0xac00 && wc < 0xd7a4) {
unsigned int index1;
unsigned int index2;
unsigned int index3;
unsigned short c;
unsigned int tmp = wc - 0xac00;
index3 = tmp % 28; tmp = tmp / 28;
index2 = tmp % 21; tmp = tmp / 21;
index1 = tmp;
c = (((((1 << 5)
| jamo_initial_index_inverse[index1]) << 5)
| jamo_medial_index_inverse[index2]) << 5)
| jamo_final_index_inverse[index3];
r[0] = (c >> 8); r[1] = (c & 0xff);
return 2;
}
return RET_ILUNI;
}
return RET_TOOSMALL;
}
static int johab_hangul_decompose (conv_t conv, ucs4_t* r, ucs4_t wc)
{
unsigned char buf[2];
int ret = johab_hangul_wctomb(conv,buf,wc,2);
if (ret != RET_ILUNI) {
unsigned int hangul = (buf[0] << 8) | buf[1];
unsigned char jamo1 = jamo_initial[(hangul >> 10) & 31];
unsigned char jamo2 = jamo_medial[(hangul >> 5) & 31];
unsigned char jamo3 = jamo_final[hangul & 31];
if ((hangul >> 15) != 1) abort();
if (jamo1 != NONE && jamo2 != NONE && jamo3 != NONE) {
ucs4_t* p = r;
if (jamo1 != FILL)
*p++ = 0x3130 + jamo1;
if (jamo2 != FILL)
*p++ = 0x3130 + jamo2;
if (jamo3 != FILL)
*p++ = 0x3130 + jamo3;
return p-r;
}
}
return RET_ILUNI;
}
#undef fill
#undef none
#undef FILL
#undef NONE
|