1 /*
2  * conversion functions between pg_wchar and multibyte streams.
3  * Tatsuo Ishii
4  * src/backend/utils/mb/wchar.c
5  *
6  */
7 /* can be used in either frontend or backend */
8 #include "pool_parser.h"
9 #include "utils/elog.h"
10 #include <stdio.h>
11 #include <string.h>
12 #include "pg_wchar.h"
13 
14 #ifndef WIN32
15 #define DEF_ENC2NAME(name, codepage) { #name, PG_##name }
16 #else
17 #define DEF_ENC2NAME(name, codepage) { #name, PG_##name, codepage }
18 #endif
19 
20 const pg_enc2name pg_enc2name_tbl[] =
21 {
22 	DEF_ENC2NAME(SQL_ASCII, 0),
23 	DEF_ENC2NAME(EUC_JP, 20932),
24 	DEF_ENC2NAME(EUC_CN, 20936),
25 	DEF_ENC2NAME(EUC_KR, 51949),
26 	DEF_ENC2NAME(EUC_TW, 0),
27 	DEF_ENC2NAME(EUC_JIS_2004, 20932),
28 	DEF_ENC2NAME(UTF8, 65001),
29 	DEF_ENC2NAME(MULE_INTERNAL, 0),
30 	DEF_ENC2NAME(LATIN1, 28591),
31 	DEF_ENC2NAME(LATIN2, 28592),
32 	DEF_ENC2NAME(LATIN3, 28593),
33 	DEF_ENC2NAME(LATIN4, 28594),
34 	DEF_ENC2NAME(LATIN5, 28599),
35 	DEF_ENC2NAME(LATIN6, 0),
36 	DEF_ENC2NAME(LATIN7, 0),
37 	DEF_ENC2NAME(LATIN8, 0),
38 	DEF_ENC2NAME(LATIN9, 28605),
39 	DEF_ENC2NAME(LATIN10, 0),
40 	DEF_ENC2NAME(WIN1256, 1256),
41 	DEF_ENC2NAME(WIN1258, 1258),
42 	DEF_ENC2NAME(WIN866, 866),
43 	DEF_ENC2NAME(WIN874, 874),
44 	DEF_ENC2NAME(KOI8R, 20866),
45 	DEF_ENC2NAME(WIN1251, 1251),
46 	DEF_ENC2NAME(WIN1252, 1252),
47 	DEF_ENC2NAME(ISO_8859_5, 28595),
48 	DEF_ENC2NAME(ISO_8859_6, 28596),
49 	DEF_ENC2NAME(ISO_8859_7, 28597),
50 	DEF_ENC2NAME(ISO_8859_8, 28598),
51 	DEF_ENC2NAME(WIN1250, 1250),
52 	DEF_ENC2NAME(WIN1253, 1253),
53 	DEF_ENC2NAME(WIN1254, 1254),
54 	DEF_ENC2NAME(WIN1255, 1255),
55 	DEF_ENC2NAME(WIN1257, 1257),
56 	DEF_ENC2NAME(KOI8U, 21866),
57 	DEF_ENC2NAME(SJIS, 932),
58 	DEF_ENC2NAME(BIG5, 950),
59 	DEF_ENC2NAME(GBK, 936),
60 	DEF_ENC2NAME(UHC, 0),
61 	DEF_ENC2NAME(GB18030, 54936),
62 	DEF_ENC2NAME(JOHAB, 0),
63 	DEF_ENC2NAME(SHIFT_JIS_2004, 932)
64 };
65 
66 /* ----------
67  * These are encoding names for gettext.
68  *
69  * This covers all encodings except MULE_INTERNAL, which is alien to gettext.
70  * ----------
71  */
72 const pg_enc2gettext pg_enc2gettext_tbl[] =
73 {
74 	{PG_SQL_ASCII, "US-ASCII"},
75 	{PG_UTF8, "UTF-8"},
76 	{PG_LATIN1, "LATIN1"},
77 	{PG_LATIN2, "LATIN2"},
78 	{PG_LATIN3, "LATIN3"},
79 	{PG_LATIN4, "LATIN4"},
80 	{PG_ISO_8859_5, "ISO-8859-5"},
81 	{PG_ISO_8859_6, "ISO_8859-6"},
82 	{PG_ISO_8859_7, "ISO-8859-7"},
83 	{PG_ISO_8859_8, "ISO-8859-8"},
84 	{PG_LATIN5, "LATIN5"},
85 	{PG_LATIN6, "LATIN6"},
86 	{PG_LATIN7, "LATIN7"},
87 	{PG_LATIN8, "LATIN8"},
88 	{PG_LATIN9, "LATIN-9"},
89 	{PG_LATIN10, "LATIN10"},
90 	{PG_KOI8R, "KOI8-R"},
91 	{PG_KOI8U, "KOI8-U"},
92 	{PG_WIN1250, "CP1250"},
93 	{PG_WIN1251, "CP1251"},
94 	{PG_WIN1252, "CP1252"},
95 	{PG_WIN1253, "CP1253"},
96 	{PG_WIN1254, "CP1254"},
97 	{PG_WIN1255, "CP1255"},
98 	{PG_WIN1256, "CP1256"},
99 	{PG_WIN1257, "CP1257"},
100 	{PG_WIN1258, "CP1258"},
101 	{PG_WIN866, "CP866"},
102 	{PG_WIN874, "CP874"},
103 	{PG_EUC_CN, "EUC-CN"},
104 	{PG_EUC_JP, "EUC-JP"},
105 	{PG_EUC_KR, "EUC-KR"},
106 	{PG_EUC_TW, "EUC-TW"},
107 	{PG_EUC_JIS_2004, "EUC-JP"},
108 	{PG_SJIS, "SHIFT-JIS"},
109 	{PG_BIG5, "BIG5"},
110 	{PG_GBK, "GBK"},
111 	{PG_UHC, "UHC"},
112 	{PG_GB18030, "GB18030"},
113 	{PG_JOHAB, "JOHAB"},
114 	{PG_SHIFT_JIS_2004, "SHIFT_JISX0213"},
115 	{0, NULL}
116 };
117 
118 
119 /*
120  * Operations on multi-byte encodings are driven by a table of helper
121  * functions.
122  *
123  * To add an encoding support, define mblen(), dsplen() and verifier() for
124  * the encoding.  For server-encodings, also define mb2wchar() and wchar2mb()
125  * conversion functions.
126  *
127  * These functions generally assume that their input is validly formed.
128  * The "verifier" functions, further down in the file, have to be more
129  * paranoid.
130  *
131  * We expect that mblen() does not need to examine more than the first byte
132  * of the character to discover the correct length.  GB18030 is an exception
133  * to that rule, though, as it also looks at second byte.  But even that
134  * behaves in a predictable way, if you only pass the first byte: it will
135  * treat 4-byte encoded characters as two 2-byte encoded characters, which is
136  * good enough for all current uses.
137  *
138  * Note: for the display output of psql to work properly, the return values
139  * of the dsplen functions must conform to the Unicode standard. In particular
140  * the NUL character is zero width and control characters are generally
141  * width -1. It is recommended that non-ASCII encodings refer their ASCII
142  * subset to the ASCII routines to ensure consistency.
143  */
144 
145 /*
146  * SQL/ASCII
147  */
148 static int
pg_ascii2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)149 pg_ascii2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
150 {
151 	int			cnt = 0;
152 
153 	while (len > 0 && *from)
154 	{
155 		*to++ = *from++;
156 		len--;
157 		cnt++;
158 	}
159 	*to = 0;
160 	return cnt;
161 }
162 
163 static int
pg_ascii_mblen(const unsigned char * s)164 pg_ascii_mblen(const unsigned char *s)
165 {
166 	return 1;
167 }
168 
169 static int
pg_ascii_dsplen(const unsigned char * s)170 pg_ascii_dsplen(const unsigned char *s)
171 {
172 	if (*s == '\0')
173 		return 0;
174 	if (*s < 0x20 || *s == 0x7f)
175 		return -1;
176 
177 	return 1;
178 }
179 
180 /*
181  * EUC
182  */
183 static int
pg_euc2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)184 pg_euc2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
185 {
186 	int			cnt = 0;
187 
188 	while (len > 0 && *from)
189 	{
190 		if (*from == SS2 && len >= 2)	/* JIS X 0201 (so called "1 byte
191 										 * KANA") */
192 		{
193 			from++;
194 			*to = (SS2 << 8) | *from++;
195 			len -= 2;
196 		}
197 		else if (*from == SS3 && len >= 3)	/* JIS X 0212 KANJI */
198 		{
199 			from++;
200 			*to = (SS3 << 16) | (*from++ << 8);
201 			*to |= *from++;
202 			len -= 3;
203 		}
204 		else if (IS_HIGHBIT_SET(*from) && len >= 2) /* JIS X 0208 KANJI */
205 		{
206 			*to = *from++ << 8;
207 			*to |= *from++;
208 			len -= 2;
209 		}
210 		else					/* must be ASCII */
211 		{
212 			*to = *from++;
213 			len--;
214 		}
215 		to++;
216 		cnt++;
217 	}
218 	*to = 0;
219 	return cnt;
220 }
221 
222 static inline int
pg_euc_mblen(const unsigned char * s)223 pg_euc_mblen(const unsigned char *s)
224 {
225 	int			len;
226 
227 	if (*s == SS2)
228 		len = 2;
229 	else if (*s == SS3)
230 		len = 3;
231 	else if (IS_HIGHBIT_SET(*s))
232 		len = 2;
233 	else
234 		len = 1;
235 	return len;
236 }
237 
238 static inline int
pg_euc_dsplen(const unsigned char * s)239 pg_euc_dsplen(const unsigned char *s)
240 {
241 	int			len;
242 
243 	if (*s == SS2)
244 		len = 2;
245 	else if (*s == SS3)
246 		len = 2;
247 	else if (IS_HIGHBIT_SET(*s))
248 		len = 2;
249 	else
250 		len = pg_ascii_dsplen(s);
251 	return len;
252 }
253 
254 /*
255  * EUC_JP
256  */
257 static int
pg_eucjp2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)258 pg_eucjp2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
259 {
260 	return pg_euc2wchar_with_len(from, to, len);
261 }
262 
263 static int
pg_eucjp_mblen(const unsigned char * s)264 pg_eucjp_mblen(const unsigned char *s)
265 {
266 	return pg_euc_mblen(s);
267 }
268 
269 static int
pg_eucjp_dsplen(const unsigned char * s)270 pg_eucjp_dsplen(const unsigned char *s)
271 {
272 	int			len;
273 
274 	if (*s == SS2)
275 		len = 1;
276 	else if (*s == SS3)
277 		len = 2;
278 	else if (IS_HIGHBIT_SET(*s))
279 		len = 2;
280 	else
281 		len = pg_ascii_dsplen(s);
282 	return len;
283 }
284 
285 /*
286  * EUC_KR
287  */
288 static int
pg_euckr2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)289 pg_euckr2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
290 {
291 	return pg_euc2wchar_with_len(from, to, len);
292 }
293 
294 static int
pg_euckr_mblen(const unsigned char * s)295 pg_euckr_mblen(const unsigned char *s)
296 {
297 	return pg_euc_mblen(s);
298 }
299 
300 static int
pg_euckr_dsplen(const unsigned char * s)301 pg_euckr_dsplen(const unsigned char *s)
302 {
303 	return pg_euc_dsplen(s);
304 }
305 
306 /*
307  * EUC_CN
308  *
309  */
310 static int
pg_euccn2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)311 pg_euccn2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
312 {
313 	int			cnt = 0;
314 
315 	while (len > 0 && *from)
316 	{
317 		if (*from == SS2 && len >= 3)	/* code set 2 (unused?) */
318 		{
319 			from++;
320 			*to = (SS2 << 16) | (*from++ << 8);
321 			*to |= *from++;
322 			len -= 3;
323 		}
324 		else if (*from == SS3 && len >= 3)	/* code set 3 (unused ?) */
325 		{
326 			from++;
327 			*to = (SS3 << 16) | (*from++ << 8);
328 			*to |= *from++;
329 			len -= 3;
330 		}
331 		else if (IS_HIGHBIT_SET(*from) && len >= 2) /* code set 1 */
332 		{
333 			*to = *from++ << 8;
334 			*to |= *from++;
335 			len -= 2;
336 		}
337 		else
338 		{
339 			*to = *from++;
340 			len--;
341 		}
342 		to++;
343 		cnt++;
344 	}
345 	*to = 0;
346 	return cnt;
347 }
348 
349 static int
pg_euccn_mblen(const unsigned char * s)350 pg_euccn_mblen(const unsigned char *s)
351 {
352 	int			len;
353 
354 	if (IS_HIGHBIT_SET(*s))
355 		len = 2;
356 	else
357 		len = 1;
358 	return len;
359 }
360 
361 static int
pg_euccn_dsplen(const unsigned char * s)362 pg_euccn_dsplen(const unsigned char *s)
363 {
364 	int			len;
365 
366 	if (IS_HIGHBIT_SET(*s))
367 		len = 2;
368 	else
369 		len = pg_ascii_dsplen(s);
370 	return len;
371 }
372 
373 /*
374  * EUC_TW
375  *
376  */
377 static int
pg_euctw2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)378 pg_euctw2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
379 {
380 	int			cnt = 0;
381 
382 	while (len > 0 && *from)
383 	{
384 		if (*from == SS2 && len >= 4)	/* code set 2 */
385 		{
386 			from++;
387 			*to = (((uint32) SS2) << 24) | (*from++ << 16);
388 			*to |= *from++ << 8;
389 			*to |= *from++;
390 			len -= 4;
391 		}
392 		else if (*from == SS3 && len >= 3)	/* code set 3 (unused?) */
393 		{
394 			from++;
395 			*to = (SS3 << 16) | (*from++ << 8);
396 			*to |= *from++;
397 			len -= 3;
398 		}
399 		else if (IS_HIGHBIT_SET(*from) && len >= 2) /* code set 2 */
400 		{
401 			*to = *from++ << 8;
402 			*to |= *from++;
403 			len -= 2;
404 		}
405 		else
406 		{
407 			*to = *from++;
408 			len--;
409 		}
410 		to++;
411 		cnt++;
412 	}
413 	*to = 0;
414 	return cnt;
415 }
416 
417 static int
pg_euctw_mblen(const unsigned char * s)418 pg_euctw_mblen(const unsigned char *s)
419 {
420 	int			len;
421 
422 	if (*s == SS2)
423 		len = 4;
424 	else if (*s == SS3)
425 		len = 3;
426 	else if (IS_HIGHBIT_SET(*s))
427 		len = 2;
428 	else
429 		len = 1;
430 	return len;
431 }
432 
433 static int
pg_euctw_dsplen(const unsigned char * s)434 pg_euctw_dsplen(const unsigned char *s)
435 {
436 	int			len;
437 
438 	if (*s == SS2)
439 		len = 2;
440 	else if (*s == SS3)
441 		len = 2;
442 	else if (IS_HIGHBIT_SET(*s))
443 		len = 2;
444 	else
445 		len = pg_ascii_dsplen(s);
446 	return len;
447 }
448 
449 /*
450  * Convert pg_wchar to EUC_* encoding.
451  * caller must allocate enough space for "to", including a trailing zero!
452  * len: length of from.
453  * "from" not necessarily null terminated.
454  */
455 static int
pg_wchar2euc_with_len(const pg_wchar * from,unsigned char * to,int len)456 pg_wchar2euc_with_len(const pg_wchar *from, unsigned char *to, int len)
457 {
458 	int			cnt = 0;
459 
460 	while (len > 0 && *from)
461 	{
462 		unsigned char c;
463 
464 		if ((c = (*from >> 24)))
465 		{
466 			*to++ = c;
467 			*to++ = (*from >> 16) & 0xff;
468 			*to++ = (*from >> 8) & 0xff;
469 			*to++ = *from & 0xff;
470 			cnt += 4;
471 		}
472 		else if ((c = (*from >> 16)))
473 		{
474 			*to++ = c;
475 			*to++ = (*from >> 8) & 0xff;
476 			*to++ = *from & 0xff;
477 			cnt += 3;
478 		}
479 		else if ((c = (*from >> 8)))
480 		{
481 			*to++ = c;
482 			*to++ = *from & 0xff;
483 			cnt += 2;
484 		}
485 		else
486 		{
487 			*to++ = *from;
488 			cnt++;
489 		}
490 		from++;
491 		len--;
492 	}
493 	*to = 0;
494 	return cnt;
495 }
496 
497 
498 /*
499  * JOHAB
500  */
501 static int
pg_johab_mblen(const unsigned char * s)502 pg_johab_mblen(const unsigned char *s)
503 {
504 	return pg_euc_mblen(s);
505 }
506 
507 static int
pg_johab_dsplen(const unsigned char * s)508 pg_johab_dsplen(const unsigned char *s)
509 {
510 	return pg_euc_dsplen(s);
511 }
512 
513 /*
514  * convert UTF8 string to pg_wchar (UCS-4)
515  * caller must allocate enough space for "to", including a trailing zero!
516  * len: length of from.
517  * "from" not necessarily null terminated.
518  */
519 static int
pg_utf2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)520 pg_utf2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
521 {
522 	int			cnt = 0;
523 	uint32		c1,
524 				c2,
525 				c3,
526 				c4;
527 
528 	while (len > 0 && *from)
529 	{
530 		if ((*from & 0x80) == 0)
531 		{
532 			*to = *from++;
533 			len--;
534 		}
535 		else if ((*from & 0xe0) == 0xc0)
536 		{
537 			if (len < 2)
538 				break;			/* drop trailing incomplete char */
539 			c1 = *from++ & 0x1f;
540 			c2 = *from++ & 0x3f;
541 			*to = (c1 << 6) | c2;
542 			len -= 2;
543 		}
544 		else if ((*from & 0xf0) == 0xe0)
545 		{
546 			if (len < 3)
547 				break;			/* drop trailing incomplete char */
548 			c1 = *from++ & 0x0f;
549 			c2 = *from++ & 0x3f;
550 			c3 = *from++ & 0x3f;
551 			*to = (c1 << 12) | (c2 << 6) | c3;
552 			len -= 3;
553 		}
554 		else if ((*from & 0xf8) == 0xf0)
555 		{
556 			if (len < 4)
557 				break;			/* drop trailing incomplete char */
558 			c1 = *from++ & 0x07;
559 			c2 = *from++ & 0x3f;
560 			c3 = *from++ & 0x3f;
561 			c4 = *from++ & 0x3f;
562 			*to = (c1 << 18) | (c2 << 12) | (c3 << 6) | c4;
563 			len -= 4;
564 		}
565 		else
566 		{
567 			/* treat a bogus char as length 1; not ours to raise error */
568 			*to = *from++;
569 			len--;
570 		}
571 		to++;
572 		cnt++;
573 	}
574 	*to = 0;
575 	return cnt;
576 }
577 
578 
579 /*
580  * Map a Unicode code point to UTF-8.  utf8string must have 4 bytes of
581  * space allocated.
582  */
583 unsigned char *
unicode_to_utf8(pg_wchar c,unsigned char * utf8string)584 unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
585 {
586 	if (c <= 0x7F)
587 	{
588 		utf8string[0] = c;
589 	}
590 	else if (c <= 0x7FF)
591 	{
592 		utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
593 		utf8string[1] = 0x80 | (c & 0x3F);
594 	}
595 	else if (c <= 0xFFFF)
596 	{
597 		utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
598 		utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
599 		utf8string[2] = 0x80 | (c & 0x3F);
600 	}
601 	else
602 	{
603 		utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
604 		utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
605 		utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
606 		utf8string[3] = 0x80 | (c & 0x3F);
607 	}
608 
609 	return utf8string;
610 }
611 
612 /*
613  * Trivial conversion from pg_wchar to UTF-8.
614  * caller should allocate enough space for "to"
615  * len: length of from.
616  * "from" not necessarily null terminated.
617  */
618 static int
pg_wchar2utf_with_len(const pg_wchar * from,unsigned char * to,int len)619 pg_wchar2utf_with_len(const pg_wchar *from, unsigned char *to, int len)
620 {
621 	int			cnt = 0;
622 
623 	while (len > 0 && *from)
624 	{
625 		int			char_len;
626 
627 		unicode_to_utf8(*from, to);
628 		char_len = pg_utf_mblen(to);
629 		cnt += char_len;
630 		to += char_len;
631 		from++;
632 		len--;
633 	}
634 	*to = 0;
635 	return cnt;
636 }
637 
638 /*
639  * Return the byte length of a UTF8 character pointed to by s
640  *
641  * Note: in the current implementation we do not support UTF8 sequences
642  * of more than 4 bytes; hence do NOT return a value larger than 4.
643  * We return "1" for any leading byte that is either flat-out illegal or
644  * indicates a length larger than we support.
645  *
646  * pg_utf2wchar_with_len(), utf8_to_unicode(), pg_utf8_islegal(), and perhaps
647  * other places would need to be fixed to change this.
648  */
649 int
pg_utf_mblen(const unsigned char * s)650 pg_utf_mblen(const unsigned char *s)
651 {
652 	int			len;
653 
654 	if ((*s & 0x80) == 0)
655 		len = 1;
656 	else if ((*s & 0xe0) == 0xc0)
657 		len = 2;
658 	else if ((*s & 0xf0) == 0xe0)
659 		len = 3;
660 	else if ((*s & 0xf8) == 0xf0)
661 		len = 4;
662 #ifdef NOT_USED
663 	else if ((*s & 0xfc) == 0xf8)
664 		len = 5;
665 	else if ((*s & 0xfe) == 0xfc)
666 		len = 6;
667 #endif
668 	else
669 		len = 1;
670 	return len;
671 }
672 
673 /*
674  * This is an implementation of wcwidth() and wcswidth() as defined in
675  * "The Single UNIX Specification, Version 2, The Open Group, 1997"
676  * <http://www.UNIX-systems.org/online.html>
677  *
678  * Markus Kuhn -- 2001-09-08 -- public domain
679  *
680  * customised for PostgreSQL
681  *
682  * original available at : http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
683  */
684 
685 struct mbinterval
686 {
687 	unsigned short first;
688 	unsigned short last;
689 };
690 
691 /* auxiliary function for binary search in interval table */
692 static int
mbbisearch(pg_wchar ucs,const struct mbinterval * table,int max)693 mbbisearch(pg_wchar ucs, const struct mbinterval *table, int max)
694 {
695 	int			min = 0;
696 	int			mid;
697 
698 	if (ucs < table[0].first || ucs > table[max].last)
699 		return 0;
700 	while (max >= min)
701 	{
702 		mid = (min + max) / 2;
703 		if (ucs > table[mid].last)
704 			min = mid + 1;
705 		else if (ucs < table[mid].first)
706 			max = mid - 1;
707 		else
708 			return 1;
709 	}
710 
711 	return 0;
712 }
713 
714 
715 /* The following functions define the column width of an ISO 10646
716  * character as follows:
717  *
718  *	  - The null character (U+0000) has a column width of 0.
719  *
720  *	  - Other C0/C1 control characters and DEL will lead to a return
721  *		value of -1.
722  *
723  *	  - Non-spacing and enclosing combining characters (general
724  *		category code Mn or Me in the Unicode database) have a
725  *		column width of 0.
726  *
727  *	  - Other format characters (general category code Cf in the Unicode
728  *		database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
729  *
730  *	  - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
731  *		have a column width of 0.
732  *
733  *	  - Spacing characters in the East Asian Wide (W) or East Asian
734  *		FullWidth (F) category as defined in Unicode Technical
735  *		Report #11 have a column width of 2.
736  *
737  *	  - All remaining characters (including all printable
738  *		ISO 8859-1 and WGL4 characters, Unicode control characters,
739  *		etc.) have a column width of 1.
740  *
741  * This implementation assumes that wchar_t characters are encoded
742  * in ISO 10646.
743  */
744 
745 static int
ucs_wcwidth(pg_wchar ucs)746 ucs_wcwidth(pg_wchar ucs)
747 {
748 	/* sorted list of non-overlapping intervals of non-spacing characters */
749 	static const struct mbinterval combining[] = {
750 		{0x0300, 0x034E}, {0x0360, 0x0362}, {0x0483, 0x0486},
751 		{0x0488, 0x0489}, {0x0591, 0x05A1}, {0x05A3, 0x05B9},
752 		{0x05BB, 0x05BD}, {0x05BF, 0x05BF}, {0x05C1, 0x05C2},
753 		{0x05C4, 0x05C4}, {0x064B, 0x0655}, {0x0670, 0x0670},
754 		{0x06D6, 0x06E4}, {0x06E7, 0x06E8}, {0x06EA, 0x06ED},
755 		{0x070F, 0x070F}, {0x0711, 0x0711}, {0x0730, 0x074A},
756 		{0x07A6, 0x07B0}, {0x0901, 0x0902}, {0x093C, 0x093C},
757 		{0x0941, 0x0948}, {0x094D, 0x094D}, {0x0951, 0x0954},
758 		{0x0962, 0x0963}, {0x0981, 0x0981}, {0x09BC, 0x09BC},
759 		{0x09C1, 0x09C4}, {0x09CD, 0x09CD}, {0x09E2, 0x09E3},
760 		{0x0A02, 0x0A02}, {0x0A3C, 0x0A3C}, {0x0A41, 0x0A42},
761 		{0x0A47, 0x0A48}, {0x0A4B, 0x0A4D}, {0x0A70, 0x0A71},
762 		{0x0A81, 0x0A82}, {0x0ABC, 0x0ABC}, {0x0AC1, 0x0AC5},
763 		{0x0AC7, 0x0AC8}, {0x0ACD, 0x0ACD}, {0x0B01, 0x0B01},
764 		{0x0B3C, 0x0B3C}, {0x0B3F, 0x0B3F}, {0x0B41, 0x0B43},
765 		{0x0B4D, 0x0B4D}, {0x0B56, 0x0B56}, {0x0B82, 0x0B82},
766 		{0x0BC0, 0x0BC0}, {0x0BCD, 0x0BCD}, {0x0C3E, 0x0C40},
767 		{0x0C46, 0x0C48}, {0x0C4A, 0x0C4D}, {0x0C55, 0x0C56},
768 		{0x0CBF, 0x0CBF}, {0x0CC6, 0x0CC6}, {0x0CCC, 0x0CCD},
769 		{0x0D41, 0x0D43}, {0x0D4D, 0x0D4D}, {0x0DCA, 0x0DCA},
770 		{0x0DD2, 0x0DD4}, {0x0DD6, 0x0DD6}, {0x0E31, 0x0E31},
771 		{0x0E34, 0x0E3A}, {0x0E47, 0x0E4E}, {0x0EB1, 0x0EB1},
772 		{0x0EB4, 0x0EB9}, {0x0EBB, 0x0EBC}, {0x0EC8, 0x0ECD},
773 		{0x0F18, 0x0F19}, {0x0F35, 0x0F35}, {0x0F37, 0x0F37},
774 		{0x0F39, 0x0F39}, {0x0F71, 0x0F7E}, {0x0F80, 0x0F84},
775 		{0x0F86, 0x0F87}, {0x0F90, 0x0F97}, {0x0F99, 0x0FBC},
776 		{0x0FC6, 0x0FC6}, {0x102D, 0x1030}, {0x1032, 0x1032},
777 		{0x1036, 0x1037}, {0x1039, 0x1039}, {0x1058, 0x1059},
778 		{0x1160, 0x11FF}, {0x17B7, 0x17BD}, {0x17C6, 0x17C6},
779 		{0x17C9, 0x17D3}, {0x180B, 0x180E}, {0x18A9, 0x18A9},
780 		{0x200B, 0x200F}, {0x202A, 0x202E}, {0x206A, 0x206F},
781 		{0x20D0, 0x20E3}, {0x302A, 0x302F}, {0x3099, 0x309A},
782 		{0xFB1E, 0xFB1E}, {0xFE20, 0xFE23}, {0xFEFF, 0xFEFF},
783 		{0xFFF9, 0xFFFB}
784 	};
785 
786 	/* test for 8-bit control characters */
787 	if (ucs == 0)
788 		return 0;
789 
790 	if (ucs < 0x20 || (ucs >= 0x7f && ucs < 0xa0) || ucs > 0x0010ffff)
791 		return -1;
792 
793 	/* binary search in table of non-spacing characters */
794 	if (mbbisearch(ucs, combining,
795 				   sizeof(combining) / sizeof(struct mbinterval) - 1))
796 		return 0;
797 
798 	/*
799 	 * if we arrive here, ucs is not a combining or C0/C1 control character
800 	 */
801 
802 	return 1 +
803 		(ucs >= 0x1100 &&
804 		 (ucs <= 0x115f ||		/* Hangul Jamo init. consonants */
805 		  (ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
806 		   ucs != 0x303f) ||	/* CJK ... Yi */
807 		  (ucs >= 0xac00 && ucs <= 0xd7a3) ||	/* Hangul Syllables */
808 		  (ucs >= 0xf900 && ucs <= 0xfaff) ||	/* CJK Compatibility
809 												 * Ideographs */
810 		  (ucs >= 0xfe30 && ucs <= 0xfe6f) ||	/* CJK Compatibility Forms */
811 		  (ucs >= 0xff00 && ucs <= 0xff5f) ||	/* Fullwidth Forms */
812 		  (ucs >= 0xffe0 && ucs <= 0xffe6) ||
813 		  (ucs >= 0x20000 && ucs <= 0x2ffff)));
814 }
815 
816 /*
817  * Convert a UTF-8 character to a Unicode code point.
818  * This is a one-character version of pg_utf2wchar_with_len.
819  *
820  * No error checks here, c must point to a long-enough string.
821  */
822 pg_wchar
utf8_to_unicode(const unsigned char * c)823 utf8_to_unicode(const unsigned char *c)
824 {
825 	if ((*c & 0x80) == 0)
826 		return (pg_wchar) c[0];
827 	else if ((*c & 0xe0) == 0xc0)
828 		return (pg_wchar) (((c[0] & 0x1f) << 6) |
829 						   (c[1] & 0x3f));
830 	else if ((*c & 0xf0) == 0xe0)
831 		return (pg_wchar) (((c[0] & 0x0f) << 12) |
832 						   ((c[1] & 0x3f) << 6) |
833 						   (c[2] & 0x3f));
834 	else if ((*c & 0xf8) == 0xf0)
835 		return (pg_wchar) (((c[0] & 0x07) << 18) |
836 						   ((c[1] & 0x3f) << 12) |
837 						   ((c[2] & 0x3f) << 6) |
838 						   (c[3] & 0x3f));
839 	else
840 		/* that is an invalid code on purpose */
841 		return 0xffffffff;
842 }
843 
844 static int
pg_utf_dsplen(const unsigned char * s)845 pg_utf_dsplen(const unsigned char *s)
846 {
847 	return ucs_wcwidth(utf8_to_unicode(s));
848 }
849 
850 /*
851  * convert mule internal code to pg_wchar
852  * caller should allocate enough space for "to"
853  * len: length of from.
854  * "from" not necessarily null terminated.
855  */
856 static int
pg_mule2wchar_with_len(const unsigned char * from,pg_wchar * to,int len)857 pg_mule2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
858 {
859 	int			cnt = 0;
860 
861 	while (len > 0 && *from)
862 	{
863 		if (IS_LC1(*from) && len >= 2)
864 		{
865 			*to = *from++ << 16;
866 			*to |= *from++;
867 			len -= 2;
868 		}
869 		else if (IS_LCPRV1(*from) && len >= 3)
870 		{
871 			from++;
872 			*to = *from++ << 16;
873 			*to |= *from++;
874 			len -= 3;
875 		}
876 		else if (IS_LC2(*from) && len >= 3)
877 		{
878 			*to = *from++ << 16;
879 			*to |= *from++ << 8;
880 			*to |= *from++;
881 			len -= 3;
882 		}
883 		else if (IS_LCPRV2(*from) && len >= 4)
884 		{
885 			from++;
886 			*to = *from++ << 16;
887 			*to |= *from++ << 8;
888 			*to |= *from++;
889 			len -= 4;
890 		}
891 		else
892 		{						/* assume ASCII */
893 			*to = (unsigned char) *from++;
894 			len--;
895 		}
896 		to++;
897 		cnt++;
898 	}
899 	*to = 0;
900 	return cnt;
901 }
902 
903 /*
904  * convert pg_wchar to mule internal code
905  * caller should allocate enough space for "to"
906  * len: length of from.
907  * "from" not necessarily null terminated.
908  */
909 static int
pg_wchar2mule_with_len(const pg_wchar * from,unsigned char * to,int len)910 pg_wchar2mule_with_len(const pg_wchar *from, unsigned char *to, int len)
911 {
912 	int			cnt = 0;
913 
914 	while (len > 0 && *from)
915 	{
916 		unsigned char lb;
917 
918 		lb = (*from >> 16) & 0xff;
919 		if (IS_LC1(lb))
920 		{
921 			*to++ = lb;
922 			*to++ = *from & 0xff;
923 			cnt += 2;
924 		}
925 		else if (IS_LC2(lb))
926 		{
927 			*to++ = lb;
928 			*to++ = (*from >> 8) & 0xff;
929 			*to++ = *from & 0xff;
930 			cnt += 3;
931 		}
932 		else if (IS_LCPRV1_A_RANGE(lb))
933 		{
934 			*to++ = LCPRV1_A;
935 			*to++ = lb;
936 			*to++ = *from & 0xff;
937 			cnt += 3;
938 		}
939 		else if (IS_LCPRV1_B_RANGE(lb))
940 		{
941 			*to++ = LCPRV1_B;
942 			*to++ = lb;
943 			*to++ = *from & 0xff;
944 			cnt += 3;
945 		}
946 		else if (IS_LCPRV2_A_RANGE(lb))
947 		{
948 			*to++ = LCPRV2_A;
949 			*to++ = lb;
950 			*to++ = (*from >> 8) & 0xff;
951 			*to++ = *from & 0xff;
952 			cnt += 4;
953 		}
954 		else if (IS_LCPRV2_B_RANGE(lb))
955 		{
956 			*to++ = LCPRV2_B;
957 			*to++ = lb;
958 			*to++ = (*from >> 8) & 0xff;
959 			*to++ = *from & 0xff;
960 			cnt += 4;
961 		}
962 		else
963 		{
964 			*to++ = *from & 0xff;
965 			cnt += 1;
966 		}
967 		from++;
968 		len--;
969 	}
970 	*to = 0;
971 	return cnt;
972 }
973 
974 int
pg_mule_mblen(const unsigned char * s)975 pg_mule_mblen(const unsigned char *s)
976 {
977 	int			len;
978 
979 	if (IS_LC1(*s))
980 		len = 2;
981 	else if (IS_LCPRV1(*s))
982 		len = 3;
983 	else if (IS_LC2(*s))
984 		len = 3;
985 	else if (IS_LCPRV2(*s))
986 		len = 4;
987 	else
988 		len = 1;				/* assume ASCII */
989 	return len;
990 }
991 
992 static int
pg_mule_dsplen(const unsigned char * s)993 pg_mule_dsplen(const unsigned char *s)
994 {
995 	int			len;
996 
997 	/*
998 	 * Note: it's not really appropriate to assume that all multibyte charsets
999 	 * are double-wide on screen.  But this seems an okay approximation for
1000 	 * the MULE charsets we currently support.
1001 	 */
1002 
1003 	if (IS_LC1(*s))
1004 		len = 1;
1005 	else if (IS_LCPRV1(*s))
1006 		len = 1;
1007 	else if (IS_LC2(*s))
1008 		len = 2;
1009 	else if (IS_LCPRV2(*s))
1010 		len = 2;
1011 	else
1012 		len = 1;				/* assume ASCII */
1013 
1014 	return len;
1015 }
1016 
1017 /*
1018  * ISO8859-1
1019  */
1020 static int
pg_latin12wchar_with_len(const unsigned char * from,pg_wchar * to,int len)1021 pg_latin12wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
1022 {
1023 	int			cnt = 0;
1024 
1025 	while (len > 0 && *from)
1026 	{
1027 		*to++ = *from++;
1028 		len--;
1029 		cnt++;
1030 	}
1031 	*to = 0;
1032 	return cnt;
1033 }
1034 
1035 /*
1036  * Trivial conversion from pg_wchar to single byte encoding. Just ignores
1037  * high bits.
1038  * caller should allocate enough space for "to"
1039  * len: length of from.
1040  * "from" not necessarily null terminated.
1041  */
1042 static int
pg_wchar2single_with_len(const pg_wchar * from,unsigned char * to,int len)1043 pg_wchar2single_with_len(const pg_wchar *from, unsigned char *to, int len)
1044 {
1045 	int			cnt = 0;
1046 
1047 	while (len > 0 && *from)
1048 	{
1049 		*to++ = *from++;
1050 		len--;
1051 		cnt++;
1052 	}
1053 	*to = 0;
1054 	return cnt;
1055 }
1056 
1057 static int
pg_latin1_mblen(const unsigned char * s)1058 pg_latin1_mblen(const unsigned char *s)
1059 {
1060 	return 1;
1061 }
1062 
1063 static int
pg_latin1_dsplen(const unsigned char * s)1064 pg_latin1_dsplen(const unsigned char *s)
1065 {
1066 	return pg_ascii_dsplen(s);
1067 }
1068 
1069 /*
1070  * SJIS
1071  */
1072 static int
pg_sjis_mblen(const unsigned char * s)1073 pg_sjis_mblen(const unsigned char *s)
1074 {
1075 	int			len;
1076 
1077 	if (*s >= 0xa1 && *s <= 0xdf)
1078 		len = 1;				/* 1 byte kana? */
1079 	else if (IS_HIGHBIT_SET(*s))
1080 		len = 2;				/* kanji? */
1081 	else
1082 		len = 1;				/* should be ASCII */
1083 	return len;
1084 }
1085 
1086 static int
pg_sjis_dsplen(const unsigned char * s)1087 pg_sjis_dsplen(const unsigned char *s)
1088 {
1089 	int			len;
1090 
1091 	if (*s >= 0xa1 && *s <= 0xdf)
1092 		len = 1;				/* 1 byte kana? */
1093 	else if (IS_HIGHBIT_SET(*s))
1094 		len = 2;				/* kanji? */
1095 	else
1096 		len = pg_ascii_dsplen(s);	/* should be ASCII */
1097 	return len;
1098 }
1099 
1100 /*
1101  * Big5
1102  */
1103 static int
pg_big5_mblen(const unsigned char * s)1104 pg_big5_mblen(const unsigned char *s)
1105 {
1106 	int			len;
1107 
1108 	if (IS_HIGHBIT_SET(*s))
1109 		len = 2;				/* kanji? */
1110 	else
1111 		len = 1;				/* should be ASCII */
1112 	return len;
1113 }
1114 
1115 static int
pg_big5_dsplen(const unsigned char * s)1116 pg_big5_dsplen(const unsigned char *s)
1117 {
1118 	int			len;
1119 
1120 	if (IS_HIGHBIT_SET(*s))
1121 		len = 2;				/* kanji? */
1122 	else
1123 		len = pg_ascii_dsplen(s);	/* should be ASCII */
1124 	return len;
1125 }
1126 
1127 /*
1128  * GBK
1129  */
1130 static int
pg_gbk_mblen(const unsigned char * s)1131 pg_gbk_mblen(const unsigned char *s)
1132 {
1133 	int			len;
1134 
1135 	if (IS_HIGHBIT_SET(*s))
1136 		len = 2;				/* kanji? */
1137 	else
1138 		len = 1;				/* should be ASCII */
1139 	return len;
1140 }
1141 
1142 static int
pg_gbk_dsplen(const unsigned char * s)1143 pg_gbk_dsplen(const unsigned char *s)
1144 {
1145 	int			len;
1146 
1147 	if (IS_HIGHBIT_SET(*s))
1148 		len = 2;				/* kanji? */
1149 	else
1150 		len = pg_ascii_dsplen(s);	/* should be ASCII */
1151 	return len;
1152 }
1153 
1154 /*
1155  * UHC
1156  */
1157 static int
pg_uhc_mblen(const unsigned char * s)1158 pg_uhc_mblen(const unsigned char *s)
1159 {
1160 	int			len;
1161 
1162 	if (IS_HIGHBIT_SET(*s))
1163 		len = 2;				/* 2byte? */
1164 	else
1165 		len = 1;				/* should be ASCII */
1166 	return len;
1167 }
1168 
1169 static int
pg_uhc_dsplen(const unsigned char * s)1170 pg_uhc_dsplen(const unsigned char *s)
1171 {
1172 	int			len;
1173 
1174 	if (IS_HIGHBIT_SET(*s))
1175 		len = 2;				/* 2byte? */
1176 	else
1177 		len = pg_ascii_dsplen(s);	/* should be ASCII */
1178 	return len;
1179 }
1180 
1181 /*
1182  * GB18030
1183  *	Added by Bill Huang <bhuang@redhat.com>,<bill_huanghb@ybb.ne.jp>
1184  */
1185 
1186 /*
1187  * Unlike all other mblen() functions, this also looks at the second byte of
1188  * the input.  However, if you only pass the first byte of a multi-byte
1189  * string, and \0 as the second byte, this still works in a predictable way:
1190  * a 4-byte character will be reported as two 2-byte characters.  That's
1191  * enough for all current uses, as a client-only encoding.  It works that
1192  * way, because in any valid 4-byte GB18030-encoded character, the third and
1193  * fourth byte look like a 2-byte encoded character, when looked at
1194  * separately.
1195  */
1196 static int
pg_gb18030_mblen(const unsigned char * s)1197 pg_gb18030_mblen(const unsigned char *s)
1198 {
1199 	int			len;
1200 
1201 	if (!IS_HIGHBIT_SET(*s))
1202 		len = 1;				/* ASCII */
1203 	else if (*(s + 1) >= 0x30 && *(s + 1) <= 0x39)
1204 		len = 4;
1205 	else
1206 		len = 2;
1207 	return len;
1208 }
1209 
1210 static int
pg_gb18030_dsplen(const unsigned char * s)1211 pg_gb18030_dsplen(const unsigned char *s)
1212 {
1213 	int			len;
1214 
1215 	if (IS_HIGHBIT_SET(*s))
1216 		len = 2;
1217 	else
1218 		len = pg_ascii_dsplen(s);	/* ASCII */
1219 	return len;
1220 }
1221 
1222 /*
1223  *-------------------------------------------------------------------
1224  * multibyte sequence validators
1225  *
1226  * These functions accept "s", a pointer to the first byte of a string,
1227  * and "len", the remaining length of the string.  If there is a validly
1228  * encoded character beginning at *s, return its length in bytes; else
1229  * return -1.
1230  *
1231  * The functions can assume that len > 0 and that *s != '\0', but they must
1232  * test for and reject zeroes in any additional bytes of a multibyte character.
1233  *
1234  * Note that this definition allows the function for a single-byte
1235  * encoding to be just "return 1".
1236  *-------------------------------------------------------------------
1237  */
1238 
1239 static int
pg_ascii_verifier(const unsigned char * s,int len)1240 pg_ascii_verifier(const unsigned char *s, int len)
1241 {
1242 	return 1;
1243 }
1244 
1245 #define IS_EUC_RANGE_VALID(c)	((c) >= 0xa1 && (c) <= 0xfe)
1246 
1247 static int
pg_eucjp_verifier(const unsigned char * s,int len)1248 pg_eucjp_verifier(const unsigned char *s, int len)
1249 {
1250 	int			l;
1251 	unsigned char c1,
1252 				c2;
1253 
1254 	c1 = *s++;
1255 
1256 	switch (c1)
1257 	{
1258 		case SS2:				/* JIS X 0201 */
1259 			l = 2;
1260 			if (l > len)
1261 				return -1;
1262 			c2 = *s++;
1263 			if (c2 < 0xa1 || c2 > 0xdf)
1264 				return -1;
1265 			break;
1266 
1267 		case SS3:				/* JIS X 0212 */
1268 			l = 3;
1269 			if (l > len)
1270 				return -1;
1271 			c2 = *s++;
1272 			if (!IS_EUC_RANGE_VALID(c2))
1273 				return -1;
1274 			c2 = *s++;
1275 			if (!IS_EUC_RANGE_VALID(c2))
1276 				return -1;
1277 			break;
1278 
1279 		default:
1280 			if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
1281 			{
1282 				l = 2;
1283 				if (l > len)
1284 					return -1;
1285 				if (!IS_EUC_RANGE_VALID(c1))
1286 					return -1;
1287 				c2 = *s++;
1288 				if (!IS_EUC_RANGE_VALID(c2))
1289 					return -1;
1290 			}
1291 			else
1292 				/* must be ASCII */
1293 			{
1294 				l = 1;
1295 			}
1296 			break;
1297 	}
1298 
1299 	return l;
1300 }
1301 
1302 static int
pg_euckr_verifier(const unsigned char * s,int len)1303 pg_euckr_verifier(const unsigned char *s, int len)
1304 {
1305 	int			l;
1306 	unsigned char c1,
1307 				c2;
1308 
1309 	c1 = *s++;
1310 
1311 	if (IS_HIGHBIT_SET(c1))
1312 	{
1313 		l = 2;
1314 		if (l > len)
1315 			return -1;
1316 		if (!IS_EUC_RANGE_VALID(c1))
1317 			return -1;
1318 		c2 = *s++;
1319 		if (!IS_EUC_RANGE_VALID(c2))
1320 			return -1;
1321 	}
1322 	else
1323 		/* must be ASCII */
1324 	{
1325 		l = 1;
1326 	}
1327 
1328 	return l;
1329 }
1330 
1331 /* EUC-CN byte sequences are exactly same as EUC-KR */
1332 #define pg_euccn_verifier	pg_euckr_verifier
1333 
1334 static int
pg_euctw_verifier(const unsigned char * s,int len)1335 pg_euctw_verifier(const unsigned char *s, int len)
1336 {
1337 	int			l;
1338 	unsigned char c1,
1339 				c2;
1340 
1341 	c1 = *s++;
1342 
1343 	switch (c1)
1344 	{
1345 		case SS2:				/* CNS 11643 Plane 1-7 */
1346 			l = 4;
1347 			if (l > len)
1348 				return -1;
1349 			c2 = *s++;
1350 			if (c2 < 0xa1 || c2 > 0xa7)
1351 				return -1;
1352 			c2 = *s++;
1353 			if (!IS_EUC_RANGE_VALID(c2))
1354 				return -1;
1355 			c2 = *s++;
1356 			if (!IS_EUC_RANGE_VALID(c2))
1357 				return -1;
1358 			break;
1359 
1360 		case SS3:				/* unused */
1361 			return -1;
1362 
1363 		default:
1364 			if (IS_HIGHBIT_SET(c1)) /* CNS 11643 Plane 1 */
1365 			{
1366 				l = 2;
1367 				if (l > len)
1368 					return -1;
1369 				/* no further range check on c1? */
1370 				c2 = *s++;
1371 				if (!IS_EUC_RANGE_VALID(c2))
1372 					return -1;
1373 			}
1374 			else
1375 				/* must be ASCII */
1376 			{
1377 				l = 1;
1378 			}
1379 			break;
1380 	}
1381 	return l;
1382 }
1383 
1384 static int
pg_johab_verifier(const unsigned char * s,int len)1385 pg_johab_verifier(const unsigned char *s, int len)
1386 {
1387 	int			l,
1388 				mbl;
1389 	unsigned char c;
1390 
1391 	l = mbl = pg_johab_mblen(s);
1392 
1393 	if (len < l)
1394 		return -1;
1395 
1396 	if (!IS_HIGHBIT_SET(*s))
1397 		return mbl;
1398 
1399 	while (--l > 0)
1400 	{
1401 		c = *++s;
1402 		if (!IS_EUC_RANGE_VALID(c))
1403 			return -1;
1404 	}
1405 	return mbl;
1406 }
1407 
1408 static int
pg_mule_verifier(const unsigned char * s,int len)1409 pg_mule_verifier(const unsigned char *s, int len)
1410 {
1411 	int			l,
1412 				mbl;
1413 	unsigned char c;
1414 
1415 	l = mbl = pg_mule_mblen(s);
1416 
1417 	if (len < l)
1418 		return -1;
1419 
1420 	while (--l > 0)
1421 	{
1422 		c = *++s;
1423 		if (!IS_HIGHBIT_SET(c))
1424 			return -1;
1425 	}
1426 	return mbl;
1427 }
1428 
1429 static int
pg_latin1_verifier(const unsigned char * s,int len)1430 pg_latin1_verifier(const unsigned char *s, int len)
1431 {
1432 	return 1;
1433 }
1434 
1435 static int
pg_sjis_verifier(const unsigned char * s,int len)1436 pg_sjis_verifier(const unsigned char *s, int len)
1437 {
1438 	int			l,
1439 				mbl;
1440 	unsigned char c1,
1441 				c2;
1442 
1443 	l = mbl = pg_sjis_mblen(s);
1444 
1445 	if (len < l)
1446 		return -1;
1447 
1448 	if (l == 1)					/* pg_sjis_mblen already verified it */
1449 		return mbl;
1450 
1451 	c1 = *s++;
1452 	c2 = *s;
1453 	if (!ISSJISHEAD(c1) || !ISSJISTAIL(c2))
1454 		return -1;
1455 	return mbl;
1456 }
1457 
1458 static int
pg_big5_verifier(const unsigned char * s,int len)1459 pg_big5_verifier(const unsigned char *s, int len)
1460 {
1461 	int			l,
1462 				mbl;
1463 
1464 	l = mbl = pg_big5_mblen(s);
1465 
1466 	if (len < l)
1467 		return -1;
1468 
1469 	while (--l > 0)
1470 	{
1471 		if (*++s == '\0')
1472 			return -1;
1473 	}
1474 
1475 	return mbl;
1476 }
1477 
1478 static int
pg_gbk_verifier(const unsigned char * s,int len)1479 pg_gbk_verifier(const unsigned char *s, int len)
1480 {
1481 	int			l,
1482 				mbl;
1483 
1484 	l = mbl = pg_gbk_mblen(s);
1485 
1486 	if (len < l)
1487 		return -1;
1488 
1489 	while (--l > 0)
1490 	{
1491 		if (*++s == '\0')
1492 			return -1;
1493 	}
1494 
1495 	return mbl;
1496 }
1497 
1498 static int
pg_uhc_verifier(const unsigned char * s,int len)1499 pg_uhc_verifier(const unsigned char *s, int len)
1500 {
1501 	int			l,
1502 				mbl;
1503 
1504 	l = mbl = pg_uhc_mblen(s);
1505 
1506 	if (len < l)
1507 		return -1;
1508 
1509 	while (--l > 0)
1510 	{
1511 		if (*++s == '\0')
1512 			return -1;
1513 	}
1514 
1515 	return mbl;
1516 }
1517 
1518 static int
pg_gb18030_verifier(const unsigned char * s,int len)1519 pg_gb18030_verifier(const unsigned char *s, int len)
1520 {
1521 	int			l;
1522 
1523 	if (!IS_HIGHBIT_SET(*s))
1524 		l = 1;					/* ASCII */
1525 	else if (len >= 4 && *(s + 1) >= 0x30 && *(s + 1) <= 0x39)
1526 	{
1527 		/* Should be 4-byte, validate remaining bytes */
1528 		if (*s >= 0x81 && *s <= 0xfe &&
1529 			*(s + 2) >= 0x81 && *(s + 2) <= 0xfe &&
1530 			*(s + 3) >= 0x30 && *(s + 3) <= 0x39)
1531 			l = 4;
1532 		else
1533 			l = -1;
1534 	}
1535 	else if (len >= 2 && *s >= 0x81 && *s <= 0xfe)
1536 	{
1537 		/* Should be 2-byte, validate */
1538 		if ((*(s + 1) >= 0x40 && *(s + 1) <= 0x7e) ||
1539 			(*(s + 1) >= 0x80 && *(s + 1) <= 0xfe))
1540 			l = 2;
1541 		else
1542 			l = -1;
1543 	}
1544 	else
1545 		l = -1;
1546 	return l;
1547 }
1548 
1549 static int
pg_utf8_verifier(const unsigned char * s,int len)1550 pg_utf8_verifier(const unsigned char *s, int len)
1551 {
1552 	int			l = pg_utf_mblen(s);
1553 
1554 	if (len < l)
1555 		return -1;
1556 
1557 	if (!pg_utf8_islegal(s, l))
1558 		return -1;
1559 
1560 	return l;
1561 }
1562 
1563 /*
1564  * Check for validity of a single UTF-8 encoded character
1565  *
1566  * This directly implements the rules in RFC3629.  The bizarre-looking
1567  * restrictions on the second byte are meant to ensure that there isn't
1568  * more than one encoding of a given Unicode character point; that is,
1569  * you may not use a longer-than-necessary byte sequence with high order
1570  * zero bits to represent a character that would fit in fewer bytes.
1571  * To do otherwise is to create security hazards (eg, create an apparent
1572  * non-ASCII character that decodes to plain ASCII).
1573  *
1574  * length is assumed to have been obtained by pg_utf_mblen(), and the
1575  * caller must have checked that that many bytes are present in the buffer.
1576  */
1577 bool
pg_utf8_islegal(const unsigned char * source,int length)1578 pg_utf8_islegal(const unsigned char *source, int length)
1579 {
1580 	unsigned char a;
1581 
1582 	switch (length)
1583 	{
1584 		default:
1585 			/* reject lengths 5 and 6 for now */
1586 			return false;
1587 		case 4:
1588 			a = source[3];
1589 			if (a < 0x80 || a > 0xBF)
1590 				return false;
1591 			/* FALL THRU */
1592 		case 3:
1593 			a = source[2];
1594 			if (a < 0x80 || a > 0xBF)
1595 				return false;
1596 			/* FALL THRU */
1597 		case 2:
1598 			a = source[1];
1599 			switch (*source)
1600 			{
1601 				case 0xE0:
1602 					if (a < 0xA0 || a > 0xBF)
1603 						return false;
1604 					break;
1605 				case 0xED:
1606 					if (a < 0x80 || a > 0x9F)
1607 						return false;
1608 					break;
1609 				case 0xF0:
1610 					if (a < 0x90 || a > 0xBF)
1611 						return false;
1612 					break;
1613 				case 0xF4:
1614 					if (a < 0x80 || a > 0x8F)
1615 						return false;
1616 					break;
1617 				default:
1618 					if (a < 0x80 || a > 0xBF)
1619 						return false;
1620 					break;
1621 			}
1622 			/* FALL THRU */
1623 		case 1:
1624 			a = *source;
1625 			if (a >= 0x80 && a < 0xC2)
1626 				return false;
1627 			if (a > 0xF4)
1628 				return false;
1629 			break;
1630 	}
1631 	return true;
1632 }
1633 
1634 #ifndef FRONTEND
1635 
1636 /*
1637  * Generic character incrementer function.
1638  *
1639  * Not knowing anything about the properties of the encoding in use, we just
1640  * keep incrementing the last byte until we get a validly-encoded result,
1641  * or we run out of values to try.  We don't bother to try incrementing
1642  * higher-order bytes, so there's no growth in runtime for wider characters.
1643  * (If we did try to do that, we'd need to consider the likelihood that 255
1644  * is not a valid final byte in the encoding.)
1645  */
1646 static bool
pg_generic_charinc(unsigned char * charptr,int len)1647 pg_generic_charinc(unsigned char *charptr, int len)
1648 {
1649 	unsigned char *lastbyte = charptr + len - 1;
1650 	mbverifier	mbverify;
1651 
1652 	/* We can just invoke the character verifier directly. */
1653 	mbverify = pg_wchar_table[GetDatabaseEncoding()].mbverify;
1654 
1655 	while (*lastbyte < (unsigned char) 255)
1656 	{
1657 		(*lastbyte)++;
1658 		if ((*mbverify) (charptr, len) == len)
1659 			return true;
1660 	}
1661 
1662 	return false;
1663 }
1664 
1665 /*
1666  * UTF-8 character incrementer function.
1667  *
1668  * For a one-byte character less than 0x7F, we just increment the byte.
1669  *
1670  * For a multibyte character, every byte but the first must fall between 0x80
1671  * and 0xBF; and the first byte must be between 0xC0 and 0xF4.  We increment
1672  * the last byte that's not already at its maximum value.  If we can't find a
1673  * byte that's less than the maximum allowable value, we simply fail.  We also
1674  * need some special-case logic to skip regions used for surrogate pair
1675  * handling, as those should not occur in valid UTF-8.
1676  *
1677  * Note that we don't reset lower-order bytes back to their minimums, since
1678  * we can't afford to make an exhaustive search (see make_greater_string).
1679  */
1680 static bool
pg_utf8_increment(unsigned char * charptr,int length)1681 pg_utf8_increment(unsigned char *charptr, int length)
1682 {
1683 	unsigned char a;
1684 	unsigned char limit;
1685 
1686 	switch (length)
1687 	{
1688 		default:
1689 			/* reject lengths 5 and 6 for now */
1690 			return false;
1691 		case 4:
1692 			a = charptr[3];
1693 			if (a < 0xBF)
1694 			{
1695 				charptr[3]++;
1696 				break;
1697 			}
1698 			/* FALL THRU */
1699 		case 3:
1700 			a = charptr[2];
1701 			if (a < 0xBF)
1702 			{
1703 				charptr[2]++;
1704 				break;
1705 			}
1706 			/* FALL THRU */
1707 		case 2:
1708 			a = charptr[1];
1709 			switch (*charptr)
1710 			{
1711 				case 0xED:
1712 					limit = 0x9F;
1713 					break;
1714 				case 0xF4:
1715 					limit = 0x8F;
1716 					break;
1717 				default:
1718 					limit = 0xBF;
1719 					break;
1720 			}
1721 			if (a < limit)
1722 			{
1723 				charptr[1]++;
1724 				break;
1725 			}
1726 			/* FALL THRU */
1727 		case 1:
1728 			a = *charptr;
1729 			if (a == 0x7F || a == 0xDF || a == 0xEF || a == 0xF4)
1730 				return false;
1731 			charptr[0]++;
1732 			break;
1733 	}
1734 
1735 	return true;
1736 }
1737 
1738 /*
1739  * EUC-JP character incrementer function.
1740  *
1741  * If the sequence starts with SS2 (0x8e), it must be a two-byte sequence
1742  * representing JIS X 0201 characters with the second byte ranging between
1743  * 0xa1 and 0xdf.  We just increment the last byte if it's less than 0xdf,
1744  * and otherwise rewrite the whole sequence to 0xa1 0xa1.
1745  *
1746  * If the sequence starts with SS3 (0x8f), it must be a three-byte sequence
1747  * in which the last two bytes range between 0xa1 and 0xfe.  The last byte
1748  * is incremented if possible, otherwise the second-to-last byte.
1749  *
1750  * If the sequence starts with a value other than the above and its MSB
1751  * is set, it must be a two-byte sequence representing JIS X 0208 characters
1752  * with both bytes ranging between 0xa1 and 0xfe.  The last byte is
1753  * incremented if possible, otherwise the second-to-last byte.
1754  *
1755  * Otherwise, the sequence is a single-byte ASCII character. It is
1756  * incremented up to 0x7f.
1757  */
1758 static bool
pg_eucjp_increment(unsigned char * charptr,int length)1759 pg_eucjp_increment(unsigned char *charptr, int length)
1760 {
1761 	unsigned char c1,
1762 				c2;
1763 	int			i;
1764 
1765 	c1 = *charptr;
1766 
1767 	switch (c1)
1768 	{
1769 		case SS2:				/* JIS X 0201 */
1770 			if (length != 2)
1771 				return false;
1772 
1773 			c2 = charptr[1];
1774 
1775 			if (c2 >= 0xdf)
1776 				charptr[0] = charptr[1] = 0xa1;
1777 			else if (c2 < 0xa1)
1778 				charptr[1] = 0xa1;
1779 			else
1780 				charptr[1]++;
1781 			break;
1782 
1783 		case SS3:				/* JIS X 0212 */
1784 			if (length != 3)
1785 				return false;
1786 
1787 			for (i = 2; i > 0; i--)
1788 			{
1789 				c2 = charptr[i];
1790 				if (c2 < 0xa1)
1791 				{
1792 					charptr[i] = 0xa1;
1793 					return true;
1794 				}
1795 				else if (c2 < 0xfe)
1796 				{
1797 					charptr[i]++;
1798 					return true;
1799 				}
1800 			}
1801 
1802 			/* Out of 3-byte code region */
1803 			return false;
1804 
1805 		default:
1806 			if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
1807 			{
1808 				if (length != 2)
1809 					return false;
1810 
1811 				for (i = 1; i >= 0; i--)
1812 				{
1813 					c2 = charptr[i];
1814 					if (c2 < 0xa1)
1815 					{
1816 						charptr[i] = 0xa1;
1817 						return true;
1818 					}
1819 					else if (c2 < 0xfe)
1820 					{
1821 						charptr[i]++;
1822 						return true;
1823 					}
1824 				}
1825 
1826 				/* Out of 2 byte code region */
1827 				return false;
1828 			}
1829 			else
1830 			{					/* ASCII, single byte */
1831 				if (c1 > 0x7e)
1832 					return false;
1833 				(*charptr)++;
1834 			}
1835 			break;
1836 	}
1837 
1838 	return true;
1839 }
1840 #endif							/* !FRONTEND */
1841 
1842 
1843 /*
1844  *-------------------------------------------------------------------
1845  * encoding info table
1846  * XXX must be sorted by the same order as enum pg_enc (in mb/pg_wchar.h)
1847  *-------------------------------------------------------------------
1848  */
1849 const pg_wchar_tbl pg_wchar_table[] = {
1850 	{pg_ascii2wchar_with_len, pg_wchar2single_with_len, pg_ascii_mblen, pg_ascii_dsplen, pg_ascii_verifier, 1}, /* PG_SQL_ASCII */
1851 	{pg_eucjp2wchar_with_len, pg_wchar2euc_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifier, 3},	/* PG_EUC_JP */
1852 	{pg_euccn2wchar_with_len, pg_wchar2euc_with_len, pg_euccn_mblen, pg_euccn_dsplen, pg_euccn_verifier, 2},	/* PG_EUC_CN */
1853 	{pg_euckr2wchar_with_len, pg_wchar2euc_with_len, pg_euckr_mblen, pg_euckr_dsplen, pg_euckr_verifier, 3},	/* PG_EUC_KR */
1854 	{pg_euctw2wchar_with_len, pg_wchar2euc_with_len, pg_euctw_mblen, pg_euctw_dsplen, pg_euctw_verifier, 4},	/* PG_EUC_TW */
1855 	{pg_eucjp2wchar_with_len, pg_wchar2euc_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifier, 3},	/* PG_EUC_JIS_2004 */
1856 	{pg_utf2wchar_with_len, pg_wchar2utf_with_len, pg_utf_mblen, pg_utf_dsplen, pg_utf8_verifier, 4},	/* PG_UTF8 */
1857 	{pg_mule2wchar_with_len, pg_wchar2mule_with_len, pg_mule_mblen, pg_mule_dsplen, pg_mule_verifier, 4},	/* PG_MULE_INTERNAL */
1858 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN1 */
1859 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN2 */
1860 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN3 */
1861 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN4 */
1862 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN5 */
1863 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN6 */
1864 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN7 */
1865 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN8 */
1866 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN9 */
1867 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN10 */
1868 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1256 */
1869 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1258 */
1870 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN866 */
1871 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN874 */
1872 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_KOI8R */
1873 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1251 */
1874 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1252 */
1875 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-5 */
1876 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-6 */
1877 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-7 */
1878 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-8 */
1879 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1250 */
1880 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1253 */
1881 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1254 */
1882 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1255 */
1883 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1257 */
1884 	{pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_KOI8U */
1885 	{0, 0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifier, 2}, /* PG_SJIS */
1886 	{0, 0, pg_big5_mblen, pg_big5_dsplen, pg_big5_verifier, 2}, /* PG_BIG5 */
1887 	{0, 0, pg_gbk_mblen, pg_gbk_dsplen, pg_gbk_verifier, 2},	/* PG_GBK */
1888 	{0, 0, pg_uhc_mblen, pg_uhc_dsplen, pg_uhc_verifier, 2},	/* PG_UHC */
1889 	{0, 0, pg_gb18030_mblen, pg_gb18030_dsplen, pg_gb18030_verifier, 4},	/* PG_GB18030 */
1890 	{0, 0, pg_johab_mblen, pg_johab_dsplen, pg_johab_verifier, 3},	/* PG_JOHAB */
1891 	{0, 0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifier, 2}	/* PG_SHIFT_JIS_2004 */
1892 };
1893 
1894 /* returns the byte length of a word for mule internal code */
1895 int
pg_mic_mblen(const unsigned char * mbstr)1896 pg_mic_mblen(const unsigned char *mbstr)
1897 {
1898 	return pg_mule_mblen(mbstr);
1899 }
1900 
1901 /*
1902  * Returns the byte length of a multibyte character.
1903  */
1904 int
pg_encoding_mblen(int encoding,const char * mbstr)1905 pg_encoding_mblen(int encoding, const char *mbstr)
1906 {
1907 	return (PG_VALID_ENCODING(encoding) ?
1908 			pg_wchar_table[encoding].mblen((const unsigned char *) mbstr) :
1909 			pg_wchar_table[PG_SQL_ASCII].mblen((const unsigned char *) mbstr));
1910 }
1911 
1912 /*
1913  * Returns the display length of a multibyte character.
1914  */
1915 int
pg_encoding_dsplen(int encoding,const char * mbstr)1916 pg_encoding_dsplen(int encoding, const char *mbstr)
1917 {
1918 	return (PG_VALID_ENCODING(encoding) ?
1919 			pg_wchar_table[encoding].dsplen((const unsigned char *) mbstr) :
1920 			pg_wchar_table[PG_SQL_ASCII].dsplen((const unsigned char *) mbstr));
1921 }
1922 
1923 /*
1924  * Verify the first multibyte character of the given string.
1925  * Return its byte length if good, -1 if bad.  (See comments above for
1926  * full details of the mbverify API.)
1927  */
1928 int
pg_encoding_verifymb(int encoding,const char * mbstr,int len)1929 pg_encoding_verifymb(int encoding, const char *mbstr, int len)
1930 {
1931 	return (PG_VALID_ENCODING(encoding) ?
1932 			pg_wchar_table[encoding].mbverify((const unsigned char *) mbstr, len) :
1933 			pg_wchar_table[PG_SQL_ASCII].mbverify((const unsigned char *) mbstr, len));
1934 }
1935 
1936 /*
1937  * fetch maximum length of a given encoding
1938  */
1939 int
pg_encoding_max_length(int encoding)1940 pg_encoding_max_length(int encoding)
1941 {
1942 	Assert(PG_VALID_ENCODING(encoding));
1943 
1944 	return pg_wchar_table[encoding].maxmblen;
1945 }
1946 
1947 #ifndef FRONTEND
1948 
1949 /*
1950  * fetch maximum length of the encoding for the current database
1951  */
1952 int
pg_database_encoding_max_length(void)1953 pg_database_encoding_max_length(void)
1954 {
1955 	return pg_wchar_table[GetDatabaseEncoding()].maxmblen;
1956 }
1957 
1958 /*
1959  * get the character incrementer for the encoding for the current database
1960  */
1961 mbcharacter_incrementer
pg_database_encoding_character_incrementer(void)1962 pg_database_encoding_character_incrementer(void)
1963 {
1964 	/*
1965 	 * Eventually it might be best to add a field to pg_wchar_table[], but for
1966 	 * now we just use a switch.
1967 	 */
1968 	switch (GetDatabaseEncoding())
1969 	{
1970 		case PG_UTF8:
1971 			return pg_utf8_increment;
1972 
1973 		case PG_EUC_JP:
1974 			return pg_eucjp_increment;
1975 
1976 		default:
1977 			return pg_generic_charinc;
1978 	}
1979 }
1980 
1981 /*
1982  * Verify mbstr to make sure that it is validly encoded in the current
1983  * database encoding.  Otherwise same as pg_verify_mbstr().
1984  */
1985 bool
pg_verifymbstr(const char * mbstr,int len,bool noError)1986 pg_verifymbstr(const char *mbstr, int len, bool noError)
1987 {
1988 	return
1989 		pg_verify_mbstr_len(GetDatabaseEncoding(), mbstr, len, noError) >= 0;
1990 }
1991 
1992 /*
1993  * Verify mbstr to make sure that it is validly encoded in the specified
1994  * encoding.
1995  */
1996 bool
pg_verify_mbstr(int encoding,const char * mbstr,int len,bool noError)1997 pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
1998 {
1999 	return pg_verify_mbstr_len(encoding, mbstr, len, noError) >= 0;
2000 }
2001 
2002 /*
2003  * Verify mbstr to make sure that it is validly encoded in the specified
2004  * encoding.
2005  *
2006  * mbstr is not necessarily zero terminated; length of mbstr is
2007  * specified by len.
2008  *
2009  * If OK, return length of string in the encoding.
2010  * If a problem is found, return -1 when noError is
2011  * true; when noError is false, ereport() a descriptive message.
2012  */
2013 int
pg_verify_mbstr_len(int encoding,const char * mbstr,int len,bool noError)2014 pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
2015 {
2016 	mbverifier	mbverify;
2017 	int			mb_len;
2018 
2019 	Assert(PG_VALID_ENCODING(encoding));
2020 
2021 	/*
2022 	 * In single-byte encodings, we need only reject nulls (\0).
2023 	 */
2024 	if (pg_encoding_max_length(encoding) <= 1)
2025 	{
2026 		const char *nullpos = memchr(mbstr, 0, len);
2027 
2028 		if (nullpos == NULL)
2029 			return len;
2030 		if (noError)
2031 			return -1;
2032 		report_invalid_encoding(encoding, nullpos, 1);
2033 	}
2034 
2035 	/* fetch function pointer just once */
2036 	mbverify = pg_wchar_table[encoding].mbverify;
2037 
2038 	mb_len = 0;
2039 
2040 	while (len > 0)
2041 	{
2042 		int			l;
2043 
2044 		/* fast path for ASCII-subset characters */
2045 		if (!IS_HIGHBIT_SET(*mbstr))
2046 		{
2047 			if (*mbstr != '\0')
2048 			{
2049 				mb_len++;
2050 				mbstr++;
2051 				len--;
2052 				continue;
2053 			}
2054 			if (noError)
2055 				return -1;
2056 			report_invalid_encoding(encoding, mbstr, len);
2057 		}
2058 
2059 		l = (*mbverify) ((const unsigned char *) mbstr, len);
2060 
2061 		if (l < 0)
2062 		{
2063 			if (noError)
2064 				return -1;
2065 			report_invalid_encoding(encoding, mbstr, len);
2066 		}
2067 
2068 		mbstr += l;
2069 		len -= l;
2070 		mb_len++;
2071 	}
2072 	return mb_len;
2073 }
2074 
2075 /*
2076  * check_encoding_conversion_args: check arguments of a conversion function
2077  *
2078  * "expected" arguments can be either an encoding ID or -1 to indicate that
2079  * the caller will check whether it accepts the ID.
2080  *
2081  * Note: the errors here are not really user-facing, so elog instead of
2082  * ereport seems sufficient.  Also, we trust that the "expected" encoding
2083  * arguments are valid encoding IDs, but we don't trust the actuals.
2084  */
2085 void
check_encoding_conversion_args(int src_encoding,int dest_encoding,int len,int expected_src_encoding,int expected_dest_encoding)2086 check_encoding_conversion_args(int src_encoding,
2087 							   int dest_encoding,
2088 							   int len,
2089 							   int expected_src_encoding,
2090 							   int expected_dest_encoding)
2091 {
2092 	if (!PG_VALID_ENCODING(src_encoding))
2093 		elog(ERROR, "invalid source encoding ID: %d", src_encoding);
2094 	if (src_encoding != expected_src_encoding && expected_src_encoding >= 0)
2095 		elog(ERROR, "expected source encoding \"%s\", but got \"%s\"",
2096 			 pg_enc2name_tbl[expected_src_encoding].name,
2097 			 pg_enc2name_tbl[src_encoding].name);
2098 	if (!PG_VALID_ENCODING(dest_encoding))
2099 		elog(ERROR, "invalid destination encoding ID: %d", dest_encoding);
2100 	if (dest_encoding != expected_dest_encoding && expected_dest_encoding >= 0)
2101 		elog(ERROR, "expected destination encoding \"%s\", but got \"%s\"",
2102 			 pg_enc2name_tbl[expected_dest_encoding].name,
2103 			 pg_enc2name_tbl[dest_encoding].name);
2104 	if (len < 0)
2105 		elog(ERROR, "encoding conversion length must not be negative");
2106 }
2107 
2108 /*
2109  * report_invalid_encoding: complain about invalid multibyte character
2110  *
2111  * note: len is remaining length of string, not length of character;
2112  * len must be greater than zero, as we always examine the first byte.
2113  */
2114 void
report_invalid_encoding(int encoding,const char * mbstr,int len)2115 report_invalid_encoding(int encoding, const char *mbstr, int len)
2116 {
2117 	int			l = pg_encoding_mblen(encoding, mbstr);
2118 	char		buf[8 * 5 + 1];
2119 	char	   *p = buf;
2120 	int			j,
2121 				jlimit;
2122 
2123 	jlimit = Min(l, len);
2124 	jlimit = Min(jlimit, 8);	/* prevent buffer overrun */
2125 
2126 	for (j = 0; j < jlimit; j++)
2127 	{
2128 		p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
2129 		if (j < jlimit - 1)
2130 			p += sprintf(p, " ");
2131 	}
2132 
2133 	ereport(ERROR,
2134 			(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
2135 			 errmsg("invalid byte sequence for encoding \"%s\": %s",
2136 					pg_enc2name_tbl[encoding].name,
2137 					buf)));
2138 }
2139 
2140 /*
2141  * report_untranslatable_char: complain about untranslatable character
2142  *
2143  * note: len is remaining length of string, not length of character;
2144  * len must be greater than zero, as we always examine the first byte.
2145  */
2146 void
report_untranslatable_char(int src_encoding,int dest_encoding,const char * mbstr,int len)2147 report_untranslatable_char(int src_encoding, int dest_encoding,
2148 						   const char *mbstr, int len)
2149 {
2150 	int			l = pg_encoding_mblen(src_encoding, mbstr);
2151 	char		buf[8 * 5 + 1];
2152 	char	   *p = buf;
2153 	int			j,
2154 				jlimit;
2155 
2156 	jlimit = Min(l, len);
2157 	jlimit = Min(jlimit, 8);	/* prevent buffer overrun */
2158 
2159 	for (j = 0; j < jlimit; j++)
2160 	{
2161 		p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
2162 		if (j < jlimit - 1)
2163 			p += sprintf(p, " ");
2164 	}
2165 
2166 	ereport(ERROR,
2167 			(errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
2168 			 errmsg("character with byte sequence %s in encoding \"%s\" has no equivalent in encoding \"%s\"",
2169 					buf,
2170 					pg_enc2name_tbl[src_encoding].name,
2171 					pg_enc2name_tbl[dest_encoding].name)));
2172 }
2173 
2174 #endif							/* !FRONTEND */
2175