wchar.c 10.0 KB
Newer Older
M
Marc G. Fournier 已提交
1
/*
M
 
Marc G. Fournier 已提交
2
 * conversion functions between pg_wchar and multi-byte streams.
M
Marc G. Fournier 已提交
3
 * Tatsuo Ishii
4
 * $Id: wchar.c,v 1.22 2001/09/23 10:59:45 ishii Exp $
5 6 7
 *
 * WIN1250 client encoding updated by Pavel Behal
 *
M
Marc G. Fournier 已提交
8
 */
9
/* can be used in either frontend or backend */
T
Tatsuo Ishii 已提交
10
#ifdef FRONTEND
11 12
#include "postgres_fe.h"
#define Assert(condition)
T
Tatsuo Ishii 已提交
13
#else
14
#include "postgres.h"
T
Tatsuo Ishii 已提交
15 16
#endif

17 18
#include "mb/pg_wchar.h"

T
Tatsuo Ishii 已提交
19

M
Marc G. Fournier 已提交
20
/*
21 22 23
 * conversion to pg_wchar is done by "table driven."
 * to add an encoding support, define mb2wchar_with_len(), mblen()
 * for the particular encoding. Note that if the encoding is only
24
 * supported in the client, you don't need to define
25
 * mb2wchar_with_len() function (SJIS is the case).
M
Marc G. Fournier 已提交
26
 */
B
Bruce Momjian 已提交
27 28 29 30

/*
 * SQL/ASCII
 */
B
Bruce Momjian 已提交
31
static int	pg_ascii2wchar_with_len
32
			(const unsigned char *from, pg_wchar * to, int len)
B
Bruce Momjian 已提交
33
{
B
Bruce Momjian 已提交
34
	int			cnt = 0;
35

36
	while (len > 0 && *from)
37 38 39
	{
		*to++ = *from++;
		len--;
40
		cnt++;
41 42
	}
	*to = 0;
B
Bruce Momjian 已提交
43
	return (cnt);
B
Bruce Momjian 已提交
44 45
}

46 47
static int
pg_ascii_mblen(const unsigned char *s)
B
Bruce Momjian 已提交
48
{
49
	return (1);
B
Bruce Momjian 已提交
50 51 52 53 54 55
}

/*
 * EUC
 */

