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: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 73.7 % 521 384 74 63 50 334 4 26
Current Date: 2026-03-14 14:10:32 -0400 Functions: 81.8 % 55 45 9 1 17 28
Baseline: lcov-20260315-024220-baseline Branches: 47.7 % 417 199 118 100 42 157 12 22
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 45.5 % 143 65 74 4 49 16
(360..) days: 84.4 % 377 318 59 318
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 50.0 % 18 9 9 9
(360..) days: 97.2 % 36 35 1 7 28
Branch coverage date bins:
(30,360] days: 28.0 % 182 51 118 13 42 9
(360..) days: 63.0 % 235 148 87 148

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

Generated by: LCOV version 2.4-beta