encnames.c 9.2 KB
Newer Older
Tatsuo Ishii's avatar
Tatsuo Ishii committed
1 2 3 4
/*
 * Encoding names and routines for work with it. All
 * in this file is shared bedween FE and BE.
 *
Bruce Momjian's avatar
Bruce Momjian committed
5
 * $PostgreSQL: pgsql/src/backend/utils/mb/encnames.c,v 1.21 2004/12/02 22:14:36 momjian Exp $
Tatsuo Ishii's avatar
Tatsuo Ishii committed
6 7 8 9 10 11 12 13 14 15
 */
#ifdef FRONTEND
#include "postgres_fe.h"
#define Assert(condition)
#else
#include "postgres.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#endif

16
#ifndef WIN32_CLIENT_ONLY
Tatsuo Ishii's avatar
Tatsuo Ishii committed
17
#include <unistd.h>
18
#endif
Tatsuo Ishii's avatar
Tatsuo Ishii committed
19 20 21 22 23

#include "mb/pg_wchar.h"
#include <ctype.h>

/* ----------
24
 * All encoding names, sorted:		 *** A L P H A B E T I C ***
Tatsuo Ishii's avatar
Tatsuo Ishii committed
25
 *
26
 * All names must be without irrelevant chars, search routines use
Tatsuo Ishii's avatar
Tatsuo Ishii committed
27 28 29
 * isalnum() chars only. It means ISO-8859-1, iso_8859-1 and Iso8859_1
 * are always converted to 'iso88591'. All must be lower case.
 *
30
 * The table doesn't contain 'cs' aliases (like csISOLatin1). It's needful?
Tatsuo Ishii's avatar
Tatsuo Ishii committed
31 32 33
 *
 * Karel Zak, Aug 2001
 * ----------
34
 */
