conv.c 37.7 KB
Newer Older
B
Bruce Momjian 已提交
1 2 3 4
/*
 * conversion between client encoding and server internal encoding
 * (currently mule internal code (mic) is used)
 * Tatsuo Ishii
T
Tatsuo Ishii 已提交
5
 *
6
 * WIN1250 client encoding support contributed by Pavel Behal
T
Tatsuo Ishii 已提交
7
 * SJIS UDC (NEC selection IBM kanji) support contributed by Eiji Tokuya
8
 *
9
 * $Id: conv.c,v 1.32 2001/10/16 10:09:17 ishii Exp $
10 11
 *
 *
B
Bruce Momjian 已提交
12
 */
13
#include "postgres.h"
M
 
Marc G. Fournier 已提交
14
#include "mb/pg_wchar.h"
B
Bruce Momjian 已提交
15

16
#ifdef UNICODE_CONVERSION
T
Tatsuo Ishii 已提交
17

18 19 20
/*
 * for Unicode (UTF-8) support
 */
21 22 23 24
#include "Unicode/utf8_to_iso8859_2.map"
#include "Unicode/utf8_to_iso8859_3.map"
#include "Unicode/utf8_to_iso8859_4.map"
#include "Unicode/utf8_to_iso8859_5.map"
T
Tatsuo Ishii 已提交
25 26 27 28 29 30 31 32 33
#include "Unicode/utf8_to_iso8859_6.map"
#include "Unicode/utf8_to_iso8859_7.map"
#include "Unicode/utf8_to_iso8859_8.map"
#include "Unicode/utf8_to_iso8859_9.map"
#include "Unicode/utf8_to_iso8859_10.map"
#include "Unicode/utf8_to_iso8859_13.map"
#include "Unicode/utf8_to_iso8859_14.map"
#include "Unicode/utf8_to_iso8859_15.map"
#include "Unicode/utf8_to_iso8859_16.map"
34 35 36 37
#include "Unicode/iso8859_2_to_utf8.map"
#include "Unicode/iso8859_3_to_utf8.map"
#include "Unicode/iso8859_4_to_utf8.map"
#include "Unicode/iso8859_5_to_utf8.map"
T
Tatsuo Ishii 已提交
38 39 40 41 42 43 44 45 46
#include "Unicode/iso8859_6_to_utf8.map"
#include "Unicode/iso8859_7_to_utf8.map"
#include "Unicode/iso8859_8_to_utf8.map"
#include "Unicode/iso8859_9_to_utf8.map"
#include "Unicode/iso8859_10_to_utf8.map"
#include "Unicode/iso8859_13_to_utf8.map"
#include "Unicode/iso8859_14_to_utf8.map"
#include "Unicode/iso8859_15_to_utf8.map"
#include "Unicode/iso8859_16_to_utf8.map"
47 48 49 50 51 52 53 54 55 56 57 58
#include "Unicode/utf8_to_euc_jp.map"
#include "Unicode/euc_jp_to_utf8.map"
#include "Unicode/utf8_to_euc_cn.map"
#include "Unicode/euc_cn_to_utf8.map"
#include "Unicode/utf8_to_euc_kr.map"
#include "Unicode/euc_kr_to_utf8.map"
#include "Unicode/utf8_to_euc_tw.map"
#include "Unicode/euc_tw_to_utf8.map"
#include "Unicode/utf8_to_sjis.map"
#include "Unicode/sjis_to_utf8.map"
#include "Unicode/utf8_to_big5.map"
#include "Unicode/big5_to_utf8.map"
59
/* Cyrillic charset conversion */
T
Tatsuo Ishii 已提交
60 61 62 63 64 65
#include "Unicode/alt_to_utf8.map"
#include "Unicode/koi8r_to_utf8.map"
#include "Unicode/win1251_to_utf8.map"
#include "Unicode/utf8_to_alt.map"
#include "Unicode/utf8_to_koi8r.map"
#include "Unicode/utf8_to_win1251.map"
66

67

B
Bruce Momjian 已提交
68
#endif	 /* UNICODE_CONVERSION */
69

70 71 72 73 74 75 76 77 78 79
/*
 * SJIS alternative code.
 * this code is used if a mapping EUC -> SJIS is not defined.
 */
#define PGSJISALTCODE 0x81ac
#define PGEUCALTCODE 0xa2ae

/*
 * conversion table between SJIS UDC (IBM kanji) and EUC_JP
 */
80 81
#include "sjis.map"

B
Bruce Momjian 已提交
82
/*
83
 * convert bogus chars that cannot be represented in the current
T
Tatsuo Ishii 已提交
84
 * encoding system.
B
Bruce Momjian 已提交
85
 */
86 87
static void
printBogusChar(unsigned char **mic, unsigned char **p)
B
Bruce Momjian 已提交
88
{
89 90 91 92 93 94 95 96 97 98 99
	char		strbuf[16];
	int			l = pg_mic_mblen(*mic);

	*(*p)++ = '(';
	while (l--)
	{
		sprintf(strbuf, "%02x", *(*mic)++);
		*(*p)++ = strbuf[0];
		*(*p)++ = strbuf[1];
	}
	*(*p)++ = ')';
B
Bruce Momjian 已提交
100 101 102 103 104
}

/*
 * SJIS ---> MIC
 */
