conv.c 8.5 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
5
 * $Id: conv.c,v 1.4 1998/12/14 04:59:58 momjian Exp $
B
Bruce Momjian 已提交
6 7 8 9
 */
#include <stdio.h>
#include <string.h>

M
 
Marc G. Fournier 已提交
10
#include "mb/pg_wchar.h"
B
Bruce Momjian 已提交
11 12 13 14 15

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

/*
 * SJIS ---> MIC
 */
35 36
static void
sjis2mic(unsigned char *sjis, unsigned char *p, int len)
B
Bruce Momjian 已提交
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
	int			c1,
				c2;

	while (len > 0 && (c1 = *sjis++))
	{
		if (c1 >= 0xa1 && c1 <= 0xdf)
		{						/* 1 byte kana? */
			len--;
			*p++ = LC_JISX0201K;
			*p++ = c1;
		}
		else if (c1 > 0x7f)
		{						/* kanji? */
			c2 = *sjis++;
			len -= 2;
			*p++ = LC_JISX0208;
			*p++ = ((c1 & 0x3f) << 1) + 0x9f + (c2 > 0x9e);
			*p++ = c2 + ((c2 > 0x9e) ? 2 : 0x60) + (c2 < 0x80);
		}
		else
		{						/* should be ASCII */
			len--;
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
64 65 66 67 68
}

/*
 * MIC ---> SJIS
 */
69 70
static void
mic2sjis(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
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
	int			c1,
				c2;

	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++;
			*p++ = ((c1 - 0xa1) >> 1) + ((c1 < 0xdf) ? 0x81 : 0xc1);
			*p++ = c2 - ((c1 & 1) ? ((c2 < 0xe0) ? 0x61 : 0x60) : 2);
		}
		else if (c1 > 0x7f)
		{						/* cannot convert to SJIS! */
			mic--;
			printBogusChar(&mic, &p);
		}
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
	}
	*p = '\0';
B
Bruce Momjian 已提交
99 100 101 102 103
}

/*
 * EUC_JP ---> MIC
 */
104 105
static void
euc_jp2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
106
{
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	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 已提交
138 139 140 141 142
}

/*
 * MIC ---> EUC_JP
 */