35
pg_encname	pg_encname_tbl[] =
Tatsuo Ishii's avatar
Tatsuo Ishii committed
36
{
Bruce Momjian's avatar
Bruce Momjian committed
37 38 39
	{
		"abc", PG_TCVN
	},							/* alias for TCVN */
40 41
	{
		"alt", PG_ALT
42
	},							/* IBM866 */
43 44
	{
		"big5", PG_BIG5
45
	},							/* Big5; Chinese for Taiwan multibyte set */
46 47 48 49 50 51 52
	{
		"euccn", PG_EUC_CN
	},							/* EUC-CN; Extended Unix Code for
								 * simplified Chinese */
	{
		"eucjp", PG_EUC_JP
	},							/* EUC-JP; Extended UNIX Code fixed Width
53
								 * for Japanese, standard OSF */
54 55
	{
		"euckr", PG_EUC_KR
Bruce Momjian's avatar
Bruce Momjian committed
56 57
	},							/* EUC-KR; Extended Unix Code for Korean ,
								 * KS X 1001 standard */
58 59 60
	{
		"euctw", PG_EUC_TW
	},							/* EUC-TW; Extended Unix Code for
Bruce Momjian's avatar
Bruce Momjian committed
61
								 *
62
								 * traditional Chinese */
63 64 65
	{
		"gb18030", PG_GB18030
	},							/* GB18030;GB18030 */
Bruce Momjian's avatar
Bruce Momjian committed
66 67 68 69
	{
		"gbk", PG_GBK
	},							/* GBK; Chinese Windows CodePage 936
								 * simplified Chinese */
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	{
		"iso88591", PG_LATIN1
	},							/* ISO-8859-1; RFC1345,KXS2 */
	{
		"iso885910", PG_LATIN6
	},							/* ISO-8859-10; RFC1345,KXS2 */
	{
		"iso885913", PG_LATIN7
	},							/* ISO-8859-13; RFC1345,KXS2 */
	{
		"iso885914", PG_LATIN8
	},							/* ISO-8859-14; RFC1345,KXS2 */
	{
		"iso885915", PG_LATIN9
	},							/* ISO-8859-15; RFC1345,KXS2 */
	{
		"iso885916", PG_LATIN10
	},							/* ISO-8859-16; RFC1345,KXS2 */
	{
		"iso88592", PG_LATIN2
	},							/* ISO-8859-2; RFC1345,KXS2 */
	{
		"iso88593", PG_LATIN3
	},							/* ISO-8859-3; RFC1345,KXS2 */
	{
		"iso88594", PG_LATIN4
	},							/* ISO-8859-4; RFC1345,KXS2 */
	{
		"iso88595", PG_ISO_8859_5
	},							/* ISO-8859-5; RFC1345,KXS2 */
	{
		"iso88596", PG_ISO_8859_6
	},							/* ISO-8859-6; RFC1345,KXS2 */
	{
		"iso88597", PG_ISO_8859_7
	},							/* ISO-8859-7; RFC1345,KXS2 */
	{
		"iso88598", PG_ISO_8859_8
	},							/* ISO-8859-8; RFC1345,KXS2 */
	{
		"iso88599", PG_LATIN5
	},							/* ISO-8859-9; RFC1345,KXS2 */
Bruce Momjian's avatar
Bruce Momjian committed
112 113 114 115
	{
		"johab", PG_JOHAB
	},							/* JOHAB; Extended Unix Code for
								 * simplified Chinese */
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	{
		"koi8", PG_KOI8R
	},							/* _dirty_ alias for KOI8-R (backward
								 * compatibility) */
	{
		"koi8r", PG_KOI8R
	},							/* KOI8-R; RFC1489 */
	{
		"latin1", PG_LATIN1
	},							/* alias for ISO-8859-1 */
	{
		"latin10", PG_LATIN10
	},							/* alias for ISO-8859-16 */
	{
		"latin2", PG_LATIN2
	},							/* alias for ISO-8859-2 */
	{
		"latin3", PG_LATIN3
	},							/* alias for ISO-8859-3 */
	{
		"latin4", PG_LATIN4
	},							/* alias for ISO-8859-4 */
	{
		"latin5", PG_LATIN5
	},							/* alias for ISO-8859-9 */
	{
		"latin6", PG_LATIN6
	},							/* alias for ISO-8859-10 */
	{
		"latin7", PG_LATIN7
	},							/* alias for ISO-8859-13 */
	{
		"latin8", PG_LATIN8
	},							/* alias for ISO-8859-14 */
	{
		"latin9", PG_LATIN9
	},							/* alias for ISO-8859-15 */
	{
		"mskanji", PG_SJIS
	},							/* alias for Shift_JIS */
	{
		"muleinternal", PG_MULE_INTERNAL
	},
	{
		"shiftjis", PG_SJIS
	},							/* Shift_JIS; JIS X 0202-1991 */
	{
		"sjis", PG_SJIS
	},							/* alias for Shift_JIS */
	{
		"sqlascii", PG_SQL_ASCII
	},
Bruce Momjian's avatar
Bruce Momjian committed
168 169 170 171
	{
		"tcvn", PG_TCVN
	},							/* TCVN; Vietnamese TCVN-5712 */
	{
Bruce Momjian's avatar
Bruce Momjian committed
172
		"tcvn5712", PG_TCVN
Bruce Momjian's avatar
Bruce Momjian committed
173 174 175 176
	},							/* alias for TCVN */
	{
		"uhc", PG_UHC
	},							/* UHC; Korean Windows CodePage 949 */
177 178 179 180 181 182
	{
		"unicode", PG_UTF8
	},							/* alias for UTF-8 */
	{
		"utf8", PG_UTF8
	},							/* UTF-8; RFC2279 */
Bruce Momjian's avatar
Bruce Momjian committed
183 184 185
	{
		"vscii", PG_TCVN
	},							/* alias for TCVN */
186 187 188 189 190 191 192 193 194 195
	{
		"win", PG_WIN1251
	},							/* _dirty_ alias for windows-1251
								 * (backward compatibility) */
	{
		"win1250", PG_WIN1250
	},							/* alias for Windows-1250 */
	{
		"win1251", PG_WIN1251
	},							/* alias for Windows-1251 */
Bruce Momjian's avatar
Bruce Momjian committed
196 197 198
	{
		"win1252", PG_WIN1252
	},							/* alias for Windows-1252 */
Bruce Momjian's avatar
Bruce Momjian committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	{
		"win1256", PG_WIN1256
	},							/* alias for Windows-1256 */
	{
		"win1258", PG_TCVN
	},							/* alias for Windows-1258 */
	{
		"win874", PG_WIN874
	},							/* alias for Windows-874 */
	{
		"win932", PG_SJIS
	},							/* alias for Shift_JIS */
	{
		"win936", PG_GBK
	},							/* alias for GBK */
	{
		"win949", PG_UHC
	},							/* alias for UHC */
	{
		"win950", PG_BIG5
	},							/* alias for BIG5 */
220 221 222 223 224 225
	{
		"windows1250", PG_WIN1250
	},							/* Windows-1251; Microsoft */
	{
		"windows1251", PG_WIN1251
	},							/* Windows-1251; Microsoft */
Bruce Momjian's avatar
Bruce Momjian committed
226 227 228
	{
		"windows1252", PG_WIN1252
	},							/* Windows-1252; Microsoft */
Bruce Momjian's avatar
Bruce Momjian committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	{
		"windows1256", PG_WIN1256
	},							/* Windows-1256; Microsoft */
	{
		"windows1258", PG_TCVN
	},							/* Windows-1258; Microsoft */
	{
		"windows874", PG_WIN874
	},							/* Windows-874; Microsoft */
	{
		"windows932", PG_SJIS
	},							/* alias for Shift_JIS */
	{
		"windows936", PG_GBK
	},							/* alias for GBK */
	{
		"windows949", PG_UHC
	},							/* alias for UHC */
	{
		"windows950", PG_BIG5
	},							/* alias for BIG5 */
250 251 252
	{
		NULL, 0
	}							/* last */
Tatsuo Ishii's avatar
Tatsuo Ishii committed
253 254 255
};

