* sysdeps/unix/sysv/linux/net/if_arp.h (ARPHRD_RAWHDLC): Added.
[glibc.git] / iconvdata / ibm937.c
blob4fee56a5066ea1078811e11c6bc3580b74b771c1
1 /* Conversion to and from IBM937.
2 Copyright (C) 2000, 2001 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.
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.
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.
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. */
21 #include <dlfcn.h>
22 #include <stdint.h>
23 #include <wchar.h>
24 #include <byteswap.h>
25 #include "ibm937.h"
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. */
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 #define MIN_NEEDED_FROM 1
36 #define MAX_NEEDED_FROM 2
37 #define MIN_NEEDED_TO 4
38 #define MAX_NEEDED_TO 4
39 #define PREPARE_LOOP \
40 int save_curcs; \
41 int *curcsp = &data->__statep->__count;
42 #define EXTRA_LOOP_ARGS , curcsp
44 /* Definitions of initialization and destructor function. */
45 #define DEFINE_INIT 1
46 #define DEFINE_FINI 1
49 /* Since this is a stateful encoding we have to provide code which resets
50 the output state to the initial state. This has to be done during the
51 flushing. */
52 #define EMIT_SHIFT_TO_INIT \
53 if ((data->__statep->__count & ~7) != sb) \
54 { \
55 if (FROM_DIRECTION) \
56 data->__statep->__count &= 7; \
57 else \
58 { \
59 unsigned char *outbuf = data->__outbuf; \
61 /* We are not in the initial state. To switch back we have \
62 to emit `SI'. */ \
63 if (__builtin_expect (outbuf >= data->__outbufend, 0)) \
64 /* We don't have enough room in the output buffer. */ \
65 status = __GCONV_FULL_OUTPUT; \
66 else \
67 { \
68 /* Write out the shift sequence. */ \
69 *outbuf++ = SI; \
70 data->__outbuf = outbuf; \
71 data->__statep->__count &= 7; \
72 } \
73 } \
77 /* Since we might have to reset input pointer we must be able to save
78 and retore the state. */
79 #define SAVE_RESET_STATE(Save) \
80 if (Save) \
81 save_curcs = *curcsp; \
82 else \
83 *curcsp = save_curcs
86 /* Current codeset type. */
87 enum
89 sb = 0,
90 db = 64
93 /* First, define the conversion function from IBM-937 to UCS4. */
94 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
95 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
96 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
97 #define LOOPFCT FROM_LOOP
98 #define BODY \
99 { \
100 uint32_t ch = *inptr; \
102 if (__builtin_expect (ch, 0) == SO) \
104 /* Shift OUT, change to DBCS converter. */ \
105 if (curcs == db) \
107 result = __GCONV_ILLEGAL_INPUT; \
108 break; \
110 curcs = db; \
111 ++inptr; \
112 continue; \
114 else if (__builtin_expect (ch, 0) == SI) \
116 /* Shift IN, change to SBCS converter. */ \
117 if (curcs == sb) \
119 result = __GCONV_ILLEGAL_INPUT; \
120 break; \
122 curcs = sb; \
123 ++inptr; \
124 continue; \
127 if (curcs == sb) \
129 /* Use the UCS4 table for single byte. */ \
130 ch = __ibm937sb_to_ucs4[ch]; \
131 if (__builtin_expect (ch, L'\1') == L'\0' && *inptr != '\0') \
133 /* This is an illegal character. */ \
134 if (! ignore_errors_p ()) \
136 result = __GCONV_ILLEGAL_INPUT; \
137 break; \
139 ++*irreversible; \
141 else \
143 put32 (outptr, ch); \
144 outptr += 4; \
146 ++inptr; \
148 else \
150 /* Use the IBM937 table for double byte. */ \
152 assert (curcs == db); \
154 ch = ibm937db_to_ucs4(inptr[0], inptr[1]); \
155 if (__builtin_expect (ch, L'\1') == L'\0' && *inptr != '\0') \
157 /* This is an illegal character. */ \
158 if (! ignore_errors_p ()) \
160 result = __GCONV_ILLEGAL_INPUT; \
161 break; \
163 ++*irreversible; \
165 else \
167 put32 (outptr, ch); \
168 outptr += 4; \
170 inptr += 2; \
173 #define LOOP_NEED_FLAGS
174 #define EXTRA_LOOP_DECLS , int *curcsp
175 #define INIT_PARAMS int curcs = *curcsp & ~7
176 #define UPDATE_PARAMS *curcsp = curcs
177 #include <iconv/loop.c>
179 /* Next, define the other direction. */
180 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
181 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
182 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
183 #define LOOPFCT TO_LOOP
184 #define BODY \
186 uint32_t ch = get32 (inptr); \
187 const char *cp; \
189 /* Use the UCS4 table for single byte. */ \
190 cp = __ucs4_to_ibm937sb[ch]; \
191 if (__builtin_expect (ch >= (sizeof (__ucs4_to_ibm937sb) \
192 / sizeof (__ucs4_to_ibm937sb[0])), 0) \
193 || (__builtin_expect (cp[0], '\1') == '\0' && ch != 0)) \
195 /* Use the UCS4 table for double byte. */ \
196 cp = __ucs4_to_ibm937db[ch]; \
197 if (__builtin_expect (ch >= (sizeof (__ucs4_to_ibm937db) \
198 / sizeof (__ucs4_to_ibm937db[0])), 0) \
199 || __builtin_expect (cp[0], '\1') == '\0') \
201 /* This is an illegal character. */ \
202 if (! ignore_errors_p ()) \
204 result = __GCONV_ILLEGAL_INPUT; \
205 break; \
207 ++*irreversible; \
209 else \
211 if (curcs == sb) \
213 *outptr++ = SO; \
214 curcs = db; \
216 if (__builtin_expect (outptr + 1 >= outend, 0)) \
218 result = __GCONV_FULL_OUTPUT; \
219 break; \
221 *outptr++ = cp[0]; \
222 *outptr++ = cp[1]; \
225 else \
227 if (curcs == db) \
229 *outptr++ = SI; \
230 curcs = sb; \
231 if (__builtin_expect (outptr == outend, 0)) \
233 result = __GCONV_FULL_OUTPUT; \
234 break; \
237 *outptr++ = cp[0]; \
240 /* Now that we wrote the output increment the input pointer. */ \
241 inptr += 4; \
243 #define LOOP_NEED_FLAGS
244 #define EXTRA_LOOP_DECLS , int *curcsp
245 #define INIT_PARAMS int curcs = *curcsp & ~7
246 #define UPDATE_PARAMS *curcsp = curcs
247 #include <iconv/loop.c>
249 /* Now define the toplevel functions. */
250 #include <iconv/skeleton.c>