PostgreSQL Source Code  git master
pg_locale.c
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------
2  *
3  * PostgreSQL locale utilities
4  *
5  * Portions Copyright (c) 2002-2021, 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 also
22  * settable at run-time. However, we don't actually set those locale
23  * categories permanently. This would have bizarre effects like no
24  * longer accepting standard floating-point literals in some locales.
25  * Instead, we only set these locale categories briefly when needed,
26  * cache the required information obtained from localeconv() or
27  * strftime(), and then set the locale categories back to "C".
28  * The cached information is only used by the formatting functions
29  * (to_char, etc.) and the money type. For the user, this should all be
30  * transparent.
31  *
32  * !!! NOW HEAR THIS !!!
33  *
34  * We've been bitten repeatedly by this bug, so let's try to keep it in
35  * mind in future: on some platforms, the locale functions return pointers
36  * to static data that will be overwritten by any later locale function.
37  * Thus, for example, the obvious-looking sequence
38  * save = setlocale(category, NULL);
39  * if (!setlocale(category, value))
40  * fail = true;
41  * setlocale(category, save);
42  * DOES NOT WORK RELIABLY: on some platforms the second setlocale() call
43  * will change the memory save is pointing at. To do this sort of thing
44  * safely, you *must* pstrdup what setlocale returns the first time.
45  *
46  * The POSIX locale standard is available here:
47  *
48  * http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html
49  *----------
50  */
51 
52 
53 #include "postgres.h"
54 
55 #include <time.h>
56 
57 #include "access/htup_details.h"
58 #include "catalog/pg_collation.h"
59 #include "catalog/pg_control.h"
60 #include "catalog/pg_database.h"
61 #include "mb/pg_wchar.h"
62 #include "miscadmin.h"
63 #include "utils/builtins.h"
64 #include "utils/formatting.h"
65 #include "utils/hsearch.h"
66 #include "utils/lsyscache.h"
67 #include "utils/memutils.h"
68 #include "utils/pg_locale.h"
69 #include "utils/syscache.h"
70 
71 #ifdef USE_ICU
72 #include <unicode/ucnv.h>
73 #endif
74 
75 #ifdef __GLIBC__
76 #include <gnu/libc-version.h>
77 #endif
78 
79 #ifdef WIN32
80 #include <shlwapi.h>
81 #endif
82 
83 #define MAX_L10N_DATA 80
84 
85 
86 /* GUC settings */
91 
92 /*
93  * lc_time localization cache.
94  *
95  * We use only the first 7 or 12 entries of these arrays. The last array
96  * element is left as NULL for the convenience of outside code that wants
97  * to sequentially scan these arrays.
98  */
99 char *localized_abbrev_days[7 + 1];
100 char *localized_full_days[7 + 1];
102 char *localized_full_months[12 + 1];
103 
104 /* indicates whether locale information cache is valid */
105 static bool CurrentLocaleConvValid = false;
106 static bool CurrentLCTimeValid = false;
107 
108 /* Cache for collation-related knowledge */
109 
110 typedef struct
111 {
112  Oid collid; /* hash key: pg_collation OID */
113  bool collate_is_c; /* is collation's LC_COLLATE C? */
114  bool ctype_is_c; /* is collation's LC_CTYPE C? */
115  bool flags_valid; /* true if above flags are valid */
116  pg_locale_t locale; /* locale_t struct, or 0 if not valid */
118 
119 static HTAB *collation_cache = NULL;
120 
121 
122 #if defined(WIN32) && defined(LC_MESSAGES)
123 static char *IsoLocaleName(const char *); /* MSVC specific */
124 #endif
125 
126 #ifdef USE_ICU
127 static void icu_set_collation_attributes(UCollator *collator, const char *loc);
128 #endif
129 
130 static char *get_collation_actual_version(char collprovider,
131  const char *collcollate);
132 
133 /*
134  * pg_perm_setlocale
135  *
136  * This wraps the libc function setlocale(), with two additions. First, when
137  * changing LC_CTYPE, update gettext's encoding for the current message
138  * domain. GNU gettext automatically tracks LC_CTYPE on most platforms, but
139  * not on Windows. Second, if the operation is successful, the corresponding
140  * LC_XXX environment variable is set to match. By setting the environment
141  * variable, we ensure that any subsequent use of setlocale(..., "") will
142  * preserve the settings made through this routine. Of course, LC_ALL must
143  * also be unset to fully ensure that, but that has to be done elsewhere after
144  * all the individual LC_XXX variables have been set correctly. (Thank you
145  * Perl for making this kluge necessary.)
146  */
147 char *
148 pg_perm_setlocale(int category, const char *locale)
149 {
150  char *result;
151  const char *envvar;
152 
153 #ifndef WIN32
154  result = setlocale(category, locale);
155 #else
156 
157  /*
158  * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
159  * the given value is good and set it in the environment variables. We
160  * must ignore attempts to set to "", which means "keep using the old
161  * environment value".
162  */
163 #ifdef LC_MESSAGES
164  if (category == LC_MESSAGES)
165  {
166  result = (char *) locale;
167  if (locale == NULL || locale[0] == '\0')
168  return result;
169  }
170  else
171 #endif
172  result = setlocale(category, locale);
173 #endif /* WIN32 */
174 
175  if (result == NULL)
176  return result; /* fall out immediately on failure */
177 
178  /*
179  * Use the right encoding in translated messages. Under ENABLE_NLS, let
180  * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
181  * format strings are ASCII, but database-encoding strings may enter the
182  * message via %s. This makes the overall message encoding equal to the
183  * database encoding.
184  */
185  if (category == LC_CTYPE)
186  {
187  static char save_lc_ctype[NAMEDATALEN + 20];
188 
189  /* copy setlocale() return value before callee invokes it again */
190  strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
191  result = save_lc_ctype;
192 
193 #ifdef ENABLE_NLS
194  SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
195 #else
197 #endif
198  }
199 
200  switch (category)
201  {
202  case LC_COLLATE:
203  envvar = "LC_COLLATE";
204  break;
205  case LC_CTYPE:
206  envvar = "LC_CTYPE";
207  break;
208 #ifdef LC_MESSAGES
209  case LC_MESSAGES:
210  envvar = "LC_MESSAGES";
211 #ifdef WIN32
212  result = IsoLocaleName(locale);
213  if (result == NULL)
214  result = (char *) locale;
215  elog(DEBUG3, "IsoLocaleName() executed; locale: \"%s\"", result);
216 #endif /* WIN32 */
217  break;
218 #endif /* LC_MESSAGES */
219  case LC_MONETARY:
220  envvar = "LC_MONETARY";
221  break;
222  case LC_NUMERIC:
223  envvar = "LC_NUMERIC";
224  break;
225  case LC_TIME:
226  envvar = "LC_TIME";
227  break;
228  default:
229  elog(FATAL, "unrecognized LC category: %d", category);
230  return NULL; /* keep compiler quiet */
231  }
232 
233  if (setenv(envvar, result, 1) != 0)
234  return NULL;
235 
236  return result;
237 }
238 
239 
240 /*
241  * Is the locale name valid for the locale category?
242  *
243  * If successful, and canonname isn't NULL, a palloc'd copy of the locale's
244  * canonical name is stored there. This is especially useful for figuring out
245  * what locale name "" means (ie, the server environment value). (Actually,
246  * it seems that on most implementations that's the only thing it's good for;
247  * we could wish that setlocale gave back a canonically spelled version of
248  * the locale name, but typically it doesn't.)
249  */
250 bool
251 check_locale(int category, const char *locale, char **canonname)
252 {
253  char *save;
254  char *res;
255 
256  if (canonname)
257  *canonname = NULL; /* in case of failure */
258 
259  save = setlocale(category, NULL);
260  if (!save)
261  return false; /* won't happen, we hope */
262 
263  /* save may be pointing at a modifiable scratch variable, see above. */
264  save = pstrdup(save);
265 
266  /* set the locale with setlocale, to see if it accepts it. */
267  res = setlocale(category, locale);
268 
269  /* save canonical name if requested. */
270  if (res && canonname)
271  *canonname = pstrdup(res);
272 
273  /* restore old value. */
274  if (!setlocale(category, save))
275  elog(WARNING, "failed to restore old locale \"%s\"", save);
276  pfree(save);
277 
278  return (res != NULL);
279 }
280 
281 
282 /*
283  * GUC check/assign hooks
284  *
285  * For most locale categories, the assign hook doesn't actually set the locale
286  * permanently, just reset flags so that the next use will cache the
287  * appropriate values. (See explanation at the top of this file.)
288  *
289  * Note: we accept value = "" as selecting the postmaster's environment
290  * value, whatever it was (so long as the environment setting is legal).
291  * This will have been locked down by an earlier call to pg_perm_setlocale.
292  */
293 bool
295 {
296  return check_locale(LC_MONETARY, *newval, NULL);
297 }
298 
299 void
300 assign_locale_monetary(const char *newval, void *extra)
301 {
302  CurrentLocaleConvValid = false;
303 }
304 
305 bool
307 {
308  return check_locale(LC_NUMERIC, *newval, NULL);
309 }
310 
311 void
312 assign_locale_numeric(const char *newval, void *extra)
313 {
314  CurrentLocaleConvValid = false;
315 }
316 
317 bool
318 check_locale_time(char **newval, void **extra, GucSource source)
319 {
320  return check_locale(LC_TIME, *newval, NULL);
321 }
322 
323 void
324 assign_locale_time(const char *newval, void *extra)
325 {
326  CurrentLCTimeValid = false;
327 }
328 
329 /*
330  * We allow LC_MESSAGES to actually be set globally.
331  *
332  * Note: we normally disallow value = "" because it wouldn't have consistent
333  * semantics (it'd effectively just use the previous value). However, this
334  * is the value passed for PGC_S_DEFAULT, so don't complain in that case,
335  * not even if the attempted setting fails due to invalid environment value.
336  * The idea there is just to accept the environment setting *if possible*
337  * during startup, until we can read the proper value from postgresql.conf.
338  */
339 bool
341 {
342  if (**newval == '\0')
343  {
344  if (source == PGC_S_DEFAULT)
345  return true;
346  else
347  return false;
348  }
349 
350  /*
351  * LC_MESSAGES category does not exist everywhere, but accept it anyway
352  *
353  * On Windows, we can't even check the value, so accept blindly
354  */
355 #if defined(LC_MESSAGES) && !defined(WIN32)
356  return check_locale(LC_MESSAGES, *newval, NULL);
357 #else
358  return true;
359 #endif
360 }
361 
362 void
363 assign_locale_messages(const char *newval, void *extra)
364 {
365  /*
366  * LC_MESSAGES category does not exist everywhere, but accept it anyway.
367  * We ignore failure, as per comment above.
368  */
369 #ifdef LC_MESSAGES
370  (void) pg_perm_setlocale(LC_MESSAGES, newval);
371 #endif
372 }
373 
374 
375 /*
376  * Frees the malloced content of a struct lconv. (But not the struct
377  * itself.) It's important that this not throw elog(ERROR).
378  */
379 static void
380 free_struct_lconv(struct lconv *s)
381 {
382  if (s->decimal_point)
383  free(s->decimal_point);
384  if (s->thousands_sep)
385  free(s->thousands_sep);
386  if (s->grouping)
387  free(s->grouping);
388  if (s->int_curr_symbol)
389  free(s->int_curr_symbol);
390  if (s->currency_symbol)
391  free(s->currency_symbol);
392  if (s->mon_decimal_point)
393  free(s->mon_decimal_point);
394  if (s->mon_thousands_sep)
395  free(s->mon_thousands_sep);
396  if (s->mon_grouping)
397  free(s->mon_grouping);
398  if (s->positive_sign)
399  free(s->positive_sign);
400  if (s->negative_sign)
401  free(s->negative_sign);
402 }
403 
404 /*
405  * Check that all fields of a struct lconv (or at least, the ones we care
406  * about) are non-NULL. The field list must match free_struct_lconv().
407  */
408 static bool
409 struct_lconv_is_valid(struct lconv *s)
410 {
411  if (s->decimal_point == NULL)
412  return false;
413  if (s->thousands_sep == NULL)
414  return false;
415  if (s->grouping == NULL)
416  return false;
417  if (s->int_curr_symbol == NULL)
418  return false;
419  if (s->currency_symbol == NULL)
420  return false;
421  if (s->mon_decimal_point == NULL)
422  return false;
423  if (s->mon_thousands_sep == NULL)
424  return false;
425  if (s->mon_grouping == NULL)
426  return false;
427  if (s->positive_sign == NULL)
428  return false;
429  if (s->negative_sign == NULL)
430  return false;
431  return true;
432 }
433 
434 
435 /*
436  * Convert the strdup'd string at *str from the specified encoding to the
437  * database encoding.
438  */
439 static void
441 {
442  char *pstr;
443  char *mstr;
444 
445  /* convert the string to the database encoding */
446  pstr = pg_any_to_server(*str, strlen(*str), encoding);
447  if (pstr == *str)
448  return; /* no conversion happened */
449 
450  /* need it malloc'd not palloc'd */
451  mstr = strdup(pstr);
452  if (mstr == NULL)
453  ereport(ERROR,
454  (errcode(ERRCODE_OUT_OF_MEMORY),
455  errmsg("out of memory")));
456 
457  /* replace old string */
458  free(*str);
459  *str = mstr;
460 
461  pfree(pstr);
462 }
463 
464 
465 /*
466  * Return the POSIX lconv struct (contains number/money formatting
467  * information) with locale information for all categories.
468  */
469 struct lconv *
471 {
472  static struct lconv CurrentLocaleConv;
473  static bool CurrentLocaleConvAllocated = false;
474  struct lconv *extlconv;
475  struct lconv worklconv;
476  char *save_lc_monetary;
477  char *save_lc_numeric;
478 #ifdef WIN32
479  char *save_lc_ctype;
480 #endif
481 
482  /* Did we do it already? */
484  return &CurrentLocaleConv;
485 
486  /* Free any already-allocated storage */
487  if (CurrentLocaleConvAllocated)
488  {
489  free_struct_lconv(&CurrentLocaleConv);
490  CurrentLocaleConvAllocated = false;
491  }
492 
493  /*
494  * This is tricky because we really don't want to risk throwing error
495  * while the locale is set to other than our usual settings. Therefore,
496  * the process is: collect the usual settings, set locale to special
497  * setting, copy relevant data into worklconv using strdup(), restore
498  * normal settings, convert data to desired encoding, and finally stash
499  * the collected data in CurrentLocaleConv. This makes it safe if we
500  * throw an error during encoding conversion or run out of memory anywhere
501  * in the process. All data pointed to by struct lconv members is
502  * allocated with strdup, to avoid premature elog(ERROR) and to allow
503  * using a single cleanup routine.
504  */
505  memset(&worklconv, 0, sizeof(worklconv));
506 
507  /* Save prevailing values of monetary and numeric locales */
508  save_lc_monetary = setlocale(LC_MONETARY, NULL);
509  if (!save_lc_monetary)
510  elog(ERROR, "setlocale(NULL) failed");
511  save_lc_monetary = pstrdup(save_lc_monetary);
512 
513  save_lc_numeric = setlocale(LC_NUMERIC, NULL);
514  if (!save_lc_numeric)
515  elog(ERROR, "setlocale(NULL) failed");
516  save_lc_numeric = pstrdup(save_lc_numeric);
517 
518 #ifdef WIN32
519 
520  /*
521  * The POSIX standard explicitly says that it is undefined what happens if
522  * LC_MONETARY or LC_NUMERIC imply an encoding (codeset) different from
523  * that implied by LC_CTYPE. In practice, all Unix-ish platforms seem to
524  * believe that localeconv() should return strings that are encoded in the
525  * codeset implied by the LC_MONETARY or LC_NUMERIC locale name. Hence,
526  * once we have successfully collected the localeconv() results, we will
527  * convert them from that codeset to the desired server encoding.
528  *
529  * Windows, of course, resolutely does things its own way; on that
530  * platform LC_CTYPE has to match LC_MONETARY/LC_NUMERIC to get sane
531  * results. Hence, we must temporarily set that category as well.
532  */
533 
534  /* Save prevailing value of ctype locale */
535  save_lc_ctype = setlocale(LC_CTYPE, NULL);
536  if (!save_lc_ctype)
537  elog(ERROR, "setlocale(NULL) failed");
538  save_lc_ctype = pstrdup(save_lc_ctype);
539 
540  /* Here begins the critical section where we must not throw error */
541 
542  /* use numeric to set the ctype */
543  setlocale(LC_CTYPE, locale_numeric);
544 #endif
545 
546  /* Get formatting information for numeric */
547  setlocale(LC_NUMERIC, locale_numeric);
548  extlconv = localeconv();
549 
550  /* Must copy data now in case setlocale() overwrites it */
551  worklconv.decimal_point = strdup(extlconv->decimal_point);
552  worklconv.thousands_sep = strdup(extlconv->thousands_sep);
553  worklconv.grouping = strdup(extlconv->grouping);
554 
555 #ifdef WIN32
556  /* use monetary to set the ctype */
557  setlocale(LC_CTYPE, locale_monetary);
558 #endif
559 
560  /* Get formatting information for monetary */
561  setlocale(LC_MONETARY, locale_monetary);
562  extlconv = localeconv();
563 
564  /* Must copy data now in case setlocale() overwrites it */
565  worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
566  worklconv.currency_symbol = strdup(extlconv->currency_symbol);
567  worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
568  worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
569  worklconv.mon_grouping = strdup(extlconv->mon_grouping);
570  worklconv.positive_sign = strdup(extlconv->positive_sign);
571  worklconv.negative_sign = strdup(extlconv->negative_sign);
572  /* Copy scalar fields as well */
573  worklconv.int_frac_digits = extlconv->int_frac_digits;
574  worklconv.frac_digits = extlconv->frac_digits;
575  worklconv.p_cs_precedes = extlconv->p_cs_precedes;
576  worklconv.p_sep_by_space = extlconv->p_sep_by_space;
577  worklconv.n_cs_precedes = extlconv->n_cs_precedes;
578  worklconv.n_sep_by_space = extlconv->n_sep_by_space;
579  worklconv.p_sign_posn = extlconv->p_sign_posn;
580  worklconv.n_sign_posn = extlconv->n_sign_posn;
581 
582  /*
583  * Restore the prevailing locale settings; failure to do so is fatal.
584  * Possibly we could limp along with nondefault LC_MONETARY or LC_NUMERIC,
585  * but proceeding with the wrong value of LC_CTYPE would certainly be bad
586  * news; and considering that the prevailing LC_MONETARY and LC_NUMERIC
587  * are almost certainly "C", there's really no reason that restoring those
588  * should fail.
589  */
590 #ifdef WIN32
591  if (!setlocale(LC_CTYPE, save_lc_ctype))
592  elog(FATAL, "failed to restore LC_CTYPE to \"%s\"", save_lc_ctype);
593 #endif
594  if (!setlocale(LC_MONETARY, save_lc_monetary))
595  elog(FATAL, "failed to restore LC_MONETARY to \"%s\"", save_lc_monetary);
596  if (!setlocale(LC_NUMERIC, save_lc_numeric))
597  elog(FATAL, "failed to restore LC_NUMERIC to \"%s\"", save_lc_numeric);
598 
599  /*
600  * At this point we've done our best to clean up, and can call functions
601  * that might possibly throw errors with a clean conscience. But let's
602  * make sure we don't leak any already-strdup'd fields in worklconv.
603  */
604  PG_TRY();
605  {
606  int encoding;
607 
608  /* Release the pstrdup'd locale names */
609  pfree(save_lc_monetary);
610  pfree(save_lc_numeric);
611 #ifdef WIN32
612  pfree(save_lc_ctype);
613 #endif
614 
615  /* If any of the preceding strdup calls failed, complain now. */
616  if (!struct_lconv_is_valid(&worklconv))
617  ereport(ERROR,
618  (errcode(ERRCODE_OUT_OF_MEMORY),
619  errmsg("out of memory")));
620 
621  /*
622  * Now we must perform encoding conversion from whatever's associated
623  * with the locales into the database encoding. If we can't identify
624  * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
625  * use PG_SQL_ASCII, which will result in just validating that the
626  * strings are OK in the database encoding.
627  */
629  if (encoding < 0)
630  encoding = PG_SQL_ASCII;
631 
632  db_encoding_convert(encoding, &worklconv.decimal_point);
633  db_encoding_convert(encoding, &worklconv.thousands_sep);
634  /* grouping is not text and does not require conversion */
635 
637  if (encoding < 0)
638  encoding = PG_SQL_ASCII;
639 
640  db_encoding_convert(encoding, &worklconv.int_curr_symbol);
641  db_encoding_convert(encoding, &worklconv.currency_symbol);
642  db_encoding_convert(encoding, &worklconv.mon_decimal_point);
643  db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
644  /* mon_grouping is not text and does not require conversion */
645  db_encoding_convert(encoding, &worklconv.positive_sign);
646  db_encoding_convert(encoding, &worklconv.negative_sign);
647  }
648  PG_CATCH();
649  {
650  free_struct_lconv(&worklconv);
651  PG_RE_THROW();
652  }
653  PG_END_TRY();
654 
655  /*
656  * Everything is good, so save the results.
657  */
658  CurrentLocaleConv = worklconv;
659  CurrentLocaleConvAllocated = true;
660  CurrentLocaleConvValid = true;
661  return &CurrentLocaleConv;
662 }
663 
664 #ifdef WIN32
665 /*
666  * On Windows, strftime() returns its output in encoding CP_ACP (the default
667  * operating system codepage for the computer), which is likely different
668  * from SERVER_ENCODING. This is especially important in Japanese versions
669  * of Windows which will use SJIS encoding, which we don't support as a
670  * server encoding.
671  *
672  * So, instead of using strftime(), use wcsftime() to return the value in
673  * wide characters (internally UTF16) and then convert to UTF8, which we
674  * know how to handle directly.
675  *
676  * Note that this only affects the calls to strftime() in this file, which are
677  * used to get the locale-aware strings. Other parts of the backend use
678  * pg_strftime(), which isn't locale-aware and does not need to be replaced.
679  */
680 static size_t
681 strftime_win32(char *dst, size_t dstlen,
682  const char *format, const struct tm *tm)
683 {
684  size_t len;
685  wchar_t wformat[8]; /* formats used below need 3 chars */
686  wchar_t wbuf[MAX_L10N_DATA];
687 
688  /*
689  * Get a wchar_t version of the format string. We only actually use
690  * plain-ASCII formats in this file, so we can say that they're UTF8.
691  */
692  len = MultiByteToWideChar(CP_UTF8, 0, format, -1,
693  wformat, lengthof(wformat));
694  if (len == 0)
695  elog(ERROR, "could not convert format string from UTF-8: error code %lu",
696  GetLastError());
697 
698  len = wcsftime(wbuf, MAX_L10N_DATA, wformat, tm);
699  if (len == 0)
700  {
701  /*
702  * wcsftime failed, possibly because the result would not fit in
703  * MAX_L10N_DATA. Return 0 with the contents of dst unspecified.
704  */
705  return 0;
706  }
707 
708  len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen - 1,
709  NULL, NULL);
710  if (len == 0)
711  elog(ERROR, "could not convert string to UTF-8: error code %lu",
712  GetLastError());
713 
714  dst[len] = '\0';
715 
716  return len;
717 }
718 
719 /* redefine strftime() */
720 #define strftime(a,b,c,d) strftime_win32(a,b,c,d)
721 #endif /* WIN32 */
722 
723 /*
724  * Subroutine for cache_locale_time().
725  * Convert the given string from encoding "encoding" to the database
726  * encoding, and store the result at *dst, replacing any previous value.
727  */
728 static void
729 cache_single_string(char **dst, const char *src, int encoding)
730 {
731  char *ptr;
732  char *olddst;
733 
734  /* Convert the string to the database encoding, or validate it's OK */
735  ptr = pg_any_to_server(src, strlen(src), encoding);
736 
737  /* Store the string in long-lived storage, replacing any previous value */
738  olddst = *dst;
740  if (olddst)
741  pfree(olddst);
742 
743  /* Might as well clean up any palloc'd conversion result, too */
744  if (ptr != src)
745  pfree(ptr);
746 }
747 
748 /*
749  * Update the lc_time localization cache variables if needed.
750  */
751 void
753 {
754  char buf[(2 * 7 + 2 * 12) * MAX_L10N_DATA];
755  char *bufptr;
756  time_t timenow;
757  struct tm *timeinfo;
758  bool strftimefail = false;
759  int encoding;
760  int i;
761  char *save_lc_time;
762 #ifdef WIN32
763  char *save_lc_ctype;
764 #endif
765 
766  /* did we do this already? */
767  if (CurrentLCTimeValid)
768  return;
769 
770  elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
771 
772  /*
773  * As in PGLC_localeconv(), it's critical that we not throw error while
774  * libc's locale settings have nondefault values. Hence, we just call
775  * strftime() within the critical section, and then convert and save its
776  * results afterwards.
777  */
778 
779  /* Save prevailing value of time locale */
780  save_lc_time = setlocale(LC_TIME, NULL);
781  if (!save_lc_time)
782  elog(ERROR, "setlocale(NULL) failed");
783  save_lc_time = pstrdup(save_lc_time);
784 
785 #ifdef WIN32
786 
787  /*
788  * On Windows, it appears that wcsftime() internally uses LC_CTYPE, so we
789  * must set it here. This code looks the same as what PGLC_localeconv()
790  * does, but the underlying reason is different: this does NOT determine
791  * the encoding we'll get back from strftime_win32().
792  */
793 
794  /* Save prevailing value of ctype locale */
795  save_lc_ctype = setlocale(LC_CTYPE, NULL);
796  if (!save_lc_ctype)
797  elog(ERROR, "setlocale(NULL) failed");
798  save_lc_ctype = pstrdup(save_lc_ctype);
799 
800  /* use lc_time to set the ctype */
801  setlocale(LC_CTYPE, locale_time);
802 #endif
803 
804  setlocale(LC_TIME, locale_time);
805 
806  /* We use times close to current time as data for strftime(). */
807  timenow = time(NULL);
808  timeinfo = localtime(&timenow);
809 
810  /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
811  bufptr = buf;
812 
813  /*
814  * MAX_L10N_DATA is sufficient buffer space for every known locale, and
815  * POSIX defines no strftime() errors. (Buffer space exhaustion is not an
816  * error.) An implementation might report errors (e.g. ENOMEM) by
817  * returning 0 (or, less plausibly, a negative value) and setting errno.
818  * Report errno just in case the implementation did that, but clear it in
819  * advance of the calls so we don't emit a stale, unrelated errno.
820  */
821  errno = 0;
822 
823  /* localized days */
824  for (i = 0; i < 7; i++)
825  {
826  timeinfo->tm_wday = i;
827  if (strftime(bufptr, MAX_L10N_DATA, "%a", timeinfo) <= 0)
828  strftimefail = true;
829  bufptr += MAX_L10N_DATA;
830  if (strftime(bufptr, MAX_L10N_DATA, "%A", timeinfo) <= 0)
831  strftimefail = true;
832  bufptr += MAX_L10N_DATA;
833  }
834 
835  /* localized months */
836  for (i = 0; i < 12; i++)
837  {
838  timeinfo->tm_mon = i;
839  timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
840  if (strftime(bufptr, MAX_L10N_DATA, "%b", timeinfo) <= 0)
841  strftimefail = true;
842  bufptr += MAX_L10N_DATA;
843  if (strftime(bufptr, MAX_L10N_DATA, "%B", timeinfo) <= 0)
844  strftimefail = true;
845  bufptr += MAX_L10N_DATA;
846  }
847 
848  /*
849  * Restore the prevailing locale settings; as in PGLC_localeconv(),
850  * failure to do so is fatal.
851  */
852 #ifdef WIN32
853  if (!setlocale(LC_CTYPE, save_lc_ctype))
854  elog(FATAL, "failed to restore LC_CTYPE to \"%s\"", save_lc_ctype);
855 #endif
856  if (!setlocale(LC_TIME, save_lc_time))
857  elog(FATAL, "failed to restore LC_TIME to \"%s\"", save_lc_time);
858 
859  /*
860  * At this point we've done our best to clean up, and can throw errors, or
861  * call functions that might throw errors, with a clean conscience.
862  */
863  if (strftimefail)
864  elog(ERROR, "strftime() failed: %m");
865 
866  /* Release the pstrdup'd locale names */
867  pfree(save_lc_time);
868 #ifdef WIN32
869  pfree(save_lc_ctype);
870 #endif
871 
872 #ifndef WIN32
873 
874  /*
875  * As in PGLC_localeconv(), we must convert strftime()'s output from the
876  * encoding implied by LC_TIME to the database encoding. If we can't
877  * identify the LC_TIME encoding, just perform encoding validation.
878  */
879  encoding = pg_get_encoding_from_locale(locale_time, true);
880  if (encoding < 0)
881  encoding = PG_SQL_ASCII;
882 
883 #else
884 
885  /*
886  * On Windows, strftime_win32() always returns UTF8 data, so convert from
887  * that if necessary.
888  */
889  encoding = PG_UTF8;
890 
891 #endif /* WIN32 */
892 
893  bufptr = buf;
894 
895  /* localized days */
896  for (i = 0; i < 7; i++)
897  {
898  cache_single_string(&localized_abbrev_days[i], bufptr, encoding);
899  bufptr += MAX_L10N_DATA;
900  cache_single_string(&localized_full_days[i], bufptr, encoding);
901  bufptr += MAX_L10N_DATA;
902  }
903  localized_abbrev_days[7] = NULL;
904  localized_full_days[7] = NULL;
905 
906  /* localized months */
907  for (i = 0; i < 12; i++)
908  {
909  cache_single_string(&localized_abbrev_months[i], bufptr, encoding);
910  bufptr += MAX_L10N_DATA;
911  cache_single_string(&localized_full_months[i], bufptr, encoding);
912  bufptr += MAX_L10N_DATA;
913  }
914  localized_abbrev_months[12] = NULL;
915  localized_full_months[12] = NULL;
916 
917  CurrentLCTimeValid = true;
918 }
919 
920 
921 #if defined(WIN32) && defined(LC_MESSAGES)
922 /*
923  * Convert a Windows setlocale() argument to a Unix-style one.
924  *
925  * Regardless of platform, we install message catalogs under a Unix-style
926  * LL[_CC][.ENCODING][@VARIANT] naming convention. Only LC_MESSAGES settings
927  * following that style will elicit localized interface strings.
928  *
929  * Before Visual Studio 2012 (msvcr110.dll), Windows setlocale() accepted "C"
930  * (but not "c") and strings of the form <Language>[_<Country>][.<CodePage>],
931  * case-insensitive. setlocale() returns the fully-qualified form; for
932  * example, setlocale("thaI") returns "Thai_Thailand.874". Internally,
933  * setlocale() and _create_locale() select a "locale identifier"[1] and store
934  * it in an undocumented _locale_t field. From that LCID, we can retrieve the
935  * ISO 639 language and the ISO 3166 country. Character encoding does not
936  * matter, because the server and client encodings govern that.
937  *
938  * Windows Vista introduced the "locale name" concept[2], closely following
939  * RFC 4646. Locale identifiers are now deprecated. Starting with Visual
940  * Studio 2012, setlocale() accepts locale names in addition to the strings it
941  * accepted historically. It does not standardize them; setlocale("Th-tH")
942  * returns "Th-tH". setlocale(category, "") still returns a traditional
943  * string. Furthermore, msvcr110.dll changed the undocumented _locale_t
944  * content to carry locale names instead of locale identifiers.
945  *
946  * Visual Studio 2015 should still be able to do the same as Visual Studio
947  * 2012, but the declaration of locale_name is missing in _locale_t, causing
948  * this code compilation to fail, hence this falls back instead on to
949  * enumerating all system locales by using EnumSystemLocalesEx to find the
950  * required locale name. If the input argument is in Unix-style then we can
951  * get ISO Locale name directly by using GetLocaleInfoEx() with LCType as
952  * LOCALE_SNAME.
953  *
954  * MinGW headers declare _create_locale(), but msvcrt.dll lacks that symbol in
955  * releases before Windows 8. IsoLocaleName() always fails in a MinGW-built
956  * postgres.exe, so only Unix-style values of the lc_messages GUC can elicit
957  * localized messages. In particular, every lc_messages setting that initdb
958  * can select automatically will yield only C-locale messages. XXX This could
959  * be fixed by running the fully-qualified locale name through a lookup table.
960  *
961  * This function returns a pointer to a static buffer bearing the converted
962  * name or NULL if conversion fails.
963  *
964  * [1] https://docs.microsoft.com/en-us/windows/win32/intl/locale-identifiers
965  * [2] https://docs.microsoft.com/en-us/windows/win32/intl/locale-names
966  */
967 
968 #if _MSC_VER >= 1900
969 /*
970  * Callback function for EnumSystemLocalesEx() in get_iso_localename().
971  *
972  * This function enumerates all system locales, searching for one that matches
973  * an input with the format: <Language>[_<Country>], e.g.
974  * English[_United States]
975  *
976  * The input is a three wchar_t array as an LPARAM. The first element is the
977  * locale_name we want to match, the second element is an allocated buffer
978  * where the Unix-style locale is copied if a match is found, and the third
979  * element is the search status, 1 if a match was found, 0 otherwise.
980  */
981 static BOOL CALLBACK
982 search_locale_enum(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
983 {
984  wchar_t test_locale[LOCALE_NAME_MAX_LENGTH];
985  wchar_t **argv;
986 
987  (void) (dwFlags);
988 
989  argv = (wchar_t **) lparam;
990  *argv[2] = (wchar_t) 0;
991 
992  memset(test_locale, 0, sizeof(test_locale));
993 
994  /* Get the name of the <Language> in English */
995  if (GetLocaleInfoEx(pStr, LOCALE_SENGLISHLANGUAGENAME,
996  test_locale, LOCALE_NAME_MAX_LENGTH))
997  {
998  /*
999  * If the enumerated locale does not have a hyphen ("en") OR the
1000  * lc_message input does not have an underscore ("English"), we only
1001  * need to compare the <Language> tags.
1002  */
1003  if (wcsrchr(pStr, '-') == NULL || wcsrchr(argv[0], '_') == NULL)
1004  {
1005  if (_wcsicmp(argv[0], test_locale) == 0)
1006  {
1007  wcscpy(argv[1], pStr);
1008  *argv[2] = (wchar_t) 1;
1009  return FALSE;
1010  }
1011  }
1012 
1013  /*
1014  * We have to compare a full <Language>_<Country> tag, so we append
1015  * the underscore and name of the country/region in English, e.g.
1016  * "English_United States".
1017  */
1018  else
1019  {
1020  size_t len;
1021 
1022  wcscat(test_locale, L"_");
1023  len = wcslen(test_locale);
1024  if (GetLocaleInfoEx(pStr, LOCALE_SENGLISHCOUNTRYNAME,
1025  test_locale + len,
1026  LOCALE_NAME_MAX_LENGTH - len))
1027  {
1028  if (_wcsicmp(argv[0], test_locale) == 0)
1029  {
1030  wcscpy(argv[1], pStr);
1031  *argv[2] = (wchar_t) 1;
1032  return FALSE;
1033  }
1034  }
1035  }
1036  }
1037 
1038  return TRUE;
1039 }
1040 
1041 /*
1042  * This function converts a Windows locale name to an ISO formatted version
1043  * for Visual Studio 2015 or greater.
1044  *
1045  * Returns NULL, if no valid conversion was found.
1046  */
1047 static char *
1048 get_iso_localename(const char *winlocname)
1049 {
1050  wchar_t wc_locale_name[LOCALE_NAME_MAX_LENGTH];
1051  wchar_t buffer[LOCALE_NAME_MAX_LENGTH];
1052  static char iso_lc_messages[LOCALE_NAME_MAX_LENGTH];
1053  char *period;
1054  int len;
1055  int ret_val;
1056 
1057  /*
1058  * Valid locales have the following syntax:
1059  * <Language>[_<Country>[.<CodePage>]]
1060  *
1061  * GetLocaleInfoEx can only take locale name without code-page and for the
1062  * purpose of this API the code-page doesn't matter.
1063  */
1064  period = strchr(winlocname, '.');
1065  if (period != NULL)
1066  len = period - winlocname;
1067  else
1068  len = pg_mbstrlen(winlocname);
1069 
1070  memset(wc_locale_name, 0, sizeof(wc_locale_name));
1071  memset(buffer, 0, sizeof(buffer));
1072  MultiByteToWideChar(CP_ACP, 0, winlocname, len, wc_locale_name,
1073  LOCALE_NAME_MAX_LENGTH);
1074 
1075  /*
1076  * If the lc_messages is already an Unix-style string, we have a direct
1077  * match with LOCALE_SNAME, e.g. en-US, en_US.
1078  */
1079  ret_val = GetLocaleInfoEx(wc_locale_name, LOCALE_SNAME, (LPWSTR) &buffer,
1080  LOCALE_NAME_MAX_LENGTH);
1081  if (!ret_val)
1082  {
1083  /*
1084  * Search for a locale in the system that matches language and country
1085  * name.
1086  */
1087  wchar_t *argv[3];
1088 
1089  argv[0] = wc_locale_name;
1090  argv[1] = buffer;
1091  argv[2] = (wchar_t *) &ret_val;
1092  EnumSystemLocalesEx(search_locale_enum, LOCALE_WINDOWS, (LPARAM) argv,
1093  NULL);
1094  }
1095 
1096  if (ret_val)
1097  {
1098  size_t rc;
1099  char *hyphen;
1100 
1101  /* Locale names use only ASCII, any conversion locale suffices. */
1102  rc = wchar2char(iso_lc_messages, buffer, sizeof(iso_lc_messages), NULL);
1103  if (rc == -1 || rc == sizeof(iso_lc_messages))
1104  return NULL;
1105 
1106  /*
1107  * Simply replace the hyphen with an underscore. See comments in
1108  * IsoLocaleName.
1109  */
1110  hyphen = strchr(iso_lc_messages, '-');
1111  if (hyphen)
1112  *hyphen = '_';
1113  return iso_lc_messages;
1114  }
1115 
1116  return NULL;
1117 }
1118 #endif /* _MSC_VER >= 1900 */
1119 
1120 static char *
1121 IsoLocaleName(const char *winlocname)
1122 {
1123 #if defined(_MSC_VER)
1124  static char iso_lc_messages[LOCALE_NAME_MAX_LENGTH];
1125 
1126  if (pg_strcasecmp("c", winlocname) == 0 ||
1127  pg_strcasecmp("posix", winlocname) == 0)
1128  {
1129  strcpy(iso_lc_messages, "C");
1130  return iso_lc_messages;
1131  }
1132  else
1133  {
1134 #if (_MSC_VER >= 1900) /* Visual Studio 2015 or later */
1135  return get_iso_localename(winlocname);
1136 #else
1137  _locale_t loct;
1138 
1139  loct = _create_locale(LC_CTYPE, winlocname);
1140  if (loct != NULL)
1141  {
1142  size_t rc;
1143  char *hyphen;
1144 
1145  /* Locale names use only ASCII, any conversion locale suffices. */
1146  rc = wchar2char(iso_lc_messages, loct->locinfo->locale_name[LC_CTYPE],
1147  sizeof(iso_lc_messages), NULL);
1148  _free_locale(loct);
1149  if (rc == -1 || rc == sizeof(iso_lc_messages))
1150  return NULL;
1151 
1152  /*
1153  * Since the message catalogs sit on a case-insensitive
1154  * filesystem, we need not standardize letter case here. So long
1155  * as we do not ship message catalogs for which it would matter,
1156  * we also need not translate the script/variant portion, e.g.
1157  * uz-Cyrl-UZ to uz_UZ@cyrillic. Simply replace the hyphen with
1158  * an underscore.
1159  *
1160  * Note that the locale name can be less-specific than the value
1161  * we would derive under earlier Visual Studio releases. For
1162  * example, French_France.1252 yields just "fr". This does not
1163  * affect any of the country-specific message catalogs available
1164  * as of this writing (pt_BR, zh_CN, zh_TW).
1165  */
1166  hyphen = strchr(iso_lc_messages, '-');
1167  if (hyphen)
1168  *hyphen = '_';
1169  return iso_lc_messages;
1170  }
1171 #endif /* Visual Studio 2015 or later */
1172  }
1173 #endif /* defined(_MSC_VER) */
1174  return NULL; /* Not supported on this version of msvc/mingw */
1175 }
1176 #endif /* WIN32 && LC_MESSAGES */
1177 
1178 
1179 /*
1180  * Detect aging strxfrm() implementations that, in a subset of locales, write
1181  * past the specified buffer length. Affected users must update OS packages
1182  * before using PostgreSQL 9.5 or later.
1183  *
1184  * Assume that the bug can come and go from one postmaster startup to another
1185  * due to physical replication among diverse machines. Assume that the bug's
1186  * presence will not change during the life of a particular postmaster. Given
1187  * those assumptions, call this no less than once per postmaster startup per
1188  * LC_COLLATE setting used. No known-affected system offers strxfrm_l(), so
1189  * there is no need to consider pg_collation locales.
1190  */
1191 void
1193 {
1194  char buf[32];
1195  const int canary = 0x7F;
1196  bool ok = true;
1197 
1198  /*
1199  * Given a two-byte ASCII string and length limit 7, 8 or 9, Solaris 10
1200  * 05/08 returns 18 and modifies 10 bytes. It respects limits above or
1201  * below that range.
1202  *
1203  * The bug is present in Solaris 8 as well; it is absent in Solaris 10
1204  * 01/13 and Solaris 11.2. Affected locales include is_IS.ISO8859-1,
1205  * en_US.UTF-8, en_US.ISO8859-1, and ru_RU.KOI8-R. Unaffected locales
1206  * include de_DE.UTF-8, de_DE.ISO8859-1, zh_TW.UTF-8, and C.
1207  */
1208  buf[7] = canary;
1209  (void) strxfrm(buf, "ab", 7);
1210  if (buf[7] != canary)
1211  ok = false;
1212 
1213  /*
1214  * illumos bug #1594 was present in the source tree from 2010-10-11 to
1215  * 2012-02-01. Given an ASCII string of any length and length limit 1,
1216  * affected systems ignore the length limit and modify a number of bytes
1217  * one less than the return value. The problem inputs for this bug do not
1218  * overlap those for the Solaris bug, hence a distinct test.
1219  *
1220  * Affected systems include smartos-20110926T021612Z. Affected locales
1221  * include en_US.ISO8859-1 and en_US.UTF-8. Unaffected locales include C.
1222  */
1223  buf[1] = canary;
1224  (void) strxfrm(buf, "a", 1);
1225  if (buf[1] != canary)
1226  ok = false;
1227 
1228  if (!ok)
1229  ereport(ERROR,
1230  (errcode(ERRCODE_SYSTEM_ERROR),
1231  errmsg_internal("strxfrm(), in locale \"%s\", writes past the specified array length",
1232  setlocale(LC_COLLATE, NULL)),
1233  errhint("Apply system library package updates.")));
1234 }
1235 
1236 
1237 /*
1238  * Cache mechanism for collation information.
1239  *
1240  * We cache two flags: whether the collation's LC_COLLATE or LC_CTYPE is C
1241  * (or POSIX), so we can optimize a few code paths in various places.
1242  * For the built-in C and POSIX collations, we can know that without even
1243  * doing a cache lookup, but we want to support aliases for C/POSIX too.
1244  * For the "default" collation, there are separate static cache variables,
1245  * since consulting the pg_collation catalog doesn't tell us what we need.
1246  *
1247  * Also, if a pg_locale_t has been requested for a collation, we cache that
1248  * for the life of a backend.
1249  *
1250  * Note that some code relies on the flags not reporting false negatives
1251  * (that is, saying it's not C when it is). For example, char2wchar()
1252  * could fail if the locale is C, so str_tolower() shouldn't call it
1253  * in that case.
1254  *
1255  * Note that we currently lack any way to flush the cache. Since we don't
1256  * support ALTER COLLATION, this is OK. The worst case is that someone
1257  * drops a collation, and a useless cache entry hangs around in existing
1258  * backends.
1259  */
1260 
1261 static collation_cache_entry *
1262 lookup_collation_cache(Oid collation, bool set_flags)
1263 {
1264  collation_cache_entry *cache_entry;
1265  bool found;
1266 
1267  Assert(OidIsValid(collation));
1268  Assert(collation != DEFAULT_COLLATION_OID);
1269 
1270  if (collation_cache == NULL)
1271  {
1272  /* First time through, initialize the hash table */
1273  HASHCTL ctl;
1274 
1275  ctl.keysize = sizeof(Oid);
1276  ctl.entrysize = sizeof(collation_cache_entry);
1277  collation_cache = hash_create("Collation cache", 100, &ctl,
1278  HASH_ELEM | HASH_BLOBS);
1279  }
1280 
1281  cache_entry = hash_search(collation_cache, &collation, HASH_ENTER, &found);
1282  if (!found)
1283  {
1284  /*
1285  * Make sure cache entry is marked invalid, in case we fail before
1286  * setting things.
1287  */
1288  cache_entry->flags_valid = false;
1289  cache_entry->locale = 0;
1290  }
1291 
1292  if (set_flags && !cache_entry->flags_valid)
1293  {
1294  /* Attempt to set the flags */
1295  HeapTuple tp;
1296  Form_pg_collation collform;
1297  const char *collcollate;
1298  const char *collctype;
1299 
1300  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
1301  if (!HeapTupleIsValid(tp))
1302  elog(ERROR, "cache lookup failed for collation %u", collation);
1303  collform = (Form_pg_collation) GETSTRUCT(tp);
1304 
1305  collcollate = NameStr(collform->collcollate);
1306  collctype = NameStr(collform->collctype);
1307 
1308  cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) ||
1309  (strcmp(collcollate, "POSIX") == 0));
1310  cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) ||
1311  (strcmp(collctype, "POSIX") == 0));
1312 
1313  cache_entry->flags_valid = true;
1314 
1315  ReleaseSysCache(tp);
1316  }
1317 
1318  return cache_entry;
1319 }
1320 
1321 
1322 /*
1323  * Detect whether collation's LC_COLLATE property is C
1324  */
1325 bool
1327 {
1328  /*
1329  * If we're asked about "collation 0", return false, so that the code will
1330  * go into the non-C path and report that the collation is bogus.
1331  */
1332  if (!OidIsValid(collation))
1333  return false;
1334 
1335  /*
1336  * If we're asked about the default collation, we have to inquire of the C
1337  * library. Cache the result so we only have to compute it once.
1338  */
1339  if (collation == DEFAULT_COLLATION_OID)
1340  {
1341  static int result = -1;
1342  char *localeptr;
1343 
1344  if (result >= 0)
1345  return (bool) result;
1346  localeptr = setlocale(LC_COLLATE, NULL);
1347  if (!localeptr)
1348  elog(ERROR, "invalid LC_COLLATE setting");
1349 
1350  if (strcmp(localeptr, "C") == 0)
1351  result = true;
1352  else if (strcmp(localeptr, "POSIX") == 0)
1353  result = true;
1354  else
1355  result = false;
1356  return (bool) result;
1357  }
1358 
1359  /*
1360  * If we're asked about the built-in C/POSIX collations, we know that.
1361  */
1362  if (collation == C_COLLATION_OID ||
1363  collation == POSIX_COLLATION_OID)
1364  return true;
1365 
1366  /*
1367  * Otherwise, we have to consult pg_collation, but we cache that.
1368  */
1369  return (lookup_collation_cache(collation, true))->collate_is_c;
1370 }
1371 
1372 /*
1373  * Detect whether collation's LC_CTYPE property is C
1374  */
1375 bool
1376 lc_ctype_is_c(Oid collation)
1377 {
1378  /*
1379  * If we're asked about "collation 0", return false, so that the code will
1380  * go into the non-C path and report that the collation is bogus.
1381  */
1382  if (!OidIsValid(collation))
1383  return false;
1384 
1385  /*
1386  * If we're asked about the default collation, we have to inquire of the C
1387  * library. Cache the result so we only have to compute it once.
1388  */
1389  if (collation == DEFAULT_COLLATION_OID)
1390  {
1391  static int result = -1;
1392  char *localeptr;
1393 
1394  if (result >= 0)
1395  return (bool) result;
1396  localeptr = setlocale(LC_CTYPE, NULL);
1397  if (!localeptr)
1398  elog(ERROR, "invalid LC_CTYPE setting");
1399 
1400  if (strcmp(localeptr, "C") == 0)
1401  result = true;
1402  else if (strcmp(localeptr, "POSIX") == 0)
1403  result = true;
1404  else
1405  result = false;
1406  return (bool) result;
1407  }
1408 
1409  /*
1410  * If we're asked about the built-in C/POSIX collations, we know that.
1411  */
1412  if (collation == C_COLLATION_OID ||
1413  collation == POSIX_COLLATION_OID)
1414  return true;
1415 
1416  /*
1417  * Otherwise, we have to consult pg_collation, but we cache that.
1418  */
1419  return (lookup_collation_cache(collation, true))->ctype_is_c;
1420 }
1421 
1422 
1423 /* simple subroutine for reporting errors from newlocale() */
1424 #ifdef HAVE_LOCALE_T
1425 static void
1426 report_newlocale_failure(const char *localename)
1427 {
1428  int save_errno;
1429 
1430  /*
1431  * Windows doesn't provide any useful error indication from
1432  * _create_locale(), and BSD-derived platforms don't seem to feel they
1433  * need to set errno either (even though POSIX is pretty clear that
1434  * newlocale should do so). So, if errno hasn't been set, assume ENOENT
1435  * is what to report.
1436  */
1437  if (errno == 0)
1438  errno = ENOENT;
1439 
1440  /*
1441  * ENOENT means "no such locale", not "no such file", so clarify that
1442  * errno with an errdetail message.
1443  */
1444  save_errno = errno; /* auxiliary funcs might change errno */
1445  ereport(ERROR,
1446  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1447  errmsg("could not create locale \"%s\": %m",
1448  localename),
1449  (save_errno == ENOENT ?
1450  errdetail("The operating system could not find any locale data for the locale name \"%s\".",
1451  localename) : 0)));
1452 }
1453 #endif /* HAVE_LOCALE_T */
1454 
1455 
1456 /*
1457  * Create a locale_t from a collation OID. Results are cached for the
1458  * lifetime of the backend. Thus, do not free the result with freelocale().
1459  *
1460  * As a special optimization, the default/database collation returns 0.
1461  * Callers should then revert to the non-locale_t-enabled code path.
1462  * In fact, they shouldn't call this function at all when they are dealing
1463  * with the default locale. That can save quite a bit in hotspots.
1464  * Also, callers should avoid calling this before going down a C/POSIX
1465  * fastpath, because such a fastpath should work even on platforms without
1466  * locale_t support in the C library.
1467  *
1468  * For simplicity, we always generate COLLATE + CTYPE even though we
1469  * might only need one of them. Since this is called only once per session,
1470  * it shouldn't cost much.
1471  */
1474 {
1475  collation_cache_entry *cache_entry;
1476 
1477  /* Callers must pass a valid OID */
1478  Assert(OidIsValid(collid));
1479 
1480  /* Return 0 for "default" collation, just in case caller forgets */
1481  if (collid == DEFAULT_COLLATION_OID)
1482  return (pg_locale_t) 0;
1483 
1484  cache_entry = lookup_collation_cache(collid, false);
1485 
1486  if (cache_entry->locale == 0)
1487  {
1488  /* We haven't computed this yet in this session, so do it */
1489  HeapTuple tp;
1490  Form_pg_collation collform;
1491  const char *collcollate pg_attribute_unused();
1492  const char *collctype pg_attribute_unused();
1493  struct pg_locale_struct result;
1494  pg_locale_t resultp;
1495 
1496  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1497  if (!HeapTupleIsValid(tp))
1498  elog(ERROR, "cache lookup failed for collation %u", collid);
1499  collform = (Form_pg_collation) GETSTRUCT(tp);
1500 
1501  collcollate = NameStr(collform->collcollate);
1502  collctype = NameStr(collform->collctype);
1503 
1504  /* We'll fill in the result struct locally before allocating memory */
1505  memset(&result, 0, sizeof(result));
1506  result.provider = collform->collprovider;
1507  result.deterministic = collform->collisdeterministic;
1508 
1509  if (collform->collprovider == COLLPROVIDER_LIBC)
1510  {
1511 #ifdef HAVE_LOCALE_T
1512  locale_t loc;
1513 
1514  if (strcmp(collcollate, collctype) == 0)
1515  {
1516  /* Normal case where they're the same */
1517  errno = 0;
1518 #ifndef WIN32
1519  loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate,
1520  NULL);
1521 #else
1522  loc = _create_locale(LC_ALL, collcollate);
1523 #endif
1524  if (!loc)
1525  report_newlocale_failure(collcollate);
1526  }
1527  else
1528  {
1529 #ifndef WIN32
1530  /* We need two newlocale() steps */
1531  locale_t loc1;
1532 
1533  errno = 0;
1534  loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL);
1535  if (!loc1)
1536  report_newlocale_failure(collcollate);
1537  errno = 0;
1538  loc = newlocale(LC_CTYPE_MASK, collctype, loc1);
1539  if (!loc)
1540  report_newlocale_failure(collctype);
1541 #else
1542 
1543  /*
1544  * XXX The _create_locale() API doesn't appear to support
1545  * this. Could perhaps be worked around by changing
1546  * pg_locale_t to contain two separate fields.
1547  */
1548  ereport(ERROR,
1549  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1550  errmsg("collations with different collate and ctype values are not supported on this platform")));
1551 #endif
1552  }
1553 
1554  result.info.lt = loc;
1555 #else /* not HAVE_LOCALE_T */
1556  /* platform that doesn't support locale_t */
1557  ereport(ERROR,
1558  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1559  errmsg("collation provider LIBC is not supported on this platform")));
1560 #endif /* not HAVE_LOCALE_T */
1561  }
1562  else if (collform->collprovider == COLLPROVIDER_ICU)
1563  {
1564 #ifdef USE_ICU
1565  UCollator *collator;
1566  UErrorCode status;
1567 
1568  if (strcmp(collcollate, collctype) != 0)
1569  ereport(ERROR,
1570  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1571  errmsg("collations with different collate and ctype values are not supported by ICU")));
1572 
1573  status = U_ZERO_ERROR;
1574  collator = ucol_open(collcollate, &status);
1575  if (U_FAILURE(status))
1576  ereport(ERROR,
1577  (errmsg("could not open collator for locale \"%s\": %s",
1578  collcollate, u_errorName(status))));
1579 
1580  if (U_ICU_VERSION_MAJOR_NUM < 54)
1581  icu_set_collation_attributes(collator, collcollate);
1582 
1583  /* We will leak this string if we get an error below :-( */
1584  result.info.icu.locale = MemoryContextStrdup(TopMemoryContext,
1585  collcollate);
1586  result.info.icu.ucol = collator;
1587 #else /* not USE_ICU */
1588  /* could get here if a collation was created by a build with ICU */
1589  ereport(ERROR,
1590  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1591  errmsg("ICU is not supported in this build"), \
1592  errhint("You need to rebuild PostgreSQL using --with-icu.")));
1593 #endif /* not USE_ICU */
1594  }
1595 
1596  ReleaseSysCache(tp);
1597 
1598  /* We'll keep the pg_locale_t structures in TopMemoryContext */
1599  resultp = MemoryContextAlloc(TopMemoryContext, sizeof(*resultp));
1600  *resultp = result;
1601 
1602  cache_entry->locale = resultp;
1603  }
1604 
1605  return cache_entry->locale;
1606 }
1607 
1608 /*
1609  * Get provider-specific collation version string for the given collation from
1610  * the operating system/library.
1611  */
1612 static char *
1613 get_collation_actual_version(char collprovider, const char *collcollate)
1614 {
1615  char *collversion = NULL;
1616 
1617 #ifdef USE_ICU
1618  if (collprovider == COLLPROVIDER_ICU)
1619  {
1620  UCollator *collator;
1621  UErrorCode status;
1622  UVersionInfo versioninfo;
1623  char buf[U_MAX_VERSION_STRING_LENGTH];
1624 
1625  status = U_ZERO_ERROR;
1626  collator = ucol_open(collcollate, &status);
1627  if (U_FAILURE(status))
1628  ereport(ERROR,
1629  (errmsg("could not open collator for locale \"%s\": %s",
1630  collcollate, u_errorName(status))));
1631  ucol_getVersion(collator, versioninfo);
1632  ucol_close(collator);
1633 
1634  u_versionToString(versioninfo, buf);
1635  collversion = pstrdup(buf);
1636  }
1637  else
1638 #endif
1639  if (collprovider == COLLPROVIDER_LIBC &&
1640  pg_strcasecmp("C", collcollate) != 0 &&
1641  pg_strncasecmp("C.", collcollate, 2) != 0 &&
1642  pg_strcasecmp("POSIX", collcollate) != 0)
1643  {
1644 #if defined(__GLIBC__)
1645  /* Use the glibc version because we don't have anything better. */
1646  collversion = pstrdup(gnu_get_libc_version());
1647 #elif defined(LC_VERSION_MASK)
1648  locale_t loc;
1649 
1650  /* Look up FreeBSD collation version. */
1651  loc = newlocale(LC_COLLATE, collcollate, NULL);
1652  if (loc)
1653  {
1654  collversion =
1655  pstrdup(querylocale(LC_COLLATE_MASK | LC_VERSION_MASK, loc));
1656  freelocale(loc);
1657  }
1658  else
1659  ereport(ERROR,
1660  (errmsg("could not load locale \"%s\"", collcollate)));
1661 #elif defined(WIN32) && _WIN32_WINNT >= 0x0600
1662  /*
1663  * If we are targeting Windows Vista and above, we can ask for a name
1664  * given a collation name (earlier versions required a location code
1665  * that we don't have).
1666  */
1667  NLSVERSIONINFOEX version = {sizeof(NLSVERSIONINFOEX)};
1668  WCHAR wide_collcollate[LOCALE_NAME_MAX_LENGTH];
1669 
1670  MultiByteToWideChar(CP_ACP, 0, collcollate, -1, wide_collcollate,
1671  LOCALE_NAME_MAX_LENGTH);
1672  if (!GetNLSVersionEx(COMPARE_STRING, wide_collcollate, &version))
1673  {
1674  /*
1675  * GetNLSVersionEx() wants a language tag such as "en-US", not a
1676  * locale name like "English_United States.1252". Until those
1677  * values can be prevented from entering the system, or 100%
1678  * reliably converted to the more useful tag format, tolerate the
1679  * resulting error and report that we have no version data.
1680  */
1681  if (GetLastError() == ERROR_INVALID_PARAMETER)
1682  return NULL;
1683 
1684  ereport(ERROR,
1685  (errmsg("could not get collation version for locale \"%s\": error code %lu",
1686  collcollate,
1687  GetLastError())));
1688  }
1689  collversion = psprintf("%d.%d,%d.%d",
1690  (version.dwNLSVersion >> 8) & 0xFFFF,
1691  version.dwNLSVersion & 0xFF,
1692  (version.dwDefinedVersion >> 8) & 0xFFFF,
1693  version.dwDefinedVersion & 0xFF);
1694 #endif
1695  }
1696 
1697  return collversion;
1698 }
1699 
1700 /*
1701  * Get provider-specific collation version string for a given collation OID.
1702  * Return NULL if the provider doesn't support versions, or the collation is
1703  * unversioned (for example "C"). Unknown OIDs result in NULL if missing_ok is
1704  * true.
1705  */
1706 char *
1707 get_collation_version_for_oid(Oid oid, bool missing_ok)
1708 {
1709  HeapTuple tp;
1710  char *version;
1711 
1712  if (oid == DEFAULT_COLLATION_OID)
1713  {
1714  Form_pg_database dbform;
1715 
1717  if (!HeapTupleIsValid(tp))
1718  elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1719  dbform = (Form_pg_database) GETSTRUCT(tp);
1720  version = get_collation_actual_version(COLLPROVIDER_LIBC,
1721  NameStr(dbform->datcollate));
1722  }
1723  else
1724  {
1725  Form_pg_collation collform;
1726 
1728  if (!HeapTupleIsValid(tp))
1729  {
1730  if (missing_ok)
1731  return NULL;
1732  elog(ERROR, "cache lookup failed for collation %u", oid);
1733  }
1734  collform = (Form_pg_collation) GETSTRUCT(tp);
1735  version = get_collation_actual_version(collform->collprovider,
1736  NameStr(collform->collcollate));
1737  }
1738 
1739  ReleaseSysCache(tp);
1740 
1741  return version;
1742 }
1743 
1744 #ifdef USE_ICU
1745 /*
1746  * Converter object for converting between ICU's UChar strings and C strings
1747  * in database encoding. Since the database encoding doesn't change, we only
1748  * need one of these per session.
1749  */
1750 static UConverter *icu_converter = NULL;
1751 
1752 static void
1753 init_icu_converter(void)
1754 {
1755  const char *icu_encoding_name;
1756  UErrorCode status;
1757  UConverter *conv;
1758 
1759  if (icu_converter)
1760  return; /* already done */
1761 
1762  icu_encoding_name = get_encoding_name_for_icu(GetDatabaseEncoding());
1763  if (!icu_encoding_name)
1764  ereport(ERROR,
1765  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1766  errmsg("encoding \"%s\" not supported by ICU",
1768 
1769  status = U_ZERO_ERROR;
1770  conv = ucnv_open(icu_encoding_name, &status);
1771  if (U_FAILURE(status))
1772  ereport(ERROR,
1773  (errmsg("could not open ICU converter for encoding \"%s\": %s",
1774  icu_encoding_name, u_errorName(status))));
1775 
1776  icu_converter = conv;
1777 }
1778 
1779 /*
1780  * Convert a string in the database encoding into a string of UChars.
1781  *
1782  * The source string at buff is of length nbytes
1783  * (it needn't be nul-terminated)
1784  *
1785  * *buff_uchar receives a pointer to the palloc'd result string, and
1786  * the function's result is the number of UChars generated.
1787  *
1788  * The result string is nul-terminated, though most callers rely on the
1789  * result length instead.
1790  */
1791 int32_t
1792 icu_to_uchar(UChar **buff_uchar, const char *buff, size_t nbytes)
1793 {
1794  UErrorCode status;
1795  int32_t len_uchar;
1796 
1797  init_icu_converter();
1798 
1799  status = U_ZERO_ERROR;
1800  len_uchar = ucnv_toUChars(icu_converter, NULL, 0,
1801  buff, nbytes, &status);
1802  if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
1803  ereport(ERROR,
1804  (errmsg("%s failed: %s", "ucnv_toUChars", u_errorName(status))));
1805 
1806  *buff_uchar = palloc((len_uchar + 1) * sizeof(**buff_uchar));
1807 
1808  status = U_ZERO_ERROR;
1809  len_uchar = ucnv_toUChars(icu_converter, *buff_uchar, len_uchar + 1,
1810  buff, nbytes, &status);
1811  if (U_FAILURE(status))
1812  ereport(ERROR,
1813  (errmsg("%s failed: %s", "ucnv_toUChars", u_errorName(status))));
1814 
1815  return len_uchar;
1816 }
1817 
1818 /*
1819  * Convert a string of UChars into the database encoding.
1820  *
1821  * The source string at buff_uchar is of length len_uchar
1822  * (it needn't be nul-terminated)
1823  *
1824  * *result receives a pointer to the palloc'd result string, and the
1825  * function's result is the number of bytes generated (not counting nul).
1826  *
1827  * The result string is nul-terminated.
1828  */
1829 int32_t
1830 icu_from_uchar(char **result, const UChar *buff_uchar, int32_t len_uchar)
1831 {
1832  UErrorCode status;
1833  int32_t len_result;
1834 
1835  init_icu_converter();
1836 
1837  status = U_ZERO_ERROR;
1838  len_result = ucnv_fromUChars(icu_converter, NULL, 0,
1839  buff_uchar, len_uchar, &status);
1840  if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
1841  ereport(ERROR,
1842  (errmsg("%s failed: %s", "ucnv_fromUChars",
1843  u_errorName(status))));
1844 
1845  *result = palloc(len_result + 1);
1846 
1847  status = U_ZERO_ERROR;
1848  len_result = ucnv_fromUChars(icu_converter, *result, len_result + 1,
1849  buff_uchar, len_uchar, &status);
1850  if (U_FAILURE(status))
1851  ereport(ERROR,
1852  (errmsg("%s failed: %s", "ucnv_fromUChars",
1853  u_errorName(status))));
1854 
1855  return len_result;
1856 }
1857 
1858 /*
1859  * Parse collation attributes and apply them to the open collator. This takes
1860  * a string like "und@colStrength=primary;colCaseLevel=yes" and parses and
1861  * applies the key-value arguments.
1862  *
1863  * Starting with ICU version 54, the attributes are processed automatically by
1864  * ucol_open(), so this is only necessary for emulating this behavior on older
1865  * versions.
1866  */
1868 static void
1869 icu_set_collation_attributes(UCollator *collator, const char *loc)
1870 {
1871  char *str = asc_tolower(loc, strlen(loc));
1872 
1873  str = strchr(str, '@');
1874  if (!str)
1875  return;
1876  str++;
1877 
1878  for (char *token = strtok(str, ";"); token; token = strtok(NULL, ";"))
1879  {
1880  char *e = strchr(token, '=');
1881 
1882  if (e)
1883  {
1884  char *name;
1885  char *value;
1886  UColAttribute uattr;
1887  UColAttributeValue uvalue;
1888  UErrorCode status;
1889 
1890  status = U_ZERO_ERROR;
1891 
1892  *e = '\0';
1893  name = token;
1894  value = e + 1;
1895 
1896  /*
1897  * See attribute name and value lists in ICU i18n/coll.cpp
1898  */
1899  if (strcmp(name, "colstrength") == 0)
1900  uattr = UCOL_STRENGTH;
1901  else if (strcmp(name, "colbackwards") == 0)
1902  uattr = UCOL_FRENCH_COLLATION;
1903  else if (strcmp(name, "colcaselevel") == 0)
1904  uattr = UCOL_CASE_LEVEL;
1905  else if (strcmp(name, "colcasefirst") == 0)
1906  uattr = UCOL_CASE_FIRST;
1907  else if (strcmp(name, "colalternate") == 0)
1908  uattr = UCOL_ALTERNATE_HANDLING;
1909  else if (strcmp(name, "colnormalization") == 0)
1910  uattr = UCOL_NORMALIZATION_MODE;
1911  else if (strcmp(name, "colnumeric") == 0)
1912  uattr = UCOL_NUMERIC_COLLATION;
1913  else
1914  /* ignore if unknown */
1915  continue;
1916 
1917  if (strcmp(value, "primary") == 0)
1918  uvalue = UCOL_PRIMARY;
1919  else if (strcmp(value, "secondary") == 0)
1920  uvalue = UCOL_SECONDARY;
1921  else if (strcmp(value, "tertiary") == 0)
1922  uvalue = UCOL_TERTIARY;
1923  else if (strcmp(value, "quaternary") == 0)
1924  uvalue = UCOL_QUATERNARY;
1925  else if (strcmp(value, "identical") == 0)
1926  uvalue = UCOL_IDENTICAL;
1927  else if (strcmp(value, "no") == 0)
1928  uvalue = UCOL_OFF;
1929  else if (strcmp(value, "yes") == 0)
1930  uvalue = UCOL_ON;
1931  else if (strcmp(value, "shifted") == 0)
1932  uvalue = UCOL_SHIFTED;
1933  else if (strcmp(value, "non-ignorable") == 0)
1934  uvalue = UCOL_NON_IGNORABLE;
1935  else if (strcmp(value, "lower") == 0)
1936  uvalue = UCOL_LOWER_FIRST;
1937  else if (strcmp(value, "upper") == 0)
1938  uvalue = UCOL_UPPER_FIRST;
1939  else
1940  status = U_ILLEGAL_ARGUMENT_ERROR;
1941 
1942  if (status == U_ZERO_ERROR)
1943  ucol_setAttribute(collator, uattr, uvalue, &status);
1944 
1945  /*
1946  * Pretend the error came from ucol_open(), for consistent error
1947  * message across ICU versions.
1948  */
1949  if (U_FAILURE(status))
1950  ereport(ERROR,
1951  (errmsg("could not open collator for locale \"%s\": %s",
1952  loc, u_errorName(status))));
1953  }
1954  }
1955 }
1956 
1957 #endif /* USE_ICU */
1958 
1959 /*
1960  * These functions convert from/to libc's wchar_t, *not* pg_wchar_t.
1961  * Therefore we keep them here rather than with the mbutils code.
1962  */
1963 
1964 /*
1965  * wchar2char --- convert wide characters to multibyte format
1966  *
1967  * This has the same API as the standard wcstombs_l() function; in particular,
1968  * tolen is the maximum number of bytes to store at *to, and *from must be
1969  * zero-terminated. The output will be zero-terminated iff there is room.
1970  */
1971 size_t
1972 wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
1973 {
1974  size_t result;
1975 
1976  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
1977 
1978  if (tolen == 0)
1979  return 0;
1980 
1981 #ifdef WIN32
1982 
1983  /*
1984  * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
1985  * for some reason mbstowcs and wcstombs won't do this for us, so we use
1986  * MultiByteToWideChar().
1987  */
1988  if (GetDatabaseEncoding() == PG_UTF8)
1989  {
1990  result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
1991  NULL, NULL);
1992  /* A zero return is failure */
1993  if (result <= 0)
1994  result = -1;
1995  else
1996  {
1997  Assert(result <= tolen);
1998  /* Microsoft counts the zero terminator in the result */
1999  result--;
2000  }
2001  }
2002  else
2003 #endif /* WIN32 */
2004  if (locale == (pg_locale_t) 0)
2005  {
2006  /* Use wcstombs directly for the default locale */
2007  result = wcstombs(to, from, tolen);
2008  }
2009  else
2010  {
2011 #ifdef HAVE_LOCALE_T
2012 #ifdef HAVE_WCSTOMBS_L
2013  /* Use wcstombs_l for nondefault locales */
2014  result = wcstombs_l(to, from, tolen, locale->info.lt);
2015 #else /* !HAVE_WCSTOMBS_L */
2016  /* We have to temporarily set the locale as current ... ugh */
2017  locale_t save_locale = uselocale(locale->info.lt);
2018 
2019  result = wcstombs(to, from, tolen);
2020 
2021  uselocale(save_locale);
2022 #endif /* HAVE_WCSTOMBS_L */
2023 #else /* !HAVE_LOCALE_T */
2024  /* Can't have locale != 0 without HAVE_LOCALE_T */
2025  elog(ERROR, "wcstombs_l is not available");
2026  result = 0; /* keep compiler quiet */
2027 #endif /* HAVE_LOCALE_T */
2028  }
2029 
2030  return result;
2031 }
2032 
2033 /*
2034  * char2wchar --- convert multibyte characters to wide characters
2035  *
2036  * This has almost the API of mbstowcs_l(), except that *from need not be
2037  * null-terminated; instead, the number of input bytes is specified as
2038  * fromlen. Also, we ereport() rather than returning -1 for invalid
2039  * input encoding. tolen is the maximum number of wchar_t's to store at *to.
2040  * The output will be zero-terminated iff there is room.
2041  */
2042 size_t
2043 char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen,
2045 {
2046  size_t result;
2047 
2048  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
2049 
2050  if (tolen == 0)
2051  return 0;
2052 
2053 #ifdef WIN32
2054  /* See WIN32 "Unicode" comment above */
2055  if (GetDatabaseEncoding() == PG_UTF8)
2056  {
2057  /* Win32 API does not work for zero-length input */
2058  if (fromlen == 0)
2059  result = 0;
2060  else
2061  {
2062  result = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
2063  /* A zero return is failure */
2064  if (result == 0)
2065  result = -1;
2066  }
2067 
2068  if (result != -1)
2069  {
2070  Assert(result < tolen);
2071  /* Append trailing null wchar (MultiByteToWideChar() does not) */
2072  to[result] = 0;
2073  }
2074  }
2075  else
2076 #endif /* WIN32 */
2077  {
2078  /* mbstowcs requires ending '\0' */
2079  char *str = pnstrdup(from, fromlen);
2080 
2081  if (locale == (pg_locale_t) 0)
2082  {
2083  /* Use mbstowcs directly for the default locale */
2084  result = mbstowcs(to, str, tolen);
2085  }
2086  else
2087  {
2088 #ifdef HAVE_LOCALE_T
2089 #ifdef HAVE_MBSTOWCS_L
2090  /* Use mbstowcs_l for nondefault locales */
2091  result = mbstowcs_l(to, str, tolen, locale->info.lt);
2092 #else /* !HAVE_MBSTOWCS_L */
2093  /* We have to temporarily set the locale as current ... ugh */
2094  locale_t save_locale = uselocale(locale->info.lt);
2095 
2096  result = mbstowcs(to, str, tolen);
2097 
2098  uselocale(save_locale);
2099 #endif /* HAVE_MBSTOWCS_L */
2100 #else /* !HAVE_LOCALE_T */
2101  /* Can't have locale != 0 without HAVE_LOCALE_T */
2102  elog(ERROR, "mbstowcs_l is not available");
2103  result = 0; /* keep compiler quiet */
2104 #endif /* HAVE_LOCALE_T */
2105  }
2106 
2107  pfree(str);
2108  }
2109 
2110  if (result == -1)
2111  {
2112  /*
2113  * Invalid multibyte character encountered. We try to give a useful
2114  * error message by letting pg_verifymbstr check the string. But it's
2115  * possible that the string is OK to us, and not OK to mbstowcs ---
2116  * this suggests that the LC_CTYPE locale is different from the
2117  * database encoding. Give a generic error message if pg_verifymbstr
2118  * can't find anything wrong.
2119  */
2120  pg_verifymbstr(from, fromlen, false); /* might not return */
2121  /* but if it does ... */
2122  ereport(ERROR,
2123  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
2124  errmsg("invalid multibyte character for locale"),
2125  errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
2126  }
2127 
2128  return result;
2129 }
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1114
bool flags_valid
Definition: pg_locale.c:115
int errhint(const char *fmt,...)
Definition: elog.c:1156
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1310
static bool CurrentLocaleConvValid
Definition: pg_locale.c:105
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
Oid collid
Definition: pg_locale.c:112
#define HASH_ELEM
Definition: hsearch.h:95
static struct @142 value
bool ctype_is_c
Definition: pg_locale.c:114
#define setlocale(a, b)
Definition: win32_port.h:436
#define DEBUG3
Definition: elog.h:23
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
char * pstrdup(const char *in)
Definition: mcxt.c:1299
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
union pg_locale_struct::@141 info
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:251
bool check_locale_time(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:318
Size entrysize
Definition: hsearch.h:76
int errcode(int sqlerrcode)
Definition: elog.c:698
#define wcstombs_l
Definition: win32_port.h:417
void assign_locale_numeric(const char *newval, void *extra)
Definition: pg_locale.c:312
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define lengthof(array)
Definition: c.h:734
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
char * locale_numeric
Definition: pg_locale.c:89
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
bool collate_is_c
Definition: pg_locale.c:113
void assign_locale_monetary(const char *newval, void *extra)
Definition: pg_locale.c:300
GucSource
Definition: guc.h:104
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:148
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:380
static struct pg_tm tm
Definition: localtime.c:102
char * get_collation_version_for_oid(Oid oid, bool missing_ok)
Definition: pg_locale.c:1707
#define NAMEDATALEN
void assign_locale_time(const char *newval, void *extra)
Definition: pg_locale.c:324
#define setenv(x, y, z)
Definition: win32_port.h:497
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
Definition: dynahash.c:219
void pfree(void *pointer)
Definition: mcxt.c:1169
#define MAX_L10N_DATA
Definition: pg_locale.c:83
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
static char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1613
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1326
#define FATAL
Definition: elog.h:49
char * localized_abbrev_months[12+1]
Definition: pg_locale.c:101
const char * get_encoding_name_for_icu(int encoding)
Definition: encnames.c:470
void check_strxfrm_bug(void)
Definition: pg_locale.c:1192
static char * buf
Definition: pg_test_fsync.c:68
int errdetail(const char *fmt,...)
Definition: elog.c:1042
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
char * localized_full_days[7+1]
Definition: pg_locale.c:100
#define locale_t
Definition: win32_port.h:391
MemoryContext TopMemoryContext
Definition: mcxt.c:48
size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
Definition: pg_locale.c:2043
#define WARNING
Definition: elog.h:40
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
pg_locale_t locale
Definition: pg_locale.c:116
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1473
#define HASH_BLOBS
Definition: hsearch.h:97
bool check_locale_numeric(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:306
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
bool deterministic
Definition: pg_locale.h:85
Oid MyDatabaseId
Definition: globals.c:88
Size keysize
Definition: hsearch.h:75
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:980
static HTAB * collation_cache
Definition: pg_locale.c:119
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:452
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1505
#define ereport(elevel,...)
Definition: elog.h:157
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
#define PG_CATCH()
Definition: elog.h:323
char * localized_full_months[12+1]
Definition: pg_locale.c:102
char * locale_messages
Definition: pg_locale.c:87
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
void cache_locale_time(void)
Definition: pg_locale.c:752
void assign_locale_messages(const char *newval, void *extra)
Definition: pg_locale.c:363
bool check_locale_messages(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:340
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:409
bool check_locale_monetary(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:294
static rewind_source * source
Definition: pg_rewind.c:79
pg_attribute_unused()
Definition: isn.c:63
struct lconv * PGLC_localeconv(void)
Definition: pg_locale.c:470
#define newval
static void cache_single_string(char **dst, const char *src, int encoding)
Definition: pg_locale.c:729
#define PG_RE_THROW()
Definition: elog.h:354
int32 encoding
Definition: pg_database.h:41
const char * name
Definition: encode.c:515
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
#define mbstowcs_l
Definition: win32_port.h:418
e
Definition: preproc-init.c:82
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1286
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
size_t wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
Definition: pg_locale.c:1972
#define elog(elevel,...)
Definition: elog.h:232
int i
#define NameStr(name)
Definition: c.h:681
static char * locale
Definition: initdb.c:126
char * localized_abbrev_days[7+1]
Definition: pg_locale.c:99
static char format
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1376
static collation_cache_entry * lookup_collation_cache(Oid collation, bool set_flags)
Definition: pg_locale.c:1262
Definition: pg_locale.c:110
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
#define PG_TRY()
Definition: elog.h:313
char * locale_monetary
Definition: pg_locale.c:88
char * asc_tolower(const char *buff, size_t nbytes)
Definition: formatting.c:2035
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:440
#define PG_END_TRY()
Definition: elog.h:338
char * locale_time
Definition: pg_locale.c:90
static bool CurrentLCTimeValid
Definition: pg_locale.c:106