1 /* Conversion from and to IBM943.
2    Copyright (C) 2000-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <dlfcn.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include "ibm943.h"
23 
24 #define FROM	0
25 #define TO	1
26 
27 /* Definitions used in the body of the `gconv' function.  */
28 #define CHARSET_NAME	"IBM943//"
29 #define FROM_LOOP	from_ibm943
30 #define TO_LOOP		to_ibm943
31 #define ONE_DIRECTION	0
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-943 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 = __ibm943db_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 IBM943 table for single byte.  */			      \
63     if (__builtin_expect (ch > 0xdf, 0)					      \
64 	|| (res = __ibm943sb_to_ucs4[ch],				      \
65 	    __builtin_expect (res == 0, 0) && ch != 0))			      \
66       {									      \
67 	/* Use the IBM943 table for double byte.  */			      \
68 	if (__glibc_unlikely (inptr + 1 >= inend))			      \
69 	  {								      \
70 	    /* The second character is not available.			      \
71 	       Store the intermediate result.  */			      \
72 	    result = __GCONV_INCOMPLETE_INPUT;				      \
73 	    break;							      \
74 	  }								      \
75 									      \
76 	ch = (ch * 0x100) + inptr[1];					      \
77 	/* ch was less than 0xfd.  */					      \
78 	assert (ch < 0xfd00);						      \
79 	while (ch > rp2->end)						      \
80 	  ++rp2;							      \
81 									      \
82 	if (__builtin_expect (ch < rp2->start, 0)			      \
83 	    || (res = __ibm943db_to_ucs4[ch + rp2->idx],		      \
84 	    __builtin_expect (res, '\1') == 0 && ch !=0))		      \
85 	  {								      \
86 	    /* This is an illegal character.  */			      \
87 	    STANDARD_FROM_LOOP_ERR_HANDLER (2);				      \
88 	  }								      \
89 	else								      \
90 	  {								      \
91 	    put32 (outptr, res);					      \
92 	    outptr += 4;						      \
93 	    inptr += 2;							      \
94 	  }								      \
95       }									      \
96     else								      \
97       {									      \
98 	if (res == 0xa5)						      \
99 	  res = 0x5c;							      \
100 	else if (res == 0x203e)						      \
101 	  res = 0x7e;							      \
102 	put32 (outptr, res);						      \
103 	outptr += 4;							      \
104 	inptr++;							      \
105       }									      \
106   }
107 #define LOOP_NEED_FLAGS
108 #define ONEBYTE_BODY \
109   {									      \
110     if (c == 0x80 || c == 0xa0 || c >= 0xe0)				      \
111       return WEOF;							      \
112     uint32_t res = __ibm943sb_to_ucs4[c];				      \
113     if (res == 0 && c != 0)						      \
114       return WEOF;							      \
115     if (res == 0xa5)						              \
116       res = 0x5c;							      \
117     else if (res == 0x203e)						      \
118       res = 0x7e;							      \
119     return res;								      \
120   }
121 #include <iconv/loop.c>
122 
123 /* Next, define the other direction.  */
124 #define MIN_NEEDED_INPUT	MIN_NEEDED_TO
125 #define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
126 #define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
127 #define LOOPFCT			TO_LOOP
128 #define BODY \
129   {									      \
130     const struct gap *rp = __ucs4_to_ibm943sb_idx;			      \
131     unsigned char sc;							      \
132     uint32_t ch = get32(inptr);						      \
133     bool found = true;							      \
134     uint32_t i;								      \
135     uint32_t low;							      \
136     uint32_t high;							      \
137     uint16_t pccode;							      \
138 									      \
139     if (__glibc_unlikely (ch >= 0xffff))				      \
140       {									      \
141 	UNICODE_TAG_HANDLER (ch, 4);					      \
142 	rp = NULL;							      \
143       }									      \
144     else								      \
145       while (ch > rp->end)						      \
146 	++rp;								      \
147 									      \
148     /* Use the UCS4 table for single byte.  */				      \
149     if (__builtin_expect (rp == NULL, 0)				      \
150 	|| __builtin_expect (ch < rp->start, 0)				      \
151 	|| (sc = __ucs4_to_ibm943sb[ch + rp->idx],			      \
152 	__builtin_expect (sc, '\1') == '\0' && ch != L'\0'))		      \
153       {									      \
154 									      \
155 	/* Use the UCS4 table for double byte.  */			      \
156 	found = false;							      \
157 	low = 0;							      \
158 	high = (sizeof (__ucs4_to_ibm943db) >> 1)			      \
159 		/ sizeof (__ucs4_to_ibm943db[0][FROM]);			      \
160 	pccode = ch;							      \
161 	if (__glibc_likely (rp != NULL))				      \
162 	  while (low < high)						      \
163 	    {								      \
164 	      i = (low + high) >> 1;					      \
165 	      if (pccode < __ucs4_to_ibm943db[i][FROM])			      \
166 		high = i;						      \
167 	      else if (pccode > __ucs4_to_ibm943db[i][FROM])		      \
168 		low = i + 1;						      \
169 	      else 							      \
170 		{							      \
171 		  pccode = __ucs4_to_ibm943db[i][TO];			      \
172 		  found = true;						      \
173 		  break;						      \
174 		}							      \
175 	    }								      \
176 	if (found) 							      \
177 	  {								      \
178 	    if (__glibc_unlikely (outptr + 2 > outend))			      \
179 	      {								      \
180 		result = __GCONV_FULL_OUTPUT;				      \
181 		break;							      \
182 	      }								      \
183 	    *outptr++ = pccode >> 8 & 0xff;				      \
184 	    *outptr++ = pccode & 0xff;					      \
185 	  }								      \
186 	else								      \
187 	  {								      \
188 	    /* This is an illegal character.  */			      \
189 	    STANDARD_TO_LOOP_ERR_HANDLER (4);				      \
190 	  }								      \
191       }									      \
192     else								      \
193       {									      \
194 	if (__glibc_unlikely (outptr + 1 > outend))			      \
195 	  {								      \
196 	    result = __GCONV_FULL_OUTPUT;				      \
197 	    break;							      \
198 	  }								      \
199 	if (ch == 0x5c)							      \
200 	  *outptr++ = 0x5c;						      \
201 	else if (ch == 0x7e)						      \
202 	  *outptr++ = 0x7e;						      \
203 	else								      \
204 	  *outptr++ = sc;						      \
205       }									      \
206 									      \
207     /* Now that we wrote the output increment the input pointer.  */	      \
208     inptr += 4;								      \
209   }
210 #define LOOP_NEED_FLAGS
211 #include <iconv/loop.c>
212 
213 /* Now define the toplevel functions.  */
214 #include <iconv/skeleton.c>
215