Line data Source code
1 : /*-----------------------------------------------------------------------
2 : *
3 : * PostgreSQL locale utilities
4 : *
5 : * Portions Copyright (c) 2002-2025, PostgreSQL Global Development Group
6 : *
7 : * src/backend/utils/adt/pg_locale.c
8 : *
9 : *-----------------------------------------------------------------------
10 : */
11 :
12 : /*----------
13 : * Here is how the locale stuff is handled: LC_COLLATE and LC_CTYPE
14 : * are fixed at CREATE DATABASE time, stored in pg_database, and cannot
15 : * be changed. Thus, the effects of strcoll(), strxfrm(), isupper(),
16 : * toupper(), etc. are always in the same fixed locale.
17 : *
18 : * LC_MESSAGES is settable at run time and will take effect
19 : * immediately.
20 : *
21 : * The other categories, LC_MONETARY, LC_NUMERIC, and LC_TIME are
22 : * permanently set to "C", and then we use temporary locale_t
23 : * objects when we need to look up locale data based on the GUCs
24 : * of the same name. Information is cached when the GUCs change.
25 : * The cached information is only used by the formatting functions
26 : * (to_char, etc.) and the money type. For the user, this should all be
27 : * transparent.
28 : *----------
29 : */
30 :
31 :
32 : #include "postgres.h"
33 :
34 : #include <time.h>
35 :
36 : #include "access/htup_details.h"
37 : #include "catalog/pg_collation.h"
38 : #include "catalog/pg_database.h"
39 : #include "common/hashfn.h"
40 : #include "common/string.h"
41 : #include "mb/pg_wchar.h"
42 : #include "miscadmin.h"
43 : #include "utils/builtins.h"
44 : #include "utils/guc_hooks.h"
45 : #include "utils/lsyscache.h"
46 : #include "utils/memutils.h"
47 : #include "utils/pg_locale.h"
48 : #include "utils/pg_locale_c.h"
49 : #include "utils/relcache.h"
50 : #include "utils/syscache.h"
51 :
52 : #ifdef WIN32
53 : #include <shlwapi.h>
54 : #endif
55 :
56 : /* Error triggered for locale-sensitive subroutines */
57 : #define PGLOCALE_SUPPORT_ERROR(provider) \
58 : elog(ERROR, "unsupported collprovider for %s: %c", __func__, provider)
59 :
60 : /*
61 : * This should be large enough that most strings will fit, but small enough
62 : * that we feel comfortable putting it on the stack
63 : */
64 : #define TEXTBUFLEN 1024
65 :
66 : #define MAX_L10N_DATA 80
67 :
68 : /* pg_locale_builtin.c */
69 : extern pg_locale_t create_pg_locale_builtin(Oid collid, MemoryContext context);
70 : extern char *get_collation_actual_version_builtin(const char *collcollate);
71 :
72 : /* pg_locale_icu.c */
73 : #ifdef USE_ICU
74 : extern UCollator *pg_ucol_open(const char *loc_str);
75 : extern char *get_collation_actual_version_icu(const char *collcollate);
76 : #endif
77 : extern pg_locale_t create_pg_locale_icu(Oid collid, MemoryContext context);
78 :
79 : /* pg_locale_libc.c */
80 : extern pg_locale_t create_pg_locale_libc(Oid collid, MemoryContext context);
81 : extern char *get_collation_actual_version_libc(const char *collcollate);
82 :
83 : /* GUC settings */
84 : char *locale_messages;
85 : char *locale_monetary;
86 : char *locale_numeric;
87 : char *locale_time;
88 :
89 : int icu_validation_level = WARNING;
90 :
91 : /*
92 : * lc_time localization cache.
93 : *
94 : * We use only the first 7 or 12 entries of these arrays. The last array
95 : * element is left as NULL for the convenience of outside code that wants
96 : * to sequentially scan these arrays.
97 : */
98 : char *localized_abbrev_days[7 + 1];
99 : char *localized_full_days[7 + 1];
100 : char *localized_abbrev_months[12 + 1];
101 : char *localized_full_months[12 + 1];
102 :
103 : static pg_locale_t default_locale = NULL;
104 :
105 : /* indicates whether locale information cache is valid */
106 : static bool CurrentLocaleConvValid = false;
107 : static bool CurrentLCTimeValid = false;
108 :
109 : /* Cache for collation-related knowledge */
110 :
111 : typedef struct
112 : {
113 : Oid collid; /* hash key: pg_collation OID */
114 : pg_locale_t locale; /* locale_t struct, or 0 if not valid */
115 :
116 : /* needed for simplehash */
117 : uint32 hash;
118 : char status;
119 : } collation_cache_entry;
120 :
121 : #define SH_PREFIX collation_cache
122 : #define SH_ELEMENT_TYPE collation_cache_entry
123 : #define SH_KEY_TYPE Oid
124 : #define SH_KEY collid
125 : #define SH_HASH_KEY(tb, key) murmurhash32((uint32) key)
126 : #define SH_EQUAL(tb, a, b) (a == b)
127 : #define SH_GET_HASH(tb, a) a->hash
128 : #define SH_SCOPE static inline
129 : #define SH_STORE_HASH
130 : #define SH_DECLARE
131 : #define SH_DEFINE
132 : #include "lib/simplehash.h"
133 :
134 : static MemoryContext CollationCacheContext = NULL;
135 : static collation_cache_hash *CollationCache = NULL;
136 :
137 : /*
138 : * The collation cache is often accessed repeatedly for the same collation, so
139 : * remember the last one used.
140 : */
141 : static Oid last_collation_cache_oid = InvalidOid;
142 : static pg_locale_t last_collation_cache_locale = NULL;
143 :
144 : #if defined(WIN32) && defined(LC_MESSAGES)
145 : static char *IsoLocaleName(const char *);
146 : #endif
147 :
148 : /*
149 : * pg_perm_setlocale
150 : *
151 : * This wraps the libc function setlocale(), with two additions. First, when
152 : * changing LC_CTYPE, update gettext's encoding for the current message
153 : * domain. GNU gettext automatically tracks LC_CTYPE on most platforms, but
154 : * not on Windows. Second, if the operation is successful, the corresponding
155 : * LC_XXX environment variable is set to match. By setting the environment
156 : * variable, we ensure that any subsequent use of setlocale(..., "") will
157 : * preserve the settings made through this routine. Of course, LC_ALL must
158 : * also be unset to fully ensure that, but that has to be done elsewhere after
159 : * all the individual LC_XXX variables have been set correctly. (Thank you
160 : * Perl for making this kluge necessary.)
161 : */
162 : char *
163 73624 : pg_perm_setlocale(int category, const char *locale)
164 : {
165 : char *result;
166 : const char *envvar;
167 :
168 : #ifndef WIN32
169 73624 : result = setlocale(category, locale);
170 : #else
171 :
172 : /*
173 : * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
174 : * the given value is good and set it in the environment variables. We
175 : * must ignore attempts to set to "", which means "keep using the old
176 : * environment value".
177 : */
178 : #ifdef LC_MESSAGES
179 : if (category == LC_MESSAGES)
180 : {
181 : result = (char *) locale;
182 : if (locale == NULL || locale[0] == '\0')
183 : return result;
184 : }
185 : else
186 : #endif
187 : result = setlocale(category, locale);
188 : #endif /* WIN32 */
189 :
190 73624 : if (result == NULL)
191 0 : return result; /* fall out immediately on failure */
192 :
193 : /*
194 : * Use the right encoding in translated messages. Under ENABLE_NLS, let
195 : * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
196 : * format strings are ASCII, but database-encoding strings may enter the
197 : * message via %s. This makes the overall message encoding equal to the
198 : * database encoding.
199 : */
200 73624 : if (category == LC_CTYPE)
201 : {
202 : static char save_lc_ctype[LOCALE_NAME_BUFLEN];
203 :
204 : /* copy setlocale() return value before callee invokes it again */
205 36492 : strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
206 36492 : result = save_lc_ctype;
207 :
208 : #ifdef ENABLE_NLS
209 36492 : SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
210 : #else
211 : SetMessageEncoding(GetDatabaseEncoding());
212 : #endif
213 : }
214 :
215 73624 : switch (category)
216 : {
217 3822 : case LC_COLLATE:
218 3822 : envvar = "LC_COLLATE";
219 3822 : break;
220 36492 : case LC_CTYPE:
221 36492 : envvar = "LC_CTYPE";
222 36492 : break;
223 : #ifdef LC_MESSAGES
224 21844 : case LC_MESSAGES:
225 21844 : envvar = "LC_MESSAGES";
226 : #ifdef WIN32
227 : result = IsoLocaleName(locale);
228 : if (result == NULL)
229 : result = (char *) locale;
230 : elog(DEBUG3, "IsoLocaleName() executed; locale: \"%s\"", result);
231 : #endif /* WIN32 */
232 21844 : break;
233 : #endif /* LC_MESSAGES */
234 3822 : case LC_MONETARY:
235 3822 : envvar = "LC_MONETARY";
236 3822 : break;
237 3822 : case LC_NUMERIC:
238 3822 : envvar = "LC_NUMERIC";
239 3822 : break;
240 3822 : case LC_TIME:
241 3822 : envvar = "LC_TIME";
242 3822 : break;
243 0 : default:
244 0 : elog(FATAL, "unrecognized LC category: %d", category);
245 : return NULL; /* keep compiler quiet */
246 : }
247 :
248 73624 : if (setenv(envvar, result, 1) != 0)
249 0 : return NULL;
250 :
251 73624 : return result;
252 : }
253 :
254 :
255 : /*
256 : * Is the locale name valid for the locale category?
257 : *
258 : * If successful, and canonname isn't NULL, a palloc'd copy of the locale's
259 : * canonical name is stored there. This is especially useful for figuring out
260 : * what locale name "" means (ie, the server environment value). (Actually,
261 : * it seems that on most implementations that's the only thing it's good for;
262 : * we could wish that setlocale gave back a canonically spelled version of
263 : * the locale name, but typically it doesn't.)
264 : */
265 : bool
266 69424 : check_locale(int category, const char *locale, char **canonname)
267 : {
268 : char *save;
269 : char *res;
270 :
271 : /* Don't let Windows' non-ASCII locale names in. */
272 69424 : if (!pg_is_ascii(locale))
273 : {
274 0 : ereport(WARNING,
275 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
276 : errmsg("locale name \"%s\" contains non-ASCII characters",
277 : locale)));
278 0 : return false;
279 : }
280 :
281 69424 : if (canonname)
282 1586 : *canonname = NULL; /* in case of failure */
283 :
284 69424 : save = setlocale(category, NULL);
285 69424 : if (!save)
286 0 : return false; /* won't happen, we hope */
287 :
288 : /* save may be pointing at a modifiable scratch variable, see above. */
289 69424 : save = pstrdup(save);
290 :
291 : /* set the locale with setlocale, to see if it accepts it. */
292 69424 : res = setlocale(category, locale);
293 :
294 : /* save canonical name if requested. */
295 69424 : if (res && canonname)
296 1582 : *canonname = pstrdup(res);
297 :
298 : /* restore old value. */
299 69424 : if (!setlocale(category, save))
300 0 : elog(WARNING, "failed to restore old locale \"%s\"", save);
301 69424 : pfree(save);
302 :
303 : /* Don't let Windows' non-ASCII locale names out. */
304 69424 : if (canonname && *canonname && !pg_is_ascii(*canonname))
305 : {
306 0 : ereport(WARNING,
307 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
308 : errmsg("locale name \"%s\" contains non-ASCII characters",
309 : *canonname)));
310 0 : pfree(*canonname);
311 0 : *canonname = NULL;
312 0 : return false;
313 : }
314 :
315 69424 : return (res != NULL);
316 : }
317 :
318 :
319 : /*
320 : * GUC check/assign hooks
321 : *
322 : * For most locale categories, the assign hook doesn't actually set the locale
323 : * permanently, just reset flags so that the next use will cache the
324 : * appropriate values. (See explanation at the top of this file.)
325 : *
326 : * Note: we accept value = "" as selecting the postmaster's environment
327 : * value, whatever it was (so long as the environment setting is legal).
328 : * This will have been locked down by an earlier call to pg_perm_setlocale.
329 : */
330 : bool
331 18202 : check_locale_monetary(char **newval, void **extra, GucSource source)
332 : {
333 18202 : return check_locale(LC_MONETARY, *newval, NULL);
334 : }
335 :
336 : void
337 17990 : assign_locale_monetary(const char *newval, void *extra)
338 : {
339 17990 : CurrentLocaleConvValid = false;
340 17990 : }
341 :
342 : bool
343 18208 : check_locale_numeric(char **newval, void **extra, GucSource source)
344 : {
345 18208 : return check_locale(LC_NUMERIC, *newval, NULL);
346 : }
347 :
348 : void
349 18002 : assign_locale_numeric(const char *newval, void *extra)
350 : {
351 18002 : CurrentLocaleConvValid = false;
352 18002 : }
353 :
354 : bool
355 18202 : check_locale_time(char **newval, void **extra, GucSource source)
356 : {
357 18202 : return check_locale(LC_TIME, *newval, NULL);
358 : }
359 :
360 : void
361 17990 : assign_locale_time(const char *newval, void *extra)
362 : {
363 17990 : CurrentLCTimeValid = false;
364 17990 : }
365 :
366 : /*
367 : * We allow LC_MESSAGES to actually be set globally.
368 : *
369 : * Note: we normally disallow value = "" because it wouldn't have consistent
370 : * semantics (it'd effectively just use the previous value). However, this
371 : * is the value passed for PGC_S_DEFAULT, so don't complain in that case,
372 : * not even if the attempted setting fails due to invalid environment value.
373 : * The idea there is just to accept the environment setting *if possible*
374 : * during startup, until we can read the proper value from postgresql.conf.
375 : */
376 : bool
377 18236 : check_locale_messages(char **newval, void **extra, GucSource source)
378 : {
379 18236 : if (**newval == '\0')
380 : {
381 5010 : if (source == PGC_S_DEFAULT)
382 5010 : return true;
383 : else
384 0 : return false;
385 : }
386 :
387 : /*
388 : * LC_MESSAGES category does not exist everywhere, but accept it anyway
389 : *
390 : * On Windows, we can't even check the value, so accept blindly
391 : */
392 : #if defined(LC_MESSAGES) && !defined(WIN32)
393 13226 : return check_locale(LC_MESSAGES, *newval, NULL);
394 : #else
395 : return true;
396 : #endif
397 : }
398 :
399 : void
400 18022 : assign_locale_messages(const char *newval, void *extra)
401 : {
402 : /*
403 : * LC_MESSAGES category does not exist everywhere, but accept it anyway.
404 : * We ignore failure, as per comment above.
405 : */
406 : #ifdef LC_MESSAGES
407 18022 : (void) pg_perm_setlocale(LC_MESSAGES, newval);
408 : #endif
409 18022 : }
410 :
411 :
412 : /*
413 : * Frees the malloced content of a struct lconv. (But not the struct
414 : * itself.) It's important that this not throw elog(ERROR).
415 : */
416 : static void
417 6 : free_struct_lconv(struct lconv *s)
418 : {
419 6 : free(s->decimal_point);
420 6 : free(s->thousands_sep);
421 6 : free(s->grouping);
422 6 : free(s->int_curr_symbol);
423 6 : free(s->currency_symbol);
424 6 : free(s->mon_decimal_point);
425 6 : free(s->mon_thousands_sep);
426 6 : free(s->mon_grouping);
427 6 : free(s->positive_sign);
428 6 : free(s->negative_sign);
429 6 : }
430 :
431 : /*
432 : * Check that all fields of a struct lconv (or at least, the ones we care
433 : * about) are non-NULL. The field list must match free_struct_lconv().
434 : */
435 : static bool
436 56 : struct_lconv_is_valid(struct lconv *s)
437 : {
438 56 : if (s->decimal_point == NULL)
439 0 : return false;
440 56 : if (s->thousands_sep == NULL)
441 0 : return false;
442 56 : if (s->grouping == NULL)
443 0 : return false;
444 56 : if (s->int_curr_symbol == NULL)
445 0 : return false;
446 56 : if (s->currency_symbol == NULL)
447 0 : return false;
448 56 : if (s->mon_decimal_point == NULL)
449 0 : return false;
450 56 : if (s->mon_thousands_sep == NULL)
451 0 : return false;
452 56 : if (s->mon_grouping == NULL)
453 0 : return false;
454 56 : if (s->positive_sign == NULL)
455 0 : return false;
456 56 : if (s->negative_sign == NULL)
457 0 : return false;
458 56 : return true;
459 : }
460 :
461 :
462 : /*
463 : * Convert the strdup'd string at *str from the specified encoding to the
464 : * database encoding.
465 : */
466 : static void
467 448 : db_encoding_convert(int encoding, char **str)
468 : {
469 : char *pstr;
470 : char *mstr;
471 :
472 : /* convert the string to the database encoding */
473 448 : pstr = pg_any_to_server(*str, strlen(*str), encoding);
474 448 : if (pstr == *str)
475 448 : return; /* no conversion happened */
476 :
477 : /* need it malloc'd not palloc'd */
478 0 : mstr = strdup(pstr);
479 0 : if (mstr == NULL)
480 0 : ereport(ERROR,
481 : (errcode(ERRCODE_OUT_OF_MEMORY),
482 : errmsg("out of memory")));
483 :
484 : /* replace old string */
485 0 : free(*str);
486 0 : *str = mstr;
487 :
488 0 : pfree(pstr);
489 : }
490 :
491 :
492 : /*
493 : * Return the POSIX lconv struct (contains number/money formatting
494 : * information) with locale information for all categories.
495 : */
496 : struct lconv *
497 2966 : PGLC_localeconv(void)
498 : {
499 : static struct lconv CurrentLocaleConv;
500 : static bool CurrentLocaleConvAllocated = false;
501 : struct lconv *extlconv;
502 : struct lconv tmp;
503 2966 : struct lconv worklconv = {0};
504 :
505 : /* Did we do it already? */
506 2966 : if (CurrentLocaleConvValid)
507 2910 : return &CurrentLocaleConv;
508 :
509 : /* Free any already-allocated storage */
510 56 : if (CurrentLocaleConvAllocated)
511 : {
512 6 : free_struct_lconv(&CurrentLocaleConv);
513 6 : CurrentLocaleConvAllocated = false;
514 : }
515 :
516 : /*
517 : * Use thread-safe method of obtaining a copy of lconv from the operating
518 : * system.
519 : */
520 56 : if (pg_localeconv_r(locale_monetary,
521 : locale_numeric,
522 : &tmp) != 0)
523 0 : elog(ERROR,
524 : "could not get lconv for LC_MONETARY = \"%s\", LC_NUMERIC = \"%s\": %m",
525 : locale_monetary, locale_numeric);
526 :
527 : /* Must copy data now so we can re-encode it. */
528 56 : extlconv = &tmp;
529 56 : worklconv.decimal_point = strdup(extlconv->decimal_point);
530 56 : worklconv.thousands_sep = strdup(extlconv->thousands_sep);
531 56 : worklconv.grouping = strdup(extlconv->grouping);
532 56 : worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
533 56 : worklconv.currency_symbol = strdup(extlconv->currency_symbol);
534 56 : worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
535 56 : worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
536 56 : worklconv.mon_grouping = strdup(extlconv->mon_grouping);
537 56 : worklconv.positive_sign = strdup(extlconv->positive_sign);
538 56 : worklconv.negative_sign = strdup(extlconv->negative_sign);
539 : /* Copy scalar fields as well */
540 56 : worklconv.int_frac_digits = extlconv->int_frac_digits;
541 56 : worklconv.frac_digits = extlconv->frac_digits;
542 56 : worklconv.p_cs_precedes = extlconv->p_cs_precedes;
543 56 : worklconv.p_sep_by_space = extlconv->p_sep_by_space;
544 56 : worklconv.n_cs_precedes = extlconv->n_cs_precedes;
545 56 : worklconv.n_sep_by_space = extlconv->n_sep_by_space;
546 56 : worklconv.p_sign_posn = extlconv->p_sign_posn;
547 56 : worklconv.n_sign_posn = extlconv->n_sign_posn;
548 :
549 : /* Free the contents of the object populated by pg_localeconv_r(). */
550 56 : pg_localeconv_free(&tmp);
551 :
552 : /* If any of the preceding strdup calls failed, complain now. */
553 56 : if (!struct_lconv_is_valid(&worklconv))
554 0 : ereport(ERROR,
555 : (errcode(ERRCODE_OUT_OF_MEMORY),
556 : errmsg("out of memory")));
557 :
558 56 : PG_TRY();
559 : {
560 : int encoding;
561 :
562 : /*
563 : * Now we must perform encoding conversion from whatever's associated
564 : * with the locales into the database encoding. If we can't identify
565 : * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
566 : * use PG_SQL_ASCII, which will result in just validating that the
567 : * strings are OK in the database encoding.
568 : */
569 56 : encoding = pg_get_encoding_from_locale(locale_numeric, true);
570 56 : if (encoding < 0)
571 0 : encoding = PG_SQL_ASCII;
572 :
573 56 : db_encoding_convert(encoding, &worklconv.decimal_point);
574 56 : db_encoding_convert(encoding, &worklconv.thousands_sep);
575 : /* grouping is not text and does not require conversion */
576 :
577 56 : encoding = pg_get_encoding_from_locale(locale_monetary, true);
578 56 : if (encoding < 0)
579 0 : encoding = PG_SQL_ASCII;
580 :
581 56 : db_encoding_convert(encoding, &worklconv.int_curr_symbol);
582 56 : db_encoding_convert(encoding, &worklconv.currency_symbol);
583 56 : db_encoding_convert(encoding, &worklconv.mon_decimal_point);
584 56 : db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
585 : /* mon_grouping is not text and does not require conversion */
586 56 : db_encoding_convert(encoding, &worklconv.positive_sign);
587 56 : db_encoding_convert(encoding, &worklconv.negative_sign);
588 : }
589 0 : PG_CATCH();
590 : {
591 0 : free_struct_lconv(&worklconv);
592 0 : PG_RE_THROW();
593 : }
594 56 : PG_END_TRY();
595 :
596 : /*
597 : * Everything is good, so save the results.
598 : */
599 56 : CurrentLocaleConv = worklconv;
600 56 : CurrentLocaleConvAllocated = true;
601 56 : CurrentLocaleConvValid = true;
602 56 : return &CurrentLocaleConv;
603 : }
604 :
605 : #ifdef WIN32
606 : /*
607 : * On Windows, strftime() returns its output in encoding CP_ACP (the default
608 : * operating system codepage for the computer), which is likely different
609 : * from SERVER_ENCODING. This is especially important in Japanese versions
610 : * of Windows which will use SJIS encoding, which we don't support as a
611 : * server encoding.
612 : *
613 : * So, instead of using strftime(), use wcsftime() to return the value in
614 : * wide characters (internally UTF16) and then convert to UTF8, which we
615 : * know how to handle directly.
616 : *
617 : * Note that this only affects the calls to strftime() in this file, which are
618 : * used to get the locale-aware strings. Other parts of the backend use
619 : * pg_strftime(), which isn't locale-aware and does not need to be replaced.
620 : */
621 : static size_t
622 : strftime_l_win32(char *dst, size_t dstlen,
623 : const char *format, const struct tm *tm, locale_t locale)
624 : {
625 : size_t len;
626 : wchar_t wformat[8]; /* formats used below need 3 chars */
627 : wchar_t wbuf[MAX_L10N_DATA];
628 :
629 : /*
630 : * Get a wchar_t version of the format string. We only actually use
631 : * plain-ASCII formats in this file, so we can say that they're UTF8.
632 : */
633 : len = MultiByteToWideChar(CP_UTF8, 0, format, -1,
634 : wformat, lengthof(wformat));
635 : if (len == 0)
636 : elog(ERROR, "could not convert format string from UTF-8: error code %lu",
637 : GetLastError());
638 :
639 : len = _wcsftime_l(wbuf, MAX_L10N_DATA, wformat, tm, locale);
640 : if (len == 0)
641 : {
642 : /*
643 : * wcsftime failed, possibly because the result would not fit in
644 : * MAX_L10N_DATA. Return 0 with the contents of dst unspecified.
645 : */
646 : return 0;
647 : }
648 :
649 : len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen - 1,
650 : NULL, NULL);
651 : if (len == 0)
652 : elog(ERROR, "could not convert string to UTF-8: error code %lu",
653 : GetLastError());
654 :
655 : dst[len] = '\0';
656 :
657 : return len;
658 : }
659 :
660 : /* redefine strftime_l() */
661 : #define strftime_l(a,b,c,d,e) strftime_l_win32(a,b,c,d,e)
662 : #endif /* WIN32 */
663 :
664 : /*
665 : * Subroutine for cache_locale_time().
666 : * Convert the given string from encoding "encoding" to the database
667 : * encoding, and store the result at *dst, replacing any previous value.
668 : */
669 : static void
670 1748 : cache_single_string(char **dst, const char *src, int encoding)
671 : {
672 : char *ptr;
673 : char *olddst;
674 :
675 : /* Convert the string to the database encoding, or validate it's OK */
676 1748 : ptr = pg_any_to_server(src, strlen(src), encoding);
677 :
678 : /* Store the string in long-lived storage, replacing any previous value */
679 1748 : olddst = *dst;
680 1748 : *dst = MemoryContextStrdup(TopMemoryContext, ptr);
681 1748 : if (olddst)
682 0 : pfree(olddst);
683 :
684 : /* Might as well clean up any palloc'd conversion result, too */
685 1748 : if (ptr != src)
686 0 : pfree(ptr);
687 1748 : }
688 :
689 : /*
690 : * Update the lc_time localization cache variables if needed.
691 : */
692 : void
693 49546 : cache_locale_time(void)
694 : {
695 : char buf[(2 * 7 + 2 * 12) * MAX_L10N_DATA];
696 : char *bufptr;
697 : time_t timenow;
698 : struct tm *timeinfo;
699 : struct tm timeinfobuf;
700 49546 : bool strftimefail = false;
701 : int encoding;
702 : int i;
703 : locale_t locale;
704 :
705 : /* did we do this already? */
706 49546 : if (CurrentLCTimeValid)
707 49500 : return;
708 :
709 46 : elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
710 :
711 46 : errno = ENOENT;
712 : #ifdef WIN32
713 : locale = _create_locale(LC_ALL, locale_time);
714 : if (locale == (locale_t) 0)
715 : _dosmaperr(GetLastError());
716 : #else
717 46 : locale = newlocale(LC_ALL_MASK, locale_time, (locale_t) 0);
718 : #endif
719 46 : if (!locale)
720 0 : report_newlocale_failure(locale_time);
721 :
722 : /* We use times close to current time as data for strftime(). */
723 46 : timenow = time(NULL);
724 46 : timeinfo = gmtime_r(&timenow, &timeinfobuf);
725 :
726 : /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
727 46 : bufptr = buf;
728 :
729 : /*
730 : * MAX_L10N_DATA is sufficient buffer space for every known locale, and
731 : * POSIX defines no strftime() errors. (Buffer space exhaustion is not an
732 : * error.) An implementation might report errors (e.g. ENOMEM) by
733 : * returning 0 (or, less plausibly, a negative value) and setting errno.
734 : * Report errno just in case the implementation did that, but clear it in
735 : * advance of the calls so we don't emit a stale, unrelated errno.
736 : */
737 46 : errno = 0;
738 :
739 : /* localized days */
740 368 : for (i = 0; i < 7; i++)
741 : {
742 322 : timeinfo->tm_wday = i;
743 322 : if (strftime_l(bufptr, MAX_L10N_DATA, "%a", timeinfo, locale) <= 0)
744 0 : strftimefail = true;
745 322 : bufptr += MAX_L10N_DATA;
746 322 : if (strftime_l(bufptr, MAX_L10N_DATA, "%A", timeinfo, locale) <= 0)
747 0 : strftimefail = true;
748 322 : bufptr += MAX_L10N_DATA;
749 : }
750 :
751 : /* localized months */
752 598 : for (i = 0; i < 12; i++)
753 : {
754 552 : timeinfo->tm_mon = i;
755 552 : timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
756 552 : if (strftime_l(bufptr, MAX_L10N_DATA, "%b", timeinfo, locale) <= 0)
757 0 : strftimefail = true;
758 552 : bufptr += MAX_L10N_DATA;
759 552 : if (strftime_l(bufptr, MAX_L10N_DATA, "%B", timeinfo, locale) <= 0)
760 0 : strftimefail = true;
761 552 : bufptr += MAX_L10N_DATA;
762 : }
763 :
764 : #ifdef WIN32
765 : _free_locale(locale);
766 : #else
767 46 : freelocale(locale);
768 : #endif
769 :
770 : /*
771 : * At this point we've done our best to clean up, and can throw errors, or
772 : * call functions that might throw errors, with a clean conscience.
773 : */
774 46 : if (strftimefail)
775 0 : elog(ERROR, "strftime_l() failed");
776 :
777 : #ifndef WIN32
778 :
779 : /*
780 : * As in PGLC_localeconv(), we must convert strftime()'s output from the
781 : * encoding implied by LC_TIME to the database encoding. If we can't
782 : * identify the LC_TIME encoding, just perform encoding validation.
783 : */
784 46 : encoding = pg_get_encoding_from_locale(locale_time, true);
785 46 : if (encoding < 0)
786 0 : encoding = PG_SQL_ASCII;
787 :
788 : #else
789 :
790 : /*
791 : * On Windows, strftime_win32() always returns UTF8 data, so convert from
792 : * that if necessary.
793 : */
794 : encoding = PG_UTF8;
795 :
796 : #endif /* WIN32 */
797 :
798 46 : bufptr = buf;
799 :
800 : /* localized days */
801 368 : for (i = 0; i < 7; i++)
802 : {
803 322 : cache_single_string(&localized_abbrev_days[i], bufptr, encoding);
804 322 : bufptr += MAX_L10N_DATA;
805 322 : cache_single_string(&localized_full_days[i], bufptr, encoding);
806 322 : bufptr += MAX_L10N_DATA;
807 : }
808 46 : localized_abbrev_days[7] = NULL;
809 46 : localized_full_days[7] = NULL;
810 :
811 : /* localized months */
812 598 : for (i = 0; i < 12; i++)
813 : {
814 552 : cache_single_string(&localized_abbrev_months[i], bufptr, encoding);
815 552 : bufptr += MAX_L10N_DATA;
816 552 : cache_single_string(&localized_full_months[i], bufptr, encoding);
817 552 : bufptr += MAX_L10N_DATA;
818 : }
819 46 : localized_abbrev_months[12] = NULL;
820 46 : localized_full_months[12] = NULL;
821 :
822 46 : CurrentLCTimeValid = true;
823 : }
824 :
825 :
826 : #if defined(WIN32) && defined(LC_MESSAGES)
827 : /*
828 : * Convert a Windows setlocale() argument to a Unix-style one.
829 : *
830 : * Regardless of platform, we install message catalogs under a Unix-style
831 : * LL[_CC][.ENCODING][@VARIANT] naming convention. Only LC_MESSAGES settings
832 : * following that style will elicit localized interface strings.
833 : *
834 : * Before Visual Studio 2012 (msvcr110.dll), Windows setlocale() accepted "C"
835 : * (but not "c") and strings of the form <Language>[_<Country>][.<CodePage>],
836 : * case-insensitive. setlocale() returns the fully-qualified form; for
837 : * example, setlocale("thaI") returns "Thai_Thailand.874". Internally,
838 : * setlocale() and _create_locale() select a "locale identifier"[1] and store
839 : * it in an undocumented _locale_t field. From that LCID, we can retrieve the
840 : * ISO 639 language and the ISO 3166 country. Character encoding does not
841 : * matter, because the server and client encodings govern that.
842 : *
843 : * Windows Vista introduced the "locale name" concept[2], closely following
844 : * RFC 4646. Locale identifiers are now deprecated. Starting with Visual
845 : * Studio 2012, setlocale() accepts locale names in addition to the strings it
846 : * accepted historically. It does not standardize them; setlocale("Th-tH")
847 : * returns "Th-tH". setlocale(category, "") still returns a traditional
848 : * string. Furthermore, msvcr110.dll changed the undocumented _locale_t
849 : * content to carry locale names instead of locale identifiers.
850 : *
851 : * Visual Studio 2015 should still be able to do the same as Visual Studio
852 : * 2012, but the declaration of locale_name is missing in _locale_t, causing
853 : * this code compilation to fail, hence this falls back instead on to
854 : * enumerating all system locales by using EnumSystemLocalesEx to find the
855 : * required locale name. If the input argument is in Unix-style then we can
856 : * get ISO Locale name directly by using GetLocaleInfoEx() with LCType as
857 : * LOCALE_SNAME.
858 : *
859 : * This function returns a pointer to a static buffer bearing the converted
860 : * name or NULL if conversion fails.
861 : *
862 : * [1] https://docs.microsoft.com/en-us/windows/win32/intl/locale-identifiers
863 : * [2] https://docs.microsoft.com/en-us/windows/win32/intl/locale-names
864 : */
865 :
866 : /*
867 : * Callback function for EnumSystemLocalesEx() in get_iso_localename().
868 : *
869 : * This function enumerates all system locales, searching for one that matches
870 : * an input with the format: <Language>[_<Country>], e.g.
871 : * English[_United States]
872 : *
873 : * The input is a three wchar_t array as an LPARAM. The first element is the
874 : * locale_name we want to match, the second element is an allocated buffer
875 : * where the Unix-style locale is copied if a match is found, and the third
876 : * element is the search status, 1 if a match was found, 0 otherwise.
877 : */
878 : static BOOL CALLBACK
879 : search_locale_enum(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
880 : {
881 : wchar_t test_locale[LOCALE_NAME_MAX_LENGTH];
882 : wchar_t **argv;
883 :
884 : (void) (dwFlags);
885 :
886 : argv = (wchar_t **) lparam;
887 : *argv[2] = (wchar_t) 0;
888 :
889 : memset(test_locale, 0, sizeof(test_locale));
890 :
891 : /* Get the name of the <Language> in English */
892 : if (GetLocaleInfoEx(pStr, LOCALE_SENGLISHLANGUAGENAME,
893 : test_locale, LOCALE_NAME_MAX_LENGTH))
894 : {
895 : /*
896 : * If the enumerated locale does not have a hyphen ("en") OR the
897 : * locale_name input does not have an underscore ("English"), we only
898 : * need to compare the <Language> tags.
899 : */
900 : if (wcsrchr(pStr, '-') == NULL || wcsrchr(argv[0], '_') == NULL)
901 : {
902 : if (_wcsicmp(argv[0], test_locale) == 0)
903 : {
904 : wcscpy(argv[1], pStr);
905 : *argv[2] = (wchar_t) 1;
906 : return FALSE;
907 : }
908 : }
909 :
910 : /*
911 : * We have to compare a full <Language>_<Country> tag, so we append
912 : * the underscore and name of the country/region in English, e.g.
913 : * "English_United States".
914 : */
915 : else
916 : {
917 : size_t len;
918 :
919 : wcscat(test_locale, L"_");
920 : len = wcslen(test_locale);
921 : if (GetLocaleInfoEx(pStr, LOCALE_SENGLISHCOUNTRYNAME,
922 : test_locale + len,
923 : LOCALE_NAME_MAX_LENGTH - len))
924 : {
925 : if (_wcsicmp(argv[0], test_locale) == 0)
926 : {
927 : wcscpy(argv[1], pStr);
928 : *argv[2] = (wchar_t) 1;
929 : return FALSE;
930 : }
931 : }
932 : }
933 : }
934 :
935 : return TRUE;
936 : }
937 :
938 : /*
939 : * This function converts a Windows locale name to an ISO formatted version
940 : * for Visual Studio 2015 or greater.
941 : *
942 : * Returns NULL, if no valid conversion was found.
943 : */
944 : static char *
945 : get_iso_localename(const char *winlocname)
946 : {
947 : wchar_t wc_locale_name[LOCALE_NAME_MAX_LENGTH];
948 : wchar_t buffer[LOCALE_NAME_MAX_LENGTH];
949 : static char iso_lc_messages[LOCALE_NAME_MAX_LENGTH];
950 : char *period;
951 : int len;
952 : int ret_val;
953 :
954 : /*
955 : * Valid locales have the following syntax:
956 : * <Language>[_<Country>[.<CodePage>]]
957 : *
958 : * GetLocaleInfoEx can only take locale name without code-page and for the
959 : * purpose of this API the code-page doesn't matter.
960 : */
961 : period = strchr(winlocname, '.');
962 : if (period != NULL)
963 : len = period - winlocname;
964 : else
965 : len = pg_mbstrlen(winlocname);
966 :
967 : memset(wc_locale_name, 0, sizeof(wc_locale_name));
968 : memset(buffer, 0, sizeof(buffer));
969 : MultiByteToWideChar(CP_ACP, 0, winlocname, len, wc_locale_name,
970 : LOCALE_NAME_MAX_LENGTH);
971 :
972 : /*
973 : * If the lc_messages is already a Unix-style string, we have a direct
974 : * match with LOCALE_SNAME, e.g. en-US, en_US.
975 : */
976 : ret_val = GetLocaleInfoEx(wc_locale_name, LOCALE_SNAME, (LPWSTR) &buffer,
977 : LOCALE_NAME_MAX_LENGTH);
978 : if (!ret_val)
979 : {
980 : /*
981 : * Search for a locale in the system that matches language and country
982 : * name.
983 : */
984 : wchar_t *argv[3];
985 :
986 : argv[0] = wc_locale_name;
987 : argv[1] = buffer;
988 : argv[2] = (wchar_t *) &ret_val;
989 : EnumSystemLocalesEx(search_locale_enum, LOCALE_WINDOWS, (LPARAM) argv,
990 : NULL);
991 : }
992 :
993 : if (ret_val)
994 : {
995 : size_t rc;
996 : char *hyphen;
997 :
998 : /* Locale names use only ASCII, any conversion locale suffices. */
999 : rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), NULL);
1000 : if (rc == -1 || rc == sizeof(iso_lc_messages))
1001 : return NULL;
1002 :
1003 : /*
1004 : * Since the message catalogs sit on a case-insensitive filesystem, we
1005 : * need not standardize letter case here. So long as we do not ship
1006 : * message catalogs for which it would matter, we also need not
1007 : * translate the script/variant portion, e.g. uz-Cyrl-UZ to
1008 : * uz_UZ@cyrillic. Simply replace the hyphen with an underscore.
1009 : */
1010 : hyphen = strchr(iso_lc_messages, '-');
1011 : if (hyphen)
1012 : *hyphen = '_';
1013 : return iso_lc_messages;
1014 : }
1015 :
1016 : return NULL;
1017 : }
1018 :
1019 : static char *
1020 : IsoLocaleName(const char *winlocname)
1021 : {
1022 : static char iso_lc_messages[LOCALE_NAME_MAX_LENGTH];
1023 :
1024 : if (pg_strcasecmp("c", winlocname) == 0 ||
1025 : pg_strcasecmp("posix", winlocname) == 0)
1026 : {
1027 : strcpy(iso_lc_messages, "C");
1028 : return iso_lc_messages;
1029 : }
1030 : else
1031 : return get_iso_localename(winlocname);
1032 : }
1033 :
1034 : #endif /* WIN32 && LC_MESSAGES */
1035 :
1036 : /*
1037 : * Create a new pg_locale_t struct for the given collation oid.
1038 : */
1039 : static pg_locale_t
1040 4290 : create_pg_locale(Oid collid, MemoryContext context)
1041 : {
1042 : HeapTuple tp;
1043 : Form_pg_collation collform;
1044 : pg_locale_t result;
1045 : Datum datum;
1046 : bool isnull;
1047 :
1048 4290 : tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1049 4290 : if (!HeapTupleIsValid(tp))
1050 0 : elog(ERROR, "cache lookup failed for collation %u", collid);
1051 4290 : collform = (Form_pg_collation) GETSTRUCT(tp);
1052 :
1053 4290 : if (collform->collprovider == COLLPROVIDER_BUILTIN)
1054 52 : result = create_pg_locale_builtin(collid, context);
1055 4238 : else if (collform->collprovider == COLLPROVIDER_ICU)
1056 184 : result = create_pg_locale_icu(collid, context);
1057 4054 : else if (collform->collprovider == COLLPROVIDER_LIBC)
1058 4054 : result = create_pg_locale_libc(collid, context);
1059 : else
1060 : /* shouldn't happen */
1061 0 : PGLOCALE_SUPPORT_ERROR(collform->collprovider);
1062 :
1063 4284 : result->is_default = false;
1064 :
1065 : Assert((result->collate_is_c && result->collate == NULL) ||
1066 : (!result->collate_is_c && result->collate != NULL));
1067 :
1068 : Assert((result->ctype_is_c && result->ctype == NULL) ||
1069 : (!result->ctype_is_c && result->ctype != NULL));
1070 :
1071 4284 : datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collversion,
1072 : &isnull);
1073 4284 : if (!isnull)
1074 : {
1075 : char *actual_versionstr;
1076 : char *collversionstr;
1077 :
1078 230 : collversionstr = TextDatumGetCString(datum);
1079 :
1080 230 : if (collform->collprovider == COLLPROVIDER_LIBC)
1081 0 : datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_collcollate);
1082 : else
1083 230 : datum = SysCacheGetAttrNotNull(COLLOID, tp, Anum_pg_collation_colllocale);
1084 :
1085 230 : actual_versionstr = get_collation_actual_version(collform->collprovider,
1086 230 : TextDatumGetCString(datum));
1087 230 : if (!actual_versionstr)
1088 : {
1089 : /*
1090 : * This could happen when specifying a version in CREATE COLLATION
1091 : * but the provider does not support versioning, or manually
1092 : * creating a mess in the catalogs.
1093 : */
1094 0 : ereport(ERROR,
1095 : (errmsg("collation \"%s\" has no actual version, but a version was recorded",
1096 : NameStr(collform->collname))));
1097 : }
1098 :
1099 230 : if (strcmp(actual_versionstr, collversionstr) != 0)
1100 0 : ereport(WARNING,
1101 : (errmsg("collation \"%s\" has version mismatch",
1102 : NameStr(collform->collname)),
1103 : errdetail("The collation in the database was created using version %s, "
1104 : "but the operating system provides version %s.",
1105 : collversionstr, actual_versionstr),
1106 : errhint("Rebuild all objects affected by this collation and run "
1107 : "ALTER COLLATION %s REFRESH VERSION, "
1108 : "or build PostgreSQL with the right library version.",
1109 : quote_qualified_identifier(get_namespace_name(collform->collnamespace),
1110 : NameStr(collform->collname)))));
1111 : }
1112 :
1113 4284 : ReleaseSysCache(tp);
1114 :
1115 4284 : return result;
1116 : }
1117 :
1118 : /*
1119 : * Initialize default_locale with database locale settings.
1120 : */
1121 : void
1122 32670 : init_database_collation(void)
1123 : {
1124 : HeapTuple tup;
1125 : Form_pg_database dbform;
1126 : pg_locale_t result;
1127 :
1128 : Assert(default_locale == NULL);
1129 :
1130 : /* Fetch our pg_database row normally, via syscache */
1131 32670 : tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1132 32670 : if (!HeapTupleIsValid(tup))
1133 0 : elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1134 32670 : dbform = (Form_pg_database) GETSTRUCT(tup);
1135 :
1136 32670 : if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1137 1732 : result = create_pg_locale_builtin(DEFAULT_COLLATION_OID,
1138 : TopMemoryContext);
1139 30938 : else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1140 26 : result = create_pg_locale_icu(DEFAULT_COLLATION_OID,
1141 : TopMemoryContext);
1142 30912 : else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
1143 30912 : result = create_pg_locale_libc(DEFAULT_COLLATION_OID,
1144 : TopMemoryContext);
1145 : else
1146 : /* shouldn't happen */
1147 0 : PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
1148 :
1149 32666 : result->is_default = true;
1150 :
1151 : Assert((result->collate_is_c && result->collate == NULL) ||
1152 : (!result->collate_is_c && result->collate != NULL));
1153 :
1154 : Assert((result->ctype_is_c && result->ctype == NULL) ||
1155 : (!result->ctype_is_c && result->ctype != NULL));
1156 :
1157 32666 : ReleaseSysCache(tup);
1158 :
1159 32666 : default_locale = result;
1160 32666 : }
1161 :
1162 : /*
1163 : * Get database default locale.
1164 : */
1165 : pg_locale_t
1166 3091498 : pg_database_locale(void)
1167 : {
1168 3091498 : return pg_newlocale_from_collation(DEFAULT_COLLATION_OID);
1169 : }
1170 :
1171 : /*
1172 : * Create a pg_locale_t from a collation OID. Results are cached for the
1173 : * lifetime of the backend. Thus, do not free the result with freelocale().
1174 : *
1175 : * For simplicity, we always generate COLLATE + CTYPE even though we
1176 : * might only need one of them. Since this is called only once per session,
1177 : * it shouldn't cost much.
1178 : */
1179 : pg_locale_t
1180 31649188 : pg_newlocale_from_collation(Oid collid)
1181 : {
1182 : collation_cache_entry *cache_entry;
1183 : bool found;
1184 :
1185 31649188 : if (collid == DEFAULT_COLLATION_OID)
1186 27832558 : return default_locale;
1187 :
1188 3816630 : if (!OidIsValid(collid))
1189 0 : elog(ERROR, "cache lookup failed for collation %u", collid);
1190 :
1191 3816630 : AssertCouldGetRelation();
1192 :
1193 3816630 : if (last_collation_cache_oid == collid)
1194 3810394 : return last_collation_cache_locale;
1195 :
1196 6236 : if (CollationCache == NULL)
1197 : {
1198 3982 : CollationCacheContext = AllocSetContextCreate(TopMemoryContext,
1199 : "collation cache",
1200 : ALLOCSET_DEFAULT_SIZES);
1201 3982 : CollationCache = collation_cache_create(CollationCacheContext,
1202 : 16, NULL);
1203 : }
1204 :
1205 6236 : cache_entry = collation_cache_insert(CollationCache, collid, &found);
1206 6236 : if (!found)
1207 : {
1208 : /*
1209 : * Make sure cache entry is marked invalid, in case we fail before
1210 : * setting things.
1211 : */
1212 4290 : cache_entry->locale = 0;
1213 : }
1214 :
1215 6236 : if (cache_entry->locale == 0)
1216 : {
1217 4290 : cache_entry->locale = create_pg_locale(collid, CollationCacheContext);
1218 : }
1219 :
1220 6230 : last_collation_cache_oid = collid;
1221 6230 : last_collation_cache_locale = cache_entry->locale;
1222 :
1223 6230 : return cache_entry->locale;
1224 : }
1225 :
1226 : /*
1227 : * Get provider-specific collation version string for the given collation from
1228 : * the operating system/library.
1229 : */
1230 : char *
1231 109870 : get_collation_actual_version(char collprovider, const char *collcollate)
1232 : {
1233 109870 : char *collversion = NULL;
1234 :
1235 109870 : if (collprovider == COLLPROVIDER_BUILTIN)
1236 1852 : collversion = get_collation_actual_version_builtin(collcollate);
1237 : #ifdef USE_ICU
1238 108018 : else if (collprovider == COLLPROVIDER_ICU)
1239 77840 : collversion = get_collation_actual_version_icu(collcollate);
1240 : #endif
1241 30178 : else if (collprovider == COLLPROVIDER_LIBC)
1242 30178 : collversion = get_collation_actual_version_libc(collcollate);
1243 :
1244 109870 : return collversion;
1245 : }
1246 :
1247 : size_t
1248 437538 : pg_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1249 : pg_locale_t locale)
1250 : {
1251 437538 : return locale->ctype->strlower(dst, dstsize, src, srclen, locale);
1252 : }
1253 :
1254 : size_t
1255 232 : pg_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1256 : pg_locale_t locale)
1257 : {
1258 232 : return locale->ctype->strtitle(dst, dstsize, src, srclen, locale);
1259 : }
1260 :
1261 : size_t
1262 1036168 : pg_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1263 : pg_locale_t locale)
1264 : {
1265 1036168 : return locale->ctype->strupper(dst, dstsize, src, srclen, locale);
1266 : }
1267 :
1268 : size_t
1269 24 : pg_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen,
1270 : pg_locale_t locale)
1271 : {
1272 24 : if (locale->ctype->strfold)
1273 24 : return locale->ctype->strfold(dst, dstsize, src, srclen, locale);
1274 : else
1275 0 : return locale->ctype->strlower(dst, dstsize, src, srclen, locale);
1276 : }
1277 :
1278 : /*
1279 : * pg_strcoll
1280 : *
1281 : * Like pg_strncoll for NUL-terminated input strings.
1282 : */
1283 : int
1284 25766044 : pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale)
1285 : {
1286 25766044 : return locale->collate->strncoll(arg1, -1, arg2, -1, locale);
1287 : }
1288 :
1289 : /*
1290 : * pg_strncoll
1291 : *
1292 : * Call ucol_strcollUTF8(), ucol_strcoll(), strcoll_l() or wcscoll_l() as
1293 : * appropriate for the given locale, platform, and database encoding. If the
1294 : * locale is not specified, use the database collation.
1295 : *
1296 : * The input strings must be encoded in the database encoding. If an input
1297 : * string is NUL-terminated, its length may be specified as -1.
1298 : *
1299 : * The caller is responsible for breaking ties if the collation is
1300 : * deterministic; this maintains consistency with pg_strnxfrm(), which cannot
1301 : * easily account for deterministic collations.
1302 : */
1303 : int
1304 4448730 : pg_strncoll(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2,
1305 : pg_locale_t locale)
1306 : {
1307 4448730 : return locale->collate->strncoll(arg1, len1, arg2, len2, locale);
1308 : }
1309 :
1310 : /*
1311 : * Return true if the collation provider supports pg_strxfrm() and
1312 : * pg_strnxfrm(); otherwise false.
1313 : *
1314 : *
1315 : * No similar problem is known for the ICU provider.
1316 : */
1317 : bool
1318 45648 : pg_strxfrm_enabled(pg_locale_t locale)
1319 : {
1320 : /*
1321 : * locale->collate->strnxfrm is still a required method, even if it may
1322 : * have the wrong behavior, because the planner uses it for estimates in
1323 : * some cases.
1324 : */
1325 45648 : return locale->collate->strxfrm_is_safe;
1326 : }
1327 :
1328 : /*
1329 : * pg_strxfrm
1330 : *
1331 : * Like pg_strnxfrm for a NUL-terminated input string.
1332 : */
1333 : size_t
1334 144 : pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
1335 : {
1336 144 : return locale->collate->strnxfrm(dest, destsize, src, -1, locale);
1337 : }
1338 :
1339 : /*
1340 : * pg_strnxfrm
1341 : *
1342 : * Transforms 'src' to a nul-terminated string stored in 'dest' such that
1343 : * ordinary strcmp() on transformed strings is equivalent to pg_strcoll() on
1344 : * untransformed strings.
1345 : *
1346 : * The input string must be encoded in the database encoding. If the input
1347 : * string is NUL-terminated, its length may be specified as -1. If 'destsize'
1348 : * is zero, 'dest' may be NULL.
1349 : *
1350 : * Not all providers support pg_strnxfrm() safely. The caller should check
1351 : * pg_strxfrm_enabled() first, otherwise this function may return wrong
1352 : * results or an error.
1353 : *
1354 : * Returns the number of bytes needed (or more) to store the transformed
1355 : * string, excluding the terminating nul byte. If the value returned is
1356 : * 'destsize' or greater, the resulting contents of 'dest' are undefined.
1357 : */
1358 : size_t
1359 5748 : pg_strnxfrm(char *dest, size_t destsize, const char *src, ssize_t srclen,
1360 : pg_locale_t locale)
1361 : {
1362 5748 : return locale->collate->strnxfrm(dest, destsize, src, srclen, locale);
1363 : }
1364 :
1365 : /*
1366 : * Return true if the collation provider supports pg_strxfrm_prefix() and
1367 : * pg_strnxfrm_prefix(); otherwise false.
1368 : */
1369 : bool
1370 1668 : pg_strxfrm_prefix_enabled(pg_locale_t locale)
1371 : {
1372 1668 : return (locale->collate->strnxfrm_prefix != NULL);
1373 : }
1374 :
1375 : /*
1376 : * pg_strxfrm_prefix
1377 : *
1378 : * Like pg_strnxfrm_prefix for a NUL-terminated input string.
1379 : */
1380 : size_t
1381 1668 : pg_strxfrm_prefix(char *dest, const char *src, size_t destsize,
1382 : pg_locale_t locale)
1383 : {
1384 1668 : return locale->collate->strnxfrm_prefix(dest, destsize, src, -1, locale);
1385 : }
1386 :
1387 : /*
1388 : * pg_strnxfrm_prefix
1389 : *
1390 : * Transforms 'src' to a byte sequence stored in 'dest' such that ordinary
1391 : * memcmp() on the byte sequence is equivalent to pg_strncoll() on
1392 : * untransformed strings. The result is not nul-terminated.
1393 : *
1394 : * The input string must be encoded in the database encoding. If the input
1395 : * string is NUL-terminated, its length may be specified as -1.
1396 : *
1397 : * Not all providers support pg_strnxfrm_prefix() safely. The caller should
1398 : * check pg_strxfrm_prefix_enabled() first, otherwise this function may return
1399 : * wrong results or an error.
1400 : *
1401 : * If destsize is not large enough to hold the resulting byte sequence, stores
1402 : * only the first destsize bytes in 'dest'. Returns the number of bytes
1403 : * actually copied to 'dest'.
1404 : */
1405 : size_t
1406 0 : pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src,
1407 : ssize_t srclen, pg_locale_t locale)
1408 : {
1409 0 : return locale->collate->strnxfrm_prefix(dest, destsize, src, srclen, locale);
1410 : }
1411 :
1412 : bool
1413 37132 : pg_iswdigit(pg_wchar wc, pg_locale_t locale)
1414 : {
1415 37132 : if (locale->ctype == NULL)
1416 0 : return (wc <= (pg_wchar) 127 &&
1417 0 : (pg_char_properties[wc] & PG_ISDIGIT));
1418 : else
1419 37132 : return locale->ctype->wc_isdigit(wc, locale);
1420 : }
1421 :
1422 : bool
1423 104140 : pg_iswalpha(pg_wchar wc, pg_locale_t locale)
1424 : {
1425 104140 : if (locale->ctype == NULL)
1426 0 : return (wc <= (pg_wchar) 127 &&
1427 0 : (pg_char_properties[wc] & PG_ISALPHA));
1428 : else
1429 104140 : return locale->ctype->wc_isalpha(wc, locale);
1430 : }
1431 :
1432 : bool
1433 2804904 : pg_iswalnum(pg_wchar wc, pg_locale_t locale)
1434 : {
1435 2804904 : if (locale->ctype == NULL)
1436 0 : return (wc <= (pg_wchar) 127 &&
1437 0 : (pg_char_properties[wc] & PG_ISALNUM));
1438 : else
1439 2804904 : return locale->ctype->wc_isalnum(wc, locale);
1440 : }
1441 :
1442 : bool
1443 0 : pg_iswupper(pg_wchar wc, pg_locale_t locale)
1444 : {
1445 0 : if (locale->ctype == NULL)
1446 0 : return (wc <= (pg_wchar) 127 &&
1447 0 : (pg_char_properties[wc] & PG_ISUPPER));
1448 : else
1449 0 : return locale->ctype->wc_isupper(wc, locale);
1450 : }
1451 :
1452 : bool
1453 0 : pg_iswlower(pg_wchar wc, pg_locale_t locale)
1454 : {
1455 0 : if (locale->ctype == NULL)
1456 0 : return (wc <= (pg_wchar) 127 &&
1457 0 : (pg_char_properties[wc] & PG_ISLOWER));
1458 : else
1459 0 : return locale->ctype->wc_islower(wc, locale);
1460 : }
1461 :
1462 : bool
1463 0 : pg_iswgraph(pg_wchar wc, pg_locale_t locale)
1464 : {
1465 0 : if (locale->ctype == NULL)
1466 0 : return (wc <= (pg_wchar) 127 &&
1467 0 : (pg_char_properties[wc] & PG_ISGRAPH));
1468 : else
1469 0 : return locale->ctype->wc_isgraph(wc, locale);
1470 : }
1471 :
1472 : bool
1473 0 : pg_iswprint(pg_wchar wc, pg_locale_t locale)
1474 : {
1475 0 : if (locale->ctype == NULL)
1476 0 : return (wc <= (pg_wchar) 127 &&
1477 0 : (pg_char_properties[wc] & PG_ISPRINT));
1478 : else
1479 0 : return locale->ctype->wc_isprint(wc, locale);
1480 : }
1481 :
1482 : bool
1483 0 : pg_iswpunct(pg_wchar wc, pg_locale_t locale)
1484 : {
1485 0 : if (locale->ctype == NULL)
1486 0 : return (wc <= (pg_wchar) 127 &&
1487 0 : (pg_char_properties[wc] & PG_ISPUNCT));
1488 : else
1489 0 : return locale->ctype->wc_ispunct(wc, locale);
1490 : }
1491 :
1492 : bool
1493 678 : pg_iswspace(pg_wchar wc, pg_locale_t locale)
1494 : {
1495 678 : if (locale->ctype == NULL)
1496 0 : return (wc <= (pg_wchar) 127 &&
1497 0 : (pg_char_properties[wc] & PG_ISSPACE));
1498 : else
1499 678 : return locale->ctype->wc_isspace(wc, locale);
1500 : }
1501 :
1502 : bool
1503 18 : pg_iswxdigit(pg_wchar wc, pg_locale_t locale)
1504 : {
1505 18 : if (locale->ctype == NULL)
1506 0 : return (wc <= (pg_wchar) 127 &&
1507 0 : ((pg_char_properties[wc] & PG_ISDIGIT) ||
1508 0 : ((wc >= 'A' && wc <= 'F') ||
1509 0 : (wc >= 'a' && wc <= 'f'))));
1510 : else
1511 18 : return locale->ctype->wc_isxdigit(wc, locale);
1512 : }
1513 :
1514 : pg_wchar
1515 0 : pg_towupper(pg_wchar wc, pg_locale_t locale)
1516 : {
1517 0 : if (locale->ctype == NULL)
1518 : {
1519 0 : if (wc <= (pg_wchar) 127)
1520 0 : return pg_ascii_toupper((unsigned char) wc);
1521 0 : return wc;
1522 : }
1523 : else
1524 0 : return locale->ctype->wc_toupper(wc, locale);
1525 : }
1526 :
1527 : pg_wchar
1528 0 : pg_towlower(pg_wchar wc, pg_locale_t locale)
1529 : {
1530 0 : if (locale->ctype == NULL)
1531 : {
1532 0 : if (wc <= (pg_wchar) 127)
1533 0 : return pg_ascii_tolower((unsigned char) wc);
1534 0 : return wc;
1535 : }
1536 : else
1537 0 : return locale->ctype->wc_tolower(wc, locale);
1538 : }
1539 :
1540 : /*
1541 : * char_is_cased()
1542 : *
1543 : * Fuzzy test of whether the given char is case-varying or not. The argument
1544 : * is a single byte, so in a multibyte encoding, just assume any non-ASCII
1545 : * char is case-varying.
1546 : */
1547 : bool
1548 126 : char_is_cased(char ch, pg_locale_t locale)
1549 : {
1550 126 : return locale->ctype->char_is_cased(ch, locale);
1551 : }
1552 :
1553 : /*
1554 : * char_tolower_enabled()
1555 : *
1556 : * Does the provider support char_tolower()?
1557 : */
1558 : bool
1559 68496 : char_tolower_enabled(pg_locale_t locale)
1560 : {
1561 68496 : return (locale->ctype->char_tolower != NULL);
1562 : }
1563 :
1564 : /*
1565 : * char_tolower()
1566 : *
1567 : * Convert char (single-byte encoding) to lowercase.
1568 : */
1569 : char
1570 0 : char_tolower(unsigned char ch, pg_locale_t locale)
1571 : {
1572 0 : return locale->ctype->char_tolower(ch, locale);
1573 : }
1574 :
1575 : /*
1576 : * Return required encoding ID for the given locale, or -1 if any encoding is
1577 : * valid for the locale.
1578 : */
1579 : int
1580 1908 : builtin_locale_encoding(const char *locale)
1581 : {
1582 1908 : if (strcmp(locale, "C") == 0)
1583 76 : return -1;
1584 1832 : else if (strcmp(locale, "C.UTF-8") == 0)
1585 1802 : return PG_UTF8;
1586 30 : else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1587 30 : return PG_UTF8;
1588 :
1589 :
1590 0 : ereport(ERROR,
1591 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1592 : errmsg("invalid locale name \"%s\" for builtin provider",
1593 : locale)));
1594 :
1595 : return 0; /* keep compiler quiet */
1596 : }
1597 :
1598 :
1599 : /*
1600 : * Validate the locale and encoding combination, and return the canonical form
1601 : * of the locale name.
1602 : */
1603 : const char *
1604 1892 : builtin_validate_locale(int encoding, const char *locale)
1605 : {
1606 1892 : const char *canonical_name = NULL;
1607 : int required_encoding;
1608 :
1609 1892 : if (strcmp(locale, "C") == 0)
1610 64 : canonical_name = "C";
1611 1828 : else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1612 1788 : canonical_name = "C.UTF-8";
1613 40 : else if (strcmp(locale, "PG_UNICODE_FAST") == 0)
1614 22 : canonical_name = "PG_UNICODE_FAST";
1615 :
1616 1892 : if (!canonical_name)
1617 18 : ereport(ERROR,
1618 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1619 : errmsg("invalid locale name \"%s\" for builtin provider",
1620 : locale)));
1621 :
1622 1874 : required_encoding = builtin_locale_encoding(canonical_name);
1623 1874 : if (required_encoding >= 0 && encoding != required_encoding)
1624 2 : ereport(ERROR,
1625 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1626 : errmsg("encoding \"%s\" does not match locale \"%s\"",
1627 : pg_encoding_to_char(encoding), locale)));
1628 :
1629 1872 : return canonical_name;
1630 : }
1631 :
1632 :
1633 :
1634 : /*
1635 : * Return the BCP47 language tag representation of the requested locale.
1636 : *
1637 : * This function should be called before passing the string to ucol_open(),
1638 : * because conversion to a language tag also performs "level 2
1639 : * canonicalization". In addition to producing a consistent format, level 2
1640 : * canonicalization is able to more accurately interpret different input
1641 : * locale string formats, such as POSIX and .NET IDs.
1642 : */
1643 : char *
1644 77534 : icu_language_tag(const char *loc_str, int elevel)
1645 : {
1646 : #ifdef USE_ICU
1647 : UErrorCode status;
1648 : char *langtag;
1649 77534 : size_t buflen = 32; /* arbitrary starting buffer size */
1650 77534 : const bool strict = true;
1651 :
1652 : /*
1653 : * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
1654 : * RFC5646 section 4.4). Additionally, in older ICU versions,
1655 : * uloc_toLanguageTag() doesn't always return the ultimate length on the
1656 : * first call, necessitating a loop.
1657 : */
1658 77534 : langtag = palloc(buflen);
1659 : while (true)
1660 : {
1661 77534 : status = U_ZERO_ERROR;
1662 77534 : uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
1663 :
1664 : /* try again if the buffer is not large enough */
1665 77534 : if ((status == U_BUFFER_OVERFLOW_ERROR ||
1666 77534 : status == U_STRING_NOT_TERMINATED_WARNING) &&
1667 : buflen < MaxAllocSize)
1668 : {
1669 0 : buflen = Min(buflen * 2, MaxAllocSize);
1670 0 : langtag = repalloc(langtag, buflen);
1671 0 : continue;
1672 : }
1673 :
1674 77534 : break;
1675 : }
1676 :
1677 77534 : if (U_FAILURE(status))
1678 : {
1679 18 : pfree(langtag);
1680 :
1681 18 : if (elevel > 0)
1682 14 : ereport(elevel,
1683 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1684 : errmsg("could not convert locale name \"%s\" to language tag: %s",
1685 : loc_str, u_errorName(status))));
1686 12 : return NULL;
1687 : }
1688 :
1689 77516 : return langtag;
1690 : #else /* not USE_ICU */
1691 : ereport(ERROR,
1692 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1693 : errmsg("ICU is not supported in this build")));
1694 : return NULL; /* keep compiler quiet */
1695 : #endif /* not USE_ICU */
1696 : }
1697 :
1698 : /*
1699 : * Perform best-effort check that the locale is a valid one.
1700 : */
1701 : void
1702 166 : icu_validate_locale(const char *loc_str)
1703 : {
1704 : #ifdef USE_ICU
1705 : UCollator *collator;
1706 : UErrorCode status;
1707 : char lang[ULOC_LANG_CAPACITY];
1708 166 : bool found = false;
1709 166 : int elevel = icu_validation_level;
1710 :
1711 : /* no validation */
1712 166 : if (elevel < 0)
1713 12 : return;
1714 :
1715 : /* downgrade to WARNING during pg_upgrade */
1716 154 : if (IsBinaryUpgrade && elevel > WARNING)
1717 0 : elevel = WARNING;
1718 :
1719 : /* validate that we can extract the language */
1720 154 : status = U_ZERO_ERROR;
1721 154 : uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
1722 154 : if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1723 : {
1724 0 : ereport(elevel,
1725 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1726 : errmsg("could not get language from ICU locale \"%s\": %s",
1727 : loc_str, u_errorName(status)),
1728 : errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1729 : "icu_validation_level", "disabled")));
1730 0 : return;
1731 : }
1732 :
1733 : /* check for special language name */
1734 154 : if (strcmp(lang, "") == 0 ||
1735 46 : strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1736 108 : found = true;
1737 :
1738 : /* search for matching language within ICU */
1739 15704 : for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
1740 : {
1741 15550 : const char *otherloc = uloc_getAvailable(i);
1742 : char otherlang[ULOC_LANG_CAPACITY];
1743 :
1744 15550 : status = U_ZERO_ERROR;
1745 15550 : uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
1746 15550 : if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1747 0 : continue;
1748 :
1749 15550 : if (strcmp(lang, otherlang) == 0)
1750 32 : found = true;
1751 : }
1752 :
1753 154 : if (!found)
1754 14 : ereport(elevel,
1755 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1756 : errmsg("ICU locale \"%s\" has unknown language \"%s\"",
1757 : loc_str, lang),
1758 : errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1759 : "icu_validation_level", "disabled")));
1760 :
1761 : /* check that it can be opened */
1762 148 : collator = pg_ucol_open(loc_str);
1763 140 : ucol_close(collator);
1764 : #else /* not USE_ICU */
1765 : /* could get here if a collation was created by a build with ICU */
1766 : ereport(ERROR,
1767 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1768 : errmsg("ICU is not supported in this build")));
1769 : #endif /* not USE_ICU */
1770 : }
|