1/* Access functions for CNS 11643, plane 2 handling.
2 Copyright (C) 1998-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <stdint.h>
21#include <gconv.h>
22
23/* Table for CNS 11643, plane 2 to UCS4 conversion. */
24extern const uint16_t __cns11643l2_to_ucs4_tab[];
25
26
27static inline uint32_t
28__attribute ((always_inline))
29cns11643l2_to_ucs4 (const unsigned char **s, size_t avail,
30 unsigned char offset)
31{
32 unsigned char ch = *(*s);
33 unsigned char ch2;
34 int idx;
35
36 if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x7d)
37 return __UNKNOWN_10646_CHAR;
38
39 if (avail < 2)
40 return 0;
41
42 ch2 = (*s)[1];
43 if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
44 return __UNKNOWN_10646_CHAR;
45
46 idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
47 if (idx > 0x1de1)
48 return __UNKNOWN_10646_CHAR;
49
50 (*s) += 2;
51
52 return __cns11643l2_to_ucs4_tab[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
53}
54
55
56/* The table which contains the CNS 11643 level 2 mappings. */
57extern const char __cns11643_from_ucs4p0_tab[][3];
58
59
60static inline size_t
61__attribute ((always_inline))
62ucs4_to_cns11643l2 (uint32_t wch, unsigned char *s, size_t avail)
63{
64 unsigned int ch = (unsigned int) wch;
65 const char *cp = NULL;
66
67 if (ch >= 0x4e07 && ch <= 0x9fa4)
68 {
69 cp = __cns11643_from_ucs4p0_tab[ch - 0x3400];
70 if (cp[0] == '\2')
71 ++cp;
72 else
73 cp = NULL;
74 }
75
76 if (cp == NULL)
77 return __UNKNOWN_10646_CHAR;
78
79 if (avail < 2)
80 return 0;
81
82 s[0] = cp[0];
83 s[1] = cp[1];
84
85 return 2;
86}
87