|
Packit |
6c4009 |
/* Access functions for KS C 5601-1992 based encoding conversion.
|
|
Packit |
6c4009 |
Copyright (C) 1998-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef _KSC5601_H
|
|
Packit |
6c4009 |
#define _KSC5601_H 1
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define KSC5601_HANGUL 2350
|
|
Packit |
6c4009 |
#define KSC5601_HANJA 4888
|
|
Packit |
6c4009 |
#define KSC5601_SYMBOL 989
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <gconv.h>
|
|
Packit |
6c4009 |
#include <stdint.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Structure to map from UCS to KSC. This structure should be packed
|
|
Packit |
6c4009 |
on all platforms. */
|
|
Packit |
6c4009 |
struct map
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
uint16_t ucs;
|
|
Packit |
6c4009 |
char val[2];
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Conversion table. */
|
|
Packit |
6c4009 |
extern const uint16_t __ksc5601_hangul_to_ucs[KSC5601_HANGUL];
|
|
Packit |
6c4009 |
extern const uint16_t __ksc5601_sym_to_ucs[];
|
|
Packit |
6c4009 |
extern const struct map __ksc5601_sym_from_ucs[KSC5601_SYMBOL];
|
|
Packit |
6c4009 |
extern const uint16_t __ksc5601_hanja_to_ucs[KSC5601_HANJA];
|
|
Packit |
6c4009 |
extern const struct map __ksc5601_hanja_from_ucs[KSC5601_HANJA];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline uint32_t
|
|
Packit |
6c4009 |
__attribute ((always_inline))
|
|
Packit |
6c4009 |
ksc5601_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
unsigned char ch = **s;
|
|
Packit |
6c4009 |
unsigned char ch2;
|
|
Packit |
6c4009 |
int idx;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* row 94(0x7e) and row 41(0x49) are user-defined area in KS C 5601 */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) >= 0x7e
|
|
Packit |
6c4009 |
|| (ch - offset) == 0x49)
|
|
Packit |
6c4009 |
return __UNKNOWN_10646_CHAR;
|
|
Packit |
6c4009 |
|
|
Packit Service |
3e830d |
if (avail < 2)
|
|
Packit Service |
3e830d |
return 0;
|
|
Packit Service |
3e830d |
|
|
Packit |
6c4009 |
ch2 = (*s)[1];
|
|
Packit |
6c4009 |
if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
|
|
Packit |
6c4009 |
return __UNKNOWN_10646_CHAR;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
idx = (ch - offset - 0x21) * 94 + (ch2 - offset - 0x21);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* 1410 = 15 * 94 , 3760 = 40 * 94
|
|
Packit |
6c4009 |
Hangul in KS C 5601 : row 16 - row 40 */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
*s += 2;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (idx >= 1410 && idx < 1410 + KSC5601_HANGUL)
|
|
Packit |
6c4009 |
return (__ksc5601_hangul_to_ucs[idx - 1410]
|
|
Packit |
6c4009 |
?: (*s -= 2, __UNKNOWN_10646_CHAR));
|
|
Packit |
6c4009 |
else if (idx >= 3854)
|
|
Packit |
6c4009 |
/* Hanja : row 42 - row 93 : 3854 = 94 * (42-1) */
|
|
Packit |
6c4009 |
return (__ksc5601_hanja_to_ucs[idx - 3854]
|
|
Packit |
6c4009 |
?: (*s -= 2, __UNKNOWN_10646_CHAR));
|
|
Packit |
6c4009 |
else if (idx <= 1114)
|
|
Packit |
6c4009 |
return __ksc5601_sym_to_ucs[idx] ?: (*s -= 2, __UNKNOWN_10646_CHAR);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
*s -= 2;
|
|
Packit |
6c4009 |
return __UNKNOWN_10646_CHAR;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline size_t
|
|
Packit |
6c4009 |
__attribute ((always_inline))
|
|
Packit |
6c4009 |
ucs4_to_ksc5601_hangul (uint32_t wch, unsigned char *s, size_t avail)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int l = 0;
|
|
Packit |
6c4009 |
int u = KSC5601_HANGUL - 1;
|
|
Packit |
6c4009 |
uint32_t try;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (l <= u)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int m = (l + u) / 2;
|
|
Packit |
6c4009 |
try = (uint32_t) __ksc5601_hangul_to_ucs[m];
|
|
Packit |
6c4009 |
if (try > wch)
|
|
Packit |
6c4009 |
u = m - 1;
|
|
Packit |
6c4009 |
else if (try < wch)
|
|
Packit |
6c4009 |
l= m + 1;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (avail < 2)
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
s[0] = (m / 94) + 0x30;
|
|
Packit |
6c4009 |
s[1] = (m % 94) + 0x21;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 2;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return __UNKNOWN_10646_CHAR;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline size_t
|
|
Packit |
6c4009 |
__attribute ((always_inline))
|
|
Packit |
6c4009 |
ucs4_to_ksc5601_hanja (uint32_t wch, unsigned char *s, size_t avail)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int l = 0;
|
|
Packit |
6c4009 |
int u = KSC5601_HANJA - 1;
|
|
Packit |
6c4009 |
uint32_t try;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (l <= u)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int m = (l + u) / 2;
|
|
Packit |
6c4009 |
try = (uint32_t) __ksc5601_hanja_from_ucs[m].ucs;
|
|
Packit |
6c4009 |
if (try > wch)
|
|
Packit |
6c4009 |
u=m-1;
|
|
Packit |
6c4009 |
else if (try < wch)
|
|
Packit |
6c4009 |
l = m + 1;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (avail < 2)
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
s[0] = __ksc5601_hanja_from_ucs[m].val[0];
|
|
Packit |
6c4009 |
s[1] = __ksc5601_hanja_from_ucs[m].val[1];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 2;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return __UNKNOWN_10646_CHAR;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline size_t
|
|
Packit |
6c4009 |
__attribute ((always_inline))
|
|
Packit |
6c4009 |
ucs4_to_ksc5601_sym (uint32_t wch, unsigned char *s, size_t avail)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int l = 0;
|
|
Packit |
6c4009 |
int u = KSC5601_SYMBOL - 1;
|
|
Packit |
6c4009 |
uint32_t try;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (l <= u)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int m = (l + u) / 2;
|
|
Packit |
6c4009 |
try = __ksc5601_sym_from_ucs[m].ucs;
|
|
Packit |
6c4009 |
if (try > wch)
|
|
Packit |
6c4009 |
u = m - 1;
|
|
Packit |
6c4009 |
else if (try < wch)
|
|
Packit |
6c4009 |
l = m + 1;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (avail < 2)
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
s[0] = __ksc5601_sym_from_ucs[m].val[0];
|
|
Packit |
6c4009 |
s[1] = __ksc5601_sym_from_ucs[m].val[1];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 2;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return __UNKNOWN_10646_CHAR;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline size_t
|
|
Packit |
6c4009 |
__attribute ((always_inline))
|
|
Packit |
6c4009 |
ucs4_to_ksc5601 (uint32_t wch, unsigned char *s, size_t avail)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (wch >= 0xac00 && wch <= 0xd7a3)
|
|
Packit |
6c4009 |
return ucs4_to_ksc5601_hangul (wch, s, avail);
|
|
Packit |
6c4009 |
else if ((wch >= 0x4e00 && wch <= 0x9fff)
|
|
Packit |
6c4009 |
|| (wch >= 0xf900 && wch <= 0xfa0b))
|
|
Packit |
6c4009 |
return ucs4_to_ksc5601_hanja (wch, s, avail);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
return ucs4_to_ksc5601_sym (wch, s, avail);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#endif /* ksc5601.h */
|