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