unsigned int pg_encname_tbl_sz = \
256
sizeof(pg_encname_tbl) / sizeof(pg_encname_tbl[0]) - 1;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
257 258

/* ----------
259 260
 * These are "official" encoding names.
 * XXX must be sorted by the same order as pg_enc type (see mb/pg_wchar.h)
Tatsuo Ishii's avatar
Tatsuo Ishii committed
261 262 263 264
 * ----------
 */
pg_enc2name pg_enc2name_tbl[] =
{
265 266
	{
		"SQL_ASCII", PG_SQL_ASCII
267
	},
268 269 270 271 272 273 274 275 276 277 278 279
	{
		"EUC_JP", PG_EUC_JP
	},
	{
		"EUC_CN", PG_EUC_CN
	},
	{
		"EUC_KR", PG_EUC_KR
	},
	{
		"EUC_TW", PG_EUC_TW
	},
Bruce Momjian's avatar
Bruce Momjian committed
280 281 282
	{
		"JOHAB", PG_JOHAB
	},
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	{
		"UNICODE", PG_UTF8
	},
	{
		"MULE_INTERNAL", PG_MULE_INTERNAL
	},
	{
		"LATIN1", PG_LATIN1
	},
	{
		"LATIN2", PG_LATIN2
	},
	{
		"LATIN3", PG_LATIN3
	},
	{
		"LATIN4", PG_LATIN4
	},
	{
		"LATIN5", PG_LATIN5
	},
	{
		"LATIN6", PG_LATIN6
	},
	{
		"LATIN7", PG_LATIN7
	},
	{
		"LATIN8", PG_LATIN8
	},
	{
		"LATIN9", PG_LATIN9
	},
	{
		"LATIN10", PG_LATIN10
	},
Bruce Momjian's avatar
Bruce Momjian committed
319 320 321 322 323 324 325 326 327
	{
		"WIN1256", PG_WIN1256
	},
	{
		"TCVN", PG_TCVN
	},
	{
		"WIN874", PG_WIN874
	},
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	{
		"KOI8", PG_KOI8R
	},
	{
		"WIN", PG_WIN1251
	},
	{
		"ALT", PG_ALT
	},
	{
		"ISO_8859_5", PG_ISO_8859_5
	},
	{
		"ISO_8859_6", PG_ISO_8859_6
	},
	{
		"ISO_8859_7", PG_ISO_8859_7
	},
	{
		"ISO_8859_8", PG_ISO_8859_8
	},
349 350 351
	{
		"WIN1250", PG_WIN1250
	},
Bruce Momjian's avatar
Bruce Momjian committed
352 353 354
	{
		"WIN1252", PG_WIN1252
	},
355 356 357 358 359 360
	{
		"SJIS", PG_SJIS
	},
	{
		"BIG5", PG_BIG5
	},
Bruce Momjian's avatar
Bruce Momjian committed
361 362 363 364 365 366
	{
		"GBK", PG_GBK
	},
	{
		"UHC", PG_UHC
	},
367 368
	{
		"GB18030", PG_GB18030
369
	}
Tatsuo Ishii's avatar
Tatsuo Ishii committed
370 371 372 373 374 375 376 377 378
};

/* ----------
 * Encoding checks, for error returns -1 else encoding id
 * ----------
 */
int
pg_valid_client_encoding(const char *name)
{
379
	int			enc;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
380 381 382 383

	if ((enc = pg_char_to_encoding(name)) < 0)
		return -1;

384
	if (!PG_VALID_FE_ENCODING(enc))
Tatsuo Ishii's avatar
Tatsuo Ishii committed
385 386 387 388 389 390 391 392
		return -1;

	return enc;
}

