Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed parsing UTF-16 encoded inputs which do not end with a surrogate… #234

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 83 additions & 76 deletions include/fkYAML/detail/input/input_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,41 +117,46 @@ class iterator_input_adapter<
{
if (m_current == m_end)
{
return std::char_traits<char_type>::eof();
if (m_encoded_buf_size == 0)
{
return std::char_traits<char_type>::eof();
}
}

while (m_current != m_end && m_elems_to_read > 0)
while (m_current != m_end && m_encoded_buf_size < 2)
{
switch (m_encode_type)
{
case encode_t::UTF_16BE_N:
case encode_t::UTF_16BE_BOM:
m_encoded_buffer[2 - m_elems_to_read] = char16_t(uint8_t(*m_current) << 8);
m_encoded_buffer[m_encoded_buf_size] = char16_t(uint8_t(*m_current) << 8);
++m_current;
m_encoded_buffer[2 - m_elems_to_read] |= char16_t(*m_current);
m_encoded_buffer[m_encoded_buf_size] |= char16_t(*m_current);
break;
case encode_t::UTF_16LE_N:
case encode_t::UTF_16LE_BOM: {
m_encoded_buffer[2 - m_elems_to_read] = char16_t(*m_current);
m_encoded_buffer[m_encoded_buf_size] = char16_t(*m_current);
++m_current;
m_encoded_buffer[2 - m_elems_to_read] |= char16_t(uint8_t(*m_current) << 8);
m_encoded_buffer[m_encoded_buf_size] |= char16_t(uint8_t(*m_current) << 8);
break;
}
default: // LCOV_EXCL_LINE
// should not come here.
break; // LCOV_EXCL_LINE
}
++m_current;
--m_elems_to_read;
++m_encoded_buf_size;
}

utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, m_elems_to_read, m_utf8_buf_size);
std::size_t consumed_size = 0;
utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, consumed_size, m_utf8_buf_size);

if (m_elems_to_read == 1)
if (consumed_size == 1)
{
m_encoded_buffer[0] = m_encoded_buffer[1];
m_encoded_buffer[1] = 0;
}
m_encoded_buf_size -= consumed_size;

m_utf8_buf_index = 0;
}
Expand Down Expand Up @@ -220,8 +225,8 @@ class iterator_input_adapter<
encode_t m_encode_type {encode_t::UTF_8_N};
/// The buffer for decoding characters read from the input.
std::array<char16_t, 2> m_encoded_buffer {{0, 0}};
/// The number of elements to be read from the input next time.
std::size_t m_elems_to_read {2};
/// The number of elements in `m_encoded_buffer`.
std::size_t m_encoded_buf_size {0};
/// The buffer for UTF-8 encoded characters.
std::array<char, 4> m_utf8_buffer {{0, 0, 0, 0}};
/// The next index in `m_utf8_buffer` to read.
Expand Down Expand Up @@ -271,39 +276,44 @@ class iterator_input_adapter<
{
if (m_current == m_end)
{
return std::char_traits<char_type>::eof();
if (m_encoded_buf_size == 0)
{
return std::char_traits<char_type>::eof();
}
}

while (m_current != m_end && m_elems_to_read > 0)
while (m_current != m_end && m_encoded_buf_size < 2)
{
switch (m_encode_type)
{
case encode_t::UTF_16BE_N:
case encode_t::UTF_16BE_BOM:
m_encoded_buffer[2 - m_elems_to_read] = *m_current;
m_encoded_buffer[m_encoded_buf_size] = *m_current;
break;
case encode_t::UTF_16LE_N:
case encode_t::UTF_16LE_BOM: {
char16_t tmp = *m_current;
m_encoded_buffer[2 - m_elems_to_read] = char16_t((tmp & 0x00FFu) << 8);
m_encoded_buffer[2 - m_elems_to_read] |= char16_t((tmp & 0xFF00u) >> 8);
m_encoded_buffer[m_encoded_buf_size] = char16_t((tmp & 0x00FFu) << 8);
m_encoded_buffer[m_encoded_buf_size] |= char16_t((tmp & 0xFF00u) >> 8);
break;
}
default: // LCOV_EXCL_LINE
// should not come here.
break; // LCOV_EXCL_LINE
}
++m_current;
--m_elems_to_read;
++m_encoded_buf_size;
}

utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, m_elems_to_read, m_utf8_buf_size);
std::size_t consumed_size = 0;
utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, consumed_size, m_utf8_buf_size);

if (m_elems_to_read == 1)
if (consumed_size == 1)
{
m_encoded_buffer[0] = m_encoded_buffer[1];
m_encoded_buffer[1] = 0;
}
m_encoded_buf_size -= consumed_size;

