Properly handle fencepost with MALLOC_ALIGN_MASK
[glibc.git] / iconvdata / jis0208.h
blob35f7ff82e6374cd3b726b519df11f97915b65657
1 /* Access functions for JISX0208 conversion.
2 Copyright (C) 1997,1998,1999,2000,2003,2005,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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.
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.
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/>. */
21 #ifndef _JIS0208_H
22 #define _JIS0208_H 1
24 #include <gconv.h>
25 #include <stdint.h>
27 /* Struct for table with indeces in UCS mapping table. */
28 struct jisx0208_ucs_idx
30 uint16_t start;
31 uint16_t end;
32 uint16_t idx;
36 /* Conversion table. */
37 extern const uint16_t __jis0208_to_ucs[];
39 #define JIS0208_LAT1_MIN 0xa2
40 #define JIS0208_LAT1_MAX 0xf7
41 extern const char __jisx0208_from_ucs4_lat1[JIS0208_LAT1_MAX + 1
42 - JIS0208_LAT1_MIN][2];
43 extern const char __jisx0208_from_ucs4_greek[0xc1][2];
44 extern const struct jisx0208_ucs_idx __jisx0208_from_ucs_idx[];
45 extern const char __jisx0208_from_ucs_tab[][2];
48 static inline uint32_t
49 __attribute ((always_inline))
50 jisx0208_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
52 unsigned char ch = *(*s);
53 unsigned char ch2;
54 int idx;
56 if (ch < offset || (ch - offset) <= 0x20)
57 return __UNKNOWN_10646_CHAR;
59 if (avail < 2)
60 return 0;
62 ch2 = (*s)[1];
63 if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
64 return __UNKNOWN_10646_CHAR;
66 idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
67 if (idx >= 0x1e80)
68 return __UNKNOWN_10646_CHAR;
70 (*s) += 2;
72 return __jis0208_to_ucs[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
76 static inline size_t
77 __attribute ((always_inline))
78 ucs4_to_jisx0208 (uint32_t wch, unsigned char *s, size_t avail)
80 unsigned int ch = (unsigned int) wch;
81 const char *cp;
83 if (avail < 2)
84 return 0;
86 if (ch >= JIS0208_LAT1_MIN && ch <= JIS0208_LAT1_MAX)
87 cp = __jisx0208_from_ucs4_lat1[ch - JIS0208_LAT1_MIN];
88 else if (ch >= 0x391 && ch <= 0x451)
89 cp = __jisx0208_from_ucs4_greek[ch - 0x391];
90 else
92 const struct jisx0208_ucs_idx *rp = __jisx0208_from_ucs_idx;
94 if (ch >= 0xffff)
95 return __UNKNOWN_10646_CHAR;
96 while (ch > rp->end)
97 ++rp;
98 if (ch >= rp->start)
99 cp = __jisx0208_from_ucs_tab[rp->idx + ch - rp->start];
100 else
101 return __UNKNOWN_10646_CHAR;
104 if (cp[0] == '\0')
105 return __UNKNOWN_10646_CHAR;
107 s[0] = cp[0];
108 s[1] = cp[1];
110 return 2;
113 #endif /* jis0208.h */