B
Bruce Momjian 已提交
56
static int	pg_euc2wchar_with_len
57
			(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
58
{
B
Bruce Momjian 已提交
59
	int			cnt = 0;
60

61
	while (len > 0 && *from)
62
	{
63
		if (*from == SS2 && len >= 2)
64 65 66
		{
			from++;
			*to = 0xff & *from++;
67
			len -= 2;
68
		}
69
		else if (*from == SS3 && len >= 3)
70 71 72 73 74 75
		{
			from++;
			*to = *from++ << 8;
			*to |= 0x3f & *from++;
			len -= 3;
		}
76
		else if ((*from & 0x80) && len >= 2)
77 78 79 80 81 82 83 84 85 86 87
		{
			*to = *from++ << 8;
			*to |= *from++;
			len -= 2;
		}
		else
		{
			*to = *from++;
			len--;
		}
		to++;
88
		cnt++;
89 90
	}
	*to = 0;
B
Bruce Momjian 已提交
91
	return (cnt);
M
Marc G. Fournier 已提交
92 93
}

94 95
static int
pg_euc_mblen(const unsigned char *s)
M
Marc G. Fournier 已提交
96
{
97
	int			len;
98

99 100 101 102 103 104 105 106 107
	if (*s == SS2)
		len = 2;
	else if (*s == SS3)
		len = 3;
	else if (*s & 0x80)
		len = 2;
	else
		len = 1;
	return (len);
M
Marc G. Fournier 已提交
108 109 110
}

/*
111
 * EUC_JP
M
Marc G. Fournier 已提交
112
 */
B
Bruce Momjian 已提交
113
static int	pg_eucjp2wchar_with_len
114
			(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
115
{
B
Bruce Momjian 已提交
116
	return (pg_euc2wchar_with_len(from, to, len));
M
Marc G. Fournier 已提交
117 118
}

119 120
static int
pg_eucjp_mblen(const unsigned char *s)
M
Marc G. Fournier 已提交
121
{
122
	return (pg_euc_mblen(s));
M
Marc G. Fournier 已提交
123 124 125
}

/*
126
 * EUC_KR
M
Marc G. Fournier 已提交
127
 */
B
Bruce Momjian 已提交
128
static int	pg_euckr2wchar_with_len
129
			(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
130
{
B
Bruce Momjian 已提交
131
	return (pg_euc2wchar_with_len(from, to, len));
M
Marc G. Fournier 已提交
132 133
}

134 135
static int
pg_euckr_mblen(const unsigned char *s)
M
Marc G. Fournier 已提交
136
{
137
	return (pg_euc_mblen(s));
M
Marc G. Fournier 已提交
138 139
}

140 141 142
/*
 * EUC_CN
 */
B
Bruce Momjian 已提交
143
static int	pg_euccn2wchar_with_len
144
			(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
145
{
B
Bruce Momjian 已提交
146
	int			cnt = 0;
147

148
	while (len > 0 && *from)
149
	{
150
		if (*from == SS2 && len >= 3)
151 152 153 154
		{
			from++;
			*to = 0x3f00 & (*from++ << 8);
			*to = *from++;
155
			len -= 3;
156
		}
157
		else if (*from == SS3 && len >= 3)
158 159 160 161 162 163
		{
			from++;
			*to = *from++ << 8;
			*to |= 0x3f & *from++;
			len -= 3;
		}
164
		else if ((*from & 0x80) && len >= 2)
165 166 167 168 169 170 171 172 173 174 175
		{
			*to = *from++ << 8;
			*to |= *from++;
			len -= 2;
		}
		else
		{
			*to = *from++;
			len--;
		}
		to++;
176
		cnt++;
177 178
	}
	*to = 0;
B
Bruce Momjian 已提交
179
	return (cnt);
M
Marc G. Fournier 已提交
180 181
}

182 183
static int
pg_euccn_mblen(const unsigned char *s)
184
{
185
	int			len;
186

187
	if (*s & 0x80)
188 189 190 191
		len = 2;
	else
		len = 1;
	return (len);
192 193 194 195 196
}

/*
 * EUC_TW
 */
B
Bruce Momjian 已提交
197
static int	pg_euctw2wchar_with_len
198
			(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
199
{
B
Bruce Momjian 已提交
200
	int			cnt = 0;
201

202
	while (len > 0 && *from)
203
	{
204
		if (*from == SS2 && len >= 4)
205 206 207 208 209
		{
			from++;
			*to = *from++ << 16;
			*to |= *from++ << 8;
			*to |= *from++;
210
			len -= 4;
211
		}
212
		else if (*from == SS3 && len >= 3)
213 214 215 216 217 218
		{
			from++;
			*to = *from++ << 8;
			*to |= 0x3f & *from++;
			len -= 3;
		}
219
		else if ((*from & 0x80) && len >= 2)
220 221 222 223 224 225 226 227 228 229 230
		{
			*to = *from++ << 8;
			*to |= *from++;
			len -= 2;
		}
		else
		{
			*to = *from++;
			len--;
		}
		to++;
231
		cnt++;
232 233
	}
	*to = 0;
B
Bruce Momjian 已提交
234
	return (cnt);
M
Marc G. Fournier 已提交
235 236
}

237 238
static int
pg_euctw_mblen(const unsigned char *s)
239
{
240
	int			len;
241

242 243 244 245 246 247 248 249 250
	if (*s == SS2)
		len = 4;
	else if (*s == SS3)
		len = 3;
	else if (*s & 0x80)
		len = 2;
	else
		len = 1;
	return (len);
251 252
}

M
Marc G. Fournier 已提交
253
/*
254
 * convert UTF-8 string to pg_wchar (UCS-2)
M
Marc G. Fournier 已提交
255 256 257 258
 * caller should allocate enough space for "to"
 * len: length of from.
 * "from" not necessarily null terminated.
 */
259
static int
260
pg_utf2wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
261
{
262 263 264
	unsigned char c1,
				c2,
				c3;
B
Bruce Momjian 已提交
265
	int			cnt = 0;
266

267
	while (len > 0 && *from)
268 269 270 271 272 273
	{
		if ((*from & 0x80) == 0)
		{
			*to = *from++;
			len--;
		}
274
		else if ((*from & 0xe0) == 0xc0 && len >= 2)
275 276 277 278 279
		{
			c1 = *from++ & 0x1f;
			c2 = *from++ & 0x3f;
			*to = c1 << 6;
			*to |= c2;
280
			len -= 2;
281
		}
282
		else if ((*from & 0xe0) == 0xe0 && len >= 3)
283 284 285 286 287 288 289
		{
			c1 = *from++ & 0x0f;
			c2 = *from++ & 0x3f;
			c3 = *from++ & 0x3f;
			*to = c1 << 12;
			*to |= c2 << 6;
			*to |= c3;
290
			len -= 3;
291
		}
292 293 294 295 296
		else
		{
			*to = *from++;
			len--;
		}
297
		to++;
298
		cnt++;
299 300
	}
	*to = 0;
B
Bruce Momjian 已提交
301
	return (cnt);
M
Marc G. Fournier 已提交
302 303
}

304 305 306 307
/*
 * returns the byte length of a UTF-8 word pointed to by s
 */
int
308
pg_utf_mblen(const unsigned char *s)
309
{
310
	int			len = 1;
311

312 313 314 315 316 317 318
	if ((*s & 0x80) == 0)
		len = 1;
	else if ((*s & 0xe0) == 0xc0)
		len = 2;
	else if ((*s & 0xe0) == 0xe0)
		len = 3;
	return (len);
319 320
}

M
Marc G. Fournier 已提交
321 322 323 324 325 326
/*
 * convert mule internal code to pg_wchar
 * caller should allocate enough space for "to"
 * len: length of from.
 * "from" not necessarily null terminated.
 */
327
static int
328
pg_mule2wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
329
{
B
Bruce Momjian 已提交
330
	int			cnt = 0;
331

332
	while (len > 0 && *from)
333
	{
334
		if (IS_LC1(*from) && len >= 2)
335 336 337 338 339
		{
			*to = *from++ << 16;
			*to |= *from++;
			len -= 2;
		}
340
		else if (IS_LCPRV1(*from) && len >= 3)
341 342 343 344 345 346
		{
			from++;
			*to = *from++ << 16;
			*to |= *from++;
			len -= 3;
		}
347
		else if (IS_LC2(*from) && len >= 3)
348 349 350 351 352 353
		{
			*to = *from++ << 16;
			*to |= *from++ << 8;
			*to |= *from++;
			len -= 3;
		}
354
		else if (IS_LCPRV2(*from) && len >= 4)
355 356 357 358 359 360 361 362 363 364 365 366 367
		{
			from++;
			*to = *from++ << 16;
			*to |= *from++ << 8;
			*to |= *from++;
			len -= 4;
		}
		else
		{						/* assume ASCII */
			*to = (unsigned char) *from++;
			len--;
		}
		to++;
368
		cnt++;
369 370
	}
	*to = 0;
B
Bruce Momjian 已提交
371
	return (cnt);
M
Marc G. Fournier 已提交
372 373
}

374 375
int
pg_mule_mblen(const unsigned char *s)
M
Marc G. Fournier 已提交
376
{
377
	int			len;
M
Marc G. Fournier 已提交
378

379 380 381 382 383 384 385 386 387 388 389 390 391
	if (IS_LC1(*s))
		len = 2;
	else if (IS_LCPRV1(*s))
		len = 3;
	else if (IS_LC2(*s))
		len = 3;
	else if (IS_LCPRV2(*s))
		len = 4;
	else
	{							/* assume ASCII */
		len = 1;
	}
	return (len);
M
Marc G. Fournier 已提交
392 393
}

394 395 396
/*
 * ISO8859-1
 */
397
static int
398
pg_latin12wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
M
Marc G. Fournier 已提交
399
{
B
Bruce Momjian 已提交
400
	int			cnt = 0;
401

402
	while (len > 0 && *from)
403
	{
404
		*to++ = *from++;
405
		len--;
406 407
		cnt++;
	}
408
	*to = 0;
B
Bruce Momjian 已提交
409
	return (cnt);
M
Marc G. Fournier 已提交
410 411
}

412 413
static int
pg_latin1_mblen(const unsigned char *s)
M
Marc G. Fournier 已提交
414
{
415
	return (1);
M
Marc G. Fournier 已提交
416 417
}

418 419 420
/*
 * SJIS
 */
421 422
static int
pg_sjis_mblen(const unsigned char *s)
M
Marc G. Fournier 已提交
423
{
424
	int			len;
M
Marc G. Fournier 已提交
425

426 427 428 429 430 431 432 433 434 435 436 437 438
	if (*s >= 0xa1 && *s <= 0xdf)
	{							/* 1 byte kana? */
		len = 1;
	}
	else if (*s > 0x7f)
	{							/* kanji? */
		len = 2;
	}
	else
	{							/* should be ASCII */
		len = 1;
	}
	return (len);
M
Marc G. Fournier 已提交
439 440
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/*
 * Big5
 */
static int
pg_big5_mblen(const unsigned char *s)
{
	int			len;

	if (*s > 0x7f)
	{							/* kanji? */
		len = 2;
	}
	else
	{							/* should be ASCII */
		len = 1;
	}
	return (len);
}

M
 
Marc G. Fournier 已提交
460
pg_wchar_tbl pg_wchar_table[] = {
T
Tatsuo Ishii 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	{pg_ascii2wchar_with_len, pg_ascii_mblen, 1},	/* 0; PG_SQL_ASCII  */
	{pg_eucjp2wchar_with_len, pg_eucjp_mblen, 3},	/* 1; PG_EUC_JP */
	{pg_euccn2wchar_with_len, pg_euccn_mblen, 3},	/* 2; PG_EUC_CN */
	{pg_euckr2wchar_with_len, pg_euckr_mblen, 3},	/* 3; PG_EUC_KR */
	{pg_euctw2wchar_with_len, pg_euctw_mblen, 3},	/* 4; PG_EUC_TW */
	{pg_utf2wchar_with_len, pg_utf_mblen, 3},	/* 5; PG_UNICODE */
	{pg_mule2wchar_with_len, pg_mule_mblen, 3},	/* 6; PG_MULE_INTERNAL */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 7; PG_LATIN1 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 8; PG_LATIN2 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 9; PG_LATIN3 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 10; PG_LATIN4 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 11; PG_LATIN5 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 12; PG_KOI8 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 13; PG_WIN1251 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1},	/* 14; PG_ALT */
	{0, pg_sjis_mblen, 2},				/* 15; PG_SJIS */
	{0, pg_big5_mblen, 2},				/* 17; PG_BIG5 */
	{pg_latin12wchar_with_len, pg_latin1_mblen, 1} 	/* 18; PG_WIN1250 */
479 480 481
};

/* returns the byte length of a word for mule internal code */
482 483
int
pg_mic_mblen(const unsigned char *mbstr)
484
{
485
	return (pg_mule_mblen(mbstr));
486
}
487

B
Bruce Momjian 已提交
488
/*
489 490 491 492 493
 * Returns the byte length of a multi-byte word.
 */
int
pg_encoding_mblen(int encoding, const unsigned char *mbstr)
{
T
Tatsuo Ishii 已提交
494 495 496 497 498 499
	Assert(PG_VALID_ENCODING(encoding));

	return( (encoding >= 0 && 
	         encoding < sizeof(pg_wchar_table)/sizeof(pg_wchar_tbl)) ? 
			((*pg_wchar_table[encoding].mblen) (mbstr)) : 
			((*pg_wchar_table[PG_SQL_ASCII].mblen) (mbstr)));
500
}
T
Tatsuo Ishii 已提交
501

502 503 504 505 506 507 508 509 510 511 512
/*
 * fetch maximum length of a char encoding
 */
int
pg_encoding_max_length(int encoding)
{
	Assert(PG_VALID_ENCODING(encoding));

	return pg_wchar_table[encoding].maxmblen;
}

T
Tatsuo Ishii 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
#ifndef FRONTEND
/*
 * Verify mbstr to make sure that it has a valid character sequence.
 * mbstr is not necessarily NULL terminated. length of mbstr is
 * specified by len. If an error was found, returns an error message.
 * Note that the message is kept in a static buffer, the next invocation
 * might break the message.
 * If no error was found, this function returns NULL.
 */
char *
pg_verifymbstr(const unsigned char *mbstr, int len)
{
	int	l;
	int	i, j;
	static char	buf[256];
	int	slen = 0;

	/* we do not check single byte encodings */
531
	if (pg_encoding_max_length(GetDatabaseEncoding()) <= 1)
T
Tatsuo Ishii 已提交
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
	    return NULL;

	while (len > 0 && *mbstr)
	{
		l = pg_mblen(mbstr);

		/* multi-byte letter? */
		if (l > 1)
		{
		    for (i=1;i<l;i++)
		    {
			if (i > len || *(mbstr+i) == '\0' ||
			    /* we assume that every muti-byte letter
			     * consists of bytes being the 8th bit set
			     */
			    ((*(mbstr+i) & 0x80) == 0))
			{
			    int remains = sizeof(buf);
			    char *p = buf;

			    slen = snprintf(p, remains, "Invalid %s character sequence found (0x",
				     GetDatabaseEncodingName());
			    p += slen;
			    remains -= slen;

			    i = ((*(mbstr+i) & 0x80) == 0)?l:i;

			    for (j=0;j<i;j++)
			    {
				slen = snprintf(p, remains, "%02x",
						*(mbstr+j));
				p += slen;
				remains -= slen;
			    }
			    snprintf(p, remains, ")");
			    return(buf);
			}
		    }
		}
		len -= l;
		mbstr += l;
	}
	return NULL;
}
576 577 578 579 580 581 582 583 584 585

/*
 * fetch maximum length of a char encoding for the current database
 */
int
pg_database_encoding_max_length(void)
{
	return pg_wchar_table[GetDatabaseEncoding()].maxmblen;
}

T
Tatsuo Ishii 已提交
586
#endif