int
pg_valid_server_encoding(const char *name)
{
393
	int			enc;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
394 395 396 397

	if ((enc = pg_char_to_encoding(name)) < 0)
		return -1;

398
	if (!PG_VALID_BE_ENCODING(enc))
Tatsuo Ishii's avatar
Tatsuo Ishii committed
399 400 401 402 403 404 405 406 407 408 409 410
		return -1;

	return enc;
}

/* ----------
 * Remove irrelevant chars from encoding name
 * ----------
 */
static char *
clean_encoding_name(char *key, char *newkey)
{
411 412
	char	   *p,
			   *np;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
413

414
	for (p = key, np = newkey; *p != '\0'; p++)
Tatsuo Ishii's avatar
Tatsuo Ishii committed
415 416
	{
		if (isalnum((unsigned char) *p))
417 418 419 420 421 422
		{
			if (*p >= 'A' && *p <= 'Z')
				*np++ = *p + 'a' - 'A';
			else
				*np++ = *p;
		}
Tatsuo Ishii's avatar
Tatsuo Ishii committed
423 424 425 426 427 428 429 430 431 432 433 434
	}
	*np = '\0';
	return newkey;
}

/* ----------
 * Search encoding by encoding name
 * ----------
 */
pg_encname *
pg_char_to_encname_struct(const char *name)
{
435 436 437 438 439
	unsigned int nel = pg_encname_tbl_sz;
	pg_encname *base = pg_encname_tbl,
			   *last = base + nel - 1,
			   *position;
	int			result;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
440
	char		buff[NAMEDATALEN],
441
			   *key;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
442

443
	if (name == NULL || *name == '\0')
444
		return NULL;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
445 446 447 448

	if (strlen(name) > NAMEDATALEN)
	{
#ifdef FRONTEND
449
		fprintf(stderr, "encoding name too long\n");
Tatsuo Ishii's avatar
Tatsuo Ishii committed
450 451
		return NULL;
#else
452 453 454
		ereport(ERROR,
				(errcode(ERRCODE_NAME_TOO_LONG),
				 errmsg("encoding name too long")));
Tatsuo Ishii's avatar
Tatsuo Ishii committed
455 456 457 458 459
#endif
	}
	key = clean_encoding_name((char *) name, buff);

	while (last >= base)
460
	{
Tatsuo Ishii's avatar
Tatsuo Ishii committed
461 462
		position = base + ((last - base) >> 1);
		result = key[0] - position->name[0];
463

Tatsuo Ishii's avatar
Tatsuo Ishii committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
		if (result == 0)
		{
			result = strcmp(key, position->name);
			if (result == 0)
				return position;
		}
		if (result < 0)
			last = position - 1;
		else
			base = position + 1;
	}
	return NULL;
}

/*
 * Returns encoding or -1 for error
 */
int
pg_char_to_encoding(const char *s)
{
	pg_encname *p = NULL;

	if (!s)
		return (-1);

	p = pg_char_to_encname_struct(s);
	return p ? p->encoding : -1;
}

#ifndef FRONTEND
Datum
PG_char_to_encoding(PG_FUNCTION_ARGS)
{
	Name		s = PG_GETARG_NAME(0);
498

Tatsuo Ishii's avatar
Tatsuo Ishii committed
499 500 501 502 503 504 505 506 507
	PG_RETURN_INT32(pg_char_to_encoding(NameStr(*s)));
}
#endif

const char *
pg_encoding_to_char(int encoding)
{
	if (PG_VALID_ENCODING(encoding))
	{
508 509 510
		pg_enc2name *p = &pg_enc2name_tbl[encoding];

		Assert(encoding == p->encoding);
Tatsuo Ishii's avatar
Tatsuo Ishii committed
511 512 513 514 515 516 517 518 519 520
		return p->name;
	}
	return "";
}

#ifndef FRONTEND
Datum
PG_encoding_to_char(PG_FUNCTION_ARGS)
{
	int32		encoding = PG_GETARG_INT32(0);
521
	const char *encoding_name = pg_encoding_to_char(encoding);
Tatsuo Ishii's avatar
Tatsuo Ishii committed
522 523 524

	return DirectFunctionCall1(namein, CStringGetDatum(encoding_name));
}
525

Tatsuo Ishii's avatar
Tatsuo Ishii committed
526
#endif