wtf.c 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

#include "wc.h"
#include "wtf.h"
#include "sjis.h"
#include "big5.h"
#include "hkscs.h"
#include "johab.h"
#include "jis.h"
#include "viet.h"
#include "gbk.h"
#include "gb18030.h"
#include "uhc.h"
#ifdef USE_UNICODE
#include "ucs.h"
#include "utf8.h"
#endif

wc_uint8 WTF_WIDTH_MAP[ 0x100 ] = {
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,

    1,2,1,2,1,1,1,2, 1,2,1,2,1,1,1,1, 0,0,0,0,0,0,0,0, 0,0,0,0,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
};

wc_uint8 WTF_LEN_MAP[ 0x100 ] = {
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,

    3,4,3,4,3,3,3,4, 4,4,6,6,1,1,1,1, 3,4,3,4,3,3,3,4, 4,4,6,6,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
};

wc_uint8 WTF_TYPE_MAP[ 0x100 ] = {
    1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
    0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1,

    2,  0xA,2,  0xA, 2, 0x12,2,  0xA, 2,  0xA,2,  0xA, 0x20,0x20,0x20,0x20,
    4,  0xC,4,  0xC, 4, 0x20,4,  0xC, 4,  0xC,4,  0xC, 0x20,0x20,0x20,0x20,
 0x20,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,
};

static wc_uint16 CCS_MAP[ 33 ] = {
    WC_CCS_A_CS94    >> 8, WC_CCS_A_CS94W    >> 8,
    WC_CCS_A_CS96    >> 8, WC_CCS_A_CS96W    >> 8,
    WC_CCS_A_CS942   >> 8, WC_CCS_A_UNKNOWN  >> 8,
    WC_CCS_A_PCS     >> 8, WC_CCS_A_PCSW     >> 8,
    WC_CCS_A_WCS16   >> 8, WC_CCS_A_WCS16W   >> 8,
    WC_CCS_A_WCS32   >> 8, WC_CCS_A_WCS32W   >> 8,
    0,                     0,
    0,                     0,
    WC_CCS_A_CS94_C  >> 8, WC_CCS_A_CS94W_C  >> 8,
    WC_CCS_A_CS96_C  >> 8, WC_CCS_A_CS96W_C  >> 8,
    WC_CCS_A_CS942_C >> 8, 0,
    WC_CCS_A_PCS_C   >> 8, WC_CCS_A_PCSW_C   >> 8,
    WC_CCS_A_WCS16_C >> 8, WC_CCS_A_WCS16W_C >> 8,
    WC_CCS_A_WCS32_C >> 8, WC_CCS_A_WCS32W_C >> 8,
    0,                     0,
    0,                     0,
    0,
};

wc_ccs wtf_gr_ccs = 0;
static wc_ces wtf_major_ces = WC_CES_US_ASCII;
static wc_status wtf_major_st;

void
wtf_init(wc_ces ces1, wc_ces ces2)
{
    int i;
    wc_gset *gset;

    if (wc_check_ces(ces2))
	wtf_major_ces = ces2;

    if (! wc_check_ces(ces1))
	return;
    gset = WcCesInfo[WC_CES_INDEX(ces1)].gset;
    if (gset == NULL || gset[1].ccs == 0 ||
	gset[1].ccs & (WC_CCS_A_WCS16|WC_CCS_A_WCS32))
	return;
    wtf_gr_ccs = gset[1].ccs;

    if (WC_CCS_IS_WIDE(wtf_gr_ccs)) {
	for (i = 0xa1; i <= 0xff; i++) {
	    WTF_WIDTH_MAP[i] = 2;
	    WTF_LEN_MAP[i] = 2;
	    WTF_TYPE_MAP[i] = WTF_TYPE_WCHAR1W;
	}
    } else {
	for (i = 0xa1; i <= 0xff; i++) {
	    WTF_WIDTH_MAP[i] = 1;
	    WTF_LEN_MAP[i] = 1;
	    WTF_TYPE_MAP[i] = WTF_TYPE_WCHAR1;
	}
    }
}

