LCOV - code coverage report
Current view: top level - src/backend/utils/adt - pg_locale.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 416 494 84.2 %
Date: 2024-11-21 08:14:44 Functions: 32 32 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14