]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconvdata/jis0208.h
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / iconvdata / jis0208.h
CommitLineData
04be94a8 1/* Access functions for JISX0208 conversion.
d4697bc9 2 Copyright (C) 1997-2014 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 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
04be94a8
UD
19
20#ifndef _JIS0208_H
21#define _JIS0208_H 1
22
23#include <gconv.h>
2aea1d79 24#include <stdint.h>
04be94a8 25
04be94a8
UD
26/* Struct for table with indeces in UCS mapping table. */
27struct jisx0208_ucs_idx
28{
29 uint16_t start;
30 uint16_t end;
31 uint16_t idx;
32};
33
34
4bae262d
UD
35/* Conversion table. */
36extern const uint16_t __jis0208_to_ucs[];
37
294bda1b
UD
38#define JIS0208_LAT1_MIN 0xa2
39#define JIS0208_LAT1_MAX 0xf7
40extern const char __jisx0208_from_ucs4_lat1[JIS0208_LAT1_MAX + 1
41 - JIS0208_LAT1_MIN][2];
4bae262d
UD
42extern const char __jisx0208_from_ucs4_greek[0xc1][2];
43extern const struct jisx0208_ucs_idx __jisx0208_from_ucs_idx[];
44extern const char __jisx0208_from_ucs_tab[][2];
45
46
8619129f 47static inline uint32_t
dd9423a6 48__attribute ((always_inline))
8619129f 49jisx0208_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
04be94a8
UD
50{
51 unsigned char ch = *(*s);
52 unsigned char ch2;
53 int idx;
54
044ff622 55 if (ch < offset || (ch - offset) <= 0x20)
d64b6ad0 56 return __UNKNOWN_10646_CHAR;
04be94a8
UD
57
58 if (avail < 2)
59 return 0;
60
61 ch2 = (*s)[1];
2b474353 62 if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
d64b6ad0 63 return __UNKNOWN_10646_CHAR;
04be94a8 64
2b474353 65 idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
04be94a8 66 if (idx >= 0x1e80)
d64b6ad0 67 return __UNKNOWN_10646_CHAR;
04be94a8
UD
68
69 (*s) += 2;
70
d64b6ad0 71 return __jis0208_to_ucs[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR);
04be94a8
UD
72}
73
74
75static inline size_t
dd9423a6 76__attribute ((always_inline))
085a4412 77ucs4_to_jisx0208 (uint32_t wch, unsigned char *s, size_t avail)
04be94a8
UD
78{
79 unsigned int ch = (unsigned int) wch;
8619129f 80 const char *cp;
04be94a8
UD
81
82 if (avail < 2)
83 return 0;
84
294bda1b
UD
85 if (ch >= JIS0208_LAT1_MIN && ch <= JIS0208_LAT1_MAX)
86 cp = __jisx0208_from_ucs4_lat1[ch - JIS0208_LAT1_MIN];
04be94a8 87 else if (ch >= 0x391 && ch <= 0x451)
5c2a0669 88 cp = __jisx0208_from_ucs4_greek[ch - 0x391];
04be94a8
UD
89 else
90 {
8619129f 91 const struct jisx0208_ucs_idx *rp = __jisx0208_from_ucs_idx;
04be94a8 92
8619129f 93 if (ch >= 0xffff)
d64b6ad0 94 return __UNKNOWN_10646_CHAR;
04be94a8
UD
95 while (ch > rp->end)
96 ++rp;
97 if (ch >= rp->start)
8619129f
UD
98 cp = __jisx0208_from_ucs_tab[rp->idx + ch - rp->start];
99 else
d64b6ad0 100 return __UNKNOWN_10646_CHAR;
04be94a8
UD
101 }
102
8619129f 103 if (cp[0] == '\0')
d64b6ad0 104 return __UNKNOWN_10646_CHAR;
04be94a8 105
2b474353
UD
106 s[0] = cp[0];
107 s[1] = cp[1];
04be94a8
UD
108
109 return 2;
110}
111
112#endif /* jis0208.h */