/*
int
wtf_width(wc_uchar *p)
{
    return (int)WTF_WIDTH_MAP[*p];
}
*/

int
wtf_strwidth(wc_uchar *p)
{
    int w = 0;
123
    wc_uchar *q = p + strlen(p);
124

125
    while (p < q) {
126
	w += wtf_width(p);
127
	p += WTF_LEN_MAP[*p];
128 129 130 131 132 133 134
    }
    return w;
}

size_t
wtf_len1(wc_uchar *p)
{
135 136 137 138 139 140 141 142
    size_t len, len_max = WTF_LEN_MAP[*p];

    for (len = 0; *(p + len); len++)
	if (len == len_max)
	    break;
    if (len == 0)
	len = 1;
    return len;
143 144 145 146 147 148
}

size_t
wtf_len(wc_uchar *p)
{
    wc_uchar *q = p;
149
    wc_uchar *strz = p + strlen(p);
150 151

    q += WTF_LEN_MAP[*q];
152
    while (q < strz && ! WTF_WIDTH_MAP[*q])
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	q += WTF_LEN_MAP[*q];
    return q - p;
}

/*
int
wtf_type(wc_uchar *p)
{
    return (int)WTF_TYPE_MAP[*p];
}
*/

#define wcs16_to_wtf(c, p) \
    ((p)[0] = (((c) >> 14) & 0x03) | 0x80), \
    ((p)[1] = (((c) >>  7) & 0x7f) | 0x80), \
    ((p)[2] = ( (c)        & 0x7f) | 0x80)
#define wcs32_to_wtf(c, p) \
    ((p)[0] = (((c) >> 28) & 0x0f) | 0x80), \
    ((p)[1] = (((c) >> 21) & 0x7f) | 0x80), \
    ((p)[2] = (((c) >> 14) & 0x7f) | 0x80), \
    ((p)[3] = (((c) >>  7) & 0x7f) | 0x80), \
    ((p)[4] = ( (c)        & 0x7f) | 0x80)
#define wtf_to_wcs16(p) \
176
    ((p)[0] == 0 || (p)[1] == 0 || (p)[2] == 0 ? 0 : \
177 178
      ((wc_uint32)((p)[0] & 0x03) << 14) \
    | ((wc_uint32)((p)[1] & 0x7f) <<  7) \
179
    | ((wc_uint32)((p)[2] & 0x7f)      ))
180
#define wtf_to_wcs32(p) \
181
    ((p)[0] == 0 || (p)[1] == 0 || (p)[2] == 0 || (p)[3] == 0 || (p)[4] == 0 ? 0 : \
182 183 184 185
      ((wc_uint32)((p)[0] & 0x0f) << 28) \
    | ((wc_uint32)((p)[1] & 0x7f) << 21) \
    | ((wc_uint32)((p)[2] & 0x7f) << 14) \
    | ((wc_uint32)((p)[3] & 0x7f) <<  7) \
186
    | ((wc_uint32)((p)[4] & 0x7f)      ))
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