m_utf8_buf_index = 0;
}
Expand All @@ -322,8 +332,8 @@ class iterator_input_adapter<
encode_t m_encode_type {encode_t::UTF_16BE_N};
/// The buffer for decoding characters read from the input.
std::array<char16_t, 2> m_encoded_buffer {{0, 0}};
/// The number of elements to be read from the input next time.
std::size_t m_elems_to_read {2};
/// The number of elements in `m_encoded_buffer`.
std::size_t m_encoded_buf_size {0};
/// The buffer for UTF-8 encoded characters.
std::array<char, 4> m_utf8_buffer {{0, 0, 0, 0}};
/// The next index in `m_utf8_buffer` to read.
Expand Down Expand Up @@ -483,7 +493,7 @@ class file_input_adapter
{
char ch = 0;
size_t size = std::fread(&ch, sizeof(char), 1, m_file);
if (size == sizeof(char))
if (size == 1)
{
return std::char_traits<char_type>::to_int_type(ch);
}
Expand All @@ -496,45 +506,44 @@ class file_input_adapter
{
if (m_utf8_buf_index == m_utf8_buf_size)
{
char byte = 0;
size_t size = std::fread(&byte, sizeof(char), 1, m_file);
if (size != sizeof(char))
{
return std::char_traits<char_type>::eof();
}

do
char chars[2] = {0, 0};
while (m_encoded_buf_size < 2 && std::fread(&chars[0], sizeof(char), 2, m_file) == 2)
{
switch (m_encode_type)
{
case encode_t::UTF_16BE_N:
case encode_t::UTF_16BE_BOM:
m_encoded_buffer[2 - m_elems_to_read] = char16_t(uint8_t(byte) << 8);
size = std::fread(&byte, sizeof(char), 1, m_file);
m_encoded_buffer[2 - m_elems_to_read] |= char16_t(uint8_t(byte));
m_encoded_buffer[m_encoded_buf_size] = char16_t(uint8_t(chars[0]) << 8);
m_encoded_buffer[m_encoded_buf_size] |= char16_t(uint8_t(chars[1]));
break;
case encode_t::UTF_16LE_N:
case encode_t::UTF_16LE_BOM: {
m_encoded_buffer[2 - m_elems_to_read] = char16_t(uint8_t(byte));
size = std::fread(&byte, sizeof(char), 1, m_file);
m_encoded_buffer[2 - m_elems_to_read] |= char16_t(uint8_t(byte) << 8);
m_encoded_buffer[m_encoded_buf_size] = char16_t(uint8_t(chars[0]));
m_encoded_buffer[m_encoded_buf_size] |= char16_t(uint8_t(chars[1]) << 8);
break;
}
default: // LCOV_EXCL_LINE
// should not come here.
break; // LCOV_EXCL_LINE
}

--m_elems_to_read;
} while (m_elems_to_read > 0 && std::fread(&byte, sizeof(char), 1, m_file) == sizeof(char));
++m_encoded_buf_size;
}

if (m_encoded_buf_size == 0)
{
return std::char_traits<char_type>::eof();
}

utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, m_elems_to_read, m_utf8_buf_size);
std::size_t consumed_size = 0;
utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, consumed_size, m_utf8_buf_size);

if (m_elems_to_read == 1)
if (consumed_size == 1)
{
m_encoded_buffer[0] = m_encoded_buffer[1];
m_encoded_buffer[1] = 0;
}
m_encoded_buf_size -= consumed_size;

