]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconvdata/jis0208.h
Fix handling of conversion problem in CP932 module
[thirdparty/glibc.git] / iconvdata / jis0208.h
CommitLineData
04be94a8 1/* Access functions for JISX0208 conversion.
085a4412
UD
2 Copyright (C) 1997,1998,1999,2000,2003,2005,2007
3 Free Software Foundation, Inc.
04be94a8
UD
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6
7 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
04be94a8
UD
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
41bdb6e2 15 Lesser General Public License for more details.
04be94a8 16
41bdb6e2
AJ
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
04be94a8
UD
21
22#ifndef _JIS0208_H
23#define _JIS0208_H 1
24
25#include <gconv.h>
2aea1d79 26#include <stdint.h>
04be94a8 27
04be94a8
UD
28/* Struct for table with indeces in UCS mapping table. */
29struct jisx0208_ucs_idx
30{
31 uint16_t start;
32 uint16_t end;
33 uint16_t idx;
34};
35
36
4bae262d
UD
37/* Conversion table. */
38extern const uint16_t __jis0208_to_ucs[];
39
294bda1b
UD
40#define JIS0208_LAT1_MIN 0xa2
41#define JIS0208_LAT1_MAX 0xf7
42extern const char __jisx0208_from_ucs4_lat1[JIS0208_LAT1_MAX + 1
43 - JIS0208_LAT1_MIN][2];
4bae262d
UD
44extern const char __jisx0208_from_ucs4_greek[0xc1][2];
45extern const struct jisx0208_ucs_idx __jisx0208_from_ucs_idx[];
46extern const char __jisx0208_from_ucs_tab[][2];
47
48
8619129f 49static inline uint32_t
dd9423a6 50__attribute ((always_inline))
8619129f 51jisx0208_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
04be94a8
UD
52{
53 unsigned char ch = *(*s);
54 unsigned char ch2;
55 int idx;
56
044ff622 57 if (ch < offset || (ch - offset) <= 0x20)
d64b6ad0 58 return __UNKNOWN_10646_CHAR;
04be94a8
UD
59
60 if (avail < 2)
61 return 0;
62
63 ch2 = (*s)[1];
2b474353 64 if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
d64b6ad0 65 return __UNKNOWN_10646_CHAR;
04be94a8 66
2b474353 67 idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
04be94a8 68 if (idx >= 0x1e80)
d64b6ad0 69 return __UNKNOWN_10646_CHAR;
04be94a8
UD
70
71 (*s) += 2;
72
d64b6ad0 73 return __jis0208_to_ucs[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
04be94a8
UD
74}
75
76
77static inline size_t
dd9423a6 78__attribute ((always_inline))
085a4412 79ucs4_to_jisx0208 (uint32_t wch, unsigned char *s, size_t avail)
04be94a8
UD
80{
81 unsigned int ch = (unsigned int) wch;
8619129f 82 const char *cp;
04be94a8
UD
83
84 if (avail < 2)
85 return 0;
86
294bda1b
UD
87 if (ch >= JIS0208_LAT1_MIN && ch <= JIS0208_LAT1_MAX)
88 cp = __jisx0208_from_ucs4_lat1[ch - JIS0208_LAT1_MIN];
04be94a8 89 else if (ch >= 0x391 && ch <= 0x451)
5c2a0669 90 cp = __jisx0208_from_ucs4_greek[ch - 0x391];
04be94a8
UD
91 else
92 {
8619129f 93 const struct jisx0208_ucs_idx *rp = __jisx0208_from_ucs_idx;
04be94a8 94
8619129f 95 if (ch >= 0xffff)
d64b6ad0 96 return __UNKNOWN_10646_CHAR;
04be94a8
UD
97 while (ch > rp->end)
98 ++rp;
99 if (ch >= rp->start)
8619129f
UD
100 cp = __jisx0208_from_ucs_tab[rp->idx + ch - rp->start];
101 else
d64b6ad0 102 return __UNKNOWN_10646_CHAR;
04be94a8
UD
103 }
104
8619129f 105 if (cp[0] == '\0')
d64b6ad0 106 return __UNKNOWN_10646_CHAR;
04be94a8 107
2b474353
UD
108 s[0] = cp[0];
109 s[1] = cp[1];
04be94a8
UD
110
111 return 2;
112}
113
114#endif /* jis0208.h */