| 1 | /* Conversion module for ISO-2022-CN-EXT. | 
|---|
| 2 | Copyright (C) 2000-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. | 
|---|
| 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 <gconv.h> | 
|---|
| 22 | #include <stdint.h> | 
|---|
| 23 | #include <stdlib.h> | 
|---|
| 24 | #include <string.h> | 
|---|
| 25 | #include "gb2312.h" | 
|---|
| 26 | #include "iso-ir-165.h" | 
|---|
| 27 | #include "cns11643.h" | 
|---|
| 28 | #include "cns11643l1.h" | 
|---|
| 29 | #include "cns11643l2.h" | 
|---|
| 30 | #include <libc-diag.h> | 
|---|
| 31 |  | 
|---|
| 32 | #include <assert.h> | 
|---|
| 33 |  | 
|---|
| 34 | /* This makes obvious what everybody knows: 0x1b is the Esc character.  */ | 
|---|
| 35 | #define ESC	0x1b | 
|---|
| 36 |  | 
|---|
| 37 | /* We have single-byte shift-in and shift-out sequences, and the single | 
|---|
| 38 | shift sequences SS2 and SS3 which replaces the SS2/SS3 designation for | 
|---|
| 39 | the next two bytes.  */ | 
|---|
| 40 | #define SI	0x0f | 
|---|
| 41 | #define SO	0x0e | 
|---|
| 42 | #define SS2_0	ESC | 
|---|
| 43 | #define SS2_1	0x4e | 
|---|
| 44 | #define SS3_0	ESC | 
|---|
| 45 | #define SS3_1	0x4f | 
|---|
| 46 |  | 
|---|
| 47 | /* Definitions used in the body of the `gconv' function.  */ | 
|---|
| 48 | #define CHARSET_NAME		"ISO-2022-CN-EXT//" | 
|---|
| 49 | #define DEFINE_INIT		1 | 
|---|
| 50 | #define DEFINE_FINI		1 | 
|---|
| 51 | #define ONE_DIRECTION		0 | 
|---|
| 52 | #define FROM_LOOP		from_iso2022cn_ext_loop | 
|---|
| 53 | #define TO_LOOP			to_iso2022cn_ext_loop | 
|---|
| 54 | #define FROM_LOOP_MIN_NEEDED_FROM	1 | 
|---|
| 55 | #define FROM_LOOP_MAX_NEEDED_FROM	4 | 
|---|
| 56 | #define FROM_LOOP_MIN_NEEDED_TO		4 | 
|---|
| 57 | #define FROM_LOOP_MAX_NEEDED_TO		4 | 
|---|
| 58 | #define TO_LOOP_MIN_NEEDED_FROM		4 | 
|---|
| 59 | #define TO_LOOP_MAX_NEEDED_FROM		4 | 
|---|
| 60 | #define TO_LOOP_MIN_NEEDED_TO		1 | 
|---|
| 61 | #define TO_LOOP_MAX_NEEDED_TO		6 | 
|---|
| 62 | #define PREPARE_LOOP \ | 
|---|
| 63 | int save_set;								      \ | 
|---|
| 64 | int *setp = &data->__statep->__count; | 
|---|
| 65 | #define 		, setp | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | /* The charsets GB/T 12345-90, GB 7589-87, GB/T 13131-9X, GB 7590-87, | 
|---|
| 69 | and GB/T 13132-9X are not registered to the best of my knowledge and | 
|---|
| 70 | therefore have no escape sequence assigned.  We cannot handle them | 
|---|
| 71 | for this reason.  Tell the implementation about this.  */ | 
|---|
| 72 | #define X12345	'\0' | 
|---|
| 73 | #define X7589	'\0' | 
|---|
| 74 | #define X13131	'\0' | 
|---|
| 75 | #define X7590	'\0' | 
|---|
| 76 | #define X13132	'\0' | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | /* The COUNT element of the state keeps track of the currently selected | 
|---|
| 80 | character set.  The possible values are:  */ | 
|---|
| 81 | enum | 
|---|
| 82 | { | 
|---|
| 83 | ASCII_set = 0, | 
|---|
| 84 | GB2312_set, | 
|---|
| 85 | GB12345_set, | 
|---|
| 86 | CNS11643_1_set, | 
|---|
| 87 | ISO_IR_165_set, | 
|---|
| 88 | SO_mask = 7, | 
|---|
| 89 |  | 
|---|
| 90 | GB7589_set = 1 << 3, | 
|---|
| 91 | GB13131_set = 2 << 3, | 
|---|
| 92 | CNS11643_2_set = 3 << 3, | 
|---|
| 93 | SS2_mask = 3 << 3, | 
|---|
| 94 |  | 
|---|
| 95 | GB7590_set = 1 << 5, | 
|---|
| 96 | GB13132_set = 2 << 5, | 
|---|
| 97 | CNS11643_3_set = 3 << 5, | 
|---|
| 98 | CNS11643_4_set = 4 << 5, | 
|---|
| 99 | CNS11643_5_set = 5 << 5, | 
|---|
| 100 | CNS11643_6_set = 6 << 5, | 
|---|
| 101 | CNS11643_7_set = 7 << 5, | 
|---|
| 102 | SS3_mask = 7 << 5, | 
|---|
| 103 |  | 
|---|
| 104 | #define CURRENT_MASK (SO_mask | SS2_mask | SS3_mask) | 
|---|
| 105 |  | 
|---|
| 106 | GB2312_ann = 1 << 8, | 
|---|
| 107 | GB12345_ann = 2 << 8, | 
|---|
| 108 | CNS11643_1_ann = 3 << 8, | 
|---|
| 109 | ISO_IR_165_ann = 4 << 8, | 
|---|
| 110 | SO_ann = 7 << 8, | 
|---|
| 111 |  | 
|---|
| 112 | GB7589_ann = 1 << 11, | 
|---|
| 113 | GB13131_ann = 2 << 11, | 
|---|
| 114 | CNS11643_2_ann = 3 << 11, | 
|---|
| 115 | SS2_ann = 3 << 11, | 
|---|
| 116 |  | 
|---|
| 117 | GB7590_ann = 1 << 13, | 
|---|
| 118 | GB13132_ann = 2 << 13, | 
|---|
| 119 | CNS11643_3_ann = 3 << 13, | 
|---|
| 120 | CNS11643_4_ann = 4 << 13, | 
|---|
| 121 | CNS11643_5_ann = 5 << 13, | 
|---|
| 122 | CNS11643_6_ann = 6 << 13, | 
|---|
| 123 | CNS11643_7_ann = 7 << 13, | 
|---|
| 124 | SS3_ann = 7 << 13 | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | /* Since this is a stateful encoding we have to provide code which resets | 
|---|
| 129 | the output state to the initial state.  This has to be done during the | 
|---|
| 130 | flushing.  */ | 
|---|
| 131 | #define EMIT_SHIFT_TO_INIT \ | 
|---|
| 132 | if (data->__statep->__count >> 3 != ASCII_set)			      \ | 
|---|
| 133 | {									      \ | 
|---|
| 134 | if (FROM_DIRECTION)						      \ | 
|---|
| 135 | /* It's easy, we don't have to emit anything, we just reset the	      \ | 
|---|
| 136 | state for the input.  */					      \ | 
|---|
| 137 | data->__statep->__count = ASCII_set << 3;			      \ | 
|---|
| 138 | else								      \ | 
|---|
| 139 | {								      \ | 
|---|
| 140 | /* We are not in the initial state.  To switch back we have	      \ | 
|---|
| 141 | to emit `SI'.  */						      \ | 
|---|
| 142 | if (__glibc_unlikely (outbuf == outend))			      \ | 
|---|
| 143 | /* We don't have enough room in the output buffer.  */	      \ | 
|---|
| 144 | status = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 145 | else								      \ | 
|---|
| 146 | {								      \ | 
|---|
| 147 | /* Write out the shift sequence.  */			      \ | 
|---|
| 148 | *outbuf++ = SI;						      \ | 
|---|
| 149 | if (data->__flags & __GCONV_IS_LAST)			      \ | 
|---|
| 150 | *irreversible += 1;					      \ | 
|---|
| 151 | data->__statep->__count = ASCII_set << 3;			      \ | 
|---|
| 152 | }								      \ | 
|---|
| 153 | }								      \ | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | /* Since we might have to reset input pointer we must be able to save | 
|---|
| 158 | and retore the state.  */ | 
|---|
| 159 | #define SAVE_RESET_STATE(Save) \ | 
|---|
| 160 | if (Save)								      \ | 
|---|
| 161 | save_set = *setp;							      \ | 
|---|
| 162 | else									      \ | 
|---|
| 163 | *setp = save_set | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|
| 166 | /* First define the conversion function from ISO-2022-CN to UCS4.  */ | 
|---|
| 167 | #define MIN_NEEDED_INPUT	FROM_LOOP_MIN_NEEDED_FROM | 
|---|
| 168 | #define MAX_NEEDED_INPUT	FROM_LOOP_MAX_NEEDED_FROM | 
|---|
| 169 | #define MIN_NEEDED_OUTPUT	FROM_LOOP_MIN_NEEDED_TO | 
|---|
| 170 | #define MAX_NEEDED_OUTPUT	FROM_LOOP_MAX_NEEDED_TO | 
|---|
| 171 | #define LOOPFCT			FROM_LOOP | 
|---|
| 172 | #define BODY \ | 
|---|
| 173 | {									      \ | 
|---|
| 174 | uint32_t ch = *inptr;						      \ | 
|---|
| 175 | \ | 
|---|
| 176 | /* This is a 7bit character set, disallow all 8bit characters.  */	      \ | 
|---|
| 177 | if (ch > 0x7f)							      \ | 
|---|
| 178 | STANDARD_FROM_LOOP_ERR_HANDLER (1);				      \ | 
|---|
| 179 | \ | 
|---|
| 180 | /* Recognize escape sequences.  */					      \ | 
|---|
| 181 | if (ch == ESC)							      \ | 
|---|
| 182 | {									      \ | 
|---|
| 183 | /* There are three kinds of escape sequences we have to handle:	      \ | 
|---|
| 184 | - those announcing the use of GB and CNS characters on the	      \ | 
|---|
| 185 | line; we can simply ignore them				      \ | 
|---|
| 186 | - the initial byte of the SS2 sequence.			      \ | 
|---|
| 187 | - the initial byte of the SS3 sequence.			      \ | 
|---|
| 188 | */								      \ | 
|---|
| 189 | if (inptr + 2 > inend						      \ | 
|---|
| 190 | || (inptr[1] == '$'						      \ | 
|---|
| 191 | && (inptr + 3 > inend					      \ | 
|---|
| 192 | || (inptr[2] == ')' && inptr + 4 > inend)		      \ | 
|---|
| 193 | || (inptr[2] == '*' && inptr + 4 > inend)		      \ | 
|---|
| 194 | || (inptr[2] == '+' && inptr + 4 > inend)))		      \ | 
|---|
| 195 | || (inptr[1] == SS2_1 && inptr + 4 > inend)			      \ | 
|---|
| 196 | || (inptr[1] == SS3_1 && inptr + 4 > inend))		      \ | 
|---|
| 197 | {								      \ | 
|---|
| 198 | result = __GCONV_INCOMPLETE_INPUT;				      \ | 
|---|
| 199 | break;							      \ | 
|---|
| 200 | }								      \ | 
|---|
| 201 | if (inptr[1] == '$'						      \ | 
|---|
| 202 | && ((inptr[2] == ')'					      \ | 
|---|
| 203 | && (inptr[3] == 'A'					      \ | 
|---|
| 204 | || (X12345 != '\0' && inptr[3] == X12345)		      \ | 
|---|
| 205 | || inptr[3] == 'E' || inptr[3] == 'G'))		      \ | 
|---|
| 206 | || (inptr[2] == '*'					      \ | 
|---|
| 207 | && ((X7589 != '\0' && inptr[3] == X7589)		      \ | 
|---|
| 208 | || (X13131 != '\0' && inptr[3] == X13131)	      \ | 
|---|
| 209 | || inptr[3] == 'H'))				      \ | 
|---|
| 210 | || (inptr[2] == '+'					      \ | 
|---|
| 211 | && ((X7590 != '\0' && inptr[3] == X7590)		      \ | 
|---|
| 212 | || (X13132 != '\0' && inptr[3] == X13132)	      \ | 
|---|
| 213 | || inptr[3] == 'I' || inptr[3] == 'J'		      \ | 
|---|
| 214 | || inptr[3] == 'K' || inptr[3] == 'L'		      \ | 
|---|
| 215 | || inptr[3] == 'M'))))				      \ | 
|---|
| 216 | {								      \ | 
|---|
| 217 | /* OK, we accept those character sets.  */			      \ | 
|---|
| 218 | if (inptr[3] == 'A')					      \ | 
|---|
| 219 | ann = (ann & ~SO_ann) | GB2312_ann;			      \ | 
|---|
| 220 | else if (inptr[3] == 'G')					      \ | 
|---|
| 221 | ann = (ann & ~SO_ann) | CNS11643_1_ann;			      \ | 
|---|
| 222 | else if (inptr[3] == 'E')					      \ | 
|---|
| 223 | ann = (ann & ~SO_ann) | ISO_IR_165_ann;			      \ | 
|---|
| 224 | else if (X12345 != '\0' && inptr[3] == X12345)		      \ | 
|---|
| 225 | ann = (ann & ~SO_ann) | GB12345_ann;			      \ | 
|---|
| 226 | else if (inptr[3] == 'H')					      \ | 
|---|
| 227 | ann = (ann & ~SS2_ann) | CNS11643_2_ann;			      \ | 
|---|
| 228 | else if (X7589 != '\0' && inptr[3] == X7589)		      \ | 
|---|
| 229 | ann = (ann & ~SS2_ann) | GB7589_ann;			      \ | 
|---|
| 230 | else if (X13131 != '\0' && inptr[3] == X13131)		      \ | 
|---|
| 231 | ann = (ann & ~SS2_ann) | GB13131_ann;			      \ | 
|---|
| 232 | else if (inptr[3] == 'I')					      \ | 
|---|
| 233 | ann = (ann & ~SS3_ann) | CNS11643_3_ann;			      \ | 
|---|
| 234 | else if (inptr[3] == 'J')					      \ | 
|---|
| 235 | ann = (ann & ~SS3_ann) | CNS11643_4_ann;			      \ | 
|---|
| 236 | else if (inptr[3] == 'K')					      \ | 
|---|
| 237 | ann = (ann & ~SS3_ann) | CNS11643_5_ann;			      \ | 
|---|
| 238 | else if (inptr[3] == 'L')					      \ | 
|---|
| 239 | ann = (ann & ~SS3_ann) | CNS11643_6_ann;			      \ | 
|---|
| 240 | else if (inptr[3] == 'M')					      \ | 
|---|
| 241 | ann = (ann & ~SS3_ann) | CNS11643_7_ann;			      \ | 
|---|
| 242 | else if (X7590 != '\0' && inptr[3] == X7590)		      \ | 
|---|
| 243 | ann = (ann & ~SS3_ann) | GB7590_ann;			      \ | 
|---|
| 244 | else if (X13132 != '\0' && inptr[3] == X13132)		      \ | 
|---|
| 245 | ann = (ann & ~SS3_ann) | GB13132_ann;			      \ | 
|---|
| 246 | inptr += 4;							      \ | 
|---|
| 247 | continue;							      \ | 
|---|
| 248 | }								      \ | 
|---|
| 249 | }									      \ | 
|---|
| 250 | else if (ch == SO)							      \ | 
|---|
| 251 | {									      \ | 
|---|
| 252 | /* Switch to use GB2312, GB12345, CNS 11643 plane 1, or ISO-IR-165,   \ | 
|---|
| 253 | depending on which S0 designation came last.  The only problem     \ | 
|---|
| 254 | is what to do with faulty input files where no designator came.    \ | 
|---|
| 255 | XXX For now I'll default to use GB2312.  If this is not the	      \ | 
|---|
| 256 | best behavior (e.g., we should flag an error) let me know.  */     \ | 
|---|
| 257 | ++inptr;							      \ | 
|---|
| 258 | if ((ann & SO_ann) != 0)					      \ | 
|---|
| 259 | switch (ann & SO_ann)						      \ | 
|---|
| 260 | {								      \ | 
|---|
| 261 | case GB2312_ann:						      \ | 
|---|
| 262 | set = GB2312_set;						      \ | 
|---|
| 263 | break;							      \ | 
|---|
| 264 | case GB12345_ann:						      \ | 
|---|
| 265 | set = GB12345_set;					      \ | 
|---|
| 266 | break;							      \ | 
|---|
| 267 | case CNS11643_1_ann:					      \ | 
|---|
| 268 | set = CNS11643_1_set;					      \ | 
|---|
| 269 | break;							      \ | 
|---|
| 270 | case ISO_IR_165_ann:					      \ | 
|---|
| 271 | set = ISO_IR_165_set;					      \ | 
|---|
| 272 | break;							      \ | 
|---|
| 273 | default:							      \ | 
|---|
| 274 | abort ();							      \ | 
|---|
| 275 | }								      \ | 
|---|
| 276 | else								      \ | 
|---|
| 277 | {								      \ | 
|---|
| 278 | STANDARD_FROM_LOOP_ERR_HANDLER (1);				      \ | 
|---|
| 279 | }								      \ | 
|---|
| 280 | continue;							      \ | 
|---|
| 281 | }									      \ | 
|---|
| 282 | else if (ch == SI)							      \ | 
|---|
| 283 | {									      \ | 
|---|
| 284 | /* Switch to use ASCII.  */					      \ | 
|---|
| 285 | ++inptr;							      \ | 
|---|
| 286 | set = ASCII_set;						      \ | 
|---|
| 287 | continue;							      \ | 
|---|
| 288 | }									      \ | 
|---|
| 289 | \ | 
|---|
| 290 | if (ch == ESC && inptr[1] == SS2_1)					      \ | 
|---|
| 291 | {									      \ | 
|---|
| 292 | /* This is a character from CNS 11643 plane 2.			      \ | 
|---|
| 293 | XXX We could test here whether the use of this character	      \ | 
|---|
| 294 | set was announced.						      \ | 
|---|
| 295 | XXX Currently GB7589 and GB13131 are not supported.  */	      \ | 
|---|
| 296 | inptr += 2;							      \ | 
|---|
| 297 | ch = cns11643l2_to_ucs4 (&inptr, 2, 0);				      \ | 
|---|
| 298 | if (ch == __UNKNOWN_10646_CHAR)					      \ | 
|---|
| 299 | STANDARD_FROM_LOOP_ERR_HANDLER (2);				      \ | 
|---|
| 300 | }									      \ | 
|---|
| 301 | /* Note that we can assume here that at least 4 bytes are available if    \ | 
|---|
| 302 | the first byte is ESC since otherwise the first if would have been     \ | 
|---|
| 303 | true.  */							      \ | 
|---|
| 304 | else if (ch == ESC && inptr[1] == SS3_1)				      \ | 
|---|
| 305 | {									      \ | 
|---|
| 306 | /* This is a character from CNS 11643 plane 3 or higher.	      \ | 
|---|
| 307 | XXX Currently GB7590 and GB13132 are not supported.  */	      \ | 
|---|
| 308 | unsigned char buf[3];						      \ | 
|---|
| 309 | const unsigned char *tmp = buf;					      \ | 
|---|
| 310 | \ | 
|---|
| 311 | buf[1] = inptr[2];						      \ | 
|---|
| 312 | buf[2] = inptr[3];						      \ | 
|---|
| 313 | switch (ann & SS3_ann)						      \ | 
|---|
| 314 | {								      \ | 
|---|
| 315 | case CNS11643_3_ann:						      \ | 
|---|
| 316 | buf[0] = 0x23;						      \ | 
|---|
| 317 | ch = cns11643_to_ucs4 (&tmp, 3, 0);				      \ | 
|---|
| 318 | break;							      \ | 
|---|
| 319 | case CNS11643_4_ann:						      \ | 
|---|
| 320 | buf[0] = 0x24;						      \ | 
|---|
| 321 | ch = cns11643_to_ucs4 (&tmp, 3, 0);				      \ | 
|---|
| 322 | break;							      \ | 
|---|
| 323 | case CNS11643_5_ann:						      \ | 
|---|
| 324 | buf[0] = 0x25;						      \ | 
|---|
| 325 | ch = cns11643_to_ucs4 (&tmp, 3, 0);				      \ | 
|---|
| 326 | break;							      \ | 
|---|
| 327 | case CNS11643_6_ann:						      \ | 
|---|
| 328 | buf[0] = 0x26;						      \ | 
|---|
| 329 | ch = cns11643_to_ucs4 (&tmp, 3, 0);				      \ | 
|---|
| 330 | break;							      \ | 
|---|
| 331 | case CNS11643_7_ann:						      \ | 
|---|
| 332 | buf[0] = 0x27;						      \ | 
|---|
| 333 | ch = cns11643_to_ucs4 (&tmp, 3, 0);				      \ | 
|---|
| 334 | break;							      \ | 
|---|
| 335 | default:							      \ | 
|---|
| 336 | /* XXX Currently GB7590 and GB13132 are not supported.  */	      \ | 
|---|
| 337 | ch = __UNKNOWN_10646_CHAR;					      \ | 
|---|
| 338 | break;							      \ | 
|---|
| 339 | }								      \ | 
|---|
| 340 | if (ch == __UNKNOWN_10646_CHAR)					      \ | 
|---|
| 341 | {								      \ | 
|---|
| 342 | STANDARD_FROM_LOOP_ERR_HANDLER (4);				      \ | 
|---|
| 343 | }								      \ | 
|---|
| 344 | assert (tmp == buf + 3);					      \ | 
|---|
| 345 | inptr += 4;							      \ | 
|---|
| 346 | }									      \ | 
|---|
| 347 | else if (set == ASCII_set)						      \ | 
|---|
| 348 | {									      \ | 
|---|
| 349 | /* Almost done, just advance the input pointer.  */		      \ | 
|---|
| 350 | ++inptr;							      \ | 
|---|
| 351 | }									      \ | 
|---|
| 352 | else								      \ | 
|---|
| 353 | {									      \ | 
|---|
| 354 | /* That's pretty easy, we have a dedicated functions for this.  */    \ | 
|---|
| 355 | if (inend - inptr < 2)						      \ | 
|---|
| 356 | {								      \ | 
|---|
| 357 | result = __GCONV_INCOMPLETE_INPUT;				      \ | 
|---|
| 358 | break;							      \ | 
|---|
| 359 | }								      \ | 
|---|
| 360 | if (set == GB2312_set)						      \ | 
|---|
| 361 | ch = gb2312_to_ucs4 (&inptr, inend - inptr, 0);		      \ | 
|---|
| 362 | else if (set == ISO_IR_165_set)					      \ | 
|---|
| 363 | ch = isoir165_to_ucs4 (&inptr, inend - inptr);		      \ | 
|---|
| 364 | else								      \ | 
|---|
| 365 | {								      \ | 
|---|
| 366 | assert (set == CNS11643_1_set);				      \ | 
|---|
| 367 | ch = cns11643l1_to_ucs4 (&inptr, inend - inptr, 0);		      \ | 
|---|
| 368 | }								      \ | 
|---|
| 369 | \ | 
|---|
| 370 | if (ch == 0)							      \ | 
|---|
| 371 | {								      \ | 
|---|
| 372 | result = __GCONV_INCOMPLETE_INPUT;				      \ | 
|---|
| 373 | break;							      \ | 
|---|
| 374 | }								      \ | 
|---|
| 375 | else if (ch == __UNKNOWN_10646_CHAR)				      \ | 
|---|
| 376 | {								      \ | 
|---|
| 377 | STANDARD_FROM_LOOP_ERR_HANDLER (2);				      \ | 
|---|
| 378 | }								      \ | 
|---|
| 379 | }									      \ | 
|---|
| 380 | \ | 
|---|
| 381 | *((uint32_t *) outptr) = ch;					      \ | 
|---|
| 382 | outptr += sizeof (uint32_t);					      \ | 
|---|
| 383 | } | 
|---|
| 384 | #define 	, int *setp | 
|---|
| 385 | #define INIT_PARAMS		int set = (*setp >> 3) & CURRENT_MASK; \ | 
|---|
| 386 | int ann = (*setp >> 3) & ~CURRENT_MASK | 
|---|
| 387 | #define UPDATE_PARAMS		*setp = (set | ann) << 3 | 
|---|
| 388 | #define LOOP_NEED_FLAGS | 
|---|
| 389 | #include <iconv/loop.c> | 
|---|
| 390 |  | 
|---|
| 391 |  | 
|---|
| 392 | /* Next, define the other direction.  */ | 
|---|
| 393 | #define MIN_NEEDED_INPUT	TO_LOOP_MIN_NEEDED_FROM | 
|---|
| 394 | #define MAX_NEEDED_INPUT	TO_LOOP_MAX_NEEDED_FROM | 
|---|
| 395 | #define MIN_NEEDED_OUTPUT	TO_LOOP_MIN_NEEDED_TO | 
|---|
| 396 | #define MAX_NEEDED_OUTPUT	TO_LOOP_MAX_NEEDED_TO | 
|---|
| 397 | #define LOOPFCT			TO_LOOP | 
|---|
| 398 | /* With GCC 5.3 when compiling with -Os the compiler emits a warning | 
|---|
| 399 | that buf[0] and buf[1] may be used uninitialized.  This can only | 
|---|
| 400 | happen in the case where tmpbuf[3] is used, and in that case the | 
|---|
| 401 | write to the tmpbuf[1] and tmpbuf[2] was assured because | 
|---|
| 402 | ucs4_to_cns11643 would have filled in those entries.  The difficulty | 
|---|
| 403 | is in getting the compiler to see this logic because tmpbuf[0] is | 
|---|
| 404 | involved in determining the code page and is the indicator that | 
|---|
| 405 | tmpbuf[2] is initialized.  */ | 
|---|
| 406 | DIAG_PUSH_NEEDS_COMMENT; | 
|---|
| 407 | DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized"); | 
|---|
| 408 | #define BODY \ | 
|---|
| 409 | {									      \ | 
|---|
| 410 | uint32_t ch;							      \ | 
|---|
| 411 | size_t written = 0;							      \ | 
|---|
| 412 | \ | 
|---|
| 413 | ch = *((const uint32_t *) inptr);					      \ | 
|---|
| 414 | \ | 
|---|
| 415 | /* First see whether we can write the character using the currently	      \ | 
|---|
| 416 | selected character set.  */					      \ | 
|---|
| 417 | if (ch < 0x80)							      \ | 
|---|
| 418 | {									      \ | 
|---|
| 419 | if (set != ASCII_set)						      \ | 
|---|
| 420 | {								      \ | 
|---|
| 421 | *outptr++ = SI;						      \ | 
|---|
| 422 | set = ASCII_set;						      \ | 
|---|
| 423 | if (outptr == outend)					      \ | 
|---|
| 424 | {								      \ | 
|---|
| 425 | result = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 426 | break;							      \ | 
|---|
| 427 | }								      \ | 
|---|
| 428 | }								      \ | 
|---|
| 429 | \ | 
|---|
| 430 | *outptr++ = ch;							      \ | 
|---|
| 431 | written = 1;							      \ | 
|---|
| 432 | \ | 
|---|
| 433 | /* At the end of the line we have to clear the `ann' flags since      \ | 
|---|
| 434 | every line must contain this information again.  */		      \ | 
|---|
| 435 | if (ch == L'\n')						      \ | 
|---|
| 436 | ann = 0;							      \ | 
|---|
| 437 | }									      \ | 
|---|
| 438 | else								      \ | 
|---|
| 439 | {									      \ | 
|---|
| 440 | unsigned char buf[2] = { 0, 0 };				      \ | 
|---|
| 441 | int used;							      \ | 
|---|
| 442 | \ | 
|---|
| 443 | if (set == GB2312_set || ((ann & SO_ann) != CNS11643_1_ann	      \ | 
|---|
| 444 | && (ann & SO_ann) != ISO_IR_165_ann))	      \ | 
|---|
| 445 | {								      \ | 
|---|
| 446 | written = ucs4_to_gb2312 (ch, buf, 2);			      \ | 
|---|
| 447 | used = GB2312_set;						      \ | 
|---|
| 448 | }								      \ | 
|---|
| 449 | else if (set == ISO_IR_165_set || (ann & SO_ann) == ISO_IR_165_set)   \ | 
|---|
| 450 | {								      \ | 
|---|
| 451 | written = ucs4_to_isoir165 (ch, buf, 2);			      \ | 
|---|
| 452 | used = ISO_IR_165_set;					      \ | 
|---|
| 453 | }								      \ | 
|---|
| 454 | else								      \ | 
|---|
| 455 | {								      \ | 
|---|
| 456 | written = ucs4_to_cns11643l1 (ch, buf, 2);			      \ | 
|---|
| 457 | used = CNS11643_1_set;					      \ | 
|---|
| 458 | }								      \ | 
|---|
| 459 | \ | 
|---|
| 460 | if (written == __UNKNOWN_10646_CHAR)				      \ | 
|---|
| 461 | {								      \ | 
|---|
| 462 | /* Cannot convert it using the currently selected SO set.	      \ | 
|---|
| 463 | Next try the SS2 set.  */				      \ | 
|---|
| 464 | written = ucs4_to_cns11643l2 (ch, buf, 2);			      \ | 
|---|
| 465 | if (written != __UNKNOWN_10646_CHAR)			      \ | 
|---|
| 466 | /* Yep, that worked.  */					      \ | 
|---|
| 467 | used = CNS11643_2_set;					      \ | 
|---|
| 468 | else							      \ | 
|---|
| 469 | {								      \ | 
|---|
| 470 | unsigned char tmpbuf[3];				      \ | 
|---|
| 471 | \ | 
|---|
| 472 | switch (0)						      \ | 
|---|
| 473 | {							      \ | 
|---|
| 474 | default:						      \ | 
|---|
| 475 | /* Well, see whether we have to change the SO set.  */    \ | 
|---|
| 476 | \ | 
|---|
| 477 | if (used != GB2312_set)				      \ | 
|---|
| 478 | {							      \ | 
|---|
| 479 | written = ucs4_to_gb2312 (ch, buf, 2);		      \ | 
|---|
| 480 | if (written != __UNKNOWN_10646_CHAR)		      \ | 
|---|
| 481 | {						      \ | 
|---|
| 482 | used = GB2312_set;				      \ | 
|---|
| 483 | break;					      \ | 
|---|
| 484 | }						      \ | 
|---|
| 485 | }							      \ | 
|---|
| 486 | \ | 
|---|
| 487 | if (used != ISO_IR_165_set)				      \ | 
|---|
| 488 | {							      \ | 
|---|
| 489 | written = ucs4_to_isoir165 (ch, buf, 2);	      \ | 
|---|
| 490 | if (written != __UNKNOWN_10646_CHAR)		      \ | 
|---|
| 491 | {						      \ | 
|---|
| 492 | used = ISO_IR_165_set;			      \ | 
|---|
| 493 | break;					      \ | 
|---|
| 494 | }						      \ | 
|---|
| 495 | }							      \ | 
|---|
| 496 | \ | 
|---|
| 497 | if (used != CNS11643_1_set)				      \ | 
|---|
| 498 | {							      \ | 
|---|
| 499 | written = ucs4_to_cns11643l1 (ch, buf, 2);	      \ | 
|---|
| 500 | if (written != __UNKNOWN_10646_CHAR)		      \ | 
|---|
| 501 | {						      \ | 
|---|
| 502 | used = CNS11643_1_set;			      \ | 
|---|
| 503 | break;					      \ | 
|---|
| 504 | }						      \ | 
|---|
| 505 | }							      \ | 
|---|
| 506 | \ | 
|---|
| 507 | written = ucs4_to_cns11643 (ch, tmpbuf, 3);		      \ | 
|---|
| 508 | if (written == 3 && tmpbuf[0] >= 3 && tmpbuf[0] <= 7)     \ | 
|---|
| 509 | {							      \ | 
|---|
| 510 | buf[0] = tmpbuf[1];				      \ | 
|---|
| 511 | buf[1] = tmpbuf[2];				      \ | 
|---|
| 512 | switch (tmpbuf[0])				      \ | 
|---|
| 513 | {						      \ | 
|---|
| 514 | case 3:					      \ | 
|---|
| 515 | used = CNS11643_3_set;			      \ | 
|---|
| 516 | break;					      \ | 
|---|
| 517 | case 4:					      \ | 
|---|
| 518 | used = CNS11643_4_set;			      \ | 
|---|
| 519 | break;					      \ | 
|---|
| 520 | case 5:					      \ | 
|---|
| 521 | used = CNS11643_5_set;			      \ | 
|---|
| 522 | break;					      \ | 
|---|
| 523 | case 6:					      \ | 
|---|
| 524 | used = CNS11643_6_set;			      \ | 
|---|
| 525 | break;					      \ | 
|---|
| 526 | case 7:					      \ | 
|---|
| 527 | used = CNS11643_7_set;			      \ | 
|---|
| 528 | break;					      \ | 
|---|
| 529 | default:					      \ | 
|---|
| 530 | abort ();					      \ | 
|---|
| 531 | }						      \ | 
|---|
| 532 | written = 2;					      \ | 
|---|
| 533 | break;						      \ | 
|---|
| 534 | }							      \ | 
|---|
| 535 | \ | 
|---|
| 536 | /* XXX Currently GB7590 and GB13132 are not supported.  */\ | 
|---|
| 537 | \ | 
|---|
| 538 | /* Even this does not work.  Error.  */		      \ | 
|---|
| 539 | used = ASCII_set;					      \ | 
|---|
| 540 | }							      \ | 
|---|
| 541 | if (used == ASCII_set)					      \ | 
|---|
| 542 | {							      \ | 
|---|
| 543 | UNICODE_TAG_HANDLER (ch, 4);			      \ | 
|---|
| 544 | STANDARD_TO_LOOP_ERR_HANDLER (4);			      \ | 
|---|
| 545 | }							      \ | 
|---|
| 546 | }								      \ | 
|---|
| 547 | }								      \ | 
|---|
| 548 | assert (written == 2);						      \ | 
|---|
| 549 | \ | 
|---|
| 550 | /* See whether we have to emit an escape sequence.  */		      \ | 
|---|
| 551 | if (set != used)						      \ | 
|---|
| 552 | {								      \ | 
|---|
| 553 | /* First see whether we announced that we use this		      \ | 
|---|
| 554 | character set.  */					      \ | 
|---|
| 555 | if ((used & SO_mask) != 0 && (ann & SO_ann) != (used << 8))	      \ | 
|---|
| 556 | {								      \ | 
|---|
| 557 | const char *escseq;					      \ | 
|---|
| 558 | \ | 
|---|
| 559 | if (outptr + 4 > outend)				      \ | 
|---|
| 560 | {							      \ | 
|---|
| 561 | result = __GCONV_FULL_OUTPUT;			      \ | 
|---|
| 562 | break;						      \ | 
|---|
| 563 | }							      \ | 
|---|
| 564 | \ | 
|---|
| 565 | assert (used >= 1 && used <= 4);			      \ | 
|---|
| 566 | escseq = ")A\0\0)G)E" + (used - 1) * 2;			      \ | 
|---|
| 567 | *outptr++ = ESC;					      \ | 
|---|
| 568 | *outptr++ = '$';					      \ | 
|---|
| 569 | *outptr++ = *escseq++;					      \ | 
|---|
| 570 | *outptr++ = *escseq++;					      \ | 
|---|
| 571 | \ | 
|---|
| 572 | ann = (ann & ~SO_ann) | (used << 8);			      \ | 
|---|
| 573 | }								      \ | 
|---|
| 574 | else if ((used & SS2_mask) != 0 && (ann & SS2_ann) != (used << 8))\ | 
|---|
| 575 | {								      \ | 
|---|
| 576 | const char *escseq;					      \ | 
|---|
| 577 | \ | 
|---|
| 578 | assert (used == CNS11643_2_set); /* XXX */		      \ | 
|---|
| 579 | escseq = "*H";						      \ | 
|---|
| 580 | *outptr++ = ESC;					      \ | 
|---|
| 581 | *outptr++ = '$';					      \ | 
|---|
| 582 | *outptr++ = *escseq++;					      \ | 
|---|
| 583 | *outptr++ = *escseq++;					      \ | 
|---|
| 584 | \ | 
|---|
| 585 | ann = (ann & ~SS2_ann) | (used << 8);			      \ | 
|---|
| 586 | }								      \ | 
|---|
| 587 | else if ((used & SS3_mask) != 0 && (ann & SS3_ann) != (used << 8))\ | 
|---|
| 588 | {								      \ | 
|---|
| 589 | const char *escseq;					      \ | 
|---|
| 590 | \ | 
|---|
| 591 | assert ((used >> 5) >= 3 && (used >> 5) <= 7);		      \ | 
|---|
| 592 | escseq = "+I+J+K+L+M" + ((used >> 5) - 3) * 2;		      \ | 
|---|
| 593 | *outptr++ = ESC;					      \ | 
|---|
| 594 | *outptr++ = '$';					      \ | 
|---|
| 595 | *outptr++ = *escseq++;					      \ | 
|---|
| 596 | *outptr++ = *escseq++;					      \ | 
|---|
| 597 | \ | 
|---|
| 598 | ann = (ann & ~SS3_ann) | (used << 8);			      \ | 
|---|
| 599 | }								      \ | 
|---|
| 600 | \ | 
|---|
| 601 | if (used == CNS11643_2_set)					      \ | 
|---|
| 602 | {								      \ | 
|---|
| 603 | if (outptr + 2 > outend)				      \ | 
|---|
| 604 | {							      \ | 
|---|
| 605 | result = __GCONV_FULL_OUTPUT;			      \ | 
|---|
| 606 | break;						      \ | 
|---|
| 607 | }							      \ | 
|---|
| 608 | *outptr++ = SS2_0;					      \ | 
|---|
| 609 | *outptr++ = SS2_1;					      \ | 
|---|
| 610 | }								      \ | 
|---|
| 611 | else if (used >= CNS11643_3_set && used <= CNS11643_7_set)	      \ | 
|---|
| 612 | {								      \ | 
|---|
| 613 | if (outptr + 2 > outend)				      \ | 
|---|
| 614 | {							      \ | 
|---|
| 615 | result = __GCONV_FULL_OUTPUT;			      \ | 
|---|
| 616 | break;						      \ | 
|---|
| 617 | }							      \ | 
|---|
| 618 | *outptr++ = SS3_0;					      \ | 
|---|
| 619 | *outptr++ = SS3_1;					      \ | 
|---|
| 620 | }								      \ | 
|---|
| 621 | else							      \ | 
|---|
| 622 | {								      \ | 
|---|
| 623 | /* We only have to emit something if currently ASCII is	      \ | 
|---|
| 624 | selected.  Otherwise we are switching within the	      \ | 
|---|
| 625 | SO charset.  */					      \ | 
|---|
| 626 | if (set == ASCII_set)					      \ | 
|---|
| 627 | {							      \ | 
|---|
| 628 | if (outptr + 1 > outend)				      \ | 
|---|
| 629 | {							      \ | 
|---|
| 630 | result = __GCONV_FULL_OUTPUT;			      \ | 
|---|
| 631 | break;						      \ | 
|---|
| 632 | }							      \ | 
|---|
| 633 | *outptr++ = SO;					      \ | 
|---|
| 634 | }							      \ | 
|---|
| 635 | }								      \ | 
|---|
| 636 | \ | 
|---|
| 637 | /* Always test the length here since we have used up all the      \ | 
|---|
| 638 | guaranteed output buffer slots.  */			      \ | 
|---|
| 639 | if (outptr + 2 > outend)					      \ | 
|---|
| 640 | {								      \ | 
|---|
| 641 | result = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 642 | break;							      \ | 
|---|
| 643 | }								      \ | 
|---|
| 644 | }								      \ | 
|---|
| 645 | else if (outptr + 2 > outend)					      \ | 
|---|
| 646 | {								      \ | 
|---|
| 647 | result = __GCONV_FULL_OUTPUT;				      \ | 
|---|
| 648 | break;							      \ | 
|---|
| 649 | }								      \ | 
|---|
| 650 | \ | 
|---|
| 651 | *outptr++ = buf[0];						      \ | 
|---|
| 652 | *outptr++ = buf[1];						      \ | 
|---|
| 653 | set = used;							      \ | 
|---|
| 654 | }									      \ | 
|---|
| 655 | \ | 
|---|
| 656 | /* Now that we wrote the output increment the input pointer.  */	      \ | 
|---|
| 657 | inptr += 4;								      \ | 
|---|
| 658 | } | 
|---|
| 659 | DIAG_POP_NEEDS_COMMENT; | 
|---|
| 660 | #define 	, int *setp | 
|---|
| 661 | #define INIT_PARAMS		int set = (*setp >> 3) & CURRENT_MASK; \ | 
|---|
| 662 | int ann = (*setp >> 3) & ~CURRENT_MASK | 
|---|
| 663 | #define REINIT_PARAMS		do					      \ | 
|---|
| 664 | {					      \ | 
|---|
| 665 | set = (*setp >> 3) & CURRENT_MASK;	      \ | 
|---|
| 666 | ann = (*setp >> 3) & ~CURRENT_MASK;	      \ | 
|---|
| 667 | }					      \ | 
|---|
| 668 | while (0) | 
|---|
| 669 | #define UPDATE_PARAMS		*setp = (set | ann) << 3 | 
|---|
| 670 | #define LOOP_NEED_FLAGS | 
|---|
| 671 | #include <iconv/loop.c> | 
|---|
| 672 |  | 
|---|
| 673 |  | 
|---|
| 674 | /* Now define the toplevel functions.  */ | 
|---|
| 675 | #include <iconv/skeleton.c> | 
|---|
| 676 |  | 
|---|