]>
Commit | Line | Data |
---|---|---|
04be94a8 | 1 | /* Access functions for JISX0208 conversion. |
dd9423a6 | 2 | Copyright (C) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc. |
04be94a8 UD |
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 | |
41bdb6e2 AJ |
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. | |
04be94a8 UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
04be94a8 | 15 | |
41bdb6e2 AJ |
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. */ | |
04be94a8 UD |
20 | |
21 | #ifndef _JIS0208_H | |
22 | #define _JIS0208_H 1 | |
23 | ||
24 | #include <gconv.h> | |
2aea1d79 | 25 | #include <stdint.h> |
04be94a8 UD |
26 | |
27 | /* Conversion table. */ | |
8619129f | 28 | extern const uint16_t __jis0208_to_ucs[]; |
04be94a8 | 29 | |
8619129f UD |
30 | extern const char __jisx0208_from_ucs4_lat1[256][2]; |
31 | extern const char __jisx0208_from_ucs4_greek[0xc1][2]; | |
32 | extern const struct jisx0208_ucs_idx __jisx0208_from_ucs_idx[]; | |
33 | extern const char __jisx0208_from_ucs_tab[][2]; | |
04be94a8 UD |
34 | |
35 | ||
36 | /* Struct for table with indeces in UCS mapping table. */ | |
37 | struct jisx0208_ucs_idx | |
38 | { | |
39 | uint16_t start; | |
40 | uint16_t end; | |
41 | uint16_t idx; | |
42 | }; | |
43 | ||
44 | ||
8619129f | 45 | static inline uint32_t |
dd9423a6 | 46 | __attribute ((always_inline)) |
8619129f | 47 | jisx0208_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset) |
04be94a8 UD |
48 | { |
49 | unsigned char ch = *(*s); | |
50 | unsigned char ch2; | |
51 | int idx; | |
52 | ||
044ff622 | 53 | if (ch < offset || (ch - offset) <= 0x20) |
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 | |
2b474353 | 63 | idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset); |
04be94a8 | 64 | if (idx >= 0x1e80) |
d64b6ad0 | 65 | return __UNKNOWN_10646_CHAR; |
04be94a8 UD |
66 | |
67 | (*s) += 2; | |
68 | ||
d64b6ad0 | 69 | return __jis0208_to_ucs[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR); |
04be94a8 UD |
70 | } |
71 | ||
72 | ||
73 | static inline size_t | |
dd9423a6 | 74 | __attribute ((always_inline)) |
8619129f | 75 | ucs4_to_jisx0208 (uint32_t wch, char *s, size_t avail) |
04be94a8 UD |
76 | { |
77 | unsigned int ch = (unsigned int) wch; | |
8619129f | 78 | const char *cp; |
04be94a8 UD |
79 | |
80 | if (avail < 2) | |
81 | return 0; | |
82 | ||
83 | if (ch < 0x100) | |
8619129f | 84 | cp = __jisx0208_from_ucs4_lat1[ch]; |
04be94a8 | 85 | else if (ch >= 0x391 && ch <= 0x451) |
5c2a0669 | 86 | cp = __jisx0208_from_ucs4_greek[ch - 0x391]; |
04be94a8 UD |
87 | else |
88 | { | |
8619129f | 89 | const struct jisx0208_ucs_idx *rp = __jisx0208_from_ucs_idx; |
04be94a8 | 90 | |
8619129f | 91 | if (ch >= 0xffff) |
d64b6ad0 | 92 | return __UNKNOWN_10646_CHAR; |
04be94a8 UD |
93 | while (ch > rp->end) |
94 | ++rp; | |
95 | if (ch >= rp->start) | |
8619129f UD |
96 | cp = __jisx0208_from_ucs_tab[rp->idx + ch - rp->start]; |
97 | else | |
d64b6ad0 | 98 | return __UNKNOWN_10646_CHAR; |
04be94a8 UD |
99 | } |
100 | ||
8619129f | 101 | if (cp[0] == '\0') |
d64b6ad0 | 102 | return __UNKNOWN_10646_CHAR; |
04be94a8 | 103 | |
2b474353 UD |
104 | s[0] = cp[0]; |
105 | s[1] = cp[1]; | |
04be94a8 UD |
106 | |
107 | return 2; | |
108 | } | |
109 | ||
110 | #endif /* jis0208.h */ |