1 | /* Access functions for JISX0208 conversion. |
---|
2 | Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
---|
3 | This file is part of the GNU C Library. |
---|
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. |
---|
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, write to the Free |
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
19 | 02111-1307 USA. */ |
---|
20 | |
---|
21 | #ifndef _JIS0208_H |
---|
22 | #define _JIS0208_H 1 |
---|
23 | |
---|
24 | #include <gconv.h> |
---|
25 | #include <stdint.h> |
---|
26 | |
---|
27 | /* Struct for table with indeces in UCS mapping table. */ |
---|
28 | struct jisx0208_ucs_idx |
---|
29 | { |
---|
30 | uint16_t start; |
---|
31 | uint16_t end; |
---|
32 | uint16_t idx; |
---|
33 | }; |
---|
34 | |
---|
35 | /* Conversion table. */ |
---|
36 | extern const uint16_t __jis0208_to_ucs[]; |
---|
37 | |
---|
38 | extern const char __jisx0208_from_ucs4_lat1[256][2]; |
---|
39 | extern const char __jisx0208_from_ucs4_greek[0xc1][2]; |
---|
40 | extern const struct jisx0208_ucs_idx __jisx0208_from_ucs_idx[]; |
---|
41 | extern const char __jisx0208_from_ucs_tab[][2]; |
---|
42 | |
---|
43 | static inline uint32_t |
---|
44 | jisx0208_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset) |
---|
45 | { |
---|
46 | unsigned char ch = *(*s); |
---|
47 | unsigned char ch2; |
---|
48 | int idx; |
---|
49 | |
---|
50 | if (ch < offset || (ch - offset) <= 0x20) |
---|
51 | return __UNKNOWN_10646_CHAR; |
---|
52 | |
---|
53 | if (avail < 2) |
---|
54 | return 0; |
---|
55 | |
---|
56 | ch2 = (*s)[1]; |
---|
57 | if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) |
---|
58 | return __UNKNOWN_10646_CHAR; |
---|
59 | |
---|
60 | idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset); |
---|
61 | if (idx >= 0x1e80) |
---|
62 | return __UNKNOWN_10646_CHAR; |
---|
63 | |
---|
64 | (*s) += 2; |
---|
65 | |
---|
66 | return __jis0208_to_ucs[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | static inline size_t |
---|
71 | ucs4_to_jisx0208 (uint32_t wch, char *s, size_t avail) |
---|
72 | { |
---|
73 | unsigned int ch = (unsigned int) wch; |
---|
74 | const char *cp; |
---|
75 | |
---|
76 | if (avail < 2) |
---|
77 | return 0; |
---|
78 | |
---|
79 | if (ch < 0x100) |
---|
80 | cp = __jisx0208_from_ucs4_lat1[ch]; |
---|
81 | else if (ch >= 0x391 && ch <= 0x451) |
---|
82 | cp = __jisx0208_from_ucs4_greek[ch - 0x391]; |
---|
83 | else |
---|
84 | { |
---|
85 | const struct jisx0208_ucs_idx *rp = __jisx0208_from_ucs_idx; |
---|
86 | |
---|
87 | if (ch >= 0xffff) |
---|
88 | return __UNKNOWN_10646_CHAR; |
---|
89 | while (ch > rp->end) |
---|
90 | ++rp; |
---|
91 | if (ch >= rp->start) |
---|
92 | cp = __jisx0208_from_ucs_tab[rp->idx + ch - rp->start]; |
---|
93 | else |
---|
94 | return __UNKNOWN_10646_CHAR; |
---|
95 | } |
---|
96 | |
---|
97 | if (cp[0] == '\0') |
---|
98 | return __UNKNOWN_10646_CHAR; |
---|
99 | |
---|
100 | s[0] = cp[0]; |
---|
101 | s[1] = cp[1]; |
---|
102 | |
---|
103 | return 2; |
---|
104 | } |
---|
105 | |
---|
106 | #endif /* jis0208.h */ |
---|