2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-01-24 13:45:29 +00:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Utf8View.h>
|
2019-08-27 21:57:15 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-09-04 20:40:36 +00:00
|
|
|
Utf8View::Utf8View(const String& string)
|
|
|
|
: m_string(string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
Utf8View::Utf8View(const StringView& string)
|
|
|
|
: m_string(string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:06:39 +00:00
|
|
|
Utf8View::Utf8View(const char* string)
|
|
|
|
: m_string(string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
const unsigned char* Utf8View::begin_ptr() const
|
|
|
|
{
|
|
|
|
return (const unsigned char*)m_string.characters_without_null_termination();
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* Utf8View::end_ptr() const
|
|
|
|
{
|
2019-09-04 20:40:36 +00:00
|
|
|
return begin_ptr() + m_string.length();
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Utf8CodepointIterator Utf8View::begin() const
|
|
|
|
{
|
2021-01-01 23:28:24 +00:00
|
|
|
return { begin_ptr(), m_string.length() };
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Utf8CodepointIterator Utf8View::end() const
|
|
|
|
{
|
|
|
|
return { end_ptr(), 0 };
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:51:24 +00:00
|
|
|
size_t Utf8View::byte_offset_of(const Utf8CodepointIterator& it) const
|
2019-09-04 20:40:36 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(it.m_ptr >= begin_ptr());
|
|
|
|
VERIFY(it.m_ptr <= end_ptr());
|
2019-09-04 20:40:36 +00:00
|
|
|
|
|
|
|
return it.m_ptr - begin_ptr();
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:59:15 +00:00
|
|
|
Utf8View Utf8View::substring_view(size_t byte_offset, size_t byte_length) const
|
2019-09-04 20:40:36 +00:00
|
|
|
{
|
|
|
|
StringView string = m_string.substring_view(byte_offset, byte_length);
|
|
|
|
return Utf8View { string };
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
static inline bool decode_first_byte(
|
|
|
|
unsigned char byte,
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t& out_code_point_length_in_bytes,
|
2019-08-27 21:57:15 +00:00
|
|
|
u32& out_value)
|
|
|
|
{
|
|
|
|
if ((byte & 128) == 0) {
|
|
|
|
out_value = byte;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 1;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((byte & 64) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((byte & 32) == 0) {
|
|
|
|
out_value = byte & 31;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 2;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((byte & 16) == 0) {
|
|
|
|
out_value = byte & 15;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 3;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((byte & 8) == 0) {
|
|
|
|
out_value = byte & 7;
|
2020-08-05 20:31:20 +00:00
|
|
|
out_code_point_length_in_bytes = 4;
|
2019-08-27 21:57:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-18 09:15:18 +00:00
|
|
|
bool Utf8View::validate(size_t& valid_bytes) const
|
2019-08-27 21:57:15 +00:00
|
|
|
{
|
2020-05-18 09:15:18 +00:00
|
|
|
valid_bytes = 0;
|
2019-08-27 21:57:15 +00:00
|
|
|
for (auto ptr = begin_ptr(); ptr < end_ptr(); ptr++) {
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
u32 value;
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*ptr, code_point_length_in_bytes, value);
|
2019-08-27 21:57:15 +00:00
|
|
|
if (!first_byte_makes_sense)
|
|
|
|
return false;
|
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
for (size_t i = 1; i < code_point_length_in_bytes; i++) {
|
2019-08-27 21:57:15 +00:00
|
|
|
ptr++;
|
|
|
|
if (ptr >= end_ptr())
|
|
|
|
return false;
|
|
|
|
if (*ptr >> 6 != 2)
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-18 09:15:18 +00:00
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
valid_bytes += code_point_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:47:34 +00:00
|
|
|
size_t Utf8View::calculate_length() const
|
2020-05-17 11:02:27 +00:00
|
|
|
{
|
|
|
|
size_t length = 0;
|
2020-12-20 23:09:48 +00:00
|
|
|
for ([[maybe_unused]] auto code_point : *this) {
|
2020-05-17 11:02:27 +00:00
|
|
|
++length;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:31:15 +00:00
|
|
|
bool Utf8View::starts_with(const Utf8View& start) const
|
|
|
|
{
|
|
|
|
if (start.is_empty())
|
|
|
|
return true;
|
|
|
|
if (is_empty())
|
|
|
|
return false;
|
|
|
|
if (start.length() > length())
|
|
|
|
return false;
|
|
|
|
if (begin_ptr() == start.begin_ptr())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto k = begin(), l = start.begin(); l != start.end(); ++k, ++l) {
|
|
|
|
if (*k != *l)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
Utf8CodepointIterator::Utf8CodepointIterator(const unsigned char* ptr, size_t length)
|
2019-08-27 21:57:15 +00:00
|
|
|
: m_ptr(ptr)
|
|
|
|
, m_length(length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Utf8CodepointIterator::operator==(const Utf8CodepointIterator& other) const
|
|
|
|
{
|
|
|
|
return m_ptr == other.m_ptr && m_length == other.m_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Utf8CodepointIterator::operator!=(const Utf8CodepointIterator& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
Utf8CodepointIterator& Utf8CodepointIterator::operator++()
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes = 0;
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
u32 value;
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(first_byte_makes_sense);
|
AK: Fix buffer overrun in Utf8CodepointIterator::operator++
The old implementation tried to move forward as long as the current
byte looks like a UTF-8 character continuation byte (has its two
most significant bits set to 10). This is correct as long as we assume
the string is actually valid UTF-8, which we do (we also have a separate
method that can check whether it is the case).
We can't, however, assume that the data after the end of our string
is also valid UTF-8 (in fact, we're not even allowed to look at data
outside out string, but it happens to a valid memory region most of
the time). If the byte after the end of our string also has its most
significant bits set to 10, we would move one byte forward, and then
fail the m_length > 0 assertion.
One way to fix this would be to add a length check inside the loop
condition. The other one, implemented in this commit, is to reimplement
the whole function in terms of decode_first_byte(), which gives us
the length as encoded in the first byte. This also brings it more
in line with the other functions around it that do UTF-8 decoding.
2019-09-08 15:24:54 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(code_point_length_in_bytes <= m_length);
|
2020-08-05 20:31:20 +00:00
|
|
|
m_ptr += code_point_length_in_bytes;
|
|
|
|
m_length -= code_point_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t Utf8CodepointIterator::code_point_length_in_bytes() const
|
2019-10-18 20:49:23 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes = 0;
|
2019-10-18 20:49:23 +00:00
|
|
|
u32 value;
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(first_byte_makes_sense);
|
2020-08-05 20:31:20 +00:00
|
|
|
return code_point_length_in_bytes;
|
2019-10-18 20:49:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
u32 Utf8CodepointIterator::operator*() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_length > 0);
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
u32 code_point_value_so_far = 0;
|
2021-01-01 23:28:24 +00:00
|
|
|
size_t code_point_length_in_bytes = 0;
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(m_ptr[0], code_point_length_in_bytes, code_point_value_so_far);
|
2020-10-07 12:02:42 +00:00
|
|
|
if (!first_byte_makes_sense)
|
2021-01-01 23:28:24 +00:00
|
|
|
dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, m_length });
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(first_byte_makes_sense);
|
2020-10-07 12:02:42 +00:00
|
|
|
if (code_point_length_in_bytes > m_length)
|
|
|
|
dbgln("Not enough bytes (need {}, have {}), first byte is: {:#02x}, '{}'", code_point_length_in_bytes, m_length, m_ptr[0], (const char*)m_ptr);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(code_point_length_in_bytes <= m_length);
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2021-01-01 23:28:24 +00:00
|
|
|
for (size_t offset = 1; offset < code_point_length_in_bytes; offset++) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_ptr[offset] >> 6 == 2);
|
2020-08-05 20:31:20 +00:00
|
|
|
code_point_value_so_far <<= 6;
|
|
|
|
code_point_value_so_far |= m_ptr[offset] & 63;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 20:31:20 +00:00
|
|
|
return code_point_value_so_far;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|