conv.c 21.6 KB
Newer Older
1
/*-------------------------------------------------------------------------
T
Tatsuo Ishii 已提交
2
 *
3
 *	  Utility functions for conversion procs.
4
 *
5 6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
7
 *
8 9
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/utils/mb/conv.c,v 1.39 2002/07/18 02:02:30 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

/*
17
 * convert bogus chars that cannot be represented in the current
T
Tatsuo Ishii 已提交
18
 * encoding system.
B
Bruce Momjian 已提交
19
 */
20 21
void
pg_print_bogus_char(unsigned char **mic, unsigned char **p)
B
Bruce Momjian 已提交
22
{
23 24 25 26 27 28 29 30 31 32 33
	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 已提交
34 35 36 37 38
}

/*
 * EUC_KR ---> MIC
 */
39 40
static void
euc_kr2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
41
{
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	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 已提交
60 61 62 63 64
}

/*
 * MIC ---> EUC_KR
 */
65 66
static void
mic2euc_kr(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
67
{
68 69 70 71 72 73 74 75 76 77 78 79 80 81
	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--;
82
			pg_print_bogus_char(&mic, &p);
83 84 85 86 87 88 89
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
90 91 92 93 94
}

/*
 * EUC_CN ---> MIC
 */
95 96
static void
euc_cn2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
97
{
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	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 已提交
116 117 118 119 120
}

/*
 * MIC ---> EUC_CN
 */
121 122
static void
mic2euc_cn(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
123
{
124 125 126 127 128 129 130 131 132 133 134 135 136 137
	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--;
138
			pg_print_bogus_char(&mic, &p);
139 140 141 142 143 144 145
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
146 147
}

148
#ifdef NOT_USED
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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
/*
 * GB18030 ---> MIC
 * Added by Bill Huang <bhuang@redhat.com>,<bill_huanghb@ybb.ne.jp>
 */
static void
gb180302mic(unsigned char *gb18030, unsigned char *p, int len)
{
	int			c1;
	int			c2;

	while (len > 0 && (c1 = *gb18030++))
	{
		if (c1 < 0x80)
		{						/* should be ASCII */
			len--;
			*p++ = c1;
		}
		else if(c1 >= 0x81 && c1 <= 0xfe)
		{
			c2 = *gb18030++;
			
			if(c2 >= 0x30 && c2 <= 0x69){
				len -= 4;
				*p++ = c1;
				*p++ = c2;
				*p++ = *gb18030++;
				*p++ = *gb18030++;
				*p++ = *gb18030++;
			}
			else if ((c2 >=0x40 && c2 <= 0x7e) ||(c2 >=0x80 && c2 <= 0xfe)){
				len -= 2;
				*p++ = c1;
				*p++ = c2;
				*p++ = *gb18030++;
			}
			else{	/*throw the strange code*/
				len--;
			}
		}
	}
	*p = '\0';
}

/*
 * MIC ---> GB18030
 * Added by Bill Huang <bhuang@redhat.com>,<bill_huanghb@ybb.ne.jp>
 */
static void
mic2gb18030(unsigned char *mic, unsigned char *p, int len)
{
	int			c1;
	int			c2;

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

		if (c1 <= 0x7f) /*ASCII*/
		{					
			*p++ = c1;
		}
		else if (c1 >= 0x81 && c1 <= 0xfe)
		{		
			c2 = *mic++;
			
			if((c2 >= 0x40 && c2 <= 0x7e) || (c2 >= 0x80 && c2 <= 0xfe)){
				*p++ = c1;
				*p++ = c2;
			}
			else if(c2 >= 0x30 && c2 <= 0x39){
				*p++ = c1;
				*p++ = c2;
				*p++ = *mic++;
				*p++ = *mic++;
			}	
			else{
				mic--;
226
				pg_print_bogus_char(&mic, &p);
227
				mic--;
228
				pg_print_bogus_char(&mic, &p);
229 230 231 232
			}		
		}
		else{
			mic--;
233
			pg_print_bogus_char(&mic, &p);
B
Bruce Momjian 已提交
234
		}
235
	}
B
Bruce Momjian 已提交
236
	*p = '\0';
237
}
238
#endif
239

B
Bruce Momjian 已提交
240
/*
M
 
Marc G. Fournier 已提交
241
 * LATINn ---> MIC
B
Bruce Momjian 已提交
242
 */
243 244
static void
latin2mic(unsigned char *l, unsigned char *p, int len, int lc)
B
Bruce Momjian 已提交
245
{
246 247 248 249 250
	int			c1;

	while (len-- > 0 && (c1 = *l++))
	{
		if (c1 > 0x7f)
251
		{						/* Latin? */
252 253 254 255 256
			*p++ = lc;
		}
		*p++ = c1;
	}
	*p = '\0';
B
Bruce Momjian 已提交
257 258 259
}

/*
M
 
Marc G. Fournier 已提交
260
 * MIC ---> LATINn
B
Bruce Momjian 已提交
261
 */
262 263
static void
mic2latin(unsigned char *mic, unsigned char *p, int len, int lc)
B
Bruce Momjian 已提交
264
{
265 266 267 268 269 270 271 272 273 274 275
	int			c1;

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

		if (c1 == lc)
			*p++ = *mic++;
		else if (c1 > 0x7f)
		{
			mic--;
276
			pg_print_bogus_char(&mic, &p);
277 278 279 280 281 282 283
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
284 285
}

286 287
static void
latin12mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
288
{
289
	latin2mic(l, p, len, LC_ISO8859_1);
B
Bruce Momjian 已提交
290
}
291 292
static void
mic2latin1(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
293
{
294
	mic2latin(mic, p, len, LC_ISO8859_1);
B
Bruce Momjian 已提交
295
}
296 297
static void
latin22mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
298
{
299
	latin2mic(l, p, len, LC_ISO8859_2);
B
Bruce Momjian 已提交
300
}
301 302
static void
mic2latin2(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
303
{
304
	mic2latin(mic, p, len, LC_ISO8859_2);
B
Bruce Momjian 已提交
305
}
306 307
static void
latin32mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
308
{
309
	latin2mic(l, p, len, LC_ISO8859_3);
B
Bruce Momjian 已提交
310
}
311 312
static void
mic2latin3(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
313
{
314
	mic2latin(mic, p, len, LC_ISO8859_3);
B
Bruce Momjian 已提交
315
}
316 317
static void
latin42mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
318
{
319
	latin2mic(l, p, len, LC_ISO8859_4);
B
Bruce Momjian 已提交
320
}
321 322
static void
mic2latin4(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
323
{
324
	mic2latin(mic, p, len, LC_ISO8859_4);
B
Bruce Momjian 已提交
325
}
326

327 328 329
/*
 * ASCII ---> MIC
 */
330 331
void
pg_ascii2mic(unsigned char *l, unsigned char *p, int len)
332
{
333
	int			c1;
334

335 336 337
	while (len-- > 0 && (c1 = *l++))
		*p++ = (c1 & 0x7f);
	*p = '\0';
338 339 340 341 342
}

/*
 * MIC ---> ASCII
 */
343 344
void
pg_mic2ascii(unsigned char *mic, unsigned char *p, int len)
345
{
346 347
	int			c1;

348
	while (len-- > 0 && (c1 = *mic))
349 350
	{
		if (c1 > 0x7f)
351
			pg_print_bogus_char(&mic, &p);
352 353 354
		else
		{						/* should be ASCII */
			*p++ = c1;
355
			mic++;
356 357 358
		}
	}
	*p = '\0';
359 360
}

B
Bruce Momjian 已提交
361
/*
T
Tatsuo Ishii 已提交
362 363 364 365 366 367 368 369 370 371
 * 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 已提交
372
/* koi8r2mic: KOI8-R to Mule internal code */
T
Tatsuo Ishii 已提交
373
static void
T
Tatsuo Ishii 已提交
374
koi8r2mic(unsigned char *l, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
375
{
B
Bruce Momjian 已提交
376
	latin2mic(l, p, len, LC_KOI8_R);
T
Tatsuo Ishii 已提交
377 378
}

T
Tatsuo Ishii 已提交
379
/* mic2koi8r: Mule internal code to KOI8-R */
T
Tatsuo Ishii 已提交
380
static void
T
Tatsuo Ishii 已提交
381
mic2koi8r(unsigned char *mic, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
382
{
B
Bruce Momjian 已提交
383
	mic2latin(mic, p, len, LC_KOI8_R);
T
Tatsuo Ishii 已提交
384 385 386 387 388 389 390 391 392 393 394 395
}

/*
 * 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 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
					 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 已提交
426 427 428 429 430 431 432
}

/*
 * 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 已提交
433
 * internal code starting from 128 (0x80).
T
Tatsuo Ishii 已提交
434 435 436 437 438
 * each entry in the table
 * holds the corresponding code point for the local code.
 */
static void
mic2latin_with_table(
B
Bruce Momjian 已提交
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
					 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 已提交
476 477 478
static void
iso2mic(unsigned char *l, unsigned char *p, int len)
{
479
	static unsigned char iso2koi[] = {
B
Bruce Momjian 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
		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 已提交
499 500 501 502 503 504
}

/* mic2iso: Mule internal code to ISO8859-5 */
static void
mic2iso(unsigned char *mic, unsigned char *p, int len)
{
505
	static unsigned char koi2iso[] = {
B
Bruce Momjian 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
		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 已提交
528
static void
T
Tatsuo Ishii 已提交
529
win12512mic(unsigned char *l, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
530
{
531
	static unsigned char win2koi[] = {
B
Bruce Momjian 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		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 已提交
551 552 553 554
}

/* mic2win: Mule internal code to CP1251 */
static void
T
Tatsuo Ishii 已提交
555
mic2win1251(unsigned char *mic, unsigned char *p, int len)
T
Tatsuo Ishii 已提交
556
{
557
	static unsigned char koi2win[] = {
B
Bruce Momjian 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
		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 已提交
580 581 582
static void
alt2mic(unsigned char *l, unsigned char *p, int len)
{
583
	static unsigned char alt2koi[] = {
B
Bruce Momjian 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
		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 已提交
603 604 605 606 607 608
}

/* mic2alt: Mule internal code to CP866 */
static void
mic2alt(unsigned char *mic, unsigned char *p, int len)
{
609
	static unsigned char koi2alt[] = {
B
Bruce Momjian 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		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 已提交
629 630 631 632 633 634
}

/*
 * end of Cyrillic support
 */

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 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

/*-----------------------------------------------------------------
 * 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);
}
688

689 690 691 692
/*
 * comparison routine for bsearch()
 * this routine is intended for UTF-8 -> local code
 */
B
Bruce Momjian 已提交
693 694
static int
compare1(const void *p1, const void *p2)
695
{
B
Bruce Momjian 已提交
696 697
	unsigned int v1,
				v2;
698

B
Bruce Momjian 已提交
699 700 701
	v1 = *(unsigned int *) p1;
	v2 = ((pg_utf_to_local *) p2)->utf;
	return (v1 - v2);
702 703 704 705 706 707
}

/*
 * comparison routine for bsearch()
 * this routine is intended for local code -> UTF-8
 */
B
Bruce Momjian 已提交
708 709
static int
compare2(const void *p1, const void *p2)
710
{
B
Bruce Momjian 已提交
711 712
	unsigned int v1,
				v2;
713

B
Bruce Momjian 已提交
714 715 716
	v1 = *(unsigned int *) p1;
	v2 = ((pg_local_to_utf *) p2)->code;
	return (v1 - v2);
717 718 719 720
}

/*
 * UTF-8 ---> local code
721 722
 *
 * utf: input UTF-8 string. Its length is limited by "len" parameter
B
Bruce Momjian 已提交
723
 *		or a null terminater.
724 725 726
 * iso: pointer to the output.
 * map: the conversion map.
 * size: the size of the conversion map.
727
 */
728 729 730
void
UtfToLocal(unsigned char *utf, unsigned char *iso,
		   pg_utf_to_local *map, int size, int len)
731 732
{
	unsigned int iutf;
B
Bruce Momjian 已提交
733
	int			l;
734 735
	pg_utf_to_local *p;

B
Bruce Momjian 已提交
736
	for (; len > 0 && *utf; len -= l)
737 738 739 740 741 742 743 744
	{
		l = pg_utf_mblen(utf);
		if (l == 1)
		{
			*iso++ = *utf++;
			continue;
		}
		else if (l == 2)
B
Bruce Momjian 已提交
745
		{
746 747 748 749 750 751 752 753 754 755 756
			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);
757
		if (p == NULL)
758
		{
B
Bruce Momjian 已提交
759
			elog(WARNING, "utf_to_local: could not convert UTF-8 (0x%04x). Ignored", iutf);
760 761 762 763 764 765 766 767 768 769 770 771 772 773
			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 已提交
774
#ifdef NOT_USED
775 776 777 778 779
/*
 * Cyrillic charsets
 */

/*
780
 * UTF-8 --->KOI8-R
781 782
 */
static void
T
Tatsuo Ishii 已提交
783
utf_to_KOI8R(unsigned char *utf, unsigned char *iso, int len)
784 785

{
T
Tatsuo Ishii 已提交
786
	utf_to_local(utf, iso, ULmap_KOI8R, sizeof(ULmap_KOI8R) / sizeof(pg_utf_to_local), len);
787 788 789
}

/*
790
 * UTF-8 --->WIN1251
791 792
 */
static void
T
Tatsuo Ishii 已提交
793
utf_to_WIN1251(unsigned char *utf, unsigned char *iso, int len)
794 795

{
T
Tatsuo Ishii 已提交
796
	utf_to_local(utf, iso, ULmap_WIN1251, sizeof(ULmap_WIN1251) / sizeof(pg_utf_to_local), len);
797 798 799
}

/*
800
 * UTF-8 --->ALT
801 802 803 804 805 806 807
 */
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);
}
808

809 810
#endif

811 812 813
/*
 * local code ---> UTF-8
 */
814 815
void
LocalToUtf(unsigned char *iso, unsigned char *utf,
816
			 pg_local_to_utf *map, int size, int encoding, int len)
817 818
{
	unsigned int iiso;
B
Bruce Momjian 已提交
819
	int			l;
820 821
	pg_local_to_utf *p;

T
Tatsuo Ishii 已提交
822
	if (!PG_VALID_ENCODING(encoding))
823 824
		elog(ERROR, "Invalid encoding number %d", encoding);

B
Bruce Momjian 已提交
825
	for (; len > 0 && *iso; len -= l)
826 827 828 829 830 831 832 833
	{
		if (*iso < 0x80)
		{
			*utf++ = *iso++;
			l = 1;
			continue;
		}

834
		l = pg_encoding_mblen(encoding, iso);
835 836 837 838

		if (l == 1)
			iiso = *iso++;
		else if (l == 2)
B
Bruce Momjian 已提交
839
		{
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
			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 已提交
860
			elog(WARNING, "local_to_utf: could not convert (0x%04x) %s to UTF-8. Ignored",
861
				 iiso, (&pg_enc2name_tbl[encoding])->name);
862 863 864 865 866 867 868 869 870 871 872 873 874 875
			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 已提交
876
#ifdef NOT_USED
877
/*
T
Tatsuo Ishii 已提交
878
 * KOI8-R ---> UTF-8
879 880
 */
static void
T
Tatsuo Ishii 已提交
881
KOI8R_to_utf(unsigned char *iso, unsigned char *utf, int len)
882
{
T
Tatsuo Ishii 已提交
883
	local_to_utf(iso, utf, LUmapKOI8R, sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), PG_KOI8R, len);
884 885 886
}

/*
T
Tatsuo Ishii 已提交
887
 * WIN1251 ---> UTF-8
888 889
 */
static void
T
Tatsuo Ishii 已提交
890
WIN1251_to_utf(unsigned char *iso, unsigned char *utf, int len)
891
{
T
Tatsuo Ishii 已提交
892
	local_to_utf(iso, utf, LUmapWIN1251, sizeof(LUmapWIN1251) / sizeof(pg_local_to_utf), PG_WIN1251, len);
893 894 895 896 897 898 899 900
}

/*
 * ALT ---> UTF-8
 */
static void
ALT_to_utf(unsigned char *iso, unsigned char *utf, int len)
{
T
Tatsuo Ishii 已提交
901
	local_to_utf(iso, utf, LUmapALT, sizeof(LUmapALT) / sizeof(pg_local_to_utf), PG_ALT, len);
902
}
903

B
Bruce Momjian 已提交
904 905 906 907 908 909 910
/*
 * UTF-8 ---> WIN1250
 */
static void
utf_to_win1250(unsigned char *utf, unsigned char *euc, int len)

{
B
Bruce Momjian 已提交
911 912
		utf_to_local(utf, euc, ULmapWIN1250,
								 sizeof(ULmapWIN1250) / sizeof(pg_utf_to_local), len);
B
Bruce Momjian 已提交
913 914 915 916 917 918 919 920
}

/*
 * WIN1250 ---> UTF-8
 */
static void
win1250_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
B
Bruce Momjian 已提交
921 922
		local_to_utf(euc, utf, LUmapWIN1250,
						  sizeof(LUmapWIN1250) / sizeof(pg_local_to_utf), PG_WIN1250, len);
B
Bruce Momjian 已提交
923 924 925 926 927 928 929 930 931
}

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

{
B
Bruce Momjian 已提交
932 933
		utf_to_local(utf, euc, ULmapWIN1256,
								 sizeof(ULmapWIN1256) / sizeof(pg_utf_to_local), len);
B
Bruce Momjian 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
}

/*
 * WIN1256 ---> UTF-8
 */
static void
win1256_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
	local_to_utf(euc, utf, LUmapWIN1256,
			  sizeof(LUmapWIN1256) / sizeof(pg_local_to_utf), PG_WIN1256, len);
}

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

{
	utf_to_local(utf, euc, ULmapWIN874,
				 sizeof(ULmapWIN874) / sizeof(pg_utf_to_local), len);
}

/*
 * WIN874 ---> UTF-8
 */
static void
win874_to_utf(unsigned char *euc, unsigned char *utf, int len)
{
	local_to_utf(euc, utf, LUmapWIN874,
			  sizeof(LUmapWIN874) / sizeof(pg_local_to_utf), PG_WIN874, len);
}

967
#endif