Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/ByteBuffer.h> |
10 | | #include <AK/Error.h> |
11 | | #include <AK/StringView.h> |
12 | | |
13 | | #ifdef KERNEL |
14 | | # include <Kernel/Library/KString.h> |
15 | | #else |
16 | | # include <AK/ByteString.h> |
17 | | #endif |
18 | | |
19 | | namespace AK { |
20 | | |
21 | | constexpr u8 decode_hex_digit(char digit) |
22 | 5.74M | { |
23 | 5.74M | if (digit >= '0' && digit <= '9') |
24 | 2.42M | return digit - '0'; |
25 | 3.31M | if (digit >= 'a' && digit <= 'f') |
26 | 882k | return 10 + (digit - 'a'); |
27 | 2.43M | if (digit >= 'A' && digit <= 'F') |
28 | 1.53M | return 10 + (digit - 'A'); |
29 | 905k | return 255; |
30 | 2.43M | } |
31 | | |
32 | | ErrorOr<ByteBuffer> decode_hex(StringView); |
33 | | |
34 | | #ifdef KERNEL |
35 | | ErrorOr<NonnullOwnPtr<Kernel::KString>> encode_hex(ReadonlyBytes); |
36 | | #else |
37 | | ByteString encode_hex(ReadonlyBytes); |
38 | | #endif |
39 | | |
40 | | } |
41 | | |
42 | | #if USING_AK_GLOBALLY |
43 | | using AK::decode_hex; |
44 | | using AK::decode_hex_digit; |
45 | | using AK::encode_hex; |
46 | | #endif |