| 1 | /* Conversion from and to IBM1364. | 
|---|
| 2 | Copyright (C) 2005-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 | Contributed by Masahide Washizawa <washi@jp.ibm.com>, 2005. | 
|---|
| 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 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 19 |  | 
|---|
| 20 | #include <dlfcn.h> | 
|---|
| 21 | #include <stdint.h> | 
|---|
| 22 | #include <wchar.h> | 
|---|
| 23 | #include <byteswap.h> | 
|---|
| 24 |  | 
|---|
| 25 | #ifndef CHARSET_NAME | 
|---|
| 26 | /* This is really the IBM1364 converter, not another module sharing | 
|---|
| 27 | the code.  */ | 
|---|
| 28 | # define 	"ibm1364.h" | 
|---|
| 29 | # define CHARSET_NAME	"IBM1364//" | 
|---|
| 30 | # define FROM_LOOP	from_ibm1364 | 
|---|
| 31 | # define TO_LOOP	to_ibm1364 | 
|---|
| 32 | # define SB_TO_UCS4	__ibm1364sb_to_ucs4 | 
|---|
| 33 | # define DB_TO_UCS4_IDX	__ibm1364db_to_ucs4_idx | 
|---|
| 34 | # define DB_TO_UCS4	__ibm1364db_to_ucs4 | 
|---|
| 35 | # define UCS4_TO_SB_IDX	__ucs4_to_ibm1364sb_idx | 
|---|
| 36 | # define UCS4_TO_SB	__ucs4_to_ibm1364sb | 
|---|
| 37 | # define UCS4_TO_DB_IDX	__ucs4_to_ibm1364db_idx | 
|---|
| 38 | # define UCS4_TO_DB	__ucs4_to_ibm1364db | 
|---|
| 39 | # define UCS_LIMIT	0xffff | 
|---|
| 40 | #endif | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | #include DATA_HEADER | 
|---|
| 44 |  | 
|---|
| 45 | /* The shift sequences for this charset (it does not use ESC).  */ | 
|---|
| 46 | #define SI 		0x0F  /* Shift In, host code to turn DBCS off.  */ | 
|---|
| 47 | #define SO 		0x0E  /* Shift Out, host code to turn DBCS on.  */ | 
|---|
| 48 |  | 
|---|
| 49 | /* Definitions used in the body of the `gconv' function.  */ | 
|---|
| 50 | #define MIN_NEEDED_FROM	1 | 
|---|
| 51 | #define MAX_NEEDED_FROM	2 | 
|---|
| 52 | #define MIN_NEEDED_TO	4 | 
|---|
| 53 | #ifdef HAS_COMBINED | 
|---|
| 54 | # define MAX_NEEDED_TO	8 | 
|---|
| 55 | #else | 
|---|
| 56 | # define MAX_NEEDED_TO	4 | 
|---|
| 57 | #endif | 
|---|
| 58 | #define ONE_DIRECTION	0 | 
|---|
| 59 | #define PREPARE_LOOP \ | 
|---|
| 60 | int save_curcs;							      \ | 
|---|
| 61 | int *curcsp = &data->__statep->__count; | 
|---|
| 62 | #define 		, curcsp | 
|---|
| 63 |  | 
|---|
| 64 | /* Definitions of initialization and destructor function.  */ | 
|---|
| 65 | #define DEFINE_INIT	1 | 
|---|
| 66 | #define DEFINE_FINI	1 | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | /* Since this is a stateful encoding we have to provide code which resets | 
|---|
| 70 | the output state to the initial state.  This has to be done during the | 
|---|
| 71 | flushing.  */ | 
|---|
| 72 | #define EMIT_SHIFT_TO_INIT \ | 
|---|
| 73 | if ((data->__statep->__count & ~7) != sb)				      \ | 
|---|
| 74 | {									      \ | 
|---|
| 75 | if (FROM_DIRECTION)						      \ | 
|---|
| 76 | data->__statep->__count &= 7;					      \ | 
|---|
| 77 | else								      \ | 
|---|
| 78 | {								      \ | 
|---|
| 79 | /* We are not in the initial state.  To switch back we have	      \ | 
|---|
| 80 | to emit `SI'.  */						      \ | 
|---|
| 81 | if (__glibc_unlikely (outbuf >= outend))			      \ | 
|---|
| 82 | /* We don't have enough room in the output buffer.  */	      \ | 
|---|
| 83 | status = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 84 | else								      \ | 
|---|
| 85 | {								      \ | 
|---|
| 86 | /* Write out the shift sequence.  */			      \ | 
|---|
| 87 | *outbuf++ = SI;						      \ | 
|---|
| 88 | data->__statep->__count &= 7;				      \ | 
|---|
| 89 | }								      \ | 
|---|
| 90 | }								      \ | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | /* Since we might have to reset input pointer we must be able to save | 
|---|
| 95 | and retore the state.  */ | 
|---|
| 96 | #define SAVE_RESET_STATE(Save) \ | 
|---|
| 97 | if (Save)								      \ | 
|---|
| 98 | save_curcs = *curcsp;						      \ | 
|---|
| 99 | else									      \ | 
|---|
| 100 | *curcsp = save_curcs | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | /* Current codeset type.  */ | 
|---|
| 104 | enum | 
|---|
| 105 | { | 
|---|
| 106 | sb = 0, | 
|---|
| 107 | db = 64 | 
|---|
| 108 | }; | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | /* Subroutine to write out converted UCS4 from IBM-13XX.  */ | 
|---|
| 112 | #ifdef HAS_COMBINED | 
|---|
| 113 | # define SUB_COMBINED_UCS_FROM_IBM13XX \ | 
|---|
| 114 | {									      \ | 
|---|
| 115 | if (res != UCS_LIMIT || ch < __TO_UCS4_COMBINED_MIN			      \ | 
|---|
| 116 | || ch > __TO_UCS4_COMBINED_MAX)					      \ | 
|---|
| 117 | {									      \ | 
|---|
| 118 | put32 (outptr, res);						      \ | 
|---|
| 119 | outptr += 4;							      \ | 
|---|
| 120 | }									      \ | 
|---|
| 121 | else								      \ | 
|---|
| 122 | {									      \ | 
|---|
| 123 | /* This is a combined character.  Make sure we have room.  */	      \ | 
|---|
| 124 | if (__glibc_unlikely (outptr + 8 > outend))			      \ | 
|---|
| 125 | {								      \ | 
|---|
| 126 | result = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 127 | break;							      \ | 
|---|
| 128 | }								      \ | 
|---|
| 129 | \ | 
|---|
| 130 | const struct divide *cmbp					      \ | 
|---|
| 131 | = &DB_TO_UCS4_COMB[ch - __TO_UCS4_COMBINED_MIN];		      \ | 
|---|
| 132 | assert (cmbp->res1 != 0 && cmbp->res2 != 0);			      \ | 
|---|
| 133 | \ | 
|---|
| 134 | put32 (outptr, cmbp->res1);					      \ | 
|---|
| 135 | outptr += 4;							      \ | 
|---|
| 136 | put32 (outptr, cmbp->res2);					      \ | 
|---|
| 137 | outptr += 4;							      \ | 
|---|
| 138 | }									      \ | 
|---|
| 139 | } | 
|---|
| 140 | #else | 
|---|
| 141 | # define SUB_COMBINED_UCS_FROM_IBM13XX \ | 
|---|
| 142 | {									      \ | 
|---|
| 143 | put32 (outptr, res);						      \ | 
|---|
| 144 | outptr += 4;							      \ | 
|---|
| 145 | } | 
|---|
| 146 | #endif /* HAS_COMBINED */ | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 | /* First, define the conversion function from IBM-13XX to UCS4.  */ | 
|---|
| 150 | #define MIN_NEEDED_INPUT  	MIN_NEEDED_FROM | 
|---|
| 151 | #define MAX_NEEDED_INPUT  	MAX_NEEDED_FROM | 
|---|
| 152 | #define MIN_NEEDED_OUTPUT 	MIN_NEEDED_TO | 
|---|
| 153 | #define MAX_NEEDED_OUTPUT 	MAX_NEEDED_TO | 
|---|
| 154 | #define LOOPFCT 		FROM_LOOP | 
|---|
| 155 | #define BODY \ | 
|---|
| 156 | {									      \ | 
|---|
| 157 | uint32_t ch = *inptr;						      \ | 
|---|
| 158 | \ | 
|---|
| 159 | if (__builtin_expect (ch, 0) == SO)					      \ | 
|---|
| 160 | {									      \ | 
|---|
| 161 | /* Shift OUT, change to DBCS converter.  */			      \ | 
|---|
| 162 | if (curcs == db)						      \ | 
|---|
| 163 | {								      \ | 
|---|
| 164 | result = __GCONV_ILLEGAL_INPUT;				      \ | 
|---|
| 165 | break;							      \ | 
|---|
| 166 | }								      \ | 
|---|
| 167 | curcs = db;							      \ | 
|---|
| 168 | ++inptr;							      \ | 
|---|
| 169 | continue;							      \ | 
|---|
| 170 | }									      \ | 
|---|
| 171 | if (__builtin_expect (ch, 0) == SI)					      \ | 
|---|
| 172 | {									      \ | 
|---|
| 173 | /* Shift IN, change to SBCS converter.  */			      \ | 
|---|
| 174 | if (curcs == sb)						      \ | 
|---|
| 175 | {								      \ | 
|---|
| 176 | result = __GCONV_ILLEGAL_INPUT;				      \ | 
|---|
| 177 | break;							      \ | 
|---|
| 178 | }								      \ | 
|---|
| 179 | curcs = sb;							      \ | 
|---|
| 180 | ++inptr;							      \ | 
|---|
| 181 | continue;							      \ | 
|---|
| 182 | }									      \ | 
|---|
| 183 | \ | 
|---|
| 184 | if (curcs == sb)							      \ | 
|---|
| 185 | {									      \ | 
|---|
| 186 | /* Use the IBM13XX table for single byte.  */			      \ | 
|---|
| 187 | uint32_t res = SB_TO_UCS4[ch];				      \ | 
|---|
| 188 | if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0')	      \ | 
|---|
| 189 | {								      \ | 
|---|
| 190 | /* This is an illegal character.  */			      \ | 
|---|
| 191 | if (! ignore_errors_p ())					      \ | 
|---|
| 192 | {								      \ | 
|---|
| 193 | result = __GCONV_ILLEGAL_INPUT;				      \ | 
|---|
| 194 | break;							      \ | 
|---|
| 195 | }								      \ | 
|---|
| 196 | ++*irreversible;						      \ | 
|---|
| 197 | }								      \ | 
|---|
| 198 | else								      \ | 
|---|
| 199 | {								      \ | 
|---|
| 200 | put32 (outptr, res);					      \ | 
|---|
| 201 | outptr += 4;						      \ | 
|---|
| 202 | }								      \ | 
|---|
| 203 | ++inptr;							      \ | 
|---|
| 204 | }									      \ | 
|---|
| 205 | else								      \ | 
|---|
| 206 | {									      \ | 
|---|
| 207 | assert (curcs == db);						      \ | 
|---|
| 208 | \ | 
|---|
| 209 | if (__glibc_unlikely (inptr + 1 >= inend))			      \ | 
|---|
| 210 | {								      \ | 
|---|
| 211 | /* The second character is not available.  Store the	      \ | 
|---|
| 212 | intermediate result.  */					      \ | 
|---|
| 213 | result = __GCONV_INCOMPLETE_INPUT;				      \ | 
|---|
| 214 | break;							      \ | 
|---|
| 215 | }								      \ | 
|---|
| 216 | \ | 
|---|
| 217 | ch = (ch * 0x100) + inptr[1];					      \ | 
|---|
| 218 | \ | 
|---|
| 219 | /* Use the IBM1364 table for double byte.  */			      \ | 
|---|
| 220 | const struct gap *rp2 = DB_TO_UCS4_IDX;				      \ | 
|---|
| 221 | while (ch > rp2->end)						      \ | 
|---|
| 222 | ++rp2;							      \ | 
|---|
| 223 | \ | 
|---|
| 224 | uint32_t res;							      \ | 
|---|
| 225 | if (__builtin_expect (rp2->start == 0xffff, 0)			      \ | 
|---|
| 226 | || __builtin_expect (ch < rp2->start, 0)			      \ | 
|---|
| 227 | || (res = DB_TO_UCS4[ch + rp2->idx],			      \ | 
|---|
| 228 | __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))	      \ | 
|---|
| 229 | {								      \ | 
|---|
| 230 | /* This is an illegal character.  */			      \ | 
|---|
| 231 | if (! ignore_errors_p ())					      \ | 
|---|
| 232 | {								      \ | 
|---|
| 233 | result = __GCONV_ILLEGAL_INPUT;				      \ | 
|---|
| 234 | break;							      \ | 
|---|
| 235 | }								      \ | 
|---|
| 236 | ++*irreversible;						      \ | 
|---|
| 237 | }								      \ | 
|---|
| 238 | else								      \ | 
|---|
| 239 | {								      \ | 
|---|
| 240 | SUB_COMBINED_UCS_FROM_IBM13XX;				      \ | 
|---|
| 241 | }								      \ | 
|---|
| 242 | inptr += 2;							      \ | 
|---|
| 243 | }									      \ | 
|---|
| 244 | } | 
|---|
| 245 | #define LOOP_NEED_FLAGS | 
|---|
| 246 | #define 	, int *curcsp | 
|---|
| 247 | #define INIT_PARAMS		int curcs = *curcsp & ~7 | 
|---|
| 248 | #define UPDATE_PARAMS		*curcsp = curcs | 
|---|
| 249 | #include <iconv/loop.c> | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 | /* Subroutine to convert two UCS4 codes to IBM-13XX.  */ | 
|---|
| 253 | #ifdef HAS_COMBINED | 
|---|
| 254 | # define SUB_COMBINED_UCS_TO_IBM13XX \ | 
|---|
| 255 | {									      \ | 
|---|
| 256 | const struct combine *cmbp = UCS4_COMB_TO_DB;			      \ | 
|---|
| 257 | while (cmbp->res1 < ch)						      \ | 
|---|
| 258 | ++cmbp;								      \ | 
|---|
| 259 | /* XXX if last char is beginning of combining store in state */	      \ | 
|---|
| 260 | if (cmbp->res1 == ch && inptr + 4 < inend)				      \ | 
|---|
| 261 | {									      \ | 
|---|
| 262 | /* See if input is part of a combined character.  */		      \ | 
|---|
| 263 | uint32_t ch_next = get32 (inptr + 4);				      \ | 
|---|
| 264 | while (cmbp->res2 != ch_next)					      \ | 
|---|
| 265 | {								      \ | 
|---|
| 266 | ++cmbp;							      \ | 
|---|
| 267 | if (cmbp->res1 != ch)					      \ | 
|---|
| 268 | goto not_combined;					      \ | 
|---|
| 269 | }								      \ | 
|---|
| 270 | \ | 
|---|
| 271 | /* It is a combined character.  First make sure we are in	      \ | 
|---|
| 272 | double byte mode.  */					      \ | 
|---|
| 273 | if (curcs == sb)						      \ | 
|---|
| 274 | {								      \ | 
|---|
| 275 | /* We know there is room for at least one byte.  */		      \ | 
|---|
| 276 | *outptr++ = SO;						      \ | 
|---|
| 277 | curcs = db;							      \ | 
|---|
| 278 | }								      \ | 
|---|
| 279 | \ | 
|---|
| 280 | if (__glibc_unlikely (outptr + 2 > outend))			      \ | 
|---|
| 281 | {								      \ | 
|---|
| 282 | result = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 283 | break;							      \ | 
|---|
| 284 | }								      \ | 
|---|
| 285 | *outptr++ = cmbp->ch[0];					      \ | 
|---|
| 286 | *outptr++ = cmbp->ch[1];					      \ | 
|---|
| 287 | inptr += 8;							      \ | 
|---|
| 288 | continue;							      \ | 
|---|
| 289 | \ | 
|---|
| 290 | not_combined:;							      \ | 
|---|
| 291 | }									      \ | 
|---|
| 292 | } | 
|---|
| 293 | #else | 
|---|
| 294 | # define SUB_COMBINED_UCS_TO_IBM13XX | 
|---|
| 295 | #endif /* HAS_COMBINED */ | 
|---|
| 296 |  | 
|---|
| 297 |  | 
|---|
| 298 | /* Next, define the other direction.  */ | 
|---|
| 299 | #define MIN_NEEDED_INPUT	MIN_NEEDED_TO | 
|---|
| 300 | #define MAX_NEEDED_INPUT  	MAX_NEEDED_TO | 
|---|
| 301 | #define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM | 
|---|
| 302 | #define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM | 
|---|
| 303 | #define LOOPFCT			TO_LOOP | 
|---|
| 304 | #define BODY \ | 
|---|
| 305 | {									      \ | 
|---|
| 306 | uint32_t ch = get32 (inptr);					      \ | 
|---|
| 307 | \ | 
|---|
| 308 | if (__glibc_unlikely (ch >= UCS_LIMIT))				      \ | 
|---|
| 309 | {									      \ | 
|---|
| 310 | UNICODE_TAG_HANDLER (ch, 4);					      \ | 
|---|
| 311 | \ | 
|---|
| 312 | if (! ignore_errors_p ())					      \ | 
|---|
| 313 | {								      \ | 
|---|
| 314 | result = __GCONV_ILLEGAL_INPUT;				      \ | 
|---|
| 315 | break;							      \ | 
|---|
| 316 | }								      \ | 
|---|
| 317 | ++*irreversible;						      \ | 
|---|
| 318 | inptr += 4;							      \ | 
|---|
| 319 | continue;							      \ | 
|---|
| 320 | }									      \ | 
|---|
| 321 | \ | 
|---|
| 322 | SUB_COMBINED_UCS_TO_IBM13XX;					      \ | 
|---|
| 323 | \ | 
|---|
| 324 | const struct gap *rp1 = UCS4_TO_SB_IDX;				      \ | 
|---|
| 325 | while (ch > rp1->end)						      \ | 
|---|
| 326 | ++rp1;								      \ | 
|---|
| 327 | \ | 
|---|
| 328 | /* Use the UCS4 table for single byte.  */				      \ | 
|---|
| 329 | const char *cp;							      \ | 
|---|
| 330 | if (__builtin_expect (ch < rp1->start, 0)				      \ | 
|---|
| 331 | || (cp = UCS4_TO_SB[ch + rp1->idx],				      \ | 
|---|
| 332 | __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))	      \ | 
|---|
| 333 | {									      \ | 
|---|
| 334 | /* Use the UCS4 table for double byte.  */			      \ | 
|---|
| 335 | const struct gap *rp2 = UCS4_TO_DB_IDX;				      \ | 
|---|
| 336 | while (ch > rp2->end)						      \ | 
|---|
| 337 | ++rp2;							      \ | 
|---|
| 338 | \ | 
|---|
| 339 | if (__builtin_expect (ch < rp2->start, 0)			      \ | 
|---|
| 340 | || (cp = UCS4_TO_DB[ch + rp2->idx],				      \ | 
|---|
| 341 | __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))      \ | 
|---|
| 342 | {								      \ | 
|---|
| 343 | /* This is an illegal character.  */			      \ | 
|---|
| 344 | if (! ignore_errors_p ())					      \ | 
|---|
| 345 | {								      \ | 
|---|
| 346 | result = __GCONV_ILLEGAL_INPUT;				      \ | 
|---|
| 347 | break;							      \ | 
|---|
| 348 | }								      \ | 
|---|
| 349 | ++*irreversible;						      \ | 
|---|
| 350 | }								      \ | 
|---|
| 351 | else								      \ | 
|---|
| 352 | {								      \ | 
|---|
| 353 | if (curcs == sb)						      \ | 
|---|
| 354 | {								      \ | 
|---|
| 355 | /* We know there is room for at least one byte.  */	      \ | 
|---|
| 356 | *outptr++ = SO;						      \ | 
|---|
| 357 | curcs = db;						      \ | 
|---|
| 358 | }								      \ | 
|---|
| 359 | \ | 
|---|
| 360 | if (__glibc_unlikely (outptr + 2 > outend))			      \ | 
|---|
| 361 | {								      \ | 
|---|
| 362 | result = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 363 | break;							      \ | 
|---|
| 364 | }								      \ | 
|---|
| 365 | *outptr++ = cp[0];						      \ | 
|---|
| 366 | *outptr++ = cp[1];						      \ | 
|---|
| 367 | }								      \ | 
|---|
| 368 | }									      \ | 
|---|
| 369 | else								      \ | 
|---|
| 370 | {									      \ | 
|---|
| 371 | if (__glibc_unlikely (curcs == db))				      \ | 
|---|
| 372 | {								      \ | 
|---|
| 373 | /* We know there is room for at least one byte.  */		      \ | 
|---|
| 374 | *outptr++ = SI;						      \ | 
|---|
| 375 | curcs = sb;							      \ | 
|---|
| 376 | \ | 
|---|
| 377 | if (__glibc_unlikely (outptr >= outend))			      \ | 
|---|
| 378 | {								      \ | 
|---|
| 379 | result = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 380 | break;							      \ | 
|---|
| 381 | }								      \ | 
|---|
| 382 | }								      \ | 
|---|
| 383 | \ | 
|---|
| 384 | *outptr++ = cp[0];						      \ | 
|---|
| 385 | }									      \ | 
|---|
| 386 | \ | 
|---|
| 387 | /* Now that we wrote the output increment the input pointer.  */	      \ | 
|---|
| 388 | inptr += 4;								      \ | 
|---|
| 389 | } | 
|---|
| 390 | #define LOOP_NEED_FLAGS | 
|---|
| 391 | #define 	, int *curcsp | 
|---|
| 392 | #define INIT_PARAMS		int curcs = *curcsp & ~7 | 
|---|
| 393 | #define REINIT_PARAMS		curcs = *curcsp & ~7 | 
|---|
| 394 | #define UPDATE_PARAMS		*curcsp = curcs | 
|---|
| 395 | #include <iconv/loop.c> | 
|---|
| 396 |  | 
|---|
| 397 | /* Now define the toplevel functions.  */ | 
|---|
| 398 | #include <iconv/skeleton.c> | 
|---|
| 399 |  | 
|---|