| 1 | // std::codecvt implementation details, GNU version -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | // |
| 26 | // ISO C++ 14882: 22.2.1.5 - Template class codecvt |
| 27 | // |
| 28 | |
| 29 | // Written by Benjamin Kosnik <[email protected]> |
| 30 | |
| 31 | #include <locale> |
| 32 | #include <cstdlib> // For MB_CUR_MAX |
| 33 | #include <climits> // For MB_LEN_MAX |
| 34 | #include <bits/c++locale_internal.h> |
| 35 | |
| 36 | namespace std _GLIBCXX_VISIBILITY(default) |
| 37 | { |
| 38 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 39 | |
| 40 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 41 | namespace |
| 42 | { |
| 43 | // RAII type for changing and restoring the current thread's locale. |
| 44 | struct Guard |
| 45 | { |
| 46 | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) |
| 47 | explicit Guard(__c_locale loc) : old(__uselocale(loc)) { } |
| 48 | ~Guard() { __uselocale(old); } |
| 49 | #else |
| 50 | explicit Guard(__c_locale) { } |
| 51 | #endif |
| 52 | __c_locale old; |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | // Specializations. |
| 57 | codecvt_base::result |
| 58 | codecvt<wchar_t, char, mbstate_t>:: |
| 59 | do_out(state_type& __state, const intern_type* __from, |
| 60 | const intern_type* __from_end, const intern_type*& __from_next, |
| 61 | extern_type* __to, extern_type* __to_end, |
| 62 | extern_type*& __to_next) const |
| 63 | { |
| 64 | state_type __tmp_state(__state); |
| 65 | Guard g(_M_c_locale_codecvt); |
| 66 | |
| 67 | // wcsnrtombs is *very* fast but stops if encounters NUL characters: |
| 68 | // in case we fall back to wcrtomb and then continue, in a loop. |
| 69 | // NB: wcsnrtombs is a GNU extension |
| 70 | __from_next = __from; |
| 71 | __to_next = __to; |
| 72 | while (__from_next < __from_end) |
| 73 | { |
| 74 | if (__to_next >= __to_end) |
| 75 | return partial; |
| 76 | |
| 77 | const intern_type* __from_chunk_end |
| 78 | = wmemchr(__from_next, L'\0', __from_end - __from_next); |
| 79 | if (!__from_chunk_end) |
| 80 | __from_chunk_end = __from_end; |
| 81 | |
| 82 | __from = __from_next; |
| 83 | const size_t __conv = wcsnrtombs(__to_next, &__from_next, |
| 84 | __from_chunk_end - __from_next, |
| 85 | __to_end - __to_next, &__state); |
| 86 | if (__conv == static_cast<size_t>(-1)) |
| 87 | { |
| 88 | // In case of error, in order to stop at the exact place we |
| 89 | // have to start again from the beginning with a series of |
| 90 | // wcrtomb. |
| 91 | for (; __from < __from_next; ++__from) |
| 92 | __to_next += wcrtomb(__to_next, *__from, &__tmp_state); |
| 93 | __state = __tmp_state; |
| 94 | return error; |
| 95 | } |
| 96 | else if (__from_next && __from_next < __from_chunk_end) |
| 97 | { |
| 98 | __to_next += __conv; |
| 99 | return partial; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | __from_next = __from_chunk_end; |
| 104 | __to_next += __conv; |
| 105 | } |
| 106 | |
| 107 | if (__from_next < __from_end) |
| 108 | { |
| 109 | extern_type __buf[MB_LEN_MAX]; |
| 110 | __tmp_state = __state; |
| 111 | const size_t __conv2 = wcrtomb(__buf, *__from_next, &__tmp_state); |
| 112 | if (__conv2 > static_cast<size_t>(__to_end - __to_next)) |
| 113 | return partial; |
| 114 | else |
| 115 | { |
| 116 | memcpy(__to_next, __buf, __conv2); |
| 117 | __state = __tmp_state; |
| 118 | __to_next += __conv2; |
| 119 | ++__from_next; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return ok; |
| 125 | } |
| 126 | |
| 127 | codecvt_base::result |
| 128 | codecvt<wchar_t, char, mbstate_t>:: |
| 129 | do_in(state_type& __state, const extern_type* __from, |
| 130 | const extern_type* __from_end, const extern_type*& __from_next, |
| 131 | intern_type* __to, intern_type* __to_end, |
| 132 | intern_type*& __to_next) const |
| 133 | { |
| 134 | state_type __tmp_state(__state); |
| 135 | Guard g(_M_c_locale_codecvt); |
| 136 | |
| 137 | // mbsnrtowcs is *very* fast but stops if encounters NUL characters: |
| 138 | // in case we store a L'\0' and then continue, in a loop. |
| 139 | // NB: mbsnrtowcs is a GNU extension |
| 140 | __from_next = __from; |
| 141 | __to_next = __to; |
| 142 | while (__from_next < __from_end) |
| 143 | { |
| 144 | if (__to_next >= __to_end) |
| 145 | return partial; |
| 146 | |
| 147 | const extern_type* __from_chunk_end |
| 148 | = static_cast<const extern_type*>(memchr(__from_next, '\0', |
| 149 | __from_end - __from_next)); |
| 150 | if (!__from_chunk_end) |
| 151 | __from_chunk_end = __from_end; |
| 152 | |
| 153 | __from = __from_next; |
| 154 | size_t __conv = mbsnrtowcs(__to_next, &__from_next, |
| 155 | __from_chunk_end - __from_next, |
| 156 | __to_end - __to_next, &__state); |
| 157 | if (__conv == static_cast<size_t>(-1)) |
| 158 | { |
| 159 | // In case of error, in order to stop at the exact place we |
| 160 | // have to start again from the beginning with a series of |
| 161 | // mbrtowc. |
| 162 | for (;; ++__to_next, __from += __conv) |
| 163 | { |
| 164 | __conv = mbrtowc(__to_next, __from, __from_end - __from, |
| 165 | &__tmp_state); |
| 166 | if (__conv == static_cast<size_t>(-1) |
| 167 | || __conv == static_cast<size_t>(-2)) |
| 168 | break; |
| 169 | } |
| 170 | __from_next = __from; |
| 171 | __state = __tmp_state; |
| 172 | return error; |
| 173 | } |
| 174 | else if (__from_next && __from_next < __from_chunk_end) |
| 175 | { |
| 176 | // It is unclear what to return in this case (see DR 382). |
| 177 | __to_next += __conv; |
| 178 | return partial; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | __from_next = __from_chunk_end; |
| 183 | __to_next += __conv; |
| 184 | } |
| 185 | |
| 186 | if (__from_next < __from_end) |
| 187 | { |
| 188 | if (__to_next < __to_end) |
| 189 | { |
| 190 | // XXX Probably wrong for stateful encodings |
| 191 | __tmp_state = __state; |
| 192 | ++__from_next; |
| 193 | *__to_next++ = L'\0'; |
| 194 | } |
| 195 | else |
| 196 | return partial; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return ok; |
| 201 | } |
| 202 | |
| 203 | int |
| 204 | codecvt<wchar_t, char, mbstate_t>:: |
| 205 | do_encoding() const throw() |
| 206 | { |
| 207 | Guard g(_M_c_locale_codecvt); |
| 208 | // XXX This implementation assumes that the encoding is |
| 209 | // stateless and is either single-byte or variable-width. |
| 210 | return MB_CUR_MAX == 1; |
| 211 | } |
| 212 | |
| 213 | int |
| 214 | codecvt<wchar_t, char, mbstate_t>:: |
| 215 | do_max_length() const throw() |
| 216 | { |
| 217 | Guard g(_M_c_locale_codecvt); |
| 218 | // XXX Probably wrong for stateful encodings. |
| 219 | return MB_CUR_MAX; |
| 220 | } |
| 221 | |
| 222 | int |
| 223 | codecvt<wchar_t, char, mbstate_t>:: |
| 224 | do_length(state_type& __state, const extern_type* __from, |
| 225 | const extern_type* __end, size_t __max) const |
| 226 | { |
| 227 | int __ret = 0; |
| 228 | state_type __tmp_state(__state); |
| 229 | Guard g(_M_c_locale_codecvt); |
| 230 | |
| 231 | // mbsnrtowcs is *very* fast but stops if encounters NUL characters: |
| 232 | // in case we advance past it and then continue, in a loop. |
| 233 | // NB: mbsnrtowcs is in POSIX.1-2008 |
| 234 | |
| 235 | const size_t __to_len = 1024; // Size of alloca'd output buffer |
| 236 | |
| 237 | // A dummy internal buffer is needed in order for mbsnrtocws to consider |
| 238 | // its fourth parameter (it wouldn't with NULL as first parameter). |
| 239 | wchar_t* __to = static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t) |
| 240 | * __to_len)); |
| 241 | while (__from < __end && __max) |
| 242 | { |
| 243 | const extern_type* __from_chunk_end; |
| 244 | __from_chunk_end = static_cast<const extern_type*>(memchr(__from, '\0', |
| 245 | __end |
| 246 | - __from)); |
| 247 | if (!__from_chunk_end) |
| 248 | __from_chunk_end = __end; |
| 249 | |
| 250 | const extern_type* __tmp_from = __from; |
| 251 | size_t __conv = mbsnrtowcs(__to, &__from, |
| 252 | __from_chunk_end - __from, |
| 253 | __max > __to_len ? __to_len : __max, |
| 254 | &__state); |
| 255 | if (__conv == static_cast<size_t>(-1)) |
| 256 | { |
| 257 | // In case of error, in order to stop at the exact place we |
| 258 | // have to start again from the beginning with a series of |
| 259 | // mbrtowc. |
| 260 | for (__from = __tmp_from;; __from += __conv) |
| 261 | { |
| 262 | __conv = mbrtowc(0, __from, __end - __from, |
| 263 | &__tmp_state); |
| 264 | if (__conv == static_cast<size_t>(-1) |
| 265 | || __conv == static_cast<size_t>(-2)) |
| 266 | break; |
| 267 | } |
| 268 | __state = __tmp_state; |
| 269 | __ret += __from - __tmp_from; |
| 270 | break; |
| 271 | } |
| 272 | if (!__from) |
| 273 | __from = __from_chunk_end; |
| 274 | |
| 275 | __ret += __from - __tmp_from; |
| 276 | __max -= __conv; |
| 277 | |
| 278 | if (__from < __end && __max) |
| 279 | { |
| 280 | // XXX Probably wrong for stateful encodings |
| 281 | __tmp_state = __state; |
| 282 | ++__from; |
| 283 | ++__ret; |
| 284 | --__max; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return __ret; |
| 289 | } |
| 290 | #endif |
| 291 | |
| 292 | _GLIBCXX_END_NAMESPACE_VERSION |
| 293 | } // namespace |