1 /*-------------------------------------------------------------------------
2  *
3  * regc_pg_locale.c
4  *	  ctype functions adapted to work on pg_wchar (a/k/a chr),
5  *	  and functions to cache the results of wholesale ctype probing.
6  *
7  * This file is #included by regcomp.c; it's not meant to compile standalone.
8  *
9  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * IDENTIFICATION
13  *	  src/backend/regex/regc_pg_locale.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #include "catalog/pg_collation.h"
19 #include "utils/pg_locale.h"
20 
21 /*
22  * To provide as much functionality as possible on a variety of platforms,
23  * without going so far as to implement everything from scratch, we use
24  * several implementation strategies depending on the situation:
25  *
26  * 1. In C/POSIX collations, we use hard-wired code.  We can't depend on
27  * the <ctype.h> functions since those will obey LC_CTYPE.  Note that these
28  * collations don't give a fig about multibyte characters.
29  *
30  * 2. In the "default" collation (which is supposed to obey LC_CTYPE):
31  *
32  * 2a. When working in UTF8 encoding, we use the <wctype.h> functions if
33  * available.  This assumes that every platform uses Unicode codepoints
34  * directly as the wchar_t representation of Unicode.  On some platforms
35  * wchar_t is only 16 bits wide, so we have to punt for codepoints > 0xFFFF.
36  *
37  * 2b. In all other encodings, or on machines that lack <wctype.h>, we use
38  * the <ctype.h> functions for pg_wchar values up to 255, and punt for values
39  * above that.  This is only 100% correct in single-byte encodings such as
40  * LATINn.  However, non-Unicode multibyte encodings are mostly Far Eastern
41  * character sets for which the properties being tested here aren't very
42  * relevant for higher code values anyway.  The difficulty with using the
43  * <wctype.h> functions with non-Unicode multibyte encodings is that we can
44  * have no certainty that the platform's wchar_t representation matches
45  * what we do in pg_wchar conversions.
46  *
47  * 3. Other collations are only supported on platforms that HAVE_LOCALE_T.
48  * Here, we use the locale_t-extended forms of the <wctype.h> and <ctype.h>
49  * functions, under exactly the same cases as #2.
50  *
51  * There is one notable difference between cases 2 and 3: in the "default"
52  * collation we force ASCII letters to follow ASCII upcase/downcase rules,
53  * while in a non-default collation we just let the library functions do what
54  * they will.  The case where this matters is treatment of I/i in Turkish,
55  * and the behavior is meant to match the upper()/lower() SQL functions.
56  *
57  * We store the active collation setting in static variables.  In principle
58  * it could be passed down to here via the regex library's "struct vars" data
59  * structure; but that would require somewhat invasive changes in the regex
60  * library, and right now there's no real benefit to be gained from that.
61  *
62  * NB: the coding here assumes pg_wchar is an unsigned type.
63  */
64 
65 typedef enum
66 {
67 	PG_REGEX_LOCALE_C,			/* C locale (encoding independent) */
68 	PG_REGEX_LOCALE_WIDE,		/* Use <wctype.h> functions */
69 	PG_REGEX_LOCALE_1BYTE,		/* Use <ctype.h> functions */
70 	PG_REGEX_LOCALE_WIDE_L,		/* Use locale_t <wctype.h> functions */
71 	PG_REGEX_LOCALE_1BYTE_L		/* Use locale_t <ctype.h> functions */
72 } PG_Locale_Strategy;
73 
74 static PG_Locale_Strategy pg_regex_strategy;
75 static pg_locale_t pg_regex_locale;
76 static Oid	pg_regex_collation;
77 
78 /*
79  * Hard-wired character properties for C locale
80  */
81 #define PG_ISDIGIT	0x01
82 #define PG_ISALPHA	0x02
83 #define PG_ISALNUM	(PG_ISDIGIT | PG_ISALPHA)
84 #define PG_ISUPPER	0x04
85 #define PG_ISLOWER	0x08
86 #define PG_ISGRAPH	0x10
87 #define PG_ISPRINT	0x20
88 #define PG_ISPUNCT	0x40
89 #define PG_ISSPACE	0x80
90 
91 static const unsigned char pg_char_properties[128] = {
92 	 /* NUL */ 0,
93 	 /* ^A */ 0,
94 	 /* ^B */ 0,
95 	 /* ^C */ 0,
96 	 /* ^D */ 0,
97 	 /* ^E */ 0,
98 	 /* ^F */ 0,
99 	 /* ^G */ 0,
100 	 /* ^H */ 0,
101 	 /* ^I */ PG_ISSPACE,
102 	 /* ^J */ PG_ISSPACE,
103 	 /* ^K */ PG_ISSPACE,
104 	 /* ^L */ PG_ISSPACE,
105 	 /* ^M */ PG_ISSPACE,
106 	 /* ^N */ 0,
107 	 /* ^O */ 0,
108 	 /* ^P */ 0,
109 	 /* ^Q */ 0,
110 	 /* ^R */ 0,
111 	 /* ^S */ 0,
112 	 /* ^T */ 0,
113 	 /* ^U */ 0,
114 	 /* ^V */ 0,
115 	 /* ^W */ 0,
116 	 /* ^X */ 0,
117 	 /* ^Y */ 0,
118 	 /* ^Z */ 0,
119 	 /* ^[ */ 0,
120 	 /* ^\ */ 0,
121 	 /* ^] */ 0,
122 	 /* ^^ */ 0,
123 	 /* ^_ */ 0,
124 	 /* */ PG_ISPRINT | PG_ISSPACE,
125 	 /* !  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
126 	 /* "  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
127 	 /* #  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
128 	 /* $  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
129 	 /* %  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
130 	 /* &  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
131 	 /* '  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
132 	 /* (  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
133 	 /* )  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
134 	 /* *  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
135 	 /* +  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
136 	 /* ,  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
137 	 /* -  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
138 	 /* .  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
139 	 /* /  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
140 	 /* 0  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
141 	 /* 1  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
142 	 /* 2  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
143 	 /* 3  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
144 	 /* 4  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
145 	 /* 5  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
146 	 /* 6  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
147 	 /* 7  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
148 	 /* 8  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
149 	 /* 9  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
150 	 /* :  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
151 	 /* ;  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
152 	 /* <  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
153 	 /* =  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
154 	 /* >  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
155 	 /* ?  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
156 	 /* @  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
157 	 /* A  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
158 	 /* B  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
159 	 /* C  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
160 	 /* D  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
161 	 /* E  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
162 	 /* F  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
163 	 /* G  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
164 	 /* H  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
165 	 /* I  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
166 	 /* J  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
167 	 /* K  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
168 	 /* L  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
169 	 /* M  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
170 	 /* N  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
171 	 /* O  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
172 	 /* P  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
173 	 /* Q  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
174 	 /* R  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
175 	 /* S  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
176 	 /* T  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
177 	 /* U  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
178 	 /* V  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
179 	 /* W  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
180 	 /* X  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
181 	 /* Y  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
182 	 /* Z  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
183 	 /* [  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
184 	 /* \  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
185 	 /* ]  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
186 	 /* ^  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
187 	 /* _  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
188 	 /* `  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
189 	 /* a  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
190 	 /* b  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
191 	 /* c  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
192 	 /* d  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
193 	 /* e  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
194 	 /* f  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
195 	 /* g  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
196 	 /* h  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
197 	 /* i  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
198 	 /* j  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
199 	 /* k  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
200 	 /* l  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
201 	 /* m  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
202 	 /* n  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
203 	 /* o  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
204 	 /* p  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
205 	 /* q  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
206 	 /* r  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
207 	 /* s  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
208 	 /* t  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
209 	 /* u  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
210 	 /* v  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
211 	 /* w  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
212 	 /* x  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
213 	 /* y  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
214 	 /* z  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
215 	 /* {  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
216 	 /* |  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
217 	 /* }  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
218 	 /* ~  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
219 	 /* DEL */ 0
220 };
221 
222 
223 /*
224  * pg_set_regex_collation: set collation for these functions to obey
225  *
226  * This is called when beginning compilation or execution of a regexp.
227  * Since there's no need for re-entrancy of regexp operations, it's okay
228  * to store the results in static variables.
229  */
230 void
pg_set_regex_collation(Oid collation)231 pg_set_regex_collation(Oid collation)
232 {
233 	if (lc_ctype_is_c(collation))
234 	{
235 		/* C/POSIX collations use this path regardless of database encoding */
236 		pg_regex_strategy = PG_REGEX_LOCALE_C;
237 		pg_regex_locale = 0;
238 		pg_regex_collation = C_COLLATION_OID;
239 	}
240 	else
241 	{
242 		if (collation == DEFAULT_COLLATION_OID)
243 			pg_regex_locale = 0;
244 		else if (OidIsValid(collation))
245 		{
246 			/*
247 			 * NB: pg_newlocale_from_collation will fail if not HAVE_LOCALE_T;
248 			 * the case of pg_regex_locale != 0 but not HAVE_LOCALE_T does not
249 			 * have to be considered below.
250 			 */
251 			pg_regex_locale = pg_newlocale_from_collation(collation);
252 		}
253 		else
254 		{
255 			/*
256 			 * This typically means that the parser could not resolve a
257 			 * conflict of implicit collations, so report it that way.
258 			 */
259 			ereport(ERROR,
260 					(errcode(ERRCODE_INDETERMINATE_COLLATION),
261 					 errmsg("could not determine which collation to use for regular expression"),
262 					 errhint("Use the COLLATE clause to set the collation explicitly.")));
263 		}
264 
265 #ifdef USE_WIDE_UPPER_LOWER
266 		if (GetDatabaseEncoding() == PG_UTF8)
267 		{
268 			if (pg_regex_locale)
269 				pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L;
270 			else
271 				pg_regex_strategy = PG_REGEX_LOCALE_WIDE;
272 		}
273 		else
274 #endif   /* USE_WIDE_UPPER_LOWER */
275 		{
276 			if (pg_regex_locale)
277 				pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L;
278 			else
279 				pg_regex_strategy = PG_REGEX_LOCALE_1BYTE;
280 		}
281 
282 		pg_regex_collation = collation;
283 	}
284 }
285 
286 static int
pg_wc_isdigit(pg_wchar c)287 pg_wc_isdigit(pg_wchar c)
288 {
289 	switch (pg_regex_strategy)
290 	{
291 		case PG_REGEX_LOCALE_C:
292 			return (c <= (pg_wchar) 127 &&
293 					(pg_char_properties[c] & PG_ISDIGIT));
294 		case PG_REGEX_LOCALE_WIDE:
295 #ifdef USE_WIDE_UPPER_LOWER
296 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
297 				return iswdigit((wint_t) c);
298 #endif
299 			/* FALL THRU */
300 		case PG_REGEX_LOCALE_1BYTE:
301 			return (c <= (pg_wchar) UCHAR_MAX &&
302 					isdigit((unsigned char) c));
303 		case PG_REGEX_LOCALE_WIDE_L:
304 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
305 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
306 				return iswdigit_l((wint_t) c, pg_regex_locale);
307 #endif
308 			/* FALL THRU */
309 		case PG_REGEX_LOCALE_1BYTE_L:
310 #ifdef HAVE_LOCALE_T
311 			return (c <= (pg_wchar) UCHAR_MAX &&
312 					isdigit_l((unsigned char) c, pg_regex_locale));
313 #endif
314 			break;
315 	}
316 	return 0;					/* can't get here, but keep compiler quiet */
317 }
318 
319 static int
pg_wc_isalpha(pg_wchar c)320 pg_wc_isalpha(pg_wchar c)
321 {
322 	switch (pg_regex_strategy)
323 	{
324 		case PG_REGEX_LOCALE_C:
325 			return (c <= (pg_wchar) 127 &&
326 					(pg_char_properties[c] & PG_ISALPHA));
327 		case PG_REGEX_LOCALE_WIDE:
328 #ifdef USE_WIDE_UPPER_LOWER
329 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
330 				return iswalpha((wint_t) c);
331 #endif
332 			/* FALL THRU */
333 		case PG_REGEX_LOCALE_1BYTE:
334 			return (c <= (pg_wchar) UCHAR_MAX &&
335 					isalpha((unsigned char) c));
336 		case PG_REGEX_LOCALE_WIDE_L:
337 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
338 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
339 				return iswalpha_l((wint_t) c, pg_regex_locale);
340 #endif
341 			/* FALL THRU */
342 		case PG_REGEX_LOCALE_1BYTE_L:
343 #ifdef HAVE_LOCALE_T
344 			return (c <= (pg_wchar) UCHAR_MAX &&
345 					isalpha_l((unsigned char) c, pg_regex_locale));
346 #endif
347 			break;
348 	}
349 	return 0;					/* can't get here, but keep compiler quiet */
350 }
351 
352 static int
pg_wc_isalnum(pg_wchar c)353 pg_wc_isalnum(pg_wchar c)
354 {
355 	switch (pg_regex_strategy)
356 	{
357 		case PG_REGEX_LOCALE_C:
358 			return (c <= (pg_wchar) 127 &&
359 					(pg_char_properties[c] & PG_ISALNUM));
360 		case PG_REGEX_LOCALE_WIDE:
361 #ifdef USE_WIDE_UPPER_LOWER
362 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
363 				return iswalnum((wint_t) c);
364 #endif
365 			/* FALL THRU */
366 		case PG_REGEX_LOCALE_1BYTE:
367 			return (c <= (pg_wchar) UCHAR_MAX &&
368 					isalnum((unsigned char) c));
369 		case PG_REGEX_LOCALE_WIDE_L:
370 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
371 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
372 				return iswalnum_l((wint_t) c, pg_regex_locale);
373 #endif
374 			/* FALL THRU */
375 		case PG_REGEX_LOCALE_1BYTE_L:
376 #ifdef HAVE_LOCALE_T
377 			return (c <= (pg_wchar) UCHAR_MAX &&
378 					isalnum_l((unsigned char) c, pg_regex_locale));
379 #endif
380 			break;
381 	}
382 	return 0;					/* can't get here, but keep compiler quiet */
383 }
384 
385 static int
pg_wc_isupper(pg_wchar c)386 pg_wc_isupper(pg_wchar c)
387 {
388 	switch (pg_regex_strategy)
389 	{
390 		case PG_REGEX_LOCALE_C:
391 			return (c <= (pg_wchar) 127 &&
392 					(pg_char_properties[c] & PG_ISUPPER));
393 		case PG_REGEX_LOCALE_WIDE:
394 #ifdef USE_WIDE_UPPER_LOWER
395 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
396 				return iswupper((wint_t) c);
397 #endif
398 			/* FALL THRU */
399 		case PG_REGEX_LOCALE_1BYTE:
400 			return (c <= (pg_wchar) UCHAR_MAX &&
401 					isupper((unsigned char) c));
402 		case PG_REGEX_LOCALE_WIDE_L:
403 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
404 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
405 				return iswupper_l((wint_t) c, pg_regex_locale);
406 #endif
407 			/* FALL THRU */
408 		case PG_REGEX_LOCALE_1BYTE_L:
409 #ifdef HAVE_LOCALE_T
410 			return (c <= (pg_wchar) UCHAR_MAX &&
411 					isupper_l((unsigned char) c, pg_regex_locale));
412 #endif
413 			break;
414 	}
415 	return 0;					/* can't get here, but keep compiler quiet */
416 }
417 
418 static int
pg_wc_islower(pg_wchar c)419 pg_wc_islower(pg_wchar c)
420 {
421 	switch (pg_regex_strategy)
422 	{
423 		case PG_REGEX_LOCALE_C:
424 			return (c <= (pg_wchar) 127 &&
425 					(pg_char_properties[c] & PG_ISLOWER));
426 		case PG_REGEX_LOCALE_WIDE:
427 #ifdef USE_WIDE_UPPER_LOWER
428 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
429 				return iswlower((wint_t) c);
430 #endif
431 			/* FALL THRU */
432 		case PG_REGEX_LOCALE_1BYTE:
433 			return (c <= (pg_wchar) UCHAR_MAX &&
434 					islower((unsigned char) c));
435 		case PG_REGEX_LOCALE_WIDE_L:
436 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
437 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
438 				return iswlower_l((wint_t) c, pg_regex_locale);
439 #endif
440 			/* FALL THRU */
441 		case PG_REGEX_LOCALE_1BYTE_L:
442 #ifdef HAVE_LOCALE_T
443 			return (c <= (pg_wchar) UCHAR_MAX &&
444 					islower_l((unsigned char) c, pg_regex_locale));
445 #endif
446 			break;
447 	}
448 	return 0;					/* can't get here, but keep compiler quiet */
449 }
450 
451 static int
pg_wc_isgraph(pg_wchar c)452 pg_wc_isgraph(pg_wchar c)
453 {
454 	switch (pg_regex_strategy)
455 	{
456 		case PG_REGEX_LOCALE_C:
457 			return (c <= (pg_wchar) 127 &&
458 					(pg_char_properties[c] & PG_ISGRAPH));
459 		case PG_REGEX_LOCALE_WIDE:
460 #ifdef USE_WIDE_UPPER_LOWER
461 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
462 				return iswgraph((wint_t) c);
463 #endif
464 			/* FALL THRU */
465 		case PG_REGEX_LOCALE_1BYTE:
466 			return (c <= (pg_wchar) UCHAR_MAX &&
467 					isgraph((unsigned char) c));
468 		case PG_REGEX_LOCALE_WIDE_L:
469 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
470 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
471 				return iswgraph_l((wint_t) c, pg_regex_locale);
472 #endif
473 			/* FALL THRU */
474 		case PG_REGEX_LOCALE_1BYTE_L:
475 #ifdef HAVE_LOCALE_T
476 			return (c <= (pg_wchar) UCHAR_MAX &&
477 					isgraph_l((unsigned char) c, pg_regex_locale));
478 #endif
479 			break;
480 	}
481 	return 0;					/* can't get here, but keep compiler quiet */
482 }
483 
484 static int
pg_wc_isprint(pg_wchar c)485 pg_wc_isprint(pg_wchar c)
486 {
487 	switch (pg_regex_strategy)
488 	{
489 		case PG_REGEX_LOCALE_C:
490 			return (c <= (pg_wchar) 127 &&
491 					(pg_char_properties[c] & PG_ISPRINT));
492 		case PG_REGEX_LOCALE_WIDE:
493 #ifdef USE_WIDE_UPPER_LOWER
494 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
495 				return iswprint((wint_t) c);
496 #endif
497 			/* FALL THRU */
498 		case PG_REGEX_LOCALE_1BYTE:
499 			return (c <= (pg_wchar) UCHAR_MAX &&
500 					isprint((unsigned char) c));
501 		case PG_REGEX_LOCALE_WIDE_L:
502 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
503 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
504 				return iswprint_l((wint_t) c, pg_regex_locale);
505 #endif
506 			/* FALL THRU */
507 		case PG_REGEX_LOCALE_1BYTE_L:
508 #ifdef HAVE_LOCALE_T
509 			return (c <= (pg_wchar) UCHAR_MAX &&
510 					isprint_l((unsigned char) c, pg_regex_locale));
511 #endif
512 			break;
513 	}
514 	return 0;					/* can't get here, but keep compiler quiet */
515 }
516 
517 static int
pg_wc_ispunct(pg_wchar c)518 pg_wc_ispunct(pg_wchar c)
519 {
520 	switch (pg_regex_strategy)
521 	{
522 		case PG_REGEX_LOCALE_C:
523 			return (c <= (pg_wchar) 127 &&
524 					(pg_char_properties[c] & PG_ISPUNCT));
525 		case PG_REGEX_LOCALE_WIDE:
526 #ifdef USE_WIDE_UPPER_LOWER
527 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
528 				return iswpunct((wint_t) c);
529 #endif
530 			/* FALL THRU */
531 		case PG_REGEX_LOCALE_1BYTE:
532 			return (c <= (pg_wchar) UCHAR_MAX &&
533 					ispunct((unsigned char) c));
534 		case PG_REGEX_LOCALE_WIDE_L:
535 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
536 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
537 				return iswpunct_l((wint_t) c, pg_regex_locale);
538 #endif
539 			/* FALL THRU */
540 		case PG_REGEX_LOCALE_1BYTE_L:
541 #ifdef HAVE_LOCALE_T
542 			return (c <= (pg_wchar) UCHAR_MAX &&
543 					ispunct_l((unsigned char) c, pg_regex_locale));
544 #endif
545 			break;
546 	}
547 	return 0;					/* can't get here, but keep compiler quiet */
548 }
549 
550 static int
pg_wc_isspace(pg_wchar c)551 pg_wc_isspace(pg_wchar c)
552 {
553 	switch (pg_regex_strategy)
554 	{
555 		case PG_REGEX_LOCALE_C:
556 			return (c <= (pg_wchar) 127 &&
557 					(pg_char_properties[c] & PG_ISSPACE));
558 		case PG_REGEX_LOCALE_WIDE:
559 #ifdef USE_WIDE_UPPER_LOWER
560 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
561 				return iswspace((wint_t) c);
562 #endif
563 			/* FALL THRU */
564 		case PG_REGEX_LOCALE_1BYTE:
565 			return (c <= (pg_wchar) UCHAR_MAX &&
566 					isspace((unsigned char) c));
567 		case PG_REGEX_LOCALE_WIDE_L:
568 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
569 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
570 				return iswspace_l((wint_t) c, pg_regex_locale);
571 #endif
572 			/* FALL THRU */
573 		case PG_REGEX_LOCALE_1BYTE_L:
574 #ifdef HAVE_LOCALE_T
575 			return (c <= (pg_wchar) UCHAR_MAX &&
576 					isspace_l((unsigned char) c, pg_regex_locale));
577 #endif
578 			break;
579 	}
580 	return 0;					/* can't get here, but keep compiler quiet */
581 }
582 
583 static pg_wchar
pg_wc_toupper(pg_wchar c)584 pg_wc_toupper(pg_wchar c)
585 {
586 	switch (pg_regex_strategy)
587 	{
588 		case PG_REGEX_LOCALE_C:
589 			if (c <= (pg_wchar) 127)
590 				return pg_ascii_toupper((unsigned char) c);
591 			return c;
592 		case PG_REGEX_LOCALE_WIDE:
593 			/* force C behavior for ASCII characters, per comments above */
594 			if (c <= (pg_wchar) 127)
595 				return pg_ascii_toupper((unsigned char) c);
596 #ifdef USE_WIDE_UPPER_LOWER
597 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
598 				return towupper((wint_t) c);
599 #endif
600 			/* FALL THRU */
601 		case PG_REGEX_LOCALE_1BYTE:
602 			/* force C behavior for ASCII characters, per comments above */
603 			if (c <= (pg_wchar) 127)
604 				return pg_ascii_toupper((unsigned char) c);
605 			if (c <= (pg_wchar) UCHAR_MAX)
606 				return toupper((unsigned char) c);
607 			return c;
608 		case PG_REGEX_LOCALE_WIDE_L:
609 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
610 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
611 				return towupper_l((wint_t) c, pg_regex_locale);
612 #endif
613 			/* FALL THRU */
614 		case PG_REGEX_LOCALE_1BYTE_L:
615 #ifdef HAVE_LOCALE_T
616 			if (c <= (pg_wchar) UCHAR_MAX)
617 				return toupper_l((unsigned char) c, pg_regex_locale);
618 #endif
619 			return c;
620 	}
621 	return 0;					/* can't get here, but keep compiler quiet */
622 }
623 
624 static pg_wchar
pg_wc_tolower(pg_wchar c)625 pg_wc_tolower(pg_wchar c)
626 {
627 	switch (pg_regex_strategy)
628 	{
629 		case PG_REGEX_LOCALE_C:
630 			if (c <= (pg_wchar) 127)
631 				return pg_ascii_tolower((unsigned char) c);
632 			return c;
633 		case PG_REGEX_LOCALE_WIDE:
634 			/* force C behavior for ASCII characters, per comments above */
635 			if (c <= (pg_wchar) 127)
636 				return pg_ascii_tolower((unsigned char) c);
637 #ifdef USE_WIDE_UPPER_LOWER
638 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
639 				return towlower((wint_t) c);
640 #endif
641 			/* FALL THRU */
642 		case PG_REGEX_LOCALE_1BYTE:
643 			/* force C behavior for ASCII characters, per comments above */
644 			if (c <= (pg_wchar) 127)
645 				return pg_ascii_tolower((unsigned char) c);
646 			if (c <= (pg_wchar) UCHAR_MAX)
647 				return tolower((unsigned char) c);
648 			return c;
649 		case PG_REGEX_LOCALE_WIDE_L:
650 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
651 			if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
652 				return towlower_l((wint_t) c, pg_regex_locale);
653 #endif
654 			/* FALL THRU */
655 		case PG_REGEX_LOCALE_1BYTE_L:
656 #ifdef HAVE_LOCALE_T
657 			if (c <= (pg_wchar) UCHAR_MAX)
658 				return tolower_l((unsigned char) c, pg_regex_locale);
659 #endif
660 			return c;
661 	}
662 	return 0;					/* can't get here, but keep compiler quiet */
663 }
664 
665 
666 /*
667  * These functions cache the results of probing libc's ctype behavior for
668  * all character codes of interest in a given encoding/collation.  The
669  * result is provided as a "struct cvec", but notice that the representation
670  * is a touch different from a cvec created by regc_cvec.c: we allocate the
671  * chrs[] and ranges[] arrays separately from the struct so that we can
672  * realloc them larger at need.  This is okay since the cvecs made here
673  * should never be freed by freecvec().
674  *
675  * We use malloc not palloc since we mustn't lose control on out-of-memory;
676  * the main regex code expects us to return a failure indication instead.
677  */
678 
679 typedef int (*pg_wc_probefunc) (pg_wchar c);
680 
681 typedef struct pg_ctype_cache
682 {
683 	pg_wc_probefunc probefunc;	/* pg_wc_isalpha or a sibling */
684 	Oid			collation;		/* collation this entry is for */
685 	struct cvec cv;				/* cache entry contents */
686 	struct pg_ctype_cache *next;	/* chain link */
687 } pg_ctype_cache;
688 
689 static pg_ctype_cache *pg_ctype_cache_list = NULL;
690 
691 /*
692  * Add a chr or range to pcc->cv; return false if run out of memory
693  */
694 static bool
store_match(pg_ctype_cache * pcc,pg_wchar chr1,int nchrs)695 store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
696 {
697 	chr		   *newchrs;
698 
699 	if (nchrs > 1)
700 	{
701 		if (pcc->cv.nranges >= pcc->cv.rangespace)
702 		{
703 			pcc->cv.rangespace *= 2;
704 			newchrs = (chr *) realloc(pcc->cv.ranges,
705 									  pcc->cv.rangespace * sizeof(chr) * 2);
706 			if (newchrs == NULL)
707 				return false;
708 			pcc->cv.ranges = newchrs;
709 		}
710 		pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
711 		pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
712 		pcc->cv.nranges++;
713 	}
714 	else
715 	{
716 		assert(nchrs == 1);
717 		if (pcc->cv.nchrs >= pcc->cv.chrspace)
718 		{
719 			pcc->cv.chrspace *= 2;
720 			newchrs = (chr *) realloc(pcc->cv.chrs,
721 									  pcc->cv.chrspace * sizeof(chr));
722 			if (newchrs == NULL)
723 				return false;
724 			pcc->cv.chrs = newchrs;
725 		}
726 		pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
727 	}
728 	return true;
729 }
730 
731 /*
732  * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
733  * chrs satisfying the probe function.  The active collation is the one
734  * previously set by pg_set_regex_collation.  Return NULL if out of memory.
735  *
736  * Note that the result must not be freed or modified by caller.
737  */
738 static struct cvec *
pg_ctype_get_cache(pg_wc_probefunc probefunc)739 pg_ctype_get_cache(pg_wc_probefunc probefunc)
740 {
741 	pg_ctype_cache *pcc;
742 	pg_wchar	max_chr;
743 	pg_wchar	cur_chr;
744 	int			nmatches;
745 	chr		   *newchrs;
746 
747 	/*
748 	 * Do we already have the answer cached?
749 	 */
750 	for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
751 	{
752 		if (pcc->probefunc == probefunc &&
753 			pcc->collation == pg_regex_collation)
754 			return &pcc->cv;
755 	}
756 
757 	/*
758 	 * Nope, so initialize some workspace ...
759 	 */
760 	pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
761 	if (pcc == NULL)
762 		return NULL;
763 	pcc->probefunc = probefunc;
764 	pcc->collation = pg_regex_collation;
765 	pcc->cv.nchrs = 0;
766 	pcc->cv.chrspace = 128;
767 	pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
768 	pcc->cv.nranges = 0;
769 	pcc->cv.rangespace = 64;
770 	pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
771 	if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
772 		goto out_of_memory;
773 
774 	/*
775 	 * Decide how many character codes we ought to look through.  For C locale
776 	 * there's no need to go further than 127.  Otherwise, if the encoding is
777 	 * UTF8 go up to 0x7FF, which is a pretty arbitrary cutoff but we cannot
778 	 * extend it as far as we'd like (say, 0xFFFF, the end of the Basic
779 	 * Multilingual Plane) without creating significant performance issues due
780 	 * to too many characters being fed through the colormap code.  This will
781 	 * need redesign to fix reasonably, but at least for the moment we have
782 	 * all common European languages covered.  Otherwise (not C, not UTF8) go
783 	 * up to 255.  These limits are interrelated with restrictions discussed
784 	 * at the head of this file.
785 	 */
786 	switch (pg_regex_strategy)
787 	{
788 		case PG_REGEX_LOCALE_C:
789 			max_chr = (pg_wchar) 127;
790 			break;
791 		case PG_REGEX_LOCALE_WIDE:
792 		case PG_REGEX_LOCALE_WIDE_L:
793 			max_chr = (pg_wchar) 0x7FF;
794 			break;
795 		case PG_REGEX_LOCALE_1BYTE:
796 		case PG_REGEX_LOCALE_1BYTE_L:
797 			max_chr = (pg_wchar) UCHAR_MAX;
798 			break;
799 		default:
800 			max_chr = 0;		/* can't get here, but keep compiler quiet */
801 			break;
802 	}
803 
804 	/*
805 	 * And scan 'em ...
806 	 */
807 	nmatches = 0;				/* number of consecutive matches */
808 
809 	for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
810 	{
811 		if ((*probefunc) (cur_chr))
812 			nmatches++;
813 		else if (nmatches > 0)
814 		{
815 			if (!store_match(pcc, cur_chr - nmatches, nmatches))
816 				goto out_of_memory;
817 			nmatches = 0;
818 		}
819 	}
820 
821 	if (nmatches > 0)
822 		if (!store_match(pcc, cur_chr - nmatches, nmatches))
823 			goto out_of_memory;
824 
825 	/*
826 	 * We might have allocated more memory than needed, if so free it
827 	 */
828 	if (pcc->cv.nchrs == 0)
829 	{
830 		free(pcc->cv.chrs);
831 		pcc->cv.chrs = NULL;
832 		pcc->cv.chrspace = 0;
833 	}
834 	else if (pcc->cv.nchrs < pcc->cv.chrspace)
835 	{
836 		newchrs = (chr *) realloc(pcc->cv.chrs,
837 								  pcc->cv.nchrs * sizeof(chr));
838 		if (newchrs == NULL)
839 			goto out_of_memory;
840 		pcc->cv.chrs = newchrs;
841 		pcc->cv.chrspace = pcc->cv.nchrs;
842 	}
843 	if (pcc->cv.nranges == 0)
844 	{
845 		free(pcc->cv.ranges);
846 		pcc->cv.ranges = NULL;
847 		pcc->cv.rangespace = 0;
848 	}
849 	else if (pcc->cv.nranges < pcc->cv.rangespace)
850 	{
851 		newchrs = (chr *) realloc(pcc->cv.ranges,
852 								  pcc->cv.nranges * sizeof(chr) * 2);
853 		if (newchrs == NULL)
854 			goto out_of_memory;
855 		pcc->cv.ranges = newchrs;
856 		pcc->cv.rangespace = pcc->cv.nranges;
857 	}
858 
859 	/*
860 	 * Success, link it into cache chain
861 	 */
862 	pcc->next = pg_ctype_cache_list;
863 	pg_ctype_cache_list = pcc;
864 
865 	return &pcc->cv;
866 
867 	/*
868 	 * Failure, clean up
869 	 */
870 out_of_memory:
871 	if (pcc->cv.chrs)
872 		free(pcc->cv.chrs);
873 	if (pcc->cv.ranges)
874 		free(pcc->cv.ranges);
875 	free(pcc);
876 
877 	return NULL;
878 }
879