PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_locale.c
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------
2  *
3  * PostgreSQL locale utilities
4  *
5  * Portions Copyright (c) 2002-2017, 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 the locales briefly when needed, cache the
26  * required information obtained from localeconv(), and set them back.
27  * The cached information is only used by the formatting functions
28  * (to_char, etc.) and the money type. For the user, this should all be
29  * transparent.
30  *
31  * !!! NOW HEAR THIS !!!
32  *
33  * We've been bitten repeatedly by this bug, so let's try to keep it in
34  * mind in future: on some platforms, the locale functions return pointers
35  * to static data that will be overwritten by any later locale function.
36  * Thus, for example, the obvious-looking sequence
37  * save = setlocale(category, NULL);
38  * if (!setlocale(category, value))
39  * fail = true;
40  * setlocale(category, save);
41  * DOES NOT WORK RELIABLY: on some platforms the second setlocale() call
42  * will change the memory save is pointing at. To do this sort of thing
43  * safely, you *must* pstrdup what setlocale returns the first time.
44  *
45  * FYI, The Open Group locale standard is defined here:
46  *
47  * http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html
48  *----------
49  */
50 
51 
52 #include "postgres.h"
53 
54 #include <time.h>
55 
56 #include "access/htup_details.h"
57 #include "catalog/pg_collation.h"
58 #include "catalog/pg_control.h"
59 #include "mb/pg_wchar.h"
60 #include "utils/builtins.h"
61 #include "utils/hsearch.h"
62 #include "utils/lsyscache.h"
63 #include "utils/memutils.h"
64 #include "utils/pg_locale.h"
65 #include "utils/syscache.h"
66 
67 #ifdef USE_ICU
68 #include <unicode/ucnv.h>
69 #endif
70 
71 #ifdef WIN32
72 /*
73  * This Windows file defines StrNCpy. We don't need it here, so we undefine
74  * it to keep the compiler quiet, and undefine it again after the file is
75  * included, so we don't accidentally use theirs.
76  */
77 #undef StrNCpy
78 #include <shlwapi.h>
79 #ifdef StrNCpy
80 #undef STrNCpy
81 #endif
82 #endif
83 
84 #define MAX_L10N_DATA 80
85 
86 
87 /* GUC settings */
92 
93 /* lc_time localization cache */
98 
99 /* indicates whether locale information cache is valid */
100 static bool CurrentLocaleConvValid = false;
101 static bool CurrentLCTimeValid = false;
102 
103 /* Environment variable storage area */
104 
105 #define LC_ENV_BUFSIZE (NAMEDATALEN + 20)
106 
109 
110 #ifdef LC_MESSAGES
111 static char lc_messages_envbuf[LC_ENV_BUFSIZE];
112 #endif
116 
117 /* Cache for collation-related knowledge */
118 
119 typedef struct
120 {
121  Oid collid; /* hash key: pg_collation OID */
122  bool collate_is_c; /* is collation's LC_COLLATE C? */
123  bool ctype_is_c; /* is collation's LC_CTYPE C? */
124  bool flags_valid; /* true if above flags are valid */
125  pg_locale_t locale; /* locale_t struct, or 0 if not valid */
127 
129 
130 
131 #if defined(WIN32) && defined(LC_MESSAGES)
132 static char *IsoLocaleName(const char *); /* MSVC specific */
133 #endif
134 
135 
136 /*
137  * pg_perm_setlocale
138  *
139  * This wraps the libc function setlocale(), with two additions. First, when
140  * changing LC_CTYPE, update gettext's encoding for the current message
141  * domain. GNU gettext automatically tracks LC_CTYPE on most platforms, but
142  * not on Windows. Second, if the operation is successful, the corresponding
143  * LC_XXX environment variable is set to match. By setting the environment
144  * variable, we ensure that any subsequent use of setlocale(..., "") will
145  * preserve the settings made through this routine. Of course, LC_ALL must
146  * also be unset to fully ensure that, but that has to be done elsewhere after
147  * all the individual LC_XXX variables have been set correctly. (Thank you
148  * Perl for making this kluge necessary.)
149  */
150 char *
151 pg_perm_setlocale(int category, const char *locale)
152 {
153  char *result;
154  const char *envvar;
155  char *envbuf;
156 
157 #ifndef WIN32
158  result = setlocale(category, locale);
159 #else
160 
161  /*
162  * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
163  * the given value is good and set it in the environment variables. We
164  * must ignore attempts to set to "", which means "keep using the old
165  * environment value".
166  */
167 #ifdef LC_MESSAGES
168  if (category == LC_MESSAGES)
169  {
170  result = (char *) locale;
171  if (locale == NULL || locale[0] == '\0')
172  return result;
173  }
174  else
175 #endif
176  result = setlocale(category, locale);
177 #endif /* WIN32 */
178 
179  if (result == NULL)
180  return result; /* fall out immediately on failure */
181 
182  /*
183  * Use the right encoding in translated messages. Under ENABLE_NLS, let
184  * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
185  * format strings are ASCII, but database-encoding strings may enter the
186  * message via %s. This makes the overall message encoding equal to the
187  * database encoding.
188  */
189  if (category == LC_CTYPE)
190  {
191  static char save_lc_ctype[LC_ENV_BUFSIZE];
192 
193  /* copy setlocale() return value before callee invokes it again */
194  strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
195  result = save_lc_ctype;
196 
197 #ifdef ENABLE_NLS
198  SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
199 #else
201 #endif
202  }
203 
204  switch (category)
205  {
206  case LC_COLLATE:
207  envvar = "LC_COLLATE";
208  envbuf = lc_collate_envbuf;
209  break;
210  case LC_CTYPE:
211  envvar = "LC_CTYPE";
212  envbuf = lc_ctype_envbuf;
213  break;
214 #ifdef LC_MESSAGES
215  case LC_MESSAGES:
216  envvar = "LC_MESSAGES";
217  envbuf = lc_messages_envbuf;
218 #ifdef WIN32
219  result = IsoLocaleName(locale);
220  if (result == NULL)
221  result = (char *) locale;
222 #endif /* WIN32 */
223  break;
224 #endif /* LC_MESSAGES */
225  case LC_MONETARY:
226  envvar = "LC_MONETARY";
227  envbuf = lc_monetary_envbuf;
228  break;
229  case LC_NUMERIC:
230  envvar = "LC_NUMERIC";
231  envbuf = lc_numeric_envbuf;
232  break;
233  case LC_TIME:
234  envvar = "LC_TIME";
235  envbuf = lc_time_envbuf;
236  break;
237  default:
238  elog(FATAL, "unrecognized LC category: %d", category);
239  envvar = NULL; /* keep compiler quiet */
240  envbuf = NULL;
241  return NULL;
242  }
243 
244  snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);
245 
246  if (putenv(envbuf))
247  return NULL;
248 
249  return result;
250 }
251 
252 
253 /*
254  * Is the locale name valid for the locale category?
255  *
256  * If successful, and canonname isn't NULL, a palloc'd copy of the locale's
257  * canonical name is stored there. This is especially useful for figuring out
258  * what locale name "" means (ie, the server environment value). (Actually,
259  * it seems that on most implementations that's the only thing it's good for;
260  * we could wish that setlocale gave back a canonically spelled version of
261  * the locale name, but typically it doesn't.)
262  */
263 bool
264 check_locale(int category, const char *locale, char **canonname)
265 {
266  char *save;
267  char *res;
268 
269  if (canonname)
270  *canonname = NULL; /* in case of failure */
271 
272  save = setlocale(category, NULL);
273  if (!save)
274  return false; /* won't happen, we hope */
275 
276  /* save may be pointing at a modifiable scratch variable, see above. */
277  save = pstrdup(save);
278 
279  /* set the locale with setlocale, to see if it accepts it. */
280  res = setlocale(category, locale);
281 
282  /* save canonical name if requested. */
283  if (res && canonname)
284  *canonname = pstrdup(res);
285 
286  /* restore old value. */
287  if (!setlocale(category, save))
288  elog(WARNING, "failed to restore old locale \"%s\"", save);
289  pfree(save);
290 
291  return (res != NULL);
292 }
293 
294 
295 /*
296  * GUC check/assign hooks
297  *
298  * For most locale categories, the assign hook doesn't actually set the locale
299  * permanently, just reset flags so that the next use will cache the
300  * appropriate values. (See explanation at the top of this file.)
301  *
302  * Note: we accept value = "" as selecting the postmaster's environment
303  * value, whatever it was (so long as the environment setting is legal).
304  * This will have been locked down by an earlier call to pg_perm_setlocale.
305  */
306 bool
307 check_locale_monetary(char **newval, void **extra, GucSource source)
308 {
309  return check_locale(LC_MONETARY, *newval, NULL);
310 }
311 
312 void
313 assign_locale_monetary(const char *newval, void *extra)
314 {
315  CurrentLocaleConvValid = false;
316 }
317 
318 bool
319 check_locale_numeric(char **newval, void **extra, GucSource source)
320 {
321  return check_locale(LC_NUMERIC, *newval, NULL);
322 }
323 
324 void
325 assign_locale_numeric(const char *newval, void *extra)
326 {
327  CurrentLocaleConvValid = false;
328 }
329 
330 bool
331 check_locale_time(char **newval, void **extra, GucSource source)
332 {
333  return check_locale(LC_TIME, *newval, NULL);
334 }
335 
336 void
337 assign_locale_time(const char *newval, void *extra)
338 {
339  CurrentLCTimeValid = false;
340 }
341 
342 /*
343  * We allow LC_MESSAGES to actually be set globally.
344  *
345  * Note: we normally disallow value = "" because it wouldn't have consistent
346  * semantics (it'd effectively just use the previous value). However, this
347  * is the value passed for PGC_S_DEFAULT, so don't complain in that case,
348  * not even if the attempted setting fails due to invalid environment value.
349  * The idea there is just to accept the environment setting *if possible*
350  * during startup, until we can read the proper value from postgresql.conf.
351  */
352 bool
353 check_locale_messages(char **newval, void **extra, GucSource source)
354 {
355  if (**newval == '\0')
356  {
357  if (source == PGC_S_DEFAULT)
358  return true;
359  else
360  return false;
361  }
362 
363  /*
364  * LC_MESSAGES category does not exist everywhere, but accept it anyway
365  *
366  * On Windows, we can't even check the value, so accept blindly
367  */
368 #if defined(LC_MESSAGES) && !defined(WIN32)
369  return check_locale(LC_MESSAGES, *newval, NULL);
370 #else
371  return true;
372 #endif
373 }
374 
375 void
376 assign_locale_messages(const char *newval, void *extra)
377 {
378  /*
379  * LC_MESSAGES category does not exist everywhere, but accept it anyway.
380  * We ignore failure, as per comment above.
381  */
382 #ifdef LC_MESSAGES
383  (void) pg_perm_setlocale(LC_MESSAGES, newval);
384 #endif
385 }
386 
387 
388 /*
389  * Frees the malloced content of a struct lconv. (But not the struct
390  * itself.) It's important that this not throw elog(ERROR).
391  */
392 static void
393 free_struct_lconv(struct lconv *s)
394 {
395  if (s->decimal_point)
396  free(s->decimal_point);
397  if (s->thousands_sep)
398  free(s->thousands_sep);
399  if (s->grouping)
400  free(s->grouping);
401  if (s->int_curr_symbol)
402  free(s->int_curr_symbol);
403  if (s->currency_symbol)
404  free(s->currency_symbol);
405  if (s->mon_decimal_point)
406  free(s->mon_decimal_point);
407  if (s->mon_thousands_sep)
408  free(s->mon_thousands_sep);
409  if (s->mon_grouping)
410  free(s->mon_grouping);
411  if (s->positive_sign)
412  free(s->positive_sign);
413  if (s->negative_sign)
414  free(s->negative_sign);
415 }
416 
417 /*
418  * Check that all fields of a struct lconv (or at least, the ones we care
419  * about) are non-NULL. The field list must match free_struct_lconv().
420  */
421 static bool
422 struct_lconv_is_valid(struct lconv *s)
423 {
424  if (s->decimal_point == NULL)
425  return false;
426  if (s->thousands_sep == NULL)
427  return false;
428  if (s->grouping == NULL)
429  return false;
430  if (s->int_curr_symbol == NULL)
431  return false;
432  if (s->currency_symbol == NULL)
433  return false;
434  if (s->mon_decimal_point == NULL)
435  return false;
436  if (s->mon_thousands_sep == NULL)
437  return false;
438  if (s->mon_grouping == NULL)
439  return false;
440  if (s->positive_sign == NULL)
441  return false;
442  if (s->negative_sign == NULL)
443  return false;
444  return true;
445 }
446 
447 
448 /*
449  * Convert the strdup'd string at *str from the specified encoding to the
450  * database encoding.
451  */
452 static void
454 {
455  char *pstr;
456  char *mstr;
457 
458  /* convert the string to the database encoding */
459  pstr = pg_any_to_server(*str, strlen(*str), encoding);
460  if (pstr == *str)
461  return; /* no conversion happened */
462 
463  /* need it malloc'd not palloc'd */
464  mstr = strdup(pstr);
465  if (mstr == NULL)
466  ereport(ERROR,
467  (errcode(ERRCODE_OUT_OF_MEMORY),
468  errmsg("out of memory")));
469 
470  /* replace old string */
471  free(*str);
472  *str = mstr;
473 
474  pfree(pstr);
475 }
476 
477 
478 /*
479  * Return the POSIX lconv struct (contains number/money formatting
480  * information) with locale information for all categories.
481  */
482 struct lconv *
484 {
485  static struct lconv CurrentLocaleConv;
486  static bool CurrentLocaleConvAllocated = false;
487  struct lconv *extlconv;
488  struct lconv worklconv;
489  bool trouble = false;
490  char *save_lc_monetary;
491  char *save_lc_numeric;
492 #ifdef WIN32
493  char *save_lc_ctype;
494 #endif
495 
496  /* Did we do it already? */
498  return &CurrentLocaleConv;
499 
500  /* Free any already-allocated storage */
501  if (CurrentLocaleConvAllocated)
502  {
503  free_struct_lconv(&CurrentLocaleConv);
504  CurrentLocaleConvAllocated = false;
505  }
506 
507  /*
508  * This is tricky because we really don't want to risk throwing error
509  * while the locale is set to other than our usual settings. Therefore,
510  * the process is: collect the usual settings, set locale to special
511  * setting, copy relevant data into worklconv using strdup(), restore
512  * normal settings, convert data to desired encoding, and finally stash
513  * the collected data in CurrentLocaleConv. This makes it safe if we
514  * throw an error during encoding conversion or run out of memory anywhere
515  * in the process. All data pointed to by struct lconv members is
516  * allocated with strdup, to avoid premature elog(ERROR) and to allow
517  * using a single cleanup routine.
518  */
519  memset(&worklconv, 0, sizeof(worklconv));
520 
521  /* Save user's values of monetary and numeric locales */
522  save_lc_monetary = setlocale(LC_MONETARY, NULL);
523  if (save_lc_monetary)
524  save_lc_monetary = pstrdup(save_lc_monetary);
525 
526  save_lc_numeric = setlocale(LC_NUMERIC, NULL);
527  if (save_lc_numeric)
528  save_lc_numeric = pstrdup(save_lc_numeric);
529 
530 #ifdef WIN32
531 
532  /*
533  * Ideally, monetary and numeric local symbols could be returned in any
534  * server encoding. Unfortunately, the WIN32 API does not allow
535  * setlocale() to return values in a codepage/CTYPE that uses more than
536  * two bytes per character, such as UTF-8:
537  *
538  * http://msdn.microsoft.com/en-us/library/x99tb11d.aspx
539  *
540  * Evidently, LC_CTYPE allows us to control the encoding used for strings
541  * returned by localeconv(). The Open Group standard, mentioned at the
542  * top of this C file, doesn't explicitly state this.
543  *
544  * Therefore, we set LC_CTYPE to match LC_NUMERIC or LC_MONETARY (which
545  * cannot be UTF8), call localeconv(), and then convert from the
546  * numeric/monetary LC_CTYPE to the server encoding. One example use of
547  * this is for the Euro symbol.
548  *
549  * Perhaps someday we will use GetLocaleInfoW() which returns values in
550  * UTF16 and convert from that.
551  */
552 
553  /* save user's value of ctype locale */
554  save_lc_ctype = setlocale(LC_CTYPE, NULL);
555  if (save_lc_ctype)
556  save_lc_ctype = pstrdup(save_lc_ctype);
557 
558  /* Here begins the critical section where we must not throw error */
559 
560  /* use numeric to set the ctype */
561  setlocale(LC_CTYPE, locale_numeric);
562 #endif
563 
564  /* Get formatting information for numeric */
565  setlocale(LC_NUMERIC, locale_numeric);
566  extlconv = localeconv();
567 
568  /* Must copy data now in case setlocale() overwrites it */
569  worklconv.decimal_point = strdup(extlconv->decimal_point);
570  worklconv.thousands_sep = strdup(extlconv->thousands_sep);
571  worklconv.grouping = strdup(extlconv->grouping);
572 
573 #ifdef WIN32
574  /* use monetary to set the ctype */
575  setlocale(LC_CTYPE, locale_monetary);
576 #endif
577 
578  /* Get formatting information for monetary */
579  setlocale(LC_MONETARY, locale_monetary);
580  extlconv = localeconv();
581 
582  /* Must copy data now in case setlocale() overwrites it */
583  worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
584  worklconv.currency_symbol = strdup(extlconv->currency_symbol);
585  worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
586  worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
587  worklconv.mon_grouping = strdup(extlconv->mon_grouping);
588  worklconv.positive_sign = strdup(extlconv->positive_sign);
589  worklconv.negative_sign = strdup(extlconv->negative_sign);
590  /* Copy scalar fields as well */
591  worklconv.int_frac_digits = extlconv->int_frac_digits;
592  worklconv.frac_digits = extlconv->frac_digits;
593  worklconv.p_cs_precedes = extlconv->p_cs_precedes;
594  worklconv.p_sep_by_space = extlconv->p_sep_by_space;
595  worklconv.n_cs_precedes = extlconv->n_cs_precedes;
596  worklconv.n_sep_by_space = extlconv->n_sep_by_space;
597  worklconv.p_sign_posn = extlconv->p_sign_posn;
598  worklconv.n_sign_posn = extlconv->n_sign_posn;
599 
600  /* Try to restore internal settings */
601  if (save_lc_monetary)
602  {
603  if (!setlocale(LC_MONETARY, save_lc_monetary))
604  trouble = true;
605  }
606 
607  if (save_lc_numeric)
608  {
609  if (!setlocale(LC_NUMERIC, save_lc_numeric))
610  trouble = true;
611  }
612 
613 #ifdef WIN32
614  /* Try to restore internal ctype settings */
615  if (save_lc_ctype)
616  {
617  if (!setlocale(LC_CTYPE, save_lc_ctype))
618  trouble = true;
619  }
620 #endif
621 
622  /*
623  * At this point we've done our best to clean up, and can call functions
624  * that might possibly throw errors with a clean conscience. But let's
625  * make sure we don't leak any already-strdup'd fields in worklconv.
626  */
627  PG_TRY();
628  {
629  int encoding;
630 
631  /*
632  * Report it if we failed to restore anything. Perhaps this should be
633  * FATAL, rather than continuing with bad locale settings?
634  */
635  if (trouble)
636  elog(WARNING, "failed to restore old locale");
637 
638  /* Release the pstrdup'd locale names */
639  if (save_lc_monetary)
640  pfree(save_lc_monetary);
641  if (save_lc_numeric)
642  pfree(save_lc_numeric);
643 #ifdef WIN32
644  if (save_lc_ctype)
645  pfree(save_lc_ctype);
646 #endif
647 
648  /* If any of the preceding strdup calls failed, complain now. */
649  if (!struct_lconv_is_valid(&worklconv))
650  ereport(ERROR,
651  (errcode(ERRCODE_OUT_OF_MEMORY),
652  errmsg("out of memory")));
653 
654  /*
655  * Now we must perform encoding conversion from whatever's associated
656  * with the locale into the database encoding.
657  */
659 
660  db_encoding_convert(encoding, &worklconv.decimal_point);
661  db_encoding_convert(encoding, &worklconv.thousands_sep);
662  /* grouping is not text and does not require conversion */
663 
665 
666  db_encoding_convert(encoding, &worklconv.int_curr_symbol);
667  db_encoding_convert(encoding, &worklconv.currency_symbol);
668  db_encoding_convert(encoding, &worklconv.mon_decimal_point);
669  db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
670  /* mon_grouping is not text and does not require conversion */
671  db_encoding_convert(encoding, &worklconv.positive_sign);
672  db_encoding_convert(encoding, &worklconv.negative_sign);
673  }
674  PG_CATCH();
675  {
676  free_struct_lconv(&worklconv);
677  PG_RE_THROW();
678  }
679  PG_END_TRY();
680 
681  /*
682  * Everything is good, so save the results.
683  */
684  CurrentLocaleConv = worklconv;
685  CurrentLocaleConvAllocated = true;
686  CurrentLocaleConvValid = true;
687  return &CurrentLocaleConv;
688 }
689 
690 #ifdef WIN32
691 /*
692  * On WIN32, strftime() returns the encoding in CP_ACP (the default
693  * operating system codpage for that computer), which is likely different
694  * from SERVER_ENCODING. This is especially important in Japanese versions
695  * of Windows which will use SJIS encoding, which we don't support as a
696  * server encoding.
697  *
698  * So, instead of using strftime(), use wcsftime() to return the value in
699  * wide characters (internally UTF16) and then convert it to the appropriate
700  * database encoding.
701  *
702  * Note that this only affects the calls to strftime() in this file, which are
703  * used to get the locale-aware strings. Other parts of the backend use
704  * pg_strftime(), which isn't locale-aware and does not need to be replaced.
705  */
706 static size_t
707 strftime_win32(char *dst, size_t dstlen,
708  const char *format, const struct tm *tm)
709 {
710  size_t len;
711  wchar_t wformat[8]; /* formats used below need 3 bytes */
712  wchar_t wbuf[MAX_L10N_DATA];
713 
714  /* get a wchar_t version of the format string */
715  len = MultiByteToWideChar(CP_UTF8, 0, format, -1,
716  wformat, lengthof(wformat));
717  if (len == 0)
718  elog(ERROR, "could not convert format string from UTF-8: error code %lu",
719  GetLastError());
720 
721  len = wcsftime(wbuf, MAX_L10N_DATA, wformat, tm);
722  if (len == 0)
723  {
724  /*
725  * strftime failed, possibly because the result would not fit in
726  * MAX_L10N_DATA. Return 0 with the contents of dst unspecified.
727  */
728  return 0;
729  }
730 
731  len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen - 1,
732  NULL, NULL);
733  if (len == 0)
734  elog(ERROR, "could not convert string to UTF-8: error code %lu",
735  GetLastError());
736 
737  dst[len] = '\0';
738  if (GetDatabaseEncoding() != PG_UTF8)
739  {
740  char *convstr = pg_any_to_server(dst, len, PG_UTF8);
741 
742  if (convstr != dst)
743  {
744  strlcpy(dst, convstr, dstlen);
745  len = strlen(dst);
746  pfree(convstr);
747  }
748  }
749 
750  return len;
751 }
752 
753 /* redefine strftime() */
754 #define strftime(a,b,c,d) strftime_win32(a,b,c,d)
755 #endif /* WIN32 */
756 
757 /* Subroutine for cache_locale_time(). */
758 static void
759 cache_single_time(char **dst, const char *format, const struct tm *tm)
760 {
761  char buf[MAX_L10N_DATA];
762  char *ptr;
763 
764  /*
765  * MAX_L10N_DATA is sufficient buffer space for every known locale, and
766  * POSIX defines no strftime() errors. (Buffer space exhaustion is not an
767  * error.) An implementation might report errors (e.g. ENOMEM) by
768  * returning 0 (or, less plausibly, a negative value) and setting errno.
769  * Report errno just in case the implementation did that, but clear it in
770  * advance of the call so we don't emit a stale, unrelated errno.
771  */
772  errno = 0;
773  if (strftime(buf, MAX_L10N_DATA, format, tm) <= 0)
774  elog(ERROR, "strftime(%s) failed: %m", format);
775 
777  if (*dst)
778  pfree(*dst);
779  *dst = ptr;
780 }
781 
782 /*
783  * Update the lc_time localization cache variables if needed.
784  */
785 void
787 {
788  char *save_lc_time;
789  time_t timenow;
790  struct tm *timeinfo;
791  int i;
792 
793 #ifdef WIN32
794  char *save_lc_ctype;
795 #endif
796 
797  /* did we do this already? */
798  if (CurrentLCTimeValid)
799  return;
800 
801  elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
802 
803  /* save user's value of time locale */
804  save_lc_time = setlocale(LC_TIME, NULL);
805  if (save_lc_time)
806  save_lc_time = pstrdup(save_lc_time);
807 
808 #ifdef WIN32
809 
810  /*
811  * On WIN32, there is no way to get locale-specific time values in a
812  * specified locale, like we do for monetary/numeric. We can only get
813  * CP_ACP (see strftime_win32) or UTF16. Therefore, we get UTF16 and
814  * convert it to the database locale. However, wcsftime() internally uses
815  * LC_CTYPE, so we set it here. See the WIN32 comment near the top of
816  * PGLC_localeconv().
817  */
818 
819  /* save user's value of ctype locale */
820  save_lc_ctype = setlocale(LC_CTYPE, NULL);
821  if (save_lc_ctype)
822  save_lc_ctype = pstrdup(save_lc_ctype);
823 
824  /* use lc_time to set the ctype */
825  setlocale(LC_CTYPE, locale_time);
826 #endif
827 
828  setlocale(LC_TIME, locale_time);
829 
830  timenow = time(NULL);
831  timeinfo = localtime(&timenow);
832 
833  /* localized days */
834  for (i = 0; i < 7; i++)
835  {
836  timeinfo->tm_wday = i;
837  cache_single_time(&localized_abbrev_days[i], "%a", timeinfo);
838  cache_single_time(&localized_full_days[i], "%A", timeinfo);
839  }
840 
841  /* localized months */
842  for (i = 0; i < 12; i++)
843  {
844  timeinfo->tm_mon = i;
845  timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
846  cache_single_time(&localized_abbrev_months[i], "%b", timeinfo);
847  cache_single_time(&localized_full_months[i], "%B", timeinfo);
848  }
849 
850  /* try to restore internal settings */
851  if (save_lc_time)
852  {
853  if (!setlocale(LC_TIME, save_lc_time))
854  elog(WARNING, "failed to restore old locale");
855  pfree(save_lc_time);
856  }
857 
858 #ifdef WIN32
859  /* try to restore internal ctype settings */
860  if (save_lc_ctype)
861  {
862  if (!setlocale(LC_CTYPE, save_lc_ctype))
863  elog(WARNING, "failed to restore old locale");
864  pfree(save_lc_ctype);
865  }
866 #endif
867 
868  CurrentLCTimeValid = true;
869 }
870 
871 
872 #if defined(WIN32) && defined(LC_MESSAGES)
873 /*
874  * Convert a Windows setlocale() argument to a Unix-style one.
875  *
876  * Regardless of platform, we install message catalogs under a Unix-style
877  * LL[_CC][.ENCODING][@VARIANT] naming convention. Only LC_MESSAGES settings
878  * following that style will elicit localized interface strings.
879  *
880  * Before Visual Studio 2012 (msvcr110.dll), Windows setlocale() accepted "C"
881  * (but not "c") and strings of the form <Language>[_<Country>][.<CodePage>],
882  * case-insensitive. setlocale() returns the fully-qualified form; for
883  * example, setlocale("thaI") returns "Thai_Thailand.874". Internally,
884  * setlocale() and _create_locale() select a "locale identifier"[1] and store
885  * it in an undocumented _locale_t field. From that LCID, we can retrieve the
886  * ISO 639 language and the ISO 3166 country. Character encoding does not
887  * matter, because the server and client encodings govern that.
888  *
889  * Windows Vista introduced the "locale name" concept[2], closely following
890  * RFC 4646. Locale identifiers are now deprecated. Starting with Visual
891  * Studio 2012, setlocale() accepts locale names in addition to the strings it
892  * accepted historically. It does not standardize them; setlocale("Th-tH")
893  * returns "Th-tH". setlocale(category, "") still returns a traditional
894  * string. Furthermore, msvcr110.dll changed the undocumented _locale_t
895  * content to carry locale names instead of locale identifiers.
896  *
897  * MinGW headers declare _create_locale(), but msvcrt.dll lacks that symbol.
898  * IsoLocaleName() always fails in a MinGW-built postgres.exe, so only
899  * Unix-style values of the lc_messages GUC can elicit localized messages. In
900  * particular, every lc_messages setting that initdb can select automatically
901  * will yield only C-locale messages. XXX This could be fixed by running the
902  * fully-qualified locale name through a lookup table.
903  *
904  * This function returns a pointer to a static buffer bearing the converted
905  * name or NULL if conversion fails.
906  *
907  * [1] http://msdn.microsoft.com/en-us/library/windows/desktop/dd373763.aspx
908  * [2] http://msdn.microsoft.com/en-us/library/windows/desktop/dd373814.aspx
909  */
910 static char *
911 IsoLocaleName(const char *winlocname)
912 {
913 #if (_MSC_VER >= 1400) /* VC8.0 or later */
914  static char iso_lc_messages[32];
915  _locale_t loct = NULL;
916 
917  if (pg_strcasecmp("c", winlocname) == 0 ||
918  pg_strcasecmp("posix", winlocname) == 0)
919  {
920  strcpy(iso_lc_messages, "C");
921  return iso_lc_messages;
922  }
923 
924  loct = _create_locale(LC_CTYPE, winlocname);
925  if (loct != NULL)
926  {
927 #if (_MSC_VER >= 1700) /* Visual Studio 2012 or later */
928  size_t rc;
929  char *hyphen;
930 
931  /* Locale names use only ASCII, any conversion locale suffices. */
932  rc = wchar2char(iso_lc_messages, loct->locinfo->locale_name[LC_CTYPE],
933  sizeof(iso_lc_messages), NULL);
934  _free_locale(loct);
935  if (rc == -1 || rc == sizeof(iso_lc_messages))
936  return NULL;
937 
938  /*
939  * Since the message catalogs sit on a case-insensitive filesystem, we
940  * need not standardize letter case here. So long as we do not ship
941  * message catalogs for which it would matter, we also need not
942  * translate the script/variant portion, e.g. uz-Cyrl-UZ to
943  * uz_UZ@cyrillic. Simply replace the hyphen with an underscore.
944  *
945  * Note that the locale name can be less-specific than the value we
946  * would derive under earlier Visual Studio releases. For example,
947  * French_France.1252 yields just "fr". This does not affect any of
948  * the country-specific message catalogs available as of this writing
949  * (pt_BR, zh_CN, zh_TW).
950  */
951  hyphen = strchr(iso_lc_messages, '-');
952  if (hyphen)
953  *hyphen = '_';
954 #else
955  char isolang[32],
956  isocrty[32];
957  LCID lcid;
958 
959  lcid = loct->locinfo->lc_handle[LC_CTYPE];
960  if (lcid == 0)
961  lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
962  _free_locale(loct);
963 
964  if (!GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME, isolang, sizeof(isolang)))
965  return NULL;
966  if (!GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME, isocrty, sizeof(isocrty)))
967  return NULL;
968  snprintf(iso_lc_messages, sizeof(iso_lc_messages) - 1, "%s_%s", isolang, isocrty);
969 #endif
970  return iso_lc_messages;
971  }
972  return NULL;
973 #else
974  return NULL; /* Not supported on this version of msvc/mingw */
975 #endif /* _MSC_VER >= 1400 */
976 }
977 #endif /* WIN32 && LC_MESSAGES */
978 
979 
980 /*
981  * Detect aging strxfrm() implementations that, in a subset of locales, write
982  * past the specified buffer length. Affected users must update OS packages
983  * before using PostgreSQL 9.5 or later.
984  *
985  * Assume that the bug can come and go from one postmaster startup to another
986  * due to physical replication among diverse machines. Assume that the bug's
987  * presence will not change during the life of a particular postmaster. Given
988  * those assumptions, call this no less than once per postmaster startup per
989  * LC_COLLATE setting used. No known-affected system offers strxfrm_l(), so
990  * there is no need to consider pg_collation locales.
991  */
992 void
994 {
995  char buf[32];
996  const int canary = 0x7F;
997  bool ok = true;
998 
999  /*
1000  * Given a two-byte ASCII string and length limit 7, 8 or 9, Solaris 10
1001  * 05/08 returns 18 and modifies 10 bytes. It respects limits above or
1002  * below that range.
1003  *
1004  * The bug is present in Solaris 8 as well; it is absent in Solaris 10
1005  * 01/13 and Solaris 11.2. Affected locales include is_IS.ISO8859-1,
1006  * en_US.UTF-8, en_US.ISO8859-1, and ru_RU.KOI8-R. Unaffected locales
1007  * include de_DE.UTF-8, de_DE.ISO8859-1, zh_TW.UTF-8, and C.
1008  */
1009  buf[7] = canary;
1010  (void) strxfrm(buf, "ab", 7);
1011  if (buf[7] != canary)
1012  ok = false;
1013 
1014  /*
1015  * illumos bug #1594 was present in the source tree from 2010-10-11 to
1016  * 2012-02-01. Given an ASCII string of any length and length limit 1,
1017  * affected systems ignore the length limit and modify a number of bytes
1018  * one less than the return value. The problem inputs for this bug do not
1019  * overlap those for the Solaris bug, hence a distinct test.
1020  *
1021  * Affected systems include smartos-20110926T021612Z. Affected locales
1022  * include en_US.ISO8859-1 and en_US.UTF-8. Unaffected locales include C.
1023  */
1024  buf[1] = canary;
1025  (void) strxfrm(buf, "a", 1);
1026  if (buf[1] != canary)
1027  ok = false;
1028 
1029  if (!ok)
1030  ereport(ERROR,
1031  (errcode(ERRCODE_SYSTEM_ERROR),
1032  errmsg_internal("strxfrm(), in locale \"%s\", writes past the specified array length",
1033  setlocale(LC_COLLATE, NULL)),
1034  errhint("Apply system library package updates.")));
1035 }
1036 
1037 
1038 /*
1039  * Cache mechanism for collation information.
1040  *
1041  * We cache two flags: whether the collation's LC_COLLATE or LC_CTYPE is C
1042  * (or POSIX), so we can optimize a few code paths in various places.
1043  * For the built-in C and POSIX collations, we can know that without even
1044  * doing a cache lookup, but we want to support aliases for C/POSIX too.
1045  * For the "default" collation, there are separate static cache variables,
1046  * since consulting the pg_collation catalog doesn't tell us what we need.
1047  *
1048  * Also, if a pg_locale_t has been requested for a collation, we cache that
1049  * for the life of a backend.
1050  *
1051  * Note that some code relies on the flags not reporting false negatives
1052  * (that is, saying it's not C when it is). For example, char2wchar()
1053  * could fail if the locale is C, so str_tolower() shouldn't call it
1054  * in that case.
1055  *
1056  * Note that we currently lack any way to flush the cache. Since we don't
1057  * support ALTER COLLATION, this is OK. The worst case is that someone
1058  * drops a collation, and a useless cache entry hangs around in existing
1059  * backends.
1060  */
1061 
1062 static collation_cache_entry *
1063 lookup_collation_cache(Oid collation, bool set_flags)
1064 {
1065  collation_cache_entry *cache_entry;
1066  bool found;
1067 
1068  Assert(OidIsValid(collation));
1069  Assert(collation != DEFAULT_COLLATION_OID);
1070 
1071  if (collation_cache == NULL)
1072  {
1073  /* First time through, initialize the hash table */
1074  HASHCTL ctl;
1075 
1076  memset(&ctl, 0, sizeof(ctl));
1077  ctl.keysize = sizeof(Oid);
1078  ctl.entrysize = sizeof(collation_cache_entry);
1079  collation_cache = hash_create("Collation cache", 100, &ctl,
1080  HASH_ELEM | HASH_BLOBS);
1081  }
1082 
1083  cache_entry = hash_search(collation_cache, &collation, HASH_ENTER, &found);
1084  if (!found)
1085  {
1086  /*
1087  * Make sure cache entry is marked invalid, in case we fail before
1088  * setting things.
1089  */
1090  cache_entry->flags_valid = false;
1091  cache_entry->locale = 0;
1092  }
1093 
1094  if (set_flags && !cache_entry->flags_valid)
1095  {
1096  /* Attempt to set the flags */
1097  HeapTuple tp;
1098  Form_pg_collation collform;
1099  const char *collcollate;
1100  const char *collctype;
1101 
1102  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
1103  if (!HeapTupleIsValid(tp))
1104  elog(ERROR, "cache lookup failed for collation %u", collation);
1105  collform = (Form_pg_collation) GETSTRUCT(tp);
1106 
1107  collcollate = NameStr(collform->collcollate);
1108  collctype = NameStr(collform->collctype);
1109 
1110  cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) ||
1111  (strcmp(collcollate, "POSIX") == 0));
1112  cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) ||
1113  (strcmp(collctype, "POSIX") == 0));
1114 
1115  cache_entry->flags_valid = true;
1116 
1117  ReleaseSysCache(tp);
1118  }
1119 
1120  return cache_entry;
1121 }
1122 
1123 
1124 /*
1125  * Detect whether collation's LC_COLLATE property is C
1126  */
1127 bool
1129 {
1130  /*
1131  * If we're asked about "collation 0", return false, so that the code will
1132  * go into the non-C path and report that the collation is bogus.
1133  */
1134  if (!OidIsValid(collation))
1135  return false;
1136 
1137  /*
1138  * If we're asked about the default collation, we have to inquire of the C
1139  * library. Cache the result so we only have to compute it once.
1140  */
1141  if (collation == DEFAULT_COLLATION_OID)
1142  {
1143  static int result = -1;
1144  char *localeptr;
1145 
1146  if (result >= 0)
1147  return (bool) result;
1148  localeptr = setlocale(LC_COLLATE, NULL);
1149  if (!localeptr)
1150  elog(ERROR, "invalid LC_COLLATE setting");
1151 
1152  if (strcmp(localeptr, "C") == 0)
1153  result = true;
1154  else if (strcmp(localeptr, "POSIX") == 0)
1155  result = true;
1156  else
1157  result = false;
1158  return (bool) result;
1159  }
1160 
1161  /*
1162  * If we're asked about the built-in C/POSIX collations, we know that.
1163  */
1164  if (collation == C_COLLATION_OID ||
1165  collation == POSIX_COLLATION_OID)
1166  return true;
1167 
1168  /*
1169  * Otherwise, we have to consult pg_collation, but we cache that.
1170  */
1171  return (lookup_collation_cache(collation, true))->collate_is_c;
1172 }
1173 
1174 /*
1175  * Detect whether collation's LC_CTYPE property is C
1176  */
1177 bool
1178 lc_ctype_is_c(Oid collation)
1179 {
1180  /*
1181  * If we're asked about "collation 0", return false, so that the code will
1182  * go into the non-C path and report that the collation is bogus.
1183  */
1184  if (!OidIsValid(collation))
1185  return false;
1186 
1187  /*
1188  * If we're asked about the default collation, we have to inquire of the C
1189  * library. Cache the result so we only have to compute it once.
1190  */
1191  if (collation == DEFAULT_COLLATION_OID)
1192  {
1193  static int result = -1;
1194  char *localeptr;
1195 
1196  if (result >= 0)
1197  return (bool) result;
1198  localeptr = setlocale(LC_CTYPE, NULL);
1199  if (!localeptr)
1200  elog(ERROR, "invalid LC_CTYPE setting");
1201 
1202  if (strcmp(localeptr, "C") == 0)
1203  result = true;
1204  else if (strcmp(localeptr, "POSIX") == 0)
1205  result = true;
1206  else
1207  result = false;
1208  return (bool) result;
1209  }
1210 
1211  /*
1212  * If we're asked about the built-in C/POSIX collations, we know that.
1213  */
1214  if (collation == C_COLLATION_OID ||
1215  collation == POSIX_COLLATION_OID)
1216  return true;
1217 
1218  /*
1219  * Otherwise, we have to consult pg_collation, but we cache that.
1220  */
1221  return (lookup_collation_cache(collation, true))->ctype_is_c;
1222 }
1223 
1224 
1225 /* simple subroutine for reporting errors from newlocale() */
1226 #ifdef HAVE_LOCALE_T
1227 static void
1228 report_newlocale_failure(const char *localename)
1229 {
1230  /* copy errno in case one of the ereport auxiliary functions changes it */
1231  int save_errno = errno;
1232 
1233  /*
1234  * ENOENT means "no such locale", not "no such file", so clarify that
1235  * errno with an errdetail message.
1236  */
1237  ereport(ERROR,
1238  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1239  errmsg("could not create locale \"%s\": %m",
1240  localename),
1241  (save_errno == ENOENT ?
1242  errdetail("The operating system could not find any locale data for the locale name \"%s\".",
1243  localename) : 0)));
1244 }
1245 #endif /* HAVE_LOCALE_T */
1246 
1247 
1248 /*
1249  * Create a locale_t from a collation OID. Results are cached for the
1250  * lifetime of the backend. Thus, do not free the result with freelocale().
1251  *
1252  * As a special optimization, the default/database collation returns 0.
1253  * Callers should then revert to the non-locale_t-enabled code path.
1254  * In fact, they shouldn't call this function at all when they are dealing
1255  * with the default locale. That can save quite a bit in hotspots.
1256  * Also, callers should avoid calling this before going down a C/POSIX
1257  * fastpath, because such a fastpath should work even on platforms without
1258  * locale_t support in the C library.
1259  *
1260  * For simplicity, we always generate COLLATE + CTYPE even though we
1261  * might only need one of them. Since this is called only once per session,
1262  * it shouldn't cost much.
1263  */
1266 {
1267  collation_cache_entry *cache_entry;
1268 
1269  /* Callers must pass a valid OID */
1270  Assert(OidIsValid(collid));
1271 
1272  /* Return 0 for "default" collation, just in case caller forgets */
1273  if (collid == DEFAULT_COLLATION_OID)
1274  return (pg_locale_t) 0;
1275 
1276  cache_entry = lookup_collation_cache(collid, false);
1277 
1278  if (cache_entry->locale == 0)
1279  {
1280  /* We haven't computed this yet in this session, so do it */
1281  HeapTuple tp;
1282  Form_pg_collation collform;
1283  const char *collcollate;
1284  const char *collctype pg_attribute_unused();
1286  Datum collversion;
1287  bool isnull;
1288 
1289  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1290  if (!HeapTupleIsValid(tp))
1291  elog(ERROR, "cache lookup failed for collation %u", collid);
1292  collform = (Form_pg_collation) GETSTRUCT(tp);
1293 
1294  collcollate = NameStr(collform->collcollate);
1295  collctype = NameStr(collform->collctype);
1296 
1297  result = malloc(sizeof(*result));
1298  memset(result, 0, sizeof(*result));
1299  result->provider = collform->collprovider;
1300 
1301  if (collform->collprovider == COLLPROVIDER_LIBC)
1302  {
1303 #ifdef HAVE_LOCALE_T
1304  locale_t loc;
1305 
1306  if (strcmp(collcollate, collctype) == 0)
1307  {
1308  /* Normal case where they're the same */
1309 #ifndef WIN32
1310  loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate,
1311  NULL);
1312 #else
1313  loc = _create_locale(LC_ALL, collcollate);
1314 #endif
1315  if (!loc)
1316  report_newlocale_failure(collcollate);
1317  }
1318  else
1319  {
1320 #ifndef WIN32
1321  /* We need two newlocale() steps */
1322  locale_t loc1;
1323 
1324  loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL);
1325  if (!loc1)
1326  report_newlocale_failure(collcollate);
1327  loc = newlocale(LC_CTYPE_MASK, collctype, loc1);
1328  if (!loc)
1329  report_newlocale_failure(collctype);
1330 #else
1331 
1332  /*
1333  * XXX The _create_locale() API doesn't appear to support
1334  * this. Could perhaps be worked around by changing
1335  * pg_locale_t to contain two separate fields.
1336  */
1337  ereport(ERROR,
1338  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1339  errmsg("collations with different collate and ctype values are not supported on this platform")));
1340 #endif
1341  }
1342 
1343  result->info.lt = loc;
1344 #else /* not HAVE_LOCALE_T */
1345  /* platform that doesn't support locale_t */
1346  ereport(ERROR,
1347  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1348  errmsg("collation provider LIBC is not supported on this platform")));
1349 #endif /* not HAVE_LOCALE_T */
1350  }
1351  else if (collform->collprovider == COLLPROVIDER_ICU)
1352  {
1353 #ifdef USE_ICU
1354  UCollator *collator;
1355  UErrorCode status;
1356 
1357  status = U_ZERO_ERROR;
1358  collator = ucol_open(collcollate, &status);
1359  if (U_FAILURE(status))
1360  ereport(ERROR,
1361  (errmsg("could not open collator for locale \"%s\": %s",
1362  collcollate, u_errorName(status))));
1363 
1364  result->info.icu.locale = strdup(collcollate);
1365  result->info.icu.ucol = collator;
1366 #else /* not USE_ICU */
1367  /* could get here if a collation was created by a build with ICU */
1368  ereport(ERROR,
1369  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1370  errmsg("ICU is not supported in this build"), \
1371  errhint("You need to rebuild PostgreSQL using --with-icu.")));
1372 #endif /* not USE_ICU */
1373  }
1374 
1376  &isnull);
1377  if (!isnull)
1378  {
1379  char *actual_versionstr;
1380  char *collversionstr;
1381 
1382  actual_versionstr = get_collation_actual_version(collform->collprovider, collcollate);
1383  if (!actual_versionstr)
1384  {
1385  /*
1386  * This could happen when specifying a version in CREATE
1387  * COLLATION for a libc locale, or manually creating a mess in
1388  * the catalogs.
1389  */
1390  ereport(ERROR,
1391  (errmsg("collation \"%s\" has no actual version, but a version was specified",
1392  NameStr(collform->collname))));
1393  }
1394  collversionstr = TextDatumGetCString(collversion);
1395 
1396  if (strcmp(actual_versionstr, collversionstr) != 0)
1397  ereport(WARNING,
1398  (errmsg("collation \"%s\" has version mismatch",
1399  NameStr(collform->collname)),
1400  errdetail("The collation in the database was created using version %s, "
1401  "but the operating system provides version %s.",
1402  collversionstr, actual_versionstr),
1403  errhint("Rebuild all objects affected by this collation and run "
1404  "ALTER COLLATION %s REFRESH VERSION, "
1405  "or build PostgreSQL with the right library version.",
1406  quote_qualified_identifier(get_namespace_name(collform->collnamespace),
1407  NameStr(collform->collname)))));
1408  }
1409 
1410  ReleaseSysCache(tp);
1411 
1412  cache_entry->locale = result;
1413  }
1414 
1415  return cache_entry->locale;
1416 }
1417 
1418 /*
1419  * Get provider-specific collation version string for the given collation from
1420  * the operating system/library.
1421  *
1422  * A particular provider must always either return a non-NULL string or return
1423  * NULL (if it doesn't support versions). It must not return NULL for some
1424  * collcollate and not NULL for others.
1425  */
1426 char *
1427 get_collation_actual_version(char collprovider, const char *collcollate)
1428 {
1429  char *collversion;
1430 
1431 #ifdef USE_ICU
1432  if (collprovider == COLLPROVIDER_ICU)
1433  {
1434  UCollator *collator;
1435  UErrorCode status;
1436  UVersionInfo versioninfo;
1437  char buf[U_MAX_VERSION_STRING_LENGTH];
1438 
1439  status = U_ZERO_ERROR;
1440  collator = ucol_open(collcollate, &status);
1441  if (U_FAILURE(status))
1442  ereport(ERROR,
1443  (errmsg("could not open collator for locale \"%s\": %s",
1444  collcollate, u_errorName(status))));
1445  ucol_getVersion(collator, versioninfo);
1446  ucol_close(collator);
1447 
1448  u_versionToString(versioninfo, buf);
1449  collversion = pstrdup(buf);
1450  }
1451  else
1452 #endif
1453  collversion = NULL;
1454 
1455  return collversion;
1456 }
1457 
1458 
1459 #ifdef USE_ICU
1460 /*
1461  * Converter object for converting between ICU's UChar strings and C strings
1462  * in database encoding. Since the database encoding doesn't change, we only
1463  * need one of these per session.
1464  */
1465 static UConverter *icu_converter = NULL;
1466 
1467 static void
1468 init_icu_converter(void)
1469 {
1470  const char *icu_encoding_name;
1471  UErrorCode status;
1472  UConverter *conv;
1473 
1474  if (icu_converter)
1475  return;
1476 
1477  icu_encoding_name = get_encoding_name_for_icu(GetDatabaseEncoding());
1478 
1479  status = U_ZERO_ERROR;
1480  conv = ucnv_open(icu_encoding_name, &status);
1481  if (U_FAILURE(status))
1482  ereport(ERROR,
1483  (errmsg("could not open ICU converter for encoding \"%s\": %s",
1484  icu_encoding_name, u_errorName(status))));
1485 
1486  icu_converter = conv;
1487 }
1488 
1489 /*
1490  * Convert a string in the database encoding into a string of UChars.
1491  *
1492  * The source string at buff is of length nbytes
1493  * (it needn't be nul-terminated)
1494  *
1495  * *buff_uchar receives a pointer to the palloc'd result string, and
1496  * the function's result is the number of UChars generated.
1497  *
1498  * The result string is nul-terminated, though most callers rely on the
1499  * result length instead.
1500  */
1501 int32_t
1502 icu_to_uchar(UChar **buff_uchar, const char *buff, size_t nbytes)
1503 {
1504  UErrorCode status;
1505  int32_t len_uchar;
1506 
1507  init_icu_converter();
1508 
1509  len_uchar = 2 * nbytes + 1; /* max length per docs */
1510  *buff_uchar = palloc(len_uchar * sizeof(**buff_uchar));
1511  status = U_ZERO_ERROR;
1512  len_uchar = ucnv_toUChars(icu_converter, *buff_uchar, len_uchar,
1513  buff, nbytes, &status);
1514  if (U_FAILURE(status))
1515  ereport(ERROR,
1516  (errmsg("ucnv_toUChars failed: %s", u_errorName(status))));
1517  return len_uchar;
1518 }
1519 
1520 /*
1521  * Convert a string of UChars into the database encoding.
1522  *
1523  * The source string at buff_uchar is of length len_uchar
1524  * (it needn't be nul-terminated)
1525  *
1526  * *result receives a pointer to the palloc'd result string, and the
1527  * function's result is the number of bytes generated (not counting nul).
1528  *
1529  * The result string is nul-terminated.
1530  */
1531 int32_t
1532 icu_from_uchar(char **result, const UChar *buff_uchar, int32_t len_uchar)
1533 {
1534  UErrorCode status;
1535  int32_t len_result;
1536 
1537  init_icu_converter();
1538 
1539  len_result = UCNV_GET_MAX_BYTES_FOR_STRING(len_uchar, ucnv_getMaxCharSize(icu_converter));
1540  *result = palloc(len_result + 1);
1541  status = U_ZERO_ERROR;
1542  len_result = ucnv_fromUChars(icu_converter, *result, len_result + 1,
1543  buff_uchar, len_uchar, &status);
1544  if (U_FAILURE(status))
1545  ereport(ERROR,
1546  (errmsg("ucnv_fromUChars failed: %s", u_errorName(status))));
1547  return len_result;
1548 }
1549 #endif /* USE_ICU */
1550 
1551 /*
1552  * These functions convert from/to libc's wchar_t, *not* pg_wchar_t.
1553  * Therefore we keep them here rather than with the mbutils code.
1554  */
1555 
1556 #ifdef USE_WIDE_UPPER_LOWER
1557 
1558 /*
1559  * wchar2char --- convert wide characters to multibyte format
1560  *
1561  * This has the same API as the standard wcstombs_l() function; in particular,
1562  * tolen is the maximum number of bytes to store at *to, and *from must be
1563  * zero-terminated. The output will be zero-terminated iff there is room.
1564  */
1565 size_t
1566 wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
1567 {
1568  size_t result;
1569 
1570  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
1571 
1572  if (tolen == 0)
1573  return 0;
1574 
1575 #ifdef WIN32
1576 
1577  /*
1578  * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
1579  * for some reason mbstowcs and wcstombs won't do this for us, so we use
1580  * MultiByteToWideChar().
1581  */
1582  if (GetDatabaseEncoding() == PG_UTF8)
1583  {
1584  result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
1585  NULL, NULL);
1586  /* A zero return is failure */
1587  if (result <= 0)
1588  result = -1;
1589  else
1590  {
1591  Assert(result <= tolen);
1592  /* Microsoft counts the zero terminator in the result */
1593  result--;
1594  }
1595  }
1596  else
1597 #endif /* WIN32 */
1598  if (locale == (pg_locale_t) 0)
1599  {
1600  /* Use wcstombs directly for the default locale */
1601  result = wcstombs(to, from, tolen);
1602  }
1603  else
1604  {
1605 #ifdef HAVE_LOCALE_T
1606 #ifdef HAVE_WCSTOMBS_L
1607  /* Use wcstombs_l for nondefault locales */
1608  result = wcstombs_l(to, from, tolen, locale->info.lt);
1609 #else /* !HAVE_WCSTOMBS_L */
1610  /* We have to temporarily set the locale as current ... ugh */
1611  locale_t save_locale = uselocale(locale->info.lt);
1612 
1613  result = wcstombs(to, from, tolen);
1614 
1615  uselocale(save_locale);
1616 #endif /* HAVE_WCSTOMBS_L */
1617 #else /* !HAVE_LOCALE_T */
1618  /* Can't have locale != 0 without HAVE_LOCALE_T */
1619  elog(ERROR, "wcstombs_l is not available");
1620  result = 0; /* keep compiler quiet */
1621 #endif /* HAVE_LOCALE_T */
1622  }
1623 
1624  return result;
1625 }
1626 
1627 /*
1628  * char2wchar --- convert multibyte characters to wide characters
1629  *
1630  * This has almost the API of mbstowcs_l(), except that *from need not be
1631  * null-terminated; instead, the number of input bytes is specified as
1632  * fromlen. Also, we ereport() rather than returning -1 for invalid
1633  * input encoding. tolen is the maximum number of wchar_t's to store at *to.
1634  * The output will be zero-terminated iff there is room.
1635  */
1636 size_t
1637 char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen,
1638  pg_locale_t locale)
1639 {
1640  size_t result;
1641 
1642  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
1643 
1644  if (tolen == 0)
1645  return 0;
1646 
1647 #ifdef WIN32
1648  /* See WIN32 "Unicode" comment above */
1649  if (GetDatabaseEncoding() == PG_UTF8)
1650  {
1651  /* Win32 API does not work for zero-length input */
1652  if (fromlen == 0)
1653  result = 0;
1654  else
1655  {
1656  result = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
1657  /* A zero return is failure */
1658  if (result == 0)
1659  result = -1;
1660  }
1661 
1662  if (result != -1)
1663  {
1664  Assert(result < tolen);
1665  /* Append trailing null wchar (MultiByteToWideChar() does not) */
1666  to[result] = 0;
1667  }
1668  }
1669  else
1670 #endif /* WIN32 */
1671  {
1672  /* mbstowcs requires ending '\0' */
1673  char *str = pnstrdup(from, fromlen);
1674 
1675  if (locale == (pg_locale_t) 0)
1676  {
1677  /* Use mbstowcs directly for the default locale */
1678  result = mbstowcs(to, str, tolen);
1679  }
1680  else
1681  {
1682 #ifdef HAVE_LOCALE_T
1683 #ifdef HAVE_MBSTOWCS_L
1684  /* Use mbstowcs_l for nondefault locales */
1685  result = mbstowcs_l(to, str, tolen, locale->info.lt);
1686 #else /* !HAVE_MBSTOWCS_L */
1687  /* We have to temporarily set the locale as current ... ugh */
1688  locale_t save_locale = uselocale(locale->info.lt);
1689 
1690  result = mbstowcs(to, str, tolen);
1691 
1692  uselocale(save_locale);
1693 #endif /* HAVE_MBSTOWCS_L */
1694 #else /* !HAVE_LOCALE_T */
1695  /* Can't have locale != 0 without HAVE_LOCALE_T */
1696  elog(ERROR, "mbstowcs_l is not available");
1697  result = 0; /* keep compiler quiet */
1698 #endif /* HAVE_LOCALE_T */
1699  }
1700 
1701  pfree(str);
1702  }
1703 
1704  if (result == -1)
1705  {
1706  /*
1707  * Invalid multibyte character encountered. We try to give a useful
1708  * error message by letting pg_verifymbstr check the string. But it's
1709  * possible that the string is OK to us, and not OK to mbstowcs ---
1710  * this suggests that the LC_CTYPE locale is different from the
1711  * database encoding. Give a generic error message if verifymbstr
1712  * can't find anything wrong.
1713  */
1714  pg_verifymbstr(from, fromlen, false); /* might not return */
1715  /* but if it does ... */
1716  ereport(ERROR,
1717  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1718  errmsg("invalid multibyte character for locale"),
1719  errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
1720  }
1721 
1722  return result;
1723 }
1724 
1725 #endif /* USE_WIDE_UPPER_LOWER */
#define COLLPROVIDER_ICU
Definition: pg_collation.h:85
static char lc_numeric_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:114
#define Anum_pg_collation_collversion
Definition: pg_collation.h:66
void SetMessageEncoding(int encoding)
Definition: mbutils.c:919
bool flags_valid
Definition: pg_locale.c:124
int errhint(const char *fmt,...)
Definition: elog.c:987
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1088
static bool CurrentLocaleConvValid
Definition: pg_locale.c:100
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid collid
Definition: pg_locale.c:121
#define HASH_ELEM
Definition: hsearch.h:87
bool ctype_is_c
Definition: pg_locale.c:123
static char lc_monetary_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:113
#define locale_t
Definition: win32.h:323
#define DEBUG3
Definition: elog.h:23
#define wcstombs_l
Definition: win32.h:349
char * pstrdup(const char *in)
Definition: mcxt.c:1077
Datum timenow(PG_FUNCTION_ARGS)
Definition: nabstime.c:997
#define LC_ENV_BUFSIZE
Definition: pg_locale.c:105
union pg_locale_struct::@120 info
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:264
bool check_locale_time(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:331
Size entrysize
Definition: hsearch.h:73
int errcode(int sqlerrcode)
Definition: elog.c:575
void assign_locale_numeric(const char *newval, void *extra)
Definition: pg_locale.c:325
return result
Definition: formatting.c:1633
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define lengthof(array)
Definition: c.h:562
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
char * locale_numeric
Definition: pg_locale.c:90
unsigned int Oid
Definition: postgres_ext.h:31
char * localized_abbrev_months[12]
Definition: pg_locale.c:96
#define putenv(x)
Definition: win32.h:411
#define OidIsValid(objectId)
Definition: c.h:538
bool collate_is_c
Definition: pg_locale.c:122
void assign_locale_monetary(const char *newval, void *extra)
Definition: pg_locale.c:313
static char lc_time_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:115
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
GucSource
Definition: guc.h:105
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:151
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:393
static struct pg_tm tm
Definition: localtime.c:111
#define malloc(a)
Definition: header.h:50
#define mbstowcs_l
Definition: win32.h:350
void assign_locale_time(const char *newval, void *extra)
Definition: pg_locale.c:337
Definition: dynahash.c:193
void pfree(void *pointer)
Definition: mcxt.c:950
#define MAX_L10N_DATA
Definition: pg_locale.c:84
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
bool lc_collate_is_c(Oid collation)
Definition: pg_locale.c:1128
#define FATAL
Definition: elog.h:52
const char * get_encoding_name_for_icu(int encoding)
Definition: encnames.c:461
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
void check_strxfrm_bug(void)
Definition: pg_locale.c:993
static char * buf
Definition: pg_test_fsync.c:66
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static char lc_ctype_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:108
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10366
#define WARNING
Definition: elog.h:40
pg_locale_t locale
Definition: pg_locale.c:125
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1265
#define HASH_BLOBS
Definition: hsearch.h:88
#define TextDatumGetCString(d)
Definition: builtins.h:92
bool check_locale_numeric(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:319
uintptr_t Datum
Definition: postgres.h:372
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
Size keysize
Definition: hsearch.h:72
static HTAB * collation_cache
Definition: pg_locale.c:128
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:433
char * localized_full_days[7]
Definition: pg_locale.c:95
static char * encoding
Definition: initdb.c:122
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define PG_CATCH()
Definition: elog.h:293
char * locale_messages
Definition: pg_locale.c:88
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void cache_locale_time(void)
Definition: pg_locale.c:786
void assign_locale_messages(const char *newval, void *extra)
Definition: pg_locale.c:376
bool check_locale_messages(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:353
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:422
bool check_locale_monetary(char **newval, void **extra, GucSource source)
Definition: pg_locale.c:307
struct lconv * PGLC_localeconv(void)
Definition: pg_locale.c:483
#define newval
#define PG_RE_THROW()
Definition: elog.h:314
#define C_COLLATION_OID
Definition: pg_collation.h:78
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:52
#define POSIX_COLLATION_OID
Definition: pg_collation.h:81
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1064
int i
#define NameStr(name)
Definition: c.h:499
static char * locale
Definition: initdb.c:123
int int32_t
Definition: crypt.c:108
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
static char format
Definition: pg_basebackup.c:80
#define elog
Definition: elog.h:219
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1178
static collation_cache_entry * lookup_collation_cache(Oid collation, bool set_flags)
Definition: pg_locale.c:1063
Definition: pg_locale.c:119
char * localized_full_months[12]
Definition: pg_locale.c:97
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:572
#define PG_TRY()
Definition: elog.h:284
static char lc_collate_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:107
pg_attribute_unused()
Definition: pg_regress.c:881
char * locale_monetary
Definition: pg_locale.c:89
char * localized_abbrev_days[7]
Definition: pg_locale.c:94
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:453
#define PG_END_TRY()
Definition: elog.h:300
static void cache_single_time(char **dst, const char *format, const struct tm *tm)
Definition: pg_locale.c:759
#define COLLPROVIDER_LIBC
Definition: pg_collation.h:86
char * locale_time
Definition: pg_locale.c:91
static bool CurrentLCTimeValid
Definition: pg_locale.c:101
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1427