]>
Commit | Line | Data |
---|---|---|
a44d2393 | 1 | /* Access functions for KS C 5601-1992 based encoding conversion. |
b168057a | 2 | Copyright (C) 1998-2015 Free Software Foundation, Inc. |
a44d2393 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
a44d2393 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
a44d2393 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
a44d2393 UD |
18 | |
19 | #ifndef _KSC5601_H | |
20 | #define _KSC5601_H 1 | |
21 | ||
22 | #define KSC5601_HANGUL 2350 | |
23 | #define KSC5601_HANJA 4888 | |
06c17c78 | 24 | #define KSC5601_SYMBOL 989 |
a44d2393 UD |
25 | |
26 | #include <gconv.h> | |
2aea1d79 | 27 | #include <stdint.h> |
a44d2393 | 28 | |
918b9d72 UD |
29 | /* Structure to map from UCS to KSC. This structure should be packed |
30 | on all platforms. */ | |
31 | struct map | |
32 | { | |
33 | uint16_t ucs; | |
34 | char val[2]; | |
35 | }; | |
36 | ||
a44d2393 | 37 | /* Conversion table. */ |
8619129f UD |
38 | extern const uint16_t __ksc5601_hangul_to_ucs[KSC5601_HANGUL]; |
39 | extern const uint16_t __ksc5601_sym_to_ucs[]; | |
918b9d72 | 40 | extern const struct map __ksc5601_sym_from_ucs[KSC5601_SYMBOL]; |
8619129f | 41 | extern const uint16_t __ksc5601_hanja_to_ucs[KSC5601_HANJA]; |
918b9d72 | 42 | extern const struct map __ksc5601_hanja_from_ucs[KSC5601_HANJA]; |
a44d2393 UD |
43 | |
44 | ||
8619129f | 45 | static inline uint32_t |
dd9423a6 | 46 | __attribute ((always_inline)) |
918b9d72 | 47 | ksc5601_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset) |
a44d2393 | 48 | { |
bd805dba | 49 | unsigned char ch = **s; |
a44d2393 UD |
50 | unsigned char ch2; |
51 | int idx; | |
52 | ||
53 | /* row 94(0x7e) and row 41(0x49) are user-defined area in KS C 5601 */ | |
54 | ||
918b9d72 UD |
55 | if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) >= 0x7e |
56 | || (ch - offset) == 0x49) | |
d64b6ad0 | 57 | return __UNKNOWN_10646_CHAR; |
a44d2393 | 58 | |
918b9d72 UD |
59 | if (avail < 2) |
60 | return 0; | |
61 | ||
62 | ch2 = (*s)[1]; | |
63 | if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) | |
d64b6ad0 | 64 | return __UNKNOWN_10646_CHAR; |
a44d2393 | 65 | |
918b9d72 | 66 | idx = (ch - offset - 0x21) * 94 + (ch2 - offset - 0x21); |
a44d2393 UD |
67 | |
68 | /* 1410 = 15 * 94 , 3760 = 40 * 94 | |
69 | Hangul in KS C 5601 : row 16 - row 40 */ | |
70 | ||
bd805dba | 71 | *s += 2; |
918b9d72 | 72 | |
bd805dba | 73 | if (idx >= 1410 && idx < 1410 + KSC5601_HANGUL) |
918b9d72 | 74 | return (__ksc5601_hangul_to_ucs[idx - 1410] |
b0f1ca68 | 75 | ?: (*s -= 2, __UNKNOWN_10646_CHAR)); |
66175fa8 | 76 | else if (idx >= 3854) |
a44d2393 | 77 | /* Hanja : row 42 - row 93 : 3854 = 94 * (42-1) */ |
918b9d72 | 78 | return (__ksc5601_hanja_to_ucs[idx - 3854] |
b0f1ca68 | 79 | ?: (*s -= 2, __UNKNOWN_10646_CHAR)); |
bd805dba | 80 | else if (idx <= 1114) |
b0f1ca68 | 81 | return __ksc5601_sym_to_ucs[idx] ?: (*s -= 2, __UNKNOWN_10646_CHAR); |
bd805dba | 82 | |
b0f1ca68 | 83 | *s -= 2; |
bd805dba | 84 | return __UNKNOWN_10646_CHAR; |
a44d2393 UD |
85 | } |
86 | ||
87 | static inline size_t | |
dd9423a6 | 88 | __attribute ((always_inline)) |
bc900b11 | 89 | ucs4_to_ksc5601_hangul (uint32_t wch, unsigned char *s, size_t avail) |
a44d2393 | 90 | { |
8619129f | 91 | int l = 0; |
8619129f UD |
92 | int u = KSC5601_HANGUL - 1; |
93 | uint32_t try; | |
a44d2393 UD |
94 | |
95 | while (l <= u) | |
96 | { | |
fed8f7f7 | 97 | int m = (l + u) / 2; |
918b9d72 | 98 | try = (uint32_t) __ksc5601_hangul_to_ucs[m]; |
a44d2393 UD |
99 | if (try > wch) |
100 | u = m - 1; | |
101 | else if (try < wch) | |
102 | l= m + 1; | |
103 | else | |
104 | { | |
918b9d72 UD |
105 | if (avail < 2) |
106 | return 0; | |
107 | ||
bc900b11 UD |
108 | s[0] = (m / 94) + 0x30; |
109 | s[1] = (m % 94) + 0x21; | |
918b9d72 | 110 | |
a44d2393 UD |
111 | return 2; |
112 | } | |
113 | } | |
918b9d72 | 114 | |
d64b6ad0 | 115 | return __UNKNOWN_10646_CHAR; |
a44d2393 UD |
116 | } |
117 | ||
118 | ||
119 | static inline size_t | |
dd9423a6 | 120 | __attribute ((always_inline)) |
bc900b11 | 121 | ucs4_to_ksc5601_hanja (uint32_t wch, unsigned char *s, size_t avail) |
a44d2393 | 122 | { |
8619129f | 123 | int l = 0; |
8619129f UD |
124 | int u = KSC5601_HANJA - 1; |
125 | uint32_t try; | |
a44d2393 UD |
126 | |
127 | while (l <= u) | |
128 | { | |
fed8f7f7 | 129 | int m = (l + u) / 2; |
918b9d72 | 130 | try = (uint32_t) __ksc5601_hanja_from_ucs[m].ucs; |
a44d2393 UD |
131 | if (try > wch) |
132 | u=m-1; | |
133 | else if (try < wch) | |
134 | l = m + 1; | |
135 | else | |
136 | { | |
918b9d72 UD |
137 | if (avail < 2) |
138 | return 0; | |
139 | ||
bc900b11 UD |
140 | s[0] = __ksc5601_hanja_from_ucs[m].val[0]; |
141 | s[1] = __ksc5601_hanja_from_ucs[m].val[1]; | |
918b9d72 | 142 | |
a44d2393 UD |
143 | return 2; |
144 | } | |
145 | } | |
918b9d72 | 146 | |
d64b6ad0 | 147 | return __UNKNOWN_10646_CHAR; |
a44d2393 UD |
148 | } |
149 | ||
150 | static inline size_t | |
dd9423a6 | 151 | __attribute ((always_inline)) |
bc900b11 | 152 | ucs4_to_ksc5601_sym (uint32_t wch, unsigned char *s, size_t avail) |
a44d2393 UD |
153 | { |
154 | int l = 0; | |
a44d2393 | 155 | int u = KSC5601_SYMBOL - 1; |
8619129f | 156 | uint32_t try; |
a44d2393 UD |
157 | |
158 | while (l <= u) | |
159 | { | |
fed8f7f7 | 160 | int m = (l + u) / 2; |
918b9d72 | 161 | try = __ksc5601_sym_from_ucs[m].ucs; |
a44d2393 UD |
162 | if (try > wch) |
163 | u = m - 1; | |
164 | else if (try < wch) | |
165 | l = m + 1; | |
166 | else | |
167 | { | |
918b9d72 UD |
168 | if (avail < 2) |
169 | return 0; | |
170 | ||
bc900b11 UD |
171 | s[0] = __ksc5601_sym_from_ucs[m].val[0]; |
172 | s[1] = __ksc5601_sym_from_ucs[m].val[1]; | |
918b9d72 | 173 | |
a44d2393 UD |
174 | return 2; |
175 | } | |
176 | } | |
918b9d72 | 177 | |
d64b6ad0 | 178 | return __UNKNOWN_10646_CHAR; |
a44d2393 UD |
179 | } |
180 | ||
181 | ||
a44d2393 | 182 | static inline size_t |
dd9423a6 | 183 | __attribute ((always_inline)) |
bc900b11 | 184 | ucs4_to_ksc5601 (uint32_t wch, unsigned char *s, size_t avail) |
a44d2393 | 185 | { |
918b9d72 | 186 | if (wch >= 0xac00 && wch <= 0xd7a3) |
bc900b11 | 187 | return ucs4_to_ksc5601_hangul (wch, s, avail); |
918b9d72 UD |
188 | else if ((wch >= 0x4e00 && wch <= 0x9fff) |
189 | || (wch >= 0xf900 && wch <= 0xfa0b)) | |
190 | return ucs4_to_ksc5601_hanja (wch, s, avail); | |
a44d2393 | 191 | else |
918b9d72 | 192 | return ucs4_to_ksc5601_sym (wch, s, avail); |
a44d2393 UD |
193 | } |
194 | ||
195 | #endif /* ksc5601.h */ |