Remove pre-2.4 Linux kernel support.
[platform/upstream/glibc.git] / iconvdata / ibm932.c
1 /* Conversion from and to IBM932.
2    Copyright (C) 2000-2002, 2004 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Masahide Washizawa <washi@jp.ibm.com>, 2000.
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, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <dlfcn.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include "ibm932.h"
24
25 #define FROM    0
26 #define TO      1
27
28 /* Definitions used in the body of the `gconv' function.  */
29 #define CHARSET_NAME    "IBM932//"
30 #define FROM_LOOP       from_ibm932
31 #define TO_LOOP         to_ibm932
32
33 /* Definitions of initialization and destructor function.  */
34 #define DEFINE_INIT     1
35 #define DEFINE_FINI     1
36
37 #define MIN_NEEDED_FROM 1
38 #define MAX_NEEDED_FROM 2
39 #define MIN_NEEDED_TO   4
40
41 /* First, define the conversion function from IBM-932 to UCS4.  */
42 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
43 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
44 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
45 #define LOOPFCT                 FROM_LOOP
46 #define BODY \
47   {                                                                           \
48     const struct gap *rp2 = __ibm932db_to_ucs4_idx;                           \
49     uint32_t ch = *inptr;                                                     \
50     uint32_t res;                                                             \
51                                                                               \
52     if (__builtin_expect (ch == 0x80, 0)                                      \
53         || __builtin_expect (ch == 0xa0, 0)                                   \
54         || __builtin_expect (ch == 0xfd, 0)                                   \
55         || __builtin_expect (ch == 0xfe, 0)                                   \
56         || __builtin_expect (ch == 0xff, 0))                                  \
57       {                                                                       \
58         /* This is an illegal character.  */                                  \
59         STANDARD_FROM_LOOP_ERR_HANDLER (1);                                   \
60       }                                                                       \
61                                                                               \
62     /* Use the IBM932 table for single byte.  */                              \
63     res = __ibm932sb_to_ucs4[ch];                                             \
64     if (__builtin_expect (res == 0, 0) && ch != 0)                            \
65       {                                                                       \
66         /* Use the IBM932 table for double byte.  */                          \
67         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
68           {                                                                   \
69             /* The second character is not available.                         \
70                Store the intermediate result.  */                             \
71             result = __GCONV_INCOMPLETE_INPUT;                                \
72             break;                                                            \
73           }                                                                   \
74                                                                               \
75         ch = (ch * 0x100) + inptr[1];                                         \
76         while (ch > rp2->end)                                                 \
77           ++rp2;                                                              \
78                                                                               \
79         if (__builtin_expect (rp2 == NULL, 0)                                 \
80             || __builtin_expect (ch < rp2->start, 0)                          \
81             || (res = __ibm932db_to_ucs4[ch + rp2->idx],                      \
82             __builtin_expect (res, '\1') == 0 && ch !=0))                     \
83           {                                                                   \
84             /* This is an illegal character.  */                              \
85             STANDARD_FROM_LOOP_ERR_HANDLER (2);                               \
86           }                                                                   \
87         else                                                                  \
88           {                                                                   \
89             put32 (outptr, res);                                              \
90             outptr += 4;                                                      \
91             inptr += 2;                                                       \
92           }                                                                   \
93       }                                                                       \
94     else                                                                      \
95       {                                                                       \
96         if (res == 0xa5)                                                      \
97           res = 0x5c;                                                         \
98         else if (res == 0x203e)                                               \
99           res = 0x7e;                                                         \
100         put32 (outptr, res);                                                  \
101         outptr += 4;                                                          \
102         inptr++;                                                              \
103       }                                                                       \
104   }
105 #define LOOP_NEED_FLAGS
106 #define ONEBYTE_BODY \
107   {                                                                           \
108     if (c == 0x80 || c == 0xa0 || c >= 0xfd)                                  \
109       return WEOF;                                                            \
110     uint32_t res = __ibm932sb_to_ucs4[c];                                     \
111     if (res == 0 && c != 0)                                                   \
112       return WEOF;                                                            \
113     if (res == 0xa5)                                                          \
114       res = 0x5c;                                                             \
115     else if (res == 0x203e)                                                   \
116       res = 0x7e;                                                             \
117     return res;                                                               \
118   }
119 #include <iconv/loop.c>
120
121 /* Next, define the other direction.  */
122 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
123 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
124 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
125 #define LOOPFCT                 TO_LOOP
126 #define BODY \
127   {                                                                           \
128     const struct gap *rp = __ucs4_to_ibm932sb_idx;                            \
129     unsigned char sc;                                                         \
130     uint32_t ch = get32 (inptr);                                              \
131     bool found = true;                                                        \
132     uint32_t i;                                                               \
133     uint32_t low;                                                             \
134     uint32_t high;                                                            \
135     uint16_t pccode;                                                          \
136                                                                               \
137     if (__builtin_expect (ch >= 0xffff, 0))                                   \
138       {                                                                       \
139         UNICODE_TAG_HANDLER (ch, 4);                                          \
140         rp = NULL;                                                            \
141       }                                                                       \
142     else                                                                      \
143       while (ch > rp->end)                                                    \
144         ++rp;                                                                 \
145                                                                               \
146     /* Use the UCS4 table for single byte.  */                                \
147     if (__builtin_expect (rp == NULL, 0)                                      \
148         || __builtin_expect (ch < rp->start, 0)                               \
149         || (sc = __ucs4_to_ibm932sb[ch + rp->idx],                            \
150         __builtin_expect (sc, '\1') == '\0' && ch != L'\0'))                  \
151       {                                                                       \
152                                                                               \
153         /* Use the UCS4 table for double byte.  */                            \
154         found = false;                                                        \
155         low = 0;                                                              \
156         high = (sizeof (__ucs4_to_ibm932db) >> 1)                             \
157                 / sizeof (__ucs4_to_ibm932db[0][FROM]);                       \
158         pccode = ch;                                                          \
159         if (__builtin_expect (rp != NULL, 1))                                 \
160           while (low < high)                                                  \
161             {                                                                 \
162               i = (low + high) >> 1;                                          \
163               if (pccode < __ucs4_to_ibm932db[i][FROM])                       \
164                 high = i;                                                     \
165               else if (pccode > __ucs4_to_ibm932db[i][FROM])                  \
166                 low = i + 1;                                                  \
167               else                                                            \
168                 {                                                             \
169                   pccode = __ucs4_to_ibm932db[i][TO];                         \
170                   found = true;                                               \
171                   break;                                                      \
172                 }                                                             \
173             }                                                                 \
174         if (found)                                                            \
175           {                                                                   \
176             if (__builtin_expect (outptr + 2 > outend, 0))                    \
177               {                                                               \
178                 result = __GCONV_FULL_OUTPUT;                                 \
179                 break;                                                        \
180               }                                                               \
181             *outptr++ = pccode >> 8 & 0xff;                                   \
182             *outptr++ = pccode & 0xff;                                        \
183           }                                                                   \
184         else                                                                  \
185           {                                                                   \
186             /* This is an illegal character.  */                              \
187             STANDARD_TO_LOOP_ERR_HANDLER (4);                                 \
188           }                                                                   \
189       }                                                                       \
190     else                                                                      \
191       {                                                                       \
192         if (__builtin_expect (outptr + 1 > outend, 0))                        \
193           {                                                                   \
194             result = __GCONV_FULL_OUTPUT;                                     \
195             break;                                                            \
196           }                                                                   \
197         if (ch == 0x5c)                                                       \
198           *outptr++ = 0x5c;                                                   \
199         else if (ch == 0x7e)                                                  \
200           *outptr++ = 0x7e;                                                   \
201         else                                                                  \
202           *outptr++ = sc;                                                     \
203       }                                                                       \
204                                                                               \
205     /* Now that we wrote the output increment the input pointer.  */          \
206     inptr += 4;                                                               \
207   }
208 #define LOOP_NEED_FLAGS
209 #include <iconv/loop.c>
210
211 /* Now define the toplevel functions.  */
212 #include <iconv/skeleton.c>