Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | /* * Copyright (c) 2005, 2008 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include "xlocale_private.h" #include <assert.h> #include <errno.h> #include <stddef.h> #include <string.h> #include "collate.h" #include "ldpart.h" #include "lmonetary.h" #include "lnumeric.h" #include "mblocal.h" extern struct xlocale_collate __xlocale_C_collate; #ifndef nitems #define nitems(x) (sizeof((x)) / sizeof((x)[0])) #endif #define NMBSTATET 10 #define C_LOCALE_INITIALIZER { \ { 1, NULL }, \ {}, {}, {}, {}, {}, \ {}, {}, {}, {}, {}, \ OS_UNFAIR_LOCK_INIT, \ XMAGIC, \ 0, 0, 0, 0, 1, 1, 0, \ { \ [XLC_COLLATE] = (void *)&__xlocale_C_collate, \ [XLC_CTYPE] = (void *)&_DefaultRuneXLocale, \ }, \ } #define LDPART_BUFFER(x) \ (((struct xlocale_ldpart *)(x))->buffer) static char C[] = "C"; static struct _xlocale __c_locale = C_LOCALE_INITIALIZER; const locale_t _c_locale = (const locale_t)&__c_locale; struct _xlocale __global_locale = C_LOCALE_INITIALIZER; pthread_key_t __locale_key = (pthread_key_t)-1; extern int __collate_load_tables(const char *, locale_t); extern int __detect_path_locale(void); extern const char *__get_locale_env(int); extern int __messages_load_locale(const char *, locale_t); extern int __monetary_load_locale(const char *, locale_t); extern int __numeric_load_locale(const char *, locale_t); extern int __setrunelocale(const char *, locale_t); extern int __time_load_locale(const char *, locale_t); static void destruct_locale(void *v); /* * check that the encoding is the right size, isn't . or .. and doesn't * contain any slashes */ static inline __attribute__((always_inline)) int _checkencoding(const char *encoding) { return (encoding && (strlen(encoding) > ENCODING_LEN || (encoding[0] == '.' && (encoding[1] == 0 || (encoding[1] == '.' && encoding[2] == 0))) || strchr(encoding, '/') != NULL)) ? -1 : 0; } /* * check that the locale has the right magic number */ static inline __attribute__((always_inline)) int _checklocale(const locale_t loc) { if (!loc) return 0; return (loc == LC_GLOBAL_LOCALE || loc->__magic == XMAGIC) ? 0 : -1; } /* * copy a locale_t except anything before the magic value */ static inline __attribute__((always_inline)) void _copylocale(locale_t dst, const locale_t src) { memcpy(&dst->__magic, &src->__magic, sizeof(*dst) - offsetof(struct _xlocale, __magic)); } /* * Make a copy of a locale_t, locking/unlocking the source. * A NULL locale_t means to make a copy of the current * locale while LC_GLOBAL_LOCALE means to copy the global locale. If * &__c_locale is passed (meaning a C locale is desired), just make * a copy. */ static locale_t _duplocale(locale_t loc) { locale_t new; if ((new = (locale_t)malloc(sizeof(struct _xlocale))) == NULL) return NULL; new->header.retain_count = 1; new->header.destructor = destruct_locale; new->__lock = OS_UNFAIR_LOCK_INIT; if (loc == NULL) loc = __current_locale(); else if (loc == LC_GLOBAL_LOCALE) loc = &__global_locale; else if (loc == &__c_locale) { *new = __c_locale; new->header.retain_count = 1; new->header.destructor = destruct_locale; new->__lock = OS_UNFAIR_LOCK_INIT; return new; } XL_LOCK(loc); _copylocale(new, loc); XL_UNLOCK(loc); /* __mbs_mblen is the first of NMBSTATET mbstate_t buffers */ bzero(&new->__mbs_mblen, offsetof(struct _xlocale, __magic) - offsetof(struct _xlocale, __mbs_mblen)); for (int i = XLC_COLLATE; i < XLC_LAST; i++) { xlocale_retain(new->components[i]); } xlocale_retain(new->__lc_numeric_loc); return new; } /* * Modify a locale_t, setting the parts specified in the mask * to the locale specified by the string. If the string is NULL, the C * locale is used. If the string is empty, the value is determined from * the environment. -1 is returned on error, and loc is in a partial state. */ static int _modifylocale(locale_t loc, int mask, __const char *locale) { int m, ret; const char *enc = NULL; char *oenc; if (!locale) locale = C; ret = __detect_path_locale(); if (ret) { errno = ret; return -1; } if (*locale) enc = locale; for(m = 1; m <= _LC_LAST_MASK; m <<= 1) { if (m & mask) { switch(m) { case LC_COLLATE_MASK: if (!*locale) { enc = __get_locale_env(LC_COLLATE); if (_checkencoding(enc) < 0) { errno = EINVAL; return -1; } } oenc = (XLOCALE_COLLATE(loc)->__collate_load_error ? C : loc->components[XLC_COLLATE]->locale); if (strcmp(enc, oenc) != 0 && __collate_load_tables(enc, loc) == _LDP_ERROR) return -1; xlocale_fill_name(loc->components[XLC_COLLATE], enc); break; case LC_CTYPE_MASK: if (!*locale) { enc = __get_locale_env(LC_CTYPE); if (_checkencoding(enc) < 0) { errno = EINVAL; return -1; } } if (strcmp(enc, loc->components[XLC_CTYPE]->locale) != 0) { if ((ret = __setrunelocale(enc, loc)) != 0) { errno = ret; return -1; } xlocale_fill_name(loc->components[XLC_CTYPE], enc); if (loc->__numeric_fp_cvt == LC_NUMERIC_FP_SAME_LOCALE) loc->__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED; } break; case LC_MESSAGES_MASK: if (!*locale) { enc = __get_locale_env(LC_MESSAGES); if (_checkencoding(enc) < 0) { errno = EINVAL; return -1; } } oenc = (loc->_messages_using_locale ? LDPART_BUFFER(XLOCALE_MESSAGES(loc)) : C); if (strcmp(enc, oenc) != 0 && __messages_load_locale(enc, loc) == _LDP_ERROR) return -1; xlocale_fill_name(loc->components[XLC_MESSAGES], enc); break; case LC_MONETARY_MASK: if (!*locale) { enc = __get_locale_env(LC_MONETARY); if (_checkencoding(enc) < 0) { errno = EINVAL; return -1; } } oenc = (loc->_monetary_using_locale ? LDPART_BUFFER(XLOCALE_MONETARY(loc)) : C); if (strcmp(enc, oenc) != 0 && __monetary_load_locale(enc, loc) == _LDP_ERROR) return -1; xlocale_fill_name(loc->components[XLC_MONETARY], enc); break; case LC_NUMERIC_MASK: if (!*locale) { enc = __get_locale_env(LC_NUMERIC); if (_checkencoding(enc) < 0) { errno = EINVAL; return -1; } } oenc = (loc->_numeric_using_locale ? LDPART_BUFFER(XLOCALE_NUMERIC(loc)) : C); if (strcmp(enc, oenc) != 0) { if (__numeric_load_locale(enc, loc) == _LDP_ERROR) return -1; xlocale_fill_name(loc->components[XLC_NUMERIC], enc); loc->__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED; xlocale_release(loc->__lc_numeric_loc); loc->__lc_numeric_loc = NULL; } break; case LC_TIME_MASK: if (!*locale) { enc = __get_locale_env(LC_TIME); if (_checkencoding(enc) < 0) { errno = EINVAL; return -1; } } oenc = (loc->_time_using_locale ? LDPART_BUFFER(XLOCALE_TIME(loc)) : C); if (strcmp(enc, oenc) != 0 && __time_load_locale(enc, loc) == _LDP_ERROR) return -1; xlocale_fill_name(loc->components[XLC_TIME], enc); break; } } } return 0; } /* * release all the memory objects (the memory will be freed when the refcount * becomes zero) */ static void destruct_locale(void *v) { locale_t loc = v; for (int i = XLC_COLLATE; i < XLC_LAST; i++) xlocale_release(loc->components[i]); xlocale_release(loc->__lc_numeric_loc); free(loc); } /* * EXTERNAL: Duplicate a (non-NULL) locale_t. LC_GLOBAL_LOCALE means the * global locale, while NULL means the current locale. NULL is returned * on error. */ locale_t duplocale(locale_t loc) { if (_checklocale(loc) < 0) { errno = EINVAL; return NULL; } return _duplocale(loc); } /* * EXTERNAL: Free a locale_t, releasing all memory objects. Don't free * illegal locale_t's or the global locale. */ int freelocale(locale_t loc) { if (!loc || _checklocale(loc) < 0 || loc == &__global_locale || loc == LC_GLOBAL_LOCALE || loc == &__c_locale) { errno = EINVAL; return -1; } xlocale_release(loc); return 0; } /* * EXTERNAL: Create a new locale_t, based on the base locale_t, and modified * by the mask and locale string. If the base is NULL, the current locale * is used as the base. If locale is NULL, changes are made from the C locale * for categories set in mask. */ locale_t newlocale(int mask, __const char *locale, locale_t base) { locale_t new; int lcmask = (mask & LC_ALL_MASK); if (_checkencoding(locale) < 0) { errno = EINVAL; return NULL; } if (lcmask == LC_ALL_MASK) base = (locale_t)&__c_locale; else if (_checklocale(base) < 0) { errno = EINVAL; return NULL; } new = _duplocale(base); if (new == NULL) return NULL; if (lcmask == 0 || (lcmask == LC_ALL_MASK && locale == NULL)) return new; if (_modifylocale(new, lcmask, locale) < 0) { freelocale(new); return NULL; } return new; } /* * PRIVATE EXTERNAL: Returns the locale that can be used by wcstod and * family, to convert the wide character string to a multi-byte string * (the LC_NUMERIC and LC_CTYPE locales may be different). */ __private_extern__ locale_t __numeric_ctype(locale_t loc) { switch(loc->__numeric_fp_cvt) { case LC_NUMERIC_FP_UNINITIALIZED: { const char *ctype = loc->components[XLC_CTYPE]->locale; const char *numeric = (loc->_numeric_using_locale ? LDPART_BUFFER(XLOCALE_NUMERIC(loc)) : C); if (strcmp(ctype, numeric) == 0) { loc->__numeric_fp_cvt = LC_NUMERIC_FP_SAME_LOCALE; return loc; } else { loc->__lc_numeric_loc = newlocale(LC_CTYPE_MASK, numeric, (locale_t)&__c_locale); if (loc->__lc_numeric_loc) { loc->__numeric_fp_cvt = LC_NUMERIC_FP_USE_LOCALE; return loc->__lc_numeric_loc; } else { /* shouldn't happen, but just use the same locale */ loc->__numeric_fp_cvt = LC_NUMERIC_FP_SAME_LOCALE; return loc; } } } case LC_NUMERIC_FP_SAME_LOCALE: return loc; case LC_NUMERIC_FP_USE_LOCALE: return loc->__lc_numeric_loc; } return loc; /* shouldn't happen */ } /* * Darwin's querylocale(3) mask's assigned bits are in alphabetical order of the * category name, so map these back to XLC_* constants. */ static int querylocale_mapping[] = { [0] = XLC_COLLATE, [1] = XLC_CTYPE, [2] = XLC_MESSAGES, [3] = XLC_MONETARY, [4] = XLC_NUMERIC, [5] = XLC_TIME, }; /* * EXTERNAL: Returns the locale string for the part specified in mask. The * least significant bit is used. If loc is NULL, the current per-thread * locale is used. */ const char * querylocale(int mask, locale_t loc) { int type; const char *ret; if (_checklocale(loc) < 0 || (mask & LC_ALL_MASK) == 0) { errno = EINVAL; return NULL; } DEFAULT_CURRENT_LOCALE(loc); /* XXX VERSION_MASK */ type = ffs(mask) - 1; if (type >= nitems(querylocale_mapping)) { errno = EINVAL; return NULL; } type = querylocale_mapping[type]; assert(type < XLC_LAST); /* * We don't need to consult _numeric_using_locale, et al., here, because * if its components[type] is set and we're not using it, it should be * populated as the C locale and we'll still return C. */ XL_LOCK(loc); if (loc->components[type] != NULL) ret = loc->components[type]->locale; else ret = C; XL_UNLOCK(loc); return ret; } /* * EXTERNAL: Set the thread-specific locale. The previous locale is returned. * Use LC_GLOBAL_LOCALE to set the global locale. LC_GLOBAL_LOCALE * may also be returned if there was no previous thread-specific locale in * effect. If loc is NULL, the current locale is returned, but no locale * chance is made. NULL is returned on error. */ locale_t uselocale(locale_t loc) { locale_t orig; if (loc == NULL) orig = (locale_t)pthread_getspecific(__locale_key); else { if (_checklocale(loc) < 0) { errno = EINVAL; return NULL; } if (loc == LC_GLOBAL_LOCALE || loc == &__global_locale) /* should never happen */ loc = NULL; xlocale_retain(loc); orig = pthread_getspecific(__locale_key); pthread_setspecific(__locale_key, loc); xlocale_release(orig); } return (orig ? orig : LC_GLOBAL_LOCALE); } /* * EXTERNAL: Used by the MB_CUR_MAX macro to determine the thread-specific * value. */ int ___mb_cur_max(void) { return XLOCALE_CTYPE(__current_locale())->__mb_cur_max; } /* * EXTERNAL: Used by the MB_CUR_MAX_L macro to determine the thread-specific * value, from the given locale_t. */ int ___mb_cur_max_l(locale_t loc) { return XLOCALE_CTYPE(__locale_ptr(loc))->__mb_cur_max; } static void __xlocale_release(void *loc) { locale_t l = loc; xlocale_release(l); } /* * Called from the Libc initializer to setup the thread-specific key. */ __private_extern__ void __xlocale_init(void) { if (__locale_key == (pthread_key_t)-1) { __locale_key = __LIBC_PTHREAD_KEY_XLOCALE; pthread_key_init_np(__locale_key, __xlocale_release); } } |