📄 hangul.c
字号:
1, 1, 0, 0, 0, 0, /* でび,に,ぬ,ぬっ,ぬつ,ぬび */
0, 0,
0, 1, 1, 0 /* ば,ぱ,ぱび,び */
};
/* (罐魔) 俊 蝶扼 葛澜 葛剧俊 康氢 */
static int lcon_kind[] = {
0, 0, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
/* (葛澜) 俊 蝶扼 罐魔 葛剧 搬沥 */
static int lcon_map[] = {
0, 0,
0, 0, 2, 0, 2, 1,
0, 0,
2, 1, 2, 3, 0, 0,
0, 0,
0, 3, 3, 1, 1, 1,
0, 0,
3, 3, 0, 1
};
/* 肯己屈 内靛甫 炼钦屈 内靛肺 官槽促.
h : 肯己屈 内靛 霉 官捞飘
l : 肯己屈 内靛 滴锅掳 官捞飘
des : 炼钦屈 内靛甫 3 官捞飘肺 倒妨霖促.
*/
void convert_ks_to_3(h, l, des)
int h, l;
unsigned char *des;
{
int c;
int i;
if ((i = ks_table1_index(h, l)) >= 0 && i < sizeof(ks_table1)/sizeof(ks_table1[0])) {
*des++ = ks_table1[i][0];
*des++ = ks_table1[i][1];
*des++ = ks_table1[i][2];
}
else {
c = (h << 8) | l;
for (i = 0; i < 40; i++)
if (ks_table2[i][0] == c) {
*des++ = ks_table2[i][1];
*des++ = ks_table2[i][2];
*des++ = ks_table2[i][3];
return;
}
*des++ = 0xff; /* 弊贰侨 内靛 */
*des++ = h;
*des++ = l;
}
}
/* 炼钦屈 檬己 - 肯己屈 彻磊 函券 */
static unsigned char johab_fcon_to_wan[] = {
0,
0xd4, 0xa1, 0xa2, 0xa4, 0xa7, /* (盲框),ぁ,あ,い,ぇ */
0xa8, 0xa9, 0xb1, 0xb2, 0xb3, /* え,ぉ,け,げ,こ */
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, /* さ,ざ,し,じ,す */
0xba, 0xbb, 0xbc, 0xbd, 0xbe /* ず,せ,ぜ,そ,ぞ */
};
/* 炼钦屈 吝己 -> 肯己屈 彻磊 函券 */
static unsigned char johab_vow_to_wan[] = {
0, 0,
0xd4, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, /* (盲框),た,だ,ち,ぢ,っ */
0, 0,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, /* つ,づ,て,で,でた,でだ */
0, 0,
0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* でび,に,ぬ,ぬっ,ぬつ,ぬび */
0, 0,
0xd0, 0xd1, 0xd2, 0xd3 /* ば,ぱ,ぱび,び */
};
/* 炼钦屈 辆己 -> 肯己屈 彻磊 函券 */
static unsigned char johab_lcon_to_wan[] = {
0,
0xd4, 0xa1, 0xa2, 0xa3, 0xa4, /* (盲框), ぁ, あ, ぁさ, い */
0xa5, 0xa6, 0xa7, 0xa9, 0xaa, /* いじ, いぞ, ぇ, ぉ, ぉぁ */
0xab, 0xac, 0xad, 0xae, 0xaf, /* ぉけ, ぉげ, ぉさ, ぉぜ, ぉそ */
0xb0, 0xb1, 0, 0xb2, 0xb4, /* ぉぞ, け, 0, げ, げさ */
0xb5, 0xb6, 0xb7, 0xb8, 0xba, /* さ, ざ, し, じ, ず */
0xbb, 0xbc, 0xbd, 0xbe /* せ, ぜ, そ, ぞ */
};
/* 炼钦屈 内靛甫 肯己屈 内靛肺 官槽促.
f : 檬己 (1 = (盲框), 2 = ぁ, 3 = あ, 4 = い, 5 = ぇ, 6 = え, 7 = ぉ, ...
m : 吝己 (2 = (盲框), 3 = た, 4 = だ, 5 = ち, 6 = ぢ, 7 = っ, 8 = *, ...
l : 辆己 (1 = (盲框), 2 = ぁ, 3 = あ, 4 = ぁさ, 5 = い, 6 = いじ, ...
des : 肯己屈 内靛
肯己屈 内靛俊 绝阑 版快俊绰 促澜苞 鞍捞 8 byte 甫 倒妨 霖促.
(盲框) (檬己) (吝己) (辆己)
(盲框) = 0xA4D4
(檬己) = 0xA4A1 - 0xA4FE
(檬己) = 0xA4A1 - 0xA4FE
(檬己) = 0xA4A1 - 0xA4FE
搬苞 : des 俊 持绢柳 byte 荐, 2 | 8
*/
int convert_3_to_ks(f, m, l, des)
unsigned int f, m, l;
unsigned char *des;
{
unsigned char key[3];
register int hi, lo, mi, result, found;
if (f == 0xff) {
des[0] = m;
des[1] = l;
return 2;
}
key[0] = f;
key[1] = m;
key[2] = l;
lo = 0;
hi = sizeof(ks_table1)/3 - 1;
found = 0;
while (lo + 1 < hi) {
mi = (lo + hi)/2;
result = strncmp((char*)ks_table1[mi], (char*)key, 3);
if (result == 0) {
found = 1;
break;
}
else if (result > 0) {
hi = mi;
}
else {
lo = mi;
}
}
if (!found) {
if (!strncmp((char*)ks_table1[lo], (char*)key, 3)) {
found = 1;
mi = lo;
}
if (!strncmp((char*)ks_table1[hi], (char*)key, 3)) {
found = 1;
mi = hi;
}
}
if (!found) {
for (mi = 0; mi < 40; mi++)
if (ks_table2[mi][1] == f && ks_table2[mi][2] == m &&
ks_table2[mi][3] == l) {
des[0] = ks_table2[mi][0] >> 8;
des[1] = ks_table2[mi][0];
return 2; /* found */
}
}
else {
des[0] = mi / (0xff-0xa1) + 0xb0;
des[1] = mi % (0xff-0xa1) + 0xa1;
return 2; /* found */
}
/* 肯己屈 钎俊 绝促. ``KS C 5601 - 1992 沥焊 背券侩 何龋 秦汲''
3.3 例俊 汲疙等 规过栏肺 encoding 茄促. */
*des++ = 0xa4; /* 盲框 */
*des++ = 0xd4;
*des++ = 0xa4; /* 彻磊绰 葛滴 a4 青俊 乐促. */
*des++ = johab_fcon_to_wan[f];
*des++ = 0xa4;
*des++ = johab_vow_to_wan[m];
*des++ = 0xa4;
*des++ = johab_lcon_to_wan[l];
return 8;
}
/* 3 官捞飘肺 等 炼钦屈 -> 2 官捞飘 炼钦屈 */
int convert_3_to_johab( f , m , l , buf )
int f, m, l;
unsigned char *buf;
{
if (f == 0xff) {
if (m >= 0xa1 && m <= 0xac) {
buf[0] = (m - 0xa1) / 2 + 0xd9;
if (m & 1) { /* 0xa1, 0xa3, 0xa5, ... */
buf[1] = (l-0xa1 < 0x7f-0x31) ? l-0xa1+0x31 : l-0xa1+0x91-0x7f+0x31; /* bug ! */
} else {
buf[1] = l;
}
} else if (m >= 0xad && m <= 0xaf) { /* 沥狼 救等 康开 */
buf[0] = 0xd9; /* 傍鄂巩磊肺 盲框 */
buf[1] = 0x31;
} else if (m == 0xc9 || m == 0xfe) {
buf[0] = 0xd8;
if (m & 1) {
buf[1] = (l-0xa1 < 0x7f-0x31) ? l-0xa1+0x31 : l-0xa1+0x91;
} else {
buf[1] = l;
}
} else if (m > 0xc9 && m < 0xfe) {
buf[0] = (m - 0xca) / 2 + 0xe0;
if (m & 1) {
buf[1] = l;
} else {
buf[1] = (l-0xa1 < 0x7f-0x31) ? l-0xa1+0x31 : l-0xa1+0x91-0x7f+0x31; /* bug ! */
}
}
} else {
buf[0] = 0x80 | (f << 2) | (m >> 3);
buf[1] = (m << 5) | l;
}
return 2;
}
static char jamo_index1[]= { /* 0:fill, 2:ぁ, ... */
0, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
static char jamo_index2[]= { /* 0:fill 1:た, ... */
0, 0, 0, 1, 2, 3, 4, 5,
0, 0, 6, 7, 8, 9, 10, 11,
0, 0, 12, 13, 14, 15, 16, 17,
0, 0, 18, 19, 20, 21, 0, 0
};
static char jamo_index3[]= { /* 0:fill, 2:ぁ, ... */
0, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 0, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 0, 0
};
#if !defined(NO_PATCH_UTF8) /* by hanmaum 1998.2.5 */
static char johab_index1[]= { /* 0:fill, 1:ぁ, ... */
0, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
static char johab_index2[]= { /* 0:fill 1:た, ... */
0, 3, 4, 5, 6, 7, 10, 11,
12, 13, 14, 15, 18, 19, 20, 21,
22, 23, 26, 27, 28, 29, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
static char johab_index3[]= { /* 0:fill 1:ぁ, ... */
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 0, 0, 0, 0
};
#endif
#if !defined(NO_PATCH_UTF8) /* by hanmaum 1998.2.5 */
/* 3 官捞飘肺 等 炼钦屈 -> UTF-8 */
int convert_3_to_utf8( f , m , l , buf )
int f, m, l;
unsigned char *buf;
{
int index_f, index_m, index_l;
unsigned long code = 0;
#if defined(DEBUG_PATCH_UTF8) /* by hanmaum 1998.2.5 */
printf("convert_3_to_utf8(%02x,%02x,%02x)\n", f, m, l);
#endif
if (f == 0xff) {
code = convert_char_ksc5601_to_ucs2((unsigned char)m, (unsigned char)l);
}
else {
index_f = jamo_index1[f];
index_m = jamo_index2[m];
index_l = jamo_index3[l];
if (index_f > 0 && index_m > 0 && index_l >= 0) {
code = 0xac00 + ((((index_f-1) * 21) + (index_m-1)) * 28) + index_l;
/* 0xac00 <= code <= 0xd7a3 */
}
else if (index_f > 0 && index_m == 0 && index_l == 0) {
code = 0x3100
| (johab_fcon_to_wan[index_f+1] - (unsigned char)0x70);
}
else if (index_f == 0 && index_m > 0 && index_l == 0) {
code = 0x3100
| (johab_vow_to_wan[index_m+2] - (unsigned char)0x70);
}
else if (index_f > 0 && index_m == 0 && index_l == 0) {
code = 0x3100
| (johab_lcon_to_wan[index_l+1] - (unsigned char)0x70);
}
}
if (code > 0)
return convert_wc_to_mb(buf, code);
#if defined(DEBUG_PATCH_UTF8) /* by hanmaum 1998.2.5 */
printf("convert_3_to_utf8() scope error! (%d,%d,%d)\n", f, m, l);
#endif
buf[0] = '?';
buf[1] = '?';
return 2;
}
#endif
/* 炼钦屈 巩磊甫 3 官捞飘 炼钦屈栏肺 官槽促. */
convert_johab_to_3(src, f, m, l)
unsigned char *src;
unsigned int *f, *m, *l;
{
if (src[0] >= 0x84 && src[0] <= 0xd3) { /* 焊烹 茄臂 */
*f = (unsigned char)((src[0] >> 2) & 0x1f);
*m = (unsigned char)(((src[0] << 3) | (src[1] >> 5)) & 0x1f);
*l = (unsigned char)(src[1] & 0x1f);
} else { /* 漂荐 巩磊 */
*f = (unsigned char)0xff;
if (src[0] >= 0xd9 && src[0] <= 0xde) { /* 缴国甸 */
*m = (unsigned char)((src[0] - 0xd9)*2 + 0xa1);
} else if (src[0] >= 0xe0 && src[0] <= 0xf9) { /* 茄磊 */
*m = (unsigned char)((src[0] - 0xe0)*2 + 0xca); /* good */
} else if (src[0] == 0xd8) {
*m = (unsigned char) 0xc9;
}
if (src[1] >= 0x31 && src[1] <= 0x7e) {
*l = (unsigned char)(src[1] - 0x31 + 0xa1);
} else if (src[1] >= 0x91 && src[1] <= 0xa0) {
*l = (unsigned char)(src[1] - 0x91 + (0x7f - 0x31) + 0xa1);
} else if (src[1] >= 0xa1 && src[1] <= 0xfe) {
*l = src[1];
if (src[0] == 0xd8) {
*m = (unsigned char)0xfe;
}
else{
(*m)++;
}
} else {
*l = 0;
}
}
}
#if !defined(NO_PATCH_UTF8) /* by hanmaum 1998.2.5 */
/* 炼钦屈 巩磊甫 UTF-8肺 官槽促. */
int
convert_johab_to_utf8(src, des, len)
unsigned char *src;
unsigned char *des;
int len;
{
unsigned int f, m, l;
int des_len = 0;
#if defined(DEBUG_PATCH_UTF8) /* by hanmaum 1998.2.5 */
printf("convert_johab_to_utf8() : ");
if (len == 1) printf("%02x)", src[0]);
else if (len > 1) printf("%02x, %02x, ...)", src[0], src[1]);
printf("\n");
#endif
while (len > 0) {
if (src[0] < (unsigned char)0x80) {
des[0] = src[0];
src++, des++, len--, des_len++;
}
else if (src[0] >= 0x84 && src[0] <= 0xd3) { /* 焊烹 茄臂 */
f = (unsigned char)((src[0] >> 2) & 0x1f);
m = (unsigned char)(((src[0] << 3) | (src[1] >> 5)) & 0x1f);
l = (unsigned char)(src[1] & 0x1f);
des_len += convert_3_to_utf8(f, m, l, des);
des += des_len;
src += 2; len -= 2;
} else { /* 漂荐 巩磊 */
f = (unsigned char)0xff;
if (src[0] >= 0xd9 && src[0] <= 0xde) { /* 缴国甸 */
m = (unsigned char)((src[0] - 0xd9)*2 + 0xa1);
} else if (src[0] >= 0xe0 && src[0] <= 0xf9) { /* 茄磊 */
m = (unsigned char)((src[0] - 0xe0)*2 + 0xca); /* good */
} else if (src[0] == 0xd8) {
m = (unsigned char) 0xc9;
}
if (src[1] >= 0x31 && src[1] <= 0x7e) {
l = (unsigned char)(src[1] - 0x31 + 0xa1);
} else if (src[1] >= 0x91 && src[1] <= 0xa0) {
l = (unsigned char)(src[1] - 0x91 + (0x7f - 0x31) + 0xa1);
} else if (src[1] >= 0xa1 && src[1] <= 0xfe) {
l = src[1];
if (src[0] == 0xd8) {
m = (unsigned char)0xfe;
}
else{
m++;
}
} else {
l = 0;
}
des_len += convert_3_to_utf8(f, m, l, des);
des += des_len;
src += 2; len -= 2;
}
#if 0 /* not reached */
else
{
#if defined(DEBUG_PATCH_UTF8) /* by hanmaum 1998.2.5 */
printf("convert_johab_to_utf8() scope error! (%02x,%02x)\n",
src[0],src[1]);
#endif
des[0] = '?';
des[1] = '?';
des += 2; des_len += 2;
src += 2; len -= 2;
}
#endif
}
return des_len;
}
#endif
/* 肯己屈 巩磊凯阑 2 官捞飘 炼钦屈 巩磊凯肺 官槽促.
炼钦屈 巩磊凯狼 辨捞甫 倒妨霖促. */
convert_ks_to_johab(src, des, n)
unsigned char *src, *des;
int n;
{
int l;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -