| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "js/CharacterEncoding.h" |
| 8 |
|
| 9 |
#include "mozilla/CheckedInt.h" |
| 10 |
#include "mozilla/DebugOnly.h" |
| 11 |
#include "mozilla/Latin1.h" |
| 12 |
#include "mozilla/Maybe.h" |
| 13 |
#include "mozilla/Range.h" |
| 14 |
#include "mozilla/Span.h" |
| 15 |
#include "mozilla/Sprintf.h" |
| 16 |
#include "mozilla/TextUtils.h" |
| 17 |
#include "mozilla/Utf8.h" |
| 18 |
|
| 19 |
#ifndef XP_LINUX |
| 20 |
// We still support libstd++ versions without codecvt support on Linux. |
| 21 |
// |
| 22 |
// When the minimum supported libstd++ version is bumped to 3.4.21, we can |
| 23 |
// enable the codecvt code path for Linux, too. This should happen in 2024 when |
| 24 |
// support for CentOS 7 is removed. |
| 25 |
# include <codecvt> |
| 26 |
#endif |
| 27 |
#include <cwchar> |
| 28 |
#include <limits> |
| 29 |
#include <locale> |
| 30 |
#include <type_traits> |
| 31 |
|
| 32 |
#include "frontend/FrontendContext.h" |
| 33 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 34 |
#include "util/StringBuilder.h" |
| 35 |
#include "util/Unicode.h" // unicode::REPLACEMENT_CHARACTER |
| 36 |
#include "vm/JSContext.h" |
| 37 |
|
| 38 |
using mozilla::AsChars; |
| 39 |
using mozilla::AsciiValidUpTo; |
| 40 |
using mozilla::AsWritableChars; |
| 41 |
using mozilla::ConvertLatin1toUtf8Partial; |
| 42 |
using mozilla::ConvertUtf16toUtf8Partial; |
| 43 |
using mozilla::IsAscii; |
| 44 |
using mozilla::IsUtf8Latin1; |
| 45 |
using mozilla::LossyConvertUtf16toLatin1; |
| 46 |
using mozilla::Span; |
| 47 |
using mozilla::Utf8Unit; |
| 48 |
|
| 49 |
using JS::Latin1CharsZ; |
| 50 |
using JS::TwoByteCharsZ; |
| 51 |
using JS::UTF8Chars; |
| 52 |
using JS::UTF8CharsZ; |
| 53 |
|
| 54 |
using namespace js; |
| 55 |
using namespace js::unicode; |
| 56 |
|
| 57 |
Latin1CharsZ JS::LossyTwoByteCharsToNewLatin1CharsZ( |
| 58 |
JSContext* cx, const mozilla::Range<const char16_t>& tbchars) { |
| 59 |
MOZ_ASSERT(cx); |
| 60 |
size_t len = tbchars.length(); |
| 61 |
unsigned char* latin1 = cx->pod_malloc<unsigned char>(len + 1); |
| 62 |
if (!latin1) { |
| 63 |
return Latin1CharsZ(); |
| 64 |
} |
| 65 |
LossyConvertUtf16toLatin1(tbchars, AsWritableChars(Span(latin1, len))); |
| 66 |
latin1[len] = '\0'; |
| 67 |
return Latin1CharsZ(latin1, len); |
| 68 |
} |
| 69 |
|
| 70 |
template <typename CharT> |
| 71 |
static size_t GetDeflatedUTF8StringLength(const CharT* chars, size_t nchars) { |
| 72 |
size_t nbytes = nchars; |
| 73 |
for (const CharT* end = chars + nchars; chars < end; chars++) { |
| 74 |
char16_t c = *chars; |
| 75 |
if (c < 0x80) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
char32_t v; |
| 79 |
if (IsSurrogate(c)) { |
| 80 |
/* nbytes sets 1 length since this is surrogate pair. */ |
| 81 |
if (IsTrailSurrogate(c) || (chars + 1) == end) { |
| 82 |
nbytes += 2; /* Bad Surrogate */ |
| 83 |
continue; |
| 84 |
} |
| 85 |
char16_t c2 = chars[1]; |
| 86 |
if (!IsTrailSurrogate(c2)) { |
| 87 |
nbytes += 2; /* Bad Surrogate */ |
| 88 |
continue; |
| 89 |
} |
| 90 |
v = UTF16Decode(c, c2); |
| 91 |
nbytes--; |
| 92 |
chars++; |
| 93 |
} else { |
| 94 |
v = c; |
| 95 |
} |
| 96 |
v >>= 11; |
| 97 |
nbytes++; |
| 98 |
while (v) { |
| 99 |
v >>= 5; |
| 100 |
nbytes++; |
| 101 |
} |
| 102 |
} |
| 103 |
return nbytes; |
| 104 |
} |
| 105 |
|
| 106 |
JS_PUBLIC_API size_t JS::GetDeflatedUTF8StringLength(JSLinearString* s) { |
| 107 |
JS::AutoCheckCannotGC nogc; |
| 108 |
return s->hasLatin1Chars() |
| 109 |
? ::GetDeflatedUTF8StringLength(s->latin1Chars(nogc), s->length()) |
| 110 |
: ::GetDeflatedUTF8StringLength(s->twoByteChars(nogc), |
| 111 |
s->length()); |
| 112 |
} |
| 113 |
|
| 114 |
JS_PUBLIC_API size_t JS::DeflateStringToUTF8Buffer(JSLinearString* src, |
| 115 |
mozilla::Span<char> dst) { |
| 116 |
JS::AutoCheckCannotGC nogc; |
| 117 |
if (src->hasLatin1Chars()) { |
| 118 |
auto source = AsChars(Span(src->latin1Chars(nogc), src->length())); |
| 119 |
auto [read, written] = ConvertLatin1toUtf8Partial(source, dst); |
| 120 |
(void)read; |
| 121 |
return written; |
| 122 |
} |
| 123 |
auto source = Span(src->twoByteChars(nogc), src->length()); |
| 124 |
auto [read, written] = ConvertUtf16toUtf8Partial(source, dst); |
| 125 |
(void)read; |
| 126 |
return written; |
| 127 |
} |
| 128 |
|
| 129 |
template <typename CharT> |
| 130 |
void ConvertToUTF8(mozilla::Span<CharT> src, mozilla::Span<char> dst); |
| 131 |
|
| 132 |
template <> |
| 133 |
void ConvertToUTF8<const char16_t>(mozilla::Span<const char16_t> src, |
| 134 |
mozilla::Span<char> dst) { |
| 135 |
(void)ConvertUtf16toUtf8Partial(src, dst); |
| 136 |
} |
| 137 |
|
| 138 |
template <> |
| 139 |
void ConvertToUTF8<const Latin1Char>(mozilla::Span<const Latin1Char> src, |
| 140 |
mozilla::Span<char> dst) { |
| 141 |
(void)ConvertLatin1toUtf8Partial(AsChars(src), dst); |
| 142 |
} |
| 143 |
|
| 144 |
template <typename CharT, typename Allocator> |
| 145 |
UTF8CharsZ JS::CharsToNewUTF8CharsZ(Allocator* alloc, |
| 146 |
const mozilla::Range<CharT>& chars) { |
| 147 |
/* Get required buffer size. */ |
| 148 |
const CharT* str = chars.begin().get(); |
| 149 |
size_t len = ::GetDeflatedUTF8StringLength(str, chars.length()); |
| 150 |
|
| 151 |
/* Allocate buffer. */ |
| 152 |
char* utf8 = alloc->template pod_malloc<char>(len + 1); |
| 153 |
if (!utf8) { |
| 154 |
return UTF8CharsZ(); |
| 155 |
} |
| 156 |
|
| 157 |
/* Encode to UTF8. */ |
| 158 |
::ConvertToUTF8(Span(str, chars.length()), Span(utf8, len)); |
| 159 |
utf8[len] = '\0'; |
| 160 |
|
| 161 |
return UTF8CharsZ(utf8, len); |
| 162 |
} |
| 163 |
|
| 164 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 165 |
JSContext* cx, const mozilla::Range<Latin1Char>& chars); |
| 166 |
|
| 167 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 168 |
JSContext* cx, const mozilla::Range<char16_t>& chars); |
| 169 |
|
| 170 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 171 |
JSContext* cx, const mozilla::Range<const Latin1Char>& chars); |
| 172 |
|
| 173 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 174 |
JSContext* cx, const mozilla::Range<const char16_t>& chars); |
| 175 |
|
| 176 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 177 |
FrontendAllocator* cx, const mozilla::Range<Latin1Char>& chars); |
| 178 |
|
| 179 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 180 |
FrontendAllocator* cx, const mozilla::Range<char16_t>& chars); |
| 181 |
|
| 182 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 183 |
FrontendAllocator* cx, const mozilla::Range<const Latin1Char>& chars); |
| 184 |
|
| 185 |
template UTF8CharsZ JS::CharsToNewUTF8CharsZ( |
| 186 |
FrontendAllocator* cx, const mozilla::Range<const char16_t>& chars); |
| 187 |
|
| 188 |
static constexpr uint32_t INVALID_UTF8 = std::numeric_limits<char32_t>::max(); |
| 189 |
|
| 190 |
/* |
| 191 |
* Convert a UTF-8 character sequence into a UCS-4 character and return that |
| 192 |
* character. It is assumed that the caller already checked that the sequence |
| 193 |
* is valid. |
| 194 |
*/ |
| 195 |
static char32_t Utf8ToOneUcs4CharImpl(const uint8_t* utf8Buffer, |
| 196 |
int utf8Length) { |
| 197 |
MOZ_ASSERT(1 <= utf8Length && utf8Length <= 4); |
| 198 |
|
| 199 |
if (utf8Length == 1) { |
| 200 |
MOZ_ASSERT(!(*utf8Buffer & 0x80)); |
| 201 |
return *utf8Buffer; |
| 202 |
} |
| 203 |
|
| 204 |
/* from Unicode 3.1, non-shortest form is illegal */ |
| 205 |
static const char32_t minucs4Table[] = {0x80, 0x800, NonBMPMin}; |
| 206 |
|
| 207 |
MOZ_ASSERT((*utf8Buffer & (0x100 - (1 << (7 - utf8Length)))) == |
| 208 |
(0x100 - (1 << (8 - utf8Length)))); |
| 209 |
char32_t ucs4Char = *utf8Buffer++ & ((1 << (7 - utf8Length)) - 1); |
| 210 |
char32_t minucs4Char = minucs4Table[utf8Length - 2]; |
| 211 |
while (--utf8Length) { |
| 212 |
MOZ_ASSERT((*utf8Buffer & 0xC0) == 0x80); |
| 213 |
ucs4Char = (ucs4Char << 6) | (*utf8Buffer++ & 0x3F); |
| 214 |
} |
| 215 |
|
| 216 |
if (MOZ_UNLIKELY(ucs4Char < minucs4Char)) { |
| 217 |
return INVALID_UTF8; |
| 218 |
} |
| 219 |
|
| 220 |
if (MOZ_UNLIKELY(IsSurrogate(ucs4Char))) { |
| 221 |
return INVALID_UTF8; |
| 222 |
} |
| 223 |
|
| 224 |
return ucs4Char; |
| 225 |
} |
| 226 |
|
| 227 |
char32_t JS::Utf8ToOneUcs4Char(const uint8_t* utf8Buffer, int utf8Length) { |
| 228 |
return Utf8ToOneUcs4CharImpl(utf8Buffer, utf8Length); |
| 229 |
} |
| 230 |
|
| 231 |
static void ReportInvalidCharacter(JSContext* cx, uint32_t offset) { |
| 232 |
char buffer[10]; |
| 233 |
SprintfLiteral(buffer, "%u", offset); |
| 234 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 235 |
JSMSG_MALFORMED_UTF8_CHAR, buffer); |
| 236 |
} |
| 237 |
|
| 238 |
static void ReportBufferTooSmall(JSContext* cx, uint32_t dummy) { |
| 239 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 240 |
JSMSG_BUFFER_TOO_SMALL); |
| 241 |
} |
| 242 |
|
| 243 |
static void ReportTooBigCharacter(JSContext* cx, uint32_t v) { |
| 244 |
char buffer[11]; |
| 245 |
SprintfLiteral(buffer, "0x%x", v); |
| 246 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 247 |
JSMSG_UTF8_CHAR_TOO_LARGE, buffer); |
| 248 |
} |
| 249 |
|
| 250 |
enum class LoopDisposition { |
| 251 |
Break, |
| 252 |
Continue, |
| 253 |
}; |
| 254 |
|
| 255 |
enum class OnUTF8Error { |
| 256 |
InsertReplacementCharacter, |
| 257 |
InsertQuestionMark, |
| 258 |
Throw, |
| 259 |
Crash, |
| 260 |
}; |
| 261 |
|
| 262 |
inline bool IsInvalidSecondByte(uint32_t first, uint8_t second) { |
| 263 |
// Perform an extra check aginst the second byte. |
| 264 |
// From Unicode Standard v6.2, Table 3-7 Well-Formed UTF-8 Byte Sequences. |
| 265 |
// |
| 266 |
// The consumer should perform a followup check for second & 0xC0 == 0x80. |
| 267 |
return (first == 0xE0 && (second & 0xE0) != 0xA0) || // E0 A0~BF |
| 268 |
(first == 0xED && (second & 0xE0) != 0x80) || // ED 80~9F |
| 269 |
(first == 0xF0 && (second & 0xF0) == 0x80) || // F0 90~BF |
| 270 |
(first == 0xF4 && (second & 0xF0) != 0x80); // F4 80~8F |
| 271 |
} |
| 272 |
|
| 273 |
// Scan UTF-8 input and (internally, at least) convert it to a series of UTF-16 |
| 274 |
// code units. But you can also do odd things like pass an empty lambda for |
| 275 |
// `dst`, in which case the output is discarded entirely--the only effect of |
| 276 |
// calling the template that way is error-checking. |
| 277 |
template <OnUTF8Error ErrorAction, typename OutputFn> |
| 278 |
static bool InflateUTF8ToUTF16(JSContext* cx, const UTF8Chars& src, |
| 279 |
OutputFn dst) { |
| 280 |
size_t srclen = src.length(); |
| 281 |
for (uint32_t i = 0; i < srclen; i++) { |
| 282 |
uint32_t v = uint32_t(src[i]); |
| 283 |
if (!(v & 0x80)) { |
| 284 |
// ASCII code unit. Simple copy. |
| 285 |
if (dst(uint16_t(v)) == LoopDisposition::Break) { |
| 286 |
break; |
| 287 |
} |
| 288 |
} else { |
| 289 |
#define INVALID(report, arg, n2) \ |
| 290 |
do { \ |
| 291 |
if (ErrorAction == OnUTF8Error::Throw) { \ |
| 292 |
report(cx, arg); \ |
| 293 |
return false; \ |
| 294 |
} else if (ErrorAction == OnUTF8Error::Crash) { \ |
| 295 |
MOZ_CRASH("invalid UTF-8 string: " #report); \ |
| 296 |
} else { \ |
| 297 |
char16_t replacement; \ |
| 298 |
if (ErrorAction == OnUTF8Error::InsertReplacementCharacter) { \ |
| 299 |
replacement = REPLACEMENT_CHARACTER; \ |
| 300 |
} else { \ |
| 301 |
MOZ_ASSERT(ErrorAction == OnUTF8Error::InsertQuestionMark); \ |
| 302 |
replacement = '?'; \ |
| 303 |
} \ |
| 304 |
if (dst(replacement) == LoopDisposition::Break) { \ |
| 305 |
break; \ |
| 306 |
} \ |
| 307 |
n = n2; \ |
| 308 |
goto invalidMultiByteCodeUnit; \ |
| 309 |
} \ |
| 310 |
} while (0) |
| 311 |
|
| 312 |
// Non-ASCII code unit. Determine its length in bytes (n). |
| 313 |
// |
| 314 |
// Avoid undefined behavior from passing in 0 |
| 315 |
// (https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fclz) |
| 316 |
// by turning on the low bit so that 0xff will set n=31-24=7, which will |
| 317 |
// be detected as an invalid character. |
| 318 |
uint32_t n = mozilla::CountLeadingZeroes32(~int8_t(src[i]) | 0x1) - 24; |
| 319 |
|
| 320 |
// Check the leading byte. |
| 321 |
if (n < 2 || n > 4) { |
| 322 |
INVALID(ReportInvalidCharacter, i, 1); |
| 323 |
} |
| 324 |
|
| 325 |
// Check that |src| is large enough to hold an n-byte code unit. |
| 326 |
if (i + n > srclen) { |
| 327 |
// Check the second and continuation bytes, to replace maximal subparts |
| 328 |
// of an ill-formed subsequence with single U+FFFD. |
| 329 |
if (i + 2 > srclen) { |
| 330 |
INVALID(ReportBufferTooSmall, /* dummy = */ 0, 1); |
| 331 |
} |
| 332 |
|
| 333 |
if (IsInvalidSecondByte(v, (uint8_t)src[i + 1])) { |
| 334 |
INVALID(ReportInvalidCharacter, i, 1); |
| 335 |
} |
| 336 |
|
| 337 |
if ((src[i + 1] & 0xC0) != 0x80) { |
| 338 |
INVALID(ReportInvalidCharacter, i, 1); |
| 339 |
} |
| 340 |
|
| 341 |
if (n == 3) { |
| 342 |
INVALID(ReportInvalidCharacter, i, 2); |
| 343 |
} else { |
| 344 |
if (i + 3 > srclen) { |
| 345 |
INVALID(ReportBufferTooSmall, /* dummy = */ 0, 2); |
| 346 |
} |
| 347 |
if ((src[i + 2] & 0xC0) != 0x80) { |
| 348 |
INVALID(ReportInvalidCharacter, i, 2); |
| 349 |
} |
| 350 |
INVALID(ReportInvalidCharacter, i, 3); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if (IsInvalidSecondByte(v, (uint8_t)src[i + 1])) { |
| 355 |
INVALID(ReportInvalidCharacter, i, 1); |
| 356 |
} |
| 357 |
|
| 358 |
// Check the continuation bytes. |
| 359 |
for (uint32_t m = 1; m < n; m++) { |
| 360 |
if ((src[i + m] & 0xC0) != 0x80) { |
| 361 |
INVALID(ReportInvalidCharacter, i, m); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
// Determine the code unit's length in CharT and act accordingly. |
| 366 |
v = Utf8ToOneUcs4CharImpl((uint8_t*)&src[i], n); |
| 367 |
if (v < NonBMPMin) { |
| 368 |
// The n-byte UTF8 code unit will fit in a single CharT. |
| 369 |
if (dst(char16_t(v)) == LoopDisposition::Break) { |
| 370 |
break; |
| 371 |
} |
| 372 |
} else if (v <= NonBMPMax) { |
| 373 |
// The n-byte UTF8 code unit will fit in two CharT units. |
| 374 |
if (dst(LeadSurrogate(v)) == LoopDisposition::Break) { |
| 375 |
break; |
| 376 |
} |
| 377 |
if (dst(TrailSurrogate(v)) == LoopDisposition::Break) { |
| 378 |
break; |
| 379 |
} |
| 380 |
} else { |
| 381 |
// The n-byte UTF8 code unit won't fit in two CharT units. |
| 382 |
INVALID(ReportTooBigCharacter, v, 1); |
| 383 |
} |
| 384 |
|
| 385 |
invalidMultiByteCodeUnit: |
| 386 |
// Move i to the last byte of the multi-byte code unit; the loop |
| 387 |
// header will do the final i++ to move to the start of the next |
| 388 |
// code unit. |
| 389 |
i += n - 1; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return true; |
| 394 |
} |
| 395 |
|
| 396 |
template <OnUTF8Error ErrorAction, typename CharT> |
| 397 |
static void CopyAndInflateUTF8IntoBuffer(JSContext* cx, const UTF8Chars& src, |
| 398 |
CharT* dst, size_t outlen, |
| 399 |
bool allASCII) { |
| 400 |
if (allASCII) { |
| 401 |
size_t srclen = src.length(); |
| 402 |
MOZ_ASSERT(outlen == srclen); |
| 403 |
for (uint32_t i = 0; i < srclen; i++) { |
| 404 |
dst[i] = CharT(src[i]); |
| 405 |
} |
| 406 |
} else { |
| 407 |
size_t j = 0; |
| 408 |
auto push = [dst, &j](char16_t c) -> LoopDisposition { |
| 409 |
dst[j++] = CharT(c); |
| 410 |
return LoopDisposition::Continue; |
| 411 |
}; |
| 412 |
MOZ_ALWAYS_TRUE((InflateUTF8ToUTF16<ErrorAction>(cx, src, push))); |
| 413 |
MOZ_ASSERT(j == outlen); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
template <OnUTF8Error ErrorAction, typename CharsT> |
| 418 |
static CharsT InflateUTF8StringHelper(JSContext* cx, const UTF8Chars& src, |
| 419 |
size_t* outlen, arena_id_t destArenaId) { |
| 420 |
using CharT = typename CharsT::CharT; |
| 421 |
static_assert( |
| 422 |
std::is_same_v<CharT, char16_t> || std::is_same_v<CharT, Latin1Char>, |
| 423 |
"bad CharT"); |
| 424 |
|
| 425 |
*outlen = 0; |
| 426 |
|
| 427 |
size_t len = 0; |
| 428 |
bool allASCII = true; |
| 429 |
auto count = [&len, &allASCII](char16_t c) -> LoopDisposition { |
| 430 |
len++; |
| 431 |
allASCII &= (c < 0x80); |
| 432 |
return LoopDisposition::Continue; |
| 433 |
}; |
| 434 |
if (!InflateUTF8ToUTF16<ErrorAction>(cx, src, count)) { |
| 435 |
return CharsT(); |
| 436 |
} |
| 437 |
*outlen = len; |
| 438 |
|
| 439 |
CharT* dst = cx->pod_arena_malloc<CharT>(destArenaId, |
| 440 |
*outlen + 1); // +1 for NUL |
| 441 |
|
| 442 |
if (!dst) { |
| 443 |
ReportOutOfMemory(cx); |
| 444 |
return CharsT(); |
| 445 |
} |
| 446 |
|
| 447 |
constexpr OnUTF8Error errorMode = |
| 448 |
std::is_same_v<CharT, Latin1Char> |
| 449 |
? OnUTF8Error::InsertQuestionMark |
| 450 |
: OnUTF8Error::InsertReplacementCharacter; |
| 451 |
CopyAndInflateUTF8IntoBuffer<errorMode>(cx, src, dst, *outlen, allASCII); |
| 452 |
dst[*outlen] = CharT('\0'); |
| 453 |
|
| 454 |
return CharsT(dst, *outlen); |
| 455 |
} |
| 456 |
|
| 457 |
TwoByteCharsZ JS::UTF8CharsToNewTwoByteCharsZ(JSContext* cx, |
| 458 |
const UTF8Chars& utf8, |
| 459 |
size_t* outlen, |
| 460 |
arena_id_t destArenaId) { |
| 461 |
return InflateUTF8StringHelper<OnUTF8Error::Throw, TwoByteCharsZ>( |
| 462 |
cx, utf8, outlen, destArenaId); |
| 463 |
} |
| 464 |
|
| 465 |
TwoByteCharsZ JS::LossyUTF8CharsToNewTwoByteCharsZ(JSContext* cx, |
| 466 |
const JS::UTF8Chars& utf8, |
| 467 |
size_t* outlen, |
| 468 |
arena_id_t destArenaId) { |
| 469 |
return InflateUTF8StringHelper<OnUTF8Error::InsertReplacementCharacter, |
| 470 |
TwoByteCharsZ>(cx, utf8, outlen, destArenaId); |
| 471 |
} |
| 472 |
|
| 473 |
static void UpdateSmallestEncodingForChar(char16_t c, |
| 474 |
JS::SmallestEncoding* encoding) { |
| 475 |
JS::SmallestEncoding newEncoding = JS::SmallestEncoding::ASCII; |
| 476 |
if (c >= 0x80) { |
| 477 |
if (c < 0x100) { |
| 478 |
newEncoding = JS::SmallestEncoding::Latin1; |
| 479 |
} else { |
| 480 |
newEncoding = JS::SmallestEncoding::UTF16; |
| 481 |
} |
| 482 |
} |
| 483 |
if (newEncoding > *encoding) { |
| 484 |
*encoding = newEncoding; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
JS::SmallestEncoding JS::FindSmallestEncoding(const UTF8Chars& utf8) { |
| 489 |
Span<const unsigned char> unsignedSpan = utf8; |
| 490 |
auto charSpan = AsChars(unsignedSpan); |
| 491 |
size_t upTo = AsciiValidUpTo(charSpan); |
| 492 |
if (upTo == charSpan.Length()) { |
| 493 |
return SmallestEncoding::ASCII; |
| 494 |
} |
| 495 |
if (IsUtf8Latin1(charSpan.From(upTo))) { |
| 496 |
return SmallestEncoding::Latin1; |
| 497 |
} |
| 498 |
return SmallestEncoding::UTF16; |
| 499 |
} |
| 500 |
|
| 501 |
Latin1CharsZ JS::UTF8CharsToNewLatin1CharsZ(JSContext* cx, |
| 502 |
const UTF8Chars& utf8, |
| 503 |
size_t* outlen, |
| 504 |
arena_id_t destArenaId) { |
| 505 |
return InflateUTF8StringHelper<OnUTF8Error::Throw, Latin1CharsZ>( |
| 506 |
cx, utf8, outlen, destArenaId); |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* Atomization Helpers. |
| 511 |
* |
| 512 |
* These functions are extremely single-use, and are not intended for general |
| 513 |
* consumption. |
| 514 |
*/ |
| 515 |
|
| 516 |
bool GetUTF8AtomizationData(JSContext* cx, const JS::UTF8Chars& utf8, |
| 517 |
size_t* outlen, JS::SmallestEncoding* encoding, |
| 518 |
HashNumber* hashNum) { |
| 519 |
*outlen = 0; |
| 520 |
*encoding = JS::SmallestEncoding::ASCII; |
| 521 |
*hashNum = 0; |
| 522 |
|
| 523 |
auto getMetadata = [outlen, encoding, |
| 524 |
hashNum](char16_t c) -> LoopDisposition { |
| 525 |
(*outlen)++; |
| 526 |
UpdateSmallestEncodingForChar(c, encoding); |
| 527 |
*hashNum = mozilla::AddToHash(*hashNum, c); |
| 528 |
return LoopDisposition::Continue; |
| 529 |
}; |
| 530 |
if (!InflateUTF8ToUTF16<OnUTF8Error::Throw>(cx, utf8, getMetadata)) { |
| 531 |
return false; |
| 532 |
} |
| 533 |
|
| 534 |
return true; |
| 535 |
} |
| 536 |
|
| 537 |
template <typename CharT> |
| 538 |
bool UTF8EqualsChars(const JS::UTF8Chars& utfChars, const CharT* chars) { |
| 539 |
static_assert(std::is_same_v<CharT, JS::Latin1Char> || |
| 540 |
std::is_same_v<CharT, char16_t>); |
| 541 |
|
| 542 |
size_t ind = 0; |
| 543 |
bool isEqual = true; |
| 544 |
|
| 545 |
auto checkEqual = [&isEqual, &ind, chars](char16_t c) -> LoopDisposition { |
| 546 |
if (c != char16_t(chars[ind])) { |
| 547 |
isEqual = false; |
| 548 |
return LoopDisposition::Break; |
| 549 |
} |
| 550 |
|
| 551 |
ind++; |
| 552 |
return LoopDisposition::Continue; |
| 553 |
}; |
| 554 |
|
| 555 |
// The caller must have already validated UTF-8 well-formedness. |
| 556 |
InflateUTF8ToUTF16<OnUTF8Error::Crash>(/* cx = */ nullptr, utfChars, |
| 557 |
checkEqual); |
| 558 |
|
| 559 |
return isEqual; |
| 560 |
} |
| 561 |
|
| 562 |
template bool UTF8EqualsChars(const JS::UTF8Chars&, const char16_t*); |
| 563 |
template bool UTF8EqualsChars(const JS::UTF8Chars&, const JS::Latin1Char*); |
| 564 |
|
| 565 |
template <typename CharT> |
| 566 |
void InflateUTF8CharsToBuffer(const JS::UTF8Chars& src, CharT* dst, |
| 567 |
size_t dstLen, JS::SmallestEncoding encoding) { |
| 568 |
CopyAndInflateUTF8IntoBuffer<OnUTF8Error::Crash>( |
| 569 |
/* cx = */ nullptr, src, dst, dstLen, |
| 570 |
encoding == JS::SmallestEncoding::ASCII); |
| 571 |
} |
| 572 |
|
| 573 |
template void InflateUTF8CharsToBuffer(const UTF8Chars& src, char16_t* dst, |
| 574 |
size_t dstLen, |
| 575 |
JS::SmallestEncoding encoding); |
| 576 |
template void InflateUTF8CharsToBuffer(const UTF8Chars& src, |
| 577 |
JS::Latin1Char* dst, size_t dstLen, |
| 578 |
JS::SmallestEncoding encoding); |
| 579 |
|
| 580 |
#ifdef DEBUG |
| 581 |
void JS::ConstUTF8CharsZ::validate(size_t aLength) { |
| 582 |
MOZ_ASSERT(data_); |
| 583 |
UTF8Chars chars(data_, aLength); |
| 584 |
auto nop = [](char16_t) -> LoopDisposition { |
| 585 |
return LoopDisposition::Continue; |
| 586 |
}; |
| 587 |
InflateUTF8ToUTF16<OnUTF8Error::Crash>(/* cx = */ nullptr, chars, nop); |
| 588 |
} |
| 589 |
void JS::ConstUTF8CharsZ::validateWithoutLength() { |
| 590 |
MOZ_ASSERT(data_); |
| 591 |
validate(strlen(data_)); |
| 592 |
} |
| 593 |
#endif |
| 594 |
|
| 595 |
bool JS::StringIsASCII(const char* s) { |
| 596 |
while (*s) { |
| 597 |
if (*s & 0x80) { |
| 598 |
return false; |
| 599 |
} |
| 600 |
s++; |
| 601 |
} |
| 602 |
return true; |
| 603 |
} |
| 604 |
|
| 605 |
bool JS::StringIsASCII(Span<const char> s) { return IsAscii(s); } |
| 606 |
|
| 607 |
JS_PUBLIC_API JS::UniqueChars JS::EncodeNarrowToUtf8(JSContext* cx, |
| 608 |
const char* chars) { |
| 609 |
// Convert the narrow multibyte character string to a wide string and then |
| 610 |
// use EncodeWideToUtf8() to convert the wide string to a UTF-8 string. |
| 611 |
|
| 612 |
std::mbstate_t mb{}; |
| 613 |
|
| 614 |
// NOTE: The 2nd parameter is overwritten even if the 1st parameter is nullptr |
| 615 |
// on Android NDK older than v16. Use a temporary variable to save the |
| 616 |
// `chars` for the subsequent call. See bug 1492090. |
| 617 |
const char* tmpChars = chars; |
| 618 |
|
| 619 |
size_t wideLen = std::mbsrtowcs(nullptr, &tmpChars, 0, &mb); |
| 620 |
if (wideLen == size_t(-1)) { |
| 621 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 622 |
JSMSG_CANT_CONVERT_TO_WIDE); |
| 623 |
return nullptr; |
| 624 |
} |
| 625 |
MOZ_ASSERT(std::mbsinit(&mb), |
| 626 |
"multi-byte state is in its initial state when no conversion " |
| 627 |
"error occured"); |
| 628 |
|
| 629 |
size_t bufLen = wideLen + 1; |
| 630 |
auto wideChars = cx->make_pod_array<wchar_t>(bufLen); |
| 631 |
if (!wideChars) { |
| 632 |
return nullptr; |
| 633 |
} |
| 634 |
|
| 635 |
mozilla::DebugOnly<size_t> actualLen = |
| 636 |
std::mbsrtowcs(wideChars.get(), &chars, bufLen, &mb); |
| 637 |
MOZ_ASSERT(wideLen == actualLen); |
| 638 |
MOZ_ASSERT(wideChars[actualLen] == '\0'); |
| 639 |
|
| 640 |
return EncodeWideToUtf8(cx, wideChars.get()); |
| 641 |
} |
| 642 |
|
| 643 |
JS_PUBLIC_API JS::UniqueChars JS::EncodeWideToUtf8(JSContext* cx, |
| 644 |
const wchar_t* chars) { |
| 645 |
using CheckedSizeT = mozilla::CheckedInt<size_t>; |
| 646 |
|
| 647 |
#ifndef XP_LINUX |
| 648 |
// Use the standard codecvt facet to convert a wide string to UTF-8. |
| 649 |
std::codecvt_utf8<wchar_t> cv; |
| 650 |
|
| 651 |
size_t len = std::wcslen(chars); |
| 652 |
CheckedSizeT utf8MaxLen = CheckedSizeT(len) * cv.max_length(); |
| 653 |
CheckedSizeT utf8BufLen = utf8MaxLen + 1; |
| 654 |
if (!utf8BufLen.isValid()) { |
| 655 |
JS_ReportAllocationOverflow(cx); |
| 656 |
return nullptr; |
| 657 |
} |
| 658 |
auto utf8 = cx->make_pod_array<char>(utf8BufLen.value()); |
| 659 |
if (!utf8) { |
| 660 |
return nullptr; |
| 661 |
} |
| 662 |
|
| 663 |
// STL returns |codecvt_base::partial| for empty strings. |
| 664 |
if (len == 0) { |
| 665 |
utf8[0] = '\0'; // Explicit null-termination required. |
| 666 |
return utf8; |
| 667 |
} |
| 668 |
|
| 669 |
std::mbstate_t mb{}; |
| 670 |
const wchar_t* fromNext; |
| 671 |
char* toNext; |
| 672 |
std::codecvt_base::result result = |
| 673 |
cv.out(mb, chars, chars + len, fromNext, utf8.get(), |
| 674 |
utf8.get() + utf8MaxLen.value(), toNext); |
| 675 |
if (result != std::codecvt_base::ok) { |
| 676 |
MOZ_ASSERT(result == std::codecvt_base::error); |
| 677 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 678 |
JSMSG_CANT_CONVERT_WIDE_TO_UTF8); |
| 679 |
return nullptr; |
| 680 |
} |
| 681 |
*toNext = '\0'; // Explicit null-termination required. |
| 682 |
|
| 683 |
// codecvt_utf8 doesn't validate its output and may produce WTF-8 instead |
| 684 |
// of UTF-8 on some platforms when the input contains unpaired surrogate |
| 685 |
// characters. We don't allow this. |
| 686 |
if (!mozilla::IsUtf8( |
| 687 |
mozilla::Span(utf8.get(), size_t(toNext - utf8.get())))) { |
| 688 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 689 |
JSMSG_CANT_CONVERT_WIDE_TO_UTF8); |
| 690 |
return nullptr; |
| 691 |
} |
| 692 |
|
| 693 |
return utf8; |
| 694 |
#else |
| 695 |
// Alternative code path for Linux, because we still support libstd++ versions |
| 696 |
// without codecvt support. See also the top comment where <codecvt> is |
| 697 |
// included. |
| 698 |
|
| 699 |
static_assert(sizeof(wchar_t) == 4, |
| 700 |
"Assume wchar_t is UTF-32 on Linux systems"); |
| 701 |
|
| 702 |
constexpr size_t MaxUtf8CharLength = 4; |
| 703 |
|
| 704 |
size_t len = std::wcslen(chars); |
| 705 |
CheckedSizeT utf8MaxLen = CheckedSizeT(len) * MaxUtf8CharLength; |
| 706 |
CheckedSizeT utf8BufLen = utf8MaxLen + 1; |
| 707 |
if (!utf8BufLen.isValid()) { |
| 708 |
JS_ReportAllocationOverflow(cx); |
| 709 |
return nullptr; |
| 710 |
} |
| 711 |
auto utf8 = cx->make_pod_array<char>(utf8BufLen.value()); |
| 712 |
if (!utf8) { |
| 713 |
return nullptr; |
| 714 |
} |
| 715 |
|
| 716 |
char* dst = utf8.get(); |
| 717 |
for (size_t i = 0; i < len; i++) { |
| 718 |
uint8_t utf8buf[MaxUtf8CharLength]; |
| 719 |
uint32_t utf8Len = OneUcs4ToUtf8Char(utf8buf, chars[i]); |
| 720 |
for (size_t j = 0; j < utf8Len; j++) { |
| 721 |
*dst++ = char(utf8buf[j]); |
| 722 |
} |
| 723 |
} |
| 724 |
*dst = '\0'; |
| 725 |
|
| 726 |
return utf8; |
| 727 |
#endif |
| 728 |
} |
| 729 |
|
| 730 |
JS_PUBLIC_API JS::UniqueChars JS::EncodeUtf8ToNarrow(JSContext* cx, |
| 731 |
const char* chars) { |
| 732 |
// Convert the UTF-8 string to a wide string via EncodeUtf8ToWide() and |
| 733 |
// then convert the resulting wide string to a narrow multibyte character |
| 734 |
// string. |
| 735 |
|
| 736 |
auto wideChars = EncodeUtf8ToWide(cx, chars); |
| 737 |
if (!wideChars) { |
| 738 |
return nullptr; |
| 739 |
} |
| 740 |
|
| 741 |
const wchar_t* cWideChars = wideChars.get(); |
| 742 |
std::mbstate_t mb{}; |
| 743 |
size_t narrowLen = std::wcsrtombs(nullptr, &cWideChars, 0, &mb); |
| 744 |
if (narrowLen == size_t(-1)) { |
| 745 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 746 |
JSMSG_CANT_CONVERT_TO_NARROW); |
| 747 |
return nullptr; |
| 748 |
} |
| 749 |
MOZ_ASSERT(std::mbsinit(&mb), |
| 750 |
"multi-byte state is in its initial state when no conversion " |
| 751 |
"error occured"); |
| 752 |
|
| 753 |
size_t bufLen = narrowLen + 1; |
| 754 |
auto narrow = cx->make_pod_array<char>(bufLen); |
| 755 |
if (!narrow) { |
| 756 |
return nullptr; |
| 757 |
} |
| 758 |
|
| 759 |
mozilla::DebugOnly<size_t> actualLen = |
| 760 |
std::wcsrtombs(narrow.get(), &cWideChars, bufLen, &mb); |
| 761 |
MOZ_ASSERT(narrowLen == actualLen); |
| 762 |
MOZ_ASSERT(narrow[actualLen] == '\0'); |
| 763 |
|
| 764 |
return narrow; |
| 765 |
} |
| 766 |
|
| 767 |
JS_PUBLIC_API JS::UniqueWideChars JS::EncodeUtf8ToWide(JSContext* cx, |
| 768 |
const char* chars) { |
| 769 |
// Only valid UTF-8 strings should be passed to this function. |
| 770 |
MOZ_ASSERT(mozilla::IsUtf8(mozilla::Span(chars, strlen(chars)))); |
| 771 |
|
| 772 |
#ifndef XP_LINUX |
| 773 |
// Use the standard codecvt facet to convert from UTF-8 to a wide string. |
| 774 |
std::codecvt_utf8<wchar_t> cv; |
| 775 |
|
| 776 |
size_t len = strlen(chars); |
| 777 |
auto wideChars = cx->make_pod_array<wchar_t>(len + 1); |
| 778 |
if (!wideChars) { |
| 779 |
return nullptr; |
| 780 |
} |
| 781 |
|
| 782 |
// STL returns |codecvt_base::partial| for empty strings. |
| 783 |
if (len == 0) { |
| 784 |
wideChars[0] = '\0'; // Explicit null-termination required. |
| 785 |
return wideChars; |
| 786 |
} |
| 787 |
|
| 788 |
std::mbstate_t mb{}; |
| 789 |
const char* fromNext; |
| 790 |
wchar_t* toNext; |
| 791 |
std::codecvt_base::result result = |
| 792 |
cv.in(mb, chars, chars + len, fromNext, wideChars.get(), |
| 793 |
wideChars.get() + len, toNext); |
| 794 |
if (result != std::codecvt_base::ok) { |
| 795 |
MOZ_ASSERT(result == std::codecvt_base::error); |
| 796 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 797 |
JSMSG_CANT_CONVERT_UTF8_TO_WIDE); |
| 798 |
return nullptr; |
| 799 |
} |
| 800 |
*toNext = '\0'; // Explicit null-termination required. |
| 801 |
|
| 802 |
return wideChars; |
| 803 |
#else |
| 804 |
// Alternative code path for Linux, because we still support libstd++ versions |
| 805 |
// without codecvt support. See also the top comment where <codecvt> is |
| 806 |
// included. |
| 807 |
|
| 808 |
static_assert(sizeof(wchar_t) == 4, |
| 809 |
"Assume wchar_t is UTF-32 on Linux systems"); |
| 810 |
|
| 811 |
size_t len = strlen(chars); |
| 812 |
auto wideChars = cx->make_pod_array<wchar_t>(len + 1); |
| 813 |
if (!wideChars) { |
| 814 |
return nullptr; |
| 815 |
} |
| 816 |
|
| 817 |
const auto* s = reinterpret_cast<const unsigned char*>(chars); |
| 818 |
const auto* const limit = s + len; |
| 819 |
|
| 820 |
wchar_t* dst = wideChars.get(); |
| 821 |
while (s < limit) { |
| 822 |
unsigned char c = *s++; |
| 823 |
|
| 824 |
if (mozilla::IsAscii(c)) { |
| 825 |
*dst++ = wchar_t(c); |
| 826 |
continue; |
| 827 |
} |
| 828 |
|
| 829 |
mozilla::Utf8Unit utf8(c); |
| 830 |
mozilla::Maybe<char32_t> codePoint = |
| 831 |
mozilla::DecodeOneUtf8CodePoint(utf8, &s, limit); |
| 832 |
MOZ_ASSERT(codePoint.isSome()); |
| 833 |
*dst++ = wchar_t(*codePoint); |
| 834 |
} |
| 835 |
*dst++ = '\0'; |
| 836 |
|
| 837 |
return wideChars; |
| 838 |
#endif |
| 839 |
} |
| 840 |
|
| 841 |
bool StringBuilder::append(const Utf8Unit* units, size_t len) { |
| 842 |
MOZ_ASSERT(maybeCx_); |
| 843 |
|
| 844 |
if (isLatin1()) { |
| 845 |
Latin1CharBuffer& latin1 = latin1Chars(); |
| 846 |
|
| 847 |
while (len > 0) { |
| 848 |
if (!IsAscii(*units)) { |
| 849 |
break; |
| 850 |
} |
| 851 |
|
| 852 |
if (!latin1.append(units->toUnsignedChar())) { |
| 853 |
return false; |
| 854 |
} |
| 855 |
|
| 856 |
++units; |
| 857 |
--len; |
| 858 |
} |
| 859 |
if (len == 0) { |
| 860 |
return true; |
| 861 |
} |
| 862 |
|
| 863 |
// Non-ASCII doesn't *necessarily* mean we couldn't keep appending to |
| 864 |
// |latin1|, but it's only possible for [U+0080, U+0100) code points, |
| 865 |
// and handling the full complexity of UTF-8 only for that very small |
| 866 |
// additional range isn't worth it. Inflate to two-byte storage before |
| 867 |
// appending the remaining code points. |
| 868 |
if (!inflateChars()) { |
| 869 |
return false; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
UTF8Chars remainingUtf8(units, len); |
| 874 |
|
| 875 |
// Determine how many UTF-16 code units are required to represent the |
| 876 |
// remaining units. |
| 877 |
size_t utf16Len = 0; |
| 878 |
auto countInflated = [&utf16Len](char16_t c) -> LoopDisposition { |
| 879 |
utf16Len++; |
| 880 |
return LoopDisposition::Continue; |
| 881 |
}; |
| 882 |
if (!InflateUTF8ToUTF16<OnUTF8Error::Throw>(maybeCx_, remainingUtf8, |
| 883 |
countInflated)) { |
| 884 |
return false; |
| 885 |
} |
| 886 |
|
| 887 |
TwoByteCharBuffer& buf = twoByteChars(); |
| 888 |
|
| 889 |
size_t i = buf.length(); |
| 890 |
if (!buf.growByUninitialized(utf16Len)) { |
| 891 |
return false; |
| 892 |
} |
| 893 |
MOZ_ASSERT(i + utf16Len == buf.length(), |
| 894 |
"growByUninitialized assumed to increase length immediately"); |
| 895 |
|
| 896 |
char16_t* toFill = &buf[i]; |
| 897 |
auto appendUtf16 = [&toFill](char16_t unit) { |
| 898 |
*toFill++ = unit; |
| 899 |
return LoopDisposition::Continue; |
| 900 |
}; |
| 901 |
|
| 902 |
MOZ_ALWAYS_TRUE(InflateUTF8ToUTF16<OnUTF8Error::Throw>( |
| 903 |
maybeCx_, remainingUtf8, appendUtf16)); |
| 904 |
MOZ_ASSERT(toFill == buf.end()); |
| 905 |
return true; |
| 906 |
} |