]>
Commit | Line | Data |
---|---|---|
04be94a8 | 1 | /* Access functions for JISX0212 conversion. |
2b778ceb | 2 | Copyright (C) 1997-2021 Free Software Foundation, Inc. |
04be94a8 | 3 | This file is part of the GNU C Library. |
04be94a8 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
04be94a8 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
04be94a8 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
04be94a8 UD |
18 | |
19 | #ifndef _JIS0212_H | |
20 | #define _JIS0212_H 1 | |
21 | ||
085a4412 | 22 | #include <assert.h> |
04be94a8 | 23 | #include <gconv.h> |
2aea1d79 | 24 | #include <stdint.h> |
04be94a8 UD |
25 | |
26 | ||
14ef9c18 | 27 | /* Struct for table with indices in mapping table. */ |
04be94a8 UD |
28 | struct jisx0212_idx |
29 | { | |
30 | uint16_t start; | |
31 | uint16_t end; | |
32 | uint16_t idx; | |
33 | }; | |
34 | ||
35 | /* Conversion table. */ | |
8619129f UD |
36 | extern const struct jisx0212_idx __jisx0212_to_ucs_idx[]; |
37 | extern const uint16_t __jisx0212_to_ucs[]; | |
04be94a8 | 38 | |
8619129f UD |
39 | extern const struct jisx0212_idx __jisx0212_from_ucs_idx[]; |
40 | extern const char __jisx0212_from_ucs[][2]; | |
04be94a8 UD |
41 | |
42 | ||
9b26f5c4 | 43 | static inline uint32_t |
dd9423a6 | 44 | __attribute ((always_inline)) |
8619129f | 45 | jisx0212_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset) |
04be94a8 | 46 | { |
8619129f | 47 | const struct jisx0212_idx *rp = __jisx0212_to_ucs_idx; |
04be94a8 UD |
48 | unsigned char ch = *(*s); |
49 | unsigned char ch2; | |
8619129f | 50 | uint32_t wch = 0; |
04be94a8 UD |
51 | int idx; |
52 | ||
9b26f5c4 | 53 | if (ch < offset || (ch - offset) < 0x22 || (ch - offset) > 0x6d) |
d64b6ad0 | 54 | return __UNKNOWN_10646_CHAR; |
04be94a8 UD |
55 | |
56 | if (avail < 2) | |
57 | return 0; | |
58 | ||
59 | ch2 = (*s)[1]; | |
2b474353 | 60 | if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) |
d64b6ad0 | 61 | return __UNKNOWN_10646_CHAR; |
04be94a8 | 62 | |
9b26f5c4 | 63 | idx = (ch - offset - 0x21) * 94 + (ch2 - offset - 0x21); |
04be94a8 | 64 | |
8619129f | 65 | while (idx > rp->end) |
04be94a8 | 66 | ++rp; |
8619129f UD |
67 | if (idx >= rp->start) |
68 | wch = __jisx0212_to_ucs[rp->idx + idx - rp->start]; | |
04be94a8 UD |
69 | |
70 | if (wch != L'\0') | |
71 | (*s) += 2; | |
72 | else | |
d64b6ad0 | 73 | wch = __UNKNOWN_10646_CHAR; |
04be94a8 UD |
74 | |
75 | return wch; | |
76 | } | |
77 | ||
78 | ||
79 | static inline size_t | |
dd9423a6 | 80 | __attribute ((always_inline)) |
085a4412 | 81 | ucs4_to_jisx0212 (uint32_t wch, unsigned char *s, size_t avail) |
04be94a8 | 82 | { |
8619129f | 83 | const struct jisx0212_idx *rp = __jisx0212_from_ucs_idx; |
04be94a8 | 84 | unsigned int ch = (unsigned int) wch; |
8619129f | 85 | const char *cp; |
04be94a8 | 86 | |
8619129f | 87 | if (ch >= 0xffff) |
d64b6ad0 | 88 | return __UNKNOWN_10646_CHAR; |
04be94a8 UD |
89 | while (ch > rp->end) |
90 | ++rp; | |
91 | if (ch >= rp->start) | |
8619129f UD |
92 | cp = __jisx0212_from_ucs[rp->idx + ch - rp->start]; |
93 | else | |
d64b6ad0 | 94 | return __UNKNOWN_10646_CHAR; |
04be94a8 | 95 | |
8619129f | 96 | if (cp[0] == '\0') |
d64b6ad0 | 97 | return __UNKNOWN_10646_CHAR; |
04be94a8 | 98 | |
2b474353 | 99 | s[0] = cp[0]; |
085a4412 UD |
100 | assert (cp[1] != '\0'); |
101 | if (avail < 2) | |
102 | return 0; | |
04be94a8 | 103 | |
085a4412 | 104 | s[1] = cp[1]; |
04be94a8 UD |
105 | return 2; |
106 | } | |
107 | ||
108 | #endif /* jis0212.h */ |