void
wtf_push(Str os, wc_ccs ccs, wc_uint32 code)
{
    wc_uchar s[8];
    wc_wchar_t cc, cc2;
    size_t n;

    if (ccs == WC_CCS_US_ASCII) {
	Strcat_char(os, (char)(code & 0x7f));
	return;
    }
    cc.ccs = ccs;
    cc.code = code;
    if (WcOption.pre_conv && !(cc.ccs & WC_CCS_A_UNKNOWN)) {
	if ((ccs == WC_CCS_JOHAB || ccs == WC_CCS_JOHAB_1 ||
		ccs == WC_CCS_JOHAB_2 || ccs == WC_CCS_JOHAB_3) &&
		(wtf_major_ces == WC_CES_EUC_KR ||
		wtf_major_ces == WC_CES_ISO_2022_KR)) {
	    cc2 = wc_johab_to_ksx1001(cc);
	    if (!WC_CCS_IS_UNKNOWN(cc2.ccs))
		cc = cc2;
	} else if (ccs == WC_CCS_KS_X_1001 &&
		wtf_major_ces == WC_CES_JOHAB) {
	    cc2 = wc_ksx1001_to_johab(cc);
	    if (!WC_CCS_IS_UNKNOWN(cc2.ccs))
		cc = cc2;
	}
#ifdef USE_UNICODE
	else if (WcOption.ucs_conv) {
	    wc_bool fix_width_conv = WcOption.fix_width_conv;
	    WcOption.fix_width_conv = WC_FALSE;
	    wc_output_init(wtf_major_ces, &wtf_major_st);
	    if (! wc_ces_has_ccs(WC_CCS_SET(ccs), &wtf_major_st)) {
		cc2 = wc_any_to_any_ces(cc, &wtf_major_st);
		if (cc2.ccs == WC_CCS_US_ASCII) {
		    Strcat_char(os, (char)(cc2.code & 0x7f));
		    return;
		}
		if (!WC_CCS_IS_UNKNOWN(cc2.ccs) &&
			cc2.ccs != WC_CCS_CP1258_2 &&
			cc2.ccs != WC_CCS_TCVN_5712_3)
		    cc = cc2;
	    }
	    WcOption.fix_width_conv = fix_width_conv;
	}
#endif
    }

    switch (WC_CCS_TYPE(cc.ccs)) {
    case WC_CCS_A_CS94:
	if (cc.ccs == wtf_gr_ccs) {
	    s[0] = (cc.code & 0x7f) | 0x80;
	    n = 1;
	    break;
	}
	if (cc.ccs == WC_CCS_JIS_X_0201K && !WcOption.use_jisx0201k) {
	    cc2 = wc_jisx0201k_to_jisx0208(cc);
	    if (!WC_CCS_IS_UNKNOWN(cc2.ccs)) {
		wtf_push(os, cc2.ccs, cc2.code);
		return;
	    }
	}
	s[0] = WTF_C_CS94;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = (cc.code & 0x7f) | 0x80;
	n = 3;
	break;
    case WC_CCS_A_CS94W:
	if (cc.ccs == wtf_gr_ccs) {
	    s[0] = ((cc.code >> 8) & 0x7f) | 0x80;
	    s[1] = ( cc.code       & 0x7f) | 0x80;
	    n = 2;
	    break;
	}
	s[0] = WTF_C_CS94W;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = ((cc.code >> 8) & 0x7f) | 0x80;
	s[3] = ( cc.code       & 0x7f) | 0x80;
	n = 4;
	break;
    case WC_CCS_A_CS96:
	if (WcOption.use_combining && wc_is_combining(cc))
	    s[0] = WTF_C_CS96_C;
	else if (cc.ccs == wtf_gr_ccs && (cc.code & 0x7f) > 0x20) {
	    s[0] = (cc.code & 0x7f) | 0x80;
	    n = 1;
	    break;
	} else
	    s[0] = WTF_C_CS96;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = (cc.code & 0x7f) | 0x80;
	n = 3;
	break;
    case WC_CCS_A_CS96W:
	if (cc.ccs == wtf_gr_ccs && ((cc.code >> 8) & 0x7f) > 0x20) {
	    s[0] = ((cc.code >> 8) & 0x7f) | 0x80;
	    s[1] = ( cc.code       & 0x7f) | 0x80;
	    n = 2;
	    break;
	}
	s[0] = WTF_C_CS96W;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = ((cc.code >> 8) & 0x7f) | 0x80;
	s[3] = ( cc.code       & 0x7f) | 0x80;
	n = 4;
	break;
    case WC_CCS_A_CS942:
	if (cc.ccs == wtf_gr_ccs) {
	    s[0] = (cc.code & 0x7f) | 0x80;
	    n = 1;
	    break;
	}
	s[0] = WTF_C_CS942;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = (cc.code & 0x7f) | 0x80;
	n = 3;
	break;
    case WC_CCS_A_PCS:
	if (WcOption.use_combining && wc_is_combining(cc))
	    s[0] = WTF_C_PCS_C;
	else if (cc.ccs == wtf_gr_ccs && (cc.code & 0x7f) > 0x20) {
	    s[0] = (cc.code & 0x7f) | 0x80;
	    n = 1;
	    break;
	} else
	    s[0] = WTF_C_PCS;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = (cc.code & 0x7f) | 0x80;
	n = 3;
	break;
    case WC_CCS_A_PCSW:
	switch (cc.ccs) {
	case WC_CCS_SJIS_EXT:
	    cc = wc_sjis_ext_to_cs94w(cc);
	    break;
	case WC_CCS_GBK:
	    cc = wc_gbk_to_cs128w(cc);
	    break;
	case WC_CCS_GBK_EXT:
	    cc = wc_gbk_ext_to_cs128w(cc);
	    break;
	case WC_CCS_BIG5:
	    cc = wc_big5_to_cs94w(cc);
	    break;
	case WC_CCS_HKSCS:
	    cc = wc_hkscs_to_cs128w(cc);
	    break;
	case WC_CCS_JOHAB:
	    cc = wc_johab_to_cs128w(cc);
	    break;
	case WC_CCS_UHC:
	    cc = wc_uhc_to_cs128w(cc);
	    break;
	}
	if (cc.ccs == wtf_gr_ccs && ((cc.code >> 8) & 0x7f) > 0x20) {
	    s[0] = ((cc.code >> 8) & 0x7f) | 0x80;
	    s[1] = ( cc.code       & 0x7f) | 0x80;
	    n = 2;
	    break;
	}
	s[0] = WTF_C_PCSW;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = ((cc.code >> 8) & 0x7f) | 0x80;
	s[3] = ( cc.code       & 0x7f) | 0x80;
	n = 4;
	break;
    case WC_CCS_A_WCS16:
	s[0] = (WC_CCS_IS_WIDE(cc.ccs) ? WTF_C_WCS16W : WTF_C_WCS16)
	     | (WC_CCS_IS_COMB(cc.ccs) ? WTF_C_COMB : 0);
	wcs16_to_wtf(cc.code, s + 1);
	s[1] |= (WC_CCS_INDEX(cc.ccs) << 2);
	n = 4;
	break;
    case WC_CCS_A_WCS32:
	s[0] = (WC_CCS_IS_WIDE(cc.ccs) ? WTF_C_WCS32W : WTF_C_WCS32)
	     | (WC_CCS_IS_COMB(cc.ccs) ? WTF_C_COMB : 0);
	wcs32_to_wtf(cc.code, s + 1);
	s[1] |= (WC_CCS_INDEX(cc.ccs) << 4);
	n = 6;
	break;
    default:
	s[0] = WTF_C_UNKNOWN;
	s[1] = WC_CCS_INDEX(cc.ccs) | 0x80;
	s[2] = (cc.code & 0x7f) | 0x80;
	n = 3;
	break;
    }
    Strcat_charp_n(os, (char *)s, n);
}

