1 /* Tables for conversion to and from ISO-IR-165.
2    converting from UCS using gaps.
3    Copyright (C) 2000-2022 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
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    <https://www.gnu.org/licenses/>.  */
19 
20 #ifndef _ISO_IR_165_H
21 #define _ISO_IR_165_H	1
22 
23 #include <gconv.h>
24 #include <stdint.h>
25 
26 struct gap
27 {
28   uint16_t start;
29   uint16_t end;
30   int32_t idx;
31 };
32 
33 /* Table for ISO-IR-165 (CCITT Chinese) to UCS4 conversion.  */
34 #define ISOIR165_FROMSIZE	0x2284
35 extern const uint16_t __isoir165_to_tab[ISOIR165_FROMSIZE];
36 
37 
38 /* XXX If we at some point need an offset value to decode the byte
39    sequences another parameter can be added.  */
uint32_t(always_inline)40 static inline uint32_t
41 __attribute ((always_inline))
42 isoir165_to_ucs4 (const unsigned char **s, size_t avail)
43 {
44   unsigned char ch = *(*s);
45   unsigned char ch2;
46   uint32_t res;
47 
48   if (ch <= 0x20 || ch >= 0x7f)
49     return __UNKNOWN_10646_CHAR;
50 
51   if (avail < 2)
52     return 0;
53 
54   ch2 = (*s)[1];
55   if (ch2 <= 0x20 || ch2 >= 0x7f)
56     return __UNKNOWN_10646_CHAR;
57 
58   res = __isoir165_to_tab[(ch - 0x21) * 94 + (ch2 - 0x21)];
59   if (res == 0)
60     return __UNKNOWN_10646_CHAR;
61 
62   *s += 2;
63   return res;
64 }
65 
66 
67 /* Tables for ISO-IR-165 (CCITT Chinese) from UCS4 conversion.  */
68 extern const struct gap __isoir165_from_idx[];
69 extern const char __isoir165_from_tab[];
70 
size_t(always_inline)71 static inline size_t
72 __attribute ((always_inline))
73 ucs4_to_isoir165 (uint32_t wch, unsigned char *s, size_t avail)
74 {
75   unsigned int ch = (unsigned int) wch;
76   const char *cp;
77   const struct gap *rp = __isoir165_from_idx;
78 
79   if (ch > 0xffe5)
80     /* This is an illegal character.  */
81     return __UNKNOWN_10646_CHAR;
82 
83   while (ch > rp->end)
84     ++rp;
85   if (ch < rp->start)
86     /* This is an illegal character.  */
87     return __UNKNOWN_10646_CHAR;
88 
89   /* The two bytes following the index given in this record give the
90      encoding in ISO-IR-165.  Unless the bytes are zero.  */
91   cp = &__isoir165_from_tab[(ch + rp->idx) * 2];
92   if (*cp == '\0')
93     /* This is an illegal character.  */
94     return __UNKNOWN_10646_CHAR;
95 
96   if (avail < 2)
97     return 0;
98 
99   s[0] = cp[0];
100   s[1] = cp[1];
101 
102   return 2;
103 }
104 
105 #endif	/* iso-ir-165.h */
106