1
/* Access functions for KS C 5601-1992 based encoding conversion.
2
Copyright (C) 1998, 1999, 2000, 2003, 2007 Free Software Foundation, Inc.
3
This file is part of the GNU C Library.
5
The GNU C Library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
10
The GNU C Library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
15
You should have received a copy of the GNU Lesser General Public
16
License along with the GNU C Library; if not, write to the Free
17
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23
#define KSC5601_HANGUL 2350
24
#define KSC5601_HANJA 4888
25
#define KSC5601_SYMBOL 989
30
/* Structure to map from UCS to KSC. This structure should be packed
38
/* Conversion table. */
39
extern const uint16_t __ksc5601_hangul_to_ucs[KSC5601_HANGUL];
40
extern const uint16_t __ksc5601_sym_to_ucs[];
41
extern const struct map __ksc5601_sym_from_ucs[KSC5601_SYMBOL];
42
extern const uint16_t __ksc5601_hanja_to_ucs[KSC5601_HANJA];
43
extern const struct map __ksc5601_hanja_from_ucs[KSC5601_HANJA];
46
static inline uint32_t
47
__attribute ((always_inline))
48
ksc5601_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
50
unsigned char ch = **s;
54
/* row 94(0x7e) and row 41(0x49) are user-defined area in KS C 5601 */
56
if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) >= 0x7e
57
|| (ch - offset) == 0x49)
58
return __UNKNOWN_10646_CHAR;
64
if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
65
return __UNKNOWN_10646_CHAR;
67
idx = (ch - offset - 0x21) * 94 + (ch2 - offset - 0x21);
69
/* 1410 = 15 * 94 , 3760 = 40 * 94
70
Hangul in KS C 5601 : row 16 - row 40 */
74
if (idx >= 1410 && idx < 1410 + KSC5601_HANGUL)
75
return (__ksc5601_hangul_to_ucs[idx - 1410]
76
?: (*s -= 2, __UNKNOWN_10646_CHAR));
78
/* Hanja : row 42 - row 93 : 3854 = 94 * (42-1) */
79
return (__ksc5601_hanja_to_ucs[idx - 3854]
80
?: (*s -= 2, __UNKNOWN_10646_CHAR));
82
return __ksc5601_sym_to_ucs[idx] ?: (*s -= 2, __UNKNOWN_10646_CHAR);
85
return __UNKNOWN_10646_CHAR;
89
__attribute ((always_inline))
90
ucs4_to_ksc5601_hangul (uint32_t wch, unsigned char *s, size_t avail)
93
int u = KSC5601_HANGUL - 1;
99
try = (uint32_t) __ksc5601_hangul_to_ucs[m];
109
s[0] = (m / 94) + 0x30;
110
s[1] = (m % 94) + 0x21;
116
return __UNKNOWN_10646_CHAR;
121
__attribute ((always_inline))
122
ucs4_to_ksc5601_hanja (uint32_t wch, unsigned char *s, size_t avail)
125
int u = KSC5601_HANJA - 1;
131
try = (uint32_t) __ksc5601_hanja_from_ucs[m].ucs;
141
s[0] = __ksc5601_hanja_from_ucs[m].val[0];
142
s[1] = __ksc5601_hanja_from_ucs[m].val[1];
148
return __UNKNOWN_10646_CHAR;
152
__attribute ((always_inline))
153
ucs4_to_ksc5601_sym (uint32_t wch, unsigned char *s, size_t avail)
156
int u = KSC5601_SYMBOL - 1;
162
try = __ksc5601_sym_from_ucs[m].ucs;
172
s[0] = __ksc5601_sym_from_ucs[m].val[0];
173
s[1] = __ksc5601_sym_from_ucs[m].val[1];
179
return __UNKNOWN_10646_CHAR;
184
__attribute ((always_inline))
185
ucs4_to_ksc5601 (uint32_t wch, unsigned char *s, size_t avail)
187
if (wch >= 0xac00 && wch <= 0xd7a3)
188
return ucs4_to_ksc5601_hangul (wch, s, avail);
189
else if ((wch >= 0x4e00 && wch <= 0x9fff)
190
|| (wch >= 0xf900 && wch <= 0xfa0b))
191
return ucs4_to_ksc5601_hanja (wch, s, avail);
193
return ucs4_to_ksc5601_sym (wch, s, avail);
196
#endif /* ksc5601.h */