lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Tables for conversion to and from ISO-IR-165. |
| 2 | converting from UCS using gaps. |
| 3 | Copyright (C) 2000-2015 Free Software Foundation, Inc. |
| 4 | This file is part of the GNU C Library. |
| 5 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. |
| 6 | |
| 7 | The GNU C Library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | The GNU C Library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with the GNU C Library; if not, see |
| 19 | <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #ifndef _ISO_IR_165_H |
| 22 | #define _ISO_IR_165_H 1 |
| 23 | |
| 24 | #include <gconv.h> |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | struct gap |
| 28 | { |
| 29 | uint16_t start; |
| 30 | uint16_t end; |
| 31 | int32_t idx; |
| 32 | }; |
| 33 | |
| 34 | /* Table for ISO-IR-165 (CCITT Chinese) to UCS4 conversion. */ |
| 35 | #define ISOIR165_FROMSIZE 0x2284 |
| 36 | extern const uint16_t __isoir165_to_tab[ISOIR165_FROMSIZE]; |
| 37 | |
| 38 | |
| 39 | /* XXX If we at some point need an offset value to decode the byte |
| 40 | sequences another parameter can be added. */ |
| 41 | static inline uint32_t |
| 42 | __attribute ((always_inline)) |
| 43 | isoir165_to_ucs4 (const unsigned char **s, size_t avail) |
| 44 | { |
| 45 | unsigned char ch = *(*s); |
| 46 | unsigned char ch2; |
| 47 | uint32_t res; |
| 48 | |
| 49 | if (ch <= 0x20 || ch >= 0x7f) |
| 50 | return __UNKNOWN_10646_CHAR; |
| 51 | |
| 52 | if (avail < 2) |
| 53 | return 0; |
| 54 | |
| 55 | ch2 = (*s)[1]; |
| 56 | if (ch2 <= 0x20 || ch2 >= 0x7f) |
| 57 | return __UNKNOWN_10646_CHAR; |
| 58 | |
| 59 | res = __isoir165_to_tab[(ch - 0x21) * 94 + (ch2 - 0x21)]; |
| 60 | if (res == 0) |
| 61 | return __UNKNOWN_10646_CHAR; |
| 62 | |
| 63 | *s += 2; |
| 64 | return res; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /* Tables for ISO-IR-165 (CCITT Chinese) from UCS4 conversion. */ |
| 69 | extern const struct gap __isoir165_from_idx[]; |
| 70 | extern const char __isoir165_from_tab[]; |
| 71 | |
| 72 | static inline size_t |
| 73 | __attribute ((always_inline)) |
| 74 | ucs4_to_isoir165 (uint32_t wch, unsigned char *s, size_t avail) |
| 75 | { |
| 76 | unsigned int ch = (unsigned int) wch; |
| 77 | const char *cp; |
| 78 | const struct gap *rp = __isoir165_from_idx; |
| 79 | |
| 80 | if (ch > 0xffe5) |
| 81 | /* This is an illegal character. */ |
| 82 | return __UNKNOWN_10646_CHAR; |
| 83 | |
| 84 | while (ch > rp->end) |
| 85 | ++rp; |
| 86 | if (ch < rp->start) |
| 87 | /* This is an illegal character. */ |
| 88 | return __UNKNOWN_10646_CHAR; |
| 89 | |
| 90 | /* The two bytes following the index given in this record give the |
| 91 | encoding in ISO-IR-165. Unless the bytes are zero. */ |
| 92 | cp = &__isoir165_from_tab[(ch + rp->idx) * 2]; |
| 93 | if (*cp == '\0') |
| 94 | /* This is an illegal character. */ |
| 95 | return __UNKNOWN_10646_CHAR; |
| 96 | |
| 97 | if (avail < 2) |
| 98 | return 0; |
| 99 | |
| 100 | s[0] = cp[0]; |
| 101 | s[1] = cp[1]; |
| 102 | |
| 103 | return 2; |
| 104 | } |
| 105 | |
| 106 | #endif /* iso-ir-165.h */ |