summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibDNS/Name.cpp
blob: 39ee9ae0cc0e447d9c6d8a0a478682d445e4dc60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Name.h"
#include <AK/Random.h>
#include <AK/Vector.h>
#include <ctype.h>

namespace DNS {

Name::Name(DeprecatedString const& name)
{
    if (name.ends_with('.'))
        m_name = name.substring(0, name.length() - 1);
    else
        m_name = name;
}

Name Name::parse(u8 const* data, size_t& offset, size_t max_offset, size_t recursion_level)
{
    if (recursion_level > 4)
        return {};

    StringBuilder builder;
    while (true) {
        if (offset >= max_offset)
            return {};
        u8 b = data[offset++];
        if (b == '\0') {
            // This terminates the name.
            return builder.to_deprecated_string();
        } else if ((b & 0xc0) == 0xc0) {
            // The two bytes tell us the offset when to continue from.
            if (offset >= max_offset)
                return {};
            size_t dummy = (b & 0x3f) << 8 | data[offset++];
            auto rest_of_name = parse(data, dummy, max_offset, recursion_level + 1);
            builder.append(rest_of_name.as_string());
            return builder.to_deprecated_string();
        } else {
            // This is the length of a part.
            if (offset + b >= max_offset)
                return {};
            builder.append((char const*)&data[offset], (size_t)b);
            builder.append('.');
            offset += b;
        }
    }
}

size_t Name::serialized_size() const
{
    if (m_name.is_empty())
        return 1;
    return m_name.length() + 2;
}

void Name::randomize_case()
{
    StringBuilder builder;
    for (char c : m_name) {
        // Randomize the 0x20 bit in every ASCII character.
        if (isalpha(c)) {
            if (get_random_uniform(2))
                c |= 0x20;
            else
                c &= ~0x20;
        }
        builder.append(c);
    }
    m_name = builder.to_deprecated_string();
}

ErrorOr<void> Name::write_to_stream(Core::Stream::Stream& stream) const
{
    auto parts = as_string().split_view('.');
    for (auto& part : parts) {
        TRY(stream.write_value<u8>(part.length()));
        TRY(stream.write_entire_buffer(part.bytes()));
    }
    TRY(stream.write_value('\0'));
    return {};
}

unsigned Name::Traits::hash(Name const& name)
{
    return CaseInsensitiveStringTraits::hash(name.as_string());
}

bool Name::Traits::equals(Name const& a, Name const& b)
{
    return CaseInsensitiveStringTraits::equals(a.as_string(), b.as_string());
}

}