98e9683253c69e64ee1444098863fd5effcb9651
[kopensolaris-gnu/glibc.git] / iconvdata / ibm937.c
1 /* Conversion to and from IBM937.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Masahide Washizawa <washi@yamato.ibm.co.jp>, 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 Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    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    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <dlfcn.h>
22 #include <stdint.h>
23 #include <wchar.h>
24 #include <byteswap.h>
25 #include "ibm937.h"
26
27 /* The shift sequences for this charset (it does not use ESC).  */
28 #define SI              0x0F  /* Shift In, host code to turn DBCS off.  */
29 #define SO              0x0E  /* Shift Out, host code to turn DBCS on.  */
30
31 /* Definitions used in the body of the `gconv' function.  */
32 #define CHARSET_NAME    "IBM937//"
33 #define FROM_LOOP       from_ibm937
34 #define TO_LOOP         to_ibm937
35
36 /* Definitions of initialization and destructor function.  */
37 #define DEFINE_INIT     1
38 #define DEFINE_FINI     1
39
40 #define MIN_NEEDED_FROM 1
41 #define MIN_NEEDED_TO   4
42
43 /* Current codeset type.  */
44 enum
45 {
46   init = 0,
47   sb,
48   db
49 };
50
51 /* First, define the conversion function from IBM-937 to UCS4.  */
52 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
53 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
54 #define INIT_PARAMS             int curcs = init;
55 #define LOOPFCT                 FROM_LOOP
56 #define BODY \
57   {                                                                           \
58     uint32_t ch = *inptr;                                                     \
59                                                                               \
60     if (__builtin_expect (ch, 0) == SO)                                       \
61       {                                                                       \
62         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
63           {                                                                   \
64             result = __GCONV_INCOMPLETE_INPUT;                                \
65             break;                                                            \
66           }                                                                   \
67                                                                               \
68         /* Shift OUT, change to DBCS converter.  */                           \
69         if (curcs == db)                                                      \
70           {                                                                   \
71             result = __GCONV_ILLEGAL_INPUT;                                   \
72             break;                                                            \
73           }                                                                   \
74         curcs = db;                                                           \
75         ++inptr;                                                              \
76         ch = *inptr;                                                          \
77       }                                                                       \
78     else if (__builtin_expect (ch, 0) == SI)                                  \
79       {                                                                       \
80         if (__builtin_expect (inptr + 1 >= inend, 0))                         \
81           {                                                                   \
82             result = __GCONV_INCOMPLETE_INPUT;                                \
83             break;                                                            \
84           }                                                                   \
85                                                                               \
86         /* Shift IN, change to SBCS converter.  */                            \
87         if (curcs == sb)                                                      \
88           {                                                                   \
89             result = __GCONV_ILLEGAL_INPUT;                                   \
90             break;                                                            \
91           }                                                                   \
92         curcs = sb;                                                           \
93         ++inptr;                                                              \
94         ch = *inptr;                                                          \
95       }                                                                       \
96                                                                               \
97     if (curcs == sb || curcs == init)                                         \
98       {                                                                       \
99         /* Use the UCS4 table for single byte.  */                            \
100         ch = __ibm937sb_to_ucs4[ch];                                          \
101         if (__builtin_expect (ch, L'\1') == L'\0' && *inptr != '\0')          \
102           {                                                                   \
103             /* This is an illegal character.  */                              \
104             if (! ignore_errors_p ())                                         \
105               {                                                               \
106                 result = __GCONV_ILLEGAL_INPUT;                               \
107                 break;                                                        \
108               }                                                               \
109             ++*irreversible;                                                  \
110             ++inptr;                                                          \
111             continue;                                                         \
112           }                                                                   \
113         else                                                                  \
114           {                                                                   \
115             put32 (outptr, ch);                                               \
116             outptr += 4;                                                      \
117             inptr++;                                                          \
118           }                                                                   \
119       }                                                                       \
120     else if (curcs == db)                                                     \
121       {                                                                       \
122         /* Use the IBM937 table for double byte.  */                          \
123         ch = ibm937db_to_ucs4(inptr[0], inptr[1]);                            \
124         if (__builtin_expect (ch, L'\1') == L'\0' && *inptr != '\0')          \
125           {                                                                   \
126             /* This is an illegal character.  */                              \
127             if (! ignore_errors_p ())                                         \
128               {                                                               \
129                 result = __GCONV_ILLEGAL_INPUT;                               \
130                 break;                                                        \
131               }                                                               \
132             ++*irreversible;                                                  \
133             inptr += 2;                                                       \
134             continue;                                                         \
135           }                                                                   \
136         else                                                                  \
137           {                                                                   \
138             put32 (outptr, ch);                                               \
139             outptr += 4;                                                      \
140             inptr += 2;                                                       \
141           }                                                                   \
142       }                                                                       \
143   }
144 #define LOOP_NEED_FLAGS
145 #include <iconv/loop.c>
146
147 /* Next, define the other direction.  */
148 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
149 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
150 #define INIT_PARAMS             int curcs = init;
151 #define LOOPFCT                 TO_LOOP
152 #define BODY \
153   {                                                                           \
154     uint32_t ch = get32 (inptr);                                              \
155     const char *cp;                                                           \
156                                                                               \
157     /* Use the UCS4 table for single byte.  */                                \
158     cp = __ucs4_to_ibm937sb[ch];                                              \
159     if (__builtin_expect (ch >= sizeof (__ucs4_to_ibm937sb)                   \
160                           / sizeof (__ucs4_to_ibm937sb[0]), 0)                \
161         || (__builtin_expect (cp[0], '\1') == '\0' && ch != 0))               \
162       {                                                                       \
163         /* Use the UCS4 table for double byte.  */                            \
164         cp = __ucs4_to_ibm937db[ch];                                          \
165         if (__builtin_expect (cp[0], '\1') == '\0' && ch != 0)                \
166           {                                                                   \
167             /* This is an illegal character.  */                              \
168             if (! ignore_errors_p ())                                         \
169               {                                                               \
170                 result = __GCONV_ILLEGAL_INPUT;                               \
171                 break;                                                        \
172               }                                                               \
173             ++*irreversible;                                                  \
174           }                                                                   \
175         else                                                                  \
176           {                                                                   \
177             if (curcs == init || curcs == sb)                                 \
178               {                                                               \
179                 *outptr++ = SO;                                               \
180                 if (__builtin_expect (outptr == outend, 0))                   \
181                   {                                                           \
182                     result = __GCONV_FULL_OUTPUT;                             \
183                     break;                                                    \
184                   }                                                           \
185                 curcs = db;                                                   \
186               }                                                               \
187             *outptr++ = cp[0];                                                \
188             *outptr++ = cp[1];                                                \
189           }                                                                   \
190       }                                                                       \
191     else                                                                      \
192       {                                                                       \
193         if (curcs == db)                                                      \
194           {                                                                   \
195             *outptr++ = SI;                                                   \
196             if (__builtin_expect (outptr == outend, 0))                       \
197               {                                                               \
198                 result = __GCONV_FULL_OUTPUT;                                 \
199                 break;                                                        \
200               }                                                               \
201           }                                                                   \
202         curcs = sb;                                                           \
203         *outptr++ = cp[0];                                                    \
204       }                                                                       \
205                                                                               \
206     /* Now that we wrote the output increment the input pointer.  */          \
207     inptr += 4;                                                               \
208   }
209 #define LOOP_NEED_FLAGS
210 #include <iconv/loop.c>
211
212 /* Now define the toplevel functions.  */
213 #include <iconv/skeleton.c>