LCOV - code coverage report
Current view: top level - src/backend/utils/adt - pg_locale.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 322 433 74.4 %
Date: 2020-11-27 12:05:55 Functions: 24 26 92.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13