void
wtf_push_unknown(Str os, wc_uchar *p, size_t len)
{
    for (; len--; p++) {
	if (*p & 0x80)
	    wtf_push(os, WC_CCS_UNKNOWN, *p);
	else
	    Strcat_char(os, (char)*p);
    }
}

wc_wchar_t
wtf_parse1(wc_uchar **p)
{
    wc_uchar *q = *p;
    wc_wchar_t cc;

    if (*q < 0x80) {
	cc.ccs = WC_CCS_US_ASCII;
	cc.code = *(q++);
    } else if (*q > 0xa0) {
	cc.ccs = wtf_gr_ccs;
400
	if (WC_CCS_IS_WIDE(cc.ccs)) {
401 402 403 404 405
	    cc.code = ((wc_uint32)*q << 8) | *(q+1);
	    q += 2;
	} else
	    cc.code = *(q++);
    } else {
406
	cc.ccs = (wc_uint32)CCS_MAP[*(q++) - 0x80] << 8;
407 408 409 410 411 412
	switch (WC_CCS_TYPE(cc.ccs)) {
	case WC_CCS_A_CS94:
	case WC_CCS_A_CS96:
	case WC_CCS_A_CS942:
	case WC_CCS_A_PCS:
	case WC_CCS_A_UNKNOWN:
413 414
	    cc.ccs |= *(q++) & 0x7f;
	    cc.code = *(q++);
415 416 417 418
	    break;
	case WC_CCS_A_CS94W:
	case WC_CCS_A_CS96W:
	case WC_CCS_A_PCSW:
419 420 421
	    cc.ccs |= *(q++) & 0x7f;
	    cc.code = ((wc_uint32)*q << 8) | *(q+1);
	    q += 2;
422 423 424
	    break;
	case WC_CCS_A_WCS16:
	case WC_CCS_A_WCS16W:
425 426 427
	    cc.ccs |= (*q & 0x7c) >> 2;
	    cc.code = wtf_to_wcs16(q);
	    q += 3;
428 429 430
	    break;
	case WC_CCS_A_WCS32:
	case WC_CCS_A_WCS32W:
431 432 433 434 435 436 437 438
	    cc.ccs |= (*q & 0x70) >> 4;
	    cc.code = wtf_to_wcs32(q);
	    q += 5;
	    break;
	default:
	/* case 0: */
	    cc.ccs = WC_CCS_US_ASCII;
	    cc.code = (wc_uint32)' ';
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	    break;
	}
    }

    *p = q;
    switch (cc.ccs) {
    case WC_CCS_SJIS_EXT_1:
    case WC_CCS_SJIS_EXT_2:
	return wc_cs94w_to_sjis_ext(cc);
    case WC_CCS_GBK_1:
    case WC_CCS_GBK_2:
	return wc_cs128w_to_gbk(cc);
    case WC_CCS_GBK_EXT_1:
    case WC_CCS_GBK_EXT_2:
	return wc_cs128w_to_gbk_ext(cc);
    case WC_CCS_BIG5_1:
    case WC_CCS_BIG5_2:
	return wc_cs94w_to_big5(cc);
    case WC_CCS_HKSCS_1:
    case WC_CCS_HKSCS_2:
	return wc_cs128w_to_hkscs(cc);
    case WC_CCS_JOHAB_1:
    case WC_CCS_JOHAB_2:
    case WC_CCS_JOHAB_3:
	return wc_cs128w_to_johab(cc);
    case WC_CCS_UHC_1:
    case WC_CCS_UHC_2:
	return wc_cs128w_to_uhc(cc);
    }
    return cc;
}

