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