AK+Userland: Make AK::decode_hex() return ErrorOr

This lets us propagate the reason why it failed up to the caller. :^)
This commit is contained in:
Sam Atkins 2022-01-20 17:01:39 +00:00 committed by Andreas Kling
parent 45cf40653a
commit f590cd1850
Notes: sideshowbarker 2024-07-17 21:26:19 +09:00
6 changed files with 31 additions and 33 deletions

View file

@ -7,7 +7,7 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Optional.h>
#include <AK/Error.h>
#include <AK/String.h>
#include <AK/StringView.h>

View file

@ -1,44 +1,38 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Array.h>
#include <AK/ByteBuffer.h>
#include <AK/Hex.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <AK/Vector.h>
namespace AK {
Optional<ByteBuffer> decode_hex(StringView input)
ErrorOr<ByteBuffer> decode_hex(StringView input)
{
if ((input.length() % 2) != 0)
return {};
return Error::from_string_literal("Hex string was not an even length");
auto output_result = ByteBuffer::create_zeroed(input.length() / 2);
if (output_result.is_error())
return {};
auto& output = output_result.value();
auto output = TRY(ByteBuffer::create_zeroed(input.length() / 2));
for (size_t i = 0; i < input.length() / 2; ++i) {
const auto c1 = decode_hex_digit(input[i * 2]);
if (c1 >= 16)
return {};
return Error::from_string_literal("Hex string contains invalid digit");
const auto c2 = decode_hex_digit(input[i * 2 + 1]);
if (c2 >= 16)
return {};
return Error::from_string_literal("Hex string contains invalid digit");
output[i] = (c1 << 4) + c2;
}
return output_result.release_value();
return { move(output) };
}
String encode_hex(const ReadonlyBytes input)

View file

@ -7,7 +7,7 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Optional.h>
#include <AK/Error.h>
#include <AK/String.h>
#include <AK/StringView.h>
@ -24,7 +24,7 @@ constexpr u8 decode_hex_digit(char digit)
return 255;
}
Optional<ByteBuffer> decode_hex(StringView);
ErrorOr<ByteBuffer> decode_hex(StringView);
String encode_hex(ReadonlyBytes);

View file

@ -19,21 +19,21 @@ UUID::UUID(Array<u8, 16> uuid_buffer)
void UUID::convert_string_view_to_uuid(StringView uuid_string_view)
{
VERIFY(uuid_string_view.length() == 36);
auto first_unit = decode_hex(uuid_string_view.substring_view(0, 8));
auto second_unit = decode_hex(uuid_string_view.substring_view(9, 4));
auto third_unit = decode_hex(uuid_string_view.substring_view(14, 4));
auto fourth_unit = decode_hex(uuid_string_view.substring_view(19, 4));
auto fifth_unit = decode_hex(uuid_string_view.substring_view(24, 12));
auto first_unit = MUST(decode_hex(uuid_string_view.substring_view(0, 8)));
auto second_unit = MUST(decode_hex(uuid_string_view.substring_view(9, 4)));
auto third_unit = MUST(decode_hex(uuid_string_view.substring_view(14, 4)));
auto fourth_unit = MUST(decode_hex(uuid_string_view.substring_view(19, 4)));
auto fifth_unit = MUST(decode_hex(uuid_string_view.substring_view(24, 12)));
VERIFY(first_unit.value().size() == 4 && second_unit.value().size() == 2
&& third_unit.value().size() == 2 && fourth_unit.value().size() == 2
&& fifth_unit.value().size() == 6);
VERIFY(first_unit.size() == 4 && second_unit.size() == 2
&& third_unit.size() == 2 && fourth_unit.size() == 2
&& fifth_unit.size() == 6);
m_uuid_buffer.span().overwrite(0, first_unit.value().data(), first_unit.value().size());
m_uuid_buffer.span().overwrite(4, second_unit.value().data(), second_unit.value().size());
m_uuid_buffer.span().overwrite(6, third_unit.value().data(), third_unit.value().size());
m_uuid_buffer.span().overwrite(8, fourth_unit.value().data(), fourth_unit.value().size());
m_uuid_buffer.span().overwrite(10, fifth_unit.value().data(), fifth_unit.value().size());
m_uuid_buffer.span().overwrite(0, first_unit.data(), first_unit.size());
m_uuid_buffer.span().overwrite(4, second_unit.data(), second_unit.size());
m_uuid_buffer.span().overwrite(6, third_unit.data(), third_unit.size());
m_uuid_buffer.span().overwrite(8, fourth_unit.data(), fourth_unit.size());
m_uuid_buffer.span().overwrite(10, fifth_unit.data(), fifth_unit.size());
}
UUID::UUID(StringView uuid_string_view)

View file

@ -77,8 +77,8 @@ Result<ByteBuffer, String> FindDialog::process_input(String text_value, OptionId
case OPTION_HEX_VALUE: {
auto decoded = decode_hex(text_value.replace(" ", "", true));
if (!decoded.has_value())
return String("Input contains invalid hex values.");
if (decoded.is_error())
return String::formatted("Input is invalid: {}", decoded.error().string_literal());
return decoded.value();
}

View file

@ -39,8 +39,12 @@ Optional<ByteBuffer> Filter::decode(ReadonlyBytes bytes, FlyString const& encodi
Optional<ByteBuffer> Filter::decode_ascii_hex(ReadonlyBytes bytes)
{
if (bytes.size() % 2 == 0)
return decode_hex(bytes);
if (bytes.size() % 2 == 0) {
auto decode_result = decode_hex(bytes);
if (decode_result.is_error())
return {};
return decode_result.release_value();
}
// FIXME: Integrate this padding into AK/Hex?