m_utf8_buf_index = 0;
}
Expand All @@ -550,9 +559,9 @@ class file_input_adapter
{
if (m_utf8_buf_index == m_utf8_buf_size)
{
char byte = 0;
std::size_t size = std::fread(&byte, sizeof(char), 1, m_file);
if (size != sizeof(char))
char chars[4] = {0, 0, 0, 0};
std::size_t size = std::fread(&chars[0], sizeof(char), 4, m_file);
if (size != 4)
{
return std::char_traits<char_type>::eof();
}
Expand All @@ -562,23 +571,17 @@ class file_input_adapter
{
case encode_t::UTF_32BE_N:
case encode_t::UTF_32BE_BOM:
utf32 = char32_t(uint8_t(byte) << 24);
std::fread(&byte, sizeof(char), 1, m_file);
utf32 |= char32_t(uint8_t(byte) << 16);
std::fread(&byte, sizeof(char), 1, m_file);
utf32 |= char32_t(uint8_t(byte) << 8);
std::fread(&byte, sizeof(char), 1, m_file);
utf32 |= char32_t(uint8_t(byte));
utf32 = char32_t(uint8_t(chars[0]) << 24);
utf32 |= char32_t(uint8_t(chars[1]) << 16);
utf32 |= char32_t(uint8_t(chars[2]) << 8);
utf32 |= char32_t(uint8_t(chars[3]));
break;
case encode_t::UTF_32LE_N:
case encode_t::UTF_32LE_BOM: {
utf32 = char32_t(uint8_t(byte));
std::fread(&byte, sizeof(char), 1, m_file);
utf32 |= char32_t(uint8_t(byte) << 8);
std::fread(&byte, sizeof(char), 1, m_file);
utf32 |= char32_t(uint8_t(byte) << 16);
std::fread(&byte, sizeof(char), 1, m_file);
utf32 |= char32_t(uint8_t(byte) << 24);
utf32 = char32_t(uint8_t(chars[0]));
utf32 |= char32_t(uint8_t(chars[1]) << 8);
utf32 |= char32_t(uint8_t(chars[2]) << 16);
utf32 |= char32_t(uint8_t(chars[3]) << 24);
break;
}
default: // LCOV_EXCL_LINE
Expand All @@ -602,8 +605,8 @@ class file_input_adapter
encode_t m_encode_type {encode_t::UTF_8_N};
/// The buffer for decoding characters read from the input.
std::array<char16_t, 2> m_encoded_buffer {{0, 0}};
/// The number of elements to be read from the input next time.
std::size_t m_elems_to_read {2};
/// The number of elements in `m_encoded_buffer`.
std::size_t m_encoded_buf_size {0};
/// The buffer for UTF-8 encoded characters.
std::array<char, 4> m_utf8_buffer {{0, 0, 0, 0}};
/// The next index in `m_utf8_buffer` to read.
Expand Down Expand Up @@ -676,46 +679,50 @@ class stream_input_adapter
{
if (m_utf8_buf_index == m_utf8_buf_size)
{
do
while (m_encoded_buf_size < 2)
{
char ch = 0;
m_istream->read(&ch, 1);
char chars[2] = {0, 0};
m_istream->read(&chars[0], 2);
std::streamsize size = m_istream->gcount();
if (size != 1)
if (size != 2)
{
return std::char_traits<char_type>::eof();
if (m_encoded_buf_size == 0)
{
return std::char_traits<char_type>::eof();
}
break;
}

switch (m_encode_type)
{
case encode_t::UTF_16BE_N:
case encode_t::UTF_16BE_BOM:
m_encoded_buffer[2 - m_elems_to_read] = char16_t(uint8_t(ch) << 8);
m_istream->read(&ch, 1);
m_encoded_buffer[2 - m_elems_to_read] |= char16_t(uint8_t(ch));
m_encoded_buffer[m_encoded_buf_size] = char16_t(uint8_t(chars[0]) << 8);
m_encoded_buffer[m_encoded_buf_size] |= char16_t(uint8_t(chars[1]));
break;
case encode_t::UTF_16LE_N:
case encode_t::UTF_16LE_BOM: {
m_encoded_buffer[2 - m_elems_to_read] = char16_t(uint8_t(ch));
m_istream->read(&ch, 1);
m_encoded_buffer[2 - m_elems_to_read] |= char16_t(uint8_t(ch) << 8);
m_encoded_buffer[m_encoded_buf_size] = char16_t(uint8_t(chars[0]));
m_encoded_buffer[m_encoded_buf_size] |= char16_t(uint8_t(chars[1]) << 8);
break;
}
default: // LCOV_EXCL_LINE
// should not come here.
break; // LCOV_EXCL_LINE
}

--m_elems_to_read;
} while (m_elems_to_read > 0);
++m_encoded_buf_size;
};

utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, m_elems_to_read, m_utf8_buf_size);
std::size_t consumed_size = 0;
utf8_encoding::from_utf16(m_encoded_buffer, m_utf8_buffer, consumed_size, m_utf8_buf_size);

if (m_elems_to_read == 1)
if (consumed_size == 1)
{
m_encoded_buffer[0] = m_encoded_buffer[1];
m_encoded_buffer[1] = 0;
}
m_encoded_buf_size -= consumed_size;

m_utf8_buf_index = 0;
}
Expand Down Expand Up @@ -784,8 +791,8 @@ class stream_input_adapter
encode_t m_encode_type {encode_t::UTF_8_N};
/// The buffer for decoding characters read from the input.
std::array<char16_t, 2> m_encoded_buffer {{0, 0}};
/// The number of elements to be read from the input next time.
std::size_t m_elems_to_read {2};
/// The number of elements in `m_encoded_buffer`.
std::size_t m_encoded_buf_size {0};
/// The buffer for UTF-8 encoded characters.
std::array<char, 4> m_utf8_buffer {{0, 0, 0, 0}};
/// The next index in `m_utf8_buffer` to read.
Expand Down
Binary file not shown.
Binary file modified test/unit_test/test_data/input_adapter_test_data_utf16ben.txt
Binary file not shown.
Binary file not shown.
Binary file modified test/unit_test/test_data/input_adapter_test_data_utf16len.txt
Binary file not shown.
Loading