]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconvdata/ibm943.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / iconvdata / ibm943.c
CommitLineData
3b7caeac 1/* Conversion from and to IBM943.
04277e02 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3b7caeac
UD
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
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.
3b7caeac
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.
3b7caeac 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/>. */
3b7caeac 19
e438a468
UD
20#include <dlfcn.h>
21#include <stdint.h>
f9ad060c 22#include <stdbool.h>
3b7caeac
UD
23#include "ibm943.h"
24
3b7caeac 25#define FROM 0
db2d05f9 26#define TO 1
3b7caeac
UD
27
28/* Definitions used in the body of the `gconv' function. */
29#define CHARSET_NAME "IBM943//"
30#define FROM_LOOP from_ibm943
31#define TO_LOOP to_ibm943
13e402e7 32#define ONE_DIRECTION 0
3b7caeac
UD
33
34/* Definitions of initialization and destructor function. */
35#define DEFINE_INIT 1
36#define DEFINE_FINI 1
37
38#define MIN_NEEDED_FROM 1
39#define MAX_NEEDED_FROM 2
40#define MIN_NEEDED_TO 4
41
42/* First, define the conversion function from IBM-943 to UCS4. */
43#define MIN_NEEDED_INPUT MIN_NEEDED_FROM
44#define MAX_NEEDED_INPUT MAX_NEEDED_FROM
45#define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
46#define LOOPFCT FROM_LOOP
47#define BODY \
48 { \
3b7caeac
UD
49 const struct gap *rp2 = __ibm943db_to_ucs4_idx; \
50 uint32_t ch = *inptr; \
51 uint32_t res; \
52 \
f9ad060c
UD
53 if (__builtin_expect (ch == 0x80, 0) \
54 || __builtin_expect (ch == 0xa0, 0) \
55 || __builtin_expect (ch == 0xfd, 0) \
56 || __builtin_expect (ch == 0xfe, 0) \
57 || __builtin_expect (ch == 0xff, 0)) \
3b7caeac
UD
58 { \
59 /* This is an illegal character. */ \
e438a468 60 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
3b7caeac
UD
61 } \
62 \
63 /* Use the IBM943 table for single byte. */ \
f9ad060c
UD
64 if (__builtin_expect (ch > 0xdf, 0) \
65 || (res = __ibm943sb_to_ucs4[ch], \
66 __builtin_expect (res == 0, 0) && ch != 0)) \
3b7caeac 67 { \
3b7caeac 68 /* Use the IBM943 table for double byte. */ \
a1ffb40e 69 if (__glibc_unlikely (inptr + 1 >= inend)) \
3b7caeac
UD
70 { \
71 /* The second character is not available. \
72 Store the intermediate result. */ \
73 result = __GCONV_INCOMPLETE_INPUT; \
74 break; \
75 } \
76 \
77 ch = (ch * 0x100) + inptr[1]; \
41488498
FW
78 /* ch was less than 0xfd. */ \
79 assert (ch < 0xfd00); \
3b7caeac
UD
80 while (ch > rp2->end) \
81 ++rp2; \
82 \
41488498 83 if (__builtin_expect (ch < rp2->start, 0) \
3b7caeac
UD
84 || (res = __ibm943db_to_ucs4[ch + rp2->idx], \
85 __builtin_expect (res, '\1') == 0 && ch !=0)) \
86 { \
87 /* This is an illegal character. */ \
e438a468 88 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
3b7caeac
UD
89 } \
90 else \
91 { \
92 put32 (outptr, res); \
93 outptr += 4; \
94 inptr += 2; \
95 } \
96 } \
97 else \
98 { \
15627a21 99 if (res == 0xa5) \
3b7caeac
UD
100 res = 0x5c; \
101 else if (res == 0x203e) \
102 res = 0x7e; \
3b7caeac
UD
103 put32 (outptr, res); \
104 outptr += 4; \
105 inptr++; \
106 } \
107 }
108#define LOOP_NEED_FLAGS
f9ad060c
UD
109#define ONEBYTE_BODY \
110 { \
111 if (c == 0x80 || c == 0xa0 || c >= 0xe0) \
112 return WEOF; \
113 uint32_t res = __ibm943sb_to_ucs4[c]; \
114 if (res == 0 && c != 0) \
115 return WEOF; \
15627a21 116 if (res == 0xa5) \
f9ad060c
UD
117 res = 0x5c; \
118 else if (res == 0x203e) \
119 res = 0x7e; \
f9ad060c
UD
120 return res; \
121 }
3b7caeac
UD
122#include <iconv/loop.c>
123
124/* Next, define the other direction. */
125#define MIN_NEEDED_INPUT MIN_NEEDED_TO
126#define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
127#define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
128#define LOOPFCT TO_LOOP
129#define BODY \
130 { \
131 const struct gap *rp = __ucs4_to_ibm943sb_idx; \
132 unsigned char sc; \
133 uint32_t ch = get32(inptr); \
f9ad060c 134 bool found = true; \
3b7caeac
UD
135 uint32_t i; \
136 uint32_t low; \
137 uint32_t high; \
138 uint16_t pccode; \
139 \
a1ffb40e 140 if (__glibc_unlikely (ch >= 0xffff)) \
601d2942
UD
141 { \
142 UNICODE_TAG_HANDLER (ch, 4); \
3b7caeac 143 rp = NULL; \
601d2942 144 } \
3b7caeac
UD
145 else \
146 while (ch > rp->end) \
147 ++rp; \
148 \
149 /* Use the UCS4 table for single byte. */ \
150 if (__builtin_expect (rp == NULL, 0) \
151 || __builtin_expect (ch < rp->start, 0) \
152 || (sc = __ucs4_to_ibm943sb[ch + rp->idx], \
153 __builtin_expect (sc, '\1') == '\0' && ch != L'\0')) \
154 { \
155 \
156 /* Use the UCS4 table for double byte. */ \
f9ad060c 157 found = false; \
3b7caeac
UD
158 low = 0; \
159 high = (sizeof (__ucs4_to_ibm943db) >> 1) \
160 / sizeof (__ucs4_to_ibm943db[0][FROM]); \
161 pccode = ch; \
a1ffb40e 162 if (__glibc_likely (rp != NULL)) \
5d32be9a
UD
163 while (low < high) \
164 { \
165 i = (low + high) >> 1; \
166 if (pccode < __ucs4_to_ibm943db[i][FROM]) \
167 high = i; \
168 else if (pccode > __ucs4_to_ibm943db[i][FROM]) \
169 low = i + 1; \
170 else \
171 { \
172 pccode = __ucs4_to_ibm943db[i][TO]; \
173 found = true; \
174 break; \
175 } \
176 } \
601d2942
UD
177 if (found) \
178 { \
a1ffb40e 179 if (__glibc_unlikely (outptr + 2 > outend)) \
601d2942
UD
180 { \
181 result = __GCONV_FULL_OUTPUT; \
182 break; \
183 } \
184 *outptr++ = pccode >> 8 & 0xff; \
185 *outptr++ = pccode & 0xff; \
186 } \
187 else \
188 { \
189 /* This is an illegal character. */ \
e438a468 190 STANDARD_TO_LOOP_ERR_HANDLER (4); \
601d2942 191 } \
3b7caeac
UD
192 } \
193 else \
194 { \
a1ffb40e 195 if (__glibc_unlikely (outptr + 1 > outend)) \
3b7caeac
UD
196 { \
197 result = __GCONV_FULL_OUTPUT; \
198 break; \
199 } \
200 if (ch == 0x5c) \
201 *outptr++ = 0x5c; \
202 else if (ch == 0x7e) \
203 *outptr++ = 0x7e; \
204 else \
205 *outptr++ = sc; \
206 } \
207 \
601d2942
UD
208 /* Now that we wrote the output increment the input pointer. */ \
209 inptr += 4; \
3b7caeac
UD
210 }
211#define LOOP_NEED_FLAGS
212#include <iconv/loop.c>
213
214/* Now define the toplevel functions. */
215#include <iconv/skeleton.c>