1 /* Access functions for CNS 11643, plane 2 handling.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
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
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.
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
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <stdint.h>
20 #include <gconv.h>
21
22 /* Table for CNS 11643, plane 2 to UCS4 conversion. */
23 extern const uint16_t __cns11643l2_to_ucs4_tab[];
24
25
uint32_t(always_inline)26 static inline uint32_t
27 __attribute ((always_inline))
28 cns11643l2_to_ucs4 (const unsigned char **s, size_t avail,
29 unsigned char offset)
30 {
31 unsigned char ch = *(*s);
32 unsigned char ch2;
33 int idx;
34
35 if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x7d)
36 return __UNKNOWN_10646_CHAR;
37
38 if (avail < 2)
39 return 0;
40
41 ch2 = (*s)[1];
42 if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
43 return __UNKNOWN_10646_CHAR;
44
45 idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
46 if (idx > 0x1de1)
47 return __UNKNOWN_10646_CHAR;
48
49 (*s) += 2;
50
51 return __cns11643l2_to_ucs4_tab[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
52 }
53
54
55 /* The table which contains the CNS 11643 level 2 mappings. */
56 extern const char __cns11643_from_ucs4p0_tab[][3];
57
58
size_t(always_inline)59 static inline size_t
60 __attribute ((always_inline))
61 ucs4_to_cns11643l2 (uint32_t wch, unsigned char *s, size_t avail)
62 {
63 unsigned int ch = (unsigned int) wch;
64 const char *cp = NULL;
65
66 if (ch >= 0x4e07 && ch <= 0x9fa4)
67 {
68 cp = __cns11643_from_ucs4p0_tab[ch - 0x3400];
69 if (cp[0] == '\2')
70 ++cp;
71 else
72 cp = NULL;
73 }
74
75 if (cp == NULL)
76 return __UNKNOWN_10646_CHAR;
77
78 if (avail < 2)
79 return 0;
80
81 s[0] = cp[0];
82 s[1] = cp[1];
83
84 return 2;
85 }
86