105 106
static void
sjis2mic(unsigned char *sjis, unsigned char *p, int len)
B
Bruce Momjian 已提交
107
{
108
	int			c1,
109
				c2,
T
Tatsuo Ishii 已提交
110 111 112 113
/* Eiji Tokuya patched begin */
				i,
				k,
				k2;
B
Bruce Momjian 已提交
114

T
Tatsuo Ishii 已提交
115
/* Eiji Tokuya patched end */
116 117 118
	while (len > 0 && (c1 = *sjis++))
	{
		if (c1 >= 0xa1 && c1 <= 0xdf)
119 120
		{
			/* JIS X0201 (1 byte kana) */
121 122 123 124 125
			len--;
			*p++ = LC_JISX0201K;
			*p++ = c1;
		}
		else if (c1 > 0x7f)
126
		{
127 128 129 130

			/*
			 * JIS X0208, X0212, user defined extended characters
			 */
131
			c2 = *sjis++;
132
			k = (c1 << 8) + c2;
T
Tatsuo Ishii 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
/* Eiji Tokuya patched begin */
			if (k >= 0xed40 && k < 0xf040)
			{
				/* NEC selection IBM kanji */
				for (i = 0;; i++)
				{
					k2 = ibmkanji[i].nec;
					if (k2 == 0xffff)
						break;
					if (k2 == k)
					{
						k = ibmkanji[i].sjis;
						c1 = (k >> 8) & 0xff;
B
Bruce Momjian 已提交
146
						c2 = k & 0xff;
T
Tatsuo Ishii 已提交
147 148 149
					}
				}
			}
150

T
Tatsuo Ishii 已提交
151 152
			if (k < 0xeb3f)
/* Eiji Tokuya patched end */
153 154 155 156 157 158 159
			{
				/* JIS X0208 */
				len -= 2;
				*p++ = LC_JISX0208;
				*p++ = ((c1 & 0x3f) << 1) + 0x9f + (c2 > 0x9e);
				*p++ = c2 + ((c2 > 0x9e) ? 2 : 0x60) + (c2 < 0x80);
			}
T
Tatsuo Ishii 已提交
160
/* Eiji Tokuya patched begin */
B
Bruce Momjian 已提交
161
			else if ((k >= 0xeb40 && k < 0xf040) || (k >= 0xfc4c && k <= 0xfcfc))
162
			{
T
Tatsuo Ishii 已提交
163 164
				/* NEC selection IBM kanji - Other undecided justice */
/* Eiji Tokuya patched end */
165 166
				*p++ = LC_JISX0208;
				*p++ = PGEUCALTCODE >> 8;
167
				*p++ = PGEUCALTCODE & 0xff;
168 169 170
			}
			else if (k >= 0xf040 && k < 0xf540)
			{
171 172 173 174 175

				/*
				 * UDC1 mapping to X0208 85 ku - 94 ku JIS code 0x7521 -
				 * 0x7e7e EUC 0xf5a1 - 0xfefe
				 */
176 177 178 179 180 181 182 183
				len -= 2;
				*p++ = LC_JISX0208;
				c1 -= 0x6f;
				*p++ = ((c1 & 0x3f) << 1) + 0xf3 + (c2 > 0x9e);
				*p++ = c2 + ((c2 > 0x9e) ? 2 : 0x60) + (c2 < 0x80);
			}
			else if (k >= 0xf540 && k < 0xfa40)
			{
184 185 186 187 188

				/*
				 * UDC2 mapping to X0212 85 ku - 94 ku JIS code 0x7521 -
				 * 0x7e7e EUC 0x8ff5a1 - 0x8ffefe
				 */
189 190 191 192 193 194 195 196
				len -= 2;
				*p++ = LC_JISX0212;
				c1 -= 0x74;
				*p++ = ((c1 & 0x3f) << 1) + 0xf3 + (c2 > 0x9e);
				*p++ = c2 + ((c2 > 0x9e) ? 2 : 0x60) + (c2 < 0x80);
			}
			else if (k >= 0xfa40)
			{
197 198 199

				/*
				 * mapping IBM kanji to X0208 and X0212
200 201 202
				 *
				 */
				len -= 2;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
				for (i = 0;; i++)
				{
					k2 = ibmkanji[i].sjis;
					if (k2 == 0xffff)
						break;
					if (k2 == k)
					{
						k = ibmkanji[i].euc;
						if (k >= 0x8f0000)
						{
							*p++ = LC_JISX0212;
							*p++ = 0x80 | ((k & 0xff00) >> 8);
							*p++ = 0x80 | (k & 0xff);
						}
						else
						{
							*p++ = LC_JISX0208;
							*p++ = 0x80 | (k >> 8);
							*p++ = 0x80 | (k & 0xff);
						}
					}
224 225
				}
			}
226 227 228 229 230 231 232 233
		}
		else
		{						/* should be ASCII */
			len--;
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
234 235 236 237 238
}

/*
 * MIC ---> SJIS
 */
239 240
static void
mic2sjis(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
241
{
242
	int			c1,
243 244
				c2,
				k;
245 246 247 248 249 250 251 252 253 254 255

	while (len > 0 && (c1 = *mic))
	{
		len -= pg_mic_mblen(mic++);

		if (c1 == LC_JISX0201K)
			*p++ = *mic++;
		else if (c1 == LC_JISX0208)
		{
			c1 = *mic++;
			c2 = *mic++;
256 257 258 259 260 261 262 263 264
			k = (c1 << 8) | (c2 & 0xff);
			if (k >= 0xf5a1)
			{
				/* UDC1 */
				c1 -= 0x54;
				*p++ = ((c1 - 0xa1) >> 1) + ((c1 < 0xdf) ? 0x81 : 0xc1) + 0x6f;
			}
			else
				*p++ = ((c1 - 0xa1) >> 1) + ((c1 < 0xdf) ? 0x81 : 0xc1);
265 266
			*p++ = c2 - ((c1 & 1) ? ((c2 < 0xe0) ? 0x61 : 0x60) : 2);
		}
267 268
		else if (c1 == LC_JISX0212)
		{
269 270 271
			int			i,
						k2;

272 273 274 275 276 277 278 279 280 281 282 283
			c1 = *mic++;
			c2 = *mic++;
			k = c1 << 8 | c2;
			if (k >= 0xf5a1)
			{
				/* UDC2 */
				c1 -= 0x54;
				*p++ = ((c1 - 0xa1) >> 1) + ((c1 < 0xdf) ? 0x81 : 0xc1) + 0x74;
				*p++ = c2 - ((c1 & 1) ? ((c2 < 0xe0) ? 0x61 : 0x60) : 2);
			}
			else
			{
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
				/* IBM kanji */
				for (i = 0;; i++)
				{
					k2 = ibmkanji[i].euc & 0xffff;
					if (k2 == 0xffff)
					{
						*p++ = PGSJISALTCODE >> 8;
						*p++ = PGSJISALTCODE & 0xff;
						break;
					}
					if (k2 == k)
					{
						k = ibmkanji[i].sjis;
						*p++ = k >> 8;
						*p++ = k & 0xff;
						break;
					}
				}
302 303
			}
		}
304
		else if (c1 > 0x7f)
305 306 307 308
		{
			/* cannot convert to SJIS! */
			*p++ = PGSJISALTCODE >> 8;
			*p++ = PGSJISALTCODE & 0xff;
309 310 311 312 313 314 315
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
316 317 318 319 320
}

/*
 * EUC_JP ---> MIC
 */
321 322
static void
euc_jp2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
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
	int			c1;

	while (len > 0 && (c1 = *euc++))
	{
		if (c1 == SS2)
		{						/* 1 byte kana? */
			len -= 2;
			*p++ = LC_JISX0201K;
			*p++ = *euc++;
		}
		else if (c1 == SS3)
		{						/* JIS X0212 kanji? */
			len -= 3;
			*p++ = LC_JISX0212;
			*p++ = *euc++;
			*p++ = *euc++;
		}
		else if (c1 & 0x80)
		{						/* kanji? */
			len -= 2;
			*p++ = LC_JISX0208;
			*p++ = c1;
			*p++ = *euc++;
		}
		else
		{						/* should be ASCII */
			len--;
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
355 356 357 358 359
}

/*
 * MIC ---> EUC_JP
 */
360 361
static void
mic2euc_jp(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
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
	int			c1;

	while (len > 0 && (c1 = *mic))
	{
		len -= pg_mic_mblen(mic++);

		if (c1 == LC_JISX0201K)
		{
			*p++ = SS2;
			*p++ = *mic++;
		}
		else if (c1 == LC_JISX0212)
		{
			*p++ = SS3;
			*p++ = *mic++;
			*p++ = *mic++;
		}
		else if (c1 == LC_JISX0208)
		{
			*p++ = *mic++;
			*p++ = *mic++;
		}
		else if (c1 > 0x7f)
		{						/* cannot convert to EUC_JP! */
			mic--;
			printBogusChar(&mic, &p);
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
396 397 398 399 400
}

/*
 * EUC_KR ---> MIC
 */
401 402
static void
euc_kr2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
403
{
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
	int			c1;

	while (len > 0 && (c1 = *euc++))
	{
		if (c1 & 0x80)
		{
			len -= 2;
			*p++ = LC_KS5601;
			*p++ = c1;
			*p++ = *euc++;
		}
		else
		{						/* should be ASCII */
			len--;
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
422 423 424 425 426
}

/*
 * MIC ---> EUC_KR
 */
427 428
static void
mic2euc_kr(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
429
{
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	int			c1;

	while (len > 0 && (c1 = *mic))
	{
		len -= pg_mic_mblen(mic++);

		if (c1 == LC_KS5601)
		{
			*p++ = *mic++;
			*p++ = *mic++;
		}
		else if (c1 > 0x7f)
		{						/* cannot convert to EUC_KR! */
			mic--;
			printBogusChar(&mic, &p);
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
452 453 454 455 456
}

/*
 * EUC_CN ---> MIC
 */
457 458
static void
euc_cn2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
459
{
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	int			c1;

	while (len > 0 && (c1 = *euc++))
	{
		if (c1 & 0x80)
		{
			len -= 2;
			*p++ = LC_GB2312_80;
			*p++ = c1;
			*p++ = *euc++;
		}
		else
		{						/* should be ASCII */
			len--;
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
478 479 480 481 482
}

/*
 * MIC ---> EUC_CN
 */
483 484
static void
mic2euc_cn(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
485
{
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	int			c1;

	while (len > 0 && (c1 = *mic))
	{
		len -= pg_mic_mblen(mic++);

		if (c1 == LC_GB2312_80)
		{
			*p++ = *mic++;
			*p++ = *mic++;
		}
		else if (c1 > 0x7f)
		{						/* cannot convert to EUC_CN! */
			mic--;
			printBogusChar(&mic, &p);
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
508 509 510 511 512
}

/*
 * EUC_TW ---> MIC
 */
513 514
static void
euc_tw2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
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
	int			c1;

	while (len > 0 && (c1 = *euc++))
	{
		if (c1 == SS2)
		{
			len -= 4;
			c1 = *euc++;		/* plane No. */
			if (c1 == 0xa1)
				*p++ = LC_CNS11643_1;
			else if (c1 == 0xa2)
				*p++ = LC_CNS11643_2;
			else
			{
				*p++ = 0x9d;	/* LCPRV2 */
				*p++ = 0xa3 - c1 + LC_CNS11643_3;
			}
			*p++ = *euc++;
			*p++ = *euc++;
		}
		else if (c1 & 0x80)
		{						/* CNS11643-1 */
			len -= 2;
			*p++ = LC_CNS11643_1;
			*p++ = c1;
			*p++ = *euc++;
		}
		else
		{						/* should be ASCII */
			len--;
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
550 551 552 553 554
}

/*
 * MIC ---> EUC_TW
 */
555 556
static void
mic2euc_tw(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
557
{
558 559 560 561 562 563
	int			c1;

	while (len > 0 && (c1 = *mic))
	{
		len -= pg_mic_mblen(mic++);

564
		if (c1 == LC_CNS11643_1)
565 566 567 568
		{
			*p++ = *mic++;
			*p++ = *mic++;
		}
569 570 571 572 573 574 575
		else if (c1 == LC_CNS11643_2)
		{
			*p++ = SS2;
			*p++ = 0xa2;
			*p++ = *mic++;
			*p++ = *mic++;
		}
576 577 578
		else if (c1 == 0x9d)
		{						/* LCPRV2? */
			*p++ = SS2;
579
			*p++ = *mic++ - LC_CNS11643_3 + 0xa3;
580 581 582 583 584 585 586 587 588 589 590 591 592 593
			*p++ = *mic++;
			*p++ = *mic++;
		}
		else if (c1 > 0x7f)
		{						/* cannot convert to EUC_TW! */
			mic--;
			printBogusChar(&mic, &p);
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
594 595
}

596 597 598 599 600 601
/*
 * Big5 ---> MIC
 */
static void
big52mic(unsigned char *big5, unsigned char *p, int len)
{
B
Bruce Momjian 已提交
602 603 604 605
	unsigned short c1;
	unsigned short big5buf,
				cnsBuf;
	unsigned char lc;
606
	char		bogusBuf[3];
B
Bruce Momjian 已提交
607 608 609 610
	int			i;

	while (len > 0 && (c1 = *big5++))
	{
611
		if (c1 <= 0x7fU)
B
Bruce Momjian 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		{						/* ASCII */
			len--;
			*p++ = c1;
		}
		else
		{
			len -= 2;
			big5buf = c1 << 8;
			c1 = *big5++;
			big5buf |= c1;
			cnsBuf = BIG5toCNS(big5buf, &lc);
			if (lc != 0)
			{
				if (lc == LC_CNS11643_3 || lc == LC_CNS11643_4)
				{
					*p++ = 0x9d;/* LCPRV2 */
				}
				*p++ = lc;		/* Plane No. */
				*p++ = (cnsBuf >> 8) & 0x00ff;
				*p++ = cnsBuf & 0x00ff;
			}
			else
			{					/* cannot convert */
				big5 -= 2;
				*p++ = '(';
				for (i = 0; i < 2; i++)
				{
					sprintf(bogusBuf, "%02x", *big5++);
					*p++ = bogusBuf[0];
					*p++ = bogusBuf[1];
				}
				*p++ = ')';
			}
		}
646
	}
B
Bruce Momjian 已提交
647
	*p = '\0';
648 649 650 651 652 653 654 655
}

/*
 * MIC ---> Big5
 */
static void
mic2big5(unsigned char *mic, unsigned char *p, int len)
{
B
Bruce Momjian 已提交
656 657 658 659
	int			l;
	unsigned short c1;
	unsigned short big5buf,
				cnsBuf;
660

B
Bruce Momjian 已提交
661
	while (len > 0 && (c1 = *mic))
662
	{
B
Bruce Momjian 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		l = pg_mic_mblen(mic++);
		len -= l;

		/* 0x9d means LCPRV2 */
		if (c1 == LC_CNS11643_1 || c1 == LC_CNS11643_2 || c1 == 0x9d)
		{
			if (c1 == 0x9d)
			{
				c1 = *mic++;	/* get plane no. */
			}
			cnsBuf = (*mic++) << 8;
			cnsBuf |= (*mic++) & 0x00ff;
			big5buf = CNStoBIG5(cnsBuf, c1);
			if (big5buf == 0)
			{					/* cannot convert to Big5! */
				mic -= l;
				printBogusChar(&mic, &p);
			}
			else
			{
				*p++ = (big5buf >> 8) & 0x00ff;
				*p++ = big5buf & 0x00ff;
			}
		}
		else if (c1 <= 0x7f)	/* ASCII */
			*p++ = c1;
		else
		{						/* cannot convert to Big5! */
			mic--;
			printBogusChar(&mic, &p);
		}
694
	}
B
Bruce Momjian 已提交
695
	*p = '\0';
696 697
}

B
Bruce Momjian 已提交
698
/*
M
 
Marc G. Fournier 已提交
699
 * LATINn ---> MIC
B
Bruce Momjian 已提交
700
 */
701 702
static void
latin2mic(unsigned char *l, unsigned char *p, int len, int lc)
B
Bruce Momjian 已提交
703
{
704 705 706 707 708 709 710 711 712 713 714
	int			c1;

	while (len-- > 0 && (c1 = *l++))
	{
		if (c1 > 0x7f)
		{						/* Latin1? */
			*p++ = lc;
		}
		*p++ = c1;
	}
	*p = '\0';
B
Bruce Momjian 已提交
715 716 717
}

/*
M
 
Marc G. Fournier 已提交
718
 * MIC ---> LATINn
B
Bruce Momjian 已提交
719
 */
720 721
static void
mic2latin(unsigned char *mic, unsigned char *p, int len, int lc)
B
Bruce Momjian 已提交
722
{
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	int			c1;

	while (len > 0 && (c1 = *mic))
	{
		len -= pg_mic_mblen(mic++);

		if (c1 == lc)
			*p++ = *mic++;
		else if (c1 > 0x7f)
		{
			mic--;
			printBogusChar(&mic, &p);
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
742 743
}

744 745
static void
latin12mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
746
{
747
	latin2mic(l, p, len, LC_ISO8859_1);
B
Bruce Momjian 已提交
748
}
749 750
static void
mic2latin1(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
751
{
752
	mic2latin(mic, p, len, LC_ISO8859_1);
B
Bruce Momjian 已提交
753
}
754 755
static void
latin22mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
756
{
757
	latin2mic(l, p, len, LC_ISO8859_2);
B
Bruce Momjian 已提交
758
}
759 760
static void
mic2latin2(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
761
{
762
	mic2latin(mic, p, len, LC_ISO8859_2);
B
Bruce Momjian 已提交
763
}
764 765
static void
latin32mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
766
{
767
	latin2mic(l, p, len, LC_ISO8859_3);
B
Bruce Momjian 已提交
768
}
769 770
static void
mic2latin3(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
771
{
772
	mic2latin(mic, p, len, LC_ISO8859_3);
B
Bruce Momjian 已提交
773
}
774 775
static void
latin42mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
776
{
777
	latin2mic(l, p, len, LC_ISO8859_4);
B
Bruce Momjian 已提交
778
}
779 780
static void
mic2latin4(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
781
{
782
	mic2latin(mic, p, len, LC_ISO8859_4);
B
Bruce Momjian 已提交
783
}
784

785
#ifdef NOT_USED
786 787
static void
latin52mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
788
{
789
	latin2mic(l, p, len, LC_ISO8859_5);
B
Bruce Momjian 已提交
790
}
791 792
static void
mic2latin5(unsigned char *mic, unsigned char *p, int len)
M
 
Marc G. Fournier 已提交
793
{
794
	mic2latin(mic, p, len, LC_ISO8859_5);
M
 
Marc G. Fournier 已提交
795
}
B
Bruce Momjian 已提交
796

797
#endif
M
 
Marc G. Fournier 已提交
798

799 800 801
/*
 * ASCII ---> MIC
 */
802 803
static void
ascii2mic(unsigned char *l, unsigned char *p, int len)
804
{
805
	int			c1;
806

807 808 809
	while (len-- > 0 && (c1 = *l++))
		*p++ = (c1 & 0x7f);
	*p = '\0';
810 811 812 813 814
}

/*
 * MIC ---> ASCII
 */
815 816
static void
mic2ascii(unsigned char *mic, unsigned char *p, int len)
817
{
818 819
	int			c1;

820
	while (len-- > 0 && (c1 = *mic))
821 822 823 824 825 826
	{
		if (c1 > 0x7f)
			printBogusChar(&mic, &p);
		else
		{						/* should be ASCII */
			*p++ = c1;
827
			mic++;
828 829 830
		}
	}
	*p = '\0';
831 832
}

B
Bruce Momjian 已提交
833
/*
T
Tatsuo Ishii 已提交
834 835 836 837 838 839 840 841 842 843
 * Cyrillic support
 * currently supported Cyrillic encodings:
 *
 * KOI8-R (this is the charset for the mule internal code
 *		for Cyrillic)
 * ISO-8859-5
 * Microsoft's CP1251(windows-1251)
 * Alternativny Variant (MS-DOS CP866)
 */

T
Tatsuo Ishii 已提交
844
/* koi8r2mic: KOI8-R to Mule internal code */
T
Tatsuo Ishii 已提交
845
static void
T
Tatsuo Ishii 已提交
846
koi8r2mic(unsigned char *l, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
847
{
B
Bruce Momjian 已提交
848
	latin2mic(l, p, len, LC_KOI8_R);
T
Tatsuo Ishii 已提交
849 850
}

T
Tatsuo Ishii 已提交
851
/* mic2koi8r: Mule internal code to KOI8-R */
T
Tatsuo Ishii 已提交
852
static void
T
Tatsuo Ishii 已提交
853
mic2koi8r(unsigned char *mic, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
854
{
B
Bruce Momjian 已提交
855
	mic2latin(mic, p, len, LC_KOI8_R);
T
Tatsuo Ishii 已提交
856 857 858 859 860 861 862 863 864 865 866 867
}

/*
 * latin2mic_with_table: a generic single byte charset encoding
 * conversion from a local charset to the mule internal code.
 * with a encoding conversion table.
 * the table is ordered according to the local charset,
 * starting from 128 (0x80). each entry in the table
 * holds the corresponding code point for the mule internal code.
 */
static void
latin2mic_with_table(
B
Bruce Momjian 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
					 unsigned char *l,	/* local charset string (source) */
					 unsigned char *p,	/* pointer to store mule internal
										 * code (destination) */
					 int len,	/* length of l */
					 int lc,	/* leading character of p */
					 unsigned char *tab /* code conversion table */
)
{
	unsigned char c1,
				c2;

	while (len-- > 0 && (c1 = *l++))
	{
		if (c1 < 128)
			*p++ = c1;
		else
		{
			c2 = tab[c1 - 128];
			if (c2)
			{
				*p++ = lc;
				*p++ = c2;
			}
			else
			{
				*p++ = ' ';		/* cannot convert */
			}
		}
	}
	*p = '\0';
T
Tatsuo Ishii 已提交
898 899 900 901 902 903 904
}

/*
 * mic2latin_with_table: a generic single byte charset encoding
 * conversion from the mule internal code to a local charset
 * with a encoding conversion table.
 * the table is ordered according to the second byte of the mule
B
Bruce Momjian 已提交
905
 * internal code starting from 128 (0x80).
T
Tatsuo Ishii 已提交
906 907 908 909 910
 * each entry in the table
 * holds the corresponding code point for the local code.
 */
static void
mic2latin_with_table(
B
Bruce Momjian 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
					 unsigned char *mic,		/* mule internal code
												 * (source) */
					 unsigned char *p,	/* local code (destination) */
					 int len,	/* length of p */
					 int lc,	/* leading character */
					 unsigned char *tab /* code conversion table */
)
{

	unsigned char c1,
				c2;

	while (len-- > 0 && (c1 = *mic++))
	{
		if (c1 < 128)
			*p++ = c1;
		else if (c1 == lc)
		{
			c1 = *mic++;
			len--;
			c2 = tab[c1 - 128];
			if (c2)
				*p++ = c2;
			else
			{
				*p++ = ' ';		/* cannot convert */
			}
		}
		else
		{
			*p++ = ' ';			/* bogus character */
		}
	}
	*p = '\0';
}

/* iso2mic: ISO-8859-5 to Mule internal code */
T
Tatsuo Ishii 已提交
948 949 950
static void
iso2mic(unsigned char *l, unsigned char *p, int len)
{
951
	static unsigned char iso2koi[] = {
B
Bruce Momjian 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xe1, 0xe2, 0xf7, 0xe7, 0xe4, 0xe5, 0xf6, 0xfa,
		0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
		0xf2, 0xf3, 0xf4, 0xf5, 0xe6, 0xe8, 0xe3, 0xfe,
		0xfb, 0xfd, 0xff, 0xf9, 0xf8, 0xfc, 0xe0, 0xf1,
		0xc1, 0xc2, 0xd7, 0xc7, 0xc4, 0xc5, 0xd6, 0xda,
		0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
		0xd2, 0xd3, 0xd4, 0xd5, 0xc6, 0xc8, 0xc3, 0xde,
		0xdb, 0xdd, 0xdf, 0xd9, 0xd8, 0xdc, 0xc0, 0xd1,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};

	latin2mic_with_table(l, p, len, LC_KOI8_R, iso2koi);
T
Tatsuo Ishii 已提交
971 972 973 974 975 976
}

/* mic2iso: Mule internal code to ISO8859-5 */
static void
mic2iso(unsigned char *mic, unsigned char *p, int len)
{
977
	static unsigned char koi2iso[] = {
B
Bruce Momjian 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xee, 0xd0, 0xd1, 0xe6, 0xd4, 0xd5, 0xe4, 0xd3,
		0xe5, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
		0xdf, 0xef, 0xe0, 0xe1, 0xe2, 0xe3, 0xd6, 0xd2,
		0xec, 0xeb, 0xd7, 0xe8, 0xed, 0xe9, 0xe7, 0xea,
		0xce, 0xb0, 0xb1, 0xc6, 0xb4, 0xb5, 0xc4, 0xb3,
		0xc5, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
		0xbf, 0xcf, 0xc0, 0xc1, 0xc2, 0xc3, 0xb6, 0xb2,
		0xcc, 0xcb, 0xb7, 0xc8, 0xcd, 0xc9, 0xc7, 0xca
	};

	mic2latin_with_table(mic, p, len, LC_KOI8_R, koi2iso);
}

/* win2mic: CP1251 to Mule internal code */
T
Tatsuo Ishii 已提交
1000
static void
T
Tatsuo Ishii 已提交
1001
win12512mic(unsigned char *l, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
1002
{
1003
	static unsigned char win2koi[] = {
B
Bruce Momjian 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0xbd, 0x00, 0x00,
		0xb3, 0x00, 0xb4, 0x00, 0x00, 0x00, 0x00, 0xb7,
		0x00, 0x00, 0xb6, 0xa6, 0xad, 0x00, 0x00, 0x00,
		0xa3, 0x00, 0xa4, 0x00, 0x00, 0x00, 0x00, 0xa7,
		0xe1, 0xe2, 0xf7, 0xe7, 0xe4, 0xe5, 0xf6, 0xfa,
		0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
		0xf2, 0xf3, 0xf4, 0xf5, 0xe6, 0xe8, 0xe3, 0xfe,
		0xfb, 0xfd, 0xff, 0xf9, 0xf8, 0xfc, 0xe0, 0xf1,
		0xc1, 0xc2, 0xd7, 0xc7, 0xc4, 0xc5, 0xd6, 0xda,
		0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
		0xd2, 0xd3, 0xd4, 0xd5, 0xc6, 0xc8, 0xc3, 0xde,
		0xdb, 0xdd, 0xdf, 0xd9, 0xd8, 0xdc, 0xc0, 0xd1
	};

	latin2mic_with_table(l, p, len, LC_KOI8_R, win2koi);
T
Tatsuo Ishii 已提交
1023 1024 1025 1026
}

/* mic2win: Mule internal code to CP1251 */
static void
T
Tatsuo Ishii 已提交
1027
mic2win1251(unsigned char *mic, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
1028
{
1029
	static unsigned char koi2win[] = {
B
Bruce Momjian 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0xb8, 0xba, 0x00, 0xb3, 0xbf,
		0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x00, 0x00,
		0x00, 0x00, 0x00, 0xa8, 0xaa, 0x00, 0xb2, 0xaf,
		0x00, 0x00, 0x00, 0x00, 0x00, 0xa5, 0x00, 0x00,
		0xfe, 0xe0, 0xe1, 0xf6, 0xe4, 0xe5, 0xf4, 0xe3,
		0xf5, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
		0xef, 0xff, 0xf0, 0xf1, 0xf2, 0xf3, 0xe6, 0xe2,
		0xfc, 0xfb, 0xe7, 0xf8, 0xfd, 0xf9, 0xf7, 0xfa,
		0xde, 0xc0, 0xc1, 0xd6, 0xc4, 0xc5, 0xd4, 0xc3,
		0xd5, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
		0xcf, 0xdf, 0xd0, 0xd1, 0xd2, 0xd3, 0xc6, 0xc2,
		0xdc, 0xdb, 0xc7, 0xd8, 0xdd, 0xd9, 0xd7, 0xda
	};

	mic2latin_with_table(mic, p, len, LC_KOI8_R, koi2win);
}

/* alt2mic: CP866 to Mule internal code */
T
Tatsuo Ishii 已提交
1052 1053 1054
static void
alt2mic(unsigned char *l, unsigned char *p, int len)
{
1055
	static unsigned char alt2koi[] = {
B
Bruce Momjian 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
		0xe1, 0xe2, 0xf7, 0xe7, 0xe4, 0xe5, 0xf6, 0xfa,
		0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
		0xf2, 0xf3, 0xf4, 0xf5, 0xe6, 0xe8, 0xe3, 0xfe,
		0xfb, 0xfd, 0xff, 0xf9, 0xf8, 0xfc, 0xe0, 0xf1,
		0xc1, 0xc2, 0xd7, 0xc7, 0xc4, 0xc5, 0xd6, 0xda,
		0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0xbd, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xd2, 0xd3, 0xd4, 0xd5, 0xc6, 0xc8, 0xc3, 0xde,
		0xdb, 0xdd, 0xdf, 0xd9, 0xd8, 0xdc, 0xc0, 0xd1,
		0xb3, 0xa3, 0xb4, 0xa4, 0xb7, 0xa7, 0x00, 0x00,
		0xb6, 0xa6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	};

	latin2mic_with_table(l, p, len, LC_KOI8_R, alt2koi);
T
Tatsuo Ishii 已提交
1075 1076 1077 1078 1079 1080
}

/* mic2alt: Mule internal code to CP866 */
static void
mic2alt(unsigned char *mic, unsigned char *p, int len)
{
1081
	static unsigned char koi2alt[] = {
B
Bruce Momjian 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0xf1, 0xf3, 0x00, 0xf9, 0xf5,
		0x00, 0x00, 0x00, 0x00, 0x00, 0xad, 0x00, 0x00,
		0x00, 0x00, 0x00, 0xf0, 0xf2, 0x00, 0xf8, 0xf4,
		0x00, 0x00, 0x00, 0x00, 0x00, 0xbd, 0x00, 0x00,
		0xee, 0xa0, 0xa1, 0xe6, 0xa4, 0xa5, 0xe4, 0xa3,
		0xe5, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
		0xaf, 0xef, 0xe0, 0xe1, 0xe2, 0xe3, 0xa6, 0xa2,
		0xec, 0xeb, 0xa7, 0xe8, 0xed, 0xe9, 0xe7, 0xea,
		0x9e, 0x80, 0x81, 0x96, 0x84, 0x85, 0x94, 0x83,
		0x95, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
		0x8f, 0x9f, 0x90, 0x91, 0x92, 0x93, 0x86, 0x82,
		0x9c, 0x9b, 0x87, 0x98, 0x9d, 0x99, 0x97, 0x9a
	};

	mic2latin_with_table(mic, p, len, LC_KOI8_R, koi2alt);
T
Tatsuo Ishii 已提交
1101 1102 1103 1104 1105 1106
}

/*
 * end of Cyrillic support
 */

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

/*-----------------------------------------------------------------
 * WIN1250
 * Microsoft's CP1250(windows-1250)
 *-----------------------------------------------------------------*/
static void
win12502mic(unsigned char *l, unsigned char *p, int len)
{
	static unsigned char win1250_2_iso88592[] = {
		0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
		0x88, 0x89, 0xA9, 0x8B, 0xA6, 0xAB, 0xAE, 0xAC,
		0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
		0x98, 0x99, 0xB9, 0x9B, 0xB6, 0xBB, 0xBE, 0xBC,
		0xA0, 0xB7, 0xA2, 0xA3, 0xA4, 0xA1, 0x00, 0xA7,
		0xA8, 0x00, 0xAA, 0x00, 0x00, 0xAD, 0x00, 0xAF,
		0xB0, 0x00, 0xB2, 0xB3, 0xB4, 0x00, 0x00, 0x00,
		0xB8, 0xB1, 0xBA, 0x00, 0xA5, 0xBD, 0xB5, 0xBF,
		0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
		0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
		0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
		0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
		0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
		0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
		0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
		0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
	};

	latin2mic_with_table(l, p, len, LC_ISO8859_2, win1250_2_iso88592);
}
static void
mic2win1250(unsigned char *mic, unsigned char *p, int len)
{
	static unsigned char iso88592_2_win1250[] = {
		0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
		0x88, 0x89, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x00,
		0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
		0x98, 0x99, 0x00, 0x9B, 0x00, 0x00, 0x00, 0x00,
		0xA0, 0xA5, 0xA2, 0xA3, 0xA4, 0xBC, 0x8C, 0xA7,
		0xA8, 0x8A, 0xAA, 0x8D, 0x8F, 0xAD, 0x8E, 0xAF,
		0xB0, 0xB9, 0xB2, 0xB3, 0xB4, 0xBE, 0x9C, 0xA1,
		0xB8, 0x9A, 0xBA, 0x9D, 0x9F, 0xBD, 0x9E, 0xBF,
		0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
		0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
		0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
		0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
		0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
		0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
		0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
		0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
	};

	mic2latin_with_table(mic, p, len, LC_ISO8859_2, iso88592_2_win1250);
}
1160

1161 1162
#ifdef UNICODE_CONVERSION

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
/*
 * UNICODE(UTF-8) support
 */

/*
 * ASCII ---> UTF-8
 */
static void
ascii2utf(unsigned char *ascii, unsigned char *utf, int len)
{
B
Bruce Momjian 已提交
1173
	ascii2mic(ascii, utf, len);
1174 1175 1176 1177 1178 1179 1180 1181
}

/*
 * UTF-8 ---> ASCII
 */
static void
utf2ascii(unsigned char *utf, unsigned char *ascii, int len)
{
B
Bruce Momjian 已提交
1182
	mic2ascii(utf, ascii, len);
1183 1184 1185 1186 1187 1188
}

/*
 * ISO8859-1 ---> UTF-8
 */
static void
T
Tatsuo Ishii 已提交
1189
iso8859_1_to_utf(unsigned char *iso, unsigned char *utf, int len)
1190
{
B
Bruce Momjian 已提交
1191
	unsigned short c;
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

	while (len-- > 0 && (c = *iso++))
	{
		if (c < 0x80)
			*utf++ = c;
		else
		{
			*utf++ = (c >> 6) | 0xc0;
			*utf++ = (c & 0x003f) | 0x80;
		}
	}
	*utf = '\0';
}

/*
 * UTF-8 ---> ISO8859-1
 */
static void
T
Tatsuo Ishii 已提交
1210
utf_to_iso8859_1(unsigned char *utf, unsigned char *iso, int len)
1211
{
B
Bruce Momjian 已提交
1212 1213 1214
	unsigned short c,
				c1,
				c2;
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

	while (len > 0 && (c = *utf++))
	{
		if ((c & 0xe0) == 0xc0)
		{
			c1 = c & 0x1f;
			c2 = *utf++ & 0x3f;
			*iso = c1 << 6;
			*iso++ |= c2;
			len -= 2;
		}
		else if ((c & 0xe0) == 0xe0)
			elog(ERROR, "Could not convert UTF-8 to ISO8859-1");
		else
		{
			*iso++ = c;
			len--;
		}
	}
	*iso = '\0';
}

/*
 * comparison routine for bsearch()
 * this routine is intended for UTF-8 -> local code
 */
B
Bruce Momjian 已提交
1241 1242
static int
compare1(const void *p1, const void *p2)
1243
{
B
Bruce Momjian 已提交
1244 1245
	unsigned int v1,
				v2;
1246

B
Bruce Momjian 已提交
1247 1248 1249
	v1 = *(unsigned int *) p1;
	v2 = ((pg_utf_to_local *) p2)->utf;
	return (v1 - v2);
1250 1251 1252 1253 1254 1255
}

/*
 * comparison routine for bsearch()
 * this routine is intended for local code -> UTF-8
 */
B
Bruce Momjian 已提交
1256 1257
static int
compare2(const void *p1, const void *p2)
1258
{
B
Bruce Momjian 已提交
1259 1260
	unsigned int v1,
				v2;
1261

B
Bruce Momjian 已提交
1262 1263 1264
	v1 = *(unsigned int *) p1;
	v2 = ((pg_local_to_utf *) p2)->code;
	return (v1 - v2);
1265 1266 1267 1268
}

/*
 * UTF-8 ---> local code
1269 1270
 *
 * utf: input UTF-8 string. Its length is limited by "len" parameter
B
Bruce Momjian 已提交
1271
 *		or a null terminater.
1272 1273 1274
 * iso: pointer to the output.
 * map: the conversion map.
 * size: the size of the conversion map.
1275 1276 1277
 */
static void
utf_to_local(unsigned char *utf, unsigned char *iso,
B
Bruce Momjian 已提交
1278
			 pg_utf_to_local * map, int size, int len)
1279 1280
{
	unsigned int iutf;
B
Bruce Momjian 已提交
1281
	int			l;
1282 1283
	pg_utf_to_local *p;

B
Bruce Momjian 已提交
1284
	for (; len > 0 && *utf; len -= l)
1285 1286 1287 1288 1289 1290 1291 1292
	{
		l = pg_utf_mblen(utf);
		if (l == 1)
		{
			*iso++ = *utf++;
			continue;
		}
		else if (l == 2)
B
Bruce Momjian 已提交
1293
		{
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
			iutf = *utf++ << 8;
			iutf |= *utf++;
		}
		else
		{
			iutf = *utf++ << 16;
			iutf |= *utf++ << 8;
			iutf |= *utf++;
		}
		p = bsearch(&iutf, map, size,
					sizeof(pg_utf_to_local), compare1);
1305
		if (p == NULL)
1306
		{
1307
			elog(NOTICE, "utf_to_local: could not convert UTF-8 (0x%04x). Ignored", iutf);
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
			continue;
		}
		if (p->code & 0xff000000)
			*iso++ = p->code >> 24;
		if (p->code & 0x00ff0000)
			*iso++ = (p->code & 0x00ff0000) >> 16;
		if (p->code & 0x0000ff00)
			*iso++ = (p->code & 0x0000ff00) >> 8;
		if (p->code & 0x000000ff)
			*iso++ = p->code & 0x000000ff;
	}
	*iso = '\0';
}

T
Tatsuo Ishii 已提交
1322
#ifdef NOT_USED
1323 1324 1325 1326 1327 1328
/*
 * UTF-8 ---> ISO8859-2
 */
static void
utf_to_latin2(unsigned char *utf, unsigned char *iso, int len)
{
B
Bruce Momjian 已提交
1329
	utf_to_local(utf, iso, ULmapISO8859_2, sizeof(ULmapISO8859_2) / sizeof(pg_utf_to_local), len);
1330 1331 1332 1333 1334 1335 1336 1337
}

/*
 * UTF-8 ---> ISO8859-3
 */
static void
utf_to_latin3(unsigned char *utf, unsigned char *iso, int len)
{
B
Bruce Momjian 已提交
1338
	utf_to_local(utf, iso, ULmapISO8859_3, sizeof(ULmapISO8859_3) / sizeof(pg_utf_to_local), len);
1339 1340 1341 1342 1343 1344 1345 1346
}

/*
 * UTF-8 ---> ISO8859-4
 */
static void
utf_to_latin4(unsigned char *utf, unsigned char *iso, int len)
{
B
Bruce Momjian 已提交
1347
	utf_to_local(utf, iso, ULmapISO8859_4, sizeof(ULmapISO8859_4) / sizeof(pg_utf_to_local), len);
1348 1349 1350 1351 1352 1353 1354 1355
}

/*
 * UTF-8 ---> ISO8859-5
 */
static void
utf_to_latin5(unsigned char *utf, unsigned char *iso, int len)
{
B
Bruce Momjian 已提交
1356
	utf_to_local(utf, iso, ULmapISO8859_5, sizeof(ULmapISO8859_5) / sizeof(pg_utf_to_local), len);
1357
}
T
Tatsuo Ishii 已提交
1358 1359
#endif /* NOT_USED */

1360 1361 1362 1363 1364
/*
 * Cyrillic charsets
 */

/*
T
Tatsuo Ishii 已提交
1365
 * UTF-8 --->KOI8-R 
1366 1367
 */
static void
T
Tatsuo Ishii 已提交
1368
utf_to_KOI8R(unsigned char *utf, unsigned char *iso, int len)
1369 1370

{
T
Tatsuo Ishii 已提交
1371
	utf_to_local(utf, iso, ULmap_KOI8R, sizeof(ULmap_KOI8R) / sizeof(pg_utf_to_local), len);
1372 1373 1374
}

/*
T
Tatsuo Ishii 已提交
1375
 * UTF-8 --->WIN1251 
1376 1377
 */
static void
T
Tatsuo Ishii 已提交
1378
utf_to_WIN1251(unsigned char *utf, unsigned char *iso, int len)
1379 1380

{
T
Tatsuo Ishii 已提交
1381
	utf_to_local(utf, iso, ULmap_WIN1251, sizeof(ULmap_WIN1251) / sizeof(pg_utf_to_local), len);
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
}

/*
 * UTF-8 --->ALT 
 */
static void
utf_to_ALT(unsigned char *utf, unsigned char *iso, int len)

{
	utf_to_local(utf, iso, ULmap_ALT, sizeof(ULmap_ALT) / sizeof(pg_utf_to_local), len);
}
1393 1394 1395 1396 1397 1398

/*
 * local code ---> UTF-8
 */
static void
local_to_utf(unsigned char *iso, unsigned char *utf,
B
Bruce Momjian 已提交
1399
			 pg_local_to_utf * map, int size, int encoding, int len)
1400 1401
{
	unsigned int iiso;
B
Bruce Momjian 已提交
1402
	int			l;
1403 1404
	pg_local_to_utf *p;

T
Tatsuo Ishii 已提交
1405
	if (!PG_VALID_ENCODING(encoding))
1406 1407
		elog(ERROR, "Invalid encoding number %d", encoding);

B
Bruce Momjian 已提交
1408
	for (; len > 0 && *iso; len -= l)
1409 1410 1411 1412 1413 1414 1415 1416
	{
		if (*iso < 0x80)
		{
			*utf++ = *iso++;
			l = 1;
			continue;
		}

1417
		l = pg_encoding_mblen(encoding, iso);
1418 1419 1420 1421

		if (l == 1)
			iiso = *iso++;
		else if (l == 2)
B
Bruce Momjian 已提交
1422
		{
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
			iiso = *iso++ << 8;
			iiso |= *iso++;
		}
		else if (l == 3)
		{
			iiso = *iso++ << 16;
			iiso |= *iso++ << 8;
			iiso |= *iso++;
		}
		else if (l == 4)
		{
			iiso = *iso++ << 24;
			iiso |= *iso++ << 16;
			iiso |= *iso++ << 8;
			iiso |= *iso++;
		}
		p = bsearch(&iiso, map, size,
					sizeof(pg_local_to_utf), compare2);
		if (p == NULL)
		{
B
Bruce Momjian 已提交
1443
			elog(NOTICE, "local_to_utf: could not convert (0x%04x) %s to UTF-8. Ignored",
T
Tatsuo Ishii 已提交
1444
				 iiso, (&pg_enc2name_tbl[ encoding ])->name);
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
			continue;
		}
		if (p->utf & 0xff000000)
			*utf++ = p->utf >> 24;
		if (p->utf & 0x00ff0000)
			*utf++ = (p->utf & 0x00ff0000) >> 16;
		if (p->utf & 0x0000ff00)
			*utf++ = (p->utf & 0x0000ff00) >> 8;
		if (p->utf & 0x000000ff)
			*utf++ = p->utf & 0x000000ff;
	}
	*utf = '\0';
}

T
Tatsuo Ishii 已提交
1459
#ifdef NOT_USED
1460 1461 1462 1463 1464 1465
/*
 * ISO-8859-2 ---> UTF-8
 */
static void
latin2_to_utf(unsigned char *iso, unsigned char *utf, int len)
{
T
Tatsuo Ishii 已提交
1466
	local_to_utf(iso, utf, LUmapISO8859_2, sizeof(LUmapISO8859_2) / sizeof(pg_local_to_utf), PG_LATIN2, len);
1467 1468 1469 1470 1471 1472 1473 1474
}

/*
 * ISO-8859-3 ---> UTF-8
 */
static void
latin3_to_utf(unsigned char *iso, unsigned char *utf, int len)
{
T
Tatsuo Ishii 已提交
1475
	local_to_utf(iso, utf, LUmapISO8859_3, sizeof(LUmapISO8859_3) / sizeof(pg_local_to_utf), PG_LATIN3, len);
1476 1477 1478 1479 1480 1481 1482 1483
}

/*
 * ISO-8859-4 ---> UTF-8
 */
static void
latin4_to_utf(unsigned char *iso, unsigned char *utf, int len)
{
T
Tatsuo Ishii 已提交
1484
	local_to_utf(iso, utf, LUmapISO8859_4, sizeof(LUmapISO8859_4) / sizeof(pg_local_to_utf), PG_LATIN4, len);
1485 1486 1487 1488 1489 1490 1491 1492
}

/*
 * ISO-8859-5 ---> UTF-8
 */
static void
latin5_to_utf(unsigned char *iso, unsigned char *utf, int len)
{
T
Tatsuo Ishii 已提交
1493
	local_to_utf(iso, utf, LUmapISO8859_5, sizeof(LUmapISO8859_5) / sizeof(pg_local_to_utf), PG_LATIN5, len);
1494
}
T
Tatsuo Ishii 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
#endif /* NOT_USED */

#define UTF_ISO8859(_id_) \
static void \
utf_to_iso8859_##_id_(unsigned char *utf, unsigned char *iso, int len) \
{ \
	utf_to_local(utf, iso, ULmapISO8859_##_id_, sizeof(ULmapISO8859_##_id_) / sizeof(pg_utf_to_local), len); \
} \
static void \
iso8859_##_id_##_to_utf(unsigned char *iso, unsigned char *utf, int len) \
{ \
	local_to_utf(iso, utf, LUmapISO8859_##_id_, sizeof(LUmapISO8859_##_id_) / sizeof(pg_local_to_utf), PG_LATIN1, len); \
}

UTF_ISO8859(2);
UTF_ISO8859(3);
UTF_ISO8859(4);
UTF_ISO8859(5);
UTF_ISO8859(6);
UTF_ISO8859(7);
UTF_ISO8859(8);
UTF_ISO8859(9);
UTF_ISO8859(10);
UTF_ISO8859(13);
UTF_ISO8859(14);
UTF_ISO8859(15);
UTF_ISO8859(16);
1522

1523
/*
T
Tatsuo Ishii 已提交
1524
 * KOI8-R ---> UTF-8
1525 1526
 */
static void
T
Tatsuo Ishii 已提交
1527
KOI8R_to_utf(unsigned char *iso, unsigned char *utf, int len)
1528
{
T
Tatsuo Ishii 已提交
1529
	local_to_utf(iso, utf, LUmapKOI8R, sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), PG_KOI8R, len);
1530 1531 1532
}

/*
T
Tatsuo Ishii 已提交
1533
 * WIN1251 ---> UTF-8
1534 1535
 */
static void
T
Tatsuo Ishii 已提交
1536
WIN1251_to_utf(unsigned char *iso, unsigned char *utf, int len)
1537
{
T
Tatsuo Ishii 已提交
1538
	local_to_utf(iso, utf, LUmapWIN1251, sizeof(LUmapWIN1251) / sizeof(pg_local_to_utf), PG_WIN1251, len);
1539 1540 1541 1542 1543 1544 1545 1546
}

/*
 * ALT ---> UTF-8
 */
static void
ALT_to_utf(unsigned char *iso, unsigned char *utf, int len)
{
T
Tatsuo Ishii 已提交
1547
	local_to_utf(iso, utf, LUmapALT, sizeof(LUmapALT) / sizeof(pg_local_to_utf), PG_ALT, len);
1548
}
1549 1550 1551 1552 1553 1554 1555
/*
 * UTF-8 ---> EUC_JP
 */
static void
utf_to_euc_jp(unsigned char *utf, unsigned char *euc, int len)

{
B
Bruce Momjian 已提交
1556 1557
	utf_to_local(utf, euc, ULmapEUC_JP,
				 sizeof(ULmapEUC_JP) / sizeof(pg_utf_to_local), len);
1558 1559 1560 1561 1562 1563 1564 1565
}

/*
 * EUC_JP ---> UTF-8
 */
static void
euc_jp_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
1566
	local_to_utf(euc, utf, LUmapEUC_JP,
T
Tatsuo Ishii 已提交
1567
			 sizeof(LUmapEUC_JP) / sizeof(pg_local_to_utf), PG_EUC_JP, len);
1568 1569 1570 1571 1572 1573 1574 1575 1576
}

/*
 * UTF-8 ---> EUC_CN
 */
static void
utf_to_euc_cn(unsigned char *utf, unsigned char *euc, int len)

{
B
Bruce Momjian 已提交
1577 1578
	utf_to_local(utf, euc, ULmapEUC_CN,
				 sizeof(ULmapEUC_CN) / sizeof(pg_utf_to_local), len);
1579 1580 1581 1582 1583 1584 1585 1586 1587
}

/*
 * EUC_CN ---> UTF-8
 */
static void
euc_cn_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
	local_to_utf(euc, utf, LUmapEUC_CN,
T
Tatsuo Ishii 已提交
1588
			 sizeof(LUmapEUC_CN) / sizeof(pg_local_to_utf), PG_EUC_CN, len);
1589 1590 1591 1592 1593 1594 1595 1596 1597
}

/*
 * UTF-8 ---> EUC_KR
 */
static void
utf_to_euc_kr(unsigned char *utf, unsigned char *euc, int len)

{
B
Bruce Momjian 已提交
1598 1599
	utf_to_local(utf, euc, ULmapEUC_KR,
				 sizeof(ULmapEUC_KR) / sizeof(pg_utf_to_local), len);
1600 1601 1602 1603 1604 1605 1606 1607 1608
}

/*
 * EUC_KR ---> UTF-8
 */
static void
euc_kr_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
	local_to_utf(euc, utf, LUmapEUC_KR,
T
Tatsuo Ishii 已提交
1609
			 sizeof(LUmapEUC_KR) / sizeof(pg_local_to_utf), PG_EUC_KR, len);
1610 1611 1612 1613 1614 1615 1616 1617 1618
}

/*
 * UTF-8 ---> EUC_TW
 */
static void
utf_to_euc_tw(unsigned char *utf, unsigned char *euc, int len)

{
B
Bruce Momjian 已提交
1619 1620
	utf_to_local(utf, euc, ULmapEUC_TW,
				 sizeof(ULmapEUC_TW) / sizeof(pg_utf_to_local), len);
1621 1622 1623 1624 1625 1626 1627 1628 1629
}

/*
 * EUC_TW ---> UTF-8
 */
static void
euc_tw_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
	local_to_utf(euc, utf, LUmapEUC_TW,
T
Tatsuo Ishii 已提交
1630
			 sizeof(LUmapEUC_TW) / sizeof(pg_local_to_utf), PG_EUC_TW, len);
1631 1632 1633 1634 1635 1636 1637 1638 1639
}

/*
 * UTF-8 ---> SJIS
 */
static void
utf_to_sjis(unsigned char *utf, unsigned char *euc, int len)

{
B
Bruce Momjian 已提交
1640 1641
	utf_to_local(utf, euc, ULmapSJIS,
				 sizeof(ULmapSJIS) / sizeof(pg_utf_to_local), len);
1642 1643 1644 1645 1646 1647 1648 1649 1650
}

/*
 * SJIS ---> UTF-8
 */
static void
sjis_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
	local_to_utf(euc, utf, LUmapSJIS,
T
Tatsuo Ishii 已提交
1651
				 sizeof(LUmapSJIS) / sizeof(pg_local_to_utf), PG_SJIS, len);
1652 1653 1654 1655 1656 1657 1658 1659 1660
}

/*
 * UTF-8 ---> BIG5
 */
static void
utf_to_big5(unsigned char *utf, unsigned char *euc, int len)

{
B
Bruce Momjian 已提交
1661 1662
	utf_to_local(utf, euc, ULmapBIG5,
				 sizeof(ULmapBIG5) / sizeof(pg_utf_to_local), len);
1663 1664 1665 1666 1667 1668 1669 1670 1671
}

/*
 * BIG5 ---> UTF-8
 */
static void
big5_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
	local_to_utf(euc, utf, LUmapBIG5,
T
Tatsuo Ishii 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
				 sizeof(LUmapBIG5) / sizeof(pg_local_to_utf), PG_BIG5, len);
}

/* ----------
 * Encoding conversion routines
 *
 * WARINIG: must by same order as pg_enc in include/mb/pg_wchar.h!
 * ----------
 */
pg_enconv pg_enconv_tbl[] = 
{
T
Tatsuo Ishii 已提交
1683 1684 1685 1686 1687 1688 1689
	{ PG_SQL_ASCII,	ascii2mic, mic2ascii, ascii2utf, utf2ascii},
	{ PG_EUC_JP,	euc_jp2mic, mic2euc_jp, euc_jp_to_utf, utf_to_euc_jp},
	{ PG_EUC_CN,	euc_cn2mic, mic2euc_cn, euc_cn_to_utf, utf_to_euc_cn},
	{ PG_EUC_KR,	euc_kr2mic, mic2euc_kr, euc_kr_to_utf, utf_to_euc_kr},
	{ PG_EUC_TW,	euc_tw2mic, mic2euc_tw, euc_tw_to_utf, utf_to_euc_tw},
	{ PG_UTF8, 	0, 0, 0, 0},
	{ PG_MULE_INTERNAL, 0, 0, 0, 0},
T
Tatsuo Ishii 已提交
1690 1691 1692 1693 1694
	{ PG_LATIN1,	latin12mic, mic2latin1, iso8859_1_to_utf, utf_to_iso8859_1},
	{ PG_LATIN2,	latin22mic, mic2latin2, iso8859_2_to_utf, utf_to_iso8859_2},
	{ PG_LATIN3,	latin32mic, mic2latin3, iso8859_3_to_utf, utf_to_iso8859_3},
	{ PG_LATIN4,	latin42mic, mic2latin4, iso8859_4_to_utf, utf_to_iso8859_4},
	{ PG_LATIN5,	iso2mic, mic2iso, iso8859_9_to_utf, utf_to_iso8859_9},
1695 1696 1697 1698 1699
	{ PG_LATIN6,	0, 0, iso8859_10_to_utf, utf_to_iso8859_10},
	{ PG_LATIN7,	0, 0, iso8859_13_to_utf, utf_to_iso8859_13},
	{ PG_LATIN8,	0, 0, iso8859_14_to_utf, utf_to_iso8859_14},
	{ PG_LATIN9,	0, 0, iso8859_15_to_utf, utf_to_iso8859_15},
	{ PG_LATIN10,	0, 0, iso8859_16_to_utf, utf_to_iso8859_16},
T
Tatsuo Ishii 已提交
1700 1701 1702
	{ PG_KOI8R,	koi8r2mic, mic2koi8r, KOI8R_to_utf, utf_to_KOI8R},
	{ PG_WIN1251,	win12512mic, mic2win1251, WIN1251_to_utf, utf_to_WIN1251},
	{ PG_ALT,	alt2mic, mic2alt, ALT_to_utf, utf_to_ALT},
T
Tatsuo Ishii 已提交
1703 1704 1705 1706
	{ PG_ISO_8859_5,	0, 0, iso8859_5_to_utf, utf_to_iso8859_5},
	{ PG_ISO_8859_6,	0, 0, iso8859_6_to_utf, utf_to_iso8859_6},
	{ PG_ISO_8859_7,	0, 0, iso8859_7_to_utf, utf_to_iso8859_7},
	{ PG_ISO_8859_8,	0, 0, iso8859_8_to_utf, utf_to_iso8859_8},
1707

T
Tatsuo Ishii 已提交
1708
	{ PG_SJIS,	sjis2mic, mic2sjis, sjis_to_utf, utf_to_sjis},
T
Tatsuo Ishii 已提交
1709
	{ PG_BIG5,	big52mic, mic2big5, big5_to_utf, utf_to_big5},
T
Tatsuo Ishii 已提交
1710
	{ PG_WIN1250,	win12502mic, mic2win1250, 0, 0},
1711 1712 1713 1714
};

#else

T
Tatsuo Ishii 已提交
1715 1716
pg_enconv pg_enconv_tbl[] = 
{
T
Tatsuo Ishii 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
	{ PG_SQL_ASCII,	ascii2mic, mic2ascii, 0, 0},
	{ PG_EUC_JP,	euc_jp2mic, mic2euc_jp, 0, 0},
	{ PG_EUC_CN,	euc_cn2mic, mic2euc_cn, 0, 0},
	{ PG_EUC_KR,	euc_kr2mic, mic2euc_kr, 0, 0},
	{ PG_EUC_TW,	euc_tw2mic, mic2euc_tw, 0, 0},
	{ PG_UTF8, 	0, 0, 0, 0},
	{ PG_MULE_INTERNAL, 0, 0, 0, 0},
	{ PG_LATIN1,	latin12mic, mic2latin1, 0, 0},
	{ PG_LATIN2,	latin22mic, mic2latin2, 0, 0},
	{ PG_LATIN3,	latin32mic, mic2latin3, 0, 0},
	{ PG_LATIN4,	latin42mic, mic2latin4, 0, 0},
	{ PG_LATIN5,	iso2mic, mic2iso, 0, 0},
1729 1730 1731 1732 1733
	{ PG_LATIN6,	0, 0, 0, 0},
	{ PG_LATIN7,	0, 0, 0, 0},
	{ PG_LATIN8,	0, 0, 0, 0},
	{ PG_LATIN9,	0, 0, 0, 0},
	{ PG_LATIN10,	0, 0, 0, 0},
T
Tatsuo Ishii 已提交
1734 1735 1736
	{ PG_KOI8R,	koi8r2mic, mic2koi8r, 0, 0},
	{ PG_WIN1251,	win12512mic, mic2win1251, 0, 0},
	{ PG_ALT,	alt2mic, mic2alt, 0, 0},
T
Tatsuo Ishii 已提交
1737 1738 1739 1740
	{ PG_ISO_8859_5,	0, 0, 0, 0},
	{ PG_ISO_8859_6,	0, 0, 0, 0},
	{ PG_ISO_8859_7,	0, 0, 0, 0},
	{ PG_ISO_8859_8,	0, 0, 0, 0},
T
Tatsuo Ishii 已提交
1741 1742 1743
	{ PG_SJIS,	sjis2mic, mic2sjis, 0, 0},
	{ PG_BIG5,	big52mic, mic2big5, 0, 0},
	{ PG_WIN1250,	win12502mic, mic2win1250, 0, 0},
M
 
Marc G. Fournier 已提交
1744
};
1745

B
Bruce Momjian 已提交
1746
#endif	 /* UNICODE_CONVERSION */