LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - pg_locale.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: b45a8d7d8b306b43f31a002f1b3f1dddc8defeaf vs 8767b449a3a1e75626dfb08f24da54933171d4c5 Lines: 75.9 % 478 363 52 63 29 334 6 22
Current Date: 2025-10-28 08:26:42 +0900 Functions: 82.7 % 52 43 8 1 14 29
Baseline: lcov-20251028-005825-baseline Branches: 49.6 % 359 178 81 100 21 157
Baseline Date: 2025-10-27 06:37:35 +0000 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 27.9 % 68 19 49 19
(30,360] days: 87.7 % 65 57 3 5 10 47
(360..) days: 83.2 % 345 287 58 287
Function coverage date bins:
(7,30] days: 46.2 % 13 6 7 6
(30,360] days: 85.7 % 7 6 1 6
(360..) days: 96.9 % 32 31 1 2 29
Branch coverage date bins:
(7,30] days: 16.3 % 92 15 77 15
(30,360] days: 54.0 % 50 27 4 19 6 21
(360..) days: 62.7 % 217 136 81 136

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

Generated by: LCOV version 2.4-beta