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