wc_wchar_t
wtf_parse(wc_uchar **p)
{
    wc_uchar *q;
    wc_wchar_t cc, cc2;
    wc_uint32 ucs, ucs2;

    if (**p < 0x80) {
	cc.ccs = WC_CCS_US_ASCII;
	cc.code = *((*p)++);
    } else
	cc = wtf_parse1(p);
    if ((! WcOption.use_combining) || WTF_WIDTH_MAP[**p])
	return cc;

    q = *p;
    cc2 = wtf_parse1(&q);
    if ((cc.ccs == WC_CCS_US_ASCII || cc.ccs == WC_CCS_CP1258_1) &&
	WC_CCS_SET(cc2.ccs) == WC_CCS_CP1258_1) {
	cc2.code = wc_cp1258_precompose(cc.code, cc2.code);
	if (cc2.code) {
	    cc2.ccs = WC_CCS_CP1258_2;
	    *p = q;
	    return cc2;
	}
    } else if ((cc.ccs == WC_CCS_US_ASCII || cc.ccs == WC_CCS_TCVN_5712_1) &&
	WC_CCS_SET(cc2.ccs) == WC_CCS_TCVN_5712_1) {
	cc2.code = wc_tcvn5712_precompose(cc.code, cc2.code);
	if (cc2.code) {
	    cc2.ccs = WC_CCS_TCVN_5712_3;
	    *p = q;
	    return cc2;
	}
    }
#ifdef USE_UNICODE
    else if ((cc.ccs == WC_CCS_US_ASCII || cc.ccs == WC_CCS_ISO_8859_1 ||
	WC_CCS_IS_UNICODE(cc.ccs)) && WC_CCS_IS_UNICODE(cc2.ccs)) {
	while (1) {
	    ucs = (WC_CCS_SET(cc.ccs) == WC_CCS_UCS_TAG)
		? wc_ucs_tag_to_ucs(cc.code) : cc.code;
	    ucs2 = (WC_CCS_SET(cc2.ccs) == WC_CCS_UCS_TAG)
		? wc_ucs_tag_to_ucs(cc2.code) : cc2.code;
	    ucs = wc_ucs_precompose(ucs, ucs2);
	    if (ucs == WC_C_UCS4_ERROR)
		break;
	    if (WC_CCS_SET(cc.ccs) == WC_CCS_UCS_TAG)
		cc.code = wc_ucs_to_ucs_tag(ucs, wc_ucs_tag_to_tag(cc.code));
	    else {
		cc.ccs = wc_ucs_to_ccs(ucs);
		cc.code = ucs;
	    }
	    *p = q;
	    if (! WTF_WIDTH_MAP[*q])
		break;
	    cc2 = wtf_parse1(&q);
	    if (! WC_CCS_IS_UNICODE(cc2.ccs))
		break;
	}
    }
#endif
    return cc;
}

