* sysdeps/m68k/dl-machine.h (elf_machine_runtime_setup): Set
[glibc.git] / iconvdata / ksc5601.h
blob0b10dcd6b383cd6aafe131c56114bc5ea5cc66cf
1 /* Access functions for KS C 5601-1992 based encoding conversion.
2 Copyright (C) 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifndef _KSC5601_H
21 #define _KSC5601_H 1
23 #define KSC5601_HANGUL 2350
24 #define KSC5601_HANJA 4888
25 #define KSC5601_SYMBOL 986
27 #include <gconv.h>
28 #include <stdint.h>
30 /* Structure to map from UCS to KSC. This structure should be packed
31 on all platforms. */
32 struct map
34 uint16_t ucs;
35 char val[2];
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 ksc5601_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
49 unsigned char ch = *(*s);
50 unsigned char ch2;
51 int idx;
53 /* row 94(0x7e) and row 41(0x49) are user-defined area in KS C 5601 */
55 if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) >= 0x7e
56 || (ch - offset) == 0x49)
57 return UNKNOWN_10646_CHAR;
59 if (avail < 2)
60 return 0;
62 ch2 = (*s)[1];
63 if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
64 return UNKNOWN_10646_CHAR;
66 idx = (ch - offset - 0x21) * 94 + (ch2 - offset - 0x21);
68 /* 1410 = 15 * 94 , 3760 = 40 * 94
69 Hangul in KS C 5601 : row 16 - row 40 */
71 (*s) += 2;
73 if (idx >= 1410 && idx < 3760)
74 return (__ksc5601_hangul_to_ucs[idx - 1410]
75 ?: ((*s) -= 2, UNKNOWN_10646_CHAR));
76 else if (idx >= 3854)
77 /* Hanja : row 42 - row 93 : 3854 = 94 * (42-1) */
78 return (__ksc5601_hanja_to_ucs[idx - 3854]
79 ?: ((*s) -= 2, UNKNOWN_10646_CHAR));
80 else
81 return __ksc5601_sym_to_ucs[idx] ?: ((*s) -= 2, UNKNOWN_10646_CHAR);
84 static inline size_t
85 ucs4_to_ksc5601_hangul (uint32_t wch, unsigned char *s, size_t avail)
87 int l = 0;
88 int m;
89 int u = KSC5601_HANGUL - 1;
90 uint32_t try;
92 while (l <= u)
94 m = (l + u) / 2;
95 try = (uint32_t) __ksc5601_hangul_to_ucs[m];
96 if (try > wch)
97 u = m - 1;
98 else if (try < wch)
99 l= m + 1;
100 else
102 if (avail < 2)
103 return 0;
105 s[0] = (m / 94) + 0x30;
106 s[1] = (m % 94) + 0x21;
108 return 2;
112 return UNKNOWN_10646_CHAR;
116 static inline size_t
117 ucs4_to_ksc5601_hanja (uint32_t wch, unsigned char *s, size_t avail)
119 int l = 0;
120 int m;
121 int u = KSC5601_HANJA - 1;
122 uint32_t try;
124 while (l <= u)
126 m = (l + u) / 2;
127 try = (uint32_t) __ksc5601_hanja_from_ucs[m].ucs;
128 if (try > wch)
129 u=m-1;
130 else if (try < wch)
131 l = m + 1;
132 else
134 if (avail < 2)
135 return 0;
137 s[0] = __ksc5601_hanja_from_ucs[m].val[0];
138 s[1] = __ksc5601_hanja_from_ucs[m].val[1];
140 return 2;
144 return UNKNOWN_10646_CHAR;
147 static inline size_t
148 ucs4_to_ksc5601_sym (uint32_t wch, unsigned char *s, size_t avail)
150 int l = 0;
151 int m;
152 int u = KSC5601_SYMBOL - 1;
153 uint32_t try;
155 while (l <= u)
157 m = (l + u) / 2;
158 try = __ksc5601_sym_from_ucs[m].ucs;
159 if (try > wch)
160 u = m - 1;
161 else if (try < wch)
162 l = m + 1;
163 else
165 if (avail < 2)
166 return 0;
168 s[0] = __ksc5601_sym_from_ucs[m].val[0];
169 s[1] = __ksc5601_sym_from_ucs[m].val[1];
171 return 2;
175 return UNKNOWN_10646_CHAR;
179 static inline size_t
180 ucs4_to_ksc5601 (uint32_t wch, unsigned char *s, size_t avail)
182 if (wch >= 0xac00 && wch <= 0xd7a3)
183 return ucs4_to_ksc5601_hangul (wch, s, avail);
184 else if ((wch >= 0x4e00 && wch <= 0x9fff)
185 || (wch >= 0xf900 && wch <= 0xfa0b))
186 return ucs4_to_ksc5601_hanja (wch, s, avail);
187 else
188 return ucs4_to_ksc5601_sym (wch, s, avail);
191 #endif /* ksc5601.h */