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