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
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Assertions.h>
|
2019-09-04 20:41:22 +00:00
|
|
|
#include <AK/LogStream.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
|
|
|
|
{
|
2019-12-09 16:45:40 +00:00
|
|
|
return { begin_ptr(), (int)m_string.length() };
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Utf8CodepointIterator Utf8View::end() const
|
|
|
|
{
|
|
|
|
return { end_ptr(), 0 };
|
|
|
|
}
|
|
|
|
|
2019-09-04 20:40:36 +00:00
|
|
|
int Utf8View::byte_offset_of(const Utf8CodepointIterator& it) const
|
|
|
|
{
|
|
|
|
ASSERT(it.m_ptr >= begin_ptr());
|
|
|
|
ASSERT(it.m_ptr <= end_ptr());
|
|
|
|
|
|
|
|
return it.m_ptr - begin_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
Utf8View Utf8View::substring_view(int byte_offset, int byte_length) const
|
|
|
|
{
|
|
|
|
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,
|
2020-08-05 20:28:19 +00:00
|
|
|
int& out_codepoint_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:28:19 +00:00
|
|
|
out_codepoint_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:28:19 +00:00
|
|
|
out_codepoint_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:28:19 +00:00
|
|
|
out_codepoint_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:28:19 +00:00
|
|
|
out_codepoint_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++) {
|
2020-08-05 20:28:19 +00:00
|
|
|
int codepoint_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
u32 value;
|
2020-08-05 20:28:19 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*ptr, codepoint_length_in_bytes, value);
|
2019-08-27 21:57:15 +00:00
|
|
|
if (!first_byte_makes_sense)
|
|
|
|
return false;
|
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
for (int i = 1; i < codepoint_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:28:19 +00:00
|
|
|
valid_bytes += codepoint_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
size_t Utf8View::length_in_codepoints() const
|
2020-05-17 11:02:27 +00:00
|
|
|
{
|
|
|
|
size_t length = 0;
|
2020-08-05 20:28:19 +00:00
|
|
|
for (auto codepoint : *this) {
|
|
|
|
(void)codepoint;
|
2020-05-17 11:02:27 +00:00
|
|
|
++length;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
Utf8CodepointIterator::Utf8CodepointIterator(const unsigned char* ptr, int length)
|
|
|
|
: 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++()
|
|
|
|
{
|
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
|
|
|
ASSERT(m_length > 0);
|
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
int codepoint_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:28:19 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, codepoint_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
|
|
|
|
|
|
|
ASSERT(first_byte_makes_sense);
|
|
|
|
(void)value;
|
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
ASSERT(codepoint_length_in_bytes <= m_length);
|
|
|
|
m_ptr += codepoint_length_in_bytes;
|
|
|
|
m_length -= codepoint_length_in_bytes;
|
2019-08-27 21:57:15 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
int Utf8CodepointIterator::codepoint_length_in_bytes() const
|
2019-10-18 20:49:23 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_length > 0);
|
2020-08-05 20:28:19 +00:00
|
|
|
int codepoint_length_in_bytes = 0;
|
2019-10-18 20:49:23 +00:00
|
|
|
u32 value;
|
2020-08-05 20:28:19 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(*m_ptr, codepoint_length_in_bytes, value);
|
2019-10-18 20:49:23 +00:00
|
|
|
ASSERT(first_byte_makes_sense);
|
2020-08-05 20:28:19 +00:00
|
|
|
return codepoint_length_in_bytes;
|
2019-10-18 20:49:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:57:15 +00:00
|
|
|
u32 Utf8CodepointIterator::operator*() const
|
|
|
|
{
|
|
|
|
ASSERT(m_length > 0);
|
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
u32 codepoint_value_so_far = 0;
|
|
|
|
int codepoint_length_in_bytes = 0;
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
bool first_byte_makes_sense = decode_first_byte(m_ptr[0], codepoint_length_in_bytes, codepoint_value_so_far);
|
2019-09-04 20:41:22 +00:00
|
|
|
if (!first_byte_makes_sense) {
|
2019-09-15 13:46:40 +00:00
|
|
|
dbg() << "First byte doesn't make sense, bytes: " << StringView((const char*)m_ptr, m_length);
|
2019-09-04 20:41:22 +00:00
|
|
|
}
|
2019-08-27 21:57:15 +00:00
|
|
|
ASSERT(first_byte_makes_sense);
|
2020-08-05 20:28:19 +00:00
|
|
|
if (codepoint_length_in_bytes > m_length) {
|
|
|
|
dbg() << "Not enough bytes (need " << codepoint_length_in_bytes << ", have " << m_length << "), first byte is: " << m_ptr[0] << " " << (const char*)m_ptr;
|
2019-09-04 20:41:22 +00:00
|
|
|
}
|
2020-08-05 20:28:19 +00:00
|
|
|
ASSERT(codepoint_length_in_bytes <= m_length);
|
2019-08-27 21:57:15 +00:00
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
for (int offset = 1; offset < codepoint_length_in_bytes; offset++) {
|
2019-08-27 21:57:15 +00:00
|
|
|
ASSERT(m_ptr[offset] >> 6 == 2);
|
2020-08-05 20:28:19 +00:00
|
|
|
codepoint_value_so_far <<= 6;
|
|
|
|
codepoint_value_so_far |= m_ptr[offset] & 63;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 20:28:19 +00:00
|
|
|
return codepoint_value_so_far;
|
2019-08-27 21:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|