Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Dan Albert | 75ef63d | 2014-11-21 00:18:07 -0800 | [diff] [blame] | 29 | #include <errno.h> |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 30 | #include <locale.h> |
| 31 | #include <pthread.h> |
| 32 | #include <stdlib.h> |
Elliott Hughes | b20c244 | 2014-11-06 15:04:08 -0800 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <strings.h> |
| 35 | #include <time.h> |
| 36 | #include <wchar.h> |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 37 | |
Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 38 | #include "platform/bionic/macros.h" |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 39 | |
Dan Albert | 1a8b7f2 | 2017-07-12 13:47:42 -0700 | [diff] [blame] | 40 | #if defined(__BIONIC_BUILD_FOR_ANDROID_SUPPORT) |
| 41 | #define USE_TLS_SLOT 0 |
| 42 | #else |
| 43 | #define USE_TLS_SLOT 1 |
| 44 | #endif |
| 45 | |
| 46 | #if USE_TLS_SLOT |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 47 | #include "bionic/pthread_internal.h" |
Dan Albert | 1a8b7f2 | 2017-07-12 13:47:42 -0700 | [diff] [blame] | 48 | #endif |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 53de874 | 2016-10-24 14:50:31 -0700 | [diff] [blame] | 50 | // We only support two locales, the "C" locale (also known as "POSIX"), |
| 51 | // and the "C.UTF-8" locale (also known as "en_US.UTF-8"). |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 52 | |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 53 | static bool __bionic_current_locale_is_utf8 = true; |
| 54 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 55 | struct __locale_t { |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 56 | size_t mb_cur_max; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Ryan Prichard | bcb9715 | 2022-04-29 00:20:02 -0700 | [diff] [blame^] | 59 | // Avoid using new/delete in this file, because a user may have overridden |
| 60 | // new/delete, and we want to avoid making extraneous calls to them. This isn't |
| 61 | // an issue for libc.so in the platform, but this file is also compiled into the |
| 62 | // NDK's libandroid_support.a, and there are libc++ tests that count the number |
| 63 | // of calls to new/delete. |
| 64 | #pragma clang poison new delete |
| 65 | |
| 66 | static inline locale_t __alloc_locale(size_t mb_cur_max) { |
| 67 | auto result = static_cast<__locale_t*>(malloc(sizeof(__locale_t))); |
| 68 | if (result == nullptr) return nullptr; |
| 69 | result->mb_cur_max = mb_cur_max; |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | static inline size_t get_locale_mb_cur_max(locale_t l) { |
Yabin Cui | f7e3b3e | 2015-03-05 20:08:21 -0800 | [diff] [blame] | 74 | if (l == LC_GLOBAL_LOCALE) { |
| 75 | return __bionic_current_locale_is_utf8 ? 4 : 1; |
| 76 | } else { |
| 77 | return l->mb_cur_max; |
| 78 | } |
| 79 | } |
| 80 | |
Ryan Prichard | bcb9715 | 2022-04-29 00:20:02 -0700 | [diff] [blame^] | 81 | size_t __ctype_get_mb_cur_max() { |
| 82 | return get_locale_mb_cur_max(uselocale(nullptr)); |
| 83 | } |
| 84 | |
Dan Albert | 1a8b7f2 | 2017-07-12 13:47:42 -0700 | [diff] [blame] | 85 | #if !USE_TLS_SLOT |
| 86 | static thread_local locale_t g_current_locale; |
| 87 | #endif |
| 88 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 89 | static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT; |
| 90 | static lconv g_locale; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 91 | |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 92 | static void __locale_init() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 93 | g_locale.decimal_point = const_cast<char*>("."); |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 94 | |
| 95 | char* not_available = const_cast<char*>(""); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 96 | g_locale.thousands_sep = not_available; |
| 97 | g_locale.grouping = not_available; |
| 98 | g_locale.int_curr_symbol = not_available; |
| 99 | g_locale.currency_symbol = not_available; |
| 100 | g_locale.mon_decimal_point = not_available; |
| 101 | g_locale.mon_thousands_sep = not_available; |
| 102 | g_locale.mon_grouping = not_available; |
| 103 | g_locale.positive_sign = not_available; |
| 104 | g_locale.negative_sign = not_available; |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 105 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 106 | g_locale.int_frac_digits = CHAR_MAX; |
| 107 | g_locale.frac_digits = CHAR_MAX; |
| 108 | g_locale.p_cs_precedes = CHAR_MAX; |
| 109 | g_locale.p_sep_by_space = CHAR_MAX; |
| 110 | g_locale.n_cs_precedes = CHAR_MAX; |
| 111 | g_locale.n_sep_by_space = CHAR_MAX; |
| 112 | g_locale.p_sign_posn = CHAR_MAX; |
| 113 | g_locale.n_sign_posn = CHAR_MAX; |
| 114 | g_locale.int_p_cs_precedes = CHAR_MAX; |
| 115 | g_locale.int_p_sep_by_space = CHAR_MAX; |
| 116 | g_locale.int_n_cs_precedes = CHAR_MAX; |
| 117 | g_locale.int_n_sep_by_space = CHAR_MAX; |
| 118 | g_locale.int_p_sign_posn = CHAR_MAX; |
| 119 | g_locale.int_n_sign_posn = CHAR_MAX; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Elliott Hughes | 53de874 | 2016-10-24 14:50:31 -0700 | [diff] [blame] | 122 | static bool __is_supported_locale(const char* locale_name) { |
| 123 | return (strcmp(locale_name, "") == 0 || |
| 124 | strcmp(locale_name, "C") == 0 || |
| 125 | strcmp(locale_name, "C.UTF-8") == 0 || |
| 126 | strcmp(locale_name, "en_US.UTF-8") == 0 || |
| 127 | strcmp(locale_name, "POSIX") == 0); |
| 128 | } |
| 129 | |
| 130 | static bool __is_utf8_locale(const char* locale_name) { |
| 131 | return (*locale_name == '\0' || strstr(locale_name, "UTF-8")); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 134 | lconv* localeconv() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 135 | pthread_once(&g_locale_once, __locale_init); |
| 136 | return &g_locale; |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 139 | locale_t duplocale(locale_t l) { |
Ryan Prichard | bcb9715 | 2022-04-29 00:20:02 -0700 | [diff] [blame^] | 140 | return __alloc_locale(get_locale_mb_cur_max(l)); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void freelocale(locale_t l) { |
Ryan Prichard | bcb9715 | 2022-04-29 00:20:02 -0700 | [diff] [blame^] | 144 | free(l); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) { |
Elliott Hughes | 7e0d0f8 | 2014-11-04 18:03:44 -0800 | [diff] [blame] | 148 | // Are 'category_mask' and 'locale_name' valid? |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 149 | if ((category_mask & ~LC_ALL_MASK) != 0 || locale_name == nullptr) { |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 150 | errno = EINVAL; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 151 | return nullptr; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if (!__is_supported_locale(locale_name)) { |
| 155 | errno = ENOENT; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 156 | return nullptr; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ryan Prichard | bcb9715 | 2022-04-29 00:20:02 -0700 | [diff] [blame^] | 159 | return __alloc_locale(__is_utf8_locale(locale_name) ? 4 : 1); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 162 | char* setlocale(int category, const char* locale_name) { |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 163 | // Is 'category' valid? |
| 164 | if (category < LC_CTYPE || category > LC_IDENTIFICATION) { |
| 165 | errno = EINVAL; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 166 | return nullptr; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 169 | // Caller wants to set the locale rather than just query? |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 170 | if (locale_name != nullptr) { |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 171 | if (!__is_supported_locale(locale_name)) { |
| 172 | // We don't support this locale. |
| 173 | errno = ENOENT; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 174 | return nullptr; |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 175 | } |
Elliott Hughes | 53de874 | 2016-10-24 14:50:31 -0700 | [diff] [blame] | 176 | __bionic_current_locale_is_utf8 = __is_utf8_locale(locale_name); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | 5a0aa3d | 2014-04-30 22:03:12 -0700 | [diff] [blame] | 179 | return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C"); |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 180 | } |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 181 | |
Dan Albert | 1a8b7f2 | 2017-07-12 13:47:42 -0700 | [diff] [blame] | 182 | static locale_t* get_current_locale_ptr() { |
| 183 | #if USE_TLS_SLOT |
| 184 | return &__get_bionic_tls().locale; |
| 185 | #else |
| 186 | return &g_current_locale; |
| 187 | #endif |
| 188 | } |
| 189 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 190 | locale_t uselocale(locale_t new_locale) { |
Dan Albert | 1a8b7f2 | 2017-07-12 13:47:42 -0700 | [diff] [blame] | 191 | locale_t old_locale = *get_current_locale_ptr(); |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 192 | |
| 193 | // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 194 | if (old_locale == nullptr) { |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 195 | old_locale = LC_GLOBAL_LOCALE; |
| 196 | } |
| 197 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 198 | if (new_locale != nullptr) { |
Dan Albert | 1a8b7f2 | 2017-07-12 13:47:42 -0700 | [diff] [blame] | 199 | *get_current_locale_ptr() = new_locale; |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return old_locale; |
| 203 | } |