143 144
static void
mic2euc_jp(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
145
{
146 147 148 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
	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 已提交
179 180 181 182 183
}

/*
 * EUC_KR ---> MIC
 */
184 185
static void
euc_kr2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
186
{
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	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 已提交
205 206 207 208 209
}

/*
 * MIC ---> EUC_KR
 */
210 211
static void
mic2euc_kr(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
212
{
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	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 已提交
235 236 237 238 239
}

/*
 * EUC_CN ---> MIC
 */
240 241
static void
euc_cn2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
242
{
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	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 已提交
261 262 263 264 265
}

/*
 * MIC ---> EUC_CN
 */
266 267
static void
mic2euc_cn(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
268
{
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	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 已提交
291 292 293 294 295
}

/*
 * EUC_TW ---> MIC
 */
296 297
static void
euc_tw2mic(unsigned char *euc, unsigned char *p, int len)
B
Bruce Momjian 已提交
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
	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 已提交
333 334 335 336 337
}

/*
 * MIC ---> EUC_TW
 */
338 339
static void
mic2euc_tw(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
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
	int			c1;

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

		if (c1 == LC_CNS11643_1 || c1 == LC_CNS11643_2)
		{
			*p++ = *mic++;
			*p++ = *mic++;
		}
		else if (c1 == 0x9d)
		{						/* LCPRV2? */
			*p++ = SS2;
			*p++ = c1 - LC_CNS11643_3 + 0xa3;
			*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 已提交
370 371 372
}

/*
M
 
Marc G. Fournier 已提交
373
 * LATINn ---> MIC
B
Bruce Momjian 已提交
374
 */
375 376
static void
latin2mic(unsigned char *l, unsigned char *p, int len, int lc)
B
Bruce Momjian 已提交
377
{
378 379 380 381 382 383 384 385 386 387 388
	int			c1;

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

/*
M
 
Marc G. Fournier 已提交
392
 * MIC ---> LATINn
B
Bruce Momjian 已提交
393
 */
394 395
static void
mic2latin(unsigned char *mic, unsigned char *p, int len, int lc)
B
Bruce Momjian 已提交
396
{
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	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 已提交
416 417
}

418 419
static void
latin12mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
420
{
421
	latin2mic(l, p, len, LC_ISO8859_1);
B
Bruce Momjian 已提交
422
}
423 424
static void
mic2latin1(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
425
{
426
	mic2latin(mic, p, len, LC_ISO8859_1);
B
Bruce Momjian 已提交
427
}
428 429
static void
latin22mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
430
{
431
	latin2mic(l, p, len, LC_ISO8859_2);
B
Bruce Momjian 已提交
432
}
433 434
static void
mic2latin2(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
435
{
436
	mic2latin(mic, p, len, LC_ISO8859_2);
B
Bruce Momjian 已提交
437
}
438 439
static void
latin32mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
440
{
441
	latin2mic(l, p, len, LC_ISO8859_3);
B
Bruce Momjian 已提交
442
}
443 444
static void
mic2latin3(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
445
{
446
	mic2latin(mic, p, len, LC_ISO8859_3);
B
Bruce Momjian 已提交
447
}
448 449
static void
latin42mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
450
{
451
	latin2mic(l, p, len, LC_ISO8859_4);
B
Bruce Momjian 已提交
452
}
453 454
static void
mic2latin4(unsigned char *mic, unsigned char *p, int len)
B
Bruce Momjian 已提交
455
{
456
	mic2latin(mic, p, len, LC_ISO8859_4);
B
Bruce Momjian 已提交
457
}
458 459
static void
latin52mic(unsigned char *l, unsigned char *p, int len)
B
Bruce Momjian 已提交
460
{
461
	latin2mic(l, p, len, LC_ISO8859_5);
B
Bruce Momjian 已提交
462
}
463 464
static void
mic2latin5(unsigned char *mic, unsigned char *p, int len)
M
 
Marc G. Fournier 已提交
465
{
466
	mic2latin(mic, p, len, LC_ISO8859_5);
M
 
Marc G. Fournier 已提交
467 468
}

469 470 471
/*
 * ASCII ---> MIC
 */
472 473
static void
ascii2mic(unsigned char *l, unsigned char *p, int len)
474
{
475
	int			c1;
476

477 478 479
	while (len-- > 0 && (c1 = *l++))
		*p++ = (c1 & 0x7f);
	*p = '\0';
480 481 482 483 484
}

/*
 * MIC ---> ASCII
 */
485 486
static void
mic2ascii(unsigned char *mic, unsigned char *p, int len)
487
{
488 489
	int			c1;

490
	while (len-- > 0 && (c1 = *mic))
491 492 493 494 495 496 497
	{
		if (c1 > 0x7f)
			printBogusChar(&mic, &p);
		else
		{						/* should be ASCII */
			*p++ = c1;
		}
498
		mic++;
499 500
	}
	*p = '\0';
501 502
}

M
 
Marc G. Fournier 已提交
503
pg_encoding_conv_tbl pg_conv_tbl[] = {
504 505 506 507 508 509 510 511 512 513 514 515 516 517
	{SQL_ASCII, "SQL_ASCII", 0, ascii2mic, mic2ascii},	/* SQL/ACII */
	{EUC_JP, "EUC_JP", 0, euc_jp2mic, mic2euc_jp},		/* EUC_JP */
	{EUC_CN, "EUC_CN", 0, euc_cn2mic, mic2euc_cn},		/* EUC_CN */
	{EUC_KR, "EUC_KR", 0, euc_kr2mic, mic2euc_kr},		/* EUC_KR */
	{EUC_TW, "EUC_TW", 0, euc_tw2mic, mic2euc_tw},		/* EUC_TW */
	{UNICODE, "UNICODE", 0, 0, 0},		/* UNICODE */
	{MULE_INTERNAL, "MULE_INTERNAL", 0, 0, 0},	/* MULE_INTERNAL */
	{LATIN1, "LATIN1", 0, latin12mic, mic2latin1},		/* ISO 8859 Latin 1 */
	{LATIN2, "LATIN2", 0, latin22mic, mic2latin2},		/* ISO 8859 Latin 2 */
	{LATIN3, "LATIN3", 0, latin32mic, mic2latin3},		/* ISO 8859 Latin 3 */
	{LATIN4, "LATIN4", 0, latin42mic, mic2latin4},		/* ISO 8859 Latin 4 */
	{LATIN5, "LATIN5", 0, latin52mic, mic2latin5},		/* ISO 8859 Latin 5 */
	{SJIS, "SJIS", 1, sjis2mic, mic2sjis},		/* SJIS */
	{-1, "", 0, 0, 0}			/* end mark */
M
 
Marc G. Fournier 已提交
518
};