| 1 | /* Conversion module for ISO-2022-JP-3. |
| 2 | Copyright (C) 1998-2021 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998, |
| 5 | and Bruno Haible <bruno@clisp.org>, 2002. |
| 6 | |
| 7 | The GNU C Library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | The GNU C Library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with the GNU C Library; if not, see |
| 19 | <https://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <dlfcn.h> |
| 23 | #include <gconv.h> |
| 24 | #include <stdint.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "jis0201.h" |
| 28 | #include "jis0208.h" |
| 29 | #include "jisx0213.h" |
| 30 | |
| 31 | /* This makes obvious what everybody knows: 0x1b is the Esc character. */ |
| 32 | #define ESC 0x1b |
| 33 | |
| 34 | /* Definitions used in the body of the `gconv' function. */ |
| 35 | #define CHARSET_NAME "ISO-2022-JP-3//" |
| 36 | #define FROM_LOOP from_iso2022jp3_loop |
| 37 | #define TO_LOOP to_iso2022jp3_loop |
| 38 | #define DEFINE_INIT 1 |
| 39 | #define DEFINE_FINI 1 |
| 40 | #define ONE_DIRECTION 0 |
| 41 | #define FROM_LOOP_MIN_NEEDED_FROM 1 |
| 42 | #define FROM_LOOP_MAX_NEEDED_FROM 4 |
| 43 | #define FROM_LOOP_MIN_NEEDED_TO 4 |
| 44 | #define FROM_LOOP_MAX_NEEDED_TO 8 |
| 45 | #define TO_LOOP_MIN_NEEDED_FROM 4 |
| 46 | #define TO_LOOP_MAX_NEEDED_FROM 4 |
| 47 | #define TO_LOOP_MIN_NEEDED_TO 1 |
| 48 | #define TO_LOOP_MAX_NEEDED_TO 6 |
| 49 | #define PREPARE_LOOP \ |
| 50 | int saved_state; \ |
| 51 | int *statep = &data->__statep->__count; |
| 52 | #define , statep |
| 53 | |
| 54 | |
| 55 | /* The COUNT element of the state keeps track of the currently selected |
| 56 | character set. The possible values are: */ |
| 57 | enum |
| 58 | { |
| 59 | ASCII_set = 0, /* Esc ( B */ |
| 60 | JISX0208_1978_set = 1 << 3, /* Esc $ @ */ |
| 61 | JISX0208_1983_set = 2 << 3, /* Esc $ B */ |
| 62 | JISX0201_Roman_set = 3 << 3, /* Esc ( J */ |
| 63 | JISX0201_Kana_set = 4 << 3, /* Esc ( I */ |
| 64 | JISX0213_1_2000_set = 5 << 3, /* Esc $ ( O */ |
| 65 | JISX0213_2_set = 6 << 3, /* Esc $ ( P */ |
| 66 | JISX0213_1_2004_set = 7 << 3, /* Esc $ ( Q */ |
| 67 | CURRENT_SEL_MASK = 7 << 3 |
| 68 | }; |
| 69 | |
| 70 | /* During UCS-4 to ISO-2022-JP-3 conversion, the COUNT element of the |
| 71 | state also contains the last two bytes to be output, shifted by 6 |
| 72 | bits, and a one-bit indicator whether they must be preceded by the |
| 73 | shift sequence, in bit 22. During ISO-2022-JP-3 to UCS-4 |
| 74 | conversion, COUNT may also contain a non-zero pending wide |
| 75 | character, shifted by six bits. This happens for certain inputs in |
| 76 | JISX0213_1_2004_set and JISX0213_2_set if the second wide character |
| 77 | in a combining sequence cannot be written because the buffer is |
| 78 | full. */ |
| 79 | |
| 80 | /* Since this is a stateful encoding we have to provide code which resets |
| 81 | the output state to the initial state. This has to be done during the |
| 82 | flushing. */ |
| 83 | #define EMIT_SHIFT_TO_INIT \ |
| 84 | if (data->__statep->__count != ASCII_set) \ |
| 85 | { \ |
| 86 | if (FROM_DIRECTION) \ |
| 87 | { \ |
| 88 | if (__glibc_likely (outbuf + 4 <= outend)) \ |
| 89 | { \ |
| 90 | /* Write out the last character. */ \ |
| 91 | *((uint32_t *) outbuf) = data->__statep->__count >> 6; \ |
| 92 | outbuf += sizeof (uint32_t); \ |
| 93 | data->__statep->__count = ASCII_set; \ |
| 94 | } \ |
| 95 | else \ |
| 96 | /* We don't have enough room in the output buffer. */ \ |
| 97 | status = __GCONV_FULL_OUTPUT; \ |
| 98 | } \ |
| 99 | else \ |
| 100 | { \ |
| 101 | /* We are not in the initial state. To switch back we have \ |
| 102 | to write out the buffered character and/or emit the sequence \ |
| 103 | `Esc ( B'. */ \ |
| 104 | size_t need = \ |
| 105 | (data->__statep->__count >> 6 \ |
| 106 | ? (data->__statep->__count >> 22 ? 3 : 0) + 2 \ |
| 107 | : 0) \ |
| 108 | + ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set \ |
| 109 | ? 3 : 0); \ |
| 110 | \ |
| 111 | if (__glibc_unlikely (outbuf + need > outend)) \ |
| 112 | /* We don't have enough room in the output buffer. */ \ |
| 113 | status = __GCONV_FULL_OUTPUT; \ |
| 114 | else \ |
| 115 | { \ |
| 116 | if (data->__statep->__count >> 6) \ |
| 117 | { \ |
| 118 | uint32_t lasttwo = data->__statep->__count >> 6; \ |
| 119 | \ |
| 120 | if (lasttwo >> 16) \ |
| 121 | { \ |
| 122 | /* Write out the shift sequence before the last \ |
| 123 | character. */ \ |
| 124 | assert ((data->__statep->__count & CURRENT_SEL_MASK) \ |
| 125 | == JISX0208_1983_set); \ |
| 126 | *outbuf++ = ESC; \ |
| 127 | *outbuf++ = '$'; \ |
| 128 | *outbuf++ = 'B'; \ |
| 129 | } \ |
| 130 | /* Write out the last character. */ \ |
| 131 | *outbuf++ = (lasttwo >> 8) & 0xff; \ |
| 132 | *outbuf++ = lasttwo & 0xff; \ |
| 133 | } \ |
| 134 | if ((data->__statep->__count & CURRENT_SEL_MASK) != ASCII_set) \ |
| 135 | { \ |
| 136 | /* Write out the shift sequence. */ \ |
| 137 | *outbuf++ = ESC; \ |
| 138 | *outbuf++ = '('; \ |
| 139 | *outbuf++ = 'B'; \ |
| 140 | } \ |
| 141 | data->__statep->__count &= 7; \ |
| 142 | data->__statep->__count |= ASCII_set; \ |
| 143 | } \ |
| 144 | } \ |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /* Since we might have to reset input pointer we must be able to save |
| 149 | and retore the state. */ |
| 150 | #define SAVE_RESET_STATE(Save) \ |
| 151 | if (Save) \ |
| 152 | saved_state = *statep; \ |
| 153 | else \ |
| 154 | *statep = saved_state |
| 155 | |
| 156 | |
| 157 | /* First define the conversion function from ISO-2022-JP-3 to UCS-4. */ |
| 158 | #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM |
| 159 | #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM |
| 160 | #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO |
| 161 | #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO |
| 162 | #define LOOPFCT FROM_LOOP |
| 163 | #define BODY \ |
| 164 | { \ |
| 165 | uint32_t ch; \ |
| 166 | \ |
| 167 | /* Output any pending character. */ \ |
| 168 | ch = set >> 6; \ |
| 169 | if (__glibc_unlikely (ch != 0)) \ |
| 170 | { \ |
| 171 | put32 (outptr, ch); \ |
| 172 | outptr += 4; \ |
| 173 | /* Remove the pending character, but preserve state bits. */ \ |
| 174 | set &= (1 << 6) - 1; \ |
| 175 | continue; \ |
| 176 | } \ |
| 177 | \ |
| 178 | /* Otherwise read the next input byte. */ \ |
| 179 | ch = *inptr; \ |
| 180 | \ |
| 181 | /* Recognize escape sequences. */ \ |
| 182 | if (__glibc_unlikely (ch == ESC)) \ |
| 183 | { \ |
| 184 | /* We now must be prepared to read two to three more bytes. \ |
| 185 | If we have a match in the first byte but then the input buffer \ |
| 186 | ends we terminate with an error since we must not risk missing \ |
| 187 | an escape sequence just because it is not entirely in the \ |
| 188 | current input buffer. */ \ |
| 189 | if (__builtin_expect (inptr + 2 >= inend, 0) \ |
| 190 | || (inptr[1] == '$' && inptr[2] == '(' \ |
| 191 | && __builtin_expect (inptr + 3 >= inend, 0))) \ |
| 192 | { \ |
| 193 | /* Not enough input available. */ \ |
| 194 | result = __GCONV_INCOMPLETE_INPUT; \ |
| 195 | break; \ |
| 196 | } \ |
| 197 | \ |
| 198 | if (inptr[1] == '(') \ |
| 199 | { \ |
| 200 | if (inptr[2] == 'B') \ |
| 201 | { \ |
| 202 | /* ASCII selected. */ \ |
| 203 | set = ASCII_set; \ |
| 204 | inptr += 3; \ |
| 205 | continue; \ |
| 206 | } \ |
| 207 | else if (inptr[2] == 'J') \ |
| 208 | { \ |
| 209 | /* JIS X 0201 selected. */ \ |
| 210 | set = JISX0201_Roman_set; \ |
| 211 | inptr += 3; \ |
| 212 | continue; \ |
| 213 | } \ |
| 214 | else if (inptr[2] == 'I') \ |
| 215 | { \ |
| 216 | /* JIS X 0201 selected. */ \ |
| 217 | set = JISX0201_Kana_set; \ |
| 218 | inptr += 3; \ |
| 219 | continue; \ |
| 220 | } \ |
| 221 | } \ |
| 222 | else if (inptr[1] == '$') \ |
| 223 | { \ |
| 224 | if (inptr[2] == '@') \ |
| 225 | { \ |
| 226 | /* JIS X 0208-1978 selected. */ \ |
| 227 | set = JISX0208_1978_set; \ |
| 228 | inptr += 3; \ |
| 229 | continue; \ |
| 230 | } \ |
| 231 | else if (inptr[2] == 'B') \ |
| 232 | { \ |
| 233 | /* JIS X 0208-1983 selected. */ \ |
| 234 | set = JISX0208_1983_set; \ |
| 235 | inptr += 3; \ |
| 236 | continue; \ |
| 237 | } \ |
| 238 | else if (inptr[2] == '(') \ |
| 239 | { \ |
| 240 | if (inptr[3] == 'O' || inptr[3] == 'Q') \ |
| 241 | { \ |
| 242 | /* JIS X 0213 plane 1 selected. */ \ |
| 243 | /* In this direction we don't need to distinguish the \ |
| 244 | versions from 2000 and 2004. */ \ |
| 245 | set = JISX0213_1_2004_set; \ |
| 246 | inptr += 4; \ |
| 247 | continue; \ |
| 248 | } \ |
| 249 | else if (inptr[3] == 'P') \ |
| 250 | { \ |
| 251 | /* JIS X 0213 plane 2 selected. */ \ |
| 252 | set = JISX0213_2_set; \ |
| 253 | inptr += 4; \ |
| 254 | continue; \ |
| 255 | } \ |
| 256 | } \ |
| 257 | } \ |
| 258 | } \ |
| 259 | \ |
| 260 | if (ch >= 0x80) \ |
| 261 | { \ |
| 262 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
| 263 | } \ |
| 264 | else if (set == ASCII_set || (ch < 0x21 || ch == 0x7f)) \ |
| 265 | /* Almost done, just advance the input pointer. */ \ |
| 266 | ++inptr; \ |
| 267 | else if (set == JISX0201_Roman_set) \ |
| 268 | { \ |
| 269 | /* Use the JIS X 0201 table. */ \ |
| 270 | ch = jisx0201_to_ucs4 (ch); \ |
| 271 | if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \ |
| 272 | { \ |
| 273 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
| 274 | } \ |
| 275 | ++inptr; \ |
| 276 | } \ |
| 277 | else if (set == JISX0201_Kana_set) \ |
| 278 | { \ |
| 279 | /* Use the JIS X 0201 table. */ \ |
| 280 | ch = jisx0201_to_ucs4 (ch + 0x80); \ |
| 281 | if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \ |
| 282 | { \ |
| 283 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
| 284 | } \ |
| 285 | ++inptr; \ |
| 286 | } \ |
| 287 | else if (set == JISX0208_1978_set || set == JISX0208_1983_set) \ |
| 288 | { \ |
| 289 | /* XXX I don't have the tables for these two old variants of \ |
| 290 | JIS X 0208. Therefore I'm using the tables for JIS X \ |
| 291 | 0208-1990. If somebody has problems with this please \ |
| 292 | provide the appropriate tables. */ \ |
| 293 | ch = jisx0208_to_ucs4 (&inptr, inend - inptr, 0); \ |
| 294 | \ |
| 295 | if (__glibc_unlikely (ch == 0)) \ |
| 296 | { \ |
| 297 | result = __GCONV_INCOMPLETE_INPUT; \ |
| 298 | break; \ |
| 299 | } \ |
| 300 | else if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)) \ |
| 301 | { \ |
| 302 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
| 303 | } \ |
| 304 | } \ |
| 305 | else /* (set == JISX0213_1_2004_set || set == JISX0213_2_set) */ \ |
| 306 | { \ |
| 307 | if (__glibc_unlikely (inptr + 1 >= inend)) \ |
| 308 | { \ |
| 309 | result = __GCONV_INCOMPLETE_INPUT; \ |
| 310 | break; \ |
| 311 | } \ |
| 312 | \ |
| 313 | ch = jisx0213_to_ucs4 ( \ |
| 314 | ((JISX0213_1_2004_set - set + (1 << 3)) << 5) + ch, \ |
| 315 | inptr[1]); \ |
| 316 | if (ch == 0) \ |
| 317 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
| 318 | \ |
| 319 | if (ch < 0x80) \ |
| 320 | { \ |
| 321 | /* It's a combining character. */ \ |
| 322 | uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0]; \ |
| 323 | uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1]; \ |
| 324 | \ |
| 325 | inptr += 2; \ |
| 326 | \ |
| 327 | put32 (outptr, u1); \ |
| 328 | outptr += 4; \ |
| 329 | \ |
| 330 | /* See whether we have room for two characters. */ \ |
| 331 | if (outptr + 4 <= outend) \ |
| 332 | { \ |
| 333 | put32 (outptr, u2); \ |
| 334 | outptr += 4; \ |
| 335 | continue; \ |
| 336 | } \ |
| 337 | \ |
| 338 | /* Otherwise store only the first character now, and \ |
| 339 | put the second one into the queue. */ \ |
| 340 | set |= u2 << 6; \ |
| 341 | /* Tell the caller why we terminate the loop. */ \ |
| 342 | result = __GCONV_FULL_OUTPUT; \ |
| 343 | break; \ |
| 344 | } \ |
| 345 | \ |
| 346 | inptr += 2; \ |
| 347 | } \ |
| 348 | \ |
| 349 | put32 (outptr, ch); \ |
| 350 | outptr += 4; \ |
| 351 | } |
| 352 | #define LOOP_NEED_FLAGS |
| 353 | #define , int *statep |
| 354 | #define INIT_PARAMS int set = *statep |
| 355 | #define UPDATE_PARAMS *statep = set |
| 356 | #include <iconv/loop.c> |
| 357 | |
| 358 | |
| 359 | /* Next, define the other direction, from UCS-4 to ISO-2022-JP-3. */ |
| 360 | |
| 361 | /* Composition tables for each of the relevant combining characters. */ |
| 362 | static const struct |
| 363 | { |
| 364 | uint16_t base; |
| 365 | uint16_t composed; |
| 366 | } comp_table_data[] = |
| 367 | { |
| 368 | #define COMP_TABLE_IDX_02E5 0 |
| 369 | #define COMP_TABLE_LEN_02E5 1 |
| 370 | { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */ |
| 371 | #define COMP_TABLE_IDX_02E9 (COMP_TABLE_IDX_02E5 + COMP_TABLE_LEN_02E5) |
| 372 | #define COMP_TABLE_LEN_02E9 1 |
| 373 | { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */ |
| 374 | #define COMP_TABLE_IDX_0300 (COMP_TABLE_IDX_02E9 + COMP_TABLE_LEN_02E9) |
| 375 | #define COMP_TABLE_LEN_0300 5 |
| 376 | { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */ |
| 377 | { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */ |
| 378 | { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */ |
| 379 | { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */ |
| 380 | { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */ |
| 381 | #define COMP_TABLE_IDX_0301 (COMP_TABLE_IDX_0300 + COMP_TABLE_LEN_0300) |
| 382 | #define COMP_TABLE_LEN_0301 4 |
| 383 | { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */ |
| 384 | { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */ |
| 385 | { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */ |
| 386 | { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */ |
| 387 | #define COMP_TABLE_IDX_309A (COMP_TABLE_IDX_0301 + COMP_TABLE_LEN_0301) |
| 388 | #define COMP_TABLE_LEN_309A 14 |
| 389 | { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */ |
| 390 | { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */ |
| 391 | { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */ |
| 392 | { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */ |
| 393 | { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */ |
| 394 | { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */ |
| 395 | { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */ |
| 396 | { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */ |
| 397 | { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */ |
| 398 | { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */ |
| 399 | { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */ |
| 400 | { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */ |
| 401 | { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */ |
| 402 | { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */ |
| 403 | }; |
| 404 | |
| 405 | #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM |
| 406 | #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM |
| 407 | #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO |
| 408 | #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO |
| 409 | #define LOOPFCT TO_LOOP |
| 410 | #define BODY \ |
| 411 | { \ |
| 412 | uint32_t ch = get32 (inptr); \ |
| 413 | \ |
| 414 | if (lasttwo != 0) \ |
| 415 | { \ |
| 416 | /* Attempt to combine the last character with this one. */ \ |
| 417 | unsigned int idx; \ |
| 418 | unsigned int len; \ |
| 419 | \ |
| 420 | if (ch == 0x02e5) \ |
| 421 | idx = COMP_TABLE_IDX_02E5, len = COMP_TABLE_LEN_02E5; \ |
| 422 | else if (ch == 0x02e9) \ |
| 423 | idx = COMP_TABLE_IDX_02E9, len = COMP_TABLE_LEN_02E9; \ |
| 424 | else if (ch == 0x0300) \ |
| 425 | idx = COMP_TABLE_IDX_0300, len = COMP_TABLE_LEN_0300; \ |
| 426 | else if (ch == 0x0301) \ |
| 427 | idx = COMP_TABLE_IDX_0301, len = COMP_TABLE_LEN_0301; \ |
| 428 | else if (ch == 0x309a) \ |
| 429 | idx = COMP_TABLE_IDX_309A, len = COMP_TABLE_LEN_309A; \ |
| 430 | else \ |
| 431 | goto not_combining; \ |
| 432 | \ |
| 433 | do \ |
| 434 | if (comp_table_data[idx].base == (uint16_t) lasttwo) \ |
| 435 | break; \ |
| 436 | while (++idx, --len > 0); \ |
| 437 | \ |
| 438 | if (len > 0) \ |
| 439 | { \ |
| 440 | /* Output the combined character. */ \ |
| 441 | /* We know the combined character is in JISX0213 plane 1, \ |
| 442 | but the buffered character may have been in JISX0208 or in \ |
| 443 | JISX0213 plane 1. */ \ |
| 444 | size_t need = \ |
| 445 | (lasttwo >> 16 \ |
| 446 | || (set != JISX0213_1_2000_set && set != JISX0213_1_2004_set) \ |
| 447 | ? 4 : 0); \ |
| 448 | \ |
| 449 | if (__glibc_unlikely (outptr + need + 2 > outend)) \ |
| 450 | { \ |
| 451 | result = __GCONV_FULL_OUTPUT; \ |
| 452 | break; \ |
| 453 | } \ |
| 454 | if (need) \ |
| 455 | { \ |
| 456 | /* But first, output the escape sequence. */ \ |
| 457 | *outptr++ = ESC; \ |
| 458 | *outptr++ = '$'; \ |
| 459 | *outptr++ = '('; \ |
| 460 | *outptr++ = 'O'; \ |
| 461 | set = JISX0213_1_2000_set; \ |
| 462 | } \ |
| 463 | lasttwo = comp_table_data[idx].composed; \ |
| 464 | *outptr++ = (lasttwo >> 8) & 0xff; \ |
| 465 | *outptr++ = lasttwo & 0xff; \ |
| 466 | lasttwo = 0; \ |
| 467 | inptr += 4; \ |
| 468 | continue; \ |
| 469 | } \ |
| 470 | \ |
| 471 | not_combining: \ |
| 472 | /* Output the buffered character. */ \ |
| 473 | /* We know it is in JISX0208 or in JISX0213 plane 1. */ \ |
| 474 | { \ |
| 475 | size_t need = (lasttwo >> 16 ? 3 : 0); \ |
| 476 | \ |
| 477 | if (__glibc_unlikely (outptr + need + 2 > outend)) \ |
| 478 | { \ |
| 479 | result = __GCONV_FULL_OUTPUT; \ |
| 480 | break; \ |
| 481 | } \ |
| 482 | if (need) \ |
| 483 | { \ |
| 484 | /* But first, output the escape sequence. */ \ |
| 485 | assert (set == JISX0208_1983_set); \ |
| 486 | *outptr++ = ESC; \ |
| 487 | *outptr++ = '$'; \ |
| 488 | *outptr++ = 'B'; \ |
| 489 | } \ |
| 490 | *outptr++ = (lasttwo >> 8) & 0xff; \ |
| 491 | *outptr++ = lasttwo & 0xff; \ |
| 492 | lasttwo = 0; \ |
| 493 | continue; \ |
| 494 | } \ |
| 495 | } \ |
| 496 | \ |
| 497 | /* First see whether we can write the character using the currently \ |
| 498 | selected character set. */ \ |
| 499 | if (set == ASCII_set) \ |
| 500 | { \ |
| 501 | /* Please note that the NUL byte is *not* matched if we are not \ |
| 502 | currently using the ASCII charset. This is because we must \ |
| 503 | switch to the initial state whenever a NUL byte is written. */ \ |
| 504 | if (ch <= 0x7f) \ |
| 505 | { \ |
| 506 | *outptr++ = ch; \ |
| 507 | inptr += 4; \ |
| 508 | continue; \ |
| 509 | } \ |
| 510 | } \ |
| 511 | /* ISO-2022-JP recommends to encode the newline character always in \ |
| 512 | ASCII since this allows a context-free interpretation of the \ |
| 513 | characters at the beginning of the next line. Otherwise it would \ |
| 514 | have to be known whether the last line ended using ASCII or \ |
| 515 | JIS X 0201. */ \ |
| 516 | else if (set == JISX0201_Roman_set) \ |
| 517 | { \ |
| 518 | unsigned char buf[1]; \ |
| 519 | if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \ |
| 520 | && buf[0] > 0x20 && buf[0] < 0x80) \ |
| 521 | { \ |
| 522 | *outptr++ = buf[0]; \ |
| 523 | inptr += 4; \ |
| 524 | continue; \ |
| 525 | } \ |
| 526 | } \ |
| 527 | else if (set == JISX0201_Kana_set) \ |
| 528 | { \ |
| 529 | unsigned char buf[1]; \ |
| 530 | if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \ |
| 531 | && buf[0] >= 0x80) \ |
| 532 | { \ |
| 533 | *outptr++ = buf[0] - 0x80; \ |
| 534 | inptr += 4; \ |
| 535 | continue; \ |
| 536 | } \ |
| 537 | } \ |
| 538 | else if (/*set == JISX0208_1978_set || */ set == JISX0208_1983_set) \ |
| 539 | { \ |
| 540 | size_t written = ucs4_to_jisx0208 (ch, outptr, outend - outptr); \ |
| 541 | \ |
| 542 | if (written != __UNKNOWN_10646_CHAR) \ |
| 543 | { \ |
| 544 | uint32_t jch = ucs4_to_jisx0213 (ch); \ |
| 545 | \ |
| 546 | if (jch & 0x0080) \ |
| 547 | { \ |
| 548 | /* A possible match in comp_table_data. Buffer it. */ \ |
| 549 | lasttwo = jch & 0x7f7f; \ |
| 550 | inptr += 4; \ |
| 551 | continue; \ |
| 552 | } \ |
| 553 | if (__glibc_unlikely (written == 0)) \ |
| 554 | { \ |
| 555 | result = __GCONV_FULL_OUTPUT; \ |
| 556 | break; \ |
| 557 | } \ |
| 558 | else \ |
| 559 | { \ |
| 560 | outptr += written; \ |
| 561 | inptr += 4; \ |
| 562 | continue; \ |
| 563 | } \ |
| 564 | } \ |
| 565 | } \ |
| 566 | else \ |
| 567 | { \ |
| 568 | /* (set == JISX0213_1_2000_set || set == JISX0213_1_2004_set \ |
| 569 | || set == JISX0213_2_set) */ \ |
| 570 | uint32_t jch = ucs4_to_jisx0213 (ch); \ |
| 571 | \ |
| 572 | if (jch != 0 \ |
| 573 | && (jch & 0x8000 \ |
| 574 | ? set == JISX0213_2_set \ |
| 575 | : (set == JISX0213_1_2004_set \ |
| 576 | || (set == JISX0213_1_2000_set \ |
| 577 | && !jisx0213_added_in_2004_p (jch))))) \ |
| 578 | { \ |
| 579 | if (jch & 0x0080) \ |
| 580 | { \ |
| 581 | /* A possible match in comp_table_data. Buffer it. */ \ |
| 582 | \ |
| 583 | /* We know it's a JISX 0213 plane 1 character. */ \ |
| 584 | assert ((jch & 0x8000) == 0); \ |
| 585 | \ |
| 586 | lasttwo = jch & 0x7f7f; \ |
| 587 | inptr += 4; \ |
| 588 | continue; \ |
| 589 | } \ |
| 590 | \ |
| 591 | if (__glibc_unlikely (outptr + 1 >= outend)) \ |
| 592 | { \ |
| 593 | result = __GCONV_FULL_OUTPUT; \ |
| 594 | break; \ |
| 595 | } \ |
| 596 | *outptr++ = (jch >> 8) & 0x7f; \ |
| 597 | *outptr++ = jch & 0x7f; \ |
| 598 | inptr += 4; \ |
| 599 | continue; \ |
| 600 | } \ |
| 601 | } \ |
| 602 | \ |
| 603 | /* The attempts to use the currently selected character set failed, \ |
| 604 | either because the character requires a different character set, \ |
| 605 | or because the character is unknown. */ \ |
| 606 | \ |
| 607 | if (ch <= 0x7f) \ |
| 608 | { \ |
| 609 | /* We must encode using ASCII. First write out the escape \ |
| 610 | sequence. */ \ |
| 611 | if (__glibc_unlikely (outptr + 3 > outend)) \ |
| 612 | { \ |
| 613 | result = __GCONV_FULL_OUTPUT; \ |
| 614 | break; \ |
| 615 | } \ |
| 616 | \ |
| 617 | *outptr++ = ESC; \ |
| 618 | *outptr++ = '('; \ |
| 619 | *outptr++ = 'B'; \ |
| 620 | set = ASCII_set; \ |
| 621 | \ |
| 622 | if (__glibc_unlikely (outptr >= outend)) \ |
| 623 | { \ |
| 624 | result = __GCONV_FULL_OUTPUT; \ |
| 625 | break; \ |
| 626 | } \ |
| 627 | *outptr++ = ch; \ |
| 628 | } \ |
| 629 | else \ |
| 630 | { \ |
| 631 | unsigned char buf[2]; \ |
| 632 | \ |
| 633 | /* Try JIS X 0201 Roman. */ \ |
| 634 | if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \ |
| 635 | && buf[0] > 0x20 && buf[0] < 0x80) \ |
| 636 | { \ |
| 637 | if (set != JISX0201_Roman_set) \ |
| 638 | { \ |
| 639 | if (__glibc_unlikely (outptr + 3 > outend)) \ |
| 640 | { \ |
| 641 | result = __GCONV_FULL_OUTPUT; \ |
| 642 | break; \ |
| 643 | } \ |
| 644 | *outptr++ = ESC; \ |
| 645 | *outptr++ = '('; \ |
| 646 | *outptr++ = 'J'; \ |
| 647 | set = JISX0201_Roman_set; \ |
| 648 | } \ |
| 649 | \ |
| 650 | if (__glibc_unlikely (outptr >= outend)) \ |
| 651 | { \ |
| 652 | result = __GCONV_FULL_OUTPUT; \ |
| 653 | break; \ |
| 654 | } \ |
| 655 | *outptr++ = buf[0]; \ |
| 656 | } \ |
| 657 | else \ |
| 658 | { \ |
| 659 | uint32_t jch = ucs4_to_jisx0213 (ch); \ |
| 660 | \ |
| 661 | /* Try JIS X 0208. */ \ |
| 662 | size_t written = ucs4_to_jisx0208 (ch, buf, 2); \ |
| 663 | if (written != __UNKNOWN_10646_CHAR) \ |
| 664 | { \ |
| 665 | if (jch & 0x0080) \ |
| 666 | { \ |
| 667 | /* A possible match in comp_table_data. Buffer it. */ \ |
| 668 | lasttwo = ((set != JISX0208_1983_set ? 1 : 0) << 16) \ |
| 669 | | (jch & 0x7f7f); \ |
| 670 | set = JISX0208_1983_set; \ |
| 671 | inptr += 4; \ |
| 672 | continue; \ |
| 673 | } \ |
| 674 | \ |
| 675 | if (set != JISX0208_1983_set) \ |
| 676 | { \ |
| 677 | if (__glibc_unlikely (outptr + 3 > outend)) \ |
| 678 | { \ |
| 679 | result = __GCONV_FULL_OUTPUT; \ |
| 680 | break; \ |
| 681 | } \ |
| 682 | *outptr++ = ESC; \ |
| 683 | *outptr++ = '$'; \ |
| 684 | *outptr++ = 'B'; \ |
| 685 | set = JISX0208_1983_set; \ |
| 686 | } \ |
| 687 | \ |
| 688 | if (__glibc_unlikely (outptr + 2 > outend)) \ |
| 689 | { \ |
| 690 | result = __GCONV_FULL_OUTPUT; \ |
| 691 | break; \ |
| 692 | } \ |
| 693 | *outptr++ = buf[0]; \ |
| 694 | *outptr++ = buf[1]; \ |
| 695 | } \ |
| 696 | else \ |
| 697 | { \ |
| 698 | /* Try JIS X 0213. */ \ |
| 699 | if (jch != 0) \ |
| 700 | { \ |
| 701 | int new_set = \ |
| 702 | (jch & 0x8000 \ |
| 703 | ? JISX0213_2_set \ |
| 704 | : jisx0213_added_in_2004_p (jch) \ |
| 705 | ? JISX0213_1_2004_set \ |
| 706 | : JISX0213_1_2000_set); \ |
| 707 | \ |
| 708 | if (set != new_set) \ |
| 709 | { \ |
| 710 | if (__glibc_unlikely (outptr + 4 > outend)) \ |
| 711 | { \ |
| 712 | result = __GCONV_FULL_OUTPUT; \ |
| 713 | break; \ |
| 714 | } \ |
| 715 | *outptr++ = ESC; \ |
| 716 | *outptr++ = '$'; \ |
| 717 | *outptr++ = '('; \ |
| 718 | *outptr++ = \ |
| 719 | ((new_set - JISX0213_1_2000_set) >> 3) + 'O'; \ |
| 720 | set = new_set; \ |
| 721 | } \ |
| 722 | \ |
| 723 | if (jch & 0x0080) \ |
| 724 | { \ |
| 725 | /* A possible match in comp_table_data. \ |
| 726 | Buffer it. */ \ |
| 727 | \ |
| 728 | /* We know it's a JIS X 0213 plane 1 character. */ \ |
| 729 | assert ((jch & 0x8000) == 0); \ |
| 730 | \ |
| 731 | lasttwo = jch & 0x7f7f; \ |
| 732 | inptr += 4; \ |
| 733 | continue; \ |
| 734 | } \ |
| 735 | \ |
| 736 | if (__glibc_unlikely (outptr + 1 >= outend)) \ |
| 737 | { \ |
| 738 | result = __GCONV_FULL_OUTPUT; \ |
| 739 | break; \ |
| 740 | } \ |
| 741 | *outptr++ = (jch >> 8) & 0x7f; \ |
| 742 | *outptr++ = jch & 0x7f; \ |
| 743 | } \ |
| 744 | else \ |
| 745 | { \ |
| 746 | /* Try JIS X 0201 Katakana. This is officially not part \ |
| 747 | of ISO-2022-JP-3. Therefore we try it after all other \ |
| 748 | attempts. */ \ |
| 749 | if (ucs4_to_jisx0201 (ch, buf) != __UNKNOWN_10646_CHAR \ |
| 750 | && buf[0] >= 0x80) \ |
| 751 | { \ |
| 752 | if (set != JISX0201_Kana_set) \ |
| 753 | { \ |
| 754 | if (__builtin_expect (outptr + 3 > outend, 0)) \ |
| 755 | { \ |
| 756 | result = __GCONV_FULL_OUTPUT; \ |
| 757 | break; \ |
| 758 | } \ |
| 759 | *outptr++ = ESC; \ |
| 760 | *outptr++ = '('; \ |
| 761 | *outptr++ = 'I'; \ |
| 762 | set = JISX0201_Kana_set; \ |
| 763 | } \ |
| 764 | \ |
| 765 | if (__glibc_unlikely (outptr >= outend)) \ |
| 766 | { \ |
| 767 | result = __GCONV_FULL_OUTPUT; \ |
| 768 | break; \ |
| 769 | } \ |
| 770 | *outptr++ = buf[0] - 0x80; \ |
| 771 | } \ |
| 772 | else \ |
| 773 | { \ |
| 774 | UNICODE_TAG_HANDLER (ch, 4); \ |
| 775 | \ |
| 776 | /* Illegal character. */ \ |
| 777 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
| 778 | } \ |
| 779 | } \ |
| 780 | } \ |
| 781 | } \ |
| 782 | } \ |
| 783 | \ |
| 784 | /* Now that we wrote the output increment the input pointer. */ \ |
| 785 | inptr += 4; \ |
| 786 | } |
| 787 | #define LOOP_NEED_FLAGS |
| 788 | #define , int *statep |
| 789 | #define INIT_PARAMS int set = *statep & CURRENT_SEL_MASK; \ |
| 790 | uint32_t lasttwo = *statep >> 6 |
| 791 | #define REINIT_PARAMS do \ |
| 792 | { \ |
| 793 | set = *statep & CURRENT_SEL_MASK; \ |
| 794 | lasttwo = *statep >> 6; \ |
| 795 | } \ |
| 796 | while (0) |
| 797 | #define UPDATE_PARAMS *statep = set | (lasttwo << 6) |
| 798 | #include <iconv/loop.c> |
| 799 | |
| 800 | |
| 801 | /* Now define the toplevel functions. */ |
| 802 | #include <iconv/skeleton.c> |
| 803 | |