2020-12-13 01:35:14 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-13 01:35:14 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2022-01-20 20:01:39 +03:00
|
|
|
#include <AK/Error.h>
|
2020-12-13 01:35:14 +03:00
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
2022-02-16 00:25:25 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
# include <Kernel/KString.h>
|
|
|
|
#else
|
|
|
|
# include <AK/String.h>
|
|
|
|
#endif
|
|
|
|
|
2020-12-13 01:35:14 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2021-04-18 20:06:36 +03:00
|
|
|
constexpr u8 decode_hex_digit(char digit)
|
|
|
|
{
|
|
|
|
if (digit >= '0' && digit <= '9')
|
|
|
|
return digit - '0';
|
|
|
|
if (digit >= 'a' && digit <= 'f')
|
|
|
|
return 10 + (digit - 'a');
|
|
|
|
if (digit >= 'A' && digit <= 'F')
|
|
|
|
return 10 + (digit - 'A');
|
|
|
|
return 255;
|
|
|
|
}
|
2021-04-13 22:49:05 +03:00
|
|
|
|
2022-01-20 20:01:39 +03:00
|
|
|
ErrorOr<ByteBuffer> decode_hex(StringView);
|
2020-12-13 01:35:14 +03:00
|
|
|
|
2022-02-16 00:25:25 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
ErrorOr<NonnullOwnPtr<Kernel::KString>> encode_hex(ReadonlyBytes);
|
|
|
|
#else
|
2020-12-13 01:35:14 +03:00
|
|
|
String encode_hex(ReadonlyBytes);
|
2022-02-16 00:25:25 +03:00
|
|
|
#endif
|
2020-12-13 01:35:14 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-12-13 01:35:14 +03:00
|
|
|
using AK::decode_hex;
|
2021-04-13 22:49:05 +03:00
|
|
|
using AK::decode_hex_digit;
|
2020-12-13 01:35:14 +03:00
|
|
|
using AK::encode_hex;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|