wc_ccs
wtf_get_ccs(wc_uchar *p)
{
   return wtf_parse1(&p).ccs;
}

wc_uint32
wtf_get_code(wc_uchar *p)
{
   return wtf_parse1(&p).code;
}

wc_bool
wtf_is_hangul(wc_uchar *p)
{
    if (*p > 0xa0)
	return (wtf_gr_ccs == WC_CCS_KS_X_1001 || wtf_gr_ccs == WC_CCS_JOHAB_1);
    else if (*p == WTF_C_CS94W)
	return ((*(p + 1) & 0x7f) == WC_F_KS_X_1001);
    else if (*p == WTF_C_PCSW) {
	wc_uchar f = *(p + 1) & 0x7f;
	return (f == WC_F_JOHAB_1 || f == WC_F_JOHAB_2 || f == WC_F_JOHAB_3 ||
		f == WC_F_UHC_1 || f == WC_F_UHC_2);
    }
#ifdef USE_UNICODE
    else if (*p == WTF_C_WCS16W) {
	wc_uchar f = (*(++p) & 0x7f) >> 2;
	if (f == WC_F_UCS2)
	    return wc_is_ucs_hangul(wtf_to_wcs16(p));
    } else if (*p == WTF_C_WCS32W) {
	wc_uchar f = (*(++p) & 0x7f) >> 4;
	if (f == WC_F_UCS_TAG)
	    return wc_is_ucs_hangul(wc_ucs_tag_to_ucs(wtf_to_wcs32(p)));
    }
#endif
    return WC_FALSE;
}

char *
wtf_conv_fit(char *s, wc_ces ces)
{
    wc_uchar *p;
    Str os;
    wc_wchar_t cc;
    wc_ces major_ces;
    wc_bool pre_conv, ucs_conv;

    if (ces == WC_CES_WTF || ces == WC_CES_US_ASCII)
	return s;

    for (p = (wc_uchar *)s; *p && *p < 0x80; p++)
	;
    if (! *p)
	return s;

    os = Strnew_size(strlen(s));
    if (p > (wc_uchar *)s)
	Strcopy_charp_n(os, s, (int)(p - (wc_uchar *)s));

    major_ces = wtf_major_ces;
    pre_conv = WcOption.pre_conv;
    ucs_conv = WcOption.ucs_conv;
    wtf_major_ces = ces;
    WcOption.pre_conv = WC_TRUE;
    WcOption.ucs_conv = WC_TRUE;
    while (*p) {
	cc = wtf_parse1(&p);
	wtf_push(os, cc.ccs, cc.code);
    }
    wtf_major_ces = major_ces;
    WcOption.pre_conv = pre_conv;
    WcOption.ucs_conv = ucs_conv;
    return os->ptr;
}